1 | /* $NetBSD: uipc_accf.c,v 1.13 2014/02/25 18:30:11 pooka Exp $ */ |
2 | |
3 | /*- |
4 | * Copyright (c) 2008 The NetBSD Foundation, Inc. |
5 | * All rights reserved. |
6 | * |
7 | * This code is derived from software developed for The NetBSD Foundation |
8 | * by Andrew Doran. |
9 | * |
10 | * Redistribution and use in source and binary forms, with or without |
11 | * modification, are permitted provided that the following conditions |
12 | * are met: |
13 | * 1. Redistributions of source code must retain the above copyright |
14 | * notice, this list of conditions and the following disclaimer. |
15 | * 2. Redistributions in binary form must reproduce the above copyright |
16 | * notice, this list of conditions and the following disclaimer in the |
17 | * documentation and/or other materials provided with the distribution. |
18 | * |
19 | * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS |
20 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
21 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
22 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS |
23 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
29 | * POSSIBILITY OF SUCH DAMAGE. |
30 | */ |
31 | |
32 | /*- |
33 | * Copyright (c) 2000 Paycounter, Inc. |
34 | * Copyright (c) 2005 Robert N. M. Watson |
35 | * Author: Alfred Perlstein <alfred@paycounter.com>, <alfred@FreeBSD.org> |
36 | * All rights reserved. |
37 | * |
38 | * Redistribution and use in source and binary forms, with or without |
39 | * modification, are permitted provided that the following conditions |
40 | * are met: |
41 | * 1. Redistributions of source code must retain the above copyright |
42 | * notice, this list of conditions and the following disclaimer. |
43 | * 2. Redistributions in binary form must reproduce the above copyright |
44 | * notice, this list of conditions and the following disclaimer in the |
45 | * documentation and/or other materials provided with the distribution. |
46 | * |
47 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
48 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
49 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
50 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
51 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
52 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
53 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
54 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
55 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
56 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
57 | * SUCH DAMAGE. |
58 | */ |
59 | |
60 | #include <sys/cdefs.h> |
61 | __KERNEL_RCSID(0, "$NetBSD: uipc_accf.c,v 1.13 2014/02/25 18:30:11 pooka Exp $" ); |
62 | |
63 | #define ACCEPT_FILTER_MOD |
64 | |
65 | #include <sys/param.h> |
66 | #include <sys/systm.h> |
67 | #include <sys/domain.h> |
68 | #include <sys/kernel.h> |
69 | #include <sys/lock.h> |
70 | #include <sys/kmem.h> |
71 | #include <sys/mbuf.h> |
72 | #include <sys/rwlock.h> |
73 | #include <sys/protosw.h> |
74 | #include <sys/sysctl.h> |
75 | #include <sys/socket.h> |
76 | #include <sys/socketvar.h> |
77 | #include <sys/queue.h> |
78 | #include <sys/once.h> |
79 | #include <sys/atomic.h> |
80 | #include <sys/module.h> |
81 | |
82 | static krwlock_t accept_filter_lock; |
83 | |
84 | static LIST_HEAD(, accept_filter) accept_filtlsthd = |
85 | LIST_HEAD_INITIALIZER(&accept_filtlsthd); |
86 | |
87 | /* |
88 | * Names of Accept filter sysctl objects |
89 | */ |
90 | static struct sysctllog *ctllog; |
91 | static void |
92 | sysctl_net_inet_accf_setup(void) |
93 | { |
94 | |
95 | sysctl_createv(&ctllog, 0, NULL, NULL, |
96 | CTLFLAG_PERMANENT, |
97 | CTLTYPE_NODE, "inet" , NULL, |
98 | NULL, 0, NULL, 0, |
99 | CTL_NET, PF_INET, CTL_EOL); |
100 | sysctl_createv(&ctllog, 0, NULL, NULL, |
101 | CTLFLAG_PERMANENT, |
102 | CTLTYPE_NODE, "accf" , |
103 | SYSCTL_DESCR("Accept filters" ), |
104 | NULL, 0, NULL, 0, |
105 | CTL_NET, PF_INET, SO_ACCEPTFILTER, CTL_EOL); |
106 | } |
107 | |
108 | int |
109 | accept_filt_add(struct accept_filter *filt) |
110 | { |
111 | struct accept_filter *p; |
112 | |
113 | accept_filter_init(); |
114 | |
115 | rw_enter(&accept_filter_lock, RW_WRITER); |
116 | LIST_FOREACH(p, &accept_filtlsthd, accf_next) { |
117 | if (strcmp(p->accf_name, filt->accf_name) == 0) { |
118 | rw_exit(&accept_filter_lock); |
119 | return EEXIST; |
120 | } |
121 | } |
122 | LIST_INSERT_HEAD(&accept_filtlsthd, filt, accf_next); |
123 | rw_exit(&accept_filter_lock); |
124 | |
125 | return 0; |
126 | } |
127 | |
128 | int |
129 | accept_filt_del(struct accept_filter *p) |
130 | { |
131 | |
132 | rw_enter(&accept_filter_lock, RW_WRITER); |
133 | if (p->accf_refcnt != 0) { |
134 | rw_exit(&accept_filter_lock); |
135 | return EBUSY; |
136 | } |
137 | LIST_REMOVE(p, accf_next); |
138 | rw_exit(&accept_filter_lock); |
139 | |
140 | return 0; |
141 | } |
142 | |
143 | struct accept_filter * |
144 | accept_filt_get(char *name) |
145 | { |
146 | struct accept_filter *p; |
147 | char buf[32]; |
148 | u_int gen; |
149 | |
150 | do { |
151 | rw_enter(&accept_filter_lock, RW_READER); |
152 | LIST_FOREACH(p, &accept_filtlsthd, accf_next) { |
153 | if (strcmp(p->accf_name, name) == 0) { |
154 | atomic_inc_uint(&p->accf_refcnt); |
155 | break; |
156 | } |
157 | } |
158 | rw_exit(&accept_filter_lock); |
159 | if (p != NULL) { |
160 | break; |
161 | } |
162 | /* Try to autoload a module to satisfy the request. */ |
163 | strcpy(buf, "accf_" ); |
164 | strlcat(buf, name, sizeof(buf)); |
165 | gen = module_gen; |
166 | (void)module_autoload(buf, MODULE_CLASS_ANY); |
167 | } while (gen != module_gen); |
168 | |
169 | return p; |
170 | } |
171 | |
172 | /* |
173 | * Accept filter initialization routine. |
174 | * This should be called only once. |
175 | */ |
176 | |
177 | static int |
178 | accept_filter_init0(void) |
179 | { |
180 | |
181 | rw_init(&accept_filter_lock); |
182 | sysctl_net_inet_accf_setup(); |
183 | |
184 | return 0; |
185 | } |
186 | |
187 | /* |
188 | * Initialization routine: This can also be replaced with |
189 | * accept_filt_generic_mod_event for attaching new accept filter. |
190 | */ |
191 | |
192 | void |
193 | accept_filter_init(void) |
194 | { |
195 | static ONCE_DECL(accept_filter_init_once); |
196 | |
197 | RUN_ONCE(&accept_filter_init_once, accept_filter_init0); |
198 | } |
199 | |
200 | int |
201 | accept_filt_getopt(struct socket *so, struct sockopt *sopt) |
202 | { |
203 | struct accept_filter_arg afa; |
204 | int error; |
205 | |
206 | KASSERT(solocked(so)); |
207 | |
208 | if ((so->so_options & SO_ACCEPTCONN) == 0) { |
209 | error = EINVAL; |
210 | goto out; |
211 | } |
212 | if ((so->so_options & SO_ACCEPTFILTER) == 0) { |
213 | error = EINVAL; |
214 | goto out; |
215 | } |
216 | |
217 | memset(&afa, 0, sizeof(afa)); |
218 | strcpy(afa.af_name, so->so_accf->so_accept_filter->accf_name); |
219 | if (so->so_accf->so_accept_filter_str != NULL) |
220 | strcpy(afa.af_arg, so->so_accf->so_accept_filter_str); |
221 | error = sockopt_set(sopt, &afa, sizeof(afa)); |
222 | out: |
223 | return error; |
224 | } |
225 | |
226 | /* |
227 | * Simple delete case, with socket locked. |
228 | */ |
229 | int |
230 | accept_filt_clear(struct socket *so) |
231 | { |
232 | struct accept_filter_arg afa; |
233 | struct accept_filter *afp; |
234 | struct socket *so2, *next; |
235 | struct so_accf *af; |
236 | |
237 | KASSERT(solocked(so)); |
238 | |
239 | if ((so->so_options & SO_ACCEPTCONN) == 0) { |
240 | return EINVAL; |
241 | } |
242 | if (so->so_accf != NULL) { |
243 | /* Break in-flight processing. */ |
244 | for (so2 = TAILQ_FIRST(&so->so_q0); so2 != NULL; so2 = next) { |
245 | next = TAILQ_NEXT(so2, so_qe); |
246 | if (so2->so_upcall == NULL) { |
247 | continue; |
248 | } |
249 | so2->so_upcall = NULL; |
250 | so2->so_upcallarg = NULL; |
251 | so2->so_options &= ~SO_ACCEPTFILTER; |
252 | so2->so_rcv.sb_flags &= ~SB_UPCALL; |
253 | soisconnected(so2); |
254 | } |
255 | af = so->so_accf; |
256 | afp = af->so_accept_filter; |
257 | if (afp != NULL && afp->accf_destroy != NULL) { |
258 | (*afp->accf_destroy)(so); |
259 | } |
260 | if (af->so_accept_filter_str != NULL) { |
261 | kmem_free(af->so_accept_filter_str, |
262 | sizeof(afa.af_name)); |
263 | } |
264 | kmem_free(af, sizeof(*af)); |
265 | so->so_accf = NULL; |
266 | atomic_dec_uint(&afp->accf_refcnt); |
267 | } |
268 | so->so_options &= ~SO_ACCEPTFILTER; |
269 | return 0; |
270 | } |
271 | |
272 | /* |
273 | * setsockopt() for accept filters. Called with the socket unlocked, |
274 | * will always return it locked. |
275 | */ |
276 | int |
277 | accept_filt_setopt(struct socket *so, const struct sockopt *sopt) |
278 | { |
279 | struct accept_filter_arg afa; |
280 | struct accept_filter *afp; |
281 | struct so_accf *newaf; |
282 | int error; |
283 | |
284 | accept_filter_init(); |
285 | |
286 | if (sopt == NULL || sopt->sopt_size == 0) { |
287 | solock(so); |
288 | return accept_filt_clear(so); |
289 | } |
290 | |
291 | /* |
292 | * Pre-allocate any memory we may need later to avoid blocking at |
293 | * untimely moments. This does not optimize for invalid arguments. |
294 | */ |
295 | error = sockopt_get(sopt, &afa, sizeof(afa)); |
296 | if (error) { |
297 | solock(so); |
298 | return error; |
299 | } |
300 | afa.af_name[sizeof(afa.af_name)-1] = '\0'; |
301 | afa.af_arg[sizeof(afa.af_arg)-1] = '\0'; |
302 | afp = accept_filt_get(afa.af_name); |
303 | if (afp == NULL) { |
304 | solock(so); |
305 | return ENOENT; |
306 | } |
307 | /* |
308 | * Allocate the new accept filter instance storage. We may |
309 | * have to free it again later if we fail to attach it. If |
310 | * attached properly, 'newaf' is NULLed to avoid a free() |
311 | * while in use. |
312 | */ |
313 | newaf = kmem_zalloc(sizeof(*newaf), KM_SLEEP); |
314 | if (afp->accf_create != NULL && afa.af_name[0] != '\0') { |
315 | /* |
316 | * FreeBSD did a variable-size allocation here |
317 | * with the actual string length from afa.af_name |
318 | * but it is so short, why bother tracking it? |
319 | * XXX as others have noted, this is an API mistake; |
320 | * XXX accept_filter_arg should have a mandatory namelen. |
321 | * XXX (but it's a bit too late to fix that now) |
322 | */ |
323 | newaf->so_accept_filter_str = |
324 | kmem_alloc(sizeof(afa.af_name), KM_SLEEP); |
325 | strcpy(newaf->so_accept_filter_str, afa.af_name); |
326 | } |
327 | |
328 | /* |
329 | * Require a listen socket; don't try to replace an existing filter |
330 | * without first removing it. |
331 | */ |
332 | solock(so); |
333 | if ((so->so_options & SO_ACCEPTCONN) == 0 || so->so_accf != NULL) { |
334 | error = EINVAL; |
335 | goto out; |
336 | } |
337 | |
338 | /* |
339 | * Invoke the accf_create() method of the filter if required. The |
340 | * socket lock is held over this call, so create methods for filters |
341 | * shouldn't block. |
342 | */ |
343 | if (afp->accf_create != NULL) { |
344 | newaf->so_accept_filter_arg = |
345 | (*afp->accf_create)(so, afa.af_arg); |
346 | if (newaf->so_accept_filter_arg == NULL) { |
347 | error = EINVAL; |
348 | goto out; |
349 | } |
350 | } |
351 | newaf->so_accept_filter = afp; |
352 | so->so_accf = newaf; |
353 | so->so_options |= SO_ACCEPTFILTER; |
354 | newaf = NULL; |
355 | out: |
356 | if (newaf != NULL) { |
357 | if (newaf->so_accept_filter_str != NULL) |
358 | kmem_free(newaf->so_accept_filter_str, |
359 | sizeof(afa.af_name)); |
360 | kmem_free(newaf, sizeof(*newaf)); |
361 | atomic_dec_uint(&afp->accf_refcnt); |
362 | } |
363 | return error; |
364 | } |
365 | |