diff --git a/rust/agama-dbus-server/src/network/model.rs b/rust/agama-dbus-server/src/network/model.rs index 7091e746d3..e1d53d2f5b 100644 --- a/rust/agama-dbus-server/src/network/model.rs +++ b/rust/agama-dbus-server/src/network/model.rs @@ -445,6 +445,18 @@ impl Connection { self.base().status == Status::Removed } + pub fn is_up(&self) -> bool { + self.base().status == Status::Up + } + + pub fn set_up(&mut self) { + self.base_mut().status = Status::Up + } + + pub fn set_down(&mut self) { + self.base_mut().status = Status::Down + } + /// Determines whether it is a loopback interface. pub fn is_loopback(&self) -> bool { matches!(self, Connection::Loopback(_)) @@ -540,7 +552,8 @@ impl From for zbus::fdo::Error { #[derive(Debug, Default, Clone, Copy, PartialEq)] pub enum Status { #[default] - Present, + Up, + Down, Removed, } diff --git a/rust/agama-dbus-server/src/network/nm/client.rs b/rust/agama-dbus-server/src/network/nm/client.rs index a37486eafa..26cd0511d6 100644 --- a/rust/agama-dbus-server/src/network/nm/client.rs +++ b/rust/agama-dbus-server/src/network/nm/client.rs @@ -137,7 +137,11 @@ impl<'a> NetworkManagerClient<'a> { proxy.add_connection(new_conn).await? }; - self.activate_connection(path).await?; + if conn.is_up() { + self.activate_connection(path).await?; + } else { + self.deactivate_connection(path).await?; + } Ok(()) } @@ -160,6 +164,24 @@ impl<'a> NetworkManagerClient<'a> { Ok(()) } + /// Deactivates a NetworkManager connection. + /// + /// * `path`: D-Bus patch of the connection. + async fn deactivate_connection(&self, path: OwnedObjectPath) -> Result<(), ServiceError> { + let proxy = NetworkManagerProxy::new(&self.connection).await?; + match proxy.deactivate_connection(&path.as_ref()).await { + Err(e) => { + // Ignore ConnectionNotActive error since this just means the state is already correct + if e.to_string().contains("ConnectionNotActive") { + Ok(()) + } else { + Err(ServiceError::DBus(e)) + } + } + _ => Ok(()), + } + } + async fn get_connection_proxy(&self, uuid: Uuid) -> Result { let proxy = SettingsProxy::new(&self.connection).await?; let uuid_s = uuid.to_string();