Blame SOURCES/1018-initrd-accept-mac-ifaces-rh1879795.patch

146a1d
From c3b47a965a9b073527e7b71f88de4887fc88ecc9 Mon Sep 17 00:00:00 2001
146a1d
From: Beniamino Galvani <bgalvani@redhat.com>
146a1d
Date: Tue, 22 Sep 2020 17:54:18 +0200
146a1d
Subject: [PATCH 1/2] initrd: accept mac address as interface specifier
146a1d
146a1d
The interface can be specified either by name or MAC address:
146a1d
146a1d
ip=192.0.2.2:::::eth0
146a1d
ip=192.0.2.2:::::00-11-22-33-44-55
146a1d
146a1d
https://bugzilla.redhat.com/show_bug.cgi?id=1879795
146a1d
(cherry picked from commit f22364429135f55094fb89879f1fa7bad066727f)
146a1d
(cherry picked from commit eeef91aa6e8d3eb1510563c413dda19b578a8a6a)
146a1d
---
146a1d
 src/initrd/nmi-cmdline-reader.c        | 85 ++++++++++++++++++--------
146a1d
 src/initrd/tests/test-cmdline-reader.c | 68 +++++++++++++++++++++
146a1d
 2 files changed, 128 insertions(+), 25 deletions(-)
146a1d
146a1d
diff --git a/src/initrd/nmi-cmdline-reader.c b/src/initrd/nmi-cmdline-reader.c
146a1d
index ba747b30ad..8196e9fb78 100644
146a1d
--- a/src/initrd/nmi-cmdline-reader.c
146a1d
+++ b/src/initrd/nmi-cmdline-reader.c
146a1d
@@ -78,6 +78,7 @@ reader_create_connection (Reader *reader,
146a1d
                           const char *basename,
146a1d
                           const char *id,
146a1d
                           const char *ifname,
146a1d
+                          const char *mac,
146a1d
                           const char *type_name,
146a1d
                           NMConnectionMultiConnect multi_connect)
146a1d
 {
146a1d
@@ -120,6 +121,14 @@ reader_create_connection (Reader *reader,
146a1d
 	              NM_SETTING_CONNECTION_MULTI_CONNECT, multi_connect,
146a1d
 	              NULL);
146a1d
 
146a1d
+	if (mac) {
146a1d
+		setting = nm_setting_wired_new ();
146a1d
+		nm_connection_add_setting (connection, setting);
146a1d
+		g_object_set (setting,
146a1d
+		              NM_SETTING_WIRED_MAC_ADDRESS, mac,
146a1d
+		              NULL);
146a1d
+	}
146a1d
+
146a1d
 	return connection;
146a1d
 }
146a1d
 
146a1d
@@ -133,6 +142,7 @@ reader_get_default_connection (Reader *reader)
146a1d
 		                                "default_connection",
146a1d
 		                                "Wired Connection",
146a1d
 		                                NULL,
146a1d
+		                                NULL,
146a1d
 		                                NM_SETTING_WIRED_SETTING_NAME,
146a1d
 		                                NM_CONNECTION_MULTI_CONNECT_MULTIPLE);
146a1d
 		nm_connection_add_setting (con, nm_setting_wired_new ());
146a1d
@@ -143,14 +153,26 @@ reader_get_default_connection (Reader *reader)
146a1d
 
146a1d
 static NMConnection *
146a1d
 reader_get_connection (Reader *reader,
146a1d
-                       const char *ifname,
146a1d
+                       const char *iface_spec,
146a1d
                        const char *type_name,
146a1d
                        gboolean create_if_missing)
