]> git.proxmox.com Git - pve-firewall.git/blame - PVE/Firewall.pm
implement option 'tcpflags' to log illegal combinations of TCP flags
[pve-firewall.git] / PVE / Firewall.pm
CommitLineData
b6360c3f
DM
1package PVE::Firewall;
2
3use warnings;
4use strict;
5use Data::Dumper;
09d5f68e 6use Digest::SHA;
f789653a 7use PVE::Tools;
b6360c3f 8use PVE::QemuServer;
5e1267a5
DM
9use File::Path;
10use IO::File;
ecbea048 11use Net::IP;
06320eb0 12use PVE::Tools qw(run_command lock_file);
ecbea048 13
5e1267a5 14use Data::Dumper;
b6360c3f 15
06320eb0
DM
16my $pve_fw_lock_filename = "/var/lock/pvefw.lck";
17
857f62c8 18# imported/converted from: /usr/share/shorewall/macro.*
961b4928 19my $pve_fw_macros = {
857f62c8
DM
20 'Amanda' => [
21 { action => 'PARAM', proto => 'udp', dport => '10080' },
22 { action => 'PARAM', proto => 'tcp', dport => '10080' },
23 ],
24 'Auth' => [
25 { action => 'PARAM', proto => 'tcp', dport => '113' },
26 ],
27 'BGP' => [
28 { action => 'PARAM', proto => 'tcp', dport => '179' },
29 ],
30 'BitTorrent' => [
961b4928 31 { action => 'PARAM', proto => 'tcp', dport => '6881:6889' },
857f62c8
DM
32 { action => 'PARAM', proto => 'udp', dport => '6881' },
33 ],
34 'BitTorrent32' => [
35 { action => 'PARAM', proto => 'tcp', dport => '6881:6999' },
36 { action => 'PARAM', proto => 'udp', dport => '6881' },
37 ],
38 'CVS' => [
39 { action => 'PARAM', proto => 'tcp', dport => '2401' },
40 ],
41 'Citrix' => [
42 { action => 'PARAM', proto => 'tcp', dport => '1494' },
43 { action => 'PARAM', proto => 'udp', dport => '1604' },
44 { action => 'PARAM', proto => 'tcp', dport => '2598' },
45 ],
46 'DAAP' => [
47 { action => 'PARAM', proto => 'tcp', dport => '3689' },
48 { action => 'PARAM', proto => 'udp', dport => '3689' },
49 ],
50 'DCC' => [
51 { action => 'PARAM', proto => 'tcp', dport => '6277' },
52 ],
53 'DHCPfwd' => [
54 { action => 'PARAM', proto => 'udp', dport => '67:68', sport => '67:68' },
55 { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '67:68', sport => '67:68' },
56 ],
57 'DNS' => [
58 { action => 'PARAM', proto => 'udp', dport => '53' },
59 { action => 'PARAM', proto => 'tcp', dport => '53' },
60 ],
61 'Distcc' => [
62 { action => 'PARAM', proto => 'tcp', dport => '3632' },
63 ],
64 'Edonkey' => [
65 { action => 'PARAM', proto => 'tcp', dport => '4662' },
66 { action => 'PARAM', proto => 'udp', dport => '4665' },
961b4928 67 ],
857f62c8
DM
68 'FTP' => [
69 { action => 'PARAM', proto => 'tcp', dport => '21' },
70 ],
71 'Finger' => [
72 { action => 'PARAM', proto => 'tcp', dport => '79' },
73 ],
74 'GNUnet' => [
75 { action => 'PARAM', proto => 'tcp', dport => '2086' },
76 { action => 'PARAM', proto => 'udp', dport => '2086' },
77 { action => 'PARAM', proto => 'tcp', dport => '1080' },
78 { action => 'PARAM', proto => 'udp', dport => '1080' },
79 ],
80 'GRE' => [
81 { action => 'PARAM', proto => '47' },
82 { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => '47' },
83 ],
84 'Git' => [
85 { action => 'PARAM', proto => 'tcp', dport => '9418' },
86 ],
87 'Gnutella' => [
88 { action => 'PARAM', proto => 'tcp', dport => '6346' },
89 { action => 'PARAM', proto => 'udp', dport => '6346' },
90 ],
91 'HKP' => [
92 { action => 'PARAM', proto => 'tcp', dport => '11371' },
93 ],
94 'HTTP' => [
961b4928
DM
95 { action => 'PARAM', proto => 'tcp', dport => '80' },
96 ],
857f62c8
DM
97 'HTTPS' => [
98 { action => 'PARAM', proto => 'tcp', dport => '443' },
99 ],
100 'ICPV2' => [
101 { action => 'PARAM', proto => 'udp', dport => '3130' },
102 ],
103 'ICQ' => [
104 { action => 'PARAM', proto => 'tcp', dport => '5190' },
105 ],
106 'IMAP' => [
107 { action => 'PARAM', proto => 'tcp', dport => '143' },
108 ],
109 'IMAPS' => [
110 { action => 'PARAM', proto => 'tcp', dport => '993' },
111 ],
112 'IPIP' => [
113 { action => 'PARAM', proto => '94' },
114 { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => '94' },
115 ],
116 'IPP' => [
117 { action => 'PARAM', proto => 'tcp', dport => '631' },
118 ],
119 'IPPbrd' => [
120 { action => 'PARAM', proto => 'udp', dport => '631' },
121 ],
122 'IPPserver' => [
123 { action => 'PARAM', source => 'SOURCE', dest => 'DEST', proto => 'tcp', dport => '631' },
124 { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '631' },
125 ],
126 'IPsec' => [
127 { action => 'PARAM', proto => 'udp', dport => '500', sport => '500' },
128 { action => 'PARAM', proto => '50' },
129 { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '500', sport => '500' },
130 { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => '50' },
131 ],
132 'IPsecah' => [
133 { action => 'PARAM', proto => 'udp', dport => '500', sport => '500' },
134 { action => 'PARAM', proto => '51' },
135 { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '500', sport => '500' },
136 { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => '51' },
137 ],
138 'IPsecnat' => [
139 { action => 'PARAM', proto => 'udp', dport => '500' },
140 { action => 'PARAM', proto => 'udp', dport => '4500' },
141 { action => 'PARAM', proto => '50' },
142 { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '500' },
143 { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '4500' },
144 { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => '50' },
145 ],
146 'IRC' => [
147 { action => 'PARAM', proto => 'tcp', dport => '6667' },
148 ],
149 'JabberPlain' => [
150 { action => 'PARAM', proto => 'tcp', dport => '5222' },
151 ],
152 'JabberSecure' => [
153 { action => 'PARAM', proto => 'tcp', dport => '5223' },
154 ],
155 'Jabberd' => [
156 { action => 'PARAM', proto => 'tcp', dport => '5269' },
157 ],
158 'Jetdirect' => [
159 { action => 'PARAM', proto => 'tcp', dport => '9100' },
160 ],
161 'L2TP' => [
162 { action => 'PARAM', proto => 'udp', dport => '1701' },
163 { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '1701' },
164 ],
165 'LDAP' => [
166 { action => 'PARAM', proto => 'tcp', dport => '389' },
167 ],
168 'LDAPS' => [
169 { action => 'PARAM', proto => 'tcp', dport => '636' },
170 ],
171 'MSNP' => [
172 { action => 'PARAM', proto => 'tcp', dport => '1863' },
173 ],
174 'MSSQL' => [
175 { action => 'PARAM', proto => 'tcp', dport => '1433' },
176 ],
177 'Mail' => [
178 { action => 'PARAM', proto => 'tcp', dport => '25' },
179 { action => 'PARAM', proto => 'tcp', dport => '465' },
180 { action => 'PARAM', proto => 'tcp', dport => '587' },
181 ],
182 'Munin' => [
183 { action => 'PARAM', proto => 'tcp', dport => '4949' },
184 ],
185 'MySQL' => [
186 { action => 'PARAM', proto => 'tcp', dport => '3306' },
187 ],
188 'NNTP' => [
189 { action => 'PARAM', proto => 'tcp', dport => '119' },
190 ],
191 'NNTPS' => [
192 { action => 'PARAM', proto => 'tcp', dport => '563' },
193 ],
194 'NTP' => [
195 { action => 'PARAM', proto => 'udp', dport => '123' },
196 ],
197 'NTPbi' => [
198 { action => 'PARAM', proto => 'udp', dport => '123' },
199 { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '123' },
200 ],
201 'NTPbrd' => [
202 { action => 'PARAM', proto => 'udp', dport => '123' },
203 { action => 'PARAM', proto => 'udp', dport => '1024:65535', sport => '123' },
204 ],
205 'OSPF' => [
206 { action => 'PARAM', proto => '89' },
207 ],
208 'OpenVPN' => [
209 { action => 'PARAM', proto => 'udp', dport => '1194' },
210 ],
211 'PCA' => [
212 { action => 'PARAM', proto => 'udp', dport => '5632' },
213 { action => 'PARAM', proto => 'tcp', dport => '5631' },
214 ],
215 'POP3' => [
216 { action => 'PARAM', proto => 'tcp', dport => '110' },
217 ],
218 'POP3S' => [
219 { action => 'PARAM', proto => 'tcp', dport => '995' },
220 ],
221 'PPtP' => [
222 { action => 'PARAM', proto => '47' },
223 { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => '47' },
224 { action => 'PARAM', proto => 'tcp', dport => '1723' },
225 ],
226 'Ping' => [
227 { action => 'PARAM', proto => 'icmp', dport => '8' },
228 ],
229 'PostgreSQL' => [
230 { action => 'PARAM', proto => 'tcp', dport => '5432' },
231 ],
232 'Printer' => [
233 { action => 'PARAM', proto => 'tcp', dport => '515' },
234 ],
235 'RDP' => [
236 { action => 'PARAM', proto => 'tcp', dport => '3389' },
237 ],
238 'RIPbi' => [
239 { action => 'PARAM', proto => 'udp', dport => '520' },
240 { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '520' },
241 ],
242 'RNDC' => [
243 { action => 'PARAM', proto => 'tcp', dport => '953' },
244 ],
245 'Razor' => [
246 { action => 'ACCEPT', proto => 'tcp', dport => '2703' },
247 ],
248 'Rdate' => [
249 { action => 'PARAM', proto => 'tcp', dport => '37' },
250 ],
251 'Rsync' => [
252 { action => 'PARAM', proto => 'tcp', dport => '873' },
253 ],
254 'SANE' => [
255 { action => 'PARAM', proto => 'tcp', dport => '6566' },
256 ],
257 'SMB' => [
258 { action => 'PARAM', proto => 'udp', dport => '135,445' },
259 { action => 'PARAM', proto => 'udp', dport => '137:139' },
260 { action => 'PARAM', proto => 'udp', dport => '1024:65535', sport => '137' },
261 { action => 'PARAM', proto => 'tcp', dport => '135,139,445' },
262 ],
263 'SMBBI' => [
264 { action => 'PARAM', proto => 'udp', dport => '135,445' },
265 { action => 'PARAM', proto => 'udp', dport => '137:139' },
266 { action => 'PARAM', proto => 'udp', dport => '1024:65535', sport => '137' },
267 { action => 'PARAM', proto => 'tcp', dport => '135,139,445' },
268 { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '135,445' },
269 { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '137:139' },
270 { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '1024:65535', sport => '137' },
271 { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'tcp', dport => '135,139,445' },
272 ],
273 'SMBswat' => [
274 { action => 'PARAM', proto => 'tcp', dport => '901' },
275 ],
276 'SMTP' => [
277 { action => 'PARAM', proto => 'tcp', dport => '25' },
278 ],
279 'SMTPS' => [
280 { action => 'PARAM', proto => 'tcp', dport => '465' },
281 ],
282 'SNMP' => [
283 { action => 'PARAM', proto => 'udp', dport => '161:162' },
284 { action => 'PARAM', proto => 'tcp', dport => '161' },
285 ],
286 'SPAMD' => [
287 { action => 'PARAM', proto => 'tcp', dport => '783' },
288 ],
289 'SSH' => [
290 { action => 'PARAM', proto => 'tcp', dport => '22' },
291 ],
292 'SVN' => [
293 { action => 'PARAM', proto => 'tcp', dport => '3690' },
294 ],
295 'SixXS' => [
296 { action => 'PARAM', proto => 'tcp', dport => '3874' },
297 { action => 'PARAM', proto => 'udp', dport => '3740' },
298 { action => 'PARAM', proto => '41' },
299 { action => 'PARAM', proto => 'udp', dport => '5072,8374' },
300 ],
301 'Squid' => [
302 { action => 'PARAM', proto => 'tcp', dport => '3128' },
303 ],
304 'Submission' => [
305 { action => 'PARAM', proto => 'tcp', dport => '587' },
306 ],
307 'Syslog' => [
308 { action => 'PARAM', proto => 'udp', dport => '514' },
309 { action => 'PARAM', proto => 'tcp', dport => '514' },
310 ],
311 'TFTP' => [
312 { action => 'PARAM', proto => 'udp', dport => '69' },
313 ],
314 'Telnet' => [
315 { action => 'PARAM', proto => 'tcp', dport => '23' },
316 ],
317 'Telnets' => [
318 { action => 'PARAM', proto => 'tcp', dport => '992' },
319 ],
320 'Time' => [
321 { action => 'PARAM', proto => 'tcp', dport => '37' },
322 ],
323 'Trcrt' => [
324 { action => 'PARAM', proto => 'udp', dport => '33434:33524' },
325 { action => 'PARAM', proto => 'icmp', dport => '8' },
326 ],
327 'VNC' => [
328 { action => 'PARAM', proto => 'tcp', dport => '5900:5909' },
329 ],
330 'VNCL' => [
331 { action => 'PARAM', proto => 'tcp', dport => '5500' },
332 ],
333 'Web' => [
334 { action => 'PARAM', proto => 'tcp', dport => '80' },
961b4928
DM
335 { action => 'PARAM', proto => 'tcp', dport => '443' },
336 ],
857f62c8
DM
337 'Webcache' => [
338 { action => 'PARAM', proto => 'tcp', dport => '8080' },
339 ],
340 'Webmin' => [
341 { action => 'PARAM', proto => 'tcp', dport => '10000' },
342 ],
343 'Whois' => [
344 { action => 'PARAM', proto => 'tcp', dport => '43' },
345 ],
961b4928
DM
346};
347
348my $pve_fw_parsed_macros;
349my $pve_fw_preferred_macro_names = {};
3a616aa0 350
d8f2505e
DM
351my $pve_std_chains = {
352 'PVEFW-SET-ACCEPT-MARK' => [
353 "-j MARK --set-mark 1",
354 ],
79929d9c
DM
355 'PVEFW-DropBroadcast' => [
356 # same as shorewall 'Broadcast'
357 # simply DROP BROADCAST/MULTICAST/ANYCAST
358 # we can use this to reduce logging
359 { action => 'DROP', dsttype => 'BROADCAST' },
360 { action => 'DROP', dsttype => 'MULTICAST' },
361 { action => 'DROP', dsttype => 'ANYCAST' },
362 { action => 'DROP', dest => '224.0.0.0/4' },
363 ],
364 'PVEFW-reject' => [
365 # same as shorewall 'reject'
366 { action => 'DROP', dsttype => 'BROADCAST' },
367 { action => 'DROP', source => '224.0.0.0/4' },
368 { action => 'DROP', proto => 'icmp' },
369 "-p tcp -j REJECT --reject-with tcp-reset",
370 "-p udp -j REJECT --reject-with icmp-port-unreachable",
371 "-p icmp -j REJECT --reject-with icmp-host-unreachable",
372 "-j REJECT --reject-with icmp-host-prohibited",
373 ],
374 'PVEFW-Drop' => [
375 # same as shorewall 'Drop', which is equal to DROP,
376 # but REJECT/DROP some packages to reduce logging,
377 # and ACCEPT critical ICMP types
378 { action => 'PVEFW-reject', proto => 'tcp', dport => '43' }, # REJECT 'auth'
379 # we are not interested in BROADCAST/MULTICAST/ANYCAST
380 { action => 'PVEFW-DropBroadcast' },
381 # ACCEPT critical ICMP types
382 { action => 'ACCEPT', proto => 'icmp', dport => 'fragmentation-needed' },
383 { action => 'ACCEPT', proto => 'icmp', dport => 'time-exceeded' },
384 # Drop packets with INVALID state
385 "-m conntrack --ctstate INVALID -j DROP",
386 # Drop Microsoft SMB noise
387 { action => 'DROP', proto => 'udp', dport => '135,445', nbdport => 2 },
388 { action => 'DROP', proto => 'udp', dport => '137:139'},
389 { action => 'DROP', proto => 'udp', dport => '1024:65535', sport => 137 },
390 { action => 'DROP', proto => 'tcp', dport => '135,139,445', nbdport => 3 },
391 { action => 'DROP', proto => 'udp', dport => 1900 }, # UPnP
392 # Drop new/NotSyn traffic so that it doesn't get logged
393 "-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -j DROP",
394 # Drop DNS replies
395 { action => 'DROP', proto => 'udp', sport => 53 },
396 ],
397 'PVEFW-Reject' => [
398 # same as shorewall 'Reject', which is equal to Reject,
399 # but REJECT/DROP some packages to reduce logging,
400 # and ACCEPT critical ICMP types
401 { action => 'PVEFW-reject', proto => 'tcp', dport => '43' }, # REJECT 'auth'
402 # we are not interested in BROADCAST/MULTICAST/ANYCAST
403 { action => 'PVEFW-DropBroadcast' },
404 # ACCEPT critical ICMP types
405 { action => 'ACCEPT', proto => 'icmp', dport => 'fragmentation-needed' },
406 { action => 'ACCEPT', proto => 'icmp', dport => 'time-exceeded' },
407 # Drop packets with INVALID state
408 "-m conntrack --ctstate INVALID -j DROP",
409 # Drop Microsoft SMB noise
410 { action => 'PVEFW-reject', proto => 'udp', dport => '135,445', nbdport => 2 },
411 { action => 'PVEFW-reject', proto => 'udp', dport => '137:139'},
412 { action => 'PVEFW-reject', proto => 'udp', dport => '1024:65535', sport => 137 },
413 { action => 'PVEFW-reject', proto => 'tcp', dport => '135,139,445', nbdport => 3 },
414 { action => 'DROP', proto => 'udp', dport => 1900 }, # UPnP
415 # Drop new/NotSyn traffic so that it doesn't get logged
416 "-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -j DROP",
417 # Drop DNS replies
418 { action => 'DROP', proto => 'udp', sport => 53 },
419 ],
d86659ef
DM
420 'PVEFW-logflags' => [
421 # same as shorewall logflags action.
422 "-j LOG --log-prefix \"logflags-dropped:\" --log-level 4 --log-ip-options",
423 "-j DROP",
424 ],
425 'PVEFW-tcpflags' => [
426 # same as shorewall tcpflags action.
427 # Packets arriving on this interface are checked for som illegal combinations of TCP flags
428 "-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG FIN,PSH,URG -g PVEFW-logflags",
429 "-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG NONE -g PVEFW-logflags",
430 "-p tcp -m tcp --tcp-flags SYN,RST SYN,RST -g PVEFW-logflags",
431 "-p tcp -m tcp --tcp-flags FIN,SYN FIN,SYN -g PVEFW-logflags",
432 "-p tcp -m tcp --sport 0 --tcp-flags FIN,SYN,RST,ACK SYN -g PVEFW-logflags",
433 ],
d8f2505e
DM
434};
435
4b586518
DM
436# iptables -p icmp -h
437my $icmp_type_names = {
438 any => 1,
439 'echo-reply' => 1,
440 'destination-unreachable' => 1,
441 'network-unreachable' => 1,
442 'host-unreachable' => 1,
443 'protocol-unreachable' => 1,
444 'port-unreachable' => 1,
445 'fragmentation-needed' => 1,
446 'source-route-failed' => 1,
447 'network-unknown' => 1,
448 'host-unknown' => 1,
449 'network-prohibited' => 1,
450 'host-prohibited' => 1,
451 'TOS-network-unreachable' => 1,
452 'TOS-host-unreachable' => 1,
453 'communication-prohibited' => 1,
454 'host-precedence-violation' => 1,
455 'precedence-cutoff' => 1,
456 'source-quench' => 1,
457 'redirect' => 1,
458 'network-redirect' => 1,
459 'host-redirect' => 1,
460 'TOS-network-redirect' => 1,
461 'TOS-host-redirect' => 1,
462 'echo-request' => 1,
463 'router-advertisement' => 1,
464 'router-solicitation' => 1,
465 'time-exceeded' => 1,
466 'ttl-zero-during-transit' => 1,
467 'ttl-zero-during-reassembly' => 1,
468 'parameter-problem' => 1,
469 'ip-header-bad' => 1,
470 'required-option-missing' => 1,
471 'timestamp-request' => 1,
472 'timestamp-reply' => 1,
473 'address-mask-request' => 1,
474 'address-mask-reply' => 1,
475};
476
a332200b 477sub get_firewall_macros {
9aab3127 478
961b4928 479 return $pve_fw_parsed_macros if $pve_fw_parsed_macros;
6158271d 480
961b4928 481 $pve_fw_parsed_macros = {};
9aab3127 482
961b4928
DM
483 foreach my $k (keys %$pve_fw_macros) {
484 my $name = lc($k);
a332200b 485
961b4928
DM
486 my $macro = $pve_fw_macros->{$k};
487 $pve_fw_preferred_macro_names->{$name} = $k;
488 $pve_fw_parsed_macros->{$name} = $macro;
489 }
a332200b 490
961b4928 491 return $pve_fw_parsed_macros;
9aab3127
DM
492}
493
fcba0beb
DM
494my $etc_services;
495
496sub get_etc_services {
497
498 return $etc_services if $etc_services;
499
500 my $filename = "/etc/services";
501
502 my $fh = IO::File->new($filename, O_RDONLY);
503 if (!$fh) {
504 warn "unable to read '$filename' - $!\n";
505 return {};
506 }
507
508 my $services = {};
509
510 while (my $line = <$fh>) {
511 chomp ($line);
512 next if $line =~m/^#/;
513 next if ($line =~m/^\s*$/);
514
515 if ($line =~ m!^(\S+)\s+(\S+)/(tcp|udp).*$!) {
516 $services->{byid}->{$2}->{name} = $1;
35b66e9d 517 $services->{byid}->{$2}->{port} = $2;
fcba0beb
DM
518 $services->{byid}->{$2}->{$3} = 1;
519 $services->{byname}->{$1} = $services->{byid}->{$2};
520 }
521 }
522
523 close($fh);
524
6158271d
DM
525 $etc_services = $services;
526
3a616aa0 527
fcba0beb
DM
528 return $etc_services;
529}
530
531my $etc_protocols;
532
533sub get_etc_protocols {
534 return $etc_protocols if $etc_protocols;
535
536 my $filename = "/etc/protocols";
537
538 my $fh = IO::File->new($filename, O_RDONLY);
539 if (!$fh) {
540 warn "unable to read '$filename' - $!\n";
541 return {};
542 }
543
544 my $protocols = {};
545
546 while (my $line = <$fh>) {
547 chomp ($line);
548 next if $line =~m/^#/;
549 next if ($line =~m/^\s*$/);
550
551 if ($line =~ m!^(\S+)\s+(\d+)\s+.*$!) {
552 $protocols->{byid}->{$2}->{name} = $1;
553 $protocols->{byname}->{$1} = $protocols->{byid}->{$2};
554 }
555 }
556
557 close($fh);
558
559 $etc_protocols = $protocols;
560
561 return $etc_protocols;
562}
563
ecbea048
DM
564sub parse_address_list {
565 my ($str) = @_;
566
d6de1dc2 567 my $nbaor = 0;
ecbea048
DM
568 foreach my $aor (split(/,/, $str)) {
569 if (!Net::IP->new($aor)) {
570 my $err = Net::IP::Error();
571 die "invalid IP address: $err\n";
d6de1dc2
AD
572 }else{
573 $nbaor++;
ecbea048
DM
574 }
575 }
d6de1dc2 576 return $nbaor;
ecbea048 577}
dddd9413 578
fcba0beb
DM
579sub parse_port_name_number_or_range {
580 my ($str) = @_;
581
582 my $services = PVE::Firewall::get_etc_services();
4cdbb3b7 583 my $nbports = 0;
fcba0beb 584 foreach my $item (split(/,/, $str)) {
4cdbb3b7 585 my $portlist = "";
35b66e9d 586 my $oldpon = undef;
4b586518 587 $nbports++;
fcba0beb 588 foreach my $pon (split(':', $item, 2)) {
35b66e9d 589 $pon = $services->{byname}->{$pon}->{port} if $services->{byname}->{$pon}->{port};
4cdbb3b7 590 if ($pon =~ m/^\d+$/){
857f62c8 591 die "invalid port '$pon'\n" if $pon < 0 && $pon > 65535;
35b66e9d
AD
592 die "port '$pon' must be bigger than port '$oldpon' \n" if $oldpon && ($pon < $oldpon);
593 $oldpon = $pon;
4cdbb3b7
AD
594 }else{
595 die "invalid port $services->{byname}->{$pon}\n" if !$services->{byname}->{$pon};
596 }
fcba0beb
DM
597 }
598 }
599
4cdbb3b7 600 return ($nbports);
fcba0beb
DM
601}
602
780bcc0f
DM
603my $bridge_firewall_enabled = 0;
604
605sub enable_bridge_firewall {
606
607 return if $bridge_firewall_enabled; # only once
608
609 system("echo 1 > /proc/sys/net/bridge/bridge-nf-call-iptables");
610 system("echo 1 > /proc/sys/net/bridge/bridge-nf-call-ip6tables");
611
612 $bridge_firewall_enabled = 1;
613}
614
8cebfa6f 615my $rule_format = "%-15s %-30s %-30s %-15s %-15s %-15s\n";
dddd9413 616
3a616aa0
AD
617sub iptables {
618 my ($cmd) = @_;
619
620 run_command("/sbin/iptables $cmd", outfunc => sub {}, errfunc => sub {});
621}
622
b16e818e
DM
623sub iptables_restore_cmdlist {
624 my ($cmdlist) = @_;
3a616aa0 625
3fa83edf 626 run_command("/sbin/iptables-restore -n", input => $cmdlist);
3a616aa0
AD
627}
628
de2a57cd
DM
629sub iptables_get_chains {
630
631 my $res = {};
632
633 # check what chains we want to track
634 my $is_pvefw_chain = sub {
635 my $name = shift;
636
dec84fcd
DM
637 return 1 if $name =~ m/^PVEFW-\S+$/;
638
de2a57cd 639 return 1 if $name =~ m/^tap\d+i\d+-(:?IN|OUT)$/;
5927497d 640 return 1 if $name =~ m/^vmbr\d+-(:?FW|IN|OUT)$/;
f50656c2 641 return 1 if $name =~ m/^GROUP-(:?[^\s\-]+)-(:?IN|OUT)$/;
de2a57cd
DM
642
643 return undef;
644 };
645
646 my $table = '';
647
de2a57cd
DM
648 my $parser = sub {
649 my $line = shift;
650
651 return if $line =~ m/^#/;
652 return if $line =~ m/^\s*$/;
653
654 if ($line =~ m/^\*(\S+)$/) {
655 $table = $1;
656 return;
657 }
658
659 return if $table ne 'filter';
660
661 if ($line =~ m/^:(\S+)\s/) {
662 my $chain = $1;
663 return if !&$is_pvefw_chain($chain);
3fa83edf 664 $res->{$chain} = "unknown";
09d5f68e 665 } elsif ($line =~ m/^-A\s+(\S+)\s.*--comment\s+\"PVESIG:(\S+)\"/) {
3fa83edf 666 my ($chain, $sig) = ($1, $2);
de2a57cd 667 return if !&$is_pvefw_chain($chain);
3fa83edf 668 $res->{$chain} = $sig;
de2a57cd
DM
669 } else {
670 # simply ignore the rest
671 return;
672 }
673 };
674
675 run_command("/sbin/iptables-save", outfunc => $parser);
676
de2a57cd
DM
677 return $res;
678}
679
3a616aa0
AD
680sub iptables_chain_exist {
681 my ($chain) = @_;
682
683 eval{
684 iptables("-n --list $chain");
685 };
686 return undef if $@;
687
688 return 1;
689}
690
691sub iptables_rule_exist {
692 my ($rule) = @_;
693
694 eval{
695 iptables("-C $rule");
696 };
697 return undef if $@;
698
699 return 1;
700}
701
3fa83edf 702sub ruleset_generate_rule {
4e6112f9 703 my ($ruleset, $chain, $rule, $goto) = @_;
3a616aa0 704
3fa83edf 705 my $cmd = '';
3a616aa0 706
d6de1dc2 707 $cmd .= " -m iprange --src-range" if $rule->{nbsource} && $rule->{nbsource} > 1;
3a616aa0 708 $cmd .= " -s $rule->{source}" if $rule->{source};
d6de1dc2 709 $cmd .= " -m iprange --dst-range" if $rule->{nbdest} && $rule->{nbdest} > 1;
8c32a215 710 $cmd .= " -d $rule->{dest}" if $rule->{dest};
3a616aa0 711 $cmd .= " -p $rule->{proto}" if $rule->{proto};
4b586518
DM
712
713 if (($rule->{nbdport} && $rule->{nbdport} > 1) ||
714 ($rule->{nbsport} && $rule->{nbsport} > 1)) {
715 $cmd .= " --match multiport"
716 }
717
718 if ($rule->{dport}) {
719 if ($rule->{proto} && $rule->{proto} eq 'icmp') {
720 # Note: we use dport to store --icmp-type
721 die "unknown icmp-type\n" if !$icmp_type_names->{$rule->{dport}};
722 $cmd .= " -m icmp --icmp-type $rule->{dport}";
723 } else {
724 if ($rule->{nbdport} && $rule->{nbdport} > 1) {
725 $cmd .= " --dports $rule->{dport}";
726 } else {
727 $cmd .= " --dport $rule->{dport}";
728 }
729 }
730 }
731
732 if ($rule->{sport}) {
733 if ($rule->{nbsport} && $rule->{nbsport} > 1) {
734 $cmd .= " --sports $rule->{sport}";
735 } else {
736 $cmd .= " --sport $rule->{sport}";
737 }
738 }
739
740 $cmd .= " -m addrtype --dst-type $rule->{dsttype}" if $rule->{dsttype};
4e6112f9
DM
741
742 if (my $action = $rule->{action}) {
743 $goto = 1 if !defined($goto) && $action eq 'PVEFW-SET-ACCEPT-MARK';
744 $cmd .= $goto ? " -g $action" : " -j $action";
745 };
3a616aa0 746
3fa83edf
DM
747 ruleset_addrule($ruleset, $chain, $cmd) if $cmd;
748}
749
750sub ruleset_create_chain {
751 my ($ruleset, $chain) = @_;
3a616aa0 752
d050c724
DM
753 die "Invalid chain name '$chain' (28 char max)\n" if length($chain) > 28;
754
3fa83edf
DM
755 die "chain '$chain' already exists\n" if $ruleset->{$chain};
756
757 $ruleset->{$chain} = [];
3a616aa0
AD
758}
759
3fa83edf
DM
760sub ruleset_chain_exist {
761 my ($ruleset, $chain) = @_;
3a616aa0 762
3fa83edf
DM
763 return $ruleset->{$chain} ? 1 : undef;
764}
3a616aa0 765
3fa83edf
DM
766sub ruleset_addrule {
767 my ($ruleset, $chain, $rule) = @_;
3a616aa0 768
3fa83edf 769 die "no such chain '$chain'\n" if !$ruleset->{$chain};
3a616aa0 770
3fa83edf
DM
771 push @{$ruleset->{$chain}}, "-A $chain $rule";
772}
3a616aa0 773
3fa83edf
DM
774sub ruleset_insertrule {
775 my ($ruleset, $chain, $rule) = @_;
3a616aa0 776
3fa83edf 777 die "no such chain '$chain'\n" if !$ruleset->{$chain};
3a616aa0 778
3fa83edf
DM
779 unshift @{$ruleset->{$chain}}, "-A $chain $rule";
780}
3a616aa0 781
3fa83edf
DM
782sub generate_bridge_chains {
783 my ($ruleset, $bridge) = @_;
3a616aa0 784
dec84fcd
DM
785 if (!ruleset_chain_exist($ruleset, "PVEFW-FORWARD")){
786 ruleset_create_chain($ruleset, "PVEFW-FORWARD");
7fab95bc 787 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT");
3a616aa0
AD
788 }
789
5927497d
DM
790 if (!ruleset_chain_exist($ruleset, "$bridge-FW")) {
791 ruleset_create_chain($ruleset, "$bridge-FW");
792 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-o $bridge -m physdev --physdev-is-bridged -j $bridge-FW");
793 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-i $bridge -m physdev --physdev-is-bridged -j $bridge-FW");
9fcad984
AD
794 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-o $bridge -j DROP"); # disable interbridge routing
795 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-i $bridge -j DROP"); # disable interbridge routing
3fa83edf 796 }
3a616aa0 797
3fa83edf
DM
798 if (!ruleset_chain_exist($ruleset, "$bridge-OUT")) {
799 ruleset_create_chain($ruleset, "$bridge-OUT");
5927497d 800 ruleset_addrule($ruleset, "$bridge-FW", "-m physdev --physdev-is-bridged --physdev-is-in -j $bridge-OUT");
9fcad984
AD
801 }
802
803 if (!ruleset_chain_exist($ruleset, "$bridge-IN")) {
804 ruleset_create_chain($ruleset, "$bridge-IN");
5927497d 805 ruleset_addrule($ruleset, "$bridge-FW", "-m physdev --physdev-is-bridged --physdev-is-out -j $bridge-IN");
fdb0bf20 806 ruleset_addrule($ruleset, "$bridge-FW", "-m mark --mark 1 -j ACCEPT");
3fa83edf
DM
807 }
808}
3a616aa0
AD
809
810sub generate_tap_rules_direction {
fa9c4a6f
DM
811 my ($ruleset, $group_rules, $iface, $netid, $macaddr, $vmfw_conf, $bridge, $direction) = @_;
812
813 my $rules = $vmfw_conf->{lc($direction)};
814 my $options = $vmfw_conf->{options};
3a616aa0
AD
815
816 my $tapchain = "$iface-$direction";
817
3fa83edf 818 ruleset_create_chain($ruleset, $tapchain);
3a616aa0 819
7fab95bc
DM
820 ruleset_addrule($ruleset, $tapchain, "-m conntrack --ctstate INVALID -j DROP");
821 ruleset_addrule($ruleset, $tapchain, "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT");
3a616aa0 822
9c6b6efd
DM
823 if ($direction eq 'OUT' && defined($macaddr) &&
824 !(defined($options->{macfilter}) && $options->{macfilter} == 0)) {
c29f55c9
DM
825 ruleset_addrule($ruleset, $tapchain, "-m mac ! --mac-source $macaddr -j DROP");
826 }
827
d86659ef
DM
828 if ($options->{tcpflags}) {
829 ruleset_addrule($ruleset, $tapchain, "-p tcp -j PVEFW-tcpflags");
830 }
831
3fa83edf 832 if ($rules) {
3a616aa0
AD
833 foreach my $rule (@$rules) {
834 next if $rule->{iface} && $rule->{iface} ne $netid;
bfbfa8b6 835 # we go to $bridge-IN if accept in out rules
3a616aa0 836 if($rule->{action} =~ m/^(GROUP-(\S+))$/){
3fa83edf
DM
837 $rule->{action} .= "-$direction";
838 # generate empty group rule if don't exist
839 if(!ruleset_chain_exist($ruleset, $rule->{action})){
51bae274 840 generate_group_rules($ruleset, $group_rules, $2);
3fa83edf 841 }
4e6112f9 842 ruleset_generate_rule($ruleset, $tapchain, $rule);
7b291cab 843 ruleset_addrule($ruleset, $tapchain, "-m mark --mark 1 -j RETURN")
31ff3ef6 844 if $direction eq 'OUT';
4e6112f9 845 } else {
fdb0bf20 846 $rule->{action} = "PVEFW-SET-ACCEPT-MARK" if $rule->{action} eq 'ACCEPT' && $direction eq 'OUT';
4e6112f9 847 ruleset_generate_rule($ruleset, $tapchain, $rule);
3a616aa0 848 }
4e6112f9 849 }
3a616aa0
AD
850 }
851
ccae0b50
DM
852 # implement policy
853 my $policy;
854
855 if ($direction eq 'OUT') {
856 $policy = $options->{'policy-out'} || 'ACCEPT'; # allow everything by default
857 } else {
858 $policy = $options->{'policy-in'} || 'DROP'; # allow everything by default
859 }
860
861 if ($policy eq 'ACCEPT') {
862 if ($direction eq 'OUT') {
fdb0bf20 863 ruleset_addrule($ruleset, $tapchain, "-g PVEFW-SET-ACCEPT-MARK");
ccae0b50
DM
864 } else {
865 ruleset_addrule($ruleset, $tapchain, "-j ACCEPT");
866 }
867 } elsif ($policy eq 'DROP') {
25c7b224 868 ruleset_addrule($ruleset, $tapchain, "-j PVEFW-Drop");
ccae0b50
DM
869 ruleset_addrule($ruleset, $tapchain, "-j LOG --log-prefix \"$tapchain-dropped: \" --log-level 4");
870 ruleset_addrule($ruleset, $tapchain, "-j DROP");
871 } elsif ($policy eq 'REJECT') {
25c7b224 872 ruleset_addrule($ruleset, $tapchain, "-j PVEFW-Reject");
ccae0b50 873 ruleset_addrule($ruleset, $tapchain, "-j LOG --log-prefix \"$tapchain-reject: \" --log-level 4");
25c7b224 874 ruleset_addrule($ruleset, $tapchain, "-g PVEFW-reject");
ccae0b50
DM
875 } else {
876 # should not happen
877 die "internal error: unknown policy '$policy'";
878 }
3a616aa0 879
3fa83edf
DM
880 # plug the tap chain to bridge chain
881 my $physdevdirection = $direction eq 'IN' ? "out" : "in";
882 my $rule = "-m physdev --physdev-$physdevdirection $iface --physdev-is-bridged -j $tapchain";
883 ruleset_insertrule($ruleset, "$bridge-$direction", $rule);
3a616aa0 884
3fa83edf
DM
885 if ($direction eq 'OUT'){
886 # add tap->host rules
887 my $rule = "-m physdev --physdev-$physdevdirection $iface -j $tapchain";
dec84fcd 888 ruleset_addrule($ruleset, "PVEFW-INPUT", $rule);
3a616aa0
AD
889 }
890}
891
0bd5f137 892sub enablehostfw {
51bae274 893 my ($ruleset, $rules, $group_rules) = @_;
0bd5f137 894
51bae274 895 # fixme: allow security groups
0bd5f137 896
3fa83edf 897 # host inbound firewall
dec84fcd
DM
898 my $chain = "PVEFW-HOST-IN";
899 ruleset_create_chain($ruleset, $chain);
0bd5f137 900
7fab95bc
DM
901 ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate INVALID -j DROP");
902 ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT");
dec84fcd
DM
903 ruleset_addrule($ruleset, $chain, "-i lo -j ACCEPT");
904 ruleset_addrule($ruleset, $chain, "-m addrtype --dst-type MULTICAST -j ACCEPT");
7fab95bc 905 ruleset_addrule($ruleset, $chain, "-p udp -m conntrack --ctstate NEW -m multiport --dports 5404,5405 -j ACCEPT");
dec84fcd 906 ruleset_addrule($ruleset, $chain, "-p udp -m udp --dport 9000 -j ACCEPT"); #corosync
0bd5f137 907
3fa83edf
DM
908 if ($rules->{in}) {
909 foreach my $rule (@{$rules->{in}}) {
910 # we use RETURN because we need to check also tap rules
911 $rule->{action} = 'RETURN' if $rule->{action} eq 'ACCEPT';
dec84fcd 912 ruleset_generate_rule($ruleset, $chain, $rule);
3fa83edf 913 }
0bd5f137
AD
914 }
915
dec84fcd
DM
916 ruleset_addrule($ruleset, $chain, "-j LOG --log-prefix \"kvmhost-IN dropped: \" --log-level 4");
917 ruleset_addrule($ruleset, $chain, "-j DROP");
0bd5f137 918
3fa83edf 919 # host outbound firewall
aadd745e 920 $chain = "PVEFW-HOST-OUT";
dec84fcd
DM
921 ruleset_create_chain($ruleset, $chain);
922
7fab95bc
DM
923 ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate INVALID -j DROP");
924 ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT");
dec84fcd
DM
925 ruleset_addrule($ruleset, $chain, "-o lo -j ACCEPT");
926 ruleset_addrule($ruleset, $chain, "-m addrtype --dst-type MULTICAST -j ACCEPT");
7fab95bc 927 ruleset_addrule($ruleset, $chain, "-p udp -m conntrack --ctstate NEW -m multiport --dports 5404,5405 -j ACCEPT");
dec84fcd 928 ruleset_addrule($ruleset, $chain, "-p udp -m udp --dport 9000 -j ACCEPT"); #corosync
0bd5f137 929
3fa83edf
DM
930 if ($rules->{out}) {
931 foreach my $rule (@{$rules->{out}}) {
932 # we use RETURN because we need to check also tap rules
933 $rule->{action} = 'RETURN' if $rule->{action} eq 'ACCEPT';
dec84fcd 934 ruleset_generate_rule($ruleset, $chain, $rule);
3fa83edf 935 }
0bd5f137
AD
936 }
937
dec84fcd
DM
938 ruleset_addrule($ruleset, $chain, "-j LOG --log-prefix \"kvmhost-OUT dropped: \" --log-level 4");
939 ruleset_addrule($ruleset, $chain, "-j DROP");
6158271d 940
dec84fcd
DM
941 ruleset_addrule($ruleset, "PVEFW-OUTPUT", "-j PVEFW-HOST-OUT");
942 ruleset_addrule($ruleset, "PVEFW-INPUT", "-j PVEFW-HOST-IN");
9d31b418
AD
943}
944
945sub generate_group_rules {
51bae274 946 my ($ruleset, $group_rules, $group) = @_;
9d31b418 947
51bae274 948 my $rules = $group_rules->{$group};
9d31b418 949
51bae274 950 die "no such security group '$group'\n" if !$rules;
6158271d 951
3fa83edf 952 my $chain = "GROUP-${group}-IN";
9d31b418 953
3fa83edf 954 ruleset_create_chain($ruleset, $chain);
9d31b418 955
3fa83edf
DM
956 if ($rules->{in}) {
957 foreach my $rule (@{$rules->{in}}) {
958 ruleset_generate_rule($ruleset, $chain, $rule);
9d31b418
AD
959 }
960 }
961
3fa83edf 962 $chain = "GROUP-${group}-OUT";
9d31b418 963
3fa83edf 964 ruleset_create_chain($ruleset, $chain);
4e6112f9 965 ruleset_addrule($ruleset, $chain, "-j MARK --set-mark 0"); # clear mark
9d31b418 966
3fa83edf
DM
967 if ($rules->{out}) {
968 foreach my $rule (@{$rules->{out}}) {
6158271d
DM
969 # we go the PVEFW-SET-ACCEPT-MARK Instead of ACCEPT) because we need to
970 # check also other tap rules (and group rules can be set on any bridge,
4e6112f9
DM
971 # so we can't go to VMBRXX-IN)
972 $rule->{action} = 'PVEFW-SET-ACCEPT-MARK' if $rule->{action} eq 'ACCEPT';
6a6c7af7 973 ruleset_generate_rule($ruleset, $chain, $rule);
9d31b418
AD
974 }
975 }
9d31b418
AD
976}
977
51bae274
DM
978my $MAX_NETS = 32;
979my $valid_netdev_names = {};
980for (my $i = 0; $i < $MAX_NETS; $i++) {
981 $valid_netdev_names->{"net$i"} = 1;
982}
5e1267a5 983
51bae274
DM
984sub parse_fw_rule {
985 my ($line, $need_iface, $allow_groups) = @_;
5e1267a5 986
a332200b 987 my $macros = get_firewall_macros();
fcba0beb 988 my $protocols = get_etc_protocols();
51bae274
DM
989
990 my ($action, $iface, $source, $dest, $proto, $dport, $sport);
991
992 $line =~ s/#.*$//;
993
994 my @data = split(/\s+/, $line);
995 my $expected_elements = $need_iface ? 7 : 6;
996
997 die "wrong number of rule elements\n" if scalar(@data) > $expected_elements;
998
999 if ($need_iface) {
1000 ($action, $iface, $source, $dest, $proto, $dport, $sport) = @data
1001 } else {
1002 ($action, $source, $dest, $proto, $dport, $sport) = @data;
1003 }
1004
1005 die "incomplete rule\n" if !$action;
1006
961b4928
DM
1007 my $macro;
1008 my $macro_name;
1009
51bae274
DM
1010 if ($action =~ m/^(ACCEPT|DROP|REJECT)$/) {
1011 # OK
1012 } elsif ($allow_groups && $action =~ m/^GROUP-(:?\S+)$/) {
1013 # OK
1014 } elsif ($action =~ m/^(\S+)\((ACCEPT|DROP|REJECT)\)$/) {
961b4928
DM
1015 ($macro_name, $action) = ($1, $2);
1016 my $lc_macro_name = lc($macro_name);
1017 my $preferred_name = $pve_fw_preferred_macro_names->{$lc_macro_name};
1018 $macro_name = $preferred_name if $preferred_name;
1019 $macro = $macros->{$lc_macro_name};
1020 die "unknown macro '$macro_name'\n" if !$macro;
51bae274
DM
1021 } else {
1022 die "unknown action '$action'\n";
1023 }
1024
1025 if ($need_iface) {
1026 $iface = undef if $iface && $iface eq '-';
6158271d 1027 die "unknown interface '$iface'\n"
51bae274
DM
1028 if defined($iface) && !$valid_netdev_names->{$iface};
1029 }
1030
1031 $proto = undef if $proto && $proto eq '-';
6158271d 1032 die "unknown protokol '$proto'\n" if $proto &&
51bae274
DM
1033 !(defined($protocols->{byname}->{$proto}) ||
1034 defined($protocols->{byid}->{$proto}));
1035
1036 $source = undef if $source && $source eq '-';
1037 $dest = undef if $dest && $dest eq '-';
1038
1039 $dport = undef if $dport && $dport eq '-';
1040 $sport = undef if $sport && $sport eq '-';
1041
51bae274
DM
1042 my $nbsource = undef;
1043 my $nbdest = undef;
1044
1045 $nbsource = parse_address_list($source) if $source;
1046 $nbdest = parse_address_list($dest) if $dest;
6158271d 1047
961b4928 1048 my $rules = [];
51bae274 1049
961b4928 1050 my $param = {
51bae274 1051 action => $action,
51bae274
DM
1052 iface => $iface,
1053 source => $source,
1054 dest => $dest,
1055 nbsource => $nbsource,
1056 nbdest => $nbdest,
1057 proto => $proto,
1058 dport => $dport,
1059 sport => $sport,
51bae274 1060 };
961b4928
DM
1061
1062 if ($macro) {
1063 foreach my $templ (@$macro) {
1064 my $rule = {};
1065 foreach my $k (keys %$templ) {
1066 my $v = $templ->{$k};
857f62c8
DM
1067 if ($v eq 'PARAM') {
1068 $v = $param->{$k};
1069 } elsif ($v eq 'DEST') {
1070 $v = $param->{dest};
1071 } elsif ($v eq 'SOURCE') {
1072 $v = $param->{source};
1073 }
1074
961b4928
DM
1075 die "missing parameter '$k' in macro '$macro_name'\n" if !defined($v);
1076 $rule->{$k} = $v;
1077 }
1078 push @$rules, $rule;
1079 }
1080 } else {
1081 push @$rules, $param;
1082 }
1083
1084 foreach my $rule (@$rules) {
6158271d 1085 $rule->{nbdport} = parse_port_name_number_or_range($rule->{dport})
961b4928 1086 if defined($rule->{dport});
6158271d 1087 $rule->{nbsport} = parse_port_name_number_or_range($rule->{sport})
961b4928
DM
1088 if defined($rule->{sport});
1089 }
1090
1091 return $rules;
51bae274
DM
1092}
1093
85c6eaed
DM
1094sub parse_fw_option {
1095 my ($line) = @_;
1096
1097 my ($opt, $value);
1098
d86659ef 1099 if ($line =~ m/^(enable|macfilter|tcpflags):\s*(0|1)\s*$/i) {
9c6b6efd
DM
1100 $opt = lc($1);
1101 $value = int($2);
85c6eaed
DM
1102 } elsif ($line =~ m/^(policy-(in|out)):\s*(ACCEPT|DROP|REJECT)\s*$/i) {
1103 $opt = lc($1);
1104 $value = uc($3);
9c6b6efd 1105 } else {
85c6eaed
DM
1106 chomp $line;
1107 die "can't parse option '$line'\n"
1108 }
1109
1110 return ($opt, $value);
1111}
1112
51bae274
DM
1113sub parse_vm_fw_rules {
1114 my ($filename, $fh) = @_;
1115
85c6eaed 1116 my $res = { in => [], out => [], options => {}};
51bae274
DM
1117
1118 my $section;
1119
5e1267a5
DM
1120 while (defined(my $line = <$fh>)) {
1121 next if $line =~ m/^#/;
1122 next if $line =~ m/^\s*$/;
1123
961b4928
DM
1124 my $linenr = $fh->input_line_number();
1125 my $prefix = "$filename (line $linenr)";
1126
85c6eaed 1127 if ($line =~ m/^\[(\S+)\]\s*$/i) {
5e1267a5 1128 $section = lc($1);
85c6eaed 1129 warn "$prefix: ignore unknown section '$section'\n" if !$res->{$section};
5e1267a5
DM
1130 next;
1131 }
51bae274 1132 if (!$section) {
961b4928 1133 warn "$prefix: skip line - no section";
5e1267a5
DM
1134 next;
1135 }
1136
85c6eaed
DM
1137 next if !$res->{$section}; # skip undefined section
1138
1139 if ($section eq 'options') {
6158271d
DM
1140 eval {
1141 my ($opt, $value) = parse_fw_option($line);
85c6eaed
DM
1142 $res->{options}->{$opt} = $value;
1143 };
1144 warn "$prefix: $@" if $@;
1145 next;
1146 }
1147
961b4928
DM
1148 my $rules;
1149 eval { $rules = parse_fw_rule($line, 1, 1); };
51bae274 1150 if (my $err = $@) {
961b4928 1151 warn "$prefix: $err";
5e1267a5
DM
1152 next;
1153 }
1154
961b4928 1155 push @{$res->{$section}}, @$rules;
51bae274
DM
1156 }
1157
1158 return $res;
1159}
1160
1161sub parse_host_fw_rules {
1162 my ($filename, $fh) = @_;
1163
1164 my $res = { in => [], out => [] };
1165
1166 my $section;
1167
1168 while (defined(my $line = <$fh>)) {
1169 next if $line =~ m/^#/;
1170 next if $line =~ m/^\s*$/;
1171
961b4928
DM
1172 my $linenr = $fh->input_line_number();
1173 my $prefix = "$filename (line $linenr)";
1174
51bae274
DM
1175 if ($line =~ m/^\[(in|out)\]\s*$/i) {
1176 $section = lc($1);
5e1267a5
DM
1177 next;
1178 }
51bae274 1179 if (!$section) {
961b4928 1180 warn "$prefix: skip line - no section";
5e1267a5
DM
1181 next;
1182 }
1183
961b4928
DM
1184 my $rules;
1185 eval { $rules = parse_fw_rule($line, 1, 1); };
ecbea048 1186 if (my $err = $@) {
961b4928 1187 warn "$prefix: $err";
ecbea048 1188 next;
ecbea048
DM
1189 }
1190
961b4928 1191 push @{$res->{$section}}, @$rules;
51bae274 1192 }
ecbea048 1193
51bae274
DM
1194 return $res;
1195}
4cdbb3b7 1196
51bae274
DM
1197sub parse_group_fw_rules {
1198 my ($filename, $fh) = @_;
5e1267a5 1199
51bae274
DM
1200 my $section;
1201 my $group;
1202
1203 my $res = { in => [], out => [] };
6158271d 1204
51bae274
DM
1205 while (defined(my $line = <$fh>)) {
1206 next if $line =~ m/^#/;
1207 next if $line =~ m/^\s*$/;
1208
961b4928
DM
1209 my $linenr = $fh->input_line_number();
1210 my $prefix = "$filename (line $linenr)";
1211
51bae274
DM
1212 if ($line =~ m/^\[(in|out):(\S+)\]\s*$/i) {
1213 $section = lc($1);
1214 $group = lc($2);
1215 next;
1216 }
1217 if (!$section || !$group) {
961b4928 1218 warn "$prefix: skip line - no section";
51bae274
DM
1219 next;
1220 }
1221
961b4928
DM
1222 my $rules;
1223 eval { $rules = parse_fw_rule($line, 0, 0); };
51bae274 1224 if (my $err = $@) {
961b4928 1225 warn "$prefix: $err";
51bae274
DM
1226 next;
1227 }
1228
961b4928 1229 push @{$res->{$group}->{$section}}, @$rules;
5e1267a5
DM
1230 }
1231
1232 return $res;
1233}
1234
06320eb0
DM
1235sub run_locked {
1236 my ($code, @param) = @_;
1237
1238 my $timeout = 10;
1239
1240 my $res = lock_file($pve_fw_lock_filename, $timeout, $code, @param);
1241
1242 die $@ if $@;
1243
1244 return $res;
1245}
1246
5e1267a5
DM
1247sub read_local_vm_config {
1248
1249 my $openvz = {};
1250
1251 my $qemu = {};
1252
1253 my $list = PVE::QemuServer::config_list();
1254
1255 foreach my $vmid (keys %$list) {
5e1267a5
DM
1256 my $cfspath = PVE::QemuServer::cfs_config_path($vmid);
1257 if (my $conf = PVE::Cluster::cfs_read_file($cfspath)) {
1258 $qemu->{$vmid} = $conf;
1259 }
1260 }
1261
1262 my $vmdata = { openvz => $openvz, qemu => $qemu };
1263
1264 return $vmdata;
1265};
1266
1267sub read_vm_firewall_rules {
1268 my ($vmdata) = @_;
1269 my $rules = {};
1270 foreach my $vmid (keys %{$vmdata->{qemu}}, keys %{$vmdata->{openvz}}) {
1271 my $filename = "/etc/pve/firewall/$vmid.fw";
1272 my $fh = IO::File->new($filename, O_RDONLY);
1273 next if !$fh;
1274
51bae274 1275 $rules->{$vmid} = parse_vm_fw_rules($filename, $fh);
5e1267a5
DM
1276 }
1277
1278 return $rules;
1279}
1280
d8f2505e
DM
1281sub generate_std_chains {
1282 my ($ruleset) = @_;
1283
1284 foreach my $chain (keys %$pve_std_chains) {
1285 ruleset_create_chain($ruleset, $chain);
1286 foreach my $rule (@{$pve_std_chains->{$chain}}) {
1287 if (ref($rule)) {
1288 ruleset_generate_rule($ruleset, $chain, $rule);
1289 } else {
1290 ruleset_addrule($ruleset, $chain, $rule);
1291 }
1292 }
1293 }
1294}
1295
5e1267a5 1296sub compile {
5e1267a5
DM
1297 my $vmdata = read_local_vm_config();
1298 my $rules = read_vm_firewall_rules($vmdata);
6158271d 1299
51bae274
DM
1300 my $group_rules = {};
1301 my $filename = "/etc/pve/firewall/groups.fw";
1302 if (my $fh = IO::File->new($filename, O_RDONLY)) {
1303 $group_rules = parse_group_fw_rules($filename, $fh);
1304 }
5e1267a5 1305
3fa83edf
DM
1306 #print Dumper($rules);
1307
1308 my $ruleset = {};
1309
dec84fcd
DM
1310 ruleset_create_chain($ruleset, "PVEFW-INPUT");
1311 ruleset_create_chain($ruleset, "PVEFW-OUTPUT");
fadb13dd 1312 ruleset_create_chain($ruleset, "PVEFW-FORWARD");
3fa83edf 1313
d8f2505e 1314 generate_std_chains($ruleset);
4e6112f9 1315
fadb13dd 1316 my $enable_hostfw = 0;
51bae274
DM
1317 $filename = "/etc/pve/local/host.fw";
1318 if (my $fh = IO::File->new($filename, O_RDONLY)) {
1319 my $host_rules = parse_host_fw_rules($filename, $fh);
fadb13dd
DM
1320
1321 $enable_hostfw = 1;
1322
51bae274
DM
1323 enablehostfw($ruleset, $host_rules, $group_rules);
1324 }
3fa83edf 1325
6158271d 1326 # generate firewall rules for QEMU VMs
3fa83edf
DM
1327 foreach my $vmid (keys %{$vmdata->{qemu}}) {
1328 my $conf = $vmdata->{qemu}->{$vmid};
fa9c4a6f
DM
1329 my $vmfw_conf = $rules->{$vmid};
1330 next if !$vmfw_conf;
1331 next if defined($vmfw_conf->{options}->{enable}) && ($vmfw_conf->{options}->{enable} == 0);
3fa83edf
DM
1332
1333 foreach my $netid (keys %$conf) {
1334 next if $netid !~ m/^net(\d+)$/;
1335 my $net = PVE::QemuServer::parse_net($conf->{$netid});
1336 next if !$net;
1337 my $iface = "tap${vmid}i$1";
5e1267a5 1338
3fa83edf
DM
1339 my $bridge = $net->{bridge};
1340 next if !$bridge; # fixme: ?
1341
1342 $bridge .= "v$net->{tag}" if $net->{tag};
1343
fadb13dd 1344
3fa83edf
DM
1345 generate_bridge_chains($ruleset, $bridge);
1346
c29f55c9 1347 my $macaddr = $net->{macaddr};
fa9c4a6f
DM
1348 generate_tap_rules_direction($ruleset, $group_rules, $iface, $netid, $macaddr, $vmfw_conf, $bridge, 'IN');
1349 generate_tap_rules_direction($ruleset, $group_rules, $iface, $netid, $macaddr, $vmfw_conf, $bridge, 'OUT');
3fa83edf
DM
1350 }
1351 }
c0413e35 1352
fadb13dd
DM
1353 if ($enable_hostfw) {
1354 # allow traffic from lo (ourself)
1355 ruleset_addrule($ruleset, "PVEFW-INPUT", "-i lo -j ACCEPT");
1356 }
c0413e35 1357
3fa83edf
DM
1358 return $ruleset;
1359}
1360
1361sub get_ruleset_status {
1362 my ($ruleset, $verbose) = @_;
1363
1364 my $active_chains = iptables_get_chains();
1365
1366 my $statushash = {};
1367
1368 foreach my $chain (sort keys %$ruleset) {
09d5f68e 1369 my $digest = Digest::SHA->new('sha1');
3fa83edf
DM
1370 foreach my $cmd (@{$ruleset->{$chain}}) {
1371 $digest->add("$cmd\n");
1372 }
1373 my $sig = $digest->b64digest;
1374 $statushash->{$chain}->{sig} = $sig;
1375
1376 my $oldsig = $active_chains->{$chain};
1377 if (!defined($oldsig)) {
1378 $statushash->{$chain}->{action} = 'create';
1379 } else {
1380 if ($oldsig eq $sig) {
1381 $statushash->{$chain}->{action} = 'exists';
1382 } else {
1383 $statushash->{$chain}->{action} = 'update';
1384 }
1385 }
1386 print "$statushash->{$chain}->{action} $chain ($sig)\n" if $verbose;
1387 foreach my $cmd (@{$ruleset->{$chain}}) {
1388 print "\t$cmd\n" if $verbose;
1389 }
1390 }
1391
1392 foreach my $chain (sort keys %$active_chains) {
1393 if (!defined($ruleset->{$chain})) {
1394 my $sig = $active_chains->{$chain};
1395 $statushash->{$chain}->{action} = 'delete';
1396 $statushash->{$chain}->{sig} = $sig;
1397 print "delete $chain ($sig)\n" if $verbose;
1398 }
6158271d 1399 }
3fa83edf
DM
1400
1401 return $statushash;
1402}
1403
1404sub print_ruleset {
1405 my ($ruleset) = @_;
1406
1407 get_ruleset_status($ruleset, 1);
1408}
1409
1410sub print_sig_rule {
1411 my ($chain, $sig) = @_;
1412
09d5f68e
DM
1413 # We just use this to store a SHA1 checksum used to detect changes
1414 return "-A $chain -m comment --comment \"PVESIG:$sig\"\n";
b6360c3f
DM
1415}
1416
a84f4d96
DM
1417sub apply_ruleset {
1418 my ($ruleset, $verbose) = @_;
3fa83edf 1419
a84f4d96 1420 enable_bridge_firewall();
3fa83edf
DM
1421
1422 my $cmdlist = "*filter\n"; # we pass this to iptables-restore;
1423
1424 my $statushash = get_ruleset_status($ruleset, $verbose);
1425
1426 # create missing chains first
1427 foreach my $chain (sort keys %$ruleset) {
1428 my $stat = $statushash->{$chain};
1429 die "internal error" if !$stat;
1430 next if $stat->{action} ne 'create';
886aba9c 1431
3fa83edf
DM
1432 $cmdlist .= ":$chain - [0:0]\n";
1433 }
1434
dec84fcd 1435 my $rule = "INPUT -j PVEFW-INPUT";
3fa83edf
DM
1436 if (!PVE::Firewall::iptables_rule_exist($rule)) {
1437 $cmdlist .= "-A $rule\n";
1438 }
dec84fcd 1439 $rule = "OUTPUT -j PVEFW-OUTPUT";
3fa83edf
DM
1440 if (!PVE::Firewall::iptables_rule_exist($rule)) {
1441 $cmdlist .= "-A $rule\n";
1442 }
1443
dec84fcd 1444 $rule = "FORWARD -j PVEFW-FORWARD";
3fa83edf
DM
1445 if (!PVE::Firewall::iptables_rule_exist($rule)) {
1446 $cmdlist .= "-A $rule\n";
1447 }
1448
1449 foreach my $chain (sort keys %$ruleset) {
1450 my $stat = $statushash->{$chain};
1451 die "internal error" if !$stat;
1452
1453 if ($stat->{action} eq 'update' || $stat->{action} eq 'create') {
1454 $cmdlist .= "-F $chain\n";
1455 foreach my $cmd (@{$ruleset->{$chain}}) {
1456 $cmdlist .= "$cmd\n";
1457 }
1458 $cmdlist .= print_sig_rule($chain, $stat->{sig});
1459 } elsif ($stat->{action} eq 'delete') {
f5d28682 1460 die "internal error"; # this should not happen
3fa83edf
DM
1461 } elsif ($stat->{action} eq 'exists') {
1462 # do nothing
1463 } else {
1464 die "internal error - unknown status '$stat->{action}'";
1465 }
1466 }
1467
f5d28682
DM
1468 foreach my $chain (keys %$statushash) {
1469 next if $statushash->{$chain}->{action} ne 'delete';
1470 $cmdlist .= "-F $chain\n";
1471 }
1472 foreach my $chain (keys %$statushash) {
1473 next if $statushash->{$chain}->{action} ne 'delete';
fadb13dd
DM
1474 next if $chain eq 'PVEFW-INPUT';
1475 next if $chain eq 'PVEFW-OUTPUT';
1476 next if $chain eq 'PVEFW-FORWARD';
f5d28682
DM
1477 $cmdlist .= "-X $chain\n";
1478 }
1479
3fa83edf
DM
1480 $cmdlist .= "COMMIT\n";
1481
1482 print $cmdlist if $verbose;
1483
1484 iptables_restore_cmdlist($cmdlist);
1485
6158271d 1486 # test: re-read status and check if everything is up to date
3fa83edf
DM
1487 $statushash = get_ruleset_status($ruleset);
1488
1489 my $errors;
1490 foreach my $chain (sort keys %$ruleset) {
1491 my $stat = $statushash->{$chain};
1492 if ($stat->{action} ne 'exists') {
1493 warn "unable to update chain '$chain'\n";
1494 $errors = 1;
1495 }
1496 }
b6360c3f 1497
3fa83edf 1498 die "unable to apply firewall changes\n" if $errors;
b6360c3f
DM
1499}
1500
b6360c3f 15011;