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