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