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
|
Test various address selection rules in redirect rules.
-- 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": [
{
"name": "wan",
"network": [ "wan", "wan6" ],
"masq": 1,
"masq6": 1
},
{
"name": "lan",
"network": "lan",
"auto_helper": 0
},
{
"name": "noaddr",
"network": [ "noaddr" ],
"masq": 1,
"masq6": 1
}
],
"ipset": [
{
"name": "dest-a",
"match": "dest_ip",
"entry": [ "198.51.100.10", "203.0.113.10" ]
},
{
"name": "dest-b",
"match": "dest_ip",
"entry": [ "198.51.100.20", "203.0.113.20" ]
}
],
"redirect": [
{
".description": "Ensure unspecified family with no src, dest or rewrite address is treated as IPv4 only",
"name": "Redirect test #1",
"src": "lan",
"dest": "wan",
"proto": "udp",
"src_dport": "53",
"dest_port": "53",
"target": "dnat"
},
{
".description": "Ensure that explicit family any with no src, dest or rewrite address is treated as IPv4/IPv6",
"name": "Redirect test #2",
"family": "any",
"src": "lan",
"dest": "wan",
"proto": "udp",
"src_dport": "53",
"dest_port": "53",
"target": "dnat"
},
{
".description": "Ensure that a DNAT without explicit dest zone infers the zone from the rewrite address",
"name": "Redirect test #3",
"src": "wan",
"proto": "tcp",
"src_dport": "22",
"dest_ip": "192.168.26.100"
},
{
".description": "Ensure that external reflection source uses the src zone addr as reflection source IP",
"name": "Redirect test #4",
"src": "wan",
"proto": "tcp",
"src_dport": "23",
"dest_ip": "192.168.26.100",
"dest_port": "23",
"reflection_src": "external"
},
{
".description": "Ensure that reflection is disabled if external address cannot be determined",
"name": "Redirect test #5",
"src": "noaddr",
"dest": "lan",
"proto": "tcp",
"src_dport": "24",
"dest_ip": "192.168.26.100"
},
{
".description": "Ensure that the rewrite IPv6 address is using bracket notation when a port is specified",
"name": "Redirect test #6",
"family": "ipv6",
"src": "wan",
"dest": "lan",
"proto": "tcp",
"src_dport": "25",
"dest_ip": "2001:db8:1000:1::1234",
"dest_port": "25",
"target": "dnat"
},
{
".description": "Ensure that family restricted redirect rules work properly",
"name": "Redirect test #7",
"family": "ipv4",
"src": "wan",
"dest": "lan",
"proto": "tcp",
"src_dport": "26",
"dest_port": "26",
"target": "dnat"
},
{
".description": "Ensure that family restricted redirect rules work properly",
"name": "Redirect test #8",
"family": "ipv6",
"src": "wan",
"dest": "lan",
"proto": "tcp",
"src_dport": "27",
"target": "dnat"
},
{
".description": "Ensure inverse address match works properly",
"name": "Redirect test #9",
"src": "lan",
"dest": "wan",
"proto": "tcp udp",
"src_dport": "53",
"dest_ip": "192.168.1.53",
"src_ip": "!192.168.1.53",
"target": "dnat"
},
{
".description": "Ensure reflection uses the destination ipset instead of the WAN address",
"name": "Redirect test #9",
"src": "wan",
"dest": "lan",
"proto": "tcpudp",
"src_dport": "443",
"dest_ip": "192.168.26.100",
"dest_port": "8443",
"ipset": "dest-a",
"reflection": 1,
"target": "dnat"
},
{
".description": "Ensure reflection keeps different destination ipsets for forwards sharing a port",
"name": "Redirect test #10",
"src": "wan",
"dest": "lan",
"proto": "tcpudp",
"src_dport": "443",
"dest_ip": "192.168.26.200",
"dest_port": "9443",
"ipset": "dest-b",
"reflection": 1,
"target": "dnat"
}
],
"nat": [
{
".description": "Ensure that masq to specific port or port range rules work properly",
"name": "masq snat test #1",
"family": "ipv4",
"src": "wan",
"proto": "tcp udp",
"snat_port": "61000-62900",
"target": "MASQUERADE"
}
]
}
-- End --
-- Expect stderr --
[!] Section @redirect[2] (Redirect test #3) does not specify a destination, assuming 'lan'
[!] Section @redirect[3] (Redirect test #4) does not specify a destination, assuming 'lan'
[!] Section @redirect[4] (Redirect test #5) external address range cannot be determined, disabling reflection
-- End --
-- Expect stdout --
table inet fw4
flush table inet fw4
table inet fw4 {
#
# Set definitions
#
set dest-a {
type ipv4_addr
elements = {
198.51.100.10,
203.0.113.10,
}
}
set dest-b {
type ipv4_addr
elements = {
198.51.100.20,
203.0.113.20,
}
}
#
# Defines
#
define wan_devices = { "pppoe-wan" }
define wan_subnets = { 10.11.12.0/24, 2001:db8:54:321::/64 }
define lan_devices = { "br-lan" }
define lan_subnets = { 10.0.0.0/24, 192.168.26.0/24, 2001:db8:1000::/60, fd63:e2f:f706::/60 }
define noaddr_devices = { "wwan0" }
define noaddr_subnets = { }
#
# 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"
iifname "pppoe-wan" jump input_wan comment "!fw4: Handle wan IPv4/IPv6 input traffic"
iifname "br-lan" jump input_lan comment "!fw4: Handle lan IPv4/IPv6 input traffic"
iifname "wwan0" jump input_noaddr comment "!fw4: Handle noaddr IPv4/IPv6 input traffic"
}
chain forward {
type filter hook forward priority filter; policy drop;
ct state vmap { established : accept, related : accept } comment "!fw4: Handle forwarded flows"
iifname "pppoe-wan" jump forward_wan comment "!fw4: Handle wan IPv4/IPv6 forward traffic"
iifname "br-lan" jump forward_lan comment "!fw4: Handle lan IPv4/IPv6 forward traffic"
iifname "wwan0" jump forward_noaddr comment "!fw4: Handle noaddr IPv4/IPv6 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"
oifname "pppoe-wan" jump output_wan comment "!fw4: Handle wan IPv4/IPv6 output traffic"
oifname "br-lan" jump output_lan comment "!fw4: Handle lan IPv4/IPv6 output traffic"
oifname "wwan0" jump output_noaddr comment "!fw4: Handle noaddr IPv4/IPv6 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_wan {
ct status dnat accept comment "!fw4: Accept port redirections"
jump drop_from_wan
}
chain output_wan {
jump drop_to_wan
}
chain forward_wan {
ct status dnat accept comment "!fw4: Accept port forwards"
jump drop_to_wan
}
chain drop_from_wan {
iifname "pppoe-wan" counter drop comment "!fw4: drop wan IPv4/IPv6 traffic"
}
chain drop_to_wan {
oifname "pppoe-wan" counter drop comment "!fw4: drop wan IPv4/IPv6 traffic"
}
chain input_lan {
ct status dnat accept comment "!fw4: Accept port redirections"
jump drop_from_lan
}
chain output_lan {
jump drop_to_lan
}
chain forward_lan {
ct status dnat accept comment "!fw4: Accept port forwards"
jump drop_to_lan
}
chain accept_to_lan {
oifname "br-lan" counter accept comment "!fw4: accept lan IPv4/IPv6 traffic"
}
chain drop_from_lan {
iifname "br-lan" counter drop comment "!fw4: drop lan IPv4/IPv6 traffic"
}
chain drop_to_lan {
oifname "br-lan" counter drop comment "!fw4: drop lan IPv4/IPv6 traffic"
}
chain input_noaddr {
ct status dnat accept comment "!fw4: Accept port redirections"
jump drop_from_noaddr
}
chain output_noaddr {
jump drop_to_noaddr
}
chain forward_noaddr {
ct status dnat accept comment "!fw4: Accept port forwards"
jump drop_to_noaddr
}
chain drop_from_noaddr {
iifname "wwan0" counter drop comment "!fw4: drop noaddr IPv4/IPv6 traffic"
}
chain drop_to_noaddr {
oifname "wwan0" counter drop comment "!fw4: drop noaddr IPv4/IPv6 traffic"
}
#
# NAT rules
#
chain dstnat {
type nat hook prerouting priority dstnat; policy accept;
iifname "pppoe-wan" jump dstnat_wan comment "!fw4: Handle wan IPv4/IPv6 dstnat traffic"
iifname "br-lan" jump dstnat_lan comment "!fw4: Handle lan IPv4/IPv6 dstnat traffic"
iifname "wwan0" jump dstnat_noaddr comment "!fw4: Handle noaddr IPv4/IPv6 dstnat traffic"
}
chain srcnat {
type nat hook postrouting priority srcnat; policy accept;
oifname "pppoe-wan" jump srcnat_wan comment "!fw4: Handle wan IPv4/IPv6 srcnat traffic"
oifname "br-lan" jump srcnat_lan comment "!fw4: Handle lan IPv4/IPv6 srcnat traffic"
oifname "wwan0" jump srcnat_noaddr comment "!fw4: Handle noaddr IPv4/IPv6 srcnat traffic"
}
chain dstnat_wan {
meta nfproto ipv4 tcp dport 22 counter dnat 192.168.26.100 comment "!fw4: Redirect test #3"
meta nfproto ipv4 tcp dport 23 counter dnat 192.168.26.100:23 comment "!fw4: Redirect test #4"
meta nfproto ipv6 tcp dport 25 counter dnat [2001:db8:1000:1::1234]:25 comment "!fw4: Redirect test #6"
meta nfproto ipv4 tcp dport 26 counter redirect to 26 comment "!fw4: Redirect test #7"
meta nfproto ipv6 tcp dport 27 counter redirect comment "!fw4: Redirect test #8"
meta nfproto ipv4 tcp dport 443 ip daddr @dest-a counter dnat 192.168.26.100:8443 comment "!fw4: Redirect test #9"
meta nfproto ipv4 udp dport 443 ip daddr @dest-a counter dnat 192.168.26.100:8443 comment "!fw4: Redirect test #9"
meta nfproto ipv4 tcp dport 443 ip daddr @dest-b counter dnat 192.168.26.200:9443 comment "!fw4: Redirect test #10"
meta nfproto ipv4 udp dport 443 ip daddr @dest-b counter dnat 192.168.26.200:9443 comment "!fw4: Redirect test #10"
}
chain srcnat_wan {
meta nfproto ipv4 meta l4proto tcp counter masquerade to :61000-62900 comment "!fw4: masq snat test #1"
meta nfproto ipv4 meta l4proto udp counter masquerade to :61000-62900 comment "!fw4: masq snat test #1"
meta nfproto ipv4 masquerade comment "!fw4: Masquerade IPv4 wan traffic"
meta nfproto ipv6 masquerade comment "!fw4: Masquerade IPv6 wan traffic"
}
chain dstnat_lan {
meta nfproto ipv4 udp dport 53 counter redirect to 53 comment "!fw4: Redirect test #1"
udp dport 53 counter redirect to 53 comment "!fw4: Redirect test #2"
ip saddr { 10.0.0.0/24, 192.168.26.0/24 } ip daddr 10.11.12.194 tcp dport 22 dnat 192.168.26.100 comment "!fw4: Redirect test #3 (reflection)"
ip saddr { 10.0.0.0/24, 192.168.26.0/24 } ip daddr 10.11.12.194 tcp dport 23 dnat 192.168.26.100:23 comment "!fw4: Redirect test #4 (reflection)"
ip6 saddr { 2001:db8:1000::/60, fd63:e2f:f706::/60 } ip6 daddr 2001:db8:54:321::2 tcp dport 25 dnat [2001:db8:1000:1::1234]:25 comment "!fw4: Redirect test #6 (reflection)"
ip saddr != 192.168.1.53 tcp dport 53 counter dnat 192.168.1.53 comment "!fw4: Redirect test #9"
ip saddr != 192.168.1.53 udp dport 53 counter dnat 192.168.1.53 comment "!fw4: Redirect test #9"
ip saddr { 10.0.0.0/24, 192.168.26.0/24 } tcp dport 443 ip daddr @dest-a dnat 192.168.26.100:8443 comment "!fw4: Redirect test #9 (reflection)"
ip saddr { 10.0.0.0/24, 192.168.26.0/24 } udp dport 443 ip daddr @dest-a dnat 192.168.26.100:8443 comment "!fw4: Redirect test #9 (reflection)"
ip saddr { 10.0.0.0/24, 192.168.26.0/24 } tcp dport 443 ip daddr @dest-b dnat 192.168.26.200:9443 comment "!fw4: Redirect test #10 (reflection)"
ip saddr { 10.0.0.0/24, 192.168.26.0/24 } udp dport 443 ip daddr @dest-b dnat 192.168.26.200:9443 comment "!fw4: Redirect test #10 (reflection)"
}
chain srcnat_lan {
ip saddr { 10.0.0.0/24, 192.168.26.0/24 } ip daddr 192.168.26.100 tcp dport 22 snat 192.168.26.1 comment "!fw4: Redirect test #3 (reflection)"
ip saddr { 10.0.0.0/24, 192.168.26.0/24 } ip daddr 192.168.26.100 tcp dport 23 snat 10.11.12.194 comment "!fw4: Redirect test #4 (reflection)"
ip6 saddr { 2001:db8:1000::/60, fd63:e2f:f706::/60 } ip6 daddr 2001:db8:1000:1::1234 tcp dport 25 snat 2001:db8:1000:1::1 comment "!fw4: Redirect test #6 (reflection)"
ip saddr { 10.0.0.0/24, 192.168.26.0/24 } ip daddr 192.168.26.100 tcp dport 8443 snat 192.168.26.1 comment "!fw4: Redirect test #9 (reflection)"
ip saddr { 10.0.0.0/24, 192.168.26.0/24 } ip daddr 192.168.26.100 udp dport 8443 snat 192.168.26.1 comment "!fw4: Redirect test #9 (reflection)"
ip saddr { 10.0.0.0/24, 192.168.26.0/24 } ip daddr 192.168.26.200 tcp dport 9443 snat 192.168.26.1 comment "!fw4: Redirect test #10 (reflection)"
ip saddr { 10.0.0.0/24, 192.168.26.0/24 } ip daddr 192.168.26.200 udp dport 9443 snat 192.168.26.1 comment "!fw4: Redirect test #10 (reflection)"
}
chain dstnat_noaddr {
meta nfproto ipv4 tcp dport 24 counter dnat 192.168.26.100 comment "!fw4: Redirect test #5"
}
chain srcnat_noaddr {
meta nfproto ipv4 masquerade comment "!fw4: Masquerade IPv4 noaddr traffic"
meta nfproto ipv6 masquerade comment "!fw4: Masquerade IPv6 noaddr traffic"
}
#
# 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 --
|