diff options
author | Beniamino Galvani <bgalvani@redhat.com> | 2016-04-22 13:45:08 +0200 |
---|---|---|
committer | Beniamino Galvani <bgalvani@redhat.com> | 2016-04-22 15:25:53 +0200 |
commit | 3ae8b4f269c59e30a3ee7e14aec7304294b05342 (patch) | |
tree | 3ec3d8b131301bdfe0060cd211fcd3da3c376ec2 | |
parent | cf0c300058722a01898770f1e061ab2dfebf3b71 (diff) | |
download | NetworkManager-3ae8b4f269c59e30a3ee7e14aec7304294b05342.tar.gz |
libnm-core: remove gateway when never-default=yes in NMSettingIPConfig
Having a gateway defined when never-default=yes causes troubles in
connection matching and anyway makes no sense.
If the combination is found, remove the gateway during the
normalization phase.
https://bugzilla.redhat.com/show_bug.cgi?id=1313091
-rw-r--r-- | libnm-core/nm-connection.c | 15 | ||||
-rw-r--r-- | libnm-core/nm-setting-ip-config.c | 10 | ||||
-rw-r--r-- | libnm-core/tests/test-general.c | 52 |
3 files changed, 76 insertions, 1 deletions
diff --git a/libnm-core/nm-connection.c b/libnm-core/nm-connection.c index c03e62656f..d95e11583a 100644 --- a/libnm-core/nm-connection.c +++ b/libnm-core/nm-connection.c @@ -724,6 +724,7 @@ _normalize_ip_config (NMConnection *self, GHashTable *parameters) const char *default_ip6_method = NULL; NMSettingIPConfig *s_ip4, *s_ip6; NMSetting *setting; + gboolean gateway_removed = FALSE; if (parameters) default_ip6_method = g_hash_table_lookup (parameters, NM_CONNECTION_NORMALIZE_PARAM_IP6_CONFIG_METHOD); @@ -756,6 +757,12 @@ _normalize_ip_config (NMConnection *self, GHashTable *parameters) NM_SETTING_IP_CONFIG_METHOD, default_ip4_method, NULL); nm_connection_add_setting (self, setting); + } else { + if ( nm_setting_ip_config_get_gateway (s_ip4) + && nm_setting_ip_config_get_never_default (s_ip4)) { + g_object_set (s_ip4, NM_SETTING_IP_CONFIG_GATEWAY, NULL, NULL); + gateway_removed = TRUE; + } } if (!s_ip6) { setting = nm_setting_ip6_config_new (); @@ -765,8 +772,14 @@ _normalize_ip_config (NMConnection *self, GHashTable *parameters) NM_SETTING_IP_CONFIG_MAY_FAIL, TRUE, NULL); nm_connection_add_setting (self, setting); + } else { + if ( nm_setting_ip_config_get_gateway (s_ip6) + && nm_setting_ip_config_get_never_default (s_ip6)) { + g_object_set (s_ip6, NM_SETTING_IP_CONFIG_GATEWAY, NULL, NULL); + gateway_removed = TRUE; + } } - return !s_ip4 || !s_ip6; + return !s_ip4 || !s_ip6 || gateway_removed; } } diff --git a/libnm-core/nm-setting-ip-config.c b/libnm-core/nm-setting-ip-config.c index 3e7f93b065..1afaabca3b 100644 --- a/libnm-core/nm-setting-ip-config.c +++ b/libnm-core/nm-setting-ip-config.c @@ -2274,6 +2274,16 @@ verify (NMSetting *setting, NMConnection *connection, GError **error) } } + if (priv->gateway && priv->never_default) { + g_set_error (error, + NM_CONNECTION_ERROR, + NM_CONNECTION_ERROR_INVALID_PROPERTY, + _("a gateway is incompatible with '%s'"), + NM_SETTING_IP_CONFIG_NEVER_DEFAULT); + g_prefix_error (error, "%s.%s: ", nm_setting_get_name (setting), NM_SETTING_IP_CONFIG_GATEWAY); + return NM_SETTING_VERIFY_NORMALIZABLE_ERROR; + } + return TRUE; } diff --git a/libnm-core/tests/test-general.c b/libnm-core/tests/test-general.c index a9f851d1ab..0c7fd870f0 100644 --- a/libnm-core/tests/test-general.c +++ b/libnm-core/tests/test-general.c @@ -3717,6 +3717,57 @@ test_connection_normalize_infiniband_mtu (void) } static void +test_connection_normalize_gateway_never_default (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_MANUAL, + 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); + + g_object_set (s_ip4, + NM_SETTING_IP_CONFIG_GATEWAY, "1.1.1.254", + NM_SETTING_IP_CONFIG_NEVER_DEFAULT, FALSE, + NULL); + + 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); + g_assert_cmpstr ("1.1.1.254", ==, nm_setting_ip_config_get_gateway (s_ip4)); + + /* Now set never-default to TRUE and check that the gateway is + * removed during normalization + * */ + g_object_set (s_ip4, + NM_SETTING_IP_CONFIG_NEVER_DEFAULT, TRUE, + NULL); + + nmtst_assert_connection_verifies_after_normalization (con, + NM_CONNECTION_ERROR, + NM_CONNECTION_ERROR_INVALID_PROPERTY); + nmtst_connection_normalize (con); + g_assert_cmpstr (NULL, ==, nm_setting_ip_config_get_gateway (s_ip4)); +} + +static void test_setting_ip4_gateway (void) { NMConnection *conn; @@ -5114,6 +5165,7 @@ int main (int argc, char **argv) g_test_add_func ("/core/general/test_connection_normalize_slave_type_1", test_connection_normalize_slave_type_1); g_test_add_func ("/core/general/test_connection_normalize_slave_type_2", test_connection_normalize_slave_type_2); 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_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); |