initrd: support setting the DHCP client-id

In some cases it is necessary to set a custom DHCP client-id during
early boot. For example, the firmware of some InfiniBand NIC uses a
48-bit MAC derived from the InfiniBand 20-byte MAC when doing
PXE. NetworkManager doesn't have any knowledge of that 48-bit MAC and
uses the full MAC as client-id, therefore getting a different lease.

Introduce a new option 'rd.net.dhcp.client-id' to specify a custom
client-id.

Resolves: https://issues.redhat.com/browse/RHEL-108454

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/2277
This commit is contained in:
Beniamino Galvani 2025-08-28 11:51:59 +02:00
parent f472111e58
commit 40aa27690c
3 changed files with 229 additions and 1 deletions

View file

@ -162,6 +162,7 @@
<member><option>rd.net.dns-backend</option></member> <member><option>rd.net.dns-backend</option></member>
<member><option>rd.net.dns-resolve-mode</option></member> <member><option>rd.net.dns-resolve-mode</option></member>
<member><option>rd.net.timeout.dhcp</option></member> <member><option>rd.net.timeout.dhcp</option></member>
<member><option>rd.net.dhcp.client-id</option></member>
<member><option>rd.net.dhcp.retry</option></member> <member><option>rd.net.dhcp.retry</option></member>
<member><option>rd.net.dhcp.vendor-class</option></member> <member><option>rd.net.dhcp.vendor-class</option></member>
<member><option>rd.net.dhcp.dscp</option></member> <member><option>rd.net.dhcp.dscp</option></member>
@ -268,6 +269,23 @@
</para> </para>
</listitem> </listitem>
<listitem>
<para>NetworkManager supports the
<option>rd.net.dhcp.client-id</option>=<replaceable>interface</replaceable>:<replaceable>client-id</replaceable>
kernel command line option to set a specific DHCPv4 client identifier
for the given interface. The client-id can be specified either as a
sequence of bytes in hexadecimal format separated by dashes, or as the
character '@' followed by a non-empty string. When using the second
format, NetworkManager prepends a zero byte to the given string,
according to section 9.14 of RFC 2132. See the "ipv4.dhcp-client-id"
section of <link
linkend='nm-settings-nmcli'><citerefentry><refentrytitle>nm-settings-nmcli</refentrytitle><manvolnum>5</manvolnum></citerefentry></link>
for more details. Examples:
<literal>rd.net.dhcp.client-id=eth0:01-52-54-00-45-87-42</literal>,
<literal>rd.net.dhcp.client-id=enp1s0:@example.com</literal>.
</para>
</listitem>
</itemizedlist> </itemizedlist>
</refsect1> </refsect1>
@ -278,6 +296,7 @@
<refsect1 id='see_also'><title>See Also</title> <refsect1 id='see_also'><title>See Also</title>
<para><link linkend='dracut.cmdline'><citerefentry><refentrytitle>dracut.cmdline</refentrytitle><manvolnum>7</manvolnum></citerefentry></link>, <para><link linkend='dracut.cmdline'><citerefentry><refentrytitle>dracut.cmdline</refentrytitle><manvolnum>7</manvolnum></citerefentry></link>,
<link linkend='NetworkManager'><citerefentry><refentrytitle>NetworkManager</refentrytitle><manvolnum>8</manvolnum></citerefentry></link>.</para> <link linkend='NetworkManager'><citerefentry><refentrytitle>NetworkManager</refentrytitle><manvolnum>8</manvolnum></citerefentry></link>,
<link linkend='nm-settings-nmcli'><citerefentry><refentrytitle>nm-settings-nmcli</refentrytitle><manvolnum>5</manvolnum></citerefentry></link>.</para>
</refsect1> </refsect1>
</refentry> </refentry>

View file

