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) }};
24 {% if (fw4.default_option("flow_offloading_hw")): %}
31 {% if (length(available_helpers)): %}
33 # CT helper definitions
36 {% for (let helper in available_helpers): %}
37 {% for (let proto in helper.proto): %}
38 ct helper {{ helper.name }} {
39 type {{ fw4.quote(helper.name, true) }} protocol {{ proto.name }};
46 {% if (length(defined_ipsets)): %}
51 {% for (let set in defined_ipsets): %}
53 {% if (set.comment): %}
54 comment {{ fw4.quote(set.comment, true) }}
56 type {{ fw4.concat(set.types) }}
57 {% if (set.maxelem > 0): %}
58 size {{ set.maxelem }}
60 {% if (set.timeout > 0): %}
61 timeout {{ set.timeout }}s
63 {% if (set.interval): %}
67 flags {{ join(',', set.flags) }}
69 {% fw4.print_setentries(set) %}
79 {% for (let zone in fw4.zones()): %}
80 define {{ replace(zone.name, /^[0-9]/, '_$&') }}_devices = {{ fw4.set(zone.match_devices, true) }}
81 define {{ replace(zone.name, /^[0-9]/, '_$&') }}_subnets = {{ fw4.set(zone.match_subnets, true) }}
89 include "/etc/nftables.d/*.nft"
90 {% fw4.includes('table-prepend') %}
98 type filter hook input priority filter; policy {{ fw4.input_policy(true) }};
100 iifname "lo" accept comment "!fw4: Accept traffic from loopback"
102 {% fw4.includes('chain-prepend', 'input') %}
103 ct state established,related accept comment "!fw4: Allow inbound established and related flows"
104 {% if (fw4.default_option("drop_invalid")): %}
105 ct state invalid drop comment "!fw4: Drop flows with invalid conntrack state"
107 {% if (fw4.default_option("synflood_protect") && fw4.default_option("synflood_rate")): %}
108 tcp flags & (fin | syn | rst | ack) == syn jump syn_flood comment "!fw4: Rate limit TCP syn packets"
110 {% for (let rule in fw4.rules("input")): %}
111 {%+ include("rule.uc", { fw4, rule }) %}
113 {% for (let zone in fw4.zones()): for (let rule in zone.match_rules): %}
114 {%+ include("zone-jump.uc", { fw4, zone, rule, direction: "input" }) %}
116 {% if (fw4.input_policy() == "reject"): %}
119 {% fw4.includes('chain-append', 'input') %}
123 type filter hook forward priority filter; policy {{ fw4.forward_policy(true) }};
125 {% if (length(flowtable_devices) > 0): %}
126 meta l4proto { tcp, udp } flow offload @ft;
128 {% fw4.includes('chain-prepend', 'forward') %}
129 ct state established,related accept comment "!fw4: Allow forwarded established and related flows"
130 {% if (fw4.default_option("drop_invalid")): %}
131 ct state invalid drop comment "!fw4: Drop flows with invalid conntrack state"
133 {% for (let rule in fw4.rules("forward")): %}
134 {%+ include("rule.uc", { fw4, rule }) %}
136 {% for (let zone in fw4.zones()): for (let rule in zone.match_rules): %}
137 {%+ include("zone-jump.uc", { fw4, zone, rule, direction: "forward" }) %}
139 {% fw4.includes('chain-append', 'forward') %}
140 {% if (fw4.forward_policy() == "reject"): %}
146 type filter hook output priority filter; policy {{ fw4.output_policy(true) }};
148 oifname "lo" accept comment "!fw4: Accept traffic towards loopback"
150 {% fw4.includes('chain-prepend', 'output') %}
151 ct state established,related accept comment "!fw4: Allow outbound established and related flows"
152 {% if (fw4.default_option("drop_invalid")): %}
153 ct state invalid drop comment "!fw4: Drop flows with invalid conntrack state"
155 {% for (let rule in fw4.rules("output")): %}
156 {%+ include("rule.uc", { fw4, rule }) %}
158 {% for (let zone in fw4.zones()): %}
159 {% for (let rule in zone.match_rules): %}
160 {% if (zone.dflags.helper): %}
161 {% let devices_pos = fw4.filter_loopback_devs(rule.devices_pos, true); %}
162 {% let subnets_pos = fw4.filter_loopback_addrs(rule.subnets_pos, true); %}
163 {% if (devices_pos || subnets_pos): %}
164 {%+ include("zone-jump.uc", { fw4, zone, rule: { ...rule, devices_pos, subnets_pos }, direction: "helper" }) %}
167 {%+ include("zone-jump.uc", { fw4, zone, rule, direction: "output" }) %}
170 {% fw4.includes('chain-append', 'output') %}
171 {% if (fw4.output_policy() == "reject"): %}
177 type filter hook prerouting priority filter; policy accept;
178 {% for (let zone in fw4.zones()): %}
179 {% if (zone.dflags.helper): %}
180 {% for (let rule in zone.match_rules): %}
181 {% let devices_pos = fw4.filter_loopback_devs(rule.devices_pos, false); %}
182 {% let subnets_pos = fw4.filter_loopback_addrs(rule.subnets_pos, false); %}
183 {% if (rule.devices_neg || rule.subnets_neg || devices_pos || subnets_pos): %}
184 {%+ include("zone-jump.uc", { fw4, zone, rule: { ...rule, devices_pos, subnets_pos }, direction: "helper" }) %}
191 chain handle_reject {
192 meta l4proto tcp reject with {{
193 (fw4.default_option("tcp_reject_code") != "tcp-reset")
194 ? `icmpx type ${fw4.default_option("tcp_reject_code")}`
196 }} comment "!fw4: Reject TCP traffic"
198 (fw4.default_option("any_reject_code") != "tcp-reset")
199 ? `icmpx type ${fw4.default_option("any_reject_code")}`
201 }} comment "!fw4: Reject any other traffic"
204 {% if (fw4.default_option("synflood_protect") && fw4.default_option("synflood_rate")):
205 let r = fw4.default_option("synflood_rate");
206 let b = fw4.default_option("synflood_burst");
209 limit rate {{ r.rate }}/{{ r.unit }}
210 {%- if (b): %} burst {{ b }} packets{% endif %} return comment "!fw4: Accept SYN packets below rate-limit"
211 drop comment "!fw4: Drop excess packets"
215 {% for (let zone in fw4.zones()): %}
216 chain input_{{ zone.name }} {
217 {% fw4.includes('chain-prepend', `input_${zone.name}`) %}
218 {% for (let rule in fw4.rules(`input_${zone.name}`)): %}
219 {%+ include("rule.uc", { fw4, rule }) %}
221 {% if (zone.dflags.dnat): %}
222 ct status dnat accept comment "!fw4: Accept port redirections"
224 {% fw4.includes('chain-append', `input_${zone.name}`) %}
225 jump {{ zone.input }}_from_{{ zone.name }}
228 chain output_{{ zone.name }} {
229 {% fw4.includes('chain-prepend', `output_${zone.name}`) %}
230 {% for (let rule in fw4.rules(`output_${zone.name}`)): %}
231 {%+ include("rule.uc", { fw4, rule }) %}
233 {% fw4.includes('chain-append', `output_${zone.name}`) %}
234 jump {{ zone.output }}_to_{{ zone.name }}
237 chain forward_{{ zone.name }} {
238 {% fw4.includes('chain-prepend', `forward_${zone.name}`) %}
239 {% for (let rule in fw4.rules(`forward_${zone.name}`)): %}
240 {%+ include("rule.uc", { fw4, rule }) %}
242 {% if (zone.dflags.dnat): %}
243 ct status dnat accept comment "!fw4: Accept port forwards"
245 {% fw4.includes('chain-append', `forward_${zone.name}`) %}
246 jump {{ zone.forward }}_to_{{ zone.name }}
247 {% if (fw4.forward_policy() != "accept" && (zone.log & 1)): %}
248 log prefix "{{ fw4.forward_policy() }} {{ zone.name }} forward: "
252 {% if (zone.dflags.helper): %}
253 chain helper_{{ zone.name }} {
254 {% for (let rule in fw4.rules(`helper_${zone.name}`)): %}
255 {%+ include("rule.uc", { fw4, rule }) %}
260 {% for (let verdict in ["accept", "reject", "drop"]): %}
261 {% if (zone.sflags[verdict]): %}
262 chain {{ verdict }}_from_{{ zone.name }} {
263 {% for (let rule in zone.match_rules): %}
264 {%+ include("zone-verdict.uc", { fw4, zone, rule, egress: false, verdict }) %}
269 {% if (zone.dflags[verdict]): %}
270 chain {{ verdict }}_to_{{ zone.name }} {
271 {% for (let rule in zone.match_rules): %}
272 {% if (verdict == "accept" && (zone.masq || zone.masq6) && !zone.masq_allow_invalid): %}
273 {%+ include("zone-drop-invalid.uc", { fw4, zone, rule }) %}
275 {%+ include("zone-verdict.uc", { fw4, zone, rule, egress: true, verdict }) %}
288 type nat hook prerouting priority dstnat; policy accept;
289 {% fw4.includes('chain-prepend', 'dstnat') %}
290 {% for (let zone in fw4.zones()): %}
291 {% if (zone.dflags.dnat): %}
292 {% for (let rule in zone.match_rules): %}
293 {%+ include("zone-jump.uc", { fw4, zone, rule, direction: "dstnat" }) %}
297 {% fw4.includes('chain-append', 'dstnat') %}
301 type nat hook postrouting priority srcnat; policy accept;
302 {% fw4.includes('chain-prepend', 'srcnat') %}
303 {% for (let redirect in fw4.redirects("srcnat")): %}
304 {%+ include("redirect.uc", { fw4, redirect }) %}
306 {% for (let zone in fw4.zones()): %}
307 {% if (zone.dflags.snat): %}
308 {% for (let rule in zone.match_rules): %}
309 {%+ include("zone-jump.uc", { fw4, zone, rule, direction: "srcnat" }) %}
313 {% fw4.includes('chain-append', 'srcnat') %}
316 {% for (let zone in fw4.zones()): %}
317 {% if (zone.dflags.dnat): %}
318 chain dstnat_{{ zone.name }} {
319 {% fw4.includes('chain-prepend', `dstnat_${zone.name}`) %}
320 {% for (let redirect in fw4.redirects(`dstnat_${zone.name}`)): %}
321 {%+ include("redirect.uc", { fw4, redirect }) %}
323 {% fw4.includes('chain-append', `dstnat_${zone.name}`) %}
327 {% if (zone.dflags.snat): %}
328 chain srcnat_{{ zone.name }} {
329 {% fw4.includes('chain-prepend', `srcnat_${zone.name}`) %}
330 {% for (let redirect in fw4.redirects(`srcnat_${zone.name}`)): %}
331 {%+ include("redirect.uc", { fw4, redirect }) %}
333 {% if (zone.masq): %}
334 {% for (let saddrs in zone.masq4_src_subnets): %}
335 {% for (let daddrs in zone.masq4_dest_subnets): %}
336 {%+ include("zone-masq.uc", { fw4, zone, family: 4, saddrs, daddrs }) %}
340 {% if (zone.masq6): %}
341 {% for (let saddrs in zone.masq6_src_subnets): %}
342 {% for (let daddrs in zone.masq6_dest_subnets): %}
343 {%+ include("zone-masq.uc", { fw4, zone, family: 6, saddrs, daddrs }) %}
347 {% fw4.includes('chain-append', `srcnat_${zone.name}`) %}
354 # Raw rules (notrack)
357 chain raw_prerouting {
358 type filter hook prerouting priority raw; policy accept;
359 {% for (let zone in fw4.zones()): %}
360 {% if (zone.dflags["notrack"]): %}
361 {% for (let rule in zone.match_rules): %}
362 {% let devices_pos = fw4.filter_loopback_devs(rule.devices_pos, false); %}
363 {% let subnets_pos = fw4.filter_loopback_addrs(rule.subnets_pos, false); %}
364 {% if (rule.devices_neg || rule.subnets_neg || devices_pos || subnets_pos): %}
365 {%+ include("zone-jump.uc", { fw4, zone, rule: { ...rule, devices_pos, subnets_pos }, direction: "notrack" }) %}
370 {% fw4.includes('chain-append', 'raw_prerouting') %}
374 type filter hook output priority raw; policy accept;
375 {% fw4.includes('chain-prepend', 'raw_output') %}
376 {% for (let zone in fw4.zones()): %}
377 {% if (zone.dflags["notrack"]): %}
378 {% for (let rule in zone.match_rules): %}
379 {% let devices_pos = fw4.filter_loopback_devs(rule.devices_pos, true); %}
380 {% let subnets_pos = fw4.filter_loopback_addrs(rule.subnets_pos, true); %}
381 {% if (devices_pos || subnets_pos): %}
382 {%+ include("zone-jump.uc", { fw4, zone, rule: { ...rule, devices_pos, subnets_pos }, direction: "notrack" }) %}
387 {% fw4.includes('chain-append', 'raw_output') %}
390 {% for (let zone in fw4.zones()): %}
391 {% if (zone.dflags.notrack): %}
392 chain notrack_{{ zone.name }} {
393 {% for (let rule in fw4.rules(`notrack_${zone.name}`)): %}
394 {%+ include("rule.uc", { fw4, rule }) %}
405 chain mangle_prerouting {
406 type filter hook prerouting priority mangle; policy accept;
407 {% fw4.includes('chain-prepend', 'mangle_prerouting') %}
408 {% for (let rule in fw4.rules("mangle_prerouting")): %}
409 {%+ include("rule.uc", { fw4, rule }) %}
411 {% fw4.includes('chain-append', 'mangle_prerouting') %}
414 chain mangle_postrouting {
415 type filter hook postrouting priority mangle; policy accept;
416 {% fw4.includes('chain-prepend', 'mangle_postrouting') %}
417 {% for (let rule in fw4.rules("mangle_postrouting")): %}
418 {%+ include("rule.uc", { fw4, rule }) %}
420 {% fw4.includes('chain-append', 'mangle_postrouting') %}
424 type filter hook input priority mangle; policy accept;
425 {% fw4.includes('chain-prepend', 'mangle_input') %}
426 {% for (let rule in fw4.rules("mangle_input")): %}
427 {%+ include("rule.uc", { fw4, rule }) %}
429 {% fw4.includes('chain-append', 'mangle_input') %}
432 chain mangle_output {
433 type route hook output priority mangle; policy accept;
434 {% fw4.includes('chain-prepend', 'mangle_output') %}
435 {% for (let rule in fw4.rules("mangle_output")): %}
436 {%+ include("rule.uc", { fw4, rule }) %}
438 {% fw4.includes('chain-append', 'mangle_output') %}
441 chain mangle_forward {
442 type filter hook forward priority mangle; policy accept;
443 {% fw4.includes('chain-prepend', 'mangle_forward') %}
444 {% for (let rule in fw4.rules("mangle_forward")): %}
445 {%+ include("rule.uc", { fw4, rule }) %}
447 {% for (let zone in fw4.zones()): %}
448 {% if (zone.mtu_fix): %}
449 {% for (let rule in zone.match_rules): %}
450 {%+ include("zone-mssfix.uc", { fw4, zone, rule, egress: false }) %}
451 {%+ include("zone-mssfix.uc", { fw4, zone, rule, egress: true }) %}
455 {% fw4.includes('chain-append', 'mangle_forward') %}
457 {% fw4.includes('table-append') %}
459 {% fw4.includes('ruleset-append') %}