summaryrefslogtreecommitdiffhomepage
path: root/talpid-wireguard/src
diff options
context:
space:
mode:
authorDavid Lönnhager <david.l@mullvad.net>2024-08-19 10:54:20 +0200
committerDavid Lönnhager <david.l@mullvad.net>2024-08-20 12:02:58 +0200
commitba6d56a7c5bcbfb8bb35adc50abfc94daf898671 (patch)
treeee946e1784f1cae2cf0e17e14a7d15922a424387 /talpid-wireguard/src
parent435b8571210dd8bb7e5bad5f54f487037a35ba4c (diff)
downloadmullvadvpn-ba6d56a7c5bcbfb8bb35adc50abfc94daf898671.tar.xz
mullvadvpn-ba6d56a7c5bcbfb8bb35adc50abfc94daf898671.zip
Move MTU helpers to talpid-net crate
Diffstat (limited to 'talpid-wireguard/src')
-rw-r--r--talpid-wireguard/src/lib.rs2
-rw-r--r--talpid-wireguard/src/mtu_detection.rs2
-rw-r--r--talpid-wireguard/src/unix.rs77
3 files changed, 1 insertions, 80 deletions
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 {
diff --git a/talpid-wireguard/src/unix.rs b/talpid-wireguard/src/unix.rs
deleted file mode 100644
index 1e58a696df..0000000000
--- a/talpid-wireguard/src/unix.rs
+++ /dev/null
@@ -1,77 +0,0 @@
-use std::{io, os::fd::AsRawFd};
-
-use socket2::Domain;
-use talpid_types::ErrorExt;
-
-#[cfg(target_os = "macos")]
-const SIOCSIFMTU: u64 = 0x80206934;
-#[cfg(target_os = "macos")]
-const SIOCGIFMTU: u64 = 0xc0206933;
-#[cfg(target_os = "linux")]
-const SIOCSIFMTU: u64 = libc::SIOCSIFMTU;
-#[cfg(target_os = "linux")]
-const SIOCGIFMTU: u64 = libc::SIOCSIFMTU;
-
-pub fn set_mtu(interface_name: &str, mtu: u16) -> Result<(), io::Error> {
- let sock = socket2::Socket::new(
- Domain::IPV4,
- socket2::Type::STREAM,
- Some(socket2::Protocol::TCP),
- )?;
-
- let mut ifr: libc::ifreq = unsafe { std::mem::zeroed() };
- if interface_name.len() >= ifr.ifr_name.len() {
- return Err(io::Error::new(
- io::ErrorKind::InvalidInput,
- "Interface name too long",
- ));
- }
-
- unsafe {
- std::ptr::copy_nonoverlapping(
- interface_name.as_ptr() as *const libc::c_char,
- &mut ifr.ifr_name as *mut _,
- interface_name.len(),
- )
- };
- ifr.ifr_ifru.ifru_mtu = mtu as i32;
-
- 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"));
- return Err(e);
- }
- Ok(())
-}
-
-pub fn get_mtu(interface_name: &str) -> Result<u16, io::Error> {
- let sock = socket2::Socket::new(
- Domain::IPV4,
- socket2::Type::STREAM,
- Some(socket2::Protocol::TCP),
- )?;
-
- let mut ifr: libc::ifreq = unsafe { std::mem::zeroed() };
- if interface_name.len() >= ifr.ifr_name.len() {
- return Err(io::Error::new(
- io::ErrorKind::InvalidInput,
- "Interface name too long",
- ));
- }
-
- unsafe {
- std::ptr::copy_nonoverlapping(
- interface_name.as_ptr() as *const libc::c_char,
- &mut ifr.ifr_name as *mut _,
- interface_name.len(),
- )
- };
-
- 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"));
- return Err(e);
- }
- // SAFETY: ifru_mtu is initialized by SIOCGIFMTU
- Ok(u16::try_from(unsafe { ifr.ifr_ifru.ifru_mtu }).unwrap())
-}