npf_ctl.c revision 1.57 1 /*-
2 * Copyright (c) 2009-2019 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.57 2019/08/25 13:21:03 rmind 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 error = nvlist_copyin(data, nvl, NPF_IOCTL_DATA_LIMIT);
85 } else {
86 *nvl = (nvlist_t *)data;
87 }
88 return error;
89 }
90
91 static int
92 npf_nvlist_copyout(npf_t *npf, void *data, nvlist_t *nvl)
93 {
94 int error = 0;
95
96 if (npf->mbufops == NULL) {
97 error = nvlist_copyout(data, nvl);
98 }
99 nvlist_destroy(nvl);
100 return error;
101 }
102
103 static int __noinline
104 npf_mk_params(npf_t *npf, nvlist_t *npf_dict, nvlist_t *errdict, bool set)
105 {
106 const nvlist_t *params;
107 int type, error, val;
108 const char *name;
109 void *cookie;
110
111 params = dnvlist_get_nvlist(npf_dict, "params", NULL);
112 if (params == NULL) {
113 return 0;
114 }
115 cookie = NULL;
116 while ((name = nvlist_next(params, &type, &cookie)) != NULL) {
117 if (type != NV_TYPE_NUMBER) {
118 NPF_ERR_DEBUG(errdict);
119 return EINVAL;
120 }
121 val = (int)nvlist_get_number(params, name);
122 if (set) {
123 /* Actually set the parameter. */
124 error = npfk_param_set(npf, name, val);
125 KASSERT(error == 0);
126 continue;
127 }
128
129 /* Validate the parameter and its value. */
130 error = npf_param_check(npf, name, val);
131 if (__predict_true(error == 0)) {
132 continue;
133 }
134 if (error == ENOENT) {
135 nvlist_add_stringf(errdict, "error-msg",
136 "invalid parameter `%s`", name);
137 }
138 if (error == EINVAL) {
139 nvlist_add_stringf(errdict, "error-msg",
140 "invalid parameter `%s` value %d", name, val);
141 }
142 return error;
143 }
144 return 0;
145 }
146
147 static int __noinline
148 npf_mk_table_entries(npf_table_t *t, const nvlist_t *table, nvlist_t *errdict)
149 {
150 const nvlist_t * const *entries;
151 size_t nitems;
152 int error = 0;
153
154 if (!nvlist_exists_nvlist_array(table, "entries")) {
155 return 0;
156 }
157 entries = nvlist_get_nvlist_array(table, "entries", &nitems);
158 for (unsigned i = 0; i < nitems; i++) {
159 const nvlist_t *entry = entries[i];
160 const npf_addr_t *addr;
161 npf_netmask_t mask;
162 size_t alen;
163
164 /* Get address and mask. Add a table entry. */
165 addr = dnvlist_get_binary(entry, "addr", &alen, NULL, 0);
166 mask = dnvlist_get_number(entry, "mask", NPF_NO_NETMASK);
167 if (addr == NULL || alen == 0) {
168 NPF_ERR_DEBUG(errdict);
169 error = EINVAL;
170 break;
171 }
172 error = npf_table_insert(t, alen, addr, mask);
173 if (error) {
174 NPF_ERR_DEBUG(errdict);
175 break;
176 }
177 }
178 return error;
179 }
180
181 /*
182 * npf_mk_table: create a table from provided nvlist.
183 */
184 static int __noinline
185 npf_mk_table(npf_t *npf, const nvlist_t *tbl_dict, nvlist_t *errdict,
186 npf_tableset_t *tblset, npf_table_t **tblp, bool replacing)
187 {
188 npf_table_t *t;
189 const char *name;
190 const void *blob;
191 uint64_t tid;
192 size_t size;
193 int type;
194 int error = 0;
195
196 KASSERT(tblp != NULL);
197
198 /* Table name, ID and type. Validate them. */
199 name = dnvlist_get_string(tbl_dict, "name", NULL);
200 if (!name) {
201 NPF_ERR_DEBUG(errdict);
202 error = EINVAL;
203 goto out;
204 }
205 tid = dnvlist_get_number(tbl_dict, "id", UINT64_MAX);
206 type = dnvlist_get_number(tbl_dict, "type", UINT64_MAX);
207 error = npf_table_check(tblset, name, tid, type, replacing);
208 if (error) {
209 NPF_ERR_DEBUG(errdict);
210 goto out;
211 }
212
213 /* Get the entries or binary data. */
214 blob = dnvlist_get_binary(tbl_dict, "data", &size, NULL, 0);
215 if (type == NPF_TABLE_CONST && (blob == NULL || size == 0)) {
216 NPF_ERR_DEBUG(errdict);
217 error = EINVAL;
218 goto out;
219 }
220
221 t = npf_table_create(name, (u_int)tid, type, blob, size);
222 if (t == NULL) {
223 NPF_ERR_DEBUG(errdict);
224 error = ENOMEM;
225 goto out;
226 }
227
228 if ((error = npf_mk_table_entries(t, tbl_dict, errdict)) != 0) {
229 npf_table_destroy(t);
230 goto out;
231 }
232
233 *tblp = t;
234 out:
235 return error;
236 }
237
238 static int __noinline
239 npf_mk_tables(npf_t *npf, nvlist_t *npf_dict, nvlist_t *errdict,
240 npf_config_t *nc)
241 {
242 const nvlist_t * const *tables;
243 npf_tableset_t *tblset;
244 size_t nitems;
245 int error = 0;
246
247 if (nvlist_exists_nvlist_array(npf_dict, "tables")) {
248 tables = nvlist_get_nvlist_array(npf_dict, "tables", &nitems);
249 if (nitems > NPF_MAX_TABLES) {
250 NPF_ERR_DEBUG(errdict);
251 return E2BIG;
252 }
253 } else {
254 tables = NULL;
255 nitems = 0;
256 }
257 tblset = npf_tableset_create(nitems);
258 for (unsigned i = 0; i < nitems; i++) {
259 const nvlist_t *table = tables[i];
260 npf_table_t *t;
261
262 error = npf_mk_table(npf, table, errdict, tblset, &t, 0);
263 if (error) {
264 break;
265 }
266
267 error = npf_tableset_insert(tblset, t);
268 KASSERT(error == 0);
269 }
270 nc->tableset = tblset;
271 return error;
272 }
273
274 static npf_rproc_t *
275 npf_mk_singlerproc(npf_t *npf, const nvlist_t *rproc, nvlist_t *errdict)
276 {
277 const nvlist_t * const *extcalls;
278 size_t nitems;
279 npf_rproc_t *rp;
280
281 if (!nvlist_exists_nvlist_array(rproc, "extcalls")) {
282 NPF_ERR_DEBUG(errdict);
283 return NULL;
284 }
285 rp = npf_rproc_create(rproc);
286 if (rp == NULL) {
287 NPF_ERR_DEBUG(errdict);
288 return NULL;
289 }
290 extcalls = nvlist_get_nvlist_array(rproc, "extcalls", &nitems);
291 for (unsigned i = 0; i < nitems; i++) {
292 const nvlist_t *extcall = extcalls[i];
293 const char *name;
294
295 name = dnvlist_get_string(extcall, "name", NULL);
296 if (!name || npf_ext_construct(npf, name, rp, extcall)) {
297 NPF_ERR_DEBUG(errdict);
298 npf_rproc_release(rp);
299 rp = NULL;
300 break;
301 }
302 }
303 return rp;
304 }
305
306 static int __noinline
307 npf_mk_rprocs(npf_t *npf, nvlist_t *npf_dict, nvlist_t *errdict,
308 npf_config_t *nc)
309 {
310 const nvlist_t * const *rprocs;
311 npf_rprocset_t *rpset;
312 size_t nitems;
313 int error = 0;
314
315 if (nvlist_exists_nvlist_array(npf_dict, "rprocs")) {
316 rprocs = nvlist_get_nvlist_array(npf_dict, "rprocs", &nitems);
317 if (nitems > NPF_MAX_RPROCS) {
318 NPF_ERR_DEBUG(errdict);
319 return E2BIG;
320 }
321 } else {
322 rprocs = NULL;
323 nitems = 0;
324 }
325 rpset = npf_rprocset_create();
326 for (unsigned i = 0; i < nitems; i++) {
327 const nvlist_t *rproc = rprocs[i];
328 npf_rproc_t *rp;
329
330 if ((rp = npf_mk_singlerproc(npf, rproc, errdict)) == NULL) {
331 error = EINVAL;
332 break;
333 }
334 npf_rprocset_insert(rpset, rp);
335 }
336 nc->rule_procs = rpset;
337 return error;
338 }
339
340 static int __noinline
341 npf_mk_algs(npf_t *npf, nvlist_t *npf_dict, nvlist_t *errdict)
342 {
343 const nvlist_t * const *algs;
344 size_t nitems;
345
346 if (nvlist_exists_nvlist_array(npf_dict, "algs")) {
347 algs = nvlist_get_nvlist_array(npf_dict, "algs", &nitems);
348 } else {
349 algs = NULL;
350 nitems = 0;
351 }
352 for (unsigned i = 0; i < nitems; i++) {
353 const nvlist_t *alg = algs[i];
354 const char *name;
355
356 name = dnvlist_get_string(alg, "name", NULL);
357 if (!name) {
358 NPF_ERR_DEBUG(errdict);
359 return EINVAL;
360 }
361 if (!npf_alg_construct(npf, name)) {
362 NPF_ERR_DEBUG(errdict);
363 return EINVAL;
364 }
365 }
366 return 0;
367 }
368
369 static int __noinline
370 npf_mk_singlerule(npf_t *npf, const nvlist_t *rule, npf_rprocset_t *rpset,
371 npf_rule_t **rlret, nvlist_t *errdict)
372 {
373 npf_rule_t *rl;
374 const char *rname;
375 const void *code;
376 size_t clen;
377 int error = 0;
378
379 if ((rl = npf_rule_alloc(npf, rule)) == NULL) {
380 NPF_ERR_DEBUG(errdict);
381 return EINVAL;
382 }
383
384 /* Assign the rule procedure, if any. */
385 if ((rname = dnvlist_get_string(rule, "rproc", NULL)) != NULL) {
386 npf_rproc_t *rp;
387
388 if (rpset == NULL) {
389 NPF_ERR_DEBUG(errdict);
390 error = EINVAL;
391 goto err;
392 }
393 if ((rp = npf_rprocset_lookup(rpset, rname)) == NULL) {
394 NPF_ERR_DEBUG(errdict);
395 error = EINVAL;
396 goto err;
397 }
398 npf_rule_setrproc(rl, rp);
399 }
400
401 /* Filter byte-code (binary data). */
402 code = dnvlist_get_binary(rule, "code", &clen, NULL, 0);
403 if (code) {
404 void *bc;
405 int type;
406
407 type = dnvlist_get_number(rule, "code-type", UINT64_MAX);
408 if (type != NPF_CODE_BPF) {
409 NPF_ERR_DEBUG(errdict);
410 error = ENOTSUP;
411 goto err;
412 }
413 if (clen == 0) {
414 NPF_ERR_DEBUG(errdict);
415 error = EINVAL;
416 goto err;
417 }
418 if (!npf_bpf_validate(code, clen)) {
419 NPF_ERR_DEBUG(errdict);
420 error = EINVAL;
421 goto err;
422 }
423 bc = kmem_alloc(clen, KM_SLEEP);
424 memcpy(bc, code, clen); // XXX: use nvlist_take
425 npf_rule_setcode(rl, type, bc, clen);
426 }
427
428 *rlret = rl;
429 return 0;
430 err:
431 nvlist_add_number(errdict, "id", dnvlist_get_number(rule, "prio", 0));
432 npf_rule_free(rl);
433 return error;
434 }
435
436 static int __noinline
437 npf_mk_rules(npf_t *npf, nvlist_t *npf_dict, nvlist_t *errdict,
438 npf_config_t *nc)
439 {
440 const nvlist_t * const *rules;
441 npf_ruleset_t *rlset;
442 size_t nitems;
443 int error = 0;
444
445 if (nvlist_exists_nvlist_array(npf_dict, "rules")) {
446 rules = nvlist_get_nvlist_array(npf_dict, "rules", &nitems);
447 if (nitems > NPF_MAX_RULES) {
448 NPF_ERR_DEBUG(errdict);
449 return E2BIG;
450 }
451 } else {
452 rules = NULL;
453 nitems = 0;
454 }
455 rlset = npf_ruleset_create(nitems);
456 for (unsigned i = 0; i < nitems; i++) {
457 const nvlist_t *rule = rules[i];
458 npf_rule_t *rl = NULL;
459 const char *name;
460
461 error = npf_mk_singlerule(npf, rule, nc->rule_procs, &rl,
462 errdict);
463 if (error) {
464 break;
465 }
466 name = dnvlist_get_string(rule, "name", NULL);
467 if (name && npf_ruleset_lookup(rlset, name)) {
468 NPF_ERR_DEBUG(errdict);
469 npf_rule_free(rl);
470 error = EEXIST;
471 break;
472 }
473 npf_ruleset_insert(rlset, rl);
474 }
475 nc->ruleset = rlset;
476 return error;
477 }
478
479 static int __noinline
480 npf_mk_singlenat(npf_t *npf, const nvlist_t *nat, npf_ruleset_t *ntset,
481 npf_tableset_t *tblset, nvlist_t *errdict, npf_rule_t **rlp)
482 {
483 npf_rule_t *rl = NULL;
484 npf_natpolicy_t *np;
485 int error;
486
487 /*
488 * NAT rules are standard rules, plus the translation policy.
489 * We first construct the rule structure.
490 */
491 error = npf_mk_singlerule(npf, nat, NULL, &rl, errdict);
492 if (error) {
493 return error;
494 }
495 KASSERT(rl != NULL);
496 *rlp = rl;
497
498 /* If rule is named, it is a group with NAT policies. */
499 if (dnvlist_get_string(nat, "name", NULL)) {
500 return 0;
501 }
502
503 /* Check the table ID. */
504 if (nvlist_exists_number(nat, "nat-table-id")) {
505 unsigned tid = nvlist_get_number(nat, "nat-table-id");
506
507 if (!npf_tableset_getbyid(tblset, tid)) {
508 NPF_ERR_DEBUG(errdict);
509 error = EINVAL;
510 goto out;
511 }
512 }
513
514 /* Allocate a new NAT policy and assign it to the rule. */
515 np = npf_nat_newpolicy(npf, nat, ntset);
516 if (np == NULL) {
517 NPF_ERR_DEBUG(errdict);
518 error = ENOMEM;
519 goto out;
520 }
521 npf_rule_setnat(rl, np);
522 out:
523 if (error) {
524 npf_rule_free(rl);
525 }
526 return error;
527 }
528
529 static int __noinline
530 npf_mk_natlist(npf_t *npf, nvlist_t *npf_dict, nvlist_t *errdict,
531 npf_config_t *nc)
532 {
533 const nvlist_t * const *nat_rules;
534 npf_ruleset_t *ntset;
535 size_t nitems;
536 int error = 0;
537
538 /*
539 * NAT policies must be an array, but enforce a limit.
540 */
541 if (nvlist_exists_nvlist_array(npf_dict, "nat")) {
542 nat_rules = nvlist_get_nvlist_array(npf_dict, "nat", &nitems);
543 if (nitems > NPF_MAX_RULES) {
544 NPF_ERR_DEBUG(errdict);
545 return E2BIG;
546 }
547 } else {
548 nat_rules = NULL;
549 nitems = 0;
550 }
551 ntset = npf_ruleset_create(nitems);
552 for (unsigned i = 0; i < nitems; i++) {
553 const nvlist_t *nat = nat_rules[i];
554 npf_rule_t *rl = NULL;
555
556 error = npf_mk_singlenat(npf, nat, ntset, nc->tableset,
557 errdict, &rl);
558 if (error) {
559 break;
560 }
561 npf_ruleset_insert(ntset, rl);
562 }
563 nc->nat_ruleset = ntset;
564 return error;
565 }
566
567 /*
568 * npf_mk_connlist: import a list of connections and load them.
569 */
570 static int __noinline
571 npf_mk_connlist(npf_t *npf, nvlist_t *npf_dict, nvlist_t *errdict,
572 npf_config_t *nc, npf_conndb_t **conndb)
573 {
574 const nvlist_t * const *conns;
575 npf_conndb_t *cd;
576 size_t nitems;
577 int error = 0;
578
579 if (!nvlist_exists_nvlist_array(npf_dict, "conn-list")) {
580 *conndb = NULL;
581 return 0;
582 }
583 cd = npf_conndb_create();
584 conns = nvlist_get_nvlist_array(npf_dict, "conn-list", &nitems);
585 for (unsigned i = 0; i < nitems; i++) {
586 const nvlist_t *conn = conns[i];
587
588 /* Construct and insert the connection. */
589 error = npf_conn_import(npf, cd, conn, nc->nat_ruleset);
590 if (error) {
591 NPF_ERR_DEBUG(errdict);
592 break;
593 }
594 }
595 if (error) {
596 npf_conndb_gc(npf, cd, true, false);
597 npf_conndb_destroy(cd);
598 } else {
599 *conndb = cd;
600 }
601 return error;
602 }
603
604 /*
605 * npfctl_load_nvlist: store passed data i.e. the update settings, create
606 * the passed tables, rules, etc and atomically activate all them.
607 */
608 static int
609 npfctl_load_nvlist(npf_t *npf, nvlist_t *npf_dict, nvlist_t *errdict)
610 {
611 npf_config_t *nc;
612 npf_conndb_t *conndb = NULL;
613 uint64_t ver;
614 bool flush;
615 int error;
616
617 nc = npf_config_create();
618 ver = dnvlist_get_number(npf_dict, "version", UINT64_MAX);
619 if (ver != NPF_VERSION) {
620 error = EPROGMISMATCH;
621 goto fail;
622 }
623 error = npf_mk_params(npf, npf_dict, errdict, false /* validate */);
624 if (error) {
625 goto fail;
626 }
627 error = npf_mk_algs(npf, npf_dict, errdict);
628 if (error) {
629 goto fail;
630 }
631 error = npf_mk_tables(npf, npf_dict, errdict, nc);
632 if (error) {
633 goto fail;
634 }
635 error = npf_mk_rprocs(npf, npf_dict, errdict, nc);
636 if (error) {
637 goto fail;
638 }
639 error = npf_mk_natlist(npf, npf_dict, errdict, nc);
640 if (error) {
641 goto fail;
642 }
643 error = npf_mk_rules(npf, npf_dict, errdict, nc);
644 if (error) {
645 goto fail;
646 }
647 error = npf_mk_connlist(npf, npf_dict, errdict, nc, &conndb);
648 if (error) {
649 goto fail;
650 }
651
652 flush = dnvlist_get_bool(npf_dict, "flush", false);
653 nc->default_pass = flush;
654
655 /*
656 * Finally - perform the load.
657 */
658 npf_config_load(npf, nc, conndb, flush);
659 npf_mk_params(npf, npf_dict, errdict, true /* set the params */);
660
661 /* Done. Since data is consumed now, we shall not destroy it. */
662 nc = NULL;
663 fail:
664 if (nc) {
665 npf_config_destroy(nc);
666 }
667 nvlist_destroy(npf_dict);
668 return error;
669 }
670
671 int
672 npfctl_load(npf_t *npf, u_long cmd, void *data)
673 {
674 nvlist_t *request, *response;
675 int error;
676
677 /*
678 * Retrieve the configuration and check the version.
679 * Construct a response with error reporting.
680 */
681 error = npf_nvlist_copyin(npf, data, &request);
682 if (error) {
683 return error;
684 }
685 response = nvlist_create(0);
686 error = npfctl_load_nvlist(npf, request, response);
687 nvlist_add_number(response, "errno", error);
688 return npf_nvlist_copyout(npf, data, response);
689 }
690
691 /*
692 * npfctl_save: export the config dictionary as it was submitted,
693 * including the current snapshot of the connections. Additionally,
694 * indicate whether the ruleset is currently active.
695 */
696 int
697 npfctl_save(npf_t *npf, u_long cmd, void *data)
698 {
699 npf_config_t *nc;
700 nvlist_t *npf_dict;
701 int error;
702
703 npf_dict = nvlist_create(0);
704 nvlist_add_number(npf_dict, "version", NPF_VERSION);
705
706 /*
707 * Serialise the whole NPF config, including connections.
708 */
709 nc = npf_config_enter(npf);
710 error = npf_conndb_export(npf, npf_dict);
711 if (error) {
712 goto out;
713 }
714 error = npf_ruleset_export(npf, nc->ruleset, "rules", npf_dict);
715 if (error) {
716 goto out;
717 }
718 error = npf_ruleset_export(npf, nc->nat_ruleset, "nat", npf_dict);
719 if (error) {
720 goto out;
721 }
722 error = npf_tableset_export(npf, nc->tableset, npf_dict);
723 if (error) {
724 goto out;
725 }
726 error = npf_rprocset_export(nc->rule_procs, npf_dict);
727 if (error) {
728 goto out;
729 }
730 error = npf_alg_export(npf, npf_dict);
731 if (error) {
732 goto out;
733 }
734 nvlist_add_bool(npf_dict, "active", npf_pfil_registered_p());
735 error = npf_nvlist_copyout(npf, data, npf_dict);
736 npf_dict = NULL;
737 out:
738 npf_config_exit(npf);
739 if (npf_dict) {
740 nvlist_destroy(npf_dict);
741 }
742 return error;
743 }
744
745 /*
746 * npfctl_table_replace_nvlist: atomically replace a table's contents
747 * with the passed table data.
748 */
749 static int __noinline
750 npfctl_table_replace_nvlist(npf_t *npf, nvlist_t *npf_dict, nvlist_t *errdict)
751 {
752 npf_table_t *tbl, *gc_tbl = NULL;
753 npf_config_t *nc;
754 int error = 0;
755
756 nc = npf_config_enter(npf);
757 error = npf_mk_table(npf, npf_dict, errdict, nc->tableset, &tbl, true);
758 if (error) {
759 goto err;
760 }
761 gc_tbl = npf_tableset_swap(nc->tableset, tbl);
762 if (gc_tbl == NULL) {
763 error = EINVAL;
764 gc_tbl = tbl;
765 goto err;
766 }
767 npf_config_sync(npf);
768 err:
769 npf_config_exit(npf);
770 if (gc_tbl) {
771 npf_table_destroy(gc_tbl);
772 }
773 return error;
774 }
775
776 int
777 npfctl_table_replace(npf_t *npf, u_long cmd, void *data)
778 {
779 nvlist_t *request, *response;
780 int error;
781
782 /*
783 * Retrieve the configuration and check the version.
784 * Construct a response with error reporting.
785 */
786 error = npf_nvlist_copyin(npf, data, &request);
787 if (error) {
788 return error;
789 }
790 response = nvlist_create(0);
791 error = npfctl_table_replace_nvlist(npf, request, response);
792 nvlist_add_number(response, "errno", error);
793 error = npf_nvlist_copyout(npf, data, response);
794 nvlist_destroy(request);
795 return error;
796 }
797
798 /*
799 * npfctl_conn_lookup: lookup a connection in the list of connections
800 */
801 int
802 npfctl_conn_lookup(npf_t *npf, u_long cmd, void *data)
803 {
804 nvlist_t *conn_data, *conn_result;
805 int error;
806
807 error = npf_nvlist_copyin(npf, data, &conn_data);
808 if (error) {
809 return error;
810 }
811 error = npf_conn_find(npf, conn_data, &conn_result);
812 if (error) {
813 goto out;
814 }
815 error = npf_nvlist_copyout(npf, data, conn_result);
816 out:
817 nvlist_destroy(conn_data);
818 return error;
819 }
820
821 /*
822 * npfctl_rule: add or remove dynamic rules in the specified ruleset.
823 */
824 int
825 npfctl_rule(npf_t *npf, u_long cmd, void *data)
826 {
827 nvlist_t *npf_rule, *retdict = NULL;
828 npf_ruleset_t *rlset;
829 npf_rule_t *rl = NULL;
830 const char *ruleset_name;
831 npf_config_t *nc;
832 uint32_t rcmd;
833 int error = 0;
834 bool natset;
835
836 error = npf_nvlist_copyin(npf, data, &npf_rule);
837 if (error) {
838 return error;
839 }
840 rcmd = dnvlist_get_number(npf_rule, "command", 0);
841 natset = dnvlist_get_bool(npf_rule, "nat-rule", false);
842 ruleset_name = dnvlist_get_string(npf_rule, "ruleset-name", NULL);
843 if (!ruleset_name) {
844 error = EINVAL;
845 goto out;
846 }
847
848 nc = npf_config_enter(npf);
849 rlset = natset ? nc->nat_ruleset : nc->ruleset;
850 switch (rcmd) {
851 case NPF_CMD_RULE_ADD: {
852 retdict = nvlist_create(0);
853 if (natset) {
854 /*
855 * Translation rule.
856 */
857 error = npf_mk_singlenat(npf, npf_rule, rlset,
858 nc->tableset, retdict, &rl);
859 } else {
860 /*
861 * Standard rule.
862 */
863 error = npf_mk_singlerule(npf, npf_rule, NULL,
864 &rl, retdict);
865 }
866 if (error) {
867 npf_config_exit(npf);
868 goto out;
869 }
870 if ((error = npf_ruleset_add(rlset, ruleset_name, rl)) == 0) {
871 /* Success. */
872 uint64_t id = npf_rule_getid(rl);
873 nvlist_add_number(retdict, "id", id);
874 rl = NULL;
875 }
876 break;
877 }
878 case NPF_CMD_RULE_REMOVE: {
879 uint64_t id = dnvlist_get_number(npf_rule, "id", UINT64_MAX);
880 error = npf_ruleset_remove(rlset, ruleset_name, id);
881 break;
882 }
883 case NPF_CMD_RULE_REMKEY: {
884 const void *key;
885 size_t len;
886
887 key = dnvlist_get_binary(npf_rule, "key", &len, NULL, 0);
888 if (len == 0 || len > NPF_RULE_MAXKEYLEN) {
889 error = EINVAL;
890 break;
891 }
892 error = npf_ruleset_remkey(rlset, ruleset_name, key, len);
893 break;
894 }
895 case NPF_CMD_RULE_LIST: {
896 retdict = npf_ruleset_list(npf, rlset, ruleset_name);
897 if (!retdict) {
898 error = ESRCH;
899 }
900 break;
901 }
902 case NPF_CMD_RULE_FLUSH: {
903 error = npf_ruleset_flush(rlset, ruleset_name);
904 break;
905 }
906 default:
907 error = EINVAL;
908 break;
909 }
910
911 /* Destroy any removed rules. */
912 if (!error && rcmd != NPF_CMD_RULE_ADD && rcmd != NPF_CMD_RULE_LIST) {
913 npf_config_sync(npf);
914 npf_ruleset_gc(rlset);
915 }
916 npf_config_exit(npf);
917
918 if (rl) {
919 KASSERT(error);
920 npf_rule_free(rl);
921 }
922 out:
923 if (retdict && npf_nvlist_copyout(npf, data, retdict) != 0) {
924 error = EFAULT; // copyout failure
925 }
926 nvlist_destroy(npf_rule);
927 return error;
928 }
929
930 /*
931 * npfctl_table: add, remove or query entries in the specified table.
932 *
933 * For maximum performance, the interface is using plain structures.
934 */
935 int
936 npfctl_table(npf_t *npf, void *data)
937 {
938 const npf_ioctl_table_t *nct = data;
939 char tname[NPF_TABLE_MAXNAMELEN];
940 npf_config_t *nc;
941 npf_table_t *t;
942 int error;
943
944 error = copyinstr(nct->nct_name, tname, sizeof(tname), NULL);
945 if (error) {
946 return error;
947 }
948
949 nc = npf_config_enter(npf);
950 if ((t = npf_tableset_getbyname(nc->tableset, tname)) == NULL) {
951 npf_config_exit(npf);
952 return EINVAL;
953 }
954
955 switch (nct->nct_cmd) {
956 case NPF_CMD_TABLE_LOOKUP:
957 error = npf_table_lookup(t, nct->nct_data.ent.alen,
958 &nct->nct_data.ent.addr);
959 break;
960 case NPF_CMD_TABLE_ADD:
961 error = npf_table_insert(t, nct->nct_data.ent.alen,
962 &nct->nct_data.ent.addr, nct->nct_data.ent.mask);
963 break;
964 case NPF_CMD_TABLE_REMOVE:
965 error = npf_table_remove(t, nct->nct_data.ent.alen,
966 &nct->nct_data.ent.addr, nct->nct_data.ent.mask);
967 break;
968 case NPF_CMD_TABLE_LIST:
969 error = npf_table_list(t, nct->nct_data.buf.buf,
970 nct->nct_data.buf.len);
971 break;
972 case NPF_CMD_TABLE_FLUSH:
973 error = npf_table_flush(t);
974 break;
975 default:
976 error = EINVAL;
977 break;
978 }
979 npf_table_gc(npf, t);
980 npf_config_exit(npf);
981
982 return error;
983 }
984