1/* $OpenBSD: pfctl_optimize.c,v 1.5 2005/01/03 15:18:10 frantzen Exp $ */
| 1/* $OpenBSD: pfctl_optimize.c,v 1.13 2006/10/31 14:17:45 mcbride Exp $ */
|
2 3/* 4 * Copyright (c) 2004 Mike Frantzen <frantzen@openbsd.org> 5 * 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. 9 * 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. 17 */ 18 19#include <sys/cdefs.h>
| 2 3/* 4 * Copyright (c) 2004 Mike Frantzen <frantzen@openbsd.org> 5 * 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. 9 * 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. 17 */ 18 19#include <sys/cdefs.h>
|
20__FBSDID("$FreeBSD: head/contrib/pf/pfctl/pfctl_optimize.c 145840 2005-05-03 16:55:20Z mlaier $");
| 20__FBSDID("$FreeBSD: head/contrib/pf/pfctl/pfctl_optimize.c 171172 2007-07-03 12:30:03Z mlaier $");
|
21 22#include <sys/types.h> 23#include <sys/ioctl.h> 24#include <sys/socket.h> 25 26#include <net/if.h> 27#include <net/pfvar.h> 28 29#include <netinet/in.h> 30#include <arpa/inet.h> 31 32#include <assert.h> 33#include <ctype.h> 34#include <err.h> 35#include <errno.h> 36#include <stddef.h> 37#include <stdio.h> 38#include <stdlib.h> 39#include <string.h> 40 41#include "pfctl_parser.h" 42#include "pfctl.h" 43 44/* The size at which a table becomes faster than individual rules */ 45#define TABLE_THRESHOLD 6 46 47 48/* #define OPT_DEBUG 1 */ 49#ifdef OPT_DEBUG 50# define DEBUG(str, v...) \ 51 printf("%s: " str "\n", __FUNCTION__ , ## v) 52#else 53# define DEBUG(str, v...) ((void)0) 54#endif 55 56 57/* 58 * A container that lets us sort a superblock to optimize the skip step jumps 59 */ 60struct pf_skip_step { 61 int ps_count; /* number of items */ 62 TAILQ_HEAD( , pf_opt_rule) ps_rules; 63 TAILQ_ENTRY(pf_skip_step) ps_entry; 64}; 65 66 67/* 68 * A superblock is a block of adjacent rules of similar action. If there 69 * are five PASS rules in a row, they all become members of a superblock. 70 * Once we have a superblock, we are free to re-order any rules within it 71 * in order to improve performance; if a packet is passed, it doesn't matter 72 * who passed it. 73 */ 74struct superblock { 75 TAILQ_HEAD( , pf_opt_rule) sb_rules; 76 TAILQ_ENTRY(superblock) sb_entry; 77 struct superblock *sb_profiled_block; 78 TAILQ_HEAD(skiplist, pf_skip_step) sb_skipsteps[PF_SKIP_COUNT]; 79}; 80TAILQ_HEAD(superblocks, superblock); 81 82 83/* 84 * Description of the PF rule structure. 85 */ 86enum { 87 BARRIER, /* the presence of the field puts the rule in it's own block */ 88 BREAK, /* the field may not differ between rules in a superblock */ 89 NOMERGE, /* the field may not differ between rules when combined */ 90 COMBINED, /* the field may itself be combined with other rules */ 91 DC, /* we just don't care about the field */ 92 NEVER}; /* we should never see this field set?!? */ 93struct pf_rule_field { 94 const char *prf_name; 95 int prf_type; 96 size_t prf_offset; 97 size_t prf_size; 98} pf_rule_desc[] = { 99#define PF_RULE_FIELD(field, ty) \ 100 {#field, \ 101 ty, \ 102 offsetof(struct pf_rule, field), \ 103 sizeof(((struct pf_rule *)0)->field)} 104 105 106 /* 107 * The presence of these fields in a rule put the rule in it's own 108 * superblock. Thus it will not be optimized. It also prevents the 109 * rule from being re-ordered at all. 110 */ 111 PF_RULE_FIELD(label, BARRIER), 112 PF_RULE_FIELD(prob, BARRIER), 113 PF_RULE_FIELD(max_states, BARRIER), 114 PF_RULE_FIELD(max_src_nodes, BARRIER),
| 21 22#include <sys/types.h> 23#include <sys/ioctl.h> 24#include <sys/socket.h> 25 26#include <net/if.h> 27#include <net/pfvar.h> 28 29#include <netinet/in.h> 30#include <arpa/inet.h> 31 32#include <assert.h> 33#include <ctype.h> 34#include <err.h> 35#include <errno.h> 36#include <stddef.h> 37#include <stdio.h> 38#include <stdlib.h> 39#include <string.h> 40 41#include "pfctl_parser.h" 42#include "pfctl.h" 43 44/* The size at which a table becomes faster than individual rules */ 45#define TABLE_THRESHOLD 6 46 47 48/* #define OPT_DEBUG 1 */ 49#ifdef OPT_DEBUG 50# define DEBUG(str, v...) \ 51 printf("%s: " str "\n", __FUNCTION__ , ## v) 52#else 53# define DEBUG(str, v...) ((void)0) 54#endif 55 56 57/* 58 * A container that lets us sort a superblock to optimize the skip step jumps 59 */ 60struct pf_skip_step { 61 int ps_count; /* number of items */ 62 TAILQ_HEAD( , pf_opt_rule) ps_rules; 63 TAILQ_ENTRY(pf_skip_step) ps_entry; 64}; 65 66 67/* 68 * A superblock is a block of adjacent rules of similar action. If there 69 * are five PASS rules in a row, they all become members of a superblock. 70 * Once we have a superblock, we are free to re-order any rules within it 71 * in order to improve performance; if a packet is passed, it doesn't matter 72 * who passed it. 73 */ 74struct superblock { 75 TAILQ_HEAD( , pf_opt_rule) sb_rules; 76 TAILQ_ENTRY(superblock) sb_entry; 77 struct superblock *sb_profiled_block; 78 TAILQ_HEAD(skiplist, pf_skip_step) sb_skipsteps[PF_SKIP_COUNT]; 79}; 80TAILQ_HEAD(superblocks, superblock); 81 82 83/* 84 * Description of the PF rule structure. 85 */ 86enum { 87 BARRIER, /* the presence of the field puts the rule in it's own block */ 88 BREAK, /* the field may not differ between rules in a superblock */ 89 NOMERGE, /* the field may not differ between rules when combined */ 90 COMBINED, /* the field may itself be combined with other rules */ 91 DC, /* we just don't care about the field */ 92 NEVER}; /* we should never see this field set?!? */ 93struct pf_rule_field { 94 const char *prf_name; 95 int prf_type; 96 size_t prf_offset; 97 size_t prf_size; 98} pf_rule_desc[] = { 99#define PF_RULE_FIELD(field, ty) \ 100 {#field, \ 101 ty, \ 102 offsetof(struct pf_rule, field), \ 103 sizeof(((struct pf_rule *)0)->field)} 104 105 106 /* 107 * The presence of these fields in a rule put the rule in it's own 108 * superblock. Thus it will not be optimized. It also prevents the 109 * rule from being re-ordered at all. 110 */ 111 PF_RULE_FIELD(label, BARRIER), 112 PF_RULE_FIELD(prob, BARRIER), 113 PF_RULE_FIELD(max_states, BARRIER), 114 PF_RULE_FIELD(max_src_nodes, BARRIER),
|
| 115 PF_RULE_FIELD(max_src_states, BARRIER), 116 PF_RULE_FIELD(max_src_conn, BARRIER), 117 PF_RULE_FIELD(max_src_conn_rate, BARRIER), 118 PF_RULE_FIELD(anchor, BARRIER), /* for now */
|
115 116 /* 117 * These fields must be the same between all rules in the same superblock. 118 * These rules are allowed to be re-ordered but only among like rules. 119 * For instance we can re-order all 'tag "foo"' rules because they have the 120 * same tag. But we can not re-order between a 'tag "foo"' and a 121 * 'tag "bar"' since that would change the meaning of the ruleset. 122 */ 123 PF_RULE_FIELD(tagname, BREAK), 124 PF_RULE_FIELD(keep_state, BREAK), 125 PF_RULE_FIELD(qname, BREAK),
| 119 120 /* 121 * These fields must be the same between all rules in the same superblock. 122 * These rules are allowed to be re-ordered but only among like rules. 123 * For instance we can re-order all 'tag "foo"' rules because they have the 124 * same tag. But we can not re-order between a 'tag "foo"' and a 125 * 'tag "bar"' since that would change the meaning of the ruleset. 126 */ 127 PF_RULE_FIELD(tagname, BREAK), 128 PF_RULE_FIELD(keep_state, BREAK), 129 PF_RULE_FIELD(qname, BREAK),
|
| 130 PF_RULE_FIELD(pqname, BREAK),
|
126 PF_RULE_FIELD(rt, BREAK), 127 PF_RULE_FIELD(allow_opts, BREAK), 128 PF_RULE_FIELD(rule_flag, BREAK), 129 PF_RULE_FIELD(action, BREAK),
| 131 PF_RULE_FIELD(rt, BREAK), 132 PF_RULE_FIELD(allow_opts, BREAK), 133 PF_RULE_FIELD(rule_flag, BREAK), 134 PF_RULE_FIELD(action, BREAK),
|
| 135 PF_RULE_FIELD(log, BREAK), 136 PF_RULE_FIELD(quick, BREAK), 137 PF_RULE_FIELD(return_ttl, BREAK), 138 PF_RULE_FIELD(overload_tblname, BREAK), 139 PF_RULE_FIELD(flush, BREAK), 140 PF_RULE_FIELD(rpool, BREAK), 141 PF_RULE_FIELD(logif, BREAK),
|
130 131 /* 132 * Any fields not listed in this structure act as BREAK fields 133 */ 134 135 136 /* 137 * These fields must not differ when we merge two rules together but 138 * their difference isn't enough to put the rules in different superblocks. 139 * There are no problems re-ordering any rules with these fields. 140 */ 141 PF_RULE_FIELD(af, NOMERGE), 142 PF_RULE_FIELD(ifnot, NOMERGE),
| 142 143 /* 144 * Any fields not listed in this structure act as BREAK fields 145 */ 146 147 148 /* 149 * These fields must not differ when we merge two rules together but 150 * their difference isn't enough to put the rules in different superblocks. 151 * There are no problems re-ordering any rules with these fields. 152 */ 153 PF_RULE_FIELD(af, NOMERGE), 154 PF_RULE_FIELD(ifnot, NOMERGE),
|
143 PF_RULE_FIELD(ifname, NOMERGE),
| 155 PF_RULE_FIELD(ifname, NOMERGE), /* hack for IF groups */
|
144 PF_RULE_FIELD(match_tag_not, NOMERGE), 145 PF_RULE_FIELD(match_tagname, NOMERGE), 146 PF_RULE_FIELD(os_fingerprint, NOMERGE), 147 PF_RULE_FIELD(timeout, NOMERGE), 148 PF_RULE_FIELD(return_icmp, NOMERGE), 149 PF_RULE_FIELD(return_icmp6, NOMERGE), 150 PF_RULE_FIELD(uid, NOMERGE), 151 PF_RULE_FIELD(gid, NOMERGE), 152 PF_RULE_FIELD(direction, NOMERGE), 153 PF_RULE_FIELD(proto, NOMERGE), 154 PF_RULE_FIELD(type, NOMERGE), 155 PF_RULE_FIELD(code, NOMERGE), 156 PF_RULE_FIELD(flags, NOMERGE), 157 PF_RULE_FIELD(flagset, NOMERGE), 158 PF_RULE_FIELD(tos, NOMERGE), 159 PF_RULE_FIELD(src.port, NOMERGE), 160 PF_RULE_FIELD(dst.port, NOMERGE), 161 PF_RULE_FIELD(src.port_op, NOMERGE), 162 PF_RULE_FIELD(dst.port_op, NOMERGE), 163 PF_RULE_FIELD(src.neg, NOMERGE), 164 PF_RULE_FIELD(dst.neg, NOMERGE), 165 166 /* These fields can be merged */ 167 PF_RULE_FIELD(src.addr, COMBINED), 168 PF_RULE_FIELD(dst.addr, COMBINED), 169 170 /* We just don't care about these fields. They're set by the kernel */ 171 PF_RULE_FIELD(skip, DC), 172 PF_RULE_FIELD(evaluations, DC), 173 PF_RULE_FIELD(packets, DC), 174 PF_RULE_FIELD(bytes, DC), 175 PF_RULE_FIELD(kif, DC),
| 156 PF_RULE_FIELD(match_tag_not, NOMERGE), 157 PF_RULE_FIELD(match_tagname, NOMERGE), 158 PF_RULE_FIELD(os_fingerprint, NOMERGE), 159 PF_RULE_FIELD(timeout, NOMERGE), 160 PF_RULE_FIELD(return_icmp, NOMERGE), 161 PF_RULE_FIELD(return_icmp6, NOMERGE), 162 PF_RULE_FIELD(uid, NOMERGE), 163 PF_RULE_FIELD(gid, NOMERGE), 164 PF_RULE_FIELD(direction, NOMERGE), 165 PF_RULE_FIELD(proto, NOMERGE), 166 PF_RULE_FIELD(type, NOMERGE), 167 PF_RULE_FIELD(code, NOMERGE), 168 PF_RULE_FIELD(flags, NOMERGE), 169 PF_RULE_FIELD(flagset, NOMERGE), 170 PF_RULE_FIELD(tos, NOMERGE), 171 PF_RULE_FIELD(src.port, NOMERGE), 172 PF_RULE_FIELD(dst.port, NOMERGE), 173 PF_RULE_FIELD(src.port_op, NOMERGE), 174 PF_RULE_FIELD(dst.port_op, NOMERGE), 175 PF_RULE_FIELD(src.neg, NOMERGE), 176 PF_RULE_FIELD(dst.neg, NOMERGE), 177 178 /* These fields can be merged */ 179 PF_RULE_FIELD(src.addr, COMBINED), 180 PF_RULE_FIELD(dst.addr, COMBINED), 181 182 /* We just don't care about these fields. They're set by the kernel */ 183 PF_RULE_FIELD(skip, DC), 184 PF_RULE_FIELD(evaluations, DC), 185 PF_RULE_FIELD(packets, DC), 186 PF_RULE_FIELD(bytes, DC), 187 PF_RULE_FIELD(kif, DC),
|
176 PF_RULE_FIELD(anchor, DC),
| |
177 PF_RULE_FIELD(states, DC), 178 PF_RULE_FIELD(src_nodes, DC), 179 PF_RULE_FIELD(nr, DC), 180 PF_RULE_FIELD(entries, DC), 181 PF_RULE_FIELD(qid, DC), 182 PF_RULE_FIELD(pqid, DC), 183 PF_RULE_FIELD(anchor_relative, DC), 184 PF_RULE_FIELD(anchor_wildcard, DC),
| 188 PF_RULE_FIELD(states, DC), 189 PF_RULE_FIELD(src_nodes, DC), 190 PF_RULE_FIELD(nr, DC), 191 PF_RULE_FIELD(entries, DC), 192 PF_RULE_FIELD(qid, DC), 193 PF_RULE_FIELD(pqid, DC), 194 PF_RULE_FIELD(anchor_relative, DC), 195 PF_RULE_FIELD(anchor_wildcard, DC),
|
| 196 PF_RULE_FIELD(tag, DC), 197 PF_RULE_FIELD(match_tag, DC), 198 PF_RULE_FIELD(overload_tbl, DC),
|
185 186 /* These fields should never be set in a PASS/BLOCK rule */ 187 PF_RULE_FIELD(natpass, NEVER), 188 PF_RULE_FIELD(max_mss, NEVER), 189 PF_RULE_FIELD(min_ttl, NEVER), 190}; 191 192 193 194int add_opt_table(struct pfctl *, struct pf_opt_tbl **, sa_family_t, 195 struct pf_rule_addr *); 196int addrs_combineable(struct pf_rule_addr *, struct pf_rule_addr *); 197int addrs_equal(struct pf_rule_addr *, struct pf_rule_addr *); 198int block_feedback(struct pfctl *, struct superblock *); 199int combine_rules(struct pfctl *, struct superblock *); 200void comparable_rule(struct pf_rule *, const struct pf_rule *, int); 201int construct_superblocks(struct pfctl *, struct pf_opt_queue *, 202 struct superblocks *); 203void exclude_supersets(struct pf_rule *, struct pf_rule *);
| 199 200 /* These fields should never be set in a PASS/BLOCK rule */ 201 PF_RULE_FIELD(natpass, NEVER), 202 PF_RULE_FIELD(max_mss, NEVER), 203 PF_RULE_FIELD(min_ttl, NEVER), 204}; 205 206 207 208int add_opt_table(struct pfctl *, struct pf_opt_tbl **, sa_family_t, 209 struct pf_rule_addr *); 210int addrs_combineable(struct pf_rule_addr *, struct pf_rule_addr *); 211int addrs_equal(struct pf_rule_addr *, struct pf_rule_addr *); 212int block_feedback(struct pfctl *, struct superblock *); 213int combine_rules(struct pfctl *, struct superblock *); 214void comparable_rule(struct pf_rule *, const struct pf_rule *, int); 215int construct_superblocks(struct pfctl *, struct pf_opt_queue *, 216 struct superblocks *); 217void exclude_supersets(struct pf_rule *, struct pf_rule *);
|
| 218int interface_group(const char *);
|
204int load_feedback_profile(struct pfctl *, struct superblocks *); 205int optimize_superblock(struct pfctl *, struct superblock *); 206int pf_opt_create_table(struct pfctl *, struct pf_opt_tbl *); 207void remove_from_skipsteps(struct skiplist *, struct superblock *, 208 struct pf_opt_rule *, struct pf_skip_step *); 209int remove_identical_rules(struct pfctl *, struct superblock *); 210int reorder_rules(struct pfctl *, struct superblock *, int); 211int rules_combineable(struct pf_rule *, struct pf_rule *); 212void skip_append(struct superblock *, int, struct pf_skip_step *, 213 struct pf_opt_rule *); 214int skip_compare(int, struct pf_skip_step *, struct pf_opt_rule *); 215void skip_init(void); 216int skip_cmp_af(struct pf_rule *, struct pf_rule *); 217int skip_cmp_dir(struct pf_rule *, struct pf_rule *); 218int skip_cmp_dst_addr(struct pf_rule *, struct pf_rule *); 219int skip_cmp_dst_port(struct pf_rule *, struct pf_rule *); 220int skip_cmp_ifp(struct pf_rule *, struct pf_rule *); 221int skip_cmp_proto(struct pf_rule *, struct pf_rule *); 222int skip_cmp_src_addr(struct pf_rule *, struct pf_rule *); 223int skip_cmp_src_port(struct pf_rule *, struct pf_rule *); 224int superblock_inclusive(struct superblock *, struct pf_opt_rule *); 225void superblock_free(struct pfctl *, struct superblock *); 226 227 228int (*skip_comparitors[PF_SKIP_COUNT])(struct pf_rule *, struct pf_rule *); 229const char *skip_comparitors_names[PF_SKIP_COUNT]; 230#define PF_SKIP_COMPARITORS { \ 231 { "ifp", PF_SKIP_IFP, skip_cmp_ifp }, \ 232 { "dir", PF_SKIP_DIR, skip_cmp_dir }, \ 233 { "af", PF_SKIP_AF, skip_cmp_af }, \ 234 { "proto", PF_SKIP_PROTO, skip_cmp_proto }, \ 235 { "saddr", PF_SKIP_SRC_ADDR, skip_cmp_src_addr }, \ 236 { "sport", PF_SKIP_SRC_PORT, skip_cmp_src_port }, \ 237 { "daddr", PF_SKIP_DST_ADDR, skip_cmp_dst_addr }, \ 238 { "dport", PF_SKIP_DST_PORT, skip_cmp_dst_port } \ 239} 240 241struct pfr_buffer table_buffer; 242int table_identifier; 243 244 245int
| 219int load_feedback_profile(struct pfctl *, struct superblocks *); 220int optimize_superblock(struct pfctl *, struct superblock *); 221int pf_opt_create_table(struct pfctl *, struct pf_opt_tbl *); 222void remove_from_skipsteps(struct skiplist *, struct superblock *, 223 struct pf_opt_rule *, struct pf_skip_step *); 224int remove_identical_rules(struct pfctl *, struct superblock *); 225int reorder_rules(struct pfctl *, struct superblock *, int); 226int rules_combineable(struct pf_rule *, struct pf_rule *); 227void skip_append(struct superblock *, int, struct pf_skip_step *, 228 struct pf_opt_rule *); 229int skip_compare(int, struct pf_skip_step *, struct pf_opt_rule *); 230void skip_init(void); 231int skip_cmp_af(struct pf_rule *, struct pf_rule *); 232int skip_cmp_dir(struct pf_rule *, struct pf_rule *); 233int skip_cmp_dst_addr(struct pf_rule *, struct pf_rule *); 234int skip_cmp_dst_port(struct pf_rule *, struct pf_rule *); 235int skip_cmp_ifp(struct pf_rule *, struct pf_rule *); 236int skip_cmp_proto(struct pf_rule *, struct pf_rule *); 237int skip_cmp_src_addr(struct pf_rule *, struct pf_rule *); 238int skip_cmp_src_port(struct pf_rule *, struct pf_rule *); 239int superblock_inclusive(struct superblock *, struct pf_opt_rule *); 240void superblock_free(struct pfctl *, struct superblock *); 241 242 243int (*skip_comparitors[PF_SKIP_COUNT])(struct pf_rule *, struct pf_rule *); 244const char *skip_comparitors_names[PF_SKIP_COUNT]; 245#define PF_SKIP_COMPARITORS { \ 246 { "ifp", PF_SKIP_IFP, skip_cmp_ifp }, \ 247 { "dir", PF_SKIP_DIR, skip_cmp_dir }, \ 248 { "af", PF_SKIP_AF, skip_cmp_af }, \ 249 { "proto", PF_SKIP_PROTO, skip_cmp_proto }, \ 250 { "saddr", PF_SKIP_SRC_ADDR, skip_cmp_src_addr }, \ 251 { "sport", PF_SKIP_SRC_PORT, skip_cmp_src_port }, \ 252 { "daddr", PF_SKIP_DST_ADDR, skip_cmp_dst_addr }, \ 253 { "dport", PF_SKIP_DST_PORT, skip_cmp_dst_port } \ 254} 255 256struct pfr_buffer table_buffer; 257int table_identifier; 258 259 260int
|
246pfctl_optimize_rules(struct pfctl *pf)
| 261pfctl_optimize_ruleset(struct pfctl *pf, struct pf_ruleset *rs)
|
247{ 248 struct superblocks superblocks;
| 262{ 263 struct superblocks superblocks;
|
| 264 struct pf_opt_queue opt_queue;
|
249 struct superblock *block; 250 struct pf_opt_rule *por;
| 265 struct superblock *block; 266 struct pf_opt_rule *por;
|
251 int nr;
| 267 struct pf_rule *r; 268 struct pf_rulequeue *old_rules;
|
252 253 DEBUG("optimizing ruleset"); 254 memset(&table_buffer, 0, sizeof(table_buffer)); 255 skip_init();
| 269 270 DEBUG("optimizing ruleset"); 271 memset(&table_buffer, 0, sizeof(table_buffer)); 272 skip_init();
|
| 273 TAILQ_INIT(&opt_queue);
|
256
| 274
|
257 if (TAILQ_FIRST(&pf->opt_queue)) 258 nr = TAILQ_FIRST(&pf->opt_queue)->por_rule.nr;
| 275 old_rules = rs->rules[PF_RULESET_FILTER].active.ptr; 276 rs->rules[PF_RULESET_FILTER].active.ptr = 277 rs->rules[PF_RULESET_FILTER].inactive.ptr; 278 rs->rules[PF_RULESET_FILTER].inactive.ptr = old_rules;
|
259
| 279
|
| 280 /* 281 * XXX expanding the pf_opt_rule format throughout pfctl might allow 282 * us to avoid all this copying. 283 */ 284 while ((r = TAILQ_FIRST(rs->rules[PF_RULESET_FILTER].inactive.ptr)) 285 != NULL) { 286 TAILQ_REMOVE(rs->rules[PF_RULESET_FILTER].inactive.ptr, r, 287 entries); 288 if ((por = calloc(1, sizeof(*por))) == NULL) 289 err(1, "calloc"); 290 memcpy(&por->por_rule, r, sizeof(*r)); 291 if (TAILQ_FIRST(&r->rpool.list) != NULL) { 292 TAILQ_INIT(&por->por_rule.rpool.list); 293 pfctl_move_pool(&r->rpool, &por->por_rule.rpool); 294 } else 295 bzero(&por->por_rule.rpool, 296 sizeof(por->por_rule.rpool)); 297 298 299 TAILQ_INSERT_TAIL(&opt_queue, por, por_entry); 300 } 301
|
260 TAILQ_INIT(&superblocks);
| 302 TAILQ_INIT(&superblocks);
|
261 if (construct_superblocks(pf, &pf->opt_queue, &superblocks))
| 303 if (construct_superblocks(pf, &opt_queue, &superblocks))
|
262 goto error; 263
| 304 goto error; 305
|
264 if (pf->opts & PF_OPT_OPTIMIZE_PROFILE) {
| 306 if (pf->optimize & PF_OPTIMIZE_PROFILE) {
|
265 if (load_feedback_profile(pf, &superblocks)) 266 goto error; 267 } 268 269 TAILQ_FOREACH(block, &superblocks, sb_entry) { 270 if (optimize_superblock(pf, block)) 271 goto error; 272 } 273
| 307 if (load_feedback_profile(pf, &superblocks)) 308 goto error; 309 } 310 311 TAILQ_FOREACH(block, &superblocks, sb_entry) { 312 if (optimize_superblock(pf, block)) 313 goto error; 314 } 315
|
274 275 /* 276 * Optimizations are done so we turn off the optimization flag and 277 * put the rules right back into the regular codepath. 278 */ 279 pf->opts &= ~PF_OPT_OPTIMIZE; 280
| 316 rs->anchor->refcnt = 0;
|
281 while ((block = TAILQ_FIRST(&superblocks))) { 282 TAILQ_REMOVE(&superblocks, block, sb_entry); 283 284 while ((por = TAILQ_FIRST(&block->sb_rules))) { 285 TAILQ_REMOVE(&block->sb_rules, por, por_entry);
| 317 while ((block = TAILQ_FIRST(&superblocks))) { 318 TAILQ_REMOVE(&superblocks, block, sb_entry); 319 320 while ((por = TAILQ_FIRST(&block->sb_rules))) { 321 TAILQ_REMOVE(&block->sb_rules, por, por_entry);
|
286 por->por_rule.nr = nr++; 287 if (pfctl_add_rule(pf, &por->por_rule, 288 por->por_anchor)) { 289 free(por); 290 goto error; 291 }
| 322 por->por_rule.nr = rs->anchor->refcnt++; 323 if ((r = calloc(1, sizeof(*r))) == NULL) 324 err(1, "calloc"); 325 memcpy(r, &por->por_rule, sizeof(*r)); 326 TAILQ_INIT(&r->rpool.list); 327 pfctl_move_pool(&por->por_rule.rpool, &r->rpool); 328 TAILQ_INSERT_TAIL( 329 rs->rules[PF_RULESET_FILTER].active.ptr, 330 r, entries);
|
292 free(por); 293 } 294 free(block); 295 } 296 297 return (0); 298 299error:
| 331 free(por); 332 } 333 free(block); 334 } 335 336 return (0); 337 338error:
|
300 while ((por = TAILQ_FIRST(&pf->opt_queue))) { 301 TAILQ_REMOVE(&pf->opt_queue, por, por_entry);
| 339 while ((por = TAILQ_FIRST(&opt_queue))) { 340 TAILQ_REMOVE(&opt_queue, por, por_entry);
|
302 if (por->por_src_tbl) { 303 pfr_buf_clear(por->por_src_tbl->pt_buf); 304 free(por->por_src_tbl->pt_buf); 305 free(por->por_src_tbl); 306 } 307 if (por->por_dst_tbl) { 308 pfr_buf_clear(por->por_dst_tbl->pt_buf); 309 free(por->por_dst_tbl->pt_buf); 310 free(por->por_dst_tbl); 311 } 312 free(por); 313 } 314 while ((block = TAILQ_FIRST(&superblocks))) { 315 TAILQ_REMOVE(&superblocks, block, sb_entry); 316 superblock_free(pf, block); 317 } 318 return (1); 319} 320 321 322/* 323 * Go ahead and optimize a superblock 324 */ 325int 326optimize_superblock(struct pfctl *pf, struct superblock *block) 327{ 328#ifdef OPT_DEBUG 329 struct pf_opt_rule *por; 330#endif /* OPT_DEBUG */ 331 332 /* We have a few optimization passes: 333 * 1) remove duplicate rules or rules that are a subset of other 334 * rules 335 * 2) combine otherwise identical rules with different IP addresses 336 * into a single rule and put the addresses in a table. 337 * 3) re-order the rules to improve kernel skip steps 338 * 4) re-order the 'quick' rules based on feedback from the 339 * active ruleset statistics 340 * 341 * XXX combine_rules() doesn't combine v4 and v6 rules. would just 342 * have to keep af in the table container, make af 'COMBINE' and 343 * twiddle the af on the merged rule 344 * XXX maybe add a weighting to the metric on skipsteps when doing 345 * reordering. sometimes two sequential tables will be better 346 * that four consecutive interfaces. 347 * XXX need to adjust the skipstep count of everything after PROTO, 348 * since they aren't actually checked on a proto mismatch in 349 * pf_test_{tcp, udp, icmp}() 350 * XXX should i treat proto=0, af=0 or dir=0 special in skepstep 351 * calculation since they are a DC? 352 * XXX keep last skiplist of last superblock to influence this 353 * superblock. '5 inet6 log' should make '3 inet6' come before '4 354 * inet' in the next superblock. 355 * XXX would be useful to add tables for ports 356 * XXX we can also re-order some mutually exclusive superblocks to 357 * try merging superblocks before any of these optimization passes. 358 * for instance a single 'log in' rule in the middle of non-logging 359 * out rules. 360 */ 361 362 /* shortcut. there will be alot of 1-rule superblocks */ 363 if (!TAILQ_NEXT(TAILQ_FIRST(&block->sb_rules), por_entry)) 364 return (0); 365 366#ifdef OPT_DEBUG 367 printf("--- Superblock ---\n"); 368 TAILQ_FOREACH(por, &block->sb_rules, por_entry) { 369 printf(" ");
| 341 if (por->por_src_tbl) { 342 pfr_buf_clear(por->por_src_tbl->pt_buf); 343 free(por->por_src_tbl->pt_buf); 344 free(por->por_src_tbl); 345 } 346 if (por->por_dst_tbl) { 347 pfr_buf_clear(por->por_dst_tbl->pt_buf); 348 free(por->por_dst_tbl->pt_buf); 349 free(por->por_dst_tbl); 350 } 351 free(por); 352 } 353 while ((block = TAILQ_FIRST(&superblocks))) { 354 TAILQ_REMOVE(&superblocks, block, sb_entry); 355 superblock_free(pf, block); 356 } 357 return (1); 358} 359 360 361/* 362 * Go ahead and optimize a superblock 363 */ 364int 365optimize_superblock(struct pfctl *pf, struct superblock *block) 366{ 367#ifdef OPT_DEBUG 368 struct pf_opt_rule *por; 369#endif /* OPT_DEBUG */ 370 371 /* We have a few optimization passes: 372 * 1) remove duplicate rules or rules that are a subset of other 373 * rules 374 * 2) combine otherwise identical rules with different IP addresses 375 * into a single rule and put the addresses in a table. 376 * 3) re-order the rules to improve kernel skip steps 377 * 4) re-order the 'quick' rules based on feedback from the 378 * active ruleset statistics 379 * 380 * XXX combine_rules() doesn't combine v4 and v6 rules. would just 381 * have to keep af in the table container, make af 'COMBINE' and 382 * twiddle the af on the merged rule 383 * XXX maybe add a weighting to the metric on skipsteps when doing 384 * reordering. sometimes two sequential tables will be better 385 * that four consecutive interfaces. 386 * XXX need to adjust the skipstep count of everything after PROTO, 387 * since they aren't actually checked on a proto mismatch in 388 * pf_test_{tcp, udp, icmp}() 389 * XXX should i treat proto=0, af=0 or dir=0 special in skepstep 390 * calculation since they are a DC? 391 * XXX keep last skiplist of last superblock to influence this 392 * superblock. '5 inet6 log' should make '3 inet6' come before '4 393 * inet' in the next superblock. 394 * XXX would be useful to add tables for ports 395 * XXX we can also re-order some mutually exclusive superblocks to 396 * try merging superblocks before any of these optimization passes. 397 * for instance a single 'log in' rule in the middle of non-logging 398 * out rules. 399 */ 400 401 /* shortcut. there will be alot of 1-rule superblocks */ 402 if (!TAILQ_NEXT(TAILQ_FIRST(&block->sb_rules), por_entry)) 403 return (0); 404 405#ifdef OPT_DEBUG 406 printf("--- Superblock ---\n"); 407 TAILQ_FOREACH(por, &block->sb_rules, por_entry) { 408 printf(" ");
|
370 print_rule(&por->por_rule, por->por_anchor, 1);
| 409 print_rule(&por->por_rule, por->por_rule.anchor ? 410 por->por_rule.anchor->name : "", 1);
|
371 } 372#endif /* OPT_DEBUG */ 373 374 375 if (remove_identical_rules(pf, block)) 376 return (1); 377 if (combine_rules(pf, block)) 378 return (1);
| 411 } 412#endif /* OPT_DEBUG */ 413 414 415 if (remove_identical_rules(pf, block)) 416 return (1); 417 if (combine_rules(pf, block)) 418 return (1);
|
379 if ((pf->opts & PF_OPT_OPTIMIZE_PROFILE) &&
| 419 if ((pf->optimize & PF_OPTIMIZE_PROFILE) &&
|
380 TAILQ_FIRST(&block->sb_rules)->por_rule.quick && 381 block->sb_profiled_block) { 382 if (block_feedback(pf, block)) 383 return (1); 384 } else if (reorder_rules(pf, block, 0)) { 385 return (1); 386 } 387 388 /* 389 * Don't add any optimization passes below reorder_rules(). It will 390 * have divided superblocks into smaller blocks for further refinement 391 * and doesn't put them back together again. What once was a true 392 * superblock might have been split into multiple superblocks. 393 */ 394 395#ifdef OPT_DEBUG 396 printf("--- END Superblock ---\n"); 397#endif /* OPT_DEBUG */ 398 return (0); 399} 400 401 402/* 403 * Optimization pass #1: remove identical rules 404 */ 405int 406remove_identical_rules(struct pfctl *pf, struct superblock *block) 407{ 408 struct pf_opt_rule *por1, *por2, *por_next, *por2_next; 409 struct pf_rule a, a2, b, b2; 410 411 for (por1 = TAILQ_FIRST(&block->sb_rules); por1; por1 = por_next) { 412 por_next = TAILQ_NEXT(por1, por_entry); 413 for (por2 = por_next; por2; por2 = por2_next) { 414 por2_next = TAILQ_NEXT(por2, por_entry); 415 comparable_rule(&a, &por1->por_rule, DC); 416 comparable_rule(&b, &por2->por_rule, DC); 417 memcpy(&a2, &a, sizeof(a2)); 418 memcpy(&b2, &b, sizeof(b2)); 419 420 exclude_supersets(&a, &b); 421 exclude_supersets(&b2, &a2); 422 if (memcmp(&a, &b, sizeof(a)) == 0) { 423 DEBUG("removing identical rule nr%d = *nr%d*", 424 por1->por_rule.nr, por2->por_rule.nr); 425 TAILQ_REMOVE(&block->sb_rules, por2, por_entry); 426 if (por_next == por2) 427 por_next = TAILQ_NEXT(por1, por_entry); 428 free(por2); 429 } else if (memcmp(&a2, &b2, sizeof(a2)) == 0) { 430 DEBUG("removing identical rule *nr%d* = nr%d", 431 por1->por_rule.nr, por2->por_rule.nr); 432 TAILQ_REMOVE(&block->sb_rules, por1, por_entry); 433 free(por1); 434 break; 435 } 436 } 437 } 438 439 return (0); 440} 441 442 443/* 444 * Optimization pass #2: combine similar rules with different addresses 445 * into a single rule and a table 446 */ 447int 448combine_rules(struct pfctl *pf, struct superblock *block) 449{ 450 struct pf_opt_rule *p1, *p2, *por_next; 451 int src_eq, dst_eq; 452 453 if ((pf->loadopt & PFCTL_FLAG_TABLE) == 0) { 454 warnx("Must enable table loading for optimizations"); 455 return (1); 456 } 457 458 /* First we make a pass to combine the rules. O(n log n) */ 459 TAILQ_FOREACH(p1, &block->sb_rules, por_entry) { 460 for (p2 = TAILQ_NEXT(p1, por_entry); p2; p2 = por_next) { 461 por_next = TAILQ_NEXT(p2, por_entry); 462 463 src_eq = addrs_equal(&p1->por_rule.src, 464 &p2->por_rule.src); 465 dst_eq = addrs_equal(&p1->por_rule.dst, 466 &p2->por_rule.dst); 467 468 if (src_eq && !dst_eq && p1->por_src_tbl == NULL && 469 p2->por_dst_tbl == NULL && 470 p2->por_src_tbl == NULL && 471 rules_combineable(&p1->por_rule, &p2->por_rule) && 472 addrs_combineable(&p1->por_rule.dst, 473 &p2->por_rule.dst)) { 474 DEBUG("can combine rules nr%d = nr%d", 475 p1->por_rule.nr, p2->por_rule.nr); 476 if (p1->por_dst_tbl == NULL && 477 add_opt_table(pf, &p1->por_dst_tbl, 478 p1->por_rule.af, &p1->por_rule.dst)) 479 return (1); 480 if (add_opt_table(pf, &p1->por_dst_tbl, 481 p1->por_rule.af, &p2->por_rule.dst)) 482 return (1); 483 p2->por_dst_tbl = p1->por_dst_tbl; 484 if (p1->por_dst_tbl->pt_rulecount >= 485 TABLE_THRESHOLD) { 486 TAILQ_REMOVE(&block->sb_rules, p2, 487 por_entry); 488 free(p2); 489 } 490 } else if (!src_eq && dst_eq && p1->por_dst_tbl == NULL 491 && p2->por_src_tbl == NULL && 492 p2->por_dst_tbl == NULL && 493 rules_combineable(&p1->por_rule, &p2->por_rule) && 494 addrs_combineable(&p1->por_rule.src, 495 &p2->por_rule.src)) { 496 DEBUG("can combine rules nr%d = nr%d", 497 p1->por_rule.nr, p2->por_rule.nr); 498 if (p1->por_src_tbl == NULL && 499 add_opt_table(pf, &p1->por_src_tbl, 500 p1->por_rule.af, &p1->por_rule.src)) 501 return (1); 502 if (add_opt_table(pf, &p1->por_src_tbl, 503 p1->por_rule.af, &p2->por_rule.src)) 504 return (1); 505 p2->por_src_tbl = p1->por_src_tbl; 506 if (p1->por_src_tbl->pt_rulecount >= 507 TABLE_THRESHOLD) { 508 TAILQ_REMOVE(&block->sb_rules, p2, 509 por_entry); 510 free(p2); 511 } 512 } 513 } 514 } 515 516 517 /* 518 * Then we make a final pass to create a valid table name and 519 * insert the name into the rules. 520 */ 521 for (p1 = TAILQ_FIRST(&block->sb_rules); p1; p1 = por_next) { 522 por_next = TAILQ_NEXT(p1, por_entry); 523 assert(p1->por_src_tbl == NULL || p1->por_dst_tbl == NULL); 524 525 if (p1->por_src_tbl && p1->por_src_tbl->pt_rulecount >= 526 TABLE_THRESHOLD) { 527 if (p1->por_src_tbl->pt_generated) { 528 /* This rule is included in a table */ 529 TAILQ_REMOVE(&block->sb_rules, p1, por_entry); 530 free(p1); 531 continue; 532 } 533 p1->por_src_tbl->pt_generated = 1; 534 535 if ((pf->opts & PF_OPT_NOACTION) == 0 && 536 pf_opt_create_table(pf, p1->por_src_tbl)) 537 return (1); 538 539 pf->tdirty = 1; 540 541 if (pf->opts & PF_OPT_VERBOSE) 542 print_tabledef(p1->por_src_tbl->pt_name, 543 PFR_TFLAG_CONST, 1, 544 &p1->por_src_tbl->pt_nodes); 545 546 memset(&p1->por_rule.src.addr, 0, 547 sizeof(p1->por_rule.src.addr)); 548 p1->por_rule.src.addr.type = PF_ADDR_TABLE; 549 strlcpy(p1->por_rule.src.addr.v.tblname, 550 p1->por_src_tbl->pt_name, 551 sizeof(p1->por_rule.src.addr.v.tblname)); 552 553 pfr_buf_clear(p1->por_src_tbl->pt_buf); 554 free(p1->por_src_tbl->pt_buf); 555 p1->por_src_tbl->pt_buf = NULL; 556 } 557 if (p1->por_dst_tbl && p1->por_dst_tbl->pt_rulecount >= 558 TABLE_THRESHOLD) { 559 if (p1->por_dst_tbl->pt_generated) { 560 /* This rule is included in a table */ 561 TAILQ_REMOVE(&block->sb_rules, p1, por_entry); 562 free(p1); 563 continue; 564 } 565 p1->por_dst_tbl->pt_generated = 1; 566 567 if ((pf->opts & PF_OPT_NOACTION) == 0 && 568 pf_opt_create_table(pf, p1->por_dst_tbl)) 569 return (1); 570 pf->tdirty = 1; 571 572 if (pf->opts & PF_OPT_VERBOSE) 573 print_tabledef(p1->por_dst_tbl->pt_name, 574 PFR_TFLAG_CONST, 1, 575 &p1->por_dst_tbl->pt_nodes); 576 577 memset(&p1->por_rule.dst.addr, 0, 578 sizeof(p1->por_rule.dst.addr)); 579 p1->por_rule.dst.addr.type = PF_ADDR_TABLE; 580 strlcpy(p1->por_rule.dst.addr.v.tblname, 581 p1->por_dst_tbl->pt_name, 582 sizeof(p1->por_rule.dst.addr.v.tblname)); 583 584 pfr_buf_clear(p1->por_dst_tbl->pt_buf); 585 free(p1->por_dst_tbl->pt_buf); 586 p1->por_dst_tbl->pt_buf = NULL; 587 } 588 } 589 590 return (0); 591} 592 593 594/* 595 * Optimization pass #3: re-order rules to improve skip steps 596 */ 597int 598reorder_rules(struct pfctl *pf, struct superblock *block, int depth) 599{ 600 struct superblock *newblock; 601 struct pf_skip_step *skiplist; 602 struct pf_opt_rule *por; 603 int i, largest, largest_list, rule_count = 0; 604 TAILQ_HEAD( , pf_opt_rule) head; 605 606 /* 607 * Calculate the best-case skip steps. We put each rule in a list 608 * of other rules with common fields 609 */ 610 for (i = 0; i < PF_SKIP_COUNT; i++) { 611 TAILQ_FOREACH(por, &block->sb_rules, por_entry) { 612 TAILQ_FOREACH(skiplist, &block->sb_skipsteps[i], 613 ps_entry) { 614 if (skip_compare(i, skiplist, por) == 0) 615 break; 616 } 617 if (skiplist == NULL) { 618 if ((skiplist = calloc(1, sizeof(*skiplist))) == 619 NULL) 620 err(1, "calloc"); 621 TAILQ_INIT(&skiplist->ps_rules); 622 TAILQ_INSERT_TAIL(&block->sb_skipsteps[i], 623 skiplist, ps_entry); 624 } 625 skip_append(block, i, skiplist, por); 626 } 627 } 628 629 TAILQ_FOREACH(por, &block->sb_rules, por_entry) 630 rule_count++; 631 632 /* 633 * Now we're going to ignore any fields that are identical between 634 * all of the rules in the superblock and those fields which differ 635 * between every rule in the superblock. 636 */ 637 largest = 0; 638 for (i = 0; i < PF_SKIP_COUNT; i++) { 639 skiplist = TAILQ_FIRST(&block->sb_skipsteps[i]); 640 if (skiplist->ps_count == rule_count) { 641 DEBUG("(%d) original skipstep '%s' is all rules", 642 depth, skip_comparitors_names[i]); 643 skiplist->ps_count = 0; 644 } else if (skiplist->ps_count == 1) { 645 skiplist->ps_count = 0; 646 } else { 647 DEBUG("(%d) original skipstep '%s' largest jump is %d", 648 depth, skip_comparitors_names[i], 649 skiplist->ps_count); 650 if (skiplist->ps_count > largest) 651 largest = skiplist->ps_count; 652 } 653 } 654 if (largest == 0) { 655 /* Ugh. There is NO commonality in the superblock on which 656 * optimize the skipsteps optimization. 657 */ 658 goto done; 659 } 660 661 /* 662 * Now we're going to empty the superblock rule list and re-create 663 * it based on a more optimal skipstep order. 664 */ 665 TAILQ_INIT(&head); 666 while ((por = TAILQ_FIRST(&block->sb_rules))) { 667 TAILQ_REMOVE(&block->sb_rules, por, por_entry); 668 TAILQ_INSERT_TAIL(&head, por, por_entry); 669 } 670 671 672 while (!TAILQ_EMPTY(&head)) { 673 largest = 1; 674 675 /* 676 * Find the most useful skip steps remaining 677 */ 678 for (i = 0; i < PF_SKIP_COUNT; i++) { 679 skiplist = TAILQ_FIRST(&block->sb_skipsteps[i]); 680 if (skiplist->ps_count > largest) { 681 largest = skiplist->ps_count; 682 largest_list = i; 683 } 684 } 685 686 if (largest <= 1) { 687 /* 688 * Nothing useful left. Leave remaining rules in order. 689 */ 690 DEBUG("(%d) no more commonality for skip steps", depth); 691 while ((por = TAILQ_FIRST(&head))) { 692 TAILQ_REMOVE(&head, por, por_entry); 693 TAILQ_INSERT_TAIL(&block->sb_rules, por, 694 por_entry); 695 } 696 } else { 697 /* 698 * There is commonality. Extract those common rules 699 * and place them in the ruleset adjacent to each 700 * other. 701 */ 702 skiplist = TAILQ_FIRST(&block->sb_skipsteps[ 703 largest_list]); 704 DEBUG("(%d) skipstep '%s' largest jump is %d @ #%d", 705 depth, skip_comparitors_names[largest_list], 706 largest, TAILQ_FIRST(&TAILQ_FIRST(&block-> 707 sb_skipsteps [largest_list])->ps_rules)-> 708 por_rule.nr); 709 TAILQ_REMOVE(&block->sb_skipsteps[largest_list], 710 skiplist, ps_entry); 711 712 713 /* 714 * There may be further commonality inside these 715 * rules. So we'll split them off into they're own 716 * superblock and pass it back into the optimizer. 717 */ 718 if (skiplist->ps_count > 2) { 719 if ((newblock = calloc(1, sizeof(*newblock))) 720 == NULL) { 721 warn("calloc"); 722 return (1); 723 } 724 TAILQ_INIT(&newblock->sb_rules); 725 for (i = 0; i < PF_SKIP_COUNT; i++) 726 TAILQ_INIT(&newblock->sb_skipsteps[i]); 727 TAILQ_INSERT_BEFORE(block, newblock, sb_entry); 728 DEBUG("(%d) splitting off %d rules from superblock @ #%d", 729 depth, skiplist->ps_count, 730 TAILQ_FIRST(&skiplist->ps_rules)-> 731 por_rule.nr); 732 } else { 733 newblock = block; 734 } 735 736 while ((por = TAILQ_FIRST(&skiplist->ps_rules))) { 737 TAILQ_REMOVE(&head, por, por_entry); 738 TAILQ_REMOVE(&skiplist->ps_rules, por, 739 por_skip_entry[largest_list]); 740 TAILQ_INSERT_TAIL(&newblock->sb_rules, por, 741 por_entry); 742 743 /* Remove this rule from all other skiplists */ 744 remove_from_skipsteps(&block->sb_skipsteps[ 745 largest_list], block, por, skiplist); 746 } 747 free(skiplist); 748 if (newblock != block) 749 if (reorder_rules(pf, newblock, depth + 1)) 750 return (1); 751 } 752 } 753 754done: 755 for (i = 0; i < PF_SKIP_COUNT; i++) { 756 while ((skiplist = TAILQ_FIRST(&block->sb_skipsteps[i]))) { 757 TAILQ_REMOVE(&block->sb_skipsteps[i], skiplist, 758 ps_entry); 759 free(skiplist); 760 } 761 } 762 763 return (0); 764} 765 766 767/* 768 * Optimization pass #4: re-order 'quick' rules based on feedback from the 769 * currently running ruleset 770 */ 771int 772block_feedback(struct pfctl *pf, struct superblock *block) 773{ 774 TAILQ_HEAD( , pf_opt_rule) queue; 775 struct pf_opt_rule *por1, *por2; 776 u_int64_t total_count = 0; 777 struct pf_rule a, b; 778 779 780 /* 781 * Walk through all of the profiled superblock's rules and copy 782 * the counters onto our rules. 783 */ 784 TAILQ_FOREACH(por1, &block->sb_profiled_block->sb_rules, por_entry) { 785 comparable_rule(&a, &por1->por_rule, DC);
| 420 TAILQ_FIRST(&block->sb_rules)->por_rule.quick && 421 block->sb_profiled_block) { 422 if (block_feedback(pf, block)) 423 return (1); 424 } else if (reorder_rules(pf, block, 0)) { 425 return (1); 426 } 427 428 /* 429 * Don't add any optimization passes below reorder_rules(). It will 430 * have divided superblocks into smaller blocks for further refinement 431 * and doesn't put them back together again. What once was a true 432 * superblock might have been split into multiple superblocks. 433 */ 434 435#ifdef OPT_DEBUG 436 printf("--- END Superblock ---\n"); 437#endif /* OPT_DEBUG */ 438 return (0); 439} 440 441 442/* 443 * Optimization pass #1: remove identical rules 444 */ 445int 446remove_identical_rules(struct pfctl *pf, struct superblock *block) 447{ 448 struct pf_opt_rule *por1, *por2, *por_next, *por2_next; 449 struct pf_rule a, a2, b, b2; 450 451 for (por1 = TAILQ_FIRST(&block->sb_rules); por1; por1 = por_next) { 452 por_next = TAILQ_NEXT(por1, por_entry); 453 for (por2 = por_next; por2; por2 = por2_next) { 454 por2_next = TAILQ_NEXT(por2, por_entry); 455 comparable_rule(&a, &por1->por_rule, DC); 456 comparable_rule(&b, &por2->por_rule, DC); 457 memcpy(&a2, &a, sizeof(a2)); 458 memcpy(&b2, &b, sizeof(b2)); 459 460 exclude_supersets(&a, &b); 461 exclude_supersets(&b2, &a2); 462 if (memcmp(&a, &b, sizeof(a)) == 0) { 463 DEBUG("removing identical rule nr%d = *nr%d*", 464 por1->por_rule.nr, por2->por_rule.nr); 465 TAILQ_REMOVE(&block->sb_rules, por2, por_entry); 466 if (por_next == por2) 467 por_next = TAILQ_NEXT(por1, por_entry); 468 free(por2); 469 } else if (memcmp(&a2, &b2, sizeof(a2)) == 0) { 470 DEBUG("removing identical rule *nr%d* = nr%d", 471 por1->por_rule.nr, por2->por_rule.nr); 472 TAILQ_REMOVE(&block->sb_rules, por1, por_entry); 473 free(por1); 474 break; 475 } 476 } 477 } 478 479 return (0); 480} 481 482 483/* 484 * Optimization pass #2: combine similar rules with different addresses 485 * into a single rule and a table 486 */ 487int 488combine_rules(struct pfctl *pf, struct superblock *block) 489{ 490 struct pf_opt_rule *p1, *p2, *por_next; 491 int src_eq, dst_eq; 492 493 if ((pf->loadopt & PFCTL_FLAG_TABLE) == 0) { 494 warnx("Must enable table loading for optimizations"); 495 return (1); 496 } 497 498 /* First we make a pass to combine the rules. O(n log n) */ 499 TAILQ_FOREACH(p1, &block->sb_rules, por_entry) { 500 for (p2 = TAILQ_NEXT(p1, por_entry); p2; p2 = por_next) { 501 por_next = TAILQ_NEXT(p2, por_entry); 502 503 src_eq = addrs_equal(&p1->por_rule.src, 504 &p2->por_rule.src); 505 dst_eq = addrs_equal(&p1->por_rule.dst, 506 &p2->por_rule.dst); 507 508 if (src_eq && !dst_eq && p1->por_src_tbl == NULL && 509 p2->por_dst_tbl == NULL && 510 p2->por_src_tbl == NULL && 511 rules_combineable(&p1->por_rule, &p2->por_rule) && 512 addrs_combineable(&p1->por_rule.dst, 513 &p2->por_rule.dst)) { 514 DEBUG("can combine rules nr%d = nr%d", 515 p1->por_rule.nr, p2->por_rule.nr); 516 if (p1->por_dst_tbl == NULL && 517 add_opt_table(pf, &p1->por_dst_tbl, 518 p1->por_rule.af, &p1->por_rule.dst)) 519 return (1); 520 if (add_opt_table(pf, &p1->por_dst_tbl, 521 p1->por_rule.af, &p2->por_rule.dst)) 522 return (1); 523 p2->por_dst_tbl = p1->por_dst_tbl; 524 if (p1->por_dst_tbl->pt_rulecount >= 525 TABLE_THRESHOLD) { 526 TAILQ_REMOVE(&block->sb_rules, p2, 527 por_entry); 528 free(p2); 529 } 530 } else if (!src_eq && dst_eq && p1->por_dst_tbl == NULL 531 && p2->por_src_tbl == NULL && 532 p2->por_dst_tbl == NULL && 533 rules_combineable(&p1->por_rule, &p2->por_rule) && 534 addrs_combineable(&p1->por_rule.src, 535 &p2->por_rule.src)) { 536 DEBUG("can combine rules nr%d = nr%d", 537 p1->por_rule.nr, p2->por_rule.nr); 538 if (p1->por_src_tbl == NULL && 539 add_opt_table(pf, &p1->por_src_tbl, 540 p1->por_rule.af, &p1->por_rule.src)) 541 return (1); 542 if (add_opt_table(pf, &p1->por_src_tbl, 543 p1->por_rule.af, &p2->por_rule.src)) 544 return (1); 545 p2->por_src_tbl = p1->por_src_tbl; 546 if (p1->por_src_tbl->pt_rulecount >= 547 TABLE_THRESHOLD) { 548 TAILQ_REMOVE(&block->sb_rules, p2, 549 por_entry); 550 free(p2); 551 } 552 } 553 } 554 } 555 556 557 /* 558 * Then we make a final pass to create a valid table name and 559 * insert the name into the rules. 560 */ 561 for (p1 = TAILQ_FIRST(&block->sb_rules); p1; p1 = por_next) { 562 por_next = TAILQ_NEXT(p1, por_entry); 563 assert(p1->por_src_tbl == NULL || p1->por_dst_tbl == NULL); 564 565 if (p1->por_src_tbl && p1->por_src_tbl->pt_rulecount >= 566 TABLE_THRESHOLD) { 567 if (p1->por_src_tbl->pt_generated) { 568 /* This rule is included in a table */ 569 TAILQ_REMOVE(&block->sb_rules, p1, por_entry); 570 free(p1); 571 continue; 572 } 573 p1->por_src_tbl->pt_generated = 1; 574 575 if ((pf->opts & PF_OPT_NOACTION) == 0 && 576 pf_opt_create_table(pf, p1->por_src_tbl)) 577 return (1); 578 579 pf->tdirty = 1; 580 581 if (pf->opts & PF_OPT_VERBOSE) 582 print_tabledef(p1->por_src_tbl->pt_name, 583 PFR_TFLAG_CONST, 1, 584 &p1->por_src_tbl->pt_nodes); 585 586 memset(&p1->por_rule.src.addr, 0, 587 sizeof(p1->por_rule.src.addr)); 588 p1->por_rule.src.addr.type = PF_ADDR_TABLE; 589 strlcpy(p1->por_rule.src.addr.v.tblname, 590 p1->por_src_tbl->pt_name, 591 sizeof(p1->por_rule.src.addr.v.tblname)); 592 593 pfr_buf_clear(p1->por_src_tbl->pt_buf); 594 free(p1->por_src_tbl->pt_buf); 595 p1->por_src_tbl->pt_buf = NULL; 596 } 597 if (p1->por_dst_tbl && p1->por_dst_tbl->pt_rulecount >= 598 TABLE_THRESHOLD) { 599 if (p1->por_dst_tbl->pt_generated) { 600 /* This rule is included in a table */ 601 TAILQ_REMOVE(&block->sb_rules, p1, por_entry); 602 free(p1); 603 continue; 604 } 605 p1->por_dst_tbl->pt_generated = 1; 606 607 if ((pf->opts & PF_OPT_NOACTION) == 0 && 608 pf_opt_create_table(pf, p1->por_dst_tbl)) 609 return (1); 610 pf->tdirty = 1; 611 612 if (pf->opts & PF_OPT_VERBOSE) 613 print_tabledef(p1->por_dst_tbl->pt_name, 614 PFR_TFLAG_CONST, 1, 615 &p1->por_dst_tbl->pt_nodes); 616 617 memset(&p1->por_rule.dst.addr, 0, 618 sizeof(p1->por_rule.dst.addr)); 619 p1->por_rule.dst.addr.type = PF_ADDR_TABLE; 620 strlcpy(p1->por_rule.dst.addr.v.tblname, 621 p1->por_dst_tbl->pt_name, 622 sizeof(p1->por_rule.dst.addr.v.tblname)); 623 624 pfr_buf_clear(p1->por_dst_tbl->pt_buf); 625 free(p1->por_dst_tbl->pt_buf); 626 p1->por_dst_tbl->pt_buf = NULL; 627 } 628 } 629 630 return (0); 631} 632 633 634/* 635 * Optimization pass #3: re-order rules to improve skip steps 636 */ 637int 638reorder_rules(struct pfctl *pf, struct superblock *block, int depth) 639{ 640 struct superblock *newblock; 641 struct pf_skip_step *skiplist; 642 struct pf_opt_rule *por; 643 int i, largest, largest_list, rule_count = 0; 644 TAILQ_HEAD( , pf_opt_rule) head; 645 646 /* 647 * Calculate the best-case skip steps. We put each rule in a list 648 * of other rules with common fields 649 */ 650 for (i = 0; i < PF_SKIP_COUNT; i++) { 651 TAILQ_FOREACH(por, &block->sb_rules, por_entry) { 652 TAILQ_FOREACH(skiplist, &block->sb_skipsteps[i], 653 ps_entry) { 654 if (skip_compare(i, skiplist, por) == 0) 655 break; 656 } 657 if (skiplist == NULL) { 658 if ((skiplist = calloc(1, sizeof(*skiplist))) == 659 NULL) 660 err(1, "calloc"); 661 TAILQ_INIT(&skiplist->ps_rules); 662 TAILQ_INSERT_TAIL(&block->sb_skipsteps[i], 663 skiplist, ps_entry); 664 } 665 skip_append(block, i, skiplist, por); 666 } 667 } 668 669 TAILQ_FOREACH(por, &block->sb_rules, por_entry) 670 rule_count++; 671 672 /* 673 * Now we're going to ignore any fields that are identical between 674 * all of the rules in the superblock and those fields which differ 675 * between every rule in the superblock. 676 */ 677 largest = 0; 678 for (i = 0; i < PF_SKIP_COUNT; i++) { 679 skiplist = TAILQ_FIRST(&block->sb_skipsteps[i]); 680 if (skiplist->ps_count == rule_count) { 681 DEBUG("(%d) original skipstep '%s' is all rules", 682 depth, skip_comparitors_names[i]); 683 skiplist->ps_count = 0; 684 } else if (skiplist->ps_count == 1) { 685 skiplist->ps_count = 0; 686 } else { 687 DEBUG("(%d) original skipstep '%s' largest jump is %d", 688 depth, skip_comparitors_names[i], 689 skiplist->ps_count); 690 if (skiplist->ps_count > largest) 691 largest = skiplist->ps_count; 692 } 693 } 694 if (largest == 0) { 695 /* Ugh. There is NO commonality in the superblock on which 696 * optimize the skipsteps optimization. 697 */ 698 goto done; 699 } 700 701 /* 702 * Now we're going to empty the superblock rule list and re-create 703 * it based on a more optimal skipstep order. 704 */ 705 TAILQ_INIT(&head); 706 while ((por = TAILQ_FIRST(&block->sb_rules))) { 707 TAILQ_REMOVE(&block->sb_rules, por, por_entry); 708 TAILQ_INSERT_TAIL(&head, por, por_entry); 709 } 710 711 712 while (!TAILQ_EMPTY(&head)) { 713 largest = 1; 714 715 /* 716 * Find the most useful skip steps remaining 717 */ 718 for (i = 0; i < PF_SKIP_COUNT; i++) { 719 skiplist = TAILQ_FIRST(&block->sb_skipsteps[i]); 720 if (skiplist->ps_count > largest) { 721 largest = skiplist->ps_count; 722 largest_list = i; 723 } 724 } 725 726 if (largest <= 1) { 727 /* 728 * Nothing useful left. Leave remaining rules in order. 729 */ 730 DEBUG("(%d) no more commonality for skip steps", depth); 731 while ((por = TAILQ_FIRST(&head))) { 732 TAILQ_REMOVE(&head, por, por_entry); 733 TAILQ_INSERT_TAIL(&block->sb_rules, por, 734 por_entry); 735 } 736 } else { 737 /* 738 * There is commonality. Extract those common rules 739 * and place them in the ruleset adjacent to each 740 * other. 741 */ 742 skiplist = TAILQ_FIRST(&block->sb_skipsteps[ 743 largest_list]); 744 DEBUG("(%d) skipstep '%s' largest jump is %d @ #%d", 745 depth, skip_comparitors_names[largest_list], 746 largest, TAILQ_FIRST(&TAILQ_FIRST(&block-> 747 sb_skipsteps [largest_list])->ps_rules)-> 748 por_rule.nr); 749 TAILQ_REMOVE(&block->sb_skipsteps[largest_list], 750 skiplist, ps_entry); 751 752 753 /* 754 * There may be further commonality inside these 755 * rules. So we'll split them off into they're own 756 * superblock and pass it back into the optimizer. 757 */ 758 if (skiplist->ps_count > 2) { 759 if ((newblock = calloc(1, sizeof(*newblock))) 760 == NULL) { 761 warn("calloc"); 762 return (1); 763 } 764 TAILQ_INIT(&newblock->sb_rules); 765 for (i = 0; i < PF_SKIP_COUNT; i++) 766 TAILQ_INIT(&newblock->sb_skipsteps[i]); 767 TAILQ_INSERT_BEFORE(block, newblock, sb_entry); 768 DEBUG("(%d) splitting off %d rules from superblock @ #%d", 769 depth, skiplist->ps_count, 770 TAILQ_FIRST(&skiplist->ps_rules)-> 771 por_rule.nr); 772 } else { 773 newblock = block; 774 } 775 776 while ((por = TAILQ_FIRST(&skiplist->ps_rules))) { 777 TAILQ_REMOVE(&head, por, por_entry); 778 TAILQ_REMOVE(&skiplist->ps_rules, por, 779 por_skip_entry[largest_list]); 780 TAILQ_INSERT_TAIL(&newblock->sb_rules, por, 781 por_entry); 782 783 /* Remove this rule from all other skiplists */ 784 remove_from_skipsteps(&block->sb_skipsteps[ 785 largest_list], block, por, skiplist); 786 } 787 free(skiplist); 788 if (newblock != block) 789 if (reorder_rules(pf, newblock, depth + 1)) 790 return (1); 791 } 792 } 793 794done: 795 for (i = 0; i < PF_SKIP_COUNT; i++) { 796 while ((skiplist = TAILQ_FIRST(&block->sb_skipsteps[i]))) { 797 TAILQ_REMOVE(&block->sb_skipsteps[i], skiplist, 798 ps_entry); 799 free(skiplist); 800 } 801 } 802 803 return (0); 804} 805 806 807/* 808 * Optimization pass #4: re-order 'quick' rules based on feedback from the 809 * currently running ruleset 810 */ 811int 812block_feedback(struct pfctl *pf, struct superblock *block) 813{ 814 TAILQ_HEAD( , pf_opt_rule) queue; 815 struct pf_opt_rule *por1, *por2; 816 u_int64_t total_count = 0; 817 struct pf_rule a, b; 818 819 820 /* 821 * Walk through all of the profiled superblock's rules and copy 822 * the counters onto our rules. 823 */ 824 TAILQ_FOREACH(por1, &block->sb_profiled_block->sb_rules, por_entry) { 825 comparable_rule(&a, &por1->por_rule, DC);
|
786 total_count += por1->por_rule.packets;
| 826 total_count += por1->por_rule.packets[0] + 827 por1->por_rule.packets[1];
|
787 TAILQ_FOREACH(por2, &block->sb_rules, por_entry) { 788 if (por2->por_profile_count) 789 continue; 790 comparable_rule(&b, &por2->por_rule, DC); 791 if (memcmp(&a, &b, sizeof(a)) == 0) { 792 por2->por_profile_count =
| 828 TAILQ_FOREACH(por2, &block->sb_rules, por_entry) { 829 if (por2->por_profile_count) 830 continue; 831 comparable_rule(&b, &por2->por_rule, DC); 832 if (memcmp(&a, &b, sizeof(a)) == 0) { 833 por2->por_profile_count =
|
793 por1->por_rule.packets;
| 834 por1->por_rule.packets[0] + 835 por1->por_rule.packets[1];
|
794 break; 795 } 796 } 797 } 798 superblock_free(pf, block->sb_profiled_block); 799 block->sb_profiled_block = NULL; 800 801 /* 802 * Now we pull all of the rules off the superblock and re-insert them 803 * in sorted order. 804 */ 805 806 TAILQ_INIT(&queue); 807 while ((por1 = TAILQ_FIRST(&block->sb_rules)) != NULL) { 808 TAILQ_REMOVE(&block->sb_rules, por1, por_entry); 809 TAILQ_INSERT_TAIL(&queue, por1, por_entry); 810 } 811 812 while ((por1 = TAILQ_FIRST(&queue)) != NULL) { 813 TAILQ_REMOVE(&queue, por1, por_entry); 814/* XXX I should sort all of the unused rules based on skip steps */ 815 TAILQ_FOREACH(por2, &block->sb_rules, por_entry) { 816 if (por1->por_profile_count > por2->por_profile_count) { 817 TAILQ_INSERT_BEFORE(por2, por1, por_entry); 818 break; 819 } 820 } 821#ifdef __FreeBSD__ 822 if (por2 == NULL) 823#else 824 if (por2 == TAILQ_END(&block->sb_rules)) 825#endif 826 TAILQ_INSERT_TAIL(&block->sb_rules, por1, por_entry); 827 } 828 829 return (0); 830} 831 832 833/* 834 * Load the current ruleset from the kernel and try to associate them with 835 * the ruleset we're optimizing. 836 */ 837int 838load_feedback_profile(struct pfctl *pf, struct superblocks *superblocks) 839{ 840 struct superblock *block, *blockcur; 841 struct superblocks prof_superblocks; 842 struct pf_opt_rule *por; 843 struct pf_opt_queue queue; 844 struct pfioc_rule pr; 845 struct pf_rule a, b; 846 int nr, mnr; 847 848 TAILQ_INIT(&queue); 849 TAILQ_INIT(&prof_superblocks); 850 851 memset(&pr, 0, sizeof(pr)); 852 pr.rule.action = PF_PASS; 853 if (ioctl(pf->dev, DIOCGETRULES, &pr)) { 854 warn("DIOCGETRULES"); 855 return (1); 856 } 857 mnr = pr.nr; 858 859 DEBUG("Loading %d active rules for a feedback profile", mnr); 860 for (nr = 0; nr < mnr; ++nr) {
| 836 break; 837 } 838 } 839 } 840 superblock_free(pf, block->sb_profiled_block); 841 block->sb_profiled_block = NULL; 842 843 /* 844 * Now we pull all of the rules off the superblock and re-insert them 845 * in sorted order. 846 */ 847 848 TAILQ_INIT(&queue); 849 while ((por1 = TAILQ_FIRST(&block->sb_rules)) != NULL) { 850 TAILQ_REMOVE(&block->sb_rules, por1, por_entry); 851 TAILQ_INSERT_TAIL(&queue, por1, por_entry); 852 } 853 854 while ((por1 = TAILQ_FIRST(&queue)) != NULL) { 855 TAILQ_REMOVE(&queue, por1, por_entry); 856/* XXX I should sort all of the unused rules based on skip steps */ 857 TAILQ_FOREACH(por2, &block->sb_rules, por_entry) { 858 if (por1->por_profile_count > por2->por_profile_count) { 859 TAILQ_INSERT_BEFORE(por2, por1, por_entry); 860 break; 861 } 862 } 863#ifdef __FreeBSD__ 864 if (por2 == NULL) 865#else 866 if (por2 == TAILQ_END(&block->sb_rules)) 867#endif 868 TAILQ_INSERT_TAIL(&block->sb_rules, por1, por_entry); 869 } 870 871 return (0); 872} 873 874 875/* 876 * Load the current ruleset from the kernel and try to associate them with 877 * the ruleset we're optimizing. 878 */ 879int 880load_feedback_profile(struct pfctl *pf, struct superblocks *superblocks) 881{ 882 struct superblock *block, *blockcur; 883 struct superblocks prof_superblocks; 884 struct pf_opt_rule *por; 885 struct pf_opt_queue queue; 886 struct pfioc_rule pr; 887 struct pf_rule a, b; 888 int nr, mnr; 889 890 TAILQ_INIT(&queue); 891 TAILQ_INIT(&prof_superblocks); 892 893 memset(&pr, 0, sizeof(pr)); 894 pr.rule.action = PF_PASS; 895 if (ioctl(pf->dev, DIOCGETRULES, &pr)) { 896 warn("DIOCGETRULES"); 897 return (1); 898 } 899 mnr = pr.nr; 900 901 DEBUG("Loading %d active rules for a feedback profile", mnr); 902 for (nr = 0; nr < mnr; ++nr) {
|
| 903 struct pf_ruleset *rs;
|
861 if ((por = calloc(1, sizeof(*por))) == NULL) { 862 warn("calloc"); 863 return (1); 864 } 865 pr.nr = nr; 866 if (ioctl(pf->dev, DIOCGETRULE, &pr)) { 867 warn("DIOCGETRULES"); 868 return (1); 869 } 870 memcpy(&por->por_rule, &pr.rule, sizeof(por->por_rule));
| 904 if ((por = calloc(1, sizeof(*por))) == NULL) { 905 warn("calloc"); 906 return (1); 907 } 908 pr.nr = nr; 909 if (ioctl(pf->dev, DIOCGETRULE, &pr)) { 910 warn("DIOCGETRULES"); 911 return (1); 912 } 913 memcpy(&por->por_rule, &pr.rule, sizeof(por->por_rule));
|
871 strlcpy(por->por_anchor, pr.anchor_call, 872 sizeof(por->por_anchor));
| 914 rs = pf_find_or_create_ruleset(pr.anchor_call); 915 por->por_rule.anchor = rs->anchor;
|
873 if (TAILQ_EMPTY(&por->por_rule.rpool.list)) 874 memset(&por->por_rule.rpool, 0, 875 sizeof(por->por_rule.rpool)); 876 TAILQ_INSERT_TAIL(&queue, por, por_entry); 877 878 /* XXX pfctl_get_pool(pf->dev, &pr.rule.rpool, nr, pr.ticket, 879 * PF_PASS, pf->anchor) ??? 880 * ... pfctl_clear_pool(&pr.rule.rpool) 881 */ 882 } 883 884 if (construct_superblocks(pf, &queue, &prof_superblocks)) 885 return (1); 886 887 888 /* 889 * Now we try to associate the active ruleset's superblocks with 890 * the superblocks we're compiling. 891 */ 892 block = TAILQ_FIRST(superblocks); 893 blockcur = TAILQ_FIRST(&prof_superblocks); 894 while (block && blockcur) { 895 comparable_rule(&a, &TAILQ_FIRST(&block->sb_rules)->por_rule, 896 BREAK); 897 comparable_rule(&b, &TAILQ_FIRST(&blockcur->sb_rules)->por_rule, 898 BREAK); 899 if (memcmp(&a, &b, sizeof(a)) == 0) { 900 /* The two superblocks lined up */ 901 block->sb_profiled_block = blockcur; 902 } else { 903 DEBUG("superblocks don't line up between #%d and #%d", 904 TAILQ_FIRST(&block->sb_rules)->por_rule.nr, 905 TAILQ_FIRST(&blockcur->sb_rules)->por_rule.nr); 906 break; 907 } 908 block = TAILQ_NEXT(block, sb_entry); 909 blockcur = TAILQ_NEXT(blockcur, sb_entry); 910 } 911 912 913 914 /* Free any superblocks we couldn't link */ 915 while (blockcur) { 916 block = TAILQ_NEXT(blockcur, sb_entry); 917 superblock_free(pf, blockcur); 918 blockcur = block; 919 } 920 return (0); 921} 922 923 924/* 925 * Compare a rule to a skiplist to see if the rule is a member 926 */ 927int 928skip_compare(int skipnum, struct pf_skip_step *skiplist, 929 struct pf_opt_rule *por) 930{ 931 struct pf_rule *a, *b; 932 if (skipnum >= PF_SKIP_COUNT || skipnum < 0) 933 errx(1, "skip_compare() out of bounds"); 934 a = &por->por_rule; 935 b = &TAILQ_FIRST(&skiplist->ps_rules)->por_rule; 936 937 return ((skip_comparitors[skipnum])(a, b)); 938} 939 940 941/* 942 * Add a rule to a skiplist 943 */ 944void 945skip_append(struct superblock *superblock, int skipnum, 946 struct pf_skip_step *skiplist, struct pf_opt_rule *por) 947{ 948 struct pf_skip_step *prev; 949 950 skiplist->ps_count++; 951 TAILQ_INSERT_TAIL(&skiplist->ps_rules, por, por_skip_entry[skipnum]); 952 953 /* Keep the list of skiplists sorted by whichever is larger */ 954 while ((prev = TAILQ_PREV(skiplist, skiplist, ps_entry)) && 955 prev->ps_count < skiplist->ps_count) { 956 TAILQ_REMOVE(&superblock->sb_skipsteps[skipnum], 957 skiplist, ps_entry); 958 TAILQ_INSERT_BEFORE(prev, skiplist, ps_entry); 959 } 960} 961 962 963/* 964 * Remove a rule from the other skiplist calculations. 965 */ 966void 967remove_from_skipsteps(struct skiplist *head, struct superblock *block, 968 struct pf_opt_rule *por, struct pf_skip_step *active_list) 969{ 970 struct pf_skip_step *sk, *next; 971 struct pf_opt_rule *p2; 972 int i, found; 973 974 for (i = 0; i < PF_SKIP_COUNT; i++) { 975 sk = TAILQ_FIRST(&block->sb_skipsteps[i]); 976 if (sk == NULL || sk == active_list || sk->ps_count <= 1) 977 continue; 978 found = 0; 979 do { 980 TAILQ_FOREACH(p2, &sk->ps_rules, por_skip_entry[i]) 981 if (p2 == por) { 982 TAILQ_REMOVE(&sk->ps_rules, p2, 983 por_skip_entry[i]); 984 found = 1; 985 sk->ps_count--; 986 break; 987 } 988 } while (!found && (sk = TAILQ_NEXT(sk, ps_entry))); 989 if (found && sk) { 990 /* Does this change the sorting order? */ 991 while ((next = TAILQ_NEXT(sk, ps_entry)) && 992 next->ps_count > sk->ps_count) { 993 TAILQ_REMOVE(head, sk, ps_entry); 994 TAILQ_INSERT_AFTER(head, next, sk, ps_entry); 995 } 996#ifdef OPT_DEBUG 997 next = TAILQ_NEXT(sk, ps_entry); 998 assert(next == NULL || next->ps_count <= sk->ps_count); 999#endif /* OPT_DEBUG */ 1000 } 1001 } 1002} 1003 1004 1005/* Compare two rules AF field for skiplist construction */ 1006int 1007skip_cmp_af(struct pf_rule *a, struct pf_rule *b) 1008{ 1009 if (a->af != b->af || a->af == 0) 1010 return (1); 1011 return (0); 1012} 1013 1014/* Compare two rules DIRECTION field for skiplist construction */ 1015int 1016skip_cmp_dir(struct pf_rule *a, struct pf_rule *b) 1017{ 1018 if (a->direction == 0 || a->direction != b->direction) 1019 return (1); 1020 return (0); 1021} 1022 1023/* Compare two rules DST Address field for skiplist construction */ 1024int 1025skip_cmp_dst_addr(struct pf_rule *a, struct pf_rule *b) 1026{ 1027 if (a->dst.neg != b->dst.neg || 1028 a->dst.addr.type != b->dst.addr.type) 1029 return (1); 1030 /* XXX if (a->proto != b->proto && a->proto != 0 && b->proto != 0 1031 * && (a->proto == IPPROTO_TCP || a->proto == IPPROTO_UDP || 1032 * a->proto == IPPROTO_ICMP 1033 * return (1); 1034 */ 1035 switch (a->dst.addr.type) { 1036 case PF_ADDR_ADDRMASK: 1037 if (memcmp(&a->dst.addr.v.a.addr, &b->dst.addr.v.a.addr, 1038 sizeof(a->dst.addr.v.a.addr)) || 1039 memcmp(&a->dst.addr.v.a.mask, &b->dst.addr.v.a.mask, 1040 sizeof(a->dst.addr.v.a.mask)) || 1041 (a->dst.addr.v.a.addr.addr32[0] == 0 && 1042 a->dst.addr.v.a.addr.addr32[1] == 0 && 1043 a->dst.addr.v.a.addr.addr32[2] == 0 && 1044 a->dst.addr.v.a.addr.addr32[3] == 0)) 1045 return (1); 1046 return (0); 1047 case PF_ADDR_DYNIFTL: 1048 if (strcmp(a->dst.addr.v.ifname, b->dst.addr.v.ifname) != 0 || 1049 a->dst.addr.iflags != a->dst.addr.iflags || 1050 memcmp(&a->dst.addr.v.a.mask, &b->dst.addr.v.a.mask, 1051 sizeof(a->dst.addr.v.a.mask))) 1052 return (1); 1053 return (0); 1054 case PF_ADDR_NOROUTE:
| 916 if (TAILQ_EMPTY(&por->por_rule.rpool.list)) 917 memset(&por->por_rule.rpool, 0, 918 sizeof(por->por_rule.rpool)); 919 TAILQ_INSERT_TAIL(&queue, por, por_entry); 920 921 /* XXX pfctl_get_pool(pf->dev, &pr.rule.rpool, nr, pr.ticket, 922 * PF_PASS, pf->anchor) ??? 923 * ... pfctl_clear_pool(&pr.rule.rpool) 924 */ 925 } 926 927 if (construct_superblocks(pf, &queue, &prof_superblocks)) 928 return (1); 929 930 931 /* 932 * Now we try to associate the active ruleset's superblocks with 933 * the superblocks we're compiling. 934 */ 935 block = TAILQ_FIRST(superblocks); 936 blockcur = TAILQ_FIRST(&prof_superblocks); 937 while (block && blockcur) { 938 comparable_rule(&a, &TAILQ_FIRST(&block->sb_rules)->por_rule, 939 BREAK); 940 comparable_rule(&b, &TAILQ_FIRST(&blockcur->sb_rules)->por_rule, 941 BREAK); 942 if (memcmp(&a, &b, sizeof(a)) == 0) { 943 /* The two superblocks lined up */ 944 block->sb_profiled_block = blockcur; 945 } else { 946 DEBUG("superblocks don't line up between #%d and #%d", 947 TAILQ_FIRST(&block->sb_rules)->por_rule.nr, 948 TAILQ_FIRST(&blockcur->sb_rules)->por_rule.nr); 949 break; 950 } 951 block = TAILQ_NEXT(block, sb_entry); 952 blockcur = TAILQ_NEXT(blockcur, sb_entry); 953 } 954 955 956 957 /* Free any superblocks we couldn't link */ 958 while (blockcur) { 959 block = TAILQ_NEXT(blockcur, sb_entry); 960 superblock_free(pf, blockcur); 961 blockcur = block; 962 } 963 return (0); 964} 965 966 967/* 968 * Compare a rule to a skiplist to see if the rule is a member 969 */ 970int 971skip_compare(int skipnum, struct pf_skip_step *skiplist, 972 struct pf_opt_rule *por) 973{ 974 struct pf_rule *a, *b; 975 if (skipnum >= PF_SKIP_COUNT || skipnum < 0) 976 errx(1, "skip_compare() out of bounds"); 977 a = &por->por_rule; 978 b = &TAILQ_FIRST(&skiplist->ps_rules)->por_rule; 979 980 return ((skip_comparitors[skipnum])(a, b)); 981} 982 983 984/* 985 * Add a rule to a skiplist 986 */ 987void 988skip_append(struct superblock *superblock, int skipnum, 989 struct pf_skip_step *skiplist, struct pf_opt_rule *por) 990{ 991 struct pf_skip_step *prev; 992 993 skiplist->ps_count++; 994 TAILQ_INSERT_TAIL(&skiplist->ps_rules, por, por_skip_entry[skipnum]); 995 996 /* Keep the list of skiplists sorted by whichever is larger */ 997 while ((prev = TAILQ_PREV(skiplist, skiplist, ps_entry)) && 998 prev->ps_count < skiplist->ps_count) { 999 TAILQ_REMOVE(&superblock->sb_skipsteps[skipnum], 1000 skiplist, ps_entry); 1001 TAILQ_INSERT_BEFORE(prev, skiplist, ps_entry); 1002 } 1003} 1004 1005 1006/* 1007 * Remove a rule from the other skiplist calculations. 1008 */ 1009void 1010remove_from_skipsteps(struct skiplist *head, struct superblock *block, 1011 struct pf_opt_rule *por, struct pf_skip_step *active_list) 1012{ 1013 struct pf_skip_step *sk, *next; 1014 struct pf_opt_rule *p2; 1015 int i, found; 1016 1017 for (i = 0; i < PF_SKIP_COUNT; i++) { 1018 sk = TAILQ_FIRST(&block->sb_skipsteps[i]); 1019 if (sk == NULL || sk == active_list || sk->ps_count <= 1) 1020 continue; 1021 found = 0; 1022 do { 1023 TAILQ_FOREACH(p2, &sk->ps_rules, por_skip_entry[i]) 1024 if (p2 == por) { 1025 TAILQ_REMOVE(&sk->ps_rules, p2, 1026 por_skip_entry[i]); 1027 found = 1; 1028 sk->ps_count--; 1029 break; 1030 } 1031 } while (!found && (sk = TAILQ_NEXT(sk, ps_entry))); 1032 if (found && sk) { 1033 /* Does this change the sorting order? */ 1034 while ((next = TAILQ_NEXT(sk, ps_entry)) && 1035 next->ps_count > sk->ps_count) { 1036 TAILQ_REMOVE(head, sk, ps_entry); 1037 TAILQ_INSERT_AFTER(head, next, sk, ps_entry); 1038 } 1039#ifdef OPT_DEBUG 1040 next = TAILQ_NEXT(sk, ps_entry); 1041 assert(next == NULL || next->ps_count <= sk->ps_count); 1042#endif /* OPT_DEBUG */ 1043 } 1044 } 1045} 1046 1047 1048/* Compare two rules AF field for skiplist construction */ 1049int 1050skip_cmp_af(struct pf_rule *a, struct pf_rule *b) 1051{ 1052 if (a->af != b->af || a->af == 0) 1053 return (1); 1054 return (0); 1055} 1056 1057/* Compare two rules DIRECTION field for skiplist construction */ 1058int 1059skip_cmp_dir(struct pf_rule *a, struct pf_rule *b) 1060{ 1061 if (a->direction == 0 || a->direction != b->direction) 1062 return (1); 1063 return (0); 1064} 1065 1066/* Compare two rules DST Address field for skiplist construction */ 1067int 1068skip_cmp_dst_addr(struct pf_rule *a, struct pf_rule *b) 1069{ 1070 if (a->dst.neg != b->dst.neg || 1071 a->dst.addr.type != b->dst.addr.type) 1072 return (1); 1073 /* XXX if (a->proto != b->proto && a->proto != 0 && b->proto != 0 1074 * && (a->proto == IPPROTO_TCP || a->proto == IPPROTO_UDP || 1075 * a->proto == IPPROTO_ICMP 1076 * return (1); 1077 */ 1078 switch (a->dst.addr.type) { 1079 case PF_ADDR_ADDRMASK: 1080 if (memcmp(&a->dst.addr.v.a.addr, &b->dst.addr.v.a.addr, 1081 sizeof(a->dst.addr.v.a.addr)) || 1082 memcmp(&a->dst.addr.v.a.mask, &b->dst.addr.v.a.mask, 1083 sizeof(a->dst.addr.v.a.mask)) || 1084 (a->dst.addr.v.a.addr.addr32[0] == 0 && 1085 a->dst.addr.v.a.addr.addr32[1] == 0 && 1086 a->dst.addr.v.a.addr.addr32[2] == 0 && 1087 a->dst.addr.v.a.addr.addr32[3] == 0)) 1088 return (1); 1089 return (0); 1090 case PF_ADDR_DYNIFTL: 1091 if (strcmp(a->dst.addr.v.ifname, b->dst.addr.v.ifname) != 0 || 1092 a->dst.addr.iflags != a->dst.addr.iflags || 1093 memcmp(&a->dst.addr.v.a.mask, &b->dst.addr.v.a.mask, 1094 sizeof(a->dst.addr.v.a.mask))) 1095 return (1); 1096 return (0); 1097 case PF_ADDR_NOROUTE:
|
| 1098 case PF_ADDR_URPFFAILED:
|
1055 return (0); 1056 case PF_ADDR_TABLE: 1057 return (strcmp(a->dst.addr.v.tblname, b->dst.addr.v.tblname)); 1058 } 1059 return (1); 1060} 1061 1062/* Compare two rules DST port field for skiplist construction */ 1063int 1064skip_cmp_dst_port(struct pf_rule *a, struct pf_rule *b) 1065{ 1066 /* XXX if (a->proto != b->proto && a->proto != 0 && b->proto != 0 1067 * && (a->proto == IPPROTO_TCP || a->proto == IPPROTO_UDP || 1068 * a->proto == IPPROTO_ICMP 1069 * return (1); 1070 */ 1071 if (a->dst.port_op == PF_OP_NONE || a->dst.port_op != b->dst.port_op || 1072 a->dst.port[0] != b->dst.port[0] || 1073 a->dst.port[1] != b->dst.port[1]) 1074 return (1); 1075 return (0); 1076} 1077 1078/* Compare two rules IFP field for skiplist construction */ 1079int 1080skip_cmp_ifp(struct pf_rule *a, struct pf_rule *b) 1081{ 1082 if (strcmp(a->ifname, b->ifname) || a->ifname[0] == '\0') 1083 return (1); 1084 return (a->ifnot != b->ifnot); 1085} 1086 1087/* Compare two rules PROTO field for skiplist construction */ 1088int 1089skip_cmp_proto(struct pf_rule *a, struct pf_rule *b) 1090{ 1091 return (a->proto != b->proto || a->proto == 0); 1092} 1093 1094/* Compare two rules SRC addr field for skiplist construction */ 1095int 1096skip_cmp_src_addr(struct pf_rule *a, struct pf_rule *b) 1097{ 1098 if (a->src.neg != b->src.neg || 1099 a->src.addr.type != b->src.addr.type) 1100 return (1); 1101 /* XXX if (a->proto != b->proto && a->proto != 0 && b->proto != 0 1102 * && (a->proto == IPPROTO_TCP || a->proto == IPPROTO_UDP || 1103 * a->proto == IPPROTO_ICMP 1104 * return (1); 1105 */ 1106 switch (a->src.addr.type) { 1107 case PF_ADDR_ADDRMASK: 1108 if (memcmp(&a->src.addr.v.a.addr, &b->src.addr.v.a.addr, 1109 sizeof(a->src.addr.v.a.addr)) || 1110 memcmp(&a->src.addr.v.a.mask, &b->src.addr.v.a.mask, 1111 sizeof(a->src.addr.v.a.mask)) || 1112 (a->src.addr.v.a.addr.addr32[0] == 0 && 1113 a->src.addr.v.a.addr.addr32[1] == 0 && 1114 a->src.addr.v.a.addr.addr32[2] == 0 && 1115 a->src.addr.v.a.addr.addr32[3] == 0)) 1116 return (1); 1117 return (0); 1118 case PF_ADDR_DYNIFTL: 1119 if (strcmp(a->src.addr.v.ifname, b->src.addr.v.ifname) != 0 || 1120 a->src.addr.iflags != a->src.addr.iflags || 1121 memcmp(&a->src.addr.v.a.mask, &b->src.addr.v.a.mask, 1122 sizeof(a->src.addr.v.a.mask))) 1123 return (1); 1124 return (0); 1125 case PF_ADDR_NOROUTE:
| 1099 return (0); 1100 case PF_ADDR_TABLE: 1101 return (strcmp(a->dst.addr.v.tblname, b->dst.addr.v.tblname)); 1102 } 1103 return (1); 1104} 1105 1106/* Compare two rules DST port field for skiplist construction */ 1107int 1108skip_cmp_dst_port(struct pf_rule *a, struct pf_rule *b) 1109{ 1110 /* XXX if (a->proto != b->proto && a->proto != 0 && b->proto != 0 1111 * && (a->proto == IPPROTO_TCP || a->proto == IPPROTO_UDP || 1112 * a->proto == IPPROTO_ICMP 1113 * return (1); 1114 */ 1115 if (a->dst.port_op == PF_OP_NONE || a->dst.port_op != b->dst.port_op || 1116 a->dst.port[0] != b->dst.port[0] || 1117 a->dst.port[1] != b->dst.port[1]) 1118 return (1); 1119 return (0); 1120} 1121 1122/* Compare two rules IFP field for skiplist construction */ 1123int 1124skip_cmp_ifp(struct pf_rule *a, struct pf_rule *b) 1125{ 1126 if (strcmp(a->ifname, b->ifname) || a->ifname[0] == '\0') 1127 return (1); 1128 return (a->ifnot != b->ifnot); 1129} 1130 1131/* Compare two rules PROTO field for skiplist construction */ 1132int 1133skip_cmp_proto(struct pf_rule *a, struct pf_rule *b) 1134{ 1135 return (a->proto != b->proto || a->proto == 0); 1136} 1137 1138/* Compare two rules SRC addr field for skiplist construction */ 1139int 1140skip_cmp_src_addr(struct pf_rule *a, struct pf_rule *b) 1141{ 1142 if (a->src.neg != b->src.neg || 1143 a->src.addr.type != b->src.addr.type) 1144 return (1); 1145 /* XXX if (a->proto != b->proto && a->proto != 0 && b->proto != 0 1146 * && (a->proto == IPPROTO_TCP || a->proto == IPPROTO_UDP || 1147 * a->proto == IPPROTO_ICMP 1148 * return (1); 1149 */ 1150 switch (a->src.addr.type) { 1151 case PF_ADDR_ADDRMASK: 1152 if (memcmp(&a->src.addr.v.a.addr, &b->src.addr.v.a.addr, 1153 sizeof(a->src.addr.v.a.addr)) || 1154 memcmp(&a->src.addr.v.a.mask, &b->src.addr.v.a.mask, 1155 sizeof(a->src.addr.v.a.mask)) || 1156 (a->src.addr.v.a.addr.addr32[0] == 0 && 1157 a->src.addr.v.a.addr.addr32[1] == 0 && 1158 a->src.addr.v.a.addr.addr32[2] == 0 && 1159 a->src.addr.v.a.addr.addr32[3] == 0)) 1160 return (1); 1161 return (0); 1162 case PF_ADDR_DYNIFTL: 1163 if (strcmp(a->src.addr.v.ifname, b->src.addr.v.ifname) != 0 || 1164 a->src.addr.iflags != a->src.addr.iflags || 1165 memcmp(&a->src.addr.v.a.mask, &b->src.addr.v.a.mask, 1166 sizeof(a->src.addr.v.a.mask))) 1167 return (1); 1168 return (0); 1169 case PF_ADDR_NOROUTE:
|
| 1170 case PF_ADDR_URPFFAILED:
|
1126 return (0); 1127 case PF_ADDR_TABLE: 1128 return (strcmp(a->src.addr.v.tblname, b->src.addr.v.tblname)); 1129 } 1130 return (1); 1131} 1132 1133/* Compare two rules SRC port field for skiplist construction */ 1134int 1135skip_cmp_src_port(struct pf_rule *a, struct pf_rule *b) 1136{ 1137 if (a->src.port_op == PF_OP_NONE || a->src.port_op != b->src.port_op || 1138 a->src.port[0] != b->src.port[0] || 1139 a->src.port[1] != b->src.port[1]) 1140 return (1); 1141 /* XXX if (a->proto != b->proto && a->proto != 0 && b->proto != 0 1142 * && (a->proto == IPPROTO_TCP || a->proto == IPPROTO_UDP || 1143 * a->proto == IPPROTO_ICMP 1144 * return (1); 1145 */ 1146 return (0); 1147} 1148 1149 1150void 1151skip_init(void) 1152{ 1153 struct { 1154 char *name; 1155 int skipnum; 1156 int (*func)(struct pf_rule *, struct pf_rule *); 1157 } comps[] = PF_SKIP_COMPARITORS; 1158 int skipnum, i; 1159 1160 for (skipnum = 0; skipnum < PF_SKIP_COUNT; skipnum++) { 1161 for (i = 0; i < sizeof(comps)/sizeof(*comps); i++) 1162 if (comps[i].skipnum == skipnum) { 1163 skip_comparitors[skipnum] = comps[i].func; 1164 skip_comparitors_names[skipnum] = comps[i].name; 1165 } 1166 } 1167 for (skipnum = 0; skipnum < PF_SKIP_COUNT; skipnum++) 1168 if (skip_comparitors[skipnum] == NULL) 1169 errx(1, "Need to add skip step comparitor to pfctl?!"); 1170} 1171 1172/* 1173 * Add a host/netmask to a table 1174 */ 1175int 1176add_opt_table(struct pfctl *pf, struct pf_opt_tbl **tbl, sa_family_t af, 1177 struct pf_rule_addr *addr) 1178{ 1179#ifdef OPT_DEBUG 1180 char buf[128]; 1181#endif /* OPT_DEBUG */ 1182 static int tablenum = 0; 1183 struct node_host node_host; 1184 1185 if (*tbl == NULL) { 1186 if ((*tbl = calloc(1, sizeof(**tbl))) == NULL || 1187 ((*tbl)->pt_buf = calloc(1, sizeof(*(*tbl)->pt_buf))) == 1188 NULL) 1189 err(1, "calloc"); 1190 (*tbl)->pt_buf->pfrb_type = PFRB_ADDRS; 1191 SIMPLEQ_INIT(&(*tbl)->pt_nodes); 1192 1193 /* This is just a temporary table name */ 1194 snprintf((*tbl)->pt_name, sizeof((*tbl)->pt_name), "%s%d", 1195 PF_OPT_TABLE_PREFIX, tablenum++); 1196 DEBUG("creating table <%s>", (*tbl)->pt_name); 1197 } 1198 1199 memset(&node_host, 0, sizeof(node_host)); 1200 node_host.af = af; 1201 node_host.addr = addr->addr; 1202 1203#ifdef OPT_DEBUG 1204 DEBUG("<%s> adding %s/%d", (*tbl)->pt_name, inet_ntop(af, 1205 &node_host.addr.v.a.addr, buf, sizeof(buf)), 1206 unmask(&node_host.addr.v.a.mask, af)); 1207#endif /* OPT_DEBUG */ 1208 1209 if (append_addr_host((*tbl)->pt_buf, &node_host, 0, 0)) { 1210 warn("failed to add host"); 1211 return (1); 1212 } 1213 if (pf->opts & PF_OPT_VERBOSE) { 1214 struct node_tinit *ti; 1215 1216 if ((ti = calloc(1, sizeof(*ti))) == NULL) 1217 err(1, "malloc"); 1218 if ((ti->host = malloc(sizeof(*ti->host))) == NULL) 1219 err(1, "malloc"); 1220 memcpy(ti->host, &node_host, sizeof(*ti->host)); 1221 SIMPLEQ_INSERT_TAIL(&(*tbl)->pt_nodes, ti, entries); 1222 } 1223 1224 (*tbl)->pt_rulecount++; 1225 if ((*tbl)->pt_rulecount == TABLE_THRESHOLD) 1226 DEBUG("table <%s> now faster than skip steps", (*tbl)->pt_name); 1227 1228 return (0); 1229} 1230 1231 1232/* 1233 * Do the dirty work of choosing an unused table name and creating it. 1234 * (be careful with the table name, it might already be used in another anchor) 1235 */ 1236int 1237pf_opt_create_table(struct pfctl *pf, struct pf_opt_tbl *tbl) 1238{ 1239 static int tablenum; 1240 struct pfr_table *t; 1241 1242 if (table_buffer.pfrb_type == 0) { 1243 /* Initialize the list of tables */ 1244 table_buffer.pfrb_type = PFRB_TABLES; 1245 for (;;) { 1246 pfr_buf_grow(&table_buffer, table_buffer.pfrb_size); 1247 table_buffer.pfrb_size = table_buffer.pfrb_msize; 1248 if (pfr_get_tables(NULL, table_buffer.pfrb_caddr, 1249 &table_buffer.pfrb_size, PFR_FLAG_ALLRSETS)) 1250 err(1, "pfr_get_tables"); 1251 if (table_buffer.pfrb_size <= table_buffer.pfrb_msize) 1252 break; 1253 } 1254 table_identifier = arc4random(); 1255 } 1256 1257 /* XXX would be *really* nice to avoid duplicating identical tables */ 1258 1259 /* Now we have to pick a table name that isn't used */ 1260again: 1261 DEBUG("translating temporary table <%s> to <%s%x_%d>", tbl->pt_name, 1262 PF_OPT_TABLE_PREFIX, table_identifier, tablenum); 1263 snprintf(tbl->pt_name, sizeof(tbl->pt_name), "%s%x_%d", 1264 PF_OPT_TABLE_PREFIX, table_identifier, tablenum); 1265 PFRB_FOREACH(t, &table_buffer) { 1266 if (strcasecmp(t->pfrt_name, tbl->pt_name) == 0) { 1267 /* Collision. Try again */ 1268 DEBUG("wow, table <%s> in use. trying again", 1269 tbl->pt_name); 1270 table_identifier = arc4random(); 1271 goto again; 1272 } 1273 } 1274 tablenum++; 1275 1276
| 1171 return (0); 1172 case PF_ADDR_TABLE: 1173 return (strcmp(a->src.addr.v.tblname, b->src.addr.v.tblname)); 1174 } 1175 return (1); 1176} 1177 1178/* Compare two rules SRC port field for skiplist construction */ 1179int 1180skip_cmp_src_port(struct pf_rule *a, struct pf_rule *b) 1181{ 1182 if (a->src.port_op == PF_OP_NONE || a->src.port_op != b->src.port_op || 1183 a->src.port[0] != b->src.port[0] || 1184 a->src.port[1] != b->src.port[1]) 1185 return (1); 1186 /* XXX if (a->proto != b->proto && a->proto != 0 && b->proto != 0 1187 * && (a->proto == IPPROTO_TCP || a->proto == IPPROTO_UDP || 1188 * a->proto == IPPROTO_ICMP 1189 * return (1); 1190 */ 1191 return (0); 1192} 1193 1194 1195void 1196skip_init(void) 1197{ 1198 struct { 1199 char *name; 1200 int skipnum; 1201 int (*func)(struct pf_rule *, struct pf_rule *); 1202 } comps[] = PF_SKIP_COMPARITORS; 1203 int skipnum, i; 1204 1205 for (skipnum = 0; skipnum < PF_SKIP_COUNT; skipnum++) { 1206 for (i = 0; i < sizeof(comps)/sizeof(*comps); i++) 1207 if (comps[i].skipnum == skipnum) { 1208 skip_comparitors[skipnum] = comps[i].func; 1209 skip_comparitors_names[skipnum] = comps[i].name; 1210 } 1211 } 1212 for (skipnum = 0; skipnum < PF_SKIP_COUNT; skipnum++) 1213 if (skip_comparitors[skipnum] == NULL) 1214 errx(1, "Need to add skip step comparitor to pfctl?!"); 1215} 1216 1217/* 1218 * Add a host/netmask to a table 1219 */ 1220int 1221add_opt_table(struct pfctl *pf, struct pf_opt_tbl **tbl, sa_family_t af, 1222 struct pf_rule_addr *addr) 1223{ 1224#ifdef OPT_DEBUG 1225 char buf[128]; 1226#endif /* OPT_DEBUG */ 1227 static int tablenum = 0; 1228 struct node_host node_host; 1229 1230 if (*tbl == NULL) { 1231 if ((*tbl = calloc(1, sizeof(**tbl))) == NULL || 1232 ((*tbl)->pt_buf = calloc(1, sizeof(*(*tbl)->pt_buf))) == 1233 NULL) 1234 err(1, "calloc"); 1235 (*tbl)->pt_buf->pfrb_type = PFRB_ADDRS; 1236 SIMPLEQ_INIT(&(*tbl)->pt_nodes); 1237 1238 /* This is just a temporary table name */ 1239 snprintf((*tbl)->pt_name, sizeof((*tbl)->pt_name), "%s%d", 1240 PF_OPT_TABLE_PREFIX, tablenum++); 1241 DEBUG("creating table <%s>", (*tbl)->pt_name); 1242 } 1243 1244 memset(&node_host, 0, sizeof(node_host)); 1245 node_host.af = af; 1246 node_host.addr = addr->addr; 1247 1248#ifdef OPT_DEBUG 1249 DEBUG("<%s> adding %s/%d", (*tbl)->pt_name, inet_ntop(af, 1250 &node_host.addr.v.a.addr, buf, sizeof(buf)), 1251 unmask(&node_host.addr.v.a.mask, af)); 1252#endif /* OPT_DEBUG */ 1253 1254 if (append_addr_host((*tbl)->pt_buf, &node_host, 0, 0)) { 1255 warn("failed to add host"); 1256 return (1); 1257 } 1258 if (pf->opts & PF_OPT_VERBOSE) { 1259 struct node_tinit *ti; 1260 1261 if ((ti = calloc(1, sizeof(*ti))) == NULL) 1262 err(1, "malloc"); 1263 if ((ti->host = malloc(sizeof(*ti->host))) == NULL) 1264 err(1, "malloc"); 1265 memcpy(ti->host, &node_host, sizeof(*ti->host)); 1266 SIMPLEQ_INSERT_TAIL(&(*tbl)->pt_nodes, ti, entries); 1267 } 1268 1269 (*tbl)->pt_rulecount++; 1270 if ((*tbl)->pt_rulecount == TABLE_THRESHOLD) 1271 DEBUG("table <%s> now faster than skip steps", (*tbl)->pt_name); 1272 1273 return (0); 1274} 1275 1276 1277/* 1278 * Do the dirty work of choosing an unused table name and creating it. 1279 * (be careful with the table name, it might already be used in another anchor) 1280 */ 1281int 1282pf_opt_create_table(struct pfctl *pf, struct pf_opt_tbl *tbl) 1283{ 1284 static int tablenum; 1285 struct pfr_table *t; 1286 1287 if (table_buffer.pfrb_type == 0) { 1288 /* Initialize the list of tables */ 1289 table_buffer.pfrb_type = PFRB_TABLES; 1290 for (;;) { 1291 pfr_buf_grow(&table_buffer, table_buffer.pfrb_size); 1292 table_buffer.pfrb_size = table_buffer.pfrb_msize; 1293 if (pfr_get_tables(NULL, table_buffer.pfrb_caddr, 1294 &table_buffer.pfrb_size, PFR_FLAG_ALLRSETS)) 1295 err(1, "pfr_get_tables"); 1296 if (table_buffer.pfrb_size <= table_buffer.pfrb_msize) 1297 break; 1298 } 1299 table_identifier = arc4random(); 1300 } 1301 1302 /* XXX would be *really* nice to avoid duplicating identical tables */ 1303 1304 /* Now we have to pick a table name that isn't used */ 1305again: 1306 DEBUG("translating temporary table <%s> to <%s%x_%d>", tbl->pt_name, 1307 PF_OPT_TABLE_PREFIX, table_identifier, tablenum); 1308 snprintf(tbl->pt_name, sizeof(tbl->pt_name), "%s%x_%d", 1309 PF_OPT_TABLE_PREFIX, table_identifier, tablenum); 1310 PFRB_FOREACH(t, &table_buffer) { 1311 if (strcasecmp(t->pfrt_name, tbl->pt_name) == 0) { 1312 /* Collision. Try again */ 1313 DEBUG("wow, table <%s> in use. trying again", 1314 tbl->pt_name); 1315 table_identifier = arc4random(); 1316 goto again; 1317 } 1318 } 1319 tablenum++; 1320 1321
|
1277 if (pfctl_define_table(tbl->pt_name, PFR_TFLAG_CONST, 1, pf->anchor, 1278 tbl->pt_buf, pf->tticket)) {
| 1322 if (pfctl_define_table(tbl->pt_name, PFR_TFLAG_CONST, 1, 1323 pf->anchor->name, tbl->pt_buf, pf->anchor->ruleset.tticket)) {
|
1279 warn("failed to create table %s", tbl->pt_name); 1280 return (1); 1281 } 1282 return (0); 1283} 1284 1285/* 1286 * Partition the flat ruleset into a list of distinct superblocks 1287 */ 1288int 1289construct_superblocks(struct pfctl *pf, struct pf_opt_queue *opt_queue, 1290 struct superblocks *superblocks) 1291{ 1292 struct superblock *block = NULL; 1293 struct pf_opt_rule *por; 1294 int i; 1295 1296 while (!TAILQ_EMPTY(opt_queue)) { 1297 por = TAILQ_FIRST(opt_queue); 1298 TAILQ_REMOVE(opt_queue, por, por_entry); 1299 if (block == NULL || !superblock_inclusive(block, por)) { 1300 if ((block = calloc(1, sizeof(*block))) == NULL) { 1301 warn("calloc"); 1302 return (1); 1303 } 1304 TAILQ_INIT(&block->sb_rules); 1305 for (i = 0; i < PF_SKIP_COUNT; i++) 1306 TAILQ_INIT(&block->sb_skipsteps[i]); 1307 TAILQ_INSERT_TAIL(superblocks, block, sb_entry); 1308 } 1309 TAILQ_INSERT_TAIL(&block->sb_rules, por, por_entry); 1310 } 1311 1312 return (0); 1313} 1314 1315 1316/* 1317 * Compare two rule addresses 1318 */ 1319int 1320addrs_equal(struct pf_rule_addr *a, struct pf_rule_addr *b) 1321{ 1322 if (a->neg != b->neg) 1323 return (0); 1324 return (memcmp(&a->addr, &b->addr, sizeof(a->addr)) == 0); 1325} 1326 1327 1328/* 1329 * The addresses are not equal, but can we combine them into one table? 1330 */ 1331int 1332addrs_combineable(struct pf_rule_addr *a, struct pf_rule_addr *b) 1333{ 1334 if (a->addr.type != PF_ADDR_ADDRMASK || 1335 b->addr.type != PF_ADDR_ADDRMASK) 1336 return (0); 1337 if (a->neg != b->neg || a->port_op != b->port_op || 1338 a->port[0] != b->port[0] || a->port[1] != b->port[1]) 1339 return (0); 1340 return (1); 1341} 1342 1343 1344/* 1345 * Are we allowed to combine these two rules 1346 */ 1347int 1348rules_combineable(struct pf_rule *p1, struct pf_rule *p2) 1349{ 1350 struct pf_rule a, b; 1351 1352 comparable_rule(&a, p1, COMBINED); 1353 comparable_rule(&b, p2, COMBINED); 1354 return (memcmp(&a, &b, sizeof(a)) == 0); 1355} 1356 1357 1358/* 1359 * Can a rule be included inside a superblock 1360 */ 1361int 1362superblock_inclusive(struct superblock *block, struct pf_opt_rule *por) 1363{ 1364 struct pf_rule a, b; 1365 int i, j; 1366 1367 /* First check for hard breaks */ 1368 for (i = 0; i < sizeof(pf_rule_desc)/sizeof(*pf_rule_desc); i++) { 1369 if (pf_rule_desc[i].prf_type == BARRIER) { 1370 for (j = 0; j < pf_rule_desc[i].prf_size; j++) 1371 if (((char *)&por->por_rule)[j + 1372 pf_rule_desc[i].prf_offset] != 0) 1373 return (0); 1374 } 1375 } 1376
| 1324 warn("failed to create table %s", tbl->pt_name); 1325 return (1); 1326 } 1327 return (0); 1328} 1329 1330/* 1331 * Partition the flat ruleset into a list of distinct superblocks 1332 */ 1333int 1334construct_superblocks(struct pfctl *pf, struct pf_opt_queue *opt_queue, 1335 struct superblocks *superblocks) 1336{ 1337 struct superblock *block = NULL; 1338 struct pf_opt_rule *por; 1339 int i; 1340 1341 while (!TAILQ_EMPTY(opt_queue)) { 1342 por = TAILQ_FIRST(opt_queue); 1343 TAILQ_REMOVE(opt_queue, por, por_entry); 1344 if (block == NULL || !superblock_inclusive(block, por)) { 1345 if ((block = calloc(1, sizeof(*block))) == NULL) { 1346 warn("calloc"); 1347 return (1); 1348 } 1349 TAILQ_INIT(&block->sb_rules); 1350 for (i = 0; i < PF_SKIP_COUNT; i++) 1351 TAILQ_INIT(&block->sb_skipsteps[i]); 1352 TAILQ_INSERT_TAIL(superblocks, block, sb_entry); 1353 } 1354 TAILQ_INSERT_TAIL(&block->sb_rules, por, por_entry); 1355 } 1356 1357 return (0); 1358} 1359 1360 1361/* 1362 * Compare two rule addresses 1363 */ 1364int 1365addrs_equal(struct pf_rule_addr *a, struct pf_rule_addr *b) 1366{ 1367 if (a->neg != b->neg) 1368 return (0); 1369 return (memcmp(&a->addr, &b->addr, sizeof(a->addr)) == 0); 1370} 1371 1372 1373/* 1374 * The addresses are not equal, but can we combine them into one table? 1375 */ 1376int 1377addrs_combineable(struct pf_rule_addr *a, struct pf_rule_addr *b) 1378{ 1379 if (a->addr.type != PF_ADDR_ADDRMASK || 1380 b->addr.type != PF_ADDR_ADDRMASK) 1381 return (0); 1382 if (a->neg != b->neg || a->port_op != b->port_op || 1383 a->port[0] != b->port[0] || a->port[1] != b->port[1]) 1384 return (0); 1385 return (1); 1386} 1387 1388 1389/* 1390 * Are we allowed to combine these two rules 1391 */ 1392int 1393rules_combineable(struct pf_rule *p1, struct pf_rule *p2) 1394{ 1395 struct pf_rule a, b; 1396 1397 comparable_rule(&a, p1, COMBINED); 1398 comparable_rule(&b, p2, COMBINED); 1399 return (memcmp(&a, &b, sizeof(a)) == 0); 1400} 1401 1402 1403/* 1404 * Can a rule be included inside a superblock 1405 */ 1406int 1407superblock_inclusive(struct superblock *block, struct pf_opt_rule *por) 1408{ 1409 struct pf_rule a, b; 1410 int i, j; 1411 1412 /* First check for hard breaks */ 1413 for (i = 0; i < sizeof(pf_rule_desc)/sizeof(*pf_rule_desc); i++) { 1414 if (pf_rule_desc[i].prf_type == BARRIER) { 1415 for (j = 0; j < pf_rule_desc[i].prf_size; j++) 1416 if (((char *)&por->por_rule)[j + 1417 pf_rule_desc[i].prf_offset] != 0) 1418 return (0); 1419 } 1420 } 1421
|
1377 /* 'anchor' heads and per-rule src-track are also hard breaks */ 1378 if (por->por_anchor[0] != '\0' || 1379 (por->por_rule.rule_flag & PFRULE_RULESRCTRACK))
| 1422 /* per-rule src-track is also a hard break */ 1423 if (por->por_rule.rule_flag & PFRULE_RULESRCTRACK)
|
1380 return (0); 1381
| 1424 return (0); 1425
|
| 1426 /* 1427 * Have to handle interface groups seperately. Consider the following 1428 * rules: 1429 * block on EXTIFS to any port 22 1430 * pass on em0 to any port 22 1431 * (where EXTIFS is an arbitrary interface group) 1432 * The optimizer may decide to re-order the pass rule in front of the 1433 * block rule. But what if EXTIFS includes em0??? Such a reordering 1434 * would change the meaning of the ruleset. 1435 * We can't just lookup the EXTIFS group and check if em0 is a member 1436 * because the user is allowed to add interfaces to a group during 1437 * runtime. 1438 * Ergo interface groups become a defacto superblock break :-( 1439 */ 1440 if (interface_group(por->por_rule.ifname) || 1441 interface_group(TAILQ_FIRST(&block->sb_rules)->por_rule.ifname)) { 1442 if (strcasecmp(por->por_rule.ifname, 1443 TAILQ_FIRST(&block->sb_rules)->por_rule.ifname) != 0) 1444 return (0); 1445 } 1446
|
1382 comparable_rule(&a, &TAILQ_FIRST(&block->sb_rules)->por_rule, NOMERGE); 1383 comparable_rule(&b, &por->por_rule, NOMERGE);
| 1447 comparable_rule(&a, &TAILQ_FIRST(&block->sb_rules)->por_rule, NOMERGE); 1448 comparable_rule(&b, &por->por_rule, NOMERGE);
|
1384 if (strcmp(TAILQ_FIRST(&block->sb_rules)->por_anchor, 1385 por->por_anchor) == 0 && memcmp(&a, &b, sizeof(a)) == 0)
| 1449 if (memcmp(&a, &b, sizeof(a)) == 0)
|
1386 return (1); 1387 1388#ifdef OPT_DEBUG 1389 for (i = 0; i < sizeof(por->por_rule); i++) { 1390 int closest = -1; 1391 if (((u_int8_t *)&a)[i] != ((u_int8_t *)&b)[i]) { 1392 for (j = 0; j < sizeof(pf_rule_desc) / 1393 sizeof(*pf_rule_desc); j++) { 1394 if (i >= pf_rule_desc[j].prf_offset && 1395 i < pf_rule_desc[j].prf_offset + 1396 pf_rule_desc[j].prf_size) { 1397 DEBUG("superblock break @ %d due to %s", 1398 por->por_rule.nr, 1399 pf_rule_desc[j].prf_name); 1400 return (0); 1401 } 1402 if (i > pf_rule_desc[j].prf_offset) { 1403 if (closest == -1 || 1404 i-pf_rule_desc[j].prf_offset < 1405 i-pf_rule_desc[closest].prf_offset) 1406 closest = j; 1407 } 1408 } 1409 1410 if (closest >= 0) 1411 DEBUG("superblock break @ %d on %s+%xh", 1412 por->por_rule.nr, 1413 pf_rule_desc[closest].prf_name, 1414 i - pf_rule_desc[closest].prf_offset - 1415 pf_rule_desc[closest].prf_size); 1416 else 1417 DEBUG("superblock break @ %d on field @ %d", 1418 por->por_rule.nr, i); 1419 return (0); 1420 } 1421 } 1422#endif /* OPT_DEBUG */ 1423 1424 return (0); 1425} 1426 1427 1428/*
| 1450 return (1); 1451 1452#ifdef OPT_DEBUG 1453 for (i = 0; i < sizeof(por->por_rule); i++) { 1454 int closest = -1; 1455 if (((u_int8_t *)&a)[i] != ((u_int8_t *)&b)[i]) { 1456 for (j = 0; j < sizeof(pf_rule_desc) / 1457 sizeof(*pf_rule_desc); j++) { 1458 if (i >= pf_rule_desc[j].prf_offset && 1459 i < pf_rule_desc[j].prf_offset + 1460 pf_rule_desc[j].prf_size) { 1461 DEBUG("superblock break @ %d due to %s", 1462 por->por_rule.nr, 1463 pf_rule_desc[j].prf_name); 1464 return (0); 1465 } 1466 if (i > pf_rule_desc[j].prf_offset) { 1467 if (closest == -1 || 1468 i-pf_rule_desc[j].prf_offset < 1469 i-pf_rule_desc[closest].prf_offset) 1470 closest = j; 1471 } 1472 } 1473 1474 if (closest >= 0) 1475 DEBUG("superblock break @ %d on %s+%xh", 1476 por->por_rule.nr, 1477 pf_rule_desc[closest].prf_name, 1478 i - pf_rule_desc[closest].prf_offset - 1479 pf_rule_desc[closest].prf_size); 1480 else 1481 DEBUG("superblock break @ %d on field @ %d", 1482 por->por_rule.nr, i); 1483 return (0); 1484 } 1485 } 1486#endif /* OPT_DEBUG */ 1487 1488 return (0); 1489} 1490 1491 1492/*
|
| 1493 * Figure out if an interface name is an actual interface or actually a 1494 * group of interfaces. 1495 */ 1496int 1497interface_group(const char *ifname) 1498{ 1499 if (ifname == NULL || !ifname[0]) 1500 return (0); 1501 1502 /* Real interfaces must end in a number, interface groups do not */ 1503 if (isdigit(ifname[strlen(ifname) - 1])) 1504 return (0); 1505 else 1506 return (1); 1507} 1508 1509 1510/*
|
1429 * Make a rule that can directly compared by memcmp() 1430 */ 1431void 1432comparable_rule(struct pf_rule *dst, const struct pf_rule *src, int type) 1433{ 1434 int i; 1435 /* 1436 * To simplify the comparison, we just zero out the fields that are 1437 * allowed to be different and then do a simple memcmp() 1438 */ 1439 memcpy(dst, src, sizeof(*dst)); 1440 for (i = 0; i < sizeof(pf_rule_desc)/sizeof(*pf_rule_desc); i++) 1441 if (pf_rule_desc[i].prf_type >= type) { 1442#ifdef OPT_DEBUG 1443 assert(pf_rule_desc[i].prf_type != NEVER || 1444 *(((char *)dst) + pf_rule_desc[i].prf_offset) == 0); 1445#endif /* OPT_DEBUG */ 1446 memset(((char *)dst) + pf_rule_desc[i].prf_offset, 0, 1447 pf_rule_desc[i].prf_size); 1448 } 1449} 1450 1451 1452/* 1453 * Remove superset information from two rules so we can directly compare them 1454 * with memcmp() 1455 */ 1456void 1457exclude_supersets(struct pf_rule *super, struct pf_rule *sub) 1458{ 1459 if (super->ifname[0] == '\0') 1460 memset(sub->ifname, 0, sizeof(sub->ifname)); 1461 if (super->direction == PF_INOUT) 1462 sub->direction = PF_INOUT; 1463 if ((super->proto == 0 || super->proto == sub->proto) && 1464 super->flags == 0 && super->flagset == 0 && (sub->flags || 1465 sub->flagset)) { 1466 sub->flags = super->flags; 1467 sub->flagset = super->flagset; 1468 } 1469 if (super->proto == 0) 1470 sub->proto = 0; 1471 1472 if (super->src.port_op == 0) { 1473 sub->src.port_op = 0; 1474 sub->src.port[0] = 0; 1475 sub->src.port[1] = 0; 1476 } 1477 if (super->dst.port_op == 0) { 1478 sub->dst.port_op = 0; 1479 sub->dst.port[0] = 0; 1480 sub->dst.port[1] = 0; 1481 } 1482 1483 if (super->src.addr.type == PF_ADDR_ADDRMASK && !super->src.neg && 1484 !sub->src.neg && super->src.addr.v.a.mask.addr32[0] == 0 && 1485 super->src.addr.v.a.mask.addr32[1] == 0 && 1486 super->src.addr.v.a.mask.addr32[2] == 0 && 1487 super->src.addr.v.a.mask.addr32[3] == 0) 1488 memset(&sub->src.addr, 0, sizeof(sub->src.addr)); 1489 else if (super->src.addr.type == PF_ADDR_ADDRMASK && 1490 sub->src.addr.type == PF_ADDR_ADDRMASK && 1491 super->src.neg == sub->src.neg && 1492 super->af == sub->af && 1493 unmask(&super->src.addr.v.a.mask, super->af) < 1494 unmask(&sub->src.addr.v.a.mask, sub->af) && 1495 super->src.addr.v.a.addr.addr32[0] == 1496 (sub->src.addr.v.a.addr.addr32[0] & 1497 super->src.addr.v.a.mask.addr32[0]) && 1498 super->src.addr.v.a.addr.addr32[1] == 1499 (sub->src.addr.v.a.addr.addr32[1] & 1500 super->src.addr.v.a.mask.addr32[1]) && 1501 super->src.addr.v.a.addr.addr32[2] == 1502 (sub->src.addr.v.a.addr.addr32[2] & 1503 super->src.addr.v.a.mask.addr32[2]) && 1504 super->src.addr.v.a.addr.addr32[3] == 1505 (sub->src.addr.v.a.addr.addr32[3] & 1506 super->src.addr.v.a.mask.addr32[3])) { 1507 /* sub->src.addr is a subset of super->src.addr/mask */ 1508 memcpy(&sub->src.addr, &super->src.addr, sizeof(sub->src.addr)); 1509 } 1510 1511 if (super->dst.addr.type == PF_ADDR_ADDRMASK && !super->dst.neg && 1512 !sub->dst.neg && super->dst.addr.v.a.mask.addr32[0] == 0 && 1513 super->dst.addr.v.a.mask.addr32[1] == 0 && 1514 super->dst.addr.v.a.mask.addr32[2] == 0 && 1515 super->dst.addr.v.a.mask.addr32[3] == 0) 1516 memset(&sub->dst.addr, 0, sizeof(sub->dst.addr)); 1517 else if (super->dst.addr.type == PF_ADDR_ADDRMASK && 1518 sub->dst.addr.type == PF_ADDR_ADDRMASK && 1519 super->dst.neg == sub->dst.neg && 1520 super->af == sub->af && 1521 unmask(&super->dst.addr.v.a.mask, super->af) < 1522 unmask(&sub->dst.addr.v.a.mask, sub->af) && 1523 super->dst.addr.v.a.addr.addr32[0] == 1524 (sub->dst.addr.v.a.addr.addr32[0] & 1525 super->dst.addr.v.a.mask.addr32[0]) && 1526 super->dst.addr.v.a.addr.addr32[1] == 1527 (sub->dst.addr.v.a.addr.addr32[1] & 1528 super->dst.addr.v.a.mask.addr32[1]) && 1529 super->dst.addr.v.a.addr.addr32[2] == 1530 (sub->dst.addr.v.a.addr.addr32[2] & 1531 super->dst.addr.v.a.mask.addr32[2]) && 1532 super->dst.addr.v.a.addr.addr32[3] == 1533 (sub->dst.addr.v.a.addr.addr32[3] & 1534 super->dst.addr.v.a.mask.addr32[3])) { 1535 /* sub->dst.addr is a subset of super->dst.addr/mask */ 1536 memcpy(&sub->dst.addr, &super->dst.addr, sizeof(sub->dst.addr)); 1537 } 1538 1539 if (super->af == 0) 1540 sub->af = 0; 1541} 1542 1543 1544void 1545superblock_free(struct pfctl *pf, struct superblock *block) 1546{ 1547 struct pf_opt_rule *por; 1548 while ((por = TAILQ_FIRST(&block->sb_rules))) { 1549 TAILQ_REMOVE(&block->sb_rules, por, por_entry); 1550 if (por->por_src_tbl) { 1551 if (por->por_src_tbl->pt_buf) { 1552 pfr_buf_clear(por->por_src_tbl->pt_buf); 1553 free(por->por_src_tbl->pt_buf); 1554 } 1555 free(por->por_src_tbl); 1556 } 1557 if (por->por_dst_tbl) { 1558 if (por->por_dst_tbl->pt_buf) { 1559 pfr_buf_clear(por->por_dst_tbl->pt_buf); 1560 free(por->por_dst_tbl->pt_buf); 1561 } 1562 free(por->por_dst_tbl); 1563 } 1564 free(por); 1565 } 1566 if (block->sb_profiled_block) 1567 superblock_free(pf, block->sb_profiled_block); 1568 free(block); 1569} 1570
| 1511 * Make a rule that can directly compared by memcmp() 1512 */ 1513void 1514comparable_rule(struct pf_rule *dst, const struct pf_rule *src, int type) 1515{ 1516 int i; 1517 /* 1518 * To simplify the comparison, we just zero out the fields that are 1519 * allowed to be different and then do a simple memcmp() 1520 */ 1521 memcpy(dst, src, sizeof(*dst)); 1522 for (i = 0; i < sizeof(pf_rule_desc)/sizeof(*pf_rule_desc); i++) 1523 if (pf_rule_desc[i].prf_type >= type) { 1524#ifdef OPT_DEBUG 1525 assert(pf_rule_desc[i].prf_type != NEVER || 1526 *(((char *)dst) + pf_rule_desc[i].prf_offset) == 0); 1527#endif /* OPT_DEBUG */ 1528 memset(((char *)dst) + pf_rule_desc[i].prf_offset, 0, 1529 pf_rule_desc[i].prf_size); 1530 } 1531} 1532 1533 1534/* 1535 * Remove superset information from two rules so we can directly compare them 1536 * with memcmp() 1537 */ 1538void 1539exclude_supersets(struct pf_rule *super, struct pf_rule *sub) 1540{ 1541 if (super->ifname[0] == '\0') 1542 memset(sub->ifname, 0, sizeof(sub->ifname)); 1543 if (super->direction == PF_INOUT) 1544 sub->direction = PF_INOUT; 1545 if ((super->proto == 0 || super->proto == sub->proto) && 1546 super->flags == 0 && super->flagset == 0 && (sub->flags || 1547 sub->flagset)) { 1548 sub->flags = super->flags; 1549 sub->flagset = super->flagset; 1550 } 1551 if (super->proto == 0) 1552 sub->proto = 0; 1553 1554 if (super->src.port_op == 0) { 1555 sub->src.port_op = 0; 1556 sub->src.port[0] = 0; 1557 sub->src.port[1] = 0; 1558 } 1559 if (super->dst.port_op == 0) { 1560 sub->dst.port_op = 0; 1561 sub->dst.port[0] = 0; 1562 sub->dst.port[1] = 0; 1563 } 1564 1565 if (super->src.addr.type == PF_ADDR_ADDRMASK && !super->src.neg && 1566 !sub->src.neg && super->src.addr.v.a.mask.addr32[0] == 0 && 1567 super->src.addr.v.a.mask.addr32[1] == 0 && 1568 super->src.addr.v.a.mask.addr32[2] == 0 && 1569 super->src.addr.v.a.mask.addr32[3] == 0) 1570 memset(&sub->src.addr, 0, sizeof(sub->src.addr)); 1571 else if (super->src.addr.type == PF_ADDR_ADDRMASK && 1572 sub->src.addr.type == PF_ADDR_ADDRMASK && 1573 super->src.neg == sub->src.neg && 1574 super->af == sub->af && 1575 unmask(&super->src.addr.v.a.mask, super->af) < 1576 unmask(&sub->src.addr.v.a.mask, sub->af) && 1577 super->src.addr.v.a.addr.addr32[0] == 1578 (sub->src.addr.v.a.addr.addr32[0] & 1579 super->src.addr.v.a.mask.addr32[0]) && 1580 super->src.addr.v.a.addr.addr32[1] == 1581 (sub->src.addr.v.a.addr.addr32[1] & 1582 super->src.addr.v.a.mask.addr32[1]) && 1583 super->src.addr.v.a.addr.addr32[2] == 1584 (sub->src.addr.v.a.addr.addr32[2] & 1585 super->src.addr.v.a.mask.addr32[2]) && 1586 super->src.addr.v.a.addr.addr32[3] == 1587 (sub->src.addr.v.a.addr.addr32[3] & 1588 super->src.addr.v.a.mask.addr32[3])) { 1589 /* sub->src.addr is a subset of super->src.addr/mask */ 1590 memcpy(&sub->src.addr, &super->src.addr, sizeof(sub->src.addr)); 1591 } 1592 1593 if (super->dst.addr.type == PF_ADDR_ADDRMASK && !super->dst.neg && 1594 !sub->dst.neg && super->dst.addr.v.a.mask.addr32[0] == 0 && 1595 super->dst.addr.v.a.mask.addr32[1] == 0 && 1596 super->dst.addr.v.a.mask.addr32[2] == 0 && 1597 super->dst.addr.v.a.mask.addr32[3] == 0) 1598 memset(&sub->dst.addr, 0, sizeof(sub->dst.addr)); 1599 else if (super->dst.addr.type == PF_ADDR_ADDRMASK && 1600 sub->dst.addr.type == PF_ADDR_ADDRMASK && 1601 super->dst.neg == sub->dst.neg && 1602 super->af == sub->af && 1603 unmask(&super->dst.addr.v.a.mask, super->af) < 1604 unmask(&sub->dst.addr.v.a.mask, sub->af) && 1605 super->dst.addr.v.a.addr.addr32[0] == 1606 (sub->dst.addr.v.a.addr.addr32[0] & 1607 super->dst.addr.v.a.mask.addr32[0]) && 1608 super->dst.addr.v.a.addr.addr32[1] == 1609 (sub->dst.addr.v.a.addr.addr32[1] & 1610 super->dst.addr.v.a.mask.addr32[1]) && 1611 super->dst.addr.v.a.addr.addr32[2] == 1612 (sub->dst.addr.v.a.addr.addr32[2] & 1613 super->dst.addr.v.a.mask.addr32[2]) && 1614 super->dst.addr.v.a.addr.addr32[3] == 1615 (sub->dst.addr.v.a.addr.addr32[3] & 1616 super->dst.addr.v.a.mask.addr32[3])) { 1617 /* sub->dst.addr is a subset of super->dst.addr/mask */ 1618 memcpy(&sub->dst.addr, &super->dst.addr, sizeof(sub->dst.addr)); 1619 } 1620 1621 if (super->af == 0) 1622 sub->af = 0; 1623} 1624 1625 1626void 1627superblock_free(struct pfctl *pf, struct superblock *block) 1628{ 1629 struct pf_opt_rule *por; 1630 while ((por = TAILQ_FIRST(&block->sb_rules))) { 1631 TAILQ_REMOVE(&block->sb_rules, por, por_entry); 1632 if (por->por_src_tbl) { 1633 if (por->por_src_tbl->pt_buf) { 1634 pfr_buf_clear(por->por_src_tbl->pt_buf); 1635 free(por->por_src_tbl->pt_buf); 1636 } 1637 free(por->por_src_tbl); 1638 } 1639 if (por->por_dst_tbl) { 1640 if (por->por_dst_tbl->pt_buf) { 1641 pfr_buf_clear(por->por_dst_tbl->pt_buf); 1642 free(por->por_dst_tbl->pt_buf); 1643 } 1644 free(por->por_dst_tbl); 1645 } 1646 free(por); 1647 } 1648 if (block->sb_profiled_block) 1649 superblock_free(pf, block->sb_profiled_block); 1650 free(block); 1651} 1652
|