X-Git-Url: https://git.proxmox.com/?p=pve-firewall.git;a=blobdiff_plain;f=src%2FPVE%2FFirewall.pm;h=6707e669e3065c6539b120f928a170c7603d0c5f;hp=02c602fe17c037ebc6840612066ce6268e07b3f2;hb=68c90e210e53b10f63b47da62c06507afc68ef6e;hpb=c6f5cc88d85a9e63654defb0bee4b1c5d6a574cd diff --git a/src/PVE/Firewall.pm b/src/PVE/Firewall.pm index 02c602f..6707e66 100644 --- a/src/PVE/Firewall.pm +++ b/src/PVE/Firewall.pm @@ -16,6 +16,7 @@ use Net::IP; use PVE::Tools qw(run_command lock_file); use Encode; +my $hostfw_conf_filename = "/etc/pve/local/host.fw"; my $clusterfw_conf_filename = "/etc/pve/firewall/cluster.fw"; # dynamically include PVE::QemuServer and PVE::OpenVZ @@ -791,12 +792,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 +801,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 { @@ -833,6 +828,8 @@ sub iptables_get_chains { my $table = ''; + my $hooks = {}; + my $parser = sub { my $line = shift; @@ -854,6 +851,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,11 +861,21 @@ sub iptables_get_chains { run_command("/sbin/iptables-save", outfunc => $parser); - return $res; + return wantarray ? ($res, $hooks) : $res; +} + +sub iptables_chain_digest { + my ($rules) = @_; + my $digest = Digest::SHA->new('sha1'); + foreach my $rule (@$rules) { # order is important + $digest->add($rule); + } + return $digest->b64digest; } sub ipset_chain_digest { my ($rules) = @_; + my $digest = Digest::SHA->new('sha1'); foreach my $rule (sort @$rules) { # note: sorted $digest->add($rule); @@ -884,8 +893,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; @@ -902,28 +913,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) = @_; @@ -1770,7 +1759,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'); @@ -1799,17 +1788,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; } @@ -1842,17 +1823,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; } @@ -1917,6 +1906,66 @@ sub load_vmfw_conf { return $vmfw_conf; } +my $format_rules = sub { + my ($rules, $need_iface) = @_; + + my $raw = ''; + + foreach my $rule (@$rules) { + if ($rule->{type} eq 'in' || $rule->{type} eq 'out') { + $raw .= '|' if defined($rule->{enable}) && !$rule->{enable}; + $raw .= uc($rule->{type}); + $raw .= " " . $rule->{action}; + $raw .= " " . ($rule->{iface} || '-') if $need_iface; + $raw .= " " . ($rule->{source} || '-'); + $raw .= " " . ($rule->{dest} || '-'); + $raw .= " " . ($rule->{proto} || '-'); + $raw .= " " . ($rule->{dport} || '-'); + $raw .= " " . ($rule->{sport} || '-'); + $raw .= " # " . encode('utf8', $rule->{comment}) + if $rule->{comment} && $rule->{comment} !~ m/^\s*$/; + $raw .= "\n"; + } else { + die "implement me '$rule->{type}'"; + } + } + + return $raw; +}; + +my $format_options = sub { + my ($options) = @_; + + my $raw = ''; + + $raw .= "[OPTIONS]\n\n"; + foreach my $opt (keys %$options) { + $raw .= "$opt: $options->{$opt}\n"; + } + $raw .= "\n"; + + return $raw; +}; + +sub save_vmfw_conf { + my ($vmid, $vmfw_conf) = @_; + + my $raw = ''; + + my $options = $vmfw_conf->{options}; + $raw .= &$format_options($options) if scalar(keys %$options); + + my $rules = $vmfw_conf->{rules}; + if (scalar(@$rules)) { + $raw .= "[RULES]\n\n"; + $raw .= &$format_rules($rules, 1); + $raw .= "\n"; + } + + my $filename = "/etc/pve/firewall/$vmid.fw"; + PVE::Tools::file_set_contents($filename, $raw); +} + sub read_vm_firewall_configs { my ($vmdata) = @_; my $vmfw_configs = {}; @@ -1978,23 +2027,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); + } - foreach my $ip (@{$options->{ip}}) { + push @{$ipset_ruleset->{$name}}, "create $name hash:net family inet hashsize $hashsize maxelem $hashsize"; + + 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) = @_; @@ -2057,60 +2121,27 @@ sub load_clusterfw_conf { return $cluster_conf; } -my $rules_to_conf = sub { - my ($rules, $need_iface) = @_; - - my $raw = ''; - - foreach my $rule (@$rules) { - if ($rule->{type} eq 'in' || $rule->{type} eq 'out') { - $raw .= '|' if defined($rule->{enable}) && !$rule->{enable}; - $raw .= uc($rule->{type}); - $raw .= " " . $rule->{action}; - $raw .= " " . ($rule->{iface} || '-') if $need_iface; - $raw .= " " . ($rule->{source} || '-'); - $raw .= " " . ($rule->{dest} || '-'); - $raw .= " " . ($rule->{proto} || '-'); - $raw .= " " . ($rule->{dport} || '-'); - $raw .= " " . ($rule->{sport} || '-'); - $raw .= " # " . encode('utf8', $rule->{comment}) - if $rule->{comment} && $rule->{comment} !~ m/^\s*$/; - $raw .= "\n"; - } else { - die "implement me '$rule->{type}'"; - } - } - - return $raw; -}; - sub save_clusterfw_conf { my ($cluster_conf) = @_; my $raw = ''; my $options = $cluster_conf->{options}; - if (scalar(keys %$options)) { - $raw .= "[OPTIONS]\n\n"; - foreach my $opt (keys %$options) { - $raw .= "$opt: $options->{$opt}\n"; - } - $raw .= "\n"; - } + $raw .= &$format_options($options) if scalar(keys %$options); # fixme: save ipset my $rules = $cluster_conf->{rules}; if (scalar(@$rules)) { $raw .= "[RULES]\n\n"; - $raw .= &$rules_to_conf($rules, 1); + $raw .= &$format_rules($rules, 1); $raw .= "\n"; } foreach my $group (sort keys %{$cluster_conf->{groups}}) { my $rules = $cluster_conf->{groups}->{$group}; $raw .= "[group $group]\n\n"; - $raw .= &$rules_to_conf($rules, 0); + $raw .= &$format_rules($rules, 0); $raw .= "\n"; } @@ -2120,13 +2151,30 @@ sub save_clusterfw_conf { sub load_hostfw_conf { my $hostfw_conf = {}; - my $filename = "/etc/pve/local/host.fw"; - if (my $fh = IO::File->new($filename, O_RDONLY)) { - $hostfw_conf = parse_host_fw_rules($filename, $fh); + if (my $fh = IO::File->new($hostfw_conf_filename, O_RDONLY)) { + $hostfw_conf = parse_host_fw_rules($hostfw_conf_filename, $fh); } return $hostfw_conf; } +sub save_hostfw_conf { + my ($hostfw_conf) = @_; + + my $raw = ''; + + my $options = $hostfw_conf->{options}; + $raw .= &$format_options($options) if scalar(keys %$options); + + my $rules = $hostfw_conf->{rules}; + if (scalar(@$rules)) { + $raw .= "[RULES]\n\n"; + $raw .= &$format_rules($rules, 1); + $raw .= "\n"; + } + + PVE::Tools::file_set_contents($hostfw_conf_filename, $raw); +} + sub compile { my $vmdata = read_local_vm_config(); my $vmfw_configs = read_vm_firewall_configs($vmdata); @@ -2245,19 +2293,13 @@ sub compile { } sub get_ruleset_status { - my ($ruleset, $verbose, $ipset) = @_; - - my $active_chains = undef; - if($ipset){ - $active_chains = ipset_get_chains(); - }else{ - $active_chains = iptables_get_chains(); - } + my ($ruleset, $active_chains, $digest_fn, $verbose) = @_; my $statushash = {}; foreach my $chain (sort keys %$ruleset) { - my $sig = ipset_chain_digest($ruleset->{$chain}); + my $sig = &$digest_fn($ruleset->{$chain}); + $statushash->{$chain}->{sig} = $sig; my $oldsig = $active_chains->{$chain}; @@ -2284,7 +2326,7 @@ sub get_ruleset_status { print "delete $chain ($sig)\n" if $verbose; } } - + return $statushash; } @@ -2295,12 +2337,13 @@ sub print_sig_rule { return "-A $chain -m comment --comment \"PVESIG:$sig\"\n"; } -sub get_rulset_cmdlist { +sub get_ruleset_cmdlist { my ($ruleset, $verbose) = @_; my $cmdlist = "*filter\n"; # we pass this to iptables-restore; - - my $statushash = get_ruleset_status($ruleset, $verbose); + + my ($active_chains, $hooks) = iptables_get_chains(); + my $statushash = get_ruleset_status($ruleset, $active_chains, \&iptables_chain_digest, $verbose); # create missing chains first foreach my $chain (sort keys %$ruleset) { @@ -2311,18 +2354,10 @@ sub get_rulset_cmdlist { $cmdlist .= ":$chain - [0:0]\n"; } - my $rule = "INPUT -j PVEFW-INPUT"; - if (!PVE::Firewall::iptables_rule_exist($rule)) { - $cmdlist .= "-A $rule\n"; - } - $rule = "OUTPUT -j PVEFW-OUTPUT"; - if (!PVE::Firewall::iptables_rule_exist($rule)) { - $cmdlist .= "-A $rule\n"; - } - - $rule = "FORWARD -j PVEFW-FORWARD"; - if (!PVE::Firewall::iptables_rule_exist($rule)) { - $cmdlist .= "-A $rule\n"; + foreach my $h (qw(INPUT OUTPUT FORWARD)) { + if (!$hooks->{$h}) { + $cmdlist .= "-A $h -j PVEFW-$h\n"; + } } foreach my $chain (sort keys %$ruleset) { @@ -2357,7 +2392,7 @@ sub get_rulset_cmdlist { } my $changes = $cmdlist ne "*filter\n" ? 1 : 0; - + $cmdlist .= "COMMIT\n"; return wantarray ? ($cmdlist, $changes) : $cmdlist; @@ -2368,7 +2403,10 @@ sub get_ipset_cmdlist { my $cmdlist = ""; - my $statushash = get_ruleset_status($ruleset, $verbose, 1); + my $delete_cmdlist = ""; + + my $active_chains = ipset_get_chains(); + my $statushash = get_ruleset_status($ruleset, $active_chains, \&ipset_chain_digest, $verbose); foreach my $chain (sort keys %$ruleset) { my $stat = $statushash->{$chain}; @@ -2378,33 +2416,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 { @@ -2414,20 +2451,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_rulset_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 $statushash = get_ruleset_status($ruleset); + my $active_chains = iptables_get_chains(); + my $statushash = get_ruleset_status($ruleset, $active_chains, \&iptables_chain_digest, 0); my $errors; foreach my $chain (sort keys %$ruleset) { @@ -2465,6 +2515,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) = @_;