summaryrefslogtreecommitdiffstats
path: root/src/lib.h
blob: a5d436256c34e052448132a0614bb81585c1d8c1 (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
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
#ifndef _LIB_H
#define _LIB_H

#include <libmnl/libmnl.h>
#include <stdlib.h>
#include "linux_list.h"

#define ARRAY_MAX	4096

struct array {
	void *data[ARRAY_MAX];
	int num;
};

struct array_u8 {
	uint8_t data[ARRAY_MAX];
	int num;
};

struct array_u32 {
	uint32_t data[ARRAY_MAX];
	int num;
};

enum test_batch_type {
	ADD_TABLE	= 0,
	SET_TABLE,
	DEL_TABLE,
	ADD_BASECHAIN,
	DEL_BASECHAIN,
	ADD_CHAIN,
	DEL_CHAIN,
	ADD_SET,
	SET_SET,
	FLUSH_SET,
	ADD_SETELEM,
	DEL_SETELEM,
	DEL_SET,
	ADD_FLOWTABLE,
	DEL_FLOWTABLE,
	ADD_RULE,
	DEL_RULE,
	ADD_OBJECT,
	DEL_OBJECT,
};

struct test_batch_cmd {
	struct list_head list;
	enum test_batch_type type;
	int lineno;
	void *obj;
};

struct test_batch {
	struct list_head cmd;
	uint32_t lineno;
	char buf[1 << 21];
	struct mnl_socket *nl;
	struct mnl_nlmsg_batch *batch;
	struct {
		int family;
		const char *table;
		struct rule *rule;
		struct list_head *expr_list;	/* for inner */
		struct set *set;
	} ctx;
};

void setup_batch(struct test_batch *batch);

struct table {
	const char	*name;
	uint32_t	*nfproto;
	uint32_t	*flags;
	uint64_t	*handle;
	const char	*userdata;

	/* storage */
	uint32_t	_nfproto;
	uint32_t	_flags;
	uint64_t	_handle;
};

static inline struct table *table_init(void)
{
	struct table *table = calloc(1, sizeof(*table));

	table->nfproto = &table->_nfproto;
	table->flags = &table->_flags;
	table->handle = &table->_handle;

	return table;
}

void free_table(struct table *table);

void set_table(struct test_batch *batch, struct table *table);
void add_table(struct test_batch *batch, struct table *table);
void del_table(struct test_batch *batch, struct table *table);

struct chain {
	const char *table;
	const char *name;
	const char *type;
	const char *dev;
	struct array *dev_array;
	const char *userdata;

	uint32_t *family;
	uint32_t *flags;
	uint32_t *chain_id;
	uint32_t *hooknum;
	uint64_t *handle;
	uint32_t *policy;
	uint64_t *bytes;
	uint64_t *pkts;
	uint32_t *prio;

	/* storage */
	uint32_t _family;
	uint32_t _flags;
	uint32_t _chain_id;
	uint32_t _hooknum;
	uint64_t _handle;
	uint32_t _policy;
	uint64_t _bytes;
	uint64_t _pkts;
	uint32_t _prio;
};

static inline struct chain *chain_init(void)
{
	struct chain *c = calloc(1, sizeof(*c));

	c->family = &c->_family;
	c->flags = &c->_flags;
	c->chain_id = &c->_chain_id;
	c->hooknum = &c->_hooknum;
	c->handle = &c->_handle;
	c->policy = &c->_policy;
	c->bytes = &c->_bytes;
	c->pkts = &c->_pkts;
	c->prio = &c->_prio;

	return c;
}

void free_chain(struct chain *chain);

void add_chain(struct test_batch *batch, struct chain *chain);
void del_chain(struct test_batch *batch, struct chain *chain);
void add_basechain(struct test_batch *batch, struct chain *chain);
void del_basechain(struct test_batch *batch, struct chain *chain);

struct set {
	const char *table;
	const char *name;
	struct list_head expr_list;

	uint32_t *family;
	uint32_t *flags;
	uint32_t *key_len;
	uint32_t *key_type;
	uint32_t *data_len;
	uint32_t *data_type;
	uint32_t *policy;
	struct array_u8 *field_array;
	uint32_t *size;
	uint32_t *set_id;
	uint64_t *timeout;
	uint32_t *gc_interval;
	const char *userdata;
	uint32_t *obj_type;
	uint64_t *handle;

	/* storage */
	uint32_t _family;
	uint32_t _flags;
	uint32_t _key_len;
	uint32_t _key_type;
	uint32_t _data_len;
	uint32_t _data_type;
	uint32_t _policy;
	uint32_t _size;
	uint32_t _set_id;
	uint64_t _timeout;
	uint32_t _gc_interval;
	uint32_t _obj_type;
	uint64_t _handle;
};

static inline struct set *set_init(void)
{
	struct set *set = calloc(1, sizeof(*set));

	INIT_LIST_HEAD(&set->expr_list);

	set->family = &set->_family;
	set->flags = &set->_flags;
	set->key_len = &set->_key_len;
	set->key_type = &set->_key_type;
	set->data_len = &set->_data_len;
	set->data_type = &set->_data_type;
	set->policy = &set->_policy;
	set->size = &set->_size;
	set->set_id = &set->_set_id;
	set->timeout = &set->_timeout;
	set->gc_interval = &set->_gc_interval;
	set->obj_type = &set->_obj_type;
	set->handle = &set->_handle;

	return set;
}

void add_set(struct test_batch *batch, struct set *set);
void del_set(struct test_batch *batch, struct set *set);
void flush_set(struct test_batch *batch, struct set *set);
void set_set(struct test_batch *batch, struct set *set);
void free_set(struct set *set);

struct elem {
	const char *table;
	const char *set;
	uint32_t set_id;

	const char *chain;
	const char *objname;
	uint8_t *userdata;

	struct list_head expr_list;

	uint32_t *family;
	uint8_t *key;
	int key_len;
	uint8_t *key_end;
	int key_end_len;
	uint8_t *data;
	int data_len;
	uint32_t *flags;
	uint32_t *verdict;
	uint64_t *timeout;
	uint64_t *expiration;

	/* storage */
	uint32_t _family;
	uint8_t _key[16];
	uint8_t _key_end[16];
	uint8_t _data[64];
	uint32_t _flags;
	uint32_t _verdict;
	uint64_t _timeout;
	uint64_t _expiration;
};

static inline struct elem *elem_init(void)
{
	struct elem *elem = calloc(1, sizeof(*elem));

	INIT_LIST_HEAD(&elem->expr_list);

	elem->family = &elem->_family;
	elem->key = elem->_key;
	elem->key_end = elem->_key_end;
	elem->data = elem->_data;
	elem->flags = &elem->_flags;
	elem->verdict = &elem->_verdict;
	elem->timeout = &elem->_timeout;
	elem->expiration = &elem->_expiration;

	return elem;
}

void free_elem(struct elem *elem);

void add_elem(struct test_batch *batch, struct elem *elem);
void del_elem(struct test_batch *batch, struct elem *elem);

struct flowtable {
	const char *table;
	const char *name;
	struct array *dev_array;

	uint32_t *family;
	uint32_t *hooknum;
	uint32_t *prio;
	uint64_t *handle;

	/* storage */
	uint32_t _family;
	uint32_t _hooknum;
	uint32_t _prio;
	uint64_t _handle;
};

static inline struct flowtable  *flowtable_init(void)
{
	struct flowtable *f = calloc(1, sizeof(*f));

	f->family = &f->_family;
	f->hooknum = &f->_hooknum;
	f->handle = &f->_handle;
	f->prio = &f->_prio;

	return f;
}

void add_flowtable(struct test_batch *batch, struct flowtable *flowtable);
void del_flowtable(struct test_batch *batch, struct flowtable *flowtable);
void free_flowtable(struct flowtable *flowtable);

struct rule {
	const char *table;
	const char *chain;
	uint32_t *family;
	uint32_t *rule_id;
	uint32_t *pos_id;
	uint64_t *handle;
	uint64_t *pos;
	const char *userdata;

	/* set up when parsing match/target. */
	struct {
		uint32_t *l4proto;
		uint32_t *flags;
	} compat;

	struct list_head expr_list;

	/* storage */
	uint32_t _family;
	uint32_t _rule_id;
	uint32_t _pos_id;
	uint64_t _handle;
	uint64_t _pos;
};

static inline struct rule *rule_init(void)
{
	struct rule *rule = calloc(1, sizeof(*rule));

	rule->family = &rule->_family;
	rule->rule_id = &rule->_rule_id;
	rule->pos_id = &rule->_pos_id;
	rule->handle = &rule->_handle;
	rule->pos = &rule->_pos;

	INIT_LIST_HEAD(&rule->expr_list);

	return rule;
}

void free_rule(struct rule *rule);

void add_rule(struct test_batch *batch, struct rule *rule);
void del_rule(struct test_batch *batch, struct rule *rule);

enum expr_type {
	INVALID = 0,
	BITWISE,
	BYTEORDER,
	CMP,
	COUNTER,
	CONNLIMIT,
	CT,
	DUP,
	DYNSET,
	EXTHDR,
	FIB,
	FWD,
	HASH,
	INNER,
	IMMEDIATE,
	LAST,
	LIMIT,
	LOG,
	LOOKUP,
	MASQ,
	MATCH,
	META,
	NAT,
	NUMGEN,
	OBJREF,
	OSF,
	PAYLOAD,
	QUEUE,
	QUOTA,
	RANGE,
	REDIR,
	REJECT,
	RT,
	SOCKET,
	SYNPROXY,
	TARGET,
	TPROXY,
	TUNNEL,
	XFRM,
};

struct expr {
	struct list_head	list;
	enum expr_type		type;
};

int add_expr(struct test_batch *batch, struct expr *expr);

struct bitwise {
	struct expr expr;

	uint32_t *sreg;
	uint32_t *dreg;
	uint32_t *op;
	uint32_t *len;
	uint8_t *mask;
	int mask_len;
	uint8_t *xor;
	int xor_len;
	uint8_t *data;
	int data_len;

	uint32_t _sreg;
	uint32_t _dreg;
	uint32_t _op;
	uint32_t _len;
	uint8_t _mask[16];
	uint8_t _xor[16];
	uint8_t _data[16];
};

static inline struct bitwise *bitwise_init(void)
{
	struct bitwise *bitwise = calloc(1, sizeof(*bitwise));

	bitwise->expr.type = BITWISE;

	bitwise->sreg = &bitwise->_sreg;
	bitwise->dreg = &bitwise->_dreg;
	bitwise->op = &bitwise->_op;
	bitwise->len = &bitwise->_len;
	bitwise->mask = bitwise->_mask;
	bitwise->xor = bitwise->_xor;
	bitwise->data = bitwise->_data;

	return bitwise;
}

static inline void free_bitwise(struct bitwise *bitwise)
{
	free(bitwise);
}

struct byteorder {
	struct expr expr;

	uint32_t *sreg;
	uint32_t *dreg;
	uint32_t *op;
	uint32_t *len;
	uint32_t *size;

	uint32_t _sreg;
	uint32_t _dreg;
	uint32_t _op;
	uint32_t _len;
	uint32_t _size;
};

static inline struct byteorder *byteorder_init(void)
{
	struct byteorder *byteorder = calloc(1, sizeof(*byteorder));

	byteorder->expr.type = BYTEORDER;

	byteorder->sreg = &byteorder->_sreg;
	byteorder->dreg = &byteorder->_dreg;
	byteorder->op = &byteorder->_op;
	byteorder->len = &byteorder->_len;
	byteorder->size = &byteorder->_size;

	return byteorder;
}

static inline void free_byteorder(struct byteorder *byteorder)
{
	free(byteorder);
}

struct cmp {
	struct expr expr;

	uint32_t *sreg;
	uint32_t *op;
	uint8_t *data;
	int data_len;

	uint32_t _sreg;
	uint32_t _op;
	uint8_t _data[16];
};

static inline struct cmp *cmp_init(void)
{
	struct cmp *cmp = calloc(1, sizeof(*cmp));

	cmp->expr.type = CMP;

	cmp->sreg = &cmp->_sreg;
	cmp->op = &cmp->_op;
	cmp->data = cmp->_data;

	return cmp;
}

static inline void free_cmp(struct cmp *cmp)
{
	free(cmp);
}

struct connlimit {
	struct expr expr;

	uint32_t *count;
	uint32_t *flags;

	uint32_t _count;
	uint32_t _flags;
};

static inline struct connlimit *connlimit_init(void)
{
	struct connlimit *connlimit = calloc(1, sizeof(*connlimit));

	connlimit->expr.type = CONNLIMIT;

	connlimit->count = &connlimit->_count;
	connlimit->flags = &connlimit->_flags;

	return connlimit;
}

static inline void free_connlimit(struct connlimit *connlimit)
{
	free(connlimit);
}

struct counter {
	struct expr expr;

	uint64_t *pkts;
	uint64_t *bytes;

	/* storage */
	uint64_t _pkts;
	uint64_t _bytes;
};

static inline struct counter *counter_init(void)
{
	struct counter *counter = calloc(1, sizeof(*counter));

	counter->expr.type = COUNTER;

	counter->pkts = &counter->_pkts;
	counter->bytes = &counter->_bytes;

	return counter;
}

static inline void free_counter(struct counter *counter)
{
	free(counter);
}

struct ct {
	struct expr expr;

	uint32_t *sreg;
	uint32_t *dreg;
	uint32_t *key;
	uint8_t *dir;

	uint32_t _sreg;
	uint32_t _dreg;
	uint32_t _key;
	uint8_t _dir;
};

static inline struct ct *ct_init(void)
{
	struct ct *ct = calloc(1, sizeof(*ct));

	ct->expr.type = CT;

	ct->sreg = &ct->_sreg;
	ct->dreg = &ct->_dreg;
	ct->key = &ct->_key;
	ct->dir = &ct->_dir;

	return ct;
}

static inline void free_ct(struct ct *ct)
{
	free(ct);
}

struct dup {
	struct expr expr;

	uint32_t *sreg_addr;
	uint32_t *sreg_dev;

	uint32_t _sreg_addr;
	uint32_t _sreg_dev;
};

static inline struct dup *dup_init(void)
{
	struct dup *dup = calloc(1, sizeof(*dup));

	dup->expr.type = DUP;

	dup->sreg_addr = &dup->_sreg_addr;
	dup->sreg_dev = &dup->_sreg_dev;

	return dup;
}

static inline void free_dup(struct dup *dup)
{
	free(dup);
}

struct dynset {
	struct expr expr;

	const char *set;
	struct list_head expr_list;

	uint32_t *set_id;
	uint32_t *op;
	uint32_t *sreg_key;
	uint32_t *sreg_data;
	uint64_t *timeout;
	uint32_t *flags;
	/* exprs */

	/* storage */
	uint32_t _set_id;
	uint32_t _op;
	uint32_t _sreg_key;
	uint32_t _sreg_data;
	uint64_t _timeout;
	uint32_t _flags;
};

static inline struct dynset *dynset_init(void)
{
	struct dynset *dynset = calloc(1, sizeof(*dynset));

	dynset->expr.type = DYNSET;

	INIT_LIST_HEAD(&dynset->expr_list);

	dynset->set_id = &dynset->_set_id;
	dynset->op = &dynset->_op;
	dynset->sreg_key= &dynset->_sreg_key;
	dynset->sreg_data = &dynset->_sreg_data;
	dynset->timeout = &dynset->_timeout;
	dynset->flags = &dynset->_flags;

	return dynset;
}

static inline void free_dynset(struct dynset *dynset)
{
	free((void *)dynset->set);
	free(dynset);
}

struct exthdr {
	struct expr expr;

	uint32_t *sreg;
	uint32_t *dreg;
	uint32_t *offset;
	uint32_t *len;
	uint32_t *type;
	uint32_t *op;
	uint32_t *flags;

	uint32_t _sreg;
	uint32_t _dreg;
	uint32_t _offset;
	uint32_t _len;
	uint32_t _type;
	uint32_t _op;
	uint32_t _flags;
};

static inline struct exthdr *exthdr_init(void)
{
	struct exthdr *exthdr = calloc(1, sizeof(*exthdr));

	exthdr->expr.type = EXTHDR;

	exthdr->sreg = &exthdr->_sreg;
	exthdr->dreg = &exthdr->_dreg;
	exthdr->offset = &exthdr->_offset;
	exthdr->len = &exthdr->_len;
	exthdr->type = &exthdr->_type;
	exthdr->op = &exthdr->_op;
	exthdr->flags = &exthdr->_flags;

	return exthdr;
}

static inline void free_exthdr(struct exthdr *exthdr)
{
	free(exthdr);
}

struct fib {
	struct expr expr;

	uint32_t *flags;
	uint32_t *result;
	uint32_t *dreg;

	/* storage */
	uint32_t _flags;
	uint32_t _result;
	uint32_t _dreg;
};

static inline struct fib *fib_init(void)
{
	struct fib *fib = calloc(1, sizeof(*fib));

	fib->expr.type = FIB;

	fib->flags = &fib->_flags;
	fib->result = &fib->_result;
	fib->dreg = &fib->_dreg;

	return fib;
}

static inline void free_fib(struct fib *fib)
{
	free(fib);
}

struct fwd {
	struct expr expr;

	uint32_t *sreg_addr;
	uint32_t *sreg_dev;
	uint32_t *nfproto;

	uint32_t _sreg_addr;
	uint32_t _sreg_dev;
	uint32_t _nfproto;
};

static inline struct fwd *fwd_init(void)
{
	struct fwd *fwd = calloc(1, sizeof(*fwd));

	fwd->expr.type = FWD;

	fwd->sreg_addr = &fwd->_sreg_addr;
	fwd->sreg_dev = &fwd->_sreg_dev;
	fwd->nfproto = &fwd->_nfproto;

	return fwd;
}

static inline void free_fwd(struct fwd *fwd)
{
	free(fwd);
}

struct hash {
	struct expr expr;

	uint32_t *type;
	uint32_t *sreg;
	uint32_t *dreg;
	uint32_t *len;
	uint32_t *modulus;
	uint32_t *seed;
	uint32_t *offset;

	uint32_t _type;
	uint32_t _sreg;
	uint32_t _dreg;
	uint32_t _len;
	uint32_t _modulus;
	uint32_t _seed;
	uint32_t _offset;
};

static inline struct hash *hash_init(void)
{
	struct hash *hash = calloc(1, sizeof(*hash));

	hash->expr.type = HASH;

	hash->type = &hash->_type;
	hash->sreg = &hash->_sreg;
	hash->dreg = &hash->_dreg;
	hash->len = &hash->_len;
	hash->modulus = &hash->_modulus;
	hash->seed = &hash->_seed;
	hash->offset = &hash->_offset;

	return hash;
}

static inline void free_hash(struct hash *hash)
{
	free(hash);
}

struct inner {
	struct expr expr;

	uint32_t _num;
	uint32_t _type;
	uint32_t _flags;
	uint32_t _hdrsize;

	uint32_t *num;
	uint32_t *type;
	uint32_t *flags;
	uint32_t *hdrsize;

	struct list_head expr_list;
};

static inline struct inner *inner_init(void)
{
	struct inner *inner = calloc(1, sizeof(*inner));

	inner->expr.type = INNER;

	inner->num = &inner->_num;
	inner->type = &inner->_type;
	inner->flags = &inner->_flags;
	inner->hdrsize = &inner->_hdrsize;

	INIT_LIST_HEAD(&inner->expr_list);

	return inner;
}

void free_expr_list(struct list_head *expr_list);

static inline void free_inner(struct inner *inner)
{
	free_expr_list(&inner->expr_list);
	free(inner);
}

struct immediate {
	struct expr expr;

	const char *chain;

	uint32_t *dreg;
	uint32_t *chain_id;
	uint32_t *verdict;
	uint8_t *data;
	int data_len;

	uint32_t _dreg;
	uint32_t _chain_id;
	uint32_t _verdict;
	uint8_t _data[16];
};

static inline struct immediate *immediate_init(void)
{
	struct immediate *imm = calloc(1, sizeof(*imm));

	imm->expr.type = IMMEDIATE;

	imm->dreg = &imm->_dreg;
	imm->chain_id = &imm->_chain_id;
	imm->verdict = &imm->_verdict;
	imm->data = imm->_data;

	return imm;
}

static inline void free_immediate(struct immediate *immediate)
{
	free((void *)immediate->chain);
	free(immediate);
}

struct last {
	struct expr expr;

	uint64_t *msecs;
	uint32_t *set;

	/* storage */
	uint64_t _msecs;
	uint32_t _set;
};

static inline struct last *last_init(void)
{
	struct last *last = calloc(1, sizeof(*last));

	last->expr.type = LAST;

	last->msecs = &last->_msecs;
	last->set = &last->_set;

	return last;
}

static inline void free_last(struct last *last)
{
	free(last);
}

struct limit {
	struct expr expr;

	uint64_t *rate;
	uint64_t *unit;
	uint32_t *burst;
	uint32_t *type;
	uint32_t *flags;

	/* storage */
	uint64_t _rate;
	uint64_t _unit;
	uint32_t _burst;
	uint32_t _type;
	uint32_t _flags;
};

static inline struct limit *limit_init(void)
{
	struct limit *limit = calloc(1, sizeof(*limit));

	limit->expr.type = LIMIT;

	limit->rate = &limit->_rate;
	limit->unit = &limit->_unit;
	limit->burst = &limit->_burst;
	limit->type = &limit->_type;
	limit->flags = &limit->_flags;

	return limit;
}

static inline void free_limit(struct limit *limit)
{
	free(limit);
}

struct log {
	struct expr expr;

        const char *prefix;

        uint32_t *snaplen;
        uint16_t *group;
        uint16_t *qthreshold;
        uint32_t *level;
        uint32_t *flags;

	/* storage */
        uint32_t _snaplen;
        uint16_t _group;
        uint16_t _qthreshold;
        uint32_t _level;
        uint32_t _flags;
};

static inline struct log *log_init(void)
{
	struct log *log = calloc(1, sizeof(*log));

	log->expr.type = LOG;

	log->snaplen = &log->_snaplen;
	log->group = &log->_group;
	log->qthreshold = &log->_qthreshold;
	log->level = &log->_level;
	log->flags = &log->_flags;

	return log;
}

static inline void free_log(struct log *log)
{
	free((void *)log->prefix);
	free(log);
}

struct lookup {
	struct expr expr;

	const char *set;
	uint32_t *set_id;
	uint32_t *sreg;
	uint32_t *dreg;
	uint32_t *flags;

	/* storage */
	uint32_t _set_id;
	uint32_t _sreg;
	uint32_t _dreg;
};

static inline struct lookup *lookup_init(void)
{
	struct lookup *lookup = calloc(1, sizeof(*lookup));

	lookup->expr.type = LOOKUP;

	lookup->set_id = &lookup->_set_id;
	lookup->sreg = &lookup->_sreg;
	lookup->dreg = &lookup->_dreg;
	lookup->flags = NULL; // TODO

	return lookup;
}

static inline void free_lookup(struct lookup *lookup)
{
	free((void *)lookup->set);
	free(lookup);
}

struct masq {
	struct expr expr;

	uint32_t *sreg_proto_min;
	uint32_t *sreg_proto_max;
	uint32_t *flags;

	uint32_t _sreg_proto_min;
	uint32_t _sreg_proto_max;
	uint32_t _flags;
};

static inline struct masq *masq_init(void)
{
	struct masq *masq = calloc(1, sizeof(*masq));

	masq->expr.type = MASQ;

	masq->sreg_proto_min = &masq->_sreg_proto_min;
	masq->sreg_proto_max = &masq->_sreg_proto_max;
	masq->flags = &masq->_flags;

	return masq;
}

static inline void free_masq(struct masq *masq)
{
	free(masq);
}

struct match {
	struct expr expr;

	const char *name;

	uint32_t *rev;
	uint8_t *data;
	int data_len;
	uint32_t *l4proto;
	uint32_t *flags;

	/* storage */
	uint32_t _rev;
	uint8_t _data[65536];
	uint32_t _l4proto;
	uint32_t _flags;
};

static inline struct match *match_init(void)
{
	struct match *match = calloc(1, sizeof(*match));

	match->expr.type = MATCH;

	match->rev = &match->_rev;
	match->data = match->_data;
	match->l4proto = &match->_l4proto;
	match->flags = &match->_flags;

	return match;
}

static inline void free_match(struct match *match)
{
	free((void *)match->name);
	free(match);
}

struct meta {
	struct expr expr;

	uint32_t *sreg;
	uint32_t *dreg;
	uint32_t *key;

	uint32_t _sreg;
	uint32_t _dreg;
	uint32_t _key;
};

static inline struct meta *meta_init(void)
{
	struct meta *meta = calloc(1, sizeof(*meta));

	meta->expr.type = META;

	meta->sreg = &meta->_sreg;
	meta->dreg = &meta->_dreg;
	meta->key = &meta->_key;

	return meta;
}

static inline void free_meta(struct meta *meta)
{
	free(meta);
}

struct nat {
	struct expr expr;

	uint32_t *sreg_addr_min;
	uint32_t *sreg_addr_max;
	uint32_t *sreg_proto_min;
	uint32_t *sreg_proto_max;
	uint32_t *nfproto;
	uint32_t *type;
	uint32_t *flags;

	uint32_t _sreg_addr_min;
	uint32_t _sreg_addr_max;
	uint32_t _sreg_proto_min;
	uint32_t _sreg_proto_max;
	uint32_t _nfproto;
	uint32_t _type;
	uint32_t _flags;
};

static inline struct nat *nat_init(void)
{
	struct nat *nat = calloc(1, sizeof(*nat));

	nat->expr.type = NAT;

	nat->sreg_addr_min = &nat->_sreg_addr_min;
	nat->sreg_addr_max = &nat->_sreg_addr_max;
	nat->sreg_proto_min = &nat->_sreg_proto_min;
	nat->sreg_proto_max = &nat->_sreg_proto_max;
	nat->nfproto = &nat->_nfproto;
	nat->type = &nat->_type;
	nat->flags = &nat->_flags;

	return nat;
}

static inline void free_nat(struct nat *nat)
{
	free(nat);
}

struct numgen {
	struct expr expr;

	uint32_t *dreg;
	uint32_t *modulus;
	uint32_t *type;
	uint32_t *offset;

	uint32_t _dreg;
	uint32_t _modulus;
	uint32_t _type;
	uint32_t _offset;
};

static inline struct numgen *numgen_init(void)
{
	struct numgen *numgen = calloc(1, sizeof(*numgen));

	numgen->expr.type = NUMGEN;

	numgen->dreg = &numgen->_dreg;
	numgen->modulus = &numgen->_modulus;
	numgen->type = &numgen->_type;
	numgen->offset = &numgen->_offset;

	return numgen;
}

static inline void free_numgen(struct numgen *numgen)
{
	free(numgen);
}

struct objref {
	struct expr expr;

	const char *set_name;
	const char *name;

	uint32_t *type;
	uint32_t *sreg;
	uint32_t *set_id;

	uint32_t _type;
	uint32_t _sreg;
	uint32_t _set_id;
};

static inline struct objref *objref_init(void)
{
	struct objref *objref = calloc(1, sizeof(*objref));

	objref->expr.type = OBJREF;

	objref->type = &objref->_type;
	objref->sreg = &objref->_sreg;
	objref->set_id = &objref->_set_id;

	return objref;
}

static inline void free_objref(struct objref *objref)
{
	free((void *)objref->set_name);
	free((void *)objref->name);
	free(objref);
}

struct osf {
	struct expr expr;

	uint32_t *dreg;
	uint32_t *ttl;
	uint32_t *flags;

	uint32_t _dreg;
	uint32_t _ttl;
	uint32_t _flags;
};

static inline struct osf *osf_init(void)
{
	struct osf *osf = calloc(1, sizeof(*osf));

	osf->expr.type = OSF;

	osf->dreg = &osf->_dreg;
	osf->ttl = &osf->_ttl;
	osf->flags = &osf->_flags;

	return osf;
}

static inline void free_osf(struct osf *osf)
{
	free(osf);
}

struct payload {
	struct expr expr;

	uint32_t *sreg;
	uint32_t *dreg;
	uint32_t *offset;
	uint32_t *base;
	uint32_t *len;
	uint32_t *csum_type;
	uint32_t *csum_offset;
	uint32_t *csum_flags;

	/* storage */
	uint32_t _sreg;
	uint32_t _dreg;
	uint32_t _offset;
	uint32_t _base;
	uint32_t _len;
	uint32_t _csum_type;
	uint32_t _csum_offset;
	uint32_t _csum_flags;
};

static inline struct payload *payload_init(void)
{
	struct payload *payload = calloc(1, sizeof(*payload));

	payload->expr.type = PAYLOAD;

	payload->sreg = &payload->_sreg;
	payload->dreg = &payload->_dreg;
	payload->offset = &payload->_offset;
	payload->base = &payload->_base;
	payload->len = &payload->_len;
	payload->csum_type = &payload->_csum_type;
	payload->csum_offset = &payload->_csum_offset;
	payload->csum_flags = &payload->_csum_flags;

	return payload;
}

static inline void free_payload(struct payload *payload)
{
	free(payload);
}

struct queue {
	struct expr expr;

	uint32_t *sreg_qnum;
	uint16_t *queue_num;
	uint16_t *queue_total;
	uint16_t *flags;

	uint32_t _sreg_qnum;
	uint16_t _queue_num;
	uint16_t _queue_total;
	uint16_t _flags;
};

static inline struct queue *queue_init(void)
{
	struct queue *queue = calloc(1, sizeof(*queue));

	queue->expr.type = QUEUE;

	queue->sreg_qnum = &queue->_sreg_qnum;
	queue->queue_num = &queue->_queue_num;
	queue->queue_total = &queue->_queue_total;
	queue->flags = &queue->_flags;

	return queue;
}

static inline void free_queue(struct queue *queue)
{
	free(queue);
}

struct quota {
	struct expr expr;

	uint64_t *bytes;
	uint64_t *consumed;
	uint32_t *flags;

	/* storage */
	uint64_t _bytes;
	uint64_t _consumed;
	uint32_t _flags;
};

static inline struct quota *quota_init(void)
{
	struct quota *quota = calloc(1, sizeof(*quota));

	quota->expr.type = QUOTA;

	quota->bytes = &quota->_bytes;
	quota->consumed = &quota->_consumed;
	quota->flags = &quota->_flags;

	return quota;
}

static inline void free_quota(struct quota *quota)
{
	free(quota);
}

struct range {
	struct expr expr;

	uint32_t *sreg;
	uint32_t *op;
	uint8_t *data_from;
	int data_from_len;
	uint8_t *data_to;
	int data_to_len;

	uint32_t _sreg;
	uint32_t _op;
	uint8_t _data_from[16];
	uint8_t _data_to[16];
};

static inline struct range *range_init(void)
{
	struct range *range = calloc(1, sizeof(*range));

	range->expr.type = RANGE;

	range->sreg = &range->_sreg;
	range->op = &range->_op;
	range->data_from = range->_data_from;
	range->data_to = range->_data_to;

	return range;
}

static inline void free_range(struct range *range)
{
	free(range);
}

struct redir {
	struct expr expr;

	uint32_t *sreg_proto_min;
	uint32_t *sreg_proto_max;
	uint32_t *flags;

	uint32_t _sreg_proto_min;
	uint32_t _sreg_proto_max;
	uint32_t _flags;
};

static inline struct redir *redir_init(void)
{
	struct redir *redir = calloc(1, sizeof(*redir));

	redir->expr.type = REDIR;

	redir->sreg_proto_min = &redir->_sreg_proto_min;
	redir->sreg_proto_max = &redir->_sreg_proto_max;
	redir->flags = &redir->_flags;

	return redir;
}

static inline void free_redir(struct redir *redir)
{
	free(redir);
}

struct reject {
	struct expr expr;

	uint32_t *type;
	uint8_t *icmp_code;

	/* storage */
	uint32_t _type;
	uint8_t _icmp_code;
};

static inline struct reject *reject_init(void)
{
	struct reject *reject = calloc(1, sizeof(*reject));

	reject->expr.type = REJECT;

	reject->type = &reject->_type;
	reject->icmp_code = &reject->_icmp_code;

	return reject;
}

static inline void free_reject(struct reject *reject)
{
	free(reject);
}

struct rt {
	struct expr expr;

	uint32_t *dreg;
	uint32_t *key;

	uint32_t _dreg;
	uint32_t _key;
};

static inline struct rt *rt_init(void)
{
	struct rt *rt = calloc(1, sizeof(*rt));

	rt->expr.type = RT;

	rt->dreg = &rt->_dreg;
	rt->key = &rt->_key;

	return rt;
}

static inline void free_rt(struct rt *rt)
{
	free(rt);
}

struct socket {
	struct expr expr;

	uint32_t *dreg;
	uint32_t *key;
	uint32_t *level;

	uint32_t _dreg;
	uint32_t _key;
	uint32_t _level;
};

static inline struct socket *socket_init(void)
{
	struct socket *socket = calloc(1, sizeof(*socket));

	socket->expr.type = SOCKET;

	socket->dreg = &socket->_dreg;
	socket->key = &socket->_key;
	socket->level = &socket->_level;

	return socket;
}

static inline void free_socket(struct socket *socket)
{
	free(socket);
}

struct synproxy {
	struct expr expr;

	uint16_t *mss;
	uint8_t *wscale;
	uint32_t *flags;

	/* storage */
	uint16_t _mss;
	uint8_t _wscale;
	uint32_t _flags;
};

static inline struct synproxy *synproxy_init(void)
{
	struct synproxy *synproxy = calloc(1, sizeof(*synproxy));

	synproxy->expr.type = SYNPROXY;

	synproxy->mss = &synproxy->_mss;
	synproxy->wscale = &synproxy->_wscale;
	synproxy->flags = &synproxy->_flags;

	return synproxy;
}

static inline void free_synproxy(struct synproxy *synproxy)
{
	free(synproxy);
}

struct target {
	struct expr expr;

	const char *name;

	uint32_t *rev;
	uint8_t *data;
	int data_len;
	uint32_t *l4proto;
	uint32_t *flags;

	/* storage */
	uint32_t _rev;
	uint8_t _data[65536];
	uint32_t _l4proto;
	uint32_t _flags;
};

static inline struct target *target_init(void)
{
	struct target *target = calloc(1, sizeof(*target));

	target->expr.type = TARGET;

	target->rev = &target->_rev;
	target->data = target->_data;
	target->l4proto = &target->_l4proto;
	target->flags = &target->_flags;

	return target;
}

static void free_target(struct target *target)
{
	free((void *)target->name);
	free(target);
}

struct tproxy {
	struct expr expr;

	uint32_t *nfproto;
	uint32_t *sreg_addr;
	uint32_t *sreg_port;

	uint32_t _nfproto;
	uint32_t _sreg_addr;
	uint32_t _sreg_port;
};

static inline struct tproxy *tproxy_init(void)
{
	struct tproxy *tproxy = calloc(1, sizeof(*tproxy));

	tproxy->expr.type = TPROXY;

	tproxy->nfproto = &tproxy->_nfproto;
	tproxy->sreg_addr = &tproxy->_sreg_addr;
	tproxy->sreg_port = &tproxy->_sreg_port;

	return tproxy;
}

static inline void free_tproxy(struct tproxy *tproxy)
{
	free(tproxy);
}

struct tunnel {
	struct expr expr;

	uint32_t *dreg;
	uint32_t *key;
//	uint32_t *mode;

	uint32_t _dreg;
	uint32_t _key;
//XXX	uint32_t _mode;
};

static inline struct tunnel *tunnel_init(void)
{
	struct tunnel *tunnel = calloc(1, sizeof(*tunnel));

	tunnel->expr.type = TUNNEL;

	tunnel->dreg = &tunnel->_dreg;
	tunnel->key = &tunnel->_key;
//XXX	tunnel->mode = &tunnel->_mode;

	return tunnel;
}

static inline void free_tunnel(struct tunnel *tunnel)
{
	free(tunnel);
}

struct xfrm {
	struct expr expr;

	uint32_t *dreg;
	uint32_t *key;
	uint32_t *spnum;
	uint8_t *dir;

	uint32_t _dreg;
	uint32_t _key;
	uint32_t _spnum;
	uint8_t _dir;
};

static inline struct xfrm *xfrm_init(void)
{
	struct xfrm *xfrm = calloc(1, sizeof(*xfrm));

	xfrm->expr.type = XFRM;

	xfrm->dreg = &xfrm->_dreg;
	xfrm->key = &xfrm->_key;
	xfrm->spnum = &xfrm->_spnum;
	xfrm->dir = &xfrm->_dir;

	return xfrm;
}

static inline void free_xfrm(struct xfrm *xfrm)
{
	free(xfrm);
}

struct ct_helper {
	const char *name;
	uint8_t *l4proto;
	uint16_t *l3proto;

	/* storage */
	uint8_t _l4proto;
	uint16_t _l3proto;
};

static inline struct ct_helper *ct_helper_init(void)
{
	struct ct_helper *ct_helper = calloc(1, sizeof(*ct_helper));

	ct_helper->l4proto = &ct_helper->_l4proto;
	ct_helper->l3proto = &ct_helper->_l3proto;

	return ct_helper;
}

struct ct_timeout {
	const char *name;
	uint8_t *l4proto;
	uint16_t *l3proto;
	struct array_u32 *timeout_array;

	/* storage */
	uint8_t _l4proto;
	uint16_t _l3proto;
};

static inline struct ct_timeout *ct_timeout_init(void)
{
	struct ct_timeout *ct_timeout = calloc(1, sizeof(*ct_timeout));

	ct_timeout->l4proto = &ct_timeout->_l4proto;
	ct_timeout->l3proto = &ct_timeout->_l3proto;

	return ct_timeout;
}

struct ct_expect {
	const char *name;
	uint8_t *l4proto;
	uint16_t *l3proto;
	uint16_t *dport;
	uint32_t *timeout;
	uint8_t *size;

	/* storage */
	uint8_t _l4proto;
	uint16_t _l3proto;
	uint16_t _dport;
	uint32_t _timeout;
	uint8_t _size;
};

static inline struct ct_expect *ct_expect_init(void)
{
	struct ct_expect *ct_expect = calloc(1, sizeof(*ct_expect));

	ct_expect->l4proto = &ct_expect->_l4proto;
	ct_expect->l3proto = &ct_expect->_l3proto;
	ct_expect->dport = &ct_expect->_dport;
	ct_expect->timeout = &ct_expect->_timeout;
	ct_expect->size = &ct_expect->_size;

	return ct_expect;
}

struct tunnel_tmpl {
	uint32_t *id;
	uint32_t *flags;
	uint16_t *sport;
	uint16_t *dport;
	uint8_t *tos;
	uint8_t *ttl;
	uint32_t *src_v4;
	uint8_t *src_v6;
	uint32_t *dst_v4;
	uint8_t *dst_v6;
	uint32_t *flowlabel;

	/* storage */
	uint32_t _id;
	uint32_t _flags;
	uint16_t _sport;
	uint16_t _dport;
	uint8_t _tos;
	uint8_t _ttl;
	uint32_t _src_v4;
	uint8_t _src_v6[16];
	uint32_t _dst_v4;
	uint8_t _dst_v6[16];
	uint32_t _flowlabel;
};

static inline struct tunnel_tmpl *tunnel_tmpl_init(void)
{
	struct tunnel_tmpl *tunnel = calloc(1, sizeof(*tunnel));

	tunnel->id = &tunnel->_id;
	tunnel->flags = &tunnel->_flags;
	tunnel->sport = &tunnel->_sport;
	tunnel->dport = &tunnel->_dport;
	tunnel->tos = &tunnel->_tos;
	tunnel->ttl = &tunnel->_ttl;
	tunnel->src_v4 = &tunnel->_src_v4;
	tunnel->src_v6 = tunnel->_src_v6;
	tunnel->dst_v4 = &tunnel->_dst_v4;
	tunnel->dst_v6 = tunnel->_dst_v6;
	tunnel->flowlabel = &tunnel->_flowlabel;

	return tunnel;
}

struct secmark {
	char *ctx;
};

static inline struct secmark *secmark_init(void)
{
	struct secmark *secmark = calloc(1, sizeof(*secmark));

	return secmark;
}

struct obj {
	const char *table;
	const char *name;
	uint64_t *handle;
	const char *userdata;
	uint32_t *type;
	uint32_t *family;

	union {
		struct counter *counter;
		struct quota *quota;
		struct ct_helper *ct_helper;
		struct limit *limit;
		struct connlimit *connlimit;
		struct tunnel_tmpl *tun;
		struct ct_timeout *ct_timeout;
		struct ct_expect *ct_expect;
		struct secmark *secmark;
		struct synproxy *synproxy;
	} u;

	/* storage */
	uint32_t _type;
	uint32_t _family;
	uint64_t _handle;
};

static inline struct obj *obj_init(void)
{
	struct obj *obj = calloc(1, sizeof(*obj));

	obj->type = &obj->_type;
	obj->family = &obj->_family;
	obj->handle = &obj->_handle;

	return obj;
}

void add_obj(struct test_batch *batch, struct obj *obj);
void del_obj(struct test_batch *batch, struct obj *obj);
void free_obj(struct obj *obj);

int batch_commit(struct test_batch *batch);
int batch_abort(struct test_batch *batch);

static inline bool batch_empty(struct test_batch *batch)
{
	return list_empty(&batch->cmd);
}

void batch_reset(struct test_batch *batch);
void batch_stop(struct test_batch *batch);

int flush_ruleset(struct mnl_socket *nl);

struct array *array_alloc(void);
bool array_add(struct array *array, void *data);
void array_free(struct array *array);

struct array_u8 *array_u8_alloc(void);
bool array_u8_add(struct array_u8 *array, const char *data);
void array_u8_free(struct array_u8 *array);

struct array_u32 *array_u32_alloc(void);
bool array_u32_add(struct array_u32 *array, const char *value);
void array_u32_free(struct array_u32 *array);

bool kernel_is_tainted(void);
int list_hooks(struct mnl_socket *nl);

#endif