diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/initrd/nmi-cmdline-reader.c | 374 | ||||
-rw-r--r-- | src/initrd/tests/test-cmdline-reader.c | 42 |
2 files changed, 254 insertions, 162 deletions
diff --git a/src/initrd/nmi-cmdline-reader.c b/src/initrd/nmi-cmdline-reader.c index 6f50c08c3f..38d6d05210 100644 --- a/src/initrd/nmi-cmdline-reader.c +++ b/src/initrd/nmi-cmdline-reader.c @@ -20,6 +20,8 @@ typedef struct { GHashTable *hash; GPtrArray *array; + NMConnection *bootdev_connection; /* connection for bootdev=$ifname */ + NMConnection *default_connection; /* connection not bound to any ifname */ } Reader; static Reader * @@ -98,24 +100,32 @@ create_connection (Reader *reader, } static NMConnection * -get_connection (Reader *reader, const char *ifname, const char *type_name) +get_default_connection (Reader *reader) { - NMConnection *connection; - NMSetting *setting; - const char *basename; - NMConnectionMultiConnect multi_connect; + NMConnection *con; + + if (!reader->default_connection) { + con = create_connection (reader, + "default_connection", + "Wired Connection", + NULL, + NM_SETTING_WIRED_SETTING_NAME, + NM_CONNECTION_MULTI_CONNECT_MULTIPLE); + reader->default_connection = con; + } + return reader->default_connection; +} - if (ifname) { - basename = ifname; - multi_connect = NM_CONNECTION_MULTI_CONNECT_SINGLE; - } else { - /* This is essentially for the "ip=dhcp" scenario. */ - basename = "default_connection"; - multi_connect = NM_CONNECTION_MULTI_CONNECT_MULTIPLE; - } +static NMConnection * +get_connection (Reader *reader, + const char *ifname, + const char *type_name, + gboolean create_if_missing) +{ + NMConnection *connection = NULL; + NMSetting *setting; - connection = g_hash_table_lookup (reader->hash, (gpointer) basename); - if (!connection && !ifname) { + if (!ifname) { NMConnection *candidate; NMSettingConnection *s_con; guint i; @@ -126,7 +136,6 @@ get_connection (Reader *reader, const char *ifname, const char *type_name) * This is so that things like "bond=bond0:eth1,eth2 nameserver=1.3.3.7 end up * slapping the nameserver to the most reasonable connection (bond0). */ - for (i = 0; i < reader->array->len; i++) { candidate = g_hash_table_lookup (reader->hash, reader->array->pdata[i]); s_con = nm_connection_get_setting_connection (candidate); @@ -143,17 +152,22 @@ get_connection (Reader *reader, const char *ifname, const char *type_name) break; } } - } + } else + connection = g_hash_table_lookup (reader->hash, (gpointer) ifname); if (!connection) { + if (!create_if_missing) + return NULL; + if (!type_name) type_name = NM_SETTING_WIRED_SETTING_NAME; - connection = create_connection (reader, basename, + connection = create_connection (reader, ifname, ifname ?: "Wired Connection", - ifname, type_name, multi_connect); + ifname, type_name, + NM_CONNECTION_MULTI_CONNECT_SINGLE); } - setting = (NMSetting *)nm_connection_get_setting_connection (connection); + setting = (NMSetting *) nm_connection_get_setting_connection (connection); if (type_name) { g_object_set (setting, NM_SETTING_CONNECTION_TYPE, type_name, NULL); @@ -353,7 +367,11 @@ parse_ip (Reader *reader, const char *sysfs_dir, char *argument) } /* Parsing done, construct the NMConnection. */ - connection = get_connection (reader, ifname, NULL); + if (ifname) + connection = get_connection (reader, ifname, NULL, TRUE); + else + connection = get_default_connection (reader); + s_ip4 = nm_connection_get_setting_ip4_config (connection); s_ip6 = nm_connection_get_setting_ip6_config (connection); @@ -566,7 +584,7 @@ parse_master (Reader *reader, master = master_to_free = g_strdup_printf ("%s0", default_name ?: type_name); slaves = get_word (&argument, ':'); - connection = get_connection (reader, master, type_name); + connection = get_connection (reader, master, type_name, TRUE); s_con = nm_connection_get_setting_connection (connection); master = nm_setting_connection_get_uuid (s_con); @@ -588,7 +606,7 @@ parse_master (Reader *reader, if (slave == NULL) slave = "eth0"; - connection = get_connection (reader, slave, NULL); + connection = get_connection (reader, slave, NULL, TRUE); s_con = nm_connection_get_setting_connection (connection); g_object_set (s_con, NM_SETTING_CONNECTION_SLAVE_TYPE, type_name, @@ -603,65 +621,77 @@ parse_master (Reader *reader, } static void -parse_rd_route (Reader *reader, char *argument) +add_routes (Reader *reader, GPtrArray *array) { - NMConnection *connection; - const char *net; - const char *gateway; - const char *interface; - int family = AF_UNSPEC; - NMIPAddr net_addr = { }; - NMIPAddr gateway_addr = { }; - int net_prefix = -1; - NMIPRoute *route; - NMSettingIPConfig *s_ip; - GError *error = NULL; - - net = get_word (&argument, ':'); - gateway = get_word (&argument, ':'); - interface = get_word (&argument, ':'); - - connection = get_connection (reader, interface, NULL); + guint i; + + for (i = 0; i < array->len; i++) { + NMConnection *connection = NULL; + const char *net; + const char *gateway; + const char *interface; + int family = AF_UNSPEC; + NMIPAddr net_addr = { }; + NMIPAddr gateway_addr = { }; + int net_prefix = -1; + NMIPRoute *route; + NMSettingIPConfig *s_ip; + char *argument; + gs_free_error GError *error = NULL; + + argument = array->pdata[i]; + net = get_word (&argument, ':'); + gateway = get_word (&argument, ':'); + interface = get_word (&argument, ':'); + + if (interface) + connection = get_connection (reader, interface, NULL, TRUE); + if (!connection) + connection = reader->bootdev_connection; + if (!connection) + connection = get_connection (reader, interface, NULL, FALSE); + if (!connection) + connection = get_default_connection (reader); + + if (net && *net) { + if (!nm_utils_parse_inaddr_prefix_bin (family, net, &family, &net_addr, &net_prefix)) { + _LOGW (LOGD_CORE, "Unrecognized address: %s", net); + continue; + } + } - if (net && *net) { - if (!nm_utils_parse_inaddr_prefix_bin (family, net, &family, &net_addr, &net_prefix)) { - _LOGW (LOGD_CORE, "Unrecognized address: %s", net); - return; + if (gateway && *gateway) { + if (!nm_utils_parse_inaddr_bin (family, gateway, &family, &gateway_addr)) { + _LOGW (LOGD_CORE, "Unrecognized address: %s", gateway); + continue; + } } - } - if (gateway && *gateway) { - if (!nm_utils_parse_inaddr_bin (family, gateway, &family, &gateway_addr)) { - _LOGW (LOGD_CORE, "Unrecognized address: %s", gateway); - return; + switch (family) { + case AF_INET: + s_ip = nm_connection_get_setting_ip4_config (connection); + if (net_prefix == -1) + net_prefix = 32; + break; + case AF_INET6: + s_ip = nm_connection_get_setting_ip6_config (connection); + if (net_prefix == -1) + net_prefix = 128; + break; + default: + _LOGW (LOGD_CORE, "Unknown address family: %s", net); + continue; } - } - switch (family) { - case AF_INET: - s_ip = nm_connection_get_setting_ip4_config (connection); - if (net_prefix == -1) - net_prefix = 32; - break; - case AF_INET6: - s_ip = nm_connection_get_setting_ip6_config (connection); - if (net_prefix == -1) - net_prefix = 128; - break; - default: - _LOGW (LOGD_CORE, "Unknown address family: %s", net); - return; - } + route = nm_ip_route_new_binary (family, &net_addr.addr_ptr, net_prefix, &gateway_addr.addr_ptr, -1, &error); + if (!route) { + g_warning ("Invalid route '%s via %s': %s\n", net, gateway, error->message); + continue; + } - route = nm_ip_route_new_binary (family, &net_addr.addr_ptr, net_prefix, &gateway_addr.addr_ptr, -1, &error); - if (!route) { - g_warning ("Invalid route '%s via %s': %s\n", net, gateway, error->message); - g_clear_error (&error); - return; + nm_setting_ip_config_add_route (s_ip, route); + nm_ip_route_unref (route); } - - nm_setting_ip_config_add_route (s_ip, route); - nm_ip_route_unref (route); } static void @@ -681,7 +711,7 @@ parse_vlan (Reader *reader, char *argument) break; } - connection = get_connection (reader, vlan, NM_SETTING_VLAN_SETTING_NAME); + connection = get_connection (reader, vlan, NM_SETTING_VLAN_SETTING_NAME, TRUE); s_vlan = nm_connection_get_setting_vlan (connection); g_object_set (s_vlan, @@ -694,76 +724,6 @@ parse_vlan (Reader *reader, char *argument) } static void -parse_bootdev (Reader *reader, char *argument) -{ - NMConnection *connection; - NMSettingConnection *s_con; - - connection = get_connection (reader, 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_connection (reader, argument, NULL); - } - - s_con = nm_connection_get_setting_connection (connection); - g_object_set (s_con, - NM_SETTING_CONNECTION_INTERFACE_NAME, argument, - NULL); -} - -static void -parse_nameserver (Reader *reader, char *argument) -{ - NMConnection *connection; - NMSettingIPConfig *s_ip = NULL; - char *dns; - - connection = get_connection (reader, NULL, NULL); - - dns = get_word (&argument, '\0'); - - switch (guess_ip_address_family (dns)) { - case AF_INET: - s_ip = nm_connection_get_setting_ip4_config (connection); - break; - case AF_INET6: - s_ip = nm_connection_get_setting_ip6_config (connection); - break; - default: - _LOGW (LOGD_CORE, "Unknown address family: %s", dns); - break; - } - - nm_setting_ip_config_add_dns (s_ip, dns); - - if (argument && *argument) - _LOGW (LOGD_CORE, "Ignoring extra: '%s'.", argument); -} - -static void -parse_rd_peerdns (Reader *reader, char *argument) -{ - gboolean auto_dns = !_nm_utils_ascii_str_to_bool (argument, TRUE); - NMConnection *connection; - NMSettingIPConfig *s_ip = NULL; - - connection = get_connection (reader, NULL, NULL); - - s_ip = nm_connection_get_setting_ip4_config (connection); - g_object_set (s_ip, - NM_SETTING_IP_CONFIG_IGNORE_AUTO_DNS, auto_dns, - NULL); - - s_ip = nm_connection_get_setting_ip6_config (connection); - g_object_set (s_ip, - NM_SETTING_IP_CONFIG_IGNORE_AUTO_DNS, auto_dns, - NULL); -} - -static void parse_rd_znet (Reader *reader, char *argument, gboolean net_ifnames) { const char *nettype; @@ -814,7 +774,7 @@ parse_rd_znet (Reader *reader, char *argument, gboolean net_ifnames) ifname = g_strdup_printf ("%s%d", prefix, index); } - connection = get_connection (reader, ifname, NM_SETTING_WIRED_SETTING_NAME); + connection = get_connection (reader, ifname, NM_SETTING_WIRED_SETTING_NAME, TRUE); s_wired = nm_connection_get_setting_wired (connection); g_object_set (s_wired, NM_SETTING_WIRED_S390_NETTYPE, nettype, @@ -844,6 +804,72 @@ _normalize_conn (gpointer key, gpointer value, gpointer user_data) nm_connection_normalize (connection, NULL, NULL, NULL); } +static void +set_ignore_auto_dns (Reader *reader) +{ + GHashTableIter iter; + NMConnection *connection; + NMSettingIPConfig *s_ip = NULL; + + g_hash_table_iter_init (&iter, reader->hash); + while (g_hash_table_iter_next (&iter, NULL, (gpointer *) &connection)) { + s_ip = nm_connection_get_setting_ip4_config (connection); + g_object_set (s_ip, + NM_SETTING_IP_CONFIG_IGNORE_AUTO_DNS, TRUE, + NULL); + + s_ip = nm_connection_get_setting_ip6_config (connection); + g_object_set (s_ip, + NM_SETTING_IP_CONFIG_IGNORE_AUTO_DNS, TRUE, + NULL); + } +} + + +static void +add_nameservers (Reader *reader, GPtrArray *nameservers) +{ + NMConnection *connection; + NMSettingIPConfig *s_ip; + const char *ns; + guint i; + + connection = reader->bootdev_connection; + if (!connection) + connection = reader->default_connection; + if (!connection) + connection = get_connection (reader, NULL, NULL, FALSE); + if (!connection) + return; + + for (i = 0; i < nameservers->len; i++) { + ns = nameservers->pdata[i]; + + switch (guess_ip_address_family (ns)) { + case AF_INET: + s_ip = nm_connection_get_setting_ip4_config (connection); + if (!NM_IN_STRSET (nm_setting_ip_config_get_method (s_ip), + NM_SETTING_IP4_CONFIG_METHOD_AUTO, + NM_SETTING_IP4_CONFIG_METHOD_MANUAL)) + continue; + break; + case AF_INET6: + s_ip = nm_connection_get_setting_ip6_config (connection); + if (!NM_IN_STRSET (nm_setting_ip_config_get_method (s_ip), + NM_SETTING_IP6_CONFIG_METHOD_AUTO, + NM_SETTING_IP6_CONFIG_METHOD_DHCP, + NM_SETTING_IP6_CONFIG_METHOD_MANUAL)) + continue; + break; + default: + _LOGW (LOGD_CORE, "Unknown address family: %s", ns); + continue; + } + + nm_setting_ip_config_add_dns (s_ip, ns); + } +} + GHashTable * nmi_cmdline_reader_parse (const char *sysfs_dir, const char *const*argv) { @@ -853,7 +879,11 @@ nmi_cmdline_reader_parse (const char *sysfs_dir, const char *const*argv) gboolean ignore_bootif = FALSE; gboolean neednet = FALSE; gs_free char *bootif_val = NULL; + gs_free char *bootdev = NULL; gboolean net_ifnames = TRUE; + gs_unref_ptrarray GPtrArray *nameservers = NULL; + gs_unref_ptrarray GPtrArray *routes = NULL; + gboolean ignore_auto_dns = FALSE; int i; reader = reader_init (); @@ -868,16 +898,19 @@ nmi_cmdline_reader_parse (const char *sysfs_dir, const char *const*argv) for (i = 0; argv[i]; i++) { gs_free char *argument_clone = NULL; char *argument; + char *word; argument_clone = g_strdup (argv[i]); argument = argument_clone; tag = get_word (&argument, '='); - if (strcmp (tag, "ip") == 0) + if (nm_streq (tag, "ip")) parse_ip (reader, sysfs_dir, argument); - else if (strcmp (tag, "rd.route") == 0) - parse_rd_route (reader, argument); - else if (strcmp (tag, "bridge") == 0) + else if (strcmp (tag, "rd.route") == 0) { + if (!routes) + routes = g_ptr_array_new_with_free_func (g_free); + g_ptr_array_add (routes, g_strdup (argument)); + } else if (strcmp (tag, "bridge") == 0) parse_master (reader, argument, NM_SETTING_BRIDGE_SETTING_NAME, "br"); else if (strcmp (tag, "bond") == 0) parse_master (reader, argument, NM_SETTING_BOND_SETTING_NAME, NULL); @@ -886,11 +919,18 @@ nmi_cmdline_reader_parse (const char *sysfs_dir, const char *const*argv) else if (strcmp (tag, "vlan") == 0) parse_vlan (reader, argument); else if (strcmp (tag, "bootdev") == 0) - parse_bootdev (reader, argument); - else if (strcmp (tag, "nameserver") == 0) - parse_nameserver (reader, argument); - else if (strcmp (tag, "rd.peerdns") == 0) - parse_rd_peerdns (reader, argument); + bootdev = g_strdup (argument); + else if (strcmp (tag, "nameserver") == 0) { + word = get_word (&argument, '\0'); + if (word) { + if (!nameservers) + nameservers = g_ptr_array_new_with_free_func (g_free); + g_ptr_array_add (nameservers, g_strdup (word)); + } + if (argument && *argument) + _LOGW (LOGD_CORE, "Ignoring extra: '%s'.", argument); + } else if (strcmp (tag, "rd.peerdns") == 0) + ignore_auto_dns = !_nm_utils_ascii_str_to_bool (argument, TRUE); else if (strcmp (tag, "rd.iscsi.ibft") == 0 && _nm_utils_ascii_str_to_bool (argument, TRUE)) read_all_connections_from_fw (reader, sysfs_dir); else if (strcmp (tag, "rd.bootif") == 0) @@ -922,7 +962,10 @@ nmi_cmdline_reader_parse (const char *sysfs_dir, const char *const*argv) bootif += 3; } - connection = get_connection (reader, NULL, NM_SETTING_WIRED_SETTING_NAME); + connection = get_connection (reader, NULL, NM_SETTING_WIRED_SETTING_NAME, FALSE); + if (!connection) + connection = get_default_connection (reader); + s_wired = nm_connection_get_setting_wired (connection); if ( nm_connection_get_interface_name (connection) @@ -940,11 +983,28 @@ nmi_cmdline_reader_parse (const char *sysfs_dir, const char *const*argv) NM_SETTING_WIRED_MAC_ADDRESS, bootif, NULL); } + + if (bootdev) { + NMConnection *connection; + + connection = get_connection (reader, bootdev, NULL, TRUE); + reader->bootdev_connection = connection; + } + if (neednet && g_hash_table_size (reader->hash) == 0) { /* Make sure there's some connection. */ - get_connection (reader, NULL, NM_SETTING_WIRED_SETTING_NAME); + get_default_connection (reader); } + if (routes) + add_routes (reader, routes); + + if (nameservers) + add_nameservers (reader, nameservers); + + if (ignore_auto_dns) + set_ignore_auto_dns (reader); + g_hash_table_foreach (reader->hash, _normalize_conn, NULL); ret = g_hash_table_ref (reader->hash); reader_destroy (reader); diff --git a/src/initrd/tests/test-cmdline-reader.c b/src/initrd/tests/test-cmdline-reader.c index 8951e4913e..4f71a91e3e 100644 --- a/src/initrd/tests/test-cmdline-reader.c +++ b/src/initrd/tests/test-cmdline-reader.c @@ -264,7 +264,7 @@ test_if_ip6_manual (void) } static void -test_multiple (void) +test_multiple_merge (void) { gs_unref_hashtable GHashTable *connections = NULL; const char *const*ARGV = NM_MAKE_STRV ("ip=192.0.2.2:::::eth0", @@ -307,6 +307,37 @@ test_multiple (void) } static void +test_multiple_bootdev (void) +{ + gs_unref_hashtable GHashTable *connections = NULL; + const char *const*ARGV = NM_MAKE_STRV ("nameserver=1.2.3.4", + "ip=eth3:auto6", + "ip=eth4:dhcp", + "bootdev=eth4"); + NMConnection *connection; + 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, "eth3"); + g_assert (connection); + 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); + + connection = g_hash_table_lookup (connections, "eth4"); + g_assert (connection); + 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_cmpint (nm_setting_ip_config_get_num_dns (s_ip4), ==, 1); + g_assert_cmpstr (nm_setting_ip_config_get_dns (s_ip4, 0), ==, "1.2.3.4"); +} + +static void test_bootdev (void) { gs_unref_hashtable GHashTable *connections = NULL; @@ -358,16 +389,16 @@ test_some_more (void) g_assert (connections); g_assert_cmpint (g_hash_table_size (connections), ==, 2); - connection = g_hash_table_lookup (connections, "default_connection"); + connection = g_hash_table_lookup (connections, "eth1"); 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), ==, "Wired Connection"); + g_assert_cmpstr (nm_setting_connection_get_id (s_con), ==, "eth1"); g_assert_cmpstr (nm_setting_connection_get_interface_name (s_con), ==, "eth1"); - g_assert_cmpint (nm_setting_connection_get_multi_connect (s_con), ==, NM_CONNECTION_MULTI_CONNECT_MULTIPLE); + g_assert_cmpint (nm_setting_connection_get_multi_connect (s_con), ==, NM_CONNECTION_MULTI_CONNECT_SINGLE); s_wired = nm_connection_get_setting_wired (connection); g_assert (s_wired); @@ -1075,7 +1106,8 @@ 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/multiple", test_multiple); + 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/some_more", test_some_more); g_test_add_func ("/initrd/cmdline/bootdev", test_bootdev); g_test_add_func ("/initrd/cmdline/bond", test_bond); |