summaryrefslogtreecommitdiff
path: root/sbin/pfctl/pfctl_optimize.c
blob: 9522dbf784ae5f7c90277b9fc48b802123fa22e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
/*	$OpenBSD: pfctl_optimize.c,v 1.35 2015/01/21 21:50:33 deraadt Exp $ */

/*
 * Copyright (c) 2004 Mike Frantzen <frantzen@openbsd.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/socket.h>

#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <net/pfvar.h>

#include <assert.h>
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "pfctl_parser.h"
#include "pfctl.h"

/* The size at which a table becomes faster than individual rules */
#define TABLE_THRESHOLD		6


/* #define OPT_DEBUG	1 */
#ifdef OPT_DEBUG
# define DEBUG(str, v...) \
	printf("%s: " str "\n", __FUNCTION__ , ## v)
#else
# define DEBUG(str, v...) ((void)0)
#endif


/*
 * A container that lets us sort a superblock to optimize the skip step jumps
 */
struct pf_skip_step {
	int				ps_count;	/* number of items */
	TAILQ_HEAD( , pf_opt_rule)	ps_rules;
	TAILQ_ENTRY(pf_skip_step)	ps_entry;
};


/*
 * A superblock is a block of adjacent rules of similar action.  If there
 * are five PASS rules in a row, they all become members of a superblock.
 * Once we have a superblock, we are free to re-order any rules within it
 * in order to improve performance; if a packet is passed, it doesn't matter
 * who passed it.
 */
struct superblock {
	TAILQ_HEAD( , pf_opt_rule)		 sb_rules;
	TAILQ_ENTRY(superblock)			 sb_entry;
	struct superblock			*sb_profiled_block;
	TAILQ_HEAD(skiplist, pf_skip_step)	 sb_skipsteps[PF_SKIP_COUNT];
};
TAILQ_HEAD(superblocks, superblock);


/*
 * Description of the PF rule structure.
 */
enum {
    BARRIER,	/* the presence of the field puts the rule in it's own block */
    BREAK,	/* the field may not differ between rules in a superblock */
    NOMERGE,	/* the field may not differ between rules when combined */
    COMBINED,	/* the field may itself be combined with other rules */
    DC,		/* we just don't care about the field */
    NEVER};	/* we should never see this field set?!? */
struct pf_rule_field {
	const char	*prf_name;
	int		 prf_type;
	size_t		 prf_offset;
	size_t		 prf_size;
} pf_rule_desc[] = {
#define PF_RULE_FIELD(field, ty)	\
    {#field,				\
    ty,					\
    offsetof(struct pf_rule, field),	\
    sizeof(((struct pf_rule *)0)->field)}


    /*
     * The presence of these fields in a rule put the rule in it's own
     * superblock.  Thus it will not be optimized.  It also prevents the
     * rule from being re-ordered at all.
     */
    PF_RULE_FIELD(label,		BARRIER),
    PF_RULE_FIELD(prob,			BARRIER),
    PF_RULE_FIELD(max_states,		BARRIER),
    PF_RULE_FIELD(max_src_nodes,	BARRIER),
    PF_RULE_FIELD(max_src_states,	BARRIER),
    PF_RULE_FIELD(max_src_conn,		BARRIER),
    PF_RULE_FIELD(max_src_conn_rate,	BARRIER),
    PF_RULE_FIELD(anchor,		BARRIER),	/* for now */

    /*
     * These fields must be the same between all rules in the same superblock.
     * These rules are allowed to be re-ordered but only among like rules.
     * For instance we can re-order all 'tag "foo"' rules because they have the
     * same tag.  But we can not re-order between a 'tag "foo"' and a
     * 'tag "bar"' since that would change the meaning of the ruleset.
     */
    PF_RULE_FIELD(tagname,		BREAK),
    PF_RULE_FIELD(keep_state,		BREAK),
    PF_RULE_FIELD(qname,		BREAK),
    PF_RULE_FIELD(pqname,		BREAK),
    PF_RULE_FIELD(rt,			BREAK),
    PF_RULE_FIELD(allow_opts,		BREAK),
    PF_RULE_FIELD(rule_flag,		BREAK),
    PF_RULE_FIELD(action,		BREAK),
    PF_RULE_FIELD(log,			BREAK),
    PF_RULE_FIELD(quick,		BREAK),
    PF_RULE_FIELD(return_ttl,		BREAK),
    PF_RULE_FIELD(overload_tblname,	BREAK),
    PF_RULE_FIELD(flush,		BREAK),
    PF_RULE_FIELD(rdr,			BREAK),
    PF_RULE_FIELD(nat,			BREAK),
    PF_RULE_FIELD(logif,		BREAK),
    PF_RULE_FIELD(route,		BREAK),
    PF_RULE_FIELD(rtableid,		BREAK),

    /*
     * Any fields not listed in this structure act as BREAK fields
     */


    /*
     * These fields must not differ when we merge two rules together but
     * their difference isn't enough to put the rules in different superblocks.
     * There are no problems re-ordering any rules with these fields.
     */
    PF_RULE_FIELD(af,			NOMERGE),
    PF_RULE_FIELD(ifnot,		NOMERGE),
    PF_RULE_FIELD(ifname,		NOMERGE),	/* hack for IF groups */
    PF_RULE_FIELD(match_tag_not,	NOMERGE),
    PF_RULE_FIELD(match_tagname,	NOMERGE),
    PF_RULE_FIELD(os_fingerprint,	NOMERGE),
    PF_RULE_FIELD(timeout,		NOMERGE),
    PF_RULE_FIELD(return_icmp,		NOMERGE),
    PF_RULE_FIELD(return_icmp6,		NOMERGE),
    PF_RULE_FIELD(uid,			NOMERGE),
    PF_RULE_FIELD(gid,			NOMERGE),
    PF_RULE_FIELD(direction,		NOMERGE),
    PF_RULE_FIELD(proto,		NOMERGE),
    PF_RULE_FIELD(type,			NOMERGE),
    PF_RULE_FIELD(code,			NOMERGE),
    PF_RULE_FIELD(flags,		NOMERGE),
    PF_RULE_FIELD(flagset,		NOMERGE),
    PF_RULE_FIELD(tos,			NOMERGE),
    PF_RULE_FIELD(src.port,		NOMERGE),
    PF_RULE_FIELD(dst.port,		NOMERGE),
    PF_RULE_FIELD(src.port_op,		NOMERGE),
    PF_RULE_FIELD(dst.port_op,		NOMERGE),
    PF_RULE_FIELD(src.neg,		NOMERGE),
    PF_RULE_FIELD(dst.neg,		NOMERGE),
    PF_RULE_FIELD(onrdomain,		NOMERGE),
    PF_RULE_FIELD(naf,			NOMERGE),

    /* These fields can be merged */
    PF_RULE_FIELD(src.addr,		COMBINED),
    PF_RULE_FIELD(dst.addr,		COMBINED),

    /* We just don't care about these fields.  They're set by the kernel */
    PF_RULE_FIELD(skip,			DC),
    PF_RULE_FIELD(evaluations,		DC),
    PF_RULE_FIELD(packets,		DC),
    PF_RULE_FIELD(bytes,		DC),
    PF_RULE_FIELD(kif,			DC),
    PF_RULE_FIELD(states_cur,		DC),
    PF_RULE_FIELD(states_tot,		DC),
    PF_RULE_FIELD(src_nodes,		DC),
    PF_RULE_FIELD(nr,			DC),
    PF_RULE_FIELD(entries,		DC),
    PF_RULE_FIELD(qid,			DC),
    PF_RULE_FIELD(pqid,			DC),
    PF_RULE_FIELD(anchor_relative,	DC),
    PF_RULE_FIELD(anchor_wildcard,	DC),
    PF_RULE_FIELD(tag,			DC),
    PF_RULE_FIELD(match_tag,		DC),
    PF_RULE_FIELD(overload_tbl,		DC),

    /* These fields should never be set in a PASS/BLOCK rule XXX fix*/
    PF_RULE_FIELD(max_mss,		NEVER),
    PF_RULE_FIELD(min_ttl,		NEVER),
    PF_RULE_FIELD(set_tos,		NEVER),
};



int	addrs_combineable(struct pf_rule_addr *, struct pf_rule_addr *);
int	addrs_equal(struct pf_rule_addr *, struct pf_rule_addr *);
int	block_feedback(struct pfctl *, struct superblock *);
int	combine_rules(struct pfctl *, struct superblock *);
void	comparable_rule(struct pf_rule *, const struct pf_rule *, int);
int	construct_superblocks(struct pfctl *, struct pf_opt_queue *,
	    struct superblocks *);
void	exclude_supersets(struct pf_rule *, struct pf_rule *);
int	interface_group(const char *);
int	load_feedback_profile(struct pfctl *, struct superblocks *);
int	optimize_superblock(struct pfctl *, struct superblock *);
void	remove_from_skipsteps(struct skiplist *, struct superblock *,
	    struct pf_opt_rule *, struct pf_skip_step *);
int	remove_identical_rules(struct pfctl *, struct superblock *);
int	reorder_rules(struct pfctl *, struct superblock *, int);
int	rules_combineable(struct pf_rule *, struct pf_rule *);
void	skip_append(struct superblock *, int, struct pf_skip_step *,
	    struct pf_opt_rule *);
int	skip_compare(int, struct pf_skip_step *, struct pf_opt_rule *);
void	skip_init(void);
int	skip_cmp_af(struct pf_rule *, struct pf_rule *);
int	skip_cmp_dir(struct pf_rule *, struct pf_rule *);
int	skip_cmp_rdom(struct pf_rule *, struct pf_rule *);
int	skip_cmp_dst_addr(struct pf_rule *, struct pf_rule *);
int	skip_cmp_dst_port(struct pf_rule *, struct pf_rule *);
int	skip_cmp_ifp(struct pf_rule *, struct pf_rule *);
int	skip_cmp_proto(struct pf_rule *, struct pf_rule *);
int	skip_cmp_src_addr(struct pf_rule *, struct pf_rule *);
int	skip_cmp_src_port(struct pf_rule *, struct pf_rule *);
int	superblock_inclusive(struct superblock *, struct pf_opt_rule *);
void	superblock_free(struct pfctl *, struct superblock *);


int (*skip_comparitors[PF_SKIP_COUNT])(struct pf_rule *, struct pf_rule *);
const char *skip_comparitors_names[PF_SKIP_COUNT];
#define PF_SKIP_COMPARITORS {				\
    { "ifp", PF_SKIP_IFP, skip_cmp_ifp },		\
    { "dir", PF_SKIP_DIR, skip_cmp_dir },		\
    { "rdomain", PF_SKIP_RDOM, skip_cmp_rdom },		\
    { "af", PF_SKIP_AF, skip_cmp_af },			\
    { "proto", PF_SKIP_PROTO, skip_cmp_proto },		\
    { "saddr", PF_SKIP_SRC_ADDR, skip_cmp_src_addr },	\
    { "daddr", PF_SKIP_DST_ADDR, skip_cmp_dst_addr },	\
    { "sport", PF_SKIP_SRC_PORT, skip_cmp_src_port },	\
    { "dport", PF_SKIP_DST_PORT, skip_cmp_dst_port }	\
}

