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