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