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