]> git.proxmox.com Git - pve-firewall.git/commitdiff
avoid calls to iptables_rule_exist
authorDietmar Maurer <dietmar@proxmox.com>
Mon, 31 Mar 2014 09:35:12 +0000 (11:35 +0200)
committerDietmar Maurer <dietmar@proxmox.com>
Mon, 31 Mar 2014 09:35:12 +0000 (11:35 +0200)
We can return that info with iptables_get_chains().

example/cluster.fw
src/PVE/Firewall.pm
src/pvefw

index b9c088fb58160b7231b25bcc6c3a6a41af0d2278..fe283fcf94600c4cc1e36c4ab341e9c855f9a2cf 100644 (file)
@@ -1,3 +1,11 @@
+[OPTIONS]
+
+enable: 1
+
+[RULES]
+
+IN  SSH(ACCEPT) vmbr0
+
 [group group1]
 
 IN  ACCEPT - - tcp 22 -
index 02c602fe17c037ebc6840612066ce6268e07b3f2..126717d8585fcb8dc0e3a599cb4fc59781358bd1 100644 (file)
@@ -833,6 +833,8 @@ sub iptables_get_chains {
 
     my $table = '';
 
+    my $hooks = {};
+
     my $parser = sub {
        my $line = shift;
 
@@ -854,6 +856,8 @@ sub iptables_get_chains {
            my ($chain, $sig) = ($1, $2);
            return if !&$is_pvefw_chain($chain);
            $res->{$chain} = $sig;
+       } elsif ($line =~ m/^-A\s+(INPUT|OUTPUT|FORWARD)\s+-j\s+PVEFW-\1$/) {
+           $hooks->{$1} = 1;
        } else {
            # simply ignore the rest
            return;
@@ -862,7 +866,7 @@ sub iptables_get_chains {
 
     run_command("/sbin/iptables-save", outfunc => $parser);
 
-    return $res;
+    return wantarray ? ($res, $hooks) : $res;
 }
 
 sub ipset_chain_digest {
@@ -1770,7 +1774,7 @@ sub parse_cluster_fw_rules {
     my $section;
     my $group;
 
-    my $res = { rules => {}, options => {}, groups => {}, ipset => {} };
+    my $res = { rules => [], options => {}, groups => {}, ipset => {} };
 
     my $digest = Digest::SHA->new('sha1');
 
@@ -2465,6 +2469,29 @@ sub update_nf_conntrack_max {
     }
 }
 
+sub remove_pvefw_chains {
+
+    my ($chash, $hooks) = iptables_get_chains();
+    my $cmdlist = "*filter\n";
+
+    foreach my $h (qw(INPUT OUTPUT FORWARD)) {
+       if ($hooks->{$h}) {
+           $cmdlist .= "-D $h -j PVEFW-$h\n";
+       }
+    }
+    foreach my $chain (keys %$chash) {
+       $cmdlist .= "-F $chain\n";
+    }
+
+    foreach my $chain (keys %$chash) {
+       $cmdlist .= "-X $chain\n";
+    }
+    $cmdlist .= "COMMIT\n";
+
+    iptables_restore_cmdlist($cmdlist);
+}
+
 sub update {
     my ($start, $verbose) = @_;
 
index f700e95d03b43b00432ac9f6daa3b8120c22c955..a87bafb8cee4e233ebfebec9586bec594bc58e48 100755 (executable)
--- a/src/pvefw
+++ b/src/pvefw
@@ -196,33 +196,7 @@ __PACKAGE__->register_method ({
        my ($param) = @_;
 
        my $code = sub {
-
-           my $chash = PVE::Firewall::iptables_get_chains();
-           my $cmdlist = "*filter\n";
-           my $rule = "INPUT -j PVEFW-INPUT";
-           if (PVE::Firewall::iptables_rule_exist($rule)) {
-               $cmdlist .= "-D $rule\n";
-           }
-           $rule = "OUTPUT -j PVEFW-OUTPUT";
-           if (PVE::Firewall::iptables_rule_exist($rule)) {
-               $cmdlist .= "-D $rule\n";
-           }
-
-           $rule = "FORWARD -j PVEFW-FORWARD";
-           if (PVE::Firewall::iptables_rule_exist($rule)) {
-               $cmdlist .= "-D $rule\n";
-           }
-
-           foreach my $chain (keys %$chash) {
-               $cmdlist .= "-F $chain\n";
-           }
-           foreach my $chain (keys %$chash) {
-               $cmdlist .= "-X $chain\n";
-           }
-           $cmdlist .= "COMMIT\n";
-
-           PVE::Firewall::iptables_restore_cmdlist($cmdlist);
-
+           PVE::Firewall::remove_pvefw_chains();
            PVE::Firewall::save_pvefw_status('stopped');
        };