Home | History | Annotate | Line # | Download | only in kern
uipc_domain.c revision 1.92
      1  1.92  christos /*	$NetBSD: uipc_domain.c,v 1.92 2014/04/17 16:14:22 christos 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.92  christos __KERNEL_RCSID(0, "$NetBSD: uipc_domain.c,v 1.92 2014/04/17 16:14:22 christos Exp $");
     36   1.1       cgd 
     37   1.5   mycroft #include <sys/param.h>
     38   1.5   mycroft #include <sys/socket.h>
     39  1.50    atatat #include <sys/socketvar.h>
     40   1.5   mycroft #include <sys/protosw.h>
     41   1.5   mycroft #include <sys/domain.h>
     42   1.5   mycroft #include <sys/mbuf.h>
     43   1.5   mycroft #include <sys/time.h>
     44   1.5   mycroft #include <sys/kernel.h>
     45  1.11   mycroft #include <sys/systm.h>
     46  1.30   thorpej #include <sys/callout.h>
     47  1.49      matt #include <sys/queue.h>
     48  1.10       cgd #include <sys/proc.h>
     49  1.10       cgd #include <sys/sysctl.h>
     50  1.50    atatat #include <sys/un.h>
     51  1.50    atatat #include <sys/unpcb.h>
     52  1.50    atatat #include <sys/file.h>
     53  1.72        ad #include <sys/filedesc.h>
     54  1.57      elad #include <sys/kauth.h>
     55  1.88     joerg #include <netinet/in.h>
     56  1.13  christos 
     57  1.69    dyoung MALLOC_DECLARE(M_SOCKADDR);
     58  1.69    dyoung 
     59  1.69    dyoung MALLOC_DEFINE(M_SOCKADDR, "sockaddr", "socket endpoints");
     60  1.69    dyoung 
     61  1.45  junyoung void	pffasttimo(void *);
     62  1.45  junyoung void	pfslowtimo(void *);
     63  1.37      matt 
     64  1.49      matt struct domainhead domains = STAILQ_HEAD_INITIALIZER(domains);
     65  1.64    dyoung static struct domain *domain_array[AF_MAX];
     66  1.11   mycroft 
     67  1.66        ad callout_t pffasttimo_ch, pfslowtimo_ch;
     68  1.30   thorpej 
     69  1.19   thorpej /*
     70  1.19   thorpej  * Current time values for fast and slow timeouts.  We can use u_int
     71  1.19   thorpej  * relatively safely.  The fast timer will roll over in 27 years and
     72  1.19   thorpej  * the slow timer in 68 years.
     73  1.19   thorpej  */
     74  1.19   thorpej u_int	pfslowtimo_now;
     75  1.19   thorpej u_int	pffasttimo_now;
     76  1.19   thorpej 
     77  1.77     pooka static struct sysctllog *domain_sysctllog;
     78  1.77     pooka static void sysctl_net_setup(void);
     79  1.77     pooka 
     80  1.49      matt void
     81  1.82     pooka domaininit(bool addroute)
     82  1.49      matt {
     83  1.49      matt 	__link_set_decl(domains, struct domain);
     84  1.49      matt 	struct domain * const * dpp;
     85  1.49      matt 	struct domain *rt_domain = NULL;
     86  1.49      matt 
     87  1.77     pooka 	sysctl_net_setup();
     88  1.77     pooka 
     89  1.49      matt 	/*
     90  1.49      matt 	 * Add all of the domains.  Make sure the PF_ROUTE
     91  1.49      matt 	 * domain is added last.
     92  1.49      matt 	 */
     93  1.49      matt 	__link_set_foreach(dpp, domains) {
     94  1.49      matt 		if ((*dpp)->dom_family == PF_ROUTE)
     95  1.49      matt 			rt_domain = *dpp;
     96  1.49      matt 		else
     97  1.49      matt 			domain_attach(*dpp);
     98  1.49      matt 	}
     99  1.82     pooka 	if (rt_domain && addroute)
    100  1.49      matt 		domain_attach(rt_domain);
    101  1.49      matt 
    102  1.76        ad 	callout_init(&pffasttimo_ch, CALLOUT_MPSAFE);
    103  1.76        ad 	callout_init(&pfslowtimo_ch, CALLOUT_MPSAFE);
    104  1.49      matt 
    105  1.49      matt 	callout_reset(&pffasttimo_ch, 1, pffasttimo, NULL);
    106  1.49      matt 	callout_reset(&pfslowtimo_ch, 1, pfslowtimo, NULL);
    107   1.1       cgd }
    108   1.1       cgd 
    109   1.4    andrew void
    110  1.49      matt domain_attach(struct domain *dp)
    111   1.1       cgd {
    112  1.47      matt 	const struct protosw *pr;
    113   1.1       cgd 
    114  1.49      matt 	STAILQ_INSERT_TAIL(&domains, dp, dom_link);
    115  1.64    dyoung 	if (dp->dom_family < __arraycount(domain_array))
    116  1.64    dyoung 		domain_array[dp->dom_family] = dp;
    117  1.49      matt 
    118  1.49      matt 	if (dp->dom_init)
    119  1.49      matt 		(*dp->dom_init)();
    120   1.1       cgd 
    121  1.38      matt #ifdef MBUFTRACE
    122  1.49      matt 	if (dp->dom_mowner.mo_name[0] == '\0') {
    123  1.49      matt 		strncpy(dp->dom_mowner.mo_name, dp->dom_name,
    124  1.49      matt 		    sizeof(dp->dom_mowner.mo_name));
    125  1.49      matt 		MOWNER_ATTACH(&dp->dom_mowner);
    126  1.49      matt 	}
    127  1.38      matt #endif
    128  1.49      matt 	for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
    129  1.49      matt 		if (pr->pr_init)
    130  1.49      matt 			(*pr->pr_init)();
    131   1.1       cgd 	}
    132   1.1       cgd 
    133  1.16  explorer 	if (max_linkhdr < 16)		/* XXX */
    134  1.16  explorer 		max_linkhdr = 16;
    135   1.1       cgd 	max_hdr = max_linkhdr + max_protohdr;
    136   1.1       cgd 	max_datalen = MHLEN - max_hdr;
    137   1.1       cgd }
    138   1.1       cgd 
    139  1.29   thorpej struct domain *
    140  1.49      matt pffinddomain(int family)
    141  1.29   thorpej {
    142  1.29   thorpej 	struct domain *dp;
    143  1.29   thorpej 
    144  1.64    dyoung 	if (family < __arraycount(domain_array) && domain_array[family] != NULL)
    145  1.64    dyoung 		return domain_array[family];
    146  1.64    dyoung 
    147  1.49      matt 	DOMAIN_FOREACH(dp)
    148  1.29   thorpej 		if (dp->dom_family == family)
    149  1.81    dyoung 			return dp;
    150  1.81    dyoung 	return NULL;
    151  1.29   thorpej }
    152  1.29   thorpej 
    153  1.47      matt const struct protosw *
    154  1.49      matt pffindtype(int family, int type)
    155   1.1       cgd {
    156  1.29   thorpej 	struct domain *dp;
    157  1.47      matt 	const struct protosw *pr;
    158  1.29   thorpej 
    159  1.29   thorpej 	dp = pffinddomain(family);
    160  1.29   thorpej 	if (dp == NULL)
    161  1.81    dyoung 		return NULL;
    162   1.1       cgd 
    163   1.1       cgd 	for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
    164   1.1       cgd 		if (pr->pr_type && pr->pr_type == type)
    165  1.81    dyoung 			return pr;
    166  1.29   thorpej 
    167  1.81    dyoung 	return NULL;
    168   1.1       cgd }
    169   1.1       cgd 
    170  1.47      matt const struct protosw *
    171  1.49      matt pffindproto(int family, int protocol, int type)
    172   1.1       cgd {
    173  1.29   thorpej 	struct domain *dp;
    174  1.47      matt 	const struct protosw *pr;
    175  1.47      matt 	const struct protosw *maybe = NULL;
    176   1.1       cgd 
    177   1.1       cgd 	if (family == 0)
    178  1.81    dyoung 		return NULL;
    179  1.29   thorpej 
    180  1.29   thorpej 	dp = pffinddomain(family);
    181  1.29   thorpej 	if (dp == NULL)
    182  1.81    dyoung 		return NULL;
    183  1.29   thorpej 
    184   1.1       cgd 	for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
    185   1.1       cgd 		if ((pr->pr_protocol == protocol) && (pr->pr_type == type))
    186  1.81    dyoung 			return pr;
    187   1.1       cgd 
    188   1.1       cgd 		if (type == SOCK_RAW && pr->pr_type == SOCK_RAW &&
    189  1.29   thorpej 		    pr->pr_protocol == 0 && maybe == NULL)
    190   1.1       cgd 			maybe = pr;
    191   1.1       cgd 	}
    192  1.81    dyoung 	return maybe;
    193  1.10       cgd }
    194  1.10       cgd 
    195  1.71    dyoung void *
    196  1.71    dyoung sockaddr_addr(struct sockaddr *sa, socklen_t *slenp)
    197  1.71    dyoung {
    198  1.71    dyoung 	const struct domain *dom;
    199  1.71    dyoung 
    200  1.71    dyoung 	if ((dom = pffinddomain(sa->sa_family)) == NULL ||
    201  1.71    dyoung 	    dom->dom_sockaddr_addr == NULL)
    202  1.71    dyoung 		return NULL;
    203  1.71    dyoung 
    204  1.71    dyoung 	return (*dom->dom_sockaddr_addr)(sa, slenp);
    205  1.71    dyoung }
    206  1.71    dyoung 
    207  1.71    dyoung const void *
    208  1.71    dyoung sockaddr_const_addr(const struct sockaddr *sa, socklen_t *slenp)
    209  1.71    dyoung {
    210  1.71    dyoung 	const struct domain *dom;
    211  1.83    dyoung 
    212  1.71    dyoung 	if ((dom = pffinddomain(sa->sa_family)) == NULL ||
    213  1.71    dyoung 	    dom->dom_sockaddr_const_addr == NULL)
    214  1.71    dyoung 		return NULL;
    215  1.71    dyoung 
    216  1.71    dyoung 	return (*dom->dom_sockaddr_const_addr)(sa, slenp);
    217  1.71    dyoung }
    218  1.71    dyoung 
    219  1.71    dyoung const struct sockaddr *
    220  1.80    dyoung sockaddr_any_by_family(int family)
    221  1.71    dyoung {
    222  1.71    dyoung 	const struct domain *dom;
    223  1.83    dyoung 
    224  1.80    dyoung 	if ((dom = pffinddomain(family)) == NULL)
    225  1.71    dyoung 		return NULL;
    226  1.71    dyoung 
    227  1.71    dyoung 	return dom->dom_sa_any;
    228  1.71    dyoung }
    229  1.71    dyoung 
    230  1.80    dyoung const struct sockaddr *
    231  1.80    dyoung sockaddr_any(const struct sockaddr *sa)
    232  1.80    dyoung {
    233  1.80    dyoung 	return sockaddr_any_by_family(sa->sa_family);
    234  1.80    dyoung }
    235  1.80    dyoung 
    236  1.71    dyoung const void *
    237  1.71    dyoung sockaddr_anyaddr(const struct sockaddr *sa, socklen_t *slenp)
    238  1.71    dyoung {
    239  1.71    dyoung 	const struct sockaddr *any;
    240  1.71    dyoung 
    241  1.71    dyoung 	if ((any = sockaddr_any(sa)) == NULL)
    242  1.71    dyoung 		return NULL;
    243  1.71    dyoung 
    244  1.71    dyoung 	return sockaddr_const_addr(any, slenp);
    245  1.71    dyoung }
    246  1.71    dyoung 
    247  1.64    dyoung struct sockaddr *
    248  1.69    dyoung sockaddr_alloc(sa_family_t af, socklen_t socklen, int flags)
    249  1.64    dyoung {
    250  1.64    dyoung 	struct sockaddr *sa;
    251  1.69    dyoung 	socklen_t reallen = MAX(socklen, offsetof(struct sockaddr, sa_data[0]));
    252  1.64    dyoung 
    253  1.69    dyoung 	if ((sa = malloc(reallen, M_SOCKADDR, flags)) == NULL)
    254  1.64    dyoung 		return NULL;
    255  1.64    dyoung 
    256  1.64    dyoung 	sa->sa_family = af;
    257  1.69    dyoung 	sa->sa_len = reallen;
    258  1.64    dyoung 	return sa;
    259  1.64    dyoung }
    260  1.64    dyoung 
    261  1.64    dyoung struct sockaddr *
    262  1.69    dyoung sockaddr_copy(struct sockaddr *dst, socklen_t socklen,
    263  1.69    dyoung     const struct sockaddr *src)
    264  1.64    dyoung {
    265  1.70    dyoung 	if (__predict_false(socklen < src->sa_len)) {
    266  1.70    dyoung 		panic("%s: source too long, %d < %d bytes", __func__, socklen,
    267  1.70    dyoung 		    src->sa_len);
    268  1.70    dyoung 	}
    269  1.70    dyoung 	return memcpy(dst, src, src->sa_len);
    270  1.64    dyoung }
    271  1.64    dyoung 
    272  1.84    dyoung struct sockaddr *
    273  1.84    dyoung sockaddr_externalize(struct sockaddr *dst, socklen_t socklen,
    274  1.84    dyoung     const struct sockaddr *src)
    275  1.84    dyoung {
    276  1.84    dyoung 	struct domain *dom;
    277  1.84    dyoung 
    278  1.84    dyoung 	dom = pffinddomain(src->sa_family);
    279  1.84    dyoung 
    280  1.84    dyoung 	if (dom != NULL && dom->dom_sockaddr_externalize != NULL)
    281  1.84    dyoung 		return (*dom->dom_sockaddr_externalize)(dst, socklen, src);
    282  1.84    dyoung 
    283  1.84    dyoung 	return sockaddr_copy(dst, socklen, src);
    284  1.84    dyoung }
    285  1.84    dyoung 
    286  1.64    dyoung int
    287  1.64    dyoung sockaddr_cmp(const struct sockaddr *sa1, const struct sockaddr *sa2)
    288  1.64    dyoung {
    289  1.65    dyoung 	int len, rc;
    290  1.64    dyoung 	struct domain *dom;
    291  1.64    dyoung 
    292  1.64    dyoung 	if (sa1->sa_family != sa2->sa_family)
    293  1.64    dyoung 		return sa1->sa_family - sa2->sa_family;
    294  1.64    dyoung 
    295  1.65    dyoung 	dom = pffinddomain(sa1->sa_family);
    296  1.65    dyoung 
    297  1.65    dyoung 	if (dom != NULL && dom->dom_sockaddr_cmp != NULL)
    298  1.64    dyoung 		return (*dom->dom_sockaddr_cmp)(sa1, sa2);
    299  1.64    dyoung 
    300  1.65    dyoung 	len = MIN(sa1->sa_len, sa2->sa_len);
    301  1.65    dyoung 
    302  1.65    dyoung 	if (dom == NULL || dom->dom_sa_cmplen == 0) {
    303  1.65    dyoung 		if ((rc = memcmp(sa1, sa2, len)) != 0)
    304  1.65    dyoung 			return rc;
    305  1.65    dyoung 		return sa1->sa_len - sa2->sa_len;
    306  1.65    dyoung 	}
    307  1.65    dyoung 
    308  1.65    dyoung 	if ((rc = memcmp((const char *)sa1 + dom->dom_sa_cmpofs,
    309  1.65    dyoung 		         (const char *)sa2 + dom->dom_sa_cmpofs,
    310  1.65    dyoung 			 MIN(dom->dom_sa_cmplen,
    311  1.65    dyoung 			     len - MIN(len, dom->dom_sa_cmpofs)))) != 0)
    312  1.64    dyoung 		return rc;
    313  1.64    dyoung 
    314  1.65    dyoung 	return MIN(dom->dom_sa_cmplen + dom->dom_sa_cmpofs, sa1->sa_len) -
    315  1.65    dyoung 	       MIN(dom->dom_sa_cmplen + dom->dom_sa_cmpofs, sa2->sa_len);
    316  1.64    dyoung }
    317  1.64    dyoung 
    318  1.64    dyoung struct sockaddr *
    319  1.64    dyoung sockaddr_dup(const struct sockaddr *src, int flags)
    320  1.64    dyoung {
    321  1.64    dyoung 	struct sockaddr *dst;
    322  1.64    dyoung 
    323  1.69    dyoung 	if ((dst = sockaddr_alloc(src->sa_family, src->sa_len, flags)) == NULL)
    324  1.64    dyoung 		return NULL;
    325  1.64    dyoung 
    326  1.69    dyoung 	return sockaddr_copy(dst, dst->sa_len, src);
    327  1.64    dyoung }
    328  1.64    dyoung 
    329  1.64    dyoung void
    330  1.64    dyoung sockaddr_free(struct sockaddr *sa)
    331  1.64    dyoung {
    332  1.69    dyoung 	free(sa, M_SOCKADDR);
    333  1.64    dyoung }
    334  1.64    dyoung 
    335  1.88     joerg void
    336  1.88     joerg sockaddr_format(const struct sockaddr *sa, char *buf, size_t len)
    337  1.88     joerg {
    338  1.88     joerg 	const struct sockaddr_un *sun = (const struct sockaddr_un *)sa;
    339  1.88     joerg 	const struct sockaddr_in *sin = (const struct sockaddr_in *)sa;
    340  1.88     joerg 	const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)sa;
    341  1.88     joerg 	const uint8_t *data;
    342  1.88     joerg 	size_t data_len;
    343  1.88     joerg 
    344  1.88     joerg 	if (sa == NULL) {
    345  1.88     joerg 		strlcpy(buf, "(null)", len);
    346  1.88     joerg 		return;
    347  1.88     joerg 	}
    348  1.88     joerg 
    349  1.88     joerg 	switch (sa->sa_family) {
    350  1.88     joerg 	default:
    351  1.88     joerg 		snprintf(buf, len, "(unknown socket family %d)",
    352  1.88     joerg 		    (int)sa->sa_family);
    353  1.88     joerg 		return;
    354  1.88     joerg 	case AF_LOCAL:
    355  1.88     joerg 		strlcpy(buf, "unix:", len);
    356  1.88     joerg 		strlcat(buf, sun->sun_path, len);
    357  1.88     joerg 		return;
    358  1.88     joerg 	case AF_INET:
    359  1.88     joerg 		strlcpy(buf, "inet:", len);
    360  1.88     joerg 		if (len < 6)
    361  1.88     joerg 			return;
    362  1.88     joerg 		buf += 5;
    363  1.88     joerg 		len -= 5;
    364  1.88     joerg 		data = (const uint8_t *)&sin->sin_addr;
    365  1.88     joerg 		data_len = sizeof(sin->sin_addr);
    366  1.88     joerg 		break;
    367  1.88     joerg 	case AF_INET6:
    368  1.88     joerg 		strlcpy(buf, "inet6:", len);
    369  1.88     joerg 		if (len < 7)
    370  1.88     joerg 			return;
    371  1.88     joerg 		buf += 6;
    372  1.88     joerg 		len -= 6;
    373  1.88     joerg 		data = (const uint8_t *)&sin6->sin6_addr;
    374  1.88     joerg 		data_len = sizeof(sin6->sin6_addr);
    375  1.88     joerg 		break;
    376  1.88     joerg 	}
    377  1.88     joerg 	for (;;) {
    378  1.88     joerg 		if (--len == 0)
    379  1.88     joerg 			break;
    380  1.88     joerg 
    381  1.88     joerg 		uint8_t hi = *data >> 4;
    382  1.88     joerg 		uint8_t lo = *data & 15;
    383  1.88     joerg 		--data_len;
    384  1.88     joerg 		++data;
    385  1.88     joerg 		*buf++ = hi + (hi >= 10 ? 'a' - 10 : '0');
    386  1.88     joerg 		if (--len == 0)
    387  1.88     joerg 			break;
    388  1.88     joerg 		*buf++ = lo + (lo >= 10 ? 'a' - 10 : '0');
    389  1.88     joerg 		if (data_len == 0)
    390  1.88     joerg 			break;
    391  1.88     joerg 	}
    392  1.88     joerg 	*buf = 0;
    393  1.88     joerg }
    394  1.88     joerg 
    395  1.50    atatat /*
    396  1.50    atatat  * sysctl helper to stuff PF_LOCAL pcbs into sysctl structures
    397  1.50    atatat  */
    398  1.50    atatat static void
    399  1.50    atatat sysctl_dounpcb(struct kinfo_pcb *pcb, const struct socket *so)
    400  1.50    atatat {
    401  1.50    atatat 	struct unpcb *unp = sotounpcb(so);
    402  1.50    atatat 	struct sockaddr_un *un = unp->unp_addr;
    403  1.50    atatat 
    404  1.50    atatat 	memset(pcb, 0, sizeof(*pcb));
    405  1.50    atatat 
    406  1.50    atatat 	pcb->ki_family = so->so_proto->pr_domain->dom_family;
    407  1.50    atatat 	pcb->ki_type = so->so_proto->pr_type;
    408  1.50    atatat 	pcb->ki_protocol = so->so_proto->pr_protocol;
    409  1.50    atatat 	pcb->ki_pflags = unp->unp_flags;
    410  1.50    atatat 
    411  1.50    atatat 	pcb->ki_pcbaddr = PTRTOUINT64(unp);
    412  1.50    atatat 	/* pcb->ki_ppcbaddr = unp has no ppcb... */
    413  1.50    atatat 	pcb->ki_sockaddr = PTRTOUINT64(so);
    414  1.50    atatat 
    415  1.50    atatat 	pcb->ki_sostate = so->so_state;
    416  1.50    atatat 	/* pcb->ki_prstate = unp has no state... */
    417  1.50    atatat 
    418  1.50    atatat 	pcb->ki_rcvq = so->so_rcv.sb_cc;
    419  1.50    atatat 	pcb->ki_sndq = so->so_snd.sb_cc;
    420  1.50    atatat 
    421  1.92  christos 	un = (struct sockaddr_un *)pcb->ki_spad;
    422  1.50    atatat 	/*
    423  1.50    atatat 	 * local domain sockets may bind without having a local
    424  1.50    atatat 	 * endpoint.  bleah!
    425  1.50    atatat 	 */
    426  1.50    atatat 	if (unp->unp_addr != NULL) {
    427  1.91     seanb 		/*
    428  1.91     seanb 		 * We've added one to sun_len when allocating to
    429  1.91     seanb 		 * hold terminating NUL which we want here.  See
    430  1.91     seanb 		 * makeun().
    431  1.91     seanb 		 */
    432  1.91     seanb 		memcpy(un, unp->unp_addr,
    433  1.92  christos 		    min(sizeof(pcb->ki_spad), unp->unp_addr->sun_len + 1));
    434  1.50    atatat 	}
    435  1.50    atatat 	else {
    436  1.50    atatat 		un->sun_len = offsetof(struct sockaddr_un, sun_path);
    437  1.50    atatat 		un->sun_family = pcb->ki_family;
    438  1.50    atatat 	}
    439  1.50    atatat 	if (unp->unp_conn != NULL) {
    440  1.92  christos 		un = (struct sockaddr_un *)pcb->ki_dpad;
    441  1.50    atatat 		if (unp->unp_conn->unp_addr != NULL) {
    442  1.91     seanb 			memcpy(un, unp->unp_conn->unp_addr,
    443  1.92  christos 			    min(sizeof(pcb->ki_dpad), unp->unp_conn->unp_addr->sun_len + 1));
    444  1.50    atatat 		}
    445  1.50    atatat 		else {
    446  1.50    atatat 			un->sun_len = offsetof(struct sockaddr_un, sun_path);
    447  1.50    atatat 			un->sun_family = pcb->ki_family;
    448  1.50    atatat 		}
    449  1.50    atatat 	}
    450  1.50    atatat 
    451  1.50    atatat 	pcb->ki_inode = unp->unp_ino;
    452  1.50    atatat 	pcb->ki_vnode = PTRTOUINT64(unp->unp_vnode);
    453  1.50    atatat 	pcb->ki_conn = PTRTOUINT64(unp->unp_conn);
    454  1.50    atatat 	pcb->ki_refs = PTRTOUINT64(unp->unp_refs);
    455  1.50    atatat 	pcb->ki_nextref = PTRTOUINT64(unp->unp_nextref);
    456  1.50    atatat }
    457  1.50    atatat 
    458  1.50    atatat static int
    459  1.50    atatat sysctl_unpcblist(SYSCTLFN_ARGS)
    460  1.50    atatat {
    461  1.89    martin 	struct file *fp, *dfp;
    462  1.50    atatat 	struct socket *so;
    463  1.50    atatat 	struct kinfo_pcb pcb;
    464  1.50    atatat 	char *dp;
    465  1.50    atatat 	size_t len, needed, elem_size, out_size;
    466  1.89    martin 	int error, elem_count, pf, type;
    467  1.50    atatat 
    468  1.50    atatat 	if (namelen == 1 && name[0] == CTL_QUERY)
    469  1.81    dyoung 		return sysctl_query(SYSCTLFN_CALL(rnode));
    470  1.50    atatat 
    471  1.50    atatat 	if (namelen != 4)
    472  1.81    dyoung 		return EINVAL;
    473  1.50    atatat 
    474  1.56  christos 	if (oldp != NULL) {
    475  1.56  christos 		len = *oldlenp;
    476  1.56  christos 		elem_size = name[2];
    477  1.56  christos 		elem_count = name[3];
    478  1.56  christos 		if (elem_size != sizeof(pcb))
    479  1.56  christos 			return EINVAL;
    480  1.56  christos 	} else {
    481  1.56  christos 		len = 0;
    482  1.56  christos 		elem_size = sizeof(pcb);
    483  1.56  christos 		elem_count = INT_MAX;
    484  1.56  christos 	}
    485  1.50    atatat 	error = 0;
    486  1.50    atatat 	dp = oldp;
    487  1.56  christos 	out_size = elem_size;
    488  1.50    atatat 	needed = 0;
    489  1.50    atatat 
    490  1.50    atatat 	if (name - oname != 4)
    491  1.81    dyoung 		return EINVAL;
    492  1.50    atatat 
    493  1.50    atatat 	pf = oname[1];
    494  1.50    atatat 	type = oname[2];
    495  1.50    atatat 
    496  1.50    atatat 	/*
    497  1.74        ad 	 * allocate dummy file descriptor to make position in list.
    498  1.74        ad 	 */
    499  1.74        ad 	sysctl_unlock();
    500  1.74        ad 	if ((dfp = fgetdummy()) == NULL) {
    501  1.74        ad 	 	sysctl_relock();
    502  1.74        ad 		return ENOMEM;
    503  1.74        ad 	}
    504  1.74        ad 
    505  1.74        ad 	/*
    506  1.50    atatat 	 * there's no "list" of local domain sockets, so we have
    507  1.50    atatat 	 * to walk the file list looking for them.  :-/
    508  1.50    atatat 	 */
    509  1.72        ad 	mutex_enter(&filelist_lock);
    510  1.50    atatat 	LIST_FOREACH(fp, &filehead, f_list) {
    511  1.74        ad 		if (fp->f_count == 0 || fp->f_type != DTYPE_SOCKET ||
    512  1.74        ad 		    fp->f_data == NULL)
    513  1.74        ad 			continue;
    514  1.50    atatat 		so = (struct socket *)fp->f_data;
    515  1.50    atatat 		if (so->so_type != type)
    516  1.50    atatat 			continue;
    517  1.50    atatat 		if (so->so_proto->pr_domain->dom_family != pf)
    518  1.50    atatat 			continue;
    519  1.85      elad 		if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_SOCKET,
    520  1.85      elad 		    KAUTH_REQ_NETWORK_SOCKET_CANSEE, so, NULL, NULL) != 0)
    521  1.85      elad 			continue;
    522  1.50    atatat 		if (len >= elem_size && elem_count > 0) {
    523  1.75        ad 			mutex_enter(&fp->f_lock);
    524  1.75        ad 			fp->f_count++;
    525  1.75        ad 			mutex_exit(&fp->f_lock);
    526  1.74        ad 			LIST_INSERT_AFTER(fp, dfp, f_list);
    527  1.72        ad 			mutex_exit(&filelist_lock);
    528  1.50    atatat 			sysctl_dounpcb(&pcb, so);
    529  1.50    atatat 			error = copyout(&pcb, dp, out_size);
    530  1.75        ad 			closef(fp);
    531  1.72        ad 			mutex_enter(&filelist_lock);
    532  1.74        ad 			LIST_REMOVE(dfp, f_list);
    533  1.50    atatat 			if (error)
    534  1.50    atatat 				break;
    535  1.50    atatat 			dp += elem_size;
    536  1.50    atatat 			len -= elem_size;
    537  1.50    atatat 		}
    538  1.78       mrg 		needed += elem_size;
    539  1.78       mrg 		if (elem_count > 0 && elem_count != INT_MAX)
    540  1.78       mrg 			elem_count--;
    541  1.50    atatat 	}
    542  1.72        ad 	mutex_exit(&filelist_lock);
    543  1.74        ad 	fputdummy(dfp);
    544  1.74        ad  	*oldlenp = needed;
    545  1.50    atatat 	if (oldp == NULL)
    546  1.50    atatat 		*oldlenp += PCB_SLOP * sizeof(struct kinfo_pcb);
    547  1.74        ad  	sysctl_relock();
    548  1.50    atatat 
    549  1.81    dyoung 	return error;
    550  1.50    atatat }
    551  1.50    atatat 
    552  1.77     pooka static void
    553  1.79    cegger sysctl_net_setup(void)
    554  1.10       cgd {
    555  1.77     pooka 
    556  1.77     pooka 	KASSERT(domain_sysctllog == NULL);
    557  1.77     pooka 	sysctl_createv(&domain_sysctllog, 0, NULL, NULL,
    558  1.46    atatat 		       CTLFLAG_PERMANENT,
    559  1.48    atatat 		       CTLTYPE_NODE, "local",
    560  1.48    atatat 		       SYSCTL_DESCR("PF_LOCAL related settings"),
    561  1.44    atatat 		       NULL, 0, NULL, 0,
    562  1.44    atatat 		       CTL_NET, PF_LOCAL, CTL_EOL);
    563  1.77     pooka 	sysctl_createv(&domain_sysctllog, 0, NULL, NULL,
    564  1.50    atatat 		       CTLFLAG_PERMANENT,
    565  1.50    atatat 		       CTLTYPE_NODE, "stream",
    566  1.50    atatat 		       SYSCTL_DESCR("SOCK_STREAM settings"),
    567  1.50    atatat 		       NULL, 0, NULL, 0,
    568  1.50    atatat 		       CTL_NET, PF_LOCAL, SOCK_STREAM, CTL_EOL);
    569  1.77     pooka 	sysctl_createv(&domain_sysctllog, 0, NULL, NULL,
    570  1.50    atatat 		       CTLFLAG_PERMANENT,
    571  1.86      manu 		       CTLTYPE_NODE, "seqpacket",
    572  1.86      manu 		       SYSCTL_DESCR("SOCK_SEQPACKET settings"),
    573  1.86      manu 		       NULL, 0, NULL, 0,
    574  1.86      manu 		       CTL_NET, PF_LOCAL, SOCK_SEQPACKET, CTL_EOL);
    575  1.86      manu 	sysctl_createv(&domain_sysctllog, 0, NULL, NULL,
    576  1.86      manu 		       CTLFLAG_PERMANENT,
    577  1.50    atatat 		       CTLTYPE_NODE, "dgram",
    578  1.50    atatat 		       SYSCTL_DESCR("SOCK_DGRAM settings"),
    579  1.50    atatat 		       NULL, 0, NULL, 0,
    580  1.50    atatat 		       CTL_NET, PF_LOCAL, SOCK_DGRAM, CTL_EOL);
    581  1.10       cgd 
    582  1.77     pooka 	sysctl_createv(&domain_sysctllog, 0, NULL, NULL,
    583  1.50    atatat 		       CTLFLAG_PERMANENT,
    584  1.50    atatat 		       CTLTYPE_STRUCT, "pcblist",
    585  1.50    atatat 		       SYSCTL_DESCR("SOCK_STREAM protocol control block list"),
    586  1.50    atatat 		       sysctl_unpcblist, 0, NULL, 0,
    587  1.50    atatat 		       CTL_NET, PF_LOCAL, SOCK_STREAM, CTL_CREATE, CTL_EOL);
    588  1.77     pooka 	sysctl_createv(&domain_sysctllog, 0, NULL, NULL,
    589  1.50    atatat 		       CTLFLAG_PERMANENT,
    590  1.50    atatat 		       CTLTYPE_STRUCT, "pcblist",
    591  1.86      manu 		       SYSCTL_DESCR("SOCK_SEQPACKET protocol control "
    592  1.86      manu 				    "block list"),
    593  1.86      manu 		       sysctl_unpcblist, 0, NULL, 0,
    594  1.86      manu 		       CTL_NET, PF_LOCAL, SOCK_SEQPACKET, CTL_CREATE, CTL_EOL);
    595  1.86      manu 	sysctl_createv(&domain_sysctllog, 0, NULL, NULL,
    596  1.86      manu 		       CTLFLAG_PERMANENT,
    597  1.86      manu 		       CTLTYPE_STRUCT, "pcblist",
    598  1.50    atatat 		       SYSCTL_DESCR("SOCK_DGRAM protocol control block list"),
    599  1.50    atatat 		       sysctl_unpcblist, 0, NULL, 0,
    600  1.50    atatat 		       CTL_NET, PF_LOCAL, SOCK_DGRAM, CTL_CREATE, CTL_EOL);
    601   1.1       cgd }
    602   1.1       cgd 
    603   1.4    andrew void
    604  1.63    dyoung pfctlinput(int cmd, const struct sockaddr *sa)
    605   1.1       cgd {
    606  1.31  augustss 	struct domain *dp;
    607  1.47      matt 	const struct protosw *pr;
    608   1.1       cgd 
    609  1.63    dyoung 	DOMAIN_FOREACH(dp) {
    610  1.63    dyoung 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
    611  1.63    dyoung 			if (pr->pr_ctlinput != NULL)
    612  1.13  christos 				(*pr->pr_ctlinput)(cmd, sa, NULL);
    613  1.63    dyoung 		}
    614  1.63    dyoung 	}
    615  1.34    itojun }
    616  1.34    itojun 
    617  1.34    itojun void
    618  1.63    dyoung pfctlinput2(int cmd, const struct sockaddr *sa, void *ctlparam)
    619  1.34    itojun {
    620  1.34    itojun 	struct domain *dp;
    621  1.47      matt 	const struct protosw *pr;
    622  1.34    itojun 
    623  1.63    dyoung 	if (sa == NULL)
    624  1.34    itojun 		return;
    625  1.49      matt 
    626  1.49      matt 	DOMAIN_FOREACH(dp) {
    627  1.34    itojun 		/*
    628  1.34    itojun 		 * the check must be made by xx_ctlinput() anyways, to
    629  1.34    itojun 		 * make sure we use data item pointed to by ctlparam in
    630  1.34    itojun 		 * correct way.  the following check is made just for safety.
    631  1.34    itojun 		 */
    632  1.34    itojun 		if (dp->dom_family != sa->sa_family)
    633  1.34    itojun 			continue;
    634  1.34    itojun 
    635  1.63    dyoung 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
    636  1.63    dyoung 			if (pr->pr_ctlinput != NULL)
    637  1.34    itojun 				(*pr->pr_ctlinput)(cmd, sa, ctlparam);
    638  1.63    dyoung 		}
    639  1.34    itojun 	}
    640   1.1       cgd }
    641   1.1       cgd 
    642   1.4    andrew void
    643  1.62      yamt pfslowtimo(void *arg)
    644   1.1       cgd {
    645  1.31  augustss 	struct domain *dp;
    646  1.47      matt 	const struct protosw *pr;
    647   1.1       cgd 
    648  1.19   thorpej 	pfslowtimo_now++;
    649  1.19   thorpej 
    650  1.49      matt 	DOMAIN_FOREACH(dp) {
    651   1.1       cgd 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
    652   1.1       cgd 			if (pr->pr_slowtimo)
    653   1.1       cgd 				(*pr->pr_slowtimo)();
    654  1.49      matt 	}
    655  1.87     seanb 	callout_schedule(&pfslowtimo_ch, hz / PR_SLOWHZ);
    656   1.1       cgd }
    657   1.1       cgd 
    658   1.4    andrew void
    659  1.62      yamt pffasttimo(void *arg)
    660   1.1       cgd {
    661  1.31  augustss 	struct domain *dp;
    662  1.47      matt 	const struct protosw *pr;
    663  1.19   thorpej 
    664  1.19   thorpej 	pffasttimo_now++;
    665   1.1       cgd 
    666  1.49      matt 	DOMAIN_FOREACH(dp) {
    667   1.1       cgd 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
    668   1.1       cgd 			if (pr->pr_fasttimo)
    669   1.1       cgd 				(*pr->pr_fasttimo)();
    670  1.49      matt 	}
    671  1.87     seanb 	callout_schedule(&pffasttimo_ch, hz / PR_FASTHZ);
    672   1.1       cgd }
    673