Home | History | Annotate | Line # | Download | only in kern
uipc_accf.c revision 1.5
      1  1.5      ad /*	$NetBSD: uipc_accf.c,v 1.5 2008/10/14 13:45:26 ad Exp $	*/
      2  1.5      ad 
      3  1.5      ad /*-
      4  1.5      ad  * Copyright (c) 2008 The NetBSD Foundation, Inc.
      5  1.5      ad  * All rights reserved.
      6  1.5      ad  *
      7  1.5      ad  * This code is derived from software developed for The NetBSD Foundation
      8  1.5      ad  * by Andrew Doran.
      9  1.5      ad  *
     10  1.5      ad  * Redistribution and use in source and binary forms, with or without
     11  1.5      ad  * modification, are permitted provided that the following conditions
     12  1.5      ad  * are met:
     13  1.5      ad  * 1. Redistributions of source code must retain the above copyright
     14  1.5      ad  *    notice, this list of conditions and the following disclaimer.
     15  1.5      ad  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.5      ad  *    notice, this list of conditions and the following disclaimer in the
     17  1.5      ad  *    documentation and/or other materials provided with the distribution.
     18  1.5      ad  *
     19  1.5      ad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.5      ad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.5      ad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.5      ad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.5      ad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.5      ad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.5      ad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.5      ad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.5      ad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.5      ad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.5      ad  * POSSIBILITY OF SUCH DAMAGE.
     30  1.5      ad  */
     31  1.3   pooka 
     32  1.1     tls /*-
     33  1.1     tls  * Copyright (c) 2000 Paycounter, Inc.
     34  1.1     tls  * Copyright (c) 2005 Robert N. M. Watson
     35  1.1     tls  * Author: Alfred Perlstein <alfred (at) paycounter.com>, <alfred (at) FreeBSD.org>
     36  1.1     tls  * All rights reserved.
     37  1.1     tls  *
     38  1.1     tls  * Redistribution and use in source and binary forms, with or without
     39  1.1     tls  * modification, are permitted provided that the following conditions
     40  1.1     tls  * are met:
     41  1.1     tls  * 1. Redistributions of source code must retain the above copyright
     42  1.1     tls  *    notice, this list of conditions and the following disclaimer.
     43  1.1     tls  * 2. Redistributions in binary form must reproduce the above copyright
     44  1.1     tls  *    notice, this list of conditions and the following disclaimer in the
     45  1.1     tls  *    documentation and/or other materials provided with the distribution.
     46  1.1     tls  *
     47  1.1     tls  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     48  1.1     tls  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     49  1.1     tls  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     50  1.1     tls  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     51  1.1     tls  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     52  1.1     tls  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     53  1.1     tls  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     54  1.1     tls  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     55  1.1     tls  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     56  1.1     tls  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     57  1.1     tls  * SUCH DAMAGE.
     58  1.1     tls  */
     59  1.1     tls 
     60  1.1     tls #include <sys/cdefs.h>
     61  1.5      ad __KERNEL_RCSID(0, "$NetBSD: uipc_accf.c,v 1.5 2008/10/14 13:45:26 ad Exp $");
     62  1.1     tls 
     63  1.1     tls #define ACCEPT_FILTER_MOD
     64  1.1     tls 
     65  1.1     tls #include <sys/param.h>
     66  1.1     tls #include <sys/systm.h>
     67  1.1     tls #include <sys/domain.h>
     68  1.1     tls #include <sys/kernel.h>
     69  1.1     tls #include <sys/lock.h>
     70  1.4     tls #include <sys/kmem.h>
     71  1.1     tls #include <sys/mbuf.h>
     72  1.1     tls #include <sys/lkm.h>
     73  1.5      ad #include <sys/rwlock.h>
     74  1.1     tls #include <sys/protosw.h>
     75  1.1     tls #include <sys/sysctl.h>
     76  1.1     tls #include <sys/socket.h>
     77  1.1     tls #include <sys/socketvar.h>
     78  1.1     tls #include <sys/queue.h>
     79  1.1     tls #include <sys/once.h>
     80  1.5      ad #include <sys/atomic.h>
     81  1.1     tls 
     82  1.5      ad static krwlock_t accept_filter_lock;
     83  1.1     tls 
     84  1.5      ad static LIST_HEAD(, accept_filter) accept_filtlsthd =
     85  1.5      ad     LIST_HEAD_INITIALIZER(&accept_filtlsthd);
     86  1.1     tls 
     87  1.1     tls /*
     88  1.1     tls  * Names of Accept filter sysctl objects
     89  1.1     tls  */
     90  1.1     tls SYSCTL_SETUP(sysctl_net_inet_accf_setup, "sysctl net.inet.accf subtree setup")
     91  1.1     tls {
     92  1.5      ad 
     93  1.1     tls 	sysctl_createv(clog, 0, NULL, NULL,
     94  1.1     tls 		       CTLFLAG_PERMANENT,
     95  1.1     tls 		       CTLTYPE_NODE, "net", NULL,
     96  1.1     tls 		       NULL, 0, NULL, 0,
     97  1.1     tls 		       CTL_NET, CTL_EOL);
     98  1.1     tls 	sysctl_createv(clog, 0, NULL, NULL,
     99  1.1     tls 		       CTLFLAG_PERMANENT,
    100  1.1     tls 		       CTLTYPE_NODE, "inet", NULL,
    101  1.1     tls 		       NULL, 0, NULL, 0,
    102  1.1     tls 		       CTL_NET, PF_INET, CTL_EOL);
    103  1.1     tls 	sysctl_createv(clog, 0, NULL, NULL,
    104  1.1     tls 		       CTLFLAG_PERMANENT,
    105  1.1     tls 		       CTLTYPE_NODE, "accf",
    106  1.1     tls 		       SYSCTL_DESCR("Accept filters"),
    107  1.1     tls 		       NULL, 0, NULL, 0,
    108  1.1     tls 		       CTL_NET, PF_INET, SO_ACCEPTFILTER, CTL_EOL);
    109  1.1     tls }
    110  1.1     tls 
    111  1.1     tls int
    112  1.1     tls accept_filt_add(struct accept_filter *filt)
    113  1.1     tls {
    114  1.1     tls 	struct accept_filter *p;
    115  1.1     tls 
    116  1.5      ad 	rw_enter(&accept_filter_lock, RW_WRITER);
    117  1.5      ad 	LIST_FOREACH(p, &accept_filtlsthd, accf_next) {
    118  1.1     tls 		if (strcmp(p->accf_name, filt->accf_name) == 0)  {
    119  1.5      ad 			rw_exit(&accept_filter_lock);
    120  1.5      ad 			return EEXIST;
    121  1.1     tls 		}
    122  1.5      ad 	}
    123  1.5      ad 	LIST_INSERT_HEAD(&accept_filtlsthd, filt, accf_next);
    124  1.5      ad 	rw_exit(&accept_filter_lock);
    125  1.5      ad 
    126  1.5      ad 	return 0;
    127  1.1     tls }
    128  1.1     tls 
    129  1.1     tls int
    130  1.5      ad accept_filt_del(struct accept_filter *p)
    131  1.1     tls {
    132  1.1     tls 
    133  1.5      ad 	rw_enter(&accept_filter_lock, RW_WRITER);
    134  1.5      ad 	if (p->accf_refcnt != 0) {
    135  1.5      ad 		rw_exit(&accept_filter_lock);
    136  1.5      ad 		return EBUSY;
    137  1.5      ad 	}
    138  1.5      ad 	LIST_REMOVE(p, accf_next);
    139  1.5      ad 	rw_exit(&accept_filter_lock);
    140  1.1     tls 
    141  1.5      ad 	return 0;
    142  1.1     tls }
    143  1.1     tls 
    144  1.1     tls struct accept_filter *
    145  1.1     tls accept_filt_get(char *name)
    146  1.1     tls {
    147  1.1     tls 	struct accept_filter *p;
    148  1.1     tls 
    149  1.5      ad 	rw_enter(&accept_filter_lock, RW_READER);
    150  1.5      ad 	LIST_FOREACH(p, &accept_filtlsthd, accf_next) {
    151  1.5      ad 		if (strcmp(p->accf_name, name) == 0) {
    152  1.5      ad 			atomic_inc_uint(&p->accf_refcnt);
    153  1.1     tls 			break;
    154  1.5      ad 		}
    155  1.5      ad 	}
    156  1.5      ad 	rw_exit(&accept_filter_lock);
    157  1.1     tls 
    158  1.5      ad 	return p;
    159  1.1     tls }
    160  1.1     tls 
    161  1.1     tls /*
    162  1.1     tls  * Accept filter initialization routine.
    163  1.1     tls  * This should be called only once.
    164  1.1     tls  */
    165  1.1     tls 
    166  1.1     tls static int
    167  1.1     tls accept_filter_init0(void)
    168  1.1     tls {
    169  1.5      ad 
    170  1.5      ad 	rw_init(&accept_filter_lock);
    171  1.1     tls 
    172  1.1     tls 	return 0;
    173  1.1     tls }
    174  1.1     tls 
    175  1.1     tls /*
    176  1.1     tls  * Initialization routine: This can also be replaced with
    177  1.1     tls  * accept_filt_generic_mod_event for attaching new accept filter.
    178  1.1     tls  */
    179  1.1     tls 
    180  1.1     tls void
    181  1.1     tls accept_filter_init(void)
    182  1.1     tls {
    183  1.1     tls 	static ONCE_DECL(accept_filter_init_once);
    184  1.1     tls 
    185  1.1     tls 	RUN_ONCE(&accept_filter_init_once, accept_filter_init0);
    186  1.1     tls }
    187  1.1     tls 
    188  1.1     tls int
    189  1.1     tls accept_filt_generic_mod_event(struct lkm_table *lkmtp, int event, void *data)
    190  1.1     tls {
    191  1.1     tls 	struct accept_filter *accfp = (struct accept_filter *) data;
    192  1.1     tls 	int error;
    193  1.1     tls 
    194  1.1     tls 	switch (event) {
    195  1.1     tls 	case LKM_E_LOAD:
    196  1.1     tls 		accept_filter_init();
    197  1.5      ad 		error = accept_filt_add(accfp);
    198  1.1     tls 		break;
    199  1.1     tls 
    200  1.1     tls 	case LKM_E_UNLOAD:
    201  1.5      ad 		error = accept_filt_del(accfp);
    202  1.1     tls 		break;
    203  1.1     tls 
    204  1.1     tls 	case LKM_E_STAT:
    205  1.1     tls 		error = 0;
    206  1.1     tls 		break;
    207  1.1     tls 
    208  1.1     tls 	default:
    209  1.1     tls 		error = EOPNOTSUPP;
    210  1.1     tls 		break;
    211  1.1     tls 	}
    212  1.1     tls 
    213  1.5      ad 	return error;
    214  1.1     tls }
    215  1.1     tls 
    216  1.1     tls int
    217  1.5      ad accept_filt_getopt(struct socket *so, struct sockopt *sopt)
    218  1.1     tls {
    219  1.2  plunky 	struct accept_filter_arg afa;
    220  1.1     tls 	int error;
    221  1.1     tls 
    222  1.5      ad 	KASSERT(solocked(so));
    223  1.5      ad 
    224  1.1     tls 	if ((so->so_options & SO_ACCEPTCONN) == 0) {
    225  1.1     tls 		error = EINVAL;
    226  1.1     tls 		goto out;
    227  1.1     tls 	}
    228  1.1     tls 	if ((so->so_options & SO_ACCEPTFILTER) == 0) {
    229  1.1     tls 		error = EINVAL;
    230  1.1     tls 		goto out;
    231  1.1     tls 	}
    232  1.2  plunky 
    233  1.2  plunky 	memset(&afa, 0, sizeof(afa));
    234  1.2  plunky 	strcpy(afa.af_name, so->so_accf->so_accept_filter->accf_name);
    235  1.1     tls 	if (so->so_accf->so_accept_filter_str != NULL)
    236  1.2  plunky 		strcpy(afa.af_arg, so->so_accf->so_accept_filter_str);
    237  1.2  plunky 	error = sockopt_set(sopt, &afa, sizeof(afa));
    238  1.1     tls out:
    239  1.5      ad 	return error;
    240  1.5      ad }
    241  1.5      ad 
    242  1.5      ad /*
    243  1.5      ad  * Simple delete case, with socket locked.
    244  1.5      ad  */
    245  1.5      ad int
    246  1.5      ad accept_filt_clear(struct socket *so)
    247  1.5      ad {
    248  1.5      ad 	struct accept_filter_arg afa;
    249  1.5      ad 	struct accept_filter *afp;
    250  1.5      ad 	struct socket *so2;
    251  1.5      ad 	struct so_accf *af;
    252  1.5      ad 
    253  1.5      ad 	KASSERT(solocked(so));
    254  1.5      ad 
    255  1.5      ad 	if ((so->so_options & SO_ACCEPTCONN) == 0) {
    256  1.5      ad 		return EINVAL;
    257  1.5      ad 	}
    258  1.5      ad 	if (so->so_accf != NULL) {
    259  1.5      ad 		/* Break in-flight processing. */
    260  1.5      ad 		TAILQ_FOREACH(so2, &so->so_q0, so_qe) {
    261  1.5      ad 			if (so2->so_upcall == NULL) {
    262  1.5      ad 				continue;
    263  1.5      ad 			}
    264  1.5      ad 			so2->so_upcall = NULL;
    265  1.5      ad 			so2->so_upcallarg = NULL;
    266  1.5      ad 			so2->so_rcv.sb_flags &= ~SB_UPCALL;
    267  1.5      ad 			soqremque(so2, 0);
    268  1.5      ad 			soqinsque(so, so2, 1);
    269  1.5      ad 			sorwakeup(so);
    270  1.5      ad 			cv_broadcast(&so->so_cv);
    271  1.5      ad 		}
    272  1.5      ad 		af = so->so_accf;
    273  1.5      ad 		afp = af->so_accept_filter;
    274  1.5      ad 		if (afp != NULL && afp->accf_destroy != NULL) {
    275  1.5      ad 			(*afp->accf_destroy)(so);
    276  1.5      ad 		}
    277  1.5      ad 		if (af->so_accept_filter_str != NULL) {
    278  1.5      ad 			kmem_free(af->so_accept_filter_str,
    279  1.5      ad 			    sizeof(afa.af_name));
    280  1.5      ad 		}
    281  1.5      ad 		kmem_free(af, sizeof(*af));
    282  1.5      ad 		so->so_accf = NULL;
    283  1.5      ad 		atomic_dec_uint(&afp->accf_refcnt);
    284  1.5      ad 	}
    285  1.5      ad 	so->so_options &= ~SO_ACCEPTFILTER;
    286  1.5      ad 	return 0;
    287  1.1     tls }
    288  1.1     tls 
    289  1.5      ad /*
    290  1.5      ad  * setsockopt() for accept filters.  Called with the socket unlocked,
    291  1.5      ad  * will always return it locked.
    292  1.5      ad  */
    293  1.1     tls int
    294  1.5      ad accept_filt_setopt(struct socket *so, const struct sockopt *sopt)
    295  1.1     tls {
    296  1.2  plunky 	struct accept_filter_arg afa;
    297  1.1     tls 	struct accept_filter *afp;
    298  1.1     tls 	struct so_accf *newaf;
    299  1.2  plunky 	int error;
    300  1.1     tls 
    301  1.2  plunky 	if (sopt == NULL || sopt->sopt_size == 0) {
    302  1.5      ad 		solock(so);
    303  1.5      ad 		return accept_filt_clear(so);
    304  1.1     tls 	}
    305  1.1     tls 
    306  1.1     tls 	/*
    307  1.1     tls 	 * Pre-allocate any memory we may need later to avoid blocking at
    308  1.1     tls 	 * untimely moments.  This does not optimize for invalid arguments.
    309  1.1     tls 	 */
    310  1.2  plunky 	error = sockopt_get(sopt, &afa, sizeof(afa));
    311  1.2  plunky 	if (error) {
    312  1.5      ad 		solock(so);
    313  1.5      ad 		return error;
    314  1.1     tls 	}
    315  1.2  plunky 	afa.af_name[sizeof(afa.af_name)-1] = '\0';
    316  1.2  plunky 	afa.af_arg[sizeof(afa.af_arg)-1] = '\0';
    317  1.2  plunky 	afp = accept_filt_get(afa.af_name);
    318  1.1     tls 	if (afp == NULL) {
    319  1.5      ad 		solock(so);
    320  1.5      ad 		return ENOENT;
    321  1.1     tls 	}
    322  1.1     tls 	/*
    323  1.1     tls 	 * Allocate the new accept filter instance storage.  We may
    324  1.1     tls 	 * have to free it again later if we fail to attach it.  If
    325  1.1     tls 	 * attached properly, 'newaf' is NULLed to avoid a free()
    326  1.1     tls 	 * while in use.
    327  1.1     tls 	 */
    328  1.5      ad 	newaf = kmem_zalloc(sizeof(*newaf), KM_SLEEP);
    329  1.2  plunky 	if (afp->accf_create != NULL && afa.af_name[0] != '\0') {
    330  1.4     tls 		/*
    331  1.4     tls 		 * FreeBSD did a variable-size allocation here
    332  1.4     tls 		 * with the actual string length from afa.af_name
    333  1.4     tls 		 * but it is so short, why bother tracking it?
    334  1.4     tls 		 * XXX as others have noted, this is an API mistake;
    335  1.4     tls 		 * XXX accept_filter_arg should have a mandatory namelen.
    336  1.4     tls 		 * XXX (but it's a bit too late to fix that now)
    337  1.4     tls 		 */
    338  1.4     tls 		newaf->so_accept_filter_str =
    339  1.5      ad 		    kmem_alloc(sizeof(afa.af_name), KM_SLEEP);
    340  1.2  plunky 		strcpy(newaf->so_accept_filter_str, afa.af_name);
    341  1.1     tls 	}
    342  1.1     tls 
    343  1.1     tls 	/*
    344  1.1     tls 	 * Require a listen socket; don't try to replace an existing filter
    345  1.1     tls 	 * without first removing it.
    346  1.1     tls 	 */
    347  1.5      ad 	solock(so);
    348  1.5      ad 	if ((so->so_options & SO_ACCEPTCONN) == 0 || so->so_accf != NULL) {
    349  1.1     tls 		error = EINVAL;
    350  1.1     tls 		goto out;
    351  1.1     tls 	}
    352  1.1     tls 
    353  1.1     tls 	/*
    354  1.1     tls 	 * Invoke the accf_create() method of the filter if required.  The
    355  1.5      ad 	 * socket lock is held over this call, so create methods for filters
    356  1.5      ad 	 * shouldn't block.
    357  1.1     tls 	 */
    358  1.1     tls 	if (afp->accf_create != NULL) {
    359  1.1     tls 		newaf->so_accept_filter_arg =
    360  1.5      ad 		    (*afp->accf_create)(so, afa.af_arg);
    361  1.1     tls 		if (newaf->so_accept_filter_arg == NULL) {
    362  1.1     tls 			error = EINVAL;
    363  1.1     tls 			goto out;
    364  1.1     tls 		}
    365  1.1     tls 	}
    366  1.1     tls 	newaf->so_accept_filter = afp;
    367  1.1     tls 	so->so_accf = newaf;
    368  1.1     tls 	so->so_options |= SO_ACCEPTFILTER;
    369  1.1     tls 	newaf = NULL;
    370  1.1     tls out:
    371  1.1     tls 	if (newaf != NULL) {
    372  1.1     tls 		if (newaf->so_accept_filter_str != NULL)
    373  1.4     tls 			kmem_free(newaf->so_accept_filter_str,
    374  1.5      ad 			    sizeof(afa.af_name));
    375  1.4     tls 		kmem_free(newaf, sizeof(*newaf));
    376  1.5      ad 		atomic_dec_uint(&afp->accf_refcnt);
    377  1.1     tls 	}
    378  1.5      ad 	return error;
    379  1.1     tls }
    380