13b3a8eb9SGleb Smirnoff /* $OpenBSD: pfctl_optimize.c,v 1.17 2008/05/06 03:45:21 mpf Exp $ */ 23b3a8eb9SGleb Smirnoff 33b3a8eb9SGleb Smirnoff /* 43b3a8eb9SGleb Smirnoff * Copyright (c) 2004 Mike Frantzen <frantzen@openbsd.org> 53b3a8eb9SGleb Smirnoff * 63b3a8eb9SGleb Smirnoff * Permission to use, copy, modify, and distribute this software for any 73b3a8eb9SGleb Smirnoff * purpose with or without fee is hereby granted, provided that the above 83b3a8eb9SGleb Smirnoff * copyright notice and this permission notice appear in all copies. 93b3a8eb9SGleb Smirnoff * 103b3a8eb9SGleb Smirnoff * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 113b3a8eb9SGleb Smirnoff * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 123b3a8eb9SGleb Smirnoff * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 133b3a8eb9SGleb Smirnoff * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 143b3a8eb9SGleb Smirnoff * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 153b3a8eb9SGleb Smirnoff * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 163b3a8eb9SGleb Smirnoff * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 173b3a8eb9SGleb Smirnoff */ 183b3a8eb9SGleb Smirnoff 193b3a8eb9SGleb Smirnoff #include <sys/types.h> 203b3a8eb9SGleb Smirnoff #include <sys/ioctl.h> 213b3a8eb9SGleb Smirnoff #include <sys/socket.h> 223b3a8eb9SGleb Smirnoff 233b3a8eb9SGleb Smirnoff #include <net/if.h> 243b3a8eb9SGleb Smirnoff #include <net/pfvar.h> 253b3a8eb9SGleb Smirnoff 263b3a8eb9SGleb Smirnoff #include <netinet/in.h> 273b3a8eb9SGleb Smirnoff #include <arpa/inet.h> 283b3a8eb9SGleb Smirnoff 293b3a8eb9SGleb Smirnoff #include <assert.h> 303b3a8eb9SGleb Smirnoff #include <ctype.h> 313b3a8eb9SGleb Smirnoff #include <err.h> 323b3a8eb9SGleb Smirnoff #include <errno.h> 330d71f9f3SKristof Provost #include <libpfctl.h> 343b3a8eb9SGleb Smirnoff #include <stddef.h> 353b3a8eb9SGleb Smirnoff #include <stdio.h> 363b3a8eb9SGleb Smirnoff #include <stdlib.h> 373b3a8eb9SGleb Smirnoff #include <string.h> 383b3a8eb9SGleb Smirnoff 393b3a8eb9SGleb Smirnoff #include "pfctl_parser.h" 403b3a8eb9SGleb Smirnoff #include "pfctl.h" 413b3a8eb9SGleb Smirnoff 423b3a8eb9SGleb Smirnoff /* The size at which a table becomes faster than individual rules */ 433b3a8eb9SGleb Smirnoff #define TABLE_THRESHOLD 6 443b3a8eb9SGleb Smirnoff 453b3a8eb9SGleb Smirnoff 463b3a8eb9SGleb Smirnoff /* #define OPT_DEBUG 1 */ 473b3a8eb9SGleb Smirnoff #ifdef OPT_DEBUG 483b3a8eb9SGleb Smirnoff # define DEBUG(str, v...) \ 493b3a8eb9SGleb Smirnoff printf("%s: " str "\n", __FUNCTION__ , ## v) 503b3a8eb9SGleb Smirnoff #else 513b3a8eb9SGleb Smirnoff # define DEBUG(str, v...) ((void)0) 523b3a8eb9SGleb Smirnoff #endif 533b3a8eb9SGleb Smirnoff 543b3a8eb9SGleb Smirnoff 553b3a8eb9SGleb Smirnoff /* 563b3a8eb9SGleb Smirnoff * A container that lets us sort a superblock to optimize the skip step jumps 573b3a8eb9SGleb Smirnoff */ 583b3a8eb9SGleb Smirnoff struct pf_skip_step { 593b3a8eb9SGleb Smirnoff int ps_count; /* number of items */ 603b3a8eb9SGleb Smirnoff TAILQ_HEAD( , pf_opt_rule) ps_rules; 613b3a8eb9SGleb Smirnoff TAILQ_ENTRY(pf_skip_step) ps_entry; 623b3a8eb9SGleb Smirnoff }; 633b3a8eb9SGleb Smirnoff 643b3a8eb9SGleb Smirnoff 653b3a8eb9SGleb Smirnoff /* 663b3a8eb9SGleb Smirnoff * A superblock is a block of adjacent rules of similar action. If there 673b3a8eb9SGleb Smirnoff * are five PASS rules in a row, they all become members of a superblock. 683b3a8eb9SGleb Smirnoff * Once we have a superblock, we are free to re-order any rules within it 693b3a8eb9SGleb Smirnoff * in order to improve performance; if a packet is passed, it doesn't matter 703b3a8eb9SGleb Smirnoff * who passed it. 713b3a8eb9SGleb Smirnoff */ 723b3a8eb9SGleb Smirnoff struct superblock { 733b3a8eb9SGleb Smirnoff TAILQ_HEAD( , pf_opt_rule) sb_rules; 743b3a8eb9SGleb Smirnoff TAILQ_ENTRY(superblock) sb_entry; 753b3a8eb9SGleb Smirnoff struct superblock *sb_profiled_block; 763b3a8eb9SGleb Smirnoff TAILQ_HEAD(skiplist, pf_skip_step) sb_skipsteps[PF_SKIP_COUNT]; 773b3a8eb9SGleb Smirnoff }; 783b3a8eb9SGleb Smirnoff TAILQ_HEAD(superblocks, superblock); 793b3a8eb9SGleb Smirnoff 803b3a8eb9SGleb Smirnoff 813b3a8eb9SGleb Smirnoff /* 823b3a8eb9SGleb Smirnoff * Description of the PF rule structure. 833b3a8eb9SGleb Smirnoff */ 843b3a8eb9SGleb Smirnoff enum { 8528323addSBryan Drewery BARRIER, /* the presence of the field puts the rule in its own block */ 863b3a8eb9SGleb Smirnoff BREAK, /* the field may not differ between rules in a superblock */ 873b3a8eb9SGleb Smirnoff NOMERGE, /* the field may not differ between rules when combined */ 883b3a8eb9SGleb Smirnoff COMBINED, /* the field may itself be combined with other rules */ 893b3a8eb9SGleb Smirnoff DC, /* we just don't care about the field */ 903b3a8eb9SGleb Smirnoff NEVER}; /* we should never see this field set?!? */ 9113cfafabSKristof Provost static struct pf_rule_field { 923b3a8eb9SGleb Smirnoff const char *prf_name; 933b3a8eb9SGleb Smirnoff int prf_type; 943b3a8eb9SGleb Smirnoff size_t prf_offset; 953b3a8eb9SGleb Smirnoff size_t prf_size; 963b3a8eb9SGleb Smirnoff } pf_rule_desc[] = { 973b3a8eb9SGleb Smirnoff #define PF_RULE_FIELD(field, ty) \ 983b3a8eb9SGleb Smirnoff {#field, \ 993b3a8eb9SGleb Smirnoff ty, \ 100e9eb0941SKristof Provost offsetof(struct pfctl_rule, field), \ 101e9eb0941SKristof Provost sizeof(((struct pfctl_rule *)0)->field)} 1023b3a8eb9SGleb Smirnoff 1033b3a8eb9SGleb Smirnoff 1043b3a8eb9SGleb Smirnoff /* 10528323addSBryan Drewery * The presence of these fields in a rule put the rule in its own 1063b3a8eb9SGleb Smirnoff * superblock. Thus it will not be optimized. It also prevents the 1073b3a8eb9SGleb Smirnoff * rule from being re-ordered at all. 1083b3a8eb9SGleb Smirnoff */ 1093b3a8eb9SGleb Smirnoff PF_RULE_FIELD(label, BARRIER), 1103b3a8eb9SGleb Smirnoff PF_RULE_FIELD(prob, BARRIER), 1113b3a8eb9SGleb Smirnoff PF_RULE_FIELD(max_states, BARRIER), 1123b3a8eb9SGleb Smirnoff PF_RULE_FIELD(max_src_nodes, BARRIER), 1133b3a8eb9SGleb Smirnoff PF_RULE_FIELD(max_src_states, BARRIER), 1143b3a8eb9SGleb Smirnoff PF_RULE_FIELD(max_src_conn, BARRIER), 1153b3a8eb9SGleb Smirnoff PF_RULE_FIELD(max_src_conn_rate, BARRIER), 1163b3a8eb9SGleb Smirnoff PF_RULE_FIELD(anchor, BARRIER), /* for now */ 1173b3a8eb9SGleb Smirnoff 1183b3a8eb9SGleb Smirnoff /* 1193b3a8eb9SGleb Smirnoff * These fields must be the same between all rules in the same superblock. 1203b3a8eb9SGleb Smirnoff * These rules are allowed to be re-ordered but only among like rules. 1213b3a8eb9SGleb Smirnoff * For instance we can re-order all 'tag "foo"' rules because they have the 1223b3a8eb9SGleb Smirnoff * same tag. But we can not re-order between a 'tag "foo"' and a 1233b3a8eb9SGleb Smirnoff * 'tag "bar"' since that would change the meaning of the ruleset. 1243b3a8eb9SGleb Smirnoff */ 1253b3a8eb9SGleb Smirnoff PF_RULE_FIELD(tagname, BREAK), 1263b3a8eb9SGleb Smirnoff PF_RULE_FIELD(keep_state, BREAK), 1273b3a8eb9SGleb Smirnoff PF_RULE_FIELD(qname, BREAK), 1283b3a8eb9SGleb Smirnoff PF_RULE_FIELD(pqname, BREAK), 1293b3a8eb9SGleb Smirnoff PF_RULE_FIELD(rt, BREAK), 1303b3a8eb9SGleb Smirnoff PF_RULE_FIELD(allow_opts, BREAK), 1313b3a8eb9SGleb Smirnoff PF_RULE_FIELD(rule_flag, BREAK), 1323b3a8eb9SGleb Smirnoff PF_RULE_FIELD(action, BREAK), 1333b3a8eb9SGleb Smirnoff PF_RULE_FIELD(log, BREAK), 1343b3a8eb9SGleb Smirnoff PF_RULE_FIELD(quick, BREAK), 1353b3a8eb9SGleb Smirnoff PF_RULE_FIELD(return_ttl, BREAK), 1363b3a8eb9SGleb Smirnoff PF_RULE_FIELD(overload_tblname, BREAK), 1373b3a8eb9SGleb Smirnoff PF_RULE_FIELD(flush, BREAK), 138096efeb6SKristof Provost PF_RULE_FIELD(rdr, BREAK), 1395cb08fddSKristof Provost PF_RULE_FIELD(nat, BREAK), 140*0972294eSKristof Provost PF_RULE_FIELD(route, BREAK), 1413b3a8eb9SGleb Smirnoff PF_RULE_FIELD(logif, BREAK), 1423b3a8eb9SGleb Smirnoff 1433b3a8eb9SGleb Smirnoff /* 1443b3a8eb9SGleb Smirnoff * Any fields not listed in this structure act as BREAK fields 1453b3a8eb9SGleb Smirnoff */ 1463b3a8eb9SGleb Smirnoff 1473b3a8eb9SGleb Smirnoff 1483b3a8eb9SGleb Smirnoff /* 1493b3a8eb9SGleb Smirnoff * These fields must not differ when we merge two rules together but 1503b3a8eb9SGleb Smirnoff * their difference isn't enough to put the rules in different superblocks. 1513b3a8eb9SGleb Smirnoff * There are no problems re-ordering any rules with these fields. 1523b3a8eb9SGleb Smirnoff */ 1533b3a8eb9SGleb Smirnoff PF_RULE_FIELD(af, NOMERGE), 1543b3a8eb9SGleb Smirnoff PF_RULE_FIELD(ifnot, NOMERGE), 1553b3a8eb9SGleb Smirnoff PF_RULE_FIELD(ifname, NOMERGE), /* hack for IF groups */ 1563b3a8eb9SGleb Smirnoff PF_RULE_FIELD(match_tag_not, NOMERGE), 1573b3a8eb9SGleb Smirnoff PF_RULE_FIELD(match_tagname, NOMERGE), 1583b3a8eb9SGleb Smirnoff PF_RULE_FIELD(os_fingerprint, NOMERGE), 1593b3a8eb9SGleb Smirnoff PF_RULE_FIELD(timeout, NOMERGE), 1603b3a8eb9SGleb Smirnoff PF_RULE_FIELD(return_icmp, NOMERGE), 1613b3a8eb9SGleb Smirnoff PF_RULE_FIELD(return_icmp6, NOMERGE), 1623b3a8eb9SGleb Smirnoff PF_RULE_FIELD(uid, NOMERGE), 1633b3a8eb9SGleb Smirnoff PF_RULE_FIELD(gid, NOMERGE), 1643b3a8eb9SGleb Smirnoff PF_RULE_FIELD(direction, NOMERGE), 1653b3a8eb9SGleb Smirnoff PF_RULE_FIELD(proto, NOMERGE), 1663b3a8eb9SGleb Smirnoff PF_RULE_FIELD(type, NOMERGE), 1673b3a8eb9SGleb Smirnoff PF_RULE_FIELD(code, NOMERGE), 1683b3a8eb9SGleb Smirnoff PF_RULE_FIELD(flags, NOMERGE), 1693b3a8eb9SGleb Smirnoff PF_RULE_FIELD(flagset, NOMERGE), 1703b3a8eb9SGleb Smirnoff PF_RULE_FIELD(tos, NOMERGE), 1713b3a8eb9SGleb Smirnoff PF_RULE_FIELD(src.port, NOMERGE), 1723b3a8eb9SGleb Smirnoff PF_RULE_FIELD(dst.port, NOMERGE), 1733b3a8eb9SGleb Smirnoff PF_RULE_FIELD(src.port_op, NOMERGE), 1743b3a8eb9SGleb Smirnoff PF_RULE_FIELD(dst.port_op, NOMERGE), 1753b3a8eb9SGleb Smirnoff PF_RULE_FIELD(src.neg, NOMERGE), 1763b3a8eb9SGleb Smirnoff PF_RULE_FIELD(dst.neg, NOMERGE), 1770d68985bSKristof Provost PF_RULE_FIELD(af, NOMERGE), 1783b3a8eb9SGleb Smirnoff 1793b3a8eb9SGleb Smirnoff /* These fields can be merged */ 1803b3a8eb9SGleb Smirnoff PF_RULE_FIELD(src.addr, COMBINED), 1813b3a8eb9SGleb Smirnoff PF_RULE_FIELD(dst.addr, COMBINED), 1823b3a8eb9SGleb Smirnoff 1833b3a8eb9SGleb Smirnoff /* We just don't care about these fields. They're set by the kernel */ 1843b3a8eb9SGleb Smirnoff PF_RULE_FIELD(skip, DC), 1853b3a8eb9SGleb Smirnoff PF_RULE_FIELD(evaluations, DC), 1863b3a8eb9SGleb Smirnoff PF_RULE_FIELD(packets, DC), 1873b3a8eb9SGleb Smirnoff PF_RULE_FIELD(bytes, DC), 1883b3a8eb9SGleb Smirnoff PF_RULE_FIELD(kif, DC), 1893b3a8eb9SGleb Smirnoff PF_RULE_FIELD(states_cur, DC), 1903b3a8eb9SGleb Smirnoff PF_RULE_FIELD(states_tot, DC), 1913b3a8eb9SGleb Smirnoff PF_RULE_FIELD(src_nodes, DC), 1923b3a8eb9SGleb Smirnoff PF_RULE_FIELD(nr, DC), 1933b3a8eb9SGleb Smirnoff PF_RULE_FIELD(entries, DC), 1943b3a8eb9SGleb Smirnoff PF_RULE_FIELD(qid, DC), 1953b3a8eb9SGleb Smirnoff PF_RULE_FIELD(pqid, DC), 1963b3a8eb9SGleb Smirnoff PF_RULE_FIELD(anchor_relative, DC), 1973b3a8eb9SGleb Smirnoff PF_RULE_FIELD(anchor_wildcard, DC), 1983b3a8eb9SGleb Smirnoff PF_RULE_FIELD(tag, DC), 1993b3a8eb9SGleb Smirnoff PF_RULE_FIELD(match_tag, DC), 2003b3a8eb9SGleb Smirnoff PF_RULE_FIELD(overload_tbl, DC), 2013b3a8eb9SGleb Smirnoff 2023b3a8eb9SGleb Smirnoff /* These fields should never be set in a PASS/BLOCK rule */ 2033b3a8eb9SGleb Smirnoff PF_RULE_FIELD(natpass, NEVER), 2043b3a8eb9SGleb Smirnoff PF_RULE_FIELD(max_mss, NEVER), 2053b3a8eb9SGleb Smirnoff PF_RULE_FIELD(min_ttl, NEVER), 2063b3a8eb9SGleb Smirnoff PF_RULE_FIELD(set_tos, NEVER), 2073b3a8eb9SGleb Smirnoff }; 2083b3a8eb9SGleb Smirnoff 2093b3a8eb9SGleb Smirnoff 2103b3a8eb9SGleb Smirnoff 2113b3a8eb9SGleb Smirnoff int add_opt_table(struct pfctl *, struct pf_opt_tbl **, sa_family_t, 2123b3a8eb9SGleb Smirnoff struct pf_rule_addr *); 2133b3a8eb9SGleb Smirnoff int addrs_combineable(struct pf_rule_addr *, struct pf_rule_addr *); 2143b3a8eb9SGleb Smirnoff int addrs_equal(struct pf_rule_addr *, struct pf_rule_addr *); 2153b3a8eb9SGleb Smirnoff int block_feedback(struct pfctl *, struct superblock *); 2163b3a8eb9SGleb Smirnoff int combine_rules(struct pfctl *, struct superblock *); 217e9eb0941SKristof Provost void comparable_rule(struct pfctl_rule *, const struct pfctl_rule *, int); 2183b3a8eb9SGleb Smirnoff int construct_superblocks(struct pfctl *, struct pf_opt_queue *, 2193b3a8eb9SGleb Smirnoff struct superblocks *); 220e9eb0941SKristof Provost void exclude_supersets(struct pfctl_rule *, struct pfctl_rule *); 2213b3a8eb9SGleb Smirnoff int interface_group(const char *); 2223b3a8eb9SGleb Smirnoff int load_feedback_profile(struct pfctl *, struct superblocks *); 2233b3a8eb9SGleb Smirnoff int optimize_superblock(struct pfctl *, struct superblock *); 2243b3a8eb9SGleb Smirnoff int pf_opt_create_table(struct pfctl *, struct pf_opt_tbl *); 2253b3a8eb9SGleb Smirnoff void remove_from_skipsteps(struct skiplist *, struct superblock *, 2263b3a8eb9SGleb Smirnoff struct pf_opt_rule *, struct pf_skip_step *); 2273b3a8eb9SGleb Smirnoff int remove_identical_rules(struct pfctl *, struct superblock *); 2283b3a8eb9SGleb Smirnoff int reorder_rules(struct pfctl *, struct superblock *, int); 229e9eb0941SKristof Provost int rules_combineable(struct pfctl_rule *, struct pfctl_rule *); 2303b3a8eb9SGleb Smirnoff void skip_append(struct superblock *, int, struct pf_skip_step *, 2313b3a8eb9SGleb Smirnoff struct pf_opt_rule *); 2323b3a8eb9SGleb Smirnoff int skip_compare(int, struct pf_skip_step *, struct pf_opt_rule *); 2333b3a8eb9SGleb Smirnoff void skip_init(void); 234e9eb0941SKristof Provost int skip_cmp_af(struct pfctl_rule *, struct pfctl_rule *); 235e9eb0941SKristof Provost int skip_cmp_dir(struct pfctl_rule *, struct pfctl_rule *); 236e9eb0941SKristof Provost int skip_cmp_dst_addr(struct pfctl_rule *, struct pfctl_rule *); 237e9eb0941SKristof Provost int skip_cmp_dst_port(struct pfctl_rule *, struct pfctl_rule *); 238e9eb0941SKristof Provost int skip_cmp_ifp(struct pfctl_rule *, struct pfctl_rule *); 239e9eb0941SKristof Provost int skip_cmp_proto(struct pfctl_rule *, struct pfctl_rule *); 240e9eb0941SKristof Provost int skip_cmp_src_addr(struct pfctl_rule *, struct pfctl_rule *); 241e9eb0941SKristof Provost int skip_cmp_src_port(struct pfctl_rule *, struct pfctl_rule *); 2423b3a8eb9SGleb Smirnoff int superblock_inclusive(struct superblock *, struct pf_opt_rule *); 2433b3a8eb9SGleb Smirnoff void superblock_free(struct pfctl *, struct superblock *); 2443b3a8eb9SGleb Smirnoff 2453b3a8eb9SGleb Smirnoff 246e9eb0941SKristof Provost static int (*skip_comparitors[PF_SKIP_COUNT])(struct pfctl_rule *, 247e9eb0941SKristof Provost struct pfctl_rule *); 24813cfafabSKristof Provost static const char *skip_comparitors_names[PF_SKIP_COUNT]; 2493b3a8eb9SGleb Smirnoff #define PF_SKIP_COMPARITORS { \ 2503b3a8eb9SGleb Smirnoff { "ifp", PF_SKIP_IFP, skip_cmp_ifp }, \ 2513b3a8eb9SGleb Smirnoff { "dir", PF_SKIP_DIR, skip_cmp_dir }, \ 2523b3a8eb9SGleb Smirnoff { "af", PF_SKIP_AF, skip_cmp_af }, \ 2533b3a8eb9SGleb Smirnoff { "proto", PF_SKIP_PROTO, skip_cmp_proto }, \ 2543b3a8eb9SGleb Smirnoff { "saddr", PF_SKIP_SRC_ADDR, skip_cmp_src_addr }, \ 2553b3a8eb9SGleb Smirnoff { "daddr", PF_SKIP_DST_ADDR, skip_cmp_dst_addr }, \ 256288bec2bSKristof Provost { "sport", PF_SKIP_SRC_PORT, skip_cmp_src_port }, \ 2573b3a8eb9SGleb Smirnoff { "dport", PF_SKIP_DST_PORT, skip_cmp_dst_port } \ 2583b3a8eb9SGleb Smirnoff } 2593b3a8eb9SGleb Smirnoff 26013cfafabSKristof Provost static struct pfr_buffer table_buffer; 26113cfafabSKristof Provost static int table_identifier; 2623b3a8eb9SGleb Smirnoff 2633b3a8eb9SGleb Smirnoff 2643b3a8eb9SGleb Smirnoff int 265e9eb0941SKristof Provost pfctl_optimize_ruleset(struct pfctl *pf, struct pfctl_ruleset *rs) 2663b3a8eb9SGleb Smirnoff { 2673b3a8eb9SGleb Smirnoff struct superblocks superblocks; 2683b3a8eb9SGleb Smirnoff struct pf_opt_queue opt_queue; 2693b3a8eb9SGleb Smirnoff struct superblock *block; 2703b3a8eb9SGleb Smirnoff struct pf_opt_rule *por; 271e9eb0941SKristof Provost struct pfctl_rule *r; 272e9eb0941SKristof Provost struct pfctl_rulequeue *old_rules; 2733b3a8eb9SGleb Smirnoff 2743b3a8eb9SGleb Smirnoff DEBUG("optimizing ruleset"); 2753b3a8eb9SGleb Smirnoff memset(&table_buffer, 0, sizeof(table_buffer)); 2763b3a8eb9SGleb Smirnoff skip_init(); 2773b3a8eb9SGleb Smirnoff TAILQ_INIT(&opt_queue); 2783b3a8eb9SGleb Smirnoff 2793b3a8eb9SGleb Smirnoff old_rules = rs->rules[PF_RULESET_FILTER].active.ptr; 2803b3a8eb9SGleb Smirnoff rs->rules[PF_RULESET_FILTER].active.ptr = 2813b3a8eb9SGleb Smirnoff rs->rules[PF_RULESET_FILTER].inactive.ptr; 2823b3a8eb9SGleb Smirnoff rs->rules[PF_RULESET_FILTER].inactive.ptr = old_rules; 2833b3a8eb9SGleb Smirnoff 2843b3a8eb9SGleb Smirnoff /* 2853b3a8eb9SGleb Smirnoff * XXX expanding the pf_opt_rule format throughout pfctl might allow 2863b3a8eb9SGleb Smirnoff * us to avoid all this copying. 2873b3a8eb9SGleb Smirnoff */ 2883b3a8eb9SGleb Smirnoff while ((r = TAILQ_FIRST(rs->rules[PF_RULESET_FILTER].inactive.ptr)) 2893b3a8eb9SGleb Smirnoff != NULL) { 2903b3a8eb9SGleb Smirnoff TAILQ_REMOVE(rs->rules[PF_RULESET_FILTER].inactive.ptr, r, 2913b3a8eb9SGleb Smirnoff entries); 2923b3a8eb9SGleb Smirnoff if ((por = calloc(1, sizeof(*por))) == NULL) 2933b3a8eb9SGleb Smirnoff err(1, "calloc"); 2943b3a8eb9SGleb Smirnoff memcpy(&por->por_rule, r, sizeof(*r)); 295096efeb6SKristof Provost if (TAILQ_FIRST(&r->rdr.list) != NULL) { 296096efeb6SKristof Provost TAILQ_INIT(&por->por_rule.rdr.list); 297096efeb6SKristof Provost pfctl_move_pool(&r->rdr, &por->por_rule.rdr); 2983b3a8eb9SGleb Smirnoff } else 299096efeb6SKristof Provost bzero(&por->por_rule.rdr, 300096efeb6SKristof Provost sizeof(por->por_rule.rdr)); 3015cb08fddSKristof Provost if (TAILQ_FIRST(&r->nat.list) != NULL) { 3025cb08fddSKristof Provost TAILQ_INIT(&por->por_rule.nat.list); 3035cb08fddSKristof Provost pfctl_move_pool(&r->nat, &por->por_rule.nat); 3045cb08fddSKristof Provost } else 3055cb08fddSKristof Provost bzero(&por->por_rule.nat, 3065cb08fddSKristof Provost sizeof(por->por_rule.nat)); 307*0972294eSKristof Provost if (TAILQ_FIRST(&r->route.list) != NULL) { 308*0972294eSKristof Provost TAILQ_INIT(&por->por_rule.route.list); 309*0972294eSKristof Provost pfctl_move_pool(&r->route, &por->por_rule.route); 310*0972294eSKristof Provost } else 311*0972294eSKristof Provost bzero(&por->por_rule.route, 312*0972294eSKristof Provost sizeof(por->por_rule.route)); 3133b3a8eb9SGleb Smirnoff 3143b3a8eb9SGleb Smirnoff TAILQ_INSERT_TAIL(&opt_queue, por, por_entry); 3153b3a8eb9SGleb Smirnoff } 3163b3a8eb9SGleb Smirnoff 3173b3a8eb9SGleb Smirnoff TAILQ_INIT(&superblocks); 3183b3a8eb9SGleb Smirnoff if (construct_superblocks(pf, &opt_queue, &superblocks)) 3193b3a8eb9SGleb Smirnoff goto error; 3203b3a8eb9SGleb Smirnoff 3213b3a8eb9SGleb Smirnoff if (pf->optimize & PF_OPTIMIZE_PROFILE) { 3223b3a8eb9SGleb Smirnoff if (load_feedback_profile(pf, &superblocks)) 3233b3a8eb9SGleb Smirnoff goto error; 3243b3a8eb9SGleb Smirnoff } 3253b3a8eb9SGleb Smirnoff 3263b3a8eb9SGleb Smirnoff TAILQ_FOREACH(block, &superblocks, sb_entry) { 3273b3a8eb9SGleb Smirnoff if (optimize_superblock(pf, block)) 3283b3a8eb9SGleb Smirnoff goto error; 3293b3a8eb9SGleb Smirnoff } 3303b3a8eb9SGleb Smirnoff 3313b3a8eb9SGleb Smirnoff rs->anchor->refcnt = 0; 3323b3a8eb9SGleb Smirnoff while ((block = TAILQ_FIRST(&superblocks))) { 3333b3a8eb9SGleb Smirnoff TAILQ_REMOVE(&superblocks, block, sb_entry); 3343b3a8eb9SGleb Smirnoff 3353b3a8eb9SGleb Smirnoff while ((por = TAILQ_FIRST(&block->sb_rules))) { 3363b3a8eb9SGleb Smirnoff TAILQ_REMOVE(&block->sb_rules, por, por_entry); 3373b3a8eb9SGleb Smirnoff por->por_rule.nr = rs->anchor->refcnt++; 3383b3a8eb9SGleb Smirnoff if ((r = calloc(1, sizeof(*r))) == NULL) 3393b3a8eb9SGleb Smirnoff err(1, "calloc"); 3403b3a8eb9SGleb Smirnoff memcpy(r, &por->por_rule, sizeof(*r)); 341096efeb6SKristof Provost TAILQ_INIT(&r->rdr.list); 342096efeb6SKristof Provost pfctl_move_pool(&por->por_rule.rdr, &r->rdr); 3435cb08fddSKristof Provost TAILQ_INIT(&r->nat.list); 3445cb08fddSKristof Provost pfctl_move_pool(&por->por_rule.nat, &r->nat); 3453b3a8eb9SGleb Smirnoff TAILQ_INSERT_TAIL( 3463b3a8eb9SGleb Smirnoff rs->rules[PF_RULESET_FILTER].active.ptr, 3473b3a8eb9SGleb Smirnoff r, entries); 3483b3a8eb9SGleb Smirnoff free(por); 3493b3a8eb9SGleb Smirnoff } 3503b3a8eb9SGleb Smirnoff free(block); 3513b3a8eb9SGleb Smirnoff } 3523b3a8eb9SGleb Smirnoff 3533b3a8eb9SGleb Smirnoff return (0); 3543b3a8eb9SGleb Smirnoff 3553b3a8eb9SGleb Smirnoff error: 3563b3a8eb9SGleb Smirnoff while ((por = TAILQ_FIRST(&opt_queue))) { 3573b3a8eb9SGleb Smirnoff TAILQ_REMOVE(&opt_queue, por, por_entry); 3583b3a8eb9SGleb Smirnoff if (por->por_src_tbl) { 3593b3a8eb9SGleb Smirnoff pfr_buf_clear(por->por_src_tbl->pt_buf); 3603b3a8eb9SGleb Smirnoff free(por->por_src_tbl->pt_buf); 3613b3a8eb9SGleb Smirnoff free(por->por_src_tbl); 3623b3a8eb9SGleb Smirnoff } 3633b3a8eb9SGleb Smirnoff if (por->por_dst_tbl) { 3643b3a8eb9SGleb Smirnoff pfr_buf_clear(por->por_dst_tbl->pt_buf); 3653b3a8eb9SGleb Smirnoff free(por->por_dst_tbl->pt_buf); 3663b3a8eb9SGleb Smirnoff free(por->por_dst_tbl); 3673b3a8eb9SGleb Smirnoff } 3683b3a8eb9SGleb Smirnoff free(por); 3693b3a8eb9SGleb Smirnoff } 3703b3a8eb9SGleb Smirnoff while ((block = TAILQ_FIRST(&superblocks))) { 3713b3a8eb9SGleb Smirnoff TAILQ_REMOVE(&superblocks, block, sb_entry); 3723b3a8eb9SGleb Smirnoff superblock_free(pf, block); 3733b3a8eb9SGleb Smirnoff } 3743b3a8eb9SGleb Smirnoff return (1); 3753b3a8eb9SGleb Smirnoff } 3763b3a8eb9SGleb Smirnoff 3773b3a8eb9SGleb Smirnoff 3783b3a8eb9SGleb Smirnoff /* 3793b3a8eb9SGleb Smirnoff * Go ahead and optimize a superblock 3803b3a8eb9SGleb Smirnoff */ 3813b3a8eb9SGleb Smirnoff int 3823b3a8eb9SGleb Smirnoff optimize_superblock(struct pfctl *pf, struct superblock *block) 3833b3a8eb9SGleb Smirnoff { 3843b3a8eb9SGleb Smirnoff #ifdef OPT_DEBUG 3853b3a8eb9SGleb Smirnoff struct pf_opt_rule *por; 3863b3a8eb9SGleb Smirnoff #endif /* OPT_DEBUG */ 3873b3a8eb9SGleb Smirnoff 3883b3a8eb9SGleb Smirnoff /* We have a few optimization passes: 3893b3a8eb9SGleb Smirnoff * 1) remove duplicate rules or rules that are a subset of other 3903b3a8eb9SGleb Smirnoff * rules 3913b3a8eb9SGleb Smirnoff * 2) combine otherwise identical rules with different IP addresses 3923b3a8eb9SGleb Smirnoff * into a single rule and put the addresses in a table. 3933b3a8eb9SGleb Smirnoff * 3) re-order the rules to improve kernel skip steps 3943b3a8eb9SGleb Smirnoff * 4) re-order the 'quick' rules based on feedback from the 3953b3a8eb9SGleb Smirnoff * active ruleset statistics 3963b3a8eb9SGleb Smirnoff * 3973b3a8eb9SGleb Smirnoff * XXX combine_rules() doesn't combine v4 and v6 rules. would just 3983b3a8eb9SGleb Smirnoff * have to keep af in the table container, make af 'COMBINE' and 3993b3a8eb9SGleb Smirnoff * twiddle the af on the merged rule 4003b3a8eb9SGleb Smirnoff * XXX maybe add a weighting to the metric on skipsteps when doing 4013b3a8eb9SGleb Smirnoff * reordering. sometimes two sequential tables will be better 4023b3a8eb9SGleb Smirnoff * that four consecutive interfaces. 4033b3a8eb9SGleb Smirnoff * XXX need to adjust the skipstep count of everything after PROTO, 4043b3a8eb9SGleb Smirnoff * since they aren't actually checked on a proto mismatch in 4053b3a8eb9SGleb Smirnoff * pf_test_{tcp, udp, icmp}() 4063b3a8eb9SGleb Smirnoff * XXX should i treat proto=0, af=0 or dir=0 special in skepstep 4073b3a8eb9SGleb Smirnoff * calculation since they are a DC? 4083b3a8eb9SGleb Smirnoff * XXX keep last skiplist of last superblock to influence this 4093b3a8eb9SGleb Smirnoff * superblock. '5 inet6 log' should make '3 inet6' come before '4 4103b3a8eb9SGleb Smirnoff * inet' in the next superblock. 4113b3a8eb9SGleb Smirnoff * XXX would be useful to add tables for ports 4123b3a8eb9SGleb Smirnoff * XXX we can also re-order some mutually exclusive superblocks to 4133b3a8eb9SGleb Smirnoff * try merging superblocks before any of these optimization passes. 4143b3a8eb9SGleb Smirnoff * for instance a single 'log in' rule in the middle of non-logging 4153b3a8eb9SGleb Smirnoff * out rules. 4163b3a8eb9SGleb Smirnoff */ 4173b3a8eb9SGleb Smirnoff 4183b3a8eb9SGleb Smirnoff /* shortcut. there will be a lot of 1-rule superblocks */ 4193b3a8eb9SGleb Smirnoff if (!TAILQ_NEXT(TAILQ_FIRST(&block->sb_rules), por_entry)) 4203b3a8eb9SGleb Smirnoff return (0); 4213b3a8eb9SGleb Smirnoff 4223b3a8eb9SGleb Smirnoff #ifdef OPT_DEBUG 4233b3a8eb9SGleb Smirnoff printf("--- Superblock ---\n"); 4243b3a8eb9SGleb Smirnoff TAILQ_FOREACH(por, &block->sb_rules, por_entry) { 4253b3a8eb9SGleb Smirnoff printf(" "); 4263b3a8eb9SGleb Smirnoff print_rule(&por->por_rule, por->por_rule.anchor ? 4273b3a8eb9SGleb Smirnoff por->por_rule.anchor->name : "", 1, 0); 4283b3a8eb9SGleb Smirnoff } 4293b3a8eb9SGleb Smirnoff #endif /* OPT_DEBUG */ 4303b3a8eb9SGleb Smirnoff 4313b3a8eb9SGleb Smirnoff 4323b3a8eb9SGleb Smirnoff if (remove_identical_rules(pf, block)) 4333b3a8eb9SGleb Smirnoff return (1); 4343b3a8eb9SGleb Smirnoff if (combine_rules(pf, block)) 4353b3a8eb9SGleb Smirnoff return (1); 4363b3a8eb9SGleb Smirnoff if ((pf->optimize & PF_OPTIMIZE_PROFILE) && 4373b3a8eb9SGleb Smirnoff TAILQ_FIRST(&block->sb_rules)->por_rule.quick && 4383b3a8eb9SGleb Smirnoff block->sb_profiled_block) { 4393b3a8eb9SGleb Smirnoff if (block_feedback(pf, block)) 4403b3a8eb9SGleb Smirnoff return (1); 4413b3a8eb9SGleb Smirnoff } else if (reorder_rules(pf, block, 0)) { 4423b3a8eb9SGleb Smirnoff return (1); 4433b3a8eb9SGleb Smirnoff } 4443b3a8eb9SGleb Smirnoff 4453b3a8eb9SGleb Smirnoff /* 4463b3a8eb9SGleb Smirnoff * Don't add any optimization passes below reorder_rules(). It will 4473b3a8eb9SGleb Smirnoff * have divided superblocks into smaller blocks for further refinement 4483b3a8eb9SGleb Smirnoff * and doesn't put them back together again. What once was a true 4493b3a8eb9SGleb Smirnoff * superblock might have been split into multiple superblocks. 4503b3a8eb9SGleb Smirnoff */ 4513b3a8eb9SGleb Smirnoff 4523b3a8eb9SGleb Smirnoff #ifdef OPT_DEBUG 4533b3a8eb9SGleb Smirnoff printf("--- END Superblock ---\n"); 4543b3a8eb9SGleb Smirnoff #endif /* OPT_DEBUG */ 4553b3a8eb9SGleb Smirnoff return (0); 4563b3a8eb9SGleb Smirnoff } 4573b3a8eb9SGleb Smirnoff 4583b3a8eb9SGleb Smirnoff 4593b3a8eb9SGleb Smirnoff /* 4603b3a8eb9SGleb Smirnoff * Optimization pass #1: remove identical rules 4613b3a8eb9SGleb Smirnoff */ 4623b3a8eb9SGleb Smirnoff int 4633b3a8eb9SGleb Smirnoff remove_identical_rules(struct pfctl *pf, struct superblock *block) 4643b3a8eb9SGleb Smirnoff { 4653b3a8eb9SGleb Smirnoff struct pf_opt_rule *por1, *por2, *por_next, *por2_next; 466e9eb0941SKristof Provost struct pfctl_rule a, a2, b, b2; 4673b3a8eb9SGleb Smirnoff 4683b3a8eb9SGleb Smirnoff for (por1 = TAILQ_FIRST(&block->sb_rules); por1; por1 = por_next) { 4693b3a8eb9SGleb Smirnoff por_next = TAILQ_NEXT(por1, por_entry); 4703b3a8eb9SGleb Smirnoff for (por2 = por_next; por2; por2 = por2_next) { 4713b3a8eb9SGleb Smirnoff por2_next = TAILQ_NEXT(por2, por_entry); 4723b3a8eb9SGleb Smirnoff comparable_rule(&a, &por1->por_rule, DC); 4733b3a8eb9SGleb Smirnoff comparable_rule(&b, &por2->por_rule, DC); 4743b3a8eb9SGleb Smirnoff memcpy(&a2, &a, sizeof(a2)); 4753b3a8eb9SGleb Smirnoff memcpy(&b2, &b, sizeof(b2)); 4763b3a8eb9SGleb Smirnoff 4773b3a8eb9SGleb Smirnoff exclude_supersets(&a, &b); 4783b3a8eb9SGleb Smirnoff exclude_supersets(&b2, &a2); 4793b3a8eb9SGleb Smirnoff if (memcmp(&a, &b, sizeof(a)) == 0) { 4803b3a8eb9SGleb Smirnoff DEBUG("removing identical rule nr%d = *nr%d*", 4813b3a8eb9SGleb Smirnoff por1->por_rule.nr, por2->por_rule.nr); 4823b3a8eb9SGleb Smirnoff TAILQ_REMOVE(&block->sb_rules, por2, por_entry); 4833b3a8eb9SGleb Smirnoff if (por_next == por2) 4843b3a8eb9SGleb Smirnoff por_next = TAILQ_NEXT(por1, por_entry); 4853b3a8eb9SGleb Smirnoff free(por2); 4863b3a8eb9SGleb Smirnoff } else if (memcmp(&a2, &b2, sizeof(a2)) == 0) { 4873b3a8eb9SGleb Smirnoff DEBUG("removing identical rule *nr%d* = nr%d", 4883b3a8eb9SGleb Smirnoff por1->por_rule.nr, por2->por_rule.nr); 4893b3a8eb9SGleb Smirnoff TAILQ_REMOVE(&block->sb_rules, por1, por_entry); 4903b3a8eb9SGleb Smirnoff free(por1); 4913b3a8eb9SGleb Smirnoff break; 4923b3a8eb9SGleb Smirnoff } 4933b3a8eb9SGleb Smirnoff } 4943b3a8eb9SGleb Smirnoff } 4953b3a8eb9SGleb Smirnoff 4963b3a8eb9SGleb Smirnoff return (0); 4973b3a8eb9SGleb Smirnoff } 4983b3a8eb9SGleb Smirnoff 4993b3a8eb9SGleb Smirnoff 5003b3a8eb9SGleb Smirnoff /* 5013b3a8eb9SGleb Smirnoff * Optimization pass #2: combine similar rules with different addresses 5023b3a8eb9SGleb Smirnoff * into a single rule and a table 5033b3a8eb9SGleb Smirnoff */ 5043b3a8eb9SGleb Smirnoff int 5053b3a8eb9SGleb Smirnoff combine_rules(struct pfctl *pf, struct superblock *block) 5063b3a8eb9SGleb Smirnoff { 5073b3a8eb9SGleb Smirnoff struct pf_opt_rule *p1, *p2, *por_next; 5083b3a8eb9SGleb Smirnoff int src_eq, dst_eq; 5093b3a8eb9SGleb Smirnoff 5103b3a8eb9SGleb Smirnoff if ((pf->loadopt & PFCTL_FLAG_TABLE) == 0) { 5113b3a8eb9SGleb Smirnoff warnx("Must enable table loading for optimizations"); 5123b3a8eb9SGleb Smirnoff return (1); 5133b3a8eb9SGleb Smirnoff } 5143b3a8eb9SGleb Smirnoff 5153b3a8eb9SGleb Smirnoff /* First we make a pass to combine the rules. O(n log n) */ 5163b3a8eb9SGleb Smirnoff TAILQ_FOREACH(p1, &block->sb_rules, por_entry) { 5173b3a8eb9SGleb Smirnoff for (p2 = TAILQ_NEXT(p1, por_entry); p2; p2 = por_next) { 5183b3a8eb9SGleb Smirnoff por_next = TAILQ_NEXT(p2, por_entry); 5193b3a8eb9SGleb Smirnoff 5203b3a8eb9SGleb Smirnoff src_eq = addrs_equal(&p1->por_rule.src, 5213b3a8eb9SGleb Smirnoff &p2->por_rule.src); 5223b3a8eb9SGleb Smirnoff dst_eq = addrs_equal(&p1->por_rule.dst, 5233b3a8eb9SGleb Smirnoff &p2->por_rule.dst); 5243b3a8eb9SGleb Smirnoff 5253b3a8eb9SGleb Smirnoff if (src_eq && !dst_eq && p1->por_src_tbl == NULL && 5263b3a8eb9SGleb Smirnoff p2->por_dst_tbl == NULL && 5273b3a8eb9SGleb Smirnoff p2->por_src_tbl == NULL && 5283b3a8eb9SGleb Smirnoff rules_combineable(&p1->por_rule, &p2->por_rule) && 5293b3a8eb9SGleb Smirnoff addrs_combineable(&p1->por_rule.dst, 5303b3a8eb9SGleb Smirnoff &p2->por_rule.dst)) { 5313b3a8eb9SGleb Smirnoff DEBUG("can combine rules nr%d = nr%d", 5323b3a8eb9SGleb Smirnoff p1->por_rule.nr, p2->por_rule.nr); 5333b3a8eb9SGleb Smirnoff if (p1->por_dst_tbl == NULL && 5343b3a8eb9SGleb Smirnoff add_opt_table(pf, &p1->por_dst_tbl, 5353b3a8eb9SGleb Smirnoff p1->por_rule.af, &p1->por_rule.dst)) 5363b3a8eb9SGleb Smirnoff return (1); 5373b3a8eb9SGleb Smirnoff if (add_opt_table(pf, &p1->por_dst_tbl, 5383b3a8eb9SGleb Smirnoff p1->por_rule.af, &p2->por_rule.dst)) 5393b3a8eb9SGleb Smirnoff return (1); 5403b3a8eb9SGleb Smirnoff p2->por_dst_tbl = p1->por_dst_tbl; 5413b3a8eb9SGleb Smirnoff if (p1->por_dst_tbl->pt_rulecount >= 5423b3a8eb9SGleb Smirnoff TABLE_THRESHOLD) { 5433b3a8eb9SGleb Smirnoff TAILQ_REMOVE(&block->sb_rules, p2, 5443b3a8eb9SGleb Smirnoff por_entry); 5453b3a8eb9SGleb Smirnoff free(p2); 5463b3a8eb9SGleb Smirnoff } 5473b3a8eb9SGleb Smirnoff } else if (!src_eq && dst_eq && p1->por_dst_tbl == NULL 5483b3a8eb9SGleb Smirnoff && p2->por_src_tbl == NULL && 5493b3a8eb9SGleb Smirnoff p2->por_dst_tbl == NULL && 5503b3a8eb9SGleb Smirnoff rules_combineable(&p1->por_rule, &p2->por_rule) && 5513b3a8eb9SGleb Smirnoff addrs_combineable(&p1->por_rule.src, 5523b3a8eb9SGleb Smirnoff &p2->por_rule.src)) { 5533b3a8eb9SGleb Smirnoff DEBUG("can combine rules nr%d = nr%d", 5543b3a8eb9SGleb Smirnoff p1->por_rule.nr, p2->por_rule.nr); 5553b3a8eb9SGleb Smirnoff if (p1->por_src_tbl == NULL && 5563b3a8eb9SGleb Smirnoff add_opt_table(pf, &p1->por_src_tbl, 5573b3a8eb9SGleb Smirnoff p1->por_rule.af, &p1->por_rule.src)) 5583b3a8eb9SGleb Smirnoff return (1); 5593b3a8eb9SGleb Smirnoff if (add_opt_table(pf, &p1->por_src_tbl, 5603b3a8eb9SGleb Smirnoff p1->por_rule.af, &p2->por_rule.src)) 5613b3a8eb9SGleb Smirnoff return (1); 5623b3a8eb9SGleb Smirnoff p2->por_src_tbl = p1->por_src_tbl; 5633b3a8eb9SGleb Smirnoff if (p1->por_src_tbl->pt_rulecount >= 5643b3a8eb9SGleb Smirnoff TABLE_THRESHOLD) { 5653b3a8eb9SGleb Smirnoff TAILQ_REMOVE(&block->sb_rules, p2, 5663b3a8eb9SGleb Smirnoff por_entry); 5673b3a8eb9SGleb Smirnoff free(p2); 5683b3a8eb9SGleb Smirnoff } 5693b3a8eb9SGleb Smirnoff } 5703b3a8eb9SGleb Smirnoff } 5713b3a8eb9SGleb Smirnoff } 5723b3a8eb9SGleb Smirnoff 5733b3a8eb9SGleb Smirnoff 5743b3a8eb9SGleb Smirnoff /* 5753b3a8eb9SGleb Smirnoff * Then we make a final pass to create a valid table name and 5763b3a8eb9SGleb Smirnoff * insert the name into the rules. 5773b3a8eb9SGleb Smirnoff */ 5783b3a8eb9SGleb Smirnoff for (p1 = TAILQ_FIRST(&block->sb_rules); p1; p1 = por_next) { 5793b3a8eb9SGleb Smirnoff por_next = TAILQ_NEXT(p1, por_entry); 5803b3a8eb9SGleb Smirnoff assert(p1->por_src_tbl == NULL || p1->por_dst_tbl == NULL); 5813b3a8eb9SGleb Smirnoff 5823b3a8eb9SGleb Smirnoff if (p1->por_src_tbl && p1->por_src_tbl->pt_rulecount >= 5833b3a8eb9SGleb Smirnoff TABLE_THRESHOLD) { 5843b3a8eb9SGleb Smirnoff if (p1->por_src_tbl->pt_generated) { 5853b3a8eb9SGleb Smirnoff /* This rule is included in a table */ 5863b3a8eb9SGleb Smirnoff TAILQ_REMOVE(&block->sb_rules, p1, por_entry); 5873b3a8eb9SGleb Smirnoff free(p1); 5883b3a8eb9SGleb Smirnoff continue; 5893b3a8eb9SGleb Smirnoff } 5903b3a8eb9SGleb Smirnoff p1->por_src_tbl->pt_generated = 1; 5913b3a8eb9SGleb Smirnoff 5923b3a8eb9SGleb Smirnoff if ((pf->opts & PF_OPT_NOACTION) == 0 && 5933b3a8eb9SGleb Smirnoff pf_opt_create_table(pf, p1->por_src_tbl)) 5943b3a8eb9SGleb Smirnoff return (1); 5953b3a8eb9SGleb Smirnoff 5963b3a8eb9SGleb Smirnoff pf->tdirty = 1; 5973b3a8eb9SGleb Smirnoff 5983b3a8eb9SGleb Smirnoff if (pf->opts & PF_OPT_VERBOSE) 5993b3a8eb9SGleb Smirnoff print_tabledef(p1->por_src_tbl->pt_name, 6003b3a8eb9SGleb Smirnoff PFR_TFLAG_CONST, 1, 6013b3a8eb9SGleb Smirnoff &p1->por_src_tbl->pt_nodes); 6023b3a8eb9SGleb Smirnoff 6033b3a8eb9SGleb Smirnoff memset(&p1->por_rule.src.addr, 0, 6043b3a8eb9SGleb Smirnoff sizeof(p1->por_rule.src.addr)); 6053b3a8eb9SGleb Smirnoff p1->por_rule.src.addr.type = PF_ADDR_TABLE; 6063b3a8eb9SGleb Smirnoff strlcpy(p1->por_rule.src.addr.v.tblname, 6073b3a8eb9SGleb Smirnoff p1->por_src_tbl->pt_name, 6083b3a8eb9SGleb Smirnoff sizeof(p1->por_rule.src.addr.v.tblname)); 6093b3a8eb9SGleb Smirnoff 6103b3a8eb9SGleb Smirnoff pfr_buf_clear(p1->por_src_tbl->pt_buf); 6113b3a8eb9SGleb Smirnoff free(p1->por_src_tbl->pt_buf); 6123b3a8eb9SGleb Smirnoff p1->por_src_tbl->pt_buf = NULL; 6133b3a8eb9SGleb Smirnoff } 6143b3a8eb9SGleb Smirnoff if (p1->por_dst_tbl && p1->por_dst_tbl->pt_rulecount >= 6153b3a8eb9SGleb Smirnoff TABLE_THRESHOLD) { 6163b3a8eb9SGleb Smirnoff if (p1->por_dst_tbl->pt_generated) { 6173b3a8eb9SGleb Smirnoff /* This rule is included in a table */ 6183b3a8eb9SGleb Smirnoff TAILQ_REMOVE(&block->sb_rules, p1, por_entry); 6193b3a8eb9SGleb Smirnoff free(p1); 6203b3a8eb9SGleb Smirnoff continue; 6213b3a8eb9SGleb Smirnoff } 6223b3a8eb9SGleb Smirnoff p1->por_dst_tbl->pt_generated = 1; 6233b3a8eb9SGleb Smirnoff 6243b3a8eb9SGleb Smirnoff if ((pf->opts & PF_OPT_NOACTION) == 0 && 6253b3a8eb9SGleb Smirnoff pf_opt_create_table(pf, p1->por_dst_tbl)) 6263b3a8eb9SGleb Smirnoff return (1); 6273b3a8eb9SGleb Smirnoff pf->tdirty = 1; 6283b3a8eb9SGleb Smirnoff 6293b3a8eb9SGleb Smirnoff if (pf->opts & PF_OPT_VERBOSE) 6303b3a8eb9SGleb Smirnoff print_tabledef(p1->por_dst_tbl->pt_name, 6313b3a8eb9SGleb Smirnoff PFR_TFLAG_CONST, 1, 6323b3a8eb9SGleb Smirnoff &p1->por_dst_tbl->pt_nodes); 6333b3a8eb9SGleb Smirnoff 6343b3a8eb9SGleb Smirnoff memset(&p1->por_rule.dst.addr, 0, 6353b3a8eb9SGleb Smirnoff sizeof(p1->por_rule.dst.addr)); 6363b3a8eb9SGleb Smirnoff p1->por_rule.dst.addr.type = PF_ADDR_TABLE; 6373b3a8eb9SGleb Smirnoff strlcpy(p1->por_rule.dst.addr.v.tblname, 6383b3a8eb9SGleb Smirnoff p1->por_dst_tbl->pt_name, 6393b3a8eb9SGleb Smirnoff sizeof(p1->por_rule.dst.addr.v.tblname)); 6403b3a8eb9SGleb Smirnoff 6413b3a8eb9SGleb Smirnoff pfr_buf_clear(p1->por_dst_tbl->pt_buf); 6423b3a8eb9SGleb Smirnoff free(p1->por_dst_tbl->pt_buf); 6433b3a8eb9SGleb Smirnoff p1->por_dst_tbl->pt_buf = NULL; 6443b3a8eb9SGleb Smirnoff } 6453b3a8eb9SGleb Smirnoff } 6463b3a8eb9SGleb Smirnoff 6473b3a8eb9SGleb Smirnoff return (0); 6483b3a8eb9SGleb Smirnoff } 6493b3a8eb9SGleb Smirnoff 6503b3a8eb9SGleb Smirnoff 6513b3a8eb9SGleb Smirnoff /* 6523b3a8eb9SGleb Smirnoff * Optimization pass #3: re-order rules to improve skip steps 6533b3a8eb9SGleb Smirnoff */ 6543b3a8eb9SGleb Smirnoff int 6553b3a8eb9SGleb Smirnoff reorder_rules(struct pfctl *pf, struct superblock *block, int depth) 6563b3a8eb9SGleb Smirnoff { 6573b3a8eb9SGleb Smirnoff struct superblock *newblock; 6583b3a8eb9SGleb Smirnoff struct pf_skip_step *skiplist; 6593b3a8eb9SGleb Smirnoff struct pf_opt_rule *por; 6603b3a8eb9SGleb Smirnoff int i, largest, largest_list, rule_count = 0; 6613b3a8eb9SGleb Smirnoff TAILQ_HEAD( , pf_opt_rule) head; 6623b3a8eb9SGleb Smirnoff 6633b3a8eb9SGleb Smirnoff /* 6643b3a8eb9SGleb Smirnoff * Calculate the best-case skip steps. We put each rule in a list 6653b3a8eb9SGleb Smirnoff * of other rules with common fields 6663b3a8eb9SGleb Smirnoff */ 6673b3a8eb9SGleb Smirnoff for (i = 0; i < PF_SKIP_COUNT; i++) { 6683b3a8eb9SGleb Smirnoff TAILQ_FOREACH(por, &block->sb_rules, por_entry) { 6693b3a8eb9SGleb Smirnoff TAILQ_FOREACH(skiplist, &block->sb_skipsteps[i], 6703b3a8eb9SGleb Smirnoff ps_entry) { 6713b3a8eb9SGleb Smirnoff if (skip_compare(i, skiplist, por) == 0) 6723b3a8eb9SGleb Smirnoff break; 6733b3a8eb9SGleb Smirnoff } 6743b3a8eb9SGleb Smirnoff if (skiplist == NULL) { 6753b3a8eb9SGleb Smirnoff if ((skiplist = calloc(1, sizeof(*skiplist))) == 6763b3a8eb9SGleb Smirnoff NULL) 6773b3a8eb9SGleb Smirnoff err(1, "calloc"); 6783b3a8eb9SGleb Smirnoff TAILQ_INIT(&skiplist->ps_rules); 6793b3a8eb9SGleb Smirnoff TAILQ_INSERT_TAIL(&block->sb_skipsteps[i], 6803b3a8eb9SGleb Smirnoff skiplist, ps_entry); 6813b3a8eb9SGleb Smirnoff } 6823b3a8eb9SGleb Smirnoff skip_append(block, i, skiplist, por); 6833b3a8eb9SGleb Smirnoff } 6843b3a8eb9SGleb Smirnoff } 6853b3a8eb9SGleb Smirnoff 6863b3a8eb9SGleb Smirnoff TAILQ_FOREACH(por, &block->sb_rules, por_entry) 6873b3a8eb9SGleb Smirnoff rule_count++; 6883b3a8eb9SGleb Smirnoff 6893b3a8eb9SGleb Smirnoff /* 6903b3a8eb9SGleb Smirnoff * Now we're going to ignore any fields that are identical between 6913b3a8eb9SGleb Smirnoff * all of the rules in the superblock and those fields which differ 6923b3a8eb9SGleb Smirnoff * between every rule in the superblock. 6933b3a8eb9SGleb Smirnoff */ 6943b3a8eb9SGleb Smirnoff largest = 0; 6953b3a8eb9SGleb Smirnoff for (i = 0; i < PF_SKIP_COUNT; i++) { 6963b3a8eb9SGleb Smirnoff skiplist = TAILQ_FIRST(&block->sb_skipsteps[i]); 6973b3a8eb9SGleb Smirnoff if (skiplist->ps_count == rule_count) { 6983b3a8eb9SGleb Smirnoff DEBUG("(%d) original skipstep '%s' is all rules", 6993b3a8eb9SGleb Smirnoff depth, skip_comparitors_names[i]); 7003b3a8eb9SGleb Smirnoff skiplist->ps_count = 0; 7013b3a8eb9SGleb Smirnoff } else if (skiplist->ps_count == 1) { 7023b3a8eb9SGleb Smirnoff skiplist->ps_count = 0; 7033b3a8eb9SGleb Smirnoff } else { 7043b3a8eb9SGleb Smirnoff DEBUG("(%d) original skipstep '%s' largest jump is %d", 7053b3a8eb9SGleb Smirnoff depth, skip_comparitors_names[i], 7063b3a8eb9SGleb Smirnoff skiplist->ps_count); 7073b3a8eb9SGleb Smirnoff if (skiplist->ps_count > largest) 7083b3a8eb9SGleb Smirnoff largest = skiplist->ps_count; 7093b3a8eb9SGleb Smirnoff } 7103b3a8eb9SGleb Smirnoff } 7113b3a8eb9SGleb Smirnoff if (largest == 0) { 7123b3a8eb9SGleb Smirnoff /* Ugh. There is NO commonality in the superblock on which 7133b3a8eb9SGleb Smirnoff * optimize the skipsteps optimization. 7143b3a8eb9SGleb Smirnoff */ 7153b3a8eb9SGleb Smirnoff goto done; 7163b3a8eb9SGleb Smirnoff } 7173b3a8eb9SGleb Smirnoff 7183b3a8eb9SGleb Smirnoff /* 7193b3a8eb9SGleb Smirnoff * Now we're going to empty the superblock rule list and re-create 7203b3a8eb9SGleb Smirnoff * it based on a more optimal skipstep order. 7213b3a8eb9SGleb Smirnoff */ 7223b3a8eb9SGleb Smirnoff TAILQ_INIT(&head); 7233b3a8eb9SGleb Smirnoff while ((por = TAILQ_FIRST(&block->sb_rules))) { 7243b3a8eb9SGleb Smirnoff TAILQ_REMOVE(&block->sb_rules, por, por_entry); 7253b3a8eb9SGleb Smirnoff TAILQ_INSERT_TAIL(&head, por, por_entry); 7263b3a8eb9SGleb Smirnoff } 7273b3a8eb9SGleb Smirnoff 7283b3a8eb9SGleb Smirnoff 7293b3a8eb9SGleb Smirnoff while (!TAILQ_EMPTY(&head)) { 7303b3a8eb9SGleb Smirnoff largest = 1; 7313b3a8eb9SGleb Smirnoff 7323b3a8eb9SGleb Smirnoff /* 7333b3a8eb9SGleb Smirnoff * Find the most useful skip steps remaining 7343b3a8eb9SGleb Smirnoff */ 7353b3a8eb9SGleb Smirnoff for (i = 0; i < PF_SKIP_COUNT; i++) { 7363b3a8eb9SGleb Smirnoff skiplist = TAILQ_FIRST(&block->sb_skipsteps[i]); 7373b3a8eb9SGleb Smirnoff if (skiplist->ps_count > largest) { 7383b3a8eb9SGleb Smirnoff largest = skiplist->ps_count; 7393b3a8eb9SGleb Smirnoff largest_list = i; 7403b3a8eb9SGleb Smirnoff } 7413b3a8eb9SGleb Smirnoff } 7423b3a8eb9SGleb Smirnoff 7433b3a8eb9SGleb Smirnoff if (largest <= 1) { 7443b3a8eb9SGleb Smirnoff /* 7453b3a8eb9SGleb Smirnoff * Nothing useful left. Leave remaining rules in order. 7463b3a8eb9SGleb Smirnoff */ 7473b3a8eb9SGleb Smirnoff DEBUG("(%d) no more commonality for skip steps", depth); 7483b3a8eb9SGleb Smirnoff while ((por = TAILQ_FIRST(&head))) { 7493b3a8eb9SGleb Smirnoff TAILQ_REMOVE(&head, por, por_entry); 7503b3a8eb9SGleb Smirnoff TAILQ_INSERT_TAIL(&block->sb_rules, por, 7513b3a8eb9SGleb Smirnoff por_entry); 7523b3a8eb9SGleb Smirnoff } 7533b3a8eb9SGleb Smirnoff } else { 7543b3a8eb9SGleb Smirnoff /* 7553b3a8eb9SGleb Smirnoff * There is commonality. Extract those common rules 7563b3a8eb9SGleb Smirnoff * and place them in the ruleset adjacent to each 7573b3a8eb9SGleb Smirnoff * other. 7583b3a8eb9SGleb Smirnoff */ 7593b3a8eb9SGleb Smirnoff skiplist = TAILQ_FIRST(&block->sb_skipsteps[ 7603b3a8eb9SGleb Smirnoff largest_list]); 7613b3a8eb9SGleb Smirnoff DEBUG("(%d) skipstep '%s' largest jump is %d @ #%d", 7623b3a8eb9SGleb Smirnoff depth, skip_comparitors_names[largest_list], 7633b3a8eb9SGleb Smirnoff largest, TAILQ_FIRST(&TAILQ_FIRST(&block-> 7643b3a8eb9SGleb Smirnoff sb_skipsteps [largest_list])->ps_rules)-> 7653b3a8eb9SGleb Smirnoff por_rule.nr); 7663b3a8eb9SGleb Smirnoff TAILQ_REMOVE(&block->sb_skipsteps[largest_list], 7673b3a8eb9SGleb Smirnoff skiplist, ps_entry); 7683b3a8eb9SGleb Smirnoff 7693b3a8eb9SGleb Smirnoff 7703b3a8eb9SGleb Smirnoff /* 7713b3a8eb9SGleb Smirnoff * There may be further commonality inside these 7723b3a8eb9SGleb Smirnoff * rules. So we'll split them off into they're own 7733b3a8eb9SGleb Smirnoff * superblock and pass it back into the optimizer. 7743b3a8eb9SGleb Smirnoff */ 7753b3a8eb9SGleb Smirnoff if (skiplist->ps_count > 2) { 7763b3a8eb9SGleb Smirnoff if ((newblock = calloc(1, sizeof(*newblock))) 7773b3a8eb9SGleb Smirnoff == NULL) { 7783b3a8eb9SGleb Smirnoff warn("calloc"); 7793b3a8eb9SGleb Smirnoff return (1); 7803b3a8eb9SGleb Smirnoff } 7813b3a8eb9SGleb Smirnoff TAILQ_INIT(&newblock->sb_rules); 7823b3a8eb9SGleb Smirnoff for (i = 0; i < PF_SKIP_COUNT; i++) 7833b3a8eb9SGleb Smirnoff TAILQ_INIT(&newblock->sb_skipsteps[i]); 7843b3a8eb9SGleb Smirnoff TAILQ_INSERT_BEFORE(block, newblock, sb_entry); 7853b3a8eb9SGleb Smirnoff DEBUG("(%d) splitting off %d rules from superblock @ #%d", 7863b3a8eb9SGleb Smirnoff depth, skiplist->ps_count, 7873b3a8eb9SGleb Smirnoff TAILQ_FIRST(&skiplist->ps_rules)-> 7883b3a8eb9SGleb Smirnoff por_rule.nr); 7893b3a8eb9SGleb Smirnoff } else { 7903b3a8eb9SGleb Smirnoff newblock = block; 7913b3a8eb9SGleb Smirnoff } 7923b3a8eb9SGleb Smirnoff 7933b3a8eb9SGleb Smirnoff while ((por = TAILQ_FIRST(&skiplist->ps_rules))) { 7943b3a8eb9SGleb Smirnoff TAILQ_REMOVE(&head, por, por_entry); 7953b3a8eb9SGleb Smirnoff TAILQ_REMOVE(&skiplist->ps_rules, por, 7963b3a8eb9SGleb Smirnoff por_skip_entry[largest_list]); 7973b3a8eb9SGleb Smirnoff TAILQ_INSERT_TAIL(&newblock->sb_rules, por, 7983b3a8eb9SGleb Smirnoff por_entry); 7993b3a8eb9SGleb Smirnoff 8003b3a8eb9SGleb Smirnoff /* Remove this rule from all other skiplists */ 8013b3a8eb9SGleb Smirnoff remove_from_skipsteps(&block->sb_skipsteps[ 8023b3a8eb9SGleb Smirnoff largest_list], block, por, skiplist); 8033b3a8eb9SGleb Smirnoff } 8043b3a8eb9SGleb Smirnoff free(skiplist); 8053b3a8eb9SGleb Smirnoff if (newblock != block) 8063b3a8eb9SGleb Smirnoff if (reorder_rules(pf, newblock, depth + 1)) 8073b3a8eb9SGleb Smirnoff return (1); 8083b3a8eb9SGleb Smirnoff } 8093b3a8eb9SGleb Smirnoff } 8103b3a8eb9SGleb Smirnoff 8113b3a8eb9SGleb Smirnoff done: 8123b3a8eb9SGleb Smirnoff for (i = 0; i < PF_SKIP_COUNT; i++) { 8133b3a8eb9SGleb Smirnoff while ((skiplist = TAILQ_FIRST(&block->sb_skipsteps[i]))) { 8143b3a8eb9SGleb Smirnoff TAILQ_REMOVE(&block->sb_skipsteps[i], skiplist, 8153b3a8eb9SGleb Smirnoff ps_entry); 8163b3a8eb9SGleb Smirnoff free(skiplist); 8173b3a8eb9SGleb Smirnoff } 8183b3a8eb9SGleb Smirnoff } 8193b3a8eb9SGleb Smirnoff 8203b3a8eb9SGleb Smirnoff return (0); 8213b3a8eb9SGleb Smirnoff } 8223b3a8eb9SGleb Smirnoff 8233b3a8eb9SGleb Smirnoff 8243b3a8eb9SGleb Smirnoff /* 8253b3a8eb9SGleb Smirnoff * Optimization pass #4: re-order 'quick' rules based on feedback from the 8263b3a8eb9SGleb Smirnoff * currently running ruleset 8273b3a8eb9SGleb Smirnoff */ 8283b3a8eb9SGleb Smirnoff int 8293b3a8eb9SGleb Smirnoff block_feedback(struct pfctl *pf, struct superblock *block) 8303b3a8eb9SGleb Smirnoff { 8313b3a8eb9SGleb Smirnoff TAILQ_HEAD( , pf_opt_rule) queue; 8323b3a8eb9SGleb Smirnoff struct pf_opt_rule *por1, *por2; 833e9eb0941SKristof Provost struct pfctl_rule a, b; 8343b3a8eb9SGleb Smirnoff 8353b3a8eb9SGleb Smirnoff 8363b3a8eb9SGleb Smirnoff /* 8373b3a8eb9SGleb Smirnoff * Walk through all of the profiled superblock's rules and copy 8383b3a8eb9SGleb Smirnoff * the counters onto our rules. 8393b3a8eb9SGleb Smirnoff */ 8403b3a8eb9SGleb Smirnoff TAILQ_FOREACH(por1, &block->sb_profiled_block->sb_rules, por_entry) { 8413b3a8eb9SGleb Smirnoff comparable_rule(&a, &por1->por_rule, DC); 8423b3a8eb9SGleb Smirnoff TAILQ_FOREACH(por2, &block->sb_rules, por_entry) { 8433b3a8eb9SGleb Smirnoff if (por2->por_profile_count) 8443b3a8eb9SGleb Smirnoff continue; 8453b3a8eb9SGleb Smirnoff comparable_rule(&b, &por2->por_rule, DC); 8463b3a8eb9SGleb Smirnoff if (memcmp(&a, &b, sizeof(a)) == 0) { 8473b3a8eb9SGleb Smirnoff por2->por_profile_count = 8483b3a8eb9SGleb Smirnoff por1->por_rule.packets[0] + 8493b3a8eb9SGleb Smirnoff por1->por_rule.packets[1]; 8503b3a8eb9SGleb Smirnoff break; 8513b3a8eb9SGleb Smirnoff } 8523b3a8eb9SGleb Smirnoff } 8533b3a8eb9SGleb Smirnoff } 8543b3a8eb9SGleb Smirnoff superblock_free(pf, block->sb_profiled_block); 8553b3a8eb9SGleb Smirnoff block->sb_profiled_block = NULL; 8563b3a8eb9SGleb Smirnoff 8573b3a8eb9SGleb Smirnoff /* 8583b3a8eb9SGleb Smirnoff * Now we pull all of the rules off the superblock and re-insert them 8593b3a8eb9SGleb Smirnoff * in sorted order. 8603b3a8eb9SGleb Smirnoff */ 8613b3a8eb9SGleb Smirnoff 8623b3a8eb9SGleb Smirnoff TAILQ_INIT(&queue); 8633b3a8eb9SGleb Smirnoff while ((por1 = TAILQ_FIRST(&block->sb_rules)) != NULL) { 8643b3a8eb9SGleb Smirnoff TAILQ_REMOVE(&block->sb_rules, por1, por_entry); 8653b3a8eb9SGleb Smirnoff TAILQ_INSERT_TAIL(&queue, por1, por_entry); 8663b3a8eb9SGleb Smirnoff } 8673b3a8eb9SGleb Smirnoff 8683b3a8eb9SGleb Smirnoff while ((por1 = TAILQ_FIRST(&queue)) != NULL) { 8693b3a8eb9SGleb Smirnoff TAILQ_REMOVE(&queue, por1, por_entry); 8703b3a8eb9SGleb Smirnoff /* XXX I should sort all of the unused rules based on skip steps */ 8713b3a8eb9SGleb Smirnoff TAILQ_FOREACH(por2, &block->sb_rules, por_entry) { 8723b3a8eb9SGleb Smirnoff if (por1->por_profile_count > por2->por_profile_count) { 8733b3a8eb9SGleb Smirnoff TAILQ_INSERT_BEFORE(por2, por1, por_entry); 8743b3a8eb9SGleb Smirnoff break; 8753b3a8eb9SGleb Smirnoff } 8763b3a8eb9SGleb Smirnoff } 8773b3a8eb9SGleb Smirnoff #ifdef __FreeBSD__ 8783b3a8eb9SGleb Smirnoff if (por2 == NULL) 8793b3a8eb9SGleb Smirnoff #else 8803b3a8eb9SGleb Smirnoff if (por2 == TAILQ_END(&block->sb_rules)) 8813b3a8eb9SGleb Smirnoff #endif 8823b3a8eb9SGleb Smirnoff TAILQ_INSERT_TAIL(&block->sb_rules, por1, por_entry); 8833b3a8eb9SGleb Smirnoff } 8843b3a8eb9SGleb Smirnoff 8853b3a8eb9SGleb Smirnoff return (0); 8863b3a8eb9SGleb Smirnoff } 8873b3a8eb9SGleb Smirnoff 8883b3a8eb9SGleb Smirnoff 8893b3a8eb9SGleb Smirnoff /* 8903b3a8eb9SGleb Smirnoff * Load the current ruleset from the kernel and try to associate them with 8913b3a8eb9SGleb Smirnoff * the ruleset we're optimizing. 8923b3a8eb9SGleb Smirnoff */ 8933b3a8eb9SGleb Smirnoff int 8943b3a8eb9SGleb Smirnoff load_feedback_profile(struct pfctl *pf, struct superblocks *superblocks) 8953b3a8eb9SGleb Smirnoff { 89647a0b593SKristof Provost char anchor_call[MAXPATHLEN] = ""; 8973b3a8eb9SGleb Smirnoff struct superblock *block, *blockcur; 8983b3a8eb9SGleb Smirnoff struct superblocks prof_superblocks; 8993b3a8eb9SGleb Smirnoff struct pf_opt_rule *por; 9003b3a8eb9SGleb Smirnoff struct pf_opt_queue queue; 90147a0b593SKristof Provost struct pfctl_rules_info rules; 902e9eb0941SKristof Provost struct pfctl_rule a, b, rule; 9033b3a8eb9SGleb Smirnoff int nr, mnr; 9043b3a8eb9SGleb Smirnoff 9053b3a8eb9SGleb Smirnoff TAILQ_INIT(&queue); 9063b3a8eb9SGleb Smirnoff TAILQ_INIT(&prof_superblocks); 9073b3a8eb9SGleb Smirnoff 908f1612e70SKristof Provost if (pfctl_get_rules_info_h(pf->h, &rules, PF_PASS, "")) { 9093b3a8eb9SGleb Smirnoff warn("DIOCGETRULES"); 9103b3a8eb9SGleb Smirnoff return (1); 9113b3a8eb9SGleb Smirnoff } 91247a0b593SKristof Provost mnr = rules.nr; 9133b3a8eb9SGleb Smirnoff 9143b3a8eb9SGleb Smirnoff DEBUG("Loading %d active rules for a feedback profile", mnr); 9153b3a8eb9SGleb Smirnoff for (nr = 0; nr < mnr; ++nr) { 916e9eb0941SKristof Provost struct pfctl_ruleset *rs; 9173b3a8eb9SGleb Smirnoff if ((por = calloc(1, sizeof(*por))) == NULL) { 9183b3a8eb9SGleb Smirnoff warn("calloc"); 9193b3a8eb9SGleb Smirnoff return (1); 9203b3a8eb9SGleb Smirnoff } 9210d6c8174SKristof Provost 922cd2054d4SKristof Provost if (pfctl_get_rule_h(pf->h, nr, rules.ticket, "", PF_PASS, 92347a0b593SKristof Provost &rule, anchor_call)) { 9240d6c8174SKristof Provost warn("DIOCGETRULENV"); 9253b3a8eb9SGleb Smirnoff return (1); 9263b3a8eb9SGleb Smirnoff } 927e9eb0941SKristof Provost memcpy(&por->por_rule, &rule, sizeof(por->por_rule)); 92847a0b593SKristof Provost rs = pf_find_or_create_ruleset(anchor_call); 9293b3a8eb9SGleb Smirnoff por->por_rule.anchor = rs->anchor; 930096efeb6SKristof Provost if (TAILQ_EMPTY(&por->por_rule.rdr.list)) 931096efeb6SKristof Provost memset(&por->por_rule.rdr, 0, 932096efeb6SKristof Provost sizeof(por->por_rule.rdr)); 9335cb08fddSKristof Provost if (TAILQ_EMPTY(&por->por_rule.nat.list)) 9345cb08fddSKristof Provost memset(&por->por_rule.nat, 0, 9355cb08fddSKristof Provost sizeof(por->por_rule.nat)); 9363b3a8eb9SGleb Smirnoff TAILQ_INSERT_TAIL(&queue, por, por_entry); 9373b3a8eb9SGleb Smirnoff 938096efeb6SKristof Provost /* XXX pfctl_get_pool(pf->dev, &rule.rdr, nr, pr.ticket, 9393b3a8eb9SGleb Smirnoff * PF_PASS, pf->anchor) ??? 940096efeb6SKristof Provost * ... pfctl_clear_pool(&rule.rdr) 9413b3a8eb9SGleb Smirnoff */ 9423b3a8eb9SGleb Smirnoff } 9433b3a8eb9SGleb Smirnoff 9443b3a8eb9SGleb Smirnoff if (construct_superblocks(pf, &queue, &prof_superblocks)) 9453b3a8eb9SGleb Smirnoff return (1); 9463b3a8eb9SGleb Smirnoff 9473b3a8eb9SGleb Smirnoff 9483b3a8eb9SGleb Smirnoff /* 9493b3a8eb9SGleb Smirnoff * Now we try to associate the active ruleset's superblocks with 9503b3a8eb9SGleb Smirnoff * the superblocks we're compiling. 9513b3a8eb9SGleb Smirnoff */ 9523b3a8eb9SGleb Smirnoff block = TAILQ_FIRST(superblocks); 9533b3a8eb9SGleb Smirnoff blockcur = TAILQ_FIRST(&prof_superblocks); 9543b3a8eb9SGleb Smirnoff while (block && blockcur) { 9553b3a8eb9SGleb Smirnoff comparable_rule(&a, &TAILQ_FIRST(&block->sb_rules)->por_rule, 9563b3a8eb9SGleb Smirnoff BREAK); 9573b3a8eb9SGleb Smirnoff comparable_rule(&b, &TAILQ_FIRST(&blockcur->sb_rules)->por_rule, 9583b3a8eb9SGleb Smirnoff BREAK); 9593b3a8eb9SGleb Smirnoff if (memcmp(&a, &b, sizeof(a)) == 0) { 9603b3a8eb9SGleb Smirnoff /* The two superblocks lined up */ 9613b3a8eb9SGleb Smirnoff block->sb_profiled_block = blockcur; 9623b3a8eb9SGleb Smirnoff } else { 9633b3a8eb9SGleb Smirnoff DEBUG("superblocks don't line up between #%d and #%d", 9643b3a8eb9SGleb Smirnoff TAILQ_FIRST(&block->sb_rules)->por_rule.nr, 9653b3a8eb9SGleb Smirnoff TAILQ_FIRST(&blockcur->sb_rules)->por_rule.nr); 9663b3a8eb9SGleb Smirnoff break; 9673b3a8eb9SGleb Smirnoff } 9683b3a8eb9SGleb Smirnoff block = TAILQ_NEXT(block, sb_entry); 9693b3a8eb9SGleb Smirnoff blockcur = TAILQ_NEXT(blockcur, sb_entry); 9703b3a8eb9SGleb Smirnoff } 9713b3a8eb9SGleb Smirnoff 9723b3a8eb9SGleb Smirnoff 9733b3a8eb9SGleb Smirnoff 9743b3a8eb9SGleb Smirnoff /* Free any superblocks we couldn't link */ 9753b3a8eb9SGleb Smirnoff while (blockcur) { 9763b3a8eb9SGleb Smirnoff block = TAILQ_NEXT(blockcur, sb_entry); 9773b3a8eb9SGleb Smirnoff superblock_free(pf, blockcur); 9783b3a8eb9SGleb Smirnoff blockcur = block; 9793b3a8eb9SGleb Smirnoff } 9803b3a8eb9SGleb Smirnoff return (0); 9813b3a8eb9SGleb Smirnoff } 9823b3a8eb9SGleb Smirnoff 9833b3a8eb9SGleb Smirnoff 9843b3a8eb9SGleb Smirnoff /* 9853b3a8eb9SGleb Smirnoff * Compare a rule to a skiplist to see if the rule is a member 9863b3a8eb9SGleb Smirnoff */ 9873b3a8eb9SGleb Smirnoff int 9883b3a8eb9SGleb Smirnoff skip_compare(int skipnum, struct pf_skip_step *skiplist, 9893b3a8eb9SGleb Smirnoff struct pf_opt_rule *por) 9903b3a8eb9SGleb Smirnoff { 991e9eb0941SKristof Provost struct pfctl_rule *a, *b; 9923b3a8eb9SGleb Smirnoff if (skipnum >= PF_SKIP_COUNT || skipnum < 0) 9933b3a8eb9SGleb Smirnoff errx(1, "skip_compare() out of bounds"); 9943b3a8eb9SGleb Smirnoff a = &por->por_rule; 9953b3a8eb9SGleb Smirnoff b = &TAILQ_FIRST(&skiplist->ps_rules)->por_rule; 9963b3a8eb9SGleb Smirnoff 9973b3a8eb9SGleb Smirnoff return ((skip_comparitors[skipnum])(a, b)); 9983b3a8eb9SGleb Smirnoff } 9993b3a8eb9SGleb Smirnoff 10003b3a8eb9SGleb Smirnoff 10013b3a8eb9SGleb Smirnoff /* 10023b3a8eb9SGleb Smirnoff * Add a rule to a skiplist 10033b3a8eb9SGleb Smirnoff */ 10043b3a8eb9SGleb Smirnoff void 10053b3a8eb9SGleb Smirnoff skip_append(struct superblock *superblock, int skipnum, 10063b3a8eb9SGleb Smirnoff struct pf_skip_step *skiplist, struct pf_opt_rule *por) 10073b3a8eb9SGleb Smirnoff { 10083b3a8eb9SGleb Smirnoff struct pf_skip_step *prev; 10093b3a8eb9SGleb Smirnoff 10103b3a8eb9SGleb Smirnoff skiplist->ps_count++; 10113b3a8eb9SGleb Smirnoff TAILQ_INSERT_TAIL(&skiplist->ps_rules, por, por_skip_entry[skipnum]); 10123b3a8eb9SGleb Smirnoff 10133b3a8eb9SGleb Smirnoff /* Keep the list of skiplists sorted by whichever is larger */ 10143b3a8eb9SGleb Smirnoff while ((prev = TAILQ_PREV(skiplist, skiplist, ps_entry)) && 10153b3a8eb9SGleb Smirnoff prev->ps_count < skiplist->ps_count) { 10163b3a8eb9SGleb Smirnoff TAILQ_REMOVE(&superblock->sb_skipsteps[skipnum], 10173b3a8eb9SGleb Smirnoff skiplist, ps_entry); 10183b3a8eb9SGleb Smirnoff TAILQ_INSERT_BEFORE(prev, skiplist, ps_entry); 10193b3a8eb9SGleb Smirnoff } 10203b3a8eb9SGleb Smirnoff } 10213b3a8eb9SGleb Smirnoff 10223b3a8eb9SGleb Smirnoff 10233b3a8eb9SGleb Smirnoff /* 10243b3a8eb9SGleb Smirnoff * Remove a rule from the other skiplist calculations. 10253b3a8eb9SGleb Smirnoff */ 10263b3a8eb9SGleb Smirnoff void 10273b3a8eb9SGleb Smirnoff remove_from_skipsteps(struct skiplist *head, struct superblock *block, 10283b3a8eb9SGleb Smirnoff struct pf_opt_rule *por, struct pf_skip_step *active_list) 10293b3a8eb9SGleb Smirnoff { 10303b3a8eb9SGleb Smirnoff struct pf_skip_step *sk, *next; 10313b3a8eb9SGleb Smirnoff struct pf_opt_rule *p2; 10323b3a8eb9SGleb Smirnoff int i, found; 10333b3a8eb9SGleb Smirnoff 10343b3a8eb9SGleb Smirnoff for (i = 0; i < PF_SKIP_COUNT; i++) { 10353b3a8eb9SGleb Smirnoff sk = TAILQ_FIRST(&block->sb_skipsteps[i]); 10363b3a8eb9SGleb Smirnoff if (sk == NULL || sk == active_list || sk->ps_count <= 1) 10373b3a8eb9SGleb Smirnoff continue; 10383b3a8eb9SGleb Smirnoff found = 0; 10393b3a8eb9SGleb Smirnoff do { 10403b3a8eb9SGleb Smirnoff TAILQ_FOREACH(p2, &sk->ps_rules, por_skip_entry[i]) 10413b3a8eb9SGleb Smirnoff if (p2 == por) { 10423b3a8eb9SGleb Smirnoff TAILQ_REMOVE(&sk->ps_rules, p2, 10433b3a8eb9SGleb Smirnoff por_skip_entry[i]); 10443b3a8eb9SGleb Smirnoff found = 1; 10453b3a8eb9SGleb Smirnoff sk->ps_count--; 10463b3a8eb9SGleb Smirnoff break; 10473b3a8eb9SGleb Smirnoff } 10483b3a8eb9SGleb Smirnoff } while (!found && (sk = TAILQ_NEXT(sk, ps_entry))); 10493b3a8eb9SGleb Smirnoff if (found && sk) { 10503b3a8eb9SGleb Smirnoff /* Does this change the sorting order? */ 10513b3a8eb9SGleb Smirnoff while ((next = TAILQ_NEXT(sk, ps_entry)) && 10523b3a8eb9SGleb Smirnoff next->ps_count > sk->ps_count) { 10533b3a8eb9SGleb Smirnoff TAILQ_REMOVE(head, sk, ps_entry); 10543b3a8eb9SGleb Smirnoff TAILQ_INSERT_AFTER(head, next, sk, ps_entry); 10553b3a8eb9SGleb Smirnoff } 10563b3a8eb9SGleb Smirnoff #ifdef OPT_DEBUG 10573b3a8eb9SGleb Smirnoff next = TAILQ_NEXT(sk, ps_entry); 10583b3a8eb9SGleb Smirnoff assert(next == NULL || next->ps_count <= sk->ps_count); 10593b3a8eb9SGleb Smirnoff #endif /* OPT_DEBUG */ 10603b3a8eb9SGleb Smirnoff } 10613b3a8eb9SGleb Smirnoff } 10623b3a8eb9SGleb Smirnoff } 10633b3a8eb9SGleb Smirnoff 10643b3a8eb9SGleb Smirnoff 10653b3a8eb9SGleb Smirnoff /* Compare two rules AF field for skiplist construction */ 10663b3a8eb9SGleb Smirnoff int 1067e9eb0941SKristof Provost skip_cmp_af(struct pfctl_rule *a, struct pfctl_rule *b) 10683b3a8eb9SGleb Smirnoff { 10693b3a8eb9SGleb Smirnoff if (a->af != b->af || a->af == 0) 10703b3a8eb9SGleb Smirnoff return (1); 10713b3a8eb9SGleb Smirnoff return (0); 10723b3a8eb9SGleb Smirnoff } 10733b3a8eb9SGleb Smirnoff 10743b3a8eb9SGleb Smirnoff /* Compare two rules DIRECTION field for skiplist construction */ 10753b3a8eb9SGleb Smirnoff int 1076e9eb0941SKristof Provost skip_cmp_dir(struct pfctl_rule *a, struct pfctl_rule *b) 10773b3a8eb9SGleb Smirnoff { 10783b3a8eb9SGleb Smirnoff if (a->direction == 0 || a->direction != b->direction) 10793b3a8eb9SGleb Smirnoff return (1); 10803b3a8eb9SGleb Smirnoff return (0); 10813b3a8eb9SGleb Smirnoff } 10823b3a8eb9SGleb Smirnoff 10833b3a8eb9SGleb Smirnoff /* Compare two rules DST Address field for skiplist construction */ 10843b3a8eb9SGleb Smirnoff int 1085e9eb0941SKristof Provost skip_cmp_dst_addr(struct pfctl_rule *a, struct pfctl_rule *b) 10863b3a8eb9SGleb Smirnoff { 10873b3a8eb9SGleb Smirnoff if (a->dst.neg != b->dst.neg || 10883b3a8eb9SGleb Smirnoff a->dst.addr.type != b->dst.addr.type) 10893b3a8eb9SGleb Smirnoff return (1); 10903b3a8eb9SGleb Smirnoff /* XXX if (a->proto != b->proto && a->proto != 0 && b->proto != 0 10913b3a8eb9SGleb Smirnoff * && (a->proto == IPPROTO_TCP || a->proto == IPPROTO_UDP || 10923b3a8eb9SGleb Smirnoff * a->proto == IPPROTO_ICMP 10933b3a8eb9SGleb Smirnoff * return (1); 10943b3a8eb9SGleb Smirnoff */ 10953b3a8eb9SGleb Smirnoff switch (a->dst.addr.type) { 10963b3a8eb9SGleb Smirnoff case PF_ADDR_ADDRMASK: 10973b3a8eb9SGleb Smirnoff if (memcmp(&a->dst.addr.v.a.addr, &b->dst.addr.v.a.addr, 10983b3a8eb9SGleb Smirnoff sizeof(a->dst.addr.v.a.addr)) || 10993b3a8eb9SGleb Smirnoff memcmp(&a->dst.addr.v.a.mask, &b->dst.addr.v.a.mask, 11003b3a8eb9SGleb Smirnoff sizeof(a->dst.addr.v.a.mask)) || 11013b3a8eb9SGleb Smirnoff (a->dst.addr.v.a.addr.addr32[0] == 0 && 11023b3a8eb9SGleb Smirnoff a->dst.addr.v.a.addr.addr32[1] == 0 && 11033b3a8eb9SGleb Smirnoff a->dst.addr.v.a.addr.addr32[2] == 0 && 11043b3a8eb9SGleb Smirnoff a->dst.addr.v.a.addr.addr32[3] == 0)) 11053b3a8eb9SGleb Smirnoff return (1); 11063b3a8eb9SGleb Smirnoff return (0); 11073b3a8eb9SGleb Smirnoff case PF_ADDR_DYNIFTL: 11083b3a8eb9SGleb Smirnoff if (strcmp(a->dst.addr.v.ifname, b->dst.addr.v.ifname) != 0 || 1109e2d84d5aSPedro F. Giffuni a->dst.addr.iflags != b->dst.addr.iflags || 11103b3a8eb9SGleb Smirnoff memcmp(&a->dst.addr.v.a.mask, &b->dst.addr.v.a.mask, 11113b3a8eb9SGleb Smirnoff sizeof(a->dst.addr.v.a.mask))) 11123b3a8eb9SGleb Smirnoff return (1); 11133b3a8eb9SGleb Smirnoff return (0); 11143b3a8eb9SGleb Smirnoff case PF_ADDR_NOROUTE: 11153b3a8eb9SGleb Smirnoff case PF_ADDR_URPFFAILED: 11163b3a8eb9SGleb Smirnoff return (0); 11173b3a8eb9SGleb Smirnoff case PF_ADDR_TABLE: 11183b3a8eb9SGleb Smirnoff return (strcmp(a->dst.addr.v.tblname, b->dst.addr.v.tblname)); 11193b3a8eb9SGleb Smirnoff } 11203b3a8eb9SGleb Smirnoff return (1); 11213b3a8eb9SGleb Smirnoff } 11223b3a8eb9SGleb Smirnoff 11233b3a8eb9SGleb Smirnoff /* Compare two rules DST port field for skiplist construction */ 11243b3a8eb9SGleb Smirnoff int 1125e9eb0941SKristof Provost skip_cmp_dst_port(struct pfctl_rule *a, struct pfctl_rule *b) 11263b3a8eb9SGleb Smirnoff { 11273b3a8eb9SGleb Smirnoff /* XXX if (a->proto != b->proto && a->proto != 0 && b->proto != 0 11283b3a8eb9SGleb Smirnoff * && (a->proto == IPPROTO_TCP || a->proto == IPPROTO_UDP || 11293b3a8eb9SGleb Smirnoff * a->proto == IPPROTO_ICMP 11303b3a8eb9SGleb Smirnoff * return (1); 11313b3a8eb9SGleb Smirnoff */ 11323b3a8eb9SGleb Smirnoff if (a->dst.port_op == PF_OP_NONE || a->dst.port_op != b->dst.port_op || 11333b3a8eb9SGleb Smirnoff a->dst.port[0] != b->dst.port[0] || 11343b3a8eb9SGleb Smirnoff a->dst.port[1] != b->dst.port[1]) 11353b3a8eb9SGleb Smirnoff return (1); 11363b3a8eb9SGleb Smirnoff return (0); 11373b3a8eb9SGleb Smirnoff } 11383b3a8eb9SGleb Smirnoff 11393b3a8eb9SGleb Smirnoff /* Compare two rules IFP field for skiplist construction */ 11403b3a8eb9SGleb Smirnoff int 1141e9eb0941SKristof Provost skip_cmp_ifp(struct pfctl_rule *a, struct pfctl_rule *b) 11423b3a8eb9SGleb Smirnoff { 11433b3a8eb9SGleb Smirnoff if (strcmp(a->ifname, b->ifname) || a->ifname[0] == '\0') 11443b3a8eb9SGleb Smirnoff return (1); 11453b3a8eb9SGleb Smirnoff return (a->ifnot != b->ifnot); 11463b3a8eb9SGleb Smirnoff } 11473b3a8eb9SGleb Smirnoff 11483b3a8eb9SGleb Smirnoff /* Compare two rules PROTO field for skiplist construction */ 11493b3a8eb9SGleb Smirnoff int 1150e9eb0941SKristof Provost skip_cmp_proto(struct pfctl_rule *a, struct pfctl_rule *b) 11513b3a8eb9SGleb Smirnoff { 11523b3a8eb9SGleb Smirnoff return (a->proto != b->proto || a->proto == 0); 11533b3a8eb9SGleb Smirnoff } 11543b3a8eb9SGleb Smirnoff 11553b3a8eb9SGleb Smirnoff /* Compare two rules SRC addr field for skiplist construction */ 11563b3a8eb9SGleb Smirnoff int 1157e9eb0941SKristof Provost skip_cmp_src_addr(struct pfctl_rule *a, struct pfctl_rule *b) 11583b3a8eb9SGleb Smirnoff { 11593b3a8eb9SGleb Smirnoff if (a->src.neg != b->src.neg || 11603b3a8eb9SGleb Smirnoff a->src.addr.type != b->src.addr.type) 11613b3a8eb9SGleb Smirnoff return (1); 11623b3a8eb9SGleb Smirnoff /* XXX if (a->proto != b->proto && a->proto != 0 && b->proto != 0 11633b3a8eb9SGleb Smirnoff * && (a->proto == IPPROTO_TCP || a->proto == IPPROTO_UDP || 11643b3a8eb9SGleb Smirnoff * a->proto == IPPROTO_ICMP 11653b3a8eb9SGleb Smirnoff * return (1); 11663b3a8eb9SGleb Smirnoff */ 11673b3a8eb9SGleb Smirnoff switch (a->src.addr.type) { 11683b3a8eb9SGleb Smirnoff case PF_ADDR_ADDRMASK: 11693b3a8eb9SGleb Smirnoff if (memcmp(&a->src.addr.v.a.addr, &b->src.addr.v.a.addr, 11703b3a8eb9SGleb Smirnoff sizeof(a->src.addr.v.a.addr)) || 11713b3a8eb9SGleb Smirnoff memcmp(&a->src.addr.v.a.mask, &b->src.addr.v.a.mask, 11723b3a8eb9SGleb Smirnoff sizeof(a->src.addr.v.a.mask)) || 11733b3a8eb9SGleb Smirnoff (a->src.addr.v.a.addr.addr32[0] == 0 && 11743b3a8eb9SGleb Smirnoff a->src.addr.v.a.addr.addr32[1] == 0 && 11753b3a8eb9SGleb Smirnoff a->src.addr.v.a.addr.addr32[2] == 0 && 11763b3a8eb9SGleb Smirnoff a->src.addr.v.a.addr.addr32[3] == 0)) 11773b3a8eb9SGleb Smirnoff return (1); 11783b3a8eb9SGleb Smirnoff return (0); 11793b3a8eb9SGleb Smirnoff case PF_ADDR_DYNIFTL: 11803b3a8eb9SGleb Smirnoff if (strcmp(a->src.addr.v.ifname, b->src.addr.v.ifname) != 0 || 1181e2d84d5aSPedro F. Giffuni a->src.addr.iflags != b->src.addr.iflags || 11823b3a8eb9SGleb Smirnoff memcmp(&a->src.addr.v.a.mask, &b->src.addr.v.a.mask, 11833b3a8eb9SGleb Smirnoff sizeof(a->src.addr.v.a.mask))) 11843b3a8eb9SGleb Smirnoff return (1); 11853b3a8eb9SGleb Smirnoff return (0); 11863b3a8eb9SGleb Smirnoff case PF_ADDR_NOROUTE: 11873b3a8eb9SGleb Smirnoff case PF_ADDR_URPFFAILED: 11883b3a8eb9SGleb Smirnoff return (0); 11893b3a8eb9SGleb Smirnoff case PF_ADDR_TABLE: 11903b3a8eb9SGleb Smirnoff return (strcmp(a->src.addr.v.tblname, b->src.addr.v.tblname)); 11913b3a8eb9SGleb Smirnoff } 11923b3a8eb9SGleb Smirnoff return (1); 11933b3a8eb9SGleb Smirnoff } 11943b3a8eb9SGleb Smirnoff 11953b3a8eb9SGleb Smirnoff /* Compare two rules SRC port field for skiplist construction */ 11963b3a8eb9SGleb Smirnoff int 1197e9eb0941SKristof Provost skip_cmp_src_port(struct pfctl_rule *a, struct pfctl_rule *b) 11983b3a8eb9SGleb Smirnoff { 11993b3a8eb9SGleb Smirnoff if (a->src.port_op == PF_OP_NONE || a->src.port_op != b->src.port_op || 12003b3a8eb9SGleb Smirnoff a->src.port[0] != b->src.port[0] || 12013b3a8eb9SGleb Smirnoff a->src.port[1] != b->src.port[1]) 12023b3a8eb9SGleb Smirnoff return (1); 12033b3a8eb9SGleb Smirnoff /* XXX if (a->proto != b->proto && a->proto != 0 && b->proto != 0 12043b3a8eb9SGleb Smirnoff * && (a->proto == IPPROTO_TCP || a->proto == IPPROTO_UDP || 12053b3a8eb9SGleb Smirnoff * a->proto == IPPROTO_ICMP 12063b3a8eb9SGleb Smirnoff * return (1); 12073b3a8eb9SGleb Smirnoff */ 12083b3a8eb9SGleb Smirnoff return (0); 12093b3a8eb9SGleb Smirnoff } 12103b3a8eb9SGleb Smirnoff 12113b3a8eb9SGleb Smirnoff 12123b3a8eb9SGleb Smirnoff void 12133b3a8eb9SGleb Smirnoff skip_init(void) 12143b3a8eb9SGleb Smirnoff { 12153b3a8eb9SGleb Smirnoff struct { 12163b3a8eb9SGleb Smirnoff char *name; 12173b3a8eb9SGleb Smirnoff int skipnum; 1218e9eb0941SKristof Provost int (*func)(struct pfctl_rule *, struct pfctl_rule *); 12193b3a8eb9SGleb Smirnoff } comps[] = PF_SKIP_COMPARITORS; 12203b3a8eb9SGleb Smirnoff int skipnum, i; 12213b3a8eb9SGleb Smirnoff 12223b3a8eb9SGleb Smirnoff for (skipnum = 0; skipnum < PF_SKIP_COUNT; skipnum++) { 12233b3a8eb9SGleb Smirnoff for (i = 0; i < sizeof(comps)/sizeof(*comps); i++) 12243b3a8eb9SGleb Smirnoff if (comps[i].skipnum == skipnum) { 12253b3a8eb9SGleb Smirnoff skip_comparitors[skipnum] = comps[i].func; 12263b3a8eb9SGleb Smirnoff skip_comparitors_names[skipnum] = comps[i].name; 12273b3a8eb9SGleb Smirnoff } 12283b3a8eb9SGleb Smirnoff } 12293b3a8eb9SGleb Smirnoff for (skipnum = 0; skipnum < PF_SKIP_COUNT; skipnum++) 12303b3a8eb9SGleb Smirnoff if (skip_comparitors[skipnum] == NULL) 12313b3a8eb9SGleb Smirnoff errx(1, "Need to add skip step comparitor to pfctl?!"); 12323b3a8eb9SGleb Smirnoff } 12333b3a8eb9SGleb Smirnoff 12343b3a8eb9SGleb Smirnoff /* 12353b3a8eb9SGleb Smirnoff * Add a host/netmask to a table 12363b3a8eb9SGleb Smirnoff */ 12373b3a8eb9SGleb Smirnoff int 12383b3a8eb9SGleb Smirnoff add_opt_table(struct pfctl *pf, struct pf_opt_tbl **tbl, sa_family_t af, 12393b3a8eb9SGleb Smirnoff struct pf_rule_addr *addr) 12403b3a8eb9SGleb Smirnoff { 12413b3a8eb9SGleb Smirnoff #ifdef OPT_DEBUG 12423b3a8eb9SGleb Smirnoff char buf[128]; 12433b3a8eb9SGleb Smirnoff #endif /* OPT_DEBUG */ 12443b3a8eb9SGleb Smirnoff static int tablenum = 0; 12453b3a8eb9SGleb Smirnoff struct node_host node_host; 12463b3a8eb9SGleb Smirnoff 12473b3a8eb9SGleb Smirnoff if (*tbl == NULL) { 12483b3a8eb9SGleb Smirnoff if ((*tbl = calloc(1, sizeof(**tbl))) == NULL || 12493b3a8eb9SGleb Smirnoff ((*tbl)->pt_buf = calloc(1, sizeof(*(*tbl)->pt_buf))) == 12503b3a8eb9SGleb Smirnoff NULL) 12513b3a8eb9SGleb Smirnoff err(1, "calloc"); 12523b3a8eb9SGleb Smirnoff (*tbl)->pt_buf->pfrb_type = PFRB_ADDRS; 12533b3a8eb9SGleb Smirnoff SIMPLEQ_INIT(&(*tbl)->pt_nodes); 12543b3a8eb9SGleb Smirnoff 12553b3a8eb9SGleb Smirnoff /* This is just a temporary table name */ 12563b3a8eb9SGleb Smirnoff snprintf((*tbl)->pt_name, sizeof((*tbl)->pt_name), "%s%d", 12573b3a8eb9SGleb Smirnoff PF_OPT_TABLE_PREFIX, tablenum++); 12583b3a8eb9SGleb Smirnoff DEBUG("creating table <%s>", (*tbl)->pt_name); 12593b3a8eb9SGleb Smirnoff } 12603b3a8eb9SGleb Smirnoff 12613b3a8eb9SGleb Smirnoff memset(&node_host, 0, sizeof(node_host)); 12623b3a8eb9SGleb Smirnoff node_host.af = af; 12633b3a8eb9SGleb Smirnoff node_host.addr = addr->addr; 12643b3a8eb9SGleb Smirnoff 12653b3a8eb9SGleb Smirnoff #ifdef OPT_DEBUG 12663b3a8eb9SGleb Smirnoff DEBUG("<%s> adding %s/%d", (*tbl)->pt_name, inet_ntop(af, 12673b3a8eb9SGleb Smirnoff &node_host.addr.v.a.addr, buf, sizeof(buf)), 12683b3a8eb9SGleb Smirnoff unmask(&node_host.addr.v.a.mask, af)); 12693b3a8eb9SGleb Smirnoff #endif /* OPT_DEBUG */ 12703b3a8eb9SGleb Smirnoff 12713b3a8eb9SGleb Smirnoff if (append_addr_host((*tbl)->pt_buf, &node_host, 0, 0)) { 12723b3a8eb9SGleb Smirnoff warn("failed to add host"); 12733b3a8eb9SGleb Smirnoff return (1); 12743b3a8eb9SGleb Smirnoff } 12753b3a8eb9SGleb Smirnoff if (pf->opts & PF_OPT_VERBOSE) { 12763b3a8eb9SGleb Smirnoff struct node_tinit *ti; 12773b3a8eb9SGleb Smirnoff 12783b3a8eb9SGleb Smirnoff if ((ti = calloc(1, sizeof(*ti))) == NULL) 12793b3a8eb9SGleb Smirnoff err(1, "malloc"); 12803b3a8eb9SGleb Smirnoff if ((ti->host = malloc(sizeof(*ti->host))) == NULL) 12813b3a8eb9SGleb Smirnoff err(1, "malloc"); 12823b3a8eb9SGleb Smirnoff memcpy(ti->host, &node_host, sizeof(*ti->host)); 12833b3a8eb9SGleb Smirnoff SIMPLEQ_INSERT_TAIL(&(*tbl)->pt_nodes, ti, entries); 12843b3a8eb9SGleb Smirnoff } 12853b3a8eb9SGleb Smirnoff 12863b3a8eb9SGleb Smirnoff (*tbl)->pt_rulecount++; 12873b3a8eb9SGleb Smirnoff if ((*tbl)->pt_rulecount == TABLE_THRESHOLD) 12883b3a8eb9SGleb Smirnoff DEBUG("table <%s> now faster than skip steps", (*tbl)->pt_name); 12893b3a8eb9SGleb Smirnoff 12903b3a8eb9SGleb Smirnoff return (0); 12913b3a8eb9SGleb Smirnoff } 12923b3a8eb9SGleb Smirnoff 12933b3a8eb9SGleb Smirnoff 12943b3a8eb9SGleb Smirnoff /* 12953b3a8eb9SGleb Smirnoff * Do the dirty work of choosing an unused table name and creating it. 12963b3a8eb9SGleb Smirnoff * (be careful with the table name, it might already be used in another anchor) 12973b3a8eb9SGleb Smirnoff */ 12983b3a8eb9SGleb Smirnoff int 12993b3a8eb9SGleb Smirnoff pf_opt_create_table(struct pfctl *pf, struct pf_opt_tbl *tbl) 13003b3a8eb9SGleb Smirnoff { 13013b3a8eb9SGleb Smirnoff static int tablenum; 13023b3a8eb9SGleb Smirnoff struct pfr_table *t; 13033b3a8eb9SGleb Smirnoff 13043b3a8eb9SGleb Smirnoff if (table_buffer.pfrb_type == 0) { 13053b3a8eb9SGleb Smirnoff /* Initialize the list of tables */ 13063b3a8eb9SGleb Smirnoff table_buffer.pfrb_type = PFRB_TABLES; 13073b3a8eb9SGleb Smirnoff for (;;) { 13083b3a8eb9SGleb Smirnoff pfr_buf_grow(&table_buffer, table_buffer.pfrb_size); 13093b3a8eb9SGleb Smirnoff table_buffer.pfrb_size = table_buffer.pfrb_msize; 13103b3a8eb9SGleb Smirnoff if (pfr_get_tables(NULL, table_buffer.pfrb_caddr, 13113b3a8eb9SGleb Smirnoff &table_buffer.pfrb_size, PFR_FLAG_ALLRSETS)) 13123b3a8eb9SGleb Smirnoff err(1, "pfr_get_tables"); 13133b3a8eb9SGleb Smirnoff if (table_buffer.pfrb_size <= table_buffer.pfrb_msize) 13143b3a8eb9SGleb Smirnoff break; 13153b3a8eb9SGleb Smirnoff } 13163b3a8eb9SGleb Smirnoff table_identifier = arc4random(); 13173b3a8eb9SGleb Smirnoff } 13183b3a8eb9SGleb Smirnoff 13193b3a8eb9SGleb Smirnoff /* XXX would be *really* nice to avoid duplicating identical tables */ 13203b3a8eb9SGleb Smirnoff 13213b3a8eb9SGleb Smirnoff /* Now we have to pick a table name that isn't used */ 13223b3a8eb9SGleb Smirnoff again: 13233b3a8eb9SGleb Smirnoff DEBUG("translating temporary table <%s> to <%s%x_%d>", tbl->pt_name, 13243b3a8eb9SGleb Smirnoff PF_OPT_TABLE_PREFIX, table_identifier, tablenum); 13253b3a8eb9SGleb Smirnoff snprintf(tbl->pt_name, sizeof(tbl->pt_name), "%s%x_%d", 13263b3a8eb9SGleb Smirnoff PF_OPT_TABLE_PREFIX, table_identifier, tablenum); 13273b3a8eb9SGleb Smirnoff PFRB_FOREACH(t, &table_buffer) { 13283b3a8eb9SGleb Smirnoff if (strcasecmp(t->pfrt_name, tbl->pt_name) == 0) { 13293b3a8eb9SGleb Smirnoff /* Collision. Try again */ 13303b3a8eb9SGleb Smirnoff DEBUG("wow, table <%s> in use. trying again", 13313b3a8eb9SGleb Smirnoff tbl->pt_name); 13323b3a8eb9SGleb Smirnoff table_identifier = arc4random(); 13333b3a8eb9SGleb Smirnoff goto again; 13343b3a8eb9SGleb Smirnoff } 13353b3a8eb9SGleb Smirnoff } 13363b3a8eb9SGleb Smirnoff tablenum++; 13373b3a8eb9SGleb Smirnoff 13383b3a8eb9SGleb Smirnoff 13393b3a8eb9SGleb Smirnoff if (pfctl_define_table(tbl->pt_name, PFR_TFLAG_CONST, 1, 13403b3a8eb9SGleb Smirnoff pf->astack[0]->name, tbl->pt_buf, pf->astack[0]->ruleset.tticket)) { 13413b3a8eb9SGleb Smirnoff warn("failed to create table %s in %s", 13423b3a8eb9SGleb Smirnoff tbl->pt_name, pf->astack[0]->name); 13433b3a8eb9SGleb Smirnoff return (1); 13443b3a8eb9SGleb Smirnoff } 13453b3a8eb9SGleb Smirnoff return (0); 13463b3a8eb9SGleb Smirnoff } 13473b3a8eb9SGleb Smirnoff 13483b3a8eb9SGleb Smirnoff /* 13493b3a8eb9SGleb Smirnoff * Partition the flat ruleset into a list of distinct superblocks 13503b3a8eb9SGleb Smirnoff */ 13513b3a8eb9SGleb Smirnoff int 13523b3a8eb9SGleb Smirnoff construct_superblocks(struct pfctl *pf, struct pf_opt_queue *opt_queue, 13533b3a8eb9SGleb Smirnoff struct superblocks *superblocks) 13543b3a8eb9SGleb Smirnoff { 13553b3a8eb9SGleb Smirnoff struct superblock *block = NULL; 13563b3a8eb9SGleb Smirnoff struct pf_opt_rule *por; 13573b3a8eb9SGleb Smirnoff int i; 13583b3a8eb9SGleb Smirnoff 13593b3a8eb9SGleb Smirnoff while (!TAILQ_EMPTY(opt_queue)) { 13603b3a8eb9SGleb Smirnoff por = TAILQ_FIRST(opt_queue); 13613b3a8eb9SGleb Smirnoff TAILQ_REMOVE(opt_queue, por, por_entry); 13623b3a8eb9SGleb Smirnoff if (block == NULL || !superblock_inclusive(block, por)) { 13633b3a8eb9SGleb Smirnoff if ((block = calloc(1, sizeof(*block))) == NULL) { 13643b3a8eb9SGleb Smirnoff warn("calloc"); 13653b3a8eb9SGleb Smirnoff return (1); 13663b3a8eb9SGleb Smirnoff } 13673b3a8eb9SGleb Smirnoff TAILQ_INIT(&block->sb_rules); 13683b3a8eb9SGleb Smirnoff for (i = 0; i < PF_SKIP_COUNT; i++) 13693b3a8eb9SGleb Smirnoff TAILQ_INIT(&block->sb_skipsteps[i]); 13703b3a8eb9SGleb Smirnoff TAILQ_INSERT_TAIL(superblocks, block, sb_entry); 13713b3a8eb9SGleb Smirnoff } 13723b3a8eb9SGleb Smirnoff TAILQ_INSERT_TAIL(&block->sb_rules, por, por_entry); 13733b3a8eb9SGleb Smirnoff } 13743b3a8eb9SGleb Smirnoff 13753b3a8eb9SGleb Smirnoff return (0); 13763b3a8eb9SGleb Smirnoff } 13773b3a8eb9SGleb Smirnoff 13783b3a8eb9SGleb Smirnoff 13793b3a8eb9SGleb Smirnoff /* 13803b3a8eb9SGleb Smirnoff * Compare two rule addresses 13813b3a8eb9SGleb Smirnoff */ 13823b3a8eb9SGleb Smirnoff int 13833b3a8eb9SGleb Smirnoff addrs_equal(struct pf_rule_addr *a, struct pf_rule_addr *b) 13843b3a8eb9SGleb Smirnoff { 13853b3a8eb9SGleb Smirnoff if (a->neg != b->neg) 13863b3a8eb9SGleb Smirnoff return (0); 13873b3a8eb9SGleb Smirnoff return (memcmp(&a->addr, &b->addr, sizeof(a->addr)) == 0); 13883b3a8eb9SGleb Smirnoff } 13893b3a8eb9SGleb Smirnoff 13903b3a8eb9SGleb Smirnoff 13913b3a8eb9SGleb Smirnoff /* 13923b3a8eb9SGleb Smirnoff * The addresses are not equal, but can we combine them into one table? 13933b3a8eb9SGleb Smirnoff */ 13943b3a8eb9SGleb Smirnoff int 13953b3a8eb9SGleb Smirnoff addrs_combineable(struct pf_rule_addr *a, struct pf_rule_addr *b) 13963b3a8eb9SGleb Smirnoff { 13973b3a8eb9SGleb Smirnoff if (a->addr.type != PF_ADDR_ADDRMASK || 13983b3a8eb9SGleb Smirnoff b->addr.type != PF_ADDR_ADDRMASK) 13993b3a8eb9SGleb Smirnoff return (0); 14003b3a8eb9SGleb Smirnoff if (a->neg != b->neg || a->port_op != b->port_op || 14013b3a8eb9SGleb Smirnoff a->port[0] != b->port[0] || a->port[1] != b->port[1]) 14023b3a8eb9SGleb Smirnoff return (0); 14033b3a8eb9SGleb Smirnoff return (1); 14043b3a8eb9SGleb Smirnoff } 14053b3a8eb9SGleb Smirnoff 14063b3a8eb9SGleb Smirnoff 14073b3a8eb9SGleb Smirnoff /* 14083b3a8eb9SGleb Smirnoff * Are we allowed to combine these two rules 14093b3a8eb9SGleb Smirnoff */ 14103b3a8eb9SGleb Smirnoff int 1411e9eb0941SKristof Provost rules_combineable(struct pfctl_rule *p1, struct pfctl_rule *p2) 14123b3a8eb9SGleb Smirnoff { 1413e9eb0941SKristof Provost struct pfctl_rule a, b; 14143b3a8eb9SGleb Smirnoff 14153b3a8eb9SGleb Smirnoff comparable_rule(&a, p1, COMBINED); 14163b3a8eb9SGleb Smirnoff comparable_rule(&b, p2, COMBINED); 14173b3a8eb9SGleb Smirnoff return (memcmp(&a, &b, sizeof(a)) == 0); 14183b3a8eb9SGleb Smirnoff } 14193b3a8eb9SGleb Smirnoff 14203b3a8eb9SGleb Smirnoff 14213b3a8eb9SGleb Smirnoff /* 14223b3a8eb9SGleb Smirnoff * Can a rule be included inside a superblock 14233b3a8eb9SGleb Smirnoff */ 14243b3a8eb9SGleb Smirnoff int 14253b3a8eb9SGleb Smirnoff superblock_inclusive(struct superblock *block, struct pf_opt_rule *por) 14263b3a8eb9SGleb Smirnoff { 1427e9eb0941SKristof Provost struct pfctl_rule a, b; 14283b3a8eb9SGleb Smirnoff int i, j; 14293b3a8eb9SGleb Smirnoff 14303b3a8eb9SGleb Smirnoff /* First check for hard breaks */ 14313b3a8eb9SGleb Smirnoff for (i = 0; i < sizeof(pf_rule_desc)/sizeof(*pf_rule_desc); i++) { 14323b3a8eb9SGleb Smirnoff if (pf_rule_desc[i].prf_type == BARRIER) { 14333b3a8eb9SGleb Smirnoff for (j = 0; j < pf_rule_desc[i].prf_size; j++) 14343b3a8eb9SGleb Smirnoff if (((char *)&por->por_rule)[j + 14353b3a8eb9SGleb Smirnoff pf_rule_desc[i].prf_offset] != 0) 14363b3a8eb9SGleb Smirnoff return (0); 14373b3a8eb9SGleb Smirnoff } 14383b3a8eb9SGleb Smirnoff } 14393b3a8eb9SGleb Smirnoff 14403b3a8eb9SGleb Smirnoff /* per-rule src-track is also a hard break */ 14413b3a8eb9SGleb Smirnoff if (por->por_rule.rule_flag & PFRULE_RULESRCTRACK) 14423b3a8eb9SGleb Smirnoff return (0); 14433b3a8eb9SGleb Smirnoff 14443b3a8eb9SGleb Smirnoff /* 14453b3a8eb9SGleb Smirnoff * Have to handle interface groups separately. Consider the following 14463b3a8eb9SGleb Smirnoff * rules: 14473b3a8eb9SGleb Smirnoff * block on EXTIFS to any port 22 14483b3a8eb9SGleb Smirnoff * pass on em0 to any port 22 14493b3a8eb9SGleb Smirnoff * (where EXTIFS is an arbitrary interface group) 14503b3a8eb9SGleb Smirnoff * The optimizer may decide to re-order the pass rule in front of the 14513b3a8eb9SGleb Smirnoff * block rule. But what if EXTIFS includes em0??? Such a reordering 14523b3a8eb9SGleb Smirnoff * would change the meaning of the ruleset. 14533b3a8eb9SGleb Smirnoff * We can't just lookup the EXTIFS group and check if em0 is a member 14543b3a8eb9SGleb Smirnoff * because the user is allowed to add interfaces to a group during 14553b3a8eb9SGleb Smirnoff * runtime. 14563b3a8eb9SGleb Smirnoff * Ergo interface groups become a defacto superblock break :-( 14573b3a8eb9SGleb Smirnoff */ 14583b3a8eb9SGleb Smirnoff if (interface_group(por->por_rule.ifname) || 14593b3a8eb9SGleb Smirnoff interface_group(TAILQ_FIRST(&block->sb_rules)->por_rule.ifname)) { 14603b3a8eb9SGleb Smirnoff if (strcasecmp(por->por_rule.ifname, 14613b3a8eb9SGleb Smirnoff TAILQ_FIRST(&block->sb_rules)->por_rule.ifname) != 0) 14623b3a8eb9SGleb Smirnoff return (0); 14633b3a8eb9SGleb Smirnoff } 14643b3a8eb9SGleb Smirnoff 14653b3a8eb9SGleb Smirnoff comparable_rule(&a, &TAILQ_FIRST(&block->sb_rules)->por_rule, NOMERGE); 14663b3a8eb9SGleb Smirnoff comparable_rule(&b, &por->por_rule, NOMERGE); 14673b3a8eb9SGleb Smirnoff if (memcmp(&a, &b, sizeof(a)) == 0) 14683b3a8eb9SGleb Smirnoff return (1); 14693b3a8eb9SGleb Smirnoff 14703b3a8eb9SGleb Smirnoff #ifdef OPT_DEBUG 14713b3a8eb9SGleb Smirnoff for (i = 0; i < sizeof(por->por_rule); i++) { 14723b3a8eb9SGleb Smirnoff int closest = -1; 14733b3a8eb9SGleb Smirnoff if (((u_int8_t *)&a)[i] != ((u_int8_t *)&b)[i]) { 14743b3a8eb9SGleb Smirnoff for (j = 0; j < sizeof(pf_rule_desc) / 14753b3a8eb9SGleb Smirnoff sizeof(*pf_rule_desc); j++) { 14763b3a8eb9SGleb Smirnoff if (i >= pf_rule_desc[j].prf_offset && 14773b3a8eb9SGleb Smirnoff i < pf_rule_desc[j].prf_offset + 14783b3a8eb9SGleb Smirnoff pf_rule_desc[j].prf_size) { 14793b3a8eb9SGleb Smirnoff DEBUG("superblock break @ %d due to %s", 14803b3a8eb9SGleb Smirnoff por->por_rule.nr, 14813b3a8eb9SGleb Smirnoff pf_rule_desc[j].prf_name); 14823b3a8eb9SGleb Smirnoff return (0); 14833b3a8eb9SGleb Smirnoff } 14843b3a8eb9SGleb Smirnoff if (i > pf_rule_desc[j].prf_offset) { 14853b3a8eb9SGleb Smirnoff if (closest == -1 || 14863b3a8eb9SGleb Smirnoff i-pf_rule_desc[j].prf_offset < 14873b3a8eb9SGleb Smirnoff i-pf_rule_desc[closest].prf_offset) 14883b3a8eb9SGleb Smirnoff closest = j; 14893b3a8eb9SGleb Smirnoff } 14903b3a8eb9SGleb Smirnoff } 14913b3a8eb9SGleb Smirnoff 14923b3a8eb9SGleb Smirnoff if (closest >= 0) 14939f1beeaeSKajetan Staszkiewicz DEBUG("superblock break @ %d on %s+%zxh", 14943b3a8eb9SGleb Smirnoff por->por_rule.nr, 14953b3a8eb9SGleb Smirnoff pf_rule_desc[closest].prf_name, 14963b3a8eb9SGleb Smirnoff i - pf_rule_desc[closest].prf_offset - 14973b3a8eb9SGleb Smirnoff pf_rule_desc[closest].prf_size); 14983b3a8eb9SGleb Smirnoff else 14993b3a8eb9SGleb Smirnoff DEBUG("superblock break @ %d on field @ %d", 15003b3a8eb9SGleb Smirnoff por->por_rule.nr, i); 15013b3a8eb9SGleb Smirnoff return (0); 15023b3a8eb9SGleb Smirnoff } 15033b3a8eb9SGleb Smirnoff } 15043b3a8eb9SGleb Smirnoff #endif /* OPT_DEBUG */ 15053b3a8eb9SGleb Smirnoff 15063b3a8eb9SGleb Smirnoff return (0); 15073b3a8eb9SGleb Smirnoff } 15083b3a8eb9SGleb Smirnoff 15093b3a8eb9SGleb Smirnoff 15103b3a8eb9SGleb Smirnoff /* 15113b3a8eb9SGleb Smirnoff * Figure out if an interface name is an actual interface or actually a 15123b3a8eb9SGleb Smirnoff * group of interfaces. 15133b3a8eb9SGleb Smirnoff */ 15143b3a8eb9SGleb Smirnoff int 15153b3a8eb9SGleb Smirnoff interface_group(const char *ifname) 15163b3a8eb9SGleb Smirnoff { 15177296d6c9SKristof Provost int s; 15187296d6c9SKristof Provost struct ifgroupreq ifgr; 15197296d6c9SKristof Provost 15203b3a8eb9SGleb Smirnoff if (ifname == NULL || !ifname[0]) 15213b3a8eb9SGleb Smirnoff return (0); 15223b3a8eb9SGleb Smirnoff 15237296d6c9SKristof Provost s = get_query_socket(); 15247296d6c9SKristof Provost 15257296d6c9SKristof Provost memset(&ifgr, 0, sizeof(ifgr)); 15267296d6c9SKristof Provost strlcpy(ifgr.ifgr_name, ifname, IFNAMSIZ); 15277296d6c9SKristof Provost if (ioctl(s, SIOCGIFGMEMB, (caddr_t)&ifgr) == -1) { 15287296d6c9SKristof Provost if (errno == ENOENT) 15293b3a8eb9SGleb Smirnoff return (0); 15303b3a8eb9SGleb Smirnoff else 15317296d6c9SKristof Provost err(1, "SIOCGIFGMEMB"); 15327296d6c9SKristof Provost } 15337296d6c9SKristof Provost 15343b3a8eb9SGleb Smirnoff return (1); 15353b3a8eb9SGleb Smirnoff } 15363b3a8eb9SGleb Smirnoff 15373b3a8eb9SGleb Smirnoff 15383b3a8eb9SGleb Smirnoff /* 15393b3a8eb9SGleb Smirnoff * Make a rule that can directly compared by memcmp() 15403b3a8eb9SGleb Smirnoff */ 15413b3a8eb9SGleb Smirnoff void 1542e9eb0941SKristof Provost comparable_rule(struct pfctl_rule *dst, const struct pfctl_rule *src, int type) 15433b3a8eb9SGleb Smirnoff { 15443b3a8eb9SGleb Smirnoff int i; 15453b3a8eb9SGleb Smirnoff /* 15463b3a8eb9SGleb Smirnoff * To simplify the comparison, we just zero out the fields that are 15473b3a8eb9SGleb Smirnoff * allowed to be different and then do a simple memcmp() 15483b3a8eb9SGleb Smirnoff */ 15493b3a8eb9SGleb Smirnoff memcpy(dst, src, sizeof(*dst)); 15503b3a8eb9SGleb Smirnoff for (i = 0; i < sizeof(pf_rule_desc)/sizeof(*pf_rule_desc); i++) 15513b3a8eb9SGleb Smirnoff if (pf_rule_desc[i].prf_type >= type) { 15523b3a8eb9SGleb Smirnoff #ifdef OPT_DEBUG 15533b3a8eb9SGleb Smirnoff assert(pf_rule_desc[i].prf_type != NEVER || 15543b3a8eb9SGleb Smirnoff *(((char *)dst) + pf_rule_desc[i].prf_offset) == 0); 15553b3a8eb9SGleb Smirnoff #endif /* OPT_DEBUG */ 15563b3a8eb9SGleb Smirnoff memset(((char *)dst) + pf_rule_desc[i].prf_offset, 0, 15573b3a8eb9SGleb Smirnoff pf_rule_desc[i].prf_size); 15583b3a8eb9SGleb Smirnoff } 15593b3a8eb9SGleb Smirnoff } 15603b3a8eb9SGleb Smirnoff 15613b3a8eb9SGleb Smirnoff 15623b3a8eb9SGleb Smirnoff /* 15633b3a8eb9SGleb Smirnoff * Remove superset information from two rules so we can directly compare them 15643b3a8eb9SGleb Smirnoff * with memcmp() 15653b3a8eb9SGleb Smirnoff */ 15663b3a8eb9SGleb Smirnoff void 1567e9eb0941SKristof Provost exclude_supersets(struct pfctl_rule *super, struct pfctl_rule *sub) 15683b3a8eb9SGleb Smirnoff { 15693b3a8eb9SGleb Smirnoff if (super->ifname[0] == '\0') 15703b3a8eb9SGleb Smirnoff memset(sub->ifname, 0, sizeof(sub->ifname)); 15713b3a8eb9SGleb Smirnoff if (super->direction == PF_INOUT) 15723b3a8eb9SGleb Smirnoff sub->direction = PF_INOUT; 15733b3a8eb9SGleb Smirnoff if ((super->proto == 0 || super->proto == sub->proto) && 15743b3a8eb9SGleb Smirnoff super->flags == 0 && super->flagset == 0 && (sub->flags || 15753b3a8eb9SGleb Smirnoff sub->flagset)) { 15763b3a8eb9SGleb Smirnoff sub->flags = super->flags; 15773b3a8eb9SGleb Smirnoff sub->flagset = super->flagset; 15783b3a8eb9SGleb Smirnoff } 15793b3a8eb9SGleb Smirnoff if (super->proto == 0) 15803b3a8eb9SGleb Smirnoff sub->proto = 0; 15813b3a8eb9SGleb Smirnoff 15823b3a8eb9SGleb Smirnoff if (super->src.port_op == 0) { 15833b3a8eb9SGleb Smirnoff sub->src.port_op = 0; 15843b3a8eb9SGleb Smirnoff sub->src.port[0] = 0; 15853b3a8eb9SGleb Smirnoff sub->src.port[1] = 0; 15863b3a8eb9SGleb Smirnoff } 15873b3a8eb9SGleb Smirnoff if (super->dst.port_op == 0) { 15883b3a8eb9SGleb Smirnoff sub->dst.port_op = 0; 15893b3a8eb9SGleb Smirnoff sub->dst.port[0] = 0; 15903b3a8eb9SGleb Smirnoff sub->dst.port[1] = 0; 15913b3a8eb9SGleb Smirnoff } 15923b3a8eb9SGleb Smirnoff 15933b3a8eb9SGleb Smirnoff if (super->src.addr.type == PF_ADDR_ADDRMASK && !super->src.neg && 15943b3a8eb9SGleb Smirnoff !sub->src.neg && super->src.addr.v.a.mask.addr32[0] == 0 && 15953b3a8eb9SGleb Smirnoff super->src.addr.v.a.mask.addr32[1] == 0 && 15963b3a8eb9SGleb Smirnoff super->src.addr.v.a.mask.addr32[2] == 0 && 15973b3a8eb9SGleb Smirnoff super->src.addr.v.a.mask.addr32[3] == 0) 15983b3a8eb9SGleb Smirnoff memset(&sub->src.addr, 0, sizeof(sub->src.addr)); 15993b3a8eb9SGleb Smirnoff else if (super->src.addr.type == PF_ADDR_ADDRMASK && 16003b3a8eb9SGleb Smirnoff sub->src.addr.type == PF_ADDR_ADDRMASK && 16013b3a8eb9SGleb Smirnoff super->src.neg == sub->src.neg && 16023b3a8eb9SGleb Smirnoff super->af == sub->af && 16033b3a8eb9SGleb Smirnoff unmask(&super->src.addr.v.a.mask, super->af) < 16043b3a8eb9SGleb Smirnoff unmask(&sub->src.addr.v.a.mask, sub->af) && 16053b3a8eb9SGleb Smirnoff super->src.addr.v.a.addr.addr32[0] == 16063b3a8eb9SGleb Smirnoff (sub->src.addr.v.a.addr.addr32[0] & 16073b3a8eb9SGleb Smirnoff super->src.addr.v.a.mask.addr32[0]) && 16083b3a8eb9SGleb Smirnoff super->src.addr.v.a.addr.addr32[1] == 16093b3a8eb9SGleb Smirnoff (sub->src.addr.v.a.addr.addr32[1] & 16103b3a8eb9SGleb Smirnoff super->src.addr.v.a.mask.addr32[1]) && 16113b3a8eb9SGleb Smirnoff super->src.addr.v.a.addr.addr32[2] == 16123b3a8eb9SGleb Smirnoff (sub->src.addr.v.a.addr.addr32[2] & 16133b3a8eb9SGleb Smirnoff super->src.addr.v.a.mask.addr32[2]) && 16143b3a8eb9SGleb Smirnoff super->src.addr.v.a.addr.addr32[3] == 16153b3a8eb9SGleb Smirnoff (sub->src.addr.v.a.addr.addr32[3] & 16163b3a8eb9SGleb Smirnoff super->src.addr.v.a.mask.addr32[3])) { 16173b3a8eb9SGleb Smirnoff /* sub->src.addr is a subset of super->src.addr/mask */ 16183b3a8eb9SGleb Smirnoff memcpy(&sub->src.addr, &super->src.addr, sizeof(sub->src.addr)); 16193b3a8eb9SGleb Smirnoff } 16203b3a8eb9SGleb Smirnoff 16213b3a8eb9SGleb Smirnoff if (super->dst.addr.type == PF_ADDR_ADDRMASK && !super->dst.neg && 16223b3a8eb9SGleb Smirnoff !sub->dst.neg && super->dst.addr.v.a.mask.addr32[0] == 0 && 16233b3a8eb9SGleb Smirnoff super->dst.addr.v.a.mask.addr32[1] == 0 && 16243b3a8eb9SGleb Smirnoff super->dst.addr.v.a.mask.addr32[2] == 0 && 16253b3a8eb9SGleb Smirnoff super->dst.addr.v.a.mask.addr32[3] == 0) 16263b3a8eb9SGleb Smirnoff memset(&sub->dst.addr, 0, sizeof(sub->dst.addr)); 16273b3a8eb9SGleb Smirnoff else if (super->dst.addr.type == PF_ADDR_ADDRMASK && 16283b3a8eb9SGleb Smirnoff sub->dst.addr.type == PF_ADDR_ADDRMASK && 16293b3a8eb9SGleb Smirnoff super->dst.neg == sub->dst.neg && 16303b3a8eb9SGleb Smirnoff super->af == sub->af && 16313b3a8eb9SGleb Smirnoff unmask(&super->dst.addr.v.a.mask, super->af) < 16323b3a8eb9SGleb Smirnoff unmask(&sub->dst.addr.v.a.mask, sub->af) && 16333b3a8eb9SGleb Smirnoff super->dst.addr.v.a.addr.addr32[0] == 16343b3a8eb9SGleb Smirnoff (sub->dst.addr.v.a.addr.addr32[0] & 16353b3a8eb9SGleb Smirnoff super->dst.addr.v.a.mask.addr32[0]) && 16363b3a8eb9SGleb Smirnoff super->dst.addr.v.a.addr.addr32[1] == 16373b3a8eb9SGleb Smirnoff (sub->dst.addr.v.a.addr.addr32[1] & 16383b3a8eb9SGleb Smirnoff super->dst.addr.v.a.mask.addr32[1]) && 16393b3a8eb9SGleb Smirnoff super->dst.addr.v.a.addr.addr32[2] == 16403b3a8eb9SGleb Smirnoff (sub->dst.addr.v.a.addr.addr32[2] & 16413b3a8eb9SGleb Smirnoff super->dst.addr.v.a.mask.addr32[2]) && 16423b3a8eb9SGleb Smirnoff super->dst.addr.v.a.addr.addr32[3] == 16433b3a8eb9SGleb Smirnoff (sub->dst.addr.v.a.addr.addr32[3] & 16443b3a8eb9SGleb Smirnoff super->dst.addr.v.a.mask.addr32[3])) { 16453b3a8eb9SGleb Smirnoff /* sub->dst.addr is a subset of super->dst.addr/mask */ 16463b3a8eb9SGleb Smirnoff memcpy(&sub->dst.addr, &super->dst.addr, sizeof(sub->dst.addr)); 16473b3a8eb9SGleb Smirnoff } 16483b3a8eb9SGleb Smirnoff 16493b3a8eb9SGleb Smirnoff if (super->af == 0) 16503b3a8eb9SGleb Smirnoff sub->af = 0; 16513b3a8eb9SGleb Smirnoff } 16523b3a8eb9SGleb Smirnoff 16533b3a8eb9SGleb Smirnoff 16543b3a8eb9SGleb Smirnoff void 16553b3a8eb9SGleb Smirnoff superblock_free(struct pfctl *pf, struct superblock *block) 16563b3a8eb9SGleb Smirnoff { 16573b3a8eb9SGleb Smirnoff struct pf_opt_rule *por; 16583b3a8eb9SGleb Smirnoff while ((por = TAILQ_FIRST(&block->sb_rules))) { 16593b3a8eb9SGleb Smirnoff TAILQ_REMOVE(&block->sb_rules, por, por_entry); 16603b3a8eb9SGleb Smirnoff if (por->por_src_tbl) { 16613b3a8eb9SGleb Smirnoff if (por->por_src_tbl->pt_buf) { 16623b3a8eb9SGleb Smirnoff pfr_buf_clear(por->por_src_tbl->pt_buf); 16633b3a8eb9SGleb Smirnoff free(por->por_src_tbl->pt_buf); 16643b3a8eb9SGleb Smirnoff } 16653b3a8eb9SGleb Smirnoff free(por->por_src_tbl); 16663b3a8eb9SGleb Smirnoff } 16673b3a8eb9SGleb Smirnoff if (por->por_dst_tbl) { 16683b3a8eb9SGleb Smirnoff if (por->por_dst_tbl->pt_buf) { 16693b3a8eb9SGleb Smirnoff pfr_buf_clear(por->por_dst_tbl->pt_buf); 16703b3a8eb9SGleb Smirnoff free(por->por_dst_tbl->pt_buf); 16713b3a8eb9SGleb Smirnoff } 16723b3a8eb9SGleb Smirnoff free(por->por_dst_tbl); 16733b3a8eb9SGleb Smirnoff } 16743b3a8eb9SGleb Smirnoff free(por); 16753b3a8eb9SGleb Smirnoff } 16763b3a8eb9SGleb Smirnoff if (block->sb_profiled_block) 16773b3a8eb9SGleb Smirnoff superblock_free(pf, block->sb_profiled_block); 16783b3a8eb9SGleb Smirnoff free(block); 16793b3a8eb9SGleb Smirnoff } 16803b3a8eb9SGleb Smirnoff 1681