struct pfr_buffer table_buffer;
int table_identifier;


int
pfctl_optimize_ruleset(struct pfctl *pf, struct pf_ruleset *rs)
{
	struct superblocks superblocks;
	struct pf_opt_queue opt_queue;
	struct superblock *block;
	struct pf_opt_rule *por;
	struct pf_rule *r;
	struct pf_rulequeue *old_rules;

	DEBUG("optimizing ruleset");
	memset(&table_buffer, 0, sizeof(table_buffer));
	skip_init();
	TAILQ_INIT(&opt_queue);

	old_rules = rs->rules.active.ptr;
	rs->rules.active.ptr = rs->rules.inactive.ptr;
	rs->rules.inactive.ptr = old_rules;

	/*
	 * XXX expanding the pf_opt_rule format throughout pfctl might allow
	 * us to avoid all this copying.
	 */
	while ((r = TAILQ_FIRST(rs->rules.inactive.ptr)) != NULL) {
		TAILQ_REMOVE(rs->rules.inactive.ptr, r, entries);
		if ((por = calloc(1, sizeof(*por))) == NULL)
			err(1, "calloc");
		memcpy(&por->por_rule, r, sizeof(*r));

		TAILQ_INSERT_TAIL(&opt_queue, por, por_entry);
	}

	TAILQ_INIT(&superblocks);
	if (construct_superblocks(pf, &opt_queue, &superblocks))
		goto error;

	if (pf->optimize & PF_OPTIMIZE_PROFILE) {
		if (load_feedback_profile(pf, &superblocks))
			goto error;
	}

	TAILQ_FOREACH(block, &superblocks, sb_entry) {
		if (optimize_superblock(pf, block))
			goto error;
	}

	rs->anchor->refcnt = 0;
	while ((block = TAILQ_FIRST(&superblocks))) {
		TAILQ_REMOVE(&superblocks, block, sb_entry);

		while ((por = TAILQ_FIRST(&block->sb_rules))) {
			TAILQ_REMOVE(&block->sb_rules, por, por_entry);
			por->por_rule.nr = rs->anchor->refcnt++;
			if ((r = calloc(1, sizeof(*r))) == NULL)
				err(1, "calloc");
			memcpy(r, &por->por_rule, sizeof(*r));
			TAILQ_INSERT_TAIL(rs->rules.active.ptr, r, entries);
			free(por);
		}
		free(block);
	}

	return (0);

error:
	while ((por = TAILQ_FIRST(&opt_queue))) {
		TAILQ_REMOVE(&opt_queue, por, por_entry);
		if (por->por_src_tbl) {
			pfr_buf_clear(por->por_src_tbl->pt_buf);
			free(por->por_src_tbl->pt_buf);
			free(por->por_src_tbl);
		}
		if (por->por_dst_tbl) {
			pfr_buf_clear(por->por_dst_tbl->pt_buf);
			free(por->por_dst_tbl->pt_buf);
			free(por->por_dst_tbl);
		}
		free(por);
	}
	while ((block = TAILQ_FIRST(&superblocks))) {
		TAILQ_REMOVE(&superblocks, block, sb_entry);
		superblock_free(pf, block);
	}
	return (1);
}


/*
 * Go ahead and optimize a superblock
 */
