From 80ae0f9d0f9e51ea29863e414217182bec2e3e72 Mon Sep 17 00:00:00 2001 From: rhaskia Date: Sun, 3 Mar 2024 10:32:32 +1300 Subject: [PATCH] Add setup command to TunInterface --- Cargo.lock | 90 +++++++++++++++++++++++++++++++++++ burrow/src/daemon/instance.rs | 3 +- tun/Cargo.toml | 1 + tun/src/tokio/mod.rs | 9 +++- tun/src/unix/linux/mod.rs | 10 ++++ 5 files changed, 111 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a75bd28..78fc44f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1416,6 +1416,71 @@ dependencies = [ "tempfile", ] +[[package]] +name = "netlink-packet-core" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72724faf704479d67b388da142b186f916188505e7e0b26719019c525882eda4" +dependencies = [ + "anyhow", + "byteorder", + "netlink-packet-utils", +] + +[[package]] +name = "netlink-packet-route" +version = "0.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "74c171cd77b4ee8c7708da746ce392440cb7bcf618d122ec9ecc607b12938bf4" +dependencies = [ + "anyhow", + "byteorder", + "libc", + "log", + "netlink-packet-core", + "netlink-packet-utils", +] + +[[package]] +name = "netlink-packet-utils" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ede8a08c71ad5a95cdd0e4e52facd37190977039a4704eb82a283f713747d34" +dependencies = [ + "anyhow", + "byteorder", + "paste", + "thiserror", +] + +[[package]] +name = "netlink-proto" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "86b33524dc0968bfad349684447bfce6db937a9ac3332a1fe60c0c5a5ce63f21" +dependencies = [ + "bytes", + "futures", + "log", + "netlink-packet-core", + "netlink-sys", + "thiserror", + "tokio", +] + +[[package]] +name = "netlink-sys" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6471bf08e7ac0135876a9581bf3217ef0333c191c128d34878079f42ee150411" +dependencies = [ + "bytes", + "futures", + "libc", + "log", + "tokio", +] + [[package]] name = "nix" version = "0.26.4" @@ -1591,6 +1656,12 @@ dependencies = [ "subtle", ] +[[package]] +name = "paste" +version = "1.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "de3145af08024dea9fa9914f381a17b8fc6034dfb00f3a84013f7ff43f29ed4c" + [[package]] name = "pbkdf2" version = "0.11.0" @@ -1877,6 +1948,24 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "rtnetlink" +version = "0.14.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b684475344d8df1859ddb2d395dd3dac4f8f3422a1aa0725993cb375fc5caba5" +dependencies = [ + "futures", + "log", + "netlink-packet-core", + "netlink-packet-route", + "netlink-packet-utils", + "netlink-proto", + "netlink-sys", + "nix 0.27.1", + "thiserror", + "tokio", +] + [[package]] name = "rustc-demangle" version = "0.1.23" @@ -2535,6 +2624,7 @@ dependencies = [ "log", "nix 0.26.4", "reqwest", + "rtnetlink", "schemars", "serde", "socket2", diff --git a/burrow/src/daemon/instance.rs b/burrow/src/daemon/instance.rs index 0d3e726..df8eb21 100644 --- a/burrow/src/daemon/instance.rs +++ b/burrow/src/daemon/instance.rs @@ -51,6 +51,8 @@ impl DaemonInstance { } RunState::Idle => { let tun_if = st.tun.open()?; + tun_if.set_up(true).await?; + debug!("Setting tun on wg_interface"); self.wg_interface.read().await.set_tun(tun_if).await; debug!("tun set on wg_interface"); @@ -59,7 +61,6 @@ impl DaemonInstance { self.tun_interface = self.wg_interface.read().await.get_tun(); debug!("tun_interface set: {:?}", self.tun_interface); - debug!("Cloning wg_interface"); let tmp_wg = self.wg_interface.clone(); debug!("wg_interface cloned"); diff --git a/tun/Cargo.toml b/tun/Cargo.toml index 7413f65..d4cd67e 100644 --- a/tun/Cargo.toml +++ b/tun/Cargo.toml @@ -14,6 +14,7 @@ tracing = "0.1" log = "0.4" serde = { version = "1", features = ["derive"], optional = true } schemars = { version = "0.8", optional = true } +rtnetlink = "0.14" futures = { version = "0.3.28", optional = true } diff --git a/tun/src/tokio/mod.rs b/tun/src/tokio/mod.rs index bd27109..6ede3e4 100644 --- a/tun/src/tokio/mod.rs +++ b/tun/src/tokio/mod.rs @@ -1,6 +1,6 @@ use std::io; -use tokio::io::unix::AsyncFd; +use tokio::io::unix::{AsyncFd, TryIoError}; use tracing::instrument; #[derive(Debug)] @@ -15,6 +15,13 @@ impl TunInterface { Ok(Self { inner: AsyncFd::new(tun)? }) } + #[instrument] + pub async fn set_up(&self, up: bool) -> io::Result<()> { + let mut guard = self.inner.readable().await?; + guard.try_io(|inner| inner.get_ref().set_up(up)); + Ok(()) + } + #[instrument] pub async fn send(&self, buf: &[u8]) -> io::Result { loop { diff --git a/tun/src/unix/linux/mod.rs b/tun/src/unix/linux/mod.rs index 60d6341..2913357 100644 --- a/tun/src/unix/linux/mod.rs +++ b/tun/src/unix/linux/mod.rs @@ -11,6 +11,7 @@ use std::{ use fehler::throws; use libc::in6_ifreq; +use rtnetlink::new_connection; use socket2::{Domain, SockAddr, Socket, Type}; use tracing::{info, instrument}; @@ -95,6 +96,15 @@ impl TunInterface { unsafe { iff.ifr_ifru.ifru_ifindex } } + #[throws] + #[instrument] + pub fn set_up(&self, up: bool) { + let connection = new_connection()?; + let handle = connection.1; + let link = handle.link().set(self.index()? as u32); + if up { link.up() } else { link.down() } + } + #[throws] #[instrument] pub fn set_ipv4_addr(&self, addr: Ipv4Addr) {