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