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