9 {% for (let set in fw4.ipsets()): %}
11 type {{ fw4.concat(set.types) }}
12 {% if (set.maxelem > 0): %}
13 size {{ set.maxelem }}
15 {% if (set.interval): %}
18 {% fw4.print_setentries(set) %}
27 {% for (let zone in fw4.zones()): %}
28 {% if (length(zone.match_devices)): %}
29 define {{ zone.name }}_devices = {{ fw4.set(zone.match_devices, true) }}
31 {% if (length(zone.match_subnets)): %}
32 define {{ zone.name }}_subnets = {{ fw4.set(zone.match_subnets, true) }}
40 include "/etc/nftables.d/*.nft"
48 type filter hook input priority filter; policy {{ fw4.input_policy(true) }};
50 iifname "lo" accept comment "!fw4: Accept traffic from loopback"
52 ct state established,related accept comment "!fw4: Allow inbound established and related flows"
53 {% if (fw4.default_option("drop_invalid")): %}
54 ct state invalid drop comment "!fw4: Drop flows with invalid conntrack state"
56 {% if (fw4.default_option("synflood_protect")): %}
57 tcp flags & (fin | syn | rst | ack) == syn jump syn_flood comment "!fw4: Rate limit TCP syn packets"
59 {% for (let rule in fw4.rules("input")): %}
60 {%+ include("rule.uc", { fw4, rule }) %}
62 {% for (let zone in fw4.zones()): for (let rule in zone.match_rules): %}
63 {%+ include("zone-match.uc", { fw4, zone, rule, direction: "input" }) %}
65 {% if (fw4.input_policy() == "reject"): %}
71 type filter hook forward priority filter; policy {{ fw4.forward_policy(true) }};
73 ct state established,related accept comment "!fw4: Allow forwarded established and related flows"
74 {% if (fw4.default_option("drop_invalid")): %}
75 ct state invalid drop comment "!fw4: Drop flows with invalid conntrack state"
77 {% for (let rule in fw4.rules("forward")): %}
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: "forward" }) %}
83 {% if (fw4.forward_policy() == "reject"): %}
89 type filter hook output priority filter; policy {{ fw4.output_policy(true) }};
91 oifname "lo" accept comment "!fw4: Accept traffic towards loopback"
93 ct state established,related accept comment "!fw4: Allow outbound 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("output")): %}
98 {%+ include("rule.uc", { fw4, rule }) %}
100 {% for (let zone in fw4.zones()): for (let rule in zone.match_rules): %}
101 {%+ include("zone-match.uc", { fw4, zone, rule, direction: "output" }) %}
103 {% if (fw4.output_policy() == "reject"): %}
108 chain handle_reject {
109 meta l4proto tcp reject with {{
110 (fw4.default_option("tcp_reject_code") != "tcp-reset")
111 ? "icmpx type " + fw4.default_option("tcp_reject_code")
113 }} comment "!fw4: Reject TCP traffic"
115 (fw4.default_option("any_reject_code") != "tcp-reset")
116 ? "icmpx type " + fw4.default_option("any_reject_code")
118 }} comment "!fw4: Reject any other traffic"
121 {% if (fw4.default_option("synflood_protect")):
122 let r = fw4.default_option("synflood_rate");
123 let b = fw4.default_option("synflood_burst");
126 tcp flags & (fin | syn | rst | ack) == syn
127 {%- if (r): %} limit rate {{ r.rate }}/{{ r.unit }}{% endif %}
128 {%- if (b): %} burst {{ b }} packets{% endif %} return comment "!fw4: Accept SYN packets below rate-limit"
129 drop comment "!fw4: Drop excess packets"
133 {% for (let zone in fw4.zones()): %}
134 chain input_{{ zone.name }} {
135 {% for (let rule in fw4.rules("input_"+zone.name)): %}
136 {%+ include("rule.uc", { fw4, rule }) %}
138 {% if (zone.dflags.dnat): %}
139 ct status dnat accept comment "!fw4: Accept port redirections"
141 jump {{ zone.input }}_from_{{ zone.name }}
144 chain output_{{ zone.name }} {
145 {% for (let rule in fw4.rules("output_"+zone.name)): %}
146 {%+ include("rule.uc", { fw4, rule }) %}
148 jump {{ zone.output }}_to_{{ zone.name }}
151 chain forward_{{ zone.name }} {
152 {% for (let rule in fw4.rules("forward_"+zone.name)): %}
153 {%+ include("rule.uc", { fw4, rule }) %}
155 {% if (zone.dflags.dnat): %}
156 ct status dnat accept comment "!fw4: Accept port forwards"
158 jump {{ zone.forward }}_to_{{ zone.name }}
161 {% for (let verdict in ["accept", "reject", "drop"]): %}
162 {% if (zone.sflags[verdict]): %}
163 chain {{ verdict }}_from_{{ zone.name }} {
164 {% for (let rule in zone.match_rules): %}
165 {%+ include("zone-verdict.uc", { fw4, zone, rule, egress: false, verdict }) %}
170 {% if (zone.dflags[verdict]): %}
171 chain {{ verdict }}_to_{{ zone.name }} {
172 {% for (let rule in zone.match_rules): %}
173 {%+ include("zone-verdict.uc", { fw4, zone, rule, egress: true, verdict }) %}
186 type nat hook prerouting priority dstnat; policy accept;
187 {% for (let zone in fw4.zones()): %}
188 {% if (zone.dflags.dnat): %}
189 {% for (let rule in zone.match_rules): %}
190 {%+ include("zone-match.uc", { fw4, zone, rule, direction: "dstnat" }) %}
197 type nat hook postrouting priority srcnat; policy accept;
198 {% for (let redirect in fw4.redirects("srcnat")): %}
199 {%+ include("redirect.uc", { fw4, redirect }) %}
201 {% for (let zone in fw4.zones()): %}
202 {% if (zone.dflags.snat): %}
203 {% for (let rule in zone.match_rules): %}
204 {%+ include("zone-match.uc", { fw4, zone, rule, direction: "srcnat" }) %}
210 {% for (let zone in fw4.zones()): %}
211 {% if (zone.dflags.dnat): %}
212 chain dstnat_{{ zone.name }} {
213 {% for (let redirect in fw4.redirects("dstnat_"+zone.name)): %}
214 {%+ include("redirect.uc", { fw4, redirect }) %}
219 {% if (zone.dflags.snat): %}
220 chain srcnat_{{ zone.name }} {
221 {% for (let redirect in fw4.redirects("srcnat_"+zone.name)): %}
222 {%+ include("redirect.uc", { fw4, redirect }) %}
224 {% if (zone.masq): %}
225 meta nfproto ipv4 {%+ if (zone.masq4_src_pos): -%}
226 ip saddr {{ fw4.set(zone.masq4_src_pos) }} {%+ endif -%}
227 {%+ if (zone.masq4_src_neg): -%}
228 ip saddr != {{ fw4.set(zone.masq4_src_neg) }} {%+ endif -%}
229 {%+ if (zone.masq4_dest_pos): -%}
230 ip daddr {{ fw4.set(zone.masq4_dest_pos) }} {%+ endif -%}
231 {%+ if (zone.masq4_dest_neg): -%}
232 ip daddr != {{ fw4.set(zone.masq4_dest_neg) }} {%+ endif -%}
233 masquerade comment "!fw4: Masquerade IPv4 {{ zone.name }} traffic"
235 {% if (zone.masq6): %}
236 meta nfproto ipv6 {%+ if (zone.masq6_src_pos): -%}
237 ip6 saddr {{ fw4.set(zone.masq6_src_pos) }} {%+ endif -%}
238 {%+ if (zone.masq6_src_neg): -%}
239 ip6 saddr != {{ fw4.set(zone.masq6_src_neg) }} {%+ endif -%}
240 {%+ if (zone.masq6_dest_pos): -%}
241 ip6 daddr {{ fw4.set(zone.masq6_dest_pos) }} {%+ endif -%}
242 {%+ if (zone.masq6_dest_neg): -%}
243 ip6 daddr != {{ fw4.set(zone.masq6_dest_neg) }} {%+ endif -%}
244 masquerade comment "!fw4: Masquerade IPv6 {{ zone.name }} traffic"
252 # Raw rules (notrack & helper)
255 chain raw_prerouting {
256 type filter hook prerouting priority raw; policy accept;
257 {% for (let target in ["helper", "notrack"]): %}
258 {% for (let zone in fw4.zones()): %}
259 {% if (zone.dflags[target]): %}
260 {% for (let rule in zone.match_rules): %}
261 {% let devs = fw4.filter_loopback_devs(rule.devices_pos, false); %}
262 {% let nets = fw4.filter_loopback_addrs(rule.subnets_pos, false); %}
263 {% if (rule.devices_neg || rule.subnets_neg || length(devs) || length(nets)): %}
264 {%+ if (rule.family): -%}
265 meta nfproto {{ fw4.nfproto(rule.family) }} {%+ endif -%}
266 {%+ if (length(devs)): -%}
267 iifname {{ fw4.set(devs) }} {%+ endif -%}
268 {%+ if (rule.devices_neg): -%}
269 iifname != {{ fw4.set(rule.devices_neg) }} {%+ endif -%}
270 {%+ if (length(nets)): -%}
271 {{ fw4.ipproto(rule.family) }} saddr {{ fw4.set(nets) }} {%+ endif -%}
272 {%+ if (rule.subnets_neg): -%}
273 {{ fw4.ipproto(rule.family) }} saddr != {{ fw4.set(rule.subnets_neg) }} {%+ endif -%}
274 jump {{ target }}_{{ zone.name }} comment "!fw4: {{ zone.name }} {{ fw4.nfproto(rule.family, true) }} {{
275 (target == "helper") ? "CT helper assignment" : "CT bypass"
285 type filter hook output priority raw; policy accept;
286 {% for (let target in ["helper", "notrack"]): %}
287 {% for (let zone in fw4.zones()): %}
288 {% if (zone.dflags[target]): %}
289 {% for (let rule in zone.match_rules): %}
290 {% let devs = fw4.filter_loopback_devs(rule.devices_pos, true); %}
291 {% let nets = fw4.filter_loopback_addrs(rule.subnets_pos, true); %}
292 {% if (length(devs) || length(nets)): %}
293 {%+ if (rule.family): -%}
294 meta nfproto {{ fw4.nfproto(rule.family) }} {%+ endif -%}
295 {%+ if (length(devs)): -%}
296 iifname {{ fw4.set(devs) }} {%+ endif -%}
297 {%+ if (length(nets)): -%}
298 {{ fw4.ipproto(rule.family) }} saddr {{ fw4.set(nets) }} {%+ endif -%}
299 jump {{ target }}_{{ zone.name }} comment "!fw4: {{ zone.name }} {{ fw4.nfproto(rule.family, true) }} {{
300 (target == "helper") ? "CT helper assignment" : "CT bypass"
309 {% for (let helper in fw4.helpers()): %}
310 {% if (helper.available): %}
311 {% for (let proto in helper.proto): %}
312 ct helper {{ helper.name }} {
313 type {{ fw4.quote(helper.name, true) }} protocol {{ proto.name }};
319 {% for (let target in ["helper", "notrack"]): %}
320 {% for (let zone in fw4.zones()): %}
321 {% if (zone.dflags[target]): %}
322 chain {{ target }}_{{ zone.name }} {
323 {% for (let rule in fw4.rules(target+"_"+zone.name)): %}
324 {%+ include("rule.uc", { fw4, rule }) %}
336 chain mangle_prerouting {
337 type filter hook prerouting priority mangle; policy accept;
338 {% for (let rule in fw4.rules("mangle_prerouting")): %}
339 {%+ include("rule.uc", { fw4, rule }) %}
343 chain mangle_output {
344 type filter hook output priority mangle; policy accept;
345 {% for (let rule in fw4.rules("mangle_output")): %}
346 {%+ include("rule.uc", { fw4, rule }) %}
350 chain mangle_forward {
351 type filter hook forward priority mangle; policy accept;
352 {% for (let zone in fw4.zones()): %}
353 {% if (zone.mtu_fix): %}
354 {% for (let rule in zone.match_rules): %}
355 {%+ include("zone-mssfix.uc", { fw4, zone, rule, egress: false }) %}
356 {%+ include("zone-mssfix.uc", { fw4, zone, rule, egress: true }) %}