138889Sjdp/*-
238889Sjdp * Copyright (c) 2000 Paycounter, Inc.
360484Sobrien * Copyright (c) 2005 Robert N. M. Watson
460484Sobrien * Author: Alfred Perlstein <alfred@paycounter.com>, <alfred@FreeBSD.org>
560484Sobrien * All rights reserved.
638889Sjdp *
738889Sjdp * Redistribution and use in source and binary forms, with or without
860484Sobrien * modification, are permitted provided that the following conditions
938889Sjdp * are met:
1038889Sjdp * 1. Redistributions of source code must retain the above copyright
1138889Sjdp *    notice, this list of conditions and the following disclaimer.
1260484Sobrien * 2. Redistributions in binary form must reproduce the above copyright
1360484Sobrien *    notice, this list of conditions and the following disclaimer in the
1438889Sjdp *    documentation and/or other materials provided with the distribution.
1577298Sobrien *
1677298Sobrien * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1777298Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1860484Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1938889Sjdp * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2038889Sjdp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2138889Sjdp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2238889Sjdp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2338889Sjdp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2438889Sjdp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2538889Sjdp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2638889Sjdp * SUCH DAMAGE.
2738889Sjdp */
2838889Sjdp
2938889Sjdp#include <sys/cdefs.h>
3038889Sjdp__FBSDID("$FreeBSD$");
3138889Sjdp
3238889Sjdp#define ACCEPT_FILTER_MOD
3338889Sjdp
3438889Sjdp#include "opt_param.h"
3538889Sjdp#include <sys/param.h>
3638889Sjdp#include <sys/systm.h>
3738889Sjdp#include <sys/domain.h>
3838889Sjdp#include <sys/kernel.h>
3938889Sjdp#include <sys/lock.h>
4038889Sjdp#include <sys/malloc.h>
4138889Sjdp#include <sys/mbuf.h>
4238889Sjdp#include <sys/module.h>
4338889Sjdp#include <sys/mutex.h>
4460484Sobrien#include <sys/protosw.h>
4577298Sobrien#include <sys/sysctl.h>
4638889Sjdp#include <sys/socket.h>
4760484Sobrien#include <sys/socketvar.h>
4860484Sobrien#include <sys/queue.h>
4938889Sjdp
5038889Sjdpstatic struct mtx accept_filter_mtx;
5138889SjdpMTX_SYSINIT(accept_filter, &accept_filter_mtx, "accept_filter_mtx",
5277298Sobrien	MTX_DEF);
5360484Sobrien#define	ACCEPT_FILTER_LOCK()	mtx_lock(&accept_filter_mtx)
5438889Sjdp#define	ACCEPT_FILTER_UNLOCK()	mtx_unlock(&accept_filter_mtx)
5538889Sjdp
5638889Sjdpstatic SLIST_HEAD(, accept_filter) accept_filtlsthd =
5738889Sjdp	SLIST_HEAD_INITIALIZER(accept_filtlsthd);
5877298Sobrien
5938889SjdpMALLOC_DEFINE(M_ACCF, "accf", "accept filter data");
6038889Sjdp
6160484Sobrienstatic int unloadable = 0;
6238889Sjdp
6338889SjdpSYSCTL_DECL(_net_inet);	/* XXX: some header should do this for me */
6438889SjdpSYSCTL_NODE(_net_inet, OID_AUTO, accf, CTLFLAG_RW, 0, "Accept filters");
6538889SjdpSYSCTL_INT(_net_inet_accf, OID_AUTO, unloadable, CTLFLAG_RW, &unloadable, 0,
6660484Sobrien	"Allow unload of accept filters (not recommended)");
6738889Sjdp
6838889Sjdp/*
6938889Sjdp * Must be passed a malloc'd structure so we don't explode if the kld is
7038889Sjdp * unloaded, we leak the struct on deallocation to deal with this, but if a
7138889Sjdp * filter is loaded with the same name as a leaked one we re-use the entry.
7277298Sobrien */
7360484Sobrienint
7438889Sjdpaccept_filt_add(struct accept_filter *filt)
7538889Sjdp{
7638889Sjdp	struct accept_filter *p;
7738889Sjdp
7838889Sjdp	ACCEPT_FILTER_LOCK();
7938889Sjdp	SLIST_FOREACH(p, &accept_filtlsthd, accf_next)
8078828Sobrien		if (strcmp(p->accf_name, filt->accf_name) == 0)  {
8138889Sjdp			if (p->accf_callback != NULL) {
8238889Sjdp				ACCEPT_FILTER_UNLOCK();
8338889Sjdp				return (EEXIST);
8438889Sjdp			} else {
8538889Sjdp				p->accf_callback = filt->accf_callback;
8638889Sjdp				ACCEPT_FILTER_UNLOCK();
8738889Sjdp				free(filt, M_ACCF);
8838889Sjdp				return (0);
8938889Sjdp			}
9038889Sjdp		}
9138889Sjdp
9238889Sjdp	if (p == NULL)
9338889Sjdp		SLIST_INSERT_HEAD(&accept_filtlsthd, filt, accf_next);
9438889Sjdp	ACCEPT_FILTER_UNLOCK();
9538889Sjdp	return (0);
9638889Sjdp}
9738889Sjdp
9838889Sjdpint
9938889Sjdpaccept_filt_del(char *name)
10038889Sjdp{
10138889Sjdp	struct accept_filter *p;
10238889Sjdp
10377298Sobrien	p = accept_filt_get(name);
10438889Sjdp	if (p == NULL)
10538889Sjdp		return (ENOENT);
10638889Sjdp
10738889Sjdp	p->accf_callback = NULL;
10838889Sjdp	return (0);
10938889Sjdp}
11077298Sobrien
11177298Sobrienstruct accept_filter *
11277298Sobrienaccept_filt_get(char *name)
11377298Sobrien{
11477298Sobrien	struct accept_filter *p;
11538889Sjdp
11638889Sjdp	ACCEPT_FILTER_LOCK();
11738889Sjdp	SLIST_FOREACH(p, &accept_filtlsthd, accf_next)
11838889Sjdp		if (strcmp(p->accf_name, name) == 0)
11938889Sjdp			break;
12038889Sjdp	ACCEPT_FILTER_UNLOCK();
12138889Sjdp
12238889Sjdp	return (p);
12338889Sjdp}
12438889Sjdp
12538889Sjdpint
12638889Sjdpaccept_filt_generic_mod_event(module_t mod, int event, void *data)
12738889Sjdp{
12838889Sjdp	struct accept_filter *p;
12938889Sjdp	struct accept_filter *accfp = (struct accept_filter *) data;
13038889Sjdp	int error;
13138889Sjdp
13238889Sjdp	switch (event) {
13338889Sjdp	case MOD_LOAD:
13460484Sobrien		p = malloc(sizeof(*p), M_ACCF,
13538889Sjdp		    M_WAITOK);
13677298Sobrien		bcopy(accfp, p, sizeof(*p));
13760484Sobrien		error = accept_filt_add(p);
13860484Sobrien		break;
13938889Sjdp
14060484Sobrien	case MOD_UNLOAD:
14160484Sobrien		/*
14260484Sobrien		 * Do not support unloading yet. we don't keep track of
14377298Sobrien		 * refcounts and unloading an accept filter callback and then
14460484Sobrien		 * having it called is a bad thing.  A simple fix would be to
14577298Sobrien		 * track the refcount in the struct accept_filter.
14677298Sobrien		 */
14777298Sobrien		if (unloadable != 0) {
14877298Sobrien			error = accept_filt_del(accfp->accf_name);
14960484Sobrien		} else
15060484Sobrien			error = EOPNOTSUPP;
15160484Sobrien		break;
15260484Sobrien
15360484Sobrien	case MOD_SHUTDOWN:
15438889Sjdp		error = 0;
15538889Sjdp		break;
15638889Sjdp
15738889Sjdp	default:
15838889Sjdp		error = EOPNOTSUPP;
15938889Sjdp		break;
16038889Sjdp	}
16138889Sjdp
16238889Sjdp	return (error);
16338889Sjdp}
16438889Sjdp
16560484Sobrienint
16638889Sjdpdo_getopt_accept_filter(struct socket *so, struct sockopt *sopt)
16738889Sjdp{
16838889Sjdp	struct accept_filter_arg *afap;
16938889Sjdp	int error;
17038889Sjdp
17138889Sjdp	error = 0;
17238889Sjdp	afap = malloc(sizeof(*afap), M_TEMP,
17338889Sjdp	    M_WAITOK | M_ZERO);
17438889Sjdp	SOCK_LOCK(so);
17538889Sjdp	if ((so->so_options & SO_ACCEPTCONN) == 0) {
17638889Sjdp		error = EINVAL;
17738889Sjdp		goto out;
17838889Sjdp	}
17938889Sjdp	if ((so->so_options & SO_ACCEPTFILTER) == 0) {
18038889Sjdp		error = EINVAL;
18138889Sjdp		goto out;
18238889Sjdp	}
18338889Sjdp	strcpy(afap->af_name, so->so_accf->so_accept_filter->accf_name);
18438889Sjdp	if (so->so_accf->so_accept_filter_str != NULL)
18538889Sjdp		strcpy(afap->af_arg, so->so_accf->so_accept_filter_str);
18638889Sjdpout:
18738889Sjdp	SOCK_UNLOCK(so);
18838889Sjdp	if (error == 0)
18938889Sjdp		error = sooptcopyout(sopt, afap, sizeof(*afap));
19038889Sjdp	free(afap, M_TEMP);
19138889Sjdp	return (error);
19260484Sobrien}
19338889Sjdp
19438889Sjdpint
19560484Sobriendo_setopt_accept_filter(struct socket *so, struct sockopt *sopt)
19660484Sobrien{
19738889Sjdp	struct accept_filter_arg *afap;
19838889Sjdp	struct accept_filter *afp;
19938889Sjdp	struct so_accf *newaf;
20038889Sjdp	int error = 0;
20138889Sjdp
20238889Sjdp	/*
20338889Sjdp	 * Handle the simple delete case first.
20438889Sjdp	 */
20538889Sjdp	if (sopt == NULL || sopt->sopt_val == NULL) {
20638889Sjdp		SOCK_LOCK(so);
20738889Sjdp		if ((so->so_options & SO_ACCEPTCONN) == 0) {
20838889Sjdp			SOCK_UNLOCK(so);
20938889Sjdp			return (EINVAL);
21038889Sjdp		}
21138889Sjdp		if (so->so_accf != NULL) {
21238889Sjdp			struct so_accf *af = so->so_accf;
21338889Sjdp			if (af->so_accept_filter != NULL &&
21438889Sjdp				af->so_accept_filter->accf_destroy != NULL) {
21538889Sjdp				af->so_accept_filter->accf_destroy(so);
21638889Sjdp			}
21738889Sjdp			if (af->so_accept_filter_str != NULL)
21838889Sjdp				free(af->so_accept_filter_str, M_ACCF);
21938889Sjdp			free(af, M_ACCF);
22038889Sjdp			so->so_accf = NULL;
22138889Sjdp		}
22238889Sjdp		so->so_options &= ~SO_ACCEPTFILTER;
22338889Sjdp		SOCK_UNLOCK(so);
22438889Sjdp		return (0);
22560484Sobrien	}
22677298Sobrien
22738889Sjdp	/*
22860484Sobrien	 * Pre-allocate any memory we may need later to avoid blocking at
22960484Sobrien	 * untimely moments.  This does not optimize for invalid arguments.
23038889Sjdp	 */
23138889Sjdp	afap = malloc(sizeof(*afap), M_TEMP,
23238889Sjdp	    M_WAITOK);
23377298Sobrien	error = sooptcopyin(sopt, afap, sizeof *afap, sizeof *afap);
23460484Sobrien	afap->af_name[sizeof(afap->af_name)-1] = '\0';
23538889Sjdp	afap->af_arg[sizeof(afap->af_arg)-1] = '\0';
23638889Sjdp	if (error) {
23738889Sjdp		free(afap, M_TEMP);
23838889Sjdp		return (error);
23977298Sobrien	}
24038889Sjdp	afp = accept_filt_get(afap->af_name);
24138889Sjdp	if (afp == NULL) {
24260484Sobrien		free(afap, M_TEMP);
24338889Sjdp		return (ENOENT);
24438889Sjdp	}
24538889Sjdp	/*
24638889Sjdp	 * Allocate the new accept filter instance storage.  We may
24760484Sobrien	 * have to free it again later if we fail to attach it.  If
24838889Sjdp	 * attached properly, 'newaf' is NULLed to avoid a free()
24938889Sjdp	 * while in use.
25038889Sjdp	 */
25138889Sjdp	newaf = malloc(sizeof(*newaf), M_ACCF, M_WAITOK |
25238889Sjdp	    M_ZERO);
25377298Sobrien	if (afp->accf_create != NULL && afap->af_name[0] != '\0') {
25460484Sobrien		int len = strlen(afap->af_name) + 1;
25538889Sjdp		newaf->so_accept_filter_str = malloc(len, M_ACCF,
25638889Sjdp		    M_WAITOK);
25738889Sjdp		strcpy(newaf->so_accept_filter_str, afap->af_name);
25838889Sjdp	}
25938889Sjdp
26038889Sjdp	/*
26138889Sjdp	 * Require a listen socket; don't try to replace an existing filter
26238889Sjdp	 * without first removing it.
26338889Sjdp	 */
26438889Sjdp	SOCK_LOCK(so);
26560484Sobrien	if (((so->so_options & SO_ACCEPTCONN) == 0) ||
26677298Sobrien	    (so->so_accf != NULL)) {
26738889Sjdp		error = EINVAL;
26860484Sobrien		goto out;
26960484Sobrien	}
27038889Sjdp
27138889Sjdp	/*
27238889Sjdp	 * Invoke the accf_create() method of the filter if required.  The
27377298Sobrien	 * socket mutex is held over this call, so create methods for filters
27460484Sobrien	 * can't block.
27538889Sjdp	 */
27638889Sjdp	if (afp->accf_create != NULL) {
27738889Sjdp		newaf->so_accept_filter_arg =
27838889Sjdp		    afp->accf_create(so, afap->af_arg);
27977298Sobrien		if (newaf->so_accept_filter_arg == NULL) {
28038889Sjdp			error = EINVAL;
28138889Sjdp			goto out;
28260484Sobrien		}
28338889Sjdp	}
28438889Sjdp	newaf->so_accept_filter = afp;
28538889Sjdp	so->so_accf = newaf;
28638889Sjdp	so->so_options |= SO_ACCEPTFILTER;
28760484Sobrien	newaf = NULL;
28838889Sjdpout:
28938889Sjdp	SOCK_UNLOCK(so);
29038889Sjdp	if (newaf != NULL) {
29138889Sjdp		if (newaf->so_accept_filter_str != NULL)
29238889Sjdp			free(newaf->so_accept_filter_str, M_ACCF);
29377298Sobrien		free(newaf, M_ACCF);
29460484Sobrien	}
29538889Sjdp	if (afap != NULL)
29638889Sjdp		free(afap, M_TEMP);
29738889Sjdp	return (error);
29838889Sjdp}
29938889Sjdp