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