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