From 8b1ed9c5bcad6345d3028bcd5b7477cedf00e6c7 Mon Sep 17 00:00:00 2001 From: Clemens Famulla-Conrad Date: Mon, 17 Jun 2024 16:57:20 +0200 Subject: [PATCH 1/2] agama-server: add tun/tap connection config --- rust/agama-server/src/network/model.rs | 17 ++++++++ rust/agama-server/src/network/nm/dbus.rs | 50 ++++++++++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/rust/agama-server/src/network/model.rs b/rust/agama-server/src/network/model.rs index 51ad27fcf2..c6d6f3ba56 100644 --- a/rust/agama-server/src/network/model.rs +++ b/rust/agama-server/src/network/model.rs @@ -663,6 +663,7 @@ pub enum ConnectionConfig { Vlan(VlanConfig), Bridge(BridgeConfig), Infiniband(InfinibandConfig), + Tun(TunConfig), } #[derive(Default, Debug, PartialEq, Clone, Serialize)] @@ -1313,6 +1314,22 @@ impl fmt::Display for InfinibandTransportMode { } } + +#[derive(Default, Debug, PartialEq, Clone, Serialize)] +pub enum TunMode { + #[default] + Tun = 1, + Tap = 2, +} + +#[derive(Default, Debug, PartialEq, Clone, Serialize)] +pub struct TunConfig { + pub mode: TunMode, + pub group: Option, + pub owner: Option, +} + + /// Represents a network change. #[derive(Debug, Clone, Serialize)] #[serde(rename_all = "camelCase")] diff --git a/rust/agama-server/src/network/nm/dbus.rs b/rust/agama-server/src/network/nm/dbus.rs index 0b30e8e943..d5eba9a5b2 100644 --- a/rust/agama-server/src/network/nm/dbus.rs +++ b/rust/agama-server/src/network/nm/dbus.rs @@ -24,6 +24,7 @@ const VLAN_KEY: &str = "vlan"; const BRIDGE_KEY: &str = "bridge"; const BRIDGE_PORT_KEY: &str = "bridge-port"; const INFINIBAND_KEY: &str = "infiniband"; +const TUN_KEY: &str = "tun"; /// Converts a connection struct into a HashMap that can be sent over D-Bus. /// @@ -122,6 +123,10 @@ pub fn connection_to_dbus<'a>( ConnectionConfig::Loopback => { connection_dbus.insert("type", LOOPBACK_KEY.into()); } + ConnectionConfig::Tun(tun) => { + connection_dbus.insert("type", TUN_KEY.into()); + result.insert(TUN_KEY, tun_config_to_dbus(tun)); + } _ => {} } @@ -171,6 +176,11 @@ pub fn connection_from_dbus(conn: OwnedNestedHash) -> Option { return Some(connection); } + if let Some(tun_config) = tun_config_from_dbus(&conn) { + connection.config = ConnectionConfig::Tun(tun_config); + return Some(connection); + } + if conn.contains_key(DUMMY_KEY) { connection.config = ConnectionConfig::Dummy; return Some(connection); @@ -532,6 +542,46 @@ fn infiniband_config_from_dbus(conn: &OwnedNestedHash) -> Option HashMap<&str, zvariant::Value> { + let mut tun_config: HashMap<&str, zvariant::Value> = HashMap::from([ + ("mode", Value::new(config.mode.clone() as u32)), + ]); + + if let Some(group) = &config.group { + tun_config.insert("group", group.into()); + } + + if let Some(owner) = &config.owner { + tun_config.insert("owner", owner.into()); + } + + tun_config +} + +fn tun_config_from_dbus(conn: &OwnedNestedHash) -> Option { + let tun = conn.get(TUN_KEY)?; + + let mut tun_config = TunConfig::default(); + + + if let Some(mode) = tun.get("mode") { + tun_config.mode = match mode.downcast_ref::()? { + 2 => TunMode::Tap, + _ => TunMode::Tun, + } + } + + if let Some(group) = tun.get("group") { + tun_config.group = Some(group.downcast_ref::()?.to_string()); + } + + if let Some(owner) = tun.get("owner") { + tun_config.owner = Some(owner.downcast_ref::()?.to_string()); + } + + Some(tun_config) +} + /// Converts a MatchConfig struct into a HashMap that can be sent over D-Bus. /// /// * `match_config`: MatchConfig to convert. From f433cb34c6411000be11177b7a1fe25fb0a285b3 Mon Sep 17 00:00:00 2001 From: Clemens Famulla-Conrad Date: Fri, 21 Jun 2024 15:23:38 +0200 Subject: [PATCH 2/2] Update changelog - add tun/tap --- rust/package/agama.changes | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/rust/package/agama.changes b/rust/package/agama.changes index 2c0bffdd96..0c53fd5a17 100644 --- a/rust/package/agama.changes +++ b/rust/package/agama.changes @@ -1,3 +1,8 @@ +------------------------------------------------------------------- +Thu Jun 21 15:00:00 UTC 2024 - Clemens Famulla-Conrad + +- Add tun/tap model (gh#openSUSE/agama#1353) + ------------------------------------------------------------------- Fri Jun 14 06:17:52 UTC 2024 - Imobach Gonzalez Sosa