2
0
mirror of https://github.com/xcat2/xcat-core.git synced 2026-11-22 02:12:16 +00:00

fix(ipv6): site.master takes the link-local address of the management node

On an EL10 management node whose own name resolves to both families,
xcatconfig writes a link-local IPv6 address into site.master and
site.nameservers. Measured on an AlmaLinux 10.2 node: getent reports
fe80::4054:ff:fec9:9ad2%2 before 192.168.201.154, and
xCAT::NetworkUtils->getipaddr($hostname) answers
fe80::4054:ff:fec9:9ad2%mgmt0, which xcatconfig stores. A node on another link
cannot reach that address, and the %zone form is not a literal isIpv6addr or
any consumer of site.master accepts.

getipaddr resolves through Socket::getaddrinfo with AF_UNSPEC when no family is
asked for, and returned the first entry getaddrinfo gave. prefer_routable_addresses
orders address literals so a routable one comes before a link-local one and
removes the zone identifier, and _addrinfo_routable_first applies the same order
to the getaddrinfo entries before getipaddr reads the first. A link-local
address is still returned when the host has no other.

networkutils_linklocal_preference.t covers the order, the zone identifier and
both link-local ranges. Removing the routable split fails 4 of its 10
assertions, removing the zone strip fails 3, and treating 169.254.0.0/16 as
routable fails 1.

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 4d5ca9964e
commit 1253af09c7
2 changed files with 154 additions and 0 deletions
+87
View File
@@ -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
@@ -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();