ruleset.uc: consolidate ip and ip6 offload
[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 meta l4proto { tcp, udp } flow offload @ft;
93 {% endif %}
94 ct state established,related accept comment "!fw4: Allow forwarded established and related flows"
95 {% if (fw4.default_option("drop_invalid")): %}
96 ct state invalid drop comment "!fw4: Drop flows with invalid conntrack state"
97 {% endif %}
98 {% for (let rule in fw4.rules("forward")): %}
99 {%+ include("rule.uc", { fw4, rule }) %}
100 {% endfor %}
101 {% for (let zone in fw4.zones()): for (let rule in zone.match_rules): %}
102 {%+ include("zone-match.uc", { fw4, zone, rule, direction: "forward" }) %}
103 {% endfor; endfor %}
104 {% if (fw4.forward_policy() == "reject"): %}
105 jump handle_reject
106 {% endif %}
107 }
108
109 chain output {
110 type filter hook output priority filter; policy {{ fw4.output_policy(true) }};
111
112 oifname "lo" accept comment "!fw4: Accept traffic towards loopback"
113
114 ct state established,related accept comment "!fw4: Allow outbound established and related flows"
115 {% if (fw4.default_option("drop_invalid")): %}
116 ct state invalid drop comment "!fw4: Drop flows with invalid conntrack state"
117 {% endif %}
118 {% for (let rule in fw4.rules("output")): %}
119 {%+ include("rule.uc", { fw4, rule }) %}
120 {% endfor %}
121 {% for (let zone in fw4.zones()): for (let rule in zone.match_rules): %}
122 {%+ include("zone-match.uc", { fw4, zone, rule, direction: "output" }) %}
123 {% endfor; endfor %}
124 {% if (fw4.output_policy() == "reject"): %}
125 jump handle_reject
126 {% endif %}
127 }
128
129 chain handle_reject {
130 meta l4proto tcp reject with {{
131 (fw4.default_option("tcp_reject_code") != "tcp-reset")
132 ? "icmpx type " + fw4.default_option("tcp_reject_code")
133 : "tcp reset"
134 }} comment "!fw4: Reject TCP traffic"
135 reject with {{
136 (fw4.default_option("any_reject_code") != "tcp-reset")
137 ? "icmpx type " + fw4.default_option("any_reject_code")
138 : "tcp reset"
139 }} comment "!fw4: Reject any other traffic"
140 }
141
142 {% if (fw4.default_option("synflood_protect")):
143 let r = fw4.default_option("synflood_rate");
144 let b = fw4.default_option("synflood_burst");
145 %}
146 chain syn_flood {
147 tcp flags & (fin | syn | rst | ack) == syn
148 {%- if (r): %} limit rate {{ r.rate }}/{{ r.unit }}{% endif %}
149 {%- if (b): %} burst {{ b }} packets{% endif %} return comment "!fw4: Accept SYN packets below rate-limit"
150 drop comment "!fw4: Drop excess packets"
151 }
152
153 {% endif %}
154 {% for (let zone in fw4.zones()): %}
155 chain input_{{ zone.name }} {
156 {% for (let rule in fw4.rules("input_"+zone.name)): %}
157 {%+ include("rule.uc", { fw4, rule }) %}
158 {% endfor %}
159 {% if (zone.dflags.dnat): %}
160 ct status dnat accept comment "!fw4: Accept port redirections"
161 {% endif %}
162 jump {{ zone.input }}_from_{{ zone.name }}
163 }
164
165 chain output_{{ zone.name }} {
166 {% for (let rule in fw4.rules("output_"+zone.name)): %}
167 {%+ include("rule.uc", { fw4, rule }) %}
168 {% endfor %}
169 jump {{ zone.output }}_to_{{ zone.name }}
170 }
171
172 chain forward_{{ zone.name }} {
173 {% for (let rule in fw4.rules("forward_"+zone.name)): %}
174 {%+ include("rule.uc", { fw4, rule }) %}
175 {% endfor %}
176 {% if (zone.dflags.dnat): %}
177 ct status dnat accept comment "!fw4: Accept port forwards"
178 {% endif %}
179 jump {{ zone.forward }}_to_{{ zone.name }}
180 }
181
182 {% for (let verdict in ["accept", "reject", "drop"]): %}
183 {% if (zone.sflags[verdict]): %}
184 chain {{ verdict }}_from_{{ zone.name }} {
185 {% for (let rule in zone.match_rules): %}
186 {%+ include("zone-verdict.uc", { fw4, zone, rule, egress: false, verdict }) %}
187 {% endfor %}
188 }
189
190 {% endif %}
191 {% if (zone.dflags[verdict]): %}
192 chain {{ verdict }}_to_{{ zone.name }} {
193 {% for (let rule in zone.match_rules): %}
194 {%+ include("zone-verdict.uc", { fw4, zone, rule, egress: true, verdict }) %}
195 {% endfor %}
196 }
197
198 {% endif %}
199 {% endfor %}
200 {% endfor %}
201
202 #
203 # NAT rules
204 #
205
206 chain dstnat {
207 type nat hook prerouting priority dstnat; policy accept;
208 {% for (let zone in fw4.zones()): %}
209 {% if (zone.dflags.dnat): %}
210 {% for (let rule in zone.match_rules): %}
211 {%+ include("zone-match.uc", { fw4, zone, rule, direction: "dstnat" }) %}
212 {% endfor %}
213 {% endif %}
214 {% endfor %}
215 }
216
217 chain srcnat {
218 type nat hook postrouting priority srcnat; policy accept;
219 {% for (let redirect in fw4.redirects("srcnat")): %}
220 {%+ include("redirect.uc", { fw4, redirect }) %}
221 {% endfor %}
222 {% for (let zone in fw4.zones()): %}
223 {% if (zone.dflags.snat): %}
224 {% for (let rule in zone.match_rules): %}
225 {%+ include("zone-match.uc", { fw4, zone, rule, direction: "srcnat" }) %}
226 {% endfor %}
227 {% endif %}
228 {% endfor %}
229 }
230
231 {% for (let zone in fw4.zones()): %}
232 {% if (zone.dflags.dnat): %}
233 chain dstnat_{{ zone.name }} {
234 {% for (let redirect in fw4.redirects("dstnat_"+zone.name)): %}
235 {%+ include("redirect.uc", { fw4, redirect }) %}
236 {% endfor %}
237 }
238
239 {% endif %}
240 {% if (zone.dflags.snat): %}
241 chain srcnat_{{ zone.name }} {
242 {% for (let redirect in fw4.redirects("srcnat_"+zone.name)): %}
243 {%+ include("redirect.uc", { fw4, redirect }) %}
244 {% endfor %}
245 {% if (zone.masq): %}
246 meta nfproto ipv4 {%+ if (zone.masq4_src_pos): -%}
247 ip saddr {{ fw4.set(zone.masq4_src_pos) }} {%+ endif -%}
248 {%+ if (zone.masq4_src_neg): -%}
249 ip saddr != {{ fw4.set(zone.masq4_src_neg) }} {%+ endif -%}
250 {%+ if (zone.masq4_dest_pos): -%}
251 ip daddr {{ fw4.set(zone.masq4_dest_pos) }} {%+ endif -%}
252 {%+ if (zone.masq4_dest_neg): -%}
253 ip daddr != {{ fw4.set(zone.masq4_dest_neg) }} {%+ endif -%}
254 masquerade comment "!fw4: Masquerade IPv4 {{ zone.name }} traffic"
255 {% endif %}
256 {% if (zone.masq6): %}
257 meta nfproto ipv6 {%+ if (zone.masq6_src_pos): -%}
258 ip6 saddr {{ fw4.set(zone.masq6_src_pos) }} {%+ endif -%}
259 {%+ if (zone.masq6_src_neg): -%}
260 ip6 saddr != {{ fw4.set(zone.masq6_src_neg) }} {%+ endif -%}
261 {%+ if (zone.masq6_dest_pos): -%}
262 ip6 daddr {{ fw4.set(zone.masq6_dest_pos) }} {%+ endif -%}
263 {%+ if (zone.masq6_dest_neg): -%}
264 ip6 daddr != {{ fw4.set(zone.masq6_dest_neg) }} {%+ endif -%}
265 masquerade comment "!fw4: Masquerade IPv6 {{ zone.name }} traffic"
266 {% endif %}
267 }
268
269 {% endif %}
270 {% endfor %}
271
272 #
273 # Raw rules (notrack & helper)
274 #
275
276 chain raw_prerouting {
277 type filter hook prerouting priority raw; policy accept;
278 {% for (let target in ["helper", "notrack"]): %}
279 {% for (let zone in fw4.zones()): %}
280 {% if (zone.dflags[target]): %}
281 {% for (let rule in zone.match_rules): %}
282 {% let devs = fw4.filter_loopback_devs(rule.devices_pos, false); %}
283 {% let nets = fw4.filter_loopback_addrs(rule.subnets_pos, false); %}
284 {% if (rule.devices_neg || rule.subnets_neg || length(devs) || length(nets)): %}
285 {%+ if (rule.family): -%}
286 meta nfproto {{ fw4.nfproto(rule.family) }} {%+ endif -%}
287 {%+ if (length(devs)): -%}
288 iifname {{ fw4.set(devs) }} {%+ endif -%}
289 {%+ if (rule.devices_neg): -%}
290 iifname != {{ fw4.set(rule.devices_neg) }} {%+ endif -%}
291 {%+ if (length(nets)): -%}
292 {{ fw4.ipproto(rule.family) }} saddr {{ fw4.set(nets) }} {%+ endif -%}
293 {%+ if (rule.subnets_neg): -%}
294 {{ fw4.ipproto(rule.family) }} saddr != {{ fw4.set(rule.subnets_neg) }} {%+ endif -%}
295 jump {{ target }}_{{ zone.name }} comment "!fw4: {{ zone.name }} {{ fw4.nfproto(rule.family, true) }} {{
296 (target == "helper") ? "CT helper assignment" : "CT bypass"
297 }}"
298 {% endif %}
299 {% endfor %}
300 {% endif %}
301 {% endfor %}
302 {% endfor %}
303 }
304
305 chain raw_output {
306 type filter hook output priority raw; policy accept;
307 {% for (let target in ["helper", "notrack"]): %}
308 {% for (let zone in fw4.zones()): %}
309 {% if (zone.dflags[target]): %}
310 {% for (let rule in zone.match_rules): %}
311 {% let devs = fw4.filter_loopback_devs(rule.devices_pos, true); %}
312 {% let nets = fw4.filter_loopback_addrs(rule.subnets_pos, true); %}
313 {% if (length(devs) || length(nets)): %}
314 {%+ if (rule.family): -%}
315 meta nfproto {{ fw4.nfproto(rule.family) }} {%+ endif -%}
316 {%+ if (length(devs)): -%}
317 iifname {{ fw4.set(devs) }} {%+ endif -%}
318 {%+ if (length(nets)): -%}
319 {{ fw4.ipproto(rule.family) }} saddr {{ fw4.set(nets) }} {%+ endif -%}
320 jump {{ target }}_{{ zone.name }} comment "!fw4: {{ zone.name }} {{ fw4.nfproto(rule.family, true) }} {{
321 (target == "helper") ? "CT helper assignment" : "CT bypass"
322 }}"
323 {% endif %}
324 {% endfor %}
325 {% endif %}
326 {% endfor %}
327 {% endfor %}
328 }
329
330 {% for (let helper in fw4.helpers()): %}
331 {% if (helper.available): %}
332 {% for (let proto in helper.proto): %}
333 ct helper {{ helper.name }} {
334 type {{ fw4.quote(helper.name, true) }} protocol {{ proto.name }};
335 }
336
337 {% endfor %}
338 {% endif %}
339 {% endfor %}
340 {% for (let target in ["helper", "notrack"]): %}
341 {% for (let zone in fw4.zones()): %}
342 {% if (zone.dflags[target]): %}
343 chain {{ target }}_{{ zone.name }} {
344 {% for (let rule in fw4.rules(target+"_"+zone.name)): %}
345 {%+ include("rule.uc", { fw4, rule }) %}
346 {% endfor %}
347 }
348
349 {% endif %}
350 {% endfor %}
351 {% endfor %}
352
353 #
354 # Mangle rules
355 #
356
357 chain mangle_prerouting {
358 type filter hook prerouting priority mangle; policy accept;
359 {% for (let rule in fw4.rules("mangle_prerouting")): %}
360 {%+ include("rule.uc", { fw4, rule }) %}
361 {% endfor %}
362 }
363
364 chain mangle_output {
365 type filter hook output priority mangle; policy accept;
366 {% for (let rule in fw4.rules("mangle_output")): %}
367 {%+ include("rule.uc", { fw4, rule }) %}
368 {% endfor %}
369 }
370
371 chain mangle_forward {
372 type filter hook forward priority mangle; policy accept;
373 {% for (let zone in fw4.zones()): %}
374 {% if (zone.mtu_fix): %}
375 {% for (let rule in zone.match_rules): %}
376 {%+ include("zone-mssfix.uc", { fw4, zone, rule, egress: false }) %}
377 {%+ include("zone-mssfix.uc", { fw4, zone, rule, egress: true }) %}
378 {% endfor %}
379 {% endif %}
380 {% endfor %}
381 }
382 }