Deleted Added
full compact
uipc_domain.c (247667) uipc_domain.c (275329)
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>
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: head/sys/kern/uipc_domain.c 247667 2013-03-02 21:11:30Z pjd $");
33__FBSDID("$FreeBSD: head/sys/kern/uipc_domain.c 275329 2014-11-30 13:24:21Z glebius $");
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
61static void domaininit(void *);
62SYSINIT(domain, SI_SUB_PROTO_DOMAININIT, SI_ORDER_ANY, domaininit, NULL);
63
64static void domainfinalize(void *);
65SYSINIT(domainfin, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_FIRST, domainfinalize,
66 NULL);
67
68static struct callout pffast_callout;
69static struct callout pfslow_callout;
70
71static void pffasttimo(void *);
72static void pfslowtimo(void *);
73
74struct domain *domains; /* registered protocol domains */
75int domain_init_status = 0;
76static struct mtx dom_mtx; /* domain list lock */
77MTX_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 */
83struct 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
104static void
105protosw_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_bind, pru_bind_notsupp);
139 DEFAULT(pu->pru_bindat, pru_bindat_notsupp);
140 DEFAULT(pu->pru_connect, pru_connect_notsupp);
141 DEFAULT(pu->pru_connect2, pru_connect2_notsupp);
142 DEFAULT(pu->pru_connectat, pru_connectat_notsupp);
143 DEFAULT(pu->pru_control, pru_control_notsupp);
144 DEFAULT(pu->pru_disconnect, pru_disconnect_notsupp);
145 DEFAULT(pu->pru_listen, pru_listen_notsupp);
146 DEFAULT(pu->pru_peeraddr, pru_peeraddr_notsupp);
147 DEFAULT(pu->pru_rcvd, pru_rcvd_notsupp);
148 DEFAULT(pu->pru_rcvoob, pru_rcvoob_notsupp);
149 DEFAULT(pu->pru_sense, pru_sense_null);
150 DEFAULT(pu->pru_shutdown, pru_shutdown_notsupp);
151 DEFAULT(pu->pru_sockaddr, pru_sockaddr_notsupp);
152 DEFAULT(pu->pru_sosend, sosend_generic);
153 DEFAULT(pu->pru_soreceive, soreceive_generic);
154 DEFAULT(pu->pru_sopoll, sopoll_generic);
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
61static void domaininit(void *);
62SYSINIT(domain, SI_SUB_PROTO_DOMAININIT, SI_ORDER_ANY, domaininit, NULL);
63
64static void domainfinalize(void *);
65SYSINIT(domainfin, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_FIRST, domainfinalize,
66 NULL);
67
68static struct callout pffast_callout;
69static struct callout pfslow_callout;
70
71static void pffasttimo(void *);
72static void pfslowtimo(void *);
73
74struct domain *domains; /* registered protocol domains */
75int domain_init_status = 0;
76static struct mtx dom_mtx; /* domain list lock */
77MTX_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 */
83struct 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
104static void
105protosw_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_bind, pru_bind_notsupp);
139 DEFAULT(pu->pru_bindat, pru_bindat_notsupp);
140 DEFAULT(pu->pru_connect, pru_connect_notsupp);
141 DEFAULT(pu->pru_connect2, pru_connect2_notsupp);
142 DEFAULT(pu->pru_connectat, pru_connectat_notsupp);
143 DEFAULT(pu->pru_control, pru_control_notsupp);
144 DEFAULT(pu->pru_disconnect, pru_disconnect_notsupp);
145 DEFAULT(pu->pru_listen, pru_listen_notsupp);
146 DEFAULT(pu->pru_peeraddr, pru_peeraddr_notsupp);
147 DEFAULT(pu->pru_rcvd, pru_rcvd_notsupp);
148 DEFAULT(pu->pru_rcvoob, pru_rcvoob_notsupp);
149 DEFAULT(pu->pru_sense, pru_sense_null);
150 DEFAULT(pu->pru_shutdown, pru_shutdown_notsupp);
151 DEFAULT(pu->pru_sockaddr, pru_sockaddr_notsupp);
152 DEFAULT(pu->pru_sosend, sosend_generic);
153 DEFAULT(pu->pru_soreceive, soreceive_generic);
154 DEFAULT(pu->pru_sopoll, sopoll_generic);
155 DEFAULT(pu->pru_ready, pru_ready_notsupp);
155#undef DEFAULT
156 if (pr->pr_init)
157 (*pr->pr_init)();
158}
159
160/*
161 * Add a new protocol domain to the list of supported domains
162 * Note: you cant unload it again because a socket may be using it.
163 * XXX can't fail at this time.
164 */
165void
166domain_init(void *arg)
167{
168 struct domain *dp = arg;
169 struct protosw *pr;
170
171 if (dp->dom_init)
172 (*dp->dom_init)();
173 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
174 protosw_init(pr);
175 /*
176 * update global information about maximums
177 */
178 max_hdr = max_linkhdr + max_protohdr;
179 max_datalen = MHLEN - max_hdr;
180 if (max_datalen < 1)
181 panic("%s: max_datalen < 1", __func__);
182}
183
184#ifdef VIMAGE
185void
186vnet_domain_init(void *arg)
187{
188
189 /* Virtualized case is no different -- call init functions. */
190 domain_init(arg);
191}
192
193void
194vnet_domain_uninit(void *arg)
195{
196 struct domain *dp = arg;
197 struct protosw *pr;
198
199 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
200 if (pr->pr_destroy)
201 (*pr->pr_destroy)();
202 if (dp->dom_destroy)
203 (*dp->dom_destroy)();
204}
205#endif
206
207/*
208 * Add a new protocol domain to the list of supported domains
209 * Note: you cant unload it again because a socket may be using it.
210 * XXX can't fail at this time.
211 */
212void
213domain_add(void *data)
214{
215 struct domain *dp;
216
217 dp = (struct domain *)data;
218 mtx_lock(&dom_mtx);
219 dp->dom_next = domains;
220 domains = dp;
221
222 KASSERT(domain_init_status >= 1,
223 ("attempt to domain_add(%s) before domaininit()",
224 dp->dom_name));
225#ifndef INVARIANTS
226 if (domain_init_status < 1)
227 printf("WARNING: attempt to domain_add(%s) before "
228 "domaininit()\n", dp->dom_name);
229#endif
230#ifdef notyet
231 KASSERT(domain_init_status < 2,
232 ("attempt to domain_add(%s) after domainfinalize()",
233 dp->dom_name));
234#else
235 if (domain_init_status >= 2)
236 printf("WARNING: attempt to domain_add(%s) after "
237 "domainfinalize()\n", dp->dom_name);
238#endif
239 mtx_unlock(&dom_mtx);
240}
241
242/* ARGSUSED*/
243static void
244domaininit(void *dummy)
245{
246
247 if (max_linkhdr < 16) /* XXX */
248 max_linkhdr = 16;
249
250 callout_init(&pffast_callout, CALLOUT_MPSAFE);
251 callout_init(&pfslow_callout, CALLOUT_MPSAFE);
252
253 mtx_lock(&dom_mtx);
254 KASSERT(domain_init_status == 0, ("domaininit called too late!"));
255 domain_init_status = 1;
256 mtx_unlock(&dom_mtx);
257}
258
259/* ARGSUSED*/
260static void
261domainfinalize(void *dummy)
262{
263
264 mtx_lock(&dom_mtx);
265 KASSERT(domain_init_status == 1, ("domainfinalize called too late!"));
266 domain_init_status = 2;
267 mtx_unlock(&dom_mtx);
268
269 callout_reset(&pffast_callout, 1, pffasttimo, NULL);
270 callout_reset(&pfslow_callout, 1, pfslowtimo, NULL);
271}
272
273struct domain *
274pffinddomain(int family)
275{
276 struct domain *dp;
277
278 for (dp = domains; dp != NULL; dp = dp->dom_next)
279 if (dp->dom_family == family)
280 return (dp);
281 return (NULL);
282}
283
284struct protosw *
285pffindtype(int family, int type)
286{
287 struct domain *dp;
288 struct protosw *pr;
289
290 dp = pffinddomain(family);
291 if (dp == NULL)
292 return (NULL);
293
294 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
295 if (pr->pr_type && pr->pr_type == type)
296 return (pr);
297 return (NULL);
298}
299
300struct protosw *
301pffindproto(int family, int protocol, int type)
302{
303 struct domain *dp;
304 struct protosw *pr;
305 struct protosw *maybe;
306
307 maybe = NULL;
308 if (family == 0)
309 return (NULL);
310
311 dp = pffinddomain(family);
312 if (dp == NULL)
313 return (NULL);
314
315 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
316 if ((pr->pr_protocol == protocol) && (pr->pr_type == type))
317 return (pr);
318
319 if (type == SOCK_RAW && pr->pr_type == SOCK_RAW &&
320 pr->pr_protocol == 0 && maybe == NULL)
321 maybe = pr;
322 }
323 return (maybe);
324}
325
326/*
327 * The caller must make sure that the new protocol is fully set up and ready to
328 * accept requests before it is registered.
329 */
330int
331pf_proto_register(int family, struct protosw *npr)
332{
333 VNET_ITERATOR_DECL(vnet_iter);
334 struct domain *dp;
335 struct protosw *pr, *fpr;
336
337 /* Sanity checks. */
338 if (family == 0)
339 return (EPFNOSUPPORT);
340 if (npr->pr_type == 0)
341 return (EPROTOTYPE);
342 if (npr->pr_protocol == 0)
343 return (EPROTONOSUPPORT);
344 if (npr->pr_usrreqs == NULL)
345 return (ENXIO);
346
347 /* Try to find the specified domain based on the family. */
348 dp = pffinddomain(family);
349 if (dp == NULL)
350 return (EPFNOSUPPORT);
351
352 /* Initialize backpointer to struct domain. */
353 npr->pr_domain = dp;
354 fpr = NULL;
355
356 /*
357 * Protect us against races when two protocol registrations for
358 * the same protocol happen at the same time.
359 */
360 mtx_lock(&dom_mtx);
361
362 /* The new protocol must not yet exist. */
363 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
364 if ((pr->pr_type == npr->pr_type) &&
365 (pr->pr_protocol == npr->pr_protocol)) {
366 mtx_unlock(&dom_mtx);
367 return (EEXIST); /* XXX: Check only protocol? */
368 }
369 /* While here, remember the first free spacer. */
370 if ((fpr == NULL) && (pr->pr_protocol == PROTO_SPACER))
371 fpr = pr;
372 }
373
374 /* If no free spacer is found we can't add the new protocol. */
375 if (fpr == NULL) {
376 mtx_unlock(&dom_mtx);
377 return (ENOMEM);
378 }
379
380 /* Copy the new struct protosw over the spacer. */
381 bcopy(npr, fpr, sizeof(*fpr));
382
383 /* Job is done, no more protection required. */
384 mtx_unlock(&dom_mtx);
385
386 /* Initialize and activate the protocol. */
387 VNET_LIST_RLOCK();
388 VNET_FOREACH(vnet_iter) {
389 CURVNET_SET_QUIET(vnet_iter);
390 protosw_init(fpr);
391 CURVNET_RESTORE();
392 }
393 VNET_LIST_RUNLOCK();
394
395 return (0);
396}
397
398/*
399 * The caller must make sure the protocol and its functions correctly shut down
400 * all sockets and release all locks and memory references.
401 */
402int
403pf_proto_unregister(int family, int protocol, int type)
404{
405 struct domain *dp;
406 struct protosw *pr, *dpr;
407
408 /* Sanity checks. */
409 if (family == 0)
410 return (EPFNOSUPPORT);
411 if (protocol == 0)
412 return (EPROTONOSUPPORT);
413 if (type == 0)
414 return (EPROTOTYPE);
415
416 /* Try to find the specified domain based on the family type. */
417 dp = pffinddomain(family);
418 if (dp == NULL)
419 return (EPFNOSUPPORT);
420
421 dpr = NULL;
422
423 /* Lock out everyone else while we are manipulating the protosw. */
424 mtx_lock(&dom_mtx);
425
426 /* The protocol must exist and only once. */
427 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
428 if ((pr->pr_type == type) && (pr->pr_protocol == protocol)) {
429 if (dpr != NULL) {
430 mtx_unlock(&dom_mtx);
431 return (EMLINK); /* Should not happen! */
432 } else
433 dpr = pr;
434 }
435 }
436
437 /* Protocol does not exist. */
438 if (dpr == NULL) {
439 mtx_unlock(&dom_mtx);
440 return (EPROTONOSUPPORT);
441 }
442
443 /* De-orbit the protocol and make the slot available again. */
444 dpr->pr_type = 0;
445 dpr->pr_domain = dp;
446 dpr->pr_protocol = PROTO_SPACER;
447 dpr->pr_flags = 0;
448 dpr->pr_input = NULL;
449 dpr->pr_output = NULL;
450 dpr->pr_ctlinput = NULL;
451 dpr->pr_ctloutput = NULL;
452 dpr->pr_init = NULL;
453 dpr->pr_fasttimo = NULL;
454 dpr->pr_slowtimo = NULL;
455 dpr->pr_drain = NULL;
456 dpr->pr_usrreqs = &nousrreqs;
457
458 /* Job is done, not more protection required. */
459 mtx_unlock(&dom_mtx);
460
461 return (0);
462}
463
464void
465pfctlinput(int cmd, struct sockaddr *sa)
466{
467 struct domain *dp;
468 struct protosw *pr;
469
470 for (dp = domains; dp; dp = dp->dom_next)
471 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
472 if (pr->pr_ctlinput)
473 (*pr->pr_ctlinput)(cmd, sa, (void *)0);
474}
475
476void
477pfctlinput2(int cmd, struct sockaddr *sa, void *ctlparam)
478{
479 struct domain *dp;
480 struct protosw *pr;
481
482 if (!sa)
483 return;
484 for (dp = domains; dp; dp = dp->dom_next) {
485 /*
486 * the check must be made by xx_ctlinput() anyways, to
487 * make sure we use data item pointed to by ctlparam in
488 * correct way. the following check is made just for safety.
489 */
490 if (dp->dom_family != sa->sa_family)
491 continue;
492
493 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
494 if (pr->pr_ctlinput)
495 (*pr->pr_ctlinput)(cmd, sa, ctlparam);
496 }
497}
498
499static void
500pfslowtimo(void *arg)
501{
502 struct domain *dp;
503 struct protosw *pr;
504
505 for (dp = domains; dp; dp = dp->dom_next)
506 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
507 if (pr->pr_slowtimo)
508 (*pr->pr_slowtimo)();
509 callout_reset(&pfslow_callout, hz/2, pfslowtimo, NULL);
510}
511
512static void
513pffasttimo(void *arg)
514{
515 struct domain *dp;
516 struct protosw *pr;
517
518 for (dp = domains; dp; dp = dp->dom_next)
519 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
520 if (pr->pr_fasttimo)
521 (*pr->pr_fasttimo)();
522 callout_reset(&pffast_callout, hz/5, pffasttimo, NULL);
523}
156#undef DEFAULT
157 if (pr->pr_init)
158 (*pr->pr_init)();
159}
160
161/*
162 * Add a new protocol domain to the list of supported domains
163 * Note: you cant unload it again because a socket may be using it.
164 * XXX can't fail at this time.
165 */
166void
167domain_init(void *arg)
168{
169 struct domain *dp = arg;
170 struct protosw *pr;
171
172 if (dp->dom_init)
173 (*dp->dom_init)();
174 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
175 protosw_init(pr);
176 /*
177 * update global information about maximums
178 */
179 max_hdr = max_linkhdr + max_protohdr;
180 max_datalen = MHLEN - max_hdr;
181 if (max_datalen < 1)
182 panic("%s: max_datalen < 1", __func__);
183}
184
185#ifdef VIMAGE
186void
187vnet_domain_init(void *arg)
188{
189
190 /* Virtualized case is no different -- call init functions. */
191 domain_init(arg);
192}
193
194void
195vnet_domain_uninit(void *arg)
196{
197 struct domain *dp = arg;
198 struct protosw *pr;
199
200 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
201 if (pr->pr_destroy)
202 (*pr->pr_destroy)();
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 */
213void
214domain_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*/
244static void
245domaininit(void *dummy)
246{
247
248 if (max_linkhdr < 16) /* XXX */
249 max_linkhdr = 16;
250
251 callout_init(&pffast_callout, CALLOUT_MPSAFE);
252 callout_init(&pfslow_callout, CALLOUT_MPSAFE);
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*/
261static void
262domainfinalize(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
274struct domain *
275pffinddomain(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
285struct protosw *
286pffindtype(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
301struct protosw *
302pffindproto(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 */
331int
332pf_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 */
403int
404pf_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
465void
466pfctlinput(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
477void
478pfctlinput2(int cmd, struct sockaddr *sa, void *ctlparam)
479{
480 struct domain *dp;
481 struct protosw *pr;
482
483 if (!sa)
484 return;
485 for (dp = domains; dp; dp = dp->dom_next) {
486 /*
487 * the check must be made by xx_ctlinput() anyways, to
488 * make sure we use data item pointed to by ctlparam in
489 * correct way. the following check is made just for safety.
490 */
491 if (dp->dom_family != sa->sa_family)
492 continue;
493
494 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
495 if (pr->pr_ctlinput)
496 (*pr->pr_ctlinput)(cmd, sa, ctlparam);
497 }
498}
499
500static void
501pfslowtimo(void *arg)
502{
503 struct domain *dp;
504 struct protosw *pr;
505
506 for (dp = domains; dp; dp = dp->dom_next)
507 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
508 if (pr->pr_slowtimo)
509 (*pr->pr_slowtimo)();
510 callout_reset(&pfslow_callout, hz/2, pfslowtimo, NULL);
511}
512
513static void
514pffasttimo(void *arg)
515{
516 struct domain *dp;
517 struct protosw *pr;
518
519 for (dp = domains; dp; dp = dp->dom_next)
520 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
521 if (pr->pr_fasttimo)
522 (*pr->pr_fasttimo)();
523 callout_reset(&pffast_callout, hz/5, pffasttimo, NULL);
524}