12 use PVE
::Tools
qw(run_command lock_file);
16 my $pve_fw_lock_filename = "/var/lock/pvefw.lck";
18 # imported/converted from: /usr/share/shorewall/macro.*
21 { action
=> 'PARAM', proto
=> 'udp', dport
=> '10080' },
22 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '10080' },
25 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '113' },
28 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '179' },
31 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6881:6889' },
32 { action
=> 'PARAM', proto
=> 'udp', dport
=> '6881' },
35 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6881:6999' },
36 { action
=> 'PARAM', proto
=> 'udp', dport
=> '6881' },
39 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '2401' },
42 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1494' },
43 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1604' },
44 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '2598' },
47 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3689' },
48 { action
=> 'PARAM', proto
=> 'udp', dport
=> '3689' },
51 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6277' },
54 { action
=> 'PARAM', proto
=> 'udp', dport
=> '67:68', sport
=> '67:68' },
55 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '67:68', sport
=> '67:68' },
58 { action
=> 'PARAM', proto
=> 'udp', dport
=> '53' },
59 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '53' },
62 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3632' },
65 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '4662' },
66 { action
=> 'PARAM', proto
=> 'udp', dport
=> '4665' },
69 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '21' },
72 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '79' },
75 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '2086' },
76 { action
=> 'PARAM', proto
=> 'udp', dport
=> '2086' },
77 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1080' },
78 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1080' },
81 { action
=> 'PARAM', proto
=> '47' },
82 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> '47' },
85 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '9418' },
88 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6346' },
89 { action
=> 'PARAM', proto
=> 'udp', dport
=> '6346' },
92 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '11371' },
95 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '80' },
98 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '443' },
101 { action
=> 'PARAM', proto
=> 'udp', dport
=> '3130' },
104 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5190' },
107 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '143' },
110 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '993' },
113 { action
=> 'PARAM', proto
=> '94' },
114 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> '94' },
117 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '631' },
120 { action
=> 'PARAM', proto
=> 'udp', dport
=> '631' },
123 { action
=> 'PARAM', source
=> 'SOURCE', dest
=> 'DEST', proto
=> 'tcp', dport
=> '631' },
124 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '631' },
127 { action
=> 'PARAM', proto
=> 'udp', dport
=> '500', sport
=> '500' },
128 { action
=> 'PARAM', proto
=> '50' },
129 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '500', sport
=> '500' },
130 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> '50' },
133 { action
=> 'PARAM', proto
=> 'udp', dport
=> '500', sport
=> '500' },
134 { action
=> 'PARAM', proto
=> '51' },
135 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '500', sport
=> '500' },
136 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> '51' },
139 { action
=> 'PARAM', proto
=> 'udp', dport
=> '500' },
140 { action
=> 'PARAM', proto
=> 'udp', dport
=> '4500' },
141 { action
=> 'PARAM', proto
=> '50' },
142 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '500' },
143 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '4500' },
144 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> '50' },
147 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6667' },
150 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5222' },
153 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5223' },
156 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5269' },
159 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '9100' },
162 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1701' },
163 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '1701' },
166 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '389' },
169 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '636' },
172 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1863' },
175 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1433' },
178 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '25' },
179 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '465' },
180 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '587' },
183 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '4949' },
186 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3306' },
189 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '119' },
192 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '563' },
195 { action
=> 'PARAM', proto
=> 'udp', dport
=> '123' },
198 { action
=> 'PARAM', proto
=> 'udp', dport
=> '123' },
199 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '123' },
202 { action
=> 'PARAM', proto
=> 'udp', dport
=> '123' },
203 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1024:65535', sport
=> '123' },
206 { action
=> 'PARAM', proto
=> '89' },
209 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1194' },
212 { action
=> 'PARAM', proto
=> 'udp', dport
=> '5632' },
213 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5631' },
216 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '110' },
219 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '995' },
222 { action
=> 'PARAM', proto
=> '47' },
223 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> '47' },
224 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1723' },
227 { action
=> 'PARAM', proto
=> 'icmp', dport
=> '8' },
230 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5432' },
233 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '515' },
236 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3389' },
239 { action
=> 'PARAM', proto
=> 'udp', dport
=> '520' },
240 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '520' },
243 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '953' },
246 { action
=> 'ACCEPT', proto
=> 'tcp', dport
=> '2703' },
249 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '37' },
252 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '873' },
255 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6566' },
258 { action
=> 'PARAM', proto
=> 'udp', dport
=> '135,445' },
259 { action
=> 'PARAM', proto
=> 'udp', dport
=> '137:139' },
260 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1024:65535', sport
=> '137' },
261 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '135,139,445' },
264 { action
=> 'PARAM', proto
=> 'udp', dport
=> '135,445' },
265 { action
=> 'PARAM', proto
=> 'udp', dport
=> '137:139' },
266 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1024:65535', sport
=> '137' },
267 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '135,139,445' },
268 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '135,445' },
269 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '137:139' },
270 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '1024:65535', sport
=> '137' },
271 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'tcp', dport
=> '135,139,445' },
274 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '901' },
277 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '25' },
280 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '465' },
283 { action
=> 'PARAM', proto
=> 'udp', dport
=> '161:162' },
284 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '161' },
287 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '783' },
290 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '22' },
293 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3690' },
296 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3874' },
297 { action
=> 'PARAM', proto
=> 'udp', dport
=> '3740' },
298 { action
=> 'PARAM', proto
=> '41' },
299 { action
=> 'PARAM', proto
=> 'udp', dport
=> '5072,8374' },
302 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3128' },
305 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '587' },
308 { action
=> 'PARAM', proto
=> 'udp', dport
=> '514' },
309 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '514' },
312 { action
=> 'PARAM', proto
=> 'udp', dport
=> '69' },
315 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '23' },
318 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '992' },
321 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '37' },
324 { action
=> 'PARAM', proto
=> 'udp', dport
=> '33434:33524' },
325 { action
=> 'PARAM', proto
=> 'icmp', dport
=> '8' },
328 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5900:5909' },
331 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5500' },
334 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '80' },
335 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '443' },
338 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '8080' },
341 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '10000' },
344 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '43' },
348 my $pve_fw_parsed_macros;
349 my $pve_fw_preferred_macro_names = {};
351 my $pve_std_chains = {
352 'PVEFW-SET-ACCEPT-MARK' => [
353 "-j MARK --set-mark 1",
355 'PVEFW-DropBroadcast' => [
356 # same as shorewall 'Broadcast'
357 # simply DROP BROADCAST/MULTICAST/ANYCAST
358 # we can use this to reduce logging
359 { action
=> 'DROP', dsttype
=> 'BROADCAST' },
360 { action
=> 'DROP', dsttype
=> 'MULTICAST' },
361 { action
=> 'DROP', dsttype
=> 'ANYCAST' },
362 { action
=> 'DROP', dest
=> '224.0.0.0/4' },
365 # same as shorewall 'reject'
366 { action
=> 'DROP', dsttype
=> 'BROADCAST' },
367 { action
=> 'DROP', source
=> '224.0.0.0/4' },
368 { action
=> 'DROP', proto
=> 'icmp' },
369 "-p tcp -j REJECT --reject-with tcp-reset",
370 "-p udp -j REJECT --reject-with icmp-port-unreachable",
371 "-p icmp -j REJECT --reject-with icmp-host-unreachable",
372 "-j REJECT --reject-with icmp-host-prohibited",
375 # same as shorewall 'Drop', which is equal to DROP,
376 # but REJECT/DROP some packages to reduce logging,
377 # and ACCEPT critical ICMP types
378 { action
=> 'PVEFW-reject', proto
=> 'tcp', dport
=> '43' }, # REJECT 'auth'
379 # we are not interested in BROADCAST/MULTICAST/ANYCAST
380 { action
=> 'PVEFW-DropBroadcast' },
381 # ACCEPT critical ICMP types
382 { action
=> 'ACCEPT', proto
=> 'icmp', dport
=> 'fragmentation-needed' },
383 { action
=> 'ACCEPT', proto
=> 'icmp', dport
=> 'time-exceeded' },
384 # Drop packets with INVALID state
385 "-m conntrack --ctstate INVALID -j DROP",
386 # Drop Microsoft SMB noise
387 { action
=> 'DROP', proto
=> 'udp', dport
=> '135,445', nbdport
=> 2 },
388 { action
=> 'DROP', proto
=> 'udp', dport
=> '137:139'},
389 { action
=> 'DROP', proto
=> 'udp', dport
=> '1024:65535', sport
=> 137 },
390 { action
=> 'DROP', proto
=> 'tcp', dport
=> '135,139,445', nbdport
=> 3 },
391 { action
=> 'DROP', proto
=> 'udp', dport
=> 1900 }, # UPnP
392 # Drop new/NotSyn traffic so that it doesn't get logged
393 "-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -j DROP",
395 { action
=> 'DROP', proto
=> 'udp', sport
=> 53 },
398 # same as shorewall 'Reject', which is equal to Reject,
399 # but REJECT/DROP some packages to reduce logging,
400 # and ACCEPT critical ICMP types
401 { action
=> 'PVEFW-reject', proto
=> 'tcp', dport
=> '43' }, # REJECT 'auth'
402 # we are not interested in BROADCAST/MULTICAST/ANYCAST
403 { action
=> 'PVEFW-DropBroadcast' },
404 # ACCEPT critical ICMP types
405 { action
=> 'ACCEPT', proto
=> 'icmp', dport
=> 'fragmentation-needed' },
406 { action
=> 'ACCEPT', proto
=> 'icmp', dport
=> 'time-exceeded' },
407 # Drop packets with INVALID state
408 "-m conntrack --ctstate INVALID -j DROP",
409 # Drop Microsoft SMB noise
410 { action
=> 'PVEFW-reject', proto
=> 'udp', dport
=> '135,445', nbdport
=> 2 },
411 { action
=> 'PVEFW-reject', proto
=> 'udp', dport
=> '137:139'},
412 { action
=> 'PVEFW-reject', proto
=> 'udp', dport
=> '1024:65535', sport
=> 137 },
413 { action
=> 'PVEFW-reject', proto
=> 'tcp', dport
=> '135,139,445', nbdport
=> 3 },
414 { action
=> 'DROP', proto
=> 'udp', dport
=> 1900 }, # UPnP
415 # Drop new/NotSyn traffic so that it doesn't get logged
416 "-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -j DROP",
418 { action
=> 'DROP', proto
=> 'udp', sport
=> 53 },
422 # iptables -p icmp -h
423 my $icmp_type_names = {
426 'destination-unreachable' => 1,
427 'network-unreachable' => 1,
428 'host-unreachable' => 1,
429 'protocol-unreachable' => 1,
430 'port-unreachable' => 1,
431 'fragmentation-needed' => 1,
432 'source-route-failed' => 1,
433 'network-unknown' => 1,
435 'network-prohibited' => 1,
436 'host-prohibited' => 1,
437 'TOS-network-unreachable' => 1,
438 'TOS-host-unreachable' => 1,
439 'communication-prohibited' => 1,
440 'host-precedence-violation' => 1,
441 'precedence-cutoff' => 1,
442 'source-quench' => 1,
444 'network-redirect' => 1,
445 'host-redirect' => 1,
446 'TOS-network-redirect' => 1,
447 'TOS-host-redirect' => 1,
449 'router-advertisement' => 1,
450 'router-solicitation' => 1,
451 'time-exceeded' => 1,
452 'ttl-zero-during-transit' => 1,
453 'ttl-zero-during-reassembly' => 1,
454 'parameter-problem' => 1,
455 'ip-header-bad' => 1,
456 'required-option-missing' => 1,
457 'timestamp-request' => 1,
458 'timestamp-reply' => 1,
459 'address-mask-request' => 1,
460 'address-mask-reply' => 1,
463 sub get_firewall_macros
{
465 return $pve_fw_parsed_macros if $pve_fw_parsed_macros;
467 $pve_fw_parsed_macros = {};
469 foreach my $k (keys %$pve_fw_macros) {
472 my $macro = $pve_fw_macros->{$k};
473 $pve_fw_preferred_macro_names->{$name} = $k;
474 $pve_fw_parsed_macros->{$name} = $macro;
477 return $pve_fw_parsed_macros;
482 sub get_etc_services
{
484 return $etc_services if $etc_services;
486 my $filename = "/etc/services";
488 my $fh = IO
::File-
>new($filename, O_RDONLY
);
490 warn "unable to read '$filename' - $!\n";
496 while (my $line = <$fh>) {
498 next if $line =~m/^#/;
499 next if ($line =~m/^\s*$/);
501 if ($line =~ m!^(\S+)\s+(\S+)/(tcp|udp).*$!) {
502 $services->{byid
}->{$2}->{name
} = $1;
503 $services->{byid
}->{$2}->{port
} = $2;
504 $services->{byid
}->{$2}->{$3} = 1;
505 $services->{byname
}->{$1} = $services->{byid
}->{$2};
511 $etc_services = $services;
514 return $etc_services;
519 sub get_etc_protocols
{
520 return $etc_protocols if $etc_protocols;
522 my $filename = "/etc/protocols";
524 my $fh = IO
::File-
>new($filename, O_RDONLY
);
526 warn "unable to read '$filename' - $!\n";
532 while (my $line = <$fh>) {
534 next if $line =~m/^#/;
535 next if ($line =~m/^\s*$/);
537 if ($line =~ m!^(\S+)\s+(\d+)\s+.*$!) {
538 $protocols->{byid
}->{$2}->{name
} = $1;
539 $protocols->{byname
}->{$1} = $protocols->{byid
}->{$2};
545 $etc_protocols = $protocols;
547 return $etc_protocols;
550 sub parse_address_list
{
554 foreach my $aor (split(/,/, $str)) {
555 if (!Net
::IP-
>new($aor)) {
556 my $err = Net
::IP
::Error
();
557 die "invalid IP address: $err\n";
565 sub parse_port_name_number_or_range
{
568 my $services = PVE
::Firewall
::get_etc_services
();
570 foreach my $item (split(/,/, $str)) {
574 foreach my $pon (split(':', $item, 2)) {
575 $pon = $services->{byname
}->{$pon}->{port
} if $services->{byname
}->{$pon}->{port
};
576 if ($pon =~ m/^\d+$/){
577 die "invalid port '$pon'\n" if $pon < 0 && $pon > 65535;
578 die "port '$pon' must be bigger than port '$oldpon' \n" if $oldpon && ($pon < $oldpon);
581 die "invalid port $services->{byname}->{$pon}\n" if !$services->{byname
}->{$pon};
589 my $bridge_firewall_enabled = 0;
591 sub enable_bridge_firewall
{
593 return if $bridge_firewall_enabled; # only once
595 system("echo 1 > /proc/sys/net/bridge/bridge-nf-call-iptables");
596 system("echo 1 > /proc/sys/net/bridge/bridge-nf-call-ip6tables");
598 $bridge_firewall_enabled = 1;
601 my $rule_format = "%-15s %-30s %-30s %-15s %-15s %-15s\n";
606 run_command
("/sbin/iptables $cmd", outfunc
=> sub {}, errfunc
=> sub {});
609 sub iptables_restore_cmdlist
{
612 run_command
("/sbin/iptables-restore -n", input
=> $cmdlist);
615 sub iptables_get_chains
{
619 # check what chains we want to track
620 my $is_pvefw_chain = sub {
623 return 1 if $name =~ m/^PVEFW-\S+$/;
625 return 1 if $name =~ m/^tap\d+i\d+-(:?IN|OUT)$/;
626 return 1 if $name =~ m/^vmbr\d+-(:?FW|IN|OUT)$/;
627 return 1 if $name =~ m/^GROUP-(:?[^\s\-]+)-(:?IN|OUT)$/;
637 return if $line =~ m/^#/;
638 return if $line =~ m/^\s*$/;
640 if ($line =~ m/^\*(\S+)$/) {
645 return if $table ne 'filter';
647 if ($line =~ m/^:(\S+)\s/) {
649 return if !&$is_pvefw_chain($chain);
650 $res->{$chain} = "unknown";
651 } elsif ($line =~ m/^-A\s+(\S+)\s.*--comment\s+\"PVESIG:(\S+)\"/) {
652 my ($chain, $sig) = ($1, $2);
653 return if !&$is_pvefw_chain($chain);
654 $res->{$chain} = $sig;
656 # simply ignore the rest
661 run_command
("/sbin/iptables-save", outfunc
=> $parser);
666 sub iptables_chain_exist
{
670 iptables
("-n --list $chain");
677 sub iptables_rule_exist
{
681 iptables
("-C $rule");
688 sub ruleset_generate_rule
{
689 my ($ruleset, $chain, $rule, $goto) = @_;
693 $cmd .= " -m iprange --src-range" if $rule->{nbsource
} && $rule->{nbsource
} > 1;
694 $cmd .= " -s $rule->{source}" if $rule->{source
};
695 $cmd .= " -m iprange --dst-range" if $rule->{nbdest
} && $rule->{nbdest
} > 1;
696 $cmd .= " -d $rule->{dest}" if $rule->{dest
};
697 $cmd .= " -p $rule->{proto}" if $rule->{proto
};
699 if (($rule->{nbdport
} && $rule->{nbdport
} > 1) ||
700 ($rule->{nbsport
} && $rule->{nbsport
} > 1)) {
701 $cmd .= " --match multiport"
704 if ($rule->{dport
}) {
705 if ($rule->{proto
} && $rule->{proto
} eq 'icmp') {
706 # Note: we use dport to store --icmp-type
707 die "unknown icmp-type\n" if !$icmp_type_names->{$rule->{dport
}};
708 $cmd .= " -m icmp --icmp-type $rule->{dport}";
710 if ($rule->{nbdport
} && $rule->{nbdport
} > 1) {
711 $cmd .= " --dports $rule->{dport}";
713 $cmd .= " --dport $rule->{dport}";
718 if ($rule->{sport
}) {
719 if ($rule->{nbsport
} && $rule->{nbsport
} > 1) {
720 $cmd .= " --sports $rule->{sport}";
722 $cmd .= " --sport $rule->{sport}";
726 $cmd .= " -m addrtype --dst-type $rule->{dsttype}" if $rule->{dsttype
};
728 if (my $action = $rule->{action
}) {
729 $goto = 1 if !defined($goto) && $action eq 'PVEFW-SET-ACCEPT-MARK';
730 $cmd .= $goto ?
" -g $action" : " -j $action";
733 ruleset_addrule
($ruleset, $chain, $cmd) if $cmd;
736 sub ruleset_create_chain
{
737 my ($ruleset, $chain) = @_;
739 die "Invalid chain name '$chain' (28 char max)\n" if length($chain) > 28;
741 die "chain '$chain' already exists\n" if $ruleset->{$chain};
743 $ruleset->{$chain} = [];
746 sub ruleset_chain_exist
{
747 my ($ruleset, $chain) = @_;
749 return $ruleset->{$chain} ?
1 : undef;
752 sub ruleset_addrule
{
753 my ($ruleset, $chain, $rule) = @_;
755 die "no such chain '$chain'\n" if !$ruleset->{$chain};
757 push @{$ruleset->{$chain}}, "-A $chain $rule";
760 sub ruleset_insertrule
{
761 my ($ruleset, $chain, $rule) = @_;
763 die "no such chain '$chain'\n" if !$ruleset->{$chain};
765 unshift @{$ruleset->{$chain}}, "-A $chain $rule";
768 sub generate_bridge_chains
{
769 my ($ruleset, $bridge) = @_;
771 if (!ruleset_chain_exist
($ruleset, "PVEFW-FORWARD")){
772 ruleset_create_chain
($ruleset, "PVEFW-FORWARD");
773 ruleset_addrule
($ruleset, "PVEFW-FORWARD", "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT");
776 if (!ruleset_chain_exist
($ruleset, "$bridge-FW")) {
777 ruleset_create_chain
($ruleset, "$bridge-FW");
778 ruleset_addrule
($ruleset, "PVEFW-FORWARD", "-o $bridge -m physdev --physdev-is-bridged -j $bridge-FW");
779 ruleset_addrule
($ruleset, "PVEFW-FORWARD", "-i $bridge -m physdev --physdev-is-bridged -j $bridge-FW");
780 ruleset_addrule
($ruleset, "PVEFW-FORWARD", "-o $bridge -j DROP"); # disable interbridge routing
781 ruleset_addrule
($ruleset, "PVEFW-FORWARD", "-i $bridge -j DROP"); # disable interbridge routing
784 if (!ruleset_chain_exist
($ruleset, "$bridge-OUT")) {
785 ruleset_create_chain
($ruleset, "$bridge-OUT");
786 ruleset_addrule
($ruleset, "$bridge-FW", "-m physdev --physdev-is-bridged --physdev-is-in -j $bridge-OUT");
789 if (!ruleset_chain_exist
($ruleset, "$bridge-IN")) {
790 ruleset_create_chain
($ruleset, "$bridge-IN");
791 ruleset_addrule
($ruleset, "$bridge-FW", "-m physdev --physdev-is-bridged --physdev-is-out -j $bridge-IN");
792 ruleset_addrule
($ruleset, "$bridge-FW", "-m mark --mark 1 -j ACCEPT");
796 sub generate_tap_rules_direction
{
797 my ($ruleset, $group_rules, $iface, $netid, $macaddr, $vmfw_conf, $bridge, $direction) = @_;
799 my $rules = $vmfw_conf->{lc($direction)};
800 my $options = $vmfw_conf->{options
};
802 my $tapchain = "$iface-$direction";
804 ruleset_create_chain
($ruleset, $tapchain);
806 ruleset_addrule
($ruleset, $tapchain, "-m conntrack --ctstate INVALID -j DROP");
807 ruleset_addrule
($ruleset, $tapchain, "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT");
809 if ($direction eq 'OUT' && defined($macaddr) &&
810 !(defined($options->{macfilter
}) && $options->{macfilter
} == 0)) {
811 ruleset_addrule
($ruleset, $tapchain, "-m mac ! --mac-source $macaddr -j DROP");
815 foreach my $rule (@$rules) {
816 next if $rule->{iface
} && $rule->{iface
} ne $netid;
817 # we go to $bridge-IN if accept in out rules
818 if($rule->{action
} =~ m/^(GROUP-(\S+))$/){
819 $rule->{action
} .= "-$direction";
820 # generate empty group rule if don't exist
821 if(!ruleset_chain_exist
($ruleset, $rule->{action
})){
822 generate_group_rules
($ruleset, $group_rules, $2);
824 ruleset_generate_rule
($ruleset, $tapchain, $rule);
825 ruleset_addrule
($ruleset, $tapchain, "-m mark --mark 1 -j RETURN")
826 if $direction eq 'OUT';
828 $rule->{action
} = "PVEFW-SET-ACCEPT-MARK" if $rule->{action
} eq 'ACCEPT' && $direction eq 'OUT';
829 ruleset_generate_rule
($ruleset, $tapchain, $rule);
837 if ($direction eq 'OUT') {
838 $policy = $options->{'policy-out'} || 'ACCEPT'; # allow everything by default
840 $policy = $options->{'policy-in'} || 'DROP'; # allow everything by default
843 if ($policy eq 'ACCEPT') {
844 if ($direction eq 'OUT') {
845 ruleset_addrule
($ruleset, $tapchain, "-g PVEFW-SET-ACCEPT-MARK");
847 ruleset_addrule
($ruleset, $tapchain, "-j ACCEPT");
849 } elsif ($policy eq 'DROP') {
850 ruleset_addrule
($ruleset, $tapchain, "-j PVEFW-Drop");
851 ruleset_addrule
($ruleset, $tapchain, "-j LOG --log-prefix \"$tapchain-dropped: \" --log-level 4");
852 ruleset_addrule
($ruleset, $tapchain, "-j DROP");
853 } elsif ($policy eq 'REJECT') {
854 ruleset_addrule
($ruleset, $tapchain, "-j PVEFW-Reject");
855 ruleset_addrule
($ruleset, $tapchain, "-j LOG --log-prefix \"$tapchain-reject: \" --log-level 4");
856 ruleset_addrule
($ruleset, $tapchain, "-g PVEFW-reject");
859 die "internal error: unknown policy '$policy'";
862 # plug the tap chain to bridge chain
863 my $physdevdirection = $direction eq 'IN' ?
"out" : "in";
864 my $rule = "-m physdev --physdev-$physdevdirection $iface --physdev-is-bridged -j $tapchain";
865 ruleset_insertrule
($ruleset, "$bridge-$direction", $rule);
867 if ($direction eq 'OUT'){
868 # add tap->host rules
869 my $rule = "-m physdev --physdev-$physdevdirection $iface -j $tapchain";
870 ruleset_addrule
($ruleset, "PVEFW-INPUT", $rule);
875 my ($ruleset, $rules, $group_rules) = @_;
877 # fixme: allow security groups
879 # host inbound firewall
880 my $chain = "PVEFW-HOST-IN";
881 ruleset_create_chain
($ruleset, $chain);
883 ruleset_addrule
($ruleset, $chain, "-m conntrack --ctstate INVALID -j DROP");
884 ruleset_addrule
($ruleset, $chain, "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT");
885 ruleset_addrule
($ruleset, $chain, "-i lo -j ACCEPT");
886 ruleset_addrule
($ruleset, $chain, "-m addrtype --dst-type MULTICAST -j ACCEPT");
887 ruleset_addrule
($ruleset, $chain, "-p udp -m conntrack --ctstate NEW -m multiport --dports 5404,5405 -j ACCEPT");
888 ruleset_addrule
($ruleset, $chain, "-p udp -m udp --dport 9000 -j ACCEPT"); #corosync
891 foreach my $rule (@{$rules->{in}}) {
892 # we use RETURN because we need to check also tap rules
893 $rule->{action
} = 'RETURN' if $rule->{action
} eq 'ACCEPT';
894 ruleset_generate_rule
($ruleset, $chain, $rule);
898 ruleset_addrule
($ruleset, $chain, "-j LOG --log-prefix \"kvmhost-IN dropped: \" --log-level 4");
899 ruleset_addrule
($ruleset, $chain, "-j DROP");
901 # host outbound firewall
902 $chain = "PVEFW-HOST-OUT";
903 ruleset_create_chain
($ruleset, $chain);
905 ruleset_addrule
($ruleset, $chain, "-m conntrack --ctstate INVALID -j DROP");
906 ruleset_addrule
($ruleset, $chain, "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT");
907 ruleset_addrule
($ruleset, $chain, "-o lo -j ACCEPT");
908 ruleset_addrule
($ruleset, $chain, "-m addrtype --dst-type MULTICAST -j ACCEPT");
909 ruleset_addrule
($ruleset, $chain, "-p udp -m conntrack --ctstate NEW -m multiport --dports 5404,5405 -j ACCEPT");
910 ruleset_addrule
($ruleset, $chain, "-p udp -m udp --dport 9000 -j ACCEPT"); #corosync
913 foreach my $rule (@{$rules->{out
}}) {
914 # we use RETURN because we need to check also tap rules
915 $rule->{action
} = 'RETURN' if $rule->{action
} eq 'ACCEPT';
916 ruleset_generate_rule
($ruleset, $chain, $rule);
920 ruleset_addrule
($ruleset, $chain, "-j LOG --log-prefix \"kvmhost-OUT dropped: \" --log-level 4");
921 ruleset_addrule
($ruleset, $chain, "-j DROP");
923 ruleset_addrule
($ruleset, "PVEFW-OUTPUT", "-j PVEFW-HOST-OUT");
924 ruleset_addrule
($ruleset, "PVEFW-INPUT", "-j PVEFW-HOST-IN");
927 sub generate_group_rules
{
928 my ($ruleset, $group_rules, $group) = @_;
930 my $rules = $group_rules->{$group};
932 die "no such security group '$group'\n" if !$rules;
934 my $chain = "GROUP-${group}-IN";
936 ruleset_create_chain
($ruleset, $chain);
939 foreach my $rule (@{$rules->{in}}) {
940 ruleset_generate_rule
($ruleset, $chain, $rule);
944 $chain = "GROUP-${group}-OUT";
946 ruleset_create_chain
($ruleset, $chain);
947 ruleset_addrule
($ruleset, $chain, "-j MARK --set-mark 0"); # clear mark
950 foreach my $rule (@{$rules->{out
}}) {
951 # we go the PVEFW-SET-ACCEPT-MARK Instead of ACCEPT) because we need to
952 # check also other tap rules (and group rules can be set on any bridge,
953 # so we can't go to VMBRXX-IN)
954 $rule->{action
} = 'PVEFW-SET-ACCEPT-MARK' if $rule->{action
} eq 'ACCEPT';
955 ruleset_generate_rule
($ruleset, $chain, $rule);
961 my $valid_netdev_names = {};
962 for (my $i = 0; $i < $MAX_NETS; $i++) {
963 $valid_netdev_names->{"net$i"} = 1;
967 my ($line, $need_iface, $allow_groups) = @_;
969 my $macros = get_firewall_macros
();
970 my $protocols = get_etc_protocols
();
972 my ($action, $iface, $source, $dest, $proto, $dport, $sport);
976 my @data = split(/\s+/, $line);
977 my $expected_elements = $need_iface ?
7 : 6;
979 die "wrong number of rule elements\n" if scalar(@data) > $expected_elements;
982 ($action, $iface, $source, $dest, $proto, $dport, $sport) = @data
984 ($action, $source, $dest, $proto, $dport, $sport) = @data;
987 die "incomplete rule\n" if !$action;
992 if ($action =~ m/^(ACCEPT|DROP|REJECT)$/) {
994 } elsif ($allow_groups && $action =~ m/^GROUP-(:?\S+)$/) {
996 } elsif ($action =~ m/^(\S+)\((ACCEPT|DROP|REJECT)\)$/) {
997 ($macro_name, $action) = ($1, $2);
998 my $lc_macro_name = lc($macro_name);
999 my $preferred_name = $pve_fw_preferred_macro_names->{$lc_macro_name};
1000 $macro_name = $preferred_name if $preferred_name;
1001 $macro = $macros->{$lc_macro_name};
1002 die "unknown macro '$macro_name'\n" if !$macro;
1004 die "unknown action '$action'\n";
1008 $iface = undef if $iface && $iface eq '-';
1009 die "unknown interface '$iface'\n"
1010 if defined($iface) && !$valid_netdev_names->{$iface};
1013 $proto = undef if $proto && $proto eq '-';
1014 die "unknown protokol '$proto'\n" if $proto &&
1015 !(defined($protocols->{byname
}->{$proto}) ||
1016 defined($protocols->{byid
}->{$proto}));
1018 $source = undef if $source && $source eq '-';
1019 $dest = undef if $dest && $dest eq '-';
1021 $dport = undef if $dport && $dport eq '-';
1022 $sport = undef if $sport && $sport eq '-';
1024 my $nbsource = undef;
1027 $nbsource = parse_address_list
($source) if $source;
1028 $nbdest = parse_address_list
($dest) if $dest;
1037 nbsource
=> $nbsource,
1045 foreach my $templ (@$macro) {
1047 foreach my $k (keys %$templ) {
1048 my $v = $templ->{$k};
1049 if ($v eq 'PARAM') {
1051 } elsif ($v eq 'DEST') {
1052 $v = $param->{dest
};
1053 } elsif ($v eq 'SOURCE') {
1054 $v = $param->{source
};
1057 die "missing parameter '$k' in macro '$macro_name'\n" if !defined($v);
1060 push @$rules, $rule;
1063 push @$rules, $param;
1066 foreach my $rule (@$rules) {
1067 $rule->{nbdport
} = parse_port_name_number_or_range
($rule->{dport
})
1068 if defined($rule->{dport
});
1069 $rule->{nbsport
} = parse_port_name_number_or_range
($rule->{sport
})
1070 if defined($rule->{sport
});
1076 sub parse_fw_option
{
1081 if ($line =~ m/^(enable|macfilter):\s*(0|1)\s*$/i) {
1084 } elsif ($line =~ m/^(policy-(in|out)):\s*(ACCEPT|DROP|REJECT)\s*$/i) {
1089 die "can't parse option '$line'\n"
1092 return ($opt, $value);
1095 sub parse_vm_fw_rules
{
1096 my ($filename, $fh) = @_;
1098 my $res = { in => [], out
=> [], options
=> {}};
1102 while (defined(my $line = <$fh>)) {
1103 next if $line =~ m/^#/;
1104 next if $line =~ m/^\s*$/;
1106 my $linenr = $fh->input_line_number();
1107 my $prefix = "$filename (line $linenr)";
1109 if ($line =~ m/^\[(\S+)\]\s*$/i) {
1111 warn "$prefix: ignore unknown section '$section'\n" if !$res->{$section};
1115 warn "$prefix: skip line - no section";
1119 next if !$res->{$section}; # skip undefined section
1121 if ($section eq 'options') {
1123 my ($opt, $value) = parse_fw_option
($line);
1124 $res->{options
}->{$opt} = $value;
1126 warn "$prefix: $@" if $@;
1131 eval { $rules = parse_fw_rule
($line, 1, 1); };
1133 warn "$prefix: $err";
1137 push @{$res->{$section}}, @$rules;
1143 sub parse_host_fw_rules
{
1144 my ($filename, $fh) = @_;
1146 my $res = { in => [], out
=> [] };
1150 while (defined(my $line = <$fh>)) {
1151 next if $line =~ m/^#/;
1152 next if $line =~ m/^\s*$/;
1154 my $linenr = $fh->input_line_number();
1155 my $prefix = "$filename (line $linenr)";
1157 if ($line =~ m/^\[(in|out)\]\s*$/i) {
1162 warn "$prefix: skip line - no section";
1167 eval { $rules = parse_fw_rule
($line, 1, 1); };
1169 warn "$prefix: $err";
1173 push @{$res->{$section}}, @$rules;
1179 sub parse_group_fw_rules
{
1180 my ($filename, $fh) = @_;
1185 my $res = { in => [], out
=> [] };
1187 while (defined(my $line = <$fh>)) {
1188 next if $line =~ m/^#/;
1189 next if $line =~ m/^\s*$/;
1191 my $linenr = $fh->input_line_number();
1192 my $prefix = "$filename (line $linenr)";
1194 if ($line =~ m/^\[(in|out):(\S+)\]\s*$/i) {
1199 if (!$section || !$group) {
1200 warn "$prefix: skip line - no section";
1205 eval { $rules = parse_fw_rule
($line, 0, 0); };
1207 warn "$prefix: $err";
1211 push @{$res->{$group}->{$section}}, @$rules;
1218 my ($code, @param) = @_;
1222 my $res = lock_file
($pve_fw_lock_filename, $timeout, $code, @param);
1229 sub read_local_vm_config
{
1235 my $list = PVE
::QemuServer
::config_list
();
1237 foreach my $vmid (keys %$list) {
1238 my $cfspath = PVE
::QemuServer
::cfs_config_path
($vmid);
1239 if (my $conf = PVE
::Cluster
::cfs_read_file
($cfspath)) {
1240 $qemu->{$vmid} = $conf;
1244 my $vmdata = { openvz
=> $openvz, qemu
=> $qemu };
1249 sub read_vm_firewall_rules
{
1252 foreach my $vmid (keys %{$vmdata->{qemu
}}, keys %{$vmdata->{openvz
}}) {
1253 my $filename = "/etc/pve/firewall/$vmid.fw";
1254 my $fh = IO
::File-
>new($filename, O_RDONLY
);
1257 $rules->{$vmid} = parse_vm_fw_rules
($filename, $fh);
1263 sub generate_std_chains
{
1266 foreach my $chain (keys %$pve_std_chains) {
1267 ruleset_create_chain
($ruleset, $chain);
1268 foreach my $rule (@{$pve_std_chains->{$chain}}) {
1270 ruleset_generate_rule
($ruleset, $chain, $rule);
1272 ruleset_addrule
($ruleset, $chain, $rule);
1279 my $vmdata = read_local_vm_config
();
1280 my $rules = read_vm_firewall_rules
($vmdata);
1282 my $group_rules = {};
1283 my $filename = "/etc/pve/firewall/groups.fw";
1284 if (my $fh = IO
::File-
>new($filename, O_RDONLY
)) {
1285 $group_rules = parse_group_fw_rules
($filename, $fh);
1288 #print Dumper($rules);
1292 ruleset_create_chain
($ruleset, "PVEFW-INPUT");
1293 ruleset_create_chain
($ruleset, "PVEFW-OUTPUT");
1294 ruleset_create_chain
($ruleset, "PVEFW-FORWARD");
1296 generate_std_chains
($ruleset);
1298 my $enable_hostfw = 0;
1299 $filename = "/etc/pve/local/host.fw";
1300 if (my $fh = IO
::File-
>new($filename, O_RDONLY
)) {
1301 my $host_rules = parse_host_fw_rules
($filename, $fh);
1305 enablehostfw
($ruleset, $host_rules, $group_rules);
1308 # generate firewall rules for QEMU VMs
1309 foreach my $vmid (keys %{$vmdata->{qemu
}}) {
1310 my $conf = $vmdata->{qemu
}->{$vmid};
1311 my $vmfw_conf = $rules->{$vmid};
1312 next if !$vmfw_conf;
1313 next if defined($vmfw_conf->{options
}->{enable
}) && ($vmfw_conf->{options
}->{enable
} == 0);
1315 foreach my $netid (keys %$conf) {
1316 next if $netid !~ m/^net(\d+)$/;
1317 my $net = PVE
::QemuServer
::parse_net
($conf->{$netid});
1319 my $iface = "tap${vmid}i$1";
1321 my $bridge = $net->{bridge
};
1322 next if !$bridge; # fixme: ?
1324 $bridge .= "v$net->{tag}" if $net->{tag
};
1327 generate_bridge_chains
($ruleset, $bridge);
1329 my $macaddr = $net->{macaddr
};
1330 generate_tap_rules_direction
($ruleset, $group_rules, $iface, $netid, $macaddr, $vmfw_conf, $bridge, 'IN');
1331 generate_tap_rules_direction
($ruleset, $group_rules, $iface, $netid, $macaddr, $vmfw_conf, $bridge, 'OUT');
1335 if ($enable_hostfw) {
1336 # allow traffic from lo (ourself)
1337 ruleset_addrule
($ruleset, "PVEFW-INPUT", "-i lo -j ACCEPT");
1343 sub get_ruleset_status
{
1344 my ($ruleset, $verbose) = @_;
1346 my $active_chains = iptables_get_chains
();
1348 my $statushash = {};
1350 foreach my $chain (sort keys %$ruleset) {
1351 my $digest = Digest
::SHA-
>new('sha1');
1352 foreach my $cmd (@{$ruleset->{$chain}}) {
1353 $digest->add("$cmd\n");
1355 my $sig = $digest->b64digest;
1356 $statushash->{$chain}->{sig
} = $sig;
1358 my $oldsig = $active_chains->{$chain};
1359 if (!defined($oldsig)) {
1360 $statushash->{$chain}->{action
} = 'create';
1362 if ($oldsig eq $sig) {
1363 $statushash->{$chain}->{action
} = 'exists';
1365 $statushash->{$chain}->{action
} = 'update';
1368 print "$statushash->{$chain}->{action} $chain ($sig)\n" if $verbose;
1369 foreach my $cmd (@{$ruleset->{$chain}}) {
1370 print "\t$cmd\n" if $verbose;
1374 foreach my $chain (sort keys %$active_chains) {
1375 if (!defined($ruleset->{$chain})) {
1376 my $sig = $active_chains->{$chain};
1377 $statushash->{$chain}->{action
} = 'delete';
1378 $statushash->{$chain}->{sig
} = $sig;
1379 print "delete $chain ($sig)\n" if $verbose;
1389 get_ruleset_status
($ruleset, 1);
1392 sub print_sig_rule
{
1393 my ($chain, $sig) = @_;
1395 # We just use this to store a SHA1 checksum used to detect changes
1396 return "-A $chain -m comment --comment \"PVESIG:$sig\"\n";
1400 my ($ruleset, $verbose) = @_;
1402 enable_bridge_firewall();
1404 my $cmdlist = "*filter
\n"; # we pass this to iptables-restore;
1406 my $statushash = get_ruleset_status($ruleset, $verbose);
1408 # create missing chains first
1409 foreach my $chain (sort keys %$ruleset) {
1410 my $stat = $statushash->{$chain};
1411 die "internal error
" if !$stat;
1412 next if $stat->{action} ne 'create';
1414 $cmdlist .= ":$chain - [0:0]\n";
1417 my $rule = "INPUT
-j PVEFW-INPUT
";
1418 if (!PVE::Firewall::iptables_rule_exist($rule)) {
1419 $cmdlist .= "-A
$rule\n";
1421 $rule = "OUTPUT
-j PVEFW-OUTPUT
";
1422 if (!PVE::Firewall::iptables_rule_exist($rule)) {
1423 $cmdlist .= "-A
$rule\n";
1426 $rule = "FORWARD
-j PVEFW-FORWARD
";
1427 if (!PVE::Firewall::iptables_rule_exist($rule)) {
1428 $cmdlist .= "-A
$rule\n";
1431 foreach my $chain (sort keys %$ruleset) {
1432 my $stat = $statushash->{$chain};
1433 die "internal error
" if !$stat;
1435 if ($stat->{action} eq 'update' || $stat->{action} eq 'create') {
1436 $cmdlist .= "-F
$chain\n";
1437 foreach my $cmd (@{$ruleset->{$chain}}) {
1438 $cmdlist .= "$cmd\n";
1440 $cmdlist .= print_sig_rule($chain, $stat->{sig});
1441 } elsif ($stat->{action} eq 'delete') {
1442 die "internal error
"; # this should not happen
1443 } elsif ($stat->{action} eq 'exists') {
1446 die "internal error
- unknown status
'$stat->{action}'";
1450 foreach my $chain (keys %$statushash) {
1451 next if $statushash->{$chain}->{action} ne 'delete';
1452 $cmdlist .= "-F
$chain\n";
1454 foreach my $chain (keys %$statushash) {
1455 next if $statushash->{$chain}->{action} ne 'delete';
1456 next if $chain eq 'PVEFW-INPUT';
1457 next if $chain eq 'PVEFW-OUTPUT';
1458 next if $chain eq 'PVEFW-FORWARD';
1459 $cmdlist .= "-X
$chain\n";
1462 $cmdlist .= "COMMIT
\n";
1464 print $cmdlist if $verbose;
1466 iptables_restore_cmdlist($cmdlist);
1468 # test: re-read status and check if everything is up to date
1469 $statushash = get_ruleset_status($ruleset);
1472 foreach my $chain (sort keys %$ruleset) {
1473 my $stat = $statushash->{$chain};
1474 if ($stat->{action} ne 'exists') {
1475 warn "unable to update chain
'$chain'\n";
1480 die "unable to apply firewall changes
\n" if $errors;