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