Home | History | Annotate | Line # | Download | only in npf
npf_ruleset.c revision 1.2.2.2
      1 /*	$NetBSD: npf_ruleset.c,v 1.2.2.2 2010/10/09 03:32:37 yamt Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2009-2010 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 ruleset module.
     34  *
     35  * Lock order:
     36  *
     37  *	ruleset_lock -> table_lock -> npf_table_t::t_lock
     38  */
     39 
     40 #ifdef _KERNEL
     41 #include <sys/cdefs.h>
     42 __KERNEL_RCSID(0, "$NetBSD: npf_ruleset.c,v 1.2.2.2 2010/10/09 03:32:37 yamt Exp $");
     43 
     44 #include <sys/param.h>
     45 #include <sys/kernel.h>
     46 #endif
     47 
     48 #include <sys/atomic.h>
     49 #include <sys/kmem.h>
     50 #include <sys/pool.h>
     51 #include <sys/queue.h>
     52 #include <sys/rwlock.h>
     53 #include <sys/types.h>
     54 
     55 #include <net/if.h>
     56 #include <net/pfil.h>
     57 
     58 #include "npf_ncode.h"
     59 #include "npf_impl.h"
     60 
     61 struct npf_hook {
     62 	void				(*hk_fn)(const npf_cache_t *, void *);
     63 	void *				hk_arg;
     64 	LIST_ENTRY(npf_hook)		hk_entry;
     65 };
     66 
     67 struct npf_ruleset {
     68 	TAILQ_HEAD(, npf_rule)		rs_queue;
     69 	npf_rule_t *			rs_default;
     70 	int				_reserved;
     71 };
     72 
     73 /* Rule structure. */
     74 struct npf_rule {
     75 	/* List entry in the ruleset. */
     76 	TAILQ_ENTRY(npf_rule)		r_entry;
     77 	/* Optional: sub-ruleset, NAT policy. */
     78 	npf_ruleset_t			r_subset;
     79 	npf_natpolicy_t *		r_nat;
     80 	/* Rule priority: (highest) 0, 1, 2 ... n (lowest). */
     81 	u_int				r_priority;
     82 	/* N-code to process. */
     83 	void *				r_ncode;
     84 	size_t				r_nc_size;
     85 	/* Attributes of this rule. */
     86 	int				r_attr;
     87 	/* Interface. */
     88 	u_int				r_ifid;
     89 	/* Hit counter. */
     90 	u_long				r_hitcount;
     91 	/* List of hooks to process on match. */
     92 	LIST_HEAD(, npf_hook)		r_hooks;
     93 };
     94 
     95 /* Global ruleset, its lock, cache and NAT ruleset. */
     96 static npf_ruleset_t *			ruleset;
     97 static krwlock_t			ruleset_lock;
     98 static pool_cache_t			rule_cache;
     99 
    100 /*
    101  * npf_ruleset_sysinit: initialise ruleset structures.
    102  */
    103 int
    104 npf_ruleset_sysinit(void)
    105 {
    106 
    107 	rule_cache = pool_cache_init(sizeof(npf_rule_t), coherency_unit,
    108 	    0, 0, "npfrlpl", NULL, IPL_NONE, NULL, NULL, NULL);
    109 	if (rule_cache == NULL) {
    110 		return ENOMEM;
    111 	}
    112 	rw_init(&ruleset_lock);
    113 	ruleset = npf_ruleset_create();
    114 	return 0;
    115 }
    116 
    117 void
    118 npf_ruleset_sysfini(void)
    119 {
    120 
    121 	npf_ruleset_destroy(ruleset);
    122 	rw_destroy(&ruleset_lock);
    123 	pool_cache_destroy(rule_cache);
    124 }
    125 
    126 npf_ruleset_t *
    127 npf_ruleset_create(void)
    128 {
    129 	npf_ruleset_t *rlset;
    130 
    131 	rlset = kmem_zalloc(sizeof(npf_ruleset_t), KM_SLEEP);
    132 	TAILQ_INIT(&rlset->rs_queue);
    133 	return rlset;
    134 }
    135 
    136 void
    137 npf_ruleset_destroy(npf_ruleset_t *rlset)
    138 {
    139 	npf_rule_t *rl;
    140 
    141 	while ((rl = TAILQ_FIRST(&rlset->rs_queue)) != NULL) {
    142 		TAILQ_REMOVE(&rlset->rs_queue, rl, r_entry);
    143 		npf_rule_free(rl);
    144 	}
    145 	kmem_free(rlset, sizeof(npf_ruleset_t));
    146 }
    147 
    148 /*
    149  * npf_ruleset_insert: insert the rule into the specified ruleset.
    150  *
    151  * Note: multiple rules at the same priority are allowed.
    152  */
    153 void
    154 npf_ruleset_insert(npf_ruleset_t *rlset, npf_rule_t *rl)
    155 {
    156 	npf_rule_t *it;
    157 
    158 	if (rl->r_attr & NPF_RULE_DEFAULT) {
    159 		rlset->rs_default = rl;
    160 		return;
    161 	}
    162 	TAILQ_FOREACH(it, &rlset->rs_queue, r_entry) {
    163 		/* Rule priority: (highest) 0, 1, 2, 4 ... n (lowest). */
    164 		if (it->r_priority > rl->r_priority)
    165 			break;
    166 	}
    167 	if (it == NULL) {
    168 		TAILQ_INSERT_TAIL(&rlset->rs_queue, rl, r_entry);
    169 	} else {
    170 		TAILQ_INSERT_BEFORE(it, rl, r_entry);
    171 	}
    172 }
    173 
    174 /*
    175  * npf_ruleset_reload: atomically load new ruleset and tableset,
    176  * and destroy old structures.
    177  */
    178 void
    179 npf_ruleset_reload(npf_ruleset_t *nrlset, npf_tableset_t *ntblset)
    180 {
    181 	npf_ruleset_t *oldrlset;
    182 	npf_tableset_t *oldtblset;
    183 
    184 	/*
    185 	 * Swap old ruleset with the new.
    186 	 * XXX: Rework to be fully lock-less; later.
    187 	 */
    188 	rw_enter(&ruleset_lock, RW_WRITER);
    189 	oldrlset = atomic_swap_ptr(&ruleset, nrlset);
    190 
    191 	/*
    192 	 * Setup a new tableset.  It will lock the global tableset lock,
    193 	 * therefore ensures atomicity.  We shall free the old table-set.
    194 	 */
    195 	oldtblset = npf_tableset_reload(ntblset);
    196 	KASSERT(oldtblset != NULL);
    197 	/* Unlock.  Everything goes "live" now. */
    198 	rw_exit(&ruleset_lock);
    199 
    200 	npf_tableset_destroy(oldtblset);
    201 	npf_ruleset_destroy(oldrlset);
    202 }
    203 
    204 /*
    205  * npf_rule_alloc: allocate a rule and copy ncode from user-space.
    206  */
    207 npf_rule_t *
    208 npf_rule_alloc(int attr, pri_t pri, int ifidx, void *nc, size_t sz)
    209 {
    210 	npf_rule_t *rl;
    211 	int errat;
    212 
    213 	/* Perform validation & building of n-code. */
    214 	if (nc && npf_ncode_validate(nc, sz, &errat)) {
    215 		return NULL;
    216 	}
    217 	/* Allocate a rule structure. */
    218 	rl = pool_cache_get(rule_cache, PR_WAITOK);
    219 	if (rl == NULL) {
    220 		return NULL;
    221 	}
    222 	TAILQ_INIT(&rl->r_subset.rs_queue);
    223 	LIST_INIT(&rl->r_hooks);
    224 	rl->r_priority = pri;
    225 	rl->r_attr = attr;
    226 	rl->r_ifid = ifidx;
    227 	rl->r_ncode = nc;
    228 	rl->r_nc_size = sz;
    229 	rl->r_hitcount = 0;
    230 	rl->r_nat = NULL;
    231 	return rl;
    232 }
    233 
    234 #if 0
    235 /*
    236  * npf_activate_rule: activate rule by inserting it into the global ruleset.
    237  */
    238 void
    239 npf_activate_rule(npf_rule_t *rl)
    240 {
    241 
    242 	rw_enter(&ruleset_lock, RW_WRITER);
    243 	npf_ruleset_insert(ruleset, rl);
    244 	rw_exit(&ruleset_lock);
    245 }
    246 
    247 /*
    248  * npf_deactivate_rule: deactivate rule by removing it from the ruleset.
    249  */
    250 void
    251 npf_deactivate_rule(npf_rule_t *)
    252 {
    253 
    254 	rw_enter(&ruleset_lock, RW_WRITER);
    255 	TAILQ_REMOVE(&ruleset->rs_queue, rl, r_entry);
    256 	rw_exit(&ruleset_lock);
    257 }
    258 #endif
    259 
    260 /*
    261  * npf_rule_free: free the specified rule.
    262  */
    263 void
    264 npf_rule_free(npf_rule_t *rl)
    265 {
    266 
    267 	if (rl->r_ncode) {
    268 		/* Free n-code (if any). */
    269 		npf_ncode_free(rl->r_ncode, rl->r_nc_size);
    270 	}
    271 	if (rl->r_nat) {
    272 		/* Free NAT policy (if associated). */
    273 		npf_nat_freepolicy(rl->r_nat);
    274 	}
    275 	pool_cache_put(rule_cache, rl);
    276 }
    277 
    278 /*
    279  * npf_rule_subset: return sub-ruleset, if any.
    280  * npf_rule_getnat: get NAT policy assigned to the rule.
    281  * npf_rule_setnat: assign NAT policy to the rule.
    282  */
    283 
    284 npf_ruleset_t *
    285 npf_rule_subset(npf_rule_t *rl)
    286 {
    287 	return &rl->r_subset;
    288 }
    289 
    290 npf_natpolicy_t *
    291 npf_rule_getnat(const npf_rule_t *rl)
    292 {
    293 	return rl->r_nat;
    294 }
    295 
    296 void
    297 npf_rule_setnat(npf_rule_t *rl, npf_natpolicy_t *np)
    298 {
    299 	rl->r_nat = np;
    300 }
    301 
    302 /*
    303  * npf_hook_register: register action hook in the rule.
    304  */
    305 npf_hook_t *
    306 npf_hook_register(npf_rule_t *rl,
    307     void (*fn)(const npf_cache_t *, void *), void *arg)
    308 {
    309 	npf_hook_t *hk;
    310 
    311 	hk = kmem_alloc(sizeof(npf_hook_t), KM_SLEEP);
    312 	if (hk != NULL) {
    313 		hk->hk_fn = fn;
    314 		hk->hk_arg = arg;
    315 		rw_enter(&ruleset_lock, RW_WRITER);
    316 		LIST_INSERT_HEAD(&rl->r_hooks, hk, hk_entry);
    317 		rw_exit(&ruleset_lock);
    318 	}
    319 	return hk;
    320 }
    321 
    322 /*
    323  * npf_hook_unregister: unregister a specified hook.
    324  *
    325  * => Hook should have been registered in the rule.
    326  */
    327 void
    328 npf_hook_unregister(npf_rule_t *rl, npf_hook_t *hk)
    329 {
    330 
    331 	rw_enter(&ruleset_lock, RW_WRITER);
    332 	LIST_REMOVE(hk, hk_entry);
    333 	rw_exit(&ruleset_lock);
    334 	kmem_free(hk, sizeof(npf_hook_t));
    335 }
    336 
    337 /*
    338  * npf_ruleset_match: inspect the packet against the given ruleset.
    339  *
    340  * Loop for each rule in the set and run n-code processor of each rule
    341  * against the packet (nbuf chain).
    342  */
    343 npf_rule_t *
    344 npf_ruleset_match(npf_ruleset_t *rlset, npf_cache_t *npc, nbuf_t *nbuf,
    345     struct ifnet *ifp, const int di, const int layer)
    346 {
    347 	npf_rule_t *final_rl = NULL, *rl;
    348 
    349 	KASSERT(((di & PFIL_IN) != 0) ^ ((di & PFIL_OUT) != 0));
    350 
    351 	TAILQ_FOREACH(rl, &rlset->rs_queue, r_entry) {
    352 		KASSERT(!final_rl || rl->r_priority >= final_rl->r_priority);
    353 
    354 		/* Match the interface. */
    355 		if (rl->r_ifid && rl->r_ifid != ifp->if_index) {
    356 			continue;
    357 		}
    358 		/* Match the direction. */
    359 		if ((rl->r_attr & NPF_RULE_DIMASK) != NPF_RULE_DIMASK) {
    360 			const int di_mask =
    361 			    (di & PFIL_IN) ? NPF_RULE_IN : NPF_RULE_OUT;
    362 
    363 			if ((rl->r_attr & di_mask) == 0)
    364 				continue;
    365 		}
    366 		/* Process the n-code, if any. */
    367 		const void *nc = rl->r_ncode;
    368 		if (nc && npf_ncode_process(npc, nc, nbuf, layer)) {
    369 			continue;
    370 		}
    371 		/* Set the matching rule and check for "final". */
    372 		final_rl = rl;
    373 		if (rl->r_attr & NPF_RULE_FINAL) {
    374 			break;
    375 		}
    376 	}
    377 	return final_rl;
    378 }
    379 
    380 /*
    381  * npf_ruleset_inspect: inspection of the main ruleset for filtering.
    382  * If sub-ruleset is found, inspect it.
    383  *
    384  * => If found, ruleset is kept read-locked.
    385  * => Caller should protect the nbuf chain.
    386  */
    387 npf_rule_t *
    388 npf_ruleset_inspect(npf_cache_t *npc, nbuf_t *nbuf,
    389     struct ifnet *ifp, const int di, const int layer)
    390 {
    391 	npf_ruleset_t *rlset = ruleset;
    392 	npf_rule_t *rl;
    393 	bool defed;
    394 
    395 	defed = false;
    396 	rw_enter(&ruleset_lock, RW_READER);
    397 reinspect:
    398 	rl = npf_ruleset_match(rlset, npc, nbuf, ifp, di, layer);
    399 
    400 	/* If no final rule, then - default. */
    401 	if (rl == NULL && !defed) {
    402 		rl = ruleset->rs_default;
    403 		defed = true;
    404 	}
    405 	/* Inspect the sub-ruleset, if any. */
    406 	if (rl && !TAILQ_EMPTY(&rl->r_subset.rs_queue)) {
    407 		rlset = &rl->r_subset;
    408 		goto reinspect;
    409 	}
    410 	if (rl == NULL) {
    411 		rw_exit(&ruleset_lock);
    412 	}
    413 	return rl;
    414 }
    415 
    416 /*
    417  * npf_rule_apply: apply the rule i.e. run hooks and return appropriate value.
    418  *
    419  * => Returns ENETUNREACH if "block" and 0 if "pass".
    420  * => Releases the ruleset lock.
    421  */
    422 int
    423 npf_rule_apply(const npf_cache_t *npc, npf_rule_t *rl,
    424     bool *keepstate, int *retfl)
    425 {
    426 	npf_hook_t *hk;
    427 
    428 	KASSERT(rw_lock_held(&ruleset_lock));
    429 
    430 	/* Update the "hit" counter. */
    431 	if (rl->r_attr & NPF_RULE_COUNT) {
    432 		atomic_inc_ulong(&rl->r_hitcount);
    433 	}
    434 
    435 	/* If not passing - drop the packet. */
    436 	if ((rl->r_attr & NPF_RULE_PASS) == 0) {
    437 		/* Determine whether any return message is needed. */
    438 		*retfl = rl->r_attr & (NPF_RULE_RETRST | NPF_RULE_RETICMP);
    439 		rw_exit(&ruleset_lock);
    440 		return ENETUNREACH;
    441 	}
    442 
    443 	/* Passing.  Run the hooks. */
    444 	LIST_FOREACH(hk, &rl->r_hooks, hk_entry) {
    445 		KASSERT(hk->hk_fn != NULL);
    446 		(*hk->hk_fn)(npc, hk->hk_arg);
    447 	}
    448 	*keepstate = (rl->r_attr & NPF_RULE_KEEPSTATE) != 0;
    449 	rw_exit(&ruleset_lock);
    450 
    451 	return 0;
    452 }
    453 
    454 #if defined(DDB) || defined(_NPF_TESTING)
    455 
    456 void
    457 npf_rulenc_dump(npf_rule_t *rl)
    458 {
    459 	uint32_t *op = rl->r_ncode;
    460 	size_t n = rl->r_nc_size;
    461 
    462 	while (n) {
    463 		printf("\t> |0x%02x|\n", (uint32_t)*op);
    464 		op++;
    465 		n -= sizeof(*op);
    466 	}
    467 	printf("-> %s\n", (rl->r_attr & NPF_RULE_PASS) ? "pass" : "block");
    468 }
    469 
    470 #endif
    471