2
0
mirror of https://github.com/xcat2/xcat-core.git synced 2026-11-20 01:12:18 +00:00

fix(ipv6): the link-local order is applied to one resolver path of two

getipaddr reaches the resolver through Socket6 when that module is present and
through core Socket otherwise, and only the core path ordered its results. A
management node with Socket6 installed therefore still answered
fe80::4054:ff:fec9:9ad2%mgmt0 for its own name, which is what xcatconfig wrote
into site.master. Measured on an AlmaLinux 10.2 node with perl-Socket6: the
previous commit was installed and getipaddr returned the link-local address
unchanged.

_socket6_addrinfo_routable_first reorders the flat five-element records
Socket6::getaddrinfo answers, by the same rule the core path uses. A list whose
length is not a whole number of records is returned untouched.

networkutils_linklocal_preference.t covers the record reorder with a stubbed
Socket6::getnameinfo. Making the link-local test constant false fails 2 of its
assertions. The call site itself has no unit test; the cluster run is what
shows site.master routable.

Signed-off-by: Daniel Hilst <392820+dhilst@users.noreply.github.com>
This commit is contained in:
Daniel Hilst
2026-10-21 18:59:07 -03:00
parent f1016503ca
commit d9763bd2e3
2 changed files with 74 additions and 0 deletions
+31
View File
@@ -366,6 +366,9 @@ sub getipaddr
}else{
@addrinfo=Socket6::getaddrinfo($iporhost, 0, $reqfamily, SOCK_STREAM, 6);
}
# The resolver lists every address of the host, link-local included,
# and the first one is what a caller that asked for no family gets.
@addrinfo = _socket6_addrinfo_routable_first(@addrinfo);
my ($family, $socket, $protocol, $ip, $name) = splice(@addrinfo, 0, 5);
unless($reqfamily == AF_INET6){
if($isip){
@@ -2685,6 +2688,34 @@ sub _is_link_local
return 0;
}
#-------------------------------------------------------------------------------
# Socket6::getaddrinfo answers a flat list of five-element records. Reorder the
# records, not the elements, by the rule prefer_routable_addresses applies.
sub _socket6_addrinfo_routable_first
{
my @addrinfo = @_;
return @addrinfo if scalar(@addrinfo) % 5;
my (@routable, @linklocal, @unreadable);
while (my @record = splice(@addrinfo, 0, 5))
{
my $numeric_ip = (Socket6::getnameinfo($record[3], Socket6::NI_NUMERICHOST()))[0];
if (!defined($numeric_ip))
{
push @unreadable, @record;
}
elsif (_is_link_local($numeric_ip))
{
push @linklocal, @record;
}
else
{
push @routable, @record;
}
}
return (@routable, @linklocal, @unreadable);
}
#-------------------------------------------------------------------------------
# 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.
@@ -60,6 +60,49 @@ is_deeply(
is_deeply( [ xCAT::NetworkUtils->prefer_routable_addresses() ],
[], 'an empty list stays empty' );
# getipaddr reaches the resolver through one of two code paths, and only the
# Socket6 one runs on a management node that has Socket6 installed. Both order
# their results, so both are covered here. Socket6::getaddrinfo answers a flat
# list of five-element records, so the records move and their elements do not.
{
no warnings 'once', 'redefine';
local *Socket6::NI_NUMERICHOST = sub { 1 };
local *Socket6::getnameinfo = sub {
my %address_of = (
PACKED_LINKLOCAL => 'fe80::4054:ff:fec9:9ad2',
PACKED_GLOBAL => '2001:db8::50',
PACKED_V4 => '192.0.2.154',
PACKED_BROKEN => undef,
);
return ( $address_of{ $_[0] } );
};
my @flat = (
10, 1, 6, 'PACKED_LINKLOCAL', 'linklocal.example',
2, 1, 6, 'PACKED_V4', 'v4.example',
10, 1, 6, 'PACKED_GLOBAL', 'global.example',
);
my @ordered = xCAT::NetworkUtils::_socket6_addrinfo_routable_first(@flat);
is( scalar(@ordered), 15, 'every record survives the reorder' );
is_deeply( [ @ordered[ 3, 8, 13 ] ],
[ 'PACKED_V4', 'PACKED_GLOBAL', 'PACKED_LINKLOCAL' ],
'the link-local record moves behind both routable records' );
is_deeply( [ @ordered[ 0 .. 4 ] ],
[ 2, 1, 6, 'PACKED_V4', 'v4.example' ],
'the five elements of a record stay with that record' );
my @with_broken = xCAT::NetworkUtils::_socket6_addrinfo_routable_first(
10, 1, 6, 'PACKED_BROKEN', 'broken.example',
10, 1, 6, 'PACKED_GLOBAL', 'global.example',
);
is( $with_broken[3], 'PACKED_GLOBAL',
'a record whose address cannot be read goes last, not first' );
is_deeply( [ xCAT::NetworkUtils::_socket6_addrinfo_routable_first( 1, 2, 3 ) ],
[ 1, 2, 3 ],
'a list that is not a whole number of records is returned untouched' );
}
is_deeply(
[ xCAT::NetworkUtils->prefer_routable_addresses( undef, '', '192.0.2.154' ) ],
['192.0.2.154'], 'undef and empty entries are dropped' );