1 {% let flowtable_devices = fw4.resolve_offload_devices(); -%}
5 {% if (fw4.check_flowtable()): %}
6 delete flowtable inet fw4 ft
10 {% if (length(flowtable_devices) > 0): %}
16 hook ingress priority 0;
17 devices = {{ fw4.set(flowtable_devices, true) }};
18 {% if (fw4.default_option("flow_offloading_hw")): %}
28 {% for (let set in fw4.ipsets()): %}
30 type {{ fw4.concat(set.types) }}
31 {% if (set.maxelem > 0): %}
32 size {{ set.maxelem }}
34 {% if (set.timeout >= 0): %}
35 timeout {{ set.timeout }}s
37 {% if (set.interval): %}
40 {% fw4.print_setentries(set) %}
49 {% for (let zone in fw4.zones()): %}
50 {% if (length(zone.match_devices)): %}
51 define {{ zone.name }}_devices = {{ fw4.set(zone.match_devices, true) }}
53 {% if (length(zone.match_subnets)): %}
54 define {{ zone.name }}_subnets = {{ fw4.set(zone.match_subnets, true) }}
62 include "/etc/nftables.d/*.nft"
70 type filter hook input priority filter; policy {{ fw4.input_policy(true) }};
72 iifname "lo" accept comment "!fw4: Accept traffic from loopback"
74 ct state established,related accept comment "!fw4: Allow inbound established and related flows"
75 {% if (fw4.default_option("drop_invalid")): %}
76 ct state invalid drop comment "!fw4: Drop flows with invalid conntrack state"
78 {% if (fw4.default_option("synflood_protect") && fw4.default_option("synflood_rate")): %}
79 tcp flags & (fin | syn | rst | ack) == syn jump syn_flood comment "!fw4: Rate limit TCP syn packets"
81 {% for (let rule in fw4.rules("input")): %}
82 {%+ include("rule.uc", { fw4, rule }) %}
84 {% for (let zone in fw4.zones()): for (let rule in zone.match_rules): %}
85 {%+ include("zone-jump.uc", { fw4, zone, rule, direction: "input" }) %}
87 {% if (fw4.input_policy() == "reject"): %}
93 type filter hook forward priority filter; policy {{ fw4.forward_policy(true) }};
95 {% if (length(flowtable_devices) > 0): %}
96 meta l4proto { tcp, udp } flow offload @ft;
98 ct state established,related accept comment "!fw4: Allow forwarded established and related flows"
99 {% if (fw4.default_option("drop_invalid")): %}
100 ct state invalid drop comment "!fw4: Drop flows with invalid conntrack state"
102 {% for (let rule in fw4.rules("forward")): %}
103 {%+ include("rule.uc", { fw4, rule }) %}
105 {% for (let zone in fw4.zones()): for (let rule in zone.match_rules): %}
106 {%+ include("zone-jump.uc", { fw4, zone, rule, direction: "forward" }) %}
108 {% if (fw4.forward_policy() == "reject"): %}
114 type filter hook output priority filter; policy {{ fw4.output_policy(true) }};
116 oifname "lo" accept comment "!fw4: Accept traffic towards loopback"
118 ct state established,related accept comment "!fw4: Allow outbound 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("output")): %}
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: "output" }) %}
128 {% if (fw4.output_policy() == "reject"): %}
133 chain handle_reject {
134 meta l4proto tcp reject with {{
135 (fw4.default_option("tcp_reject_code") != "tcp-reset")
136 ? "icmpx type " + fw4.default_option("tcp_reject_code")
138 }} comment "!fw4: Reject TCP traffic"
140 (fw4.default_option("any_reject_code") != "tcp-reset")
141 ? "icmpx type " + fw4.default_option("any_reject_code")
143 }} comment "!fw4: Reject any other traffic"
146 {% if (fw4.default_option("synflood_protect") && fw4.default_option("synflood_rate")):
147 let r = fw4.default_option("synflood_rate");
148 let b = fw4.default_option("synflood_burst");
151 limit rate {{ r.rate }}/{{ r.unit }}
152 {%- if (b): %} burst {{ b }} packets{% endif %} return comment "!fw4: Accept SYN packets below rate-limit"
153 drop comment "!fw4: Drop excess packets"
157 {% for (let zone in fw4.zones()): %}
158 chain input_{{ zone.name }} {
159 {% for (let rule in fw4.rules("input_"+zone.name)): %}
160 {%+ include("rule.uc", { fw4, rule }) %}
162 {% if (zone.dflags.dnat): %}
163 ct status dnat accept comment "!fw4: Accept port redirections"
165 jump {{ zone.input }}_from_{{ zone.name }}
168 chain output_{{ zone.name }} {
169 {% for (let rule in fw4.rules("output_"+zone.name)): %}
170 {%+ include("rule.uc", { fw4, rule }) %}
172 jump {{ zone.output }}_to_{{ zone.name }}
175 chain forward_{{ zone.name }} {
176 {% for (let rule in fw4.rules("forward_"+zone.name)): %}
177 {%+ include("rule.uc", { fw4, rule }) %}
179 {% if (zone.dflags.dnat): %}
180 ct status dnat accept comment "!fw4: Accept port forwards"
182 jump {{ zone.forward }}_to_{{ zone.name }}
185 {% for (let verdict in ["accept", "reject", "drop"]): %}
186 {% if (zone.sflags[verdict]): %}
187 chain {{ verdict }}_from_{{ zone.name }} {
188 {% for (let rule in zone.match_rules): %}
189 {%+ include("zone-verdict.uc", { fw4, zone, rule, egress: false, verdict }) %}
194 {% if (zone.dflags[verdict]): %}
195 chain {{ verdict }}_to_{{ zone.name }} {
196 {% for (let rule in zone.match_rules): %}
197 {%+ include("zone-verdict.uc", { fw4, zone, rule, egress: true, verdict }) %}
210 type nat hook prerouting priority dstnat; policy accept;
211 {% for (let zone in fw4.zones()): %}
212 {% if (zone.dflags.dnat): %}
213 {% for (let rule in zone.match_rules): %}
214 {%+ include("zone-jump.uc", { fw4, zone, rule, direction: "dstnat" }) %}
221 type nat hook postrouting priority srcnat; policy accept;
222 {% for (let redirect in fw4.redirects("srcnat")): %}
223 {%+ include("redirect.uc", { fw4, redirect }) %}
225 {% for (let zone in fw4.zones()): %}
226 {% if (zone.dflags.snat): %}
227 {% for (let rule in zone.match_rules): %}
228 {%+ include("zone-jump.uc", { fw4, zone, rule, direction: "srcnat" }) %}
234 {% for (let zone in fw4.zones()): %}
235 {% if (zone.dflags.dnat): %}
236 chain dstnat_{{ zone.name }} {
237 {% for (let redirect in fw4.redirects("dstnat_"+zone.name)): %}
238 {%+ include("redirect.uc", { fw4, redirect }) %}
243 {% if (zone.dflags.snat): %}
244 chain srcnat_{{ zone.name }} {
245 {% for (let redirect in fw4.redirects("srcnat_"+zone.name)): %}
246 {%+ include("redirect.uc", { fw4, redirect }) %}
248 {% if (zone.masq): %}
249 {% for (let saddrs in zone.masq4_src_subnets): %}
250 {% for (let daddrs in zone.masq4_dest_subnets): %}
251 {%+ include("zone-masq.uc", { fw4, zone, family: 4, saddrs, daddrs }) %}
255 {% if (zone.masq6): %}
256 {% for (let saddrs in zone.masq6_src_subnets): %}
257 {% for (let daddrs in zone.masq6_dest_subnets): %}
258 {%+ include("zone-masq.uc", { fw4, zone, family: 6, saddrs, daddrs }) %}
268 # Raw rules (notrack & helper)
271 chain raw_prerouting {
272 type filter hook prerouting priority raw; policy accept;
273 {% for (let target in ["helper", "notrack"]): %}
274 {% for (let zone in fw4.zones()): %}
275 {% if (zone.dflags[target]): %}
276 {% for (let rule in zone.match_rules): %}
277 {% let devices_pos = fw4.filter_loopback_devs(rule.devices_pos, false); %}
278 {% let subnets_pos = fw4.filter_loopback_addrs(rule.subnets_pos, false); %}
279 {% if (rule.devices_neg || rule.subnets_neg || devices_pos || subnets_pos): %}
280 {%+ if (rule.family): -%}
281 meta nfproto {{ fw4.nfproto(rule.family) }} {%+ endif -%}
282 {%+ include("zone-match.uc", { fw4, egress: false, rule: { ...rule, devices_pos, subnets_pos } }) -%}
283 jump {{ target }}_{{ zone.name }} comment "!fw4: {{ zone.name }} {{ fw4.nfproto(rule.family, true) }} {{
284 (target == "helper") ? "CT helper assignment" : "CT bypass"
294 type filter hook output priority raw; policy accept;
295 {% for (let target in ["helper", "notrack"]): %}
296 {% for (let zone in fw4.zones()): %}
297 {% if (zone.dflags[target]): %}
298 {% for (let rule in zone.match_rules): %}
299 {% let devices_pos = fw4.filter_loopback_devs(rule.devices_pos, true); %}
300 {% let subnets_pos = fw4.filter_loopback_addrs(rule.subnets_pos, true); %}
301 {% if (devices_pos || subnets_pos): %}
302 {%+ if (rule.family): -%}
303 meta nfproto {{ fw4.nfproto(rule.family) }} {%+ endif -%}
304 {%+ include("zone-match.uc", { fw4, egress: false, rule: { ...rule, devices_pos, subnets_pos } }) -%}
305 jump {{ target }}_{{ zone.name }} comment "!fw4: {{ zone.name }} {{ fw4.nfproto(rule.family, true) }} {{
306 (target == "helper") ? "CT helper assignment" : "CT bypass"
315 {% for (let helper in fw4.helpers()): %}
316 {% if (helper.available): %}
317 {% for (let proto in helper.proto): %}
318 ct helper {{ helper.name }} {
319 type {{ fw4.quote(helper.name, true) }} protocol {{ proto.name }};
325 {% for (let target in ["helper", "notrack"]): %}
326 {% for (let zone in fw4.zones()): %}
327 {% if (zone.dflags[target]): %}
328 chain {{ target }}_{{ zone.name }} {
329 {% for (let rule in fw4.rules(target+"_"+zone.name)): %}
330 {%+ include("rule.uc", { fw4, rule }) %}
342 chain mangle_prerouting {
343 type filter hook prerouting priority mangle; policy accept;
344 {% for (let rule in fw4.rules("mangle_prerouting")): %}
345 {%+ include("rule.uc", { fw4, rule }) %}
349 chain mangle_postrouting {
350 type filter hook postrouting priority mangle; policy accept;
351 {% for (let rule in fw4.rules("mangle_postrouting")): %}
352 {%+ include("rule.uc", { fw4, rule }) %}
357 type filter hook input priority mangle; policy accept;
358 {% for (let rule in fw4.rules("mangle_input")): %}
359 {%+ include("rule.uc", { fw4, rule }) %}
363 chain mangle_output {
364 type filter hook output priority mangle; policy accept;
365 {% for (let rule in fw4.rules("mangle_output")): %}
366 {%+ include("rule.uc", { fw4, rule }) %}
370 chain mangle_forward {
371 type filter hook forward priority mangle; policy accept;
372 {% for (let rule in fw4.rules("mangle_forward")): %}
373 {%+ include("rule.uc", { fw4, rule }) %}
375 {% for (let zone in fw4.zones()): %}
376 {% if (zone.mtu_fix): %}
377 {% for (let rule in zone.match_rules): %}
378 {%+ include("zone-mssfix.uc", { fw4, zone, rule, egress: false }) %}
379 {%+ include("zone-mssfix.uc", { fw4, zone, rule, egress: true }) %}