14 use PVE
::Tools
qw(run_command lock_file);
18 my $pve_fw_lock_filename = "/var/lock/pvefw.lck";
19 my $pve_fw_status_filename = "/var/lib/pve-firewall/pvefw.status";
21 my $default_log_level = 'info';
23 my $log_level_hash = {
34 # imported/converted from: /usr/share/shorewall/macro.*
37 { action
=> 'PARAM', proto
=> 'udp', dport
=> '10080' },
38 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '10080' },
41 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '113' },
44 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '179' },
47 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6881:6889' },
48 { action
=> 'PARAM', proto
=> 'udp', dport
=> '6881' },
51 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6881:6999' },
52 { action
=> 'PARAM', proto
=> 'udp', dport
=> '6881' },
55 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '2401' },
58 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1494' },
59 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1604' },
60 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '2598' },
63 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3689' },
64 { action
=> 'PARAM', proto
=> 'udp', dport
=> '3689' },
67 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6277' },
70 { action
=> 'PARAM', proto
=> 'udp', dport
=> '67:68', sport
=> '67:68' },
71 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '67:68', sport
=> '67:68' },
74 { action
=> 'PARAM', proto
=> 'udp', dport
=> '53' },
75 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '53' },
78 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3632' },
81 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '4662' },
82 { action
=> 'PARAM', proto
=> 'udp', dport
=> '4665' },
85 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '21' },
88 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '79' },
91 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '2086' },
92 { action
=> 'PARAM', proto
=> 'udp', dport
=> '2086' },
93 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1080' },
94 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1080' },
97 { action
=> 'PARAM', proto
=> '47' },
98 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> '47' },
101 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '9418' },
104 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6346' },
105 { action
=> 'PARAM', proto
=> 'udp', dport
=> '6346' },
108 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '11371' },
111 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '80' },
114 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '443' },
117 { action
=> 'PARAM', proto
=> 'udp', dport
=> '3130' },
120 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5190' },
123 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '143' },
126 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '993' },
129 { action
=> 'PARAM', proto
=> '94' },
130 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> '94' },
133 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '631' },
136 { action
=> 'PARAM', proto
=> 'udp', dport
=> '631' },
139 { action
=> 'PARAM', source
=> 'SOURCE', dest
=> 'DEST', proto
=> 'tcp', dport
=> '631' },
140 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '631' },
143 { action
=> 'PARAM', proto
=> 'udp', dport
=> '500', sport
=> '500' },
144 { action
=> 'PARAM', proto
=> '50' },
145 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '500', sport
=> '500' },
146 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> '50' },
149 { action
=> 'PARAM', proto
=> 'udp', dport
=> '500', sport
=> '500' },
150 { action
=> 'PARAM', proto
=> '51' },
151 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '500', sport
=> '500' },
152 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> '51' },
155 { action
=> 'PARAM', proto
=> 'udp', dport
=> '500' },
156 { action
=> 'PARAM', proto
=> 'udp', dport
=> '4500' },
157 { action
=> 'PARAM', proto
=> '50' },
158 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '500' },
159 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '4500' },
160 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> '50' },
163 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6667' },
166 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5222' },
169 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5223' },
172 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5269' },
175 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '9100' },
178 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1701' },
179 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '1701' },
182 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '389' },
185 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '636' },
188 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1863' },
191 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1433' },
194 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '25' },
195 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '465' },
196 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '587' },
199 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '4949' },
202 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3306' },
205 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '119' },
208 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '563' },
211 { action
=> 'PARAM', proto
=> 'udp', dport
=> '123' },
214 { action
=> 'PARAM', proto
=> 'udp', dport
=> '123' },
215 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '123' },
218 { action
=> 'PARAM', proto
=> 'udp', dport
=> '123' },
219 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1024:65535', sport
=> '123' },
222 { action
=> 'PARAM', proto
=> '89' },
225 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1194' },
228 { action
=> 'PARAM', proto
=> 'udp', dport
=> '5632' },
229 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5631' },
232 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '110' },
235 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '995' },
238 { action
=> 'PARAM', proto
=> '47' },
239 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> '47' },
240 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1723' },
243 { action
=> 'PARAM', proto
=> 'icmp', dport
=> 'echo-request' },
246 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5432' },
249 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '515' },
252 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3389' },
255 { action
=> 'PARAM', proto
=> 'udp', dport
=> '520' },
256 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '520' },
259 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '953' },
262 { action
=> 'ACCEPT', proto
=> 'tcp', dport
=> '2703' },
265 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '37' },
268 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '873' },
271 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6566' },
274 { action
=> 'PARAM', proto
=> 'udp', dport
=> '135,445' },
275 { action
=> 'PARAM', proto
=> 'udp', dport
=> '137:139' },
276 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1024:65535', sport
=> '137' },
277 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '135,139,445' },
280 { action
=> 'PARAM', proto
=> 'udp', dport
=> '135,445' },
281 { action
=> 'PARAM', proto
=> 'udp', dport
=> '137:139' },
282 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1024:65535', sport
=> '137' },
283 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '135,139,445' },
284 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '135,445' },
285 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '137:139' },
286 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '1024:65535', sport
=> '137' },
287 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'tcp', dport
=> '135,139,445' },
290 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '901' },
293 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '25' },
296 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '465' },
299 { action
=> 'PARAM', proto
=> 'udp', dport
=> '161:162' },
300 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '161' },
303 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '783' },
306 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '22' },
309 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3690' },
312 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3874' },
313 { action
=> 'PARAM', proto
=> 'udp', dport
=> '3740' },
314 { action
=> 'PARAM', proto
=> '41' },
315 { action
=> 'PARAM', proto
=> 'udp', dport
=> '5072,8374' },
318 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3128' },
321 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '587' },
324 { action
=> 'PARAM', proto
=> 'udp', dport
=> '514' },
325 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '514' },
328 { action
=> 'PARAM', proto
=> 'udp', dport
=> '69' },
331 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '23' },
334 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '992' },
337 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '37' },
340 { action
=> 'PARAM', proto
=> 'udp', dport
=> '33434:33524' },
341 { action
=> 'PARAM', proto
=> 'icmp', dport
=> 'echo-request' },
344 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5900:5909' },
347 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5500' },
350 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '80' },
351 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '443' },
354 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '8080' },
357 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '10000' },
360 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '43' },
364 my $pve_fw_parsed_macros;
365 my $pve_fw_preferred_macro_names = {};
367 my $pve_std_chains = {
368 'PVEFW-SET-ACCEPT-MARK' => [
369 "-j MARK --set-mark 1",
371 'PVEFW-DropBroadcast' => [
372 # same as shorewall 'Broadcast'
373 # simply DROP BROADCAST/MULTICAST/ANYCAST
374 # we can use this to reduce logging
375 { action
=> 'DROP', dsttype
=> 'BROADCAST' },
376 { action
=> 'DROP', dsttype
=> 'MULTICAST' },
377 { action
=> 'DROP', dsttype
=> 'ANYCAST' },
378 { action
=> 'DROP', dest
=> '224.0.0.0/4' },
381 # same as shorewall 'reject'
382 { action
=> 'DROP', dsttype
=> 'BROADCAST' },
383 { action
=> 'DROP', source
=> '224.0.0.0/4' },
384 { action
=> 'DROP', proto
=> 'icmp' },
385 "-p tcp -j REJECT --reject-with tcp-reset",
386 "-p udp -j REJECT --reject-with icmp-port-unreachable",
387 "-p icmp -j REJECT --reject-with icmp-host-unreachable",
388 "-j REJECT --reject-with icmp-host-prohibited",
391 # same as shorewall 'Drop', which is equal to DROP,
392 # but REJECT/DROP some packages to reduce logging,
393 # and ACCEPT critical ICMP types
394 { action
=> 'PVEFW-reject', proto
=> 'tcp', dport
=> '43' }, # REJECT 'auth'
395 # we are not interested in BROADCAST/MULTICAST/ANYCAST
396 { action
=> 'PVEFW-DropBroadcast' },
397 # ACCEPT critical ICMP types
398 { action
=> 'ACCEPT', proto
=> 'icmp', dport
=> 'fragmentation-needed' },
399 { action
=> 'ACCEPT', proto
=> 'icmp', dport
=> 'time-exceeded' },
400 # Drop packets with INVALID state
401 "-m conntrack --ctstate INVALID -j DROP",
402 # Drop Microsoft SMB noise
403 { action
=> 'DROP', proto
=> 'udp', dport
=> '135,445', nbdport
=> 2 },
404 { action
=> 'DROP', proto
=> 'udp', dport
=> '137:139'},
405 { action
=> 'DROP', proto
=> 'udp', dport
=> '1024:65535', sport
=> 137 },
406 { action
=> 'DROP', proto
=> 'tcp', dport
=> '135,139,445', nbdport
=> 3 },
407 { action
=> 'DROP', proto
=> 'udp', dport
=> 1900 }, # UPnP
408 # Drop new/NotSyn traffic so that it doesn't get logged
409 "-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -j DROP",
411 { action
=> 'DROP', proto
=> 'udp', sport
=> 53 },
414 # same as shorewall 'Reject', which is equal to Reject,
415 # but REJECT/DROP some packages to reduce logging,
416 # and ACCEPT critical ICMP types
417 { action
=> 'PVEFW-reject', proto
=> 'tcp', dport
=> '43' }, # REJECT 'auth'
418 # we are not interested in BROADCAST/MULTICAST/ANYCAST
419 { action
=> 'PVEFW-DropBroadcast' },
420 # ACCEPT critical ICMP types
421 { action
=> 'ACCEPT', proto
=> 'icmp', dport
=> 'fragmentation-needed' },
422 { action
=> 'ACCEPT', proto
=> 'icmp', dport
=> 'time-exceeded' },
423 # Drop packets with INVALID state
424 "-m conntrack --ctstate INVALID -j DROP",
425 # Drop Microsoft SMB noise
426 { action
=> 'PVEFW-reject', proto
=> 'udp', dport
=> '135,445', nbdport
=> 2 },
427 { action
=> 'PVEFW-reject', proto
=> 'udp', dport
=> '137:139'},
428 { action
=> 'PVEFW-reject', proto
=> 'udp', dport
=> '1024:65535', sport
=> 137 },
429 { action
=> 'PVEFW-reject', proto
=> 'tcp', dport
=> '135,139,445', nbdport
=> 3 },
430 { action
=> 'DROP', proto
=> 'udp', dport
=> 1900 }, # UPnP
431 # Drop new/NotSyn traffic so that it doesn't get logged
432 "-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -j DROP",
434 { action
=> 'DROP', proto
=> 'udp', sport
=> 53 },
436 'PVEFW-tcpflags' => [
437 # same as shorewall tcpflags action.
438 # Packets arriving on this interface are checked for som illegal combinations of TCP flags
439 "-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG FIN,PSH,URG -g PVEFW-logflags",
440 "-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG NONE -g PVEFW-logflags",
441 "-p tcp -m tcp --tcp-flags SYN,RST SYN,RST -g PVEFW-logflags",
442 "-p tcp -m tcp --tcp-flags FIN,SYN FIN,SYN -g PVEFW-logflags",
443 "-p tcp -m tcp --sport 0 --tcp-flags FIN,SYN,RST,ACK SYN -g PVEFW-logflags",
446 # same as shorewall smurfs action
447 # Filter packets for smurfs (packets with a broadcast address as the source).
448 "-s 0.0.0.0/32 -j RETURN",
449 "-m addrtype --src-type BROADCAST -g PVEFW-smurflog",
450 "-s 224.0.0.0/4 -g PVEFW-smurflog",
454 # iptables -p icmp -h
455 my $icmp_type_names = {
458 'destination-unreachable' => 1,
459 'network-unreachable' => 1,
460 'host-unreachable' => 1,
461 'protocol-unreachable' => 1,
462 'port-unreachable' => 1,
463 'fragmentation-needed' => 1,
464 'source-route-failed' => 1,
465 'network-unknown' => 1,
467 'network-prohibited' => 1,
468 'host-prohibited' => 1,
469 'TOS-network-unreachable' => 1,
470 'TOS-host-unreachable' => 1,
471 'communication-prohibited' => 1,
472 'host-precedence-violation' => 1,
473 'precedence-cutoff' => 1,
474 'source-quench' => 1,
476 'network-redirect' => 1,
477 'host-redirect' => 1,
478 'TOS-network-redirect' => 1,
479 'TOS-host-redirect' => 1,
481 'router-advertisement' => 1,
482 'router-solicitation' => 1,
483 'time-exceeded' => 1,
484 'ttl-zero-during-transit' => 1,
485 'ttl-zero-during-reassembly' => 1,
486 'parameter-problem' => 1,
487 'ip-header-bad' => 1,
488 'required-option-missing' => 1,
489 'timestamp-request' => 1,
490 'timestamp-reply' => 1,
491 'address-mask-request' => 1,
492 'address-mask-reply' => 1,
495 sub get_firewall_macros
{
497 return $pve_fw_parsed_macros if $pve_fw_parsed_macros;
499 $pve_fw_parsed_macros = {};
501 foreach my $k (keys %$pve_fw_macros) {
504 my $macro = $pve_fw_macros->{$k};
505 $pve_fw_preferred_macro_names->{$name} = $k;
506 $pve_fw_parsed_macros->{$name} = $macro;
509 return $pve_fw_parsed_macros;
514 sub get_etc_services
{
516 return $etc_services if $etc_services;
518 my $filename = "/etc/services";
520 my $fh = IO
::File-
>new($filename, O_RDONLY
);
522 warn "unable to read '$filename' - $!\n";
528 while (my $line = <$fh>) {
530 next if $line =~m/^#/;
531 next if ($line =~m/^\s*$/);
533 if ($line =~ m!^(\S+)\s+(\S+)/(tcp|udp).*$!) {
534 $services->{byid
}->{$2}->{name
} = $1;
535 $services->{byid
}->{$2}->{port
} = $2;
536 $services->{byid
}->{$2}->{$3} = 1;
537 $services->{byname
}->{$1} = $services->{byid
}->{$2};
543 $etc_services = $services;
546 return $etc_services;
551 sub get_etc_protocols
{
552 return $etc_protocols if $etc_protocols;
554 my $filename = "/etc/protocols";
556 my $fh = IO
::File-
>new($filename, O_RDONLY
);
558 warn "unable to read '$filename' - $!\n";
564 while (my $line = <$fh>) {
566 next if $line =~m/^#/;
567 next if ($line =~m/^\s*$/);
569 if ($line =~ m!^(\S+)\s+(\d+)\s+.*$!) {
570 $protocols->{byid
}->{$2}->{name
} = $1;
571 $protocols->{byname
}->{$1} = $protocols->{byid
}->{$2};
577 $etc_protocols = $protocols;
579 return $etc_protocols;
582 sub parse_address_list
{
586 foreach my $aor (split(/,/, $str)) {
587 if (!Net
::IP-
>new($aor)) {
588 my $err = Net
::IP
::Error
();
589 die "invalid IP address: $err\n";
597 sub parse_port_name_number_or_range
{
600 my $services = PVE
::Firewall
::get_etc_services
();
602 foreach my $item (split(/,/, $str)) {
606 foreach my $pon (split(':', $item, 2)) {
607 $pon = $services->{byname
}->{$pon}->{port
} if $services->{byname
}->{$pon}->{port
};
608 if ($pon =~ m/^\d+$/){
609 die "invalid port '$pon'\n" if $pon < 0 && $pon > 65535;
610 die "port '$pon' must be bigger than port '$oldpon' \n" if $oldpon && ($pon < $oldpon);
613 die "invalid port $services->{byname}->{$pon}\n" if !$services->{byname
}->{$pon};
621 my $bridge_firewall_enabled = 0;
623 sub enable_bridge_firewall
{
625 return if $bridge_firewall_enabled; # only once
627 PVE
::ProcFSTools
::write_proc_entry
("/proc/sys/net/bridge/bridge-nf-call-iptables", "1");
628 PVE
::ProcFSTools
::write_proc_entry
("/proc/sys/net/bridge/bridge-nf-call-ip6tables", "1");
630 $bridge_firewall_enabled = 1;
633 my $rule_format = "%-15s %-30s %-30s %-15s %-15s %-15s\n";
638 run_command
("/sbin/iptables $cmd", outfunc
=> sub {}, errfunc
=> sub {});
641 sub iptables_restore_cmdlist
{
644 run_command
("/sbin/iptables-restore -n", input
=> $cmdlist);
647 sub iptables_get_chains
{
651 # check what chains we want to track
652 my $is_pvefw_chain = sub {
655 return 1 if $name =~ m/^PVEFW-\S+$/;
657 return 1 if $name =~ m/^tap\d+i\d+-(:?IN|OUT)$/;
658 return 1 if $name =~ m/^vmbr\d+-(:?FW|IN|OUT)$/;
659 return 1 if $name =~ m/^GROUP-(:?[^\s\-]+)-(:?IN|OUT)$/;
669 return if $line =~ m/^#/;
670 return if $line =~ m/^\s*$/;
672 if ($line =~ m/^\*(\S+)$/) {
677 return if $table ne 'filter';
679 if ($line =~ m/^:(\S+)\s/) {
681 return if !&$is_pvefw_chain($chain);
682 $res->{$chain} = "unknown";
683 } elsif ($line =~ m/^-A\s+(\S+)\s.*--comment\s+\"PVESIG:(\S+)\"/) {
684 my ($chain, $sig) = ($1, $2);
685 return if !&$is_pvefw_chain($chain);
686 $res->{$chain} = $sig;
688 # simply ignore the rest
693 run_command
("/sbin/iptables-save", outfunc
=> $parser);
698 sub iptables_chain_exist
{
702 iptables
("-n --list $chain");
709 sub iptables_rule_exist
{
713 iptables
("-C $rule");
720 sub ruleset_generate_rule
{
721 my ($ruleset, $chain, $rule, $actions, $goto) = @_;
723 return if $rule->{disable
};
727 push @cmd, "-m iprange --src-range" if $rule->{nbsource
} && $rule->{nbsource
} > 1;
728 push @cmd, "-s $rule->{source}" if $rule->{source
};
729 push @cmd, "-m iprange --dst-range" if $rule->{nbdest
} && $rule->{nbdest
} > 1;
730 push @cmd, "-d $rule->{dest}" if $rule->{dest
};
732 if ($rule->{proto
}) {
733 push @cmd, "-p $rule->{proto}";
736 $multiport++ if $rule->{nbdport
} && ($rule->{nbdport
} > 1);
737 $multiport++ if $rule->{nbsport
} && ($rule->{nbsport
} > 1);
739 push @cmd, "--match multiport" if $multiport;
741 die "multiport: option '--sports' cannot be used together with '--dports'\n"
742 if ($multiport == 2) && ($rule->{dport
} ne $rule->{sport
});
744 if ($rule->{dport
}) {
745 if ($rule->{proto
} && $rule->{proto
} eq 'icmp') {
746 # Note: we use dport to store --icmp-type
747 die "unknown icmp-type '$rule->{dport}'\n" if !defined($icmp_type_names->{$rule->{dport
}});
748 push @cmd, "-m icmp --icmp-type $rule->{dport}";
750 if ($rule->{nbdport
} && $rule->{nbdport
} > 1) {
751 if ($multiport == 2) {
752 push @cmd, "--ports $rule->{dport}";
754 push @cmd, "--dports $rule->{dport}";
757 push @cmd, "--dport $rule->{dport}";
762 if ($rule->{sport
}) {
763 if ($rule->{nbsport
} && $rule->{nbsport
} > 1) {
764 push @cmd, "--sports $rule->{sport}" if $multiport != 2;
766 push @cmd, "--sport $rule->{sport}";
769 } elsif ($rule->{dport
} || $rule->{sport
}) {
770 warn "ignoring destination port '$rule->{dport}' - no protocol specified\n" if $rule->{dport
};
771 warn "ignoring source port '$rule->{sport}' - no protocol specified\n" if $rule->{sport
};
774 push @cmd, "-m addrtype --dst-type $rule->{dsttype}" if $rule->{dsttype
};
776 if (my $action = $rule->{action
}) {
777 $action = $actions->{$action} if defined($actions->{$action});
778 $goto = 1 if !defined($goto) && $action eq 'PVEFW-SET-ACCEPT-MARK';
779 push @cmd, $goto ?
"-g $action" : "-j $action";
783 my $cmdstr = join(' ', @cmd);
784 ruleset_addrule
($ruleset, $chain, $cmdstr);
788 sub ruleset_create_chain
{
789 my ($ruleset, $chain) = @_;
791 die "Invalid chain name '$chain' (28 char max)\n" if length($chain) > 28;
793 die "chain '$chain' already exists\n" if $ruleset->{$chain};
795 $ruleset->{$chain} = [];
798 sub ruleset_chain_exist
{
799 my ($ruleset, $chain) = @_;
801 return $ruleset->{$chain} ?
1 : undef;
804 sub ruleset_addrule
{
805 my ($ruleset, $chain, $rule) = @_;
807 die "no such chain '$chain'\n" if !$ruleset->{$chain};
809 push @{$ruleset->{$chain}}, "-A $chain $rule";
812 sub ruleset_insertrule
{
813 my ($ruleset, $chain, $rule) = @_;
815 die "no such chain '$chain'\n" if !$ruleset->{$chain};
817 unshift @{$ruleset->{$chain}}, "-A $chain $rule";
820 sub generate_bridge_chains
{
821 my ($ruleset, $bridge) = @_;
823 if (!ruleset_chain_exist
($ruleset, "PVEFW-FORWARD")){
824 ruleset_create_chain
($ruleset, "PVEFW-FORWARD");
825 ruleset_addrule
($ruleset, "PVEFW-FORWARD", "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT");
828 if (!ruleset_chain_exist
($ruleset, "$bridge-FW")) {
829 ruleset_create_chain
($ruleset, "$bridge-FW");
830 ruleset_addrule
($ruleset, "PVEFW-FORWARD", "-o $bridge -m physdev --physdev-is-bridged -j $bridge-FW");
831 ruleset_addrule
($ruleset, "PVEFW-FORWARD", "-i $bridge -m physdev --physdev-is-bridged -j $bridge-FW");
832 ruleset_addrule
($ruleset, "PVEFW-FORWARD", "-o $bridge -j DROP"); # disable interbridge routing
833 ruleset_addrule
($ruleset, "PVEFW-FORWARD", "-i $bridge -j DROP"); # disable interbridge routing
836 if (!ruleset_chain_exist
($ruleset, "$bridge-OUT")) {
837 ruleset_create_chain
($ruleset, "$bridge-OUT");
838 ruleset_addrule
($ruleset, "$bridge-FW", "-m physdev --physdev-is-bridged --physdev-is-in -j $bridge-OUT");
841 if (!ruleset_chain_exist
($ruleset, "$bridge-IN")) {
842 ruleset_create_chain
($ruleset, "$bridge-IN");
843 ruleset_addrule
($ruleset, "$bridge-FW", "-m physdev --physdev-is-bridged --physdev-is-out -j $bridge-IN");
844 ruleset_addrule
($ruleset, "$bridge-FW", "-m mark --mark 1 -j ACCEPT");
845 # accept traffic to unmanaged bridge ports
846 ruleset_addrule
($ruleset, "$bridge-FW", "-m physdev --physdev-is-bridged --physdev-is-out -j ACCEPT ");
850 sub generate_tap_rules_direction
{
851 my ($ruleset, $groups_conf, $iface, $netid, $macaddr, $vmfw_conf, $bridge, $direction) = @_;
853 my $lc_direction = lc($direction);
855 my $rules = $vmfw_conf->{rules
};
857 my $options = $vmfw_conf->{options
};
858 my $loglevel = get_option_log_level
($options, "log_level_${lc_direction}");
860 my $tapchain = "$iface-$direction";
862 ruleset_create_chain
($ruleset, $tapchain);
864 if (!(defined($options->{nosmurfs
}) && $options->{nosmurfs
} == 0)) {
865 ruleset_addrule
($ruleset, $tapchain, "-m conntrack --ctstate INVALID,NEW -j PVEFW-smurfs");
868 if (!(defined($options->{dhcp
}) && $options->{dhcp
} == 0)) {
869 ruleset_addrule
($ruleset, $tapchain, "-p udp -m udp --dport 67:68 -j ACCEPT");
872 if ($options->{tcpflags
}) {
873 ruleset_addrule
($ruleset, $tapchain, "-p tcp -j PVEFW-tcpflags");
876 ruleset_addrule
($ruleset, $tapchain, "-m conntrack --ctstate INVALID -j DROP");
877 ruleset_addrule
($ruleset, $tapchain, "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT");
879 if ($direction eq 'OUT' && defined($macaddr) &&
880 !(defined($options->{macfilter
}) && $options->{macfilter
} == 0)) {
881 ruleset_addrule
($ruleset, $tapchain, "-m mac ! --mac-source $macaddr -j DROP");
884 foreach my $rule (@$rules) {
885 next if $rule->{iface
} && $rule->{iface
} ne $netid;
886 next if $rule->{disable
};
887 if ($rule->{type
} eq 'group') {
888 my $group_chain = "GROUP-$rule->{action}-$direction";
889 if(!ruleset_chain_exist
($ruleset, $group_chain)){
890 generate_group_rules
($ruleset, $groups_conf, $rule->{action
});
892 ruleset_addrule
($ruleset, $tapchain, "-j $group_chain");
893 ruleset_addrule
($ruleset, $tapchain, "-m mark --mark 1 -j RETURN")
894 if $direction eq 'OUT';
896 next if $rule->{type
} ne $lc_direction;
897 if ($direction eq 'OUT') {
898 ruleset_generate_rule
($ruleset, $tapchain, $rule,
899 { ACCEPT
=> "PVEFW-SET-ACCEPT-MARK", REJECT
=> "PVEFW-reject" });
901 ruleset_generate_rule
($ruleset, $tapchain, $rule, { REJECT
=> "PVEFW-reject" });
909 if ($direction eq 'OUT') {
910 $policy = $options->{'policy-out'} || 'ACCEPT'; # allow everything by default
912 $policy = $options->{'policy-in'} || 'DROP'; # allow everything by default
915 if ($policy eq 'ACCEPT') {
916 if ($direction eq 'OUT') {
917 ruleset_addrule
($ruleset, $tapchain, "-g PVEFW-SET-ACCEPT-MARK");
919 ruleset_addrule
($ruleset, $tapchain, "-j ACCEPT");
921 } elsif ($policy eq 'DROP') {
923 ruleset_addrule
($ruleset, $tapchain, "-j PVEFW-Drop");
925 ruleset_addrule
($ruleset, $tapchain, "-j LOG --log-prefix \"$tapchain-dropped: \" --log-level $loglevel")
926 if defined($loglevel);
928 ruleset_addrule
($ruleset, $tapchain, "-j DROP");
929 } elsif ($policy eq 'REJECT') {
930 ruleset_addrule
($ruleset, $tapchain, "-j PVEFW-Reject");
932 ruleset_addrule
($ruleset, $tapchain, "-j LOG --log-prefix \"$tapchain-reject: \" --log-level $loglevel")
933 if defined($loglevel);
935 ruleset_addrule
($ruleset, $tapchain, "-g PVEFW-reject");
938 die "internal error: unknown policy '$policy'";
941 # plug the tap chain to bridge chain
942 my $physdevdirection = $direction eq 'IN' ?
"out" : "in";
943 my $rule = "-m physdev --physdev-$physdevdirection $iface --physdev-is-bridged -j $tapchain";
944 ruleset_insertrule
($ruleset, "$bridge-$direction", $rule);
946 if ($direction eq 'OUT'){
947 # add tap->host rules
948 my $rule = "-m physdev --physdev-$physdevdirection $iface -j $tapchain";
949 ruleset_addrule
($ruleset, "PVEFW-INPUT", $rule);
954 my ($ruleset, $hostfw_conf, $groups_conf) = @_;
956 # fixme: allow security groups
958 my $options = $hostfw_conf->{options
};
959 my $rules = $hostfw_conf->{rules
};
961 # host inbound firewall
962 my $chain = "PVEFW-HOST-IN";
963 ruleset_create_chain
($ruleset, $chain);
965 my $loglevel = get_option_log_level
($options, "log_level_in");
967 ruleset_addrule
($ruleset, $chain, "-m conntrack --ctstate INVALID -j DROP");
968 ruleset_addrule
($ruleset, $chain, "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT");
969 ruleset_addrule
($ruleset, $chain, "-i lo -j ACCEPT");
970 ruleset_addrule
($ruleset, $chain, "-m addrtype --dst-type MULTICAST -j ACCEPT");
971 ruleset_addrule
($ruleset, $chain, "-p udp -m conntrack --ctstate NEW -m multiport --dports 5404,5405 -j ACCEPT");
972 ruleset_addrule
($ruleset, $chain, "-p udp -m udp --dport 9000 -j ACCEPT"); #corosync
974 foreach my $rule (@$rules) {
975 next if $rule->{type
} ne 'in';
976 # we use RETURN because we need to check also tap rules
977 ruleset_generate_rule
($ruleset, $chain, $rule, { ACCEPT
=> 'RETURN', REJECT
=> "PVEFW-reject" });
980 ruleset_addrule
($ruleset, $chain, "-j LOG --log-prefix \"kvmhost-IN dropped: \" --log-level $loglevel")
981 if defined($loglevel);
983 ruleset_addrule
($ruleset, $chain, "-j DROP");
985 # host outbound firewall
986 $chain = "PVEFW-HOST-OUT";
987 ruleset_create_chain
($ruleset, $chain);
989 $loglevel = get_option_log_level
($options, "log_level_out");
991 ruleset_addrule
($ruleset, $chain, "-m conntrack --ctstate INVALID -j DROP");
992 ruleset_addrule
($ruleset, $chain, "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT");
993 ruleset_addrule
($ruleset, $chain, "-o lo -j ACCEPT");
994 ruleset_addrule
($ruleset, $chain, "-m addrtype --dst-type MULTICAST -j ACCEPT");
995 ruleset_addrule
($ruleset, $chain, "-p udp -m conntrack --ctstate NEW -m multiport --dports 5404,5405 -j ACCEPT");
996 ruleset_addrule
($ruleset, $chain, "-p udp -m udp --dport 9000 -j ACCEPT"); #corosync
998 foreach my $rule (@$rules) {
999 next if $rule->{type
} ne 'out';
1000 # we use RETURN because we need to check also tap rules
1001 ruleset_generate_rule
($ruleset, $chain, $rule, { ACCEPT
=> 'RETURN', REJECT
=> "PVEFW-reject" });
1004 ruleset_addrule
($ruleset, $chain, "-j LOG --log-prefix \"kvmhost-OUT dropped: \" --log-level $loglevel")
1005 if defined($loglevel);
1007 ruleset_addrule
($ruleset, $chain, "-j DROP");
1009 ruleset_addrule
($ruleset, "PVEFW-OUTPUT", "-j PVEFW-HOST-OUT");
1010 ruleset_addrule
($ruleset, "PVEFW-INPUT", "-j PVEFW-HOST-IN");
1013 sub generate_group_rules
{
1014 my ($ruleset, $groups_conf, $group) = @_;
1016 die "no such security group '$group'\n" if !$groups_conf->{$group};
1018 my $rules = $groups_conf->{$group}->{rules
};
1020 my $chain = "GROUP-${group}-IN";
1022 ruleset_create_chain
($ruleset, $chain);
1024 foreach my $rule (@$rules) {
1025 next if $rule->{type
} ne 'in';
1026 ruleset_generate_rule
($ruleset, $chain, $rule, { REJECT
=> "PVEFW-reject" });
1029 $chain = "GROUP-${group}-OUT";
1031 ruleset_create_chain
($ruleset, $chain);
1032 ruleset_addrule
($ruleset, $chain, "-j MARK --set-mark 0"); # clear mark
1034 foreach my $rule (@$rules) {
1035 next if $rule->{type
} ne 'out';
1036 # we use PVEFW-SET-ACCEPT-MARK (Instead of ACCEPT) because we need to
1037 # check also other tap rules later
1038 ruleset_generate_rule
($ruleset, $chain, $rule,
1039 { ACCEPT
=> 'PVEFW-SET-ACCEPT-MARK', REJECT
=> "PVEFW-reject" });
1044 my $valid_netdev_names = {};
1045 for (my $i = 0; $i < $MAX_NETS; $i++) {
1046 $valid_netdev_names->{"net$i"} = 1;
1050 my ($line, $need_iface, $allow_groups) = @_;
1052 my $macros = get_firewall_macros
();
1053 my $protocols = get_etc_protocols
();
1055 my ($type, $action, $iface, $source, $dest, $proto, $dport, $sport);
1057 # we can add single line comments to the end of the rule
1058 my $comment = $1 if $line =~ s/#\s*(.*?)\s*$//;
1060 # we can disable a rule when prefixed with '|'
1061 my $disable = 1 if $line =~ s/^\|//;
1063 my @data = split(/\s+/, $line);
1064 my $expected_elements = $need_iface ?
8 : 7;
1066 die "wrong number of rule elements\n" if scalar(@data) > $expected_elements;
1069 ($type, $action, $iface, $source, $dest, $proto, $dport, $sport) = @data
1071 ($type, $action, $source, $dest, $proto, $dport, $sport) = @data;
1074 die "incomplete rule\n" if ! ($type && $action);
1081 if ($type eq 'in' || $type eq 'out') {
1082 if ($action =~ m/^(ACCEPT|DROP|REJECT)$/) {
1084 } elsif ($action =~ m/^(\S+)\((ACCEPT|DROP|REJECT)\)$/) {
1085 ($macro_name, $action) = ($1, $2);
1086 my $lc_macro_name = lc($macro_name);
1087 my $preferred_name = $pve_fw_preferred_macro_names->{$lc_macro_name};
1088 $macro_name = $preferred_name if $preferred_name;
1089 $macro = $macros->{$lc_macro_name};
1090 die "unknown macro '$macro_name'\n" if !$macro;
1092 die "unknown action '$action'\n";
1094 } elsif ($type eq 'group') {
1095 die "wrong number of rule elements\n" if scalar(@data) != 3;
1096 die "groups disabled\n" if !$allow_groups;
1098 die "invalid characters in group name\n" if $action !~ m/^[A-Za-z0-9_\-]+$/;
1100 die "unknown rule type '$type'\n";
1104 $iface = undef if $iface && $iface eq '-';
1105 die "unknown interface '$iface'\n"
1106 if defined($iface) && !$valid_netdev_names->{$iface};
1109 $proto = undef if $proto && $proto eq '-';
1110 die "unknown protokol '$proto'\n" if $proto &&
1111 !(defined($protocols->{byname
}->{$proto}) ||
1112 defined($protocols->{byid
}->{$proto}));
1114 $source = undef if $source && $source eq '-';
1115 $dest = undef if $dest && $dest eq '-';
1117 $dport = undef if $dport && $dport eq '-';
1118 $sport = undef if $sport && $sport eq '-';
1120 my $nbsource = undef;
1123 $nbsource = parse_address_list
($source) if $source;
1124 $nbdest = parse_address_list
($dest) if $dest;
1130 disable
=> $disable,
1131 comment
=> $comment,
1136 nbsource
=> $nbsource,
1144 foreach my $templ (@$macro) {
1146 my $param_used = {};
1147 foreach my $k (keys %$templ) {
1148 my $v = $templ->{$k};
1149 if ($v eq 'PARAM') {
1151 $param_used->{$k} = 1;
1152 } elsif ($v eq 'DEST') {
1153 $v = $param->{dest
};
1154 $param_used->{dest
} = 1;
1155 } elsif ($v eq 'SOURCE') {
1156 $v = $param->{source
};
1157 $param_used->{source
} = 1;
1160 die "missing parameter '$k' in macro '$macro_name'\n" if !defined($v);
1163 foreach my $k (keys %$param) {
1164 next if !defined($param->{$k});
1165 next if $param_used->{$k};
1166 if (defined($rule->{$k})) {
1167 die "parameter '$k' already define in macro (value = '$rule->{$k}')\n"
1168 if $rule->{$k} ne $param->{$k};
1170 $rule->{$k} = $param->{$k};
1173 push @$rules, $rule;
1176 push @$rules, $param;
1179 foreach my $rule (@$rules) {
1180 $rule->{nbdport
} = parse_port_name_number_or_range
($rule->{dport
})
1181 if defined($rule->{dport
});
1182 $rule->{nbsport
} = parse_port_name_number_or_range
($rule->{sport
})
1183 if defined($rule->{sport
});
1189 sub parse_vmfw_option
{
1194 my $loglevels = "emerg|alert|crit|err|warning|notice|info|debug|nolog";
1196 if ($line =~ m/^(enable|dhcp|macfilter|nosmurfs|tcpflags):\s*(0|1)\s*$/i) {
1199 } elsif ($line =~ m/^(log_level_in|log_level_out):\s*(($loglevels)\s*)?$/i) {
1201 $value = $2 ?
lc($3) : '';
1202 } elsif ($line =~ m/^(policy-(in|out)):\s*(ACCEPT|DROP|REJECT)\s*$/i) {
1207 die "can't parse option '$line'\n"
1210 return ($opt, $value);
1213 sub parse_hostfw_option
{
1218 my $loglevels = "emerg|alert|crit|err|warning|notice|info|debug|nolog";
1220 if ($line =~ m/^(enable|dhcp|nosmurfs|tcpflags):\s*(0|1)\s*$/i) {
1223 } elsif ($line =~ m/^(log_level_in|log_level_out|tcp_flags_log_level|smurf_log_level):\s*(($loglevels)\s*)?$/i) {
1225 $value = $2 ?
lc($3) : '';
1226 } elsif ($line =~ m/^(policy-(in|out)):\s*(ACCEPT|DROP|REJECT)\s*$/i) {
1231 die "can't parse option '$line'\n"
1234 return ($opt, $value);
1237 sub parse_vm_fw_rules
{
1238 my ($filename, $fh) = @_;
1240 my $res = { rules
=> [], options
=> {}};
1244 while (defined(my $line = <$fh>)) {
1245 next if $line =~ m/^#/;
1246 next if $line =~ m/^\s*$/;
1248 my $linenr = $fh->input_line_number();
1249 my $prefix = "$filename (line $linenr)";
1251 if ($line =~ m/^\[(\S+)\]\s*$/i) {
1253 warn "$prefix: ignore unknown section '$section'\n" if !$res->{$section};
1257 warn "$prefix: skip line - no section";
1261 next if !$res->{$section}; # skip undefined section
1263 if ($section eq 'options') {
1265 my ($opt, $value) = parse_vmfw_option
($line);
1266 $res->{options
}->{$opt} = $value;
1268 warn "$prefix: $@" if $@;
1273 eval { $rules = parse_fw_rule
($line, 1, 1); };
1275 warn "$prefix: $err";
1279 push @{$res->{$section}}, @$rules;
1285 sub parse_host_fw_rules
{
1286 my ($filename, $fh) = @_;
1288 my $res = { rules
=> [], options
=> {}};
1292 while (defined(my $line = <$fh>)) {
1293 next if $line =~ m/^#/;
1294 next if $line =~ m/^\s*$/;
1296 my $linenr = $fh->input_line_number();
1297 my $prefix = "$filename (line $linenr)";
1299 if ($line =~ m/^\[(\S+)\]\s*$/i) {
1301 warn "$prefix: ignore unknown section '$section'\n" if !$res->{$section};
1305 warn "$prefix: skip line - no section";
1309 next if !$res->{$section}; # skip undefined section
1311 if ($section eq 'options') {
1313 my ($opt, $value) = parse_hostfw_option
($line);
1314 $res->{options
}->{$opt} = $value;
1316 warn "$prefix: $@" if $@;
1321 eval { $rules = parse_fw_rule
($line, 1, 1); };
1323 warn "$prefix: $err";
1327 push @{$res->{$section}}, @$rules;
1333 sub parse_group_fw_rules
{
1334 my ($filename, $fh) = @_;
1339 my $res = { rules
=> [] };
1341 while (defined(my $line = <$fh>)) {
1342 next if $line =~ m/^#/;
1343 next if $line =~ m/^\s*$/;
1345 my $linenr = $fh->input_line_number();
1346 my $prefix = "$filename (line $linenr)";
1348 if ($line =~ m/^\[group\s+(\S+)\]\s*$/i) {
1353 if (!$section || !$group) {
1354 warn "$prefix: skip line - no section";
1359 eval { $rules = parse_fw_rule
($line, 0, 0); };
1361 warn "$prefix: $err";
1365 push @{$res->{$group}->{$section}}, @$rules;
1372 my ($code, @param) = @_;
1376 my $res = lock_file
($pve_fw_lock_filename, $timeout, $code, @param);
1383 sub read_local_vm_config
{
1389 my $list = PVE
::QemuServer
::config_list
();
1391 foreach my $vmid (keys %$list) {
1392 my $cfspath = PVE
::QemuServer
::cfs_config_path
($vmid);
1393 if (my $conf = PVE
::Cluster
::cfs_read_file
($cfspath)) {
1394 $qemu->{$vmid} = $conf;
1398 my $vmdata = { openvz
=> $openvz, qemu
=> $qemu };
1403 sub read_vm_firewall_rules
{
1406 foreach my $vmid (keys %{$vmdata->{qemu
}}, keys %{$vmdata->{openvz
}}) {
1407 my $filename = "/etc/pve/firewall/$vmid.fw";
1408 my $fh = IO
::File-
>new($filename, O_RDONLY
);
1411 $rules->{$vmid} = parse_vm_fw_rules
($filename, $fh);
1417 sub get_option_log_level
{
1418 my ($options, $k) = @_;
1420 my $v = $options->{$k};
1421 $v = $default_log_level if !defined($v);
1423 return undef if $v eq '' || $v eq 'nolog';
1425 $v = $log_level_hash->{$v} if defined($log_level_hash->{$v});
1427 return $v if ($v >= 0) && ($v <= 7);
1429 warn "unknown log level ($k = '$v')\n";
1434 sub generate_std_chains
{
1435 my ($ruleset, $options) = @_;
1437 my $loglevel = get_option_log_level
($options, 'smurf_log_level');
1439 # same as shorewall smurflog.
1440 if (defined($loglevel)) {
1441 $pve_std_chains-> {'PVEFW-smurflog'} = [
1442 "-j LOG --log-prefix \"smurfs-dropped\" --log-level $loglevel",
1446 $pve_std_chains-> {'PVEFW-smurflog'} = [ "-j DROP" ];
1449 # same as shorewall logflags action.
1450 $loglevel = get_option_log_level
($options, 'tcp_flags_log_level');
1451 if (defined($loglevel)) {
1452 $pve_std_chains-> {'PVEFW-logflags'} = [
1453 "-j LOG --log-prefix \"logflags-dropped:\" --log-level $loglevel --log-ip-options",
1457 $pve_std_chains-> {'PVEFW-logflags'} = [ "-j DROP" ];
1460 foreach my $chain (keys %$pve_std_chains) {
1461 ruleset_create_chain
($ruleset, $chain);
1462 foreach my $rule (@{$pve_std_chains->{$chain}}) {
1464 ruleset_generate_rule
($ruleset, $chain, $rule);
1466 ruleset_addrule
($ruleset, $chain, $rule);
1472 sub save_pvefw_status
{
1475 die "unknown status '$status' - internal error"
1476 if $status !~ m/^(stopped|active)$/;
1478 mkdir dirname
($pve_fw_status_filename);
1479 PVE
::Tools
::file_set_contents
($pve_fw_status_filename, $status);
1482 sub read_pvefw_status
{
1484 my $status = 'unknown';
1486 return 'stopped' if ! -f
$pve_fw_status_filename;
1489 $status = PVE
::Tools
::file_get_contents
($pve_fw_status_filename);
1497 my $vmdata = read_local_vm_config
();
1498 my $rules = read_vm_firewall_rules
($vmdata);
1500 my $groups_conf = {};
1501 my $filename = "/etc/pve/firewall/groups.fw";
1502 if (my $fh = IO
::File-
>new($filename, O_RDONLY
)) {
1503 $groups_conf = parse_group_fw_rules
($filename, $fh);
1506 #print Dumper($rules);
1510 ruleset_create_chain
($ruleset, "PVEFW-INPUT");
1511 ruleset_create_chain
($ruleset, "PVEFW-OUTPUT");
1512 ruleset_create_chain
($ruleset, "PVEFW-FORWARD");
1514 my $hostfw_options = {};
1517 $filename = "/etc/pve/local/host.fw";
1518 if (my $fh = IO
::File-
>new($filename, O_RDONLY
)) {
1519 $hostfw_conf = parse_host_fw_rules
($filename, $fh);
1520 $hostfw_options = $hostfw_conf->{options
};
1523 generate_std_chains
($ruleset, $hostfw_options);
1525 my $hostfw_enable = $hostfw_conf &&
1526 !(defined($hostfw_options->{enable
}) && ($hostfw_options->{enable
} == 0));
1528 enablehostfw
($ruleset, $hostfw_conf, $groups_conf) if $hostfw_enable;
1530 # generate firewall rules for QEMU VMs
1531 foreach my $vmid (keys %{$vmdata->{qemu
}}) {
1532 my $conf = $vmdata->{qemu
}->{$vmid};
1533 my $vmfw_conf = $rules->{$vmid};
1534 next if !$vmfw_conf;
1535 next if defined($vmfw_conf->{options
}->{enable
}) && ($vmfw_conf->{options
}->{enable
} == 0);
1537 foreach my $netid (keys %$conf) {
1538 next if $netid !~ m/^net(\d+)$/;
1539 my $net = PVE
::QemuServer
::parse_net
($conf->{$netid});
1541 my $iface = "tap${vmid}i$1";
1543 my $bridge = $net->{bridge
};
1544 next if !$bridge; # fixme: ?
1546 $bridge .= "v$net->{tag}" if $net->{tag
};
1548 generate_bridge_chains
($ruleset, $bridge);
1550 my $macaddr = $net->{macaddr
};
1551 generate_tap_rules_direction
($ruleset, $groups_conf, $iface, $netid, $macaddr, $vmfw_conf, $bridge, 'IN');
1552 generate_tap_rules_direction
($ruleset, $groups_conf, $iface, $netid, $macaddr, $vmfw_conf, $bridge, 'OUT');
1556 if ($hostfw_enable) {
1557 # allow traffic from lo (ourself)
1558 ruleset_addrule
($ruleset, "PVEFW-INPUT", "-i lo -j ACCEPT");
1564 sub get_ruleset_status
{
1565 my ($ruleset, $verbose) = @_;
1567 my $active_chains = iptables_get_chains
();
1569 my $statushash = {};
1571 foreach my $chain (sort keys %$ruleset) {
1572 my $digest = Digest
::SHA-
>new('sha1');
1573 foreach my $cmd (@{$ruleset->{$chain}}) {
1574 $digest->add("$cmd\n");
1576 my $sig = $digest->b64digest;
1577 $statushash->{$chain}->{sig
} = $sig;
1579 my $oldsig = $active_chains->{$chain};
1580 if (!defined($oldsig)) {
1581 $statushash->{$chain}->{action
} = 'create';
1583 if ($oldsig eq $sig) {
1584 $statushash->{$chain}->{action
} = 'exists';
1586 $statushash->{$chain}->{action
} = 'update';
1589 print "$statushash->{$chain}->{action} $chain ($sig)\n" if $verbose;
1590 foreach my $cmd (@{$ruleset->{$chain}}) {
1591 print "\t$cmd\n" if $verbose;
1595 foreach my $chain (sort keys %$active_chains) {
1596 if (!defined($ruleset->{$chain})) {
1597 my $sig = $active_chains->{$chain};
1598 $statushash->{$chain}->{action
} = 'delete';
1599 $statushash->{$chain}->{sig
} = $sig;
1600 print "delete $chain ($sig)\n" if $verbose;
1610 get_ruleset_status
($ruleset, 1);
1613 sub print_sig_rule
{
1614 my ($chain, $sig) = @_;
1616 # We just use this to store a SHA1 checksum used to detect changes
1617 return "-A $chain -m comment --comment \"PVESIG:$sig\"\n";
1620 sub get_rulset_cmdlist {
1621 my ($ruleset, $verbose) = @_;
1623 my $cmdlist = "*filter
\n"; # we pass this to iptables-restore;
1625 my $statushash = get_ruleset_status($ruleset, $verbose);
1627 # create missing chains first
1628 foreach my $chain (sort keys %$ruleset) {
1629 my $stat = $statushash->{$chain};
1630 die "internal error
" if !$stat;
1631 next if $stat->{action} ne 'create';
1633 $cmdlist .= ":$chain - [0:0]\n";
1636 my $rule = "INPUT
-j PVEFW-INPUT
";
1637 if (!PVE::Firewall::iptables_rule_exist($rule)) {
1638 $cmdlist .= "-A
$rule\n";
1640 $rule = "OUTPUT
-j PVEFW-OUTPUT
";
1641 if (!PVE::Firewall::iptables_rule_exist($rule)) {
1642 $cmdlist .= "-A
$rule\n";
1645 $rule = "FORWARD
-j PVEFW-FORWARD
";
1646 if (!PVE::Firewall::iptables_rule_exist($rule)) {
1647 $cmdlist .= "-A
$rule\n";
1650 foreach my $chain (sort keys %$ruleset) {
1651 my $stat = $statushash->{$chain};
1652 die "internal error
" if !$stat;
1654 if ($stat->{action} eq 'update' || $stat->{action} eq 'create') {
1655 $cmdlist .= "-F
$chain\n";
1656 foreach my $cmd (@{$ruleset->{$chain}}) {
1657 $cmdlist .= "$cmd\n";
1659 $cmdlist .= print_sig_rule($chain, $stat->{sig});
1660 } elsif ($stat->{action} eq 'delete') {
1661 die "internal error
"; # this should not happen
1662 } elsif ($stat->{action} eq 'exists') {
1665 die "internal error
- unknown status
'$stat->{action}'";
1669 foreach my $chain (keys %$statushash) {
1670 next if $statushash->{$chain}->{action} ne 'delete';
1671 $cmdlist .= "-F
$chain\n";
1673 foreach my $chain (keys %$statushash) {
1674 next if $statushash->{$chain}->{action} ne 'delete';
1675 next if $chain eq 'PVEFW-INPUT';
1676 next if $chain eq 'PVEFW-OUTPUT';
1677 next if $chain eq 'PVEFW-FORWARD';
1678 $cmdlist .= "-X
$chain\n";
1681 $cmdlist .= "COMMIT
\n";
1687 my ($ruleset, $verbose) = @_;
1689 enable_bridge_firewall();
1691 my $cmdlist = get_rulset_cmdlist($ruleset, $verbose);
1693 print $cmdlist if $verbose;
1695 iptables_restore_cmdlist($cmdlist);
1697 # test: re-read status and check if everything is up to date
1698 my $statushash = get_ruleset_status($ruleset);
1701 foreach my $chain (sort keys %$ruleset) {
1702 my $stat = $statushash->{$chain};
1703 if ($stat->{action} ne 'exists') {
1704 warn "unable to update chain
'$chain'\n";
1709 die "unable to apply firewall changes
\n" if $errors;
1713 my ($start, $verbose) = @_;
1716 my $status = read_pvefw_status();
1718 my $ruleset = PVE::Firewall::compile();
1720 if ($start || $status eq 'active') {
1722 save_pvefw_status('active') if ($status ne 'active');
1724 PVE::Firewall::apply_ruleset($ruleset, $verbose);
1726 print "Firewall
not active
(status
= $status)\n" if $verbose;