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