treewide: convert deprecated syntax
[project/firewall4.git] / root / usr / share / firewall4 / templates / ruleset.uc
1 table inet fw4
2 flush table inet fw4
3
4 table inet fw4 {
5 #
6 # Set definitions
7 #
8
9 {% for (let set in fw4.ipsets()): %}
10 set {{ set.name }} {
11 type {{ fw4.concat(set.types) }}
12 {% if (set.maxelem > 0): %}
13 size {{ set.maxelem }}
14 {% endif %}
15 {% if (set.interval): %}
16 flags interval
17 {% endif %}
18 {% fw4.print_setentries(set) %}
19 }
20
21 {% endfor %}
22
23 #
24 # Defines
25 #
26
27 {% for (let zone in fw4.zones()): %}
28 {% if (length(zone.match_devices)): %}
29 define {{ zone.name }}_devices = {{ fw4.set(zone.match_devices, true) }}
30 {% endif %}
31 {% if (length(zone.match_subnets)): %}
32 define {{ zone.name }}_subnets = {{ fw4.set(zone.match_subnets, true) }}
33 {% endif %}
34 {% endfor %}
35
36 #
37 # User includes
38 #
39
40 include "/etc/nftables.d/*.nft"
41
42
43 #
44 # Filter rules
45 #
46
47 chain input {
48 type filter hook input priority filter; policy {{ fw4.input_policy(true) }};
49
50 iifname "lo" accept comment "!fw4: Accept traffic from loopback"
51
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"
55 {% endif %}
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"
58 {% endif %}
59 {% for (let rule in fw4.rules("input")): %}
60 {%+ include("rule.uc", { fw4, rule }) %}
61 {% endfor %}
62 {% for (let zone in fw4.zones()): for (let rule in zone.match_rules): %}
63 {%+ include("zone-match.uc", { fw4, zone, rule, direction: "input" }) %}
64 {% endfor; endfor %}
65 {% if (fw4.input_policy() == "reject"): %}
66 jump handle_reject
67 {% endif %}
68 }
69
70 chain forward {
71 type filter hook forward priority filter; policy {{ fw4.forward_policy(true) }};
72
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"
76 {% endif %}
77 {% for (let rule in fw4.rules("forward")): %}
78 {%+ include("rule.uc", { fw4, rule }) %}
79 {% endfor %}
80 {% for (let zone in fw4.zones()): for (let rule in zone.match_rules): %}
81 {%+ include("zone-match.uc", { fw4, zone, rule, direction: "forward" }) %}
82 {% endfor; endfor %}
83 {% if (fw4.forward_policy() == "reject"): %}
84 jump handle_reject
85 {% endif %}
86 }
87
88 chain output {
89 type filter hook output priority filter; policy {{ fw4.output_policy(true) }};
90
91 oifname "lo" accept comment "!fw4: Accept traffic towards loopback"
92
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"
96 {% endif %}
97 {% for (let rule in fw4.rules("output")): %}
98 {%+ include("rule.uc", { fw4, rule }) %}
99 {% endfor %}
100 {% for (let zone in fw4.zones()): for (let rule in zone.match_rules): %}
101 {%+ include("zone-match.uc", { fw4, zone, rule, direction: "output" }) %}
102 {% endfor; endfor %}
103 {% if (fw4.output_policy() == "reject"): %}
104 jump handle_reject
105 {% endif %}
106 }
107
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")
112 : "tcp reset"
113 }} comment "!fw4: Reject TCP traffic"
114 reject with {{
115 (fw4.default_option("any_reject_code") != "tcp-reset")
116 ? "icmpx type " + fw4.default_option("any_reject_code")
117 : "tcp reset"
118 }} comment "!fw4: Reject any other traffic"
119 }
120
121 {% if (fw4.default_option("synflood_protect")):
122 let r = fw4.default_option("synflood_rate");
123 let b = fw4.default_option("synflood_burst");
124 %}
125 chain syn_flood {
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"
130 }
131
132 {% endif %}
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 }) %}
137 {% endfor %}
138 {% if (zone.dflags.dnat): %}
139 ct status dnat accept comment "!fw4: Accept port redirections"
140 {% endif %}
141 jump {{ zone.input }}_from_{{ zone.name }}
142 }
143
144 chain output_{{ zone.name }} {
145 {% for (let rule in fw4.rules("output_"+zone.name)): %}
146 {%+ include("rule.uc", { fw4, rule }) %}
147 {% endfor %}
148 jump {{ zone.output }}_to_{{ zone.name }}
149 }
150
151 chain forward_{{ zone.name }} {
152 {% for (let rule in fw4.rules("forward_"+zone.name)): %}
153 {%+ include("rule.uc", { fw4, rule }) %}
154 {% endfor %}
155 {% if (zone.dflags.dnat): %}
156 ct status dnat accept comment "!fw4: Accept port forwards"
157 {% endif %}
158 jump {{ zone.forward }}_to_{{ zone.name }}
159 }
160
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 }) %}
166 {% endfor %}
167 }
168
169 {% endif %}
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 }) %}
174 {% endfor %}
175 }
176
177 {% endif %}
178 {% endfor %}
179 {% endfor %}
180
181 #
182 # NAT rules
183 #
184
185 chain dstnat {
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" }) %}
191 {% endfor %}
192 {% endif %}
193 {% endfor %}
194 }
195
196 chain srcnat {
197 type nat hook postrouting priority srcnat; policy accept;
198 {% for (let redirect in fw4.redirects("srcnat")): %}
199 {%+ include("redirect.uc", { fw4, redirect }) %}
200 {% endfor %}
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" }) %}
205 {% endfor %}
206 {% endif %}
207 {% endfor %}
208 }
209
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 }) %}
215 {% endfor %}
216 }
217
218 {% endif %}
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 }) %}
223 {% endfor %}
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"
234 {% endif %}
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"
245 {% endif %}
246 }
247
248 {% endif %}
249 {% endfor %}
250
251 #
252 # Raw rules (notrack & helper)
253 #
254
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"
276 }}"
277 {% endif %}
278 {% endfor %}
279 {% endif %}
280 {% endfor %}
281 {% endfor %}
282 }
283
284 chain raw_output {
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"
301 }}"
302 {% endif %}
303 {% endfor %}
304 {% endif %}
305 {% endfor %}
306 {% endfor %}
307 }
308
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 }};
314 }
315
316 {% endfor %}
317 {% endif %}
318 {% endfor %}
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 }) %}
325 {% endfor %}
326 }
327
328 {% endif %}
329 {% endfor %}
330 {% endfor %}
331
332 #
333 # Mangle rules
334 #
335
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 }) %}
340 {% endfor %}
341 }
342
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 }) %}
347 {% endfor %}
348 }
349
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 }) %}
357 {% endfor %}
358 {% endif %}
359 {% endfor %}
360 }
361 }