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