Blob Blame History Raw
From c3b47a965a9b073527e7b71f88de4887fc88ecc9 Mon Sep 17 00:00:00 2001
From: Beniamino Galvani <bgalvani@redhat.com>
Date: Tue, 22 Sep 2020 17:54:18 +0200
Subject: [PATCH 1/2] initrd: accept mac address as interface specifier

The interface can be specified either by name or MAC address:

ip=192.0.2.2:::::eth0
ip=192.0.2.2:::::00-11-22-33-44-55

https://bugzilla.redhat.com/show_bug.cgi?id=1879795
(cherry picked from commit f22364429135f55094fb89879f1fa7bad066727f)
(cherry picked from commit eeef91aa6e8d3eb1510563c413dda19b578a8a6a)
---
 src/initrd/nmi-cmdline-reader.c        | 85 ++++++++++++++++++--------
 src/initrd/tests/test-cmdline-reader.c | 68 +++++++++++++++++++++
 2 files changed, 128 insertions(+), 25 deletions(-)

diff --git a/src/initrd/nmi-cmdline-reader.c b/src/initrd/nmi-cmdline-reader.c
index ba747b30ad..8196e9fb78 100644
--- a/src/initrd/nmi-cmdline-reader.c
+++ b/src/initrd/nmi-cmdline-reader.c
@@ -78,6 +78,7 @@ reader_create_connection (Reader *reader,
                           const char *basename,
                           const char *id,
                           const char *ifname,
+                          const char *mac,
                           const char *type_name,
                           NMConnectionMultiConnect multi_connect)
 {
@@ -120,6 +121,14 @@ reader_create_connection (Reader *reader,
 	              NM_SETTING_CONNECTION_MULTI_CONNECT, multi_connect,
 	              NULL);
 
+	if (mac) {
+		setting = nm_setting_wired_new ();
+		nm_connection_add_setting (connection, setting);
+		g_object_set (setting,
+		              NM_SETTING_WIRED_MAC_ADDRESS, mac,
+		              NULL);
+	}
+
 	return connection;
 }
 
@@ -133,6 +142,7 @@ reader_get_default_connection (Reader *reader)
 		                                "default_connection",
 		                                "Wired Connection",
 		                                NULL,
+		                                NULL,
 		                                NM_SETTING_WIRED_SETTING_NAME,
 		                                NM_CONNECTION_MULTI_CONNECT_MULTIPLE);
 		nm_connection_add_setting (con, nm_setting_wired_new ());
@@ -143,14 +153,26 @@ reader_get_default_connection (Reader *reader)
 
 static NMConnection *
 reader_get_connection (Reader *reader,
-                       const char *ifname,
+                       const char *iface_spec,
                        const char *type_name,
                        gboolean create_if_missing)
 {
 	NMConnection *connection = NULL;
 	NMSetting *setting;
+	const char *ifname = NULL;
+	gs_free char *mac = NULL;
+
+	if (iface_spec) {
+		if (nm_utils_is_valid_iface_name (iface_spec, NULL))
+			ifname = iface_spec;
+		else {
+			mac = nm_utils_hwaddr_canonical (iface_spec, ETH_ALEN);
+			if (!mac)
+				_LOGW (LOGD_CORE, "invalid interface '%s'", iface_spec);
+		}
+	}
 
-	if (!ifname) {
+	if (!ifname && !mac) {
 		NMConnection *candidate;
 		NMSettingConnection *s_con;
 		guint i;
@@ -178,7 +200,7 @@ reader_get_connection (Reader *reader,
 			}
 		}
 	} else
-		connection = g_hash_table_lookup (reader->hash, (gpointer) ifname);
+		connection = g_hash_table_lookup (reader->hash, (gpointer) ifname ?: mac);
 
 	if (!connection) {
 		if (!create_if_missing)
@@ -187,9 +209,9 @@ reader_get_connection (Reader *reader,
 		if (!type_name)
 			type_name = NM_SETTING_WIRED_SETTING_NAME;
 
-		connection = reader_create_connection (reader, ifname,
-		                                       ifname ?: "Wired Connection",
-		                                       ifname, type_name,
+		connection = reader_create_connection (reader, ifname ?: mac,
+		                                       ifname ?: (mac ?: "Wired Connection"),
+		                                       ifname, mac, type_name,
 		                                       NM_CONNECTION_MULTI_CONNECT_SINGLE);
 	}
 	setting = (NMSetting *) nm_connection_get_setting_connection (connection);
@@ -331,7 +353,7 @@ reader_parse_ip (Reader *reader, const char *sysfs_dir, char *argument)
 	const char *gateway_ip = NULL;
 	const char *netmask = NULL;
 	const char *client_hostname = NULL;
-	const char *ifname = NULL;
+	const char *iface_spec = NULL;
 	const char *mtu = NULL;
 	const char *macaddr = NULL;
 	int client_ip_family = AF_UNSPEC;
@@ -357,9 +379,9 @@ reader_parse_ip (Reader *reader, const char *sysfs_dir, char *argument)
 			gateway_ip = get_word (&argument, ':');
 			netmask = get_word (&argument, ':');
 			client_hostname = get_word (&argument, ':');
-			ifname = get_word (&argument, ':');
+			iface_spec = get_word (&argument, ':');
 		} else {
-			ifname = tmp;
+			iface_spec = tmp;
 		}
 
 		if (client_hostname && !nm_sd_hostname_is_valid (client_hostname, FALSE))
@@ -388,15 +410,15 @@ reader_parse_ip (Reader *reader, const char *sysfs_dir, char *argument)
 		}
 	}
 
