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