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