X-Git-Url: https://git.proxmox.com/?p=pve-firewall.git;a=blobdiff_plain;f=PVE%2FFirewall.pm;h=f5ae88f392bd76fbb9e5c10374f6957509507a95;hp=1b52ca3a2029f326d2040d5517a3a03803db9db5;hb=35b66e9d6bd3243b3afd979b3305e7c9e5f48c57;hpb=8c32a215951ba04399f29ef651bdf8dc3fdd3011 diff --git a/PVE/Firewall.pm b/PVE/Firewall.pm index 1b52ca3..f5ae88f 100644 --- a/PVE/Firewall.pm +++ b/PVE/Firewall.pm @@ -16,18 +16,334 @@ use Data::Dumper; my $pve_fw_lock_filename = "/var/lock/pvefw.lck"; # todo: define more MACROS -# inspired by: /usr/share/shorewall/macro.* +# imported/converted from: /usr/share/shorewall/macro.* my $pve_fw_macros = { - BitTorrent => [ + 'Amanda' => [ + { action => 'PARAM', proto => 'udp', dport => '10080' }, + { action => 'PARAM', proto => 'tcp', dport => '10080' }, + ], + 'Auth' => [ + { action => 'PARAM', proto => 'tcp', dport => '113' }, + ], + 'BGP' => [ + { action => 'PARAM', proto => 'tcp', dport => '179' }, + ], + 'BitTorrent' => [ { action => 'PARAM', proto => 'tcp', dport => '6881:6889' }, - { action => 'PARAM', proto => 'udp', dport => '6881' } + { action => 'PARAM', proto => 'udp', dport => '6881' }, + ], + 'BitTorrent32' => [ + { action => 'PARAM', proto => 'tcp', dport => '6881:6999' }, + { action => 'PARAM', proto => 'udp', dport => '6881' }, + ], + 'CVS' => [ + { action => 'PARAM', proto => 'tcp', dport => '2401' }, + ], + 'Citrix' => [ + { action => 'PARAM', proto => 'tcp', dport => '1494' }, + { action => 'PARAM', proto => 'udp', dport => '1604' }, + { action => 'PARAM', proto => 'tcp', dport => '2598' }, + ], + 'DAAP' => [ + { action => 'PARAM', proto => 'tcp', dport => '3689' }, + { action => 'PARAM', proto => 'udp', dport => '3689' }, + ], + 'DCC' => [ + { action => 'PARAM', proto => 'tcp', dport => '6277' }, + ], + 'DHCPfwd' => [ + { 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' => [ + { action => 'PARAM', proto => 'udp', dport => '53' }, + { action => 'PARAM', proto => 'tcp', dport => '53' }, + ], + 'Distcc' => [ + { action => 'PARAM', proto => 'tcp', dport => '3632' }, + ], + 'Edonkey' => [ + { action => 'PARAM', proto => 'tcp', dport => '4662' }, + { action => 'PARAM', proto => 'udp', dport => '4665' }, + ], + 'FTP' => [ + { action => 'PARAM', proto => 'tcp', dport => '21' }, + ], + 'Finger' => [ + { action => 'PARAM', proto => 'tcp', dport => '79' }, ], - HTTP => [ + 'GNUnet' => [ + { action => 'PARAM', proto => 'tcp', dport => '2086' }, + { action => 'PARAM', proto => 'udp', dport => '2086' }, + { action => 'PARAM', proto => 'tcp', dport => '1080' }, + { action => 'PARAM', proto => 'udp', dport => '1080' }, + ], + 'GRE' => [ + { action => 'PARAM', proto => '47' }, + { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => '47' }, + ], + 'Git' => [ + { action => 'PARAM', proto => 'tcp', dport => '9418' }, + ], + 'Gnutella' => [ + { action => 'PARAM', proto => 'tcp', dport => '6346' }, + { action => 'PARAM', proto => 'udp', dport => '6346' }, + ], + 'HKP' => [ + { action => 'PARAM', proto => 'tcp', dport => '11371' }, + ], + 'HTTP' => [ { action => 'PARAM', proto => 'tcp', dport => '80' }, ], - HTTPS => [ + 'HTTPS' => [ { action => 'PARAM', proto => 'tcp', dport => '443' }, ], + 'ICPV2' => [ + { action => 'PARAM', proto => 'udp', dport => '3130' }, + ], + 'ICQ' => [ + { action => 'PARAM', proto => 'tcp', dport => '5190' }, + ], + 'IMAP' => [ + { action => 'PARAM', proto => 'tcp', dport => '143' }, + ], + 'IMAPS' => [ + { action => 'PARAM', proto => 'tcp', dport => '993' }, + ], + 'IPIP' => [ + { action => 'PARAM', proto => '94' }, + { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => '94' }, + ], + 'IPP' => [ + { action => 'PARAM', proto => 'tcp', dport => '631' }, + ], + 'IPPbrd' => [ + { action => 'PARAM', proto => 'udp', dport => '631' }, + ], + 'IPPserver' => [ + { action => 'PARAM', source => 'SOURCE', dest => 'DEST', proto => 'tcp', dport => '631' }, + { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '631' }, + ], + 'IPsec' => [ + { 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' => [ + { 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' => [ + { 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' => [ + { action => 'PARAM', proto => 'tcp', dport => '6667' }, + ], + 'JabberPlain' => [ + { action => 'PARAM', proto => 'tcp', dport => '5222' }, + ], + 'JabberSecure' => [ + { action => 'PARAM', proto => 'tcp', dport => '5223' }, + ], + 'Jabberd' => [ + { action => 'PARAM', proto => 'tcp', dport => '5269' }, + ], + 'Jetdirect' => [ + { action => 'PARAM', proto => 'tcp', dport => '9100' }, + ], + 'L2TP' => [ + { action => 'PARAM', proto => 'udp', dport => '1701' }, + { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '1701' }, + ], + 'LDAP' => [ + { action => 'PARAM', proto => 'tcp', dport => '389' }, + ], + 'LDAPS' => [ + { action => 'PARAM', proto => 'tcp', dport => '636' }, + ], + 'MSNP' => [ + { action => 'PARAM', proto => 'tcp', dport => '1863' }, + ], + 'MSSQL' => [ + { action => 'PARAM', proto => 'tcp', dport => '1433' }, + ], + 'Mail' => [ + { action => 'PARAM', proto => 'tcp', dport => '25' }, + { action => 'PARAM', proto => 'tcp', dport => '465' }, + { action => 'PARAM', proto => 'tcp', dport => '587' }, + ], + 'Munin' => [ + { action => 'PARAM', proto => 'tcp', dport => '4949' }, + ], + 'MySQL' => [ + { action => 'PARAM', proto => 'tcp', dport => '3306' }, + ], + 'NNTP' => [ + { action => 'PARAM', proto => 'tcp', dport => '119' }, + ], + 'NNTPS' => [ + { action => 'PARAM', proto => 'tcp', dport => '563' }, + ], + 'NTP' => [ + { action => 'PARAM', proto => 'udp', dport => '123' }, + ], + 'NTPbi' => [ + { action => 'PARAM', proto => 'udp', dport => '123' }, + { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '123' }, + ], + 'NTPbrd' => [ + { action => 'PARAM', proto => 'udp', dport => '123' }, + { action => 'PARAM', proto => 'udp', dport => '1024:65535', sport => '123' }, + ], + 'OSPF' => [ + { action => 'PARAM', proto => '89' }, + ], + 'OpenVPN' => [ + { action => 'PARAM', proto => 'udp', dport => '1194' }, + ], + 'PCA' => [ + { action => 'PARAM', proto => 'udp', dport => '5632' }, + { action => 'PARAM', proto => 'tcp', dport => '5631' }, + ], + 'POP3' => [ + { action => 'PARAM', proto => 'tcp', dport => '110' }, + ], + 'POP3S' => [ + { action => 'PARAM', proto => 'tcp', dport => '995' }, + ], + 'PPtP' => [ + { action => 'PARAM', proto => '47' }, + { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => '47' }, + { action => 'PARAM', proto => 'tcp', dport => '1723' }, + ], + 'Ping' => [ + { action => 'PARAM', proto => 'icmp', dport => '8' }, + ], + 'PostgreSQL' => [ + { action => 'PARAM', proto => 'tcp', dport => '5432' }, + ], + 'Printer' => [ + { action => 'PARAM', proto => 'tcp', dport => '515' }, + ], + 'RDP' => [ + { action => 'PARAM', proto => 'tcp', dport => '3389' }, + ], + 'RIPbi' => [ + { action => 'PARAM', proto => 'udp', dport => '520' }, + { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '520' }, + ], + 'RNDC' => [ + { action => 'PARAM', proto => 'tcp', dport => '953' }, + ], + 'Razor' => [ + { action => 'ACCEPT', proto => 'tcp', dport => '2703' }, + ], + 'Rdate' => [ + { action => 'PARAM', proto => 'tcp', dport => '37' }, + ], + 'Rsync' => [ + { action => 'PARAM', proto => 'tcp', dport => '873' }, + ], + 'SANE' => [ + { action => 'PARAM', proto => 'tcp', dport => '6566' }, + ], + 'SMB' => [ + { 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' }, + ], + 'SMBBI' => [ + { 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' => [ + { action => 'PARAM', proto => 'tcp', dport => '901' }, + ], + 'SMTP' => [ + { action => 'PARAM', proto => 'tcp', dport => '25' }, + ], + 'SMTPS' => [ + { action => 'PARAM', proto => 'tcp', dport => '465' }, + ], + 'SNMP' => [ + { action => 'PARAM', proto => 'udp', dport => '161:162' }, + { action => 'PARAM', proto => 'tcp', dport => '161' }, + ], + 'SPAMD' => [ + { action => 'PARAM', proto => 'tcp', dport => '783' }, + ], + 'SSH' => [ + { action => 'PARAM', proto => 'tcp', dport => '22' }, + ], + 'SVN' => [ + { action => 'PARAM', proto => 'tcp', dport => '3690' }, + ], + 'SixXS' => [ + { action => 'PARAM', proto => 'tcp', dport => '3874' }, + { action => 'PARAM', proto => 'udp', dport => '3740' }, + { action => 'PARAM', proto => '41' }, + { action => 'PARAM', proto => 'udp', dport => '5072,8374' }, + ], + 'Squid' => [ + { action => 'PARAM', proto => 'tcp', dport => '3128' }, + ], + 'Submission' => [ + { action => 'PARAM', proto => 'tcp', dport => '587' }, + ], + 'Syslog' => [ + { action => 'PARAM', proto => 'udp', dport => '514' }, + { action => 'PARAM', proto => 'tcp', dport => '514' }, + ], + 'TFTP' => [ + { action => 'PARAM', proto => 'udp', dport => '69' }, + ], + 'Telnet' => [ + { action => 'PARAM', proto => 'tcp', dport => '23' }, + ], + 'Telnets' => [ + { action => 'PARAM', proto => 'tcp', dport => '992' }, + ], + 'Time' => [ + { action => 'PARAM', proto => 'tcp', dport => '37' }, + ], + 'Trcrt' => [ + { action => 'PARAM', proto => 'udp', dport => '33434:33524' }, + { action => 'PARAM', proto => 'icmp', dport => '8' }, + ], + 'VNC' => [ + { action => 'PARAM', proto => 'tcp', dport => '5900:5909' }, + ], + 'VNCL' => [ + { action => 'PARAM', proto => 'tcp', dport => '5500' }, + ], + 'Web' => [ + { action => 'PARAM', proto => 'tcp', dport => '80' }, + { action => 'PARAM', proto => 'tcp', dport => '443' }, + ], + 'Webcache' => [ + { action => 'PARAM', proto => 'tcp', dport => '8080' }, + ], + 'Webmin' => [ + { action => 'PARAM', proto => 'tcp', dport => '10000' }, + ], + 'Whois' => [ + { action => 'PARAM', proto => 'tcp', dport => '43' }, + ], }; my $pve_fw_parsed_macros; @@ -73,6 +389,7 @@ sub get_etc_services { if ($line =~ m!^(\S+)\s+(\S+)/(tcp|udp).*$!) { $services->{byid}->{$2}->{name} = $1; + $services->{byid}->{$2}->{port} = $2; $services->{byid}->{$2}->{$3} = 1; $services->{byname}->{$1} = $services->{byid}->{$2}; } @@ -141,9 +458,13 @@ sub parse_port_name_number_or_range { my $nbports = 0; foreach my $item (split(/,/, $str)) { my $portlist = ""; + my $oldpon = undef; foreach my $pon (split(':', $item, 2)) { + $pon = $services->{byname}->{$pon}->{port} if $services->{byname}->{$pon}->{port}; if ($pon =~ m/^\d+$/){ - die "invalid port '$pon'\n" if $pon < 0 && $pon > 65536; + die "invalid port '$pon'\n" if $pon < 0 && $pon > 65535; + die "port '$pon' must be bigger than port '$oldpon' \n" if $oldpon && ($pon < $oldpon); + $oldpon = $pon; }else{ die "invalid port $services->{byname}->{$pon}\n" if !$services->{byname}->{$pon}; } @@ -322,7 +643,7 @@ sub generate_bridge_chains { if (!ruleset_chain_exist($ruleset, "PVEFW-FORWARD")){ ruleset_create_chain($ruleset, "PVEFW-FORWARD"); - ruleset_addrule($ruleset, "PVEFW-FORWARD", "-m state --state RELATED,ESTABLISHED -j ACCEPT"); + ruleset_addrule($ruleset, "PVEFW-FORWARD", "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT"); ruleset_addrule($ruleset, "PVEFW-FORWARD", "-m physdev --physdev-is-in --physdev-is-bridged -j PVEFW-BRIDGE-OUT"); ruleset_addrule($ruleset, "PVEFW-FORWARD", "-m physdev --physdev-is-out --physdev-is-bridged -j PVEFW-BRIDGE-IN"); } @@ -348,8 +669,8 @@ sub generate_tap_rules_direction { ruleset_create_chain($ruleset, $tapchain); - ruleset_addrule($ruleset, $tapchain, "-m state --state INVALID -j DROP"); - ruleset_addrule($ruleset, $tapchain, "-m state --state RELATED,ESTABLISHED -j ACCEPT"); + ruleset_addrule($ruleset, $tapchain, "-m conntrack --ctstate INVALID -j DROP"); + ruleset_addrule($ruleset, $tapchain, "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT"); if ($direction eq 'OUT' && defined($macaddr)) { ruleset_addrule($ruleset, $tapchain, "-m mac ! --mac-source $macaddr -j DROP"); @@ -399,11 +720,11 @@ sub enablehostfw { my $chain = "PVEFW-HOST-IN"; ruleset_create_chain($ruleset, $chain); - ruleset_addrule($ruleset, $chain, "-m state --state INVALID -j DROP"); - ruleset_addrule($ruleset, $chain, "-m state --state RELATED,ESTABLISHED -j ACCEPT"); + ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate INVALID -j DROP"); + ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT"); ruleset_addrule($ruleset, $chain, "-i lo -j ACCEPT"); ruleset_addrule($ruleset, $chain, "-m addrtype --dst-type MULTICAST -j ACCEPT"); - ruleset_addrule($ruleset, $chain, "-p udp -m state --state NEW -m multiport --dports 5404,5405 -j ACCEPT"); + ruleset_addrule($ruleset, $chain, "-p udp -m conntrack --ctstate NEW -m multiport --dports 5404,5405 -j ACCEPT"); ruleset_addrule($ruleset, $chain, "-p udp -m udp --dport 9000 -j ACCEPT"); #corosync if ($rules->{in}) { @@ -421,11 +742,11 @@ sub enablehostfw { $chain = "PVEFW-HOST-OUT"; ruleset_create_chain($ruleset, $chain); - ruleset_addrule($ruleset, $chain, "-m state --state INVALID -j DROP"); - ruleset_addrule($ruleset, $chain, "-m state --state RELATED,ESTABLISHED -j ACCEPT"); + ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate INVALID -j DROP"); + ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT"); ruleset_addrule($ruleset, $chain, "-o lo -j ACCEPT"); ruleset_addrule($ruleset, $chain, "-m addrtype --dst-type MULTICAST -j ACCEPT"); - ruleset_addrule($ruleset, $chain, "-p udp -m state --state NEW -m multiport --dports 5404,5405 -j ACCEPT"); + ruleset_addrule($ruleset, $chain, "-p udp -m conntrack --ctstate NEW -m multiport --dports 5404,5405 -j ACCEPT"); ruleset_addrule($ruleset, $chain, "-p udp -m udp --dport 9000 -j ACCEPT"); #corosync if ($rules->{out}) { @@ -565,7 +886,14 @@ sub parse_fw_rule { my $rule = {}; foreach my $k (keys %$templ) { my $v = $templ->{$k}; - $v = $param->{$k} if $v eq 'PARAM'; + if ($v eq 'PARAM') { + $v = $param->{$k}; + } elsif ($v eq 'DEST') { + $v = $param->{dest}; + } elsif ($v eq 'SOURCE') { + $v = $param->{source}; + } + die "missing parameter '$k' in macro '$macro_name'\n" if !defined($v); $rule->{$k} = $v; } @@ -585,10 +913,29 @@ sub parse_fw_rule { return $rules; } +sub parse_fw_option { + my ($line) = @_; + + my ($opt, $value); + + if ($line =~ m/^enable:\s*(0|1)\s*$/i) { + $opt = 'enable'; + $value = int($1); + } elsif ($line =~ m/^(policy-(in|out)):\s*(ACCEPT|DROP|REJECT)\s*$/i) { + $opt = lc($1); + $value = uc($3); + } else { + chomp $line; + die "can't parse option '$line'\n" + } + + return ($opt, $value); +} + sub parse_vm_fw_rules { my ($filename, $fh) = @_; - my $res = { in => [], out => [] }; + my $res = { in => [], out => [], options => {}}; my $section; @@ -599,8 +946,9 @@ sub parse_vm_fw_rules { my $linenr = $fh->input_line_number(); my $prefix = "$filename (line $linenr)"; - if ($line =~ m/^\[(in|out)\]\s*$/i) { + if ($line =~ m/^\[(\S+)\]\s*$/i) { $section = lc($1); + warn "$prefix: ignore unknown section '$section'\n" if !$res->{$section}; next; } if (!$section) { @@ -608,6 +956,17 @@ sub parse_vm_fw_rules { next; } + next if !$res->{$section}; # skip undefined section + + if ($section eq 'options') { + eval { + my ($opt, $value) = parse_fw_option($line); + $res->{options}->{$opt} = $value; + }; + warn "$prefix: $@" if $@; + next; + } + my $rules; eval { $rules = parse_fw_rule($line, 1, 1); }; if (my $err = $@) { @@ -755,23 +1114,30 @@ sub compile { my $ruleset = {}; - # setup host firewall rules ruleset_create_chain($ruleset, "PVEFW-INPUT"); ruleset_create_chain($ruleset, "PVEFW-OUTPUT"); + ruleset_create_chain($ruleset, "PVEFW-FORWARD"); ruleset_create_chain($ruleset, "PVEFW-SET-ACCEPT-MARK"); ruleset_addrule($ruleset, "PVEFW-SET-ACCEPT-MARK", "-j MARK --set-mark 1"); + my $enable_hostfw = 0; $filename = "/etc/pve/local/host.fw"; if (my $fh = IO::File->new($filename, O_RDONLY)) { my $host_rules = parse_host_fw_rules($filename, $fh); + + $enable_hostfw = 1; + enablehostfw($ruleset, $host_rules, $group_rules); } # generate firewall rules for QEMU VMs foreach my $vmid (keys %{$vmdata->{qemu}}) { my $conf = $vmdata->{qemu}->{$vmid}; + next if !$rules->{$vmid}; + my $options = $rules->{$vmid}->{options}; + next if defined($options->{enable}) && ($options->{enable} == 0); foreach my $netid (keys %$conf) { next if $netid !~ m/^net(\d+)$/; @@ -784,6 +1150,7 @@ sub compile { $bridge .= "v$net->{tag}" if $net->{tag}; + generate_bridge_chains($ruleset, $bridge); my $macaddr = $net->{macaddr}; @@ -791,6 +1158,12 @@ sub compile { generate_tap_rules_direction($ruleset, $group_rules, $iface, $netid, $macaddr, $rules->{$vmid}->{out}, $bridge, 'OUT'); } } + + if ($enable_hostfw) { + # allow traffic from lo (ourself) + ruleset_addrule($ruleset, "PVEFW-INPUT", "-i lo -j ACCEPT"); + } + return $ruleset; } @@ -907,6 +1280,9 @@ sub apply_ruleset { } foreach my $chain (keys %$statushash) { next if $statushash->{$chain}->{action} ne 'delete'; + next if $chain eq 'PVEFW-INPUT'; + next if $chain eq 'PVEFW-OUTPUT'; + next if $chain eq 'PVEFW-FORWARD'; $cmdlist .= "-X $chain\n"; }