@ -1368,6 +1368,67 @@ reader_parse_ethtool(Reader *reader, char *argument)
_LOGW(LOGD_CORE, "rd.ethtool: extra argument ignored"); _LOGW(LOGD_CORE, "rd.ethtool: extra argument ignored");
} }
static void
reader_parse_dhcp_client_id(Reader *reader, char *argument)
{
NMConnection *connection;
NMSettingIPConfig *s_ip4;
const char *interface;
gs_free char *client_id = NULL;
gs_free guint8 *buf = NULL;
gsize len = 0;
interface = get_word(&argument, ':');
if (!interface) {
_LOGW(LOGD_CORE, "rd.net.dhcp.client-id: missing interface");
return;
}
if (!argument || !*argument) {
_LOGW(LOGD_CORE, "rd.net.dhcp.client-id: missing client-id");
return;
}
if (argument[0] == '@') {
/* The client-id is a plain string but we still encode it as
* hex string. Otherwise, we could pass the string as-is, but we
* would need to handle special keywords like "mac", "perm-mac", etc.
*/
if (argument[1] != '\0') {
len = strlen(argument);
buf = (guint8 *) nm_memdup(argument, len + 1);
buf[0] = '\0';
}
} else {
/* Try to parse it as hex string */
buf = nm_utils_hexstr2bin_alloc(argument, FALSE, FALSE, "-", 0, &len);
}
if (buf) {
client_id = nm_utils_bin2hexstr_full(buf, len, ':', FALSE, NULL);
}
if (!client_id) {
_LOGW(LOGD_CORE,
"rd.net.dhcp.client-id: invalid client-id \"%s\". Must be hexadecimal bytes "
"separated by dashes (for example \"00-01-02-03-04-05-06\"), or '@' followed by a "
"string",
argument);
return;
}
if (len < 2) {
_LOGW(LOGD_CORE,
"rd.net.dhcp.client-id: invalid client-id \"%s\". Must be at least two bytes",
argument);
return;
}
connection = reader_get_connection(reader, interface, NULL, TRUE);
s_ip4 = nm_connection_get_setting_ip4_config(connection);
g_object_set(s_ip4, NM_SETTING_IP4_CONFIG_DHCP_CLIENT_ID, client_id, NULL);
}
static void static void
_normalize_conn(gpointer key, gpointer value, gpointer user_data) _normalize_conn(gpointer key, gpointer value, gpointer user_data)
{ {
@ -1384,6 +1445,8 @@ _normalize_conn(gpointer key, gpointer value, gpointer user_data)
NULL, NULL,
NM_SETTING_IP_CONFIG_DHCP_TIMEOUT, NM_SETTING_IP_CONFIG_DHCP_TIMEOUT,
NULL, NULL,
NM_SETTING_IP4_CONFIG_DHCP_CLIENT_ID,
NULL,
NM_SETTING_IP4_CONFIG_DHCP_VENDOR_CLASS_IDENTIFIER, NM_SETTING_IP4_CONFIG_DHCP_VENDOR_CLASS_IDENTIFIER,
NULL, NULL,
NM_SETTING_IP_CONFIG_DHCP_DSCP, NM_SETTING_IP_CONFIG_DHCP_DSCP,
@ -1602,6 +1665,8 @@ nmi_cmdline_reader_parse(const char *etc_connections_dir,
g_ptr_array_add(znets, g_strdup(argument)); g_ptr_array_add(znets, g_strdup(argument));
} else if (nm_streq(tag, "rd.znet_ifname")) { } else if (nm_streq(tag, "rd.znet_ifname")) {
reader_parse_znet_ifname(reader, argument); reader_parse_znet_ifname(reader, argument);
} else if (nm_streq(tag, "rd.net.dhcp.client-id")) {
reader_parse_dhcp_client_id(reader, argument);
} else if (g_ascii_strcasecmp(tag, "BOOTIF") == 0) { } else if (g_ascii_strcasecmp(tag, "BOOTIF") == 0) {
nm_clear_g_free(&bootif_val); nm_clear_g_free(&bootif_val);
bootif_val = g_strdup(argument); bootif_val = g_strdup(argument);

View file

@ -2846,6 +2846,149 @@ test_plain_equal_char(void)
/*****************************************************************************/ /*****************************************************************************/
#define _dhcp_client_id_check_invalid(arg) \
G_STMT_START \
{ \
gs_unref_hashtable GHashTable *_connections2 = NULL; \
\
_connections2 = _parse_cons(NM_MAKE_STRV(arg)); \
g_test_assert_expected_messages(); \
g_assert_cmpint(g_hash_table_size(_connections2), ==, 0); \
} \
G_STMT_END
#define _dhcp_client_id_check_v(strv, exp_ifname, exp_client_id) \
G_STMT_START \
{ \
gs_unref_object NMConnection *_connection = NULL; \
NMSettingIPConfig *_s_ip4; \
\
_connection = _parse_con(strv, exp_ifname); \
\
g_test_assert_expected_messages(); \
\
g_assert(nm_connection_get_setting_connection(_connection)); \
g_assert(nm_connection_is_type(_connection, NM_SETTING_WIRED_SETTING_NAME)); \
g_assert(nm_connection_get_setting_ip4_config(_connection)); \
g_assert(nm_connection_get_setting_ip6_config(_connection)); \
_s_ip4 = nm_connection_get_setting_ip4_config(_connection); \
g_assert(NM_IS_SETTING_IP_CONFIG(_s_ip4)); \
\
g_assert_cmpstr(nm_setting_ip4_config_get_dhcp_client_id(NM_SETTING_IP4_CONFIG(_s_ip4)), \
==, \
(exp_client_id)); \
} \
G_STMT_END
#define _dhcp_client_id_check(arg, exp_ifname, exp_client_id) \
_dhcp_client_id_check_v(NM_MAKE_STRV("" arg ""), (exp_ifname), (exp_client_id))
#define DHCP_CLIENT_ID_INVALID_MSG(_id) \
"cmdline-reader: " \
"rd.net.dhcp.client-id: invalid client-id \"" _id "\". Must be hexadecimal bytes " \
"separated by dashes (for example \"00-01-02-03-04-05-06\"), or '@' followed by a string"
static void
test_rd_dhcp_client_id(void)
{
NMTST_EXPECT_NM_WARN("cmdline-reader: rd.net.dhcp.client-id: missing interface");
_dhcp_client_id_check_invalid("rd.net.dhcp.client-id=");
NMTST_EXPECT_NM_WARN("cmdline-reader: rd.net.dhcp.client-id: missing interface");
_dhcp_client_id_check_invalid("rd.net.dhcp.client-id=:");
NMTST_EXPECT_NM_WARN("cmdline-reader: rd.net.dhcp.client-id: missing client-id");
_dhcp_client_id_check_invalid("rd.net.dhcp.client-id=eth0:");
NMTST_EXPECT_NM_WARN(DHCP_CLIENT_ID_INVALID_MSG("invalid"));
_dhcp_client_id_check_invalid("rd.net.dhcp.client-id=eth0:invalid");
NMTST_EXPECT_NM_WARN(DHCP_CLIENT_ID_INVALID_MSG("01:AA:BB:CC:DD:EE:FF"));
_dhcp_client_id_check_invalid("rd.net.dhcp.client-id=eth0:01:AA:BB:CC:DD:EE:FF");
NMTST_EXPECT_NM_WARN(DHCP_CLIENT_ID_INVALID_MSG("@"));
_dhcp_client_id_check_invalid("rd.net.dhcp.client-id=eth0:@");
NMTST_EXPECT_NM_WARN("cmdline-reader: rd.net.dhcp.client-id: invalid client-id \"01\". Must be "
"at least two bytes");
_dhcp_client_id_check_invalid("rd.net.dhcp.client-id=eth0:01");
/* Client-id with hex string */
_dhcp_client_id_check("rd.net.dhcp.client-id=eth0:01-aa-BB-cc-dd-EE-ff",
"eth0",
"01:aa:bb:cc:dd:ee:ff");
/* Client-id with plain string */
_dhcp_client_id_check("rd.net.dhcp.client-id=eth0:@test.com",
"eth0",
"00:74:65:73:74:2e:63:6f:6d");
/* Minimal client-id, hex */
_dhcp_client_id_check("rd.net.dhcp.client-id=eth1:01-02", "eth1", "01:02");
/* Minimal client-id, string */
_dhcp_client_id_check("rd.net.dhcp.client-id=eth1:@1", "eth1", "00:31");
/* Long client-id */
_dhcp_client_id_check(
"rd.net.dhcp.client-id=enp1s0:"
"01-02-03-04-05-06-07-08-09-10-11-12-13-14-15-16-17-18-19-20-21-22-23-24-"
"25-26-27-28-29-30-31-32-33-34-35-36-37-38-39-40-41-42-43-44-45-46-47-48-"
"49-50-51-52-53-54-55-56-57-58-59-60-61-62-63-64-65-66-67-68-69-70-71-72",
"enp1s0",
"01:02:03:04:05:06:07:08:09:10:11:12:13:14:15:16:17:18:19:20:21:22:23:24:"
"25:26:27:28:29:30:31:32:33:34:35:36:37:38:39:40:41:42:43:44:45:46:47:48:"
"49:50:51:52:53:54:55:56:57:58:59:60:61:62:63:64:65:66:67:68:69:70:71:72");
/* Test ordering: client-id before ip= */
_dhcp_client_id_check_v(
NM_MAKE_STRV("rd.net.dhcp.client-id=eth0:aa-bb-cc-dd-ee-ff", "ip=eth0:dhcp"),
"eth0",
"aa:bb:cc:dd:ee:ff");
/* Test ordering: client-id after ip= */
_dhcp_client_id_check_v(
NM_MAKE_STRV("ip=eth2:dhcp", "rd.net.dhcp.client-id=eth2:ba-da-cc-dd-ee-ff"),
"eth2",
"ba:da:cc:dd:ee:ff");
/* Duplicate option: last wins */
_dhcp_client_id_check_v(NM_MAKE_STRV("ip=eth3:dhcp",
"rd.net.dhcp.client-id=eth3:01-02",
"rd.net.dhcp.client-id=eth3:01-03"),
"eth3",
"01:03");
/* Multiple connections */
{
gs_unref_hashtable GHashTable *connections = NULL;
NMConnection *connection;
NMSettingIP4Config *s_ip4;
connections = _parse_cons(NM_MAKE_STRV("ip=eth0:dhcp",
"ip=eth1:dhcp",
"rd.net.dhcp.client-id=eth1:01-01-01",
"rd.net.dhcp.client-id=eth0:00-00-00"));
g_assert_nonnull(connections);
g_assert_cmpint(g_hash_table_size(connections), ==, 2);
connection = g_hash_table_lookup(connections, "eth0");
g_assert_nonnull(connection);
s_ip4 = (NMSettingIP4Config *) nm_connection_get_setting_ip4_config(connection);
g_assert_nonnull(s_ip4);
g_assert_cmpstr(nm_setting_ip4_config_get_dhcp_client_id(s_ip4), ==, "00:00:00");
connection = g_hash_table_lookup(connections, "eth1");
g_assert_nonnull(connection);
s_ip4 = (NMSettingIP4Config *) nm_connection_get_setting_ip4_config(connection);
g_assert_nonnull(s_ip4);
g_assert_cmpstr(nm_setting_ip4_config_get_dhcp_client_id(s_ip4), ==, "01:01:01");
}
}
/*****************************************************************************/
NMTST_DEFINE(); NMTST_DEFINE();
int int
@ -2909,6 +3052,7 @@ main(int argc, char **argv)
g_test_add_func("/initrd/cmdline/rd_ethtool", test_rd_ethtool); g_test_add_func("/initrd/cmdline/rd_ethtool", test_rd_ethtool);
g_test_add_func("/initrd/cmdline/plain_equal_char", test_plain_equal_char); g_test_add_func("/initrd/cmdline/plain_equal_char", test_plain_equal_char);
g_test_add_func("/initrd/cmdline/global_dns", test_global_dns); g_test_add_func("/initrd/cmdline/global_dns", test_global_dns);
g_test_add_func("/initrd/cmdline/rd_dhcp_client_id", test_rd_dhcp_client_id);
return g_test_run(); return g_test_run();
} }