summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJanito Vaqueiro Ferreira Filho <janito@mullvad.net>2019-05-29 09:30:10 -0300
committerJanito Vaqueiro Ferreira Filho <janito@mullvad.net>2019-05-29 09:30:10 -0300
commitd0d9d53cd9d02334a5c0bdbc87c7e9d01212359e (patch)
tree03737f7a875bf3b373e26b7a120dc4b0410f4e8b
parentc90aaf9c3f5cc830080ddaf94a71120e4c5eaf3d (diff)
parent846a6ed7991a588c8762ae24657e21eafe630a02 (diff)
downloadmullvadvpn-d0d9d53cd9d02334a5c0bdbc87c7e9d01212359e.tar.xz
mullvadvpn-d0d9d53cd9d02334a5c0bdbc87c7e9d01212359e.zip
Merge branch 'extra-tun-config'
-rw-r--r--talpid-core/Cargo.toml2
-rw-r--r--talpid-core/src/tunnel/mod.rs2
-rw-r--r--talpid-core/src/tunnel/tun_provider/mod.rs18
-rw-r--r--talpid-core/src/tunnel/wireguard/mod.rs36
-rw-r--r--talpid-core/src/tunnel/wireguard/wireguard_go.rs19
5 files changed, 48 insertions, 29 deletions
diff --git a/talpid-core/Cargo.toml b/talpid-core/Cargo.toml
index 2402bd2e52..5152de73e1 100644
--- a/talpid-core/Cargo.toml
+++ b/talpid-core/Cargo.toml
@@ -13,6 +13,7 @@ derive_more = "0.14"
duct = "0.12"
err-derive = "0.1.5"
futures = "0.1"
+ipnetwork = "0.14"
jsonrpc-core = { git = "https://github.com/mullvad/jsonrpc", branch = "mullvad-fork" }
jsonrpc-macros = { git = "https://github.com/mullvad/jsonrpc", branch = "mullvad-fork" }
libc = "0.2.20"
@@ -30,7 +31,6 @@ uuid = { version = "0.7", features = ["v4"] }
[target.'cfg(unix)'.dependencies]
hex = "0.3"
-ipnetwork = "0.14"
lazy_static = "1.0"
nix = "0.13"
tokio-process = "0.2"
diff --git a/talpid-core/src/tunnel/mod.rs b/talpid-core/src/tunnel/mod.rs
index 68f6c8ed63..62bc165ed6 100644
--- a/talpid-core/src/tunnel/mod.rs
+++ b/talpid-core/src/tunnel/mod.rs
@@ -139,7 +139,7 @@ pub struct TunnelMonitor {
impl TunnelMonitor {
/// Creates a new `TunnelMonitor` that connects to the given remote and notifies `on_event`
/// on tunnel state changes.
- #[cfg_attr(target_os = "android", allow(unused_variables))]
+ #[cfg_attr(any(target_os = "android", windows), allow(unused_variables))]
pub fn start<L>(
tunnel_parameters: &TunnelParameters,
log_dir: &Option<PathBuf>,
diff --git a/talpid-core/src/tunnel/tun_provider/mod.rs b/talpid-core/src/tunnel/tun_provider/mod.rs
index 43020114c7..69ff7d8b62 100644
--- a/talpid-core/src/tunnel/tun_provider/mod.rs
+++ b/talpid-core/src/tunnel/tun_provider/mod.rs
@@ -1,4 +1,5 @@
use cfg_if::cfg_if;
+use ipnetwork::IpNetwork;
use std::net::IpAddr;
#[cfg(unix)]
use std::os::unix::io::AsRawFd;
@@ -47,16 +48,17 @@ pub trait TunProvider: Send + 'static {
}
/// Configuration for creating a tunnel device.
+#[derive(Clone, Debug)]
pub struct TunConfig {
/// IP addresses for the tunnel interface.
pub addresses: Vec<IpAddr>,
-}
-impl TunConfig {
- /// Create a new tunnel device configuration using the specified tunnel addresses.
- pub fn new(addresses: impl IntoIterator<Item = IpAddr>) -> Self {
- TunConfig {
- addresses: addresses.into_iter().collect(),
- }
- }
+ /// IP addresses for the DNS servers to use.
+ pub dns_servers: Vec<IpAddr>,
+
+ /// Routes to configure for the tunnel.
+ pub routes: Vec<IpNetwork>,
+
+ /// Maximum Transmission Unit in the tunnel.
+ pub mtu: u16,
}
diff --git a/talpid-core/src/tunnel/wireguard/mod.rs b/talpid-core/src/tunnel/wireguard/mod.rs
index 05fecdb29f..36d0f9d61b 100644
--- a/talpid-core/src/tunnel/wireguard/mod.rs
+++ b/talpid-core/src/tunnel/wireguard/mod.rs
@@ -68,7 +68,12 @@ impl WireguardMonitor {
on_event: F,
tun_provider: &dyn TunProvider,
) -> Result<WireguardMonitor> {
- let tunnel = Box::new(WgGoTunnel::start_tunnel(&config, log_path, tun_provider)?);
+ let tunnel = Box::new(WgGoTunnel::start_tunnel(
+ &config,
+ log_path,
+ tun_provider,
+ Self::get_tunnel_routes(config),
+ )?);
let iface_name = tunnel.get_interface_name();
let route_handle = routing::RouteManager::new(
Self::get_routes(iface_name, &config),
@@ -135,12 +140,8 @@ impl WireguardMonitor {
wait_result
}
- fn get_routes(
- iface_name: &str,
- config: &Config,
- ) -> HashMap<ipnetwork::IpNetwork, crate::routing::NetNode> {
- let node = routing::Node::device(iface_name.to_string());
- let mut routes: HashMap<_, _> = config
+ fn get_tunnel_routes(config: &Config) -> impl Iterator<Item = ipnetwork::IpNetwork> + '_ {
+ config
.peers
.iter()
.flat_map(|peer| peer.allowed_ips.iter())
@@ -148,20 +149,23 @@ impl WireguardMonitor {
.flat_map(|allowed_ip| {
if allowed_ip.prefix() == 0 {
if allowed_ip.is_ipv4() {
- vec![
- ("0.0.0.0/1".parse().unwrap(), node.clone().into()),
- ("128.0.0.0/1".parse().unwrap(), node.clone().into()),
- ]
+ vec!["0.0.0.0/1".parse().unwrap(), "128.0.0.0/1".parse().unwrap()]
} else {
- vec![
- ("8000::/1".parse().unwrap(), node.clone().into()),
- ("::/1".parse().unwrap(), node.clone().into()),
- ]
+ vec!["8000::/1".parse().unwrap(), "::/1".parse().unwrap()]
}
} else {
- vec![(allowed_ip, node.clone().into())]
+ vec![allowed_ip]
}
})
+ }
+
+ fn get_routes(
+ iface_name: &str,
+ config: &Config,
+ ) -> HashMap<ipnetwork::IpNetwork, crate::routing::NetNode> {
+ let node = routing::Node::device(iface_name.to_string());
+ let mut routes: HashMap<_, _> = Self::get_tunnel_routes(config)
+ .map(|network| (network, node.clone().into()))
.collect();
// route endpoints with specific routes
diff --git a/talpid-core/src/tunnel/wireguard/wireguard_go.rs b/talpid-core/src/tunnel/wireguard/wireguard_go.rs
index 9071766dbb..eca00b76bc 100644
--- a/talpid-core/src/tunnel/wireguard/wireguard_go.rs
+++ b/talpid-core/src/tunnel/wireguard/wireguard_go.rs
@@ -1,6 +1,7 @@
use super::{Config, Error, Result, Tunnel};
use crate::tunnel::tun_provider::{Tun, TunConfig, TunProvider};
-use std::{ffi::CString, fs, os::unix::io::AsRawFd, path::Path};
+use ipnetwork::IpNetwork;
+use std::{ffi::CString, fs, net::IpAddr, os::unix::io::AsRawFd, path::Path};
pub struct WgGoTunnel {
interface_name: String,
@@ -16,10 +17,10 @@ impl WgGoTunnel {
config: &Config,
log_path: Option<&Path>,
tun_provider: &dyn TunProvider,
+ routes: impl Iterator<Item = IpNetwork>,
) -> Result<Self> {
- let tunnel_config = TunConfig::new(config.tunnel.addresses.clone());
let tunnel_device = tun_provider
- .create_tun(tunnel_config)
+ .create_tun(Self::create_tunnel_config(config, routes))
.map_err(Error::SetupTunnelDeviceError)?;
let interface_name: String = tunnel_device.interface_name().to_string();
@@ -52,6 +53,18 @@ impl WgGoTunnel {
})
}
+ fn create_tunnel_config(config: &Config, routes: impl Iterator<Item = IpNetwork>) -> TunConfig {
+ let mut dns_servers = vec![IpAddr::V4(config.ipv4_gateway)];
+ dns_servers.extend(config.ipv6_gateway.clone().map(IpAddr::V6));
+
+ TunConfig {
+ addresses: config.tunnel.addresses.clone(),
+ dns_servers,
+ routes: routes.collect(),
+ mtu: config.mtu,
+ }
+ }
+
fn stop_tunnel(&mut self) -> Result<()> {
if let Some(handle) = self.handle.take() {
let status = unsafe { wgTurnOff(handle) };