X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;ds=inline;f=src%2FPVE%2FFirewall.pm;h=54235fb64f89a7860a9d649bc19ba570d1da5ca5;hb=81a0bf433dda8e829156306a34767a2b0ba3456d;hp=8ba1ba9bad89ab273e9b2b5da60f1f0d1ba4366a;hpb=4bd0a9c427e756b9b31bc6401bdcccf20b1eca7f;p=pve-firewall.git diff --git a/src/PVE/Firewall.pm b/src/PVE/Firewall.pm index 8ba1ba9..54235fb 100644 --- a/src/PVE/Firewall.pm +++ b/src/PVE/Firewall.pm @@ -791,12 +791,6 @@ sub enable_bridge_firewall { my $rule_format = "%-15s %-30s %-30s %-15s %-15s %-15s\n"; -sub iptables { - my ($cmd) = @_; - - run_command("/sbin/iptables $cmd", outfunc => sub {}, errfunc => sub {}); -} - sub iptables_restore_cmdlist { my ($cmdlist) = @_; @@ -806,7 +800,7 @@ sub iptables_restore_cmdlist { sub ipset_restore_cmdlist { my ($cmdlist) = @_; - run_command(" /usr/sbin/ipset restore", input => $cmdlist); + run_command("/usr/sbin/ipset restore", input => $cmdlist); } sub iptables_get_chains { @@ -898,8 +892,10 @@ sub ipset_get_chains { return if $line =~ m/^#/; return if $line =~ m/^\s*$/; - if ($line =~ m/^(\S+)\s(\S+)\s(\S+)/) { - push @{$chains->{$2}}, $line; + if ($line =~ m/^(?:\S+)\s(\S+)\s(?:\S+).*/) { + my $chain = $1; + $line =~ s/\s+$//; # delete trailing white space + push @{$chains->{$chain}}, $line; } else { # simply ignore the rest return; @@ -916,28 +912,6 @@ sub ipset_get_chains { return $res; } -sub iptables_chain_exist { - my ($chain) = @_; - - eval{ - iptables("-n --list $chain"); - }; - return undef if $@; - - return 1; -} - -sub iptables_rule_exist { - my ($rule) = @_; - - eval{ - iptables("-C $rule"); - }; - return undef if $@; - - return 1; -} - sub ruleset_generate_cmdstr { my ($ruleset, $chain, $rule, $actions, $goto) = @_; @@ -1813,17 +1787,9 @@ sub parse_cluster_fw_rules { next; } - if ($line =~ m/^\[ipgroup\s+(\S+)\]\s*$/i) { - $section = 'ipset'; - $group = lc($1); - $res->{$section}->{$group}->{type} = 'hash:ip family inet hashsize 1024 maxelem 65536 '; - next; - } - if ($line =~ m/^\[netgroup\s+(\S+)\]\s*$/i) { $section = 'ipset'; $group = lc($1); - $res->{$section}->{$group}->{type} = 'hash:net family inet hashsize 1024 maxelem 65536 '; next; } @@ -1856,17 +1822,25 @@ sub parse_cluster_fw_rules { push @{$res->{$section}->{$group}}, $rule; } elsif ($section eq 'ipset') { chomp $line; - my $ip; - if (!Net::IP->new($line)) { + $line =~ m/^(\!)?(\s)?((\d+)\.(\d+)\.(\d+)\.(\d+)(\/(\d+))?)/; + my $nomatch = $1; + my $ip = $3; + + if(!$ip){ + warn "$prefix: $line is not an valid ip address"; + next; + } + if (!Net::IP->new($ip)) { warn "$prefix: $line is not an valid ip address"; next; } - push @{$res->{$section}->{$group}->{ip}}, $line; + $ip .= " nomatch" if $nomatch; + + push @{$res->{$section}->{$group}}, $ip; } } $res->{digest} = $digest->b64digest; - return $res; } @@ -1992,23 +1966,38 @@ sub generate_std_chains { } sub generate_ipset_chains { - my ($ipset_ruleset, $options) = @_; + my ($ipset_ruleset, $fw_conf) = @_; - foreach my $ipset (keys %{$options->{ipset}}) { - generate_ipset($ipset_ruleset, $ipset, $options->{ipset}->{$ipset}); + foreach my $ipset (keys %{$fw_conf->{ipset}}) { + generate_ipset($ipset_ruleset, $ipset, $fw_conf->{ipset}->{$ipset}); } } sub generate_ipset { my ($ipset_ruleset, $name, $options) = @_; - push @{$ipset_ruleset->{$name}}, "create $name $options->{type}"; + my $hashsize = scalar(@$options); + if ($hashsize <= 64) { + $hashsize = 64; + } else { + $hashsize = round_powerof2($hashsize); + } + + push @{$ipset_ruleset->{$name}}, "create $name hash:net family inet hashsize $hashsize maxelem $hashsize"; - foreach my $ip (@{$options->{ip}}) { + foreach my $ip (@$options) { push @{$ipset_ruleset->{$name}}, "add $name $ip"; } } +sub round_powerof2 { + my ($int) = @_; + + $int--; + $int |= $int >> $_ foreach (1,2,4,8,16); + return ++$int; +} + sub save_pvefw_status { my ($status) = @_; @@ -2292,7 +2281,7 @@ sub get_ruleset_status { print "delete $chain ($sig)\n" if $verbose; } } - + return $statushash; } @@ -2369,6 +2358,8 @@ sub get_ipset_cmdlist { my $cmdlist = ""; + my $delete_cmdlist = ""; + my $active_chains = ipset_get_chains(); my $statushash = get_ruleset_status($ruleset, $active_chains, \&ipset_chain_digest, $verbose); @@ -2380,33 +2371,32 @@ sub get_ipset_cmdlist { foreach my $cmd (@{$ruleset->{$chain}}) { $cmdlist .= "$cmd\n"; } - } + } if ($stat->{action} eq 'update') { my $chain_swap = $chain."_swap"; - + foreach my $cmd (@{$ruleset->{$chain}}) { $cmd =~ s/$chain/$chain_swap/; $cmdlist .= "$cmd\n"; - } $cmdlist .= "swap $chain_swap $chain\n"; $cmdlist .= "flush $chain_swap\n"; $cmdlist .= "destroy $chain_swap\n"; - } + } } foreach my $chain (keys %$statushash) { next if $statushash->{$chain}->{action} ne 'delete'; - $cmdlist .= "flush $chain\n"; - $cmdlist .= "destroy $chain\n"; + $delete_cmdlist .= "flush $chain\n"; + $delete_cmdlist .= "destroy $chain\n"; } - my $changes = $cmdlist ? 1 : 0; - - return wantarray ? ($cmdlist, $changes) : $cmdlist; + my $changes = ($cmdlist || $delete_cmdlist) ? 1 : 0; + + return ($cmdlist, $delete_cmdlist, $changes); } sub apply_ruleset { @@ -2416,21 +2406,33 @@ sub apply_ruleset { update_nf_conntrack_max($hostfw_conf); - my $ipsetcmdlist = get_ipset_cmdlist($ipset_ruleset, $verbose); + my ($ipset_create_cmdlist, $ipset_delete_cmdlist, $ipset_changes) = + get_ipset_cmdlist($ipset_ruleset, undef, $verbose); - my $cmdlist = get_ruleset_cmdlist($ruleset, $verbose); + my ($cmdlist, $changes) = get_ruleset_cmdlist($ruleset, $verbose); - print $ipsetcmdlist if $verbose; + if ($verbose) { + if ($ipset_changes) { + print "ipset changes:\n"; + print $ipset_create_cmdlist if $ipset_create_cmdlist; + print $ipset_delete_cmdlist if $ipset_delete_cmdlist; + } - print $cmdlist if $verbose; + if ($changes) { + print "iptables changes:\n"; + print $cmdlist; + } + } - ipset_restore_cmdlist($ipsetcmdlist); + ipset_restore_cmdlist($ipset_create_cmdlist); iptables_restore_cmdlist($cmdlist); + ipset_restore_cmdlist($ipset_delete_cmdlist) if $ipset_delete_cmdlist; + # test: re-read status and check if everything is up to date my $active_chains = iptables_get_chains(); - my $statushash = get_ruleset_status($ruleset, $active_chains, \&iptables_chain_digest, $verbose); + my $statushash = get_ruleset_status($ruleset, $active_chains, \&iptables_chain_digest, 0); my $errors; foreach my $chain (sort keys %$ruleset) {