summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLubomir Rintel <lkundrak@v3.sk>2019-11-18 13:41:14 +0100
committerLubomir Rintel <lkundrak@v3.sk>2019-11-18 13:41:14 +0100
commita65982c5ad7c15681fb6670b7a449380fce33c51 (patch)
tree99106ed32fe0f58bbb66c7ab5c41d5532c4e8d60
parent7a84388a9ba61b5f0e0fb98c05170c66b61608ca (diff)
parentcdfa3d342811f271597fa18ecc458bebf60a7989 (diff)
downloadNetworkManager-a65982c5ad7c15681fb6670b7a449380fce33c51.tar.gz
merge: branch 'lr/initrd-fixes'
https://gitlab.freedesktop.org/NetworkManager/NetworkManager/merge_requests/333
-rw-r--r--libnm-core/nm-utils.c17
-rw-r--r--libnm-core/tests/test-general.c10
-rw-r--r--src/initrd/nmi-cmdline-reader.c113
-rw-r--r--src/initrd/tests/test-cmdline-reader.c163
4 files changed, 239 insertions, 64 deletions
diff --git a/libnm-core/nm-utils.c b/libnm-core/nm-utils.c
index f4e55fc05c..c825bb12ba 100644
--- a/libnm-core/nm-utils.c
+++ b/libnm-core/nm-utils.c
@@ -4276,14 +4276,15 @@ nm_utils_hwaddr_matches (gconstpointer hwaddr1,
gsize l;
if (hwaddr1_len == -1) {
- g_return_val_if_fail (hwaddr1 != NULL, FALSE);
-
- if (!hwaddr_aton (hwaddr1, buf1, sizeof (buf1), &l)) {
+ if (hwaddr1 == NULL) {
+ hwaddr1_len = 0;
+ } else if (hwaddr_aton (hwaddr1, buf1, sizeof (buf1), &l)) {
+ hwaddr1 = buf1;
+ hwaddr1_len = l;
+ } else {
g_return_val_if_fail ((hwaddr2_len == -1 && hwaddr2) || (hwaddr2_len > 0 && hwaddr2_len <= NM_UTILS_HWADDR_LEN_MAX), FALSE);
return FALSE;
}
- hwaddr1 = buf1;
- hwaddr1_len = l;
} else {
g_return_val_if_fail (hwaddr1_len > 0 && hwaddr1_len <= NM_UTILS_HWADDR_LEN_MAX, FALSE);
@@ -4294,9 +4295,9 @@ nm_utils_hwaddr_matches (gconstpointer hwaddr1,
}
if (hwaddr2_len == -1) {
- g_return_val_if_fail (hwaddr2 != NULL, FALSE);
-
- if (!hwaddr_aton (hwaddr2, buf2, sizeof (buf2), &l))
+ if (hwaddr2 == NULL)
+ l = 0;
+ else if (!hwaddr_aton (hwaddr2, buf2, sizeof (buf2), &l))
return FALSE;
if (l != hwaddr1_len)
return FALSE;
diff --git a/libnm-core/tests/test-general.c b/libnm-core/tests/test-general.c
index 32e3a807d0..e91bba301d 100644
--- a/libnm-core/tests/test-general.c
+++ b/libnm-core/tests/test-general.c
@@ -3950,6 +3950,16 @@ test_hwaddr_equal (void)
g_assert (nm_utils_hwaddr_matches (null_binary, sizeof (null_binary), null_string, -1));
g_assert (nm_utils_hwaddr_matches (null_binary, sizeof (null_binary), null_binary, sizeof (null_binary)));
g_assert (nm_utils_hwaddr_matches (null_binary, sizeof (null_binary), NULL, ETH_ALEN));
+
+ g_assert (nm_utils_hwaddr_matches (NULL, -1, NULL, -1));
+ g_assert (!nm_utils_hwaddr_matches (NULL, -1, string, -1));
+ g_assert (!nm_utils_hwaddr_matches (string, -1, NULL, -1));
+ g_assert (!nm_utils_hwaddr_matches (NULL, -1, null_string, -1));
+ g_assert (!nm_utils_hwaddr_matches (null_string, -1, NULL, -1));
+ g_assert (!nm_utils_hwaddr_matches (NULL, -1, binary, sizeof (binary)));
+ g_assert (!nm_utils_hwaddr_matches (binary, sizeof (binary), NULL, -1));
+ g_assert (!nm_utils_hwaddr_matches (NULL, -1, null_binary, sizeof (null_binary)));
+ g_assert (!nm_utils_hwaddr_matches (null_binary, sizeof (null_binary), NULL, -1));
}
static void
diff --git a/src/initrd/nmi-cmdline-reader.c b/src/initrd/nmi-cmdline-reader.c
index a4f4687651..c7a01aaea1 100644
--- a/src/initrd/nmi-cmdline-reader.c
+++ b/src/initrd/nmi-cmdline-reader.c
@@ -32,6 +32,49 @@ _connection_matches_type (gpointer key, gpointer value, gpointer user_data)
}
static NMConnection *
+add_conn (GHashTable *connections,
+ const char *basename,
+ const char *id,
+ const char *ifname,
+ const char *type_name,
+ NMConnectionMultiConnect multi_connect)
+{
+ NMConnection *connection;
+ NMSetting *setting;
+
+ connection = nm_simple_connection_new ();
+ g_hash_table_insert (connections, g_strdup (basename), connection);
+
+ /* Start off assuming dynamic IP configurations. */
+
+ setting = nm_setting_ip4_config_new ();
+ nm_connection_add_setting (connection, setting);
+ g_object_set (setting,
+ NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO,
+ NM_SETTING_IP_CONFIG_MAY_FAIL, TRUE,
+ NULL);
+
+ setting = nm_setting_ip6_config_new ();
+ nm_connection_add_setting (connection, setting);
+ g_object_set (setting,
+ NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO,
+ NM_SETTING_IP_CONFIG_MAY_FAIL, TRUE,
+ NULL);
+
+ setting = nm_setting_connection_new ();
+ nm_connection_add_setting (connection, setting);
+ g_object_set (setting,
+ NM_SETTING_CONNECTION_ID, id,
+ NM_SETTING_CONNECTION_UUID, nm_utils_uuid_generate_a (),
+ NM_SETTING_CONNECTION_INTERFACE_NAME, ifname,
+ NM_SETTING_CONNECTION_TYPE, type_name,
+ NM_SETTING_CONNECTION_MULTI_CONNECT, multi_connect,
+ NULL);
+
+ return connection;
+}
+
+static NMConnection *
get_conn (GHashTable *connections, const char *ifname, const char *type_name)
{
NMConnection *connection;
@@ -61,40 +104,15 @@ get_conn (GHashTable *connections, const char *ifname, const char *type_name)
(gpointer) type_name);
}
- if (connection) {
- setting = (NMSetting *)nm_connection_get_setting_connection (connection);
- } else {
- connection = nm_simple_connection_new ();
- g_hash_table_insert (connections, g_strdup (basename), connection);
-
- /* Start off assuming dynamic IP configurations. */
-
- setting = nm_setting_ip4_config_new ();
- nm_connection_add_setting (connection, setting);
- g_object_set (setting,
- NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO,
- NM_SETTING_IP_CONFIG_MAY_FAIL, TRUE,
- NULL);
-
- setting = nm_setting_ip6_config_new ();
- nm_connection_add_setting (connection, setting);
- g_object_set (setting,
- NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO,
- NM_SETTING_IP_CONFIG_MAY_FAIL, TRUE,
- NULL);
-
- setting = nm_setting_connection_new ();
- nm_connection_add_setting (connection, setting);
- g_object_set (setting,
- NM_SETTING_CONNECTION_ID, ifname ?: "Wired Connection",
- NM_SETTING_CONNECTION_UUID, nm_utils_uuid_generate_a (),
- NM_SETTING_CONNECTION_INTERFACE_NAME, ifname,
- NM_SETTING_CONNECTION_MULTI_CONNECT, multi_connect,
- NULL);
-
+ if (!connection) {
if (!type_name)
type_name = NM_SETTING_WIRED_SETTING_NAME;
+
+ connection = add_conn (connections, basename,
+ ifname ?: "Wired Connection",
+ ifname, type_name, multi_connect);
}
+ setting = (NMSetting *)nm_connection_get_setting_connection (connection);
if (type_name) {
g_object_set (setting, NM_SETTING_CONNECTION_TYPE, type_name, NULL);
@@ -473,7 +491,10 @@ parse_ip (GHashTable *connections, const char *sysfs_dir, char *argument)
}
static void
-parse_master (GHashTable *connections, char *argument, const char *type_name)
+parse_master (GHashTable *connections,
+ char *argument,
+ const char *type_name,
+ const char *default_name)
{
NMConnection *connection;
NMSettingConnection *s_con;
@@ -489,7 +510,7 @@ parse_master (GHashTable *connections, char *argument, const char *type_name)
master = get_word (&argument, ':');
if (!master)
- master = master_to_free = g_strdup_printf ("%s0", type_name);
+ master = master_to_free = g_strdup_printf ("%s0", default_name ?: type_name);
slaves = get_word (&argument, ':');
connection = get_conn (connections, master, type_name);
@@ -627,6 +648,13 @@ parse_bootdev (GHashTable *connections, char *argument)
connection = get_conn (connections, NULL, NULL);
+ if ( nm_connection_get_interface_name (connection)
+ && strcmp (nm_connection_get_interface_name (connection), argument) != 0) {
+ /* If the default connection already has an interface name,
+ * we should not overwrite it. Create a new one instead. */
+ connection = get_conn (connections, argument, NULL);
+ }
+
s_con = nm_connection_get_setting_connection (connection);
g_object_set (s_con,
NM_SETTING_CONNECTION_INTERFACE_NAME, argument,
@@ -796,11 +824,11 @@ nmi_cmdline_reader_parse (const char *sysfs_dir, const char *const*argv)
else if (strcmp (tag, "rd.route") == 0)
parse_rd_route (connections, argument);
else if (strcmp (tag, "bridge") == 0)
- parse_master (connections, argument, NM_SETTING_BRIDGE_SETTING_NAME);
+ parse_master (connections, argument, NM_SETTING_BRIDGE_SETTING_NAME, "br");
else if (strcmp (tag, "bond") == 0)
- parse_master (connections, argument, NM_SETTING_BOND_SETTING_NAME);
+ parse_master (connections, argument, NM_SETTING_BOND_SETTING_NAME, NULL);
else if (strcmp (tag, "team") == 0)
- parse_master (connections, argument, NM_SETTING_TEAM_SETTING_NAME);
+ parse_master (connections, argument, NM_SETTING_TEAM_SETTING_NAME, NULL);
else if (strcmp (tag, "vlan") == 0)
parse_vlan (connections, argument);
else if (strcmp (tag, "bootdev") == 0)
@@ -839,8 +867,19 @@ nmi_cmdline_reader_parse (const char *sysfs_dir, const char *const*argv)
}
connection = get_conn (connections, NULL, NM_SETTING_WIRED_SETTING_NAME);
-
s_wired = nm_connection_get_setting_wired (connection);
+
+ if ( nm_connection_get_interface_name (connection)
+ || ( nm_setting_wired_get_mac_address (s_wired)
+ && !nm_utils_hwaddr_matches (nm_setting_wired_get_mac_address (s_wired), -1,
+ bootif, -1))) {
+ connection = add_conn (connections, "bootif_connection", "BOOTIF Connection",
+ NULL, 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);
+ }
+
g_object_set (s_wired,
NM_SETTING_WIRED_MAC_ADDRESS, bootif,
NULL);
diff --git a/src/initrd/tests/test-cmdline-reader.c b/src/initrd/tests/test-cmdline-reader.c
index a69c374147..1d4bb9a6e8 100644
--- a/src/initrd/tests/test-cmdline-reader.c
+++ b/src/initrd/tests/test-cmdline-reader.c
@@ -268,8 +268,7 @@ test_multiple (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",
- "BOOTIF=00:53:AB:cd:02:03");
+ "ip=[2001:db8::2]:::::eth0");
NMConnection *connection;
NMSettingWired *s_wired;
NMSettingIPConfig *s_ip4;
@@ -287,7 +286,6 @@ test_multiple (void)
s_wired = nm_connection_get_setting_wired (connection);
g_assert (s_wired);
- g_assert_cmpstr (nm_setting_wired_get_mac_address (s_wired), ==, "00:53:AB:CD:02:03");
s_ip4 = nm_connection_get_setting_ip4_config (connection);
g_assert (s_ip4);
@@ -309,12 +307,45 @@ test_multiple (void)
}
static void
+test_bootdev (void)
+{
+ gs_unref_hashtable GHashTable *connections = NULL;
+ const char *const*ARGV = NM_MAKE_STRV ("vlan=vlan2:ens5", "bootdev=ens3");
+ NMConnection *connection;
+ NMSettingConnection *s_con;
+
+ connections = nmi_cmdline_reader_parse (TEST_INITRD_DIR "/sysfs", ARGV);
+ g_assert (connections);
+ g_assert_cmpint (g_hash_table_size (connections), ==, 2);
+
+ connection = g_hash_table_lookup (connections, "ens3");
+ g_assert (connection);
+ nmtst_assert_connection_verifies_without_normalization (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_id (s_con), ==, "ens3");
+ g_assert_cmpstr (nm_setting_connection_get_interface_name (s_con), ==, "ens3");
+
+ connection = g_hash_table_lookup (connections, "vlan2");
+ g_assert (connection);
+ nmtst_assert_connection_verifies_without_normalization (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_VLAN_SETTING_NAME);
+ g_assert_cmpstr (nm_setting_connection_get_id (s_con), ==, "vlan2");
+ g_assert_cmpstr (nm_setting_connection_get_interface_name (s_con), ==, "vlan2");
+}
+
+static void
test_some_more (void)
{
gs_unref_hashtable GHashTable *connections = NULL;
const char *const*ARGV = NM_MAKE_STRV ("bootdev=eth1", "hail", "nameserver=[2001:DB8:3::53]",
"satan", "nameserver=192.0.2.53", "worship",
- "BOOTIF=01-00-53-AB-cd-02-03", "doom", "rd.peerdns=0",
+ "doom", "rd.peerdns=0",
"rd.route=[2001:DB8:3::/48]:[2001:DB8:2::1]:ens10");
NMConnection *connection;
NMSettingConnection *s_con;
@@ -340,7 +371,6 @@ test_some_more (void)
s_wired = nm_connection_get_setting_wired (connection);
g_assert (s_wired);
- g_assert_cmpstr (nm_setting_wired_get_mac_address (s_wired), ==, "00:53:AB:CD:02:03");
s_ip4 = nm_connection_get_setting_ip4_config (connection);
g_assert (s_ip4);
@@ -391,17 +421,6 @@ test_some_more (void)
}
static void
-test_no_bootif (void)
-{
- gs_unref_hashtable GHashTable *connections = NULL;
- const char *const*ARGV = NM_MAKE_STRV ("BOOTIF=01-00-53-AB-cd-02-03", "rd.bootif=0");
-
- connections = nmi_cmdline_reader_parse (TEST_INITRD_DIR "/sysfs", ARGV);
- g_assert (connections);
- g_assert_cmpint (g_hash_table_size (connections), ==, 0);
-}
-
-static void
test_bond (void)
{
gs_unref_hashtable GHashTable *connections = NULL;
@@ -636,12 +655,12 @@ test_bridge_default (void)
g_assert (connections);
g_assert_cmpint (g_hash_table_size (connections), ==, 2);
- connection = g_hash_table_lookup (connections, "bridge0");
+ connection = g_hash_table_lookup (connections, "br0");
g_assert (connection);
nmtst_assert_connection_verifies_without_normalization (connection);
g_assert_cmpstr (nm_connection_get_connection_type (connection), ==, NM_SETTING_BRIDGE_SETTING_NAME);
- g_assert_cmpstr (nm_connection_get_id (connection), ==, "bridge0");
+ g_assert_cmpstr (nm_connection_get_id (connection), ==, "br0");
master_uuid = nm_connection_get_uuid (connection);
g_assert (master_uuid);
@@ -902,6 +921,109 @@ test_rd_znet_legacy (void)
nmtst_assert_connection_verifies_without_normalization (connection);
}
+static void
+test_bootif (void)
+{
+ gs_unref_hashtable GHashTable *connections = NULL;
+ const char *const*ARGV = NM_MAKE_STRV ("BOOTIF=00:53:AB:cd:02:03",
+ "ip=dhcp");
+ NMConnection *connection;
+ NMSettingWired *s_wired;
+ NMSettingIPConfig *s_ip4;
+ NMSettingIPConfig *s_ip6;
+
+ connections = nmi_cmdline_reader_parse (TEST_INITRD_DIR "/sysfs", ARGV);
+ g_assert (connections);
+ g_assert_cmpint (g_hash_table_size (connections), ==, 1);
+
+ connection = g_hash_table_lookup (connections, "default_connection");
+ g_assert (connection);
+ nmtst_assert_connection_verifies_without_normalization (connection);
+ g_assert_cmpstr (nm_connection_get_id (connection), ==, "Wired Connection");
+
+ s_wired = nm_connection_get_setting_wired (connection);
+ g_assert_cmpstr (nm_setting_wired_get_mac_address (s_wired), ==, "00:53:AB:CD:02:03");
+ g_assert (s_wired);
+
+ s_ip4 = nm_connection_get_setting_ip4_config (connection);
+ g_assert (s_ip4);
+ g_assert_cmpstr (nm_setting_ip_config_get_method (s_ip4), ==, NM_SETTING_IP4_CONFIG_METHOD_AUTO);
+ g_assert (!nm_setting_ip_config_get_ignore_auto_dns (s_ip4));
+ g_assert (!nm_setting_ip_config_get_may_fail (s_ip4));
+
+ 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_DISABLED);
+ g_assert (!nm_setting_ip_config_get_ignore_auto_dns (s_ip6));
+}
+
+static void
+test_bootif_hwtype (void)
+{
+ gs_unref_hashtable GHashTable *connections = NULL;
+ const char *const*ARGV = NM_MAKE_STRV ("ip=eth0:dhcp",
+ "BOOTIF=01-00-53-AB-cd-02-03");
+ NMConnection *connection;
+ NMSettingWired *s_wired;
+ NMSettingIPConfig *s_ip4;
+ NMSettingIPConfig *s_ip6;
+
+ connections = nmi_cmdline_reader_parse (TEST_INITRD_DIR "/sysfs", ARGV);
+ g_assert (connections);
+ g_assert_cmpint (g_hash_table_size (connections), ==, 2);
+
+ connection = g_hash_table_lookup (connections, "eth0");
+ g_assert (connection);
+ nmtst_assert_connection_verifies_without_normalization (connection);
+ g_assert_cmpstr (nm_connection_get_id (connection), ==, "eth0");
+
+ s_wired = nm_connection_get_setting_wired (connection);
+ g_assert (!nm_setting_wired_get_mac_address (s_wired));
+ g_assert (s_wired);
+
+ s_ip4 = nm_connection_get_setting_ip4_config (connection);
+ g_assert (s_ip4);
+ g_assert_cmpstr (nm_setting_ip_config_get_method (s_ip4), ==, NM_SETTING_IP4_CONFIG_METHOD_AUTO);
+ g_assert (!nm_setting_ip_config_get_ignore_auto_dns (s_ip4));
+ g_assert (!nm_setting_ip_config_get_may_fail (s_ip4));
+
+ 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_DISABLED);
+ g_assert (!nm_setting_ip_config_get_ignore_auto_dns (s_ip6));
+
+ connection = g_hash_table_lookup (connections, "bootif_connection");
+ g_assert (connection);
+ nmtst_assert_connection_verifies_without_normalization (connection);
+ g_assert_cmpstr (nm_connection_get_id (connection), ==, "BOOTIF Connection");
+
+ s_wired = nm_connection_get_setting_wired (connection);
+ g_assert_cmpstr (nm_setting_wired_get_mac_address (s_wired), ==, "00:53:AB:CD:02:03");
+ g_assert (s_wired);
+
+ s_ip4 = nm_connection_get_setting_ip4_config (connection);
+ g_assert (s_ip4);
+ g_assert_cmpstr (nm_setting_ip_config_get_method (s_ip4), ==, NM_SETTING_IP4_CONFIG_METHOD_AUTO);
+ g_assert (!nm_setting_ip_config_get_ignore_auto_dns (s_ip4));
+ g_assert (nm_setting_ip_config_get_may_fail (s_ip4));
+
+ 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_AUTO);
+ g_assert (!nm_setting_ip_config_get_ignore_auto_dns (s_ip6));
+ g_assert (nm_setting_ip_config_get_may_fail (s_ip6));
+}
+
+static void
+test_bootif_off (void)
+{
+ gs_unref_hashtable GHashTable *connections = NULL;
+ const char *const*ARGV = NM_MAKE_STRV ("BOOTIF=01-00-53-AB-cd-02-03", "rd.bootif=0");
+
+ connections = nmi_cmdline_reader_parse (TEST_INITRD_DIR "/sysfs", ARGV);
+ g_assert (connections);
+ g_assert_cmpint (g_hash_table_size (connections), ==, 0);
+}
NMTST_DEFINE ();
@@ -917,7 +1039,7 @@ int main (int argc, char **argv)
g_test_add_func ("/initrd/cmdline/if_ip6_manual", test_if_ip6_manual);
g_test_add_func ("/initrd/cmdline/multiple", test_multiple);
g_test_add_func ("/initrd/cmdline/some_more", test_some_more);
- g_test_add_func ("/initrd/cmdline/no_bootif", test_no_bootif);
+ g_test_add_func ("/initrd/cmdline/bootdev", test_bootdev);
g_test_add_func ("/initrd/cmdline/bond", test_bond);
g_test_add_func ("/initrd/cmdline/bond/default", test_bond_default);
g_test_add_func ("/initrd/cmdline/team", test_team);
@@ -927,6 +1049,9 @@ int main (int argc, char **argv)
g_test_add_func ("/initrd/cmdline/ignore_extra", test_ignore_extra);
g_test_add_func ("/initrd/cmdline/rd_znet", test_rd_znet);
g_test_add_func ("/initrd/cmdline/rd_znet/legacy", test_rd_znet_legacy);
+ g_test_add_func ("/initrd/cmdline/bootif", test_bootif);
+ g_test_add_func ("/initrd/cmdline/bootif/hwtype", test_bootif_hwtype);
+ g_test_add_func ("/initrd/cmdline/bootif/off", test_bootif_off);
return g_test_run ();
}