xref: /freebsd/sbin/pfctl/pfctl_optimize.c (revision 9f21a946d01690cdfba43beba60ab04fc884741a)
13b3a8eb9SGleb Smirnoff /*	$OpenBSD: pfctl_optimize.c,v 1.17 2008/05/06 03:45:21 mpf Exp $ */
23b3a8eb9SGleb Smirnoff 
33b3a8eb9SGleb Smirnoff /*
43b3a8eb9SGleb Smirnoff  * Copyright (c) 2004 Mike Frantzen <frantzen@openbsd.org>
53b3a8eb9SGleb Smirnoff  *
63b3a8eb9SGleb Smirnoff  * Permission to use, copy, modify, and distribute this software for any
73b3a8eb9SGleb Smirnoff  * purpose with or without fee is hereby granted, provided that the above
83b3a8eb9SGleb Smirnoff  * copyright notice and this permission notice appear in all copies.
93b3a8eb9SGleb Smirnoff  *
103b3a8eb9SGleb Smirnoff  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
113b3a8eb9SGleb Smirnoff  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
123b3a8eb9SGleb Smirnoff  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
133b3a8eb9SGleb Smirnoff  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
143b3a8eb9SGleb Smirnoff  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
153b3a8eb9SGleb Smirnoff  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
163b3a8eb9SGleb Smirnoff  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
173b3a8eb9SGleb Smirnoff  */
183b3a8eb9SGleb Smirnoff 
193b3a8eb9SGleb Smirnoff #include <sys/types.h>
203b3a8eb9SGleb Smirnoff #include <sys/ioctl.h>
213b3a8eb9SGleb Smirnoff #include <sys/socket.h>
223b3a8eb9SGleb Smirnoff 
233b3a8eb9SGleb Smirnoff #include <net/if.h>
243b3a8eb9SGleb Smirnoff #include <net/pfvar.h>
253b3a8eb9SGleb Smirnoff 
263b3a8eb9SGleb Smirnoff #include <netinet/in.h>
273b3a8eb9SGleb Smirnoff #include <arpa/inet.h>
283b3a8eb9SGleb Smirnoff 
293b3a8eb9SGleb Smirnoff #include <assert.h>
303b3a8eb9SGleb Smirnoff #include <ctype.h>
313b3a8eb9SGleb Smirnoff #include <err.h>
323b3a8eb9SGleb Smirnoff #include <errno.h>
330d71f9f3SKristof Provost #include <libpfctl.h>
343b3a8eb9SGleb Smirnoff #include <stddef.h>
353b3a8eb9SGleb Smirnoff #include <stdio.h>
363b3a8eb9SGleb Smirnoff #include <stdlib.h>
373b3a8eb9SGleb Smirnoff #include <string.h>
383b3a8eb9SGleb Smirnoff 
393b3a8eb9SGleb Smirnoff #include "pfctl_parser.h"
403b3a8eb9SGleb Smirnoff #include "pfctl.h"
413b3a8eb9SGleb Smirnoff 
423b3a8eb9SGleb Smirnoff /* The size at which a table becomes faster than individual rules */
433b3a8eb9SGleb Smirnoff #define TABLE_THRESHOLD		6
443b3a8eb9SGleb Smirnoff 
453b3a8eb9SGleb Smirnoff 
463b3a8eb9SGleb Smirnoff /* #define OPT_DEBUG	1 */
473b3a8eb9SGleb Smirnoff #ifdef OPT_DEBUG
483b3a8eb9SGleb Smirnoff # define DEBUG(str, v...) \
493b3a8eb9SGleb Smirnoff 	printf("%s: " str "\n", __FUNCTION__ , ## v)
503b3a8eb9SGleb Smirnoff #else
513b3a8eb9SGleb Smirnoff # define DEBUG(str, v...) ((void)0)
523b3a8eb9SGleb Smirnoff #endif
533b3a8eb9SGleb Smirnoff 
543b3a8eb9SGleb Smirnoff 
553b3a8eb9SGleb Smirnoff /*
563b3a8eb9SGleb Smirnoff  * A container that lets us sort a superblock to optimize the skip step jumps
573b3a8eb9SGleb Smirnoff  */
583b3a8eb9SGleb Smirnoff struct pf_skip_step {
593b3a8eb9SGleb Smirnoff 	int				ps_count;	/* number of items */
603b3a8eb9SGleb Smirnoff 	TAILQ_HEAD( , pf_opt_rule)	ps_rules;
613b3a8eb9SGleb Smirnoff 	TAILQ_ENTRY(pf_skip_step)	ps_entry;
623b3a8eb9SGleb Smirnoff };
633b3a8eb9SGleb Smirnoff 
643b3a8eb9SGleb Smirnoff 
653b3a8eb9SGleb Smirnoff /*
663b3a8eb9SGleb Smirnoff  * A superblock is a block of adjacent rules of similar action.  If there
673b3a8eb9SGleb Smirnoff  * are five PASS rules in a row, they all become members of a superblock.
683b3a8eb9SGleb Smirnoff  * Once we have a superblock, we are free to re-order any rules within it
693b3a8eb9SGleb Smirnoff  * in order to improve performance; if a packet is passed, it doesn't matter
703b3a8eb9SGleb Smirnoff  * who passed it.
713b3a8eb9SGleb Smirnoff  */
723b3a8eb9SGleb Smirnoff struct superblock {
733b3a8eb9SGleb Smirnoff 	TAILQ_HEAD( , pf_opt_rule)		 sb_rules;
743b3a8eb9SGleb Smirnoff 	TAILQ_ENTRY(superblock)			 sb_entry;
753b3a8eb9SGleb Smirnoff 	struct superblock			*sb_profiled_block;
763b3a8eb9SGleb Smirnoff 	TAILQ_HEAD(skiplist, pf_skip_step)	 sb_skipsteps[PF_SKIP_COUNT];
773b3a8eb9SGleb Smirnoff };
783b3a8eb9SGleb Smirnoff TAILQ_HEAD(superblocks, superblock);
793b3a8eb9SGleb Smirnoff 
803b3a8eb9SGleb Smirnoff 
813b3a8eb9SGleb Smirnoff /*
823b3a8eb9SGleb Smirnoff  * Description of the PF rule structure.
833b3a8eb9SGleb Smirnoff  */
843b3a8eb9SGleb Smirnoff enum {
8528323addSBryan Drewery     BARRIER,	/* the presence of the field puts the rule in its own block */
863b3a8eb9SGleb Smirnoff     BREAK,	/* the field may not differ between rules in a superblock */
873b3a8eb9SGleb Smirnoff     NOMERGE,	/* the field may not differ between rules when combined */
883b3a8eb9SGleb Smirnoff     COMBINED,	/* the field may itself be combined with other rules */
893b3a8eb9SGleb Smirnoff     DC,		/* we just don't care about the field */
903b3a8eb9SGleb Smirnoff     NEVER};	/* we should never see this field set?!? */
9113cfafabSKristof Provost static struct pf_rule_field {
923b3a8eb9SGleb Smirnoff 	const char	*prf_name;
933b3a8eb9SGleb Smirnoff 	int		 prf_type;
943b3a8eb9SGleb Smirnoff 	size_t		 prf_offset;
953b3a8eb9SGleb Smirnoff 	size_t		 prf_size;
963b3a8eb9SGleb Smirnoff } pf_rule_desc[] = {
973b3a8eb9SGleb Smirnoff #define PF_RULE_FIELD(field, ty)	\
983b3a8eb9SGleb Smirnoff     {#field,				\
993b3a8eb9SGleb Smirnoff     ty,					\
100e9eb0941SKristof Provost     offsetof(struct pfctl_rule, field),	\
101e9eb0941SKristof Provost     sizeof(((struct pfctl_rule *)0)->field)}
1023b3a8eb9SGleb Smirnoff 
1033b3a8eb9SGleb Smirnoff 
1043b3a8eb9SGleb Smirnoff     /*
10528323addSBryan Drewery      * The presence of these fields in a rule put the rule in its own
1063b3a8eb9SGleb Smirnoff      * superblock.  Thus it will not be optimized.  It also prevents the
1073b3a8eb9SGleb Smirnoff      * rule from being re-ordered at all.
1083b3a8eb9SGleb Smirnoff      */
1093b3a8eb9SGleb Smirnoff     PF_RULE_FIELD(label,		BARRIER),
1103b3a8eb9SGleb Smirnoff     PF_RULE_FIELD(prob,			BARRIER),
1113b3a8eb9SGleb Smirnoff     PF_RULE_FIELD(max_states,		BARRIER),
1123b3a8eb9SGleb Smirnoff     PF_RULE_FIELD(max_src_nodes,	BARRIER),
1133b3a8eb9SGleb Smirnoff     PF_RULE_FIELD(max_src_states,	BARRIER),
1143b3a8eb9SGleb Smirnoff     PF_RULE_FIELD(max_src_conn,		BARRIER),
1153b3a8eb9SGleb Smirnoff     PF_RULE_FIELD(max_src_conn_rate,	BARRIER),
1163b3a8eb9SGleb Smirnoff     PF_RULE_FIELD(anchor,		BARRIER),	/* for now */
1173b3a8eb9SGleb Smirnoff 
1183b3a8eb9SGleb Smirnoff     /*
1193b3a8eb9SGleb Smirnoff      * These fields must be the same between all rules in the same superblock.
1203b3a8eb9SGleb Smirnoff      * These rules are allowed to be re-ordered but only among like rules.
1213b3a8eb9SGleb Smirnoff      * For instance we can re-order all 'tag "foo"' rules because they have the
1223b3a8eb9SGleb Smirnoff      * same tag.  But we can not re-order between a 'tag "foo"' and a
1233b3a8eb9SGleb Smirnoff      * 'tag "bar"' since that would change the meaning of the ruleset.
1243b3a8eb9SGleb Smirnoff      */
1253b3a8eb9SGleb Smirnoff     PF_RULE_FIELD(tagname,		BREAK),
1263b3a8eb9SGleb Smirnoff     PF_RULE_FIELD(keep_state,		BREAK),
1273b3a8eb9SGleb Smirnoff     PF_RULE_FIELD(qname,		BREAK),
1283b3a8eb9SGleb Smirnoff     PF_RULE_FIELD(pqname,		BREAK),
1293b3a8eb9SGleb Smirnoff     PF_RULE_FIELD(rt,			BREAK),
1303b3a8eb9SGleb Smirnoff     PF_RULE_FIELD(allow_opts,		BREAK),
1313b3a8eb9SGleb Smirnoff     PF_RULE_FIELD(rule_flag,		BREAK),
1323b3a8eb9SGleb Smirnoff     PF_RULE_FIELD(action,		BREAK),
1333b3a8eb9SGleb Smirnoff     PF_RULE_FIELD(log,			BREAK),
1343b3a8eb9SGleb Smirnoff     PF_RULE_FIELD(quick,		BREAK),
1353b3a8eb9SGleb Smirnoff     PF_RULE_FIELD(return_ttl,		BREAK),
1363b3a8eb9SGleb Smirnoff     PF_RULE_FIELD(overload_tblname,	BREAK),
1373b3a8eb9SGleb Smirnoff     PF_RULE_FIELD(flush,		BREAK),
138096efeb6SKristof Provost     PF_RULE_FIELD(rdr,			BREAK),
1395cb08fddSKristof Provost     PF_RULE_FIELD(nat,			BREAK),
1400972294eSKristof Provost     PF_RULE_FIELD(route,		BREAK),
1413b3a8eb9SGleb Smirnoff     PF_RULE_FIELD(logif,		BREAK),
1423b3a8eb9SGleb Smirnoff 
1433b3a8eb9SGleb Smirnoff     /*
1443b3a8eb9SGleb Smirnoff      * Any fields not listed in this structure act as BREAK fields
1453b3a8eb9SGleb Smirnoff      */
1463b3a8eb9SGleb Smirnoff 
1473b3a8eb9SGleb Smirnoff 
1483b3a8eb9SGleb Smirnoff     /*
1493b3a8eb9SGleb Smirnoff      * These fields must not differ when we merge two rules together but
1503b3a8eb9SGleb Smirnoff      * their difference isn't enough to put the rules in different superblocks.
1513b3a8eb9SGleb Smirnoff      * There are no problems re-ordering any rules with these fields.
1523b3a8eb9SGleb Smirnoff      */
1533b3a8eb9SGleb Smirnoff     PF_RULE_FIELD(af,			NOMERGE),
1543b3a8eb9SGleb Smirnoff     PF_RULE_FIELD(ifnot,		NOMERGE),
1553b3a8eb9SGleb Smirnoff     PF_RULE_FIELD(ifname,		NOMERGE),	/* hack for IF groups */
1563b3a8eb9SGleb Smirnoff     PF_RULE_FIELD(match_tag_not,	NOMERGE),
1573b3a8eb9SGleb Smirnoff     PF_RULE_FIELD(match_tagname,	NOMERGE),
1583b3a8eb9SGleb Smirnoff     PF_RULE_FIELD(os_fingerprint,	NOMERGE),
1593b3a8eb9SGleb Smirnoff     PF_RULE_FIELD(timeout,		NOMERGE),
1603b3a8eb9SGleb Smirnoff     PF_RULE_FIELD(return_icmp,		NOMERGE),
1613b3a8eb9SGleb Smirnoff     PF_RULE_FIELD(return_icmp6,		NOMERGE),
1623b3a8eb9SGleb Smirnoff     PF_RULE_FIELD(uid,			NOMERGE),
1633b3a8eb9SGleb Smirnoff     PF_RULE_FIELD(gid,			NOMERGE),
1643b3a8eb9SGleb Smirnoff     PF_RULE_FIELD(direction,		NOMERGE),
1653b3a8eb9SGleb Smirnoff     PF_RULE_FIELD(proto,		NOMERGE),
1663b3a8eb9SGleb Smirnoff     PF_RULE_FIELD(type,			NOMERGE),
1673b3a8eb9SGleb Smirnoff     PF_RULE_FIELD(code,			NOMERGE),
1683b3a8eb9SGleb Smirnoff     PF_RULE_FIELD(flags,		NOMERGE),
1693b3a8eb9SGleb Smirnoff     PF_RULE_FIELD(flagset,		NOMERGE),
1703b3a8eb9SGleb Smirnoff     PF_RULE_FIELD(tos,			NOMERGE),
1713b3a8eb9SGleb Smirnoff     PF_RULE_FIELD(src.port,		NOMERGE),
1723b3a8eb9SGleb Smirnoff     PF_RULE_FIELD(dst.port,		NOMERGE),
1733b3a8eb9SGleb Smirnoff     PF_RULE_FIELD(src.port_op,		NOMERGE),
1743b3a8eb9SGleb Smirnoff     PF_RULE_FIELD(dst.port_op,		NOMERGE),
1753b3a8eb9SGleb Smirnoff     PF_RULE_FIELD(src.neg,		NOMERGE),
1763b3a8eb9SGleb Smirnoff     PF_RULE_FIELD(dst.neg,		NOMERGE),
1770d68985bSKristof Provost     PF_RULE_FIELD(af,			NOMERGE),
1783b3a8eb9SGleb Smirnoff 
1793b3a8eb9SGleb Smirnoff     /* These fields can be merged */
1803b3a8eb9SGleb Smirnoff     PF_RULE_FIELD(src.addr,		COMBINED),
1813b3a8eb9SGleb Smirnoff     PF_RULE_FIELD(dst.addr,		COMBINED),
1823b3a8eb9SGleb Smirnoff 
1833b3a8eb9SGleb Smirnoff     /* We just don't care about these fields.  They're set by the kernel */
1843b3a8eb9SGleb Smirnoff     PF_RULE_FIELD(skip,			DC),
1853b3a8eb9SGleb Smirnoff     PF_RULE_FIELD(evaluations,		DC),
1863b3a8eb9SGleb Smirnoff     PF_RULE_FIELD(packets,		DC),
1873b3a8eb9SGleb Smirnoff     PF_RULE_FIELD(bytes,		DC),
1883b3a8eb9SGleb Smirnoff     PF_RULE_FIELD(kif,			DC),
1893b3a8eb9SGleb Smirnoff     PF_RULE_FIELD(states_cur,		DC),
1903b3a8eb9SGleb Smirnoff     PF_RULE_FIELD(states_tot,		DC),
1913b3a8eb9SGleb Smirnoff     PF_RULE_FIELD(src_nodes,		DC),
1923b3a8eb9SGleb Smirnoff     PF_RULE_FIELD(nr,			DC),
1933b3a8eb9SGleb Smirnoff     PF_RULE_FIELD(entries,		DC),
1943b3a8eb9SGleb Smirnoff     PF_RULE_FIELD(qid,			DC),
1953b3a8eb9SGleb Smirnoff     PF_RULE_FIELD(pqid,			DC),
1963b3a8eb9SGleb Smirnoff     PF_RULE_FIELD(anchor_relative,	DC),
1973b3a8eb9SGleb Smirnoff     PF_RULE_FIELD(anchor_wildcard,	DC),
1983b3a8eb9SGleb Smirnoff     PF_RULE_FIELD(tag,			DC),
1993b3a8eb9SGleb Smirnoff     PF_RULE_FIELD(match_tag,		DC),
2003b3a8eb9SGleb Smirnoff     PF_RULE_FIELD(overload_tbl,		DC),
2013b3a8eb9SGleb Smirnoff 
2023b3a8eb9SGleb Smirnoff     /* These fields should never be set in a PASS/BLOCK rule */
2033b3a8eb9SGleb Smirnoff     PF_RULE_FIELD(natpass,		NEVER),
2043b3a8eb9SGleb Smirnoff     PF_RULE_FIELD(max_mss,		NEVER),
2053b3a8eb9SGleb Smirnoff     PF_RULE_FIELD(min_ttl,		NEVER),
2063b3a8eb9SGleb Smirnoff     PF_RULE_FIELD(set_tos,		NEVER),
2073b3a8eb9SGleb Smirnoff };
2083b3a8eb9SGleb Smirnoff 
2093b3a8eb9SGleb Smirnoff 
2103b3a8eb9SGleb Smirnoff 
2113b3a8eb9SGleb Smirnoff int	add_opt_table(struct pfctl *, struct pf_opt_tbl **, sa_family_t,
2123b3a8eb9SGleb Smirnoff 	    struct pf_rule_addr *);
2133b3a8eb9SGleb Smirnoff int	addrs_combineable(struct pf_rule_addr *, struct pf_rule_addr *);
2143b3a8eb9SGleb Smirnoff int	addrs_equal(struct pf_rule_addr *, struct pf_rule_addr *);
2153b3a8eb9SGleb Smirnoff int	block_feedback(struct pfctl *, struct superblock *);
2163b3a8eb9SGleb Smirnoff int	combine_rules(struct pfctl *, struct superblock *);
217e9eb0941SKristof Provost void	comparable_rule(struct pfctl_rule *, const struct pfctl_rule *, int);
2183b3a8eb9SGleb Smirnoff int	construct_superblocks(struct pfctl *, struct pf_opt_queue *,
2193b3a8eb9SGleb Smirnoff 	    struct superblocks *);
220e9eb0941SKristof Provost void	exclude_supersets(struct pfctl_rule *, struct pfctl_rule *);
2213b3a8eb9SGleb Smirnoff int	interface_group(const char *);
2223b3a8eb9SGleb Smirnoff int	load_feedback_profile(struct pfctl *, struct superblocks *);
2233b3a8eb9SGleb Smirnoff int	optimize_superblock(struct pfctl *, struct superblock *);
2243b3a8eb9SGleb Smirnoff int	pf_opt_create_table(struct pfctl *, struct pf_opt_tbl *);
2253b3a8eb9SGleb Smirnoff void	remove_from_skipsteps(struct skiplist *, struct superblock *,
2263b3a8eb9SGleb Smirnoff 	    struct pf_opt_rule *, struct pf_skip_step *);
2273b3a8eb9SGleb Smirnoff int	remove_identical_rules(struct pfctl *, struct superblock *);
2283b3a8eb9SGleb Smirnoff int	reorder_rules(struct pfctl *, struct superblock *, int);
229e9eb0941SKristof Provost int	rules_combineable(struct pfctl_rule *, struct pfctl_rule *);
2303b3a8eb9SGleb Smirnoff void	skip_append(struct superblock *, int, struct pf_skip_step *,
2313b3a8eb9SGleb Smirnoff 	    struct pf_opt_rule *);
2323b3a8eb9SGleb Smirnoff int	skip_compare(int, struct pf_skip_step *, struct pf_opt_rule *);
2333b3a8eb9SGleb Smirnoff void	skip_init(void);
234e9eb0941SKristof Provost int	skip_cmp_af(struct pfctl_rule *, struct pfctl_rule *);
235e9eb0941SKristof Provost int	skip_cmp_dir(struct pfctl_rule *, struct pfctl_rule *);
236e9eb0941SKristof Provost int	skip_cmp_dst_addr(struct pfctl_rule *, struct pfctl_rule *);
237e9eb0941SKristof Provost int	skip_cmp_dst_port(struct pfctl_rule *, struct pfctl_rule *);
238e9eb0941SKristof Provost int	skip_cmp_ifp(struct pfctl_rule *, struct pfctl_rule *);
239e9eb0941SKristof Provost int	skip_cmp_proto(struct pfctl_rule *, struct pfctl_rule *);
240e9eb0941SKristof Provost int	skip_cmp_src_addr(struct pfctl_rule *, struct pfctl_rule *);
241e9eb0941SKristof Provost int	skip_cmp_src_port(struct pfctl_rule *, struct pfctl_rule *);
2423b3a8eb9SGleb Smirnoff int	superblock_inclusive(struct superblock *, struct pf_opt_rule *);
2433b3a8eb9SGleb Smirnoff void	superblock_free(struct pfctl *, struct superblock *);
244a7d631f6SKristof Provost struct pf_opt_tbl *pf_opt_table_ref(struct pf_opt_tbl *);
245a7d631f6SKristof Provost void	pf_opt_table_unref(struct pf_opt_tbl *);
2463b3a8eb9SGleb Smirnoff 
2473b3a8eb9SGleb Smirnoff 
248e9eb0941SKristof Provost static int (*skip_comparitors[PF_SKIP_COUNT])(struct pfctl_rule *,
249e9eb0941SKristof Provost     struct pfctl_rule *);
25013cfafabSKristof Provost static const char *skip_comparitors_names[PF_SKIP_COUNT];
2513b3a8eb9SGleb Smirnoff #define PF_SKIP_COMPARITORS {				\
2523b3a8eb9SGleb Smirnoff     { "ifp", PF_SKIP_IFP, skip_cmp_ifp },		\
2533b3a8eb9SGleb Smirnoff     { "dir", PF_SKIP_DIR, skip_cmp_dir },		\
2543b3a8eb9SGleb Smirnoff     { "af", PF_SKIP_AF, skip_cmp_af },			\
2553b3a8eb9SGleb Smirnoff     { "proto", PF_SKIP_PROTO, skip_cmp_proto },		\
2563b3a8eb9SGleb Smirnoff     { "saddr", PF_SKIP_SRC_ADDR, skip_cmp_src_addr },	\
2573b3a8eb9SGleb Smirnoff     { "daddr", PF_SKIP_DST_ADDR, skip_cmp_dst_addr },	\
258288bec2bSKristof Provost     { "sport", PF_SKIP_SRC_PORT, skip_cmp_src_port },	\
2593b3a8eb9SGleb Smirnoff     { "dport", PF_SKIP_DST_PORT, skip_cmp_dst_port }	\
2603b3a8eb9SGleb Smirnoff }
2613b3a8eb9SGleb Smirnoff 
26213cfafabSKristof Provost static struct pfr_buffer table_buffer;
26313cfafabSKristof Provost static int table_identifier;
2643b3a8eb9SGleb Smirnoff 
2653b3a8eb9SGleb Smirnoff 
2663b3a8eb9SGleb Smirnoff int
pfctl_optimize_ruleset(struct pfctl * pf,struct pfctl_ruleset * rs)267e9eb0941SKristof Provost pfctl_optimize_ruleset(struct pfctl *pf, struct pfctl_ruleset *rs)
2683b3a8eb9SGleb Smirnoff {
2693b3a8eb9SGleb Smirnoff 	struct superblocks superblocks;
2703b3a8eb9SGleb Smirnoff 	struct pf_opt_queue opt_queue;
2713b3a8eb9SGleb Smirnoff 	struct superblock *block;
2723b3a8eb9SGleb Smirnoff 	struct pf_opt_rule *por;
273e9eb0941SKristof Provost 	struct pfctl_rule *r;
274e9eb0941SKristof Provost 	struct pfctl_rulequeue *old_rules;
2753b3a8eb9SGleb Smirnoff 
276f97839e0SKristof Provost 	if (TAILQ_EMPTY(rs->rules[PF_RULESET_FILTER].active.ptr))
277f97839e0SKristof Provost 		return (0);
278f97839e0SKristof Provost 
279f97839e0SKristof Provost 	DEBUG("optimizing ruleset \"%s\"", rs->anchor->path);
2803b3a8eb9SGleb Smirnoff 	memset(&table_buffer, 0, sizeof(table_buffer));
2813b3a8eb9SGleb Smirnoff 	skip_init();
2823b3a8eb9SGleb Smirnoff 	TAILQ_INIT(&opt_queue);
2833b3a8eb9SGleb Smirnoff 
2843b3a8eb9SGleb Smirnoff 	old_rules = rs->rules[PF_RULESET_FILTER].active.ptr;
2853b3a8eb9SGleb Smirnoff 	rs->rules[PF_RULESET_FILTER].active.ptr =
2863b3a8eb9SGleb Smirnoff 	    rs->rules[PF_RULESET_FILTER].inactive.ptr;
2873b3a8eb9SGleb Smirnoff 	rs->rules[PF_RULESET_FILTER].inactive.ptr = old_rules;
2883b3a8eb9SGleb Smirnoff 
2893b3a8eb9SGleb Smirnoff 	/*
2903b3a8eb9SGleb Smirnoff 	 * XXX expanding the pf_opt_rule format throughout pfctl might allow
2913b3a8eb9SGleb Smirnoff 	 * us to avoid all this copying.
2923b3a8eb9SGleb Smirnoff 	 */
2933b3a8eb9SGleb Smirnoff 	while ((r = TAILQ_FIRST(rs->rules[PF_RULESET_FILTER].inactive.ptr))
2943b3a8eb9SGleb Smirnoff 	    != NULL) {
2953b3a8eb9SGleb Smirnoff 		TAILQ_REMOVE(rs->rules[PF_RULESET_FILTER].inactive.ptr, r,
2963b3a8eb9SGleb Smirnoff 		    entries);
2973b3a8eb9SGleb Smirnoff 		if ((por = calloc(1, sizeof(*por))) == NULL)
2983b3a8eb9SGleb Smirnoff 			err(1, "calloc");
2993b3a8eb9SGleb Smirnoff 		memcpy(&por->por_rule, r, sizeof(*r));
300096efeb6SKristof Provost 		if (TAILQ_FIRST(&r->rdr.list) != NULL) {
301096efeb6SKristof Provost 			TAILQ_INIT(&por->por_rule.rdr.list);
302096efeb6SKristof Provost 			pfctl_move_pool(&r->rdr, &por->por_rule.rdr);
3033b3a8eb9SGleb Smirnoff 		} else
304096efeb6SKristof Provost 			bzero(&por->por_rule.rdr,
305096efeb6SKristof Provost 			    sizeof(por->por_rule.rdr));
3065cb08fddSKristof Provost 		if (TAILQ_FIRST(&r->nat.list) != NULL) {
3075cb08fddSKristof Provost 			TAILQ_INIT(&por->por_rule.nat.list);
3085cb08fddSKristof Provost 			pfctl_move_pool(&r->nat, &por->por_rule.nat);
3095cb08fddSKristof Provost 		} else
3105cb08fddSKristof Provost 			bzero(&por->por_rule.nat,
3115cb08fddSKristof Provost 			    sizeof(por->por_rule.nat));
3120972294eSKristof Provost 		if (TAILQ_FIRST(&r->route.list) != NULL) {
3130972294eSKristof Provost 			TAILQ_INIT(&por->por_rule.route.list);
3140972294eSKristof Provost 			pfctl_move_pool(&r->route, &por->por_rule.route);
3150972294eSKristof Provost 		} else
3160972294eSKristof Provost 			bzero(&por->por_rule.route,
3170972294eSKristof Provost 			    sizeof(por->por_rule.route));
3183b3a8eb9SGleb Smirnoff 
3193b3a8eb9SGleb Smirnoff 		TAILQ_INSERT_TAIL(&opt_queue, por, por_entry);
3203b3a8eb9SGleb Smirnoff 	}
3213b3a8eb9SGleb Smirnoff 
3223b3a8eb9SGleb Smirnoff 	TAILQ_INIT(&superblocks);
3233b3a8eb9SGleb Smirnoff 	if (construct_superblocks(pf, &opt_queue, &superblocks))
3243b3a8eb9SGleb Smirnoff 		goto error;
3253b3a8eb9SGleb Smirnoff 
3263b3a8eb9SGleb Smirnoff 	if (pf->optimize & PF_OPTIMIZE_PROFILE) {
3273b3a8eb9SGleb Smirnoff 		if (load_feedback_profile(pf, &superblocks))
3283b3a8eb9SGleb Smirnoff 			goto error;
3293b3a8eb9SGleb Smirnoff 	}
3303b3a8eb9SGleb Smirnoff 
3313b3a8eb9SGleb Smirnoff 	TAILQ_FOREACH(block, &superblocks, sb_entry) {
3323b3a8eb9SGleb Smirnoff 		if (optimize_superblock(pf, block))
3333b3a8eb9SGleb Smirnoff 			goto error;
3343b3a8eb9SGleb Smirnoff 	}
3353b3a8eb9SGleb Smirnoff 
3363b3a8eb9SGleb Smirnoff 	rs->anchor->refcnt = 0;
3373b3a8eb9SGleb Smirnoff 	while ((block = TAILQ_FIRST(&superblocks))) {
3383b3a8eb9SGleb Smirnoff 		TAILQ_REMOVE(&superblocks, block, sb_entry);
3393b3a8eb9SGleb Smirnoff 
3403b3a8eb9SGleb Smirnoff 		while ((por = TAILQ_FIRST(&block->sb_rules))) {
3413b3a8eb9SGleb Smirnoff 			TAILQ_REMOVE(&block->sb_rules, por, por_entry);
3423b3a8eb9SGleb Smirnoff 			por->por_rule.nr = rs->anchor->refcnt++;
3433b3a8eb9SGleb Smirnoff 			if ((r = calloc(1, sizeof(*r))) == NULL)
3443b3a8eb9SGleb Smirnoff 				err(1, "calloc");
3453b3a8eb9SGleb Smirnoff 			memcpy(r, &por->por_rule, sizeof(*r));
346096efeb6SKristof Provost 			TAILQ_INIT(&r->rdr.list);
347096efeb6SKristof Provost 			pfctl_move_pool(&por->por_rule.rdr, &r->rdr);
3485cb08fddSKristof Provost 			TAILQ_INIT(&r->nat.list);
3495cb08fddSKristof Provost 			pfctl_move_pool(&por->por_rule.nat, &r->nat);
3503b3a8eb9SGleb Smirnoff 			TAILQ_INSERT_TAIL(
3513b3a8eb9SGleb Smirnoff 			    rs->rules[PF_RULESET_FILTER].active.ptr,
3523b3a8eb9SGleb Smirnoff 			    r, entries);
353a7d631f6SKristof Provost 			pf_opt_table_unref(por->por_src_tbl);
354a7d631f6SKristof Provost 			pf_opt_table_unref(por->por_dst_tbl);
3553b3a8eb9SGleb Smirnoff 			free(por);
3563b3a8eb9SGleb Smirnoff 		}
357a7d631f6SKristof Provost 		superblock_free(pf, block);
3583b3a8eb9SGleb Smirnoff 	}
3593b3a8eb9SGleb Smirnoff 
3603b3a8eb9SGleb Smirnoff 	return (0);
3613b3a8eb9SGleb Smirnoff 
3623b3a8eb9SGleb Smirnoff error:
3633b3a8eb9SGleb Smirnoff 	while ((por = TAILQ_FIRST(&opt_queue))) {
3643b3a8eb9SGleb Smirnoff 		TAILQ_REMOVE(&opt_queue, por, por_entry);
365a7d631f6SKristof Provost 		pf_opt_table_unref(por->por_src_tbl);
366a7d631f6SKristof Provost 		pf_opt_table_unref(por->por_dst_tbl);
3673b3a8eb9SGleb Smirnoff 		free(por);
3683b3a8eb9SGleb Smirnoff 	}
3693b3a8eb9SGleb Smirnoff 	while ((block = TAILQ_FIRST(&superblocks))) {
3703b3a8eb9SGleb Smirnoff 		TAILQ_REMOVE(&superblocks, block, sb_entry);
3713b3a8eb9SGleb Smirnoff 		superblock_free(pf, block);
3723b3a8eb9SGleb Smirnoff 	}
3733b3a8eb9SGleb Smirnoff 	return (1);
3743b3a8eb9SGleb Smirnoff }
3753b3a8eb9SGleb Smirnoff 
3763b3a8eb9SGleb Smirnoff 
3773b3a8eb9SGleb Smirnoff /*
3783b3a8eb9SGleb Smirnoff  * Go ahead and optimize a superblock
3793b3a8eb9SGleb Smirnoff  */
3803b3a8eb9SGleb Smirnoff int
optimize_superblock(struct pfctl * pf,struct superblock * block)3813b3a8eb9SGleb Smirnoff optimize_superblock(struct pfctl *pf, struct superblock *block)
3823b3a8eb9SGleb Smirnoff {
3833b3a8eb9SGleb Smirnoff #ifdef OPT_DEBUG
3843b3a8eb9SGleb Smirnoff 	struct pf_opt_rule *por;
3853b3a8eb9SGleb Smirnoff #endif /* OPT_DEBUG */
3863b3a8eb9SGleb Smirnoff 
3873b3a8eb9SGleb Smirnoff 	/* We have a few optimization passes:
3883b3a8eb9SGleb Smirnoff 	 *   1) remove duplicate rules or rules that are a subset of other
3893b3a8eb9SGleb Smirnoff 	 *      rules
3903b3a8eb9SGleb Smirnoff 	 *   2) combine otherwise identical rules with different IP addresses
3913b3a8eb9SGleb Smirnoff 	 *      into a single rule and put the addresses in a table.
3923b3a8eb9SGleb Smirnoff 	 *   3) re-order the rules to improve kernel skip steps
3933b3a8eb9SGleb Smirnoff 	 *   4) re-order the 'quick' rules based on feedback from the
3943b3a8eb9SGleb Smirnoff 	 *      active ruleset statistics
3953b3a8eb9SGleb Smirnoff 	 *
3963b3a8eb9SGleb Smirnoff 	 * XXX combine_rules() doesn't combine v4 and v6 rules.  would just
3973b3a8eb9SGleb Smirnoff 	 *     have to keep af in the table container, make af 'COMBINE' and
3983b3a8eb9SGleb Smirnoff 	 *     twiddle the af on the merged rule
3993b3a8eb9SGleb Smirnoff 	 * XXX maybe add a weighting to the metric on skipsteps when doing
4003b3a8eb9SGleb Smirnoff 	 *     reordering.  sometimes two sequential tables will be better
4013b3a8eb9SGleb Smirnoff 	 *     that four consecutive interfaces.
4023b3a8eb9SGleb Smirnoff 	 * XXX need to adjust the skipstep count of everything after PROTO,
4033b3a8eb9SGleb Smirnoff 	 *     since they aren't actually checked on a proto mismatch in
4043b3a8eb9SGleb Smirnoff 	 *     pf_test_{tcp, udp, icmp}()
4053b3a8eb9SGleb Smirnoff 	 * XXX should i treat proto=0, af=0 or dir=0 special in skepstep
4063b3a8eb9SGleb Smirnoff 	 *     calculation since they are a DC?
4073b3a8eb9SGleb Smirnoff 	 * XXX keep last skiplist of last superblock to influence this
4083b3a8eb9SGleb Smirnoff 	 *     superblock.  '5 inet6 log' should make '3 inet6' come before '4
4093b3a8eb9SGleb Smirnoff 	 *     inet' in the next superblock.
4103b3a8eb9SGleb Smirnoff 	 * XXX would be useful to add tables for ports
4113b3a8eb9SGleb Smirnoff 	 * XXX we can also re-order some mutually exclusive superblocks to
4123b3a8eb9SGleb Smirnoff 	 *     try merging superblocks before any of these optimization passes.
4133b3a8eb9SGleb Smirnoff 	 *     for instance a single 'log in' rule in the middle of non-logging
4143b3a8eb9SGleb Smirnoff 	 *     out rules.
4153b3a8eb9SGleb Smirnoff 	 */
4163b3a8eb9SGleb Smirnoff 
4173b3a8eb9SGleb Smirnoff 	/* shortcut.  there will be a lot of 1-rule superblocks */
4183b3a8eb9SGleb Smirnoff 	if (!TAILQ_NEXT(TAILQ_FIRST(&block->sb_rules), por_entry))
4193b3a8eb9SGleb Smirnoff 		return (0);
4203b3a8eb9SGleb Smirnoff 
4213b3a8eb9SGleb Smirnoff #ifdef OPT_DEBUG
4223b3a8eb9SGleb Smirnoff 	printf("--- Superblock ---\n");
4233b3a8eb9SGleb Smirnoff 	TAILQ_FOREACH(por, &block->sb_rules, por_entry) {
4243b3a8eb9SGleb Smirnoff 		printf("  ");
4253b3a8eb9SGleb Smirnoff 		print_rule(&por->por_rule, por->por_rule.anchor ?
4263b3a8eb9SGleb Smirnoff 		    por->por_rule.anchor->name : "", 1, 0);
4273b3a8eb9SGleb Smirnoff 	}
4283b3a8eb9SGleb Smirnoff #endif /* OPT_DEBUG */
4293b3a8eb9SGleb Smirnoff 
4303b3a8eb9SGleb Smirnoff 
4313b3a8eb9SGleb Smirnoff 	if (remove_identical_rules(pf, block))
4323b3a8eb9SGleb Smirnoff 		return (1);
4333b3a8eb9SGleb Smirnoff 	if (combine_rules(pf, block))
4343b3a8eb9SGleb Smirnoff 		return (1);
4353b3a8eb9SGleb Smirnoff 	if ((pf->optimize & PF_OPTIMIZE_PROFILE) &&
4363b3a8eb9SGleb Smirnoff 	    TAILQ_FIRST(&block->sb_rules)->por_rule.quick &&
4373b3a8eb9SGleb Smirnoff 	    block->sb_profiled_block) {
4383b3a8eb9SGleb Smirnoff 		if (block_feedback(pf, block))
4393b3a8eb9SGleb Smirnoff 			return (1);
4403b3a8eb9SGleb Smirnoff 	} else if (reorder_rules(pf, block, 0)) {
4413b3a8eb9SGleb Smirnoff 		return (1);
4423b3a8eb9SGleb Smirnoff 	}
4433b3a8eb9SGleb Smirnoff 
4443b3a8eb9SGleb Smirnoff 	/*
4453b3a8eb9SGleb Smirnoff 	 * Don't add any optimization passes below reorder_rules().  It will
4463b3a8eb9SGleb Smirnoff 	 * have divided superblocks into smaller blocks for further refinement
4473b3a8eb9SGleb Smirnoff 	 * and doesn't put them back together again.  What once was a true
4483b3a8eb9SGleb Smirnoff 	 * superblock might have been split into multiple superblocks.
4493b3a8eb9SGleb Smirnoff 	 */
4503b3a8eb9SGleb Smirnoff 
4513b3a8eb9SGleb Smirnoff #ifdef OPT_DEBUG
4523b3a8eb9SGleb Smirnoff 	printf("--- END Superblock ---\n");
4533b3a8eb9SGleb Smirnoff #endif /* OPT_DEBUG */
4543b3a8eb9SGleb Smirnoff 	return (0);
4553b3a8eb9SGleb Smirnoff }
4563b3a8eb9SGleb Smirnoff 
4573b3a8eb9SGleb Smirnoff 
4583b3a8eb9SGleb Smirnoff /*
4593b3a8eb9SGleb Smirnoff  * Optimization pass #1: remove identical rules
4603b3a8eb9SGleb Smirnoff  */
4613b3a8eb9SGleb Smirnoff int
remove_identical_rules(struct pfctl * pf,struct superblock * block)4623b3a8eb9SGleb Smirnoff remove_identical_rules(struct pfctl *pf, struct superblock *block)
4633b3a8eb9SGleb Smirnoff {
4643b3a8eb9SGleb Smirnoff 	struct pf_opt_rule *por1, *por2, *por_next, *por2_next;
465e9eb0941SKristof Provost 	struct pfctl_rule a, a2, b, b2;
4663b3a8eb9SGleb Smirnoff 
4673b3a8eb9SGleb Smirnoff 	for (por1 = TAILQ_FIRST(&block->sb_rules); por1; por1 = por_next) {
4683b3a8eb9SGleb Smirnoff 		por_next = TAILQ_NEXT(por1, por_entry);
4693b3a8eb9SGleb Smirnoff 		for (por2 = por_next; por2; por2 = por2_next) {
4703b3a8eb9SGleb Smirnoff 			por2_next = TAILQ_NEXT(por2, por_entry);
4713b3a8eb9SGleb Smirnoff 			comparable_rule(&a, &por1->por_rule, DC);
4723b3a8eb9SGleb Smirnoff 			comparable_rule(&b, &por2->por_rule, DC);
4733b3a8eb9SGleb Smirnoff 			memcpy(&a2, &a, sizeof(a2));
4743b3a8eb9SGleb Smirnoff 			memcpy(&b2, &b, sizeof(b2));
4753b3a8eb9SGleb Smirnoff 
4763b3a8eb9SGleb Smirnoff 			exclude_supersets(&a, &b);
4773b3a8eb9SGleb Smirnoff 			exclude_supersets(&b2, &a2);
4783b3a8eb9SGleb Smirnoff 			if (memcmp(&a, &b, sizeof(a)) == 0) {
4793b3a8eb9SGleb Smirnoff 				DEBUG("removing identical rule  nr%d = *nr%d*",
4803b3a8eb9SGleb Smirnoff 				    por1->por_rule.nr, por2->por_rule.nr);
4813b3a8eb9SGleb Smirnoff 				TAILQ_REMOVE(&block->sb_rules, por2, por_entry);
4823b3a8eb9SGleb Smirnoff 				if (por_next == por2)
4833b3a8eb9SGleb Smirnoff 					por_next = TAILQ_NEXT(por1, por_entry);
4843b3a8eb9SGleb Smirnoff 				free(por2);
4853b3a8eb9SGleb Smirnoff 			} else if (memcmp(&a2, &b2, sizeof(a2)) == 0) {
4863b3a8eb9SGleb Smirnoff 				DEBUG("removing identical rule  *nr%d* = nr%d",
4873b3a8eb9SGleb Smirnoff 				    por1->por_rule.nr, por2->por_rule.nr);
4883b3a8eb9SGleb Smirnoff 				TAILQ_REMOVE(&block->sb_rules, por1, por_entry);
4893b3a8eb9SGleb Smirnoff 				free(por1);
4903b3a8eb9SGleb Smirnoff 				break;
4913b3a8eb9SGleb Smirnoff 			}
4923b3a8eb9SGleb Smirnoff 		}
4933b3a8eb9SGleb Smirnoff 	}
4943b3a8eb9SGleb Smirnoff 
4953b3a8eb9SGleb Smirnoff 	return (0);
4963b3a8eb9SGleb Smirnoff }
4973b3a8eb9SGleb Smirnoff 
4983b3a8eb9SGleb Smirnoff 
4993b3a8eb9SGleb Smirnoff /*
5003b3a8eb9SGleb Smirnoff  * Optimization pass #2: combine similar rules with different addresses
5013b3a8eb9SGleb Smirnoff  * into a single rule and a table
5023b3a8eb9SGleb Smirnoff  */
5033b3a8eb9SGleb Smirnoff int
combine_rules(struct pfctl * pf,struct superblock * block)5043b3a8eb9SGleb Smirnoff combine_rules(struct pfctl *pf, struct superblock *block)
5053b3a8eb9SGleb Smirnoff {
5063b3a8eb9SGleb Smirnoff 	struct pf_opt_rule *p1, *p2, *por_next;
5073b3a8eb9SGleb Smirnoff 	int src_eq, dst_eq;
5083b3a8eb9SGleb Smirnoff 
5093b3a8eb9SGleb Smirnoff 	if ((pf->loadopt & PFCTL_FLAG_TABLE) == 0) {
5103b3a8eb9SGleb Smirnoff 		warnx("Must enable table loading for optimizations");
5113b3a8eb9SGleb Smirnoff 		return (1);
5123b3a8eb9SGleb Smirnoff 	}
5133b3a8eb9SGleb Smirnoff 
5143b3a8eb9SGleb Smirnoff 	/* First we make a pass to combine the rules.  O(n log n) */
5153b3a8eb9SGleb Smirnoff 	TAILQ_FOREACH(p1, &block->sb_rules, por_entry) {
5163b3a8eb9SGleb Smirnoff 		for (p2 = TAILQ_NEXT(p1, por_entry); p2; p2 = por_next) {
5173b3a8eb9SGleb Smirnoff 			por_next = TAILQ_NEXT(p2, por_entry);
5183b3a8eb9SGleb Smirnoff 
5193b3a8eb9SGleb Smirnoff 			src_eq = addrs_equal(&p1->por_rule.src,
5203b3a8eb9SGleb Smirnoff 			    &p2->por_rule.src);
5213b3a8eb9SGleb Smirnoff 			dst_eq = addrs_equal(&p1->por_rule.dst,
5223b3a8eb9SGleb Smirnoff 			    &p2->por_rule.dst);
5233b3a8eb9SGleb Smirnoff 
5243b3a8eb9SGleb Smirnoff 			if (src_eq && !dst_eq && p1->por_src_tbl == NULL &&
5253b3a8eb9SGleb Smirnoff 			    p2->por_dst_tbl == NULL &&
5263b3a8eb9SGleb Smirnoff 			    p2->por_src_tbl == NULL &&
5273b3a8eb9SGleb Smirnoff 			    rules_combineable(&p1->por_rule, &p2->por_rule) &&
5283b3a8eb9SGleb Smirnoff 			    addrs_combineable(&p1->por_rule.dst,
5293b3a8eb9SGleb Smirnoff 			    &p2->por_rule.dst)) {
5303b3a8eb9SGleb Smirnoff 				DEBUG("can combine rules  nr%d = nr%d",
5313b3a8eb9SGleb Smirnoff 				    p1->por_rule.nr, p2->por_rule.nr);
5323b3a8eb9SGleb Smirnoff 				if (p1->por_dst_tbl == NULL &&
5333b3a8eb9SGleb Smirnoff 				    add_opt_table(pf, &p1->por_dst_tbl,
5343b3a8eb9SGleb Smirnoff 				    p1->por_rule.af, &p1->por_rule.dst))
5353b3a8eb9SGleb Smirnoff 					return (1);
5363b3a8eb9SGleb Smirnoff 				if (add_opt_table(pf, &p1->por_dst_tbl,
5373b3a8eb9SGleb Smirnoff 				    p1->por_rule.af, &p2->por_rule.dst))
5383b3a8eb9SGleb Smirnoff 					return (1);
5393b3a8eb9SGleb Smirnoff 				if (p1->por_dst_tbl->pt_rulecount >=
5403b3a8eb9SGleb Smirnoff 				    TABLE_THRESHOLD) {
5413b3a8eb9SGleb Smirnoff 					TAILQ_REMOVE(&block->sb_rules, p2,
5423b3a8eb9SGleb Smirnoff 					    por_entry);
5433b3a8eb9SGleb Smirnoff 					free(p2);
544a7d631f6SKristof Provost 				} else {
545a7d631f6SKristof Provost 					p2->por_dst_tbl =
546a7d631f6SKristof Provost 					    pf_opt_table_ref(p1->por_dst_tbl);
5473b3a8eb9SGleb Smirnoff 				}
5483b3a8eb9SGleb Smirnoff 			} else if (!src_eq && dst_eq && p1->por_dst_tbl == NULL
5493b3a8eb9SGleb Smirnoff 			    && p2->por_src_tbl == NULL &&
5503b3a8eb9SGleb Smirnoff 			    p2->por_dst_tbl == NULL &&
5513b3a8eb9SGleb Smirnoff 			    rules_combineable(&p1->por_rule, &p2->por_rule) &&
5523b3a8eb9SGleb Smirnoff 			    addrs_combineable(&p1->por_rule.src,
5533b3a8eb9SGleb Smirnoff 			    &p2->por_rule.src)) {
5543b3a8eb9SGleb Smirnoff 				DEBUG("can combine rules  nr%d = nr%d",
5553b3a8eb9SGleb Smirnoff 				    p1->por_rule.nr, p2->por_rule.nr);
5563b3a8eb9SGleb Smirnoff 				if (p1->por_src_tbl == NULL &&
5573b3a8eb9SGleb Smirnoff 				    add_opt_table(pf, &p1->por_src_tbl,
5583b3a8eb9SGleb Smirnoff 				    p1->por_rule.af, &p1->por_rule.src))
5593b3a8eb9SGleb Smirnoff 					return (1);
5603b3a8eb9SGleb Smirnoff 				if (add_opt_table(pf, &p1->por_src_tbl,
5613b3a8eb9SGleb Smirnoff 				    p1->por_rule.af, &p2->por_rule.src))
5623b3a8eb9SGleb Smirnoff 					return (1);
5633b3a8eb9SGleb Smirnoff 				if (p1->por_src_tbl->pt_rulecount >=
5643b3a8eb9SGleb Smirnoff 				    TABLE_THRESHOLD) {
5653b3a8eb9SGleb Smirnoff 					TAILQ_REMOVE(&block->sb_rules, p2,
5663b3a8eb9SGleb Smirnoff 					    por_entry);
5673b3a8eb9SGleb Smirnoff 					free(p2);
568a7d631f6SKristof Provost 				} else {
569a7d631f6SKristof Provost 					p2->por_src_tbl =
570a7d631f6SKristof Provost 					    pf_opt_table_ref(p1->por_src_tbl);
5713b3a8eb9SGleb Smirnoff 				}
5723b3a8eb9SGleb Smirnoff 			}
5733b3a8eb9SGleb Smirnoff 		}
5743b3a8eb9SGleb Smirnoff 	}
5753b3a8eb9SGleb Smirnoff 
5763b3a8eb9SGleb Smirnoff 
5773b3a8eb9SGleb Smirnoff 	/*
5783b3a8eb9SGleb Smirnoff 	 * Then we make a final pass to create a valid table name and
5793b3a8eb9SGleb Smirnoff 	 * insert the name into the rules.
5803b3a8eb9SGleb Smirnoff 	 */
5813b3a8eb9SGleb Smirnoff 	for (p1 = TAILQ_FIRST(&block->sb_rules); p1; p1 = por_next) {
5823b3a8eb9SGleb Smirnoff 		por_next = TAILQ_NEXT(p1, por_entry);
5833b3a8eb9SGleb Smirnoff 		assert(p1->por_src_tbl == NULL || p1->por_dst_tbl == NULL);
5843b3a8eb9SGleb Smirnoff 
5853b3a8eb9SGleb Smirnoff 		if (p1->por_src_tbl && p1->por_src_tbl->pt_rulecount >=
5863b3a8eb9SGleb Smirnoff 		    TABLE_THRESHOLD) {
5873b3a8eb9SGleb Smirnoff 			if (p1->por_src_tbl->pt_generated) {
5883b3a8eb9SGleb Smirnoff 				/* This rule is included in a table */
5893b3a8eb9SGleb Smirnoff 				TAILQ_REMOVE(&block->sb_rules, p1, por_entry);
5903b3a8eb9SGleb Smirnoff 				free(p1);
5913b3a8eb9SGleb Smirnoff 				continue;
5923b3a8eb9SGleb Smirnoff 			}
5933b3a8eb9SGleb Smirnoff 			p1->por_src_tbl->pt_generated = 1;
5943b3a8eb9SGleb Smirnoff 
5953b3a8eb9SGleb Smirnoff 			if ((pf->opts & PF_OPT_NOACTION) == 0 &&
5963b3a8eb9SGleb Smirnoff 			    pf_opt_create_table(pf, p1->por_src_tbl))
5973b3a8eb9SGleb Smirnoff 				return (1);
5983b3a8eb9SGleb Smirnoff 
5993b3a8eb9SGleb Smirnoff 			pf->tdirty = 1;
6003b3a8eb9SGleb Smirnoff 
6013b3a8eb9SGleb Smirnoff 			if (pf->opts & PF_OPT_VERBOSE)
6023b3a8eb9SGleb Smirnoff 				print_tabledef(p1->por_src_tbl->pt_name,
6033b3a8eb9SGleb Smirnoff 				    PFR_TFLAG_CONST, 1,
6043b3a8eb9SGleb Smirnoff 				    &p1->por_src_tbl->pt_nodes);
6053b3a8eb9SGleb Smirnoff 
6063b3a8eb9SGleb Smirnoff 			memset(&p1->por_rule.src.addr, 0,
6073b3a8eb9SGleb Smirnoff 			    sizeof(p1->por_rule.src.addr));
6083b3a8eb9SGleb Smirnoff 			p1->por_rule.src.addr.type = PF_ADDR_TABLE;
6093b3a8eb9SGleb Smirnoff 			strlcpy(p1->por_rule.src.addr.v.tblname,
6103b3a8eb9SGleb Smirnoff 			    p1->por_src_tbl->pt_name,
6113b3a8eb9SGleb Smirnoff 			    sizeof(p1->por_rule.src.addr.v.tblname));
6123b3a8eb9SGleb Smirnoff 
6133b3a8eb9SGleb Smirnoff 			pfr_buf_clear(p1->por_src_tbl->pt_buf);
6143b3a8eb9SGleb Smirnoff 			free(p1->por_src_tbl->pt_buf);
6153b3a8eb9SGleb Smirnoff 			p1->por_src_tbl->pt_buf = NULL;
6163b3a8eb9SGleb Smirnoff 		}
6173b3a8eb9SGleb Smirnoff 		if (p1->por_dst_tbl && p1->por_dst_tbl->pt_rulecount >=
6183b3a8eb9SGleb Smirnoff 		    TABLE_THRESHOLD) {
6193b3a8eb9SGleb Smirnoff 			if (p1->por_dst_tbl->pt_generated) {
6203b3a8eb9SGleb Smirnoff 				/* This rule is included in a table */
6213b3a8eb9SGleb Smirnoff 				TAILQ_REMOVE(&block->sb_rules, p1, por_entry);
6223b3a8eb9SGleb Smirnoff 				free(p1);
6233b3a8eb9SGleb Smirnoff 				continue;
6243b3a8eb9SGleb Smirnoff 			}
6253b3a8eb9SGleb Smirnoff 			p1->por_dst_tbl->pt_generated = 1;
6263b3a8eb9SGleb Smirnoff 
6273b3a8eb9SGleb Smirnoff 			if ((pf->opts & PF_OPT_NOACTION) == 0 &&
6283b3a8eb9SGleb Smirnoff 			    pf_opt_create_table(pf, p1->por_dst_tbl))
6293b3a8eb9SGleb Smirnoff 				return (1);
6303b3a8eb9SGleb Smirnoff 			pf->tdirty = 1;
6313b3a8eb9SGleb Smirnoff 
6323b3a8eb9SGleb Smirnoff 			if (pf->opts & PF_OPT_VERBOSE)
6333b3a8eb9SGleb Smirnoff 				print_tabledef(p1->por_dst_tbl->pt_name,
6343b3a8eb9SGleb Smirnoff 				    PFR_TFLAG_CONST, 1,
6353b3a8eb9SGleb Smirnoff 				    &p1->por_dst_tbl->pt_nodes);
6363b3a8eb9SGleb Smirnoff 
6373b3a8eb9SGleb Smirnoff 			memset(&p1->por_rule.dst.addr, 0,
6383b3a8eb9SGleb Smirnoff 			    sizeof(p1->por_rule.dst.addr));
6393b3a8eb9SGleb Smirnoff 			p1->por_rule.dst.addr.type = PF_ADDR_TABLE;
6403b3a8eb9SGleb Smirnoff 			strlcpy(p1->por_rule.dst.addr.v.tblname,
6413b3a8eb9SGleb Smirnoff 			    p1->por_dst_tbl->pt_name,
6423b3a8eb9SGleb Smirnoff 			    sizeof(p1->por_rule.dst.addr.v.tblname));
6433b3a8eb9SGleb Smirnoff 
6443b3a8eb9SGleb Smirnoff 			pfr_buf_clear(p1->por_dst_tbl->pt_buf);
6453b3a8eb9SGleb Smirnoff 			free(p1->por_dst_tbl->pt_buf);
6463b3a8eb9SGleb Smirnoff 			p1->por_dst_tbl->pt_buf = NULL;
6473b3a8eb9SGleb Smirnoff 		}
6483b3a8eb9SGleb Smirnoff 	}
6493b3a8eb9SGleb Smirnoff 
6503b3a8eb9SGleb Smirnoff 	return (0);
6513b3a8eb9SGleb Smirnoff }
6523b3a8eb9SGleb Smirnoff 
6533b3a8eb9SGleb Smirnoff 
6543b3a8eb9SGleb Smirnoff /*
6553b3a8eb9SGleb Smirnoff  * Optimization pass #3: re-order rules to improve skip steps
6563b3a8eb9SGleb Smirnoff  */
6573b3a8eb9SGleb Smirnoff int
reorder_rules(struct pfctl * pf,struct superblock * block,int depth)6583b3a8eb9SGleb Smirnoff reorder_rules(struct pfctl *pf, struct superblock *block, int depth)
6593b3a8eb9SGleb Smirnoff {
6603b3a8eb9SGleb Smirnoff 	struct superblock *newblock;
6613b3a8eb9SGleb Smirnoff 	struct pf_skip_step *skiplist;
6623b3a8eb9SGleb Smirnoff 	struct pf_opt_rule *por;
6633b3a8eb9SGleb Smirnoff 	int i, largest, largest_list, rule_count = 0;
6643b3a8eb9SGleb Smirnoff 	TAILQ_HEAD( , pf_opt_rule) head;
6653b3a8eb9SGleb Smirnoff 
6663b3a8eb9SGleb Smirnoff 	/*
6673b3a8eb9SGleb Smirnoff 	 * Calculate the best-case skip steps.  We put each rule in a list
6683b3a8eb9SGleb Smirnoff 	 * of other rules with common fields
6693b3a8eb9SGleb Smirnoff 	 */
6703b3a8eb9SGleb Smirnoff 	for (i = 0; i < PF_SKIP_COUNT; i++) {
6713b3a8eb9SGleb Smirnoff 		TAILQ_FOREACH(por, &block->sb_rules, por_entry) {
6723b3a8eb9SGleb Smirnoff 			TAILQ_FOREACH(skiplist, &block->sb_skipsteps[i],
6733b3a8eb9SGleb Smirnoff 			    ps_entry) {
6743b3a8eb9SGleb Smirnoff 				if (skip_compare(i, skiplist, por) == 0)
6753b3a8eb9SGleb Smirnoff 					break;
6763b3a8eb9SGleb Smirnoff 			}
6773b3a8eb9SGleb Smirnoff 			if (skiplist == NULL) {
6783b3a8eb9SGleb Smirnoff 				if ((skiplist = calloc(1, sizeof(*skiplist))) ==
6793b3a8eb9SGleb Smirnoff 				    NULL)
6803b3a8eb9SGleb Smirnoff 					err(1, "calloc");
6813b3a8eb9SGleb Smirnoff 				TAILQ_INIT(&skiplist->ps_rules);
6823b3a8eb9SGleb Smirnoff 				TAILQ_INSERT_TAIL(&block->sb_skipsteps[i],
6833b3a8eb9SGleb Smirnoff 				    skiplist, ps_entry);
6843b3a8eb9SGleb Smirnoff 			}
6853b3a8eb9SGleb Smirnoff 			skip_append(block, i, skiplist, por);
6863b3a8eb9SGleb Smirnoff 		}
6873b3a8eb9SGleb Smirnoff 	}
6883b3a8eb9SGleb Smirnoff 
6893b3a8eb9SGleb Smirnoff 	TAILQ_FOREACH(por, &block->sb_rules, por_entry)
6903b3a8eb9SGleb Smirnoff 		rule_count++;
6913b3a8eb9SGleb Smirnoff 
6923b3a8eb9SGleb Smirnoff 	/*
6933b3a8eb9SGleb Smirnoff 	 * Now we're going to ignore any fields that are identical between
6943b3a8eb9SGleb Smirnoff 	 * all of the rules in the superblock and those fields which differ
6953b3a8eb9SGleb Smirnoff 	 * between every rule in the superblock.
6963b3a8eb9SGleb Smirnoff 	 */
6973b3a8eb9SGleb Smirnoff 	largest = 0;
6983b3a8eb9SGleb Smirnoff 	for (i = 0; i < PF_SKIP_COUNT; i++) {
6993b3a8eb9SGleb Smirnoff 		skiplist = TAILQ_FIRST(&block->sb_skipsteps[i]);
7003b3a8eb9SGleb Smirnoff 		if (skiplist->ps_count == rule_count) {
7013b3a8eb9SGleb Smirnoff 			DEBUG("(%d) original skipstep '%s' is all rules",
7023b3a8eb9SGleb Smirnoff 			    depth, skip_comparitors_names[i]);
7033b3a8eb9SGleb Smirnoff 			skiplist->ps_count = 0;
7043b3a8eb9SGleb Smirnoff 		} else if (skiplist->ps_count == 1) {
7053b3a8eb9SGleb Smirnoff 			skiplist->ps_count = 0;
7063b3a8eb9SGleb Smirnoff 		} else {
7073b3a8eb9SGleb Smirnoff 			DEBUG("(%d) original skipstep '%s' largest jump is %d",
7083b3a8eb9SGleb Smirnoff 			    depth, skip_comparitors_names[i],
7093b3a8eb9SGleb Smirnoff 			    skiplist->ps_count);
7103b3a8eb9SGleb Smirnoff 			if (skiplist->ps_count > largest)
7113b3a8eb9SGleb Smirnoff 				largest = skiplist->ps_count;
7123b3a8eb9SGleb Smirnoff 		}
7133b3a8eb9SGleb Smirnoff 	}
7143b3a8eb9SGleb Smirnoff 	if (largest == 0) {
7153b3a8eb9SGleb Smirnoff 		/* Ugh.  There is NO commonality in the superblock on which
7163b3a8eb9SGleb Smirnoff 		 * optimize the skipsteps optimization.
7173b3a8eb9SGleb Smirnoff 		 */
7183b3a8eb9SGleb Smirnoff 		goto done;
7193b3a8eb9SGleb Smirnoff 	}
7203b3a8eb9SGleb Smirnoff 
7213b3a8eb9SGleb Smirnoff 	/*
7223b3a8eb9SGleb Smirnoff 	 * Now we're going to empty the superblock rule list and re-create
7233b3a8eb9SGleb Smirnoff 	 * it based on a more optimal skipstep order.
7243b3a8eb9SGleb Smirnoff 	 */
7253b3a8eb9SGleb Smirnoff 	TAILQ_INIT(&head);
726a9706d78SKristof Provost 	TAILQ_CONCAT(&head, &block->sb_rules, por_entry);
7273b3a8eb9SGleb Smirnoff 
7283b3a8eb9SGleb Smirnoff 	while (!TAILQ_EMPTY(&head)) {
7293b3a8eb9SGleb Smirnoff 		largest = 1;
7303b3a8eb9SGleb Smirnoff 
7313b3a8eb9SGleb Smirnoff 		/*
7323b3a8eb9SGleb Smirnoff 		 * Find the most useful skip steps remaining
7333b3a8eb9SGleb Smirnoff 		 */
7343b3a8eb9SGleb Smirnoff 		for (i = 0; i < PF_SKIP_COUNT; i++) {
7353b3a8eb9SGleb Smirnoff 			skiplist = TAILQ_FIRST(&block->sb_skipsteps[i]);
7363b3a8eb9SGleb Smirnoff 			if (skiplist->ps_count > largest) {
7373b3a8eb9SGleb Smirnoff 				largest = skiplist->ps_count;
7383b3a8eb9SGleb Smirnoff 				largest_list = i;
7393b3a8eb9SGleb Smirnoff 			}
7403b3a8eb9SGleb Smirnoff 		}
7413b3a8eb9SGleb Smirnoff 
7423b3a8eb9SGleb Smirnoff 		if (largest <= 1) {
7433b3a8eb9SGleb Smirnoff 			/*
7443b3a8eb9SGleb Smirnoff 			 * Nothing useful left.  Leave remaining rules in order.
7453b3a8eb9SGleb Smirnoff 			 */
7463b3a8eb9SGleb Smirnoff 			DEBUG("(%d) no more commonality for skip steps", depth);
747a9706d78SKristof Provost 			TAILQ_CONCAT(&block->sb_rules, &head, por_entry);
7483b3a8eb9SGleb Smirnoff 		} else {
7493b3a8eb9SGleb Smirnoff 			/*
7503b3a8eb9SGleb Smirnoff 			 * There is commonality.  Extract those common rules
7513b3a8eb9SGleb Smirnoff 			 * and place them in the ruleset adjacent to each
7523b3a8eb9SGleb Smirnoff 			 * other.
7533b3a8eb9SGleb Smirnoff 			 */
7543b3a8eb9SGleb Smirnoff 			skiplist = TAILQ_FIRST(&block->sb_skipsteps[
7553b3a8eb9SGleb Smirnoff 			    largest_list]);
7563b3a8eb9SGleb Smirnoff 			DEBUG("(%d) skipstep '%s' largest jump is %d @ #%d",
7573b3a8eb9SGleb Smirnoff 			    depth, skip_comparitors_names[largest_list],
7583b3a8eb9SGleb Smirnoff 			    largest, TAILQ_FIRST(&TAILQ_FIRST(&block->
7593b3a8eb9SGleb Smirnoff 			    sb_skipsteps [largest_list])->ps_rules)->
7603b3a8eb9SGleb Smirnoff 			    por_rule.nr);
7613b3a8eb9SGleb Smirnoff 			TAILQ_REMOVE(&block->sb_skipsteps[largest_list],
7623b3a8eb9SGleb Smirnoff 			    skiplist, ps_entry);
7633b3a8eb9SGleb Smirnoff 
7643b3a8eb9SGleb Smirnoff 
7653b3a8eb9SGleb Smirnoff 			/*
7663b3a8eb9SGleb Smirnoff 			 * There may be further commonality inside these
7673b3a8eb9SGleb Smirnoff 			 * rules.  So we'll split them off into they're own
7683b3a8eb9SGleb Smirnoff 			 * superblock and pass it back into the optimizer.
7693b3a8eb9SGleb Smirnoff 			 */
7703b3a8eb9SGleb Smirnoff 			if (skiplist->ps_count > 2) {
7713b3a8eb9SGleb Smirnoff 				if ((newblock = calloc(1, sizeof(*newblock)))
7723b3a8eb9SGleb Smirnoff 				    == NULL) {
7733b3a8eb9SGleb Smirnoff 					warn("calloc");
7743b3a8eb9SGleb Smirnoff 					return (1);
7753b3a8eb9SGleb Smirnoff 				}
7763b3a8eb9SGleb Smirnoff 				TAILQ_INIT(&newblock->sb_rules);
7773b3a8eb9SGleb Smirnoff 				for (i = 0; i < PF_SKIP_COUNT; i++)
7783b3a8eb9SGleb Smirnoff 					TAILQ_INIT(&newblock->sb_skipsteps[i]);
7793b3a8eb9SGleb Smirnoff 				TAILQ_INSERT_BEFORE(block, newblock, sb_entry);
7803b3a8eb9SGleb Smirnoff 				DEBUG("(%d) splitting off %d rules from superblock @ #%d",
7813b3a8eb9SGleb Smirnoff 				    depth, skiplist->ps_count,
7823b3a8eb9SGleb Smirnoff 				    TAILQ_FIRST(&skiplist->ps_rules)->
7833b3a8eb9SGleb Smirnoff 				    por_rule.nr);
7843b3a8eb9SGleb Smirnoff 			} else {
7853b3a8eb9SGleb Smirnoff 				newblock = block;
7863b3a8eb9SGleb Smirnoff 			}
7873b3a8eb9SGleb Smirnoff 
7883b3a8eb9SGleb Smirnoff 			while ((por = TAILQ_FIRST(&skiplist->ps_rules))) {
7893b3a8eb9SGleb Smirnoff 				TAILQ_REMOVE(&head, por, por_entry);
7903b3a8eb9SGleb Smirnoff 				TAILQ_REMOVE(&skiplist->ps_rules, por,
7913b3a8eb9SGleb Smirnoff 				    por_skip_entry[largest_list]);
7923b3a8eb9SGleb Smirnoff 				TAILQ_INSERT_TAIL(&newblock->sb_rules, por,
7933b3a8eb9SGleb Smirnoff 				    por_entry);
7943b3a8eb9SGleb Smirnoff 
7953b3a8eb9SGleb Smirnoff 				/* Remove this rule from all other skiplists */
7963b3a8eb9SGleb Smirnoff 				remove_from_skipsteps(&block->sb_skipsteps[
7973b3a8eb9SGleb Smirnoff 				    largest_list], block, por, skiplist);
7983b3a8eb9SGleb Smirnoff 			}
7993b3a8eb9SGleb Smirnoff 			free(skiplist);
8003b3a8eb9SGleb Smirnoff 			if (newblock != block)
8013b3a8eb9SGleb Smirnoff 				if (reorder_rules(pf, newblock, depth + 1))
8023b3a8eb9SGleb Smirnoff 					return (1);
8033b3a8eb9SGleb Smirnoff 		}
8043b3a8eb9SGleb Smirnoff 	}
8053b3a8eb9SGleb Smirnoff 
8063b3a8eb9SGleb Smirnoff done:
8073b3a8eb9SGleb Smirnoff 	for (i = 0; i < PF_SKIP_COUNT; i++) {
8083b3a8eb9SGleb Smirnoff 		while ((skiplist = TAILQ_FIRST(&block->sb_skipsteps[i]))) {
8093b3a8eb9SGleb Smirnoff 			TAILQ_REMOVE(&block->sb_skipsteps[i], skiplist,
8103b3a8eb9SGleb Smirnoff 			    ps_entry);
8113b3a8eb9SGleb Smirnoff 			free(skiplist);
8123b3a8eb9SGleb Smirnoff 		}
8133b3a8eb9SGleb Smirnoff 	}
8143b3a8eb9SGleb Smirnoff 
8153b3a8eb9SGleb Smirnoff 	return (0);
8163b3a8eb9SGleb Smirnoff }
8173b3a8eb9SGleb Smirnoff 
8183b3a8eb9SGleb Smirnoff 
8193b3a8eb9SGleb Smirnoff /*
8203b3a8eb9SGleb Smirnoff  * Optimization pass #4: re-order 'quick' rules based on feedback from the
8213b3a8eb9SGleb Smirnoff  * currently running ruleset
8223b3a8eb9SGleb Smirnoff  */
8233b3a8eb9SGleb Smirnoff int
block_feedback(struct pfctl * pf,struct superblock * block)8243b3a8eb9SGleb Smirnoff block_feedback(struct pfctl *pf, struct superblock *block)
8253b3a8eb9SGleb Smirnoff {
8263b3a8eb9SGleb Smirnoff 	TAILQ_HEAD( , pf_opt_rule) queue;
8273b3a8eb9SGleb Smirnoff 	struct pf_opt_rule *por1, *por2;
828e9eb0941SKristof Provost 	struct pfctl_rule a, b;
8293b3a8eb9SGleb Smirnoff 
8303b3a8eb9SGleb Smirnoff 
8313b3a8eb9SGleb Smirnoff 	/*
8323b3a8eb9SGleb Smirnoff 	 * Walk through all of the profiled superblock's rules and copy
8333b3a8eb9SGleb Smirnoff 	 * the counters onto our rules.
8343b3a8eb9SGleb Smirnoff 	 */
8353b3a8eb9SGleb Smirnoff 	TAILQ_FOREACH(por1, &block->sb_profiled_block->sb_rules, por_entry) {
8363b3a8eb9SGleb Smirnoff 		comparable_rule(&a, &por1->por_rule, DC);
8373b3a8eb9SGleb Smirnoff 		TAILQ_FOREACH(por2, &block->sb_rules, por_entry) {
8383b3a8eb9SGleb Smirnoff 			if (por2->por_profile_count)
8393b3a8eb9SGleb Smirnoff 				continue;
8403b3a8eb9SGleb Smirnoff 			comparable_rule(&b, &por2->por_rule, DC);
8413b3a8eb9SGleb Smirnoff 			if (memcmp(&a, &b, sizeof(a)) == 0) {
8423b3a8eb9SGleb Smirnoff 				por2->por_profile_count =
8433b3a8eb9SGleb Smirnoff 				    por1->por_rule.packets[0] +
8443b3a8eb9SGleb Smirnoff 				    por1->por_rule.packets[1];
8453b3a8eb9SGleb Smirnoff 				break;
8463b3a8eb9SGleb Smirnoff 			}
8473b3a8eb9SGleb Smirnoff 		}
8483b3a8eb9SGleb Smirnoff 	}
8493b3a8eb9SGleb Smirnoff 	superblock_free(pf, block->sb_profiled_block);
8503b3a8eb9SGleb Smirnoff 	block->sb_profiled_block = NULL;
8513b3a8eb9SGleb Smirnoff 
8523b3a8eb9SGleb Smirnoff 	/*
8533b3a8eb9SGleb Smirnoff 	 * Now we pull all of the rules off the superblock and re-insert them
8543b3a8eb9SGleb Smirnoff 	 * in sorted order.
8553b3a8eb9SGleb Smirnoff 	 */
8563b3a8eb9SGleb Smirnoff 
8573b3a8eb9SGleb Smirnoff 	TAILQ_INIT(&queue);
858a9706d78SKristof Provost 	TAILQ_CONCAT(&queue, &block->sb_rules, por_entry);
8593b3a8eb9SGleb Smirnoff 
8603b3a8eb9SGleb Smirnoff 	while ((por1 = TAILQ_FIRST(&queue)) != NULL) {
8613b3a8eb9SGleb Smirnoff 		TAILQ_REMOVE(&queue, por1, por_entry);
8623b3a8eb9SGleb Smirnoff /* XXX I should sort all of the unused rules based on skip steps */
8633b3a8eb9SGleb Smirnoff 		TAILQ_FOREACH(por2, &block->sb_rules, por_entry) {
8643b3a8eb9SGleb Smirnoff 			if (por1->por_profile_count > por2->por_profile_count) {
8653b3a8eb9SGleb Smirnoff 				TAILQ_INSERT_BEFORE(por2, por1, por_entry);
8663b3a8eb9SGleb Smirnoff 				break;
8673b3a8eb9SGleb Smirnoff 			}
8683b3a8eb9SGleb Smirnoff 		}
8693b3a8eb9SGleb Smirnoff #ifdef __FreeBSD__
8703b3a8eb9SGleb Smirnoff 		if (por2 == NULL)
8713b3a8eb9SGleb Smirnoff #else
8723b3a8eb9SGleb Smirnoff 		if (por2 == TAILQ_END(&block->sb_rules))
8733b3a8eb9SGleb Smirnoff #endif
8743b3a8eb9SGleb Smirnoff 			TAILQ_INSERT_TAIL(&block->sb_rules, por1, por_entry);
8753b3a8eb9SGleb Smirnoff 	}
8763b3a8eb9SGleb Smirnoff 
8773b3a8eb9SGleb Smirnoff 	return (0);
8783b3a8eb9SGleb Smirnoff }
8793b3a8eb9SGleb Smirnoff 
8803b3a8eb9SGleb Smirnoff 
8813b3a8eb9SGleb Smirnoff /*
8823b3a8eb9SGleb Smirnoff  * Load the current ruleset from the kernel and try to associate them with
8833b3a8eb9SGleb Smirnoff  * the ruleset we're optimizing.
8843b3a8eb9SGleb Smirnoff  */
8853b3a8eb9SGleb Smirnoff int
load_feedback_profile(struct pfctl * pf,struct superblocks * superblocks)8863b3a8eb9SGleb Smirnoff load_feedback_profile(struct pfctl *pf, struct superblocks *superblocks)
8873b3a8eb9SGleb Smirnoff {
88847a0b593SKristof Provost 	char anchor_call[MAXPATHLEN] = "";
8893b3a8eb9SGleb Smirnoff 	struct superblock *block, *blockcur;
8903b3a8eb9SGleb Smirnoff 	struct superblocks prof_superblocks;
8913b3a8eb9SGleb Smirnoff 	struct pf_opt_rule *por;
8923b3a8eb9SGleb Smirnoff 	struct pf_opt_queue queue;
89347a0b593SKristof Provost 	struct pfctl_rules_info rules;
894e9eb0941SKristof Provost 	struct pfctl_rule a, b, rule;
8957153a62dSKristof Provost 	int nr, mnr, ret;
8963b3a8eb9SGleb Smirnoff 
8973b3a8eb9SGleb Smirnoff 	TAILQ_INIT(&queue);
8983b3a8eb9SGleb Smirnoff 	TAILQ_INIT(&prof_superblocks);
8993b3a8eb9SGleb Smirnoff 
9007153a62dSKristof Provost 	if ((ret = pfctl_get_rules_info_h(pf->h, &rules, PF_PASS, "")) != 0) {
90100406234SKristof Provost 		warnx("%s", pf_strerror(ret));
9023b3a8eb9SGleb Smirnoff 		return (1);
9033b3a8eb9SGleb Smirnoff 	}
90447a0b593SKristof Provost 	mnr = rules.nr;
9053b3a8eb9SGleb Smirnoff 
9063b3a8eb9SGleb Smirnoff 	DEBUG("Loading %d active rules for a feedback profile", mnr);
9073b3a8eb9SGleb Smirnoff 	for (nr = 0; nr < mnr; ++nr) {
908e9eb0941SKristof Provost 		struct pfctl_ruleset *rs;
9093b3a8eb9SGleb Smirnoff 		if ((por = calloc(1, sizeof(*por))) == NULL) {
9103b3a8eb9SGleb Smirnoff 			warn("calloc");
9113b3a8eb9SGleb Smirnoff 			return (1);
9123b3a8eb9SGleb Smirnoff 		}
9130d6c8174SKristof Provost 
914cd2054d4SKristof Provost 		if (pfctl_get_rule_h(pf->h, nr, rules.ticket, "", PF_PASS,
91547a0b593SKristof Provost 		    &rule, anchor_call)) {
91600406234SKristof Provost 			warnx("%s", pf_strerror(ret));
917753da351SKristof Provost 			free(por);
9183b3a8eb9SGleb Smirnoff 			return (1);
9193b3a8eb9SGleb Smirnoff 		}
920e9eb0941SKristof Provost 		memcpy(&por->por_rule, &rule, sizeof(por->por_rule));
92147a0b593SKristof Provost 		rs = pf_find_or_create_ruleset(anchor_call);
9223b3a8eb9SGleb Smirnoff 		por->por_rule.anchor = rs->anchor;
923096efeb6SKristof Provost 		if (TAILQ_EMPTY(&por->por_rule.rdr.list))
924096efeb6SKristof Provost 			memset(&por->por_rule.rdr, 0,
925096efeb6SKristof Provost 			    sizeof(por->por_rule.rdr));
9265cb08fddSKristof Provost 		if (TAILQ_EMPTY(&por->por_rule.nat.list))
9275cb08fddSKristof Provost 			memset(&por->por_rule.nat, 0,
9285cb08fddSKristof Provost 			    sizeof(por->por_rule.nat));
9293b3a8eb9SGleb Smirnoff 		TAILQ_INSERT_TAIL(&queue, por, por_entry);
9303b3a8eb9SGleb Smirnoff 
931096efeb6SKristof Provost 		/* XXX pfctl_get_pool(pf->dev, &rule.rdr, nr, pr.ticket,
9323b3a8eb9SGleb Smirnoff 		 *         PF_PASS, pf->anchor) ???
933096efeb6SKristof Provost 		 * ... pfctl_clear_pool(&rule.rdr)
9343b3a8eb9SGleb Smirnoff 		 */
9353b3a8eb9SGleb Smirnoff 	}
9363b3a8eb9SGleb Smirnoff 
9373b3a8eb9SGleb Smirnoff 	if (construct_superblocks(pf, &queue, &prof_superblocks))
9383b3a8eb9SGleb Smirnoff 		return (1);
9393b3a8eb9SGleb Smirnoff 
9403b3a8eb9SGleb Smirnoff 
9413b3a8eb9SGleb Smirnoff 	/*
9423b3a8eb9SGleb Smirnoff 	 * Now we try to associate the active ruleset's superblocks with
9433b3a8eb9SGleb Smirnoff 	 * the superblocks we're compiling.
9443b3a8eb9SGleb Smirnoff 	 */
9453b3a8eb9SGleb Smirnoff 	block = TAILQ_FIRST(superblocks);
9463b3a8eb9SGleb Smirnoff 	blockcur = TAILQ_FIRST(&prof_superblocks);
9473b3a8eb9SGleb Smirnoff 	while (block && blockcur) {
9483b3a8eb9SGleb Smirnoff 		comparable_rule(&a, &TAILQ_FIRST(&block->sb_rules)->por_rule,
9493b3a8eb9SGleb Smirnoff 		    BREAK);
9503b3a8eb9SGleb Smirnoff 		comparable_rule(&b, &TAILQ_FIRST(&blockcur->sb_rules)->por_rule,
9513b3a8eb9SGleb Smirnoff 		    BREAK);
9523b3a8eb9SGleb Smirnoff 		if (memcmp(&a, &b, sizeof(a)) == 0) {
9533b3a8eb9SGleb Smirnoff 			/* The two superblocks lined up */
9543b3a8eb9SGleb Smirnoff 			block->sb_profiled_block = blockcur;
9553b3a8eb9SGleb Smirnoff 		} else {
9563b3a8eb9SGleb Smirnoff 			DEBUG("superblocks don't line up between #%d and #%d",
9573b3a8eb9SGleb Smirnoff 			    TAILQ_FIRST(&block->sb_rules)->por_rule.nr,
9583b3a8eb9SGleb Smirnoff 			    TAILQ_FIRST(&blockcur->sb_rules)->por_rule.nr);
9593b3a8eb9SGleb Smirnoff 			break;
9603b3a8eb9SGleb Smirnoff 		}
9613b3a8eb9SGleb Smirnoff 		block = TAILQ_NEXT(block, sb_entry);
9623b3a8eb9SGleb Smirnoff 		blockcur = TAILQ_NEXT(blockcur, sb_entry);
9633b3a8eb9SGleb Smirnoff 	}
9643b3a8eb9SGleb Smirnoff 
9653b3a8eb9SGleb Smirnoff 
9663b3a8eb9SGleb Smirnoff 
9673b3a8eb9SGleb Smirnoff 	/* Free any superblocks we couldn't link */
9683b3a8eb9SGleb Smirnoff 	while (blockcur) {
9693b3a8eb9SGleb Smirnoff 		block = TAILQ_NEXT(blockcur, sb_entry);
9703b3a8eb9SGleb Smirnoff 		superblock_free(pf, blockcur);
9713b3a8eb9SGleb Smirnoff 		blockcur = block;
9723b3a8eb9SGleb Smirnoff 	}
9733b3a8eb9SGleb Smirnoff 	return (0);
9743b3a8eb9SGleb Smirnoff }
9753b3a8eb9SGleb Smirnoff 
9763b3a8eb9SGleb Smirnoff 
9773b3a8eb9SGleb Smirnoff /*
9783b3a8eb9SGleb Smirnoff  * Compare a rule to a skiplist to see if the rule is a member
9793b3a8eb9SGleb Smirnoff  */
9803b3a8eb9SGleb Smirnoff int
skip_compare(int skipnum,struct pf_skip_step * skiplist,struct pf_opt_rule * por)9813b3a8eb9SGleb Smirnoff skip_compare(int skipnum, struct pf_skip_step *skiplist,
9823b3a8eb9SGleb Smirnoff     struct pf_opt_rule *por)
9833b3a8eb9SGleb Smirnoff {
984e9eb0941SKristof Provost 	struct pfctl_rule *a, *b;
9853b3a8eb9SGleb Smirnoff 	if (skipnum >= PF_SKIP_COUNT || skipnum < 0)
9863b3a8eb9SGleb Smirnoff 		errx(1, "skip_compare() out of bounds");
9873b3a8eb9SGleb Smirnoff 	a = &por->por_rule;
9883b3a8eb9SGleb Smirnoff 	b = &TAILQ_FIRST(&skiplist->ps_rules)->por_rule;
9893b3a8eb9SGleb Smirnoff 
9903b3a8eb9SGleb Smirnoff 	return ((skip_comparitors[skipnum])(a, b));
9913b3a8eb9SGleb Smirnoff }
9923b3a8eb9SGleb Smirnoff 
9933b3a8eb9SGleb Smirnoff 
9943b3a8eb9SGleb Smirnoff /*
9953b3a8eb9SGleb Smirnoff  * Add a rule to a skiplist
9963b3a8eb9SGleb Smirnoff  */
9973b3a8eb9SGleb Smirnoff void
skip_append(struct superblock * superblock,int skipnum,struct pf_skip_step * skiplist,struct pf_opt_rule * por)9983b3a8eb9SGleb Smirnoff skip_append(struct superblock *superblock, int skipnum,
9993b3a8eb9SGleb Smirnoff     struct pf_skip_step *skiplist, struct pf_opt_rule *por)
10003b3a8eb9SGleb Smirnoff {
10013b3a8eb9SGleb Smirnoff 	struct pf_skip_step *prev;
10023b3a8eb9SGleb Smirnoff 
10033b3a8eb9SGleb Smirnoff 	skiplist->ps_count++;
10043b3a8eb9SGleb Smirnoff 	TAILQ_INSERT_TAIL(&skiplist->ps_rules, por, por_skip_entry[skipnum]);
10053b3a8eb9SGleb Smirnoff 
10063b3a8eb9SGleb Smirnoff 	/* Keep the list of skiplists sorted by whichever is larger */
10073b3a8eb9SGleb Smirnoff 	while ((prev = TAILQ_PREV(skiplist, skiplist, ps_entry)) &&
10083b3a8eb9SGleb Smirnoff 	    prev->ps_count < skiplist->ps_count) {
10093b3a8eb9SGleb Smirnoff 		TAILQ_REMOVE(&superblock->sb_skipsteps[skipnum],
10103b3a8eb9SGleb Smirnoff 		    skiplist, ps_entry);
10113b3a8eb9SGleb Smirnoff 		TAILQ_INSERT_BEFORE(prev, skiplist, ps_entry);
10123b3a8eb9SGleb Smirnoff 	}
10133b3a8eb9SGleb Smirnoff }
10143b3a8eb9SGleb Smirnoff 
10153b3a8eb9SGleb Smirnoff 
10163b3a8eb9SGleb Smirnoff /*
10173b3a8eb9SGleb Smirnoff  * Remove a rule from the other skiplist calculations.
10183b3a8eb9SGleb Smirnoff  */
10193b3a8eb9SGleb Smirnoff void
remove_from_skipsteps(struct skiplist * head,struct superblock * block,struct pf_opt_rule * por,struct pf_skip_step * active_list)10203b3a8eb9SGleb Smirnoff remove_from_skipsteps(struct skiplist *head, struct superblock *block,
10213b3a8eb9SGleb Smirnoff     struct pf_opt_rule *por, struct pf_skip_step *active_list)
10223b3a8eb9SGleb Smirnoff {
10233b3a8eb9SGleb Smirnoff 	struct pf_skip_step *sk, *next;
10243b3a8eb9SGleb Smirnoff 	struct pf_opt_rule *p2;
10253b3a8eb9SGleb Smirnoff 	int i, found;
10263b3a8eb9SGleb Smirnoff 
10273b3a8eb9SGleb Smirnoff 	for (i = 0; i < PF_SKIP_COUNT; i++) {
10283b3a8eb9SGleb Smirnoff 		sk = TAILQ_FIRST(&block->sb_skipsteps[i]);
10293b3a8eb9SGleb Smirnoff 		if (sk == NULL || sk == active_list || sk->ps_count <= 1)
10303b3a8eb9SGleb Smirnoff 			continue;
10313b3a8eb9SGleb Smirnoff 		found = 0;
10323b3a8eb9SGleb Smirnoff 		do {
10333b3a8eb9SGleb Smirnoff 			TAILQ_FOREACH(p2, &sk->ps_rules, por_skip_entry[i])
10343b3a8eb9SGleb Smirnoff 				if (p2 == por) {
10353b3a8eb9SGleb Smirnoff 					TAILQ_REMOVE(&sk->ps_rules, p2,
10363b3a8eb9SGleb Smirnoff 					    por_skip_entry[i]);
10373b3a8eb9SGleb Smirnoff 					found = 1;
10383b3a8eb9SGleb Smirnoff 					sk->ps_count--;
10393b3a8eb9SGleb Smirnoff 					break;
10403b3a8eb9SGleb Smirnoff 				}
10413b3a8eb9SGleb Smirnoff 		} while (!found && (sk = TAILQ_NEXT(sk, ps_entry)));
10423b3a8eb9SGleb Smirnoff 		if (found && sk) {
10433b3a8eb9SGleb Smirnoff 			/* Does this change the sorting order? */
10443b3a8eb9SGleb Smirnoff 			while ((next = TAILQ_NEXT(sk, ps_entry)) &&
10453b3a8eb9SGleb Smirnoff 			    next->ps_count > sk->ps_count) {
10463b3a8eb9SGleb Smirnoff 				TAILQ_REMOVE(head, sk, ps_entry);
10473b3a8eb9SGleb Smirnoff 				TAILQ_INSERT_AFTER(head, next, sk, ps_entry);
10483b3a8eb9SGleb Smirnoff 			}
10493b3a8eb9SGleb Smirnoff #ifdef OPT_DEBUG
10503b3a8eb9SGleb Smirnoff 			next = TAILQ_NEXT(sk, ps_entry);
10513b3a8eb9SGleb Smirnoff 			assert(next == NULL || next->ps_count <= sk->ps_count);
10523b3a8eb9SGleb Smirnoff #endif /* OPT_DEBUG */
10533b3a8eb9SGleb Smirnoff 		}
10543b3a8eb9SGleb Smirnoff 	}
10553b3a8eb9SGleb Smirnoff }
10563b3a8eb9SGleb Smirnoff 
10573b3a8eb9SGleb Smirnoff 
10583b3a8eb9SGleb Smirnoff /* Compare two rules AF field for skiplist construction */
10593b3a8eb9SGleb Smirnoff int
skip_cmp_af(struct pfctl_rule * a,struct pfctl_rule * b)1060e9eb0941SKristof Provost skip_cmp_af(struct pfctl_rule *a, struct pfctl_rule *b)
10613b3a8eb9SGleb Smirnoff {
10623b3a8eb9SGleb Smirnoff 	if (a->af != b->af || a->af == 0)
10633b3a8eb9SGleb Smirnoff 		return (1);
10643b3a8eb9SGleb Smirnoff 	return (0);
10653b3a8eb9SGleb Smirnoff }
10663b3a8eb9SGleb Smirnoff 
10673b3a8eb9SGleb Smirnoff /* Compare two rules DIRECTION field for skiplist construction */
10683b3a8eb9SGleb Smirnoff int
skip_cmp_dir(struct pfctl_rule * a,struct pfctl_rule * b)1069e9eb0941SKristof Provost skip_cmp_dir(struct pfctl_rule *a, struct pfctl_rule *b)
10703b3a8eb9SGleb Smirnoff {
10713b3a8eb9SGleb Smirnoff 	if (a->direction == 0 || a->direction != b->direction)
10723b3a8eb9SGleb Smirnoff 		return (1);
10733b3a8eb9SGleb Smirnoff 	return (0);
10743b3a8eb9SGleb Smirnoff }
10753b3a8eb9SGleb Smirnoff 
10763b3a8eb9SGleb Smirnoff /* Compare two rules DST Address field for skiplist construction */
10773b3a8eb9SGleb Smirnoff int
skip_cmp_dst_addr(struct pfctl_rule * a,struct pfctl_rule * b)1078e9eb0941SKristof Provost skip_cmp_dst_addr(struct pfctl_rule *a, struct pfctl_rule *b)
10793b3a8eb9SGleb Smirnoff {
10803b3a8eb9SGleb Smirnoff 	if (a->dst.neg != b->dst.neg ||
10813b3a8eb9SGleb Smirnoff 	    a->dst.addr.type != b->dst.addr.type)
10823b3a8eb9SGleb Smirnoff 		return (1);
10833b3a8eb9SGleb Smirnoff 	/* XXX if (a->proto != b->proto && a->proto != 0 && b->proto != 0
10843b3a8eb9SGleb Smirnoff 	 *    && (a->proto == IPPROTO_TCP || a->proto == IPPROTO_UDP ||
10853b3a8eb9SGleb Smirnoff 	 *    a->proto == IPPROTO_ICMP
10863b3a8eb9SGleb Smirnoff 	 *	return (1);
10873b3a8eb9SGleb Smirnoff 	 */
10883b3a8eb9SGleb Smirnoff 	switch (a->dst.addr.type) {
10893b3a8eb9SGleb Smirnoff 	case PF_ADDR_ADDRMASK:
10903b3a8eb9SGleb Smirnoff 		if (memcmp(&a->dst.addr.v.a.addr, &b->dst.addr.v.a.addr,
10913b3a8eb9SGleb Smirnoff 		    sizeof(a->dst.addr.v.a.addr)) ||
10923b3a8eb9SGleb Smirnoff 		    memcmp(&a->dst.addr.v.a.mask, &b->dst.addr.v.a.mask,
10933b3a8eb9SGleb Smirnoff 		    sizeof(a->dst.addr.v.a.mask)) ||
10943b3a8eb9SGleb Smirnoff 		    (a->dst.addr.v.a.addr.addr32[0] == 0 &&
10953b3a8eb9SGleb Smirnoff 		    a->dst.addr.v.a.addr.addr32[1] == 0 &&
10963b3a8eb9SGleb Smirnoff 		    a->dst.addr.v.a.addr.addr32[2] == 0 &&
10973b3a8eb9SGleb Smirnoff 		    a->dst.addr.v.a.addr.addr32[3] == 0))
10983b3a8eb9SGleb Smirnoff 			return (1);
10993b3a8eb9SGleb Smirnoff 		return (0);
11003b3a8eb9SGleb Smirnoff 	case PF_ADDR_DYNIFTL:
11013b3a8eb9SGleb Smirnoff 		if (strcmp(a->dst.addr.v.ifname, b->dst.addr.v.ifname) != 0 ||
1102e2d84d5aSPedro F. Giffuni 		    a->dst.addr.iflags != b->dst.addr.iflags ||
11033b3a8eb9SGleb Smirnoff 		    memcmp(&a->dst.addr.v.a.mask, &b->dst.addr.v.a.mask,
11043b3a8eb9SGleb Smirnoff 		    sizeof(a->dst.addr.v.a.mask)))
11053b3a8eb9SGleb Smirnoff 			return (1);
11063b3a8eb9SGleb Smirnoff 		return (0);
11073b3a8eb9SGleb Smirnoff 	case PF_ADDR_NOROUTE:
11083b3a8eb9SGleb Smirnoff 	case PF_ADDR_URPFFAILED:
11093b3a8eb9SGleb Smirnoff 		return (0);
11103b3a8eb9SGleb Smirnoff 	case PF_ADDR_TABLE:
11113b3a8eb9SGleb Smirnoff 		return (strcmp(a->dst.addr.v.tblname, b->dst.addr.v.tblname));
11123b3a8eb9SGleb Smirnoff 	}
11133b3a8eb9SGleb Smirnoff 	return (1);
11143b3a8eb9SGleb Smirnoff }
11153b3a8eb9SGleb Smirnoff 
11163b3a8eb9SGleb Smirnoff /* Compare two rules DST port field for skiplist construction */
11173b3a8eb9SGleb Smirnoff int
skip_cmp_dst_port(struct pfctl_rule * a,struct pfctl_rule * b)1118e9eb0941SKristof Provost skip_cmp_dst_port(struct pfctl_rule *a, struct pfctl_rule *b)
11193b3a8eb9SGleb Smirnoff {
11203b3a8eb9SGleb Smirnoff 	/* XXX if (a->proto != b->proto && a->proto != 0 && b->proto != 0
11213b3a8eb9SGleb Smirnoff 	 *    && (a->proto == IPPROTO_TCP || a->proto == IPPROTO_UDP ||
11223b3a8eb9SGleb Smirnoff 	 *    a->proto == IPPROTO_ICMP
11233b3a8eb9SGleb Smirnoff 	 *	return (1);
11243b3a8eb9SGleb Smirnoff 	 */
11253b3a8eb9SGleb Smirnoff 	if (a->dst.port_op == PF_OP_NONE || a->dst.port_op != b->dst.port_op ||
11263b3a8eb9SGleb Smirnoff 	    a->dst.port[0] != b->dst.port[0] ||
11273b3a8eb9SGleb Smirnoff 	    a->dst.port[1] != b->dst.port[1])
11283b3a8eb9SGleb Smirnoff 		return (1);
11293b3a8eb9SGleb Smirnoff 	return (0);
11303b3a8eb9SGleb Smirnoff }
11313b3a8eb9SGleb Smirnoff 
11323b3a8eb9SGleb Smirnoff /* Compare two rules IFP field for skiplist construction */
11333b3a8eb9SGleb Smirnoff int
skip_cmp_ifp(struct pfctl_rule * a,struct pfctl_rule * b)1134e9eb0941SKristof Provost skip_cmp_ifp(struct pfctl_rule *a, struct pfctl_rule *b)
11353b3a8eb9SGleb Smirnoff {
11363b3a8eb9SGleb Smirnoff 	if (strcmp(a->ifname, b->ifname) || a->ifname[0] == '\0')
11373b3a8eb9SGleb Smirnoff 		return (1);
11383b3a8eb9SGleb Smirnoff 	return (a->ifnot != b->ifnot);
11393b3a8eb9SGleb Smirnoff }
11403b3a8eb9SGleb Smirnoff 
11413b3a8eb9SGleb Smirnoff /* Compare two rules PROTO field for skiplist construction */
11423b3a8eb9SGleb Smirnoff int
skip_cmp_proto(struct pfctl_rule * a,struct pfctl_rule * b)1143e9eb0941SKristof Provost skip_cmp_proto(struct pfctl_rule *a, struct pfctl_rule *b)
11443b3a8eb9SGleb Smirnoff {
11453b3a8eb9SGleb Smirnoff 	return (a->proto != b->proto || a->proto == 0);
11463b3a8eb9SGleb Smirnoff }
11473b3a8eb9SGleb Smirnoff 
11483b3a8eb9SGleb Smirnoff /* Compare two rules SRC addr field for skiplist construction */
11493b3a8eb9SGleb Smirnoff int
skip_cmp_src_addr(struct pfctl_rule * a,struct pfctl_rule * b)1150e9eb0941SKristof Provost skip_cmp_src_addr(struct pfctl_rule *a, struct pfctl_rule *b)
11513b3a8eb9SGleb Smirnoff {
11523b3a8eb9SGleb Smirnoff 	if (a->src.neg != b->src.neg ||
11533b3a8eb9SGleb Smirnoff 	    a->src.addr.type != b->src.addr.type)
11543b3a8eb9SGleb Smirnoff 		return (1);
11553b3a8eb9SGleb Smirnoff 	/* XXX if (a->proto != b->proto && a->proto != 0 && b->proto != 0
11563b3a8eb9SGleb Smirnoff 	 *    && (a->proto == IPPROTO_TCP || a->proto == IPPROTO_UDP ||
11573b3a8eb9SGleb Smirnoff 	 *    a->proto == IPPROTO_ICMP
11583b3a8eb9SGleb Smirnoff 	 *	return (1);
11593b3a8eb9SGleb Smirnoff 	 */
11603b3a8eb9SGleb Smirnoff 	switch (a->src.addr.type) {
11613b3a8eb9SGleb Smirnoff 	case PF_ADDR_ADDRMASK:
11623b3a8eb9SGleb Smirnoff 		if (memcmp(&a->src.addr.v.a.addr, &b->src.addr.v.a.addr,
11633b3a8eb9SGleb Smirnoff 		    sizeof(a->src.addr.v.a.addr)) ||
11643b3a8eb9SGleb Smirnoff 		    memcmp(&a->src.addr.v.a.mask, &b->src.addr.v.a.mask,
11653b3a8eb9SGleb Smirnoff 		    sizeof(a->src.addr.v.a.mask)) ||
11663b3a8eb9SGleb Smirnoff 		    (a->src.addr.v.a.addr.addr32[0] == 0 &&
11673b3a8eb9SGleb Smirnoff 		    a->src.addr.v.a.addr.addr32[1] == 0 &&
11683b3a8eb9SGleb Smirnoff 		    a->src.addr.v.a.addr.addr32[2] == 0 &&
11693b3a8eb9SGleb Smirnoff 		    a->src.addr.v.a.addr.addr32[3] == 0))
11703b3a8eb9SGleb Smirnoff 			return (1);
11713b3a8eb9SGleb Smirnoff 		return (0);
11723b3a8eb9SGleb Smirnoff 	case PF_ADDR_DYNIFTL:
11733b3a8eb9SGleb Smirnoff 		if (strcmp(a->src.addr.v.ifname, b->src.addr.v.ifname) != 0 ||
1174e2d84d5aSPedro F. Giffuni 		    a->src.addr.iflags != b->src.addr.iflags ||
11753b3a8eb9SGleb Smirnoff 		    memcmp(&a->src.addr.v.a.mask, &b->src.addr.v.a.mask,
11763b3a8eb9SGleb Smirnoff 		    sizeof(a->src.addr.v.a.mask)))
11773b3a8eb9SGleb Smirnoff 			return (1);
11783b3a8eb9SGleb Smirnoff 		return (0);
11793b3a8eb9SGleb Smirnoff 	case PF_ADDR_NOROUTE:
11803b3a8eb9SGleb Smirnoff 	case PF_ADDR_URPFFAILED:
11813b3a8eb9SGleb Smirnoff 		return (0);
11823b3a8eb9SGleb Smirnoff 	case PF_ADDR_TABLE:
11833b3a8eb9SGleb Smirnoff 		return (strcmp(a->src.addr.v.tblname, b->src.addr.v.tblname));
11843b3a8eb9SGleb Smirnoff 	}
11853b3a8eb9SGleb Smirnoff 	return (1);
11863b3a8eb9SGleb Smirnoff }
11873b3a8eb9SGleb Smirnoff 
11883b3a8eb9SGleb Smirnoff /* Compare two rules SRC port field for skiplist construction */
11893b3a8eb9SGleb Smirnoff int
skip_cmp_src_port(struct pfctl_rule * a,struct pfctl_rule * b)1190e9eb0941SKristof Provost skip_cmp_src_port(struct pfctl_rule *a, struct pfctl_rule *b)
11913b3a8eb9SGleb Smirnoff {
11923b3a8eb9SGleb Smirnoff 	if (a->src.port_op == PF_OP_NONE || a->src.port_op != b->src.port_op ||
11933b3a8eb9SGleb Smirnoff 	    a->src.port[0] != b->src.port[0] ||
11943b3a8eb9SGleb Smirnoff 	    a->src.port[1] != b->src.port[1])
11953b3a8eb9SGleb Smirnoff 		return (1);
11963b3a8eb9SGleb Smirnoff 	/* XXX if (a->proto != b->proto && a->proto != 0 && b->proto != 0
11973b3a8eb9SGleb Smirnoff 	 *    && (a->proto == IPPROTO_TCP || a->proto == IPPROTO_UDP ||
11983b3a8eb9SGleb Smirnoff 	 *    a->proto == IPPROTO_ICMP
11993b3a8eb9SGleb Smirnoff 	 *	return (1);
12003b3a8eb9SGleb Smirnoff 	 */
12013b3a8eb9SGleb Smirnoff 	return (0);
12023b3a8eb9SGleb Smirnoff }
12033b3a8eb9SGleb Smirnoff 
12043b3a8eb9SGleb Smirnoff 
12053b3a8eb9SGleb Smirnoff void
skip_init(void)12063b3a8eb9SGleb Smirnoff skip_init(void)
12073b3a8eb9SGleb Smirnoff {
12083b3a8eb9SGleb Smirnoff 	struct {
12093b3a8eb9SGleb Smirnoff 		char *name;
12103b3a8eb9SGleb Smirnoff 		int skipnum;
1211e9eb0941SKristof Provost 		int (*func)(struct pfctl_rule *, struct pfctl_rule *);
12123b3a8eb9SGleb Smirnoff 	} comps[] = PF_SKIP_COMPARITORS;
12133b3a8eb9SGleb Smirnoff 	int skipnum, i;
12143b3a8eb9SGleb Smirnoff 
12153b3a8eb9SGleb Smirnoff 	for (skipnum = 0; skipnum < PF_SKIP_COUNT; skipnum++) {
12163b3a8eb9SGleb Smirnoff 		for (i = 0; i < sizeof(comps)/sizeof(*comps); i++)
12173b3a8eb9SGleb Smirnoff 			if (comps[i].skipnum == skipnum) {
12183b3a8eb9SGleb Smirnoff 				skip_comparitors[skipnum] = comps[i].func;
12193b3a8eb9SGleb Smirnoff 				skip_comparitors_names[skipnum] = comps[i].name;
12203b3a8eb9SGleb Smirnoff 			}
12213b3a8eb9SGleb Smirnoff 	}
12223b3a8eb9SGleb Smirnoff 	for (skipnum = 0; skipnum < PF_SKIP_COUNT; skipnum++)
12233b3a8eb9SGleb Smirnoff 		if (skip_comparitors[skipnum] == NULL)
12243b3a8eb9SGleb Smirnoff 			errx(1, "Need to add skip step comparitor to pfctl?!");
12253b3a8eb9SGleb Smirnoff }
12263b3a8eb9SGleb Smirnoff 
12273b3a8eb9SGleb Smirnoff /*
12283b3a8eb9SGleb Smirnoff  * Add a host/netmask to a table
12293b3a8eb9SGleb Smirnoff  */
12303b3a8eb9SGleb Smirnoff int
add_opt_table(struct pfctl * pf,struct pf_opt_tbl ** tbl,sa_family_t af,struct pf_rule_addr * addr)12313b3a8eb9SGleb Smirnoff add_opt_table(struct pfctl *pf, struct pf_opt_tbl **tbl, sa_family_t af,
12323b3a8eb9SGleb Smirnoff     struct pf_rule_addr *addr)
12333b3a8eb9SGleb Smirnoff {
12343b3a8eb9SGleb Smirnoff #ifdef OPT_DEBUG
12353b3a8eb9SGleb Smirnoff 	char buf[128];
12363b3a8eb9SGleb Smirnoff #endif /* OPT_DEBUG */
12373b3a8eb9SGleb Smirnoff 	static int tablenum = 0;
12383b3a8eb9SGleb Smirnoff 	struct node_host node_host;
12393b3a8eb9SGleb Smirnoff 
12403b3a8eb9SGleb Smirnoff 	if (*tbl == NULL) {
12413b3a8eb9SGleb Smirnoff 		if ((*tbl = calloc(1, sizeof(**tbl))) == NULL ||
12423b3a8eb9SGleb Smirnoff 		    ((*tbl)->pt_buf = calloc(1, sizeof(*(*tbl)->pt_buf))) ==
12433b3a8eb9SGleb Smirnoff 		    NULL)
12443b3a8eb9SGleb Smirnoff 			err(1, "calloc");
1245a7d631f6SKristof Provost 		(*tbl)->pt_refcnt = 1;
12463b3a8eb9SGleb Smirnoff 		(*tbl)->pt_buf->pfrb_type = PFRB_ADDRS;
12473b3a8eb9SGleb Smirnoff 		SIMPLEQ_INIT(&(*tbl)->pt_nodes);
12483b3a8eb9SGleb Smirnoff 
12493b3a8eb9SGleb Smirnoff 		/* This is just a temporary table name */
12503b3a8eb9SGleb Smirnoff 		snprintf((*tbl)->pt_name, sizeof((*tbl)->pt_name), "%s%d",
1251*9f21a946SKristof Provost 		    PF_OPTIMIZER_TABLE_PFX, tablenum++);
12523b3a8eb9SGleb Smirnoff 		DEBUG("creating table <%s>", (*tbl)->pt_name);
12533b3a8eb9SGleb Smirnoff 	}
12543b3a8eb9SGleb Smirnoff 
12553b3a8eb9SGleb Smirnoff 	memset(&node_host, 0, sizeof(node_host));
12563b3a8eb9SGleb Smirnoff 	node_host.af = af;
12573b3a8eb9SGleb Smirnoff 	node_host.addr = addr->addr;
12583b3a8eb9SGleb Smirnoff 
12593b3a8eb9SGleb Smirnoff #ifdef OPT_DEBUG
12603b3a8eb9SGleb Smirnoff 	DEBUG("<%s> adding %s/%d", (*tbl)->pt_name, inet_ntop(af,
12613b3a8eb9SGleb Smirnoff 	    &node_host.addr.v.a.addr, buf, sizeof(buf)),
1262fb48e6d7SKristof Provost 	    unmask(&node_host.addr.v.a.mask));
12633b3a8eb9SGleb Smirnoff #endif /* OPT_DEBUG */
12643b3a8eb9SGleb Smirnoff 
12653b3a8eb9SGleb Smirnoff 	if (append_addr_host((*tbl)->pt_buf, &node_host, 0, 0)) {
12663b3a8eb9SGleb Smirnoff 		warn("failed to add host");
12673b3a8eb9SGleb Smirnoff 		return (1);
12683b3a8eb9SGleb Smirnoff 	}
12693b3a8eb9SGleb Smirnoff 	if (pf->opts & PF_OPT_VERBOSE) {
12703b3a8eb9SGleb Smirnoff 		struct node_tinit *ti;
12713b3a8eb9SGleb Smirnoff 
12723b3a8eb9SGleb Smirnoff 		if ((ti = calloc(1, sizeof(*ti))) == NULL)
12733b3a8eb9SGleb Smirnoff 			err(1, "malloc");
12743b3a8eb9SGleb Smirnoff 		if ((ti->host = malloc(sizeof(*ti->host))) == NULL)
12753b3a8eb9SGleb Smirnoff 			err(1, "malloc");
12763b3a8eb9SGleb Smirnoff 		memcpy(ti->host, &node_host, sizeof(*ti->host));
12773b3a8eb9SGleb Smirnoff 		SIMPLEQ_INSERT_TAIL(&(*tbl)->pt_nodes, ti, entries);
12783b3a8eb9SGleb Smirnoff 	}
12793b3a8eb9SGleb Smirnoff 
12803b3a8eb9SGleb Smirnoff 	(*tbl)->pt_rulecount++;
12813b3a8eb9SGleb Smirnoff 	if ((*tbl)->pt_rulecount == TABLE_THRESHOLD)
12823b3a8eb9SGleb Smirnoff 		DEBUG("table <%s> now faster than skip steps", (*tbl)->pt_name);
12833b3a8eb9SGleb Smirnoff 
12843b3a8eb9SGleb Smirnoff 	return (0);
12853b3a8eb9SGleb Smirnoff }
12863b3a8eb9SGleb Smirnoff 
12873b3a8eb9SGleb Smirnoff 
12883b3a8eb9SGleb Smirnoff /*
12893b3a8eb9SGleb Smirnoff  * Do the dirty work of choosing an unused table name and creating it.
12903b3a8eb9SGleb Smirnoff  * (be careful with the table name, it might already be used in another anchor)
12913b3a8eb9SGleb Smirnoff  */
12923b3a8eb9SGleb Smirnoff int
pf_opt_create_table(struct pfctl * pf,struct pf_opt_tbl * tbl)12933b3a8eb9SGleb Smirnoff pf_opt_create_table(struct pfctl *pf, struct pf_opt_tbl *tbl)
12943b3a8eb9SGleb Smirnoff {
12953b3a8eb9SGleb Smirnoff 	static int tablenum;
12963b3a8eb9SGleb Smirnoff 	struct pfr_table *t;
12973b3a8eb9SGleb Smirnoff 
12983b3a8eb9SGleb Smirnoff 	if (table_buffer.pfrb_type == 0) {
12993b3a8eb9SGleb Smirnoff 		/* Initialize the list of tables */
13003b3a8eb9SGleb Smirnoff 		table_buffer.pfrb_type = PFRB_TABLES;
13013b3a8eb9SGleb Smirnoff 		for (;;) {
13023b3a8eb9SGleb Smirnoff 			pfr_buf_grow(&table_buffer, table_buffer.pfrb_size);
13033b3a8eb9SGleb Smirnoff 			table_buffer.pfrb_size = table_buffer.pfrb_msize;
13043b3a8eb9SGleb Smirnoff 			if (pfr_get_tables(NULL, table_buffer.pfrb_caddr,
13053b3a8eb9SGleb Smirnoff 			    &table_buffer.pfrb_size, PFR_FLAG_ALLRSETS))
13063b3a8eb9SGleb Smirnoff 				err(1, "pfr_get_tables");
13073b3a8eb9SGleb Smirnoff 			if (table_buffer.pfrb_size <= table_buffer.pfrb_msize)
13083b3a8eb9SGleb Smirnoff 				break;
13093b3a8eb9SGleb Smirnoff 		}
13103b3a8eb9SGleb Smirnoff 		table_identifier = arc4random();
13113b3a8eb9SGleb Smirnoff 	}
13123b3a8eb9SGleb Smirnoff 
13133b3a8eb9SGleb Smirnoff 	/* XXX would be *really* nice to avoid duplicating identical tables */
13143b3a8eb9SGleb Smirnoff 
13153b3a8eb9SGleb Smirnoff 	/* Now we have to pick a table name that isn't used */
13163b3a8eb9SGleb Smirnoff again:
13173b3a8eb9SGleb Smirnoff 	DEBUG("translating temporary table <%s> to <%s%x_%d>", tbl->pt_name,
1318*9f21a946SKristof Provost 	    PF_OPTIMIZER_TABLE_PFX, table_identifier, tablenum);
13193b3a8eb9SGleb Smirnoff 	snprintf(tbl->pt_name, sizeof(tbl->pt_name), "%s%x_%d",
1320*9f21a946SKristof Provost 	    PF_OPTIMIZER_TABLE_PFX, table_identifier, tablenum);
13213b3a8eb9SGleb Smirnoff 	PFRB_FOREACH(t, &table_buffer) {
13223b3a8eb9SGleb Smirnoff 		if (strcasecmp(t->pfrt_name, tbl->pt_name) == 0) {
13233b3a8eb9SGleb Smirnoff 			/* Collision.  Try again */
13243b3a8eb9SGleb Smirnoff 			DEBUG("wow, table <%s> in use.  trying again",
13253b3a8eb9SGleb Smirnoff 			    tbl->pt_name);
13263b3a8eb9SGleb Smirnoff 			table_identifier = arc4random();
13273b3a8eb9SGleb Smirnoff 			goto again;
13283b3a8eb9SGleb Smirnoff 		}
13293b3a8eb9SGleb Smirnoff 	}
13303b3a8eb9SGleb Smirnoff 	tablenum++;
13313b3a8eb9SGleb Smirnoff 
13323b3a8eb9SGleb Smirnoff 
13333b3a8eb9SGleb Smirnoff 	if (pfctl_define_table(tbl->pt_name, PFR_TFLAG_CONST, 1,
1334809ba93cSKristof Provost 	    pf->astack[0]->path, tbl->pt_buf, pf->astack[0]->ruleset.tticket)) {
13353b3a8eb9SGleb Smirnoff 		warn("failed to create table %s in %s",
13363b3a8eb9SGleb Smirnoff 		    tbl->pt_name, pf->astack[0]->name);
13373b3a8eb9SGleb Smirnoff 		return (1);
13383b3a8eb9SGleb Smirnoff 	}
13393b3a8eb9SGleb Smirnoff 	return (0);
13403b3a8eb9SGleb Smirnoff }
13413b3a8eb9SGleb Smirnoff 
13423b3a8eb9SGleb Smirnoff /*
13433b3a8eb9SGleb Smirnoff  * Partition the flat ruleset into a list of distinct superblocks
13443b3a8eb9SGleb Smirnoff  */
13453b3a8eb9SGleb Smirnoff int
construct_superblocks(struct pfctl * pf,struct pf_opt_queue * opt_queue,struct superblocks * superblocks)13463b3a8eb9SGleb Smirnoff construct_superblocks(struct pfctl *pf, struct pf_opt_queue *opt_queue,
13473b3a8eb9SGleb Smirnoff     struct superblocks *superblocks)
13483b3a8eb9SGleb Smirnoff {
13493b3a8eb9SGleb Smirnoff 	struct superblock *block = NULL;
13503b3a8eb9SGleb Smirnoff 	struct pf_opt_rule *por;
13513b3a8eb9SGleb Smirnoff 	int i;
13523b3a8eb9SGleb Smirnoff 
13533b3a8eb9SGleb Smirnoff 	while (!TAILQ_EMPTY(opt_queue)) {
13543b3a8eb9SGleb Smirnoff 		por = TAILQ_FIRST(opt_queue);
13553b3a8eb9SGleb Smirnoff 		TAILQ_REMOVE(opt_queue, por, por_entry);
13563b3a8eb9SGleb Smirnoff 		if (block == NULL || !superblock_inclusive(block, por)) {
13573b3a8eb9SGleb Smirnoff 			if ((block = calloc(1, sizeof(*block))) == NULL) {
13583b3a8eb9SGleb Smirnoff 				warn("calloc");
13593b3a8eb9SGleb Smirnoff 				return (1);
13603b3a8eb9SGleb Smirnoff 			}
13613b3a8eb9SGleb Smirnoff 			TAILQ_INIT(&block->sb_rules);
13623b3a8eb9SGleb Smirnoff 			for (i = 0; i < PF_SKIP_COUNT; i++)
13633b3a8eb9SGleb Smirnoff 				TAILQ_INIT(&block->sb_skipsteps[i]);
13643b3a8eb9SGleb Smirnoff 			TAILQ_INSERT_TAIL(superblocks, block, sb_entry);
13653b3a8eb9SGleb Smirnoff 		}
13663b3a8eb9SGleb Smirnoff 		TAILQ_INSERT_TAIL(&block->sb_rules, por, por_entry);
13673b3a8eb9SGleb Smirnoff 	}
13683b3a8eb9SGleb Smirnoff 
13693b3a8eb9SGleb Smirnoff 	return (0);
13703b3a8eb9SGleb Smirnoff }
13713b3a8eb9SGleb Smirnoff 
13723b3a8eb9SGleb Smirnoff 
13733b3a8eb9SGleb Smirnoff /*
13743b3a8eb9SGleb Smirnoff  * Compare two rule addresses
13753b3a8eb9SGleb Smirnoff  */
13763b3a8eb9SGleb Smirnoff int
addrs_equal(struct pf_rule_addr * a,struct pf_rule_addr * b)13773b3a8eb9SGleb Smirnoff addrs_equal(struct pf_rule_addr *a, struct pf_rule_addr *b)
13783b3a8eb9SGleb Smirnoff {
13793b3a8eb9SGleb Smirnoff 	if (a->neg != b->neg)
13803b3a8eb9SGleb Smirnoff 		return (0);
13813b3a8eb9SGleb Smirnoff 	return (memcmp(&a->addr, &b->addr, sizeof(a->addr)) == 0);
13823b3a8eb9SGleb Smirnoff }
13833b3a8eb9SGleb Smirnoff 
13843b3a8eb9SGleb Smirnoff 
13853b3a8eb9SGleb Smirnoff /*
13863b3a8eb9SGleb Smirnoff  * The addresses are not equal, but can we combine them into one table?
13873b3a8eb9SGleb Smirnoff  */
13883b3a8eb9SGleb Smirnoff int
addrs_combineable(struct pf_rule_addr * a,struct pf_rule_addr * b)13893b3a8eb9SGleb Smirnoff addrs_combineable(struct pf_rule_addr *a, struct pf_rule_addr *b)
13903b3a8eb9SGleb Smirnoff {
13913b3a8eb9SGleb Smirnoff 	if (a->addr.type != PF_ADDR_ADDRMASK ||
13923b3a8eb9SGleb Smirnoff 	    b->addr.type != PF_ADDR_ADDRMASK)
13933b3a8eb9SGleb Smirnoff 		return (0);
13943b3a8eb9SGleb Smirnoff 	if (a->neg != b->neg || a->port_op != b->port_op ||
13953b3a8eb9SGleb Smirnoff 	    a->port[0] != b->port[0] || a->port[1] != b->port[1])
13963b3a8eb9SGleb Smirnoff 		return (0);
13973b3a8eb9SGleb Smirnoff 	return (1);
13983b3a8eb9SGleb Smirnoff }
13993b3a8eb9SGleb Smirnoff 
14003b3a8eb9SGleb Smirnoff 
14013b3a8eb9SGleb Smirnoff /*
14023b3a8eb9SGleb Smirnoff  * Are we allowed to combine these two rules
14033b3a8eb9SGleb Smirnoff  */
14043b3a8eb9SGleb Smirnoff int
rules_combineable(struct pfctl_rule * p1,struct pfctl_rule * p2)1405e9eb0941SKristof Provost rules_combineable(struct pfctl_rule *p1, struct pfctl_rule *p2)
14063b3a8eb9SGleb Smirnoff {
1407e9eb0941SKristof Provost 	struct pfctl_rule a, b;
14083b3a8eb9SGleb Smirnoff 
14093b3a8eb9SGleb Smirnoff 	comparable_rule(&a, p1, COMBINED);
14103b3a8eb9SGleb Smirnoff 	comparable_rule(&b, p2, COMBINED);
14113b3a8eb9SGleb Smirnoff 	return (memcmp(&a, &b, sizeof(a)) == 0);
14123b3a8eb9SGleb Smirnoff }
14133b3a8eb9SGleb Smirnoff 
14143b3a8eb9SGleb Smirnoff 
14153b3a8eb9SGleb Smirnoff /*
14163b3a8eb9SGleb Smirnoff  * Can a rule be included inside a superblock
14173b3a8eb9SGleb Smirnoff  */
14183b3a8eb9SGleb Smirnoff int
superblock_inclusive(struct superblock * block,struct pf_opt_rule * por)14193b3a8eb9SGleb Smirnoff superblock_inclusive(struct superblock *block, struct pf_opt_rule *por)
14203b3a8eb9SGleb Smirnoff {
1421e9eb0941SKristof Provost 	struct pfctl_rule a, b;
14223b3a8eb9SGleb Smirnoff 	int i, j;
14233b3a8eb9SGleb Smirnoff 
14243b3a8eb9SGleb Smirnoff 	/* First check for hard breaks */
14253b3a8eb9SGleb Smirnoff 	for (i = 0; i < sizeof(pf_rule_desc)/sizeof(*pf_rule_desc); i++) {
14263b3a8eb9SGleb Smirnoff 		if (pf_rule_desc[i].prf_type == BARRIER) {
14273b3a8eb9SGleb Smirnoff 			for (j = 0; j < pf_rule_desc[i].prf_size; j++)
14283b3a8eb9SGleb Smirnoff 				if (((char *)&por->por_rule)[j +
14293b3a8eb9SGleb Smirnoff 				    pf_rule_desc[i].prf_offset] != 0)
14303b3a8eb9SGleb Smirnoff 					return (0);
14313b3a8eb9SGleb Smirnoff 		}
14323b3a8eb9SGleb Smirnoff 	}
14333b3a8eb9SGleb Smirnoff 
14343b3a8eb9SGleb Smirnoff 	/* per-rule src-track is also a hard break */
14353b3a8eb9SGleb Smirnoff 	if (por->por_rule.rule_flag & PFRULE_RULESRCTRACK)
14363b3a8eb9SGleb Smirnoff 		return (0);
14373b3a8eb9SGleb Smirnoff 
14383b3a8eb9SGleb Smirnoff 	/*
14393b3a8eb9SGleb Smirnoff 	 * Have to handle interface groups separately.  Consider the following
14403b3a8eb9SGleb Smirnoff 	 * rules:
14413b3a8eb9SGleb Smirnoff 	 *	block on EXTIFS to any port 22
14423b3a8eb9SGleb Smirnoff 	 *	pass  on em0 to any port 22
14433b3a8eb9SGleb Smirnoff 	 * (where EXTIFS is an arbitrary interface group)
14443b3a8eb9SGleb Smirnoff 	 * The optimizer may decide to re-order the pass rule in front of the
14453b3a8eb9SGleb Smirnoff 	 * block rule.  But what if EXTIFS includes em0???  Such a reordering
14463b3a8eb9SGleb Smirnoff 	 * would change the meaning of the ruleset.
14473b3a8eb9SGleb Smirnoff 	 * We can't just lookup the EXTIFS group and check if em0 is a member
14483b3a8eb9SGleb Smirnoff 	 * because the user is allowed to add interfaces to a group during
14493b3a8eb9SGleb Smirnoff 	 * runtime.
14503b3a8eb9SGleb Smirnoff 	 * Ergo interface groups become a defacto superblock break :-(
14513b3a8eb9SGleb Smirnoff 	 */
14523b3a8eb9SGleb Smirnoff 	if (interface_group(por->por_rule.ifname) ||
14533b3a8eb9SGleb Smirnoff 	    interface_group(TAILQ_FIRST(&block->sb_rules)->por_rule.ifname)) {
14543b3a8eb9SGleb Smirnoff 		if (strcasecmp(por->por_rule.ifname,
14553b3a8eb9SGleb Smirnoff 		    TAILQ_FIRST(&block->sb_rules)->por_rule.ifname) != 0)
14563b3a8eb9SGleb Smirnoff 			return (0);
14573b3a8eb9SGleb Smirnoff 	}
14583b3a8eb9SGleb Smirnoff 
14593b3a8eb9SGleb Smirnoff 	comparable_rule(&a, &TAILQ_FIRST(&block->sb_rules)->por_rule, NOMERGE);
14603b3a8eb9SGleb Smirnoff 	comparable_rule(&b, &por->por_rule, NOMERGE);
14613b3a8eb9SGleb Smirnoff 	if (memcmp(&a, &b, sizeof(a)) == 0)
14623b3a8eb9SGleb Smirnoff 		return (1);
14633b3a8eb9SGleb Smirnoff 
14643b3a8eb9SGleb Smirnoff #ifdef OPT_DEBUG
14653b3a8eb9SGleb Smirnoff 	for (i = 0; i < sizeof(por->por_rule); i++) {
14663b3a8eb9SGleb Smirnoff 		int closest = -1;
14673b3a8eb9SGleb Smirnoff 		if (((u_int8_t *)&a)[i] != ((u_int8_t *)&b)[i]) {
14683b3a8eb9SGleb Smirnoff 			for (j = 0; j < sizeof(pf_rule_desc) /
14693b3a8eb9SGleb Smirnoff 			    sizeof(*pf_rule_desc); j++) {
14703b3a8eb9SGleb Smirnoff 				if (i >= pf_rule_desc[j].prf_offset &&
14713b3a8eb9SGleb Smirnoff 				    i < pf_rule_desc[j].prf_offset +
14723b3a8eb9SGleb Smirnoff 				    pf_rule_desc[j].prf_size) {
14733b3a8eb9SGleb Smirnoff 					DEBUG("superblock break @ %d due to %s",
14743b3a8eb9SGleb Smirnoff 					    por->por_rule.nr,
14753b3a8eb9SGleb Smirnoff 					    pf_rule_desc[j].prf_name);
14763b3a8eb9SGleb Smirnoff 					return (0);
14773b3a8eb9SGleb Smirnoff 				}
14783b3a8eb9SGleb Smirnoff 				if (i > pf_rule_desc[j].prf_offset) {
14793b3a8eb9SGleb Smirnoff 					if (closest == -1 ||
14803b3a8eb9SGleb Smirnoff 					    i-pf_rule_desc[j].prf_offset <
14813b3a8eb9SGleb Smirnoff 					    i-pf_rule_desc[closest].prf_offset)
14823b3a8eb9SGleb Smirnoff 						closest = j;
14833b3a8eb9SGleb Smirnoff 				}
14843b3a8eb9SGleb Smirnoff 			}
14853b3a8eb9SGleb Smirnoff 
14863b3a8eb9SGleb Smirnoff 			if (closest >= 0)
14879f1beeaeSKajetan Staszkiewicz 				DEBUG("superblock break @ %d on %s+%zxh",
14883b3a8eb9SGleb Smirnoff 				    por->por_rule.nr,
14893b3a8eb9SGleb Smirnoff 				    pf_rule_desc[closest].prf_name,
14903b3a8eb9SGleb Smirnoff 				    i - pf_rule_desc[closest].prf_offset -
14913b3a8eb9SGleb Smirnoff 				    pf_rule_desc[closest].prf_size);
14923b3a8eb9SGleb Smirnoff 			else
14933b3a8eb9SGleb Smirnoff 				DEBUG("superblock break @ %d on field @ %d",
14943b3a8eb9SGleb Smirnoff 				    por->por_rule.nr, i);
14953b3a8eb9SGleb Smirnoff 			return (0);
14963b3a8eb9SGleb Smirnoff 		}
14973b3a8eb9SGleb Smirnoff 	}
14983b3a8eb9SGleb Smirnoff #endif /* OPT_DEBUG */
14993b3a8eb9SGleb Smirnoff 
15003b3a8eb9SGleb Smirnoff 	return (0);
15013b3a8eb9SGleb Smirnoff }
15023b3a8eb9SGleb Smirnoff 
15033b3a8eb9SGleb Smirnoff 
15043b3a8eb9SGleb Smirnoff /*
15053b3a8eb9SGleb Smirnoff  * Figure out if an interface name is an actual interface or actually a
15063b3a8eb9SGleb Smirnoff  * group of interfaces.
15073b3a8eb9SGleb Smirnoff  */
15083b3a8eb9SGleb Smirnoff int
interface_group(const char * ifname)15093b3a8eb9SGleb Smirnoff interface_group(const char *ifname)
15103b3a8eb9SGleb Smirnoff {
15117296d6c9SKristof Provost 	int			s;
15127296d6c9SKristof Provost 	struct ifgroupreq	ifgr;
15137296d6c9SKristof Provost 
15143b3a8eb9SGleb Smirnoff 	if (ifname == NULL || !ifname[0])
15153b3a8eb9SGleb Smirnoff 		return (0);
15163b3a8eb9SGleb Smirnoff 
15177296d6c9SKristof Provost 	s = get_query_socket();
15187296d6c9SKristof Provost 
15197296d6c9SKristof Provost 	memset(&ifgr, 0, sizeof(ifgr));
15207296d6c9SKristof Provost 	strlcpy(ifgr.ifgr_name, ifname, IFNAMSIZ);
15217296d6c9SKristof Provost 	if (ioctl(s, SIOCGIFGMEMB, (caddr_t)&ifgr) == -1) {
15227296d6c9SKristof Provost 		if (errno == ENOENT)
15233b3a8eb9SGleb Smirnoff 			return (0);
15243b3a8eb9SGleb Smirnoff 		else
15257296d6c9SKristof Provost 			err(1, "SIOCGIFGMEMB");
15267296d6c9SKristof Provost 	}
15277296d6c9SKristof Provost 
15283b3a8eb9SGleb Smirnoff 	return (1);
15293b3a8eb9SGleb Smirnoff }
15303b3a8eb9SGleb Smirnoff 
15313b3a8eb9SGleb Smirnoff 
15323b3a8eb9SGleb Smirnoff /*
15333b3a8eb9SGleb Smirnoff  * Make a rule that can directly compared by memcmp()
15343b3a8eb9SGleb Smirnoff  */
15353b3a8eb9SGleb Smirnoff void
comparable_rule(struct pfctl_rule * dst,const struct pfctl_rule * src,int type)1536e9eb0941SKristof Provost comparable_rule(struct pfctl_rule *dst, const struct pfctl_rule *src, int type)
15373b3a8eb9SGleb Smirnoff {
15383b3a8eb9SGleb Smirnoff 	int i;
15393b3a8eb9SGleb Smirnoff 	/*
15403b3a8eb9SGleb Smirnoff 	 * To simplify the comparison, we just zero out the fields that are
15413b3a8eb9SGleb Smirnoff 	 * allowed to be different and then do a simple memcmp()
15423b3a8eb9SGleb Smirnoff 	 */
15433b3a8eb9SGleb Smirnoff 	memcpy(dst, src, sizeof(*dst));
15443b3a8eb9SGleb Smirnoff 	for (i = 0; i < sizeof(pf_rule_desc)/sizeof(*pf_rule_desc); i++)
15453b3a8eb9SGleb Smirnoff 		if (pf_rule_desc[i].prf_type >= type) {
15463b3a8eb9SGleb Smirnoff #ifdef OPT_DEBUG
15473b3a8eb9SGleb Smirnoff 			assert(pf_rule_desc[i].prf_type != NEVER ||
15483b3a8eb9SGleb Smirnoff 			    *(((char *)dst) + pf_rule_desc[i].prf_offset) == 0);
15493b3a8eb9SGleb Smirnoff #endif /* OPT_DEBUG */
15503b3a8eb9SGleb Smirnoff 			memset(((char *)dst) + pf_rule_desc[i].prf_offset, 0,
15513b3a8eb9SGleb Smirnoff 			    pf_rule_desc[i].prf_size);
15523b3a8eb9SGleb Smirnoff 		}
15533b3a8eb9SGleb Smirnoff }
15543b3a8eb9SGleb Smirnoff 
15553b3a8eb9SGleb Smirnoff 
15563b3a8eb9SGleb Smirnoff /*
15573b3a8eb9SGleb Smirnoff  * Remove superset information from two rules so we can directly compare them
15583b3a8eb9SGleb Smirnoff  * with memcmp()
15593b3a8eb9SGleb Smirnoff  */
15603b3a8eb9SGleb Smirnoff void
exclude_supersets(struct pfctl_rule * super,struct pfctl_rule * sub)1561e9eb0941SKristof Provost exclude_supersets(struct pfctl_rule *super, struct pfctl_rule *sub)
15623b3a8eb9SGleb Smirnoff {
15633b3a8eb9SGleb Smirnoff 	if (super->ifname[0] == '\0')
15643b3a8eb9SGleb Smirnoff 		memset(sub->ifname, 0, sizeof(sub->ifname));
15653b3a8eb9SGleb Smirnoff 	if (super->direction == PF_INOUT)
15663b3a8eb9SGleb Smirnoff 		sub->direction = PF_INOUT;
15673b3a8eb9SGleb Smirnoff 	if ((super->proto == 0 || super->proto == sub->proto) &&
15683b3a8eb9SGleb Smirnoff 	    super->flags == 0 && super->flagset == 0 && (sub->flags ||
15693b3a8eb9SGleb Smirnoff 	    sub->flagset)) {
15703b3a8eb9SGleb Smirnoff 		sub->flags = super->flags;
15713b3a8eb9SGleb Smirnoff 		sub->flagset = super->flagset;
15723b3a8eb9SGleb Smirnoff 	}
15733b3a8eb9SGleb Smirnoff 	if (super->proto == 0)
15743b3a8eb9SGleb Smirnoff 		sub->proto = 0;
15753b3a8eb9SGleb Smirnoff 
15763b3a8eb9SGleb Smirnoff 	if (super->src.port_op == 0) {
15773b3a8eb9SGleb Smirnoff 		sub->src.port_op = 0;
15783b3a8eb9SGleb Smirnoff 		sub->src.port[0] = 0;
15793b3a8eb9SGleb Smirnoff 		sub->src.port[1] = 0;
15803b3a8eb9SGleb Smirnoff 	}
15813b3a8eb9SGleb Smirnoff 	if (super->dst.port_op == 0) {
15823b3a8eb9SGleb Smirnoff 		sub->dst.port_op = 0;
15833b3a8eb9SGleb Smirnoff 		sub->dst.port[0] = 0;
15843b3a8eb9SGleb Smirnoff 		sub->dst.port[1] = 0;
15853b3a8eb9SGleb Smirnoff 	}
15863b3a8eb9SGleb Smirnoff 
15873b3a8eb9SGleb Smirnoff 	if (super->src.addr.type == PF_ADDR_ADDRMASK && !super->src.neg &&
15883b3a8eb9SGleb Smirnoff 	    !sub->src.neg && super->src.addr.v.a.mask.addr32[0] == 0 &&
15893b3a8eb9SGleb Smirnoff 	    super->src.addr.v.a.mask.addr32[1] == 0 &&
15903b3a8eb9SGleb Smirnoff 	    super->src.addr.v.a.mask.addr32[2] == 0 &&
15913b3a8eb9SGleb Smirnoff 	    super->src.addr.v.a.mask.addr32[3] == 0)
15923b3a8eb9SGleb Smirnoff 		memset(&sub->src.addr, 0, sizeof(sub->src.addr));
15933b3a8eb9SGleb Smirnoff 	else if (super->src.addr.type == PF_ADDR_ADDRMASK &&
15943b3a8eb9SGleb Smirnoff 	    sub->src.addr.type == PF_ADDR_ADDRMASK &&
15953b3a8eb9SGleb Smirnoff 	    super->src.neg == sub->src.neg &&
15963b3a8eb9SGleb Smirnoff 	    super->af == sub->af &&
1597fb48e6d7SKristof Provost 	    unmask(&super->src.addr.v.a.mask) <
1598fb48e6d7SKristof Provost 	    unmask(&sub->src.addr.v.a.mask) &&
15993b3a8eb9SGleb Smirnoff 	    super->src.addr.v.a.addr.addr32[0] ==
16003b3a8eb9SGleb Smirnoff 	    (sub->src.addr.v.a.addr.addr32[0] &
16013b3a8eb9SGleb Smirnoff 	    super->src.addr.v.a.mask.addr32[0]) &&
16023b3a8eb9SGleb Smirnoff 	    super->src.addr.v.a.addr.addr32[1] ==
16033b3a8eb9SGleb Smirnoff 	    (sub->src.addr.v.a.addr.addr32[1] &
16043b3a8eb9SGleb Smirnoff 	    super->src.addr.v.a.mask.addr32[1]) &&
16053b3a8eb9SGleb Smirnoff 	    super->src.addr.v.a.addr.addr32[2] ==
16063b3a8eb9SGleb Smirnoff 	    (sub->src.addr.v.a.addr.addr32[2] &
16073b3a8eb9SGleb Smirnoff 	    super->src.addr.v.a.mask.addr32[2]) &&
16083b3a8eb9SGleb Smirnoff 	    super->src.addr.v.a.addr.addr32[3] ==
16093b3a8eb9SGleb Smirnoff 	    (sub->src.addr.v.a.addr.addr32[3] &
16103b3a8eb9SGleb Smirnoff 	    super->src.addr.v.a.mask.addr32[3])) {
16113b3a8eb9SGleb Smirnoff 		/* sub->src.addr is a subset of super->src.addr/mask */
16123b3a8eb9SGleb Smirnoff 		memcpy(&sub->src.addr, &super->src.addr, sizeof(sub->src.addr));
16133b3a8eb9SGleb Smirnoff 	}
16143b3a8eb9SGleb Smirnoff 
16153b3a8eb9SGleb Smirnoff 	if (super->dst.addr.type == PF_ADDR_ADDRMASK && !super->dst.neg &&
16163b3a8eb9SGleb Smirnoff 	    !sub->dst.neg && super->dst.addr.v.a.mask.addr32[0] == 0 &&
16173b3a8eb9SGleb Smirnoff 	    super->dst.addr.v.a.mask.addr32[1] == 0 &&
16183b3a8eb9SGleb Smirnoff 	    super->dst.addr.v.a.mask.addr32[2] == 0 &&
16193b3a8eb9SGleb Smirnoff 	    super->dst.addr.v.a.mask.addr32[3] == 0)
16203b3a8eb9SGleb Smirnoff 		memset(&sub->dst.addr, 0, sizeof(sub->dst.addr));
16213b3a8eb9SGleb Smirnoff 	else if (super->dst.addr.type == PF_ADDR_ADDRMASK &&
16223b3a8eb9SGleb Smirnoff 	    sub->dst.addr.type == PF_ADDR_ADDRMASK &&
16233b3a8eb9SGleb Smirnoff 	    super->dst.neg == sub->dst.neg &&
16243b3a8eb9SGleb Smirnoff 	    super->af == sub->af &&
1625fb48e6d7SKristof Provost 	    unmask(&super->dst.addr.v.a.mask) <
1626fb48e6d7SKristof Provost 	    unmask(&sub->dst.addr.v.a.mask) &&
16273b3a8eb9SGleb Smirnoff 	    super->dst.addr.v.a.addr.addr32[0] ==
16283b3a8eb9SGleb Smirnoff 	    (sub->dst.addr.v.a.addr.addr32[0] &
16293b3a8eb9SGleb Smirnoff 	    super->dst.addr.v.a.mask.addr32[0]) &&
16303b3a8eb9SGleb Smirnoff 	    super->dst.addr.v.a.addr.addr32[1] ==
16313b3a8eb9SGleb Smirnoff 	    (sub->dst.addr.v.a.addr.addr32[1] &
16323b3a8eb9SGleb Smirnoff 	    super->dst.addr.v.a.mask.addr32[1]) &&
16333b3a8eb9SGleb Smirnoff 	    super->dst.addr.v.a.addr.addr32[2] ==
16343b3a8eb9SGleb Smirnoff 	    (sub->dst.addr.v.a.addr.addr32[2] &
16353b3a8eb9SGleb Smirnoff 	    super->dst.addr.v.a.mask.addr32[2]) &&
16363b3a8eb9SGleb Smirnoff 	    super->dst.addr.v.a.addr.addr32[3] ==
16373b3a8eb9SGleb Smirnoff 	    (sub->dst.addr.v.a.addr.addr32[3] &
16383b3a8eb9SGleb Smirnoff 	    super->dst.addr.v.a.mask.addr32[3])) {
16393b3a8eb9SGleb Smirnoff 		/* sub->dst.addr is a subset of super->dst.addr/mask */
16403b3a8eb9SGleb Smirnoff 		memcpy(&sub->dst.addr, &super->dst.addr, sizeof(sub->dst.addr));
16413b3a8eb9SGleb Smirnoff 	}
16423b3a8eb9SGleb Smirnoff 
16433b3a8eb9SGleb Smirnoff 	if (super->af == 0)
16443b3a8eb9SGleb Smirnoff 		sub->af = 0;
16453b3a8eb9SGleb Smirnoff }
16463b3a8eb9SGleb Smirnoff 
16473b3a8eb9SGleb Smirnoff 
16483b3a8eb9SGleb Smirnoff void
superblock_free(struct pfctl * pf,struct superblock * block)16493b3a8eb9SGleb Smirnoff superblock_free(struct pfctl *pf, struct superblock *block)
16503b3a8eb9SGleb Smirnoff {
16513b3a8eb9SGleb Smirnoff 	struct pf_opt_rule *por;
16523b3a8eb9SGleb Smirnoff 	while ((por = TAILQ_FIRST(&block->sb_rules))) {
16533b3a8eb9SGleb Smirnoff 		TAILQ_REMOVE(&block->sb_rules, por, por_entry);
1654a7d631f6SKristof Provost 		pf_opt_table_unref(por->por_src_tbl);
1655a7d631f6SKristof Provost 		pf_opt_table_unref(por->por_dst_tbl);
16563b3a8eb9SGleb Smirnoff 		free(por);
16573b3a8eb9SGleb Smirnoff 	}
16583b3a8eb9SGleb Smirnoff 	if (block->sb_profiled_block)
16593b3a8eb9SGleb Smirnoff 		superblock_free(pf, block->sb_profiled_block);
16603b3a8eb9SGleb Smirnoff 	free(block);
16613b3a8eb9SGleb Smirnoff }
16623b3a8eb9SGleb Smirnoff 
1663a7d631f6SKristof Provost struct pf_opt_tbl *
pf_opt_table_ref(struct pf_opt_tbl * pt)1664a7d631f6SKristof Provost pf_opt_table_ref(struct pf_opt_tbl *pt)
1665a7d631f6SKristof Provost {
1666a7d631f6SKristof Provost 	/* parser does not run concurrently, we don't need atomic ops. */
1667a7d631f6SKristof Provost 	if (pt != NULL)
1668a7d631f6SKristof Provost 		pt->pt_refcnt++;
1669a7d631f6SKristof Provost 
1670a7d631f6SKristof Provost 	return (pt);
1671a7d631f6SKristof Provost }
1672a7d631f6SKristof Provost 
1673a7d631f6SKristof Provost void
pf_opt_table_unref(struct pf_opt_tbl * pt)1674a7d631f6SKristof Provost pf_opt_table_unref(struct pf_opt_tbl *pt)
1675a7d631f6SKristof Provost {
1676a7d631f6SKristof Provost 	if ((pt != NULL) && ((--pt->pt_refcnt) == 0)) {
1677a7d631f6SKristof Provost 		if (pt->pt_buf != NULL) {
1678a7d631f6SKristof Provost 			pfr_buf_clear(pt->pt_buf);
1679a7d631f6SKristof Provost 			free(pt->pt_buf);
1680a7d631f6SKristof Provost 		}
1681a7d631f6SKristof Provost 		free(pt);
1682a7d631f6SKristof Provost 	}
1683a7d631f6SKristof Provost }
1684