12 use PVE
::OpenVZ
; # dependeny problem?!
17 use PVE
::Tools
qw(run_command lock_file);
21 # fixme: use ULOG instead of LOG?
23 my $nodename = PVE
::INotify
::nodename
();
25 my $pve_fw_lock_filename = "/var/lock/pvefw.lck";
26 my $pve_fw_status_filename = "/var/lib/pve-firewall/pvefw.status";
28 my $default_log_level = 'info';
30 my $log_level_hash = {
41 # imported/converted from: /usr/share/shorewall/macro.*
44 { action
=> 'PARAM', proto
=> 'udp', dport
=> '10080' },
45 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '10080' },
48 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '113' },
51 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '179' },
54 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6881:6889' },
55 { action
=> 'PARAM', proto
=> 'udp', dport
=> '6881' },
58 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6881:6999' },
59 { action
=> 'PARAM', proto
=> 'udp', dport
=> '6881' },
62 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '2401' },
65 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1494' },
66 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1604' },
67 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '2598' },
70 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3689' },
71 { action
=> 'PARAM', proto
=> 'udp', dport
=> '3689' },
74 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6277' },
77 { action
=> 'PARAM', proto
=> 'udp', dport
=> '67:68', sport
=> '67:68' },
78 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '67:68', sport
=> '67:68' },
81 { action
=> 'PARAM', proto
=> 'udp', dport
=> '53' },
82 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '53' },
85 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3632' },
88 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '4662' },
89 { action
=> 'PARAM', proto
=> 'udp', dport
=> '4665' },
92 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '21' },
95 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '79' },
98 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '2086' },
99 { action
=> 'PARAM', proto
=> 'udp', dport
=> '2086' },
100 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1080' },
101 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1080' },
104 { action
=> 'PARAM', proto
=> '47' },
105 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> '47' },
108 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '9418' },
111 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6346' },
112 { action
=> 'PARAM', proto
=> 'udp', dport
=> '6346' },
115 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '11371' },
118 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '80' },
121 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '443' },
124 { action
=> 'PARAM', proto
=> 'udp', dport
=> '3130' },
127 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5190' },
130 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '143' },
133 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '993' },
136 { action
=> 'PARAM', proto
=> '94' },
137 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> '94' },
140 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '631' },
143 { action
=> 'PARAM', proto
=> 'udp', dport
=> '631' },
146 { action
=> 'PARAM', source
=> 'SOURCE', dest
=> 'DEST', proto
=> 'tcp', dport
=> '631' },
147 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '631' },
150 { action
=> 'PARAM', proto
=> 'udp', dport
=> '500', sport
=> '500' },
151 { action
=> 'PARAM', proto
=> '50' },
152 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '500', sport
=> '500' },
153 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> '50' },
156 { action
=> 'PARAM', proto
=> 'udp', dport
=> '500', sport
=> '500' },
157 { action
=> 'PARAM', proto
=> '51' },
158 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '500', sport
=> '500' },
159 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> '51' },
162 { action
=> 'PARAM', proto
=> 'udp', dport
=> '500' },
163 { action
=> 'PARAM', proto
=> 'udp', dport
=> '4500' },
164 { action
=> 'PARAM', proto
=> '50' },
165 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '500' },
166 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '4500' },
167 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> '50' },
170 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6667' },
173 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5222' },
176 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5223' },
179 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5269' },
182 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '9100' },
185 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1701' },
186 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '1701' },
189 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '389' },
192 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '636' },
195 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1863' },
198 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1433' },
201 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '25' },
202 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '465' },
203 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '587' },
206 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '4949' },
209 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3306' },
212 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '119' },
215 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '563' },
218 { action
=> 'PARAM', proto
=> 'udp', dport
=> '123' },
221 { action
=> 'PARAM', proto
=> 'udp', dport
=> '123' },
222 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '123' },
225 { action
=> 'PARAM', proto
=> 'udp', dport
=> '123' },
226 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1024:65535', sport
=> '123' },
229 { action
=> 'PARAM', proto
=> '89' },
232 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1194' },
235 { action
=> 'PARAM', proto
=> 'udp', dport
=> '5632' },
236 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5631' },
239 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '110' },
242 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '995' },
245 { action
=> 'PARAM', proto
=> '47' },
246 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> '47' },
247 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1723' },
250 { action
=> 'PARAM', proto
=> 'icmp', dport
=> 'echo-request' },
253 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5432' },
256 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '515' },
259 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3389' },
262 { action
=> 'PARAM', proto
=> 'udp', dport
=> '520' },
263 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '520' },
266 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '953' },
269 { action
=> 'ACCEPT', proto
=> 'tcp', dport
=> '2703' },
272 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '37' },
275 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '873' },
278 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6566' },
281 { action
=> 'PARAM', proto
=> 'udp', dport
=> '135,445' },
282 { action
=> 'PARAM', proto
=> 'udp', dport
=> '137:139' },
283 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1024:65535', sport
=> '137' },
284 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '135,139,445' },
287 { action
=> 'PARAM', proto
=> 'udp', dport
=> '135,445' },
288 { action
=> 'PARAM', proto
=> 'udp', dport
=> '137:139' },
289 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1024:65535', sport
=> '137' },
290 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '135,139,445' },
291 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '135,445' },
292 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '137:139' },
293 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '1024:65535', sport
=> '137' },
294 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'tcp', dport
=> '135,139,445' },
297 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '901' },
300 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '25' },
303 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '465' },
306 { action
=> 'PARAM', proto
=> 'udp', dport
=> '161:162' },
307 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '161' },
310 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '783' },
313 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '22' },
316 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3690' },
319 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3874' },
320 { action
=> 'PARAM', proto
=> 'udp', dport
=> '3740' },
321 { action
=> 'PARAM', proto
=> '41' },
322 { action
=> 'PARAM', proto
=> 'udp', dport
=> '5072,8374' },
325 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3128' },
328 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '587' },
331 { action
=> 'PARAM', proto
=> 'udp', dport
=> '514' },
332 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '514' },
335 { action
=> 'PARAM', proto
=> 'udp', dport
=> '69' },
338 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '23' },
341 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '992' },
344 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '37' },
347 { action
=> 'PARAM', proto
=> 'udp', dport
=> '33434:33524' },
348 { action
=> 'PARAM', proto
=> 'icmp', dport
=> 'echo-request' },
351 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5900:5909' },
354 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5500' },
357 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '80' },
358 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '443' },
361 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '8080' },
364 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '10000' },
367 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '43' },
371 my $pve_fw_parsed_macros;
372 my $pve_fw_preferred_macro_names = {};
374 my $pve_std_chains = {
375 'PVEFW-SET-ACCEPT-MARK' => [
376 "-j MARK --set-mark 1",
378 'PVEFW-DropBroadcast' => [
379 # same as shorewall 'Broadcast'
380 # simply DROP BROADCAST/MULTICAST/ANYCAST
381 # we can use this to reduce logging
382 { action
=> 'DROP', dsttype
=> 'BROADCAST' },
383 { action
=> 'DROP', dsttype
=> 'MULTICAST' },
384 { action
=> 'DROP', dsttype
=> 'ANYCAST' },
385 { action
=> 'DROP', dest
=> '224.0.0.0/4' },
388 # same as shorewall 'reject'
389 { action
=> 'DROP', dsttype
=> 'BROADCAST' },
390 { action
=> 'DROP', source
=> '224.0.0.0/4' },
391 { action
=> 'DROP', proto
=> 'icmp' },
392 "-p tcp -j REJECT --reject-with tcp-reset",
393 "-p udp -j REJECT --reject-with icmp-port-unreachable",
394 "-p icmp -j REJECT --reject-with icmp-host-unreachable",
395 "-j REJECT --reject-with icmp-host-prohibited",
398 # same as shorewall 'Drop', which is equal to DROP,
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
=> 'DROP', proto
=> 'udp', dport
=> '135,445', nbdport
=> 2 },
411 { action
=> 'DROP', proto
=> 'udp', dport
=> '137:139'},
412 { action
=> 'DROP', proto
=> 'udp', dport
=> '1024:65535', sport
=> 137 },
413 { action
=> 'DROP', 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",
418 { action
=> 'DROP', proto
=> 'udp', sport
=> 53 },
421 # same as shorewall 'Reject', which is equal to Reject,
422 # but REJECT/DROP some packages to reduce logging,
423 # and ACCEPT critical ICMP types
424 { action
=> 'PVEFW-reject', proto
=> 'tcp', dport
=> '43' }, # REJECT 'auth'
425 # we are not interested in BROADCAST/MULTICAST/ANYCAST
426 { action
=> 'PVEFW-DropBroadcast' },
427 # ACCEPT critical ICMP types
428 { action
=> 'ACCEPT', proto
=> 'icmp', dport
=> 'fragmentation-needed' },
429 { action
=> 'ACCEPT', proto
=> 'icmp', dport
=> 'time-exceeded' },
430 # Drop packets with INVALID state
431 "-m conntrack --ctstate INVALID -j DROP",
432 # Drop Microsoft SMB noise
433 { action
=> 'PVEFW-reject', proto
=> 'udp', dport
=> '135,445', nbdport
=> 2 },
434 { action
=> 'PVEFW-reject', proto
=> 'udp', dport
=> '137:139'},
435 { action
=> 'PVEFW-reject', proto
=> 'udp', dport
=> '1024:65535', sport
=> 137 },
436 { action
=> 'PVEFW-reject', proto
=> 'tcp', dport
=> '135,139,445', nbdport
=> 3 },
437 { action
=> 'DROP', proto
=> 'udp', dport
=> 1900 }, # UPnP
438 # Drop new/NotSyn traffic so that it doesn't get logged
439 "-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -j DROP",
441 { action
=> 'DROP', proto
=> 'udp', sport
=> 53 },
443 'PVEFW-tcpflags' => [
444 # same as shorewall tcpflags action.
445 # Packets arriving on this interface are checked for som illegal combinations of TCP flags
446 "-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG FIN,PSH,URG -g PVEFW-logflags",
447 "-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG NONE -g PVEFW-logflags",
448 "-p tcp -m tcp --tcp-flags SYN,RST SYN,RST -g PVEFW-logflags",
449 "-p tcp -m tcp --tcp-flags FIN,SYN FIN,SYN -g PVEFW-logflags",
450 "-p tcp -m tcp --sport 0 --tcp-flags FIN,SYN,RST,ACK SYN -g PVEFW-logflags",
453 # same as shorewall smurfs action
454 # Filter packets for smurfs (packets with a broadcast address as the source).
455 "-s 0.0.0.0/32 -j RETURN",
456 "-m addrtype --src-type BROADCAST -g PVEFW-smurflog",
457 "-s 224.0.0.0/4 -g PVEFW-smurflog",
461 # iptables -p icmp -h
462 my $icmp_type_names = {
465 'destination-unreachable' => 1,
466 'network-unreachable' => 1,
467 'host-unreachable' => 1,
468 'protocol-unreachable' => 1,
469 'port-unreachable' => 1,
470 'fragmentation-needed' => 1,
471 'source-route-failed' => 1,
472 'network-unknown' => 1,
474 'network-prohibited' => 1,
475 'host-prohibited' => 1,
476 'TOS-network-unreachable' => 1,
477 'TOS-host-unreachable' => 1,
478 'communication-prohibited' => 1,
479 'host-precedence-violation' => 1,
480 'precedence-cutoff' => 1,
481 'source-quench' => 1,
483 'network-redirect' => 1,
484 'host-redirect' => 1,
485 'TOS-network-redirect' => 1,
486 'TOS-host-redirect' => 1,
488 'router-advertisement' => 1,
489 'router-solicitation' => 1,
490 'time-exceeded' => 1,
491 'ttl-zero-during-transit' => 1,
492 'ttl-zero-during-reassembly' => 1,
493 'parameter-problem' => 1,
494 'ip-header-bad' => 1,
495 'required-option-missing' => 1,
496 'timestamp-request' => 1,
497 'timestamp-reply' => 1,
498 'address-mask-request' => 1,
499 'address-mask-reply' => 1,
502 sub get_firewall_macros
{
504 return $pve_fw_parsed_macros if $pve_fw_parsed_macros;
506 $pve_fw_parsed_macros = {};
508 foreach my $k (keys %$pve_fw_macros) {
511 my $macro = $pve_fw_macros->{$k};
512 $pve_fw_preferred_macro_names->{$name} = $k;
513 $pve_fw_parsed_macros->{$name} = $macro;
516 return $pve_fw_parsed_macros;
521 sub get_etc_services
{
523 return $etc_services if $etc_services;
525 my $filename = "/etc/services";
527 my $fh = IO
::File-
>new($filename, O_RDONLY
);
529 warn "unable to read '$filename' - $!\n";
535 while (my $line = <$fh>) {
537 next if $line =~m/^#/;
538 next if ($line =~m/^\s*$/);
540 if ($line =~ m!^(\S+)\s+(\S+)/(tcp|udp).*$!) {
541 $services->{byid
}->{$2}->{name
} = $1;
542 $services->{byid
}->{$2}->{port
} = $2;
543 $services->{byid
}->{$2}->{$3} = 1;
544 $services->{byname
}->{$1} = $services->{byid
}->{$2};
550 $etc_services = $services;
553 return $etc_services;
558 sub get_etc_protocols
{
559 return $etc_protocols if $etc_protocols;
561 my $filename = "/etc/protocols";
563 my $fh = IO
::File-
>new($filename, O_RDONLY
);
565 warn "unable to read '$filename' - $!\n";
571 while (my $line = <$fh>) {
573 next if $line =~m/^#/;
574 next if ($line =~m/^\s*$/);
576 if ($line =~ m!^(\S+)\s+(\d+)\s+.*$!) {
577 $protocols->{byid
}->{$2}->{name
} = $1;
578 $protocols->{byname
}->{$1} = $protocols->{byid
}->{$2};
584 $etc_protocols = $protocols;
586 return $etc_protocols;
589 sub parse_address_list
{
593 foreach my $aor (split(/,/, $str)) {
594 if (!Net
::IP-
>new($aor)) {
595 my $err = Net
::IP
::Error
();
596 die "invalid IP address: $err\n";
604 sub parse_port_name_number_or_range
{
607 my $services = PVE
::Firewall
::get_etc_services
();
609 foreach my $item (split(/,/, $str)) {
613 foreach my $pon (split(':', $item, 2)) {
614 $pon = $services->{byname
}->{$pon}->{port
} if $services->{byname
}->{$pon}->{port
};
615 if ($pon =~ m/^\d+$/){
616 die "invalid port '$pon'\n" if $pon < 0 && $pon > 65535;
617 die "port '$pon' must be bigger than port '$oldpon' \n" if $oldpon && ($pon < $oldpon);
620 die "invalid port $services->{byname}->{$pon}\n" if !$services->{byname
}->{$pon};
628 my $bridge_firewall_enabled = 0;
630 sub enable_bridge_firewall
{
632 return if $bridge_firewall_enabled; # only once
634 PVE
::ProcFSTools
::write_proc_entry
("/proc/sys/net/bridge/bridge-nf-call-iptables", "1");
635 PVE
::ProcFSTools
::write_proc_entry
("/proc/sys/net/bridge/bridge-nf-call-ip6tables", "1");
637 # make sure syncookies are enabled (which is default on newer 3.X kernels anyways)
638 PVE
::ProcFSTools
::write_proc_entry
("/proc/sys/net/ipv4/tcp_syncookies", "1");
640 $bridge_firewall_enabled = 1;
643 my $rule_format = "%-15s %-30s %-30s %-15s %-15s %-15s\n";
648 run_command
("/sbin/iptables $cmd", outfunc
=> sub {}, errfunc
=> sub {});
651 sub iptables_restore_cmdlist
{
654 run_command
("/sbin/iptables-restore -n", input
=> $cmdlist);
657 sub iptables_get_chains
{
661 # check what chains we want to track
662 my $is_pvefw_chain = sub {
665 return 1 if $name =~ m/^PVEFW-\S+$/;
667 return 1 if $name =~ m/^tap\d+i\d+-(:?IN|OUT)$/;
669 return 1 if $name =~ m/^veth\d+.\d+-(:?IN|OUT)$/; # fixme: dev name is configurable
671 return 1 if $name =~ m/^venet0-\d+-(:?IN|OUT)$/;
673 return 1 if $name =~ m/^vmbr\d+-(:?FW|IN|OUT)$/;
674 return 1 if $name =~ m/^GROUP-(:?[^\s\-]+)-(:?IN|OUT)$/;
684 return if $line =~ m/^#/;
685 return if $line =~ m/^\s*$/;
687 if ($line =~ m/^\*(\S+)$/) {
692 return if $table ne 'filter';
694 if ($line =~ m/^:(\S+)\s/) {
696 return if !&$is_pvefw_chain($chain);
697 $res->{$chain} = "unknown";
698 } elsif ($line =~ m/^-A\s+(\S+)\s.*--comment\s+\"PVESIG:(\S+)\"/) {
699 my ($chain, $sig) = ($1, $2);
700 return if !&$is_pvefw_chain($chain);
701 $res->{$chain} = $sig;
703 # simply ignore the rest
708 run_command
("/sbin/iptables-save", outfunc
=> $parser);
713 sub iptables_chain_exist
{
717 iptables
("-n --list $chain");
724 sub iptables_rule_exist
{
728 iptables
("-C $rule");
735 sub ruleset_generate_cmdstr
{
736 my ($ruleset, $chain, $rule, $actions, $goto) = @_;
738 return if $rule->{disable
};
742 push @cmd, "-i $rule->{iface_in}" if $rule->{iface_in
};
743 push @cmd, "-o $rule->{iface_out}" if $rule->{iface_out
};
745 push @cmd, "-m iprange --src-range" if $rule->{nbsource
} && $rule->{nbsource
} > 1;
746 push @cmd, "-s $rule->{source}" if $rule->{source
};
747 push @cmd, "-m iprange --dst-range" if $rule->{nbdest
} && $rule->{nbdest
} > 1;
748 push @cmd, "-d $rule->{dest}" if $rule->{dest
};
750 if ($rule->{proto
}) {
751 push @cmd, "-p $rule->{proto}";
754 $multiport++ if $rule->{nbdport
} && ($rule->{nbdport
} > 1);
755 $multiport++ if $rule->{nbsport
} && ($rule->{nbsport
} > 1);
757 push @cmd, "--match multiport" if $multiport;
759 die "multiport: option '--sports' cannot be used together with '--dports'\n"
760 if ($multiport == 2) && ($rule->{dport
} ne $rule->{sport
});
762 if ($rule->{dport
}) {
763 if ($rule->{proto
} && $rule->{proto
} eq 'icmp') {
764 # Note: we use dport to store --icmp-type
765 die "unknown icmp-type '$rule->{dport}'\n" if !defined($icmp_type_names->{$rule->{dport
}});
766 push @cmd, "-m icmp --icmp-type $rule->{dport}";
768 if ($rule->{nbdport
} && $rule->{nbdport
} > 1) {
769 if ($multiport == 2) {
770 push @cmd, "--ports $rule->{dport}";
772 push @cmd, "--dports $rule->{dport}";
775 push @cmd, "--dport $rule->{dport}";
780 if ($rule->{sport
}) {
781 if ($rule->{nbsport
} && $rule->{nbsport
} > 1) {
782 push @cmd, "--sports $rule->{sport}" if $multiport != 2;
784 push @cmd, "--sport $rule->{sport}";
787 } elsif ($rule->{dport
} || $rule->{sport
}) {
788 warn "ignoring destination port '$rule->{dport}' - no protocol specified\n" if $rule->{dport
};
789 warn "ignoring source port '$rule->{sport}' - no protocol specified\n" if $rule->{sport
};
792 push @cmd, "-m addrtype --dst-type $rule->{dsttype}" if $rule->{dsttype
};
794 if (my $action = $rule->{action
}) {
795 $action = $actions->{$action} if defined($actions->{$action});
796 $goto = 1 if !defined($goto) && $action eq 'PVEFW-SET-ACCEPT-MARK';
797 push @cmd, $goto ?
"-g $action" : "-j $action";
800 return scalar(@cmd) ?
join(' ', @cmd) : undef;
803 sub ruleset_generate_rule
{
804 my ($ruleset, $chain, $rule, $actions, $goto) = @_;
806 if (my $cmdstr = ruleset_generate_cmdstr
($ruleset, $chain, $rule, $actions, $goto)) {
807 ruleset_addrule
($ruleset, $chain, $cmdstr);
810 sub ruleset_generate_rule_insert
{
811 my ($ruleset, $chain, $rule, $actions, $goto) = @_;
813 if (my $cmdstr = ruleset_generate_cmdstr
($ruleset, $chain, $rule, $actions, $goto)) {
814 ruleset_insertrule
($ruleset, $chain, $cmdstr);
818 sub ruleset_create_chain
{
819 my ($ruleset, $chain) = @_;
821 die "Invalid chain name '$chain' (28 char max)\n" if length($chain) > 28;
823 die "chain '$chain' already exists\n" if $ruleset->{$chain};
825 $ruleset->{$chain} = [];
828 sub ruleset_chain_exist
{
829 my ($ruleset, $chain) = @_;
831 return $ruleset->{$chain} ?
1 : undef;
834 sub ruleset_addrule
{
835 my ($ruleset, $chain, $rule) = @_;
837 die "no such chain '$chain'\n" if !$ruleset->{$chain};
839 push @{$ruleset->{$chain}}, "-A $chain $rule";
842 sub ruleset_insertrule
{
843 my ($ruleset, $chain, $rule) = @_;
845 die "no such chain '$chain'\n" if !$ruleset->{$chain};
847 unshift @{$ruleset->{$chain}}, "-A $chain $rule";
850 sub generate_bridge_chains
{
851 my ($ruleset, $hostfw_conf, $bridge, $routing_table) = @_;
853 my $options = $hostfw_conf->{options
} || {};
855 die "error: detected direct route to bridge '$bridge'\n"
856 if !$options->{allow_bridge_route
} && $routing_table->{$bridge};
858 if (!ruleset_chain_exist
($ruleset, "$bridge-FW")) {
859 ruleset_create_chain
($ruleset, "$bridge-FW");
860 ruleset_addrule
($ruleset, "PVEFW-FORWARD", "-o $bridge -m physdev --physdev-is-out -j $bridge-FW");
861 ruleset_addrule
($ruleset, "PVEFW-FORWARD", "-i $bridge -m physdev --physdev-is-in -j $bridge-FW");
864 if (!ruleset_chain_exist
($ruleset, "$bridge-OUT")) {
865 ruleset_create_chain
($ruleset, "$bridge-OUT");
866 ruleset_addrule
($ruleset, "$bridge-FW", "-m physdev --physdev-is-in -j $bridge-OUT");
867 ruleset_insertrule
($ruleset, "PVEFW-INPUT", "-i $bridge -m physdev --physdev-is-in -j $bridge-OUT");
870 if (!ruleset_chain_exist
($ruleset, "$bridge-IN")) {
871 ruleset_create_chain
($ruleset, "$bridge-IN");
872 ruleset_addrule
($ruleset, "$bridge-FW", "-m physdev --physdev-is-out -j $bridge-IN");
873 ruleset_addrule
($ruleset, "$bridge-FW", "-m mark --mark 1 -j ACCEPT");
874 # accept traffic to unmanaged bridge ports
875 ruleset_addrule
($ruleset, "$bridge-FW", "-m physdev --physdev-is-out -j ACCEPT ");
879 sub ruleset_add_chain_policy
{
880 my ($ruleset, $chain, $policy, $loglevel, $accept_action) = @_;
882 if ($policy eq 'ACCEPT') {
884 ruleset_generate_rule
($ruleset, $chain, { action
=> 'ACCEPT' },
885 { ACCEPT
=> $accept_action});
887 } elsif ($policy eq 'DROP') {
889 ruleset_addrule
($ruleset, $chain, "-j PVEFW-Drop");
891 ruleset_addrule
($ruleset, $chain, "-j LOG --log-prefix \"$chain-dropped: \" --log-level $loglevel")
892 if defined($loglevel);
894 ruleset_addrule
($ruleset, $chain, "-j DROP");
895 } elsif ($policy eq 'REJECT') {
896 ruleset_addrule
($ruleset, $chain, "-j PVEFW-Reject");
898 ruleset_addrule
($ruleset, $chain, "-j LOG --log-prefix \"$chain-reject: \" --log-level $loglevel")
899 if defined($loglevel);
901 ruleset_addrule
($ruleset, $chain, "-g PVEFW-reject");
904 die "internal error: unknown policy '$policy'";
908 sub ruleset_create_vm_chain
{
909 my ($ruleset, $chain, $options, $macaddr, $direction) = @_;
911 ruleset_create_chain
($ruleset, $chain);
913 if (!(defined($options->{nosmurfs
}) && $options->{nosmurfs
} == 0)) {
914 ruleset_addrule
($ruleset, $chain, "-m conntrack --ctstate INVALID,NEW -j PVEFW-smurfs");
917 if (!(defined($options->{dhcp
}) && $options->{dhcp
} == 0)) {
918 ruleset_addrule
($ruleset, $chain, "-p udp -m udp --dport 67:68 -j ACCEPT");
921 if ($options->{tcpflags
}) {
922 ruleset_addrule
($ruleset, $chain, "-p tcp -j PVEFW-tcpflags");
925 ruleset_addrule
($ruleset, $chain, "-m conntrack --ctstate INVALID -j DROP");
926 ruleset_addrule
($ruleset, $chain, "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT");
928 if ($direction eq 'OUT') {
929 if (defined($macaddr) && !(defined($options->{macfilter
}) && $options->{macfilter
} == 0)) {
930 ruleset_addrule
($ruleset, $chain, "-m mac ! --mac-source $macaddr -j DROP");
932 ruleset_addrule
($ruleset, $chain, "-j MARK --set-mark 0"); # clear mark
936 sub ruleset_generate_vm_rules
{
937 my ($ruleset, $rules, $groups_conf, $chain, $netid, $direction) = @_;
939 my $lc_direction = lc($direction);
941 foreach my $rule (@$rules) {
942 next if $rule->{iface
} && $rule->{iface
} ne $netid;
943 next if $rule->{disable
};
944 if ($rule->{type
} eq 'group') {
945 my $group_chain = "GROUP-$rule->{action}-$direction";
946 if(!ruleset_chain_exist
($ruleset, $group_chain)){
947 generate_group_rules
($ruleset, $groups_conf, $rule->{action
});
949 ruleset_addrule
($ruleset, $chain, "-j $group_chain");
950 ruleset_addrule
($ruleset, $chain, "-m mark --mark 1 -j RETURN")
951 if $direction eq 'OUT';
953 next if $rule->{type
} ne $lc_direction;
954 if ($direction eq 'OUT') {
955 ruleset_generate_rule
($ruleset, $chain, $rule,
956 { ACCEPT
=> "PVEFW-SET-ACCEPT-MARK", REJECT
=> "PVEFW-reject" });
958 ruleset_generate_rule
($ruleset, $chain, $rule, { REJECT
=> "PVEFW-reject" });
964 sub generate_venet_rules_direction
{
965 my ($ruleset, $groups_conf, $vmfw_conf, $vmid, $ip, $direction) = @_;
967 parse_address_list
($ip); # make sure we have a valid $ip list
969 my $lc_direction = lc($direction);
971 my $rules = $vmfw_conf->{rules
};
973 my $options = $vmfw_conf->{options
};
974 my $loglevel = get_option_log_level
($options, "log_level_${lc_direction}");
976 my $chain = "venet0-$vmid-$direction";
978 ruleset_create_vm_chain
($ruleset, $chain, $options, undef, $direction);
980 ruleset_generate_vm_rules
($ruleset, $rules, $groups_conf, $chain, 'venet', $direction);
985 if ($direction eq 'OUT') {
986 $policy = $options->{policy_out
} || 'ACCEPT'; # allow everything by default
988 $policy = $options->{policy_in
} || 'DROP'; # allow nothing by default
991 my $accept_action = $direction eq 'OUT' ?
"PVEFW-SET-ACCEPT-MARK" : "ACCEPT";
992 ruleset_add_chain_policy
($ruleset, $chain, $policy, $loglevel, $accept_action);
994 # plug into FORWARD, INPUT and OUTPUT chain
995 if ($direction eq 'OUT') {
996 ruleset_generate_rule_insert
($ruleset, "PVEFW-FORWARD", {
999 iface_in
=> 'venet0'});
1001 ruleset_generate_rule_insert
($ruleset, "PVEFW-INPUT", {
1004 iface_in
=> 'venet0'});
1006 ruleset_generate_rule
($ruleset, "PVEFW-FORWARD", {
1009 iface_out
=> 'venet0'});
1011 ruleset_generate_rule
($ruleset, "PVEFW-OUTPUT", {
1014 iface_out
=> 'venet0'});
1018 sub generate_tap_rules_direction
{
1019 my ($ruleset, $groups_conf, $iface, $netid, $macaddr, $vmfw_conf, $bridge, $direction) = @_;
1021 my $lc_direction = lc($direction);
1023 my $rules = $vmfw_conf->{rules
};
1025 my $options = $vmfw_conf->{options
};
1026 my $loglevel = get_option_log_level
($options, "log_level_${lc_direction}");
1028 my $tapchain = "$iface-$direction";
1030 ruleset_create_vm_chain
($ruleset, $tapchain, $options, $macaddr, $direction);
1032 ruleset_generate_vm_rules
($ruleset, $rules, $groups_conf, $tapchain, $netid, $direction);
1037 if ($direction eq 'OUT') {
1038 $policy = $options->{policy_out
} || 'ACCEPT'; # allow everything by default
1040 $policy = $options->{policy_in
} || 'DROP'; # allow nothing by default
1043 my $accept_action = $direction eq 'OUT' ?
"PVEFW-SET-ACCEPT-MARK" : "ACCEPT";
1044 ruleset_add_chain_policy
($ruleset, $tapchain, $policy, $loglevel, $accept_action);
1046 # plug the tap chain to bridge chain
1047 if ($direction eq 'IN') {
1048 ruleset_insertrule
($ruleset, "$bridge-IN",
1049 "-m physdev --physdev-is-bridged --physdev-out $iface -j $tapchain");
1051 ruleset_insertrule
($ruleset, "$bridge-OUT",
1052 "-m physdev --physdev-in $iface -j $tapchain");
1056 sub enable_host_firewall
{
1057 my ($ruleset, $hostfw_conf, $groups_conf) = @_;
1059 # fixme: allow security groups
1061 my $options = $hostfw_conf->{options
};
1062 my $rules = $hostfw_conf->{rules
};
1064 # host inbound firewall
1065 my $chain = "PVEFW-HOST-IN";
1066 ruleset_create_chain
($ruleset, $chain);
1068 my $loglevel = get_option_log_level
($options, "log_level_in");
1070 ruleset_addrule
($ruleset, $chain, "-m conntrack --ctstate INVALID -j DROP");
1071 ruleset_addrule
($ruleset, $chain, "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT");
1072 ruleset_addrule
($ruleset, $chain, "-i lo -j ACCEPT");
1073 ruleset_addrule
($ruleset, $chain, "-m addrtype --dst-type MULTICAST -j ACCEPT");
1074 ruleset_addrule
($ruleset, $chain, "-p udp -m conntrack --ctstate NEW --dport 5404:5405 -j ACCEPT");
1075 ruleset_addrule
($ruleset, $chain, "-p udp -m udp --dport 9000 -j ACCEPT"); #corosync
1077 # we use RETURN because we need to check also tap rules
1078 my $accept_action = 'RETURN';
1080 foreach my $rule (@$rules) {
1081 next if $rule->{type
} ne 'in';
1082 ruleset_generate_rule
($ruleset, $chain, $rule, { ACCEPT
=> $accept_action, REJECT
=> "PVEFW-reject" });
1085 # implement input policy
1086 my $policy = $options->{policy_in
} || 'DROP'; # allow nothing by default
1087 ruleset_add_chain_policy
($ruleset, $chain, $policy, $loglevel, $accept_action);
1089 # host outbound firewall
1090 $chain = "PVEFW-HOST-OUT";
1091 ruleset_create_chain
($ruleset, $chain);
1093 $loglevel = get_option_log_level
($options, "log_level_out");
1095 ruleset_addrule
($ruleset, $chain, "-m conntrack --ctstate INVALID -j DROP");
1096 ruleset_addrule
($ruleset, $chain, "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT");
1097 ruleset_addrule
($ruleset, $chain, "-o lo -j ACCEPT");
1098 ruleset_addrule
($ruleset, $chain, "-m addrtype --dst-type MULTICAST -j ACCEPT");
1099 ruleset_addrule
($ruleset, $chain, "-p udp -m conntrack --ctstate NEW --dport 5404:5405 -j ACCEPT");
1100 ruleset_addrule
($ruleset, $chain, "-p udp -m udp --dport 9000 -j ACCEPT"); #corosync
1102 # we use RETURN because we may want to check other thigs later
1103 $accept_action = 'RETURN';
1105 foreach my $rule (@$rules) {
1106 next if $rule->{type
} ne 'out';
1107 ruleset_generate_rule
($ruleset, $chain, $rule, { ACCEPT
=> $accept_action, REJECT
=> "PVEFW-reject" });
1110 # implement output policy
1111 $policy = $options->{policy_out
} || 'ACCEPT'; # allow everything by default
1112 ruleset_add_chain_policy
($ruleset, $chain, $policy, $loglevel, $accept_action);
1114 ruleset_addrule
($ruleset, "PVEFW-OUTPUT", "-j PVEFW-HOST-OUT");
1115 ruleset_addrule
($ruleset, "PVEFW-INPUT", "-j PVEFW-HOST-IN");
1118 sub generate_group_rules
{
1119 my ($ruleset, $groups_conf, $group) = @_;
1121 die "no such security group '$group'\n" if !$groups_conf->{$group};
1123 my $rules = $groups_conf->{$group}->{rules
};
1125 my $chain = "GROUP-${group}-IN";
1127 ruleset_create_chain
($ruleset, $chain);
1129 foreach my $rule (@$rules) {
1130 next if $rule->{type
} ne 'in';
1131 ruleset_generate_rule
($ruleset, $chain, $rule, { REJECT
=> "PVEFW-reject" });
1134 $chain = "GROUP-${group}-OUT";
1136 ruleset_create_chain
($ruleset, $chain);
1137 ruleset_addrule
($ruleset, $chain, "-j MARK --set-mark 0"); # clear mark
1139 foreach my $rule (@$rules) {
1140 next if $rule->{type
} ne 'out';
1141 # we use PVEFW-SET-ACCEPT-MARK (Instead of ACCEPT) because we need to
1142 # check also other tap rules later
1143 ruleset_generate_rule
($ruleset, $chain, $rule,
1144 { ACCEPT
=> 'PVEFW-SET-ACCEPT-MARK', REJECT
=> "PVEFW-reject" });
1149 my $valid_netdev_names = {};
1150 for (my $i = 0; $i < $MAX_NETS; $i++) {
1151 $valid_netdev_names->{"net$i"} = 1;
1155 my ($line, $need_iface, $allow_groups) = @_;
1157 my $macros = get_firewall_macros
();
1158 my $protocols = get_etc_protocols
();
1160 my ($type, $action, $iface, $source, $dest, $proto, $dport, $sport);
1162 # we can add single line comments to the end of the rule
1163 my $comment = $1 if $line =~ s/#\s*(.*?)\s*$//;
1165 # we can disable a rule when prefixed with '|'
1166 my $disable = 1 if $line =~ s/^\|//;
1168 my @data = split(/\s+/, $line);
1169 my $expected_elements = $need_iface ?
8 : 7;
1171 die "wrong number of rule elements\n" if scalar(@data) > $expected_elements;
1174 ($type, $action, $iface, $source, $dest, $proto, $dport, $sport) = @data
1176 ($type, $action, $source, $dest, $proto, $dport, $sport) = @data;
1179 die "incomplete rule\n" if ! ($type && $action);
1186 if ($type eq 'in' || $type eq 'out') {
1187 if ($action =~ m/^(ACCEPT|DROP|REJECT)$/) {
1189 } elsif ($action =~ m/^(\S+)\((ACCEPT|DROP|REJECT)\)$/) {
1190 ($macro_name, $action) = ($1, $2);
1191 my $lc_macro_name = lc($macro_name);
1192 my $preferred_name = $pve_fw_preferred_macro_names->{$lc_macro_name};
1193 $macro_name = $preferred_name if $preferred_name;
1194 $macro = $macros->{$lc_macro_name};
1195 die "unknown macro '$macro_name'\n" if !$macro;
1197 die "unknown action '$action'\n";
1199 } elsif ($type eq 'group') {
1200 die "wrong number of rule elements\n" if scalar(@data) != 3;
1201 die "groups disabled\n" if !$allow_groups;
1203 die "invalid characters in group name\n" if $action !~ m/^[A-Za-z0-9_\-]+$/;
1205 die "unknown rule type '$type'\n";
1209 $iface = undef if $iface && $iface eq '-';
1212 $proto = undef if $proto && $proto eq '-';
1213 die "unknown protokol '$proto'\n" if $proto &&
1214 !(defined($protocols->{byname
}->{$proto}) ||
1215 defined($protocols->{byid
}->{$proto}));
1217 $source = undef if $source && $source eq '-';
1218 $dest = undef if $dest && $dest eq '-';
1220 $dport = undef if $dport && $dport eq '-';
1221 $sport = undef if $sport && $sport eq '-';
1223 my $nbsource = undef;
1226 $nbsource = parse_address_list
($source) if $source;
1227 $nbdest = parse_address_list
($dest) if $dest;
1233 disable
=> $disable,
1234 comment
=> $comment,
1239 nbsource
=> $nbsource,
1247 foreach my $templ (@$macro) {
1249 my $param_used = {};
1250 foreach my $k (keys %$templ) {
1251 my $v = $templ->{$k};
1252 if ($v eq 'PARAM') {
1254 $param_used->{$k} = 1;
1255 } elsif ($v eq 'DEST') {
1256 $v = $param->{dest
};
1257 $param_used->{dest
} = 1;
1258 } elsif ($v eq 'SOURCE') {
1259 $v = $param->{source
};
1260 $param_used->{source
} = 1;
1263 die "missing parameter '$k' in macro '$macro_name'\n" if !defined($v);
1266 foreach my $k (keys %$param) {
1267 next if !defined($param->{$k});
1268 next if $param_used->{$k};
1269 if (defined($rule->{$k})) {
1270 die "parameter '$k' already define in macro (value = '$rule->{$k}')\n"
1271 if $rule->{$k} ne $param->{$k};
1273 $rule->{$k} = $param->{$k};
1276 push @$rules, $rule;
1279 push @$rules, $param;
1282 foreach my $rule (@$rules) {
1283 $rule->{nbdport
} = parse_port_name_number_or_range
($rule->{dport
})
1284 if defined($rule->{dport
});
1285 $rule->{nbsport
} = parse_port_name_number_or_range
($rule->{sport
})
1286 if defined($rule->{sport
});
1292 sub parse_vmfw_option
{
1297 my $loglevels = "emerg|alert|crit|err|warning|notice|info|debug|nolog";
1299 if ($line =~ m/^(enable|dhcp|macfilter|nosmurfs|tcpflags):\s*(0|1)\s*$/i) {
1302 } elsif ($line =~ m/^(log_level_in|log_level_out):\s*(($loglevels)\s*)?$/i) {
1304 $value = $2 ?
lc($3) : '';
1305 } elsif ($line =~ m/^(policy_(in|out)):\s*(ACCEPT|DROP|REJECT)\s*$/i) {
1310 die "can't parse option '$line'\n"
1313 return ($opt, $value);
1316 sub parse_hostfw_option
{
1321 my $loglevels = "emerg|alert|crit|err|warning|notice|info|debug|nolog";
1323 if ($line =~ m/^(enable|dhcp|nosmurfs|tcpflags|allow_bridge_route):\s*(0|1)\s*$/i) {
1326 } elsif ($line =~ m/^(log_level_in|log_level_out|tcp_flags_log_level|smurf_log_level):\s*(($loglevels)\s*)?$/i) {
1328 $value = $2 ?
lc($3) : '';
1329 } elsif ($line =~ m/^(policy_(in|out)):\s*(ACCEPT|DROP|REJECT)\s*$/i) {
1332 } elsif ($line =~ m/^(nf_conntrack_max):\s*(\d+)\s*$/i) {
1337 die "can't parse option '$line'\n"
1340 return ($opt, $value);
1343 sub parse_vm_fw_rules
{
1344 my ($filename, $fh) = @_;
1346 my $res = { rules
=> [], options
=> {}};
1350 while (defined(my $line = <$fh>)) {
1351 next if $line =~ m/^#/;
1352 next if $line =~ m/^\s*$/;
1354 my $linenr = $fh->input_line_number();
1355 my $prefix = "$filename (line $linenr)";
1357 if ($line =~ m/^\[(\S+)\]\s*$/i) {
1359 warn "$prefix: ignore unknown section '$section'\n" if !$res->{$section};
1363 warn "$prefix: skip line - no section";
1367 next if !$res->{$section}; # skip undefined section
1369 if ($section eq 'options') {
1371 my ($opt, $value) = parse_vmfw_option
($line);
1372 $res->{options
}->{$opt} = $value;
1374 warn "$prefix: $@" if $@;
1379 eval { $rules = parse_fw_rule
($line, 1, 1); };
1381 warn "$prefix: $err";
1385 push @{$res->{$section}}, @$rules;
1391 sub parse_host_fw_rules
{
1392 my ($filename, $fh) = @_;
1394 my $res = { rules
=> [], options
=> {}};
1398 while (defined(my $line = <$fh>)) {
1399 next if $line =~ m/^#/;
1400 next if $line =~ m/^\s*$/;
1402 my $linenr = $fh->input_line_number();
1403 my $prefix = "$filename (line $linenr)";
1405 if ($line =~ m/^\[(\S+)\]\s*$/i) {
1407 warn "$prefix: ignore unknown section '$section'\n" if !$res->{$section};
1411 warn "$prefix: skip line - no section";
1415 next if !$res->{$section}; # skip undefined section
1417 if ($section eq 'options') {
1419 my ($opt, $value) = parse_hostfw_option
($line);
1420 $res->{options
}->{$opt} = $value;
1422 warn "$prefix: $@" if $@;
1427 eval { $rules = parse_fw_rule
($line, 1, 1); };
1429 warn "$prefix: $err";
1433 push @{$res->{$section}}, @$rules;
1439 sub parse_group_fw_rules
{
1440 my ($filename, $fh) = @_;
1445 my $res = { rules
=> [] };
1447 while (defined(my $line = <$fh>)) {
1448 next if $line =~ m/^#/;
1449 next if $line =~ m/^\s*$/;
1451 my $linenr = $fh->input_line_number();
1452 my $prefix = "$filename (line $linenr)";
1454 if ($line =~ m/^\[group\s+(\S+)\]\s*$/i) {
1459 if (!$section || !$group) {
1460 warn "$prefix: skip line - no section";
1465 eval { $rules = parse_fw_rule
($line, 0, 0); };
1467 warn "$prefix: $err";
1471 push @{$res->{$group}->{$section}}, @$rules;
1478 my ($code, @param) = @_;
1482 my $res = lock_file
($pve_fw_lock_filename, $timeout, $code, @param);
1489 sub read_local_vm_config
{
1494 my $vmdata = { openvz
=> $openvz, qemu
=> $qemu };
1496 my $vmlist = PVE
::Cluster
::get_vmlist
();
1497 return $vmdata if !$vmlist || !$vmlist->{ids
};
1498 my $ids = $vmlist->{ids
};
1500 foreach my $vmid (keys %$ids) {
1501 next if !$vmid; # skip VE0
1502 my $d = $ids->{$vmid};
1503 next if !$d->{node
} || $d->{node
} ne $nodename;
1504 next if !$d->{type
};
1505 if ($d->{type
} eq 'openvz') {
1506 my $cfspath = PVE
::OpenVZ
::cfs_config_path
($vmid);
1507 if (my $conf = PVE
::Cluster
::cfs_read_file
($cfspath)) {
1508 $openvz->{$vmid} = $conf;
1510 } elsif ($d->{type
} eq 'qemu') {
1511 my $cfspath = PVE
::QemuServer
::cfs_config_path
($vmid);
1512 if (my $conf = PVE
::Cluster
::cfs_read_file
($cfspath)) {
1513 $qemu->{$vmid} = $conf;
1521 sub read_vm_firewall_configs
{
1523 my $vmfw_configs = {};
1525 foreach my $vmid (keys %{$vmdata->{qemu
}}, keys %{$vmdata->{openvz
}}) {
1526 my $filename = "/etc/pve/firewall/$vmid.fw";
1527 my $fh = IO
::File-
>new($filename, O_RDONLY
);
1530 $vmfw_configs->{$vmid} = parse_vm_fw_rules
($filename, $fh);
1533 return $vmfw_configs;
1536 sub get_option_log_level
{
1537 my ($options, $k) = @_;
1539 my $v = $options->{$k};
1540 $v = $default_log_level if !defined($v);
1542 return undef if $v eq '' || $v eq 'nolog';
1544 $v = $log_level_hash->{$v} if defined($log_level_hash->{$v});
1546 return $v if ($v >= 0) && ($v <= 7);
1548 warn "unknown log level ($k = '$v')\n";
1553 sub generate_std_chains
{
1554 my ($ruleset, $options) = @_;
1556 my $loglevel = get_option_log_level
($options, 'smurf_log_level');
1558 # same as shorewall smurflog.
1559 if (defined($loglevel)) {
1560 $pve_std_chains-> {'PVEFW-smurflog'} = [
1561 "-j LOG --log-prefix \"smurfs-dropped: \" --log-level $loglevel",
1565 $pve_std_chains-> {'PVEFW-smurflog'} = [ "-j DROP" ];
1568 # same as shorewall logflags action.
1569 $loglevel = get_option_log_level
($options, 'tcp_flags_log_level');
1570 if (defined($loglevel)) {
1571 $pve_std_chains-> {'PVEFW-logflags'} = [
1572 "-j LOG --log-prefix \"logflags-dropped: \" --log-level $loglevel --log-ip-options",
1576 $pve_std_chains-> {'PVEFW-logflags'} = [ "-j DROP" ];
1579 foreach my $chain (keys %$pve_std_chains) {
1580 ruleset_create_chain
($ruleset, $chain);
1581 foreach my $rule (@{$pve_std_chains->{$chain}}) {
1583 ruleset_generate_rule
($ruleset, $chain, $rule);
1585 ruleset_addrule
($ruleset, $chain, $rule);
1591 sub save_pvefw_status
{
1594 die "unknown status '$status' - internal error"
1595 if $status !~ m/^(stopped|active)$/;
1597 mkdir dirname
($pve_fw_status_filename);
1598 PVE
::Tools
::file_set_contents
($pve_fw_status_filename, $status);
1601 sub read_pvefw_status
{
1603 my $status = 'unknown';
1605 return 'stopped' if ! -f
$pve_fw_status_filename;
1608 $status = PVE
::Tools
::file_get_contents
($pve_fw_status_filename);
1615 # fixme: move to pve-common PVE::ProcFSTools
1616 sub read_proc_net_route
{
1617 my $filename = "/proc/net/route";
1621 my $fh = IO
::File-
>new ($filename, "r");
1622 return $res if !$fh;
1624 my $int_to_quad = sub {
1625 return join '.' => map { ($_[0] >> 8*(3-$_)) % 256 } (3, 2, 1, 0);
1628 while (defined(my $line = <$fh>)) {
1629 next if $line =~/^Iface\s+Destination/; # skip head
1630 my ($iface, $dest, $gateway, $metric, $mask, $mtu) = (split(/\s+/, $line))[0,1,2,6,7,8];
1631 push @{$res->{$iface}}, {
1632 dest
=> &$int_to_quad(hex($dest)),
1633 gateway
=> &$int_to_quad(hex($gateway)),
1634 mask
=> &$int_to_quad(hex($mask)),
1644 my $vmdata = read_local_vm_config
();
1645 my $vmfw_configs = read_vm_firewall_configs
($vmdata);
1647 my $routing_table = read_proc_net_route
();
1649 my $groups_conf = {};
1650 my $filename = "/etc/pve/firewall/groups.fw";
1651 if (my $fh = IO
::File-
>new($filename, O_RDONLY
)) {
1652 $groups_conf = parse_group_fw_rules
($filename, $fh);
1657 ruleset_create_chain
($ruleset, "PVEFW-INPUT");
1658 ruleset_create_chain
($ruleset, "PVEFW-OUTPUT");
1660 ruleset_create_chain
($ruleset, "PVEFW-FORWARD");
1662 my $hostfw_options = {};
1663 my $hostfw_conf = {};
1665 $filename = "/etc/pve/local/host.fw";
1666 if (my $fh = IO
::File-
>new($filename, O_RDONLY
)) {
1667 $hostfw_conf = parse_host_fw_rules
($filename, $fh);
1668 $hostfw_options = $hostfw_conf->{options
};
1671 generate_std_chains
($ruleset, $hostfw_options);
1673 my $hostfw_enable = !(defined($hostfw_options->{enable
}) && ($hostfw_options->{enable
} == 0));
1675 enable_host_firewall
($ruleset, $hostfw_conf, $groups_conf) if $hostfw_enable;
1677 # generate firewall rules for QEMU VMs
1678 foreach my $vmid (keys %{$vmdata->{qemu
}}) {
1679 my $conf = $vmdata->{qemu
}->{$vmid};
1680 my $vmfw_conf = $vmfw_configs->{$vmid};
1681 next if !$vmfw_conf;
1682 next if defined($vmfw_conf->{options
}->{enable
}) && ($vmfw_conf->{options
}->{enable
} == 0);
1684 foreach my $netid (keys %$conf) {
1685 next if $netid !~ m/^net(\d+)$/;
1686 my $net = PVE
::QemuServer
::parse_net
($conf->{$netid});
1688 my $iface = "tap${vmid}i$1";
1690 my $bridge = $net->{bridge
};
1691 next if !$bridge; # fixme: ?
1693 $bridge .= "v$net->{tag}" if $net->{tag
};
1695 generate_bridge_chains
($ruleset, $hostfw_conf, $bridge, $routing_table);
1697 my $macaddr = $net->{macaddr
};
1698 generate_tap_rules_direction
($ruleset, $groups_conf, $iface, $netid, $macaddr, $vmfw_conf, $bridge, 'IN');
1699 generate_tap_rules_direction
($ruleset, $groups_conf, $iface, $netid, $macaddr, $vmfw_conf, $bridge, 'OUT');
1703 # generate firewall rules for OpenVZ containers
1704 foreach my $vmid (keys %{$vmdata->{openvz
}}) {
1705 my $conf = $vmdata->{openvz
}->{$vmid};
1707 my $vmfw_conf = $vmfw_configs->{$vmid};
1708 next if !$vmfw_conf;
1709 next if defined($vmfw_conf->{options
}->{enable
}) && ($vmfw_conf->{options
}->{enable
} == 0);
1711 if ($conf->{ip_address
} && $conf->{ip_address
}->{value
}) {
1712 my $ip = $conf->{ip_address
}->{value
};
1713 generate_venet_rules_direction
($ruleset, $groups_conf, $vmfw_conf, $vmid, $ip, 'IN');
1714 generate_venet_rules_direction
($ruleset, $groups_conf, $vmfw_conf, $vmid, $ip, 'OUT');
1717 if ($conf->{netif
} && $conf->{netif
}->{value
}) {
1718 my $netif = PVE
::OpenVZ
::parse_netif
($conf->{netif
}->{value
});
1719 foreach my $netid (keys %$netif) {
1720 my $d = $netif->{$netid};
1721 my $bridge = $d->{bridge
};
1723 warn "no bridge device for CT $vmid iface '$netid'\n";
1727 generate_bridge_chains
($ruleset, $hostfw_conf, $bridge, $routing_table);
1729 my $macaddr = $d->{mac
};
1730 my $iface = $d->{host_ifname
};
1731 generate_tap_rules_direction
($ruleset, $groups_conf, $iface, $netid, $macaddr, $vmfw_conf, $bridge, 'IN');
1732 generate_tap_rules_direction
($ruleset, $groups_conf, $iface, $netid, $macaddr, $vmfw_conf, $bridge, 'OUT');
1737 # fixme: this is an optimization? if so, we should also drop INVALID packages?
1738 ruleset_insertrule
($ruleset, "PVEFW-FORWARD", "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT");
1740 # fixme: what log level should we use here?
1741 my $loglevel = get_option_log_level
($hostfw_options, "log_level_out");
1743 # fixme: should we really block inter-bridge traffic?
1745 # always allow traffic from containers?
1746 ruleset_addrule
($ruleset, "PVEFW-FORWARD", "-i venet0 -j RETURN");
1748 # disable interbridge routing
1749 ruleset_addrule
($ruleset, "PVEFW-FORWARD", "-o vmbr+ -j PVEFW-Drop");
1750 ruleset_addrule
($ruleset, "PVEFW-FORWARD", "-i vmbr+ -j PVEFW-Drop");
1751 ruleset_addrule
($ruleset, "PVEFW-FORWARD", "-o vmbr+ -j LOG --log-prefix \"PVEFW-FORWARD-dropped \" --log-level $loglevel");
1752 ruleset_addrule
($ruleset, "PVEFW-FORWARD", "-i vmbr+ -j LOG --log-prefix \"PVEFW-FORWARD-dropped \" --log-level $loglevel");
1753 ruleset_addrule
($ruleset, "PVEFW-FORWARD", "-o vmbr+ -j DROP");
1754 ruleset_addrule
($ruleset, "PVEFW-FORWARD", "-i vmbr+ -j DROP");
1756 return wantarray ?
($ruleset, $hostfw_conf) : $ruleset;
1759 sub get_ruleset_status
{
1760 my ($ruleset, $verbose) = @_;
1762 my $active_chains = iptables_get_chains
();
1764 my $statushash = {};
1766 foreach my $chain (sort keys %$ruleset) {
1767 my $digest = Digest
::SHA-
>new('sha1');
1768 foreach my $cmd (@{$ruleset->{$chain}}) {
1769 $digest->add("$cmd\n");
1771 my $sig = $digest->b64digest;
1772 $statushash->{$chain}->{sig
} = $sig;
1774 my $oldsig = $active_chains->{$chain};
1775 if (!defined($oldsig)) {
1776 $statushash->{$chain}->{action
} = 'create';
1778 if ($oldsig eq $sig) {
1779 $statushash->{$chain}->{action
} = 'exists';
1781 $statushash->{$chain}->{action
} = 'update';
1784 print "$statushash->{$chain}->{action} $chain ($sig)\n" if $verbose;
1785 foreach my $cmd (@{$ruleset->{$chain}}) {
1786 print "\t$cmd\n" if $verbose;
1790 foreach my $chain (sort keys %$active_chains) {
1791 if (!defined($ruleset->{$chain})) {
1792 my $sig = $active_chains->{$chain};
1793 $statushash->{$chain}->{action
} = 'delete';
1794 $statushash->{$chain}->{sig
} = $sig;
1795 print "delete $chain ($sig)\n" if $verbose;
1805 get_ruleset_status
($ruleset, 1);
1808 sub print_sig_rule
{
1809 my ($chain, $sig) = @_;
1811 # We just use this to store a SHA1 checksum used to detect changes
1812 return "-A $chain -m comment --comment \"PVESIG:$sig\"\n";
1815 sub get_rulset_cmdlist {
1816 my ($ruleset, $verbose) = @_;
1818 my $cmdlist = "*filter
\n"; # we pass this to iptables-restore;
1820 my $statushash = get_ruleset_status($ruleset, $verbose);
1822 # create missing chains first
1823 foreach my $chain (sort keys %$ruleset) {
1824 my $stat = $statushash->{$chain};
1825 die "internal error
" if !$stat;
1826 next if $stat->{action} ne 'create';
1828 $cmdlist .= ":$chain - [0:0]\n";
1831 my $rule = "INPUT
-j PVEFW-INPUT
";
1832 if (!PVE::Firewall::iptables_rule_exist($rule)) {
1833 $cmdlist .= "-A
$rule\n";
1835 $rule = "OUTPUT
-j PVEFW-OUTPUT
";
1836 if (!PVE::Firewall::iptables_rule_exist($rule)) {
1837 $cmdlist .= "-A
$rule\n";
1840 $rule = "FORWARD
-j PVEFW-FORWARD
";
1841 if (!PVE::Firewall::iptables_rule_exist($rule)) {
1842 $cmdlist .= "-A
$rule\n";
1845 foreach my $chain (sort keys %$ruleset) {
1846 my $stat = $statushash->{$chain};
1847 die "internal error
" if !$stat;
1849 if ($stat->{action} eq 'update' || $stat->{action} eq 'create') {
1850 $cmdlist .= "-F
$chain\n";
1851 foreach my $cmd (@{$ruleset->{$chain}}) {
1852 $cmdlist .= "$cmd\n";
1854 $cmdlist .= print_sig_rule($chain, $stat->{sig});
1855 } elsif ($stat->{action} eq 'delete') {
1856 die "internal error
"; # this should not happen
1857 } elsif ($stat->{action} eq 'exists') {
1860 die "internal error
- unknown status
'$stat->{action}'";
1864 foreach my $chain (keys %$statushash) {
1865 next if $statushash->{$chain}->{action} ne 'delete';
1866 $cmdlist .= "-F
$chain\n";
1868 foreach my $chain (keys %$statushash) {
1869 next if $statushash->{$chain}->{action} ne 'delete';
1870 next if $chain eq 'PVEFW-INPUT';
1871 next if $chain eq 'PVEFW-OUTPUT';
1872 next if $chain eq 'PVEFW-FORWARD';
1873 $cmdlist .= "-X
$chain\n";
1876 $cmdlist .= "COMMIT
\n";
1882 my ($ruleset, $hostfw_conf, $verbose) = @_;
1884 enable_bridge_firewall();
1886 update_nf_conntrack_max($hostfw_conf);
1888 my $cmdlist = get_rulset_cmdlist($ruleset, $verbose);
1890 print $cmdlist if $verbose;
1892 iptables_restore_cmdlist($cmdlist);
1894 # test: re-read status and check if everything is up to date
1895 my $statushash = get_ruleset_status($ruleset);
1898 foreach my $chain (sort keys %$ruleset) {
1899 my $stat = $statushash->{$chain};
1900 if ($stat->{action} ne 'exists') {
1901 warn "unable to update chain
'$chain'\n";
1906 die "unable to apply firewall changes
\n" if $errors;
1909 sub update_nf_conntrack_max {
1910 my ($hostfw_conf) = @_;
1912 my $max = 65536; # reasonable default
1914 my $options = $hostfw_conf->{options} || {};
1916 if (defined($options->{nf_conntrack_max}) && ($options->{nf_conntrack_max} > $max)) {
1917 $max = $options->{nf_conntrack_max};
1918 $max = int(($max+ 8191)/8192)*8192; # round to multiples of 8192
1921 my $filename_nf_conntrack_max = "/proc/sys
/net/nf_conntrack_max
";
1922 my $filename_hashsize = "/sys/module
/nf_conntrack/parameters
/hashsize
";
1924 my $current = int(PVE::Tools::file_read_firstline($filename_nf_conntrack_max) || $max);
1926 if ($current != $max) {
1927 my $hashsize = int($max/4);
1928 PVE::ProcFSTools::write_proc_entry($filename_hashsize, $hashsize);
1929 PVE::ProcFSTools::write_proc_entry($filename_nf_conntrack_max, $max);
1934 my ($start, $verbose) = @_;
1937 my $status = read_pvefw_status();
1939 my ($ruleset, $hostfw_conf) = PVE::Firewall::compile();
1941 if ($start || $status eq 'active') {
1943 save_pvefw_status('active') if ($status ne 'active');
1945 apply_ruleset($ruleset, $hostfw_conf, $verbose);
1947 print "Firewall
not active
(status
= $status)\n" if $verbose;