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