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