Skip to content

Commit 2553725

Browse files
committed
Clarify default MTU
1 parent d7db4c1 commit 2553725

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
@@ -154,14 +154,30 @@ impl TunnelMonitor {
154154
+ Clone
155155
+ 'static,
156156
{
157-
#[cfg(target_os = "linux")]
158-
let detect_mtu = params.options.mtu.is_none();
157+
let default_mtu = if cfg!(target_os = "android") {
158+
// Set the MTU to the lowest possible whilst still allowing for IPv6 to help
159+
// with wireless carriers that do a lot of encapsulation.
160+
1280
161+
} else {
162+
1380
163+
};
159164

160165
#[cfg(any(target_os = "linux", target_os = "windows"))]
161-
args.runtime
162-
.block_on(Self::assign_mtu(&args.route_manager, params));
163-
let config = talpid_wireguard::config::Config::from_parameters(params)?;
166+
// Detects the MTU of the device, calculates what the virtual device MTU should be use that
167+
// as the default mtu
168+
let default_mtu = args
169+
.runtime
170+
.block_on(
171+
args.route_manager
172+
.get_mtu_for_route(params.connection.peer.endpoint.ip()),
173+
)
174+
.map(|mtu| Self::clamp_mtu(params, mtu))
175+
.unwrap_or(default_mtu);
164176

177+
#[cfg(target_os = "linux")]
178+
let detect_mtu = params.options.mtu.is_none();
179+
180+
let config = talpid_wireguard::config::Config::from_parameters(params, default_mtu)?;
165181
let monitor = talpid_wireguard::WireguardMonitor::start(
166182
config,
167183
params.options.quantum_resistant,
@@ -174,11 +190,10 @@ impl TunnelMonitor {
174190
monitor: InternalTunnelMonitor::Wireguard(monitor),
175191
})
176192
}
177-
178-
/// Set the MTU in the tunnel parameters based on the inputted device MTU and some
193+
/// Calculates the MTU in the tunnel parameters based on the inputted device MTU and some
179194
/// calculations. `peer_mtu` is the detected device MTU.
180195
#[cfg(any(target_os = "linux", target_os = "windows"))]
181-
fn set_mtu(params: &mut wireguard_types::TunnelParameters, peer_mtu: u16) {
196+
fn clamp_mtu(params: &wireguard_types::TunnelParameters, peer_mtu: u16) -> u16 {
182197
// Some users experience fragmentation issues even when we take the interface MTU and
183198
// subtract the header sizes. This is likely due to some program that they use which does
184199
// not change the interface MTU but adds its own header onto the outgoing packets. For this
@@ -202,31 +217,10 @@ impl TunnelMonitor {
202217
false => MIN_IPV4_MTU,
203218
true => MIN_IPV6_MTU,
204219
};
205-
let tunnel_mtu = peer_mtu
206-
.saturating_sub(total_header_size)
207-
.clamp(min_mtu, max_peer_mtu);
208-
params.options.mtu = Some(tunnel_mtu);
209-
}
210220

211-
/// Detects the MTU of the device, calculates what the virtual device MTU should be and sets
212-
/// that in the tunnel parameters.
213-
#[cfg(any(target_os = "linux", target_os = "windows"))]
214-
async fn assign_mtu(
215-
route_manager: &RouteManagerHandle,
216-
params: &mut wireguard_types::TunnelParameters,
217-
) {
218-
// Only calculate the mtu automatically if the user has not set any
219-
if params.options.mtu.is_none() {
220-
match route_manager
221-
.get_mtu_for_route(params.connection.peer.endpoint.ip())
222-
.await
223-
{
224-
Ok(mtu) => Self::set_mtu(params, mtu),
225-
Err(e) => {
226-
log::error!("Could not get the MTU for route {}", e);
227-
}
228-
}
229-
}
221+
peer_mtu
222+
.saturating_sub(total_header_size)
223+
.clamp(min_mtu, max_peer_mtu)
230224
}
231225

232226
#[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)