]> git.proxmox.com Git - pve-firewall.git/blob - PVE/Firewall.pm
implement option 'tcpflags' to log illegal combinations of TCP flags
[pve-firewall.git] / PVE / Firewall.pm
1 package PVE::Firewall;
2
3 use warnings;
4 use strict;
5 use Data::Dumper;
6 use Digest::SHA;
7 use PVE::Tools;
8 use PVE::QemuServer;
9 use File::Path;
10 use IO::File;
11 use Net::IP;
12 use PVE::Tools qw(run_command lock_file);
13
14 use Data::Dumper;
15
16 my $pve_fw_lock_filename = "/var/lock/pvefw.lck";
17
18 # imported/converted from: /usr/share/shorewall/macro.*
19 my $pve_fw_macros = {
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' => [
31 { action => 'PARAM', proto => 'tcp', dport => '6881:6889' },
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' },
67 ],
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' => [
95 { action => 'PARAM', proto => 'tcp', dport => '80' },
96 ],
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' },
335 { action => 'PARAM', proto => 'tcp', dport => '443' },
336 ],
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 ],
346 };
347
348 my $pve_fw_parsed_macros;
349 my $pve_fw_preferred_macro_names = {};
350
351 my $pve_std_chains = {
352 'PVEFW-SET-ACCEPT-MARK' => [
353 "-j MARK --set-mark 1",
354 ],
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 ],
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 ],
434 };
435
436 # iptables -p icmp -h
437 my $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
477 sub get_firewall_macros {
478
479 return $pve_fw_parsed_macros if $pve_fw_parsed_macros;
480
481 $pve_fw_parsed_macros = {};
482
483 foreach my $k (keys %$pve_fw_macros) {
484 my $name = lc($k);
485
486 my $macro = $pve_fw_macros->{$k};
487 $pve_fw_preferred_macro_names->{$name} = $k;
488 $pve_fw_parsed_macros->{$name} = $macro;
489 }
490
491 return $pve_fw_parsed_macros;
492 }
493
494 my $etc_services;
495
496 sub 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;
517 $services->{byid}->{$2}->{port} = $2;
518 $services->{byid}->{$2}->{$3} = 1;
519 $services->{byname}->{$1} = $services->{byid}->{$2};
520 }
521 }
522
523 close($fh);
524
525 $etc_services = $services;
526
527
528 return $etc_services;
529 }
530
531 my $etc_protocols;
532
533 sub 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
564 sub parse_address_list {
565 my ($str) = @_;
566
567 my $nbaor = 0;
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";
572 }else{
573 $nbaor++;
574 }
575 }
576 return $nbaor;
577 }
578
579 sub parse_port_name_number_or_range {
580 my ($str) = @_;
581
582 my $services = PVE::Firewall::get_etc_services();
583 my $nbports = 0;
584 foreach my $item (split(/,/, $str)) {
585 my $portlist = "";
586 my $oldpon = undef;
587 $nbports++;
588 foreach my $pon (split(':', $item, 2)) {
589 $pon = $services->{byname}->{$pon}->{port} if $services->{byname}->{$pon}->{port};
590 if ($pon =~ m/^\d+$/){
591 die "invalid port '$pon'\n" if $pon < 0 && $pon > 65535;
592 die "port '$pon' must be bigger than port '$oldpon' \n" if $oldpon && ($pon < $oldpon);
593 $oldpon = $pon;
594 }else{
595 die "invalid port $services->{byname}->{$pon}\n" if !$services->{byname}->{$pon};
596 }
597 }
598 }
599
600 return ($nbports);
601 }
602
603 my $bridge_firewall_enabled = 0;
604
605 sub 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
615 my $rule_format = "%-15s %-30s %-30s %-15s %-15s %-15s\n";
616
617 sub iptables {
618 my ($cmd) = @_;
619
620 run_command("/sbin/iptables $cmd", outfunc => sub {}, errfunc => sub {});
621 }
622
623 sub iptables_restore_cmdlist {
624 my ($cmdlist) = @_;
625
626 run_command("/sbin/iptables-restore -n", input => $cmdlist);
627 }
628
629 sub 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
637 return 1 if $name =~ m/^PVEFW-\S+$/;
638
639 return 1 if $name =~ m/^tap\d+i\d+-(:?IN|OUT)$/;
640 return 1 if $name =~ m/^vmbr\d+-(:?FW|IN|OUT)$/;
641 return 1 if $name =~ m/^GROUP-(:?[^\s\-]+)-(:?IN|OUT)$/;
642
643 return undef;
644 };
645
646 my $table = '';
647
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);
664 $res->{$chain} = "unknown";
665 } elsif ($line =~ m/^-A\s+(\S+)\s.*--comment\s+\"PVESIG:(\S+)\"/) {
666 my ($chain, $sig) = ($1, $2);
667 return if !&$is_pvefw_chain($chain);
668 $res->{$chain} = $sig;
669 } else {
670 # simply ignore the rest
671 return;
672 }
673 };
674
675 run_command("/sbin/iptables-save", outfunc => $parser);
676
677 return $res;
678 }
679
680 sub 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
691 sub iptables_rule_exist {
692 my ($rule) = @_;
693
694 eval{
695 iptables("-C $rule");
696 };
697 return undef if $@;
698
699 return 1;
700 }
701
702 sub ruleset_generate_rule {
703 my ($ruleset, $chain, $rule, $goto) = @_;
704
705 my $cmd = '';
706
707 $cmd .= " -m iprange --src-range" if $rule->{nbsource} && $rule->{nbsource} > 1;
708 $cmd .= " -s $rule->{source}" if $rule->{source};
709 $cmd .= " -m iprange --dst-range" if $rule->{nbdest} && $rule->{nbdest} > 1;
710 $cmd .= " -d $rule->{dest}" if $rule->{dest};
711 $cmd .= " -p $rule->{proto}" if $rule->{proto};
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};
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 };
746
747 ruleset_addrule($ruleset, $chain, $cmd) if $cmd;
748 }
749
750 sub ruleset_create_chain {
751 my ($ruleset, $chain) = @_;
752
753 die "Invalid chain name '$chain' (28 char max)\n" if length($chain) > 28;
754
755 die "chain '$chain' already exists\n" if $ruleset->{$chain};
756
757 $ruleset->{$chain} = [];
758 }
759
760 sub ruleset_chain_exist {
761 my ($ruleset, $chain) = @_;
762
763 return $ruleset->{$chain} ? 1 : undef;
764 }
765
766 sub ruleset_addrule {
767 my ($ruleset, $chain, $rule) = @_;
768
769 die "no such chain '$chain'\n" if !$ruleset->{$chain};
770
771 push @{$ruleset->{$chain}}, "-A $chain $rule";
772 }
773
774 sub ruleset_insertrule {
775 my ($ruleset, $chain, $rule) = @_;
776
777 die "no such chain '$chain'\n" if !$ruleset->{$chain};
778
779 unshift @{$ruleset->{$chain}}, "-A $chain $rule";
780 }
781
782 sub generate_bridge_chains {
783 my ($ruleset, $bridge) = @_;
784
785 if (!ruleset_chain_exist($ruleset, "PVEFW-FORWARD")){
786 ruleset_create_chain($ruleset, "PVEFW-FORWARD");
787 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT");
788 }
789
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");
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
796 }
797
798 if (!ruleset_chain_exist($ruleset, "$bridge-OUT")) {
799 ruleset_create_chain($ruleset, "$bridge-OUT");
800 ruleset_addrule($ruleset, "$bridge-FW", "-m physdev --physdev-is-bridged --physdev-is-in -j $bridge-OUT");
801 }
802
803 if (!ruleset_chain_exist($ruleset, "$bridge-IN")) {
804 ruleset_create_chain($ruleset, "$bridge-IN");
805 ruleset_addrule($ruleset, "$bridge-FW", "-m physdev --physdev-is-bridged --physdev-is-out -j $bridge-IN");
806 ruleset_addrule($ruleset, "$bridge-FW", "-m mark --mark 1 -j ACCEPT");
807 }
808 }
809
810 sub generate_tap_rules_direction {
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};
815
816 my $tapchain = "$iface-$direction";
817
818 ruleset_create_chain($ruleset, $tapchain);
819
820 ruleset_addrule($ruleset, $tapchain, "-m conntrack --ctstate INVALID -j DROP");
821 ruleset_addrule($ruleset, $tapchain, "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT");
822
823 if ($direction eq 'OUT' && defined($macaddr) &&
824 !(defined($options->{macfilter}) && $options->{macfilter} == 0)) {
825 ruleset_addrule($ruleset, $tapchain, "-m mac ! --mac-source $macaddr -j DROP");
826 }
827
828 if ($options->{tcpflags}) {
829 ruleset_addrule($ruleset, $tapchain, "-p tcp -j PVEFW-tcpflags");
830 }
831
832 if ($rules) {
833 foreach my $rule (@$rules) {
834 next if $rule->{iface} && $rule->{iface} ne $netid;
835 # we go to $bridge-IN if accept in out rules
836 if($rule->{action} =~ m/^(GROUP-(\S+))$/){
837 $rule->{action} .= "-$direction";
838 # generate empty group rule if don't exist
839 if(!ruleset_chain_exist($ruleset, $rule->{action})){
840 generate_group_rules($ruleset, $group_rules, $2);
841 }
842 ruleset_generate_rule($ruleset, $tapchain, $rule);
843 ruleset_addrule($ruleset, $tapchain, "-m mark --mark 1 -j RETURN")
844 if $direction eq 'OUT';
845 } else {
846 $rule->{action} = "PVEFW-SET-ACCEPT-MARK" if $rule->{action} eq 'ACCEPT' && $direction eq 'OUT';
847 ruleset_generate_rule($ruleset, $tapchain, $rule);
848 }
849 }
850 }
851
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') {
863 ruleset_addrule($ruleset, $tapchain, "-g PVEFW-SET-ACCEPT-MARK");
864 } else {
865 ruleset_addrule($ruleset, $tapchain, "-j ACCEPT");
866 }
867 } elsif ($policy eq 'DROP') {
868 ruleset_addrule($ruleset, $tapchain, "-j PVEFW-Drop");
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') {
872 ruleset_addrule($ruleset, $tapchain, "-j PVEFW-Reject");
873 ruleset_addrule($ruleset, $tapchain, "-j LOG --log-prefix \"$tapchain-reject: \" --log-level 4");
874 ruleset_addrule($ruleset, $tapchain, "-g PVEFW-reject");
875 } else {
876 # should not happen
877 die "internal error: unknown policy '$policy'";
878 }
879
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);
884
885 if ($direction eq 'OUT'){
886 # add tap->host rules
887 my $rule = "-m physdev --physdev-$physdevdirection $iface -j $tapchain";
888 ruleset_addrule($ruleset, "PVEFW-INPUT", $rule);
889 }
890 }
891
892 sub enablehostfw {
893 my ($ruleset, $rules, $group_rules) = @_;
894
895 # fixme: allow security groups
896
897 # host inbound firewall
898 my $chain = "PVEFW-HOST-IN";
899 ruleset_create_chain($ruleset, $chain);
900
901 ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate INVALID -j DROP");
902 ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT");
903 ruleset_addrule($ruleset, $chain, "-i lo -j ACCEPT");
904 ruleset_addrule($ruleset, $chain, "-m addrtype --dst-type MULTICAST -j ACCEPT");
905 ruleset_addrule($ruleset, $chain, "-p udp -m conntrack --ctstate NEW -m multiport --dports 5404,5405 -j ACCEPT");
906 ruleset_addrule($ruleset, $chain, "-p udp -m udp --dport 9000 -j ACCEPT"); #corosync
907
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';
912 ruleset_generate_rule($ruleset, $chain, $rule);
913 }
914 }
915
916 ruleset_addrule($ruleset, $chain, "-j LOG --log-prefix \"kvmhost-IN dropped: \" --log-level 4");
917 ruleset_addrule($ruleset, $chain, "-j DROP");
918
919 # host outbound firewall
920 $chain = "PVEFW-HOST-OUT";
921 ruleset_create_chain($ruleset, $chain);
922
923 ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate INVALID -j DROP");
924 ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT");
925 ruleset_addrule($ruleset, $chain, "-o lo -j ACCEPT");
926 ruleset_addrule($ruleset, $chain, "-m addrtype --dst-type MULTICAST -j ACCEPT");
927 ruleset_addrule($ruleset, $chain, "-p udp -m conntrack --ctstate NEW -m multiport --dports 5404,5405 -j ACCEPT");
928 ruleset_addrule($ruleset, $chain, "-p udp -m udp --dport 9000 -j ACCEPT"); #corosync
929
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';
934 ruleset_generate_rule($ruleset, $chain, $rule);
935 }
936 }
937
938 ruleset_addrule($ruleset, $chain, "-j LOG --log-prefix \"kvmhost-OUT dropped: \" --log-level 4");
939 ruleset_addrule($ruleset, $chain, "-j DROP");
940
941 ruleset_addrule($ruleset, "PVEFW-OUTPUT", "-j PVEFW-HOST-OUT");
942 ruleset_addrule($ruleset, "PVEFW-INPUT", "-j PVEFW-HOST-IN");
943 }
944
945 sub generate_group_rules {
946 my ($ruleset, $group_rules, $group) = @_;
947
948 my $rules = $group_rules->{$group};
949
950 die "no such security group '$group'\n" if !$rules;
951
952 my $chain = "GROUP-${group}-IN";
953
954 ruleset_create_chain($ruleset, $chain);
955
956 if ($rules->{in}) {
957 foreach my $rule (@{$rules->{in}}) {
958 ruleset_generate_rule($ruleset, $chain, $rule);
959 }
960 }
961
962 $chain = "GROUP-${group}-OUT";
963
964 ruleset_create_chain($ruleset, $chain);
965 ruleset_addrule($ruleset, $chain, "-j MARK --set-mark 0"); # clear mark
966
967 if ($rules->{out}) {
968 foreach my $rule (@{$rules->{out}}) {
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,
971 # so we can't go to VMBRXX-IN)
972 $rule->{action} = 'PVEFW-SET-ACCEPT-MARK' if $rule->{action} eq 'ACCEPT';
973 ruleset_generate_rule($ruleset, $chain, $rule);
974 }
975 }
976 }
977
978 my $MAX_NETS = 32;
979 my $valid_netdev_names = {};
980 for (my $i = 0; $i < $MAX_NETS; $i++) {
981 $valid_netdev_names->{"net$i"} = 1;
982 }
983
984 sub parse_fw_rule {
985 my ($line, $need_iface, $allow_groups) = @_;
986
987 my $macros = get_firewall_macros();
988 my $protocols = get_etc_protocols();
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
1007 my $macro;
1008 my $macro_name;
1009
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)\)$/) {
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;
1021 } else {
1022 die "unknown action '$action'\n";
1023 }
1024
1025 if ($need_iface) {
1026 $iface = undef if $iface && $iface eq '-';
1027 die "unknown interface '$iface'\n"
1028 if defined($iface) && !$valid_netdev_names->{$iface};
1029 }
1030
1031 $proto = undef if $proto && $proto eq '-';
1032 die "unknown protokol '$proto'\n" if $proto &&
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
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;
1047
1048 my $rules = [];
1049
1050 my $param = {
1051 action => $action,
1052 iface => $iface,
1053 source => $source,
1054 dest => $dest,
1055 nbsource => $nbsource,
1056 nbdest => $nbdest,
1057 proto => $proto,
1058 dport => $dport,
1059 sport => $sport,
1060 };
1061
1062 if ($macro) {
1063 foreach my $templ (@$macro) {
1064 my $rule = {};
1065 foreach my $k (keys %$templ) {
1066 my $v = $templ->{$k};
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
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) {
1085 $rule->{nbdport} = parse_port_name_number_or_range($rule->{dport})
1086 if defined($rule->{dport});
1087 $rule->{nbsport} = parse_port_name_number_or_range($rule->{sport})
1088 if defined($rule->{sport});
1089 }
1090
1091 return $rules;
1092 }
1093
1094 sub parse_fw_option {
1095 my ($line) = @_;
1096
1097 my ($opt, $value);
1098
1099 if ($line =~ m/^(enable|macfilter|tcpflags):\s*(0|1)\s*$/i) {
1100 $opt = lc($1);
1101 $value = int($2);
1102 } elsif ($line =~ m/^(policy-(in|out)):\s*(ACCEPT|DROP|REJECT)\s*$/i) {
1103 $opt = lc($1);
1104 $value = uc($3);
1105 } else {
1106 chomp $line;
1107 die "can't parse option '$line'\n"
1108 }
1109
1110 return ($opt, $value);
1111 }
1112
1113 sub parse_vm_fw_rules {
1114 my ($filename, $fh) = @_;
1115
1116 my $res = { in => [], out => [], options => {}};
1117
1118 my $section;
1119
1120 while (defined(my $line = <$fh>)) {
1121 next if $line =~ m/^#/;
1122 next if $line =~ m/^\s*$/;
1123
1124 my $linenr = $fh->input_line_number();
1125 my $prefix = "$filename (line $linenr)";
1126
1127 if ($line =~ m/^\[(\S+)\]\s*$/i) {
1128 $section = lc($1);
1129 warn "$prefix: ignore unknown section '$section'\n" if !$res->{$section};
1130 next;
1131 }
1132 if (!$section) {
1133 warn "$prefix: skip line - no section";
1134 next;
1135 }
1136
1137 next if !$res->{$section}; # skip undefined section
1138
1139 if ($section eq 'options') {
1140 eval {
1141 my ($opt, $value) = parse_fw_option($line);
1142 $res->{options}->{$opt} = $value;
1143 };
1144 warn "$prefix: $@" if $@;
1145 next;
1146 }
1147
1148 my $rules;
1149 eval { $rules = parse_fw_rule($line, 1, 1); };
1150 if (my $err = $@) {
1151 warn "$prefix: $err";
1152 next;
1153 }
1154
1155 push @{$res->{$section}}, @$rules;
1156 }
1157
1158 return $res;
1159 }
1160
1161 sub 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
1172 my $linenr = $fh->input_line_number();
1173 my $prefix = "$filename (line $linenr)";
1174
1175 if ($line =~ m/^\[(in|out)\]\s*$/i) {
1176 $section = lc($1);
1177 next;
1178 }
1179 if (!$section) {
1180 warn "$prefix: skip line - no section";
1181 next;
1182 }
1183
1184 my $rules;
1185 eval { $rules = parse_fw_rule($line, 1, 1); };
1186 if (my $err = $@) {
1187 warn "$prefix: $err";
1188 next;
1189 }
1190
1191 push @{$res->{$section}}, @$rules;
1192 }
1193
1194 return $res;
1195 }
1196
1197 sub parse_group_fw_rules {
1198 my ($filename, $fh) = @_;
1199
1200 my $section;
1201 my $group;
1202
1203 my $res = { in => [], out => [] };
1204
1205 while (defined(my $line = <$fh>)) {
1206 next if $line =~ m/^#/;
1207 next if $line =~ m/^\s*$/;
1208
1209 my $linenr = $fh->input_line_number();
1210 my $prefix = "$filename (line $linenr)";
1211
1212 if ($line =~ m/^\[(in|out):(\S+)\]\s*$/i) {
1213 $section = lc($1);
1214 $group = lc($2);
1215 next;
1216 }
1217 if (!$section || !$group) {
1218 warn "$prefix: skip line - no section";
1219 next;
1220 }
1221
1222 my $rules;
1223 eval { $rules = parse_fw_rule($line, 0, 0); };
1224 if (my $err = $@) {
1225 warn "$prefix: $err";
1226 next;
1227 }
1228
1229 push @{$res->{$group}->{$section}}, @$rules;
1230 }
1231
1232 return $res;
1233 }
1234
1235 sub 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
1247 sub 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) {
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
1267 sub 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
1275 $rules->{$vmid} = parse_vm_fw_rules($filename, $fh);
1276 }
1277
1278 return $rules;
1279 }
1280
1281 sub 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
1296 sub compile {
1297 my $vmdata = read_local_vm_config();
1298 my $rules = read_vm_firewall_rules($vmdata);
1299
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 }
1305
1306 #print Dumper($rules);
1307
1308 my $ruleset = {};
1309
1310 ruleset_create_chain($ruleset, "PVEFW-INPUT");
1311 ruleset_create_chain($ruleset, "PVEFW-OUTPUT");
1312 ruleset_create_chain($ruleset, "PVEFW-FORWARD");
1313
1314 generate_std_chains($ruleset);
1315
1316 my $enable_hostfw = 0;
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);
1320
1321 $enable_hostfw = 1;
1322
1323 enablehostfw($ruleset, $host_rules, $group_rules);
1324 }
1325
1326 # generate firewall rules for QEMU VMs
1327 foreach my $vmid (keys %{$vmdata->{qemu}}) {
1328 my $conf = $vmdata->{qemu}->{$vmid};
1329 my $vmfw_conf = $rules->{$vmid};
1330 next if !$vmfw_conf;
1331 next if defined($vmfw_conf->{options}->{enable}) && ($vmfw_conf->{options}->{enable} == 0);
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";
1338
1339 my $bridge = $net->{bridge};
1340 next if !$bridge; # fixme: ?
1341
1342 $bridge .= "v$net->{tag}" if $net->{tag};
1343
1344
1345 generate_bridge_chains($ruleset, $bridge);
1346
1347 my $macaddr = $net->{macaddr};
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');
1350 }
1351 }
1352
1353 if ($enable_hostfw) {
1354 # allow traffic from lo (ourself)
1355 ruleset_addrule($ruleset, "PVEFW-INPUT", "-i lo -j ACCEPT");
1356 }
1357
1358 return $ruleset;
1359 }
1360
1361 sub 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) {
1369 my $digest = Digest::SHA->new('sha1');
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 }
1399 }
1400
1401 return $statushash;
1402 }
1403
1404 sub print_ruleset {
1405 my ($ruleset) = @_;
1406
1407 get_ruleset_status($ruleset, 1);
1408 }
1409
1410 sub print_sig_rule {
1411 my ($chain, $sig) = @_;
1412
1413 # We just use this to store a SHA1 checksum used to detect changes
1414 return "-A $chain -m comment --comment \"PVESIG:$sig\"\n";
1415 }
1416
1417 sub apply_ruleset {
1418 my ($ruleset, $verbose) = @_;
1419
1420 enable_bridge_firewall();
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';
1431
1432 $cmdlist .= ":$chain - [0:0]\n";
1433 }
1434
1435 my $rule = "INPUT -j PVEFW-INPUT";
1436 if (!PVE::Firewall::iptables_rule_exist($rule)) {
1437 $cmdlist .= "-A $rule\n";
1438 }
1439 $rule = "OUTPUT -j PVEFW-OUTPUT";
1440 if (!PVE::Firewall::iptables_rule_exist($rule)) {
1441 $cmdlist .= "-A $rule\n";
1442 }
1443
1444 $rule = "FORWARD -j PVEFW-FORWARD";
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') {
1460 die "internal error"; # this should not happen
1461 } elsif ($stat->{action} eq 'exists') {
1462 # do nothing
1463 } else {
1464 die "internal error - unknown status '$stat->{action}'";
1465 }
1466 }
1467
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';
1474 next if $chain eq 'PVEFW-INPUT';
1475 next if $chain eq 'PVEFW-OUTPUT';
1476 next if $chain eq 'PVEFW-FORWARD';
1477 $cmdlist .= "-X $chain\n";
1478 }
1479
1480 $cmdlist .= "COMMIT\n";
1481
1482 print $cmdlist if $verbose;
1483
1484 iptables_restore_cmdlist($cmdlist);
1485
1486 # test: re-read status and check if everything is up to date
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 }
1497
1498 die "unable to apply firewall changes\n" if $errors;
1499 }
1500
1501 1;