004cfca6bfb9bcfec926276ba412245237cdb943
[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") && fw4.default_option("synflood_rate")): %}
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") && fw4.default_option("synflood_rate")):
142 let r = fw4.default_option("synflood_rate");
143 let b = fw4.default_option("synflood_burst");
144 %}
145 chain syn_flood {
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"
149 }
150
151 {% endif %}
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 }) %}
156 {% endfor %}
157 {% if (zone.dflags.dnat): %}
158 ct status dnat accept comment "!fw4: Accept port redirections"
159 {% endif %}
160 jump {{ zone.input }}_from_{{ zone.name }}
161 }
162
163 chain output_{{ zone.name }} {
164 {% for (let rule in fw4.rules("output_"+zone.name)): %}
165 {%+ include("rule.uc", { fw4, rule }) %}
166 {% endfor %}
167 jump {{ zone.output }}_to_{{ zone.name }}
168 }
169
170 chain forward_{{ zone.name }} {
171 {% for (let rule in fw4.rules("forward_"+zone.name)): %}
172 {%+ include("rule.uc", { fw4, rule }) %}
173 {% endfor %}
174 {% if (zone.dflags.dnat): %}
175 ct status dnat accept comment "!fw4: Accept port forwards"
176 {% endif %}
177 jump {{ zone.forward }}_to_{{ zone.name }}
178 }
179
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 }) %}
185 {% endfor %}
186 }
187
188 {% endif %}
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 }) %}
193 {% endfor %}
194 }
195
196 {% endif %}
197 {% endfor %}
198 {% endfor %}
199
200 #
201 # NAT rules
202 #
203
204 chain dstnat {
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" }) %}
210 {% endfor %}
211 {% endif %}
212 {% endfor %}
213 }
214
215 chain srcnat {
216 type nat hook postrouting priority srcnat; policy accept;
217 {% for (let redirect in fw4.redirects("srcnat")): %}
218 {%+ include("redirect.uc", { fw4, redirect }) %}
219 {% endfor %}
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" }) %}
224 {% endfor %}
225 {% endif %}
226 {% endfor %}
227 }
228
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 }) %}
234 {% endfor %}
235 }
236
237 {% endif %}
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 }) %}
242 {% endfor %}
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 }) %}
247 {% endfor %}
248 {% endfor %}
249 {% endif %}
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 }) %}
254 {% endfor %}
255 {% endfor %}
256 {% endif %}
257 }
258
259 {% endif %}
260 {% endfor %}
261
262 #
263 # Raw rules (notrack & helper)
264 #
265
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"
280 }}"
281 {% endif %}
282 {% endfor %}
283 {% endif %}
284 {% endfor %}
285 {% endfor %}
286 }
287
288 chain raw_output {
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"
302 }}"
303 {% endif %}
304 {% endfor %}
305 {% endif %}
306 {% endfor %}
307 {% endfor %}
308 }
309
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 }};
315 }
316
317 {% endfor %}
318 {% endif %}
319 {% endfor %}
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 }) %}
326 {% endfor %}
327 }
328
329 {% endif %}
330 {% endfor %}
331 {% endfor %}
332
333 #
334 # Mangle rules
335 #
336
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 }) %}
341 {% endfor %}
342 }
343
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 }) %}
348 {% endfor %}
349 }
350
351 chain mangle_input {
352 type filter hook input priority mangle; policy accept;
353 {% for (let rule in fw4.rules("mangle_input")): %}
354 {%+ include("rule.uc", { fw4, rule }) %}
355 {% endfor %}
356 }
357
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 }) %}
362 {% endfor %}
363 }
364
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 }) %}
369 {% endfor %}
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 }) %}
375 {% endfor %}
376 {% endif %}
377 {% endfor %}
378 }
379 }