]> git.proxmox.com Git - pve-firewall.git/blobdiff - PVE/Firewall.pm
consider security group chains in iptables_get_chains
[pve-firewall.git] / PVE / Firewall.pm
index 2b8842bd26ea45ba71db950279180e9b2a9b0856..50fd20e9dda5597d1561b32d0ba1eeb95877351d 100644 (file)
@@ -3,27 +3,34 @@ package PVE::Firewall;
 use warnings;
 use strict;
 use Data::Dumper;
+use Digest::SHA;
 use PVE::Tools;
 use PVE::QemuServer;
 use File::Path;
 use IO::File;
 use Net::IP;
-use PVE::Tools qw(run_command);
+use PVE::Tools qw(run_command lock_file);
 
 use Data::Dumper;
 
+my $pve_fw_lock_filename = "/var/lock/pvefw.lck";
+
 my $macros;
 my @ruleset = ();
 
-sub get_shorewall_macros {
+# todo: implement some kind of MACROS, like shorewall /usr/share/shorewall/macro.*
+sub get_firewall_macros {
 
     return $macros if $macros;
 
-    foreach my $path (</usr/share/shorewall/macro.*>) {
-       if ($path =~ m|/macro\.(\S+)$|) {
-           $macros->{$1} = 1;
-       }
-    }
+    #foreach my $path (</usr/share/shorewall/macro.*>) {
+    #  if ($path =~ m|/macro\.(\S+)$|) {
+    #    $macros->{$1} = 1;
+    #  }
+    #}
+
+    $macros = {}; # fixme: implemet me
+
     return $macros;
 }
 
@@ -99,12 +106,16 @@ sub get_etc_protocols {
 sub parse_address_list {
     my ($str) = @_;
 
+    my $nbaor = 0;
     foreach my $aor (split(/,/, $str)) {
        if (!Net::IP->new($aor)) {
            my $err = Net::IP::Error();
            die "invalid IP address: $err\n";
+       }else{
+           $nbaor++;
        }
     }
+    return $nbaor;
 }
 
 sub parse_port_name_number_or_range {
@@ -135,14 +146,88 @@ sub iptables {
     run_command("/sbin/iptables $cmd", outfunc => sub {}, errfunc => sub {});
 }
 
+sub iptables_restore_cmdlist {
+    my ($cmdlist) = @_;
+
+    my $verbose = 1; # fixme: how/when do we set this
+
+    #run_command("echo '$cmdlist' | /sbin/iptables-restore -n");
+    eval { run_command("/sbin/iptables-restore -n ", input => $cmdlist); };
+    if (my $err = $@) {
+       print STDERR $cmdlist if $verbose;
+       die $err;
+    }
+}
+
 sub iptables_restore {
 
     unshift (@ruleset, '*filter');
     push (@ruleset, 'COMMIT');
 
-    my $cmdlist = join("\n", @ruleset);
+    my $cmdlist = join("\n", @ruleset) . "\n";
 
-    run_command("echo '$cmdlist' | /sbin/iptables-restore -n", outfunc => sub {});
+    iptables_restore_cmdlist($cmdlist);
+}
+
+# experimental code to read existing chains and compute SHA1 checksum
+# for each chain. 
+sub iptables_get_chains {
+
+    my $res = {};
+
+    # check what chains we want to track
+    my $is_pvefw_chain = sub {
+       my $name = shift;
+
+       return 1 if $name =~ m/^BRIDGEFW-(:?IN|OUT)$/;
+       return 1 if $name =~ m/^proxmoxfw-\S+$/;
+       return 1 if $name =~ m/^tap\d+i\d+-(:?IN|OUT)$/;
+       return 1 if $name =~ m/^vmbr\d+-(:?IN|OUT)$/;
+       return 1 if $name =~ m/^GROUP-(:?[^\s\-]+)-(:?IN|OUT)$/;
+
+       return undef;
+    };
+
+    my $table = '';
+
+    my $dhash = {};
+
+    my $parser = sub {
+       my $line = shift;
+
+       return if $line =~ m/^#/;
+       return if $line =~ m/^\s*$/;
+
+       if ($line =~ m/^\*(\S+)$/) {
+           $table = $1;
+           return;
+       }
+
+       return if $table ne 'filter';
+
+       if ($line =~ m/^:(\S+)\s/) {
+           my $chain = $1;
+           return if !&$is_pvefw_chain($chain);
+           $dhash->{$chain} = Digest::SHA->new('sha1');
+       } elsif ($line =~ m/^-([A-Z]) (\S+)\s/) {
+           my $chain = $2;
+           return if !&$is_pvefw_chain($chain);
+           my $sha = $dhash->{$chain} || die "undefined chain '$chain'";
+           $sha->add_bits("$line\n");
+       } else {
+           # simply ignore the rest
+           return;
+       }
+    };
+
+    run_command("/sbin/iptables-save", outfunc => $parser);
+
+    foreach my $chain (keys %$dhash) {
+       my $sha = $dhash->{$chain};
+       $res->{$chain} = $sha->b64digest;
+    }
+
+    return $res;
 }
 
 sub iptables_addrule {
@@ -178,7 +263,9 @@ sub iptables_generate_rule {
 
     my $cmd = "-A $chain";
 
+    $cmd .= " -m iprange --src-range" if $rule->{nbsource} && $rule->{nbsource} > 1;
     $cmd .= " -s $rule->{source}" if $rule->{source};
+    $cmd .= " -m iprange --dst-range" if $rule->{nbdest} && $rule->{nbdest} > 1;
     $cmd .= " -d $rule->{dest}" if $rule->{destination};
     $cmd .= " -p $rule->{proto}" if $rule->{proto};
     $cmd .= "  --match multiport" if $rule->{nbdport} && $rule->{nbdport} > 1;
@@ -331,8 +418,7 @@ sub flush_tap_rules_direction {
 
        if($direction eq 'OUT'){
            my $rule = "proxmoxfw-INPUT -m physdev --physdev-$physdevdirection $iface -j $tapchain";
-
-           if(!iptables_rule_exist($rule)){
+           if(iptables_rule_exist($rule)){
                iptables_addrule("-D $rule");
            }
        }
@@ -341,337 +427,221 @@ sub flush_tap_rules_direction {
     }
 }
 
-my $generate_input_rule = sub {
-    my ($zoneinfo, $rule, $net, $netid) = @_;
+sub enablehostfw {
 
-    my $zone = $net->{zone} || die "internal error";
-    my $zid = $zoneinfo->{$zone}->{zoneref} || die "internal error";
-    my $tap = $net->{tap} || die "internal error";
-
-    my $dest = "$zid:$tap";
-
-    if ($rule->{dest}) {
-       $dest .= ":$rule->{dest}";
-    }
-
-    my $action = $rule->{service} ? 
-       "$rule->{service}($rule->{action})" : $rule->{action};
+    generate_proxmoxfwinput();
+    generate_proxmoxfwoutput();
 
-    my $sources = [];
+    my $filename = "/etc/pve/local/host.fw";
+    my $fh = IO::File->new($filename, O_RDONLY);
+    return if !$fh;
 
-    if (!$rule->{source}) {
-       push @$sources, 'all'; 
-    } elsif ($zoneinfo->{$zone}->{type} eq 'bport') {
-       my $bridge_zone = $zoneinfo->{$zone}->{bridge_zone} || die "internal error";
-       my $zoneref = $zoneinfo->{$bridge_zone}->{zoneref} || die "internal error";
+    my $rules = parse_fw_rules($filename, $fh);
+    my $inrules = $rules->{in};
+    my $outrules = $rules->{out};
 
-       # using 'all' does not work, so we create one rule for
-       # each related zone on the same bridge
-       push @$sources, "${zoneref}:$rule->{source}";
-       foreach my $z (keys %$zoneinfo) {
-           next if $z eq $zone;
-           next if !$zoneinfo->{$z}->{bridge_zone};
-           next if $zoneinfo->{$z}->{bridge_zone} ne $bridge_zone;
-           $zoneref = $zoneinfo->{$z}->{zoneref} || die "internal error";
-           push @$sources, "${zoneref}:$rule->{source}";
-       }
-    } else {
-       push @$sources, "all:$rule->{source}";
+    #host inbound firewall
+    iptables_addrule(":host-IN - [0:0]");
+    iptables_addrule("-A host-IN -m state --state INVALID -j DROP");
+    iptables_addrule("-A host-IN -m state --state RELATED,ESTABLISHED -j ACCEPT");
+    iptables_addrule("-A host-IN -i lo -j ACCEPT");
+    iptables_addrule("-A host-IN -m addrtype --dst-type MULTICAST -j ACCEPT");
+    iptables_addrule("-A host-IN -p udp -m state --state NEW -m multiport --dports 5404,5405 -j ACCEPT");
+    iptables_addrule("-A host-IN -p udp -m udp --dport 9000 -j ACCEPT"); #corosync
+
+    if (scalar(@$inrules)) {
+        foreach my $rule (@$inrules) {
+            #we use RETURN because we need to check also tap rules
+            $rule->{action} = 'RETURN' if $rule->{action} eq 'ACCEPT';
+            iptables_generate_rule('host-IN', $rule);
+        }
     }
 
-    my $out = '';
-
-    foreach my $source (@$sources) {
-       $out .= sprintf($rule_format, $action, $source, $dest, $rule->{proto} || '-', 
-                       $rule->{dport} || '-', $rule->{sport} || '-');
+    iptables_addrule("-A host-IN -j LOG --log-prefix \"kvmhost-IN dropped: \" --log-level 4");
+    iptables_addrule("-A host-IN -j DROP");
+
+    #host outbound firewall
+    iptables_addrule(":host-OUT - [0:0]");
+    iptables_addrule("-A host-OUT -m state --state INVALID -j DROP");
+    iptables_addrule("-A host-OUT -m state --state RELATED,ESTABLISHED -j ACCEPT");
+    iptables_addrule("-A host-OUT -o lo -j ACCEPT");
+    iptables_addrule("-A host-OUT -m addrtype --dst-type MULTICAST -j ACCEPT");
+    iptables_addrule("-A host-OUT -p udp -m state --state NEW -m multiport --dports 5404,5405 -j ACCEPT");
+    iptables_addrule("-A host-OUT -p udp -m udp --dport 9000 -j ACCEPT"); #corosync
+
+    if (scalar(@$outrules)) {
+        foreach my $rule (@$outrules) {
+            #we use RETURN because we need to check also tap rules
+            $rule->{action} = 'RETURN' if $rule->{action} eq 'ACCEPT';
+            iptables_generate_rule('host-OUT', $rule);
+        }
     }
 
-    return $out;
-};
-
-my $generate_output_rule = sub {
-    my ($zoneinfo, $rule, $net, $netid) = @_;
+    iptables_addrule("-A host-OUT -j LOG --log-prefix \"kvmhost-OUT dropped: \" --log-level 4");
+    iptables_addrule("-A host-OUT -j DROP");
 
-    my $zone = $net->{zone} || die "internal error";
-    my $zid = $zoneinfo->{$zone}->{zoneref} || die "internal error";
-    my $tap = $net->{tap} || die "internal error";
-
-    my $action = $rule->{service} ? 
-       "$rule->{service}($rule->{action})" : $rule->{action};
     
-    my $dest;
-
-    if (!$rule->{dest}) {
-       $dest = 'all';
-    } else {
-       $dest = "all:$rule->{dest}";
+    my $rule = "proxmoxfw-INPUT -j host-IN";
+    if(!iptables_rule_exist($rule)){
+       iptables_addrule("-I $rule");
     }
 
-    return sprintf($rule_format, $action, "$zid:$tap", $dest, 
-                  $rule->{proto} || '-', $rule->{dport} || '-', $rule->{sport} || '-');
-};
-
-# we need complete VM configuration of all VMs (openvz/qemu)
-# in vmdata
-
-my $compile_shorewall = sub {
-    my ($targetdir, $vmdata, $rules) = @_;
-
-    # remove existing data ?
-    foreach my $file (qw(params zones rules interfaces  maclist  policy)) {
-       unlink "$targetdir/$file";
+    $rule = "proxmoxfw-OUTPUT -j host-OUT";
+    if(!iptables_rule_exist($rule)){
+       iptables_addrule("-I $rule");
     }
 
-    my $netinfo;
-
-    my $zoneinfo = {
-       fw => { type => 'firewall' },
-    };
-
-    my $maclist = {};
-
-    my $register_bridge;
-
-    $register_bridge = sub {
-       my ($bridge, $vlan) = @_;
-
-       my $zone =  $bridge;
-
-       return $zone if $zoneinfo->{$zone};
+    iptables_restore();
 
-       my $ext_zone = "${bridge}_ext";
 
-       $zoneinfo->{$zone} = {
-           type => 'bridge',
-           bridge => $bridge,
-           bridge_ext_zone => $ext_zone,
-       };
-
-       # physical input devices
-       my $dir = "/sys/class/net/$bridge/brif";
-       my $physical = {};
-       PVE::Tools::dir_glob_foreach($dir, '((eth|bond).+)', sub {
-           my ($slave) = @_;
-           $physical->{$slave} = 1;
-       });
-
-       $zoneinfo->{$ext_zone} = {
-           type => 'bport',
-           bridge_zone => $zone,
-           ifaces => $physical,
-       };
-
-       return &$register_bridge("${bridge}v${vlan}") if defined($vlan);
-       
-       return $zone;
-    };
+}
 
-    my $register_bridge_port = sub {
-       my ($bridge, $vlan, $vmzone, $tap) = @_;
+sub disablehostfw {
 
-       my $bridge_zone = &$register_bridge($bridge, $vlan);
-       my $zone = $bridge_zone . '_' . $vmzone;
+    my $chain = "host-IN";
 
-       if (!$zoneinfo->{$zone}) {
-           $zoneinfo->{$zone} = {
-               type => 'bport',
-               bridge_zone => $bridge_zone,
-               ifaces => {},
-           };
-       }
-
-       $zoneinfo->{$zone}->{ifaces}->{$tap} = 1;
-       
-       return $zone;
-    };
+    my $rule = "proxmoxfw-INPUT -j $chain";
+    if(iptables_rule_exist($rule)){
+       iptables_addrule("-D $rule");
+    }
 
-    foreach my $vmid (keys %{$vmdata->{qemu}}) {
-       $netinfo->{$vmid} = {};
-       my $conf = $vmdata->{qemu}->{$vmid};
-       foreach my $opt (keys %$conf) {
-           next if $opt !~ m/^net(\d+)$/;
-           my $netnum = $1;
-           my $net = PVE::QemuServer::parse_net($conf->{$opt});
-           next if !$net;
-           die "implement me" if !$net->{bridge};
-
-           my $vmzone = $conf->{zone} || "vm$vmid";
-           $net->{tap} = "tap${vmid}i${netnum}";
-           $maclist->{$net->{tap}} = $net->{macaddr} || die "internal error";
-           $net->{zone} = &$register_bridge_port($net->{bridge}, $net->{tag}, $vmzone, $net->{tap});
-           $netinfo->{$vmid}->{$opt} = $net;
-       }
+    if(iptables_chain_exist($chain)){
+       iptables_addrule("-F $chain");
+       iptables_addrule("-X $chain");
     }
 
-    #print Dumper($netinfo);
+    $chain = "host-OUT";
 
-    # NOTE: zone names have length limit, so we need to
-    # translate them into shorter names 
+    $rule = "proxmoxfw-OUTPUT -j $chain";
+    if(iptables_rule_exist($rule)){
+       iptables_addrule("-D $rule");
+    }
 
-    my $zoneid = 0;
-    my $zonemap = { fw => 'fw' };
+    if(iptables_chain_exist($chain)){
+       iptables_addrule("-F $chain");
+       iptables_addrule("-X $chain");
+    }
 
-    my $lookup_zonename = sub {
-       my ($zone) = @_;
+    iptables_restore();   
+}
 
-       return $zonemap->{$zone} if defined($zonemap->{$zone});
-       $zonemap->{$zone} = 'z' . $zoneid++;
+sub generate_proxmoxfwinput {
 
-       return $zonemap->{$zone};
-    };
-
-    foreach my $z (sort keys %$zoneinfo) {
-       $zoneinfo->{$z}->{id} = &$lookup_zonename($z);
-       $zoneinfo->{$z}->{zonevar} = uc($z);
-       $zoneinfo->{$z}->{zoneref} = '$' . $zoneinfo->{$z}->{zonevar};
+    if(!iptables_chain_exist("proxmoxfw-INPUT")){
+        iptables_addrule(":proxmoxfw-INPUT - [0:0]");
+        iptables_addrule("-I INPUT -j proxmoxfw-INPUT");
+        iptables_addrule("-A INPUT -j ACCEPT");
     }
+}
 
-    my $out;
+sub generate_proxmoxfwoutput {
 
-    # dump params file
-    $out = "# PVE zones\n";
-    foreach my $z (sort keys %$zoneinfo) {
-       $out .= "$zoneinfo->{$z}->{zonevar}=$zoneinfo->{$z}->{id}\n";
+    if(!iptables_chain_exist("proxmoxfw-OUTPUT")){
+        iptables_addrule(":proxmoxfw-OUTPUT - [0:0]");
+        iptables_addrule("-I OUTPUT -j proxmoxfw-OUTPUT");
+        iptables_addrule("-A OUTPUT -j ACCEPT");
     }
-    PVE::Tools::file_set_contents("$targetdir/params", $out);
 
-    # dump zone file
+}
 
-    my $format = "%-30s %-10s %-15s\n";
-    $out = sprintf($format, '#ZONE', 'TYPE', 'OPTIONS');
+sub enable_group_rules {
+    my ($group) = @_;
     
-    foreach my $z (sort keys %$zoneinfo) {
-       my $zid = $zoneinfo->{$z}->{zoneref};
-       if ($zoneinfo->{$z}->{type} eq 'firewall') {
-           $out .= sprintf($format, $zid, $zoneinfo->{$z}->{type}, '');
-       } elsif ($zoneinfo->{$z}->{type} eq 'bridge') {
-           $out .= sprintf($format, $zid, 'ipv4', '');
-       } elsif ($zoneinfo->{$z}->{type} eq 'bport') {
-           my $bridge_zone = $zoneinfo->{$z}->{bridge_zone} || die "internal error";
-           my $bzid = $zoneinfo->{$bridge_zone}->{zoneref} || die "internal error";
-           $out .= sprintf($format, "$zid:$bzid", 'bport', '');
-       } else {
-           die "internal error";
-       }
-    }
-
-    $out .= sprintf("#LAST LINE - ADD YOUR ENTRIES ABOVE THIS ONE - DO NOT REMOVE\n");
+    generate_group_rules($group);
+    iptables_restore();
+}
 
-    PVE::Tools::file_set_contents("$targetdir/zones", $out);
+sub generate_group_rules {
+    my ($group) = @_;
 
-    # dump interfaces
+    my $filename = "/etc/pve/firewall/groups.fw";
+    my $fh = IO::File->new($filename, O_RDONLY);
+    return if !$fh;
 
-    $format = "%-25s %-20s %-10s %-15s\n";
-    $out = sprintf($format, '#ZONE', 'INTERFACE', 'BROADCAST', 'OPTIONS');
+    my $rules = parse_fw_rules($filename, $fh, $group);
+    my $inrules = $rules->{in};
+    my $outrules = $rules->{out};
 
-    my $maclist_format = "%-15s %-15s %-15s\n";
-    my $macs = sprintf($maclist_format, '#DISPOSITION', 'INTERFACE', 'MACZONE');
+    my $chain = "GROUP-".$group."-IN";
 
-    foreach my $z (sort keys %$zoneinfo) {
-       my $zid = $zoneinfo->{$z}->{zoneref};
-       if ($zoneinfo->{$z}->{type} eq 'firewall') {
-           # do nothing;
-       } elsif ($zoneinfo->{$z}->{type} eq 'bridge') {
-           my $bridge = $zoneinfo->{$z}->{bridge} || die "internal error";
-           $out .= sprintf($format, $zid, $bridge, 'detect', 'bridge,optional');
-       } elsif ($zoneinfo->{$z}->{type} eq 'bport') {
-           my $ifaces = $zoneinfo->{$z}->{ifaces};
-           foreach my $iface (sort keys %$ifaces) {
-               my $bridge_zone = $zoneinfo->{$z}->{bridge_zone} || die "internal error";
-               my $bridge = $zoneinfo->{$bridge_zone}->{bridge} || die "internal error";
-               my $iftxt = "$bridge:$iface";
+    iptables_addrule(":$chain - [0:0]");
 
-               if ($maclist->{$iface}) {
-                   $out .= sprintf($format, $zid, $iftxt, '-', 'maclist');
-                   $macs .= sprintf($maclist_format, 'ACCEPT', $iface, $maclist->{$iface});
-               } else {
-                   $out .= sprintf($format, $zid, $iftxt, '-', '');
-               }
-           }
-       } else {
-           die "internal error";
-       }
+    if (scalar(@$inrules)) {
+        foreach my $rule (@$inrules) {
+            iptables_generate_rule($chain, $rule);
+        }
     }
 
-    $out .= sprintf("#LAST LINE - ADD YOUR ENTRIES ABOVE THIS ONE - DO NOT REMOVE\n");
-
-    PVE::Tools::file_set_contents("$targetdir/interfaces", $out);
+    $chain = "GROUP-".$group."-OUT";
 
-    # dump maclist
-    PVE::Tools::file_set_contents("$targetdir/maclist", $macs);
+    iptables_addrule(":$chain - [0:0]");
 
-    # dump policy
-
-    $format = "%-15s %-15s %-15s %s\n";
-    $out = sprintf($format, '#SOURCE', 'DEST', 'POLICY', 'LOG');
-    $out .= sprintf($format, 'fw', 'all', 'ACCEPT', '');
+    if(!iptables_chain_exist("BRIDGEFW-OUT")){
+       iptables_addrule(":BRIDGEFW-OUT - [0:0]");
+    }
 
-    # we need to disable intra-zone traffic on bridges. Else traffic
-    # from untracked interfaces simply pass the firewall
-    foreach my $z (sort keys %$zoneinfo) {
-       my $zid = $zoneinfo->{$z}->{zoneref};
-       if ($zoneinfo->{$z}->{type} eq 'bridge') {
-           $out .= sprintf($format, $zid, $zid, 'REJECT', 'info');
-       }
+    if(!iptables_chain_exist("BRIDGEFW-IN")){
+       iptables_addrule(":BRIDGEFW-IN - [0:0]");
     }
-    $out .= sprintf($format, 'all', 'all', 'REJECT', 'info');
 
-    PVE::Tools::file_set_contents("$targetdir/policy", $out);
+    if (scalar(@$outrules)) {
+        foreach my $rule (@$outrules) {
+            #we go the BRIDGEFW-IN because we need to check also other tap rules 
+            #(and group rules can be set on any bridge, so we can't go to VMBRXX-IN)
+            $rule->{action} = 'BRIDGEFW-IN' if $rule->{action} eq 'ACCEPT';
+            iptables_generate_rule($chain, $rule);
+        }
+    }
 
-    # dump rules
-    $out = '';
+}
 
-    $out = sprintf($rule_format, '#ACTION', 'SOURCE', 'DEST', 'PROTO', 'DPORT', 'SPORT');
-    foreach my $vmid (sort keys %$rules) {
-       my $inrules = $rules->{$vmid}->{in};
-       my $outrules = $rules->{$vmid}->{out};
+sub disable_group_rules {
+    my ($group) = @_;
 
-       if (scalar(@$inrules)) {
-           $out .= "# IN to VM $vmid\n";
-           foreach my $rule (@$inrules) {
-               foreach my $netid (keys %{$netinfo->{$vmid}}) {
-                   my $net = $netinfo->{$vmid}->{$netid};
-                   next if $rule->{iface} && $rule->{iface} ne $netid;
-                   $out .= &$generate_input_rule($zoneinfo, $rule, $net, $netid);
-               }
-           }
-       }
+    my $chain = "GROUP-".$group."-IN";
 
-       if (scalar(@$outrules)) {
-           $out .= "# OUT from VM $vmid\n";
-           foreach my $rule (@$outrules) {
-               foreach my $netid (keys %{$netinfo->{$vmid}}) {
-                   my $net = $netinfo->{$vmid}->{$netid};
-                   next if $rule->{iface} && $rule->{iface} ne $netid;
-                   $out .= &$generate_output_rule($zoneinfo, $rule, $net, $netid);
-               }
-           }
-       }
+    if(iptables_chain_exist($chain)){
+       iptables_addrule("-F $chain");
+       iptables_addrule("-X $chain");
     }
 
-    PVE::Tools::file_set_contents("$targetdir/rules", $out);
-};
+    $chain = "GROUP-".$group."-OUT";
+
+    if(iptables_chain_exist($chain)){
+       iptables_addrule("-F $chain");
+       iptables_addrule("-X $chain");
+    }
 
+    #iptables_restore will die if security group is linked in a tap chain
+    #maybe can we improve that, parsing each vm config, or parsing iptables -S
+    #to see if the security group is linked or not
+    iptables_restore();
+}
 
 sub parse_fw_rules {
-    my ($filename, $fh) = @_;
+    my ($filename, $fh, $group) = @_;
 
     my $section;
+    my $securitygroup;
+    my $securitygroupexist;
 
     my $res = { in => [], out => [] };
 
-    my $macros = get_shorewall_macros();
+    my $macros = get_firewall_macros();
     my $protocols = get_etc_protocols();
     
     while (defined(my $line = <$fh>)) {
        next if $line =~ m/^#/;
        next if $line =~ m/^\s*$/;
 
-       if ($line =~ m/^\[(in|out)\]\s*$/i) {
+       if ($line =~ m/^\[(in|out)(:(\S+))?\]\s*$/i) {
            $section = lc($1);
+           $securitygroup = lc($3) if $3;
+           $securitygroupexist = 1 if $securitygroup &&  $securitygroup eq $group;
            next;
        }
        next if !$section;
+       next if $group && $securitygroup ne $group;
 
        my ($action, $iface, $source, $dest, $proto, $dport, $sport) =
            split(/\s+/, $line);
@@ -715,10 +685,12 @@ sub parse_fw_rules {
        $sport = undef if $sport && $sport eq '-';
        my $nbdport = undef;
        my $nbsport = undef;
+       my $nbsource = undef;
+       my $nbdest = undef;
 
        eval {
-           parse_address_list($source) if $source;
-           parse_address_list($dest) if $dest;
+           $nbsource = parse_address_list($source) if $source;
+           $nbdest = parse_address_list($dest) if $dest;
            $nbdport = parse_port_name_number_or_range($dport) if $dport;
            $nbsport = parse_port_name_number_or_range($sport) if $sport;
        };
@@ -735,6 +707,8 @@ sub parse_fw_rules {
            iface => $iface,
            source => $source,
            dest => $dest,
+           nbsource => $nbsource,
+           nbdest => $nbdest,
            proto => $proto,
            dport => $dport,
            sport => $sport,
@@ -746,6 +720,19 @@ sub parse_fw_rules {
        push @{$res->{$section}}, $rule;
     }
 
+    die "security group $group don't exist" if $group && !$securitygroupexist;
+    return $res;
+}
+
+sub run_locked {
+    my ($code, @param) = @_;
+
+    my $timeout = 10;
+
+    my $res = lock_file($pve_fw_lock_filename, $timeout, $code, @param);
+
+    die $@ if $@;
+
     return $res;
 }
 
@@ -785,18 +772,12 @@ sub read_vm_firewall_rules {
 }
 
 sub compile {
-
     my $vmdata = read_local_vm_config();
     my $rules = read_vm_firewall_rules($vmdata);
 
     # print Dumper($vmdata);
 
-    my $swdir = '/etc/shorewall';
-    mkdir $swdir;
-
-    &$compile_shorewall($swdir, $vmdata, $rules);
-
-    PVE::Tools::run_command(['shorewall', 'compile']);
+    die "implement me";
 }
 
 sub compile_and_start {
@@ -804,8 +785,7 @@ sub compile_and_start {
 
     compile();
 
-    PVE::Tools::run_command(['shorewall', $restart ? 'restart' : 'start']);
+     die "implement me";  
 }
 
-
 1;