Home | History | Annotate | Line # | Download | only in npf
npf_ctl.c revision 1.57
      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.57     rmind __KERNEL_RCSID(0, "$NetBSD: npf_ctl.c,v 1.57 2019/08/25 13:21:03 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.56     rmind /*
    182  1.56     rmind  * npf_mk_table: create a table from provided nvlist.
    183  1.56     rmind  */
    184  1.56     rmind static int __noinline
    185  1.56     rmind npf_mk_table(npf_t *npf, const nvlist_t *tbl_dict, nvlist_t *errdict,
    186  1.56     rmind     npf_tableset_t *tblset, npf_table_t **tblp, bool replacing)
    187  1.56     rmind {
    188  1.56     rmind 	npf_table_t *t;
    189  1.56     rmind 	const char *name;
    190  1.56     rmind 	const void *blob;
    191  1.56     rmind 	uint64_t tid;
    192  1.56     rmind 	size_t size;
    193  1.56     rmind 	int type;
    194  1.56     rmind 	int error = 0;
    195  1.56     rmind 
    196  1.56     rmind 	KASSERT(tblp != NULL);
    197  1.56     rmind 
    198  1.56     rmind 	/* Table name, ID and type.  Validate them. */
    199  1.56     rmind 	name = dnvlist_get_string(tbl_dict, "name", NULL);
    200  1.56     rmind 	if (!name) {
    201  1.56     rmind 		NPF_ERR_DEBUG(errdict);
    202  1.56     rmind 		error = EINVAL;
    203  1.56     rmind 		goto out;
    204  1.56     rmind 	}
    205  1.56     rmind 	tid = dnvlist_get_number(tbl_dict, "id", UINT64_MAX);
    206  1.56     rmind 	type = dnvlist_get_number(tbl_dict, "type", UINT64_MAX);
    207  1.56     rmind 	error = npf_table_check(tblset, name, tid, type, replacing);
    208  1.56     rmind 	if (error) {
    209  1.56     rmind 		NPF_ERR_DEBUG(errdict);
    210  1.56     rmind 		goto out;
    211  1.56     rmind 	}
    212  1.56     rmind 
    213  1.56     rmind 	/* Get the entries or binary data. */
    214  1.56     rmind 	blob = dnvlist_get_binary(tbl_dict, "data", &size, NULL, 0);
    215  1.56     rmind 	if (type == NPF_TABLE_CONST && (blob == NULL || size == 0)) {
    216  1.56     rmind 		NPF_ERR_DEBUG(errdict);
    217  1.56     rmind 		error = EINVAL;
    218  1.56     rmind 		goto out;
    219  1.56     rmind 	}
    220  1.56     rmind 
    221  1.56     rmind 	t = npf_table_create(name, (u_int)tid, type, blob, size);
    222  1.56     rmind 	if (t == NULL) {
    223  1.56     rmind 		NPF_ERR_DEBUG(errdict);
    224  1.56     rmind 		error = ENOMEM;
    225  1.56     rmind 		goto out;
    226  1.56     rmind 	}
    227  1.56     rmind 
    228  1.56     rmind 	if ((error = npf_mk_table_entries(t, tbl_dict, errdict)) != 0) {
    229  1.56     rmind 		npf_table_destroy(t);
    230  1.56     rmind 		goto out;
    231  1.56     rmind 	}
    232  1.56     rmind 
    233  1.56     rmind 	*tblp = t;
    234  1.56     rmind out:
    235  1.56     rmind 	return error;
    236  1.56     rmind }
    237  1.56     rmind 
    238  1.33     rmind static int __noinline
    239  1.51     rmind npf_mk_tables(npf_t *npf, nvlist_t *npf_dict, nvlist_t *errdict,
    240  1.57     rmind     npf_config_t *nc)
    241   1.1     rmind {
    242  1.51     rmind 	const nvlist_t * const *tables;
    243  1.51     rmind 	npf_tableset_t *tblset;
    244  1.51     rmind 	size_t nitems;
    245   1.1     rmind 	int error = 0;
    246   1.1     rmind 
    247  1.51     rmind 	if (nvlist_exists_nvlist_array(npf_dict, "tables")) {
    248  1.51     rmind 		tables = nvlist_get_nvlist_array(npf_dict, "tables", &nitems);
    249  1.51     rmind 		if (nitems > NPF_MAX_TABLES) {
    250  1.51     rmind 			NPF_ERR_DEBUG(errdict);
    251  1.51     rmind 			return E2BIG;
    252  1.51     rmind 		}
    253  1.51     rmind 	} else {
    254  1.51     rmind 		tables = NULL;
    255  1.51     rmind 		nitems = 0;
    256  1.12     rmind 	}
    257  1.51     rmind 	tblset = npf_tableset_create(nitems);
    258  1.51     rmind 	for (unsigned i = 0; i < nitems; i++) {
    259  1.51     rmind 		const nvlist_t *table = tables[i];
    260   1.1     rmind 		npf_table_t *t;
    261   1.1     rmind 
    262  1.56     rmind 		error = npf_mk_table(npf, table, errdict, tblset, &t, 0);
    263  1.32     rmind 		if (error) {
    264  1.33     rmind 			break;
    265  1.33     rmind 		}
    266  1.33     rmind 
    267   1.1     rmind 		error = npf_tableset_insert(tblset, t);
    268   1.1     rmind 		KASSERT(error == 0);
    269   1.1     rmind 	}
    270  1.57     rmind 	nc->tableset = tblset;
    271   1.1     rmind 	return error;
    272   1.1     rmind }
    273   1.1     rmind 
    274   1.5     rmind static npf_rproc_t *
    275  1.51     rmind npf_mk_singlerproc(npf_t *npf, const nvlist_t *rproc, nvlist_t *errdict)
    276   1.5     rmind {
    277  1.51     rmind 	const nvlist_t * const *extcalls;
    278  1.51     rmind 	size_t nitems;
    279   1.5     rmind 	npf_rproc_t *rp;
    280  1.18     rmind 
    281  1.51     rmind 	if (!nvlist_exists_nvlist_array(rproc, "extcalls")) {
    282  1.51     rmind 		NPF_ERR_DEBUG(errdict);
    283  1.18     rmind 		return NULL;
    284  1.18     rmind 	}
    285  1.51     rmind 	rp = npf_rproc_create(rproc);
    286  1.21     rmind 	if (rp == NULL) {
    287  1.51     rmind 		NPF_ERR_DEBUG(errdict);
    288  1.18     rmind 		return NULL;
    289  1.18     rmind 	}
    290  1.51     rmind 	extcalls = nvlist_get_nvlist_array(rproc, "extcalls", &nitems);
    291  1.51     rmind 	for (unsigned i = 0; i < nitems; i++) {
    292  1.51     rmind 		const nvlist_t *extcall = extcalls[i];
    293  1.21     rmind 		const char *name;
    294  1.21     rmind 
    295  1.51     rmind 		name = dnvlist_get_string(extcall, "name", NULL);
    296  1.51     rmind 		if (!name || npf_ext_construct(npf, name, rp, extcall)) {
    297  1.51     rmind 			NPF_ERR_DEBUG(errdict);
    298  1.18     rmind 			npf_rproc_release(rp);
    299  1.18     rmind 			rp = NULL;
    300  1.18     rmind 			break;
    301  1.18     rmind 		}
    302  1.18     rmind 	}
    303  1.21     rmind 	return rp;
    304  1.21     rmind }
    305  1.21     rmind 
    306  1.21     rmind static int __noinline
    307  1.51     rmind npf_mk_rprocs(npf_t *npf, nvlist_t *npf_dict, nvlist_t *errdict,
    308  1.57     rmind     npf_config_t *nc)
    309  1.21     rmind {
    310  1.51     rmind 	const nvlist_t * const *rprocs;
    311  1.51     rmind 	npf_rprocset_t *rpset;
    312  1.51     rmind 	size_t nitems;
    313  1.21     rmind 	int error = 0;
    314  1.21     rmind 
    315  1.51     rmind 	if (nvlist_exists_nvlist_array(npf_dict, "rprocs")) {
    316  1.51     rmind 		rprocs = nvlist_get_nvlist_array(npf_dict, "rprocs", &nitems);
    317  1.51     rmind 		if (nitems > NPF_MAX_RPROCS) {
    318  1.51     rmind 			NPF_ERR_DEBUG(errdict);
    319  1.51     rmind 			return E2BIG;
    320  1.51     rmind 		}
    321  1.51     rmind 	} else {
    322  1.51     rmind 		rprocs = NULL;
    323  1.51     rmind 		nitems = 0;
    324  1.51     rmind 	}
    325  1.51     rmind 	rpset = npf_rprocset_create();
    326  1.51     rmind 	for (unsigned i = 0; i < nitems; i++) {
    327  1.51     rmind 		const nvlist_t *rproc = rprocs[i];
    328  1.21     rmind 		npf_rproc_t *rp;
    329  1.18     rmind 
    330  1.51     rmind 		if ((rp = npf_mk_singlerproc(npf, rproc, errdict)) == NULL) {
    331  1.21     rmind 			error = EINVAL;
    332  1.21     rmind 			break;
    333  1.21     rmind 		}
    334  1.21     rmind 		npf_rprocset_insert(rpset, rp);
    335   1.5     rmind 	}
    336  1.57     rmind 	nc->rule_procs = rpset;
    337  1.21     rmind 	return error;
    338   1.5     rmind }
    339   1.5     rmind 
    340  1.51     rmind static int __noinline
    341  1.51     rmind npf_mk_algs(npf_t *npf, nvlist_t *npf_dict, nvlist_t *errdict)
    342  1.24  christos {
    343  1.51     rmind 	const nvlist_t * const *algs;
    344  1.51     rmind 	size_t nitems;
    345  1.24  christos 
    346  1.51     rmind 	if (nvlist_exists_nvlist_array(npf_dict, "algs")) {
    347  1.51     rmind 		algs = nvlist_get_nvlist_array(npf_dict, "algs", &nitems);
    348  1.51     rmind 	} else {
    349  1.51     rmind 		algs = NULL;
    350  1.51     rmind 		nitems = 0;
    351  1.51     rmind 	}
    352  1.51     rmind 	for (unsigned i = 0; i < nitems; i++) {
    353  1.51     rmind 		const nvlist_t *alg = algs[i];
    354  1.51     rmind 		const char *name;
    355  1.24  christos 
    356  1.51     rmind 		name = dnvlist_get_string(alg, "name", NULL);
    357  1.51     rmind 		if (!name) {
    358  1.51     rmind 			NPF_ERR_DEBUG(errdict);
    359  1.51     rmind 			return EINVAL;
    360  1.51     rmind 		}
    361  1.51     rmind 		if (!npf_alg_construct(npf, name)) {
    362  1.24  christos 			NPF_ERR_DEBUG(errdict);
    363  1.51     rmind 			return EINVAL;
    364  1.24  christos 		}
    365  1.24  christos 	}
    366  1.12     rmind 	return 0;
    367  1.12     rmind }
    368  1.12     rmind 
    369  1.12     rmind static int __noinline
    370  1.51     rmind npf_mk_singlerule(npf_t *npf, const nvlist_t *rule, npf_rprocset_t *rpset,
    371  1.51     rmind     npf_rule_t **rlret, nvlist_t *errdict)
    372   1.1     rmind {
    373  1.21     rmind 	npf_rule_t *rl;
    374  1.21     rmind 	const char *rname;
    375  1.51     rmind 	const void *code;
    376  1.51     rmind 	size_t clen;
    377  1.51     rmind 	int error = 0;
    378   1.1     rmind 
    379  1.51     rmind 	if ((rl = npf_rule_alloc(npf, rule)) == NULL) {
    380  1.21     rmind 		NPF_ERR_DEBUG(errdict);
    381  1.21     rmind 		return EINVAL;
    382  1.21     rmind 	}
    383   1.1     rmind 
    384  1.51     rmind 	/* Assign the rule procedure, if any. */
    385  1.51     rmind 	if ((rname = dnvlist_get_string(rule, "rproc", NULL)) != NULL) {
    386  1.21     rmind 		npf_rproc_t *rp;
    387  1.21     rmind 
    388  1.21     rmind 		if (rpset == NULL) {
    389  1.51     rmind 			NPF_ERR_DEBUG(errdict);
    390  1.21     rmind 			error = EINVAL;
    391  1.21     rmind 			goto err;
    392  1.21     rmind 		}
    393  1.21     rmind 		if ((rp = npf_rprocset_lookup(rpset, rname)) == NULL) {
    394  1.18     rmind 			NPF_ERR_DEBUG(errdict);
    395  1.18     rmind 			error = EINVAL;
    396  1.18     rmind 			goto err;
    397  1.18     rmind 		}
    398  1.21     rmind 		npf_rule_setrproc(rl, rp);
    399  1.18     rmind 	}
    400  1.18     rmind 
    401  1.51     rmind 	/* Filter byte-code (binary data). */
    402  1.51     rmind 	code = dnvlist_get_binary(rule, "code", &clen, NULL, 0);
    403  1.51     rmind 	if (code) {
    404  1.51     rmind 		void *bc;
    405  1.21     rmind 		int type;
    406  1.21     rmind 
    407  1.51     rmind 		type = dnvlist_get_number(rule, "code-type", UINT64_MAX);
    408  1.51     rmind 		if (type != NPF_CODE_BPF) {
    409  1.51     rmind 			NPF_ERR_DEBUG(errdict);
    410  1.51     rmind 			error = ENOTSUP;
    411  1.51     rmind 			goto err;
    412  1.51     rmind 		}
    413  1.51     rmind 		if (clen == 0) {
    414  1.51     rmind 			NPF_ERR_DEBUG(errdict);
    415  1.51     rmind 			error = EINVAL;
    416  1.51     rmind 			goto err;
    417  1.51     rmind 		}
    418  1.51     rmind 		if (!npf_bpf_validate(code, clen)) {
    419  1.51     rmind 			NPF_ERR_DEBUG(errdict);
    420  1.51     rmind 			error = EINVAL;
    421  1.12     rmind 			goto err;
    422   1.4     rmind 		}
    423  1.51     rmind 		bc = kmem_alloc(clen, KM_SLEEP);
    424  1.51     rmind 		memcpy(bc, code, clen); // XXX: use nvlist_take
    425  1.51     rmind 		npf_rule_setcode(rl, type, bc, clen);
    426   1.1     rmind 	}
    427   1.1     rmind 
    428  1.21     rmind 	*rlret = rl;
    429   1.6     rmind 	return 0;
    430  1.12     rmind err:
    431  1.51     rmind 	nvlist_add_number(errdict, "id", dnvlist_get_number(rule, "prio", 0));
    432  1.21     rmind 	npf_rule_free(rl);
    433  1.12     rmind 	return error;
    434   1.6     rmind }
    435   1.6     rmind 
    436   1.6     rmind static int __noinline
    437  1.51     rmind npf_mk_rules(npf_t *npf, nvlist_t *npf_dict, nvlist_t *errdict,
    438  1.57     rmind     npf_config_t *nc)
    439   1.6     rmind {
    440  1.51     rmind 	const nvlist_t * const *rules;
    441  1.51     rmind 	npf_ruleset_t *rlset;
    442  1.51     rmind 	size_t nitems;
    443  1.51     rmind 	int error = 0;
    444   1.6     rmind 
    445  1.51     rmind 	if (nvlist_exists_nvlist_array(npf_dict, "rules")) {
    446  1.51     rmind 		rules = nvlist_get_nvlist_array(npf_dict, "rules", &nitems);
    447  1.51     rmind 		if (nitems > NPF_MAX_RULES) {
    448  1.51     rmind 			NPF_ERR_DEBUG(errdict);
    449  1.51     rmind 			return E2BIG;
    450  1.51     rmind 		}
    451  1.51     rmind 	} else {
    452  1.51     rmind 		rules = NULL;
    453  1.51     rmind 		nitems = 0;
    454   1.6     rmind 	}
    455  1.51     rmind 	rlset = npf_ruleset_create(nitems);
    456  1.51     rmind 	for (unsigned i = 0; i < nitems; i++) {
    457  1.51     rmind 		const nvlist_t *rule = rules[i];
    458  1.21     rmind 		npf_rule_t *rl = NULL;
    459  1.50     rmind 		const char *name;
    460   1.1     rmind 
    461  1.57     rmind 		error = npf_mk_singlerule(npf, rule, nc->rule_procs, &rl,
    462  1.57     rmind 		    errdict);
    463   1.6     rmind 		if (error) {
    464   1.1     rmind 			break;
    465   1.6     rmind 		}
    466  1.51     rmind 		name = dnvlist_get_string(rule, "name", NULL);
    467  1.51     rmind 		if (name && npf_ruleset_lookup(rlset, name)) {
    468  1.50     rmind 			NPF_ERR_DEBUG(errdict);
    469  1.50     rmind 			npf_rule_free(rl);
    470  1.51     rmind 			error = EEXIST;
    471  1.51     rmind 			break;
    472  1.50     rmind 		}
    473   1.6     rmind 		npf_ruleset_insert(rlset, rl);
    474   1.1     rmind 	}
    475  1.57     rmind 	nc->ruleset = rlset;
    476   1.1     rmind 	return error;
    477   1.1     rmind }
    478   1.1     rmind 
    479   1.6     rmind static int __noinline
    480  1.54     rmind npf_mk_singlenat(npf_t *npf, const nvlist_t *nat, npf_ruleset_t *ntset,
    481  1.54     rmind     npf_tableset_t *tblset, nvlist_t *errdict, npf_rule_t **rlp)
    482  1.54     rmind {
    483  1.54     rmind 	npf_rule_t *rl = NULL;
    484  1.54     rmind 	npf_natpolicy_t *np;
    485  1.54     rmind 	int error;
    486  1.54     rmind 
    487  1.54     rmind 	/*
    488  1.54     rmind 	 * NAT rules are standard rules, plus the translation policy.
    489  1.54     rmind 	 * We first construct the rule structure.
    490  1.54     rmind 	 */
    491  1.54     rmind 	error = npf_mk_singlerule(npf, nat, NULL, &rl, errdict);
    492  1.54     rmind 	if (error) {
    493  1.54     rmind 		return error;
    494  1.54     rmind 	}
    495  1.54     rmind 	KASSERT(rl != NULL);
    496  1.54     rmind 	*rlp = rl;
    497  1.54     rmind 
    498  1.54     rmind 	/* If rule is named, it is a group with NAT policies. */
    499  1.54     rmind 	if (dnvlist_get_string(nat, "name", NULL)) {
    500  1.54     rmind 		return 0;
    501  1.54     rmind 	}
    502  1.54     rmind 
    503  1.54     rmind 	/* Check the table ID. */
    504  1.54     rmind 	if (nvlist_exists_number(nat, "nat-table-id")) {
    505  1.54     rmind 		unsigned tid = nvlist_get_number(nat, "nat-table-id");
    506  1.54     rmind 
    507  1.54     rmind 		if (!npf_tableset_getbyid(tblset, tid)) {
    508  1.54     rmind 			NPF_ERR_DEBUG(errdict);
    509  1.54     rmind 			error = EINVAL;
    510  1.54     rmind 			goto out;
    511  1.54     rmind 		}
    512  1.54     rmind 	}
    513  1.54     rmind 
    514  1.54     rmind 	/* Allocate a new NAT policy and assign it to the rule. */
    515  1.54     rmind 	np = npf_nat_newpolicy(npf, nat, ntset);
    516  1.54     rmind 	if (np == NULL) {
    517  1.54     rmind 		NPF_ERR_DEBUG(errdict);
    518  1.54     rmind 		error = ENOMEM;
    519  1.54     rmind 		goto out;
    520  1.54     rmind 	}
    521  1.54     rmind 	npf_rule_setnat(rl, np);
    522  1.54     rmind out:
    523  1.54     rmind 	if (error) {
    524  1.54     rmind 		npf_rule_free(rl);
    525  1.54     rmind 	}
    526  1.54     rmind 	return error;
    527  1.54     rmind }
    528  1.54     rmind 
    529  1.54     rmind static int __noinline
    530  1.57     rmind npf_mk_natlist(npf_t *npf, nvlist_t *npf_dict, nvlist_t *errdict,
    531  1.57     rmind     npf_config_t *nc)
    532   1.1     rmind {
    533  1.51     rmind 	const nvlist_t * const *nat_rules;
    534  1.51     rmind 	npf_ruleset_t *ntset;
    535  1.51     rmind 	size_t nitems;
    536  1.51     rmind 	int error = 0;
    537   1.1     rmind 
    538  1.51     rmind 	/*
    539  1.51     rmind 	 * NAT policies must be an array, but enforce a limit.
    540  1.51     rmind 	 */
    541  1.51     rmind 	if (nvlist_exists_nvlist_array(npf_dict, "nat")) {
    542  1.51     rmind 		nat_rules = nvlist_get_nvlist_array(npf_dict, "nat", &nitems);
    543  1.51     rmind 		if (nitems > NPF_MAX_RULES) {
    544  1.51     rmind 			NPF_ERR_DEBUG(errdict);
    545  1.51     rmind 			return E2BIG;
    546  1.51     rmind 		}
    547  1.51     rmind 	} else {
    548  1.51     rmind 		nat_rules = NULL;
    549  1.51     rmind 		nitems = 0;
    550  1.12     rmind 	}
    551  1.51     rmind 	ntset = npf_ruleset_create(nitems);
    552  1.51     rmind 	for (unsigned i = 0; i < nitems; i++) {
    553  1.51     rmind 		const nvlist_t *nat = nat_rules[i];
    554  1.21     rmind 		npf_rule_t *rl = NULL;
    555   1.1     rmind 
    556  1.57     rmind 		error = npf_mk_singlenat(npf, nat, ntset, nc->tableset,
    557  1.57     rmind 		    errdict, &rl);
    558   1.6     rmind 		if (error) {
    559   1.1     rmind 			break;
    560   1.6     rmind 		}
    561  1.51     rmind 		npf_ruleset_insert(ntset, rl);
    562   1.1     rmind 	}
    563  1.57     rmind 	nc->nat_ruleset = ntset;
    564   1.1     rmind 	return error;
    565   1.1     rmind }
    566   1.1     rmind 
    567   1.1     rmind /*
    568  1.35     rmind  * npf_mk_connlist: import a list of connections and load them.
    569  1.35     rmind  */
    570  1.35     rmind static int __noinline
    571  1.51     rmind npf_mk_connlist(npf_t *npf, nvlist_t *npf_dict, nvlist_t *errdict,
    572  1.57     rmind     npf_config_t *nc, npf_conndb_t **conndb)
    573  1.35     rmind {
    574  1.51     rmind 	const nvlist_t * const *conns;
    575  1.35     rmind 	npf_conndb_t *cd;
    576  1.51     rmind 	size_t nitems;
    577  1.40     rmind 	int error = 0;
    578  1.35     rmind 
    579  1.51     rmind 	if (!nvlist_exists_nvlist_array(npf_dict, "conn-list")) {
    580  1.51     rmind 		*conndb = NULL;
    581  1.51     rmind 		return 0;
    582  1.35     rmind 	}
    583  1.51     rmind 	cd = npf_conndb_create();
    584  1.51     rmind 	conns = nvlist_get_nvlist_array(npf_dict, "conn-list", &nitems);
    585  1.51     rmind 	for (unsigned i = 0; i < nitems; i++) {
    586  1.51     rmind 		const nvlist_t *conn = conns[i];
    587  1.35     rmind 
    588  1.40     rmind 		/* Construct and insert the connection. */
    589  1.57     rmind 		error = npf_conn_import(npf, cd, conn, nc->nat_ruleset);
    590  1.35     rmind 		if (error) {
    591  1.35     rmind 			NPF_ERR_DEBUG(errdict);
    592  1.35     rmind 			break;
    593  1.35     rmind 		}
    594  1.35     rmind 	}
    595  1.35     rmind 	if (error) {
    596  1.53     rmind 		npf_conndb_gc(npf, cd, true, false);
    597  1.35     rmind 		npf_conndb_destroy(cd);
    598  1.35     rmind 	} else {
    599  1.35     rmind 		*conndb = cd;
    600  1.35     rmind 	}
    601  1.35     rmind 	return error;
    602  1.35     rmind }
    603  1.35     rmind 
    604  1.35     rmind /*
    605  1.51     rmind  * npfctl_load_nvlist: store passed data i.e. the update settings, create
    606  1.51     rmind  * the passed tables, rules, etc and atomically activate all them.
    607   1.1     rmind  */
    608  1.51     rmind static int
    609  1.51     rmind npfctl_load_nvlist(npf_t *npf, nvlist_t *npf_dict, nvlist_t *errdict)
    610   1.1     rmind {
    611  1.57     rmind 	npf_config_t *nc;
    612  1.35     rmind 	npf_conndb_t *conndb = NULL;
    613  1.51     rmind 	uint64_t ver;
    614  1.11     rmind 	bool flush;
    615   1.1     rmind 	int error;
    616   1.1     rmind 
    617  1.57     rmind 	nc = npf_config_create();
    618  1.51     rmind 	ver = dnvlist_get_number(npf_dict, "version", UINT64_MAX);
    619  1.20     rmind 	if (ver != NPF_VERSION) {
    620  1.20     rmind 		error = EPROGMISMATCH;
    621  1.20     rmind 		goto fail;
    622  1.20     rmind 	}
    623  1.54     rmind 	error = npf_mk_params(npf, npf_dict, errdict, false /* validate */);
    624  1.54     rmind 	if (error) {
    625  1.54     rmind 		goto fail;
    626  1.54     rmind 	}
    627  1.51     rmind 	error = npf_mk_algs(npf, npf_dict, errdict);
    628  1.24  christos 	if (error) {
    629  1.24  christos 		goto fail;
    630  1.24  christos 	}
    631  1.57     rmind 	error = npf_mk_tables(npf, npf_dict, errdict, nc);
    632  1.51     rmind 	if (error) {
    633  1.30     rmind 		goto fail;
    634  1.30     rmind 	}
    635  1.57     rmind 	error = npf_mk_rprocs(npf, npf_dict, errdict, nc);
    636  1.10     rmind 	if (error) {
    637   1.1     rmind 		goto fail;
    638  1.10     rmind 	}
    639  1.57     rmind 	error = npf_mk_natlist(npf, npf_dict, errdict, nc);
    640  1.10     rmind 	if (error) {
    641   1.1     rmind 		goto fail;
    642  1.10     rmind 	}
    643  1.57     rmind 	error = npf_mk_rules(npf, npf_dict, errdict, nc);
    644  1.21     rmind 	if (error) {
    645  1.21     rmind 		goto fail;
    646  1.21     rmind 	}
    647  1.57     rmind 	error = npf_mk_connlist(npf, npf_dict, errdict, nc, &conndb);
    648  1.10     rmind 	if (error) {
    649   1.1     rmind 		goto fail;
    650  1.10     rmind 	}
    651   1.1     rmind 
    652  1.57     rmind 	flush = dnvlist_get_bool(npf_dict, "flush", false);
    653  1.57     rmind 	nc->default_pass = flush;
    654  1.57     rmind 
    655   1.1     rmind 	/*
    656  1.35     rmind 	 * Finally - perform the load.
    657   1.1     rmind 	 */
    658  1.57     rmind 	npf_config_load(npf, nc, conndb, flush);
    659  1.54     rmind 	npf_mk_params(npf, npf_dict, errdict, true /* set the params */);
    660  1.11     rmind 
    661   1.1     rmind 	/* Done.  Since data is consumed now, we shall not destroy it. */
    662  1.57     rmind 	nc = NULL;
    663   1.1     rmind fail:
    664  1.57     rmind 	if (nc) {
    665  1.57     rmind 		npf_config_destroy(nc);
    666   1.1     rmind 	}
    667  1.51     rmind 	nvlist_destroy(npf_dict);
    668  1.51     rmind 	return error;
    669  1.51     rmind }
    670  1.51     rmind 
    671  1.51     rmind int
    672  1.51     rmind npfctl_load(npf_t *npf, u_long cmd, void *data)
    673  1.51     rmind {
    674  1.51     rmind 	nvlist_t *request, *response;
    675  1.51     rmind 	int error;
    676  1.12     rmind 
    677  1.49     ozaki 	/*
    678  1.51     rmind 	 * Retrieve the configuration and check the version.
    679  1.51     rmind 	 * Construct a response with error reporting.
    680  1.49     ozaki 	 */
    681  1.52  christos 	error = npf_nvlist_copyin(npf, data, &request);
    682  1.51     rmind 	if (error) {
    683  1.51     rmind 		return error;
    684  1.49     ozaki 	}
    685  1.51     rmind 	response = nvlist_create(0);
    686  1.51     rmind 	error = npfctl_load_nvlist(npf, request, response);
    687  1.51     rmind 	nvlist_add_number(response, "errno", error);
    688  1.52  christos 	return npf_nvlist_copyout(npf, data, response);
    689   1.6     rmind }
    690   1.6     rmind 
    691  1.21     rmind /*
    692  1.35     rmind  * npfctl_save: export the config dictionary as it was submitted,
    693  1.35     rmind  * including the current snapshot of the connections.  Additionally,
    694  1.35     rmind  * indicate whether the ruleset is currently active.
    695  1.35     rmind  */
    696  1.35     rmind int
    697  1.45  christos npfctl_save(npf_t *npf, u_long cmd, void *data)
    698  1.35     rmind {
    699  1.57     rmind 	npf_config_t *nc;
    700  1.51     rmind 	nvlist_t *npf_dict;
    701  1.35     rmind 	int error;
    702  1.35     rmind 
    703  1.51     rmind 	npf_dict = nvlist_create(0);
    704  1.51     rmind 	nvlist_add_number(npf_dict, "version", NPF_VERSION);
    705  1.35     rmind 
    706  1.37     rmind 	/*
    707  1.51     rmind 	 * Serialise the whole NPF config, including connections.
    708  1.37     rmind 	 */
    709  1.57     rmind 	nc = npf_config_enter(npf);
    710  1.51     rmind 	error = npf_conndb_export(npf, npf_dict);
    711  1.37     rmind 	if (error) {
    712  1.37     rmind 		goto out;
    713  1.37     rmind 	}
    714  1.57     rmind 	error = npf_ruleset_export(npf, nc->ruleset, "rules", npf_dict);
    715  1.38     rmind 	if (error) {
    716  1.38     rmind 		goto out;
    717  1.38     rmind 	}
    718  1.57     rmind 	error = npf_ruleset_export(npf, nc->nat_ruleset, "nat", npf_dict);
    719  1.35     rmind 	if (error) {
    720  1.35     rmind 		goto out;
    721  1.35     rmind 	}
    722  1.57     rmind 	error = npf_tableset_export(npf, nc->tableset, npf_dict);
    723  1.38     rmind 	if (error) {
    724  1.38     rmind 		goto out;
    725  1.38     rmind 	}
    726  1.57     rmind 	error = npf_rprocset_export(nc->rule_procs, npf_dict);
    727  1.38     rmind 	if (error) {
    728  1.38     rmind 		goto out;
    729  1.38     rmind 	}
    730  1.51     rmind 	error = npf_alg_export(npf, npf_dict);
    731  1.51     rmind 	if (error) {
    732  1.51     rmind 		goto out;
    733  1.51     rmind 	}
    734  1.51     rmind 	nvlist_add_bool(npf_dict, "active", npf_pfil_registered_p());
    735  1.52  christos 	error = npf_nvlist_copyout(npf, data, npf_dict);
    736  1.51     rmind 	npf_dict = NULL;
    737  1.35     rmind out:
    738  1.45  christos 	npf_config_exit(npf);
    739  1.51     rmind 	if (npf_dict) {
    740  1.51     rmind 		nvlist_destroy(npf_dict);
    741  1.37     rmind 	}
    742  1.35     rmind 	return error;
    743  1.35     rmind }
    744  1.35     rmind 
    745  1.35     rmind /*
    746  1.56     rmind  * npfctl_table_replace_nvlist: atomically replace a table's contents
    747  1.56     rmind  * with the passed table data.
    748  1.56     rmind  */
    749  1.56     rmind static int __noinline
    750  1.56     rmind npfctl_table_replace_nvlist(npf_t *npf, nvlist_t *npf_dict, nvlist_t *errdict)
    751  1.56     rmind {
    752  1.56     rmind 	npf_table_t *tbl, *gc_tbl = NULL;
    753  1.57     rmind 	npf_config_t *nc;
    754  1.56     rmind 	int error = 0;
    755  1.56     rmind 
    756  1.57     rmind 	nc = npf_config_enter(npf);
    757  1.57     rmind 	error = npf_mk_table(npf, npf_dict, errdict, nc->tableset, &tbl, true);
    758  1.56     rmind 	if (error) {
    759  1.56     rmind 		goto err;
    760  1.56     rmind 	}
    761  1.57     rmind 	gc_tbl = npf_tableset_swap(nc->tableset, tbl);
    762  1.56     rmind 	if (gc_tbl == NULL) {
    763  1.56     rmind 		error = EINVAL;
    764  1.56     rmind 		gc_tbl = tbl;
    765  1.56     rmind 		goto err;
    766  1.56     rmind 	}
    767  1.56     rmind 	npf_config_sync(npf);
    768  1.56     rmind err:
    769  1.56     rmind 	npf_config_exit(npf);
    770  1.56     rmind 	if (gc_tbl) {
    771  1.56     rmind 		npf_table_destroy(gc_tbl);
    772  1.56     rmind 	}
    773  1.56     rmind 	return error;
    774  1.56     rmind }
    775  1.56     rmind 
    776  1.56     rmind int
    777  1.56     rmind npfctl_table_replace(npf_t *npf, u_long cmd, void *data)
    778  1.56     rmind {
    779  1.56     rmind 	nvlist_t *request, *response;
    780  1.56     rmind 	int error;
    781  1.56     rmind 
    782  1.56     rmind 	/*
    783  1.56     rmind 	 * Retrieve the configuration and check the version.
    784  1.56     rmind 	 * Construct a response with error reporting.
    785  1.56     rmind 	 */
    786  1.56     rmind 	error = npf_nvlist_copyin(npf, data, &request);
    787  1.56     rmind 	if (error) {
    788  1.56     rmind 		return error;
    789  1.56     rmind 	}
    790  1.56     rmind 	response = nvlist_create(0);
    791  1.56     rmind 	error = npfctl_table_replace_nvlist(npf, request, response);
    792  1.56     rmind 	nvlist_add_number(response, "errno", error);
    793  1.56     rmind 	error = npf_nvlist_copyout(npf, data, response);
    794  1.56     rmind 	nvlist_destroy(request);
    795  1.56     rmind 	return error;
    796  1.56     rmind }
    797  1.56     rmind 
    798  1.56     rmind /*
    799  1.44  christos  * npfctl_conn_lookup: lookup a connection in the list of connections
    800  1.44  christos  */
    801  1.44  christos int
    802  1.45  christos npfctl_conn_lookup(npf_t *npf, u_long cmd, void *data)
    803  1.44  christos {
    804  1.51     rmind 	nvlist_t *conn_data, *conn_result;
    805  1.44  christos 	int error;
    806  1.44  christos 
    807  1.52  christos 	error = npf_nvlist_copyin(npf, data, &conn_data);
    808  1.44  christos 	if (error) {
    809  1.44  christos 		return error;
    810  1.44  christos 	}
    811  1.45  christos 	error = npf_conn_find(npf, conn_data, &conn_result);
    812  1.44  christos 	if (error) {
    813  1.44  christos 		goto out;
    814  1.44  christos 	}
    815  1.52  christos 	error = npf_nvlist_copyout(npf, data, conn_result);
    816  1.44  christos out:
    817  1.51     rmind 	nvlist_destroy(conn_data);
    818  1.44  christos 	return error;
    819  1.44  christos }
    820  1.44  christos 
    821  1.44  christos /*
    822  1.21     rmind  * npfctl_rule: add or remove dynamic rules in the specified ruleset.
    823  1.21     rmind  */
    824  1.14     rmind int
    825  1.45  christos npfctl_rule(npf_t *npf, u_long cmd, void *data)
    826  1.14     rmind {
    827  1.51     rmind 	nvlist_t *npf_rule, *retdict = NULL;
    828  1.21     rmind 	npf_ruleset_t *rlset;
    829  1.21     rmind 	npf_rule_t *rl = NULL;
    830  1.21     rmind 	const char *ruleset_name;
    831  1.57     rmind 	npf_config_t *nc;
    832  1.51     rmind 	uint32_t rcmd;
    833  1.45  christos 	int error = 0;
    834  1.54     rmind 	bool natset;
    835  1.14     rmind 
    836  1.52  christos 	error = npf_nvlist_copyin(npf, data, &npf_rule);
    837  1.21     rmind 	if (error) {
    838  1.21     rmind 		return error;
    839  1.21     rmind 	}
    840  1.51     rmind 	rcmd = dnvlist_get_number(npf_rule, "command", 0);
    841  1.54     rmind 	natset = dnvlist_get_bool(npf_rule, "nat-rule", false);
    842  1.51     rmind 	ruleset_name = dnvlist_get_string(npf_rule, "ruleset-name", NULL);
    843  1.51     rmind 	if (!ruleset_name) {
    844  1.27     rmind 		error = EINVAL;
    845  1.27     rmind 		goto out;
    846  1.21     rmind 	}
    847  1.21     rmind 
    848  1.57     rmind 	nc = npf_config_enter(npf);
    849  1.57     rmind 	rlset = natset ? nc->nat_ruleset : nc->ruleset;
    850  1.54     rmind 	switch (rcmd) {
    851  1.54     rmind 	case NPF_CMD_RULE_ADD: {
    852  1.51     rmind 		retdict = nvlist_create(0);
    853  1.54     rmind 		if (natset) {
    854  1.54     rmind 			/*
    855  1.54     rmind 			 * Translation rule.
    856  1.54     rmind 			 */
    857  1.54     rmind 			error = npf_mk_singlenat(npf, npf_rule, rlset,
    858  1.57     rmind 			    nc->tableset, retdict, &rl);
    859  1.54     rmind 		} else {
    860  1.54     rmind 			/*
    861  1.54     rmind 			 * Standard rule.
    862  1.54     rmind 			 */
    863  1.54     rmind 			error = npf_mk_singlerule(npf, npf_rule, NULL,
    864  1.54     rmind 			    &rl, retdict);
    865  1.54     rmind 		}
    866  1.51     rmind 		if (error) {
    867  1.54     rmind 			npf_config_exit(npf);
    868  1.27     rmind 			goto out;
    869  1.21     rmind 		}
    870  1.21     rmind 		if ((error = npf_ruleset_add(rlset, ruleset_name, rl)) == 0) {
    871  1.21     rmind 			/* Success. */
    872  1.23     rmind 			uint64_t id = npf_rule_getid(rl);
    873  1.51     rmind 			nvlist_add_number(retdict, "id", id);
    874  1.21     rmind 			rl = NULL;
    875  1.21     rmind 		}
    876  1.21     rmind 		break;
    877  1.21     rmind 	}
    878  1.21     rmind 	case NPF_CMD_RULE_REMOVE: {
    879  1.51     rmind 		uint64_t id = dnvlist_get_number(npf_rule, "id", UINT64_MAX);
    880  1.23     rmind 		error = npf_ruleset_remove(rlset, ruleset_name, id);
    881  1.21     rmind 		break;
    882  1.21     rmind 	}
    883  1.21     rmind 	case NPF_CMD_RULE_REMKEY: {
    884  1.51     rmind 		const void *key;
    885  1.51     rmind 		size_t len;
    886  1.21     rmind 
    887  1.51     rmind 		key = dnvlist_get_binary(npf_rule, "key", &len, NULL, 0);
    888  1.21     rmind 		if (len == 0 || len > NPF_RULE_MAXKEYLEN) {
    889  1.21     rmind 			error = EINVAL;
    890  1.21     rmind 			break;
    891  1.21     rmind 		}
    892  1.22     rmind 		error = npf_ruleset_remkey(rlset, ruleset_name, key, len);
    893  1.22     rmind 		break;
    894  1.22     rmind 	}
    895  1.22     rmind 	case NPF_CMD_RULE_LIST: {
    896  1.45  christos 		retdict = npf_ruleset_list(npf, rlset, ruleset_name);
    897  1.41     rmind 		if (!retdict) {
    898  1.41     rmind 			error = ESRCH;
    899  1.41     rmind 		}
    900  1.22     rmind 		break;
    901  1.22     rmind 	}
    902  1.22     rmind 	case NPF_CMD_RULE_FLUSH: {
    903  1.22     rmind 		error = npf_ruleset_flush(rlset, ruleset_name);
    904  1.21     rmind 		break;
    905  1.21     rmind 	}
    906  1.21     rmind 	default:
    907  1.21     rmind 		error = EINVAL;
    908  1.21     rmind 		break;
    909  1.21     rmind 	}
    910  1.22     rmind 
    911  1.22     rmind 	/* Destroy any removed rules. */
    912  1.22     rmind 	if (!error && rcmd != NPF_CMD_RULE_ADD && rcmd != NPF_CMD_RULE_LIST) {
    913  1.45  christos 		npf_config_sync(npf);
    914  1.22     rmind 		npf_ruleset_gc(rlset);
    915  1.22     rmind 	}
    916  1.45  christos 	npf_config_exit(npf);
    917  1.14     rmind 
    918  1.21     rmind 	if (rl) {
    919  1.41     rmind 		KASSERT(error);
    920  1.21     rmind 		npf_rule_free(rl);
    921  1.21     rmind 	}
    922  1.27     rmind out:
    923  1.52  christos 	if (retdict && npf_nvlist_copyout(npf, data, retdict) != 0) {
    924  1.51     rmind 		error = EFAULT; // copyout failure
    925  1.21     rmind 	}
    926  1.51     rmind 	nvlist_destroy(npf_rule);
    927  1.14     rmind 	return error;
    928  1.14     rmind }
    929  1.14     rmind 
    930   1.6     rmind /*
    931   1.2     rmind  * npfctl_table: add, remove or query entries in the specified table.
    932   1.1     rmind  *
    933  1.51     rmind  * For maximum performance, the interface is using plain structures.
    934   1.1     rmind  */
    935   1.1     rmind int
    936  1.45  christos npfctl_table(npf_t *npf, void *data)
    937   1.1     rmind {
    938  1.19     rmind 	const npf_ioctl_table_t *nct = data;
    939  1.32     rmind 	char tname[NPF_TABLE_MAXNAMELEN];
    940  1.57     rmind 	npf_config_t *nc;
    941  1.32     rmind 	npf_table_t *t;
    942  1.53     rmind 	int error;
    943  1.32     rmind 
    944  1.32     rmind 	error = copyinstr(nct->nct_name, tname, sizeof(tname), NULL);
    945  1.32     rmind 	if (error) {
    946  1.32     rmind 		return error;
    947  1.32     rmind 	}
    948   1.1     rmind 
    949  1.57     rmind 	nc = npf_config_enter(npf);
    950  1.57     rmind 	if ((t = npf_tableset_getbyname(nc->tableset, tname)) == NULL) {
    951  1.53     rmind 		npf_config_exit(npf);
    952  1.32     rmind 		return EINVAL;
    953  1.32     rmind 	}
    954  1.21     rmind 
    955  1.21     rmind 	switch (nct->nct_cmd) {
    956  1.21     rmind 	case NPF_CMD_TABLE_LOOKUP:
    957  1.32     rmind 		error = npf_table_lookup(t, nct->nct_data.ent.alen,
    958  1.32     rmind 		    &nct->nct_data.ent.addr);
    959  1.20     rmind 		break;
    960  1.21     rmind 	case NPF_CMD_TABLE_ADD:
    961  1.32     rmind 		error = npf_table_insert(t, nct->nct_data.ent.alen,
    962  1.32     rmind 		    &nct->nct_data.ent.addr, nct->nct_data.ent.mask);
    963   1.1     rmind 		break;
    964  1.21     rmind 	case NPF_CMD_TABLE_REMOVE:
    965  1.32     rmind 		error = npf_table_remove(t, nct->nct_data.ent.alen,
    966  1.32     rmind 		    &nct->nct_data.ent.addr, nct->nct_data.ent.mask);
    967  1.19     rmind 		break;
    968  1.21     rmind 	case NPF_CMD_TABLE_LIST:
    969  1.32     rmind 		error = npf_table_list(t, nct->nct_data.buf.buf,
    970  1.32     rmind 		    nct->nct_data.buf.len);
    971   1.1     rmind 		break;
    972  1.25     rmind 	case NPF_CMD_TABLE_FLUSH:
    973  1.32     rmind 		error = npf_table_flush(t);
    974  1.25     rmind 		break;
    975   1.1     rmind 	default:
    976  1.19     rmind 		error = EINVAL;
    977  1.19     rmind 		break;
    978   1.1     rmind 	}
    979  1.53     rmind 	npf_table_gc(npf, t);
    980  1.53     rmind 	npf_config_exit(npf);
    981  1.21     rmind 
    982   1.1     rmind 	return error;
    983   1.1     rmind }
    984