npf_ctl.c revision 1.13 1 /* $NetBSD: npf_ctl.c,v 1.13 2012/02/20 00:18:19 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.13 2012/02/20 00:18:19 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_register_pfil();
70 } else {
71 /* Disable: remove pfil hooks. */
72 npf_unregister_pfil();
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 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 #ifdef _KERNEL
438 error = prop_dictionary_copyin_ioctl(pref, cmd, &dict);
439 if (error)
440 return error;
441 #else
442 dict = prop_dictionary_internalize_from_file(data);
443 if (dict == NULL)
444 return EINVAL;
445 #endif
446 /* Dictionary for error reporting. */
447 errdict = prop_dictionary_create();
448
449 /* NAT policies. */
450 nset = npf_ruleset_create();
451 natlist = prop_dictionary_get(dict, "translation");
452 error = npf_mk_natlist(nset, natlist, errdict);
453 if (error) {
454 goto fail;
455 }
456
457 /* Tables. */
458 tblset = npf_tableset_create();
459 tables = prop_dictionary_get(dict, "tables");
460 error = npf_mk_tables(tblset, tables, errdict);
461 if (error) {
462 goto fail;
463 }
464
465 /* Rules and rule procedures. */
466 rlset = npf_ruleset_create();
467 rprocs = prop_dictionary_get(dict, "rprocs");
468 rules = prop_dictionary_get(dict, "rules");
469 error = npf_mk_rules(rlset, rules, rprocs, errdict);
470 if (error) {
471 goto fail;
472 }
473
474 flush = false;
475 prop_dictionary_get_bool(dict, "flush", &flush);
476
477 /*
478 * Finally - reload ruleset, tableset and NAT policies.
479 * Operation will be performed as a single transaction.
480 */
481 npf_reload(rlset, tblset, nset, flush);
482
483 /* Turn on/off session tracking accordingly. */
484 npf_session_tracking(!flush);
485
486 /* Done. Since data is consumed now, we shall not destroy it. */
487 tblset = NULL;
488 rlset = NULL;
489 nset = NULL;
490 fail:
491 /*
492 * Note: destroy rulesets first, to drop references to the tableset.
493 */
494 KASSERT(error == 0 || (nset || rlset || tblset));
495 if (nset) {
496 npf_ruleset_destroy(nset);
497 }
498 if (rlset) {
499 npf_ruleset_destroy(rlset);
500 }
501 if (tblset) {
502 npf_tableset_destroy(tblset);
503 }
504 prop_object_release(dict);
505
506 /* Error report. */
507 prop_dictionary_set_int32(errdict, "errno", error);
508 #ifdef _KERNEL
509 prop_dictionary_copyout_ioctl(pref, cmd, errdict);
510 #endif
511 prop_object_release(errdict);
512 return 0;
513 }
514
515 /*
516 * npfctl_update_rule: reload a specific rule identified by the name.
517 */
518 int
519 npfctl_update_rule(u_long cmd, void *data)
520 {
521 struct plistref *pref = data;
522 prop_dictionary_t dict, errdict;
523 prop_array_t subrules;
524 prop_object_t obj;
525 npf_ruleset_t *rlset;
526 const char *name;
527 int error;
528
529 #ifdef _KERNEL
530 /* Retrieve and construct the rule. */
531 error = prop_dictionary_copyin_ioctl(pref, cmd, &dict);
532 if (error) {
533 return error;
534 }
535 #else
536 dict = prop_dictionary_internalize_from_file(data);
537 if (dict == NULL)
538 return EINVAL;
539 #endif
540
541 /* Dictionary for error reporting. */
542 errdict = prop_dictionary_create();
543
544 /* Create the ruleset and construct sub-rules. */
545 rlset = npf_ruleset_create();
546 subrules = prop_dictionary_get(dict, "subrules");
547 error = npf_mk_subrules(rlset, subrules, NULL, errdict);
548 if (error) {
549 goto out;
550 }
551
552 /* Lookup the rule by name, and replace its subset (sub-rules). */
553 obj = prop_dictionary_get(dict, "name");
554 name = prop_string_cstring_nocopy(obj);
555 if (npf_ruleset_replace(name, rlset) == NULL) {
556 /* Not found. */
557 error = ENOENT;
558 out: /* Error path. */
559 npf_ruleset_destroy(rlset);
560 }
561 prop_object_release(dict);
562
563 /* Error report. */
564 prop_dictionary_set_int32(errdict, "errno", error);
565 #ifdef _KERNEL
566 prop_dictionary_copyout_ioctl(pref, cmd, errdict);
567 #endif
568 prop_object_release(errdict);
569 return error;
570 }
571
572 /*
573 * npfctl_sessions_save: construct a list of sessions and export for saving.
574 */
575 int
576 npfctl_sessions_save(u_long cmd, void *data)
577 {
578 struct plistref *pref = data;
579 prop_dictionary_t sesdict;
580 prop_array_t selist, nplist;
581 int error;
582
583 /* Create a dictionary and two lists. */
584 sesdict = prop_dictionary_create();
585 selist = prop_array_create();
586 nplist = prop_array_create();
587
588 /* Save the sessions. */
589 error = npf_session_save(selist, nplist);
590 if (error) {
591 goto fail;
592 }
593
594 /* Set the session list, NAT policy list and export the dictionary. */
595 prop_dictionary_set(sesdict, "session-list", selist);
596 prop_dictionary_set(sesdict, "nat-policy-list", nplist);
597 #ifdef _KERNEL
598 error = prop_dictionary_copyout_ioctl(pref, cmd, sesdict);
599 #else
600 error = prop_dictionary_externalize_to_file(sesdict, data) ? 0 : errno;
601 #endif
602 fail:
603 prop_object_release(sesdict);
604 return error;
605 }
606
607 /*
608 * npfctl_sessions_load: import a list of sessions, reconstruct them and load.
609 */
610 int
611 npfctl_sessions_load(u_long cmd, void *data)
612 {
613 const struct plistref *pref = data;
614 npf_sehash_t *sehasht = NULL;
615 prop_dictionary_t sesdict, sedict;
616 prop_object_iterator_t it;
617 prop_array_t selist;
618 int error;
619
620 /* Retrieve the dictionary containing session and NAT policy lists. */
621 #ifdef _KERNEL
622 error = prop_dictionary_copyin_ioctl(pref, cmd, &sesdict);
623 if (error)
624 return error;
625 #else
626 sesdict = prop_dictionary_internalize_from_file(data);
627 if (sesdict == NULL)
628 return EINVAL;
629 #endif
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