Home | History | Annotate | Line # | Download | only in kern
uipc_domain.c revision 1.63.4.3
      1  1.63.4.3        ad /*	$NetBSD: uipc_domain.c,v 1.63.4.3 2007/08/20 21:27:41 ad 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.63.4.3        ad __KERNEL_RCSID(0, "$NetBSD: uipc_domain.c,v 1.63.4.3 2007/08/20 21:27:41 ad 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.57      elad #include <sys/kauth.h>
     54      1.13  christos 
     55      1.45  junyoung void	pffasttimo(void *);
     56      1.45  junyoung void	pfslowtimo(void *);
     57      1.37      matt 
     58      1.49      matt struct domainhead domains = STAILQ_HEAD_INITIALIZER(domains);
     59  1.63.4.1        ad static struct domain *domain_array[AF_MAX];
     60      1.11   mycroft 
     61  1.63.4.2        ad callout_t pffasttimo_ch, pfslowtimo_ch;
     62      1.30   thorpej 
     63      1.19   thorpej /*
     64      1.19   thorpej  * Current time values for fast and slow timeouts.  We can use u_int
     65      1.19   thorpej  * relatively safely.  The fast timer will roll over in 27 years and
     66      1.19   thorpej  * the slow timer in 68 years.
     67      1.19   thorpej  */
     68      1.19   thorpej u_int	pfslowtimo_now;
     69      1.19   thorpej u_int	pffasttimo_now;
     70      1.19   thorpej 
     71      1.49      matt void
     72      1.55   thorpej domaininit(void)
     73      1.49      matt {
     74      1.49      matt 	__link_set_decl(domains, struct domain);
     75      1.49      matt 	struct domain * const * dpp;
     76      1.49      matt 	struct domain *rt_domain = NULL;
     77      1.49      matt 
     78      1.49      matt 	/*
     79      1.49      matt 	 * Add all of the domains.  Make sure the PF_ROUTE
     80      1.49      matt 	 * domain is added last.
     81      1.49      matt 	 */
     82      1.49      matt 	__link_set_foreach(dpp, domains) {
     83      1.49      matt 		if ((*dpp)->dom_family == PF_ROUTE)
     84      1.49      matt 			rt_domain = *dpp;
     85      1.49      matt 		else
     86      1.49      matt 			domain_attach(*dpp);
     87      1.49      matt 	}
     88      1.49      matt 	if (rt_domain)
     89      1.49      matt 		domain_attach(rt_domain);
     90      1.49      matt 
     91  1.63.4.2        ad 	callout_init(&pffasttimo_ch, 0);
     92  1.63.4.2        ad 	callout_init(&pfslowtimo_ch, 0);
     93      1.49      matt 
     94      1.49      matt 	callout_reset(&pffasttimo_ch, 1, pffasttimo, NULL);
     95      1.49      matt 	callout_reset(&pfslowtimo_ch, 1, pfslowtimo, NULL);
     96       1.1       cgd }
     97       1.1       cgd 
     98       1.4    andrew void
     99      1.49      matt domain_attach(struct domain *dp)
    100       1.1       cgd {
    101      1.47      matt 	const struct protosw *pr;
    102       1.1       cgd 
    103      1.49      matt 	STAILQ_INSERT_TAIL(&domains, dp, dom_link);
    104  1.63.4.1        ad 	if (dp->dom_family < __arraycount(domain_array))
    105  1.63.4.1        ad 		domain_array[dp->dom_family] = dp;
    106      1.49      matt 
    107      1.49      matt 	if (dp->dom_init)
    108      1.49      matt 		(*dp->dom_init)();
    109       1.1       cgd 
    110      1.38      matt #ifdef MBUFTRACE
    111      1.49      matt 	if (dp->dom_mowner.mo_name[0] == '\0') {
    112      1.49      matt 		strncpy(dp->dom_mowner.mo_name, dp->dom_name,
    113      1.49      matt 		    sizeof(dp->dom_mowner.mo_name));
    114      1.49      matt 		MOWNER_ATTACH(&dp->dom_mowner);
    115      1.49      matt 	}
    116      1.38      matt #endif
    117      1.49      matt 	for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
    118      1.49      matt 		if (pr->pr_init)
    119      1.49      matt 			(*pr->pr_init)();
    120       1.1       cgd 	}
    121       1.1       cgd 
    122  1.63.4.3        ad 	if (dp->dom_sa_pool != NULL) {
    123  1.63.4.3        ad 		pool_setlowat(dp->dom_sa_pool, 32);
    124  1.63.4.3        ad 		if (pool_prime(dp->dom_sa_pool, 32) != 0)
    125  1.63.4.3        ad 			printf("%s: pool_prime failed\n", __func__);
    126  1.63.4.3        ad 	}
    127  1.63.4.3        ad 
    128      1.16  explorer 	if (max_linkhdr < 16)		/* XXX */
    129      1.16  explorer 		max_linkhdr = 16;
    130       1.1       cgd 	max_hdr = max_linkhdr + max_protohdr;
    131       1.1       cgd 	max_datalen = MHLEN - max_hdr;
    132       1.1       cgd }
    133       1.1       cgd 
    134      1.29   thorpej struct domain *
    135      1.49      matt pffinddomain(int family)
    136      1.29   thorpej {
    137      1.29   thorpej 	struct domain *dp;
    138      1.29   thorpej 
    139  1.63.4.1        ad 	if (family < __arraycount(domain_array) && domain_array[family] != NULL)
    140  1.63.4.1        ad 		return domain_array[family];
    141  1.63.4.1        ad 
    142      1.49      matt 	DOMAIN_FOREACH(dp)
    143      1.29   thorpej 		if (dp->dom_family == family)
    144      1.29   thorpej 			return (dp);
    145      1.29   thorpej 	return (NULL);
    146      1.29   thorpej }
    147      1.29   thorpej 
    148      1.47      matt const struct protosw *
    149      1.49      matt pffindtype(int family, int type)
    150       1.1       cgd {
    151      1.29   thorpej 	struct domain *dp;
    152      1.47      matt 	const struct protosw *pr;
    153      1.29   thorpej 
    154      1.29   thorpej 	dp = pffinddomain(family);
    155      1.29   thorpej 	if (dp == NULL)
    156      1.29   thorpej 		return (NULL);
    157       1.1       cgd 
    158       1.1       cgd 	for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
    159       1.1       cgd 		if (pr->pr_type && pr->pr_type == type)
    160       1.1       cgd 			return (pr);
    161      1.29   thorpej 
    162      1.29   thorpej 	return (NULL);
    163       1.1       cgd }
    164       1.1       cgd 
    165      1.47      matt const struct protosw *
    166      1.49      matt pffindproto(int family, int protocol, int type)
    167       1.1       cgd {
    168      1.29   thorpej 	struct domain *dp;
    169      1.47      matt 	const struct protosw *pr;
    170      1.47      matt 	const struct protosw *maybe = NULL;
    171       1.1       cgd 
    172       1.1       cgd 	if (family == 0)
    173      1.29   thorpej 		return (NULL);
    174      1.29   thorpej 
    175      1.29   thorpej 	dp = pffinddomain(family);
    176      1.29   thorpej 	if (dp == NULL)
    177      1.29   thorpej 		return (NULL);
    178      1.29   thorpej 
    179       1.1       cgd 	for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
    180       1.1       cgd 		if ((pr->pr_protocol == protocol) && (pr->pr_type == type))
    181       1.1       cgd 			return (pr);
    182       1.1       cgd 
    183       1.1       cgd 		if (type == SOCK_RAW && pr->pr_type == SOCK_RAW &&
    184      1.29   thorpej 		    pr->pr_protocol == 0 && maybe == NULL)
    185       1.1       cgd 			maybe = pr;
    186       1.1       cgd 	}
    187       1.1       cgd 	return (maybe);
    188      1.10       cgd }
    189      1.10       cgd 
    190  1.63.4.1        ad struct sockaddr *
    191  1.63.4.1        ad sockaddr_alloc(sa_family_t af, int flags)
    192  1.63.4.1        ad {
    193  1.63.4.1        ad 	const struct domain *dom;
    194  1.63.4.1        ad 	struct sockaddr *sa;
    195  1.63.4.1        ad 
    196  1.63.4.1        ad 	if ((dom = pffinddomain(af)) == NULL)
    197  1.63.4.1        ad 		return NULL;
    198  1.63.4.1        ad 
    199  1.63.4.1        ad 	if ((sa = pool_get(dom->dom_sa_pool, flags)) == NULL)
    200  1.63.4.1        ad 		return NULL;
    201  1.63.4.1        ad 
    202  1.63.4.1        ad 	sa->sa_family = af;
    203  1.63.4.1        ad 	sa->sa_len = dom->dom_sa_len;
    204  1.63.4.1        ad 	return sa;
    205  1.63.4.1        ad }
    206  1.63.4.1        ad 
    207  1.63.4.3        ad static void
    208  1.63.4.3        ad sockaddr_fixlen(struct sockaddr *dst, uint8_t deslen)
    209  1.63.4.3        ad {
    210  1.63.4.3        ad 	struct domain *dom;
    211  1.63.4.3        ad 
    212  1.63.4.3        ad 	if ((dom = pffinddomain(dst->sa_family)) == NULL)
    213  1.63.4.3        ad 		panic("%s: unknown domain %d", __func__, dst->sa_family);
    214  1.63.4.3        ad 	if (dom->dom_sa_len < deslen)
    215  1.63.4.3        ad 		panic("%s: source too long, %d bytes", __func__, deslen);
    216  1.63.4.3        ad 	dst->sa_len = dom->dom_sa_len;
    217  1.63.4.3        ad }
    218  1.63.4.3        ad 
    219  1.63.4.1        ad struct sockaddr *
    220  1.63.4.1        ad sockaddr_copy(struct sockaddr *dst, const struct sockaddr *src)
    221  1.63.4.1        ad {
    222  1.63.4.1        ad 	KASSERT(dst->sa_family == src->sa_family);
    223  1.63.4.3        ad 
    224  1.63.4.3        ad 	if (__predict_false(dst->sa_len < src->sa_len))
    225  1.63.4.3        ad 		sockaddr_fixlen(dst, src->sa_len);
    226  1.63.4.3        ad 
    227  1.63.4.1        ad 	memcpy(dst, src, src->sa_len);
    228  1.63.4.3        ad 
    229  1.63.4.1        ad 	return dst;
    230  1.63.4.1        ad }
    231  1.63.4.1        ad 
    232  1.63.4.1        ad int
    233  1.63.4.1        ad sockaddr_cmp(const struct sockaddr *sa1, const struct sockaddr *sa2)
    234  1.63.4.1        ad {
    235  1.63.4.1        ad 	int len, rc;
    236  1.63.4.1        ad 	struct domain *dom;
    237  1.63.4.1        ad 
    238  1.63.4.1        ad 	if (sa1->sa_family != sa2->sa_family)
    239  1.63.4.1        ad 		return sa1->sa_family - sa2->sa_family;
    240  1.63.4.1        ad 
    241  1.63.4.1        ad 	dom = pffinddomain(sa1->sa_family);
    242  1.63.4.1        ad 
    243  1.63.4.1        ad 	if (dom != NULL && dom->dom_sockaddr_cmp != NULL)
    244  1.63.4.1        ad 		return (*dom->dom_sockaddr_cmp)(sa1, sa2);
    245  1.63.4.1        ad 
    246  1.63.4.1        ad 	len = MIN(sa1->sa_len, sa2->sa_len);
    247  1.63.4.1        ad 
    248  1.63.4.1        ad 	if (dom == NULL || dom->dom_sa_cmplen == 0) {
    249  1.63.4.1        ad 		if ((rc = memcmp(sa1, sa2, len)) != 0)
    250  1.63.4.1        ad 			return rc;
    251  1.63.4.1        ad 		return sa1->sa_len - sa2->sa_len;
    252  1.63.4.1        ad 	}
    253  1.63.4.1        ad 
    254  1.63.4.1        ad 	if ((rc = memcmp((const char *)sa1 + dom->dom_sa_cmpofs,
    255  1.63.4.1        ad 		         (const char *)sa2 + dom->dom_sa_cmpofs,
    256  1.63.4.1        ad 			 MIN(dom->dom_sa_cmplen,
    257  1.63.4.1        ad 			     len - MIN(len, dom->dom_sa_cmpofs)))) != 0)
    258  1.63.4.1        ad 		return rc;
    259  1.63.4.1        ad 
    260  1.63.4.1        ad 	return MIN(dom->dom_sa_cmplen + dom->dom_sa_cmpofs, sa1->sa_len) -
    261  1.63.4.1        ad 	       MIN(dom->dom_sa_cmplen + dom->dom_sa_cmpofs, sa2->sa_len);
    262  1.63.4.1        ad }
    263  1.63.4.1        ad 
    264  1.63.4.1        ad struct sockaddr *
    265  1.63.4.1        ad sockaddr_dup(const struct sockaddr *src, int flags)
    266  1.63.4.1        ad {
    267  1.63.4.1        ad 	struct sockaddr *dst;
    268  1.63.4.1        ad 
    269  1.63.4.1        ad 	if ((dst = sockaddr_alloc(src->sa_family, flags)) == NULL)
    270  1.63.4.1        ad 		return NULL;
    271  1.63.4.1        ad 
    272  1.63.4.1        ad 	KASSERT(dst->sa_len >= src->sa_len);
    273  1.63.4.1        ad 
    274  1.63.4.1        ad 	return sockaddr_copy(dst, src);
    275  1.63.4.1        ad }
    276  1.63.4.1        ad 
    277  1.63.4.1        ad void
    278  1.63.4.1        ad sockaddr_free(struct sockaddr *sa)
    279  1.63.4.1        ad {
    280  1.63.4.1        ad 	const struct domain *dom;
    281  1.63.4.1        ad 
    282  1.63.4.1        ad 	if ((dom = pffinddomain(sa->sa_family)) == NULL)
    283  1.63.4.1        ad 		panic("%s: no such domain %d\n", __func__, sa->sa_family);
    284  1.63.4.1        ad 
    285  1.63.4.1        ad 	pool_put(dom->dom_sa_pool, sa);
    286  1.63.4.1        ad }
    287  1.63.4.1        ad 
    288      1.50    atatat /*
    289      1.50    atatat  * sysctl helper to stuff PF_LOCAL pcbs into sysctl structures
    290      1.50    atatat  */
    291      1.50    atatat static void
    292      1.50    atatat sysctl_dounpcb(struct kinfo_pcb *pcb, const struct socket *so)
    293      1.50    atatat {
    294      1.50    atatat 	struct unpcb *unp = sotounpcb(so);
    295      1.50    atatat 	struct sockaddr_un *un = unp->unp_addr;
    296      1.50    atatat 
    297      1.50    atatat 	memset(pcb, 0, sizeof(*pcb));
    298      1.50    atatat 
    299      1.50    atatat 	pcb->ki_family = so->so_proto->pr_domain->dom_family;
    300      1.50    atatat 	pcb->ki_type = so->so_proto->pr_type;
    301      1.50    atatat 	pcb->ki_protocol = so->so_proto->pr_protocol;
    302      1.50    atatat 	pcb->ki_pflags = unp->unp_flags;
    303      1.50    atatat 
    304      1.50    atatat 	pcb->ki_pcbaddr = PTRTOUINT64(unp);
    305      1.50    atatat 	/* pcb->ki_ppcbaddr = unp has no ppcb... */
    306      1.50    atatat 	pcb->ki_sockaddr = PTRTOUINT64(so);
    307      1.50    atatat 
    308      1.50    atatat 	pcb->ki_sostate = so->so_state;
    309      1.50    atatat 	/* pcb->ki_prstate = unp has no state... */
    310      1.50    atatat 
    311      1.50    atatat 	pcb->ki_rcvq = so->so_rcv.sb_cc;
    312      1.50    atatat 	pcb->ki_sndq = so->so_snd.sb_cc;
    313      1.50    atatat 
    314      1.50    atatat 	un = (struct sockaddr_un *)&pcb->ki_src;
    315      1.50    atatat 	/*
    316      1.50    atatat 	 * local domain sockets may bind without having a local
    317      1.50    atatat 	 * endpoint.  bleah!
    318      1.50    atatat 	 */
    319      1.50    atatat 	if (unp->unp_addr != NULL) {
    320      1.50    atatat 		un->sun_len = unp->unp_addr->sun_len;
    321      1.50    atatat 		un->sun_family = unp->unp_addr->sun_family;
    322      1.50    atatat 		strlcpy(un->sun_path, unp->unp_addr->sun_path,
    323      1.50    atatat 		    sizeof(pcb->ki_s));
    324      1.50    atatat 	}
    325      1.50    atatat 	else {
    326      1.50    atatat 		un->sun_len = offsetof(struct sockaddr_un, sun_path);
    327      1.50    atatat 		un->sun_family = pcb->ki_family;
    328      1.50    atatat 	}
    329      1.50    atatat 	if (unp->unp_conn != NULL) {
    330      1.50    atatat 		un = (struct sockaddr_un *)&pcb->ki_dst;
    331      1.50    atatat 		if (unp->unp_conn->unp_addr != NULL) {
    332      1.50    atatat 			un->sun_len = unp->unp_conn->unp_addr->sun_len;
    333      1.50    atatat 			un->sun_family = unp->unp_conn->unp_addr->sun_family;
    334      1.50    atatat 			un->sun_family = unp->unp_conn->unp_addr->sun_family;
    335      1.50    atatat 			strlcpy(un->sun_path, unp->unp_conn->unp_addr->sun_path,
    336      1.50    atatat 				sizeof(pcb->ki_d));
    337      1.50    atatat 		}
    338      1.50    atatat 		else {
    339      1.50    atatat 			un->sun_len = offsetof(struct sockaddr_un, sun_path);
    340      1.50    atatat 			un->sun_family = pcb->ki_family;
    341      1.50    atatat 		}
    342      1.50    atatat 	}
    343      1.50    atatat 
    344      1.50    atatat 	pcb->ki_inode = unp->unp_ino;
    345      1.50    atatat 	pcb->ki_vnode = PTRTOUINT64(unp->unp_vnode);
    346      1.50    atatat 	pcb->ki_conn = PTRTOUINT64(unp->unp_conn);
    347      1.50    atatat 	pcb->ki_refs = PTRTOUINT64(unp->unp_refs);
    348      1.50    atatat 	pcb->ki_nextref = PTRTOUINT64(unp->unp_nextref);
    349      1.50    atatat }
    350      1.50    atatat 
    351      1.50    atatat static int
    352      1.50    atatat sysctl_unpcblist(SYSCTLFN_ARGS)
    353      1.50    atatat {
    354      1.50    atatat 	struct file *fp;
    355      1.50    atatat 	struct socket *so;
    356      1.50    atatat 	struct kinfo_pcb pcb;
    357      1.50    atatat 	char *dp;
    358      1.50    atatat 	u_int op, arg;
    359      1.50    atatat 	size_t len, needed, elem_size, out_size;
    360      1.50    atatat 	int error, elem_count, pf, type, pf2;
    361      1.50    atatat 
    362      1.50    atatat 	if (namelen == 1 && name[0] == CTL_QUERY)
    363      1.52    atatat 		return (sysctl_query(SYSCTLFN_CALL(rnode)));
    364      1.50    atatat 
    365      1.50    atatat 	if (namelen != 4)
    366      1.50    atatat 		return (EINVAL);
    367      1.50    atatat 
    368      1.56  christos 	if (oldp != NULL) {
    369      1.56  christos 		len = *oldlenp;
    370      1.56  christos 		elem_size = name[2];
    371      1.56  christos 		elem_count = name[3];
    372      1.56  christos 		if (elem_size != sizeof(pcb))
    373      1.56  christos 			return EINVAL;
    374      1.56  christos 	} else {
    375      1.56  christos 		len = 0;
    376      1.56  christos 		elem_size = sizeof(pcb);
    377      1.56  christos 		elem_count = INT_MAX;
    378      1.56  christos 	}
    379      1.50    atatat 	error = 0;
    380      1.50    atatat 	dp = oldp;
    381      1.50    atatat 	op = name[0];
    382      1.50    atatat 	arg = name[1];
    383      1.56  christos 	out_size = elem_size;
    384      1.50    atatat 	needed = 0;
    385      1.50    atatat 
    386      1.50    atatat 	if (name - oname != 4)
    387      1.50    atatat 		return (EINVAL);
    388      1.50    atatat 
    389      1.50    atatat 	pf = oname[1];
    390      1.50    atatat 	type = oname[2];
    391      1.50    atatat 	pf2 = (oldp == NULL) ? 0 : pf;
    392      1.50    atatat 
    393      1.50    atatat 	/*
    394      1.50    atatat 	 * there's no "list" of local domain sockets, so we have
    395      1.50    atatat 	 * to walk the file list looking for them.  :-/
    396      1.50    atatat 	 */
    397      1.50    atatat 	LIST_FOREACH(fp, &filehead, f_list) {
    398      1.60        ad 		if (kauth_authorize_generic(l->l_cred,
    399      1.59      elad 		    KAUTH_GENERIC_CANSEE, fp->f_cred) != 0)
    400      1.53      elad 			continue;
    401      1.50    atatat 		if (fp->f_type != DTYPE_SOCKET)
    402      1.50    atatat 			continue;
    403      1.50    atatat 		so = (struct socket *)fp->f_data;
    404      1.50    atatat 		if (so->so_type != type)
    405      1.50    atatat 			continue;
    406      1.50    atatat 		if (so->so_proto->pr_domain->dom_family != pf)
    407      1.50    atatat 			continue;
    408      1.50    atatat 		if (len >= elem_size && elem_count > 0) {
    409      1.50    atatat 			sysctl_dounpcb(&pcb, so);
    410      1.50    atatat 			error = copyout(&pcb, dp, out_size);
    411      1.50    atatat 			if (error)
    412      1.50    atatat 				break;
    413      1.50    atatat 			dp += elem_size;
    414      1.50    atatat 			len -= elem_size;
    415      1.50    atatat 		}
    416      1.50    atatat 		if (elem_count > 0) {
    417      1.50    atatat 			needed += elem_size;
    418      1.50    atatat 			if (elem_count != INT_MAX)
    419      1.50    atatat 				elem_count--;
    420      1.50    atatat 		}
    421      1.50    atatat 	}
    422      1.50    atatat 
    423      1.50    atatat 	*oldlenp = needed;
    424      1.50    atatat 	if (oldp == NULL)
    425      1.50    atatat 		*oldlenp += PCB_SLOP * sizeof(struct kinfo_pcb);
    426      1.50    atatat 
    427      1.50    atatat 	return (error);
    428      1.50    atatat }
    429      1.50    atatat 
    430      1.44    atatat SYSCTL_SETUP(sysctl_net_setup, "sysctl net subtree setup")
    431      1.10       cgd {
    432      1.46    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    433      1.46    atatat 		       CTLFLAG_PERMANENT,
    434      1.44    atatat 		       CTLTYPE_NODE, "net", NULL,
    435      1.44    atatat 		       NULL, 0, NULL, 0,
    436      1.44    atatat 		       CTL_NET, CTL_EOL);
    437      1.46    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    438      1.46    atatat 		       CTLFLAG_PERMANENT,
    439      1.48    atatat 		       CTLTYPE_NODE, "local",
    440      1.48    atatat 		       SYSCTL_DESCR("PF_LOCAL related settings"),
    441      1.44    atatat 		       NULL, 0, NULL, 0,
    442      1.44    atatat 		       CTL_NET, PF_LOCAL, CTL_EOL);
    443      1.50    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    444      1.50    atatat 		       CTLFLAG_PERMANENT,
    445      1.50    atatat 		       CTLTYPE_NODE, "stream",
    446      1.50    atatat 		       SYSCTL_DESCR("SOCK_STREAM settings"),
    447      1.50    atatat 		       NULL, 0, NULL, 0,
    448      1.50    atatat 		       CTL_NET, PF_LOCAL, SOCK_STREAM, CTL_EOL);
    449      1.50    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    450      1.50    atatat 		       CTLFLAG_PERMANENT,
    451      1.50    atatat 		       CTLTYPE_NODE, "dgram",
    452      1.50    atatat 		       SYSCTL_DESCR("SOCK_DGRAM settings"),
    453      1.50    atatat 		       NULL, 0, NULL, 0,
    454      1.50    atatat 		       CTL_NET, PF_LOCAL, SOCK_DGRAM, CTL_EOL);
    455      1.10       cgd 
    456      1.50    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    457      1.50    atatat 		       CTLFLAG_PERMANENT,
    458      1.50    atatat 		       CTLTYPE_STRUCT, "pcblist",
    459      1.50    atatat 		       SYSCTL_DESCR("SOCK_STREAM protocol control block list"),
    460      1.50    atatat 		       sysctl_unpcblist, 0, NULL, 0,
    461      1.50    atatat 		       CTL_NET, PF_LOCAL, SOCK_STREAM, CTL_CREATE, CTL_EOL);
    462      1.50    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    463      1.50    atatat 		       CTLFLAG_PERMANENT,
    464      1.50    atatat 		       CTLTYPE_STRUCT, "pcblist",
    465      1.50    atatat 		       SYSCTL_DESCR("SOCK_DGRAM protocol control block list"),
    466      1.50    atatat 		       sysctl_unpcblist, 0, NULL, 0,
    467      1.50    atatat 		       CTL_NET, PF_LOCAL, SOCK_DGRAM, CTL_CREATE, CTL_EOL);
    468       1.1       cgd }
    469       1.1       cgd 
    470       1.4    andrew void
    471      1.63    dyoung pfctlinput(int cmd, const struct sockaddr *sa)
    472       1.1       cgd {
    473      1.31  augustss 	struct domain *dp;
    474      1.47      matt 	const struct protosw *pr;
    475       1.1       cgd 
    476      1.63    dyoung 	DOMAIN_FOREACH(dp) {
    477      1.63    dyoung 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
    478      1.63    dyoung 			if (pr->pr_ctlinput != NULL)
    479      1.13  christos 				(*pr->pr_ctlinput)(cmd, sa, NULL);
    480      1.63    dyoung 		}
    481      1.63    dyoung 	}
    482      1.34    itojun }
    483      1.34    itojun 
    484      1.34    itojun void
    485      1.63    dyoung pfctlinput2(int cmd, const struct sockaddr *sa, void *ctlparam)
    486      1.34    itojun {
    487      1.34    itojun 	struct domain *dp;
    488      1.47      matt 	const struct protosw *pr;
    489      1.34    itojun 
    490      1.63    dyoung 	if (sa == NULL)
    491      1.34    itojun 		return;
    492      1.49      matt 
    493      1.49      matt 	DOMAIN_FOREACH(dp) {
    494      1.34    itojun 		/*
    495      1.34    itojun 		 * the check must be made by xx_ctlinput() anyways, to
    496      1.34    itojun 		 * make sure we use data item pointed to by ctlparam in
    497      1.34    itojun 		 * correct way.  the following check is made just for safety.
    498      1.34    itojun 		 */
    499      1.34    itojun 		if (dp->dom_family != sa->sa_family)
    500      1.34    itojun 			continue;
    501      1.34    itojun 
    502      1.63    dyoung 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
    503      1.63    dyoung 			if (pr->pr_ctlinput != NULL)
    504      1.34    itojun 				(*pr->pr_ctlinput)(cmd, sa, ctlparam);
    505      1.63    dyoung 		}
    506      1.34    itojun 	}
    507       1.1       cgd }
    508       1.1       cgd 
    509       1.4    andrew void
    510      1.62      yamt pfslowtimo(void *arg)
    511       1.1       cgd {
    512      1.31  augustss 	struct domain *dp;
    513      1.47      matt 	const struct protosw *pr;
    514       1.1       cgd 
    515      1.19   thorpej 	pfslowtimo_now++;
    516      1.19   thorpej 
    517      1.49      matt 	DOMAIN_FOREACH(dp) {
    518       1.1       cgd 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
    519       1.1       cgd 			if (pr->pr_slowtimo)
    520       1.1       cgd 				(*pr->pr_slowtimo)();
    521      1.49      matt 	}
    522      1.30   thorpej 	callout_reset(&pfslowtimo_ch, hz / 2, pfslowtimo, NULL);
    523       1.1       cgd }
    524       1.1       cgd 
    525       1.4    andrew void
    526      1.62      yamt pffasttimo(void *arg)
    527       1.1       cgd {
    528      1.31  augustss 	struct domain *dp;
    529      1.47      matt 	const struct protosw *pr;
    530      1.19   thorpej 
    531      1.19   thorpej 	pffasttimo_now++;
    532       1.1       cgd 
    533      1.49      matt 	DOMAIN_FOREACH(dp) {
    534       1.1       cgd 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
    535       1.1       cgd 			if (pr->pr_fasttimo)
    536       1.1       cgd 				(*pr->pr_fasttimo)();
    537      1.49      matt 	}
    538      1.30   thorpej 	callout_reset(&pffasttimo_ch, hz / 5, pffasttimo, NULL);
    539       1.1       cgd }
    540