12 use PVE
::Tools
qw(run_command lock_file);
16 my $pve_fw_lock_filename = "/var/lock/pvefw.lck";
21 # todo: implement some kind of MACROS, like shorewall /usr/share/shorewall/macro.*
22 sub get_firewall_macros
{
24 return $macros if $macros;
26 #foreach my $path (</usr/share/shorewall/macro.*>) {
27 # if ($path =~ m|/macro\.(\S+)$|) {
32 $macros = {}; # fixme: implemet me
39 sub get_etc_services
{
41 return $etc_services if $etc_services;
43 my $filename = "/etc/services";
45 my $fh = IO
::File-
>new($filename, O_RDONLY
);
47 warn "unable to read '$filename' - $!\n";
53 while (my $line = <$fh>) {
55 next if $line =~m/^#/;
56 next if ($line =~m/^\s*$/);
58 if ($line =~ m!^(\S+)\s+(\S+)/(tcp|udp).*$!) {
59 $services->{byid
}->{$2}->{name
} = $1;
60 $services->{byid
}->{$2}->{$3} = 1;
61 $services->{byname
}->{$1} = $services->{byid
}->{$2};
67 $etc_services = $services;
75 sub get_etc_protocols
{
76 return $etc_protocols if $etc_protocols;
78 my $filename = "/etc/protocols";
80 my $fh = IO
::File-
>new($filename, O_RDONLY
);
82 warn "unable to read '$filename' - $!\n";
88 while (my $line = <$fh>) {
90 next if $line =~m/^#/;
91 next if ($line =~m/^\s*$/);
93 if ($line =~ m!^(\S+)\s+(\d+)\s+.*$!) {
94 $protocols->{byid
}->{$2}->{name
} = $1;
95 $protocols->{byname
}->{$1} = $protocols->{byid
}->{$2};
101 $etc_protocols = $protocols;
103 return $etc_protocols;
106 sub parse_address_list
{
110 foreach my $aor (split(/,/, $str)) {
111 if (!Net
::IP-
>new($aor)) {
112 my $err = Net
::IP
::Error
();
113 die "invalid IP address: $err\n";
121 sub parse_port_name_number_or_range
{
124 my $services = PVE
::Firewall
::get_etc_services
();
126 foreach my $item (split(/,/, $str)) {
128 foreach my $pon (split(':', $item, 2)) {
129 if ($pon =~ m/^\d+$/){
130 die "invalid port '$pon'\n" if $pon < 0 && $pon > 65536;
132 die "invalid port $services->{byname}->{$pon}\n" if !$services->{byname
}->{$pon};
141 my $rule_format = "%-15s %-30s %-30s %-15s %-15s %-15s\n";
146 run_command
("/sbin/iptables $cmd", outfunc
=> sub {}, errfunc
=> sub {});
149 sub iptables_restore_cmdlist
{
152 my $verbose = 1; # fixme: how/when do we set this
154 #run_command("echo '$cmdlist' | /sbin/iptables-restore -n");
155 eval { run_command
("/sbin/iptables-restore -n ", input
=> $cmdlist); };
157 print STDERR
$cmdlist if $verbose;
162 sub iptables_restore
{
164 unshift (@ruleset, '*filter');
165 push (@ruleset, 'COMMIT');
167 my $cmdlist = join("\n", @ruleset) . "\n";
169 iptables_restore_cmdlist
($cmdlist);
172 # experimental code to read existing chains and compute SHA1 checksum
174 sub iptables_get_chains
{
178 # check what chains we want to track
179 my $is_pvefw_chain = sub {
182 return 1 if $name =~ m/^BRIDGEFW-(:?IN|OUT)$/;
183 return 1 if $name =~ m/^proxmoxfw-\S+$/;
184 return 1 if $name =~ m/^tap\d+i\d+-(:?IN|OUT)$/;
185 return 1 if $name =~ m/^vmbr\d+-(:?IN|OUT)$/;
197 return if $line =~ m/^#/;
198 return if $line =~ m/^\s*$/;
200 if ($line =~ m/^\*(\S+)$/) {
205 return if $table ne 'filter';
207 if ($line =~ m/^:(\S+)\s/) {
209 return if !&$is_pvefw_chain($chain);
210 $dhash->{$chain} = Digest
::SHA-
>new('sha1');
211 } elsif ($line =~ m/^-([A-Z]) (\S+)\s/) {
213 return if !&$is_pvefw_chain($chain);
214 my $sha = $dhash->{$chain} || die "undefined chain '$chain'";
215 $sha->add_bits("$line\n");
217 # simply ignore the rest
222 run_command
("/sbin/iptables-save", outfunc
=> $parser);
224 foreach my $chain (keys %$dhash) {
225 my $sha = $dhash->{$chain};
226 $res->{$chain} = $sha->b64digest;
232 sub iptables_addrule
{
235 push (@ruleset, $rule);
238 sub iptables_chain_exist
{
242 iptables
("-n --list $chain");
249 sub iptables_rule_exist
{
253 iptables
("-C $rule");
260 sub iptables_generate_rule
{
261 my ($chain, $rule) = @_;
263 my $cmd = "-A $chain";
265 $cmd .= " -m iprange --src-range" if $rule->{nbsource
} && $rule->{nbsource
} > 1;
266 $cmd .= " -s $rule->{source}" if $rule->{source
};
267 $cmd .= " -m iprange --dst-range" if $rule->{nbdest
} && $rule->{nbdest
} > 1;
268 $cmd .= " -d $rule->{dest}" if $rule->{destination
};
269 $cmd .= " -p $rule->{proto}" if $rule->{proto
};
270 $cmd .= " --match multiport" if $rule->{nbdport
} && $rule->{nbdport
} > 1;
271 $cmd .= " --dport $rule->{dport}" if $rule->{dport
};
272 $cmd .= " --match multiport" if $rule->{nbsport
} && $rule->{nbsport
} > 1;
273 $cmd .= " --sport $rule->{sport}" if $rule->{sport
};
274 $cmd .= " -j $rule->{action}" if $rule->{action
};
276 iptables_addrule
($cmd);
280 sub generate_bridge_rules
{
283 if(!iptables_chain_exist
("BRIDGEFW-OUT")){
284 iptables_addrule
(":BRIDGEFW-OUT - [0:0]");
287 if(!iptables_chain_exist
("BRIDGEFW-IN")){
288 iptables_addrule
(":BRIDGEFW-IN - [0:0]");
291 if(!iptables_chain_exist
("proxmoxfw-FORWARD")){
292 iptables_addrule
(":proxmoxfw-FORWARD - [0:0]");
293 iptables_addrule
("-I FORWARD -j proxmoxfw-FORWARD");
294 iptables_addrule
("-A proxmoxfw-FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT");
295 iptables_addrule
("-A proxmoxfw-FORWARD -m physdev --physdev-is-in --physdev-is-bridged -j BRIDGEFW-OUT");
296 iptables_addrule
("-A proxmoxfw-FORWARD -m physdev --physdev-is-out --physdev-is-bridged -j BRIDGEFW-IN");
300 generate_proxmoxfwinput
();
302 if(!iptables_chain_exist
("$bridge-IN")){
303 iptables_addrule
(":$bridge-IN - [0:0]");
304 iptables_addrule
("-A proxmoxfw-FORWARD -i $bridge -j DROP"); #disable interbridge routing
305 iptables_addrule
("-A BRIDGEFW-IN -j $bridge-IN");
306 iptables_addrule
("-A $bridge-IN -j ACCEPT");
310 if(!iptables_chain_exist
("$bridge-OUT")){
311 iptables_addrule
(":$bridge-OUT - [0:0]");
312 iptables_addrule
("-A proxmoxfw-FORWARD -o $bridge -j DROP"); # disable interbridge routing
313 iptables_addrule
("-A BRIDGEFW-OUT -j $bridge-OUT");
320 sub generate_tap_rules_direction
{
321 my ($iface, $netid, $rules, $bridge, $direction) = @_;
323 my $tapchain = "$iface-$direction";
325 iptables_addrule
(":$tapchain - [0:0]");
327 iptables_addrule
("-A $tapchain -m state --state INVALID -j DROP");
328 iptables_addrule
("-A $tapchain -m state --state RELATED,ESTABLISHED -j ACCEPT");
330 if (scalar(@$rules)) {
331 foreach my $rule (@$rules) {
332 next if $rule->{iface
} && $rule->{iface
} ne $netid;
333 if($rule->{action
} =~ m/^(GROUP-(\S+))$/){
334 $rule->{action
} .= "-$direction";
335 #generate empty group rule if don't exist
336 if(!iptables_chain_exist
($rule->{action
})){
337 generate_group_rules
($2);
340 #we go to vmbr-IN if accept in out rules
341 $rule->{action
} = "$bridge-IN" if $rule->{action
} eq 'ACCEPT' && $direction eq 'OUT';
342 iptables_generate_rule
($tapchain, $rule);
346 iptables_addrule
("-A $tapchain -j LOG --log-prefix \"$tapchain-dropped: \" --log-level 4");
347 iptables_addrule
("-A $tapchain -j DROP");
349 #plug the tap chain to bridge chain
350 my $physdevdirection = $direction eq 'IN' ?
"out":"in";
351 my $rule = "$bridge-$direction -m physdev --physdev-$physdevdirection $iface --physdev-is-bridged -j $tapchain";
353 if(!iptables_rule_exist
($rule)){
354 iptables_addrule
("-I $rule");
357 if($direction eq 'OUT'){
359 my $rule = "proxmoxfw-INPUT -m physdev --physdev-$physdevdirection $iface -j $tapchain";
361 if(!iptables_rule_exist
($rule)){
362 iptables_addrule
("-A $rule");
367 sub generate_tap_rules
{
368 my ($net, $netid, $vmid) = @_;
370 my $filename = "/etc/pve/firewall/$vmid.fw";
371 my $fh = IO
::File-
>new($filename, O_RDONLY
);
374 #generate bridge rules
375 my $bridge = $net->{bridge
};
376 my $tag = $net->{tag
};
377 $bridge .= "v$tag" if $tag;
380 my $rules = parse_fw_rules
($filename, $fh);
382 my $inrules = $rules->{in};
383 my $outrules = $rules->{out
};
385 my $iface = "tap".$vmid."i".$1 if $netid =~ m/net(\d+)/;
387 generate_bridge_rules
($bridge);
388 generate_tap_rules_direction
($iface, $netid, $inrules, $bridge, 'IN');
389 generate_tap_rules_direction
($iface, $netid, $outrules, $bridge, 'OUT');
393 sub flush_tap_rules
{
394 my ($net, $netid, $vmid) = @_;
396 my $bridge = $net->{bridge
};
397 my $iface = "tap".$vmid."i".$1 if $netid =~ m/net(\d+)/;
399 flush_tap_rules_direction
($iface, $bridge, 'IN');
400 flush_tap_rules_direction
($iface, $bridge, 'OUT');
404 sub flush_tap_rules_direction
{
405 my ($iface, $bridge, $direction) = @_;
407 my $tapchain = "$iface-$direction";
409 if(iptables_chain_exist
($tapchain)){
410 iptables_addrule
("-F $tapchain");
412 my $physdevdirection = $direction eq 'IN' ?
"out":"in";
413 my $rule = "$bridge-$direction -m physdev --physdev-$physdevdirection $iface --physdev-is-bridged -j $tapchain";
414 if(iptables_rule_exist
($rule)){
415 iptables_addrule
("-D $rule");
418 if($direction eq 'OUT'){
419 my $rule = "proxmoxfw-INPUT -m physdev --physdev-$physdevdirection $iface -j $tapchain";
420 if(iptables_rule_exist
($rule)){
421 iptables_addrule
("-D $rule");
425 iptables_addrule
("-X $tapchain");
431 generate_proxmoxfwinput
();
432 generate_proxmoxfwoutput
();
434 my $filename = "/etc/pve/local/host.fw";
435 my $fh = IO
::File-
>new($filename, O_RDONLY
);
438 my $rules = parse_fw_rules
($filename, $fh);
439 my $inrules = $rules->{in};
440 my $outrules = $rules->{out
};
442 #host inbound firewall
443 iptables_addrule
(":host-IN - [0:0]");
444 iptables_addrule
("-A host-IN -m state --state INVALID -j DROP");
445 iptables_addrule
("-A host-IN -m state --state RELATED,ESTABLISHED -j ACCEPT");
446 iptables_addrule
("-A host-IN -i lo -j ACCEPT");
447 iptables_addrule
("-A host-IN -m addrtype --dst-type MULTICAST -j ACCEPT");
448 iptables_addrule
("-A host-IN -p udp -m state --state NEW -m multiport --dports 5404,5405 -j ACCEPT");
449 iptables_addrule
("-A host-IN -p udp -m udp --dport 9000 -j ACCEPT"); #corosync
451 if (scalar(@$inrules)) {
452 foreach my $rule (@$inrules) {
453 #we use RETURN because we need to check also tap rules
454 $rule->{action
} = 'RETURN' if $rule->{action
} eq 'ACCEPT';
455 iptables_generate_rule
('host-IN', $rule);
459 iptables_addrule
("-A host-IN -j LOG --log-prefix \"kvmhost-IN dropped: \" --log-level 4");
460 iptables_addrule
("-A host-IN -j DROP");
462 #host outbound firewall
463 iptables_addrule
(":host-OUT - [0:0]");
464 iptables_addrule
("-A host-OUT -m state --state INVALID -j DROP");
465 iptables_addrule
("-A host-OUT -m state --state RELATED,ESTABLISHED -j ACCEPT");
466 iptables_addrule
("-A host-OUT -o lo -j ACCEPT");
467 iptables_addrule
("-A host-OUT -m addrtype --dst-type MULTICAST -j ACCEPT");
468 iptables_addrule
("-A host-OUT -p udp -m state --state NEW -m multiport --dports 5404,5405 -j ACCEPT");
469 iptables_addrule
("-A host-OUT -p udp -m udp --dport 9000 -j ACCEPT"); #corosync
471 if (scalar(@$outrules)) {
472 foreach my $rule (@$outrules) {
473 #we use RETURN because we need to check also tap rules
474 $rule->{action
} = 'RETURN' if $rule->{action
} eq 'ACCEPT';
475 iptables_generate_rule
('host-OUT', $rule);
479 iptables_addrule
("-A host-OUT -j LOG --log-prefix \"kvmhost-OUT dropped: \" --log-level 4");
480 iptables_addrule
("-A host-OUT -j DROP");
483 my $rule = "proxmoxfw-INPUT -j host-IN";
484 if(!iptables_rule_exist
($rule)){
485 iptables_addrule
("-I $rule");
488 $rule = "proxmoxfw-OUTPUT -j host-OUT";
489 if(!iptables_rule_exist
($rule)){
490 iptables_addrule
("-I $rule");
500 my $chain = "host-IN";
502 my $rule = "proxmoxfw-INPUT -j $chain";
503 if(iptables_rule_exist
($rule)){
504 iptables_addrule
("-D $rule");
507 if(iptables_chain_exist
($chain)){
508 iptables_addrule
("-F $chain");
509 iptables_addrule
("-X $chain");
514 $rule = "proxmoxfw-OUTPUT -j $chain";
515 if(iptables_rule_exist
($rule)){
516 iptables_addrule
("-D $rule");
519 if(iptables_chain_exist
($chain)){
520 iptables_addrule
("-F $chain");
521 iptables_addrule
("-X $chain");
527 sub generate_proxmoxfwinput
{
529 if(!iptables_chain_exist
("proxmoxfw-INPUT")){
530 iptables_addrule
(":proxmoxfw-INPUT - [0:0]");
531 iptables_addrule
("-I INPUT -j proxmoxfw-INPUT");
532 iptables_addrule
("-A INPUT -j ACCEPT");
536 sub generate_proxmoxfwoutput
{
538 if(!iptables_chain_exist
("proxmoxfw-OUTPUT")){
539 iptables_addrule
(":proxmoxfw-OUTPUT - [0:0]");
540 iptables_addrule
("-I OUTPUT -j proxmoxfw-OUTPUT");
541 iptables_addrule
("-A OUTPUT -j ACCEPT");
546 sub enable_group_rules
{
549 generate_group_rules
($group);
553 sub generate_group_rules
{
556 my $filename = "/etc/pve/firewall/groups.fw";
557 my $fh = IO
::File-
>new($filename, O_RDONLY
);
560 my $rules = parse_fw_rules
($filename, $fh, $group);
561 my $inrules = $rules->{in};
562 my $outrules = $rules->{out
};
564 my $chain = "GROUP-".$group."-IN";
566 iptables_addrule
(":$chain - [0:0]");
568 if (scalar(@$inrules)) {
569 foreach my $rule (@$inrules) {
570 iptables_generate_rule
($chain, $rule);
574 $chain = "GROUP-".$group."-OUT";
576 iptables_addrule
(":$chain - [0:0]");
578 if(!iptables_chain_exist
("BRIDGEFW-OUT")){
579 iptables_addrule
(":BRIDGEFW-OUT - [0:0]");
582 if(!iptables_chain_exist
("BRIDGEFW-IN")){
583 iptables_addrule
(":BRIDGEFW-IN - [0:0]");
586 if (scalar(@$outrules)) {
587 foreach my $rule (@$outrules) {
588 #we go the BRIDGEFW-IN because we need to check also other tap rules
589 #(and group rules can be set on any bridge, so we can't go to VMBRXX-IN)
590 $rule->{action
} = 'BRIDGEFW-IN' if $rule->{action
} eq 'ACCEPT';
591 iptables_generate_rule
($chain, $rule);
597 sub disable_group_rules
{
600 my $chain = "GROUP-".$group."-IN";
602 if(iptables_chain_exist
($chain)){
603 iptables_addrule
("-F $chain");
604 iptables_addrule
("-X $chain");
607 $chain = "GROUP-".$group."-OUT";
609 if(iptables_chain_exist
($chain)){
610 iptables_addrule
("-F $chain");
611 iptables_addrule
("-X $chain");
614 #iptables_restore will die if security group is linked in a tap chain
615 #maybe can we improve that, parsing each vm config, or parsing iptables -S
616 #to see if the security group is linked or not
621 my ($filename, $fh, $group) = @_;
625 my $securitygroupexist;
627 my $res = { in => [], out
=> [] };
629 my $macros = get_firewall_macros
();
630 my $protocols = get_etc_protocols
();
632 while (defined(my $line = <$fh>)) {
633 next if $line =~ m/^#/;
634 next if $line =~ m/^\s*$/;
636 if ($line =~ m/^\[(in|out)(:(\S+))?\]\s*$/i) {
638 $securitygroup = lc($3) if $3;
639 $securitygroupexist = 1 if $securitygroup && $securitygroup eq $group;
643 next if $group && $securitygroup ne $group;
645 my ($action, $iface, $source, $dest, $proto, $dport, $sport) =
649 warn "skip incomplete line\n";
654 if ($action =~ m/^(ACCEPT|DROP|REJECT|GROUP-(\S+))$/) {
656 } elsif ($action =~ m/^(\S+)\((ACCEPT|DROP|REJECT)\)$/) {
657 ($service, $action) = ($1, $2);
658 if (!$macros->{$service}) {
659 warn "unknown service '$service'\n";
663 warn "unknown action '$action'\n";
667 $iface = undef if $iface && $iface eq '-';
668 if ($iface && $iface !~ m/^(net0|net1|net2|net3|net4|net5)$/) {
669 warn "unknown interface '$iface'\n";
673 $proto = undef if $proto && $proto eq '-';
674 if ($proto && !(defined($protocols->{byname
}->{$proto}) ||
675 defined($protocols->{byid
}->{$proto}))) {
676 warn "unknown protokol '$proto'\n";
680 $source = undef if $source && $source eq '-';
681 $dest = undef if $dest && $dest eq '-';
683 $dport = undef if $dport && $dport eq '-';
684 $sport = undef if $sport && $sport eq '-';
687 my $nbsource = undef;
691 $nbsource = parse_address_list
($source) if $source;
692 $nbdest = parse_address_list
($dest) if $dest;
693 $nbdport = parse_port_name_number_or_range
($dport) if $dport;
694 $nbsport = parse_port_name_number_or_range
($sport) if $sport;
709 nbsource
=> $nbsource,
719 push @{$res->{$section}}, $rule;
722 die "security group $group don't exist" if $group && !$securitygroupexist;
727 my ($code, @param) = @_;
731 my $res = lock_file
($pve_fw_lock_filename, $timeout, $code, @param);
738 sub read_local_vm_config
{
744 my $list = PVE
::QemuServer
::config_list
();
746 foreach my $vmid (keys %$list) {
747 #next if !($vmid eq '100' || $vmid eq '102');
748 my $cfspath = PVE
::QemuServer
::cfs_config_path
($vmid);
749 if (my $conf = PVE
::Cluster
::cfs_read_file
($cfspath)) {
750 $qemu->{$vmid} = $conf;
754 my $vmdata = { openvz
=> $openvz, qemu
=> $qemu };
759 sub read_vm_firewall_rules
{
762 foreach my $vmid (keys %{$vmdata->{qemu
}}, keys %{$vmdata->{openvz
}}) {
763 my $filename = "/etc/pve/firewall/$vmid.fw";
764 my $fh = IO
::File-
>new($filename, O_RDONLY
);
767 $rules->{$vmid} = parse_fw_rules
($filename, $fh);
774 my $vmdata = read_local_vm_config
();
775 my $rules = read_vm_firewall_rules
($vmdata);
777 # print Dumper($vmdata);
782 sub compile_and_start
{