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