1 /* $OpenBSD: pfctl_optimize.c,v 1.40 2019/01/03 22:49:00 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 /* XXX pfctl_get_pool(pf->dev, &pr.rule.rpool, nr, pr.ticket, 897 * PF_PASS, pf->anchor) ??? 898 * ... pfctl_clear_pool(&pr.rule.rpool) 899 */ 900 } 901 902 if (construct_superblocks(pf, &queue, &prof_superblocks)) 903 return (1); 904 905 906 /* 907 * Now we try to associate the active ruleset's superblocks with 908 * the superblocks we're compiling. 909 */ 910 block = TAILQ_FIRST(superblocks); 911 blockcur = TAILQ_FIRST(&prof_superblocks); 912 while (block && blockcur) { 913 comparable_rule(&a, &TAILQ_FIRST(&block->sb_rules)->por_rule, 914 BREAK); 915 comparable_rule(&b, &TAILQ_FIRST(&blockcur->sb_rules)->por_rule, 916 BREAK); 917 if (memcmp(&a, &b, sizeof(a)) == 0) { 918 /* The two superblocks lined up */ 919 block->sb_profiled_block = blockcur; 920 } else { 921 DEBUG("superblocks don't line up between #%d and #%d", 922 TAILQ_FIRST(&block->sb_rules)->por_rule.nr, 923 TAILQ_FIRST(&blockcur->sb_rules)->por_rule.nr); 924 break; 925 } 926 block = TAILQ_NEXT(block, sb_entry); 927 blockcur = TAILQ_NEXT(blockcur, sb_entry); 928 } 929 930 931 932 /* Free any superblocks we couldn't link */ 933 while (blockcur) { 934 block = TAILQ_NEXT(blockcur, sb_entry); 935 superblock_free(pf, blockcur); 936 blockcur = block; 937 } 938 return (0); 939 } 940 941 942 /* 943 * Compare a rule to a skiplist to see if the rule is a member 944 */ 945 int 946 skip_compare(int skipnum, struct pf_skip_step *skiplist, 947 struct pf_opt_rule *por) 948 { 949 struct pf_rule *a, *b; 950 if (skipnum >= PF_SKIP_COUNT || skipnum < 0) 951 errx(1, "skip_compare() out of bounds"); 952 a = &por->por_rule; 953 b = &TAILQ_FIRST(&skiplist->ps_rules)->por_rule; 954 955 return ((skip_comparitors[skipnum])(a, b)); 956 } 957 958 959 /* 960 * Add a rule to a skiplist 961 */ 962 void 963 skip_append(struct superblock *superblock, int skipnum, 964 struct pf_skip_step *skiplist, struct pf_opt_rule *por) 965 { 966 struct pf_skip_step *prev; 967 968 skiplist->ps_count++; 969 TAILQ_INSERT_TAIL(&skiplist->ps_rules, por, por_skip_entry[skipnum]); 970 971 /* Keep the list of skiplists sorted by whichever is larger */ 972 while ((prev = TAILQ_PREV(skiplist, skiplist, ps_entry)) && 973 prev->ps_count < skiplist->ps_count) { 974 TAILQ_REMOVE(&superblock->sb_skipsteps[skipnum], 975 skiplist, ps_entry); 976 TAILQ_INSERT_BEFORE(prev, skiplist, ps_entry); 977 } 978 } 979 980 981 /* 982 * Remove a rule from the other skiplist calculations. 983 */ 984 void 985 remove_from_skipsteps(struct skiplist *head, struct superblock *block, 986 struct pf_opt_rule *por, struct pf_skip_step *active_list) 987 { 988 struct pf_skip_step *sk, *next; 989 struct pf_opt_rule *p2; 990 int i, found; 991 992 for (i = 0; i < PF_SKIP_COUNT; i++) { 993 sk = TAILQ_FIRST(&block->sb_skipsteps[i]); 994 if (sk == NULL || sk == active_list || sk->ps_count <= 1) 995 continue; 996 found = 0; 997 do { 998 TAILQ_FOREACH(p2, &sk->ps_rules, por_skip_entry[i]) 999 if (p2 == por) { 1000 TAILQ_REMOVE(&sk->ps_rules, p2, 1001 por_skip_entry[i]); 1002 found = 1; 1003 sk->ps_count--; 1004 break; 1005 } 1006 } while (!found && (sk = TAILQ_NEXT(sk, ps_entry))); 1007 if (found && sk) { 1008 /* Does this change the sorting order? */ 1009 while ((next = TAILQ_NEXT(sk, ps_entry)) && 1010 next->ps_count > sk->ps_count) { 1011 TAILQ_REMOVE(head, sk, ps_entry); 1012 TAILQ_INSERT_AFTER(head, next, sk, ps_entry); 1013 } 1014 #ifdef OPT_DEBUG 1015 next = TAILQ_NEXT(sk, ps_entry); 1016 assert(next == NULL || next->ps_count <= sk->ps_count); 1017 #endif /* OPT_DEBUG */ 1018 } 1019 } 1020 } 1021 1022 1023 /* Compare two rules AF field for skiplist construction */ 1024 int 1025 skip_cmp_af(struct pf_rule *a, struct pf_rule *b) 1026 { 1027 if (a->af != b->af || a->af == 0) 1028 return (1); 1029 return (0); 1030 } 1031 1032 /* Compare two rules DIRECTION field for skiplist construction */ 1033 int 1034 skip_cmp_dir(struct pf_rule *a, struct pf_rule *b) 1035 { 1036 if (a->direction == 0 || a->direction != b->direction) 1037 return (1); 1038 return (0); 1039 } 1040 1041 /* Compare two rules ON RDOMAIN field for skiplist construction */ 1042 int 1043 skip_cmp_rdom(struct pf_rule *a, struct pf_rule *b) 1044 { 1045 if (a->onrdomain == -1 || a->onrdomain != b->onrdomain) 1046 return (1); 1047 return (a->ifnot != b->ifnot); 1048 } 1049 1050 /* Compare two rules DST Address field for skiplist construction */ 1051 int 1052 skip_cmp_dst_addr(struct pf_rule *a, struct pf_rule *b) 1053 { 1054 if (a->dst.neg != b->dst.neg || 1055 a->dst.addr.type != b->dst.addr.type) 1056 return (1); 1057 /* XXX if (a->proto != b->proto && a->proto != 0 && b->proto != 0 1058 * && (a->proto == IPPROTO_TCP || a->proto == IPPROTO_UDP || 1059 * a->proto == IPPROTO_ICMP 1060 * return (1); 1061 */ 1062 switch (a->dst.addr.type) { 1063 case PF_ADDR_ADDRMASK: 1064 if (memcmp(&a->dst.addr.v.a.addr, &b->dst.addr.v.a.addr, 1065 sizeof(a->dst.addr.v.a.addr)) || 1066 memcmp(&a->dst.addr.v.a.mask, &b->dst.addr.v.a.mask, 1067 sizeof(a->dst.addr.v.a.mask)) || 1068 (a->dst.addr.v.a.addr.addr32[0] == 0 && 1069 a->dst.addr.v.a.addr.addr32[1] == 0 && 1070 a->dst.addr.v.a.addr.addr32[2] == 0 && 1071 a->dst.addr.v.a.addr.addr32[3] == 0)) 1072 return (1); 1073 return (0); 1074 case PF_ADDR_DYNIFTL: 1075 if (strcmp(a->dst.addr.v.ifname, b->dst.addr.v.ifname) != 0 || 1076 a->dst.addr.iflags != b->dst.addr.iflags || 1077 memcmp(&a->dst.addr.v.a.mask, &b->dst.addr.v.a.mask, 1078 sizeof(a->dst.addr.v.a.mask))) 1079 return (1); 1080 return (0); 1081 case PF_ADDR_NOROUTE: 1082 case PF_ADDR_URPFFAILED: 1083 return (0); 1084 case PF_ADDR_TABLE: 1085 return (strcmp(a->dst.addr.v.tblname, b->dst.addr.v.tblname)); 1086 } 1087 return (1); 1088 } 1089 1090 /* Compare two rules DST port field for skiplist construction */ 1091 int 1092 skip_cmp_dst_port(struct pf_rule *a, struct pf_rule *b) 1093 { 1094 /* XXX if (a->proto != b->proto && a->proto != 0 && b->proto != 0 1095 * && (a->proto == IPPROTO_TCP || a->proto == IPPROTO_UDP || 1096 * a->proto == IPPROTO_ICMP 1097 * return (1); 1098 */ 1099 if (a->dst.port_op == PF_OP_NONE || a->dst.port_op != b->dst.port_op || 1100 a->dst.port[0] != b->dst.port[0] || 1101 a->dst.port[1] != b->dst.port[1]) 1102 return (1); 1103 return (0); 1104 } 1105 1106 /* Compare two rules IFP field for skiplist construction */ 1107 int 1108 skip_cmp_ifp(struct pf_rule *a, struct pf_rule *b) 1109 { 1110 if (strcmp(a->ifname, b->ifname) || a->ifname[0] == '\0') 1111 return (1); 1112 return (a->ifnot != b->ifnot); 1113 } 1114 1115 /* Compare two rules PROTO field for skiplist construction */ 1116 int 1117 skip_cmp_proto(struct pf_rule *a, struct pf_rule *b) 1118 { 1119 return (a->proto != b->proto || a->proto == 0); 1120 } 1121 1122 /* Compare two rules SRC addr field for skiplist construction */ 1123 int 1124 skip_cmp_src_addr(struct pf_rule *a, struct pf_rule *b) 1125 { 1126 if (a->src.neg != b->src.neg || 1127 a->src.addr.type != b->src.addr.type) 1128 return (1); 1129 /* XXX if (a->proto != b->proto && a->proto != 0 && b->proto != 0 1130 * && (a->proto == IPPROTO_TCP || a->proto == IPPROTO_UDP || 1131 * a->proto == IPPROTO_ICMP 1132 * return (1); 1133 */ 1134 switch (a->src.addr.type) { 1135 case PF_ADDR_ADDRMASK: 1136 if (memcmp(&a->src.addr.v.a.addr, &b->src.addr.v.a.addr, 1137 sizeof(a->src.addr.v.a.addr)) || 1138 memcmp(&a->src.addr.v.a.mask, &b->src.addr.v.a.mask, 1139 sizeof(a->src.addr.v.a.mask)) || 1140 (a->src.addr.v.a.addr.addr32[0] == 0 && 1141 a->src.addr.v.a.addr.addr32[1] == 0 && 1142 a->src.addr.v.a.addr.addr32[2] == 0 && 1143 a->src.addr.v.a.addr.addr32[3] == 0)) 1144 return (1); 1145 return (0); 1146 case PF_ADDR_DYNIFTL: 1147 if (strcmp(a->src.addr.v.ifname, b->src.addr.v.ifname) != 0 || 1148 a->src.addr.iflags != b->src.addr.iflags || 1149 memcmp(&a->src.addr.v.a.mask, &b->src.addr.v.a.mask, 1150 sizeof(a->src.addr.v.a.mask))) 1151 return (1); 1152 return (0); 1153 case PF_ADDR_NOROUTE: 1154 case PF_ADDR_URPFFAILED: 1155 return (0); 1156 case PF_ADDR_TABLE: 1157 return (strcmp(a->src.addr.v.tblname, b->src.addr.v.tblname)); 1158 } 1159 return (1); 1160 } 1161 1162 /* Compare two rules SRC port field for skiplist construction */ 1163 int 1164 skip_cmp_src_port(struct pf_rule *a, struct pf_rule *b) 1165 { 1166 if (a->src.port_op == PF_OP_NONE || a->src.port_op != b->src.port_op || 1167 a->src.port[0] != b->src.port[0] || 1168 a->src.port[1] != b->src.port[1]) 1169 return (1); 1170 /* XXX if (a->proto != b->proto && a->proto != 0 && b->proto != 0 1171 * && (a->proto == IPPROTO_TCP || a->proto == IPPROTO_UDP || 1172 * a->proto == IPPROTO_ICMP 1173 * return (1); 1174 */ 1175 return (0); 1176 } 1177 1178 1179 void 1180 skip_init(void) 1181 { 1182 struct { 1183 char *name; 1184 int skipnum; 1185 int (*func)(struct pf_rule *, struct pf_rule *); 1186 } comps[] = PF_SKIP_COMPARITORS; 1187 int skipnum, i; 1188 1189 for (skipnum = 0; skipnum < PF_SKIP_COUNT; skipnum++) { 1190 for (i = 0; i < sizeof(comps)/sizeof(*comps); i++) 1191 if (comps[i].skipnum == skipnum) { 1192 skip_comparitors[skipnum] = comps[i].func; 1193 skip_comparitors_names[skipnum] = comps[i].name; 1194 } 1195 } 1196 for (skipnum = 0; skipnum < PF_SKIP_COUNT; skipnum++) 1197 if (skip_comparitors[skipnum] == NULL) 1198 errx(1, "Need to add skip step comparitor to pfctl?!"); 1199 } 1200 1201 /* 1202 * Add a host/netmask to a table 1203 */ 1204 int 1205 add_opt_table(struct pfctl *pf, struct pf_opt_tbl **tbl, sa_family_t af, 1206 struct pf_rule_addr *addr, char *ifname) 1207 { 1208 #ifdef OPT_DEBUG 1209 char buf[128]; 1210 #endif /* OPT_DEBUG */ 1211 static int tablenum = 0; 1212 struct node_host node_host; 1213 1214 if (*tbl == NULL) { 1215 if ((*tbl = calloc(1, sizeof(**tbl))) == NULL || 1216 ((*tbl)->pt_buf = calloc(1, sizeof(*(*tbl)->pt_buf))) == 1217 NULL) 1218 err(1, "calloc"); 1219 (*tbl)->pt_refcnt = 1; 1220 (*tbl)->pt_buf->pfrb_type = PFRB_ADDRS; 1221 SIMPLEQ_INIT(&(*tbl)->pt_nodes); 1222 1223 /* This is just a temporary table name */ 1224 snprintf((*tbl)->pt_name, sizeof((*tbl)->pt_name), "%s%d", 1225 PF_OPT_TABLE_PREFIX, tablenum++); 1226 DEBUG("creating table <%s>", (*tbl)->pt_name); 1227 } 1228 1229 memset(&node_host, 0, sizeof(node_host)); 1230 node_host.af = af; 1231 node_host.addr = addr->addr; 1232 node_host.ifname = ifname; 1233 node_host.weight = addr->weight; 1234 1235 DEBUG("<%s> adding %s/%d", (*tbl)->pt_name, inet_ntop(af, 1236 &node_host.addr.v.a.addr, buf, sizeof(buf)), 1237 unmask(&node_host.addr.v.a.mask)); 1238 1239 if (append_addr_host((*tbl)->pt_buf, &node_host, 0, 0)) { 1240 warn("failed to add host"); 1241 return (1); 1242 } 1243 if (pf->opts & PF_OPT_VERBOSE) { 1244 struct node_tinit *ti; 1245 1246 if ((ti = calloc(1, sizeof(*ti))) == NULL) 1247 err(1, "malloc"); 1248 if ((ti->host = malloc(sizeof(*ti->host))) == NULL) 1249 err(1, "malloc"); 1250 memcpy(ti->host, &node_host, sizeof(*ti->host)); 1251 SIMPLEQ_INSERT_TAIL(&(*tbl)->pt_nodes, ti, entries); 1252 } 1253 1254 (*tbl)->pt_rulecount++; 1255 if ((*tbl)->pt_rulecount == TABLE_THRESHOLD) 1256 DEBUG("table <%s> now faster than skip steps", (*tbl)->pt_name); 1257 1258 return (0); 1259 } 1260 1261 1262 /* 1263 * Do the dirty work of choosing an unused table name and creating it. 1264 * (be careful with the table name, it might already be used in another anchor) 1265 */ 1266 int 1267 pf_opt_create_table(struct pfctl *pf, struct pf_opt_tbl *tbl) 1268 { 1269 static int tablenum; 1270 struct pfr_table *t; 1271 1272 if (table_buffer.pfrb_type == 0) { 1273 /* Initialize the list of tables */ 1274 table_buffer.pfrb_type = PFRB_TABLES; 1275 for (;;) { 1276 pfr_buf_grow(&table_buffer, table_buffer.pfrb_size); 1277 table_buffer.pfrb_size = table_buffer.pfrb_msize; 1278 if (pfr_get_tables(NULL, table_buffer.pfrb_caddr, 1279 &table_buffer.pfrb_size, PFR_FLAG_ALLRSETS)) 1280 err(1, "pfr_get_tables"); 1281 if (table_buffer.pfrb_size <= table_buffer.pfrb_msize) 1282 break; 1283 } 1284 table_identifier = arc4random(); 1285 } 1286 1287 /* XXX would be *really* nice to avoid duplicating identical tables */ 1288 1289 /* Now we have to pick a table name that isn't used */ 1290 again: 1291 DEBUG("translating temporary table <%s> to <%s%x_%d>", tbl->pt_name, 1292 PF_OPT_TABLE_PREFIX, table_identifier, tablenum); 1293 snprintf(tbl->pt_name, sizeof(tbl->pt_name), "%s%x_%d", 1294 PF_OPT_TABLE_PREFIX, table_identifier, tablenum); 1295 PFRB_FOREACH(t, &table_buffer) { 1296 if (strcasecmp(t->pfrt_name, tbl->pt_name) == 0) { 1297 /* Collision. Try again */ 1298 DEBUG("wow, table <%s> in use. trying again", 1299 tbl->pt_name); 1300 table_identifier = arc4random(); 1301 goto again; 1302 } 1303 } 1304 tablenum++; 1305 1306 if (pfctl_define_table(tbl->pt_name, PFR_TFLAG_CONST | tbl->pt_flags, 1, 1307 pf->astack[0]->path, tbl->pt_buf, pf->astack[0]->ruleset.tticket)) { 1308 warn("failed to create table %s in %s", 1309 tbl->pt_name, pf->astack[0]->name); 1310 return (1); 1311 } 1312 return (0); 1313 } 1314 1315 /* 1316 * Partition the flat ruleset into a list of distinct superblocks 1317 */ 1318 int 1319 construct_superblocks(struct pfctl *pf, struct pf_opt_queue *opt_queue, 1320 struct superblocks *superblocks) 1321 { 1322 struct superblock *block = NULL; 1323 struct pf_opt_rule *por; 1324 int i; 1325 1326 while (!TAILQ_EMPTY(opt_queue)) { 1327 por = TAILQ_FIRST(opt_queue); 1328 TAILQ_REMOVE(opt_queue, por, por_entry); 1329 if (block == NULL || !superblock_inclusive(block, por)) { 1330 if ((block = calloc(1, sizeof(*block))) == NULL) { 1331 warn("calloc"); 1332 return (1); 1333 } 1334 TAILQ_INIT(&block->sb_rules); 1335 for (i = 0; i < PF_SKIP_COUNT; i++) 1336 TAILQ_INIT(&block->sb_skipsteps[i]); 1337 TAILQ_INSERT_TAIL(superblocks, block, sb_entry); 1338 } 1339 TAILQ_INSERT_TAIL(&block->sb_rules, por, por_entry); 1340 } 1341 1342 return (0); 1343 } 1344 1345 1346 /* 1347 * Compare two rule addresses 1348 */ 1349 int 1350 addrs_equal(struct pf_rule_addr *a, struct pf_rule_addr *b) 1351 { 1352 if (a->neg != b->neg) 1353 return (0); 1354 return (memcmp(&a->addr, &b->addr, sizeof(a->addr)) == 0); 1355 } 1356 1357 1358 /* 1359 * The addresses are not equal, but can we combine them into one table? 1360 */ 1361 int 1362 addrs_combineable(struct pf_rule_addr *a, struct pf_rule_addr *b) 1363 { 1364 if (a->addr.type != PF_ADDR_ADDRMASK || 1365 b->addr.type != PF_ADDR_ADDRMASK) 1366 return (0); 1367 if (a->neg != b->neg || a->port_op != b->port_op || 1368 a->port[0] != b->port[0] || a->port[1] != b->port[1]) 1369 return (0); 1370 return (1); 1371 } 1372 1373 1374 /* 1375 * Are we allowed to combine these two rules 1376 */ 1377 int 1378 rules_combineable(struct pf_rule *p1, struct pf_rule *p2) 1379 { 1380 struct pf_rule a, b; 1381 1382 comparable_rule(&a, p1, COMBINED); 1383 comparable_rule(&b, p2, COMBINED); 1384 return (memcmp(&a, &b, sizeof(a)) == 0); 1385 } 1386 1387 1388 /* 1389 * Can a rule be included inside a superblock 1390 */ 1391 int 1392 superblock_inclusive(struct superblock *block, struct pf_opt_rule *por) 1393 { 1394 struct pf_rule a, b; 1395 int i, j; 1396 1397 /* First check for hard breaks */ 1398 for (i = 0; i < sizeof(pf_rule_desc)/sizeof(*pf_rule_desc); i++) { 1399 if (pf_rule_desc[i].prf_type == BARRIER) { 1400 for (j = 0; j < pf_rule_desc[i].prf_size; j++) 1401 if (((char *)&por->por_rule)[j + 1402 pf_rule_desc[i].prf_offset] != 0) 1403 return (0); 1404 } 1405 } 1406 1407 /* per-rule src-track is also a hard break */ 1408 if (por->por_rule.rule_flag & PFRULE_RULESRCTRACK) 1409 return (0); 1410 1411 /* 1412 * Have to handle interface groups separately. Consider the following 1413 * rules: 1414 * block on EXTIFS to any port 22 1415 * pass on em0 to any port 22 1416 * (where EXTIFS is an arbitrary interface group) 1417 * The optimizer may decide to re-order the pass rule in front of the 1418 * block rule. But what if EXTIFS includes em0??? Such a reordering 1419 * would change the meaning of the ruleset. 1420 * We can't just lookup the EXTIFS group and check if em0 is a member 1421 * because the user is allowed to add interfaces to a group during 1422 * runtime. 1423 * Ergo interface groups become a defacto superblock break :-( 1424 */ 1425 if (interface_group(por->por_rule.ifname) || 1426 interface_group(TAILQ_FIRST(&block->sb_rules)->por_rule.ifname)) { 1427 if (strcasecmp(por->por_rule.ifname, 1428 TAILQ_FIRST(&block->sb_rules)->por_rule.ifname) != 0) 1429 return (0); 1430 } 1431 1432 comparable_rule(&a, &TAILQ_FIRST(&block->sb_rules)->por_rule, NOMERGE); 1433 comparable_rule(&b, &por->por_rule, NOMERGE); 1434 if (memcmp(&a, &b, sizeof(a)) == 0) 1435 return (1); 1436 1437 #ifdef OPT_DEBUG 1438 for (i = 0; i < sizeof(por->por_rule); i++) { 1439 int closest = -1; 1440 if (((u_int8_t *)&a)[i] != ((u_int8_t *)&b)[i]) { 1441 for (j = 0; j < sizeof(pf_rule_desc) / 1442 sizeof(*pf_rule_desc); j++) { 1443 if (i >= pf_rule_desc[j].prf_offset && 1444 i < pf_rule_desc[j].prf_offset + 1445 pf_rule_desc[j].prf_size) { 1446 DEBUG("superblock break @ %d due to %s", 1447 por->por_rule.nr, 1448 pf_rule_desc[j].prf_name); 1449 return (0); 1450 } 1451 if (i > pf_rule_desc[j].prf_offset) { 1452 if (closest == -1 || 1453 i-pf_rule_desc[j].prf_offset < 1454 i-pf_rule_desc[closest].prf_offset) 1455 closest = j; 1456 } 1457 } 1458 1459 if (closest >= 0) 1460 DEBUG("superblock break @ %d on %s+%lxh", 1461 por->por_rule.nr, 1462 pf_rule_desc[closest].prf_name, 1463 i - pf_rule_desc[closest].prf_offset - 1464 pf_rule_desc[closest].prf_size); 1465 else 1466 DEBUG("superblock break @ %d on field @ %d", 1467 por->por_rule.nr, i); 1468 return (0); 1469 } 1470 } 1471 #endif /* OPT_DEBUG */ 1472 1473 return (0); 1474 } 1475 1476 1477 /* 1478 * Figure out if an interface name is an actual interface or actually a 1479 * group of interfaces. 1480 */ 1481 int 1482 interface_group(const char *ifname) 1483 { 1484 if (ifname == NULL || !ifname[0]) 1485 return (0); 1486 1487 /* Real interfaces must end in a number, interface groups do not */ 1488 if (isdigit((unsigned char)ifname[strlen(ifname) - 1])) 1489 return (0); 1490 else 1491 return (1); 1492 } 1493 1494 1495 /* 1496 * Make a rule that can directly compared by memcmp() 1497 */ 1498 void 1499 comparable_rule(struct pf_rule *dst, const struct pf_rule *src, int type) 1500 { 1501 int i; 1502 /* 1503 * To simplify the comparison, we just zero out the fields that are 1504 * allowed to be different and then do a simple memcmp() 1505 */ 1506 memcpy(dst, src, sizeof(*dst)); 1507 for (i = 0; i < sizeof(pf_rule_desc)/sizeof(*pf_rule_desc); i++) 1508 if (pf_rule_desc[i].prf_type >= type) { 1509 #ifdef OPT_DEBUG 1510 assert(pf_rule_desc[i].prf_type != NEVER || 1511 *(((char *)dst) + pf_rule_desc[i].prf_offset) == 0); 1512 #endif /* OPT_DEBUG */ 1513 memset(((char *)dst) + pf_rule_desc[i].prf_offset, 0, 1514 pf_rule_desc[i].prf_size); 1515 } 1516 } 1517 1518 1519 /* 1520 * Remove superset information from two rules so we can directly compare them 1521 * with memcmp() 1522 */ 1523 void 1524 exclude_supersets(struct pf_rule *super, struct pf_rule *sub) 1525 { 1526 if (super->ifname[0] == '\0') 1527 memset(sub->ifname, 0, sizeof(sub->ifname)); 1528 if (super->direction == PF_INOUT) 1529 sub->direction = PF_INOUT; 1530 if ((super->proto == 0 || super->proto == sub->proto) && 1531 super->flags == 0 && super->flagset == 0 && (sub->flags || 1532 sub->flagset)) { 1533 sub->flags = super->flags; 1534 sub->flagset = super->flagset; 1535 } 1536 if (super->proto == 0) 1537 sub->proto = 0; 1538 1539 if (super->src.port_op == 0) { 1540 sub->src.port_op = 0; 1541 sub->src.port[0] = 0; 1542 sub->src.port[1] = 0; 1543 } 1544 if (super->dst.port_op == 0) { 1545 sub->dst.port_op = 0; 1546 sub->dst.port[0] = 0; 1547 sub->dst.port[1] = 0; 1548 } 1549 1550 if (super->src.addr.type == PF_ADDR_ADDRMASK && !super->src.neg && 1551 !sub->src.neg && super->src.addr.v.a.mask.addr32[0] == 0 && 1552 super->src.addr.v.a.mask.addr32[1] == 0 && 1553 super->src.addr.v.a.mask.addr32[2] == 0 && 1554 super->src.addr.v.a.mask.addr32[3] == 0) 1555 memset(&sub->src.addr, 0, sizeof(sub->src.addr)); 1556 else if (super->src.addr.type == PF_ADDR_ADDRMASK && 1557 sub->src.addr.type == PF_ADDR_ADDRMASK && 1558 super->src.neg == sub->src.neg && 1559 super->af == sub->af && 1560 unmask(&super->src.addr.v.a.mask) < 1561 unmask(&sub->src.addr.v.a.mask) && 1562 super->src.addr.v.a.addr.addr32[0] == 1563 (sub->src.addr.v.a.addr.addr32[0] & 1564 super->src.addr.v.a.mask.addr32[0]) && 1565 super->src.addr.v.a.addr.addr32[1] == 1566 (sub->src.addr.v.a.addr.addr32[1] & 1567 super->src.addr.v.a.mask.addr32[1]) && 1568 super->src.addr.v.a.addr.addr32[2] == 1569 (sub->src.addr.v.a.addr.addr32[2] & 1570 super->src.addr.v.a.mask.addr32[2]) && 1571 super->src.addr.v.a.addr.addr32[3] == 1572 (sub->src.addr.v.a.addr.addr32[3] & 1573 super->src.addr.v.a.mask.addr32[3])) { 1574 /* sub->src.addr is a subset of super->src.addr/mask */ 1575 memcpy(&sub->src.addr, &super->src.addr, sizeof(sub->src.addr)); 1576 } 1577 1578 if (super->dst.addr.type == PF_ADDR_ADDRMASK && !super->dst.neg && 1579 !sub->dst.neg && super->dst.addr.v.a.mask.addr32[0] == 0 && 1580 super->dst.addr.v.a.mask.addr32[1] == 0 && 1581 super->dst.addr.v.a.mask.addr32[2] == 0 && 1582 super->dst.addr.v.a.mask.addr32[3] == 0) 1583 memset(&sub->dst.addr, 0, sizeof(sub->dst.addr)); 1584 else if (super->dst.addr.type == PF_ADDR_ADDRMASK && 1585 sub->dst.addr.type == PF_ADDR_ADDRMASK && 1586 super->dst.neg == sub->dst.neg && 1587 super->af == sub->af && 1588 unmask(&super->dst.addr.v.a.mask) < 1589 unmask(&sub->dst.addr.v.a.mask) && 1590 super->dst.addr.v.a.addr.addr32[0] == 1591 (sub->dst.addr.v.a.addr.addr32[0] & 1592 super->dst.addr.v.a.mask.addr32[0]) && 1593 super->dst.addr.v.a.addr.addr32[1] == 1594 (sub->dst.addr.v.a.addr.addr32[1] & 1595 super->dst.addr.v.a.mask.addr32[1]) && 1596 super->dst.addr.v.a.addr.addr32[2] == 1597 (sub->dst.addr.v.a.addr.addr32[2] & 1598 super->dst.addr.v.a.mask.addr32[2]) && 1599 super->dst.addr.v.a.addr.addr32[3] == 1600 (sub->dst.addr.v.a.addr.addr32[3] & 1601 super->dst.addr.v.a.mask.addr32[3])) { 1602 /* sub->dst.addr is a subset of super->dst.addr/mask */ 1603 memcpy(&sub->dst.addr, &super->dst.addr, sizeof(sub->dst.addr)); 1604 } 1605 1606 if (super->af == 0) 1607 sub->af = 0; 1608 } 1609 1610 1611 void 1612 superblock_free(struct pfctl *pf, struct superblock *block) 1613 { 1614 struct pf_opt_rule *por; 1615 while ((por = TAILQ_FIRST(&block->sb_rules))) { 1616 TAILQ_REMOVE(&block->sb_rules, por, por_entry); 1617 pf_opt_table_unref(por->por_src_tbl); 1618 pf_opt_table_unref(por->por_dst_tbl); 1619 free(por); 1620 } 1621 if (block->sb_profiled_block) 1622 superblock_free(pf, block->sb_profiled_block); 1623 free(block); 1624 } 1625 1626 struct pf_opt_tbl * 1627 pf_opt_table_ref(struct pf_opt_tbl *pt) 1628 { 1629 /* parser does not run concurrently, we don't need atomic ops. */ 1630 if (pt != NULL) 1631 pt->pt_refcnt++; 1632 1633 return (pt); 1634 } 1635 1636 void 1637 pf_opt_table_unref(struct pf_opt_tbl *pt) 1638 { 1639 if ((pt != NULL) && ((--pt->pt_refcnt) == 0)) { 1640 if (pt->pt_buf != NULL) { 1641 pfr_buf_clear(pt->pt_buf); 1642 free(pt->pt_buf); 1643 } 1644 free(pt); 1645 } 1646 } 1647