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