Skip to content

Commit 82f46bd

Browse files
committed
Clarify default MTU
1 parent 1e95106 commit 82f46bd

File tree

2 files changed

+34
-42
lines changed

2 files changed

+34
-42
lines changed

talpid-core/src/tunnel/mod.rs

+26-32
Original file line numberDiff line numberDiff line change
@@ -137,14 +137,30 @@ impl TunnelMonitor {
137137
+ Clone
138138
+ 'static,
139139
{
140-
#[cfg(target_os = "linux")]
141-
let detect_mtu = params.options.mtu.is_none();
140+
let default_mtu = if cfg!(target_os = "android") {
141+
// Set the MTU to the lowest possible whilst still allowing for IPv6 to help
142+
// with wireless carriers that do a lot of encapsulation.
143+
1280
144+
} else {
145+
1380
146+
};
142147

143148
#[cfg(any(target_os = "linux", target_os = "windows"))]
144-
args.runtime
145-
.block_on(Self::assign_mtu(&args.route_manager, params));
146-
let config = talpid_wireguard::config::Config::from_parameters(params)?;
149+
// Detects the MTU of the device, calculates what the virtual device MTU should be use that
150+
// as the default mtu
151+
let default_mtu = args
152+
.runtime
153+
.block_on(
154+
args.route_manager
155+
.get_mtu_for_route(params.connection.peer.endpoint.ip()),
156+
)
157+
.map(|mtu| Self::clamp_mtu(params, mtu))
158+
.unwrap_or(default_mtu);
147159

160+
#[cfg(target_os = "linux")]
161+
let detect_mtu = params.options.mtu.is_none();
162+
163+
let config = talpid_wireguard::config::Config::from_parameters(params, default_mtu)?;
148164
let monitor = talpid_wireguard::WireguardMonitor::start(
149165
config,
150166
params.options.quantum_resistant,
@@ -157,11 +173,10 @@ impl TunnelMonitor {
157173
monitor: InternalTunnelMonitor::Wireguard(monitor),
158174
})
159175
}
160-
161-
/// Set the MTU in the tunnel parameters based on the inputted device MTU and some
176+
/// Calculates the MTU in the tunnel parameters based on the inputted device MTU and some
162177
/// calculations. `peer_mtu` is the detected device MTU.
163178
#[cfg(any(target_os = "linux", target_os = "windows"))]
164-
fn set_mtu(params: &mut wireguard_types::TunnelParameters, peer_mtu: u16) {
179+
fn clamp_mtu(params: &wireguard_types::TunnelParameters, peer_mtu: u16) -> u16 {
165180
// Some users experience fragmentation issues even when we take the interface MTU and
166181
// subtract the header sizes. This is likely due to some program that they use which does
167182
// not change the interface MTU but adds its own header onto the outgoing packets. For this
@@ -185,31 +200,10 @@ impl TunnelMonitor {
185200
false => MIN_IPV4_MTU,
186201
true => MIN_IPV6_MTU,
187202
};
188-
let tunnel_mtu = peer_mtu
189-
.saturating_sub(total_header_size)
190-
.clamp(min_mtu, max_peer_mtu);
191-
params.options.mtu = Some(tunnel_mtu);
192-
}
193203

194-
/// Detects the MTU of the device, calculates what the virtual device MTU should be and sets
195-
/// that in the tunnel parameters.
196-
#[cfg(any(target_os = "linux", target_os = "windows"))]
197-
async fn assign_mtu(
198-
route_manager: &RouteManagerHandle,
199-
params: &mut wireguard_types::TunnelParameters,
200-
) {
201-
// Only calculate the mtu automatically if the user has not set any
202-
if params.options.mtu.is_none() {
203-
match route_manager
204-
.get_mtu_for_route(params.connection.peer.endpoint.ip())
205-
.await
206-
{
207-
Ok(mtu) => Self::set_mtu(params, mtu),
208-
Err(e) => {
209-
log::error!("Could not get the MTU for route {}", e);
210-
}
211-
}
212-
}
204+
peer_mtu
205+
.saturating_sub(total_header_size)
206+
.clamp(min_mtu, max_peer_mtu)
213207
}
214208

215209
#[cfg(not(target_os = "android"))]

talpid-wireguard/src/config.rs

+8-10
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,6 @@ pub struct Config {
3030
pub obfuscator_config: Option<ObfuscatorConfig>,
3131
}
3232

33-
/// Set the MTU to the lowest possible whilst still allowing for IPv6 to help with wireless
34-
/// carriers that do a lot of encapsulation.
35-
const DEFAULT_MTU: u16 = if cfg!(target_os = "android") {
36-
1280
37-
} else {
38-
1380
39-
};
40-
4133
/// Configuration errors
4234
#[derive(err_derive::Error, Debug)]
4335
pub enum Error {
@@ -52,12 +44,16 @@ pub enum Error {
5244

5345
impl Config {
5446
/// Constructs a Config from parameters
55-
pub fn from_parameters(params: &wireguard::TunnelParameters) -> Result<Config, Error> {
47+
pub fn from_parameters(
48+
params: &wireguard::TunnelParameters,
49+
default_mtu: u16,
50+
) -> Result<Config, Error> {
5651
Self::new(
5752
&params.connection,
5853
&params.options,
5954
&params.generic_options,
6055
&params.obfuscation,
56+
default_mtu,
6157
)
6258
}
6359

@@ -67,9 +63,11 @@ impl Config {
6763
wg_options: &wireguard::TunnelOptions,
6864
generic_options: &GenericTunnelOptions,
6965
obfuscator_config: &Option<ObfuscatorConfig>,
66+
default_mtu: u16,
7067
) -> Result<Config, Error> {
7168
let mut tunnel = connection.tunnel.clone();
72-
let mtu = wg_options.mtu.unwrap_or(DEFAULT_MTU);
69+
70+
let mtu = wg_options.mtu.unwrap_or(default_mtu);
7371

7472
if tunnel.addresses.is_empty() {
7573
return Err(Error::InvalidTunnelIpError);

0 commit comments

Comments
 (0)