xref: /freebsd/sys/kern/uipc_domain.c (revision 195ebc7e9e4b129de810833791a19dfb4349d6a9)
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 
70 static struct callout pffast_callout;
71 static struct callout pfslow_callout;
72 
73 static void	pffasttimo(void *);
74 static void	pfslowtimo(void *);
75 
76 struct domain *domains;		/* registered protocol domains */
77 int domain_init_status = 0;
78 static struct mtx dom_mtx;		/* domain list lock */
79 MTX_SYSINIT(domain, &dom_mtx, "domain list", MTX_DEF);
80 
81 /*
82  * Dummy protocol specific user requests function pointer array.
83  * All functions return EOPNOTSUPP.
84  */
85 struct pr_usrreqs nousrreqs = {
86 	.pru_accept =		pru_accept_notsupp,
87 	.pru_attach =		pru_attach_notsupp,
88 	.pru_bind =		pru_bind_notsupp,
89 	.pru_connect =		pru_connect_notsupp,
90 	.pru_connect2 =		pru_connect2_notsupp,
91 	.pru_control =		pru_control_notsupp,
92 	.pru_disconnect	=	pru_disconnect_notsupp,
93 	.pru_listen =		pru_listen_notsupp,
94 	.pru_peeraddr =		pru_peeraddr_notsupp,
95 	.pru_rcvd =		pru_rcvd_notsupp,
96 	.pru_rcvoob =		pru_rcvoob_notsupp,
97 	.pru_send =		pru_send_notsupp,
98 	.pru_sense =		pru_sense_null,
99 	.pru_shutdown =		pru_shutdown_notsupp,
100 	.pru_sockaddr =		pru_sockaddr_notsupp,
101 	.pru_sosend =		pru_sosend_notsupp,
102 	.pru_soreceive =	pru_soreceive_notsupp,
103 	.pru_sopoll =		pru_sopoll_notsupp,
104 };
105 
106 #ifndef VIMAGE_GLOBALS
107 vnet_modinfo_t vnet_domain_modinfo = {
108 	.vmi_id		= VNET_MOD_DOMAIN,
109 	.vmi_name	= "domain",
110 	.vmi_iattach	= net_init_domain
111 };
112 #endif
113 
114 static void
115 protosw_init(struct protosw *pr)
116 {
117 	struct pr_usrreqs *pu;
118 
119 	pu = pr->pr_usrreqs;
120 	KASSERT(pu != NULL, ("protosw_init: %ssw[%d] has no usrreqs!",
121 	    pr->pr_domain->dom_name,
122 	    (int)(pr - pr->pr_domain->dom_protosw)));
123 
124 	/*
125 	 * Protocol switch methods fall into three categories: mandatory,
126 	 * mandatory but protosw_init() provides a default, and optional.
127 	 *
128 	 * For true protocols (i.e., pru_attach != NULL), KASSERT truly
129 	 * mandatory methods with no defaults, and initialize defaults for
130 	 * other mandatory methods if the protocol hasn't defined an
131 	 * implementation (NULL function pointer).
132 	 */
133 #if 0
134 	if (pu->pru_attach != NULL) {
135 		KASSERT(pu->pru_abort != NULL,
136 		    ("protosw_init: %ssw[%d] pru_abort NULL",
137 		    pr->pr_domain->dom_name,
138 		    (int)(pr - pr->pr_domain->dom_protosw)));
139 		KASSERT(pu->pru_send != NULL,
140 		    ("protosw_init: %ssw[%d] pru_send NULL",
141 		    pr->pr_domain->dom_name,
142 		    (int)(pr - pr->pr_domain->dom_protosw)));
143 	}
144 #endif
145 
146 #define DEFAULT(foo, bar)	if ((foo) == NULL)  (foo) = (bar)
147 	DEFAULT(pu->pru_accept, pru_accept_notsupp);
148 	DEFAULT(pu->pru_bind, pru_bind_notsupp);
149 	DEFAULT(pu->pru_connect, pru_connect_notsupp);
150 	DEFAULT(pu->pru_connect2, pru_connect2_notsupp);
151 	DEFAULT(pu->pru_control, pru_control_notsupp);
152 	DEFAULT(pu->pru_disconnect, pru_disconnect_notsupp);
153 	DEFAULT(pu->pru_listen, pru_listen_notsupp);
154 	DEFAULT(pu->pru_peeraddr, pru_peeraddr_notsupp);
155 	DEFAULT(pu->pru_rcvd, pru_rcvd_notsupp);
156 	DEFAULT(pu->pru_rcvoob, pru_rcvoob_notsupp);
157 	DEFAULT(pu->pru_sense, pru_sense_null);
158 	DEFAULT(pu->pru_shutdown, pru_shutdown_notsupp);
159 	DEFAULT(pu->pru_sockaddr, pru_sockaddr_notsupp);
160 	DEFAULT(pu->pru_sosend, sosend_generic);
161 	DEFAULT(pu->pru_soreceive, soreceive_generic);
162 	DEFAULT(pu->pru_sopoll, sopoll_generic);
163 #undef DEFAULT
164 	if (pr->pr_init)
165 		(*pr->pr_init)();
166 }
167 
168 /*
169  * Add a new protocol domain to the list of supported domains
170  * Note: you cant unload it again because a socket may be using it.
171  * XXX can't fail at this time.
172  */
173 static int
174 net_init_domain(const void *arg)
175 {
176 	const struct domain *dp = arg;
177 	struct protosw *pr;
178 
179 	if (dp->dom_init)
180 		(*dp->dom_init)();
181 	for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
182 		protosw_init(pr);
183 	/*
184 	 * update global information about maximums
185 	 */
186 	max_hdr = max_linkhdr + max_protohdr;
187 	max_datalen = MHLEN - max_hdr;
188 	if (max_datalen < 1)
189 		panic("%s: max_datalen < 1", __func__);
190 	return (0);
191 }
192 
193 /*
194  * Add a new protocol domain to the list of supported domains
195  * Note: you cant unload it again because a socket may be using it.
196  * XXX can't fail at this time.
197  */
198 void
199 net_add_domain(void *data)
200 {
201 	struct domain *dp;
202 
203 	dp = (struct domain *)data;
204 	mtx_lock(&dom_mtx);
205 	dp->dom_next = domains;
206 	domains = dp;
207 
208 	KASSERT(domain_init_status >= 1,
209 	    ("attempt to net_add_domain(%s) before domaininit()",
210 	    dp->dom_name));
211 #ifndef INVARIANTS
212 	if (domain_init_status < 1)
213 		printf("WARNING: attempt to net_add_domain(%s) before "
214 		    "domaininit()\n", dp->dom_name);
215 #endif
216 #ifdef notyet
217 	KASSERT(domain_init_status < 2,
218 	    ("attempt to net_add_domain(%s) after domainfinalize()",
219 	    dp->dom_name));
220 #else
221 	if (domain_init_status >= 2)
222 		printf("WARNING: attempt to net_add_domain(%s) after "
223 		    "domainfinalize()\n", dp->dom_name);
224 #endif
225 	mtx_unlock(&dom_mtx);
226 #ifndef VIMAGE_GLOBALS
227 	vnet_mod_register_multi(&vnet_domain_modinfo, dp, dp->dom_name);
228 #else
229 	net_init_domain(dp);
230 #endif
231 }
232 
233 static void
234 socket_zone_change(void *tag)
235 {
236 
237 	uma_zone_set_max(socket_zone, maxsockets);
238 }
239 
240 /* ARGSUSED*/
241 static void
242 domaininit(void *dummy)
243 {
244 
245 	/*
246 	 * Before we do any setup, make sure to initialize the
247 	 * zone allocator we get struct sockets from.
248 	 */
249 	socket_zone = uma_zcreate("socket", sizeof(struct socket), NULL, NULL,
250 	    NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
251 	uma_zone_set_max(socket_zone, maxsockets);
252 	EVENTHANDLER_REGISTER(maxsockets_change, socket_zone_change, NULL,
253 		EVENTHANDLER_PRI_FIRST);
254 
255 	if (max_linkhdr < 16)		/* XXX */
256 		max_linkhdr = 16;
257 
258 	callout_init(&pffast_callout, CALLOUT_MPSAFE);
259 	callout_init(&pfslow_callout, CALLOUT_MPSAFE);
260 
261 	mtx_lock(&dom_mtx);
262 	KASSERT(domain_init_status == 0, ("domaininit called too late!"));
263 	domain_init_status = 1;
264 	mtx_unlock(&dom_mtx);
265 }
266 
267 /* ARGSUSED*/
268 static void
269 domainfinalize(void *dummy)
270 {
271 
272 	mtx_lock(&dom_mtx);
273 	KASSERT(domain_init_status == 1, ("domainfinalize called too late!"));
274 	domain_init_status = 2;
275 	mtx_unlock(&dom_mtx);
276 
277 	callout_reset(&pffast_callout, 1, pffasttimo, NULL);
278 	callout_reset(&pfslow_callout, 1, pfslowtimo, NULL);
279 }
280 
281 struct protosw *
282 pffindtype(int family, int type)
283 {
284 	struct domain *dp;
285 	struct protosw *pr;
286 
287 	for (dp = domains; dp; dp = dp->dom_next)
288 		if (dp->dom_family == family)
289 			goto found;
290 	return (0);
291 found:
292 	for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
293 		if (pr->pr_type && pr->pr_type == type)
294 			return (pr);
295 	return (0);
296 }
297 
298 struct protosw *
299 pffindproto(int family, int protocol, int type)
300 {
301 	struct domain *dp;
302 	struct protosw *pr;
303 	struct protosw *maybe = 0;
304 
305 	if (family == 0)
306 		return (0);
307 	for (dp = domains; dp; dp = dp->dom_next)
308 		if (dp->dom_family == family)
309 			goto found;
310 	return (0);
311 found:
312 	for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
313 		if ((pr->pr_protocol == protocol) && (pr->pr_type == type))
314 			return (pr);
315 
316 		if (type == SOCK_RAW && pr->pr_type == SOCK_RAW &&
317 		    pr->pr_protocol == 0 && maybe == (struct protosw *)0)
318 			maybe = pr;
319 	}
320 	return (maybe);
321 }
322 
323 /*
324  * The caller must make sure that the new protocol is fully set up and ready to
325  * accept requests before it is registered.
326  */
327 int
328 pf_proto_register(int family, struct protosw *npr)
329 {
330 	struct domain *dp;
331 	struct protosw *pr, *fpr;
332 
333 	/* Sanity checks. */
334 	if (family == 0)
335 		return (EPFNOSUPPORT);
336 	if (npr->pr_type == 0)
337 		return (EPROTOTYPE);
338 	if (npr->pr_protocol == 0)
339 		return (EPROTONOSUPPORT);
340 	if (npr->pr_usrreqs == NULL)
341 		return (ENXIO);
342 
343 	/* Try to find the specified domain based on the family. */
344 	for (dp = domains; dp; dp = dp->dom_next)
345 		if (dp->dom_family == family)
346 			goto found;
347 	return (EPFNOSUPPORT);
348 
349 found:
350 	/* Initialize backpointer to struct domain. */
351 	npr->pr_domain = dp;
352 	fpr = NULL;
353 
354 	/*
355 	 * Protect us against races when two protocol registrations for
356 	 * the same protocol happen at the same time.
357 	 */
358 	mtx_lock(&dom_mtx);
359 
360 	/* The new protocol must not yet exist. */
361 	for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
362 		if ((pr->pr_type == npr->pr_type) &&
363 		    (pr->pr_protocol == npr->pr_protocol)) {
364 			mtx_unlock(&dom_mtx);
365 			return (EEXIST);	/* XXX: Check only protocol? */
366 		}
367 		/* While here, remember the first free spacer. */
368 		if ((fpr == NULL) && (pr->pr_protocol == PROTO_SPACER))
369 			fpr = pr;
370 	}
371 
372 	/* If no free spacer is found we can't add the new protocol. */
373 	if (fpr == NULL) {
374 		mtx_unlock(&dom_mtx);
375 		return (ENOMEM);
376 	}
377 
378 	/* Copy the new struct protosw over the spacer. */
379 	bcopy(npr, fpr, sizeof(*fpr));
380 
381 	/* Job is done, no more protection required. */
382 	mtx_unlock(&dom_mtx);
383 
384 	/* Initialize and activate the protocol. */
385 	protosw_init(fpr);
386 
387 	return (0);
388 }
389 
390 /*
391  * The caller must make sure the protocol and its functions correctly shut down
392  * all sockets and release all locks and memory references.
393  */
394 int
395 pf_proto_unregister(int family, int protocol, int type)
396 {
397 	struct domain *dp;
398 	struct protosw *pr, *dpr;
399 
400 	/* Sanity checks. */
401 	if (family == 0)
402 		return (EPFNOSUPPORT);
403 	if (protocol == 0)
404 		return (EPROTONOSUPPORT);
405 	if (type == 0)
406 		return (EPROTOTYPE);
407 
408 	/* Try to find the specified domain based on the family type. */
409 	for (dp = domains; dp; dp = dp->dom_next)
410 		if (dp->dom_family == family)
411 			goto found;
412 	return (EPFNOSUPPORT);
413 
414 found:
415 	dpr = NULL;
416 
417 	/* Lock out everyone else while we are manipulating the protosw. */
418 	mtx_lock(&dom_mtx);
419 
420 	/* The protocol must exist and only once. */
421 	for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
422 		if ((pr->pr_type == type) && (pr->pr_protocol == protocol)) {
423 			if (dpr != NULL) {
424 				mtx_unlock(&dom_mtx);
425 				return (EMLINK);   /* Should not happen! */
426 			} else
427 				dpr = pr;
428 		}
429 	}
430 
431 	/* Protocol does not exist. */
432 	if (dpr == NULL) {
433 		mtx_unlock(&dom_mtx);
434 		return (EPROTONOSUPPORT);
435 	}
436 
437 	/* De-orbit the protocol and make the slot available again. */
438 	dpr->pr_type = 0;
439 	dpr->pr_domain = dp;
440 	dpr->pr_protocol = PROTO_SPACER;
441 	dpr->pr_flags = 0;
442 	dpr->pr_input = NULL;
443 	dpr->pr_output = NULL;
444 	dpr->pr_ctlinput = NULL;
445 	dpr->pr_ctloutput = NULL;
446 	dpr->pr_init = NULL;
447 	dpr->pr_fasttimo = NULL;
448 	dpr->pr_slowtimo = NULL;
449 	dpr->pr_drain = NULL;
450 	dpr->pr_usrreqs = &nousrreqs;
451 
452 	/* Job is done, not more protection required. */
453 	mtx_unlock(&dom_mtx);
454 
455 	return (0);
456 }
457 
458 void
459 pfctlinput(int cmd, struct sockaddr *sa)
460 {
461 	struct domain *dp;
462 	struct protosw *pr;
463 
464 	for (dp = domains; dp; dp = dp->dom_next)
465 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
466 			if (pr->pr_ctlinput)
467 				(*pr->pr_ctlinput)(cmd, sa, (void *)0);
468 }
469 
470 void
471 pfctlinput2(int cmd, struct sockaddr *sa, void *ctlparam)
472 {
473 	struct domain *dp;
474 	struct protosw *pr;
475 
476 	if (!sa)
477 		return;
478 	for (dp = domains; dp; dp = dp->dom_next) {
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)
489 				(*pr->pr_ctlinput)(cmd, sa, ctlparam);
490 	}
491 }
492 
493 static void
494 pfslowtimo(void *arg)
495 {
496 	struct domain *dp;
497 	struct protosw *pr;
498 
499 	for (dp = domains; dp; dp = dp->dom_next)
500 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
501 			if (pr->pr_slowtimo)
502 				(*pr->pr_slowtimo)();
503 	callout_reset(&pfslow_callout, hz/2, pfslowtimo, NULL);
504 }
505 
506 static void
507 pffasttimo(void *arg)
508 {
509 	struct domain *dp;
510 	struct protosw *pr;
511 
512 	for (dp = domains; dp; dp = dp->dom_next)
513 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
514 			if (pr->pr_fasttimo)
515 				(*pr->pr_fasttimo)();
516 	callout_reset(&pffast_callout, hz/5, pffasttimo, NULL);
517 }
518