source: rtems-libbsd/freebsd/kern/uipc_domain.c @ 7345c71

4.1155-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since 7345c71 was 7345c71, checked in by Jennifer Averett <jennifer.averett@…>, on 06/01/12 at 18:57:50

Added rtems specific size for max_linkhdr
Note: This was needed for tcp_init to pass. This modification
should be discussed to see if a cleaner resolution to the problem
exists.

  • Property mode set to 100644
File size: 14.2 KB
Line 
1#include <freebsd/machine/rtems-bsd-config.h>
2
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 * 4. 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 <freebsd/sys/cdefs.h>
35__FBSDID("$FreeBSD$");
36
37#include <freebsd/sys/param.h>
38#include <freebsd/sys/socket.h>
39#include <freebsd/sys/protosw.h>
40#include <freebsd/sys/domain.h>
41#include <freebsd/sys/eventhandler.h>
42#include <freebsd/sys/mbuf.h>
43#include <freebsd/sys/kernel.h>
44#include <freebsd/sys/lock.h>
45#include <freebsd/sys/mutex.h>
46#include <freebsd/sys/socketvar.h>
47#include <freebsd/sys/systm.h>
48
49#include <freebsd/net/vnet.h>
50
51#include <freebsd/vm/uma.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
65static void domaininit(void *);
66SYSINIT(domain, SI_SUB_PROTO_DOMAININIT, SI_ORDER_ANY, domaininit, NULL);
67
68static void domainfinalize(void *);
69SYSINIT(domainfin, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_FIRST, domainfinalize,
70    NULL);
71
72static struct callout pffast_callout;
73static struct callout pfslow_callout;
74
75static void     pffasttimo(void *);
76static void     pfslowtimo(void *);
77
78struct domain *domains;         /* registered protocol domains */
79int domain_init_status = 0;
80static struct mtx dom_mtx;              /* domain list lock */
81MTX_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 */
87struct 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
108static void
109protosw_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_bind, pru_bind_notsupp);
143        DEFAULT(pu->pru_connect, pru_connect_notsupp);
144        DEFAULT(pu->pru_connect2, pru_connect2_notsupp);
145        DEFAULT(pu->pru_control, pru_control_notsupp);
146        DEFAULT(pu->pru_disconnect, pru_disconnect_notsupp);
147        DEFAULT(pu->pru_listen, pru_listen_notsupp);
148        DEFAULT(pu->pru_peeraddr, pru_peeraddr_notsupp);
149        DEFAULT(pu->pru_rcvd, pru_rcvd_notsupp);
150        DEFAULT(pu->pru_rcvoob, pru_rcvoob_notsupp);
151        DEFAULT(pu->pru_sense, pru_sense_null);
152        DEFAULT(pu->pru_shutdown, pru_shutdown_notsupp);
153        DEFAULT(pu->pru_sockaddr, pru_sockaddr_notsupp);
154        DEFAULT(pu->pru_sosend, sosend_generic);
155        DEFAULT(pu->pru_soreceive, soreceive_generic);
156        DEFAULT(pu->pru_sopoll, sopoll_generic);
157#undef DEFAULT
158        if (pr->pr_init)
159                (*pr->pr_init)();
160}
161
162/*
163 * Add a new protocol domain to the list of supported domains
164 * Note: you cant unload it again because a socket may be using it.
165 * XXX can't fail at this time.
166 */
167void
168domain_init(void *arg)
169{
170        struct domain *dp = arg;
171        struct protosw *pr;
172
173        if (dp->dom_init)
174                (*dp->dom_init)();
175        for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
176                protosw_init(pr);
177        /*
178         * update global information about maximums
179         */
180        max_hdr = max_linkhdr + max_protohdr;
181        max_datalen = MHLEN - max_hdr;
182        if (max_datalen < 1)
183                panic("%s: max_datalen < 1", __func__);
184}
185
186#ifdef VIMAGE
187void
188vnet_domain_init(void *arg)
189{
190
191        /* Virtualized case is no different -- call init functions. */
192        domain_init(arg);
193}
194
195void
196vnet_domain_uninit(void *arg)
197{
198        struct domain *dp = arg;
199        struct protosw *pr;
200
201        for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
202                if (pr->pr_destroy)
203                        (*pr->pr_destroy)();
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 */
214void
215domain_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 net_add_domain(%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
244static void
245socket_zone_change(void *tag)
246{
247
248        uma_zone_set_max(socket_zone, maxsockets);
249}
250
251/* ARGSUSED*/
252static void
253domaininit(void *dummy)
254{
255
256        /*
257         * Before we do any setup, make sure to initialize the
258         * zone allocator we get struct sockets from.
259         */
260        socket_zone = uma_zcreate("socket", sizeof(struct socket), NULL, NULL,
261            NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
262        uma_zone_set_max(socket_zone, maxsockets);
263        EVENTHANDLER_REGISTER(maxsockets_change, socket_zone_change, NULL,
264                EVENTHANDLER_PRI_FIRST);
265#ifndef __rtems__
266        if (max_linkhdr < 16)           /* XXX */
267                max_linkhdr = 16;
268#else /*__rtems__ */
269        if (max_linkhdr < 10)           /* XXX -- Not sure this is correct*/
270                max_linkhdr = 10;
271#endif /*__rtems__ */
272
273        callout_init(&pffast_callout, CALLOUT_MPSAFE);
274        callout_init(&pfslow_callout, CALLOUT_MPSAFE);
275
276        mtx_lock(&dom_mtx);
277        KASSERT(domain_init_status == 0, ("domaininit called too late!"));
278        domain_init_status = 1;
279        mtx_unlock(&dom_mtx);
280}
281
282/* ARGSUSED*/
283static void
284domainfinalize(void *dummy)
285{
286
287        mtx_lock(&dom_mtx);
288        KASSERT(domain_init_status == 1, ("domainfinalize called too late!"));
289        domain_init_status = 2;
290        mtx_unlock(&dom_mtx);   
291
292        callout_reset(&pffast_callout, 1, pffasttimo, NULL);
293        callout_reset(&pfslow_callout, 1, pfslowtimo, NULL);
294}
295
296struct protosw *
297pffindtype(int family, int type)
298{
299        struct domain *dp;
300        struct protosw *pr;
301
302        for (dp = domains; dp; dp = dp->dom_next)
303                if (dp->dom_family == family)
304                        goto found;
305        return (0);
306found:
307        for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
308                if (pr->pr_type && pr->pr_type == type)
309                        return (pr);
310        return (0);
311}
312
313struct protosw *
314pffindproto(int family, int protocol, int type)
315{
316        struct domain *dp;
317        struct protosw *pr;
318        struct protosw *maybe = 0;
319
320        if (family == 0)
321                return (0);
322        for (dp = domains; dp; dp = dp->dom_next)
323                if (dp->dom_family == family)
324                        goto found;
325        return (0);
326found:
327        for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
328                if ((pr->pr_protocol == protocol) && (pr->pr_type == type))
329                        return (pr);
330
331                if (type == SOCK_RAW && pr->pr_type == SOCK_RAW &&
332                    pr->pr_protocol == 0 && maybe == (struct protosw *)0)
333                        maybe = pr;
334        }
335        return (maybe);
336}
337
338/*
339 * The caller must make sure that the new protocol is fully set up and ready to
340 * accept requests before it is registered.
341 */
342int
343pf_proto_register(int family, struct protosw *npr)
344{
345        VNET_ITERATOR_DECL(vnet_iter);
346        struct domain *dp;
347        struct protosw *pr, *fpr;
348
349        /* Sanity checks. */
350        if (family == 0)
351                return (EPFNOSUPPORT);
352        if (npr->pr_type == 0)
353                return (EPROTOTYPE);
354        if (npr->pr_protocol == 0)
355                return (EPROTONOSUPPORT);
356        if (npr->pr_usrreqs == NULL)
357                return (ENXIO);
358
359        /* Try to find the specified domain based on the family. */
360        for (dp = domains; dp; dp = dp->dom_next)
361                if (dp->dom_family == family)
362                        goto found;
363        return (EPFNOSUPPORT);
364
365found:
366        /* Initialize backpointer to struct domain. */
367        npr->pr_domain = dp;
368        fpr = NULL;
369
370        /*
371         * Protect us against races when two protocol registrations for
372         * the same protocol happen at the same time.
373         */
374        mtx_lock(&dom_mtx);
375
376        /* The new protocol must not yet exist. */
377        for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
378                if ((pr->pr_type == npr->pr_type) &&
379                    (pr->pr_protocol == npr->pr_protocol)) {
380                        mtx_unlock(&dom_mtx);
381                        return (EEXIST);        /* XXX: Check only protocol? */
382                }
383                /* While here, remember the first free spacer. */
384                if ((fpr == NULL) && (pr->pr_protocol == PROTO_SPACER))
385                        fpr = pr;
386        }
387
388        /* If no free spacer is found we can't add the new protocol. */
389        if (fpr == NULL) {
390                mtx_unlock(&dom_mtx);
391                return (ENOMEM);
392        }
393
394        /* Copy the new struct protosw over the spacer. */
395        bcopy(npr, fpr, sizeof(*fpr));
396
397        /* Job is done, no more protection required. */
398        mtx_unlock(&dom_mtx);
399
400        /* Initialize and activate the protocol. */
401        VNET_LIST_RLOCK();
402        VNET_FOREACH(vnet_iter) {
403                CURVNET_SET_QUIET(vnet_iter);
404                protosw_init(fpr);
405                CURVNET_RESTORE();
406        }
407        VNET_LIST_RUNLOCK();
408
409        return (0);
410}
411
412/*
413 * The caller must make sure the protocol and its functions correctly shut down
414 * all sockets and release all locks and memory references.
415 */
416int
417pf_proto_unregister(int family, int protocol, int type)
418{
419        struct domain *dp;
420        struct protosw *pr, *dpr;
421
422        /* Sanity checks. */
423        if (family == 0)
424                return (EPFNOSUPPORT);
425        if (protocol == 0)
426                return (EPROTONOSUPPORT);
427        if (type == 0)
428                return (EPROTOTYPE);
429
430        /* Try to find the specified domain based on the family type. */
431        for (dp = domains; dp; dp = dp->dom_next)
432                if (dp->dom_family == family)
433                        goto found;
434        return (EPFNOSUPPORT);
435
436found:
437        dpr = NULL;
438
439        /* Lock out everyone else while we are manipulating the protosw. */
440        mtx_lock(&dom_mtx);
441
442        /* The protocol must exist and only once. */
443        for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
444                if ((pr->pr_type == type) && (pr->pr_protocol == protocol)) {
445                        if (dpr != NULL) {
446                                mtx_unlock(&dom_mtx);
447                                return (EMLINK);   /* Should not happen! */
448                        } else
449                                dpr = pr;
450                }
451        }
452
453        /* Protocol does not exist. */
454        if (dpr == NULL) {
455                mtx_unlock(&dom_mtx);
456                return (EPROTONOSUPPORT);
457        }
458
459        /* De-orbit the protocol and make the slot available again. */
460        dpr->pr_type = 0;
461        dpr->pr_domain = dp;
462        dpr->pr_protocol = PROTO_SPACER;
463        dpr->pr_flags = 0;
464        dpr->pr_input = NULL;
465        dpr->pr_output = NULL;
466        dpr->pr_ctlinput = NULL;
467        dpr->pr_ctloutput = NULL;
468        dpr->pr_init = NULL;
469        dpr->pr_fasttimo = NULL;
470        dpr->pr_slowtimo = NULL;
471        dpr->pr_drain = NULL;
472        dpr->pr_usrreqs = &nousrreqs;
473
474        /* Job is done, not more protection required. */
475        mtx_unlock(&dom_mtx);
476
477        return (0);
478}
479
480void
481pfctlinput(int cmd, struct sockaddr *sa)
482{
483        struct domain *dp;
484        struct protosw *pr;
485
486        for (dp = domains; dp; dp = dp->dom_next)
487                for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
488                        if (pr->pr_ctlinput)
489                                (*pr->pr_ctlinput)(cmd, sa, (void *)0);
490}
491
492void
493pfctlinput2(int cmd, struct sockaddr *sa, void *ctlparam)
494{
495        struct domain *dp;
496        struct protosw *pr;
497
498        if (!sa)
499                return;
500        for (dp = domains; dp; dp = dp->dom_next) {
501                /*
502                 * the check must be made by xx_ctlinput() anyways, to
503                 * make sure we use data item pointed to by ctlparam in
504                 * correct way.  the following check is made just for safety.
505                 */
506                if (dp->dom_family != sa->sa_family)
507                        continue;
508
509                for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
510                        if (pr->pr_ctlinput)
511                                (*pr->pr_ctlinput)(cmd, sa, ctlparam);
512        }
513}
514
515static void
516pfslowtimo(void *arg)
517{
518        struct domain *dp;
519        struct protosw *pr;
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_slowtimo)
524                                (*pr->pr_slowtimo)();
525        callout_reset(&pfslow_callout, hz/2, pfslowtimo, NULL);
526}
527
528static void
529pffasttimo(void *arg)
530{
531        struct domain *dp;
532        struct protosw *pr;
533
534        for (dp = domains; dp; dp = dp->dom_next)
535                for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
536                        if (pr->pr_fasttimo)
537                                (*pr->pr_fasttimo)();
538        callout_reset(&pffast_callout, hz/5, pffasttimo, NULL);
539}
Note: See TracBrowser for help on using the repository browser.