-	if (   ifname == NULL
+	if (   iface_spec == NULL
 	    && NM_IN_STRSET (kind, "fw", "ibft")) {
 		reader_read_all_connections_from_fw (reader, sysfs_dir);
 		return;
 	}
 
 	/* Parsing done, construct the NMConnection. */
-	if (ifname)
-		connection = reader_get_connection (reader, ifname, NULL, TRUE);
+	if (iface_spec)
+		connection = reader_get_connection (reader, iface_spec, NULL, TRUE);
 	else
 		connection = reader_get_default_connection (reader);
 
@@ -498,22 +520,36 @@ reader_parse_ip (Reader *reader, const char *sysfs_dir, char *argument)
 			              NULL);
 		}
 	} else if (nm_streq0 (kind, "ibft")) {
-		gs_free char *address_path = g_build_filename (sysfs_dir, "class", "net", ifname, "address", NULL);
-		gs_free char *mac, *mac_up = NULL;
+		NMSettingWired *s_wired;
+		const char *mac = NULL;
+		const char *ifname;
+		gs_free char *mac_free = NULL;
+		gs_free char *address_path = NULL;
 		GHashTable *nic = NULL;
 
-		if (!g_file_get_contents (address_path, &mac, NULL, &error)) {
-			_LOGW (LOGD_CORE, "Can't get a MAC address for %s: %s", ifname, error->message);
-			g_clear_error (&error);
+		if (   (s_wired = nm_connection_get_setting_wired (connection))
+		    && (mac = nm_setting_wired_get_mac_address (s_wired))) {
+			/* got mac from the connection */
+		} else if ((ifname = nm_connection_get_interface_name (connection))) {
+			/* read it from sysfs */
+			address_path = g_build_filename (sysfs_dir, "class", "net", ifname, "address", NULL);
+			if (g_file_get_contents (address_path, &mac_free, NULL, &error)) {
+				g_strchomp (mac_free);
+				mac = mac_free;
+			} else {
+				_LOGW (LOGD_CORE, "Can't get a MAC address for %s: %s", ifname, error->message);
+				g_clear_error (&error);
+			}
 		}
 
 		if (mac) {
-			g_strchomp (mac);
+			gs_free char *mac_up = NULL;
+
 			mac_up = g_ascii_strup (mac, -1);
 			ibft = nmi_ibft_read (sysfs_dir);
 			nic = g_hash_table_lookup (ibft, mac_up);
 			if (!nic)
-				_LOGW (LOGD_CORE, "No iBFT NIC for %s (%s)", ifname, mac_up);
+				_LOGW (LOGD_CORE, "No iBFT NIC for %s (%s)", iface_spec, mac_up);
 		}
 
 		if (nic) {
@@ -1018,15 +1054,14 @@ nmi_cmdline_reader_parse (const char *sysfs_dir, const char *const*argv, char **
 			                                       "bootif_connection",
 			                                       "BOOTIF Connection",
 			                                       NULL,
+			                                       bootif,
 			                                       NM_SETTING_WIRED_SETTING_NAME,
 			                                       NM_CONNECTION_MULTI_CONNECT_SINGLE);
-			s_wired = (NMSettingWired *) nm_setting_wired_new ();
-			nm_connection_add_setting (connection, (NMSetting *) s_wired);
+		} else {
+			g_object_set (s_wired,
+			              NM_SETTING_WIRED_MAC_ADDRESS, bootif,
+			              NULL);
 		}
-
-		g_object_set (s_wired,
-		              NM_SETTING_WIRED_MAC_ADDRESS, bootif,
-		              NULL);
 	}
 
 	if (bootdev) {
diff --git a/src/initrd/tests/test-cmdline-reader.c b/src/initrd/tests/test-cmdline-reader.c
index a11b76e015..4a2f33fc02 100644
--- a/src/initrd/tests/test-cmdline-reader.c
+++ b/src/initrd/tests/test-cmdline-reader.c
@@ -293,6 +293,49 @@ test_if_ip6_manual (void)
 	g_assert_cmpstr (nm_setting_ip_config_get_dhcp_hostname (s_ip6), ==, "hostname0.example.com");
 }
 
+static void
+test_if_mac_ifname (void)
+{
+	gs_unref_hashtable GHashTable *connections = NULL;
+	const char *const*ARGV = NM_MAKE_STRV ("ip=[2001:0db8::42]/64::[2001:0db8::01]::"
+	                                       "hostname0:00-11-22-33-44-55::[2001:0db8::53]");
+	NMConnection *connection;
+	NMSettingIPConfig *s_ip6;
+	NMSettingWired *s_wired;
+	NMIPAddress *ip_addr;
+	gs_free char *hostname = NULL;
+
+	connections = nmi_cmdline_reader_parse (TEST_INITRD_DIR "/sysfs", ARGV, &hostname);
+	g_assert (connections);
+	g_assert_cmpint (g_hash_table_size (connections), ==, 1);
+	g_assert_cmpstr (hostname, ==, "hostname0");
+
+	connection = g_hash_table_lookup (connections, "00:11:22:33:44:55");
+	g_assert (connection);
+	nmtst_assert_connection_verifies_without_normalization (connection);
+	g_assert_cmpstr (nm_connection_get_id (connection), ==, "00:11:22:33:44:55");
+	g_assert_cmpstr (nm_connection_get_interface_name (connection), ==, NULL);
+
+	s_wired = nm_connection_get_setting_wired (connection);
+	g_assert (s_wired);
+	g_assert_cmpstr (nm_setting_wired_get_mac_address(s_wired), ==, "00:11:22:33:44:55");
+
+	s_ip6 = nm_connection_get_setting_ip6_config (connection);
+	g_assert (s_ip6);
+	g_assert_cmpstr (nm_setting_ip_config_get_method (s_ip6), ==, NM_SETTING_IP6_CONFIG_METHOD_MANUAL);
+	g_assert (!nm_setting_ip_config_get_ignore_auto_dns (s_ip6));
+	g_assert_cmpint (nm_setting_ip_config_get_num_dns (s_ip6), ==, 1);
+	g_assert_cmpstr (nm_setting_ip_config_get_dns (s_ip6, 0), ==, "2001:db8::53");
+	g_assert_cmpint (nm_setting_ip_config_get_num_routes (s_ip6), ==, 0);
+	g_assert_cmpint (nm_setting_ip_config_get_num_addresses (s_ip6), ==, 1);
+	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::42");
+	g_assert_cmpint (nm_ip_address_get_prefix (ip_addr), ==, 64);
+	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), ==, "hostname0");
+}
+
 static void
 test_multiple_merge (void)
 {
@@ -1042,6 +1085,29 @@ test_ibft_ip_dev (void)
 	g_assert_cmpstr (nm_setting_connection_get_interface_name (s_con), ==, NULL);
 }
 
