npf_ctl.c revision 1.17 1 /* $NetBSD: npf_ctl.c,v 1.17 2012/08/15 18:44:56 rmind Exp $ */
2
3 /*-
4 * Copyright (c) 2009-2012 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 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: npf_ctl.c,v 1.17 2012/08/15 18:44:56 rmind Exp $");
41
42 #include <sys/param.h>
43 #include <sys/conf.h>
44
45 #include <prop/proplib.h>
46
47 #include "npf_ncode.h"
48 #include "npf_impl.h"
49
50 #if defined(DEBUG) || defined(DIAGNOSTIC)
51 #define NPF_ERR_DEBUG(e) \
52 prop_dictionary_set_cstring_nocopy((e), "source-file", __FILE__); \
53 prop_dictionary_set_uint32((e), "source-line", __LINE__);
54 #else
55 #define NPF_ERR_DEBUG(e)
56 #endif
57
58 /*
59 * npfctl_switch: enable or disable packet inspection.
60 */
61 int
62 npfctl_switch(void *data)
63 {
64 const bool onoff = *(int *)data ? true : false;
65 int error;
66
67 if (onoff) {
68 /* Enable: add pfil hooks. */
69 error = npf_pfil_register();
70 } else {
71 /* Disable: remove pfil hooks. */
72 npf_pfil_unregister();
73 error = 0;
74 }
75 return error;
76 }
77
78 static int __noinline
79 npf_mk_tables(npf_tableset_t *tblset, prop_array_t tables,
80 prop_dictionary_t errdict)
81 {
82 prop_object_iterator_t it;
83 prop_dictionary_t tbldict;
84 int error = 0;
85
86 /* Tables - array. */
87 if (prop_object_type(tables) != PROP_TYPE_ARRAY) {
88 NPF_ERR_DEBUG(errdict);
89 return EINVAL;
90 }
91
92 it = prop_array_iterator(tables);
93 while ((tbldict = prop_object_iterator_next(it)) != NULL) {
94 prop_dictionary_t ent;
95 prop_object_iterator_t eit;
96 prop_array_t entries;
97 npf_table_t *t;
98 u_int tid;
99 int type;
100
101 /* Table - dictionary. */
102 if (prop_object_type(tbldict) != PROP_TYPE_DICTIONARY) {
103 NPF_ERR_DEBUG(errdict);
104 error = EINVAL;
105 break;
106 }
107
108 /* Table ID and type. */
109 prop_dictionary_get_uint32(tbldict, "id", &tid);
110 prop_dictionary_get_int32(tbldict, "type", &type);
111
112 /* Validate them, check for duplicate IDs. */
113 error = npf_table_check(tblset, tid, type);
114 if (error)
115 break;
116
117 /* Create and insert the table. */
118 t = npf_table_create(tid, type, 1024); /* XXX */
119 if (t == NULL) {
120 NPF_ERR_DEBUG(errdict);
121 error = ENOMEM;
122 break;
123 }
124 error = npf_tableset_insert(tblset, t);
125 KASSERT(error == 0);
126
127 /* Entries. */
128 entries = prop_dictionary_get(tbldict, "entries");
129 if (prop_object_type(entries) != PROP_TYPE_ARRAY) {
130 NPF_ERR_DEBUG(errdict);
131 error = EINVAL;
132 break;
133 }
134 eit = prop_array_iterator(entries);
135 while ((ent = prop_object_iterator_next(eit)) != NULL) {
136 const npf_addr_t *addr;
137 npf_netmask_t mask;
138 int alen;
139
140 /* Get address and mask. Add a table entry. */
141 prop_object_t obj = prop_dictionary_get(ent, "addr");
142 addr = (const npf_addr_t *)prop_data_data_nocopy(obj);
143 prop_dictionary_get_uint8(ent, "mask", &mask);
144 alen = prop_data_size(obj);
145
146 error = npf_table_insert(tblset, tid, alen, addr, mask);
147 if (error)
148 break;
149 }
150 prop_object_iterator_release(eit);
151 if (error)
152 break;
153 }
154 prop_object_iterator_release(it);
155 /*
156 * Note: in a case of error, caller will free the tableset.
157 */
158 return error;
159 }
160
161 static npf_rproc_t *
162 npf_mk_rproc(prop_array_t rprocs, const char *rpname)
163 {
164 prop_object_iterator_t it;
165 prop_dictionary_t rpdict;
166 npf_rproc_t *rp;
167 uint64_t rpval;
168
169 it = prop_array_iterator(rprocs);
170 while ((rpdict = prop_object_iterator_next(it)) != NULL) {
171 const char *iname;
172 prop_dictionary_get_cstring_nocopy(rpdict, "name", &iname);
173 KASSERT(iname != NULL);
174 if (strcmp(rpname, iname) == 0)
175 break;
176 }
177 prop_object_iterator_release(it);
178 if (rpdict == NULL) {
179 return NULL;
180 }
181 CTASSERT(sizeof(uintptr_t) <= sizeof(uint64_t));
182 if (!prop_dictionary_get_uint64(rpdict, "rproc-ptr", &rpval)) {
183 rp = npf_rproc_create(rpdict);
184 rpval = (uint64_t)(uintptr_t)rp;
185 prop_dictionary_set_uint64(rpdict, "rproc-ptr", rpval);
186 } else {
187 rp = (npf_rproc_t *)(uintptr_t)rpval;
188 }
189 return rp;
190 }
191
192 static int __noinline
193 npf_mk_ncode(prop_object_t obj, void **code, size_t *csize,
194 prop_dictionary_t errdict)
195 {
196 const void *ncptr;
197 int nc_err, errat;
198 size_t nc_size;
199 void *nc;
200
201 /*
202 * Allocate, copy and validate n-code. XXX: Inefficient.
203 */
204 ncptr = prop_data_data_nocopy(obj);
205 nc_size = prop_data_size(obj);
206 if (ncptr == NULL || nc_size > NPF_NCODE_LIMIT) {
207 NPF_ERR_DEBUG(errdict);
208 return ERANGE;
209 }
210 nc = npf_ncode_alloc(nc_size);
211 if (nc == NULL) {
212 NPF_ERR_DEBUG(errdict);
213 return ENOMEM;
214 }
215 memcpy(nc, ncptr, nc_size);
216 nc_err = npf_ncode_validate(nc, nc_size, &errat);
217 if (nc_err) {
218 npf_ncode_free(nc, nc_size);
219 prop_dictionary_set_int32(errdict, "ncode-error", nc_err);
220 prop_dictionary_set_int32(errdict, "ncode-errat", errat);
221 return EINVAL;
222 }
223 *code = nc;
224 *csize = nc_size;
225 return 0;
226 }
227
228 static int __noinline
229 npf_mk_singlerule(prop_dictionary_t rldict, prop_array_t rps, npf_rule_t **rl,
230 prop_dictionary_t errdict)
231 {
232 const char *rnm;
233 npf_rproc_t *rp;
234 prop_object_t obj;
235 size_t nc_size;
236 void *nc;
237 int p, error;
238
239 /* Rule - dictionary. */
240 if (prop_object_type(rldict) != PROP_TYPE_DICTIONARY) {
241 NPF_ERR_DEBUG(errdict);
242 return EINVAL;
243 }
244
245 error = 0;
246 obj = prop_dictionary_get(rldict, "ncode");
247 if (obj) {
248 /* N-code (binary data). */
249 error = npf_mk_ncode(obj, &nc, &nc_size, errdict);
250 if (error) {
251 goto err;
252 }
253 } else {
254 /* No n-code. */
255 nc = NULL;
256 nc_size = 0;
257 }
258
259 /* Check for rule procedure. */
260 if (rps && prop_dictionary_get_cstring_nocopy(rldict, "rproc", &rnm)) {
261 rp = npf_mk_rproc(rps, rnm);
262 if (rp == NULL) {
263 if (nc) {
264 npf_ncode_free(nc, nc_size); /* XXX */
265 }
266 NPF_ERR_DEBUG(errdict);
267 error = EINVAL;
268 goto err;
269 }
270 } else {
271 rp = NULL;
272 }
273
274 /* Finally, allocate and return the rule. */
275 *rl = npf_rule_alloc(rldict, rp, nc, nc_size);
276 KASSERT(*rl != NULL);
277 return 0;
278 err:
279 prop_dictionary_get_int32(rldict, "priority", &p); /* XXX */
280 prop_dictionary_set_int32(errdict, "id", p);
281 return error;
282 }
283
284 static int __noinline
285 npf_mk_subrules(npf_ruleset_t *rlset, prop_array_t rules, prop_array_t rprocs,
286 prop_dictionary_t errdict)
287 {
288 prop_object_iterator_t it;
289 prop_dictionary_t rldict;
290 int error = 0;
291
292 if (prop_object_type(rules) != PROP_TYPE_ARRAY) {
293 NPF_ERR_DEBUG(errdict);
294 return EINVAL;
295 }
296 it = prop_array_iterator(rules);
297 while ((rldict = prop_object_iterator_next(it)) != NULL) {
298 npf_rule_t *rl;
299 error = npf_mk_singlerule(rldict, rprocs, &rl, errdict);
300 if (error) {
301 break;
302 }
303 npf_ruleset_insert(rlset, rl);
304 }
305 prop_object_iterator_release(it);
306 return error;
307 }
308
309 static int __noinline
310 npf_mk_rules(npf_ruleset_t *rlset, prop_array_t rules, prop_array_t rprocs,
311 prop_dictionary_t errdict)
312 {
313 prop_object_iterator_t it;
314 prop_dictionary_t rldict, rpdict;
315 int error;
316
317 /* Rule procedures and the ruleset - arrays. */
318 if (prop_object_type(rprocs) != PROP_TYPE_ARRAY ||
319 prop_object_type(rules) != PROP_TYPE_ARRAY) {
320 NPF_ERR_DEBUG(errdict);
321 return EINVAL;
322 }
323
324 it = prop_array_iterator(rprocs);
325 while ((rpdict = prop_object_iterator_next(it)) != NULL) {
326 if (prop_dictionary_get(rpdict, "rproc-ptr")) {
327 prop_object_iterator_release(it);
328 NPF_ERR_DEBUG(errdict);
329 return EINVAL;
330 }
331 }
332 prop_object_iterator_release(it);
333
334 error = 0;
335 it = prop_array_iterator(rules);
336 while ((rldict = prop_object_iterator_next(it)) != NULL) {
337 prop_array_t subrules;
338 npf_ruleset_t *rlsetsub;
339 npf_rule_t *rl;
340
341 /* Generate a single rule. */
342 error = npf_mk_singlerule(rldict, rprocs, &rl, errdict);
343 if (error) {
344 break;
345 }
346 npf_ruleset_insert(rlset, rl);
347
348 /* Check for sub-rules and generate, if any. */
349 subrules = prop_dictionary_get(rldict, "subrules");
350 if (subrules == NULL) {
351 /* No subrules, next.. */
352 continue;
353 }
354 rlsetsub = npf_rule_subset(rl);
355 error = npf_mk_subrules(rlsetsub, subrules, rprocs, errdict);
356 if (error)
357 break;
358 }
359 prop_object_iterator_release(it);
360 /*
361 * Note: in a case of error, caller will free the ruleset.
362 */
363 return error;
364 }
365
366 static int __noinline
367 npf_mk_natlist(npf_ruleset_t *nset, prop_array_t natlist,
368 prop_dictionary_t errdict)
369 {
370 prop_object_iterator_t it;
371 prop_dictionary_t natdict;
372 int error;
373
374 /* NAT policies - array. */
375 if (prop_object_type(natlist) != PROP_TYPE_ARRAY) {
376 NPF_ERR_DEBUG(errdict);
377 return EINVAL;
378 }
379
380 error = 0;
381 it = prop_array_iterator(natlist);
382 while ((natdict = prop_object_iterator_next(it)) != NULL) {
383 npf_natpolicy_t *np;
384 npf_rule_t *rl;
385
386 /* NAT policy - dictionary. */
387 if (prop_object_type(natdict) != PROP_TYPE_DICTIONARY) {
388 NPF_ERR_DEBUG(errdict);
389 error = EINVAL;
390 break;
391 }
392
393 /*
394 * NAT policies are standard rules, plus additional
395 * information for translation. Make a rule.
396 */
397 error = npf_mk_singlerule(natdict, NULL, &rl, errdict);
398 if (error) {
399 break;
400 }
401 npf_ruleset_insert(nset, rl);
402
403 /* If rule is named, it is a group with NAT policies. */
404 if (prop_dictionary_get(natdict, "name") &&
405 prop_dictionary_get(natdict, "subrules")) {
406 continue;
407 }
408
409 /* Allocate a new NAT policy and assign to the rule. */
410 np = npf_nat_newpolicy(natdict, nset);
411 if (np == NULL) {
412 NPF_ERR_DEBUG(errdict);
413 error = ENOMEM;
414 break;
415 }
416 npf_rule_setnat(rl, np);
417 }
418 prop_object_iterator_release(it);
419 /*
420 * Note: in a case of error, caller will free entire NAT ruleset
421 * with assigned NAT policies.
422 */
423 return error;
424 }
425
426 /*
427 * npfctl_reload: store passed data i.e. update settings, create passed
428 * tables, rules and atomically activate all them.
429 */
430 int
431 npfctl_reload(u_long cmd, void *data)
432 {
433 struct plistref *pref = data;
434 prop_dictionary_t npf_dict, errdict;
435 prop_array_t natlist, tables, rprocs, rules;
436 npf_tableset_t *tblset = NULL;
437 npf_ruleset_t *rlset = NULL;
438 npf_ruleset_t *nset = NULL;
439 bool flush;
440 int error;
441
442 /* Retrieve the dictionary. */
443 #ifndef _NPF_TESTING
444 error = prop_dictionary_copyin_ioctl(pref, cmd, &npf_dict);
445 if (error)
446 return error;
447 #else
448 npf_dict = (prop_dictionary_t)pref;
449 #endif
450
451 /* Dictionary for error reporting. */
452 errdict = prop_dictionary_create();
453
454 /* NAT policies. */
455 nset = npf_ruleset_create();
456 natlist = prop_dictionary_get(npf_dict, "translation");
457 error = npf_mk_natlist(nset, natlist, errdict);
458 if (error) {
459 goto fail;
460 }
461
462 /* Tables. */
463 tblset = npf_tableset_create();
464 tables = prop_dictionary_get(npf_dict, "tables");
465 error = npf_mk_tables(tblset, tables, errdict);
466 if (error) {
467 goto fail;
468 }
469
470 /* Rules and rule procedures. */
471 rlset = npf_ruleset_create();
472 rprocs = prop_dictionary_get(npf_dict, "rprocs");
473 rules = prop_dictionary_get(npf_dict, "rules");
474 error = npf_mk_rules(rlset, rules, rprocs, errdict);
475 if (error) {
476 goto fail;
477 }
478
479 flush = false;
480 prop_dictionary_get_bool(npf_dict, "flush", &flush);
481
482 /*
483 * Finally - reload ruleset, tableset and NAT policies.
484 * Operation will be performed as a single transaction.
485 */
486 npf_reload(npf_dict, rlset, tblset, nset, flush);
487
488 /* Turn on/off session tracking accordingly. */
489 npf_session_tracking(!flush);
490
491 /* Done. Since data is consumed now, we shall not destroy it. */
492 tblset = NULL;
493 rlset = NULL;
494 nset = NULL;
495 fail:
496 /*
497 * Note: destroy rulesets first, to drop references to the tableset.
498 */
499 KASSERT(error == 0 || (nset || rlset || tblset));
500 if (nset) {
501 npf_ruleset_destroy(nset);
502 }
503 if (rlset) {
504 npf_ruleset_destroy(rlset);
505 }
506 if (tblset) {
507 npf_tableset_destroy(tblset);
508 }
509 if (error) {
510 prop_object_release(npf_dict);
511 }
512
513 /* Error report. */
514 prop_dictionary_set_int32(errdict, "errno", error);
515 #ifndef _NPF_TESTING
516 prop_dictionary_copyout_ioctl(pref, cmd, errdict);
517 #endif
518 prop_object_release(errdict);
519 return 0;
520 }
521
522 int
523 npfctl_getconf(u_long cmd, void *data)
524 {
525 struct plistref *pref = data;
526 prop_dictionary_t npf_dict;
527 int error;
528
529 npf_core_enter();
530 npf_dict = npf_core_dict();
531 prop_dictionary_set_bool(npf_dict, "active", npf_pfil_registered_p());
532 error = prop_dictionary_copyout_ioctl(pref, cmd, npf_dict);
533 npf_core_exit();
534
535 return error;
536 }
537
538 /*
539 * npfctl_update_rule: reload a specific rule identified by the name.
540 */
541 int
542 npfctl_update_rule(u_long cmd, void *data)
543 {
544 struct plistref *pref = data;
545 prop_dictionary_t dict, errdict;
546 prop_array_t subrules;
547 prop_object_t obj;
548 npf_ruleset_t *rlset;
549 const char *name;
550 int error;
551
552 /* Retrieve and construct the rule. */
553 error = prop_dictionary_copyin_ioctl(pref, cmd, &dict);
554 if (error) {
555 return error;
556 }
557
558 /* Dictionary for error reporting. */
559 errdict = prop_dictionary_create();
560
561 /* Create the ruleset and construct sub-rules. */
562 rlset = npf_ruleset_create();
563 subrules = prop_dictionary_get(dict, "subrules");
564 error = npf_mk_subrules(rlset, subrules, NULL, errdict);
565 if (error) {
566 goto out;
567 }
568
569 /* Lookup the rule by name, and replace its subset (sub-rules). */
570 obj = prop_dictionary_get(dict, "name");
571 name = prop_string_cstring_nocopy(obj);
572 if (npf_ruleset_replace(name, rlset) == NULL) {
573 /* Not found. */
574 error = ENOENT;
575 out: /* Error path. */
576 npf_ruleset_destroy(rlset);
577 }
578 prop_object_release(dict);
579
580 /* Error report. */
581 prop_dictionary_set_int32(errdict, "errno", error);
582 prop_dictionary_copyout_ioctl(pref, cmd, errdict);
583 prop_object_release(errdict);
584 return error;
585 }
586
587 /*
588 * npfctl_sessions_save: construct a list of sessions and export for saving.
589 */
590 int
591 npfctl_sessions_save(u_long cmd, void *data)
592 {
593 struct plistref *pref = data;
594 prop_dictionary_t sesdict;
595 prop_array_t selist, nplist;
596 int error;
597
598 /* Create a dictionary and two lists. */
599 sesdict = prop_dictionary_create();
600 selist = prop_array_create();
601 nplist = prop_array_create();
602
603 /* Save the sessions. */
604 error = npf_session_save(selist, nplist);
605 if (error) {
606 goto fail;
607 }
608
609 /* Set the session list, NAT policy list and export the dictionary. */
610 prop_dictionary_set(sesdict, "session-list", selist);
611 prop_dictionary_set(sesdict, "nat-policy-list", nplist);
612 error = prop_dictionary_copyout_ioctl(pref, cmd, sesdict);
613 fail:
614 prop_object_release(sesdict);
615 return error;
616 }
617
618 /*
619 * npfctl_sessions_load: import a list of sessions, reconstruct them and load.
620 */
621 int
622 npfctl_sessions_load(u_long cmd, void *data)
623 {
624 const struct plistref *pref = data;
625 npf_sehash_t *sehasht = NULL;
626 prop_dictionary_t sesdict, sedict;
627 prop_object_iterator_t it;
628 prop_array_t selist;
629 int error;
630
631 /* Retrieve the dictionary containing session and NAT policy lists. */
632 error = prop_dictionary_copyin_ioctl(pref, cmd, &sesdict);
633 if (error)
634 return error;
635
636 /*
637 * Note: session objects contain the references to the NAT policy
638 * entries. Therefore, no need to directly access it.
639 */
640 selist = prop_dictionary_get(sesdict, "session-list");
641 if (prop_object_type(selist) != PROP_TYPE_ARRAY) {
642 error = EINVAL;
643 goto fail;
644 }
645
646 /* Create a session hash table. */
647 sehasht = sess_htable_create();
648 if (sehasht == NULL) {
649 error = ENOMEM;
650 goto fail;
651 }
652
653 /*
654 * Iterate through and construct each session.
655 */
656 error = 0;
657 it = prop_array_iterator(selist);
658 npf_core_enter();
659 while ((sedict = prop_object_iterator_next(it)) != NULL) {
660 /* Session - dictionary. */
661 if (prop_object_type(sedict) != PROP_TYPE_DICTIONARY) {
662 error = EINVAL;
663 goto fail;
664 }
665 /* Construct and insert real session structure. */
666 error = npf_session_restore(sehasht, sedict);
667 if (error) {
668 goto fail;
669 }
670 }
671 npf_core_exit();
672 sess_htable_reload(sehasht);
673 fail:
674 prop_object_release(selist);
675 if (error && sehasht) {
676 /* Destroy session table. */
677 sess_htable_destroy(sehasht);
678 }
679 return error;
680 }
681
682 /*
683 * npfctl_table: add, remove or query entries in the specified table.
684 *
685 * For maximum performance, interface is avoiding proplib(3)'s overhead.
686 */
687 int
688 npfctl_table(void *data)
689 {
690 npf_ioctl_table_t *nct = data;
691 npf_tableset_t *tblset;
692 int error;
693
694 npf_core_enter(); /* XXXSMP */
695 tblset = npf_core_tableset();
696 switch (nct->nct_action) {
697 case NPF_IOCTL_TBLENT_ADD:
698 error = npf_table_insert(tblset, nct->nct_tid,
699 nct->nct_alen, &nct->nct_addr, nct->nct_mask);
700 break;
701 case NPF_IOCTL_TBLENT_REM:
702 error = npf_table_remove(tblset, nct->nct_tid,
703 nct->nct_alen, &nct->nct_addr, nct->nct_mask);
704 break;
705 default:
706 error = npf_table_lookup(tblset, nct->nct_tid,
707 nct->nct_alen, &nct->nct_addr);
708 }
709 npf_core_exit(); /* XXXSMP */
710 return error;
711 }
712