146a1d
 {
146a1d
 	NMConnection *connection = NULL;
146a1d
 	NMSetting *setting;
146a1d
+	const char *ifname = NULL;
146a1d
+	gs_free char *mac = NULL;
146a1d
+
146a1d
+	if (iface_spec) {
146a1d
+		if (nm_utils_is_valid_iface_name (iface_spec, NULL))
146a1d
+			ifname = iface_spec;
146a1d
+		else {
146a1d
+			mac = nm_utils_hwaddr_canonical (iface_spec, ETH_ALEN);
146a1d
+			if (!mac)
146a1d
+				_LOGW (LOGD_CORE, "invalid interface '%s'", iface_spec);
146a1d
+		}
146a1d
+	}
146a1d
 
146a1d
-	if (!ifname) {
146a1d
+	if (!ifname && !mac) {
146a1d
 		NMConnection *candidate;
146a1d
 		NMSettingConnection *s_con;
146a1d
 		guint i;
146a1d
@@ -178,7 +200,7 @@ reader_get_connection (Reader *reader,
146a1d
 			}
146a1d
 		}
146a1d
 	} else
146a1d
-		connection = g_hash_table_lookup (reader->hash, (gpointer) ifname);
146a1d
+		connection = g_hash_table_lookup (reader->hash, (gpointer) ifname ?: mac);
146a1d
 
146a1d
 	if (!connection) {
146a1d
 		if (!create_if_missing)
146a1d
@@ -187,9 +209,9 @@ reader_get_connection (Reader *reader,
146a1d
 		if (!type_name)
146a1d
 			type_name = NM_SETTING_WIRED_SETTING_NAME;
146a1d
 
146a1d
-		connection = reader_create_connection (reader, ifname,
146a1d
-		                                       ifname ?: "Wired Connection",
146a1d
-		                                       ifname, type_name,
146a1d
+		connection = reader_create_connection (reader, ifname ?: mac,
146a1d
+		                                       ifname ?: (mac ?: "Wired Connection"),
146a1d
+		                                       ifname, mac, type_name,
146a1d
 		                                       NM_CONNECTION_MULTI_CONNECT_SINGLE);
146a1d
 	}
146a1d
 	setting = (NMSetting *) nm_connection_get_setting_connection (connection);
146a1d
@@ -331,7 +353,7 @@ reader_parse_ip (Reader *reader, const char *sysfs_dir, char *argument)
146a1d
 	const char *gateway_ip = NULL;
146a1d
 	const char *netmask = NULL;
146a1d
 	const char *client_hostname = NULL;
146a1d
-	const char *ifname = NULL;
146a1d
+	const char *iface_spec = NULL;
146a1d
 	const char *mtu = NULL;
146a1d
 	const char *macaddr = NULL;
146a1d
 	int client_ip_family = AF_UNSPEC;
146a1d
@@ -357,9 +379,9 @@ reader_parse_ip (Reader *reader, const char *sysfs_dir, char *argument)
146a1d
 			gateway_ip = get_word (&argument, ':');
146a1d
 			netmask = get_word (&argument, ':');
146a1d
 			client_hostname = get_word (&argument, ':');
146a1d
-			ifname = get_word (&argument, ':');
146a1d
+			iface_spec = get_word (&argument, ':');
146a1d
 		} else {
146a1d
-			ifname = tmp;
146a1d
+			iface_spec = tmp;
146a1d
 		}
146a1d
 
146a1d
 		if (client_hostname && !nm_sd_hostname_is_valid (client_hostname, FALSE))
146a1d
@@ -388,15 +410,15 @@ reader_parse_ip (Reader *reader, const char *sysfs_dir, char *argument)
146a1d
 		}
146a1d
 	}
146a1d
 
146a1d
-	if (   ifname == NULL
146a1d
+	if (   iface_spec == NULL
146a1d
 	    && NM_IN_STRSET (kind, "fw", "ibft")) {
146a1d
 		reader_read_all_connections_from_fw (reader, sysfs_dir);
146a1d
 		return;
146a1d
 	}
146a1d
 
146a1d
 	/* Parsing done, construct the NMConnection. */
146a1d
-	if (ifname)
146a1d
-		connection = reader_get_connection (reader, ifname, NULL, TRUE);
146a1d
+	if (iface_spec)
146a1d
+		connection = reader_get_connection (reader, iface_spec, NULL, TRUE);
146a1d
 	else
146a1d
 		connection = reader_get_default_connection (reader);
146a1d
 
146a1d
@@ -498,22 +520,36 @@ reader_parse_ip (Reader *reader, const char *sysfs_dir, char *argument)
146a1d
 			              NULL);