+static void
+test_ibft_ip_dev_mac (void)
+{
+	const char *const*ARGV = NM_MAKE_STRV ("ip=00-53-06-66-ab-01:ibft");
+	gs_unref_hashtable GHashTable *connections = NULL;
+	NMSettingConnection *s_con;
+	NMConnection *connection;
+	gs_free char *hostname = NULL;
+
+	connections = nmi_cmdline_reader_parse (TEST_INITRD_DIR "/sysfs", ARGV, &hostname);
+	g_assert (connections);
+	g_assert_cmpint (g_hash_table_size (connections), ==, 1);
+	g_assert_cmpstr (hostname, ==, NULL);
+
+	connection = g_hash_table_lookup (connections, "00:53:06:66:AB:01");
+	g_assert (connection);
+
+	s_con = nm_connection_get_setting_connection (connection);
+	g_assert (s_con);
+	g_assert_cmpstr (nm_setting_connection_get_connection_type (s_con), ==, NM_SETTING_WIRED_SETTING_NAME);
+	g_assert_cmpstr (nm_setting_connection_get_interface_name (s_con), ==, NULL);
+}
+
 static void
 _test_ibft_ip (const char *const*ARGV)
 {
@@ -1545,6 +1611,7 @@ int main (int argc, char **argv)
 	g_test_add_func ("/initrd/cmdline/if_auto_with_mtu_and_mac", test_if_auto_with_mtu_and_mac);
 	g_test_add_func ("/initrd/cmdline/if_ip4_manual", test_if_ip4_manual);
 	g_test_add_func ("/initrd/cmdline/if_ip6_manual", test_if_ip6_manual);
+	g_test_add_func ("/initrd/cmdline/if_mac_ifname", test_if_mac_ifname);
 	g_test_add_func ("/initrd/cmdline/multiple/merge", test_multiple_merge);
 	g_test_add_func ("/initrd/cmdline/multiple/bootdev", test_multiple_bootdev);
 	g_test_add_func ("/initrd/cmdline/nameserver", test_nameserver);
@@ -1558,6 +1625,7 @@ int main (int argc, char **argv)
 	g_test_add_func ("/initrd/cmdline/bridge/default", test_bridge_default);
 	g_test_add_func ("/initrd/cmdline/bridge/ip", test_bridge_ip);
 	g_test_add_func ("/initrd/cmdline/ibft/ip_dev", test_ibft_ip_dev);
+	g_test_add_func ("/initrd/cmdline/ibft/ip_dev_mac", test_ibft_ip_dev_mac);
 	g_test_add_func ("/initrd/cmdline/ibft/ip", test_ibft_ip);
 	g_test_add_func ("/initrd/cmdline/ibft/rd_iscsi_ibft", test_ibft_rd_iscsi_ibft);
 	g_test_add_func ("/initrd/cmdline/ignore_extra", test_ignore_extra);
-- 
2.26.2

From a65c364d2cfe7a36f5207f652ec77bb42b532f88 Mon Sep 17 00:00:00 2001
From: Beniamino Galvani <bgalvani@redhat.com>
Date: Wed, 23 Sep 2020 10:58:03 +0200
Subject: [PATCH 2/2] initrd: fix parsing IPv6 prefix length

The generator didn't accept prefix lengths > 32 for IPv6:

$ src/initrd/nm-initrd-generator --stdout -- ip=[fd01::1]:::40::ens0
 <warn>  [1600851580.7875] cmdline-reader: Invalid IP mask: 40

https://bugzilla.redhat.com/show_bug.cgi?id=1879795
(cherry picked from commit 364c7c278a19959c6e880329533937dbcf7b19a4)
(cherry picked from commit 09804cac6ef2016cfa70d3e63c7d73f2dfc3cf91)
---
 src/initrd/nmi-cmdline-reader.c        | 5 +++--
 src/initrd/tests/test-cmdline-reader.c | 3 ++-
 2 files changed, 5 insertions(+), 3 deletions(-)

diff --git a/src/initrd/nmi-cmdline-reader.c b/src/initrd/nmi-cmdline-reader.c
index 8196e9fb78..ee3dab2574 100644
--- a/src/initrd/nmi-cmdline-reader.c
+++ b/src/initrd/nmi-cmdline-reader.c
@@ -426,12 +426,13 @@ reader_parse_ip (Reader *reader, const char *sysfs_dir, char *argument)
 	s_ip6 = nm_connection_get_setting_ip6_config (connection);
 
 	if (netmask && *netmask) {
+		gboolean is_ipv4 = client_ip_family == AF_INET;
 		NMIPAddr addr;
 
-		if (nm_utils_parse_inaddr_bin (AF_INET, netmask, NULL, &addr))
+		if (is_ipv4 && nm_utils_parse_inaddr_bin (AF_INET, netmask, NULL, &addr))
 			client_ip_prefix = nm_utils_ip4_netmask_to_prefix (addr.addr4);
 		else
-			client_ip_prefix = _nm_utils_ascii_str_to_int64 (netmask, 10, 0, 32, -1);
+			client_ip_prefix = _nm_utils_ascii_str_to_int64 (netmask, 10, 0, is_ipv4 ? 32 : 128, -1);
 
 		if (client_ip_prefix == -1)
 			_LOGW (LOGD_CORE, "Invalid IP mask: %s", netmask);
diff --git a/src/initrd/tests/test-cmdline-reader.c b/src/initrd/tests/test-cmdline-reader.c
index 4a2f33fc02..d67f599337 100644
--- a/src/initrd/tests/test-cmdline-reader.c
+++ b/src/initrd/tests/test-cmdline-reader.c
@@ -341,7 +341,7 @@ test_multiple_merge (void)
 {
 	gs_unref_hashtable GHashTable *connections = NULL;
 	const char *const*ARGV = NM_MAKE_STRV ("ip=192.0.2.2:::::eth0",
-	                                       "ip=[2001:db8::2]:::::eth0");
+	                                       "ip=[2001:db8::2]:::56::eth0");
 	NMConnection *connection;
 	NMSettingConnection *s_con;
 	NMSettingWired *s_wired;
@@ -384,6 +384,7 @@ test_multiple_merge (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), ==, 56);
 }
 
 static void
-- 
2.26.2