Home | History | Annotate | Line # | Download | only in npf
npf_tableset.c revision 1.9.2.7
      1  1.9.2.7     riz /*	$NetBSD: npf_tableset.c,v 1.9.2.7 2012/12/11 04:31:53 riz Exp $	*/
      2      1.1   rmind 
      3      1.1   rmind /*-
      4      1.9   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.4   rmind  * NPF tableset module.
     34      1.1   rmind  *
     35  1.9.2.6     riz  * Notes
     36  1.9.2.6     riz  *
     37  1.9.2.6     riz  *	The tableset is an array of tables.  After the creation, the array
     38  1.9.2.6     riz  *	is immutable.  The caller is responsible to synchronise the access
     39  1.9.2.6     riz  *	to the tableset.  The table can either be a hash or a tree.  Its
     40  1.9.2.6     riz  *	entries are protected by a read-write lock.
     41      1.1   rmind  */
     42      1.1   rmind 
     43      1.1   rmind #include <sys/cdefs.h>
     44  1.9.2.7     riz __KERNEL_RCSID(0, "$NetBSD: npf_tableset.c,v 1.9.2.7 2012/12/11 04:31:53 riz Exp $");
     45      1.1   rmind 
     46      1.1   rmind #include <sys/param.h>
     47  1.9.2.1     riz #include <sys/types.h>
     48      1.1   rmind 
     49      1.1   rmind #include <sys/atomic.h>
     50      1.1   rmind #include <sys/hash.h>
     51      1.1   rmind #include <sys/kmem.h>
     52      1.1   rmind #include <sys/pool.h>
     53      1.1   rmind #include <sys/queue.h>
     54      1.1   rmind #include <sys/rwlock.h>
     55      1.1   rmind #include <sys/systm.h>
     56      1.1   rmind #include <sys/types.h>
     57      1.1   rmind 
     58      1.1   rmind #include "npf_impl.h"
     59      1.1   rmind 
     60  1.9.2.4     riz /*
     61  1.9.2.5     riz  * Table structures.
     62  1.9.2.4     riz  */
     63  1.9.2.4     riz 
     64  1.9.2.6     riz typedef struct npf_tblent {
     65      1.1   rmind 	union {
     66  1.9.2.4     riz 		LIST_ENTRY(npf_tblent) hashq;
     67  1.9.2.4     riz 		pt_node_t	node;
     68      1.1   rmind 	} te_entry;
     69  1.9.2.4     riz 	int			te_alen;
     70  1.9.2.4     riz 	npf_addr_t		te_addr;
     71  1.9.2.6     riz } npf_tblent_t;
     72      1.1   rmind 
     73      1.1   rmind LIST_HEAD(npf_hashl, npf_tblent);
     74      1.1   rmind 
     75      1.1   rmind struct npf_table {
     76  1.9.2.4     riz 	char			t_name[16];
     77      1.1   rmind 	/* Lock and reference count. */
     78  1.9.2.4     riz 	krwlock_t		t_lock;
     79  1.9.2.4     riz 	u_int			t_refcnt;
     80  1.9.2.6     riz 	/* Total number of items. */
     81  1.9.2.6     riz 	u_int			t_nitems;
     82      1.1   rmind 	/* Table ID. */
     83  1.9.2.4     riz 	u_int			t_id;
     84  1.9.2.5     riz 	/* The storage type can be: a) hash b) tree. */
     85  1.9.2.4     riz 	int			t_type;
     86  1.9.2.4     riz 	struct npf_hashl *	t_hashl;
     87  1.9.2.4     riz 	u_long			t_hashmask;
     88  1.9.2.6     riz 	/* Separate trees for IPv4 and IPv6. */
     89  1.9.2.4     riz 	pt_tree_t		t_tree[2];
     90      1.1   rmind };
     91      1.1   rmind 
     92  1.9.2.4     riz #define	NPF_ADDRLEN2TREE(alen)	((alen) >> 4)
     93  1.9.2.4     riz 
     94  1.9.2.4     riz static pool_cache_t		tblent_cache	__read_mostly;
     95      1.1   rmind 
     96      1.1   rmind /*
     97      1.1   rmind  * npf_table_sysinit: initialise tableset structures.
     98      1.1   rmind  */
     99      1.4   rmind void
    100      1.1   rmind npf_tableset_sysinit(void)
    101      1.1   rmind {
    102      1.1   rmind 
    103      1.1   rmind 	tblent_cache = pool_cache_init(sizeof(npf_tblent_t), coherency_unit,
    104  1.9.2.5     riz 	    0, 0, "npftblpl", NULL, IPL_NONE, NULL, NULL, NULL);
    105      1.1   rmind }
    106      1.1   rmind 
    107      1.1   rmind void
    108      1.1   rmind npf_tableset_sysfini(void)
    109      1.1   rmind {
    110      1.1   rmind 
    111      1.1   rmind 	pool_cache_destroy(tblent_cache);
    112      1.1   rmind }
    113      1.1   rmind 
    114      1.1   rmind npf_tableset_t *
    115      1.1   rmind npf_tableset_create(void)
    116      1.1   rmind {
    117      1.1   rmind 	const size_t sz = NPF_TABLE_SLOTS * sizeof(npf_table_t *);
    118      1.1   rmind 
    119      1.1   rmind 	return kmem_zalloc(sz, KM_SLEEP);
    120      1.1   rmind }
    121      1.1   rmind 
    122      1.1   rmind void
    123      1.1   rmind npf_tableset_destroy(npf_tableset_t *tblset)
    124      1.1   rmind {
    125      1.1   rmind 	const size_t sz = NPF_TABLE_SLOTS * sizeof(npf_table_t *);
    126      1.1   rmind 	npf_table_t *t;
    127      1.1   rmind 	u_int tid;
    128      1.1   rmind 
    129      1.1   rmind 	/*
    130      1.1   rmind 	 * Destroy all tables (no references should be held, as ruleset
    131      1.1   rmind 	 * should be destroyed before).
    132      1.1   rmind 	 */
    133      1.1   rmind 	for (tid = 0; tid < NPF_TABLE_SLOTS; tid++) {
    134      1.1   rmind 		t = tblset[tid];
    135  1.9.2.6     riz 		if (t && --t->t_refcnt == 0) {
    136      1.1   rmind 			npf_table_destroy(t);
    137      1.1   rmind 		}
    138      1.1   rmind 	}
    139      1.1   rmind 	kmem_free(tblset, sz);
    140      1.1   rmind }
    141      1.1   rmind 
    142      1.1   rmind /*
    143      1.1   rmind  * npf_tableset_insert: insert the table into the specified tableset.
    144      1.1   rmind  *
    145  1.9.2.4     riz  * => Returns 0 on success.  Fails and returns error if ID is already used.
    146      1.1   rmind  */
    147      1.1   rmind int
    148      1.1   rmind npf_tableset_insert(npf_tableset_t *tblset, npf_table_t *t)
    149      1.1   rmind {
    150      1.1   rmind 	const u_int tid = t->t_id;
    151      1.1   rmind 	int error;
    152      1.1   rmind 
    153      1.1   rmind 	KASSERT((u_int)tid < NPF_TABLE_SLOTS);
    154      1.1   rmind 
    155      1.1   rmind 	if (tblset[tid] == NULL) {
    156      1.1   rmind 		tblset[tid] = t;
    157  1.9.2.6     riz 		t->t_refcnt++;
    158      1.1   rmind 		error = 0;
    159      1.1   rmind 	} else {
    160      1.1   rmind 		error = EEXIST;
    161      1.1   rmind 	}
    162      1.1   rmind 	return error;
    163      1.1   rmind }
    164      1.1   rmind 
    165      1.1   rmind /*
    166  1.9.2.6     riz  * npf_tableset_reload: iterate all tables and if the new table is of the
    167  1.9.2.6     riz  * same type and has no items, then we preserve the old one and its entries.
    168  1.9.2.6     riz  *
    169  1.9.2.6     riz  * => The caller is responsible for providing synchronisation.
    170  1.9.2.6     riz  */
    171  1.9.2.6     riz void
    172  1.9.2.6     riz npf_tableset_reload(npf_tableset_t *ntset, npf_tableset_t *otset)
    173  1.9.2.6     riz {
    174  1.9.2.6     riz 	for (int i = 0; i < NPF_TABLE_SLOTS; i++) {
    175  1.9.2.6     riz 		npf_table_t *t = ntset[i], *ot = otset[i];
    176  1.9.2.6     riz 
    177  1.9.2.6     riz 		if (t == NULL || ot == NULL) {
    178  1.9.2.6     riz 			continue;
    179  1.9.2.6     riz 		}
    180  1.9.2.6     riz 		if (t->t_nitems || t->t_type != ot->t_type) {
    181  1.9.2.6     riz 			continue;
    182  1.9.2.6     riz 		}
    183  1.9.2.6     riz 		ntset[i] = ot;
    184  1.9.2.6     riz 		ot->t_refcnt++;
    185  1.9.2.6     riz 		npf_table_destroy(t);
    186  1.9.2.6     riz 	}
    187  1.9.2.6     riz }
    188  1.9.2.6     riz 
    189  1.9.2.6     riz /*
    190  1.9.2.4     riz  * Few helper routines.
    191      1.1   rmind  */
    192      1.1   rmind 
    193  1.9.2.4     riz static npf_tblent_t *
    194  1.9.2.4     riz table_hash_lookup(const npf_table_t *t, const npf_addr_t *addr,
    195  1.9.2.4     riz     const int alen, struct npf_hashl **rhtbl)
    196      1.1   rmind {
    197  1.9.2.4     riz 	const uint32_t hidx = hash32_buf(addr, alen, HASH32_BUF_INIT);
    198  1.9.2.4     riz 	struct npf_hashl *htbl = &t->t_hashl[hidx & t->t_hashmask];
    199  1.9.2.4     riz 	npf_tblent_t *ent;
    200      1.1   rmind 
    201  1.9.2.4     riz 	/*
    202  1.9.2.4     riz 	 * Lookup the hash table and check for duplicates.
    203  1.9.2.4     riz 	 * Note: mask is ignored for the hash storage.
    204  1.9.2.4     riz 	 */
    205  1.9.2.4     riz 	LIST_FOREACH(ent, htbl, te_entry.hashq) {
    206  1.9.2.4     riz 		if (ent->te_alen != alen) {
    207  1.9.2.4     riz 			continue;
    208  1.9.2.4     riz 		}
    209  1.9.2.4     riz 		if (memcmp(&ent->te_addr, addr, alen) == 0) {
    210  1.9.2.4     riz 			break;
    211  1.9.2.4     riz 		}
    212  1.9.2.4     riz 	}
    213  1.9.2.4     riz 	*rhtbl = htbl;
    214  1.9.2.4     riz 	return ent;
    215      1.1   rmind }
    216      1.1   rmind 
    217  1.9.2.4     riz static void
    218  1.9.2.4     riz table_tree_destroy(pt_tree_t *tree)
    219      1.1   rmind {
    220  1.9.2.4     riz 	npf_tblent_t *ent;
    221      1.1   rmind 
    222  1.9.2.4     riz 	while ((ent = ptree_iterate(tree, NULL, PT_ASCENDING)) != NULL) {
    223  1.9.2.4     riz 		ptree_remove_node(tree, ent);
    224  1.9.2.4     riz 		pool_cache_put(tblent_cache, ent);
    225  1.9.2.4     riz 	}
    226      1.1   rmind }
    227      1.1   rmind 
    228      1.1   rmind /*
    229      1.1   rmind  * npf_table_create: create table with a specified ID.
    230      1.1   rmind  */
    231      1.1   rmind npf_table_t *
    232      1.1   rmind npf_table_create(u_int tid, int type, size_t hsize)
    233      1.1   rmind {
    234      1.1   rmind 	npf_table_t *t;
    235      1.1   rmind 
    236      1.1   rmind 	KASSERT((u_int)tid < NPF_TABLE_SLOTS);
    237      1.1   rmind 
    238      1.1   rmind 	t = kmem_zalloc(sizeof(npf_table_t), KM_SLEEP);
    239      1.1   rmind 	switch (type) {
    240      1.9   rmind 	case NPF_TABLE_TREE:
    241  1.9.2.4     riz 		ptree_init(&t->t_tree[0], &npf_table_ptree_ops,
    242  1.9.2.4     riz 		    (void *)(sizeof(struct in_addr) / sizeof(uint32_t)),
    243  1.9.2.4     riz 		    offsetof(npf_tblent_t, te_entry.node),
    244  1.9.2.4     riz 		    offsetof(npf_tblent_t, te_addr));
    245  1.9.2.4     riz 		ptree_init(&t->t_tree[1], &npf_table_ptree_ops,
    246  1.9.2.4     riz 		    (void *)(sizeof(struct in6_addr) / sizeof(uint32_t)),
    247  1.9.2.4     riz 		    offsetof(npf_tblent_t, te_entry.node),
    248  1.9.2.4     riz 		    offsetof(npf_tblent_t, te_addr));
    249      1.1   rmind 		break;
    250      1.1   rmind 	case NPF_TABLE_HASH:
    251      1.1   rmind 		t->t_hashl = hashinit(hsize, HASH_LIST, true, &t->t_hashmask);
    252      1.1   rmind 		if (t->t_hashl == NULL) {
    253      1.1   rmind 			kmem_free(t, sizeof(npf_table_t));
    254      1.1   rmind 			return NULL;
    255      1.1   rmind 		}
    256      1.1   rmind 		break;
    257      1.1   rmind 	default:
    258      1.1   rmind 		KASSERT(false);
    259      1.1   rmind 	}
    260      1.1   rmind 	rw_init(&t->t_lock);
    261      1.1   rmind 	t->t_type = type;
    262      1.1   rmind 	t->t_id = tid;
    263  1.9.2.6     riz 
    264      1.1   rmind 	return t;
    265      1.1   rmind }
    266      1.1   rmind 
    267      1.1   rmind /*
    268      1.1   rmind  * npf_table_destroy: free all table entries and table itself.
    269      1.1   rmind  */
    270      1.1   rmind void
    271      1.1   rmind npf_table_destroy(npf_table_t *t)
    272      1.1   rmind {
    273      1.1   rmind 
    274      1.1   rmind 	switch (t->t_type) {
    275  1.9.2.6     riz 	case NPF_TABLE_HASH:
    276  1.9.2.4     riz 		for (unsigned n = 0; n <= t->t_hashmask; n++) {
    277  1.9.2.4     riz 			npf_tblent_t *ent;
    278  1.9.2.4     riz 
    279  1.9.2.4     riz 			while ((ent = LIST_FIRST(&t->t_hashl[n])) != NULL) {
    280  1.9.2.4     riz 				LIST_REMOVE(ent, te_entry.hashq);
    281  1.9.2.4     riz 				pool_cache_put(tblent_cache, ent);
    282      1.1   rmind 			}
    283      1.1   rmind 		}
    284      1.1   rmind 		hashdone(t->t_hashl, HASH_LIST, t->t_hashmask);
    285      1.1   rmind 		break;
    286  1.9.2.6     riz 	case NPF_TABLE_TREE:
    287  1.9.2.4     riz 		table_tree_destroy(&t->t_tree[0]);
    288  1.9.2.4     riz 		table_tree_destroy(&t->t_tree[1]);
    289      1.1   rmind 		break;
    290      1.1   rmind 	default:
    291      1.1   rmind 		KASSERT(false);
    292      1.1   rmind 	}
    293      1.1   rmind 	rw_destroy(&t->t_lock);
    294      1.1   rmind 	kmem_free(t, sizeof(npf_table_t));
    295      1.1   rmind }
    296      1.1   rmind 
    297      1.1   rmind /*
    298      1.1   rmind  * npf_table_check: validate ID and type.
    299  1.9.2.4     riz  */
    300      1.1   rmind int
    301  1.9.2.4     riz npf_table_check(const npf_tableset_t *tset, u_int tid, int type)
    302      1.1   rmind {
    303      1.1   rmind 
    304      1.1   rmind 	if ((u_int)tid >= NPF_TABLE_SLOTS) {
    305      1.1   rmind 		return EINVAL;
    306      1.1   rmind 	}
    307      1.1   rmind 	if (tset[tid] != NULL) {
    308      1.1   rmind 		return EEXIST;
    309      1.1   rmind 	}
    310      1.9   rmind 	if (type != NPF_TABLE_TREE && type != NPF_TABLE_HASH) {
    311      1.1   rmind 		return EINVAL;
    312      1.1   rmind 	}
    313      1.1   rmind 	return 0;
    314      1.1   rmind }
    315      1.1   rmind 
    316  1.9.2.4     riz static int
    317  1.9.2.6     riz table_cidr_check(const u_int aidx, const npf_addr_t *addr,
    318  1.9.2.4     riz     const npf_netmask_t mask)
    319  1.9.2.4     riz {
    320  1.9.2.4     riz 
    321  1.9.2.4     riz 	if (mask > NPF_MAX_NETMASK && mask != NPF_NO_NETMASK) {
    322  1.9.2.4     riz 		return EINVAL;
    323  1.9.2.4     riz 	}
    324  1.9.2.4     riz 	if (aidx > 1) {
    325  1.9.2.4     riz 		return EINVAL;
    326  1.9.2.4     riz 	}
    327  1.9.2.4     riz 
    328  1.9.2.4     riz 	/*
    329  1.9.2.4     riz 	 * For IPv4 (aidx = 0) - 32 and for IPv6 (aidx = 1) - 128.
    330  1.9.2.4     riz 	 * If it is a host - shall use NPF_NO_NETMASK.
    331  1.9.2.4     riz 	 */
    332  1.9.2.4     riz 	if (mask >= (aidx ? 128 : 32) && mask != NPF_NO_NETMASK) {
    333  1.9.2.4     riz 		return EINVAL;
    334  1.9.2.4     riz 	}
    335  1.9.2.4     riz 	return 0;
    336  1.9.2.4     riz }
    337  1.9.2.4     riz 
    338      1.1   rmind /*
    339  1.9.2.4     riz  * npf_table_insert: add an IP CIDR entry into the table.
    340      1.1   rmind  */
    341      1.1   rmind int
    342  1.9.2.4     riz npf_table_insert(npf_tableset_t *tset, u_int tid, const int alen,
    343      1.6  zoltan     const npf_addr_t *addr, const npf_netmask_t mask)
    344      1.1   rmind {
    345  1.9.2.4     riz 	const u_int aidx = NPF_ADDRLEN2TREE(alen);
    346  1.9.2.4     riz 	npf_tblent_t *ent;
    347      1.1   rmind 	npf_table_t *t;
    348  1.9.2.4     riz 	int error;
    349      1.1   rmind 
    350  1.9.2.6     riz 	if ((u_int)tid >= NPF_TABLE_SLOTS || (t = tset[tid]) == NULL) {
    351  1.9.2.6     riz 		return EINVAL;
    352  1.9.2.6     riz 	}
    353  1.9.2.6     riz 
    354  1.9.2.6     riz 	error = table_cidr_check(aidx, addr, mask);
    355  1.9.2.4     riz 	if (error) {
    356  1.9.2.4     riz 		return error;
    357      1.8   rmind 	}
    358  1.9.2.3     riz 	ent = pool_cache_get(tblent_cache, PR_WAITOK);
    359  1.9.2.4     riz 	memcpy(&ent->te_addr, addr, alen);
    360  1.9.2.4     riz 	ent->te_alen = alen;
    361      1.1   rmind 
    362  1.9.2.4     riz 	/*
    363  1.9.2.4     riz 	 * Insert the entry.  Return an error on duplicate.
    364  1.9.2.4     riz 	 */
    365  1.9.2.6     riz 	rw_enter(&t->t_lock, RW_WRITER);
    366      1.1   rmind 	switch (t->t_type) {
    367  1.9.2.4     riz 	case NPF_TABLE_HASH: {
    368  1.9.2.4     riz 		struct npf_hashl *htbl;
    369  1.9.2.3     riz 
    370  1.9.2.4     riz 		/*
    371  1.9.2.4     riz 		 * Hash tables by the concept support only IPs.
    372  1.9.2.4     riz 		 */
    373  1.9.2.4     riz 		if (mask != NPF_NO_NETMASK) {
    374  1.9.2.4     riz 			error = EINVAL;
    375  1.9.2.4     riz 			break;
    376  1.9.2.4     riz 		}
    377  1.9.2.4     riz 		if (!table_hash_lookup(t, addr, alen, &htbl)) {
    378  1.9.2.3     riz 			LIST_INSERT_HEAD(htbl, ent, te_entry.hashq);
    379  1.9.2.6     riz 			t->t_nitems++;
    380      1.1   rmind 		} else {
    381      1.1   rmind 			error = EEXIST;
    382      1.1   rmind 		}
    383      1.1   rmind 		break;
    384  1.9.2.4     riz 	}
    385  1.9.2.4     riz 	case NPF_TABLE_TREE: {
    386  1.9.2.4     riz 		pt_tree_t *tree = &t->t_tree[aidx];
    387  1.9.2.4     riz 		bool ok;
    388  1.9.2.4     riz 
    389  1.9.2.4     riz 		/*
    390  1.9.2.4     riz 		 * If no mask specified, use maximum mask.
    391  1.9.2.4     riz 		 */
    392  1.9.2.6     riz 		ok = (mask != NPF_NO_NETMASK) ?
    393  1.9.2.6     riz 		    ptree_insert_mask_node(tree, ent, mask) :
    394  1.9.2.6     riz 		    ptree_insert_node(tree, ent);
    395  1.9.2.6     riz 		if (ok) {
    396  1.9.2.6     riz 			t->t_nitems++;
    397  1.9.2.6     riz 			error = 0;
    398  1.9.2.4     riz 		} else {
    399  1.9.2.6     riz 			error = EEXIST;
    400      1.1   rmind 		}
    401      1.1   rmind 		break;
    402  1.9.2.4     riz 	}
    403      1.1   rmind 	default:
    404      1.1   rmind 		KASSERT(false);
    405      1.1   rmind 	}
    406  1.9.2.6     riz 	rw_exit(&t->t_lock);
    407      1.1   rmind 
    408      1.8   rmind 	if (error) {
    409  1.9.2.3     riz 		pool_cache_put(tblent_cache, ent);
    410      1.1   rmind 	}
    411      1.1   rmind 	return error;
    412      1.1   rmind }
    413      1.1   rmind 
    414      1.1   rmind /*
    415  1.9.2.4     riz  * npf_table_remove: remove the IP CIDR entry from the table.
    416      1.1   rmind  */
    417      1.1   rmind int
    418  1.9.2.4     riz npf_table_remove(npf_tableset_t *tset, u_int tid, const int alen,
    419      1.6  zoltan     const npf_addr_t *addr, const npf_netmask_t mask)
    420      1.1   rmind {
    421  1.9.2.4     riz 	const u_int aidx = NPF_ADDRLEN2TREE(alen);
    422  1.9.2.3     riz 	npf_tblent_t *ent;
    423      1.1   rmind 	npf_table_t *t;
    424  1.9.2.4     riz 	int error;
    425      1.1   rmind 
    426  1.9.2.6     riz 	error = table_cidr_check(aidx, addr, mask);
    427  1.9.2.4     riz 	if (error) {
    428  1.9.2.4     riz 		return error;
    429      1.8   rmind 	}
    430  1.9.2.6     riz 
    431  1.9.2.6     riz 	if ((u_int)tid >= NPF_TABLE_SLOTS || (t = tset[tid]) == NULL) {
    432      1.1   rmind 		return EINVAL;
    433      1.1   rmind 	}
    434  1.9.2.3     riz 
    435  1.9.2.6     riz 	rw_enter(&t->t_lock, RW_WRITER);
    436      1.1   rmind 	switch (t->t_type) {
    437  1.9.2.4     riz 	case NPF_TABLE_HASH: {
    438  1.9.2.4     riz 		struct npf_hashl *htbl;
    439  1.9.2.4     riz 
    440  1.9.2.4     riz 		ent = table_hash_lookup(t, addr, alen, &htbl);
    441  1.9.2.3     riz 		if (__predict_true(ent != NULL)) {
    442  1.9.2.3     riz 			LIST_REMOVE(ent, te_entry.hashq);
    443  1.9.2.6     riz 			t->t_nitems--;
    444      1.1   rmind 		}
    445      1.1   rmind 		break;
    446  1.9.2.4     riz 	}
    447  1.9.2.4     riz 	case NPF_TABLE_TREE: {
    448  1.9.2.4     riz 		pt_tree_t *tree = &t->t_tree[aidx];
    449  1.9.2.4     riz 
    450  1.9.2.4     riz 		ent = ptree_find_node(tree, addr);
    451  1.9.2.3     riz 		if (__predict_true(ent != NULL)) {
    452  1.9.2.4     riz 			ptree_remove_node(tree, ent);
    453  1.9.2.6     riz 			t->t_nitems--;
    454      1.1   rmind 		}
    455      1.1   rmind 		break;
    456  1.9.2.4     riz 	}
    457      1.1   rmind 	default:
    458      1.1   rmind 		KASSERT(false);
    459  1.9.2.4     riz 		ent = NULL;
    460      1.1   rmind 	}
    461  1.9.2.6     riz 	rw_exit(&t->t_lock);
    462      1.1   rmind 
    463  1.9.2.3     riz 	if (ent == NULL) {
    464      1.8   rmind 		return ENOENT;
    465      1.1   rmind 	}
    466  1.9.2.3     riz 	pool_cache_put(tblent_cache, ent);
    467      1.8   rmind 	return 0;
    468      1.1   rmind }
    469      1.1   rmind 
    470      1.1   rmind /*
    471  1.9.2.4     riz  * npf_table_lookup: find the table according to ID, lookup and match
    472  1.9.2.4     riz  * the contents with the specified IP address.
    473      1.1   rmind  */
    474      1.1   rmind int
    475  1.9.2.4     riz npf_table_lookup(npf_tableset_t *tset, u_int tid,
    476  1.9.2.4     riz     const int alen, const npf_addr_t *addr)
    477      1.1   rmind {
    478  1.9.2.4     riz 	const u_int aidx = NPF_ADDRLEN2TREE(alen);
    479  1.9.2.4     riz 	npf_tblent_t *ent;
    480      1.1   rmind 	npf_table_t *t;
    481      1.1   rmind 
    482  1.9.2.4     riz 	if (__predict_false(aidx > 1)) {
    483  1.9.2.4     riz 		return EINVAL;
    484  1.9.2.4     riz 	}
    485  1.9.2.4     riz 
    486  1.9.2.6     riz 	if ((u_int)tid >= NPF_TABLE_SLOTS || (t = tset[tid]) == NULL) {
    487      1.1   rmind 		return EINVAL;
    488      1.1   rmind 	}
    489  1.9.2.6     riz 
    490  1.9.2.6     riz 	rw_enter(&t->t_lock, RW_READER);
    491      1.1   rmind 	switch (t->t_type) {
    492  1.9.2.4     riz 	case NPF_TABLE_HASH: {
    493  1.9.2.4     riz 		struct npf_hashl *htbl;
    494  1.9.2.4     riz 		ent = table_hash_lookup(t, addr, alen, &htbl);
    495      1.1   rmind 		break;
    496  1.9.2.4     riz 	}
    497  1.9.2.4     riz 	case NPF_TABLE_TREE: {
    498  1.9.2.4     riz 		ent = ptree_find_node(&t->t_tree[aidx], addr);
    499      1.1   rmind 		break;
    500  1.9.2.4     riz 	}
    501      1.1   rmind 	default:
    502      1.1   rmind 		KASSERT(false);
    503  1.9.2.4     riz 		ent = NULL;
    504      1.1   rmind 	}
    505  1.9.2.6     riz 	rw_exit(&t->t_lock);
    506      1.1   rmind 
    507  1.9.2.4     riz 	return ent ? 0 : ENOENT;
    508      1.1   rmind }
    509  1.9.2.6     riz 
    510  1.9.2.6     riz static int
    511  1.9.2.6     riz table_ent_copyout(npf_tblent_t *ent, npf_netmask_t mask,
    512  1.9.2.6     riz     void *ubuf, size_t len, size_t *off)
    513  1.9.2.6     riz {
    514  1.9.2.6     riz 	void *ubufp = (uint8_t *)ubuf + *off;
    515  1.9.2.6     riz 	npf_ioctl_ent_t uent;
    516  1.9.2.6     riz 
    517  1.9.2.6     riz 	if ((*off += sizeof(npf_ioctl_ent_t)) > len) {
    518  1.9.2.6     riz 		return ENOMEM;
    519  1.9.2.6     riz 	}
    520  1.9.2.6     riz 	uent.alen = ent->te_alen;
    521  1.9.2.6     riz 	memcpy(&uent.addr, &ent->te_addr, sizeof(npf_addr_t));
    522  1.9.2.6     riz 	uent.mask = mask;
    523  1.9.2.6     riz 
    524  1.9.2.6     riz 	return copyout(&uent, ubufp, sizeof(npf_ioctl_ent_t));
    525  1.9.2.6     riz }
    526  1.9.2.6     riz 
    527  1.9.2.6     riz static int
    528  1.9.2.6     riz table_tree_list(pt_tree_t *tree, npf_netmask_t maxmask, void *ubuf,
    529  1.9.2.6     riz     size_t len, size_t *off)
    530  1.9.2.6     riz {
    531  1.9.2.6     riz 	npf_tblent_t *ent = NULL;
    532  1.9.2.6     riz 	int error = 0;
    533  1.9.2.6     riz 
    534  1.9.2.6     riz 	while ((ent = ptree_iterate(tree, ent, PT_ASCENDING)) != NULL) {
    535  1.9.2.6     riz 		pt_bitlen_t blen;
    536  1.9.2.6     riz 
    537  1.9.2.6     riz 		if (!ptree_mask_node_p(tree, ent, &blen)) {
    538  1.9.2.6     riz 			blen = maxmask;
    539  1.9.2.6     riz 		}
    540  1.9.2.6     riz 		error = table_ent_copyout(ent, blen, ubuf, len, off);
    541  1.9.2.6     riz 		if (error)
    542  1.9.2.6     riz 			break;
    543  1.9.2.6     riz 	}
    544  1.9.2.6     riz 	return error;
    545  1.9.2.6     riz }
    546  1.9.2.6     riz 
    547  1.9.2.6     riz /*
    548  1.9.2.6     riz  * npf_table_list: copy a list of all table entries into a userspace buffer.
    549  1.9.2.6     riz  */
    550  1.9.2.6     riz int
    551  1.9.2.6     riz npf_table_list(npf_tableset_t *tset, u_int tid, void *ubuf, size_t len)
    552  1.9.2.6     riz {
    553  1.9.2.6     riz 	npf_table_t *t;
    554  1.9.2.6     riz 	size_t off = 0;
    555  1.9.2.6     riz 	int error = 0;
    556  1.9.2.6     riz 
    557  1.9.2.6     riz 	if ((u_int)tid >= NPF_TABLE_SLOTS || (t = tset[tid]) == NULL) {
    558  1.9.2.6     riz 		return EINVAL;
    559  1.9.2.6     riz 	}
    560  1.9.2.6     riz 
    561  1.9.2.6     riz 	rw_enter(&t->t_lock, RW_READER);
    562  1.9.2.6     riz 	switch (t->t_type) {
    563  1.9.2.6     riz 	case NPF_TABLE_HASH:
    564  1.9.2.6     riz 		for (unsigned n = 0; n <= t->t_hashmask; n++) {
    565  1.9.2.6     riz 			npf_tblent_t *ent;
    566  1.9.2.6     riz 
    567  1.9.2.6     riz 			LIST_FOREACH(ent, &t->t_hashl[n], te_entry.hashq)
    568  1.9.2.6     riz 				if ((error = table_ent_copyout(ent, 0, ubuf,
    569  1.9.2.6     riz 				    len, &off)) != 0)
    570  1.9.2.6     riz 					break;
    571  1.9.2.6     riz 		}
    572  1.9.2.6     riz 		break;
    573  1.9.2.6     riz 	case NPF_TABLE_TREE:
    574  1.9.2.6     riz 		error = table_tree_list(&t->t_tree[0], 32, ubuf, len, &off);
    575  1.9.2.6     riz 		if (error)
    576  1.9.2.6     riz 			break;
    577  1.9.2.6     riz 		error = table_tree_list(&t->t_tree[1], 128, ubuf, len, &off);
    578  1.9.2.7     riz 		break;
    579  1.9.2.6     riz 	default:
    580  1.9.2.6     riz 		KASSERT(false);
    581  1.9.2.6     riz 	}
    582  1.9.2.6     riz 	rw_exit(&t->t_lock);
    583  1.9.2.6     riz 
    584  1.9.2.6     riz 	return error;
    585  1.9.2.6     riz }
    586