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