From e6b3a6a2b6c18a62d025fe55a5fd57759c9e1a69 Mon Sep 17 00:00:00 2001 From: Beniamino Galvani Date: Tue, 28 Mar 2023 23:10:25 +0200 Subject: [PATCH 1/3] core: move deactivation of active connections to the manager It's needed for the next commit. --- src/core/nm-manager.c | 27 +++++++++++++++++++++++++++ src/core/nm-manager.h | 2 ++ src/core/nm-policy.c | 31 +------------------------------ 3 files changed, 30 insertions(+), 30 deletions(-) diff --git a/src/core/nm-manager.c b/src/core/nm-manager.c index 52f6304785..8285723739 100644 --- a/src/core/nm-manager.c +++ b/src/core/nm-manager.c @@ -5978,6 +5978,33 @@ fail: error_desc ?: error->message); } +void +nm_manager_deactivate_ac(NMManager *self, NMSettingsConnection *connection) +{ + NMActiveConnection *ac; + const CList *tmp_list, *tmp_safe; + GError *error = NULL; + + nm_assert(NM_IS_SETTINGS_CONNECTION(connection)); + + nm_manager_for_each_active_connection_safe (self, ac, tmp_list, tmp_safe) { + if (nm_active_connection_get_settings_connection(ac) == connection + && (nm_active_connection_get_state(ac) <= NM_ACTIVE_CONNECTION_STATE_ACTIVATED)) { + if (!nm_manager_deactivate_connection(self, + ac, + NM_DEVICE_STATE_REASON_CONNECTION_REMOVED, + &error)) { + _LOGW(LOGD_DEVICE, + "connection '%s' disappeared, but error deactivating it: (%d) %s", + nm_settings_connection_get_id(connection), + error ? error->code : -1, + error ? error->message : "(unknown)"); + g_clear_error(&error); + } + } + } +} + /** * nm_manager_activate_connection(): * @self: the #NMManager diff --git a/src/core/nm-manager.h b/src/core/nm-manager.h index ad8d0ba81a..fe78eb2687 100644 --- a/src/core/nm-manager.h +++ b/src/core/nm-manager.h @@ -121,6 +121,8 @@ NMSettingsConnection **nm_manager_get_activatable_connections(NMManager *manager gboolean sort, guint *out_len); +void nm_manager_deactivate_ac(NMManager *self, NMSettingsConnection *connection); + void nm_manager_write_device_state_all(NMManager *manager); gboolean nm_manager_write_device_state(NMManager *manager, NMDevice *device, int *out_ifindex); diff --git a/src/core/nm-policy.c b/src/core/nm-policy.c index 8d5054c824..ef7d2e434a 100644 --- a/src/core/nm-policy.c +++ b/src/core/nm-policy.c @@ -2624,41 +2624,12 @@ connection_updated(NMSettings *settings, schedule_activate_all(self); } -static void -_deactivate_if_active(NMPolicy *self, NMSettingsConnection *connection) -{ - NMPolicyPrivate *priv = NM_POLICY_GET_PRIVATE(self); - NMActiveConnection *ac; - const CList *tmp_list, *tmp_safe; - GError *error = NULL; - - nm_assert(NM_IS_SETTINGS_CONNECTION(connection)); - - nm_manager_for_each_active_connection_safe (priv->manager, ac, tmp_list, tmp_safe) { - if (nm_active_connection_get_settings_connection(ac) == connection - && (nm_active_connection_get_state(ac) <= NM_ACTIVE_CONNECTION_STATE_ACTIVATED)) { - if (!nm_manager_deactivate_connection(priv->manager, - ac, - NM_DEVICE_STATE_REASON_CONNECTION_REMOVED, - &error)) { - _LOGW(LOGD_DEVICE, - "connection '%s' disappeared, but error deactivating it: (%d) %s", - nm_settings_connection_get_id(connection), - error ? error->code : -1, - error ? error->message : "(unknown)"); - g_clear_error(&error); - } - } - } -} - static void connection_removed(NMSettings *settings, NMSettingsConnection *connection, gpointer user_data) { NMPolicyPrivate *priv = user_data; - NMPolicy *self = _PRIV_TO_SELF(priv); - _deactivate_if_active(self, connection); + nm_manager_deactivate_ac(priv->manager, connection); } static void From 6d6bd9251002d6fc767e1cb3beab1aeddcef08a9 Mon Sep 17 00:00:00 2001 From: Beniamino Galvani Date: Tue, 28 Mar 2023 23:12:32 +0200 Subject: [PATCH 2/3] core: also deactivate ACs that are not authorized yet If we are deactivating active-connections for a specific settings-connection, also consider active-connections that are waiting for authorization. Otherwise, when the connection is deleted, a active-connection might still reference it. --- src/core/nm-manager.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/core/nm-manager.c b/src/core/nm-manager.c index 8285723739..d44e25c02c 100644 --- a/src/core/nm-manager.c +++ b/src/core/nm-manager.c @@ -5981,9 +5981,12 @@ fail: void nm_manager_deactivate_ac(NMManager *self, NMSettingsConnection *connection) { + NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE(self); NMActiveConnection *ac; const CList *tmp_list, *tmp_safe; GError *error = NULL; + AsyncOpData *async_op_data; + AsyncOpData *async_op_data_safe; nm_assert(NM_IS_SETTINGS_CONNECTION(connection)); @@ -6003,6 +6006,25 @@ nm_manager_deactivate_ac(NMManager *self, NMSettingsConnection *connection) } } } + + c_list_for_each_entry_safe (async_op_data, + async_op_data_safe, + &priv->async_op_lst_head, + async_op_lst) { + if (!NM_IN_SET(async_op_data->async_op_type, + ASYNC_OP_TYPE_AC_AUTH_ACTIVATE_INTERNAL, + ASYNC_OP_TYPE_AC_AUTH_ACTIVATE_USER, + ASYNC_OP_TYPE_AC_AUTH_ADD_AND_ACTIVATE, + ASYNC_OP_TYPE_AC_AUTH_ADD_AND_ACTIVATE2)) + continue; + + ac = async_op_data->ac_auth.active; + if (nm_active_connection_get_settings_connection(ac) == connection) { + nm_active_connection_set_state(ac, + NM_ACTIVE_CONNECTION_STATE_DEACTIVATED, + NM_ACTIVE_CONNECTION_STATE_REASON_CONNECTION_REMOVED); + } + } } /** From b3e550497275598c7a34484a9d0a00c712916066 Mon Sep 17 00:00:00 2001 From: Beniamino Galvani Date: Tue, 28 Mar 2023 23:13:08 +0200 Subject: [PATCH 3/3] core: don't block a connection that was removed Don't try to block a device/connection pair when the connection was removed. Doing so would create a new devcon entry associated with the connection that is being deleted. Fixes: b73b34c3ee30 ('policy: track autoconnect retries per Device x Connection') --- src/core/nm-policy.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/core/nm-policy.c b/src/core/nm-policy.c index ef7d2e434a..b2ccb16478 100644 --- a/src/core/nm-policy.c +++ b/src/core/nm-policy.c @@ -1326,7 +1326,8 @@ pending_ac_state_changed(NMActiveConnection *ac, guint state, guint reason, NMPo * device, but block the current connection to avoid an activation * loop. */ - if (reason != NM_ACTIVE_CONNECTION_STATE_REASON_DEVICE_DISCONNECTED) { + if (reason != NM_ACTIVE_CONNECTION_STATE_REASON_DEVICE_DISCONNECTED + && reason != NM_ACTIVE_CONNECTION_STATE_REASON_CONNECTION_REMOVED) { con = nm_active_connection_get_settings_connection(ac); nm_manager_devcon_autoconnect_blocked_reason_set( priv->manager,