xref: /freebsd/sys/kern/uipc_domain.c (revision 5861f9665471e98e544f6fa3ce73c4912229ff82)
1 /*-
2  * Copyright (c) 1982, 1986, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *	@(#)uipc_domain.c	8.2 (Berkeley) 10/18/93
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include <sys/param.h>
36 #include <sys/socket.h>
37 #include <sys/protosw.h>
38 #include <sys/domain.h>
39 #include <sys/eventhandler.h>
40 #include <sys/mbuf.h>
41 #include <sys/kernel.h>
42 #include <sys/lock.h>
43 #include <sys/mutex.h>
44 #include <sys/socketvar.h>
45 #include <sys/systm.h>
46 #include <sys/vimage.h>
47 #include <vm/uma.h>
48 
49 /*
50  * System initialization
51  *
52  * Note: domain initialization takes place on a per domain basis
53  * as a result of traversing a SYSINIT linker set.  Most likely,
54  * each domain would want to call DOMAIN_SET(9) itself, which
55  * would cause the domain to be added just after domaininit()
56  * is called during startup.
57  *
58  * See DOMAIN_SET(9) for details on its use.
59  */
60 
61 static void domaininit(void *);
62 SYSINIT(domain, SI_SUB_PROTO_DOMAIN, SI_ORDER_FIRST, domaininit, NULL);
63 
64 static void domainfinalize(void *);
65 SYSINIT(domainfin, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_FIRST, domainfinalize,
66     NULL);
67 
68 static vnet_attach_fn net_init_domain;
69 #ifdef VIMAGE
70 static vnet_detach_fn net_detach_domain;
71 #endif
72 
73 static struct callout pffast_callout;
74 static struct callout pfslow_callout;
75 
76 static void	pffasttimo(void *);
77 static void	pfslowtimo(void *);
78 
79 struct domain *domains;		/* registered protocol domains */
80 int domain_init_status = 0;
81 static struct mtx dom_mtx;		/* domain list lock */
82 MTX_SYSINIT(domain, &dom_mtx, "domain list", MTX_DEF);
83 
84 /*
85  * Dummy protocol specific user requests function pointer array.
86  * All functions return EOPNOTSUPP.
87  */
88 struct pr_usrreqs nousrreqs = {
89 	.pru_accept =		pru_accept_notsupp,
90 	.pru_attach =		pru_attach_notsupp,
91 	.pru_bind =		pru_bind_notsupp,
92 	.pru_connect =		pru_connect_notsupp,
93 	.pru_connect2 =		pru_connect2_notsupp,
94 	.pru_control =		pru_control_notsupp,
95 	.pru_disconnect	=	pru_disconnect_notsupp,
96 	.pru_listen =		pru_listen_notsupp,
97 	.pru_peeraddr =		pru_peeraddr_notsupp,
98 	.pru_rcvd =		pru_rcvd_notsupp,
99 	.pru_rcvoob =		pru_rcvoob_notsupp,
100 	.pru_send =		pru_send_notsupp,
101 	.pru_sense =		pru_sense_null,
102 	.pru_shutdown =		pru_shutdown_notsupp,
103 	.pru_sockaddr =		pru_sockaddr_notsupp,
104 	.pru_sosend =		pru_sosend_notsupp,
105 	.pru_soreceive =	pru_soreceive_notsupp,
106 	.pru_sopoll =		pru_sopoll_notsupp,
107 };
108 
109 #ifndef VIMAGE_GLOBALS
110 vnet_modinfo_t vnet_domain_modinfo = {
111 	.vmi_id		= VNET_MOD_DOMAIN,
112 	.vmi_name	= "domain",
113 	.vmi_iattach	= net_init_domain,
114 #ifdef VIMAGE
115 	.vmi_idetach	= net_detach_domain,
116 #endif
117 };
118 #endif
119 
120 static void
121 protosw_init(struct protosw *pr)
122 {
123 	struct pr_usrreqs *pu;
124 
125 	pu = pr->pr_usrreqs;
126 	KASSERT(pu != NULL, ("protosw_init: %ssw[%d] has no usrreqs!",
127 	    pr->pr_domain->dom_name,
128 	    (int)(pr - pr->pr_domain->dom_protosw)));
129 
130 	/*
131 	 * Protocol switch methods fall into three categories: mandatory,
132 	 * mandatory but protosw_init() provides a default, and optional.
133 	 *
134 	 * For true protocols (i.e., pru_attach != NULL), KASSERT truly
135 	 * mandatory methods with no defaults, and initialize defaults for
136 	 * other mandatory methods if the protocol hasn't defined an
137 	 * implementation (NULL function pointer).
138 	 */
139 #if 0
140 	if (pu->pru_attach != NULL) {
141 		KASSERT(pu->pru_abort != NULL,
142 		    ("protosw_init: %ssw[%d] pru_abort NULL",
143 		    pr->pr_domain->dom_name,
144 		    (int)(pr - pr->pr_domain->dom_protosw)));
145 		KASSERT(pu->pru_send != NULL,
146 		    ("protosw_init: %ssw[%d] pru_send NULL",
147 		    pr->pr_domain->dom_name,
148 		    (int)(pr - pr->pr_domain->dom_protosw)));
149 	}
150 #endif
151 
152 #define DEFAULT(foo, bar)	if ((foo) == NULL)  (foo) = (bar)
153 	DEFAULT(pu->pru_accept, pru_accept_notsupp);
154 	DEFAULT(pu->pru_bind, pru_bind_notsupp);
155 	DEFAULT(pu->pru_connect, pru_connect_notsupp);
156 	DEFAULT(pu->pru_connect2, pru_connect2_notsupp);
157 	DEFAULT(pu->pru_control, pru_control_notsupp);
158 	DEFAULT(pu->pru_disconnect, pru_disconnect_notsupp);
159 	DEFAULT(pu->pru_listen, pru_listen_notsupp);
160 	DEFAULT(pu->pru_peeraddr, pru_peeraddr_notsupp);
161 	DEFAULT(pu->pru_rcvd, pru_rcvd_notsupp);
162 	DEFAULT(pu->pru_rcvoob, pru_rcvoob_notsupp);
163 	DEFAULT(pu->pru_sense, pru_sense_null);
164 	DEFAULT(pu->pru_shutdown, pru_shutdown_notsupp);
165 	DEFAULT(pu->pru_sockaddr, pru_sockaddr_notsupp);
166 	DEFAULT(pu->pru_sosend, sosend_generic);
167 	DEFAULT(pu->pru_soreceive, soreceive_generic);
168 	DEFAULT(pu->pru_sopoll, sopoll_generic);
169 #undef DEFAULT
170 	if (pr->pr_init)
171 		(*pr->pr_init)();
172 }
173 
174 /*
175  * Add a new protocol domain to the list of supported domains
176  * Note: you cant unload it again because a socket may be using it.
177  * XXX can't fail at this time.
178  */
179 static int
180 net_init_domain(const void *arg)
181 {
182 	const struct domain *dp = arg;
183 	struct protosw *pr;
184 
185 	if (dp->dom_init)
186 		(*dp->dom_init)();
187 	for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
188 		protosw_init(pr);
189 	/*
190 	 * update global information about maximums
191 	 */
192 	max_hdr = max_linkhdr + max_protohdr;
193 	max_datalen = MHLEN - max_hdr;
194 	if (max_datalen < 1)
195 		panic("%s: max_datalen < 1", __func__);
196 	return (0);
197 }
198 
199 #ifdef VIMAGE
200 /*
201  * Detach / free a domain instance.
202  */
203 static int
204 net_detach_domain(const void *arg)
205 {
206 	const struct domain *dp = arg;
207 	struct protosw *pr;
208 
209 	for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
210 		if (pr->pr_destroy)
211 			(*pr->pr_destroy)();
212 	if (dp->dom_destroy)
213 		(*dp->dom_destroy)();
214 
215 	return (0);
216 }
217 #endif
218 
219 /*
220  * Add a new protocol domain to the list of supported domains
221  * Note: you cant unload it again because a socket may be using it.
222  * XXX can't fail at this time.
223  */
224 void
225 net_add_domain(void *data)
226 {
227 	struct domain *dp;
228 
229 	dp = (struct domain *)data;
230 	mtx_lock(&dom_mtx);
231 	dp->dom_next = domains;
232 	domains = dp;
233 
234 	KASSERT(domain_init_status >= 1,
235 	    ("attempt to net_add_domain(%s) before domaininit()",
236 	    dp->dom_name));
237 #ifndef INVARIANTS
238 	if (domain_init_status < 1)
239 		printf("WARNING: attempt to net_add_domain(%s) before "
240 		    "domaininit()\n", dp->dom_name);
241 #endif
242 #ifdef notyet
243 	KASSERT(domain_init_status < 2,
244 	    ("attempt to net_add_domain(%s) after domainfinalize()",
245 	    dp->dom_name));
246 #else
247 	if (domain_init_status >= 2)
248 		printf("WARNING: attempt to net_add_domain(%s) after "
249 		    "domainfinalize()\n", dp->dom_name);
250 #endif
251 	mtx_unlock(&dom_mtx);
252 #ifndef VIMAGE_GLOBALS
253 	vnet_mod_register_multi(&vnet_domain_modinfo, dp, dp->dom_name);
254 #else
255 	net_init_domain(dp);
256 #endif
257 }
258 
259 static void
260 socket_zone_change(void *tag)
261 {
262 
263 	uma_zone_set_max(socket_zone, maxsockets);
264 }
265 
266 /* ARGSUSED*/
267 static void
268 domaininit(void *dummy)
269 {
270 
271 	/*
272 	 * Before we do any setup, make sure to initialize the
273 	 * zone allocator we get struct sockets from.
274 	 */
275 	socket_zone = uma_zcreate("socket", sizeof(struct socket), NULL, NULL,
276 	    NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
277 	uma_zone_set_max(socket_zone, maxsockets);
278 	EVENTHANDLER_REGISTER(maxsockets_change, socket_zone_change, NULL,
279 		EVENTHANDLER_PRI_FIRST);
280 
281 	if (max_linkhdr < 16)		/* XXX */
282 		max_linkhdr = 16;
283 
284 	callout_init(&pffast_callout, CALLOUT_MPSAFE);
285 	callout_init(&pfslow_callout, CALLOUT_MPSAFE);
286 
287 	mtx_lock(&dom_mtx);
288 	KASSERT(domain_init_status == 0, ("domaininit called too late!"));
289 	domain_init_status = 1;
290 	mtx_unlock(&dom_mtx);
291 }
292 
293 /* ARGSUSED*/
294 static void
295 domainfinalize(void *dummy)
296 {
297 
298 	mtx_lock(&dom_mtx);
299 	KASSERT(domain_init_status == 1, ("domainfinalize called too late!"));
300 	domain_init_status = 2;
301 	mtx_unlock(&dom_mtx);
302 
303 	callout_reset(&pffast_callout, 1, pffasttimo, NULL);
304 	callout_reset(&pfslow_callout, 1, pfslowtimo, NULL);
305 }
306 
307 struct protosw *
308 pffindtype(int family, int type)
309 {
310 	struct domain *dp;
311 	struct protosw *pr;
312 
313 	for (dp = domains; dp; dp = dp->dom_next)
314 		if (dp->dom_family == family)
315 			goto found;
316 	return (0);
317 found:
318 	for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
319 		if (pr->pr_type && pr->pr_type == type)
320 			return (pr);
321 	return (0);
322 }
323 
324 struct protosw *
325 pffindproto(int family, int protocol, int type)
326 {
327 	struct domain *dp;
328 	struct protosw *pr;
329 	struct protosw *maybe = 0;
330 
331 	if (family == 0)
332 		return (0);
333 	for (dp = domains; dp; dp = dp->dom_next)
334 		if (dp->dom_family == family)
335 			goto found;
336 	return (0);
337 found:
338 	for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
339 		if ((pr->pr_protocol == protocol) && (pr->pr_type == type))
340 			return (pr);
341 
342 		if (type == SOCK_RAW && pr->pr_type == SOCK_RAW &&
343 		    pr->pr_protocol == 0 && maybe == (struct protosw *)0)
344 			maybe = pr;
345 	}
346 	return (maybe);
347 }
348 
349 /*
350  * The caller must make sure that the new protocol is fully set up and ready to
351  * accept requests before it is registered.
352  */
353 int
354 pf_proto_register(int family, struct protosw *npr)
355 {
356 	struct domain *dp;
357 	struct protosw *pr, *fpr;
358 
359 	/* Sanity checks. */
360 	if (family == 0)
361 		return (EPFNOSUPPORT);
362 	if (npr->pr_type == 0)
363 		return (EPROTOTYPE);
364 	if (npr->pr_protocol == 0)
365 		return (EPROTONOSUPPORT);
366 	if (npr->pr_usrreqs == NULL)
367 		return (ENXIO);
368 
369 	/* Try to find the specified domain based on the family. */
370 	for (dp = domains; dp; dp = dp->dom_next)
371 		if (dp->dom_family == family)
372 			goto found;
373 	return (EPFNOSUPPORT);
374 
375 found:
376 	/* Initialize backpointer to struct domain. */
377 	npr->pr_domain = dp;
378 	fpr = NULL;
379 
380 	/*
381 	 * Protect us against races when two protocol registrations for
382 	 * the same protocol happen at the same time.
383 	 */
384 	mtx_lock(&dom_mtx);
385 
386 	/* The new protocol must not yet exist. */
387 	for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
388 		if ((pr->pr_type == npr->pr_type) &&
389 		    (pr->pr_protocol == npr->pr_protocol)) {
390 			mtx_unlock(&dom_mtx);
391 			return (EEXIST);	/* XXX: Check only protocol? */
392 		}
393 		/* While here, remember the first free spacer. */
394 		if ((fpr == NULL) && (pr->pr_protocol == PROTO_SPACER))
395 			fpr = pr;
396 	}
397 
398 	/* If no free spacer is found we can't add the new protocol. */
399 	if (fpr == NULL) {
400 		mtx_unlock(&dom_mtx);
401 		return (ENOMEM);
402 	}
403 
404 	/* Copy the new struct protosw over the spacer. */
405 	bcopy(npr, fpr, sizeof(*fpr));
406 
407 	/* Job is done, no more protection required. */
408 	mtx_unlock(&dom_mtx);
409 
410 	/* Initialize and activate the protocol. */
411 	protosw_init(fpr);
412 
413 	return (0);
414 }
415 
416 /*
417  * The caller must make sure the protocol and its functions correctly shut down
418  * all sockets and release all locks and memory references.
419  */
420 int
421 pf_proto_unregister(int family, int protocol, int type)
422 {
423 	struct domain *dp;
424 	struct protosw *pr, *dpr;
425 
426 	/* Sanity checks. */
427 	if (family == 0)
428 		return (EPFNOSUPPORT);
429 	if (protocol == 0)
430 		return (EPROTONOSUPPORT);
431 	if (type == 0)
432 		return (EPROTOTYPE);
433 
434 	/* Try to find the specified domain based on the family type. */
435 	for (dp = domains; dp; dp = dp->dom_next)
436 		if (dp->dom_family == family)
437 			goto found;
438 	return (EPFNOSUPPORT);
439 
440 found:
441 	dpr = NULL;
442 
443 	/* Lock out everyone else while we are manipulating the protosw. */
444 	mtx_lock(&dom_mtx);
445 
446 	/* The protocol must exist and only once. */
447 	for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
448 		if ((pr->pr_type == type) && (pr->pr_protocol == protocol)) {
449 			if (dpr != NULL) {
450 				mtx_unlock(&dom_mtx);
451 				return (EMLINK);   /* Should not happen! */
452 			} else
453 				dpr = pr;
454 		}
455 	}
456 
457 	/* Protocol does not exist. */
458 	if (dpr == NULL) {
459 		mtx_unlock(&dom_mtx);
460 		return (EPROTONOSUPPORT);
461 	}
462 
463 	/* De-orbit the protocol and make the slot available again. */
464 	dpr->pr_type = 0;
465 	dpr->pr_domain = dp;
466 	dpr->pr_protocol = PROTO_SPACER;
467 	dpr->pr_flags = 0;
468 	dpr->pr_input = NULL;
469 	dpr->pr_output = NULL;
470 	dpr->pr_ctlinput = NULL;
471 	dpr->pr_ctloutput = NULL;
472 	dpr->pr_init = NULL;
473 	dpr->pr_fasttimo = NULL;
474 	dpr->pr_slowtimo = NULL;
475 	dpr->pr_drain = NULL;
476 	dpr->pr_usrreqs = &nousrreqs;
477 
478 	/* Job is done, not more protection required. */
479 	mtx_unlock(&dom_mtx);
480 
481 	return (0);
482 }
483 
484 void
485 pfctlinput(int cmd, struct sockaddr *sa)
486 {
487 	struct domain *dp;
488 	struct protosw *pr;
489 
490 	for (dp = domains; dp; dp = dp->dom_next)
491 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
492 			if (pr->pr_ctlinput)
493 				(*pr->pr_ctlinput)(cmd, sa, (void *)0);
494 }
495 
496 void
497 pfctlinput2(int cmd, struct sockaddr *sa, void *ctlparam)
498 {
499 	struct domain *dp;
500 	struct protosw *pr;
501 
502 	if (!sa)
503 		return;
504 	for (dp = domains; dp; dp = dp->dom_next) {
505 		/*
506 		 * the check must be made by xx_ctlinput() anyways, to
507 		 * make sure we use data item pointed to by ctlparam in
508 		 * correct way.  the following check is made just for safety.
509 		 */
510 		if (dp->dom_family != sa->sa_family)
511 			continue;
512 
513 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
514 			if (pr->pr_ctlinput)
515 				(*pr->pr_ctlinput)(cmd, sa, ctlparam);
516 	}
517 }
518 
519 static void
520 pfslowtimo(void *arg)
521 {
522 	struct domain *dp;
523 	struct protosw *pr;
524 
525 	for (dp = domains; dp; dp = dp->dom_next)
526 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
527 			if (pr->pr_slowtimo)
528 				(*pr->pr_slowtimo)();
529 	callout_reset(&pfslow_callout, hz/2, pfslowtimo, NULL);
530 }
531 
532 static void
533 pffasttimo(void *arg)
534 {
535 	struct domain *dp;
536 	struct protosw *pr;
537 
538 	for (dp = domains; dp; dp = dp->dom_next)
539 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
540 			if (pr->pr_fasttimo)
541 				(*pr->pr_fasttimo)();
542 	callout_reset(&pffast_callout, hz/5, pffasttimo, NULL);
543 }
544