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")): %}
23 {% for (let set in fw4.ipsets()): %}
25 type {{ fw4.concat(set.types) }}
26 {% if (set.maxelem > 0): %}
27 size {{ set.maxelem }}
29 {% if (set.timeout >= 0): %}
30 timeout {{ set.timeout }}s
32 {% if (set.interval): %}
35 {% fw4.print_setentries(set) %}
44 {% for (let zone in fw4.zones()): %}
45 {% if (length(zone.match_devices)): %}
46 define {{ zone.name }}_devices = {{ fw4.set(zone.match_devices, true) }}
48 {% if (length(zone.match_subnets)): %}
49 define {{ zone.name }}_subnets = {{ fw4.set(zone.match_subnets, true) }}
57 include "/etc/nftables.d/*.nft"
65 type filter hook input priority filter; policy {{ fw4.input_policy(true) }};
67 iifname "lo" accept comment "!fw4: Accept traffic from loopback"
69 ct state established,related accept comment "!fw4: Allow inbound established and related flows"
70 {% if (fw4.default_option("drop_invalid")): %}
71 ct state invalid drop comment "!fw4: Drop flows with invalid conntrack state"
73 {% if (fw4.default_option("synflood_protect") && fw4.default_option("synflood_rate")): %}
74 tcp flags & (fin | syn | rst | ack) == syn jump syn_flood comment "!fw4: Rate limit TCP syn packets"
76 {% for (let rule in fw4.rules("input")): %}
77 {%+ include("rule.uc", { fw4, rule }) %}
79 {% for (let zone in fw4.zones()): for (let rule in zone.match_rules): %}
80 {%+ include("zone-jump.uc", { fw4, zone, rule, direction: "input" }) %}
82 {% if (fw4.input_policy() == "reject"): %}
88 type filter hook forward priority filter; policy {{ fw4.forward_policy(true) }};
90 {% if (fw4.default_option("flow_offloading") && length(devices) > 0): %}
91 meta l4proto { tcp, udp } flow offload @ft;
93 ct state established,related accept comment "!fw4: Allow forwarded 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 {% for (let rule in fw4.rules("forward")): %}
98 {%+ include("rule.uc", { fw4, rule }) %}
100 {% for (let zone in fw4.zones()): for (let rule in zone.match_rules): %}
101 {%+ include("zone-jump.uc", { fw4, zone, rule, direction: "forward" }) %}
103 {% if (fw4.forward_policy() == "reject"): %}
109 type filter hook output priority filter; policy {{ fw4.output_policy(true) }};
111 oifname "lo" accept comment "!fw4: Accept traffic towards loopback"
113 ct state established,related accept comment "!fw4: Allow outbound established and related flows"
114 {% if (fw4.default_option("drop_invalid")): %}
115 ct state invalid drop comment "!fw4: Drop flows with invalid conntrack state"
117 {% for (let rule in fw4.rules("output")): %}
118 {%+ include("rule.uc", { fw4, rule }) %}
120 {% for (let zone in fw4.zones()): for (let rule in zone.match_rules): %}
121 {%+ include("zone-jump.uc", { fw4, zone, rule, direction: "output" }) %}
123 {% if (fw4.output_policy() == "reject"): %}
128 chain handle_reject {
129 meta l4proto tcp reject with {{
130 (fw4.default_option("tcp_reject_code") != "tcp-reset")
131 ? "icmpx type " + fw4.default_option("tcp_reject_code")
133 }} comment "!fw4: Reject TCP traffic"
135 (fw4.default_option("any_reject_code") != "tcp-reset")
136 ? "icmpx type " + fw4.default_option("any_reject_code")
138 }} comment "!fw4: Reject any other traffic"
141 {% if (fw4.default_option("synflood_protect") && fw4.default_option("synflood_rate")):
142 let r = fw4.default_option("synflood_rate");
143 let b = fw4.default_option("synflood_burst");
146 limit rate {{ r.rate }}/{{ r.unit }}
147 {%- if (b): %} burst {{ b }} packets{% endif %} return comment "!fw4: Accept SYN packets below rate-limit"
148 drop comment "!fw4: Drop excess packets"
152 {% for (let zone in fw4.zones()): %}
153 chain input_{{ zone.name }} {
154 {% for (let rule in fw4.rules("input_"+zone.name)): %}
155 {%+ include("rule.uc", { fw4, rule }) %}
157 {% if (zone.dflags.dnat): %}
158 ct status dnat accept comment "!fw4: Accept port redirections"
160 jump {{ zone.input }}_from_{{ zone.name }}
163 chain output_{{ zone.name }} {
164 {% for (let rule in fw4.rules("output_"+zone.name)): %}
165 {%+ include("rule.uc", { fw4, rule }) %}
167 jump {{ zone.output }}_to_{{ zone.name }}
170 chain forward_{{ zone.name }} {
171 {% for (let rule in fw4.rules("forward_"+zone.name)): %}
172 {%+ include("rule.uc", { fw4, rule }) %}
174 {% if (zone.dflags.dnat): %}
175 ct status dnat accept comment "!fw4: Accept port forwards"
177 jump {{ zone.forward }}_to_{{ zone.name }}
180 {% for (let verdict in ["accept", "reject", "drop"]): %}
181 {% if (zone.sflags[verdict]): %}
182 chain {{ verdict }}_from_{{ zone.name }} {
183 {% for (let rule in zone.match_rules): %}
184 {%+ include("zone-verdict.uc", { fw4, zone, rule, egress: false, verdict }) %}
189 {% if (zone.dflags[verdict]): %}
190 chain {{ verdict }}_to_{{ zone.name }} {
191 {% for (let rule in zone.match_rules): %}
192 {%+ include("zone-verdict.uc", { fw4, zone, rule, egress: true, verdict }) %}
205 type nat hook prerouting priority dstnat; policy accept;
206 {% for (let zone in fw4.zones()): %}
207 {% if (zone.dflags.dnat): %}
208 {% for (let rule in zone.match_rules): %}
209 {%+ include("zone-jump.uc", { fw4, zone, rule, direction: "dstnat" }) %}
216 type nat hook postrouting priority srcnat; policy accept;
217 {% for (let redirect in fw4.redirects("srcnat")): %}
218 {%+ include("redirect.uc", { fw4, redirect }) %}
220 {% for (let zone in fw4.zones()): %}
221 {% if (zone.dflags.snat): %}
222 {% for (let rule in zone.match_rules): %}
223 {%+ include("zone-jump.uc", { fw4, zone, rule, direction: "srcnat" }) %}
229 {% for (let zone in fw4.zones()): %}
230 {% if (zone.dflags.dnat): %}
231 chain dstnat_{{ zone.name }} {
232 {% for (let redirect in fw4.redirects("dstnat_"+zone.name)): %}
233 {%+ include("redirect.uc", { fw4, redirect }) %}
238 {% if (zone.dflags.snat): %}
239 chain srcnat_{{ zone.name }} {
240 {% for (let redirect in fw4.redirects("srcnat_"+zone.name)): %}
241 {%+ include("redirect.uc", { fw4, redirect }) %}
243 {% if (zone.masq): %}
244 {% for (let saddrs in zone.masq4_src_subnets): %}
245 {% for (let daddrs in zone.masq4_dest_subnets): %}
246 {%+ include("zone-masq.uc", { fw4, zone, family: 4, saddrs, daddrs }) %}
250 {% if (zone.masq6): %}
251 {% for (let saddrs in zone.masq6_src_subnets): %}
252 {% for (let daddrs in zone.masq6_dest_subnets): %}
253 {%+ include("zone-masq.uc", { fw4, zone, family: 6, saddrs, daddrs }) %}
263 # Raw rules (notrack & helper)
266 chain raw_prerouting {
267 type filter hook prerouting priority raw; policy accept;
268 {% for (let target in ["helper", "notrack"]): %}
269 {% for (let zone in fw4.zones()): %}
270 {% if (zone.dflags[target]): %}
271 {% for (let rule in zone.match_rules): %}
272 {% let devices_pos = fw4.filter_loopback_devs(rule.devices_pos, false); %}
273 {% let subnets_pos = fw4.filter_loopback_addrs(rule.subnets_pos, false); %}
274 {% if (rule.devices_neg || rule.subnets_neg || devices_pos || subnets_pos): %}
275 {%+ if (rule.family): -%}
276 meta nfproto {{ fw4.nfproto(rule.family) }} {%+ endif -%}
277 {%+ include("zone-match.uc", { fw4, egress: false, rule: { ...rule, devices_pos, subnets_pos } }) -%}
278 jump {{ target }}_{{ zone.name }} comment "!fw4: {{ zone.name }} {{ fw4.nfproto(rule.family, true) }} {{
279 (target == "helper") ? "CT helper assignment" : "CT bypass"
289 type filter hook output priority raw; policy accept;
290 {% for (let target in ["helper", "notrack"]): %}
291 {% for (let zone in fw4.zones()): %}
292 {% if (zone.dflags[target]): %}
293 {% for (let rule in zone.match_rules): %}
294 {% let devices_pos = fw4.filter_loopback_devs(rule.devices_pos, true); %}
295 {% let subnets_pos = fw4.filter_loopback_addrs(rule.subnets_pos, true); %}
296 {% if (devices_pos || subnets_pos): %}
297 {%+ if (rule.family): -%}
298 meta nfproto {{ fw4.nfproto(rule.family) }} {%+ endif -%}
299 {%+ include("zone-match.uc", { fw4, egress: false, rule: { ...rule, devices_pos, subnets_pos } }) -%}
300 jump {{ target }}_{{ zone.name }} comment "!fw4: {{ zone.name }} {{ fw4.nfproto(rule.family, true) }} {{
301 (target == "helper") ? "CT helper assignment" : "CT bypass"
310 {% for (let helper in fw4.helpers()): %}
311 {% if (helper.available): %}
312 {% for (let proto in helper.proto): %}
313 ct helper {{ helper.name }} {
314 type {{ fw4.quote(helper.name, true) }} protocol {{ proto.name }};
320 {% for (let target in ["helper", "notrack"]): %}
321 {% for (let zone in fw4.zones()): %}
322 {% if (zone.dflags[target]): %}
323 chain {{ target }}_{{ zone.name }} {
324 {% for (let rule in fw4.rules(target+"_"+zone.name)): %}
325 {%+ include("rule.uc", { fw4, rule }) %}
337 chain mangle_prerouting {
338 type filter hook prerouting priority mangle; policy accept;
339 {% for (let rule in fw4.rules("mangle_prerouting")): %}
340 {%+ include("rule.uc", { fw4, rule }) %}
344 chain mangle_postrouting {
345 type filter hook postrouting priority mangle; policy accept;
346 {% for (let rule in fw4.rules("mangle_postrouting")): %}
347 {%+ include("rule.uc", { fw4, rule }) %}
352 type filter hook input priority mangle; policy accept;
353 {% for (let rule in fw4.rules("mangle_input")): %}
354 {%+ include("rule.uc", { fw4, rule }) %}
358 chain mangle_output {
359 type filter hook output priority mangle; policy accept;
360 {% for (let rule in fw4.rules("mangle_output")): %}
361 {%+ include("rule.uc", { fw4, rule }) %}
365 chain mangle_forward {
366 type filter hook forward priority mangle; policy accept;
367 {% for (let rule in fw4.rules("mangle_forward")): %}
368 {%+ include("rule.uc", { fw4, rule }) %}
370 {% for (let zone in fw4.zones()): %}
371 {% if (zone.mtu_fix): %}
372 {% for (let rule in zone.match_rules): %}
373 {%+ include("zone-mssfix.uc", { fw4, zone, rule, egress: false }) %}
374 {%+ include("zone-mssfix.uc", { fw4, zone, rule, egress: true }) %}