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