From d9763bd2e3eba6c7bb97ce0431bb95ce1a26e6be Mon Sep 17 00:00:00 2001 From: Daniel Hilst <392820+dhilst@users.noreply.github.com> Date: Wed, 21 Oct 2026 18:59:07 -0300 Subject: [PATCH] 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> --- perl-xCAT/xCAT/NetworkUtils.pm | 31 +++++++++++++ .../unit/networkutils_linklocal_preference.t | 43 +++++++++++++++++++ 2 files changed, 74 insertions(+) diff --git a/perl-xCAT/xCAT/NetworkUtils.pm b/perl-xCAT/xCAT/NetworkUtils.pm index dc3752e4ab..de9aa3f118 100644 --- a/perl-xCAT/xCAT/NetworkUtils.pm +++ b/perl-xCAT/xCAT/NetworkUtils.pm @@ -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. diff --git a/xCAT-test/unit/networkutils_linklocal_preference.t b/xCAT-test/unit/networkutils_linklocal_preference.t index 7363765c00..d444d0eefb 100644 --- a/xCAT-test/unit/networkutils_linklocal_preference.t +++ b/xCAT-test/unit/networkutils_linklocal_preference.t @@ -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' );