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