int
optimize_superblock(struct pfctl *pf, struct superblock *block)
{
#ifdef OPT_DEBUG
	struct pf_opt_rule *por;
#endif /* OPT_DEBUG */

	/* We have a few optimization passes:
	 *   1) remove duplicate rules or rules that are a subset of other
	 *      rules
	 *   2) combine otherwise identical rules with different IP addresses
	 *      into a single rule and put the addresses in a table.
	 *   3) re-order the rules to improve kernel skip steps
	 *   4) re-order the 'quick' rules based on feedback from the
	 *      active ruleset statistics
	 *
	 * XXX combine_rules() doesn't combine v4 and v6 rules.  would just
	 *     have to keep af in the table container, make af 'COMBINE' and
	 *     twiddle the af on the merged rule
	 * XXX maybe add a weighting to the metric on skipsteps when doing
	 *     reordering.  sometimes two sequential tables will be better
	 *     that four consecutive interfaces.
	 * XXX need to adjust the skipstep count of everything after PROTO,
	 *     since they aren't actually checked on a proto mismatch in
	 *     pf_test_{tcp, udp, icmp}()
	 * XXX should i treat proto=0, af=0 or dir=0 special in skepstep
	 *     calculation since they are a DC?
	 * XXX keep last skiplist of last superblock to influence this
	 *     superblock.  '5 inet6 log' should make '3 inet6' come before '4
	 *     inet' in the next superblock.
	 * XXX would be useful to add tables for ports
	 * XXX we can also re-order some mutually exclusive superblocks to
	 *     try merging superblocks before any of these optimization passes.
	 *     for instance a single 'log in' rule in the middle of non-logging
	 *     out rules.
	 */

	/* shortcut.  there will be a lot of 1-rule superblocks */
	if (!TAILQ_NEXT(TAILQ_FIRST(&block->sb_rules), por_entry))
		return (0);

#ifdef OPT_DEBUG
	printf("--- Superblock ---\n");
	TAILQ_FOREACH(por, &block->sb_rules, por_entry) {
		printf("  ");
		print_rule(&por->por_rule, por->por_rule.anchor ?
		    por->por_rule.anchor->name : "", PF_OPT_DEBUG);
	}
#endif /* OPT_DEBUG */


	if (remove_identical_rules(pf, block))
		return (1);
	if (combine_rules(pf, block))
		return (1);
	if ((pf->optimize & PF_OPTIMIZE_PROFILE) &&
	    TAILQ_FIRST(&block->sb_rules)->por_rule.quick &&
	    block->sb_profiled_block) {
		if (block_feedback(pf, block))
			return (1);
	} else if (reorder_rules(pf, block, 0)) {
		return (1);
	}

	/*
	 * Don't add any optimization passes below reorder_rules().  It will
	 * have divided superblocks into smaller blocks for further refinement
	 * and doesn't put them back together again.  What once was a true
	 * superblock might have been split into multiple superblocks.
	 */

#ifdef OPT_DEBUG
	printf("--- END Superblock ---\n");
#endif /* OPT_DEBUG */
	return (0);
}


/*
 * Optimization pass #1: remove identical rules
 */
int
remove_identical_rules(struct pfctl *pf, struct superblock *block)
{
	struct pf_opt_rule *por1, *por2, *por_next, *por2_next;
	struct pf_rule a, a2, b, b2;

	for (por1 = TAILQ_FIRST(&block->sb_rules); por1; por1 = por_next) {
		por_next = TAILQ_NEXT(por1, por_entry);
		for (por2 = por_next; por2; por2 = por2_next) {
			por2_next = TAILQ_NEXT(por2, por_entry);
			comparable_rule(&a, &por1->por_rule, DC);
			comparable_rule(&b, &por2->por_rule, DC);
			memcpy(&a2, &a, sizeof(a2));
			memcpy(&b2, &b, sizeof(b2));

			exclude_supersets(&a, &b);
			exclude_supersets(&b2, &a2);
			if (memcmp(&a, &b, sizeof(a)) == 0) {
				DEBUG("removing identical rule  nr%d = *nr%d*",
				    por1->por_rule.nr, por2->por_rule.nr);
				TAILQ_REMOVE(&block->sb_rules, por2, por_entry);
				if (por_next == por2)
					por_next = TAILQ_NEXT(por1, por_entry);
				free(por2);
			} else if (memcmp(&a2, &b2, sizeof(a2)) == 0) {
				DEBUG("removing identical rule  *nr%d* = nr%d",
				    por1->por_rule.nr, por2->por_rule.nr);
				TAILQ_REMOVE(&block->sb_rules, por1, por_entry);
				free(por1);
				break;
			}
		}
	}

	return (0);
}


/*
 * Optimization pass #2: combine similar rules with different addresses
 * into a single rule and a table
 */
int
combine_rules(struct pfctl *pf, struct superblock *block)
{
	struct pf_opt_rule *p1, *p2, *por_next;
	int src_eq, dst_eq;

	/* First we make a pass to combine the rules.  O(n log n) */
	TAILQ_FOREACH(p1, &block->sb_rules, por_entry) {
		for (p2 = TAILQ_NEXT(p1, por_entry); p2; p2 = por_next) {
			por_next = TAILQ_NEXT(p2, por_entry);

			src_eq = addrs_equal(&p1->por_rule.src,
			    &p2->por_rule.src);
			dst_eq = addrs_equal(&p1->por_rule.dst,
			    &p2->por_rule.dst);

			if (src_eq && !dst_eq && p1->por_src_tbl == NULL &&
			    p2->por_dst_tbl == NULL &&
			    p2->por_src_tbl == NULL &&
			    rules_combineable(&p1->por_rule, &p2->por_rule) &&
			    addrs_combineable(&p1->por_rule.dst,
			    &p2->por_rule.dst)) {
				DEBUG("can combine rules  nr%d = nr%d",
				    p1->por_rule.nr, p2->por_rule.nr);
				if (p1->por_dst_tbl == NULL &&
				    add_opt_table(pf, &p1->por_dst_tbl,
				    p1->por_rule.af, &p1->por_rule.dst, NULL))
					return (1);
				if (add_opt_table(pf, &p1->por_dst_tbl,
				    p1->por_rule.af, &p2->por_rule.dst, NULL))
					return (1);
				p2->por_dst_tbl = p1->por_dst_tbl;
				if (p1->por_dst_tbl->pt_rulecount >=
				    TABLE_THRESHOLD) {
					TAILQ_REMOVE(&block->sb_rules, p2,
					    por_entry);
					free(p2);
				}
			} else if (!src_eq && dst_eq && p1->por_dst_tbl == NULL
			    && p2->por_src_tbl == NULL &&
			    p2->por_dst_tbl == NULL &&
			    rules_combineable(&p1->por_rule, &p2->por_rule) &&
			    addrs_combineable(&p1->por_rule.src,
			    &p2->por_rule.src)) {
				DEBUG("can combine rules  nr%d = nr%d",
				    p1->por_rule.nr, p2->por_rule.nr);
				if (p1->por_src_tbl == NULL &&
				    add_opt_table(pf, &p1->por_src_tbl,
				    p1->por_rule.af, &p1->por_rule.src, NULL))
					return (1);
				if (add_opt_table(pf, &p1->por_src_tbl,
				    p1->por_rule.af, &p2->por_rule.src, NULL))
					return (1);
				p2->por_src_tbl = p1->por_src_tbl;
				if (p1->por_src_tbl->pt_rulecount >=
				    TABLE_THRESHOLD) {
					TAILQ_REMOVE(&block->sb_rules, p2,
					    por_entry);
					free(p2);
				}
			}
		}
	}


	/*
	 * Then we make a final pass to create a valid table name and
	 * insert the name into the rules.
	 * Convert translation/routing mapping pools to tables as well.
	 */
	for (p1 = TAILQ_FIRST(&block->sb_rules); p1; p1 = por_next) {
		por_next = TAILQ_NEXT(p1, por_entry);
		assert(p1->por_src_tbl == NULL || p1->por_dst_tbl == NULL);

		if (p1->por_src_tbl && p1->por_src_tbl->pt_rulecount >=
		    TABLE_THRESHOLD) {
			if (p1->por_src_tbl->pt_generated) {
				/* This rule is included in a table */
				TAILQ_REMOVE(&block->sb_rules, p1, por_entry);
				free(p1);
				continue;
			}
			p1->por_src_tbl->pt_generated = 1;

			if ((pf->opts & PF_OPT_NOACTION) == 0 &&
			    pf_opt_create_table(pf, p1->por_src_tbl))
				return (1);

			pf->tdirty = 1;

			if (pf->opts & PF_OPT_VERBOSE)
				print_tabledef(p1->por_src_tbl->pt_name,
				    PFR_TFLAG_CONST, 1,
				    &p1->por_src_tbl->pt_nodes);

			memset(&p1->por_rule.src.addr, 0,
			    sizeof(p1->por_rule.src.addr));
			p1->por_rule.src.addr.type = PF_ADDR_TABLE;
			strlcpy(p1->por_rule.src.addr.v.tblname,
			    p1->por_src_tbl->pt_name,
			    sizeof(p1->por_rule.src.addr.v.tblname));

			pfr_buf_clear(p1->por_src_tbl->pt_buf);
			free(p1->por_src_tbl->pt_buf);
			p1->por_src_tbl->pt_buf = NULL;
		}
		if (p1->por_dst_tbl && p1->por_dst_tbl->pt_rulecount >=
		    TABLE_THRESHOLD) {
			if (p1->por_dst_tbl->pt_generated) {
				/* This rule is included in a table */
				TAILQ_REMOVE(&block->sb_rules, p1, por_entry);
				free(p1);
				continue;
			}
			p1->por_dst_tbl->pt_generated = 1;

			if ((pf->opts & PF_OPT_NOACTION) == 0 &&
			    pf_opt_create_table(pf, p1->por_dst_tbl))
				return (1);
			pf->tdirty = 1;

			if (pf->opts & PF_OPT_VERBOSE)
				print_tabledef(p1->por_dst_tbl->pt_name,
				    PFR_TFLAG_CONST, 1,
				    &p1->por_dst_tbl->pt_nodes);

			memset(&p1->por_rule.dst.addr, 0,
			    sizeof(p1->por_rule.dst.addr));
			p1->por_rule.dst.addr.type = PF_ADDR_TABLE;
			strlcpy(p1->por_rule.dst.addr.v.tblname,
			    p1->por_dst_tbl->pt_name,
			    sizeof(p1->por_rule.dst.addr.v.tblname));

			pfr_buf_clear(p1->por_dst_tbl->pt_buf);
			free(p1->por_dst_tbl->pt_buf);
			p1->por_dst_tbl->pt_buf = NULL;
		}
	}

	return (0);
}


