Home | History | Annotate | Line # | Download | only in npf
npf_tableset.c revision 1.12
      1 /*	$NetBSD: npf_tableset.c,v 1.12 2012/07/01 23:21:06 rmind Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2009-2012 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 tableset module.
     34  *
     35  * TODO:
     36  * - Convert to Patricia tree.
     37  * - Dynamic hash growing/shrinking (i.e. re-hash functionality), maybe?
     38  * - Dynamic array resize.
     39  */
     40 
     41 #include <sys/cdefs.h>
     42 __KERNEL_RCSID(0, "$NetBSD: npf_tableset.c,v 1.12 2012/07/01 23:21:06 rmind Exp $");
     43 
     44 #include <sys/param.h>
     45 #include <sys/types.h>
     46 
     47 #include <sys/atomic.h>
     48 #include <sys/hash.h>
     49 #include <sys/kmem.h>
     50 #include <sys/pool.h>
     51 #include <sys/queue.h>
     52 #include <sys/rwlock.h>
     53 #include <sys/systm.h>
     54 #include <sys/types.h>
     55 
     56 #include "npf_impl.h"
     57 
     58 /* Table entry structure. */
     59 struct npf_tblent {
     60 	/* Hash/tree entry. */
     61 	union {
     62 		LIST_ENTRY(npf_tblent)	hashq;
     63 		rb_node_t		rbnode;
     64 	} te_entry;
     65 	/* CIDR block. */
     66 	npf_addr_t			te_addr;
     67 	npf_netmask_t			te_mask;
     68 };
     69 
     70 LIST_HEAD(npf_hashl, npf_tblent);
     71 
     72 /* Table structure. */
     73 struct npf_table {
     74 	char				t_name[16];
     75 	/* Lock and reference count. */
     76 	krwlock_t			t_lock;
     77 	u_int				t_refcnt;
     78 	/* Table ID. */
     79 	u_int				t_id;
     80 	/* The storage type can be: 1. Hash 2. RB-tree. */
     81 	int				t_type;
     82 	struct npf_hashl *		t_hashl;
     83 	u_long				t_hashmask;
     84 	rb_tree_t			t_rbtree;
     85 };
     86 
     87 static pool_cache_t			tblent_cache	__read_mostly;
     88 
     89 /*
     90  * npf_table_sysinit: initialise tableset structures.
     91  */
     92 void
     93 npf_tableset_sysinit(void)
     94 {
     95 
     96 	tblent_cache = pool_cache_init(sizeof(npf_tblent_t), coherency_unit,
     97 	    0, 0, "npftenpl", NULL, IPL_NONE, NULL, NULL, NULL);
     98 }
     99 
    100 void
    101 npf_tableset_sysfini(void)
    102 {
    103 
    104 	pool_cache_destroy(tblent_cache);
    105 }
    106 
    107 npf_tableset_t *
    108 npf_tableset_create(void)
    109 {
    110 	const size_t sz = NPF_TABLE_SLOTS * sizeof(npf_table_t *);
    111 
    112 	return kmem_zalloc(sz, KM_SLEEP);
    113 }
    114 
    115 void
    116 npf_tableset_destroy(npf_tableset_t *tblset)
    117 {
    118 	const size_t sz = NPF_TABLE_SLOTS * sizeof(npf_table_t *);
    119 	npf_table_t *t;
    120 	u_int tid;
    121 
    122 	/*
    123 	 * Destroy all tables (no references should be held, as ruleset
    124 	 * should be destroyed before).
    125 	 */
    126 	for (tid = 0; tid < NPF_TABLE_SLOTS; tid++) {
    127 		t = tblset[tid];
    128 		if (t != NULL) {
    129 			npf_table_destroy(t);
    130 		}
    131 	}
    132 	kmem_free(tblset, sz);
    133 }
    134 
    135 /*
    136  * npf_tableset_insert: insert the table into the specified tableset.
    137  *
    138  * => Returns 0 on success, fails and returns errno if ID is already used.
    139  */
    140 int
    141 npf_tableset_insert(npf_tableset_t *tblset, npf_table_t *t)
    142 {
    143 	const u_int tid = t->t_id;
    144 	int error;
    145 
    146 	KASSERT((u_int)tid < NPF_TABLE_SLOTS);
    147 
    148 	if (tblset[tid] == NULL) {
    149 		tblset[tid] = t;
    150 		error = 0;
    151 	} else {
    152 		error = EEXIST;
    153 	}
    154 	return error;
    155 }
    156 
    157 /*
    158  * Red-black tree storage.
    159  */
    160 
    161 static signed int
    162 table_rbtree_cmp_nodes(void *ctx, const void *n1, const void *n2)
    163 {
    164 	const npf_tblent_t * const te1 = n1;
    165 	const npf_tblent_t * const te2 = n2;
    166 
    167 	return npf_addr_cmp(&te1->te_addr, te1->te_mask,
    168 	    &te2->te_addr, te2->te_mask, sizeof(npf_addr_t));
    169 }
    170 
    171 static signed int
    172 table_rbtree_cmp_key(void *ctx, const void *n1, const void *key)
    173 {
    174 	const npf_tblent_t * const te = n1;
    175 	const npf_addr_t *t2 = key;
    176 
    177 	return npf_addr_cmp(&te->te_addr, te->te_mask,
    178 	    t2, NPF_NO_NETMASK, sizeof(npf_addr_t));
    179 }
    180 
    181 static const rb_tree_ops_t table_rbtree_ops = {
    182 	.rbto_compare_nodes = table_rbtree_cmp_nodes,
    183 	.rbto_compare_key = table_rbtree_cmp_key,
    184 	.rbto_node_offset = offsetof(npf_tblent_t, te_entry.rbnode),
    185 	.rbto_context = NULL
    186 };
    187 
    188 /*
    189  * Hash helper routine.
    190  */
    191 
    192 static inline struct npf_hashl *
    193 table_hash_bucket(npf_table_t *t, const void *buf, size_t sz)
    194 {
    195 	const uint32_t hidx = hash32_buf(buf, sz, HASH32_BUF_INIT);
    196 
    197 	return &t->t_hashl[hidx & t->t_hashmask];
    198 }
    199 
    200 /*
    201  * npf_table_create: create table with a specified ID.
    202  */
    203 npf_table_t *
    204 npf_table_create(u_int tid, int type, size_t hsize)
    205 {
    206 	npf_table_t *t;
    207 
    208 	KASSERT((u_int)tid < NPF_TABLE_SLOTS);
    209 
    210 	t = kmem_zalloc(sizeof(npf_table_t), KM_SLEEP);
    211 	switch (type) {
    212 	case NPF_TABLE_TREE:
    213 		rb_tree_init(&t->t_rbtree, &table_rbtree_ops);
    214 		break;
    215 	case NPF_TABLE_HASH:
    216 		t->t_hashl = hashinit(hsize, HASH_LIST, true, &t->t_hashmask);
    217 		if (t->t_hashl == NULL) {
    218 			kmem_free(t, sizeof(npf_table_t));
    219 			return NULL;
    220 		}
    221 		break;
    222 	default:
    223 		KASSERT(false);
    224 	}
    225 	rw_init(&t->t_lock);
    226 	t->t_type = type;
    227 	t->t_refcnt = 1;
    228 	t->t_id = tid;
    229 	return t;
    230 }
    231 
    232 /*
    233  * npf_table_destroy: free all table entries and table itself.
    234  */
    235 void
    236 npf_table_destroy(npf_table_t *t)
    237 {
    238 	npf_tblent_t *e;
    239 	u_int n;
    240 
    241 	switch (t->t_type) {
    242 	case NPF_TABLE_HASH:
    243 		for (n = 0; n <= t->t_hashmask; n++) {
    244 			while ((e = LIST_FIRST(&t->t_hashl[n])) != NULL) {
    245 				LIST_REMOVE(e, te_entry.hashq);
    246 				pool_cache_put(tblent_cache, e);
    247 			}
    248 		}
    249 		hashdone(t->t_hashl, HASH_LIST, t->t_hashmask);
    250 		break;
    251 	case NPF_TABLE_TREE:
    252 		while ((e = rb_tree_iterate(&t->t_rbtree, NULL,
    253 		    RB_DIR_LEFT)) != NULL) {
    254 			rb_tree_remove_node(&t->t_rbtree, e);
    255 			pool_cache_put(tblent_cache, e);
    256 		}
    257 		break;
    258 	default:
    259 		KASSERT(false);
    260 	}
    261 	rw_destroy(&t->t_lock);
    262 	kmem_free(t, sizeof(npf_table_t));
    263 }
    264 
    265 /*
    266  * npf_table_ref: holds the reference on table.
    267  *
    268  * => Table must be locked.
    269  */
    270 void
    271 npf_table_ref(npf_table_t *t)
    272 {
    273 
    274 	KASSERT(rw_lock_held(&t->t_lock));
    275 	atomic_inc_uint(&t->t_refcnt);
    276 }
    277 
    278 /*
    279  * npf_table_unref: drop reference from the table and destroy the table if
    280  * it is the last reference.
    281  */
    282 void
    283 npf_table_unref(npf_table_t *t)
    284 {
    285 
    286 	if (atomic_dec_uint_nv(&t->t_refcnt) != 0) {
    287 		return;
    288 	}
    289 	npf_table_destroy(t);
    290 }
    291 
    292 /*
    293  * npf_table_get: find the table according to ID and "get it" by locking it.
    294  */
    295 npf_table_t *
    296 npf_table_get(npf_tableset_t *tset, u_int tid)
    297 {
    298 	npf_table_t *t;
    299 
    300 	KASSERT(tset != NULL);
    301 
    302 	if ((u_int)tid >= NPF_TABLE_SLOTS) {
    303 		return NULL;
    304 	}
    305 	t = tset[tid];
    306 	if (t != NULL) {
    307 		rw_enter(&t->t_lock, RW_READER);
    308 	}
    309 	return t;
    310 }
    311 
    312 /*
    313  * npf_table_put: "put table back" by unlocking it.
    314  */
    315 void
    316 npf_table_put(npf_table_t *t)
    317 {
    318 
    319 	rw_exit(&t->t_lock);
    320 }
    321 
    322 /*
    323  * npf_table_check: validate ID and type.
    324  * */
    325 int
    326 npf_table_check(npf_tableset_t *tset, u_int tid, int type)
    327 {
    328 
    329 	if ((u_int)tid >= NPF_TABLE_SLOTS) {
    330 		return EINVAL;
    331 	}
    332 	if (tset[tid] != NULL) {
    333 		return EEXIST;
    334 	}
    335 	if (type != NPF_TABLE_TREE && type != NPF_TABLE_HASH) {
    336 		return EINVAL;
    337 	}
    338 	return 0;
    339 }
    340 
    341 /*
    342  * npf_table_add_cidr: add an IP CIDR into the table.
    343  */
    344 int
    345 npf_table_add_cidr(npf_tableset_t *tset, u_int tid,
    346     const npf_addr_t *addr, const npf_netmask_t mask)
    347 {
    348 	struct npf_hashl *htbl;
    349 	npf_tblent_t *ent, *it;
    350 	npf_table_t *t;
    351 	npf_addr_t val;
    352 	int error = 0;
    353 
    354 	if (mask > NPF_MAX_NETMASK) {
    355 		return EINVAL;
    356 	}
    357 	ent = pool_cache_get(tblent_cache, PR_WAITOK);
    358 	memcpy(&ent->te_addr, addr, sizeof(npf_addr_t));
    359 	ent->te_mask = mask;
    360 
    361 	/* Get the table (acquire the lock). */
    362 	t = npf_table_get(tset, tid);
    363 	if (t == NULL) {
    364 		pool_cache_put(tblent_cache, ent);
    365 		return EINVAL;
    366 	}
    367 
    368 	switch (t->t_type) {
    369 	case NPF_TABLE_HASH:
    370 		/* Generate hash value from: address & mask. */
    371 		npf_addr_mask(addr, mask, sizeof(npf_addr_t), &val);
    372 		htbl = table_hash_bucket(t, &val, sizeof(npf_addr_t));
    373 
    374 		/* Lookup to check for duplicates. */
    375 		LIST_FOREACH(it, htbl, te_entry.hashq) {
    376 			if (it->te_mask != mask) {
    377 				continue;
    378 			}
    379 			if (!memcmp(&it->te_addr, addr, sizeof(npf_addr_t))) {
    380 				break;
    381 			}
    382 		}
    383 
    384 		/* If no duplicate - insert entry. */
    385 		if (__predict_true(it == NULL)) {
    386 			LIST_INSERT_HEAD(htbl, ent, te_entry.hashq);
    387 		} else {
    388 			error = EEXIST;
    389 		}
    390 		break;
    391 	case NPF_TABLE_TREE:
    392 		/* Insert entry.  Returns false, if duplicate. */
    393 		if (rb_tree_insert_node(&t->t_rbtree, ent) != ent) {
    394 			error = EEXIST;
    395 		}
    396 		break;
    397 	default:
    398 		KASSERT(false);
    399 	}
    400 	npf_table_put(t);
    401 
    402 	if (error) {
    403 		pool_cache_put(tblent_cache, ent);
    404 	}
    405 	return error;
    406 }
    407 
    408 /*
    409  * npf_table_rem_cidr: remove an IP CIDR from the table.
    410  */
    411 int
    412 npf_table_rem_cidr(npf_tableset_t *tset, u_int tid,
    413     const npf_addr_t *addr, const npf_netmask_t mask)
    414 {
    415 	struct npf_hashl *htbl;
    416 	npf_tblent_t *ent;
    417 	npf_table_t *t;
    418 	npf_addr_t val;
    419 
    420 	if (mask > NPF_MAX_NETMASK) {
    421 		return EINVAL;
    422 	}
    423 
    424 	/* Get the table (acquire the lock). */
    425 	t = npf_table_get(tset, tid);
    426 	if (__predict_false(t == NULL)) {
    427 		return EINVAL;
    428 	}
    429 
    430 	/* Key: (address & mask). */
    431 	npf_addr_mask(addr, mask, sizeof(npf_addr_t), &val);
    432 	ent = NULL;
    433 
    434 	switch (t->t_type) {
    435 	case NPF_TABLE_HASH:
    436 		/* Generate hash value from: (address & mask). */
    437 		htbl = table_hash_bucket(t, &val, sizeof(npf_addr_t));
    438 		LIST_FOREACH(ent, htbl, te_entry.hashq) {
    439 			if (ent->te_mask != mask) {
    440 				continue;
    441 			}
    442 			if (!memcmp(&ent->te_addr, addr, sizeof(npf_addr_t))) {
    443 				break;
    444 			}
    445 		}
    446 		if (__predict_true(ent != NULL)) {
    447 			LIST_REMOVE(ent, te_entry.hashq);
    448 		}
    449 		break;
    450 	case NPF_TABLE_TREE:
    451 		ent = rb_tree_find_node(&t->t_rbtree, &val);
    452 		if (__predict_true(ent != NULL)) {
    453 			rb_tree_remove_node(&t->t_rbtree, ent);
    454 		}
    455 		break;
    456 	default:
    457 		KASSERT(false);
    458 	}
    459 	npf_table_put(t);
    460 
    461 	if (ent == NULL) {
    462 		return ENOENT;
    463 	}
    464 	pool_cache_put(tblent_cache, ent);
    465 	return 0;
    466 }
    467 
    468 /*
    469  * npf_table_match_addr: find the table according to ID, lookup and
    470  * match the contents with specified IPv4 address.
    471  */
    472 int
    473 npf_table_match_addr(npf_tableset_t *tset, u_int tid, const npf_addr_t *addr)
    474 {
    475 	struct npf_hashl *htbl;
    476 	npf_tblent_t *ent = NULL;
    477 	npf_table_t *t;
    478 
    479 	/* Get the table (acquire the lock). */
    480 	t = npf_table_get(tset, tid);
    481 	if (__predict_false(t == NULL)) {
    482 		return EINVAL;
    483 	}
    484 	switch (t->t_type) {
    485 	case NPF_TABLE_HASH:
    486 		htbl = table_hash_bucket(t, addr, sizeof(npf_addr_t));
    487 		LIST_FOREACH(ent, htbl, te_entry.hashq) {
    488 			if (npf_addr_cmp(addr, ent->te_mask, &ent->te_addr,
    489 			    NPF_NO_NETMASK, sizeof(npf_addr_t)) == 0) {
    490 				break;
    491 			}
    492 		}
    493 		break;
    494 	case NPF_TABLE_TREE:
    495 		ent = rb_tree_find_node(&t->t_rbtree, addr);
    496 		break;
    497 	default:
    498 		KASSERT(false);
    499 	}
    500 	npf_table_put(t);
    501 
    502 	if (ent == NULL) {
    503 		return ENOENT;
    504 	}
    505 	KASSERT(npf_addr_cmp(addr, ent->te_mask, &ent->te_addr,
    506 	    NPF_NO_NETMASK, sizeof(npf_addr_t)) == 0);
    507 	return 0;
    508 }
    509