],
'Ceph' => [
"Ceph Storage Cluster traffic (Ceph Monitors, OSD & MDS Deamons)",
+ # 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' => [
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 => {
default => 0,
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',
enable => {
description => "Enable/disable firewall rules.",
type => 'boolean',
+ default => 0,
optional => 1,
},
macfilter => {
description => "Enable/disable MAC address filter.",
type => 'boolean',
+ default => 0,
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 => {
}
sub iptables_restore_cmdlist {
- my ($cmdlist) = @_;
+ my ($cmdlist, $table) = @_;
- run_command("/usr/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("/usr/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("/usr/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("/usr/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 = {};
return;
}
- return if $table ne 'filter';
+ return if $table ne $t;
if ($line =~ m/^:(\S+)\s/) {
my $chain = $1;
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
}
};
- run_command("/usr/sbin/$iptablescmd-save", outfunc => $parser);
+ run_command(["$iptablescmd-save"], outfunc => $parser);
return wantarray ? ($res, $hooks) : $res;
}
}
};
- run_command("/usr/sbin/ipset save", outfunc => $parser);
+ run_command(['ipset', 'save'], outfunc => $parser);
# compute digest for each chain
foreach my $chain (keys %$chains) {
}
};
- run_command("/usr/sbin/ebtables-save", outfunc => $parser);
+ run_command(['ebtables-save'], outfunc => $parser);
# compute digest for each chain and store rules as well
foreach my $chain (keys %$chains) {
$res->{$chain}->{rules} = $chains->{$chain};
my $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}) {
+ # 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);
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
# corosync inbound rules
if (defined($corosync_conf)) {
my $loglevels = "emerg|alert|crit|err|warning|notice|info|debug|nolog";
- if ($line =~ m/^(enable|nosmurfs|tcpflags|ndp|log_nf_conntrack|nf_conntrack_allow_invalid):\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_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 {
push @{$cluster_conf->{ipset}->{management}}, { cidr => $localnet };
- my $ruleset = compile_iptables_filter($cluster_conf, $hostfw_conf, $vmfw_configs, $vmdata, $corosync_conf, 4);
- my $rulesetv6 = compile_iptables_filter($cluster_conf, $hostfw_conf, $vmfw_configs, $vmdata, $corosync_conf, 6);
+ 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;
+
+ 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_create_chain($ruleset, "PVEFW-PREROUTING");
+ 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");
+ }
+
+ return $ruleset;
+}
+
sub compile_iptables_filter {
my ($cluster_conf, $hostfw_conf, $vmfw_configs, $vmdata, $corosync_conf, $ipversion) = @_;
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
# 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");
}
}
sub get_ruleset_cmdlist {
- my ($ruleset, $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 $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
$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";
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";
my ($ipset_create_cmdlist, $ipset_delete_cmdlist, $ipset_changes) =
get_ipset_cmdlist($ipset_ruleset);
- my ($cmdlist, $changes) = get_ruleset_cmdlist($ruleset);
- my ($cmdlistv6, $changesv6) = get_ruleset_cmdlist($rulesetv6, "ip6tables");
+ 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) {
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;
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 || '');
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);
+ 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";
}
}
+ my $rulesetv6_filter = $rulesetv6->{filter};
my $active_chainsv6 = iptables_get_chains("ip6tables");
- my $statushashv6 = get_ruleset_status($rulesetv6, $active_chainsv6, \&iptables_chain_digest);
+ 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";
}
}
+ 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,
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} || {};
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 $tmpfile = "$pve_fw_status_dir/log_nf_conntrack";
PVE::Tools::file_set_contents($tmpfile, $value);
- PVE::Tools::run_command([qw(systemctl try-reload-or-restart pvefw-logger.service)]);
+ run_command([qw(systemctl try-reload-or-restart pvefw-logger.service)]);
$log_nf_conntrack_enabled = $value;
}
}
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";
}
$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);
}
}