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