]> git.proxmox.com Git - pve-firewall.git/blobdiff - src/PVE/Firewall.pm
bump version to 5.0.3
[pve-firewall.git] / src / PVE / Firewall.pm
index 96cf9bd915b900ad73ecca18ebad22da62bcaebb..77cbaf46598ff7eff0842a0484ef90239e4a58b0 100644 (file)
@@ -2,27 +2,30 @@ package PVE::Firewall;
 
 use warnings;
 use strict;
-use POSIX;
-use Data::Dumper;
+
 use Digest::SHA;
-use Socket qw(AF_INET6 inet_ntop inet_pton);
-use PVE::INotify;
-use PVE::Exception qw(raise raise_param_exc);
-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 Encode;
 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 POSIX;
+use Socket qw(AF_INET AF_INET6 inet_ntop inet_pton);
 use Storable qw(dclone);
 
-my $hostfw_conf_filename = "/etc/pve/local/host.fw";
+use PVE::Cluster;
+use PVE::Corosync;
+use PVE::Exception qw(raise raise_param_exc);
+use PVE::INotify;
+use PVE::JSONSchema qw(register_standard_option get_standard_option);
+use PVE::Network;
+use PVE::ProcFSTools;
+use PVE::SafeSyslog;
+use PVE::Tools qw($IPV4RE $IPV6RE);
+use PVE::Tools qw(run_command lock_file dir_glob_foreach);
+
+use PVE::Firewall::Helpers;
+
 my $pvefw_conf_dir = "/etc/pve/firewall";
 my $clusterfw_conf_filename = "$pvefw_conf_dir/cluster.fw";
 
@@ -41,7 +44,6 @@ eval {
     $have_lxc = 1;
 };
 
-
 my $pve_fw_status_dir = "/var/lib/pve-firewall";
 
 mkdir $pve_fw_status_dir; # make sure this exists
