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