source: rtems-libbsd/freebsd/kern/uipc_domain.c @ 1e88db13

4.1155-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since 1e88db13 was 471e6f9, checked in by Jennifer Averett <jennifer.averett@…>, on 03/22/12 at 13:44:35

Added uipc_domain.c to resolve missing symbols when linking the RealTek? Nic.

  • Property mode set to 100644
File size: 14.1 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
266        if (max_linkhdr < 16)           /* XXX */
267                max_linkhdr = 16;
268
269        callout_init(&pffast_callout, CALLOUT_MPSAFE);
270        callout_init(&pfslow_callout, CALLOUT_MPSAFE);
271
272        mtx_lock(&dom_mtx);
273        KASSERT(domain_init_status == 0, ("domaininit called too late!"));
274        domain_init_status = 1;
275        mtx_unlock(&dom_mtx);
276}
277
278/* ARGSUSED*/
279static void
280domainfinalize(void *dummy)
281{
282
283        mtx_lock(&dom_mtx);
284        KASSERT(domain_init_status == 1, ("domainfinalize called too late!"));
285        domain_init_status = 2;
286        mtx_unlock(&dom_mtx);   
287
288        callout_reset(&pffast_callout, 1, pffasttimo, NULL);
289        callout_reset(&pfslow_callout, 1, pfslowtimo, NULL);
290}
291
292struct protosw *
293pffindtype(int family, int type)
294{
295        struct domain *dp;
296        struct protosw *pr;
297
298        for (dp = domains; dp; dp = dp->dom_next)
299                if (dp->dom_family == family)
300                        goto found;
301        return (0);
302found:
303        for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
304                if (pr->pr_type && pr->pr_type == type)
305                        return (pr);
306        return (0);
307}
308
309struct protosw *
310pffindproto(int family, int protocol, int type)
311{
312        struct domain *dp;
313        struct protosw *pr;
314        struct protosw *maybe = 0;
315
316        if (family == 0)
317                return (0);
318        for (dp = domains; dp; dp = dp->dom_next)
319                if (dp->dom_family == family)
320                        goto found;
321        return (0);
322found:
323        for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
324                if ((pr->pr_protocol == protocol) && (pr->pr_type == type))
325                        return (pr);
326
327                if (type == SOCK_RAW && pr->pr_type == SOCK_RAW &&
328                    pr->pr_protocol == 0 && maybe == (struct protosw *)0)
329                        maybe = pr;
330        }
331        return (maybe);
332}
333
334/*
335 * The caller must make sure that the new protocol is fully set up and ready to
336 * accept requests before it is registered.
337 */
338int
339pf_proto_register(int family, struct protosw *npr)
340{
341        VNET_ITERATOR_DECL(vnet_iter);
342        struct domain *dp;
343        struct protosw *pr, *fpr;
344
345        /* Sanity checks. */
346        if (family == 0)
347                return (EPFNOSUPPORT);
348        if (npr->pr_type == 0)
349                return (EPROTOTYPE);
350        if (npr->pr_protocol == 0)
351                return (EPROTONOSUPPORT);
352        if (npr->pr_usrreqs == NULL)
353                return (ENXIO);
354
355        /* Try to find the specified domain based on the family. */
356        for (dp = domains; dp; dp = dp->dom_next)
357                if (dp->dom_family == family)
358                        goto found;
359        return (EPFNOSUPPORT);
360
361found:
362        /* Initialize backpointer to struct domain. */
363        npr->pr_domain = dp;
364        fpr = NULL;
365
366        /*
367         * Protect us against races when two protocol registrations for
368         * the same protocol happen at the same time.
369         */
370        mtx_lock(&dom_mtx);
371
372        /* The new protocol must not yet exist. */
373        for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
374                if ((pr->pr_type == npr->pr_type) &&
375                    (pr->pr_protocol == npr->pr_protocol)) {
376                        mtx_unlock(&dom_mtx);
377                        return (EEXIST);        /* XXX: Check only protocol? */
378                }
379                /* While here, remember the first free spacer. */
380                if ((fpr == NULL) && (pr->pr_protocol == PROTO_SPACER))
381                        fpr = pr;
382        }
383
384        /* If no free spacer is found we can't add the new protocol. */
385        if (fpr == NULL) {
386                mtx_unlock(&dom_mtx);
387                return (ENOMEM);
388        }
389
390        /* Copy the new struct protosw over the spacer. */
391        bcopy(npr, fpr, sizeof(*fpr));
392
393        /* Job is done, no more protection required. */
394        mtx_unlock(&dom_mtx);
395
396        /* Initialize and activate the protocol. */
397        VNET_LIST_RLOCK();
398        VNET_FOREACH(vnet_iter) {
399                CURVNET_SET_QUIET(vnet_iter);
400                protosw_init(fpr);
401                CURVNET_RESTORE();
402        }
403        VNET_LIST_RUNLOCK();
404
405        return (0);
406}
407
408/*
409 * The caller must make sure the protocol and its functions correctly shut down
410 * all sockets and release all locks and memory references.
411 */
412int
413pf_proto_unregister(int family, int protocol, int type)
414{
415        struct domain *dp;
416        struct protosw *pr, *dpr;
417
418        /* Sanity checks. */
419        if (family == 0)
420                return (EPFNOSUPPORT);
421        if (protocol == 0)
422                return (EPROTONOSUPPORT);
423        if (type == 0)
424                return (EPROTOTYPE);
425
426        /* Try to find the specified domain based on the family type. */
427        for (dp = domains; dp; dp = dp->dom_next)
428                if (dp->dom_family == family)
429                        goto found;
430        return (EPFNOSUPPORT);
431
432found:
433        dpr = NULL;
434
435        /* Lock out everyone else while we are manipulating the protosw. */
436        mtx_lock(&dom_mtx);
437
438        /* The protocol must exist and only once. */
439        for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
440                if ((pr->pr_type == type) && (pr->pr_protocol == protocol)) {
441                        if (dpr != NULL) {
442                                mtx_unlock(&dom_mtx);
443                                return (EMLINK);   /* Should not happen! */
444                        } else
445                                dpr = pr;
446                }
447        }
448
449        /* Protocol does not exist. */
450        if (dpr == NULL) {
451                mtx_unlock(&dom_mtx);
452                return (EPROTONOSUPPORT);
453        }
454
455        /* De-orbit the protocol and make the slot available again. */
456        dpr->pr_type = 0;
457        dpr->pr_domain = dp;
458        dpr->pr_protocol = PROTO_SPACER;
459        dpr->pr_flags = 0;
460        dpr->pr_input = NULL;
461        dpr->pr_output = NULL;
462        dpr->pr_ctlinput = NULL;
463        dpr->pr_ctloutput = NULL;
464        dpr->pr_init = NULL;
465        dpr->pr_fasttimo = NULL;
466        dpr->pr_slowtimo = NULL;
467        dpr->pr_drain = NULL;
468        dpr->pr_usrreqs = &nousrreqs;
469
470        /* Job is done, not more protection required. */
471        mtx_unlock(&dom_mtx);
472
473        return (0);
474}
475
476void
477pfctlinput(int cmd, struct sockaddr *sa)
478{
479        struct domain *dp;
480        struct protosw *pr;
481
482        for (dp = domains; dp; dp = dp->dom_next)
483                for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
484                        if (pr->pr_ctlinput)
485                                (*pr->pr_ctlinput)(cmd, sa, (void *)0);
486}
487
488void
489pfctlinput2(int cmd, struct sockaddr *sa, void *ctlparam)
490{
491        struct domain *dp;
492        struct protosw *pr;
493
494        if (!sa)
495                return;
496        for (dp = domains; dp; dp = dp->dom_next) {
497                /*
498                 * the check must be made by xx_ctlinput() anyways, to
499                 * make sure we use data item pointed to by ctlparam in
500                 * correct way.  the following check is made just for safety.
501                 */
502                if (dp->dom_family != sa->sa_family)
503                        continue;
504
505                for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
506                        if (pr->pr_ctlinput)
507                                (*pr->pr_ctlinput)(cmd, sa, ctlparam);
508        }
509}
510
511static void
512pfslowtimo(void *arg)
513{
514        struct domain *dp;
515        struct protosw *pr;
516
517        for (dp = domains; dp; dp = dp->dom_next)
518                for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
519                        if (pr->pr_slowtimo)
520                                (*pr->pr_slowtimo)();
521        callout_reset(&pfslow_callout, hz/2, pfslowtimo, NULL);
522}
523
524static void
525pffasttimo(void *arg)
526{
527        struct domain *dp;
528        struct protosw *pr;
529
530        for (dp = domains; dp; dp = dp->dom_next)
531                for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
532                        if (pr->pr_fasttimo)
533                                (*pr->pr_fasttimo)();
534        callout_reset(&pffast_callout, hz/5, pffasttimo, NULL);
535}
Note: See TracBrowser for help on using the repository browser.