Home | History | Annotate | Line # | Download | only in libnpf
npf.c revision 1.16
      1 /*	$NetBSD: npf.c,v 1.16 2013/02/09 03:35:33 rmind Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2010-2013 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 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: npf.c,v 1.16 2013/02/09 03:35:33 rmind Exp $");
     34 
     35 #include <sys/types.h>
     36 #include <netinet/in_systm.h>
     37 #include <netinet/in.h>
     38 #include <net/if.h>
     39 #include <prop/proplib.h>
     40 
     41 #include <stdlib.h>
     42 #include <string.h>
     43 #include <assert.h>
     44 #include <errno.h>
     45 #include <err.h>
     46 
     47 #define	_NPF_PRIVATE
     48 #include "npf.h"
     49 
     50 struct nl_config {
     51 	/* Rules, translations, tables, procedures. */
     52 	prop_dictionary_t	ncf_dict;
     53 	prop_array_t		ncf_rules_list;
     54 	prop_array_t		ncf_rproc_list;
     55 	prop_array_t		ncf_table_list;
     56 	prop_array_t		ncf_nat_list;
     57 	/* Debug information. */
     58 	prop_dictionary_t	ncf_debug;
     59 	/* Error report. */
     60 	prop_dictionary_t	ncf_err;
     61 	/* Custom file to externalise property-list. */
     62 	const char *		ncf_plist;
     63 	bool			ncf_flush;
     64 };
     65 
     66 struct nl_rule {
     67 	prop_dictionary_t	nrl_dict;
     68 };
     69 
     70 struct nl_rproc {
     71 	prop_dictionary_t	nrp_dict;
     72 };
     73 
     74 struct nl_table {
     75 	prop_dictionary_t	ntl_dict;
     76 };
     77 
     78 struct nl_ext {
     79 	const char *		nxt_name;
     80 	prop_dictionary_t	nxt_dict;
     81 };
     82 
     83 static prop_array_t	_npf_ruleset_transform(prop_array_t);
     84 
     85 /*
     86  * CONFIGURATION INTERFACE.
     87  */
     88 
     89 nl_config_t *
     90 npf_config_create(void)
     91 {
     92 	nl_config_t *ncf;
     93 
     94 	ncf = calloc(1, sizeof(*ncf));
     95 	if (ncf == NULL) {
     96 		return NULL;
     97 	}
     98 	ncf->ncf_rules_list = prop_array_create();
     99 	ncf->ncf_rproc_list = prop_array_create();
    100 	ncf->ncf_table_list = prop_array_create();
    101 	ncf->ncf_nat_list = prop_array_create();
    102 
    103 	ncf->ncf_plist = NULL;
    104 	ncf->ncf_flush = false;
    105 
    106 	return ncf;
    107 }
    108 
    109 int
    110 npf_config_submit(nl_config_t *ncf, int fd)
    111 {
    112 	const char *plist = ncf->ncf_plist;
    113 	prop_dictionary_t npf_dict;
    114 	prop_array_t rlset;
    115 	int error = 0;
    116 
    117 	npf_dict = prop_dictionary_create();
    118 	if (npf_dict == NULL) {
    119 		return ENOMEM;
    120 	}
    121 	prop_dictionary_set_uint32(npf_dict, "version", NPF_VERSION);
    122 
    123 	rlset = _npf_ruleset_transform(ncf->ncf_rules_list);
    124 	if (rlset == NULL) {
    125 		prop_object_release(npf_dict);
    126 		return ENOMEM;
    127 	}
    128 	prop_dictionary_set(npf_dict, "rules", rlset);
    129 	prop_object_release(rlset);
    130 
    131 	prop_dictionary_set(npf_dict, "rprocs", ncf->ncf_rproc_list);
    132 	prop_dictionary_set(npf_dict, "tables", ncf->ncf_table_list);
    133 	prop_dictionary_set(npf_dict, "translation", ncf->ncf_nat_list);
    134 	prop_dictionary_set_bool(npf_dict, "flush", ncf->ncf_flush);
    135 	if (ncf->ncf_debug) {
    136 		prop_dictionary_set(npf_dict, "debug", ncf->ncf_debug);
    137 	}
    138 
    139 	if (plist) {
    140 		if (!prop_dictionary_externalize_to_file(npf_dict, plist)) {
    141 			error = errno;
    142 		}
    143 		prop_object_release(npf_dict);
    144 		return error;
    145 	}
    146 
    147 	error = prop_dictionary_sendrecv_ioctl(npf_dict, fd,
    148 	    IOC_NPF_RELOAD, &ncf->ncf_err);
    149 	if (error) {
    150 		prop_object_release(npf_dict);
    151 		assert(ncf->ncf_err == NULL);
    152 		return error;
    153 	}
    154 
    155 	prop_dictionary_get_int32(ncf->ncf_err, "errno", &error);
    156 	prop_object_release(npf_dict);
    157 	return error;
    158 }
    159 
    160 nl_config_t *
    161 npf_config_retrieve(int fd, bool *active, bool *loaded)
    162 {
    163 	prop_dictionary_t npf_dict;
    164 	nl_config_t *ncf;
    165 	int error;
    166 
    167 	error = prop_dictionary_recv_ioctl(fd, IOC_NPF_GETCONF, &npf_dict);
    168 	if (error) {
    169 		return NULL;
    170 	}
    171 	ncf = calloc(1, sizeof(*ncf));
    172 	if (ncf == NULL) {
    173 		prop_object_release(npf_dict);
    174 		return NULL;
    175 	}
    176 	ncf->ncf_dict = npf_dict;
    177 	ncf->ncf_rules_list = prop_dictionary_get(npf_dict, "rules");
    178 	ncf->ncf_rproc_list = prop_dictionary_get(npf_dict, "rprocs");
    179 	ncf->ncf_table_list = prop_dictionary_get(npf_dict, "tables");
    180 	ncf->ncf_nat_list = prop_dictionary_get(npf_dict, "translation");
    181 
    182 	prop_dictionary_get_bool(npf_dict, "active", active);
    183 	*loaded = (ncf->ncf_rules_list != NULL);
    184 	return ncf;
    185 }
    186 
    187 int
    188 npf_config_flush(int fd)
    189 {
    190 	nl_config_t *ncf;
    191 	int error;
    192 
    193 	ncf = npf_config_create();
    194 	if (ncf == NULL) {
    195 		return ENOMEM;
    196 	}
    197 	ncf->ncf_flush = true;
    198 	error = npf_config_submit(ncf, fd);
    199 	npf_config_destroy(ncf);
    200 	return error;
    201 }
    202 
    203 void
    204 _npf_config_error(nl_config_t *ncf, nl_error_t *ne)
    205 {
    206 	memset(ne, 0, sizeof(*ne));
    207 	prop_dictionary_get_int32(ncf->ncf_err, "id", &ne->ne_id);
    208 	prop_dictionary_get_cstring(ncf->ncf_err,
    209 	    "source-file", &ne->ne_source_file);
    210 	prop_dictionary_get_uint32(ncf->ncf_err,
    211 	    "source-line", &ne->ne_source_line);
    212 	prop_dictionary_get_int32(ncf->ncf_err,
    213 	    "code-error", &ne->ne_ncode_error);
    214 	prop_dictionary_get_int32(ncf->ncf_err,
    215 	    "code-errat", &ne->ne_ncode_errat);
    216 }
    217 
    218 void
    219 npf_config_destroy(nl_config_t *ncf)
    220 {
    221 
    222 	if (!ncf->ncf_dict) {
    223 		prop_object_release(ncf->ncf_rules_list);
    224 		prop_object_release(ncf->ncf_rproc_list);
    225 		prop_object_release(ncf->ncf_table_list);
    226 		prop_object_release(ncf->ncf_nat_list);
    227 	}
    228 	if (ncf->ncf_err) {
    229 		prop_object_release(ncf->ncf_err);
    230 	}
    231 	if (ncf->ncf_debug) {
    232 		prop_object_release(ncf->ncf_debug);
    233 	}
    234 	free(ncf);
    235 }
    236 
    237 void
    238 _npf_config_setsubmit(nl_config_t *ncf, const char *plist_file)
    239 {
    240 
    241 	ncf->ncf_plist = plist_file;
    242 }
    243 
    244 static bool
    245 _npf_prop_array_lookup(prop_array_t array, const char *key, const char *name)
    246 {
    247 	prop_dictionary_t dict;
    248 	prop_object_iterator_t it;
    249 
    250 	it = prop_array_iterator(array);
    251 	while ((dict = prop_object_iterator_next(it)) != NULL) {
    252 		const char *lname;
    253 		prop_dictionary_get_cstring_nocopy(dict, key, &lname);
    254 		if (strcmp(name, lname) == 0)
    255 			break;
    256 	}
    257 	prop_object_iterator_release(it);
    258 	return dict ? true : false;
    259 }
    260 
    261 /*
    262  * DYNAMIC RULESET INTERFACE.
    263  */
    264 
    265 int
    266 npf_ruleset_add(int fd, const char *rname, nl_rule_t *rl, uintptr_t *id)
    267 {
    268 	prop_dictionary_t rldict = rl->nrl_dict;
    269 	prop_dictionary_t ret;
    270 	uint64_t id64;
    271 	int error;
    272 
    273 	prop_dictionary_set_cstring(rldict, "ruleset-name", rname);
    274 	prop_dictionary_set_uint32(rldict, "command", NPF_CMD_RULE_ADD);
    275 	error = prop_dictionary_sendrecv_ioctl(rldict, fd, IOC_NPF_RULE, &ret);
    276 	if (!error) {
    277 		prop_dictionary_get_uint64(ret, "id", &id64);
    278 		*id = (uintptr_t)id64;
    279 	}
    280 	return error;
    281 }
    282 
    283 int
    284 npf_ruleset_remove(int fd, const char *rname, uintptr_t id)
    285 {
    286 	prop_dictionary_t rldict;
    287 
    288 	rldict = prop_dictionary_create();
    289 	if (rldict == NULL) {
    290 		return ENOMEM;
    291 	}
    292 	prop_dictionary_set_cstring(rldict, "ruleset-name", rname);
    293 	prop_dictionary_set_uint32(rldict, "command", NPF_CMD_RULE_REMOVE);
    294 	__CTASSERT(sizeof(uintptr_t) <= sizeof(uint64_t));
    295 	prop_dictionary_set_uint64(rldict, "id", (uint64_t)id);
    296 	return prop_dictionary_send_ioctl(rldict, fd, IOC_NPF_RULE);
    297 }
    298 
    299 int
    300 npf_ruleset_remkey(int fd, const char *rname, const void *key, size_t len)
    301 {
    302 	prop_dictionary_t rldict;
    303 	prop_data_t keyobj;
    304 
    305 	rldict = prop_dictionary_create();
    306 	if (rldict == NULL) {
    307 		return ENOMEM;
    308 	}
    309 	prop_dictionary_set_cstring(rldict, "ruleset-name", rname);
    310 	prop_dictionary_set_uint32(rldict, "command", NPF_CMD_RULE_REMKEY);
    311 
    312 	keyobj = prop_data_create_data(key, len);
    313 	if (keyobj == NULL) {
    314 		prop_object_release(rldict);
    315 		return ENOMEM;
    316 	}
    317 	prop_dictionary_set(rldict, "key", keyobj);
    318 	prop_object_release(keyobj);
    319 
    320 	return prop_dictionary_send_ioctl(rldict, fd, IOC_NPF_RULE);
    321 }
    322 
    323 /*
    324  * _npf_ruleset_transform: transform the ruleset representing nested
    325  * rules with lists into an array.
    326  */
    327 
    328 static void
    329 _npf_ruleset_transform1(prop_array_t rlset, prop_array_t rules)
    330 {
    331 	prop_object_iterator_t it;
    332 	prop_dictionary_t rldict;
    333 	prop_array_t subrlset;
    334 
    335 	it = prop_array_iterator(rules);
    336 	while ((rldict = prop_object_iterator_next(it)) != NULL) {
    337 		unsigned idx;
    338 
    339 		/* Add rules to the array (reference is retained). */
    340 		prop_array_add(rlset, rldict);
    341 
    342 		subrlset = prop_dictionary_get(rldict, "subrules");
    343 		if (subrlset) {
    344 			/* Process subrules recursively. */
    345 			_npf_ruleset_transform1(rlset, subrlset);
    346 			/* Add the skip-to position. */
    347 			idx = prop_array_count(rlset);
    348 			prop_dictionary_set_uint32(rldict, "skip-to", idx);
    349 			prop_dictionary_remove(rldict, "subrules");
    350 		}
    351 	}
    352 	prop_object_iterator_release(it);
    353 }
    354 
    355 static prop_array_t
    356 _npf_ruleset_transform(prop_array_t rlset)
    357 {
    358 	prop_array_t nrlset;
    359 
    360 	nrlset = prop_array_create();
    361 	_npf_ruleset_transform1(nrlset, rlset);
    362 	return nrlset;
    363 }
    364 
    365 /*
    366  * NPF EXTENSION INTERFACE.
    367  */
    368 
    369 nl_ext_t *
    370 npf_ext_construct(const char *name)
    371 {
    372 	nl_ext_t *ext;
    373 
    374 	ext = malloc(sizeof(*ext));
    375 	if (ext == NULL) {
    376 		return NULL;
    377 	}
    378 	ext->nxt_name = strdup(name);
    379 	if (ext->nxt_name == NULL) {
    380 		free(ext);
    381 		return NULL;
    382 	}
    383 	ext->nxt_dict = prop_dictionary_create();
    384 
    385 	return ext;
    386 }
    387 
    388 void
    389 npf_ext_param_u32(nl_ext_t *ext, const char *key, uint32_t val)
    390 {
    391 	prop_dictionary_t extdict = ext->nxt_dict;
    392 	prop_dictionary_set_uint32(extdict, key, val);
    393 }
    394 
    395 void
    396 npf_ext_param_bool(nl_ext_t *ext, const char *key, bool val)
    397 {
    398 	prop_dictionary_t extdict = ext->nxt_dict;
    399 	prop_dictionary_set_bool(extdict, key, val);
    400 }
    401 
    402 /*
    403  * RULE INTERFACE.
    404  */
    405 
    406 nl_rule_t *
    407 npf_rule_create(const char *name, uint32_t attr, u_int if_idx)
    408 {
    409 	prop_dictionary_t rldict;
    410 	nl_rule_t *rl;
    411 
    412 	rl = malloc(sizeof(*rl));
    413 	if (rl == NULL) {
    414 		return NULL;
    415 	}
    416 	rldict = prop_dictionary_create();
    417 	if (rldict == NULL) {
    418 		free(rl);
    419 		return NULL;
    420 	}
    421 	if (name) {
    422 		prop_dictionary_set_cstring(rldict, "name", name);
    423 	}
    424 	prop_dictionary_set_uint32(rldict, "attributes", attr);
    425 
    426 	if (if_idx) {
    427 		prop_dictionary_set_uint32(rldict, "interface", if_idx);
    428 	}
    429 	rl->nrl_dict = rldict;
    430 	return rl;
    431 }
    432 
    433 int
    434 npf_rule_setcode(nl_rule_t *rl, int type, const void *code, size_t len)
    435 {
    436 	prop_dictionary_t rldict = rl->nrl_dict;
    437 	prop_data_t cdata;
    438 
    439 	switch (type) {
    440 	case NPF_CODE_NC:
    441 	case NPF_CODE_BPF:
    442 		break;
    443 	default:
    444 		return ENOTSUP;
    445 	}
    446 	prop_dictionary_set_uint32(rldict, "code-type", type);
    447 	if ((cdata = prop_data_create_data(code, len)) == NULL) {
    448 		return ENOMEM;
    449 	}
    450 	prop_dictionary_set(rldict, "code", cdata);
    451 	prop_object_release(cdata);
    452 	return 0;
    453 }
    454 
    455 int
    456 npf_rule_setkey(nl_rule_t *rl, const void *key, size_t len)
    457 {
    458 	prop_dictionary_t rldict = rl->nrl_dict;
    459 	prop_data_t kdata;
    460 
    461 	if ((kdata = prop_data_create_data(key, len)) == NULL) {
    462 		return ENOMEM;
    463 	}
    464 	prop_dictionary_set(rldict, "key", kdata);
    465 	prop_object_release(kdata);
    466 	return 0;
    467 }
    468 
    469 int
    470 npf_rule_setprio(nl_rule_t *rl, pri_t pri)
    471 {
    472 	prop_dictionary_t rldict = rl->nrl_dict;
    473 
    474 	prop_dictionary_set_int32(rldict, "priority", pri);
    475 	return 0;
    476 }
    477 
    478 int
    479 npf_rule_setproc(nl_rule_t *rl, const char *name)
    480 {
    481 	prop_dictionary_t rldict = rl->nrl_dict;
    482 
    483 	prop_dictionary_set_cstring(rldict, "rproc", name);
    484 	return 0;
    485 }
    486 
    487 void *
    488 npf_rule_export(nl_rule_t *rl, size_t *length)
    489 {
    490 	prop_dictionary_t rldict = rl->nrl_dict;
    491 	void *xml;
    492 
    493 	if ((xml = prop_dictionary_externalize(rldict)) == NULL) {
    494 		return NULL;
    495 	}
    496 	*length = strlen(xml);
    497 	return xml;
    498 }
    499 
    500 bool
    501 npf_rule_exists_p(nl_config_t *ncf, const char *name)
    502 {
    503 	return _npf_prop_array_lookup(ncf->ncf_rules_list, "name", name);
    504 }
    505 
    506 int
    507 npf_rule_insert(nl_config_t *ncf, nl_rule_t *parent, nl_rule_t *rl)
    508 {
    509 	prop_dictionary_t rldict = rl->nrl_dict;
    510 	prop_array_t rlset;
    511 
    512 	if (parent) {
    513 		prop_dictionary_t pdict = parent->nrl_dict;
    514 		rlset = prop_dictionary_get(pdict, "subrules");
    515 		if (rlset == NULL) {
    516 			rlset = prop_array_create();
    517 			prop_dictionary_set(pdict, "subrules", rlset);
    518 			prop_object_release(rlset);
    519 		}
    520 	} else {
    521 		rlset = ncf->ncf_rules_list;
    522 	}
    523 	prop_array_add(rlset, rldict);
    524 	return 0;
    525 }
    526 
    527 static int
    528 _npf_rule_foreach1(prop_array_t rules, nl_rule_callback_t func)
    529 {
    530 	prop_dictionary_t rldict;
    531 	prop_object_iterator_t it;
    532 	unsigned reduce[16], n;
    533 	unsigned nlevel;
    534 
    535 	if (!rules || prop_object_type(rules) != PROP_TYPE_ARRAY) {
    536 		return ENOENT;
    537 	}
    538 	it = prop_array_iterator(rules);
    539 	if (it == NULL) {
    540 		return ENOMEM;
    541 	}
    542 
    543 	nlevel = 0;
    544 	reduce[nlevel] = 0;
    545 	n = 0;
    546 
    547 	while ((rldict = prop_object_iterator_next(it)) != NULL) {
    548 		nl_rule_t nrl = { .nrl_dict = rldict };
    549 		uint32_t skipto = 0;
    550 
    551 		prop_dictionary_get_uint32(rldict, "skip-to", &skipto);
    552 		(*func)(&nrl, nlevel);
    553 		if (skipto) {
    554 			nlevel++;
    555 			reduce[nlevel] = skipto;
    556 		}
    557 		if (reduce[nlevel] == ++n) {
    558 			assert(nlevel > 0);
    559 			nlevel--;
    560 		}
    561 	}
    562 	prop_object_iterator_release(it);
    563 	return 0;
    564 }
    565 
    566 int
    567 _npf_rule_foreach(nl_config_t *ncf, nl_rule_callback_t func)
    568 {
    569 	return _npf_rule_foreach1(ncf->ncf_rules_list, func);
    570 }
    571 
    572 pri_t
    573 _npf_rule_getinfo(nl_rule_t *nrl, const char **rname, uint32_t *attr,
    574     u_int *if_idx)
    575 {
    576 	prop_dictionary_t rldict = nrl->nrl_dict;
    577 	pri_t prio;
    578 
    579 	prop_dictionary_get_cstring_nocopy(rldict, "name", rname);
    580 	prop_dictionary_get_uint32(rldict, "attributes", attr);
    581 	prop_dictionary_get_int32(rldict, "priority", &prio);
    582 	prop_dictionary_get_uint32(rldict, "interface", if_idx);
    583 	return prio;
    584 }
    585 
    586 const void *
    587 _npf_rule_ncode(nl_rule_t *nrl, size_t *size)
    588 {
    589 	prop_dictionary_t rldict = nrl->nrl_dict;
    590 	prop_object_t obj = prop_dictionary_get(rldict, "code");
    591 	*size = prop_data_size(obj);
    592 	return prop_data_data_nocopy(obj);
    593 }
    594 
    595 const char *
    596 _npf_rule_rproc(nl_rule_t *nrl)
    597 {
    598 	prop_dictionary_t rldict = nrl->nrl_dict;
    599 	const char *rpname = NULL;
    600 
    601 	prop_dictionary_get_cstring_nocopy(rldict, "rproc", &rpname);
    602 	return rpname;
    603 }
    604 
    605 void
    606 npf_rule_destroy(nl_rule_t *rl)
    607 {
    608 
    609 	prop_object_release(rl->nrl_dict);
    610 	free(rl);
    611 }
    612 
    613 /*
    614  * RULE PROCEDURE INTERFACE.
    615  */
    616 
    617 nl_rproc_t *
    618 npf_rproc_create(const char *name)
    619 {
    620 	prop_dictionary_t rpdict;
    621 	prop_array_t extcalls;
    622 	nl_rproc_t *nrp;
    623 
    624 	nrp = malloc(sizeof(nl_rproc_t));
    625 	if (nrp == NULL) {
    626 		return NULL;
    627 	}
    628 	rpdict = prop_dictionary_create();
    629 	if (rpdict == NULL) {
    630 		free(nrp);
    631 		return NULL;
    632 	}
    633 	prop_dictionary_set_cstring(rpdict, "name", name);
    634 
    635 	extcalls = prop_array_create();
    636 	if (extcalls == NULL) {
    637 		prop_object_release(rpdict);
    638 		free(nrp);
    639 		return NULL;
    640 	}
    641 	prop_dictionary_set(rpdict, "extcalls", extcalls);
    642 	prop_object_release(extcalls);
    643 
    644 	nrp->nrp_dict = rpdict;
    645 	return nrp;
    646 }
    647 
    648 int
    649 npf_rproc_extcall(nl_rproc_t *rp, nl_ext_t *ext)
    650 {
    651 	prop_dictionary_t rpdict = rp->nrp_dict;
    652 	prop_dictionary_t extdict = ext->nxt_dict;
    653 	prop_array_t extcalls;
    654 
    655 	extcalls = prop_dictionary_get(rpdict, "extcalls");
    656 	if (_npf_prop_array_lookup(extcalls, "name", ext->nxt_name)) {
    657 		return EEXIST;
    658 	}
    659 	prop_dictionary_set_cstring(extdict, "name", ext->nxt_name);
    660 	prop_array_add(extcalls, extdict);
    661 	return 0;
    662 }
    663 
    664 bool
    665 npf_rproc_exists_p(nl_config_t *ncf, const char *name)
    666 {
    667 
    668 	return _npf_prop_array_lookup(ncf->ncf_rproc_list, "name", name);
    669 }
    670 
    671 int
    672 npf_rproc_insert(nl_config_t *ncf, nl_rproc_t *rp)
    673 {
    674 	prop_dictionary_t rpdict = rp->nrp_dict;
    675 	const char *name;
    676 
    677 	if (!prop_dictionary_get_cstring_nocopy(rpdict, "name", &name)) {
    678 		return EINVAL;
    679 	}
    680 	if (npf_rproc_exists_p(ncf, name)) {
    681 		return EEXIST;
    682 	}
    683 	prop_array_add(ncf->ncf_rproc_list, rpdict);
    684 	return 0;
    685 }
    686 
    687 /*
    688  * TRANSLATION INTERFACE.
    689  */
    690 
    691 nl_nat_t *
    692 npf_nat_create(int type, u_int flags, u_int if_idx,
    693     npf_addr_t *addr, int af, in_port_t port)
    694 {
    695 	nl_rule_t *rl;
    696 	prop_dictionary_t rldict;
    697 	prop_data_t addrdat;
    698 	uint32_t attr;
    699 	size_t sz;
    700 
    701 	if (af == AF_INET) {
    702 		sz = sizeof(struct in_addr);
    703 	} else if (af == AF_INET6) {
    704 		sz = sizeof(struct in6_addr);
    705 	} else {
    706 		return NULL;
    707 	}
    708 
    709 	attr = NPF_RULE_PASS | NPF_RULE_FINAL |
    710 	    (type == NPF_NATOUT ? NPF_RULE_OUT : NPF_RULE_IN);
    711 
    712 	/* Create a rule for NAT policy.  Next, will add translation data. */
    713 	rl = npf_rule_create(NULL, attr, if_idx);
    714 	if (rl == NULL) {
    715 		return NULL;
    716 	}
    717 	rldict = rl->nrl_dict;
    718 
    719 	/* Translation type and flags. */
    720 	prop_dictionary_set_int32(rldict, "type", type);
    721 	prop_dictionary_set_uint32(rldict, "flags", flags);
    722 
    723 	/* Translation IP. */
    724 	addrdat = prop_data_create_data(addr, sz);
    725 	if (addrdat == NULL) {
    726 		npf_rule_destroy(rl);
    727 		return NULL;
    728 	}
    729 	prop_dictionary_set(rldict, "translation-ip", addrdat);
    730 	prop_object_release(addrdat);
    731 
    732 	/* Translation port (for redirect case). */
    733 	prop_dictionary_set_uint16(rldict, "translation-port", port);
    734 
    735 	return (nl_nat_t *)rl;
    736 }
    737 
    738 int
    739 npf_nat_insert(nl_config_t *ncf, nl_nat_t *nt, pri_t pri)
    740 {
    741 	prop_dictionary_t rldict = nt->nrl_dict;
    742 
    743 	prop_dictionary_set_int32(rldict, "priority", NPF_PRI_LAST);
    744 	prop_array_add(ncf->ncf_nat_list, rldict);
    745 	return 0;
    746 }
    747 
    748 int
    749 _npf_nat_foreach(nl_config_t *ncf, nl_rule_callback_t func)
    750 {
    751 	return _npf_rule_foreach1(ncf->ncf_nat_list, func);
    752 }
    753 
    754 void
    755 _npf_nat_getinfo(nl_nat_t *nt, int *type, u_int *flags, npf_addr_t *addr,
    756     size_t *alen, in_port_t *port)
    757 {
    758 	prop_dictionary_t rldict = nt->nrl_dict;
    759 
    760 	prop_dictionary_get_int32(rldict, "type", type);
    761 	prop_dictionary_get_uint32(rldict, "flags", flags);
    762 
    763 	prop_object_t obj = prop_dictionary_get(rldict, "translation-ip");
    764 	*alen = prop_data_size(obj);
    765 	memcpy(addr, prop_data_data_nocopy(obj), *alen);
    766 
    767 	prop_dictionary_get_uint16(rldict, "translation-port", port);
    768 }
    769 
    770 /*
    771  * TABLE INTERFACE.
    772  */
    773 
    774 nl_table_t *
    775 npf_table_create(u_int id, int type)
    776 {
    777 	prop_dictionary_t tldict;
    778 	prop_array_t tblents;
    779 	nl_table_t *tl;
    780 
    781 	tl = malloc(sizeof(*tl));
    782 	if (tl == NULL) {
    783 		return NULL;
    784 	}
    785 	tldict = prop_dictionary_create();
    786 	if (tldict == NULL) {
    787 		free(tl);
    788 		return NULL;
    789 	}
    790 	prop_dictionary_set_uint32(tldict, "id", id);
    791 	prop_dictionary_set_int32(tldict, "type", type);
    792 
    793 	tblents = prop_array_create();
    794 	if (tblents == NULL) {
    795 		prop_object_release(tldict);
    796 		free(tl);
    797 		return NULL;
    798 	}
    799 	prop_dictionary_set(tldict, "entries", tblents);
    800 	prop_object_release(tblents);
    801 
    802 	tl->ntl_dict = tldict;
    803 	return tl;
    804 }
    805 
    806 int
    807 npf_table_add_entry(nl_table_t *tl, int af, const npf_addr_t *addr,
    808     const npf_netmask_t mask)
    809 {
    810 	prop_dictionary_t tldict = tl->ntl_dict, entdict;
    811 	prop_array_t tblents;
    812 	prop_data_t addrdata;
    813 	unsigned alen;
    814 
    815 	/* Create the table entry. */
    816 	entdict = prop_dictionary_create();
    817 	if (entdict == NULL) {
    818 		return ENOMEM;
    819 	}
    820 
    821 	switch (af) {
    822 	case AF_INET:
    823 		alen = sizeof(struct in_addr);
    824 		break;
    825 	case AF_INET6:
    826 		alen = sizeof(struct in6_addr);
    827 		break;
    828 	default:
    829 		return EINVAL;
    830 	}
    831 
    832 	addrdata = prop_data_create_data(addr, alen);
    833 	prop_dictionary_set(entdict, "addr", addrdata);
    834 	prop_dictionary_set_uint8(entdict, "mask", mask);
    835 	prop_object_release(addrdata);
    836 
    837 	tblents = prop_dictionary_get(tldict, "entries");
    838 	prop_array_add(tblents, entdict);
    839 	prop_object_release(entdict);
    840 	return 0;
    841 }
    842 
    843 bool
    844 npf_table_exists_p(nl_config_t *ncf, u_int tid)
    845 {
    846 	prop_dictionary_t tldict;
    847 	prop_object_iterator_t it;
    848 
    849 	it = prop_array_iterator(ncf->ncf_table_list);
    850 	while ((tldict = prop_object_iterator_next(it)) != NULL) {
    851 		u_int i;
    852 		if (prop_dictionary_get_uint32(tldict, "id", &i) && tid == i)
    853 			break;
    854 	}
    855 	prop_object_iterator_release(it);
    856 	return tldict ? true : false;
    857 }
    858 
    859 int
    860 npf_table_insert(nl_config_t *ncf, nl_table_t *tl)
    861 {
    862 	prop_dictionary_t tldict = tl->ntl_dict;
    863 	u_int tid;
    864 
    865 	if (!prop_dictionary_get_uint32(tldict, "id", &tid)) {
    866 		return EINVAL;
    867 	}
    868 	if (npf_table_exists_p(ncf, tid)) {
    869 		return EEXIST;
    870 	}
    871 	prop_array_add(ncf->ncf_table_list, tldict);
    872 	return 0;
    873 }
    874 
    875 void
    876 npf_table_destroy(nl_table_t *tl)
    877 {
    878 
    879 	prop_object_release(tl->ntl_dict);
    880 	free(tl);
    881 }
    882 
    883 void
    884 _npf_table_foreach(nl_config_t *ncf, nl_table_callback_t func)
    885 {
    886 	prop_dictionary_t tldict;
    887 	prop_object_iterator_t it;
    888 
    889 	it = prop_array_iterator(ncf->ncf_table_list);
    890 	while ((tldict = prop_object_iterator_next(it)) != NULL) {
    891 		u_int id;
    892 		int type;
    893 
    894 		prop_dictionary_get_uint32(tldict, "id", &id);
    895 		prop_dictionary_get_int32(tldict, "type", &type);
    896 		(*func)(id, type);
    897 	}
    898 	prop_object_iterator_release(it);
    899 }
    900 
    901 /*
    902  * MISC.
    903  */
    904 
    905 int
    906 npf_sessions_recv(int fd, const char *fpath)
    907 {
    908 	prop_dictionary_t sdict;
    909 	int error;
    910 
    911 	error = prop_dictionary_recv_ioctl(fd, IOC_NPF_SESSIONS_SAVE, &sdict);
    912 	if (error) {
    913 		return error;
    914 	}
    915 	if (!prop_dictionary_externalize_to_file(sdict, fpath)) {
    916 		error = errno;
    917 	}
    918 	prop_object_release(sdict);
    919 	return error;
    920 }
    921 
    922 int
    923 npf_sessions_send(int fd, const char *fpath)
    924 {
    925 	prop_dictionary_t sdict;
    926 	int error;
    927 
    928 	if (fpath) {
    929 		sdict = prop_dictionary_internalize_from_file(fpath);
    930 		if (sdict == NULL) {
    931 			return errno;
    932 		}
    933 	} else {
    934 		/* Empty: will flush the sessions. */
    935 		prop_array_t selist = prop_array_create();
    936 		sdict = prop_dictionary_create();
    937 		prop_dictionary_set(sdict, "session-list", selist);
    938 		prop_object_release(selist);
    939 	}
    940 	error = prop_dictionary_send_ioctl(sdict, fd, IOC_NPF_SESSIONS_LOAD);
    941 	prop_object_release(sdict);
    942 	return error;
    943 }
    944 
    945 static prop_dictionary_t
    946 _npf_debug_initonce(nl_config_t *ncf)
    947 {
    948 	if (!ncf->ncf_debug) {
    949 		prop_array_t iflist = prop_array_create();
    950 		ncf->ncf_debug = prop_dictionary_create();
    951 		prop_dictionary_set(ncf->ncf_debug, "interfaces", iflist);
    952 		prop_object_release(iflist);
    953 	}
    954 	return ncf->ncf_debug;
    955 }
    956 
    957 void
    958 _npf_debug_addif(nl_config_t *ncf, struct ifaddrs *ifa, u_int if_idx)
    959 {
    960 	prop_dictionary_t ifdict, dbg = _npf_debug_initonce(ncf);
    961 	prop_array_t iflist = prop_dictionary_get(dbg, "interfaces");
    962 
    963 	if (_npf_prop_array_lookup(iflist, "name", ifa->ifa_name)) {
    964 		return;
    965 	}
    966 
    967 	ifdict = prop_dictionary_create();
    968 	prop_dictionary_set_cstring(ifdict, "name", ifa->ifa_name);
    969 	prop_dictionary_set_uint32(ifdict, "flags", ifa->ifa_flags);
    970 	if (!if_idx) {
    971 		if_idx = if_nametoindex(ifa->ifa_name);
    972 	}
    973 	prop_dictionary_set_uint32(ifdict, "idx", if_idx);
    974 
    975 	const struct sockaddr *sa = ifa->ifa_addr;
    976 	npf_addr_t addr;
    977 	size_t alen = 0;
    978 
    979 	switch (sa ? sa->sa_family : -1) {
    980 	case AF_INET: {
    981 		const struct sockaddr_in *sin = (const void *)sa;
    982 		alen = sizeof(sin->sin_addr);
    983 		memcpy(&addr, &sin->sin_addr, alen);
    984 		break;
    985 	}
    986 	case AF_INET6: {
    987 		const struct sockaddr_in6 *sin6 = (const void *)sa;
    988 		alen = sizeof(sin6->sin6_addr);
    989 		memcpy(&addr, &sin6->sin6_addr, alen);
    990 		break;
    991 	}
    992 	default:
    993 		break;
    994 	}
    995 
    996 	if (alen) {
    997 		prop_data_t addrdata = prop_data_create_data(&addr, alen);
    998 		prop_dictionary_set(ifdict, "addr", addrdata);
    999 		prop_object_release(addrdata);
   1000 	}
   1001 	prop_array_add(iflist, ifdict);
   1002 	prop_object_release(ifdict);
   1003 }
   1004