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