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
|
Testing various option constraints.
-- Testcase --
{%
include("./root/usr/share/firewall4/main.uc", {
getenv: function(varname) {
switch (varname) {
case 'ACTION':
return 'print';
}
}
})
%}
-- End --
-- File uci/helpers.json --
{}
-- End --
-- File uci/firewall.json --
{
"zone": [
{
".description": "A zone matching only IPv4 subnets is assumed to be an IPv4 only zone",
"name": "ipv4only",
"subnet": "192.168.1.0/24",
"auto_helper": 0
},
{
".description": "A zone with conflicting family and subnet settings should be skipped",
"name": "afconflict",
"subnet": "10.0.0.0/8",
"family": "IPv6",
"auto_helper": 0
}
],
"ipset": [
{
"name": "ipv4set",
"match": "src_ip",
"entry": [
"10.0.0.2",
"10.0.0.3",
"10.0.0.4"
]
}
],
"rule": [
{
".description": "Rules referencing an IPv4 only zone should be restricted to IPv4 themselves",
"src": "ipv4only",
"proto": "tcp",
"dest_port": "22",
"name": "Rule #1",
"target": "accept"
},
{
".description": "Rules whose family conflicts with their addresses should be skipped",
"proto": "tcp",
"src_ip": "10.0.0.1",
"dest_port": "22",
"name": "Rule #2",
"target": "accept",
"family": "IPv6"
},
{
".description": "Rules whose family conflicts with the zone family should be skipped",
"src": "ipv4only",
"proto": "tcp",
"dest_port": "22",
"name": "Rule #3",
"target": "accept",
"family": "IPv6"
},
{
".description": "Rules whose family conflicts with the referenced set family should be skipped",
"src": "ipv4only",
"proto": "tcp",
"ipset": "ipv4set",
"name": "Rule #4",
"target": "accept",
"family": "IPv6"
}
],
"redirect": [
{
".description": "Redirects whose family conflicts with the referenced zone family should be skipped",
"src": "ipv4only",
"proto": "tcp",
"src_dport": "22",
"dest_ip": "fdca::1",
"name": "Redirect #1",
"target": "dnat"
},
],
"nat": [
{
".description": "NAT rules whose family conflicts with the referenced zone family should be skipped",
"name": "NAT #1",
"family": "ipv6",
"src": "ipv4only",
"target": "masquerade"
},
{
".description": "NAT rules whose family conflicts with their addresses should be skipped",
"name": "NAT #2",
"family": "ipv4",
"src": "*",
"src_ip": "fc00::/7",
"target": "masquerade"
},
{
".description": "NAT rules without any AF specific bits and unspecified family should default to IPv4 for backwards compatibility",
"name": "NAT #3",
"src": "*",
"target": "masquerade"
},
{
".description": "NAT rules without explicit family but IPv6 specific bits should be IPv6",
"name": "NAT #4",
"src": "*",
"src_ip": "fc00::/7",
"target": "masquerade"
},
{
".description": "NAT rules with explicit family any should inherit zone restrictions",
"name": "NAT #5",
"src": "ipv4only",
"target": "masquerade"
},
{
".description": "NAT rules without any AF specific bits but explicit family any should be IPv4/IPv6",
"name": "NAT #6",
"family": "any",
"src": "*",
"target": "masquerade"
}
]
}
-- End --
-- Expect stderr --
[!] Section @zone[1] (afconflict) is restricted to IPv6 but referenced subnet list is IPv4 only, skipping
[!] Section @rule[1] (Rule #2) is restricted to IPv6 but referenced source IP is IPv4 only, skipping
[!] Section @rule[2] (Rule #3) is restricted to IPv6 but referenced source zone is IPv4 only, skipping
[!] Section @rule[3] (Rule #4) is restricted to IPv6 but referenced set match is IPv4 only, skipping
[!] Section @redirect[0] (Redirect #1) is restricted to IPv6 but referenced source zone is IPv4 only, skipping
[!] Section @nat[0] (NAT #1) is restricted to IPv6 but referenced source zone is IPv4 only, skipping
[!] Section @nat[1] (NAT #2) is restricted to IPv4 but referenced source IP is IPv6 only, skipping
-- End --
-- Expect stdout --
table inet fw4
flush table inet fw4
table inet fw4 {
#
# Set definitions
#
set ipv4set {
type ipv4_addr
elements = {
10.0.0.2,
10.0.0.3,
10.0.0.4,
}
}
#
# Defines
#
define ipv4only_devices = { }
define ipv4only_subnets = { 192.168.1.0/24 }
#
# User includes
#
include "/etc/nftables.d/*.nft"
#
# Filter rules
#
chain input {
type filter hook input priority filter; policy drop;
iif "lo" accept comment "!fw4: Accept traffic from loopback"
ct state vmap { established : accept, related : accept } comment "!fw4: Handle inbound flows"
meta nfproto ipv4 ip saddr 192.168.1.0/24 jump input_ipv4only comment "!fw4: Handle ipv4only IPv4 input traffic"
}
chain forward {
type filter hook forward priority filter; policy drop;
ct state vmap { established : accept, related : accept } comment "!fw4: Handle forwarded flows"
meta nfproto ipv4 ip saddr 192.168.1.0/24 jump forward_ipv4only comment "!fw4: Handle ipv4only IPv4 forward traffic"
}
chain output {
type filter hook output priority filter; policy drop;
oif "lo" accept comment "!fw4: Accept traffic towards loopback"
ct state vmap { established : accept, related : accept } comment "!fw4: Handle outbound flows"
meta nfproto ipv4 ip daddr 192.168.1.0/24 jump output_ipv4only comment "!fw4: Handle ipv4only IPv4 output traffic"
}
chain prerouting {
type filter hook prerouting priority filter; policy accept;
}
chain handle_reject {
meta l4proto tcp reject with tcp reset comment "!fw4: Reject TCP traffic"
reject with icmpx type port-unreachable comment "!fw4: Reject any other traffic"
}
chain input_ipv4only {
meta nfproto ipv4 tcp dport 22 counter accept comment "!fw4: Rule #1"
ct status dnat accept comment "!fw4: Accept port redirections"
jump drop_from_ipv4only
}
chain output_ipv4only {
jump drop_to_ipv4only
}
chain forward_ipv4only {
ct status dnat accept comment "!fw4: Accept port forwards"
jump drop_to_ipv4only
}
chain drop_from_ipv4only {
meta nfproto ipv4 ip saddr 192.168.1.0/24 counter drop comment "!fw4: drop ipv4only IPv4 traffic"
}
chain drop_to_ipv4only {
meta nfproto ipv4 ip daddr 192.168.1.0/24 counter drop comment "!fw4: drop ipv4only IPv4 traffic"
}
#
# NAT rules
#
chain dstnat {
type nat hook prerouting priority dstnat; policy accept;
meta nfproto ipv4 ip saddr 192.168.1.0/24 jump dstnat_ipv4only comment "!fw4: Handle ipv4only IPv4 dstnat traffic"
}
chain srcnat {
type nat hook postrouting priority srcnat; policy accept;
meta nfproto ipv4 counter masquerade comment "!fw4: NAT #3"
ip6 saddr fc00::/7 counter masquerade comment "!fw4: NAT #4"
counter masquerade comment "!fw4: NAT #6"
meta nfproto ipv4 ip daddr 192.168.1.0/24 jump srcnat_ipv4only comment "!fw4: Handle ipv4only IPv4 srcnat traffic"
}
chain dstnat_ipv4only {
}
chain srcnat_ipv4only {
meta nfproto ipv4 counter masquerade comment "!fw4: NAT #5"
}
#
# Raw rules (notrack)
#
chain raw_prerouting {
type filter hook prerouting priority raw; policy accept;
}
chain raw_output {
type filter hook output priority raw; policy accept;
}
#
# Mangle rules
#
chain mangle_prerouting {
type filter hook prerouting priority mangle; policy accept;
}
chain mangle_postrouting {
type filter hook postrouting priority mangle; policy accept;
}
chain mangle_input {
type filter hook input priority mangle; policy accept;
}
chain mangle_output {
type route hook output priority mangle; policy accept;
}
chain mangle_forward {
type filter hook forward priority mangle; policy accept;
}
}
-- End --
|