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