2 let flowtable_devices = fw4.resolve_offload_devices();
3 let available_helpers = filter(fw4.helpers(), h => h.available);
8 {% if (fw4.check_flowtable()): %}
9 delete flowtable inet fw4 ft
13 {% if (length(flowtable_devices) > 0): %}
19 hook ingress priority 0;
20 devices = {{ fw4.set(flowtable_devices, true) }};
21 {% if (fw4.default_option("flow_offloading_hw")): %}
27 {% if (length(available_helpers)): %}
29 # CT helper definitions
32 {% for (let helper in available_helpers): %}
33 {% for (let proto in helper.proto): %}
34 ct helper {{ helper.name }} {
35 type {{ fw4.quote(helper.name, true) }} protocol {{ proto.name }};
46 {% for (let set in fw4.ipsets()): %}
48 type {{ fw4.concat(set.types) }}
49 {% if (set.maxelem > 0): %}
50 size {{ set.maxelem }}
52 {% if (set.timeout >= 0): %}
53 timeout {{ set.timeout }}s
55 {% if (set.interval): %}
59 {% fw4.print_setentries(set) %}
68 {% for (let zone in fw4.zones()): %}
69 {% if (length(zone.match_devices)): %}
70 define {{ zone.name }}_devices = {{ fw4.set(zone.match_devices, true) }}
72 {% if (length(zone.match_subnets)): %}
73 define {{ zone.name }}_subnets = {{ fw4.set(zone.match_subnets, true) }}
81 include "/etc/nftables.d/*.nft"
89 type filter hook input priority filter; policy {{ fw4.input_policy(true) }};
91 iifname "lo" accept comment "!fw4: Accept traffic from loopback"
93 ct state established,related accept comment "!fw4: Allow inbound established and related flows"
94 {% if (fw4.default_option("drop_invalid")): %}
95 ct state invalid drop comment "!fw4: Drop flows with invalid conntrack state"
97 {% if (fw4.default_option("synflood_protect") && fw4.default_option("synflood_rate")): %}
98 tcp flags & (fin | syn | rst | ack) == syn jump syn_flood comment "!fw4: Rate limit TCP syn packets"
100 {% for (let rule in fw4.rules("input")): %}
101 {%+ include("rule.uc", { fw4, rule }) %}
103 {% for (let zone in fw4.zones()): for (let rule in zone.match_rules): %}
104 {%+ include("zone-jump.uc", { fw4, zone, rule, direction: "input" }) %}
106 {% if (fw4.input_policy() == "reject"): %}
112 type filter hook forward priority filter; policy {{ fw4.forward_policy(true) }};
114 {% if (length(flowtable_devices) > 0): %}
115 meta l4proto { tcp, udp } flow offload @ft;
117 ct state established,related accept comment "!fw4: Allow forwarded established and related flows"
118 {% if (fw4.default_option("drop_invalid")): %}
119 ct state invalid drop comment "!fw4: Drop flows with invalid conntrack state"
121 {% for (let rule in fw4.rules("forward")): %}
122 {%+ include("rule.uc", { fw4, rule }) %}
124 {% for (let zone in fw4.zones()): for (let rule in zone.match_rules): %}
125 {%+ include("zone-jump.uc", { fw4, zone, rule, direction: "forward" }) %}
127 {% if (fw4.forward_policy() == "reject"): %}
133 type filter hook output priority filter; policy {{ fw4.output_policy(true) }};
135 oifname "lo" accept comment "!fw4: Accept traffic towards loopback"
137 ct state established,related accept comment "!fw4: Allow outbound established and related flows"
138 {% if (fw4.default_option("drop_invalid")): %}
139 ct state invalid drop comment "!fw4: Drop flows with invalid conntrack state"
141 {% for (let rule in fw4.rules("output")): %}
142 {%+ include("rule.uc", { fw4, rule }) %}
144 {% for (let zone in fw4.zones()): %}
145 {% for (let rule in zone.match_rules): %}
146 {% if (zone.dflags.helper): %}
147 {% let devices_pos = fw4.filter_loopback_devs(rule.devices_pos, true); %}
148 {% let subnets_pos = fw4.filter_loopback_addrs(rule.subnets_pos, true); %}
149 {% if (devices_pos || subnets_pos): %}
150 {%+ if (rule.family): -%}
151 meta nfproto {{ fw4.nfproto(rule.family) }} {%+ endif -%}
152 {%+ include("zone-match.uc", { fw4, egress: false, rule: { ...rule, devices_pos, subnets_pos } }) -%}
153 jump helper_{{ zone.name }} comment "!fw4: {{ zone.name }} {{ fw4.nfproto(rule.family, true) }} CT helper assignment"
156 {%+ include("zone-jump.uc", { fw4, zone, rule, direction: "output" }) %}
159 {% if (fw4.output_policy() == "reject"): %}
165 type filter hook prerouting priority filter; policy accept;
166 {% for (let zone in fw4.zones()): %}
167 {% if (zone.dflags.helper): %}
168 {% for (let rule in zone.match_rules): %}
169 {% let devices_pos = fw4.filter_loopback_devs(rule.devices_pos, false); %}
170 {% let subnets_pos = fw4.filter_loopback_addrs(rule.subnets_pos, false); %}
171 {% if (rule.devices_neg || rule.subnets_neg || devices_pos || subnets_pos): %}
172 {%+ if (rule.family): -%}
173 meta nfproto {{ fw4.nfproto(rule.family) }} {%+ endif -%}
174 {%+ include("zone-match.uc", { fw4, egress: false, rule: { ...rule, devices_pos, subnets_pos } }) -%}
175 jump helper_{{ zone.name }} comment "!fw4: {{ zone.name }} {{ fw4.nfproto(rule.family, true) }} CT helper assignment"
182 chain handle_reject {
183 meta l4proto tcp reject with {{
184 (fw4.default_option("tcp_reject_code") != "tcp-reset")
185 ? `icmpx type ${fw4.default_option("tcp_reject_code")}`
187 }} comment "!fw4: Reject TCP traffic"
189 (fw4.default_option("any_reject_code") != "tcp-reset")
190 ? `icmpx type ${fw4.default_option("any_reject_code")}`
192 }} comment "!fw4: Reject any other traffic"
195 {% if (fw4.default_option("synflood_protect") && fw4.default_option("synflood_rate")):
196 let r = fw4.default_option("synflood_rate");
197 let b = fw4.default_option("synflood_burst");
200 limit rate {{ r.rate }}/{{ r.unit }}
201 {%- if (b): %} burst {{ b }} packets{% endif %} return comment "!fw4: Accept SYN packets below rate-limit"
202 drop comment "!fw4: Drop excess packets"
206 {% for (let zone in fw4.zones()): %}
207 chain input_{{ zone.name }} {
208 {% for (let rule in fw4.rules(`input_${zone.name}`)): %}
209 {%+ include("rule.uc", { fw4, rule }) %}
211 {% if (zone.dflags.dnat): %}
212 ct status dnat accept comment "!fw4: Accept port redirections"
214 jump {{ zone.input }}_from_{{ zone.name }}
217 chain output_{{ zone.name }} {
218 {% for (let rule in fw4.rules(`output_${zone.name}`)): %}
219 {%+ include("rule.uc", { fw4, rule }) %}
221 jump {{ zone.output }}_to_{{ zone.name }}
224 chain forward_{{ zone.name }} {
225 {% for (let rule in fw4.rules(`forward_${zone.name}`)): %}
226 {%+ include("rule.uc", { fw4, rule }) %}
228 {% if (zone.dflags.dnat): %}
229 ct status dnat accept comment "!fw4: Accept port forwards"
231 jump {{ zone.forward }}_to_{{ zone.name }}
234 {% if (zone.dflags.helper): %}
235 chain helper_{{ zone.name }} {
236 {% for (let rule in fw4.rules(`helper_${zone.name}`)): %}
237 {%+ include("rule.uc", { fw4, rule }) %}
242 {% for (let verdict in ["accept", "reject", "drop"]): %}
243 {% if (zone.sflags[verdict]): %}
244 chain {{ verdict }}_from_{{ zone.name }} {
245 {% for (let rule in zone.match_rules): %}
246 {%+ include("zone-verdict.uc", { fw4, zone, rule, egress: false, verdict }) %}
251 {% if (zone.dflags[verdict]): %}
252 chain {{ verdict }}_to_{{ zone.name }} {
253 {% for (let rule in zone.match_rules): %}
254 {%+ include("zone-verdict.uc", { fw4, zone, rule, egress: true, verdict }) %}
267 type nat hook prerouting priority dstnat; policy accept;
268 {% for (let zone in fw4.zones()): %}
269 {% if (zone.dflags.dnat): %}
270 {% for (let rule in zone.match_rules): %}
271 {%+ include("zone-jump.uc", { fw4, zone, rule, direction: "dstnat" }) %}
278 type nat hook postrouting priority srcnat; policy accept;
279 {% for (let redirect in fw4.redirects("srcnat")): %}
280 {%+ include("redirect.uc", { fw4, redirect }) %}
282 {% for (let zone in fw4.zones()): %}
283 {% if (zone.dflags.snat): %}
284 {% for (let rule in zone.match_rules): %}
285 {%+ include("zone-jump.uc", { fw4, zone, rule, direction: "srcnat" }) %}
291 {% for (let zone in fw4.zones()): %}
292 {% if (zone.dflags.dnat): %}
293 chain dstnat_{{ zone.name }} {
294 {% for (let redirect in fw4.redirects(`dstnat_${zone.name}`)): %}
295 {%+ include("redirect.uc", { fw4, redirect }) %}
300 {% if (zone.dflags.snat): %}
301 chain srcnat_{{ zone.name }} {
302 {% for (let redirect in fw4.redirects(`srcnat_${zone.name}`)): %}
303 {%+ include("redirect.uc", { fw4, redirect }) %}
305 {% if (zone.masq): %}
306 {% for (let saddrs in zone.masq4_src_subnets): %}
307 {% for (let daddrs in zone.masq4_dest_subnets): %}
308 {%+ include("zone-masq.uc", { fw4, zone, family: 4, saddrs, daddrs }) %}
312 {% if (zone.masq6): %}
313 {% for (let saddrs in zone.masq6_src_subnets): %}
314 {% for (let daddrs in zone.masq6_dest_subnets): %}
315 {%+ include("zone-masq.uc", { fw4, zone, family: 6, saddrs, daddrs }) %}
325 # Raw rules (notrack)
328 chain raw_prerouting {
329 type filter hook prerouting priority raw; policy accept;
330 {% for (let zone in fw4.zones()): %}
331 {% if (zone.dflags["notrack"]): %}
332 {% for (let rule in zone.match_rules): %}
333 {% let devices_pos = fw4.filter_loopback_devs(rule.devices_pos, false); %}
334 {% let subnets_pos = fw4.filter_loopback_addrs(rule.subnets_pos, false); %}
335 {% if (rule.devices_neg || rule.subnets_neg || devices_pos || subnets_pos): %}
336 {%+ if (rule.family): -%}
337 meta nfproto {{ fw4.nfproto(rule.family) }} {%+ endif -%}
338 {%+ include("zone-match.uc", { fw4, egress: false, rule: { ...rule, devices_pos, subnets_pos } }) -%}
339 jump notrack_{{ zone.name }} comment "!fw4: {{ zone.name }} {{ fw4.nfproto(rule.family, true) }} CT bypass"
347 type filter hook output priority raw; policy accept;
348 {% for (let zone in fw4.zones()): %}
349 {% if (zone.dflags["notrack"]): %}
350 {% for (let rule in zone.match_rules): %}
351 {% let devices_pos = fw4.filter_loopback_devs(rule.devices_pos, true); %}
352 {% let subnets_pos = fw4.filter_loopback_addrs(rule.subnets_pos, true); %}
353 {% if (devices_pos || subnets_pos): %}
354 {%+ if (rule.family): -%}
355 meta nfproto {{ fw4.nfproto(rule.family) }} {%+ endif -%}
356 {%+ include("zone-match.uc", { fw4, egress: false, rule: { ...rule, devices_pos, subnets_pos } }) -%}
357 jump notrack_{{ zone.name }} comment "!fw4: {{ zone.name }} {{ fw4.nfproto(rule.family, true) }} CT bypass"
364 {% for (let zone in fw4.zones()): %}
365 {% if (zone.dflags.notrack): %}
366 chain notrack_{{ zone.name }} {
367 {% for (let rule in fw4.rules(`notrack_${zone.name}`)): %}
368 {%+ include("rule.uc", { fw4, rule }) %}
379 chain mangle_prerouting {
380 type filter hook prerouting priority mangle; policy accept;
381 {% for (let rule in fw4.rules("mangle_prerouting")): %}
382 {%+ include("rule.uc", { fw4, rule }) %}
386 chain mangle_postrouting {
387 type filter hook postrouting priority mangle; policy accept;
388 {% for (let rule in fw4.rules("mangle_postrouting")): %}
389 {%+ include("rule.uc", { fw4, rule }) %}
394 type filter hook input priority mangle; policy accept;
395 {% for (let rule in fw4.rules("mangle_input")): %}
396 {%+ include("rule.uc", { fw4, rule }) %}
400 chain mangle_output {
401 type route hook output priority mangle; policy accept;
402 {% for (let rule in fw4.rules("mangle_output")): %}
403 {%+ include("rule.uc", { fw4, rule }) %}
407 chain mangle_forward {
408 type filter hook forward priority mangle; policy accept;
409 {% for (let rule in fw4.rules("mangle_forward")): %}
410 {%+ include("rule.uc", { fw4, rule }) %}
412 {% for (let zone in fw4.zones()): %}
413 {% if (zone.mtu_fix): %}
414 {% for (let rule in zone.match_rules): %}
415 {%+ include("zone-mssfix.uc", { fw4, zone, rule, egress: false }) %}
416 {%+ include("zone-mssfix.uc", { fw4, zone, rule, egress: true }) %}