ruleset: properly handle zone names starting with a digit
[project/firewall4.git] / root / usr / share / firewall4 / templates / ruleset.uc
1 {%
2 let flowtable_devices = fw4.resolve_offload_devices();
3 let available_helpers = filter(fw4.helpers(), h => h.available);
4 let defined_ipsets = fw4.ipsets();
5 -%}
6
7 table inet fw4
8 flush table inet fw4
9 {% if (fw4.check_flowtable()): %}
10 delete flowtable inet fw4 ft
11 {% endif %}
12 {% fw4.includes('ruleset-prepend') %}
13
14 table inet fw4 {
15 {% if (length(flowtable_devices) > 0): %}
16 #
17 # Flowtable
18 #
19
20 flowtable ft {
21 hook ingress priority 0;
22 devices = {{ fw4.set(flowtable_devices, true) }};
23 {% if (fw4.default_option("flow_offloading_hw")): %}
24 flags offload;
25 {% endif %}
26 }
27
28
29 {% endif %}
30 {% if (length(available_helpers)): %}
31 #
32 # CT helper definitions
33 #
34
35 {% for (let helper in available_helpers): %}
36 {% for (let proto in helper.proto): %}
37 ct helper {{ helper.name }} {
38 type {{ fw4.quote(helper.name, true) }} protocol {{ proto.name }};
39 }
40
41 {% endfor %}
42 {% endfor %}
43
44 {% endif %}
45 {% if (length(defined_ipsets)): %}
46 #
47 # Set definitions
48 #
49
50 {% for (let set in defined_ipsets): %}
51 set {{ set.name }} {
52 type {{ fw4.concat(set.types) }}
53 {% if (set.maxelem > 0): %}
54 size {{ set.maxelem }}
55 {% endif %}
56 {% if (set.timeout > 0): %}
57 timeout {{ set.timeout }}s
58 {% endif %}
59 {% if (set.interval): %}
60 auto-merge
61 {% endif %}
62 {% if (set.flags): %}
63 flags {{ join(',', set.flags) }}
64 {% endif %}
65 {% fw4.print_setentries(set) %}
66 }
67
68 {% endfor %}
69
70 {% endif %}
71 #
72 # Defines
73 #
74
75 {% for (let zone in fw4.zones()): %}
76 define {{ replace(zone.name, /^[0-9]/, '_$&') }}_devices = {{ fw4.set(zone.match_devices, true) }}
77 define {{ replace(zone.name, /^[0-9]/, '_$&') }}_subnets = {{ fw4.set(zone.match_subnets, true) }}
78
79 {% endfor %}
80
81 #
82 # User includes
83 #
84
85 include "/etc/nftables.d/*.nft"
86 {% fw4.includes('table-prepend') %}
87
88
89 #
90 # Filter rules
91 #
92
93 chain input {
94 type filter hook input priority filter; policy {{ fw4.input_policy(true) }};
95
96 iifname "lo" accept comment "!fw4: Accept traffic from loopback"
97
98 {% fw4.includes('chain-prepend', 'input') %}
99 ct state established,related accept comment "!fw4: Allow inbound established and related flows"
100 {% if (fw4.default_option("drop_invalid")): %}
101 ct state invalid drop comment "!fw4: Drop flows with invalid conntrack state"
102 {% endif %}
103 {% if (fw4.default_option("synflood_protect") && fw4.default_option("synflood_rate")): %}
104 tcp flags & (fin | syn | rst | ack) == syn jump syn_flood comment "!fw4: Rate limit TCP syn packets"
105 {% endif %}
106 {% for (let rule in fw4.rules("input")): %}
107 {%+ include("rule.uc", { fw4, rule }) %}
108 {% endfor %}
109 {% for (let zone in fw4.zones()): for (let rule in zone.match_rules): %}
110 {%+ include("zone-jump.uc", { fw4, zone, rule, direction: "input" }) %}
111 {% endfor; endfor %}
112 {% if (fw4.input_policy() == "reject"): %}
113 jump handle_reject
114 {% endif %}
115 {% fw4.includes('chain-append', 'input') %}
116 }
117
118 chain forward {
119 type filter hook forward priority filter; policy {{ fw4.forward_policy(true) }};
120
121 {% if (length(flowtable_devices) > 0): %}
122 meta l4proto { tcp, udp } flow offload @ft;
123 {% endif %}
124 {% fw4.includes('chain-prepend', 'forward') %}
125 ct state established,related accept comment "!fw4: Allow forwarded established and related flows"
126 {% if (fw4.default_option("drop_invalid")): %}
127 ct state invalid drop comment "!fw4: Drop flows with invalid conntrack state"
128 {% endif %}
129 {% for (let rule in fw4.rules("forward")): %}
130 {%+ include("rule.uc", { fw4, rule }) %}
131 {% endfor %}
132 {% for (let zone in fw4.zones()): for (let rule in zone.match_rules): %}
133 {%+ include("zone-jump.uc", { fw4, zone, rule, direction: "forward" }) %}
134 {% endfor; endfor %}
135 {% fw4.includes('chain-append', 'forward') %}
136 {% if (fw4.forward_policy() == "reject"): %}
137 jump handle_reject
138 {% endif %}
139 }
140
141 chain output {
142 type filter hook output priority filter; policy {{ fw4.output_policy(true) }};
143
144 oifname "lo" accept comment "!fw4: Accept traffic towards loopback"
145
146 {% fw4.includes('chain-prepend', 'output') %}
147 ct state established,related accept comment "!fw4: Allow outbound established and related flows"
148 {% if (fw4.default_option("drop_invalid")): %}
149 ct state invalid drop comment "!fw4: Drop flows with invalid conntrack state"
150 {% endif %}
151 {% for (let rule in fw4.rules("output")): %}
152 {%+ include("rule.uc", { fw4, rule }) %}
153 {% endfor %}
154 {% for (let zone in fw4.zones()): %}
155 {% for (let rule in zone.match_rules): %}
156 {% if (zone.dflags.helper): %}
157 {% let devices_pos = fw4.filter_loopback_devs(rule.devices_pos, true); %}
158 {% let subnets_pos = fw4.filter_loopback_addrs(rule.subnets_pos, true); %}
159 {% if (devices_pos || subnets_pos): %}
160 {%+ include("zone-jump.uc", { fw4, zone, rule: { ...rule, devices_pos, subnets_pos }, direction: "helper" }) %}
161 {% endif %}
162 {% endif %}
163 {%+ include("zone-jump.uc", { fw4, zone, rule, direction: "output" }) %}
164 {% endfor %}
165 {% endfor %}
166 {% fw4.includes('chain-append', 'output') %}
167 {% if (fw4.output_policy() == "reject"): %}
168 jump handle_reject
169 {% endif %}
170 }
171
172 chain prerouting {
173 type filter hook prerouting priority filter; policy accept;
174 {% for (let zone in fw4.zones()): %}
175 {% if (zone.dflags.helper): %}
176 {% for (let rule in zone.match_rules): %}
177 {% let devices_pos = fw4.filter_loopback_devs(rule.devices_pos, false); %}
178 {% let subnets_pos = fw4.filter_loopback_addrs(rule.subnets_pos, false); %}
179 {% if (rule.devices_neg || rule.subnets_neg || devices_pos || subnets_pos): %}
180 {%+ include("zone-jump.uc", { fw4, zone, rule: { ...rule, devices_pos, subnets_pos }, direction: "helper" }) %}
181 {% endif %}
182 {% endfor %}
183 {% endif %}
184 {% endfor %}
185 }
186
187 chain handle_reject {
188 meta l4proto tcp reject with {{
189 (fw4.default_option("tcp_reject_code") != "tcp-reset")
190 ? `icmpx type ${fw4.default_option("tcp_reject_code")}`
191 : "tcp reset"
192 }} comment "!fw4: Reject TCP traffic"
193 reject with {{
194 (fw4.default_option("any_reject_code") != "tcp-reset")
195 ? `icmpx type ${fw4.default_option("any_reject_code")}`
196 : "tcp reset"
197 }} comment "!fw4: Reject any other traffic"
198 }
199
200 {% if (fw4.default_option("synflood_protect") && fw4.default_option("synflood_rate")):
201 let r = fw4.default_option("synflood_rate");
202 let b = fw4.default_option("synflood_burst");
203 %}
204 chain syn_flood {
205 limit rate {{ r.rate }}/{{ r.unit }}
206 {%- if (b): %} burst {{ b }} packets{% endif %} return comment "!fw4: Accept SYN packets below rate-limit"
207 drop comment "!fw4: Drop excess packets"
208 }
209
210 {% endif %}
211 {% for (let zone in fw4.zones()): %}
212 chain input_{{ zone.name }} {
213 {% fw4.includes('chain-prepend', `input_${zone.name}`) %}
214 {% for (let rule in fw4.rules(`input_${zone.name}`)): %}
215 {%+ include("rule.uc", { fw4, rule }) %}
216 {% endfor %}
217 {% if (zone.dflags.dnat): %}
218 ct status dnat accept comment "!fw4: Accept port redirections"
219 {% endif %}
220 {% fw4.includes('chain-append', `input_${zone.name}`) %}
221 jump {{ zone.input }}_from_{{ zone.name }}
222 }
223
224 chain output_{{ zone.name }} {
225 {% fw4.includes('chain-prepend', `output_${zone.name}`) %}
226 {% for (let rule in fw4.rules(`output_${zone.name}`)): %}
227 {%+ include("rule.uc", { fw4, rule }) %}
228 {% endfor %}
229 {% fw4.includes('chain-append', `output_${zone.name}`) %}
230 jump {{ zone.output }}_to_{{ zone.name }}
231 }
232
233 chain forward_{{ zone.name }} {
234 {% fw4.includes('chain-prepend', `forward_${zone.name}`) %}
235 {% for (let rule in fw4.rules(`forward_${zone.name}`)): %}
236 {%+ include("rule.uc", { fw4, rule }) %}
237 {% endfor %}
238 {% if (zone.dflags.dnat): %}
239 ct status dnat accept comment "!fw4: Accept port forwards"
240 {% endif %}
241 {% fw4.includes('chain-append', `forward_${zone.name}`) %}
242 jump {{ zone.forward }}_to_{{ zone.name }}
243 }
244
245 {% if (zone.dflags.helper): %}
246 chain helper_{{ zone.name }} {
247 {% for (let rule in fw4.rules(`helper_${zone.name}`)): %}
248 {%+ include("rule.uc", { fw4, rule }) %}
249 {% endfor %}
250 }
251
252 {% endif %}
253 {% for (let verdict in ["accept", "reject", "drop"]): %}
254 {% if (zone.sflags[verdict]): %}
255 chain {{ verdict }}_from_{{ zone.name }} {
256 {% for (let rule in zone.match_rules): %}
257 {%+ include("zone-verdict.uc", { fw4, zone, rule, egress: false, verdict }) %}
258 {% endfor %}
259 }
260
261 {% endif %}
262 {% if (zone.dflags[verdict]): %}
263 chain {{ verdict }}_to_{{ zone.name }} {
264 {% for (let rule in zone.match_rules): %}
265 {%+ include("zone-verdict.uc", { fw4, zone, rule, egress: true, verdict }) %}
266 {% endfor %}
267 }
268
269 {% endif %}
270 {% endfor %}
271 {% endfor %}
272
273 #
274 # NAT rules
275 #
276
277 chain dstnat {
278 type nat hook prerouting priority dstnat; policy accept;
279 {% fw4.includes('chain-prepend', 'dstnat') %}
280 {% for (let zone in fw4.zones()): %}
281 {% if (zone.dflags.dnat): %}
282 {% for (let rule in zone.match_rules): %}
283 {%+ include("zone-jump.uc", { fw4, zone, rule, direction: "dstnat" }) %}
284 {% endfor %}
285 {% endif %}
286 {% endfor %}
287 {% fw4.includes('chain-append', 'dstnat') %}
288 }
289
290 chain srcnat {
291 type nat hook postrouting priority srcnat; policy accept;
292 {% fw4.includes('chain-prepend', 'srcnat') %}
293 {% for (let redirect in fw4.redirects("srcnat")): %}
294 {%+ include("redirect.uc", { fw4, redirect }) %}
295 {% endfor %}
296 {% for (let zone in fw4.zones()): %}
297 {% if (zone.dflags.snat): %}
298 {% for (let rule in zone.match_rules): %}
299 {%+ include("zone-jump.uc", { fw4, zone, rule, direction: "srcnat" }) %}
300 {% endfor %}
301 {% endif %}
302 {% endfor %}
303 {% fw4.includes('chain-append', 'srcnat') %}
304 }
305
306 {% for (let zone in fw4.zones()): %}
307 {% if (zone.dflags.dnat): %}
308 chain dstnat_{{ zone.name }} {
309 {% fw4.includes('chain-prepend', `dstnat_${zone.name}`) %}
310 {% for (let redirect in fw4.redirects(`dstnat_${zone.name}`)): %}
311 {%+ include("redirect.uc", { fw4, redirect }) %}
312 {% endfor %}
313 {% fw4.includes('chain-append', `dstnat_${zone.name}`) %}
314 }
315
316 {% endif %}
317 {% if (zone.dflags.snat): %}
318 chain srcnat_{{ zone.name }} {
319 {% fw4.includes('chain-prepend', `srcnat_${zone.name}`) %}
320 {% for (let redirect in fw4.redirects(`srcnat_${zone.name}`)): %}
321 {%+ include("redirect.uc", { fw4, redirect }) %}
322 {% endfor %}
323 {% if (zone.masq): %}
324 {% for (let saddrs in zone.masq4_src_subnets): %}
325 {% for (let daddrs in zone.masq4_dest_subnets): %}
326 {%+ include("zone-masq.uc", { fw4, zone, family: 4, saddrs, daddrs }) %}
327 {% endfor %}
328 {% endfor %}
329 {% endif %}
330 {% if (zone.masq6): %}
331 {% for (let saddrs in zone.masq6_src_subnets): %}
332 {% for (let daddrs in zone.masq6_dest_subnets): %}
333 {%+ include("zone-masq.uc", { fw4, zone, family: 6, saddrs, daddrs }) %}
334 {% endfor %}
335 {% endfor %}
336 {% endif %}
337 {% fw4.includes('chain-append', `srcnat_${zone.name}`) %}
338 }
339
340 {% endif %}
341 {% endfor %}
342
343 #
344 # Raw rules (notrack)
345 #
346
347 chain raw_prerouting {
348 type filter hook prerouting priority raw; policy accept;
349 {% for (let zone in fw4.zones()): %}
350 {% if (zone.dflags["notrack"]): %}
351 {% for (let rule in zone.match_rules): %}
352 {% let devices_pos = fw4.filter_loopback_devs(rule.devices_pos, false); %}
353 {% let subnets_pos = fw4.filter_loopback_addrs(rule.subnets_pos, false); %}
354 {% if (rule.devices_neg || rule.subnets_neg || devices_pos || subnets_pos): %}
355 {%+ include("zone-jump.uc", { fw4, zone, rule: { ...rule, devices_pos, subnets_pos }, direction: "notrack" }) %}
356 {% endif %}
357 {% endfor %}
358 {% endif %}
359 {% endfor %}
360 {% fw4.includes('chain-append', 'raw_prerouting') %}
361 }
362
363 chain raw_output {
364 type filter hook output priority raw; policy accept;
365 {% fw4.includes('chain-prepend', 'raw_output') %}
366 {% for (let zone in fw4.zones()): %}
367 {% if (zone.dflags["notrack"]): %}
368 {% for (let rule in zone.match_rules): %}
369 {% let devices_pos = fw4.filter_loopback_devs(rule.devices_pos, true); %}
370 {% let subnets_pos = fw4.filter_loopback_addrs(rule.subnets_pos, true); %}
371 {% if (devices_pos || subnets_pos): %}
372 {%+ include("zone-jump.uc", { fw4, zone, rule: { ...rule, devices_pos, subnets_pos }, direction: "notrack" }) %}
373 {% endif %}
374 {% endfor %}
375 {% endif %}
376 {% endfor %}
377 {% fw4.includes('chain-append', 'raw_output') %}
378 }
379
380 {% for (let zone in fw4.zones()): %}
381 {% if (zone.dflags.notrack): %}
382 chain notrack_{{ zone.name }} {
383 {% for (let rule in fw4.rules(`notrack_${zone.name}`)): %}
384 {%+ include("rule.uc", { fw4, rule }) %}
385 {% endfor %}
386 }
387
388 {% endif %}
389 {% endfor %}
390
391 #
392 # Mangle rules
393 #
394
395 chain mangle_prerouting {
396 type filter hook prerouting priority mangle; policy accept;
397 {% fw4.includes('chain-prepend', 'mangle_prerouting') %}
398 {% for (let rule in fw4.rules("mangle_prerouting")): %}
399 {%+ include("rule.uc", { fw4, rule }) %}
400 {% endfor %}
401 {% fw4.includes('chain-append', 'mangle_prerouting') %}
402 }
403
404 chain mangle_postrouting {
405 type filter hook postrouting priority mangle; policy accept;
406 {% fw4.includes('chain-prepend', 'mangle_postrouting') %}
407 {% for (let rule in fw4.rules("mangle_postrouting")): %}
408 {%+ include("rule.uc", { fw4, rule }) %}
409 {% endfor %}
410 {% fw4.includes('chain-append', 'mangle_postrouting') %}
411 }
412
413 chain mangle_input {
414 type filter hook input priority mangle; policy accept;
415 {% fw4.includes('chain-prepend', 'mangle_input') %}
416 {% for (let rule in fw4.rules("mangle_input")): %}
417 {%+ include("rule.uc", { fw4, rule }) %}
418 {% endfor %}
419 {% fw4.includes('chain-append', 'mangle_input') %}
420 }
421
422 chain mangle_output {
423 type route hook output priority mangle; policy accept;
424 {% fw4.includes('chain-prepend', 'mangle_output') %}
425 {% for (let rule in fw4.rules("mangle_output")): %}
426 {%+ include("rule.uc", { fw4, rule }) %}
427 {% endfor %}
428 {% fw4.includes('chain-append', 'mangle_output') %}
429 }
430
431 chain mangle_forward {
432 type filter hook forward priority mangle; policy accept;
433 {% fw4.includes('chain-prepend', 'mangle_forward') %}
434 {% for (let rule in fw4.rules("mangle_forward")): %}
435 {%+ include("rule.uc", { fw4, rule }) %}
436 {% endfor %}
437 {% for (let zone in fw4.zones()): %}
438 {% if (zone.mtu_fix): %}
439 {% for (let rule in zone.match_rules): %}
440 {%+ include("zone-mssfix.uc", { fw4, zone, rule, egress: false }) %}
441 {%+ include("zone-mssfix.uc", { fw4, zone, rule, egress: true }) %}
442 {% endfor %}
443 {% endif %}
444 {% endfor %}
445 {% fw4.includes('chain-append', 'mangle_forward') %}
446 }
447 {% fw4.includes('table-append') %}
448 }
449 {% fw4.includes('ruleset-append') %}