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