Home | History | Annotate | Line # | Download | only in npf
npf_tableset.c revision 1.33
      1   1.1     rmind /*-
      2  1.33     rmind  * Copyright (c) 2009-2019 The NetBSD Foundation, Inc.
      3   1.1     rmind  * All rights reserved.
      4   1.1     rmind  *
      5   1.1     rmind  * This material is based upon work partially supported by The
      6   1.1     rmind  * NetBSD Foundation under a contract with Mindaugas Rasiukevicius.
      7   1.1     rmind  *
      8   1.1     rmind  * Redistribution and use in source and binary forms, with or without
      9   1.1     rmind  * modification, are permitted provided that the following conditions
     10   1.1     rmind  * are met:
     11   1.1     rmind  * 1. Redistributions of source code must retain the above copyright
     12   1.1     rmind  *    notice, this list of conditions and the following disclaimer.
     13   1.1     rmind  * 2. Redistributions in binary form must reproduce the above copyright
     14   1.1     rmind  *    notice, this list of conditions and the following disclaimer in the
     15   1.1     rmind  *    documentation and/or other materials provided with the distribution.
     16   1.1     rmind  *
     17   1.1     rmind  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     18   1.1     rmind  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     19   1.1     rmind  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     20   1.1     rmind  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     21   1.1     rmind  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     22   1.1     rmind  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     23   1.1     rmind  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     24   1.1     rmind  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     25   1.1     rmind  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     26   1.1     rmind  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     27   1.1     rmind  * POSSIBILITY OF SUCH DAMAGE.
     28   1.1     rmind  */
     29   1.1     rmind 
     30   1.1     rmind /*
     31   1.4     rmind  * NPF tableset module.
     32   1.1     rmind  *
     33  1.15     rmind  * Notes
     34  1.15     rmind  *
     35  1.15     rmind  *	The tableset is an array of tables.  After the creation, the array
     36  1.15     rmind  *	is immutable.  The caller is responsible to synchronise the access
     37  1.29     rmind  *	to the tableset.
     38  1.33     rmind  *
     39  1.33     rmind  * Warning (not applicable for the userspace npfkern):
     40  1.33     rmind  *
     41  1.33     rmind  *	The thmap_put()/thmap_del() are not called from the interrupt
     42  1.33     rmind  *	context and are protected by a mutex(9), therefore they do not
     43  1.33     rmind  *	SPL wrappers -- see the comment at the top of the npf_conndb.c
     44  1.33     rmind  *	source file.
     45   1.1     rmind  */
     46   1.1     rmind 
     47  1.25  christos #ifdef _KERNEL
     48   1.1     rmind #include <sys/cdefs.h>
     49  1.33     rmind __KERNEL_RCSID(0, "$NetBSD: npf_tableset.c,v 1.33 2019/07/23 00:52:01 rmind Exp $");
     50   1.1     rmind 
     51   1.1     rmind #include <sys/param.h>
     52  1.10     rmind #include <sys/types.h>
     53   1.1     rmind 
     54   1.1     rmind #include <sys/atomic.h>
     55  1.21     rmind #include <sys/cdbr.h>
     56   1.1     rmind #include <sys/kmem.h>
     57   1.1     rmind #include <sys/pool.h>
     58   1.1     rmind #include <sys/queue.h>
     59  1.28     rmind #include <sys/mutex.h>
     60  1.29     rmind #include <sys/thmap.h>
     61   1.1     rmind 
     62  1.25  christos #include "lpm.h"
     63  1.25  christos #endif
     64  1.25  christos 
     65   1.1     rmind #include "npf_impl.h"
     66   1.1     rmind 
     67  1.15     rmind typedef struct npf_tblent {
     68  1.24  christos 	LIST_ENTRY(npf_tblent)	te_listent;
     69  1.24  christos 	uint16_t		te_preflen;
     70  1.24  christos 	uint16_t		te_alen;
     71  1.13     rmind 	npf_addr_t		te_addr;
     72  1.15     rmind } npf_tblent_t;
     73   1.1     rmind 
     74  1.29     rmind #define	NPF_ADDRLEN2IDX(alen)	((alen) >> 4)
     75  1.29     rmind #define	NPF_ADDR_SLOTS		(2)
     76   1.1     rmind 
     77   1.1     rmind struct npf_table {
     78  1.19     rmind 	/*
     79  1.29     rmind 	 * The storage type can be: a) hashmap b) LPM c) cdb.
     80  1.19     rmind 	 * There are separate trees for IPv4 and IPv6.
     81  1.19     rmind 	 */
     82  1.21     rmind 	union {
     83  1.21     rmind 		struct {
     84  1.29     rmind 			thmap_t *	t_map;
     85  1.29     rmind 			LIST_HEAD(, npf_tblent) t_gc;
     86  1.21     rmind 		};
     87  1.29     rmind 		lpm_t *			t_lpm;
     88  1.21     rmind 		struct {
     89  1.21     rmind 			void *		t_blob;
     90  1.21     rmind 			size_t		t_bsize;
     91  1.21     rmind 			struct cdbr *	t_cdb;
     92  1.21     rmind 		};
     93  1.29     rmind 		struct {
     94  1.29     rmind 			npf_tblent_t **	t_elements[NPF_ADDR_SLOTS];
     95  1.29     rmind 			unsigned	t_allocated[NPF_ADDR_SLOTS];
     96  1.29     rmind 			unsigned	t_used[NPF_ADDR_SLOTS];
     97  1.29     rmind 		};
     98  1.21     rmind 	} /* C11 */;
     99  1.29     rmind 	LIST_HEAD(, npf_tblent)		t_list;
    100  1.29     rmind 	unsigned			t_nitems;
    101  1.19     rmind 
    102  1.19     rmind 	/*
    103  1.19     rmind 	 * Table ID, type and lock.  The ID may change during the
    104  1.19     rmind 	 * config reload, it is protected by the npf_config_lock.
    105  1.19     rmind 	 */
    106  1.19     rmind 	int			t_type;
    107  1.29     rmind 	unsigned		t_id;
    108  1.28     rmind 	kmutex_t		t_lock;
    109  1.19     rmind 
    110  1.29     rmind 	/* Reference count and table name. */
    111  1.29     rmind 	unsigned		t_refcnt;
    112  1.19     rmind 	char			t_name[NPF_TABLE_MAXNAMELEN];
    113  1.19     rmind };
    114  1.19     rmind 
    115  1.19     rmind struct npf_tableset {
    116  1.29     rmind 	unsigned		ts_nitems;
    117  1.19     rmind 	npf_table_t *		ts_map[];
    118   1.1     rmind };
    119   1.1     rmind 
    120  1.19     rmind #define	NPF_TABLESET_SIZE(n)	\
    121  1.19     rmind     (offsetof(npf_tableset_t, ts_map[n]) * sizeof(npf_table_t *))
    122  1.19     rmind 
    123  1.29     rmind #define	NPF_IFADDR_STEP		4
    124  1.13     rmind 
    125  1.13     rmind static pool_cache_t		tblent_cache	__read_mostly;
    126   1.1     rmind 
    127   1.1     rmind /*
    128   1.1     rmind  * npf_table_sysinit: initialise tableset structures.
    129   1.1     rmind  */
    130   1.4     rmind void
    131   1.1     rmind npf_tableset_sysinit(void)
    132   1.1     rmind {
    133  1.29     rmind 	tblent_cache = pool_cache_init(sizeof(npf_tblent_t), 0,
    134  1.14     rmind 	    0, 0, "npftblpl", NULL, IPL_NONE, NULL, NULL, NULL);
    135   1.1     rmind }
    136   1.1     rmind 
    137   1.1     rmind void
    138   1.1     rmind npf_tableset_sysfini(void)
    139   1.1     rmind {
    140   1.1     rmind 	pool_cache_destroy(tblent_cache);
    141   1.1     rmind }
    142   1.1     rmind 
    143   1.1     rmind npf_tableset_t *
    144  1.19     rmind npf_tableset_create(u_int nitems)
    145   1.1     rmind {
    146  1.19     rmind 	npf_tableset_t *ts = kmem_zalloc(NPF_TABLESET_SIZE(nitems), KM_SLEEP);
    147  1.19     rmind 	ts->ts_nitems = nitems;
    148  1.19     rmind 	return ts;
    149   1.1     rmind }
    150   1.1     rmind 
    151   1.1     rmind void
    152  1.19     rmind npf_tableset_destroy(npf_tableset_t *ts)
    153   1.1     rmind {
    154   1.1     rmind 	/*
    155  1.19     rmind 	 * Destroy all tables (no references should be held, since the
    156  1.19     rmind 	 * ruleset should be destroyed before).
    157   1.1     rmind 	 */
    158  1.19     rmind 	for (u_int tid = 0; tid < ts->ts_nitems; tid++) {
    159  1.19     rmind 		npf_table_t *t = ts->ts_map[tid];
    160  1.19     rmind 
    161  1.17     rmind 		if (t && atomic_dec_uint_nv(&t->t_refcnt) == 0) {
    162   1.1     rmind 			npf_table_destroy(t);
    163   1.1     rmind 		}
    164   1.1     rmind 	}
    165  1.19     rmind 	kmem_free(ts, NPF_TABLESET_SIZE(ts->ts_nitems));
    166   1.1     rmind }
    167   1.1     rmind 
    168   1.1     rmind /*
    169   1.1     rmind  * npf_tableset_insert: insert the table into the specified tableset.
    170   1.1     rmind  *
    171  1.13     rmind  * => Returns 0 on success.  Fails and returns error if ID is already used.
    172   1.1     rmind  */
    173   1.1     rmind int
    174  1.19     rmind npf_tableset_insert(npf_tableset_t *ts, npf_table_t *t)
    175   1.1     rmind {
    176   1.1     rmind 	const u_int tid = t->t_id;
    177   1.1     rmind 	int error;
    178   1.1     rmind 
    179  1.19     rmind 	KASSERT((u_int)tid < ts->ts_nitems);
    180   1.1     rmind 
    181  1.19     rmind 	if (ts->ts_map[tid] == NULL) {
    182  1.17     rmind 		atomic_inc_uint(&t->t_refcnt);
    183  1.19     rmind 		ts->ts_map[tid] = t;
    184   1.1     rmind 		error = 0;
    185   1.1     rmind 	} else {
    186   1.1     rmind 		error = EEXIST;
    187   1.1     rmind 	}
    188   1.1     rmind 	return error;
    189   1.1     rmind }
    190   1.1     rmind 
    191  1.26     rmind npf_table_t *
    192  1.26     rmind npf_tableset_swap(npf_tableset_t *ts, npf_table_t *newt)
    193  1.26     rmind {
    194  1.26     rmind 	const u_int tid = newt->t_id;
    195  1.26     rmind 	npf_table_t *oldt = ts->ts_map[tid];
    196  1.26     rmind 
    197  1.26     rmind 	KASSERT(tid < ts->ts_nitems);
    198  1.26     rmind 	KASSERT(oldt->t_id == newt->t_id);
    199  1.26     rmind 
    200  1.26     rmind 	newt->t_refcnt = oldt->t_refcnt;
    201  1.26     rmind 	oldt->t_refcnt = 0;
    202  1.26     rmind 
    203  1.26     rmind 	return atomic_swap_ptr(&ts->ts_map[tid], newt);
    204  1.26     rmind }
    205  1.26     rmind 
    206   1.1     rmind /*
    207  1.19     rmind  * npf_tableset_getbyname: look for a table in the set given the name.
    208  1.19     rmind  */
    209  1.19     rmind npf_table_t *
    210  1.19     rmind npf_tableset_getbyname(npf_tableset_t *ts, const char *name)
    211  1.19     rmind {
    212  1.19     rmind 	npf_table_t *t;
    213  1.19     rmind 
    214  1.19     rmind 	for (u_int tid = 0; tid < ts->ts_nitems; tid++) {
    215  1.19     rmind 		if ((t = ts->ts_map[tid]) == NULL)
    216  1.19     rmind 			continue;
    217  1.19     rmind 		if (strcmp(name, t->t_name) == 0)
    218  1.19     rmind 			return t;
    219  1.19     rmind 	}
    220  1.19     rmind 	return NULL;
    221  1.19     rmind }
    222  1.19     rmind 
    223  1.19     rmind npf_table_t *
    224  1.19     rmind npf_tableset_getbyid(npf_tableset_t *ts, u_int tid)
    225  1.19     rmind {
    226  1.19     rmind 	if (__predict_true(tid < ts->ts_nitems)) {
    227  1.19     rmind 		return ts->ts_map[tid];
    228  1.19     rmind 	}
    229  1.19     rmind 	return NULL;
    230  1.19     rmind }
    231  1.19     rmind 
    232  1.19     rmind /*
    233  1.15     rmind  * npf_tableset_reload: iterate all tables and if the new table is of the
    234  1.15     rmind  * same type and has no items, then we preserve the old one and its entries.
    235  1.15     rmind  *
    236  1.15     rmind  * => The caller is responsible for providing synchronisation.
    237  1.15     rmind  */
    238  1.15     rmind void
    239  1.25  christos npf_tableset_reload(npf_t *npf, npf_tableset_t *nts, npf_tableset_t *ots)
    240  1.15     rmind {
    241  1.19     rmind 	for (u_int tid = 0; tid < nts->ts_nitems; tid++) {
    242  1.19     rmind 		npf_table_t *t, *ot;
    243  1.19     rmind 
    244  1.19     rmind 		if ((t = nts->ts_map[tid]) == NULL) {
    245  1.19     rmind 			continue;
    246  1.19     rmind 		}
    247  1.15     rmind 
    248  1.19     rmind 		/* If our table has entries, just load it. */
    249  1.19     rmind 		if (t->t_nitems) {
    250  1.15     rmind 			continue;
    251  1.15     rmind 		}
    252  1.19     rmind 
    253  1.19     rmind 		/* Look for a currently existing table with such name. */
    254  1.19     rmind 		ot = npf_tableset_getbyname(ots, t->t_name);
    255  1.19     rmind 		if (ot == NULL) {
    256  1.19     rmind 			/* Not found: we have a new table. */
    257  1.19     rmind 			continue;
    258  1.19     rmind 		}
    259  1.19     rmind 
    260  1.19     rmind 		/* Found.  Did the type change? */
    261  1.19     rmind 		if (t->t_type != ot->t_type) {
    262  1.19     rmind 			/* Yes, load the new. */
    263  1.15     rmind 			continue;
    264  1.15     rmind 		}
    265  1.17     rmind 
    266  1.17     rmind 		/*
    267  1.19     rmind 		 * Preserve the current table.  Acquire a reference since
    268  1.19     rmind 		 * we are keeping it in the old table set.  Update its ID.
    269  1.17     rmind 		 */
    270  1.17     rmind 		atomic_inc_uint(&ot->t_refcnt);
    271  1.19     rmind 		nts->ts_map[tid] = ot;
    272  1.19     rmind 
    273  1.25  christos 		KASSERT(npf_config_locked_p(npf));
    274  1.19     rmind 		ot->t_id = tid;
    275  1.17     rmind 
    276  1.21     rmind 		/* Destroy the new table (we hold the only reference). */
    277  1.17     rmind 		t->t_refcnt--;
    278  1.15     rmind 		npf_table_destroy(t);
    279  1.15     rmind 	}
    280  1.15     rmind }
    281  1.15     rmind 
    282  1.22     rmind int
    283  1.28     rmind npf_tableset_export(npf_t *npf, const npf_tableset_t *ts, nvlist_t *npf_dict)
    284  1.20     rmind {
    285  1.20     rmind 	const npf_table_t *t;
    286  1.20     rmind 
    287  1.25  christos 	KASSERT(npf_config_locked_p(npf));
    288  1.20     rmind 
    289  1.20     rmind 	for (u_int tid = 0; tid < ts->ts_nitems; tid++) {
    290  1.28     rmind 		nvlist_t *table;
    291  1.28     rmind 
    292  1.20     rmind 		if ((t = ts->ts_map[tid]) == NULL) {
    293  1.20     rmind 			continue;
    294  1.20     rmind 		}
    295  1.28     rmind 		table = nvlist_create(0);
    296  1.28     rmind 		nvlist_add_string(table, "name", t->t_name);
    297  1.28     rmind 		nvlist_add_number(table, "type", t->t_type);
    298  1.28     rmind 		nvlist_add_number(table, "id", tid);
    299  1.20     rmind 
    300  1.28     rmind 		nvlist_append_nvlist_array(npf_dict, "tables", table);
    301  1.28     rmind 		nvlist_destroy(table);
    302  1.20     rmind 	}
    303  1.22     rmind 	return 0;
    304  1.20     rmind }
    305  1.20     rmind 
    306  1.15     rmind /*
    307  1.13     rmind  * Few helper routines.
    308   1.1     rmind  */
    309   1.1     rmind 
    310  1.29     rmind static void
    311  1.29     rmind table_ipset_flush(npf_table_t *t)
    312   1.1     rmind {
    313  1.13     rmind 	npf_tblent_t *ent;
    314   1.1     rmind 
    315  1.29     rmind 	while ((ent = LIST_FIRST(&t->t_list)) != NULL) {
    316  1.29     rmind 		thmap_del(t->t_map, &ent->te_addr, ent->te_alen);
    317  1.29     rmind 		LIST_REMOVE(ent, te_listent);
    318  1.29     rmind 		pool_cache_put(tblent_cache, ent);
    319  1.13     rmind 	}
    320  1.29     rmind 	t->t_nitems = 0;
    321   1.1     rmind }
    322   1.1     rmind 
    323  1.13     rmind static void
    324  1.29     rmind table_tree_flush(npf_table_t *t)
    325  1.18     rmind {
    326  1.29     rmind 	npf_tblent_t *ent;
    327  1.18     rmind 
    328  1.29     rmind 	while ((ent = LIST_FIRST(&t->t_list)) != NULL) {
    329  1.29     rmind 		LIST_REMOVE(ent, te_listent);
    330  1.29     rmind 		pool_cache_put(tblent_cache, ent);
    331  1.18     rmind 	}
    332  1.29     rmind 	lpm_clear(t->t_lpm, NULL, NULL);
    333  1.29     rmind 	t->t_nitems = 0;
    334  1.18     rmind }
    335  1.18     rmind 
    336  1.18     rmind static void
    337  1.29     rmind table_ifaddr_flush(npf_table_t *t)
    338   1.1     rmind {
    339  1.13     rmind 	npf_tblent_t *ent;
    340   1.1     rmind 
    341  1.29     rmind 	for (unsigned i = 0; i < NPF_ADDR_SLOTS; i++) {
    342  1.29     rmind 		size_t len;
    343  1.29     rmind 
    344  1.29     rmind 		if (!t->t_allocated[i]) {
    345  1.29     rmind 			KASSERT(t->t_elements[i] == NULL);
    346  1.29     rmind 			continue;
    347  1.29     rmind 		}
    348  1.29     rmind 		len = t->t_allocated[i] * sizeof(npf_tblent_t *);
    349  1.29     rmind 		kmem_free(t->t_elements[i], len);
    350  1.29     rmind 		t->t_elements[i] = NULL;
    351  1.29     rmind 		t->t_allocated[i] = 0;
    352  1.29     rmind 		t->t_used[i] = 0;
    353  1.29     rmind 	}
    354  1.24  christos 	while ((ent = LIST_FIRST(&t->t_list)) != NULL) {
    355  1.24  christos 		LIST_REMOVE(ent, te_listent);
    356  1.13     rmind 		pool_cache_put(tblent_cache, ent);
    357  1.13     rmind 	}
    358  1.29     rmind 	t->t_nitems = 0;
    359   1.1     rmind }
    360   1.1     rmind 
    361   1.1     rmind /*
    362   1.1     rmind  * npf_table_create: create table with a specified ID.
    363   1.1     rmind  */
    364   1.1     rmind npf_table_t *
    365  1.21     rmind npf_table_create(const char *name, u_int tid, int type,
    366  1.28     rmind     const void *blob, size_t size)
    367   1.1     rmind {
    368   1.1     rmind 	npf_table_t *t;
    369   1.1     rmind 
    370  1.25  christos 	t = kmem_zalloc(sizeof(npf_table_t), KM_SLEEP);
    371  1.19     rmind 	strlcpy(t->t_name, name, NPF_TABLE_MAXNAMELEN);
    372   1.1     rmind 
    373   1.1     rmind 	switch (type) {
    374  1.29     rmind 	case NPF_TABLE_LPM:
    375  1.30  christos 		t->t_lpm = lpm_create(KM_NOSLEEP);
    376  1.29     rmind 		if (t->t_lpm == NULL) {
    377  1.24  christos 			goto out;
    378  1.25  christos 		}
    379  1.24  christos 		LIST_INIT(&t->t_list);
    380   1.1     rmind 		break;
    381  1.29     rmind 	case NPF_TABLE_IPSET:
    382  1.29     rmind 		t->t_map = thmap_create(0, NULL, THMAP_NOCOPY);
    383  1.29     rmind 		if (t->t_map == NULL) {
    384  1.24  christos 			goto out;
    385  1.25  christos 		}
    386   1.1     rmind 		break;
    387  1.29     rmind 	case NPF_TABLE_CONST:
    388  1.28     rmind 		t->t_blob = kmem_alloc(size, KM_SLEEP);
    389  1.28     rmind 		if (t->t_blob == NULL) {
    390  1.28     rmind 			goto out;
    391  1.28     rmind 		}
    392  1.28     rmind 		memcpy(t->t_blob, blob, size);
    393  1.21     rmind 		t->t_bsize = size;
    394  1.28     rmind 
    395  1.28     rmind 		t->t_cdb = cdbr_open_mem(t->t_blob, size,
    396  1.28     rmind 		    CDBR_DEFAULT, NULL, NULL);
    397  1.21     rmind 		if (t->t_cdb == NULL) {
    398  1.28     rmind 			kmem_free(t->t_blob, t->t_bsize);
    399  1.24  christos 			goto out;
    400  1.21     rmind 		}
    401  1.21     rmind 		t->t_nitems = cdbr_entries(t->t_cdb);
    402  1.21     rmind 		break;
    403  1.29     rmind 	case NPF_TABLE_IFADDR:
    404  1.29     rmind 		break;
    405   1.1     rmind 	default:
    406   1.1     rmind 		KASSERT(false);
    407   1.1     rmind 	}
    408  1.30  christos 	mutex_init(&t->t_lock, MUTEX_DEFAULT, IPL_NET);
    409   1.1     rmind 	t->t_type = type;
    410   1.1     rmind 	t->t_id = tid;
    411   1.1     rmind 	return t;
    412  1.24  christos out:
    413  1.25  christos 	kmem_free(t, sizeof(npf_table_t));
    414  1.24  christos 	return NULL;
    415   1.1     rmind }
    416   1.1     rmind 
    417   1.1     rmind /*
    418   1.1     rmind  * npf_table_destroy: free all table entries and table itself.
    419   1.1     rmind  */
    420   1.1     rmind void
    421   1.1     rmind npf_table_destroy(npf_table_t *t)
    422   1.1     rmind {
    423  1.17     rmind 	KASSERT(t->t_refcnt == 0);
    424   1.1     rmind 
    425   1.1     rmind 	switch (t->t_type) {
    426  1.29     rmind 	case NPF_TABLE_IPSET:
    427  1.29     rmind 		table_ipset_flush(t);
    428  1.29     rmind 		npf_table_gc(NULL, t);
    429  1.29     rmind 		thmap_destroy(t->t_map);
    430   1.1     rmind 		break;
    431  1.29     rmind 	case NPF_TABLE_LPM:
    432  1.24  christos 		table_tree_flush(t);
    433  1.24  christos 		lpm_destroy(t->t_lpm);
    434   1.1     rmind 		break;
    435  1.29     rmind 	case NPF_TABLE_CONST:
    436  1.21     rmind 		cdbr_close(t->t_cdb);
    437  1.28     rmind 		kmem_free(t->t_blob, t->t_bsize);
    438  1.21     rmind 		break;
    439  1.29     rmind 	case NPF_TABLE_IFADDR:
    440  1.29     rmind 		table_ifaddr_flush(t);
    441  1.29     rmind 		break;
    442   1.1     rmind 	default:
    443   1.1     rmind 		KASSERT(false);
    444   1.1     rmind 	}
    445  1.28     rmind 	mutex_destroy(&t->t_lock);
    446  1.25  christos 	kmem_free(t, sizeof(npf_table_t));
    447   1.1     rmind }
    448   1.1     rmind 
    449  1.26     rmind u_int
    450  1.26     rmind npf_table_getid(npf_table_t *t)
    451  1.26     rmind {
    452  1.26     rmind 	return t->t_id;
    453  1.26     rmind }
    454  1.26     rmind 
    455   1.1     rmind /*
    456  1.19     rmind  * npf_table_check: validate the name, ID and type.
    457  1.13     rmind  */
    458   1.1     rmind int
    459  1.28     rmind npf_table_check(npf_tableset_t *ts, const char *name, uint64_t tid, uint64_t type)
    460   1.1     rmind {
    461  1.28     rmind 	if (tid >= ts->ts_nitems) {
    462   1.1     rmind 		return EINVAL;
    463   1.1     rmind 	}
    464  1.19     rmind 	if (ts->ts_map[tid] != NULL) {
    465   1.1     rmind 		return EEXIST;
    466   1.1     rmind 	}
    467  1.21     rmind 	switch (type) {
    468  1.29     rmind 	case NPF_TABLE_LPM:
    469  1.29     rmind 	case NPF_TABLE_IPSET:
    470  1.29     rmind 	case NPF_TABLE_CONST:
    471  1.29     rmind 	case NPF_TABLE_IFADDR:
    472  1.21     rmind 		break;
    473  1.21     rmind 	default:
    474   1.1     rmind 		return EINVAL;
    475   1.1     rmind 	}
    476  1.19     rmind 	if (strlen(name) >= NPF_TABLE_MAXNAMELEN) {
    477  1.19     rmind 		return ENAMETOOLONG;
    478  1.19     rmind 	}
    479  1.19     rmind 	if (npf_tableset_getbyname(ts, name)) {
    480  1.20     rmind 		return EEXIST;
    481  1.19     rmind 	}
    482   1.1     rmind 	return 0;
    483   1.1     rmind }
    484   1.1     rmind 
    485  1.13     rmind static int
    486  1.29     rmind table_ifaddr_insert(npf_table_t *t, const int alen, npf_tblent_t *ent)
    487  1.29     rmind {
    488  1.29     rmind 	const unsigned aidx = NPF_ADDRLEN2IDX(alen);
    489  1.29     rmind 	const unsigned allocated = t->t_allocated[aidx];
    490  1.29     rmind 	const unsigned used = t->t_used[aidx];
    491  1.13     rmind 
    492  1.13     rmind 	/*
    493  1.29     rmind 	 * No need to check for duplicates.
    494  1.13     rmind 	 */
    495  1.29     rmind 	if (allocated <= used) {
    496  1.29     rmind 		npf_tblent_t **old_elements = t->t_elements[aidx];
    497  1.29     rmind 		npf_tblent_t **elements;
    498  1.29     rmind 		size_t toalloc, newsize;
    499  1.29     rmind 
    500  1.29     rmind 		toalloc = roundup2(allocated + 1, NPF_IFADDR_STEP);
    501  1.29     rmind 		newsize = toalloc * sizeof(npf_tblent_t *);
    502  1.29     rmind 
    503  1.31  christos 		elements = kmem_zalloc(newsize, KM_NOSLEEP);
    504  1.32  christos 		if (elements == NULL) {
    505  1.32  christos 			return ENOMEM;
    506  1.32  christos 		}
    507  1.29     rmind 		for (unsigned i = 0; i < used; i++) {
    508  1.29     rmind 			elements[i] = old_elements[i];
    509  1.29     rmind 		}
    510  1.29     rmind 		if (allocated) {
    511  1.29     rmind 			const size_t len = allocated * sizeof(npf_tblent_t *);
    512  1.29     rmind 			KASSERT(old_elements != NULL);
    513  1.29     rmind 			kmem_free(old_elements, len);
    514  1.29     rmind 		}
    515  1.29     rmind 		t->t_elements[aidx] = elements;
    516  1.29     rmind 		t->t_allocated[aidx] = toalloc;
    517  1.13     rmind 	}
    518  1.29     rmind 	t->t_elements[aidx][used] = ent;
    519  1.29     rmind 	t->t_used[aidx]++;
    520  1.32  christos 	return 0;
    521  1.13     rmind }
    522  1.13     rmind 
    523   1.1     rmind /*
    524  1.13     rmind  * npf_table_insert: add an IP CIDR entry into the table.
    525   1.1     rmind  */
    526   1.1     rmind int
    527  1.19     rmind npf_table_insert(npf_table_t *t, const int alen,
    528   1.6    zoltan     const npf_addr_t *addr, const npf_netmask_t mask)
    529   1.1     rmind {
    530  1.13     rmind 	npf_tblent_t *ent;
    531  1.13     rmind 	int error;
    532   1.1     rmind 
    533  1.33     rmind 	error = npf_netmask_check(alen, mask);
    534  1.13     rmind 	if (error) {
    535  1.13     rmind 		return error;
    536   1.8     rmind 	}
    537  1.12     rmind 	ent = pool_cache_get(tblent_cache, PR_WAITOK);
    538  1.13     rmind 	memcpy(&ent->te_addr, addr, alen);
    539  1.13     rmind 	ent->te_alen = alen;
    540  1.29     rmind 	ent->te_preflen = 0;
    541   1.1     rmind 
    542  1.13     rmind 	/*
    543  1.13     rmind 	 * Insert the entry.  Return an error on duplicate.
    544  1.13     rmind 	 */
    545  1.28     rmind 	mutex_enter(&t->t_lock);
    546   1.1     rmind 	switch (t->t_type) {
    547  1.29     rmind 	case NPF_TABLE_IPSET:
    548  1.13     rmind 		/*
    549  1.29     rmind 		 * Hashmap supports only IPs.
    550  1.29     rmind 		 *
    551  1.29     rmind 		 * Note: the key must be already persistent, since we
    552  1.29     rmind 		 * use THMAP_NOCOPY.
    553  1.13     rmind 		 */
    554  1.13     rmind 		if (mask != NPF_NO_NETMASK) {
    555  1.13     rmind 			error = EINVAL;
    556  1.13     rmind 			break;
    557   1.1     rmind 		}
    558  1.29     rmind 		if (thmap_put(t->t_map, &ent->te_addr, alen, ent) == ent) {
    559  1.29     rmind 			LIST_INSERT_HEAD(&t->t_list, ent, te_listent);
    560  1.15     rmind 			t->t_nitems++;
    561   1.1     rmind 		} else {
    562   1.1     rmind 			error = EEXIST;
    563   1.1     rmind 		}
    564   1.1     rmind 		break;
    565  1.29     rmind 	case NPF_TABLE_LPM: {
    566  1.24  christos 		const unsigned preflen =
    567  1.24  christos 		    (mask == NPF_NO_NETMASK) ? (alen * 8) : mask;
    568  1.29     rmind 		ent->te_preflen = preflen;
    569  1.29     rmind 
    570  1.24  christos 		if (lpm_lookup(t->t_lpm, addr, alen) == NULL &&
    571  1.24  christos 		    lpm_insert(t->t_lpm, addr, alen, preflen, ent) == 0) {
    572  1.24  christos 			LIST_INSERT_HEAD(&t->t_list, ent, te_listent);
    573  1.15     rmind 			t->t_nitems++;
    574  1.15     rmind 			error = 0;
    575  1.13     rmind 		} else {
    576  1.15     rmind 			error = EEXIST;
    577   1.1     rmind 		}
    578   1.1     rmind 		break;
    579  1.13     rmind 	}
    580  1.29     rmind 	case NPF_TABLE_CONST:
    581  1.21     rmind 		error = EINVAL;
    582  1.21     rmind 		break;
    583  1.29     rmind 	case NPF_TABLE_IFADDR:
    584  1.32  christos 		if ((error = table_ifaddr_insert(t, alen, ent)) != 0) {
    585  1.32  christos 			break;
    586  1.32  christos 		}
    587  1.29     rmind 		LIST_INSERT_HEAD(&t->t_list, ent, te_listent);
    588  1.29     rmind 		t->t_nitems++;
    589  1.29     rmind 		break;
    590   1.1     rmind 	default:
    591   1.1     rmind 		KASSERT(false);
    592   1.1     rmind 	}
    593  1.28     rmind 	mutex_exit(&t->t_lock);
    594   1.1     rmind 
    595   1.8     rmind 	if (error) {
    596  1.12     rmind 		pool_cache_put(tblent_cache, ent);
    597   1.1     rmind 	}
    598   1.1     rmind 	return error;
    599   1.1     rmind }
    600   1.1     rmind 
    601   1.1     rmind /*
    602  1.13     rmind  * npf_table_remove: remove the IP CIDR entry from the table.
    603   1.1     rmind  */
    604   1.1     rmind int
    605  1.19     rmind npf_table_remove(npf_table_t *t, const int alen,
    606   1.6    zoltan     const npf_addr_t *addr, const npf_netmask_t mask)
    607   1.1     rmind {
    608  1.21     rmind 	npf_tblent_t *ent = NULL;
    609  1.29     rmind 	int error;
    610   1.1     rmind 
    611  1.33     rmind 	error = npf_netmask_check(alen, mask);
    612  1.13     rmind 	if (error) {
    613  1.13     rmind 		return error;
    614   1.8     rmind 	}
    615  1.15     rmind 
    616  1.28     rmind 	mutex_enter(&t->t_lock);
    617  1.13     rmind 	switch (t->t_type) {
    618  1.29     rmind 	case NPF_TABLE_IPSET:
    619  1.29     rmind 		ent = thmap_del(t->t_map, addr, alen);
    620  1.12     rmind 		if (__predict_true(ent != NULL)) {
    621  1.24  christos 			LIST_REMOVE(ent, te_listent);
    622  1.29     rmind 			LIST_INSERT_HEAD(&t->t_gc, ent, te_listent);
    623  1.29     rmind 			ent = NULL; // to be G/C'ed
    624  1.15     rmind 			t->t_nitems--;
    625  1.29     rmind 		} else {
    626  1.29     rmind 			error = ENOENT;
    627   1.1     rmind 		}
    628   1.1     rmind 		break;
    629  1.29     rmind 	case NPF_TABLE_LPM:
    630  1.24  christos 		ent = lpm_lookup(t->t_lpm, addr, alen);
    631  1.12     rmind 		if (__predict_true(ent != NULL)) {
    632  1.24  christos 			LIST_REMOVE(ent, te_listent);
    633  1.24  christos 			lpm_remove(t->t_lpm, &ent->te_addr,
    634  1.24  christos 			    ent->te_alen, ent->te_preflen);
    635  1.15     rmind 			t->t_nitems--;
    636  1.29     rmind 		} else {
    637  1.29     rmind 			error = ENOENT;
    638   1.1     rmind 		}
    639   1.1     rmind 		break;
    640  1.29     rmind 	case NPF_TABLE_CONST:
    641  1.29     rmind 	case NPF_TABLE_IFADDR:
    642  1.21     rmind 		error = EINVAL;
    643  1.21     rmind 		break;
    644   1.1     rmind 	default:
    645   1.1     rmind 		KASSERT(false);
    646  1.13     rmind 		ent = NULL;
    647   1.1     rmind 	}
    648  1.28     rmind 	mutex_exit(&t->t_lock);
    649   1.1     rmind 
    650  1.21     rmind 	if (ent) {
    651  1.21     rmind 		pool_cache_put(tblent_cache, ent);
    652   1.1     rmind 	}
    653  1.21     rmind 	return error;
    654   1.1     rmind }
    655   1.1     rmind 
    656   1.1     rmind /*
    657  1.13     rmind  * npf_table_lookup: find the table according to ID, lookup and match
    658  1.13     rmind  * the contents with the specified IP address.
    659   1.1     rmind  */
    660   1.1     rmind int
    661  1.19     rmind npf_table_lookup(npf_table_t *t, const int alen, const npf_addr_t *addr)
    662   1.1     rmind {
    663  1.21     rmind 	const void *data;
    664  1.21     rmind 	size_t dlen;
    665  1.21     rmind 	bool found;
    666  1.29     rmind 	int error;
    667   1.1     rmind 
    668  1.33     rmind 	error = npf_netmask_check(alen, NPF_NO_NETMASK);
    669  1.29     rmind 	if (error) {
    670  1.29     rmind 		return error;
    671  1.13     rmind 	}
    672  1.13     rmind 
    673   1.1     rmind 	switch (t->t_type) {
    674  1.29     rmind 	case NPF_TABLE_IPSET:
    675  1.29     rmind 		found = thmap_get(t->t_map, addr, alen) != NULL;
    676   1.1     rmind 		break;
    677  1.29     rmind 	case NPF_TABLE_LPM:
    678  1.28     rmind 		mutex_enter(&t->t_lock);
    679  1.24  christos 		found = lpm_lookup(t->t_lpm, addr, alen) != NULL;
    680  1.28     rmind 		mutex_exit(&t->t_lock);
    681  1.21     rmind 		break;
    682  1.29     rmind 	case NPF_TABLE_CONST:
    683  1.21     rmind 		if (cdbr_find(t->t_cdb, addr, alen, &data, &dlen) == 0) {
    684  1.29     rmind 			found = dlen == (unsigned)alen &&
    685  1.25  christos 			    memcmp(addr, data, dlen) == 0;
    686  1.21     rmind 		} else {
    687  1.21     rmind 			found = false;
    688  1.21     rmind 		}
    689   1.1     rmind 		break;
    690  1.29     rmind 	case NPF_TABLE_IFADDR: {
    691  1.29     rmind 		const unsigned aidx = NPF_ADDRLEN2IDX(alen);
    692  1.29     rmind 
    693  1.29     rmind 		found = false;
    694  1.29     rmind 		for (unsigned i = 0; i < t->t_used[aidx]; i++) {
    695  1.29     rmind 			const npf_tblent_t *elm = t->t_elements[aidx][i];
    696  1.29     rmind 
    697  1.29     rmind 			KASSERT(elm->te_alen == alen);
    698  1.29     rmind 
    699  1.29     rmind 			if (memcmp(&elm->te_addr, addr, alen) == 0) {
    700  1.29     rmind 				found = true;
    701  1.29     rmind 				break;
    702  1.29     rmind 			}
    703  1.29     rmind 		}
    704  1.29     rmind 		break;
    705  1.29     rmind 	}
    706   1.1     rmind 	default:
    707   1.1     rmind 		KASSERT(false);
    708  1.21     rmind 		found = false;
    709   1.1     rmind 	}
    710   1.1     rmind 
    711  1.21     rmind 	return found ? 0 : ENOENT;
    712   1.1     rmind }
    713  1.15     rmind 
    714  1.29     rmind npf_addr_t *
    715  1.29     rmind npf_table_getsome(npf_table_t *t, const int alen, unsigned idx)
    716  1.29     rmind {
    717  1.29     rmind 	const unsigned aidx = NPF_ADDRLEN2IDX(alen);
    718  1.29     rmind 	npf_tblent_t *elm;
    719  1.29     rmind 	unsigned nitems;
    720  1.29     rmind 
    721  1.29     rmind 	KASSERT(t->t_type == NPF_TABLE_IFADDR);
    722  1.29     rmind 	KASSERT(aidx < NPF_ADDR_SLOTS);
    723  1.29     rmind 
    724  1.29     rmind 	nitems = t->t_used[aidx];
    725  1.29     rmind 	if (nitems == 0) {
    726  1.29     rmind 		return NULL;
    727  1.29     rmind 	}
    728  1.29     rmind 
    729  1.29     rmind 	/*
    730  1.29     rmind 	 * No need to acquire the lock, since the table is immutable.
    731  1.29     rmind 	 */
    732  1.29     rmind 	elm = t->t_elements[aidx][idx % nitems];
    733  1.29     rmind 	return &elm->te_addr;
    734  1.29     rmind }
    735  1.29     rmind 
    736  1.15     rmind static int
    737  1.21     rmind table_ent_copyout(const npf_addr_t *addr, const int alen, npf_netmask_t mask,
    738  1.15     rmind     void *ubuf, size_t len, size_t *off)
    739  1.15     rmind {
    740  1.15     rmind 	void *ubufp = (uint8_t *)ubuf + *off;
    741  1.15     rmind 	npf_ioctl_ent_t uent;
    742  1.15     rmind 
    743  1.15     rmind 	if ((*off += sizeof(npf_ioctl_ent_t)) > len) {
    744  1.15     rmind 		return ENOMEM;
    745  1.15     rmind 	}
    746  1.21     rmind 	uent.alen = alen;
    747  1.21     rmind 	memcpy(&uent.addr, addr, sizeof(npf_addr_t));
    748  1.15     rmind 	uent.mask = mask;
    749  1.15     rmind 
    750  1.15     rmind 	return copyout(&uent, ubufp, sizeof(npf_ioctl_ent_t));
    751  1.15     rmind }
    752  1.15     rmind 
    753  1.15     rmind static int
    754  1.29     rmind table_generic_list(const npf_table_t *t, void *ubuf, size_t len)
    755  1.15     rmind {
    756  1.24  christos 	npf_tblent_t *ent;
    757  1.24  christos 	size_t off = 0;
    758  1.15     rmind 	int error = 0;
    759  1.15     rmind 
    760  1.24  christos 	LIST_FOREACH(ent, &t->t_list, te_listent) {
    761  1.24  christos 		error = table_ent_copyout(&ent->te_addr,
    762  1.29     rmind 		    ent->te_alen, ent->te_preflen, ubuf, len, &off);
    763  1.21     rmind 		if (error)
    764  1.21     rmind 			break;
    765  1.21     rmind 	}
    766  1.21     rmind 	return error;
    767  1.21     rmind }
    768  1.21     rmind 
    769  1.21     rmind static int
    770  1.21     rmind table_cdb_list(npf_table_t *t, void *ubuf, size_t len)
    771  1.21     rmind {
    772  1.21     rmind 	size_t off = 0, dlen;
    773  1.21     rmind 	const void *data;
    774  1.21     rmind 	int error = 0;
    775  1.21     rmind 
    776  1.21     rmind 	for (size_t i = 0; i < t->t_nitems; i++) {
    777  1.21     rmind 		if (cdbr_get(t->t_cdb, i, &data, &dlen) != 0) {
    778  1.21     rmind 			return EINVAL;
    779  1.21     rmind 		}
    780  1.21     rmind 		error = table_ent_copyout(data, dlen, 0, ubuf, len, &off);
    781  1.15     rmind 		if (error)
    782  1.15     rmind 			break;
    783  1.15     rmind 	}
    784  1.15     rmind 	return error;
    785  1.15     rmind }
    786  1.15     rmind 
    787  1.15     rmind /*
    788  1.15     rmind  * npf_table_list: copy a list of all table entries into a userspace buffer.
    789  1.15     rmind  */
    790  1.15     rmind int
    791  1.19     rmind npf_table_list(npf_table_t *t, void *ubuf, size_t len)
    792  1.15     rmind {
    793  1.15     rmind 	int error = 0;
    794  1.15     rmind 
    795  1.28     rmind 	mutex_enter(&t->t_lock);
    796  1.15     rmind 	switch (t->t_type) {
    797  1.29     rmind 	case NPF_TABLE_IPSET:
    798  1.29     rmind 		error = table_generic_list(t, ubuf, len);
    799  1.15     rmind 		break;
    800  1.29     rmind 	case NPF_TABLE_LPM:
    801  1.29     rmind 		error = table_generic_list(t, ubuf, len);
    802  1.16     rmind 		break;
    803  1.29     rmind 	case NPF_TABLE_CONST:
    804  1.21     rmind 		error = table_cdb_list(t, ubuf, len);
    805  1.21     rmind 		break;
    806  1.29     rmind 	case NPF_TABLE_IFADDR:
    807  1.29     rmind 		error = table_generic_list(t, ubuf, len);
    808  1.29     rmind 		break;
    809  1.15     rmind 	default:
    810  1.15     rmind 		KASSERT(false);
    811  1.15     rmind 	}
    812  1.28     rmind 	mutex_exit(&t->t_lock);
    813  1.15     rmind 
    814  1.15     rmind 	return error;
    815  1.15     rmind }
    816  1.18     rmind 
    817  1.18     rmind /*
    818  1.18     rmind  * npf_table_flush: remove all table entries.
    819  1.18     rmind  */
    820  1.18     rmind int
    821  1.19     rmind npf_table_flush(npf_table_t *t)
    822  1.18     rmind {
    823  1.21     rmind 	int error = 0;
    824  1.21     rmind 
    825  1.28     rmind 	mutex_enter(&t->t_lock);
    826  1.18     rmind 	switch (t->t_type) {
    827  1.29     rmind 	case NPF_TABLE_IPSET:
    828  1.29     rmind 		table_ipset_flush(t);
    829  1.18     rmind 		break;
    830  1.29     rmind 	case NPF_TABLE_LPM:
    831  1.24  christos 		table_tree_flush(t);
    832  1.18     rmind 		break;
    833  1.29     rmind 	case NPF_TABLE_CONST:
    834  1.29     rmind 	case NPF_TABLE_IFADDR:
    835  1.21     rmind 		error = EINVAL;
    836  1.21     rmind 		break;
    837  1.18     rmind 	default:
    838  1.18     rmind 		KASSERT(false);
    839  1.18     rmind 	}
    840  1.28     rmind 	mutex_exit(&t->t_lock);
    841  1.21     rmind 	return error;
    842  1.18     rmind }
    843  1.29     rmind 
    844  1.29     rmind void
    845  1.29     rmind npf_table_gc(npf_t *npf, npf_table_t *t)
    846  1.29     rmind {
    847  1.29     rmind 	npf_tblent_t *ent;
    848  1.29     rmind 	void *ref;
    849  1.29     rmind 
    850  1.29     rmind 	if (t->t_type != NPF_TABLE_IPSET || LIST_EMPTY(&t->t_gc)) {
    851  1.29     rmind 		return;
    852  1.29     rmind 	}
    853  1.29     rmind 
    854  1.29     rmind 	ref = thmap_stage_gc(t->t_map);
    855  1.29     rmind 	if (npf) {
    856  1.29     rmind 		npf_config_locked_p(npf);
    857  1.29     rmind 		npf_config_sync(npf);
    858  1.29     rmind 	}
    859  1.29     rmind 	thmap_gc(t->t_map, ref);
    860  1.29     rmind 
    861  1.29     rmind 	while ((ent = LIST_FIRST(&t->t_gc)) != NULL) {
    862  1.29     rmind 		LIST_REMOVE(ent, te_listent);
    863  1.29     rmind 		pool_cache_put(tblent_cache, ent);
    864  1.29     rmind 	}
    865  1.29     rmind }
    866