13 use PVE
::Tools
qw(run_command lock_file);
17 my $pve_fw_lock_filename = "/var/lock/pvefw.lck";
18 my $pve_fw_status_filename = "/var/lib/pve-firewall/pvefw.status";
20 # imported/converted from: /usr/share/shorewall/macro.*
23 { action
=> 'PARAM', proto
=> 'udp', dport
=> '10080' },
24 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '10080' },
27 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '113' },
30 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '179' },
33 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6881:6889' },
34 { action
=> 'PARAM', proto
=> 'udp', dport
=> '6881' },
37 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6881:6999' },
38 { action
=> 'PARAM', proto
=> 'udp', dport
=> '6881' },
41 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '2401' },
44 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1494' },
45 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1604' },
46 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '2598' },
49 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3689' },
50 { action
=> 'PARAM', proto
=> 'udp', dport
=> '3689' },
53 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6277' },
56 { action
=> 'PARAM', proto
=> 'udp', dport
=> '67:68', sport
=> '67:68' },
57 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '67:68', sport
=> '67:68' },
60 { action
=> 'PARAM', proto
=> 'udp', dport
=> '53' },
61 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '53' },
64 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3632' },
67 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '4662' },
68 { action
=> 'PARAM', proto
=> 'udp', dport
=> '4665' },
71 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '21' },
74 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '79' },
77 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '2086' },
78 { action
=> 'PARAM', proto
=> 'udp', dport
=> '2086' },
79 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1080' },
80 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1080' },
83 { action
=> 'PARAM', proto
=> '47' },
84 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> '47' },
87 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '9418' },
90 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6346' },
91 { action
=> 'PARAM', proto
=> 'udp', dport
=> '6346' },
94 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '11371' },
97 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '80' },
100 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '443' },
103 { action
=> 'PARAM', proto
=> 'udp', dport
=> '3130' },
106 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5190' },
109 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '143' },
112 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '993' },
115 { action
=> 'PARAM', proto
=> '94' },
116 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> '94' },
119 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '631' },
122 { action
=> 'PARAM', proto
=> 'udp', dport
=> '631' },
125 { action
=> 'PARAM', source
=> 'SOURCE', dest
=> 'DEST', proto
=> 'tcp', dport
=> '631' },
126 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '631' },
129 { action
=> 'PARAM', proto
=> 'udp', dport
=> '500', sport
=> '500' },
130 { action
=> 'PARAM', proto
=> '50' },
131 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '500', sport
=> '500' },
132 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> '50' },
135 { action
=> 'PARAM', proto
=> 'udp', dport
=> '500', sport
=> '500' },
136 { action
=> 'PARAM', proto
=> '51' },
137 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '500', sport
=> '500' },
138 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> '51' },
141 { action
=> 'PARAM', proto
=> 'udp', dport
=> '500' },
142 { action
=> 'PARAM', proto
=> 'udp', dport
=> '4500' },
143 { action
=> 'PARAM', proto
=> '50' },
144 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '500' },
145 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '4500' },
146 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> '50' },
149 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6667' },
152 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5222' },
155 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5223' },
158 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5269' },
161 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '9100' },
164 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1701' },
165 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '1701' },
168 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '389' },
171 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '636' },
174 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1863' },
177 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1433' },
180 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '25' },
181 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '465' },
182 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '587' },
185 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '4949' },
188 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3306' },
191 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '119' },
194 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '563' },
197 { action
=> 'PARAM', proto
=> 'udp', dport
=> '123' },
200 { action
=> 'PARAM', proto
=> 'udp', dport
=> '123' },
201 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '123' },
204 { action
=> 'PARAM', proto
=> 'udp', dport
=> '123' },
205 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1024:65535', sport
=> '123' },
208 { action
=> 'PARAM', proto
=> '89' },
211 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1194' },
214 { action
=> 'PARAM', proto
=> 'udp', dport
=> '5632' },
215 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5631' },
218 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '110' },
221 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '995' },
224 { action
=> 'PARAM', proto
=> '47' },
225 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> '47' },
226 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1723' },
229 { action
=> 'PARAM', proto
=> 'icmp', dport
=> 'echo-request' },
232 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5432' },
235 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '515' },
238 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3389' },
241 { action
=> 'PARAM', proto
=> 'udp', dport
=> '520' },
242 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '520' },
245 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '953' },
248 { action
=> 'ACCEPT', proto
=> 'tcp', dport
=> '2703' },
251 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '37' },
254 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '873' },
257 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6566' },
260 { action
=> 'PARAM', proto
=> 'udp', dport
=> '135,445' },
261 { action
=> 'PARAM', proto
=> 'udp', dport
=> '137:139' },
262 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1024:65535', sport
=> '137' },
263 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '135,139,445' },
266 { action
=> 'PARAM', proto
=> 'udp', dport
=> '135,445' },
267 { action
=> 'PARAM', proto
=> 'udp', dport
=> '137:139' },
268 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1024:65535', sport
=> '137' },
269 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '135,139,445' },
270 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '135,445' },
271 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '137:139' },
272 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '1024:65535', sport
=> '137' },
273 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'tcp', dport
=> '135,139,445' },
276 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '901' },
279 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '25' },
282 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '465' },
285 { action
=> 'PARAM', proto
=> 'udp', dport
=> '161:162' },
286 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '161' },
289 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '783' },
292 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '22' },
295 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3690' },
298 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3874' },
299 { action
=> 'PARAM', proto
=> 'udp', dport
=> '3740' },
300 { action
=> 'PARAM', proto
=> '41' },
301 { action
=> 'PARAM', proto
=> 'udp', dport
=> '5072,8374' },
304 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3128' },
307 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '587' },
310 { action
=> 'PARAM', proto
=> 'udp', dport
=> '514' },
311 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '514' },
314 { action
=> 'PARAM', proto
=> 'udp', dport
=> '69' },
317 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '23' },
320 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '992' },
323 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '37' },
326 { action
=> 'PARAM', proto
=> 'udp', dport
=> '33434:33524' },
327 { action
=> 'PARAM', proto
=> 'icmp', dport
=> 'echo-request' },
330 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5900:5909' },
333 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5500' },
336 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '80' },
337 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '443' },
340 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '8080' },
343 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '10000' },
346 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '43' },
350 my $pve_fw_parsed_macros;
351 my $pve_fw_preferred_macro_names = {};
353 my $pve_std_chains = {
354 'PVEFW-SET-ACCEPT-MARK' => [
355 "-j MARK --set-mark 1",
357 'PVEFW-DropBroadcast' => [
358 # same as shorewall 'Broadcast'
359 # simply DROP BROADCAST/MULTICAST/ANYCAST
360 # we can use this to reduce logging
361 { action
=> 'DROP', dsttype
=> 'BROADCAST' },
362 { action
=> 'DROP', dsttype
=> 'MULTICAST' },
363 { action
=> 'DROP', dsttype
=> 'ANYCAST' },
364 { action
=> 'DROP', dest
=> '224.0.0.0/4' },
367 # same as shorewall 'reject'
368 { action
=> 'DROP', dsttype
=> 'BROADCAST' },
369 { action
=> 'DROP', source
=> '224.0.0.0/4' },
370 { action
=> 'DROP', proto
=> 'icmp' },
371 "-p tcp -j REJECT --reject-with tcp-reset",
372 "-p udp -j REJECT --reject-with icmp-port-unreachable",
373 "-p icmp -j REJECT --reject-with icmp-host-unreachable",
374 "-j REJECT --reject-with icmp-host-prohibited",
377 # same as shorewall 'Drop', which is equal to DROP,
378 # but REJECT/DROP some packages to reduce logging,
379 # and ACCEPT critical ICMP types
380 { action
=> 'PVEFW-reject', proto
=> 'tcp', dport
=> '43' }, # REJECT 'auth'
381 # we are not interested in BROADCAST/MULTICAST/ANYCAST
382 { action
=> 'PVEFW-DropBroadcast' },
383 # ACCEPT critical ICMP types
384 { action
=> 'ACCEPT', proto
=> 'icmp', dport
=> 'fragmentation-needed' },
385 { action
=> 'ACCEPT', proto
=> 'icmp', dport
=> 'time-exceeded' },
386 # Drop packets with INVALID state
387 "-m conntrack --ctstate INVALID -j DROP",
388 # Drop Microsoft SMB noise
389 { action
=> 'DROP', proto
=> 'udp', dport
=> '135,445', nbdport
=> 2 },
390 { action
=> 'DROP', proto
=> 'udp', dport
=> '137:139'},
391 { action
=> 'DROP', proto
=> 'udp', dport
=> '1024:65535', sport
=> 137 },
392 { action
=> 'DROP', proto
=> 'tcp', dport
=> '135,139,445', nbdport
=> 3 },
393 { action
=> 'DROP', proto
=> 'udp', dport
=> 1900 }, # UPnP
394 # Drop new/NotSyn traffic so that it doesn't get logged
395 "-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -j DROP",
397 { action
=> 'DROP', proto
=> 'udp', sport
=> 53 },
400 # same as shorewall 'Reject', which is equal to Reject,
401 # but REJECT/DROP some packages to reduce logging,
402 # and ACCEPT critical ICMP types
403 { action
=> 'PVEFW-reject', proto
=> 'tcp', dport
=> '43' }, # REJECT 'auth'
404 # we are not interested in BROADCAST/MULTICAST/ANYCAST
405 { action
=> 'PVEFW-DropBroadcast' },
406 # ACCEPT critical ICMP types
407 { action
=> 'ACCEPT', proto
=> 'icmp', dport
=> 'fragmentation-needed' },
408 { action
=> 'ACCEPT', proto
=> 'icmp', dport
=> 'time-exceeded' },
409 # Drop packets with INVALID state
410 "-m conntrack --ctstate INVALID -j DROP",
411 # Drop Microsoft SMB noise
412 { action
=> 'PVEFW-reject', proto
=> 'udp', dport
=> '135,445', nbdport
=> 2 },
413 { action
=> 'PVEFW-reject', proto
=> 'udp', dport
=> '137:139'},
414 { action
=> 'PVEFW-reject', proto
=> 'udp', dport
=> '1024:65535', sport
=> 137 },
415 { action
=> 'PVEFW-reject', proto
=> 'tcp', dport
=> '135,139,445', nbdport
=> 3 },
416 { action
=> 'DROP', proto
=> 'udp', dport
=> 1900 }, # UPnP
417 # Drop new/NotSyn traffic so that it doesn't get logged
418 "-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -j DROP",
420 { action
=> 'DROP', proto
=> 'udp', sport
=> 53 },
422 'PVEFW-logflags' => [
423 # same as shorewall logflags action. (fixme: enable/disable logging)
424 "-j LOG --log-prefix \"logflags-dropped:\" --log-level 4 --log-ip-options",
427 'PVEFW-tcpflags' => [
428 # same as shorewall tcpflags action.
429 # Packets arriving on this interface are checked for som illegal combinations of TCP flags
430 "-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG FIN,PSH,URG -g PVEFW-logflags",
431 "-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG NONE -g PVEFW-logflags",
432 "-p tcp -m tcp --tcp-flags SYN,RST SYN,RST -g PVEFW-logflags",
433 "-p tcp -m tcp --tcp-flags FIN,SYN FIN,SYN -g PVEFW-logflags",
434 "-p tcp -m tcp --sport 0 --tcp-flags FIN,SYN,RST,ACK SYN -g PVEFW-logflags",
436 'PVEFW-smurflog' => [
437 # same as shorewall smurflog. (fixme: enable/disable logging)
438 "-j LOG --log-prefix \"smurfs-dropped\" --log-level 4",
442 # same as shorewall smurfs action
443 # Filter packets for smurfs (packets with a broadcast address as the source).
444 "-s 0.0.0.0/32 -j RETURN",
445 "-m addrtype --src-type BROADCAST -g PVEFW-smurflog",
446 "-s 224.0.0.0/4 -g PVEFW-smurflog",
450 # iptables -p icmp -h
451 my $icmp_type_names = {
454 'destination-unreachable' => 1,
455 'network-unreachable' => 1,
456 'host-unreachable' => 1,
457 'protocol-unreachable' => 1,
458 'port-unreachable' => 1,
459 'fragmentation-needed' => 1,
460 'source-route-failed' => 1,
461 'network-unknown' => 1,
463 'network-prohibited' => 1,
464 'host-prohibited' => 1,
465 'TOS-network-unreachable' => 1,
466 'TOS-host-unreachable' => 1,
467 'communication-prohibited' => 1,
468 'host-precedence-violation' => 1,
469 'precedence-cutoff' => 1,
470 'source-quench' => 1,
472 'network-redirect' => 1,
473 'host-redirect' => 1,
474 'TOS-network-redirect' => 1,
475 'TOS-host-redirect' => 1,
477 'router-advertisement' => 1,
478 'router-solicitation' => 1,
479 'time-exceeded' => 1,
480 'ttl-zero-during-transit' => 1,
481 'ttl-zero-during-reassembly' => 1,
482 'parameter-problem' => 1,
483 'ip-header-bad' => 1,
484 'required-option-missing' => 1,
485 'timestamp-request' => 1,
486 'timestamp-reply' => 1,
487 'address-mask-request' => 1,
488 'address-mask-reply' => 1,
491 sub get_firewall_macros
{
493 return $pve_fw_parsed_macros if $pve_fw_parsed_macros;
495 $pve_fw_parsed_macros = {};
497 foreach my $k (keys %$pve_fw_macros) {
500 my $macro = $pve_fw_macros->{$k};
501 $pve_fw_preferred_macro_names->{$name} = $k;
502 $pve_fw_parsed_macros->{$name} = $macro;
505 return $pve_fw_parsed_macros;
510 sub get_etc_services
{
512 return $etc_services if $etc_services;
514 my $filename = "/etc/services";
516 my $fh = IO
::File-
>new($filename, O_RDONLY
);
518 warn "unable to read '$filename' - $!\n";
524 while (my $line = <$fh>) {
526 next if $line =~m/^#/;
527 next if ($line =~m/^\s*$/);
529 if ($line =~ m!^(\S+)\s+(\S+)/(tcp|udp).*$!) {
530 $services->{byid
}->{$2}->{name
} = $1;
531 $services->{byid
}->{$2}->{port
} = $2;
532 $services->{byid
}->{$2}->{$3} = 1;
533 $services->{byname
}->{$1} = $services->{byid
}->{$2};
539 $etc_services = $services;
542 return $etc_services;
547 sub get_etc_protocols
{
548 return $etc_protocols if $etc_protocols;
550 my $filename = "/etc/protocols";
552 my $fh = IO
::File-
>new($filename, O_RDONLY
);
554 warn "unable to read '$filename' - $!\n";
560 while (my $line = <$fh>) {
562 next if $line =~m/^#/;
563 next if ($line =~m/^\s*$/);
565 if ($line =~ m!^(\S+)\s+(\d+)\s+.*$!) {
566 $protocols->{byid
}->{$2}->{name
} = $1;
567 $protocols->{byname
}->{$1} = $protocols->{byid
}->{$2};
573 $etc_protocols = $protocols;
575 return $etc_protocols;
578 sub parse_address_list
{
582 foreach my $aor (split(/,/, $str)) {
583 if (!Net
::IP-
>new($aor)) {
584 my $err = Net
::IP
::Error
();
585 die "invalid IP address: $err\n";
593 sub parse_port_name_number_or_range
{
596 my $services = PVE
::Firewall
::get_etc_services
();
598 foreach my $item (split(/,/, $str)) {
602 foreach my $pon (split(':', $item, 2)) {
603 $pon = $services->{byname
}->{$pon}->{port
} if $services->{byname
}->{$pon}->{port
};
604 if ($pon =~ m/^\d+$/){
605 die "invalid port '$pon'\n" if $pon < 0 && $pon > 65535;
606 die "port '$pon' must be bigger than port '$oldpon' \n" if $oldpon && ($pon < $oldpon);
609 die "invalid port $services->{byname}->{$pon}\n" if !$services->{byname
}->{$pon};
617 my $bridge_firewall_enabled = 0;
619 sub enable_bridge_firewall
{
621 return if $bridge_firewall_enabled; # only once
623 system("echo 1 > /proc/sys/net/bridge/bridge-nf-call-iptables");
624 system("echo 1 > /proc/sys/net/bridge/bridge-nf-call-ip6tables");
626 $bridge_firewall_enabled = 1;
629 my $rule_format = "%-15s %-30s %-30s %-15s %-15s %-15s\n";
634 run_command
("/sbin/iptables $cmd", outfunc
=> sub {}, errfunc
=> sub {});
637 sub iptables_restore_cmdlist
{
640 run_command
("/sbin/iptables-restore -n", input
=> $cmdlist);
643 sub iptables_get_chains
{
647 # check what chains we want to track
648 my $is_pvefw_chain = sub {
651 return 1 if $name =~ m/^PVEFW-\S+$/;
653 return 1 if $name =~ m/^tap\d+i\d+-(:?IN|OUT)$/;
654 return 1 if $name =~ m/^vmbr\d+-(:?FW|IN|OUT)$/;
655 return 1 if $name =~ m/^GROUP-(:?[^\s\-]+)-(:?IN|OUT)$/;
665 return if $line =~ m/^#/;
666 return if $line =~ m/^\s*$/;
668 if ($line =~ m/^\*(\S+)$/) {
673 return if $table ne 'filter';
675 if ($line =~ m/^:(\S+)\s/) {
677 return if !&$is_pvefw_chain($chain);
678 $res->{$chain} = "unknown";
679 } elsif ($line =~ m/^-A\s+(\S+)\s.*--comment\s+\"PVESIG:(\S+)\"/) {
680 my ($chain, $sig) = ($1, $2);
681 return if !&$is_pvefw_chain($chain);
682 $res->{$chain} = $sig;
684 # simply ignore the rest
689 run_command
("/sbin/iptables-save", outfunc
=> $parser);
694 sub iptables_chain_exist
{
698 iptables
("-n --list $chain");
705 sub iptables_rule_exist
{
709 iptables
("-C $rule");
716 sub ruleset_generate_rule
{
717 my ($ruleset, $chain, $rule, $actions, $goto) = @_;
719 return if $rule->{disable
};
723 $cmd .= " -m iprange --src-range" if $rule->{nbsource
} && $rule->{nbsource
} > 1;
724 $cmd .= " -s $rule->{source}" if $rule->{source
};
725 $cmd .= " -m iprange --dst-range" if $rule->{nbdest
} && $rule->{nbdest
} > 1;
726 $cmd .= " -d $rule->{dest}" if $rule->{dest
};
728 if ($rule->{proto
}) {
729 $cmd .= " -p $rule->{proto}";
732 $multiport++ if $rule->{nbdport
} && ($rule->{nbdport
} > 1);
733 $multiport++ if $rule->{nbsport
} && ($rule->{nbsport
} > 1);
735 $cmd .= " --match multiport" if $multiport;
737 die "multiport: option '--sports' cannot be used together with '--dports'\n"
738 if ($multiport == 2) && ($rule->{dport
} ne $rule->{sport
});
740 if ($rule->{dport
}) {
741 if ($rule->{proto
} && $rule->{proto
} eq 'icmp') {
742 # Note: we use dport to store --icmp-type
743 die "unknown icmp-type '$rule->{dport}'\n" if !defined($icmp_type_names->{$rule->{dport
}});
744 $cmd .= " -m icmp --icmp-type $rule->{dport}";
746 if ($rule->{nbdport
} && $rule->{nbdport
} > 1) {
747 if ($multiport == 2) {
748 $cmd .= " --ports $rule->{dport}";
750 $cmd .= " --dports $rule->{dport}";
753 $cmd .= " --dport $rule->{dport}";
758 if ($rule->{sport
}) {
759 if ($rule->{nbsport
} && $rule->{nbsport
} > 1) {
760 $cmd .= " --sports $rule->{sport}" if $multiport != 2;
762 $cmd .= " --sport $rule->{sport}";
765 } elsif ($rule->{dport
} || $rule->{sport
}) {
766 warn "ignoring destination port '$rule->{dport}' - no protocol specified\n" if $rule->{dport
};
767 warn "ignoring source port '$rule->{sport}' - no protocol specified\n" if $rule->{sport
};
770 $cmd .= " -m addrtype --dst-type $rule->{dsttype}" if $rule->{dsttype
};
772 if (my $action = $rule->{action
}) {
773 $action = $actions->{$action} if defined($actions->{$action});
774 $goto = 1 if !defined($goto) && $action eq 'PVEFW-SET-ACCEPT-MARK';
775 $cmd .= $goto ?
" -g $action" : " -j $action";
778 ruleset_addrule
($ruleset, $chain, $cmd) if $cmd;
781 sub ruleset_create_chain
{
782 my ($ruleset, $chain) = @_;
784 die "Invalid chain name '$chain' (28 char max)\n" if length($chain) > 28;
786 die "chain '$chain' already exists\n" if $ruleset->{$chain};
788 $ruleset->{$chain} = [];
791 sub ruleset_chain_exist
{
792 my ($ruleset, $chain) = @_;
794 return $ruleset->{$chain} ?
1 : undef;
797 sub ruleset_addrule
{
798 my ($ruleset, $chain, $rule) = @_;
800 die "no such chain '$chain'\n" if !$ruleset->{$chain};
802 push @{$ruleset->{$chain}}, "-A $chain $rule";
805 sub ruleset_insertrule
{
806 my ($ruleset, $chain, $rule) = @_;
808 die "no such chain '$chain'\n" if !$ruleset->{$chain};
810 unshift @{$ruleset->{$chain}}, "-A $chain $rule";
813 sub generate_bridge_chains
{
814 my ($ruleset, $bridge) = @_;
816 if (!ruleset_chain_exist
($ruleset, "PVEFW-FORWARD")){
817 ruleset_create_chain
($ruleset, "PVEFW-FORWARD");
818 ruleset_addrule
($ruleset, "PVEFW-FORWARD", "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT");
821 if (!ruleset_chain_exist
($ruleset, "$bridge-FW")) {
822 ruleset_create_chain
($ruleset, "$bridge-FW");
823 ruleset_addrule
($ruleset, "PVEFW-FORWARD", "-o $bridge -m physdev --physdev-is-bridged -j $bridge-FW");
824 ruleset_addrule
($ruleset, "PVEFW-FORWARD", "-i $bridge -m physdev --physdev-is-bridged -j $bridge-FW");
825 ruleset_addrule
($ruleset, "PVEFW-FORWARD", "-o $bridge -j DROP"); # disable interbridge routing
826 ruleset_addrule
($ruleset, "PVEFW-FORWARD", "-i $bridge -j DROP"); # disable interbridge routing
829 if (!ruleset_chain_exist
($ruleset, "$bridge-OUT")) {
830 ruleset_create_chain
($ruleset, "$bridge-OUT");
831 ruleset_addrule
($ruleset, "$bridge-FW", "-m physdev --physdev-is-bridged --physdev-is-in -j $bridge-OUT");
834 if (!ruleset_chain_exist
($ruleset, "$bridge-IN")) {
835 ruleset_create_chain
($ruleset, "$bridge-IN");
836 ruleset_addrule
($ruleset, "$bridge-FW", "-m physdev --physdev-is-bridged --physdev-is-out -j $bridge-IN");
837 ruleset_addrule
($ruleset, "$bridge-FW", "-m mark --mark 1 -j ACCEPT");
838 # accept traffic to unmanaged bridge ports
839 ruleset_addrule
($ruleset, "$bridge-FW", "-m physdev --physdev-is-bridged --physdev-is-out -j ACCEPT ");
843 sub generate_tap_rules_direction
{
844 my ($ruleset, $group_rules, $iface, $netid, $macaddr, $vmfw_conf, $bridge, $direction) = @_;
846 my $rules = $vmfw_conf->{lc($direction)};
847 my $options = $vmfw_conf->{options
};
849 my $tapchain = "$iface-$direction";
851 ruleset_create_chain
($ruleset, $tapchain);
853 if (!(defined($options->{nosmurfs
}) && $options->{nosmurfs
} == 0)) {
854 ruleset_addrule
($ruleset, $tapchain, "-m conntrack --ctstate INVALID,NEW -j PVEFW-smurfs");
857 if (!(defined($options->{dhcp
}) && $options->{dhcp
} == 0)) {
858 ruleset_addrule
($ruleset, $tapchain, "-p udp -m udp --dport 67:68 -j ACCEPT");
861 if ($options->{tcpflags
}) {
862 ruleset_addrule
($ruleset, $tapchain, "-p tcp -j PVEFW-tcpflags");
865 ruleset_addrule
($ruleset, $tapchain, "-m conntrack --ctstate INVALID -j DROP");
866 ruleset_addrule
($ruleset, $tapchain, "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT");
868 if ($direction eq 'OUT' && defined($macaddr) &&
869 !(defined($options->{macfilter
}) && $options->{macfilter
} == 0)) {
870 ruleset_addrule
($ruleset, $tapchain, "-m mac ! --mac-source $macaddr -j DROP");
874 foreach my $rule (@$rules) {
875 next if $rule->{iface
} && $rule->{iface
} ne $netid;
876 # we go to $bridge-IN if accept in out rules
877 if($rule->{action
} =~ m/^(GROUP-(\S+))$/){
878 $rule->{action
} .= "-$direction";
879 # generate empty group rule if don't exist
880 if(!ruleset_chain_exist
($ruleset, $rule->{action
})){
881 generate_group_rules
($ruleset, $group_rules, $2);
883 ruleset_generate_rule
($ruleset, $tapchain, $rule);
884 ruleset_addrule
($ruleset, $tapchain, "-m mark --mark 1 -j RETURN")
885 if $direction eq 'OUT';
887 if ($direction eq 'OUT') {
888 ruleset_generate_rule
($ruleset, $tapchain, $rule,
889 { ACCEPT
=> "PVEFW-SET-ACCEPT-MARK", REJECT
=> "PVEFW-reject" });
891 ruleset_generate_rule
($ruleset, $tapchain, $rule, { REJECT
=> "PVEFW-reject" });
900 if ($direction eq 'OUT') {
901 $policy = $options->{'policy-out'} || 'ACCEPT'; # allow everything by default
903 $policy = $options->{'policy-in'} || 'DROP'; # allow everything by default
906 if ($policy eq 'ACCEPT') {
907 if ($direction eq 'OUT') {
908 ruleset_addrule
($ruleset, $tapchain, "-g PVEFW-SET-ACCEPT-MARK");
910 ruleset_addrule
($ruleset, $tapchain, "-j ACCEPT");
912 } elsif ($policy eq 'DROP') {
913 ruleset_addrule
($ruleset, $tapchain, "-j PVEFW-Drop");
914 ruleset_addrule
($ruleset, $tapchain, "-j LOG --log-prefix \"$tapchain-dropped: \" --log-level 4");
915 ruleset_addrule
($ruleset, $tapchain, "-j DROP");
916 } elsif ($policy eq 'REJECT') {
917 ruleset_addrule
($ruleset, $tapchain, "-j PVEFW-Reject");
918 ruleset_addrule
($ruleset, $tapchain, "-j LOG --log-prefix \"$tapchain-reject: \" --log-level 4");
919 ruleset_addrule
($ruleset, $tapchain, "-g PVEFW-reject");
922 die "internal error: unknown policy '$policy'";
925 # plug the tap chain to bridge chain
926 my $physdevdirection = $direction eq 'IN' ?
"out" : "in";
927 my $rule = "-m physdev --physdev-$physdevdirection $iface --physdev-is-bridged -j $tapchain";
928 ruleset_insertrule
($ruleset, "$bridge-$direction", $rule);
930 if ($direction eq 'OUT'){
931 # add tap->host rules
932 my $rule = "-m physdev --physdev-$physdevdirection $iface -j $tapchain";
933 ruleset_addrule
($ruleset, "PVEFW-INPUT", $rule);
938 my ($ruleset, $rules, $group_rules) = @_;
940 # fixme: allow security groups
942 # host inbound firewall
943 my $chain = "PVEFW-HOST-IN";
944 ruleset_create_chain
($ruleset, $chain);
946 ruleset_addrule
($ruleset, $chain, "-m conntrack --ctstate INVALID -j DROP");
947 ruleset_addrule
($ruleset, $chain, "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT");
948 ruleset_addrule
($ruleset, $chain, "-i lo -j ACCEPT");
949 ruleset_addrule
($ruleset, $chain, "-m addrtype --dst-type MULTICAST -j ACCEPT");
950 ruleset_addrule
($ruleset, $chain, "-p udp -m conntrack --ctstate NEW -m multiport --dports 5404,5405 -j ACCEPT");
951 ruleset_addrule
($ruleset, $chain, "-p udp -m udp --dport 9000 -j ACCEPT"); #corosync
954 foreach my $rule (@{$rules->{in}}) {
955 # we use RETURN because we need to check also tap rules
956 ruleset_generate_rule
($ruleset, $chain, $rule, { ACCEPT
=> 'RETURN', REJECT
=> "PVEFW-reject" });
960 ruleset_addrule
($ruleset, $chain, "-j LOG --log-prefix \"kvmhost-IN dropped: \" --log-level 4");
961 ruleset_addrule
($ruleset, $chain, "-j DROP");
963 # host outbound firewall
964 $chain = "PVEFW-HOST-OUT";
965 ruleset_create_chain
($ruleset, $chain);
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, "-o 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
975 foreach my $rule (@{$rules->{out
}}) {
976 # we use RETURN because we need to check also tap rules
977 ruleset_generate_rule
($ruleset, $chain, $rule, { ACCEPT
=> 'RETURN', REJECT
=> "PVEFW-reject" });
981 ruleset_addrule
($ruleset, $chain, "-j LOG --log-prefix \"kvmhost-OUT dropped: \" --log-level 4");
982 ruleset_addrule
($ruleset, $chain, "-j DROP");
984 ruleset_addrule
($ruleset, "PVEFW-OUTPUT", "-j PVEFW-HOST-OUT");
985 ruleset_addrule
($ruleset, "PVEFW-INPUT", "-j PVEFW-HOST-IN");
988 sub generate_group_rules
{
989 my ($ruleset, $group_rules, $group) = @_;
991 my $rules = $group_rules->{$group};
993 die "no such security group '$group'\n" if !$rules;
995 my $chain = "GROUP-${group}-IN";
997 ruleset_create_chain
($ruleset, $chain);
1000 foreach my $rule (@{$rules->{in}}) {
1001 ruleset_generate_rule
($ruleset, $chain, $rule, { REJECT
=> "PVEFW-reject" });
1005 $chain = "GROUP-${group}-OUT";
1007 ruleset_create_chain
($ruleset, $chain);
1008 ruleset_addrule
($ruleset, $chain, "-j MARK --set-mark 0"); # clear mark
1010 if ($rules->{out
}) {
1011 foreach my $rule (@{$rules->{out
}}) {
1012 # we use PVEFW-SET-ACCEPT-MARK (Instead of ACCEPT) because we need to
1013 # check also other tap rules later
1014 ruleset_generate_rule
($ruleset, $chain, $rule,
1015 { ACCEPT
=> 'PVEFW-SET-ACCEPT-MARK', REJECT
=> "PVEFW-reject" });
1021 my $valid_netdev_names = {};
1022 for (my $i = 0; $i < $MAX_NETS; $i++) {
1023 $valid_netdev_names->{"net$i"} = 1;
1027 my ($line, $need_iface, $allow_groups) = @_;
1029 my $macros = get_firewall_macros
();
1030 my $protocols = get_etc_protocols
();
1032 my ($action, $iface, $source, $dest, $proto, $dport, $sport);
1034 # we can add single line comments to the end of the rule
1035 my $comment = $1 if $line =~ s/#\s*(.*?)\s*$//;
1037 # we can disable a rule when prefixed with '|'
1038 my $disable = 1 if $line =~ s/^\|//;
1040 my @data = split(/\s+/, $line);
1041 my $expected_elements = $need_iface ?
7 : 6;
1043 die "wrong number of rule elements\n" if scalar(@data) > $expected_elements;
1046 ($action, $iface, $source, $dest, $proto, $dport, $sport) = @data
1048 ($action, $source, $dest, $proto, $dport, $sport) = @data;
1051 die "incomplete rule\n" if !$action;
1056 if ($action =~ m/^(ACCEPT|DROP|REJECT)$/) {
1058 } elsif ($allow_groups && $action =~ m/^GROUP-(:?\S+)$/) {
1060 } elsif ($action =~ m/^(\S+)\((ACCEPT|DROP|REJECT)\)$/) {
1061 ($macro_name, $action) = ($1, $2);
1062 my $lc_macro_name = lc($macro_name);
1063 my $preferred_name = $pve_fw_preferred_macro_names->{$lc_macro_name};
1064 $macro_name = $preferred_name if $preferred_name;
1065 $macro = $macros->{$lc_macro_name};
1066 die "unknown macro '$macro_name'\n" if !$macro;
1068 die "unknown action '$action'\n";
1072 $iface = undef if $iface && $iface eq '-';
1073 die "unknown interface '$iface'\n"
1074 if defined($iface) && !$valid_netdev_names->{$iface};
1077 $proto = undef if $proto && $proto eq '-';
1078 die "unknown protokol '$proto'\n" if $proto &&
1079 !(defined($protocols->{byname
}->{$proto}) ||
1080 defined($protocols->{byid
}->{$proto}));
1082 $source = undef if $source && $source eq '-';
1083 $dest = undef if $dest && $dest eq '-';
1085 $dport = undef if $dport && $dport eq '-';
1086 $sport = undef if $sport && $sport eq '-';
1088 my $nbsource = undef;
1091 $nbsource = parse_address_list
($source) if $source;
1092 $nbdest = parse_address_list
($dest) if $dest;
1097 disable
=> $disable,
1098 comment
=> $comment,
1103 nbsource
=> $nbsource,
1111 foreach my $templ (@$macro) {
1113 my $param_used = {};
1114 foreach my $k (keys %$templ) {
1115 my $v = $templ->{$k};
1116 if ($v eq 'PARAM') {
1118 $param_used->{$k} = 1;
1119 } elsif ($v eq 'DEST') {
1120 $v = $param->{dest
};
1121 $param_used->{dest
} = 1;
1122 } elsif ($v eq 'SOURCE') {
1123 $v = $param->{source
};
1124 $param_used->{source
} = 1;
1127 die "missing parameter '$k' in macro '$macro_name'\n" if !defined($v);
1130 foreach my $k (keys %$param) {
1131 next if !defined($param->{$k});
1132 next if $param_used->{$k};
1133 if (defined($rule->{$k})) {
1134 die "parameter '$k' already define in macro (value = '$rule->{$k}')\n"
1135 if $rule->{$k} ne $param->{$k};
1137 $rule->{$k} = $param->{$k};
1140 push @$rules, $rule;
1143 push @$rules, $param;
1146 foreach my $rule (@$rules) {
1147 $rule->{nbdport
} = parse_port_name_number_or_range
($rule->{dport
})
1148 if defined($rule->{dport
});
1149 $rule->{nbsport
} = parse_port_name_number_or_range
($rule->{sport
})
1150 if defined($rule->{sport
});
1156 sub parse_fw_option
{
1161 if ($line =~ m/^(enable|dhcp|macfilter|nosmurfs|tcpflags):\s*(0|1)\s*$/i) {
1164 } elsif ($line =~ m/^(policy-(in|out)):\s*(ACCEPT|DROP|REJECT)\s*$/i) {
1169 die "can't parse option '$line'\n"
1172 return ($opt, $value);
1175 sub parse_vm_fw_rules
{
1176 my ($filename, $fh) = @_;
1178 my $res = { in => [], out
=> [], options
=> {}};
1182 while (defined(my $line = <$fh>)) {
1183 next if $line =~ m/^#/;
1184 next if $line =~ m/^\s*$/;
1186 my $linenr = $fh->input_line_number();
1187 my $prefix = "$filename (line $linenr)";
1189 if ($line =~ m/^\[(\S+)\]\s*$/i) {
1191 warn "$prefix: ignore unknown section '$section'\n" if !$res->{$section};
1195 warn "$prefix: skip line - no section";
1199 next if !$res->{$section}; # skip undefined section
1201 if ($section eq 'options') {
1203 my ($opt, $value) = parse_fw_option
($line);
1204 $res->{options
}->{$opt} = $value;
1206 warn "$prefix: $@" if $@;
1211 eval { $rules = parse_fw_rule
($line, 1, 1); };
1213 warn "$prefix: $err";
1217 push @{$res->{$section}}, @$rules;
1223 sub parse_host_fw_rules
{
1224 my ($filename, $fh) = @_;
1226 my $res = { in => [], out
=> [] };
1230 while (defined(my $line = <$fh>)) {
1231 next if $line =~ m/^#/;
1232 next if $line =~ m/^\s*$/;
1234 my $linenr = $fh->input_line_number();
1235 my $prefix = "$filename (line $linenr)";
1237 if ($line =~ m/^\[(in|out)\]\s*$/i) {
1242 warn "$prefix: skip line - no section";
1247 eval { $rules = parse_fw_rule
($line, 1, 1); };
1249 warn "$prefix: $err";
1253 push @{$res->{$section}}, @$rules;
1259 sub parse_group_fw_rules
{
1260 my ($filename, $fh) = @_;
1265 my $res = { in => [], out
=> [] };
1267 while (defined(my $line = <$fh>)) {
1268 next if $line =~ m/^#/;
1269 next if $line =~ m/^\s*$/;
1271 my $linenr = $fh->input_line_number();
1272 my $prefix = "$filename (line $linenr)";
1274 if ($line =~ m/^\[(in|out):(\S+)\]\s*$/i) {
1279 if (!$section || !$group) {
1280 warn "$prefix: skip line - no section";
1285 eval { $rules = parse_fw_rule
($line, 0, 0); };
1287 warn "$prefix: $err";
1291 push @{$res->{$group}->{$section}}, @$rules;
1298 my ($code, @param) = @_;
1302 my $res = lock_file
($pve_fw_lock_filename, $timeout, $code, @param);
1309 sub read_local_vm_config
{
1315 my $list = PVE
::QemuServer
::config_list
();
1317 foreach my $vmid (keys %$list) {
1318 my $cfspath = PVE
::QemuServer
::cfs_config_path
($vmid);
1319 if (my $conf = PVE
::Cluster
::cfs_read_file
($cfspath)) {
1320 $qemu->{$vmid} = $conf;
1324 my $vmdata = { openvz
=> $openvz, qemu
=> $qemu };
1329 sub read_vm_firewall_rules
{
1332 foreach my $vmid (keys %{$vmdata->{qemu
}}, keys %{$vmdata->{openvz
}}) {
1333 my $filename = "/etc/pve/firewall/$vmid.fw";
1334 my $fh = IO
::File-
>new($filename, O_RDONLY
);
1337 $rules->{$vmid} = parse_vm_fw_rules
($filename, $fh);
1343 sub generate_std_chains
{
1346 foreach my $chain (keys %$pve_std_chains) {
1347 ruleset_create_chain
($ruleset, $chain);
1348 foreach my $rule (@{$pve_std_chains->{$chain}}) {
1350 ruleset_generate_rule
($ruleset, $chain, $rule);
1352 ruleset_addrule
($ruleset, $chain, $rule);
1358 sub save_pvefw_status
{
1361 die "unknown status '$status' - internal error"
1362 if $status !~ m/^(stopped|active)$/;
1364 mkdir dirname
($pve_fw_status_filename);
1365 PVE
::Tools
::file_set_contents
($pve_fw_status_filename, $status);
1368 sub read_pvefw_status
{
1370 my $status = 'unknown';
1372 return 'stopped' if ! -f
$pve_fw_status_filename;
1375 $status = PVE
::Tools
::file_get_contents
($pve_fw_status_filename);
1383 my $vmdata = read_local_vm_config
();
1384 my $rules = read_vm_firewall_rules
($vmdata);
1386 my $group_rules = {};
1387 my $filename = "/etc/pve/firewall/groups.fw";
1388 if (my $fh = IO
::File-
>new($filename, O_RDONLY
)) {
1389 $group_rules = parse_group_fw_rules
($filename, $fh);
1392 #print Dumper($rules);
1396 ruleset_create_chain
($ruleset, "PVEFW-INPUT");
1397 ruleset_create_chain
($ruleset, "PVEFW-OUTPUT");
1398 ruleset_create_chain
($ruleset, "PVEFW-FORWARD");
1400 generate_std_chains
($ruleset);
1402 my $enable_hostfw = 0;
1403 $filename = "/etc/pve/local/host.fw";
1404 if (my $fh = IO
::File-
>new($filename, O_RDONLY
)) {
1405 my $host_rules = parse_host_fw_rules
($filename, $fh);
1409 enablehostfw
($ruleset, $host_rules, $group_rules);
1412 # generate firewall rules for QEMU VMs
1413 foreach my $vmid (keys %{$vmdata->{qemu
}}) {
1414 my $conf = $vmdata->{qemu
}->{$vmid};
1415 my $vmfw_conf = $rules->{$vmid};
1416 next if !$vmfw_conf;
1417 next if defined($vmfw_conf->{options
}->{enable
}) && ($vmfw_conf->{options
}->{enable
} == 0);
1419 foreach my $netid (keys %$conf) {
1420 next if $netid !~ m/^net(\d+)$/;
1421 my $net = PVE
::QemuServer
::parse_net
($conf->{$netid});
1423 my $iface = "tap${vmid}i$1";
1425 my $bridge = $net->{bridge
};
1426 next if !$bridge; # fixme: ?
1428 $bridge .= "v$net->{tag}" if $net->{tag
};
1431 generate_bridge_chains
($ruleset, $bridge);
1433 my $macaddr = $net->{macaddr
};
1434 generate_tap_rules_direction
($ruleset, $group_rules, $iface, $netid, $macaddr, $vmfw_conf, $bridge, 'IN');
1435 generate_tap_rules_direction
($ruleset, $group_rules, $iface, $netid, $macaddr, $vmfw_conf, $bridge, 'OUT');
1439 if ($enable_hostfw) {
1440 # allow traffic from lo (ourself)
1441 ruleset_addrule
($ruleset, "PVEFW-INPUT", "-i lo -j ACCEPT");
1447 sub get_ruleset_status
{
1448 my ($ruleset, $verbose) = @_;
1450 my $active_chains = iptables_get_chains
();
1452 my $statushash = {};
1454 foreach my $chain (sort keys %$ruleset) {
1455 my $digest = Digest
::SHA-
>new('sha1');
1456 foreach my $cmd (@{$ruleset->{$chain}}) {
1457 $digest->add("$cmd\n");
1459 my $sig = $digest->b64digest;
1460 $statushash->{$chain}->{sig
} = $sig;
1462 my $oldsig = $active_chains->{$chain};
1463 if (!defined($oldsig)) {
1464 $statushash->{$chain}->{action
} = 'create';
1466 if ($oldsig eq $sig) {
1467 $statushash->{$chain}->{action
} = 'exists';
1469 $statushash->{$chain}->{action
} = 'update';
1472 print "$statushash->{$chain}->{action} $chain ($sig)\n" if $verbose;
1473 foreach my $cmd (@{$ruleset->{$chain}}) {
1474 print "\t$cmd\n" if $verbose;
1478 foreach my $chain (sort keys %$active_chains) {
1479 if (!defined($ruleset->{$chain})) {
1480 my $sig = $active_chains->{$chain};
1481 $statushash->{$chain}->{action
} = 'delete';
1482 $statushash->{$chain}->{sig
} = $sig;
1483 print "delete $chain ($sig)\n" if $verbose;
1493 get_ruleset_status
($ruleset, 1);
1496 sub print_sig_rule
{
1497 my ($chain, $sig) = @_;
1499 # We just use this to store a SHA1 checksum used to detect changes
1500 return "-A $chain -m comment --comment \"PVESIG:$sig\"\n";
1503 sub get_rulset_cmdlist {
1504 my ($ruleset, $verbose) = @_;
1506 my $cmdlist = "*filter
\n"; # we pass this to iptables-restore;
1508 my $statushash = get_ruleset_status($ruleset, $verbose);
1510 # create missing chains first
1511 foreach my $chain (sort keys %$ruleset) {
1512 my $stat = $statushash->{$chain};
1513 die "internal error
" if !$stat;
1514 next if $stat->{action} ne 'create';
1516 $cmdlist .= ":$chain - [0:0]\n";
1519 my $rule = "INPUT
-j PVEFW-INPUT
";
1520 if (!PVE::Firewall::iptables_rule_exist($rule)) {
1521 $cmdlist .= "-A
$rule\n";
1523 $rule = "OUTPUT
-j PVEFW-OUTPUT
";
1524 if (!PVE::Firewall::iptables_rule_exist($rule)) {
1525 $cmdlist .= "-A
$rule\n";
1528 $rule = "FORWARD
-j PVEFW-FORWARD
";
1529 if (!PVE::Firewall::iptables_rule_exist($rule)) {
1530 $cmdlist .= "-A
$rule\n";
1533 foreach my $chain (sort keys %$ruleset) {
1534 my $stat = $statushash->{$chain};
1535 die "internal error
" if !$stat;
1537 if ($stat->{action} eq 'update' || $stat->{action} eq 'create') {
1538 $cmdlist .= "-F
$chain\n";
1539 foreach my $cmd (@{$ruleset->{$chain}}) {
1540 $cmdlist .= "$cmd\n";
1542 $cmdlist .= print_sig_rule($chain, $stat->{sig});
1543 } elsif ($stat->{action} eq 'delete') {
1544 die "internal error
"; # this should not happen
1545 } elsif ($stat->{action} eq 'exists') {
1548 die "internal error
- unknown status
'$stat->{action}'";
1552 foreach my $chain (keys %$statushash) {
1553 next if $statushash->{$chain}->{action} ne 'delete';
1554 $cmdlist .= "-F
$chain\n";
1556 foreach my $chain (keys %$statushash) {
1557 next if $statushash->{$chain}->{action} ne 'delete';
1558 next if $chain eq 'PVEFW-INPUT';
1559 next if $chain eq 'PVEFW-OUTPUT';
1560 next if $chain eq 'PVEFW-FORWARD';
1561 $cmdlist .= "-X
$chain\n";
1564 $cmdlist .= "COMMIT
\n";
1570 my ($ruleset, $verbose) = @_;
1572 enable_bridge_firewall();
1574 my $cmdlist = get_rulset_cmdlist($ruleset, $verbose);
1576 print $cmdlist if $verbose;
1578 iptables_restore_cmdlist($cmdlist);
1580 # test: re-read status and check if everything is up to date
1581 my $statushash = get_ruleset_status($ruleset);
1584 foreach my $chain (sort keys %$ruleset) {
1585 my $stat = $statushash->{$chain};
1586 if ($stat->{action} ne 'exists') {
1587 warn "unable to update chain
'$chain'\n";
1592 die "unable to apply firewall changes
\n" if $errors;
1596 my ($start, $verbose) = @_;
1599 my $status = read_pvefw_status();
1601 my $ruleset = PVE::Firewall::compile();
1603 if ($start || $status eq 'active') {
1605 save_pvefw_status('active') if ($status ne 'active');
1607 PVE::Firewall::apply_ruleset($ruleset, $verbose);
1609 print "Firewall
not active
(status
= $status)\n" if $verbose;