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
14 {% if (length(flowtable_devices) > 0): %}
20 hook ingress priority 0;
21 devices = {{ fw4.set(flowtable_devices, true) }};
22 {% if (fw4.default_option("flow_offloading_hw")): %}
29 {% if (length(available_helpers)): %}
31 # CT helper definitions
34 {% for (let helper in available_helpers): %}
35 {% for (let proto in helper.proto): %}
36 ct helper {{ helper.name }} {
37 type {{ fw4.quote(helper.name, true) }} protocol {{ proto.name }};
44 {% if (length(defined_ipsets)): %}
49 {% for (let set in defined_ipsets): %}
51 type {{ fw4.concat(set.types) }}
52 {% if (set.maxelem > 0): %}
53 size {{ set.maxelem }}
55 {% if (set.timeout >= 0): %}
56 timeout {{ set.timeout }}s
58 {% if (set.interval): %}
62 {% fw4.print_setentries(set) %}
72 {% for (let zone in fw4.zones()): %}
73 define {{ zone.name }}_devices = {{ fw4.set(zone.match_devices, true) }}
74 define {{ zone.name }}_subnets = {{ fw4.set(zone.match_subnets, true) }}
82 include "/etc/nftables.d/*.nft"
90 type filter hook input priority filter; policy {{ fw4.input_policy(true) }};
92 iifname "lo" accept comment "!fw4: Accept traffic from loopback"
94 ct state established,related accept comment "!fw4: Allow inbound established and related flows"
95 {% if (fw4.default_option("drop_invalid")): %}
96 ct state invalid drop comment "!fw4: Drop flows with invalid conntrack state"
98 {% if (fw4.default_option("synflood_protect") && fw4.default_option("synflood_rate")): %}
99 tcp flags & (fin | syn | rst | ack) == syn jump syn_flood comment "!fw4: Rate limit TCP syn packets"
101 {% for (let rule in fw4.rules("input")): %}
102 {%+ include("rule.uc", { fw4, rule }) %}
104 {% for (let zone in fw4.zones()): for (let rule in zone.match_rules): %}
105 {%+ include("zone-jump.uc", { fw4, zone, rule, direction: "input" }) %}
107 {% if (fw4.input_policy() == "reject"): %}
113 type filter hook forward priority filter; policy {{ fw4.forward_policy(true) }};
115 {% if (length(flowtable_devices) > 0): %}
116 meta l4proto { tcp, udp } flow offload @ft;
118 ct state established,related accept comment "!fw4: Allow forwarded established and related flows"
119 {% if (fw4.default_option("drop_invalid")): %}
120 ct state invalid drop comment "!fw4: Drop flows with invalid conntrack state"
122 {% for (let rule in fw4.rules("forward")): %}
123 {%+ include("rule.uc", { fw4, rule }) %}
125 {% for (let zone in fw4.zones()): for (let rule in zone.match_rules): %}
126 {%+ include("zone-jump.uc", { fw4, zone, rule, direction: "forward" }) %}
128 {% if (fw4.forward_policy() == "reject"): %}
134 type filter hook output priority filter; policy {{ fw4.output_policy(true) }};
136 oifname "lo" accept comment "!fw4: Accept traffic towards loopback"
138 ct state established,related accept comment "!fw4: Allow outbound established and related flows"
139 {% if (fw4.default_option("drop_invalid")): %}
140 ct state invalid drop comment "!fw4: Drop flows with invalid conntrack state"
142 {% for (let rule in fw4.rules("output")): %}
143 {%+ include("rule.uc", { fw4, rule }) %}
145 {% for (let zone in fw4.zones()): %}
146 {% for (let rule in zone.match_rules): %}
147 {% if (zone.dflags.helper): %}
148 {% let devices_pos = fw4.filter_loopback_devs(rule.devices_pos, true); %}
149 {% let subnets_pos = fw4.filter_loopback_addrs(rule.subnets_pos, true); %}
150 {% if (devices_pos || subnets_pos): %}
151 {%+ include("zone-jump.uc", { fw4, zone, rule: { ...rule, devices_pos, subnets_pos }, direction: "helper" }) %}
154 {%+ include("zone-jump.uc", { fw4, zone, rule, direction: "output" }) %}
157 {% if (fw4.output_policy() == "reject"): %}
163 type filter hook prerouting priority filter; policy accept;
164 {% for (let zone in fw4.zones()): %}
165 {% if (zone.dflags.helper): %}
166 {% for (let rule in zone.match_rules): %}
167 {% let devices_pos = fw4.filter_loopback_devs(rule.devices_pos, false); %}
168 {% let subnets_pos = fw4.filter_loopback_addrs(rule.subnets_pos, false); %}
169 {% if (rule.devices_neg || rule.subnets_neg || devices_pos || subnets_pos): %}
170 {%+ include("zone-jump.uc", { fw4, zone, rule: { ...rule, devices_pos, subnets_pos }, direction: "helper" }) %}
177 chain handle_reject {
178 meta l4proto tcp reject with {{
179 (fw4.default_option("tcp_reject_code") != "tcp-reset")
180 ? `icmpx type ${fw4.default_option("tcp_reject_code")}`
182 }} comment "!fw4: Reject TCP traffic"
184 (fw4.default_option("any_reject_code") != "tcp-reset")
185 ? `icmpx type ${fw4.default_option("any_reject_code")}`
187 }} comment "!fw4: Reject any other traffic"
190 {% if (fw4.default_option("synflood_protect") && fw4.default_option("synflood_rate")):
191 let r = fw4.default_option("synflood_rate");
192 let b = fw4.default_option("synflood_burst");
195 limit rate {{ r.rate }}/{{ r.unit }}
196 {%- if (b): %} burst {{ b }} packets{% endif %} return comment "!fw4: Accept SYN packets below rate-limit"
197 drop comment "!fw4: Drop excess packets"
201 {% for (let zone in fw4.zones()): %}
202 chain input_{{ zone.name }} {
203 {% for (let rule in fw4.rules(`input_${zone.name}`)): %}
204 {%+ include("rule.uc", { fw4, rule }) %}
206 {% if (zone.dflags.dnat): %}
207 ct status dnat accept comment "!fw4: Accept port redirections"
209 jump {{ zone.input }}_from_{{ zone.name }}
212 chain output_{{ zone.name }} {
213 {% for (let rule in fw4.rules(`output_${zone.name}`)): %}
214 {%+ include("rule.uc", { fw4, rule }) %}
216 jump {{ zone.output }}_to_{{ zone.name }}
219 chain forward_{{ zone.name }} {
220 {% for (let rule in fw4.rules(`forward_${zone.name}`)): %}
221 {%+ include("rule.uc", { fw4, rule }) %}
223 {% if (zone.dflags.dnat): %}
224 ct status dnat accept comment "!fw4: Accept port forwards"
226 jump {{ zone.forward }}_to_{{ zone.name }}
229 {% if (zone.dflags.helper): %}
230 chain helper_{{ zone.name }} {
231 {% for (let rule in fw4.rules(`helper_${zone.name}`)): %}
232 {%+ include("rule.uc", { fw4, rule }) %}
237 {% for (let verdict in ["accept", "reject", "drop"]): %}
238 {% if (zone.sflags[verdict]): %}
239 chain {{ verdict }}_from_{{ zone.name }} {
240 {% for (let rule in zone.match_rules): %}
241 {%+ include("zone-verdict.uc", { fw4, zone, rule, egress: false, verdict }) %}
246 {% if (zone.dflags[verdict]): %}
247 chain {{ verdict }}_to_{{ zone.name }} {
248 {% for (let rule in zone.match_rules): %}
249 {%+ include("zone-verdict.uc", { fw4, zone, rule, egress: true, verdict }) %}
262 type nat hook prerouting priority dstnat; policy accept;
263 {% for (let zone in fw4.zones()): %}
264 {% if (zone.dflags.dnat): %}
265 {% for (let rule in zone.match_rules): %}
266 {%+ include("zone-jump.uc", { fw4, zone, rule, direction: "dstnat" }) %}
273 type nat hook postrouting priority srcnat; policy accept;
274 {% for (let redirect in fw4.redirects("srcnat")): %}
275 {%+ include("redirect.uc", { fw4, redirect }) %}
277 {% for (let zone in fw4.zones()): %}
278 {% if (zone.dflags.snat): %}
279 {% for (let rule in zone.match_rules): %}
280 {%+ include("zone-jump.uc", { fw4, zone, rule, direction: "srcnat" }) %}
286 {% for (let zone in fw4.zones()): %}
287 {% if (zone.dflags.dnat): %}
288 chain dstnat_{{ zone.name }} {
289 {% for (let redirect in fw4.redirects(`dstnat_${zone.name}`)): %}
290 {%+ include("redirect.uc", { fw4, redirect }) %}
295 {% if (zone.dflags.snat): %}
296 chain srcnat_{{ zone.name }} {
297 {% for (let redirect in fw4.redirects(`srcnat_${zone.name}`)): %}
298 {%+ include("redirect.uc", { fw4, redirect }) %}
300 {% if (zone.masq): %}
301 {% for (let saddrs in zone.masq4_src_subnets): %}
302 {% for (let daddrs in zone.masq4_dest_subnets): %}
303 {%+ include("zone-masq.uc", { fw4, zone, family: 4, saddrs, daddrs }) %}
307 {% if (zone.masq6): %}
308 {% for (let saddrs in zone.masq6_src_subnets): %}
309 {% for (let daddrs in zone.masq6_dest_subnets): %}
310 {%+ include("zone-masq.uc", { fw4, zone, family: 6, saddrs, daddrs }) %}
320 # Raw rules (notrack)
323 chain raw_prerouting {
324 type filter hook prerouting priority raw; policy accept;
325 {% for (let zone in fw4.zones()): %}
326 {% if (zone.dflags["notrack"]): %}
327 {% for (let rule in zone.match_rules): %}
328 {% let devices_pos = fw4.filter_loopback_devs(rule.devices_pos, false); %}
329 {% let subnets_pos = fw4.filter_loopback_addrs(rule.subnets_pos, false); %}
330 {% if (rule.devices_neg || rule.subnets_neg || devices_pos || subnets_pos): %}
331 {%+ include("zone-jump.uc", { fw4, zone, rule: { ...rule, devices_pos, subnets_pos }, direction: "notrack" }) %}
339 type filter hook output priority raw; policy accept;
340 {% for (let zone in fw4.zones()): %}
341 {% if (zone.dflags["notrack"]): %}
342 {% for (let rule in zone.match_rules): %}
343 {% let devices_pos = fw4.filter_loopback_devs(rule.devices_pos, true); %}
344 {% let subnets_pos = fw4.filter_loopback_addrs(rule.subnets_pos, true); %}
345 {% if (devices_pos || subnets_pos): %}
346 {%+ include("zone-jump.uc", { fw4, zone, rule: { ...rule, devices_pos, subnets_pos }, direction: "notrack" }) %}
353 {% for (let zone in fw4.zones()): %}
354 {% if (zone.dflags.notrack): %}
355 chain notrack_{{ zone.name }} {
356 {% for (let rule in fw4.rules(`notrack_${zone.name}`)): %}
357 {%+ include("rule.uc", { fw4, rule }) %}
368 chain mangle_prerouting {
369 type filter hook prerouting priority mangle; policy accept;
370 {% for (let rule in fw4.rules("mangle_prerouting")): %}
371 {%+ include("rule.uc", { fw4, rule }) %}
375 chain mangle_postrouting {
376 type filter hook postrouting priority mangle; policy accept;
377 {% for (let rule in fw4.rules("mangle_postrouting")): %}
378 {%+ include("rule.uc", { fw4, rule }) %}
383 type filter hook input priority mangle; policy accept;
384 {% for (let rule in fw4.rules("mangle_input")): %}
385 {%+ include("rule.uc", { fw4, rule }) %}
389 chain mangle_output {
390 type route hook output priority mangle; policy accept;
391 {% for (let rule in fw4.rules("mangle_output")): %}
392 {%+ include("rule.uc", { fw4, rule }) %}
396 chain mangle_forward {
397 type filter hook forward priority mangle; policy accept;
398 {% for (let rule in fw4.rules("mangle_forward")): %}
399 {%+ include("rule.uc", { fw4, rule }) %}
401 {% for (let zone in fw4.zones()): %}
402 {% if (zone.mtu_fix): %}
403 {% for (let rule in zone.match_rules): %}
404 {%+ include("zone-mssfix.uc", { fw4, zone, rule, egress: false }) %}
405 {%+ include("zone-mssfix.uc", { fw4, zone, rule, egress: true }) %}