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