1/*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2000 Paycounter, Inc.
5 * Copyright (c) 2005 Robert N. M. Watson
6 * Author: Alfred Perlstein <alfred@paycounter.com>, <alfred@FreeBSD.org>
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#include <sys/cdefs.h>
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
50static struct mtx accept_filter_mtx;
51MTX_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
56static SLIST_HEAD(, accept_filter) accept_filtlsthd =
57	SLIST_HEAD_INITIALIZER(accept_filtlsthd);
58
59MALLOC_DEFINE(M_ACCF, "accf", "accept filter data");
60
61static int unloadable = 0;
62
63SYSCTL_NODE(_net, OID_AUTO, accf, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
64    "Accept filters");
65SYSCTL_INT(_net_accf, OID_AUTO, unloadable, CTLFLAG_RW, &unloadable, 0,
66	"Allow unload of accept filters (not recommended)");
67
68/*
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.
72 */
73int
74accept_filt_add(struct accept_filter *filt)
75{
76	struct accept_filter *p;
77
78	ACCEPT_FILTER_LOCK();
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();
83				return (EEXIST);
84			} else {
85				p->accf_callback = filt->accf_callback;
86				ACCEPT_FILTER_UNLOCK();
87				free(filt, M_ACCF);
88				return (0);
89			}
90		}
91
92	if (p == NULL)
93		SLIST_INSERT_HEAD(&accept_filtlsthd, filt, accf_next);
94	ACCEPT_FILTER_UNLOCK();
95	return (0);
96}
97
98int
99accept_filt_del(char *name)
100{
101	struct accept_filter *p;
102
103	p = accept_filt_get(name);
104	if (p == NULL)
105		return (ENOENT);
106
107	p->accf_callback = NULL;
108	return (0);
109}
110
111struct accept_filter *
112accept_filt_get(char *name)
113{
114	struct accept_filter *p;
115
116	ACCEPT_FILTER_LOCK();
117	SLIST_FOREACH(p, &accept_filtlsthd, accf_next)
118		if (strcmp(p->accf_name, name) == 0)
119			break;
120	ACCEPT_FILTER_UNLOCK();
121
122	return (p);
123}
124
125int
126accept_filt_generic_mod_event(module_t mod, int event, void *data)
127{
128	struct accept_filter *p;
129	struct accept_filter *accfp = (struct accept_filter *) data;
130	int error;
131
132	switch (event) {
133	case MOD_LOAD:
134		p = malloc(sizeof(*p), M_ACCF, M_WAITOK);
135		bcopy(accfp, p, sizeof(*p));
136		error = accept_filt_add(p);
137		break;
138
139	case MOD_UNLOAD:
140		/*
141		 * Do not support unloading yet. we don't keep track of
142		 * refcounts and unloading an accept filter callback and then
143		 * having it called is a bad thing.  A simple fix would be to
144		 * track the refcount in the struct accept_filter.
145		 */
146		if (unloadable != 0) {
147			error = accept_filt_del(accfp->accf_name);
148		} else
149			error = EOPNOTSUPP;
150		break;
151
152	case MOD_SHUTDOWN:
153		error = 0;
154		break;
155
156	default:
157		error = EOPNOTSUPP;
158		break;
159	}
160
161	return (error);
162}
163
164int
165accept_filt_getopt(struct socket *so, struct sockopt *sopt)
166{
167	struct accept_filter_arg *afap;
168	int error;
169
170	error = 0;
171	afap = malloc(sizeof(*afap), M_TEMP, M_WAITOK | M_ZERO);
172	SOCK_LOCK(so);
173	if (!SOLISTENING(so)) {
174		error = EINVAL;
175		goto out;
176	}
177	if (so->sol_accept_filter == NULL) {
178		error = EINVAL;
179		goto out;
180	}
181	strcpy(afap->af_name, so->sol_accept_filter->accf_name);
182	if (so->sol_accept_filter_str != NULL)
183		strcpy(afap->af_arg, so->sol_accept_filter_str);
184out:
185	SOCK_UNLOCK(so);
186	if (error == 0)
187		error = sooptcopyout(sopt, afap, sizeof(*afap));
188	free(afap, M_TEMP);
189	return (error);
190}
191
192int
193accept_filt_setopt(struct socket *so, struct sockopt *sopt)
194{
195	struct accept_filter_arg *afap;
196	struct accept_filter *afp;
197	char *accept_filter_str = NULL;
198	void *accept_filter_arg = NULL;
199	int error;
200
201	/*
202	 * Handle the simple delete case first.
203	 */
204	if (sopt == NULL || sopt->sopt_val == NULL) {
205		struct socket *sp, *sp1;
206		int wakeup;
207
208		SOCK_LOCK(so);
209		if (!SOLISTENING(so)) {
210			SOCK_UNLOCK(so);
211			return (EINVAL);
212		}
213		if (so->sol_accept_filter == NULL) {
214			SOCK_UNLOCK(so);
215			return (0);
216		}
217		if (so->sol_accept_filter->accf_destroy != NULL)
218			so->sol_accept_filter->accf_destroy(so);
219		if (so->sol_accept_filter_str != NULL)
220			free(so->sol_accept_filter_str, M_ACCF);
221		so->sol_accept_filter = NULL;
222		so->sol_accept_filter_arg = NULL;
223		so->sol_accept_filter_str = NULL;
224		so->so_options &= ~SO_ACCEPTFILTER;
225
226		/*
227		 * Move from incomplete queue to complete only those
228		 * connections, that are blocked by us.
229		 */
230		wakeup = 0;
231		TAILQ_FOREACH_SAFE(sp, &so->sol_incomp, so_list, sp1) {
232			SOCK_LOCK(sp);
233			if (sp->so_options & SO_ACCEPTFILTER) {
234				TAILQ_REMOVE(&so->sol_incomp, sp, so_list);
235				TAILQ_INSERT_TAIL(&so->sol_comp, sp, so_list);
236				sp->so_qstate = SQ_COMP;
237				sp->so_options &= ~SO_ACCEPTFILTER;
238				so->sol_incqlen--;
239				so->sol_qlen++;
240				wakeup = 1;
241			}
242			SOCK_UNLOCK(sp);
243		}
244		if (wakeup)
245			solisten_wakeup(so);  /* unlocks */
246		else
247			SOLISTEN_UNLOCK(so);
248		return (0);
249	}
250
251	/*
252	 * Pre-allocate any memory we may need later to avoid blocking at
253	 * untimely moments.  This does not optimize for invalid arguments.
254	 */
255	afap = malloc(sizeof(*afap), M_TEMP, M_WAITOK);
256	error = sooptcopyin(sopt, afap, sizeof *afap, sizeof *afap);
257	afap->af_name[sizeof(afap->af_name)-1] = '\0';
258	afap->af_arg[sizeof(afap->af_arg)-1] = '\0';
259	if (error) {
260		free(afap, M_TEMP);
261		return (error);
262	}
263	afp = accept_filt_get(afap->af_name);
264	if (afp == NULL) {
265		free(afap, M_TEMP);
266		return (ENOENT);
267	}
268	if (afp->accf_create != NULL && afap->af_name[0] != '\0') {
269		size_t len = strlen(afap->af_name) + 1;
270		accept_filter_str = malloc(len, M_ACCF, M_WAITOK);
271		strcpy(accept_filter_str, afap->af_name);
272	}
273
274	/*
275	 * Require a listen socket; don't try to replace an existing filter
276	 * without first removing it.
277	 */
278	SOCK_LOCK(so);
279	if (__predict_false(!SOLISTENING(so))) {
280		error = EINVAL;
281		goto out;
282	}
283	if (__predict_false(so->sol_accept_filter != NULL)) {
284		error = EBUSY;
285		goto out;
286	}
287
288	/*
289	 * Invoke the accf_create() method of the filter if required.  The
290	 * socket mutex is held over this call, so create methods for filters
291	 * can't block.
292	 */
293	if (afp->accf_create != NULL) {
294		accept_filter_arg = afp->accf_create(so, afap->af_arg);
295		if (accept_filter_arg == NULL) {
296			error = EINVAL;
297			goto out;
298		}
299	}
300	so->sol_accept_filter = afp;
301	so->sol_accept_filter_arg = accept_filter_arg;
302	so->sol_accept_filter_str = accept_filter_str;
303	accept_filter_str = NULL;
304	so->so_options |= SO_ACCEPTFILTER;
305out:
306	SOCK_UNLOCK(so);
307	if (accept_filter_str != NULL)
308		free(accept_filter_str, M_ACCF);
309	free(afap, M_TEMP);
310	return (error);
311}
312