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