diff --git a/perl-xCAT/xCAT/NetworkUtils.pm b/perl-xCAT/xCAT/NetworkUtils.pm index c401f562b6..dc3752e4ab 100644 --- a/perl-xCAT/xCAT/NetworkUtils.pm +++ b/perl-xCAT/xCAT/NetworkUtils.pm @@ -438,6 +438,11 @@ sub getipaddr return; } + # getaddrinfo returns every address of the host, link-local included, + # and a caller that asked for no family takes the first one. Order the + # entries the way prefer_routable_addresses orders their literals. + @addrinfo = _addrinfo_routable_first(@addrinfo); + my @returns; foreach my $address (@addrinfo) { @@ -2668,6 +2673,88 @@ sub isIpv6addr return defined($packed_address) ? 1 : 0; } +#------------------------------------------------------------------------------- +# A link-local address reaches one link only, so it cannot serve as a cluster +# address: fe80::/10 for IPv6, 169.254.0.0/16 for IPv4. +sub _is_link_local +{ + my $address = shift; + return 0 unless defined($address) && length($address); + return 1 if $address =~ /^fe[89ab][0-9a-f]:/i; + return 1 if $address =~ /^169\.254\./; + return 0; +} + +#------------------------------------------------------------------------------- +# Order getaddrinfo entries by the same rule prefer_routable_addresses applies +# to literals. An entry whose address cannot be read keeps its place at the end. +sub _addrinfo_routable_first +{ + my @addrinfo = @_; + my (@routable, @linklocal, @unreadable); + foreach my $address (@addrinfo) + { + my ($name_error, $numeric_ip) = + Socket::getnameinfo($address->{addr}, Socket::NI_NUMERICHOST()); + if ($name_error || !defined($numeric_ip)) + { + push @unreadable, $address; + } + elsif (_is_link_local($numeric_ip)) + { + push @linklocal, $address; + } + else + { + push @routable, $address; + } + } + return (@routable, @linklocal, @unreadable); +} + +#------------------------------------------------------------------------------- + +=head3 prefer_routable_addresses + + Orders resolved addresses so a routable one comes before a link-local one, + and removes the IPv6 zone identifier. + + Arguments: + list of address literals, in resolver order + Returns: + the same addresses, routable ones first, order kept inside each group + Comments: + A host resolves its own name to every address on it, and a link-local + address is often first. site.master takes that answer and reaches every + node, which cannot use fe80::/10 across a link or parse the %zone form. + +=cut + +#------------------------------------------------------------------------------- +sub prefer_routable_addresses +{ + my @addresses = @_; + shift @addresses + if @addresses && defined($addresses[0]) && $addresses[0] eq __PACKAGE__; + + my (@routable, @linklocal); + foreach my $address (@addresses) + { + next unless defined($address) && length($address); + $address =~ s/%.*$//; + next unless length($address); + if (_is_link_local($address)) + { + push @linklocal, $address; + } + else + { + push @routable, $address; + } + } + return (@routable, @linklocal); +} + #------------------------------------------------------------------------------- =head3 isValidIp diff --git a/xCAT-test/unit/networkutils_linklocal_preference.t b/xCAT-test/unit/networkutils_linklocal_preference.t new file mode 100644 index 0000000000..7363765c00 --- /dev/null +++ b/xCAT-test/unit/networkutils_linklocal_preference.t @@ -0,0 +1,67 @@ +#!/usr/bin/env perl +# A management node resolves its own name to every address on it, and a +# link-local address is usually first. xcatconfig writes that answer into +# site.master and site.nameservers, which reach every node through DHCP, DNS +# and the kernel command line, so a link-local address there is unusable: a +# node on another link cannot reach it, and the %zone form is not a literal any +# consumer accepts. +use strict; +use warnings; + +use FindBin; +use lib "$FindBin::Bin/../../perl-xCAT"; + +use Test::More; +use xCAT::NetworkUtils; + +my @cases = ( + [ 'a global IPv6 address outranks a link-local one', + [ 'fe80::4054:ff:fec9:9ad2', '2001:db8::50' ], + '2001:db8::50', + ], + [ 'an IPv4 address outranks a link-local IPv6 address', + [ 'fe80::4054:ff:fec9:9ad2', '192.0.2.154' ], + '192.0.2.154', + ], + [ 'the zone identifier is removed from the address that is returned', + [ 'fe80::4054:ff:fec9:9ad2%mgmt0' ], + 'fe80::4054:ff:fec9:9ad2', + ], + [ 'a link-local address is still returned when it is the only one', + [ 'fe80::1%eth0' ], + 'fe80::1', + ], + [ 'an IPv4 link-local address outranks nothing but another link-local', + [ '169.254.1.1', '192.0.2.154' ], + '192.0.2.154', + ], + [ 'the first address wins when none is link-local', + [ '2001:db8::50', '192.0.2.154' ], + '2001:db8::50', + ], + [ 'order is kept among link-local addresses', + [ 'fe80::2', 'fe80::3' ], + 'fe80::2', + ], +); + +foreach my $case (@cases) { + my ( $why, $addresses, $expected ) = @{$case}; + my @ordered = xCAT::NetworkUtils->prefer_routable_addresses( @{$addresses} ); + is( $ordered[0], $expected, $why ); +} + +is_deeply( + [ xCAT::NetworkUtils->prefer_routable_addresses( + 'fe80::1%eth0', '192.0.2.154', 'fe80::2', '2001:db8::50' ) ], + [ '192.0.2.154', '2001:db8::50', 'fe80::1', 'fe80::2' ], + 'every routable address comes before every link-local one, order kept inside each group' +); + +is_deeply( [ xCAT::NetworkUtils->prefer_routable_addresses() ], + [], 'an empty list stays empty' ); +is_deeply( + [ xCAT::NetworkUtils->prefer_routable_addresses( undef, '', '192.0.2.154' ) ], + ['192.0.2.154'], 'undef and empty entries are dropped' ); + +done_testing();