/*
 * Optimization pass #3: re-order rules to improve skip steps
 */
int
reorder_rules(struct pfctl *pf, struct superblock *block, int depth)
{
	struct superblock *newblock;
	struct pf_skip_step *skiplist;
	struct pf_opt_rule *por;
	int i, largest, largest_list, rule_count = 0;
	TAILQ_HEAD( , pf_opt_rule) head;

	/*
	 * Calculate the best-case skip steps.  We put each rule in a list
	 * of other rules with common fields
	 */
	for (i = 0; i < PF_SKIP_COUNT; i++) {
		TAILQ_FOREACH(por, &block->sb_rules, por_entry) {
			TAILQ_FOREACH(skiplist, &block->sb_skipsteps[i],
			    ps_entry) {
				if (skip_compare(i, skiplist, por) == 0)
					break;
			}
			if (skiplist == NULL) {
				if ((skiplist = calloc(1, sizeof(*skiplist))) ==
				    NULL)
					err(1, "calloc");
				TAILQ_INIT(&skiplist->ps_rules);
				TAILQ_INSERT_TAIL(&block->sb_skipsteps[i],
				    skiplist, ps_entry);
			}
			skip_append(block, i, skiplist, por);
		}
	}

	TAILQ_FOREACH(por, &block->sb_rules, por_entry)
		rule_count++;

	/*
	 * Now we're going to ignore any fields that are identical between
	 * all of the rules in the superblock and those fields which differ
	 * between every rule in the superblock.
	 */
	largest = 0;
	for (i = 0; i < PF_SKIP_COUNT; i++) {
		skiplist = TAILQ_FIRST(&block->sb_skipsteps[i]);
		if (skiplist->ps_count == rule_count) {
			DEBUG("(%d) original skipstep '%s' is all rules",
			    depth, skip_comparitors_names[i]);
			skiplist->ps_count = 0;
		} else if (skiplist->ps_count == 1) {
			skiplist->ps_count = 0;
		} else {
			DEBUG("(%d) original skipstep '%s' largest jump is %d",
			    depth, skip_comparitors_names[i],
			    skiplist->ps_count);
			if (skiplist->ps_count > largest)
				largest = skiplist->ps_count;
		}
	}
	if (largest == 0) {
		/* Ugh.  There is NO commonality in the superblock on which
		 * optimize the skipsteps optimization.
		 */
		goto done;
	}

	/*
	 * Now we're going to empty the superblock rule list and re-create
	 * it based on a more optimal skipstep order.
	 */
	TAILQ_INIT(&head);
	while ((por = TAILQ_FIRST(&block->sb_rules))) {
		TAILQ_REMOVE(&block->sb_rules, por, por_entry);
		TAILQ_INSERT_TAIL(&head, por, por_entry);
	}


	while (!TAILQ_EMPTY(&head)) {
		largest = 1;

		/*
		 * Find the most useful skip steps remaining
		 */
		for (i = 0; i < PF_SKIP_COUNT; i++) {
			skiplist = TAILQ_FIRST(&block->sb_skipsteps[i]);
			if (skiplist->ps_count > largest) {
				largest = skiplist->ps_count;
				largest_list = i;
			}
		}

		if (largest <= 1) {
			/*
			 * Nothing useful left.  Leave remaining rules in order.
			 */
			DEBUG("(%d) no more commonality for skip steps", depth);
			while ((por = TAILQ_FIRST(&head))) {
				TAILQ_REMOVE(&head, por, por_entry);
				TAILQ_INSERT_TAIL(&block->sb_rules, por,
				    por_entry);
			}
		} else {
			/*
			 * There is commonality.  Extract those common rules
			 * and place them in the ruleset adjacent to each
			 * other.
			 */
			skiplist = TAILQ_FIRST(&block->sb_skipsteps[
			    largest_list]);
			DEBUG("(%d) skipstep '%s' largest jump is %d @ #%d",
			    depth, skip_comparitors_names[largest_list],
			    largest, TAILQ_FIRST(&TAILQ_FIRST(&block->
			    sb_skipsteps [largest_list])->ps_rules)->
			    por_rule.nr);
			TAILQ_REMOVE(&block->sb_skipsteps[largest_list],
			    skiplist, ps_entry);


			/*
			 * There may be further commonality inside these
			 * rules.  So we'll split them off into they're own
			 * superblock and pass it back into the optimizer.
			 */
			if (skiplist->ps_count > 2) {
				if ((newblock = calloc(1, sizeof(*newblock)))
				    == NULL) {
					warn("calloc");
					return (1);
				}
				TAILQ_INIT(&newblock->sb_rules);
				for (i = 0; i < PF_SKIP_COUNT; i++)
					TAILQ_INIT(&newblock->sb_skipsteps[i]);
				TAILQ_INSERT_BEFORE(block, newblock, sb_entry);
				DEBUG("(%d) splitting off %d rules from superblock @ #%d",
				    depth, skiplist->ps_count,
				    TAILQ_FIRST(&skiplist->ps_rules)->
				    por_rule.nr);
			} else {
				newblock = block;
			}

			while ((por = TAILQ_FIRST(&skiplist->ps_rules))) {
				TAILQ_REMOVE(&head, por, por_entry);
				TAILQ_REMOVE(&skiplist->ps_rules, por,
				    por_skip_entry[largest_list]);
				TAILQ_INSERT_TAIL(&newblock->sb_rules, por,
				    por_entry);

				/* Remove this rule from all other skiplists */
				remove_from_skipsteps(&block->sb_skipsteps[
				    largest_list], block, por, skiplist);
			}
			free(skiplist);
			if (newblock != block)
				if (reorder_rules(pf, newblock, depth + 1))
					return (1);
		}
	}

done:
	for (i = 0; i < PF_SKIP_COUNT; i++) {
		while ((skiplist = TAILQ_FIRST(&block->sb_skipsteps[i]))) {
			TAILQ_REMOVE(&block->sb_skipsteps[i], skiplist,
			    ps_entry);
			free(skiplist);
		}
	}

	return (0);
}


/*
 * Optimization pass #4: re-order 'quick' rules based on feedback from the
 * currently running ruleset
 */