@@ -66,9 +68,13 @@ PVE::JSONSchema::register_format('IPorCIDR', \&pve_verify_ip_or_cidr);
 sub pve_verify_ip_or_cidr {
     my ($cidr, $noerr) = @_;
 
-    if ($cidr =~ m!^(?:$IPV6RE|$IPV4RE)(/(\d+))?$!) {
-       return $cidr if Net::IP->new($cidr);
+    if ($cidr =~ m!^(?:$IPV6RE|$IPV4RE)(?:/\d+)?$!) {
+        # Net::IP throws an error if the masked CIDR part isn't zero, e.g., `192.168.1.155/24`
+        # fails but `192.168.1.0/24` succeeds. clean_cidr removes the non zero bits from the CIDR.
+       my $clean_cidr = clean_cidr($cidr);
+       return $cidr if Net::IP->new($clean_cidr);
        return undef if $noerr;
+
        die Net::IP::Error() . "\n";
     }
     return undef if $noerr;
@@ -79,11 +85,24 @@ PVE::JSONSchema::register_format('IPorCIDRorAlias', \&pve_verify_ip_or_cidr_or_a
 sub pve_verify_ip_or_cidr_or_alias {
     my ($cidr, $noerr) = @_;
 
-    return if $cidr =~ m/^(?:$ip_alias_pattern)$/;
+    return if $cidr =~ m@^(dc/|guest/)?(?:$ip_alias_pattern)$@;
 
     return pve_verify_ip_or_cidr($cidr, $noerr);
 }
 
+sub clean_cidr {
+    my ($cidr) = @_;
+    my ($ip, $len) = split('/', $cidr);
+    return $cidr if !$len;
+    my $ver = ($ip =~ m!^$IPV4RE$!) ? 4 : 6;
+
+    my $bin_ip = Net::IP::ip_iptobin( Net::IP::ip_expand_address($ip, $ver), $ver);
+    my $bin_mask = Net::IP::ip_get_mask($len, $ver);
+    my $clean_ip = Net::IP::ip_compress_address( Net::IP::ip_bintoip($bin_ip & $bin_mask, $ver), $ver);
+
+    return "${clean_ip}/$len";
+}
+
 PVE::JSONSchema::register_standard_option('ipset-name', {
     description => "IP set name.",
     type => 'string',
@@ -127,10 +146,12 @@ eval  {
 };
 
 my $nodename = PVE::INotify::nodename();
+my $hostfw_conf_filename = "/etc/pve/nodes/$nodename/host.fw";
 
 my $pve_fw_lock_filename = "/var/lock/pvefw.lck";
 
 my $default_log_level = 'nolog'; # avoid logs by default
+my $global_log_ratelimit = '--limit 1/sec';
 
 my $log_level_hash = {
     debug => 7,
@@ -143,6 +164,11 @@ my $log_level_hash = {
     emerg => 0,
 };
 
+my $verbose = 0;
+sub set_verbose {
+    $verbose = shift;
+}
+
 # %rule
 #
 # name => optional
@@ -206,8 +232,11 @@ my $pve_fw_macros = {
        { action => 'PARAM', proto => 'udp', dport => '6881' },
     ],
     'Ceph' => [
-        "Ceph Storage Cluster traffic (Ceph Monitors, OSD & MDS Deamons)",
+        "Ceph Storage Cluster traffic (Ceph Monitors, OSD & MDS Daemons)",
+       # Legacy port for protocol v1
         { action => 'PARAM', proto => 'tcp', dport => '6789' },
+       # New port for protocol v2
+        { action => 'PARAM', proto => 'tcp', dport => '3300' },
         { action => 'PARAM', proto => 'tcp', dport => '6800:7300' },
     ],
     'CVS' => [
@@ -266,7 +295,7 @@ my $pve_fw_macros = {
        { action => 'PARAM', proto => 'tcp', dport => '9418' },
     ],
     'HKP' => [
-       "OpenPGP HTTP keyserver protocol traffic",
+       "OpenPGP HTTP key server protocol traffic",
        { action => 'PARAM', proto => 'tcp', dport => '11371' },
     ],
     'HTTP' => [
@@ -384,6 +413,10 @@ my $pve_fw_macros = {
        { action => 'PARAM', proto => 'udp', dport => '5632' },
        { action => 'PARAM', proto => 'tcp', dport => '5631' },
     ],
+    'PMG' => [
+       "Proxmox Mail Gateway web interface",
+       { action => 'PARAM', proto => 'tcp', dport => '8006' },
+    ],
     'POP3' => [
        "POP3 traffic",
        { action => 'PARAM', proto => 'tcp', dport => '110' },
@@ -423,7 +456,7 @@ my $pve_fw_macros = {
     ],
     'Razor' => [
        "Razor Antispam System",
-       { action => 'ACCEPT', proto => 'tcp', dport => '2703' },
+       { action => 'PARAM', proto => 'tcp', dport => '2703' },
     ],
     'Rdate' => [
        "Remote time retrieval (rdate)",
@@ -480,6 +513,10 @@ my $pve_fw_macros = {
        { action => 'PARAM', proto => '41' },
        { action => 'PARAM', proto => 'udp', dport => '5072,8374' },
     ],
+    'SPICEproxy' => [
+       "Proxmox VE SPICE display proxy traffic",
+       { action => 'PARAM', proto => 'tcp', dport => '3128' },
+    ],
     'Squid' => [
        "Squid web proxy traffic",
        { action => 'PARAM', proto => 'tcp', dport => '3128' },
@@ -541,6 +578,18 @@ my $pve_fw_macros = {
     ],
 };
 
+my $pve_fw_helpers = {
+    'amanda' => { proto => 'udp', dport => '10080', 'v4' => 1, 'v6' => 1 },
+    'ftp' => { proto => 'tcp', dport => '21', 'v4' => 1, 'v6' => 1},
+    'irc' => { proto => 'tcp', dport => '6667', 'v4' => 1 },
+    'netbios-ns' => { proto => 'udp', dport => '137', 'v4' => 1 },
+    'pptp' => { proto => 'tcp', dport => '1723', 'v4' => 1, },
+    'sane' => { proto => 'tcp', dport => '6566', 'v4' => 1, 'v6' => 1 },
+    'sip' => { proto => 'udp', dport => '5060', 'v4' => 1, 'v6' => 1 },
+    'snmp' => { proto => 'udp', dport => '161', 'v4' => 1 },
+    'tftp' => { proto => 'udp', dport => '69', 'v4' => 1, 'v6' => 1},
+};
+
 my $pve_fw_parsed_macros;
 my $pve_fw_macro_descr;
 my $pve_fw_macro_ipversion = {};
@@ -578,7 +627,6 @@ $pve_std_chains_conf->{4} = {
        # 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
@@ -601,7 +649,6 @@ $pve_std_chains_conf->{4} = {
        # 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
@@ -622,7 +669,7 @@ $pve_std_chains_conf->{4} = {
     ],
     'PVEFW-tcpflags' => [
        # same as shorewall tcpflags action.
-       # Packets arriving on this interface are checked for som illegal combinations of TCP flags
+       # Packets arriving on this interface are checked for some 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' },
@@ -660,14 +707,10 @@ $pve_std_chains_conf->{6} = {
        #{ action => 'DROP', dest => '224.0.0.0/4' },
     ],
     'PVEFW-reject' => [
-       # same as shorewall 'reject'
-       #{ action => 'DROP', dsttype => 'BROADCAST' },
-       #{ action => 'DROP', source => '224.0.0.0/4' },
        { action => 'DROP', proto => 'icmpv6' },
        { 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",
+       { match => '-p udp', target => '-j REJECT --reject-with icmp6-port-unreachable' },
+       { target => '-j REJECT --reject-with icmp6-adm-prohibited' },
     ],
     'PVEFW-Drop' => [
        # same as shorewall 'Drop', which is equal to DROP,
@@ -719,7 +762,7 @@ $pve_std_chains_conf->{6} = {
     ],
     'PVEFW-tcpflags' => [
        # same as shorewall tcpflags action.
-       # Packets arriving on this interface are checked for som illegal combinations of TCP flags
+       # Packets arriving on this interface are checked for some 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' },
@@ -775,12 +818,14 @@ my $icmp_type_names = {
 # ip6tables -p icmpv6 -h
 
 my $icmpv6_type_names = {
-    'any' => 1,
     'destination-unreachable' => 1,
     'no-route' => 1,
     'communication-prohibited' => 1,
+    'beyond-scope' => 1,
     'address-unreachable' => 1,
     'port-unreachable' => 1,
+    'failed-policy' => 1,
+    'reject-route' => 1,
     'packet-too-big' => 1,
     'time-exceeded' => 1,
     'ttl-zero-during-transit' => 1,
@@ -800,6 +845,22 @@ my $icmpv6_type_names = {
     'redirect' => 1,
 };
 
+my $is_valid_icmp_type = sub {
+    my ($type, $valid_types) = @_;
+
+    if ($type =~ m/^\d+$/) {
+       # values for icmp-type range between 0 and 255 (8 bit field)
+       die "invalid icmp-type '$type'\n" if $type > 255;
+    } else {
+       die "unknown icmp-type '$type'\n" if !defined($valid_types->{$type});
+    }
+};
+
+my $proto_is_icmp = sub {
+    my $proto = shift;
+    return $proto eq 'icmp' || $proto eq 'icmpv6' || $proto eq 'ipv6-icmp';
+};
+
 sub init_firewall_macros {
 
     $pve_fw_parsed_macros = {};
@@ -972,8 +1033,8 @@ sub local_network {
 }
 
 # ipset names are limited to 31 characters,
-# and we use '-v4' or '-v6' to indicate IP versions, 
-# and we use '_swap' suffix for atomic update, 
+# and we use '-v4' or '-v6' to indicate IP versions,
+# and we use '_swap' suffix for atomic update,
 # for example PVEFW-${VMID}-${ipset_name}_swap
 
 my $max_iptables_ipset_name_length = 31 - length("PVEFW-") - length("_swap");
@@ -1006,7 +1067,7 @@ sub parse_address_list {
        return;
     }
 
-    if ($str =~ m/^${ip_alias_pattern}$/) {
+    if ($str =~ m@^(dc/|guest/)?${ip_alias_pattern}$@) {
        die "alias name too long\n" if length($str) > $max_alias_name_length;
        return;
     }
@@ -1039,6 +1100,9 @@ sub parse_address_list {
     return $ipversion;
 }
 
+# $dport must only be set to 1 if the parsed parameter is dport and the
+# protocol is one of the ICMP variants - ICMP type values used to be stored in
+# the dport parameter.
 sub parse_port_name_number_or_range {
     my ($str, $dport) = @_;
 
@@ -1049,12 +1113,13 @@ sub parse_port_name_number_or_range {
     my @elements = split(/,/, $str);
     die "extraneous commas in list\n" if $str ne join(',', @elements);
     foreach my $item (@elements) {
-       if ($item =~ m/^(\d+):(\d+)$/) {
+       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;
@@ -1069,7 +1134,7 @@ sub parse_port_name_number_or_range {
        }
     }
 
-    die "ICPM ports not allowed in port range\n" if $icmp_port && $count > 0;
+    die "ICMP 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
@@ -1080,6 +1145,19 @@ sub parse_port_name_number_or_range {
     return (scalar(@elements) > 1);
 }
 
+PVE::JSONSchema::register_format('pve-fw-conntrack-helper', \&pve_fw_verify_conntrack_helper);
+sub pve_fw_verify_conntrack_helper {
+   my ($list) = @_;
+
+   my @helpers = split(/,/, $list);
+   die "extraneous commas in list\n" if $list ne join(',', @helpers);
+   foreach my $helper (@helpers) {
+       die "unknown helper $helper" if !$pve_fw_helpers->{$helper};
+   }
+
+   return $list;
+}
+
 PVE::JSONSchema::register_format('pve-fw-sport-spec', \&pve_fw_verify_sport_spec);
 sub pve_fw_verify_sport_spec {
    my ($portstr) = @_;
@@ -1120,6 +1198,19 @@ sub pve_fw_verify_protocol_spec {
    return $proto;
 }
 
+PVE::JSONSchema::register_format('pve-fw-icmp-type-spec', \&pve_fw_verify_icmp_type_spec);
+sub pve_fw_verify_icmp_type_spec {
+    my ($icmp_type) = @_;
+
+    if ($icmp_type_names->{$icmp_type} ||  $icmpv6_type_names->{$icmp_type}) {
+       return $icmp_type;
+    }
+
+    die "invalid icmp-type value '$icmp_type'\n" if $icmp_type ne '';
+
+    return $icmp_type;
+}
+
 
 # helper function for API
 
@@ -1180,6 +1271,12 @@ our $cluster_option_properties = {
        minimum => 0,
        optional => 1,
     },
+    ebtables => {
+       description => "Enable ebtables rules cluster wide.",
+       type => 'boolean',
+       default => 1,
+       optional => 1,
+    },
     policy_in => {
        description => "Input policy.",
        type => 'string',
@@ -1192,6 +1289,33 @@ our $cluster_option_properties = {
        optional => 1,
        enum => ['ACCEPT', 'REJECT', 'DROP'],
     },
+    log_ratelimit => {
+       description => "Log ratelimiting settings",
+       type => 'string', format => {
+           enable => {
+               default_key => 1,
+               description => 'Enable or disable log rate limiting',
+               type => 'boolean',
+               default => '1',
+           },
+           rate => {
+               type => 'string',
+               description => 'Frequency with which the burst bucket gets refilled',
+               optional => 1,
+               pattern => '[1-9][0-9]*\/(second|minute|hour|day)',
+               format_description => 'rate',
+               default => '1/second',
+           },
+           burst => {
+               type => 'integer',
+               minimum => 0,
+               optional => 1,
+               description => 'Initial burst of packages which will always get logged before the rate is applied',
+               default => 5,
+           },
+       },
+       optional => 1,
+    },
 };
 
 our $host_option_properties = {
@@ -1216,46 +1340,99 @@ our $host_option_properties = {
     tcpflags => {
        description => "Filter illegal combinations of TCP flags.",
        type => 'boolean',
+       default => 0,
        optional => 1,
     },
     nf_conntrack_max => {
        description => "Maximum number of tracked connections.",
        type => 'integer',
        optional => 1,
+       default => 262144,
        minimum => 32768,
     },
     nf_conntrack_tcp_timeout_established => {
        description => "Conntrack established timeout.",
        type => 'integer',
        optional => 1,
+       default => 432000,
        minimum => 7875,
     },
+    nf_conntrack_tcp_timeout_syn_recv => {
+       description => "Conntrack syn recv timeout.",
+       type => 'integer',
+       optional => 1,
+       default => 60,
+       minimum => 30,
+       maximum => 60,
+    },
     ndp => {
-       description => "Enable NDP.",
+       description => "Enable NDP (Neighbor Discovery Protocol).",
+       type => 'boolean',
+       default => 0,
+       optional => 1,
+    },
+    nf_conntrack_allow_invalid => {
+       description => "Allow invalid packets on connection tracking.",
+       type => 'boolean',
+       default => 0,
+       optional => 1,
+    },
+    nf_conntrack_helpers => {
+       type => 'string', format => 'pve-fw-conntrack-helper',
+       description => "Enable conntrack helpers for specific protocols. ".
+           "Supported protocols: amanda, ftp, irc, netbios-ns, pptp, sane, sip, snmp, tftp",
+       default => '',
+       optional => 1,
+    },
+    protection_synflood => {
+       description => "Enable synflood protection",
        type => 'boolean',
+       default => 0,
        optional => 1,
     },
+    protection_synflood_rate => {
+       description => "Synflood protection rate syn/sec by ip src.",
+       type => 'integer',
+       optional => 1,
+       default => 200,
+    },
+    protection_synflood_burst => {
+       description => "Synflood protection rate burst by ip src.",
+       type => 'integer',
+       optional => 1,
+       default => 1000,
+    },
+    log_nf_conntrack => {
+       description => "Enable logging of conntrack information.",
+       type => 'boolean',
+       default => 0,
+       optional => 1
+    },
 };
 
 our $vm_option_properties = {
     enable => {
        description => "Enable/disable firewall rules.",
        type => 'boolean',
+       default => 0,
        optional => 1,
     },
     macfilter => {
        description => "Enable/disable MAC address filter.",
        type => 'boolean',
+       default => 1,
        optional => 1,
     },
     dhcp => {
        description => "Enable DHCP.",
        type => 'boolean',
+       default => 0,
        optional => 1,
     },
     ndp => {
-       description => "Enable NDP.",
+       description => "Enable NDP (Neighbor Discovery Protocol).",
        type => 'boolean',
+       default => 0,
        optional => 1,
     },
     radv => {
@@ -1333,11 +1510,13 @@ my $rule_properties = {
        description => "Restrict packet source address. $addr_list_descr",
        type => 'string', format => 'pve-fw-addr-spec',
        optional => 1,
+       maxLength => 512,
     },
     dest => {
        description => "Restrict packet destination address. $addr_list_descr",
        type => 'string', format => 'pve-fw-addr-spec',
        optional => 1,
+       maxLength => 512,
     },
     proto => {
        description => "IP protocol. You can use protocol names ('tcp'/'udp') or simple numbers, as defined in '/etc/protocols'.",
@@ -1350,6 +1529,9 @@ my $rule_properties = {
        minimum => 0,
        optional => 1,
     },
+    log => get_standard_option('pve-fw-loglevel', {
+       description => "Log level for firewall rule.",
+    }),
     sport => {
        description => "Restrict TCP/UDP source port. $port_descr",
        type => 'string', format => 'pve-fw-sport-spec',
@@ -1365,6 +1547,11 @@ my $rule_properties = {
        type => 'string',
        optional => 1,
     },
+    'icmp-type' => {
+       description => "Specify icmp-type. Only valid if proto equals 'icmp' or 'icmpv6'/'ipv6-icmp'.",
+       type => 'string', format => 'pve-fw-icmp-type-spec',
+       optional => 1,
+    },
 };
 
 sub add_rule_properties {
@@ -1485,7 +1672,7 @@ sub verify_rule {
     my $set_ip_version = sub {
        my $vers = shift;
        if ($vers) {
-           die "detected mixed ipv4/ipv6 adresses in rule\n"
+           die "detected mixed ipv4/ipv6 addresses in rule\n"
                if $ipversion && ($vers != $ipversion);
            $ipversion = $vers;
        }
@@ -1496,19 +1683,26 @@ sub verify_rule {
 
        if (my $value = $rule->{$name}) {
            if ($value =~ m/^\+/) {
-               if ($value =~ m/^\+(${ipset_name_pattern})$/) {
-                   &$add_error($name, "no such ipset '$1'")
-                       if !($cluster_conf->{ipset}->{$1} || ($fw_conf && $fw_conf->{ipset}->{$1}));
+               if ($value =~ m@^\+(guest/|dc/)?(${ipset_name_pattern})$@) {
+                   &$add_error($name, "no such ipset '$2'")
+                       if !($cluster_conf->{ipset}->{$2} || ($fw_conf && $fw_conf->{ipset}->{$2}));
 
                } else {
                    &$add_error($name, "invalid ipset name '$value'");
                }
-           } elsif ($value =~ m/^${ip_alias_pattern}$/){
-               my $alias = lc($value);
+           } elsif ($value =~ m@^(guest/|dc/)?(${ip_alias_pattern})$@){
+               my $scope = $1 // "";
+               my $alias = lc($2);
                &$add_error($name, "no such alias '$value'")
                    if !($cluster_conf->{aliases}->{$alias} || ($fw_conf && $fw_conf->{aliases}->{$alias}));
-               my $e = $fw_conf ? $fw_conf->{aliases}->{$alias} : undef;
-               $e = $cluster_conf->{aliases}->{$alias} if !$e && $cluster_conf;
+
+               my $e;
+               if ($scope ne 'dc/' && $fw_conf) {
+                   $e = $fw_conf->{aliases}->{$alias};
+               }
+               if ($scope ne 'guest/' && !$e && $cluster_conf) {
+                   $e = $cluster_conf->{aliases}->{$alias};
+               }
 
                &$set_ip_version($e->{ipversion});
            }
@@ -1554,22 +1748,37 @@ sub verify_rule {
        }
     }
 
+    my $is_icmp = 0;
     if ($rule->{proto}) {
        eval { pve_fw_verify_protocol_spec($rule->{proto}); };
        &$add_error('proto', $@) if $@;
        &$set_ip_version(4) if $rule->{proto} eq 'icmp';
-       &$set_ip_version(6) if $rule->{proto} eq 'icmpv6';
+       &$set_ip_version(6) if $rule->{proto} eq 'icmpv6';
+       &$set_ip_version(6) if $rule->{proto} eq 'ipv6-icmp';
+       $is_icmp = $proto_is_icmp->($rule->{proto});
     }
 
     if ($rule->{dport}) {
-       eval { parse_port_name_number_or_range($rule->{dport}, 1); };
+       eval { parse_port_name_number_or_range($rule->{dport}, $is_icmp); };
        &$add_error('dport', $@) if $@;
        my $proto = $rule->{proto};
        &$add_error('proto', "missing property - 'dport' requires this property")
            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 !$PROTOCOLS_WITH_PORTS->{$proto} && !$is_icmp; #special cases
+    }
+
+    if (my $icmp_type = $rule ->{'icmp-type'}) {
+       my $proto = $rule->{proto};
+       &$add_error('proto', "missing property - 'icmp-type' requires this property")
+           if !$is_icmp;
+       &$add_error('icmp-type', "'icmp-type' cannot be specified together with 'dport'")
+           if $rule->{dport};
+       if ($proto eq 'icmp' && !$icmp_type_names->{$icmp_type}) {
+           &$add_error('icmp-type', "invalid icmp-type '$icmp_type' for proto 'icmp'");
+       } elsif (($proto eq 'icmpv6' || $proto eq 'ipv6-icmp') && !$icmpv6_type_names->{$icmp_type}) {
+           &$add_error('icmp-type', "invalid icmp-type '$icmp_type' for proto '$proto'");
+       }
     }
 
     if ($rule->{sport}) {
@@ -1583,7 +1792,7 @@ sub verify_rule {
     }
 
     if ($rule->{source}) {
-       eval { 
+       eval {
            my $source_ipversion = parse_address_list($rule->{source});
            &$set_ip_version($source_ipversion);
        };
@@ -1592,8 +1801,8 @@ sub verify_rule {
     }
 
     if ($rule->{dest}) {
-       eval { 
-           my $dest_ipversion = parse_address_list($rule->{dest}); 
+       eval {
+           my $dest_ipversion = parse_address_list($rule->{dest});
            &$set_ip_version($dest_ipversion);
        };
        &$add_error('dest', $@) if $@;
@@ -1678,11 +1887,9 @@ sub rules_audit_permissions {
 }
 
 # core functions
-my $bridge_firewall_enabled = 0;
 
 sub enable_bridge_firewall {
 
-    return if $bridge_firewall_enabled; # only once
 
     PVE::ProcFSTools::write_proc_entry("/proc/sys/net/bridge/bridge-nf-call-iptables", "1");
     PVE::ProcFSTools::write_proc_entry("/proc/sys/net/bridge/bridge-nf-call-ip6tables", "1");
@@ -1690,37 +1897,39 @@ sub enable_bridge_firewall {
     # make sure syncookies are enabled (which is default on newer 3.X kernels anyways)
     PVE::ProcFSTools::write_proc_entry("/proc/sys/net/ipv4/tcp_syncookies", "1");
 
-    $bridge_firewall_enabled = 1;
 }
 
 sub iptables_restore_cmdlist {
-    my ($cmdlist) = @_;
+    my ($cmdlist, $table) = @_;
 
-    run_command("/sbin/iptables-restore -n", input => $cmdlist, errmsg => "iptables_restore_cmdlist");
+    $table = 'filter' if !$table;
+    run_command(['iptables-restore', '-T', $table, '-n'], input => $cmdlist, errmsg => "iptables_restore_cmdlist");
 }
 
 sub ip6tables_restore_cmdlist {
-    my ($cmdlist) = @_;
+    my ($cmdlist, $table) = @_;
 
-    run_command("/sbin/ip6tables-restore -n", input => $cmdlist, errmsg => "iptables_restore_cmdlist");
+    $table = 'filter' if !$table;
+    run_command(['ip6tables-restore', '-T', $table, '-n'], input => $cmdlist, errmsg => "iptables_restore_cmdlist");
 }
 
 sub ipset_restore_cmdlist {
     my ($cmdlist) = @_;
 
-    run_command("/sbin/ipset restore", input => $cmdlist, errmsg => "ipset_restore_cmdlist");
+    run_command(['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");
+    run_command(['ebtables-restore'], input => $cmdlist, errmsg => "ebtables_restore_cmdlist");
 }
 
 sub iptables_get_chains {
-    my ($iptablescmd) = @_;
+    my ($iptablescmd, $t) = @_;
 
     $iptablescmd = "iptables" if !$iptablescmd;
+    $t = 'filter' if !$t;
 
     my $res = {};
 
@@ -1755,7 +1964,7 @@ sub iptables_get_chains {
            return;
        }
 
-       return if $table ne 'filter';
+       return if $table ne $t;
 
        if ($line =~ m/^:(\S+)\s/) {
            my $chain = $1;
@@ -1765,7 +1974,7 @@ sub iptables_get_chains {
            my ($chain, $sig) = ($1, $2);
            return if !&$is_pvefw_chain($chain);
            $res->{$chain} = $sig;
-       } elsif ($line =~ m/^-A\s+(INPUT|OUTPUT|FORWARD)\s+-j\s+PVEFW-\1$/) {
+       } elsif ($line =~ m/^-A\s+(INPUT|OUTPUT|FORWARD|PREROUTING)\s+-j\s+PVEFW-\1$/) {
            $hooks->{$1} = 1;
        } else {
            # simply ignore the rest
@@ -1773,7 +1982,7 @@ sub iptables_get_chains {
        }
     };
 
-    run_command("/sbin/$iptablescmd-save", outfunc => $parser);
+    run_command(["$iptablescmd-save"], outfunc => $parser);
 
     return wantarray ? ($res, $hooks) : $res;
 }
@@ -1809,6 +2018,8 @@ sub ipset_get_chains {
        return if $line =~ m/^\s*$/;
        if ($line =~ m/^(?:\S+)\s(PVEFW-\S+)\s(?:\S+).*/) {
            my $chain = $1;
+           # ignore initval from ipset v7.7+, won't set that yet so it'd mess up change detection
+           $line =~ s/\binitval 0x[0-9a-f]+//;
            $line =~ s/\s+$//; # delete trailing white space
            push @{$chains->{$chain}}, $line;
        } else {
@@ -1817,7 +2028,7 @@ sub ipset_get_chains {
        }
     };
 
-    run_command("/sbin/ipset save", outfunc => $parser);
+    run_command(['ipset', 'save'], outfunc => $parser);
 
     # compute digest for each chain
     foreach my $chain (keys %$chains) {
@@ -1831,23 +2042,23 @@ sub ebtables_get_chains {
 
     my $res = {};
     my $chains = {};
-
+    my $table;
     my $parser = sub {
        my $line = shift;
        return if $line =~ m/^#/;
        return if $line =~ m/^\s*$/;
-       if ($line =~ m/^:(\S+)\s\S+$/) {
+       if ($line =~ m/^\*(\S+)$/) {
+           $table = $1;
+           return;
+       }
+
+       return if $table ne "filter";
+
+       if ($line =~ m/^:(\S+)\s(ACCEPT|DROP|RETURN)$/) {
            # Make sure we know chains exist even if they're empty.
            $chains->{$1} //= [];
-       } elsif ($line =~ m/^(?:\S+)\s(PVEFW-\S+)\s(?:\S+).*/) {
-           my $chain = $1;
-           $line =~ s/\s+$//;
-           push @{$chains->{$chain}}, $line;
-       } elsif ($line =~ m/^(?:\S+)\s(tap\d+i\d+-(:?IN|OUT))\s(?:\S+).*/) {
-           my $chain = $1;
-           $line =~ s/\s+$//;
-           push @{$chains->{$chain}}, $line;
-       } elsif ($line =~ m/^(?:\S+)\s(veth\d+i\d+-(:?IN|OUT))\s(?:\S+).*/) {
+           $res->{$1}->{policy} = $2;
+       } elsif ($line =~ m/^(?:\S+)\s(\S+)\s(?:\S+).*/) {
            my $chain = $1;
            $line =~ s/\s+$//;
            push @{$chains->{$chain}}, $line;
@@ -1857,16 +2068,16 @@ sub ebtables_get_chains {
        }
     };
 
-    run_command("/sbin/ebtables-save", outfunc => $parser);
-
-    # compute digest for each chain
+    run_command(['ebtables-save'], outfunc => $parser);
+    # compute digest for each chain and store rules as well
     foreach my $chain (keys %$chains) {
-       $res->{$chain} = iptables_chain_digest($chains->{$chain});
+       $res->{$chain}->{rules} = $chains->{$chain};
+       $res->{$chain}->{sig} = iptables_chain_digest($chains->{$chain});
     }
     return $res;
 }
 
-# substitude action of rule according to action hash
+# substitute action of rule according to action hash
 sub rule_substitude_action {
     my ($rule, $actions) = @_;
 
@@ -1891,12 +2102,13 @@ sub ipt_gen_src_or_dst_match {
 
     my $match;
     if ($adr =~ m/^\+/) {
-       if ($adr =~ m/^\+(${ipset_name_pattern})$/) {
-           my $name = $1;
+       if ($adr =~ m@^\+(guest/|dc/)?(${ipset_name_pattern})$@) {
+           my $scope = $1 // "";
+           my $name = $2;
            my $ipset_chain;
-           if ($fw_conf && $fw_conf->{ipset}->{$name}) {
+           if ($scope ne 'dc/' && $fw_conf && $fw_conf->{ipset}->{$name}) {
                $ipset_chain = compute_ipset_chain_name($fw_conf->{vmid}, $name, $ipversion);
-           } elsif ($cluster_conf && $cluster_conf->{ipset}->{$name}) {
+           } elsif ($scope ne 'guest/' && $cluster_conf && $cluster_conf->{ipset}->{$name}) {
                $ipset_chain = compute_ipset_chain_name(0, $name, $ipversion);
            } else {
                die "no such ipset '$name'\n";
@@ -1905,10 +2117,16 @@ sub ipt_gen_src_or_dst_match {
        } else {
            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;
+    } elsif ($adr =~ m@^(dc/|guest/)?(${ip_alias_pattern})$@){
+       my $scope = $1 // "";
+       my $alias = lc($2);
+       my $e;
+       if ($scope ne 'dc/' && $fw_conf) {
+           $e = $fw_conf->{aliases}->{$alias};
+       }
+       if ($scope ne 'guest/' && !$e && $cluster_conf) {
+           $e = $cluster_conf->{aliases}->{$alias};
+       }
        die "no such alias '$adr'\n" if !$e;
        $match = "-${dir} $e->{cidr}";
     } elsif ($adr =~ m/\-/){
@@ -1943,28 +2161,27 @@ sub ipt_rule_to_cmds {
 
        if (my $proto = $rule->{proto}) {
            push @match, "-p $proto";
+           my $is_icmp = $proto_is_icmp->($proto);
 
-           my $multidport = defined($rule->{dport}) && parse_port_name_number_or_range($rule->{dport}, 1);
+           my $multidport = defined($rule->{dport}) && parse_port_name_number_or_range($rule->{dport}, $is_icmp);
            my $multisport = defined($rule->{sport}) && parse_port_name_number_or_range($rule->{sport}, 0);
 
            my $add_dport = sub {
-               return if !$rule->{dport};
+               return if !defined($rule->{dport});
 
+               # NOTE: we re-use dport to store --icmp-type for icmp* protocol
                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}});
+                   $is_valid_icmp_type->($rule->{dport}, $icmp_type_names);
                    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}});
+                   $is_valid_icmp_type->($rule->{dport}, $icmpv6_type_names);
                    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 {
+                   return if !$rule->{dport};
                    push @match, "--dport $rule->{dport}";
                }
            };
@@ -1981,7 +2198,18 @@ sub ipt_rule_to_cmds {
                }
            };
 
+           my $add_icmp_type = sub {
+               return if !defined($rule->{'icmp-type'}) || $rule->{'icmp-type'} eq '';
+
+               die "'icmp-type' can only be set if 'icmp', 'icmpv6' or 'ipv6-icmp' is specified\n"
+                   if !$is_icmp;
+               my $type = $proto eq 'icmp' ? 'icmp-type' : 'icmpv6-type';
+
+               push @match, "-m $proto --$type $rule->{'icmp-type'}";
+           };
+
            # order matters - single port before multiport!
+           $add_icmp_type->();
            $add_dport->() if $multisport;
            $add_sport->();
            $add_dport->() if !$multisport;
@@ -1999,13 +2227,14 @@ sub ipt_rule_to_cmds {
        $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";
+       $targetstr = $action eq 'PVEFW-SET-ACCEPT-MARK' ? "-g $action" : "-j $action";
     }
 
     my @iptcmds;
-    if (defined $rule->{log} && $rule->{log}) {
-       my $logaction = get_log_rule_base($chain, $vmid, $rule->{logmsg}, $rule->{log});
+    my $log = $rule->{log};
+    if (defined($log) && $log ne 'nolog') {
+       my $loglevel = $log_level_hash->{$log};
+       my $logaction = get_log_rule_base($chain, $vmid, $rule->{logmsg}, $loglevel);
        push @iptcmds, "-A $chain $matchstr $logaction";
     }
     push @iptcmds, "-A $chain $matchstr $targetstr";
@@ -2013,7 +2242,7 @@ sub ipt_rule_to_cmds {
 }
 
 sub ruleset_generate_rule {
-    my ($ruleset, $chain, $ipversion, $rule, $cluster_conf, $fw_conf) = @_;
+    my ($ruleset, $chain, $ipversion, $rule, $cluster_conf, $fw_conf, $vmid) = @_;
 
     my $rules;
 
@@ -2026,7 +2255,7 @@ sub ruleset_generate_rule {
     # update all or nothing
     my @ipt_rule_cmds;
     foreach my $r (@$rules) {
-       push @ipt_rule_cmds, ipt_rule_to_cmds($r, $chain, $ipversion, $cluster_conf, $fw_conf);
+       push @ipt_rule_cmds, ipt_rule_to_cmds($r, $chain, $ipversion, $cluster_conf, $fw_conf, $vmid);
     }
     foreach my $c (@ipt_rule_cmds) {
        ruleset_add_ipt_cmd($ruleset, $chain, $c);
@@ -2060,17 +2289,18 @@ sub ruleset_add_ipt_cmd {
 }
 
 sub ruleset_addrule {
-   my ($ruleset, $chain, $match, $action, $log, $logmsg, $vmid) = @_;
+    my ($ruleset, $chain, $match, $action, $log, $logmsg, $vmid) = @_;
 
-   die "no such chain '$chain'\n" if !$ruleset->{$chain};
+    die "no such chain '$chain'\n" if !$ruleset->{$chain};
 
-   if (defined($log) && $log) {
-       my $logaction = get_log_rule_base($chain, $vmid, $logmsg, $log);
+    if ($log) {
+       my $loglevel = $log_level_hash->{$log};
+       my $logaction = get_log_rule_base($chain, $vmid, $logmsg, $loglevel);
        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";
+    }
+    # 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 {
@@ -2087,10 +2317,14 @@ sub get_log_rule_base {
     $vmid = 0 if !defined($vmid);
     $msg = "" if !defined($msg);
 
+    my $rlimit = '';
+    if (defined($global_log_ratelimit)) {
+       $rlimit = "-m limit $global_log_ratelimit ";
+    }
+
     # Note: we use special format for prefix to pass further
     # info to log daemon (VMID, LOGLEVEL and CHAIN)
-
-    return "-j NFLOG --nflog-prefix \":$vmid:$loglevel:$chain: $msg\"";
+    return "${rlimit}-j NFLOG --nflog-prefix \":$vmid:$loglevel:$chain: $msg\"";
 }
 
 sub ruleset_add_chain_policy {
@@ -2110,7 +2344,7 @@ sub ruleset_add_chain_policy {
     } elsif ($policy eq 'REJECT') {
        ruleset_addrule($ruleset, $chain, "", "-j PVEFW-Reject");
 
-       ruleset_addrule($ruleset, $chain, "", "-g PVEFW-reject", $loglevel, "policy $policy:", $vmid);
+       ruleset_addrule($ruleset, $chain, "", "-g PVEFW-reject", $loglevel, "policy $policy: ", $vmid);
     } else {
        # should not happen
        die "internal error: unknown policy '$policy'";
@@ -2130,9 +2364,11 @@ sub ruleset_chain_add_ndp {
 }
 
 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");
+    if (!$allow_invalid) {
+       ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate INVALID", "-j DROP");
+    }
     ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate RELATED,ESTABLISHED", "-j $accept");
 }
 
@@ -2142,7 +2378,7 @@ 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_addrule($ruleset, "PVEFW-blacklist", "", "-j DROP", $loglevel, "DROP: ");
+           ruleset_addrule($ruleset, "PVEFW-blacklist", "", "-j DROP", $loglevel, "DROP: ", 0);
        }
        my $ipset_chain = compute_ipset_chain_name(0, 'blacklist', $ipversion);
        ruleset_addrule($ruleset, $chain, "-m set --match-set ${ipset_chain} src", "-j PVEFW-blacklist");
@@ -2168,7 +2404,7 @@ sub ruleset_create_vm_chain {
     if (!(defined($options->{dhcp}) && $options->{dhcp} == 0)) {
        if ($ipversion == 4) {
            if ($direction eq 'OUT') {
-               ruleset_generate_rule($ruleset, $chain, $ipversion, 
+               ruleset_generate_rule($ruleset, $chain, $ipversion,
                                      { action => 'PVEFW-SET-ACCEPT-MARK',
                                        proto => 'udp', sport => 68, dport => 67 });
            } else {
@@ -2228,7 +2464,7 @@ sub ruleset_add_group_rule {
 }
 
 sub ruleset_generate_vm_rules {
-    my ($ruleset, $rules, $cluster_conf, $vmfw_conf, $chain, $netid, $direction, $options, $ipversion) = @_;
+    my ($ruleset, $rules, $cluster_conf, $vmfw_conf, $chain, $netid, $direction, $options, $ipversion, $vmid) = @_;
 
     my $lc_direction = lc($direction);
 
@@ -2245,12 +2481,13 @@ sub ruleset_generate_vm_rules {
        } else {
            next if $rule->{type} ne $lc_direction;
            eval {
+               $rule->{logmsg} = "$rule->{action}: ";
                if ($direction eq 'OUT') {
                    rule_substitude_action($rule, { ACCEPT => "PVEFW-SET-ACCEPT-MARK", REJECT => "PVEFW-reject" });
-                   ruleset_generate_rule($ruleset, $chain, $ipversion, $rule, $cluster_conf, $vmfw_conf);
+                   ruleset_generate_rule($ruleset, $chain, $ipversion, $rule, $cluster_conf, $vmfw_conf, $vmid);
                } else {
                    rule_substitude_action($rule, { ACCEPT => $in_accept , REJECT => "PVEFW-reject" });
-                   ruleset_generate_rule($ruleset, $chain, $ipversion, $rule, $cluster_conf, $vmfw_conf);
+                   ruleset_generate_rule($ruleset, $chain, $ipversion, $rule, $cluster_conf, $vmfw_conf, $vmid);
                }
            };
            warn $@ if $@;
@@ -2304,14 +2541,15 @@ sub generate_tap_rules_direction {
     my $tapchain = "$iface-$direction";
 
     my $ipfilter_name = compute_ipfilter_ipset_name($netid);
-    my $ipfilter_ipset = compute_ipset_chain_name($vmid, $ipfilter_name, $ipversion)
+    my $ipfilter_ipset;
+    $ipfilter_ipset = compute_ipset_chain_name($vmid, $ipfilter_name, $ipversion)
        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);
-
     if ($options->{enable}) {
-       ruleset_generate_vm_rules($ruleset, $rules, $cluster_conf, $vmfw_conf, $tapchain, $netid, $direction, $options, $ipversion);
+       # create chain with mac and ip filter
+       ruleset_create_vm_chain($ruleset, $tapchain, $ipversion, $options, $macaddr, $ipfilter_ipset, $direction);
+
+       ruleset_generate_vm_rules($ruleset, $rules, $cluster_conf, $vmfw_conf, $tapchain, $netid, $direction, $options, $ipversion, $vmid);
 
        ruleset_generate_vm_ipsrules($ruleset, $options, $direction, $iface);
 
@@ -2321,7 +2559,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);
@@ -2343,13 +2581,32 @@ sub generate_tap_rules_direction {
 }
 
 sub enable_host_firewall {
-    my ($ruleset, $hostfw_conf, $cluster_conf, $ipversion) = @_;
+    my ($ruleset, $hostfw_conf, $cluster_conf, $ipversion, $corosync_conf) = @_;
 
     my $options = $hostfw_conf->{options};
     my $cluster_options = $cluster_conf->{options};
     my $rules = $hostfw_conf->{rules};
     my $cluster_rules = $cluster_conf->{rules};
 
+    # corosync preparation
+    my $corosync_rule = "-p udp --dport 5404:5405";
+    my $corosync_local_addresses = {};
+    my $multicast_enabled;
+    my $local_hostname = PVE::INotify::nodename();
+    if (defined($corosync_conf)) {
+       PVE::Corosync::for_all_corosync_addresses($corosync_conf, $ipversion, sub {
+           my ($node_name, $node_ip, $node_ipversion, $key) = @_;
+
+           if ($node_name eq $local_hostname) {
+               $corosync_local_addresses->{$key} = $node_ip;
+           }
+       });
+
+       # allow multicast only if enabled in config
+       my $corosync_transport = $corosync_conf->{main}->{totem}->{transport};
+       $multicast_enabled = defined($corosync_transport) && $corosync_transport eq 'udp';
+    }
+
     # host inbound firewall
     my $chain = "PVEFW-HOST-IN";
     ruleset_create_chain($ruleset, $chain);
@@ -2358,7 +2615,7 @@ sub enable_host_firewall {
 
     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);
 
@@ -2375,11 +2632,12 @@ sub enable_host_firewall {
        $rule->{iface_in} = $rule->{iface} if $rule->{iface};
 
        eval {
+           $rule->{logmsg} = "$rule->{action}: ";
            if ($rule->{type} eq 'group') {
                ruleset_add_group_rule($ruleset, $cluster_conf, $chain, $rule, 'IN', $accept_action, $ipversion);
            } elsif ($rule->{type} eq 'in') {
                rule_substitude_action($rule, { ACCEPT => $accept_action, REJECT => "PVEFW-reject" });
-               ruleset_generate_rule($ruleset, $chain, $ipversion, $rule, $cluster_conf, $hostfw_conf);
+               ruleset_generate_rule($ruleset, $chain, $ipversion, $rule, $cluster_conf, $hostfw_conf, 0);
            }
        };
        warn $@ if $@;
@@ -2393,15 +2651,22 @@ sub enable_host_firewall {
     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 60000:60050", "-j $accept_action");  # Migration
 
-    my $localnet = $cluster_conf->{aliases}->{local_network}->{cidr};
-    my $localnet_ver = $cluster_conf->{aliases}->{local_network}->{ipversion};
+    # corosync inbound rules
+    if (defined($corosync_conf)) {
+       ruleset_addrule($ruleset, $chain, "-m addrtype --dst-type MULTICAST $corosync_rule", "-j $accept_action")
+           if $multicast_enabled;
 
-    # corosync
-    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");
+       PVE::Corosync::for_all_corosync_addresses($corosync_conf, $ipversion, sub {
+           my ($node_name, $node_ip, $node_ipversion, $key) = @_;
+           my $destination = $corosync_local_addresses->{$key};
+
+           if ($node_name ne $local_hostname && defined($destination)) {
+               # accept only traffic on same ring
+               ruleset_addrule($ruleset, $chain, "-d $destination -s $node_ip $corosync_rule", "-j $accept_action");
+           }
+       });
     }
 
     # implement input policy
@@ -2416,7 +2681,7 @@ sub enable_host_firewall {
 
     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';
@@ -2431,11 +2696,12 @@ sub enable_host_firewall {
 
        $rule->{iface_out} = $rule->{iface} if $rule->{iface};
        eval {
+           $rule->{logmsg} = "$rule->{action}: ";
            if ($rule->{type} eq 'group') {
                ruleset_add_group_rule($ruleset, $cluster_conf, $chain, $rule, 'OUT', $accept_action, $ipversion);
            } elsif ($rule->{type} eq 'out') {
                rule_substitude_action($rule, { ACCEPT => $accept_action, REJECT => "PVEFW-reject" });
-               ruleset_generate_rule($ruleset, $chain, $ipversion, $rule, $cluster_conf, $hostfw_conf);
+               ruleset_generate_rule($ruleset, $chain, $ipversion, $rule, $cluster_conf, $hostfw_conf, 0);
            }
        };
        warn $@ if $@;
@@ -2443,15 +2709,30 @@ sub enable_host_firewall {
     }
 
     # allow standard traffic on cluster network
+    my $localnet = $cluster_conf->{aliases}->{local_network}->{cidr};
+    my $localnet_ver = $cluster_conf->{aliases}->{local_network}->{ipversion};
+
     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");
+    # corosync outbound rules
+    if (defined($corosync_conf)) {
+       ruleset_addrule($ruleset, $chain, "-m addrtype --dst-type MULTICAST $corosync_rule", "-j $accept_action")
+           if $multicast_enabled;
+
+       PVE::Corosync::for_all_corosync_addresses($corosync_conf, $ipversion, sub {
+           my ($node_name, $node_ip, $node_ipversion, $key) = @_;
+           my $source = $corosync_local_addresses->{$key};
+
+           if ($node_name ne $local_hostname && defined($source)) {
+               # accept only traffic on same ring
+               ruleset_addrule($ruleset, $chain, "-s $source -d $node_ip $corosync_rule", "-j $accept_action");
+           }
+       });
     }
 
     # implement output policy
@@ -2516,7 +2797,7 @@ sub get_mark_values {
 }
 
 sub parse_fw_rule {
-    my ($prefix, $line, $cluster_conf, $fw_conf, $rule_env, $verbose) = @_;
+    my ($prefix, $line, $cluster_conf, $fw_conf, $rule_env) = @_;
 
     my $orig_line = $line;
 
@@ -2552,28 +2833,36 @@ sub parse_fw_rule {
 
        last if $rule->{type} eq 'group';
 
-       if ($line =~ s/^-p (\S+)\s*//) {
+       if ($line =~ s/^(?:-p|--?proto) (\S+)\s*//) {
            $rule->{proto} = $1;
            next;
        }
 
-       if ($line =~ s/^-dport (\S+)\s*//) {
+       if ($line =~ s/^--?dport (\S+)\s*//) {
            $rule->{dport} = $1;
            next;
        }
 
-       if ($line =~ s/^-sport (\S+)\s*//) {
+       if ($line =~ s/^--?sport (\S+)\s*//) {
            $rule->{sport} = $1;
            next;
        }
-       if ($line =~ s/^-source (\S+)\s*//) {
+       if ($line =~ s/^--?source (\S+)\s*//) {
            $rule->{source} = $1;
            next;
        }
-       if ($line =~ s/^-dest (\S+)\s*//) {
+       if ($line =~ s/^--?dest (\S+)\s*//) {
            $rule->{dest} = $1;
            next;
        }
+       if ($line =~ s/^--?log (emerg|alert|crit|err|warning|notice|info|debug|nolog)\s*//) {
+           $rule->{log} = $1;
+           next;
+       }
+       if ($line =~ s/^--?icmp-type (\S+)\s*//) {
+           $rule->{'icmp-type'} = $1;
+           next;
+       }
 
        last;
     }
@@ -2640,13 +2929,17 @@ sub parse_hostfw_option {
 
     my $loglevels = "emerg|alert|crit|err|warning|notice|info|debug|nolog";
 
-    if ($line =~ m/^(enable|nosmurfs|tcpflags|ndp):\s*(0|1)\s*$/i) {
+    if ($line =~ m/^(enable|nosmurfs|tcpflags|ndp|log_nf_conntrack|nf_conntrack_allow_invalid|protection_synflood):\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) {
        $opt = lc($1);
        $value = $2 ? lc($3) : '';
-    } elsif ($line =~ m/^(nf_conntrack_max|nf_conntrack_tcp_timeout_established):\s*(\d+)\s*$/i) {
+    } elsif ($line =~ m/^(nf_conntrack_helpers):\s*(((\S+)[,]?)+)\s*$/i) {
+       $opt = lc($1);
+       $value = lc($2);
+       pve_fw_verify_conntrack_helper($value);
+    } elsif ($line =~ m/^(nf_conntrack_max|nf_conntrack_tcp_timeout_established|nf_conntrack_tcp_timeout_syn_recv|protection_synflood_rate|protection_synflood_burst|protection_limit):\s*(\d+)\s*$/i) {
        $opt = lc($1);
        $value = int($2);
     } else {
@@ -2667,9 +2960,15 @@ sub parse_clusterfw_option {
        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) {
        $opt = lc($1);
        $value = uc($3);
+    } elsif ($line =~ m/^(log_ratelimit):\s*(\S+)\s*$/) {
+       $opt = lc($1);
+       $value = $2;
     } else {
        die "can't parse option '$line'\n"
     }
@@ -2678,11 +2977,26 @@ sub parse_clusterfw_option {
 }
 
 sub resolve_alias {
-    my ($clusterfw_conf, $fw_conf, $cidr) = @_;
+    my ($clusterfw_conf, $fw_conf, $cidr, $scope) = @_;
+
+    # When we're on the cluster level, the cluster config only gets
+    # saved into fw_conf, so we need some extra handling here (to
+    # stay consistent)
+    my ($cluster_config, $local_config);
+    if (!$clusterfw_conf) {
+       ($cluster_config, $local_config) = ($fw_conf, undef);
+    } else {
+       ($cluster_config, $local_config) = ($clusterfw_conf, $fw_conf);
+    }
 
     my $alias = lc($cidr);
-    my $e = $fw_conf ? $fw_conf->{aliases}->{$alias} : undef;
-    $e = $clusterfw_conf->{aliases}->{$alias} if !$e && $clusterfw_conf;
+    my $e;
+    if ($scope ne 'dc/' && $local_config) {
+       $e = $local_config->{aliases}->{$alias};
+    }
+    if ($scope ne 'guest/' && !$e && $cluster_config) {
+       $e = $cluster_config->{aliases}->{$alias};
+    }
 
     die "no such alias '$cidr'\n" if !$e;;
 
@@ -2693,7 +3007,7 @@ sub parse_ip_or_cidr {
     my ($cidr) = @_;
 
     my $ipversion;
-    
+
     if ($cidr =~ m!^(?:$IPV6RE)(/(\d+))?$!) {
        $cidr =~ s|/128$||;
        $ipversion = 6;
@@ -2711,7 +3025,7 @@ sub parse_alias {
     my ($line) = @_;
 
     # we can add single line comments to the end of the line
-    my $comment = decode('utf8', $1) if $line =~ s/\s*#\s*(.*?)\s*$//;
+    my $comment = $line =~ s/\s*#\s*(.*?)\s*$// ? decode('utf8', $1) : undef;
 
     if ($line =~ m/^(\S+)\s(\S+)$/) {
        my ($name, $cidr) = ($1, $2);
@@ -2732,20 +3046,31 @@ sub parse_alias {
 }
 
 sub generic_fw_config_parser {
-    my ($filename, $fh, $verbose, $cluster_conf, $empty_conf, $rule_env) = @_;
+    my ($filename, $cluster_conf, $empty_conf, $rule_env) = @_;
 
     my $section;
     my $group;
 
     my $res = $empty_conf;
 
-    while (defined(my $line = <$fh>)) {
+    my $raw;
+    if ($filename =~ m!^/etc/pve/(.*)$!) {
+       $raw = PVE::Cluster::get_config($1);
+    } else {
+       $raw = eval { PVE::Tools::file_get_contents($filename) }; # ignore errors
+    }
+    return {} if !$raw;
+
+    my $curr_group_keys = {};
+
+    my $linenr = 0;
+    while ($raw =~ /^\h*(.*?)\h*$/gm) {
+       my $line = $1;
+       $linenr++;
        next if $line =~ m/^#/;
        next if $line =~ m/^\s*$/;
-
        chomp $line;
 
-       my $linenr = $fh->input_line_number();
        my $prefix = "$filename (line $linenr)";
 
        if ($empty_conf->{options} && ($line =~ m/^\[options\]$/i)) {
@@ -2771,7 +3096,7 @@ sub generic_fw_config_parser {
                warn "$prefix: $err";
                next;
            }
-           
+
            $res->{$section}->{$group} = [];
            $res->{group_comments}->{$group} =  decode('utf8', $comment)
                if $comment;
@@ -2787,7 +3112,7 @@ sub generic_fw_config_parser {
            $section = 'ipset';
            $group = lc($1);
            my $comment = $2;
-           eval {      
+           eval {
                die "ipset name too long\n" if length($group) > $max_ipset_name_length;
                die "invalid ipset name '$group'\n" if $group !~ m/^${ipset_name_pattern}$/;
            };
@@ -2798,6 +3123,8 @@ sub generic_fw_config_parser {
            }
 
            $res->{$section}->{$group} = [];
+           $curr_group_keys = {};
+
            $res->{ipset_comments}->{$group} = decode('utf8', $comment)
                if $comment;
            next;
@@ -2829,7 +3156,7 @@ sub generic_fw_config_parser {
            warn "$prefix: $@" if $@;
        } elsif ($section eq 'rules') {
            my $rule;
-           eval { $rule = parse_fw_rule($prefix, $line, $cluster_conf, $res, $rule_env, $verbose); };
+           eval { $rule = parse_fw_rule($prefix, $line, $cluster_conf, $res, $rule_env); };
            if (my $err = $@) {
                warn "$prefix: $err";
                next;
@@ -2837,7 +3164,7 @@ sub generic_fw_config_parser {
            push @{$res->{$section}}, $rule;
        } elsif ($section eq 'groups') {
            my $rule;
-           eval { $rule = parse_fw_rule($prefix, $line, $cluster_conf, undef, 'group', $verbose); };
+           eval { $rule = parse_fw_rule($prefix, $line, $cluster_conf, undef, 'group'); };
            if (my $err = $@) {
                warn "$prefix: $err";
                next;
@@ -2845,7 +3172,7 @@ sub generic_fw_config_parser {
            push @{$res->{$section}->{$group}}, $rule;
        } elsif ($section eq 'ipset') {
            # we can add single line comments to the end of the rule
-           my $comment = decode('utf8', $1) if $line =~ s/#\s*(.*?)\s*$//;
+           my $comment = $line =~ s/#\s*(.*?)\s*$// ? decode('utf8', $1) : undef;
 
            $line =~ m/^(\!)?\s*(\S+)\s*$/;
            my $nomatch = $1;
@@ -2856,12 +3183,16 @@ sub generic_fw_config_parser {
                $errors->{nomatch} = "nomatch not supported by kernel";
            }
 
-           eval { 
-               if ($cidr =~ m/^${ip_alias_pattern}$/) {
-                   resolve_alias($cluster_conf, $res, $cidr); # make sure alias exists
+           eval {
+               if ($cidr =~ m@^(dc/|guest/)?(${ip_alias_pattern}$)@) {
+                   my $scope = $1 // "";
+                   my $alias = $2;
+                   resolve_alias($cluster_conf, $res, $alias, $scope); # make sure alias exists
                } else {
                    $cidr = parse_ip_or_cidr($cidr);
                }
+               die "duplicate ipset entry for '$cidr'\n"
+                   if defined($curr_group_keys->{$cidr});
            };
            if (my $err = $@) {
                chomp $err;
@@ -2885,6 +3216,7 @@ sub generic_fw_config_parser {
            }
 
            push @{$res->{$section}->{$group}}, $entry;
+           $curr_group_keys->{$cidr} = 1;
        } else {
            warn "$prefix: skip line - unknown section\n";
            next;
@@ -2894,47 +3226,8 @@ sub generic_fw_config_parser {
     return $res;
 }
 
-sub parse_hostfw_config {
-    my ($filename, $fh, $cluster_conf, $verbose) = @_;
-
-    my $empty_conf = { rules => [], options => {}};
-
-    return generic_fw_config_parser($filename, $fh, $verbose, $cluster_conf, $empty_conf, 'host');
-}
-
-sub parse_vmfw_config {
-    my ($filename, $fh, $cluster_conf, $rule_env, $verbose) = @_;
-
-    my $empty_conf = {
-       rules => [],
-       options => {},
-       aliases => {},
-       ipset => {} ,
-       ipset_comments => {},
-    };
-
-    return generic_fw_config_parser($filename, $fh, $verbose, $cluster_conf, $empty_conf, $rule_env);
-}
-
-sub parse_clusterfw_config {
-    my ($filename, $fh, $verbose) = @_;
-
-    my $section;
-    my $group;
-
-    my $empty_conf = {
-       rules => [],
-       options => {},
-       aliases => {},
-       groups => {},
-       group_comments => {},
-       ipset => {} ,
-       ipset_comments => {},
-    };
-
-    return generic_fw_config_parser($filename, $fh, $verbose, $empty_conf, $empty_conf, 'cluster');
-}
-
+# this is only used to prevent concurrent runs of rule compilation/application
+# see lock_*_conf for cfs locks protectiong config modification
 sub run_locked {
     my ($code, @param) = @_;
 
@@ -2971,30 +3264,39 @@ sub read_local_vm_config {
                }
            }
         } 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;
-                }
-            }
-        }
+           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;
 };
 
-sub load_vmfw_conf {
-    my ($cluster_conf, $rule_env, $vmid, $dir, $verbose) = @_;
+# FIXME: move use sites over to moved helper and break older packages, then remove this here
+sub lock_vmfw_conf {
+    return PVE::Firewall::Helpers::lock_vmfw_conf(@_);
+}
 
-    my $vmfw_conf = {};
+sub load_vmfw_conf {
+    my ($cluster_conf, $rule_env, $vmid, $dir) = @_;
 
     $dir = $pvefw_conf_dir if !defined($dir);
-
     my $filename = "$dir/$vmid.fw";
-    if (my $fh = IO::File->new($filename, O_RDONLY)) {
-       $vmfw_conf = parse_vmfw_config($filename, $fh, $cluster_conf, $rule_env, $verbose);
-       $vmfw_conf->{vmid} = $vmid;
-    }
+
+    my $empty_conf = {
+       rules => [],
+       options => {},
+       aliases => {},
+       ipset => {} ,
+       ipset_comments => {},
+    };
+
+    my $vmfw_conf = generic_fw_config_parser($filename, $cluster_conf, $empty_conf, $rule_env);
+    $vmfw_conf->{vmid} = $vmid;
 
     return $vmfw_conf;
 }
@@ -3023,6 +3325,8 @@ my $format_rules = sub {
                $raw .= " -p $rule->{proto}" if $rule->{proto};
                $raw .= " -dport $rule->{dport}" if $rule->{dport};
                $raw .= " -sport $rule->{sport}" if $rule->{sport};
+               $raw .= " -log $rule->{log}" if $rule->{log};
+               $raw .= " -icmp-type $rule->{'icmp-type'}" if defined($rule->{'icmp-type'}) && $rule->{'icmp-type'} ne '';
            }
 
            $raw .= " # " . encode('utf8', $rule->{comment})
@@ -3070,7 +3374,7 @@ my $format_aliases = sub {
 
 my $format_ipsets = sub {
     my ($fw_conf) = @_;
-    
+
     my $raw = '';
 
     foreach my $ipset (sort keys %{$fw_conf->{ipset}}) {
@@ -3084,7 +3388,13 @@ my $format_ipsets = sub {
 
        my $nethash = {};
        foreach my $entry (@$options) {
-           $nethash->{$entry->{cidr}} = $entry;
+           my $cidr = $entry->{cidr};
+           if (defined($nethash->{$cidr})) {
+               warn "ignoring duplicate ipset entry '$cidr'\n";
+               next;
+           }
+
+           $nethash->{$cidr} = $entry;
        }
 
        foreach my $cidr (sort keys %$nethash) {
@@ -3131,42 +3441,29 @@ sub save_vmfw_conf {
     }
 }
 
+# FIXME: remove with 8.0 and break older qemu-server/pve-container
 sub remove_vmfw_conf {
-    my ($vmid) = @_;
-
-    my $vmfw_conffile = "$pvefw_conf_dir/$vmid.fw";
-
-    unlink $vmfw_conffile;
+    return PVE::Firewall::Helpers::remove_vmfw_conf(@_);
 }
 
+# FIXME: remove with 8.0 and break older qemu-server/pve-container
 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);
-    }
+    return PVE::Firewall::Helpers::clone_vmfw_conf(@_);
 }
 
 sub read_vm_firewall_configs {
-    my ($cluster_conf, $vmdata, $dir, $verbose) = @_;
+    my ($cluster_conf, $vmdata, $dir) = @_;
 
     my $vmfw_configs = {};
 
     foreach my $vmid (keys %{$vmdata->{qemu}}) {
-       my $vmfw_conf = load_vmfw_conf($cluster_conf, 'vm', $vmid, $dir, $verbose);
-       next if !$vmfw_conf->{options}; # skip if file does not exists
+       my $vmfw_conf = load_vmfw_conf($cluster_conf, 'vm', $vmid, $dir);
+       next if !$vmfw_conf->{options}; # skip if file does not exist
        $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
+        my $vmfw_conf = load_vmfw_conf($cluster_conf, 'ct', $vmid, $dir);
+        next if !$vmfw_conf->{options}; # skip if file does not exist
         $vmfw_configs->{$vmid} = $vmfw_conf;
     }
 
@@ -3181,9 +3478,7 @@ sub get_option_log_level {
 
     return undef if $v eq '' || $v eq 'nolog';
 
-    $v = $log_level_hash->{$v} if defined($log_level_hash->{$v});
-
-    return $v if ($v >= 0) && ($v <= 7);
+    return $v if defined($log_level_hash->{$v});
 
     warn "unknown log level ($k = '$v')\n";
 
@@ -3216,7 +3511,7 @@ sub generate_std_chains {
        ruleset_create_chain($ruleset, $chain);
        foreach my $rule (@{$std_chains->{$chain}}) {
            if (ref($rule)) {
-               ruleset_generate_rule($ruleset, $chain, $ipversion, $rule);
+               ruleset_generate_rule($ruleset, $chain, $ipversion, $rule, 0);
            } else {
                die "rule $rule as string - should not happen";
            }
@@ -3243,8 +3538,10 @@ sub generate_ipset_chains {
            next if $entry->{errors}; # skip entries with errors
            eval {
                my ($cidr, $ver);
-               if ($entry->{cidr} =~ m/^${ip_alias_pattern}$/) {
-                   ($cidr, $ver) = resolve_alias($clusterfw_conf, $fw_conf, $entry->{cidr});
+               if ($entry->{cidr} =~ m@^(dc/|guest/)?(${ip_alias_pattern})$@) {
+                   my $scope = $1 // "";
+                   my $alias = $2;
+                   ($cidr, $ver) = resolve_alias($clusterfw_conf, $fw_conf, $alias, $scope);
                } else {
                    ($cidr, $ver) = parse_ip_or_cidr($entry->{cidr});
                }
@@ -3276,9 +3573,13 @@ sub generate_ipset_chains {
                $hashsize = round_powerof2($hashsize);
            }
 
+           my $bucketsize = 12; # lower than the default of 14, faster but slightly more memory use
+
            my $family = $ipversion == "6" ? "inet6" : "inet";
 
-           $ipset_ruleset->{$name} = ["create $name hash:net family $family hashsize $hashsize maxelem $hashsize"];
+           $ipset_ruleset->{$name} = [
+               "create $name hash:net family $family hashsize $hashsize maxelem $hashsize bucketsize $bucketsize"
+           ];
 
            foreach my $cidr (sort keys %$data) {
                my $entry = $data->{$cidr};
@@ -3306,15 +3607,52 @@ sub round_powerof2 {
     return ++$int;
 }
 
+my $set_global_log_ratelimit = sub {
+    my $cluster_opts = shift;
+
+    $global_log_ratelimit = '--limit 1/sec';
+    if (defined(my $log_rlimit = $cluster_opts->{log_ratelimit})) {
+       my $ll_format = $cluster_option_properties->{log_ratelimit}->{format};
+       my $limit = PVE::JSONSchema::parse_property_string($ll_format, $log_rlimit);
+
+       if ($limit->{enable}) {
+           if (my $rate = $limit->{rate}) {
+               $global_log_ratelimit = "--limit $rate";
+           }
+           if (my $burst = $limit->{burst}) {
+               $global_log_ratelimit .= " --limit-burst $burst";
+           }
+       } else {
+           $global_log_ratelimit = undef;
+       }
+    }
+};
+
+sub lock_clusterfw_conf {
+    my ($timeout, $code, @param) = @_;
+
+    my $res = PVE::Cluster::cfs_lock_firewall("cluster", $timeout, $code, @param);
+    die $@ if $@;
+
+    return $res;
+}
+
 sub load_clusterfw_conf {
-    my ($filename, $verbose) = @_;
+    my ($filename) = @_;
 
     $filename = $clusterfw_conf_filename if !defined($filename);
+    my $empty_conf = {
+       rules => [],
+       options => {},
+       aliases => {},
+       groups => {},
+       group_comments => {},
+       ipset => {} ,
+       ipset_comments => {},
+    };
 
-    my $cluster_conf = {};
-    if (my $fh = IO::File->new($filename, O_RDONLY)) {
-       $cluster_conf = parse_clusterfw_config($filename, $fh, $verbose);
-    }
+    my $cluster_conf = generic_fw_config_parser($filename, $empty_conf, $empty_conf, 'cluster');
+    $set_global_log_ratelimit->($cluster_conf->{options});
 
     return $cluster_conf;
 }
@@ -3331,7 +3669,7 @@ sub save_clusterfw_conf {
     $raw .= &$format_aliases($aliases) if $aliases && scalar(keys %$aliases);
 
     $raw .= &$format_ipsets($cluster_conf) if $cluster_conf->{ipset};
+
     my $rules = $cluster_conf->{rules};
     if ($rules && scalar(@$rules)) {
        $raw .= "[RULES]\n\n";
@@ -3362,20 +3700,30 @@ sub save_clusterfw_conf {
     }
 }
 
+sub lock_hostfw_conf : prototype($$$@) {
+    my ($node, $timeout, $code, @param) = @_;
+
+    $node = $nodename if !defined($node);
+
+    my $res = PVE::Cluster::cfs_lock_firewall("host-$node", $timeout, $code, @param);
+    die $@ if $@;
+
+    return $res;
+}
+
 sub load_hostfw_conf {
-    my ($cluster_conf, $filename, $verbose) = @_;
+    my ($cluster_conf, $filename) = @_;
 
     $filename = $hostfw_conf_filename if !defined($filename);
 
-    my $hostfw_conf = {};
-    if (my $fh = IO::File->new($filename, O_RDONLY)) {
-       $hostfw_conf = parse_hostfw_config($filename, $fh, $cluster_conf, $verbose);
-    }
-    return $hostfw_conf;
+    my $empty_conf = { rules => [], options => {}};
+    return generic_fw_config_parser($filename, $cluster_conf, $empty_conf, 'host');
 }
 
 sub save_hostfw_conf {
-    my ($hostfw_conf) = @_;
+    my ($hostfw_conf, $filename) = @_;
+
+    $filename = $hostfw_conf_filename if !defined($filename);
 
     my $raw = '';
 
@@ -3390,14 +3738,14 @@ sub save_hostfw_conf {
     }
 
     if ($raw) {
-       PVE::Tools::file_set_contents($hostfw_conf_filename, $raw);
+       PVE::Tools::file_set_contents($filename, $raw);
     } else {
-       unlink $hostfw_conf_filename;
+       unlink $filename;
     }
 }
 
 sub compile {
-    my ($cluster_conf, $hostfw_conf, $vmdata, $verbose) = @_;
+    my ($cluster_conf, $hostfw_conf, $vmdata, $corosync_conf) = @_;
 
     my $vmfw_configs;
 
@@ -3407,22 +3755,26 @@ sub compile {
     if ($vmdata) { # test mode
        my $testdir = $vmdata->{testdir} || die "no test directory specified";
        my $filename = "$testdir/cluster.fw";
-       $cluster_conf = load_clusterfw_conf($filename, $verbose);
+       $cluster_conf = load_clusterfw_conf($filename);
 
        $filename = "$testdir/host.fw";
-       $hostfw_conf = load_hostfw_conf($cluster_conf, $filename, $verbose);
+       $hostfw_conf = load_hostfw_conf($cluster_conf, $filename);
 
-       $vmfw_configs = read_vm_firewall_configs($cluster_conf, $vmdata, $testdir, $verbose);
+       $vmfw_configs = read_vm_firewall_configs($cluster_conf, $vmdata, $testdir);
     } else { # normal operation
-       $cluster_conf = load_clusterfw_conf(undef, $verbose) if !$cluster_conf;
+       $cluster_conf = load_clusterfw_conf(undef) if !$cluster_conf;
+
+       $hostfw_conf = load_hostfw_conf($cluster_conf, undef) if !$hostfw_conf;
 
-       $hostfw_conf = load_hostfw_conf($cluster_conf, undef, $verbose) if !$hostfw_conf;
+       # cfs_update is handled by daemon or API
+       $corosync_conf = PVE::Cluster::cfs_read_file("corosync.conf")
+           if !defined($corosync_conf) && PVE::Corosync::check_conf_exists(1);
 
        $vmdata = read_local_vm_config();
-       $vmfw_configs = read_vm_firewall_configs($cluster_conf, $vmdata, undef, $verbose);
+       $vmfw_configs = read_vm_firewall_configs($cluster_conf, $vmdata, undef);
     }
 
-    return ({},{},{}) if !$cluster_conf->{options}->{enable};
+    return ({},{},{},{}) if !$cluster_conf->{options}->{enable};
 
     my $localnet;
     if ($cluster_conf->{aliases}->{local_network}) {
@@ -3431,22 +3783,57 @@ sub compile {
        my $localnet_ver;
        ($localnet, $localnet_ver) = parse_ip_or_cidr(local_network() || '127.0.0.0/8');
 
-       $cluster_conf->{aliases}->{local_network} = { 
+       $cluster_conf->{aliases}->{local_network} = {
            name => 'local_network', cidr => $localnet, ipversion => $localnet_ver };
     }
 
     push @{$cluster_conf->{ipset}->{management}}, { cidr => $localnet };
 
-    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 $ruleset = {};
+    my $rulesetv6 = {};
+    $ruleset->{filter} = compile_iptables_filter($cluster_conf, $hostfw_conf, $vmfw_configs, $vmdata, $corosync_conf, 4);
+    $ruleset->{raw} = compile_iptables_raw($cluster_conf, $hostfw_conf, $vmfw_configs, $vmdata, $corosync_conf, 4);
+    $rulesetv6->{filter} = compile_iptables_filter($cluster_conf, $hostfw_conf, $vmfw_configs, $vmdata, $corosync_conf, 6);
+    $rulesetv6->{raw} = compile_iptables_raw($cluster_conf, $hostfw_conf, $vmfw_configs, $vmdata, $corosync_conf, 6);
+    my $ebtables_ruleset = compile_ebtables_filter($cluster_conf, $hostfw_conf, $vmfw_configs, $vmdata);
     my $ipset_ruleset = compile_ipsets($cluster_conf, $vmfw_configs, $vmdata);
 
     return ($ruleset, $ipset_ruleset, $rulesetv6, $ebtables_ruleset);
 }
 
+sub compile_iptables_raw {
+    my ($cluster_conf, $hostfw_conf, $vmfw_configs, $vmdata, $corosync_conf, $ipversion) = @_;
+
+    my $ruleset = {};
+
+    my $hostfw_options = $hostfw_conf->{options} || {};
+    my $protection_synflood = $hostfw_options->{protection_synflood} || 0;
+    my $conntrack_helpers = $hostfw_options->{nf_conntrack_helpers} || '';
+
+    ruleset_create_chain($ruleset, "PVEFW-PREROUTING") if $protection_synflood != 0 || $conntrack_helpers ne '';
+
+    if($protection_synflood) {
+
+       my $protection_synflood_rate = $hostfw_options->{protection_synflood_rate} ? $hostfw_options->{protection_synflood_rate} : 200;
+       my $protection_synflood_burst = $hostfw_options->{protection_synflood_burst} ? $hostfw_options->{protection_synflood_burst} : 1000;
+       my $protection_synflood_limit = $hostfw_options->{protection_synflood_limit} ? $hostfw_options->{protection_synflood_limit} : 3000;
+       my $protection_synflood_expire = $hostfw_options->{nf_conntrack_tcp_timeout_syn_recv} ? $hostfw_options->{nf_conntrack_tcp_timeout_syn_recv} : 60;
+       $protection_synflood_expire = $protection_synflood_expire * 1000;
+       my $protection_synflood_mask = $ipversion == 4 ? 32 : 64;
+
+       ruleset_addrule($ruleset, "PVEFW-PREROUTING", "-p tcp -m tcp --tcp-flags FIN,SYN,RST,ACK SYN -m hashlimit --hashlimit-above $protection_synflood_rate/sec --hashlimit-burst $protection_synflood_burst --hashlimit-mode srcip --hashlimit-name syn --hashlimit-htable-size 2097152 --hashlimit-srcmask $protection_synflood_mask --hashlimit-htable-expire $protection_synflood_expire", "-j DROP");
+    }
+
+    foreach my $conntrack_helper (split(/,/, $conntrack_helpers)) {
+       my $helper = $pve_fw_helpers->{$conntrack_helper};
+       ruleset_addrule($ruleset, "PVEFW-PREROUTING", "-p $helper->{proto} -m $helper->{proto} --dport $helper->{dport} -j CT", "--helper $conntrack_helper") if $helper && $helper->{"v$ipversion"};
+    }
+
+    return $ruleset;
+}
+
 sub compile_iptables_filter {
-    my ($cluster_conf, $hostfw_conf, $vmfw_configs, $vmdata, $ipversion, $verbose) = @_;
+    my ($cluster_conf, $hostfw_conf, $vmfw_configs, $vmdata, $corosync_conf, $ipversion) = @_;
 
     my $ruleset = {};
 
@@ -3460,7 +3847,8 @@ 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");
+    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);
@@ -3475,7 +3863,7 @@ sub compile_iptables_filter {
     my $hostfw_enable = !(defined($hostfw_options->{enable}) && ($hostfw_options->{enable} == 0));
 
     if ($hostfw_enable) {
-       eval { enable_host_firewall($ruleset, $hostfw_conf, $cluster_conf, $ipversion); };
+       eval { enable_host_firewall($ruleset, $hostfw_conf, $cluster_conf, $ipversion, $corosync_conf); };
        warn $@ if $@; # just to be sure - should not happen
     }
 
@@ -3484,19 +3872,19 @@ sub compile_iptables_filter {
        eval {
            my $conf = $vmdata->{qemu}->{$vmid};
            my $vmfw_conf = $vmfw_configs->{$vmid};
-           return if !$vmfw_conf;
+           return if !$vmfw_conf || !$vmfw_conf->{options}->{enable};
 
            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 $iface = "tap${vmid}i$1";
                my $macaddr = $net->{macaddr};
                generate_tap_rules_direction($ruleset, $cluster_conf, $iface, $netid, $macaddr,
-                                            $vmfw_conf, $vmid, 'IN', $ipversion);
+                                            $vmfw_conf, $vmid, 'IN', $ipversion);
                generate_tap_rules_direction($ruleset, $cluster_conf, $iface, $netid, $macaddr,
-                                            $vmfw_conf, $vmid, 'OUT', $ipversion);
+                                            $vmfw_conf, $vmid, 'OUT', $ipversion);
            }
        };
        warn $@ if $@; # just to be sure - should not happen
@@ -3504,29 +3892,28 @@ sub compile_iptables_filter {
 
     # 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
+       eval {
+           my $conf = $vmdata->{lxc}->{$vmid};
+           my $vmfw_conf = $vmfw_configs->{$vmid};
+           return if !$vmfw_conf || !$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")){
+    if (ruleset_chain_exist($ruleset, "PVEFW-IPS")){
        ruleset_insertrule($ruleset, "PVEFW-FORWARD", "-m conntrack --ctstate RELATED,ESTABLISHED", "-j PVEFW-IPS");
     }
 
@@ -3555,7 +3942,7 @@ sub compile_ipsets {
        my $localnet_ver;
        ($localnet, $localnet_ver) = parse_ip_or_cidr(local_network() || '127.0.0.0/8');
 
-       $cluster_conf->{aliases}->{local_network} = { 
+       $cluster_conf->{aliases}->{local_network} = {
            name => 'local_network', cidr => $localnet, ipversion => $localnet_ver };
     }
 
@@ -3572,7 +3959,7 @@ sub compile_ipsets {
            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
+           # is no 'ipfilter-netX' ipset defined gets an implicit empty default
            # ipset.
            # The reason is that ipfilter ipsets are always filled with standard
            # IPv6 link-local filters.
@@ -3611,7 +3998,7 @@ sub compile_ipsets {
            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
+           # is no 'ipfilter-netX' ipset defined 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
@@ -3655,15 +4042,16 @@ sub compile_ipsets {
 }
 
 sub compile_ebtables_filter {
-    my ($cluster_conf, $hostfw_conf, $vmfw_configs, $vmdata, $verbose) = @_;
+    my ($cluster_conf, $hostfw_conf, $vmfw_configs, $vmdata) = @_;
 
-    return ({}, {}) if !$cluster_conf->{options}->{enable};
+    if (!($cluster_conf->{options}->{ebtables} // 1)) {
+       return {};
+    }
 
     my $ruleset = {};
 
     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');
@@ -3671,41 +4059,65 @@ sub compile_ebtables_filter {
     ruleset_addrule($ruleset, 'PVEFW-FORWARD', '-o fwln+', '-j PVEFW-FWBR-OUT');
 
     # 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;
+           return if !$vmfw_conf || !$vmfw_conf->{options}->{enable};
+           my $ipsets = $vmfw_conf->{ipset};
 
-           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};
                my $iface = "tap${vmid}i$1";
                my $macaddr = $net->{macaddr};
-
-               generate_tap_layer2filter($ruleset, $iface, $macaddr, $vmfw_conf, $vmid);
-
+               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 (keys %{$vmdata->{lxc}}) {
+    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};
 
-           foreach my $netid (keys %$conf) {
+           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_layer2filter($ruleset, $iface, $macaddr, $vmfw_conf, $vmid);
+               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);
+                   }
+               }
+               if (defined(my $ip = $net->{ip}) && $vmfw_conf->{options}->{ipfilter}) {
+                   # ebtables changes this to a .0/MASK network but we just
+                   # want the address here, no network - see #2193
+                   $ip =~ s|/(\d+)$||;
+                   if ($ip ne 'dhcp') {
+                       push @$arpfilter, $ip;
+                   }
+               }
+               generate_tap_layer2filter($ruleset, $iface, $macaddr, $vmfw_conf, $vmid, $arpfilter);
            }
        };
        warn $@ if $@; # just to be sure - should not happen
@@ -3715,7 +4127,7 @@ sub compile_ebtables_filter {
 }
 
 sub generate_tap_layer2filter {
-    my ($ruleset, $iface, $macaddr, $vmfw_conf, $vmid) = @_;
+    my ($ruleset, $iface, $macaddr, $vmfw_conf, $vmid, $arpfilter) = @_;
     my $options = $vmfw_conf->{options};
 
     my $tapchain = $iface."-OUT";
@@ -3727,32 +4139,57 @@ sub generate_tap_layer2filter {
     ruleset_create_chain($ruleset, $tapchain);
 
     if (defined($macaddr) && !(defined($options->{macfilter}) && $options->{macfilter} == 0)) {
-           ruleset_addrule($ruleset, $tapchain, "-s ! $macaddr", '-j DROP');
+       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, $tapchain, "-p $proto", '-j ACCEPT');
+           ruleset_addrule($ruleset, $protochain, "-p $proto", '-j RETURN');
        }
-       ruleset_addrule($ruleset, $tapchain, '', "-j DROP");
-    } else {
-       ruleset_addrule($ruleset, $tapchain, '', '-j ACCEPT');
+       ruleset_addrule($ruleset, $protochain, '', '-j DROP');
     }
 
+    ruleset_addrule($ruleset, $tapchain, '', '-j ACCEPT');
+
     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, $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 {
@@ -3762,19 +4199,27 @@ 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};
+           $statushash->{$chain}->{policy} = $active_chains->{$chain}->{policy};
+           $sig = $sig->{sig};
        }
+       $statushash->{$chain}->{action} = $action;
+       $statushash->{$chain}->{sig} = $sig;
+       print "$action $chain ($sig)\n" if $verbose;
     }
 
     return $statushash;
@@ -3788,12 +4233,14 @@ sub print_sig_rule {
 }
 
 sub get_ruleset_cmdlist {
-    my ($ruleset, $verbose, $iptablescmd) = @_;
+    my ($ruleset, $iptablescmd, $table) = @_;
 
-    my $cmdlist = "*filter\n"; # we pass this to iptables-restore;
+    $table = 'filter' if !$table;
 
-    my ($active_chains, $hooks) = iptables_get_chains($iptablescmd);
-    my $statushash = get_ruleset_status($ruleset, $active_chains, \&iptables_chain_digest, $verbose);
+    my $cmdlist = "*$table\n"; # we pass this to iptables-restore;
+
+    my ($active_chains, $hooks) = iptables_get_chains($iptablescmd, $table);
+    my $statushash = get_ruleset_status($ruleset, $active_chains, \&iptables_chain_digest);
 
     # create missing chains first
     foreach my $chain (sort keys %$ruleset) {
@@ -3804,7 +4251,7 @@ sub get_ruleset_cmdlist {
        $cmdlist .= ":$chain - [0:0]\n";
     }
 
-    foreach my $h (qw(INPUT OUTPUT FORWARD)) {
+    foreach my $h (qw(INPUT OUTPUT FORWARD PREROUTING)) {
        my $chain = "PVEFW-$h";
        if ($ruleset->{$chain} && !$hooks->{$h}) {
            $cmdlist .= "-A $h -j $chain\n";
@@ -3839,58 +4286,67 @@ sub get_ruleset_cmdlist {
        next if $chain eq 'PVEFW-INPUT';
        next if $chain eq 'PVEFW-OUTPUT';
        next if $chain eq 'PVEFW-FORWARD';
+       next if $chain eq 'PVEFW-PREROUTING';
        $cmdlist .= "-X $chain\n";
     }
 
-    my $changes = $cmdlist ne "*filter\n" ? 1 : 0;
+    my $changes = $cmdlist ne "*$table\n" ? 1 : 0;
 
     $cmdlist .= "COMMIT\n";
 
     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 ($ruleset) = @_;
 
     my $changes = 0;
     my $cmdlist = "*filter\n";
 
-    my ($active_chains, $hooks) = ebtables_get_chains();
-    my $statushash = get_ruleset_status($ruleset, $active_chains, \&iptables_chain_digest, $verbose);
-
-    # create chains first
-    foreach my $chain (sort keys %$ruleset) {
-       my $stat = $statushash->{$chain};
-       die "internal error" if !$stat;
-       $cmdlist .= ":$chain ACCEPT\n";
-    }
+    my $active_chains = ebtables_get_chains();
+    my $statushash = get_ruleset_status($ruleset, $active_chains,
+                                       \&iptables_chain_digest,
+                                       $pve_ebtables_chainname_regex);
 
-    if ($ruleset->{'PVEFW-FORWARD'}) {
-       $cmdlist .= "-A FORWARD -j PVEFW-FORWARD\n";
+    # 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');
+       my $policy = $statushash->{$chain}->{policy} // 'ACCEPT';
+       $cmdlist .= ":$chain $policy\n";
+       $pve_include = 1 if ($chain eq 'PVEFW-FORWARD');
     }
 
-    foreach my $chain (sort keys %$ruleset) {
+    foreach my $chain (sort keys %$statushash) {
        my $stat = $statushash->{$chain};
-       die "internal error" if !$stat;
-       $changes = 1 if ($stat->{action} ne 'exists');
+       $changes = 1 if ($stat->{action} !~ 'ignore|exists');
+       next if ($stat->{action} eq 'delete');
 
-       foreach my $cmd (@{$ruleset->{$chain}}) {
+       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) = @_;
+    my ($ruleset) = @_;
 
     my $cmdlist = "";
 
     my $delete_cmdlist = "";
 
     my $active_chains = ipset_get_chains();
-    my $statushash = get_ruleset_status($ruleset, $active_chains, \&ipset_chain_digest, $verbose);
+    my $statushash = get_ruleset_status($ruleset, $active_chains, \&ipset_chain_digest);
 
     # remove stale _swap chains
     foreach my $chain (keys %$active_chains) {
@@ -3941,16 +4397,18 @@ sub get_ipset_cmdlist {
 }
 
 sub apply_ruleset {
-    my ($ruleset, $hostfw_conf, $ipset_ruleset, $rulesetv6, $ebtables_ruleset, $verbose) = @_;
+    my ($ruleset, $hostfw_conf, $ipset_ruleset, $rulesetv6, $ebtables_ruleset) = @_;
 
     enable_bridge_firewall();
 
     my ($ipset_create_cmdlist, $ipset_delete_cmdlist, $ipset_changes) =
-       get_ipset_cmdlist($ipset_ruleset, $verbose);
+       get_ipset_cmdlist($ipset_ruleset);
 
-    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);
+    my ($cmdlist, $changes) = get_ruleset_cmdlist($ruleset->{filter});
+    my ($cmdlistv6, $changesv6) = get_ruleset_cmdlist($rulesetv6->{filter}, "ip6tables");
+    my ($ebtables_cmdlist, $ebtables_changes) = get_ebtables_cmdlist($ebtables_ruleset);
+    my ($cmdlist_raw, $changes_raw) = get_ruleset_cmdlist($ruleset->{raw}, undef, 'raw');
+    my ($cmdlistv6_raw, $changesv6_raw) = get_ruleset_cmdlist($rulesetv6->{raw}, "ip6tables", 'raw');
 
     if ($verbose) {
        if ($ipset_changes) {
@@ -3969,6 +4427,16 @@ sub apply_ruleset {
            print $cmdlistv6;
        }
 
+       if ($changes_raw) {
+           print "iptables table raw changes:\n";
+           print $cmdlist_raw;
+       }
+
+       if ($changesv6_raw) {
+           print "ip6tables table raw changes:\n";
+           print $cmdlistv6_raw;
+       }
+
        if ($ebtables_changes) {
            print "ebtables changes:\n";
            print $ebtables_cmdlist;
@@ -3985,11 +4453,21 @@ sub apply_ruleset {
 
     iptables_restore_cmdlist($cmdlist);
 
+    $tmpfile = "$pve_fw_status_dir/ip4cmdlistraw";
+    PVE::Tools::file_set_contents($tmpfile, $cmdlist_raw || '');
+
+    iptables_restore_cmdlist($cmdlist_raw, 'raw');
+
     $tmpfile = "$pve_fw_status_dir/ip6cmdlist";
     PVE::Tools::file_set_contents($tmpfile, $cmdlistv6 || '');
 
     ip6tables_restore_cmdlist($cmdlistv6);
 
+    $tmpfile = "$pve_fw_status_dir/ip6cmdlistraw";
+    PVE::Tools::file_set_contents($tmpfile, $cmdlistv6_raw || '');
+
+    ip6tables_restore_cmdlist($cmdlistv6_raw, 'raw');
+
     $tmpfile = "$pve_fw_status_dir/ipsetcmdlist2";
     PVE::Tools::file_set_contents($tmpfile, $ipset_delete_cmdlist || '');
 
@@ -4001,11 +4479,12 @@ sub apply_ruleset {
     PVE::Tools::file_set_contents($tmpfile, $ebtables_cmdlist || '');
 
     # test: re-read status and check if everything is up to date
+    my $ruleset_filter = $ruleset->{filter};
     my $active_chains = iptables_get_chains();
-    my $statushash = get_ruleset_status($ruleset, $active_chains, \&iptables_chain_digest, 0);
+    my $statushash = get_ruleset_status($ruleset_filter, $active_chains, \&iptables_chain_digest);
 
     my $errors;
-    foreach my $chain (sort keys %$ruleset) {
+    foreach my $chain (sort keys %$ruleset_filter) {
        my $stat = $statushash->{$chain};
        if ($stat->{action} ne 'exists') {
            warn "unable to update chain '$chain'\n";
@@ -4013,10 +4492,11 @@ sub apply_ruleset {
        }
     }
 
+    my $rulesetv6_filter = $rulesetv6->{filter};
     my $active_chainsv6 = iptables_get_chains("ip6tables");
-    my $statushashv6 = get_ruleset_status($rulesetv6, $active_chainsv6, \&iptables_chain_digest, 0);
+    my $statushashv6 = get_ruleset_status($rulesetv6_filter, $active_chainsv6, \&iptables_chain_digest);
 
-    foreach my $chain (sort keys %$rulesetv6) {
+    foreach my $chain (sort keys %$rulesetv6_filter) {
        my $stat = $statushashv6->{$chain};
        if ($stat->{action} ne 'exists') {
            warn "unable to update chain '$chain'\n";
@@ -4024,8 +4504,34 @@ sub apply_ruleset {
        }
     }
 
+    my $ruleset_raw = $ruleset->{raw};
+    my $active_chains_raw = iptables_get_chains(undef, 'raw');
+    my $statushash_raw = get_ruleset_status($ruleset_raw, $active_chains_raw, \&iptables_chain_digest);
+
+    foreach my $chain (sort keys %$ruleset_raw) {
+       my $stat = $statushash_raw->{$chain};
+       if ($stat->{action} ne 'exists') {
+           warn "unable to update chain '$chain'\n";
+           $errors = 1;
+       }
+    }
+
+    my $rulesetv6_raw = $rulesetv6->{raw};
+    my $active_chainsv6_raw = iptables_get_chains("ip6tables", 'raw');
+    my $statushashv6_raw = get_ruleset_status($rulesetv6_raw, $active_chainsv6_raw, \&iptables_chain_digest);
+
+    foreach my $chain (sort keys %$rulesetv6_raw) {
+       my $stat = $statushashv6_raw->{$chain};
+       if ($stat->{action} ne 'exists') {
+           warn "unable to update chain '$chain'\n";
+           $errors = 1;
+       }
+    }
+
     my $active_ebtables_chains = ebtables_get_chains();
-    my $ebtables_statushash = get_ruleset_status($ebtables_ruleset, $active_ebtables_chains, \&iptables_chain_digest, 0);
+    my $ebtables_statushash = get_ruleset_status($ebtables_ruleset,
+                               $active_ebtables_chains, \&iptables_chain_digest,
+                               $pve_ebtables_chainname_regex);
 
     foreach my $chain (sort keys %$ebtables_ruleset) {
        my $stat = $ebtables_statushash->{$chain};
@@ -4041,12 +4547,15 @@ sub apply_ruleset {
 
     update_nf_conntrack_tcp_timeout_established($hostfw_conf);
 
+    update_nf_conntrack_tcp_timeout_syn_recv($hostfw_conf);
+
+    update_nf_conntrack_logging($hostfw_conf);
 }
 
 sub update_nf_conntrack_max {
     my ($hostfw_conf) = @_;
 
-    my $max = 65536; # reasonable default
+    my $max = 262144; # reasonable default (2^16 * 4), see nf_conntrack-sysctl docs
 
     my $options = $hostfw_conf->{options} || {};
 
@@ -4077,21 +4586,53 @@ sub update_nf_conntrack_tcp_timeout_established {
     PVE::ProcFSTools::write_proc_entry("/proc/sys/net/netfilter/nf_conntrack_tcp_timeout_established", $value);
 }
 
+sub update_nf_conntrack_tcp_timeout_syn_recv {
+    my ($hostfw_conf) = @_;
+
+    my $options = $hostfw_conf->{options} || {};
+
+    my $value = defined($options->{nf_conntrack_tcp_timeout_syn_recv}) ? $options->{nf_conntrack_tcp_timeout_syn_recev} : 60;
+
+    PVE::ProcFSTools::write_proc_entry("/proc/sys/net/netfilter/nf_conntrack_tcp_timeout_syn_recv", $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);
+
+       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");
     PVE::Firewall::remove_pvefw_chains_iptables("ip6tables");
+    PVE::Firewall::remove_pvefw_chains_iptables("iptables", "raw");
+    PVE::Firewall::remove_pvefw_chains_iptables("ip6tables", "raw");
     PVE::Firewall::remove_pvefw_chains_ipset();
+    PVE::Firewall::remove_pvefw_chains_ebtables();
 
 }
 
 sub remove_pvefw_chains_iptables {
-    my ($iptablescmd) = @_;
+    my ($iptablescmd, $table) = @_;
 
-    my ($chash, $hooks) = iptables_get_chains($iptablescmd);
-    my $cmdlist = "*filter\n";
+    $table = 'filter' if !$table;
+
+    my ($chash, $hooks) = iptables_get_chains($iptablescmd, $table);
+    my $cmdlist = "*$table\n";
 
-    foreach my $h (qw(INPUT OUTPUT FORWARD)) {
+    foreach my $h (qw(INPUT OUTPUT FORWARD PREROUTING)) {
        if ($hooks->{$h}) {
            $cmdlist .= "-D $h -j PVEFW-$h\n";
        }
@@ -4107,9 +4648,9 @@ sub remove_pvefw_chains_iptables {
     $cmdlist .= "COMMIT\n";
 
     if($iptablescmd eq "ip6tables") {
-       ip6tables_restore_cmdlist($cmdlist);
+       ip6tables_restore_cmdlist($cmdlist, $table);
     } else {
-       iptables_restore_cmdlist($cmdlist);
+       iptables_restore_cmdlist($cmdlist, $table);
     }
 }
 
@@ -4118,7 +4659,7 @@ sub remove_pvefw_chains_ipset {
     my $ipset_chains = ipset_get_chains();
 
     my $cmdlist = "";
+
     foreach my $chain (keys %$ipset_chains) {
        $cmdlist .= "flush $chain\n";
        $cmdlist .= "destroy $chain\n";
@@ -4127,6 +4668,11 @@ sub remove_pvefw_chains_ipset {
     ipset_restore_cmdlist($cmdlist) if $cmdlist;
 }
 
+sub remove_pvefw_chains_ebtables {
+    # apply empty ruleset = remove all our chains
+    ebtables_restore_cmdlist(get_ebtables_cmdlist({}));
+}
+
 sub init {
     my $cluster_conf = load_clusterfw_conf();
     my $cluster_options = $cluster_conf->{options};