Home | History | Annotate | Line # | Download | only in kern
uipc_domain.c revision 1.69
      1 /*	$NetBSD: uipc_domain.c,v 1.69 2007/08/30 02:17:34 dyoung 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.69 2007/08/30 02:17:34 dyoung 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/kauth.h>
     54 
     55 MALLOC_DECLARE(M_SOCKADDR);
     56 
     57 MALLOC_DEFINE(M_SOCKADDR, "sockaddr", "socket endpoints");
     58 
     59 void	pffasttimo(void *);
     60 void	pfslowtimo(void *);
     61 
     62 struct domainhead domains = STAILQ_HEAD_INITIALIZER(domains);
     63 static struct domain *domain_array[AF_MAX];
     64 
     65 callout_t pffasttimo_ch, pfslowtimo_ch;
     66 
     67 /*
     68  * Current time values for fast and slow timeouts.  We can use u_int
     69  * relatively safely.  The fast timer will roll over in 27 years and
     70  * the slow timer in 68 years.
     71  */
     72 u_int	pfslowtimo_now;
     73 u_int	pffasttimo_now;
     74 
     75 void
     76 domaininit(void)
     77 {
     78 	__link_set_decl(domains, struct domain);
     79 	struct domain * const * dpp;
     80 	struct domain *rt_domain = NULL;
     81 
     82 	/*
     83 	 * Add all of the domains.  Make sure the PF_ROUTE
     84 	 * domain is added last.
     85 	 */
     86 	__link_set_foreach(dpp, domains) {
     87 		if ((*dpp)->dom_family == PF_ROUTE)
     88 			rt_domain = *dpp;
     89 		else
     90 			domain_attach(*dpp);
     91 	}
     92 	if (rt_domain)
     93 		domain_attach(rt_domain);
     94 
     95 	callout_init(&pffasttimo_ch, 0);
     96 	callout_init(&pfslowtimo_ch, 0);
     97 
     98 	callout_reset(&pffasttimo_ch, 1, pffasttimo, NULL);
     99 	callout_reset(&pfslowtimo_ch, 1, pfslowtimo, NULL);
    100 }
    101 
    102 void
    103 domain_attach(struct domain *dp)
    104 {
    105 	const struct protosw *pr;
    106 
    107 	STAILQ_INSERT_TAIL(&domains, dp, dom_link);
    108 	if (dp->dom_family < __arraycount(domain_array))
    109 		domain_array[dp->dom_family] = dp;
    110 
    111 	if (dp->dom_init)
    112 		(*dp->dom_init)();
    113 
    114 #ifdef MBUFTRACE
    115 	if (dp->dom_mowner.mo_name[0] == '\0') {
    116 		strncpy(dp->dom_mowner.mo_name, dp->dom_name,
    117 		    sizeof(dp->dom_mowner.mo_name));
    118 		MOWNER_ATTACH(&dp->dom_mowner);
    119 	}
    120 #endif
    121 	for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
    122 		if (pr->pr_init)
    123 			(*pr->pr_init)();
    124 	}
    125 
    126 	if (max_linkhdr < 16)		/* XXX */
    127 		max_linkhdr = 16;
    128 	max_hdr = max_linkhdr + max_protohdr;
    129 	max_datalen = MHLEN - max_hdr;
    130 }
    131 
    132 struct domain *
    133 pffinddomain(int family)
    134 {
    135 	struct domain *dp;
    136 
    137 	if (family < __arraycount(domain_array) && domain_array[family] != NULL)
    138 		return domain_array[family];
    139 
    140 	DOMAIN_FOREACH(dp)
    141 		if (dp->dom_family == family)
    142 			return (dp);
    143 	return (NULL);
    144 }
    145 
    146 const struct protosw *
    147 pffindtype(int family, int type)
    148 {
    149 	struct domain *dp;
    150 	const struct protosw *pr;
    151 
    152 	dp = pffinddomain(family);
    153 	if (dp == NULL)
    154 		return (NULL);
    155 
    156 	for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
    157 		if (pr->pr_type && pr->pr_type == type)
    158 			return (pr);
    159 
    160 	return (NULL);
    161 }
    162 
    163 const struct protosw *
    164 pffindproto(int family, int protocol, int type)
    165 {
    166 	struct domain *dp;
    167 	const struct protosw *pr;
    168 	const struct protosw *maybe = NULL;
    169 
    170 	if (family == 0)
    171 		return (NULL);
    172 
    173 	dp = pffinddomain(family);
    174 	if (dp == NULL)
    175 		return (NULL);
    176 
    177 	for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
    178 		if ((pr->pr_protocol == protocol) && (pr->pr_type == type))
    179 			return (pr);
    180 
    181 		if (type == SOCK_RAW && pr->pr_type == SOCK_RAW &&
    182 		    pr->pr_protocol == 0 && maybe == NULL)
    183 			maybe = pr;
    184 	}
    185 	return (maybe);
    186 }
    187 
    188 struct sockaddr *
    189 sockaddr_alloc(sa_family_t af, socklen_t socklen, int flags)
    190 {
    191 	struct sockaddr *sa;
    192 	socklen_t reallen = MAX(socklen, offsetof(struct sockaddr, sa_data[0]));
    193 
    194 	if ((sa = malloc(reallen, M_SOCKADDR, flags)) == NULL)
    195 		return NULL;
    196 
    197 	sa->sa_family = af;
    198 	sa->sa_len = reallen;
    199 	return sa;
    200 }
    201 
    202 static void
    203 sockaddr_fixlen(struct sockaddr *dst, socklen_t socklen, uint8_t deslen)
    204 {
    205 	if (socklen < deslen)
    206 		panic("%s: source too long, %d bytes", __func__, deslen);
    207 	dst->sa_len = deslen;
    208 }
    209 
    210 struct sockaddr *
    211 sockaddr_copy(struct sockaddr *dst, socklen_t socklen,
    212     const struct sockaddr *src)
    213 {
    214 	KASSERT(dst->sa_family == src->sa_family);
    215 
    216 	if (__predict_false(dst->sa_len < src->sa_len))
    217 		sockaddr_fixlen(dst, socklen, src->sa_len);
    218 
    219 	memcpy(dst, src, src->sa_len);
    220 
    221 	return dst;
    222 }
    223 
    224 int
    225 sockaddr_cmp(const struct sockaddr *sa1, const struct sockaddr *sa2)
    226 {
    227 	int len, rc;
    228 	struct domain *dom;
    229 
    230 	if (sa1->sa_family != sa2->sa_family)
    231 		return sa1->sa_family - sa2->sa_family;
    232 
    233 	dom = pffinddomain(sa1->sa_family);
    234 
    235 	if (dom != NULL && dom->dom_sockaddr_cmp != NULL)
    236 		return (*dom->dom_sockaddr_cmp)(sa1, sa2);
    237 
    238 	len = MIN(sa1->sa_len, sa2->sa_len);
    239 
    240 	if (dom == NULL || dom->dom_sa_cmplen == 0) {
    241 		if ((rc = memcmp(sa1, sa2, len)) != 0)
    242 			return rc;
    243 		return sa1->sa_len - sa2->sa_len;
    244 	}
    245 
    246 	if ((rc = memcmp((const char *)sa1 + dom->dom_sa_cmpofs,
    247 		         (const char *)sa2 + dom->dom_sa_cmpofs,
    248 			 MIN(dom->dom_sa_cmplen,
    249 			     len - MIN(len, dom->dom_sa_cmpofs)))) != 0)
    250 		return rc;
    251 
    252 	return MIN(dom->dom_sa_cmplen + dom->dom_sa_cmpofs, sa1->sa_len) -
    253 	       MIN(dom->dom_sa_cmplen + dom->dom_sa_cmpofs, sa2->sa_len);
    254 }
    255 
    256 struct sockaddr *
    257 sockaddr_dup(const struct sockaddr *src, int flags)
    258 {
    259 	struct sockaddr *dst;
    260 
    261 	if ((dst = sockaddr_alloc(src->sa_family, src->sa_len, flags)) == NULL)
    262 		return NULL;
    263 
    264 	return sockaddr_copy(dst, dst->sa_len, src);
    265 }
    266 
    267 void
    268 sockaddr_free(struct sockaddr *sa)
    269 {
    270 	free(sa, M_SOCKADDR);
    271 }
    272 
    273 /*
    274  * sysctl helper to stuff PF_LOCAL pcbs into sysctl structures
    275  */
    276 static void
    277 sysctl_dounpcb(struct kinfo_pcb *pcb, const struct socket *so)
    278 {
    279 	struct unpcb *unp = sotounpcb(so);
    280 	struct sockaddr_un *un = unp->unp_addr;
    281 
    282 	memset(pcb, 0, sizeof(*pcb));
    283 
    284 	pcb->ki_family = so->so_proto->pr_domain->dom_family;
    285 	pcb->ki_type = so->so_proto->pr_type;
    286 	pcb->ki_protocol = so->so_proto->pr_protocol;
    287 	pcb->ki_pflags = unp->unp_flags;
    288 
    289 	pcb->ki_pcbaddr = PTRTOUINT64(unp);
    290 	/* pcb->ki_ppcbaddr = unp has no ppcb... */
    291 	pcb->ki_sockaddr = PTRTOUINT64(so);
    292 
    293 	pcb->ki_sostate = so->so_state;
    294 	/* pcb->ki_prstate = unp has no state... */
    295 
    296 	pcb->ki_rcvq = so->so_rcv.sb_cc;
    297 	pcb->ki_sndq = so->so_snd.sb_cc;
    298 
    299 	un = (struct sockaddr_un *)&pcb->ki_src;
    300 	/*
    301 	 * local domain sockets may bind without having a local
    302 	 * endpoint.  bleah!
    303 	 */
    304 	if (unp->unp_addr != NULL) {
    305 		un->sun_len = unp->unp_addr->sun_len;
    306 		un->sun_family = unp->unp_addr->sun_family;
    307 		strlcpy(un->sun_path, unp->unp_addr->sun_path,
    308 		    sizeof(pcb->ki_s));
    309 	}
    310 	else {
    311 		un->sun_len = offsetof(struct sockaddr_un, sun_path);
    312 		un->sun_family = pcb->ki_family;
    313 	}
    314 	if (unp->unp_conn != NULL) {
    315 		un = (struct sockaddr_un *)&pcb->ki_dst;
    316 		if (unp->unp_conn->unp_addr != NULL) {
    317 			un->sun_len = unp->unp_conn->unp_addr->sun_len;
    318 			un->sun_family = unp->unp_conn->unp_addr->sun_family;
    319 			un->sun_family = unp->unp_conn->unp_addr->sun_family;
    320 			strlcpy(un->sun_path, unp->unp_conn->unp_addr->sun_path,
    321 				sizeof(pcb->ki_d));
    322 		}
    323 		else {
    324 			un->sun_len = offsetof(struct sockaddr_un, sun_path);
    325 			un->sun_family = pcb->ki_family;
    326 		}
    327 	}
    328 
    329 	pcb->ki_inode = unp->unp_ino;
    330 	pcb->ki_vnode = PTRTOUINT64(unp->unp_vnode);
    331 	pcb->ki_conn = PTRTOUINT64(unp->unp_conn);
    332 	pcb->ki_refs = PTRTOUINT64(unp->unp_refs);
    333 	pcb->ki_nextref = PTRTOUINT64(unp->unp_nextref);
    334 }
    335 
    336 static int
    337 sysctl_unpcblist(SYSCTLFN_ARGS)
    338 {
    339 	struct file *fp;
    340 	struct socket *so;
    341 	struct kinfo_pcb pcb;
    342 	char *dp;
    343 	u_int op, arg;
    344 	size_t len, needed, elem_size, out_size;
    345 	int error, elem_count, pf, type, pf2;
    346 
    347 	if (namelen == 1 && name[0] == CTL_QUERY)
    348 		return (sysctl_query(SYSCTLFN_CALL(rnode)));
    349 
    350 	if (namelen != 4)
    351 		return (EINVAL);
    352 
    353 	if (oldp != NULL) {
    354 		len = *oldlenp;
    355 		elem_size = name[2];
    356 		elem_count = name[3];
    357 		if (elem_size != sizeof(pcb))
    358 			return EINVAL;
    359 	} else {
    360 		len = 0;
    361 		elem_size = sizeof(pcb);
    362 		elem_count = INT_MAX;
    363 	}
    364 	error = 0;
    365 	dp = oldp;
    366 	op = name[0];
    367 	arg = name[1];
    368 	out_size = elem_size;
    369 	needed = 0;
    370 
    371 	if (name - oname != 4)
    372 		return (EINVAL);
    373 
    374 	pf = oname[1];
    375 	type = oname[2];
    376 	pf2 = (oldp == NULL) ? 0 : pf;
    377 
    378 	/*
    379 	 * there's no "list" of local domain sockets, so we have
    380 	 * to walk the file list looking for them.  :-/
    381 	 */
    382 	LIST_FOREACH(fp, &filehead, f_list) {
    383 		if (kauth_authorize_generic(l->l_cred,
    384 		    KAUTH_GENERIC_CANSEE, fp->f_cred) != 0)
    385 			continue;
    386 		if (fp->f_type != DTYPE_SOCKET)
    387 			continue;
    388 		so = (struct socket *)fp->f_data;
    389 		if (so->so_type != type)
    390 			continue;
    391 		if (so->so_proto->pr_domain->dom_family != pf)
    392 			continue;
    393 		if (len >= elem_size && elem_count > 0) {
    394 			sysctl_dounpcb(&pcb, so);
    395 			error = copyout(&pcb, dp, out_size);
    396 			if (error)
    397 				break;
    398 			dp += elem_size;
    399 			len -= elem_size;
    400 		}
    401 		if (elem_count > 0) {
    402 			needed += elem_size;
    403 			if (elem_count != INT_MAX)
    404 				elem_count--;
    405 		}
    406 	}
    407 
    408 	*oldlenp = needed;
    409 	if (oldp == NULL)
    410 		*oldlenp += PCB_SLOP * sizeof(struct kinfo_pcb);
    411 
    412 	return (error);
    413 }
    414 
    415 SYSCTL_SETUP(sysctl_net_setup, "sysctl net subtree setup")
    416 {
    417 	sysctl_createv(clog, 0, NULL, NULL,
    418 		       CTLFLAG_PERMANENT,
    419 		       CTLTYPE_NODE, "net", NULL,
    420 		       NULL, 0, NULL, 0,
    421 		       CTL_NET, CTL_EOL);
    422 	sysctl_createv(clog, 0, NULL, NULL,
    423 		       CTLFLAG_PERMANENT,
    424 		       CTLTYPE_NODE, "local",
    425 		       SYSCTL_DESCR("PF_LOCAL related settings"),
    426 		       NULL, 0, NULL, 0,
    427 		       CTL_NET, PF_LOCAL, CTL_EOL);
    428 	sysctl_createv(clog, 0, NULL, NULL,
    429 		       CTLFLAG_PERMANENT,
    430 		       CTLTYPE_NODE, "stream",
    431 		       SYSCTL_DESCR("SOCK_STREAM settings"),
    432 		       NULL, 0, NULL, 0,
    433 		       CTL_NET, PF_LOCAL, SOCK_STREAM, CTL_EOL);
    434 	sysctl_createv(clog, 0, NULL, NULL,
    435 		       CTLFLAG_PERMANENT,
    436 		       CTLTYPE_NODE, "dgram",
    437 		       SYSCTL_DESCR("SOCK_DGRAM settings"),
    438 		       NULL, 0, NULL, 0,
    439 		       CTL_NET, PF_LOCAL, SOCK_DGRAM, CTL_EOL);
    440 
    441 	sysctl_createv(clog, 0, NULL, NULL,
    442 		       CTLFLAG_PERMANENT,
    443 		       CTLTYPE_STRUCT, "pcblist",
    444 		       SYSCTL_DESCR("SOCK_STREAM protocol control block list"),
    445 		       sysctl_unpcblist, 0, NULL, 0,
    446 		       CTL_NET, PF_LOCAL, SOCK_STREAM, CTL_CREATE, CTL_EOL);
    447 	sysctl_createv(clog, 0, NULL, NULL,
    448 		       CTLFLAG_PERMANENT,
    449 		       CTLTYPE_STRUCT, "pcblist",
    450 		       SYSCTL_DESCR("SOCK_DGRAM protocol control block list"),
    451 		       sysctl_unpcblist, 0, NULL, 0,
    452 		       CTL_NET, PF_LOCAL, SOCK_DGRAM, CTL_CREATE, CTL_EOL);
    453 }
    454 
    455 void
    456 pfctlinput(int cmd, const struct sockaddr *sa)
    457 {
    458 	struct domain *dp;
    459 	const struct protosw *pr;
    460 
    461 	DOMAIN_FOREACH(dp) {
    462 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
    463 			if (pr->pr_ctlinput != NULL)
    464 				(*pr->pr_ctlinput)(cmd, sa, NULL);
    465 		}
    466 	}
    467 }
    468 
    469 void
    470 pfctlinput2(int cmd, const struct sockaddr *sa, void *ctlparam)
    471 {
    472 	struct domain *dp;
    473 	const struct protosw *pr;
    474 
    475 	if (sa == NULL)
    476 		return;
    477 
    478 	DOMAIN_FOREACH(dp) {
    479 		/*
    480 		 * the check must be made by xx_ctlinput() anyways, to
    481 		 * make sure we use data item pointed to by ctlparam in
    482 		 * correct way.  the following check is made just for safety.
    483 		 */
    484 		if (dp->dom_family != sa->sa_family)
    485 			continue;
    486 
    487 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
    488 			if (pr->pr_ctlinput != NULL)
    489 				(*pr->pr_ctlinput)(cmd, sa, ctlparam);
    490 		}
    491 	}
    492 }
    493 
    494 void
    495 pfslowtimo(void *arg)
    496 {
    497 	struct domain *dp;
    498 	const struct protosw *pr;
    499 
    500 	pfslowtimo_now++;
    501 
    502 	DOMAIN_FOREACH(dp) {
    503 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
    504 			if (pr->pr_slowtimo)
    505 				(*pr->pr_slowtimo)();
    506 	}
    507 	callout_reset(&pfslowtimo_ch, hz / 2, pfslowtimo, NULL);
    508 }
    509 
    510 void
    511 pffasttimo(void *arg)
    512 {
    513 	struct domain *dp;
    514 	const struct protosw *pr;
    515 
    516 	pffasttimo_now++;
    517 
    518 	DOMAIN_FOREACH(dp) {
    519 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
    520 			if (pr->pr_fasttimo)
    521 				(*pr->pr_fasttimo)();
    522 	}
    523 	callout_reset(&pffasttimo_ch, hz / 5, pffasttimo, NULL);
    524 }
    525