int
block_feedback(struct pfctl *pf, struct superblock *block)
{
	TAILQ_HEAD( , pf_opt_rule) queue;
	struct pf_opt_rule *por1, *por2;
	u_int64_t total_count = 0;
	struct pf_rule a, b;


	/*
	 * Walk through all of the profiled superblock's rules and copy
	 * the counters onto our rules.
	 */
	TAILQ_FOREACH(por1, &block->sb_profiled_block->sb_rules, por_entry) {
		comparable_rule(&a, &por1->por_rule, DC);
		total_count += por1->por_rule.packets[0] +
		    por1->por_rule.packets[1];
		TAILQ_FOREACH(por2, &block->sb_rules, por_entry) {
			if (por2->por_profile_count)
				continue;
			comparable_rule(&b, &por2->por_rule, DC);
			if (memcmp(&a, &b, sizeof(a)) == 0) {
				por2->por_profile_count =
				    por1->por_rule.packets[0] +
				    por1->por_rule.packets[1];
				break;
			}
		}
	}
	superblock_free(pf, block->sb_profiled_block);
	block->sb_profiled_block = NULL;

	/*
	 * Now we pull all of the rules off the superblock and re-insert them
	 * in sorted order.
	 */

	TAILQ_INIT(&queue);
	while ((por1 = TAILQ_FIRST(&block->sb_rules)) != NULL) {
		TAILQ_REMOVE(&block->sb_rules, por1, por_entry);
		TAILQ_INSERT_TAIL(&queue, por1, por_entry);
	}

	while ((por1 = TAILQ_FIRST(&queue)) != NULL) {
		TAILQ_REMOVE(&queue, por1, por_entry);
/* XXX I should sort all of the unused rules based on skip steps */
		TAILQ_FOREACH(por2, &block->sb_rules, por_entry) {
			if (por1->por_profile_count > por2->por_profile_count) {
				TAILQ_INSERT_BEFORE(por2, por1, por_entry);
				break;
			}
		}
		if (por2 == NULL)
			TAILQ_INSERT_TAIL(&block->sb_rules, por1, por_entry);
	}

	return (0);
}


/*
 * Load the current ruleset from the kernel and try to associate them with
 * the ruleset we're optimizing.
 */
int
load_feedback_profile(struct pfctl *pf, struct superblocks *superblocks)
{
	struct superblock *block, *blockcur;
	struct superblocks prof_superblocks;
	struct pf_opt_rule *por;
	struct pf_opt_queue queue;
	struct pfioc_rule pr;
	struct pf_rule a, b;
	int nr, mnr;

	TAILQ_INIT(&queue);
	TAILQ_INIT(&prof_superblocks);

	memset(&pr, 0, sizeof(pr));
	pr.rule.action = PF_PASS;
	if (ioctl(pf->dev, DIOCGETRULES, &pr)) {
		warn("DIOCGETRULES");
		return (1);
	}
	mnr = pr.nr;

	DEBUG("Loading %d active rules for a feedback profile", mnr);
	for (nr = 0; nr < mnr; ++nr) {
		struct pf_ruleset *rs;
		if ((por = calloc(1, sizeof(*por))) == NULL) {
			warn("calloc");
			return (1);
		}
		pr.nr = nr;
		if (ioctl(pf->dev, DIOCGETRULE, &pr)) {
			warn("DIOCGETRULES");
			free(por);
			return (1);
		}
		memcpy(&por->por_rule, &pr.rule, sizeof(por->por_rule));
		rs = pf_find_or_create_ruleset(pr.anchor_call);
		por->por_rule.anchor = rs->anchor;
		TAILQ_INSERT_TAIL(&queue, por, por_entry);

		/* XXX pfctl_get_pool(pf->dev, &pr.rule.rpool, nr, pr.ticket,
		 *         PF_PASS, pf->anchor) ???
		 * ... pfctl_clear_pool(&pr.rule.rpool)
		 */
	}

	if (construct_superblocks(pf, &queue, &prof_superblocks))
		return (1);


	/*
	 * Now we try to associate the active ruleset's superblocks with
	 * the superblocks we're compiling.
	 */
	block = TAILQ_FIRST(superblocks);
	blockcur = TAILQ_FIRST(&prof_superblocks);
	while (block && blockcur) {
		comparable_rule(&a, &TAILQ_FIRST(&block->sb_rules)->por_rule,
		    BREAK);
		comparable_rule(&b, &TAILQ_FIRST(&blockcur->sb_rules)->por_rule,
		    BREAK);
		if (memcmp(&a, &b, sizeof(a)) == 0) {
			/* The two superblocks lined up */
			block->sb_profiled_block = blockcur;
		} else {
			DEBUG("superblocks don't line up between #%d and #%d",
			    TAILQ_FIRST(&block->sb_rules)->por_rule.nr,
			    TAILQ_FIRST(&blockcur->sb_rules)->por_rule.nr);
			break;
		}
		block = TAILQ_NEXT(block, sb_entry);
		blockcur = TAILQ_NEXT(blockcur, sb_entry);
	}



	/* Free any superblocks we couldn't link */
	while (blockcur) {
		block = TAILQ_NEXT(blockcur, sb_entry);
		superblock_free(pf, blockcur);
		blockcur = block;
	}
	return (0);
}


/*
 * Compare a rule to a skiplist to see if the rule is a member
 */
int
skip_compare(int skipnum, struct pf_skip_step *skiplist,
    struct pf_opt_rule *por)
{
	struct pf_rule *a, *b;
	if (skipnum >= PF_SKIP_COUNT || skipnum < 0)
		errx(1, "skip_compare() out of bounds");
	a = &por->por_rule;
	b = &TAILQ_FIRST(&skiplist->ps_rules)->por_rule;

	return ((skip_comparitors[skipnum])(a, b));
}


/*
 * Add a rule to a skiplist
 */
void
skip_append(struct superblock *superblock, int skipnum,
    struct pf_skip_step *skiplist, struct pf_opt_rule *por)
{
	struct pf_skip_step *prev;

	skiplist->ps_count++;
	TAILQ_INSERT_TAIL(&skiplist->ps_rules, por, por_skip_entry[skipnum]);

	/* Keep the list of skiplists sorted by whichever is larger */
	while ((prev = TAILQ_PREV(skiplist, skiplist, ps_entry)) &&
	    prev->ps_count < skiplist->ps_count) {
		TAILQ_REMOVE(&superblock->sb_skipsteps[skipnum],
		    skiplist, ps_entry);
		TAILQ_INSERT_BEFORE(prev, skiplist, ps_entry);
	}
}


/*
 * Remove a rule from the other skiplist calculations.
 */
void
remove_from_skipsteps(struct skiplist *head, struct superblock *block,
    struct pf_opt_rule *por, struct pf_skip_step *active_list)
{
	struct pf_skip_step *sk, *next;
	struct pf_opt_rule *p2;
	int i, found;

	for (i = 0; i < PF_SKIP_COUNT; i++) {
		sk = TAILQ_FIRST(&block->sb_skipsteps[i]);
		if (sk == NULL || sk == active_list || sk->ps_count <= 1)
			continue;
		found = 0;
		do {
			TAILQ_FOREACH(p2, &sk->ps_rules, por_skip_entry[i])
				if (p2 == por) {
					TAILQ_REMOVE(&sk->ps_rules, p2,
					    por_skip_entry[i]);
					found = 1;
					sk->ps_count--;
					break;
				}
		} while (!found && (sk = TAILQ_NEXT(sk, ps_entry)));
		if (found && sk) {
			/* Does this change the sorting order? */
			while ((next = TAILQ_NEXT(sk, ps_entry)) &&
			    next->ps_count > sk->ps_count) {
				TAILQ_REMOVE(head, sk, ps_entry);
				TAILQ_INSERT_AFTER(head, next, sk, ps_entry);
			}
#ifdef OPT_DEBUG
			next = TAILQ_NEXT(sk, ps_entry);
			assert(next == NULL || next->ps_count <= sk->ps_count);
#endif /* OPT_DEBUG */
		}
	}
}


/* Compare two rules AF field for skiplist construction */
int
skip_cmp_af(struct pf_rule *a, struct pf_rule *b)
{
	if (a->af != b->af || a->af == 0)
		return (1);
	return (0);
}

