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 {% fw4.print_setentries(set) %}
73 {% for (let zone in fw4.zones()): %}
74 define {{ zone.name }}_devices = {{ fw4.set(zone.match_devices, true) }}
75 define {{ zone.name }}_subnets = {{ fw4.set(zone.match_subnets, true) }}
83 include "/etc/nftables.d/*.nft"
84 {% fw4.includes('table-prepend') %}
92 type filter hook input priority filter; policy {{ fw4.input_policy(true) }};
94 iifname "lo" accept comment "!fw4: Accept traffic from loopback"
96 {% fw4.includes('chain-prepend', 'input') %}
97 ct state established,related accept comment "!fw4: Allow inbound established and related flows"
98 {% if (fw4.default_option("drop_invalid")): %}
99 ct state invalid drop comment "!fw4: Drop flows with invalid conntrack state"
101 {% if (fw4.default_option("synflood_protect") && fw4.default_option("synflood_rate")): %}
102 tcp flags & (fin | syn | rst | ack) == syn jump syn_flood comment "!fw4: Rate limit TCP syn packets"
104 {% for (let rule in fw4.rules("input")): %}
105 {%+ include("rule.uc", { fw4, rule }) %}
107 {% for (let zone in fw4.zones()): for (let rule in zone.match_rules): %}
108 {%+ include("zone-jump.uc", { fw4, zone, rule, direction: "input" }) %}
110 {% if (fw4.input_policy() == "reject"): %}
113 {% fw4.includes('chain-append', 'input') %}
117 type filter hook forward priority filter; policy {{ fw4.forward_policy(true) }};
119 {% if (length(flowtable_devices) > 0): %}
120 meta l4proto { tcp, udp } flow offload @ft;
122 {% fw4.includes('chain-prepend', 'forward') %}
123 ct state established,related accept comment "!fw4: Allow forwarded established and related flows"
124 {% if (fw4.default_option("drop_invalid")): %}
125 ct state invalid drop comment "!fw4: Drop flows with invalid conntrack state"
127 {% for (let rule in fw4.rules("forward")): %}
128 {%+ include("rule.uc", { fw4, rule }) %}
130 {% for (let zone in fw4.zones()): for (let rule in zone.match_rules): %}
131 {%+ include("zone-jump.uc", { fw4, zone, rule, direction: "forward" }) %}
133 {% fw4.includes('chain-append', 'forward') %}
134 {% if (fw4.forward_policy() == "reject"): %}
140 type filter hook output priority filter; policy {{ fw4.output_policy(true) }};
142 oifname "lo" accept comment "!fw4: Accept traffic towards loopback"
144 {% fw4.includes('chain-prepend', 'output') %}
145 ct state established,related accept comment "!fw4: Allow outbound established and related flows"
146 {% if (fw4.default_option("drop_invalid")): %}
147 ct state invalid drop comment "!fw4: Drop flows with invalid conntrack state"
149 {% for (let rule in fw4.rules("output")): %}
150 {%+ include("rule.uc", { fw4, rule }) %}
152 {% for (let zone in fw4.zones()): %}
153 {% for (let rule in zone.match_rules): %}
154 {% if (zone.dflags.helper): %}
155 {% let devices_pos = fw4.filter_loopback_devs(rule.devices_pos, true); %}
156 {% let subnets_pos = fw4.filter_loopback_addrs(rule.subnets_pos, true); %}
157 {% if (devices_pos || subnets_pos): %}
158 {%+ include("zone-jump.uc", { fw4, zone, rule: { ...rule, devices_pos, subnets_pos }, direction: "helper" }) %}
161 {%+ include("zone-jump.uc", { fw4, zone, rule, direction: "output" }) %}
164 {% fw4.includes('chain-append', 'output') %}
165 {% if (fw4.output_policy() == "reject"): %}
171 type filter hook prerouting priority filter; policy accept;
172 {% for (let zone in fw4.zones()): %}
173 {% if (zone.dflags.helper): %}
174 {% for (let rule in zone.match_rules): %}
175 {% let devices_pos = fw4.filter_loopback_devs(rule.devices_pos, false); %}
176 {% let subnets_pos = fw4.filter_loopback_addrs(rule.subnets_pos, false); %}
177 {% if (rule.devices_neg || rule.subnets_neg || devices_pos || subnets_pos): %}
178 {%+ include("zone-jump.uc", { fw4, zone, rule: { ...rule, devices_pos, subnets_pos }, direction: "helper" }) %}
185 chain handle_reject {
186 meta l4proto tcp reject with {{
187 (fw4.default_option("tcp_reject_code") != "tcp-reset")
188 ? `icmpx type ${fw4.default_option("tcp_reject_code")}`
190 }} comment "!fw4: Reject TCP traffic"
192 (fw4.default_option("any_reject_code") != "tcp-reset")
193 ? `icmpx type ${fw4.default_option("any_reject_code")}`
195 }} comment "!fw4: Reject any other traffic"
198 {% if (fw4.default_option("synflood_protect") && fw4.default_option("synflood_rate")):
199 let r = fw4.default_option("synflood_rate");
200 let b = fw4.default_option("synflood_burst");
203 limit rate {{ r.rate }}/{{ r.unit }}
204 {%- if (b): %} burst {{ b }} packets{% endif %} return comment "!fw4: Accept SYN packets below rate-limit"
205 drop comment "!fw4: Drop excess packets"
209 {% for (let zone in fw4.zones()): %}
210 chain input_{{ zone.name }} {
211 {% fw4.includes('chain-prepend', `input_${zone.name}`) %}
212 {% for (let rule in fw4.rules(`input_${zone.name}`)): %}
213 {%+ include("rule.uc", { fw4, rule }) %}
215 {% if (zone.dflags.dnat): %}
216 ct status dnat accept comment "!fw4: Accept port redirections"
218 {% fw4.includes('chain-append', `input_${zone.name}`) %}
219 jump {{ zone.input }}_from_{{ zone.name }}
222 chain output_{{ zone.name }} {
223 {% fw4.includes('chain-prepend', `output_${zone.name}`) %}
224 {% for (let rule in fw4.rules(`output_${zone.name}`)): %}
225 {%+ include("rule.uc", { fw4, rule }) %}
227 {% fw4.includes('chain-append', `output_${zone.name}`) %}
228 jump {{ zone.output }}_to_{{ zone.name }}
231 chain forward_{{ zone.name }} {
232 {% fw4.includes('chain-prepend', `forward_${zone.name}`) %}
233 {% for (let rule in fw4.rules(`forward_${zone.name}`)): %}
234 {%+ include("rule.uc", { fw4, rule }) %}
236 {% if (zone.dflags.dnat): %}
237 ct status dnat accept comment "!fw4: Accept port forwards"
239 {% fw4.includes('chain-append', `forward_${zone.name}`) %}
240 jump {{ zone.forward }}_to_{{ zone.name }}
243 {% if (zone.dflags.helper): %}
244 chain helper_{{ zone.name }} {
245 {% for (let rule in fw4.rules(`helper_${zone.name}`)): %}
246 {%+ include("rule.uc", { fw4, rule }) %}
251 {% for (let verdict in ["accept", "reject", "drop"]): %}
252 {% if (zone.sflags[verdict]): %}
253 chain {{ verdict }}_from_{{ zone.name }} {
254 {% for (let rule in zone.match_rules): %}
255 {%+ include("zone-verdict.uc", { fw4, zone, rule, egress: false, verdict }) %}
260 {% if (zone.dflags[verdict]): %}
261 chain {{ verdict }}_to_{{ zone.name }} {
262 {% for (let rule in zone.match_rules): %}
263 {%+ include("zone-verdict.uc", { fw4, zone, rule, egress: true, verdict }) %}
276 type nat hook prerouting priority dstnat; policy accept;
277 {% fw4.includes('chain-prepend', 'dstnat') %}
278 {% for (let zone in fw4.zones()): %}
279 {% if (zone.dflags.dnat): %}
280 {% for (let rule in zone.match_rules): %}
281 {%+ include("zone-jump.uc", { fw4, zone, rule, direction: "dstnat" }) %}
285 {% fw4.includes('chain-append', 'dstnat') %}
289 type nat hook postrouting priority srcnat; policy accept;
290 {% fw4.includes('chain-prepend', 'srcnat') %}
291 {% for (let redirect in fw4.redirects("srcnat")): %}
292 {%+ include("redirect.uc", { fw4, redirect }) %}
294 {% for (let zone in fw4.zones()): %}
295 {% if (zone.dflags.snat): %}
296 {% for (let rule in zone.match_rules): %}
297 {%+ include("zone-jump.uc", { fw4, zone, rule, direction: "srcnat" }) %}
301 {% fw4.includes('chain-append', 'srcnat') %}
304 {% for (let zone in fw4.zones()): %}
305 {% if (zone.dflags.dnat): %}
306 chain dstnat_{{ zone.name }} {
307 {% fw4.includes('chain-prepend', `dstnat_${zone.name}`) %}
308 {% for (let redirect in fw4.redirects(`dstnat_${zone.name}`)): %}
309 {%+ include("redirect.uc", { fw4, redirect }) %}
311 {% fw4.includes('chain-append', `dstnat_${zone.name}`) %}
315 {% if (zone.dflags.snat): %}
316 chain srcnat_{{ zone.name }} {
317 {% fw4.includes('chain-prepend', `srcnat_${zone.name}`) %}
318 {% for (let redirect in fw4.redirects(`srcnat_${zone.name}`)): %}
319 {%+ include("redirect.uc", { fw4, redirect }) %}
321 {% if (zone.masq): %}
322 {% for (let saddrs in zone.masq4_src_subnets): %}
323 {% for (let daddrs in zone.masq4_dest_subnets): %}
324 {%+ include("zone-masq.uc", { fw4, zone, family: 4, saddrs, daddrs }) %}
328 {% if (zone.masq6): %}
329 {% for (let saddrs in zone.masq6_src_subnets): %}
330 {% for (let daddrs in zone.masq6_dest_subnets): %}
331 {%+ include("zone-masq.uc", { fw4, zone, family: 6, saddrs, daddrs }) %}
335 {% fw4.includes('chain-append', `srcnat_${zone.name}`) %}
342 # Raw rules (notrack)
345 chain raw_prerouting {
346 type filter hook prerouting priority raw; policy accept;
347 {% for (let zone in fw4.zones()): %}
348 {% if (zone.dflags["notrack"]): %}
349 {% for (let rule in zone.match_rules): %}
350 {% let devices_pos = fw4.filter_loopback_devs(rule.devices_pos, false); %}
351 {% let subnets_pos = fw4.filter_loopback_addrs(rule.subnets_pos, false); %}
352 {% if (rule.devices_neg || rule.subnets_neg || devices_pos || subnets_pos): %}
353 {%+ include("zone-jump.uc", { fw4, zone, rule: { ...rule, devices_pos, subnets_pos }, direction: "notrack" }) %}
358 {% fw4.includes('chain-append', 'raw_prerouting') %}
362 type filter hook output priority raw; policy accept;
363 {% fw4.includes('chain-prepend', 'raw_output') %}
364 {% for (let zone in fw4.zones()): %}
365 {% if (zone.dflags["notrack"]): %}
366 {% for (let rule in zone.match_rules): %}
367 {% let devices_pos = fw4.filter_loopback_devs(rule.devices_pos, true); %}
368 {% let subnets_pos = fw4.filter_loopback_addrs(rule.subnets_pos, true); %}
369 {% if (devices_pos || subnets_pos): %}
370 {%+ include("zone-jump.uc", { fw4, zone, rule: { ...rule, devices_pos, subnets_pos }, direction: "notrack" }) %}
375 {% fw4.includes('chain-append', 'raw_output') %}
378 {% for (let zone in fw4.zones()): %}
379 {% if (zone.dflags.notrack): %}
380 chain notrack_{{ zone.name }} {
381 {% for (let rule in fw4.rules(`notrack_${zone.name}`)): %}
382 {%+ include("rule.uc", { fw4, rule }) %}
393 chain mangle_prerouting {
394 type filter hook prerouting priority mangle; policy accept;
395 {% fw4.includes('chain-prepend', 'mangle_prerouting') %}
396 {% for (let rule in fw4.rules("mangle_prerouting")): %}
397 {%+ include("rule.uc", { fw4, rule }) %}
399 {% fw4.includes('chain-append', 'mangle_prerouting') %}
402 chain mangle_postrouting {
403 type filter hook postrouting priority mangle; policy accept;
404 {% fw4.includes('chain-prepend', 'mangle_postrouting') %}
405 {% for (let rule in fw4.rules("mangle_postrouting")): %}
406 {%+ include("rule.uc", { fw4, rule }) %}
408 {% fw4.includes('chain-append', 'mangle_postrouting') %}
412 type filter hook input priority mangle; policy accept;
413 {% fw4.includes('chain-prepend', 'mangle_input') %}
414 {% for (let rule in fw4.rules("mangle_input")): %}
415 {%+ include("rule.uc", { fw4, rule }) %}
417 {% fw4.includes('chain-append', 'mangle_input') %}
420 chain mangle_output {
421 type route hook output priority mangle; policy accept;
422 {% fw4.includes('chain-prepend', 'mangle_output') %}
423 {% for (let rule in fw4.rules("mangle_output")): %}
424 {%+ include("rule.uc", { fw4, rule }) %}
426 {% fw4.includes('chain-append', 'mangle_output') %}
429 chain mangle_forward {
430 type filter hook forward priority mangle; policy accept;
431 {% fw4.includes('chain-prepend', 'mangle_forward') %}
432 {% for (let rule in fw4.rules("mangle_forward")): %}
433 {%+ include("rule.uc", { fw4, rule }) %}
435 {% for (let zone in fw4.zones()): %}
436 {% if (zone.mtu_fix): %}
437 {% for (let rule in zone.match_rules): %}
438 {%+ include("zone-mssfix.uc", { fw4, zone, rule, egress: false }) %}
439 {%+ include("zone-mssfix.uc", { fw4, zone, rule, egress: true }) %}
443 {% fw4.includes('chain-append', 'mangle_forward') %}
445 {% fw4.includes('table-append') %}
447 {% fw4.includes('ruleset-append') %}