diff --git a/rust/connlib/tunnel/src/client.rs b/rust/connlib/tunnel/src/client.rs index 6a9a5e161..81fdf9c8b 100644 --- a/rust/connlib/tunnel/src/client.rs +++ b/rust/connlib/tunnel/src/client.rs @@ -212,7 +212,7 @@ where let callbacks = self.callbacks.clone(); - self.io.device_mut().initialize( + self.io.device_mut().set_config( &config, // We can just sort in here because sentinel ips are created in order dns_mapping.left_values().copied().sorted().collect(), diff --git a/rust/connlib/tunnel/src/device_channel.rs b/rust/connlib/tunnel/src/device_channel.rs index 80c791172..6396f6b9f 100644 --- a/rust/connlib/tunnel/src/device_channel.rs +++ b/rust/connlib/tunnel/src/device_channel.rs @@ -63,8 +63,8 @@ impl Device { } } - #[cfg(target_family = "unix")] - pub(crate) fn initialize( + #[cfg(any(target_os = "android", target_os = "linux"))] + pub(crate) fn set_config( &mut self, config: &Interface, dns_config: Vec, @@ -83,8 +83,34 @@ impl Device { Ok(()) } + #[cfg(any(target_os = "ios", target_os = "macos"))] + pub(crate) fn set_config( + &mut self, + config: &Interface, + dns_config: Vec, + callbacks: &impl Callbacks, + ) -> Result<(), ConnlibError> { + // For macos the filedescriptor is the same throughout its lifetime. + // If we reinitialzie tun, we might drop the old tun after the new one is created + // this unregisters the file descriptor with the reactor so we never wake up + // in case an event is triggered. + if self.tun.is_none() { + self.tun = Some(Tun::new()?); + } + + self.mtu = ioctl::interface_mtu_by_name(self.tun.as_ref().unwrap().name())?; + + callbacks.on_set_interface_config(config.ipv4, config.ipv6, dns_config); + + if let Some(waker) = self.waker.take() { + waker.wake(); + } + + Ok(()) + } + #[cfg(target_family = "windows")] - pub(crate) fn initialize( + pub(crate) fn set_config( &mut self, config: &Interface, dns_config: Vec, diff --git a/rust/connlib/tunnel/src/device_channel/tun_darwin.rs b/rust/connlib/tunnel/src/device_channel/tun_darwin.rs index 4f778dfee..46644fe35 100644 --- a/rust/connlib/tunnel/src/device_channel/tun_darwin.rs +++ b/rust/connlib/tunnel/src/device_channel/tun_darwin.rs @@ -1,12 +1,11 @@ use crate::device_channel::{ipv4, ipv6}; -use connlib_shared::{messages::Interface as InterfaceConfig, Callbacks, Error, Result}; +use connlib_shared::{Callbacks, Error, Result}; use ip_network::IpNetwork; use libc::{ ctl_info, fcntl, getpeername, getsockopt, ioctl, iovec, msghdr, recvmsg, sendmsg, sockaddr_ctl, socklen_t, AF_INET, AF_INET6, AF_SYSTEM, CTLIOCGINFO, F_GETFL, F_SETFL, IF_NAMESIZE, O_NONBLOCK, SYSPROTO_CONTROL, UTUN_OPT_IFNAME, }; -use std::net::IpAddr; use std::task::{Context, Poll}; use std::{ collections::HashSet, @@ -70,11 +69,7 @@ impl Tun { } } - pub fn new( - config: &InterfaceConfig, - dns_config: Vec, - callbacks: &impl Callbacks, - ) -> Result { + pub fn new() -> Result { let mut info = ctl_info { ctl_id: 0, ctl_name: [0; 96], @@ -131,8 +126,6 @@ impl Tun { } if addr.sc_id == info.ctl_id { - callbacks.on_set_interface_config(config.ipv4, config.ipv6, dns_config); - set_non_blocking(fd)?; return Ok(Self { diff --git a/rust/connlib/tunnel/src/gateway.rs b/rust/connlib/tunnel/src/gateway.rs index b9b01df88..798025d57 100644 --- a/rust/connlib/tunnel/src/gateway.rs +++ b/rust/connlib/tunnel/src/gateway.rs @@ -50,7 +50,7 @@ where let callbacks = self.callbacks.clone(); self.io .device_mut() - .initialize(config, vec![], &callbacks)?; + .set_config(config, vec![], &callbacks)?; self.io.device_mut().set_routes( HashSet::from([PEERS_IPV4.parse().unwrap(), PEERS_IPV6.parse().unwrap()]), &callbacks,