uipc_accf.c revision 1.4 1 1.4 tls /* $NetBSD: uipc_accf.c,v 1.4 2008/10/11 16:39:07 tls 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.4 tls __KERNEL_RCSID(0, "$NetBSD: uipc_accf.c,v 1.4 2008/10/11 16:39:07 tls 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.4 tls #include <sys/kmem.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.4 tls #define SOCK_LOCK(so) KASSERT(solocked(so));
57 1.4 tls #define SOCK_UNLOCK(so) KASSERT(solocked(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 static int unloadable = 0;
63 1.1 tls
64 1.1 tls /*
65 1.1 tls * Names of Accept filter sysctl objects
66 1.1 tls */
67 1.1 tls
68 1.1 tls #define ACCFCTL_UNLOADABLE 1 /* Allow module to be unloaded */
69 1.1 tls
70 1.1 tls
71 1.1 tls SYSCTL_SETUP(sysctl_net_inet_accf_setup, "sysctl net.inet.accf subtree setup")
72 1.1 tls {
73 1.1 tls sysctl_createv(clog, 0, NULL, NULL,
74 1.1 tls CTLFLAG_PERMANENT,
75 1.1 tls CTLTYPE_NODE, "net", NULL,
76 1.1 tls NULL, 0, NULL, 0,
77 1.1 tls CTL_NET, CTL_EOL);
78 1.1 tls sysctl_createv(clog, 0, NULL, NULL,
79 1.1 tls CTLFLAG_PERMANENT,
80 1.1 tls CTLTYPE_NODE, "inet", NULL,
81 1.1 tls NULL, 0, NULL, 0,
82 1.1 tls CTL_NET, PF_INET, CTL_EOL);
83 1.1 tls sysctl_createv(clog, 0, NULL, NULL,
84 1.1 tls CTLFLAG_PERMANENT,
85 1.1 tls CTLTYPE_NODE, "accf",
86 1.1 tls SYSCTL_DESCR("Accept filters"),
87 1.1 tls NULL, 0, NULL, 0,
88 1.1 tls CTL_NET, PF_INET, SO_ACCEPTFILTER, CTL_EOL);
89 1.1 tls sysctl_createv(clog, 0, NULL, NULL,
90 1.1 tls CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
91 1.1 tls CTLTYPE_INT, "unloadable",
92 1.1 tls SYSCTL_DESCR("Allow unload of accept filters "
93 1.1 tls "(not recommended)"),
94 1.1 tls NULL, 0, &unloadable, 0,
95 1.1 tls CTL_NET, PF_INET, SO_ACCEPTFILTER,
96 1.1 tls ACCFCTL_UNLOADABLE, CTL_EOL);
97 1.1 tls }
98 1.1 tls
99 1.1 tls /*
100 1.4 tls * Must be passed a kmem_malloc'd structure so we don't explode if the kld is
101 1.1 tls * unloaded, we leak the struct on deallocation to deal with this, but if a
102 1.1 tls * filter is loaded with the same name as a leaked one we re-use the entry.
103 1.1 tls */
104 1.1 tls int
105 1.1 tls accept_filt_add(struct accept_filter *filt)
106 1.1 tls {
107 1.1 tls struct accept_filter *p;
108 1.1 tls
109 1.1 tls ACCEPT_FILTER_LOCK();
110 1.1 tls SLIST_FOREACH(p, &accept_filtlsthd, accf_next)
111 1.1 tls if (strcmp(p->accf_name, filt->accf_name) == 0) {
112 1.1 tls if (p->accf_callback != NULL) {
113 1.1 tls ACCEPT_FILTER_UNLOCK();
114 1.1 tls return (EEXIST);
115 1.1 tls } else {
116 1.1 tls p->accf_callback = filt->accf_callback;
117 1.1 tls ACCEPT_FILTER_UNLOCK();
118 1.4 tls kmem_free(filt, sizeof(struct accept_filter));
119 1.1 tls return (0);
120 1.1 tls }
121 1.1 tls }
122 1.1 tls
123 1.1 tls if (p == NULL)
124 1.1 tls SLIST_INSERT_HEAD(&accept_filtlsthd, filt, accf_next);
125 1.1 tls ACCEPT_FILTER_UNLOCK();
126 1.1 tls return (0);
127 1.1 tls }
128 1.1 tls
129 1.1 tls int
130 1.1 tls accept_filt_del(char *name)
131 1.1 tls {
132 1.1 tls struct accept_filter *p;
133 1.1 tls
134 1.1 tls p = accept_filt_get(name);
135 1.1 tls if (p == NULL)
136 1.1 tls return (ENOENT);
137 1.1 tls
138 1.1 tls p->accf_callback = NULL;
139 1.1 tls return (0);
140 1.1 tls }
141 1.1 tls
142 1.1 tls struct accept_filter *
143 1.1 tls accept_filt_get(char *name)
144 1.1 tls {
145 1.1 tls struct accept_filter *p;
146 1.1 tls
147 1.1 tls ACCEPT_FILTER_LOCK();
148 1.1 tls SLIST_FOREACH(p, &accept_filtlsthd, accf_next)
149 1.1 tls if (strcmp(p->accf_name, name) == 0)
150 1.1 tls break;
151 1.1 tls ACCEPT_FILTER_UNLOCK();
152 1.1 tls
153 1.1 tls return (p);
154 1.1 tls }
155 1.1 tls
156 1.1 tls /*
157 1.1 tls * Accept filter initialization routine.
158 1.1 tls * This should be called only once.
159 1.1 tls */
160 1.1 tls
161 1.1 tls static int
162 1.1 tls accept_filter_init0(void)
163 1.1 tls {
164 1.1 tls mutex_init(&accept_filter_mtx, MUTEX_DEFAULT, IPL_NET);
165 1.1 tls
166 1.1 tls return 0;
167 1.1 tls }
168 1.1 tls
169 1.1 tls /*
170 1.1 tls * Initialization routine: This can also be replaced with
171 1.1 tls * accept_filt_generic_mod_event for attaching new accept filter.
172 1.1 tls */
173 1.1 tls
174 1.1 tls void
175 1.1 tls accept_filter_init(void)
176 1.1 tls {
177 1.1 tls static ONCE_DECL(accept_filter_init_once);
178 1.1 tls
179 1.1 tls RUN_ONCE(&accept_filter_init_once, accept_filter_init0);
180 1.1 tls }
181 1.1 tls
182 1.1 tls int
183 1.1 tls accept_filt_generic_mod_event(struct lkm_table *lkmtp, int event, void *data)
184 1.1 tls {
185 1.1 tls struct accept_filter *p;
186 1.1 tls struct accept_filter *accfp = (struct accept_filter *) data;
187 1.1 tls int error;
188 1.1 tls
189 1.1 tls switch (event) {
190 1.1 tls case LKM_E_LOAD:
191 1.1 tls accept_filter_init();
192 1.4 tls p = kmem_alloc(sizeof(struct accept_filter), KM_SLEEP);
193 1.1 tls bcopy(accfp, p, sizeof(*p));
194 1.1 tls error = accept_filt_add(p);
195 1.1 tls break;
196 1.1 tls
197 1.1 tls case LKM_E_UNLOAD:
198 1.1 tls /*
199 1.1 tls * Do not support unloading yet. we don't keep track of
200 1.1 tls * refcounts and unloading an accept filter callback and then
201 1.1 tls * having it called is a bad thing. A simple fix would be to
202 1.1 tls * track the refcount in the struct accept_filter.
203 1.1 tls */
204 1.1 tls if (unloadable != 0) {
205 1.1 tls error = accept_filt_del(accfp->accf_name);
206 1.1 tls } else
207 1.1 tls error = EOPNOTSUPP;
208 1.1 tls break;
209 1.1 tls
210 1.1 tls case LKM_E_STAT:
211 1.1 tls error = 0;
212 1.1 tls break;
213 1.1 tls
214 1.1 tls default:
215 1.1 tls error = EOPNOTSUPP;
216 1.1 tls break;
217 1.1 tls }
218 1.1 tls
219 1.1 tls return (error);
220 1.1 tls }
221 1.1 tls
222 1.1 tls int
223 1.2 plunky do_getopt_accept_filter(struct socket *so, struct sockopt *sopt)
224 1.1 tls {
225 1.2 plunky struct accept_filter_arg afa;
226 1.1 tls int error;
227 1.1 tls
228 1.1 tls SOCK_LOCK(so);
229 1.1 tls if ((so->so_options & SO_ACCEPTCONN) == 0) {
230 1.1 tls error = EINVAL;
231 1.1 tls goto out;
232 1.1 tls }
233 1.1 tls if ((so->so_options & SO_ACCEPTFILTER) == 0) {
234 1.1 tls error = EINVAL;
235 1.1 tls goto out;
236 1.1 tls }
237 1.2 plunky
238 1.2 plunky memset(&afa, 0, sizeof(afa));
239 1.2 plunky strcpy(afa.af_name, so->so_accf->so_accept_filter->accf_name);
240 1.1 tls if (so->so_accf->so_accept_filter_str != NULL)
241 1.2 plunky strcpy(afa.af_arg, so->so_accf->so_accept_filter_str);
242 1.2 plunky error = sockopt_set(sopt, &afa, sizeof(afa));
243 1.1 tls out:
244 1.1 tls SOCK_UNLOCK(so);
245 1.1 tls return (error);
246 1.1 tls }
247 1.1 tls
248 1.1 tls int
249 1.2 plunky do_setopt_accept_filter(struct socket *so, const struct sockopt *sopt)
250 1.1 tls {
251 1.2 plunky struct accept_filter_arg afa;
252 1.1 tls struct accept_filter *afp;
253 1.1 tls struct so_accf *newaf;
254 1.2 plunky int error;
255 1.1 tls
256 1.1 tls /*
257 1.1 tls * Handle the simple delete case first.
258 1.1 tls */
259 1.2 plunky if (sopt == NULL || sopt->sopt_size == 0) {
260 1.1 tls SOCK_LOCK(so);
261 1.1 tls if ((so->so_options & SO_ACCEPTCONN) == 0) {
262 1.1 tls SOCK_UNLOCK(so);
263 1.1 tls return (EINVAL);
264 1.1 tls }
265 1.1 tls if (so->so_accf != NULL) {
266 1.1 tls struct so_accf *af = so->so_accf;
267 1.1 tls if (af->so_accept_filter != NULL &&
268 1.1 tls af->so_accept_filter->accf_destroy != NULL) {
269 1.1 tls af->so_accept_filter->accf_destroy(so);
270 1.1 tls }
271 1.4 tls if (af->so_accept_filter_str != NULL) {
272 1.4 tls kmem_free(af->so_accept_filter_str,
273 1.4 tls sizeof(afa.af_name));
274 1.4 tls }
275 1.4 tls kmem_free(af, sizeof(*af));
276 1.1 tls so->so_accf = NULL;
277 1.1 tls }
278 1.1 tls so->so_options &= ~SO_ACCEPTFILTER;
279 1.1 tls SOCK_UNLOCK(so);
280 1.1 tls return (0);
281 1.1 tls }
282 1.1 tls
283 1.1 tls /*
284 1.1 tls * Pre-allocate any memory we may need later to avoid blocking at
285 1.1 tls * untimely moments. This does not optimize for invalid arguments.
286 1.4 tls *
287 1.4 tls * XXX on NetBSD, we're called with the socket lock already held,
288 1.4 tls * XXX so we should not allow this allocation to block either.
289 1.1 tls */
290 1.2 plunky error = sockopt_get(sopt, &afa, sizeof(afa));
291 1.2 plunky if (error) {
292 1.2 plunky return (error);
293 1.1 tls }
294 1.2 plunky afa.af_name[sizeof(afa.af_name)-1] = '\0';
295 1.2 plunky afa.af_arg[sizeof(afa.af_arg)-1] = '\0';
296 1.2 plunky afp = accept_filt_get(afa.af_name);
297 1.1 tls if (afp == NULL) {
298 1.1 tls return (ENOENT);
299 1.1 tls }
300 1.1 tls /*
301 1.1 tls * Allocate the new accept filter instance storage. We may
302 1.1 tls * have to free it again later if we fail to attach it. If
303 1.1 tls * attached properly, 'newaf' is NULLed to avoid a free()
304 1.1 tls * while in use.
305 1.1 tls */
306 1.4 tls newaf = kmem_zalloc(sizeof(*newaf), KM_NOSLEEP);
307 1.4 tls if (!newaf) {
308 1.4 tls return ENOMEM;
309 1.4 tls }
310 1.2 plunky if (afp->accf_create != NULL && afa.af_name[0] != '\0') {
311 1.4 tls /*
312 1.4 tls * FreeBSD did a variable-size allocation here
313 1.4 tls * with the actual string length from afa.af_name
314 1.4 tls * but it is so short, why bother tracking it?
315 1.4 tls * XXX as others have noted, this is an API mistake;
316 1.4 tls * XXX accept_filter_arg should have a mandatory namelen.
317 1.4 tls * XXX (but it's a bit too late to fix that now)
318 1.4 tls */
319 1.4 tls newaf->so_accept_filter_str =
320 1.4 tls kmem_alloc(sizeof(afa.af_name), KM_NOSLEEP);
321 1.4 tls if (!newaf->so_accept_filter_str) {
322 1.4 tls kmem_free(newaf, sizeof(*newaf));
323 1.4 tls return ENOMEM;
324 1.4 tls }
325 1.2 plunky strcpy(newaf->so_accept_filter_str, afa.af_name);
326 1.1 tls }
327 1.1 tls
328 1.1 tls /*
329 1.1 tls * Require a listen socket; don't try to replace an existing filter
330 1.1 tls * without first removing it.
331 1.1 tls */
332 1.1 tls SOCK_LOCK(so);
333 1.1 tls if (((so->so_options & SO_ACCEPTCONN) == 0) ||
334 1.1 tls (so->so_accf != NULL)) {
335 1.1 tls error = EINVAL;
336 1.1 tls goto out;
337 1.1 tls }
338 1.1 tls
339 1.1 tls /*
340 1.1 tls * Invoke the accf_create() method of the filter if required. The
341 1.1 tls * socket mutex is held over this call, so create methods for filters
342 1.1 tls * can't block.
343 1.1 tls */
344 1.1 tls if (afp->accf_create != NULL) {
345 1.1 tls newaf->so_accept_filter_arg =
346 1.2 plunky afp->accf_create(so, afa.af_arg);
347 1.1 tls if (newaf->so_accept_filter_arg == NULL) {
348 1.1 tls error = EINVAL;
349 1.1 tls goto out;
350 1.1 tls }
351 1.1 tls }
352 1.1 tls newaf->so_accept_filter = afp;
353 1.1 tls so->so_accf = newaf;
354 1.1 tls so->so_options |= SO_ACCEPTFILTER;
355 1.1 tls newaf = NULL;
356 1.1 tls out:
357 1.1 tls SOCK_UNLOCK(so);
358 1.1 tls if (newaf != NULL) {
359 1.1 tls if (newaf->so_accept_filter_str != NULL)
360 1.4 tls kmem_free(newaf->so_accept_filter_str,
361 1.4 tls sizeof(afa.af_name));
362 1.4 tls kmem_free(newaf, sizeof(*newaf));
363 1.1 tls }
364 1.1 tls return (error);
365 1.1 tls }
366