FreeBSD/Linux Kernel Cross Reference
sys/kern/uipc_accf.c
1 /*-
2 * Copyright (c) 2000 Paycounter, Inc.
3 * Author: Alfred Perlstein <alfred@paycounter.com>, <alfred@FreeBSD.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD: releng/5.4/sys/kern/uipc_accf.c 145335 2005-04-20 19:11:07Z cvs2svn $");
30
31 #define ACCEPT_FILTER_MOD
32
33 #include "opt_param.h"
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/domain.h>
37 #include <sys/kernel.h>
38 #include <sys/lock.h>
39 #include <sys/malloc.h>
40 #include <sys/mbuf.h>
41 #include <sys/module.h>
42 #include <sys/mutex.h>
43 #include <sys/protosw.h>
44 #include <sys/sysctl.h>
45 #include <sys/socket.h>
46 #include <sys/socketvar.h>
47 #include <sys/queue.h>
48
49 static struct mtx accept_filter_mtx;
50 MTX_SYSINIT(accept_filter, &accept_filter_mtx, "accept_filter_mtx",
51 MTX_DEF);
52 #define ACCEPT_FILTER_LOCK() mtx_lock(&accept_filter_mtx)
53 #define ACCEPT_FILTER_UNLOCK() mtx_unlock(&accept_filter_mtx)
54
55 static SLIST_HEAD(, accept_filter) accept_filtlsthd =
56 SLIST_HEAD_INITIALIZER(&accept_filtlsthd);
57
58 MALLOC_DEFINE(M_ACCF, "accf", "accept filter data");
59
60 static int unloadable = 0;
61
62 SYSCTL_DECL(_net_inet); /* XXX: some header should do this for me */
63 SYSCTL_NODE(_net_inet, OID_AUTO, accf, CTLFLAG_RW, 0, "Accept filters");
64 SYSCTL_INT(_net_inet_accf, OID_AUTO, unloadable, CTLFLAG_RW, &unloadable, 0,
65 "Allow unload of accept filters (not recommended)");
66
67 /*
68 * Must be passed a malloc'd structure so we don't explode if the kld is
69 * unloaded, we leak the struct on deallocation to deal with this, but if a
70 * filter is loaded with the same name as a leaked one we re-use the entry.
71 */
72 int
73 accept_filt_add(struct accept_filter *filt)
74 {
75 struct accept_filter *p;
76
77 ACCEPT_FILTER_LOCK();
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 ACCEPT_FILTER_UNLOCK();
82 return (EEXIST);
83 } else {
84 p->accf_callback = filt->accf_callback;
85 ACCEPT_FILTER_UNLOCK();
86 FREE(filt, M_ACCF);
87 return (0);
88 }
89 }
90
91 if (p == NULL)
92 SLIST_INSERT_HEAD(&accept_filtlsthd, filt, accf_next);
93 ACCEPT_FILTER_UNLOCK();
94 return (0);
95 }
96
97 int
98 accept_filt_del(char *name)
99 {
100 struct accept_filter *p;
101
102 p = accept_filt_get(name);
103 if (p == NULL)
104 return (ENOENT);
105
106 p->accf_callback = NULL;
107 return (0);
108 }
109
110 struct accept_filter *
111 accept_filt_get(char *name)
112 {
113 struct accept_filter *p;
114
115 ACCEPT_FILTER_LOCK();
116 SLIST_FOREACH(p, &accept_filtlsthd, accf_next)
117 if (strcmp(p->accf_name, name) == 0)
118 break;
119 ACCEPT_FILTER_UNLOCK();
120
121 return (p);
122 }
123
124 int
125 accept_filt_generic_mod_event(module_t mod, int event, void *data)
126 {
127 struct accept_filter *p;
128 struct accept_filter *accfp = (struct accept_filter *) data;
129 int error;
130
131 switch (event) {
132 case MOD_LOAD:
133 MALLOC(p, struct accept_filter *, sizeof(*p), M_ACCF,
134 M_WAITOK);
135 bcopy(accfp, p, sizeof(*p));
136 error = accept_filt_add(p);
137 break;
138
139 case MOD_UNLOAD:
140 /*
141 * Do not support unloading yet. we don't keep track of
142 * refcounts and unloading an accept filter callback and then
143 * having it called is a bad thing. A simple fix would be to
144 * track the refcount in the struct accept_filter.
145 */
146 if (unloadable != 0) {
147 error = accept_filt_del(accfp->accf_name);
148 } else
149 error = EOPNOTSUPP;
150 break;
151
152 case MOD_SHUTDOWN:
153 error = 0;
154 break;
155
156 default:
157 error = EOPNOTSUPP;
158 break;
159 }
160
161 return (error);
162 }
163
164 int
165 do_setopt_accept_filter(struct socket *so, struct sockopt *sopt)
166 {
167 struct accept_filter_arg *afap;
168 struct accept_filter *afp;
169 struct so_accf *newaf;
170 int error = 0;
171
172 newaf = NULL;
173 afap = NULL;
174
175 /*
176 * XXXRW: Configuring accept filters should be an atomic test-and-set
177 * operation to prevent races during setup and attach. There may be
178 * more general issues of racing and ordering here that are not yet
179 * addressed by locking.
180 */
181 /* do not set/remove accept filters on non listen sockets */
182 SOCK_LOCK(so);
183 if ((so->so_options & SO_ACCEPTCONN) == 0) {
184 SOCK_UNLOCK(so);
185 return (EINVAL);
186 }
187
188 /* removing the filter */
189 if (sopt == NULL) {
190 if (so->so_accf != NULL) {
191 struct so_accf *af = so->so_accf;
192 if (af->so_accept_filter != NULL &&
193 af->so_accept_filter->accf_destroy != NULL) {
194 af->so_accept_filter->accf_destroy(so);
195 }
196 if (af->so_accept_filter_str != NULL) {
197 FREE(af->so_accept_filter_str, M_ACCF);
198 }
199 FREE(af, M_ACCF);
200 so->so_accf = NULL;
201 }
202 so->so_options &= ~SO_ACCEPTFILTER;
203 SOCK_UNLOCK(so);
204 return (0);
205 }
206 SOCK_UNLOCK(so);
207
208 /*-
209 * Adding a filter.
210 *
211 * Do memory allocation, copyin, and filter lookup now while we're
212 * not holding any locks. Avoids sleeping with a mutex, as well as
213 * introducing a lock order between accept filter locks and socket
214 * locks here.
215 */
216 MALLOC(afap, struct accept_filter_arg *, sizeof(*afap), M_TEMP,
217 M_WAITOK);
218 /* don't put large objects on the kernel stack */
219 error = sooptcopyin(sopt, afap, sizeof *afap, sizeof *afap);
220 afap->af_name[sizeof(afap->af_name)-1] = '\0';
221 afap->af_arg[sizeof(afap->af_arg)-1] = '\0';
222 if (error) {
223 FREE(afap, M_TEMP);
224 return (error);
225 }
226 afp = accept_filt_get(afap->af_name);
227 if (afp == NULL) {
228 FREE(afap, M_TEMP);
229 return (ENOENT);
230 }
231
232 /*
233 * Allocate the new accept filter instance storage. We may have to
234 * free it again later if we fail to attach it. If attached
235 * properly, 'newaf' is NULLed to avoid a free() while in use.
236 */
237 MALLOC(newaf, struct so_accf *, sizeof(*newaf), M_ACCF, M_WAITOK |
238 M_ZERO);
239 if (afp->accf_create != NULL && afap->af_name[0] != '\0') {
240 int len = strlen(afap->af_name) + 1;
241 MALLOC(newaf->so_accept_filter_str, char *, len, M_ACCF,
242 M_WAITOK);
243 strcpy(newaf->so_accept_filter_str, afap->af_name);
244 }
245
246 SOCK_LOCK(so);
247 /* must remove previous filter first */
248 if (so->so_accf != NULL) {
249 error = EINVAL;
250 goto out;
251 }
252 /*
253 * Invoke the accf_create() method of the filter if required.
254 * XXXRW: the socket mutex is held over this call, so the create
255 * method cannot block. This may be something we have to change, but
256 * it would require addressing possible races.
257 */
258 if (afp->accf_create != NULL) {
259 newaf->so_accept_filter_arg =
260 afp->accf_create(so, afap->af_arg);
261 if (newaf->so_accept_filter_arg == NULL) {
262 error = EINVAL;
263 goto out;
264 }
265 }
266 newaf->so_accept_filter = afp;
267 so->so_accf = newaf;
268 so->so_options |= SO_ACCEPTFILTER;
269 newaf = NULL;
270 out:
271 SOCK_UNLOCK(so);
272 if (newaf != NULL) {
273 if (newaf->so_accept_filter_str != NULL)
274 FREE(newaf->so_accept_filter_str, M_ACCF);
275 FREE(newaf, M_ACCF);
276 }
277 if (afap != NULL)
278 FREE(afap, M_TEMP);
279 return (error);
280 }
Cache object: d146167a0f936df5f572bf8a9344ca54
|