uipc_accf.c revision 1.2.2.3 1 /* $NetBSD: uipc_accf.c,v 1.2.2.3 2008/10/10 22:34:14 skrll Exp $ */
2
3 /*-
4 * Copyright (c) 2000 Paycounter, Inc.
5 * Copyright (c) 2005 Robert N. M. Watson
6 * Author: Alfred Perlstein <alfred (at) paycounter.com>, <alfred (at) FreeBSD.org>
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: uipc_accf.c,v 1.2.2.3 2008/10/10 22:34:14 skrll Exp $");
33
34 #define ACCEPT_FILTER_MOD
35
36 #include "opt_inet.h"
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/domain.h>
40 #include <sys/kernel.h>
41 #include <sys/lock.h>
42 #include <sys/malloc.h>
43 #include <sys/mbuf.h>
44 #include <sys/lkm.h>
45 #include <sys/mutex.h>
46 #include <sys/protosw.h>
47 #include <sys/sysctl.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
50 #include <sys/queue.h>
51 #include <sys/once.h>
52
53 static kmutex_t accept_filter_mtx;
54 #define ACCEPT_FILTER_LOCK() mutex_spin_enter(&accept_filter_mtx)
55 #define ACCEPT_FILTER_UNLOCK() mutex_spin_exit(&accept_filter_mtx);
56 #define SOCK_LOCK(so)
57 #define SOCK_UNLOCK(so)
58
59 static SLIST_HEAD(, accept_filter) accept_filtlsthd =
60 SLIST_HEAD_INITIALIZER(&accept_filtlsthd);
61
62 MALLOC_DEFINE(M_ACCF, "accf", "accept filter data");
63
64 static int unloadable = 0;
65
66 /*
67 * Names of Accept filter sysctl objects
68 */
69
70 #define ACCFCTL_UNLOADABLE 1 /* Allow module to be unloaded */
71
72
73 SYSCTL_SETUP(sysctl_net_inet_accf_setup, "sysctl net.inet.accf subtree setup")
74 {
75 sysctl_createv(clog, 0, NULL, NULL,
76 CTLFLAG_PERMANENT,
77 CTLTYPE_NODE, "net", NULL,
78 NULL, 0, NULL, 0,
79 CTL_NET, CTL_EOL);
80 sysctl_createv(clog, 0, NULL, NULL,
81 CTLFLAG_PERMANENT,
82 CTLTYPE_NODE, "inet", NULL,
83 NULL, 0, NULL, 0,
84 CTL_NET, PF_INET, CTL_EOL);
85 sysctl_createv(clog, 0, NULL, NULL,
86 CTLFLAG_PERMANENT,
87 CTLTYPE_NODE, "accf",
88 SYSCTL_DESCR("Accept filters"),
89 NULL, 0, NULL, 0,
90 CTL_NET, PF_INET, SO_ACCEPTFILTER, CTL_EOL);
91 sysctl_createv(clog, 0, NULL, NULL,
92 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
93 CTLTYPE_INT, "unloadable",
94 SYSCTL_DESCR("Allow unload of accept filters "
95 "(not recommended)"),
96 NULL, 0, &unloadable, 0,
97 CTL_NET, PF_INET, SO_ACCEPTFILTER,
98 ACCFCTL_UNLOADABLE, CTL_EOL);
99 }
100
101 /*
102 * Must be passed a malloc'd structure so we don't explode if the kld is
103 * unloaded, we leak the struct on deallocation to deal with this, but if a
104 * filter is loaded with the same name as a leaked one we re-use the entry.
105 */
106 int
107 accept_filt_add(struct accept_filter *filt)
108 {
109 struct accept_filter *p;
110
111 ACCEPT_FILTER_LOCK();
112 SLIST_FOREACH(p, &accept_filtlsthd, accf_next)
113 if (strcmp(p->accf_name, filt->accf_name) == 0) {
114 if (p->accf_callback != NULL) {
115 ACCEPT_FILTER_UNLOCK();
116 return (EEXIST);
117 } else {
118 p->accf_callback = filt->accf_callback;
119 ACCEPT_FILTER_UNLOCK();
120 FREE(filt, M_ACCF);
121 return (0);
122 }
123 }
124
125 if (p == NULL)
126 SLIST_INSERT_HEAD(&accept_filtlsthd, filt, accf_next);
127 ACCEPT_FILTER_UNLOCK();
128 return (0);
129 }
130
131 int
132 accept_filt_del(char *name)
133 {
134 struct accept_filter *p;
135
136 p = accept_filt_get(name);
137 if (p == NULL)
138 return (ENOENT);
139
140 p->accf_callback = NULL;
141 return (0);
142 }
143
144 struct accept_filter *
145 accept_filt_get(char *name)
146 {
147 struct accept_filter *p;
148
149 ACCEPT_FILTER_LOCK();
150 SLIST_FOREACH(p, &accept_filtlsthd, accf_next)
151 if (strcmp(p->accf_name, name) == 0)
152 break;
153 ACCEPT_FILTER_UNLOCK();
154
155 return (p);
156 }
157
158 /*
159 * Accept filter initialization routine.
160 * This should be called only once.
161 */
162
163 static int
164 accept_filter_init0(void)
165 {
166 mutex_init(&accept_filter_mtx, MUTEX_DEFAULT, IPL_NET);
167
168 return 0;
169 }
170
171 /*
172 * Initialization routine: This can also be replaced with
173 * accept_filt_generic_mod_event for attaching new accept filter.
174 */
175
176 void
177 accept_filter_init(void)
178 {
179 static ONCE_DECL(accept_filter_init_once);
180
181 RUN_ONCE(&accept_filter_init_once, accept_filter_init0);
182 }
183
184 int
185 accept_filt_generic_mod_event(struct lkm_table *lkmtp, int event, void *data)
186 {
187 struct accept_filter *p;
188 struct accept_filter *accfp = (struct accept_filter *) data;
189 int error;
190
191 switch (event) {
192 case LKM_E_LOAD:
193 accept_filter_init();
194 MALLOC(p, struct accept_filter *, sizeof(*p), M_ACCF,
195 M_WAITOK);
196 bcopy(accfp, p, sizeof(*p));
197 error = accept_filt_add(p);
198 break;
199
200 case LKM_E_UNLOAD:
201 /*
202 * Do not support unloading yet. we don't keep track of
203 * refcounts and unloading an accept filter callback and then
204 * having it called is a bad thing. A simple fix would be to
205 * track the refcount in the struct accept_filter.
206 */
207 if (unloadable != 0) {
208 error = accept_filt_del(accfp->accf_name);
209 } else
210 error = EOPNOTSUPP;
211 break;
212
213 case LKM_E_STAT:
214 error = 0;
215 break;
216
217 default:
218 error = EOPNOTSUPP;
219 break;
220 }
221
222 return (error);
223 }
224
225 int
226 do_getopt_accept_filter(struct socket *so, struct sockopt *sopt)
227 {
228 struct accept_filter_arg afa;
229 int error;
230
231 SOCK_LOCK(so);
232 if ((so->so_options & SO_ACCEPTCONN) == 0) {
233 error = EINVAL;
234 goto out;
235 }
236 if ((so->so_options & SO_ACCEPTFILTER) == 0) {
237 error = EINVAL;
238 goto out;
239 }
240
241 memset(&afa, 0, sizeof(afa));
242 strcpy(afa.af_name, so->so_accf->so_accept_filter->accf_name);
243 if (so->so_accf->so_accept_filter_str != NULL)
244 strcpy(afa.af_arg, so->so_accf->so_accept_filter_str);
245 error = sockopt_set(sopt, &afa, sizeof(afa));
246 out:
247 SOCK_UNLOCK(so);
248 return (error);
249 }
250
251 int
252 do_setopt_accept_filter(struct socket *so, const struct sockopt *sopt)
253 {
254 struct accept_filter_arg afa;
255 struct accept_filter *afp;
256 struct so_accf *newaf;
257 int error;
258
259 /*
260 * Handle the simple delete case first.
261 */
262 if (sopt == NULL || sopt->sopt_size == 0) {
263 SOCK_LOCK(so);
264 if ((so->so_options & SO_ACCEPTCONN) == 0) {
265 SOCK_UNLOCK(so);
266 return (EINVAL);
267 }
268 if (so->so_accf != NULL) {
269 struct so_accf *af = so->so_accf;
270 if (af->so_accept_filter != NULL &&
271 af->so_accept_filter->accf_destroy != NULL) {
272 af->so_accept_filter->accf_destroy(so);
273 }
274 if (af->so_accept_filter_str != NULL)
275 FREE(af->so_accept_filter_str, M_ACCF);
276 FREE(af, M_ACCF);
277 so->so_accf = NULL;
278 }
279 so->so_options &= ~SO_ACCEPTFILTER;
280 SOCK_UNLOCK(so);
281 return (0);
282 }
283
284 /*
285 * Pre-allocate any memory we may need later to avoid blocking at
286 * untimely moments. This does not optimize for invalid arguments.
287 */
288 error = sockopt_get(sopt, &afa, sizeof(afa));
289 if (error) {
290 return (error);
291 }
292 afa.af_name[sizeof(afa.af_name)-1] = '\0';
293 afa.af_arg[sizeof(afa.af_arg)-1] = '\0';
294 afp = accept_filt_get(afa.af_name);
295 if (afp == NULL) {
296 return (ENOENT);
297 }
298 /*
299 * Allocate the new accept filter instance storage. We may
300 * have to free it again later if we fail to attach it. If
301 * attached properly, 'newaf' is NULLed to avoid a free()
302 * while in use.
303 */
304 MALLOC(newaf, struct so_accf *, sizeof(*newaf), M_ACCF, M_WAITOK |
305 M_ZERO);
306 if (afp->accf_create != NULL && afa.af_name[0] != '\0') {
307 int len = strlen(afa.af_name) + 1;
308 MALLOC(newaf->so_accept_filter_str, char *, len, M_ACCF,
309 M_WAITOK);
310 strcpy(newaf->so_accept_filter_str, afa.af_name);
311 }
312
313 /*
314 * Require a listen socket; don't try to replace an existing filter
315 * without first removing it.
316 */
317 SOCK_LOCK(so);
318 if (((so->so_options & SO_ACCEPTCONN) == 0) ||
319 (so->so_accf != NULL)) {
320 error = EINVAL;
321 goto out;
322 }
323
324 /*
325 * Invoke the accf_create() method of the filter if required. The
326 * socket mutex is held over this call, so create methods for filters
327 * can't block.
328 */
329 if (afp->accf_create != NULL) {
330 newaf->so_accept_filter_arg =
331 afp->accf_create(so, afa.af_arg);
332 if (newaf->so_accept_filter_arg == NULL) {
333 error = EINVAL;
334 goto out;
335 }
336 }
337 newaf->so_accept_filter = afp;
338 so->so_accf = newaf;
339 so->so_options |= SO_ACCEPTFILTER;
340 newaf = NULL;
341 out:
342 SOCK_UNLOCK(so);
343 if (newaf != NULL) {
344 if (newaf->so_accept_filter_str != NULL)
345 FREE(newaf->so_accept_filter_str, M_ACCF);
346 FREE(newaf, M_ACCF);
347 }
348 return (error);
349 }
350