Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Force WG handshake each time the config is updated #7348

Merged
merged 4 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Line wrap the file at 100 chars. Th
### Changed
- (Linux and macOS only) Update to DAITA v2. The main difference is that many different machines are
provided by relays instead of a bundled list. The bundled `maybenot_machines` file was removed.
- Test tunnel before ephemeral peer exchange. This is an attempt to fix timeout issues.

### Fixed
#### macOS
Expand Down
8 changes: 8 additions & 0 deletions talpid-wireguard/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ fn main() {
// Enable DAITA by default on desktop and android
println!("cargo::rustc-check-cfg=cfg(daita)");
println!("cargo::rustc-cfg=daita");

// Ensure that the WireGuard tunnel works before exchanging ephemeral peers.
// This is useful after updating the WireGuard config, to force a WireGuard handshake. This
// should reduce the number of PQ timeouts.
println!("cargo::rustc-check-cfg=cfg(force_wireguard_handshake)");
if matches!(target_os.as_str(), "linux" | "macos" | "windows") {
println!("cargo::rustc-cfg=force_wireguard_handshake");
}
}

fn declare_libs_dir(base: &str) {
Expand Down
2 changes: 2 additions & 0 deletions talpid-wireguard/src/connectivity/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ impl<S: Strategy> Check<S> {
// checks if the tunnel has ever worked. Intended to check if a connection to a tunnel is
// successful at the start of a connection.
pub fn establish_connectivity(&mut self, tunnel_handle: &TunnelType) -> Result<bool, Error> {
self.conn_state = ConnState::new(Instant::now(), Default::default());

// Send initial ping to prod WireGuard into connecting.
self.ping_state
.pinger
Expand Down
4 changes: 1 addition & 3 deletions talpid-wireguard/src/connectivity/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ mod mock;
mod monitor;
mod pinger;

#[cfg(target_os = "android")]
pub use check::Cancellable;
pub use check::Check;
pub use check::{Cancellable, Check};
pub use error::Error;
pub use monitor::Monitor;
65 changes: 62 additions & 3 deletions talpid-wireguard/src/ephemeral.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! This module takes care of obtaining ephemeral peers, updating the WireGuard configuration and
//! restarting obfuscation and WG tunnels when necessary.

#[cfg(force_wireguard_handshake)]
use super::connectivity;
#[cfg(target_os = "android")] // On Android, the Tunnel trait is not imported by default.
use super::Tunnel;
use super::{config::Config, obfuscation::ObfuscatorHandle, CloseMsg, Error, TunnelType};
Expand Down Expand Up @@ -31,6 +33,9 @@ pub async fn config_ephemeral_peers(
retry_attempt: u32,
obfuscator: Arc<AsyncMutex<Option<ObfuscatorHandle>>>,
close_obfs_sender: sync_mpsc::Sender<CloseMsg>,
#[cfg(force_wireguard_handshake)] connectivity: &mut connectivity::Check<
connectivity::Cancellable,
>,
) -> std::result::Result<(), CloseMsg> {
let iface_name = {
let tunnel = tunnel.lock().await;
Expand All @@ -44,8 +49,16 @@ pub async fn config_ephemeral_peers(
log::trace!("Temporarily lowering tunnel MTU before ephemeral peer config");
try_set_ipv4_mtu(&iface_name, talpid_tunnel::MIN_IPV4_MTU);

config_ephemeral_peers_inner(tunnel, config, retry_attempt, obfuscator, close_obfs_sender)
.await?;
config_ephemeral_peers_inner(
tunnel,
config,
retry_attempt,
obfuscator,
close_obfs_sender,
#[cfg(force_wireguard_handshake)]
connectivity,
)
.await?;

log::trace!("Resetting tunnel MTU");
try_set_ipv4_mtu(&iface_name, config.mtu);
Expand Down Expand Up @@ -75,6 +88,9 @@ pub async fn config_ephemeral_peers(
retry_attempt: u32,
obfuscator: Arc<AsyncMutex<Option<ObfuscatorHandle>>>,
close_obfs_sender: sync_mpsc::Sender<CloseMsg>,
#[cfg(force_wireguard_handshake)] connectivity: &mut connectivity::Check<
connectivity::Cancellable,
>,
#[cfg(target_os = "android")] tun_provider: Arc<Mutex<TunProvider>>,
) -> Result<(), CloseMsg> {
config_ephemeral_peers_inner(
Expand All @@ -83,6 +99,8 @@ pub async fn config_ephemeral_peers(
retry_attempt,
obfuscator,
close_obfs_sender,
#[cfg(force_wireguard_handshake)]
connectivity,
#[cfg(target_os = "android")]
tun_provider,
)
Expand All @@ -95,8 +113,16 @@ async fn config_ephemeral_peers_inner(
retry_attempt: u32,
obfuscator: Arc<AsyncMutex<Option<ObfuscatorHandle>>>,
close_obfs_sender: sync_mpsc::Sender<CloseMsg>,
#[cfg(force_wireguard_handshake)] connectivity: &mut connectivity::Check<
connectivity::Cancellable,
>,
#[cfg(target_os = "android")] tun_provider: Arc<Mutex<TunProvider>>,
) -> Result<(), CloseMsg> {
// NOTE: This one often fails with multihop on Windows, even though the handshake afterwards
// succeeds. So we try anyway if it fails.
#[cfg(force_wireguard_handshake)]
let _ = establish_tunnel_connection(tunnel, connectivity).await;

let ephemeral_private_key = PrivateKey::new_from_random();
let close_obfs_sender = close_obfs_sender.clone();

Expand Down Expand Up @@ -134,6 +160,10 @@ async fn config_ephemeral_peers_inner(
&tun_provider,
)
.await?;

#[cfg(force_wireguard_handshake)]
establish_tunnel_connection(tunnel, connectivity).await?;

let entry_ephemeral_peer = request_ephemeral_peer(
retry_attempt,
&entry_config,
Expand Down Expand Up @@ -214,7 +244,6 @@ async fn reconfigure_tunnel(
*obfs_guard = super::obfuscation::apply_obfuscation_config(
&mut config,
close_obfs_sender,
#[cfg(target_os = "android")]
tun_provider.clone(),
)
.await
Expand Down Expand Up @@ -268,6 +297,36 @@ async fn reconfigure_tunnel(
Ok(config)
}

/// Ensure that the WireGuard tunnel works. This is useful after updating the WireGuard config, to
/// force a WireGuard handshake. This should reduce the number of PQ timeouts.
#[cfg(force_wireguard_handshake)]
async fn establish_tunnel_connection(
tunnel: &Arc<AsyncMutex<Option<TunnelType>>>,
connectivity: &mut connectivity::Check<connectivity::Cancellable>,
) -> Result<(), CloseMsg> {
use talpid_types::ErrorExt;

let shared_tunnel = tunnel.lock().await;
let tunnel = shared_tunnel.as_ref().expect("tunnel was None");
let ping_result = connectivity.establish_connectivity(tunnel);
drop(shared_tunnel);

match ping_result {
Ok(true) => Ok(()),
Ok(false) => {
log::warn!("Timeout while checking tunnel connection");
Err(CloseMsg::PingErr)
}
Err(error) => {
log::error!(
"{}",
error.display_chain_with_msg("Failed to check tunnel connection")
);
Err(CloseMsg::PingErr)
}
}
}

async fn request_ephemeral_peer(
retry_attempt: u32,
config: &Config,
Expand Down
2 changes: 2 additions & 0 deletions talpid-wireguard/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,8 @@ impl WireguardMonitor {
args.retry_attempt,
obfuscator.clone(),
ephemeral_obfs_sender,
#[cfg(force_wireguard_handshake)]
&mut connectivity_monitor,
)
.await?;

Expand Down
Loading