xref: /freebsd/sys/kern/uipc_domain.c (revision 55620f43deef5c0eb5b4b0f675de18b30c8d1c2d)
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 
47 #include <net/vnet.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_DOMAININIT, SI_ORDER_ANY, domaininit, NULL);
63 
64 static void domainfinalize(void *);
65 SYSINIT(domainfin, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_FIRST, domainfinalize,
66     NULL);
67 
68 static struct callout pffast_callout;
69 static struct callout pfslow_callout;
70 
71 static void	pffasttimo(void *);
72 static void	pfslowtimo(void *);
73 
74 struct domain *domains;		/* registered protocol domains */
75 int domain_init_status = 0;
76 static struct mtx dom_mtx;		/* domain list lock */
77 MTX_SYSINIT(domain, &dom_mtx, "domain list", MTX_DEF);
78 
79 /*
80  * Dummy protocol specific user requests function pointer array.
81  * All functions return EOPNOTSUPP.
82  */
83 struct pr_usrreqs nousrreqs = {
84 	.pru_accept =		pru_accept_notsupp,
85 	.pru_attach =		pru_attach_notsupp,
86 	.pru_bind =		pru_bind_notsupp,
87 	.pru_connect =		pru_connect_notsupp,
88 	.pru_connect2 =		pru_connect2_notsupp,
89 	.pru_control =		pru_control_notsupp,
90 	.pru_disconnect	=	pru_disconnect_notsupp,
91 	.pru_listen =		pru_listen_notsupp,
92 	.pru_peeraddr =		pru_peeraddr_notsupp,
93 	.pru_rcvd =		pru_rcvd_notsupp,
94 	.pru_rcvoob =		pru_rcvoob_notsupp,
95 	.pru_send =		pru_send_notsupp,
96 	.pru_sense =		pru_sense_null,
97 	.pru_shutdown =		pru_shutdown_notsupp,
98 	.pru_sockaddr =		pru_sockaddr_notsupp,
99 	.pru_sosend =		pru_sosend_notsupp,
100 	.pru_soreceive =	pru_soreceive_notsupp,
101 	.pru_sopoll =		pru_sopoll_notsupp,
102 };
103 
104 static void
105 protosw_init(struct protosw *pr)
106 {
107 	struct pr_usrreqs *pu;
108 
109 	pu = pr->pr_usrreqs;
110 	KASSERT(pu != NULL, ("protosw_init: %ssw[%d] has no usrreqs!",
111 	    pr->pr_domain->dom_name,
112 	    (int)(pr - pr->pr_domain->dom_protosw)));
113 
114 	/*
115 	 * Protocol switch methods fall into three categories: mandatory,
116 	 * mandatory but protosw_init() provides a default, and optional.
117 	 *
118 	 * For true protocols (i.e., pru_attach != NULL), KASSERT truly
119 	 * mandatory methods with no defaults, and initialize defaults for
120 	 * other mandatory methods if the protocol hasn't defined an
121 	 * implementation (NULL function pointer).
122 	 */
123 #if 0
124 	if (pu->pru_attach != NULL) {
125 		KASSERT(pu->pru_abort != NULL,
126 		    ("protosw_init: %ssw[%d] pru_abort NULL",
127 		    pr->pr_domain->dom_name,
128 		    (int)(pr - pr->pr_domain->dom_protosw)));
129 		KASSERT(pu->pru_send != NULL,
130 		    ("protosw_init: %ssw[%d] pru_send NULL",
131 		    pr->pr_domain->dom_name,
132 		    (int)(pr - pr->pr_domain->dom_protosw)));
133 	}
134 #endif
135 
136 #define DEFAULT(foo, bar)	if ((foo) == NULL)  (foo) = (bar)
137 	DEFAULT(pu->pru_accept, pru_accept_notsupp);
138 	DEFAULT(pu->pru_aio_queue, pru_aio_queue_notsupp);
139 	DEFAULT(pu->pru_bind, pru_bind_notsupp);
140 	DEFAULT(pu->pru_bindat, pru_bindat_notsupp);
141 	DEFAULT(pu->pru_connect, pru_connect_notsupp);
142 	DEFAULT(pu->pru_connect2, pru_connect2_notsupp);
143 	DEFAULT(pu->pru_connectat, pru_connectat_notsupp);
144 	DEFAULT(pu->pru_control, pru_control_notsupp);
145 	DEFAULT(pu->pru_disconnect, pru_disconnect_notsupp);
146 	DEFAULT(pu->pru_listen, pru_listen_notsupp);
147 	DEFAULT(pu->pru_peeraddr, pru_peeraddr_notsupp);
148 	DEFAULT(pu->pru_rcvd, pru_rcvd_notsupp);
149 	DEFAULT(pu->pru_rcvoob, pru_rcvoob_notsupp);
150 	DEFAULT(pu->pru_sense, pru_sense_null);
151 	DEFAULT(pu->pru_shutdown, pru_shutdown_notsupp);
152 	DEFAULT(pu->pru_sockaddr, pru_sockaddr_notsupp);
153 	DEFAULT(pu->pru_sosend, sosend_generic);
154 	DEFAULT(pu->pru_soreceive, soreceive_generic);
155 	DEFAULT(pu->pru_sopoll, sopoll_generic);
156 	DEFAULT(pu->pru_ready, pru_ready_notsupp);
157 #undef DEFAULT
158 	if (pr->pr_init)
159 		(*pr->pr_init)();
160 }
161 
162 /*
163  * Add a new protocol domain to the list of supported domains
164  * Note: you cant unload it again because a socket may be using it.
165  * XXX can't fail at this time.
166  */
167 void
168 domain_init(void *arg)
169 {
170 	struct domain *dp = arg;
171 	struct protosw *pr;
172 
173 	if (dp->dom_init)
174 		(*dp->dom_init)();
175 	for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
176 		protosw_init(pr);
177 	/*
178 	 * update global information about maximums
179 	 */
180 	max_hdr = max_linkhdr + max_protohdr;
181 	max_datalen = MHLEN - max_hdr;
182 	if (max_datalen < 1)
183 		panic("%s: max_datalen < 1", __func__);
184 }
185 
186 #ifdef VIMAGE
187 void
188 vnet_domain_init(void *arg)
189 {
190 
191 	/* Virtualized case is no different -- call init functions. */
192 	domain_init(arg);
193 }
194 
195 void
196 vnet_domain_uninit(void *arg)
197 {
198 	struct domain *dp = arg;
199 
200 	if (dp->dom_destroy)
201 		(*dp->dom_destroy)();
202 }
203 #endif
204 
205 /*
206  * Add a new protocol domain to the list of supported domains
207  * Note: you cant unload it again because a socket may be using it.
208  * XXX can't fail at this time.
209  */
210 void
211 domain_add(void *data)
212 {
213 	struct domain *dp;
214 
215 	dp = (struct domain *)data;
216 	mtx_lock(&dom_mtx);
217 	dp->dom_next = domains;
218 	domains = dp;
219 
220 	KASSERT(domain_init_status >= 1,
221 	    ("attempt to domain_add(%s) before domaininit()",
222 	    dp->dom_name));
223 #ifndef INVARIANTS
224 	if (domain_init_status < 1)
225 		printf("WARNING: attempt to domain_add(%s) before "
226 		    "domaininit()\n", dp->dom_name);
227 #endif
228 #ifdef notyet
229 	KASSERT(domain_init_status < 2,
230 	    ("attempt to domain_add(%s) after domainfinalize()",
231 	    dp->dom_name));
232 #else
233 	if (domain_init_status >= 2)
234 		printf("WARNING: attempt to domain_add(%s) after "
235 		    "domainfinalize()\n", dp->dom_name);
236 #endif
237 	mtx_unlock(&dom_mtx);
238 }
239 
240 /* ARGSUSED*/
241 static void
242 domaininit(void *dummy)
243 {
244 
245 	if (max_linkhdr < 16)		/* XXX */
246 		max_linkhdr = 16;
247 
248 	callout_init(&pffast_callout, 1);
249 	callout_init(&pfslow_callout, 1);
250 
251 	mtx_lock(&dom_mtx);
252 	KASSERT(domain_init_status == 0, ("domaininit called too late!"));
253 	domain_init_status = 1;
254 	mtx_unlock(&dom_mtx);
255 }
256 
257 /* ARGSUSED*/
258 static void
259 domainfinalize(void *dummy)
260 {
261 
262 	mtx_lock(&dom_mtx);
263 	KASSERT(domain_init_status == 1, ("domainfinalize called too late!"));
264 	domain_init_status = 2;
265 	mtx_unlock(&dom_mtx);
266 
267 	callout_reset(&pffast_callout, 1, pffasttimo, NULL);
268 	callout_reset(&pfslow_callout, 1, pfslowtimo, NULL);
269 }
270 
271 struct domain *
272 pffinddomain(int family)
273 {
274 	struct domain *dp;
275 
276 	for (dp = domains; dp != NULL; dp = dp->dom_next)
277 		if (dp->dom_family == family)
278 			return (dp);
279 	return (NULL);
280 }
281 
282 struct protosw *
283 pffindtype(int family, int type)
284 {
285 	struct domain *dp;
286 	struct protosw *pr;
287 
288 	dp = pffinddomain(family);
289 	if (dp == NULL)
290 		return (NULL);
291 
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 (NULL);
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;
304 
305 	maybe = NULL;
306 	if (family == 0)
307 		return (NULL);
308 
309 	dp = pffinddomain(family);
310 	if (dp == NULL)
311 		return (NULL);
312 
313 	for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
314 		if ((pr->pr_protocol == protocol) && (pr->pr_type == type))
315 			return (pr);
316 
317 		if (type == SOCK_RAW && pr->pr_type == SOCK_RAW &&
318 		    pr->pr_protocol == 0 && maybe == NULL)
319 			maybe = pr;
320 	}
321 	return (maybe);
322 }
323 
324 /*
325  * The caller must make sure that the new protocol is fully set up and ready to
326  * accept requests before it is registered.
327  */
328 int
329 pf_proto_register(int family, struct protosw *npr)
330 {
331 	VNET_ITERATOR_DECL(vnet_iter);
332 	struct domain *dp;
333 	struct protosw *pr, *fpr;
334 
335 	/* Sanity checks. */
336 	if (family == 0)
337 		return (EPFNOSUPPORT);
338 	if (npr->pr_type == 0)
339 		return (EPROTOTYPE);
340 	if (npr->pr_protocol == 0)
341 		return (EPROTONOSUPPORT);
342 	if (npr->pr_usrreqs == NULL)
343 		return (ENXIO);
344 
345 	/* Try to find the specified domain based on the family. */
346 	dp = pffinddomain(family);
347 	if (dp == NULL)
348 		return (EPFNOSUPPORT);
349 
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 	VNET_LIST_RLOCK();
386 	VNET_FOREACH(vnet_iter) {
387 		CURVNET_SET_QUIET(vnet_iter);
388 		protosw_init(fpr);
389 		CURVNET_RESTORE();
390 	}
391 	VNET_LIST_RUNLOCK();
392 
393 	return (0);
394 }
395 
396 /*
397  * The caller must make sure the protocol and its functions correctly shut down
398  * all sockets and release all locks and memory references.
399  */
400 int
401 pf_proto_unregister(int family, int protocol, int type)
402 {
403 	struct domain *dp;
404 	struct protosw *pr, *dpr;
405 
406 	/* Sanity checks. */
407 	if (family == 0)
408 		return (EPFNOSUPPORT);
409 	if (protocol == 0)
410 		return (EPROTONOSUPPORT);
411 	if (type == 0)
412 		return (EPROTOTYPE);
413 
414 	/* Try to find the specified domain based on the family type. */
415 	dp = pffinddomain(family);
416 	if (dp == NULL)
417 		return (EPFNOSUPPORT);
418 
419 	dpr = NULL;
420 
421 	/* Lock out everyone else while we are manipulating the protosw. */
422 	mtx_lock(&dom_mtx);
423 
424 	/* The protocol must exist and only once. */
425 	for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
426 		if ((pr->pr_type == type) && (pr->pr_protocol == protocol)) {
427 			if (dpr != NULL) {
428 				mtx_unlock(&dom_mtx);
429 				return (EMLINK);   /* Should not happen! */
430 			} else
431 				dpr = pr;
432 		}
433 	}
434 
435 	/* Protocol does not exist. */
436 	if (dpr == NULL) {
437 		mtx_unlock(&dom_mtx);
438 		return (EPROTONOSUPPORT);
439 	}
440 
441 	/* De-orbit the protocol and make the slot available again. */
442 	dpr->pr_type = 0;
443 	dpr->pr_domain = dp;
444 	dpr->pr_protocol = PROTO_SPACER;
445 	dpr->pr_flags = 0;
446 	dpr->pr_input = NULL;
447 	dpr->pr_output = NULL;
448 	dpr->pr_ctlinput = NULL;
449 	dpr->pr_ctloutput = NULL;
450 	dpr->pr_init = NULL;
451 	dpr->pr_fasttimo = NULL;
452 	dpr->pr_slowtimo = NULL;
453 	dpr->pr_drain = NULL;
454 	dpr->pr_usrreqs = &nousrreqs;
455 
456 	/* Job is done, not more protection required. */
457 	mtx_unlock(&dom_mtx);
458 
459 	return (0);
460 }
461 
462 void
463 pfctlinput(int cmd, struct sockaddr *sa)
464 {
465 	struct domain *dp;
466 	struct protosw *pr;
467 
468 	for (dp = domains; dp; dp = dp->dom_next)
469 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
470 			if (pr->pr_ctlinput)
471 				(*pr->pr_ctlinput)(cmd, sa, (void *)0);
472 }
473 
474 void
475 pfctlinput2(int cmd, struct sockaddr *sa, void *ctlparam)
476 {
477 	struct domain *dp;
478 	struct protosw *pr;
479 
480 	if (!sa)
481 		return;
482 	for (dp = domains; dp; dp = dp->dom_next) {
483 		/*
484 		 * the check must be made by xx_ctlinput() anyways, to
485 		 * make sure we use data item pointed to by ctlparam in
486 		 * correct way.  the following check is made just for safety.
487 		 */
488 		if (dp->dom_family != sa->sa_family)
489 			continue;
490 
491 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
492 			if (pr->pr_ctlinput)
493 				(*pr->pr_ctlinput)(cmd, sa, ctlparam);
494 	}
495 }
496 
497 static void
498 pfslowtimo(void *arg)
499 {
500 	struct domain *dp;
501 	struct protosw *pr;
502 
503 	for (dp = domains; dp; dp = dp->dom_next)
504 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
505 			if (pr->pr_slowtimo)
506 				(*pr->pr_slowtimo)();
507 	callout_reset(&pfslow_callout, hz/2, pfslowtimo, NULL);
508 }
509 
510 static void
511 pffasttimo(void *arg)
512 {
513 	struct domain *dp;
514 	struct protosw *pr;
515 
516 	for (dp = domains; dp; dp = dp->dom_next)
517 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
518 			if (pr->pr_fasttimo)
519 				(*pr->pr_fasttimo)();
520 	callout_reset(&pffast_callout, hz/5, pffasttimo, NULL);
521 }
522