2a0a8a8d6cbbc5555019a20139a7169669a3577e
[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 {% if (fw4.default_option("flow_offloading") && length(devices) > 0): %}
6 #
7 # Flowtable
8 #
9
10 flowtable ft {
11 hook ingress priority 0;
12 devices = {{ fw4.set(devices, true) }};
13 {% if (fw4.default_option("flow_offloading_hw")): %}
14 flags offload;
15 {% endif %}
16 }
17
18 {% endif %}
19 #
20 # Set definitions
21 #
22
23 {% for (let set in fw4.ipsets()): %}
24 set {{ set.name }} {
25 type {{ fw4.concat(set.types) }}
26 {% if (set.maxelem > 0): %}
27 size {{ set.maxelem }}
28 {% endif %}
29 {% if (set.timeout >= 0): %}
30 timeout {{ set.timeout }}s
31 {% endif %}
32 {% if (set.interval): %}
33 flags interval
34 {% endif %}
35 {% fw4.print_setentries(set) %}
36 }
37
38 {% endfor %}
39
40 #
41 # Defines
42 #
43
44 {% for (let zone in fw4.zones()): %}
45 {% if (length(zone.match_devices)): %}
46 define {{ zone.name }}_devices = {{ fw4.set(zone.match_devices, true) }}
47 {% endif %}
48 {% if (length(zone.match_subnets)): %}
49 define {{ zone.name }}_subnets = {{ fw4.set(zone.match_subnets, true) }}
50 {% endif %}
51 {% endfor %}
52
53 #
54 # User includes
55 #
56
57 include "/etc/nftables.d/*.nft"
58
59
60 #
61 # Filter rules
62 #
63
64 chain input {
65 type filter hook input priority filter; policy {{ fw4.input_policy(true) }};
66
67 iifname "lo" accept comment "!fw4: Accept traffic from loopback"
68
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"
72 {% endif %}
73 {% if (fw4.default_option("synflood_protect")): %}
74 tcp flags & (fin | syn | rst | ack) == syn jump syn_flood comment "!fw4: Rate limit TCP syn packets"
75 {% endif %}
76 {% for (let rule in fw4.rules("input")): %}
77 {%+ include("rule.uc", { fw4, rule }) %}
78 {% endfor %}
79 {% for (let zone in fw4.zones()): for (let rule in zone.match_rules): %}
80 {%+ include("zone-jump.uc", { fw4, zone, rule, direction: "input" }) %}
81 {% endfor; endfor %}
82 {% if (fw4.input_policy() == "reject"): %}
83 jump handle_reject
84 {% endif %}
85 }
86
87 chain forward {
88 type filter hook forward priority filter; policy {{ fw4.forward_policy(true) }};
89
90 {% if (fw4.default_option("flow_offloading") && length(devices) > 0): %}
91 meta l4proto { tcp, udp } flow offload @ft;
92 {% endif %}
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"
96 {% endif %}
97 {% for (let rule in fw4.rules("forward")): %}
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-jump.uc", { fw4, zone, rule, direction: "forward" }) %}
102 {% endfor; endfor %}
103 {% if (fw4.forward_policy() == "reject"): %}
104 jump handle_reject
105 {% endif %}
106 }
107
108 chain output {
109 type filter hook output priority filter; policy {{ fw4.output_policy(true) }};
110
111 oifname "lo" accept comment "!fw4: Accept traffic towards loopback"
112
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"
116 {% endif %}
117 {% for (let rule in fw4.rules("output")): %}
118 {%+ include("rule.uc", { fw4, rule }) %}
119 {% endfor %}
120 {% for (let zone in fw4.zones()): for (let rule in zone.match_rules): %}
121 {%+ include("zone-jump.uc", { fw4, zone, rule, direction: "output" }) %}
122 {% endfor; endfor %}
123 {% if (fw4.output_policy() == "reject"): %}
124 jump handle_reject
125 {% endif %}
126 }
127
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")
132 : "tcp reset"
133 }} comment "!fw4: Reject TCP traffic"
134 reject with {{
135 (fw4.default_option("any_reject_code") != "tcp-reset")
136 ? "icmpx type " + fw4.default_option("any_reject_code")
137 : "tcp reset"
138 }} comment "!fw4: Reject any other traffic"
139 }
140
141 {% if (fw4.default_option("synflood_protect")):
142 let r = fw4.default_option("synflood_rate");
143 let b = fw4.default_option("synflood_burst");
144 %}
145 chain syn_flood {
146 tcp flags & (fin | syn | rst | ack) == syn
147 {%- if (r): %} limit rate {{ r.rate }}/{{ r.unit }}{% endif %}
148 {%- if (b): %} burst {{ b }} packets{% endif %} return comment "!fw4: Accept SYN packets below rate-limit"
149 drop comment "!fw4: Drop excess packets"
150 }
151
152 {% endif %}
153 {% for (let zone in fw4.zones()): %}
154 chain input_{{ zone.name }} {
155 {% for (let rule in fw4.rules("input_"+zone.name)): %}
156 {%+ include("rule.uc", { fw4, rule }) %}
157 {% endfor %}
158 {% if (zone.dflags.dnat): %}
159 ct status dnat accept comment "!fw4: Accept port redirections"
160 {% endif %}
161 jump {{ zone.input }}_from_{{ zone.name }}
162 }
163
164 chain output_{{ zone.name }} {
165 {% for (let rule in fw4.rules("output_"+zone.name)): %}
166 {%+ include("rule.uc", { fw4, rule }) %}
167 {% endfor %}
168 jump {{ zone.output }}_to_{{ zone.name }}
169 }
170
171 chain forward_{{ zone.name }} {
172 {% for (let rule in fw4.rules("forward_"+zone.name)): %}
173 {%+ include("rule.uc", { fw4, rule }) %}
174 {% endfor %}
175 {% if (zone.dflags.dnat): %}
176 ct status dnat accept comment "!fw4: Accept port forwards"
177 {% endif %}
178 jump {{ zone.forward }}_to_{{ zone.name }}
179 }
180
181 {% for (let verdict in ["accept", "reject", "drop"]): %}
182 {% if (zone.sflags[verdict]): %}
183 chain {{ verdict }}_from_{{ zone.name }} {
184 {% for (let rule in zone.match_rules): %}
185 {%+ include("zone-verdict.uc", { fw4, zone, rule, egress: false, verdict }) %}
186 {% endfor %}
187 }
188
189 {% endif %}
190 {% if (zone.dflags[verdict]): %}
191 chain {{ verdict }}_to_{{ zone.name }} {
192 {% for (let rule in zone.match_rules): %}
193 {%+ include("zone-verdict.uc", { fw4, zone, rule, egress: true, verdict }) %}
194 {% endfor %}
195 }
196
197 {% endif %}
198 {% endfor %}
199 {% endfor %}
200
201 #
202 # NAT rules
203 #
204
205 chain dstnat {
206 type nat hook prerouting priority dstnat; policy accept;
207 {% for (let zone in fw4.zones()): %}
208 {% if (zone.dflags.dnat): %}
209 {% for (let rule in zone.match_rules): %}
210 {%+ include("zone-jump.uc", { fw4, zone, rule, direction: "dstnat" }) %}
211 {% endfor %}
212 {% endif %}
213 {% endfor %}
214 }
215
216 chain srcnat {
217 type nat hook postrouting priority srcnat; policy accept;
218 {% for (let redirect in fw4.redirects("srcnat")): %}
219 {%+ include("redirect.uc", { fw4, redirect }) %}
220 {% endfor %}
221 {% for (let zone in fw4.zones()): %}
222 {% if (zone.dflags.snat): %}
223 {% for (let rule in zone.match_rules): %}
224 {%+ include("zone-jump.uc", { fw4, zone, rule, direction: "srcnat" }) %}
225 {% endfor %}
226 {% endif %}
227 {% endfor %}
228 }
229
230 {% for (let zone in fw4.zones()): %}
231 {% if (zone.dflags.dnat): %}
232 chain dstnat_{{ zone.name }} {
233 {% for (let redirect in fw4.redirects("dstnat_"+zone.name)): %}
234 {%+ include("redirect.uc", { fw4, redirect }) %}
235 {% endfor %}
236 }
237
238 {% endif %}
239 {% if (zone.dflags.snat): %}
240 chain srcnat_{{ zone.name }} {
241 {% for (let redirect in fw4.redirects("srcnat_"+zone.name)): %}
242 {%+ include("redirect.uc", { fw4, redirect }) %}
243 {% endfor %}
244 {% if (zone.masq): %}
245 meta nfproto ipv4 {%+ if (zone.masq4_src_pos): -%}
246 ip saddr {{ fw4.set(zone.masq4_src_pos) }} {%+ endif -%}
247 {%+ if (zone.masq4_src_neg): -%}
248 ip saddr != {{ fw4.set(zone.masq4_src_neg) }} {%+ endif -%}
249 {%+ if (zone.masq4_dest_pos): -%}
250 ip daddr {{ fw4.set(zone.masq4_dest_pos) }} {%+ endif -%}
251 {%+ if (zone.masq4_dest_neg): -%}
252 ip daddr != {{ fw4.set(zone.masq4_dest_neg) }} {%+ endif -%}
253 masquerade comment "!fw4: Masquerade IPv4 {{ zone.name }} traffic"
254 {% endif %}
255 {% if (zone.masq6): %}
256 meta nfproto ipv6 {%+ if (zone.masq6_src_pos): -%}
257 ip6 saddr {{ fw4.set(zone.masq6_src_pos) }} {%+ endif -%}
258 {%+ if (zone.masq6_src_neg): -%}
259 ip6 saddr != {{ fw4.set(zone.masq6_src_neg) }} {%+ endif -%}
260 {%+ if (zone.masq6_dest_pos): -%}
261 ip6 daddr {{ fw4.set(zone.masq6_dest_pos) }} {%+ endif -%}
262 {%+ if (zone.masq6_dest_neg): -%}
263 ip6 daddr != {{ fw4.set(zone.masq6_dest_neg) }} {%+ endif -%}
264 masquerade comment "!fw4: Masquerade IPv6 {{ zone.name }} traffic"
265 {% endif %}
266 }
267
268 {% endif %}
269 {% endfor %}
270
271 #
272 # Raw rules (notrack & helper)
273 #
274
275 chain raw_prerouting {
276 type filter hook prerouting priority raw; policy accept;
277 {% for (let target in ["helper", "notrack"]): %}
278 {% for (let zone in fw4.zones()): %}
279 {% if (zone.dflags[target]): %}
280 {% for (let rule in zone.match_rules): %}
281 {% let devices_pos = fw4.filter_loopback_devs(rule.devices_pos, false); %}
282 {% let subnets_pos = fw4.filter_loopback_addrs(rule.subnets_pos, false); %}
283 {% if (rule.devices_neg || rule.subnets_neg || devices_pos || subnets_pos): %}
284 {%+ if (rule.family): -%}
285 meta nfproto {{ fw4.nfproto(rule.family) }} {%+ endif -%}
286 {%+ include("zone-match.uc", { fw4, rule: { ...rule, devices_pos, subnets_pos } }) -%}
287 jump {{ target }}_{{ zone.name }} comment "!fw4: {{ zone.name }} {{ fw4.nfproto(rule.family, true) }} {{
288 (target == "helper") ? "CT helper assignment" : "CT bypass"
289 }}"
290 {% endif %}
291 {% endfor %}
292 {% endif %}
293 {% endfor %}
294 {% endfor %}
295 }
296
297 chain raw_output {
298 type filter hook output priority raw; policy accept;
299 {% for (let target in ["helper", "notrack"]): %}
300 {% for (let zone in fw4.zones()): %}
301 {% if (zone.dflags[target]): %}
302 {% for (let rule in zone.match_rules): %}
303 {% let devices_pos = fw4.filter_loopback_devs(rule.devices_pos, true); %}
304 {% let subnets_pos = fw4.filter_loopback_addrs(rule.subnets_pos, true); %}
305 {% if (devices_pos || subnets_pos): %}
306 {%+ if (rule.family): -%}
307 meta nfproto {{ fw4.nfproto(rule.family) }} {%+ endif -%}
308 {%+ include("zone-match.uc", { fw4, rule: { ...rule, devices_pos, subnets_pos } }) -%}
309 jump {{ target }}_{{ zone.name }} comment "!fw4: {{ zone.name }} {{ fw4.nfproto(rule.family, true) }} {{
310 (target == "helper") ? "CT helper assignment" : "CT bypass"
311 }}"
312 {% endif %}
313 {% endfor %}
314 {% endif %}
315 {% endfor %}
316 {% endfor %}
317 }
318
319 {% for (let helper in fw4.helpers()): %}
320 {% if (helper.available): %}
321 {% for (let proto in helper.proto): %}
322 ct helper {{ helper.name }} {
323 type {{ fw4.quote(helper.name, true) }} protocol {{ proto.name }};
324 }
325
326 {% endfor %}
327 {% endif %}
328 {% endfor %}
329 {% for (let target in ["helper", "notrack"]): %}
330 {% for (let zone in fw4.zones()): %}
331 {% if (zone.dflags[target]): %}
332 chain {{ target }}_{{ zone.name }} {
333 {% for (let rule in fw4.rules(target+"_"+zone.name)): %}
334 {%+ include("rule.uc", { fw4, rule }) %}
335 {% endfor %}
336 }
337
338 {% endif %}
339 {% endfor %}
340 {% endfor %}
341
342 #
343 # Mangle rules
344 #
345
346 chain mangle_prerouting {
347 type filter hook prerouting priority mangle; policy accept;
348 {% for (let rule in fw4.rules("mangle_prerouting")): %}
349 {%+ include("rule.uc", { fw4, rule }) %}
350 {% endfor %}
351 }
352
353 chain mangle_postrouting {
354 type filter hook postrouting priority mangle; policy accept;
355 {% for (let rule in fw4.rules("mangle_postrouting")): %}
356 {%+ include("rule.uc", { fw4, rule }) %}
357 {% endfor %}
358 }
359
360 chain mangle_input {
361 type filter hook input priority mangle; policy accept;
362 {% for (let rule in fw4.rules("mangle_input")): %}
363 {%+ include("rule.uc", { fw4, rule }) %}
364 {% endfor %}
365 }
366
367 chain mangle_output {
368 type filter hook output priority mangle; policy accept;
369 {% for (let rule in fw4.rules("mangle_output")): %}
370 {%+ include("rule.uc", { fw4, rule }) %}
371 {% endfor %}
372 }
373
374 chain mangle_forward {
375 type filter hook forward priority mangle; policy accept;
376 {% for (let rule in fw4.rules("mangle_forward")): %}
377 {%+ include("rule.uc", { fw4, rule }) %}
378 {% endfor %}
379 {% for (let zone in fw4.zones()): %}
380 {% if (zone.mtu_fix): %}
381 {% for (let rule in zone.match_rules): %}
382 {%+ include("zone-mssfix.uc", { fw4, zone, rule, egress: false }) %}
383 {%+ include("zone-mssfix.uc", { fw4, zone, rule, egress: true }) %}
384 {% endfor %}
385 {% endif %}
386 {% endfor %}
387 }
388 }