]> git.karo-electronics.de Git - karo-tx-redboot.git/blob - packages/net/bsd_tcpip/v2_0/src/sys/kern/uipc_accf.c
Initial revision
[karo-tx-redboot.git] / packages / net / bsd_tcpip / v2_0 / src / sys / kern / uipc_accf.c
1 //==========================================================================
2 //
3 //      src/sys/kern/uipc_accf.c
4 //
5 //==========================================================================
6 //####BSDCOPYRIGHTBEGIN####
7 //
8 // -------------------------------------------
9 //
10 // Portions of this software may have been derived from OpenBSD, 
11 // FreeBSD or other sources, and are covered by the appropriate
12 // copyright disclaimers included herein.
13 //
14 // Portions created by Red Hat are
15 // Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
16 //
17 // -------------------------------------------
18 //
19 //####BSDCOPYRIGHTEND####
20 //==========================================================================
21
22 /*
23  * Copyright (c) 2000 Paycounter, Inc.
24  * Author: Alfred Perlstein <alfred@paycounter.com>, <alfred@FreeBSD.org>
25  * All rights reserved.
26  *
27  * Redistribution and use in source and binary forms, with or without
28  * modification, are permitted provided that the following conditions
29  * are met:
30  * 1. Redistributions of source code must retain the above copyright
31  *    notice, this list of conditions and the following disclaimer.
32  * 2. Redistributions in binary form must reproduce the above copyright
33  *    notice, this list of conditions and the following disclaimer in the
34  *    documentation and/or other materials provided with the distribution.
35  *
36  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
37  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
39  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
40  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
41  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
42  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
44  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
45  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  *
48  *      $FreeBSD: src/sys/kern/uipc_accf.c,v 1.2.2.2 2000/09/20 21:19:21 ps Exp $
49  */
50
51 #include <sys/param.h>
52 #include <sys/domain.h>
53 #include <sys/malloc.h>
54 #include <sys/mbuf.h>
55 #include <sys/protosw.h>
56 #include <sys/socket.h>
57 #include <sys/socketvar.h>
58 #include <sys/queue.h>
59
60 static SLIST_HEAD(, accept_filter) accept_filtlsthd =
61         SLIST_HEAD_INITIALIZER(&accept_filtlsthd);
62
63 #ifdef ACCEPT_FILTER_MOD
64 static int unloadable = 0;
65 #endif
66
67 /*
68  * must be passed a malloc'd structure so we don't explode if the kld
69  * is unloaded, we leak the struct on deallocation to deal with this,
70  * but if a filter is loaded with the same name as a leaked one we re-use
71  * the entry.
72  */
73 int
74 accept_filt_add(struct accept_filter *filt)
75 {
76         struct accept_filter *p;
77
78         SLIST_FOREACH(p, &accept_filtlsthd, accf_next)
79                 if (strcmp(p->accf_name, filt->accf_name) == 0)  {
80                         if (p->accf_callback != NULL) {
81                                 return (EEXIST);
82                         } else {
83                                 p->accf_callback = filt->accf_callback;
84                                 FREE(filt, M_ACCF);
85                                 return (0);
86                         }
87                 }
88                                 
89         if (p == NULL)
90                 SLIST_INSERT_HEAD(&accept_filtlsthd, filt, accf_next);
91         return (0);
92 }
93
94 int
95 accept_filt_del(char *name)
96 {
97         struct accept_filter *p;
98
99         p = accept_filt_get(name);
100         if (p == NULL)
101                 return (ENOENT);
102
103         p->accf_callback = NULL;
104         return (0);
105 }
106
107 struct accept_filter *
108 accept_filt_get(char *name)
109 {
110         struct accept_filter *p;
111
112         SLIST_FOREACH(p, &accept_filtlsthd, accf_next)
113                 if (strcmp(p->accf_name, name) == 0)
114                         return (p);
115
116         return (NULL);
117 }
118
119 #ifdef ACCEPT_FILTER_MOD
120 int
121 accept_filt_generic_mod_event(module_t mod, int event, void *data)
122 {
123         struct accept_filter *p;
124         struct accept_filter *accfp = (struct accept_filter *) data;
125         int     s, error;
126
127         switch (event) {
128         case MOD_LOAD:
129                 MALLOC(p, struct accept_filter *, sizeof(*p), M_ACCF, M_WAITOK);
130                 bcopy(accfp, p, sizeof(*p));
131                 s = splnet();
132                 error = accept_filt_add(p);
133                 splx(s);
134                 break;
135
136         case MOD_UNLOAD:
137                 /*
138                  * Do not support unloading yet. we don't keep track of refcounts
139                  * and unloading an accept filter callback and then having it called
140                  * is a bad thing.  A simple fix would be to track the refcount
141                  * in the struct accept_filter.
142                  */
143                 if (unloadable != 0) {
144                         s = splnet();
145                         error = accept_filt_del(accfp->accf_name);
146                         splx(s);
147                 } else
148                         error = EOPNOTSUPP;
149                 break;
150
151         case MOD_SHUTDOWN:
152                 error = 0;
153                 break;
154
155         default:
156                 error = EOPNOTSUPP;
157                 break;
158         }
159
160         return (error);
161 }
162 #endif