Home | History | Annotate | Line # | Download | only in npf
npf_tableset.c revision 1.16
      1  1.16   rmind /*	$NetBSD: npf_tableset.c,v 1.16 2012/12/04 19:28:16 rmind 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.15   rmind  * Notes
     36  1.15   rmind  *
     37  1.15   rmind  *	The tableset is an array of tables.  After the creation, the array
     38  1.15   rmind  *	is immutable.  The caller is responsible to synchronise the access
     39  1.15   rmind  *	to the tableset.  The table can either be a hash or a tree.  Its
     40  1.15   rmind  *	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.16   rmind __KERNEL_RCSID(0, "$NetBSD: npf_tableset.c,v 1.16 2012/12/04 19:28:16 rmind Exp $");
     45   1.1   rmind 
     46   1.1   rmind #include <sys/param.h>
     47  1.10   rmind #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.13   rmind /*
     61  1.14   rmind  * Table structures.
     62  1.13   rmind  */
     63  1.13   rmind 
     64  1.15   rmind typedef struct npf_tblent {
     65   1.1   rmind 	union {
     66  1.13   rmind 		LIST_ENTRY(npf_tblent) hashq;
     67  1.13   rmind 		pt_node_t	node;
     68   1.1   rmind 	} te_entry;
     69  1.13   rmind 	int			te_alen;
     70  1.13   rmind 	npf_addr_t		te_addr;
     71  1.15   rmind } 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.13   rmind 	char			t_name[16];
     77   1.1   rmind 	/* Lock and reference count. */
     78  1.13   rmind 	krwlock_t		t_lock;
     79  1.13   rmind 	u_int			t_refcnt;
     80  1.15   rmind 	/* Total number of items. */
     81  1.15   rmind 	u_int			t_nitems;
     82   1.1   rmind 	/* Table ID. */
     83  1.13   rmind 	u_int			t_id;
     84  1.14   rmind 	/* The storage type can be: a) hash b) tree. */
     85  1.13   rmind 	int			t_type;
     86  1.13   rmind 	struct npf_hashl *	t_hashl;
     87  1.13   rmind 	u_long			t_hashmask;
     88  1.15   rmind 	/* Separate trees for IPv4 and IPv6. */
     89  1.13   rmind 	pt_tree_t		t_tree[2];
     90   1.1   rmind };
     91   1.1   rmind 
     92  1.13   rmind #define	NPF_ADDRLEN2TREE(alen)	((alen) >> 4)
     93  1.13   rmind 
     94  1.13   rmind 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.14   rmind 	    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.15   rmind 		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.13   rmind  * => 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.15   rmind 		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.15   rmind  * npf_tableset_reload: iterate all tables and if the new table is of the
    167  1.15   rmind  * same type and has no items, then we preserve the old one and its entries.
    168  1.15   rmind  *
    169  1.15   rmind  * => The caller is responsible for providing synchronisation.
    170  1.15   rmind  */
    171  1.15   rmind void
    172  1.15   rmind npf_tableset_reload(npf_tableset_t *ntset, npf_tableset_t *otset)
    173  1.15   rmind {
    174  1.15   rmind 	for (int i = 0; i < NPF_TABLE_SLOTS; i++) {
    175  1.15   rmind 		npf_table_t *t = ntset[i], *ot = otset[i];
    176  1.15   rmind 
    177  1.15   rmind 		if (t == NULL || ot == NULL) {
    178  1.15   rmind 			continue;
    179  1.15   rmind 		}
    180  1.15   rmind 		if (t->t_nitems || t->t_type != ot->t_type) {
    181  1.15   rmind 			continue;
    182  1.15   rmind 		}
    183  1.15   rmind 		ntset[i] = ot;
    184  1.15   rmind 		ot->t_refcnt++;
    185  1.15   rmind 		npf_table_destroy(t);
    186  1.15   rmind 	}
    187  1.15   rmind }
    188  1.15   rmind 
    189  1.15   rmind /*
    190  1.13   rmind  * Few helper routines.
    191   1.1   rmind  */
    192   1.1   rmind 
    193  1.13   rmind static npf_tblent_t *
    194  1.13   rmind table_hash_lookup(const npf_table_t *t, const npf_addr_t *addr,
    195  1.13   rmind     const int alen, struct npf_hashl **rhtbl)
    196   1.1   rmind {
    197  1.13   rmind 	const uint32_t hidx = hash32_buf(addr, alen, HASH32_BUF_INIT);
    198  1.13   rmind 	struct npf_hashl *htbl = &t->t_hashl[hidx & t->t_hashmask];
    199  1.13   rmind 	npf_tblent_t *ent;
    200   1.1   rmind 
    201  1.13   rmind 	/*
    202  1.13   rmind 	 * Lookup the hash table and check for duplicates.
    203  1.13   rmind 	 * Note: mask is ignored for the hash storage.
    204  1.13   rmind 	 */
    205  1.13   rmind 	LIST_FOREACH(ent, htbl, te_entry.hashq) {
    206  1.13   rmind 		if (ent->te_alen != alen) {
    207  1.13   rmind 			continue;
    208  1.13   rmind 		}
    209  1.13   rmind 		if (memcmp(&ent->te_addr, addr, alen) == 0) {
    210  1.13   rmind 			break;
    211  1.13   rmind 		}
    212  1.13   rmind 	}
    213  1.13   rmind 	*rhtbl = htbl;
    214  1.13   rmind 	return ent;
    215   1.1   rmind }
    216   1.1   rmind 
    217  1.13   rmind static void
    218  1.13   rmind table_tree_destroy(pt_tree_t *tree)
    219   1.1   rmind {
    220  1.13   rmind 	npf_tblent_t *ent;
    221   1.1   rmind 
    222  1.13   rmind 	while ((ent = ptree_iterate(tree, NULL, PT_ASCENDING)) != NULL) {
    223  1.13   rmind 		ptree_remove_node(tree, ent);
    224  1.13   rmind 		pool_cache_put(tblent_cache, ent);
    225  1.13   rmind 	}
    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.13   rmind 		ptree_init(&t->t_tree[0], &npf_table_ptree_ops,
    242  1.13   rmind 		    (void *)(sizeof(struct in_addr) / sizeof(uint32_t)),
    243  1.13   rmind 		    offsetof(npf_tblent_t, te_entry.node),
    244  1.13   rmind 		    offsetof(npf_tblent_t, te_addr));
    245  1.13   rmind 		ptree_init(&t->t_tree[1], &npf_table_ptree_ops,
    246  1.13   rmind 		    (void *)(sizeof(struct in6_addr) / sizeof(uint32_t)),
    247  1.13   rmind 		    offsetof(npf_tblent_t, te_entry.node),
    248  1.13   rmind 		    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.15   rmind 
    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.15   rmind 	case NPF_TABLE_HASH:
    276  1.13   rmind 		for (unsigned n = 0; n <= t->t_hashmask; n++) {
    277  1.13   rmind 			npf_tblent_t *ent;
    278  1.13   rmind 
    279  1.13   rmind 			while ((ent = LIST_FIRST(&t->t_hashl[n])) != NULL) {
    280  1.13   rmind 				LIST_REMOVE(ent, te_entry.hashq);
    281  1.13   rmind 				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.15   rmind 	case NPF_TABLE_TREE:
    287  1.13   rmind 		table_tree_destroy(&t->t_tree[0]);
    288  1.13   rmind 		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.13   rmind  */
    300   1.1   rmind int
    301  1.13   rmind 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.13   rmind static int
    317  1.15   rmind table_cidr_check(const u_int aidx, const npf_addr_t *addr,
    318  1.13   rmind     const npf_netmask_t mask)
    319  1.13   rmind {
    320  1.13   rmind 
    321  1.13   rmind 	if (mask > NPF_MAX_NETMASK && mask != NPF_NO_NETMASK) {
    322  1.13   rmind 		return EINVAL;
    323  1.13   rmind 	}
    324  1.13   rmind 	if (aidx > 1) {
    325  1.13   rmind 		return EINVAL;
    326  1.13   rmind 	}
    327  1.13   rmind 
    328  1.13   rmind 	/*
    329  1.13   rmind 	 * For IPv4 (aidx = 0) - 32 and for IPv6 (aidx = 1) - 128.
    330  1.13   rmind 	 * If it is a host - shall use NPF_NO_NETMASK.
    331  1.13   rmind 	 */
    332  1.13   rmind 	if (mask >= (aidx ? 128 : 32) && mask != NPF_NO_NETMASK) {
    333  1.13   rmind 		return EINVAL;
    334  1.13   rmind 	}
    335  1.13   rmind 	return 0;
    336  1.13   rmind }
    337  1.13   rmind 
    338   1.1   rmind /*
    339  1.13   rmind  * npf_table_insert: add an IP CIDR entry into the table.
    340   1.1   rmind  */
    341   1.1   rmind int
    342  1.13   rmind 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.13   rmind 	const u_int aidx = NPF_ADDRLEN2TREE(alen);
    346  1.13   rmind 	npf_tblent_t *ent;
    347   1.1   rmind 	npf_table_t *t;
    348  1.13   rmind 	int error;
    349   1.1   rmind 
    350  1.15   rmind 	if ((u_int)tid >= NPF_TABLE_SLOTS || (t = tset[tid]) == NULL) {
    351  1.15   rmind 		return EINVAL;
    352  1.15   rmind 	}
    353  1.15   rmind 
    354  1.15   rmind 	error = table_cidr_check(aidx, addr, mask);
    355  1.13   rmind 	if (error) {
    356  1.13   rmind 		return error;
    357   1.8   rmind 	}
    358  1.12   rmind 	ent = pool_cache_get(tblent_cache, PR_WAITOK);
    359  1.13   rmind 	memcpy(&ent->te_addr, addr, alen);
    360  1.13   rmind 	ent->te_alen = alen;
    361   1.1   rmind 
    362  1.13   rmind 	/*
    363  1.13   rmind 	 * Insert the entry.  Return an error on duplicate.
    364  1.13   rmind 	 */
    365  1.15   rmind 	rw_enter(&t->t_lock, RW_WRITER);
    366   1.1   rmind 	switch (t->t_type) {
    367  1.13   rmind 	case NPF_TABLE_HASH: {
    368  1.13   rmind 		struct npf_hashl *htbl;
    369  1.13   rmind 
    370  1.13   rmind 		/*
    371  1.13   rmind 		 * Hash tables by the concept support only IPs.
    372  1.13   rmind 		 */
    373  1.13   rmind 		if (mask != NPF_NO_NETMASK) {
    374  1.13   rmind 			error = EINVAL;
    375  1.13   rmind 			break;
    376   1.1   rmind 		}
    377  1.13   rmind 		if (!table_hash_lookup(t, addr, alen, &htbl)) {
    378  1.12   rmind 			LIST_INSERT_HEAD(htbl, ent, te_entry.hashq);
    379  1.15   rmind 			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.13   rmind 	}
    385  1.13   rmind 	case NPF_TABLE_TREE: {
    386  1.13   rmind 		pt_tree_t *tree = &t->t_tree[aidx];
    387  1.13   rmind 		bool ok;
    388  1.13   rmind 
    389  1.13   rmind 		/*
    390  1.13   rmind 		 * If no mask specified, use maximum mask.
    391  1.13   rmind 		 */
    392  1.15   rmind 		ok = (mask != NPF_NO_NETMASK) ?
    393  1.15   rmind 		    ptree_insert_mask_node(tree, ent, mask) :
    394  1.15   rmind 		    ptree_insert_node(tree, ent);
    395  1.15   rmind 		if (ok) {
    396  1.15   rmind 			t->t_nitems++;
    397  1.15   rmind 			error = 0;
    398  1.13   rmind 		} else {
    399  1.15   rmind 			error = EEXIST;
    400   1.1   rmind 		}
    401   1.1   rmind 		break;
    402  1.13   rmind 	}
    403   1.1   rmind 	default:
    404   1.1   rmind 		KASSERT(false);
    405   1.1   rmind 	}
    406  1.15   rmind 	rw_exit(&t->t_lock);
    407   1.1   rmind 
    408   1.8   rmind 	if (error) {
    409  1.12   rmind 		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.13   rmind  * npf_table_remove: remove the IP CIDR entry from the table.
    416   1.1   rmind  */
    417   1.1   rmind int
    418  1.13   rmind 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.13   rmind 	const u_int aidx = NPF_ADDRLEN2TREE(alen);
    422  1.12   rmind 	npf_tblent_t *ent;
    423   1.1   rmind 	npf_table_t *t;
    424  1.13   rmind 	int error;
    425   1.1   rmind 
    426  1.15   rmind 	error = table_cidr_check(aidx, addr, mask);
    427  1.13   rmind 	if (error) {
    428  1.13   rmind 		return error;
    429   1.8   rmind 	}
    430  1.15   rmind 
    431  1.15   rmind 	if ((u_int)tid >= NPF_TABLE_SLOTS || (t = tset[tid]) == NULL) {
    432   1.1   rmind 		return EINVAL;
    433   1.1   rmind 	}
    434  1.12   rmind 
    435  1.15   rmind 	rw_enter(&t->t_lock, RW_WRITER);
    436  1.13   rmind 	switch (t->t_type) {
    437  1.13   rmind 	case NPF_TABLE_HASH: {
    438  1.13   rmind 		struct npf_hashl *htbl;
    439   1.8   rmind 
    440  1.13   rmind 		ent = table_hash_lookup(t, addr, alen, &htbl);
    441  1.12   rmind 		if (__predict_true(ent != NULL)) {
    442  1.12   rmind 			LIST_REMOVE(ent, te_entry.hashq);
    443  1.15   rmind 			t->t_nitems--;
    444   1.1   rmind 		}
    445   1.1   rmind 		break;
    446  1.13   rmind 	}
    447  1.13   rmind 	case NPF_TABLE_TREE: {
    448  1.13   rmind 		pt_tree_t *tree = &t->t_tree[aidx];
    449  1.13   rmind 
    450  1.13   rmind 		ent = ptree_find_node(tree, addr);
    451  1.12   rmind 		if (__predict_true(ent != NULL)) {
    452  1.13   rmind 			ptree_remove_node(tree, ent);
    453  1.15   rmind 			t->t_nitems--;
    454   1.1   rmind 		}
    455   1.1   rmind 		break;
    456  1.13   rmind 	}
    457   1.1   rmind 	default:
    458   1.1   rmind 		KASSERT(false);
    459  1.13   rmind 		ent = NULL;
    460   1.1   rmind 	}
    461  1.15   rmind 	rw_exit(&t->t_lock);
    462   1.1   rmind 
    463  1.12   rmind 	if (ent == NULL) {
    464   1.8   rmind 		return ENOENT;
    465   1.1   rmind 	}
    466  1.12   rmind 	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.13   rmind  * npf_table_lookup: find the table according to ID, lookup and match
    472  1.13   rmind  * the contents with the specified IP address.
    473   1.1   rmind  */
    474   1.1   rmind int
    475  1.13   rmind npf_table_lookup(npf_tableset_t *tset, u_int tid,
    476  1.13   rmind     const int alen, const npf_addr_t *addr)
    477   1.1   rmind {
    478  1.13   rmind 	const u_int aidx = NPF_ADDRLEN2TREE(alen);
    479  1.13   rmind 	npf_tblent_t *ent;
    480   1.1   rmind 	npf_table_t *t;
    481   1.1   rmind 
    482  1.13   rmind 	if (__predict_false(aidx > 1)) {
    483  1.13   rmind 		return EINVAL;
    484  1.13   rmind 	}
    485  1.13   rmind 
    486  1.15   rmind 	if ((u_int)tid >= NPF_TABLE_SLOTS || (t = tset[tid]) == NULL) {
    487   1.1   rmind 		return EINVAL;
    488   1.1   rmind 	}
    489  1.15   rmind 
    490  1.15   rmind 	rw_enter(&t->t_lock, RW_READER);
    491   1.1   rmind 	switch (t->t_type) {
    492  1.13   rmind 	case NPF_TABLE_HASH: {
    493  1.13   rmind 		struct npf_hashl *htbl;
    494  1.13   rmind 		ent = table_hash_lookup(t, addr, alen, &htbl);
    495   1.1   rmind 		break;
    496  1.13   rmind 	}
    497  1.13   rmind 	case NPF_TABLE_TREE: {
    498  1.13   rmind 		ent = ptree_find_node(&t->t_tree[aidx], addr);
    499   1.1   rmind 		break;
    500  1.13   rmind 	}
    501   1.1   rmind 	default:
    502   1.1   rmind 		KASSERT(false);
    503  1.13   rmind 		ent = NULL;
    504   1.1   rmind 	}
    505  1.15   rmind 	rw_exit(&t->t_lock);
    506   1.1   rmind 
    507  1.13   rmind 	return ent ? 0 : ENOENT;
    508   1.1   rmind }
    509  1.15   rmind 
    510  1.15   rmind static int
    511  1.15   rmind table_ent_copyout(npf_tblent_t *ent, npf_netmask_t mask,
    512  1.15   rmind     void *ubuf, size_t len, size_t *off)
    513  1.15   rmind {
    514  1.15   rmind 	void *ubufp = (uint8_t *)ubuf + *off;
    515  1.15   rmind 	npf_ioctl_ent_t uent;
    516  1.15   rmind 
    517  1.15   rmind 	if ((*off += sizeof(npf_ioctl_ent_t)) > len) {
    518  1.15   rmind 		return ENOMEM;
    519  1.15   rmind 	}
    520  1.15   rmind 	uent.alen = ent->te_alen;
    521  1.15   rmind 	memcpy(&uent.addr, &ent->te_addr, sizeof(npf_addr_t));
    522  1.15   rmind 	uent.mask = mask;
    523  1.15   rmind 
    524  1.15   rmind 	return copyout(&uent, ubufp, sizeof(npf_ioctl_ent_t));
    525  1.15   rmind }
    526  1.15   rmind 
    527  1.15   rmind static int
    528  1.15   rmind table_tree_list(pt_tree_t *tree, npf_netmask_t maxmask, void *ubuf,
    529  1.15   rmind     size_t len, size_t *off)
    530  1.15   rmind {
    531  1.15   rmind 	npf_tblent_t *ent = NULL;
    532  1.15   rmind 	int error = 0;
    533  1.15   rmind 
    534  1.15   rmind 	while ((ent = ptree_iterate(tree, ent, PT_ASCENDING)) != NULL) {
    535  1.15   rmind 		pt_bitlen_t blen;
    536  1.15   rmind 
    537  1.15   rmind 		if (!ptree_mask_node_p(tree, ent, &blen)) {
    538  1.15   rmind 			blen = maxmask;
    539  1.15   rmind 		}
    540  1.15   rmind 		error = table_ent_copyout(ent, blen, ubuf, len, off);
    541  1.15   rmind 		if (error)
    542  1.15   rmind 			break;
    543  1.15   rmind 	}
    544  1.15   rmind 	return error;
    545  1.15   rmind }
    546  1.15   rmind 
    547  1.15   rmind /*
    548  1.15   rmind  * npf_table_list: copy a list of all table entries into a userspace buffer.
    549  1.15   rmind  */
    550  1.15   rmind int
    551  1.15   rmind npf_table_list(npf_tableset_t *tset, u_int tid, void *ubuf, size_t len)
    552  1.15   rmind {
    553  1.15   rmind 	npf_table_t *t;
    554  1.15   rmind 	size_t off = 0;
    555  1.15   rmind 	int error = 0;
    556  1.15   rmind 
    557  1.15   rmind 	if ((u_int)tid >= NPF_TABLE_SLOTS || (t = tset[tid]) == NULL) {
    558  1.15   rmind 		return EINVAL;
    559  1.15   rmind 	}
    560  1.15   rmind 
    561  1.15   rmind 	rw_enter(&t->t_lock, RW_READER);
    562  1.15   rmind 	switch (t->t_type) {
    563  1.15   rmind 	case NPF_TABLE_HASH:
    564  1.15   rmind 		for (unsigned n = 0; n <= t->t_hashmask; n++) {
    565  1.15   rmind 			npf_tblent_t *ent;
    566  1.15   rmind 
    567  1.15   rmind 			LIST_FOREACH(ent, &t->t_hashl[n], te_entry.hashq)
    568  1.15   rmind 				if ((error = table_ent_copyout(ent, 0, ubuf,
    569  1.15   rmind 				    len, &off)) != 0)
    570  1.15   rmind 					break;
    571  1.15   rmind 		}
    572  1.15   rmind 		break;
    573  1.15   rmind 	case NPF_TABLE_TREE:
    574  1.15   rmind 		error = table_tree_list(&t->t_tree[0], 32, ubuf, len, &off);
    575  1.15   rmind 		if (error)
    576  1.15   rmind 			break;
    577  1.15   rmind 		error = table_tree_list(&t->t_tree[1], 128, ubuf, len, &off);
    578  1.16   rmind 		break;
    579  1.15   rmind 	default:
    580  1.15   rmind 		KASSERT(false);
    581  1.15   rmind 	}
    582  1.15   rmind 	rw_exit(&t->t_lock);
    583  1.15   rmind 
    584  1.15   rmind 	return error;
    585  1.15   rmind }
    586