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
This commit is contained in:
Beniamino Galvani 2016-09-12 18:51:00 +02:00
parent b33aacbc91
commit eaad7ae431
3 changed files with 78 additions and 0 deletions

View file

@ -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 ();

View file

@ -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)

View file

@ -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);