2 let flowtable_devices = fw4.resolve_offload_devices();
3 let available_helpers = filter(fw4.helpers(), h => h.available);
4 let defined_ipsets = fw4.ipsets();
9 {% if (fw4.check_flowtable()): %}
10 delete flowtable inet fw4 ft
12 {% fw4.includes('ruleset-prepend') %}
15 {% if (length(flowtable_devices) > 0): %}
21 hook ingress priority 0;
22 devices = {{ fw4.set(flowtable_devices, true) }};
23 {% if (fw4.default_option("flow_offloading_hw")): %}
30 {% if (length(available_helpers)): %}
32 # CT helper definitions
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 }};
45 {% if (length(defined_ipsets)): %}
50 {% for (let set in defined_ipsets): %}
52 type {{ fw4.concat(set.types) }}
53 {% if (set.maxelem > 0): %}
54 size {{ set.maxelem }}
56 {% if (set.timeout > 0): %}
57 timeout {{ set.timeout }}s
59 {% if (set.interval): %}
63 flags {{ join(',', set.flags) }}
65 {% fw4.print_setentries(set) %}
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) }}
85 include "/etc/nftables.d/*.nft"
86 {% fw4.includes('table-prepend') %}
94 type filter hook input priority filter; policy {{ fw4.input_policy(true) }};
96 iifname "lo" accept comment "!fw4: Accept traffic from loopback"
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"
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"
106 {% for (let rule in fw4.rules("input")): %}
107 {%+ include("rule.uc", { fw4, rule }) %}
109 {% for (let zone in fw4.zones()): for (let rule in zone.match_rules): %}
110 {%+ include("zone-jump.uc", { fw4, zone, rule, direction: "input" }) %}
112 {% if (fw4.input_policy() == "reject"): %}
115 {% fw4.includes('chain-append', 'input') %}
119 type filter hook forward priority filter; policy {{ fw4.forward_policy(true) }};
121 {% if (length(flowtable_devices) > 0): %}
122 meta l4proto { tcp, udp } flow offload @ft;
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"
129 {% for (let rule in fw4.rules("forward")): %}
130 {%+ include("rule.uc", { fw4, rule }) %}
132 {% for (let zone in fw4.zones()): for (let rule in zone.match_rules): %}
133 {%+ include("zone-jump.uc", { fw4, zone, rule, direction: "forward" }) %}
135 {% fw4.includes('chain-append', 'forward') %}
136 {% if (fw4.forward_policy() == "reject"): %}
142 type filter hook output priority filter; policy {{ fw4.output_policy(true) }};
144 oifname "lo" accept comment "!fw4: Accept traffic towards loopback"
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"
151 {% for (let rule in fw4.rules("output")): %}
152 {%+ include("rule.uc", { fw4, rule }) %}
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" }) %}
163 {%+ include("zone-jump.uc", { fw4, zone, rule, direction: "output" }) %}
166 {% fw4.includes('chain-append', 'output') %}
167 {% if (fw4.output_policy() == "reject"): %}
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" }) %}
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")}`
192 }} comment "!fw4: Reject TCP traffic"
194 (fw4.default_option("any_reject_code") != "tcp-reset")
195 ? `icmpx type ${fw4.default_option("any_reject_code")}`
197 }} comment "!fw4: Reject any other traffic"
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");
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"
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 }) %}
217 {% if (zone.dflags.dnat): %}
218 ct status dnat accept comment "!fw4: Accept port redirections"
220 {% fw4.includes('chain-append', `input_${zone.name}`) %}
221 jump {{ zone.input }}_from_{{ zone.name }}
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 }) %}
229 {% fw4.includes('chain-append', `output_${zone.name}`) %}
230 jump {{ zone.output }}_to_{{ zone.name }}
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 }) %}
238 {% if (zone.dflags.dnat): %}
239 ct status dnat accept comment "!fw4: Accept port forwards"
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: "
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 }) %}
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 }) %}
265 {% if (zone.dflags[verdict]): %}
266 chain {{ verdict }}_to_{{ zone.name }} {
267 {% for (let rule in zone.match_rules): %}
268 {% if (verdict == "accept" && (zone.masq || zone.masq6) && !zone.masq_allow_invalid): %}
269 {%+ include("zone-drop-invalid.uc", { fw4, zone, rule }) %}
271 {%+ include("zone-verdict.uc", { fw4, zone, rule, egress: true, verdict }) %}
284 type nat hook prerouting priority dstnat; policy accept;
285 {% fw4.includes('chain-prepend', 'dstnat') %}
286 {% for (let zone in fw4.zones()): %}
287 {% if (zone.dflags.dnat): %}
288 {% for (let rule in zone.match_rules): %}
289 {%+ include("zone-jump.uc", { fw4, zone, rule, direction: "dstnat" }) %}
293 {% fw4.includes('chain-append', 'dstnat') %}
297 type nat hook postrouting priority srcnat; policy accept;
298 {% fw4.includes('chain-prepend', 'srcnat') %}
299 {% for (let redirect in fw4.redirects("srcnat")): %}
300 {%+ include("redirect.uc", { fw4, redirect }) %}
302 {% for (let zone in fw4.zones()): %}
303 {% if (zone.dflags.snat): %}
304 {% for (let rule in zone.match_rules): %}
305 {%+ include("zone-jump.uc", { fw4, zone, rule, direction: "srcnat" }) %}
309 {% fw4.includes('chain-append', 'srcnat') %}
312 {% for (let zone in fw4.zones()): %}
313 {% if (zone.dflags.dnat): %}
314 chain dstnat_{{ zone.name }} {
315 {% fw4.includes('chain-prepend', `dstnat_${zone.name}`) %}
316 {% for (let redirect in fw4.redirects(`dstnat_${zone.name}`)): %}
317 {%+ include("redirect.uc", { fw4, redirect }) %}
319 {% fw4.includes('chain-append', `dstnat_${zone.name}`) %}
323 {% if (zone.dflags.snat): %}
324 chain srcnat_{{ zone.name }} {
325 {% fw4.includes('chain-prepend', `srcnat_${zone.name}`) %}
326 {% for (let redirect in fw4.redirects(`srcnat_${zone.name}`)): %}
327 {%+ include("redirect.uc", { fw4, redirect }) %}
329 {% if (zone.masq): %}
330 {% for (let saddrs in zone.masq4_src_subnets): %}
331 {% for (let daddrs in zone.masq4_dest_subnets): %}
332 {%+ include("zone-masq.uc", { fw4, zone, family: 4, saddrs, daddrs }) %}
336 {% if (zone.masq6): %}
337 {% for (let saddrs in zone.masq6_src_subnets): %}
338 {% for (let daddrs in zone.masq6_dest_subnets): %}
339 {%+ include("zone-masq.uc", { fw4, zone, family: 6, saddrs, daddrs }) %}
343 {% fw4.includes('chain-append', `srcnat_${zone.name}`) %}
350 # Raw rules (notrack)
353 chain raw_prerouting {
354 type filter hook prerouting priority raw; policy accept;
355 {% for (let zone in fw4.zones()): %}
356 {% if (zone.dflags["notrack"]): %}
357 {% for (let rule in zone.match_rules): %}
358 {% let devices_pos = fw4.filter_loopback_devs(rule.devices_pos, false); %}
359 {% let subnets_pos = fw4.filter_loopback_addrs(rule.subnets_pos, false); %}
360 {% if (rule.devices_neg || rule.subnets_neg || devices_pos || subnets_pos): %}
361 {%+ include("zone-jump.uc", { fw4, zone, rule: { ...rule, devices_pos, subnets_pos }, direction: "notrack" }) %}
366 {% fw4.includes('chain-append', 'raw_prerouting') %}
370 type filter hook output priority raw; policy accept;
371 {% fw4.includes('chain-prepend', 'raw_output') %}
372 {% for (let zone in fw4.zones()): %}
373 {% if (zone.dflags["notrack"]): %}
374 {% for (let rule in zone.match_rules): %}
375 {% let devices_pos = fw4.filter_loopback_devs(rule.devices_pos, true); %}
376 {% let subnets_pos = fw4.filter_loopback_addrs(rule.subnets_pos, true); %}
377 {% if (devices_pos || subnets_pos): %}
378 {%+ include("zone-jump.uc", { fw4, zone, rule: { ...rule, devices_pos, subnets_pos }, direction: "notrack" }) %}
383 {% fw4.includes('chain-append', 'raw_output') %}
386 {% for (let zone in fw4.zones()): %}
387 {% if (zone.dflags.notrack): %}
388 chain notrack_{{ zone.name }} {
389 {% for (let rule in fw4.rules(`notrack_${zone.name}`)): %}
390 {%+ include("rule.uc", { fw4, rule }) %}
401 chain mangle_prerouting {
402 type filter hook prerouting priority mangle; policy accept;
403 {% fw4.includes('chain-prepend', 'mangle_prerouting') %}
404 {% for (let rule in fw4.rules("mangle_prerouting")): %}
405 {%+ include("rule.uc", { fw4, rule }) %}
407 {% fw4.includes('chain-append', 'mangle_prerouting') %}
410 chain mangle_postrouting {
411 type filter hook postrouting priority mangle; policy accept;
412 {% fw4.includes('chain-prepend', 'mangle_postrouting') %}
413 {% for (let rule in fw4.rules("mangle_postrouting")): %}
414 {%+ include("rule.uc", { fw4, rule }) %}
416 {% fw4.includes('chain-append', 'mangle_postrouting') %}
420 type filter hook input priority mangle; policy accept;
421 {% fw4.includes('chain-prepend', 'mangle_input') %}
422 {% for (let rule in fw4.rules("mangle_input")): %}
423 {%+ include("rule.uc", { fw4, rule }) %}
425 {% fw4.includes('chain-append', 'mangle_input') %}
428 chain mangle_output {
429 type route hook output priority mangle; policy accept;
430 {% fw4.includes('chain-prepend', 'mangle_output') %}
431 {% for (let rule in fw4.rules("mangle_output")): %}
432 {%+ include("rule.uc", { fw4, rule }) %}
434 {% fw4.includes('chain-append', 'mangle_output') %}
437 chain mangle_forward {
438 type filter hook forward priority mangle; policy accept;
439 {% fw4.includes('chain-prepend', 'mangle_forward') %}
440 {% for (let rule in fw4.rules("mangle_forward")): %}
441 {%+ include("rule.uc", { fw4, rule }) %}
443 {% for (let zone in fw4.zones()): %}
444 {% if (zone.mtu_fix): %}
445 {% for (let rule in zone.match_rules): %}
446 {%+ include("zone-mssfix.uc", { fw4, zone, rule, egress: false }) %}
447 {%+ include("zone-mssfix.uc", { fw4, zone, rule, egress: true }) %}
451 {% fw4.includes('chain-append', 'mangle_forward') %}
453 {% fw4.includes('table-append') %}
455 {% fw4.includes('ruleset-append') %}