From eaad7ae4312568bf7345ecdea300b98adfa44644 Mon Sep 17 00:00:00 2001 From: Beniamino Galvani Date: Mon, 12 Sep 2016 18:51:00 +0200 Subject: [PATCH] libnm-core: drop extra IPs from shared connections during normalization The core only consider the first address for shared connections, don't pretend we accept multiple addresses. This change doesn't prevent supporting multiple addresses in the future. https://bugzilla.gnome.org/show_bug.cgi?id=763937 --- libnm-core/nm-connection.c | 10 ++++++ libnm-core/nm-setting-ip4-config.c | 14 ++++++++ libnm-core/tests/test-general.c | 54 ++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+) diff --git a/libnm-core/nm-connection.c b/libnm-core/nm-connection.c index 57f964095e..3e9db68f41 100644 --- a/libnm-core/nm-connection.c +++ b/libnm-core/nm-connection.c @@ -726,6 +726,7 @@ _normalize_ip_config (NMConnection *self, GHashTable *parameters) NMSettingIPConfig *s_ip4, *s_ip6; NMSetting *setting; gboolean changed = FALSE; + guint num, i; if (parameters) default_ip6_method = g_hash_table_lookup (parameters, NM_CONNECTION_NORMALIZE_PARAM_IP6_CONFIG_METHOD); @@ -771,6 +772,15 @@ _normalize_ip_config (NMConnection *self, GHashTable *parameters) g_object_set (s_ip4, NM_SETTING_IP_CONFIG_MAY_FAIL, TRUE, NULL); changed = TRUE; } + + num = nm_setting_ip_config_get_num_addresses (s_ip4); + if ( num > 1 + && nm_streq0 (nm_setting_ip_config_get_method (s_ip4), + NM_SETTING_IP4_CONFIG_METHOD_SHARED)) { + for (i = num - 1; i > 0; i--) + nm_setting_ip_config_remove_address (s_ip4, i); + changed = TRUE; + } } if (!s_ip6) { setting = nm_setting_ip6_config_new (); diff --git a/libnm-core/nm-setting-ip4-config.c b/libnm-core/nm-setting-ip4-config.c index 70fab0e2b1..91a4a3f50c 100644 --- a/libnm-core/nm-setting-ip4-config.c +++ b/libnm-core/nm-setting-ip4-config.c @@ -225,6 +225,20 @@ verify (NMSetting *setting, NMConnection *connection, GError **error) return FALSE; } + /* Failures from here on are NORMALIZABLE_ERROR... */ + + if ( nm_streq (method, NM_SETTING_IP4_CONFIG_METHOD_SHARED) + && nm_setting_ip_config_get_num_addresses (s_ip) > 1) { + g_set_error (error, + NM_CONNECTION_ERROR, + NM_CONNECTION_ERROR_INVALID_PROPERTY, + _("multiple addresses are not allowed for '%s=%s'"), + NM_SETTING_IP_CONFIG_METHOD, + NM_SETTING_IP4_CONFIG_METHOD_SHARED); + g_prefix_error (error, "%s.%s: ", NM_SETTING_IP4_CONFIG_SETTING_NAME, NM_SETTING_IP_CONFIG_ADDRESSES); + return NM_SETTING_VERIFY_NORMALIZABLE_ERROR; + } + /* Failures from here on are NORMALIZABLE... */ if ( !strcmp (method, NM_SETTING_IP4_CONFIG_METHOD_DISABLED) diff --git a/libnm-core/tests/test-general.c b/libnm-core/tests/test-general.c index 6aee25273d..554e6baeed 100644 --- a/libnm-core/tests/test-general.c +++ b/libnm-core/tests/test-general.c @@ -3960,6 +3960,59 @@ test_connection_normalize_may_fail (void) g_assert_cmpint (nm_setting_ip_config_get_may_fail (s_ip6), ==, TRUE); } +static void +test_connection_normalize_shared_addresses (void) +{ + gs_unref_object NMConnection *con = NULL; + NMSettingIPConfig *s_ip4, *s_ip6; + NMIPAddress *addr; + gs_free_error GError *error = NULL; + + con = nmtst_create_minimal_connection ("test1", NULL, NM_SETTING_WIRED_SETTING_NAME, NULL); + nmtst_assert_connection_verifies_and_normalizable (con); + + s_ip4 = (NMSettingIPConfig *) nm_setting_ip4_config_new (); + g_object_set (G_OBJECT (s_ip4), + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_SHARED, + NULL); + + addr = nm_ip_address_new (AF_INET, "1.1.1.1", 24, &error); + g_assert_no_error (error); + nm_setting_ip_config_add_address (s_ip4, addr); + nm_ip_address_unref (addr); + + s_ip6 = (NMSettingIPConfig *) nm_setting_ip6_config_new (); + g_object_set (s_ip6, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_AUTO, + NULL); + + nm_connection_add_setting (con, (NMSetting *) s_ip4); + nm_connection_add_setting (con, (NMSetting *) s_ip6); + + nmtst_assert_connection_verifies_without_normalization (con); + + /* Now we add other addresses and check that they are + * removed during normalization + * */ + addr = nm_ip_address_new (AF_INET, "2.2.2.2", 24, &error); + g_assert_no_error (error); + nm_setting_ip_config_add_address (s_ip4, addr); + nm_ip_address_unref (addr); + + addr = nm_ip_address_new (AF_INET, "3.3.3.3", 24, &error); + g_assert_no_error (error); + nm_setting_ip_config_add_address (s_ip4, addr); + nm_ip_address_unref (addr); + + nmtst_assert_connection_verifies_after_normalization (con, + NM_CONNECTION_ERROR, + NM_CONNECTION_ERROR_INVALID_PROPERTY); + nmtst_connection_normalize (con); + g_assert_cmpuint (nm_setting_ip_config_get_num_addresses (s_ip4), ==, 1); + addr = nm_setting_ip_config_get_address (s_ip4, 0); + g_assert_cmpstr (nm_ip_address_get_address (addr), ==, "1.1.1.1"); +} + static void test_setting_ip4_gateway (void) { @@ -5364,6 +5417,7 @@ int main (int argc, char **argv) g_test_add_func ("/core/general/test_connection_normalize_infiniband_mtu", test_connection_normalize_infiniband_mtu); g_test_add_func ("/core/general/test_connection_normalize_gateway_never_default", test_connection_normalize_gateway_never_default); g_test_add_func ("/core/general/test_connection_normalize_may_fail", test_connection_normalize_may_fail); + g_test_add_func ("/core/general/test_connection_normalize_shared_addresses", test_connection_normalize_shared_addresses); g_test_add_func ("/core/general/test_setting_connection_permissions_helpers", test_setting_connection_permissions_helpers); g_test_add_func ("/core/general/test_setting_connection_permissions_property", test_setting_connection_permissions_property);