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