Home | History | Annotate | Line # | Download | only in npf
npf_rproc.c revision 1.4
      1  1.4  mlelstv /*	$NetBSD: npf_rproc.c,v 1.4 2012/10/03 12:24:56 mlelstv Exp $	*/
      2  1.1    rmind 
      3  1.1    rmind /*-
      4  1.1    rmind  * Copyright (c) 2009-2012 The NetBSD Foundation, Inc.
      5  1.1    rmind  * All rights reserved.
      6  1.1    rmind  *
      7  1.1    rmind  * This material is based upon work partially supported by The
      8  1.1    rmind  * NetBSD Foundation under a contract with Mindaugas Rasiukevicius.
      9  1.1    rmind  *
     10  1.1    rmind  * Redistribution and use in source and binary forms, with or without
     11  1.1    rmind  * modification, are permitted provided that the following conditions
     12  1.1    rmind  * are met:
     13  1.1    rmind  * 1. Redistributions of source code must retain the above copyright
     14  1.1    rmind  *    notice, this list of conditions and the following disclaimer.
     15  1.1    rmind  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1    rmind  *    notice, this list of conditions and the following disclaimer in the
     17  1.1    rmind  *    documentation and/or other materials provided with the distribution.
     18  1.1    rmind  *
     19  1.1    rmind  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1    rmind  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1    rmind  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1    rmind  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1    rmind  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1    rmind  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1    rmind  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1    rmind  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1    rmind  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1    rmind  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1    rmind  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1    rmind  */
     31  1.1    rmind 
     32  1.1    rmind /*
     33  1.3    rmind  * NPF extension and rule procedure interface.
     34  1.1    rmind  */
     35  1.1    rmind 
     36  1.1    rmind #include <sys/cdefs.h>
     37  1.1    rmind __KERNEL_RCSID(0, "$NetBSD");
     38  1.1    rmind 
     39  1.1    rmind #include <sys/param.h>
     40  1.2    rmind #include <sys/types.h>
     41  1.1    rmind 
     42  1.1    rmind #include <sys/atomic.h>
     43  1.1    rmind #include <sys/kmem.h>
     44  1.3    rmind #include <sys/mutex.h>
     45  1.1    rmind 
     46  1.1    rmind #include "npf_impl.h"
     47  1.1    rmind 
     48  1.3    rmind #define	EXT_NAME_LEN		32
     49  1.3    rmind 
     50  1.3    rmind typedef struct npf_ext {
     51  1.3    rmind 	char			ext_callname[EXT_NAME_LEN];
     52  1.3    rmind 	LIST_ENTRY(npf_ext)	ext_entry;
     53  1.3    rmind 	const npf_ext_ops_t *	ext_ops;
     54  1.3    rmind 	unsigned		ext_refcnt;
     55  1.3    rmind } npf_ext_t;
     56  1.3    rmind 
     57  1.3    rmind #define	RPROC_NAME_LEN		32
     58  1.3    rmind #define	RPROC_EXT_COUNT		16
     59  1.1    rmind 
     60  1.1    rmind struct npf_rproc {
     61  1.3    rmind 	/* Name, reference count and flags. */
     62  1.3    rmind 	char			rp_name[RPROC_NAME_LEN];
     63  1.1    rmind 	u_int			rp_refcnt;
     64  1.1    rmind 	uint32_t		rp_flags;
     65  1.3    rmind 	/* Associated extensions and their metadata . */
     66  1.3    rmind 	unsigned		rp_ext_count;
     67  1.3    rmind 	npf_ext_t *		rp_ext[RPROC_EXT_COUNT];
     68  1.3    rmind 	void *			rp_ext_meta[RPROC_EXT_COUNT];
     69  1.1    rmind };
     70  1.1    rmind 
     71  1.3    rmind static LIST_HEAD(, npf_ext)	ext_list	__cacheline_aligned;
     72  1.3    rmind static kmutex_t			ext_lock	__cacheline_aligned;
     73  1.3    rmind 
     74  1.3    rmind void
     75  1.3    rmind npf_ext_sysinit(void)
     76  1.3    rmind {
     77  1.3    rmind 	mutex_init(&ext_lock, MUTEX_DEFAULT, IPL_NONE);
     78  1.3    rmind 	LIST_INIT(&ext_list);
     79  1.3    rmind }
     80  1.3    rmind 
     81  1.3    rmind void
     82  1.3    rmind npf_ext_sysfini(void)
     83  1.3    rmind {
     84  1.3    rmind 	KASSERT(LIST_EMPTY(&ext_list));
     85  1.3    rmind 	mutex_destroy(&ext_lock);
     86  1.3    rmind }
     87  1.3    rmind 
     88  1.3    rmind /*
     89  1.3    rmind  * NPF extension management for the rule procedures.
     90  1.3    rmind  */
     91  1.3    rmind 
     92  1.3    rmind static npf_ext_t *
     93  1.3    rmind npf_ext_lookup(const char *name)
     94  1.3    rmind {
     95  1.3    rmind 	npf_ext_t *ext = NULL;
     96  1.3    rmind 
     97  1.3    rmind 	KASSERT(mutex_owned(&ext_lock));
     98  1.3    rmind 
     99  1.3    rmind 	LIST_FOREACH(ext, &ext_list, ext_entry)
    100  1.3    rmind 		if (strcmp(ext->ext_callname, name) == 0)
    101  1.3    rmind 			break;
    102  1.3    rmind 	return ext;
    103  1.3    rmind }
    104  1.3    rmind 
    105  1.3    rmind void *
    106  1.3    rmind npf_ext_register(const char *name, const npf_ext_ops_t *ops)
    107  1.3    rmind {
    108  1.3    rmind 	npf_ext_t *ext;
    109  1.3    rmind 
    110  1.3    rmind 	ext = kmem_zalloc(sizeof(npf_ext_t), KM_SLEEP);
    111  1.3    rmind 	strlcpy(ext->ext_callname, name, EXT_NAME_LEN);
    112  1.3    rmind 	ext->ext_ops = ops;
    113  1.3    rmind 
    114  1.3    rmind 	mutex_enter(&ext_lock);
    115  1.3    rmind 	if (npf_ext_lookup(name)) {
    116  1.3    rmind 		mutex_exit(&ext_lock);
    117  1.3    rmind 		kmem_free(ext, sizeof(npf_ext_t));
    118  1.3    rmind 		return NULL;
    119  1.3    rmind 	}
    120  1.3    rmind 	LIST_INSERT_HEAD(&ext_list, ext, ext_entry);
    121  1.3    rmind 	mutex_exit(&ext_lock);
    122  1.3    rmind 
    123  1.3    rmind 	return (void *)ext;
    124  1.3    rmind }
    125  1.3    rmind 
    126  1.3    rmind int
    127  1.3    rmind npf_ext_unregister(void *extid)
    128  1.3    rmind {
    129  1.3    rmind 	npf_ext_t *ext = extid;
    130  1.3    rmind 
    131  1.3    rmind 	/*
    132  1.3    rmind 	 * Check if in-use first (re-check with the lock held).
    133  1.3    rmind 	 */
    134  1.3    rmind 	if (ext->ext_refcnt) {
    135  1.3    rmind 		return EBUSY;
    136  1.3    rmind 	}
    137  1.3    rmind 
    138  1.3    rmind 	mutex_enter(&ext_lock);
    139  1.3    rmind 	if (ext->ext_refcnt) {
    140  1.3    rmind 		mutex_exit(&ext_lock);
    141  1.3    rmind 		return EBUSY;
    142  1.3    rmind 	}
    143  1.3    rmind 	KASSERT(npf_ext_lookup(ext->ext_callname));
    144  1.3    rmind 	LIST_REMOVE(ext, ext_entry);
    145  1.3    rmind 	mutex_exit(&ext_lock);
    146  1.3    rmind 
    147  1.3    rmind 	kmem_free(ext, sizeof(npf_ext_t));
    148  1.3    rmind 	return 0;
    149  1.3    rmind }
    150  1.3    rmind 
    151  1.3    rmind int
    152  1.3    rmind npf_ext_construct(const char *name, npf_rproc_t *rp, prop_dictionary_t params)
    153  1.3    rmind {
    154  1.3    rmind 	const npf_ext_ops_t *extops;
    155  1.3    rmind 	npf_ext_t *ext;
    156  1.3    rmind 	unsigned i;
    157  1.3    rmind 	int error;
    158  1.3    rmind 
    159  1.3    rmind 	if (rp->rp_ext_count >= RPROC_EXT_COUNT) {
    160  1.3    rmind 		return ENOSPC;
    161  1.3    rmind 	}
    162  1.3    rmind 
    163  1.3    rmind 	mutex_enter(&ext_lock);
    164  1.3    rmind 	ext = npf_ext_lookup(name);
    165  1.3    rmind 	if (ext) {
    166  1.3    rmind 		atomic_inc_uint(&ext->ext_refcnt);
    167  1.3    rmind 	}
    168  1.3    rmind 	mutex_exit(&ext_lock);
    169  1.4  mlelstv 
    170  1.3    rmind 	if (!ext) {
    171  1.3    rmind 		return ENOENT;
    172  1.3    rmind 	}
    173  1.3    rmind 
    174  1.4  mlelstv 	extops = ext->ext_ops;
    175  1.4  mlelstv 	KASSERT(extops != NULL);
    176  1.4  mlelstv 
    177  1.3    rmind 	error = extops->ctor(rp, params);
    178  1.3    rmind 	if (error) {
    179  1.3    rmind 		atomic_dec_uint(&ext->ext_refcnt);
    180  1.3    rmind 		return error;
    181  1.3    rmind 	}
    182  1.3    rmind 	i = rp->rp_ext_count++;
    183  1.3    rmind 	rp->rp_ext[i] = ext;
    184  1.3    rmind 	return 0;
    185  1.3    rmind }
    186  1.3    rmind 
    187  1.3    rmind /*
    188  1.3    rmind  * Rule procedure management.
    189  1.3    rmind  */
    190  1.3    rmind 
    191  1.3    rmind /*
    192  1.3    rmind  * npf_rproc_create: construct a new rule procedure, lookup and associate
    193  1.3    rmind  * the extension calls with it.
    194  1.3    rmind  */
    195  1.1    rmind npf_rproc_t *
    196  1.1    rmind npf_rproc_create(prop_dictionary_t rpdict)
    197  1.1    rmind {
    198  1.3    rmind 	const char *name;
    199  1.1    rmind 	npf_rproc_t *rp;
    200  1.3    rmind 
    201  1.3    rmind 	if (!prop_dictionary_get_cstring_nocopy(rpdict, "name", &name)) {
    202  1.3    rmind 		return NULL;
    203  1.3    rmind 	}
    204  1.1    rmind 
    205  1.2    rmind 	rp = kmem_intr_zalloc(sizeof(npf_rproc_t), KM_SLEEP);
    206  1.1    rmind 	rp->rp_refcnt = 1;
    207  1.1    rmind 
    208  1.3    rmind 	strlcpy(rp->rp_name, name, RPROC_NAME_LEN);
    209  1.1    rmind 	prop_dictionary_get_uint32(rpdict, "flags", &rp->rp_flags);
    210  1.1    rmind 	return rp;
    211  1.1    rmind }
    212  1.1    rmind 
    213  1.3    rmind /*
    214  1.3    rmind  * npf_rproc_acquire: acquire the reference on the rule procedure.
    215  1.3    rmind  */
    216  1.1    rmind void
    217  1.1    rmind npf_rproc_acquire(npf_rproc_t *rp)
    218  1.1    rmind {
    219  1.1    rmind 
    220  1.1    rmind 	atomic_inc_uint(&rp->rp_refcnt);
    221  1.1    rmind }
    222  1.1    rmind 
    223  1.3    rmind /*
    224  1.3    rmind  * npf_rproc_release: drop the reference count and destroy the rule
    225  1.3    rmind  * procedure on the last reference.
    226  1.3    rmind  */
    227  1.1    rmind void
    228  1.1    rmind npf_rproc_release(npf_rproc_t *rp)
    229  1.1    rmind {
    230  1.1    rmind 
    231  1.1    rmind 	KASSERT(rp->rp_refcnt > 0);
    232  1.1    rmind 	if (atomic_dec_uint_nv(&rp->rp_refcnt) != 0) {
    233  1.1    rmind 		return;
    234  1.1    rmind 	}
    235  1.3    rmind 	/* XXXintr */
    236  1.3    rmind 	for (unsigned i = 0; i < rp->rp_ext_count; i++) {
    237  1.3    rmind 		npf_ext_t *ext = rp->rp_ext[i];
    238  1.3    rmind 		const npf_ext_ops_t *extops = ext->ext_ops;
    239  1.3    rmind 
    240  1.3    rmind 		extops->dtor(rp, rp->rp_ext_meta[i]);
    241  1.3    rmind 		atomic_dec_uint(&ext->ext_refcnt);
    242  1.3    rmind 	}
    243  1.2    rmind 	kmem_intr_free(rp, sizeof(npf_rproc_t));
    244  1.1    rmind }
    245  1.1    rmind 
    246  1.1    rmind void
    247  1.3    rmind npf_rproc_assign(npf_rproc_t *rp, void *params)
    248  1.1    rmind {
    249  1.3    rmind 	unsigned i = rp->rp_ext_count;
    250  1.3    rmind 
    251  1.3    rmind 	/* Note: params may be NULL. */
    252  1.3    rmind 	KASSERT(i < RPROC_EXT_COUNT);
    253  1.3    rmind 	rp->rp_ext_meta[i] = params;
    254  1.3    rmind }
    255  1.3    rmind 
    256  1.3    rmind /*
    257  1.3    rmind  * npf_rproc_run: run the rule procedure by executing each extension call.
    258  1.3    rmind  *
    259  1.3    rmind  * => Reference on the rule procedure must be held.
    260  1.3    rmind  */
    261  1.3    rmind void
    262  1.3    rmind npf_rproc_run(npf_cache_t *npc, nbuf_t *nbuf, npf_rproc_t *rp, int *decision)
    263  1.3    rmind {
    264  1.3    rmind 	const unsigned extcount = rp->rp_ext_count;
    265  1.1    rmind 
    266  1.1    rmind 	KASSERT(rp->rp_refcnt > 0);
    267  1.1    rmind 
    268  1.3    rmind 	for (unsigned i = 0; i < extcount; i++) {
    269  1.3    rmind 		const npf_ext_t *ext = rp->rp_ext[i];
    270  1.3    rmind 		const npf_ext_ops_t *extops = ext->ext_ops;
    271  1.1    rmind 
    272  1.3    rmind 		KASSERT(ext->ext_refcnt > 0);
    273  1.3    rmind 		extops->proc(npc, nbuf, rp->rp_ext_meta[i], decision);
    274  1.1    rmind 	}
    275  1.1    rmind }
    276