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. diff --git a/rust/package/agama.changes b/rust/package/agama.changes index 6216895e1b..1d22b9a3a5 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) + ------------------------------------------------------------------- Thu Jun 20 12:58:32 UTC 2024 - Imobach Gonzalez Sosa