/* Compare two rules DIRECTION field for skiplist construction */
int
skip_cmp_dir(struct pf_rule *a, struct pf_rule *b)
{
	if (a->direction == 0 || a->direction != b->direction)
		return (1);
	return (0);
}

/* Compare two rules ON RDOMAIN field for skiplist construction */
int
skip_cmp_rdom(struct pf_rule *a, struct pf_rule *b)
{
	if (a->onrdomain == -1 || a->onrdomain != b->onrdomain)
		return (1);
	return (a->ifnot != b->ifnot);
}

/* Compare two rules DST Address field for skiplist construction */
int
skip_cmp_dst_addr(struct pf_rule *a, struct pf_rule *b)
{
	if (a->dst.neg != b->dst.neg ||
	    a->dst.addr.type != b->dst.addr.type)
		return (1);
	/* XXX if (a->proto != b->proto && a->proto != 0 && b->proto != 0
	 *    && (a->proto == IPPROTO_TCP || a->proto == IPPROTO_UDP ||
	 *    a->proto == IPPROTO_ICMP
	 *	return (1);
	 */
	switch (a->dst.addr.type) {
	case PF_ADDR_ADDRMASK:
		if (memcmp(&a->dst.addr.v.a.addr, &b->dst.addr.v.a.addr,
		    sizeof(a->dst.addr.v.a.addr)) ||
		    memcmp(&a->dst.addr.v.a.mask, &b->dst.addr.v.a.mask,
		    sizeof(a->dst.addr.v.a.mask)) ||
		    (a->dst.addr.v.a.addr.addr32[0] == 0 &&
		    a->dst.addr.v.a.addr.addr32[1] == 0 &&
		    a->dst.addr.v.a.addr.addr32[2] == 0 &&
		    a->dst.addr.v.a.addr.addr32[3] == 0))
			return (1);
		return (0);
	case PF_ADDR_DYNIFTL:
		if (strcmp(a->dst.addr.v.ifname, b->dst.addr.v.ifname) != 0 ||
		    a->dst.addr.iflags != a->dst.addr.iflags ||
		    memcmp(&a->dst.addr.v.a.mask, &b->dst.addr.v.a.mask,
		    sizeof(a->dst.addr.v.a.mask)))
			return (1);
		return (0);
	case PF_ADDR_NOROUTE:
	case PF_ADDR_URPFFAILED:
		return (0);
	case PF_ADDR_TABLE:
		return (strcmp(a->dst.addr.v.tblname, b->dst.addr.v.tblname));
	}
	return (1);
}

/* Compare two rules DST port field for skiplist construction */
int
skip_cmp_dst_port(struct pf_rule *a, struct pf_rule *b)
{
	/* XXX if (a->proto != b->proto && a->proto != 0 && b->proto != 0
	 *    && (a->proto == IPPROTO_TCP || a->proto == IPPROTO_UDP ||
	 *    a->proto == IPPROTO_ICMP
	 *	return (1);
	 */
	if (a->dst.port_op == PF_OP_NONE || a->dst.port_op != b->dst.port_op ||
	    a->dst.port[0] != b->dst.port[0] ||
	    a->dst.port[1] != b->dst.port[1])
		return (1);
	return (0);
}

/* Compare two rules IFP field for skiplist construction */
int
skip_cmp_ifp(struct pf_rule *a, struct pf_rule *b)
{
	if (strcmp(a->ifname, b->ifname) || a->ifname[0] == '\0')
		return (1);
	return (a->ifnot != b->ifnot);
}

/* Compare two rules PROTO field for skiplist construction */
int
skip_cmp_proto(struct pf_rule *a, struct pf_rule *b)
{
	return (a->proto != b->proto || a->proto == 0);
}

/* Compare two rules SRC addr field for skiplist construction */
int
skip_cmp_src_addr(struct pf_rule *a, struct pf_rule *b)
{
	if (a->src.neg != b->src.neg ||
	    a->src.addr.type != b->src.addr.type)
		return (1);
	/* XXX if (a->proto != b->proto && a->proto != 0 && b->proto != 0
	 *    && (a->proto == IPPROTO_TCP || a->proto == IPPROTO_UDP ||
	 *    a->proto == IPPROTO_ICMP
	 *	return (1);
	 */
	switch (a->src.addr.type) {
	case PF_ADDR_ADDRMASK:
		if (memcmp(&a->src.addr.v.a.addr, &b->src.addr.v.a.addr,
		    sizeof(a->src.addr.v.a.addr)) ||
		    memcmp(&a->src.addr.v.a.mask, &b->src.addr.v.a.mask,
		    sizeof(a->src.addr.v.a.mask)) ||
		    (a->src.addr.v.a.addr.addr32[0] == 0 &&
		    a->src.addr.v.a.addr.addr32[1] == 0 &&
		    a->src.addr.v.a.addr.addr32[2] == 0 &&
		    a->src.addr.v.a.addr.addr32[3] == 0))
			return (1);
		return (0);
	case PF_ADDR_DYNIFTL:
		if (strcmp(a->src.addr.v.ifname, b->src.addr.v.ifname) != 0 ||
		    a->src.addr.iflags != a->src.addr.iflags ||
		    memcmp(&a->src.addr.v.a.mask, &b->src.addr.v.a.mask,
		    sizeof(a->src.addr.v.a.mask)))
			return (1);
		return (0);
	case PF_ADDR_NOROUTE:
	case PF_ADDR_URPFFAILED:
		return (0);
	case PF_ADDR_TABLE:
		return (strcmp(a->src.addr.v.tblname, b->src.addr.v.tblname));
	}
	return (1);
}

/* Compare two rules SRC port field for skiplist construction */
int
skip_cmp_src_port(struct pf_rule *a, struct pf_rule *b)
{
	if (a->src.port_op == PF_OP_NONE || a->src.port_op != b->src.port_op ||
	    a->src.port[0] != b->src.port[0] ||
	    a->src.port[1] != b->src.port[1])
		return (1);
	/* XXX if (a->proto != b->proto && a->proto != 0 && b->proto != 0
	 *    && (a->proto == IPPROTO_TCP || a->proto == IPPROTO_UDP ||
	 *    a->proto == IPPROTO_ICMP
	 *	return (1);
	 */
	return (0);
}


void
skip_init(void)
{
	struct {
		char *name;
		int skipnum;
		int (*func)(struct pf_rule *, struct pf_rule *);
	} comps[] = PF_SKIP_COMPARITORS;
	int skipnum, i;

	for (skipnum = 0; skipnum < PF_SKIP_COUNT; skipnum++) {
		for (i = 0; i < sizeof(comps)/sizeof(*comps); i++)
			if (comps[i].skipnum == skipnum) {
				skip_comparitors[skipnum] = comps[i].func;
				skip_comparitors_names[skipnum] = comps[i].name;
			}
	}
	for (skipnum = 0; skipnum < PF_SKIP_COUNT; skipnum++)
		if (skip_comparitors[skipnum] == NULL)
			errx(1, "Need to add skip step comparitor to pfctl?!");
}

/*
 * Add a host/netmask to a table
 */
int
add_opt_table(struct pfctl *pf, struct pf_opt_tbl **tbl, sa_family_t af,
    struct pf_rule_addr *addr, char *ifname)
{
#ifdef OPT_DEBUG
	char buf[128];
#endif /* OPT_DEBUG */
	static int tablenum = 0;
	struct node_host node_host;

	if (*tbl == NULL) {
		if ((*tbl = calloc(1, sizeof(**tbl))) == NULL ||
		    ((*tbl)->pt_buf = calloc(1, sizeof(*(*tbl)->pt_buf))) ==
		    NULL)
			err(1, "calloc");
		(*tbl)->pt_buf->pfrb_type = PFRB_ADDRS;
		SIMPLEQ_INIT(&(*tbl)->pt_nodes);

		/* This is just a temporary table name */
		snprintf((*tbl)->pt_name, sizeof((*tbl)->pt_name), "%s%d",
		    PF_OPT_TABLE_PREFIX, tablenum++);
		DEBUG("creating table <%s>", (*tbl)->pt_name);
	}

