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