Home | History | Annotate | Line # | Download | only in kern
uipc_domain.c revision 1.107.2.1
      1 /*	$NetBSD: uipc_domain.c,v 1.107.2.1 2020/12/14 14:38:14 thorpej 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.107.2.1 2020/12/14 14:38:14 thorpej 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 	if ((sa = malloc(reallen, M_SOCKADDR, flags)) == NULL)
    328 		return NULL;
    329 
    330 	sa->sa_family = af;
    331 	sa->sa_len = reallen;
    332 	sockaddr_checklen(sa);
    333 	return sa;
    334 }
    335 
    336 struct sockaddr *
    337 sockaddr_copy(struct sockaddr *dst, socklen_t socklen,
    338     const struct sockaddr *src)
    339 {
    340 	if (__predict_false(socklen < src->sa_len)) {
    341 		panic("%s: source too long, %d < %d bytes", __func__, socklen,
    342 		    src->sa_len);
    343 	}
    344 	sockaddr_checklen(src);
    345 	return memcpy(dst, src, src->sa_len);
    346 }
    347 
    348 struct sockaddr *
    349 sockaddr_externalize(struct sockaddr *dst, socklen_t socklen,
    350     const struct sockaddr *src)
    351 {
    352 	struct domain *dom;
    353 
    354 	dom = pffinddomain(src->sa_family);
    355 
    356 	if (dom != NULL && dom->dom_sockaddr_externalize != NULL)
    357 		return (*dom->dom_sockaddr_externalize)(dst, socklen, src);
    358 
    359 	return sockaddr_copy(dst, socklen, src);
    360 }
    361 
    362 int
    363 sockaddr_cmp(const struct sockaddr *sa1, const struct sockaddr *sa2)
    364 {
    365 	int len, rc;
    366 	struct domain *dom;
    367 
    368 	if (sa1->sa_family != sa2->sa_family)
    369 		return sa1->sa_family - sa2->sa_family;
    370 
    371 	dom = pffinddomain(sa1->sa_family);
    372 
    373 	if (dom != NULL && dom->dom_sockaddr_cmp != NULL)
    374 		return (*dom->dom_sockaddr_cmp)(sa1, sa2);
    375 
    376 	len = MIN(sa1->sa_len, sa2->sa_len);
    377 
    378 	if (dom == NULL || dom->dom_sa_cmplen == 0) {
    379 		if ((rc = memcmp(sa1, sa2, len)) != 0)
    380 			return rc;
    381 		return sa1->sa_len - sa2->sa_len;
    382 	}
    383 
    384 	if ((rc = memcmp((const char *)sa1 + dom->dom_sa_cmpofs,
    385 		         (const char *)sa2 + dom->dom_sa_cmpofs,
    386 			 MIN(dom->dom_sa_cmplen,
    387 			     len - MIN(len, dom->dom_sa_cmpofs)))) != 0)
    388 		return rc;
    389 
    390 	return MIN(dom->dom_sa_cmplen + dom->dom_sa_cmpofs, sa1->sa_len) -
    391 	       MIN(dom->dom_sa_cmplen + dom->dom_sa_cmpofs, sa2->sa_len);
    392 }
    393 
    394 struct sockaddr *
    395 sockaddr_dup(const struct sockaddr *src, int flags)
    396 {
    397 	struct sockaddr *dst;
    398 
    399 	if ((dst = sockaddr_alloc(src->sa_family, src->sa_len, flags)) == NULL)
    400 		return NULL;
    401 
    402 	return sockaddr_copy(dst, dst->sa_len, src);
    403 }
    404 
    405 void
    406 sockaddr_free(struct sockaddr *sa)
    407 {
    408 	free(sa, M_SOCKADDR);
    409 }
    410 
    411 static int
    412 sun_print(char *buf, size_t len, const void *v)
    413 {
    414 	const struct sockaddr_un *sun = v;
    415 	size_t plen;
    416 
    417 	KASSERT(sun->sun_len >= offsetof(struct sockaddr_un, sun_path[0]));
    418 	plen = sun->sun_len - offsetof(struct sockaddr_un, sun_path[0]);
    419 
    420 	len = MIN(len, plen);
    421 
    422 	return snprintf(buf, len, "%s", sun->sun_path);
    423 }
    424 
    425 int
    426 sockaddr_format(const struct sockaddr *sa, char *buf, size_t len)
    427 {
    428 	size_t plen = 0;
    429 
    430 	if (sa == NULL)
    431 		return strlcpy(buf, "(null)", len);
    432 
    433 	switch (sa->sa_family) {
    434 	case AF_LOCAL:
    435 		plen = strlcpy(buf, "unix: ", len);
    436 		break;
    437 	case AF_INET:
    438 		plen = strlcpy(buf, "inet: ", len);
    439 		break;
    440 	case AF_INET6:
    441 		plen = strlcpy(buf, "inet6: ", len);
    442 		break;
    443 	case AF_LINK:
    444 		plen = strlcpy(buf, "link: ", len);
    445 		break;
    446 	case AF_APPLETALK:
    447 		plen = strlcpy(buf, "atalk: ", len);
    448 		break;
    449 	default:
    450 		return snprintf(buf, len, "(unknown socket family %d)",
    451 		    (int)sa->sa_family);
    452 	}
    453 
    454 	buf += plen;
    455 	if (plen > len)
    456 		len = 0;
    457 	else
    458 		len -= plen;
    459 
    460 	switch (sa->sa_family) {
    461 	case AF_LOCAL:
    462 		return sun_print(buf, len, sa);
    463 	case AF_INET:
    464 		return sin_print(buf, len, sa);
    465 	case AF_INET6:
    466 		return sin6_print(buf, len, sa);
    467 	case AF_LINK:
    468 		return sdl_print(buf, len, sa);
    469 	case AF_APPLETALK:
    470 		return sat_print(buf, len, sa);
    471 	default:
    472 		panic("bad family %hhu", sa->sa_family);
    473 	}
    474 }
    475 
    476 /*
    477  * sysctl helper to stuff PF_LOCAL pcbs into sysctl structures
    478  */
    479 static void
    480 sysctl_dounpcb(struct kinfo_pcb *pcb, const struct socket *so)
    481 {
    482 	const bool allowaddr = get_expose_address(curproc);
    483 	struct unpcb *unp = sotounpcb(so);
    484 	struct sockaddr_un *un = unp->unp_addr;
    485 
    486 	memset(pcb, 0, sizeof(*pcb));
    487 
    488 	pcb->ki_family = so->so_proto->pr_domain->dom_family;
    489 	pcb->ki_type = so->so_proto->pr_type;
    490 	pcb->ki_protocol = so->so_proto->pr_protocol;
    491 	pcb->ki_pflags = unp->unp_flags;
    492 
    493 	COND_SET_VALUE(pcb->ki_pcbaddr, PTRTOUINT64(unp), allowaddr);
    494 	/* pcb->ki_ppcbaddr = unp has no ppcb... */
    495 	COND_SET_VALUE(pcb->ki_sockaddr, PTRTOUINT64(so), allowaddr);
    496 
    497 	pcb->ki_sostate = so->so_state;
    498 	/* pcb->ki_prstate = unp has no state... */
    499 
    500 	pcb->ki_rcvq = so->so_rcv.sb_cc;
    501 	pcb->ki_sndq = so->so_snd.sb_cc;
    502 
    503 	un = (struct sockaddr_un *)pcb->ki_spad;
    504 	/*
    505 	 * local domain sockets may bind without having a local
    506 	 * endpoint.  bleah!
    507 	 */
    508 	if (unp->unp_addr != NULL) {
    509 		/*
    510 		 * We've added one to sun_len when allocating to
    511 		 * hold terminating NUL which we want here.  See
    512 		 * makeun().
    513 		 */
    514 		memcpy(un, unp->unp_addr,
    515 		    uimin(sizeof(pcb->ki_spad), unp->unp_addr->sun_len + 1));
    516 	}
    517 	else {
    518 		un->sun_len = offsetof(struct sockaddr_un, sun_path);
    519 		un->sun_family = pcb->ki_family;
    520 	}
    521 	if (unp->unp_conn != NULL) {
    522 		un = (struct sockaddr_un *)pcb->ki_dpad;
    523 		if (unp->unp_conn->unp_addr != NULL) {
    524 			memcpy(un, unp->unp_conn->unp_addr,
    525 			    uimin(sizeof(pcb->ki_dpad), unp->unp_conn->unp_addr->sun_len + 1));
    526 		}
    527 		else {
    528 			un->sun_len = offsetof(struct sockaddr_un, sun_path);
    529 			un->sun_family = pcb->ki_family;
    530 		}
    531 	}
    532 
    533 	pcb->ki_inode = unp->unp_ino;
    534 	COND_SET_VALUE(pcb->ki_vnode, PTRTOUINT64(unp->unp_vnode), allowaddr);
    535 	COND_SET_VALUE(pcb->ki_conn, PTRTOUINT64(unp->unp_conn), allowaddr);
    536 	COND_SET_VALUE(pcb->ki_refs, PTRTOUINT64(unp->unp_refs), allowaddr);
    537 	COND_SET_VALUE(pcb->ki_nextref, PTRTOUINT64(unp->unp_nextref),
    538 	    allowaddr);
    539 }
    540 
    541 static int
    542 sysctl_unpcblist(SYSCTLFN_ARGS)
    543 {
    544 	struct file *fp, *np, *dfp;
    545 	struct socket *so;
    546 	struct kinfo_pcb pcb;
    547 	char *dp;
    548 	size_t len, needed, elem_size, out_size;
    549 	int error, elem_count, pf, type;
    550 
    551 	if (namelen == 1 && name[0] == CTL_QUERY)
    552 		return sysctl_query(SYSCTLFN_CALL(rnode));
    553 
    554 	if (namelen != 4)
    555 		return EINVAL;
    556 
    557 	if (oldp != NULL) {
    558 		len = *oldlenp;
    559 		elem_size = name[2];
    560 		elem_count = name[3];
    561 		if (elem_size != sizeof(pcb))
    562 			return EINVAL;
    563 	} else {
    564 		len = 0;
    565 		elem_size = sizeof(pcb);
    566 		elem_count = INT_MAX;
    567 	}
    568 	error = 0;
    569 	dp = oldp;
    570 	out_size = elem_size;
    571 	needed = 0;
    572 
    573 	if (name - oname != 4)
    574 		return EINVAL;
    575 
    576 	pf = oname[1];
    577 	type = oname[2];
    578 
    579 	/*
    580 	 * allocate dummy file descriptor to make position in list.
    581 	 */
    582 	sysctl_unlock();
    583 	if ((dfp = fgetdummy()) == NULL) {
    584 	 	sysctl_relock();
    585 		return ENOMEM;
    586 	}
    587 
    588 	/*
    589 	 * there's no "list" of local domain sockets, so we have
    590 	 * to walk the file list looking for them.  :-/
    591 	 */
    592 	mutex_enter(&filelist_lock);
    593 	LIST_FOREACH_SAFE(fp, &filehead, f_list, np) {
    594 		if (fp->f_count == 0 || fp->f_type != DTYPE_SOCKET ||
    595 		    fp->f_socket == NULL)
    596 			continue;
    597 		so = fp->f_socket;
    598 		if (so->so_type != type)
    599 			continue;
    600 		if (so->so_proto->pr_domain->dom_family != pf)
    601 			continue;
    602 		if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_SOCKET,
    603 		    KAUTH_REQ_NETWORK_SOCKET_CANSEE, so, NULL, NULL) != 0)
    604 			continue;
    605 		if (len >= elem_size && elem_count > 0) {
    606 			mutex_enter(&fp->f_lock);
    607 			/*
    608 			 * Do not add references, if the count reached 0.
    609 			 * Since the check above has been performed without
    610 			 * locking, it must be rechecked here as a concurrent
    611 			 * closef could have reduced it.
    612 			 */
    613 			if (fp->f_count == 0) {
    614 				mutex_exit(&fp->f_lock);
    615 				continue;
    616 			}
    617 			fp->f_count++;
    618 			mutex_exit(&fp->f_lock);
    619 			LIST_INSERT_AFTER(fp, dfp, f_list);
    620 			mutex_exit(&filelist_lock);
    621 			sysctl_dounpcb(&pcb, so);
    622 			error = copyout(&pcb, dp, out_size);
    623 			closef(fp);
    624 			mutex_enter(&filelist_lock);
    625 			np = LIST_NEXT(dfp, f_list);
    626 			LIST_REMOVE(dfp, f_list);
    627 			if (error)
    628 				break;
    629 			dp += elem_size;
    630 			len -= elem_size;
    631 		}
    632 		needed += elem_size;
    633 		if (elem_count > 0 && elem_count != INT_MAX)
    634 			elem_count--;
    635 	}
    636 	mutex_exit(&filelist_lock);
    637 	fputdummy(dfp);
    638  	*oldlenp = needed;
    639 	if (oldp == NULL)
    640 		*oldlenp += PCB_SLOP * sizeof(struct kinfo_pcb);
    641  	sysctl_relock();
    642 
    643 	return error;
    644 }
    645 
    646 static void
    647 sysctl_net_setup(void)
    648 {
    649 
    650 	KASSERT(domain_sysctllog == NULL);
    651 	sysctl_createv(&domain_sysctllog, 0, NULL, NULL,
    652 		       CTLFLAG_PERMANENT,
    653 		       CTLTYPE_NODE, "local",
    654 		       SYSCTL_DESCR("PF_LOCAL related settings"),
    655 		       NULL, 0, NULL, 0,
    656 		       CTL_NET, PF_LOCAL, CTL_EOL);
    657 	sysctl_createv(&domain_sysctllog, 0, NULL, NULL,
    658 		       CTLFLAG_PERMANENT,
    659 		       CTLTYPE_NODE, "stream",
    660 		       SYSCTL_DESCR("SOCK_STREAM settings"),
    661 		       NULL, 0, NULL, 0,
    662 		       CTL_NET, PF_LOCAL, SOCK_STREAM, CTL_EOL);
    663 	sysctl_createv(&domain_sysctllog, 0, NULL, NULL,
    664 		       CTLFLAG_PERMANENT,
    665 		       CTLTYPE_NODE, "seqpacket",
    666 		       SYSCTL_DESCR("SOCK_SEQPACKET settings"),
    667 		       NULL, 0, NULL, 0,
    668 		       CTL_NET, PF_LOCAL, SOCK_SEQPACKET, CTL_EOL);
    669 	sysctl_createv(&domain_sysctllog, 0, NULL, NULL,
    670 		       CTLFLAG_PERMANENT,
    671 		       CTLTYPE_NODE, "dgram",
    672 		       SYSCTL_DESCR("SOCK_DGRAM settings"),
    673 		       NULL, 0, NULL, 0,
    674 		       CTL_NET, PF_LOCAL, SOCK_DGRAM, CTL_EOL);
    675 
    676 	sysctl_createv(&domain_sysctllog, 0, NULL, NULL,
    677 		       CTLFLAG_PERMANENT,
    678 		       CTLTYPE_STRUCT, "pcblist",
    679 		       SYSCTL_DESCR("SOCK_STREAM protocol control block list"),
    680 		       sysctl_unpcblist, 0, NULL, 0,
    681 		       CTL_NET, PF_LOCAL, SOCK_STREAM, CTL_CREATE, CTL_EOL);
    682 	sysctl_createv(&domain_sysctllog, 0, NULL, NULL,
    683 		       CTLFLAG_PERMANENT,
    684 		       CTLTYPE_STRUCT, "pcblist",
    685 		       SYSCTL_DESCR("SOCK_SEQPACKET protocol control "
    686 				    "block list"),
    687 		       sysctl_unpcblist, 0, NULL, 0,
    688 		       CTL_NET, PF_LOCAL, SOCK_SEQPACKET, CTL_CREATE, CTL_EOL);
    689 	sysctl_createv(&domain_sysctllog, 0, NULL, NULL,
    690 		       CTLFLAG_PERMANENT,
    691 		       CTLTYPE_STRUCT, "pcblist",
    692 		       SYSCTL_DESCR("SOCK_DGRAM protocol control block list"),
    693 		       sysctl_unpcblist, 0, NULL, 0,
    694 		       CTL_NET, PF_LOCAL, SOCK_DGRAM, CTL_CREATE, CTL_EOL);
    695 }
    696 
    697 void
    698 pfctlinput(int cmd, const struct sockaddr *sa)
    699 {
    700 	struct domain *dp;
    701 	const struct protosw *pr;
    702 
    703 	DOMAIN_FOREACH(dp) {
    704 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
    705 			if (pr->pr_ctlinput != NULL)
    706 				(*pr->pr_ctlinput)(cmd, sa, NULL);
    707 		}
    708 	}
    709 }
    710 
    711 void
    712 pfctlinput2(int cmd, const struct sockaddr *sa, void *ctlparam)
    713 {
    714 	struct domain *dp;
    715 	const struct protosw *pr;
    716 
    717 	if (sa == NULL)
    718 		return;
    719 
    720 	DOMAIN_FOREACH(dp) {
    721 		/*
    722 		 * the check must be made by xx_ctlinput() anyways, to
    723 		 * make sure we use data item pointed to by ctlparam in
    724 		 * correct way.  the following check is made just for safety.
    725 		 */
    726 		if (dp->dom_family != sa->sa_family)
    727 			continue;
    728 
    729 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
    730 			if (pr->pr_ctlinput != NULL)
    731 				(*pr->pr_ctlinput)(cmd, sa, ctlparam);
    732 		}
    733 	}
    734 }
    735 
    736 void
    737 pfslowtimo(void *arg)
    738 {
    739 	struct domain *dp;
    740 	const struct protosw *pr;
    741 
    742 	pfslowtimo_now++;
    743 
    744 	DOMAIN_FOREACH(dp) {
    745 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
    746 			if (pr->pr_slowtimo)
    747 				(*pr->pr_slowtimo)();
    748 	}
    749 	callout_schedule(&pfslowtimo_ch, hz / PR_SLOWHZ);
    750 }
    751 
    752 void
    753 pffasttimo(void *arg)
    754 {
    755 	struct domain *dp;
    756 	const struct protosw *pr;
    757 
    758 	pffasttimo_now++;
    759 
    760 	DOMAIN_FOREACH(dp) {
    761 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
    762 			if (pr->pr_fasttimo)
    763 				(*pr->pr_fasttimo)();
    764 	}
    765 	callout_schedule(&pffasttimo_ch, hz / PR_FASTHZ);
    766 }
    767