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