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