4 delete flowtable inet fw4 ft
8 {% if (fw4.default_option("flow_offloading") && length(devices) > 0): %}
14 hook ingress priority 0;
15 devices = {{ fw4.set(devices, true) }};
16 {% if (fw4.default_option("flow_offloading_hw")): %}
26 {% for (let set in fw4.ipsets()): %}
28 type {{ fw4.concat(set.types) }}
29 {% if (set.maxelem > 0): %}
30 size {{ set.maxelem }}
32 {% if (set.timeout >= 0): %}
33 timeout {{ set.timeout }}s
35 {% if (set.interval): %}
38 {% fw4.print_setentries(set) %}
47 {% for (let zone in fw4.zones()): %}
48 {% if (length(zone.match_devices)): %}
49 define {{ zone.name }}_devices = {{ fw4.set(zone.match_devices, true) }}
51 {% if (length(zone.match_subnets)): %}
52 define {{ zone.name }}_subnets = {{ fw4.set(zone.match_subnets, true) }}
60 include "/etc/nftables.d/*.nft"
68 type filter hook input priority filter; policy {{ fw4.input_policy(true) }};
70 iifname "lo" accept comment "!fw4: Accept traffic from loopback"
72 ct state established,related accept comment "!fw4: Allow inbound established and related flows"
73 {% if (fw4.default_option("drop_invalid")): %}
74 ct state invalid drop comment "!fw4: Drop flows with invalid conntrack state"
76 {% if (fw4.default_option("synflood_protect") && fw4.default_option("synflood_rate")): %}
77 tcp flags & (fin | syn | rst | ack) == syn jump syn_flood comment "!fw4: Rate limit TCP syn packets"
79 {% for (let rule in fw4.rules("input")): %}
80 {%+ include("rule.uc", { fw4, rule }) %}
82 {% for (let zone in fw4.zones()): for (let rule in zone.match_rules): %}
83 {%+ include("zone-jump.uc", { fw4, zone, rule, direction: "input" }) %}
85 {% if (fw4.input_policy() == "reject"): %}
91 type filter hook forward priority filter; policy {{ fw4.forward_policy(true) }};
93 {% if (fw4.default_option("flow_offloading") && length(devices) > 0): %}
94 meta l4proto { tcp, udp } flow offload @ft;
96 ct state established,related accept comment "!fw4: Allow forwarded established and related flows"
97 {% if (fw4.default_option("drop_invalid")): %}
98 ct state invalid drop comment "!fw4: Drop flows with invalid conntrack state"
100 {% for (let rule in fw4.rules("forward")): %}
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: "forward" }) %}
106 {% if (fw4.forward_policy() == "reject"): %}
112 type filter hook output priority filter; policy {{ fw4.output_policy(true) }};
114 oifname "lo" accept comment "!fw4: Accept traffic towards loopback"
116 ct state established,related accept comment "!fw4: Allow outbound established and related flows"
117 {% if (fw4.default_option("drop_invalid")): %}
118 ct state invalid drop comment "!fw4: Drop flows with invalid conntrack state"
120 {% for (let rule in fw4.rules("output")): %}
121 {%+ include("rule.uc", { fw4, rule }) %}
123 {% for (let zone in fw4.zones()): for (let rule in zone.match_rules): %}
124 {%+ include("zone-jump.uc", { fw4, zone, rule, direction: "output" }) %}
126 {% if (fw4.output_policy() == "reject"): %}
131 chain handle_reject {
132 meta l4proto tcp reject with {{
133 (fw4.default_option("tcp_reject_code") != "tcp-reset")
134 ? "icmpx type " + fw4.default_option("tcp_reject_code")
136 }} comment "!fw4: Reject TCP traffic"
138 (fw4.default_option("any_reject_code") != "tcp-reset")
139 ? "icmpx type " + fw4.default_option("any_reject_code")
141 }} comment "!fw4: Reject any other traffic"
144 {% if (fw4.default_option("synflood_protect") && fw4.default_option("synflood_rate")):
145 let r = fw4.default_option("synflood_rate");
146 let b = fw4.default_option("synflood_burst");
149 limit rate {{ r.rate }}/{{ r.unit }}
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-jump.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-jump.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 {% for (let saddrs in zone.masq4_src_subnets): %}
248 {% for (let daddrs in zone.masq4_dest_subnets): %}
249 {%+ include("zone-masq.uc", { fw4, zone, family: 4, saddrs, daddrs }) %}
253 {% if (zone.masq6): %}
254 {% for (let saddrs in zone.masq6_src_subnets): %}
255 {% for (let daddrs in zone.masq6_dest_subnets): %}
256 {%+ include("zone-masq.uc", { fw4, zone, family: 6, saddrs, daddrs }) %}
266 # Raw rules (notrack & helper)
269 chain raw_prerouting {
270 type filter hook prerouting priority raw; policy accept;
271 {% for (let target in ["helper", "notrack"]): %}
272 {% for (let zone in fw4.zones()): %}
273 {% if (zone.dflags[target]): %}
274 {% for (let rule in zone.match_rules): %}
275 {% let devices_pos = fw4.filter_loopback_devs(rule.devices_pos, false); %}
276 {% let subnets_pos = fw4.filter_loopback_addrs(rule.subnets_pos, false); %}
277 {% if (rule.devices_neg || rule.subnets_neg || devices_pos || subnets_pos): %}
278 {%+ if (rule.family): -%}
279 meta nfproto {{ fw4.nfproto(rule.family) }} {%+ endif -%}
280 {%+ include("zone-match.uc", { fw4, egress: false, rule: { ...rule, devices_pos, subnets_pos } }) -%}
281 jump {{ target }}_{{ zone.name }} comment "!fw4: {{ zone.name }} {{ fw4.nfproto(rule.family, true) }} {{
282 (target == "helper") ? "CT helper assignment" : "CT bypass"
292 type filter hook output priority raw; policy accept;
293 {% for (let target in ["helper", "notrack"]): %}
294 {% for (let zone in fw4.zones()): %}
295 {% if (zone.dflags[target]): %}
296 {% for (let rule in zone.match_rules): %}
297 {% let devices_pos = fw4.filter_loopback_devs(rule.devices_pos, true); %}
298 {% let subnets_pos = fw4.filter_loopback_addrs(rule.subnets_pos, true); %}
299 {% if (devices_pos || subnets_pos): %}
300 {%+ if (rule.family): -%}
301 meta nfproto {{ fw4.nfproto(rule.family) }} {%+ endif -%}
302 {%+ include("zone-match.uc", { fw4, egress: false, rule: { ...rule, devices_pos, subnets_pos } }) -%}
303 jump {{ target }}_{{ zone.name }} comment "!fw4: {{ zone.name }} {{ fw4.nfproto(rule.family, true) }} {{
304 (target == "helper") ? "CT helper assignment" : "CT bypass"
313 {% for (let helper in fw4.helpers()): %}
314 {% if (helper.available): %}
315 {% for (let proto in helper.proto): %}
316 ct helper {{ helper.name }} {
317 type {{ fw4.quote(helper.name, true) }} protocol {{ proto.name }};
323 {% for (let target in ["helper", "notrack"]): %}
324 {% for (let zone in fw4.zones()): %}
325 {% if (zone.dflags[target]): %}
326 chain {{ target }}_{{ zone.name }} {
327 {% for (let rule in fw4.rules(target+"_"+zone.name)): %}
328 {%+ include("rule.uc", { fw4, rule }) %}
340 chain mangle_prerouting {
341 type filter hook prerouting priority mangle; policy accept;
342 {% for (let rule in fw4.rules("mangle_prerouting")): %}
343 {%+ include("rule.uc", { fw4, rule }) %}
347 chain mangle_postrouting {
348 type filter hook postrouting priority mangle; policy accept;
349 {% for (let rule in fw4.rules("mangle_postrouting")): %}
350 {%+ include("rule.uc", { fw4, rule }) %}
355 type filter hook input priority mangle; policy accept;
356 {% for (let rule in fw4.rules("mangle_input")): %}
357 {%+ include("rule.uc", { fw4, rule }) %}
361 chain mangle_output {
362 type filter hook output priority mangle; policy accept;
363 {% for (let rule in fw4.rules("mangle_output")): %}
364 {%+ include("rule.uc", { fw4, rule }) %}
368 chain mangle_forward {
369 type filter hook forward priority mangle; policy accept;
370 {% for (let rule in fw4.rules("mangle_forward")): %}
371 {%+ include("rule.uc", { fw4, rule }) %}
373 {% for (let zone in fw4.zones()): %}
374 {% if (zone.mtu_fix): %}
375 {% for (let rule in zone.match_rules): %}
376 {%+ include("zone-mssfix.uc", { fw4, zone, rule, egress: false }) %}
377 {%+ include("zone-mssfix.uc", { fw4, zone, rule, egress: true }) %}