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