diff --git a/man/nm-initrd-generator.xml b/man/nm-initrd-generator.xml index 312edff2ee..f9e254d75c 100644 --- a/man/nm-initrd-generator.xml +++ b/man/nm-initrd-generator.xml @@ -162,6 +162,7 @@ + @@ -268,6 +269,23 @@ + + NetworkManager supports the + =interface:client-id + 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 nm-settings-nmcli5 + for more details. Examples: + rd.net.dhcp.client-id=eth0:01-52-54-00-45-87-42, + rd.net.dhcp.client-id=enp1s0:@example.com. + + + @@ -278,6 +296,7 @@ See Also dracut.cmdline7, - NetworkManager8. + NetworkManager8, + nm-settings-nmcli5. diff --git a/src/nm-initrd-generator/nmi-cmdline-reader.c b/src/nm-initrd-generator/nmi-cmdline-reader.c index ba5380afb2..fdd7283e54 100644 --- a/src/nm-initrd-generator/nmi-cmdline-reader.c +++ b/src/nm-initrd-generator/nmi-cmdline-reader.c @@ -1349,6 +1349,67 @@ reader_parse_ethtool(Reader *reader, char *argument) _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 _normalize_conn(gpointer key, gpointer value, gpointer user_data) { @@ -1365,6 +1426,8 @@ _normalize_conn(gpointer key, gpointer value, gpointer user_data) NULL, NM_SETTING_IP_CONFIG_DHCP_TIMEOUT, NULL, + NM_SETTING_IP4_CONFIG_DHCP_CLIENT_ID, + NULL, NM_SETTING_IP4_CONFIG_DHCP_VENDOR_CLASS_IDENTIFIER, NULL, NM_SETTING_IP_CONFIG_DHCP_DSCP, @@ -1583,6 +1646,8 @@ nmi_cmdline_reader_parse(const char *etc_connections_dir, g_ptr_array_add(znets, g_strdup(argument)); } else if (nm_streq(tag, "rd.znet_ifname")) { 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) { nm_clear_g_free(&bootif_val); bootif_val = g_strdup(argument); diff --git a/src/nm-initrd-generator/tests/test-cmdline-reader.c b/src/nm-initrd-generator/tests/test-cmdline-reader.c index cd7b1069b6..ac72287258 100644 --- a/src/nm-initrd-generator/tests/test-cmdline-reader.c +++ b/src/nm-initrd-generator/tests/test-cmdline-reader.c @@ -2786,6 +2786,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(); int @@ -2848,6 +2991,7 @@ main(int argc, char **argv) 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/global_dns", test_global_dns); + g_test_add_func("/initrd/cmdline/rd_dhcp_client_id", test_rd_dhcp_client_id); return g_test_run(); }