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