ruleset: reuse zone-jump.uc template for notrack and helper chain jumps
[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 {%+ include("zone-jump.uc", { fw4, zone, rule: { ...rule, devices_pos, subnets_pos }, direction: "helper" }) %}
151 {% endif %}
152 {% endif %}
153 {%+ include("zone-jump.uc", { fw4, zone, rule, direction: "output" }) %}
154 {% endfor %}
155 {% endfor %}
156 {% if (fw4.output_policy() == "reject"): %}
157 jump handle_reject
158 {% endif %}
159 }
160
161 chain prerouting {
162 type filter hook prerouting priority filter; policy accept;
163 {% for (let zone in fw4.zones()): %}
164 {% if (zone.dflags.helper): %}
165 {% for (let rule in zone.match_rules): %}
166 {% let devices_pos = fw4.filter_loopback_devs(rule.devices_pos, false); %}
167 {% let subnets_pos = fw4.filter_loopback_addrs(rule.subnets_pos, false); %}
168 {% if (rule.devices_neg || rule.subnets_neg || devices_pos || subnets_pos): %}
169 {%+ include("zone-jump.uc", { fw4, zone, rule: { ...rule, devices_pos, subnets_pos }, direction: "helper" }) %}
170 {% endif %}
171 {% endfor %}
172 {% endif %}
173 {% endfor %}
174 }
175
176 chain handle_reject {
177 meta l4proto tcp reject with {{
178 (fw4.default_option("tcp_reject_code") != "tcp-reset")
179 ? `icmpx type ${fw4.default_option("tcp_reject_code")}`
180 : "tcp reset"
181 }} comment "!fw4: Reject TCP traffic"
182 reject with {{
183 (fw4.default_option("any_reject_code") != "tcp-reset")
184 ? `icmpx type ${fw4.default_option("any_reject_code")}`
185 : "tcp reset"
186 }} comment "!fw4: Reject any other traffic"
187 }
188
189 {% if (fw4.default_option("synflood_protect") && fw4.default_option("synflood_rate")):
190 let r = fw4.default_option("synflood_rate");
191 let b = fw4.default_option("synflood_burst");
192 %}
193 chain syn_flood {
194 limit rate {{ r.rate }}/{{ r.unit }}
195 {%- if (b): %} burst {{ b }} packets{% endif %} return comment "!fw4: Accept SYN packets below rate-limit"
196 drop comment "!fw4: Drop excess packets"
197 }
198
199 {% endif %}
200 {% for (let zone in fw4.zones()): %}
201 chain input_{{ zone.name }} {
202 {% for (let rule in fw4.rules(`input_${zone.name}`)): %}
203 {%+ include("rule.uc", { fw4, rule }) %}
204 {% endfor %}
205 {% if (zone.dflags.dnat): %}
206 ct status dnat accept comment "!fw4: Accept port redirections"
207 {% endif %}
208 jump {{ zone.input }}_from_{{ zone.name }}
209 }
210
211 chain output_{{ zone.name }} {
212 {% for (let rule in fw4.rules(`output_${zone.name}`)): %}
213 {%+ include("rule.uc", { fw4, rule }) %}
214 {% endfor %}
215 jump {{ zone.output }}_to_{{ zone.name }}
216 }
217
218 chain forward_{{ zone.name }} {
219 {% for (let rule in fw4.rules(`forward_${zone.name}`)): %}
220 {%+ include("rule.uc", { fw4, rule }) %}
221 {% endfor %}
222 {% if (zone.dflags.dnat): %}
223 ct status dnat accept comment "!fw4: Accept port forwards"
224 {% endif %}
225 jump {{ zone.forward }}_to_{{ zone.name }}
226 }
227
228 {% if (zone.dflags.helper): %}
229 chain helper_{{ zone.name }} {
230 {% for (let rule in fw4.rules(`helper_${zone.name}`)): %}
231 {%+ include("rule.uc", { fw4, rule }) %}
232 {% endfor %}
233 }
234
235 {% endif %}
236 {% for (let verdict in ["accept", "reject", "drop"]): %}
237 {% if (zone.sflags[verdict]): %}
238 chain {{ verdict }}_from_{{ zone.name }} {
239 {% for (let rule in zone.match_rules): %}
240 {%+ include("zone-verdict.uc", { fw4, zone, rule, egress: false, verdict }) %}
241 {% endfor %}
242 }
243
244 {% endif %}
245 {% if (zone.dflags[verdict]): %}
246 chain {{ verdict }}_to_{{ zone.name }} {
247 {% for (let rule in zone.match_rules): %}
248 {%+ include("zone-verdict.uc", { fw4, zone, rule, egress: true, verdict }) %}
249 {% endfor %}
250 }
251
252 {% endif %}
253 {% endfor %}
254 {% endfor %}
255
256 #
257 # NAT rules
258 #
259
260 chain dstnat {
261 type nat hook prerouting priority dstnat; policy accept;
262 {% for (let zone in fw4.zones()): %}
263 {% if (zone.dflags.dnat): %}
264 {% for (let rule in zone.match_rules): %}
265 {%+ include("zone-jump.uc", { fw4, zone, rule, direction: "dstnat" }) %}
266 {% endfor %}
267 {% endif %}
268 {% endfor %}
269 }
270
271 chain srcnat {
272 type nat hook postrouting priority srcnat; policy accept;
273 {% for (let redirect in fw4.redirects("srcnat")): %}
274 {%+ include("redirect.uc", { fw4, redirect }) %}
275 {% endfor %}
276 {% for (let zone in fw4.zones()): %}
277 {% if (zone.dflags.snat): %}
278 {% for (let rule in zone.match_rules): %}
279 {%+ include("zone-jump.uc", { fw4, zone, rule, direction: "srcnat" }) %}
280 {% endfor %}
281 {% endif %}
282 {% endfor %}
283 }
284
285 {% for (let zone in fw4.zones()): %}
286 {% if (zone.dflags.dnat): %}
287 chain dstnat_{{ zone.name }} {
288 {% for (let redirect in fw4.redirects(`dstnat_${zone.name}`)): %}
289 {%+ include("redirect.uc", { fw4, redirect }) %}
290 {% endfor %}
291 }
292
293 {% endif %}
294 {% if (zone.dflags.snat): %}
295 chain srcnat_{{ zone.name }} {
296 {% for (let redirect in fw4.redirects(`srcnat_${zone.name}`)): %}
297 {%+ include("redirect.uc", { fw4, redirect }) %}
298 {% endfor %}
299 {% if (zone.masq): %}
300 {% for (let saddrs in zone.masq4_src_subnets): %}
301 {% for (let daddrs in zone.masq4_dest_subnets): %}
302 {%+ include("zone-masq.uc", { fw4, zone, family: 4, saddrs, daddrs }) %}
303 {% endfor %}
304 {% endfor %}
305 {% endif %}
306 {% if (zone.masq6): %}
307 {% for (let saddrs in zone.masq6_src_subnets): %}
308 {% for (let daddrs in zone.masq6_dest_subnets): %}
309 {%+ include("zone-masq.uc", { fw4, zone, family: 6, saddrs, daddrs }) %}
310 {% endfor %}
311 {% endfor %}
312 {% endif %}
313 }
314
315 {% endif %}
316 {% endfor %}
317
318 #
319 # Raw rules (notrack)
320 #
321
322 chain raw_prerouting {
323 type filter hook prerouting priority raw; policy accept;
324 {% for (let zone in fw4.zones()): %}
325 {% if (zone.dflags["notrack"]): %}
326 {% for (let rule in zone.match_rules): %}
327 {% let devices_pos = fw4.filter_loopback_devs(rule.devices_pos, false); %}
328 {% let subnets_pos = fw4.filter_loopback_addrs(rule.subnets_pos, false); %}
329 {% if (rule.devices_neg || rule.subnets_neg || devices_pos || subnets_pos): %}
330 {%+ include("zone-jump.uc", { fw4, zone, rule: { ...rule, devices_pos, subnets_pos }, direction: "notrack" }) %}
331 {% endif %}
332 {% endfor %}
333 {% endif %}
334 {% endfor %}
335 }
336
337 chain raw_output {
338 type filter hook output priority raw; policy accept;
339 {% for (let zone in fw4.zones()): %}
340 {% if (zone.dflags["notrack"]): %}
341 {% for (let rule in zone.match_rules): %}
342 {% let devices_pos = fw4.filter_loopback_devs(rule.devices_pos, true); %}
343 {% let subnets_pos = fw4.filter_loopback_addrs(rule.subnets_pos, true); %}
344 {% if (devices_pos || subnets_pos): %}
345 {%+ include("zone-jump.uc", { fw4, zone, rule: { ...rule, devices_pos, subnets_pos }, direction: "notrack" }) %}
346 {% endif %}
347 {% endfor %}
348 {% endif %}
349 {% endfor %}
350 }
351
352 {% for (let zone in fw4.zones()): %}
353 {% if (zone.dflags.notrack): %}
354 chain notrack_{{ zone.name }} {
355 {% for (let rule in fw4.rules(`notrack_${zone.name}`)): %}
356 {%+ include("rule.uc", { fw4, rule }) %}
357 {% endfor %}
358 }
359
360 {% endif %}
361 {% endfor %}
362
363 #
364 # Mangle rules
365 #
366
367 chain mangle_prerouting {
368 type filter hook prerouting priority mangle; policy accept;
369 {% for (let rule in fw4.rules("mangle_prerouting")): %}
370 {%+ include("rule.uc", { fw4, rule }) %}
371 {% endfor %}
372 }
373
374 chain mangle_postrouting {
375 type filter hook postrouting priority mangle; policy accept;
376 {% for (let rule in fw4.rules("mangle_postrouting")): %}
377 {%+ include("rule.uc", { fw4, rule }) %}
378 {% endfor %}
379 }
380
381 chain mangle_input {
382 type filter hook input priority mangle; policy accept;
383 {% for (let rule in fw4.rules("mangle_input")): %}
384 {%+ include("rule.uc", { fw4, rule }) %}
385 {% endfor %}
386 }
387
388 chain mangle_output {
389 type route hook output priority mangle; policy accept;
390 {% for (let rule in fw4.rules("mangle_output")): %}
391 {%+ include("rule.uc", { fw4, rule }) %}
392 {% endfor %}
393 }
394
395 chain mangle_forward {
396 type filter hook forward priority mangle; policy accept;
397 {% for (let rule in fw4.rules("mangle_forward")): %}
398 {%+ include("rule.uc", { fw4, rule }) %}
399 {% endfor %}
400 {% for (let zone in fw4.zones()): %}
401 {% if (zone.mtu_fix): %}
402 {% for (let rule in zone.match_rules): %}
403 {%+ include("zone-mssfix.uc", { fw4, zone, rule, egress: false }) %}
404 {%+ include("zone-mssfix.uc", { fw4, zone, rule, egress: true }) %}
405 {% endfor %}
406 {% endif %}
407 {% endfor %}
408 }
409 }