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