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