5 {%- if (fw4.default_option("flow_offloading") && length(devices) > 0): %}
11 hook ingress priority 0;
12 devices = {{ fw4.set(devices, true) }};
13 {% if (fw4.default_option("flow_offloading_hw")): %}
24 {% for (let set in fw4.ipsets()): %}
26 type {{ fw4.concat(set.types) }}
27 {% if (set.maxelem > 0): %}
28 size {{ set.maxelem }}
30 {% if (set.timeout >= 0): %}
31 timeout {{ set.timeout }}s
33 {% if (set.interval): %}
36 {% fw4.print_setentries(set) %}
45 {% for (let zone in fw4.zones()): %}
46 {% if (length(zone.match_devices)): %}
47 define {{ zone.name }}_devices = {{ fw4.set(zone.match_devices, true) }}
49 {% if (length(zone.match_subnets)): %}
50 define {{ zone.name }}_subnets = {{ fw4.set(zone.match_subnets, true) }}
58 include "/etc/nftables.d/*.nft"
66 type filter hook input priority filter; policy {{ fw4.input_policy(true) }};
68 iifname "lo" accept comment "!fw4: Accept traffic from loopback"
70 ct state established,related accept comment "!fw4: Allow inbound established and related flows"
71 {% if (fw4.default_option("drop_invalid")): %}
72 ct state invalid drop comment "!fw4: Drop flows with invalid conntrack state"
74 {% if (fw4.default_option("synflood_protect")): %}
75 tcp flags & (fin | syn | rst | ack) == syn jump syn_flood comment "!fw4: Rate limit TCP syn packets"
77 {% for (let rule in fw4.rules("input")): %}
78 {%+ include("rule.uc", { fw4, rule }) %}
80 {% for (let zone in fw4.zones()): for (let rule in zone.match_rules): %}
81 {%+ include("zone-match.uc", { fw4, zone, rule, direction: "input" }) %}
83 {% if (fw4.input_policy() == "reject"): %}
89 type filter hook forward priority filter; policy {{ fw4.forward_policy(true) }};
91 {% if (fw4.default_option("flow_offloading") && length(devices) > 0): %}
92 ip protocol { tcp , udp } flow offload @ft;
93 ip6 nexthdr { tcp , udp } flow offload @ft;
95 ct state established,related accept comment "!fw4: Allow forwarded established and related flows"
96 {% if (fw4.default_option("drop_invalid")): %}
97 ct state invalid drop comment "!fw4: Drop flows with invalid conntrack state"
99 {% for (let rule in fw4.rules("forward")): %}
100 {%+ include("rule.uc", { fw4, rule }) %}
102 {% for (let zone in fw4.zones()): for (let rule in zone.match_rules): %}
103 {%+ include("zone-match.uc", { fw4, zone, rule, direction: "forward" }) %}
105 {% if (fw4.forward_policy() == "reject"): %}
111 type filter hook output priority filter; policy {{ fw4.output_policy(true) }};
113 oifname "lo" accept comment "!fw4: Accept traffic towards loopback"
115 ct state established,related accept comment "!fw4: Allow outbound established and related flows"
116 {% if (fw4.default_option("drop_invalid")): %}
117 ct state invalid drop comment "!fw4: Drop flows with invalid conntrack state"
119 {% for (let rule in fw4.rules("output")): %}
120 {%+ include("rule.uc", { fw4, rule }) %}
122 {% for (let zone in fw4.zones()): for (let rule in zone.match_rules): %}
123 {%+ include("zone-match.uc", { fw4, zone, rule, direction: "output" }) %}
125 {% if (fw4.output_policy() == "reject"): %}
130 chain handle_reject {
131 meta l4proto tcp reject with {{
132 (fw4.default_option("tcp_reject_code") != "tcp-reset")
133 ? "icmpx type " + fw4.default_option("tcp_reject_code")
135 }} comment "!fw4: Reject TCP traffic"
137 (fw4.default_option("any_reject_code") != "tcp-reset")
138 ? "icmpx type " + fw4.default_option("any_reject_code")
140 }} comment "!fw4: Reject any other traffic"
143 {% if (fw4.default_option("synflood_protect")):
144 let r = fw4.default_option("synflood_rate");
145 let b = fw4.default_option("synflood_burst");
148 tcp flags & (fin | syn | rst | ack) == syn
149 {%- if (r): %} limit rate {{ r.rate }}/{{ r.unit }}{% endif %}
150 {%- if (b): %} burst {{ b }} packets{% endif %} return comment "!fw4: Accept SYN packets below rate-limit"
151 drop comment "!fw4: Drop excess packets"
155 {% for (let zone in fw4.zones()): %}
156 chain input_{{ zone.name }} {
157 {% for (let rule in fw4.rules("input_"+zone.name)): %}
158 {%+ include("rule.uc", { fw4, rule }) %}
160 {% if (zone.dflags.dnat): %}
161 ct status dnat accept comment "!fw4: Accept port redirections"
163 jump {{ zone.input }}_from_{{ zone.name }}
166 chain output_{{ zone.name }} {
167 {% for (let rule in fw4.rules("output_"+zone.name)): %}
168 {%+ include("rule.uc", { fw4, rule }) %}
170 jump {{ zone.output }}_to_{{ zone.name }}
173 chain forward_{{ zone.name }} {
174 {% for (let rule in fw4.rules("forward_"+zone.name)): %}
175 {%+ include("rule.uc", { fw4, rule }) %}
177 {% if (zone.dflags.dnat): %}
178 ct status dnat accept comment "!fw4: Accept port forwards"
180 jump {{ zone.forward }}_to_{{ zone.name }}
183 {% for (let verdict in ["accept", "reject", "drop"]): %}
184 {% if (zone.sflags[verdict]): %}
185 chain {{ verdict }}_from_{{ zone.name }} {
186 {% for (let rule in zone.match_rules): %}
187 {%+ include("zone-verdict.uc", { fw4, zone, rule, egress: false, verdict }) %}
192 {% if (zone.dflags[verdict]): %}
193 chain {{ verdict }}_to_{{ zone.name }} {
194 {% for (let rule in zone.match_rules): %}
195 {%+ include("zone-verdict.uc", { fw4, zone, rule, egress: true, verdict }) %}
208 type nat hook prerouting priority dstnat; policy accept;
209 {% for (let zone in fw4.zones()): %}
210 {% if (zone.dflags.dnat): %}
211 {% for (let rule in zone.match_rules): %}
212 {%+ include("zone-match.uc", { fw4, zone, rule, direction: "dstnat" }) %}
219 type nat hook postrouting priority srcnat; policy accept;
220 {% for (let redirect in fw4.redirects("srcnat")): %}
221 {%+ include("redirect.uc", { fw4, redirect }) %}
223 {% for (let zone in fw4.zones()): %}
224 {% if (zone.dflags.snat): %}
225 {% for (let rule in zone.match_rules): %}
226 {%+ include("zone-match.uc", { fw4, zone, rule, direction: "srcnat" }) %}
232 {% for (let zone in fw4.zones()): %}
233 {% if (zone.dflags.dnat): %}
234 chain dstnat_{{ zone.name }} {
235 {% for (let redirect in fw4.redirects("dstnat_"+zone.name)): %}
236 {%+ include("redirect.uc", { fw4, redirect }) %}
241 {% if (zone.dflags.snat): %}
242 chain srcnat_{{ zone.name }} {
243 {% for (let redirect in fw4.redirects("srcnat_"+zone.name)): %}
244 {%+ include("redirect.uc", { fw4, redirect }) %}
246 {% if (zone.masq): %}
247 meta nfproto ipv4 {%+ if (zone.masq4_src_pos): -%}
248 ip saddr {{ fw4.set(zone.masq4_src_pos) }} {%+ endif -%}
249 {%+ if (zone.masq4_src_neg): -%}
250 ip saddr != {{ fw4.set(zone.masq4_src_neg) }} {%+ endif -%}
251 {%+ if (zone.masq4_dest_pos): -%}
252 ip daddr {{ fw4.set(zone.masq4_dest_pos) }} {%+ endif -%}
253 {%+ if (zone.masq4_dest_neg): -%}
254 ip daddr != {{ fw4.set(zone.masq4_dest_neg) }} {%+ endif -%}
255 masquerade comment "!fw4: Masquerade IPv4 {{ zone.name }} traffic"
257 {% if (zone.masq6): %}
258 meta nfproto ipv6 {%+ if (zone.masq6_src_pos): -%}
259 ip6 saddr {{ fw4.set(zone.masq6_src_pos) }} {%+ endif -%}
260 {%+ if (zone.masq6_src_neg): -%}
261 ip6 saddr != {{ fw4.set(zone.masq6_src_neg) }} {%+ endif -%}
262 {%+ if (zone.masq6_dest_pos): -%}
263 ip6 daddr {{ fw4.set(zone.masq6_dest_pos) }} {%+ endif -%}
264 {%+ if (zone.masq6_dest_neg): -%}
265 ip6 daddr != {{ fw4.set(zone.masq6_dest_neg) }} {%+ endif -%}
266 masquerade comment "!fw4: Masquerade IPv6 {{ zone.name }} traffic"
274 # Raw rules (notrack & helper)
277 chain raw_prerouting {
278 type filter hook prerouting priority raw; policy accept;
279 {% for (let target in ["helper", "notrack"]): %}
280 {% for (let zone in fw4.zones()): %}
281 {% if (zone.dflags[target]): %}
282 {% for (let rule in zone.match_rules): %}
283 {% let devs = fw4.filter_loopback_devs(rule.devices_pos, false); %}
284 {% let nets = fw4.filter_loopback_addrs(rule.subnets_pos, false); %}
285 {% if (rule.devices_neg || rule.subnets_neg || length(devs) || length(nets)): %}
286 {%+ if (rule.family): -%}
287 meta nfproto {{ fw4.nfproto(rule.family) }} {%+ endif -%}
288 {%+ if (length(devs)): -%}
289 iifname {{ fw4.set(devs) }} {%+ endif -%}
290 {%+ if (rule.devices_neg): -%}
291 iifname != {{ fw4.set(rule.devices_neg) }} {%+ endif -%}
292 {%+ if (length(nets)): -%}
293 {{ fw4.ipproto(rule.family) }} saddr {{ fw4.set(nets) }} {%+ endif -%}
294 {%+ if (rule.subnets_neg): -%}
295 {{ fw4.ipproto(rule.family) }} saddr != {{ fw4.set(rule.subnets_neg) }} {%+ endif -%}
296 jump {{ target }}_{{ zone.name }} comment "!fw4: {{ zone.name }} {{ fw4.nfproto(rule.family, true) }} {{
297 (target == "helper") ? "CT helper assignment" : "CT bypass"
307 type filter hook output priority raw; policy accept;
308 {% for (let target in ["helper", "notrack"]): %}
309 {% for (let zone in fw4.zones()): %}
310 {% if (zone.dflags[target]): %}
311 {% for (let rule in zone.match_rules): %}
312 {% let devs = fw4.filter_loopback_devs(rule.devices_pos, true); %}
313 {% let nets = fw4.filter_loopback_addrs(rule.subnets_pos, true); %}
314 {% if (length(devs) || length(nets)): %}
315 {%+ if (rule.family): -%}
316 meta nfproto {{ fw4.nfproto(rule.family) }} {%+ endif -%}
317 {%+ if (length(devs)): -%}
318 iifname {{ fw4.set(devs) }} {%+ endif -%}
319 {%+ if (length(nets)): -%}
320 {{ fw4.ipproto(rule.family) }} saddr {{ fw4.set(nets) }} {%+ endif -%}
321 jump {{ target }}_{{ zone.name }} comment "!fw4: {{ zone.name }} {{ fw4.nfproto(rule.family, true) }} {{
322 (target == "helper") ? "CT helper assignment" : "CT bypass"
331 {% for (let helper in fw4.helpers()): %}
332 {% if (helper.available): %}
333 {% for (let proto in helper.proto): %}
334 ct helper {{ helper.name }} {
335 type {{ fw4.quote(helper.name, true) }} protocol {{ proto.name }};
341 {% for (let target in ["helper", "notrack"]): %}
342 {% for (let zone in fw4.zones()): %}
343 {% if (zone.dflags[target]): %}
344 chain {{ target }}_{{ zone.name }} {
345 {% for (let rule in fw4.rules(target+"_"+zone.name)): %}
346 {%+ include("rule.uc", { fw4, rule }) %}
358 chain mangle_prerouting {
359 type filter hook prerouting priority mangle; policy accept;
360 {% for (let rule in fw4.rules("mangle_prerouting")): %}
361 {%+ include("rule.uc", { fw4, rule }) %}
365 chain mangle_output {
366 type filter hook output priority mangle; policy accept;
367 {% for (let rule in fw4.rules("mangle_output")): %}
368 {%+ include("rule.uc", { fw4, rule }) %}
372 chain mangle_forward {
373 type filter hook forward priority mangle; policy accept;
374 {% for (let zone in fw4.zones()): %}
375 {% if (zone.mtu_fix): %}
376 {% for (let rule in zone.match_rules): %}
377 {%+ include("zone-mssfix.uc", { fw4, zone, rule, egress: false }) %}
378 {%+ include("zone-mssfix.uc", { fw4, zone, rule, egress: true }) %}