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