13 use PVE
::Tools
qw(run_command lock_file);
17 my $pve_fw_lock_filename = "/var/lock/pvefw.lck";
18 my $pve_fw_status_filename = "/var/lib/pve-firewall/pvefw.status";
20 my $default_log_level = 'info';
22 my $log_level_hash = {
33 # imported/converted from: /usr/share/shorewall/macro.*
36 { action
=> 'PARAM', proto
=> 'udp', dport
=> '10080' },
37 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '10080' },
40 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '113' },
43 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '179' },
46 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6881:6889' },
47 { action
=> 'PARAM', proto
=> 'udp', dport
=> '6881' },
50 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6881:6999' },
51 { action
=> 'PARAM', proto
=> 'udp', dport
=> '6881' },
54 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '2401' },
57 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1494' },
58 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1604' },
59 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '2598' },
62 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3689' },
63 { action
=> 'PARAM', proto
=> 'udp', dport
=> '3689' },
66 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6277' },
69 { action
=> 'PARAM', proto
=> 'udp', dport
=> '67:68', sport
=> '67:68' },
70 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '67:68', sport
=> '67:68' },
73 { action
=> 'PARAM', proto
=> 'udp', dport
=> '53' },
74 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '53' },
77 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3632' },
80 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '4662' },
81 { action
=> 'PARAM', proto
=> 'udp', dport
=> '4665' },
84 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '21' },
87 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '79' },
90 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '2086' },
91 { action
=> 'PARAM', proto
=> 'udp', dport
=> '2086' },
92 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1080' },
93 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1080' },
96 { action
=> 'PARAM', proto
=> '47' },
97 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> '47' },
100 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '9418' },
103 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6346' },
104 { action
=> 'PARAM', proto
=> 'udp', dport
=> '6346' },
107 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '11371' },
110 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '80' },
113 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '443' },
116 { action
=> 'PARAM', proto
=> 'udp', dport
=> '3130' },
119 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5190' },
122 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '143' },
125 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '993' },
128 { action
=> 'PARAM', proto
=> '94' },
129 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> '94' },
132 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '631' },
135 { action
=> 'PARAM', proto
=> 'udp', dport
=> '631' },
138 { action
=> 'PARAM', source
=> 'SOURCE', dest
=> 'DEST', proto
=> 'tcp', dport
=> '631' },
139 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '631' },
142 { action
=> 'PARAM', proto
=> 'udp', dport
=> '500', sport
=> '500' },
143 { action
=> 'PARAM', proto
=> '50' },
144 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '500', sport
=> '500' },
145 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> '50' },
148 { action
=> 'PARAM', proto
=> 'udp', dport
=> '500', sport
=> '500' },
149 { action
=> 'PARAM', proto
=> '51' },
150 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '500', sport
=> '500' },
151 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> '51' },
154 { action
=> 'PARAM', proto
=> 'udp', dport
=> '500' },
155 { action
=> 'PARAM', proto
=> 'udp', dport
=> '4500' },
156 { action
=> 'PARAM', proto
=> '50' },
157 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '500' },
158 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '4500' },
159 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> '50' },
162 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6667' },
165 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5222' },
168 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5223' },
171 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5269' },
174 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '9100' },
177 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1701' },
178 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '1701' },
181 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '389' },
184 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '636' },
187 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1863' },
190 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1433' },
193 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '25' },
194 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '465' },
195 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '587' },
198 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '4949' },
201 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3306' },
204 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '119' },
207 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '563' },
210 { action
=> 'PARAM', proto
=> 'udp', dport
=> '123' },
213 { action
=> 'PARAM', proto
=> 'udp', dport
=> '123' },
214 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '123' },
217 { action
=> 'PARAM', proto
=> 'udp', dport
=> '123' },
218 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1024:65535', sport
=> '123' },
221 { action
=> 'PARAM', proto
=> '89' },
224 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1194' },
227 { action
=> 'PARAM', proto
=> 'udp', dport
=> '5632' },
228 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5631' },
231 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '110' },
234 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '995' },
237 { action
=> 'PARAM', proto
=> '47' },
238 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> '47' },
239 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1723' },
242 { action
=> 'PARAM', proto
=> 'icmp', dport
=> 'echo-request' },
245 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5432' },
248 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '515' },
251 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3389' },
254 { action
=> 'PARAM', proto
=> 'udp', dport
=> '520' },
255 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '520' },
258 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '953' },
261 { action
=> 'ACCEPT', proto
=> 'tcp', dport
=> '2703' },
264 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '37' },
267 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '873' },
270 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6566' },
273 { action
=> 'PARAM', proto
=> 'udp', dport
=> '135,445' },
274 { action
=> 'PARAM', proto
=> 'udp', dport
=> '137:139' },
275 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1024:65535', sport
=> '137' },
276 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '135,139,445' },
279 { action
=> 'PARAM', proto
=> 'udp', dport
=> '135,445' },
280 { action
=> 'PARAM', proto
=> 'udp', dport
=> '137:139' },
281 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1024:65535', sport
=> '137' },
282 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '135,139,445' },
283 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '135,445' },
284 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '137:139' },
285 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '1024:65535', sport
=> '137' },
286 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'tcp', dport
=> '135,139,445' },
289 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '901' },
292 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '25' },
295 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '465' },
298 { action
=> 'PARAM', proto
=> 'udp', dport
=> '161:162' },
299 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '161' },
302 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '783' },
305 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '22' },
308 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3690' },
311 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3874' },
312 { action
=> 'PARAM', proto
=> 'udp', dport
=> '3740' },
313 { action
=> 'PARAM', proto
=> '41' },
314 { action
=> 'PARAM', proto
=> 'udp', dport
=> '5072,8374' },
317 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3128' },
320 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '587' },
323 { action
=> 'PARAM', proto
=> 'udp', dport
=> '514' },
324 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '514' },
327 { action
=> 'PARAM', proto
=> 'udp', dport
=> '69' },
330 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '23' },
333 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '992' },
336 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '37' },
339 { action
=> 'PARAM', proto
=> 'udp', dport
=> '33434:33524' },
340 { action
=> 'PARAM', proto
=> 'icmp', dport
=> 'echo-request' },
343 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5900:5909' },
346 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5500' },
349 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '80' },
350 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '443' },
353 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '8080' },
356 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '10000' },
359 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '43' },
363 my $pve_fw_parsed_macros;
364 my $pve_fw_preferred_macro_names = {};
366 my $pve_std_chains = {
367 'PVEFW-SET-ACCEPT-MARK' => [
368 "-j MARK --set-mark 1",
370 'PVEFW-DropBroadcast' => [
371 # same as shorewall 'Broadcast'
372 # simply DROP BROADCAST/MULTICAST/ANYCAST
373 # we can use this to reduce logging
374 { action
=> 'DROP', dsttype
=> 'BROADCAST' },
375 { action
=> 'DROP', dsttype
=> 'MULTICAST' },
376 { action
=> 'DROP', dsttype
=> 'ANYCAST' },
377 { action
=> 'DROP', dest
=> '224.0.0.0/4' },
380 # same as shorewall 'reject'
381 { action
=> 'DROP', dsttype
=> 'BROADCAST' },
382 { action
=> 'DROP', source
=> '224.0.0.0/4' },
383 { action
=> 'DROP', proto
=> 'icmp' },
384 "-p tcp -j REJECT --reject-with tcp-reset",
385 "-p udp -j REJECT --reject-with icmp-port-unreachable",
386 "-p icmp -j REJECT --reject-with icmp-host-unreachable",
387 "-j REJECT --reject-with icmp-host-prohibited",
390 # same as shorewall 'Drop', which is equal to DROP,
391 # but REJECT/DROP some packages to reduce logging,
392 # and ACCEPT critical ICMP types
393 { action
=> 'PVEFW-reject', proto
=> 'tcp', dport
=> '43' }, # REJECT 'auth'
394 # we are not interested in BROADCAST/MULTICAST/ANYCAST
395 { action
=> 'PVEFW-DropBroadcast' },
396 # ACCEPT critical ICMP types
397 { action
=> 'ACCEPT', proto
=> 'icmp', dport
=> 'fragmentation-needed' },
398 { action
=> 'ACCEPT', proto
=> 'icmp', dport
=> 'time-exceeded' },
399 # Drop packets with INVALID state
400 "-m conntrack --ctstate INVALID -j DROP",
401 # Drop Microsoft SMB noise
402 { action
=> 'DROP', proto
=> 'udp', dport
=> '135,445', nbdport
=> 2 },
403 { action
=> 'DROP', proto
=> 'udp', dport
=> '137:139'},
404 { action
=> 'DROP', proto
=> 'udp', dport
=> '1024:65535', sport
=> 137 },
405 { action
=> 'DROP', proto
=> 'tcp', dport
=> '135,139,445', nbdport
=> 3 },
406 { action
=> 'DROP', proto
=> 'udp', dport
=> 1900 }, # UPnP
407 # Drop new/NotSyn traffic so that it doesn't get logged
408 "-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -j DROP",
410 { action
=> 'DROP', proto
=> 'udp', sport
=> 53 },
413 # same as shorewall 'Reject', which is equal to Reject,
414 # but REJECT/DROP some packages to reduce logging,
415 # and ACCEPT critical ICMP types
416 { action
=> 'PVEFW-reject', proto
=> 'tcp', dport
=> '43' }, # REJECT 'auth'
417 # we are not interested in BROADCAST/MULTICAST/ANYCAST
418 { action
=> 'PVEFW-DropBroadcast' },
419 # ACCEPT critical ICMP types
420 { action
=> 'ACCEPT', proto
=> 'icmp', dport
=> 'fragmentation-needed' },
421 { action
=> 'ACCEPT', proto
=> 'icmp', dport
=> 'time-exceeded' },
422 # Drop packets with INVALID state
423 "-m conntrack --ctstate INVALID -j DROP",
424 # Drop Microsoft SMB noise
425 { action
=> 'PVEFW-reject', proto
=> 'udp', dport
=> '135,445', nbdport
=> 2 },
426 { action
=> 'PVEFW-reject', proto
=> 'udp', dport
=> '137:139'},
427 { action
=> 'PVEFW-reject', proto
=> 'udp', dport
=> '1024:65535', sport
=> 137 },
428 { action
=> 'PVEFW-reject', proto
=> 'tcp', dport
=> '135,139,445', nbdport
=> 3 },
429 { action
=> 'DROP', proto
=> 'udp', dport
=> 1900 }, # UPnP
430 # Drop new/NotSyn traffic so that it doesn't get logged
431 "-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -j DROP",
433 { action
=> 'DROP', proto
=> 'udp', sport
=> 53 },
435 'PVEFW-tcpflags' => [
436 # same as shorewall tcpflags action.
437 # Packets arriving on this interface are checked for som illegal combinations of TCP flags
438 "-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG FIN,PSH,URG -g PVEFW-logflags",
439 "-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG NONE -g PVEFW-logflags",
440 "-p tcp -m tcp --tcp-flags SYN,RST SYN,RST -g PVEFW-logflags",
441 "-p tcp -m tcp --tcp-flags FIN,SYN FIN,SYN -g PVEFW-logflags",
442 "-p tcp -m tcp --sport 0 --tcp-flags FIN,SYN,RST,ACK SYN -g PVEFW-logflags",
445 # same as shorewall smurfs action
446 # Filter packets for smurfs (packets with a broadcast address as the source).
447 "-s 0.0.0.0/32 -j RETURN",
448 "-m addrtype --src-type BROADCAST -g PVEFW-smurflog",
449 "-s 224.0.0.0/4 -g PVEFW-smurflog",
453 # iptables -p icmp -h
454 my $icmp_type_names = {
457 'destination-unreachable' => 1,
458 'network-unreachable' => 1,
459 'host-unreachable' => 1,
460 'protocol-unreachable' => 1,
461 'port-unreachable' => 1,
462 'fragmentation-needed' => 1,
463 'source-route-failed' => 1,
464 'network-unknown' => 1,
466 'network-prohibited' => 1,
467 'host-prohibited' => 1,
468 'TOS-network-unreachable' => 1,
469 'TOS-host-unreachable' => 1,
470 'communication-prohibited' => 1,
471 'host-precedence-violation' => 1,
472 'precedence-cutoff' => 1,
473 'source-quench' => 1,
475 'network-redirect' => 1,
476 'host-redirect' => 1,
477 'TOS-network-redirect' => 1,
478 'TOS-host-redirect' => 1,
480 'router-advertisement' => 1,
481 'router-solicitation' => 1,
482 'time-exceeded' => 1,
483 'ttl-zero-during-transit' => 1,
484 'ttl-zero-during-reassembly' => 1,
485 'parameter-problem' => 1,
486 'ip-header-bad' => 1,
487 'required-option-missing' => 1,
488 'timestamp-request' => 1,
489 'timestamp-reply' => 1,
490 'address-mask-request' => 1,
491 'address-mask-reply' => 1,
494 sub get_firewall_macros
{
496 return $pve_fw_parsed_macros if $pve_fw_parsed_macros;
498 $pve_fw_parsed_macros = {};
500 foreach my $k (keys %$pve_fw_macros) {
503 my $macro = $pve_fw_macros->{$k};
504 $pve_fw_preferred_macro_names->{$name} = $k;
505 $pve_fw_parsed_macros->{$name} = $macro;
508 return $pve_fw_parsed_macros;
513 sub get_etc_services
{
515 return $etc_services if $etc_services;
517 my $filename = "/etc/services";
519 my $fh = IO
::File-
>new($filename, O_RDONLY
);
521 warn "unable to read '$filename' - $!\n";
527 while (my $line = <$fh>) {
529 next if $line =~m/^#/;
530 next if ($line =~m/^\s*$/);
532 if ($line =~ m!^(\S+)\s+(\S+)/(tcp|udp).*$!) {
533 $services->{byid
}->{$2}->{name
} = $1;
534 $services->{byid
}->{$2}->{port
} = $2;
535 $services->{byid
}->{$2}->{$3} = 1;
536 $services->{byname
}->{$1} = $services->{byid
}->{$2};
542 $etc_services = $services;
545 return $etc_services;
550 sub get_etc_protocols
{
551 return $etc_protocols if $etc_protocols;
553 my $filename = "/etc/protocols";
555 my $fh = IO
::File-
>new($filename, O_RDONLY
);
557 warn "unable to read '$filename' - $!\n";
563 while (my $line = <$fh>) {
565 next if $line =~m/^#/;
566 next if ($line =~m/^\s*$/);
568 if ($line =~ m!^(\S+)\s+(\d+)\s+.*$!) {
569 $protocols->{byid
}->{$2}->{name
} = $1;
570 $protocols->{byname
}->{$1} = $protocols->{byid
}->{$2};
576 $etc_protocols = $protocols;
578 return $etc_protocols;
581 sub parse_address_list
{
585 foreach my $aor (split(/,/, $str)) {
586 if (!Net
::IP-
>new($aor)) {
587 my $err = Net
::IP
::Error
();
588 die "invalid IP address: $err\n";
596 sub parse_port_name_number_or_range
{
599 my $services = PVE
::Firewall
::get_etc_services
();
601 foreach my $item (split(/,/, $str)) {
605 foreach my $pon (split(':', $item, 2)) {
606 $pon = $services->{byname
}->{$pon}->{port
} if $services->{byname
}->{$pon}->{port
};
607 if ($pon =~ m/^\d+$/){
608 die "invalid port '$pon'\n" if $pon < 0 && $pon > 65535;
609 die "port '$pon' must be bigger than port '$oldpon' \n" if $oldpon && ($pon < $oldpon);
612 die "invalid port $services->{byname}->{$pon}\n" if !$services->{byname
}->{$pon};
620 my $bridge_firewall_enabled = 0;
622 sub enable_bridge_firewall
{
624 return if $bridge_firewall_enabled; # only once
626 system("echo 1 > /proc/sys/net/bridge/bridge-nf-call-iptables");
627 system("echo 1 > /proc/sys/net/bridge/bridge-nf-call-ip6tables");
629 $bridge_firewall_enabled = 1;
632 my $rule_format = "%-15s %-30s %-30s %-15s %-15s %-15s\n";
637 run_command
("/sbin/iptables $cmd", outfunc
=> sub {}, errfunc
=> sub {});
640 sub iptables_restore_cmdlist
{
643 run_command
("/sbin/iptables-restore -n", input
=> $cmdlist);
646 sub iptables_get_chains
{
650 # check what chains we want to track
651 my $is_pvefw_chain = sub {
654 return 1 if $name =~ m/^PVEFW-\S+$/;
656 return 1 if $name =~ m/^tap\d+i\d+-(:?IN|OUT)$/;
657 return 1 if $name =~ m/^vmbr\d+-(:?FW|IN|OUT)$/;
658 return 1 if $name =~ m/^GROUP-(:?[^\s\-]+)-(:?IN|OUT)$/;
668 return if $line =~ m/^#/;
669 return if $line =~ m/^\s*$/;
671 if ($line =~ m/^\*(\S+)$/) {
676 return if $table ne 'filter';
678 if ($line =~ m/^:(\S+)\s/) {
680 return if !&$is_pvefw_chain($chain);
681 $res->{$chain} = "unknown";
682 } elsif ($line =~ m/^-A\s+(\S+)\s.*--comment\s+\"PVESIG:(\S+)\"/) {
683 my ($chain, $sig) = ($1, $2);
684 return if !&$is_pvefw_chain($chain);
685 $res->{$chain} = $sig;
687 # simply ignore the rest
692 run_command
("/sbin/iptables-save", outfunc
=> $parser);
697 sub iptables_chain_exist
{
701 iptables
("-n --list $chain");
708 sub iptables_rule_exist
{
712 iptables
("-C $rule");
719 sub ruleset_generate_rule
{
720 my ($ruleset, $chain, $rule, $actions, $goto) = @_;
722 return if $rule->{disable
};
726 $cmd .= " -m iprange --src-range" if $rule->{nbsource
} && $rule->{nbsource
} > 1;
727 $cmd .= " -s $rule->{source}" if $rule->{source
};
728 $cmd .= " -m iprange --dst-range" if $rule->{nbdest
} && $rule->{nbdest
} > 1;
729 $cmd .= " -d $rule->{dest}" if $rule->{dest
};
731 if ($rule->{proto
}) {
732 $cmd .= " -p $rule->{proto}";
735 $multiport++ if $rule->{nbdport
} && ($rule->{nbdport
} > 1);
736 $multiport++ if $rule->{nbsport
} && ($rule->{nbsport
} > 1);
738 $cmd .= " --match multiport" if $multiport;
740 die "multiport: option '--sports' cannot be used together with '--dports'\n"
741 if ($multiport == 2) && ($rule->{dport
} ne $rule->{sport
});
743 if ($rule->{dport
}) {
744 if ($rule->{proto
} && $rule->{proto
} eq 'icmp') {
745 # Note: we use dport to store --icmp-type
746 die "unknown icmp-type '$rule->{dport}'\n" if !defined($icmp_type_names->{$rule->{dport
}});
747 $cmd .= " -m icmp --icmp-type $rule->{dport}";
749 if ($rule->{nbdport
} && $rule->{nbdport
} > 1) {
750 if ($multiport == 2) {
751 $cmd .= " --ports $rule->{dport}";
753 $cmd .= " --dports $rule->{dport}";
756 $cmd .= " --dport $rule->{dport}";
761 if ($rule->{sport
}) {
762 if ($rule->{nbsport
} && $rule->{nbsport
} > 1) {
763 $cmd .= " --sports $rule->{sport}" if $multiport != 2;
765 $cmd .= " --sport $rule->{sport}";
768 } elsif ($rule->{dport
} || $rule->{sport
}) {
769 warn "ignoring destination port '$rule->{dport}' - no protocol specified\n" if $rule->{dport
};
770 warn "ignoring source port '$rule->{sport}' - no protocol specified\n" if $rule->{sport
};
773 $cmd .= " -m addrtype --dst-type $rule->{dsttype}" if $rule->{dsttype
};
775 if (my $action = $rule->{action
}) {
776 $action = $actions->{$action} if defined($actions->{$action});
777 $goto = 1 if !defined($goto) && $action eq 'PVEFW-SET-ACCEPT-MARK';
778 $cmd .= $goto ?
" -g $action" : " -j $action";
781 ruleset_addrule
($ruleset, $chain, $cmd) if $cmd;
784 sub ruleset_create_chain
{
785 my ($ruleset, $chain) = @_;
787 die "Invalid chain name '$chain' (28 char max)\n" if length($chain) > 28;
789 die "chain '$chain' already exists\n" if $ruleset->{$chain};
791 $ruleset->{$chain} = [];
794 sub ruleset_chain_exist
{
795 my ($ruleset, $chain) = @_;
797 return $ruleset->{$chain} ?
1 : undef;
800 sub ruleset_addrule
{
801 my ($ruleset, $chain, $rule) = @_;
803 die "no such chain '$chain'\n" if !$ruleset->{$chain};
805 push @{$ruleset->{$chain}}, "-A $chain $rule";
808 sub ruleset_insertrule
{
809 my ($ruleset, $chain, $rule) = @_;
811 die "no such chain '$chain'\n" if !$ruleset->{$chain};
813 unshift @{$ruleset->{$chain}}, "-A $chain $rule";
816 sub generate_bridge_chains
{
817 my ($ruleset, $bridge) = @_;
819 if (!ruleset_chain_exist
($ruleset, "PVEFW-FORWARD")){
820 ruleset_create_chain
($ruleset, "PVEFW-FORWARD");
821 ruleset_addrule
($ruleset, "PVEFW-FORWARD", "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT");
824 if (!ruleset_chain_exist
($ruleset, "$bridge-FW")) {
825 ruleset_create_chain
($ruleset, "$bridge-FW");
826 ruleset_addrule
($ruleset, "PVEFW-FORWARD", "-o $bridge -m physdev --physdev-is-bridged -j $bridge-FW");
827 ruleset_addrule
($ruleset, "PVEFW-FORWARD", "-i $bridge -m physdev --physdev-is-bridged -j $bridge-FW");
828 ruleset_addrule
($ruleset, "PVEFW-FORWARD", "-o $bridge -j DROP"); # disable interbridge routing
829 ruleset_addrule
($ruleset, "PVEFW-FORWARD", "-i $bridge -j DROP"); # disable interbridge routing
832 if (!ruleset_chain_exist
($ruleset, "$bridge-OUT")) {
833 ruleset_create_chain
($ruleset, "$bridge-OUT");
834 ruleset_addrule
($ruleset, "$bridge-FW", "-m physdev --physdev-is-bridged --physdev-is-in -j $bridge-OUT");
837 if (!ruleset_chain_exist
($ruleset, "$bridge-IN")) {
838 ruleset_create_chain
($ruleset, "$bridge-IN");
839 ruleset_addrule
($ruleset, "$bridge-FW", "-m physdev --physdev-is-bridged --physdev-is-out -j $bridge-IN");
840 ruleset_addrule
($ruleset, "$bridge-FW", "-m mark --mark 1 -j ACCEPT");
841 # accept traffic to unmanaged bridge ports
842 ruleset_addrule
($ruleset, "$bridge-FW", "-m physdev --physdev-is-bridged --physdev-is-out -j ACCEPT ");
846 sub generate_tap_rules_direction
{
847 my ($ruleset, $groups_conf, $iface, $netid, $macaddr, $vmfw_conf, $bridge, $direction) = @_;
849 my $lc_direction = lc($direction);
851 my $rules = $vmfw_conf->{rules
};
853 my $options = $vmfw_conf->{options
};
854 my $loglevel = get_option_log_level
($options, "log_level_${lc_direction}");
856 my $tapchain = "$iface-$direction";
858 ruleset_create_chain
($ruleset, $tapchain);
860 if (!(defined($options->{nosmurfs
}) && $options->{nosmurfs
} == 0)) {
861 ruleset_addrule
($ruleset, $tapchain, "-m conntrack --ctstate INVALID,NEW -j PVEFW-smurfs");
864 if (!(defined($options->{dhcp
}) && $options->{dhcp
} == 0)) {
865 ruleset_addrule
($ruleset, $tapchain, "-p udp -m udp --dport 67:68 -j ACCEPT");
868 if ($options->{tcpflags
}) {
869 ruleset_addrule
($ruleset, $tapchain, "-p tcp -j PVEFW-tcpflags");
872 ruleset_addrule
($ruleset, $tapchain, "-m conntrack --ctstate INVALID -j DROP");
873 ruleset_addrule
($ruleset, $tapchain, "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT");
875 if ($direction eq 'OUT' && defined($macaddr) &&
876 !(defined($options->{macfilter
}) && $options->{macfilter
} == 0)) {
877 ruleset_addrule
($ruleset, $tapchain, "-m mac ! --mac-source $macaddr -j DROP");
880 foreach my $rule (@$rules) {
881 next if $rule->{iface
} && $rule->{iface
} ne $netid;
882 next if $rule->{disable
};
883 if ($rule->{type
} eq 'group') {
884 my $group_chain = "GROUP-$rule->{action}-$direction";
885 if(!ruleset_chain_exist
($ruleset, $group_chain)){
886 generate_group_rules
($ruleset, $groups_conf, $rule->{action
});
888 ruleset_addrule
($ruleset, $tapchain, "-j $group_chain");
889 ruleset_addrule
($ruleset, $tapchain, "-m mark --mark 1 -j RETURN")
890 if $direction eq 'OUT';
892 next if $rule->{type
} ne $lc_direction;
893 if ($direction eq 'OUT') {
894 ruleset_generate_rule
($ruleset, $tapchain, $rule,
895 { ACCEPT
=> "PVEFW-SET-ACCEPT-MARK", REJECT
=> "PVEFW-reject" });
897 ruleset_generate_rule
($ruleset, $tapchain, $rule, { REJECT
=> "PVEFW-reject" });
905 if ($direction eq 'OUT') {
906 $policy = $options->{'policy-out'} || 'ACCEPT'; # allow everything by default
908 $policy = $options->{'policy-in'} || 'DROP'; # allow everything by default
911 if ($policy eq 'ACCEPT') {
912 if ($direction eq 'OUT') {
913 ruleset_addrule
($ruleset, $tapchain, "-g PVEFW-SET-ACCEPT-MARK");
915 ruleset_addrule
($ruleset, $tapchain, "-j ACCEPT");
917 } elsif ($policy eq 'DROP') {
919 ruleset_addrule
($ruleset, $tapchain, "-j PVEFW-Drop");
921 ruleset_addrule
($ruleset, $tapchain, "-j LOG --log-prefix \"$tapchain-dropped: \" --log-level $loglevel")
922 if defined($loglevel);
924 ruleset_addrule
($ruleset, $tapchain, "-j DROP");
925 } elsif ($policy eq 'REJECT') {
926 ruleset_addrule
($ruleset, $tapchain, "-j PVEFW-Reject");
928 ruleset_addrule
($ruleset, $tapchain, "-j LOG --log-prefix \"$tapchain-reject: \" --log-level $loglevel")
929 if defined($loglevel);
931 ruleset_addrule
($ruleset, $tapchain, "-g PVEFW-reject");
934 die "internal error: unknown policy '$policy'";
937 # plug the tap chain to bridge chain
938 my $physdevdirection = $direction eq 'IN' ?
"out" : "in";
939 my $rule = "-m physdev --physdev-$physdevdirection $iface --physdev-is-bridged -j $tapchain";
940 ruleset_insertrule
($ruleset, "$bridge-$direction", $rule);
942 if ($direction eq 'OUT'){
943 # add tap->host rules
944 my $rule = "-m physdev --physdev-$physdevdirection $iface -j $tapchain";
945 ruleset_addrule
($ruleset, "PVEFW-INPUT", $rule);
950 my ($ruleset, $hostfw_conf, $groups_conf) = @_;
952 # fixme: allow security groups
954 my $options = $hostfw_conf->{options
};
955 my $rules = $hostfw_conf->{rules
};
957 # host inbound firewall
958 my $chain = "PVEFW-HOST-IN";
959 ruleset_create_chain
($ruleset, $chain);
961 my $loglevel = get_option_log_level
($options, "log_level_in");
963 ruleset_addrule
($ruleset, $chain, "-m conntrack --ctstate INVALID -j DROP");
964 ruleset_addrule
($ruleset, $chain, "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT");
965 ruleset_addrule
($ruleset, $chain, "-i lo -j ACCEPT");
966 ruleset_addrule
($ruleset, $chain, "-m addrtype --dst-type MULTICAST -j ACCEPT");
967 ruleset_addrule
($ruleset, $chain, "-p udp -m conntrack --ctstate NEW -m multiport --dports 5404,5405 -j ACCEPT");
968 ruleset_addrule
($ruleset, $chain, "-p udp -m udp --dport 9000 -j ACCEPT"); #corosync
970 foreach my $rule (@$rules) {
971 next if $rule->{type
} ne 'in';
972 # we use RETURN because we need to check also tap rules
973 ruleset_generate_rule
($ruleset, $chain, $rule, { ACCEPT
=> 'RETURN', REJECT
=> "PVEFW-reject" });
976 ruleset_addrule
($ruleset, $chain, "-j LOG --log-prefix \"kvmhost-IN dropped: \" --log-level $loglevel")
977 if defined($loglevel);
979 ruleset_addrule
($ruleset, $chain, "-j DROP");
981 # host outbound firewall
982 $chain = "PVEFW-HOST-OUT";
983 ruleset_create_chain
($ruleset, $chain);
985 $loglevel = get_option_log_level
($options, "log_level_out");
987 ruleset_addrule
($ruleset, $chain, "-m conntrack --ctstate INVALID -j DROP");
988 ruleset_addrule
($ruleset, $chain, "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT");
989 ruleset_addrule
($ruleset, $chain, "-o lo -j ACCEPT");
990 ruleset_addrule
($ruleset, $chain, "-m addrtype --dst-type MULTICAST -j ACCEPT");
991 ruleset_addrule
($ruleset, $chain, "-p udp -m conntrack --ctstate NEW -m multiport --dports 5404,5405 -j ACCEPT");
992 ruleset_addrule
($ruleset, $chain, "-p udp -m udp --dport 9000 -j ACCEPT"); #corosync
994 foreach my $rule (@$rules) {
995 next if $rule->{type
} ne 'out';
996 # we use RETURN because we need to check also tap rules
997 ruleset_generate_rule
($ruleset, $chain, $rule, { ACCEPT
=> 'RETURN', REJECT
=> "PVEFW-reject" });
1000 ruleset_addrule
($ruleset, $chain, "-j LOG --log-prefix \"kvmhost-OUT dropped: \" --log-level $loglevel")
1001 if defined($loglevel);
1003 ruleset_addrule
($ruleset, $chain, "-j DROP");
1005 ruleset_addrule
($ruleset, "PVEFW-OUTPUT", "-j PVEFW-HOST-OUT");
1006 ruleset_addrule
($ruleset, "PVEFW-INPUT", "-j PVEFW-HOST-IN");
1009 sub generate_group_rules
{
1010 my ($ruleset, $groups_conf, $group) = @_;
1012 die "no such security group '$group'\n" if !$groups_conf->{$group};
1014 my $rules = $groups_conf->{$group}->{rules
};
1016 my $chain = "GROUP-${group}-IN";
1018 ruleset_create_chain
($ruleset, $chain);
1020 foreach my $rule (@$rules) {
1021 next if $rule->{type
} ne 'in';
1022 ruleset_generate_rule
($ruleset, $chain, $rule, { REJECT
=> "PVEFW-reject" });
1025 $chain = "GROUP-${group}-OUT";
1027 ruleset_create_chain
($ruleset, $chain);
1028 ruleset_addrule
($ruleset, $chain, "-j MARK --set-mark 0"); # clear mark
1030 foreach my $rule (@$rules) {
1031 next if $rule->{type
} ne 'out';
1032 # we use PVEFW-SET-ACCEPT-MARK (Instead of ACCEPT) because we need to
1033 # check also other tap rules later
1034 ruleset_generate_rule
($ruleset, $chain, $rule,
1035 { ACCEPT
=> 'PVEFW-SET-ACCEPT-MARK', REJECT
=> "PVEFW-reject" });
1040 my $valid_netdev_names = {};
1041 for (my $i = 0; $i < $MAX_NETS; $i++) {
1042 $valid_netdev_names->{"net$i"} = 1;
1046 my ($line, $need_iface, $allow_groups) = @_;
1048 my $macros = get_firewall_macros
();
1049 my $protocols = get_etc_protocols
();
1051 my ($type, $action, $iface, $source, $dest, $proto, $dport, $sport);
1053 # we can add single line comments to the end of the rule
1054 my $comment = $1 if $line =~ s/#\s*(.*?)\s*$//;
1056 # we can disable a rule when prefixed with '|'
1057 my $disable = 1 if $line =~ s/^\|//;
1059 my @data = split(/\s+/, $line);
1060 my $expected_elements = $need_iface ?
8 : 7;
1062 die "wrong number of rule elements\n" if scalar(@data) > $expected_elements;
1065 ($type, $action, $iface, $source, $dest, $proto, $dport, $sport) = @data
1067 ($type, $action, $source, $dest, $proto, $dport, $sport) = @data;
1070 die "incomplete rule\n" if ! ($type && $action);
1077 if ($type eq 'in' || $type eq 'out') {
1078 if ($action =~ m/^(ACCEPT|DROP|REJECT)$/) {
1080 } elsif ($action =~ m/^(\S+)\((ACCEPT|DROP|REJECT)\)$/) {
1081 ($macro_name, $action) = ($1, $2);
1082 my $lc_macro_name = lc($macro_name);
1083 my $preferred_name = $pve_fw_preferred_macro_names->{$lc_macro_name};
1084 $macro_name = $preferred_name if $preferred_name;
1085 $macro = $macros->{$lc_macro_name};
1086 die "unknown macro '$macro_name'\n" if !$macro;
1088 die "unknown action '$action'\n";
1090 } elsif ($type eq 'group') {
1091 die "wrong number of rule elements\n" if scalar(@data) != 3;
1092 die "groups disabled\n" if !$allow_groups;
1094 die "invalid characters in group name\n" if $action !~ m/^[A-Za-z0-9_\-]+$/;
1096 die "unknown rule type '$type'\n";
1100 $iface = undef if $iface && $iface eq '-';
1101 die "unknown interface '$iface'\n"
1102 if defined($iface) && !$valid_netdev_names->{$iface};
1105 $proto = undef if $proto && $proto eq '-';
1106 die "unknown protokol '$proto'\n" if $proto &&
1107 !(defined($protocols->{byname
}->{$proto}) ||
1108 defined($protocols->{byid
}->{$proto}));
1110 $source = undef if $source && $source eq '-';
1111 $dest = undef if $dest && $dest eq '-';
1113 $dport = undef if $dport && $dport eq '-';
1114 $sport = undef if $sport && $sport eq '-';
1116 my $nbsource = undef;
1119 $nbsource = parse_address_list
($source) if $source;
1120 $nbdest = parse_address_list
($dest) if $dest;
1126 disable
=> $disable,
1127 comment
=> $comment,
1132 nbsource
=> $nbsource,
1140 foreach my $templ (@$macro) {
1142 my $param_used = {};
1143 foreach my $k (keys %$templ) {
1144 my $v = $templ->{$k};
1145 if ($v eq 'PARAM') {
1147 $param_used->{$k} = 1;
1148 } elsif ($v eq 'DEST') {
1149 $v = $param->{dest
};
1150 $param_used->{dest
} = 1;
1151 } elsif ($v eq 'SOURCE') {
1152 $v = $param->{source
};
1153 $param_used->{source
} = 1;
1156 die "missing parameter '$k' in macro '$macro_name'\n" if !defined($v);
1159 foreach my $k (keys %$param) {
1160 next if !defined($param->{$k});
1161 next if $param_used->{$k};
1162 if (defined($rule->{$k})) {
1163 die "parameter '$k' already define in macro (value = '$rule->{$k}')\n"
1164 if $rule->{$k} ne $param->{$k};
1166 $rule->{$k} = $param->{$k};
1169 push @$rules, $rule;
1172 push @$rules, $param;
1175 foreach my $rule (@$rules) {
1176 $rule->{nbdport
} = parse_port_name_number_or_range
($rule->{dport
})
1177 if defined($rule->{dport
});
1178 $rule->{nbsport
} = parse_port_name_number_or_range
($rule->{sport
})
1179 if defined($rule->{sport
});
1185 sub parse_vmfw_option
{
1190 my $loglevels = "emerg|alert|crit|err|warning|notice|info|debug|nolog";
1192 if ($line =~ m/^(enable|dhcp|macfilter|nosmurfs|tcpflags):\s*(0|1)\s*$/i) {
1195 } elsif ($line =~ m/^(log_level_in|log_level_out):\s*(($loglevels)\s*)?$/i) {
1197 $value = $2 ?
lc($3) : '';
1198 } elsif ($line =~ m/^(policy-(in|out)):\s*(ACCEPT|DROP|REJECT)\s*$/i) {
1203 die "can't parse option '$line'\n"
1206 return ($opt, $value);
1209 sub parse_hostfw_option
{
1214 my $loglevels = "emerg|alert|crit|err|warning|notice|info|debug|nolog";
1216 if ($line =~ m/^(enable|dhcp|nosmurfs|tcpflags):\s*(0|1)\s*$/i) {
1219 } elsif ($line =~ m/^(log_level_in|log_level_out|tcp_flags_log_level|smurf_log_level):\s*(($loglevels)\s*)?$/i) {
1221 $value = $2 ?
lc($3) : '';
1222 } elsif ($line =~ m/^(policy-(in|out)):\s*(ACCEPT|DROP|REJECT)\s*$/i) {
1227 die "can't parse option '$line'\n"
1230 return ($opt, $value);
1233 sub parse_vm_fw_rules
{
1234 my ($filename, $fh) = @_;
1236 my $res = { rules
=> [], options
=> {}};
1240 while (defined(my $line = <$fh>)) {
1241 next if $line =~ m/^#/;
1242 next if $line =~ m/^\s*$/;
1244 my $linenr = $fh->input_line_number();
1245 my $prefix = "$filename (line $linenr)";
1247 if ($line =~ m/^\[(\S+)\]\s*$/i) {
1249 warn "$prefix: ignore unknown section '$section'\n" if !$res->{$section};
1253 warn "$prefix: skip line - no section";
1257 next if !$res->{$section}; # skip undefined section
1259 if ($section eq 'options') {
1261 my ($opt, $value) = parse_vmfw_option
($line);
1262 $res->{options
}->{$opt} = $value;
1264 warn "$prefix: $@" if $@;
1269 eval { $rules = parse_fw_rule
($line, 1, 1); };
1271 warn "$prefix: $err";
1275 push @{$res->{$section}}, @$rules;
1281 sub parse_host_fw_rules
{
1282 my ($filename, $fh) = @_;
1284 my $res = { rules
=> [], options
=> {}};
1288 while (defined(my $line = <$fh>)) {
1289 next if $line =~ m/^#/;
1290 next if $line =~ m/^\s*$/;
1292 my $linenr = $fh->input_line_number();
1293 my $prefix = "$filename (line $linenr)";
1295 if ($line =~ m/^\[(\S+)\]\s*$/i) {
1297 warn "$prefix: ignore unknown section '$section'\n" if !$res->{$section};
1301 warn "$prefix: skip line - no section";
1305 next if !$res->{$section}; # skip undefined section
1307 if ($section eq 'options') {
1309 my ($opt, $value) = parse_hostfw_option
($line);
1310 $res->{options
}->{$opt} = $value;
1312 warn "$prefix: $@" if $@;
1317 eval { $rules = parse_fw_rule
($line, 1, 1); };
1319 warn "$prefix: $err";
1323 push @{$res->{$section}}, @$rules;
1329 sub parse_group_fw_rules
{
1330 my ($filename, $fh) = @_;
1335 my $res = { rules
=> [] };
1337 while (defined(my $line = <$fh>)) {
1338 next if $line =~ m/^#/;
1339 next if $line =~ m/^\s*$/;
1341 my $linenr = $fh->input_line_number();
1342 my $prefix = "$filename (line $linenr)";
1344 if ($line =~ m/^\[group\s+(\S+)\]\s*$/i) {
1349 if (!$section || !$group) {
1350 warn "$prefix: skip line - no section";
1355 eval { $rules = parse_fw_rule
($line, 0, 0); };
1357 warn "$prefix: $err";
1361 push @{$res->{$group}->{$section}}, @$rules;
1368 my ($code, @param) = @_;
1372 my $res = lock_file
($pve_fw_lock_filename, $timeout, $code, @param);
1379 sub read_local_vm_config
{
1385 my $list = PVE
::QemuServer
::config_list
();
1387 foreach my $vmid (keys %$list) {
1388 my $cfspath = PVE
::QemuServer
::cfs_config_path
($vmid);
1389 if (my $conf = PVE
::Cluster
::cfs_read_file
($cfspath)) {
1390 $qemu->{$vmid} = $conf;
1394 my $vmdata = { openvz
=> $openvz, qemu
=> $qemu };
1399 sub read_vm_firewall_rules
{
1402 foreach my $vmid (keys %{$vmdata->{qemu
}}, keys %{$vmdata->{openvz
}}) {
1403 my $filename = "/etc/pve/firewall/$vmid.fw";
1404 my $fh = IO
::File-
>new($filename, O_RDONLY
);
1407 $rules->{$vmid} = parse_vm_fw_rules
($filename, $fh);
1413 sub get_option_log_level
{
1414 my ($options, $k) = @_;
1416 my $v = $options->{$k};
1417 $v = $default_log_level if !defined($v);
1419 return undef if $v eq '' || $v eq 'nolog';
1421 $v = $log_level_hash->{$v} if defined($log_level_hash->{$v});
1423 return $v if ($v >= 0) && ($v <= 7);
1425 warn "unknown log level ($k = '$v')\n";
1430 sub generate_std_chains
{
1431 my ($ruleset, $options) = @_;
1433 my $loglevel = get_option_log_level
($options, 'smurf_log_level');
1435 # same as shorewall smurflog.
1436 if (defined($loglevel)) {
1437 $pve_std_chains-> {'PVEFW-smurflog'} = [
1438 "-j LOG --log-prefix \"smurfs-dropped\" --log-level $loglevel",
1442 $pve_std_chains-> {'PVEFW-smurflog'} = [ "-j DROP" ];
1445 # same as shorewall logflags action.
1446 $loglevel = get_option_log_level
($options, 'tcp_flags_log_level');
1447 if (defined($loglevel)) {
1448 $pve_std_chains-> {'PVEFW-logflags'} = [
1449 "-j LOG --log-prefix \"logflags-dropped:\" --log-level $loglevel --log-ip-options",
1453 $pve_std_chains-> {'PVEFW-logflags'} = [ "-j DROP" ];
1456 foreach my $chain (keys %$pve_std_chains) {
1457 ruleset_create_chain
($ruleset, $chain);
1458 foreach my $rule (@{$pve_std_chains->{$chain}}) {
1460 ruleset_generate_rule
($ruleset, $chain, $rule);
1462 ruleset_addrule
($ruleset, $chain, $rule);
1468 sub save_pvefw_status
{
1471 die "unknown status '$status' - internal error"
1472 if $status !~ m/^(stopped|active)$/;
1474 mkdir dirname
($pve_fw_status_filename);
1475 PVE
::Tools
::file_set_contents
($pve_fw_status_filename, $status);
1478 sub read_pvefw_status
{
1480 my $status = 'unknown';
1482 return 'stopped' if ! -f
$pve_fw_status_filename;
1485 $status = PVE
::Tools
::file_get_contents
($pve_fw_status_filename);
1493 my $vmdata = read_local_vm_config
();
1494 my $rules = read_vm_firewall_rules
($vmdata);
1496 my $groups_conf = {};
1497 my $filename = "/etc/pve/firewall/groups.fw";
1498 if (my $fh = IO
::File-
>new($filename, O_RDONLY
)) {
1499 $groups_conf = parse_group_fw_rules
($filename, $fh);
1502 #print Dumper($rules);
1506 ruleset_create_chain
($ruleset, "PVEFW-INPUT");
1507 ruleset_create_chain
($ruleset, "PVEFW-OUTPUT");
1508 ruleset_create_chain
($ruleset, "PVEFW-FORWARD");
1510 my $hostfw_options = {};
1513 $filename = "/etc/pve/local/host.fw";
1514 if (my $fh = IO
::File-
>new($filename, O_RDONLY
)) {
1515 $hostfw_conf = parse_host_fw_rules
($filename, $fh);
1516 $hostfw_options = $hostfw_conf->{options
};
1519 generate_std_chains
($ruleset, $hostfw_options);
1521 my $hostfw_enable = $hostfw_conf &&
1522 !(defined($hostfw_options->{enable
}) && ($hostfw_options->{enable
} == 0));
1524 enablehostfw
($ruleset, $hostfw_conf, $groups_conf) if $hostfw_enable;
1526 # generate firewall rules for QEMU VMs
1527 foreach my $vmid (keys %{$vmdata->{qemu
}}) {
1528 my $conf = $vmdata->{qemu
}->{$vmid};
1529 my $vmfw_conf = $rules->{$vmid};
1530 next if !$vmfw_conf;
1531 next if defined($vmfw_conf->{options
}->{enable
}) && ($vmfw_conf->{options
}->{enable
} == 0);
1533 foreach my $netid (keys %$conf) {
1534 next if $netid !~ m/^net(\d+)$/;
1535 my $net = PVE
::QemuServer
::parse_net
($conf->{$netid});
1537 my $iface = "tap${vmid}i$1";
1539 my $bridge = $net->{bridge
};
1540 next if !$bridge; # fixme: ?
1542 $bridge .= "v$net->{tag}" if $net->{tag
};
1544 generate_bridge_chains
($ruleset, $bridge);
1546 my $macaddr = $net->{macaddr
};
1547 generate_tap_rules_direction
($ruleset, $groups_conf, $iface, $netid, $macaddr, $vmfw_conf, $bridge, 'IN');
1548 generate_tap_rules_direction
($ruleset, $groups_conf, $iface, $netid, $macaddr, $vmfw_conf, $bridge, 'OUT');
1552 if ($hostfw_enable) {
1553 # allow traffic from lo (ourself)
1554 ruleset_addrule
($ruleset, "PVEFW-INPUT", "-i lo -j ACCEPT");
1560 sub get_ruleset_status
{
1561 my ($ruleset, $verbose) = @_;
1563 my $active_chains = iptables_get_chains
();
1565 my $statushash = {};
1567 foreach my $chain (sort keys %$ruleset) {
1568 my $digest = Digest
::SHA-
>new('sha1');
1569 foreach my $cmd (@{$ruleset->{$chain}}) {
1570 $digest->add("$cmd\n");
1572 my $sig = $digest->b64digest;
1573 $statushash->{$chain}->{sig
} = $sig;
1575 my $oldsig = $active_chains->{$chain};
1576 if (!defined($oldsig)) {
1577 $statushash->{$chain}->{action
} = 'create';
1579 if ($oldsig eq $sig) {
1580 $statushash->{$chain}->{action
} = 'exists';
1582 $statushash->{$chain}->{action
} = 'update';
1585 print "$statushash->{$chain}->{action} $chain ($sig)\n" if $verbose;
1586 foreach my $cmd (@{$ruleset->{$chain}}) {
1587 print "\t$cmd\n" if $verbose;
1591 foreach my $chain (sort keys %$active_chains) {
1592 if (!defined($ruleset->{$chain})) {
1593 my $sig = $active_chains->{$chain};
1594 $statushash->{$chain}->{action
} = 'delete';
1595 $statushash->{$chain}->{sig
} = $sig;
1596 print "delete $chain ($sig)\n" if $verbose;
1606 get_ruleset_status
($ruleset, 1);
1609 sub print_sig_rule
{
1610 my ($chain, $sig) = @_;
1612 # We just use this to store a SHA1 checksum used to detect changes
1613 return "-A $chain -m comment --comment \"PVESIG:$sig\"\n";
1616 sub get_rulset_cmdlist {
1617 my ($ruleset, $verbose) = @_;
1619 my $cmdlist = "*filter
\n"; # we pass this to iptables-restore;
1621 my $statushash = get_ruleset_status($ruleset, $verbose);
1623 # create missing chains first
1624 foreach my $chain (sort keys %$ruleset) {
1625 my $stat = $statushash->{$chain};
1626 die "internal error
" if !$stat;
1627 next if $stat->{action} ne 'create';
1629 $cmdlist .= ":$chain - [0:0]\n";
1632 my $rule = "INPUT
-j PVEFW-INPUT
";
1633 if (!PVE::Firewall::iptables_rule_exist($rule)) {
1634 $cmdlist .= "-A
$rule\n";
1636 $rule = "OUTPUT
-j PVEFW-OUTPUT
";
1637 if (!PVE::Firewall::iptables_rule_exist($rule)) {
1638 $cmdlist .= "-A
$rule\n";
1641 $rule = "FORWARD
-j PVEFW-FORWARD
";
1642 if (!PVE::Firewall::iptables_rule_exist($rule)) {
1643 $cmdlist .= "-A
$rule\n";
1646 foreach my $chain (sort keys %$ruleset) {
1647 my $stat = $statushash->{$chain};
1648 die "internal error
" if !$stat;
1650 if ($stat->{action} eq 'update' || $stat->{action} eq 'create') {
1651 $cmdlist .= "-F
$chain\n";
1652 foreach my $cmd (@{$ruleset->{$chain}}) {
1653 $cmdlist .= "$cmd\n";
1655 $cmdlist .= print_sig_rule($chain, $stat->{sig});
1656 } elsif ($stat->{action} eq 'delete') {
1657 die "internal error
"; # this should not happen
1658 } elsif ($stat->{action} eq 'exists') {
1661 die "internal error
- unknown status
'$stat->{action}'";
1665 foreach my $chain (keys %$statushash) {
1666 next if $statushash->{$chain}->{action} ne 'delete';
1667 $cmdlist .= "-F
$chain\n";
1669 foreach my $chain (keys %$statushash) {
1670 next if $statushash->{$chain}->{action} ne 'delete';
1671 next if $chain eq 'PVEFW-INPUT';
1672 next if $chain eq 'PVEFW-OUTPUT';
1673 next if $chain eq 'PVEFW-FORWARD';
1674 $cmdlist .= "-X
$chain\n";
1677 $cmdlist .= "COMMIT
\n";
1683 my ($ruleset, $verbose) = @_;
1685 enable_bridge_firewall();
1687 my $cmdlist = get_rulset_cmdlist($ruleset, $verbose);
1689 print $cmdlist if $verbose;
1691 iptables_restore_cmdlist($cmdlist);
1693 # test: re-read status and check if everything is up to date
1694 my $statushash = get_ruleset_status($ruleset);
1697 foreach my $chain (sort keys %$ruleset) {
1698 my $stat = $statushash->{$chain};
1699 if ($stat->{action} ne 'exists') {
1700 warn "unable to update chain
'$chain'\n";
1705 die "unable to apply firewall changes
\n" if $errors;
1709 my ($start, $verbose) = @_;
1712 my $status = read_pvefw_status();
1714 my $ruleset = PVE::Firewall::compile();
1716 if ($start || $status eq 'active') {
1718 save_pvefw_status('active') if ($status ne 'active');
1720 PVE::Firewall::apply_ruleset($ruleset, $verbose);
1722 print "Firewall
not active
(status
= $status)\n" if $verbose;