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