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