npf_ctl.c revision 1.45 1 /* $NetBSD: npf_ctl.c,v 1.45 2016/12/26 23:05:06 christos 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.45 2016/12/26 23:05:06 christos 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_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 if (type == NPF_TABLE_HASH) {
164 size = 1024; /* XXX */
165 }
166
167 /* Create and insert the table. */
168 t = npf_table_create(name, (u_int)tid, type, blob, size);
169 if (t == NULL) {
170 NPF_ERR_DEBUG(errdict);
171 error = ENOMEM;
172 break;
173 }
174 error = npf_tableset_insert(tblset, t);
175 KASSERT(error == 0);
176
177 if (ents && (error = npf_mk_table_entries(t, ents)) != 0) {
178 NPF_ERR_DEBUG(errdict);
179 break;
180 }
181 }
182 prop_object_iterator_release(it);
183 /*
184 * Note: in a case of error, caller will free the tableset.
185 */
186 return error;
187 }
188
189 static npf_rproc_t *
190 npf_mk_singlerproc(npf_t *npf, prop_dictionary_t rpdict)
191 {
192 prop_object_iterator_t it;
193 prop_dictionary_t extdict;
194 prop_array_t extlist;
195 npf_rproc_t *rp;
196
197 extlist = prop_dictionary_get(rpdict, "extcalls");
198 if (prop_object_type(extlist) != PROP_TYPE_ARRAY) {
199 return NULL;
200 }
201
202 rp = npf_rproc_create(rpdict);
203 if (rp == NULL) {
204 return NULL;
205 }
206
207 it = prop_array_iterator(extlist);
208 while ((extdict = prop_object_iterator_next(it)) != NULL) {
209 const char *name;
210
211 if (!prop_dictionary_get_cstring_nocopy(extdict, "name",
212 &name) || npf_ext_construct(npf, name, rp, extdict)) {
213 npf_rproc_release(rp);
214 rp = NULL;
215 break;
216 }
217 }
218 prop_object_iterator_release(it);
219 return rp;
220 }
221
222 static int __noinline
223 npf_mk_rprocs(npf_t *npf, npf_rprocset_t *rpset, prop_array_t rprocs,
224 prop_dictionary_t errdict)
225 {
226 prop_object_iterator_t it;
227 prop_dictionary_t rpdict;
228 int error = 0;
229
230 it = prop_array_iterator(rprocs);
231 while ((rpdict = prop_object_iterator_next(it)) != NULL) {
232 npf_rproc_t *rp;
233
234 if ((rp = npf_mk_singlerproc(npf, rpdict)) == NULL) {
235 NPF_ERR_DEBUG(errdict);
236 error = EINVAL;
237 break;
238 }
239 npf_rprocset_insert(rpset, rp);
240 }
241 prop_object_iterator_release(it);
242 return error;
243 }
244
245 static npf_alg_t *
246 npf_mk_singlealg(npf_t *npf, prop_dictionary_t aldict)
247 {
248 const char *name;
249
250 if (!prop_dictionary_get_cstring_nocopy(aldict, "name", &name))
251 return NULL;
252 return npf_alg_construct(npf, name);
253 }
254
255 static int __noinline
256 npf_mk_algs(npf_t *npf, prop_array_t alglist, prop_dictionary_t errdict)
257 {
258 prop_object_iterator_t it;
259 prop_dictionary_t nadict;
260 int error = 0;
261
262 it = prop_array_iterator(alglist);
263 while ((nadict = prop_object_iterator_next(it)) != NULL) {
264 if (npf_mk_singlealg(npf, nadict) == NULL) {
265 NPF_ERR_DEBUG(errdict);
266 error = EINVAL;
267 break;
268 }
269 }
270 prop_object_iterator_release(it);
271 return error;
272 }
273
274 static int __noinline
275 npf_mk_code(prop_object_t obj, int type, void **code, size_t *csize,
276 prop_dictionary_t errdict)
277 {
278 const void *cptr;
279 size_t clen;
280 void *bc;
281
282 if (type != NPF_CODE_BPF) {
283 return ENOTSUP;
284 }
285 cptr = prop_data_data_nocopy(obj);
286 if (cptr == NULL || (clen = prop_data_size(obj)) == 0) {
287 NPF_ERR_DEBUG(errdict);
288 return EINVAL;
289 }
290 if (!npf_bpf_validate(cptr, clen)) {
291 NPF_ERR_DEBUG(errdict);
292 return EINVAL;
293 }
294 bc = kmem_alloc(clen, KM_SLEEP);
295 memcpy(bc, cptr, clen);
296
297 *code = bc;
298 *csize = clen;
299 return 0;
300 }
301
302 static int __noinline
303 npf_mk_singlerule(npf_t *npf, prop_dictionary_t rldict, npf_rprocset_t *rpset,
304 npf_rule_t **rlret, prop_dictionary_t errdict)
305 {
306 npf_rule_t *rl;
307 const char *rname;
308 prop_object_t obj;
309 int p, error = 0;
310
311 /* Rule - dictionary. */
312 if (prop_object_type(rldict) != PROP_TYPE_DICTIONARY) {
313 NPF_ERR_DEBUG(errdict);
314 return EINVAL;
315 }
316 if ((rl = npf_rule_alloc(npf, rldict)) == NULL) {
317 NPF_ERR_DEBUG(errdict);
318 return EINVAL;
319 }
320
321 /* Assign rule procedure, if any. */
322 if (prop_dictionary_get_cstring_nocopy(rldict, "rproc", &rname)) {
323 npf_rproc_t *rp;
324
325 if (rpset == NULL) {
326 error = EINVAL;
327 goto err;
328 }
329 if ((rp = npf_rprocset_lookup(rpset, rname)) == NULL) {
330 NPF_ERR_DEBUG(errdict);
331 error = EINVAL;
332 goto err;
333 }
334 npf_rule_setrproc(rl, rp);
335 }
336
337 /* Filter code (binary data). */
338 if ((obj = prop_dictionary_get(rldict, "code")) != NULL) {
339 int type;
340 size_t len;
341 void *code;
342
343 prop_dictionary_get_int32(rldict, "code-type", &type);
344 error = npf_mk_code(obj, type, &code, &len, errdict);
345 if (error) {
346 goto err;
347 }
348 npf_rule_setcode(rl, type, code, len);
349 }
350
351 *rlret = rl;
352 return 0;
353 err:
354 npf_rule_free(rl);
355 prop_dictionary_get_int32(rldict, "prio", &p); /* XXX */
356 prop_dictionary_set_int64(errdict, "id", p);
357 return error;
358 }
359
360 static int __noinline
361 npf_mk_rules(npf_t *npf, npf_ruleset_t *rlset, prop_array_t rules,
362 npf_rprocset_t *rpset, prop_dictionary_t errdict)
363 {
364 prop_object_iterator_t it;
365 prop_dictionary_t rldict;
366 int error;
367
368 if (prop_object_type(rules) != PROP_TYPE_ARRAY) {
369 NPF_ERR_DEBUG(errdict);
370 return EINVAL;
371 }
372
373 error = 0;
374 it = prop_array_iterator(rules);
375 while ((rldict = prop_object_iterator_next(it)) != NULL) {
376 npf_rule_t *rl = NULL;
377
378 /* Generate a single rule. */
379 error = npf_mk_singlerule(npf, rldict, rpset, &rl, errdict);
380 if (error) {
381 break;
382 }
383 npf_ruleset_insert(rlset, rl);
384 }
385 prop_object_iterator_release(it);
386 /*
387 * Note: in a case of error, caller will free the ruleset.
388 */
389 return error;
390 }
391
392 static int __noinline
393 npf_mk_natlist(npf_t *npf, npf_ruleset_t *nset, prop_array_t natlist,
394 prop_dictionary_t errdict)
395 {
396 prop_object_iterator_t it;
397 prop_dictionary_t natdict;
398 int error;
399
400 /* NAT policies - array. */
401 if (prop_object_type(natlist) != PROP_TYPE_ARRAY) {
402 NPF_ERR_DEBUG(errdict);
403 return EINVAL;
404 }
405
406 error = 0;
407 it = prop_array_iterator(natlist);
408 while ((natdict = prop_object_iterator_next(it)) != NULL) {
409 npf_rule_t *rl = NULL;
410 npf_natpolicy_t *np;
411
412 /* NAT policy - dictionary. */
413 if (prop_object_type(natdict) != PROP_TYPE_DICTIONARY) {
414 NPF_ERR_DEBUG(errdict);
415 error = EINVAL;
416 break;
417 }
418
419 /*
420 * NAT policies are standard rules, plus additional
421 * information for translation. Make a rule.
422 */
423 error = npf_mk_singlerule(npf, natdict, NULL, &rl, errdict);
424 if (error) {
425 break;
426 }
427 npf_ruleset_insert(nset, rl);
428
429 /* If rule is named, it is a group with NAT policies. */
430 if (prop_dictionary_get(natdict, "name") &&
431 prop_dictionary_get(natdict, "subrules")) {
432 continue;
433 }
434
435 /* Allocate a new NAT policy and assign to the rule. */
436 np = npf_nat_newpolicy(npf, natdict, nset);
437 if (np == NULL) {
438 NPF_ERR_DEBUG(errdict);
439 error = ENOMEM;
440 break;
441 }
442 npf_rule_setnat(rl, np);
443 }
444 prop_object_iterator_release(it);
445 /*
446 * Note: in a case of error, caller will free entire NAT ruleset
447 * with assigned NAT policies.
448 */
449 return error;
450 }
451
452 /*
453 * npf_mk_connlist: import a list of connections and load them.
454 */
455 static int __noinline
456 npf_mk_connlist(npf_t *npf, prop_array_t conlist, npf_ruleset_t *natlist,
457 npf_conndb_t **conndb, prop_dictionary_t errdict)
458 {
459 prop_dictionary_t condict;
460 prop_object_iterator_t it;
461 npf_conndb_t *cd;
462 int error = 0;
463
464 /* Connection list - array */
465 if (prop_object_type(conlist) != PROP_TYPE_ARRAY) {
466 NPF_ERR_DEBUG(errdict);
467 return EINVAL;
468 }
469
470 /* Create a connection database. */
471 cd = npf_conndb_create();
472 it = prop_array_iterator(conlist);
473 while ((condict = prop_object_iterator_next(it)) != NULL) {
474 /* Connection - dictionary. */
475 if (prop_object_type(condict) != PROP_TYPE_DICTIONARY) {
476 NPF_ERR_DEBUG(errdict);
477 error = EINVAL;
478 break;
479 }
480 /* Construct and insert the connection. */
481 error = npf_conn_import(npf, cd, condict, natlist);
482 if (error) {
483 NPF_ERR_DEBUG(errdict);
484 break;
485 }
486 }
487 prop_object_iterator_release(it);
488 if (error) {
489 npf_conn_gc(npf, cd, true, false);
490 npf_conndb_destroy(cd);
491 } else {
492 *conndb = cd;
493 }
494 return error;
495 }
496
497 /*
498 * npfctl_load: store passed data i.e. update settings, create passed
499 * tables, rules and atomically activate all them.
500 */
501 int
502 npfctl_load(npf_t *npf, u_long cmd, void *data)
503 {
504 struct plistref *pref = data;
505 prop_dictionary_t npf_dict, errdict;
506 prop_array_t alglist, natlist, tables, rprocs, rules, conlist;
507 npf_tableset_t *tblset = NULL;
508 npf_rprocset_t *rpset = NULL;
509 npf_ruleset_t *rlset = NULL;
510 npf_ruleset_t *nset = NULL;
511 npf_conndb_t *conndb = NULL;
512 uint32_t ver = 0;
513 size_t nitems;
514 bool flush;
515 int error;
516
517 /* Retrieve the dictionary. */
518 #if !defined(_NPF_TESTING) && !defined(_NPF_STANDALONE)
519 error = prop_dictionary_copyin_ioctl(pref, cmd, &npf_dict);
520 if (error)
521 return error;
522 #else
523 npf_dict = (prop_dictionary_t)pref;
524 #endif
525
526 /* Dictionary for error reporting and version check. */
527 errdict = prop_dictionary_create();
528 prop_dictionary_get_uint32(npf_dict, "version", &ver);
529 if (ver != NPF_VERSION) {
530 error = EPROGMISMATCH;
531 goto fail;
532 }
533
534 /* ALGs. */
535 alglist = prop_dictionary_get(npf_dict, "algs");
536 error = npf_mk_algs(npf, alglist, errdict);
537 if (error) {
538 goto fail;
539 }
540
541 /* NAT policies. */
542 natlist = prop_dictionary_get(npf_dict, "nat");
543 if ((nitems = prop_array_count(natlist)) > NPF_MAX_RULES) {
544 error = E2BIG;
545 goto fail;
546 }
547
548 nset = npf_ruleset_create(nitems);
549 error = npf_mk_natlist(npf, nset, natlist, errdict);
550 if (error) {
551 goto fail;
552 }
553
554 /* Tables. */
555 tables = prop_dictionary_get(npf_dict, "tables");
556 if ((nitems = prop_array_count(tables)) > NPF_MAX_TABLES) {
557 error = E2BIG;
558 goto fail;
559 }
560 tblset = npf_tableset_create(nitems);
561 error = npf_mk_tables(tblset, tables, errdict);
562 if (error) {
563 goto fail;
564 }
565
566 /* Rule procedures. */
567 rprocs = prop_dictionary_get(npf_dict, "rprocs");
568 if ((nitems = prop_array_count(rprocs)) > NPF_MAX_RPROCS) {
569 error = E2BIG;
570 goto fail;
571 }
572 rpset = npf_rprocset_create();
573 error = npf_mk_rprocs(npf, rpset, rprocs, errdict);
574 if (error) {
575 goto fail;
576 }
577
578 /* Rules. */
579 rules = prop_dictionary_get(npf_dict, "rules");
580 if ((nitems = prop_array_count(rules)) > NPF_MAX_RULES) {
581 error = E2BIG;
582 goto fail;
583 }
584
585 rlset = npf_ruleset_create(nitems);
586 error = npf_mk_rules(npf, rlset, rules, rpset, errdict);
587 if (error) {
588 goto fail;
589 }
590
591 /* Connections (if loading any). */
592 if ((conlist = prop_dictionary_get(npf_dict, "conn-list")) != NULL) {
593 error = npf_mk_connlist(npf, conlist, nset, &conndb, errdict);
594 if (error) {
595 goto fail;
596 }
597 }
598
599 flush = false;
600 prop_dictionary_get_bool(npf_dict, "flush", &flush);
601
602 /*
603 * Finally - perform the load.
604 */
605 npf_config_load(npf, rlset, tblset, nset, rpset, conndb, flush);
606
607 /* Done. Since data is consumed now, we shall not destroy it. */
608 tblset = NULL;
609 rpset = NULL;
610 rlset = NULL;
611 nset = NULL;
612 fail:
613 /*
614 * Note: destroy rulesets first, to drop references to the tableset.
615 */
616 if (nset) {
617 npf_ruleset_destroy(nset);
618 }
619 if (rlset) {
620 npf_ruleset_destroy(rlset);
621 }
622 if (rpset) {
623 npf_rprocset_destroy(rpset);
624 }
625 if (tblset) {
626 npf_tableset_destroy(tblset);
627 }
628 prop_object_release(npf_dict);
629
630 /* Error report. */
631 #if !defined(_NPF_TESTING) && !defined(_NPF_STANDALONE)
632 prop_dictionary_set_int32(errdict, "errno", error);
633 prop_dictionary_copyout_ioctl(pref, cmd, errdict);
634 prop_object_release(errdict);
635 error = 0;
636 #endif
637 return error;
638 }
639
640 /*
641 * npfctl_save: export the config dictionary as it was submitted,
642 * including the current snapshot of the connections. Additionally,
643 * indicate whether the ruleset is currently active.
644 */
645 int
646 npfctl_save(npf_t *npf, u_long cmd, void *data)
647 {
648 struct plistref *pref = data;
649 prop_array_t rulelist, natlist, tables, rprocs, conlist;
650 prop_dictionary_t npf_dict = NULL;
651 int error;
652
653 rulelist = prop_array_create();
654 natlist = prop_array_create();
655 tables = prop_array_create();
656 rprocs = prop_array_create();
657 conlist = prop_array_create();
658
659 /*
660 * Serialise the connections and NAT policies.
661 */
662 npf_config_enter(npf);
663 error = npf_conndb_export(npf, conlist);
664 if (error) {
665 goto out;
666 }
667 error = npf_ruleset_export(npf, npf_config_ruleset(npf), rulelist);
668 if (error) {
669 goto out;
670 }
671 error = npf_ruleset_export(npf, npf_config_natset(npf), natlist);
672 if (error) {
673 goto out;
674 }
675 error = npf_tableset_export(npf, npf_config_tableset(npf), tables);
676 if (error) {
677 goto out;
678 }
679 error = npf_rprocset_export(npf_config_rprocs(npf), rprocs);
680 if (error) {
681 goto out;
682 }
683 prop_array_t alglist = npf_alg_export(npf);
684
685 npf_dict = prop_dictionary_create();
686 prop_dictionary_set_uint32(npf_dict, "version", NPF_VERSION);
687 prop_dictionary_set_and_rel(npf_dict, "algs", alglist);
688 prop_dictionary_set_and_rel(npf_dict, "rules", rulelist);
689 prop_dictionary_set_and_rel(npf_dict, "nat", natlist);
690 prop_dictionary_set_and_rel(npf_dict, "tables", tables);
691 prop_dictionary_set_and_rel(npf_dict, "rprocs", rprocs);
692 prop_dictionary_set_and_rel(npf_dict, "conn-list", conlist);
693 #if !defined(_NPF_STANDALONE)
694 prop_dictionary_set_bool(npf_dict, "active", npf_pfil_registered_p());
695 error = prop_dictionary_copyout_ioctl(pref, cmd, npf_dict);
696 #else
697 prop_dictionary_set_bool(npf_dict, "active", true);
698 /* Userspace: just copy the pointer of the dictionary. */
699 CTASSERT(sizeof(prop_dictionary_t) == sizeof(void *));
700 memcpy(data, npf_dict, sizeof(void *));
701 #endif
702 out:
703 npf_config_exit(npf);
704
705 if (!npf_dict) {
706 prop_object_release(rulelist);
707 prop_object_release(natlist);
708 prop_object_release(tables);
709 prop_object_release(rprocs);
710 prop_object_release(conlist);
711 } else {
712 #if !defined(_NPF_STANDALONE)
713 prop_object_release(npf_dict);
714 #endif
715 }
716 return error;
717 }
718
719 /*
720 * npfctl_conn_lookup: lookup a connection in the list of connections
721 */
722 int
723 npfctl_conn_lookup(npf_t *npf, u_long cmd, void *data)
724 {
725 struct plistref *pref = data;
726 prop_dictionary_t conn_data, conn_result;
727 int error;
728
729 #if !defined(_NPF_STANDALONE)
730 error = prop_dictionary_copyin_ioctl(pref, cmd, &conn_data);
731 if (error) {
732 return error;
733 }
734 #else
735 conn_data = (prop_dictionary_t)pref;
736 #endif
737 error = npf_conn_find(npf, conn_data, &conn_result);
738 if (error) {
739 goto out;
740 }
741 #if !defined(_NPF_STANDALONE)
742 prop_dictionary_copyout_ioctl(pref, cmd, conn_result);
743 prop_object_release(conn_result);
744 #else
745 CTASSERT(sizeof(prop_dictionary_t) == sizeof(void *));
746 memcpy(data, conn_result, sizeof(void *));
747 #endif
748 out:
749 prop_object_release(conn_data);
750 return error;
751 }
752
753 /*
754 * npfctl_rule: add or remove dynamic rules in the specified ruleset.
755 */
756 int
757 npfctl_rule(npf_t *npf, u_long cmd, void *data)
758 {
759 struct plistref *pref = data;
760 prop_dictionary_t npf_rule, retdict = NULL;
761 npf_ruleset_t *rlset;
762 npf_rule_t *rl = NULL;
763 const char *ruleset_name;
764 uint32_t rcmd = 0;
765 int error = 0;
766
767 #if !defined(_NPF_STANDALONE)
768 error = prop_dictionary_copyin_ioctl(pref, cmd, &npf_rule);
769 if (error) {
770 return error;
771 }
772 #else
773 npf_rule = (prop_dictionary_t)pref;
774 #endif
775 prop_dictionary_get_uint32(npf_rule, "command", &rcmd);
776 if (!prop_dictionary_get_cstring_nocopy(npf_rule,
777 "ruleset-name", &ruleset_name)) {
778 error = EINVAL;
779 goto out;
780 }
781
782 if (rcmd == NPF_CMD_RULE_ADD) {
783 retdict = prop_dictionary_create();
784 if (npf_mk_singlerule(npf, npf_rule, NULL, &rl, retdict) != 0) {
785 error = EINVAL;
786 goto out;
787 }
788 }
789
790 npf_config_enter(npf);
791 rlset = npf_config_ruleset(npf);
792
793 switch (rcmd) {
794 case NPF_CMD_RULE_ADD: {
795 if ((error = npf_ruleset_add(rlset, ruleset_name, rl)) == 0) {
796 /* Success. */
797 uint64_t id = npf_rule_getid(rl);
798 prop_dictionary_set_uint64(retdict, "id", id);
799 rl = NULL;
800 }
801 break;
802 }
803 case NPF_CMD_RULE_REMOVE: {
804 uint64_t id;
805
806 if (!prop_dictionary_get_uint64(npf_rule, "id", &id)) {
807 error = EINVAL;
808 break;
809 }
810 error = npf_ruleset_remove(rlset, ruleset_name, id);
811 break;
812 }
813 case NPF_CMD_RULE_REMKEY: {
814 prop_object_t obj = prop_dictionary_get(npf_rule, "key");
815 const void *key = prop_data_data_nocopy(obj);
816 size_t len = prop_data_size(obj);
817
818 if (len == 0 || len > NPF_RULE_MAXKEYLEN) {
819 error = EINVAL;
820 break;
821 }
822 error = npf_ruleset_remkey(rlset, ruleset_name, key, len);
823 break;
824 }
825 case NPF_CMD_RULE_LIST: {
826 retdict = npf_ruleset_list(npf, rlset, ruleset_name);
827 if (!retdict) {
828 error = ESRCH;
829 }
830 break;
831 }
832 case NPF_CMD_RULE_FLUSH: {
833 error = npf_ruleset_flush(rlset, ruleset_name);
834 break;
835 }
836 default:
837 error = EINVAL;
838 break;
839 }
840
841 /* Destroy any removed rules. */
842 if (!error && rcmd != NPF_CMD_RULE_ADD && rcmd != NPF_CMD_RULE_LIST) {
843 npf_config_sync(npf);
844 npf_ruleset_gc(rlset);
845 }
846 npf_config_exit(npf);
847
848 if (rl) {
849 KASSERT(error);
850 npf_rule_free(rl);
851 }
852 out:
853 if (retdict) {
854 prop_object_release(npf_rule);
855 #if !defined(_NPF_STANDALONE)
856 prop_dictionary_copyout_ioctl(pref, cmd, retdict);
857 prop_object_release(retdict);
858 #else
859 CTASSERT(sizeof(prop_dictionary_t) == sizeof(void *));
860 memcpy(data, npf_rule, sizeof(void *));
861 #endif
862 }
863 return error;
864 }
865
866 /*
867 * npfctl_table: add, remove or query entries in the specified table.
868 *
869 * For maximum performance, interface is avoiding proplib(3)'s overhead.
870 */
871 int
872 npfctl_table(npf_t *npf, void *data)
873 {
874 const npf_ioctl_table_t *nct = data;
875 char tname[NPF_TABLE_MAXNAMELEN];
876 npf_tableset_t *ts;
877 npf_table_t *t;
878 int s, error;
879
880 error = copyinstr(nct->nct_name, tname, sizeof(tname), NULL);
881 if (error) {
882 return error;
883 }
884
885 s = npf_config_read_enter(); /* XXX */
886 ts = npf_config_tableset(npf);
887 if ((t = npf_tableset_getbyname(ts, tname)) == NULL) {
888 npf_config_read_exit(s);
889 return EINVAL;
890 }
891
892 switch (nct->nct_cmd) {
893 case NPF_CMD_TABLE_LOOKUP:
894 error = npf_table_lookup(t, nct->nct_data.ent.alen,
895 &nct->nct_data.ent.addr);
896 break;
897 case NPF_CMD_TABLE_ADD:
898 error = npf_table_insert(t, nct->nct_data.ent.alen,
899 &nct->nct_data.ent.addr, nct->nct_data.ent.mask);
900 break;
901 case NPF_CMD_TABLE_REMOVE:
902 error = npf_table_remove(t, nct->nct_data.ent.alen,
903 &nct->nct_data.ent.addr, nct->nct_data.ent.mask);
904 break;
905 case NPF_CMD_TABLE_LIST:
906 error = npf_table_list(t, nct->nct_data.buf.buf,
907 nct->nct_data.buf.len);
908 break;
909 case NPF_CMD_TABLE_FLUSH:
910 error = npf_table_flush(t);
911 break;
912 default:
913 error = EINVAL;
914 break;
915 }
916 npf_config_read_exit(s);
917
918 return error;
919 }
920