xref: /netbsd-src/sys/kern/uipc_domain.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: uipc_domain.c,v 1.71 2007/09/19 04:33:42 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.71 2007/09/19 04:33:42 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 void *
189 sockaddr_addr(struct sockaddr *sa, socklen_t *slenp)
190 {
191 	const struct domain *dom;
192 
193 	if ((dom = pffinddomain(sa->sa_family)) == NULL ||
194 	    dom->dom_sockaddr_addr == NULL)
195 		return NULL;
196 
197 	return (*dom->dom_sockaddr_addr)(sa, slenp);
198 }
199 
200 const void *
201 sockaddr_const_addr(const struct sockaddr *sa, socklen_t *slenp)
202 {
203 	const struct domain *dom;
204 
205 	if ((dom = pffinddomain(sa->sa_family)) == NULL ||
206 	    dom->dom_sockaddr_const_addr == NULL)
207 		return NULL;
208 
209 	return (*dom->dom_sockaddr_const_addr)(sa, slenp);
210 }
211 
212 const struct sockaddr *
213 sockaddr_any(const struct sockaddr *sa)
214 {
215 	const struct domain *dom;
216 
217 	if ((dom = pffinddomain(sa->sa_family)) == NULL)
218 		return NULL;
219 
220 	return dom->dom_sa_any;
221 }
222 
223 const void *
224 sockaddr_anyaddr(const struct sockaddr *sa, socklen_t *slenp)
225 {
226 	const struct sockaddr *any;
227 
228 	if ((any = sockaddr_any(sa)) == NULL)
229 		return NULL;
230 
231 	return sockaddr_const_addr(any, slenp);
232 }
233 
234 struct sockaddr *
235 sockaddr_alloc(sa_family_t af, socklen_t socklen, int flags)
236 {
237 	struct sockaddr *sa;
238 	socklen_t reallen = MAX(socklen, offsetof(struct sockaddr, sa_data[0]));
239 
240 	if ((sa = malloc(reallen, M_SOCKADDR, flags)) == NULL)
241 		return NULL;
242 
243 	sa->sa_family = af;
244 	sa->sa_len = reallen;
245 	return sa;
246 }
247 
248 struct sockaddr *
249 sockaddr_copy(struct sockaddr *dst, socklen_t socklen,
250     const struct sockaddr *src)
251 {
252 	if (__predict_false(socklen < src->sa_len)) {
253 		panic("%s: source too long, %d < %d bytes", __func__, socklen,
254 		    src->sa_len);
255 	}
256 	return memcpy(dst, src, src->sa_len);
257 }
258 
259 int
260 sockaddr_cmp(const struct sockaddr *sa1, const struct sockaddr *sa2)
261 {
262 	int len, rc;
263 	struct domain *dom;
264 
265 	if (sa1->sa_family != sa2->sa_family)
266 		return sa1->sa_family - sa2->sa_family;
267 
268 	dom = pffinddomain(sa1->sa_family);
269 
270 	if (dom != NULL && dom->dom_sockaddr_cmp != NULL)
271 		return (*dom->dom_sockaddr_cmp)(sa1, sa2);
272 
273 	len = MIN(sa1->sa_len, sa2->sa_len);
274 
275 	if (dom == NULL || dom->dom_sa_cmplen == 0) {
276 		if ((rc = memcmp(sa1, sa2, len)) != 0)
277 			return rc;
278 		return sa1->sa_len - sa2->sa_len;
279 	}
280 
281 	if ((rc = memcmp((const char *)sa1 + dom->dom_sa_cmpofs,
282 		         (const char *)sa2 + dom->dom_sa_cmpofs,
283 			 MIN(dom->dom_sa_cmplen,
284 			     len - MIN(len, dom->dom_sa_cmpofs)))) != 0)
285 		return rc;
286 
287 	return MIN(dom->dom_sa_cmplen + dom->dom_sa_cmpofs, sa1->sa_len) -
288 	       MIN(dom->dom_sa_cmplen + dom->dom_sa_cmpofs, sa2->sa_len);
289 }
290 
291 struct sockaddr *
292 sockaddr_dup(const struct sockaddr *src, int flags)
293 {
294 	struct sockaddr *dst;
295 
296 	if ((dst = sockaddr_alloc(src->sa_family, src->sa_len, flags)) == NULL)
297 		return NULL;
298 
299 	return sockaddr_copy(dst, dst->sa_len, src);
300 }
301 
302 void
303 sockaddr_free(struct sockaddr *sa)
304 {
305 	free(sa, M_SOCKADDR);
306 }
307 
308 /*
309  * sysctl helper to stuff PF_LOCAL pcbs into sysctl structures
310  */
311 static void
312 sysctl_dounpcb(struct kinfo_pcb *pcb, const struct socket *so)
313 {
314 	struct unpcb *unp = sotounpcb(so);
315 	struct sockaddr_un *un = unp->unp_addr;
316 
317 	memset(pcb, 0, sizeof(*pcb));
318 
319 	pcb->ki_family = so->so_proto->pr_domain->dom_family;
320 	pcb->ki_type = so->so_proto->pr_type;
321 	pcb->ki_protocol = so->so_proto->pr_protocol;
322 	pcb->ki_pflags = unp->unp_flags;
323 
324 	pcb->ki_pcbaddr = PTRTOUINT64(unp);
325 	/* pcb->ki_ppcbaddr = unp has no ppcb... */
326 	pcb->ki_sockaddr = PTRTOUINT64(so);
327 
328 	pcb->ki_sostate = so->so_state;
329 	/* pcb->ki_prstate = unp has no state... */
330 
331 	pcb->ki_rcvq = so->so_rcv.sb_cc;
332 	pcb->ki_sndq = so->so_snd.sb_cc;
333 
334 	un = (struct sockaddr_un *)&pcb->ki_src;
335 	/*
336 	 * local domain sockets may bind without having a local
337 	 * endpoint.  bleah!
338 	 */
339 	if (unp->unp_addr != NULL) {
340 		un->sun_len = unp->unp_addr->sun_len;
341 		un->sun_family = unp->unp_addr->sun_family;
342 		strlcpy(un->sun_path, unp->unp_addr->sun_path,
343 		    sizeof(pcb->ki_s));
344 	}
345 	else {
346 		un->sun_len = offsetof(struct sockaddr_un, sun_path);
347 		un->sun_family = pcb->ki_family;
348 	}
349 	if (unp->unp_conn != NULL) {
350 		un = (struct sockaddr_un *)&pcb->ki_dst;
351 		if (unp->unp_conn->unp_addr != NULL) {
352 			un->sun_len = unp->unp_conn->unp_addr->sun_len;
353 			un->sun_family = unp->unp_conn->unp_addr->sun_family;
354 			un->sun_family = unp->unp_conn->unp_addr->sun_family;
355 			strlcpy(un->sun_path, unp->unp_conn->unp_addr->sun_path,
356 				sizeof(pcb->ki_d));
357 		}
358 		else {
359 			un->sun_len = offsetof(struct sockaddr_un, sun_path);
360 			un->sun_family = pcb->ki_family;
361 		}
362 	}
363 
364 	pcb->ki_inode = unp->unp_ino;
365 	pcb->ki_vnode = PTRTOUINT64(unp->unp_vnode);
366 	pcb->ki_conn = PTRTOUINT64(unp->unp_conn);
367 	pcb->ki_refs = PTRTOUINT64(unp->unp_refs);
368 	pcb->ki_nextref = PTRTOUINT64(unp->unp_nextref);
369 }
370 
371 static int
372 sysctl_unpcblist(SYSCTLFN_ARGS)
373 {
374 	struct file *fp;
375 	struct socket *so;
376 	struct kinfo_pcb pcb;
377 	char *dp;
378 	u_int op, arg;
379 	size_t len, needed, elem_size, out_size;
380 	int error, elem_count, pf, type, pf2;
381 
382 	if (namelen == 1 && name[0] == CTL_QUERY)
383 		return (sysctl_query(SYSCTLFN_CALL(rnode)));
384 
385 	if (namelen != 4)
386 		return (EINVAL);
387 
388 	if (oldp != NULL) {
389 		len = *oldlenp;
390 		elem_size = name[2];
391 		elem_count = name[3];
392 		if (elem_size != sizeof(pcb))
393 			return EINVAL;
394 	} else {
395 		len = 0;
396 		elem_size = sizeof(pcb);
397 		elem_count = INT_MAX;
398 	}
399 	error = 0;
400 	dp = oldp;
401 	op = name[0];
402 	arg = name[1];
403 	out_size = elem_size;
404 	needed = 0;
405 
406 	if (name - oname != 4)
407 		return (EINVAL);
408 
409 	pf = oname[1];
410 	type = oname[2];
411 	pf2 = (oldp == NULL) ? 0 : pf;
412 
413 	/*
414 	 * there's no "list" of local domain sockets, so we have
415 	 * to walk the file list looking for them.  :-/
416 	 */
417 	LIST_FOREACH(fp, &filehead, f_list) {
418 		if (kauth_authorize_generic(l->l_cred,
419 		    KAUTH_GENERIC_CANSEE, fp->f_cred) != 0)
420 			continue;
421 		if (fp->f_type != DTYPE_SOCKET)
422 			continue;
423 		so = (struct socket *)fp->f_data;
424 		if (so->so_type != type)
425 			continue;
426 		if (so->so_proto->pr_domain->dom_family != pf)
427 			continue;
428 		if (len >= elem_size && elem_count > 0) {
429 			sysctl_dounpcb(&pcb, so);
430 			error = copyout(&pcb, dp, out_size);
431 			if (error)
432 				break;
433 			dp += elem_size;
434 			len -= elem_size;
435 		}
436 		if (elem_count > 0) {
437 			needed += elem_size;
438 			if (elem_count != INT_MAX)
439 				elem_count--;
440 		}
441 	}
442 
443 	*oldlenp = needed;
444 	if (oldp == NULL)
445 		*oldlenp += PCB_SLOP * sizeof(struct kinfo_pcb);
446 
447 	return (error);
448 }
449 
450 SYSCTL_SETUP(sysctl_net_setup, "sysctl net subtree setup")
451 {
452 	sysctl_createv(clog, 0, NULL, NULL,
453 		       CTLFLAG_PERMANENT,
454 		       CTLTYPE_NODE, "net", NULL,
455 		       NULL, 0, NULL, 0,
456 		       CTL_NET, CTL_EOL);
457 	sysctl_createv(clog, 0, NULL, NULL,
458 		       CTLFLAG_PERMANENT,
459 		       CTLTYPE_NODE, "local",
460 		       SYSCTL_DESCR("PF_LOCAL related settings"),
461 		       NULL, 0, NULL, 0,
462 		       CTL_NET, PF_LOCAL, CTL_EOL);
463 	sysctl_createv(clog, 0, NULL, NULL,
464 		       CTLFLAG_PERMANENT,
465 		       CTLTYPE_NODE, "stream",
466 		       SYSCTL_DESCR("SOCK_STREAM settings"),
467 		       NULL, 0, NULL, 0,
468 		       CTL_NET, PF_LOCAL, SOCK_STREAM, CTL_EOL);
469 	sysctl_createv(clog, 0, NULL, NULL,
470 		       CTLFLAG_PERMANENT,
471 		       CTLTYPE_NODE, "dgram",
472 		       SYSCTL_DESCR("SOCK_DGRAM settings"),
473 		       NULL, 0, NULL, 0,
474 		       CTL_NET, PF_LOCAL, SOCK_DGRAM, CTL_EOL);
475 
476 	sysctl_createv(clog, 0, NULL, NULL,
477 		       CTLFLAG_PERMANENT,
478 		       CTLTYPE_STRUCT, "pcblist",
479 		       SYSCTL_DESCR("SOCK_STREAM protocol control block list"),
480 		       sysctl_unpcblist, 0, NULL, 0,
481 		       CTL_NET, PF_LOCAL, SOCK_STREAM, CTL_CREATE, CTL_EOL);
482 	sysctl_createv(clog, 0, NULL, NULL,
483 		       CTLFLAG_PERMANENT,
484 		       CTLTYPE_STRUCT, "pcblist",
485 		       SYSCTL_DESCR("SOCK_DGRAM protocol control block list"),
486 		       sysctl_unpcblist, 0, NULL, 0,
487 		       CTL_NET, PF_LOCAL, SOCK_DGRAM, CTL_CREATE, CTL_EOL);
488 }
489 
490 void
491 pfctlinput(int cmd, const struct sockaddr *sa)
492 {
493 	struct domain *dp;
494 	const struct protosw *pr;
495 
496 	DOMAIN_FOREACH(dp) {
497 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
498 			if (pr->pr_ctlinput != NULL)
499 				(*pr->pr_ctlinput)(cmd, sa, NULL);
500 		}
501 	}
502 }
503 
504 void
505 pfctlinput2(int cmd, const struct sockaddr *sa, void *ctlparam)
506 {
507 	struct domain *dp;
508 	const struct protosw *pr;
509 
510 	if (sa == NULL)
511 		return;
512 
513 	DOMAIN_FOREACH(dp) {
514 		/*
515 		 * the check must be made by xx_ctlinput() anyways, to
516 		 * make sure we use data item pointed to by ctlparam in
517 		 * correct way.  the following check is made just for safety.
518 		 */
519 		if (dp->dom_family != sa->sa_family)
520 			continue;
521 
522 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
523 			if (pr->pr_ctlinput != NULL)
524 				(*pr->pr_ctlinput)(cmd, sa, ctlparam);
525 		}
526 	}
527 }
528 
529 void
530 pfslowtimo(void *arg)
531 {
532 	struct domain *dp;
533 	const struct protosw *pr;
534 
535 	pfslowtimo_now++;
536 
537 	DOMAIN_FOREACH(dp) {
538 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
539 			if (pr->pr_slowtimo)
540 				(*pr->pr_slowtimo)();
541 	}
542 	callout_reset(&pfslowtimo_ch, hz / 2, pfslowtimo, NULL);
543 }
544 
545 void
546 pffasttimo(void *arg)
547 {
548 	struct domain *dp;
549 	const struct protosw *pr;
550 
551 	pffasttimo_now++;
552 
553 	DOMAIN_FOREACH(dp) {
554 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
555 			if (pr->pr_fasttimo)
556 				(*pr->pr_fasttimo)();
557 	}
558 	callout_reset(&pffasttimo_ch, hz / 5, pffasttimo, NULL);
559 }
560