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