X-Git-Url: https://git.proxmox.com/?p=pve-firewall.git;a=blobdiff_plain;f=PVE%2FFirewall.pm;h=5396bcdd2b9eefaadfb6c0dae93b375323c5f7c3;hp=2b8842bd26ea45ba71db950279180e9b2a9b0856;hb=462a6553535a43ee48ce5f1b487a5eee0d2cdc3f;hpb=4cdbb3b7078ad3ddb54e5dee5448a251bc314b1b diff --git a/PVE/Firewall.pm b/PVE/Firewall.pm index 2b8842b..5396bcd 100644 --- a/PVE/Firewall.pm +++ b/PVE/Firewall.pm @@ -99,12 +99,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 { @@ -178,7 +182,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 +337,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,6 +346,124 @@ sub flush_tap_rules_direction { } } +sub enablehostfw { + + generate_proxmoxfwinput(); + generate_proxmoxfwoutput(); + + my $filename = "/etc/pve/local/host.fw"; + my $fh = IO::File->new($filename, O_RDONLY); + return if !$fh; + + my $rules = parse_fw_rules($filename, $fh); + my $inrules = $rules->{in}; + my $outrules = $rules->{out}; + + #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); + } + } + + 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); + } + } + + iptables_addrule("-A host-OUT -j LOG --log-prefix \"kvmhost-OUT dropped: \" --log-level 4"); + iptables_addrule("-A host-OUT -j DROP"); + + + my $rule = "proxmoxfw-INPUT -j host-IN"; + if(!iptables_rule_exist($rule)){ + iptables_addrule("-I $rule"); + } + + $rule = "proxmoxfw-OUTPUT -j host-OUT"; + if(!iptables_rule_exist($rule)){ + iptables_addrule("-I $rule"); + } + + iptables_restore(); + + +} + +sub disablehostfw { + + my $chain = "host-IN"; + + my $rule = "proxmoxfw-INPUT -j $chain"; + if(iptables_rule_exist($rule)){ + iptables_addrule("-D $rule"); + } + + if(iptables_chain_exist($chain)){ + iptables_addrule("-F $chain"); + iptables_addrule("-X $chain"); + } + + $chain = "host-OUT"; + + $rule = "proxmoxfw-OUTPUT -j $chain"; + if(iptables_rule_exist($rule)){ + iptables_addrule("-D $rule"); + } + + if(iptables_chain_exist($chain)){ + iptables_addrule("-F $chain"); + iptables_addrule("-X $chain"); + } + + iptables_restore(); +} + +sub generate_proxmoxfwinput { + + 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"); + } +} + +sub generate_proxmoxfwoutput { + + 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"); + } + +} + + my $generate_input_rule = sub { my ($zoneinfo, $rule, $net, $netid) = @_; @@ -715,10 +838,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 +860,8 @@ sub parse_fw_rules { iface => $iface, source => $source, dest => $dest, + nbsource => $nbsource, + nbdest => $nbdest, proto => $proto, dport => $dport, sport => $sport,