diff options
| author | David Lönnhager <david.l@mullvad.net> | 2024-08-19 10:54:20 +0200 |
|---|---|---|
| committer | David Lönnhager <david.l@mullvad.net> | 2024-08-20 12:02:58 +0200 |
| commit | ba6d56a7c5bcbfb8bb35adc50abfc94daf898671 (patch) | |
| tree | ee946e1784f1cae2cf0e17e14a7d15922a424387 | |
| parent | 435b8571210dd8bb7e5bad5f54f487037a35ba4c (diff) | |
| download | mullvadvpn-ba6d56a7c5bcbfb8bb35adc50abfc94daf898671.tar.xz mullvadvpn-ba6d56a7c5bcbfb8bb35adc50abfc94daf898671.zip | |
Move MTU helpers to talpid-net crate
| -rw-r--r-- | Cargo.lock | 11 | ||||
| -rw-r--r-- | Cargo.toml | 1 | ||||
| -rw-r--r-- | talpid-net/Cargo.toml | 17 | ||||
| -rw-r--r-- | talpid-net/src/lib.rs | 2 | ||||
| -rw-r--r-- | talpid-net/src/unix.rs (renamed from talpid-wireguard/src/unix.rs) | 6 | ||||
| -rw-r--r-- | talpid-wireguard/Cargo.toml | 3 | ||||
| -rw-r--r-- | talpid-wireguard/src/lib.rs | 2 | ||||
| -rw-r--r-- | talpid-wireguard/src/mtu_detection.rs | 2 |
8 files changed, 41 insertions, 3 deletions
diff --git a/Cargo.lock b/Cargo.lock index 6c25a0ade8..f9fa547879 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3946,6 +3946,16 @@ dependencies = [ ] [[package]] +name = "talpid-net" +version = "0.0.0" +dependencies = [ + "libc", + "log", + "socket2", + "talpid-types", +] + +[[package]] name = "talpid-openvpn" version = "0.0.0" dependencies = [ @@ -4128,6 +4138,7 @@ dependencies = [ "socket2", "surge-ping", "talpid-dbus", + "talpid-net", "talpid-routing", "talpid-tunnel", "talpid-tunnel-config-client", diff --git a/Cargo.toml b/Cargo.toml index c3b1185cbb..1317951f65 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,6 +28,7 @@ members = [ "talpid-core", "talpid-dbus", "talpid-future", + "talpid-net", "talpid-openvpn", "talpid-openvpn-plugin", "talpid-platform-metadata", diff --git a/talpid-net/Cargo.toml b/talpid-net/Cargo.toml new file mode 100644 index 0000000000..aa30ed1b5b --- /dev/null +++ b/talpid-net/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "talpid-net" +description = "Networking helpers" +authors.workspace = true +repository.workspace = true +license.workspace = true +edition.workspace = true +rust-version.workspace = true + +[lints] +workspace = true + +[target.'cfg(unix)'.dependencies] +libc = "0.2" +talpid-types = { path = "../talpid-types" } +socket2 = { version = "0.5.3", features = ["all"] } +log = { workspace = true } diff --git a/talpid-net/src/lib.rs b/talpid-net/src/lib.rs new file mode 100644 index 0000000000..b13064bf70 --- /dev/null +++ b/talpid-net/src/lib.rs @@ -0,0 +1,2 @@ +#[cfg(unix)] +pub mod unix; diff --git a/talpid-wireguard/src/unix.rs b/talpid-net/src/unix.rs index 1e58a696df..ef2bfdeb27 100644 --- a/talpid-wireguard/src/unix.rs +++ b/talpid-net/src/unix.rs @@ -1,3 +1,5 @@ +#![cfg(any(target_os = "linux", target_os = "macos"))] + use std::{io, os::fd::AsRawFd}; use socket2::Domain; @@ -27,6 +29,7 @@ pub fn set_mtu(interface_name: &str, mtu: u16) -> Result<(), io::Error> { )); } + // SAFETY: `interface_name.len()` is less than `ifr.ifr_name.len()` unsafe { std::ptr::copy_nonoverlapping( interface_name.as_ptr() as *const libc::c_char, @@ -36,6 +39,7 @@ pub fn set_mtu(interface_name: &str, mtu: u16) -> Result<(), io::Error> { }; ifr.ifr_ifru.ifru_mtu = mtu as i32; + // SAFETY: SIOCSIFMTU expects an ifreq with an MTU and interface set if unsafe { libc::ioctl(sock.as_raw_fd(), SIOCSIFMTU, &ifr) } < 0 { let e = std::io::Error::last_os_error(); log::error!("{}", e.display_chain_with_msg("SIOCSIFMTU failed")); @@ -59,6 +63,7 @@ pub fn get_mtu(interface_name: &str) -> Result<u16, io::Error> { )); } + // SAFETY: `interface_name.len()` is less than `ifr.ifr_name.len()` unsafe { std::ptr::copy_nonoverlapping( interface_name.as_ptr() as *const libc::c_char, @@ -67,6 +72,7 @@ pub fn get_mtu(interface_name: &str) -> Result<u16, io::Error> { ) }; + // SAFETY: SIOCGIFMTU expects an ifreq with an interface set if unsafe { libc::ioctl(sock.as_raw_fd(), SIOCGIFMTU, &ifr) } < 0 { let e = std::io::Error::last_os_error(); log::error!("{}", e.display_chain_with_msg("SIOCGIFMTU failed")); diff --git a/talpid-wireguard/Cargo.toml b/talpid-wireguard/Cargo.toml index 806cdd1447..3fea8a17c0 100644 --- a/talpid-wireguard/Cargo.toml +++ b/talpid-wireguard/Cargo.toml @@ -45,6 +45,9 @@ tokio-stream = { version = "0.1", features = ["io-util"] } [target.'cfg(unix)'.dependencies] nix = "0.23" +[target.'cfg(any(target_os = "linux", target_os = "macos"))'.dependencies] +talpid-net = { path = "../talpid-net" } + [target.'cfg(target_os = "linux")'.dependencies] rtnetlink = "0.11" netlink-packet-core = "0.4.2" diff --git a/talpid-wireguard/src/lib.rs b/talpid-wireguard/src/lib.rs index b9a85560ee..ca59e014ff 100644 --- a/talpid-wireguard/src/lib.rs +++ b/talpid-wireguard/src/lib.rs @@ -49,8 +49,6 @@ mod connectivity_check; mod logging; mod ping_monitor; mod stats; -#[cfg(any(target_os = "linux", target_os = "macos"))] -mod unix; #[cfg(wireguard_go)] mod wireguard_go; #[cfg(target_os = "linux")] diff --git a/talpid-wireguard/src/mtu_detection.rs b/talpid-wireguard/src/mtu_detection.rs index 5132705719..11c6625f2c 100644 --- a/talpid-wireguard/src/mtu_detection.rs +++ b/talpid-wireguard/src/mtu_detection.rs @@ -60,7 +60,7 @@ pub async fn automatic_mtu_correction( log::warn!("Lowering MTU from {} to {verified_mtu}", current_tunnel_mtu); #[cfg(any(target_os = "linux", target_os = "macos"))] - crate::unix::set_mtu(&iface_name, verified_mtu).map_err(Error::SetMtu)?; + talpid_net::unix::set_mtu(&iface_name, verified_mtu).map_err(Error::SetMtu)?; #[cfg(windows)] set_mtu_windows(verified_mtu, iface_name, ipv6).map_err(Error::SetMtu)?; } else { |
