/*
* Copyright (c) 1982, 1986, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)uipc_domain.c 8.3 (Berkeley) 2/14/95
*/
/*
* Current time values for fast and slow timeouts. We can use u_int
* relatively safely. The fast timer will roll over in 27 years and
* the slow timer in 68 years.
*/
u_int pfslowtimo_now;
u_int pffasttimo_now;
/*
* Add all of the domains. Make sure the PF_ROUTE
* domain is added last.
*/
if (attach) {
__link_set_foreach(dpp, domains) {
if (*dpp == &domain_dummy)
continue;
if ((*dpp)->dom_family == PF_ROUTE)
rt_domain = *dpp;
else
domain_attach(*dpp);
}
if (rt_domain)
domain_attach(rt_domain);
domain_init_timers();
}
}
/*
* Must be called only if domaininit has been called with false and
* after all domains have been attached.
*/
void
domaininit_post(void)
{
if ((any = sockaddr_any(sa)) == NULL)
return NULL;
return sockaddr_const_addr(any, slenp);
}
socklen_t
sockaddr_getsize_by_family(sa_family_t af)
{
switch (af) {
case AF_INET:
return sizeof(struct sockaddr_in);
case AF_INET6:
return sizeof(struct sockaddr_in6);
case AF_UNIX:
return sizeof(struct sockaddr_un);
case AF_LINK:
return sizeof(struct sockaddr_dl);
case AF_APPLETALK:
return sizeof(struct sockaddr_at);
default:
#ifdef DIAGNOSTIC
printf("%s: (%s:%u:%u) Unhandled address family=%hhu\n",
__func__, curlwp->l_proc->p_comm,
curlwp->l_proc->p_pid, curlwp->l_lid, af);
#endif
return 0;
}
}
#ifdef DIAGNOSTIC
static void
sockaddr_checklen(const struct sockaddr *sa)
{
// Can't tell how much was allocated, if it was allocated.
if (sa->sa_family == AF_LINK)
return;
socklen_t len = sockaddr_getsize_by_family(sa->sa_family);
if (len == 0 || len == sa->sa_len)
return;
char buf[512];
sockaddr_format(sa, buf, sizeof(buf));
printf("%s: %p bad len af=%hhu socklen=%hhu len=%u [%s]\n",
__func__, sa, sa->sa_family, sa->sa_len, (unsigned)len, buf);
}
#else
#define sockaddr_checklen(sa) ((void)0)
#endif
struct sockaddr *
sockaddr_alloc(sa_family_t af, socklen_t socklen, int flags)
{
struct sockaddr *sa;
socklen_t reallen = MAX(socklen, offsetof(struct sockaddr, sa_data[0]));
#ifdef DIAGNOSTIC
/*
* sockaddr_checklen passes sa to sockaddr_format which
* requires it to be fully initialized.
*
* XXX This should be factored better.
*/
flags |= M_ZERO;
#endif
if ((sa = malloc(reallen, M_SOCKADDR, flags)) == NULL)
return NULL;
/*
* allocate dummy file descriptor to make position in list.
*/
sysctl_unlock();
if ((dfp = fgetdummy()) == NULL) {
sysctl_relock();
return SET_ERROR(ENOMEM);
}
/*
* there's no "list" of local domain sockets, so we have
* to walk the file list looking for them. :-/
*/
mutex_enter(&filelist_lock);
LIST_FOREACH_SAFE(fp, &filehead, f_list, np) {
if (fp->f_count == 0 || fp->f_type != DTYPE_SOCKET ||
fp->f_socket == NULL)
continue;
so = fp->f_socket;
if (so->so_type != type)
continue;
if (so->so_proto->pr_domain->dom_family != pf)
continue;
if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_SOCKET,
KAUTH_REQ_NETWORK_SOCKET_CANSEE, so, NULL, NULL) != 0)
continue;
if (len >= elem_size && elem_count > 0) {
mutex_enter(&fp->f_lock);
/*
* Do not add references, if the count reached 0.
* Since the check above has been performed without
* locking, it must be rechecked here as a concurrent
* closef could have reduced it.
*/
if (fp->f_count == 0) {
mutex_exit(&fp->f_lock);
continue;
}
fp->f_count++;
mutex_exit(&fp->f_lock);
LIST_INSERT_AFTER(fp, dfp, f_list);
mutex_exit(&filelist_lock);
sysctl_dounpcb(&pcb, so);
error = copyout(&pcb, dp, out_size);
closef(fp);
mutex_enter(&filelist_lock);
np = LIST_NEXT(dfp, f_list);
LIST_REMOVE(dfp, f_list);
if (error)
break;
dp += elem_size;
len -= elem_size;
}
needed += elem_size;
if (elem_count > 0 && elem_count != INT_MAX)
elem_count--;
}
mutex_exit(&filelist_lock);
fputdummy(dfp);
*oldlenp = needed;
if (oldp == NULL)
*oldlenp += PCB_SLOP * sizeof(struct kinfo_pcb);
sysctl_relock();
DOMAIN_FOREACH(dp) {
/*
* the check must be made by xx_ctlinput() anyways, to
* make sure we use data item pointed to by ctlparam in
* correct way. the following check is made just for safety.
*/
if (dp->dom_family != sa->sa_family)
continue;
for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
if (pr->pr_ctlinput != NULL)
(*pr->pr_ctlinput)(cmd, sa, ctlparam);
}
}
}