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