1 /* $OpenBSD: pfctl_optimize.c,v 1.13 2006/10/31 14:17:45 mcbride Exp $ */
4 * Copyright (c) 2004 Mike Frantzen <frantzen@openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/types.h>
20 #include <sys/ioctl.h>
21 #include <sys/socket.h>
24 #include <net/pf/pfvar.h>
26 #include <netinet/in.h>
27 #include <arpa/inet.h>
38 #include "pfctl_parser.h"
41 /* The size at which a table becomes faster than individual rules */
42 #define TABLE_THRESHOLD 6
45 /* #define OPT_DEBUG 1 */
47 # define DEBUG(str, v...) \
48 printf("%s: " str "\n", __FUNCTION__ , ## v)
50 # define DEBUG(str, v...) ((void)0)
55 * A container that lets us sort a superblock to optimize the skip step jumps
58 int ps_count; /* number of items */
59 TAILQ_HEAD( , pf_opt_rule) ps_rules;
60 TAILQ_ENTRY(pf_skip_step) ps_entry;
65 * A superblock is a block of adjacent rules of similar action. If there
66 * are five PASS rules in a row, they all become members of a superblock.
67 * Once we have a superblock, we are free to re-order any rules within it
68 * in order to improve performance; if a packet is passed, it doesn't matter
72 TAILQ_HEAD( , pf_opt_rule) sb_rules;
73 TAILQ_ENTRY(superblock) sb_entry;
74 struct superblock *sb_profiled_block;
75 TAILQ_HEAD(skiplist, pf_skip_step) sb_skipsteps[PF_SKIP_COUNT];
77 TAILQ_HEAD(superblocks, superblock);
81 * Description of the PF rule structure.
84 BARRIER, /* the presence of the field puts the rule in it's own block */
85 BREAK, /* the field may not differ between rules in a superblock */
86 NOMERGE, /* the field may not differ between rules when combined */
87 COMBINED, /* the field may itself be combined with other rules */
88 DC, /* we just don't care about the field */
89 NEVER}; /* we should never see this field set?!? */
90 struct pf_rule_field {
96 #define PF_RULE_FIELD(field, ty) \
99 offsetof(struct pf_rule, field), \
100 sizeof(((struct pf_rule *)0)->field)}
104 * The presence of these fields in a rule put the rule in it's own
105 * superblock. Thus it will not be optimized. It also prevents the
106 * rule from being re-ordered at all.
108 PF_RULE_FIELD(label, BARRIER),
109 PF_RULE_FIELD(prob, BARRIER),
110 PF_RULE_FIELD(max_states, BARRIER),
111 PF_RULE_FIELD(max_src_nodes, BARRIER),
112 PF_RULE_FIELD(max_src_states, BARRIER),
113 PF_RULE_FIELD(max_src_conn, BARRIER),
114 PF_RULE_FIELD(max_src_conn_rate, BARRIER),
115 PF_RULE_FIELD(anchor, BARRIER), /* for now */
118 * These fields must be the same between all rules in the same superblock.
119 * These rules are allowed to be re-ordered but only among like rules.
120 * For instance we can re-order all 'tag "foo"' rules because they have the
121 * same tag. But we can not re-order between a 'tag "foo"' and a
122 * 'tag "bar"' since that would change the meaning of the ruleset.
124 PF_RULE_FIELD(tagname, BREAK),
125 PF_RULE_FIELD(keep_state, BREAK),
126 PF_RULE_FIELD(qname, BREAK),
127 PF_RULE_FIELD(pqname, BREAK),
128 PF_RULE_FIELD(rt, BREAK),
129 PF_RULE_FIELD(allow_opts, BREAK),
130 PF_RULE_FIELD(rule_flag, BREAK),
131 PF_RULE_FIELD(action, BREAK),
132 PF_RULE_FIELD(log, BREAK),
133 PF_RULE_FIELD(quick, BREAK),
134 PF_RULE_FIELD(return_ttl, BREAK),
135 PF_RULE_FIELD(overload_tblname, BREAK),
136 PF_RULE_FIELD(flush, BREAK),
137 PF_RULE_FIELD(rpool, BREAK),
138 PF_RULE_FIELD(logif, BREAK),
141 * Any fields not listed in this structure act as BREAK fields
146 * These fields must not differ when we merge two rules together but
147 * their difference isn't enough to put the rules in different superblocks.
148 * There are no problems re-ordering any rules with these fields.
150 PF_RULE_FIELD(af, NOMERGE),
151 PF_RULE_FIELD(ifnot, NOMERGE),
152 PF_RULE_FIELD(ifname, NOMERGE), /* hack for IF groups */
153 PF_RULE_FIELD(match_tag_not, NOMERGE),
154 PF_RULE_FIELD(match_tagname, NOMERGE),
155 PF_RULE_FIELD(os_fingerprint, NOMERGE),
156 PF_RULE_FIELD(timeout, NOMERGE),
157 PF_RULE_FIELD(return_icmp, NOMERGE),
158 PF_RULE_FIELD(return_icmp6, NOMERGE),
159 PF_RULE_FIELD(uid, NOMERGE),
160 PF_RULE_FIELD(gid, NOMERGE),
161 PF_RULE_FIELD(direction, NOMERGE),
162 PF_RULE_FIELD(proto, NOMERGE),
163 PF_RULE_FIELD(type, NOMERGE),
164 PF_RULE_FIELD(code, NOMERGE),
165 PF_RULE_FIELD(flags, NOMERGE),
166 PF_RULE_FIELD(flagset, NOMERGE),
167 PF_RULE_FIELD(tos, NOMERGE),
168 PF_RULE_FIELD(src.port, NOMERGE),
169 PF_RULE_FIELD(dst.port, NOMERGE),
170 PF_RULE_FIELD(src.port_op, NOMERGE),
171 PF_RULE_FIELD(dst.port_op, NOMERGE),
172 PF_RULE_FIELD(src.neg, NOMERGE),
173 PF_RULE_FIELD(dst.neg, NOMERGE),
175 /* These fields can be merged */
176 PF_RULE_FIELD(src.addr, COMBINED),
177 PF_RULE_FIELD(dst.addr, COMBINED),
179 /* We just don't care about these fields. They're set by the kernel */
180 PF_RULE_FIELD(skip, DC),
181 PF_RULE_FIELD(evaluations, DC),
182 PF_RULE_FIELD(packets, DC),
183 PF_RULE_FIELD(bytes, DC),
184 PF_RULE_FIELD(kif, DC),
185 PF_RULE_FIELD(states, DC),
186 PF_RULE_FIELD(src_nodes, DC),
187 PF_RULE_FIELD(nr, DC),
188 PF_RULE_FIELD(entries, DC),
189 PF_RULE_FIELD(qid, DC),
190 PF_RULE_FIELD(pqid, DC),
191 PF_RULE_FIELD(anchor_relative, DC),
192 PF_RULE_FIELD(anchor_wildcard, DC),
193 PF_RULE_FIELD(tag, DC),
194 PF_RULE_FIELD(match_tag, DC),
195 PF_RULE_FIELD(overload_tbl, DC),
197 /* These fields should never be set in a PASS/BLOCK rule */
198 PF_RULE_FIELD(natpass, NEVER),
199 PF_RULE_FIELD(max_mss, NEVER),
200 PF_RULE_FIELD(min_ttl, NEVER),
205 int add_opt_table(struct pfctl *, struct pf_opt_tbl **, sa_family_t,
206 struct pf_rule_addr *);
207 int addrs_combineable(struct pf_rule_addr *, struct pf_rule_addr *);
208 int addrs_equal(struct pf_rule_addr *, struct pf_rule_addr *);
209 int block_feedback(struct pfctl *, struct superblock *);
210 int combine_rules(struct pfctl *, struct superblock *);
211 void comparable_rule(struct pf_rule *, const struct pf_rule *, int);
212 int construct_superblocks(struct pfctl *, struct pf_opt_queue *,
213 struct superblocks *);
214 void exclude_supersets(struct pf_rule *, struct pf_rule *);
215 int interface_group(const char *);
216 int load_feedback_profile(struct pfctl *, struct superblocks *);
217 int optimize_superblock(struct pfctl *, struct superblock *);
218 int pf_opt_create_table(struct pfctl *, struct pf_opt_tbl *);
219 void remove_from_skipsteps(struct skiplist *, struct superblock *,
220 struct pf_opt_rule *, struct pf_skip_step *);
221 int remove_identical_rules(struct pfctl *, struct superblock *);
222 int reorder_rules(struct pfctl *, struct superblock *, int);
223 int rules_combineable(struct pf_rule *, struct pf_rule *);
224 void skip_append(struct superblock *, int, struct pf_skip_step *,
225 struct pf_opt_rule *);
226 int skip_compare(int, struct pf_skip_step *, struct pf_opt_rule *);
227 void skip_init(void);
228 int skip_cmp_af(struct pf_rule *, struct pf_rule *);
229 int skip_cmp_dir(struct pf_rule *, struct pf_rule *);
230 int skip_cmp_dst_addr(struct pf_rule *, struct pf_rule *);
231 int skip_cmp_dst_port(struct pf_rule *, struct pf_rule *);
232 int skip_cmp_ifp(struct pf_rule *, struct pf_rule *);
233 int skip_cmp_proto(struct pf_rule *, struct pf_rule *);
234 int skip_cmp_src_addr(struct pf_rule *, struct pf_rule *);
235 int skip_cmp_src_port(struct pf_rule *, struct pf_rule *);
236 int superblock_inclusive(struct superblock *, struct pf_opt_rule *);
237 void superblock_free(struct pfctl *, struct superblock *);
240 int (*skip_comparitors[PF_SKIP_COUNT])(struct pf_rule *, struct pf_rule *);
241 const char *skip_comparitors_names[PF_SKIP_COUNT];
242 #define PF_SKIP_COMPARITORS { \
243 { "ifp", PF_SKIP_IFP, skip_cmp_ifp }, \
244 { "dir", PF_SKIP_DIR, skip_cmp_dir }, \
245 { "af", PF_SKIP_AF, skip_cmp_af }, \
246 { "proto", PF_SKIP_PROTO, skip_cmp_proto }, \
247 { "saddr", PF_SKIP_SRC_ADDR, skip_cmp_src_addr }, \
248 { "sport", PF_SKIP_SRC_PORT, skip_cmp_src_port }, \
249 { "daddr", PF_SKIP_DST_ADDR, skip_cmp_dst_addr }, \
250 { "dport", PF_SKIP_DST_PORT, skip_cmp_dst_port } \
253 struct pfr_buffer table_buffer;
254 int table_identifier;
258 pfctl_optimize_ruleset(struct pfctl *pf, struct pf_ruleset *rs)
260 struct superblocks superblocks;
261 struct pf_opt_queue opt_queue;
262 struct superblock *block;
263 struct pf_opt_rule *por;
265 struct pf_rulequeue *old_rules;
267 DEBUG("optimizing ruleset");
268 memset(&table_buffer, 0, sizeof(table_buffer));
270 TAILQ_INIT(&opt_queue);
272 old_rules = rs->rules[PF_RULESET_FILTER].active.ptr;
273 rs->rules[PF_RULESET_FILTER].active.ptr =
274 rs->rules[PF_RULESET_FILTER].inactive.ptr;
275 rs->rules[PF_RULESET_FILTER].inactive.ptr = old_rules;
278 * XXX expanding the pf_opt_rule format throughout pfctl might allow
279 * us to avoid all this copying.
281 while ((r = TAILQ_FIRST(rs->rules[PF_RULESET_FILTER].inactive.ptr))
283 TAILQ_REMOVE(rs->rules[PF_RULESET_FILTER].inactive.ptr, r,
285 if ((por = calloc(1, sizeof(*por))) == NULL)
287 memcpy(&por->por_rule, r, sizeof(*r));
288 if (TAILQ_FIRST(&r->rpool.list) != NULL) {
289 TAILQ_INIT(&por->por_rule.rpool.list);
290 pfctl_move_pool(&r->rpool, &por->por_rule.rpool);
292 bzero(&por->por_rule.rpool,
293 sizeof(por->por_rule.rpool));
296 TAILQ_INSERT_TAIL(&opt_queue, por, por_entry);
299 TAILQ_INIT(&superblocks);
300 if (construct_superblocks(pf, &opt_queue, &superblocks))
303 if (pf->optimize & PF_OPTIMIZE_PROFILE) {
304 if (load_feedback_profile(pf, &superblocks))
308 TAILQ_FOREACH(block, &superblocks, sb_entry) {
309 if (optimize_superblock(pf, block))
313 rs->anchor->refcnt = 0;
314 while ((block = TAILQ_FIRST(&superblocks))) {
315 TAILQ_REMOVE(&superblocks, block, sb_entry);
317 while ((por = TAILQ_FIRST(&block->sb_rules))) {
318 TAILQ_REMOVE(&block->sb_rules, por, por_entry);
319 por->por_rule.nr = rs->anchor->refcnt++;
320 if ((r = calloc(1, sizeof(*r))) == NULL)
322 memcpy(r, &por->por_rule, sizeof(*r));
323 TAILQ_INIT(&r->rpool.list);
324 pfctl_move_pool(&por->por_rule.rpool, &r->rpool);
326 rs->rules[PF_RULESET_FILTER].active.ptr,
336 while ((por = TAILQ_FIRST(&opt_queue))) {
337 TAILQ_REMOVE(&opt_queue, por, por_entry);
338 if (por->por_src_tbl) {
339 pfr_buf_clear(por->por_src_tbl->pt_buf);
340 free(por->por_src_tbl->pt_buf);
341 free(por->por_src_tbl);
343 if (por->por_dst_tbl) {
344 pfr_buf_clear(por->por_dst_tbl->pt_buf);
345 free(por->por_dst_tbl->pt_buf);
346 free(por->por_dst_tbl);
350 while ((block = TAILQ_FIRST(&superblocks))) {
351 TAILQ_REMOVE(&superblocks, block, sb_entry);
352 superblock_free(pf, block);
359 * Go ahead and optimize a superblock
362 optimize_superblock(struct pfctl *pf, struct superblock *block)
365 struct pf_opt_rule *por;
366 #endif /* OPT_DEBUG */
368 /* We have a few optimization passes:
369 * 1) remove duplicate rules or rules that are a subset of other
371 * 2) combine otherwise identical rules with different IP addresses
372 * into a single rule and put the addresses in a table.
373 * 3) re-order the rules to improve kernel skip steps
374 * 4) re-order the 'quick' rules based on feedback from the
375 * active ruleset statistics
377 * XXX combine_rules() doesn't combine v4 and v6 rules. would just
378 * have to keep af in the table container, make af 'COMBINE' and
379 * twiddle the af on the merged rule
380 * XXX maybe add a weighting to the metric on skipsteps when doing
381 * reordering. sometimes two sequential tables will be better
382 * that four consecutive interfaces.
383 * XXX need to adjust the skipstep count of everything after PROTO,
384 * since they aren't actually checked on a proto mismatch in
385 * pf_test_{tcp, udp, icmp}()
386 * XXX should i treat proto=0, af=0 or dir=0 special in skepstep
387 * calculation since they are a DC?
388 * XXX keep last skiplist of last superblock to influence this
389 * superblock. '5 inet6 log' should make '3 inet6' come before '4
390 * inet' in the next superblock.
391 * XXX would be useful to add tables for ports
392 * XXX we can also re-order some mutually exclusive superblocks to
393 * try merging superblocks before any of these optimization passes.
394 * for instance a single 'log in' rule in the middle of non-logging
398 /* shortcut. there will be alot of 1-rule superblocks */
399 if (!TAILQ_NEXT(TAILQ_FIRST(&block->sb_rules), por_entry))
403 printf("--- Superblock ---\n");
404 TAILQ_FOREACH(por, &block->sb_rules, por_entry) {
406 print_rule(&por->por_rule, por->por_rule.anchor ?
407 por->por_rule.anchor->name : "", 1);
409 #endif /* OPT_DEBUG */
412 if (remove_identical_rules(pf, block))
414 if (combine_rules(pf, block))
416 if ((pf->optimize & PF_OPTIMIZE_PROFILE) &&
417 TAILQ_FIRST(&block->sb_rules)->por_rule.quick &&
418 block->sb_profiled_block) {
419 if (block_feedback(pf, block))
421 } else if (reorder_rules(pf, block, 0)) {
426 * Don't add any optimization passes below reorder_rules(). It will
427 * have divided superblocks into smaller blocks for further refinement
428 * and doesn't put them back together again. What once was a true
429 * superblock might have been split into multiple superblocks.
433 printf("--- END Superblock ---\n");
434 #endif /* OPT_DEBUG */
440 * Optimization pass #1: remove identical rules
443 remove_identical_rules(struct pfctl *pf, struct superblock *block)
445 struct pf_opt_rule *por1, *por2, *por_next, *por2_next;
446 struct pf_rule a, a2, b, b2;
448 for (por1 = TAILQ_FIRST(&block->sb_rules); por1; por1 = por_next) {
449 por_next = TAILQ_NEXT(por1, por_entry);
450 for (por2 = por_next; por2; por2 = por2_next) {
451 por2_next = TAILQ_NEXT(por2, por_entry);
452 comparable_rule(&a, &por1->por_rule, DC);
453 comparable_rule(&b, &por2->por_rule, DC);
454 memcpy(&a2, &a, sizeof(a2));
455 memcpy(&b2, &b, sizeof(b2));
457 exclude_supersets(&a, &b);
458 exclude_supersets(&b2, &a2);
459 if (memcmp(&a, &b, sizeof(a)) == 0) {
460 DEBUG("removing identical rule nr%d = *nr%d*",
461 por1->por_rule.nr, por2->por_rule.nr);
462 TAILQ_REMOVE(&block->sb_rules, por2, por_entry);
463 if (por_next == por2)
464 por_next = TAILQ_NEXT(por1, por_entry);
466 } else if (memcmp(&a2, &b2, sizeof(a2)) == 0) {
467 DEBUG("removing identical rule *nr%d* = nr%d",
468 por1->por_rule.nr, por2->por_rule.nr);
469 TAILQ_REMOVE(&block->sb_rules, por1, por_entry);
481 * Optimization pass #2: combine similar rules with different addresses
482 * into a single rule and a table
485 combine_rules(struct pfctl *pf, struct superblock *block)
487 struct pf_opt_rule *p1, *p2, *por_next;
490 if ((pf->loadopt & PFCTL_FLAG_TABLE) == 0) {
491 warnx("Must enable table loading for optimizations");
495 /* First we make a pass to combine the rules. O(n log n) */
496 TAILQ_FOREACH(p1, &block->sb_rules, por_entry) {
497 for (p2 = TAILQ_NEXT(p1, por_entry); p2; p2 = por_next) {
498 por_next = TAILQ_NEXT(p2, por_entry);
500 src_eq = addrs_equal(&p1->por_rule.src,
502 dst_eq = addrs_equal(&p1->por_rule.dst,
505 if (src_eq && !dst_eq && p1->por_src_tbl == NULL &&
506 p2->por_dst_tbl == NULL &&
507 p2->por_src_tbl == NULL &&
508 rules_combineable(&p1->por_rule, &p2->por_rule) &&
509 addrs_combineable(&p1->por_rule.dst,
510 &p2->por_rule.dst)) {
511 DEBUG("can combine rules nr%d = nr%d",
512 p1->por_rule.nr, p2->por_rule.nr);
513 if (p1->por_dst_tbl == NULL &&
514 add_opt_table(pf, &p1->por_dst_tbl,
515 p1->por_rule.af, &p1->por_rule.dst))
517 if (add_opt_table(pf, &p1->por_dst_tbl,
518 p1->por_rule.af, &p2->por_rule.dst))
520 p2->por_dst_tbl = p1->por_dst_tbl;
521 if (p1->por_dst_tbl->pt_rulecount >=
523 TAILQ_REMOVE(&block->sb_rules, p2,
527 } else if (!src_eq && dst_eq && p1->por_dst_tbl == NULL
528 && p2->por_src_tbl == NULL &&
529 p2->por_dst_tbl == NULL &&
530 rules_combineable(&p1->por_rule, &p2->por_rule) &&
531 addrs_combineable(&p1->por_rule.src,
532 &p2->por_rule.src)) {
533 DEBUG("can combine rules nr%d = nr%d",
534 p1->por_rule.nr, p2->por_rule.nr);
535 if (p1->por_src_tbl == NULL &&
536 add_opt_table(pf, &p1->por_src_tbl,
537 p1->por_rule.af, &p1->por_rule.src))
539 if (add_opt_table(pf, &p1->por_src_tbl,
540 p1->por_rule.af, &p2->por_rule.src))
542 p2->por_src_tbl = p1->por_src_tbl;
543 if (p1->por_src_tbl->pt_rulecount >=
545 TAILQ_REMOVE(&block->sb_rules, p2,
555 * Then we make a final pass to create a valid table name and
556 * insert the name into the rules.
558 for (p1 = TAILQ_FIRST(&block->sb_rules); p1; p1 = por_next) {
559 por_next = TAILQ_NEXT(p1, por_entry);
560 assert(p1->por_src_tbl == NULL || p1->por_dst_tbl == NULL);
562 if (p1->por_src_tbl && p1->por_src_tbl->pt_rulecount >=
564 if (p1->por_src_tbl->pt_generated) {
565 /* This rule is included in a table */
566 TAILQ_REMOVE(&block->sb_rules, p1, por_entry);
570 p1->por_src_tbl->pt_generated = 1;
572 if ((pf->opts & PF_OPT_NOACTION) == 0 &&
573 pf_opt_create_table(pf, p1->por_src_tbl))
578 if (pf->opts & PF_OPT_VERBOSE)
579 print_tabledef(p1->por_src_tbl->pt_name,
581 &p1->por_src_tbl->pt_nodes);
583 memset(&p1->por_rule.src.addr, 0,
584 sizeof(p1->por_rule.src.addr));
585 p1->por_rule.src.addr.type = PF_ADDR_TABLE;
586 strlcpy(p1->por_rule.src.addr.v.tblname,
587 p1->por_src_tbl->pt_name,
588 sizeof(p1->por_rule.src.addr.v.tblname));
590 pfr_buf_clear(p1->por_src_tbl->pt_buf);
591 free(p1->por_src_tbl->pt_buf);
592 p1->por_src_tbl->pt_buf = NULL;
594 if (p1->por_dst_tbl && p1->por_dst_tbl->pt_rulecount >=
596 if (p1->por_dst_tbl->pt_generated) {
597 /* This rule is included in a table */
598 TAILQ_REMOVE(&block->sb_rules, p1, por_entry);
602 p1->por_dst_tbl->pt_generated = 1;
604 if ((pf->opts & PF_OPT_NOACTION) == 0 &&
605 pf_opt_create_table(pf, p1->por_dst_tbl))
609 if (pf->opts & PF_OPT_VERBOSE)
610 print_tabledef(p1->por_dst_tbl->pt_name,
612 &p1->por_dst_tbl->pt_nodes);
614 memset(&p1->por_rule.dst.addr, 0,
615 sizeof(p1->por_rule.dst.addr));
616 p1->por_rule.dst.addr.type = PF_ADDR_TABLE;
617 strlcpy(p1->por_rule.dst.addr.v.tblname,
618 p1->por_dst_tbl->pt_name,
619 sizeof(p1->por_rule.dst.addr.v.tblname));
621 pfr_buf_clear(p1->por_dst_tbl->pt_buf);
622 free(p1->por_dst_tbl->pt_buf);
623 p1->por_dst_tbl->pt_buf = NULL;
632 * Optimization pass #3: re-order rules to improve skip steps
635 reorder_rules(struct pfctl *pf, struct superblock *block, int depth)
637 struct superblock *newblock;
638 struct pf_skip_step *skiplist;
639 struct pf_opt_rule *por;
640 int i, largest, largest_list, rule_count = 0;
641 TAILQ_HEAD( , pf_opt_rule) head;
644 * Calculate the best-case skip steps. We put each rule in a list
645 * of other rules with common fields
647 for (i = 0; i < PF_SKIP_COUNT; i++) {
648 TAILQ_FOREACH(por, &block->sb_rules, por_entry) {
649 TAILQ_FOREACH(skiplist, &block->sb_skipsteps[i],
651 if (skip_compare(i, skiplist, por) == 0)
654 if (skiplist == NULL) {
655 if ((skiplist = calloc(1, sizeof(*skiplist))) ==
658 TAILQ_INIT(&skiplist->ps_rules);
659 TAILQ_INSERT_TAIL(&block->sb_skipsteps[i],
662 skip_append(block, i, skiplist, por);
666 TAILQ_FOREACH(por, &block->sb_rules, por_entry)
670 * Now we're going to ignore any fields that are identical between
671 * all of the rules in the superblock and those fields which differ
672 * between every rule in the superblock.
675 for (i = 0; i < PF_SKIP_COUNT; i++) {
676 skiplist = TAILQ_FIRST(&block->sb_skipsteps[i]);
677 if (skiplist->ps_count == rule_count) {
678 DEBUG("(%d) original skipstep '%s' is all rules",
679 depth, skip_comparitors_names[i]);
680 skiplist->ps_count = 0;
681 } else if (skiplist->ps_count == 1) {
682 skiplist->ps_count = 0;
684 DEBUG("(%d) original skipstep '%s' largest jump is %d",
685 depth, skip_comparitors_names[i],
687 if (skiplist->ps_count > largest)
688 largest = skiplist->ps_count;
692 /* Ugh. There is NO commonality in the superblock on which
693 * optimize the skipsteps optimization.
699 * Now we're going to empty the superblock rule list and re-create
700 * it based on a more optimal skipstep order.
703 while ((por = TAILQ_FIRST(&block->sb_rules))) {
704 TAILQ_REMOVE(&block->sb_rules, por, por_entry);
705 TAILQ_INSERT_TAIL(&head, por, por_entry);
709 while (!TAILQ_EMPTY(&head)) {
713 * Find the most useful skip steps remaining
715 for (i = 0; i < PF_SKIP_COUNT; i++) {
716 skiplist = TAILQ_FIRST(&block->sb_skipsteps[i]);
717 if (skiplist->ps_count > largest) {
718 largest = skiplist->ps_count;
725 * Nothing useful left. Leave remaining rules in order.
727 DEBUG("(%d) no more commonality for skip steps", depth);
728 while ((por = TAILQ_FIRST(&head))) {
729 TAILQ_REMOVE(&head, por, por_entry);
730 TAILQ_INSERT_TAIL(&block->sb_rules, por,
735 * There is commonality. Extract those common rules
736 * and place them in the ruleset adjacent to each
739 skiplist = TAILQ_FIRST(&block->sb_skipsteps[
741 DEBUG("(%d) skipstep '%s' largest jump is %d @ #%d",
742 depth, skip_comparitors_names[largest_list],
743 largest, TAILQ_FIRST(&TAILQ_FIRST(&block->
744 sb_skipsteps [largest_list])->ps_rules)->
746 TAILQ_REMOVE(&block->sb_skipsteps[largest_list],
751 * There may be further commonality inside these
752 * rules. So we'll split them off into they're own
753 * superblock and pass it back into the optimizer.
755 if (skiplist->ps_count > 2) {
756 if ((newblock = calloc(1, sizeof(*newblock)))
761 TAILQ_INIT(&newblock->sb_rules);
762 for (i = 0; i < PF_SKIP_COUNT; i++)
763 TAILQ_INIT(&newblock->sb_skipsteps[i]);
764 TAILQ_INSERT_BEFORE(block, newblock, sb_entry);
765 DEBUG("(%d) splitting off %d rules from superblock @ #%d",
766 depth, skiplist->ps_count,
767 TAILQ_FIRST(&skiplist->ps_rules)->
773 while ((por = TAILQ_FIRST(&skiplist->ps_rules))) {
774 TAILQ_REMOVE(&head, por, por_entry);
775 TAILQ_REMOVE(&skiplist->ps_rules, por,
776 por_skip_entry[largest_list]);
777 TAILQ_INSERT_TAIL(&newblock->sb_rules, por,
780 /* Remove this rule from all other skiplists */
781 remove_from_skipsteps(&block->sb_skipsteps[
782 largest_list], block, por, skiplist);
785 if (newblock != block)
786 if (reorder_rules(pf, newblock, depth + 1))
792 for (i = 0; i < PF_SKIP_COUNT; i++) {
793 while ((skiplist = TAILQ_FIRST(&block->sb_skipsteps[i]))) {
794 TAILQ_REMOVE(&block->sb_skipsteps[i], skiplist,
805 * Optimization pass #4: re-order 'quick' rules based on feedback from the
806 * currently running ruleset
809 block_feedback(struct pfctl *pf, struct superblock *block)
811 TAILQ_HEAD( , pf_opt_rule) queue;
812 struct pf_opt_rule *por1, *por2;
813 u_int64_t total_count = 0;
818 * Walk through all of the profiled superblock's rules and copy
819 * the counters onto our rules.
821 TAILQ_FOREACH(por1, &block->sb_profiled_block->sb_rules, por_entry) {
822 comparable_rule(&a, &por1->por_rule, DC);
823 total_count += por1->por_rule.packets[0] +
824 por1->por_rule.packets[1];
825 TAILQ_FOREACH(por2, &block->sb_rules, por_entry) {
826 if (por2->por_profile_count)
828 comparable_rule(&b, &por2->por_rule, DC);
829 if (memcmp(&a, &b, sizeof(a)) == 0) {
830 por2->por_profile_count =
831 por1->por_rule.packets[0] +
832 por1->por_rule.packets[1];
837 superblock_free(pf, block->sb_profiled_block);
838 block->sb_profiled_block = NULL;
841 * Now we pull all of the rules off the superblock and re-insert them
846 while ((por1 = TAILQ_FIRST(&block->sb_rules)) != NULL) {
847 TAILQ_REMOVE(&block->sb_rules, por1, por_entry);
848 TAILQ_INSERT_TAIL(&queue, por1, por_entry);
851 while ((por1 = TAILQ_FIRST(&queue)) != NULL) {
852 TAILQ_REMOVE(&queue, por1, por_entry);
853 /* XXX I should sort all of the unused rules based on skip steps */
854 TAILQ_FOREACH(por2, &block->sb_rules, por_entry) {
855 if (por1->por_profile_count > por2->por_profile_count) {
856 TAILQ_INSERT_BEFORE(por2, por1, por_entry);
861 TAILQ_INSERT_TAIL(&block->sb_rules, por1, por_entry);
869 * Load the current ruleset from the kernel and try to associate them with
870 * the ruleset we're optimizing.
873 load_feedback_profile(struct pfctl *pf, struct superblocks *superblocks)
875 struct superblock *block, *blockcur;
876 struct superblocks prof_superblocks;
877 struct pf_opt_rule *por;
878 struct pf_opt_queue queue;
879 struct pfioc_rule pr;
884 TAILQ_INIT(&prof_superblocks);
886 memset(&pr, 0, sizeof(pr));
887 pr.rule.action = PF_PASS;
888 if (ioctl(pf->dev, DIOCGETRULES, &pr)) {
889 warn("DIOCGETRULES");
894 DEBUG("Loading %d active rules for a feedback profile", mnr);
895 for (nr = 0; nr < mnr; ++nr) {
896 struct pf_ruleset *rs;
897 if ((por = calloc(1, sizeof(*por))) == NULL) {
902 if (ioctl(pf->dev, DIOCGETRULE, &pr)) {
903 warn("DIOCGETRULES");
906 memcpy(&por->por_rule, &pr.rule, sizeof(por->por_rule));
907 rs = pf_find_or_create_ruleset(pr.anchor_call);
908 por->por_rule.anchor = rs->anchor;
909 if (TAILQ_EMPTY(&por->por_rule.rpool.list))
910 memset(&por->por_rule.rpool, 0,
911 sizeof(por->por_rule.rpool));
912 TAILQ_INSERT_TAIL(&queue, por, por_entry);
914 /* XXX pfctl_get_pool(pf->dev, &pr.rule.rpool, nr, pr.ticket,
915 * PF_PASS, pf->anchor) ???
916 * ... pfctl_clear_pool(&pr.rule.rpool)
920 if (construct_superblocks(pf, &queue, &prof_superblocks))
925 * Now we try to associate the active ruleset's superblocks with
926 * the superblocks we're compiling.
928 block = TAILQ_FIRST(superblocks);
929 blockcur = TAILQ_FIRST(&prof_superblocks);
930 while (block && blockcur) {
931 comparable_rule(&a, &TAILQ_FIRST(&block->sb_rules)->por_rule,
933 comparable_rule(&b, &TAILQ_FIRST(&blockcur->sb_rules)->por_rule,
935 if (memcmp(&a, &b, sizeof(a)) == 0) {
936 /* The two superblocks lined up */
937 block->sb_profiled_block = blockcur;
939 DEBUG("superblocks don't line up between #%d and #%d",
940 TAILQ_FIRST(&block->sb_rules)->por_rule.nr,
941 TAILQ_FIRST(&blockcur->sb_rules)->por_rule.nr);
944 block = TAILQ_NEXT(block, sb_entry);
945 blockcur = TAILQ_NEXT(blockcur, sb_entry);
950 /* Free any superblocks we couldn't link */
952 block = TAILQ_NEXT(blockcur, sb_entry);
953 superblock_free(pf, blockcur);
961 * Compare a rule to a skiplist to see if the rule is a member
964 skip_compare(int skipnum, struct pf_skip_step *skiplist,
965 struct pf_opt_rule *por)
967 struct pf_rule *a, *b;
968 if (skipnum >= PF_SKIP_COUNT || skipnum < 0)
969 errx(1, "skip_compare() out of bounds");
971 b = &TAILQ_FIRST(&skiplist->ps_rules)->por_rule;
973 return ((skip_comparitors[skipnum])(a, b));
978 * Add a rule to a skiplist
981 skip_append(struct superblock *superblock, int skipnum,
982 struct pf_skip_step *skiplist, struct pf_opt_rule *por)
984 struct pf_skip_step *prev;
986 skiplist->ps_count++;
987 TAILQ_INSERT_TAIL(&skiplist->ps_rules, por, por_skip_entry[skipnum]);
989 /* Keep the list of skiplists sorted by whichever is larger */
990 while ((prev = TAILQ_PREV(skiplist, skiplist, ps_entry)) &&
991 prev->ps_count < skiplist->ps_count) {
992 TAILQ_REMOVE(&superblock->sb_skipsteps[skipnum],
994 TAILQ_INSERT_BEFORE(prev, skiplist, ps_entry);
1000 * Remove a rule from the other skiplist calculations.
1003 remove_from_skipsteps(struct skiplist *head, struct superblock *block,
1004 struct pf_opt_rule *por, struct pf_skip_step *active_list)
1006 struct pf_skip_step *sk, *next;
1007 struct pf_opt_rule *p2;
1010 for (i = 0; i < PF_SKIP_COUNT; i++) {
1011 sk = TAILQ_FIRST(&block->sb_skipsteps[i]);
1012 if (sk == NULL || sk == active_list || sk->ps_count <= 1)
1016 TAILQ_FOREACH(p2, &sk->ps_rules, por_skip_entry[i])
1018 TAILQ_REMOVE(&sk->ps_rules, p2,
1024 } while (!found && (sk = TAILQ_NEXT(sk, ps_entry)));
1026 /* Does this change the sorting order? */
1027 while ((next = TAILQ_NEXT(sk, ps_entry)) &&
1028 next->ps_count > sk->ps_count) {
1029 TAILQ_REMOVE(head, sk, ps_entry);
1030 TAILQ_INSERT_AFTER(head, next, sk, ps_entry);
1033 next = TAILQ_NEXT(sk, ps_entry);
1034 assert(next == NULL || next->ps_count <= sk->ps_count);
1035 #endif /* OPT_DEBUG */
1041 /* Compare two rules AF field for skiplist construction */
1043 skip_cmp_af(struct pf_rule *a, struct pf_rule *b)
1045 if (a->af != b->af || a->af == 0)
1050 /* Compare two rules DIRECTION field for skiplist construction */
1052 skip_cmp_dir(struct pf_rule *a, struct pf_rule *b)
1054 if (a->direction == 0 || a->direction != b->direction)
1059 /* Compare two rules DST Address field for skiplist construction */
1061 skip_cmp_dst_addr(struct pf_rule *a, struct pf_rule *b)
1063 if (a->dst.neg != b->dst.neg ||
1064 a->dst.addr.type != b->dst.addr.type)
1066 /* XXX if (a->proto != b->proto && a->proto != 0 && b->proto != 0
1067 * && (a->proto == IPPROTO_TCP || a->proto == IPPROTO_UDP ||
1068 * a->proto == IPPROTO_ICMP
1071 switch (a->dst.addr.type) {
1072 case PF_ADDR_ADDRMASK:
1073 if (memcmp(&a->dst.addr.v.a.addr, &b->dst.addr.v.a.addr,
1074 sizeof(a->dst.addr.v.a.addr)) ||
1075 memcmp(&a->dst.addr.v.a.mask, &b->dst.addr.v.a.mask,
1076 sizeof(a->dst.addr.v.a.mask)) ||
1077 (a->dst.addr.v.a.addr.addr32[0] == 0 &&
1078 a->dst.addr.v.a.addr.addr32[1] == 0 &&
1079 a->dst.addr.v.a.addr.addr32[2] == 0 &&
1080 a->dst.addr.v.a.addr.addr32[3] == 0))
1083 case PF_ADDR_DYNIFTL:
1084 if (strcmp(a->dst.addr.v.ifname, b->dst.addr.v.ifname) != 0 ||
1085 a->dst.addr.iflags != a->dst.addr.iflags ||
1086 memcmp(&a->dst.addr.v.a.mask, &b->dst.addr.v.a.mask,
1087 sizeof(a->dst.addr.v.a.mask)))
1090 case PF_ADDR_NOROUTE:
1091 case PF_ADDR_URPFFAILED:
1094 return (strcmp(a->dst.addr.v.tblname, b->dst.addr.v.tblname));
1099 /* Compare two rules DST port field for skiplist construction */
1101 skip_cmp_dst_port(struct pf_rule *a, struct pf_rule *b)
1103 /* XXX if (a->proto != b->proto && a->proto != 0 && b->proto != 0
1104 * && (a->proto == IPPROTO_TCP || a->proto == IPPROTO_UDP ||
1105 * a->proto == IPPROTO_ICMP
1108 if (a->dst.port_op == PF_OP_NONE || a->dst.port_op != b->dst.port_op ||
1109 a->dst.port[0] != b->dst.port[0] ||
1110 a->dst.port[1] != b->dst.port[1])
1115 /* Compare two rules IFP field for skiplist construction */
1117 skip_cmp_ifp(struct pf_rule *a, struct pf_rule *b)
1119 if (strcmp(a->ifname, b->ifname) || a->ifname[0] == '\0')
1121 return (a->ifnot != b->ifnot);
1124 /* Compare two rules PROTO field for skiplist construction */
1126 skip_cmp_proto(struct pf_rule *a, struct pf_rule *b)
1128 return (a->proto != b->proto || a->proto == 0);
1131 /* Compare two rules SRC addr field for skiplist construction */
1133 skip_cmp_src_addr(struct pf_rule *a, struct pf_rule *b)
1135 if (a->src.neg != b->src.neg ||
1136 a->src.addr.type != b->src.addr.type)
1138 /* XXX if (a->proto != b->proto && a->proto != 0 && b->proto != 0
1139 * && (a->proto == IPPROTO_TCP || a->proto == IPPROTO_UDP ||
1140 * a->proto == IPPROTO_ICMP
1143 switch (a->src.addr.type) {
1144 case PF_ADDR_ADDRMASK:
1145 if (memcmp(&a->src.addr.v.a.addr, &b->src.addr.v.a.addr,
1146 sizeof(a->src.addr.v.a.addr)) ||
1147 memcmp(&a->src.addr.v.a.mask, &b->src.addr.v.a.mask,
1148 sizeof(a->src.addr.v.a.mask)) ||
1149 (a->src.addr.v.a.addr.addr32[0] == 0 &&
1150 a->src.addr.v.a.addr.addr32[1] == 0 &&
1151 a->src.addr.v.a.addr.addr32[2] == 0 &&
1152 a->src.addr.v.a.addr.addr32[3] == 0))
1155 case PF_ADDR_DYNIFTL:
1156 if (strcmp(a->src.addr.v.ifname, b->src.addr.v.ifname) != 0 ||
1157 a->src.addr.iflags != a->src.addr.iflags ||
1158 memcmp(&a->src.addr.v.a.mask, &b->src.addr.v.a.mask,
1159 sizeof(a->src.addr.v.a.mask)))
1162 case PF_ADDR_NOROUTE:
1163 case PF_ADDR_URPFFAILED:
1166 return (strcmp(a->src.addr.v.tblname, b->src.addr.v.tblname));
1171 /* Compare two rules SRC port field for skiplist construction */
1173 skip_cmp_src_port(struct pf_rule *a, struct pf_rule *b)
1175 if (a->src.port_op == PF_OP_NONE || a->src.port_op != b->src.port_op ||
1176 a->src.port[0] != b->src.port[0] ||
1177 a->src.port[1] != b->src.port[1])
1179 /* XXX if (a->proto != b->proto && a->proto != 0 && b->proto != 0
1180 * && (a->proto == IPPROTO_TCP || a->proto == IPPROTO_UDP ||
1181 * a->proto == IPPROTO_ICMP
1194 int (*func)(struct pf_rule *, struct pf_rule *);
1195 } comps[] = PF_SKIP_COMPARITORS;
1198 for (skipnum = 0; skipnum < PF_SKIP_COUNT; skipnum++) {
1199 for (i = 0; i < sizeof(comps)/sizeof(*comps); i++)
1200 if (comps[i].skipnum == skipnum) {
1201 skip_comparitors[skipnum] = comps[i].func;
1202 skip_comparitors_names[skipnum] = comps[i].name;
1205 for (skipnum = 0; skipnum < PF_SKIP_COUNT; skipnum++)
1206 if (skip_comparitors[skipnum] == NULL)
1207 errx(1, "Need to add skip step comparitor to pfctl?!");
1211 * Add a host/netmask to a table
1214 add_opt_table(struct pfctl *pf, struct pf_opt_tbl **tbl, sa_family_t af,
1215 struct pf_rule_addr *addr)
1219 #endif /* OPT_DEBUG */
1220 static int tablenum = 0;
1221 struct node_host node_host;
1224 if ((*tbl = calloc(1, sizeof(**tbl))) == NULL ||
1225 ((*tbl)->pt_buf = calloc(1, sizeof(*(*tbl)->pt_buf))) ==
1228 (*tbl)->pt_buf->pfrb_type = PFRB_ADDRS;
1229 SIMPLEQ_INIT(&(*tbl)->pt_nodes);
1231 /* This is just a temporary table name */
1232 snprintf((*tbl)->pt_name, sizeof((*tbl)->pt_name), "%s%d",
1233 PF_OPT_TABLE_PREFIX, tablenum++);
1234 DEBUG("creating table <%s>", (*tbl)->pt_name);
1237 memset(&node_host, 0, sizeof(node_host));
1239 node_host.addr = addr->addr;
1242 DEBUG("<%s> adding %s/%d", (*tbl)->pt_name, inet_ntop(af,
1243 &node_host.addr.v.a.addr, buf, sizeof(buf)),
1244 unmask(&node_host.addr.v.a.mask, af));
1245 #endif /* OPT_DEBUG */
1247 if (append_addr_host((*tbl)->pt_buf, &node_host, 0, 0)) {
1248 warn("failed to add host");
1251 if (pf->opts & PF_OPT_VERBOSE) {
1252 struct node_tinit *ti;
1254 if ((ti = calloc(1, sizeof(*ti))) == NULL)
1256 if ((ti->host = malloc(sizeof(*ti->host))) == NULL)
1258 memcpy(ti->host, &node_host, sizeof(*ti->host));
1259 SIMPLEQ_INSERT_TAIL(&(*tbl)->pt_nodes, ti, entries);
1262 (*tbl)->pt_rulecount++;
1263 if ((*tbl)->pt_rulecount == TABLE_THRESHOLD)
1264 DEBUG("table <%s> now faster than skip steps", (*tbl)->pt_name);
1271 * Do the dirty work of choosing an unused table name and creating it.
1272 * (be careful with the table name, it might already be used in another anchor)
1275 pf_opt_create_table(struct pfctl *pf, struct pf_opt_tbl *tbl)
1277 static int tablenum;
1278 const struct pfr_table *t;
1280 if (table_buffer.pfrb_type == 0) {
1281 /* Initialize the list of tables */
1282 table_buffer.pfrb_type = PFRB_TABLES;
1284 pfr_buf_grow(&table_buffer, table_buffer.pfrb_size);
1285 table_buffer.pfrb_size = table_buffer.pfrb_msize;
1286 if (pfr_get_tables(NULL, table_buffer.pfrb_caddr,
1287 &table_buffer.pfrb_size, PFR_FLAG_ALLRSETS))
1288 err(1, "pfr_get_tables");
1289 if (table_buffer.pfrb_size <= table_buffer.pfrb_msize)
1292 table_identifier = arc4random();
1295 /* XXX would be *really* nice to avoid duplicating identical tables */
1297 /* Now we have to pick a table name that isn't used */
1299 DEBUG("translating temporary table <%s> to <%s%x_%d>", tbl->pt_name,
1300 PF_OPT_TABLE_PREFIX, table_identifier, tablenum);
1301 snprintf(tbl->pt_name, sizeof(tbl->pt_name), "%s%x_%d",
1302 PF_OPT_TABLE_PREFIX, table_identifier, tablenum);
1303 PFRB_FOREACH(t, &table_buffer) {
1304 if (strcasecmp(t->pfrt_name, tbl->pt_name) == 0) {
1305 /* Collision. Try again */
1306 DEBUG("wow, table <%s> in use. trying again",
1308 table_identifier = arc4random();
1315 if (pfctl_define_table(tbl->pt_name, PFR_TFLAG_CONST, 1,
1316 pf->anchor->name, tbl->pt_buf, pf->anchor->ruleset.tticket)) {
1317 warn("failed to create table %s", tbl->pt_name);
1324 * Partition the flat ruleset into a list of distinct superblocks
1327 construct_superblocks(struct pfctl *pf, struct pf_opt_queue *opt_queue,
1328 struct superblocks *superblocks)
1330 struct superblock *block = NULL;
1331 struct pf_opt_rule *por;
1334 while (!TAILQ_EMPTY(opt_queue)) {
1335 por = TAILQ_FIRST(opt_queue);
1336 TAILQ_REMOVE(opt_queue, por, por_entry);
1337 if (block == NULL || !superblock_inclusive(block, por)) {
1338 if ((block = calloc(1, sizeof(*block))) == NULL) {
1342 TAILQ_INIT(&block->sb_rules);
1343 for (i = 0; i < PF_SKIP_COUNT; i++)
1344 TAILQ_INIT(&block->sb_skipsteps[i]);
1345 TAILQ_INSERT_TAIL(superblocks, block, sb_entry);
1347 TAILQ_INSERT_TAIL(&block->sb_rules, por, por_entry);
1355 * Compare two rule addresses
1358 addrs_equal(struct pf_rule_addr *a, struct pf_rule_addr *b)
1360 if (a->neg != b->neg)
1362 return (memcmp(&a->addr, &b->addr, sizeof(a->addr)) == 0);
1367 * The addresses are not equal, but can we combine them into one table?
1370 addrs_combineable(struct pf_rule_addr *a, struct pf_rule_addr *b)
1372 if (a->addr.type != PF_ADDR_ADDRMASK ||
1373 b->addr.type != PF_ADDR_ADDRMASK)
1375 if (a->neg != b->neg || a->port_op != b->port_op ||
1376 a->port[0] != b->port[0] || a->port[1] != b->port[1])
1383 * Are we allowed to combine these two rules
1386 rules_combineable(struct pf_rule *p1, struct pf_rule *p2)
1388 struct pf_rule a, b;
1390 comparable_rule(&a, p1, COMBINED);
1391 comparable_rule(&b, p2, COMBINED);
1392 return (memcmp(&a, &b, sizeof(a)) == 0);
1397 * Can a rule be included inside a superblock
1400 superblock_inclusive(struct superblock *block, struct pf_opt_rule *por)
1402 struct pf_rule a, b;
1405 /* First check for hard breaks */
1406 for (i = 0; i < sizeof(pf_rule_desc)/sizeof(*pf_rule_desc); i++) {
1407 if (pf_rule_desc[i].prf_type == BARRIER) {
1408 for (j = 0; j < pf_rule_desc[i].prf_size; j++)
1409 if (((char *)&por->por_rule)[j +
1410 pf_rule_desc[i].prf_offset] != 0)
1415 /* per-rule src-track is also a hard break */
1416 if (por->por_rule.rule_flag & PFRULE_RULESRCTRACK)
1420 * Have to handle interface groups seperately. Consider the following
1422 * block on EXTIFS to any port 22
1423 * pass on em0 to any port 22
1424 * (where EXTIFS is an arbitrary interface group)
1425 * The optimizer may decide to re-order the pass rule in front of the
1426 * block rule. But what if EXTIFS includes em0??? Such a reordering
1427 * would change the meaning of the ruleset.
1428 * We can't just lookup the EXTIFS group and check if em0 is a member
1429 * because the user is allowed to add interfaces to a group during
1431 * Ergo interface groups become a defacto superblock break :-(
1433 if (interface_group(por->por_rule.ifname) ||
1434 interface_group(TAILQ_FIRST(&block->sb_rules)->por_rule.ifname)) {
1435 if (strcasecmp(por->por_rule.ifname,
1436 TAILQ_FIRST(&block->sb_rules)->por_rule.ifname) != 0)
1440 comparable_rule(&a, &TAILQ_FIRST(&block->sb_rules)->por_rule, NOMERGE);
1441 comparable_rule(&b, &por->por_rule, NOMERGE);
1442 if (memcmp(&a, &b, sizeof(a)) == 0)
1446 for (i = 0; i < sizeof(por->por_rule); i++) {
1448 if (((u_int8_t *)&a)[i] != ((u_int8_t *)&b)[i]) {
1449 for (j = 0; j < sizeof(pf_rule_desc) /
1450 sizeof(*pf_rule_desc); j++) {
1451 if (i >= pf_rule_desc[j].prf_offset &&
1452 i < pf_rule_desc[j].prf_offset +
1453 pf_rule_desc[j].prf_size) {
1454 DEBUG("superblock break @ %d due to %s",
1456 pf_rule_desc[j].prf_name);
1459 if (i > pf_rule_desc[j].prf_offset) {
1460 if (closest == -1 ||
1461 i-pf_rule_desc[j].prf_offset <
1462 i-pf_rule_desc[closest].prf_offset)
1468 DEBUG("superblock break @ %d on %s+%xh",
1470 pf_rule_desc[closest].prf_name,
1471 i - pf_rule_desc[closest].prf_offset -
1472 pf_rule_desc[closest].prf_size);
1474 DEBUG("superblock break @ %d on field @ %d",
1475 por->por_rule.nr, i);
1479 #endif /* OPT_DEBUG */
1486 * Figure out if an interface name is an actual interface or actually a
1487 * group of interfaces.
1490 interface_group(const char *ifname)
1492 if (ifname == NULL || !ifname[0])
1495 /* Real interfaces must end in a number, interface groups do not */
1496 if (isdigit(ifname[strlen(ifname) - 1]))
1504 * Make a rule that can directly compared by memcmp()
1507 comparable_rule(struct pf_rule *dst, const struct pf_rule *src, int type)
1511 * To simplify the comparison, we just zero out the fields that are
1512 * allowed to be different and then do a simple memcmp()
1514 memcpy(dst, src, sizeof(*dst));
1515 for (i = 0; i < sizeof(pf_rule_desc)/sizeof(*pf_rule_desc); i++)
1516 if (pf_rule_desc[i].prf_type >= type) {
1518 assert(pf_rule_desc[i].prf_type != NEVER ||
1519 *(((char *)dst) + pf_rule_desc[i].prf_offset) == 0);
1520 #endif /* OPT_DEBUG */
1521 memset(((char *)dst) + pf_rule_desc[i].prf_offset, 0,
1522 pf_rule_desc[i].prf_size);
1528 * Remove superset information from two rules so we can directly compare them
1532 exclude_supersets(struct pf_rule *super, struct pf_rule *sub)
1534 if (super->ifname[0] == '\0')
1535 memset(sub->ifname, 0, sizeof(sub->ifname));
1536 if (super->direction == PF_INOUT)
1537 sub->direction = PF_INOUT;
1538 if ((super->proto == 0 || super->proto == sub->proto) &&
1539 super->flags == 0 && super->flagset == 0 && (sub->flags ||
1541 sub->flags = super->flags;
1542 sub->flagset = super->flagset;
1544 if (super->proto == 0)
1547 if (super->src.port_op == 0) {
1548 sub->src.port_op = 0;
1549 sub->src.port[0] = 0;
1550 sub->src.port[1] = 0;
1552 if (super->dst.port_op == 0) {
1553 sub->dst.port_op = 0;
1554 sub->dst.port[0] = 0;
1555 sub->dst.port[1] = 0;
1558 if (super->src.addr.type == PF_ADDR_ADDRMASK && !super->src.neg &&
1559 !sub->src.neg && super->src.addr.v.a.mask.addr32[0] == 0 &&
1560 super->src.addr.v.a.mask.addr32[1] == 0 &&
1561 super->src.addr.v.a.mask.addr32[2] == 0 &&
1562 super->src.addr.v.a.mask.addr32[3] == 0)
1563 memset(&sub->src.addr, 0, sizeof(sub->src.addr));
1564 else if (super->src.addr.type == PF_ADDR_ADDRMASK &&
1565 sub->src.addr.type == PF_ADDR_ADDRMASK &&
1566 super->src.neg == sub->src.neg &&
1567 super->af == sub->af &&
1568 unmask(&super->src.addr.v.a.mask, super->af) <
1569 unmask(&sub->src.addr.v.a.mask, sub->af) &&
1570 super->src.addr.v.a.addr.addr32[0] ==
1571 (sub->src.addr.v.a.addr.addr32[0] &
1572 super->src.addr.v.a.mask.addr32[0]) &&
1573 super->src.addr.v.a.addr.addr32[1] ==
1574 (sub->src.addr.v.a.addr.addr32[1] &
1575 super->src.addr.v.a.mask.addr32[1]) &&
1576 super->src.addr.v.a.addr.addr32[2] ==
1577 (sub->src.addr.v.a.addr.addr32[2] &
1578 super->src.addr.v.a.mask.addr32[2]) &&
1579 super->src.addr.v.a.addr.addr32[3] ==
1580 (sub->src.addr.v.a.addr.addr32[3] &
1581 super->src.addr.v.a.mask.addr32[3])) {
1582 /* sub->src.addr is a subset of super->src.addr/mask */
1583 memcpy(&sub->src.addr, &super->src.addr, sizeof(sub->src.addr));
1586 if (super->dst.addr.type == PF_ADDR_ADDRMASK && !super->dst.neg &&
1587 !sub->dst.neg && super->dst.addr.v.a.mask.addr32[0] == 0 &&
1588 super->dst.addr.v.a.mask.addr32[1] == 0 &&
1589 super->dst.addr.v.a.mask.addr32[2] == 0 &&
1590 super->dst.addr.v.a.mask.addr32[3] == 0)
1591 memset(&sub->dst.addr, 0, sizeof(sub->dst.addr));
1592 else if (super->dst.addr.type == PF_ADDR_ADDRMASK &&
1593 sub->dst.addr.type == PF_ADDR_ADDRMASK &&
1594 super->dst.neg == sub->dst.neg &&
1595 super->af == sub->af &&
1596 unmask(&super->dst.addr.v.a.mask, super->af) <
1597 unmask(&sub->dst.addr.v.a.mask, sub->af) &&
1598 super->dst.addr.v.a.addr.addr32[0] ==
1599 (sub->dst.addr.v.a.addr.addr32[0] &
1600 super->dst.addr.v.a.mask.addr32[0]) &&
1601 super->dst.addr.v.a.addr.addr32[1] ==
1602 (sub->dst.addr.v.a.addr.addr32[1] &
1603 super->dst.addr.v.a.mask.addr32[1]) &&
1604 super->dst.addr.v.a.addr.addr32[2] ==
1605 (sub->dst.addr.v.a.addr.addr32[2] &
1606 super->dst.addr.v.a.mask.addr32[2]) &&
1607 super->dst.addr.v.a.addr.addr32[3] ==
1608 (sub->dst.addr.v.a.addr.addr32[3] &
1609 super->dst.addr.v.a.mask.addr32[3])) {
1610 /* sub->dst.addr is a subset of super->dst.addr/mask */
1611 memcpy(&sub->dst.addr, &super->dst.addr, sizeof(sub->dst.addr));
1620 superblock_free(struct pfctl *pf, struct superblock *block)
1622 struct pf_opt_rule *por;
1623 while ((por = TAILQ_FIRST(&block->sb_rules))) {
1624 TAILQ_REMOVE(&block->sb_rules, por, por_entry);
1625 if (por->por_src_tbl) {
1626 if (por->por_src_tbl->pt_buf) {
1627 pfr_buf_clear(por->por_src_tbl->pt_buf);
1628 free(por->por_src_tbl->pt_buf);
1630 free(por->por_src_tbl);
1632 if (por->por_dst_tbl) {
1633 if (por->por_dst_tbl->pt_buf) {
1634 pfr_buf_clear(por->por_dst_tbl->pt_buf);
1635 free(por->por_dst_tbl->pt_buf);
1637 free(por->por_dst_tbl);
1641 if (block->sb_profiled_block)
1642 superblock_free(pf, block->sb_profiled_block);