1 /* $OpenBSD: pfctl_optimize.c,v 1.17 2008/05/06 03:45:21 mpf 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 <libpfctl.h> 34 #include <stddef.h> 35 #include <stdio.h> 36 #include <stdlib.h> 37 #include <string.h> 38 39 #include "pfctl_parser.h" 40 #include "pfctl.h" 41 42 /* The size at which a table becomes faster than individual rules */ 43 #define TABLE_THRESHOLD 6 44 45 46 /* #define OPT_DEBUG 1 */ 47 #ifdef OPT_DEBUG 48 # define DEBUG(str, v...) \ 49 printf("%s: " str "\n", __FUNCTION__ , ## v) 50 #else 51 # define DEBUG(str, v...) ((void)0) 52 #endif 53 54 55 /* 56 * A container that lets us sort a superblock to optimize the skip step jumps 57 */ 58 struct pf_skip_step { 59 int ps_count; /* number of items */ 60 TAILQ_HEAD( , pf_opt_rule) ps_rules; 61 TAILQ_ENTRY(pf_skip_step) ps_entry; 62 }; 63 64 65 /* 66 * A superblock is a block of adjacent rules of similar action. If there 67 * are five PASS rules in a row, they all become members of a superblock. 68 * Once we have a superblock, we are free to re-order any rules within it 69 * in order to improve performance; if a packet is passed, it doesn't matter 70 * who passed it. 71 */ 72 struct superblock { 73 TAILQ_HEAD( , pf_opt_rule) sb_rules; 74 TAILQ_ENTRY(superblock) sb_entry; 75 struct superblock *sb_profiled_block; 76 TAILQ_HEAD(skiplist, pf_skip_step) sb_skipsteps[PF_SKIP_COUNT]; 77 }; 78 TAILQ_HEAD(superblocks, superblock); 79 80 81 /* 82 * Description of the PF rule structure. 83 */ 84 enum { 85 BARRIER, /* the presence of the field puts the rule in its own block */ 86 BREAK, /* the field may not differ between rules in a superblock */ 87 NOMERGE, /* the field may not differ between rules when combined */ 88 COMBINED, /* the field may itself be combined with other rules */ 89 DC, /* we just don't care about the field */ 90 NEVER}; /* we should never see this field set?!? */ 91 static struct pf_rule_field { 92 const char *prf_name; 93 int prf_type; 94 size_t prf_offset; 95 size_t prf_size; 96 } pf_rule_desc[] = { 97 #define PF_RULE_FIELD(field, ty) \ 98 {#field, \ 99 ty, \ 100 offsetof(struct pfctl_rule, field), \ 101 sizeof(((struct pfctl_rule *)0)->field)} 102 103 104 /* 105 * The presence of these fields in a rule put the rule in its own 106 * superblock. Thus it will not be optimized. It also prevents the 107 * rule from being re-ordered at all. 108 */ 109 PF_RULE_FIELD(label, BARRIER), 110 PF_RULE_FIELD(prob, BARRIER), 111 PF_RULE_FIELD(max_states, BARRIER), 112 PF_RULE_FIELD(max_src_nodes, BARRIER), 113 PF_RULE_FIELD(max_src_states, BARRIER), 114 PF_RULE_FIELD(max_src_conn, BARRIER), 115 PF_RULE_FIELD(max_src_conn_rate, BARRIER), 116 PF_RULE_FIELD(anchor, BARRIER), /* for now */ 117 118 /* 119 * These fields must be the same between all rules in the same superblock. 120 * These rules are allowed to be re-ordered but only among like rules. 121 * For instance we can re-order all 'tag "foo"' rules because they have the 122 * same tag. But we can not re-order between a 'tag "foo"' and a 123 * 'tag "bar"' since that would change the meaning of the ruleset. 124 */ 125 PF_RULE_FIELD(tagname, BREAK), 126 PF_RULE_FIELD(keep_state, BREAK), 127 PF_RULE_FIELD(qname, BREAK), 128 PF_RULE_FIELD(pqname, BREAK), 129 PF_RULE_FIELD(rt, BREAK), 130 PF_RULE_FIELD(allow_opts, BREAK), 131 PF_RULE_FIELD(rule_flag, BREAK), 132 PF_RULE_FIELD(action, BREAK), 133 PF_RULE_FIELD(log, BREAK), 134 PF_RULE_FIELD(quick, BREAK), 135 PF_RULE_FIELD(return_ttl, BREAK), 136 PF_RULE_FIELD(overload_tblname, BREAK), 137 PF_RULE_FIELD(flush, BREAK), 138 PF_RULE_FIELD(rdr, BREAK), 139 PF_RULE_FIELD(nat, BREAK), 140 PF_RULE_FIELD(route, BREAK), 141 PF_RULE_FIELD(logif, BREAK), 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), 155 PF_RULE_FIELD(ifname, NOMERGE), /* hack for IF groups */ 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 PF_RULE_FIELD(af, NOMERGE), 178 179 /* These fields can be merged */ 180 PF_RULE_FIELD(src.addr, COMBINED), 181 PF_RULE_FIELD(dst.addr, COMBINED), 182 183 /* We just don't care about these fields. They're set by the kernel */ 184 PF_RULE_FIELD(skip, DC), 185 PF_RULE_FIELD(evaluations, DC), 186 PF_RULE_FIELD(packets, DC), 187 PF_RULE_FIELD(bytes, DC), 188 PF_RULE_FIELD(kif, DC), 189 PF_RULE_FIELD(states_cur, DC), 190 PF_RULE_FIELD(states_tot, DC), 191 PF_RULE_FIELD(src_nodes, DC), 192 PF_RULE_FIELD(nr, DC), 193 PF_RULE_FIELD(entries, DC), 194 PF_RULE_FIELD(qid, DC), 195 PF_RULE_FIELD(pqid, DC), 196 PF_RULE_FIELD(anchor_relative, DC), 197 PF_RULE_FIELD(anchor_wildcard, DC), 198 PF_RULE_FIELD(tag, DC), 199 PF_RULE_FIELD(match_tag, DC), 200 PF_RULE_FIELD(overload_tbl, DC), 201 202 /* These fields should never be set in a PASS/BLOCK rule */ 203 PF_RULE_FIELD(natpass, NEVER), 204 PF_RULE_FIELD(max_mss, NEVER), 205 PF_RULE_FIELD(min_ttl, NEVER), 206 PF_RULE_FIELD(set_tos, NEVER), 207 }; 208 209 210 211 int add_opt_table(struct pfctl *, struct pf_opt_tbl **, sa_family_t, 212 struct pf_rule_addr *); 213 int addrs_combineable(struct pf_rule_addr *, struct pf_rule_addr *); 214 int addrs_equal(struct pf_rule_addr *, struct pf_rule_addr *); 215 int block_feedback(struct pfctl *, struct superblock *); 216 int combine_rules(struct pfctl *, struct superblock *); 217 void comparable_rule(struct pfctl_rule *, const struct pfctl_rule *, int); 218 int construct_superblocks(struct pfctl *, struct pf_opt_queue *, 219 struct superblocks *); 220 void exclude_supersets(struct pfctl_rule *, struct pfctl_rule *); 221 int interface_group(const char *); 222 int load_feedback_profile(struct pfctl *, struct superblocks *); 223 int optimize_superblock(struct pfctl *, struct superblock *); 224 int pf_opt_create_table(struct pfctl *, struct pf_opt_tbl *); 225 void remove_from_skipsteps(struct skiplist *, struct superblock *, 226 struct pf_opt_rule *, struct pf_skip_step *); 227 int remove_identical_rules(struct pfctl *, struct superblock *); 228 int reorder_rules(struct pfctl *, struct superblock *, int); 229 int rules_combineable(struct pfctl_rule *, struct pfctl_rule *); 230 void skip_append(struct superblock *, int, struct pf_skip_step *, 231 struct pf_opt_rule *); 232 int skip_compare(int, struct pf_skip_step *, struct pf_opt_rule *); 233 void skip_init(void); 234 int skip_cmp_af(struct pfctl_rule *, struct pfctl_rule *); 235 int skip_cmp_dir(struct pfctl_rule *, struct pfctl_rule *); 236 int skip_cmp_dst_addr(struct pfctl_rule *, struct pfctl_rule *); 237 int skip_cmp_dst_port(struct pfctl_rule *, struct pfctl_rule *); 238 int skip_cmp_ifp(struct pfctl_rule *, struct pfctl_rule *); 239 int skip_cmp_proto(struct pfctl_rule *, struct pfctl_rule *); 240 int skip_cmp_src_addr(struct pfctl_rule *, struct pfctl_rule *); 241 int skip_cmp_src_port(struct pfctl_rule *, struct pfctl_rule *); 242 int superblock_inclusive(struct superblock *, struct pf_opt_rule *); 243 void superblock_free(struct pfctl *, struct superblock *); 244 245 246 static int (*skip_comparitors[PF_SKIP_COUNT])(struct pfctl_rule *, 247 struct pfctl_rule *); 248 static const char *skip_comparitors_names[PF_SKIP_COUNT]; 249 #define PF_SKIP_COMPARITORS { \ 250 { "ifp", PF_SKIP_IFP, skip_cmp_ifp }, \ 251 { "dir", PF_SKIP_DIR, skip_cmp_dir }, \ 252 { "af", PF_SKIP_AF, skip_cmp_af }, \ 253 { "proto", PF_SKIP_PROTO, skip_cmp_proto }, \ 254 { "saddr", PF_SKIP_SRC_ADDR, skip_cmp_src_addr }, \ 255 { "daddr", PF_SKIP_DST_ADDR, skip_cmp_dst_addr }, \ 256 { "sport", PF_SKIP_SRC_PORT, skip_cmp_src_port }, \ 257 { "dport", PF_SKIP_DST_PORT, skip_cmp_dst_port } \ 258 } 259 260 static struct pfr_buffer table_buffer; 261 static int table_identifier; 262 263 264 int 265 pfctl_optimize_ruleset(struct pfctl *pf, struct pfctl_ruleset *rs) 266 { 267 struct superblocks superblocks; 268 struct pf_opt_queue opt_queue; 269 struct superblock *block; 270 struct pf_opt_rule *por; 271 struct pfctl_rule *r; 272 struct pfctl_rulequeue *old_rules; 273 274 DEBUG("optimizing ruleset"); 275 memset(&table_buffer, 0, sizeof(table_buffer)); 276 skip_init(); 277 TAILQ_INIT(&opt_queue); 278 279 old_rules = rs->rules[PF_RULESET_FILTER].active.ptr; 280 rs->rules[PF_RULESET_FILTER].active.ptr = 281 rs->rules[PF_RULESET_FILTER].inactive.ptr; 282 rs->rules[PF_RULESET_FILTER].inactive.ptr = old_rules; 283 284 /* 285 * XXX expanding the pf_opt_rule format throughout pfctl might allow 286 * us to avoid all this copying. 287 */ 288 while ((r = TAILQ_FIRST(rs->rules[PF_RULESET_FILTER].inactive.ptr)) 289 != NULL) { 290 TAILQ_REMOVE(rs->rules[PF_RULESET_FILTER].inactive.ptr, r, 291 entries); 292 if ((por = calloc(1, sizeof(*por))) == NULL) 293 err(1, "calloc"); 294 memcpy(&por->por_rule, r, sizeof(*r)); 295 if (TAILQ_FIRST(&r->rdr.list) != NULL) { 296 TAILQ_INIT(&por->por_rule.rdr.list); 297 pfctl_move_pool(&r->rdr, &por->por_rule.rdr); 298 } else 299 bzero(&por->por_rule.rdr, 300 sizeof(por->por_rule.rdr)); 301 if (TAILQ_FIRST(&r->nat.list) != NULL) { 302 TAILQ_INIT(&por->por_rule.nat.list); 303 pfctl_move_pool(&r->nat, &por->por_rule.nat); 304 } else 305 bzero(&por->por_rule.nat, 306 sizeof(por->por_rule.nat)); 307 if (TAILQ_FIRST(&r->route.list) != NULL) { 308 TAILQ_INIT(&por->por_rule.route.list); 309 pfctl_move_pool(&r->route, &por->por_rule.route); 310 } else 311 bzero(&por->por_rule.route, 312 sizeof(por->por_rule.route)); 313 314 TAILQ_INSERT_TAIL(&opt_queue, por, por_entry); 315 } 316 317 TAILQ_INIT(&superblocks); 318 if (construct_superblocks(pf, &opt_queue, &superblocks)) 319 goto error; 320 321 if (pf->optimize & PF_OPTIMIZE_PROFILE) { 322 if (load_feedback_profile(pf, &superblocks)) 323 goto error; 324 } 325 326 TAILQ_FOREACH(block, &superblocks, sb_entry) { 327 if (optimize_superblock(pf, block)) 328 goto error; 329 } 330 331 rs->anchor->refcnt = 0; 332 while ((block = TAILQ_FIRST(&superblocks))) { 333 TAILQ_REMOVE(&superblocks, block, sb_entry); 334 335 while ((por = TAILQ_FIRST(&block->sb_rules))) { 336 TAILQ_REMOVE(&block->sb_rules, por, por_entry); 337 por->por_rule.nr = rs->anchor->refcnt++; 338 if ((r = calloc(1, sizeof(*r))) == NULL) 339 err(1, "calloc"); 340 memcpy(r, &por->por_rule, sizeof(*r)); 341 TAILQ_INIT(&r->rdr.list); 342 pfctl_move_pool(&por->por_rule.rdr, &r->rdr); 343 TAILQ_INIT(&r->nat.list); 344 pfctl_move_pool(&por->por_rule.nat, &r->nat); 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, 0); 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 pfctl_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 struct pfctl_rule a, b; 834 835 836 /* 837 * Walk through all of the profiled superblock's rules and copy 838 * the counters onto our rules. 839 */ 840 TAILQ_FOREACH(por1, &block->sb_profiled_block->sb_rules, por_entry) { 841 comparable_rule(&a, &por1->por_rule, DC); 842 TAILQ_FOREACH(por2, &block->sb_rules, por_entry) { 843 if (por2->por_profile_count) 844 continue; 845 comparable_rule(&b, &por2->por_rule, DC); 846 if (memcmp(&a, &b, sizeof(a)) == 0) { 847 por2->por_profile_count = 848 por1->por_rule.packets[0] + 849 por1->por_rule.packets[1]; 850 break; 851 } 852 } 853 } 854 superblock_free(pf, block->sb_profiled_block); 855 block->sb_profiled_block = NULL; 856 857 /* 858 * Now we pull all of the rules off the superblock and re-insert them 859 * in sorted order. 860 */ 861 862 TAILQ_INIT(&queue); 863 while ((por1 = TAILQ_FIRST(&block->sb_rules)) != NULL) { 864 TAILQ_REMOVE(&block->sb_rules, por1, por_entry); 865 TAILQ_INSERT_TAIL(&queue, por1, por_entry); 866 } 867 868 while ((por1 = TAILQ_FIRST(&queue)) != NULL) { 869 TAILQ_REMOVE(&queue, por1, por_entry); 870 /* XXX I should sort all of the unused rules based on skip steps */ 871 TAILQ_FOREACH(por2, &block->sb_rules, por_entry) { 872 if (por1->por_profile_count > por2->por_profile_count) { 873 TAILQ_INSERT_BEFORE(por2, por1, por_entry); 874 break; 875 } 876 } 877 #ifdef __FreeBSD__ 878 if (por2 == NULL) 879 #else 880 if (por2 == TAILQ_END(&block->sb_rules)) 881 #endif 882 TAILQ_INSERT_TAIL(&block->sb_rules, por1, por_entry); 883 } 884 885 return (0); 886 } 887 888 889 /* 890 * Load the current ruleset from the kernel and try to associate them with 891 * the ruleset we're optimizing. 892 */ 893 int 894 load_feedback_profile(struct pfctl *pf, struct superblocks *superblocks) 895 { 896 char anchor_call[MAXPATHLEN] = ""; 897 struct superblock *block, *blockcur; 898 struct superblocks prof_superblocks; 899 struct pf_opt_rule *por; 900 struct pf_opt_queue queue; 901 struct pfctl_rules_info rules; 902 struct pfctl_rule a, b, rule; 903 int nr, mnr; 904 905 TAILQ_INIT(&queue); 906 TAILQ_INIT(&prof_superblocks); 907 908 if (pfctl_get_rules_info_h(pf->h, &rules, PF_PASS, "")) { 909 warn("DIOCGETRULES"); 910 return (1); 911 } 912 mnr = rules.nr; 913 914 DEBUG("Loading %d active rules for a feedback profile", mnr); 915 for (nr = 0; nr < mnr; ++nr) { 916 struct pfctl_ruleset *rs; 917 if ((por = calloc(1, sizeof(*por))) == NULL) { 918 warn("calloc"); 919 return (1); 920 } 921 922 if (pfctl_get_rule_h(pf->h, nr, rules.ticket, "", PF_PASS, 923 &rule, anchor_call)) { 924 warn("DIOCGETRULENV"); 925 return (1); 926 } 927 memcpy(&por->por_rule, &rule, sizeof(por->por_rule)); 928 rs = pf_find_or_create_ruleset(anchor_call); 929 por->por_rule.anchor = rs->anchor; 930 if (TAILQ_EMPTY(&por->por_rule.rdr.list)) 931 memset(&por->por_rule.rdr, 0, 932 sizeof(por->por_rule.rdr)); 933 if (TAILQ_EMPTY(&por->por_rule.nat.list)) 934 memset(&por->por_rule.nat, 0, 935 sizeof(por->por_rule.nat)); 936 TAILQ_INSERT_TAIL(&queue, por, por_entry); 937 938 /* XXX pfctl_get_pool(pf->dev, &rule.rdr, nr, pr.ticket, 939 * PF_PASS, pf->anchor) ??? 940 * ... pfctl_clear_pool(&rule.rdr) 941 */ 942 } 943 944 if (construct_superblocks(pf, &queue, &prof_superblocks)) 945 return (1); 946 947 948 /* 949 * Now we try to associate the active ruleset's superblocks with 950 * the superblocks we're compiling. 951 */ 952 block = TAILQ_FIRST(superblocks); 953 blockcur = TAILQ_FIRST(&prof_superblocks); 954 while (block && blockcur) { 955 comparable_rule(&a, &TAILQ_FIRST(&block->sb_rules)->por_rule, 956 BREAK); 957 comparable_rule(&b, &TAILQ_FIRST(&blockcur->sb_rules)->por_rule, 958 BREAK); 959 if (memcmp(&a, &b, sizeof(a)) == 0) { 960 /* The two superblocks lined up */ 961 block->sb_profiled_block = blockcur; 962 } else { 963 DEBUG("superblocks don't line up between #%d and #%d", 964 TAILQ_FIRST(&block->sb_rules)->por_rule.nr, 965 TAILQ_FIRST(&blockcur->sb_rules)->por_rule.nr); 966 break; 967 } 968 block = TAILQ_NEXT(block, sb_entry); 969 blockcur = TAILQ_NEXT(blockcur, sb_entry); 970 } 971 972 973 974 /* Free any superblocks we couldn't link */ 975 while (blockcur) { 976 block = TAILQ_NEXT(blockcur, sb_entry); 977 superblock_free(pf, blockcur); 978 blockcur = block; 979 } 980 return (0); 981 } 982 983 984 /* 985 * Compare a rule to a skiplist to see if the rule is a member 986 */ 987 int 988 skip_compare(int skipnum, struct pf_skip_step *skiplist, 989 struct pf_opt_rule *por) 990 { 991 struct pfctl_rule *a, *b; 992 if (skipnum >= PF_SKIP_COUNT || skipnum < 0) 993 errx(1, "skip_compare() out of bounds"); 994 a = &por->por_rule; 995 b = &TAILQ_FIRST(&skiplist->ps_rules)->por_rule; 996 997 return ((skip_comparitors[skipnum])(a, b)); 998 } 999 1000 1001 /* 1002 * Add a rule to a skiplist 1003 */ 1004 void 1005 skip_append(struct superblock *superblock, int skipnum, 1006 struct pf_skip_step *skiplist, struct pf_opt_rule *por) 1007 { 1008 struct pf_skip_step *prev; 1009 1010 skiplist->ps_count++; 1011 TAILQ_INSERT_TAIL(&skiplist->ps_rules, por, por_skip_entry[skipnum]); 1012 1013 /* Keep the list of skiplists sorted by whichever is larger */ 1014 while ((prev = TAILQ_PREV(skiplist, skiplist, ps_entry)) && 1015 prev->ps_count < skiplist->ps_count) { 1016 TAILQ_REMOVE(&superblock->sb_skipsteps[skipnum], 1017 skiplist, ps_entry); 1018 TAILQ_INSERT_BEFORE(prev, skiplist, ps_entry); 1019 } 1020 } 1021 1022 1023 /* 1024 * Remove a rule from the other skiplist calculations. 1025 */ 1026 void 1027 remove_from_skipsteps(struct skiplist *head, struct superblock *block, 1028 struct pf_opt_rule *por, struct pf_skip_step *active_list) 1029 { 1030 struct pf_skip_step *sk, *next; 1031 struct pf_opt_rule *p2; 1032 int i, found; 1033 1034 for (i = 0; i < PF_SKIP_COUNT; i++) { 1035 sk = TAILQ_FIRST(&block->sb_skipsteps[i]); 1036 if (sk == NULL || sk == active_list || sk->ps_count <= 1) 1037 continue; 1038 found = 0; 1039 do { 1040 TAILQ_FOREACH(p2, &sk->ps_rules, por_skip_entry[i]) 1041 if (p2 == por) { 1042 TAILQ_REMOVE(&sk->ps_rules, p2, 1043 por_skip_entry[i]); 1044 found = 1; 1045 sk->ps_count--; 1046 break; 1047 } 1048 } while (!found && (sk = TAILQ_NEXT(sk, ps_entry))); 1049 if (found && sk) { 1050 /* Does this change the sorting order? */ 1051 while ((next = TAILQ_NEXT(sk, ps_entry)) && 1052 next->ps_count > sk->ps_count) { 1053 TAILQ_REMOVE(head, sk, ps_entry); 1054 TAILQ_INSERT_AFTER(head, next, sk, ps_entry); 1055 } 1056 #ifdef OPT_DEBUG 1057 next = TAILQ_NEXT(sk, ps_entry); 1058 assert(next == NULL || next->ps_count <= sk->ps_count); 1059 #endif /* OPT_DEBUG */ 1060 } 1061 } 1062 } 1063 1064 1065 /* Compare two rules AF field for skiplist construction */ 1066 int 1067 skip_cmp_af(struct pfctl_rule *a, struct pfctl_rule *b) 1068 { 1069 if (a->af != b->af || a->af == 0) 1070 return (1); 1071 return (0); 1072 } 1073 1074 /* Compare two rules DIRECTION field for skiplist construction */ 1075 int 1076 skip_cmp_dir(struct pfctl_rule *a, struct pfctl_rule *b) 1077 { 1078 if (a->direction == 0 || a->direction != b->direction) 1079 return (1); 1080 return (0); 1081 } 1082 1083 /* Compare two rules DST Address field for skiplist construction */ 1084 int 1085 skip_cmp_dst_addr(struct pfctl_rule *a, struct pfctl_rule *b) 1086 { 1087 if (a->dst.neg != b->dst.neg || 1088 a->dst.addr.type != b->dst.addr.type) 1089 return (1); 1090 /* XXX if (a->proto != b->proto && a->proto != 0 && b->proto != 0 1091 * && (a->proto == IPPROTO_TCP || a->proto == IPPROTO_UDP || 1092 * a->proto == IPPROTO_ICMP 1093 * return (1); 1094 */ 1095 switch (a->dst.addr.type) { 1096 case PF_ADDR_ADDRMASK: 1097 if (memcmp(&a->dst.addr.v.a.addr, &b->dst.addr.v.a.addr, 1098 sizeof(a->dst.addr.v.a.addr)) || 1099 memcmp(&a->dst.addr.v.a.mask, &b->dst.addr.v.a.mask, 1100 sizeof(a->dst.addr.v.a.mask)) || 1101 (a->dst.addr.v.a.addr.addr32[0] == 0 && 1102 a->dst.addr.v.a.addr.addr32[1] == 0 && 1103 a->dst.addr.v.a.addr.addr32[2] == 0 && 1104 a->dst.addr.v.a.addr.addr32[3] == 0)) 1105 return (1); 1106 return (0); 1107 case PF_ADDR_DYNIFTL: 1108 if (strcmp(a->dst.addr.v.ifname, b->dst.addr.v.ifname) != 0 || 1109 a->dst.addr.iflags != b->dst.addr.iflags || 1110 memcmp(&a->dst.addr.v.a.mask, &b->dst.addr.v.a.mask, 1111 sizeof(a->dst.addr.v.a.mask))) 1112 return (1); 1113 return (0); 1114 case PF_ADDR_NOROUTE: 1115 case PF_ADDR_URPFFAILED: 1116 return (0); 1117 case PF_ADDR_TABLE: 1118 return (strcmp(a->dst.addr.v.tblname, b->dst.addr.v.tblname)); 1119 } 1120 return (1); 1121 } 1122 1123 /* Compare two rules DST port field for skiplist construction */ 1124 int 1125 skip_cmp_dst_port(struct pfctl_rule *a, struct pfctl_rule *b) 1126 { 1127 /* XXX if (a->proto != b->proto && a->proto != 0 && b->proto != 0 1128 * && (a->proto == IPPROTO_TCP || a->proto == IPPROTO_UDP || 1129 * a->proto == IPPROTO_ICMP 1130 * return (1); 1131 */ 1132 if (a->dst.port_op == PF_OP_NONE || a->dst.port_op != b->dst.port_op || 1133 a->dst.port[0] != b->dst.port[0] || 1134 a->dst.port[1] != b->dst.port[1]) 1135 return (1); 1136 return (0); 1137 } 1138 1139 /* Compare two rules IFP field for skiplist construction */ 1140 int 1141 skip_cmp_ifp(struct pfctl_rule *a, struct pfctl_rule *b) 1142 { 1143 if (strcmp(a->ifname, b->ifname) || a->ifname[0] == '\0') 1144 return (1); 1145 return (a->ifnot != b->ifnot); 1146 } 1147 1148 /* Compare two rules PROTO field for skiplist construction */ 1149 int 1150 skip_cmp_proto(struct pfctl_rule *a, struct pfctl_rule *b) 1151 { 1152 return (a->proto != b->proto || a->proto == 0); 1153 } 1154 1155 /* Compare two rules SRC addr field for skiplist construction */ 1156 int 1157 skip_cmp_src_addr(struct pfctl_rule *a, struct pfctl_rule *b) 1158 { 1159 if (a->src.neg != b->src.neg || 1160 a->src.addr.type != b->src.addr.type) 1161 return (1); 1162 /* XXX if (a->proto != b->proto && a->proto != 0 && b->proto != 0 1163 * && (a->proto == IPPROTO_TCP || a->proto == IPPROTO_UDP || 1164 * a->proto == IPPROTO_ICMP 1165 * return (1); 1166 */ 1167 switch (a->src.addr.type) { 1168 case PF_ADDR_ADDRMASK: 1169 if (memcmp(&a->src.addr.v.a.addr, &b->src.addr.v.a.addr, 1170 sizeof(a->src.addr.v.a.addr)) || 1171 memcmp(&a->src.addr.v.a.mask, &b->src.addr.v.a.mask, 1172 sizeof(a->src.addr.v.a.mask)) || 1173 (a->src.addr.v.a.addr.addr32[0] == 0 && 1174 a->src.addr.v.a.addr.addr32[1] == 0 && 1175 a->src.addr.v.a.addr.addr32[2] == 0 && 1176 a->src.addr.v.a.addr.addr32[3] == 0)) 1177 return (1); 1178 return (0); 1179 case PF_ADDR_DYNIFTL: 1180 if (strcmp(a->src.addr.v.ifname, b->src.addr.v.ifname) != 0 || 1181 a->src.addr.iflags != b->src.addr.iflags || 1182 memcmp(&a->src.addr.v.a.mask, &b->src.addr.v.a.mask, 1183 sizeof(a->src.addr.v.a.mask))) 1184 return (1); 1185 return (0); 1186 case PF_ADDR_NOROUTE: 1187 case PF_ADDR_URPFFAILED: 1188 return (0); 1189 case PF_ADDR_TABLE: 1190 return (strcmp(a->src.addr.v.tblname, b->src.addr.v.tblname)); 1191 } 1192 return (1); 1193 } 1194 1195 /* Compare two rules SRC port field for skiplist construction */ 1196 int 1197 skip_cmp_src_port(struct pfctl_rule *a, struct pfctl_rule *b) 1198 { 1199 if (a->src.port_op == PF_OP_NONE || a->src.port_op != b->src.port_op || 1200 a->src.port[0] != b->src.port[0] || 1201 a->src.port[1] != b->src.port[1]) 1202 return (1); 1203 /* XXX if (a->proto != b->proto && a->proto != 0 && b->proto != 0 1204 * && (a->proto == IPPROTO_TCP || a->proto == IPPROTO_UDP || 1205 * a->proto == IPPROTO_ICMP 1206 * return (1); 1207 */ 1208 return (0); 1209 } 1210 1211 1212 void 1213 skip_init(void) 1214 { 1215 struct { 1216 char *name; 1217 int skipnum; 1218 int (*func)(struct pfctl_rule *, struct pfctl_rule *); 1219 } comps[] = PF_SKIP_COMPARITORS; 1220 int skipnum, i; 1221 1222 for (skipnum = 0; skipnum < PF_SKIP_COUNT; skipnum++) { 1223 for (i = 0; i < sizeof(comps)/sizeof(*comps); i++) 1224 if (comps[i].skipnum == skipnum) { 1225 skip_comparitors[skipnum] = comps[i].func; 1226 skip_comparitors_names[skipnum] = comps[i].name; 1227 } 1228 } 1229 for (skipnum = 0; skipnum < PF_SKIP_COUNT; skipnum++) 1230 if (skip_comparitors[skipnum] == NULL) 1231 errx(1, "Need to add skip step comparitor to pfctl?!"); 1232 } 1233 1234 /* 1235 * Add a host/netmask to a table 1236 */ 1237 int 1238 add_opt_table(struct pfctl *pf, struct pf_opt_tbl **tbl, sa_family_t af, 1239 struct pf_rule_addr *addr) 1240 { 1241 #ifdef OPT_DEBUG 1242 char buf[128]; 1243 #endif /* OPT_DEBUG */ 1244 static int tablenum = 0; 1245 struct node_host node_host; 1246 1247 if (*tbl == NULL) { 1248 if ((*tbl = calloc(1, sizeof(**tbl))) == NULL || 1249 ((*tbl)->pt_buf = calloc(1, sizeof(*(*tbl)->pt_buf))) == 1250 NULL) 1251 err(1, "calloc"); 1252 (*tbl)->pt_buf->pfrb_type = PFRB_ADDRS; 1253 SIMPLEQ_INIT(&(*tbl)->pt_nodes); 1254 1255 /* This is just a temporary table name */ 1256 snprintf((*tbl)->pt_name, sizeof((*tbl)->pt_name), "%s%d", 1257 PF_OPT_TABLE_PREFIX, tablenum++); 1258 DEBUG("creating table <%s>", (*tbl)->pt_name); 1259 } 1260 1261 memset(&node_host, 0, sizeof(node_host)); 1262 node_host.af = af; 1263 node_host.addr = addr->addr; 1264 1265 #ifdef OPT_DEBUG 1266 DEBUG("<%s> adding %s/%d", (*tbl)->pt_name, inet_ntop(af, 1267 &node_host.addr.v.a.addr, buf, sizeof(buf)), 1268 unmask(&node_host.addr.v.a.mask, af)); 1269 #endif /* OPT_DEBUG */ 1270 1271 if (append_addr_host((*tbl)->pt_buf, &node_host, 0, 0)) { 1272 warn("failed to add host"); 1273 return (1); 1274 } 1275 if (pf->opts & PF_OPT_VERBOSE) { 1276 struct node_tinit *ti; 1277 1278 if ((ti = calloc(1, sizeof(*ti))) == NULL) 1279 err(1, "malloc"); 1280 if ((ti->host = malloc(sizeof(*ti->host))) == NULL) 1281 err(1, "malloc"); 1282 memcpy(ti->host, &node_host, sizeof(*ti->host)); 1283 SIMPLEQ_INSERT_TAIL(&(*tbl)->pt_nodes, ti, entries); 1284 } 1285 1286 (*tbl)->pt_rulecount++; 1287 if ((*tbl)->pt_rulecount == TABLE_THRESHOLD) 1288 DEBUG("table <%s> now faster than skip steps", (*tbl)->pt_name); 1289 1290 return (0); 1291 } 1292 1293 1294 /* 1295 * Do the dirty work of choosing an unused table name and creating it. 1296 * (be careful with the table name, it might already be used in another anchor) 1297 */ 1298 int 1299 pf_opt_create_table(struct pfctl *pf, struct pf_opt_tbl *tbl) 1300 { 1301 static int tablenum; 1302 struct pfr_table *t; 1303 1304 if (table_buffer.pfrb_type == 0) { 1305 /* Initialize the list of tables */ 1306 table_buffer.pfrb_type = PFRB_TABLES; 1307 for (;;) { 1308 pfr_buf_grow(&table_buffer, table_buffer.pfrb_size); 1309 table_buffer.pfrb_size = table_buffer.pfrb_msize; 1310 if (pfr_get_tables(NULL, table_buffer.pfrb_caddr, 1311 &table_buffer.pfrb_size, PFR_FLAG_ALLRSETS)) 1312 err(1, "pfr_get_tables"); 1313 if (table_buffer.pfrb_size <= table_buffer.pfrb_msize) 1314 break; 1315 } 1316 table_identifier = arc4random(); 1317 } 1318 1319 /* XXX would be *really* nice to avoid duplicating identical tables */ 1320 1321 /* Now we have to pick a table name that isn't used */ 1322 again: 1323 DEBUG("translating temporary table <%s> to <%s%x_%d>", tbl->pt_name, 1324 PF_OPT_TABLE_PREFIX, table_identifier, tablenum); 1325 snprintf(tbl->pt_name, sizeof(tbl->pt_name), "%s%x_%d", 1326 PF_OPT_TABLE_PREFIX, table_identifier, tablenum); 1327 PFRB_FOREACH(t, &table_buffer) { 1328 if (strcasecmp(t->pfrt_name, tbl->pt_name) == 0) { 1329 /* Collision. Try again */ 1330 DEBUG("wow, table <%s> in use. trying again", 1331 tbl->pt_name); 1332 table_identifier = arc4random(); 1333 goto again; 1334 } 1335 } 1336 tablenum++; 1337 1338 1339 if (pfctl_define_table(tbl->pt_name, PFR_TFLAG_CONST, 1, 1340 pf->astack[0]->name, tbl->pt_buf, pf->astack[0]->ruleset.tticket)) { 1341 warn("failed to create table %s in %s", 1342 tbl->pt_name, pf->astack[0]->name); 1343 return (1); 1344 } 1345 return (0); 1346 } 1347 1348 /* 1349 * Partition the flat ruleset into a list of distinct superblocks 1350 */ 1351 int 1352 construct_superblocks(struct pfctl *pf, struct pf_opt_queue *opt_queue, 1353 struct superblocks *superblocks) 1354 { 1355 struct superblock *block = NULL; 1356 struct pf_opt_rule *por; 1357 int i; 1358 1359 while (!TAILQ_EMPTY(opt_queue)) { 1360 por = TAILQ_FIRST(opt_queue); 1361 TAILQ_REMOVE(opt_queue, por, por_entry); 1362 if (block == NULL || !superblock_inclusive(block, por)) { 1363 if ((block = calloc(1, sizeof(*block))) == NULL) { 1364 warn("calloc"); 1365 return (1); 1366 } 1367 TAILQ_INIT(&block->sb_rules); 1368 for (i = 0; i < PF_SKIP_COUNT; i++) 1369 TAILQ_INIT(&block->sb_skipsteps[i]); 1370 TAILQ_INSERT_TAIL(superblocks, block, sb_entry); 1371 } 1372 TAILQ_INSERT_TAIL(&block->sb_rules, por, por_entry); 1373 } 1374 1375 return (0); 1376 } 1377 1378 1379 /* 1380 * Compare two rule addresses 1381 */ 1382 int 1383 addrs_equal(struct pf_rule_addr *a, struct pf_rule_addr *b) 1384 { 1385 if (a->neg != b->neg) 1386 return (0); 1387 return (memcmp(&a->addr, &b->addr, sizeof(a->addr)) == 0); 1388 } 1389 1390 1391 /* 1392 * The addresses are not equal, but can we combine them into one table? 1393 */ 1394 int 1395 addrs_combineable(struct pf_rule_addr *a, struct pf_rule_addr *b) 1396 { 1397 if (a->addr.type != PF_ADDR_ADDRMASK || 1398 b->addr.type != PF_ADDR_ADDRMASK) 1399 return (0); 1400 if (a->neg != b->neg || a->port_op != b->port_op || 1401 a->port[0] != b->port[0] || a->port[1] != b->port[1]) 1402 return (0); 1403 return (1); 1404 } 1405 1406 1407 /* 1408 * Are we allowed to combine these two rules 1409 */ 1410 int 1411 rules_combineable(struct pfctl_rule *p1, struct pfctl_rule *p2) 1412 { 1413 struct pfctl_rule a, b; 1414 1415 comparable_rule(&a, p1, COMBINED); 1416 comparable_rule(&b, p2, COMBINED); 1417 return (memcmp(&a, &b, sizeof(a)) == 0); 1418 } 1419 1420 1421 /* 1422 * Can a rule be included inside a superblock 1423 */ 1424 int 1425 superblock_inclusive(struct superblock *block, struct pf_opt_rule *por) 1426 { 1427 struct pfctl_rule a, b; 1428 int i, j; 1429 1430 /* First check for hard breaks */ 1431 for (i = 0; i < sizeof(pf_rule_desc)/sizeof(*pf_rule_desc); i++) { 1432 if (pf_rule_desc[i].prf_type == BARRIER) { 1433 for (j = 0; j < pf_rule_desc[i].prf_size; j++) 1434 if (((char *)&por->por_rule)[j + 1435 pf_rule_desc[i].prf_offset] != 0) 1436 return (0); 1437 } 1438 } 1439 1440 /* per-rule src-track is also a hard break */ 1441 if (por->por_rule.rule_flag & PFRULE_RULESRCTRACK) 1442 return (0); 1443 1444 /* 1445 * Have to handle interface groups separately. Consider the following 1446 * rules: 1447 * block on EXTIFS to any port 22 1448 * pass on em0 to any port 22 1449 * (where EXTIFS is an arbitrary interface group) 1450 * The optimizer may decide to re-order the pass rule in front of the 1451 * block rule. But what if EXTIFS includes em0??? Such a reordering 1452 * would change the meaning of the ruleset. 1453 * We can't just lookup the EXTIFS group and check if em0 is a member 1454 * because the user is allowed to add interfaces to a group during 1455 * runtime. 1456 * Ergo interface groups become a defacto superblock break :-( 1457 */ 1458 if (interface_group(por->por_rule.ifname) || 1459 interface_group(TAILQ_FIRST(&block->sb_rules)->por_rule.ifname)) { 1460 if (strcasecmp(por->por_rule.ifname, 1461 TAILQ_FIRST(&block->sb_rules)->por_rule.ifname) != 0) 1462 return (0); 1463 } 1464 1465 comparable_rule(&a, &TAILQ_FIRST(&block->sb_rules)->por_rule, NOMERGE); 1466 comparable_rule(&b, &por->por_rule, NOMERGE); 1467 if (memcmp(&a, &b, sizeof(a)) == 0) 1468 return (1); 1469 1470 #ifdef OPT_DEBUG 1471 for (i = 0; i < sizeof(por->por_rule); i++) { 1472 int closest = -1; 1473 if (((u_int8_t *)&a)[i] != ((u_int8_t *)&b)[i]) { 1474 for (j = 0; j < sizeof(pf_rule_desc) / 1475 sizeof(*pf_rule_desc); j++) { 1476 if (i >= pf_rule_desc[j].prf_offset && 1477 i < pf_rule_desc[j].prf_offset + 1478 pf_rule_desc[j].prf_size) { 1479 DEBUG("superblock break @ %d due to %s", 1480 por->por_rule.nr, 1481 pf_rule_desc[j].prf_name); 1482 return (0); 1483 } 1484 if (i > pf_rule_desc[j].prf_offset) { 1485 if (closest == -1 || 1486 i-pf_rule_desc[j].prf_offset < 1487 i-pf_rule_desc[closest].prf_offset) 1488 closest = j; 1489 } 1490 } 1491 1492 if (closest >= 0) 1493 DEBUG("superblock break @ %d on %s+%zxh", 1494 por->por_rule.nr, 1495 pf_rule_desc[closest].prf_name, 1496 i - pf_rule_desc[closest].prf_offset - 1497 pf_rule_desc[closest].prf_size); 1498 else 1499 DEBUG("superblock break @ %d on field @ %d", 1500 por->por_rule.nr, i); 1501 return (0); 1502 } 1503 } 1504 #endif /* OPT_DEBUG */ 1505 1506 return (0); 1507 } 1508 1509 1510 /* 1511 * Figure out if an interface name is an actual interface or actually a 1512 * group of interfaces. 1513 */ 1514 int 1515 interface_group(const char *ifname) 1516 { 1517 int s; 1518 struct ifgroupreq ifgr; 1519 1520 if (ifname == NULL || !ifname[0]) 1521 return (0); 1522 1523 s = get_query_socket(); 1524 1525 memset(&ifgr, 0, sizeof(ifgr)); 1526 strlcpy(ifgr.ifgr_name, ifname, IFNAMSIZ); 1527 if (ioctl(s, SIOCGIFGMEMB, (caddr_t)&ifgr) == -1) { 1528 if (errno == ENOENT) 1529 return (0); 1530 else 1531 err(1, "SIOCGIFGMEMB"); 1532 } 1533 1534 return (1); 1535 } 1536 1537 1538 /* 1539 * Make a rule that can directly compared by memcmp() 1540 */ 1541 void 1542 comparable_rule(struct pfctl_rule *dst, const struct pfctl_rule *src, int type) 1543 { 1544 int i; 1545 /* 1546 * To simplify the comparison, we just zero out the fields that are 1547 * allowed to be different and then do a simple memcmp() 1548 */ 1549 memcpy(dst, src, sizeof(*dst)); 1550 for (i = 0; i < sizeof(pf_rule_desc)/sizeof(*pf_rule_desc); i++) 1551 if (pf_rule_desc[i].prf_type >= type) { 1552 #ifdef OPT_DEBUG 1553 assert(pf_rule_desc[i].prf_type != NEVER || 1554 *(((char *)dst) + pf_rule_desc[i].prf_offset) == 0); 1555 #endif /* OPT_DEBUG */ 1556 memset(((char *)dst) + pf_rule_desc[i].prf_offset, 0, 1557 pf_rule_desc[i].prf_size); 1558 } 1559 } 1560 1561 1562 /* 1563 * Remove superset information from two rules so we can directly compare them 1564 * with memcmp() 1565 */ 1566 void 1567 exclude_supersets(struct pfctl_rule *super, struct pfctl_rule *sub) 1568 { 1569 if (super->ifname[0] == '\0') 1570 memset(sub->ifname, 0, sizeof(sub->ifname)); 1571 if (super->direction == PF_INOUT) 1572 sub->direction = PF_INOUT; 1573 if ((super->proto == 0 || super->proto == sub->proto) && 1574 super->flags == 0 && super->flagset == 0 && (sub->flags || 1575 sub->flagset)) { 1576 sub->flags = super->flags; 1577 sub->flagset = super->flagset; 1578 } 1579 if (super->proto == 0) 1580 sub->proto = 0; 1581 1582 if (super->src.port_op == 0) { 1583 sub->src.port_op = 0; 1584 sub->src.port[0] = 0; 1585 sub->src.port[1] = 0; 1586 } 1587 if (super->dst.port_op == 0) { 1588 sub->dst.port_op = 0; 1589 sub->dst.port[0] = 0; 1590 sub->dst.port[1] = 0; 1591 } 1592 1593 if (super->src.addr.type == PF_ADDR_ADDRMASK && !super->src.neg && 1594 !sub->src.neg && super->src.addr.v.a.mask.addr32[0] == 0 && 1595 super->src.addr.v.a.mask.addr32[1] == 0 && 1596 super->src.addr.v.a.mask.addr32[2] == 0 && 1597 super->src.addr.v.a.mask.addr32[3] == 0) 1598 memset(&sub->src.addr, 0, sizeof(sub->src.addr)); 1599 else if (super->src.addr.type == PF_ADDR_ADDRMASK && 1600 sub->src.addr.type == PF_ADDR_ADDRMASK && 1601 super->src.neg == sub->src.neg && 1602 super->af == sub->af && 1603 unmask(&super->src.addr.v.a.mask, super->af) < 1604 unmask(&sub->src.addr.v.a.mask, sub->af) && 1605 super->src.addr.v.a.addr.addr32[0] == 1606 (sub->src.addr.v.a.addr.addr32[0] & 1607 super->src.addr.v.a.mask.addr32[0]) && 1608 super->src.addr.v.a.addr.addr32[1] == 1609 (sub->src.addr.v.a.addr.addr32[1] & 1610 super->src.addr.v.a.mask.addr32[1]) && 1611 super->src.addr.v.a.addr.addr32[2] == 1612 (sub->src.addr.v.a.addr.addr32[2] & 1613 super->src.addr.v.a.mask.addr32[2]) && 1614 super->src.addr.v.a.addr.addr32[3] == 1615 (sub->src.addr.v.a.addr.addr32[3] & 1616 super->src.addr.v.a.mask.addr32[3])) { 1617 /* sub->src.addr is a subset of super->src.addr/mask */ 1618 memcpy(&sub->src.addr, &super->src.addr, sizeof(sub->src.addr)); 1619 } 1620 1621 if (super->dst.addr.type == PF_ADDR_ADDRMASK && !super->dst.neg && 1622 !sub->dst.neg && super->dst.addr.v.a.mask.addr32[0] == 0 && 1623 super->dst.addr.v.a.mask.addr32[1] == 0 && 1624 super->dst.addr.v.a.mask.addr32[2] == 0 && 1625 super->dst.addr.v.a.mask.addr32[3] == 0) 1626 memset(&sub->dst.addr, 0, sizeof(sub->dst.addr)); 1627 else if (super->dst.addr.type == PF_ADDR_ADDRMASK && 1628 sub->dst.addr.type == PF_ADDR_ADDRMASK && 1629 super->dst.neg == sub->dst.neg && 1630 super->af == sub->af && 1631 unmask(&super->dst.addr.v.a.mask, super->af) < 1632 unmask(&sub->dst.addr.v.a.mask, sub->af) && 1633 super->dst.addr.v.a.addr.addr32[0] == 1634 (sub->dst.addr.v.a.addr.addr32[0] & 1635 super->dst.addr.v.a.mask.addr32[0]) && 1636 super->dst.addr.v.a.addr.addr32[1] == 1637 (sub->dst.addr.v.a.addr.addr32[1] & 1638 super->dst.addr.v.a.mask.addr32[1]) && 1639 super->dst.addr.v.a.addr.addr32[2] == 1640 (sub->dst.addr.v.a.addr.addr32[2] & 1641 super->dst.addr.v.a.mask.addr32[2]) && 1642 super->dst.addr.v.a.addr.addr32[3] == 1643 (sub->dst.addr.v.a.addr.addr32[3] & 1644 super->dst.addr.v.a.mask.addr32[3])) { 1645 /* sub->dst.addr is a subset of super->dst.addr/mask */ 1646 memcpy(&sub->dst.addr, &super->dst.addr, sizeof(sub->dst.addr)); 1647 } 1648 1649 if (super->af == 0) 1650 sub->af = 0; 1651 } 1652 1653 1654 void 1655 superblock_free(struct pfctl *pf, struct superblock *block) 1656 { 1657 struct pf_opt_rule *por; 1658 while ((por = TAILQ_FIRST(&block->sb_rules))) { 1659 TAILQ_REMOVE(&block->sb_rules, por, por_entry); 1660 if (por->por_src_tbl) { 1661 if (por->por_src_tbl->pt_buf) { 1662 pfr_buf_clear(por->por_src_tbl->pt_buf); 1663 free(por->por_src_tbl->pt_buf); 1664 } 1665 free(por->por_src_tbl); 1666 } 1667 if (por->por_dst_tbl) { 1668 if (por->por_dst_tbl->pt_buf) { 1669 pfr_buf_clear(por->por_dst_tbl->pt_buf); 1670 free(por->por_dst_tbl->pt_buf); 1671 } 1672 free(por->por_dst_tbl); 1673 } 1674 free(por); 1675 } 1676 if (block->sb_profiled_block) 1677 superblock_free(pf, block->sb_profiled_block); 1678 free(block); 1679 } 1680 1681