Home | History | Annotate | Line # | Download | only in npf
npf_ctl.c revision 1.55
      1   1.1     rmind /*-
      2  1.54     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.1     rmind  * NPF device control.
     32   1.1     rmind  *
     33   1.1     rmind  * Implementation of (re)loading, construction of tables and rules.
     34  1.51     rmind  * NPF nvlist(3) consumer.
     35   1.1     rmind  */
     36   1.1     rmind 
     37  1.45  christos #ifdef _KERNEL
     38   1.1     rmind #include <sys/cdefs.h>
     39  1.55     rmind __KERNEL_RCSID(0, "$NetBSD: npf_ctl.c,v 1.55 2019/08/11 20:26:33 rmind Exp $");
     40   1.1     rmind 
     41   1.1     rmind #include <sys/param.h>
     42   1.1     rmind #include <sys/conf.h>
     43  1.21     rmind #include <sys/kmem.h>
     44  1.21     rmind #include <net/bpf.h>
     45  1.45  christos #endif
     46   1.1     rmind 
     47   1.1     rmind #include "npf_impl.h"
     48  1.34     rmind #include "npf_conn.h"
     49   1.1     rmind 
     50  1.51     rmind #define	NPF_IOCTL_DATA_LIMIT	(4 * 1024 * 1024)
     51  1.51     rmind 
     52  1.12     rmind #define	NPF_ERR_DEBUG(e) \
     53  1.51     rmind 	nvlist_add_string((e), "source-file", __FILE__); \
     54  1.51     rmind 	nvlist_add_number((e), "source-line", __LINE__);
     55  1.12     rmind 
     56  1.45  christos #ifdef _KERNEL
     57   1.1     rmind /*
     58   1.1     rmind  * npfctl_switch: enable or disable packet inspection.
     59   1.1     rmind  */
     60   1.1     rmind int
     61   1.1     rmind npfctl_switch(void *data)
     62   1.1     rmind {
     63   1.1     rmind 	const bool onoff = *(int *)data ? true : false;
     64   1.1     rmind 	int error;
     65   1.1     rmind 
     66   1.1     rmind 	if (onoff) {
     67   1.1     rmind 		/* Enable: add pfil hooks. */
     68  1.31     rmind 		error = npf_pfil_register(false);
     69   1.1     rmind 	} else {
     70   1.1     rmind 		/* Disable: remove pfil hooks. */
     71  1.31     rmind 		npf_pfil_unregister(false);
     72   1.1     rmind 		error = 0;
     73   1.1     rmind 	}
     74   1.1     rmind 	return error;
     75   1.1     rmind }
     76  1.45  christos #endif
     77   1.1     rmind 
     78  1.51     rmind static int
     79  1.52  christos npf_nvlist_copyin(npf_t *npf, void *data, nvlist_t **nvl)
     80  1.51     rmind {
     81  1.51     rmind 	int error = 0;
     82  1.51     rmind 
     83  1.53     rmind 	if (npf->mbufops == NULL) {
     84  1.53     rmind 		error = nvlist_copyin(data, nvl, NPF_IOCTL_DATA_LIMIT);
     85  1.53     rmind 	} else {
     86  1.52  christos 		*nvl = (nvlist_t *)data;
     87  1.53     rmind 	}
     88  1.51     rmind 	return error;
     89  1.51     rmind }
     90  1.51     rmind 
     91  1.51     rmind static int
     92  1.52  christos npf_nvlist_copyout(npf_t *npf, void *data, nvlist_t *nvl)
     93  1.51     rmind {
     94  1.51     rmind 	int error = 0;
     95  1.51     rmind 
     96  1.53     rmind 	if (npf->mbufops == NULL) {
     97  1.52  christos 		error = nvlist_copyout(data, nvl);
     98  1.53     rmind 	}
     99  1.51     rmind 	nvlist_destroy(nvl);
    100  1.51     rmind 	return error;
    101  1.51     rmind }
    102  1.51     rmind 
    103   1.6     rmind static int __noinline
    104  1.54     rmind npf_mk_params(npf_t *npf, nvlist_t *npf_dict, nvlist_t *errdict, bool set)
    105  1.54     rmind {
    106  1.54     rmind 	const nvlist_t *params;
    107  1.54     rmind 	int type, error, val;
    108  1.54     rmind 	const char *name;
    109  1.54     rmind 	void *cookie;
    110  1.54     rmind 
    111  1.54     rmind 	params = dnvlist_get_nvlist(npf_dict, "params", NULL);
    112  1.54     rmind 	if (params == NULL) {
    113  1.54     rmind 		return 0;
    114  1.54     rmind 	}
    115  1.54     rmind 	cookie = NULL;
    116  1.54     rmind 	while ((name = nvlist_next(params, &type, &cookie)) != NULL) {
    117  1.54     rmind 		if (type != NV_TYPE_NUMBER) {
    118  1.54     rmind 			NPF_ERR_DEBUG(errdict);
    119  1.54     rmind 			return EINVAL;
    120  1.54     rmind 		}
    121  1.54     rmind 		val = (int)nvlist_get_number(params, name);
    122  1.54     rmind 		if (set) {
    123  1.54     rmind 			/* Actually set the parameter. */
    124  1.55     rmind 			error = npfk_param_set(npf, name, val);
    125  1.54     rmind 			KASSERT(error == 0);
    126  1.54     rmind 			continue;
    127  1.54     rmind 		}
    128  1.54     rmind 
    129  1.54     rmind 		/* Validate the parameter and its value. */
    130  1.54     rmind 		error = npf_param_check(npf, name, val);
    131  1.54     rmind 		if (__predict_true(error == 0)) {
    132  1.54     rmind 			continue;
    133  1.54     rmind 		}
    134  1.54     rmind 		if (error == ENOENT) {
    135  1.54     rmind 			nvlist_add_stringf(errdict, "error-msg",
    136  1.54     rmind 			    "invalid parameter `%s`", name);
    137  1.54     rmind 		}
    138  1.54     rmind 		if (error == EINVAL) {
    139  1.54     rmind 			nvlist_add_stringf(errdict, "error-msg",
    140  1.54     rmind 			    "invalid parameter `%s` value %d", name, val);
    141  1.54     rmind 		}
    142  1.54     rmind 		return error;
    143  1.54     rmind 	}
    144  1.54     rmind 	return 0;
    145  1.54     rmind }
    146  1.54     rmind 
    147  1.54     rmind static int __noinline
    148  1.51     rmind npf_mk_table_entries(npf_table_t *t, const nvlist_t *table, nvlist_t *errdict)
    149  1.33     rmind {
    150  1.51     rmind 	const nvlist_t * const *entries;
    151  1.51     rmind 	size_t nitems;
    152  1.33     rmind 	int error = 0;
    153  1.33     rmind 
    154  1.51     rmind 	if (!nvlist_exists_nvlist_array(table, "entries")) {
    155  1.51     rmind 		return 0;
    156  1.39     rmind 	}
    157  1.51     rmind 	entries = nvlist_get_nvlist_array(table, "entries", &nitems);
    158  1.51     rmind 	for (unsigned i = 0; i < nitems; i++) {
    159  1.51     rmind 		const nvlist_t *entry = entries[i];
    160  1.33     rmind 		const npf_addr_t *addr;
    161  1.33     rmind 		npf_netmask_t mask;
    162  1.51     rmind 		size_t alen;
    163  1.33     rmind 
    164  1.33     rmind 		/* Get address and mask.  Add a table entry. */
    165  1.51     rmind 		addr = dnvlist_get_binary(entry, "addr", &alen, NULL, 0);
    166  1.51     rmind 		mask = dnvlist_get_number(entry, "mask", NPF_NO_NETMASK);
    167  1.51     rmind 		if (addr == NULL || alen == 0) {
    168  1.51     rmind 			NPF_ERR_DEBUG(errdict);
    169  1.51     rmind 			error = EINVAL;
    170  1.51     rmind 			break;
    171  1.51     rmind 		}
    172  1.33     rmind 		error = npf_table_insert(t, alen, addr, mask);
    173  1.51     rmind 		if (error) {
    174  1.51     rmind 			NPF_ERR_DEBUG(errdict);
    175  1.33     rmind 			break;
    176  1.51     rmind 		}
    177  1.33     rmind 	}
    178  1.33     rmind 	return error;
    179  1.33     rmind }
    180  1.33     rmind 
    181  1.33     rmind static int __noinline
    182  1.51     rmind npf_mk_tables(npf_t *npf, nvlist_t *npf_dict, nvlist_t *errdict,
    183  1.51     rmind     npf_tableset_t **tblsetp)
    184   1.1     rmind {
    185  1.51     rmind 	const nvlist_t * const *tables;
    186  1.51     rmind 	npf_tableset_t *tblset;
    187  1.51     rmind 	size_t nitems;
    188   1.1     rmind 	int error = 0;
    189   1.1     rmind 
    190  1.51     rmind 	if (nvlist_exists_nvlist_array(npf_dict, "tables")) {
    191  1.51     rmind 		tables = nvlist_get_nvlist_array(npf_dict, "tables", &nitems);
    192  1.51     rmind 		if (nitems > NPF_MAX_TABLES) {
    193  1.51     rmind 			NPF_ERR_DEBUG(errdict);
    194  1.51     rmind 			return E2BIG;
    195  1.51     rmind 		}
    196  1.51     rmind 	} else {
    197  1.51     rmind 		tables = NULL;
    198  1.51     rmind 		nitems = 0;
    199  1.12     rmind 	}
    200  1.51     rmind 	tblset = npf_tableset_create(nitems);
    201  1.51     rmind 	for (unsigned i = 0; i < nitems; i++) {
    202  1.51     rmind 		const nvlist_t *table = tables[i];
    203  1.32     rmind 		const char *name;
    204  1.51     rmind 		const void *blob;
    205   1.1     rmind 		npf_table_t *t;
    206  1.45  christos 		uint64_t tid;
    207  1.51     rmind 		size_t size;
    208   1.1     rmind 		int type;
    209   1.1     rmind 
    210  1.32     rmind 		/* Table name, ID and type.  Validate them. */
    211  1.51     rmind 		name = dnvlist_get_string(table, "name", NULL);
    212  1.51     rmind 		if (!name) {
    213  1.32     rmind 			NPF_ERR_DEBUG(errdict);
    214  1.32     rmind 			error = EINVAL;
    215  1.32     rmind 			break;
    216  1.32     rmind 		}
    217  1.51     rmind 		tid = dnvlist_get_number(table, "id", UINT64_MAX);
    218  1.51     rmind 		type = dnvlist_get_number(table, "type", UINT64_MAX);
    219  1.51     rmind 		error = npf_table_check(tblset, name, tid, type);
    220  1.32     rmind 		if (error) {
    221  1.32     rmind 			NPF_ERR_DEBUG(errdict);
    222   1.1     rmind 			break;
    223  1.32     rmind 		}
    224   1.1     rmind 
    225  1.33     rmind 		/* Get the entries or binary data. */
    226  1.51     rmind 		blob = dnvlist_get_binary(table, "data", &size, NULL, 0);
    227  1.53     rmind 		if (type == NPF_TABLE_CONST && (blob == NULL || size == 0)) {
    228  1.33     rmind 			NPF_ERR_DEBUG(errdict);
    229  1.33     rmind 			error = EINVAL;
    230  1.33     rmind 			break;
    231  1.33     rmind 		}
    232  1.33     rmind 
    233   1.1     rmind 		/* Create and insert the table. */
    234  1.45  christos 		t = npf_table_create(name, (u_int)tid, type, blob, size);
    235   1.1     rmind 		if (t == NULL) {
    236  1.12     rmind 			NPF_ERR_DEBUG(errdict);
    237   1.1     rmind 			error = ENOMEM;
    238   1.1     rmind 			break;
    239   1.1     rmind 		}
    240   1.1     rmind 		error = npf_tableset_insert(tblset, t);
    241   1.1     rmind 		KASSERT(error == 0);
    242   1.1     rmind 
    243  1.51     rmind 		if ((error = npf_mk_table_entries(t, table, errdict)) != 0) {
    244   1.1     rmind 			break;
    245   1.1     rmind 		}
    246   1.1     rmind 	}
    247  1.51     rmind 	*tblsetp = tblset;
    248   1.1     rmind 	return error;
    249   1.1     rmind }
    250   1.1     rmind 
    251   1.5     rmind static npf_rproc_t *
    252  1.51     rmind npf_mk_singlerproc(npf_t *npf, const nvlist_t *rproc, nvlist_t *errdict)
    253   1.5     rmind {
    254  1.51     rmind 	const nvlist_t * const *extcalls;
    255  1.51     rmind 	size_t nitems;
    256   1.5     rmind 	npf_rproc_t *rp;
    257  1.18     rmind 
    258  1.51     rmind 	if (!nvlist_exists_nvlist_array(rproc, "extcalls")) {
    259  1.51     rmind 		NPF_ERR_DEBUG(errdict);
    260  1.18     rmind 		return NULL;
    261  1.18     rmind 	}
    262  1.51     rmind 	rp = npf_rproc_create(rproc);
    263  1.21     rmind 	if (rp == NULL) {
    264  1.51     rmind 		NPF_ERR_DEBUG(errdict);
    265  1.18     rmind 		return NULL;
    266  1.18     rmind 	}
    267  1.51     rmind 	extcalls = nvlist_get_nvlist_array(rproc, "extcalls", &nitems);
    268  1.51     rmind 	for (unsigned i = 0; i < nitems; i++) {
    269  1.51     rmind 		const nvlist_t *extcall = extcalls[i];
    270  1.21     rmind 		const char *name;
    271  1.21     rmind 
    272  1.51     rmind 		name = dnvlist_get_string(extcall, "name", NULL);
    273  1.51     rmind 		if (!name || npf_ext_construct(npf, name, rp, extcall)) {
    274  1.51     rmind 			NPF_ERR_DEBUG(errdict);
    275  1.18     rmind 			npf_rproc_release(rp);
    276  1.18     rmind 			rp = NULL;
    277  1.18     rmind 			break;
    278  1.18     rmind 		}
    279  1.18     rmind 	}
    280  1.21     rmind 	return rp;
    281  1.21     rmind }
    282  1.21     rmind 
    283  1.21     rmind static int __noinline
    284  1.51     rmind npf_mk_rprocs(npf_t *npf, nvlist_t *npf_dict, nvlist_t *errdict,
    285  1.51     rmind     npf_rprocset_t **rpsetp)
    286  1.21     rmind {
    287  1.51     rmind 	const nvlist_t * const *rprocs;
    288  1.51     rmind 	npf_rprocset_t *rpset;
    289  1.51     rmind 	size_t nitems;
    290  1.21     rmind 	int error = 0;
    291  1.21     rmind 
    292  1.51     rmind 	if (nvlist_exists_nvlist_array(npf_dict, "rprocs")) {
    293  1.51     rmind 		rprocs = nvlist_get_nvlist_array(npf_dict, "rprocs", &nitems);
    294  1.51     rmind 		if (nitems > NPF_MAX_RPROCS) {
    295  1.51     rmind 			NPF_ERR_DEBUG(errdict);
    296  1.51     rmind 			return E2BIG;
    297  1.51     rmind 		}
    298  1.51     rmind 	} else {
    299  1.51     rmind 		rprocs = NULL;
    300  1.51     rmind 		nitems = 0;
    301  1.51     rmind 	}
    302  1.51     rmind 	rpset = npf_rprocset_create();
    303  1.51     rmind 	for (unsigned i = 0; i < nitems; i++) {
    304  1.51     rmind 		const nvlist_t *rproc = rprocs[i];
    305  1.21     rmind 		npf_rproc_t *rp;
    306  1.18     rmind 
    307  1.51     rmind 		if ((rp = npf_mk_singlerproc(npf, rproc, errdict)) == NULL) {
    308  1.21     rmind 			error = EINVAL;
    309  1.21     rmind 			break;
    310  1.21     rmind 		}
    311  1.21     rmind 		npf_rprocset_insert(rpset, rp);
    312   1.5     rmind 	}
    313  1.51     rmind 	*rpsetp = rpset;
    314  1.21     rmind 	return error;
    315   1.5     rmind }
    316   1.5     rmind 
    317  1.51     rmind static int __noinline
    318  1.51     rmind npf_mk_algs(npf_t *npf, nvlist_t *npf_dict, nvlist_t *errdict)
    319  1.24  christos {
    320  1.51     rmind 	const nvlist_t * const *algs;
    321  1.51     rmind 	size_t nitems;
    322  1.24  christos 
    323  1.51     rmind 	if (nvlist_exists_nvlist_array(npf_dict, "algs")) {
    324  1.51     rmind 		algs = nvlist_get_nvlist_array(npf_dict, "algs", &nitems);
    325  1.51     rmind 	} else {
    326  1.51     rmind 		algs = NULL;
    327  1.51     rmind 		nitems = 0;
    328  1.51     rmind 	}
    329  1.51     rmind 	for (unsigned i = 0; i < nitems; i++) {
    330  1.51     rmind 		const nvlist_t *alg = algs[i];
    331  1.51     rmind 		const char *name;
    332  1.24  christos 
    333  1.51     rmind 		name = dnvlist_get_string(alg, "name", NULL);
    334  1.51     rmind 		if (!name) {
    335  1.51     rmind 			NPF_ERR_DEBUG(errdict);
    336  1.51     rmind 			return EINVAL;
    337  1.51     rmind 		}
    338  1.51     rmind 		if (!npf_alg_construct(npf, name)) {
    339  1.24  christos 			NPF_ERR_DEBUG(errdict);
    340  1.51     rmind 			return EINVAL;
    341  1.24  christos 		}
    342  1.24  christos 	}
    343  1.12     rmind 	return 0;
    344  1.12     rmind }
    345  1.12     rmind 
    346  1.12     rmind static int __noinline
    347  1.51     rmind npf_mk_singlerule(npf_t *npf, const nvlist_t *rule, npf_rprocset_t *rpset,
    348  1.51     rmind     npf_rule_t **rlret, nvlist_t *errdict)
    349   1.1     rmind {
    350  1.21     rmind 	npf_rule_t *rl;
    351  1.21     rmind 	const char *rname;
    352  1.51     rmind 	const void *code;
    353  1.51     rmind 	size_t clen;
    354  1.51     rmind 	int error = 0;
    355   1.1     rmind 
    356  1.51     rmind 	if ((rl = npf_rule_alloc(npf, rule)) == NULL) {
    357  1.21     rmind 		NPF_ERR_DEBUG(errdict);
    358  1.21     rmind 		return EINVAL;
    359  1.21     rmind 	}
    360   1.1     rmind 
    361  1.51     rmind 	/* Assign the rule procedure, if any. */
    362  1.51     rmind 	if ((rname = dnvlist_get_string(rule, "rproc", NULL)) != NULL) {
    363  1.21     rmind 		npf_rproc_t *rp;
    364  1.21     rmind 
    365  1.21     rmind 		if (rpset == NULL) {
    366  1.51     rmind 			NPF_ERR_DEBUG(errdict);
    367  1.21     rmind 			error = EINVAL;
    368  1.21     rmind 			goto err;
    369  1.21     rmind 		}
    370  1.21     rmind 		if ((rp = npf_rprocset_lookup(rpset, rname)) == NULL) {
    371  1.18     rmind 			NPF_ERR_DEBUG(errdict);
    372  1.18     rmind 			error = EINVAL;
    373  1.18     rmind 			goto err;
    374  1.18     rmind 		}
    375  1.21     rmind 		npf_rule_setrproc(rl, rp);
    376  1.18     rmind 	}
    377  1.18     rmind 
    378  1.51     rmind 	/* Filter byte-code (binary data). */
    379  1.51     rmind 	code = dnvlist_get_binary(rule, "code", &clen, NULL, 0);
    380  1.51     rmind 	if (code) {
    381  1.51     rmind 		void *bc;
    382  1.21     rmind 		int type;
    383  1.21     rmind 
    384  1.51     rmind 		type = dnvlist_get_number(rule, "code-type", UINT64_MAX);
    385  1.51     rmind 		if (type != NPF_CODE_BPF) {
    386  1.51     rmind 			NPF_ERR_DEBUG(errdict);
    387  1.51     rmind 			error = ENOTSUP;
    388  1.51     rmind 			goto err;
    389  1.51     rmind 		}
    390  1.51     rmind 		if (clen == 0) {
    391  1.51     rmind 			NPF_ERR_DEBUG(errdict);
    392  1.51     rmind 			error = EINVAL;
    393  1.51     rmind 			goto err;
    394  1.51     rmind 		}
    395  1.51     rmind 		if (!npf_bpf_validate(code, clen)) {
    396  1.51     rmind 			NPF_ERR_DEBUG(errdict);
    397  1.51     rmind 			error = EINVAL;
    398  1.12     rmind 			goto err;
    399   1.4     rmind 		}
    400  1.51     rmind 		bc = kmem_alloc(clen, KM_SLEEP);
    401  1.51     rmind 		memcpy(bc, code, clen); // XXX: use nvlist_take
    402  1.51     rmind 		npf_rule_setcode(rl, type, bc, clen);
    403   1.1     rmind 	}
    404   1.1     rmind 
    405  1.21     rmind 	*rlret = rl;
    406   1.6     rmind 	return 0;
    407  1.12     rmind err:
    408  1.51     rmind 	nvlist_add_number(errdict, "id", dnvlist_get_number(rule, "prio", 0));
    409  1.21     rmind 	npf_rule_free(rl);
    410  1.12     rmind 	return error;
    411   1.6     rmind }
    412   1.6     rmind 
    413   1.6     rmind static int __noinline
    414  1.51     rmind npf_mk_rules(npf_t *npf, nvlist_t *npf_dict, nvlist_t *errdict,
    415  1.51     rmind      npf_rprocset_t *rpset, npf_ruleset_t **rlsetp)
    416   1.6     rmind {
    417  1.51     rmind 	const nvlist_t * const *rules;
    418  1.51     rmind 	npf_ruleset_t *rlset;
    419  1.51     rmind 	size_t nitems;
    420  1.51     rmind 	int error = 0;
    421   1.6     rmind 
    422  1.51     rmind 	if (nvlist_exists_nvlist_array(npf_dict, "rules")) {
    423  1.51     rmind 		rules = nvlist_get_nvlist_array(npf_dict, "rules", &nitems);
    424  1.51     rmind 		if (nitems > NPF_MAX_RULES) {
    425  1.51     rmind 			NPF_ERR_DEBUG(errdict);
    426  1.51     rmind 			return E2BIG;
    427  1.51     rmind 		}
    428  1.51     rmind 	} else {
    429  1.51     rmind 		rules = NULL;
    430  1.51     rmind 		nitems = 0;
    431   1.6     rmind 	}
    432  1.51     rmind 	rlset = npf_ruleset_create(nitems);
    433  1.51     rmind 	for (unsigned i = 0; i < nitems; i++) {
    434  1.51     rmind 		const nvlist_t *rule = rules[i];
    435  1.21     rmind 		npf_rule_t *rl = NULL;
    436  1.50     rmind 		const char *name;
    437   1.1     rmind 
    438  1.51     rmind 		error = npf_mk_singlerule(npf, rule, rpset, &rl, errdict);
    439   1.6     rmind 		if (error) {
    440   1.1     rmind 			break;
    441   1.6     rmind 		}
    442  1.51     rmind 		name = dnvlist_get_string(rule, "name", NULL);
    443  1.51     rmind 		if (name && npf_ruleset_lookup(rlset, name)) {
    444  1.50     rmind 			NPF_ERR_DEBUG(errdict);
    445  1.50     rmind 			npf_rule_free(rl);
    446  1.51     rmind 			error = EEXIST;
    447  1.51     rmind 			break;
    448  1.50     rmind 		}
    449   1.6     rmind 		npf_ruleset_insert(rlset, rl);
    450   1.1     rmind 	}
    451  1.51     rmind 	*rlsetp = rlset;
    452   1.1     rmind 	return error;
    453   1.1     rmind }
    454   1.1     rmind 
    455   1.6     rmind static int __noinline
    456  1.54     rmind npf_mk_singlenat(npf_t *npf, const nvlist_t *nat, npf_ruleset_t *ntset,
    457  1.54     rmind     npf_tableset_t *tblset, nvlist_t *errdict, npf_rule_t **rlp)
    458  1.54     rmind {
    459  1.54     rmind 	npf_rule_t *rl = NULL;
    460  1.54     rmind 	npf_natpolicy_t *np;
    461  1.54     rmind 	int error;
    462  1.54     rmind 
    463  1.54     rmind 	/*
    464  1.54     rmind 	 * NAT rules are standard rules, plus the translation policy.
    465  1.54     rmind 	 * We first construct the rule structure.
    466  1.54     rmind 	 */
    467  1.54     rmind 	error = npf_mk_singlerule(npf, nat, NULL, &rl, errdict);
    468  1.54     rmind 	if (error) {
    469  1.54     rmind 		return error;
    470  1.54     rmind 	}
    471  1.54     rmind 	KASSERT(rl != NULL);
    472  1.54     rmind 	*rlp = rl;
    473  1.54     rmind 
    474  1.54     rmind 	/* If rule is named, it is a group with NAT policies. */
    475  1.54     rmind 	if (dnvlist_get_string(nat, "name", NULL)) {
    476  1.54     rmind 		return 0;
    477  1.54     rmind 	}
    478  1.54     rmind 
    479  1.54     rmind 	/* Check the table ID. */
    480  1.54     rmind 	if (nvlist_exists_number(nat, "nat-table-id")) {
    481  1.54     rmind 		unsigned tid = nvlist_get_number(nat, "nat-table-id");
    482  1.54     rmind 
    483  1.54     rmind 		if (!npf_tableset_getbyid(tblset, tid)) {
    484  1.54     rmind 			NPF_ERR_DEBUG(errdict);
    485  1.54     rmind 			error = EINVAL;
    486  1.54     rmind 			goto out;
    487  1.54     rmind 		}
    488  1.54     rmind 	}
    489  1.54     rmind 
    490  1.54     rmind 	/* Allocate a new NAT policy and assign it to the rule. */
    491  1.54     rmind 	np = npf_nat_newpolicy(npf, nat, ntset);
    492  1.54     rmind 	if (np == NULL) {
    493  1.54     rmind 		NPF_ERR_DEBUG(errdict);
    494  1.54     rmind 		error = ENOMEM;
    495  1.54     rmind 		goto out;
    496  1.54     rmind 	}
    497  1.54     rmind 	npf_rule_setnat(rl, np);
    498  1.54     rmind out:
    499  1.54     rmind 	if (error) {
    500  1.54     rmind 		npf_rule_free(rl);
    501  1.54     rmind 	}
    502  1.54     rmind 	return error;
    503  1.54     rmind }
    504  1.54     rmind 
    505  1.54     rmind static int __noinline
    506  1.54     rmind npf_mk_natlist(npf_t *npf, nvlist_t *npf_dict, npf_tableset_t *tblset,
    507  1.54     rmind     nvlist_t *errdict, npf_ruleset_t **ntsetp)
    508   1.1     rmind {
    509  1.51     rmind 	const nvlist_t * const *nat_rules;
    510  1.51     rmind 	npf_ruleset_t *ntset;
    511  1.51     rmind 	size_t nitems;
    512  1.51     rmind 	int error = 0;
    513   1.1     rmind 
    514  1.51     rmind 	/*
    515  1.51     rmind 	 * NAT policies must be an array, but enforce a limit.
    516  1.51     rmind 	 */
    517  1.51     rmind 	if (nvlist_exists_nvlist_array(npf_dict, "nat")) {
    518  1.51     rmind 		nat_rules = nvlist_get_nvlist_array(npf_dict, "nat", &nitems);
    519  1.51     rmind 		if (nitems > NPF_MAX_RULES) {
    520  1.51     rmind 			NPF_ERR_DEBUG(errdict);
    521  1.51     rmind 			return E2BIG;
    522  1.51     rmind 		}
    523  1.51     rmind 	} else {
    524  1.51     rmind 		nat_rules = NULL;
    525  1.51     rmind 		nitems = 0;
    526  1.12     rmind 	}
    527  1.51     rmind 	ntset = npf_ruleset_create(nitems);
    528  1.51     rmind 	for (unsigned i = 0; i < nitems; i++) {
    529  1.51     rmind 		const nvlist_t *nat = nat_rules[i];
    530  1.21     rmind 		npf_rule_t *rl = NULL;
    531   1.1     rmind 
    532  1.54     rmind 		error = npf_mk_singlenat(npf, nat, ntset, tblset, errdict, &rl);
    533   1.6     rmind 		if (error) {
    534   1.1     rmind 			break;
    535   1.6     rmind 		}
    536  1.51     rmind 		npf_ruleset_insert(ntset, rl);
    537   1.1     rmind 	}
    538  1.51     rmind 	*ntsetp = ntset;
    539   1.1     rmind 	return error;
    540   1.1     rmind }
    541   1.1     rmind 
    542   1.1     rmind /*
    543  1.35     rmind  * npf_mk_connlist: import a list of connections and load them.
    544  1.35     rmind  */
    545  1.35     rmind static int __noinline
    546  1.51     rmind npf_mk_connlist(npf_t *npf, nvlist_t *npf_dict, nvlist_t *errdict,
    547  1.51     rmind     npf_ruleset_t *natlist, npf_conndb_t **conndb)
    548  1.35     rmind {
    549  1.51     rmind 	const nvlist_t * const *conns;
    550  1.35     rmind 	npf_conndb_t *cd;
    551  1.51     rmind 	size_t nitems;
    552  1.40     rmind 	int error = 0;
    553  1.35     rmind 
    554  1.51     rmind 	if (!nvlist_exists_nvlist_array(npf_dict, "conn-list")) {
    555  1.51     rmind 		*conndb = NULL;
    556  1.51     rmind 		return 0;
    557  1.35     rmind 	}
    558  1.51     rmind 	cd = npf_conndb_create();
    559  1.51     rmind 	conns = nvlist_get_nvlist_array(npf_dict, "conn-list", &nitems);
    560  1.51     rmind 	for (unsigned i = 0; i < nitems; i++) {
    561  1.51     rmind 		const nvlist_t *conn = conns[i];
    562  1.35     rmind 
    563  1.40     rmind 		/* Construct and insert the connection. */
    564  1.51     rmind 		error = npf_conn_import(npf, cd, conn, natlist);
    565  1.35     rmind 		if (error) {
    566  1.35     rmind 			NPF_ERR_DEBUG(errdict);
    567  1.35     rmind 			break;
    568  1.35     rmind 		}
    569  1.35     rmind 	}
    570  1.35     rmind 	if (error) {
    571  1.53     rmind 		npf_conndb_gc(npf, cd, true, false);
    572  1.35     rmind 		npf_conndb_destroy(cd);
    573  1.35     rmind 	} else {
    574  1.35     rmind 		*conndb = cd;
    575  1.35     rmind 	}
    576  1.35     rmind 	return error;
    577  1.35     rmind }
    578  1.35     rmind 
    579  1.35     rmind /*
    580  1.51     rmind  * npfctl_load_nvlist: store passed data i.e. the update settings, create
    581  1.51     rmind  * the passed tables, rules, etc and atomically activate all them.
    582   1.1     rmind  */
    583  1.51     rmind static int
    584  1.51     rmind npfctl_load_nvlist(npf_t *npf, nvlist_t *npf_dict, nvlist_t *errdict)
    585   1.1     rmind {
    586   1.1     rmind 	npf_tableset_t *tblset = NULL;
    587  1.51     rmind 	npf_ruleset_t *ntset = NULL;
    588  1.21     rmind 	npf_rprocset_t *rpset = NULL;
    589   1.1     rmind 	npf_ruleset_t *rlset = NULL;
    590  1.35     rmind 	npf_conndb_t *conndb = NULL;
    591  1.51     rmind 	uint64_t ver;
    592  1.11     rmind 	bool flush;
    593   1.1     rmind 	int error;
    594   1.1     rmind 
    595  1.51     rmind 	ver = dnvlist_get_number(npf_dict, "version", UINT64_MAX);
    596  1.20     rmind 	if (ver != NPF_VERSION) {
    597  1.20     rmind 		error = EPROGMISMATCH;
    598  1.20     rmind 		goto fail;
    599  1.20     rmind 	}
    600  1.54     rmind 	error = npf_mk_params(npf, npf_dict, errdict, false /* validate */);
    601  1.54     rmind 	if (error) {
    602  1.54     rmind 		goto fail;
    603  1.54     rmind 	}
    604  1.51     rmind 	error = npf_mk_algs(npf, npf_dict, errdict);
    605  1.24  christos 	if (error) {
    606  1.24  christos 		goto fail;
    607  1.24  christos 	}
    608  1.53     rmind 	error = npf_mk_tables(npf, npf_dict, errdict, &tblset);
    609  1.51     rmind 	if (error) {
    610  1.30     rmind 		goto fail;
    611  1.30     rmind 	}
    612  1.53     rmind 	error = npf_mk_rprocs(npf, npf_dict, errdict, &rpset);
    613  1.10     rmind 	if (error) {
    614   1.1     rmind 		goto fail;
    615  1.10     rmind 	}
    616  1.54     rmind 	error = npf_mk_natlist(npf, npf_dict, tblset, errdict, &ntset);
    617  1.10     rmind 	if (error) {
    618   1.1     rmind 		goto fail;
    619  1.10     rmind 	}
    620  1.51     rmind 	error = npf_mk_rules(npf, npf_dict, errdict, rpset, &rlset);
    621  1.21     rmind 	if (error) {
    622  1.21     rmind 		goto fail;
    623  1.21     rmind 	}
    624  1.51     rmind 	error = npf_mk_connlist(npf, npf_dict, errdict, ntset, &conndb);
    625  1.10     rmind 	if (error) {
    626   1.1     rmind 		goto fail;
    627  1.10     rmind 	}
    628   1.1     rmind 
    629   1.1     rmind 	/*
    630  1.35     rmind 	 * Finally - perform the load.
    631   1.1     rmind 	 */
    632  1.51     rmind 	flush = dnvlist_get_bool(npf_dict, "flush", false);
    633  1.51     rmind 	npf_config_load(npf, rlset, tblset, ntset, rpset, conndb, flush);
    634  1.54     rmind 	npf_mk_params(npf, npf_dict, errdict, true /* set the params */);
    635  1.11     rmind 
    636   1.1     rmind 	/* Done.  Since data is consumed now, we shall not destroy it. */
    637   1.1     rmind 	tblset = NULL;
    638  1.21     rmind 	rpset = NULL;
    639   1.1     rmind 	rlset = NULL;
    640  1.51     rmind 	ntset = NULL;
    641   1.1     rmind fail:
    642   1.1     rmind 	/*
    643  1.51     rmind 	 * Note: the rulesets must be destroyed first, in order to drop
    644  1.51     rmind 	 * any references to the tableset.
    645   1.1     rmind 	 */
    646  1.53     rmind 	if (rlset) {
    647  1.53     rmind 		npf_ruleset_destroy(rlset);
    648  1.53     rmind 	}
    649  1.51     rmind 	if (ntset) {
    650  1.51     rmind 		npf_ruleset_destroy(ntset);
    651   1.1     rmind 	}
    652  1.21     rmind 	if (rpset) {
    653  1.21     rmind 		npf_rprocset_destroy(rpset);
    654  1.21     rmind 	}
    655   1.1     rmind 	if (tblset) {
    656   1.1     rmind 		npf_tableset_destroy(tblset);
    657   1.1     rmind 	}
    658  1.51     rmind 	nvlist_destroy(npf_dict);
    659  1.51     rmind 	return error;
    660  1.51     rmind }
    661  1.51     rmind 
    662  1.51     rmind int
    663  1.51     rmind npfctl_load(npf_t *npf, u_long cmd, void *data)
    664  1.51     rmind {
    665  1.51     rmind 	nvlist_t *request, *response;
    666  1.51     rmind 	int error;
    667  1.12     rmind 
    668  1.49     ozaki 	/*
    669  1.51     rmind 	 * Retrieve the configuration and check the version.
    670  1.51     rmind 	 * Construct a response with error reporting.
    671  1.49     ozaki 	 */
    672  1.52  christos 	error = npf_nvlist_copyin(npf, data, &request);
    673  1.51     rmind 	if (error) {
    674  1.51     rmind 		return error;
    675  1.49     ozaki 	}
    676  1.51     rmind 	response = nvlist_create(0);
    677  1.51     rmind 	error = npfctl_load_nvlist(npf, request, response);
    678  1.51     rmind 	nvlist_add_number(response, "errno", error);
    679  1.52  christos 	return npf_nvlist_copyout(npf, data, response);
    680   1.6     rmind }
    681   1.6     rmind 
    682  1.21     rmind /*
    683  1.35     rmind  * npfctl_save: export the config dictionary as it was submitted,
    684  1.35     rmind  * including the current snapshot of the connections.  Additionally,
    685  1.35     rmind  * indicate whether the ruleset is currently active.
    686  1.35     rmind  */
    687  1.35     rmind int
    688  1.45  christos npfctl_save(npf_t *npf, u_long cmd, void *data)
    689  1.35     rmind {
    690  1.51     rmind 	nvlist_t *npf_dict;
    691  1.35     rmind 	int error;
    692  1.35     rmind 
    693  1.51     rmind 	npf_dict = nvlist_create(0);
    694  1.51     rmind 	nvlist_add_number(npf_dict, "version", NPF_VERSION);
    695  1.35     rmind 
    696  1.37     rmind 	/*
    697  1.51     rmind 	 * Serialise the whole NPF config, including connections.
    698  1.37     rmind 	 */
    699  1.45  christos 	npf_config_enter(npf);
    700  1.51     rmind 	error = npf_conndb_export(npf, npf_dict);
    701  1.37     rmind 	if (error) {
    702  1.37     rmind 		goto out;
    703  1.37     rmind 	}
    704  1.51     rmind 	error = npf_ruleset_export(npf, npf_config_ruleset(npf), "rules", npf_dict);
    705  1.38     rmind 	if (error) {
    706  1.38     rmind 		goto out;
    707  1.38     rmind 	}
    708  1.51     rmind 	error = npf_ruleset_export(npf, npf_config_natset(npf), "nat", npf_dict);
    709  1.35     rmind 	if (error) {
    710  1.35     rmind 		goto out;
    711  1.35     rmind 	}
    712  1.51     rmind 	error = npf_tableset_export(npf, npf_config_tableset(npf), npf_dict);
    713  1.38     rmind 	if (error) {
    714  1.38     rmind 		goto out;
    715  1.38     rmind 	}
    716  1.51     rmind 	error = npf_rprocset_export(npf_config_rprocs(npf), npf_dict);
    717  1.38     rmind 	if (error) {
    718  1.38     rmind 		goto out;
    719  1.38     rmind 	}
    720  1.51     rmind 	error = npf_alg_export(npf, npf_dict);
    721  1.51     rmind 	if (error) {
    722  1.51     rmind 		goto out;
    723  1.51     rmind 	}
    724  1.51     rmind 	nvlist_add_bool(npf_dict, "active", npf_pfil_registered_p());
    725  1.52  christos 	error = npf_nvlist_copyout(npf, data, npf_dict);
    726  1.51     rmind 	npf_dict = NULL;
    727  1.35     rmind out:
    728  1.45  christos 	npf_config_exit(npf);
    729  1.51     rmind 	if (npf_dict) {
    730  1.51     rmind 		nvlist_destroy(npf_dict);
    731  1.37     rmind 	}
    732  1.35     rmind 	return error;
    733  1.35     rmind }
    734  1.35     rmind 
    735  1.35     rmind /*
    736  1.44  christos  * npfctl_conn_lookup: lookup a connection in the list of connections
    737  1.44  christos  */
    738  1.44  christos int
    739  1.45  christos npfctl_conn_lookup(npf_t *npf, u_long cmd, void *data)
    740  1.44  christos {
    741  1.51     rmind 	nvlist_t *conn_data, *conn_result;
    742  1.44  christos 	int error;
    743  1.44  christos 
    744  1.52  christos 	error = npf_nvlist_copyin(npf, data, &conn_data);
    745  1.44  christos 	if (error) {
    746  1.44  christos 		return error;
    747  1.44  christos 	}
    748  1.45  christos 	error = npf_conn_find(npf, conn_data, &conn_result);
    749  1.44  christos 	if (error) {
    750  1.44  christos 		goto out;
    751  1.44  christos 	}
    752  1.52  christos 	error = npf_nvlist_copyout(npf, data, conn_result);
    753  1.44  christos out:
    754  1.51     rmind 	nvlist_destroy(conn_data);
    755  1.44  christos 	return error;
    756  1.44  christos }
    757  1.44  christos 
    758  1.44  christos /*
    759  1.21     rmind  * npfctl_rule: add or remove dynamic rules in the specified ruleset.
    760  1.21     rmind  */
    761  1.14     rmind int
    762  1.45  christos npfctl_rule(npf_t *npf, u_long cmd, void *data)
    763  1.14     rmind {
    764  1.51     rmind 	nvlist_t *npf_rule, *retdict = NULL;
    765  1.21     rmind 	npf_ruleset_t *rlset;
    766  1.21     rmind 	npf_rule_t *rl = NULL;
    767  1.21     rmind 	const char *ruleset_name;
    768  1.51     rmind 	uint32_t rcmd;
    769  1.45  christos 	int error = 0;
    770  1.54     rmind 	bool natset;
    771  1.14     rmind 
    772  1.52  christos 	error = npf_nvlist_copyin(npf, data, &npf_rule);
    773  1.21     rmind 	if (error) {
    774  1.21     rmind 		return error;
    775  1.21     rmind 	}
    776  1.51     rmind 	rcmd = dnvlist_get_number(npf_rule, "command", 0);
    777  1.54     rmind 	natset = dnvlist_get_bool(npf_rule, "nat-rule", false);
    778  1.51     rmind 	ruleset_name = dnvlist_get_string(npf_rule, "ruleset-name", NULL);
    779  1.51     rmind 	if (!ruleset_name) {
    780  1.27     rmind 		error = EINVAL;
    781  1.27     rmind 		goto out;
    782  1.21     rmind 	}
    783  1.21     rmind 
    784  1.54     rmind 	npf_config_enter(npf);
    785  1.54     rmind 	rlset = natset ? npf_config_natset(npf) : npf_config_ruleset(npf);
    786  1.54     rmind 	switch (rcmd) {
    787  1.54     rmind 	case NPF_CMD_RULE_ADD: {
    788  1.51     rmind 		retdict = nvlist_create(0);
    789  1.54     rmind 		if (natset) {
    790  1.54     rmind 			/*
    791  1.54     rmind 			 * Translation rule.
    792  1.54     rmind 			 */
    793  1.54     rmind 			error = npf_mk_singlenat(npf, npf_rule, rlset,
    794  1.54     rmind 			    npf_config_tableset(npf), retdict, &rl);
    795  1.54     rmind 		} else {
    796  1.54     rmind 			/*
    797  1.54     rmind 			 * Standard rule.
    798  1.54     rmind 			 */
    799  1.54     rmind 			error = npf_mk_singlerule(npf, npf_rule, NULL,
    800  1.54     rmind 			    &rl, retdict);
    801  1.54     rmind 		}
    802  1.51     rmind 		if (error) {
    803  1.54     rmind 			npf_config_exit(npf);
    804  1.27     rmind 			goto out;
    805  1.21     rmind 		}
    806  1.21     rmind 		if ((error = npf_ruleset_add(rlset, ruleset_name, rl)) == 0) {
    807  1.21     rmind 			/* Success. */
    808  1.23     rmind 			uint64_t id = npf_rule_getid(rl);
    809  1.51     rmind 			nvlist_add_number(retdict, "id", id);
    810  1.21     rmind 			rl = NULL;
    811  1.21     rmind 		}
    812  1.21     rmind 		break;
    813  1.21     rmind 	}
    814  1.21     rmind 	case NPF_CMD_RULE_REMOVE: {
    815  1.51     rmind 		uint64_t id = dnvlist_get_number(npf_rule, "id", UINT64_MAX);
    816  1.23     rmind 		error = npf_ruleset_remove(rlset, ruleset_name, id);
    817  1.21     rmind 		break;
    818  1.21     rmind 	}
    819  1.21     rmind 	case NPF_CMD_RULE_REMKEY: {
    820  1.51     rmind 		const void *key;
    821  1.51     rmind 		size_t len;
    822  1.21     rmind 
    823  1.51     rmind 		key = dnvlist_get_binary(npf_rule, "key", &len, NULL, 0);
    824  1.21     rmind 		if (len == 0 || len > NPF_RULE_MAXKEYLEN) {
    825  1.21     rmind 			error = EINVAL;
    826  1.21     rmind 			break;
    827  1.21     rmind 		}
    828  1.22     rmind 		error = npf_ruleset_remkey(rlset, ruleset_name, key, len);
    829  1.22     rmind 		break;
    830  1.22     rmind 	}
    831  1.22     rmind 	case NPF_CMD_RULE_LIST: {
    832  1.45  christos 		retdict = npf_ruleset_list(npf, rlset, ruleset_name);
    833  1.41     rmind 		if (!retdict) {
    834  1.41     rmind 			error = ESRCH;
    835  1.41     rmind 		}
    836  1.22     rmind 		break;
    837  1.22     rmind 	}
    838  1.22     rmind 	case NPF_CMD_RULE_FLUSH: {
    839  1.22     rmind 		error = npf_ruleset_flush(rlset, ruleset_name);
    840  1.21     rmind 		break;
    841  1.21     rmind 	}
    842  1.21     rmind 	default:
    843  1.21     rmind 		error = EINVAL;
    844  1.21     rmind 		break;
    845  1.21     rmind 	}
    846  1.22     rmind 
    847  1.22     rmind 	/* Destroy any removed rules. */
    848  1.22     rmind 	if (!error && rcmd != NPF_CMD_RULE_ADD && rcmd != NPF_CMD_RULE_LIST) {
    849  1.45  christos 		npf_config_sync(npf);
    850  1.22     rmind 		npf_ruleset_gc(rlset);
    851  1.22     rmind 	}
    852  1.45  christos 	npf_config_exit(npf);
    853  1.14     rmind 
    854  1.21     rmind 	if (rl) {
    855  1.41     rmind 		KASSERT(error);
    856  1.21     rmind 		npf_rule_free(rl);
    857  1.21     rmind 	}
    858  1.27     rmind out:
    859  1.52  christos 	if (retdict && npf_nvlist_copyout(npf, data, retdict) != 0) {
    860  1.51     rmind 		error = EFAULT; // copyout failure
    861  1.21     rmind 	}
    862  1.51     rmind 	nvlist_destroy(npf_rule);
    863  1.14     rmind 	return error;
    864  1.14     rmind }
    865  1.14     rmind 
    866   1.6     rmind /*
    867   1.2     rmind  * npfctl_table: add, remove or query entries in the specified table.
    868   1.1     rmind  *
    869  1.51     rmind  * For maximum performance, the interface is using plain structures.
    870   1.1     rmind  */
    871   1.1     rmind int
    872  1.45  christos npfctl_table(npf_t *npf, void *data)
    873   1.1     rmind {
    874  1.19     rmind 	const npf_ioctl_table_t *nct = data;
    875  1.32     rmind 	char tname[NPF_TABLE_MAXNAMELEN];
    876  1.32     rmind 	npf_tableset_t *ts;
    877  1.32     rmind 	npf_table_t *t;
    878  1.53     rmind 	int error;
    879  1.32     rmind 
    880  1.32     rmind 	error = copyinstr(nct->nct_name, tname, sizeof(tname), NULL);
    881  1.32     rmind 	if (error) {
    882  1.32     rmind 		return error;
    883  1.32     rmind 	}
    884   1.1     rmind 
    885  1.53     rmind 	npf_config_enter(npf);
    886  1.45  christos 	ts = npf_config_tableset(npf);
    887  1.32     rmind 	if ((t = npf_tableset_getbyname(ts, tname)) == NULL) {
    888  1.53     rmind 		npf_config_exit(npf);
    889  1.32     rmind 		return EINVAL;
    890  1.32     rmind 	}
    891  1.21     rmind 
    892  1.21     rmind 	switch (nct->nct_cmd) {
    893  1.21     rmind 	case NPF_CMD_TABLE_LOOKUP:
    894  1.32     rmind 		error = npf_table_lookup(t, nct->nct_data.ent.alen,
    895  1.32     rmind 		    &nct->nct_data.ent.addr);
    896  1.20     rmind 		break;
    897  1.21     rmind 	case NPF_CMD_TABLE_ADD:
    898  1.32     rmind 		error = npf_table_insert(t, nct->nct_data.ent.alen,
    899  1.32     rmind 		    &nct->nct_data.ent.addr, nct->nct_data.ent.mask);
    900   1.1     rmind 		break;
    901  1.21     rmind 	case NPF_CMD_TABLE_REMOVE:
    902  1.32     rmind 		error = npf_table_remove(t, nct->nct_data.ent.alen,
    903  1.32     rmind 		    &nct->nct_data.ent.addr, nct->nct_data.ent.mask);
    904  1.19     rmind 		break;
    905  1.21     rmind 	case NPF_CMD_TABLE_LIST:
    906  1.32     rmind 		error = npf_table_list(t, nct->nct_data.buf.buf,
    907  1.32     rmind 		    nct->nct_data.buf.len);
    908   1.1     rmind 		break;
    909  1.25     rmind 	case NPF_CMD_TABLE_FLUSH:
    910  1.32     rmind 		error = npf_table_flush(t);
    911  1.25     rmind 		break;
    912   1.1     rmind 	default:
    913  1.19     rmind 		error = EINVAL;
    914  1.19     rmind 		break;
    915   1.1     rmind 	}
    916  1.53     rmind 	npf_table_gc(npf, t);
    917  1.53     rmind 	npf_config_exit(npf);
    918  1.21     rmind 
    919   1.1     rmind 	return error;
    920   1.1     rmind }
    921