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