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