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 {{ zone.name }}_devices = {{ fw4.set(zone.match_devices, true) }}
77 define {{ zone.name }}_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 }}
245 {% if (zone.dflags.helper): %}
246 chain helper_{{ zone.name }} {
247 {% for (let rule in fw4.rules(`helper_${zone.name}`)): %}
248 {%+ include("rule.uc", { fw4, rule }) %}
253 {% for (let verdict in ["accept", "reject", "drop"]): %}
254 {% if (zone.sflags[verdict]): %}
255 chain {{ verdict }}_from_{{ zone.name }} {
256 {% for (let rule in zone.match_rules): %}
257 {%+ include("zone-verdict.uc", { fw4, zone, rule, egress: false, verdict }) %}
262 {% if (zone.dflags[verdict]): %}
263 chain {{ verdict }}_to_{{ zone.name }} {
264 {% for (let rule in zone.match_rules): %}
265 {%+ include("zone-verdict.uc", { fw4, zone, rule, egress: true, verdict }) %}
278 type nat hook prerouting priority dstnat; policy accept;
279 {% fw4.includes('chain-prepend', 'dstnat') %}
280 {% for (let zone in fw4.zones()): %}
281 {% if (zone.dflags.dnat): %}
282 {% for (let rule in zone.match_rules): %}
283 {%+ include("zone-jump.uc", { fw4, zone, rule, direction: "dstnat" }) %}
287 {% fw4.includes('chain-append', 'dstnat') %}
291 type nat hook postrouting priority srcnat; policy accept;
292 {% fw4.includes('chain-prepend', 'srcnat') %}
293 {% for (let redirect in fw4.redirects("srcnat")): %}
294 {%+ include("redirect.uc", { fw4, redirect }) %}
296 {% for (let zone in fw4.zones()): %}
297 {% if (zone.dflags.snat): %}
298 {% for (let rule in zone.match_rules): %}
299 {%+ include("zone-jump.uc", { fw4, zone, rule, direction: "srcnat" }) %}
303 {% fw4.includes('chain-append', 'srcnat') %}
306 {% for (let zone in fw4.zones()): %}
307 {% if (zone.dflags.dnat): %}
308 chain dstnat_{{ zone.name }} {
309 {% fw4.includes('chain-prepend', `dstnat_${zone.name}`) %}
310 {% for (let redirect in fw4.redirects(`dstnat_${zone.name}`)): %}
311 {%+ include("redirect.uc", { fw4, redirect }) %}
313 {% fw4.includes('chain-append', `dstnat_${zone.name}`) %}
317 {% if (zone.dflags.snat): %}
318 chain srcnat_{{ zone.name }} {
319 {% fw4.includes('chain-prepend', `srcnat_${zone.name}`) %}
320 {% for (let redirect in fw4.redirects(`srcnat_${zone.name}`)): %}
321 {%+ include("redirect.uc", { fw4, redirect }) %}
323 {% if (zone.masq): %}
324 {% for (let saddrs in zone.masq4_src_subnets): %}
325 {% for (let daddrs in zone.masq4_dest_subnets): %}
326 {%+ include("zone-masq.uc", { fw4, zone, family: 4, saddrs, daddrs }) %}
330 {% if (zone.masq6): %}
331 {% for (let saddrs in zone.masq6_src_subnets): %}
332 {% for (let daddrs in zone.masq6_dest_subnets): %}
333 {%+ include("zone-masq.uc", { fw4, zone, family: 6, saddrs, daddrs }) %}
337 {% fw4.includes('chain-append', `srcnat_${zone.name}`) %}
344 # Raw rules (notrack)
347 chain raw_prerouting {
348 type filter hook prerouting priority raw; policy accept;
349 {% for (let zone in fw4.zones()): %}
350 {% if (zone.dflags["notrack"]): %}
351 {% for (let rule in zone.match_rules): %}
352 {% let devices_pos = fw4.filter_loopback_devs(rule.devices_pos, false); %}
353 {% let subnets_pos = fw4.filter_loopback_addrs(rule.subnets_pos, false); %}
354 {% if (rule.devices_neg || rule.subnets_neg || devices_pos || subnets_pos): %}
355 {%+ include("zone-jump.uc", { fw4, zone, rule: { ...rule, devices_pos, subnets_pos }, direction: "notrack" }) %}
360 {% fw4.includes('chain-append', 'raw_prerouting') %}
364 type filter hook output priority raw; policy accept;
365 {% fw4.includes('chain-prepend', 'raw_output') %}
366 {% for (let zone in fw4.zones()): %}
367 {% if (zone.dflags["notrack"]): %}
368 {% for (let rule in zone.match_rules): %}
369 {% let devices_pos = fw4.filter_loopback_devs(rule.devices_pos, true); %}
370 {% let subnets_pos = fw4.filter_loopback_addrs(rule.subnets_pos, true); %}
371 {% if (devices_pos || subnets_pos): %}
372 {%+ include("zone-jump.uc", { fw4, zone, rule: { ...rule, devices_pos, subnets_pos }, direction: "notrack" }) %}
377 {% fw4.includes('chain-append', 'raw_output') %}
380 {% for (let zone in fw4.zones()): %}
381 {% if (zone.dflags.notrack): %}
382 chain notrack_{{ zone.name }} {
383 {% for (let rule in fw4.rules(`notrack_${zone.name}`)): %}
384 {%+ include("rule.uc", { fw4, rule }) %}
395 chain mangle_prerouting {
396 type filter hook prerouting priority mangle; policy accept;
397 {% fw4.includes('chain-prepend', 'mangle_prerouting') %}
398 {% for (let rule in fw4.rules("mangle_prerouting")): %}
399 {%+ include("rule.uc", { fw4, rule }) %}
401 {% fw4.includes('chain-append', 'mangle_prerouting') %}
404 chain mangle_postrouting {
405 type filter hook postrouting priority mangle; policy accept;
406 {% fw4.includes('chain-prepend', 'mangle_postrouting') %}
407 {% for (let rule in fw4.rules("mangle_postrouting")): %}
408 {%+ include("rule.uc", { fw4, rule }) %}
410 {% fw4.includes('chain-append', 'mangle_postrouting') %}
414 type filter hook input priority mangle; policy accept;
415 {% fw4.includes('chain-prepend', 'mangle_input') %}
416 {% for (let rule in fw4.rules("mangle_input")): %}
417 {%+ include("rule.uc", { fw4, rule }) %}
419 {% fw4.includes('chain-append', 'mangle_input') %}
422 chain mangle_output {
423 type route hook output priority mangle; policy accept;
424 {% fw4.includes('chain-prepend', 'mangle_output') %}
425 {% for (let rule in fw4.rules("mangle_output")): %}
426 {%+ include("rule.uc", { fw4, rule }) %}
428 {% fw4.includes('chain-append', 'mangle_output') %}
431 chain mangle_forward {
432 type filter hook forward priority mangle; policy accept;
433 {% fw4.includes('chain-prepend', 'mangle_forward') %}
434 {% for (let rule in fw4.rules("mangle_forward")): %}
435 {%+ include("rule.uc", { fw4, rule }) %}
437 {% for (let zone in fw4.zones()): %}
438 {% if (zone.mtu_fix): %}
439 {% for (let rule in zone.match_rules): %}
440 {%+ include("zone-mssfix.uc", { fw4, zone, rule, egress: false }) %}
441 {%+ include("zone-mssfix.uc", { fw4, zone, rule, egress: true }) %}
445 {% fw4.includes('chain-append', 'mangle_forward') %}
447 {% fw4.includes('table-append') %}
449 {% fw4.includes('ruleset-append') %}