	memset(&node_host, 0, sizeof(node_host));
	node_host.af = af;
	node_host.addr = addr->addr;
	node_host.ifname = ifname;
	node_host.weight = addr->weight;

#ifdef OPT_DEBUG
	DEBUG("<%s> adding %s/%d", (*tbl)->pt_name, inet_ntop(af,
	    &node_host.addr.v.a.addr, buf, sizeof(buf)),
	    unmask(&node_host.addr.v.a.mask, af));
#endif /* OPT_DEBUG */

	if (append_addr_host((*tbl)->pt_buf, &node_host, 0, 0)) {
		warn("failed to add host");
		return (1);
	}
	if (pf->opts & PF_OPT_VERBOSE) {
		struct node_tinit *ti;

		if ((ti = calloc(1, sizeof(*ti))) == NULL)
			err(1, "malloc");
		if ((ti->host = malloc(sizeof(*ti->host))) == NULL)
			err(1, "malloc");
		memcpy(ti->host, &node_host, sizeof(*ti->host));
		SIMPLEQ_INSERT_TAIL(&(*tbl)->pt_nodes, ti, entries);
	}

	(*tbl)->pt_rulecount++;
	if ((*tbl)->pt_rulecount == TABLE_THRESHOLD)
		DEBUG("table <%s> now faster than skip steps", (*tbl)->pt_name);

	return (0);
}


/*
 * Do the dirty work of choosing an unused table name and creating it.
 * (be careful with the table name, it might already be used in another anchor)
 */
int
pf_opt_create_table(struct pfctl *pf, struct pf_opt_tbl *tbl)
{
	static int tablenum;
	struct pfr_table *t;

	if (table_buffer.pfrb_type == 0) {
		/* Initialize the list of tables */
		table_buffer.pfrb_type = PFRB_TABLES;
		for (;;) {
			pfr_buf_grow(&table_buffer, table_buffer.pfrb_size);
			table_buffer.pfrb_size = table_buffer.pfrb_msize;
			if (pfr_get_tables(NULL, table_buffer.pfrb_caddr,
			    &table_buffer.pfrb_size, PFR_FLAG_ALLRSETS))
				err(1, "pfr_get_tables");
			if (table_buffer.pfrb_size <= table_buffer.pfrb_msize)
				break;
		}
		table_identifier = arc4random();
	}

	/* XXX would be *really* nice to avoid duplicating identical tables */

	/* Now we have to pick a table name that isn't used */
again:
	DEBUG("translating temporary table <%s> to <%s%x_%d>", tbl->pt_name,
	    PF_OPT_TABLE_PREFIX, table_identifier, tablenum);
	snprintf(tbl->pt_name, sizeof(tbl->pt_name), "%s%x_%d",
	    PF_OPT_TABLE_PREFIX, table_identifier, tablenum);
	PFRB_FOREACH(t, &table_buffer) {
		if (strcasecmp(t->pfrt_name, tbl->pt_name) == 0) {
			/* Collision.  Try again */
			DEBUG("wow, table <%s> in use.  trying again",
			    tbl->pt_name);
			table_identifier = arc4random();
			goto again;
		}
	}
	tablenum++;

	if (pfctl_define_table(tbl->pt_name, PFR_TFLAG_CONST | tbl->pt_flags, 1,
	    pf->astack[0]->name, tbl->pt_buf, pf->astack[0]->ruleset.tticket)) {
		warn("failed to create table %s in %s",
		    tbl->pt_name, pf->astack[0]->name);
		return (1);
	}
	return (0);
}

/*
 * Partition the flat ruleset into a list of distinct superblocks
 */
int
construct_superblocks(struct pfctl *pf, struct pf_opt_queue *opt_queue,
    struct superblocks *superblocks)
{
	struct superblock *block = NULL;
	struct pf_opt_rule *por;
	int i;

	while (!TAILQ_EMPTY(opt_queue)) {
		por = TAILQ_FIRST(opt_queue);
		TAILQ_REMOVE(opt_queue, por, por_entry);
		if (block == NULL || !superblock_inclusive(block, por)) {
			if ((block = calloc(1, sizeof(*block))) == NULL) {
				warn("calloc");
				return (1);
			}
			TAILQ_INIT(&block->sb_rules);
			for (i = 0; i < PF_SKIP_COUNT; i++)
				TAILQ_INIT(&block->sb_skipsteps[i]);
			TAILQ_INSERT_TAIL(superblocks, block, sb_entry);
		}
		TAILQ_INSERT_TAIL(&block->sb_rules, por, por_entry);
	}

	return (0);
}


/*
 * Compare two rule addresses
 */
int
addrs_equal(struct pf_rule_addr *a, struct pf_rule_addr *b)
{
	if (a->neg != b->neg)
		return (0);
	return (memcmp(&a->addr, &b->addr, sizeof(a->addr)) == 0);
}


/*
 * The addresses are not equal, but can we combine them into one table?
 */
int
addrs_combineable(struct pf_rule_addr *a, struct pf_rule_addr *b)
{
	if (a->addr.type != PF_ADDR_ADDRMASK ||
	    b->addr.type != PF_ADDR_ADDRMASK)
		return (0);
	if (a->neg != b->neg || a->port_op != b->port_op ||
	    a->port[0] != b->port[0] || a->port[1] != b->port[1])
		return (0);
	return (1);
}


/*
 * Are we allowed to combine these two rules
 */
int
rules_combineable(struct pf_rule *p1, struct pf_rule *p2)
{
	struct pf_rule a, b;

	comparable_rule(&a, p1, COMBINED);
	comparable_rule(&b, p2, COMBINED);
	return (memcmp(&a, &b, sizeof(a)) == 0);
}


/*
 * Can a rule be included inside a superblock
 */
int
superblock_inclusive(struct superblock *block, struct pf_opt_rule *por)
{
	struct pf_rule a, b;
	int i, j;

	/* First check for hard breaks */
	for (i = 0; i < sizeof(pf_rule_desc)/sizeof(*pf_rule_desc); i++) {
		if (pf_rule_desc[i].prf_type == BARRIER) {
			for (j = 0; j < pf_rule_desc[i].prf_size; j++)
				if (((char *)&por->por_rule)[j +
				    pf_rule_desc[i].prf_offset] != 0)
					return (0);
		}
	}

	/* per-rule src-track is also a hard break */
	if (por->por_rule.rule_flag & PFRULE_RULESRCTRACK)
		return (0);

	/*
	 * Have to handle interface groups separately.  Consider the following
	 * rules:
	 *	block on EXTIFS to any port 22
	 *	pass  on em0 to any port 22
	 * (where EXTIFS is an arbitrary interface group)
	 * The optimizer may decide to re-order the pass rule in front of the
	 * block rule.  But what if EXTIFS includes em0???  Such a reordering
	 * would change the meaning of the ruleset.
	 * We can't just lookup the EXTIFS group and check if em0 is a member
	 * because the user is allowed to add interfaces to a group during
	 * runtime.
	 * Ergo interface groups become a defacto superblock break :-(
	 */
	if (interface_group(por->por_rule.ifname) ||
	    interface_group(TAILQ_FIRST(&block->sb_rules)->por_rule.ifname)) {
		if (strcasecmp(por->por_rule.ifname,
		    TAILQ_FIRST(&block->sb_rules)->por_rule.ifname) != 0)
			return (0);
	}

	comparable_rule(&a, &TAILQ_FIRST(&block->sb_rules)->por_rule, NOMERGE);
	comparable_rule(&b, &por->por_rule, NOMERGE);
	if (memcmp(&a, &b, sizeof(a)) == 0)
		return (1);

#ifdef OPT_DEBUG
	for (i = 0; i < sizeof(por->por_rule); i++) {
		int closest = -1;
		if (((u_int8_t *)&a)[i] != ((u_int8_t *)&b)[i]) {
			for (j = 0; j < sizeof(pf_rule_desc) /
			    sizeof(*pf_rule_desc); j++) {
				if (i >= pf_rule_desc[j].prf_offset &&
				    i < pf_rule_desc[j].prf_offset +
				    pf_rule_desc[j].prf_size) {
					DEBUG("superblock break @ %d due to %s",
					    por->por_rule.nr,
					    pf_rule_desc[j].prf_name);
					return (0);
				}
				if (i > pf_rule_desc[j].prf_offset) {
					if (closest == -1 ||
					    i-pf_rule_desc[j].prf_offset <
					    i-pf_rule_desc[closest].prf_offset)
						closest = j;
				}
			}

			if (closest >= 0)
				DEBUG("superblock break @ %d on %s+%lxh",
				    por->por_rule.nr,
				    pf_rule_desc[closest].prf_name,
				    i - pf_rule_desc[closest].prf_offset -
				    pf_rule_desc[closest].prf_size);
			else
				DEBUG("superblock break @ %d on field @ %d",
				    por->por_rule.nr, i);
			return (0);
		}
	}
#endif /* OPT_DEBUG */

	return (0);
}


