uipc_accf.c revision 1.8.6.2 1 1.8.6.2 yamt /* $NetBSD: uipc_accf.c,v 1.8.6.2 2009/05/04 08:13:49 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.2 yamt __KERNEL_RCSID(0, "$NetBSD: uipc_accf.c,v 1.8.6.2 2009/05/04 08:13:49 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.2 yamt SYSCTL_SETUP(sysctl_net_inet_accf_setup, "sysctl net.inet.accf subtree setup")
91 1.8.6.2 yamt {
92 1.8.6.2 yamt
93 1.8.6.2 yamt sysctl_createv(clog, 0, NULL, NULL,
94 1.8.6.2 yamt CTLFLAG_PERMANENT,
95 1.8.6.2 yamt CTLTYPE_NODE, "net", NULL,
96 1.8.6.2 yamt NULL, 0, NULL, 0,
97 1.8.6.2 yamt CTL_NET, CTL_EOL);
98 1.8.6.2 yamt sysctl_createv(clog, 0, NULL, NULL,
99 1.8.6.2 yamt CTLFLAG_PERMANENT,
100 1.8.6.2 yamt CTLTYPE_NODE, "inet", NULL,
101 1.8.6.2 yamt NULL, 0, NULL, 0,
102 1.8.6.2 yamt CTL_NET, PF_INET, CTL_EOL);
103 1.8.6.2 yamt sysctl_createv(clog, 0, NULL, NULL,
104 1.8.6.2 yamt CTLFLAG_PERMANENT,
105 1.8.6.2 yamt CTLTYPE_NODE, "accf",
106 1.8.6.2 yamt SYSCTL_DESCR("Accept filters"),
107 1.8.6.2 yamt NULL, 0, NULL, 0,
108 1.8.6.2 yamt CTL_NET, PF_INET, SO_ACCEPTFILTER, CTL_EOL);
109 1.8.6.2 yamt }
110 1.8.6.2 yamt
111 1.8.6.2 yamt int
112 1.8.6.2 yamt accept_filt_add(struct accept_filter *filt)
113 1.8.6.2 yamt {
114 1.8.6.2 yamt struct accept_filter *p;
115 1.8.6.2 yamt
116 1.8.6.2 yamt accept_filter_init();
117 1.8.6.2 yamt
118 1.8.6.2 yamt rw_enter(&accept_filter_lock, RW_WRITER);
119 1.8.6.2 yamt LIST_FOREACH(p, &accept_filtlsthd, accf_next) {
120 1.8.6.2 yamt if (strcmp(p->accf_name, filt->accf_name) == 0) {
121 1.8.6.2 yamt rw_exit(&accept_filter_lock);
122 1.8.6.2 yamt return EEXIST;
123 1.8.6.2 yamt }
124 1.8.6.2 yamt }
125 1.8.6.2 yamt LIST_INSERT_HEAD(&accept_filtlsthd, filt, accf_next);
126 1.8.6.2 yamt rw_exit(&accept_filter_lock);
127 1.8.6.2 yamt
128 1.8.6.2 yamt return 0;
129 1.8.6.2 yamt }
130 1.8.6.2 yamt
131 1.8.6.2 yamt int
132 1.8.6.2 yamt accept_filt_del(struct accept_filter *p)
133 1.8.6.2 yamt {
134 1.8.6.2 yamt
135 1.8.6.2 yamt rw_enter(&accept_filter_lock, RW_WRITER);
136 1.8.6.2 yamt if (p->accf_refcnt != 0) {
137 1.8.6.2 yamt rw_exit(&accept_filter_lock);
138 1.8.6.2 yamt return EBUSY;
139 1.8.6.2 yamt }
140 1.8.6.2 yamt LIST_REMOVE(p, accf_next);
141 1.8.6.2 yamt rw_exit(&accept_filter_lock);
142 1.8.6.2 yamt
143 1.8.6.2 yamt return 0;
144 1.8.6.2 yamt }
145 1.8.6.2 yamt
146 1.8.6.2 yamt struct accept_filter *
147 1.8.6.2 yamt accept_filt_get(char *name)
148 1.8.6.2 yamt {
149 1.8.6.2 yamt struct accept_filter *p;
150 1.8.6.2 yamt char buf[32];
151 1.8.6.2 yamt u_int gen;
152 1.8.6.2 yamt
153 1.8.6.2 yamt do {
154 1.8.6.2 yamt rw_enter(&accept_filter_lock, RW_READER);
155 1.8.6.2 yamt LIST_FOREACH(p, &accept_filtlsthd, accf_next) {
156 1.8.6.2 yamt if (strcmp(p->accf_name, name) == 0) {
157 1.8.6.2 yamt atomic_inc_uint(&p->accf_refcnt);
158 1.8.6.2 yamt break;
159 1.8.6.2 yamt }
160 1.8.6.2 yamt }
161 1.8.6.2 yamt rw_exit(&accept_filter_lock);
162 1.8.6.2 yamt if (p != NULL) {
163 1.8.6.2 yamt break;
164 1.8.6.2 yamt }
165 1.8.6.2 yamt /* Try to autoload a module to satisfy the request. */
166 1.8.6.2 yamt strcpy(buf, "accf_");
167 1.8.6.2 yamt strlcat(buf, name, sizeof(buf));
168 1.8.6.2 yamt mutex_enter(&module_lock);
169 1.8.6.2 yamt gen = module_gen;
170 1.8.6.2 yamt (void)module_autoload(buf, MODULE_CLASS_ANY);
171 1.8.6.2 yamt mutex_exit(&module_lock);
172 1.8.6.2 yamt } while (gen != module_gen);
173 1.8.6.2 yamt
174 1.8.6.2 yamt return p;
175 1.8.6.2 yamt }
176 1.8.6.2 yamt
177 1.8.6.2 yamt /*
178 1.8.6.2 yamt * Accept filter initialization routine.
179 1.8.6.2 yamt * This should be called only once.
180 1.8.6.2 yamt */
181 1.8.6.2 yamt
182 1.8.6.2 yamt static int
183 1.8.6.2 yamt accept_filter_init0(void)
184 1.8.6.2 yamt {
185 1.8.6.2 yamt
186 1.8.6.2 yamt rw_init(&accept_filter_lock);
187 1.8.6.2 yamt
188 1.8.6.2 yamt return 0;
189 1.8.6.2 yamt }
190 1.8.6.2 yamt
191 1.8.6.2 yamt /*
192 1.8.6.2 yamt * Initialization routine: This can also be replaced with
193 1.8.6.2 yamt * accept_filt_generic_mod_event for attaching new accept filter.
194 1.8.6.2 yamt */
195 1.8.6.2 yamt
196 1.8.6.2 yamt void
197 1.8.6.2 yamt accept_filter_init(void)
198 1.8.6.2 yamt {
199 1.8.6.2 yamt static ONCE_DECL(accept_filter_init_once);
200 1.8.6.2 yamt
201 1.8.6.2 yamt RUN_ONCE(&accept_filter_init_once, accept_filter_init0);
202 1.8.6.2 yamt }
203 1.8.6.2 yamt
204 1.8.6.2 yamt int
205 1.8.6.2 yamt accept_filt_getopt(struct socket *so, struct sockopt *sopt)
206 1.8.6.2 yamt {
207 1.8.6.2 yamt struct accept_filter_arg afa;
208 1.8.6.2 yamt int error;
209 1.8.6.2 yamt
210 1.8.6.2 yamt KASSERT(solocked(so));
211 1.8.6.2 yamt
212 1.8.6.2 yamt if ((so->so_options & SO_ACCEPTCONN) == 0) {
213 1.8.6.2 yamt error = EINVAL;
214 1.8.6.2 yamt goto out;
215 1.8.6.2 yamt }
216 1.8.6.2 yamt if ((so->so_options & SO_ACCEPTFILTER) == 0) {
217 1.8.6.2 yamt error = EINVAL;
218 1.8.6.2 yamt goto out;
219 1.8.6.2 yamt }
220 1.8.6.2 yamt
221 1.8.6.2 yamt memset(&afa, 0, sizeof(afa));
222 1.8.6.2 yamt strcpy(afa.af_name, so->so_accf->so_accept_filter->accf_name);
223 1.8.6.2 yamt if (so->so_accf->so_accept_filter_str != NULL)
224 1.8.6.2 yamt strcpy(afa.af_arg, so->so_accf->so_accept_filter_str);
225 1.8.6.2 yamt error = sockopt_set(sopt, &afa, sizeof(afa));
226 1.8.6.2 yamt out:
227 1.8.6.2 yamt return error;
228 1.8.6.2 yamt }
229 1.8.6.2 yamt
230 1.8.6.2 yamt /*
231 1.8.6.2 yamt * Simple delete case, with socket locked.
232 1.8.6.2 yamt */
233 1.8.6.2 yamt int
234 1.8.6.2 yamt accept_filt_clear(struct socket *so)
235 1.8.6.2 yamt {
236 1.8.6.2 yamt struct accept_filter_arg afa;
237 1.8.6.2 yamt struct accept_filter *afp;
238 1.8.6.2 yamt struct socket *so2, *next;
239 1.8.6.2 yamt struct so_accf *af;
240 1.8.6.2 yamt
241 1.8.6.2 yamt KASSERT(solocked(so));
242 1.8.6.2 yamt
243 1.8.6.2 yamt if ((so->so_options & SO_ACCEPTCONN) == 0) {
244 1.8.6.2 yamt return EINVAL;
245 1.8.6.2 yamt }
246 1.8.6.2 yamt if (so->so_accf != NULL) {
247 1.8.6.2 yamt /* Break in-flight processing. */
248 1.8.6.2 yamt for (so2 = TAILQ_FIRST(&so->so_q0); so2 != NULL; so2 = next) {
249 1.8.6.2 yamt next = TAILQ_NEXT(so2, so_qe);
250 1.8.6.2 yamt if (so2->so_upcall == NULL) {
251 1.8.6.2 yamt continue;
252 1.8.6.2 yamt }
253 1.8.6.2 yamt so2->so_upcall = NULL;
254 1.8.6.2 yamt so2->so_upcallarg = NULL;
255 1.8.6.2 yamt so2->so_options &= ~SO_ACCEPTFILTER;
256 1.8.6.2 yamt so2->so_rcv.sb_flags &= ~SB_UPCALL;
257 1.8.6.2 yamt soisconnected(so2);
258 1.8.6.2 yamt }
259 1.8.6.2 yamt af = so->so_accf;
260 1.8.6.2 yamt afp = af->so_accept_filter;
261 1.8.6.2 yamt if (afp != NULL && afp->accf_destroy != NULL) {
262 1.8.6.2 yamt (*afp->accf_destroy)(so);
263 1.8.6.2 yamt }
264 1.8.6.2 yamt if (af->so_accept_filter_str != NULL) {
265 1.8.6.2 yamt kmem_free(af->so_accept_filter_str,
266 1.8.6.2 yamt sizeof(afa.af_name));
267 1.8.6.2 yamt }
268 1.8.6.2 yamt kmem_free(af, sizeof(*af));
269 1.8.6.2 yamt so->so_accf = NULL;
270 1.8.6.2 yamt atomic_dec_uint(&afp->accf_refcnt);
271 1.8.6.2 yamt }
272 1.8.6.2 yamt so->so_options &= ~SO_ACCEPTFILTER;
273 1.8.6.2 yamt return 0;
274 1.8.6.2 yamt }
275 1.8.6.2 yamt
276 1.8.6.2 yamt /*
277 1.8.6.2 yamt * setsockopt() for accept filters. Called with the socket unlocked,
278 1.8.6.2 yamt * will always return it locked.
279 1.8.6.2 yamt */
280 1.8.6.2 yamt int
281 1.8.6.2 yamt accept_filt_setopt(struct socket *so, const struct sockopt *sopt)
282 1.8.6.2 yamt {
283 1.8.6.2 yamt struct accept_filter_arg afa;
284 1.8.6.2 yamt struct accept_filter *afp;
285 1.8.6.2 yamt struct so_accf *newaf;
286 1.8.6.2 yamt int error;
287 1.8.6.2 yamt
288 1.8.6.2 yamt if (sopt == NULL || sopt->sopt_size == 0) {
289 1.8.6.2 yamt solock(so);
290 1.8.6.2 yamt return accept_filt_clear(so);
291 1.8.6.2 yamt }
292 1.8.6.2 yamt
293 1.8.6.2 yamt /*
294 1.8.6.2 yamt * Pre-allocate any memory we may need later to avoid blocking at
295 1.8.6.2 yamt * untimely moments. This does not optimize for invalid arguments.
296 1.8.6.2 yamt */
297 1.8.6.2 yamt error = sockopt_get(sopt, &afa, sizeof(afa));
298 1.8.6.2 yamt if (error) {
299 1.8.6.2 yamt solock(so);
300 1.8.6.2 yamt return error;
301 1.8.6.2 yamt }
302 1.8.6.2 yamt afa.af_name[sizeof(afa.af_name)-1] = '\0';
303 1.8.6.2 yamt afa.af_arg[sizeof(afa.af_arg)-1] = '\0';
304 1.8.6.2 yamt afp = accept_filt_get(afa.af_name);
305 1.8.6.2 yamt if (afp == NULL) {
306 1.8.6.2 yamt solock(so);
307 1.8.6.2 yamt return ENOENT;
308 1.8.6.2 yamt }
309 1.8.6.2 yamt /*
310 1.8.6.2 yamt * Allocate the new accept filter instance storage. We may
311 1.8.6.2 yamt * have to free it again later if we fail to attach it. If
312 1.8.6.2 yamt * attached properly, 'newaf' is NULLed to avoid a free()
313 1.8.6.2 yamt * while in use.
314 1.8.6.2 yamt */
315 1.8.6.2 yamt newaf = kmem_zalloc(sizeof(*newaf), KM_SLEEP);
316 1.8.6.2 yamt if (afp->accf_create != NULL && afa.af_name[0] != '\0') {
317 1.8.6.2 yamt /*
318 1.8.6.2 yamt * FreeBSD did a variable-size allocation here
319 1.8.6.2 yamt * with the actual string length from afa.af_name
320 1.8.6.2 yamt * but it is so short, why bother tracking it?
321 1.8.6.2 yamt * XXX as others have noted, this is an API mistake;
322 1.8.6.2 yamt * XXX accept_filter_arg should have a mandatory namelen.
323 1.8.6.2 yamt * XXX (but it's a bit too late to fix that now)
324 1.8.6.2 yamt */
325 1.8.6.2 yamt newaf->so_accept_filter_str =
326 1.8.6.2 yamt kmem_alloc(sizeof(afa.af_name), KM_SLEEP);
327 1.8.6.2 yamt strcpy(newaf->so_accept_filter_str, afa.af_name);
328 1.8.6.2 yamt }
329 1.8.6.2 yamt
330 1.8.6.2 yamt /*
331 1.8.6.2 yamt * Require a listen socket; don't try to replace an existing filter
332 1.8.6.2 yamt * without first removing it.
333 1.8.6.2 yamt */
334 1.8.6.2 yamt solock(so);
335 1.8.6.2 yamt if ((so->so_options & SO_ACCEPTCONN) == 0 || so->so_accf != NULL) {
336 1.8.6.2 yamt error = EINVAL;
337 1.8.6.2 yamt goto out;
338 1.8.6.2 yamt }
339 1.8.6.2 yamt
340 1.8.6.2 yamt /*
341 1.8.6.2 yamt * Invoke the accf_create() method of the filter if required. The
342 1.8.6.2 yamt * socket lock is held over this call, so create methods for filters
343 1.8.6.2 yamt * shouldn't block.
344 1.8.6.2 yamt */
345 1.8.6.2 yamt if (afp->accf_create != NULL) {
346 1.8.6.2 yamt newaf->so_accept_filter_arg =
347 1.8.6.2 yamt (*afp->accf_create)(so, afa.af_arg);
348 1.8.6.2 yamt if (newaf->so_accept_filter_arg == NULL) {
349 1.8.6.2 yamt error = EINVAL;
350 1.8.6.2 yamt goto out;
351 1.8.6.2 yamt }
352 1.8.6.2 yamt }
353 1.8.6.2 yamt newaf->so_accept_filter = afp;
354 1.8.6.2 yamt so->so_accf = newaf;
355 1.8.6.2 yamt so->so_options |= SO_ACCEPTFILTER;
356 1.8.6.2 yamt newaf = NULL;
357 1.8.6.2 yamt out:
358 1.8.6.2 yamt if (newaf != NULL) {
359 1.8.6.2 yamt if (newaf->so_accept_filter_str != NULL)
360 1.8.6.2 yamt kmem_free(newaf->so_accept_filter_str,
361 1.8.6.2 yamt sizeof(afa.af_name));
362 1.8.6.2 yamt kmem_free(newaf, sizeof(*newaf));
363 1.8.6.2 yamt atomic_dec_uint(&afp->accf_refcnt);
364 1.8.6.2 yamt }
365 1.8.6.2 yamt return error;
366 1.8.6.2 yamt }
367