X-Git-Url: https://git.proxmox.com/?p=pve-firewall.git;a=blobdiff_plain;f=src%2FPVE%2FFirewall.pm;h=8f545e7b439768f11d524e4d1031b9675066fc58;hp=ad592676d0a9b30f4bcf1b9e4a4e255f105b122c;hb=fc1f1de97e005b3d84dca3c4723708b24ba2219f;hpb=7f7930f8cf5558659e0664737f9b03811f84f3f1 diff --git a/src/PVE/Firewall.pm b/src/PVE/Firewall.pm index ad59267..8f545e7 100644 --- a/src/PVE/Firewall.pm +++ b/src/PVE/Firewall.pm @@ -20,6 +20,7 @@ use IO::File; use Net::IP; use PVE::Tools qw(run_command lock_file dir_glob_foreach); use Encode; +use Storable qw(dclone); my $hostfw_conf_filename = "/etc/pve/local/host.fw"; my $pvefw_conf_dir = "/etc/pve/firewall"; @@ -142,6 +143,21 @@ my $log_level_hash = { emerg => 0, }; +# %rule +# +# name => optional +# enable => [0|1] +# action => +# proto => +# sport => port[,port[,port]].. or port:port +# dport => port[,port[,port]].. or port:port +# log => optional, loglevel +# logmsg => optional, logmsg - overwrites default +# iface_in => incomin interface +# iface_out => outgoing interface +# match => optional, overwrites generation of match +# target => optional, overwrites action + # we need to overwrite some macros for ipv6 my $pve_ipv6fw_macros = { 'Ping' => [ @@ -534,9 +550,10 @@ my $FWACCEPTMARK_ON = "0x80000000/0x80000000"; my $FWACCEPTMARK_OFF = "0x00000000/0x80000000"; my $pve_std_chains = {}; -$pve_std_chains->{4} = { +my $pve_std_chains_conf = {}; +$pve_std_chains_conf->{4} = { 'PVEFW-SET-ACCEPT-MARK' => [ - "-j MARK --set-mark $FWACCEPTMARK_ON", + { target => "-j MARK --set-mark $FWACCEPTMARK_ON" }, ], 'PVEFW-DropBroadcast' => [ # same as shorewall 'Broadcast' @@ -552,10 +569,10 @@ $pve_std_chains->{4} = { { action => 'DROP', dsttype => 'BROADCAST' }, { action => 'DROP', source => '224.0.0.0/4' }, { action => 'DROP', proto => 'icmp' }, - "-p tcp -j REJECT --reject-with tcp-reset", - "-p udp -j REJECT --reject-with icmp-port-unreachable", - "-p icmp -j REJECT --reject-with icmp-host-unreachable", - "-j REJECT --reject-with icmp-host-prohibited", + { match => '-p tcp', target => '-j REJECT --reject-with tcp-reset' }, + { match => '-p udp', target => '-j REJECT --reject-with icmp-port-unreachable' }, + { match => '-p icmp', target => '-j REJECT --reject-with icmp-host-unreachable' }, + { target => '-j REJECT --reject-with icmp-host-prohibited' }, ], 'PVEFW-Drop' => [ # same as shorewall 'Drop', which is equal to DROP, @@ -568,15 +585,15 @@ $pve_std_chains->{4} = { { action => 'ACCEPT', proto => 'icmp', dport => 'fragmentation-needed' }, { action => 'ACCEPT', proto => 'icmp', dport => 'time-exceeded' }, # Drop packets with INVALID state - "-m conntrack --ctstate INVALID -j DROP", + { action => 'DROP', match => '-m conntrack --ctstate INVALID', }, # Drop Microsoft SMB noise - { action => 'DROP', proto => 'udp', dport => '135,445', nbdport => 2 }, - { action => 'DROP', proto => 'udp', dport => '137:139'}, + { action => 'DROP', proto => 'udp', dport => '135,445' }, + { action => 'DROP', proto => 'udp', dport => '137:139' }, { action => 'DROP', proto => 'udp', dport => '1024:65535', sport => 137 }, - { action => 'DROP', proto => 'tcp', dport => '135,139,445', nbdport => 3 }, + { action => 'DROP', proto => 'tcp', dport => '135,139,445' }, { action => 'DROP', proto => 'udp', dport => 1900 }, # UPnP # Drop new/NotSyn traffic so that it doesn't get logged - "-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -j DROP", + { action => 'DROP', match => '-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN' }, # Drop DNS replies { action => 'DROP', proto => 'udp', sport => 53 }, ], @@ -591,119 +608,126 @@ $pve_std_chains->{4} = { { action => 'ACCEPT', proto => 'icmp', dport => 'fragmentation-needed' }, { action => 'ACCEPT', proto => 'icmp', dport => 'time-exceeded' }, # Drop packets with INVALID state - "-m conntrack --ctstate INVALID -j DROP", + { action => 'DROP', match => '-m conntrack --ctstate INVALID', }, # Drop Microsoft SMB noise - { action => 'PVEFW-reject', proto => 'udp', dport => '135,445', nbdport => 2 }, + { action => 'PVEFW-reject', proto => 'udp', dport => '135,445' }, { action => 'PVEFW-reject', proto => 'udp', dport => '137:139'}, { action => 'PVEFW-reject', proto => 'udp', dport => '1024:65535', sport => 137 }, - { action => 'PVEFW-reject', proto => 'tcp', dport => '135,139,445', nbdport => 3 }, + { action => 'PVEFW-reject', proto => 'tcp', dport => '135,139,445' }, { action => 'DROP', proto => 'udp', dport => 1900 }, # UPnP # Drop new/NotSyn traffic so that it doesn't get logged - "-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -j DROP", + { action => 'DROP', match => '-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN' }, # Drop DNS replies { action => 'DROP', proto => 'udp', sport => 53 }, ], 'PVEFW-tcpflags' => [ # same as shorewall tcpflags action. # Packets arriving on this interface are checked for som illegal combinations of TCP flags - "-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG FIN,PSH,URG -g PVEFW-logflags", - "-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG NONE -g PVEFW-logflags", - "-p tcp -m tcp --tcp-flags SYN,RST SYN,RST -g PVEFW-logflags", - "-p tcp -m tcp --tcp-flags FIN,SYN FIN,SYN -g PVEFW-logflags", - "-p tcp -m tcp --sport 0 --tcp-flags FIN,SYN,RST,ACK SYN -g PVEFW-logflags", + { match => '-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG FIN,PSH,URG', target => '-g PVEFW-logflags' }, + { match => '-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG NONE', target => '-g PVEFW-logflags' }, + { match => '-p tcp -m tcp --tcp-flags SYN,RST SYN,RST', target => '-g PVEFW-logflags' }, + { match => '-p tcp -m tcp --tcp-flags FIN,SYN FIN,SYN', target => '-g PVEFW-logflags' }, + { match => '-p tcp -m tcp --sport 0 --tcp-flags FIN,SYN,RST,ACK SYN', target => '-g PVEFW-logflags' }, ], 'PVEFW-smurfs' => [ # same as shorewall smurfs action # Filter packets for smurfs (packets with a broadcast address as the source). - "-s 0.0.0.0/32 -j RETURN", # allow DHCP - "-m addrtype --src-type BROADCAST -g PVEFW-smurflog", - "-s 224.0.0.0/4 -g PVEFW-smurflog", + { match => '-s 0.0.0.0/32', target => '-j RETURN' }, # allow DHCP + { match => '-m addrtype --src-type BROADCAST', target => '-g PVEFW-smurflog' }, + { match => '-s 224.0.0.0/4', target => '-g PVEFW-smurflog' }, + ], + 'PVEFW-smurflog' => [ + { action => 'DROP', logmsg => 'DROP: ' }, + ], + 'PVEFW-logflags' => [ + { action => 'DROP', logmsg => 'DROP: ' }, ], }; -$pve_std_chains->{6} = { +$pve_std_chains_conf->{6} = { 'PVEFW-SET-ACCEPT-MARK' => [ - "-j MARK --set-mark $FWACCEPTMARK_ON", + { target => "-j MARK --set-mark $FWACCEPTMARK_ON" }, ], 'PVEFW-DropBroadcast' => [ - # same as shorewall 'Broadcast' - # simply DROP BROADCAST/MULTICAST/ANYCAST - # we can use this to reduce logging - #{ action => 'DROP', dsttype => 'BROADCAST' }, #no broadcast in ipv6 + # same as shorewall 'Broadcast' + # simply DROP BROADCAST/MULTICAST/ANYCAST + # we can use this to reduce logging + #{ action => 'DROP', dsttype => 'BROADCAST' }, #no broadcast in ipv6 # ipv6 addrtype does not work with kernel 2.6.32 #{ action => 'DROP', dsttype => 'MULTICAST' }, - #{ action => 'DROP', dsttype => 'ANYCAST' }, - { action => 'DROP', dest => 'ff00::/8' }, - #{ action => 'DROP', dest => '224.0.0.0/4' }, + #{ action => 'DROP', dsttype => 'ANYCAST' }, + { action => 'DROP', dest => 'ff00::/8' }, + #{ action => 'DROP', dest => '224.0.0.0/4' }, ], 'PVEFW-reject' => [ - # same as shorewall 'reject' - #{ action => 'DROP', dsttype => 'BROADCAST' }, - #{ action => 'DROP', source => '224.0.0.0/4' }, + # same as shorewall 'reject' + #{ action => 'DROP', dsttype => 'BROADCAST' }, + #{ action => 'DROP', source => '224.0.0.0/4' }, { action => 'DROP', proto => 'icmpv6' }, - "-p tcp -j REJECT --reject-with tcp-reset", - #"-p udp -j REJECT --reject-with icmp-port-unreachable", - #"-p icmp -j REJECT --reject-with icmp-host-unreachable", - #"-j REJECT --reject-with icmp-host-prohibited", + { match => '-p tcp', target => '-j REJECT --reject-with tcp-reset' }, + #"-p udp -j REJECT --reject-with icmp-port-unreachable", + #"-p icmp -j REJECT --reject-with icmp-host-unreachable", + #"-j REJECT --reject-with icmp-host-prohibited", ], 'PVEFW-Drop' => [ - # same as shorewall 'Drop', which is equal to DROP, - # but REJECT/DROP some packages to reduce logging, - # and ACCEPT critical ICMP types + # same as shorewall 'Drop', which is equal to DROP, + # but REJECT/DROP some packages to reduce logging, + # and ACCEPT critical ICMP types { action => 'PVEFW-reject', proto => 'tcp', dport => '43' }, # REJECT 'auth' - # we are not interested in BROADCAST/MULTICAST/ANYCAST - { action => 'PVEFW-DropBroadcast' }, - # ACCEPT critical ICMP types - { action => 'ACCEPT', proto => 'icmpv6', dport => 'destination-unreachable' }, - { action => 'ACCEPT', proto => 'icmpv6', dport => 'time-exceeded' }, - { action => 'ACCEPT', proto => 'icmpv6', dport => 'packet-too-big' }, - - # Drop packets with INVALID state - "-m conntrack --ctstate INVALID -j DROP", - # Drop Microsoft SMB noise - { action => 'DROP', proto => 'udp', dport => '135,445', nbdport => 2 }, + # we are not interested in BROADCAST/MULTICAST/ANYCAST + { action => 'PVEFW-DropBroadcast' }, + # ACCEPT critical ICMP types + { action => 'ACCEPT', proto => 'icmpv6', dport => 'destination-unreachable' }, + { action => 'ACCEPT', proto => 'icmpv6', dport => 'time-exceeded' }, + { action => 'ACCEPT', proto => 'icmpv6', dport => 'packet-too-big' }, + # Drop packets with INVALID state + { action => 'DROP', match => '-m conntrack --ctstate INVALID', }, + # Drop Microsoft SMB noise + { action => 'DROP', proto => 'udp', dport => '135,445' }, { action => 'DROP', proto => 'udp', dport => '137:139'}, { action => 'DROP', proto => 'udp', dport => '1024:65535', sport => 137 }, - { action => 'DROP', proto => 'tcp', dport => '135,139,445', nbdport => 3 }, + { action => 'DROP', proto => 'tcp', dport => '135,139,445' }, { action => 'DROP', proto => 'udp', dport => 1900 }, # UPnP - # Drop new/NotSyn traffic so that it doesn't get logged - "-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -j DROP", - # Drop DNS replies + # Drop new/NotSyn traffic so that it doesn't get logged + { action => 'DROP', match => '-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN' }, + # Drop DNS replies { action => 'DROP', proto => 'udp', sport => 53 }, ], 'PVEFW-Reject' => [ - # same as shorewall 'Reject', which is equal to Reject, - # but REJECT/DROP some packages to reduce logging, - # and ACCEPT critical ICMP types - { action => 'PVEFW-reject', proto => 'tcp', dport => '43' }, # REJECT 'auth' - # we are not interested in BROADCAST/MULTICAST/ANYCAST - { action => 'PVEFW-DropBroadcast' }, - # ACCEPT critical ICMP types - { action => 'ACCEPT', proto => 'icmpv6', dport => 'destination-unreachable' }, - { action => 'ACCEPT', proto => 'icmpv6', dport => 'time-exceeded' }, - { action => 'ACCEPT', proto => 'icmpv6', dport => 'packet-too-big' }, - - # Drop packets with INVALID state - "-m conntrack --ctstate INVALID -j DROP", - # Drop Microsoft SMB noise - { action => 'PVEFW-reject', proto => 'udp', dport => '135,445', nbdport => 2 }, - { action => 'PVEFW-reject', proto => 'udp', dport => '137:139'}, - { action => 'PVEFW-reject', proto => 'udp', dport => '1024:65535', sport => 137 }, - { action => 'PVEFW-reject', proto => 'tcp', dport => '135,139,445', nbdport => 3 }, - { action => 'DROP', proto => 'udp', dport => 1900 }, # UPnP - # Drop new/NotSyn traffic so that it doesn't get logged - "-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -j DROP", - # Drop DNS replies - { action => 'DROP', proto => 'udp', sport => 53 }, + # same as shorewall 'Reject', which is equal to Reject, + # but REJECT/DROP some packages to reduce logging, + # and ACCEPT critical ICMP types + { action => 'PVEFW-reject', proto => 'tcp', dport => '43' }, # REJECT 'auth' + # we are not interested in BROADCAST/MULTICAST/ANYCAST + { action => 'PVEFW-DropBroadcast' }, + # ACCEPT critical ICMP types + { action => 'ACCEPT', proto => 'icmpv6', dport => 'destination-unreachable' }, + { action => 'ACCEPT', proto => 'icmpv6', dport => 'time-exceeded' }, + { action => 'ACCEPT', proto => 'icmpv6', dport => 'packet-too-big' }, + # Drop packets with INVALID state + { action => 'DROP', match => '-m conntrack --ctstate INVALID', }, + # Drop Microsoft SMB noise + { action => 'PVEFW-reject', proto => 'udp', dport => '135,445' }, + { action => 'PVEFW-reject', proto => 'udp', dport => '137:139' }, + { action => 'PVEFW-reject', proto => 'udp', dport => '1024:65535', sport => 137 }, + { action => 'PVEFW-reject', proto => 'tcp', dport => '135,139,445' }, + { action => 'DROP', proto => 'udp', dport => 1900 }, # UPnP + # Drop new/NotSyn traffic so that it doesn't get logged + { action => 'DROP', match => '-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN' }, + # Drop DNS replies + { action => 'DROP', proto => 'udp', sport => 53 }, ], 'PVEFW-tcpflags' => [ - # same as shorewall tcpflags action. - # Packets arriving on this interface are checked for som illegal combinations of TCP flags - "-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG FIN,PSH,URG -g PVEFW-logflags", - "-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG NONE -g PVEFW-logflags", - "-p tcp -m tcp --tcp-flags SYN,RST SYN,RST -g PVEFW-logflags", - "-p tcp -m tcp --tcp-flags FIN,SYN FIN,SYN -g PVEFW-logflags", - "-p tcp -m tcp --sport 0 --tcp-flags FIN,SYN,RST,ACK SYN -g PVEFW-logflags", + # same as shorewall tcpflags action. + # Packets arriving on this interface are checked for som illegal combinations of TCP flags + { match => '-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG FIN,PSH,URG', target => '-g PVEFW-logflags' }, + { match => '-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG NONE', target => '-g PVEFW-logflags' }, + { match => '-p tcp -m tcp --tcp-flags SYN,RST SYN,RST', target => '-g PVEFW-logflags' }, + { match => '-p tcp -m tcp --tcp-flags FIN,SYN FIN,SYN', target => '-g PVEFW-logflags' }, + { match => '-p tcp -m tcp --sport 0 --tcp-flags FIN,SYN,RST,ACK SYN', target => '-g PVEFW-logflags' }, + ], + 'PVEFW-logflags' => [ + { action => 'DROP', logmsg => 'DROP: ' }, ], }; @@ -836,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; @@ -852,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) { @@ -872,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}; } @@ -880,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}; @@ -889,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 { @@ -1011,12 +1049,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) { - $count++; if ($item =~ m/^(\d+):(\d+)$/) { + $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+)$/) { + $count += 1; my $port = $1; die "invalid port '$port'\n" if $port > 65535; } else { @@ -1030,9 +1069,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; + + # 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 $count; + return (scalar(@elements) > 1); } PVE::JSONSchema::register_format('pve-fw-sport-spec', \&pve_fw_verify_sport_spec); @@ -1666,6 +1711,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) = @_; @@ -1776,167 +1827,193 @@ 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 { + + my $res = {}; + my $chains = {}; - return if defined($rule->{enable}) && !$rule->{enable}; - return if $rule->{errors}; + 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(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+).*/) { + my $chain = $1; + $line =~ s/\s+$//; + push @{$chains->{$chain}}, $line; + } else { + # simply ignore the rest + return; + } + }; - die "unable to emit macro - internal error" if $rule->{macro}; # should not happen + run_command("/sbin/ebtables-save", outfunc => $parser); - 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; + # compute digest for each chain + foreach my $chain (keys %$chains) { + $res->{$chain} = 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"; - } - } else { - die "invalid security group name '$source'\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"; - } else { - push @cmd, "-s $source"; - } + my $srcdst; + if ($dir eq 's') { + $srcdst = "src"; + } elsif ($dir eq 'd') { + $srcdst = "dst"; + } else { + die "ipt_gen_src_or_dst_match: invalid direction $dir \n"; } - if ($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"; - } + 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 '$dest'\n"; + die "no such ipset '$name'\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"; + $match = "-m set --match-set ${ipset_chain} ${srcdst}"; } else { - push @cmd, "-d $dest"; - } + 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 (my $proto = $rule->{proto}) { - push @cmd, "-p $proto"; + return $match; +} - my $multiport = 0; - $multiport++ if $nbdport > 1; - $multiport++ if $nbsport > 1; +# convert a %rule to an array of iptables commands +sub ipt_rule_to_cmds { + my ($rule, $chain, $ipversion, $cluster_conf, $fw_conf, $vmid) = @_; - push @cmd, "--match multiport" if $multiport; + die "ipt_rule_to_cmds unable to handle macro" if $rule->{macro}; #should not happen - die "multiport: option '--sports' cannot be used together with '--dports'\n" - if ($multiport == 2) && ($rule->{dport} ne $rule->{sport}); + my @match = (); - 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 (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->{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->{source}) { + push @match, ipt_gen_src_or_dst_match($rule->{source}, 's', $ipversion, $cluster_conf, $fw_conf); + } + if ($rule->{dest}) { + push @match, ipt_gen_src_or_dst_match($rule->{dest}, 'd', $ipversion, $cluster_conf, $fw_conf); } - } 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}; + 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}"; + } + }; - return scalar(@cmd) ? join(' ', @cmd) : undef; -} + my $add_sport = sub { + return if !$rule->{sport}; -sub ruleset_generate_action { - my ($ruleset, $chain, $ipversion, $rule, $actions, $goto, $cluster_conf, $fw_conf) = @_; + 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; + if (defined $rule->{log} && $rule->{log}) { + my $logaction = get_log_rule_base($chain, $vmid, $rule->{logmsg}, $rule->{log}); + push @iptcmds, "-A $chain $matchstr $logaction"; + } + push @iptcmds, "-A $chain $matchstr $targetstr"; + return @iptcmds; } sub ruleset_generate_rule { - my ($ruleset, $chain, $ipversion, $rule, $actions, $goto, $cluster_conf, $fw_conf) = @_; + my ($ruleset, $chain, $ipversion, $rule, $cluster_conf, $fw_conf) = @_; my $rules; @@ -1947,32 +2024,12 @@ sub ruleset_generate_rule { } # update all or nothing - - my @mstrs = (); - my @astrs = (); - 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 : ""; - } + my @ipt_rule_cmds; + foreach my $r (@$rules) { + push @ipt_rule_cmds, ipt_rule_to_cmds($r, $chain, $ipversion, $cluster_conf, $fw_conf); } - - for my $i (0 .. $#mstrs) { - ruleset_addrule($ruleset, $chain, $mstrs[$i], $astrs[$i]); - } -} - -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); } } @@ -1993,12 +2050,13 @@ sub ruleset_chain_exist { return $ruleset->{$chain} ? 1 : undef; } -sub ruleset_addrule_old { - my ($ruleset, $chain, $rule) = @_; +# add an iptables command (like generated by ipt_rule_to_cmds) to a chain +sub ruleset_add_ipt_cmd { + my ($ruleset, $chain, $iptcmd) = @_; die "no such chain '$chain'\n" if !$ruleset->{$chain}; - push @{$ruleset->{$chain}}, "-A $chain $rule"; + push @{$ruleset->{$chain}}, $iptcmd; } sub ruleset_addrule { @@ -2010,7 +2068,9 @@ sub ruleset_addrule { my $logaction = get_log_rule_base($chain, $vmid, $logmsg, $log); 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 { @@ -2038,8 +2098,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') { @@ -2185,13 +2246,11 @@ sub ruleset_generate_vm_rules { next if $rule->{type} ne $lc_direction; eval { if ($direction eq 'OUT') { - ruleset_generate_rule($ruleset, $chain, $ipversion, $rule, - { ACCEPT => "PVEFW-SET-ACCEPT-MARK", REJECT => "PVEFW-reject" }, - undef, $cluster_conf, $vmfw_conf); + rule_substitude_action($rule, { ACCEPT => "PVEFW-SET-ACCEPT-MARK", REJECT => "PVEFW-reject" }); + ruleset_generate_rule($ruleset, $chain, $ipversion, $rule, $cluster_conf, $vmfw_conf); } else { - ruleset_generate_rule($ruleset, $chain, $ipversion, $rule, - { ACCEPT => $in_accept , REJECT => "PVEFW-reject" }, - undef, $cluster_conf, $vmfw_conf); + rule_substitude_action($rule, { ACCEPT => $in_accept , REJECT => "PVEFW-reject" }); + ruleset_generate_rule($ruleset, $chain, $ipversion, $rule, $cluster_conf, $vmfw_conf); } }; warn $@ if $@; @@ -2319,9 +2378,8 @@ sub enable_host_firewall { if ($rule->{type} eq 'group') { ruleset_add_group_rule($ruleset, $cluster_conf, $chain, $rule, 'IN', $accept_action, $ipversion); } elsif ($rule->{type} eq 'in') { - ruleset_generate_rule($ruleset, $chain, $ipversion, $rule, - { ACCEPT => $accept_action, REJECT => "PVEFW-reject" }, - undef, $cluster_conf, $hostfw_conf); + rule_substitude_action($rule, { ACCEPT => $accept_action, REJECT => "PVEFW-reject" }); + ruleset_generate_rule($ruleset, $chain, $ipversion, $rule, $cluster_conf, $hostfw_conf); } }; warn $@ if $@; @@ -2376,9 +2434,8 @@ sub enable_host_firewall { if ($rule->{type} eq 'group') { ruleset_add_group_rule($ruleset, $cluster_conf, $chain, $rule, 'OUT', $accept_action, $ipversion); } elsif ($rule->{type} eq 'out') { - ruleset_generate_rule($ruleset, $chain, $ipversion, - $rule, { ACCEPT => $accept_action, REJECT => "PVEFW-reject" }, - undef, $cluster_conf, $hostfw_conf); + rule_substitude_action($rule, { ACCEPT => $accept_action, REJECT => "PVEFW-reject" }); + ruleset_generate_rule($ruleset, $chain, $ipversion, $rule, $cluster_conf, $hostfw_conf); } }; warn $@ if $@; @@ -2422,10 +2479,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"; @@ -2435,12 +2492,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); } } @@ -2538,6 +2595,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) = @_; @@ -2557,6 +2622,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" } @@ -3127,26 +3196,21 @@ sub generate_std_chains { my $std_chains = $pve_std_chains->{$ipversion} || die "internal error"; my $loglevel = get_option_log_level($options, 'smurf_log_level'); - - my $chain; - - if ($ipversion == 4) { - # same as shorewall smurflog. - $chain = 'PVEFW-smurflog'; - $std_chains->{$chain} = []; - - push @{$std_chains->{$chain}}, get_log_rule_base($chain, 0, "DROP: ", $loglevel) if $loglevel; - push @{$std_chains->{$chain}}, "-j DROP"; + my $chain = 'PVEFW-smurflog'; + if ( $std_chains->{$chain} ) { + foreach my $r (@{$std_chains->{$chain}}) { + $r->{log} = $loglevel; + } } # same as shorewall logflags action. $loglevel = get_option_log_level($options, 'tcp_flags_log_level'); $chain = 'PVEFW-logflags'; - $std_chains->{$chain} = []; - - # fixme: is this correctly logged by pvewf-logger? (ther is no --log-ip-options for NFLOG) - push @{$std_chains->{$chain}}, get_log_rule_base($chain, 0, "DROP: ", $loglevel) if $loglevel; - push @{$std_chains->{$chain}}, "-j DROP"; + if ( $std_chains->{$chain} ) { + foreach my $r (@{$std_chains->{$chain}}) { + $r->{log} = $loglevel; + } + } foreach my $chain (keys %$std_chains) { ruleset_create_chain($ruleset, $chain); @@ -3154,7 +3218,7 @@ sub generate_std_chains { if (ref($rule)) { ruleset_generate_rule($ruleset, $chain, $ipversion, $rule); } else { - ruleset_addrule_old($ruleset, $chain, $rule); + die "rule $rule as string - should not happen"; } } } @@ -3337,6 +3401,9 @@ sub compile { my $vmfw_configs; + # fixme: once we read standard chains from config this needs to be put in test/standard cases below + $pve_std_chains = dclone($pve_std_chains_conf); + if ($vmdata) { # test mode my $testdir = $vmdata->{testdir} || die "no test directory specified"; my $filename = "$testdir/cluster.fw"; @@ -3372,9 +3439,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 { @@ -3586,6 +3654,94 @@ sub compile_ipsets { return $ipset_ruleset; } +sub compile_ebtables_filter { + my ($cluster_conf, $hostfw_conf, $vmfw_configs, $vmdata, $verbose) = @_; + + return ({}, {}) if !$cluster_conf->{options}->{enable}; + + 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 (keys %{$vmdata->{qemu}}) { + eval { + my $conf = $vmdata->{qemu}->{$vmid}; + my $vmfw_conf = $vmfw_configs->{$vmid}; + return if !$vmfw_conf; + + foreach my $netid (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); + + } + }; + warn $@ if $@; # just to be sure - should not happen + } + + # generate firewall rules for LXC containers + foreach my $vmid (keys %{$vmdata->{lxc}}) { + eval { + my $conf = $vmdata->{lxc}->{$vmid}; + + my $vmfw_conf = $vmfw_configs->{$vmid}; + return if !$vmfw_conf || !$vmfw_conf->{options}->{enable}; + + foreach my $netid (keys %$conf) { + next if $netid !~ m/^net(\d+)$/; + my $net = PVE::LXC::Config->parse_lxc_network($conf->{$netid}); + next if !$net->{firewall}; + my $iface = "veth${vmid}i$1"; + my $macaddr = $net->{hwaddr}; + generate_tap_layer2filter($ruleset, $iface, $macaddr, $vmfw_conf, $vmid); + } + }; + warn $@ if $@; # just to be sure - should not happen + } + + return $ruleset; +} + +sub generate_tap_layer2filter { + my ($ruleset, $iface, $macaddr, $vmfw_conf, $vmid) = @_; + 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 (defined($options->{layer2_protocols})){ + foreach my $proto (split(/,/, $options->{layer2_protocols})) { + ruleset_addrule($ruleset, $tapchain, "-p $proto", '-j ACCEPT'); + } + ruleset_addrule($ruleset, $tapchain, '', "-j DROP"); + } else { + ruleset_addrule($ruleset, $tapchain, '', '-j ACCEPT'); + } + + ruleset_addrule($ruleset, 'PVEFW-FWBR-OUT', "-i $iface", "-j $tapchain"); +} + sub get_ruleset_status { my ($ruleset, $active_chains, $digest_fn, $verbose) = @_; @@ -3693,6 +3849,39 @@ sub get_ruleset_cmdlist { return wantarray ? ($cmdlist, $changes) : $cmdlist; } +sub get_ebtables_cmdlist { + my ($ruleset, $verbose) = @_; + + 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"; + } + + if ($ruleset->{FORWARD}) { + $cmdlist .= "-A FORWARD -j PVEFW-FORWARD\n"; + } + + foreach my $chain (sort keys %$ruleset) { + my $stat = $statushash->{$chain}; + die "internal error" if !$stat; + $changes = 1 if ($stat->{action} ne 'exists'); + + foreach my $cmd (@{$ruleset->{$chain}}) { + $cmdlist .= "$cmd\n"; + } + } + + return wantarray ? ($cmdlist, $changes) : $cmdlist; +} + sub get_ipset_cmdlist { my ($ruleset, $verbose) = @_; @@ -3752,7 +3941,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(); @@ -3761,6 +3950,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) { @@ -3778,6 +3968,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"; @@ -3800,6 +3995,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); @@ -3824,6 +4024,17 @@ 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); + + 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); @@ -3939,9 +4150,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);