Home | History | Annotate | Line # | Download | only in libnpf
npf.c revision 1.9
      1 /*	$NetBSD: npf.c,v 1.9 2012/07/01 23:21:07 rmind Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2010-2012 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.9 2012/07/01 23:21:07 rmind Exp $");
     34 
     35 #include <sys/types.h>
     36 #include <netinet/in_systm.h>
     37 #include <netinet/in.h>
     38 #include <prop/proplib.h>
     39 
     40 #include <stdlib.h>
     41 #include <string.h>
     42 #include <assert.h>
     43 #include <errno.h>
     44 #include <err.h>
     45 
     46 #define	_NPF_PRIVATE
     47 #include "npf.h"
     48 
     49 struct nl_config {
     50 	/* Rules, translations, tables, procedures. */
     51 	prop_dictionary_t	ncf_dict;
     52 	prop_array_t		ncf_rules_list;
     53 	prop_array_t		ncf_rproc_list;
     54 	prop_array_t		ncf_table_list;
     55 	prop_array_t		ncf_nat_list;
     56 	/* Priority counters. */
     57 	pri_t			ncf_rule_pri;
     58 	pri_t			ncf_nat_pri;
     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 /*
     79  * CONFIGURATION INTERFACE.
     80  */
     81 
     82 nl_config_t *
     83 npf_config_create(void)
     84 {
     85 	nl_config_t *ncf;
     86 
     87 	ncf = calloc(1, sizeof(*ncf));
     88 	if (ncf == NULL) {
     89 		return NULL;
     90 	}
     91 	ncf->ncf_rules_list = prop_array_create();
     92 	ncf->ncf_rproc_list = prop_array_create();
     93 	ncf->ncf_table_list = prop_array_create();
     94 	ncf->ncf_nat_list = prop_array_create();
     95 
     96 	ncf->ncf_rule_pri = 1;
     97 	ncf->ncf_nat_pri = 1;
     98 
     99 	ncf->ncf_plist = NULL;
    100 	ncf->ncf_flush = false;
    101 
    102 	return ncf;
    103 }
    104 
    105 int
    106 npf_config_submit(nl_config_t *ncf, int fd)
    107 {
    108 	const char *plist = ncf->ncf_plist;
    109 	prop_dictionary_t npf_dict;
    110 	int error = 0;
    111 
    112 	npf_dict = prop_dictionary_create();
    113 	if (npf_dict == NULL) {
    114 		return ENOMEM;
    115 	}
    116 	prop_dictionary_set(npf_dict, "rules", ncf->ncf_rules_list);
    117 	prop_dictionary_set(npf_dict, "rprocs", ncf->ncf_rproc_list);
    118 	prop_dictionary_set(npf_dict, "tables", ncf->ncf_table_list);
    119 	prop_dictionary_set(npf_dict, "translation", ncf->ncf_nat_list);
    120 	prop_dictionary_set_bool(npf_dict, "flush", ncf->ncf_flush);
    121 
    122 	if (plist) {
    123 		if (!prop_dictionary_externalize_to_file(npf_dict, plist)) {
    124 			error = errno;
    125 		}
    126 		prop_object_release(npf_dict);
    127 		return error;
    128 	}
    129 
    130 	error = prop_dictionary_sendrecv_ioctl(npf_dict, fd,
    131 	    IOC_NPF_RELOAD, &ncf->ncf_err);
    132 	if (error) {
    133 		prop_object_release(npf_dict);
    134 		assert(ncf->ncf_err == NULL);
    135 		return error;
    136 	}
    137 
    138 	prop_dictionary_get_int32(ncf->ncf_err, "errno", &error);
    139 	prop_object_release(npf_dict);
    140 	return error;
    141 }
    142 
    143 nl_config_t *
    144 npf_config_retrieve(int fd, bool *active, bool *loaded)
    145 {
    146 	prop_dictionary_t npf_dict;
    147 	nl_config_t *ncf;
    148 	int error;
    149 
    150 	error = prop_dictionary_recv_ioctl(fd, IOC_NPF_GETCONF, &npf_dict);
    151 	if (error) {
    152 		return NULL;
    153 	}
    154 	ncf = calloc(1, sizeof(*ncf));
    155 	if (ncf == NULL) {
    156 		prop_object_release(npf_dict);
    157 		return NULL;
    158 	}
    159 	ncf->ncf_dict = npf_dict;
    160 	ncf->ncf_rules_list = prop_dictionary_get(npf_dict, "rules");
    161 	ncf->ncf_rproc_list = prop_dictionary_get(npf_dict, "rprocs");
    162 	ncf->ncf_table_list = prop_dictionary_get(npf_dict, "tables");
    163 	ncf->ncf_nat_list = prop_dictionary_get(npf_dict, "translation");
    164 
    165 	prop_dictionary_get_bool(npf_dict, "active", active);
    166 	*loaded = (ncf->ncf_rules_list != NULL);
    167 	return ncf;
    168 }
    169 
    170 int
    171 npf_config_flush(int fd)
    172 {
    173 	nl_config_t *ncf;
    174 	int error;
    175 
    176 	ncf = npf_config_create();
    177 	if (ncf == NULL) {
    178 		return ENOMEM;
    179 	}
    180 	ncf->ncf_flush = true;
    181 	error = npf_config_submit(ncf, fd);
    182 	npf_config_destroy(ncf);
    183 	return error;
    184 }
    185 
    186 void
    187 _npf_config_error(nl_config_t *ncf, nl_error_t *ne)
    188 {
    189 	memset(ne, 0, sizeof(*ne));
    190 	prop_dictionary_get_int32(ncf->ncf_err, "id", &ne->ne_id);
    191 	prop_dictionary_get_cstring(ncf->ncf_err,
    192 	    "source-file", &ne->ne_source_file);
    193 	prop_dictionary_get_uint32(ncf->ncf_err,
    194 	    "source-line", &ne->ne_source_line);
    195 	prop_dictionary_get_int32(ncf->ncf_err,
    196 	    "ncode-error", &ne->ne_ncode_error);
    197 	prop_dictionary_get_int32(ncf->ncf_err,
    198 	    "ncode-errat", &ne->ne_ncode_errat);
    199 }
    200 
    201 void
    202 npf_config_destroy(nl_config_t *ncf)
    203 {
    204 
    205 	if (ncf->ncf_dict == NULL) {
    206 		prop_object_release(ncf->ncf_rules_list);
    207 		prop_object_release(ncf->ncf_rproc_list);
    208 		prop_object_release(ncf->ncf_table_list);
    209 		prop_object_release(ncf->ncf_nat_list);
    210 	} else {
    211 		prop_object_release(ncf->ncf_dict);
    212 	}
    213 	if (ncf->ncf_err) {
    214 		prop_object_release(ncf->ncf_err);
    215 	}
    216 	free(ncf);
    217 }
    218 
    219 void
    220 _npf_config_setsubmit(nl_config_t *ncf, const char *plist_file)
    221 {
    222 
    223 	ncf->ncf_plist = plist_file;
    224 }
    225 
    226 static bool
    227 _npf_prop_array_lookup(prop_array_t array, const char *key, const char *name)
    228 {
    229 	prop_dictionary_t dict;
    230 	prop_object_iterator_t it;
    231 
    232 	it = prop_array_iterator(array);
    233 	while ((dict = prop_object_iterator_next(it)) != NULL) {
    234 		const char *lname;
    235 		prop_dictionary_get_cstring_nocopy(dict, key, &lname);
    236 		if (strcmp(name, lname) == 0)
    237 			break;
    238 	}
    239 	prop_object_iterator_release(it);
    240 	return dict ? true : false;
    241 }
    242 
    243 /*
    244  * RULE INTERFACE.
    245  */
    246 
    247 nl_rule_t *
    248 npf_rule_create(const char *name, uint32_t attr, u_int if_idx)
    249 {
    250 	prop_dictionary_t rldict;
    251 	nl_rule_t *rl;
    252 
    253 	rl = malloc(sizeof(*rl));
    254 	if (rl == NULL) {
    255 		return NULL;
    256 	}
    257 	rldict = prop_dictionary_create();
    258 	if (rldict == NULL) {
    259 		free(rl);
    260 		return NULL;
    261 	}
    262 	if (name) {
    263 		prop_dictionary_set_cstring(rldict, "name", name);
    264 	}
    265 	prop_dictionary_set_uint32(rldict, "attributes", attr);
    266 
    267 	if (if_idx) {
    268 		prop_dictionary_set_uint32(rldict, "interface", if_idx);
    269 	}
    270 	rl->nrl_dict = rldict;
    271 	return rl;
    272 }
    273 
    274 int
    275 npf_rule_setcode(nl_rule_t *rl, int type, const void *code, size_t sz)
    276 {
    277 	prop_dictionary_t rldict = rl->nrl_dict;
    278 	prop_data_t cdata;
    279 
    280 	if (type != NPF_CODE_NCODE) {
    281 		return ENOTSUP;
    282 	}
    283 	cdata = prop_data_create_data(code, sz);
    284 	if (cdata == NULL) {
    285 		return ENOMEM;
    286 	}
    287 	prop_dictionary_set(rldict, "ncode", cdata);
    288 	prop_object_release(cdata);
    289 	return 0;
    290 }
    291 
    292 int
    293 npf_rule_setproc(nl_config_t *ncf, nl_rule_t *rl, const char *name)
    294 {
    295 	prop_dictionary_t rldict = rl->nrl_dict;
    296 
    297 	if (!npf_rproc_exists_p(ncf, name)) {
    298 		return ENOENT;
    299 	}
    300 	prop_dictionary_set_cstring(rldict, "rproc", name);
    301 	return 0;
    302 }
    303 
    304 bool
    305 npf_rule_exists_p(nl_config_t *ncf, const char *name)
    306 {
    307 
    308 	return _npf_prop_array_lookup(ncf->ncf_rules_list, "name", name);
    309 }
    310 
    311 int
    312 npf_rule_insert(nl_config_t *ncf, nl_rule_t *parent, nl_rule_t *rl, pri_t pri)
    313 {
    314 	prop_dictionary_t rldict = rl->nrl_dict;
    315 	prop_array_t rlset;
    316 
    317 	if (pri == NPF_PRI_NEXT) {
    318 		pri = ncf->ncf_rule_pri++;
    319 	} else if (ncf) {
    320 		ncf->ncf_rule_pri = pri + 1;
    321 	}
    322 	prop_dictionary_set_int32(rldict, "priority", pri);
    323 
    324 	if (parent) {
    325 		prop_dictionary_t pdict = parent->nrl_dict;
    326 		rlset = prop_dictionary_get(pdict, "subrules");
    327 		if (rlset == NULL) {
    328 			rlset = prop_array_create();
    329 			prop_dictionary_set(pdict, "subrules", rlset);
    330 			prop_object_release(rlset);
    331 		}
    332 	} else {
    333 		rlset = ncf->ncf_rules_list;
    334 	}
    335 	prop_array_add(rlset, rldict);
    336 	return 0;
    337 }
    338 
    339 static int
    340 _npf_rule_foreach1(prop_array_t rules, unsigned nlevel, nl_rule_callback_t func)
    341 {
    342 	prop_dictionary_t rldict;
    343 	prop_object_iterator_t it;
    344 
    345 	if (!rules || prop_object_type(rules) != PROP_TYPE_ARRAY) {
    346 		return ENOENT;
    347 	}
    348 	it = prop_array_iterator(rules);
    349 	if (it == NULL) {
    350 		return ENOMEM;
    351 	}
    352 	while ((rldict = prop_object_iterator_next(it)) != NULL) {
    353 		prop_array_t subrules;
    354 		nl_rule_t nrl;
    355 
    356 		nrl.nrl_dict = rldict;
    357 		(*func)(&nrl, nlevel);
    358 
    359 		subrules = prop_dictionary_get(rldict, "subrules");
    360 		(void)_npf_rule_foreach1(subrules, nlevel + 1, func);
    361 	}
    362 	prop_object_iterator_release(it);
    363 	return 0;
    364 }
    365 
    366 int
    367 _npf_rule_foreach(nl_config_t *ncf, nl_rule_callback_t func)
    368 {
    369 
    370 	return _npf_rule_foreach1(ncf->ncf_rules_list, 0, func);
    371 }
    372 
    373 pri_t
    374 _npf_rule_getinfo(nl_rule_t *nrl, const char **rname, uint32_t *attr,
    375     u_int *if_idx)
    376 {
    377 	prop_dictionary_t rldict = nrl->nrl_dict;
    378 	pri_t prio;
    379 
    380 	prop_dictionary_get_cstring_nocopy(rldict, "name", rname);
    381 	prop_dictionary_get_uint32(rldict, "attributes", attr);
    382 	prop_dictionary_get_int32(rldict, "priority", &prio);
    383 	prop_dictionary_get_uint32(rldict, "interface", if_idx);
    384 	return prio;
    385 }
    386 
    387 const void *
    388 _npf_rule_ncode(nl_rule_t *nrl, size_t *size)
    389 {
    390 	prop_dictionary_t rldict = nrl->nrl_dict;
    391 	prop_object_t obj = prop_dictionary_get(rldict, "ncode");
    392 	*size = prop_data_size(obj);
    393 	return prop_data_data_nocopy(obj);
    394 }
    395 
    396 const char *
    397 _npf_rule_rproc(nl_rule_t *nrl)
    398 {
    399 	prop_dictionary_t rldict = nrl->nrl_dict;
    400 	const char *rpname = NULL;
    401 
    402 	prop_dictionary_get_cstring_nocopy(rldict, "rproc", &rpname);
    403 	return rpname;
    404 }
    405 
    406 void
    407 npf_rule_destroy(nl_rule_t *rl)
    408 {
    409 
    410 	prop_object_release(rl->nrl_dict);
    411 	free(rl);
    412 }
    413 
    414 /*
    415  * RULE PROCEDURE INTERFACE.
    416  */
    417 
    418 nl_rproc_t *
    419 npf_rproc_create(const char *name)
    420 {
    421 	prop_dictionary_t rpdict;
    422 	nl_rproc_t *nrp;
    423 
    424 	nrp = malloc(sizeof(nl_rproc_t));
    425 	if (nrp == NULL) {
    426 		return NULL;
    427 	}
    428 	rpdict = prop_dictionary_create();
    429 	if (rpdict == NULL) {
    430 		free(nrp);
    431 		return NULL;
    432 	}
    433 	prop_dictionary_set_cstring(rpdict, "name", name);
    434 	nrp->nrp_dict = rpdict;
    435 	return nrp;
    436 }
    437 
    438 bool
    439 npf_rproc_exists_p(nl_config_t *ncf, const char *name)
    440 {
    441 
    442 	return _npf_prop_array_lookup(ncf->ncf_rproc_list, "name", name);
    443 }
    444 
    445 int
    446 _npf_rproc_setnorm(nl_rproc_t *rp, bool rnd, bool no_df, u_int minttl,
    447     u_int maxmss)
    448 {
    449 	prop_dictionary_t rpdict = rp->nrp_dict;
    450 	uint32_t fl;
    451 
    452 	prop_dictionary_set_bool(rpdict, "randomize-id", rnd);
    453 	prop_dictionary_set_bool(rpdict, "no-df", no_df);
    454 	prop_dictionary_set_uint32(rpdict, "min-ttl", minttl);
    455 	prop_dictionary_set_uint32(rpdict, "max-mss", maxmss);
    456 
    457 	prop_dictionary_get_uint32(rpdict, "flags", &fl);
    458 	prop_dictionary_set_uint32(rpdict, "flags", fl | NPF_RPROC_NORMALIZE);
    459 	return 0;
    460 }
    461 
    462 int
    463 _npf_rproc_setlog(nl_rproc_t *rp, u_int if_idx)
    464 {
    465 	prop_dictionary_t rpdict = rp->nrp_dict;
    466 	uint32_t fl;
    467 
    468 	prop_dictionary_set_uint32(rpdict, "log-interface", if_idx);
    469 
    470 	prop_dictionary_get_uint32(rpdict, "flags", &fl);
    471 	prop_dictionary_set_uint32(rpdict, "flags", fl | NPF_RPROC_LOG);
    472 	return 0;
    473 }
    474 
    475 int
    476 npf_rproc_insert(nl_config_t *ncf, nl_rproc_t *rp)
    477 {
    478 	prop_dictionary_t rpdict = rp->nrp_dict;
    479 	const char *name;
    480 
    481 	if (!prop_dictionary_get_cstring_nocopy(rpdict, "name", &name)) {
    482 		return EINVAL;
    483 	}
    484 	if (npf_rproc_exists_p(ncf, name)) {
    485 		return EEXIST;
    486 	}
    487 	prop_array_add(ncf->ncf_rproc_list, rpdict);
    488 	return 0;
    489 }
    490 
    491 /*
    492  * TRANSLATION INTERFACE.
    493  */
    494 
    495 nl_nat_t *
    496 npf_nat_create(int type, u_int flags, u_int if_idx,
    497     npf_addr_t *addr, int af, in_port_t port)
    498 {
    499 	nl_rule_t *rl;
    500 	prop_dictionary_t rldict;
    501 	prop_data_t addrdat;
    502 	uint32_t attr;
    503 	size_t sz;
    504 
    505 	if (af == AF_INET) {
    506 		sz = sizeof(struct in_addr);
    507 	} else if (af == AF_INET6) {
    508 		sz = sizeof(struct in6_addr);
    509 	} else {
    510 		return NULL;
    511 	}
    512 
    513 	attr = NPF_RULE_PASS | NPF_RULE_FINAL |
    514 	    (type == NPF_NATOUT ? NPF_RULE_OUT : NPF_RULE_IN);
    515 
    516 	/* Create a rule for NAT policy.  Next, will add translation data. */
    517 	rl = npf_rule_create(NULL, attr, if_idx);
    518 	if (rl == NULL) {
    519 		return NULL;
    520 	}
    521 	rldict = rl->nrl_dict;
    522 
    523 	/* Translation type and flags. */
    524 	prop_dictionary_set_int32(rldict, "type", type);
    525 	prop_dictionary_set_uint32(rldict, "flags", flags);
    526 
    527 	/* Translation IP. */
    528 	addrdat = prop_data_create_data(addr, sz);
    529 	if (addrdat == NULL) {
    530 		npf_rule_destroy(rl);
    531 		return NULL;
    532 	}
    533 	prop_dictionary_set(rldict, "translation-ip", addrdat);
    534 	prop_object_release(addrdat);
    535 
    536 	/* Translation port (for redirect case). */
    537 	prop_dictionary_set_uint16(rldict, "translation-port", port);
    538 
    539 	return (nl_nat_t *)rl;
    540 }
    541 
    542 int
    543 npf_nat_insert(nl_config_t *ncf, nl_nat_t *nt, pri_t pri)
    544 {
    545 	prop_dictionary_t rldict = nt->nrl_dict;
    546 
    547 	if (pri == NPF_PRI_NEXT) {
    548 		pri = ncf->ncf_nat_pri++;
    549 	} else {
    550 		ncf->ncf_nat_pri = pri + 1;
    551 	}
    552 	prop_dictionary_set_int32(rldict, "priority", pri);
    553 	prop_array_add(ncf->ncf_nat_list, rldict);
    554 	return 0;
    555 }
    556 
    557 int
    558 _npf_nat_foreach(nl_config_t *ncf, nl_rule_callback_t func)
    559 {
    560 
    561 	return _npf_rule_foreach1(ncf->ncf_nat_list, 0, func);
    562 }
    563 
    564 void
    565 _npf_nat_getinfo(nl_nat_t *nt, int *type, u_int *flags, npf_addr_t *addr,
    566     size_t *alen, in_port_t *port)
    567 {
    568 	prop_dictionary_t rldict = nt->nrl_dict;
    569 
    570 	prop_dictionary_get_int32(rldict, "type", type);
    571 	prop_dictionary_get_uint32(rldict, "flags", flags);
    572 
    573 	prop_object_t obj = prop_dictionary_get(rldict, "translation-ip");
    574 	*alen = prop_data_size(obj);
    575 	memcpy(addr, prop_data_data_nocopy(obj), *alen);
    576 
    577 	prop_dictionary_get_uint16(rldict, "translation-port", port);
    578 }
    579 
    580 /*
    581  * TABLE INTERFACE.
    582  */
    583 
    584 nl_table_t *
    585 npf_table_create(u_int id, int type)
    586 {
    587 	prop_dictionary_t tldict;
    588 	prop_array_t tblents;
    589 	nl_table_t *tl;
    590 
    591 	tl = malloc(sizeof(*tl));
    592 	if (tl == NULL) {
    593 		return NULL;
    594 	}
    595 	tldict = prop_dictionary_create();
    596 	if (tldict == NULL) {
    597 		free(tl);
    598 		return NULL;
    599 	}
    600 	prop_dictionary_set_uint32(tldict, "id", id);
    601 	prop_dictionary_set_int32(tldict, "type", type);
    602 
    603 	tblents = prop_array_create();
    604 	if (tblents == NULL) {
    605 		prop_object_release(tldict);
    606 		free(tl);
    607 		return NULL;
    608 	}
    609 	prop_dictionary_set(tldict, "entries", tblents);
    610 	prop_object_release(tblents);
    611 
    612 	tl->ntl_dict = tldict;
    613 	return tl;
    614 }
    615 
    616 int
    617 npf_table_add_entry(nl_table_t *tl, npf_addr_t *addr, npf_netmask_t mask)
    618 {
    619 	prop_dictionary_t tldict = tl->ntl_dict, entdict;
    620 	prop_array_t tblents;
    621 	prop_data_t addrdata;
    622 
    623 	/* Create the table entry. */
    624 	entdict = prop_dictionary_create();
    625 	if (entdict) {
    626 		return ENOMEM;
    627 	}
    628 	addrdata = prop_data_create_data(addr, sizeof(npf_addr_t));
    629 	prop_dictionary_set(entdict, "addr", addrdata);
    630 	prop_dictionary_set_uint8(entdict, "mask", mask);
    631 	prop_object_release(addrdata);
    632 
    633 	/* Insert the entry. */
    634 	tblents = prop_dictionary_get(tldict, "entries");
    635 	prop_array_add(tblents, entdict);
    636 	prop_object_release(entdict);
    637 	return 0;
    638 }
    639 
    640 bool
    641 npf_table_exists_p(nl_config_t *ncf, u_int tid)
    642 {
    643 	prop_dictionary_t tldict;
    644 	prop_object_iterator_t it;
    645 
    646 	it = prop_array_iterator(ncf->ncf_table_list);
    647 	while ((tldict = prop_object_iterator_next(it)) != NULL) {
    648 		u_int i;
    649 		if (prop_dictionary_get_uint32(tldict, "id", &i) && tid == i)
    650 			break;
    651 	}
    652 	prop_object_iterator_release(it);
    653 	return tldict ? true : false;
    654 }
    655 
    656 int
    657 npf_table_insert(nl_config_t *ncf, nl_table_t *tl)
    658 {
    659 	prop_dictionary_t tldict = tl->ntl_dict;
    660 	u_int tid;
    661 
    662 	if (!prop_dictionary_get_uint32(tldict, "id", &tid)) {
    663 		return EINVAL;
    664 	}
    665 	if (npf_table_exists_p(ncf, tid)) {
    666 		return EEXIST;
    667 	}
    668 	prop_array_add(ncf->ncf_table_list, tldict);
    669 	return 0;
    670 }
    671 
    672 void
    673 npf_table_destroy(nl_table_t *tl)
    674 {
    675 
    676 	prop_object_release(tl->ntl_dict);
    677 	free(tl);
    678 }
    679 
    680 void
    681 _npf_table_foreach(nl_config_t *ncf, nl_table_callback_t func)
    682 {
    683 	prop_dictionary_t tldict;
    684 	prop_object_iterator_t it;
    685 
    686 	it = prop_array_iterator(ncf->ncf_table_list);
    687 	while ((tldict = prop_object_iterator_next(it)) != NULL) {
    688 		u_int id;
    689 		int type;
    690 
    691 		prop_dictionary_get_uint32(tldict, "id", &id);
    692 		prop_dictionary_get_int32(tldict, "type", &type);
    693 		(*func)(id, type);
    694 	}
    695 	prop_object_iterator_release(it);
    696 }
    697 
    698 /*
    699  * MISC.
    700  */
    701 
    702 int
    703 npf_update_rule(int fd, const char *rname __unused, nl_rule_t *rl)
    704 {
    705 	prop_dictionary_t rldict = rl->nrl_dict, errdict = NULL;
    706 	int error;
    707 
    708 	error = prop_dictionary_sendrecv_ioctl(rldict, fd,
    709 	    IOC_NPF_UPDATE_RULE, &errdict);
    710 	if (errdict) {
    711 		prop_object_release(errdict);
    712 	}
    713 	return error;
    714 }
    715 
    716 int
    717 npf_sessions_recv(int fd, const char *fpath)
    718 {
    719 	prop_dictionary_t sdict;
    720 	int error;
    721 
    722 	error = prop_dictionary_recv_ioctl(fd, IOC_NPF_SESSIONS_SAVE, &sdict);
    723 	if (error) {
    724 		return error;
    725 	}
    726 	if (!prop_dictionary_externalize_to_file(sdict, fpath)) {
    727 		error = errno;
    728 	}
    729 	prop_object_release(sdict);
    730 	return error;
    731 }
    732 
    733 int
    734 npf_sessions_send(int fd, const char *fpath)
    735 {
    736 	prop_dictionary_t sdict;
    737 	int error;
    738 
    739 	if (fpath) {
    740 		sdict = prop_dictionary_internalize_from_file(fpath);
    741 		if (sdict == NULL) {
    742 			return errno;
    743 		}
    744 	} else {
    745 		/* Empty: will flush the sessions. */
    746 		prop_array_t selist = prop_array_create();
    747 		sdict = prop_dictionary_create();
    748 		prop_dictionary_set(sdict, "session-list", selist);
    749 		prop_object_release(selist);
    750 	}
    751 	error = prop_dictionary_send_ioctl(sdict, fd, IOC_NPF_SESSIONS_LOAD);
    752 	prop_object_release(sdict);
    753 	return error;
    754 }
    755