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