From d12d21132db54c19cbcec7f49302d43e25ff2976 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Raphae=CC=88l=20Enrici?= Date: Thu, 19 Nov 2026 14:58:17 +0100 Subject: [PATCH] Reach a gateway on the LAN directly The host route that keeps a tunnel's transport off the tunnels named the default router even when the gateway was on the uplink's own network (a VPN server on the LAN): the packets went to the router, and the answers, sent directly, might never match (seen on Windows: handshake done, then nothing). The route now goes through the interface when the interface's own route to the gateway has no router (`route get -ifscope` on macOS, `ip route get ... oif` on Linux); a failed lookup keeps the router. Co-Authored-By: Claude Opus 5.5 --- crates/vpn-platform/src/netconfig.rs | 72 +++++++++++++++++++++++++++- 1 file changed, 71 insertions(+), 1 deletion(-) diff --git a/crates/vpn-platform/src/netconfig.rs b/crates/vpn-platform/src/netconfig.rs index 6cd545a..5c51f3a 100644 --- a/crates/vpn-platform/src/netconfig.rs +++ b/crates/vpn-platform/src/netconfig.rs @@ -144,11 +144,18 @@ impl AppliedConfig { // Keep the encrypted transport itself off every tunnel. let mut routes = GATEWAY_ROUTES.lock().await; if let std::collections::hash_map::Entry::Vacant(entry) = routes.entry(gateway) { + // A gateway on the uplink's own network (a server on the + // LAN) is reached directly: through the router, its answers + // may never come back. + let uplink = match on_link(&uplink, gateway).await { + true => Uplink { gateway: None, interface: uplink.interface.clone() }, + false => uplink.clone(), + }; // Never take ownership of a host route someone else installed. add_route(Ipv4Net::from(gateway), &Via::Physical(uplink.clone())) .await .with_context(|| format!("cannot add host route to gateway {gateway}"))?; - entry.insert((0, uplink.clone())); + entry.insert((0, uplink)); } routes.get_mut(&gateway).expect("inserted above").0 += 1; drop(routes); @@ -415,6 +422,54 @@ async fn uplink() -> anyhow::Result { }) } +/// Whether `address` is on the network of the uplink's interface: the +/// interface's own route there goes through no router. A failed lookup +/// keeps the router. +#[cfg(target_os = "macos")] +async fn on_link(uplink: &Uplink, address: Ipv4Addr) -> bool { + if uplink.gateway.is_none() { + return false; + } + let address = address.to_string(); + match cmd::run("/sbin/route", &["-n", "get", "-ifscope", &uplink.interface, &address]).await { + Ok(out) => macos_on_link(&out), + Err(e) => { + tracing::debug!("cannot look up the route to {address}: {e:#}"); + false + } + } +} + +/// `route get`'s answer names a route through a router with its +/// `GATEWAY` flag. +#[cfg(any(target_os = "macos", test))] +fn macos_on_link(out: &str) -> bool { + out.lines() + .filter_map(|l| l.trim().strip_prefix("flags:")) + .any(|flags| !flags.trim().trim_matches(['<', '>']).split(',').any(|f| f == "GATEWAY")) +} + +#[cfg(target_os = "linux")] +async fn on_link(uplink: &Uplink, address: Ipv4Addr) -> bool { + if uplink.gateway.is_none() { + return false; + } + let address = address.to_string(); + match cmd::run("ip", &["-4", "route", "get", &address, "oif", &uplink.interface]).await { + Ok(out) => linux_on_link(&out), + Err(e) => { + tracing::debug!("cannot look up the route to {address}: {e:#}"); + false + } + } +} + +/// `ip route get`'s answer names the router after `via`. +#[cfg(any(target_os = "linux", test))] +fn linux_on_link(out: &str) -> bool { + out.lines().next().is_some_and(|l| !l.is_empty() && !l.split_whitespace().any(|w| w == "via")) +} + fn physical(net: Ipv4Net, uplink: &Uplink) -> journal::Entry { journal::Entry { net, gateway: uplink.gateway, interface: uplink.interface.clone() } } @@ -900,6 +955,21 @@ async fn cleanup_stale_dns() {} #[cfg(test)] mod tests { + #[test] + fn gateway_on_the_link() { + let lan = " route to: 192.168.0.20\ndestination: 192.168.0.0\n mask: 255.255.255.0\n \ + interface: en0\n flags: \n"; + let remote = " route to: 1.1.1.1\ndestination: default\n gateway: 192.168.0.1\n \ + interface: en0\n flags: \n"; + assert!(super::macos_on_link(lan)); + assert!(!super::macos_on_link(remote)); + assert!(!super::macos_on_link("")); + + assert!(super::linux_on_link("192.168.0.20 dev eth0 src 192.168.0.3 uid 0\n cache\n")); + assert!(!super::linux_on_link("1.1.1.1 via 192.168.0.1 dev eth0 src 192.168.0.3 uid 0\n cache\n")); + assert!(!super::linux_on_link("")); + } + #[test] fn resolvers_in_place() { let scutil = "DNS configuration\n\nresolver #1\n search domain[0] : localdomain\n \