Home | History | Annotate | Line # | Download | only in kern
uipc_domain.c revision 1.76.2.4
      1  1.76.2.4      yamt /*	$NetBSD: uipc_domain.c,v 1.76.2.4 2010/03/11 15:04:20 yamt Exp $	*/
      2      1.12       cgd 
      3       1.1       cgd /*
      4      1.11   mycroft  * Copyright (c) 1982, 1986, 1993
      5      1.11   mycroft  *	The Regents of the University of California.  All rights reserved.
      6       1.1       cgd  *
      7       1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8       1.1       cgd  * modification, are permitted provided that the following conditions
      9       1.1       cgd  * are met:
     10       1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11       1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12       1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13       1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14       1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15      1.43       agc  * 3. Neither the name of the University nor the names of its contributors
     16       1.1       cgd  *    may be used to endorse or promote products derived from this software
     17       1.1       cgd  *    without specific prior written permission.
     18       1.1       cgd  *
     19       1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20       1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21       1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22       1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23       1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24       1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25       1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26       1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27       1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28       1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29       1.1       cgd  * SUCH DAMAGE.
     30       1.1       cgd  *
     31      1.18      fvdl  *	@(#)uipc_domain.c	8.3 (Berkeley) 2/14/95
     32       1.1       cgd  */
     33      1.36     lukem 
     34      1.36     lukem #include <sys/cdefs.h>
     35  1.76.2.4      yamt __KERNEL_RCSID(0, "$NetBSD: uipc_domain.c,v 1.76.2.4 2010/03/11 15:04:20 yamt Exp $");
     36       1.1       cgd 
     37       1.5   mycroft #include <sys/param.h>
     38       1.5   mycroft #include <sys/socket.h>
     39      1.50    atatat #include <sys/socketvar.h>
     40       1.5   mycroft #include <sys/protosw.h>
     41       1.5   mycroft #include <sys/domain.h>
     42       1.5   mycroft #include <sys/mbuf.h>
     43       1.5   mycroft #include <sys/time.h>
     44       1.5   mycroft #include <sys/kernel.h>
     45      1.11   mycroft #include <sys/systm.h>
     46      1.30   thorpej #include <sys/callout.h>
     47      1.49      matt #include <sys/queue.h>
     48      1.10       cgd #include <sys/proc.h>
     49      1.10       cgd #include <sys/sysctl.h>
     50      1.50    atatat #include <sys/un.h>
     51      1.50    atatat #include <sys/unpcb.h>
     52      1.50    atatat #include <sys/file.h>
     53      1.72        ad #include <sys/filedesc.h>
     54      1.57      elad #include <sys/kauth.h>
     55      1.13  christos 
     56      1.69    dyoung MALLOC_DECLARE(M_SOCKADDR);
     57      1.69    dyoung 
     58      1.69    dyoung MALLOC_DEFINE(M_SOCKADDR, "sockaddr", "socket endpoints");
     59      1.69    dyoung 
     60      1.45  junyoung void	pffasttimo(void *);
     61      1.45  junyoung void	pfslowtimo(void *);
     62      1.37      matt 
     63      1.49      matt struct domainhead domains = STAILQ_HEAD_INITIALIZER(domains);
     64      1.64    dyoung static struct domain *domain_array[AF_MAX];
     65      1.11   mycroft 
     66      1.66        ad callout_t pffasttimo_ch, pfslowtimo_ch;
     67      1.30   thorpej 
     68      1.19   thorpej /*
     69      1.19   thorpej  * Current time values for fast and slow timeouts.  We can use u_int
     70      1.19   thorpej  * relatively safely.  The fast timer will roll over in 27 years and
     71      1.19   thorpej  * the slow timer in 68 years.
     72      1.19   thorpej  */
     73      1.19   thorpej u_int	pfslowtimo_now;
     74      1.19   thorpej u_int	pffasttimo_now;
     75      1.19   thorpej 
     76  1.76.2.1      yamt static struct sysctllog *domain_sysctllog;
     77  1.76.2.1      yamt static void sysctl_net_setup(void);
     78  1.76.2.1      yamt 
     79      1.49      matt void
     80  1.76.2.2      yamt domaininit(bool addroute)
     81      1.49      matt {
     82      1.49      matt 	__link_set_decl(domains, struct domain);
     83      1.49      matt 	struct domain * const * dpp;
     84      1.49      matt 	struct domain *rt_domain = NULL;
     85      1.49      matt 
     86  1.76.2.1      yamt 	sysctl_net_setup();
     87  1.76.2.1      yamt 
     88      1.49      matt 	/*
     89      1.49      matt 	 * Add all of the domains.  Make sure the PF_ROUTE
     90      1.49      matt 	 * domain is added last.
     91      1.49      matt 	 */
     92      1.49      matt 	__link_set_foreach(dpp, domains) {
     93      1.49      matt 		if ((*dpp)->dom_family == PF_ROUTE)
     94      1.49      matt 			rt_domain = *dpp;
     95      1.49      matt 		else
     96      1.49      matt 			domain_attach(*dpp);
     97      1.49      matt 	}
     98  1.76.2.2      yamt 	if (rt_domain && addroute)
     99      1.49      matt 		domain_attach(rt_domain);
    100      1.49      matt 
    101      1.76        ad 	callout_init(&pffasttimo_ch, CALLOUT_MPSAFE);
    102      1.76        ad 	callout_init(&pfslowtimo_ch, CALLOUT_MPSAFE);
    103      1.49      matt 
    104      1.49      matt 	callout_reset(&pffasttimo_ch, 1, pffasttimo, NULL);
    105      1.49      matt 	callout_reset(&pfslowtimo_ch, 1, pfslowtimo, NULL);
    106       1.1       cgd }
    107       1.1       cgd 
    108       1.4    andrew void
    109      1.49      matt domain_attach(struct domain *dp)
    110       1.1       cgd {
    111      1.47      matt 	const struct protosw *pr;
    112       1.1       cgd 
    113      1.49      matt 	STAILQ_INSERT_TAIL(&domains, dp, dom_link);
    114      1.64    dyoung 	if (dp->dom_family < __arraycount(domain_array))
    115      1.64    dyoung 		domain_array[dp->dom_family] = dp;
    116      1.49      matt 
    117      1.49      matt 	if (dp->dom_init)
    118      1.49      matt 		(*dp->dom_init)();
    119       1.1       cgd 
    120      1.38      matt #ifdef MBUFTRACE
    121      1.49      matt 	if (dp->dom_mowner.mo_name[0] == '\0') {
    122      1.49      matt 		strncpy(dp->dom_mowner.mo_name, dp->dom_name,
    123      1.49      matt 		    sizeof(dp->dom_mowner.mo_name));
    124      1.49      matt 		MOWNER_ATTACH(&dp->dom_mowner);
    125      1.49      matt 	}
    126      1.38      matt #endif
    127      1.49      matt 	for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
    128      1.49      matt 		if (pr->pr_init)
    129      1.49      matt 			(*pr->pr_init)();
    130       1.1       cgd 	}
    131       1.1       cgd 
    132      1.16  explorer 	if (max_linkhdr < 16)		/* XXX */
    133      1.16  explorer 		max_linkhdr = 16;
    134       1.1       cgd 	max_hdr = max_linkhdr + max_protohdr;
    135       1.1       cgd 	max_datalen = MHLEN - max_hdr;
    136       1.1       cgd }
    137       1.1       cgd 
    138      1.29   thorpej struct domain *
    139      1.49      matt pffinddomain(int family)
    140      1.29   thorpej {
    141      1.29   thorpej 	struct domain *dp;
    142      1.29   thorpej 
    143      1.64    dyoung 	if (family < __arraycount(domain_array) && domain_array[family] != NULL)
    144      1.64    dyoung 		return domain_array[family];
    145      1.64    dyoung 
    146      1.49      matt 	DOMAIN_FOREACH(dp)
    147      1.29   thorpej 		if (dp->dom_family == family)
    148  1.76.2.1      yamt 			return dp;
    149  1.76.2.1      yamt 	return NULL;
    150      1.29   thorpej }
    151      1.29   thorpej 
    152      1.47      matt const struct protosw *
    153      1.49      matt pffindtype(int family, int type)
    154       1.1       cgd {
    155      1.29   thorpej 	struct domain *dp;
    156      1.47      matt 	const struct protosw *pr;
    157      1.29   thorpej 
    158      1.29   thorpej 	dp = pffinddomain(family);
    159      1.29   thorpej 	if (dp == NULL)
    160  1.76.2.1      yamt 		return NULL;
    161       1.1       cgd 
    162       1.1       cgd 	for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
    163       1.1       cgd 		if (pr->pr_type && pr->pr_type == type)
    164  1.76.2.1      yamt 			return pr;
    165      1.29   thorpej 
    166  1.76.2.1      yamt 	return NULL;
    167       1.1       cgd }
    168       1.1       cgd 
    169      1.47      matt const struct protosw *
    170      1.49      matt pffindproto(int family, int protocol, int type)
    171       1.1       cgd {
    172      1.29   thorpej 	struct domain *dp;
    173      1.47      matt 	const struct protosw *pr;
    174      1.47      matt 	const struct protosw *maybe = NULL;
    175       1.1       cgd 
    176       1.1       cgd 	if (family == 0)
    177  1.76.2.1      yamt 		return NULL;
    178      1.29   thorpej 
    179      1.29   thorpej 	dp = pffinddomain(family);
    180      1.29   thorpej 	if (dp == NULL)
    181  1.76.2.1      yamt 		return NULL;
    182      1.29   thorpej 
    183       1.1       cgd 	for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
    184       1.1       cgd 		if ((pr->pr_protocol == protocol) && (pr->pr_type == type))
    185  1.76.2.1      yamt 			return pr;
    186       1.1       cgd 
    187       1.1       cgd 		if (type == SOCK_RAW && pr->pr_type == SOCK_RAW &&
    188      1.29   thorpej 		    pr->pr_protocol == 0 && maybe == NULL)
    189       1.1       cgd 			maybe = pr;
    190       1.1       cgd 	}
    191  1.76.2.1      yamt 	return maybe;
    192      1.10       cgd }
    193      1.10       cgd 
    194      1.71    dyoung void *
    195      1.71    dyoung sockaddr_addr(struct sockaddr *sa, socklen_t *slenp)
    196      1.71    dyoung {
    197      1.71    dyoung 	const struct domain *dom;
    198      1.71    dyoung 
    199      1.71    dyoung 	if ((dom = pffinddomain(sa->sa_family)) == NULL ||
    200      1.71    dyoung 	    dom->dom_sockaddr_addr == NULL)
    201      1.71    dyoung 		return NULL;
    202      1.71    dyoung 
    203      1.71    dyoung 	return (*dom->dom_sockaddr_addr)(sa, slenp);
    204      1.71    dyoung }
    205      1.71    dyoung 
    206      1.71    dyoung const void *
    207      1.71    dyoung sockaddr_const_addr(const struct sockaddr *sa, socklen_t *slenp)
    208      1.71    dyoung {
    209      1.71    dyoung 	const struct domain *dom;
    210  1.76.2.3      yamt 
    211      1.71    dyoung 	if ((dom = pffinddomain(sa->sa_family)) == NULL ||
    212      1.71    dyoung 	    dom->dom_sockaddr_const_addr == NULL)
    213      1.71    dyoung 		return NULL;
    214      1.71    dyoung 
    215      1.71    dyoung 	return (*dom->dom_sockaddr_const_addr)(sa, slenp);
    216      1.71    dyoung }
    217      1.71    dyoung 
    218      1.71    dyoung const struct sockaddr *
    219  1.76.2.1      yamt sockaddr_any_by_family(int family)
    220      1.71    dyoung {
    221      1.71    dyoung 	const struct domain *dom;
    222  1.76.2.3      yamt 
    223  1.76.2.1      yamt 	if ((dom = pffinddomain(family)) == NULL)
    224      1.71    dyoung 		return NULL;
    225      1.71    dyoung 
    226      1.71    dyoung 	return dom->dom_sa_any;
    227      1.71    dyoung }
    228      1.71    dyoung 
    229  1.76.2.1      yamt const struct sockaddr *
    230  1.76.2.1      yamt sockaddr_any(const struct sockaddr *sa)
    231  1.76.2.1      yamt {
    232  1.76.2.1      yamt 	return sockaddr_any_by_family(sa->sa_family);
    233  1.76.2.1      yamt }
    234  1.76.2.1      yamt 
    235      1.71    dyoung const void *
    236      1.71    dyoung sockaddr_anyaddr(const struct sockaddr *sa, socklen_t *slenp)
    237      1.71    dyoung {
    238      1.71    dyoung 	const struct sockaddr *any;
    239      1.71    dyoung 
    240      1.71    dyoung 	if ((any = sockaddr_any(sa)) == NULL)
    241      1.71    dyoung 		return NULL;
    242      1.71    dyoung 
    243      1.71    dyoung 	return sockaddr_const_addr(any, slenp);
    244      1.71    dyoung }
    245      1.71    dyoung 
    246      1.64    dyoung struct sockaddr *
    247      1.69    dyoung sockaddr_alloc(sa_family_t af, socklen_t socklen, int flags)
    248      1.64    dyoung {
    249      1.64    dyoung 	struct sockaddr *sa;
    250      1.69    dyoung 	socklen_t reallen = MAX(socklen, offsetof(struct sockaddr, sa_data[0]));
    251      1.64    dyoung 
    252      1.69    dyoung 	if ((sa = malloc(reallen, M_SOCKADDR, flags)) == NULL)
    253      1.64    dyoung 		return NULL;
    254      1.64    dyoung 
    255      1.64    dyoung 	sa->sa_family = af;
    256      1.69    dyoung 	sa->sa_len = reallen;
    257      1.64    dyoung 	return sa;
    258      1.64    dyoung }
    259      1.64    dyoung 
    260      1.64    dyoung struct sockaddr *
    261      1.69    dyoung sockaddr_copy(struct sockaddr *dst, socklen_t socklen,
    262      1.69    dyoung     const struct sockaddr *src)
    263      1.64    dyoung {
    264      1.70    dyoung 	if (__predict_false(socklen < src->sa_len)) {
    265      1.70    dyoung 		panic("%s: source too long, %d < %d bytes", __func__, socklen,
    266      1.70    dyoung 		    src->sa_len);
    267      1.70    dyoung 	}
    268      1.70    dyoung 	return memcpy(dst, src, src->sa_len);
    269      1.64    dyoung }
    270      1.64    dyoung 
    271  1.76.2.3      yamt struct sockaddr *
    272  1.76.2.3      yamt sockaddr_externalize(struct sockaddr *dst, socklen_t socklen,
    273  1.76.2.3      yamt     const struct sockaddr *src)
    274  1.76.2.3      yamt {
    275  1.76.2.3      yamt 	struct domain *dom;
    276  1.76.2.3      yamt 
    277  1.76.2.3      yamt 	dom = pffinddomain(src->sa_family);
    278  1.76.2.3      yamt 
    279  1.76.2.3      yamt 	if (dom != NULL && dom->dom_sockaddr_externalize != NULL)
    280  1.76.2.3      yamt 		return (*dom->dom_sockaddr_externalize)(dst, socklen, src);
    281  1.76.2.3      yamt 
    282  1.76.2.3      yamt 	return sockaddr_copy(dst, socklen, src);
    283  1.76.2.3      yamt }
    284  1.76.2.3      yamt 
    285      1.64    dyoung int
    286      1.64    dyoung sockaddr_cmp(const struct sockaddr *sa1, const struct sockaddr *sa2)
    287      1.64    dyoung {
    288      1.65    dyoung 	int len, rc;
    289      1.64    dyoung 	struct domain *dom;
    290      1.64    dyoung 
    291      1.64    dyoung 	if (sa1->sa_family != sa2->sa_family)
    292      1.64    dyoung 		return sa1->sa_family - sa2->sa_family;
    293      1.64    dyoung 
    294      1.65    dyoung 	dom = pffinddomain(sa1->sa_family);
    295      1.65    dyoung 
    296      1.65    dyoung 	if (dom != NULL && dom->dom_sockaddr_cmp != NULL)
    297      1.64    dyoung 		return (*dom->dom_sockaddr_cmp)(sa1, sa2);
    298      1.64    dyoung 
    299      1.65    dyoung 	len = MIN(sa1->sa_len, sa2->sa_len);
    300      1.65    dyoung 
    301      1.65    dyoung 	if (dom == NULL || dom->dom_sa_cmplen == 0) {
    302      1.65    dyoung 		if ((rc = memcmp(sa1, sa2, len)) != 0)
    303      1.65    dyoung 			return rc;
    304      1.65    dyoung 		return sa1->sa_len - sa2->sa_len;
    305      1.65    dyoung 	}
    306      1.65    dyoung 
    307      1.65    dyoung 	if ((rc = memcmp((const char *)sa1 + dom->dom_sa_cmpofs,
    308      1.65    dyoung 		         (const char *)sa2 + dom->dom_sa_cmpofs,
    309      1.65    dyoung 			 MIN(dom->dom_sa_cmplen,
    310      1.65    dyoung 			     len - MIN(len, dom->dom_sa_cmpofs)))) != 0)
    311      1.64    dyoung 		return rc;
    312      1.64    dyoung 
    313      1.65    dyoung 	return MIN(dom->dom_sa_cmplen + dom->dom_sa_cmpofs, sa1->sa_len) -
    314      1.65    dyoung 	       MIN(dom->dom_sa_cmplen + dom->dom_sa_cmpofs, sa2->sa_len);
    315      1.64    dyoung }
    316      1.64    dyoung 
    317      1.64    dyoung struct sockaddr *
    318      1.64    dyoung sockaddr_dup(const struct sockaddr *src, int flags)
    319      1.64    dyoung {
    320      1.64    dyoung 	struct sockaddr *dst;
    321      1.64    dyoung 
    322      1.69    dyoung 	if ((dst = sockaddr_alloc(src->sa_family, src->sa_len, flags)) == NULL)
    323      1.64    dyoung 		return NULL;
    324      1.64    dyoung 
    325      1.69    dyoung 	return sockaddr_copy(dst, dst->sa_len, src);
    326      1.64    dyoung }
    327      1.64    dyoung 
    328      1.64    dyoung void
    329      1.64    dyoung sockaddr_free(struct sockaddr *sa)
    330      1.64    dyoung {
    331      1.69    dyoung 	free(sa, M_SOCKADDR);
    332      1.64    dyoung }
    333      1.64    dyoung 
    334      1.50    atatat /*
    335      1.50    atatat  * sysctl helper to stuff PF_LOCAL pcbs into sysctl structures
    336      1.50    atatat  */
    337      1.50    atatat static void
    338      1.50    atatat sysctl_dounpcb(struct kinfo_pcb *pcb, const struct socket *so)
    339      1.50    atatat {
    340      1.50    atatat 	struct unpcb *unp = sotounpcb(so);
    341      1.50    atatat 	struct sockaddr_un *un = unp->unp_addr;
    342      1.50    atatat 
    343      1.50    atatat 	memset(pcb, 0, sizeof(*pcb));
    344      1.50    atatat 
    345      1.50    atatat 	pcb->ki_family = so->so_proto->pr_domain->dom_family;
    346      1.50    atatat 	pcb->ki_type = so->so_proto->pr_type;
    347      1.50    atatat 	pcb->ki_protocol = so->so_proto->pr_protocol;
    348      1.50    atatat 	pcb->ki_pflags = unp->unp_flags;
    349      1.50    atatat 
    350      1.50    atatat 	pcb->ki_pcbaddr = PTRTOUINT64(unp);
    351      1.50    atatat 	/* pcb->ki_ppcbaddr = unp has no ppcb... */
    352      1.50    atatat 	pcb->ki_sockaddr = PTRTOUINT64(so);
    353      1.50    atatat 
    354      1.50    atatat 	pcb->ki_sostate = so->so_state;
    355      1.50    atatat 	/* pcb->ki_prstate = unp has no state... */
    356      1.50    atatat 
    357      1.50    atatat 	pcb->ki_rcvq = so->so_rcv.sb_cc;
    358      1.50    atatat 	pcb->ki_sndq = so->so_snd.sb_cc;
    359      1.50    atatat 
    360      1.50    atatat 	un = (struct sockaddr_un *)&pcb->ki_src;
    361      1.50    atatat 	/*
    362      1.50    atatat 	 * local domain sockets may bind without having a local
    363      1.50    atatat 	 * endpoint.  bleah!
    364      1.50    atatat 	 */
    365      1.50    atatat 	if (unp->unp_addr != NULL) {
    366      1.50    atatat 		un->sun_len = unp->unp_addr->sun_len;
    367      1.50    atatat 		un->sun_family = unp->unp_addr->sun_family;
    368      1.50    atatat 		strlcpy(un->sun_path, unp->unp_addr->sun_path,
    369      1.50    atatat 		    sizeof(pcb->ki_s));
    370      1.50    atatat 	}
    371      1.50    atatat 	else {
    372      1.50    atatat 		un->sun_len = offsetof(struct sockaddr_un, sun_path);
    373      1.50    atatat 		un->sun_family = pcb->ki_family;
    374      1.50    atatat 	}
    375      1.50    atatat 	if (unp->unp_conn != NULL) {
    376      1.50    atatat 		un = (struct sockaddr_un *)&pcb->ki_dst;
    377      1.50    atatat 		if (unp->unp_conn->unp_addr != NULL) {
    378      1.50    atatat 			un->sun_len = unp->unp_conn->unp_addr->sun_len;
    379      1.50    atatat 			un->sun_family = unp->unp_conn->unp_addr->sun_family;
    380      1.50    atatat 			un->sun_family = unp->unp_conn->unp_addr->sun_family;
    381      1.50    atatat 			strlcpy(un->sun_path, unp->unp_conn->unp_addr->sun_path,
    382      1.50    atatat 				sizeof(pcb->ki_d));
    383      1.50    atatat 		}
    384      1.50    atatat 		else {
    385      1.50    atatat 			un->sun_len = offsetof(struct sockaddr_un, sun_path);
    386      1.50    atatat 			un->sun_family = pcb->ki_family;
    387      1.50    atatat 		}
    388      1.50    atatat 	}
    389      1.50    atatat 
    390      1.50    atatat 	pcb->ki_inode = unp->unp_ino;
    391      1.50    atatat 	pcb->ki_vnode = PTRTOUINT64(unp->unp_vnode);
    392      1.50    atatat 	pcb->ki_conn = PTRTOUINT64(unp->unp_conn);
    393      1.50    atatat 	pcb->ki_refs = PTRTOUINT64(unp->unp_refs);
    394      1.50    atatat 	pcb->ki_nextref = PTRTOUINT64(unp->unp_nextref);
    395      1.50    atatat }
    396      1.50    atatat 
    397      1.50    atatat static int
    398      1.50    atatat sysctl_unpcblist(SYSCTLFN_ARGS)
    399      1.50    atatat {
    400      1.74        ad 	struct file *fp, *dfp, *np;
    401      1.50    atatat 	struct socket *so;
    402      1.50    atatat 	struct kinfo_pcb pcb;
    403      1.50    atatat 	char *dp;
    404      1.50    atatat 	u_int op, arg;
    405      1.50    atatat 	size_t len, needed, elem_size, out_size;
    406      1.50    atatat 	int error, elem_count, pf, type, pf2;
    407      1.50    atatat 
    408      1.50    atatat 	if (namelen == 1 && name[0] == CTL_QUERY)
    409  1.76.2.1      yamt 		return sysctl_query(SYSCTLFN_CALL(rnode));
    410      1.50    atatat 
    411      1.50    atatat 	if (namelen != 4)
    412  1.76.2.1      yamt 		return EINVAL;
    413      1.50    atatat 
    414      1.56  christos 	if (oldp != NULL) {
    415      1.56  christos 		len = *oldlenp;
    416      1.56  christos 		elem_size = name[2];
    417      1.56  christos 		elem_count = name[3];
    418      1.56  christos 		if (elem_size != sizeof(pcb))
    419      1.56  christos 			return EINVAL;
    420      1.56  christos 	} else {
    421      1.56  christos 		len = 0;
    422      1.56  christos 		elem_size = sizeof(pcb);
    423      1.56  christos 		elem_count = INT_MAX;
    424      1.56  christos 	}
    425      1.50    atatat 	error = 0;
    426      1.50    atatat 	dp = oldp;
    427      1.50    atatat 	op = name[0];
    428      1.50    atatat 	arg = name[1];
    429      1.56  christos 	out_size = elem_size;
    430      1.50    atatat 	needed = 0;
    431      1.50    atatat 
    432      1.50    atatat 	if (name - oname != 4)
    433  1.76.2.1      yamt 		return EINVAL;
    434      1.50    atatat 
    435      1.50    atatat 	pf = oname[1];
    436      1.50    atatat 	type = oname[2];
    437      1.50    atatat 	pf2 = (oldp == NULL) ? 0 : pf;
    438      1.50    atatat 
    439      1.50    atatat 	/*
    440      1.74        ad 	 * allocate dummy file descriptor to make position in list.
    441      1.74        ad 	 */
    442      1.74        ad 	sysctl_unlock();
    443      1.74        ad 	if ((dfp = fgetdummy()) == NULL) {
    444      1.74        ad 	 	sysctl_relock();
    445      1.74        ad 		return ENOMEM;
    446      1.74        ad 	}
    447      1.74        ad 
    448      1.74        ad 	/*
    449      1.50    atatat 	 * there's no "list" of local domain sockets, so we have
    450      1.50    atatat 	 * to walk the file list looking for them.  :-/
    451      1.50    atatat 	 */
    452      1.72        ad 	mutex_enter(&filelist_lock);
    453      1.50    atatat 	LIST_FOREACH(fp, &filehead, f_list) {
    454      1.74        ad 	    	np = LIST_NEXT(fp, f_list);
    455      1.74        ad 		if (fp->f_count == 0 || fp->f_type != DTYPE_SOCKET ||
    456      1.74        ad 		    fp->f_data == NULL)
    457      1.74        ad 			continue;
    458      1.50    atatat 		so = (struct socket *)fp->f_data;
    459      1.50    atatat 		if (so->so_type != type)
    460      1.50    atatat 			continue;
    461      1.50    atatat 		if (so->so_proto->pr_domain->dom_family != pf)
    462      1.50    atatat 			continue;
    463  1.76.2.4      yamt 		if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_SOCKET,
    464  1.76.2.4      yamt 		    KAUTH_REQ_NETWORK_SOCKET_CANSEE, so, NULL, NULL) != 0)
    465  1.76.2.4      yamt 			continue;
    466      1.50    atatat 		if (len >= elem_size && elem_count > 0) {
    467      1.75        ad 			mutex_enter(&fp->f_lock);
    468      1.75        ad 			fp->f_count++;
    469      1.75        ad 			mutex_exit(&fp->f_lock);
    470      1.74        ad 			LIST_INSERT_AFTER(fp, dfp, f_list);
    471      1.72        ad 			mutex_exit(&filelist_lock);
    472      1.50    atatat 			sysctl_dounpcb(&pcb, so);
    473      1.50    atatat 			error = copyout(&pcb, dp, out_size);
    474      1.75        ad 			closef(fp);
    475      1.72        ad 			mutex_enter(&filelist_lock);
    476      1.74        ad 			np = LIST_NEXT(dfp, f_list);
    477      1.74        ad 			LIST_REMOVE(dfp, f_list);
    478      1.50    atatat 			if (error)
    479      1.50    atatat 				break;
    480      1.50    atatat 			dp += elem_size;
    481      1.50    atatat 			len -= elem_size;
    482      1.50    atatat 		}
    483  1.76.2.1      yamt 		needed += elem_size;
    484  1.76.2.1      yamt 		if (elem_count > 0 && elem_count != INT_MAX)
    485  1.76.2.1      yamt 			elem_count--;
    486      1.50    atatat 	}
    487      1.72        ad 	mutex_exit(&filelist_lock);
    488      1.74        ad 	fputdummy(dfp);
    489      1.74        ad  	*oldlenp = needed;
    490      1.50    atatat 	if (oldp == NULL)
    491      1.50    atatat 		*oldlenp += PCB_SLOP * sizeof(struct kinfo_pcb);
    492      1.74        ad  	sysctl_relock();
    493      1.50    atatat 
    494  1.76.2.1      yamt 	return error;
    495      1.50    atatat }
    496      1.50    atatat 
    497  1.76.2.1      yamt static void
    498  1.76.2.1      yamt sysctl_net_setup(void)
    499      1.10       cgd {
    500  1.76.2.1      yamt 
    501  1.76.2.1      yamt 	KASSERT(domain_sysctllog == NULL);
    502  1.76.2.1      yamt 	sysctl_createv(&domain_sysctllog, 0, NULL, NULL,
    503      1.46    atatat 		       CTLFLAG_PERMANENT,
    504      1.44    atatat 		       CTLTYPE_NODE, "net", NULL,
    505      1.44    atatat 		       NULL, 0, NULL, 0,
    506      1.44    atatat 		       CTL_NET, CTL_EOL);
    507  1.76.2.1      yamt 	sysctl_createv(&domain_sysctllog, 0, NULL, NULL,
    508      1.46    atatat 		       CTLFLAG_PERMANENT,
    509      1.48    atatat 		       CTLTYPE_NODE, "local",
    510      1.48    atatat 		       SYSCTL_DESCR("PF_LOCAL related settings"),
    511      1.44    atatat 		       NULL, 0, NULL, 0,
    512      1.44    atatat 		       CTL_NET, PF_LOCAL, CTL_EOL);
    513  1.76.2.1      yamt 	sysctl_createv(&domain_sysctllog, 0, NULL, NULL,
    514      1.50    atatat 		       CTLFLAG_PERMANENT,
    515      1.50    atatat 		       CTLTYPE_NODE, "stream",
    516      1.50    atatat 		       SYSCTL_DESCR("SOCK_STREAM settings"),
    517      1.50    atatat 		       NULL, 0, NULL, 0,
    518      1.50    atatat 		       CTL_NET, PF_LOCAL, SOCK_STREAM, CTL_EOL);
    519  1.76.2.1      yamt 	sysctl_createv(&domain_sysctllog, 0, NULL, NULL,
    520      1.50    atatat 		       CTLFLAG_PERMANENT,
    521      1.50    atatat 		       CTLTYPE_NODE, "dgram",
    522      1.50    atatat 		       SYSCTL_DESCR("SOCK_DGRAM settings"),
    523      1.50    atatat 		       NULL, 0, NULL, 0,
    524      1.50    atatat 		       CTL_NET, PF_LOCAL, SOCK_DGRAM, CTL_EOL);
    525      1.10       cgd 
    526  1.76.2.1      yamt 	sysctl_createv(&domain_sysctllog, 0, NULL, NULL,
    527      1.50    atatat 		       CTLFLAG_PERMANENT,
    528      1.50    atatat 		       CTLTYPE_STRUCT, "pcblist",
    529      1.50    atatat 		       SYSCTL_DESCR("SOCK_STREAM protocol control block list"),
    530      1.50    atatat 		       sysctl_unpcblist, 0, NULL, 0,
    531      1.50    atatat 		       CTL_NET, PF_LOCAL, SOCK_STREAM, CTL_CREATE, CTL_EOL);
    532  1.76.2.1      yamt 	sysctl_createv(&domain_sysctllog, 0, NULL, NULL,
    533      1.50    atatat 		       CTLFLAG_PERMANENT,
    534      1.50    atatat 		       CTLTYPE_STRUCT, "pcblist",
    535      1.50    atatat 		       SYSCTL_DESCR("SOCK_DGRAM protocol control block list"),
    536      1.50    atatat 		       sysctl_unpcblist, 0, NULL, 0,
    537      1.50    atatat 		       CTL_NET, PF_LOCAL, SOCK_DGRAM, CTL_CREATE, CTL_EOL);
    538       1.1       cgd }
    539       1.1       cgd 
    540       1.4    andrew void
    541      1.63    dyoung pfctlinput(int cmd, const struct sockaddr *sa)
    542       1.1       cgd {
    543      1.31  augustss 	struct domain *dp;
    544      1.47      matt 	const struct protosw *pr;
    545       1.1       cgd 
    546      1.63    dyoung 	DOMAIN_FOREACH(dp) {
    547      1.63    dyoung 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
    548      1.63    dyoung 			if (pr->pr_ctlinput != NULL)
    549      1.13  christos 				(*pr->pr_ctlinput)(cmd, sa, NULL);
    550      1.63    dyoung 		}
    551      1.63    dyoung 	}
    552      1.34    itojun }
    553      1.34    itojun 
    554      1.34    itojun void
    555      1.63    dyoung pfctlinput2(int cmd, const struct sockaddr *sa, void *ctlparam)
    556      1.34    itojun {
    557      1.34    itojun 	struct domain *dp;
    558      1.47      matt 	const struct protosw *pr;
    559      1.34    itojun 
    560      1.63    dyoung 	if (sa == NULL)
    561      1.34    itojun 		return;
    562      1.49      matt 
    563      1.49      matt 	DOMAIN_FOREACH(dp) {
    564      1.34    itojun 		/*
    565      1.34    itojun 		 * the check must be made by xx_ctlinput() anyways, to
    566      1.34    itojun 		 * make sure we use data item pointed to by ctlparam in
    567      1.34    itojun 		 * correct way.  the following check is made just for safety.
    568      1.34    itojun 		 */
    569      1.34    itojun 		if (dp->dom_family != sa->sa_family)
    570      1.34    itojun 			continue;
    571      1.34    itojun 
    572      1.63    dyoung 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
    573      1.63    dyoung 			if (pr->pr_ctlinput != NULL)
    574      1.34    itojun 				(*pr->pr_ctlinput)(cmd, sa, ctlparam);
    575      1.63    dyoung 		}
    576      1.34    itojun 	}
    577       1.1       cgd }
    578       1.1       cgd 
    579       1.4    andrew void
    580      1.62      yamt pfslowtimo(void *arg)
    581       1.1       cgd {
    582      1.31  augustss 	struct domain *dp;
    583      1.47      matt 	const struct protosw *pr;
    584       1.1       cgd 
    585      1.19   thorpej 	pfslowtimo_now++;
    586      1.19   thorpej 
    587      1.49      matt 	DOMAIN_FOREACH(dp) {
    588       1.1       cgd 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
    589       1.1       cgd 			if (pr->pr_slowtimo)
    590       1.1       cgd 				(*pr->pr_slowtimo)();
    591      1.49      matt 	}
    592      1.76        ad 	callout_schedule(&pfslowtimo_ch, hz / 2);
    593       1.1       cgd }
    594       1.1       cgd 
    595       1.4    andrew void
    596      1.62      yamt pffasttimo(void *arg)
    597       1.1       cgd {
    598      1.31  augustss 	struct domain *dp;
    599      1.47      matt 	const struct protosw *pr;
    600      1.19   thorpej 
    601      1.19   thorpej 	pffasttimo_now++;
    602       1.1       cgd 
    603      1.49      matt 	DOMAIN_FOREACH(dp) {
    604       1.1       cgd 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
    605       1.1       cgd 			if (pr->pr_fasttimo)
    606       1.1       cgd 				(*pr->pr_fasttimo)();
    607      1.49      matt 	}
    608      1.76        ad 	callout_schedule(&pffasttimo_ch, hz / 5);
    609       1.1       cgd }
    610