xref: /freebsd/sys/kern/uipc_accf.c (revision 19307b86d31b7dea4bb5c3f7e233ee0e59049258)
19454b2d8SWarner Losh /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
38a36da99SPedro F. Giffuni  *
434b94e8bSAlfred Perlstein  * Copyright (c) 2000 Paycounter, Inc.
564c23807SRobert Watson  * Copyright (c) 2005 Robert N. M. Watson
634b94e8bSAlfred Perlstein  * Author: Alfred Perlstein <alfred@paycounter.com>, <alfred@FreeBSD.org>
7a79b7128SAlfred Perlstein  * All rights reserved.
8a79b7128SAlfred Perlstein  *
9a79b7128SAlfred Perlstein  * Redistribution and use in source and binary forms, with or without
10a79b7128SAlfred Perlstein  * modification, are permitted provided that the following conditions
11a79b7128SAlfred Perlstein  * are met:
12a79b7128SAlfred Perlstein  * 1. Redistributions of source code must retain the above copyright
13a79b7128SAlfred Perlstein  *    notice, this list of conditions and the following disclaimer.
14a79b7128SAlfred Perlstein  * 2. Redistributions in binary form must reproduce the above copyright
15a79b7128SAlfred Perlstein  *    notice, this list of conditions and the following disclaimer in the
16a79b7128SAlfred Perlstein  *    documentation and/or other materials provided with the distribution.
17a79b7128SAlfred Perlstein  *
18a79b7128SAlfred Perlstein  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19a79b7128SAlfred Perlstein  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20a79b7128SAlfred Perlstein  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21a79b7128SAlfred Perlstein  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22a79b7128SAlfred Perlstein  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23a79b7128SAlfred Perlstein  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24a79b7128SAlfred Perlstein  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25a79b7128SAlfred Perlstein  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26a79b7128SAlfred Perlstein  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27a79b7128SAlfred Perlstein  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28a79b7128SAlfred Perlstein  * SUCH DAMAGE.
29a79b7128SAlfred Perlstein  */
30a79b7128SAlfred Perlstein 
31677b542eSDavid E. O'Brien #include <sys/cdefs.h>
32a79b7128SAlfred Perlstein #define ACCEPT_FILTER_MOD
33a79b7128SAlfred Perlstein 
345b86eac4SJesper Skriver #include "opt_param.h"
35a79b7128SAlfred Perlstein #include <sys/param.h>
36a79b7128SAlfred Perlstein #include <sys/systm.h>
37a79b7128SAlfred Perlstein #include <sys/domain.h>
38a79b7128SAlfred Perlstein #include <sys/kernel.h>
39d087080cSRobert Watson #include <sys/lock.h>
40a79b7128SAlfred Perlstein #include <sys/malloc.h>
41a79b7128SAlfred Perlstein #include <sys/mbuf.h>
4241ee9f1cSPoul-Henning Kamp #include <sys/module.h>
43d087080cSRobert Watson #include <sys/mutex.h>
44a79b7128SAlfred Perlstein #include <sys/protosw.h>
4534b94e8bSAlfred Perlstein #include <sys/sysctl.h>
46a79b7128SAlfred Perlstein #include <sys/socket.h>
47a79b7128SAlfred Perlstein #include <sys/socketvar.h>
48a79b7128SAlfred Perlstein #include <sys/queue.h>
49a79b7128SAlfred Perlstein 
50d087080cSRobert Watson static struct mtx accept_filter_mtx;
51d087080cSRobert Watson MTX_SYSINIT(accept_filter, &accept_filter_mtx, "accept_filter_mtx",
52d087080cSRobert Watson 	MTX_DEF);
53d087080cSRobert Watson #define	ACCEPT_FILTER_LOCK()	mtx_lock(&accept_filter_mtx)
54d087080cSRobert Watson #define	ACCEPT_FILTER_UNLOCK()	mtx_unlock(&accept_filter_mtx)
55d087080cSRobert Watson 
56a79b7128SAlfred Perlstein static SLIST_HEAD(, accept_filter) accept_filtlsthd =
5713e403fdSAntoine Brodin 	SLIST_HEAD_INITIALIZER(accept_filtlsthd);
58a79b7128SAlfred Perlstein 
59a79b7128SAlfred Perlstein MALLOC_DEFINE(M_ACCF, "accf", "accept filter data");
60a79b7128SAlfred Perlstein 
6134b94e8bSAlfred Perlstein static int unloadable = 0;
6234b94e8bSAlfred Perlstein 
637029da5cSPawel Biernacki SYSCTL_NODE(_net, OID_AUTO, accf, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
647029da5cSPawel Biernacki     "Accept filters");
651e0a021eSMarcel Moolenaar SYSCTL_INT(_net_accf, OID_AUTO, unloadable, CTLFLAG_RW, &unloadable, 0,
6634b94e8bSAlfred Perlstein 	"Allow unload of accept filters (not recommended)");
6734b94e8bSAlfred Perlstein 
68a79b7128SAlfred Perlstein /*
691ed716a1SRobert Watson  * Must be passed a malloc'd structure so we don't explode if the kld is
701ed716a1SRobert Watson  * unloaded, we leak the struct on deallocation to deal with this, but if a
711ed716a1SRobert Watson  * filter is loaded with the same name as a leaked one we re-use the entry.
72a79b7128SAlfred Perlstein  */
73a79b7128SAlfred Perlstein int
accept_filt_add(struct accept_filter * filt)74a79b7128SAlfred Perlstein accept_filt_add(struct accept_filter *filt)
75a79b7128SAlfred Perlstein {
76a79b7128SAlfred Perlstein 	struct accept_filter *p;
77a79b7128SAlfred Perlstein 
78d087080cSRobert Watson 	ACCEPT_FILTER_LOCK();
79a79b7128SAlfred Perlstein 	SLIST_FOREACH(p, &accept_filtlsthd, accf_next)
80a79b7128SAlfred Perlstein 		if (strcmp(p->accf_name, filt->accf_name) == 0)  {
81a79b7128SAlfred Perlstein 			if (p->accf_callback != NULL) {
82d087080cSRobert Watson 				ACCEPT_FILTER_UNLOCK();
83a79b7128SAlfred Perlstein 				return (EEXIST);
84a79b7128SAlfred Perlstein 			} else {
85a79b7128SAlfred Perlstein 				p->accf_callback = filt->accf_callback;
86d087080cSRobert Watson 				ACCEPT_FILTER_UNLOCK();
871ede983cSDag-Erling Smørgrav 				free(filt, M_ACCF);
88a79b7128SAlfred Perlstein 				return (0);
89a79b7128SAlfred Perlstein 			}
90a79b7128SAlfred Perlstein 		}
91a79b7128SAlfred Perlstein 
92a79b7128SAlfred Perlstein 	if (p == NULL)
93a79b7128SAlfred Perlstein 		SLIST_INSERT_HEAD(&accept_filtlsthd, filt, accf_next);
94d087080cSRobert Watson 	ACCEPT_FILTER_UNLOCK();
95a79b7128SAlfred Perlstein 	return (0);
96a79b7128SAlfred Perlstein }
97a79b7128SAlfred Perlstein 
98a79b7128SAlfred Perlstein int
accept_filt_del(char * name)99a79b7128SAlfred Perlstein accept_filt_del(char *name)
100a79b7128SAlfred Perlstein {
101a79b7128SAlfred Perlstein 	struct accept_filter *p;
102a79b7128SAlfred Perlstein 
103a79b7128SAlfred Perlstein 	p = accept_filt_get(name);
104a79b7128SAlfred Perlstein 	if (p == NULL)
105a79b7128SAlfred Perlstein 		return (ENOENT);
106a79b7128SAlfred Perlstein 
107a79b7128SAlfred Perlstein 	p->accf_callback = NULL;
108a79b7128SAlfred Perlstein 	return (0);
109a79b7128SAlfred Perlstein }
110a79b7128SAlfred Perlstein 
111a79b7128SAlfred Perlstein struct accept_filter *
accept_filt_get(char * name)112a79b7128SAlfred Perlstein accept_filt_get(char *name)
113a79b7128SAlfred Perlstein {
114a79b7128SAlfred Perlstein 	struct accept_filter *p;
115a79b7128SAlfred Perlstein 
116d087080cSRobert Watson 	ACCEPT_FILTER_LOCK();
117a79b7128SAlfred Perlstein 	SLIST_FOREACH(p, &accept_filtlsthd, accf_next)
118a79b7128SAlfred Perlstein 		if (strcmp(p->accf_name, name) == 0)
119d087080cSRobert Watson 			break;
120d087080cSRobert Watson 	ACCEPT_FILTER_UNLOCK();
121a79b7128SAlfred Perlstein 
122d087080cSRobert Watson 	return (p);
123a79b7128SAlfred Perlstein }
124a79b7128SAlfred Perlstein 
125a79b7128SAlfred Perlstein int
accept_filt_generic_mod_event(module_t mod,int event,void * data)126a79b7128SAlfred Perlstein accept_filt_generic_mod_event(module_t mod, int event, void *data)
127a79b7128SAlfred Perlstein {
128a79b7128SAlfred Perlstein 	struct accept_filter *p;
129a79b7128SAlfred Perlstein 	struct accept_filter *accfp = (struct accept_filter *) data;
130d087080cSRobert Watson 	int error;
131a79b7128SAlfred Perlstein 
132a79b7128SAlfred Perlstein 	switch (event) {
133a79b7128SAlfred Perlstein 	case MOD_LOAD:
134810951ddSGleb Smirnoff 		p = malloc(sizeof(*p), M_ACCF, M_WAITOK);
135a79b7128SAlfred Perlstein 		bcopy(accfp, p, sizeof(*p));
136a79b7128SAlfred Perlstein 		error = accept_filt_add(p);
137a79b7128SAlfred Perlstein 		break;
138a79b7128SAlfred Perlstein 
139a79b7128SAlfred Perlstein 	case MOD_UNLOAD:
14085f5e7f0SAlfred Perlstein 		/*
1411ed716a1SRobert Watson 		 * Do not support unloading yet. we don't keep track of
1421ed716a1SRobert Watson 		 * refcounts and unloading an accept filter callback and then
1431ed716a1SRobert Watson 		 * having it called is a bad thing.  A simple fix would be to
1441ed716a1SRobert Watson 		 * track the refcount in the struct accept_filter.
14585f5e7f0SAlfred Perlstein 		 */
14634b94e8bSAlfred Perlstein 		if (unloadable != 0) {
147a79b7128SAlfred Perlstein 			error = accept_filt_del(accfp->accf_name);
14834b94e8bSAlfred Perlstein 		} else
14985f5e7f0SAlfred Perlstein 			error = EOPNOTSUPP;
150a79b7128SAlfred Perlstein 		break;
151a79b7128SAlfred Perlstein 
152a79b7128SAlfred Perlstein 	case MOD_SHUTDOWN:
153a79b7128SAlfred Perlstein 		error = 0;
154a79b7128SAlfred Perlstein 		break;
155a79b7128SAlfred Perlstein 
156a79b7128SAlfred Perlstein 	default:
157a79b7128SAlfred Perlstein 		error = EOPNOTSUPP;
158a79b7128SAlfred Perlstein 		break;
159a79b7128SAlfred Perlstein 	}
160a79b7128SAlfred Perlstein 
161a79b7128SAlfred Perlstein 	return (error);
162a79b7128SAlfred Perlstein }
16378e43644SRobert Watson 
16478e43644SRobert Watson int
accept_filt_getopt(struct socket * so,struct sockopt * sopt)165971af2a3SGleb Smirnoff accept_filt_getopt(struct socket *so, struct sockopt *sopt)
166a59f81d2SRobert Watson {
167a59f81d2SRobert Watson 	struct accept_filter_arg *afap;
168a59f81d2SRobert Watson 	int error;
169a59f81d2SRobert Watson 
170a59f81d2SRobert Watson 	error = 0;
171810951ddSGleb Smirnoff 	afap = malloc(sizeof(*afap), M_TEMP, M_WAITOK | M_ZERO);
172a59f81d2SRobert Watson 	SOCK_LOCK(so);
173f4bb1869SMark Johnston 	if (!SOLISTENING(so)) {
174a59f81d2SRobert Watson 		error = EINVAL;
175a59f81d2SRobert Watson 		goto out;
176a59f81d2SRobert Watson 	}
177779f106aSGleb Smirnoff 	if (so->sol_accept_filter == NULL) {
178922a5d9cSMaxim Konovalov 		error = EINVAL;
179a59f81d2SRobert Watson 		goto out;
180922a5d9cSMaxim Konovalov 	}
181779f106aSGleb Smirnoff 	strcpy(afap->af_name, so->sol_accept_filter->accf_name);
182779f106aSGleb Smirnoff 	if (so->sol_accept_filter_str != NULL)
183779f106aSGleb Smirnoff 		strcpy(afap->af_arg, so->sol_accept_filter_str);
184a59f81d2SRobert Watson out:
185a59f81d2SRobert Watson 	SOCK_UNLOCK(so);
186a59f81d2SRobert Watson 	if (error == 0)
187a59f81d2SRobert Watson 		error = sooptcopyout(sopt, afap, sizeof(*afap));
1881ede983cSDag-Erling Smørgrav 	free(afap, M_TEMP);
189a59f81d2SRobert Watson 	return (error);
190a59f81d2SRobert Watson }
191a59f81d2SRobert Watson 
192a59f81d2SRobert Watson int
accept_filt_setopt(struct socket * so,struct sockopt * sopt)193971af2a3SGleb Smirnoff accept_filt_setopt(struct socket *so, struct sockopt *sopt)
19478e43644SRobert Watson {
19578e43644SRobert Watson 	struct accept_filter_arg *afap;
19678e43644SRobert Watson 	struct accept_filter *afp;
197779f106aSGleb Smirnoff 	char *accept_filter_str = NULL;
198779f106aSGleb Smirnoff 	void *accept_filter_arg = NULL;
199779f106aSGleb Smirnoff 	int error;
20078e43644SRobert Watson 
20164c23807SRobert Watson 	/*
20264c23807SRobert Watson 	 * Handle the simple delete case first.
20364c23807SRobert Watson 	 */
204922a5d9cSMaxim Konovalov 	if (sopt == NULL || sopt->sopt_val == NULL) {
205779f106aSGleb Smirnoff 		struct socket *sp, *sp1;
206779f106aSGleb Smirnoff 		int wakeup;
207779f106aSGleb Smirnoff 
20864c23807SRobert Watson 		SOCK_LOCK(so);
209f4bb1869SMark Johnston 		if (!SOLISTENING(so)) {
21064c23807SRobert Watson 			SOCK_UNLOCK(so);
21164c23807SRobert Watson 			return (EINVAL);
21264c23807SRobert Watson 		}
213779f106aSGleb Smirnoff 		if (so->sol_accept_filter == NULL) {
21464c23807SRobert Watson 			SOCK_UNLOCK(so);
21564c23807SRobert Watson 			return (0);
21664c23807SRobert Watson 		}
217779f106aSGleb Smirnoff 		if (so->sol_accept_filter->accf_destroy != NULL)
218779f106aSGleb Smirnoff 			so->sol_accept_filter->accf_destroy(so);
219779f106aSGleb Smirnoff 		if (so->sol_accept_filter_str != NULL)
220779f106aSGleb Smirnoff 			free(so->sol_accept_filter_str, M_ACCF);
221779f106aSGleb Smirnoff 		so->sol_accept_filter = NULL;
222779f106aSGleb Smirnoff 		so->sol_accept_filter_arg = NULL;
223779f106aSGleb Smirnoff 		so->sol_accept_filter_str = NULL;
224779f106aSGleb Smirnoff 		so->so_options &= ~SO_ACCEPTFILTER;
225779f106aSGleb Smirnoff 
226779f106aSGleb Smirnoff 		/*
227779f106aSGleb Smirnoff 		 * Move from incomplete queue to complete only those
228779f106aSGleb Smirnoff 		 * connections, that are blocked by us.
229779f106aSGleb Smirnoff 		 */
230779f106aSGleb Smirnoff 		wakeup = 0;
231779f106aSGleb Smirnoff 		TAILQ_FOREACH_SAFE(sp, &so->sol_incomp, so_list, sp1) {
232779f106aSGleb Smirnoff 			SOCK_LOCK(sp);
233779f106aSGleb Smirnoff 			if (sp->so_options & SO_ACCEPTFILTER) {
234779f106aSGleb Smirnoff 				TAILQ_REMOVE(&so->sol_incomp, sp, so_list);
235779f106aSGleb Smirnoff 				TAILQ_INSERT_TAIL(&so->sol_comp, sp, so_list);
236779f106aSGleb Smirnoff 				sp->so_qstate = SQ_COMP;
237779f106aSGleb Smirnoff 				sp->so_options &= ~SO_ACCEPTFILTER;
238779f106aSGleb Smirnoff 				so->sol_incqlen--;
239779f106aSGleb Smirnoff 				so->sol_qlen++;
240779f106aSGleb Smirnoff 				wakeup = 1;
241779f106aSGleb Smirnoff 			}
242779f106aSGleb Smirnoff 			SOCK_UNLOCK(sp);
243779f106aSGleb Smirnoff 		}
244779f106aSGleb Smirnoff 		if (wakeup)
245779f106aSGleb Smirnoff 			solisten_wakeup(so);  /* unlocks */
246779f106aSGleb Smirnoff 		else
247779f106aSGleb Smirnoff 			SOLISTEN_UNLOCK(so);
248779f106aSGleb Smirnoff 		return (0);
249779f106aSGleb Smirnoff 	}
25064c23807SRobert Watson 
25178e43644SRobert Watson 	/*
25292081a83SRobert Watson 	 * Pre-allocate any memory we may need later to avoid blocking at
25392081a83SRobert Watson 	 * untimely moments.  This does not optimize for invalid arguments.
25478e43644SRobert Watson 	 */
255810951ddSGleb Smirnoff 	afap = malloc(sizeof(*afap), M_TEMP, M_WAITOK);
25678e43644SRobert Watson 	error = sooptcopyin(sopt, afap, sizeof *afap, sizeof *afap);
25778e43644SRobert Watson 	afap->af_name[sizeof(afap->af_name)-1] = '\0';
25878e43644SRobert Watson 	afap->af_arg[sizeof(afap->af_arg)-1] = '\0';
25978e43644SRobert Watson 	if (error) {
2601ede983cSDag-Erling Smørgrav 		free(afap, M_TEMP);
26178e43644SRobert Watson 		return (error);
26278e43644SRobert Watson 	}
26378e43644SRobert Watson 	afp = accept_filt_get(afap->af_name);
26478e43644SRobert Watson 	if (afp == NULL) {
2651ede983cSDag-Erling Smørgrav 		free(afap, M_TEMP);
26678e43644SRobert Watson 		return (ENOENT);
26778e43644SRobert Watson 	}
26878e43644SRobert Watson 	if (afp->accf_create != NULL && afap->af_name[0] != '\0') {
2692a059700SEd Maste 		size_t len = strlen(afap->af_name) + 1;
270779f106aSGleb Smirnoff 		accept_filter_str = malloc(len, M_ACCF, M_WAITOK);
271779f106aSGleb Smirnoff 		strcpy(accept_filter_str, afap->af_name);
27278e43644SRobert Watson 	}
27378e43644SRobert Watson 
27492081a83SRobert Watson 	/*
27592081a83SRobert Watson 	 * Require a listen socket; don't try to replace an existing filter
27692081a83SRobert Watson 	 * without first removing it.
27792081a83SRobert Watson 	 */
27878e43644SRobert Watson 	SOCK_LOCK(so);
279*19307b86SGleb Smirnoff 	if (__predict_false(!SOLISTENING(so))) {
28078e43644SRobert Watson 		error = EINVAL;
28178e43644SRobert Watson 		goto out;
28278e43644SRobert Watson 	}
283*19307b86SGleb Smirnoff 	if (__predict_false(so->sol_accept_filter != NULL)) {
284*19307b86SGleb Smirnoff 		error = EBUSY;
285*19307b86SGleb Smirnoff 		goto out;
286*19307b86SGleb Smirnoff 	}
28792081a83SRobert Watson 
28878e43644SRobert Watson 	/*
28992081a83SRobert Watson 	 * Invoke the accf_create() method of the filter if required.  The
29092081a83SRobert Watson 	 * socket mutex is held over this call, so create methods for filters
29192081a83SRobert Watson 	 * can't block.
29278e43644SRobert Watson 	 */
29378e43644SRobert Watson 	if (afp->accf_create != NULL) {
294779f106aSGleb Smirnoff 		accept_filter_arg = afp->accf_create(so, afap->af_arg);
295779f106aSGleb Smirnoff 		if (accept_filter_arg == NULL) {
29678e43644SRobert Watson 			error = EINVAL;
29778e43644SRobert Watson 			goto out;
29878e43644SRobert Watson 		}
29978e43644SRobert Watson 	}
300779f106aSGleb Smirnoff 	so->sol_accept_filter = afp;
301779f106aSGleb Smirnoff 	so->sol_accept_filter_arg = accept_filter_arg;
302779f106aSGleb Smirnoff 	so->sol_accept_filter_str = accept_filter_str;
303653a437cSMark Johnston 	accept_filter_str = NULL;
30478e43644SRobert Watson 	so->so_options |= SO_ACCEPTFILTER;
30578e43644SRobert Watson out:
30678e43644SRobert Watson 	SOCK_UNLOCK(so);
307779f106aSGleb Smirnoff 	if (accept_filter_str != NULL)
308779f106aSGleb Smirnoff 		free(accept_filter_str, M_ACCF);
3091ede983cSDag-Erling Smørgrav 	free(afap, M_TEMP);
31078e43644SRobert Watson 	return (error);
31178e43644SRobert Watson }
312