nm-initrd-generator: fix ignored prefix for IPv6 address with brackets

When defining an IPv6 address with square brackets and prefix, like
[dead::beef]/64, the prefix was silently ignored. The address was
accepted only accidentally, because get_word replaced ']' with '\0' so
it resulted in a valid IPv6 address string, but without the prefix.

The previous commit has fixed get_word with better logic to handle the
square brackets, uncovering this issue.

Fix it by explicitly splitting IP addresses and prefixes in
reader_parse_ip so we get a valid address and prefix.

Also, use a prefix different to 64 in the test test_if_ip6_manual. 64 is
the default one, making that the test passed despite the defined prefix
was actually ignored.

Fixes: ecc074b2f8 ('initrd: add command line parser')
This commit is contained in:
Íñigo Huguet 2025-04-25 14:45:02 +02:00
parent aeaf8ca23c
commit 6f6bb17a28
2 changed files with 16 additions and 10 deletions

View file

@ -544,7 +544,7 @@ reader_parse_ip(Reader *reader, const char *sysfs_dir, char *argument)
NMSettingConnection *s_con;
NMSettingIPConfig *s_ip4 = NULL, *s_ip6 = NULL;
gs_unref_hashtable GHashTable *ibft = NULL;
const char *tmp;
char *tmp;
const char *tmp2;
const char *tmp3;
const char *kind;
@ -589,15 +589,25 @@ reader_parse_ip(Reader *reader, const char *sysfs_dir, char *argument)
kind = tmp3;
} else {
/* <client-IP>:[<peer>]:<gateway-IP>:<netmask>:<client_hostname>:<kind> */
client_ip = tmp;
/* note: split here address and prefix to normalize IPs defined as
* [dead::beef]/64. Latter parsing would fail due to the '[]'. */
client_ip = get_word(&tmp, '/');
if (client_ip) {
client_ip_family = get_ip_address_family(client_ip, TRUE);
client_ip_family = get_ip_address_family(client_ip, FALSE);
if (client_ip_family == AF_UNSPEC) {
_LOGW(LOGD_CORE, "Invalid IP address '%s'.", client_ip);
return;
}
}
if (!nm_str_is_empty(tmp)) {
gboolean is_ipv4 = client_ip_family == AF_INET;
client_ip_prefix = _nm_utils_ascii_str_to_int64(tmp, 10, 0, is_ipv4 ? 32 : 128, -1);
}
peer = tmp2;
gateway_ip = get_word(&argument, ':');
netmask = get_word(&argument, ':');
@ -672,11 +682,7 @@ reader_parse_ip(Reader *reader, const char *sysfs_dir, char *argument)
NMIPAddress *address = NULL;
NMIPAddr addr;
if (nm_inet_parse_with_prefix_bin(client_ip_family,
client_ip,
NULL,
&addr,
client_ip_prefix == -1 ? &client_ip_prefix : NULL)) {
if (nm_inet_parse_bin(client_ip_family, client_ip, NULL, &addr)) {
if (client_ip_prefix == -1) {
switch (client_ip_family) {
case AF_INET:

View file

@ -597,7 +597,7 @@ static void
test_if_ip6_manual(void)
{
gs_unref_hashtable GHashTable *connections = NULL;
const char *const *ARGV = NM_MAKE_STRV("ip=[2001:0db8::02]/64::[2001:0db8::01]::"
const char *const *ARGV = NM_MAKE_STRV("ip=[2001:0db8::02]/56::[2001:0db8::01]::"
"hostname0.example.com:eth4::[2001:0db8::53]");
NMConnection *connection;
NMSettingIPConfig *s_ip4;
@ -633,7 +633,7 @@ test_if_ip6_manual(void)
ip_addr = nm_setting_ip_config_get_address(s_ip6, 0);
g_assert(ip_addr);
g_assert_cmpstr(nm_ip_address_get_address(ip_addr), ==, "2001:db8::2");
g_assert_cmpint(nm_ip_address_get_prefix(ip_addr), ==, 64);
g_assert_cmpint(nm_ip_address_get_prefix(ip_addr), ==, 56);
g_assert_cmpstr(nm_setting_ip_config_get_gateway(s_ip6), ==, "2001:db8::1");
g_assert_cmpstr(nm_setting_ip_config_get_dhcp_hostname(s_ip6), ==, NULL);
}