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