X-Git-Url: https://git.proxmox.com/?p=pve-firewall.git;a=blobdiff_plain;f=src%2FPVE%2FFirewall.pm;h=cacfedcf19b936947f3bdbdf50632853c05791a4;hp=03cafb86bf2fdb1819d990fb33f1d048fb5e73d1;hb=b1ef6d2e715d6aeaf67d53ca3270f0d247c407d9;hpb=c50a5a68865bfb9dff4644a7ffbe6fca14d40af9 diff --git a/src/PVE/Firewall.pm b/src/PVE/Firewall.pm index 03cafb8..cacfedc 100644 --- a/src/PVE/Firewall.pm +++ b/src/PVE/Firewall.pm @@ -98,9 +98,8 @@ use Data::Dumper; my $nodename = PVE::INotify::nodename(); my $pve_fw_lock_filename = "/var/lock/pvefw.lck"; -my $pve_fw_status_filename = "/var/lib/pve-firewall/pvefw.status"; -my $default_log_level = 'info'; +my $default_log_level = 'nolog'; # avoid logs by default my $log_level_hash = { debug => 7, @@ -158,9 +157,8 @@ my $pve_fw_macros = { { action => 'PARAM', proto => 'tcp', dport => '6277' }, ], 'DHCPfwd' => [ - "Forwarded DHCP traffic (bidirectional)", + "Forwarded DHCP traffic", { action => 'PARAM', proto => 'udp', dport => '67:68', sport => '67:68' }, - { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '67:68', sport => '67:68' }, ], 'DNS' => [ "Domain Name System traffic (upd and tcp)", @@ -187,9 +185,8 @@ my $pve_fw_macros = { { action => 'PARAM', proto => 'udp', dport => '1080' }, ], 'GRE' => [ - "Generic Routing Encapsulation tunneling protocol (bidirectional)", + "Generic Routing Encapsulation tunneling protocol", { action => 'PARAM', proto => '47' }, - { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => '47' }, ], 'Git' => [ "Git distributed revision control traffic", @@ -224,32 +221,24 @@ my $pve_fw_macros = { { action => 'PARAM', proto => 'tcp', dport => '993' }, ], 'IPIP' => [ - "IPIP capsulation traffic (bidirectional)", + "IPIP capsulation traffic", { action => 'PARAM', proto => '94' }, - { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => '94' }, ], 'IPsec' => [ - "IPsec traffic (bidirectional)", + "IPsec traffic", { action => 'PARAM', proto => 'udp', dport => '500', sport => '500' }, { action => 'PARAM', proto => '50' }, - { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '500', sport => '500' }, - { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => '50' }, ], 'IPsecah' => [ - "IPsec authentication (AH) traffic (bidirectional)", + "IPsec authentication (AH) traffic", { action => 'PARAM', proto => 'udp', dport => '500', sport => '500' }, { action => 'PARAM', proto => '51' }, - { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '500', sport => '500' }, - { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => '51' }, ], 'IPsecnat' => [ - "IPsec traffic and Nat-Traversal (bidirectional)", + "IPsec traffic and Nat-Traversal", { action => 'PARAM', proto => 'udp', dport => '500' }, { action => 'PARAM', proto => 'udp', dport => '4500' }, { action => 'PARAM', proto => '50' }, - { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '500' }, - { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '4500' }, - { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => '50' }, ], 'IRC' => [ "Internet Relay Chat traffic", @@ -262,7 +251,6 @@ my $pve_fw_macros = { 'L2TP' => [ "Layer 2 Tunneling Protocol traffic", { action => 'PARAM', proto => 'udp', dport => '1701' }, - { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '1701' }, ], 'LDAP' => [ "Lightweight Directory Access Protocol traffic", @@ -306,11 +294,6 @@ my $pve_fw_macros = { "Network Time Protocol (ntpd)", { action => 'PARAM', proto => 'udp', dport => '123' }, ], - 'NTPbi' => [ - "Bi-directional NTP (for NTP peers)", - { action => 'PARAM', proto => 'udp', dport => '123' }, - { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '123' }, - ], 'OSPF' => [ "OSPF multicast traffic", { action => 'PARAM', proto => '89' }, @@ -335,7 +318,6 @@ my $pve_fw_macros = { 'PPtP' => [ "Point-to-Point Tunneling Protocol", { action => 'PARAM', proto => '47' }, - { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => '47' }, { action => 'PARAM', proto => 'tcp', dport => '1723' }, ], 'Ping' => [ @@ -354,10 +336,9 @@ my $pve_fw_macros = { "Microsoft Remote Desktop Protocol traffic", { action => 'PARAM', proto => 'tcp', dport => '3389' }, ], - 'RIPbi' => [ + 'RIP' => [ "Routing Information Protocol (bidirectional)", { action => 'PARAM', proto => 'udp', dport => '520' }, - { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '520' }, ], 'RNDC' => [ "BIND remote management protocol", @@ -386,17 +367,6 @@ my $pve_fw_macros = { { action => 'PARAM', proto => 'udp', dport => '1024:65535', sport => '137' }, { action => 'PARAM', proto => 'tcp', dport => '135,139,445' }, ], - 'SMBBI' => [ - "Microsoft SMB traffic (bidirectional)", - { action => 'PARAM', proto => 'udp', dport => '135,445' }, - { action => 'PARAM', proto => 'udp', dport => '137:139' }, - { action => 'PARAM', proto => 'udp', dport => '1024:65535', sport => '137' }, - { action => 'PARAM', proto => 'tcp', dport => '135,139,445' }, - { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '135,445' }, - { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '137:139' }, - { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '1024:65535', sport => '137' }, - { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'tcp', dport => '135,139,445' }, - ], 'SMBswat' => [ "Samba Web Administration Tool", { action => 'PARAM', proto => 'tcp', dport => '901' }, @@ -468,8 +438,8 @@ my $pve_fw_macros = { { action => 'PARAM', proto => 'icmp', dport => 'echo-request' }, ], 'VNC' => [ - "VNC traffic for VNC display's 0 - 9", - { action => 'PARAM', proto => 'tcp', dport => '5900:5909' }, + "VNC traffic for VNC display's 0 - 99", + { action => 'PARAM', proto => 'tcp', dport => '5900:5999' }, ], 'VNCL' => [ "VNC traffic from Vncservers to Vncviewers in listen mode", @@ -717,6 +687,58 @@ sub get_etc_protocols { return $etc_protocols; } +my $ipv4_mask_hash_localnet = { + '255.255.0.0' => 16, + '255.255.128.0' => 17, + '255.255.192.0' => 18, + '255.255.224.0' => 19, + '255.255.240.0' => 20, + '255.255.248.0' => 21, + '255.255.252.0' => 22, + '255.255.254.0' => 23, + '255.255.255.0' => 24, + '255.255.255.128' => 25, + '255.255.255.192' => 26, + '255.255.255.224' => 27, + '255.255.255.240' => 28, + '255.255.255.248' => 29, + '255.255.255.252' => 30, +}; + +my $__local_network; + +sub local_network { + my ($new_value) = @_; + + $__local_network = $new_value if defined($new_value); + + return $__local_network if defined($__local_network); + + eval { + my $nodename = PVE::INotify::nodename(); + + my $ip = PVE::Cluster::remote_node_ip($nodename); + + my $testip = Net::IP->new($ip); + + my $routes = PVE::ProcFSTools::read_proc_net_route(); + foreach my $entry (@$routes) { + my $mask = $ipv4_mask_hash_localnet->{$entry->{mask}}; + next if !defined($mask); + return if $mask eq '0.0.0.0'; + my $cidr = "$entry->{dest}/$mask"; + my $testnet = Net::IP->new($cidr); + if ($testnet->overlaps($testip)) { + $__local_network = $cidr; + return; + } + } + }; + warn $@ if $@; + + return $__local_network; +} + sub parse_address_list { my ($str) = @_; @@ -893,10 +915,12 @@ my $rule_properties = { sport => { type => 'string', format => 'pve-fw-port-spec', optional => 1, + requires => 'proto', }, dport => { type => 'string', format => 'pve-fw-port-spec', optional => 1, + requires => 'proto', }, comment => { type => 'string', @@ -1001,7 +1025,10 @@ sub verify_rule { raise_param_exc({ type => "unknown rule type '$type'"}); } - # fixme: verify $rule->{iface}? + if ($rule->{iface}) { + eval { PVE::JSONSchema::pve_verify_iface($rule->{iface}); }; + raise_param_exc({ iface => $@ }) if $@; + } if ($rule->{macro}) { my $preferred_name = $pve_fw_preferred_macro_names->{lc($rule->{macro})}; @@ -1243,7 +1270,7 @@ sub ruleset_generate_cmdstr { die "invalid security group name '$dest'\n"; } } elsif ($dest =~ m/^${ip_alias_pattern}$/){ - my $alias = lc($source); + my $alias = lc($dest); my $e = $cluster_conf->{aliases}->{$alias}; die "no such alias $dest" if !$e; push @cmd, "-d $e->{cidr}"; @@ -1293,8 +1320,8 @@ sub ruleset_generate_cmdstr { } } } elsif ($rule->{dport} || $rule->{sport}) { - warn "ignoring destination port '$rule->{dport}' - no protocol specified\n" if $rule->{dport}; - warn "ignoring source port '$rule->{sport}' - no protocol specified\n" if $rule->{sport}; + die "destination port '$rule->{dport}', but no protocol specified\n" if $rule->{dport}; + die "source port '$rule->{sport}', but no protocol specified\n" if $rule->{sport}; } push @cmd, "-m addrtype --dst-type $rule->{dsttype}" if $rule->{dsttype}; @@ -1436,7 +1463,12 @@ sub ruleset_chain_add_conn_filters { } sub ruleset_chain_add_input_filters { - my ($ruleset, $chain, $options) = @_; + my ($ruleset, $chain, $options, $cluster_conf, $loglevel) = @_; + + if ($cluster_conf->{ipset}->{blacklist}){ + ruleset_addlog($ruleset, $chain, 0, "DROP: ", $loglevel, "-m set --match-set PVEFW-blacklist src"); + ruleset_addrule($ruleset, $chain, "-m set --match-set PVEFW-blacklist src -j DROP"); + } if (!(defined($options->{nosmurfs}) && $options->{nosmurfs} == 0)) { ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate INVALID,NEW -j PVEFW-smurfs"); @@ -1448,7 +1480,7 @@ sub ruleset_chain_add_input_filters { } sub ruleset_create_vm_chain { - my ($ruleset, $chain, $options, $host_options, $macaddr, $direction) = @_; + my ($ruleset, $chain, $options, $macaddr, $direction) = @_; ruleset_create_chain($ruleset, $chain); my $accept = generate_nfqueue($options); @@ -1471,6 +1503,26 @@ sub ruleset_create_vm_chain { } } +sub ruleset_add_group_rule { + my ($ruleset, $cluster_conf, $chain, $rule, $direction, $action) = @_; + + my $group = $rule->{action}; + my $group_chain = "GROUP-$group-$direction"; + if(!ruleset_chain_exist($ruleset, $group_chain)){ + generate_group_rules($ruleset, $cluster_conf, $group); + } + + if ($direction eq 'OUT' && $rule->{iface_out}) { + ruleset_addrule($ruleset, $chain, "-o $rule->{iface_out} -j $group_chain"); + } elsif ($direction eq 'IN' && $rule->{iface_in}) { + ruleset_addrule($ruleset, $chain, "-i $rule->{iface_in} -j $group_chain"); + } else { + ruleset_addrule($ruleset, $chain, "-j $group_chain"); + } + + ruleset_addrule($ruleset, $chain, "-m mark --mark 1 -j $action"); +} + sub ruleset_generate_vm_rules { my ($ruleset, $rules, $cluster_conf, $chain, $netid, $direction, $options) = @_; @@ -1482,18 +1534,8 @@ sub ruleset_generate_vm_rules { next if $rule->{iface} && $rule->{iface} ne $netid; next if !$rule->{enable}; if ($rule->{type} eq 'group') { - my $group_chain = "GROUP-$rule->{action}-$direction"; - if(!ruleset_chain_exist($ruleset, $group_chain)){ - generate_group_rules($ruleset, $cluster_conf, $rule->{action}); - } - ruleset_addrule($ruleset, $chain, "-j $group_chain"); - if ($direction eq 'OUT'){ - ruleset_addrule($ruleset, $chain, "-m mark --mark 1 -j RETURN"); - }else{ - my $accept = generate_nfqueue($options); - ruleset_addrule($ruleset, $chain, "-m mark --mark 1 -j $accept"); - } - + ruleset_add_group_rule($ruleset, $cluster_conf, $chain, $rule, $direction, + $direction eq 'OUT' ? 'RETURN' : $in_accept); } else { next if $rule->{type} ne $lc_direction; eval { @@ -1546,21 +1588,18 @@ sub ruleset_generate_vm_ipsrules { } sub generate_venet_rules_direction { - my ($ruleset, $cluster_conf, $hostfw_conf, $vmfw_conf, $vmid, $ip, $direction) = @_; - - parse_address_list($ip); # make sure we have a valid $ip list + my ($ruleset, $cluster_conf, $vmfw_conf, $vmid, $ip, $direction) = @_; my $lc_direction = lc($direction); my $rules = $vmfw_conf->{rules}; my $options = $vmfw_conf->{options}; - my $hostfw_options = $vmfw_conf->{options}; my $loglevel = get_option_log_level($options, "log_level_${lc_direction}"); my $chain = "venet0-$vmid-$direction"; - ruleset_create_vm_chain($ruleset, $chain, $options, $hostfw_options, undef, $direction); + ruleset_create_vm_chain($ruleset, $chain, $options, undef, $direction); ruleset_generate_vm_rules($ruleset, $rules, $cluster_conf, $chain, 'venet', $direction); @@ -1591,19 +1630,18 @@ sub generate_venet_rules_direction { } sub generate_tap_rules_direction { - my ($ruleset, $cluster_conf, $hostfw_conf, $iface, $netid, $macaddr, $vmfw_conf, $vmid, $direction) = @_; + my ($ruleset, $cluster_conf, $iface, $netid, $macaddr, $vmfw_conf, $vmid, $direction) = @_; my $lc_direction = lc($direction); my $rules = $vmfw_conf->{rules}; my $options = $vmfw_conf->{options}; - my $hostfw_options = $hostfw_conf->{options}; my $loglevel = get_option_log_level($options, "log_level_${lc_direction}"); my $tapchain = "$iface-$direction"; - ruleset_create_vm_chain($ruleset, $tapchain, $options, $hostfw_options, $macaddr, $direction); + ruleset_create_vm_chain($ruleset, $tapchain, $options, $macaddr, $direction); ruleset_generate_vm_rules($ruleset, $rules, $cluster_conf, $tapchain, $netid, $direction, $options); @@ -1649,19 +1687,42 @@ sub enable_host_firewall { ruleset_addrule($ruleset, $chain, "-i lo -j ACCEPT"); ruleset_chain_add_conn_filters($ruleset, $chain, 'ACCEPT'); - ruleset_chain_add_input_filters($ruleset, $chain, $options); - - ruleset_addrule($ruleset, $chain, "-m addrtype --dst-type MULTICAST -j ACCEPT"); - ruleset_addrule($ruleset, $chain, "-p udp -m conntrack --ctstate NEW --dport 5404:5405 -j ACCEPT"); - ruleset_addrule($ruleset, $chain, "-p udp -m udp --dport 9000 -j ACCEPT"); #corosync + ruleset_chain_add_input_filters($ruleset, $chain, $options, $cluster_conf, $loglevel); # we use RETURN because we need to check also tap rules my $accept_action = 'RETURN'; + ruleset_addrule($ruleset, $chain, "-p igmp -j $accept_action"); # important for multicast + # add host rules first, so that cluster wide rules can be overwritten foreach my $rule (@$rules, @$cluster_rules) { - next if $rule->{type} ne 'in'; - ruleset_generate_rule($ruleset, $chain, $rule, { ACCEPT => $accept_action, REJECT => "PVEFW-reject" }, undef, $cluster_conf); + $rule->{iface_in} = $rule->{iface} if $rule->{iface}; + eval { + if ($rule->{type} eq 'group') { + ruleset_add_group_rule($ruleset, $cluster_conf, $chain, $rule, 'IN', $accept_action); + } elsif ($rule->{type} eq 'in') { + ruleset_generate_rule($ruleset, $chain, $rule, { ACCEPT => $accept_action, REJECT => "PVEFW-reject" }, + undef, $cluster_conf); + } + }; + warn $@ if $@; + delete $rule->{iface_in}; + } + + # allow standard traffic for management ipset (includes cluster network) + my $mngmntsrc = "-m set --match-set PVEFW-management src"; + ruleset_addrule($ruleset, $chain, "$mngmntsrc -p tcp --dport 8006 -j $accept_action"); # PVE API + ruleset_addrule($ruleset, $chain, "$mngmntsrc -p tcp --dport 5900:5999 -j $accept_action"); # PVE VNC Console + ruleset_addrule($ruleset, $chain, "$mngmntsrc -p tcp --dport 3128 -j $accept_action"); # SPICE Proxy + ruleset_addrule($ruleset, $chain, "$mngmntsrc -p tcp --dport 22 -j $accept_action"); # SSH + + my $localnet = local_network(); + + # corosync + if ($localnet) { + my $corosync_rule = "-p udp --dport 5404:5405 -j $accept_action"; + ruleset_addrule($ruleset, $chain, "-s $localnet -d $localnet $corosync_rule"); + ruleset_addrule($ruleset, $chain, "-s $localnet -m addrtype --dst-type MULTICAST $corosync_rule"); } # implement input policy @@ -1678,17 +1739,36 @@ sub enable_host_firewall { ruleset_chain_add_conn_filters($ruleset, $chain, 'ACCEPT'); - ruleset_addrule($ruleset, $chain, "-m addrtype --dst-type MULTICAST -j ACCEPT"); - ruleset_addrule($ruleset, $chain, "-p udp -m conntrack --ctstate NEW --dport 5404:5405 -j ACCEPT"); - ruleset_addrule($ruleset, $chain, "-p udp -m udp --dport 9000 -j ACCEPT"); #corosync - # we use RETURN because we may want to check other thigs later $accept_action = 'RETURN'; + ruleset_addrule($ruleset, $chain, "-p igmp -j $accept_action"); # important for multicast + # add host rules first, so that cluster wide rules can be overwritten foreach my $rule (@$rules, @$cluster_rules) { - next if $rule->{type} ne 'out'; - ruleset_generate_rule($ruleset, $chain, $rule, { ACCEPT => $accept_action, REJECT => "PVEFW-reject" }, undef, $cluster_conf); + $rule->{iface_out} = $rule->{iface} if $rule->{iface}; + eval { + if ($rule->{type} eq 'group') { + ruleset_add_group_rule($ruleset, $cluster_conf, $chain, $rule, 'OUT', $accept_action); + } elsif ($rule->{type} eq 'out') { + ruleset_generate_rule($ruleset, $chain, $rule, { ACCEPT => $accept_action, REJECT => "PVEFW-reject" }, + undef, $cluster_conf); + } + }; + warn $@ if $@; + delete $rule->{iface_out}; + } + + # allow standard traffic on cluster network + if ($localnet) { + ruleset_addrule($ruleset, $chain, "-d $localnet -p tcp --dport 8006 -j $accept_action"); # PVE API + ruleset_addrule($ruleset, $chain, "-d $localnet -p tcp --dport 22 -j $accept_action"); # SSH + ruleset_addrule($ruleset, $chain, "-d $localnet -p tcp --dport 5900:5999 -j $accept_action"); # PVE VNC Console + ruleset_addrule($ruleset, $chain, "-d $localnet -p tcp --dport 3128 -j $accept_action"); # SPICE Proxy + + my $corosync_rule = "-p udp --dport 5404:5405 -j $accept_action"; + ruleset_addrule($ruleset, $chain, "-d $localnet $corosync_rule"); + ruleset_addrule($ruleset, $chain, "-m addrtype --dst-type MULTICAST $corosync_rule"); } # implement output policy @@ -1701,10 +1781,14 @@ sub enable_host_firewall { sub generate_group_rules { my ($ruleset, $cluster_conf, $group) = @_; - die "no such security group '$group'\n" if !$cluster_conf->{groups}->{$group}; my $rules = $cluster_conf->{groups}->{$group}; + if (!$rules) { + warn "no such security group '$group'\n"; + $rules = []; # create empty chain + } + my $chain = "GROUP-${group}-IN"; ruleset_create_chain($ruleset, $chain); @@ -1736,9 +1820,11 @@ for (my $i = 0; $i < $MAX_NETS; $i++) { } sub parse_fw_rule { - my ($line, $need_iface, $allow_groups) = @_; + my ($line, $allow_iface, $allow_groups) = @_; + + my ($type, $action, $macro, $iface, $source, $dest, $proto, $dport, $sport); - my ($type, $action, $iface, $source, $dest, $proto, $dport, $sport); + chomp $line; # we can add single line comments to the end of the rule my $comment = decode('utf8', $1) if $line =~ s/#\s*(.*?)\s*$//; @@ -1748,22 +1834,11 @@ sub parse_fw_rule { $enable = 0 if $line =~ s/^\|//; - my @data = split(/\s+/, $line); - my $expected_elements = $need_iface ? 8 : 7; - - die "wrong number of rule elements\n" if scalar(@data) > $expected_elements; - - if ($need_iface) { - ($type, $action, $iface, $source, $dest, $proto, $dport, $sport) = @data - } else { - ($type, $action, $source, $dest, $proto, $dport, $sport) = @data; - } - - die "incomplete rule\n" if ! ($type && $action); - - my $macro; - - $type = lc($type); + $line =~ s/^(\S+)\s+(\S+)\s*// || + die "unable to parse rule: $line\n"; + + $type = lc($1); + $action = $2; if ($type eq 'in' || $type eq 'out') { if ($action =~ m/^(ACCEPT|DROP|REJECT)$/) { @@ -1777,32 +1852,52 @@ sub parse_fw_rule { die "unknown action '$action'\n"; } } elsif ($type eq 'group') { - die "wrong number of rule elements\n" if scalar(@data) != 3; die "groups disabled\n" if !$allow_groups; - die "invalid characters in group name\n" if $action !~ m/^${security_group_name_pattern}$/; } else { die "unknown rule type '$type'\n"; } - if ($need_iface) { - $iface = undef if $iface && $iface eq '-'; - } - - $proto = undef if $proto && $proto eq '-'; - pve_fw_verify_protocol_spec($proto) if $proto; + while (length($line)) { + if ($line =~ s/^-i (\S+)\s*//) { + die "parameter -i not allowed\n" if !$allow_iface; + $iface = $1; + PVE::JSONSchema::pve_verify_iface($iface); + next; + } - $source = undef if $source && $source eq '-'; - $dest = undef if $dest && $dest eq '-'; + last if $type eq 'group'; - $dport = undef if $dport && $dport eq '-'; - $sport = undef if $sport && $sport eq '-'; + if ($line =~ s/^-p (\S+)\s*//) { + $proto = $1; + pve_fw_verify_protocol_spec($proto); + next; + } + if ($line =~ s/^-dport (\S+)\s*//) { + $dport = $1; + parse_port_name_number_or_range($dport); + next; + } + if ($line =~ s/^-sport (\S+)\s*//) { + $sport = $1; + parse_port_name_number_or_range($sport); + next; + } + if ($line =~ s/^-source (\S+)\s*//) { + $source = $1; + parse_address_list($source); + next; + } + if ($line =~ s/^-dest (\S+)\s*//) { + $dest = $1; + parse_address_list($dest); + next; + } - parse_port_name_number_or_range($dport) if defined($dport); - parse_port_name_number_or_range($sport) if defined($sport); + last; + } - parse_address_list($source) if $source; - parse_address_list($dest) if $dest; + die "unable to parse rule parameters: $line\n" if length($line); return { type => $type, @@ -1889,7 +1984,7 @@ sub parse_clusterfw_option { return ($opt, $value); } -sub parse_clusterfw_alias { +sub parse_alias { my ($line) = @_; # we can add single line comments to the end of the line @@ -1913,7 +2008,11 @@ sub parse_clusterfw_alias { sub parse_vm_fw_rules { my ($filename, $fh) = @_; - my $res = { rules => [], options => {}}; + my $res = { + rules => [], + options => {}, + aliases => {}, + }; my $section; @@ -1945,6 +2044,15 @@ sub parse_vm_fw_rules { next; } + if ($section eq 'aliases') { + eval { + my $data = parse_alias($line); + $res->{aliases}->{lc($data->{name})} = $data; + }; + warn "$prefix: $@" if $@; + next; + } + my $rule; eval { $rule = parse_fw_rule($line, 1, 1); }; if (my $err = $@) { @@ -2077,7 +2185,7 @@ sub parse_cluster_fw_rules { warn "$prefix: $@" if $@; } elsif ($section eq 'aliases') { eval { - my $data = parse_clusterfw_alias($line); + my $data = parse_alias($line); $res->{aliases}->{lc($data->{name})} = $data; }; warn "$prefix: $@" if $@; @@ -2190,7 +2298,7 @@ sub load_vmfw_conf { } my $format_rules = sub { - my ($rules, $need_iface) = @_; + my ($rules, $allow_iface) = @_; my $raw = ''; @@ -2203,14 +2311,16 @@ my $format_rules = sub { } else { $raw .= " " . $rule->{action}; } - $raw .= " " . ($rule->{iface} || '-') if $need_iface; + if ($allow_iface && $rule->{iface}) { + $raw .= " -i $rule->{iface}"; + } if ($rule->{type} ne 'group') { - $raw .= " " . ($rule->{source} || '-'); - $raw .= " " . ($rule->{dest} || '-'); - $raw .= " " . ($rule->{proto} || '-'); - $raw .= " " . ($rule->{dport} || '-'); - $raw .= " " . ($rule->{sport} || '-'); + $raw .= " -source $rule->{source}" if $rule->{source}; + $raw .= " -dest $rule->{dest}" if $rule->{dest}; + $raw .= " -p $rule->{proto}" if $rule->{proto}; + $raw .= " -dport $rule->{dport}" if $rule->{dport}; + $raw .= " -sport $rule->{sport}" if $rule->{sport}; } $raw .= " # " . encode('utf8', $rule->{comment}) @@ -2286,6 +2396,9 @@ sub save_vmfw_conf { my $options = $vmfw_conf->{options}; $raw .= &$format_options($options) if scalar(keys %$options); + my $aliases = $vmfw_conf->{aliases}; + $raw .= &$format_aliases($aliases) if scalar(keys %$aliases); + my $rules = $vmfw_conf->{rules} || []; if (scalar(@$rules)) { $raw .= "[RULES]\n\n"; @@ -2422,30 +2535,6 @@ sub round_powerof2 { return ++$int; } -sub save_pvefw_status { - my ($status) = @_; - - die "unknown status '$status' - internal error" - if $status !~ m/^(stopped|active)$/; - - mkdir dirname($pve_fw_status_filename); - PVE::Tools::file_set_contents($pve_fw_status_filename, $status); -} - -sub read_pvefw_status { - - my $status = 'unknown'; - - return 'stopped' if ! -f $pve_fw_status_filename; - - eval { - $status = PVE::Tools::file_get_contents($pve_fw_status_filename); - }; - warn $@ if $@; - - return $status; -} - sub load_clusterfw_conf { my ($filename) = @_; @@ -2543,11 +2632,9 @@ sub compile { if ($vmdata) { # test mode my $testdir = $vmdata->{testdir} || die "no test directory specified"; my $filename = "$testdir/cluster.fw"; - die "missing test file '$filename'\n" if ! -f $filename; $cluster_conf = load_clusterfw_conf($filename); $filename = "$testdir/host.fw"; - die "missing test file '$filename'\n" if ! -f $filename; $hostfw_conf = load_hostfw_conf($filename); $vmfw_configs = read_vm_firewall_configs($vmdata, $testdir); @@ -2561,9 +2648,18 @@ sub compile { } - my $ipset_ruleset = {}; - generate_ipset_chains($ipset_ruleset, $cluster_conf); + $cluster_conf->{ipset}->{venet0} = []; + + my $localnet; + if ($cluster_conf->{aliases}->{local_network}) { + $localnet = $cluster_conf->{aliases}->{local_network}->{cidr}; + } else { + $localnet = local_network() || '127.0.0.0/8'; + $cluster_conf->{aliases}->{local_network} = { cidr => $localnet }; + } + push @{$cluster_conf->{ipset}->{management}}, { cidr => $localnet }; + my $ruleset = {}; ruleset_create_chain($ruleset, "PVEFW-INPUT"); @@ -2578,17 +2674,12 @@ sub compile { ruleset_chain_add_conn_filters($ruleset, "PVEFW-FORWARD", "ACCEPT"); - if ($cluster_conf->{ipset}->{blacklist}){ - ruleset_addlog($ruleset, "PVEFW-FORWARD", 0, "DROP: ", $loglevel, "-m set --match-set PVEFW-blacklist src"); - ruleset_addrule($ruleset, "PVEFW-FORWARD", "-m set --match-set PVEFW-blacklist src -j DROP"); - } - ruleset_create_chain($ruleset, "PVEFW-VENET-OUT"); - ruleset_addrule($ruleset, "PVEFW-FORWARD", "-i venet0 -j PVEFW-VENET-OUT"); - ruleset_addrule($ruleset, "PVEFW-INPUT", "-i venet0 -j PVEFW-VENET-OUT"); + ruleset_addrule($ruleset, "PVEFW-FORWARD", "-i venet0 -m set --match-set PVEFW-venet0 src -j PVEFW-VENET-OUT"); + ruleset_addrule($ruleset, "PVEFW-INPUT", "-i venet0 -m set --match-set PVEFW-venet0 src -j PVEFW-VENET-OUT"); ruleset_create_chain($ruleset, "PVEFW-FWBR-IN"); - ruleset_chain_add_input_filters($ruleset, "PVEFW-FWBR-IN", $hostfw_options); + ruleset_chain_add_input_filters($ruleset, "PVEFW-FWBR-IN", $hostfw_options, $cluster_conf, $loglevel); ruleset_addrule($ruleset, "PVEFW-FORWARD", "-m physdev --physdev-is-bridged --physdev-in fwln+ -j PVEFW-FWBR-IN"); @@ -2596,73 +2687,95 @@ sub compile { ruleset_addrule($ruleset, "PVEFW-FORWARD", "-m physdev --physdev-is-bridged --physdev-out fwln+ -j PVEFW-FWBR-OUT"); ruleset_create_chain($ruleset, "PVEFW-VENET-IN"); - ruleset_chain_add_input_filters($ruleset, "PVEFW-VENET-IN", $hostfw_options); + ruleset_chain_add_input_filters($ruleset, "PVEFW-VENET-IN", $hostfw_options, $cluster_conf, $loglevel); - ruleset_addrule($ruleset, "PVEFW-FORWARD", "-o venet0 -j PVEFW-VENET-IN"); + ruleset_addrule($ruleset, "PVEFW-FORWARD", "-o venet0 -m set --match-set PVEFW-venet0 dst -j PVEFW-VENET-IN"); generate_std_chains($ruleset, $hostfw_options); my $hostfw_enable = !(defined($hostfw_options->{enable}) && ($hostfw_options->{enable} == 0)); - enable_host_firewall($ruleset, $hostfw_conf, $cluster_conf) if $hostfw_enable; + if ($hostfw_enable) { + eval { enable_host_firewall($ruleset, $hostfw_conf, $cluster_conf); }; + warn $@ if $@; # just to be sure - should not happen + } - ruleset_addrule($ruleset, "PVEFW-OUTPUT", "-o venet0 -j PVEFW-VENET-IN"); + ruleset_addrule($ruleset, "PVEFW-OUTPUT", "-o venet0 -m set --match-set PVEFW-venet0 dst -j PVEFW-VENET-IN"); # generate firewall rules for QEMU VMs foreach my $vmid (keys %{$vmdata->{qemu}}) { - my $conf = $vmdata->{qemu}->{$vmid}; - my $vmfw_conf = $vmfw_configs->{$vmid}; - next if !$vmfw_conf; - next if defined($vmfw_conf->{options}->{enable}) && ($vmfw_conf->{options}->{enable} == 0); - - foreach my $netid (keys %$conf) { - next if $netid !~ m/^net(\d+)$/; - my $net = PVE::QemuServer::parse_net($conf->{$netid}); - next if !$net; - my $iface = "tap${vmid}i$1"; - - my $macaddr = $net->{macaddr}; - generate_tap_rules_direction($ruleset, $cluster_conf, $hostfw_conf, $iface, $netid, $macaddr, - $vmfw_conf, $vmid, 'IN'); - generate_tap_rules_direction($ruleset, $cluster_conf, $hostfw_conf, $iface, $netid, $macaddr, - $vmfw_conf, $vmid, 'OUT'); - } + eval { + my $conf = $vmdata->{qemu}->{$vmid}; + my $vmfw_conf = $vmfw_configs->{$vmid}; + return if !$vmfw_conf; + return if !$vmfw_conf->{options}->{enable}; + + foreach my $netid (keys %$conf) { + next if $netid !~ m/^net(\d+)$/; + my $net = PVE::QemuServer::parse_net($conf->{$netid}); + next if !$net->{firewall}; + my $iface = "tap${vmid}i$1"; + + my $macaddr = $net->{macaddr}; + generate_tap_rules_direction($ruleset, $cluster_conf, $iface, $netid, $macaddr, + $vmfw_conf, $vmid, 'IN'); + generate_tap_rules_direction($ruleset, $cluster_conf, $iface, $netid, $macaddr, + $vmfw_conf, $vmid, 'OUT'); + } + }; + warn $@ if $@; # just to be sure - should not happen } # generate firewall rules for OpenVZ containers foreach my $vmid (keys %{$vmdata->{openvz}}) { - my $conf = $vmdata->{openvz}->{$vmid}; + eval { + my $conf = $vmdata->{openvz}->{$vmid}; - my $vmfw_conf = $vmfw_configs->{$vmid}; - next if !$vmfw_conf; - next if defined($vmfw_conf->{options}->{enable}) && ($vmfw_conf->{options}->{enable} == 0); + my $vmfw_conf = $vmfw_configs->{$vmid}; + return if !$vmfw_conf; + return if !$vmfw_conf->{options}->{enable}; - if ($conf->{ip_address} && $conf->{ip_address}->{value}) { - my $ip = $conf->{ip_address}->{value}; - $ip =~ s/\s+/,/g; - generate_venet_rules_direction($ruleset, $cluster_conf, $hostfw_conf, $vmfw_conf, $vmid, $ip, 'IN'); - generate_venet_rules_direction($ruleset, $cluster_conf, $hostfw_conf, $vmfw_conf, $vmid, $ip, 'OUT'); - } + if ($conf->{ip_address} && $conf->{ip_address}->{value}) { + my $ip = $conf->{ip_address}->{value}; + $ip =~ s/\s+/,/g; + parse_address_list($ip); # make sure we have a valid $ip list - if ($conf->{netif} && $conf->{netif}->{value}) { - my $netif = PVE::OpenVZ::parse_netif($conf->{netif}->{value}); - foreach my $netid (keys %$netif) { - my $d = $netif->{$netid}; + my @ips = split(',', $ip); - my $macaddr = $d->{mac}; - my $iface = $d->{host_ifname}; - generate_tap_rules_direction($ruleset, $cluster_conf, $hostfw_conf, $iface, $netid, $macaddr, - $vmfw_conf, $vmid, 'IN'); - generate_tap_rules_direction($ruleset, $cluster_conf, $hostfw_conf, $iface, $netid, $macaddr, - $vmfw_conf, $vmid, 'OUT'); + foreach my $singleip (@ips) { + my $venet0ipset = {}; + $venet0ipset->{cidr} = $singleip; + push @{$cluster_conf->{ipset}->{venet0}}, $venet0ipset; + } + + generate_venet_rules_direction($ruleset, $cluster_conf, $vmfw_conf, $vmid, $ip, 'IN'); + generate_venet_rules_direction($ruleset, $cluster_conf, $vmfw_conf, $vmid, $ip, 'OUT'); } - } + + if ($conf->{netif} && $conf->{netif}->{value}) { + my $netif = PVE::OpenVZ::parse_netif($conf->{netif}->{value}); + foreach my $netid (keys %$netif) { + my $d = $netif->{$netid}; + + my $macaddr = $d->{mac}; + my $iface = $d->{host_ifname}; + generate_tap_rules_direction($ruleset, $cluster_conf, $iface, $netid, $macaddr, + $vmfw_conf, $vmid, 'IN'); + generate_tap_rules_direction($ruleset, $cluster_conf, $iface, $netid, $macaddr, + $vmfw_conf, $vmid, 'OUT'); + } + } + }; + warn $@ if $@; # just to be sure - should not happen } if(ruleset_chain_exist($ruleset, "PVEFW-IPS")){ ruleset_insertrule($ruleset, "PVEFW-FORWARD", "-m conntrack --ctstate RELATED,ESTABLISHED -j PVEFW-IPS"); } + my $ipset_ruleset = {}; + generate_ipset_chains($ipset_ruleset, $cluster_conf); + return ($ruleset, $ipset_ruleset); } @@ -2932,8 +3045,18 @@ sub remove_pvefw_chains { iptables_restore_cmdlist($cmdlist); } +sub init { + my $cluster_conf = load_clusterfw_conf(); + my $cluster_options = $cluster_conf->{options}; + my $enable = $cluster_options->{enable}; + + return if !$enable; + + # load required modules here +} + sub update { - my ($start, $verbose) = @_; + my ($verbose) = @_; my $code = sub { @@ -2942,9 +3065,7 @@ sub update { my $enable = $cluster_options->{enable}; - my $status = read_pvefw_status(); - - die "Firewall is disabled - cannot start\n" if !$enable && $start; + die "Firewall is disabled - cannot start\n" if !$enable; if (!$enable) { PVE::Firewall::remove_pvefw_chains(); @@ -2956,18 +3077,10 @@ sub update { my ($ruleset, $ipset_ruleset) = compile($cluster_conf, $hostfw_conf); - if ($start || $status eq 'active') { - - save_pvefw_status('active') if ($status ne 'active'); - - apply_ruleset($ruleset, $hostfw_conf, $ipset_ruleset, $verbose); - } else { - print "Firewall not active (status = $status)\n" if $verbose; - } + apply_ruleset($ruleset, $hostfw_conf, $ipset_ruleset, $verbose); }; run_locked($code); } - 1;