Home | History | Annotate | Line # | Download | only in kern
uipc_domain.c revision 1.110
      1 /*	$NetBSD: uipc_domain.c,v 1.110 2024/12/06 18:36:31 riastradh Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1982, 1986, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  *
     31  *	@(#)uipc_domain.c	8.3 (Berkeley) 2/14/95
     32  */
     33 
     34 #include <sys/cdefs.h>
     35 __KERNEL_RCSID(0, "$NetBSD: uipc_domain.c,v 1.110 2024/12/06 18:36:31 riastradh Exp $");
     36 
     37 #include <sys/param.h>
     38 #include <sys/types.h>
     39 
     40 #include <sys/callout.h>
     41 #include <sys/domain.h>
     42 #include <sys/file.h>
     43 #include <sys/filedesc.h>
     44 #include <sys/kauth.h>
     45 #include <sys/kernel.h>
     46 #include <sys/mbuf.h>
     47 #include <sys/proc.h>
     48 #include <sys/protosw.h>
     49 #include <sys/queue.h>
     50 #include <sys/socket.h>
     51 #include <sys/socketvar.h>
     52 #include <sys/sysctl.h>
     53 #include <sys/systm.h>
     54 #include <sys/time.h>
     55 #include <sys/un.h>
     56 #include <sys/unpcb.h>
     57 
     58 #include <net/if_dl.h>
     59 #include <netatalk/at.h>
     60 #include <netinet/in.h>
     61 
     62 MALLOC_DECLARE(M_SOCKADDR);
     63 
     64 MALLOC_DEFINE(M_SOCKADDR, "sockaddr", "socket endpoints");
     65 
     66 void	pffasttimo(void *);
     67 void	pfslowtimo(void *);
     68 
     69 struct domainhead domains = STAILQ_HEAD_INITIALIZER(domains);
     70 static struct domain *domain_array[AF_MAX];
     71 
     72 callout_t pffasttimo_ch, pfslowtimo_ch;
     73 
     74 /*
     75  * Current time values for fast and slow timeouts.  We can use u_int
     76  * relatively safely.  The fast timer will roll over in 27 years and
     77  * the slow timer in 68 years.
     78  */
     79 u_int	pfslowtimo_now;
     80 u_int	pffasttimo_now;
     81 
     82 static struct sysctllog *domain_sysctllog;
     83 static void sysctl_net_setup(void);
     84 
     85 /* ensure successful linkage even without any domains in link sets */
     86 static struct domain domain_dummy;
     87 __link_set_add_rodata(domains,domain_dummy);
     88 
     89 static void
     90 domain_init_timers(void)
     91 {
     92 
     93 	callout_init(&pffasttimo_ch, CALLOUT_MPSAFE);
     94 	callout_init(&pfslowtimo_ch, CALLOUT_MPSAFE);
     95 
     96 	callout_reset(&pffasttimo_ch, 1, pffasttimo, NULL);
     97 	callout_reset(&pfslowtimo_ch, 1, pfslowtimo, NULL);
     98 }
     99 
    100 void
    101 domaininit(bool attach)
    102 {
    103 	__link_set_decl(domains, struct domain);
    104 	struct domain * const * dpp;
    105 	struct domain *rt_domain = NULL;
    106 
    107 	sysctl_net_setup();
    108 
    109 	/*
    110 	 * Add all of the domains.  Make sure the PF_ROUTE
    111 	 * domain is added last.
    112 	 */
    113 	if (attach) {
    114 		__link_set_foreach(dpp, domains) {
    115 			if (*dpp == &domain_dummy)
    116 				continue;
    117 			if ((*dpp)->dom_family == PF_ROUTE)
    118 				rt_domain = *dpp;
    119 			else
    120 				domain_attach(*dpp);
    121 		}
    122 		if (rt_domain)
    123 			domain_attach(rt_domain);
    124 
    125 		domain_init_timers();
    126 	}
    127 }
    128 
    129 /*
    130  * Must be called only if domaininit has been called with false and
    131  * after all domains have been attached.
    132  */
    133 void
    134 domaininit_post(void)
    135 {
    136 
    137 	domain_init_timers();
    138 }
    139 
    140 void
    141 domain_attach(struct domain *dp)
    142 {
    143 	const struct protosw *pr;
    144 
    145 	STAILQ_INSERT_TAIL(&domains, dp, dom_link);
    146 	if (dp->dom_family < __arraycount(domain_array))
    147 		domain_array[dp->dom_family] = dp;
    148 
    149 	if (dp->dom_init)
    150 		(*dp->dom_init)();
    151 
    152 #ifdef MBUFTRACE
    153 	if (dp->dom_mowner.mo_name[0] == '\0') {
    154 		strncpy(dp->dom_mowner.mo_name, dp->dom_name,
    155 		    sizeof(dp->dom_mowner.mo_name));
    156 		MOWNER_ATTACH(&dp->dom_mowner);
    157 	}
    158 #endif
    159 	for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
    160 		if (pr->pr_init)
    161 			(*pr->pr_init)();
    162 	}
    163 
    164 	if (max_linkhdr < 16)		/* XXX */
    165 		max_linkhdr = 16;
    166 	max_hdr = max_linkhdr + max_protohdr;
    167 	max_datalen = MHLEN - max_hdr;
    168 }
    169 
    170 struct domain *
    171 pffinddomain(int family)
    172 {
    173 	struct domain *dp;
    174 
    175 	if (family < __arraycount(domain_array) && domain_array[family] != NULL)
    176 		return domain_array[family];
    177 
    178 	DOMAIN_FOREACH(dp)
    179 		if (dp->dom_family == family)
    180 			return dp;
    181 	return NULL;
    182 }
    183 
    184 const struct protosw *
    185 pffindtype(int family, int type)
    186 {
    187 	struct domain *dp;
    188 	const struct protosw *pr;
    189 
    190 	dp = pffinddomain(family);
    191 	if (dp == NULL)
    192 		return NULL;
    193 
    194 	for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
    195 		if (pr->pr_type && pr->pr_type == type)
    196 			return pr;
    197 
    198 	return NULL;
    199 }
    200 
    201 const struct protosw *
    202 pffindproto(int family, int protocol, int type)
    203 {
    204 	struct domain *dp;
    205 	const struct protosw *pr;
    206 	const struct protosw *maybe = NULL;
    207 
    208 	if (family == 0)
    209 		return NULL;
    210 
    211 	dp = pffinddomain(family);
    212 	if (dp == NULL)
    213 		return NULL;
    214 
    215 	for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
    216 		if ((pr->pr_protocol == protocol) && (pr->pr_type == type))
    217 			return pr;
    218 
    219 		if (type == SOCK_RAW && pr->pr_type == SOCK_RAW &&
    220 		    pr->pr_protocol == 0 && maybe == NULL)
    221 			maybe = pr;
    222 	}
    223 	return maybe;
    224 }
    225 
    226 void *
    227 sockaddr_addr(struct sockaddr *sa, socklen_t *slenp)
    228 {
    229 	const struct domain *dom;
    230 
    231 	if ((dom = pffinddomain(sa->sa_family)) == NULL ||
    232 	    dom->dom_sockaddr_addr == NULL)
    233 		return NULL;
    234 
    235 	return (*dom->dom_sockaddr_addr)(sa, slenp);
    236 }
    237 
    238 const void *
    239 sockaddr_const_addr(const struct sockaddr *sa, socklen_t *slenp)
    240 {
    241 	const struct domain *dom;
    242 
    243 	if ((dom = pffinddomain(sa->sa_family)) == NULL ||
    244 	    dom->dom_sockaddr_const_addr == NULL)
    245 		return NULL;
    246 
    247 	return (*dom->dom_sockaddr_const_addr)(sa, slenp);
    248 }
    249 
    250 const struct sockaddr *
    251 sockaddr_any_by_family(sa_family_t family)
    252 {
    253 	const struct domain *dom;
    254 
    255 	if ((dom = pffinddomain(family)) == NULL)
    256 		return NULL;
    257 
    258 	return dom->dom_sa_any;
    259 }
    260 
    261 const struct sockaddr *
    262 sockaddr_any(const struct sockaddr *sa)
    263 {
    264 	return sockaddr_any_by_family(sa->sa_family);
    265 }
    266 
    267 const void *
    268 sockaddr_anyaddr(const struct sockaddr *sa, socklen_t *slenp)
    269 {
    270 	const struct sockaddr *any;
    271 
    272 	if ((any = sockaddr_any(sa)) == NULL)
    273 		return NULL;
    274 
    275 	return sockaddr_const_addr(any, slenp);
    276 }
    277 
    278 socklen_t
    279 sockaddr_getsize_by_family(sa_family_t af)
    280 {
    281 	switch (af) {
    282 	case AF_INET:
    283 		return sizeof(struct sockaddr_in);
    284 	case AF_INET6:
    285 		return sizeof(struct sockaddr_in6);
    286 	case AF_UNIX:
    287 		return sizeof(struct sockaddr_un);
    288 	case AF_LINK:
    289 		return sizeof(struct sockaddr_dl);
    290 	case AF_APPLETALK:
    291 		return sizeof(struct sockaddr_at);
    292 	default:
    293 #ifdef DIAGNOSTIC
    294 		printf("%s: (%s:%u:%u) Unhandled address family=%hhu\n",
    295 		    __func__, curlwp->l_proc->p_comm,
    296 		    curlwp->l_proc->p_pid, curlwp->l_lid, af);
    297 #endif
    298 		return 0;
    299 	}
    300 }
    301 
    302 #ifdef DIAGNOSTIC
    303 static void
    304 sockaddr_checklen(const struct sockaddr *sa)
    305 {
    306 	// Can't tell how much was allocated, if it was allocated.
    307 	if (sa->sa_family == AF_LINK)
    308 		return;
    309 
    310 	socklen_t len = sockaddr_getsize_by_family(sa->sa_family);
    311 	if (len == 0 || len == sa->sa_len)
    312 		return;
    313 
    314 	char buf[512];
    315 	sockaddr_format(sa, buf, sizeof(buf));
    316 	printf("%s: %p bad len af=%hhu socklen=%hhu len=%u [%s]\n",
    317 	    __func__, sa, sa->sa_family, sa->sa_len, (unsigned)len, buf);
    318 }
    319 #else
    320 #define sockaddr_checklen(sa) ((void)0)
    321 #endif
    322 
    323 struct sockaddr *
    324 sockaddr_alloc(sa_family_t af, socklen_t socklen, int flags)
    325 {
    326 	struct sockaddr *sa;
    327 	socklen_t reallen = MAX(socklen, offsetof(struct sockaddr, sa_data[0]));
    328 
    329 #ifdef DIAGNOSTIC
    330 	/*
    331 	 * sockaddr_checklen passes sa to sockaddr_format which
    332 	 * requires it to be fully initialized.
    333 	 *
    334 	 * XXX This should be factored better.
    335 	 */
    336 	flags |= M_ZERO;
    337 #endif
    338 	if ((sa = malloc(reallen, M_SOCKADDR, flags)) == NULL)
    339 		return NULL;
    340 
    341 	sa->sa_family = af;
    342 	sa->sa_len = reallen;
    343 	sockaddr_checklen(sa);
    344 	return sa;
    345 }
    346 
    347 struct sockaddr *
    348 sockaddr_copy(struct sockaddr *dst, socklen_t socklen,
    349     const struct sockaddr *src)
    350 {
    351 	if (__predict_false(socklen < src->sa_len)) {
    352 		panic("%s: source too long, %d < %d bytes", __func__, socklen,
    353 		    src->sa_len);
    354 	}
    355 	sockaddr_checklen(src);
    356 	return memcpy(dst, src, src->sa_len);
    357 }
    358 
    359 struct sockaddr *
    360 sockaddr_externalize(struct sockaddr *dst, socklen_t socklen,
    361     const struct sockaddr *src)
    362 {
    363 	struct domain *dom;
    364 
    365 	dom = pffinddomain(src->sa_family);
    366 
    367 	if (dom != NULL && dom->dom_sockaddr_externalize != NULL)
    368 		return (*dom->dom_sockaddr_externalize)(dst, socklen, src);
    369 
    370 	return sockaddr_copy(dst, socklen, src);
    371 }
    372 
    373 int
    374 sockaddr_cmp(const struct sockaddr *sa1, const struct sockaddr *sa2)
    375 {
    376 	int len, rc;
    377 	struct domain *dom;
    378 
    379 	if (sa1->sa_family != sa2->sa_family)
    380 		return sa1->sa_family - sa2->sa_family;
    381 
    382 	dom = pffinddomain(sa1->sa_family);
    383 
    384 	if (dom != NULL && dom->dom_sockaddr_cmp != NULL)
    385 		return (*dom->dom_sockaddr_cmp)(sa1, sa2);
    386 
    387 	len = MIN(sa1->sa_len, sa2->sa_len);
    388 
    389 	if (dom == NULL || dom->dom_sa_cmplen == 0) {
    390 		if ((rc = memcmp(sa1, sa2, len)) != 0)
    391 			return rc;
    392 		return sa1->sa_len - sa2->sa_len;
    393 	}
    394 
    395 	if ((rc = memcmp((const char *)sa1 + dom->dom_sa_cmpofs,
    396 		         (const char *)sa2 + dom->dom_sa_cmpofs,
    397 			 MIN(dom->dom_sa_cmplen,
    398 			     len - MIN(len, dom->dom_sa_cmpofs)))) != 0)
    399 		return rc;
    400 
    401 	return MIN(dom->dom_sa_cmplen + dom->dom_sa_cmpofs, sa1->sa_len) -
    402 	       MIN(dom->dom_sa_cmplen + dom->dom_sa_cmpofs, sa2->sa_len);
    403 }
    404 
    405 struct sockaddr *
    406 sockaddr_dup(const struct sockaddr *src, int flags)
    407 {
    408 	struct sockaddr *dst;
    409 
    410 	if ((dst = sockaddr_alloc(src->sa_family, src->sa_len, flags)) == NULL)
    411 		return NULL;
    412 
    413 	return sockaddr_copy(dst, dst->sa_len, src);
    414 }
    415 
    416 void
    417 sockaddr_free(struct sockaddr *sa)
    418 {
    419 	free(sa, M_SOCKADDR);
    420 }
    421 
    422 static int
    423 sun_print(char *buf, size_t len, const void *v)
    424 {
    425 	const struct sockaddr_un *sun = v;
    426 	size_t plen;
    427 
    428 	KASSERT(sun->sun_len >= offsetof(struct sockaddr_un, sun_path[0]));
    429 	plen = sun->sun_len - offsetof(struct sockaddr_un, sun_path[0]);
    430 
    431 	len = MIN(len, plen);
    432 
    433 	return snprintf(buf, len, "%s", sun->sun_path);
    434 }
    435 
    436 int
    437 sockaddr_format(const struct sockaddr *sa, char *buf, size_t len)
    438 {
    439 	size_t plen = 0;
    440 
    441 	if (sa == NULL)
    442 		return strlcpy(buf, "(null)", len);
    443 
    444 	switch (sa->sa_family) {
    445 	case AF_LOCAL:
    446 		plen = strlcpy(buf, "unix: ", len);
    447 		break;
    448 	case AF_INET:
    449 		plen = strlcpy(buf, "inet: ", len);
    450 		break;
    451 	case AF_INET6:
    452 		plen = strlcpy(buf, "inet6: ", len);
    453 		break;
    454 	case AF_LINK:
    455 		plen = strlcpy(buf, "link: ", len);
    456 		break;
    457 	case AF_APPLETALK:
    458 		plen = strlcpy(buf, "atalk: ", len);
    459 		break;
    460 	default:
    461 		return snprintf(buf, len, "(unknown socket family %d)",
    462 		    (int)sa->sa_family);
    463 	}
    464 
    465 	buf += plen;
    466 	if (plen > len)
    467 		len = 0;
    468 	else
    469 		len -= plen;
    470 
    471 	switch (sa->sa_family) {
    472 	case AF_LOCAL:
    473 		return sun_print(buf, len, sa);
    474 	case AF_INET:
    475 		return sin_print(buf, len, sa);
    476 	case AF_INET6:
    477 		return sin6_print(buf, len, sa);
    478 	case AF_LINK:
    479 		return sdl_print(buf, len, sa);
    480 	case AF_APPLETALK:
    481 		return sat_print(buf, len, sa);
    482 	default:
    483 		panic("bad family %hhu", sa->sa_family);
    484 	}
    485 }
    486 
    487 /*
    488  * sysctl helper to stuff PF_LOCAL pcbs into sysctl structures
    489  */
    490 static void
    491 sysctl_dounpcb(struct kinfo_pcb *pcb, const struct socket *so)
    492 {
    493 	const bool allowaddr = get_expose_address(curproc);
    494 	struct unpcb *unp = sotounpcb(so);
    495 	struct sockaddr_un *un = unp->unp_addr;
    496 
    497 	memset(pcb, 0, sizeof(*pcb));
    498 
    499 	pcb->ki_family = so->so_proto->pr_domain->dom_family;
    500 	pcb->ki_type = so->so_proto->pr_type;
    501 	pcb->ki_protocol = so->so_proto->pr_protocol;
    502 	pcb->ki_pflags = unp->unp_flags;
    503 
    504 	COND_SET_VALUE(pcb->ki_pcbaddr, PTRTOUINT64(unp), allowaddr);
    505 	/* pcb->ki_ppcbaddr = unp has no ppcb... */
    506 	COND_SET_VALUE(pcb->ki_sockaddr, PTRTOUINT64(so), allowaddr);
    507 
    508 	pcb->ki_sostate = so->so_state;
    509 	/* pcb->ki_prstate = unp has no state... */
    510 
    511 	pcb->ki_rcvq = so->so_rcv.sb_cc;
    512 	pcb->ki_sndq = so->so_snd.sb_cc;
    513 
    514 	un = (struct sockaddr_un *)pcb->ki_spad;
    515 	/*
    516 	 * local domain sockets may bind without having a local
    517 	 * endpoint.  bleah!
    518 	 */
    519 	if (unp->unp_addr != NULL) {
    520 		/*
    521 		 * We've added one to sun_len when allocating to
    522 		 * hold terminating NUL which we want here.  See
    523 		 * makeun().
    524 		 */
    525 		memcpy(un, unp->unp_addr,
    526 		    uimin(sizeof(pcb->ki_spad), unp->unp_addr->sun_len + 1));
    527 	}
    528 	else {
    529 		un->sun_len = offsetof(struct sockaddr_un, sun_path);
    530 		un->sun_family = pcb->ki_family;
    531 	}
    532 	if (unp->unp_conn != NULL) {
    533 		un = (struct sockaddr_un *)pcb->ki_dpad;
    534 		if (unp->unp_conn->unp_addr != NULL) {
    535 			memcpy(un, unp->unp_conn->unp_addr,
    536 			    uimin(sizeof(pcb->ki_dpad), unp->unp_conn->unp_addr->sun_len + 1));
    537 		}
    538 		else {
    539 			un->sun_len = offsetof(struct sockaddr_un, sun_path);
    540 			un->sun_family = pcb->ki_family;
    541 		}
    542 	}
    543 
    544 	pcb->ki_inode = unp->unp_ino;
    545 	COND_SET_VALUE(pcb->ki_vnode, PTRTOUINT64(unp->unp_vnode), allowaddr);
    546 	COND_SET_VALUE(pcb->ki_conn, PTRTOUINT64(unp->unp_conn), allowaddr);
    547 	COND_SET_VALUE(pcb->ki_refs, PTRTOUINT64(unp->unp_refs), allowaddr);
    548 	COND_SET_VALUE(pcb->ki_nextref, PTRTOUINT64(unp->unp_nextref),
    549 	    allowaddr);
    550 }
    551 
    552 static int
    553 sysctl_unpcblist(SYSCTLFN_ARGS)
    554 {
    555 	struct file *fp, *np, *dfp;
    556 	struct socket *so;
    557 	struct kinfo_pcb pcb;
    558 	char *dp;
    559 	size_t len, needed, elem_size, out_size;
    560 	int error, elem_count, pf, type;
    561 
    562 	if (namelen == 1 && name[0] == CTL_QUERY)
    563 		return sysctl_query(SYSCTLFN_CALL(rnode));
    564 
    565 	if (namelen != 4)
    566 		return EINVAL;
    567 
    568 	if (oldp != NULL) {
    569 		len = *oldlenp;
    570 		elem_size = name[2];
    571 		elem_count = name[3];
    572 		if (elem_size != sizeof(pcb))
    573 			return EINVAL;
    574 	} else {
    575 		len = 0;
    576 		elem_size = sizeof(pcb);
    577 		elem_count = INT_MAX;
    578 	}
    579 	error = 0;
    580 	dp = oldp;
    581 	out_size = elem_size;
    582 	needed = 0;
    583 
    584 	if (name - oname != 4)
    585 		return EINVAL;
    586 
    587 	pf = oname[1];
    588 	type = oname[2];
    589 
    590 	/*
    591 	 * allocate dummy file descriptor to make position in list.
    592 	 */
    593 	sysctl_unlock();
    594 	if ((dfp = fgetdummy()) == NULL) {
    595 	 	sysctl_relock();
    596 		return ENOMEM;
    597 	}
    598 
    599 	/*
    600 	 * there's no "list" of local domain sockets, so we have
    601 	 * to walk the file list looking for them.  :-/
    602 	 */
    603 	mutex_enter(&filelist_lock);
    604 	LIST_FOREACH_SAFE(fp, &filehead, f_list, np) {
    605 		if (fp->f_count == 0 || fp->f_type != DTYPE_SOCKET ||
    606 		    fp->f_socket == NULL)
    607 			continue;
    608 		so = fp->f_socket;
    609 		if (so->so_type != type)
    610 			continue;
    611 		if (so->so_proto->pr_domain->dom_family != pf)
    612 			continue;
    613 		if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_SOCKET,
    614 		    KAUTH_REQ_NETWORK_SOCKET_CANSEE, so, NULL, NULL) != 0)
    615 			continue;
    616 		if (len >= elem_size && elem_count > 0) {
    617 			mutex_enter(&fp->f_lock);
    618 			/*
    619 			 * Do not add references, if the count reached 0.
    620 			 * Since the check above has been performed without
    621 			 * locking, it must be rechecked here as a concurrent
    622 			 * closef could have reduced it.
    623 			 */
    624 			if (fp->f_count == 0) {
    625 				mutex_exit(&fp->f_lock);
    626 				continue;
    627 			}
    628 			fp->f_count++;
    629 			mutex_exit(&fp->f_lock);
    630 			LIST_INSERT_AFTER(fp, dfp, f_list);
    631 			mutex_exit(&filelist_lock);
    632 			sysctl_dounpcb(&pcb, so);
    633 			error = copyout(&pcb, dp, out_size);
    634 			closef(fp);
    635 			mutex_enter(&filelist_lock);
    636 			np = LIST_NEXT(dfp, f_list);
    637 			LIST_REMOVE(dfp, f_list);
    638 			if (error)
    639 				break;
    640 			dp += elem_size;
    641 			len -= elem_size;
    642 		}
    643 		needed += elem_size;
    644 		if (elem_count > 0 && elem_count != INT_MAX)
    645 			elem_count--;
    646 	}
    647 	mutex_exit(&filelist_lock);
    648 	fputdummy(dfp);
    649  	*oldlenp = needed;
    650 	if (oldp == NULL)
    651 		*oldlenp += PCB_SLOP * sizeof(struct kinfo_pcb);
    652  	sysctl_relock();
    653 
    654 	return error;
    655 }
    656 
    657 static void
    658 sysctl_net_setup(void)
    659 {
    660 
    661 	KASSERT(domain_sysctllog == NULL);
    662 	sysctl_createv(&domain_sysctllog, 0, NULL, NULL,
    663 		       CTLFLAG_PERMANENT,
    664 		       CTLTYPE_NODE, "local",
    665 		       SYSCTL_DESCR("PF_LOCAL related settings"),
    666 		       NULL, 0, NULL, 0,
    667 		       CTL_NET, PF_LOCAL, CTL_EOL);
    668 	sysctl_createv(&domain_sysctllog, 0, NULL, NULL,
    669 		       CTLFLAG_PERMANENT,
    670 		       CTLTYPE_NODE, "stream",
    671 		       SYSCTL_DESCR("SOCK_STREAM settings"),
    672 		       NULL, 0, NULL, 0,
    673 		       CTL_NET, PF_LOCAL, SOCK_STREAM, CTL_EOL);
    674 	sysctl_createv(&domain_sysctllog, 0, NULL, NULL,
    675 		       CTLFLAG_PERMANENT,
    676 		       CTLTYPE_NODE, "seqpacket",
    677 		       SYSCTL_DESCR("SOCK_SEQPACKET settings"),
    678 		       NULL, 0, NULL, 0,
    679 		       CTL_NET, PF_LOCAL, SOCK_SEQPACKET, CTL_EOL);
    680 	sysctl_createv(&domain_sysctllog, 0, NULL, NULL,
    681 		       CTLFLAG_PERMANENT,
    682 		       CTLTYPE_NODE, "dgram",
    683 		       SYSCTL_DESCR("SOCK_DGRAM settings"),
    684 		       NULL, 0, NULL, 0,
    685 		       CTL_NET, PF_LOCAL, SOCK_DGRAM, CTL_EOL);
    686 
    687 	sysctl_createv(&domain_sysctllog, 0, NULL, NULL,
    688 		       CTLFLAG_PERMANENT,
    689 		       CTLTYPE_STRUCT, "pcblist",
    690 		       SYSCTL_DESCR("SOCK_STREAM protocol control block list"),
    691 		       sysctl_unpcblist, 0, NULL, 0,
    692 		       CTL_NET, PF_LOCAL, SOCK_STREAM, CTL_CREATE, CTL_EOL);
    693 	sysctl_createv(&domain_sysctllog, 0, NULL, NULL,
    694 		       CTLFLAG_PERMANENT,
    695 		       CTLTYPE_STRUCT, "pcblist",
    696 		       SYSCTL_DESCR("SOCK_SEQPACKET protocol control "
    697 				    "block list"),
    698 		       sysctl_unpcblist, 0, NULL, 0,
    699 		       CTL_NET, PF_LOCAL, SOCK_SEQPACKET, CTL_CREATE, CTL_EOL);
    700 	sysctl_createv(&domain_sysctllog, 0, NULL, NULL,
    701 		       CTLFLAG_PERMANENT,
    702 		       CTLTYPE_STRUCT, "pcblist",
    703 		       SYSCTL_DESCR("SOCK_DGRAM protocol control block list"),
    704 		       sysctl_unpcblist, 0, NULL, 0,
    705 		       CTL_NET, PF_LOCAL, SOCK_DGRAM, CTL_CREATE, CTL_EOL);
    706 }
    707 
    708 void
    709 pfctlinput(int cmd, const struct sockaddr *sa)
    710 {
    711 	struct domain *dp;
    712 	const struct protosw *pr;
    713 
    714 	DOMAIN_FOREACH(dp) {
    715 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
    716 			if (pr->pr_ctlinput != NULL)
    717 				(*pr->pr_ctlinput)(cmd, sa, NULL);
    718 		}
    719 	}
    720 }
    721 
    722 void
    723 pfctlinput2(int cmd, const struct sockaddr *sa, void *ctlparam)
    724 {
    725 	struct domain *dp;
    726 	const struct protosw *pr;
    727 
    728 	if (sa == NULL)
    729 		return;
    730 
    731 	DOMAIN_FOREACH(dp) {
    732 		/*
    733 		 * the check must be made by xx_ctlinput() anyways, to
    734 		 * make sure we use data item pointed to by ctlparam in
    735 		 * correct way.  the following check is made just for safety.
    736 		 */
    737 		if (dp->dom_family != sa->sa_family)
    738 			continue;
    739 
    740 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
    741 			if (pr->pr_ctlinput != NULL)
    742 				(*pr->pr_ctlinput)(cmd, sa, ctlparam);
    743 		}
    744 	}
    745 }
    746 
    747 void
    748 pfslowtimo(void *arg)
    749 {
    750 	struct domain *dp;
    751 	const struct protosw *pr;
    752 
    753 	pfslowtimo_now++;
    754 
    755 	DOMAIN_FOREACH(dp) {
    756 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
    757 			if (pr->pr_slowtimo)
    758 				(*pr->pr_slowtimo)();
    759 	}
    760 	callout_schedule(&pfslowtimo_ch, hz / PR_SLOWHZ);
    761 }
    762 
    763 void
    764 pffasttimo(void *arg)
    765 {
    766 	struct domain *dp;
    767 	const struct protosw *pr;
    768 
    769 	pffasttimo_now++;
    770 
    771 	DOMAIN_FOREACH(dp) {
    772 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
    773 			if (pr->pr_fasttimo)
    774 				(*pr->pr_fasttimo)();
    775 	}
    776 	callout_schedule(&pffasttimo_ch, hz / PR_FASTHZ);
    777 }
    778