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