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