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