1 /* $OpenBSD: pfctl_optimize.c,v 1.17 2008/05/06 03:45:21 mpf 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/cdefs.h>
20 __FBSDID("$FreeBSD$");
22 #include <sys/types.h>
23 #include <sys/ioctl.h>
24 #include <sys/socket.h>
27 #include <net/pfvar.h>
29 #include <netinet/in.h>
30 #include <arpa/inet.h>
42 #include "pfctl_parser.h"
45 /* The size at which a table becomes faster than individual rules */
46 #define TABLE_THRESHOLD 6
49 /* #define OPT_DEBUG 1 */
51 # define DEBUG(str, v...) \
52 printf("%s: " str "\n", __FUNCTION__ , ## v)
54 # define DEBUG(str, v...) ((void)0)
59 * A container that lets us sort a superblock to optimize the skip step jumps
62 int ps_count; /* number of items */
63 TAILQ_HEAD( , pf_opt_rule) ps_rules;
64 TAILQ_ENTRY(pf_skip_step) ps_entry;
69 * A superblock is a block of adjacent rules of similar action. If there
70 * are five PASS rules in a row, they all become members of a superblock.
71 * Once we have a superblock, we are free to re-order any rules within it
72 * in order to improve performance; if a packet is passed, it doesn't matter
76 TAILQ_HEAD( , pf_opt_rule) sb_rules;
77 TAILQ_ENTRY(superblock) sb_entry;
78 struct superblock *sb_profiled_block;
79 TAILQ_HEAD(skiplist, pf_skip_step) sb_skipsteps[PF_SKIP_COUNT];
81 TAILQ_HEAD(superblocks, superblock);
85 * Description of the PF rule structure.
88 BARRIER, /* the presence of the field puts the rule in its own block */
89 BREAK, /* the field may not differ between rules in a superblock */
90 NOMERGE, /* the field may not differ between rules when combined */
91 COMBINED, /* the field may itself be combined with other rules */
92 DC, /* we just don't care about the field */
93 NEVER}; /* we should never see this field set?!? */
94 static struct pf_rule_field {
100 #define PF_RULE_FIELD(field, ty) \
103 offsetof(struct pfctl_rule, field), \
104 sizeof(((struct pfctl_rule *)0)->field)}
108 * The presence of these fields in a rule put the rule in its own
109 * superblock. Thus it will not be optimized. It also prevents the
110 * rule from being re-ordered at all.
112 PF_RULE_FIELD(label, BARRIER),
113 PF_RULE_FIELD(prob, BARRIER),
114 PF_RULE_FIELD(max_states, BARRIER),
115 PF_RULE_FIELD(max_src_nodes, BARRIER),
116 PF_RULE_FIELD(max_src_states, BARRIER),
117 PF_RULE_FIELD(max_src_conn, BARRIER),
118 PF_RULE_FIELD(max_src_conn_rate, BARRIER),
119 PF_RULE_FIELD(anchor, BARRIER), /* for now */
122 * These fields must be the same between all rules in the same superblock.
123 * These rules are allowed to be re-ordered but only among like rules.
124 * For instance we can re-order all 'tag "foo"' rules because they have the
125 * same tag. But we can not re-order between a 'tag "foo"' and a
126 * 'tag "bar"' since that would change the meaning of the ruleset.
128 PF_RULE_FIELD(tagname, BREAK),
129 PF_RULE_FIELD(keep_state, BREAK),
130 PF_RULE_FIELD(qname, BREAK),
131 PF_RULE_FIELD(pqname, BREAK),
132 PF_RULE_FIELD(rt, BREAK),
133 PF_RULE_FIELD(allow_opts, BREAK),
134 PF_RULE_FIELD(rule_flag, BREAK),
135 PF_RULE_FIELD(action, BREAK),
136 PF_RULE_FIELD(log, BREAK),
137 PF_RULE_FIELD(quick, BREAK),
138 PF_RULE_FIELD(return_ttl, BREAK),
139 PF_RULE_FIELD(overload_tblname, BREAK),
140 PF_RULE_FIELD(flush, BREAK),
141 PF_RULE_FIELD(rpool, BREAK),
142 PF_RULE_FIELD(logif, BREAK),
145 * Any fields not listed in this structure act as BREAK fields
150 * These fields must not differ when we merge two rules together but
151 * their difference isn't enough to put the rules in different superblocks.
152 * There are no problems re-ordering any rules with these fields.
154 PF_RULE_FIELD(af, NOMERGE),
155 PF_RULE_FIELD(ifnot, NOMERGE),
156 PF_RULE_FIELD(ifname, NOMERGE), /* hack for IF groups */
157 PF_RULE_FIELD(match_tag_not, NOMERGE),
158 PF_RULE_FIELD(match_tagname, NOMERGE),
159 PF_RULE_FIELD(os_fingerprint, NOMERGE),
160 PF_RULE_FIELD(timeout, NOMERGE),
161 PF_RULE_FIELD(return_icmp, NOMERGE),
162 PF_RULE_FIELD(return_icmp6, NOMERGE),
163 PF_RULE_FIELD(uid, NOMERGE),
164 PF_RULE_FIELD(gid, NOMERGE),
165 PF_RULE_FIELD(direction, NOMERGE),
166 PF_RULE_FIELD(proto, NOMERGE),
167 PF_RULE_FIELD(type, NOMERGE),
168 PF_RULE_FIELD(code, NOMERGE),
169 PF_RULE_FIELD(flags, NOMERGE),
170 PF_RULE_FIELD(flagset, NOMERGE),
171 PF_RULE_FIELD(tos, NOMERGE),
172 PF_RULE_FIELD(src.port, NOMERGE),
173 PF_RULE_FIELD(dst.port, NOMERGE),
174 PF_RULE_FIELD(src.port_op, NOMERGE),
175 PF_RULE_FIELD(dst.port_op, NOMERGE),
176 PF_RULE_FIELD(src.neg, NOMERGE),
177 PF_RULE_FIELD(dst.neg, NOMERGE),
179 /* These fields can be merged */
180 PF_RULE_FIELD(src.addr, COMBINED),
181 PF_RULE_FIELD(dst.addr, COMBINED),
183 /* We just don't care about these fields. They're set by the kernel */
184 PF_RULE_FIELD(skip, DC),
185 PF_RULE_FIELD(evaluations, DC),
186 PF_RULE_FIELD(packets, DC),
187 PF_RULE_FIELD(bytes, DC),
188 PF_RULE_FIELD(kif, DC),
189 PF_RULE_FIELD(states_cur, DC),
190 PF_RULE_FIELD(states_tot, DC),
191 PF_RULE_FIELD(src_nodes, DC),
192 PF_RULE_FIELD(nr, DC),
193 PF_RULE_FIELD(entries, DC),
194 PF_RULE_FIELD(qid, DC),
195 PF_RULE_FIELD(pqid, DC),
196 PF_RULE_FIELD(anchor_relative, DC),
197 PF_RULE_FIELD(anchor_wildcard, DC),
198 PF_RULE_FIELD(tag, DC),
199 PF_RULE_FIELD(match_tag, DC),
200 PF_RULE_FIELD(overload_tbl, DC),
202 /* These fields should never be set in a PASS/BLOCK rule */
203 PF_RULE_FIELD(natpass, NEVER),
204 PF_RULE_FIELD(max_mss, NEVER),
205 PF_RULE_FIELD(min_ttl, NEVER),
206 PF_RULE_FIELD(set_tos, NEVER),
211 int add_opt_table(struct pfctl *, struct pf_opt_tbl **, sa_family_t,
212 struct pf_rule_addr *);
213 int addrs_combineable(struct pf_rule_addr *, struct pf_rule_addr *);
214 int addrs_equal(struct pf_rule_addr *, struct pf_rule_addr *);
215 int block_feedback(struct pfctl *, struct superblock *);
216 int combine_rules(struct pfctl *, struct superblock *);
217 void comparable_rule(struct pfctl_rule *, const struct pfctl_rule *, int);
218 int construct_superblocks(struct pfctl *, struct pf_opt_queue *,
219 struct superblocks *);
220 void exclude_supersets(struct pfctl_rule *, struct pfctl_rule *);
221 int interface_group(const char *);
222 int load_feedback_profile(struct pfctl *, struct superblocks *);
223 int optimize_superblock(struct pfctl *, struct superblock *);
224 int pf_opt_create_table(struct pfctl *, struct pf_opt_tbl *);
225 void remove_from_skipsteps(struct skiplist *, struct superblock *,
226 struct pf_opt_rule *, struct pf_skip_step *);
227 int remove_identical_rules(struct pfctl *, struct superblock *);
228 int reorder_rules(struct pfctl *, struct superblock *, int);
229 int rules_combineable(struct pfctl_rule *, struct pfctl_rule *);
230 void skip_append(struct superblock *, int, struct pf_skip_step *,
231 struct pf_opt_rule *);
232 int skip_compare(int, struct pf_skip_step *, struct pf_opt_rule *);
233 void skip_init(void);
234 int skip_cmp_af(struct pfctl_rule *, struct pfctl_rule *);
235 int skip_cmp_dir(struct pfctl_rule *, struct pfctl_rule *);
236 int skip_cmp_dst_addr(struct pfctl_rule *, struct pfctl_rule *);
237 int skip_cmp_dst_port(struct pfctl_rule *, struct pfctl_rule *);
238 int skip_cmp_ifp(struct pfctl_rule *, struct pfctl_rule *);
239 int skip_cmp_proto(struct pfctl_rule *, struct pfctl_rule *);
240 int skip_cmp_src_addr(struct pfctl_rule *, struct pfctl_rule *);
241 int skip_cmp_src_port(struct pfctl_rule *, struct pfctl_rule *);
242 int superblock_inclusive(struct superblock *, struct pf_opt_rule *);
243 void superblock_free(struct pfctl *, struct superblock *);
246 static int (*skip_comparitors[PF_SKIP_COUNT])(struct pfctl_rule *,
247 struct pfctl_rule *);
248 static const char *skip_comparitors_names[PF_SKIP_COUNT];
249 #define PF_SKIP_COMPARITORS { \
250 { "ifp", PF_SKIP_IFP, skip_cmp_ifp }, \
251 { "dir", PF_SKIP_DIR, skip_cmp_dir }, \
252 { "af", PF_SKIP_AF, skip_cmp_af }, \
253 { "proto", PF_SKIP_PROTO, skip_cmp_proto }, \
254 { "saddr", PF_SKIP_SRC_ADDR, skip_cmp_src_addr }, \
255 { "sport", PF_SKIP_SRC_PORT, skip_cmp_src_port }, \
256 { "daddr", PF_SKIP_DST_ADDR, skip_cmp_dst_addr }, \
257 { "dport", PF_SKIP_DST_PORT, skip_cmp_dst_port } \
260 static struct pfr_buffer table_buffer;
261 static int table_identifier;
265 pfctl_optimize_ruleset(struct pfctl *pf, struct pfctl_ruleset *rs)
267 struct superblocks superblocks;
268 struct pf_opt_queue opt_queue;
269 struct superblock *block;
270 struct pf_opt_rule *por;
271 struct pfctl_rule *r;
272 struct pfctl_rulequeue *old_rules;
274 DEBUG("optimizing ruleset");
275 memset(&table_buffer, 0, sizeof(table_buffer));
277 TAILQ_INIT(&opt_queue);
279 old_rules = rs->rules[PF_RULESET_FILTER].active.ptr;
280 rs->rules[PF_RULESET_FILTER].active.ptr =
281 rs->rules[PF_RULESET_FILTER].inactive.ptr;
282 rs->rules[PF_RULESET_FILTER].inactive.ptr = old_rules;
285 * XXX expanding the pf_opt_rule format throughout pfctl might allow
286 * us to avoid all this copying.
288 while ((r = TAILQ_FIRST(rs->rules[PF_RULESET_FILTER].inactive.ptr))
290 TAILQ_REMOVE(rs->rules[PF_RULESET_FILTER].inactive.ptr, r,
292 if ((por = calloc(1, sizeof(*por))) == NULL)
294 memcpy(&por->por_rule, r, sizeof(*r));
295 if (TAILQ_FIRST(&r->rpool.list) != NULL) {
296 TAILQ_INIT(&por->por_rule.rpool.list);
297 pfctl_move_pool(&r->rpool, &por->por_rule.rpool);
299 bzero(&por->por_rule.rpool,
300 sizeof(por->por_rule.rpool));
303 TAILQ_INSERT_TAIL(&opt_queue, por, por_entry);
306 TAILQ_INIT(&superblocks);
307 if (construct_superblocks(pf, &opt_queue, &superblocks))
310 if (pf->optimize & PF_OPTIMIZE_PROFILE) {
311 if (load_feedback_profile(pf, &superblocks))
315 TAILQ_FOREACH(block, &superblocks, sb_entry) {
316 if (optimize_superblock(pf, block))
320 rs->anchor->refcnt = 0;
321 while ((block = TAILQ_FIRST(&superblocks))) {
322 TAILQ_REMOVE(&superblocks, block, sb_entry);
324 while ((por = TAILQ_FIRST(&block->sb_rules))) {
325 TAILQ_REMOVE(&block->sb_rules, por, por_entry);
326 por->por_rule.nr = rs->anchor->refcnt++;
327 if ((r = calloc(1, sizeof(*r))) == NULL)
329 memcpy(r, &por->por_rule, sizeof(*r));
330 TAILQ_INIT(&r->rpool.list);
331 pfctl_move_pool(&por->por_rule.rpool, &r->rpool);
333 rs->rules[PF_RULESET_FILTER].active.ptr,
343 while ((por = TAILQ_FIRST(&opt_queue))) {
344 TAILQ_REMOVE(&opt_queue, por, por_entry);
345 if (por->por_src_tbl) {
346 pfr_buf_clear(por->por_src_tbl->pt_buf);
347 free(por->por_src_tbl->pt_buf);
348 free(por->por_src_tbl);
350 if (por->por_dst_tbl) {
351 pfr_buf_clear(por->por_dst_tbl->pt_buf);
352 free(por->por_dst_tbl->pt_buf);
353 free(por->por_dst_tbl);
357 while ((block = TAILQ_FIRST(&superblocks))) {
358 TAILQ_REMOVE(&superblocks, block, sb_entry);
359 superblock_free(pf, block);
366 * Go ahead and optimize a superblock
369 optimize_superblock(struct pfctl *pf, struct superblock *block)
372 struct pf_opt_rule *por;
373 #endif /* OPT_DEBUG */
375 /* We have a few optimization passes:
376 * 1) remove duplicate rules or rules that are a subset of other
378 * 2) combine otherwise identical rules with different IP addresses
379 * into a single rule and put the addresses in a table.
380 * 3) re-order the rules to improve kernel skip steps
381 * 4) re-order the 'quick' rules based on feedback from the
382 * active ruleset statistics
384 * XXX combine_rules() doesn't combine v4 and v6 rules. would just
385 * have to keep af in the table container, make af 'COMBINE' and
386 * twiddle the af on the merged rule
387 * XXX maybe add a weighting to the metric on skipsteps when doing
388 * reordering. sometimes two sequential tables will be better
389 * that four consecutive interfaces.
390 * XXX need to adjust the skipstep count of everything after PROTO,
391 * since they aren't actually checked on a proto mismatch in
392 * pf_test_{tcp, udp, icmp}()
393 * XXX should i treat proto=0, af=0 or dir=0 special in skepstep
394 * calculation since they are a DC?
395 * XXX keep last skiplist of last superblock to influence this
396 * superblock. '5 inet6 log' should make '3 inet6' come before '4
397 * inet' in the next superblock.
398 * XXX would be useful to add tables for ports
399 * XXX we can also re-order some mutually exclusive superblocks to
400 * try merging superblocks before any of these optimization passes.
401 * for instance a single 'log in' rule in the middle of non-logging
405 /* shortcut. there will be a lot of 1-rule superblocks */
406 if (!TAILQ_NEXT(TAILQ_FIRST(&block->sb_rules), por_entry))
410 printf("--- Superblock ---\n");
411 TAILQ_FOREACH(por, &block->sb_rules, por_entry) {
413 print_rule(&por->por_rule, por->por_rule.anchor ?
414 por->por_rule.anchor->name : "", 1, 0);
416 #endif /* OPT_DEBUG */
419 if (remove_identical_rules(pf, block))
421 if (combine_rules(pf, block))
423 if ((pf->optimize & PF_OPTIMIZE_PROFILE) &&
424 TAILQ_FIRST(&block->sb_rules)->por_rule.quick &&
425 block->sb_profiled_block) {
426 if (block_feedback(pf, block))
428 } else if (reorder_rules(pf, block, 0)) {
433 * Don't add any optimization passes below reorder_rules(). It will
434 * have divided superblocks into smaller blocks for further refinement
435 * and doesn't put them back together again. What once was a true
436 * superblock might have been split into multiple superblocks.
440 printf("--- END Superblock ---\n");
441 #endif /* OPT_DEBUG */
447 * Optimization pass #1: remove identical rules
450 remove_identical_rules(struct pfctl *pf, struct superblock *block)
452 struct pf_opt_rule *por1, *por2, *por_next, *por2_next;
453 struct pfctl_rule a, a2, b, b2;
455 for (por1 = TAILQ_FIRST(&block->sb_rules); por1; por1 = por_next) {
456 por_next = TAILQ_NEXT(por1, por_entry);
457 for (por2 = por_next; por2; por2 = por2_next) {
458 por2_next = TAILQ_NEXT(por2, por_entry);
459 comparable_rule(&a, &por1->por_rule, DC);
460 comparable_rule(&b, &por2->por_rule, DC);
461 memcpy(&a2, &a, sizeof(a2));
462 memcpy(&b2, &b, sizeof(b2));
464 exclude_supersets(&a, &b);
465 exclude_supersets(&b2, &a2);
466 if (memcmp(&a, &b, sizeof(a)) == 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, por2, por_entry);
470 if (por_next == por2)
471 por_next = TAILQ_NEXT(por1, por_entry);
473 } else if (memcmp(&a2, &b2, sizeof(a2)) == 0) {
474 DEBUG("removing identical rule *nr%d* = nr%d",
475 por1->por_rule.nr, por2->por_rule.nr);
476 TAILQ_REMOVE(&block->sb_rules, por1, por_entry);
488 * Optimization pass #2: combine similar rules with different addresses
489 * into a single rule and a table
492 combine_rules(struct pfctl *pf, struct superblock *block)
494 struct pf_opt_rule *p1, *p2, *por_next;
497 if ((pf->loadopt & PFCTL_FLAG_TABLE) == 0) {
498 warnx("Must enable table loading for optimizations");
502 /* First we make a pass to combine the rules. O(n log n) */
503 TAILQ_FOREACH(p1, &block->sb_rules, por_entry) {
504 for (p2 = TAILQ_NEXT(p1, por_entry); p2; p2 = por_next) {
505 por_next = TAILQ_NEXT(p2, por_entry);
507 src_eq = addrs_equal(&p1->por_rule.src,
509 dst_eq = addrs_equal(&p1->por_rule.dst,
512 if (src_eq && !dst_eq && p1->por_src_tbl == NULL &&
513 p2->por_dst_tbl == NULL &&
514 p2->por_src_tbl == NULL &&
515 rules_combineable(&p1->por_rule, &p2->por_rule) &&
516 addrs_combineable(&p1->por_rule.dst,
517 &p2->por_rule.dst)) {
518 DEBUG("can combine rules nr%d = nr%d",
519 p1->por_rule.nr, p2->por_rule.nr);
520 if (p1->por_dst_tbl == NULL &&
521 add_opt_table(pf, &p1->por_dst_tbl,
522 p1->por_rule.af, &p1->por_rule.dst))
524 if (add_opt_table(pf, &p1->por_dst_tbl,
525 p1->por_rule.af, &p2->por_rule.dst))
527 p2->por_dst_tbl = p1->por_dst_tbl;
528 if (p1->por_dst_tbl->pt_rulecount >=
530 TAILQ_REMOVE(&block->sb_rules, p2,
534 } else if (!src_eq && dst_eq && p1->por_dst_tbl == NULL
535 && p2->por_src_tbl == NULL &&
536 p2->por_dst_tbl == NULL &&
537 rules_combineable(&p1->por_rule, &p2->por_rule) &&
538 addrs_combineable(&p1->por_rule.src,
539 &p2->por_rule.src)) {
540 DEBUG("can combine rules nr%d = nr%d",
541 p1->por_rule.nr, p2->por_rule.nr);
542 if (p1->por_src_tbl == NULL &&
543 add_opt_table(pf, &p1->por_src_tbl,
544 p1->por_rule.af, &p1->por_rule.src))
546 if (add_opt_table(pf, &p1->por_src_tbl,
547 p1->por_rule.af, &p2->por_rule.src))
549 p2->por_src_tbl = p1->por_src_tbl;
550 if (p1->por_src_tbl->pt_rulecount >=
552 TAILQ_REMOVE(&block->sb_rules, p2,
562 * Then we make a final pass to create a valid table name and
563 * insert the name into the rules.
565 for (p1 = TAILQ_FIRST(&block->sb_rules); p1; p1 = por_next) {
566 por_next = TAILQ_NEXT(p1, por_entry);
567 assert(p1->por_src_tbl == NULL || p1->por_dst_tbl == NULL);
569 if (p1->por_src_tbl && p1->por_src_tbl->pt_rulecount >=
571 if (p1->por_src_tbl->pt_generated) {
572 /* This rule is included in a table */
573 TAILQ_REMOVE(&block->sb_rules, p1, por_entry);
577 p1->por_src_tbl->pt_generated = 1;
579 if ((pf->opts & PF_OPT_NOACTION) == 0 &&
580 pf_opt_create_table(pf, p1->por_src_tbl))
585 if (pf->opts & PF_OPT_VERBOSE)
586 print_tabledef(p1->por_src_tbl->pt_name,
588 &p1->por_src_tbl->pt_nodes);
590 memset(&p1->por_rule.src.addr, 0,
591 sizeof(p1->por_rule.src.addr));
592 p1->por_rule.src.addr.type = PF_ADDR_TABLE;
593 strlcpy(p1->por_rule.src.addr.v.tblname,
594 p1->por_src_tbl->pt_name,
595 sizeof(p1->por_rule.src.addr.v.tblname));
597 pfr_buf_clear(p1->por_src_tbl->pt_buf);
598 free(p1->por_src_tbl->pt_buf);
599 p1->por_src_tbl->pt_buf = NULL;
601 if (p1->por_dst_tbl && p1->por_dst_tbl->pt_rulecount >=
603 if (p1->por_dst_tbl->pt_generated) {
604 /* This rule is included in a table */
605 TAILQ_REMOVE(&block->sb_rules, p1, por_entry);
609 p1->por_dst_tbl->pt_generated = 1;
611 if ((pf->opts & PF_OPT_NOACTION) == 0 &&
612 pf_opt_create_table(pf, p1->por_dst_tbl))
616 if (pf->opts & PF_OPT_VERBOSE)
617 print_tabledef(p1->por_dst_tbl->pt_name,
619 &p1->por_dst_tbl->pt_nodes);
621 memset(&p1->por_rule.dst.addr, 0,
622 sizeof(p1->por_rule.dst.addr));
623 p1->por_rule.dst.addr.type = PF_ADDR_TABLE;
624 strlcpy(p1->por_rule.dst.addr.v.tblname,
625 p1->por_dst_tbl->pt_name,
626 sizeof(p1->por_rule.dst.addr.v.tblname));
628 pfr_buf_clear(p1->por_dst_tbl->pt_buf);
629 free(p1->por_dst_tbl->pt_buf);
630 p1->por_dst_tbl->pt_buf = NULL;
639 * Optimization pass #3: re-order rules to improve skip steps
642 reorder_rules(struct pfctl *pf, struct superblock *block, int depth)
644 struct superblock *newblock;
645 struct pf_skip_step *skiplist;
646 struct pf_opt_rule *por;
647 int i, largest, largest_list, rule_count = 0;
648 TAILQ_HEAD( , pf_opt_rule) head;
651 * Calculate the best-case skip steps. We put each rule in a list
652 * of other rules with common fields
654 for (i = 0; i < PF_SKIP_COUNT; i++) {
655 TAILQ_FOREACH(por, &block->sb_rules, por_entry) {
656 TAILQ_FOREACH(skiplist, &block->sb_skipsteps[i],
658 if (skip_compare(i, skiplist, por) == 0)
661 if (skiplist == NULL) {
662 if ((skiplist = calloc(1, sizeof(*skiplist))) ==
665 TAILQ_INIT(&skiplist->ps_rules);
666 TAILQ_INSERT_TAIL(&block->sb_skipsteps[i],
669 skip_append(block, i, skiplist, por);
673 TAILQ_FOREACH(por, &block->sb_rules, por_entry)
677 * Now we're going to ignore any fields that are identical between
678 * all of the rules in the superblock and those fields which differ
679 * between every rule in the superblock.
682 for (i = 0; i < PF_SKIP_COUNT; i++) {
683 skiplist = TAILQ_FIRST(&block->sb_skipsteps[i]);
684 if (skiplist->ps_count == rule_count) {
685 DEBUG("(%d) original skipstep '%s' is all rules",
686 depth, skip_comparitors_names[i]);
687 skiplist->ps_count = 0;
688 } else if (skiplist->ps_count == 1) {
689 skiplist->ps_count = 0;
691 DEBUG("(%d) original skipstep '%s' largest jump is %d",
692 depth, skip_comparitors_names[i],
694 if (skiplist->ps_count > largest)
695 largest = skiplist->ps_count;
699 /* Ugh. There is NO commonality in the superblock on which
700 * optimize the skipsteps optimization.
706 * Now we're going to empty the superblock rule list and re-create
707 * it based on a more optimal skipstep order.
710 while ((por = TAILQ_FIRST(&block->sb_rules))) {
711 TAILQ_REMOVE(&block->sb_rules, por, por_entry);
712 TAILQ_INSERT_TAIL(&head, por, por_entry);
716 while (!TAILQ_EMPTY(&head)) {
720 * Find the most useful skip steps remaining
722 for (i = 0; i < PF_SKIP_COUNT; i++) {
723 skiplist = TAILQ_FIRST(&block->sb_skipsteps[i]);
724 if (skiplist->ps_count > largest) {
725 largest = skiplist->ps_count;
732 * Nothing useful left. Leave remaining rules in order.
734 DEBUG("(%d) no more commonality for skip steps", depth);
735 while ((por = TAILQ_FIRST(&head))) {
736 TAILQ_REMOVE(&head, por, por_entry);
737 TAILQ_INSERT_TAIL(&block->sb_rules, por,
742 * There is commonality. Extract those common rules
743 * and place them in the ruleset adjacent to each
746 skiplist = TAILQ_FIRST(&block->sb_skipsteps[
748 DEBUG("(%d) skipstep '%s' largest jump is %d @ #%d",
749 depth, skip_comparitors_names[largest_list],
750 largest, TAILQ_FIRST(&TAILQ_FIRST(&block->
751 sb_skipsteps [largest_list])->ps_rules)->
753 TAILQ_REMOVE(&block->sb_skipsteps[largest_list],
758 * There may be further commonality inside these
759 * rules. So we'll split them off into they're own
760 * superblock and pass it back into the optimizer.
762 if (skiplist->ps_count > 2) {
763 if ((newblock = calloc(1, sizeof(*newblock)))
768 TAILQ_INIT(&newblock->sb_rules);
769 for (i = 0; i < PF_SKIP_COUNT; i++)
770 TAILQ_INIT(&newblock->sb_skipsteps[i]);
771 TAILQ_INSERT_BEFORE(block, newblock, sb_entry);
772 DEBUG("(%d) splitting off %d rules from superblock @ #%d",
773 depth, skiplist->ps_count,
774 TAILQ_FIRST(&skiplist->ps_rules)->
780 while ((por = TAILQ_FIRST(&skiplist->ps_rules))) {
781 TAILQ_REMOVE(&head, por, por_entry);
782 TAILQ_REMOVE(&skiplist->ps_rules, por,
783 por_skip_entry[largest_list]);
784 TAILQ_INSERT_TAIL(&newblock->sb_rules, por,
787 /* Remove this rule from all other skiplists */
788 remove_from_skipsteps(&block->sb_skipsteps[
789 largest_list], block, por, skiplist);
792 if (newblock != block)
793 if (reorder_rules(pf, newblock, depth + 1))
799 for (i = 0; i < PF_SKIP_COUNT; i++) {
800 while ((skiplist = TAILQ_FIRST(&block->sb_skipsteps[i]))) {
801 TAILQ_REMOVE(&block->sb_skipsteps[i], skiplist,
812 * Optimization pass #4: re-order 'quick' rules based on feedback from the
813 * currently running ruleset
816 block_feedback(struct pfctl *pf, struct superblock *block)
818 TAILQ_HEAD( , pf_opt_rule) queue;
819 struct pf_opt_rule *por1, *por2;
820 struct pfctl_rule a, b;
824 * Walk through all of the profiled superblock's rules and copy
825 * the counters onto our rules.
827 TAILQ_FOREACH(por1, &block->sb_profiled_block->sb_rules, por_entry) {
828 comparable_rule(&a, &por1->por_rule, DC);
829 TAILQ_FOREACH(por2, &block->sb_rules, por_entry) {
830 if (por2->por_profile_count)
832 comparable_rule(&b, &por2->por_rule, DC);
833 if (memcmp(&a, &b, sizeof(a)) == 0) {
834 por2->por_profile_count =
835 por1->por_rule.packets[0] +
836 por1->por_rule.packets[1];
841 superblock_free(pf, block->sb_profiled_block);
842 block->sb_profiled_block = NULL;
845 * Now we pull all of the rules off the superblock and re-insert them
850 while ((por1 = TAILQ_FIRST(&block->sb_rules)) != NULL) {
851 TAILQ_REMOVE(&block->sb_rules, por1, por_entry);
852 TAILQ_INSERT_TAIL(&queue, por1, por_entry);
855 while ((por1 = TAILQ_FIRST(&queue)) != NULL) {
856 TAILQ_REMOVE(&queue, por1, por_entry);
857 /* XXX I should sort all of the unused rules based on skip steps */
858 TAILQ_FOREACH(por2, &block->sb_rules, por_entry) {
859 if (por1->por_profile_count > por2->por_profile_count) {
860 TAILQ_INSERT_BEFORE(por2, por1, por_entry);
867 if (por2 == TAILQ_END(&block->sb_rules))
869 TAILQ_INSERT_TAIL(&block->sb_rules, por1, por_entry);
877 * Load the current ruleset from the kernel and try to associate them with
878 * the ruleset we're optimizing.
881 load_feedback_profile(struct pfctl *pf, struct superblocks *superblocks)
883 struct superblock *block, *blockcur;
884 struct superblocks prof_superblocks;
885 struct pf_opt_rule *por;
886 struct pf_opt_queue queue;
887 struct pfioc_rule pr;
888 struct pfctl_rule a, b, rule;
892 TAILQ_INIT(&prof_superblocks);
894 memset(&pr, 0, sizeof(pr));
895 pr.rule.action = PF_PASS;
896 if (ioctl(pf->dev, DIOCGETRULES, &pr)) {
897 warn("DIOCGETRULES");
902 DEBUG("Loading %d active rules for a feedback profile", mnr);
903 for (nr = 0; nr < mnr; ++nr) {
904 struct pfctl_ruleset *rs;
905 if ((por = calloc(1, sizeof(*por))) == NULL) {
911 if (pfctl_get_rule(pf->dev, nr, pr.ticket, "", PF_PASS,
912 &rule, pr.anchor_call)) {
913 warn("DIOCGETRULENV");
916 memcpy(&por->por_rule, &rule, sizeof(por->por_rule));
917 rs = pf_find_or_create_ruleset(pr.anchor_call);
918 por->por_rule.anchor = rs->anchor;
919 if (TAILQ_EMPTY(&por->por_rule.rpool.list))
920 memset(&por->por_rule.rpool, 0,
921 sizeof(por->por_rule.rpool));
922 TAILQ_INSERT_TAIL(&queue, por, por_entry);
924 /* XXX pfctl_get_pool(pf->dev, &rule.rpool, nr, pr.ticket,
925 * PF_PASS, pf->anchor) ???
926 * ... pfctl_clear_pool(&rule.rpool)
930 if (construct_superblocks(pf, &queue, &prof_superblocks))
935 * Now we try to associate the active ruleset's superblocks with
936 * the superblocks we're compiling.
938 block = TAILQ_FIRST(superblocks);
939 blockcur = TAILQ_FIRST(&prof_superblocks);
940 while (block && blockcur) {
941 comparable_rule(&a, &TAILQ_FIRST(&block->sb_rules)->por_rule,
943 comparable_rule(&b, &TAILQ_FIRST(&blockcur->sb_rules)->por_rule,
945 if (memcmp(&a, &b, sizeof(a)) == 0) {
946 /* The two superblocks lined up */
947 block->sb_profiled_block = blockcur;
949 DEBUG("superblocks don't line up between #%d and #%d",
950 TAILQ_FIRST(&block->sb_rules)->por_rule.nr,
951 TAILQ_FIRST(&blockcur->sb_rules)->por_rule.nr);
954 block = TAILQ_NEXT(block, sb_entry);
955 blockcur = TAILQ_NEXT(blockcur, sb_entry);
960 /* Free any superblocks we couldn't link */
962 block = TAILQ_NEXT(blockcur, sb_entry);
963 superblock_free(pf, blockcur);
971 * Compare a rule to a skiplist to see if the rule is a member
974 skip_compare(int skipnum, struct pf_skip_step *skiplist,
975 struct pf_opt_rule *por)
977 struct pfctl_rule *a, *b;
978 if (skipnum >= PF_SKIP_COUNT || skipnum < 0)
979 errx(1, "skip_compare() out of bounds");
981 b = &TAILQ_FIRST(&skiplist->ps_rules)->por_rule;
983 return ((skip_comparitors[skipnum])(a, b));
988 * Add a rule to a skiplist
991 skip_append(struct superblock *superblock, int skipnum,
992 struct pf_skip_step *skiplist, struct pf_opt_rule *por)
994 struct pf_skip_step *prev;
996 skiplist->ps_count++;
997 TAILQ_INSERT_TAIL(&skiplist->ps_rules, por, por_skip_entry[skipnum]);
999 /* Keep the list of skiplists sorted by whichever is larger */
1000 while ((prev = TAILQ_PREV(skiplist, skiplist, ps_entry)) &&
1001 prev->ps_count < skiplist->ps_count) {
1002 TAILQ_REMOVE(&superblock->sb_skipsteps[skipnum],
1003 skiplist, ps_entry);
1004 TAILQ_INSERT_BEFORE(prev, skiplist, ps_entry);
1010 * Remove a rule from the other skiplist calculations.
1013 remove_from_skipsteps(struct skiplist *head, struct superblock *block,
1014 struct pf_opt_rule *por, struct pf_skip_step *active_list)
1016 struct pf_skip_step *sk, *next;
1017 struct pf_opt_rule *p2;
1020 for (i = 0; i < PF_SKIP_COUNT; i++) {
1021 sk = TAILQ_FIRST(&block->sb_skipsteps[i]);
1022 if (sk == NULL || sk == active_list || sk->ps_count <= 1)
1026 TAILQ_FOREACH(p2, &sk->ps_rules, por_skip_entry[i])
1028 TAILQ_REMOVE(&sk->ps_rules, p2,
1034 } while (!found && (sk = TAILQ_NEXT(sk, ps_entry)));
1036 /* Does this change the sorting order? */
1037 while ((next = TAILQ_NEXT(sk, ps_entry)) &&
1038 next->ps_count > sk->ps_count) {
1039 TAILQ_REMOVE(head, sk, ps_entry);
1040 TAILQ_INSERT_AFTER(head, next, sk, ps_entry);
1043 next = TAILQ_NEXT(sk, ps_entry);
1044 assert(next == NULL || next->ps_count <= sk->ps_count);
1045 #endif /* OPT_DEBUG */
1051 /* Compare two rules AF field for skiplist construction */
1053 skip_cmp_af(struct pfctl_rule *a, struct pfctl_rule *b)
1055 if (a->af != b->af || a->af == 0)
1060 /* Compare two rules DIRECTION field for skiplist construction */
1062 skip_cmp_dir(struct pfctl_rule *a, struct pfctl_rule *b)
1064 if (a->direction == 0 || a->direction != b->direction)
1069 /* Compare two rules DST Address field for skiplist construction */
1071 skip_cmp_dst_addr(struct pfctl_rule *a, struct pfctl_rule *b)
1073 if (a->dst.neg != b->dst.neg ||
1074 a->dst.addr.type != b->dst.addr.type)
1076 /* XXX if (a->proto != b->proto && a->proto != 0 && b->proto != 0
1077 * && (a->proto == IPPROTO_TCP || a->proto == IPPROTO_UDP ||
1078 * a->proto == IPPROTO_ICMP
1081 switch (a->dst.addr.type) {
1082 case PF_ADDR_ADDRMASK:
1083 if (memcmp(&a->dst.addr.v.a.addr, &b->dst.addr.v.a.addr,
1084 sizeof(a->dst.addr.v.a.addr)) ||
1085 memcmp(&a->dst.addr.v.a.mask, &b->dst.addr.v.a.mask,
1086 sizeof(a->dst.addr.v.a.mask)) ||
1087 (a->dst.addr.v.a.addr.addr32[0] == 0 &&
1088 a->dst.addr.v.a.addr.addr32[1] == 0 &&
1089 a->dst.addr.v.a.addr.addr32[2] == 0 &&
1090 a->dst.addr.v.a.addr.addr32[3] == 0))
1093 case PF_ADDR_DYNIFTL:
1094 if (strcmp(a->dst.addr.v.ifname, b->dst.addr.v.ifname) != 0 ||
1095 a->dst.addr.iflags != b->dst.addr.iflags ||
1096 memcmp(&a->dst.addr.v.a.mask, &b->dst.addr.v.a.mask,
1097 sizeof(a->dst.addr.v.a.mask)))
1100 case PF_ADDR_NOROUTE:
1101 case PF_ADDR_URPFFAILED:
1104 return (strcmp(a->dst.addr.v.tblname, b->dst.addr.v.tblname));
1109 /* Compare two rules DST port field for skiplist construction */
1111 skip_cmp_dst_port(struct pfctl_rule *a, struct pfctl_rule *b)
1113 /* XXX if (a->proto != b->proto && a->proto != 0 && b->proto != 0
1114 * && (a->proto == IPPROTO_TCP || a->proto == IPPROTO_UDP ||
1115 * a->proto == IPPROTO_ICMP
1118 if (a->dst.port_op == PF_OP_NONE || a->dst.port_op != b->dst.port_op ||
1119 a->dst.port[0] != b->dst.port[0] ||
1120 a->dst.port[1] != b->dst.port[1])
1125 /* Compare two rules IFP field for skiplist construction */
1127 skip_cmp_ifp(struct pfctl_rule *a, struct pfctl_rule *b)
1129 if (strcmp(a->ifname, b->ifname) || a->ifname[0] == '\0')
1131 return (a->ifnot != b->ifnot);
1134 /* Compare two rules PROTO field for skiplist construction */
1136 skip_cmp_proto(struct pfctl_rule *a, struct pfctl_rule *b)
1138 return (a->proto != b->proto || a->proto == 0);
1141 /* Compare two rules SRC addr field for skiplist construction */
1143 skip_cmp_src_addr(struct pfctl_rule *a, struct pfctl_rule *b)
1145 if (a->src.neg != b->src.neg ||
1146 a->src.addr.type != b->src.addr.type)
1148 /* XXX if (a->proto != b->proto && a->proto != 0 && b->proto != 0
1149 * && (a->proto == IPPROTO_TCP || a->proto == IPPROTO_UDP ||
1150 * a->proto == IPPROTO_ICMP
1153 switch (a->src.addr.type) {
1154 case PF_ADDR_ADDRMASK:
1155 if (memcmp(&a->src.addr.v.a.addr, &b->src.addr.v.a.addr,
1156 sizeof(a->src.addr.v.a.addr)) ||
1157 memcmp(&a->src.addr.v.a.mask, &b->src.addr.v.a.mask,
1158 sizeof(a->src.addr.v.a.mask)) ||
1159 (a->src.addr.v.a.addr.addr32[0] == 0 &&
1160 a->src.addr.v.a.addr.addr32[1] == 0 &&
1161 a->src.addr.v.a.addr.addr32[2] == 0 &&
1162 a->src.addr.v.a.addr.addr32[3] == 0))
1165 case PF_ADDR_DYNIFTL:
1166 if (strcmp(a->src.addr.v.ifname, b->src.addr.v.ifname) != 0 ||
1167 a->src.addr.iflags != b->src.addr.iflags ||
1168 memcmp(&a->src.addr.v.a.mask, &b->src.addr.v.a.mask,
1169 sizeof(a->src.addr.v.a.mask)))
1172 case PF_ADDR_NOROUTE:
1173 case PF_ADDR_URPFFAILED:
1176 return (strcmp(a->src.addr.v.tblname, b->src.addr.v.tblname));
1181 /* Compare two rules SRC port field for skiplist construction */
1183 skip_cmp_src_port(struct pfctl_rule *a, struct pfctl_rule *b)
1185 if (a->src.port_op == PF_OP_NONE || a->src.port_op != b->src.port_op ||
1186 a->src.port[0] != b->src.port[0] ||
1187 a->src.port[1] != b->src.port[1])
1189 /* XXX if (a->proto != b->proto && a->proto != 0 && b->proto != 0
1190 * && (a->proto == IPPROTO_TCP || a->proto == IPPROTO_UDP ||
1191 * a->proto == IPPROTO_ICMP
1204 int (*func)(struct pfctl_rule *, struct pfctl_rule *);
1205 } comps[] = PF_SKIP_COMPARITORS;
1208 for (skipnum = 0; skipnum < PF_SKIP_COUNT; skipnum++) {
1209 for (i = 0; i < sizeof(comps)/sizeof(*comps); i++)
1210 if (comps[i].skipnum == skipnum) {
1211 skip_comparitors[skipnum] = comps[i].func;
1212 skip_comparitors_names[skipnum] = comps[i].name;
1215 for (skipnum = 0; skipnum < PF_SKIP_COUNT; skipnum++)
1216 if (skip_comparitors[skipnum] == NULL)
1217 errx(1, "Need to add skip step comparitor to pfctl?!");
1221 * Add a host/netmask to a table
1224 add_opt_table(struct pfctl *pf, struct pf_opt_tbl **tbl, sa_family_t af,
1225 struct pf_rule_addr *addr)
1229 #endif /* OPT_DEBUG */
1230 static int tablenum = 0;
1231 struct node_host node_host;
1234 if ((*tbl = calloc(1, sizeof(**tbl))) == NULL ||
1235 ((*tbl)->pt_buf = calloc(1, sizeof(*(*tbl)->pt_buf))) ==
1238 (*tbl)->pt_buf->pfrb_type = PFRB_ADDRS;
1239 SIMPLEQ_INIT(&(*tbl)->pt_nodes);
1241 /* This is just a temporary table name */
1242 snprintf((*tbl)->pt_name, sizeof((*tbl)->pt_name), "%s%d",
1243 PF_OPT_TABLE_PREFIX, tablenum++);
1244 DEBUG("creating table <%s>", (*tbl)->pt_name);
1247 memset(&node_host, 0, sizeof(node_host));
1249 node_host.addr = addr->addr;
1252 DEBUG("<%s> adding %s/%d", (*tbl)->pt_name, inet_ntop(af,
1253 &node_host.addr.v.a.addr, buf, sizeof(buf)),
1254 unmask(&node_host.addr.v.a.mask, af));
1255 #endif /* OPT_DEBUG */
1257 if (append_addr_host((*tbl)->pt_buf, &node_host, 0, 0)) {
1258 warn("failed to add host");
1261 if (pf->opts & PF_OPT_VERBOSE) {
1262 struct node_tinit *ti;
1264 if ((ti = calloc(1, sizeof(*ti))) == NULL)
1266 if ((ti->host = malloc(sizeof(*ti->host))) == NULL)
1268 memcpy(ti->host, &node_host, sizeof(*ti->host));
1269 SIMPLEQ_INSERT_TAIL(&(*tbl)->pt_nodes, ti, entries);
1272 (*tbl)->pt_rulecount++;
1273 if ((*tbl)->pt_rulecount == TABLE_THRESHOLD)
1274 DEBUG("table <%s> now faster than skip steps", (*tbl)->pt_name);
1281 * Do the dirty work of choosing an unused table name and creating it.
1282 * (be careful with the table name, it might already be used in another anchor)
1285 pf_opt_create_table(struct pfctl *pf, struct pf_opt_tbl *tbl)
1287 static int tablenum;
1288 struct pfr_table *t;
1290 if (table_buffer.pfrb_type == 0) {
1291 /* Initialize the list of tables */
1292 table_buffer.pfrb_type = PFRB_TABLES;
1294 pfr_buf_grow(&table_buffer, table_buffer.pfrb_size);
1295 table_buffer.pfrb_size = table_buffer.pfrb_msize;
1296 if (pfr_get_tables(NULL, table_buffer.pfrb_caddr,
1297 &table_buffer.pfrb_size, PFR_FLAG_ALLRSETS))
1298 err(1, "pfr_get_tables");
1299 if (table_buffer.pfrb_size <= table_buffer.pfrb_msize)
1302 table_identifier = arc4random();
1305 /* XXX would be *really* nice to avoid duplicating identical tables */
1307 /* Now we have to pick a table name that isn't used */
1309 DEBUG("translating temporary table <%s> to <%s%x_%d>", tbl->pt_name,
1310 PF_OPT_TABLE_PREFIX, table_identifier, tablenum);
1311 snprintf(tbl->pt_name, sizeof(tbl->pt_name), "%s%x_%d",
1312 PF_OPT_TABLE_PREFIX, table_identifier, tablenum);
1313 PFRB_FOREACH(t, &table_buffer) {
1314 if (strcasecmp(t->pfrt_name, tbl->pt_name) == 0) {
1315 /* Collision. Try again */
1316 DEBUG("wow, table <%s> in use. trying again",
1318 table_identifier = arc4random();
1325 if (pfctl_define_table(tbl->pt_name, PFR_TFLAG_CONST, 1,
1326 pf->astack[0]->name, tbl->pt_buf, pf->astack[0]->ruleset.tticket)) {
1327 warn("failed to create table %s in %s",
1328 tbl->pt_name, pf->astack[0]->name);
1335 * Partition the flat ruleset into a list of distinct superblocks
1338 construct_superblocks(struct pfctl *pf, struct pf_opt_queue *opt_queue,
1339 struct superblocks *superblocks)
1341 struct superblock *block = NULL;
1342 struct pf_opt_rule *por;
1345 while (!TAILQ_EMPTY(opt_queue)) {
1346 por = TAILQ_FIRST(opt_queue);
1347 TAILQ_REMOVE(opt_queue, por, por_entry);
1348 if (block == NULL || !superblock_inclusive(block, por)) {
1349 if ((block = calloc(1, sizeof(*block))) == NULL) {
1353 TAILQ_INIT(&block->sb_rules);
1354 for (i = 0; i < PF_SKIP_COUNT; i++)
1355 TAILQ_INIT(&block->sb_skipsteps[i]);
1356 TAILQ_INSERT_TAIL(superblocks, block, sb_entry);
1358 TAILQ_INSERT_TAIL(&block->sb_rules, por, por_entry);
1366 * Compare two rule addresses
1369 addrs_equal(struct pf_rule_addr *a, struct pf_rule_addr *b)
1371 if (a->neg != b->neg)
1373 return (memcmp(&a->addr, &b->addr, sizeof(a->addr)) == 0);
1378 * The addresses are not equal, but can we combine them into one table?
1381 addrs_combineable(struct pf_rule_addr *a, struct pf_rule_addr *b)
1383 if (a->addr.type != PF_ADDR_ADDRMASK ||
1384 b->addr.type != PF_ADDR_ADDRMASK)
1386 if (a->neg != b->neg || a->port_op != b->port_op ||
1387 a->port[0] != b->port[0] || a->port[1] != b->port[1])
1394 * Are we allowed to combine these two rules
1397 rules_combineable(struct pfctl_rule *p1, struct pfctl_rule *p2)
1399 struct pfctl_rule a, b;
1401 comparable_rule(&a, p1, COMBINED);
1402 comparable_rule(&b, p2, COMBINED);
1403 return (memcmp(&a, &b, sizeof(a)) == 0);
1408 * Can a rule be included inside a superblock
1411 superblock_inclusive(struct superblock *block, struct pf_opt_rule *por)
1413 struct pfctl_rule a, b;
1416 /* First check for hard breaks */
1417 for (i = 0; i < sizeof(pf_rule_desc)/sizeof(*pf_rule_desc); i++) {
1418 if (pf_rule_desc[i].prf_type == BARRIER) {
1419 for (j = 0; j < pf_rule_desc[i].prf_size; j++)
1420 if (((char *)&por->por_rule)[j +
1421 pf_rule_desc[i].prf_offset] != 0)
1426 /* per-rule src-track is also a hard break */
1427 if (por->por_rule.rule_flag & PFRULE_RULESRCTRACK)
1431 * Have to handle interface groups separately. Consider the following
1433 * block on EXTIFS to any port 22
1434 * pass on em0 to any port 22
1435 * (where EXTIFS is an arbitrary interface group)
1436 * The optimizer may decide to re-order the pass rule in front of the
1437 * block rule. But what if EXTIFS includes em0??? Such a reordering
1438 * would change the meaning of the ruleset.
1439 * We can't just lookup the EXTIFS group and check if em0 is a member
1440 * because the user is allowed to add interfaces to a group during
1442 * Ergo interface groups become a defacto superblock break :-(
1444 if (interface_group(por->por_rule.ifname) ||
1445 interface_group(TAILQ_FIRST(&block->sb_rules)->por_rule.ifname)) {
1446 if (strcasecmp(por->por_rule.ifname,
1447 TAILQ_FIRST(&block->sb_rules)->por_rule.ifname) != 0)
1451 comparable_rule(&a, &TAILQ_FIRST(&block->sb_rules)->por_rule, NOMERGE);
1452 comparable_rule(&b, &por->por_rule, NOMERGE);
1453 if (memcmp(&a, &b, sizeof(a)) == 0)
1457 for (i = 0; i < sizeof(por->por_rule); i++) {
1459 if (((u_int8_t *)&a)[i] != ((u_int8_t *)&b)[i]) {
1460 for (j = 0; j < sizeof(pf_rule_desc) /
1461 sizeof(*pf_rule_desc); j++) {
1462 if (i >= pf_rule_desc[j].prf_offset &&
1463 i < pf_rule_desc[j].prf_offset +
1464 pf_rule_desc[j].prf_size) {
1465 DEBUG("superblock break @ %d due to %s",
1467 pf_rule_desc[j].prf_name);
1470 if (i > pf_rule_desc[j].prf_offset) {
1471 if (closest == -1 ||
1472 i-pf_rule_desc[j].prf_offset <
1473 i-pf_rule_desc[closest].prf_offset)
1479 DEBUG("superblock break @ %d on %s+%xh",
1481 pf_rule_desc[closest].prf_name,
1482 i - pf_rule_desc[closest].prf_offset -
1483 pf_rule_desc[closest].prf_size);
1485 DEBUG("superblock break @ %d on field @ %d",
1486 por->por_rule.nr, i);
1490 #endif /* OPT_DEBUG */
1497 * Figure out if an interface name is an actual interface or actually a
1498 * group of interfaces.
1501 interface_group(const char *ifname)
1504 struct ifgroupreq ifgr;
1506 if (ifname == NULL || !ifname[0])
1509 s = get_query_socket();
1511 memset(&ifgr, 0, sizeof(ifgr));
1512 strlcpy(ifgr.ifgr_name, ifname, IFNAMSIZ);
1513 if (ioctl(s, SIOCGIFGMEMB, (caddr_t)&ifgr) == -1) {
1514 if (errno == ENOENT)
1517 err(1, "SIOCGIFGMEMB");
1525 * Make a rule that can directly compared by memcmp()
1528 comparable_rule(struct pfctl_rule *dst, const struct pfctl_rule *src, int type)
1532 * To simplify the comparison, we just zero out the fields that are
1533 * allowed to be different and then do a simple memcmp()
1535 memcpy(dst, src, sizeof(*dst));
1536 for (i = 0; i < sizeof(pf_rule_desc)/sizeof(*pf_rule_desc); i++)
1537 if (pf_rule_desc[i].prf_type >= type) {
1539 assert(pf_rule_desc[i].prf_type != NEVER ||
1540 *(((char *)dst) + pf_rule_desc[i].prf_offset) == 0);
1541 #endif /* OPT_DEBUG */
1542 memset(((char *)dst) + pf_rule_desc[i].prf_offset, 0,
1543 pf_rule_desc[i].prf_size);
1549 * Remove superset information from two rules so we can directly compare them
1553 exclude_supersets(struct pfctl_rule *super, struct pfctl_rule *sub)
1555 if (super->ifname[0] == '\0')
1556 memset(sub->ifname, 0, sizeof(sub->ifname));
1557 if (super->direction == PF_INOUT)
1558 sub->direction = PF_INOUT;
1559 if ((super->proto == 0 || super->proto == sub->proto) &&
1560 super->flags == 0 && super->flagset == 0 && (sub->flags ||
1562 sub->flags = super->flags;
1563 sub->flagset = super->flagset;
1565 if (super->proto == 0)
1568 if (super->src.port_op == 0) {
1569 sub->src.port_op = 0;
1570 sub->src.port[0] = 0;
1571 sub->src.port[1] = 0;
1573 if (super->dst.port_op == 0) {
1574 sub->dst.port_op = 0;
1575 sub->dst.port[0] = 0;
1576 sub->dst.port[1] = 0;
1579 if (super->src.addr.type == PF_ADDR_ADDRMASK && !super->src.neg &&
1580 !sub->src.neg && super->src.addr.v.a.mask.addr32[0] == 0 &&
1581 super->src.addr.v.a.mask.addr32[1] == 0 &&
1582 super->src.addr.v.a.mask.addr32[2] == 0 &&
1583 super->src.addr.v.a.mask.addr32[3] == 0)
1584 memset(&sub->src.addr, 0, sizeof(sub->src.addr));
1585 else if (super->src.addr.type == PF_ADDR_ADDRMASK &&
1586 sub->src.addr.type == PF_ADDR_ADDRMASK &&
1587 super->src.neg == sub->src.neg &&
1588 super->af == sub->af &&
1589 unmask(&super->src.addr.v.a.mask, super->af) <
1590 unmask(&sub->src.addr.v.a.mask, sub->af) &&
1591 super->src.addr.v.a.addr.addr32[0] ==
1592 (sub->src.addr.v.a.addr.addr32[0] &
1593 super->src.addr.v.a.mask.addr32[0]) &&
1594 super->src.addr.v.a.addr.addr32[1] ==
1595 (sub->src.addr.v.a.addr.addr32[1] &
1596 super->src.addr.v.a.mask.addr32[1]) &&
1597 super->src.addr.v.a.addr.addr32[2] ==
1598 (sub->src.addr.v.a.addr.addr32[2] &
1599 super->src.addr.v.a.mask.addr32[2]) &&
1600 super->src.addr.v.a.addr.addr32[3] ==
1601 (sub->src.addr.v.a.addr.addr32[3] &
1602 super->src.addr.v.a.mask.addr32[3])) {
1603 /* sub->src.addr is a subset of super->src.addr/mask */
1604 memcpy(&sub->src.addr, &super->src.addr, sizeof(sub->src.addr));
1607 if (super->dst.addr.type == PF_ADDR_ADDRMASK && !super->dst.neg &&
1608 !sub->dst.neg && super->dst.addr.v.a.mask.addr32[0] == 0 &&
1609 super->dst.addr.v.a.mask.addr32[1] == 0 &&
1610 super->dst.addr.v.a.mask.addr32[2] == 0 &&
1611 super->dst.addr.v.a.mask.addr32[3] == 0)
1612 memset(&sub->dst.addr, 0, sizeof(sub->dst.addr));
1613 else if (super->dst.addr.type == PF_ADDR_ADDRMASK &&
1614 sub->dst.addr.type == PF_ADDR_ADDRMASK &&
1615 super->dst.neg == sub->dst.neg &&
1616 super->af == sub->af &&
1617 unmask(&super->dst.addr.v.a.mask, super->af) <
1618 unmask(&sub->dst.addr.v.a.mask, sub->af) &&
1619 super->dst.addr.v.a.addr.addr32[0] ==
1620 (sub->dst.addr.v.a.addr.addr32[0] &
1621 super->dst.addr.v.a.mask.addr32[0]) &&
1622 super->dst.addr.v.a.addr.addr32[1] ==
1623 (sub->dst.addr.v.a.addr.addr32[1] &
1624 super->dst.addr.v.a.mask.addr32[1]) &&
1625 super->dst.addr.v.a.addr.addr32[2] ==
1626 (sub->dst.addr.v.a.addr.addr32[2] &
1627 super->dst.addr.v.a.mask.addr32[2]) &&
1628 super->dst.addr.v.a.addr.addr32[3] ==
1629 (sub->dst.addr.v.a.addr.addr32[3] &
1630 super->dst.addr.v.a.mask.addr32[3])) {
1631 /* sub->dst.addr is a subset of super->dst.addr/mask */
1632 memcpy(&sub->dst.addr, &super->dst.addr, sizeof(sub->dst.addr));
1641 superblock_free(struct pfctl *pf, struct superblock *block)
1643 struct pf_opt_rule *por;
1644 while ((por = TAILQ_FIRST(&block->sb_rules))) {
1645 TAILQ_REMOVE(&block->sb_rules, por, por_entry);
1646 if (por->por_src_tbl) {
1647 if (por->por_src_tbl->pt_buf) {
1648 pfr_buf_clear(por->por_src_tbl->pt_buf);
1649 free(por->por_src_tbl->pt_buf);
1651 free(por->por_src_tbl);
1653 if (por->por_dst_tbl) {
1654 if (por->por_dst_tbl->pt_buf) {
1655 pfr_buf_clear(por->por_dst_tbl->pt_buf);
1656 free(por->por_dst_tbl->pt_buf);
1658 free(por->por_dst_tbl);
1662 if (block->sb_profiled_block)
1663 superblock_free(pf, block->sb_profiled_block);