X-Git-Url: https://git.proxmox.com/?p=pve-firewall.git;a=blobdiff_plain;f=src%2FPVE%2FFirewall.pm;h=6ac303831a62f630d437ef0e0508decd2b72f5ac;hp=958927acdf70bbf906f49b29f4af1fbcb9a62e12;hb=33efd363ab32d3f8f6a9c49e481cb62a8da40b5d;hpb=50f9a28d1aef2ed94f1c6c0e736542c8d082151e diff --git a/src/PVE/Firewall.pm b/src/PVE/Firewall.pm index 958927a..6ac3038 100644 --- a/src/PVE/Firewall.pm +++ b/src/PVE/Firewall.pm @@ -12,31 +12,36 @@ use PVE::JSONSchema qw(register_standard_option get_standard_option); use PVE::Cluster; use PVE::ProcFSTools; use PVE::Tools qw($IPV4RE $IPV6RE); +use PVE::Network; +use PVE::SafeSyslog; use File::Basename; use File::Path; use IO::File; use Net::IP; use PVE::Tools qw(run_command lock_file dir_glob_foreach); use Encode; +use Storable qw(dclone); my $hostfw_conf_filename = "/etc/pve/local/host.fw"; my $pvefw_conf_dir = "/etc/pve/firewall"; my $clusterfw_conf_filename = "$pvefw_conf_dir/cluster.fw"; -# dynamically include PVE::QemuServer and PVE::OpenVZ +# dynamically include PVE::QemuServer and PVE::LXC # to avoid dependency problems my $have_qemu_server; eval { require PVE::QemuServer; + require PVE::QemuConfig; $have_qemu_server = 1; }; -my $have_pve_manager; +my $have_lxc; eval { - require PVE::OpenVZ; - $have_pve_manager = 1; + require PVE::LXC; + $have_lxc = 1; }; + my $pve_fw_status_dir = "/var/lib/pve-firewall"; mkdir $pve_fw_status_dir; # make sure this exists @@ -47,7 +52,15 @@ our $ip_alias_pattern = '[A-Za-z][A-Za-z0-9\-\_]+'; my $max_alias_name_length = 64; my $max_ipset_name_length = 64; -my $max_group_name_length = 20; +my $max_group_name_length = 18; + +my $PROTOCOLS_WITH_PORTS = { + udp => 1, 17 => 1, + udplite => 1, 136 => 1, + tcp => 1, 6 => 1, + dccp => 1, 33 => 1, + sctp => 1, 132 => 1, +}; PVE::JSONSchema::register_format('IPorCIDR', \&pve_verify_ip_or_cidr); sub pve_verify_ip_or_cidr { @@ -113,8 +126,6 @@ eval { }; -use Data::Dumper; - my $nodename = PVE::INotify::nodename(); my $pve_fw_lock_filename = "/var/lock/pvefw.lck"; @@ -132,6 +143,43 @@ my $log_level_hash = { emerg => 0, }; +# %rule +# +# name => optional +# enable => [0|1] +# action => +# proto => +# sport => port[,port[,port]].. or port:port +# dport => port[,port[,port]].. or port:port +# log => optional, loglevel +# logmsg => optional, logmsg - overwrites default +# iface_in => incomin interface +# iface_out => outgoing interface +# match => optional, overwrites generation of match +# target => optional, overwrites action + +# we need to overwrite some macros for ipv6 +my $pve_ipv6fw_macros = { + 'Ping' => [ + { action => 'PARAM', proto => 'icmpv6', dport => 'echo-request' }, + ], + 'NeighborDiscovery' => [ + "IPv6 neighbor solicitation, neighbor and router advertisement", + { action => 'PARAM', proto => 'icmpv6', dport => 'router-solicitation' }, + { action => 'PARAM', proto => 'icmpv6', dport => 'router-advertisement' }, + { action => 'PARAM', proto => 'icmpv6', dport => 'neighbor-solicitation' }, + { action => 'PARAM', proto => 'icmpv6', dport => 'neighbor-advertisement' }, + ], + 'DHCPv6' => [ + "DHCPv6 traffic", + { action => 'PARAM', proto => 'udp', dport => '546:547', sport => '546:547' }, + ], + 'Trcrt' => [ + { action => 'PARAM', proto => 'udp', dport => '33434:33524' }, + { action => 'PARAM', proto => 'icmpv6', dport => 'echo-request' }, + ], + }; + # imported/converted from: /usr/share/shorewall/macro.* my $pve_fw_macros = { 'Amanda' => [ @@ -157,6 +205,11 @@ my $pve_fw_macros = { { action => 'PARAM', proto => 'tcp', dport => '6881:6999' }, { action => 'PARAM', proto => 'udp', dport => '6881' }, ], + 'Ceph' => [ + "Ceph Storage Cluster traffic (Ceph Monitors, OSD & MDS Deamons)", + { action => 'PARAM', proto => 'tcp', dport => '6789' }, + { action => 'PARAM', proto => 'tcp', dport => '6800:7300' }, + ], 'CVS' => [ "Concurrent Versions System pserver traffic", { action => 'PARAM', proto => 'tcp', dport => '2401' }, @@ -294,6 +347,10 @@ my $pve_fw_macros = { { action => 'PARAM', proto => 'tcp', dport => '465' }, { action => 'PARAM', proto => 'tcp', dport => '587' }, ], + 'MDNS' => [ + "Multicast DNS", + { action => 'PARAM', proto => 'udp', dport => '5353' }, + ], 'Munin' => [ "Munin networked resource monitoring traffic", { action => 'PARAM', proto => 'tcp', dport => '4949' }, @@ -486,12 +543,17 @@ my $pve_fw_macros = { my $pve_fw_parsed_macros; my $pve_fw_macro_descr; +my $pve_fw_macro_ipversion = {}; my $pve_fw_preferred_macro_names = {}; +my $FWACCEPTMARK_ON = "0x80000000/0x80000000"; +my $FWACCEPTMARK_OFF = "0x00000000/0x80000000"; + my $pve_std_chains = {}; -$pve_std_chains->{4} = { +my $pve_std_chains_conf = {}; +$pve_std_chains_conf->{4} = { 'PVEFW-SET-ACCEPT-MARK' => [ - "-j MARK --set-mark 1", + { target => "-j MARK --set-mark $FWACCEPTMARK_ON" }, ], 'PVEFW-DropBroadcast' => [ # same as shorewall 'Broadcast' @@ -507,10 +569,10 @@ $pve_std_chains->{4} = { { action => 'DROP', dsttype => 'BROADCAST' }, { 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", - "-p icmp -j REJECT --reject-with icmp-host-unreachable", - "-j REJECT --reject-with icmp-host-prohibited", + { match => '-p tcp', target => '-j REJECT --reject-with tcp-reset' }, + { match => '-p udp', target => '-j REJECT --reject-with icmp-port-unreachable' }, + { match => '-p icmp', target => '-j REJECT --reject-with icmp-host-unreachable' }, + { target => '-j REJECT --reject-with icmp-host-prohibited' }, ], 'PVEFW-Drop' => [ # same as shorewall 'Drop', which is equal to DROP, @@ -523,15 +585,15 @@ $pve_std_chains->{4} = { { action => 'ACCEPT', proto => 'icmp', dport => 'fragmentation-needed' }, { action => 'ACCEPT', proto => 'icmp', dport => 'time-exceeded' }, # Drop packets with INVALID state - "-m conntrack --ctstate INVALID -j DROP", + { action => 'DROP', match => '-m conntrack --ctstate INVALID', }, # Drop Microsoft SMB noise - { action => 'DROP', proto => 'udp', dport => '135,445', nbdport => 2 }, - { action => 'DROP', proto => 'udp', dport => '137:139'}, + { action => 'DROP', proto => 'udp', dport => '135,445' }, + { action => 'DROP', proto => 'udp', dport => '137:139' }, { action => 'DROP', proto => 'udp', dport => '1024:65535', sport => 137 }, - { action => 'DROP', proto => 'tcp', dport => '135,139,445', nbdport => 3 }, + { action => 'DROP', proto => 'tcp', dport => '135,139,445' }, { action => 'DROP', proto => 'udp', dport => 1900 }, # UPnP # Drop new/NotSyn traffic so that it doesn't get logged - "-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -j DROP", + { action => 'DROP', match => '-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN' }, # Drop DNS replies { action => 'DROP', proto => 'udp', sport => 53 }, ], @@ -546,119 +608,126 @@ $pve_std_chains->{4} = { { action => 'ACCEPT', proto => 'icmp', dport => 'fragmentation-needed' }, { action => 'ACCEPT', proto => 'icmp', dport => 'time-exceeded' }, # Drop packets with INVALID state - "-m conntrack --ctstate INVALID -j DROP", + { action => 'DROP', match => '-m conntrack --ctstate INVALID', }, # Drop Microsoft SMB noise - { action => 'PVEFW-reject', proto => 'udp', dport => '135,445', nbdport => 2 }, + { action => 'PVEFW-reject', proto => 'udp', dport => '135,445' }, { action => 'PVEFW-reject', proto => 'udp', dport => '137:139'}, { action => 'PVEFW-reject', proto => 'udp', dport => '1024:65535', sport => 137 }, - { action => 'PVEFW-reject', proto => 'tcp', dport => '135,139,445', nbdport => 3 }, + { action => 'PVEFW-reject', proto => 'tcp', dport => '135,139,445' }, { action => 'DROP', proto => 'udp', dport => 1900 }, # UPnP # Drop new/NotSyn traffic so that it doesn't get logged - "-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -j DROP", + { action => 'DROP', match => '-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN' }, # Drop DNS replies { action => 'DROP', proto => 'udp', sport => 53 }, ], 'PVEFW-tcpflags' => [ # same as shorewall tcpflags action. # Packets arriving on this interface are checked for som illegal combinations of TCP flags - "-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG FIN,PSH,URG -g PVEFW-logflags", - "-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG NONE -g PVEFW-logflags", - "-p tcp -m tcp --tcp-flags SYN,RST SYN,RST -g PVEFW-logflags", - "-p tcp -m tcp --tcp-flags FIN,SYN FIN,SYN -g PVEFW-logflags", - "-p tcp -m tcp --sport 0 --tcp-flags FIN,SYN,RST,ACK SYN -g PVEFW-logflags", + { match => '-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG FIN,PSH,URG', target => '-g PVEFW-logflags' }, + { match => '-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG NONE', target => '-g PVEFW-logflags' }, + { match => '-p tcp -m tcp --tcp-flags SYN,RST SYN,RST', target => '-g PVEFW-logflags' }, + { match => '-p tcp -m tcp --tcp-flags FIN,SYN FIN,SYN', target => '-g PVEFW-logflags' }, + { match => '-p tcp -m tcp --sport 0 --tcp-flags FIN,SYN,RST,ACK SYN', target => '-g PVEFW-logflags' }, ], 'PVEFW-smurfs' => [ # same as shorewall smurfs action # Filter packets for smurfs (packets with a broadcast address as the source). - "-s 0.0.0.0/32 -j RETURN", - "-m addrtype --src-type BROADCAST -g PVEFW-smurflog", - "-s 224.0.0.0/4 -g PVEFW-smurflog", + { match => '-s 0.0.0.0/32', target => '-j RETURN' }, # allow DHCP + { match => '-m addrtype --src-type BROADCAST', target => '-g PVEFW-smurflog' }, + { match => '-s 224.0.0.0/4', target => '-g PVEFW-smurflog' }, + ], + 'PVEFW-smurflog' => [ + { action => 'DROP', logmsg => 'DROP: ' }, + ], + 'PVEFW-logflags' => [ + { action => 'DROP', logmsg => 'DROP: ' }, ], }; -$pve_std_chains->{6} = { +$pve_std_chains_conf->{6} = { 'PVEFW-SET-ACCEPT-MARK' => [ - "-j MARK --set-mark 1", + { target => "-j MARK --set-mark $FWACCEPTMARK_ON" }, ], 'PVEFW-DropBroadcast' => [ - # same as shorewall 'Broadcast' - # simply DROP BROADCAST/MULTICAST/ANYCAST - # we can use this to reduce logging - #{ action => 'DROP', dsttype => 'BROADCAST' }, #no broadcast in ipv6 + # same as shorewall 'Broadcast' + # simply DROP BROADCAST/MULTICAST/ANYCAST + # we can use this to reduce logging + #{ action => 'DROP', dsttype => 'BROADCAST' }, #no broadcast in ipv6 # ipv6 addrtype does not work with kernel 2.6.32 #{ action => 'DROP', dsttype => 'MULTICAST' }, - #{ action => 'DROP', dsttype => 'ANYCAST' }, - { action => 'DROP', dest => 'ff00::/8' }, - #{ action => 'DROP', dest => '224.0.0.0/4' }, + #{ action => 'DROP', dsttype => 'ANYCAST' }, + { action => 'DROP', dest => 'ff00::/8' }, + #{ 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' }, + # same as shorewall 'reject' + #{ action => 'DROP', dsttype => 'BROADCAST' }, + #{ action => 'DROP', source => '224.0.0.0/4' }, { action => 'DROP', proto => 'icmpv6' }, - "-p tcp -j REJECT --reject-with tcp-reset", - #"-p udp -j REJECT --reject-with icmp-port-unreachable", - #"-p icmp -j REJECT --reject-with icmp-host-unreachable", - #"-j REJECT --reject-with icmp-host-prohibited", + { match => '-p tcp', target => '-j REJECT --reject-with tcp-reset' }, + #"-p udp -j REJECT --reject-with icmp-port-unreachable", + #"-p icmp -j REJECT --reject-with icmp-host-unreachable", + #"-j REJECT --reject-with icmp-host-prohibited", ], 'PVEFW-Drop' => [ - # same as shorewall 'Drop', which is equal to DROP, - # but REJECT/DROP some packages to reduce logging, - # and ACCEPT critical ICMP types + # 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' - # we are not interested in BROADCAST/MULTICAST/ANYCAST - { action => 'PVEFW-DropBroadcast' }, - # ACCEPT critical ICMP types - { action => 'ACCEPT', proto => 'icmpv6', dport => 'destination-unreachable' }, - { action => 'ACCEPT', proto => 'icmpv6', dport => 'time-exceeded' }, - { action => 'ACCEPT', proto => 'icmpv6', dport => 'packet-too-big' }, - - # Drop packets with INVALID state - "-m conntrack --ctstate INVALID -j DROP", - # Drop Microsoft SMB noise - { action => 'DROP', proto => 'udp', dport => '135,445', nbdport => 2 }, + # we are not interested in BROADCAST/MULTICAST/ANYCAST + { action => 'PVEFW-DropBroadcast' }, + # ACCEPT critical ICMP types + { action => 'ACCEPT', proto => 'icmpv6', dport => 'destination-unreachable' }, + { action => 'ACCEPT', proto => 'icmpv6', dport => 'time-exceeded' }, + { action => 'ACCEPT', proto => 'icmpv6', dport => 'packet-too-big' }, + # Drop packets with INVALID state + { action => 'DROP', match => '-m conntrack --ctstate INVALID', }, + # Drop Microsoft SMB noise + { action => 'DROP', proto => 'udp', dport => '135,445' }, { action => 'DROP', proto => 'udp', dport => '137:139'}, { action => 'DROP', proto => 'udp', dport => '1024:65535', sport => 137 }, - { action => 'DROP', proto => 'tcp', dport => '135,139,445', nbdport => 3 }, + { action => 'DROP', proto => 'tcp', dport => '135,139,445' }, { action => 'DROP', proto => 'udp', dport => 1900 }, # UPnP - # Drop new/NotSyn traffic so that it doesn't get logged - "-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -j DROP", - # Drop DNS replies + # Drop new/NotSyn traffic so that it doesn't get logged + { action => 'DROP', match => '-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN' }, + # Drop DNS replies { action => 'DROP', proto => 'udp', sport => 53 }, ], 'PVEFW-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' - # we are not interested in BROADCAST/MULTICAST/ANYCAST - { action => 'PVEFW-DropBroadcast' }, - # ACCEPT critical ICMP types - { action => 'ACCEPT', proto => 'icmpv6', dport => 'destination-unreachable' }, - { action => 'ACCEPT', proto => 'icmpv6', dport => 'time-exceeded' }, - { action => 'ACCEPT', proto => 'icmpv6', dport => 'packet-too-big' }, - - # Drop packets with INVALID state - "-m conntrack --ctstate INVALID -j DROP", - # Drop Microsoft SMB noise - { action => 'PVEFW-reject', proto => 'udp', dport => '135,445', nbdport => 2 }, - { action => 'PVEFW-reject', proto => 'udp', dport => '137:139'}, - { action => 'PVEFW-reject', proto => 'udp', dport => '1024:65535', sport => 137 }, - { action => 'PVEFW-reject', proto => 'tcp', dport => '135,139,445', nbdport => 3 }, - { action => 'DROP', proto => 'udp', dport => 1900 }, # UPnP - # Drop new/NotSyn traffic so that it doesn't get logged - "-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -j DROP", - # Drop DNS replies - { action => 'DROP', proto => 'udp', sport => 53 }, + # 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' + # we are not interested in BROADCAST/MULTICAST/ANYCAST + { action => 'PVEFW-DropBroadcast' }, + # ACCEPT critical ICMP types + { action => 'ACCEPT', proto => 'icmpv6', dport => 'destination-unreachable' }, + { action => 'ACCEPT', proto => 'icmpv6', dport => 'time-exceeded' }, + { action => 'ACCEPT', proto => 'icmpv6', dport => 'packet-too-big' }, + # Drop packets with INVALID state + { action => 'DROP', match => '-m conntrack --ctstate INVALID', }, + # Drop Microsoft SMB noise + { action => 'PVEFW-reject', proto => 'udp', dport => '135,445' }, + { action => 'PVEFW-reject', proto => 'udp', dport => '137:139' }, + { action => 'PVEFW-reject', proto => 'udp', dport => '1024:65535', sport => 137 }, + { action => 'PVEFW-reject', proto => 'tcp', dport => '135,139,445' }, + { action => 'DROP', proto => 'udp', dport => 1900 }, # UPnP + # Drop new/NotSyn traffic so that it doesn't get logged + { action => 'DROP', match => '-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN' }, + # Drop DNS replies + { action => 'DROP', proto => 'udp', sport => 53 }, ], 'PVEFW-tcpflags' => [ - # same as shorewall tcpflags action. - # Packets arriving on this interface are checked for som illegal combinations of TCP flags - "-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG FIN,PSH,URG -g PVEFW-logflags", - "-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG NONE -g PVEFW-logflags", - "-p tcp -m tcp --tcp-flags SYN,RST SYN,RST -g PVEFW-logflags", - "-p tcp -m tcp --tcp-flags FIN,SYN FIN,SYN -g PVEFW-logflags", - "-p tcp -m tcp --sport 0 --tcp-flags FIN,SYN,RST,ACK SYN -g PVEFW-logflags", + # same as shorewall tcpflags action. + # Packets arriving on this interface are checked for som illegal combinations of TCP flags + { match => '-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG FIN,PSH,URG', target => '-g PVEFW-logflags' }, + { match => '-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG NONE', target => '-g PVEFW-logflags' }, + { match => '-p tcp -m tcp --tcp-flags SYN,RST SYN,RST', target => '-g PVEFW-logflags' }, + { match => '-p tcp -m tcp --tcp-flags FIN,SYN FIN,SYN', target => '-g PVEFW-logflags' }, + { match => '-p tcp -m tcp --sport 0 --tcp-flags FIN,SYN,RST,ACK SYN', target => '-g PVEFW-logflags' }, + ], + 'PVEFW-logflags' => [ + { action => 'DROP', logmsg => 'DROP: ' }, ], }; @@ -724,7 +793,9 @@ my $icmpv6_type_names = { 'echo-reply' => 1, 'router-solicitation' => 1, 'router-advertisement' => 1, + 'neighbor-solicitation' => 1, 'neighbour-solicitation' => 1, + 'neighbor-advertisement' => 1, 'neighbour-advertisement' => 1, 'redirect' => 1, }; @@ -733,14 +804,32 @@ sub init_firewall_macros { $pve_fw_parsed_macros = {}; - foreach my $k (keys %$pve_fw_macros) { + my $parse = sub { + my ($k, $macro) = @_; my $lc_name = lc($k); - my $macro = $pve_fw_macros->{$k}; - if (!ref($macro->[0])) { - $pve_fw_macro_descr->{$k} = shift @$macro; + $pve_fw_macro_ipversion->{$k} = 0; + while (!ref($macro->[0])) { + my $desc = shift @$macro; + if ($desc eq 'ipv4only') { + $pve_fw_macro_ipversion->{$k} = 4; + } elsif ($desc eq 'ipv6only') { + $pve_fw_macro_ipversion->{$k} = 6; + } else { + $pve_fw_macro_descr->{$k} = $desc; + } } $pve_fw_preferred_macro_names->{$lc_name} = $k; $pve_fw_parsed_macros->{$k} = $macro; + }; + + foreach my $k (keys %$pve_fw_macros) { + &$parse($k, $pve_fw_macros->{$k}); + } + + foreach my $k (keys %$pve_ipv6fw_macros) { + next if $pve_fw_parsed_macros->{$k}; + &$parse($k, $pve_ipv6fw_macros->{$k}); + $pve_fw_macro_ipversion->{$k} = 6; } } @@ -771,7 +860,7 @@ sub get_etc_services { next if $line =~m/^#/; next if ($line =~m/^\s*$/); - if ($line =~ m!^(\S+)\s+(\S+)/(tcp|udp).*$!) { + if ($line =~ m!^(\S+)\s+(\S+)/(tcp|udp|sctp).*$!) { $services->{byid}->{$2}->{name} = $1; $services->{byid}->{$2}->{port} = $2; $services->{byid}->{$2}->{$3} = 1; @@ -787,12 +876,8 @@ sub get_etc_services { return $etc_services; } -my $etc_protocols; - -sub get_etc_protocols { - return $etc_protocols if $etc_protocols; - - my $filename = "/etc/protocols"; +sub parse_protocol_file { + my ($filename) = @_; my $fh = IO::File->new($filename, O_RDONLY); if (!$fh) { @@ -807,7 +892,7 @@ sub get_etc_protocols { next if $line =~m/^#/; next if ($line =~m/^\s*$/); - if ($line =~ m!^(\S+)\s+(\d+)\s+.*$!) { + if ($line =~ m!^(\S+)\s+(\d+)(?:\s+.*)?$!) { $protocols->{byid}->{$2}->{name} = $1; $protocols->{byname}->{$1} = $protocols->{byid}->{$2}; } @@ -815,6 +900,16 @@ sub get_etc_protocols { close($fh); + return $protocols; +} + +my $etc_protocols; + +sub get_etc_protocols { + return $etc_protocols if $etc_protocols; + + my $protocols = parse_protocol_file('/etc/protocols'); + # add special case for ICMP v6 $protocols->{byid}->{icmpv6}->{name} = "icmpv6"; $protocols->{byname}->{icmpv6} = $protocols->{byid}->{icmpv6}; @@ -824,23 +919,13 @@ 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 $etc_ethertypes; + +sub get_etc_ethertypes { + $etc_ethertypes = parse_protocol_file('/etc/ethertypes') + if !$etc_ethertypes; + return $etc_ethertypes; +} my $__local_network; @@ -858,14 +943,24 @@ sub local_network { my $testip = Net::IP->new($ip); - my $routes = PVE::ProcFSTools::read_proc_net_route(); + my $isv6 = $testip->version == 6; + my $routes = $isv6 ? PVE::ProcFSTools::read_proc_net_ipv6_route() + : 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 $mask; + if ($isv6) { + $mask = $entry->{prefix}; + next if !$mask; # skip the default route... + } else { + $mask = $PVE::Network::ipv4_mask_hash_localnet->{$entry->{mask}}; + next if !defined($mask); + } my $cidr = "$entry->{dest}/$mask"; my $testnet = Net::IP->new($cidr); - if ($testnet->overlaps($testip)) { + my $overlap = $testnet->overlaps($testip); + if ($overlap == $Net::IP::IP_B_IN_A_OVERLAP || + $overlap == $Net::IP::IP_IDENTICAL) + { $__local_network = $cidr; return; } @@ -920,7 +1015,9 @@ sub parse_address_list { my $iprange = 0; my $ipversion; - foreach my $elem (split(/,/, $str)) { + my @elements = split(/,/, $str); + die "extraneous commas in list\n" if $str ne join(',', @elements); + foreach my $elem (@elements) { $count++; my $ip = Net::IP->new($elem); if (!$ip) { @@ -943,25 +1040,29 @@ sub parse_address_list { } sub parse_port_name_number_or_range { - my ($str) = @_; + my ($str, $dport) = @_; my $services = PVE::Firewall::get_etc_services(); my $count = 0; my $icmp_port = 0; - foreach my $item (split(/,/, $str)) { - $count++; - if ($item =~ m/^(\d+):(\d+)$/) { + my @elements = split(/,/, $str); + die "extraneous commas in list\n" if $str ne join(',', @elements); + foreach my $item (@elements) { + if ($item =~ m/^([0-9]+):([0-9]+)$/) { + $count += 2; my ($port1, $port2) = ($1, $2); die "invalid port '$port1'\n" if $port1 > 65535; die "invalid port '$port2'\n" if $port2 > 65535; - } elsif ($item =~ m/^(\d+)$/) { + die "backwards range '$port1:$port2' not allowed, did you mean '$port2:$port1'?\n" if $port1 > $port2; + } elsif ($item =~ m/^([0-9]+)$/) { + $count += 1; my $port = $1; die "invalid port '$port'\n" if $port > 65535; } else { - if ($icmp_type_names->{$item}) { + if ($dport && $icmp_type_names->{$item}) { $icmp_port = 1; - } elsif ($icmpv6_type_names->{$item}) { + } elsif ($dport && $icmpv6_type_names->{$item}) { $icmp_port = 1; } else { die "invalid port '$item'\n" if !$services->{byname}->{$item}; @@ -969,16 +1070,31 @@ sub parse_port_name_number_or_range { } } - die "ICPM ports not allowed in port range\n" if $icmp_port && $count > 1; + die "ICPM ports not allowed in port range\n" if $icmp_port && $count > 0; + + # I really don't like to use the word number here, but it's the only thing + # that makes sense in a literal way. The range 1:100 counts as 2, not as + # one and not as 100... + die "too many entries in port list (> 15 numbers)\n" + if $count > 15; + + return (scalar(@elements) > 1); +} + +PVE::JSONSchema::register_format('pve-fw-sport-spec', \&pve_fw_verify_sport_spec); +sub pve_fw_verify_sport_spec { + my ($portstr) = @_; + + parse_port_name_number_or_range($portstr, 0); - return $count; + return $portstr; } -PVE::JSONSchema::register_format('pve-fw-port-spec', \&pve_fw_verify_port_spec); -sub pve_fw_verify_port_spec { +PVE::JSONSchema::register_format('pve-fw-dport-spec', \&pve_fw_verify_dport_spec); +sub pve_fw_verify_dport_spec { my ($portstr) = @_; - parse_port_name_number_or_range($portstr); + parse_port_name_number_or_range($portstr, 1); return $portstr; } @@ -1041,7 +1157,10 @@ sub copy_list_with_digest { next if !defined($v); $data->{$k} = $v; # Note: digest ignores refs ($rule->{errors}) - $sha->add($k, ':', $v, "\n") if !ref($v); ; + # since Digest::SHA expects a series of bytes, + # we have to encode the value here to prevent errors when + # using utf8 characters (eg. in comments) + $sha->add($k, ':', encode_utf8($v), "\n") if !ref($v); ; } push @$res, $data; } @@ -1055,6 +1174,142 @@ sub copy_list_with_digest { return wantarray ? ($res, $digest) : $res; } +our $cluster_option_properties = { + enable => { + description => "Enable or disable the firewall cluster wide.", + type => 'integer', + minimum => 0, + optional => 1, + }, + ebtables => { + description => "Enable ebtables rules cluster wide.", + type => 'boolean', + default => 1, + optional => 1, + }, + policy_in => { + description => "Input policy.", + type => 'string', + optional => 1, + enum => ['ACCEPT', 'REJECT', 'DROP'], + }, + policy_out => { + description => "Output policy.", + type => 'string', + optional => 1, + enum => ['ACCEPT', 'REJECT', 'DROP'], + }, +}; + +our $host_option_properties = { + enable => { + description => "Enable host firewall rules.", + type => 'boolean', + optional => 1, + }, + log_level_in => get_standard_option('pve-fw-loglevel', { + description => "Log level for incoming traffic." }), + log_level_out => get_standard_option('pve-fw-loglevel', { + description => "Log level for outgoing traffic." }), + tcp_flags_log_level => get_standard_option('pve-fw-loglevel', { + description => "Log level for illegal tcp flags filter." }), + smurf_log_level => get_standard_option('pve-fw-loglevel', { + description => "Log level for SMURFS filter." }), + nosmurfs => { + description => "Enable SMURFS filter.", + type => 'boolean', + optional => 1, + }, + tcpflags => { + description => "Filter illegal combinations of TCP flags.", + type => 'boolean', + optional => 1, + }, + nf_conntrack_max => { + description => "Maximum number of tracked connections.", + type => 'integer', + optional => 1, + minimum => 32768, + }, + nf_conntrack_tcp_timeout_established => { + description => "Conntrack established timeout.", + type => 'integer', + optional => 1, + minimum => 7875, + }, + ndp => { + description => "Enable NDP.", + type => 'boolean', + optional => 1, + }, + nf_conntrack_allow_invalid => { + description => "Allow invalid packets on connection tracking.", + type => 'boolean', + default => 0, + optional => 1, + }, +}; + +our $vm_option_properties = { + enable => { + description => "Enable/disable firewall rules.", + type => 'boolean', + optional => 1, + }, + macfilter => { + description => "Enable/disable MAC address filter.", + type => 'boolean', + optional => 1, + }, + dhcp => { + description => "Enable DHCP.", + type => 'boolean', + optional => 1, + }, + ndp => { + description => "Enable NDP.", + type => 'boolean', + optional => 1, + }, + radv => { + description => "Allow sending Router Advertisement.", + type => 'boolean', + optional => 1, + }, + ipfilter => { + description => "Enable default IP filters. " . + "This is equivalent to adding an empty ipfilter-net ipset " . + "for every interface. Such ipsets implicitly contain sane default " . + "restrictions such as restricting IPv6 link local addresses to " . + "the one derived from the interface's MAC address. For containers " . + "the configured IP addresses will be implicitly added.", + type => 'boolean', + optional => 1, + }, + policy_in => { + description => "Input policy.", + type => 'string', + optional => 1, + enum => ['ACCEPT', 'REJECT', 'DROP'], + }, + policy_out => { + description => "Output policy.", + type => 'string', + optional => 1, + enum => ['ACCEPT', 'REJECT', 'DROP'], + }, + log_level_in => get_standard_option('pve-fw-loglevel', { + description => "Log level for incoming traffic." }), + log_level_out => get_standard_option('pve-fw-loglevel', { + description => "Log level for outgoing traffic." }), + +}; + + +my $addr_list_descr = "This can refer to a single IP address, an IP set ('+ipsetname') or an IP alias definition. You can also specify an address range like '20.34.101.207-201.3.9.99', or a list of IP addresses and networks (entries are separated by comma). Please do not mix IPv4 and IPv6 addresses inside such lists."; + +my $port_descr = "You can use service names or simple numbers (0-65535), as defined in '/etc/services'. Port ranges can be specified with '\\d+:\\d+', for example '80:85', and you can use comma separated list to match several ports or ranges."; + my $rule_properties = { pos => { description => "Update rule at position .", @@ -1064,6 +1319,7 @@ my $rule_properties = { }, digest => get_standard_option('pve-config-digest'), type => { + description => "Rule type.", type => 'string', optional => 1, enum => ['in', 'out', 'group'], @@ -1077,36 +1333,48 @@ my $rule_properties = { minLength => 2, }, macro => { + description => "Use predefined standard macro.", type => 'string', optional => 1, maxLength => 128, }, - iface => get_standard_option('pve-iface', { optional => 1 }), + iface => get_standard_option('pve-iface', { + description => "Network interface name. You have to use network configuration key names for VMs and containers ('net\\d+'). Host related rules can use arbitrary strings.", + optional => 1 + }), source => { + description => "Restrict packet source address. $addr_list_descr", type => 'string', format => 'pve-fw-addr-spec', optional => 1, }, dest => { + description => "Restrict packet destination address. $addr_list_descr", type => 'string', format => 'pve-fw-addr-spec', optional => 1, }, proto => { + description => "IP protocol. You can use protocol names ('tcp'/'udp') or simple numbers, as defined in '/etc/protocols'.", type => 'string', format => 'pve-fw-protocol-spec', optional => 1, }, enable => { - type => 'boolean', + description => "Flag to enable/disable a rule.", + type => 'integer', + minimum => 0, optional => 1, }, sport => { - type => 'string', format => 'pve-fw-port-spec', + description => "Restrict TCP/UDP source port. $port_descr", + type => 'string', format => 'pve-fw-sport-spec', optional => 1, }, dport => { - type => 'string', format => 'pve-fw-port-spec', + description => "Restrict TCP/UDP destination port. $port_descr", + type => 'string', format => 'pve-fw-dport-spec', optional => 1, }, comment => { + description => "Descriptive comment.", type => 'string', optional => 1, }, @@ -1139,11 +1407,20 @@ sub delete_rule_properties { } my $apply_macro = sub { - my ($macro_name, $param, $verify) = @_; + my ($macro_name, $param, $verify, $ipversion) = @_; my $macro_rules = $pve_fw_parsed_macros->{$macro_name}; die "unknown macro '$macro_name'\n" if !$macro_rules; # should not happen + if ($ipversion && ($ipversion == 6) && $pve_ipv6fw_macros->{$macro_name}) { + $macro_rules = $pve_ipv6fw_macros->{$macro_name}; + } + + # skip macros which are specific to another ipversion + if ($ipversion && (my $required = $pve_fw_macro_ipversion->{$macro_name})) { + return if $ipversion != $required; + } + my $rules = []; foreach my $templ (@$macro_rules) { @@ -1243,8 +1520,7 @@ sub verify_rule { my $alias = lc($value); &$add_error($name, "no such alias '$value'") if !($cluster_conf->{aliases}->{$alias} || ($fw_conf && $fw_conf->{aliases}->{$alias})); - - my $e = $fw_conf->{aliases}->{$alias} if $fw_conf; + my $e = $fw_conf ? $fw_conf->{aliases}->{$alias} : undef; $e = $cluster_conf->{aliases}->{$alias} if !$e && $cluster_conf; &$set_ip_version($e->{ipversion}); @@ -1277,12 +1553,9 @@ sub verify_rule { if !$allow_iface; eval { PVE::JSONSchema::pve_verify_iface($rule->{iface}); }; &$add_error('iface', $@) if $@; - if ($rule_env eq 'vm') { + if ($rule_env eq 'vm' || $rule_env eq 'ct') { &$add_error('iface', "value does not match the regex pattern 'net\\d+'") if $rule->{iface} !~ m/^net(\d+)$/; - } elsif ($rule_env eq 'ct') { - &$add_error('iface', "value does not match the regex pattern '(venet|eth\\d+)'") - if $rule->{iface} !~ m/^(venet|eth(\d+))$/; } } @@ -1302,17 +1575,24 @@ sub verify_rule { } if ($rule->{dport}) { - eval { parse_port_name_number_or_range($rule->{dport}); }; + eval { parse_port_name_number_or_range($rule->{dport}, 1); }; &$add_error('dport', $@) if $@; + my $proto = $rule->{proto}; &$add_error('proto', "missing property - 'dport' requires this property") - if !$rule->{proto}; + if !$proto; + &$add_error('dport', "protocol '$proto' does not support ports") + if !$PROTOCOLS_WITH_PORTS->{$proto} && + $proto ne 'icmp' && $proto ne 'icmpv6'; # special cases } if ($rule->{sport}) { - eval { parse_port_name_number_or_range($rule->{sport}); }; + eval { parse_port_name_number_or_range($rule->{sport}, 0); }; &$add_error('sport', $@) if $@; + my $proto = $rule->{proto}; &$add_error('proto', "missing property - 'sport' requires this property") - if !$rule->{proto}; + if !$proto; + &$add_error('sport', "protocol '$proto' does not support ports") + if !$PROTOCOLS_WITH_PORTS->{$proto}; } if ($rule->{source}) { @@ -1333,8 +1613,10 @@ sub verify_rule { &$check_ipset_or_alias_property('dest', $ipversion); } + $rule->{ipversion} = $ipversion if $ipversion; + if ($rule->{macro} && !$error_count) { - eval { &$apply_macro($rule->{macro}, $rule, 1); }; + eval { &$apply_macro($rule->{macro}, $rule, 1, $ipversion); }; if (my $err = $@) { if (ref($err) eq "PVE::Exception" && $err->{errors}) { my $eh = $err->{errors}; @@ -1348,7 +1630,6 @@ sub verify_rule { } $rule->{errors} = $errors if $error_count; - $rule->{ipversion} = $ipversion if $ipversion; return $rule; } @@ -1380,7 +1661,7 @@ sub rules_modify_permissions { return { check => ['perm', '/', [ 'Sys.Modify' ]], }; - } elsif ($rule_env eq 'vm' || $rule_env eq 'ct') { + } elsif ($rule_env eq 'vm' || $rule_env eq 'ct') { return { check => ['perm', '/vms/{vmid}', [ 'VM.Config.Network' ]], } @@ -1400,7 +1681,7 @@ sub rules_audit_permissions { return { check => ['perm', '/', [ 'Sys.Audit' ]], }; - } elsif ($rule_env eq 'vm' || $rule_env eq 'ct') { + } elsif ($rule_env eq 'vm' || $rule_env eq 'ct') { return { check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]], } @@ -1425,8 +1706,6 @@ sub enable_bridge_firewall { $bridge_firewall_enabled = 1; } -my $rule_format = "%-15s %-30s %-30s %-15s %-15s %-15s\n"; - sub iptables_restore_cmdlist { my ($cmdlist) = @_; @@ -1442,7 +1721,13 @@ sub ip6tables_restore_cmdlist { sub ipset_restore_cmdlist { my ($cmdlist) = @_; - run_command("/usr/sbin/ipset restore", input => $cmdlist, errmsg => "ipset_restore_cmdlist"); + run_command("/sbin/ipset restore", input => $cmdlist, errmsg => "ipset_restore_cmdlist"); +} + +sub ebtables_restore_cmdlist { + my ($cmdlist) = @_; + + run_command("/sbin/ebtables-restore", input => $cmdlist, errmsg => "ebtables_restore_cmdlist"); } sub iptables_get_chains { @@ -1458,14 +1743,12 @@ sub iptables_get_chains { return 1 if $name =~ m/^PVEFW-\S+$/; - return 1 if $name =~ m/^tap\d+i\d+-(:?IN|OUT)$/; - - return 1 if $name =~ m/^veth\d+.\d+-(:?IN|OUT)$/; # fixme: dev name is configurable + return 1 if $name =~ m/^tap\d+i\d+-(?:IN|OUT)$/; - return 1 if $name =~ m/^venet0-\d+-(:?IN|OUT)$/; + return 1 if $name =~ m/^veth\d+i\d+-(?:IN|OUT)$/; - return 1 if $name =~ m/^fwbr\d+(v\d+)?-(:?FW|IN|OUT|IPS)$/; - return 1 if $name =~ m/^GROUP-(:?[^\s\-]+)-(:?IN|OUT)$/; + return 1 if $name =~ m/^fwbr\d+(v\d+)?-(?:FW|IN|OUT|IPS)$/; + return 1 if $name =~ m/^GROUP-(?:$security_group_name_pattern)-(?:IN|OUT)$/; return undef; }; @@ -1547,7 +1830,7 @@ sub ipset_get_chains { } }; - run_command("/usr/sbin/ipset save", outfunc => $parser); + run_command("/sbin/ipset save", outfunc => $parser); # compute digest for each chain foreach my $chain (keys %$chains) { @@ -1557,172 +1840,200 @@ sub ipset_get_chains { return $res; } -sub ruleset_generate_cmdstr { - my ($ruleset, $chain, $ipversion, $rule, $actions, $goto, $cluster_conf, $fw_conf) = @_; +sub ebtables_get_chains { - return if defined($rule->{enable}) && !$rule->{enable}; - return if $rule->{errors}; + my $res = {}; + my $chains = {}; + my $parser = sub { + my $line = shift; + return if $line =~ m/^#/; + return if $line =~ m/^\s*$/; + if ($line =~ m/^:(\S+)\s\S+$/) { + # Make sure we know chains exist even if they're empty. + $chains->{$1} //= []; + } elsif ($line =~ m/^(?:\S+)\s(\S+)\s(?:\S+).*/) { + my $chain = $1; + $line =~ s/\s+$//; + push @{$chains->{$chain}}, $line; + } else { + # simply ignore the rest + return; + } + }; - die "unable to emit macro - internal error" if $rule->{macro}; # should not happen + run_command("/sbin/ebtables-save", outfunc => $parser); + # compute digest for each chain and store rules as well + foreach my $chain (keys %$chains) { + $res->{$chain}->{rules} = $chains->{$chain}; + $res->{$chain}->{sig} = iptables_chain_digest($chains->{$chain}); + } + return $res; +} - 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; +# substitude action of rule according to action hash +sub rule_substitude_action { + my ($rule, $actions) = @_; - my @cmd = (); + if (my $action = $rule->{action}) { + $rule->{action} = $actions->{$action} if defined($actions->{$action}); + } +} - push @cmd, "-i $rule->{iface_in}" if $rule->{iface_in}; - push @cmd, "-o $rule->{iface_out}" if $rule->{iface_out}; +# generate a src or dst match +# $dir(ection) is either d or s +sub ipt_gen_src_or_dst_match { + my ($adr, $dir, $ipversion, $cluster_conf, $fw_conf) = @_; - my $source = $rule->{source}; - my $dest = $rule->{dest}; + my $srcdst; + if ($dir eq 's') { + $srcdst = "src"; + } elsif ($dir eq 'd') { + $srcdst = "dst"; + } else { + die "ipt_gen_src_or_dst_match: invalid direction $dir \n"; + } - if ($source) { - if ($source =~ m/^\+/) { - if ($source =~ m/^\+(${ipset_name_pattern})$/) { - my $name = $1; - if ($fw_conf && $fw_conf->{ipset}->{$name}) { - my $ipset_chain = compute_ipset_chain_name($fw_conf->{vmid}, $name, $ipversion); - push @cmd, "-m set --match-set ${ipset_chain} src"; - } elsif ($cluster_conf && $cluster_conf->{ipset}->{$name}) { - my $ipset_chain = compute_ipset_chain_name(0, $name, $ipversion); - push @cmd, "-m set --match-set ${ipset_chain} src"; - } else { - die "no such ipset '$name'\n"; - } + my $match; + if ($adr =~ m/^\+/) { + if ($adr =~ m/^\+(${ipset_name_pattern})$/) { + my $name = $1; + my $ipset_chain; + if ($fw_conf && $fw_conf->{ipset}->{$name}) { + $ipset_chain = compute_ipset_chain_name($fw_conf->{vmid}, $name, $ipversion); + } elsif ($cluster_conf && $cluster_conf->{ipset}->{$name}) { + $ipset_chain = compute_ipset_chain_name(0, $name, $ipversion); } else { - die "invalid security group name '$source'\n"; + die "no such ipset '$name'\n"; } - } elsif ($source =~ m/^${ip_alias_pattern}$/){ - my $alias = lc($source); - my $e = $fw_conf->{aliases}->{$alias} if $fw_conf; - $e = $cluster_conf->{aliases}->{$alias} if !$e && $cluster_conf; - die "no such alias '$source'\n" if !$e; - push @cmd, "-s $e->{cidr}"; - } elsif ($source =~ m/\-/){ - push @cmd, "-m iprange --src-range $source"; + $match = "-m set --match-set ${ipset_chain} ${srcdst}"; } else { - push @cmd, "-s $source"; - } + die "invalid security group name '$adr'\n"; + } + } elsif ($adr =~ m/^${ip_alias_pattern}$/){ + my $alias = lc($adr); + my $e = $fw_conf ? $fw_conf->{aliases}->{$alias} : undef; + $e = $cluster_conf->{aliases}->{$alias} if !$e && $cluster_conf; + die "no such alias '$adr'\n" if !$e; + $match = "-${dir} $e->{cidr}"; + } elsif ($adr =~ m/\-/){ + $match = "-m iprange --${srcdst}-range $adr"; + } else { + $match = "-${dir} $adr"; } - if ($dest) { - if ($dest =~ m/^\+/) { - if ($dest =~ m/^\+(${ipset_name_pattern})$/) { - my $name = $1; - if ($fw_conf && $fw_conf->{ipset}->{$name}) { - my $ipset_chain = compute_ipset_chain_name($fw_conf->{vmid}, $name, $ipversion); - push @cmd, "-m set --match-set ${ipset_chain} dst"; - } elsif ($cluster_conf && $cluster_conf->{ipset}->{$name}) { - my $ipset_chain = compute_ipset_chain_name(0, $name, $ipversion); - push @cmd, "-m set --match-set ${ipset_chain} dst"; + return $match; +} + +# convert a %rule to an array of iptables commands +sub ipt_rule_to_cmds { + my ($rule, $chain, $ipversion, $cluster_conf, $fw_conf, $vmid) = @_; + + die "ipt_rule_to_cmds unable to handle macro" if $rule->{macro}; #should not happen + + my @match = (); + + if (defined $rule->{match}) { + push @match, $rule->{match}; + } else { + push @match, "-i $rule->{iface_in}" if $rule->{iface_in}; + push @match, "-o $rule->{iface_out}" if $rule->{iface_out}; + + if ($rule->{source}) { + push @match, ipt_gen_src_or_dst_match($rule->{source}, 's', $ipversion, $cluster_conf, $fw_conf); + } + if ($rule->{dest}) { + push @match, ipt_gen_src_or_dst_match($rule->{dest}, 'd', $ipversion, $cluster_conf, $fw_conf); + } + + if (my $proto = $rule->{proto}) { + push @match, "-p $proto"; + + my $multidport = defined($rule->{dport}) && parse_port_name_number_or_range($rule->{dport}, 1); + my $multisport = defined($rule->{sport}) && parse_port_name_number_or_range($rule->{sport}, 0); + + my $add_dport = sub { + return if !$rule->{dport}; + + if ($proto eq 'icmp') { + # Note: we use dport to store --icmp-type + die "unknown icmp-type '$rule->{dport}'\n" + if $rule->{dport} !~ /^\d+$/ && !defined($icmp_type_names->{$rule->{dport}}); + push @match, "-m icmp --icmp-type $rule->{dport}"; + } elsif ($proto eq 'icmpv6') { + # Note: we use dport to store --icmpv6-type + die "unknown icmpv6-type '$rule->{dport}'\n" + if $rule->{dport} !~ /^\d+$/ && !defined($icmpv6_type_names->{$rule->{dport}}); + push @match, "-m icmpv6 --icmpv6-type $rule->{dport}"; + } elsif (!$PROTOCOLS_WITH_PORTS->{$proto}) { + die "protocol $proto does not have ports\n"; + } elsif ($multidport) { + push @match, "--match multiport", "--dports $rule->{dport}"; } else { - die "no such ipset '$name'\n"; + push @match, "--dport $rule->{dport}"; } - } else { - die "invalid security group name '$dest'\n"; - } - } elsif ($dest =~ m/^${ip_alias_pattern}$/){ - my $alias = lc($dest); - my $e = $fw_conf->{aliases}->{$alias} if $fw_conf; - $e = $cluster_conf->{aliases}->{$alias} if !$e && $cluster_conf; - die "no such alias '$dest'\n" if !$e; - push @cmd, "-d $e->{cidr}"; - } elsif ($dest =~ m/^(\d+)\.(\d+).(\d+).(\d+)\-(\d+)\.(\d+).(\d+).(\d+)$/){ - push @cmd, "-m iprange --dst-range $dest"; - } else { - push @cmd, "-d $dest"; - } - } + }; - if ($rule->{proto}) { - push @cmd, "-p $rule->{proto}"; - - my $multiport = 0; - $multiport++ if $nbdport > 1; - $multiport++ if $nbsport > 1; - - push @cmd, "--match multiport" if $multiport; - - die "multiport: option '--sports' cannot be used together with '--dports'\n" - if ($multiport == 2) && ($rule->{dport} ne $rule->{sport}); - - if ($rule->{dport}) { - if ($rule->{proto} && $rule->{proto} eq 'icmp') { - # Note: we use dport to store --icmp-type - die "unknown icmp-type '$rule->{dport}'\n" if !defined($icmp_type_names->{$rule->{dport}}); - push @cmd, "-m icmp --icmp-type $rule->{dport}"; - } elsif ($rule->{proto} && $rule->{proto} eq 'icmpv6') { - # Note: we use dport to store --icmpv6-type - die "unknown icmpv6-type '$rule->{dport}'\n" if !defined($icmpv6_type_names->{$rule->{dport}}); - push @cmd, "-m icmpv6 --icmpv6-type $rule->{dport}"; - } else { - if ($nbdport > 1) { - if ($multiport == 2) { - push @cmd, "--ports $rule->{dport}"; - } else { - push @cmd, "--dports $rule->{dport}"; - } + my $add_sport = sub { + return if !$rule->{sport}; + + die "protocol $proto does not have ports\n" + if !$PROTOCOLS_WITH_PORTS->{$proto}; + if ($multisport) { + push @match, "--match multiport", "--sports $rule->{sport}"; } else { - push @cmd, "--dport $rule->{dport}"; + push @match, "--sport $rule->{sport}"; } - } - } + }; - if ($rule->{sport}) { - if ($nbsport > 1) { - push @cmd, "--sports $rule->{sport}" if $multiport != 2; - } else { - push @cmd, "--sport $rule->{sport}"; - } + # order matters - single port before multiport! + $add_dport->() if $multisport; + $add_sport->(); + $add_dport->() if !$multisport; + } elsif ($rule->{dport} || $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}; } - } elsif ($rule->{dport} || $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}; + push @match, "-m addrtype --dst-type $rule->{dsttype}" if $rule->{dsttype}; + } + my $matchstr = scalar(@match) ? join(' ', @match) : ""; - if (my $action = $rule->{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"; + my $targetstr; + if (defined $rule->{target}) { + $targetstr = $rule->{target}; + } else { + my $action = (defined $rule->{action}) ? $rule->{action} : ""; + my $goto = 1 if $action eq 'PVEFW-SET-ACCEPT-MARK'; + $targetstr = ($goto) ? "-g $action" : "-j $action"; } - return scalar(@cmd) ? join(' ', @cmd) : undef; + my @iptcmds; + if (defined $rule->{log} && $rule->{log}) { + my $logaction = get_log_rule_base($chain, $vmid, $rule->{logmsg}, $rule->{log}); + push @iptcmds, "-A $chain $matchstr $logaction"; + } + push @iptcmds, "-A $chain $matchstr $targetstr"; + return @iptcmds; } sub ruleset_generate_rule { - my ($ruleset, $chain, $ipversion, $rule, $actions, $goto, $cluster_conf, $fw_conf) = @_; + my ($ruleset, $chain, $ipversion, $rule, $cluster_conf, $fw_conf) = @_; my $rules; if ($rule->{macro}) { - $rules = &$apply_macro($rule->{macro}, $rule); + $rules = &$apply_macro($rule->{macro}, $rule, 0, $ipversion); } else { $rules = [ $rule ]; } # update all or nothing - - my @cmds = (); - foreach my $tmp (@$rules) { - if (my $cmdstr = ruleset_generate_cmdstr($ruleset, $chain, $ipversion, $tmp, $actions, $goto, $cluster_conf, $fw_conf)) { - push @cmds, $cmdstr; - } - } - - foreach my $cmdstr (@cmds) { - ruleset_addrule($ruleset, $chain, $cmdstr); + my @ipt_rule_cmds; + foreach my $r (@$rules) { + push @ipt_rule_cmds, ipt_rule_to_cmds($r, $chain, $ipversion, $cluster_conf, $fw_conf); } -} - -sub ruleset_generate_rule_insert { - my ($ruleset, $chain, $ipversion, $rule, $actions, $goto) = @_; - - die "implement me" if $rule->{macro}; # not implemented, because not needed so far - - if (my $cmdstr = ruleset_generate_cmdstr($ruleset, $chain, $ipversion, $rule, $actions, $goto)) { - ruleset_insertrule($ruleset, $chain, $cmdstr); + foreach my $c (@ipt_rule_cmds) { + ruleset_add_ipt_cmd($ruleset, $chain, $c); } } @@ -1743,79 +2054,92 @@ sub ruleset_chain_exist { return $ruleset->{$chain} ? 1 : undef; } +# add an iptables command (like generated by ipt_rule_to_cmds) to a chain +sub ruleset_add_ipt_cmd { + my ($ruleset, $chain, $iptcmd) = @_; + + die "no such chain '$chain'\n" if !$ruleset->{$chain}; + + push @{$ruleset->{$chain}}, $iptcmd; +} + sub ruleset_addrule { - my ($ruleset, $chain, $rule) = @_; + my ($ruleset, $chain, $match, $action, $log, $logmsg, $vmid) = @_; die "no such chain '$chain'\n" if !$ruleset->{$chain}; - push @{$ruleset->{$chain}}, "-A $chain $rule"; + if (defined($log) && $log) { + my $logaction = get_log_rule_base($chain, $vmid, $logmsg, $log); + push @{$ruleset->{$chain}}, "-A $chain $match $logaction"; + } + # for stable ebtables digests avoid double-spaces to match ebtables-save output + $match .= ' ' if length($match); + push @{$ruleset->{$chain}}, "-A $chain ${match}$action"; } sub ruleset_insertrule { - my ($ruleset, $chain, $rule) = @_; + my ($ruleset, $chain, $match, $action, $log) = @_; die "no such chain '$chain'\n" if !$ruleset->{$chain}; - unshift @{$ruleset->{$chain}}, "-A $chain $rule"; + unshift @{$ruleset->{$chain}}, "-A $chain $match $action"; } sub get_log_rule_base { my ($chain, $vmid, $msg, $loglevel) = @_; - die "internal error - no log level" if !defined($loglevel); - $vmid = 0 if !defined($vmid); + $msg = "" if !defined($msg); # Note: we use special format for prefix to pass further - # info to log daemon (VMID, LOGVELEL and CHAIN) + # info to log daemon (VMID, LOGLEVEL and CHAIN) return "-j NFLOG --nflog-prefix \":$vmid:$loglevel:$chain: $msg\""; } -sub ruleset_addlog { - my ($ruleset, $chain, $vmid, $msg, $loglevel, $rule) = @_; - - return if !defined($loglevel); - - my $logrule = get_log_rule_base($chain, $vmid, $msg, $loglevel); - - $logrule = "$rule $logrule" if defined($rule); - - ruleset_addrule($ruleset, $chain, $logrule); -} - sub ruleset_add_chain_policy { my ($ruleset, $chain, $ipversion, $vmid, $policy, $loglevel, $accept_action) = @_; if ($policy eq 'ACCEPT') { - ruleset_generate_rule($ruleset, $chain, $ipversion, { action => 'ACCEPT' }, - { ACCEPT => $accept_action}); + my $rule = { action => 'ACCEPT' }; + rule_substitude_action($rule, { ACCEPT => $accept_action}); + ruleset_generate_rule($ruleset, $chain, $ipversion, $rule); } elsif ($policy eq 'DROP') { - ruleset_addrule($ruleset, $chain, "-j PVEFW-Drop"); - - ruleset_addlog($ruleset, $chain, $vmid, "policy $policy: ", $loglevel); + ruleset_addrule($ruleset, $chain, "", "-j PVEFW-Drop"); - ruleset_addrule($ruleset, $chain, "-j DROP"); + ruleset_addrule($ruleset, $chain, "", "-j DROP", $loglevel, "policy $policy: ", $vmid); } elsif ($policy eq 'REJECT') { - ruleset_addrule($ruleset, $chain, "-j PVEFW-Reject"); + ruleset_addrule($ruleset, $chain, "", "-j PVEFW-Reject"); - ruleset_addlog($ruleset, $chain, $vmid, "policy $policy: ", $loglevel); - - ruleset_addrule($ruleset, $chain, "-g PVEFW-reject"); + ruleset_addrule($ruleset, $chain, "", "-g PVEFW-reject", $loglevel, "policy $policy: ", $vmid); } else { # should not happen die "internal error: unknown policy '$policy'"; } } +sub ruleset_chain_add_ndp { + my ($ruleset, $chain, $ipversion, $options, $direction, $accept) = @_; + return if $ipversion != 6 || (defined($options->{ndp}) && !$options->{ndp}); + + ruleset_addrule($ruleset, $chain, "-p icmpv6 --icmpv6-type router-solicitation", $accept); + if ($direction ne 'OUT' || $options->{radv}) { + ruleset_addrule($ruleset, $chain, "-p icmpv6 --icmpv6-type router-advertisement", $accept); + } + ruleset_addrule($ruleset, $chain, "-p icmpv6 --icmpv6-type neighbor-solicitation", $accept); + ruleset_addrule($ruleset, $chain, "-p icmpv6 --icmpv6-type neighbor-advertisement", $accept); +} + sub ruleset_chain_add_conn_filters { - my ($ruleset, $chain, $accept) = @_; + my ($ruleset, $chain, $allow_invalid, $accept) = @_; - ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate INVALID -j DROP"); - ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate RELATED,ESTABLISHED -j $accept"); + if (!$allow_invalid) { + ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate INVALID", "-j DROP"); + } + ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate RELATED,ESTABLISHED", "-j $accept"); } sub ruleset_chain_add_input_filters { @@ -1824,21 +2148,20 @@ sub ruleset_chain_add_input_filters { if ($cluster_conf->{ipset}->{blacklist}){ if (!ruleset_chain_exist($ruleset, "PVEFW-blacklist")) { ruleset_create_chain($ruleset, "PVEFW-blacklist"); - ruleset_addlog($ruleset, "PVEFW-blacklist", 0, "DROP: ", $loglevel) if $loglevel; - ruleset_addrule($ruleset, "PVEFW-blacklist", "-j DROP"); + ruleset_addrule($ruleset, "PVEFW-blacklist", "", "-j DROP", $loglevel, "DROP: "); } my $ipset_chain = compute_ipset_chain_name(0, 'blacklist', $ipversion); - ruleset_addrule($ruleset, $chain, "-m set --match-set ${ipset_chain} src -j PVEFW-blacklist"); + ruleset_addrule($ruleset, $chain, "-m set --match-set ${ipset_chain} src", "-j PVEFW-blacklist"); } if (!(defined($options->{nosmurfs}) && $options->{nosmurfs} == 0)) { if ($ipversion == 4) { - ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate INVALID,NEW -j PVEFW-smurfs"); + ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate INVALID,NEW", "-j PVEFW-smurfs"); } } if ($options->{tcpflags}) { - ruleset_addrule($ruleset, $chain, "-p tcp -j PVEFW-tcpflags"); + ruleset_addrule($ruleset, $chain, "-p tcp", "-j PVEFW-tcpflags"); } } @@ -1849,26 +2172,45 @@ sub ruleset_create_vm_chain { my $accept = generate_nfqueue($options); if (!(defined($options->{dhcp}) && $options->{dhcp} == 0)) { - if ($direction eq 'OUT') { - ruleset_generate_rule($ruleset, $chain, $ipversion, - { action => 'PVEFW-SET-ACCEPT-MARK', - proto => 'udp', sport => 68, dport => 67 }); - } else { - ruleset_generate_rule($ruleset, $chain, $ipversion, - { action => 'ACCEPT', - proto => 'udp', sport => 67, dport => 68 }); + if ($ipversion == 4) { + if ($direction eq 'OUT') { + ruleset_generate_rule($ruleset, $chain, $ipversion, + { action => 'PVEFW-SET-ACCEPT-MARK', + proto => 'udp', sport => 68, dport => 67 }); + } else { + ruleset_generate_rule($ruleset, $chain, $ipversion, + { action => 'ACCEPT', + proto => 'udp', sport => 67, dport => 68 }); + } + } elsif ($ipversion == 6) { + if ($direction eq 'OUT') { + ruleset_generate_rule($ruleset, $chain, $ipversion, + { action => 'PVEFW-SET-ACCEPT-MARK', + proto => 'udp', sport => 546, dport => 547 }); + } else { + ruleset_generate_rule($ruleset, $chain, $ipversion, + { action => 'ACCEPT', + proto => 'udp', sport => 547, dport => 546 }); + } } + } 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, "-m mac ! --mac-source $macaddr", "-j DROP"); + } + if ($ipversion == 6 && !$options->{radv}) { + ruleset_addrule($ruleset, $chain, "-p icmpv6 --icmpv6-type router-advertisement", "-j DROP"); } if ($ipfilter_ipset) { - ruleset_addrule($ruleset, $chain, "-m set ! --match-set $ipfilter_ipset src -j DROP"); + ruleset_addrule($ruleset, $chain, "-m set ! --match-set $ipfilter_ipset src", "-j DROP"); } - ruleset_addrule($ruleset, $chain, "-j MARK --set-mark 0"); # clear mark + ruleset_addrule($ruleset, $chain, "", "-j MARK --set-mark $FWACCEPTMARK_OFF"); # clear mark } + + my $accept_action = $direction eq 'OUT' ? '-g PVEFW-SET-ACCEPT-MARK' : "-j $accept"; + ruleset_chain_add_ndp($ruleset, $chain, $ipversion, $options, $direction, $accept_action); } sub ruleset_add_group_rule { @@ -1881,14 +2223,14 @@ sub ruleset_add_group_rule { } if ($direction eq 'OUT' && $rule->{iface_out}) { - ruleset_addrule($ruleset, $chain, "-o $rule->{iface_out} -j $group_chain"); + 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"); + ruleset_addrule($ruleset, $chain, "-i $rule->{iface_in}", "-j $group_chain"); } else { - ruleset_addrule($ruleset, $chain, "-j $group_chain"); + ruleset_addrule($ruleset, $chain, "", "-j $group_chain"); } - ruleset_addrule($ruleset, $chain, "-m mark --mark 1 -j $action"); + ruleset_addrule($ruleset, $chain, "-m mark --mark $FWACCEPTMARK_ON", "-j $action"); } sub ruleset_generate_vm_rules { @@ -1910,13 +2252,11 @@ sub ruleset_generate_vm_rules { next if $rule->{type} ne $lc_direction; eval { if ($direction eq 'OUT') { - ruleset_generate_rule($ruleset, $chain, $ipversion, $rule, - { ACCEPT => "PVEFW-SET-ACCEPT-MARK", REJECT => "PVEFW-reject" }, - undef, $cluster_conf, $vmfw_conf); + rule_substitude_action($rule, { ACCEPT => "PVEFW-SET-ACCEPT-MARK", REJECT => "PVEFW-reject" }); + ruleset_generate_rule($ruleset, $chain, $ipversion, $rule, $cluster_conf, $vmfw_conf); } else { - ruleset_generate_rule($ruleset, $chain, $ipversion, $rule, - { ACCEPT => $in_accept , REJECT => "PVEFW-reject" }, - undef, $cluster_conf, $vmfw_conf); + rule_substitude_action($rule, { ACCEPT => $in_accept , REJECT => "PVEFW-reject" }); + ruleset_generate_rule($ruleset, $chain, $ipversion, $rule, $cluster_conf, $vmfw_conf); } }; warn $@ if $@; @@ -1953,49 +2293,7 @@ sub ruleset_generate_vm_ipsrules { ruleset_create_chain($ruleset, "PVEFW-IPS"); } - ruleset_addrule($ruleset, "PVEFW-IPS", "-m physdev --physdev-out $iface --physdev-is-bridged -j $nfqueue"); - } -} - -sub generate_venet_rules_direction { - my ($ruleset, $cluster_conf, $vmfw_conf, $vmid, $ip, $direction, $ipversion) = @_; - - my $lc_direction = lc($direction); - - my $rules = $vmfw_conf->{rules}; - - my $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, $ipversion, $options, undef, undef, $direction); - - ruleset_generate_vm_rules($ruleset, $rules, $cluster_conf, $vmfw_conf, $chain, 'venet', $direction, undef, $ipversion); - - # implement policy - my $policy; - - if ($direction eq 'OUT') { - $policy = $options->{policy_out} || 'ACCEPT'; # allow everything by default - } else { - $policy = $options->{policy_in} || 'DROP'; # allow nothing by default - } - - my $accept = generate_nfqueue($options); - my $accept_action = $direction eq 'OUT' ? "PVEFW-SET-ACCEPT-MARK" : $accept; - ruleset_add_chain_policy($ruleset, $chain, $ipversion, $vmid, $policy, $loglevel, $accept_action); - - if ($direction eq 'OUT') { - ruleset_generate_rule_insert($ruleset, "PVEFW-VENET-OUT", $ipversion, { - action => $chain, - source => $ip, - iface_in => 'venet0'}); - } else { - ruleset_generate_rule($ruleset, "PVEFW-VENET-IN", $ipversion, { - action => $chain, - dest => $ip, - iface_out => 'venet0'}); + ruleset_addrule($ruleset, "PVEFW-IPS", "-m physdev --physdev-out $iface --physdev-is-bridged", "-j $nfqueue"); } } @@ -2013,7 +2311,7 @@ sub generate_tap_rules_direction { my $ipfilter_name = compute_ipfilter_ipset_name($netid); my $ipfilter_ipset = compute_ipset_chain_name($vmid, $ipfilter_name, $ipversion) - if $vmfw_conf->{ipset}->{$ipfilter_name}; + if $options->{ipfilter} || $vmfw_conf->{ipset}->{$ipfilter_name}; # create chain with mac and ip filter ruleset_create_vm_chain($ruleset, $tapchain, $ipversion, $options, $macaddr, $ipfilter_ipset, $direction); @@ -2029,7 +2327,7 @@ sub generate_tap_rules_direction { if ($direction eq 'OUT') { $policy = $options->{policy_out} || 'ACCEPT'; # allow everything by default } else { - $policy = $options->{policy_in} || 'DROP'; # allow nothing by default + $policy = $options->{policy_in} || 'DROP'; # allow nothing by default } my $accept = generate_nfqueue($options); @@ -2043,10 +2341,10 @@ sub generate_tap_rules_direction { # plug the tap chain to bridge chain if ($direction eq 'IN') { ruleset_addrule($ruleset, "PVEFW-FWBR-IN", - "-m physdev --physdev-is-bridged --physdev-out $iface -j $tapchain"); + "-m physdev --physdev-is-bridged --physdev-out $iface", "-j $tapchain"); } else { ruleset_addrule($ruleset, "PVEFW-FWBR-OUT", - "-m physdev --physdev-is-bridged --physdev-in $iface -j $tapchain"); + "-m physdev --physdev-is-bridged --physdev-in $iface", "-j $tapchain"); } } @@ -2064,19 +2362,21 @@ sub enable_host_firewall { my $loglevel = get_option_log_level($options, "log_level_in"); - ruleset_addrule($ruleset, $chain, "-i lo -j ACCEPT"); + ruleset_addrule($ruleset, $chain, "-i lo", "-j ACCEPT"); - ruleset_chain_add_conn_filters($ruleset, $chain, 'ACCEPT'); + ruleset_chain_add_conn_filters($ruleset, $chain, 0, 'ACCEPT'); + ruleset_chain_add_ndp($ruleset, $chain, $ipversion, $options, 'IN', '-j RETURN'); ruleset_chain_add_input_filters($ruleset, $chain, $ipversion, $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 + 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->{enable} || $rule->{errors}; + next if $rule->{ipversion} && ($rule->{ipversion} != $ipversion); $rule->{iface_in} = $rule->{iface} if $rule->{iface}; @@ -2084,9 +2384,8 @@ sub enable_host_firewall { if ($rule->{type} eq 'group') { ruleset_add_group_rule($ruleset, $cluster_conf, $chain, $rule, 'IN', $accept_action, $ipversion); } elsif ($rule->{type} eq 'in') { - ruleset_generate_rule($ruleset, $chain, $ipversion, $rule, - { ACCEPT => $accept_action, REJECT => "PVEFW-reject" }, - undef, $cluster_conf, $hostfw_conf); + rule_substitude_action($rule, { ACCEPT => $accept_action, REJECT => "PVEFW-reject" }); + ruleset_generate_rule($ruleset, $chain, $ipversion, $rule, $cluster_conf, $hostfw_conf); } }; warn $@ if $@; @@ -2096,18 +2395,19 @@ sub enable_host_firewall { # allow standard traffic for management ipset (includes cluster network) my $mngmnt_ipset_chain = compute_ipset_chain_name(0, "management", $ipversion); my $mngmntsrc = "-m set --match-set ${mngmnt_ipset_chain} 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 + 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(); + my $localnet = $cluster_conf->{aliases}->{local_network}->{cidr}; + my $localnet_ver = $cluster_conf->{aliases}->{local_network}->{ipversion}; # 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"); + if ($localnet && ($ipversion == $localnet_ver)) { + my $corosync_rule = "-p udp --dport 5404:5405"; + ruleset_addrule($ruleset, $chain, "-s $localnet -d $localnet $corosync_rule", "-j $accept_action"); + ruleset_addrule($ruleset, $chain, "-s $localnet -m addrtype --dst-type MULTICAST $corosync_rule", "-j $accept_action"); } # implement input policy @@ -2120,27 +2420,28 @@ sub enable_host_firewall { $loglevel = get_option_log_level($options, "log_level_out"); - ruleset_addrule($ruleset, $chain, "-o lo -j ACCEPT"); + ruleset_addrule($ruleset, $chain, "-o lo", "-j ACCEPT"); - ruleset_chain_add_conn_filters($ruleset, $chain, 'ACCEPT'); + ruleset_chain_add_conn_filters($ruleset, $chain, 0, 'ACCEPT'); # we use RETURN because we may want to check other thigs later $accept_action = 'RETURN'; + ruleset_chain_add_ndp($ruleset, $chain, $ipversion, $options, 'OUT', "-j $accept_action"); - ruleset_addrule($ruleset, $chain, "-p igmp -j $accept_action"); # important for multicast + 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->{enable} || $rule->{errors}; + next if $rule->{ipversion} && ($rule->{ipversion} != $ipversion); $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, $ipversion); } elsif ($rule->{type} eq 'out') { - ruleset_generate_rule($ruleset, $chain, $ipversion, - $rule, { ACCEPT => $accept_action, REJECT => "PVEFW-reject" }, - undef, $cluster_conf, $hostfw_conf); + rule_substitude_action($rule, { ACCEPT => $accept_action, REJECT => "PVEFW-reject" }); + ruleset_generate_rule($ruleset, $chain, $ipversion, $rule, $cluster_conf, $hostfw_conf); } }; warn $@ if $@; @@ -2148,23 +2449,23 @@ sub enable_host_firewall { } # 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"); + if ($localnet && ($ipversion == $localnet_ver)) { + 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"; + ruleset_addrule($ruleset, $chain, "-d $localnet $corosync_rule", "-j $accept_action"); + ruleset_addrule($ruleset, $chain, "-m addrtype --dst-type MULTICAST $corosync_rule", "-j $accept_action"); } # implement output policy $policy = $cluster_options->{policy_out} || 'ACCEPT'; # allow everything by default ruleset_add_chain_policy($ruleset, $chain, $ipversion, 0, $policy, $loglevel, $accept_action); - ruleset_addrule($ruleset, "PVEFW-OUTPUT", "-j PVEFW-HOST-OUT"); - ruleset_addrule($ruleset, "PVEFW-INPUT", "-j PVEFW-HOST-IN"); + ruleset_addrule($ruleset, "PVEFW-OUTPUT", "", "-j PVEFW-HOST-OUT"); + ruleset_addrule($ruleset, "PVEFW-INPUT", "", "-j PVEFW-HOST-IN"); } sub generate_group_rules { @@ -2180,29 +2481,29 @@ sub generate_group_rules { my $chain = "GROUP-${group}-IN"; ruleset_create_chain($ruleset, $chain); - ruleset_addrule($ruleset, $chain, "-j MARK --set-mark 0"); # clear mark + ruleset_addrule($ruleset, $chain, "", "-j MARK --set-mark $FWACCEPTMARK_OFF"); # clear mark foreach my $rule (@$rules) { next if $rule->{type} ne 'in'; + next if !$rule->{enable} || $rule->{errors}; next if $rule->{ipversion} && $rule->{ipversion} ne $ipversion; - ruleset_generate_rule($ruleset, $chain, $ipversion, $rule, - { ACCEPT => "PVEFW-SET-ACCEPT-MARK", REJECT => "PVEFW-reject" }, - undef, $cluster_conf); + rule_substitude_action($rule, { ACCEPT => "PVEFW-SET-ACCEPT-MARK", REJECT => "PVEFW-reject" }); + ruleset_generate_rule($ruleset, $chain, $ipversion, $rule, $cluster_conf); } $chain = "GROUP-${group}-OUT"; ruleset_create_chain($ruleset, $chain); - ruleset_addrule($ruleset, $chain, "-j MARK --set-mark 0"); # clear mark + ruleset_addrule($ruleset, $chain, "", "-j MARK --set-mark $FWACCEPTMARK_OFF"); # clear mark foreach my $rule (@$rules) { next if $rule->{type} ne 'out'; + next if !$rule->{enable} || $rule->{errors}; next if $rule->{ipversion} && $rule->{ipversion} ne $ipversion; # we use PVEFW-SET-ACCEPT-MARK (Instead of ACCEPT) because we need to # check also other tap rules later - ruleset_generate_rule($ruleset, $chain, $ipversion, $rule, - { ACCEPT => 'PVEFW-SET-ACCEPT-MARK', REJECT => "PVEFW-reject" }, - undef, $cluster_conf); + rule_substitude_action($rule, { ACCEPT => 'PVEFW-SET-ACCEPT-MARK', REJECT => "PVEFW-reject" }); + ruleset_generate_rule($ruleset, $chain, $ipversion, $rule, $cluster_conf); } } @@ -2212,6 +2513,14 @@ for (my $i = 0; $i < $MAX_NETS; $i++) { $valid_netdev_names->{"net$i"} = 1; } +sub get_mark_values { + my ($value, $mask) = @_; + $value = hex($value) if $value =~ /^0x/; + $mask = hex($mask) if defined($mask) && $mask =~ /^0x/; + $mask = 0xffffffff if !defined($mask); + return ($value, $mask); +} + sub parse_fw_rule { my ($prefix, $line, $cluster_conf, $fw_conf, $rule_env, $verbose) = @_; @@ -2278,16 +2587,28 @@ sub parse_fw_rule { die "unable to parse rule parameters: $line\n" if length($line); $rule = verify_rule($rule, $cluster_conf, $fw_conf, $rule_env, 1); - if ($verbose && $rule->{errors}) { - warn "$prefix - errors in rule parameters: $orig_line\n"; + if ($rule->{errors}) { + # The verbose flag really means we're running from the CLI and want + # output on the console - in the other case we really want such errors + # to go into the syslog instead. + my $log = $verbose ? sub { warn @_ } : sub { syslog(err => @_) }; + $log->("$prefix - errors in rule parameters: $orig_line\n"); foreach my $p (keys %{$rule->{errors}}) { - warn " $p: $rule->{errors}->{$p}\n"; + $log->(" $p: $rule->{errors}->{$p}\n"); } } return $rule; } +sub verify_ethertype { + my ($value) = @_; + my $types = get_etc_ethertypes(); + die "unknown ethernet protocol type: $value\n" + if !defined($types->{byname}->{$value}) && + !defined($types->{byid}->{$value}); +} + sub parse_vmfw_option { my ($line) = @_; @@ -2295,7 +2616,7 @@ sub parse_vmfw_option { my $loglevels = "emerg|alert|crit|err|warning|notice|info|debug|nolog"; - if ($line =~ m/^(enable|dhcp|macfilter|ips):\s*(0|1)\s*$/i) { + if ($line =~ m/^(enable|dhcp|ndp|radv|macfilter|ipfilter|ips):\s*(0|1)\s*$/i) { $opt = lc($1); $value = int($2); } elsif ($line =~ m/^(log_level_in|log_level_out):\s*(($loglevels)\s*)?$/i) { @@ -2307,6 +2628,10 @@ sub parse_vmfw_option { } elsif ($line =~ m/^(ips_queues):\s*((\d+)(:(\d+))?)\s*$/i) { $opt = lc($1); $value = $2; + } elsif ($line =~ m/^(layer2_protocols):\s*(((\S+)[,]?)+)\s*$/i) { + $opt = lc($1); + $value = $2; + verify_ethertype($_) foreach split(/\s*,\s*/, $value); } else { die "can't parse option '$line'\n" } @@ -2321,7 +2646,7 @@ sub parse_hostfw_option { my $loglevels = "emerg|alert|crit|err|warning|notice|info|debug|nolog"; - if ($line =~ m/^(enable|nosmurfs|tcpflags):\s*(0|1)\s*$/i) { + if ($line =~ m/^(enable|nosmurfs|tcpflags|ndp|log_nf_conntrack|nf_conntrack_allow_invalid):\s*(0|1)\s*$/i) { $opt = lc($1); $value = int($2); } elsif ($line =~ m/^(log_level_in|log_level_out|tcp_flags_log_level|smurf_log_level):\s*(($loglevels)\s*)?$/i) { @@ -2342,7 +2667,13 @@ sub parse_clusterfw_option { my ($opt, $value); - if ($line =~ m/^(enable):\s*(0|1)\s*$/i) { + if ($line =~ m/^(enable):\s*(\d+)\s*$/i) { + $opt = lc($1); + $value = int($2); + if (($value > 1) && ((time() - $value) > 60)) { + $value = 0 + } + } elsif ($line =~ m/^(ebtables):\s*(0|1)\s*$/i) { $opt = lc($1); $value = int($2); } elsif ($line =~ m/^(policy_(in|out)):\s*(ACCEPT|DROP|REJECT)\s*$/i) { @@ -2359,7 +2690,7 @@ sub resolve_alias { my ($clusterfw_conf, $fw_conf, $cidr) = @_; my $alias = lc($cidr); - my $e = $fw_conf->{aliases}->{$alias} if $fw_conf; + my $e = $fw_conf ? $fw_conf->{aliases}->{$alias} : undef; $e = $clusterfw_conf->{aliases}->{$alias} if !$e && $clusterfw_conf; die "no such alias '$cidr'\n" if !$e;; @@ -2546,6 +2877,10 @@ sub generic_fw_config_parser { $errors->{cidr} = $err; } + if ($cidr =~ m!/0+$!) { + $errors->{cidr} = "a zero prefix is not allowed in ipset entries\n"; + } + my $entry = { cidr => $cidr }; $entry->{nomatch} = 1 if $nomatch; $entry->{comment} = $comment if $comment; @@ -2623,10 +2958,10 @@ sub run_locked { sub read_local_vm_config { - my $openvz = {}; my $qemu = {}; + my $lxc = {}; - my $vmdata = { openvz => $openvz, qemu => $qemu }; + my $vmdata = { qemu => $qemu, lxc => $lxc }; my $vmlist = PVE::Cluster::get_vmlist(); return $vmdata if !$vmlist || !$vmlist->{ids}; @@ -2637,21 +2972,21 @@ sub read_local_vm_config { my $d = $ids->{$vmid}; next if !$d->{node} || $d->{node} ne $nodename; next if !$d->{type}; - if ($d->{type} eq 'openvz') { - if ($have_pve_manager) { - my $cfspath = PVE::OpenVZ::cfs_config_path($vmid); - if (my $conf = PVE::Cluster::cfs_read_file($cfspath)) { - $openvz->{$vmid} = $conf; - } - } - } elsif ($d->{type} eq 'qemu') { + if ($d->{type} eq 'qemu') { if ($have_qemu_server) { - my $cfspath = PVE::QemuServer::cfs_config_path($vmid); + my $cfspath = PVE::QemuConfig->cfs_config_path($vmid); if (my $conf = PVE::Cluster::cfs_read_file($cfspath)) { $qemu->{$vmid} = $conf; } } - } + } elsif ($d->{type} eq 'lxc') { + if ($have_lxc) { + my $cfspath = PVE::LXC::Config->cfs_config_path($vmid); + if (my $conf = PVE::Cluster::cfs_read_file($cfspath)) { + $lxc->{$vmid} = $conf; + } + } + } } return $vmdata; @@ -2796,10 +3131,36 @@ sub save_vmfw_conf { $raw .= "\n"; } - mkdir $pvefw_conf_dir; - my $filename = "$pvefw_conf_dir/$vmid.fw"; - PVE::Tools::file_set_contents($filename, $raw); + if ($raw) { + mkdir $pvefw_conf_dir; + PVE::Tools::file_set_contents($filename, $raw); + } else { + unlink $filename; + } +} + +sub remove_vmfw_conf { + my ($vmid) = @_; + + my $vmfw_conffile = "$pvefw_conf_dir/$vmid.fw"; + + unlink $vmfw_conffile; +} + +sub clone_vmfw_conf { + my ($vmid, $newid) = @_; + + my $sourcevm_conffile = "$pvefw_conf_dir/$vmid.fw"; + my $clonevm_conffile = "$pvefw_conf_dir/$newid.fw"; + + if (-f $clonevm_conffile) { + unlink $clonevm_conffile; + } + if (-f $sourcevm_conffile) { + my $data = PVE::Tools::file_get_contents($sourcevm_conffile); + PVE::Tools::file_set_contents($clonevm_conffile, $data); + } } sub read_vm_firewall_configs { @@ -2812,10 +3173,10 @@ sub read_vm_firewall_configs { next if !$vmfw_conf->{options}; # skip if file does not exists $vmfw_configs->{$vmid} = $vmfw_conf; } - foreach my $vmid (keys %{$vmdata->{openvz}}) { - my $vmfw_conf = load_vmfw_conf($cluster_conf, 'ct', $vmid, $dir, $verbose); - next if !$vmfw_conf->{options}; # skip if file does not exists - $vmfw_configs->{$vmid} = $vmfw_conf; + foreach my $vmid (keys %{$vmdata->{lxc}}) { + my $vmfw_conf = load_vmfw_conf($cluster_conf, 'ct', $vmid, $dir, $verbose); + next if !$vmfw_conf->{options}; # skip if file does not exists + $vmfw_configs->{$vmid} = $vmfw_conf; } return $vmfw_configs; @@ -2844,26 +3205,21 @@ sub generate_std_chains { my $std_chains = $pve_std_chains->{$ipversion} || die "internal error"; my $loglevel = get_option_log_level($options, 'smurf_log_level'); - - my $chain; - - if ($ipversion == 4) { - # same as shorewall smurflog. - $chain = 'PVEFW-smurflog'; - $std_chains->{$chain} = []; - - push @{$std_chains->{$chain}}, get_log_rule_base($chain, 0, "DROP: ", $loglevel) if $loglevel; - push @{$std_chains->{$chain}}, "-j DROP"; + my $chain = 'PVEFW-smurflog'; + if ( $std_chains->{$chain} ) { + foreach my $r (@{$std_chains->{$chain}}) { + $r->{log} = $loglevel; + } } # same as shorewall logflags action. $loglevel = get_option_log_level($options, 'tcp_flags_log_level'); $chain = 'PVEFW-logflags'; - $std_chains->{$chain} = []; - - # fixme: is this correctly logged by pvewf-logger? (ther is no --log-ip-options for NFLOG) - push @{$std_chains->{$chain}}, get_log_rule_base($chain, 0, "DROP: ", $loglevel) if $loglevel; - push @{$std_chains->{$chain}}, "-j DROP"; + if ( $std_chains->{$chain} ) { + foreach my $r (@{$std_chains->{$chain}}) { + $r->{log} = $loglevel; + } + } foreach my $chain (keys %$std_chains) { ruleset_create_chain($ruleset, $chain); @@ -2871,18 +3227,24 @@ sub generate_std_chains { if (ref($rule)) { ruleset_generate_rule($ruleset, $chain, $ipversion, $rule); } else { - ruleset_addrule($ruleset, $chain, $rule); + die "rule $rule as string - should not happen"; } } } } sub generate_ipset_chains { - my ($ipset_ruleset, $clusterfw_conf, $fw_conf) = @_; #fixme + my ($ipset_ruleset, $clusterfw_conf, $fw_conf, $device_ips, $ipsets) = @_; - foreach my $ipset (keys %{$fw_conf->{ipset}}) { + foreach my $ipset (keys %{$ipsets}) { - my $options = $fw_conf->{ipset}->{$ipset}; + my $options = $ipsets->{$ipset}; + + if ($device_ips && $ipset =~ /^ipfilter-(net\d+)$/) { + if (my $ips = $device_ips->{$1}) { + $options = [@$options, @$ips]; + } + } # remove duplicates my $nethash = {}; @@ -2897,8 +3259,10 @@ sub generate_ipset_chains { } #http://backreference.org/2013/03/01/ipv6-address-normalization/ if ($ver == 6) { - my $ipv6 = inet_pton(AF_INET6, lc($cidr)); - $cidr = inet_ntop(AF_INET6, $ipv6); + # ip_compress_address takes an address only, no CIDR + my ($addr, $prefix_len) = ($cidr =~ m@^([^/]*)(/.*)?$@); + $cidr = lc(Net::IP::ip_compress_address($addr, 6)); + $cidr .= $prefix_len if defined($prefix_len); $cidr =~ s|/128$||; } else { $cidr =~ s|/32$||; @@ -2999,8 +3363,12 @@ sub save_clusterfw_conf { } } - mkdir $pvefw_conf_dir; - PVE::Tools::file_set_contents($clusterfw_conf_filename, $raw); + if ($raw) { + mkdir $pvefw_conf_dir; + PVE::Tools::file_set_contents($clusterfw_conf_filename, $raw); + } else { + unlink $clusterfw_conf_filename; + } } sub load_hostfw_conf { @@ -3030,7 +3398,11 @@ sub save_hostfw_conf { $raw .= "\n"; } - PVE::Tools::file_set_contents($hostfw_conf_filename, $raw); + if ($raw) { + PVE::Tools::file_set_contents($hostfw_conf_filename, $raw); + } else { + unlink $hostfw_conf_filename; + } } sub compile { @@ -3038,6 +3410,9 @@ sub compile { my $vmfw_configs; + # fixme: once we read standard chains from config this needs to be put in test/standard cases below + $pve_std_chains = dclone($pve_std_chains_conf); + if ($vmdata) { # test mode my $testdir = $vmdata->{testdir} || die "no test directory specified"; my $filename = "$testdir/cluster.fw"; @@ -3056,29 +3431,31 @@ sub compile { $vmfw_configs = read_vm_firewall_configs($cluster_conf, $vmdata, undef, $verbose); } - my ($ruleset, $ipset_ruleset) = compile_iptables_filter($cluster_conf, $hostfw_conf, $vmfw_configs, $vmdata, 4, $verbose); - my ($rulesetv6) = compile_iptables_filter($cluster_conf, $hostfw_conf, $vmfw_configs, $vmdata, 6, $verbose); - - return ($ruleset, $ipset_ruleset, $rulesetv6); -} - -sub compile_iptables_filter { - my ($cluster_conf, $hostfw_conf, $vmfw_configs, $vmdata, $ipversion, $verbose) = @_; - - $cluster_conf->{ipset}->{venet0} = []; - my $venet0_ipset_chain = compute_ipset_chain_name(0, 'venet0', $ipversion); + return ({},{},{},{}) if !$cluster_conf->{options}->{enable}; 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 }; + my $localnet_ver; + ($localnet, $localnet_ver) = parse_ip_or_cidr(local_network() || '127.0.0.0/8'); + + $cluster_conf->{aliases}->{local_network} = { + name => 'local_network', cidr => $localnet, ipversion => $localnet_ver }; } push @{$cluster_conf->{ipset}->{management}}, { cidr => $localnet }; - return ({}, {}) if !$cluster_conf->{options}->{enable}; + my $ruleset = compile_iptables_filter($cluster_conf, $hostfw_conf, $vmfw_configs, $vmdata, 4, $verbose); + my $rulesetv6 = compile_iptables_filter($cluster_conf, $hostfw_conf, $vmfw_configs, $vmdata, 6, $verbose); + my $ebtables_ruleset = compile_ebtables_filter($cluster_conf, $hostfw_conf, $vmfw_configs, $vmdata, $verbose); + my $ipset_ruleset = compile_ipsets($cluster_conf, $vmfw_configs, $vmdata); + + return ($ruleset, $ipset_ruleset, $rulesetv6, $ebtables_ruleset); +} + +sub compile_iptables_filter { + my ($cluster_conf, $hostfw_conf, $vmfw_configs, $vmdata, $ipversion, $verbose) = @_; my $ruleset = {}; @@ -3092,50 +3469,34 @@ sub compile_iptables_filter { # fixme: what log level should we use here? my $loglevel = get_option_log_level($hostfw_options, "log_level_out"); - ruleset_chain_add_conn_filters($ruleset, "PVEFW-FORWARD", "ACCEPT"); - - - ruleset_create_chain($ruleset, "PVEFW-VENET-OUT"); - ruleset_addrule($ruleset, "PVEFW-FORWARD", "-i venet0 -m set --match-set ${venet0_ipset_chain} src -j PVEFW-VENET-OUT"); - ruleset_addrule($ruleset, "PVEFW-INPUT", "-i venet0 -m set --match-set ${venet0_ipset_chain} src -j PVEFW-VENET-OUT"); + my $conn_allow_invalid = $hostfw_options->{nf_conntrack_allow_invalid} // 0; + ruleset_chain_add_conn_filters($ruleset, "PVEFW-FORWARD", $conn_allow_invalid, "ACCEPT"); ruleset_create_chain($ruleset, "PVEFW-FWBR-IN"); ruleset_chain_add_input_filters($ruleset, "PVEFW-FWBR-IN", $ipversion, $hostfw_options, $cluster_conf, $loglevel); - ruleset_addrule($ruleset, "PVEFW-FORWARD", "-m physdev --physdev-is-bridged --physdev-in fwln+ -j PVEFW-FWBR-IN"); + ruleset_addrule($ruleset, "PVEFW-FORWARD", "-m physdev --physdev-is-bridged --physdev-in fwln+", "-j PVEFW-FWBR-IN"); ruleset_create_chain($ruleset, "PVEFW-FWBR-OUT"); - 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", $ipversion, $hostfw_options, $cluster_conf, $loglevel); - - ruleset_addrule($ruleset, "PVEFW-FORWARD", "-o venet0 -m set --match-set ${venet0_ipset_chain} dst -j PVEFW-VENET-IN"); + ruleset_addrule($ruleset, "PVEFW-FORWARD", "-m physdev --physdev-is-bridged --physdev-out fwln+", "-j PVEFW-FWBR-OUT"); generate_std_chains($ruleset, $hostfw_options, $ipversion); my $hostfw_enable = !(defined($hostfw_options->{enable}) && ($hostfw_options->{enable} == 0)); - my $ipset_ruleset = {}; - - # currently pveproxy don't works with ipv6, so let's generate host fw ipv4 only for the moment - if ($hostfw_enable && ($ipversion == 4)) { + if ($hostfw_enable) { eval { enable_host_firewall($ruleset, $hostfw_conf, $cluster_conf, $ipversion); }; warn $@ if $@; # just to be sure - should not happen } - ruleset_addrule($ruleset, "PVEFW-OUTPUT", "-o venet0 -m set --match-set ${venet0_ipset_chain} dst -j PVEFW-VENET-IN"); - # generate firewall rules for QEMU VMs - foreach my $vmid (keys %{$vmdata->{qemu}}) { + foreach my $vmid (sort keys %{$vmdata->{qemu}}) { eval { my $conf = $vmdata->{qemu}->{$vmid}; my $vmfw_conf = $vmfw_configs->{$vmid}; return if !$vmfw_conf; - generate_ipset_chains($ipset_ruleset, $cluster_conf, $vmfw_conf); - - foreach my $netid (keys %$conf) { + foreach my $netid (sort keys %$conf) { next if $netid !~ m/^net(\d+)$/; my $net = PVE::QemuServer::parse_net($conf->{$netid}); next if !$net->{firewall}; @@ -3151,75 +3512,300 @@ sub compile_iptables_filter { warn $@ if $@; # just to be sure - should not happen } - # generate firewall rules for OpenVZ containers - foreach my $vmid (keys %{$vmdata->{openvz}}) { + # generate firewall rules for LXC containers + foreach my $vmid (sort keys %{$vmdata->{lxc}}) { + eval { + my $conf = $vmdata->{lxc}->{$vmid}; + my $vmfw_conf = $vmfw_configs->{$vmid}; + return if !$vmfw_conf; + + if ($vmfw_conf->{options}->{enable}) { + foreach my $netid (sort keys %$conf) { + next if $netid !~ m/^net(\d+)$/; + my $net = PVE::LXC::Config->parse_lxc_network($conf->{$netid}); + next if !$net->{firewall}; + my $iface = "veth${vmid}i$1"; + my $macaddr = $net->{hwaddr}; + generate_tap_rules_direction($ruleset, $cluster_conf, $iface, $netid, $macaddr, + $vmfw_conf, $vmid, 'IN', $ipversion); + generate_tap_rules_direction($ruleset, $cluster_conf, $iface, $netid, $macaddr, + $vmfw_conf, $vmid, 'OUT', $ipversion); + } + } + }; + 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"); + } + + return $ruleset; +} + +sub mac_to_linklocal { + my ($macaddr) = @_; + my @parts = split(/:/, $macaddr); + # The standard link local address uses the fe80::/64 prefix with the + # modified EUI-64 identifier derived from the MAC address by flipping the + # universal/local bit and inserting FF:FE in the middle. + # See RFC 4291. + $parts[0] = sprintf("%02x", hex($parts[0]) ^ 0x02); + my @meui64 = (@parts[0,1,2], 'ff', 'fe', @parts[3,4,5]); + return "fe80::$parts[0]$parts[1]:$parts[2]FF:FE$parts[3]:$parts[4]$parts[5]"; +} + +sub compile_ipsets { + my ($cluster_conf, $vmfw_configs, $vmdata) = @_; + + my $localnet; + if ($cluster_conf->{aliases}->{local_network}) { + $localnet = $cluster_conf->{aliases}->{local_network}->{cidr}; + } else { + my $localnet_ver; + ($localnet, $localnet_ver) = parse_ip_or_cidr(local_network() || '127.0.0.0/8'); + + $cluster_conf->{aliases}->{local_network} = { + name => 'local_network', cidr => $localnet, ipversion => $localnet_ver }; + } + + push @{$cluster_conf->{ipset}->{management}}, { cidr => $localnet }; + + + my $ipset_ruleset = {}; + + # generate ipsets for QEMU VMs + foreach my $vmid (keys %{$vmdata->{qemu}}) { eval { - my $conf = $vmdata->{openvz}->{$vmid}; + my $conf = $vmdata->{qemu}->{$vmid}; + my $vmfw_conf = $vmfw_configs->{$vmid}; + return if !$vmfw_conf; + + # When the 'ipfilter' option is enabled every device for which there + # is no 'ipfilter-netX' ipset defiend gets an implicit empty default + # ipset. + # The reason is that ipfilter ipsets are always filled with standard + # IPv6 link-local filters. + my $ipsets = $vmfw_conf->{ipset}; + my $implicit_sets = {}; + + my $device_ips = {}; + foreach my $netid (keys %$conf) { + next if $netid !~ m/^net(\d+)$/; + my $net = PVE::QemuServer::parse_net($conf->{$netid}); + next if !$net->{firewall}; + + if ($vmfw_conf->{options}->{ipfilter} && !$ipsets->{"ipfilter-$netid"}) { + $implicit_sets->{"ipfilter-$netid"} = []; + } + + my $macaddr = $net->{macaddr}; + my $linklocal = mac_to_linklocal($macaddr); + $device_ips->{$netid} = [ + { cidr => $linklocal }, + { cidr => 'fe80::/10', nomatch => 1 } + ]; + } + + generate_ipset_chains($ipset_ruleset, $cluster_conf, $vmfw_conf, $device_ips, $ipsets); + generate_ipset_chains($ipset_ruleset, $cluster_conf, $vmfw_conf, $device_ips, $implicit_sets); + }; + warn $@ if $@; # just to be sure - should not happen + } + # generate firewall rules for LXC containers + foreach my $vmid (keys %{$vmdata->{lxc}}) { + eval { + my $conf = $vmdata->{lxc}->{$vmid}; my $vmfw_conf = $vmfw_configs->{$vmid}; return if !$vmfw_conf; - generate_ipset_chains($ipset_ruleset, $cluster_conf, $vmfw_conf); + # When the 'ipfilter' option is enabled every device for which there + # is no 'ipfilter-netX' ipset defiend gets an implicit empty default + # ipset. + # The reason is that ipfilter ipsets are always filled with standard + # IPv6 link-local filters, as well as the IP addresses configured + # for the container. + my $ipsets = $vmfw_conf->{ipset}; + my $implicit_sets = {}; - if ($vmfw_conf->{options}->{enable}) { - if ($conf->{ip_address} && $conf->{ip_address}->{value}) { - my $ip = $conf->{ip_address}->{value}; - $ip =~ s/\s+/,/g; + my $device_ips = {}; + foreach my $netid (keys %$conf) { + next if $netid !~ m/^net(\d+)$/; + my $net = PVE::LXC::Config->parse_lxc_network($conf->{$netid}); + next if !$net->{firewall}; - my @ips = (); + if ($vmfw_conf->{options}->{ipfilter} && !$ipsets->{"ipfilter-$netid"}) { + $implicit_sets->{"ipfilter-$netid"} = []; + } - foreach my $singleip (split(',', $ip)) { - my $singleip_ver = parse_address_list($singleip); # make sure we have a valid $ip list - push @{$cluster_conf->{ipset}->{venet0}}, { cidr => $singleip }; - push @ips, $singleip if $singleip_ver == $ipversion; - } + my $macaddr = $net->{hwaddr}; + my $linklocal = mac_to_linklocal($macaddr); + my $set = $device_ips->{$netid} = [ + { cidr => $linklocal }, + { cidr => 'fe80::/10', nomatch => 1 } + ]; + if (defined($net->{ip}) && $net->{ip} =~ m!^($IPV4RE)(?:/\d+)?$!) { + push @$set, { cidr => $1 }; + } + if (defined($net->{ip6}) && $net->{ip6} =~ m!^($IPV6RE)(?:/\d+)?$!) { + push @$set, { cidr => $1 }; + } + } + + generate_ipset_chains($ipset_ruleset, $cluster_conf, $vmfw_conf, $device_ips, $ipsets); + generate_ipset_chains($ipset_ruleset, $cluster_conf, $vmfw_conf, $device_ips, $implicit_sets); + }; + warn $@ if $@; # just to be sure - should not happen + } + + generate_ipset_chains($ipset_ruleset, undef, $cluster_conf, undef, $cluster_conf->{ipset}); + + return $ipset_ruleset; +} + +sub compile_ebtables_filter { + my ($cluster_conf, $hostfw_conf, $vmfw_configs, $vmdata, $verbose) = @_; + + if (!($cluster_conf->{options}->{ebtables} // 1)) { + return {}; + } + + my $ruleset = {}; - if (scalar(@ips)) { - my $ip_list = join(',', @ips); - generate_venet_rules_direction($ruleset, $cluster_conf, $vmfw_conf, $vmid, $ip_list, 'IN', $ipversion); - generate_venet_rules_direction($ruleset, $cluster_conf, $vmfw_conf, $vmid, $ip_list, 'OUT', $ipversion); + ruleset_create_chain($ruleset, "PVEFW-FORWARD"); + + ruleset_create_chain($ruleset, "PVEFW-FWBR-OUT"); + #for ipv4 and ipv6, check macaddress in iptables, so we use conntrack 'ESTABLISHED', to speedup rules + ruleset_addrule($ruleset, 'PVEFW-FORWARD', '-p IPv4', '-j ACCEPT'); + ruleset_addrule($ruleset, 'PVEFW-FORWARD', '-p IPv6', '-j ACCEPT'); + ruleset_addrule($ruleset, 'PVEFW-FORWARD', '-o fwln+', '-j PVEFW-FWBR-OUT'); + + # generate firewall rules for QEMU VMs + foreach my $vmid (sort keys %{$vmdata->{qemu}}) { + eval { + my $conf = $vmdata->{qemu}->{$vmid}; + my $vmfw_conf = $vmfw_configs->{$vmid}; + return if !$vmfw_conf; + my $ipsets = $vmfw_conf->{ipset}; + + foreach my $netid (sort 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}; + my $arpfilter = []; + if (defined(my $ipset = $ipsets->{"ipfilter-$netid"})) { + foreach my $ipaddr (@$ipset) { + my($ip, $version) = parse_ip_or_cidr($ipaddr->{cidr}); + next if !$ip || ($version && $version != 4); + push(@$arpfilter, $ip); } } + generate_tap_layer2filter($ruleset, $iface, $macaddr, $vmfw_conf, $vmid, $arpfilter); } + }; + warn $@ if $@; # just to be sure - should not happen + } + + # generate firewall rules for LXC containers + foreach my $vmid (sort keys %{$vmdata->{lxc}}) { + eval { + my $conf = $vmdata->{lxc}->{$vmid}; + + my $vmfw_conf = $vmfw_configs->{$vmid}; + return if !$vmfw_conf || !$vmfw_conf->{options}->{enable}; + my $ipsets = $vmfw_conf->{ipset}; - 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 $bridge = $d->{bridge}; - next if !$bridge || $bridge !~ m/^vmbr\d+(v(\d+))?f$/; # firewall enabled ? - my $macaddr = $d->{mac}; - my $iface = $d->{host_ifname}; - generate_tap_rules_direction($ruleset, $cluster_conf, $iface, $netid, $macaddr, - $vmfw_conf, $vmid, 'IN', $ipversion); - generate_tap_rules_direction($ruleset, $cluster_conf, $iface, $netid, $macaddr, - $vmfw_conf, $vmid, 'OUT', $ipversion); + foreach my $netid (sort keys %$conf) { + next if $netid !~ m/^net(\d+)$/; + my $net = PVE::LXC::Config->parse_lxc_network($conf->{$netid}); + next if !$net->{firewall}; + my $iface = "veth${vmid}i$1"; + my $macaddr = $net->{hwaddr}; + my $arpfilter = []; + if (defined(my $ipset = $ipsets->{"ipfilter-$netid"})) { + foreach my $ipaddr (@$ipset) { + my($ip, $version) = parse_ip_or_cidr($ipaddr->{cidr}); + next if !$ip || ($version && $version != 4); + push(@$arpfilter, $ip); + } } + push(@$arpfilter, $net->{ip}) if $net->{ip} && $vmfw_conf->{options}->{ipfilter}; + generate_tap_layer2filter($ruleset, $iface, $macaddr, $vmfw_conf, $vmid, $arpfilter); } }; 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"); + return $ruleset; +} + +sub generate_tap_layer2filter { + my ($ruleset, $iface, $macaddr, $vmfw_conf, $vmid, $arpfilter) = @_; + my $options = $vmfw_conf->{options}; + + my $tapchain = $iface."-OUT"; + + # ebtables remove zeros from mac pairs + $macaddr =~ s/0([0-9a-f])/$1/ig; + $macaddr = lc($macaddr); + + ruleset_create_chain($ruleset, $tapchain); + + if (defined($macaddr) && !(defined($options->{macfilter}) && $options->{macfilter} == 0)) { + ruleset_addrule($ruleset, $tapchain, "-s ! $macaddr", '-j DROP'); + } + + if (@$arpfilter){ + my $arpchain = $tapchain."-ARP"; + ruleset_addrule($ruleset, $tapchain, "-p ARP", "-j $arpchain"); + ruleset_create_chain($ruleset, $arpchain); + + foreach my $ip (@{$arpfilter}) { + ruleset_addrule($ruleset, $arpchain, "-p ARP --arp-ip-src $ip", '-j RETURN'); + } + ruleset_addrule($ruleset, $arpchain, '', '-j DROP'); + } + + if (defined($options->{layer2_protocols})){ + my $protochain = $tapchain."-PROTO"; + ruleset_addrule($ruleset, $tapchain, '', "-j $protochain"); + ruleset_create_chain($ruleset, $protochain); + + foreach my $proto (split(/,/, $options->{layer2_protocols})) { + ruleset_addrule($ruleset, $protochain, "-p $proto", '-j RETURN'); + } + ruleset_addrule($ruleset, $protochain, '', '-j DROP'); } - generate_ipset_chains($ipset_ruleset, undef, $cluster_conf); + ruleset_addrule($ruleset, $tapchain, '', '-j ACCEPT'); - return ($ruleset, $ipset_ruleset); + ruleset_addrule($ruleset, 'PVEFW-FWBR-OUT', "-i $iface", "-j $tapchain"); } +# the parameter $change_only_regex changes two things if defined: +# * all chains not matching it will be left intact +# * both the $active_chains hash and the returned status_hash have different +# structure (they contain a key named 'rules'). sub get_ruleset_status { - my ($ruleset, $active_chains, $digest_fn, $verbose) = @_; + my ($ruleset, $active_chains, $digest_fn, $verbose, $change_only_regex) = @_; my $statushash = {}; foreach my $chain (sort keys %$ruleset) { - my $sig = &$digest_fn($ruleset->{$chain}); + my $rules = $ruleset->{$chain}; + my $sig = &$digest_fn($rules); + my $oldsig; $statushash->{$chain}->{sig} = $sig; - - my $oldsig = $active_chains->{$chain}; + if (defined($change_only_regex)) { + $oldsig = $active_chains->{$chain}->{sig}; + $statushash->{$chain}->{rules} = $rules; + } else { + $oldsig = $active_chains->{$chain}; + } if (!defined($oldsig)) { $statushash->{$chain}->{action} = 'create'; } else { @@ -3229,19 +3815,26 @@ sub get_ruleset_status { $statushash->{$chain}->{action} = 'update'; } } - print "$statushash->{$chain}->{action} $chain ($sig)\n" if $verbose; - foreach my $cmd (@{$ruleset->{$chain}}) { - print "\t$cmd\n" if $verbose; + if ($verbose) { + print "$statushash->{$chain}->{action} $chain ($sig)\n"; + foreach my $cmd (@{$rules}) { + print "\t$cmd\n"; + } } } foreach my $chain (sort keys %$active_chains) { - if (!defined($ruleset->{$chain})) { - my $sig = $active_chains->{$chain}; - $statushash->{$chain}->{action} = 'delete'; - $statushash->{$chain}->{sig} = $sig; - print "delete $chain ($sig)\n" if $verbose; + next if defined($ruleset->{$chain}); + my $action = 'delete'; + my $sig = $active_chains->{$chain}; + if (defined($change_only_regex)) { + $action = 'ignore' if ($chain !~ m/$change_only_regex/); + $statushash->{$chain}->{rules} = $active_chains->{$chain}->{rules}; + $sig = $sig->{sig}; } + $statushash->{$chain}->{action} = $action; + $statushash->{$chain}->{sig} = $sig; + print "$action $chain ($sig)\n" if $verbose; } return $statushash; @@ -3316,6 +3909,46 @@ sub get_ruleset_cmdlist { return wantarray ? ($cmdlist, $changes) : $cmdlist; } +my $pve_ebtables_chainname_regex = qr/PVEFW-\S+|(?:tap|veth)\d+i\d+-(?:IN|OUT)/; + +sub get_ebtables_cmdlist { + my ($ruleset, $verbose) = @_; + + my $changes = 0; + my $cmdlist = "*filter\n"; + + my $active_chains = ebtables_get_chains(); + my $statushash = get_ruleset_status($ruleset, $active_chains, + \&iptables_chain_digest, $verbose, + $pve_ebtables_chainname_regex); + + # create chains first and make sure PVE rules are evaluated if active + my $append_pve_to_forward = '-A FORWARD -j PVEFW-FORWARD'; + my $pve_include = 0; + foreach my $chain (sort keys %$statushash) { + next if ($statushash->{$chain}->{action} eq 'delete'); + $cmdlist .= ":$chain ACCEPT\n"; + $pve_include = 1 if ($chain eq 'PVEFW-FORWARD'); + } + + foreach my $chain (sort keys %$statushash) { + my $stat = $statushash->{$chain}; + next if ($stat->{action} eq 'delete'); + $changes = 1 if ($stat->{action} !~ 'ignore|exists'); + + foreach my $cmd (@{$statushash->{$chain}->{'rules'}}) { + if ($chain eq 'FORWARD' && $cmd eq $append_pve_to_forward) { + next if ! $pve_include; + $pve_include = 0; + } + $cmdlist .= "$cmd\n"; + } + } + $cmdlist .= "$append_pve_to_forward\n" if $pve_include; + + return wantarray ? ($cmdlist, $changes) : $cmdlist; +} + sub get_ipset_cmdlist { my ($ruleset, $verbose) = @_; @@ -3375,15 +4008,16 @@ sub get_ipset_cmdlist { } sub apply_ruleset { - my ($ruleset, $hostfw_conf, $ipset_ruleset, $rulesetv6, $verbose) = @_; + my ($ruleset, $hostfw_conf, $ipset_ruleset, $rulesetv6, $ebtables_ruleset, $verbose) = @_; enable_bridge_firewall(); my ($ipset_create_cmdlist, $ipset_delete_cmdlist, $ipset_changes) = - get_ipset_cmdlist($ipset_ruleset, undef, $verbose); + get_ipset_cmdlist($ipset_ruleset, $verbose); my ($cmdlist, $changes) = get_ruleset_cmdlist($ruleset, $verbose); my ($cmdlistv6, $changesv6) = get_ruleset_cmdlist($rulesetv6, $verbose, "ip6tables"); + my ($ebtables_cmdlist, $ebtables_changes) = get_ebtables_cmdlist($ebtables_ruleset, $verbose); if ($verbose) { if ($ipset_changes) { @@ -3401,6 +4035,11 @@ sub apply_ruleset { print "ip6tables changes:\n"; print $cmdlistv6; } + + if ($ebtables_changes) { + print "ebtables changes:\n"; + print $ebtables_cmdlist; + } } my $tmpfile = "$pve_fw_status_dir/ipsetcmdlist1"; @@ -3423,6 +4062,11 @@ sub apply_ruleset { ipset_restore_cmdlist($ipset_delete_cmdlist) if $ipset_delete_cmdlist; + ebtables_restore_cmdlist($ebtables_cmdlist); + + $tmpfile = "$pve_fw_status_dir/ebtablescmdlist"; + PVE::Tools::file_set_contents($tmpfile, $ebtables_cmdlist || ''); + # test: re-read status and check if everything is up to date my $active_chains = iptables_get_chains(); my $statushash = get_ruleset_status($ruleset, $active_chains, \&iptables_chain_digest, 0); @@ -3447,12 +4091,26 @@ sub apply_ruleset { } } + my $active_ebtables_chains = ebtables_get_chains(); + my $ebtables_statushash = get_ruleset_status($ebtables_ruleset, + $active_ebtables_chains, \&iptables_chain_digest, + 0, $pve_ebtables_chainname_regex); + + foreach my $chain (sort keys %$ebtables_ruleset) { + my $stat = $ebtables_statushash->{$chain}; + if ($stat->{action} ne 'exists') { + warn "ebtables : unable to update chain '$chain'\n"; + $errors = 1; + } + } + die "unable to apply firewall changes\n" if $errors; update_nf_conntrack_max($hostfw_conf); update_nf_conntrack_tcp_timeout_established($hostfw_conf); + update_nf_conntrack_logging($hostfw_conf); } sub update_nf_conntrack_max { @@ -3489,6 +4147,23 @@ sub update_nf_conntrack_tcp_timeout_established { PVE::ProcFSTools::write_proc_entry("/proc/sys/net/netfilter/nf_conntrack_tcp_timeout_established", $value); } +my $log_nf_conntrack_enabled = undef; +sub update_nf_conntrack_logging { + my ($hostfw_conf) = @_; + + my $options = $hostfw_conf->{options} || {}; + my $value = $options->{log_nf_conntrack} || 0; + if (!defined($log_nf_conntrack_enabled) + || $value != $log_nf_conntrack_enabled) + { + my $tmpfile = "$pve_fw_status_dir/log_nf_conntrack"; + PVE::Tools::file_set_contents($tmpfile, $value); + + PVE::Tools::run_command([qw(systemctl try-reload-or-restart pvefw-logger.service)]); + $log_nf_conntrack_enabled = $value; + } +} + sub remove_pvefw_chains { PVE::Firewall::remove_pvefw_chains_iptables("iptables"); @@ -3562,9 +4237,9 @@ sub update { my $hostfw_conf = load_hostfw_conf($cluster_conf); - my ($ruleset, $ipset_ruleset, $rulesetv6) = compile($cluster_conf, $hostfw_conf); + my ($ruleset, $ipset_ruleset, $rulesetv6, $ebtables_ruleset) = compile($cluster_conf, $hostfw_conf); - apply_ruleset($ruleset, $hostfw_conf, $ipset_ruleset, $rulesetv6); + apply_ruleset($ruleset, $hostfw_conf, $ipset_ruleset, $rulesetv6, $ebtables_ruleset); }; run_locked($code);