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