xref: /freebsd/sys/kern/uipc_accf.c (revision f56f82e0)
1 /*-
2  * Copyright (c) 2000 Paycounter, Inc.
3  * Copyright (c) 2005 Robert N. M. Watson
4  * Author: Alfred Perlstein <alfred@paycounter.com>, <alfred@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
15  *
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
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #define ACCEPT_FILTER_MOD
33 
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>
39 #include <sys/lock.h>
40 #include <sys/malloc.h>
41 #include <sys/mbuf.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>
49 
50 static struct mtx accept_filter_mtx;
51 MTX_SYSINIT(accept_filter, &accept_filter_mtx, "accept_filter_mtx",
52 	MTX_DEF);
53 #define	ACCEPT_FILTER_LOCK()	mtx_lock(&accept_filter_mtx)
54 #define	ACCEPT_FILTER_UNLOCK()	mtx_unlock(&accept_filter_mtx)
55 
56 static SLIST_HEAD(, accept_filter) accept_filtlsthd =
57 	SLIST_HEAD_INITIALIZER(accept_filtlsthd);
58 
59 MALLOC_DEFINE(M_ACCF, "accf", "accept filter data");
60 
61 static int unloadable = 0;
62 
63 SYSCTL_NODE(_net, OID_AUTO, accf, CTLFLAG_RW, 0, "Accept filters");
64 SYSCTL_INT(_net_accf, OID_AUTO, unloadable, CTLFLAG_RW, &unloadable, 0,
65 	"Allow unload of accept filters (not recommended)");
66 
67 /*
68  * Must be passed a malloc'd structure so we don't explode if the kld is
69  * unloaded, we leak the struct on deallocation to deal with this, but if a
70  * filter is loaded with the same name as a leaked one we re-use the entry.
71  */
72 int
73 accept_filt_add(struct accept_filter *filt)
74 {
75 	struct accept_filter *p;
76 
77 	ACCEPT_FILTER_LOCK();
78 	SLIST_FOREACH(p, &accept_filtlsthd, accf_next)
79 		if (strcmp(p->accf_name, filt->accf_name) == 0)  {
80 			if (p->accf_callback != NULL) {
81 				ACCEPT_FILTER_UNLOCK();
82 				return (EEXIST);
83 			} else {
84 				p->accf_callback = filt->accf_callback;
85 				ACCEPT_FILTER_UNLOCK();
86 				free(filt, M_ACCF);
87 				return (0);
88 			}
89 		}
90 
91 	if (p == NULL)
92 		SLIST_INSERT_HEAD(&accept_filtlsthd, filt, accf_next);
93 	ACCEPT_FILTER_UNLOCK();
94 	return (0);
95 }
96 
97 int
98 accept_filt_del(char *name)
99 {
100 	struct accept_filter *p;
101 
102 	p = accept_filt_get(name);
103 	if (p == NULL)
104 		return (ENOENT);
105 
106 	p->accf_callback = NULL;
107 	return (0);
108 }
109 
110 struct accept_filter *
111 accept_filt_get(char *name)
112 {
113 	struct accept_filter *p;
114 
115 	ACCEPT_FILTER_LOCK();
116 	SLIST_FOREACH(p, &accept_filtlsthd, accf_next)
117 		if (strcmp(p->accf_name, name) == 0)
118 			break;
119 	ACCEPT_FILTER_UNLOCK();
120 
121 	return (p);
122 }
123 
124 int
125 accept_filt_generic_mod_event(module_t mod, int event, void *data)
126 {
127 	struct accept_filter *p;
128 	struct accept_filter *accfp = (struct accept_filter *) data;
129 	int error;
130 
131 	switch (event) {
132 	case MOD_LOAD:
133 		p = malloc(sizeof(*p), M_ACCF, M_WAITOK);
134 		bcopy(accfp, p, sizeof(*p));
135 		error = accept_filt_add(p);
136 		break;
137 
138 	case MOD_UNLOAD:
139 		/*
140 		 * Do not support unloading yet. we don't keep track of
141 		 * refcounts and unloading an accept filter callback and then
142 		 * having it called is a bad thing.  A simple fix would be to
143 		 * track the refcount in the struct accept_filter.
144 		 */
145 		if (unloadable != 0) {
146 			error = accept_filt_del(accfp->accf_name);
147 		} else
148 			error = EOPNOTSUPP;
149 		break;
150 
151 	case MOD_SHUTDOWN:
152 		error = 0;
153 		break;
154 
155 	default:
156 		error = EOPNOTSUPP;
157 		break;
158 	}
159 
160 	return (error);
161 }
162 
163 int
164 accept_filt_getopt(struct socket *so, struct sockopt *sopt)
165 {
166 	struct accept_filter_arg *afap;
167 	int error;
168 
169 	error = 0;
170 	afap = malloc(sizeof(*afap), M_TEMP, M_WAITOK | M_ZERO);
171 	SOCK_LOCK(so);
172 	if ((so->so_options & SO_ACCEPTCONN) == 0) {
173 		error = EINVAL;
174 		goto out;
175 	}
176 	if (so->sol_accept_filter == NULL) {
177 		error = EINVAL;
178 		goto out;
179 	}
180 	strcpy(afap->af_name, so->sol_accept_filter->accf_name);
181 	if (so->sol_accept_filter_str != NULL)
182 		strcpy(afap->af_arg, so->sol_accept_filter_str);
183 out:
184 	SOCK_UNLOCK(so);
185 	if (error == 0)
186 		error = sooptcopyout(sopt, afap, sizeof(*afap));
187 	free(afap, M_TEMP);
188 	return (error);
189 }
190 
191 int
192 accept_filt_setopt(struct socket *so, struct sockopt *sopt)
193 {
194 	struct accept_filter_arg *afap;
195 	struct accept_filter *afp;
196 	char *accept_filter_str = NULL;
197 	void *accept_filter_arg = NULL;
198 	int error;
199 
200 	/*
201 	 * Handle the simple delete case first.
202 	 */
203 	if (sopt == NULL || sopt->sopt_val == NULL) {
204 		struct socket *sp, *sp1;
205 		int wakeup;
206 
207 		SOCK_LOCK(so);
208 		if ((so->so_options & SO_ACCEPTCONN) == 0) {
209 			SOCK_UNLOCK(so);
210 			return (EINVAL);
211 		}
212 		if (so->sol_accept_filter == NULL) {
213 			SOCK_UNLOCK(so);
214 			return (0);
215 		}
216 		if (so->sol_accept_filter->accf_destroy != NULL)
217 			so->sol_accept_filter->accf_destroy(so);
218 		if (so->sol_accept_filter_str != NULL)
219 			free(so->sol_accept_filter_str, M_ACCF);
220 		so->sol_accept_filter = NULL;
221 		so->sol_accept_filter_arg = NULL;
222 		so->sol_accept_filter_str = NULL;
223 		so->so_options &= ~SO_ACCEPTFILTER;
224 
225 		/*
226 		 * Move from incomplete queue to complete only those
227 		 * connections, that are blocked by us.
228 		 */
229 		wakeup = 0;
230 		TAILQ_FOREACH_SAFE(sp, &so->sol_incomp, so_list, sp1) {
231 			SOCK_LOCK(sp);
232 			if (sp->so_options & SO_ACCEPTFILTER) {
233 				TAILQ_REMOVE(&so->sol_incomp, sp, so_list);
234 				TAILQ_INSERT_TAIL(&so->sol_comp, sp, so_list);
235 				sp->so_qstate = SQ_COMP;
236 				sp->so_options &= ~SO_ACCEPTFILTER;
237 				so->sol_incqlen--;
238 				so->sol_qlen++;
239 				wakeup = 1;
240 			}
241 			SOCK_UNLOCK(sp);
242 		}
243 		if (wakeup)
244 			solisten_wakeup(so);  /* unlocks */
245 		else
246 			SOLISTEN_UNLOCK(so);
247 		return (0);
248 	}
249 
250 	/*
251 	 * Pre-allocate any memory we may need later to avoid blocking at
252 	 * untimely moments.  This does not optimize for invalid arguments.
253 	 */
254 	afap = malloc(sizeof(*afap), M_TEMP, M_WAITOK);
255 	error = sooptcopyin(sopt, afap, sizeof *afap, sizeof *afap);
256 	afap->af_name[sizeof(afap->af_name)-1] = '\0';
257 	afap->af_arg[sizeof(afap->af_arg)-1] = '\0';
258 	if (error) {
259 		free(afap, M_TEMP);
260 		return (error);
261 	}
262 	afp = accept_filt_get(afap->af_name);
263 	if (afp == NULL) {
264 		free(afap, M_TEMP);
265 		return (ENOENT);
266 	}
267 	if (afp->accf_create != NULL && afap->af_name[0] != '\0') {
268 		size_t len = strlen(afap->af_name) + 1;
269 		accept_filter_str = malloc(len, M_ACCF, M_WAITOK);
270 		strcpy(accept_filter_str, afap->af_name);
271 	}
272 
273 	/*
274 	 * Require a listen socket; don't try to replace an existing filter
275 	 * without first removing it.
276 	 */
277 	SOCK_LOCK(so);
278 	if ((so->so_options & SO_ACCEPTCONN) == 0 ||
279 	    so->sol_accept_filter != NULL) {
280 		error = EINVAL;
281 		goto out;
282 	}
283 
284 	/*
285 	 * Invoke the accf_create() method of the filter if required.  The
286 	 * socket mutex is held over this call, so create methods for filters
287 	 * can't block.
288 	 */
289 	if (afp->accf_create != NULL) {
290 		accept_filter_arg = afp->accf_create(so, afap->af_arg);
291 		if (accept_filter_arg == NULL) {
292 			error = EINVAL;
293 			goto out;
294 		}
295 	}
296 	so->sol_accept_filter = afp;
297 	so->sol_accept_filter_arg = accept_filter_arg;
298 	so->sol_accept_filter_str = accept_filter_str;
299 	so->so_options |= SO_ACCEPTFILTER;
300 out:
301 	SOCK_UNLOCK(so);
302 	if (accept_filter_str != NULL)
303 		free(accept_filter_str, M_ACCF);
304 	free(afap, M_TEMP);
305 	return (error);
306 }
307