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
{
151 unshift (@ruleset, '*filter');
152 push (@ruleset, 'COMMIT');
154 my $cmdlist = join("\n", @ruleset) . "\n";
156 my $verbose = 1; # fixme: how/when do we set this
158 #run_command("echo '$cmdlist' | /sbin/iptables-restore -n");
159 eval { run_command
("/sbin/iptables-restore -n ", input
=> $cmdlist); };
161 print STDERR
$cmdlist if $verbose;
166 # experimental code to read existing chains and compute SHA1 checksum
168 sub iptables_get_chains
{
172 # check what chains we want to track
173 my $is_pvefw_chain = sub {
176 return 1 if $name =~ m/^BRIDGEFW-(:?IN|OUT)$/;
177 return 1 if $name =~ m/^proxmoxfw-\S+$/;
178 return 1 if $name =~ m/^tap\d+i\d+-(:?IN|OUT)$/;
190 return if $line =~ m/^#/;
191 return if $line =~ m/^\s*$/;
193 if ($line =~ m/^\*(\S+)$/) {
198 return if $table ne 'filter';
200 if ($line =~ m/^:(\S+)\s/) {
202 return if !&$is_pvefw_chain($chain);
203 $dhash->{$chain} = Digest
::SHA-
>new('sha1');
204 } elsif ($line =~ m/^-([A-Z]) (\S+)\s/) {
206 return if !&$is_pvefw_chain($chain);
207 my $sha = $dhash->{$chain} || die "undefined chain '$chain'";
208 $sha->add_bits("$line\n");
210 # simply ignore the rest
215 run_command
("/sbin/iptables-save", outfunc
=> $parser);
217 foreach my $chain (keys %$dhash) {
218 my $sha = $dhash->{$chain};
219 $res->{$chain} = $sha->b64digest;
225 sub iptables_addrule
{
228 push (@ruleset, $rule);
231 sub iptables_chain_exist
{
235 iptables
("-n --list $chain");
242 sub iptables_rule_exist
{
246 iptables
("-C $rule");
253 sub iptables_generate_rule
{
254 my ($chain, $rule) = @_;
256 my $cmd = "-A $chain";
258 $cmd .= " -m iprange --src-range" if $rule->{nbsource
} && $rule->{nbsource
} > 1;
259 $cmd .= " -s $rule->{source}" if $rule->{source
};
260 $cmd .= " -m iprange --dst-range" if $rule->{nbdest
} && $rule->{nbdest
} > 1;
261 $cmd .= " -d $rule->{dest}" if $rule->{destination
};
262 $cmd .= " -p $rule->{proto}" if $rule->{proto
};
263 $cmd .= " --match multiport" if $rule->{nbdport
} && $rule->{nbdport
} > 1;
264 $cmd .= " --dport $rule->{dport}" if $rule->{dport
};
265 $cmd .= " --match multiport" if $rule->{nbsport
} && $rule->{nbsport
} > 1;
266 $cmd .= " --sport $rule->{sport}" if $rule->{sport
};
267 $cmd .= " -j $rule->{action}" if $rule->{action
};
269 iptables_addrule
($cmd);
273 sub generate_bridge_rules
{
276 if(!iptables_chain_exist
("BRIDGEFW-OUT")){
277 iptables_addrule
(":BRIDGEFW-OUT - [0:0]");
280 if(!iptables_chain_exist
("BRIDGEFW-IN")){
281 iptables_addrule
(":BRIDGEFW-IN - [0:0]");
284 if(!iptables_chain_exist
("proxmoxfw-FORWARD")){
285 iptables_addrule
(":proxmoxfw-FORWARD - [0:0]");
286 iptables_addrule
("-I FORWARD -j proxmoxfw-FORWARD");
287 iptables_addrule
("-A proxmoxfw-FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT");
288 iptables_addrule
("-A proxmoxfw-FORWARD -m physdev --physdev-is-in --physdev-is-bridged -j BRIDGEFW-OUT");
289 iptables_addrule
("-A proxmoxfw-FORWARD -m physdev --physdev-is-out --physdev-is-bridged -j BRIDGEFW-IN");
293 generate_proxmoxfwinput
();
295 if(!iptables_chain_exist
("$bridge-IN")){
296 iptables_addrule
(":$bridge-IN - [0:0]");
297 iptables_addrule
("-A proxmoxfw-FORWARD -i $bridge -j DROP"); #disable interbridge routing
298 iptables_addrule
("-A BRIDGEFW-IN -j $bridge-IN");
299 iptables_addrule
("-A $bridge-IN -j ACCEPT");
303 if(!iptables_chain_exist
("$bridge-OUT")){
304 iptables_addrule
(":$bridge-OUT - [0:0]");
305 iptables_addrule
("-A proxmoxfw-FORWARD -o $bridge -j DROP"); # disable interbridge routing
306 iptables_addrule
("-A BRIDGEFW-OUT -j $bridge-OUT");
313 sub generate_tap_rules_direction
{
314 my ($iface, $netid, $rules, $bridge, $direction) = @_;
316 my $tapchain = "$iface-$direction";
318 iptables_addrule
(":$tapchain - [0:0]");
320 iptables_addrule
("-A $tapchain -m state --state INVALID -j DROP");
321 iptables_addrule
("-A $tapchain -m state --state RELATED,ESTABLISHED -j ACCEPT");
323 if (scalar(@$rules)) {
324 foreach my $rule (@$rules) {
325 next if $rule->{iface
} && $rule->{iface
} ne $netid;
326 if($rule->{action
} =~ m/^(GROUP-(\S+))$/){
327 $rule->{action
} .= "-$direction";
328 #generate empty group rule if don't exist
329 if(!iptables_chain_exist
($rule->{action
})){
330 generate_group_rules
($2);
333 #we go to vmbr-IN if accept in out rules
334 $rule->{action
} = "$bridge-IN" if $rule->{action
} eq 'ACCEPT' && $direction eq 'OUT';
335 iptables_generate_rule
($tapchain, $rule);
339 iptables_addrule
("-A $tapchain -j LOG --log-prefix \"$tapchain-dropped: \" --log-level 4");
340 iptables_addrule
("-A $tapchain -j DROP");
342 #plug the tap chain to bridge chain
343 my $physdevdirection = $direction eq 'IN' ?
"out":"in";
344 my $rule = "$bridge-$direction -m physdev --physdev-$physdevdirection $iface --physdev-is-bridged -j $tapchain";
346 if(!iptables_rule_exist
($rule)){
347 iptables_addrule
("-I $rule");
350 if($direction eq 'OUT'){
352 my $rule = "proxmoxfw-INPUT -m physdev --physdev-$physdevdirection $iface -j $tapchain";
354 if(!iptables_rule_exist
($rule)){
355 iptables_addrule
("-A $rule");
360 sub generate_tap_rules
{
361 my ($net, $netid, $vmid) = @_;
363 my $filename = "/etc/pve/firewall/$vmid.fw";
364 my $fh = IO
::File-
>new($filename, O_RDONLY
);
367 #generate bridge rules
368 my $bridge = $net->{bridge
};
369 my $tag = $net->{tag
};
370 $bridge .= "v$tag" if $tag;
373 my $rules = parse_fw_rules
($filename, $fh);
375 my $inrules = $rules->{in};
376 my $outrules = $rules->{out
};
378 my $iface = "tap".$vmid."i".$1 if $netid =~ m/net(\d+)/;
380 generate_bridge_rules
($bridge);
381 generate_tap_rules_direction
($iface, $netid, $inrules, $bridge, 'IN');
382 generate_tap_rules_direction
($iface, $netid, $outrules, $bridge, 'OUT');
386 sub flush_tap_rules
{
387 my ($net, $netid, $vmid) = @_;
389 my $bridge = $net->{bridge
};
390 my $iface = "tap".$vmid."i".$1 if $netid =~ m/net(\d+)/;
392 flush_tap_rules_direction
($iface, $bridge, 'IN');
393 flush_tap_rules_direction
($iface, $bridge, 'OUT');
397 sub flush_tap_rules_direction
{
398 my ($iface, $bridge, $direction) = @_;
400 my $tapchain = "$iface-$direction";
402 if(iptables_chain_exist
($tapchain)){
403 iptables_addrule
("-F $tapchain");
405 my $physdevdirection = $direction eq 'IN' ?
"out":"in";
406 my $rule = "$bridge-$direction -m physdev --physdev-$physdevdirection $iface --physdev-is-bridged -j $tapchain";
407 if(iptables_rule_exist
($rule)){
408 iptables_addrule
("-D $rule");
411 if($direction eq 'OUT'){
412 my $rule = "proxmoxfw-INPUT -m physdev --physdev-$physdevdirection $iface -j $tapchain";
413 if(iptables_rule_exist
($rule)){
414 iptables_addrule
("-D $rule");
418 iptables_addrule
("-X $tapchain");
424 generate_proxmoxfwinput
();
425 generate_proxmoxfwoutput
();
427 my $filename = "/etc/pve/local/host.fw";
428 my $fh = IO
::File-
>new($filename, O_RDONLY
);
431 my $rules = parse_fw_rules
($filename, $fh);
432 my $inrules = $rules->{in};
433 my $outrules = $rules->{out
};
435 #host inbound firewall
436 iptables_addrule
(":host-IN - [0:0]");
437 iptables_addrule
("-A host-IN -m state --state INVALID -j DROP");
438 iptables_addrule
("-A host-IN -m state --state RELATED,ESTABLISHED -j ACCEPT");
439 iptables_addrule
("-A host-IN -i lo -j ACCEPT");
440 iptables_addrule
("-A host-IN -m addrtype --dst-type MULTICAST -j ACCEPT");
441 iptables_addrule
("-A host-IN -p udp -m state --state NEW -m multiport --dports 5404,5405 -j ACCEPT");
442 iptables_addrule
("-A host-IN -p udp -m udp --dport 9000 -j ACCEPT"); #corosync
444 if (scalar(@$inrules)) {
445 foreach my $rule (@$inrules) {
446 #we use RETURN because we need to check also tap rules
447 $rule->{action
} = 'RETURN' if $rule->{action
} eq 'ACCEPT';
448 iptables_generate_rule
('host-IN', $rule);
452 iptables_addrule
("-A host-IN -j LOG --log-prefix \"kvmhost-IN dropped: \" --log-level 4");
453 iptables_addrule
("-A host-IN -j DROP");
455 #host outbound firewall
456 iptables_addrule
(":host-OUT - [0:0]");
457 iptables_addrule
("-A host-OUT -m state --state INVALID -j DROP");
458 iptables_addrule
("-A host-OUT -m state --state RELATED,ESTABLISHED -j ACCEPT");
459 iptables_addrule
("-A host-OUT -o lo -j ACCEPT");
460 iptables_addrule
("-A host-OUT -m addrtype --dst-type MULTICAST -j ACCEPT");
461 iptables_addrule
("-A host-OUT -p udp -m state --state NEW -m multiport --dports 5404,5405 -j ACCEPT");
462 iptables_addrule
("-A host-OUT -p udp -m udp --dport 9000 -j ACCEPT"); #corosync
464 if (scalar(@$outrules)) {
465 foreach my $rule (@$outrules) {
466 #we use RETURN because we need to check also tap rules
467 $rule->{action
} = 'RETURN' if $rule->{action
} eq 'ACCEPT';
468 iptables_generate_rule
('host-OUT', $rule);
472 iptables_addrule
("-A host-OUT -j LOG --log-prefix \"kvmhost-OUT dropped: \" --log-level 4");
473 iptables_addrule
("-A host-OUT -j DROP");
476 my $rule = "proxmoxfw-INPUT -j host-IN";
477 if(!iptables_rule_exist
($rule)){
478 iptables_addrule
("-I $rule");
481 $rule = "proxmoxfw-OUTPUT -j host-OUT";
482 if(!iptables_rule_exist
($rule)){
483 iptables_addrule
("-I $rule");
493 my $chain = "host-IN";
495 my $rule = "proxmoxfw-INPUT -j $chain";
496 if(iptables_rule_exist
($rule)){
497 iptables_addrule
("-D $rule");
500 if(iptables_chain_exist
($chain)){
501 iptables_addrule
("-F $chain");
502 iptables_addrule
("-X $chain");
507 $rule = "proxmoxfw-OUTPUT -j $chain";
508 if(iptables_rule_exist
($rule)){
509 iptables_addrule
("-D $rule");
512 if(iptables_chain_exist
($chain)){
513 iptables_addrule
("-F $chain");
514 iptables_addrule
("-X $chain");
520 sub generate_proxmoxfwinput
{
522 if(!iptables_chain_exist
("proxmoxfw-INPUT")){
523 iptables_addrule
(":proxmoxfw-INPUT - [0:0]");
524 iptables_addrule
("-I INPUT -j proxmoxfw-INPUT");
525 iptables_addrule
("-A INPUT -j ACCEPT");
529 sub generate_proxmoxfwoutput
{
531 if(!iptables_chain_exist
("proxmoxfw-OUTPUT")){
532 iptables_addrule
(":proxmoxfw-OUTPUT - [0:0]");
533 iptables_addrule
("-I OUTPUT -j proxmoxfw-OUTPUT");
534 iptables_addrule
("-A OUTPUT -j ACCEPT");
539 sub enable_group_rules
{
542 generate_group_rules
($group);
546 sub generate_group_rules
{
549 my $filename = "/etc/pve/firewall/groups.fw";
550 my $fh = IO
::File-
>new($filename, O_RDONLY
);
553 my $rules = parse_fw_rules
($filename, $fh, $group);
554 my $inrules = $rules->{in};
555 my $outrules = $rules->{out
};
557 my $chain = "GROUP-".$group."-IN";
559 iptables_addrule
(":$chain - [0:0]");
561 if (scalar(@$inrules)) {
562 foreach my $rule (@$inrules) {
563 iptables_generate_rule
($chain, $rule);
567 $chain = "GROUP-".$group."-OUT";
569 iptables_addrule
(":$chain - [0:0]");
571 if(!iptables_chain_exist
("BRIDGEFW-OUT")){
572 iptables_addrule
(":BRIDGEFW-OUT - [0:0]");
575 if(!iptables_chain_exist
("BRIDGEFW-IN")){
576 iptables_addrule
(":BRIDGEFW-IN - [0:0]");
579 if (scalar(@$outrules)) {
580 foreach my $rule (@$outrules) {
581 #we go the BRIDGEFW-IN because we need to check also other tap rules
582 #(and group rules can be set on any bridge, so we can't go to VMBRXX-IN)
583 $rule->{action
} = 'BRIDGEFW-IN' if $rule->{action
} eq 'ACCEPT';
584 iptables_generate_rule
($chain, $rule);
590 sub disable_group_rules
{
593 my $chain = "GROUP-".$group."-IN";
595 if(iptables_chain_exist
($chain)){
596 iptables_addrule
("-F $chain");
597 iptables_addrule
("-X $chain");
600 $chain = "GROUP-".$group."-OUT";
602 if(iptables_chain_exist
($chain)){
603 iptables_addrule
("-F $chain");
604 iptables_addrule
("-X $chain");
607 #iptables_restore will die if security group is linked in a tap chain
608 #maybe can we improve that, parsing each vm config, or parsing iptables -S
609 #to see if the security group is linked or not
614 my ($filename, $fh, $group) = @_;
618 my $securitygroupexist;
620 my $res = { in => [], out
=> [] };
622 my $macros = get_firewall_macros
();
623 my $protocols = get_etc_protocols
();
625 while (defined(my $line = <$fh>)) {
626 next if $line =~ m/^#/;
627 next if $line =~ m/^\s*$/;
629 if ($line =~ m/^\[(in|out)(:(\S+))?\]\s*$/i) {
631 $securitygroup = lc($3) if $3;
632 $securitygroupexist = 1 if $securitygroup && $securitygroup eq $group;
636 next if $group && $securitygroup ne $group;
638 my ($action, $iface, $source, $dest, $proto, $dport, $sport) =
642 warn "skip incomplete line\n";
647 if ($action =~ m/^(ACCEPT|DROP|REJECT|GROUP-(\S+))$/) {
649 } elsif ($action =~ m/^(\S+)\((ACCEPT|DROP|REJECT)\)$/) {
650 ($service, $action) = ($1, $2);
651 if (!$macros->{$service}) {
652 warn "unknown service '$service'\n";
656 warn "unknown action '$action'\n";
660 $iface = undef if $iface && $iface eq '-';
661 if ($iface && $iface !~ m/^(net0|net1|net2|net3|net4|net5)$/) {
662 warn "unknown interface '$iface'\n";
666 $proto = undef if $proto && $proto eq '-';
667 if ($proto && !(defined($protocols->{byname
}->{$proto}) ||
668 defined($protocols->{byid
}->{$proto}))) {
669 warn "unknown protokol '$proto'\n";
673 $source = undef if $source && $source eq '-';
674 $dest = undef if $dest && $dest eq '-';
676 $dport = undef if $dport && $dport eq '-';
677 $sport = undef if $sport && $sport eq '-';
680 my $nbsource = undef;
684 $nbsource = parse_address_list
($source) if $source;
685 $nbdest = parse_address_list
($dest) if $dest;
686 $nbdport = parse_port_name_number_or_range
($dport) if $dport;
687 $nbsport = parse_port_name_number_or_range
($sport) if $sport;
702 nbsource
=> $nbsource,
712 push @{$res->{$section}}, $rule;
715 die "security group $group don't exist" if $group && !$securitygroupexist;
720 my ($code, @param) = @_;
724 my $res = lock_file
($pve_fw_lock_filename, $timeout, $code, @param);
731 sub read_local_vm_config
{
737 my $list = PVE
::QemuServer
::config_list
();
739 foreach my $vmid (keys %$list) {
740 #next if !($vmid eq '100' || $vmid eq '102');
741 my $cfspath = PVE
::QemuServer
::cfs_config_path
($vmid);
742 if (my $conf = PVE
::Cluster
::cfs_read_file
($cfspath)) {
743 $qemu->{$vmid} = $conf;
747 my $vmdata = { openvz
=> $openvz, qemu
=> $qemu };
752 sub read_vm_firewall_rules
{
755 foreach my $vmid (keys %{$vmdata->{qemu
}}, keys %{$vmdata->{openvz
}}) {
756 my $filename = "/etc/pve/firewall/$vmid.fw";
757 my $fh = IO
::File-
>new($filename, O_RDONLY
);
760 $rules->{$vmid} = parse_fw_rules
($filename, $fh);
767 my $vmdata = read_local_vm_config
();
768 my $rules = read_vm_firewall_rules
($vmdata);
770 # print Dumper($vmdata);
775 sub compile_and_start
{