Home | History | Annotate | Line # | Download | only in kern
uipc_accf.c revision 1.1
      1  1.1  tls /*-
      2  1.1  tls  * Copyright (c) 2000 Paycounter, Inc.
      3  1.1  tls  * Copyright (c) 2005 Robert N. M. Watson
      4  1.1  tls  * Author: Alfred Perlstein <alfred (at) paycounter.com>, <alfred (at) FreeBSD.org>
      5  1.1  tls  * All rights reserved.
      6  1.1  tls  *
      7  1.1  tls  * Redistribution and use in source and binary forms, with or without
      8  1.1  tls  * modification, are permitted provided that the following conditions
      9  1.1  tls  * are met:
     10  1.1  tls  * 1. Redistributions of source code must retain the above copyright
     11  1.1  tls  *    notice, this list of conditions and the following disclaimer.
     12  1.1  tls  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  tls  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  tls  *    documentation and/or other materials provided with the distribution.
     15  1.1  tls  *
     16  1.1  tls  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17  1.1  tls  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  1.1  tls  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  1.1  tls  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20  1.1  tls  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  1.1  tls  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  1.1  tls  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  1.1  tls  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  1.1  tls  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  1.1  tls  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  1.1  tls  * SUCH DAMAGE.
     27  1.1  tls  */
     28  1.1  tls 
     29  1.1  tls #include <sys/cdefs.h>
     30  1.1  tls __KERNEL_RCSID(0, "$NetBSD: uipc_accf.c,v 1.1 2008/08/04 03:55:47 tls Exp $");
     31  1.1  tls 
     32  1.1  tls #define ACCEPT_FILTER_MOD
     33  1.1  tls 
     34  1.1  tls #include "opt_inet.h"
     35  1.1  tls #include <sys/param.h>
     36  1.1  tls #include <sys/systm.h>
     37  1.1  tls #include <sys/domain.h>
     38  1.1  tls #include <sys/kernel.h>
     39  1.1  tls #include <sys/lock.h>
     40  1.1  tls #include <sys/malloc.h>
     41  1.1  tls #include <sys/mbuf.h>
     42  1.1  tls #include <sys/lkm.h>
     43  1.1  tls #include <sys/mutex.h>
     44  1.1  tls #include <sys/protosw.h>
     45  1.1  tls #include <sys/sysctl.h>
     46  1.1  tls #include <sys/socket.h>
     47  1.1  tls #include <sys/socketvar.h>
     48  1.1  tls #include <sys/queue.h>
     49  1.1  tls #include <sys/once.h>
     50  1.1  tls 
     51  1.1  tls static kmutex_t accept_filter_mtx;
     52  1.1  tls #define	ACCEPT_FILTER_LOCK()		mutex_spin_enter(&accept_filter_mtx)
     53  1.1  tls #define	ACCEPT_FILTER_UNLOCK()		mutex_spin_exit(&accept_filter_mtx);
     54  1.1  tls #define	SOCK_LOCK(so)
     55  1.1  tls #define	SOCK_UNLOCK(so)
     56  1.1  tls 
     57  1.1  tls static SLIST_HEAD(, accept_filter) accept_filtlsthd =
     58  1.1  tls 	SLIST_HEAD_INITIALIZER(&accept_filtlsthd);
     59  1.1  tls 
     60  1.1  tls MALLOC_DEFINE(M_ACCF, "accf", "accept filter data");
     61  1.1  tls 
     62  1.1  tls static int unloadable = 0;
     63  1.1  tls 
     64  1.1  tls /*
     65  1.1  tls  * Names of Accept filter sysctl objects
     66  1.1  tls  */
     67  1.1  tls 
     68  1.1  tls #define ACCFCTL_UNLOADABLE	1	/* Allow module to be unloaded */
     69  1.1  tls 
     70  1.1  tls 
     71  1.1  tls SYSCTL_SETUP(sysctl_net_inet_accf_setup, "sysctl net.inet.accf subtree setup")
     72  1.1  tls {
     73  1.1  tls 	sysctl_createv(clog, 0, NULL, NULL,
     74  1.1  tls 		       CTLFLAG_PERMANENT,
     75  1.1  tls 		       CTLTYPE_NODE, "net", NULL,
     76  1.1  tls 		       NULL, 0, NULL, 0,
     77  1.1  tls 		       CTL_NET, CTL_EOL);
     78  1.1  tls 	sysctl_createv(clog, 0, NULL, NULL,
     79  1.1  tls 		       CTLFLAG_PERMANENT,
     80  1.1  tls 		       CTLTYPE_NODE, "inet", NULL,
     81  1.1  tls 		       NULL, 0, NULL, 0,
     82  1.1  tls 		       CTL_NET, PF_INET, CTL_EOL);
     83  1.1  tls 	sysctl_createv(clog, 0, NULL, NULL,
     84  1.1  tls 		       CTLFLAG_PERMANENT,
     85  1.1  tls 		       CTLTYPE_NODE, "accf",
     86  1.1  tls 		       SYSCTL_DESCR("Accept filters"),
     87  1.1  tls 		       NULL, 0, NULL, 0,
     88  1.1  tls 		       CTL_NET, PF_INET, SO_ACCEPTFILTER, CTL_EOL);
     89  1.1  tls 	sysctl_createv(clog, 0, NULL, NULL,
     90  1.1  tls 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
     91  1.1  tls 		       CTLTYPE_INT, "unloadable",
     92  1.1  tls 		       SYSCTL_DESCR("Allow unload of accept filters "
     93  1.1  tls 				    "(not recommended)"),
     94  1.1  tls 		       NULL, 0, &unloadable, 0,
     95  1.1  tls 		       CTL_NET, PF_INET, SO_ACCEPTFILTER,
     96  1.1  tls 		       ACCFCTL_UNLOADABLE, CTL_EOL);
     97  1.1  tls }
     98  1.1  tls 
     99  1.1  tls /*
    100  1.1  tls  * Must be passed a malloc'd structure so we don't explode if the kld is
    101  1.1  tls  * unloaded, we leak the struct on deallocation to deal with this, but if a
    102  1.1  tls  * filter is loaded with the same name as a leaked one we re-use the entry.
    103  1.1  tls  */
    104  1.1  tls int
    105  1.1  tls accept_filt_add(struct accept_filter *filt)
    106  1.1  tls {
    107  1.1  tls 	struct accept_filter *p;
    108  1.1  tls 
    109  1.1  tls 	ACCEPT_FILTER_LOCK();
    110  1.1  tls 	SLIST_FOREACH(p, &accept_filtlsthd, accf_next)
    111  1.1  tls 		if (strcmp(p->accf_name, filt->accf_name) == 0)  {
    112  1.1  tls 			if (p->accf_callback != NULL) {
    113  1.1  tls 				ACCEPT_FILTER_UNLOCK();
    114  1.1  tls 				return (EEXIST);
    115  1.1  tls 			} else {
    116  1.1  tls 				p->accf_callback = filt->accf_callback;
    117  1.1  tls 				ACCEPT_FILTER_UNLOCK();
    118  1.1  tls 				FREE(filt, M_ACCF);
    119  1.1  tls 				return (0);
    120  1.1  tls 			}
    121  1.1  tls 		}
    122  1.1  tls 
    123  1.1  tls 	if (p == NULL)
    124  1.1  tls 		SLIST_INSERT_HEAD(&accept_filtlsthd, filt, accf_next);
    125  1.1  tls 	ACCEPT_FILTER_UNLOCK();
    126  1.1  tls 	return (0);
    127  1.1  tls }
    128  1.1  tls 
    129  1.1  tls int
    130  1.1  tls accept_filt_del(char *name)
    131  1.1  tls {
    132  1.1  tls 	struct accept_filter *p;
    133  1.1  tls 
    134  1.1  tls 	p = accept_filt_get(name);
    135  1.1  tls 	if (p == NULL)
    136  1.1  tls 		return (ENOENT);
    137  1.1  tls 
    138  1.1  tls 	p->accf_callback = NULL;
    139  1.1  tls 	return (0);
    140  1.1  tls }
    141  1.1  tls 
    142  1.1  tls struct accept_filter *
    143  1.1  tls accept_filt_get(char *name)
    144  1.1  tls {
    145  1.1  tls 	struct accept_filter *p;
    146  1.1  tls 
    147  1.1  tls 	ACCEPT_FILTER_LOCK();
    148  1.1  tls 	SLIST_FOREACH(p, &accept_filtlsthd, accf_next)
    149  1.1  tls 		if (strcmp(p->accf_name, name) == 0)
    150  1.1  tls 			break;
    151  1.1  tls 	ACCEPT_FILTER_UNLOCK();
    152  1.1  tls 
    153  1.1  tls 	return (p);
    154  1.1  tls }
    155  1.1  tls 
    156  1.1  tls /*
    157  1.1  tls  * Accept filter initialization routine.
    158  1.1  tls  * This should be called only once.
    159  1.1  tls  */
    160  1.1  tls 
    161  1.1  tls static int
    162  1.1  tls accept_filter_init0(void)
    163  1.1  tls {
    164  1.1  tls 	mutex_init(&accept_filter_mtx, MUTEX_DEFAULT, IPL_NET);
    165  1.1  tls 
    166  1.1  tls 	return 0;
    167  1.1  tls }
    168  1.1  tls 
    169  1.1  tls /*
    170  1.1  tls  * Initialization routine: This can also be replaced with
    171  1.1  tls  * accept_filt_generic_mod_event for attaching new accept filter.
    172  1.1  tls  */
    173  1.1  tls 
    174  1.1  tls void
    175  1.1  tls accept_filter_init(void)
    176  1.1  tls {
    177  1.1  tls 	static ONCE_DECL(accept_filter_init_once);
    178  1.1  tls 
    179  1.1  tls 	RUN_ONCE(&accept_filter_init_once, accept_filter_init0);
    180  1.1  tls }
    181  1.1  tls 
    182  1.1  tls int
    183  1.1  tls accept_filt_generic_mod_event(struct lkm_table *lkmtp, int event, void *data)
    184  1.1  tls {
    185  1.1  tls 	struct accept_filter *p;
    186  1.1  tls 	struct accept_filter *accfp = (struct accept_filter *) data;
    187  1.1  tls 	int error;
    188  1.1  tls 
    189  1.1  tls 	switch (event) {
    190  1.1  tls 	case LKM_E_LOAD:
    191  1.1  tls 		accept_filter_init();
    192  1.1  tls 		MALLOC(p, struct accept_filter *, sizeof(*p), M_ACCF,
    193  1.1  tls 		    M_WAITOK);
    194  1.1  tls 		bcopy(accfp, p, sizeof(*p));
    195  1.1  tls 		error = accept_filt_add(p);
    196  1.1  tls 		break;
    197  1.1  tls 
    198  1.1  tls 	case LKM_E_UNLOAD:
    199  1.1  tls 		/*
    200  1.1  tls 		 * Do not support unloading yet. we don't keep track of
    201  1.1  tls 		 * refcounts and unloading an accept filter callback and then
    202  1.1  tls 		 * having it called is a bad thing.  A simple fix would be to
    203  1.1  tls 		 * track the refcount in the struct accept_filter.
    204  1.1  tls 		 */
    205  1.1  tls 		if (unloadable != 0) {
    206  1.1  tls 			error = accept_filt_del(accfp->accf_name);
    207  1.1  tls 		} else
    208  1.1  tls 			error = EOPNOTSUPP;
    209  1.1  tls 		break;
    210  1.1  tls 
    211  1.1  tls 	case LKM_E_STAT:
    212  1.1  tls 		error = 0;
    213  1.1  tls 		break;
    214  1.1  tls 
    215  1.1  tls 	default:
    216  1.1  tls 		error = EOPNOTSUPP;
    217  1.1  tls 		break;
    218  1.1  tls 	}
    219  1.1  tls 
    220  1.1  tls 	return (error);
    221  1.1  tls }
    222  1.1  tls 
    223  1.1  tls int
    224  1.1  tls do_getopt_accept_filter(struct socket *so, struct mbuf *m)
    225  1.1  tls {
    226  1.1  tls 	int error;
    227  1.1  tls 
    228  1.1  tls 	error = 0;
    229  1.1  tls 	SOCK_LOCK(so);
    230  1.1  tls 	if ((so->so_options & SO_ACCEPTCONN) == 0) {
    231  1.1  tls 		error = EINVAL;
    232  1.1  tls 		goto out;
    233  1.1  tls 	}
    234  1.1  tls 	if ((so->so_options & SO_ACCEPTFILTER) == 0) {
    235  1.1  tls 		error = EINVAL;
    236  1.1  tls 		goto out;
    237  1.1  tls 	}
    238  1.1  tls 	m->m_len = sizeof(struct accept_filter_arg);
    239  1.1  tls 	strcpy(mtod(m, struct accept_filter_arg *)->af_name, so->so_accf->so_accept_filter->accf_name);
    240  1.1  tls 	if (so->so_accf->so_accept_filter_str != NULL)
    241  1.1  tls 		strcpy(mtod(m, struct accept_filter_arg *)->af_arg, so->so_accf->so_accept_filter_str);
    242  1.1  tls out:
    243  1.1  tls 	SOCK_UNLOCK(so);
    244  1.1  tls 	return (error);
    245  1.1  tls }
    246  1.1  tls 
    247  1.1  tls int
    248  1.1  tls do_setopt_accept_filter(struct socket *so, struct mbuf *m)
    249  1.1  tls {
    250  1.1  tls 	struct accept_filter_arg *afap;
    251  1.1  tls 	struct accept_filter *afp;
    252  1.1  tls 	struct so_accf *newaf;
    253  1.1  tls 	int error = 0;
    254  1.1  tls 
    255  1.1  tls 	/*
    256  1.1  tls 	 * Handle the simple delete case first.
    257  1.1  tls 	 */
    258  1.1  tls 	if (m == NULL || mtod(m, struct accept_filter_arg *) == NULL) {
    259  1.1  tls 		SOCK_LOCK(so);
    260  1.1  tls 		if ((so->so_options & SO_ACCEPTCONN) == 0) {
    261  1.1  tls 			SOCK_UNLOCK(so);
    262  1.1  tls 			return (EINVAL);
    263  1.1  tls 		}
    264  1.1  tls 		if (so->so_accf != NULL) {
    265  1.1  tls 			struct so_accf *af = so->so_accf;
    266  1.1  tls 			if (af->so_accept_filter != NULL &&
    267  1.1  tls 				af->so_accept_filter->accf_destroy != NULL) {
    268  1.1  tls 				af->so_accept_filter->accf_destroy(so);
    269  1.1  tls 			}
    270  1.1  tls 			if (af->so_accept_filter_str != NULL)
    271  1.1  tls 				FREE(af->so_accept_filter_str, M_ACCF);
    272  1.1  tls 			FREE(af, M_ACCF);
    273  1.1  tls 			so->so_accf = NULL;
    274  1.1  tls 		}
    275  1.1  tls 		so->so_options &= ~SO_ACCEPTFILTER;
    276  1.1  tls 		SOCK_UNLOCK(so);
    277  1.1  tls 		return (0);
    278  1.1  tls 	}
    279  1.1  tls 
    280  1.1  tls 	/*
    281  1.1  tls 	 * Pre-allocate any memory we may need later to avoid blocking at
    282  1.1  tls 	 * untimely moments.  This does not optimize for invalid arguments.
    283  1.1  tls 	 */
    284  1.1  tls 	if (m->m_len != sizeof(struct accept_filter_arg)) {
    285  1.1  tls 		return (EINVAL);
    286  1.1  tls 	}
    287  1.1  tls 	afap = mtod(m, struct accept_filter_arg *);
    288  1.1  tls 	afap->af_name[sizeof(afap->af_name)-1] = '\0';
    289  1.1  tls 	afap->af_arg[sizeof(afap->af_arg)-1] = '\0';
    290  1.1  tls 	afp = accept_filt_get(afap->af_name);
    291  1.1  tls 	if (afp == NULL) {
    292  1.1  tls 		return (ENOENT);
    293  1.1  tls 	}
    294  1.1  tls 	/*
    295  1.1  tls 	 * Allocate the new accept filter instance storage.  We may
    296  1.1  tls 	 * have to free it again later if we fail to attach it.  If
    297  1.1  tls 	 * attached properly, 'newaf' is NULLed to avoid a free()
    298  1.1  tls 	 * while in use.
    299  1.1  tls 	 */
    300  1.1  tls 	MALLOC(newaf, struct so_accf *, sizeof(*newaf), M_ACCF, M_WAITOK |
    301  1.1  tls 	    M_ZERO);
    302  1.1  tls 	if (afp->accf_create != NULL && afap->af_name[0] != '\0') {
    303  1.1  tls 		int len = strlen(afap->af_name) + 1;
    304  1.1  tls 		MALLOC(newaf->so_accept_filter_str, char *, len, M_ACCF,
    305  1.1  tls 		    M_WAITOK);
    306  1.1  tls 		strcpy(newaf->so_accept_filter_str, afap->af_name);
    307  1.1  tls 	}
    308  1.1  tls 
    309  1.1  tls 	/*
    310  1.1  tls 	 * Require a listen socket; don't try to replace an existing filter
    311  1.1  tls 	 * without first removing it.
    312  1.1  tls 	 */
    313  1.1  tls 	SOCK_LOCK(so);
    314  1.1  tls 	if (((so->so_options & SO_ACCEPTCONN) == 0) ||
    315  1.1  tls 	    (so->so_accf != NULL)) {
    316  1.1  tls 		error = EINVAL;
    317  1.1  tls 		goto out;
    318  1.1  tls 	}
    319  1.1  tls 
    320  1.1  tls 	/*
    321  1.1  tls 	 * Invoke the accf_create() method of the filter if required.  The
    322  1.1  tls 	 * socket mutex is held over this call, so create methods for filters
    323  1.1  tls 	 * can't block.
    324  1.1  tls 	 */
    325  1.1  tls 	if (afp->accf_create != NULL) {
    326  1.1  tls 		newaf->so_accept_filter_arg =
    327  1.1  tls 		    afp->accf_create(so, afap->af_arg);
    328  1.1  tls 		if (newaf->so_accept_filter_arg == NULL) {
    329  1.1  tls 			error = EINVAL;
    330  1.1  tls 			goto out;
    331  1.1  tls 		}
    332  1.1  tls 	}
    333  1.1  tls 	newaf->so_accept_filter = afp;
    334  1.1  tls 	so->so_accf = newaf;
    335  1.1  tls 	so->so_options |= SO_ACCEPTFILTER;
    336  1.1  tls 	newaf = NULL;
    337  1.1  tls out:
    338  1.1  tls 	SOCK_UNLOCK(so);
    339  1.1  tls 	if (newaf != NULL) {
    340  1.1  tls 		if (newaf->so_accept_filter_str != NULL)
    341  1.1  tls 			FREE(newaf->so_accept_filter_str, M_ACCF);
    342  1.1  tls 		FREE(newaf, M_ACCF);
    343  1.1  tls 	}
    344  1.1  tls 	return (error);
    345  1.1  tls }
    346