npf_ruleset.c revision 1.17 1 /* $NetBSD: npf_ruleset.c,v 1.17 2013/02/09 03:35:32 rmind Exp $ */
2
3 /*-
4 * Copyright (c) 2009-2013 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 ruleset module.
34 */
35
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: npf_ruleset.c,v 1.17 2013/02/09 03:35:32 rmind Exp $");
38
39 #include <sys/param.h>
40 #include <sys/types.h>
41
42 #include <sys/kmem.h>
43 #include <sys/queue.h>
44 #include <sys/mbuf.h>
45 #include <sys/types.h>
46
47 #include <net/bpf.h>
48 #include <net/pfil.h>
49 #include <net/if.h>
50
51 #include "npf_ncode.h"
52 #include "npf_impl.h"
53
54 struct npf_ruleset {
55 /* List of all rules and dynamic (i.e. named) rules. */
56 LIST_HEAD(, npf_rule) rs_all;
57 LIST_HEAD(, npf_rule) rs_dynamic;
58
59 /* Number of array slots and active rules. */
60 u_int rs_slots;
61 u_int rs_nitems;
62
63 /* Array of ordered rules. */
64 npf_rule_t * rs_rules[];
65 };
66
67 struct npf_rule {
68 /* Attributes, interface and skip slot. */
69 uint32_t r_attr;
70 u_int r_ifid;
71 u_int r_skip_to;
72
73 /* Code to process, if any. */
74 int r_type;
75 void * r_code;
76 size_t r_clen;
77
78 /* NAT policy (optional), rule procedure and subset. */
79 npf_natpolicy_t * r_natp;
80 npf_rproc_t * r_rproc;
81
82 /* Rule priority: (highest) 1, 2 ... n (lowest). */
83 pri_t r_priority;
84
85 /*
86 * Dynamic group: subset queue and a dynamic group list entry.
87 * Dynamic rule: entry and the parent rule (the group).
88 */
89 union {
90 TAILQ_HEAD(npf_ruleq, npf_rule) r_subset;
91 TAILQ_ENTRY(npf_rule) r_entry;
92 } /* C11 */;
93 union {
94 LIST_ENTRY(npf_rule) r_dentry;
95 npf_rule_t * r_parent;
96 } /* C11 */;
97
98 /* Rule name and all-list entry. */
99 char r_name[NPF_RULE_MAXNAMELEN];
100 LIST_ENTRY(npf_rule) r_aentry;
101
102 /* Key (optional). */
103 uint8_t r_key[NPF_RULE_MAXKEYLEN];
104 };
105
106 #define NPF_DYNAMIC_GROUP_P(attr) \
107 (((attr) & NPF_DYNAMIC_GROUP) == NPF_DYNAMIC_GROUP)
108
109 npf_ruleset_t *
110 npf_ruleset_create(size_t slots)
111 {
112 size_t len = offsetof(npf_ruleset_t, rs_rules[slots]);
113 npf_ruleset_t *rlset;
114
115 rlset = kmem_zalloc(len, KM_SLEEP);
116 rlset->rs_slots = slots;
117 LIST_INIT(&rlset->rs_dynamic);
118 LIST_INIT(&rlset->rs_all);
119 return rlset;
120 }
121
122 static void
123 npf_ruleset_unlink(npf_ruleset_t *rlset, npf_rule_t *rl)
124 {
125 if (NPF_DYNAMIC_GROUP_P(rl->r_attr)) {
126 LIST_REMOVE(rl, r_dentry);
127 }
128 if ((rl->r_attr & NPF_DYNAMIC_GROUP) == NPF_RULE_DYNAMIC) {
129 npf_rule_t *rg = rl->r_parent;
130 TAILQ_REMOVE(&rg->r_subset, rl, r_entry);
131 }
132 LIST_REMOVE(rl, r_aentry);
133 }
134
135 void
136 npf_ruleset_destroy(npf_ruleset_t *rlset)
137 {
138 size_t len = offsetof(npf_ruleset_t, rs_rules[rlset->rs_slots]);
139 npf_rule_t *rl;
140
141 while ((rl = LIST_FIRST(&rlset->rs_all)) != NULL) {
142 npf_ruleset_unlink(rlset, rl);
143 npf_rule_free(rl);
144 }
145 KASSERT(LIST_EMPTY(&rlset->rs_dynamic));
146 kmem_free(rlset, len);
147 }
148
149 /*
150 * npf_ruleset_insert: insert the rule into the specified ruleset.
151 */
152 void
153 npf_ruleset_insert(npf_ruleset_t *rlset, npf_rule_t *rl)
154 {
155 u_int n = rlset->rs_nitems;
156
157 KASSERT(n < rlset->rs_slots);
158
159 LIST_INSERT_HEAD(&rlset->rs_all, rl, r_aentry);
160 if (NPF_DYNAMIC_GROUP_P(rl->r_attr)) {
161 LIST_INSERT_HEAD(&rlset->rs_dynamic, rl, r_dentry);
162 }
163
164 rlset->rs_rules[n] = rl;
165 rlset->rs_nitems++;
166
167 if (rl->r_skip_to < ++n) {
168 rl->r_skip_to = n;
169 }
170 }
171
172 static npf_rule_t *
173 npf_ruleset_lookup(npf_ruleset_t *rlset, const char *name)
174 {
175 npf_rule_t *rl;
176
177 KASSERT(npf_config_locked_p());
178
179 LIST_FOREACH(rl, &rlset->rs_dynamic, r_dentry) {
180 KASSERT(NPF_DYNAMIC_GROUP_P(rl->r_attr));
181 if (strncmp(rl->r_name, name, NPF_RULE_MAXNAMELEN) == 0)
182 break;
183 }
184 return rl;
185 }
186
187 int
188 npf_ruleset_add(npf_ruleset_t *rlset, const char *rname, npf_rule_t *rl)
189 {
190 npf_rule_t *rg, *it;
191 pri_t priocmd;
192
193 rg = npf_ruleset_lookup(rlset, rname);
194 if (rg == NULL) {
195 return ENOENT;
196 }
197
198 /* Dynamic rule. */
199 rl->r_attr |= NPF_RULE_DYNAMIC;
200 rl->r_parent = rg;
201
202 /*
203 * Rule priority: (highest) 1, 2 ... n (lowest).
204 * Negative priority indicates an operation and is reset to zero.
205 */
206 if ((priocmd = rl->r_priority) < 0) {
207 rl->r_priority = 0;
208 }
209
210 switch (priocmd) {
211 case NPF_PRI_FIRST:
212 TAILQ_FOREACH(it, &rg->r_subset, r_entry) {
213 if (rl->r_priority <= it->r_priority)
214 break;
215 }
216 if (it) {
217 TAILQ_INSERT_BEFORE(it, rl, r_entry);
218 } else {
219 TAILQ_INSERT_HEAD(&rg->r_subset, rl, r_entry);
220 }
221 break;
222 case NPF_PRI_LAST:
223 default:
224 TAILQ_FOREACH(it, &rg->r_subset, r_entry) {
225 if (rl->r_priority < it->r_priority)
226 break;
227 }
228 if (it) {
229 TAILQ_INSERT_BEFORE(it, rl, r_entry);
230 } else {
231 TAILQ_INSERT_TAIL(&rg->r_subset, rl, r_entry);
232 }
233 break;
234 }
235
236 /* Finally, add into the all-list. */
237 LIST_INSERT_HEAD(&rlset->rs_all, rl, r_aentry);
238 return 0;
239 }
240
241 npf_rule_t *
242 npf_ruleset_remove(npf_ruleset_t *rlset, const char *rname, uintptr_t id)
243 {
244 npf_rule_t *rg, *rl;
245
246 if ((rg = npf_ruleset_lookup(rlset, rname)) == NULL) {
247 return NULL;
248 }
249 TAILQ_FOREACH(rl, &rg->r_subset, r_entry) {
250 /* Compare ID. On match, remove and return. */
251 if ((uintptr_t)rl == id) {
252 npf_ruleset_unlink(rlset, rl);
253 break;
254 }
255 }
256 return rl;
257 }
258
259 npf_rule_t *
260 npf_ruleset_remkey(npf_ruleset_t *rlset, const char *rname,
261 const void *key, size_t len)
262 {
263 npf_rule_t *rg, *rl;
264
265 KASSERT(len && len <= NPF_RULE_MAXKEYLEN);
266
267 if ((rg = npf_ruleset_lookup(rlset, rname)) == NULL) {
268 return NULL;
269 }
270 /* Find the last in the list. */
271 TAILQ_FOREACH_REVERSE(rl, &rg->r_subset, npf_ruleq, r_entry) {
272 /* Compare the key. On match, remove and return. */
273 if (memcmp(rl->r_key, key, len) == 0) {
274 npf_ruleset_unlink(rlset, rl);
275 break;
276 }
277 }
278 return rl;
279 }
280
281 /*
282 * npf_ruleset_reload: share the dynamic rules.
283 *
284 * => Active ruleset should be exclusively locked.
285 */
286 void
287 npf_ruleset_reload(npf_ruleset_t *nrlset, npf_ruleset_t *arlset)
288 {
289 npf_rule_t *rl, *arl;
290
291 KASSERT(npf_config_locked_p());
292
293 LIST_FOREACH(rl, &nrlset->rs_dynamic, r_dentry) {
294 if ((arl = npf_ruleset_lookup(arlset, rl->r_name)) == NULL) {
295 continue;
296 }
297 memcpy(&rl->r_subset, &arl->r_subset, sizeof(rl->r_subset));
298 }
299 }
300
301 /*
302 * npf_ruleset_matchnat: find a matching NAT policy in the ruleset.
303 */
304 npf_rule_t *
305 npf_ruleset_matchnat(npf_ruleset_t *rlset, npf_natpolicy_t *mnp)
306 {
307 npf_rule_t *rl;
308
309 /* Find a matching NAT policy in the old ruleset. */
310 LIST_FOREACH(rl, &rlset->rs_all, r_aentry) {
311 if (npf_nat_matchpolicy(rl->r_natp, mnp))
312 break;
313 }
314 return rl;
315 }
316
317 npf_rule_t *
318 npf_ruleset_sharepm(npf_ruleset_t *rlset, npf_natpolicy_t *mnp)
319 {
320 npf_natpolicy_t *np;
321 npf_rule_t *rl;
322
323 /* Find a matching NAT policy in the old ruleset. */
324 LIST_FOREACH(rl, &rlset->rs_all, r_aentry) {
325 /*
326 * NAT policy might not yet be set during the creation of
327 * the ruleset (in such case, rule is for our policy), or
328 * policies might be equal due to rule exchange on reload.
329 */
330 np = rl->r_natp;
331 if (np == NULL || np == mnp)
332 continue;
333 if (npf_nat_sharepm(np, mnp))
334 break;
335 }
336 return rl;
337 }
338
339 /*
340 * npf_ruleset_freealg: inspect the ruleset and disassociate specified
341 * ALG from all NAT entries using it.
342 */
343 void
344 npf_ruleset_freealg(npf_ruleset_t *rlset, npf_alg_t *alg)
345 {
346 npf_rule_t *rl;
347 npf_natpolicy_t *np;
348
349 LIST_FOREACH(rl, &rlset->rs_all, r_aentry) {
350 if ((np = rl->r_natp) != NULL) {
351 npf_nat_freealg(np, alg);
352 }
353 }
354 }
355
356 /*
357 * npf_ruleset_natreload: minimum reload of NAT policies by maching
358 * two (active and new) NAT rulesets.
359 *
360 * => Active ruleset should be exclusively locked.
361 */
362 void
363 npf_ruleset_natreload(npf_ruleset_t *nrlset, npf_ruleset_t *arlset)
364 {
365 npf_natpolicy_t *np, *anp;
366 npf_rule_t *rl, *arl;
367
368 /* Scan a new NAT ruleset against NAT policies in old ruleset. */
369 LIST_FOREACH(rl, &nrlset->rs_all, r_aentry) {
370 np = rl->r_natp;
371 arl = npf_ruleset_matchnat(arlset, np);
372 if (arl == NULL) {
373 continue;
374 }
375 /* On match - we exchange NAT policies. */
376 anp = arl->r_natp;
377 rl->r_natp = anp;
378 arl->r_natp = np;
379 /* Update other NAT policies to share portmap. */
380 (void)npf_ruleset_sharepm(nrlset, anp);
381 }
382 }
383
384 /*
385 * npf_rule_alloc: allocate a rule and copy n-code from user-space.
386 */
387 npf_rule_t *
388 npf_rule_alloc(prop_dictionary_t rldict)
389 {
390 npf_rule_t *rl;
391 const char *rname;
392
393 /* Allocate a rule structure. */
394 rl = kmem_zalloc(sizeof(npf_rule_t), KM_SLEEP);
395 TAILQ_INIT(&rl->r_subset);
396 rl->r_natp = NULL;
397
398 /* Name (optional) */
399 if (prop_dictionary_get_cstring_nocopy(rldict, "name", &rname)) {
400 strlcpy(rl->r_name, rname, NPF_RULE_MAXNAMELEN);
401 } else {
402 rl->r_name[0] = '\0';
403 }
404
405 /* Attributes, priority and interface ID (optional). */
406 prop_dictionary_get_uint32(rldict, "attributes", &rl->r_attr);
407 prop_dictionary_get_int32(rldict, "priority", &rl->r_priority);
408 prop_dictionary_get_uint32(rldict, "interface", &rl->r_ifid);
409
410 /* Get the skip-to index. No need to validate it. */
411 prop_dictionary_get_uint32(rldict, "skip-to", &rl->r_skip_to);
412
413 /* Key (optional). */
414 prop_object_t obj = prop_dictionary_get(rldict, "key");
415 const void *key = prop_data_data_nocopy(obj);
416
417 if (key) {
418 size_t len = prop_data_size(obj);
419 if (len > NPF_RULE_MAXKEYLEN) {
420 kmem_free(rl, sizeof(npf_rule_t));
421 return NULL;
422 }
423 memcpy(rl->r_key, key, len);
424 }
425 return rl;
426 }
427
428 /*
429 * npf_rule_setcode: assign filter code to the rule.
430 *
431 * => The code should be validated by the caller.
432 */
433 void
434 npf_rule_setcode(npf_rule_t *rl, const int type, void *code, size_t size)
435 {
436 rl->r_type = type;
437 rl->r_code = code;
438 rl->r_clen = size;
439 }
440
441 /*
442 * npf_rule_setrproc: assign a rule procedure and hold a reference on it.
443 */
444 void
445 npf_rule_setrproc(npf_rule_t *rl, npf_rproc_t *rp)
446 {
447 npf_rproc_acquire(rp);
448 rl->r_rproc = rp;
449 }
450
451 /*
452 * npf_rule_free: free the specified rule.
453 */
454 void
455 npf_rule_free(npf_rule_t *rl)
456 {
457 npf_natpolicy_t *np = rl->r_natp;
458 npf_rproc_t *rp = rl->r_rproc;
459
460 if (np) {
461 /* Free NAT policy. */
462 npf_nat_freepolicy(np);
463 }
464 if (rp) {
465 /* Release rule procedure. */
466 npf_rproc_release(rp);
467 }
468 if (rl->r_code) {
469 /* Free n-code. */
470 kmem_free(rl->r_code, rl->r_clen);
471 }
472 kmem_free(rl, sizeof(npf_rule_t));
473 }
474
475 /*
476 * npf_rule_getrproc: acquire a reference and return rule procedure, if any.
477 * npf_rule_getnat: get NAT policy assigned to the rule.
478 */
479
480 npf_rproc_t *
481 npf_rule_getrproc(npf_rule_t *rl)
482 {
483 npf_rproc_t *rp = rl->r_rproc;
484
485 if (rp) {
486 npf_rproc_acquire(rp);
487 }
488 return rp;
489 }
490
491 npf_natpolicy_t *
492 npf_rule_getnat(const npf_rule_t *rl)
493 {
494 return rl->r_natp;
495 }
496
497 /*
498 * npf_rule_setnat: assign NAT policy to the rule and insert into the
499 * NAT policy list in the ruleset.
500 */
501 void
502 npf_rule_setnat(npf_rule_t *rl, npf_natpolicy_t *np)
503 {
504
505 KASSERT(rl->r_natp == NULL);
506 rl->r_natp = np;
507 }
508
509 /*
510 * npf_rule_inspect: match the interface, direction and run the filter code.
511 * Returns true if rule matches, false otherise.
512 */
513 static inline bool
514 npf_rule_inspect(npf_cache_t *npc, nbuf_t *nbuf, const npf_rule_t *rl,
515 const int di_mask, const int layer)
516 {
517 const ifnet_t *ifp = nbuf->nb_ifp;
518 const void *code;
519
520 /* Match the interface. */
521 if (rl->r_ifid && rl->r_ifid != ifp->if_index) {
522 return false;
523 }
524
525 /* Match the direction. */
526 if ((rl->r_attr & NPF_RULE_DIMASK) != NPF_RULE_DIMASK) {
527 if ((rl->r_attr & di_mask) == 0)
528 return false;
529 }
530
531 /* Execute the code, if any. */
532 if ((code = rl->r_code) == NULL) {
533 return true;
534 }
535
536 switch (rl->r_type) {
537 case NPF_CODE_NC:
538 return npf_ncode_process(npc, code, nbuf, layer) == 0;
539 case NPF_CODE_BPF: {
540 struct mbuf *m = nbuf_head_mbuf(nbuf);
541 size_t pktlen = m_length(m);
542 return bpf_filter(code, (unsigned char *)m, pktlen, 0) != 0;
543 }
544 default:
545 KASSERT(false);
546 }
547 return false;
548 }
549
550 /*
551 * npf_rule_reinspect: re-inspect the dynamic rule by iterating its list.
552 * This is only for the dynamic rules. Subrules cannot have nested rules.
553 */
554 static npf_rule_t *
555 npf_rule_reinspect(npf_cache_t *npc, nbuf_t *nbuf, const npf_rule_t *drl,
556 const int di_mask, const int layer)
557 {
558 npf_rule_t *final_rl = NULL, *rl;
559
560 KASSERT(NPF_DYNAMIC_GROUP_P(drl->r_attr));
561
562 TAILQ_FOREACH(rl, &drl->r_subset, r_entry) {
563 if (!npf_rule_inspect(npc, nbuf, rl, di_mask, layer)) {
564 continue;
565 }
566 if (rl->r_attr & NPF_RULE_FINAL) {
567 return rl;
568 }
569 final_rl = rl;
570 }
571 return final_rl;
572 }
573
574 /*
575 * npf_ruleset_inspect: inspect the packet against the given ruleset.
576 *
577 * Loop through the rules in the set and run n-code processor of each rule
578 * against the packet (nbuf chain). If sub-ruleset is found, inspect it.
579 *
580 * => Caller is responsible for nbuf chain protection.
581 */
582 npf_rule_t *
583 npf_ruleset_inspect(npf_cache_t *npc, nbuf_t *nbuf,
584 const npf_ruleset_t *rlset, const int di, const int layer)
585 {
586 const int di_mask = (di & PFIL_IN) ? NPF_RULE_IN : NPF_RULE_OUT;
587 const u_int nitems = rlset->rs_nitems;
588 npf_rule_t *final_rl = NULL;
589 u_int n = 0;
590
591 KASSERT(((di & PFIL_IN) != 0) ^ ((di & PFIL_OUT) != 0));
592
593 while (n < nitems) {
594 npf_rule_t *rl = rlset->rs_rules[n];
595 const u_int skip_to = rl->r_skip_to;
596 const uint32_t attr = rl->r_attr;
597
598 KASSERT(!nbuf_flag_p(nbuf, NBUF_DATAREF_RESET));
599 KASSERT(!final_rl || rl->r_priority >= final_rl->r_priority);
600 KASSERT(n < skip_to);
601
602 /* Group is a barrier: return a matching if found any. */
603 if ((attr & NPF_RULE_GROUP) != 0 && final_rl) {
604 break;
605 }
606
607 /* Main inspection of the rule. */
608 if (!npf_rule_inspect(npc, nbuf, rl, di_mask, layer)) {
609 n = skip_to;
610 continue;
611 }
612
613 if (NPF_DYNAMIC_GROUP_P(attr)) {
614 /*
615 * If this is a dynamic rule, re-inspect the subrules.
616 * If it has any matching rule, then it is final.
617 */
618 rl = npf_rule_reinspect(npc, nbuf, rl, di_mask, layer);
619 if (rl != NULL) {
620 final_rl = rl;
621 break;
622 }
623 } else if ((attr & NPF_RULE_GROUP) == 0) {
624 /*
625 * Groups themselves are not matching.
626 */
627 final_rl = rl;
628 }
629
630 /* Set the matching rule and check for "final". */
631 if (attr & NPF_RULE_FINAL) {
632 break;
633 }
634 n++;
635 }
636
637 KASSERT(!nbuf_flag_p(nbuf, NBUF_DATAREF_RESET));
638 return final_rl;
639 }
640
641 /*
642 * npf_rule_conclude: return decision and the flags for conclusion.
643 *
644 * => Returns ENETUNREACH if "block" and 0 if "pass".
645 */
646 int
647 npf_rule_conclude(const npf_rule_t *rl, int *retfl)
648 {
649 /* If not passing - drop the packet. */
650 *retfl = rl->r_attr;
651 return (rl->r_attr & NPF_RULE_PASS) ? 0 : ENETUNREACH;
652 }
653
654 #if defined(DDB) || defined(_NPF_TESTING)
655
656 void
657 npf_rulenc_dump(const npf_rule_t *rl)
658 {
659 const uint32_t *op = rl->r_code;
660 size_t n = rl->r_clen;
661
662 while (n) {
663 printf("\t> |0x%02x|\n", (uint32_t)*op);
664 op++;
665 n -= sizeof(*op);
666 }
667 printf("-> %s\n", (rl->r_attr & NPF_RULE_PASS) ? "pass" : "block");
668 }
669
670 #endif
671