diff options
| author | Emīls Piņķis <emils@mullvad.net> | 2022-11-01 15:48:19 +0000 |
|---|---|---|
| committer | Emīls <emils@mullvad.net> | 2022-11-07 11:54:50 +0100 |
| commit | 61b6dc43b32e63c94743b68791a49e810244a334 (patch) | |
| tree | 16c8fc9e2b99f19cfbc1c9f2e2282883d29c4091 | |
| parent | 51c9ca595ebeb63142737c0d824032d1df4c55bc (diff) | |
| download | mullvadvpn-61b6dc43b32e63c94743b68791a49e810244a334.tar.xz mullvadvpn-61b6dc43b32e63c94743b68791a49e810244a334.zip | |
Refactor routing code
Simplify routing to abstract some of the platform specific details
| -rw-r--r-- | talpid-openvpn/src/lib.rs | 2 | ||||
| -rw-r--r-- | talpid-routing/src/lib.rs | 9 | ||||
| -rw-r--r-- | talpid-routing/src/linux.rs | 8 | ||||
| -rw-r--r-- | talpid-routing/src/windows/route_manager.rs | 9 | ||||
| -rw-r--r-- | talpid-wireguard/src/lib.rs | 16 |
5 files changed, 19 insertions, 25 deletions
diff --git a/talpid-openvpn/src/lib.rs b/talpid-openvpn/src/lib.rs index 6ea22792aa..e49cbd121a 100644 --- a/talpid-openvpn/src/lib.rs +++ b/talpid-openvpn/src/lib.rs @@ -366,7 +366,7 @@ fn extract_routes(env: &HashMap<String, String>) -> Result<HashSet<RequiredRoute let tun_node = talpid_routing::Node::device(tun_interface.to_string()); let mut routes = HashSet::new(); for network in &["0.0.0.0/0".parse().unwrap(), "::/0".parse().unwrap()] { - routes.insert(RequiredRoute::new(*network, tun_node.clone())); + routes.insert(RequiredRoute::new(*network, tun_node.clone()).use_main_table(false)); } Ok(routes) } diff --git a/talpid-routing/src/lib.rs b/talpid-routing/src/lib.rs index 06a8a09d18..6e59d01b97 100644 --- a/talpid-routing/src/lib.rs +++ b/talpid-routing/src/lib.rs @@ -80,8 +80,9 @@ pub struct RequiredRoute { /// Route's prefix pub prefix: IpNetwork, node: NetNode, + /// Specifies whether the route should be added to the main routing table or not. #[cfg(target_os = "linux")] - table_id: u32, + main_table: bool, } impl RequiredRoute { @@ -91,14 +92,14 @@ impl RequiredRoute { node: node.into(), prefix, #[cfg(target_os = "linux")] - table_id: u32::from(RT_TABLE_MAIN), + main_table: true, } } /// Sets the routing table ID of the route. #[cfg(target_os = "linux")] - pub fn table(mut self, new_id: u32) -> Self { - self.table_id = new_id; + pub fn use_main_table(mut self, main_table: bool) -> Self { + self.main_table = main_table; self } } diff --git a/talpid-routing/src/linux.rs b/talpid-routing/src/linux.rs index edd2893ff3..bdfe88f578 100644 --- a/talpid-routing/src/linux.rs +++ b/talpid-routing/src/linux.rs @@ -295,8 +295,12 @@ impl RouteManagerImpl { for route in required_routes { match route.node { NetNode::RealNode(node) => { - required_normal_routes - .insert(Route::new(node, route.prefix).table(route.table_id)); + let table = if route.main_table { + RT_TABLE_MAIN.into() + } else { + self.table_id + }; + required_normal_routes.insert(Route::new(node, route.prefix).table(table)); } } } diff --git a/talpid-routing/src/windows/route_manager.rs b/talpid-routing/src/windows/route_manager.rs index ad35d05f77..ab4c9ef45b 100644 --- a/talpid-routing/src/windows/route_manager.rs +++ b/talpid-routing/src/windows/route_manager.rs @@ -107,7 +107,6 @@ struct EventEntry { enum RecordEventType { AddRoute, - DeleteRoute, } pub type Callback = Box<dyn for<'a> Fn(RouteMonitorEventType<'a>, AddressFamily) + Send>; @@ -338,14 +337,6 @@ impl RouteManagerInternal { } records.remove(record_idx); } - RecordEventType::DeleteRoute => { - if let Err(e) = Self::restore_into_routing_table(&event.record.registered_route) - { - result = result.and(Err(e)); - continue; - } - records.push(event.record.clone()); - } } } diff --git a/talpid-wireguard/src/lib.rs b/talpid-wireguard/src/lib.rs index da4ea128a7..89da37acb1 100644 --- a/talpid-wireguard/src/lib.rs +++ b/talpid-wireguard/src/lib.rs @@ -10,8 +10,6 @@ use futures::{channel::mpsc, StreamExt}; #[cfg(target_os = "linux")] use lazy_static::lazy_static; #[cfg(target_os = "linux")] -use netlink_packet_route::rtnl::constants::RT_TABLE_MAIN; -#[cfg(target_os = "linux")] use std::env; #[cfg(windows)] use std::io; @@ -694,11 +692,6 @@ impl WireguardMonitor { }), ); - // The gateway route, as well as the exit endpoint, need to be in the main table. - // Otherwise, DNS will not work for excluded apps, nor will the exit be reachable. - #[cfg(target_os = "linux")] - let routes = routes.map(|route| route.table(u32::from(RT_TABLE_MAIN))); - routes } @@ -708,7 +701,7 @@ impl WireguardMonitor { config: &'a Config, ) -> impl Iterator<Item = RequiredRoute> + 'a { let (node_v4, node_v6) = Self::get_tunnel_nodes(iface_name, config); - Self::get_tunnel_destinations(config) + let iter = Self::get_tunnel_destinations(config) .filter(|allowed_ip| allowed_ip.prefix() == 0) .flat_map(Self::replace_default_prefixes) .map(move |allowed_ip| { @@ -717,7 +710,12 @@ impl WireguardMonitor { } else { RequiredRoute::new(allowed_ip, node_v6.clone()) } - }) + }); + #[cfg(not(target_os = "linux"))] + return iter; + + #[cfg(target_os = "linux")] + iter.map(|route| route.use_main_table(false)) } /// Return routes for all allowed IPs. |
