X-Git-Url: https://git.proxmox.com/?p=pve-firewall.git;a=blobdiff_plain;f=src%2FPVE%2FFirewall.pm;h=ea8853edc5775c4ce924be5b0c3eb885ac0f5d66;hp=d7788f0005556bbc32ff907dba802782220692d0;hb=ebd54ae918365a722c1091109e74ec21ca7f4be6;hpb=ce36326d1fc77f0dbd141b9bf0771a1ac21b221a diff --git a/src/PVE/Firewall.pm b/src/PVE/Firewall.pm index d7788f0..ea8853e 100644 --- a/src/PVE/Firewall.pm +++ b/src/PVE/Firewall.pm @@ -2,9 +2,11 @@ package PVE::Firewall; use warnings; use strict; +use POSIX; use Data::Dumper; use Digest::SHA; use PVE::INotify; +use PVE::JSONSchema qw(get_standard_option); use PVE::Cluster; use PVE::ProcFSTools; use PVE::Tools; @@ -15,7 +17,10 @@ use Net::IP; use PVE::Tools qw(run_command lock_file); use Encode; -# dynamically include PVE::QemuServer and PVE::OpenVZ +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 # to avoid dependency problems my $have_qemu_server; eval { @@ -29,6 +34,17 @@ eval { $have_pve_manager = 1; }; +my $feature_ipset_nomatch = 0; +eval { + my (undef, undef, $release) = POSIX::uname(); + if ($release =~ m/^(\d+)\.(\d+)\.\d+-/) { + my ($major, $minor) = ($1, $2); + $feature_ipset_nomatch = 1 if ($major > 3) || + ($major == 3 && $minor >= 7); + } + +}; + use Data::Dumper; my $nodename = PVE::INotify::nodename(); @@ -52,124 +68,134 @@ my $log_level_hash = { # imported/converted from: /usr/share/shorewall/macro.* my $pve_fw_macros = { 'Amanda' => [ + "Amanda Backup", { action => 'PARAM', proto => 'udp', dport => '10080' }, { action => 'PARAM', proto => 'tcp', dport => '10080' }, ], 'Auth' => [ + "Auth (identd) traffic", { action => 'PARAM', proto => 'tcp', dport => '113' }, ], 'BGP' => [ + "Border Gateway Protocol traffic", { action => 'PARAM', proto => 'tcp', dport => '179' }, ], 'BitTorrent' => [ + "BitTorrent traffic for BitTorrent 3.1 and earlier", { action => 'PARAM', proto => 'tcp', dport => '6881:6889' }, { action => 'PARAM', proto => 'udp', dport => '6881' }, ], 'BitTorrent32' => [ + "BitTorrent traffic for BitTorrent 3.2 and later", { action => 'PARAM', proto => 'tcp', dport => '6881:6999' }, { action => 'PARAM', proto => 'udp', dport => '6881' }, ], 'CVS' => [ + "Concurrent Versions System pserver traffic", { action => 'PARAM', proto => 'tcp', dport => '2401' }, ], 'Citrix' => [ + "Citrix/ICA traffic (ICA, ICA Browser, CGP)", { action => 'PARAM', proto => 'tcp', dport => '1494' }, { action => 'PARAM', proto => 'udp', dport => '1604' }, { action => 'PARAM', proto => 'tcp', dport => '2598' }, ], 'DAAP' => [ + "Digital Audio Access Protocol traffic (iTunes, Rythmbox daemons)", { action => 'PARAM', proto => 'tcp', dport => '3689' }, { action => 'PARAM', proto => 'udp', dport => '3689' }, ], 'DCC' => [ + "Distributed Checksum Clearinghouse spam filtering mechanism", { action => 'PARAM', proto => 'tcp', dport => '6277' }, ], 'DHCPfwd' => [ + "Forwarded DHCP traffic (bidirectional)", { 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)", { action => 'PARAM', proto => 'udp', dport => '53' }, { action => 'PARAM', proto => 'tcp', dport => '53' }, ], 'Distcc' => [ + "Distributed Compiler service", { action => 'PARAM', proto => 'tcp', dport => '3632' }, ], - 'Edonkey' => [ - { action => 'PARAM', proto => 'tcp', dport => '4662' }, - { action => 'PARAM', proto => 'udp', dport => '4665' }, - ], 'FTP' => [ + "File Transfer Protocol", { action => 'PARAM', proto => 'tcp', dport => '21' }, ], 'Finger' => [ + "Finger protocol (RFC 742)", { action => 'PARAM', proto => 'tcp', dport => '79' }, ], 'GNUnet' => [ + "GNUnet secure peer-to-peer networking traffic", { 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' => [ + "Generic Routing Encapsulation tunneling protocol (bidirectional)", { action => 'PARAM', proto => '47' }, { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => '47' }, ], 'Git' => [ + "Git distributed revision control traffic", { action => 'PARAM', proto => 'tcp', dport => '9418' }, ], - 'Gnutella' => [ - { action => 'PARAM', proto => 'tcp', dport => '6346' }, - { action => 'PARAM', proto => 'udp', dport => '6346' }, - ], 'HKP' => [ + "OpenPGP HTTP keyserver protocol traffic", { action => 'PARAM', proto => 'tcp', dport => '11371' }, ], 'HTTP' => [ + "Hypertext Transfer Protocol (WWW)", { action => 'PARAM', proto => 'tcp', dport => '80' }, ], 'HTTPS' => [ + "Hypertext Transfer Protocol (WWW) over SSL", { action => 'PARAM', proto => 'tcp', dport => '443' }, ], 'ICPV2' => [ + "Internet Cache Protocol V2 (Squid) traffic", { action => 'PARAM', proto => 'udp', dport => '3130' }, ], 'ICQ' => [ + "AOL Instant Messenger traffic", { action => 'PARAM', proto => 'tcp', dport => '5190' }, ], 'IMAP' => [ + "Internet Message Access Protocol", { action => 'PARAM', proto => 'tcp', dport => '143' }, ], 'IMAPS' => [ + "Internet Message Access Protocol over SSL", { action => 'PARAM', proto => 'tcp', dport => '993' }, ], 'IPIP' => [ + "IPIP capsulation traffic (bidirectional)", { 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' => [ + "IPsec traffic (bidirectional)", { 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)", { 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)", { action => 'PARAM', proto => 'udp', dport => '500' }, { action => 'PARAM', proto => 'udp', dport => '4500' }, { action => 'PARAM', proto => '50' }, @@ -178,123 +204,142 @@ my $pve_fw_macros = { { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => '50' }, ], 'IRC' => [ + "Internet Relay Chat traffic", { 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' => [ + "HP Jetdirect printing", { action => 'PARAM', proto => 'tcp', dport => '9100' }, ], '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", { action => 'PARAM', proto => 'tcp', dport => '389' }, ], 'LDAPS' => [ + "Secure Lightweight Directory Access Protocol traffic", { action => 'PARAM', proto => 'tcp', dport => '636' }, ], 'MSNP' => [ + "Microsoft Notification Protocol", { action => 'PARAM', proto => 'tcp', dport => '1863' }, ], 'MSSQL' => [ + "Microsoft SQL Server", { action => 'PARAM', proto => 'tcp', dport => '1433' }, ], 'Mail' => [ + "Mail traffic (SMTP, SMTPS, Submission)", { action => 'PARAM', proto => 'tcp', dport => '25' }, { action => 'PARAM', proto => 'tcp', dport => '465' }, { action => 'PARAM', proto => 'tcp', dport => '587' }, ], 'Munin' => [ + "Munin networked resource monitoring traffic", { action => 'PARAM', proto => 'tcp', dport => '4949' }, ], 'MySQL' => [ + "MySQL server", { action => 'PARAM', proto => 'tcp', dport => '3306' }, ], 'NNTP' => [ + "NNTP traffic (Usenet).", { action => 'PARAM', proto => 'tcp', dport => '119' }, ], 'NNTPS' => [ + "Encrypted NNTP traffic (Usenet)", { action => 'PARAM', proto => 'tcp', dport => '563' }, ], 'NTP' => [ + "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' }, ], - 'NTPbrd' => [ - { action => 'PARAM', proto => 'udp', dport => '123' }, - { action => 'PARAM', proto => 'udp', dport => '1024:65535', sport => '123' }, - ], 'OSPF' => [ + "OSPF multicast traffic", { action => 'PARAM', proto => '89' }, ], 'OpenVPN' => [ + "OpenVPN traffic", { action => 'PARAM', proto => 'udp', dport => '1194' }, ], 'PCA' => [ + "Symantec PCAnywere (tm)", { action => 'PARAM', proto => 'udp', dport => '5632' }, { action => 'PARAM', proto => 'tcp', dport => '5631' }, ], 'POP3' => [ + "POP3 traffic", { action => 'PARAM', proto => 'tcp', dport => '110' }, ], 'POP3S' => [ + "Encrypted POP3 traffic", { action => 'PARAM', proto => 'tcp', dport => '995' }, ], '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' => [ + "ICMP echo request", { action => 'PARAM', proto => 'icmp', dport => 'echo-request' }, ], 'PostgreSQL' => [ + "PostgreSQL server", { action => 'PARAM', proto => 'tcp', dport => '5432' }, ], 'Printer' => [ + "Line Printer protocol printing", { action => 'PARAM', proto => 'tcp', dport => '515' }, ], 'RDP' => [ + "Microsoft Remote Desktop Protocol traffic", { action => 'PARAM', proto => 'tcp', dport => '3389' }, ], 'RIPbi' => [ + "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", { action => 'PARAM', proto => 'tcp', dport => '953' }, ], 'Razor' => [ + "Razor Antispam System", { action => 'ACCEPT', proto => 'tcp', dport => '2703' }, ], 'Rdate' => [ + "Remote time retrieval (rdate)", { action => 'PARAM', proto => 'tcp', dport => '37' }, ], 'Rsync' => [ + "Rsync server", { action => 'PARAM', proto => 'tcp', dport => '873' }, ], 'SANE' => [ + "SANE network scanning", { action => 'PARAM', proto => 'tcp', dport => '6566' }, ], 'SMB' => [ + "Microsoft SMB traffic", { 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' => [ + "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' }, @@ -305,81 +350,104 @@ my $pve_fw_macros = { { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'tcp', dport => '135,139,445' }, ], 'SMBswat' => [ + "Samba Web Administration Tool", { action => 'PARAM', proto => 'tcp', dport => '901' }, ], 'SMTP' => [ + "Simple Mail Transfer Protocol", { action => 'PARAM', proto => 'tcp', dport => '25' }, ], 'SMTPS' => [ + "Encrypted Simple Mail Transfer Protocol", { action => 'PARAM', proto => 'tcp', dport => '465' }, ], 'SNMP' => [ + "Simple Network Management Protocol", { action => 'PARAM', proto => 'udp', dport => '161:162' }, { action => 'PARAM', proto => 'tcp', dport => '161' }, ], 'SPAMD' => [ + "Spam Assassin SPAMD traffic", { action => 'PARAM', proto => 'tcp', dport => '783' }, ], 'SSH' => [ + "Secure shell traffic", { action => 'PARAM', proto => 'tcp', dport => '22' }, ], 'SVN' => [ + "Subversion server (svnserve)", { action => 'PARAM', proto => 'tcp', dport => '3690' }, ], 'SixXS' => [ + "SixXS IPv6 Deployment and Tunnel Broker", { action => 'PARAM', proto => 'tcp', dport => '3874' }, { action => 'PARAM', proto => 'udp', dport => '3740' }, { action => 'PARAM', proto => '41' }, { action => 'PARAM', proto => 'udp', dport => '5072,8374' }, ], 'Squid' => [ + "Squid web proxy traffic", { action => 'PARAM', proto => 'tcp', dport => '3128' }, ], 'Submission' => [ + "Mail message submission traffic", { action => 'PARAM', proto => 'tcp', dport => '587' }, ], 'Syslog' => [ + "Syslog protocol (RFC 5424) traffic", { action => 'PARAM', proto => 'udp', dport => '514' }, { action => 'PARAM', proto => 'tcp', dport => '514' }, ], 'TFTP' => [ + "Trivial File Transfer Protocol traffic", { action => 'PARAM', proto => 'udp', dport => '69' }, ], 'Telnet' => [ + "Telnet traffic", { action => 'PARAM', proto => 'tcp', dport => '23' }, ], 'Telnets' => [ + "Telnet over SSL", { action => 'PARAM', proto => 'tcp', dport => '992' }, ], 'Time' => [ + "RFC 868 Time protocol", { action => 'PARAM', proto => 'tcp', dport => '37' }, ], 'Trcrt' => [ + "Traceroute (for up to 30 hops) traffic", { action => 'PARAM', proto => 'udp', dport => '33434:33524' }, { action => 'PARAM', proto => 'icmp', dport => 'echo-request' }, ], 'VNC' => [ + "VNC traffic for VNC display's 0 - 9", { action => 'PARAM', proto => 'tcp', dport => '5900:5909' }, ], 'VNCL' => [ + "VNC traffic from Vncservers to Vncviewers in listen mode", { action => 'PARAM', proto => 'tcp', dport => '5500' }, ], 'Web' => [ + "WWW traffic (HTTP and HTTPS)", { action => 'PARAM', proto => 'tcp', dport => '80' }, { action => 'PARAM', proto => 'tcp', dport => '443' }, ], 'Webcache' => [ + "Web Cache/Proxy traffic (port 8080)", { action => 'PARAM', proto => 'tcp', dport => '8080' }, ], 'Webmin' => [ + "Webmin traffic", { action => 'PARAM', proto => 'tcp', dport => '10000' }, ], 'Whois' => [ + "Whois (nicname, RFC 3912) traffic", { action => 'PARAM', proto => 'tcp', dport => '43' }, ], }; my $pve_fw_parsed_macros; +my $pve_fw_macro_descr; my $pve_fw_preferred_macro_names = {}; my $pve_std_chains = { @@ -393,12 +461,12 @@ my $pve_std_chains = { { action => 'DROP', dsttype => 'BROADCAST' }, { action => 'DROP', dsttype => 'MULTICAST' }, { action => 'DROP', dsttype => 'ANYCAST' }, - { action => 'DROP', dest => '224.0.0.0/4' }, + { action => 'DROP', dest => '224.0.0.0/4' }, ], 'PVEFW-reject' => [ # same as shorewall 'reject' { action => 'DROP', dsttype => 'BROADCAST' }, - { action => 'DROP', source => '224.0.0.0/4' }, + { action => 'DROP', source => '224.0.0.0/4' }, { action => 'DROP', proto => 'icmp' }, "-p tcp -j REJECT --reject-with tcp-reset", "-p udp -j REJECT --reject-with icmp-port-unreachable", @@ -406,7 +474,7 @@ my $pve_std_chains = { "-j REJECT --reject-with icmp-host-prohibited", ], 'PVEFW-Drop' => [ - # same as shorewall 'Drop', which is equal to DROP, + # same as shorewall 'Drop', which is equal to DROP, # but REJECT/DROP some packages to reduce logging, # and ACCEPT critical ICMP types { action => 'PVEFW-reject', proto => 'tcp', dport => '43' }, # REJECT 'auth' @@ -429,7 +497,7 @@ my $pve_std_chains = { { action => 'DROP', proto => 'udp', sport => 53 }, ], 'PVEFW-Reject' => [ - # same as shorewall 'Reject', which is equal to Reject, + # same as shorewall 'Reject', which is equal to Reject, # but REJECT/DROP some packages to reduce logging, # and ACCEPT critical ICMP types { action => 'PVEFW-reject', proto => 'tcp', dport => '43' }, # REJECT 'auth' @@ -510,20 +578,25 @@ my $icmp_type_names = { 'address-mask-reply' => 1, }; -sub get_firewall_macros { - - return $pve_fw_parsed_macros if $pve_fw_parsed_macros; +sub init_firewall_macros { $pve_fw_parsed_macros = {}; foreach my $k (keys %$pve_fw_macros) { my $lc_name = lc($k); my $macro = $pve_fw_macros->{$k}; + if (!ref($macro->[0])) { + $pve_fw_macro_descr->{$k} = shift @$macro; + } $pve_fw_preferred_macro_names->{$lc_name} = $k; $pve_fw_parsed_macros->{$k} = $macro; } +} + +init_firewall_macros(); - return $pve_fw_parsed_macros; +sub get_macros { + return wantarray ? ($pve_fw_parsed_macros, $pve_fw_macro_descr): $pve_fw_parsed_macros; } my $etc_services; @@ -599,16 +672,20 @@ sub get_etc_protocols { sub parse_address_list { my ($str) = @_; - my $nbaor = 0; - foreach my $aor (split(/,/, $str)) { - if (!Net::IP->new($aor)) { + return if $str !~ m/^(\+)(\S+)$/; # ipset ref + + my $count = 0; + my $iprange = 0; + foreach my $elem (split(/,/, $str)) { + $count++; + if (!Net::IP->new($elem)) { my $err = Net::IP::Error(); die "invalid IP address: $err\n"; - }else{ - $nbaor++; } + $iprange = 1 if $elem =~ m/-/; } - return $nbaor; + + die "you can use a range in a list\n" if $iprange && $count > 1; } sub parse_port_name_number_or_range { @@ -635,6 +712,38 @@ sub parse_port_name_number_or_range { return ($nbports); } +PVE::JSONSchema::register_format('pve-fw-port-spec', \&pve_fw_verify_port_spec); +sub pve_fw_verify_port_spec { + my ($portstr) = @_; + + parse_port_name_number_or_range($portstr); + + return $portstr; +} + +PVE::JSONSchema::register_format('pve-fw-v4addr-spec', \&pve_fw_verify_v4addr_spec); +sub pve_fw_verify_v4addr_spec { + my ($list) = @_; + + parse_address_list($list); + + return $list; +} + +PVE::JSONSchema::register_format('pve-fw-protocol-spec', \&pve_fw_verify_protocol_spec); +sub pve_fw_verify_protocol_spec { + my ($proto) = @_; + + my $protocols = get_etc_protocols(); + + die "unknown protocol '$proto'\n" if $proto && + !(defined($protocols->{byname}->{$proto}) || + defined($protocols->{byid}->{$proto})); + + return $proto; +} + + # helper function for API my $rule_properties = { @@ -647,6 +756,8 @@ my $rule_properties = { digest => { type => 'string', optional => 1, + maxLength => 27, + minLength => 27, }, type => { type => 'string', @@ -656,21 +767,24 @@ my $rule_properties = { action => { type => 'string', optional => 1, + enum => ['ACCEPT', 'DROP', 'REJECT'], }, macro => { type => 'string', optional => 1, + maxLength => 128, }, + iface => get_standard_option('pve-iface', { optional => 1 }), source => { - type => 'string', + type => 'string', format => 'pve-fw-v4addr-spec', optional => 1, }, dest => { - type => 'string', + type => 'string', format => 'pve-fw-v4addr-spec', optional => 1, }, proto => { - type => 'string', + type => 'string', format => 'pve-fw-protocol-spec', optional => 1, }, enable => { @@ -678,11 +792,11 @@ my $rule_properties = { optional => 1, }, sport => { - type => 'string', + type => 'string', format => 'pve-fw-port-spec', optional => 1, }, dport => { - type => 'string', + type => 'string', format => 'pve-fw-port-spec', optional => 1, }, comment => { @@ -712,12 +826,28 @@ sub add_rule_properties { my ($properties) = @_; foreach my $k (keys %$rule_properties) { - $properties->{$k} = $rule_properties->{$k}; + my $h = $rule_properties->{$k}; + # copy data, so that we can modify later without side effects + foreach my $opt (keys %$h) { $properties->{$k}->{$opt} = $h->{$opt}; } } - + return $properties; } +sub delete_rule_properties { + my ($rule, $delete_str) = @_; + + foreach my $opt (PVE::Tools::split_list($delete_str)) { + raise_param_exc({ 'delete' => "no such property ('$opt')"}) + if !defined($rule_properties->{$opt}); + raise_param_exc({ 'delete' => "unable to delete required property '$opt'"}) + if $opt eq 'type' || $opt eq 'action'; + delete $rule->{$opt}; + } + + return $rule; +} + sub copy_rule_data { my ($rule, $param) = @_; @@ -753,16 +883,16 @@ sub enable_bridge_firewall { my $rule_format = "%-15s %-30s %-30s %-15s %-15s %-15s\n"; -sub iptables { - my ($cmd) = @_; +sub iptables_restore_cmdlist { + my ($cmdlist) = @_; - run_command("/sbin/iptables $cmd", outfunc => sub {}, errfunc => sub {}); + run_command("/sbin/iptables-restore -n", input => $cmdlist); } -sub iptables_restore_cmdlist { +sub ipset_restore_cmdlist { my ($cmdlist) = @_; - run_command("/sbin/iptables-restore -n", input => $cmdlist); + run_command("/usr/sbin/ipset restore", input => $cmdlist); } sub iptables_get_chains { @@ -789,6 +919,8 @@ sub iptables_get_chains { my $table = ''; + my $hooks = {}; + my $parser = sub { my $line = shift; @@ -810,6 +942,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; @@ -818,33 +952,60 @@ sub iptables_get_chains { run_command("/sbin/iptables-save", outfunc => $parser); - return $res; + return wantarray ? ($res, $hooks) : $res; } -sub iptables_chain_exist { - my ($chain) = @_; +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; +} - eval{ - iptables("-n --list $chain"); - }; - return undef if $@; +sub ipset_chain_digest { + my ($rules) = @_; - return 1; + my $digest = Digest::SHA->new('sha1'); + foreach my $rule (sort @$rules) { # note: sorted + $digest->add($rule); + } + return $digest->b64digest; } -sub iptables_rule_exist { - my ($rule) = @_; +sub ipset_get_chains { + + my $res = {}; + my $chains = {}; + + my $parser = sub { + my $line = shift; - eval{ - iptables("-C $rule"); + return if $line =~ m/^#/; + return if $line =~ m/^\s*$/; + if ($line =~ m/^(?:\S+)\s(PVEFW-\S+)\s(?:\S+).*/) { + my $chain = $1; + $line =~ s/\s+$//; # delete trailing white space + push @{$chains->{$chain}}, $line; + } else { + # simply ignore the rest + return; + } }; - return undef if $@; - return 1; + run_command("/usr/sbin/ipset save", outfunc => $parser); + + # compute digest for each chain + foreach my $chain (keys %$chains) { + $res->{$chain} = ipset_chain_digest($chains->{$chain}); + } + + return $res; } sub ruleset_generate_cmdstr { - my ($ruleset, $chain, $rule, $actions, $goto) = @_; + my ($ruleset, $chain, $rule, $actions, $goto, $cluster_conf) = @_; return if defined($rule->{enable}) && !$rule->{enable}; @@ -852,18 +1013,40 @@ sub ruleset_generate_cmdstr { my $nbdport = defined($rule->{dport}) ? parse_port_name_number_or_range($rule->{dport}) : 0; my $nbsport = defined($rule->{sport}) ? parse_port_name_number_or_range($rule->{sport}) : 0; - my $nbsource = $rule->{source} ? parse_address_list( $rule->{source}) : 0; - my $nbdest = $rule->{dest} ? parse_address_list($rule->{dest}) : 0; my @cmd = (); push @cmd, "-i $rule->{iface_in}" if $rule->{iface_in}; push @cmd, "-o $rule->{iface_out}" if $rule->{iface_out}; - push @cmd, "-m iprange --src-range" if $nbsource > 1; - push @cmd, "-s $rule->{source}" if $rule->{source}; - push @cmd, "-m iprange --dst-range" if $nbdest > 1; - push @cmd, "-d $rule->{dest}" if $rule->{dest}; + my $source = $rule->{source}; + my $dest = $rule->{dest}; + + if ($source) { + if ($source =~ m/^(\+)(\S+)$/) { + die "no such ipset $2" if !$cluster_conf->{ipset}->{$2}; + push @cmd, "-m set --match-set PVEFW-$2 src"; + + } elsif ($source =~ m/\-/){ + push @cmd, "-m iprange --src-range $source"; + + } else { + push @cmd, "-s $source"; + } + } + + if ($dest) { + if ($dest =~ m/^(\+)(\S+)$/) { + die "no such ipset $2" if !$cluster_conf->{ipset}->{$2}; + push @cmd, "-m set --match-set PVEFW-$2 dst"; + + } elsif ($dest =~ m/^(\d+)\.(\d+).(\d+).(\d+)\-(\d+)\.(\d+).(\d+).(\d+)$/){ + push @cmd, "-m iprange --dst-range $dest"; + + } else { + push @cmd, "-s $dest"; + } + } if ($rule->{proto}) { push @cmd, "-p $rule->{proto}"; @@ -874,7 +1057,7 @@ sub ruleset_generate_cmdstr { push @cmd, "--match multiport" if $multiport; - die "multiport: option '--sports' cannot be used together with '--dports'\n" + die "multiport: option '--sports' cannot be used together with '--dports'\n" if ($multiport == 2) && ($rule->{dport} ne $rule->{sport}); if ($rule->{dport}) { @@ -910,7 +1093,7 @@ sub ruleset_generate_cmdstr { push @cmd, "-m addrtype --dst-type $rule->{dsttype}" if $rule->{dsttype}; if (my $action = $rule->{action}) { - $action = $actions->{$action} if defined($actions->{$action}); + $action = $actions->{$action} if defined($actions->{$action}); $goto = 1 if !defined($goto) && $action eq 'PVEFW-SET-ACCEPT-MARK'; push @cmd, $goto ? "-g $action" : "-j $action"; } @@ -963,7 +1146,7 @@ my $apply_macro = sub { }; sub ruleset_generate_rule { - my ($ruleset, $chain, $rule, $actions, $goto) = @_; + my ($ruleset, $chain, $rule, $actions, $goto, $cluster_conf) = @_; my $rules; @@ -973,8 +1156,8 @@ sub ruleset_generate_rule { $rules = [ $rule ]; } - foreach my $tmp (@$rules) { - if (my $cmdstr = ruleset_generate_cmdstr($ruleset, $chain, $tmp, $actions, $goto)) { + foreach my $tmp (@$rules) { + if (my $cmdstr = ruleset_generate_cmdstr($ruleset, $chain, $tmp, $actions, $goto, $cluster_conf)) { ruleset_addrule($ruleset, $chain, $cmdstr); } } @@ -1025,12 +1208,12 @@ sub ruleset_insertrule { sub get_log_rule_base { my ($chain, $vmid, $msg, $loglevel) = @_; - + die "internal error - no log level" if !defined($loglevel); $vmid = 0 if !defined($vmid); - # Note: we use special format for prefix to pass further + # Note: we use special format for prefix to pass further # info to log daemon (VMID, LOGVELEL and CHAIN) return "-j NFLOG --nflog-prefix \":$vmid:$loglevel:$chain: $msg\""; @@ -1116,10 +1299,10 @@ sub ruleset_create_vm_chain { if (!(defined($options->{dhcp}) && $options->{dhcp} == 0)) { if ($direction eq 'OUT') { - ruleset_generate_rule($ruleset, $chain, { action => 'PVEFW-SET-ACCEPT-MARK', + ruleset_generate_rule($ruleset, $chain, { action => 'PVEFW-SET-ACCEPT-MARK', proto => 'udp', sport => 68, dport => 67 }); } else { - ruleset_generate_rule($ruleset, $chain, { action => 'ACCEPT', + ruleset_generate_rule($ruleset, $chain, { action => 'ACCEPT', proto => 'udp', sport => 67, dport => 68 }); } } @@ -1129,24 +1312,22 @@ sub ruleset_create_vm_chain { } ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate INVALID -j DROP"); - if($direction eq 'OUT'){ + if ($direction eq 'OUT') { ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate RELATED,ESTABLISHED -g PVEFW-SET-ACCEPT-MARK"); - - }else{ + } else { ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate RELATED,ESTABLISHED -j $accept"); } + if ($direction eq 'OUT') { if (defined($macaddr) && !(defined($options->{macfilter}) && $options->{macfilter} == 0)) { ruleset_addrule($ruleset, $chain, "-m mac ! --mac-source $macaddr -j DROP"); } ruleset_addrule($ruleset, $chain, "-j MARK --set-mark 0"); # clear mark } - - } sub ruleset_generate_vm_rules { - my ($ruleset, $rules, $groups_conf, $chain, $netid, $direction, $options) = @_; + my ($ruleset, $rules, $cluster_conf, $chain, $netid, $direction, $options) = @_; my $lc_direction = lc($direction); @@ -1154,9 +1335,9 @@ 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"; + my $group_chain = "GROUP-$rule->{action}-$direction"; if(!ruleset_chain_exist($ruleset, $group_chain)){ - generate_group_rules($ruleset, $groups_conf, $rule->{action}); + generate_group_rules($ruleset, $cluster_conf, $rule->{action}); } ruleset_addrule($ruleset, $chain, "-j $group_chain"); if ($direction eq 'OUT'){ @@ -1169,11 +1350,11 @@ sub ruleset_generate_vm_rules { } else { next if $rule->{type} ne $lc_direction; if ($direction eq 'OUT') { - ruleset_generate_rule($ruleset, $chain, $rule, - { ACCEPT => "PVEFW-SET-ACCEPT-MARK", REJECT => "PVEFW-reject" }); + ruleset_generate_rule($ruleset, $chain, $rule, + { ACCEPT => "PVEFW-SET-ACCEPT-MARK", REJECT => "PVEFW-reject" }, undef, $cluster_conf); } else { my $accept = generate_nfqueue($options); - ruleset_generate_rule($ruleset, $chain, $rule, { ACCEPT => $accept , REJECT => "PVEFW-reject" }); + ruleset_generate_rule($ruleset, $chain, $rule, { ACCEPT => $accept , REJECT => "PVEFW-reject" }, undef, $cluster_conf); } } } @@ -1220,7 +1401,7 @@ sub ruleset_generate_vm_ipsrules { } sub generate_venet_rules_direction { - my ($ruleset, $groups_conf, $vmfw_conf, $vmid, $ip, $direction) = @_; + my ($ruleset, $cluster_conf, $vmfw_conf, $vmid, $ip, $direction) = @_; parse_address_list($ip); # make sure we have a valid $ip list @@ -1235,7 +1416,7 @@ sub generate_venet_rules_direction { ruleset_create_vm_chain($ruleset, $chain, $options, undef, $direction); - ruleset_generate_vm_rules($ruleset, $rules, $groups_conf, $chain, 'venet', $direction); + ruleset_generate_vm_rules($ruleset, $rules, $cluster_conf, $chain, 'venet', $direction); # implement policy my $policy; @@ -1275,7 +1456,7 @@ sub generate_venet_rules_direction { } sub generate_tap_rules_direction { - my ($ruleset, $groups_conf, $iface, $netid, $macaddr, $vmfw_conf, $vmid, $bridge, $direction) = @_; + my ($ruleset, $cluster_conf, $iface, $netid, $macaddr, $vmfw_conf, $vmid, $bridge, $direction) = @_; my $lc_direction = lc($direction); @@ -1288,7 +1469,7 @@ sub generate_tap_rules_direction { ruleset_create_vm_chain($ruleset, $tapchain, $options, $macaddr, $direction); - ruleset_generate_vm_rules($ruleset, $rules, $groups_conf, $tapchain, $netid, $direction, $options); + ruleset_generate_vm_rules($ruleset, $rules, $cluster_conf, $tapchain, $netid, $direction, $options); ruleset_generate_vm_ipsrules($ruleset, $options, $direction, $iface, $bridge); @@ -1316,7 +1497,7 @@ sub generate_tap_rules_direction { } sub enable_host_firewall { - my ($ruleset, $hostfw_conf, $groups_conf) = @_; + my ($ruleset, $hostfw_conf, $cluster_conf) = @_; # fixme: allow security groups @@ -1349,7 +1530,7 @@ sub enable_host_firewall { foreach my $rule (@$rules) { next if $rule->{type} ne 'in'; - ruleset_generate_rule($ruleset, $chain, $rule, { ACCEPT => $accept_action, REJECT => "PVEFW-reject" }); + ruleset_generate_rule($ruleset, $chain, $rule, { ACCEPT => $accept_action, REJECT => "PVEFW-reject" }, undef, $cluster_conf); } # implement input policy @@ -1374,7 +1555,7 @@ sub enable_host_firewall { foreach my $rule (@$rules) { next if $rule->{type} ne 'out'; - ruleset_generate_rule($ruleset, $chain, $rule, { ACCEPT => $accept_action, REJECT => "PVEFW-reject" }); + ruleset_generate_rule($ruleset, $chain, $rule, { ACCEPT => $accept_action, REJECT => "PVEFW-reject" }, undef, $cluster_conf); } # implement output policy @@ -1386,10 +1567,10 @@ sub enable_host_firewall { } sub generate_group_rules { - my ($ruleset, $groups_conf, $group) = @_; - die "no such security group '$group'\n" if !$groups_conf->{rules}->{$group}; + my ($ruleset, $cluster_conf, $group) = @_; + die "no such security group '$group'\n" if !$cluster_conf->{groups}->{$group}; - my $rules = $groups_conf->{rules}->{$group}; + my $rules = $cluster_conf->{groups}->{$group}; my $chain = "GROUP-${group}-IN"; @@ -1398,7 +1579,7 @@ sub generate_group_rules { foreach my $rule (@$rules) { next if $rule->{type} ne 'in'; - ruleset_generate_rule($ruleset, $chain, $rule, { ACCEPT => "PVEFW-SET-ACCEPT-MARK", REJECT => "PVEFW-reject" }); + ruleset_generate_rule($ruleset, $chain, $rule, { ACCEPT => "PVEFW-SET-ACCEPT-MARK", REJECT => "PVEFW-reject" }, undef, $cluster_conf); } $chain = "GROUP-${group}-OUT"; @@ -1410,8 +1591,8 @@ sub generate_group_rules { next if $rule->{type} ne 'out'; # we use PVEFW-SET-ACCEPT-MARK (Instead of ACCEPT) because we need to # check also other tap rules later - ruleset_generate_rule($ruleset, $chain, $rule, - { ACCEPT => 'PVEFW-SET-ACCEPT-MARK', REJECT => "PVEFW-reject" }); + ruleset_generate_rule($ruleset, $chain, $rule, + { ACCEPT => 'PVEFW-SET-ACCEPT-MARK', REJECT => "PVEFW-reject" }, undef, $cluster_conf); } } @@ -1424,9 +1605,6 @@ for (my $i = 0; $i < $MAX_NETS; $i++) { sub parse_fw_rule { my ($line, $need_iface, $allow_groups) = @_; - my $macros = get_firewall_macros(); - my $protocols = get_etc_protocols(); - my ($type, $action, $iface, $source, $dest, $proto, $dport, $sport); # we can add single line comments to the end of the rule @@ -1469,7 +1647,7 @@ sub parse_fw_rule { 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/^[A-Za-z0-9_\-]+$/; + die "invalid characters in group name\n" if $action !~ m/^[A-Za-z0-9_\-]+$/; } else { die "unknown rule type '$type'\n"; } @@ -1479,9 +1657,7 @@ sub parse_fw_rule { } $proto = undef if $proto && $proto eq '-'; - die "unknown protokol '$proto'\n" if $proto && - !(defined($protocols->{byname}->{$proto}) || - defined($protocols->{byid}->{$proto})); + pve_fw_verify_protocol_spec($proto) if $proto; $source = undef if $source && $source eq '-'; $dest = undef if $dest && $dest eq '-'; @@ -1491,7 +1667,7 @@ sub parse_fw_rule { parse_port_name_number_or_range($dport) if defined($dport); parse_port_name_number_or_range($sport) if defined($sport); - + parse_address_list($source) if $source; parse_address_list($dest) if $dest; @@ -1564,6 +1740,22 @@ sub parse_hostfw_option { return ($opt, $value); } +sub parse_clusterfw_option { + my ($line) = @_; + + my ($opt, $value); + + if ($line =~ m/^(enable):\s*(0|1)\s*$/i) { + $opt = lc($1); + $value = int($2); + } else { + chomp $line; + die "can't parse option '$line'\n" + } + + return ($opt, $value); +} + sub parse_vm_fw_rules { my ($filename, $fh) = @_; @@ -1663,22 +1855,22 @@ sub parse_host_fw_rules { warn "$prefix: $err"; next; } - + push @{$res->{$section}}, $rule; } - $res->{digest} = $digest->b64digest; +$res->{digest} = $digest->b64digest; return $res; } -sub parse_group_fw_rules { +sub parse_cluster_fw_rules { my ($filename, $fh) = @_; my $section; my $group; - my $res = { rules => {} }; + my $res = { rules => [], options => {}, groups => {}, ipset => {} }; my $digest = Digest::SHA->new('sha1'); @@ -1691,28 +1883,83 @@ sub parse_group_fw_rules { my $linenr = $fh->input_line_number(); my $prefix = "$filename (line $linenr)"; + if ($line =~ m/^\[options\]$/i) { + $section = 'options'; + next; + } + if ($line =~ m/^\[group\s+(\S+)\]\s*$/i) { - $section = 'rules'; + $section = 'groups'; $group = lc($1); next; } - if (!$section || !$group) { - warn "$prefix: skip line - no section"; + + if ($line =~ m/^\[rules\]$/i) { + $section = 'rules'; next; } - my $rule; - eval { $rule = parse_fw_rule($line, 0, 0); }; - if (my $err = $@) { - warn "$prefix: $err"; + if ($line =~ m/^\[ipset\s+(\S+)\]\s*$/i) { + $section = 'ipset'; + $group = lc($1); + next; + } + + if (!$section) { + warn "$prefix: skip line - no section\n"; next; } - - push @{$res->{$section}->{$group}}, $rule; + + if ($section eq 'options') { + eval { + my ($opt, $value) = parse_clusterfw_option($line); + $res->{options}->{$opt} = $value; + }; + warn "$prefix: $@" if $@; + } elsif ($section eq 'rules') { + my $rule; + eval { $rule = parse_fw_rule($line, 1, 1); }; + if (my $err = $@) { + warn "$prefix: $err"; + next; + } + push @{$res->{$section}}, $rule; + } elsif ($section eq 'groups') { + my $rule; + eval { $rule = parse_fw_rule($line, 0, 0); }; + if (my $err = $@) { + warn "$prefix: $err"; + next; + } + push @{$res->{$section}->{$group}}, $rule; + } elsif ($section eq 'ipset') { + chomp $line; + $line =~ m/^(\!)?\s*((\d+)\.(\d+)\.(\d+)\.(\d+)(\/(\d+))?)/; + my $nomatch = $1; + my $ip = $2; + + if(!$ip){ + warn "$prefix: $line is not an valid ip address\n"; + next; + } + if (!Net::IP->new($ip)) { + warn "$prefix: $line is not an valid ip address\n"; + next; + } + + if ($nomatch) { + if ($feature_ipset_nomatch) { + push @{$res->{$section}->{$group}}, "$ip nomatch"; + } else { + warn "$prefix: ignore $line - nomatch not supported by kernel\n"; + } + } else { + push @{$res->{$section}->{$group}}, $ip; + } + } } $res->{digest} = $digest->b64digest; - return $res; } @@ -1777,6 +2024,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 = {}; @@ -1809,7 +2116,7 @@ sub get_option_log_level { sub generate_std_chains { my ($ruleset, $options) = @_; - + my $loglevel = get_option_log_level($options, 'smurf_log_level'); # same as shorewall smurflog. @@ -1837,6 +2144,39 @@ sub generate_std_chains { } } +sub generate_ipset_chains { + my ($ipset_ruleset, $fw_conf) = @_; + + foreach my $ipset (keys %{$fw_conf->{ipset}}) { + generate_ipset($ipset_ruleset, "PVEFW-$ipset", $fw_conf->{ipset}->{$ipset}); + } +} + +sub generate_ipset { + my ($ipset_ruleset, $name, $options) = @_; + + 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) { + 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) = @_; @@ -1889,68 +2229,80 @@ sub read_proc_net_route { return $res; } -sub load_security_groups { +sub load_clusterfw_conf { - my $groups_conf = {}; - my $filename = "/etc/pve/firewall/groups.fw"; - if (my $fh = IO::File->new($filename, O_RDONLY)) { - $groups_conf = parse_group_fw_rules($filename, $fh); + my $cluster_conf = {}; + if (my $fh = IO::File->new($clusterfw_conf_filename, O_RDONLY)) { + $cluster_conf = parse_cluster_fw_rules($clusterfw_conf_filename, $fh); } - return $groups_conf; + return $cluster_conf; } -sub save_security_groups { - my ($groups_conf) = @_; +sub save_clusterfw_conf { + my ($cluster_conf) = @_; my $raw = ''; - my $filename = "/etc/pve/firewall/groups.fw"; - foreach my $group (sort keys %{$groups_conf->{rules}}) { - my $rules = $groups_conf->{rules}->{$group}; - $raw .= "[group $group]\n\n"; + my $options = $cluster_conf->{options}; + $raw .= &$format_options($options) if scalar(keys %$options); - 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->{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}'"; - } - } + # fixme: save ipset + my $rules = $cluster_conf->{rules}; + if (scalar(@$rules)) { + $raw .= "[RULES]\n\n"; + $raw .= &$format_rules($rules, 1); $raw .= "\n"; } - PVE::Tools::file_set_contents($filename, $raw); + foreach my $group (sort keys %{$cluster_conf->{groups}}) { + my $rules = $cluster_conf->{groups}->{$group}; + $raw .= "[group $group]\n\n"; + $raw .= &$format_rules($rules, 0); + $raw .= "\n"; + } + + PVE::Tools::file_set_contents($clusterfw_conf_filename, $raw); } 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); my $routing_table = read_proc_net_route(); - my $groups_conf = load_security_groups(); + my $cluster_conf = load_clusterfw_conf(); + + my $ipset_ruleset = {}; + generate_ipset_chains($ipset_ruleset, $cluster_conf); my $ruleset = {}; @@ -1966,9 +2318,7 @@ sub compile { my $hostfw_enable = !(defined($hostfw_options->{enable}) && ($hostfw_options->{enable} == 0)); - enable_host_firewall($ruleset, $hostfw_conf, $groups_conf) if $hostfw_enable; - - my $ips_enable = undef; + enable_host_firewall($ruleset, $hostfw_conf, $cluster_conf) if $hostfw_enable; # generate firewall rules for QEMU VMs foreach my $vmid (keys %{$vmdata->{qemu}}) { @@ -1977,8 +2327,6 @@ sub compile { next if !$vmfw_conf; next if defined($vmfw_conf->{options}->{enable}) && ($vmfw_conf->{options}->{enable} == 0); - $ips_enable = 1 if $vmfw_conf->{options}->{ips}; - foreach my $netid (keys %$conf) { next if $netid !~ m/^net(\d+)$/; my $net = PVE::QemuServer::parse_net($conf->{$netid}); @@ -1993,9 +2341,9 @@ sub compile { generate_bridge_chains($ruleset, $hostfw_conf, $bridge, $routing_table); my $macaddr = $net->{macaddr}; - generate_tap_rules_direction($ruleset, $groups_conf, $iface, $netid, $macaddr, + generate_tap_rules_direction($ruleset, $cluster_conf, $iface, $netid, $macaddr, $vmfw_conf, $vmid, $bridge, 'IN'); - generate_tap_rules_direction($ruleset, $groups_conf, $iface, $netid, $macaddr, + generate_tap_rules_direction($ruleset, $cluster_conf, $iface, $netid, $macaddr, $vmfw_conf, $vmid, $bridge, 'OUT'); } } @@ -2008,12 +2356,10 @@ sub compile { next if !$vmfw_conf; next if defined($vmfw_conf->{options}->{enable}) && ($vmfw_conf->{options}->{enable} == 0); - $ips_enable = 1 if $vmfw_conf->{options}->{ips}; - if ($conf->{ip_address} && $conf->{ip_address}->{value}) { my $ip = $conf->{ip_address}->{value}; - generate_venet_rules_direction($ruleset, $groups_conf, $vmfw_conf, $vmid, $ip, 'IN'); - generate_venet_rules_direction($ruleset, $groups_conf, $vmfw_conf, $vmid, $ip, 'OUT'); + 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}) { @@ -2025,14 +2371,14 @@ sub compile { warn "no bridge device for CT $vmid iface '$netid'\n"; next; # fixme? } - + generate_bridge_chains($ruleset, $hostfw_conf, $bridge, $routing_table); my $macaddr = $d->{mac}; my $iface = $d->{host_ifname}; - generate_tap_rules_direction($ruleset, $groups_conf, $iface, $netid, $macaddr, + generate_tap_rules_direction($ruleset, $cluster_conf, $iface, $netid, $macaddr, $vmfw_conf, $vmid, $bridge, 'IN'); - generate_tap_rules_direction($ruleset, $groups_conf, $iface, $netid, $macaddr, + generate_tap_rules_direction($ruleset, $cluster_conf, $iface, $netid, $macaddr, $vmfw_conf, $vmid, $bridge, 'OUT'); } } @@ -2040,7 +2386,7 @@ sub compile { if($hostfw_options->{optimize}){ - my $accept = $ips_enable ? "PVEFW-IPS" : "ACCEPT"; + my $accept = ruleset_chain_exist($ruleset, "PVEFW-IPS") ? "PVEFW-IPS" : "ACCEPT"; ruleset_insertrule($ruleset, "PVEFW-FORWARD", "-m conntrack --ctstate RELATED,ESTABLISHED -j $accept"); ruleset_insertrule($ruleset, "PVEFW-FORWARD", "-m conntrack --ctstate INVALID -j DROP"); } @@ -2054,29 +2400,24 @@ sub compile { ruleset_addrule($ruleset, "PVEFW-FORWARD", "-i venet0 -j RETURN"); # disable interbridge routing - ruleset_addrule($ruleset, "PVEFW-FORWARD", "-o vmbr+ -j PVEFW-Drop"); + ruleset_addrule($ruleset, "PVEFW-FORWARD", "-o vmbr+ -j PVEFW-Drop"); ruleset_addrule($ruleset, "PVEFW-FORWARD", "-i vmbr+ -j PVEFW-Drop"); - ruleset_addlog($ruleset, "PVEFW-FORWARD", 0, "DROP: ", $loglevel, "-o vmbr+"); - ruleset_addlog($ruleset, "PVEFW-FORWARD", 0, "DROP: ", $loglevel, "-i vmbr+"); - ruleset_addrule($ruleset, "PVEFW-FORWARD", "-o vmbr+ -j DROP"); + ruleset_addlog($ruleset, "PVEFW-FORWARD", 0, "DROP: ", $loglevel, "-o vmbr+"); + ruleset_addlog($ruleset, "PVEFW-FORWARD", 0, "DROP: ", $loglevel, "-i vmbr+"); + ruleset_addrule($ruleset, "PVEFW-FORWARD", "-o vmbr+ -j DROP"); ruleset_addrule($ruleset, "PVEFW-FORWARD", "-i vmbr+ -j DROP"); - return wantarray ? ($ruleset, $hostfw_conf) : $ruleset; + return wantarray ? ($ruleset, $hostfw_conf, $ipset_ruleset) : $ruleset; } sub get_ruleset_status { - my ($ruleset, $verbose) = @_; - - my $active_chains = iptables_get_chains(); + my ($ruleset, $active_chains, $digest_fn, $verbose) = @_; my $statushash = {}; foreach my $chain (sort keys %$ruleset) { - my $digest = Digest::SHA->new('sha1'); - foreach my $cmd (@{$ruleset->{$chain}}) { - $digest->add("$cmd\n"); - } - my $sig = $digest->b64digest; + my $sig = &$digest_fn($ruleset->{$chain}); + $statushash->{$chain}->{sig} = $sig; my $oldsig = $active_chains->{$chain}; @@ -2107,12 +2448,6 @@ sub get_ruleset_status { return $statushash; } -sub print_ruleset { - my ($ruleset) = @_; - - get_ruleset_status($ruleset, 1); -} - sub print_sig_rule { my ($chain, $sig) = @_; @@ -2120,12 +2455,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) { @@ -2136,18 +2472,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) { @@ -2181,26 +2509,93 @@ sub get_rulset_cmdlist { $cmdlist .= "-X $chain\n"; } + my $changes = $cmdlist ne "*filter\n" ? 1 : 0; + $cmdlist .= "COMMIT\n"; - return $cmdlist; + return wantarray ? ($cmdlist, $changes) : $cmdlist; +} + +sub get_ipset_cmdlist { + my ($ruleset, $verbose) = @_; + + my $cmdlist = ""; + + 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}; + die "internal error" if !$stat; + + if ($stat->{action} eq 'create') { + 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'; + + $delete_cmdlist .= "flush $chain\n"; + $delete_cmdlist .= "destroy $chain\n"; + } + + my $changes = ($cmdlist || $delete_cmdlist) ? 1 : 0; + + return ($cmdlist, $delete_cmdlist, $changes); } sub apply_ruleset { - my ($ruleset, $hostfw_conf, $verbose) = @_; + my ($ruleset, $hostfw_conf, $ipset_ruleset, $verbose) = @_; enable_bridge_firewall(); update_nf_conntrack_max($hostfw_conf); - my $cmdlist = get_rulset_cmdlist($ruleset, $verbose); + my ($ipset_create_cmdlist, $ipset_delete_cmdlist, $ipset_changes) = + get_ipset_cmdlist($ipset_ruleset, undef, $verbose); + + my ($cmdlist, $changes) = get_ruleset_cmdlist($ruleset, $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; + } + + if ($changes) { + print "iptables changes:\n"; + print $cmdlist; + } + } - print $cmdlist if $verbose; + 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) { @@ -2238,19 +2633,42 @@ 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) = @_; my $code = sub { my $status = read_pvefw_status(); - my ($ruleset, $hostfw_conf) = PVE::Firewall::compile(); + my ($ruleset, $hostfw_conf, $ipset_ruleset) = compile(); if ($start || $status eq 'active') { save_pvefw_status('active') if ($status ne 'active'); - apply_ruleset($ruleset, $hostfw_conf, $verbose); + apply_ruleset($ruleset, $hostfw_conf, $ipset_ruleset, $verbose); } else { print "Firewall not active (status = $status)\n" if $verbose; }