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