2 * Copyright (c) 2000 Paycounter, Inc.
3 * Copyright (c) 2005 Robert N. M. Watson
4 * Author: Alfred Perlstein <alfred@paycounter.com>, <alfred@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #define ACCEPT_FILTER_MOD
34 #include "opt_param.h"
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/domain.h>
38 #include <sys/kernel.h>
40 #include <sys/malloc.h>
42 #include <sys/module.h>
43 #include <sys/mutex.h>
44 #include <sys/protosw.h>
45 #include <sys/sysctl.h>
46 #include <sys/socket.h>
47 #include <sys/socketvar.h>
48 #include <sys/queue.h>
50 static struct mtx accept_filter_mtx;
51 MTX_SYSINIT(accept_filter, &accept_filter_mtx, "accept_filter_mtx",
53 #define ACCEPT_FILTER_LOCK() mtx_lock(&accept_filter_mtx)
54 #define ACCEPT_FILTER_UNLOCK() mtx_unlock(&accept_filter_mtx)
56 static SLIST_HEAD(, accept_filter) accept_filtlsthd =
57 SLIST_HEAD_INITIALIZER(accept_filtlsthd);
59 MALLOC_DEFINE(M_ACCF, "accf", "accept filter data");
61 static int unloadable = 0;
63 SYSCTL_DECL(_net_inet); /* XXX: some header should do this for me */
64 SYSCTL_NODE(_net_inet, OID_AUTO, accf, CTLFLAG_RW, 0, "Accept filters");
65 SYSCTL_INT(_net_inet_accf, OID_AUTO, unloadable, CTLFLAG_RW, &unloadable, 0,
66 "Allow unload of accept filters (not recommended)");
69 * Must be passed a malloc'd structure so we don't explode if the kld is
70 * unloaded, we leak the struct on deallocation to deal with this, but if a
71 * filter is loaded with the same name as a leaked one we re-use the entry.
74 accept_filt_add(struct accept_filter *filt)
76 struct accept_filter *p;
79 SLIST_FOREACH(p, &accept_filtlsthd, accf_next)
80 if (strcmp(p->accf_name, filt->accf_name) == 0) {
81 if (p->accf_callback != NULL) {
82 ACCEPT_FILTER_UNLOCK();
85 p->accf_callback = filt->accf_callback;
86 ACCEPT_FILTER_UNLOCK();
93 SLIST_INSERT_HEAD(&accept_filtlsthd, filt, accf_next);
94 ACCEPT_FILTER_UNLOCK();
99 accept_filt_del(char *name)
101 struct accept_filter *p;
103 p = accept_filt_get(name);
107 p->accf_callback = NULL;
111 struct accept_filter *
112 accept_filt_get(char *name)
114 struct accept_filter *p;
116 ACCEPT_FILTER_LOCK();
117 SLIST_FOREACH(p, &accept_filtlsthd, accf_next)
118 if (strcmp(p->accf_name, name) == 0)
120 ACCEPT_FILTER_UNLOCK();
126 accept_filt_generic_mod_event(module_t mod, int event, void *data)
128 struct accept_filter *p;
129 struct accept_filter *accfp = (struct accept_filter *) data;
134 p = malloc(sizeof(*p), M_ACCF,
136 bcopy(accfp, p, sizeof(*p));
137 error = accept_filt_add(p);
142 * Do not support unloading yet. we don't keep track of
143 * refcounts and unloading an accept filter callback and then
144 * having it called is a bad thing. A simple fix would be to
145 * track the refcount in the struct accept_filter.
147 if (unloadable != 0) {
148 error = accept_filt_del(accfp->accf_name);
166 do_getopt_accept_filter(struct socket *so, struct sockopt *sopt)
168 struct accept_filter_arg *afap;
172 afap = malloc(sizeof(*afap), M_TEMP,
175 if ((so->so_options & SO_ACCEPTCONN) == 0) {
179 if ((so->so_options & SO_ACCEPTFILTER) == 0) {
183 strcpy(afap->af_name, so->so_accf->so_accept_filter->accf_name);
184 if (so->so_accf->so_accept_filter_str != NULL)
185 strcpy(afap->af_arg, so->so_accf->so_accept_filter_str);
189 error = sooptcopyout(sopt, afap, sizeof(*afap));
195 do_setopt_accept_filter(struct socket *so, struct sockopt *sopt)
197 struct accept_filter_arg *afap;
198 struct accept_filter *afp;
199 struct so_accf *newaf;
203 * Handle the simple delete case first.
205 if (sopt == NULL || sopt->sopt_val == NULL) {
207 if ((so->so_options & SO_ACCEPTCONN) == 0) {
211 if (so->so_accf != NULL) {
212 struct so_accf *af = so->so_accf;
213 if (af->so_accept_filter != NULL &&
214 af->so_accept_filter->accf_destroy != NULL) {
215 af->so_accept_filter->accf_destroy(so);
217 if (af->so_accept_filter_str != NULL)
218 free(af->so_accept_filter_str, M_ACCF);
222 so->so_options &= ~SO_ACCEPTFILTER;
228 * Pre-allocate any memory we may need later to avoid blocking at
229 * untimely moments. This does not optimize for invalid arguments.
231 afap = malloc(sizeof(*afap), M_TEMP,
233 error = sooptcopyin(sopt, afap, sizeof *afap, sizeof *afap);
234 afap->af_name[sizeof(afap->af_name)-1] = '\0';
235 afap->af_arg[sizeof(afap->af_arg)-1] = '\0';
240 afp = accept_filt_get(afap->af_name);
246 * Allocate the new accept filter instance storage. We may
247 * have to free it again later if we fail to attach it. If
248 * attached properly, 'newaf' is NULLed to avoid a free()
251 newaf = malloc(sizeof(*newaf), M_ACCF, M_WAITOK |
253 if (afp->accf_create != NULL && afap->af_name[0] != '\0') {
254 int len = strlen(afap->af_name) + 1;
255 newaf->so_accept_filter_str = malloc(len, M_ACCF,
257 strcpy(newaf->so_accept_filter_str, afap->af_name);
261 * Require a listen socket; don't try to replace an existing filter
262 * without first removing it.
265 if (((so->so_options & SO_ACCEPTCONN) == 0) ||
266 (so->so_accf != NULL)) {
272 * Invoke the accf_create() method of the filter if required. The
273 * socket mutex is held over this call, so create methods for filters
276 if (afp->accf_create != NULL) {
277 newaf->so_accept_filter_arg =
278 afp->accf_create(so, afap->af_arg);
279 if (newaf->so_accept_filter_arg == NULL) {
284 newaf->so_accept_filter = afp;
286 so->so_options |= SO_ACCEPTFILTER;
291 if (newaf->so_accept_filter_str != NULL)
292 free(newaf->so_accept_filter_str, M_ACCF);