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