/*
 * Figure out if an interface name is an actual interface or actually a
 * group of interfaces.
 */
int
interface_group(const char *ifname)
{
	if (ifname == NULL || !ifname[0])
		return (0);

	/* Real interfaces must end in a number, interface groups do not */
	if (isdigit((unsigned char)ifname[strlen(ifname) - 1]))
		return (0);
	else
		return (1);
}


/*
 * Make a rule that can directly compared by memcmp()
 */
void
comparable_rule(struct pf_rule *dst, const struct pf_rule *src, int type)
{
	int i;
	/*
	 * To simplify the comparison, we just zero out the fields that are
	 * allowed to be different and then do a simple memcmp()
	 */
	memcpy(dst, src, sizeof(*dst));
	for (i = 0; i < sizeof(pf_rule_desc)/sizeof(*pf_rule_desc); i++)
		if (pf_rule_desc[i].prf_type >= type) {
#ifdef OPT_DEBUG
			assert(pf_rule_desc[i].prf_type != NEVER ||
			    *(((char *)dst) + pf_rule_desc[i].prf_offset) == 0);
#endif /* OPT_DEBUG */
			memset(((char *)dst) + pf_rule_desc[i].prf_offset, 0,
			    pf_rule_desc[i].prf_size);
		}
}


/*
 * Remove superset information from two rules so we can directly compare them
 * with memcmp()
 */
void
exclude_supersets(struct pf_rule *super, struct pf_rule *sub)
{
	if (super->ifname[0] == '\0')
		memset(sub->ifname, 0, sizeof(sub->ifname));
	if (super->direction == PF_INOUT)
		sub->direction = PF_INOUT;
	if ((super->proto == 0 || super->proto == sub->proto) &&
	    super->flags == 0 && super->flagset == 0 && (sub->flags ||
	    sub->flagset)) {
		sub->flags = super->flags;
		sub->flagset = super->flagset;
	}
	if (super->proto == 0)
		sub->proto = 0;

	if (super->src.port_op == 0) {
		sub->src.port_op = 0;
		sub->src.port[0] = 0;
		sub->src.port[1] = 0;
	}
	if (super->dst.port_op == 0) {
		sub->dst.port_op = 0;
		sub->dst.port[0] = 0;
		sub->dst.port[1] = 0;
	}

	if (super->src.addr.type == PF_ADDR_ADDRMASK && !super->src.neg &&
	    !sub->src.neg && super->src.addr.v.a.mask.addr32[0] == 0 &&
	    super->src.addr.v.a.mask.addr32[1] == 0 &&
	    super->src.addr.v.a.mask.addr32[2] == 0 &&
	    super->src.addr.v.a.mask.addr32[3] == 0)
		memset(&sub->src.addr, 0, sizeof(sub->src.addr));
	else if (super->src.addr.type == PF_ADDR_ADDRMASK &&
	    sub->src.addr.type == PF_ADDR_ADDRMASK &&
	    super->src.neg == sub->src.neg &&
	    super->af == sub->af &&
	    unmask(&super->src.addr.v.a.mask, super->af) <
	    unmask(&sub->src.addr.v.a.mask, sub->af) &&
	    super->src.addr.v.a.addr.addr32[0] ==
	    (sub->src.addr.v.a.addr.addr32[0] &
	    super->src.addr.v.a.mask.addr32[0]) &&
	    super->src.addr.v.a.addr.addr32[1] ==
	    (sub->src.addr.v.a.addr.addr32[1] &
	    super->src.addr.v.a.mask.addr32[1]) &&
	    super->src.addr.v.a.addr.addr32[2] ==
	    (sub->src.addr.v.a.addr.addr32[2] &
	    super->src.addr.v.a.mask.addr32[2]) &&
	    super->src.addr.v.a.addr.addr32[3] ==
	    (sub->src.addr.v.a.addr.addr32[3] &
	    super->src.addr.v.a.mask.addr32[3])) {
		/* sub->src.addr is a subset of super->src.addr/mask */
		memcpy(&sub->src.addr, &super->src.addr, sizeof(sub->src.addr));
	}

	if (super->dst.addr.type == PF_ADDR_ADDRMASK && !super->dst.neg &&
	    !sub->dst.neg && super->dst.addr.v.a.mask.addr32[0] == 0 &&
	    super->dst.addr.v.a.mask.addr32[1] == 0 &&
	    super->dst.addr.v.a.mask.addr32[2] == 0 &&
	    super->dst.addr.v.a.mask.addr32[3] == 0)
		memset(&sub->dst.addr, 0, sizeof(sub->dst.addr));
	else if (super->dst.addr.type == PF_ADDR_ADDRMASK &&
	    sub->dst.addr.type == PF_ADDR_ADDRMASK &&
	    super->dst.neg == sub->dst.neg &&
	    super->af == sub->af &&
	    unmask(&super->dst.addr.v.a.mask, super->af) <
	    unmask(&sub->dst.addr.v.a.mask, sub->af) &&
	    super->dst.addr.v.a.addr.addr32[0] ==
	    (sub->dst.addr.v.a.addr.addr32[0] &
	    super->dst.addr.v.a.mask.addr32[0]) &&
	    super->dst.addr.v.a.addr.addr32[1] ==
	    (sub->dst.addr.v.a.addr.addr32[1] &
	    super->dst.addr.v.a.mask.addr32[1]) &&
	    super->dst.addr.v.a.addr.addr32[2] ==
	    (sub->dst.addr.v.a.addr.addr32[2] &
	    super->dst.addr.v.a.mask.addr32[2]) &&
	    super->dst.addr.v.a.addr.addr32[3] ==
	    (sub->dst.addr.v.a.addr.addr32[3] &
	    super->dst.addr.v.a.mask.addr32[3])) {
		/* sub->dst.addr is a subset of super->dst.addr/mask */
		memcpy(&sub->dst.addr, &super->dst.addr, sizeof(sub->dst.addr));
	}

	if (super->af == 0)
		sub->af = 0;
}


void
superblock_free(struct pfctl *pf, struct superblock *block)
{
	struct pf_opt_rule *por;
	while ((por = TAILQ_FIRST(&block->sb_rules))) {
		TAILQ_REMOVE(&block->sb_rules, por, por_entry);
		if (por->por_src_tbl) {
			if (por->por_src_tbl->pt_buf) {
				pfr_buf_clear(por->por_src_tbl->pt_buf);
				free(por->por_src_tbl->pt_buf);
			}
			free(por->por_src_tbl);
		}
		if (por->por_dst_tbl) {
			if (por->por_dst_tbl->pt_buf) {
				pfr_buf_clear(por->por_dst_tbl->pt_buf);
				free(por->por_dst_tbl->pt_buf);
			}
			free(por->por_dst_tbl);
		}
		free(por);
	}
	if (block->sb_profiled_block)
		superblock_free(pf, block->sb_profiled_block);
	free(block);
}