146a1d
 		}
146a1d
 	} else if (nm_streq0 (kind, "ibft")) {
146a1d
-		gs_free char *address_path = g_build_filename (sysfs_dir, "class", "net", ifname, "address", NULL);
146a1d
-		gs_free char *mac, *mac_up = NULL;
146a1d
+		NMSettingWired *s_wired;
146a1d
+		const char *mac = NULL;
146a1d
+		const char *ifname;
146a1d
+		gs_free char *mac_free = NULL;
146a1d
+		gs_free char *address_path = NULL;
146a1d
 		GHashTable *nic = NULL;
146a1d
 
146a1d
-		if (!g_file_get_contents (address_path, &mac, NULL, &error)) {
146a1d
-			_LOGW (LOGD_CORE, "Can't get a MAC address for %s: %s", ifname, error->message);
146a1d
-			g_clear_error (&error);
146a1d
+		if (   (s_wired = nm_connection_get_setting_wired (connection))
146a1d
+		    && (mac = nm_setting_wired_get_mac_address (s_wired))) {
146a1d
+			/* got mac from the connection */
146a1d
+		} else if ((ifname = nm_connection_get_interface_name (connection))) {
146a1d
+			/* read it from sysfs */
146a1d
+			address_path = g_build_filename (sysfs_dir, "class", "net", ifname, "address", NULL);
146a1d
+			if (g_file_get_contents (address_path, &mac_free, NULL, &error)) {
146a1d
+				g_strchomp (mac_free);
146a1d
+				mac = mac_free;
146a1d
+			} else {
146a1d
+				_LOGW (LOGD_CORE, "Can't get a MAC address for %s: %s", ifname, error->message);
146a1d
+				g_clear_error (&error);
146a1d
+			}
146a1d
 		}
146a1d
 
146a1d
 		if (mac) {
146a1d
-			g_strchomp (mac);
146a1d
+			gs_free char *mac_up = NULL;
146a1d
+
146a1d
 			mac_up = g_ascii_strup (mac, -1);
146a1d
 			ibft = nmi_ibft_read (sysfs_dir);
146a1d
 			nic = g_hash_table_lookup (ibft, mac_up);
146a1d
 			if (!nic)
146a1d
-				_LOGW (LOGD_CORE, "No iBFT NIC for %s (%s)", ifname, mac_up);
146a1d
+				_LOGW (LOGD_CORE, "No iBFT NIC for %s (%s)", iface_spec, mac_up);
146a1d
 		}
146a1d
 
146a1d
 		if (nic) {
146a1d
@@ -1018,15 +1054,14 @@ nmi_cmdline_reader_parse (const char *sysfs_dir, const char *const*argv, char **
146a1d
 			                                       "bootif_connection",
146a1d
 			                                       "BOOTIF Connection",
146a1d
 			                                       NULL,
146a1d
+			                                       bootif,
146a1d
 			                                       NM_SETTING_WIRED_SETTING_NAME,
146a1d
 			                                       NM_CONNECTION_MULTI_CONNECT_SINGLE);
146a1d
-			s_wired = (NMSettingWired *) nm_setting_wired_new ();
146a1d
-			nm_connection_add_setting (connection, (NMSetting *) s_wired);
146a1d
+		} else {
146a1d
+			g_object_set (s_wired,
146a1d
+			              NM_SETTING_WIRED_MAC_ADDRESS, bootif,
146a1d
+			              NULL);
146a1d
 		}
146a1d
-
146a1d
-		g_object_set (s_wired,
146a1d
-		              NM_SETTING_WIRED_MAC_ADDRESS, bootif,
146a1d
-		              NULL);
146a1d
 	}
146a1d
 
146a1d
 	if (bootdev) {
146a1d
diff --git a/src/initrd/tests/test-cmdline-reader.c b/src/initrd/tests/test-cmdline-reader.c
146a1d
index a11b76e015..4a2f33fc02 100644
146a1d
--- a/src/initrd/tests/test-cmdline-reader.c
146a1d
+++ b/src/initrd/tests/test-cmdline-reader.c
146a1d
@@ -293,6 +293,49 @@ test_if_ip6_manual (void)
146a1d
 	g_assert_cmpstr (nm_setting_ip_config_get_dhcp_hostname (s_ip6), ==, "hostname0.example.com");
146a1d
 }
146a1d
 
146a1d
+static void
146a1d
+test_if_mac_ifname (void)
146a1d
+{
146a1d
+	gs_unref_hashtable GHashTable *connections = NULL;
146a1d
+	const char *const*ARGV = NM_MAKE_STRV ("ip=[2001:0db8::42]/64::[2001:0db8::01]::"
146a1d
+	                                       "hostname0:00-11-22-33-44-55::[2001:0db8::53]");
146a1d
+	NMConnection *connection;
146a1d
+	NMSettingIPConfig *s_ip6;
146a1d
+	NMSettingWired *s_wired;
146a1d
+	NMIPAddress *ip_addr;
146a1d
+	gs_free char *hostname = NULL;
146a1d
+
146a1d
+	connections = nmi_cmdline_reader_parse (TEST_INITRD_DIR "/sysfs", ARGV, &hostname);
146a1d
+	g_assert (connections);
146a1d
+	g_assert_cmpint (g_hash_table_size (connections), ==, 1);
146a1d
+	g_assert_cmpstr (hostname, ==, "hostname0");
146a1d
+
146a1d
+	connection = g_hash_table_lookup (connections, "00:11:22:33:44:55");
146a1d
+	g_assert (connection);
146a1d
+	nmtst_assert_connection_verifies_without_normalization (connection);
146a1d
+	g_assert_cmpstr (nm_connection_get_id (connection), ==, "00:11:22:33:44:55");
146a1d
+	g_assert_cmpstr (nm_connection_get_interface_name (connection), ==, NULL);
146a1d
+
146a1d
+	s_wired = nm_connection_get_setting_wired (connection);
146a1d
+	g_assert (s_wired);
146a1d
+	g_assert_cmpstr (nm_setting_wired_get_mac_address(s_wired), ==, "00:11:22:33:44:55");
146a1d
+
146a1d
+	s_ip6 = nm_connection_get_setting_ip6_config (connection);
146a1d
+	g_assert (s_ip6);
146a1d
+	g_assert_cmpstr (nm_setting_ip_config_get_method (s_ip6), ==, NM_SETTING_IP6_CONFIG_METHOD_MANUAL);
146a1d
+	g_assert (!nm_setting_ip_config_get_ignore_auto_dns (s_ip6));
146a1d
+	g_assert_cmpint (nm_setting_ip_config_get_num_dns (s_ip6), ==, 1);
146a1d
+	g_assert_cmpstr (nm_setting_ip_config_get_dns (s_ip6, 0), ==, "2001:db8::53");
146a1d
+	g_assert_cmpint (nm_setting_ip_config_get_num_routes (s_ip6), ==, 0);
146a1d
+	g_assert_cmpint (nm_setting_ip_config_get_num_addresses (s_ip6), ==, 1);
146a1d
+	ip_addr = nm_setting_ip_config_get_address (s_ip6, 0);
146a1d
+	g_assert (ip_addr);
146a1d
+	g_assert_cmpstr (nm_ip_address_get_address (ip_addr), ==, "2001:db8::42");
146a1d
+	g_assert_cmpint (nm_ip_address_get_prefix (ip_addr), ==, 64);
146a1d
+	g_assert_cmpstr (nm_setting_ip_config_get_gateway (s_ip6), ==, "2001:db8::1");
146a1d
+	g_assert_cmpstr (nm_setting_ip_config_get_dhcp_hostname (s_ip6), ==, "hostname0");
146a1d
+}
146a1d
+
146a1d
 static void
146a1d
 test_multiple_merge (void)
146a1d
 {
146a1d
@@ -1042,6 +1085,29 @@ test_ibft_ip_dev (void)
146a1d
 	g_assert_cmpstr (nm_setting_connection_get_interface_name (s_con), ==, NULL);
146a1d
 }
146a1d
 
146a1d
+static void
146a1d
+test_ibft_ip_dev_mac (void)
146a1d
+{
146a1d
+	const char *const*ARGV = NM_MAKE_STRV ("ip=00-53-06-66-ab-01:ibft");
146a1d
+	gs_unref_hashtable GHashTable *connections = NULL;
146a1d
+	NMSettingConnection *s_con;
146a1d
+	NMConnection *connection;
146a1d
+	gs_free char *hostname = NULL;
146a1d
+
146a1d
+	connections = nmi_cmdline_reader_parse (TEST_INITRD_DIR "/sysfs", ARGV, &hostname);
146a1d
+	g_assert (connections);
146a1d
+	g_assert_cmpint (g_hash_table_size (connections), ==, 1);
146a1d
+	g_assert_cmpstr (hostname, ==, NULL);
146a1d
+
146a1d
+	connection = g_hash_table_lookup (connections, "00:53:06:66:AB:01");
146a1d
+	g_assert (connection);
146a1d
+
146a1d
+	s_con = nm_connection_get_setting_connection (connection);
146a1d
+	g_assert (s_con);
146a1d
+	g_assert_cmpstr (nm_setting_connection_get_connection_type (s_con), ==, NM_SETTING_WIRED_SETTING_NAME);
146a1d
+	g_assert_cmpstr (nm_setting_connection_get_interface_name (s_con), ==, NULL);
146a1d
+}
146a1d
+
146a1d
 static void
146a1d
 _test_ibft_ip (const char *const*ARGV)
146a1d
 {
146a1d
@@ -1545,6 +1611,7 @@ int main (int argc, char **argv)
146a1d
 	g_test_add_func ("/initrd/cmdline/if_auto_with_mtu_and_mac", test_if_auto_with_mtu_and_mac);
146a1d
 	g_test_add_func ("/initrd/cmdline/if_ip4_manual", test_if_ip4_manual);
146a1d
 	g_test_add_func ("/initrd/cmdline/if_ip6_manual", test_if_ip6_manual);
146a1d
+	g_test_add_func ("/initrd/cmdline/if_mac_ifname", test_if_mac_ifname);
146a1d
 	g_test_add_func ("/initrd/cmdline/multiple/merge", test_multiple_merge);
146a1d
 	g_test_add_func ("/initrd/cmdline/multiple/bootdev", test_multiple_bootdev);
146a1d
 	g_test_add_func ("/initrd/cmdline/nameserver", test_nameserver);
146a1d
@@ -1558,6 +1625,7 @@ int main (int argc, char **argv)
146a1d
 	g_test_add_func ("/initrd/cmdline/bridge/default", test_bridge_default);
146a1d
 	g_test_add_func ("/initrd/cmdline/bridge/ip", test_bridge_ip);
146a1d
 	g_test_add_func ("/initrd/cmdline/ibft/ip_dev", test_ibft_ip_dev);
146a1d
+	g_test_add_func ("/initrd/cmdline/ibft/ip_dev_mac", test_ibft_ip_dev_mac);
146a1d
 	g_test_add_func ("/initrd/cmdline/ibft/ip", test_ibft_ip);
146a1d
 	g_test_add_func ("/initrd/cmdline/ibft/rd_iscsi_ibft", test_ibft_rd_iscsi_ibft);
146a1d
 	g_test_add_func ("/initrd/cmdline/ignore_extra", test_ignore_extra);
146a1d
-- 
146a1d
2.26.2
146a1d
146a1d
From a65c364d2cfe7a36f5207f652ec77bb42b532f88 Mon Sep 17 00:00:00 2001
146a1d
From: Beniamino Galvani <bgalvani@redhat.com>
146a1d
Date: Wed, 23 Sep 2020 10:58:03 +0200
146a1d
Subject: [PATCH 2/2] initrd: fix parsing IPv6 prefix length
146a1d
146a1d
The generator didn't accept prefix lengths > 32 for IPv6:
146a1d
146a1d
$ src/initrd/nm-initrd-generator --stdout -- ip=[fd01::1]:::40::ens0
146a1d
 <warn>  [1600851580.7875] cmdline-reader: Invalid IP mask: 40
146a1d
146a1d
https://bugzilla.redhat.com/show_bug.cgi?id=1879795
146a1d
(cherry picked from commit 364c7c278a19959c6e880329533937dbcf7b19a4)
146a1d
(cherry picked from commit 09804cac6ef2016cfa70d3e63c7d73f2dfc3cf91)
146a1d
---
146a1d
 src/initrd/nmi-cmdline-reader.c        | 5 +++--
146a1d
 src/initrd/tests/test-cmdline-reader.c | 3 ++-
146a1d
 2 files changed, 5 insertions(+), 3 deletions(-)
146a1d
146a1d
diff --git a/src/initrd/nmi-cmdline-reader.c b/src/initrd/nmi-cmdline-reader.c
146a1d
index 8196e9fb78..ee3dab2574 100644
146a1d
--- a/src/initrd/nmi-cmdline-reader.c
146a1d
+++ b/src/initrd/nmi-cmdline-reader.c
146a1d
@@ -426,12 +426,13 @@ reader_parse_ip (Reader *reader, const char *sysfs_dir, char *argument)
146a1d
 	s_ip6 = nm_connection_get_setting_ip6_config (connection);
146a1d
 
146a1d
 	if (netmask && *netmask) {
146a1d
+		gboolean is_ipv4 = client_ip_family == AF_INET;
146a1d
 		NMIPAddr addr;
146a1d
 
146a1d
-		if (nm_utils_parse_inaddr_bin (AF_INET, netmask, NULL, &addr))
146a1d
+		if (is_ipv4 && nm_utils_parse_inaddr_bin (AF_INET, netmask, NULL, &addr))
146a1d
 			client_ip_prefix = nm_utils_ip4_netmask_to_prefix (addr.addr4);
146a1d
 		else
146a1d
-			client_ip_prefix = _nm_utils_ascii_str_to_int64 (netmask, 10, 0, 32, -1);
146a1d
+			client_ip_prefix = _nm_utils_ascii_str_to_int64 (netmask, 10, 0, is_ipv4 ? 32 : 128, -1);
146a1d
 
146a1d
 		if (client_ip_prefix == -1)
146a1d
 			_LOGW (LOGD_CORE, "Invalid IP mask: %s", netmask);
146a1d
diff --git a/src/initrd/tests/test-cmdline-reader.c b/src/initrd/tests/test-cmdline-reader.c
146a1d
index 4a2f33fc02..d67f599337 100644
146a1d
--- a/src/initrd/tests/test-cmdline-reader.c
146a1d
+++ b/src/initrd/tests/test-cmdline-reader.c
146a1d
@@ -341,7 +341,7 @@ test_multiple_merge (void)
146a1d
 {
146a1d
 	gs_unref_hashtable GHashTable *connections = NULL;
146a1d
 	const char *const*ARGV = NM_MAKE_STRV ("ip=192.0.2.2:::::eth0",
146a1d
-	                                       "ip=[2001:db8::2]:::::eth0");
146a1d
+	                                       "ip=[2001:db8::2]:::56::eth0");
146a1d
 	NMConnection *connection;
146a1d
 	NMSettingConnection *s_con;
146a1d
 	NMSettingWired *s_wired;
146a1d
@@ -384,6 +384,7 @@ test_multiple_merge (void)
146a1d
 	ip_addr = nm_setting_ip_config_get_address (s_ip6, 0);
146a1d
 	g_assert (ip_addr);
146a1d
 	g_assert_cmpstr (nm_ip_address_get_address (ip_addr), ==, "2001:db8::2");
146a1d
+	g_assert_cmpint (nm_ip_address_get_prefix (ip_addr), ==, 56);
146a1d
 }
146a1d
 
146a1d
 static void
146a1d
-- 
146a1d
2.26.2
146a1d