X-Git-Url: https://git.proxmox.com/?p=pve-firewall.git;a=blobdiff_plain;f=src%2FPVE%2FFirewall.pm;h=f294d365545facc005c3a6195ba699eaf4674bce;hp=c7ddd10e29313f80980fa16f044116e008ee028a;hb=e17d3eec6b9bd7427a67ceb0ca21dd4fc6d6f54e;hpb=cfd7cd9caa8d26130d8eee9007b56aff805463e4 diff --git a/src/PVE/Firewall.pm b/src/PVE/Firewall.pm index c7ddd10..f294d36 100644 --- a/src/PVE/Firewall.pm +++ b/src/PVE/Firewall.pm @@ -146,14 +146,15 @@ my $log_level_hash = { # %rule # # name => optional +# enable => [0|1] # action => # proto => -# sport => -# dport => +# sport => port[,port[,port]].. or port:port +# dport => port[,port[,port]].. or port:port # log => optional, loglevel # logmsg => optional, logmsg - overwrites default -# iface_in -# iface_out +# iface_in => incomin interface +# iface_out => outgoing interface # match => optional, overwrites generation of match # target => optional, overwrites action @@ -586,10 +587,10 @@ $pve_std_chains_conf->{4} = { # Drop packets with INVALID state { action => 'DROP', match => '-m conntrack --ctstate INVALID', }, # Drop Microsoft SMB noise - { action => 'DROP', proto => 'udp', dport => '135,445', nbdport => 2 }, - { action => 'DROP', proto => 'udp', dport => '137:139'}, + { action => 'DROP', proto => 'udp', dport => '135,445' }, + { action => 'DROP', proto => 'udp', dport => '137:139' }, { action => 'DROP', proto => 'udp', dport => '1024:65535', sport => 137 }, - { action => 'DROP', proto => 'tcp', dport => '135,139,445', nbdport => 3 }, + { action => 'DROP', proto => 'tcp', dport => '135,139,445' }, { action => 'DROP', proto => 'udp', dport => 1900 }, # UPnP # Drop new/NotSyn traffic so that it doesn't get logged { action => 'DROP', match => '-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN' }, @@ -609,10 +610,10 @@ $pve_std_chains_conf->{4} = { # Drop packets with INVALID state { action => 'DROP', match => '-m conntrack --ctstate INVALID', }, # Drop Microsoft SMB noise - { action => 'PVEFW-reject', proto => 'udp', dport => '135,445', nbdport => 2 }, + { action => 'PVEFW-reject', proto => 'udp', dport => '135,445' }, { action => 'PVEFW-reject', proto => 'udp', dport => '137:139'}, { action => 'PVEFW-reject', proto => 'udp', dport => '1024:65535', sport => 137 }, - { action => 'PVEFW-reject', proto => 'tcp', dport => '135,139,445', nbdport => 3 }, + { action => 'PVEFW-reject', proto => 'tcp', dport => '135,139,445' }, { action => 'DROP', proto => 'udp', dport => 1900 }, # UPnP # Drop new/NotSyn traffic so that it doesn't get logged { action => 'DROP', match => '-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN' }, @@ -682,10 +683,10 @@ $pve_std_chains_conf->{6} = { # Drop packets with INVALID state { action => 'DROP', match => '-m conntrack --ctstate INVALID', }, # Drop Microsoft SMB noise - { action => 'DROP', proto => 'udp', dport => '135,445', nbdport => 2 }, + { action => 'DROP', proto => 'udp', dport => '135,445' }, { action => 'DROP', proto => 'udp', dport => '137:139'}, { action => 'DROP', proto => 'udp', dport => '1024:65535', sport => 137 }, - { action => 'DROP', proto => 'tcp', dport => '135,139,445', nbdport => 3 }, + { action => 'DROP', proto => 'tcp', dport => '135,139,445' }, { action => 'DROP', proto => 'udp', dport => 1900 }, # UPnP # Drop new/NotSyn traffic so that it doesn't get logged { action => 'DROP', match => '-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN' }, @@ -706,10 +707,10 @@ $pve_std_chains_conf->{6} = { # Drop packets with INVALID state { action => 'DROP', match => '-m conntrack --ctstate INVALID', }, # Drop Microsoft SMB noise - { action => 'PVEFW-reject', proto => 'udp', dport => '135,445', nbdport => 2 }, - { action => 'PVEFW-reject', proto => 'udp', dport => '137:139'}, + { action => 'PVEFW-reject', proto => 'udp', dport => '135,445' }, + { action => 'PVEFW-reject', proto => 'udp', dport => '137:139' }, { action => 'PVEFW-reject', proto => 'udp', dport => '1024:65535', sport => 137 }, - { action => 'PVEFW-reject', proto => 'tcp', dport => '135,139,445', nbdport => 3 }, + { action => 'PVEFW-reject', proto => 'tcp', dport => '135,139,445' }, { action => 'DROP', proto => 'udp', dport => 1900 }, # UPnP # Drop new/NotSyn traffic so that it doesn't get logged { action => 'DROP', match => '-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN' }, @@ -859,7 +860,7 @@ sub get_etc_services { next if $line =~m/^#/; next if ($line =~m/^\s*$/); - if ($line =~ m!^(\S+)\s+(\S+)/(tcp|udp).*$!) { + if ($line =~ m!^(\S+)\s+(\S+)/(tcp|udp|sctp).*$!) { $services->{byid}->{$2}->{name} = $1; $services->{byid}->{$2}->{port} = $2; $services->{byid}->{$2}->{$3} = 1; @@ -875,12 +876,8 @@ sub get_etc_services { return $etc_services; } -my $etc_protocols; - -sub get_etc_protocols { - return $etc_protocols if $etc_protocols; - - my $filename = "/etc/protocols"; +sub parse_protocol_file { + my ($filename) = @_; my $fh = IO::File->new($filename, O_RDONLY); if (!$fh) { @@ -895,7 +892,7 @@ sub get_etc_protocols { next if $line =~m/^#/; next if ($line =~m/^\s*$/); - if ($line =~ m!^(\S+)\s+(\d+)\s+.*$!) { + if ($line =~ m!^(\S+)\s+(\d+)(?:\s+.*)?$!) { $protocols->{byid}->{$2}->{name} = $1; $protocols->{byname}->{$1} = $protocols->{byid}->{$2}; } @@ -903,6 +900,16 @@ sub get_etc_protocols { close($fh); + return $protocols; +} + +my $etc_protocols; + +sub get_etc_protocols { + return $etc_protocols if $etc_protocols; + + my $protocols = parse_protocol_file('/etc/protocols'); + # add special case for ICMP v6 $protocols->{byid}->{icmpv6}->{name} = "icmpv6"; $protocols->{byname}->{icmpv6} = $protocols->{byid}->{icmpv6}; @@ -912,6 +919,14 @@ sub get_etc_protocols { return $etc_protocols; } +my $etc_ethertypes; + +sub get_etc_ethertypes { + $etc_ethertypes = parse_protocol_file('/etc/ethertypes') + if !$etc_ethertypes; + return $etc_ethertypes; +} + my $__local_network; sub local_network { @@ -1034,12 +1049,14 @@ 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) { - $count++; - 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; } else { @@ -1053,9 +1070,15 @@ sub parse_port_name_number_or_range { } } - die "ICPM ports not allowed in port range\n" if $icmp_port && $count > 1; + die "ICPM ports not allowed in port range\n" if $icmp_port && $count > 0; - return $count; + # I really don't like to use the word number here, but it's the only thing + # that makes sense in a literal way. The range 1:100 counts as 2, not as + # one and not as 100... + die "too many entries in port list (> 15 numbers)\n" + if $count > 15; + + return (scalar(@elements) > 1); } PVE::JSONSchema::register_format('pve-fw-sport-spec', \&pve_fw_verify_sport_spec); @@ -1158,6 +1181,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', @@ -1213,6 +1242,18 @@ our $host_option_properties = { type => 'boolean', optional => 1, }, + nf_conntrack_allow_invalid => { + description => "Allow invalid packets on connection tracking.", + type => 'boolean', + default => 0, + optional => 1, + }, + log_nf_conntrack => { + description => "Enable logging of conntrack information.", + type => 'boolean', + default => 0, + optional => 1 + }, }; our $vm_option_properties = { @@ -1328,6 +1369,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', @@ -1689,6 +1733,12 @@ sub ipset_restore_cmdlist { run_command("/sbin/ipset restore", input => $cmdlist, errmsg => "ipset_restore_cmdlist"); } +sub ebtables_restore_cmdlist { + my ($cmdlist) = @_; + + run_command("/sbin/ebtables-restore", input => $cmdlist, errmsg => "ebtables_restore_cmdlist"); +} + sub iptables_get_chains { my ($iptablescmd) = @_; @@ -1799,171 +1849,186 @@ sub ipset_get_chains { return $res; } -sub ruleset_generate_match { - my ($ruleset, $chain, $ipversion, $rule, $actions, $goto, $cluster_conf, $fw_conf) = @_; +sub ebtables_get_chains { - return if defined($rule->{enable}) && !$rule->{enable}; - return if $rule->{errors}; - - return $rule->{match} if defined $rule->{match}; - - die "unable to emit macro - internal error" if $rule->{macro}; # should not happen + my $res = {}; + my $chains = {}; + my $parser = sub { + my $line = shift; + return if $line =~ m/^#/; + return if $line =~ m/^\s*$/; + if ($line =~ m/^:(\S+)\s\S+$/) { + # Make sure we know chains exist even if they're empty. + $chains->{$1} //= []; + } elsif ($line =~ m/^(?:\S+)\s(\S+)\s(?:\S+).*/) { + my $chain = $1; + $line =~ s/\s+$//; + push @{$chains->{$chain}}, $line; + } else { + # simply ignore the rest + return; + } + }; - my $nbdport = defined($rule->{dport}) ? parse_port_name_number_or_range($rule->{dport}, 1) : 0; - my $nbsport = defined($rule->{sport}) ? parse_port_name_number_or_range($rule->{sport}, 0) : 0; + run_command("/sbin/ebtables-save", outfunc => $parser); + # compute digest for each chain and store rules as well + foreach my $chain (keys %$chains) { + $res->{$chain}->{rules} = $chains->{$chain}; + $res->{$chain}->{sig} = iptables_chain_digest($chains->{$chain}); + } + return $res; +} - my @cmd = (); +# substitude action of rule according to action hash +sub rule_substitude_action { + my ($rule, $actions) = @_; - push @cmd, "-i $rule->{iface_in}" if $rule->{iface_in}; - push @cmd, "-o $rule->{iface_out}" if $rule->{iface_out}; + if (my $action = $rule->{action}) { + $rule->{action} = $actions->{$action} if defined($actions->{$action}); + } +} - my $source = $rule->{source}; - my $dest = $rule->{dest}; +# generate a src or dst match +# $dir(ection) is either d or s +sub ipt_gen_src_or_dst_match { + my ($adr, $dir, $ipversion, $cluster_conf, $fw_conf) = @_; - if ($source) { - if ($source =~ m/^\+/) { - if ($source =~ m/^\+(${ipset_name_pattern})$/) { - my $name = $1; - if ($fw_conf && $fw_conf->{ipset}->{$name}) { - my $ipset_chain = compute_ipset_chain_name($fw_conf->{vmid}, $name, $ipversion); - push @cmd, "-m set --match-set ${ipset_chain} src"; - } elsif ($cluster_conf && $cluster_conf->{ipset}->{$name}) { - my $ipset_chain = compute_ipset_chain_name(0, $name, $ipversion); - push @cmd, "-m set --match-set ${ipset_chain} src"; - } else { - die "no such ipset '$name'\n"; - } + my $srcdst; + if ($dir eq 's') { + $srcdst = "src"; + } elsif ($dir eq 'd') { + $srcdst = "dst"; + } else { + die "ipt_gen_src_or_dst_match: invalid direction $dir \n"; + } + + my $match; + if ($adr =~ m/^\+/) { + if ($adr =~ m/^\+(${ipset_name_pattern})$/) { + my $name = $1; + my $ipset_chain; + if ($fw_conf && $fw_conf->{ipset}->{$name}) { + $ipset_chain = compute_ipset_chain_name($fw_conf->{vmid}, $name, $ipversion); + } elsif ($cluster_conf && $cluster_conf->{ipset}->{$name}) { + $ipset_chain = compute_ipset_chain_name(0, $name, $ipversion); } else { - die "invalid security group name '$source'\n"; + die "no such ipset '$name'\n"; } - } elsif ($source =~ m/^${ip_alias_pattern}$/){ - my $alias = lc($source); - my $e = $fw_conf ? $fw_conf->{aliases}->{$alias} : undef; - $e = $cluster_conf->{aliases}->{$alias} if !$e && $cluster_conf; - die "no such alias '$source'\n" if !$e; - push @cmd, "-s $e->{cidr}"; - } elsif ($source =~ m/\-/){ - push @cmd, "-m iprange --src-range $source"; + $match = "-m set --match-set ${ipset_chain} ${srcdst}"; } else { - push @cmd, "-s $source"; - } + die "invalid security group name '$adr'\n"; + } + } elsif ($adr =~ m/^${ip_alias_pattern}$/){ + my $alias = lc($adr); + my $e = $fw_conf ? $fw_conf->{aliases}->{$alias} : undef; + $e = $cluster_conf->{aliases}->{$alias} if !$e && $cluster_conf; + die "no such alias '$adr'\n" if !$e; + $match = "-${dir} $e->{cidr}"; + } elsif ($adr =~ m/\-/){ + $match = "-m iprange --${srcdst}-range $adr"; + } else { + $match = "-${dir} $adr"; } - if ($dest) { - if ($dest =~ m/^\+/) { - if ($dest =~ m/^\+(${ipset_name_pattern})$/) { - my $name = $1; - if ($fw_conf && $fw_conf->{ipset}->{$name}) { - my $ipset_chain = compute_ipset_chain_name($fw_conf->{vmid}, $name, $ipversion); - push @cmd, "-m set --match-set ${ipset_chain} dst"; - } elsif ($cluster_conf && $cluster_conf->{ipset}->{$name}) { - my $ipset_chain = compute_ipset_chain_name(0, $name, $ipversion); - push @cmd, "-m set --match-set ${ipset_chain} dst"; - } else { - die "no such ipset '$name'\n"; - } - } else { - die "invalid security group name '$dest'\n"; - } - } elsif ($dest =~ m/^${ip_alias_pattern}$/){ - my $alias = lc($dest); - my $e = $fw_conf ? $fw_conf->{aliases}->{$alias} : undef; - $e = $cluster_conf->{aliases}->{$alias} if !$e && $cluster_conf; - die "no such alias '$dest'\n" if !$e; - push @cmd, "-d $e->{cidr}"; - } elsif ($dest =~ m/^(\d+)\.(\d+).(\d+).(\d+)\-(\d+)\.(\d+).(\d+).(\d+)$/){ - push @cmd, "-m iprange --dst-range $dest"; - } else { - push @cmd, "-d $dest"; - } - } + return $match; +} - if (my $proto = $rule->{proto}) { - push @cmd, "-p $proto"; +# convert a %rule to an array of iptables commands +sub ipt_rule_to_cmds { + my ($rule, $chain, $ipversion, $cluster_conf, $fw_conf, $vmid) = @_; - my $multiport = 0; - $multiport++ if $nbdport > 1; - $multiport++ if $nbsport > 1; + die "ipt_rule_to_cmds unable to handle macro" if $rule->{macro}; #should not happen - push @cmd, "--match multiport" if $multiport; + my @match = (); - die "multiport: option '--sports' cannot be used together with '--dports'\n" - if ($multiport == 2) && ($rule->{dport} ne $rule->{sport}); + if (defined $rule->{match}) { + push @match, $rule->{match}; + } else { + push @match, "-i $rule->{iface_in}" if $rule->{iface_in}; + push @match, "-o $rule->{iface_out}" if $rule->{iface_out}; - if ($rule->{dport}) { - if ($proto eq 'icmp') { - # Note: we use dport to store --icmp-type - die "unknown icmp-type '$rule->{dport}'\n" - if $rule->{dport} !~ /^\d+$/ && !defined($icmp_type_names->{$rule->{dport}}); - push @cmd, "-m icmp --icmp-type $rule->{dport}"; - } elsif ($proto eq 'icmpv6') { - # Note: we use dport to store --icmpv6-type - die "unknown icmpv6-type '$rule->{dport}'\n" - if $rule->{dport} !~ /^\d+$/ && !defined($icmpv6_type_names->{$rule->{dport}}); - push @cmd, "-m icmpv6 --icmpv6-type $rule->{dport}"; - } elsif (!$PROTOCOLS_WITH_PORTS->{$proto}) { - die "protocol $proto does not have ports\n"; - } else { - if ($nbdport > 1) { - if ($multiport == 2) { - push @cmd, "--ports $rule->{dport}"; - } else { - push @cmd, "--dports $rule->{dport}"; - } - } else { - push @cmd, "--dport $rule->{dport}"; - } - } + if ($rule->{source}) { + push @match, ipt_gen_src_or_dst_match($rule->{source}, 's', $ipversion, $cluster_conf, $fw_conf); } - - if ($rule->{sport}) { - die "protocol $proto does not have ports\n" - if !$PROTOCOLS_WITH_PORTS->{$proto}; - if ($nbsport > 1) { - push @cmd, "--sports $rule->{sport}" if $multiport != 2; - } else { - push @cmd, "--sport $rule->{sport}"; - } + if ($rule->{dest}) { + push @match, ipt_gen_src_or_dst_match($rule->{dest}, 'd', $ipversion, $cluster_conf, $fw_conf); } - } elsif ($rule->{dport} || $rule->{sport}) { - die "destination port '$rule->{dport}', but no protocol specified\n" if $rule->{dport}; - die "source port '$rule->{sport}', but no protocol specified\n" if $rule->{sport}; - } - - push @cmd, "-m addrtype --dst-type $rule->{dsttype}" if $rule->{dsttype}; - return scalar(@cmd) ? join(' ', @cmd) : undef; -} + if (my $proto = $rule->{proto}) { + push @match, "-p $proto"; + + my $multidport = defined($rule->{dport}) && parse_port_name_number_or_range($rule->{dport}, 1); + my $multisport = defined($rule->{sport}) && parse_port_name_number_or_range($rule->{sport}, 0); + + my $add_dport = sub { + return if !$rule->{dport}; + + if ($proto eq 'icmp') { + # Note: we use dport to store --icmp-type + die "unknown icmp-type '$rule->{dport}'\n" + if $rule->{dport} !~ /^\d+$/ && !defined($icmp_type_names->{$rule->{dport}}); + push @match, "-m icmp --icmp-type $rule->{dport}"; + } elsif ($proto eq 'icmpv6') { + # Note: we use dport to store --icmpv6-type + die "unknown icmpv6-type '$rule->{dport}'\n" + if $rule->{dport} !~ /^\d+$/ && !defined($icmpv6_type_names->{$rule->{dport}}); + push @match, "-m icmpv6 --icmpv6-type $rule->{dport}"; + } elsif (!$PROTOCOLS_WITH_PORTS->{$proto}) { + die "protocol $proto does not have ports\n"; + } elsif ($multidport) { + push @match, "--match multiport", "--dports $rule->{dport}"; + } else { + push @match, "--dport $rule->{dport}"; + } + }; -sub ruleset_generate_action { - my ($ruleset, $chain, $ipversion, $rule, $actions, $goto, $cluster_conf, $fw_conf) = @_; + my $add_sport = sub { + return if !$rule->{sport}; - return $rule->{target} if defined $rule->{target}; + die "protocol $proto does not have ports\n" + if !$PROTOCOLS_WITH_PORTS->{$proto}; + if ($multisport) { + push @match, "--match multiport", "--sports $rule->{sport}"; + } else { + push @match, "--sport $rule->{sport}"; + } + }; - my @cmd = (); + # order matters - single port before multiport! + $add_dport->() if $multisport; + $add_sport->(); + $add_dport->() if !$multisport; + } elsif ($rule->{dport} || $rule->{sport}) { + die "destination port '$rule->{dport}', but no protocol specified\n" if $rule->{dport}; + die "source port '$rule->{sport}', but no protocol specified\n" if $rule->{sport}; + } - if (my $action = $rule->{action}) { - $action = $actions->{$action} if defined($actions->{$action}); - $goto = 1 if !defined($goto) && $action eq 'PVEFW-SET-ACCEPT-MARK'; - push @cmd, $goto ? "-g $action" : "-j $action"; + push @match, "-m addrtype --dst-type $rule->{dsttype}" if $rule->{dsttype}; } + my $matchstr = scalar(@match) ? join(' ', @match) : ""; - return scalar(@cmd) ? join(' ', @cmd) : undef; -} - -sub ruleset_generate_cmdstr { - my ($ruleset, $chain, $ipversion, $rule, $actions, $goto, $cluster_conf, $fw_conf) = @_; - my $match = ruleset_generate_match($ruleset, $chain, $ipversion, $rule, $actions, $goto, $cluster_conf, $fw_conf); - my $action = ruleset_generate_action($ruleset, $chain, $ipversion, $rule, $actions, $goto, $cluster_conf, $fw_conf); + my $targetstr; + if (defined $rule->{target}) { + $targetstr = $rule->{target}; + } else { + my $action = (defined $rule->{action}) ? $rule->{action} : ""; + my $goto = 1 if $action eq 'PVEFW-SET-ACCEPT-MARK'; + $targetstr = ($goto) ? "-g $action" : "-j $action"; + } - return undef if !(defined($match) or defined($action)); - my $ret = defined($match) ? $match : ""; - $ret = "$ret $action" if defined($action); - return $ret; + my @iptcmds; + 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"; + return @iptcmds; } sub ruleset_generate_rule { - my ($ruleset, $chain, $ipversion, $rule, $actions, $goto, $cluster_conf, $fw_conf) = @_; + my ($ruleset, $chain, $ipversion, $rule, $cluster_conf, $fw_conf, $vmid) = @_; my $rules; @@ -1974,37 +2039,12 @@ sub ruleset_generate_rule { } # update all or nothing - - # fixme: lots of temporary ugliness - my @mstrs = (); - my @astrs = (); - my @logging = (); - my @logmsg = (); - foreach my $tmp (@$rules) { - my $m = ruleset_generate_match($ruleset, $chain, $ipversion, $tmp, $actions, $goto, $cluster_conf, $fw_conf); - my $a = ruleset_generate_action($ruleset, $chain, $ipversion, $tmp, $actions, $goto, $cluster_conf, $fw_conf); - if (defined $m or defined $a) { - push @mstrs, defined($m) ? $m : ""; - push @astrs, defined($a) ? $a : ""; - push @logging, $tmp->{log}; - push @logmsg, $tmp->{logmsg}; - } - } - - for my $i (0 .. $#mstrs) { - ruleset_addrule($ruleset, $chain, $mstrs[$i], $astrs[$i], $logging[$i], $logmsg[$i]); + my @ipt_rule_cmds; + foreach my $r (@$rules) { + push @ipt_rule_cmds, ipt_rule_to_cmds($r, $chain, $ipversion, $cluster_conf, $fw_conf, $vmid); } -} - -sub ruleset_generate_rule_insert { - my ($ruleset, $chain, $ipversion, $rule, $actions, $goto) = @_; - - die "implement me" if $rule->{macro}; # not implemented, because not needed so far - - my $match = ruleset_generate_match($ruleset, $chain, $ipversion, $rule, $actions, $goto); - my $action = ruleset_generate_action($ruleset, $chain, $ipversion, $rule, $actions, $goto); - if (defined $match && defined $action) { - ruleset_insertrule($ruleset, $chain, $match, $action); + foreach my $c (@ipt_rule_cmds) { + ruleset_add_ipt_cmd($ruleset, $chain, $c); } } @@ -2025,16 +2065,28 @@ sub ruleset_chain_exist { return $ruleset->{$chain} ? 1 : undef; } -sub ruleset_addrule { - my ($ruleset, $chain, $match, $action, $log, $logmsg, $vmid) = @_; +# add an iptables command (like generated by ipt_rule_to_cmds) to a chain +sub ruleset_add_ipt_cmd { + my ($ruleset, $chain, $iptcmd) = @_; die "no such chain '$chain'\n" if !$ruleset->{$chain}; - if (defined($log) && $log) { - my $logaction = get_log_rule_base($chain, $vmid, $logmsg, $log); + push @{$ruleset->{$chain}}, $iptcmd; +} + +sub ruleset_addrule { + my ($ruleset, $chain, $match, $action, $log, $logmsg, $vmid) = @_; + + die "no such chain '$chain'\n" if !$ruleset->{$chain}; + + 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"; - } - 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 { @@ -2054,7 +2106,7 @@ sub get_log_rule_base { # 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 "-m limit --limit 1/sec -j NFLOG --nflog-prefix \":$vmid:$loglevel:$chain: $msg\""; } sub ruleset_add_chain_policy { @@ -2062,8 +2114,9 @@ sub ruleset_add_chain_policy { if ($policy eq 'ACCEPT') { - ruleset_generate_rule($ruleset, $chain, $ipversion, { action => 'ACCEPT' }, - { ACCEPT => $accept_action}); + my $rule = { action => 'ACCEPT' }; + rule_substitude_action($rule, { ACCEPT => $accept_action}); + ruleset_generate_rule($ruleset, $chain, $ipversion, $rule); } elsif ($policy eq 'DROP') { @@ -2073,7 +2126,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'"; @@ -2093,9 +2146,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"); } @@ -2105,7 +2160,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"); @@ -2191,7 +2246,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); @@ -2208,14 +2263,13 @@ sub ruleset_generate_vm_rules { } else { next if $rule->{type} ne $lc_direction; eval { + $rule->{logmsg} = "$rule->{action}: "; if ($direction eq 'OUT') { - ruleset_generate_rule($ruleset, $chain, $ipversion, $rule, - { ACCEPT => "PVEFW-SET-ACCEPT-MARK", REJECT => "PVEFW-reject" }, - undef, $cluster_conf, $vmfw_conf); + rule_substitude_action($rule, { ACCEPT => "PVEFW-SET-ACCEPT-MARK", REJECT => "PVEFW-reject" }); + ruleset_generate_rule($ruleset, $chain, $ipversion, $rule, $cluster_conf, $vmfw_conf, $vmid); } else { - ruleset_generate_rule($ruleset, $chain, $ipversion, $rule, - { ACCEPT => $in_accept , REJECT => "PVEFW-reject" }, - undef, $cluster_conf, $vmfw_conf); + rule_substitude_action($rule, { ACCEPT => $in_accept , REJECT => "PVEFW-reject" }); + ruleset_generate_rule($ruleset, $chain, $ipversion, $rule, $cluster_conf, $vmfw_conf, $vmid); } }; warn $@ if $@; @@ -2276,7 +2330,7 @@ sub generate_tap_rules_direction { 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); + ruleset_generate_vm_rules($ruleset, $rules, $cluster_conf, $vmfw_conf, $tapchain, $netid, $direction, $options, $ipversion, $vmid); ruleset_generate_vm_ipsrules($ruleset, $options, $direction, $iface); @@ -2286,7 +2340,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); @@ -2300,10 +2354,10 @@ sub generate_tap_rules_direction { # plug the tap chain to bridge chain if ($direction eq 'IN') { ruleset_addrule($ruleset, "PVEFW-FWBR-IN", - "-m physdev --physdev-is-bridged --physdev-out $iface", "-j $tapchain"); + "-m physdev --physdev-is-bridged --physdev-out $iface", "-j $tapchain", $loglevel, 'FWBR-IN: ', $vmid); } else { ruleset_addrule($ruleset, "PVEFW-FWBR-OUT", - "-m physdev --physdev-is-bridged --physdev-in $iface", "-j $tapchain"); + "-m physdev --physdev-is-bridged --physdev-in $iface", "-j $tapchain", $loglevel, 'FWBR-OUT: ', $vmid); } } @@ -2323,7 +2377,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); @@ -2343,9 +2397,8 @@ sub enable_host_firewall { if ($rule->{type} eq 'group') { ruleset_add_group_rule($ruleset, $cluster_conf, $chain, $rule, 'IN', $accept_action, $ipversion); } elsif ($rule->{type} eq 'in') { - ruleset_generate_rule($ruleset, $chain, $ipversion, $rule, - { ACCEPT => $accept_action, REJECT => "PVEFW-reject" }, - undef, $cluster_conf, $hostfw_conf); + rule_substitude_action($rule, { ACCEPT => $accept_action, REJECT => "PVEFW-reject" }); + ruleset_generate_rule($ruleset, $chain, $ipversion, $rule, $cluster_conf, $hostfw_conf, 0); } }; warn $@ if $@; @@ -2382,7 +2435,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'; @@ -2397,12 +2450,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') { - ruleset_generate_rule($ruleset, $chain, $ipversion, - $rule, { ACCEPT => $accept_action, REJECT => "PVEFW-reject" }, - undef, $cluster_conf, $hostfw_conf); + rule_substitude_action($rule, { ACCEPT => $accept_action, REJECT => "PVEFW-reject" }); + ruleset_generate_rule($ruleset, $chain, $ipversion, $rule, $cluster_conf, $hostfw_conf, 0); } }; warn $@ if $@; @@ -2446,10 +2499,10 @@ sub generate_group_rules { foreach my $rule (@$rules) { next if $rule->{type} ne 'in'; + next if !$rule->{enable} || $rule->{errors}; next if $rule->{ipversion} && $rule->{ipversion} ne $ipversion; - ruleset_generate_rule($ruleset, $chain, $ipversion, $rule, - { ACCEPT => "PVEFW-SET-ACCEPT-MARK", REJECT => "PVEFW-reject" }, - undef, $cluster_conf); + rule_substitude_action($rule, { ACCEPT => "PVEFW-SET-ACCEPT-MARK", REJECT => "PVEFW-reject" }); + ruleset_generate_rule($ruleset, $chain, $ipversion, $rule, $cluster_conf); } $chain = "GROUP-${group}-OUT"; @@ -2459,12 +2512,12 @@ sub generate_group_rules { foreach my $rule (@$rules) { next if $rule->{type} ne 'out'; + next if !$rule->{enable} || $rule->{errors}; next if $rule->{ipversion} && $rule->{ipversion} ne $ipversion; # we use PVEFW-SET-ACCEPT-MARK (Instead of ACCEPT) because we need to # check also other tap rules later - ruleset_generate_rule($ruleset, $chain, $ipversion, $rule, - { ACCEPT => 'PVEFW-SET-ACCEPT-MARK', REJECT => "PVEFW-reject" }, - undef, $cluster_conf); + rule_substitude_action($rule, { ACCEPT => 'PVEFW-SET-ACCEPT-MARK', REJECT => "PVEFW-reject" }); + ruleset_generate_rule($ruleset, $chain, $ipversion, $rule, $cluster_conf); } } @@ -2541,6 +2594,10 @@ sub parse_fw_rule { $rule->{dest} = $1; next; } + if ($line =~ s/^-log (emerg|alert|crit|err|warning|notice|info|debug|nolog)\s*//) { + $rule->{log} = $1; + next; + } last; } @@ -2562,6 +2619,14 @@ sub parse_fw_rule { return $rule; } +sub verify_ethertype { + my ($value) = @_; + my $types = get_etc_ethertypes(); + die "unknown ethernet protocol type: $value\n" + if !defined($types->{byname}->{$value}) && + !defined($types->{byid}->{$value}); +} + sub parse_vmfw_option { my ($line) = @_; @@ -2581,6 +2646,10 @@ sub parse_vmfw_option { } elsif ($line =~ m/^(ips_queues):\s*((\d+)(:(\d+))?)\s*$/i) { $opt = lc($1); $value = $2; + } elsif ($line =~ m/^(layer2_protocols):\s*(((\S+)[,]?)+)\s*$/i) { + $opt = lc($1); + $value = $2; + verify_ethertype($_) foreach split(/\s*,\s*/, $value); } else { die "can't parse option '$line'\n" } @@ -2595,7 +2664,7 @@ 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):\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) { @@ -2622,6 +2691,9 @@ 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); @@ -2978,6 +3050,7 @@ 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 .= " # " . encode('utf8', $rule->{comment}) @@ -3136,9 +3209,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"; @@ -3171,7 +3242,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"; } @@ -3377,7 +3448,7 @@ sub compile { $vmfw_configs = read_vm_firewall_configs($cluster_conf, $vmdata, undef, $verbose); } - return ({},{},{}) if !$cluster_conf->{options}->{enable}; + return ({},{},{},{}) if !$cluster_conf->{options}->{enable}; my $localnet; if ($cluster_conf->{aliases}->{local_network}) { @@ -3386,7 +3457,7 @@ 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 }; } @@ -3394,9 +3465,10 @@ sub compile { my $ruleset = compile_iptables_filter($cluster_conf, $hostfw_conf, $vmfw_configs, $vmdata, 4, $verbose); my $rulesetv6 = compile_iptables_filter($cluster_conf, $hostfw_conf, $vmfw_configs, $vmdata, 6, $verbose); + my $ebtables_ruleset = compile_ebtables_filter($cluster_conf, $hostfw_conf, $vmfw_configs, $vmdata, $verbose); my $ipset_ruleset = compile_ipsets($cluster_conf, $vmfw_configs, $vmdata); - return ($ruleset, $ipset_ruleset, $rulesetv6); + return ($ruleset, $ipset_ruleset, $rulesetv6, $ebtables_ruleset); } sub compile_iptables_filter { @@ -3414,7 +3486,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); @@ -3608,17 +3681,148 @@ sub compile_ipsets { return $ipset_ruleset; } +sub compile_ebtables_filter { + my ($cluster_conf, $hostfw_conf, $vmfw_configs, $vmdata, $verbose) = @_; + + if (!($cluster_conf->{options}->{ebtables} // 1)) { + return {}; + } + + my $ruleset = {}; + + ruleset_create_chain($ruleset, "PVEFW-FORWARD"); + + ruleset_create_chain($ruleset, "PVEFW-FWBR-OUT"); + #for ipv4 and ipv6, check macaddress in iptables, so we use conntrack 'ESTABLISHED', to speedup rules + ruleset_addrule($ruleset, 'PVEFW-FORWARD', '-p IPv4', '-j ACCEPT'); + ruleset_addrule($ruleset, 'PVEFW-FORWARD', '-p IPv6', '-j ACCEPT'); + ruleset_addrule($ruleset, 'PVEFW-FORWARD', '-o fwln+', '-j PVEFW-FWBR-OUT'); + + # generate firewall rules for QEMU VMs + foreach my $vmid (sort keys %{$vmdata->{qemu}}) { + eval { + my $conf = $vmdata->{qemu}->{$vmid}; + my $vmfw_conf = $vmfw_configs->{$vmid}; + return if !$vmfw_conf; + my $ipsets = $vmfw_conf->{ipset}; + + foreach my $netid (sort keys %$conf) { + next if $netid !~ m/^net(\d+)$/; + my $net = PVE::QemuServer::parse_net($conf->{$netid}); + next if !$net->{firewall}; + my $iface = "tap${vmid}i$1"; + my $macaddr = $net->{macaddr}; + my $arpfilter = []; + if (defined(my $ipset = $ipsets->{"ipfilter-$netid"})) { + foreach my $ipaddr (@$ipset) { + my($ip, $version) = parse_ip_or_cidr($ipaddr->{cidr}); + next if !$ip || ($version && $version != 4); + push(@$arpfilter, $ip); + } + } + generate_tap_layer2filter($ruleset, $iface, $macaddr, $vmfw_conf, $vmid, $arpfilter); + } + }; + warn $@ if $@; # just to be sure - should not happen + } + + # generate firewall rules for LXC containers + foreach my $vmid (sort keys %{$vmdata->{lxc}}) { + eval { + my $conf = $vmdata->{lxc}->{$vmid}; + + my $vmfw_conf = $vmfw_configs->{$vmid}; + return if !$vmfw_conf || !$vmfw_conf->{options}->{enable}; + my $ipsets = $vmfw_conf->{ipset}; + + foreach my $netid (sort keys %$conf) { + next if $netid !~ m/^net(\d+)$/; + my $net = PVE::LXC::Config->parse_lxc_network($conf->{$netid}); + next if !$net->{firewall}; + my $iface = "veth${vmid}i$1"; + my $macaddr = $net->{hwaddr}; + my $arpfilter = []; + if (defined(my $ipset = $ipsets->{"ipfilter-$netid"})) { + foreach my $ipaddr (@$ipset) { + my($ip, $version) = parse_ip_or_cidr($ipaddr->{cidr}); + next if !$ip || ($version && $version != 4); + push(@$arpfilter, $ip); + } + } + push(@$arpfilter, $net->{ip}) if $net->{ip} && $vmfw_conf->{options}->{ipfilter}; + generate_tap_layer2filter($ruleset, $iface, $macaddr, $vmfw_conf, $vmid, $arpfilter); + } + }; + warn $@ if $@; # just to be sure - should not happen + } + + return $ruleset; +} + +sub generate_tap_layer2filter { + my ($ruleset, $iface, $macaddr, $vmfw_conf, $vmid, $arpfilter) = @_; + my $options = $vmfw_conf->{options}; + + my $tapchain = $iface."-OUT"; + + # ebtables remove zeros from mac pairs + $macaddr =~ s/0([0-9a-f])/$1/ig; + $macaddr = lc($macaddr); + + ruleset_create_chain($ruleset, $tapchain); + + if (defined($macaddr) && !(defined($options->{macfilter}) && $options->{macfilter} == 0)) { + ruleset_addrule($ruleset, $tapchain, "-s ! $macaddr", '-j DROP'); + } + + if (@$arpfilter){ + my $arpchain = $tapchain."-ARP"; + ruleset_addrule($ruleset, $tapchain, "-p ARP", "-j $arpchain"); + ruleset_create_chain($ruleset, $arpchain); + + foreach my $ip (@{$arpfilter}) { + ruleset_addrule($ruleset, $arpchain, "-p ARP --arp-ip-src $ip", '-j RETURN'); + } + ruleset_addrule($ruleset, $arpchain, '', '-j DROP'); + } + + if (defined($options->{layer2_protocols})){ + my $protochain = $tapchain."-PROTO"; + ruleset_addrule($ruleset, $tapchain, '', "-j $protochain"); + ruleset_create_chain($ruleset, $protochain); + + foreach my $proto (split(/,/, $options->{layer2_protocols})) { + ruleset_addrule($ruleset, $protochain, "-p $proto", '-j RETURN'); + } + ruleset_addrule($ruleset, $protochain, '', '-j DROP'); + } + + 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, $verbose, $change_only_regex) = @_; my $statushash = {}; foreach my $chain (sort keys %$ruleset) { - my $sig = &$digest_fn($ruleset->{$chain}); + my $rules = $ruleset->{$chain}; + my $sig = &$digest_fn($rules); + my $oldsig; $statushash->{$chain}->{sig} = $sig; - - my $oldsig = $active_chains->{$chain}; + if (defined($change_only_regex)) { + $oldsig = $active_chains->{$chain}->{sig}; + $statushash->{$chain}->{rules} = $rules; + } else { + $oldsig = $active_chains->{$chain}; + } if (!defined($oldsig)) { $statushash->{$chain}->{action} = 'create'; } else { @@ -3628,19 +3832,26 @@ sub get_ruleset_status { $statushash->{$chain}->{action} = 'update'; } } - print "$statushash->{$chain}->{action} $chain ($sig)\n" if $verbose; - foreach my $cmd (@{$ruleset->{$chain}}) { - print "\t$cmd\n" if $verbose; + if ($verbose) { + print "$statushash->{$chain}->{action} $chain ($sig)\n"; + foreach my $cmd (@{$rules}) { + print "\t$cmd\n"; + } } } foreach my $chain (sort keys %$active_chains) { - if (!defined($ruleset->{$chain})) { - my $sig = $active_chains->{$chain}; - $statushash->{$chain}->{action} = 'delete'; - $statushash->{$chain}->{sig} = $sig; - print "delete $chain ($sig)\n" if $verbose; + next if defined($ruleset->{$chain}); + my $action = 'delete'; + my $sig = $active_chains->{$chain}; + if (defined($change_only_regex)) { + $action = 'ignore' if ($chain !~ m/$change_only_regex/); + $statushash->{$chain}->{rules} = $active_chains->{$chain}->{rules}; + $sig = $sig->{sig}; } + $statushash->{$chain}->{action} = $action; + $statushash->{$chain}->{sig} = $sig; + print "$action $chain ($sig)\n" if $verbose; } return $statushash; @@ -3715,6 +3926,46 @@ sub get_ruleset_cmdlist { return wantarray ? ($cmdlist, $changes) : $cmdlist; } +my $pve_ebtables_chainname_regex = qr/PVEFW-\S+|(?:tap|veth)\d+i\d+-(?:IN|OUT)/; + +sub get_ebtables_cmdlist { + my ($ruleset, $verbose) = @_; + + my $changes = 0; + my $cmdlist = "*filter\n"; + + my $active_chains = ebtables_get_chains(); + my $statushash = get_ruleset_status($ruleset, $active_chains, + \&iptables_chain_digest, $verbose, + $pve_ebtables_chainname_regex); + + # create chains first and make sure PVE rules are evaluated if active + my $append_pve_to_forward = '-A FORWARD -j PVEFW-FORWARD'; + my $pve_include = 0; + foreach my $chain (sort keys %$statushash) { + next if ($statushash->{$chain}->{action} eq 'delete'); + $cmdlist .= ":$chain ACCEPT\n"; + $pve_include = 1 if ($chain eq 'PVEFW-FORWARD'); + } + + foreach my $chain (sort keys %$statushash) { + my $stat = $statushash->{$chain}; + next if ($stat->{action} eq 'delete'); + $changes = 1 if ($stat->{action} !~ 'ignore|exists'); + + foreach my $cmd (@{$statushash->{$chain}->{'rules'}}) { + if ($chain eq 'FORWARD' && $cmd eq $append_pve_to_forward) { + next if ! $pve_include; + $pve_include = 0; + } + $cmdlist .= "$cmd\n"; + } + } + $cmdlist .= "$append_pve_to_forward\n" if $pve_include; + + return wantarray ? ($cmdlist, $changes) : $cmdlist; +} + sub get_ipset_cmdlist { my ($ruleset, $verbose) = @_; @@ -3774,7 +4025,7 @@ sub get_ipset_cmdlist { } sub apply_ruleset { - my ($ruleset, $hostfw_conf, $ipset_ruleset, $rulesetv6, $verbose) = @_; + my ($ruleset, $hostfw_conf, $ipset_ruleset, $rulesetv6, $ebtables_ruleset, $verbose) = @_; enable_bridge_firewall(); @@ -3783,6 +4034,7 @@ sub apply_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); if ($verbose) { if ($ipset_changes) { @@ -3800,6 +4052,11 @@ sub apply_ruleset { print "ip6tables changes:\n"; print $cmdlistv6; } + + if ($ebtables_changes) { + print "ebtables changes:\n"; + print $ebtables_cmdlist; + } } my $tmpfile = "$pve_fw_status_dir/ipsetcmdlist1"; @@ -3822,6 +4079,11 @@ sub apply_ruleset { ipset_restore_cmdlist($ipset_delete_cmdlist) if $ipset_delete_cmdlist; + ebtables_restore_cmdlist($ebtables_cmdlist); + + $tmpfile = "$pve_fw_status_dir/ebtablescmdlist"; + PVE::Tools::file_set_contents($tmpfile, $ebtables_cmdlist || ''); + # test: re-read status and check if everything is up to date my $active_chains = iptables_get_chains(); my $statushash = get_ruleset_status($ruleset, $active_chains, \&iptables_chain_digest, 0); @@ -3846,12 +4108,26 @@ sub apply_ruleset { } } + my $active_ebtables_chains = ebtables_get_chains(); + my $ebtables_statushash = get_ruleset_status($ebtables_ruleset, + $active_ebtables_chains, \&iptables_chain_digest, + 0, $pve_ebtables_chainname_regex); + + foreach my $chain (sort keys %$ebtables_ruleset) { + my $stat = $ebtables_statushash->{$chain}; + if ($stat->{action} ne 'exists') { + warn "ebtables : unable to update chain '$chain'\n"; + $errors = 1; + } + } + die "unable to apply firewall changes\n" if $errors; update_nf_conntrack_max($hostfw_conf); update_nf_conntrack_tcp_timeout_established($hostfw_conf); + update_nf_conntrack_logging($hostfw_conf); } sub update_nf_conntrack_max { @@ -3888,6 +4164,23 @@ sub update_nf_conntrack_tcp_timeout_established { PVE::ProcFSTools::write_proc_entry("/proc/sys/net/netfilter/nf_conntrack_tcp_timeout_established", $value); } +my $log_nf_conntrack_enabled = undef; +sub update_nf_conntrack_logging { + my ($hostfw_conf) = @_; + + my $options = $hostfw_conf->{options} || {}; + my $value = $options->{log_nf_conntrack} || 0; + if (!defined($log_nf_conntrack_enabled) + || $value != $log_nf_conntrack_enabled) + { + my $tmpfile = "$pve_fw_status_dir/log_nf_conntrack"; + PVE::Tools::file_set_contents($tmpfile, $value); + + PVE::Tools::run_command([qw(systemctl try-reload-or-restart pvefw-logger.service)]); + $log_nf_conntrack_enabled = $value; + } +} + sub remove_pvefw_chains { PVE::Firewall::remove_pvefw_chains_iptables("iptables"); @@ -3961,9 +4254,9 @@ sub update { my $hostfw_conf = load_hostfw_conf($cluster_conf); - my ($ruleset, $ipset_ruleset, $rulesetv6) = compile($cluster_conf, $hostfw_conf); + my ($ruleset, $ipset_ruleset, $rulesetv6, $ebtables_ruleset) = compile($cluster_conf, $hostfw_conf); - apply_ruleset($ruleset, $hostfw_conf, $ipset_ruleset, $rulesetv6); + apply_ruleset($ruleset, $hostfw_conf, $ipset_ruleset, $rulesetv6, $ebtables_ruleset); }; run_locked($code);