Merge pull request #25 from tilosp/mtu

This commit is contained in:
Aram 🍐 2021-12-20 15:02:37 -05:00 committed by GitHub
commit 7b6229ca1e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 4 deletions

View file

@ -17,6 +17,7 @@ pub struct Config {
pub(crate) endpoint_addr: SocketAddr, pub(crate) endpoint_addr: SocketAddr,
pub(crate) source_peer_ip: IpAddr, pub(crate) source_peer_ip: IpAddr,
pub(crate) keepalive_seconds: Option<u16>, pub(crate) keepalive_seconds: Option<u16>,
pub(crate) max_transmission_unit: usize,
pub(crate) log: String, pub(crate) log: String,
pub(crate) warnings: Vec<String>, pub(crate) warnings: Vec<String>,
} }
@ -82,6 +83,13 @@ impl Config {
.long("keep-alive") .long("keep-alive")
.env("ONETUN_KEEP_ALIVE") .env("ONETUN_KEEP_ALIVE")
.help("Configures a persistent keep-alive for the WireGuard tunnel, in seconds."), .help("Configures a persistent keep-alive for the WireGuard tunnel, in seconds."),
Arg::with_name("max-transmission-unit")
.required(false)
.takes_value(true)
.long("max-transmission-unit")
.env("ONETUN_MTU")
.default_value("1420")
.help("Configures the max-transmission-unit (MTU) of the WireGuard tunnel."),
Arg::with_name("log") Arg::with_name("log")
.required(false) .required(false)
.takes_value(true) .takes_value(true)
@ -163,6 +171,8 @@ impl Config {
.with_context(|| "Invalid source peer IP")?, .with_context(|| "Invalid source peer IP")?,
keepalive_seconds: parse_keep_alive(matches.value_of("keep-alive")) keepalive_seconds: parse_keep_alive(matches.value_of("keep-alive"))
.with_context(|| "Invalid keep-alive value")?, .with_context(|| "Invalid keep-alive value")?,
max_transmission_unit: parse_mtu(matches.value_of("max-transmission-unit"))
.with_context(|| "Invalid max-transmission-unit value")?,
log: matches.value_of("log").unwrap_or_default().into(), log: matches.value_of("log").unwrap_or_default().into(),
warnings, warnings,
}) })
@ -209,6 +219,12 @@ fn parse_keep_alive(s: Option<&str>) -> anyhow::Result<Option<u16>> {
} }
} }
fn parse_mtu(s: Option<&str>) -> anyhow::Result<usize> {
s.with_context(|| "Missing MTU")?
.parse()
.with_context(|| "Invalid MTU")
}
#[cfg(unix)] #[cfg(unix)]
fn is_file_insecurely_readable(path: &str) -> Option<(bool, bool)> { fn is_file_insecurely_readable(path: &str) -> Option<(bool, bool)> {
use std::fs::File; use std::fs::File;

View file

@ -5,9 +5,6 @@ use smoltcp::phy::{Device, DeviceCapabilities, Medium};
use smoltcp::time::Instant; use smoltcp::time::Instant;
use std::sync::Arc; use std::sync::Arc;
/// The max transmission unit for WireGuard.
const WG_MTU: usize = 1420;
/// A virtual device that processes IP packets. IP packets received from the WireGuard endpoint /// A virtual device that processes IP packets. IP packets received from the WireGuard endpoint
/// are made available to this device using a channel receiver. IP packets sent from this device /// are made available to this device using a channel receiver. IP packets sent from this device
/// are asynchronously sent out to the WireGuard tunnel. /// are asynchronously sent out to the WireGuard tunnel.
@ -71,7 +68,7 @@ impl<'a> Device<'a> for VirtualIpDevice {
fn capabilities(&self) -> DeviceCapabilities { fn capabilities(&self) -> DeviceCapabilities {
let mut cap = DeviceCapabilities::default(); let mut cap = DeviceCapabilities::default();
cap.medium = Medium::Ip; cap.medium = Medium::Ip;
cap.max_transmission_unit = WG_MTU; cap.max_transmission_unit = self.wg.max_transmission_unit;
cap cap
} }
} }

View file

@ -30,6 +30,8 @@ pub struct WireGuardTunnel {
virtual_port_ip_tx: dashmap::DashMap<VirtualPort, tokio::sync::mpsc::Sender<Vec<u8>>>, virtual_port_ip_tx: dashmap::DashMap<VirtualPort, tokio::sync::mpsc::Sender<Vec<u8>>>,
/// IP packet dispatcher for unroutable packets. `None` if not initialized. /// IP packet dispatcher for unroutable packets. `None` if not initialized.
sink_ip_tx: RwLock<Option<tokio::sync::mpsc::Sender<Vec<u8>>>>, sink_ip_tx: RwLock<Option<tokio::sync::mpsc::Sender<Vec<u8>>>>,
/// The max transmission unit for WireGuard.
pub(crate) max_transmission_unit: usize,
} }
impl WireGuardTunnel { impl WireGuardTunnel {
@ -53,6 +55,7 @@ impl WireGuardTunnel {
endpoint, endpoint,
virtual_port_ip_tx, virtual_port_ip_tx,
sink_ip_tx: RwLock::new(None), sink_ip_tx: RwLock::new(None),
max_transmission_unit: config.max_transmission_unit,
}) })
} }