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
=> 'echo-request' },
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
=> 'echo-request' },
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 },
420 'PVEFW-logflags' => [
421 # same as shorewall logflags action. (fixme: enable/disable logging)
422 "-j LOG --log-prefix \"logflags-dropped:\" --log-level 4 --log-ip-options",
425 'PVEFW-tcpflags' => [
426 # same as shorewall tcpflags action.
427 # Packets arriving on this interface are checked for som illegal combinations of TCP flags
428 "-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG FIN,PSH,URG -g PVEFW-logflags",
429 "-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG NONE -g PVEFW-logflags",
430 "-p tcp -m tcp --tcp-flags SYN,RST SYN,RST -g PVEFW-logflags",
431 "-p tcp -m tcp --tcp-flags FIN,SYN FIN,SYN -g PVEFW-logflags",
432 "-p tcp -m tcp --sport 0 --tcp-flags FIN,SYN,RST,ACK SYN -g PVEFW-logflags",
434 'PVEFW-smurflog' => [
435 # same as shorewall smurflog. (fixme: enable/disable logging)
436 "-j LOG --log-prefix \"smurfs-dropped\" --log-level 4",
440 # same as shorewall smurfs action
441 # Filter packets for smurfs (packets with a broadcast address as the source).
442 "-s 0.0.0.0/32 -j RETURN",
443 "-m addrtype --src-type BROADCAST -g PVEFW-smurflog",
444 "-s 224.0.0.0/4 -g PVEFW-smurflog",
448 # iptables -p icmp -h
449 my $icmp_type_names = {
452 'destination-unreachable' => 1,
453 'network-unreachable' => 1,
454 'host-unreachable' => 1,
455 'protocol-unreachable' => 1,
456 'port-unreachable' => 1,
457 'fragmentation-needed' => 1,
458 'source-route-failed' => 1,
459 'network-unknown' => 1,
461 'network-prohibited' => 1,
462 'host-prohibited' => 1,
463 'TOS-network-unreachable' => 1,
464 'TOS-host-unreachable' => 1,
465 'communication-prohibited' => 1,
466 'host-precedence-violation' => 1,
467 'precedence-cutoff' => 1,
468 'source-quench' => 1,
470 'network-redirect' => 1,
471 'host-redirect' => 1,
472 'TOS-network-redirect' => 1,
473 'TOS-host-redirect' => 1,
475 'router-advertisement' => 1,
476 'router-solicitation' => 1,
477 'time-exceeded' => 1,
478 'ttl-zero-during-transit' => 1,
479 'ttl-zero-during-reassembly' => 1,
480 'parameter-problem' => 1,
481 'ip-header-bad' => 1,
482 'required-option-missing' => 1,
483 'timestamp-request' => 1,
484 'timestamp-reply' => 1,
485 'address-mask-request' => 1,
486 'address-mask-reply' => 1,
489 sub get_firewall_macros
{
491 return $pve_fw_parsed_macros if $pve_fw_parsed_macros;
493 $pve_fw_parsed_macros = {};
495 foreach my $k (keys %$pve_fw_macros) {
498 my $macro = $pve_fw_macros->{$k};
499 $pve_fw_preferred_macro_names->{$name} = $k;
500 $pve_fw_parsed_macros->{$name} = $macro;
503 return $pve_fw_parsed_macros;
508 sub get_etc_services
{
510 return $etc_services if $etc_services;
512 my $filename = "/etc/services";
514 my $fh = IO
::File-
>new($filename, O_RDONLY
);
516 warn "unable to read '$filename' - $!\n";
522 while (my $line = <$fh>) {
524 next if $line =~m/^#/;
525 next if ($line =~m/^\s*$/);
527 if ($line =~ m!^(\S+)\s+(\S+)/(tcp|udp).*$!) {
528 $services->{byid
}->{$2}->{name
} = $1;
529 $services->{byid
}->{$2}->{port
} = $2;
530 $services->{byid
}->{$2}->{$3} = 1;
531 $services->{byname
}->{$1} = $services->{byid
}->{$2};
537 $etc_services = $services;
540 return $etc_services;
545 sub get_etc_protocols
{
546 return $etc_protocols if $etc_protocols;
548 my $filename = "/etc/protocols";
550 my $fh = IO
::File-
>new($filename, O_RDONLY
);
552 warn "unable to read '$filename' - $!\n";
558 while (my $line = <$fh>) {
560 next if $line =~m/^#/;
561 next if ($line =~m/^\s*$/);
563 if ($line =~ m!^(\S+)\s+(\d+)\s+.*$!) {
564 $protocols->{byid
}->{$2}->{name
} = $1;
565 $protocols->{byname
}->{$1} = $protocols->{byid
}->{$2};
571 $etc_protocols = $protocols;
573 return $etc_protocols;
576 sub parse_address_list
{
580 foreach my $aor (split(/,/, $str)) {
581 if (!Net
::IP-
>new($aor)) {
582 my $err = Net
::IP
::Error
();
583 die "invalid IP address: $err\n";
591 sub parse_port_name_number_or_range
{
594 my $services = PVE
::Firewall
::get_etc_services
();
596 foreach my $item (split(/,/, $str)) {
600 foreach my $pon (split(':', $item, 2)) {
601 $pon = $services->{byname
}->{$pon}->{port
} if $services->{byname
}->{$pon}->{port
};
602 if ($pon =~ m/^\d+$/){
603 die "invalid port '$pon'\n" if $pon < 0 && $pon > 65535;
604 die "port '$pon' must be bigger than port '$oldpon' \n" if $oldpon && ($pon < $oldpon);
607 die "invalid port $services->{byname}->{$pon}\n" if !$services->{byname
}->{$pon};
615 my $bridge_firewall_enabled = 0;
617 sub enable_bridge_firewall
{
619 return if $bridge_firewall_enabled; # only once
621 system("echo 1 > /proc/sys/net/bridge/bridge-nf-call-iptables");
622 system("echo 1 > /proc/sys/net/bridge/bridge-nf-call-ip6tables");
624 $bridge_firewall_enabled = 1;
627 my $rule_format = "%-15s %-30s %-30s %-15s %-15s %-15s\n";
632 run_command
("/sbin/iptables $cmd", outfunc
=> sub {}, errfunc
=> sub {});
635 sub iptables_restore_cmdlist
{
638 run_command
("/sbin/iptables-restore -n", input
=> $cmdlist);
641 sub iptables_get_chains
{
645 # check what chains we want to track
646 my $is_pvefw_chain = sub {
649 return 1 if $name =~ m/^PVEFW-\S+$/;
651 return 1 if $name =~ m/^tap\d+i\d+-(:?IN|OUT)$/;
652 return 1 if $name =~ m/^vmbr\d+-(:?FW|IN|OUT)$/;
653 return 1 if $name =~ m/^GROUP-(:?[^\s\-]+)-(:?IN|OUT)$/;
663 return if $line =~ m/^#/;
664 return if $line =~ m/^\s*$/;
666 if ($line =~ m/^\*(\S+)$/) {
671 return if $table ne 'filter';
673 if ($line =~ m/^:(\S+)\s/) {
675 return if !&$is_pvefw_chain($chain);
676 $res->{$chain} = "unknown";
677 } elsif ($line =~ m/^-A\s+(\S+)\s.*--comment\s+\"PVESIG:(\S+)\"/) {
678 my ($chain, $sig) = ($1, $2);
679 return if !&$is_pvefw_chain($chain);
680 $res->{$chain} = $sig;
682 # simply ignore the rest
687 run_command
("/sbin/iptables-save", outfunc
=> $parser);
692 sub iptables_chain_exist
{
696 iptables
("-n --list $chain");
703 sub iptables_rule_exist
{
707 iptables
("-C $rule");
714 sub ruleset_generate_rule
{
715 my ($ruleset, $chain, $rule, $actions, $goto) = @_;
717 return if $rule->{disable
};
721 $cmd .= " -m iprange --src-range" if $rule->{nbsource
} && $rule->{nbsource
} > 1;
722 $cmd .= " -s $rule->{source}" if $rule->{source
};
723 $cmd .= " -m iprange --dst-range" if $rule->{nbdest
} && $rule->{nbdest
} > 1;
724 $cmd .= " -d $rule->{dest}" if $rule->{dest
};
725 $cmd .= " -p $rule->{proto}" if $rule->{proto
};
728 $multiport++ if $rule->{nbdport
} && ($rule->{nbdport
} > 1);
729 $multiport++ if $rule->{nbsport
} && ($rule->{nbsport
} > 1);
731 $cmd .= " --match multiport" if $multiport;
733 die "multiport: option '--sports' cannot be used together with '--dports'\n"
734 if ($multiport == 2) && ($rule->{dport
} ne $rule->{sport
});
736 if ($rule->{dport
}) {
737 if ($rule->{proto
} && $rule->{proto
} eq 'icmp') {
738 # Note: we use dport to store --icmp-type
739 die "unknown icmp-type '$rule->{dport}'\n" if !defined($icmp_type_names->{$rule->{dport
}});
740 $cmd .= " -m icmp --icmp-type $rule->{dport}";
742 if ($rule->{nbdport
} && $rule->{nbdport
} > 1) {
743 if ($multiport == 2) {
744 $cmd .= " --ports $rule->{dport}";
746 $cmd .= " --dports $rule->{dport}";
749 $cmd .= " --dport $rule->{dport}";
754 if ($rule->{sport
}) {
755 if ($rule->{nbsport
} && $rule->{nbsport
} > 1) {
756 $cmd .= " --sports $rule->{sport}" if $multiport != 2;
758 $cmd .= " --sport $rule->{sport}";
762 $cmd .= " -m addrtype --dst-type $rule->{dsttype}" if $rule->{dsttype
};
764 if (my $action = $rule->{action
}) {
765 $action = $actions->{$action} if defined($actions->{$action});
766 $goto = 1 if !defined($goto) && $action eq 'PVEFW-SET-ACCEPT-MARK';
767 $cmd .= $goto ?
" -g $action" : " -j $action";
770 ruleset_addrule
($ruleset, $chain, $cmd) if $cmd;
773 sub ruleset_create_chain
{
774 my ($ruleset, $chain) = @_;
776 die "Invalid chain name '$chain' (28 char max)\n" if length($chain) > 28;
778 die "chain '$chain' already exists\n" if $ruleset->{$chain};
780 $ruleset->{$chain} = [];
783 sub ruleset_chain_exist
{
784 my ($ruleset, $chain) = @_;
786 return $ruleset->{$chain} ?
1 : undef;
789 sub ruleset_addrule
{
790 my ($ruleset, $chain, $rule) = @_;
792 die "no such chain '$chain'\n" if !$ruleset->{$chain};
794 push @{$ruleset->{$chain}}, "-A $chain $rule";
797 sub ruleset_insertrule
{
798 my ($ruleset, $chain, $rule) = @_;
800 die "no such chain '$chain'\n" if !$ruleset->{$chain};
802 unshift @{$ruleset->{$chain}}, "-A $chain $rule";
805 sub generate_bridge_chains
{
806 my ($ruleset, $bridge) = @_;
808 if (!ruleset_chain_exist
($ruleset, "PVEFW-FORWARD")){
809 ruleset_create_chain
($ruleset, "PVEFW-FORWARD");
810 ruleset_addrule
($ruleset, "PVEFW-FORWARD", "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT");
813 if (!ruleset_chain_exist
($ruleset, "$bridge-FW")) {
814 ruleset_create_chain
($ruleset, "$bridge-FW");
815 ruleset_addrule
($ruleset, "PVEFW-FORWARD", "-o $bridge -m physdev --physdev-is-bridged -j $bridge-FW");
816 ruleset_addrule
($ruleset, "PVEFW-FORWARD", "-i $bridge -m physdev --physdev-is-bridged -j $bridge-FW");
817 ruleset_addrule
($ruleset, "PVEFW-FORWARD", "-o $bridge -j DROP"); # disable interbridge routing
818 ruleset_addrule
($ruleset, "PVEFW-FORWARD", "-i $bridge -j DROP"); # disable interbridge routing
821 if (!ruleset_chain_exist
($ruleset, "$bridge-OUT")) {
822 ruleset_create_chain
($ruleset, "$bridge-OUT");
823 ruleset_addrule
($ruleset, "$bridge-FW", "-m physdev --physdev-is-bridged --physdev-is-in -j $bridge-OUT");
826 if (!ruleset_chain_exist
($ruleset, "$bridge-IN")) {
827 ruleset_create_chain
($ruleset, "$bridge-IN");
828 ruleset_addrule
($ruleset, "$bridge-FW", "-m physdev --physdev-is-bridged --physdev-is-out -j $bridge-IN");
829 ruleset_addrule
($ruleset, "$bridge-FW", "-m mark --mark 1 -j ACCEPT");
830 # accept traffic to unmanaged bridge ports
831 ruleset_addrule
($ruleset, "$bridge-FW", "-m physdev --physdev-is-bridged --physdev-is-out -j ACCEPT ");
835 sub generate_tap_rules_direction
{
836 my ($ruleset, $group_rules, $iface, $netid, $macaddr, $vmfw_conf, $bridge, $direction) = @_;
838 my $rules = $vmfw_conf->{lc($direction)};
839 my $options = $vmfw_conf->{options
};
841 my $tapchain = "$iface-$direction";
843 ruleset_create_chain
($ruleset, $tapchain);
845 if (!(defined($options->{nosmurfs
}) && $options->{nosmurfs
} == 0)) {
846 ruleset_addrule
($ruleset, $tapchain, "-m conntrack --ctstate INVALID,NEW -j PVEFW-smurfs");
849 if (!(defined($options->{dhcp
}) && $options->{dhcp
} == 0)) {
850 ruleset_addrule
($ruleset, $tapchain, "-p udp -m udp --dport 67:68 -j ACCEPT");
853 if ($options->{tcpflags
}) {
854 ruleset_addrule
($ruleset, $tapchain, "-p tcp -j PVEFW-tcpflags");
857 ruleset_addrule
($ruleset, $tapchain, "-m conntrack --ctstate INVALID -j DROP");
858 ruleset_addrule
($ruleset, $tapchain, "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT");
860 if ($direction eq 'OUT' && defined($macaddr) &&
861 !(defined($options->{macfilter
}) && $options->{macfilter
} == 0)) {
862 ruleset_addrule
($ruleset, $tapchain, "-m mac ! --mac-source $macaddr -j DROP");
866 foreach my $rule (@$rules) {
867 next if $rule->{iface
} && $rule->{iface
} ne $netid;
868 # we go to $bridge-IN if accept in out rules
869 if($rule->{action
} =~ m/^(GROUP-(\S+))$/){
870 $rule->{action
} .= "-$direction";
871 # generate empty group rule if don't exist
872 if(!ruleset_chain_exist
($ruleset, $rule->{action
})){
873 generate_group_rules
($ruleset, $group_rules, $2);
875 ruleset_generate_rule
($ruleset, $tapchain, $rule);
876 ruleset_addrule
($ruleset, $tapchain, "-m mark --mark 1 -j RETURN")
877 if $direction eq 'OUT';
879 if ($direction eq 'OUT') {
880 ruleset_generate_rule
($ruleset, $tapchain, $rule,
881 { ACCEPT
=> "PVEFW-SET-ACCEPT-MARK", REJECT
=> "PVEFW-reject" });
883 ruleset_generate_rule
($ruleset, $tapchain, $rule, { REJECT
=> "PVEFW-reject" });
892 if ($direction eq 'OUT') {
893 $policy = $options->{'policy-out'} || 'ACCEPT'; # allow everything by default
895 $policy = $options->{'policy-in'} || 'DROP'; # allow everything by default
898 if ($policy eq 'ACCEPT') {
899 if ($direction eq 'OUT') {
900 ruleset_addrule
($ruleset, $tapchain, "-g PVEFW-SET-ACCEPT-MARK");
902 ruleset_addrule
($ruleset, $tapchain, "-j ACCEPT");
904 } elsif ($policy eq 'DROP') {
905 ruleset_addrule
($ruleset, $tapchain, "-j PVEFW-Drop");
906 ruleset_addrule
($ruleset, $tapchain, "-j LOG --log-prefix \"$tapchain-dropped: \" --log-level 4");
907 ruleset_addrule
($ruleset, $tapchain, "-j DROP");
908 } elsif ($policy eq 'REJECT') {
909 ruleset_addrule
($ruleset, $tapchain, "-j PVEFW-Reject");
910 ruleset_addrule
($ruleset, $tapchain, "-j LOG --log-prefix \"$tapchain-reject: \" --log-level 4");
911 ruleset_addrule
($ruleset, $tapchain, "-g PVEFW-reject");
914 die "internal error: unknown policy '$policy'";
917 # plug the tap chain to bridge chain
918 my $physdevdirection = $direction eq 'IN' ?
"out" : "in";
919 my $rule = "-m physdev --physdev-$physdevdirection $iface --physdev-is-bridged -j $tapchain";
920 ruleset_insertrule
($ruleset, "$bridge-$direction", $rule);
922 if ($direction eq 'OUT'){
923 # add tap->host rules
924 my $rule = "-m physdev --physdev-$physdevdirection $iface -j $tapchain";
925 ruleset_addrule
($ruleset, "PVEFW-INPUT", $rule);
930 my ($ruleset, $rules, $group_rules) = @_;
932 # fixme: allow security groups
934 # host inbound firewall
935 my $chain = "PVEFW-HOST-IN";
936 ruleset_create_chain
($ruleset, $chain);
938 ruleset_addrule
($ruleset, $chain, "-m conntrack --ctstate INVALID -j DROP");
939 ruleset_addrule
($ruleset, $chain, "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT");
940 ruleset_addrule
($ruleset, $chain, "-i lo -j ACCEPT");
941 ruleset_addrule
($ruleset, $chain, "-m addrtype --dst-type MULTICAST -j ACCEPT");
942 ruleset_addrule
($ruleset, $chain, "-p udp -m conntrack --ctstate NEW -m multiport --dports 5404,5405 -j ACCEPT");
943 ruleset_addrule
($ruleset, $chain, "-p udp -m udp --dport 9000 -j ACCEPT"); #corosync
946 foreach my $rule (@{$rules->{in}}) {
947 # we use RETURN because we need to check also tap rules
948 ruleset_generate_rule
($ruleset, $chain, $rule, { ACCEPT
=> 'RETURN', REJECT
=> "PVEFW-reject" });
952 ruleset_addrule
($ruleset, $chain, "-j LOG --log-prefix \"kvmhost-IN dropped: \" --log-level 4");
953 ruleset_addrule
($ruleset, $chain, "-j DROP");
955 # host outbound firewall
956 $chain = "PVEFW-HOST-OUT";
957 ruleset_create_chain
($ruleset, $chain);
959 ruleset_addrule
($ruleset, $chain, "-m conntrack --ctstate INVALID -j DROP");
960 ruleset_addrule
($ruleset, $chain, "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT");
961 ruleset_addrule
($ruleset, $chain, "-o lo -j ACCEPT");
962 ruleset_addrule
($ruleset, $chain, "-m addrtype --dst-type MULTICAST -j ACCEPT");
963 ruleset_addrule
($ruleset, $chain, "-p udp -m conntrack --ctstate NEW -m multiport --dports 5404,5405 -j ACCEPT");
964 ruleset_addrule
($ruleset, $chain, "-p udp -m udp --dport 9000 -j ACCEPT"); #corosync
967 foreach my $rule (@{$rules->{out
}}) {
968 # we use RETURN because we need to check also tap rules
969 ruleset_generate_rule
($ruleset, $chain, $rule, { ACCEPT
=> 'RETURN', REJECT
=> "PVEFW-reject" });
973 ruleset_addrule
($ruleset, $chain, "-j LOG --log-prefix \"kvmhost-OUT dropped: \" --log-level 4");
974 ruleset_addrule
($ruleset, $chain, "-j DROP");
976 ruleset_addrule
($ruleset, "PVEFW-OUTPUT", "-j PVEFW-HOST-OUT");
977 ruleset_addrule
($ruleset, "PVEFW-INPUT", "-j PVEFW-HOST-IN");
980 sub generate_group_rules
{
981 my ($ruleset, $group_rules, $group) = @_;
983 my $rules = $group_rules->{$group};
985 die "no such security group '$group'\n" if !$rules;
987 my $chain = "GROUP-${group}-IN";
989 ruleset_create_chain
($ruleset, $chain);
992 foreach my $rule (@{$rules->{in}}) {
993 ruleset_generate_rule
($ruleset, $chain, $rule, { REJECT
=> "PVEFW-reject" });
997 $chain = "GROUP-${group}-OUT";
999 ruleset_create_chain
($ruleset, $chain);
1000 ruleset_addrule
($ruleset, $chain, "-j MARK --set-mark 0"); # clear mark
1002 if ($rules->{out
}) {
1003 foreach my $rule (@{$rules->{out
}}) {
1004 # we use PVEFW-SET-ACCEPT-MARK (Instead of ACCEPT) because we need to
1005 # check also other tap rules later
1006 ruleset_generate_rule
($ruleset, $chain, $rule,
1007 { ACCEPT
=> 'PVEFW-SET-ACCEPT-MARK', REJECT
=> "PVEFW-reject" });
1013 my $valid_netdev_names = {};
1014 for (my $i = 0; $i < $MAX_NETS; $i++) {
1015 $valid_netdev_names->{"net$i"} = 1;
1019 my ($line, $need_iface, $allow_groups) = @_;
1021 my $macros = get_firewall_macros
();
1022 my $protocols = get_etc_protocols
();
1024 my ($action, $iface, $source, $dest, $proto, $dport, $sport);
1026 # we can add single line comments to the end of the rule
1027 my $comment = $1 if $line =~ s/#\s*(.*?)\s*$//;
1029 # we can disable a rule when prefixed with '|'
1030 my $disable = 1 if $line =~ s/^\|//;
1032 my @data = split(/\s+/, $line);
1033 my $expected_elements = $need_iface ?
7 : 6;
1035 die "wrong number of rule elements\n" if scalar(@data) > $expected_elements;
1038 ($action, $iface, $source, $dest, $proto, $dport, $sport) = @data
1040 ($action, $source, $dest, $proto, $dport, $sport) = @data;
1043 die "incomplete rule\n" if !$action;
1048 if ($action =~ m/^(ACCEPT|DROP|REJECT)$/) {
1050 } elsif ($allow_groups && $action =~ m/^GROUP-(:?\S+)$/) {
1052 } elsif ($action =~ m/^(\S+)\((ACCEPT|DROP|REJECT)\)$/) {
1053 ($macro_name, $action) = ($1, $2);
1054 my $lc_macro_name = lc($macro_name);
1055 my $preferred_name = $pve_fw_preferred_macro_names->{$lc_macro_name};
1056 $macro_name = $preferred_name if $preferred_name;
1057 $macro = $macros->{$lc_macro_name};
1058 die "unknown macro '$macro_name'\n" if !$macro;
1060 die "unknown action '$action'\n";
1064 $iface = undef if $iface && $iface eq '-';
1065 die "unknown interface '$iface'\n"
1066 if defined($iface) && !$valid_netdev_names->{$iface};
1069 $proto = undef if $proto && $proto eq '-';
1070 die "unknown protokol '$proto'\n" if $proto &&
1071 !(defined($protocols->{byname
}->{$proto}) ||
1072 defined($protocols->{byid
}->{$proto}));
1074 $source = undef if $source && $source eq '-';
1075 $dest = undef if $dest && $dest eq '-';
1077 $dport = undef if $dport && $dport eq '-';
1078 $sport = undef if $sport && $sport eq '-';
1080 my $nbsource = undef;
1083 $nbsource = parse_address_list
($source) if $source;
1084 $nbdest = parse_address_list
($dest) if $dest;
1089 disable
=> $disable,
1090 comment
=> $comment,
1095 nbsource
=> $nbsource,
1103 foreach my $templ (@$macro) {
1105 my $param_used = {};
1106 foreach my $k (keys %$templ) {
1107 my $v = $templ->{$k};
1108 if ($v eq 'PARAM') {
1110 $param_used->{$k} = 1;
1111 } elsif ($v eq 'DEST') {
1112 $v = $param->{dest
};
1113 $param_used->{dest
} = 1;
1114 } elsif ($v eq 'SOURCE') {
1115 $v = $param->{source
};
1116 $param_used->{source
} = 1;
1119 die "missing parameter '$k' in macro '$macro_name'\n" if !defined($v);
1122 foreach my $k (keys %$param) {
1123 next if !defined($param->{$k});
1124 next if $param_used->{$k};
1125 if (defined($rule->{$k})) {
1126 die "parameter '$k' already define in macro (value = '$rule->{$k}')\n"
1127 if $rule->{$k} ne $param->{$k};
1129 $rule->{$k} = $param->{$k};
1132 push @$rules, $rule;
1135 push @$rules, $param;
1138 foreach my $rule (@$rules) {
1139 $rule->{nbdport
} = parse_port_name_number_or_range
($rule->{dport
})
1140 if defined($rule->{dport
});
1141 $rule->{nbsport
} = parse_port_name_number_or_range
($rule->{sport
})
1142 if defined($rule->{sport
});
1148 sub parse_fw_option
{
1153 if ($line =~ m/^(enable|dhcp|macfilter|nosmurfs|tcpflags):\s*(0|1)\s*$/i) {
1156 } elsif ($line =~ m/^(policy-(in|out)):\s*(ACCEPT|DROP|REJECT)\s*$/i) {
1161 die "can't parse option '$line'\n"
1164 return ($opt, $value);
1167 sub parse_vm_fw_rules
{
1168 my ($filename, $fh) = @_;
1170 my $res = { in => [], out
=> [], options
=> {}};
1174 while (defined(my $line = <$fh>)) {
1175 next if $line =~ m/^#/;
1176 next if $line =~ m/^\s*$/;
1178 my $linenr = $fh->input_line_number();
1179 my $prefix = "$filename (line $linenr)";
1181 if ($line =~ m/^\[(\S+)\]\s*$/i) {
1183 warn "$prefix: ignore unknown section '$section'\n" if !$res->{$section};
1187 warn "$prefix: skip line - no section";
1191 next if !$res->{$section}; # skip undefined section
1193 if ($section eq 'options') {
1195 my ($opt, $value) = parse_fw_option
($line);
1196 $res->{options
}->{$opt} = $value;
1198 warn "$prefix: $@" if $@;
1203 eval { $rules = parse_fw_rule
($line, 1, 1); };
1205 warn "$prefix: $err";
1209 push @{$res->{$section}}, @$rules;
1215 sub parse_host_fw_rules
{
1216 my ($filename, $fh) = @_;
1218 my $res = { in => [], out
=> [] };
1222 while (defined(my $line = <$fh>)) {
1223 next if $line =~ m/^#/;
1224 next if $line =~ m/^\s*$/;
1226 my $linenr = $fh->input_line_number();
1227 my $prefix = "$filename (line $linenr)";
1229 if ($line =~ m/^\[(in|out)\]\s*$/i) {
1234 warn "$prefix: skip line - no section";
1239 eval { $rules = parse_fw_rule
($line, 1, 1); };
1241 warn "$prefix: $err";
1245 push @{$res->{$section}}, @$rules;
1251 sub parse_group_fw_rules
{
1252 my ($filename, $fh) = @_;
1257 my $res = { in => [], out
=> [] };
1259 while (defined(my $line = <$fh>)) {
1260 next if $line =~ m/^#/;
1261 next if $line =~ m/^\s*$/;
1263 my $linenr = $fh->input_line_number();
1264 my $prefix = "$filename (line $linenr)";
1266 if ($line =~ m/^\[(in|out):(\S+)\]\s*$/i) {
1271 if (!$section || !$group) {
1272 warn "$prefix: skip line - no section";
1277 eval { $rules = parse_fw_rule
($line, 0, 0); };
1279 warn "$prefix: $err";
1283 push @{$res->{$group}->{$section}}, @$rules;
1290 my ($code, @param) = @_;
1294 my $res = lock_file
($pve_fw_lock_filename, $timeout, $code, @param);
1301 sub read_local_vm_config
{
1307 my $list = PVE
::QemuServer
::config_list
();
1309 foreach my $vmid (keys %$list) {
1310 my $cfspath = PVE
::QemuServer
::cfs_config_path
($vmid);
1311 if (my $conf = PVE
::Cluster
::cfs_read_file
($cfspath)) {
1312 $qemu->{$vmid} = $conf;
1316 my $vmdata = { openvz
=> $openvz, qemu
=> $qemu };
1321 sub read_vm_firewall_rules
{
1324 foreach my $vmid (keys %{$vmdata->{qemu
}}, keys %{$vmdata->{openvz
}}) {
1325 my $filename = "/etc/pve/firewall/$vmid.fw";
1326 my $fh = IO
::File-
>new($filename, O_RDONLY
);
1329 $rules->{$vmid} = parse_vm_fw_rules
($filename, $fh);
1335 sub generate_std_chains
{
1338 foreach my $chain (keys %$pve_std_chains) {
1339 ruleset_create_chain
($ruleset, $chain);
1340 foreach my $rule (@{$pve_std_chains->{$chain}}) {
1342 ruleset_generate_rule
($ruleset, $chain, $rule);
1344 ruleset_addrule
($ruleset, $chain, $rule);
1351 my $vmdata = read_local_vm_config
();
1352 my $rules = read_vm_firewall_rules
($vmdata);
1354 my $group_rules = {};
1355 my $filename = "/etc/pve/firewall/groups.fw";
1356 if (my $fh = IO
::File-
>new($filename, O_RDONLY
)) {
1357 $group_rules = parse_group_fw_rules
($filename, $fh);
1360 #print Dumper($rules);
1364 ruleset_create_chain
($ruleset, "PVEFW-INPUT");
1365 ruleset_create_chain
($ruleset, "PVEFW-OUTPUT");
1366 ruleset_create_chain
($ruleset, "PVEFW-FORWARD");
1368 generate_std_chains
($ruleset);
1370 my $enable_hostfw = 0;
1371 $filename = "/etc/pve/local/host.fw";
1372 if (my $fh = IO
::File-
>new($filename, O_RDONLY
)) {
1373 my $host_rules = parse_host_fw_rules
($filename, $fh);
1377 enablehostfw
($ruleset, $host_rules, $group_rules);
1380 # generate firewall rules for QEMU VMs
1381 foreach my $vmid (keys %{$vmdata->{qemu
}}) {
1382 my $conf = $vmdata->{qemu
}->{$vmid};
1383 my $vmfw_conf = $rules->{$vmid};
1384 next if !$vmfw_conf;
1385 next if defined($vmfw_conf->{options
}->{enable
}) && ($vmfw_conf->{options
}->{enable
} == 0);
1387 foreach my $netid (keys %$conf) {
1388 next if $netid !~ m/^net(\d+)$/;
1389 my $net = PVE
::QemuServer
::parse_net
($conf->{$netid});
1391 my $iface = "tap${vmid}i$1";
1393 my $bridge = $net->{bridge
};
1394 next if !$bridge; # fixme: ?
1396 $bridge .= "v$net->{tag}" if $net->{tag
};
1399 generate_bridge_chains
($ruleset, $bridge);
1401 my $macaddr = $net->{macaddr
};
1402 generate_tap_rules_direction
($ruleset, $group_rules, $iface, $netid, $macaddr, $vmfw_conf, $bridge, 'IN');
1403 generate_tap_rules_direction
($ruleset, $group_rules, $iface, $netid, $macaddr, $vmfw_conf, $bridge, 'OUT');
1407 if ($enable_hostfw) {
1408 # allow traffic from lo (ourself)
1409 ruleset_addrule
($ruleset, "PVEFW-INPUT", "-i lo -j ACCEPT");
1415 sub get_ruleset_status
{
1416 my ($ruleset, $verbose) = @_;
1418 my $active_chains = iptables_get_chains
();
1420 my $statushash = {};
1422 foreach my $chain (sort keys %$ruleset) {
1423 my $digest = Digest
::SHA-
>new('sha1');
1424 foreach my $cmd (@{$ruleset->{$chain}}) {
1425 $digest->add("$cmd\n");
1427 my $sig = $digest->b64digest;
1428 $statushash->{$chain}->{sig
} = $sig;
1430 my $oldsig = $active_chains->{$chain};
1431 if (!defined($oldsig)) {
1432 $statushash->{$chain}->{action
} = 'create';
1434 if ($oldsig eq $sig) {
1435 $statushash->{$chain}->{action
} = 'exists';
1437 $statushash->{$chain}->{action
} = 'update';
1440 print "$statushash->{$chain}->{action} $chain ($sig)\n" if $verbose;
1441 foreach my $cmd (@{$ruleset->{$chain}}) {
1442 print "\t$cmd\n" if $verbose;
1446 foreach my $chain (sort keys %$active_chains) {
1447 if (!defined($ruleset->{$chain})) {
1448 my $sig = $active_chains->{$chain};
1449 $statushash->{$chain}->{action
} = 'delete';
1450 $statushash->{$chain}->{sig
} = $sig;
1451 print "delete $chain ($sig)\n" if $verbose;
1461 get_ruleset_status
($ruleset, 1);
1464 sub print_sig_rule
{
1465 my ($chain, $sig) = @_;
1467 # We just use this to store a SHA1 checksum used to detect changes
1468 return "-A $chain -m comment --comment \"PVESIG:$sig\"\n";
1472 my ($ruleset, $verbose) = @_;
1474 enable_bridge_firewall();
1476 my $cmdlist = "*filter
\n"; # we pass this to iptables-restore;
1478 my $statushash = get_ruleset_status($ruleset, $verbose);
1480 # create missing chains first
1481 foreach my $chain (sort keys %$ruleset) {
1482 my $stat = $statushash->{$chain};
1483 die "internal error
" if !$stat;
1484 next if $stat->{action} ne 'create';
1486 $cmdlist .= ":$chain - [0:0]\n";
1489 my $rule = "INPUT
-j PVEFW-INPUT
";
1490 if (!PVE::Firewall::iptables_rule_exist($rule)) {
1491 $cmdlist .= "-A
$rule\n";
1493 $rule = "OUTPUT
-j PVEFW-OUTPUT
";
1494 if (!PVE::Firewall::iptables_rule_exist($rule)) {
1495 $cmdlist .= "-A
$rule\n";
1498 $rule = "FORWARD
-j PVEFW-FORWARD
";
1499 if (!PVE::Firewall::iptables_rule_exist($rule)) {
1500 $cmdlist .= "-A
$rule\n";
1503 foreach my $chain (sort keys %$ruleset) {
1504 my $stat = $statushash->{$chain};
1505 die "internal error
" if !$stat;
1507 if ($stat->{action} eq 'update' || $stat->{action} eq 'create') {
1508 $cmdlist .= "-F
$chain\n";
1509 foreach my $cmd (@{$ruleset->{$chain}}) {
1510 $cmdlist .= "$cmd\n";
1512 $cmdlist .= print_sig_rule($chain, $stat->{sig});
1513 } elsif ($stat->{action} eq 'delete') {
1514 die "internal error
"; # this should not happen
1515 } elsif ($stat->{action} eq 'exists') {
1518 die "internal error
- unknown status
'$stat->{action}'";
1522 foreach my $chain (keys %$statushash) {
1523 next if $statushash->{$chain}->{action} ne 'delete';
1524 $cmdlist .= "-F
$chain\n";
1526 foreach my $chain (keys %$statushash) {
1527 next if $statushash->{$chain}->{action} ne 'delete';
1528 next if $chain eq 'PVEFW-INPUT';
1529 next if $chain eq 'PVEFW-OUTPUT';
1530 next if $chain eq 'PVEFW-FORWARD';
1531 $cmdlist .= "-X
$chain\n";
1534 $cmdlist .= "COMMIT
\n";
1536 print $cmdlist if $verbose;
1538 iptables_restore_cmdlist($cmdlist);
1540 # test: re-read status and check if everything is up to date
1541 $statushash = get_ruleset_status($ruleset);
1544 foreach my $chain (sort keys %$ruleset) {
1545 my $stat = $statushash->{$chain};
1546 if ($stat->{action} ne 'exists') {
1547 warn "unable to update chain
'$chain'\n";
1552 die "unable to apply firewall changes
\n" if $errors;