Keep every active mesh reachable through a single internet exit

With several IPv4 aliases on a single TUN, the default route could select an address from a different mesh. The exit peer then rejected internet traffic as an invalid source. Concurrent cross-network exit selections could also clear each other in the running agent while SQLite kept one winner.

Serialize validated exit choices with network lifecycle and persist each choice before publishing it to the shared routing table. Replace the previous exit atomically and preserve it across allocation updates. Both mesh subnets keep their own routes. Only replies from the selected exit to the local address of its network qualify for the external-source exception.

Prefer the selected source on Linux and macOS default routes. Preserve physical transport paths and update split routes in place. On Windows reuse the Android packet translation to restore off-mesh TCP, UDP and ICMP replies to the original local socket. Existing databases, IPC commands and wire protocols remain compatible.

On macOS, formatting and strict workspace Clippy passed. The full locked workspace suite passed 577 tests with three ignored, after correcting a stale Windows script-test matcher. Three real agents covered switching, concurrent selection, offline isolation and restart. On Linux arm64, 88 focused tests and the privileged kernel routing/firewall test passed. The Android arm64 library built and linked. Wintun and privileged macOS utun were not exercised on live hosts.
This commit is contained in:
Aleksandr Bogomiakov committed 2026-11-25 02:32:09 +02:00
1 parent 21b8381fa3
commit 32aa73721a
23 files changed
+1167 -304

No files matched your search

+41 -60
View File
@@ -104,7 +104,7 @@ struct Inner {
networks: RwLock<HashMap<NetworkId, NetworkHandle>>,
shutdown: Shutdown,
/// Serialises start, stop, switching and conflict cleanup, including teardown.
lifecycle: Mutex<()>,
lifecycle: Arc<Mutex<()>>,
range_changed: watch::Sender<()>,
range_task: std::sync::Mutex<Option<JoinHandle<()>>>,
accept_task: std::sync::Mutex<Option<JoinHandle<()>>>,
@@ -450,7 +450,7 @@ impl Agent {
events,
networks: RwLock::new(HashMap::new()),
shutdown: Shutdown::new(),
lifecycle: Mutex::new(()),
lifecycle: Arc::new(Mutex::new(())),
range_changed,
range_task: std::sync::Mutex::new(None),
accept_task: std::sync::Mutex::new(None),
@@ -797,47 +797,27 @@ impl Agent {
/// choice in the others. The member has to be connected and offering
/// to be an exit node. Remembered across restarts.
pub async fn set_exit_via(&self, network_id: NetworkId, via: Option<EndpointId>) -> Result<()> {
let active = self.is_active(network_id).await;
if via.is_some() && !active {
return Err(Error::NetworkNotActive(network_id));
}
if active {
let (reply, receive) = oneshot::channel();
self.command(network_id, NetCommand::SetExitVia { via, reply })
.await?;
receive
.await
.map_err(|_| Error::Stopped)?
.map_err(|reason| Error::Rejected { reason })?;
}
self.inner
.storage
.set_exit_via(network_id, via.map(|peer| *peer.as_bytes()))
.await?;
if via.is_some() {
// One default route: whatever the other networks were doing, they
// are not any more.
let others: Vec<NetworkId> = self
.inner
.networks
.read()
.await
.keys()
.copied()
.filter(|other| *other != network_id)
.collect();
for other in others {
let (reply, receive) = oneshot::channel();
if self
.command(other, NetCommand::SetExitVia { via: None, reply })
.await
.is_ok()
{
let _ = receive.await;
}
let lifecycle = self.inner.lifecycle.clone().lock_owned().await;
if !self.is_active(network_id).await {
if via.is_some() {
return Err(Error::NetworkNotActive(network_id));
}
return self.inner.storage.set_exit_via(network_id, None).await;
}
Ok(())
// The actor owns the guard through validation, persistence and routing,
// even if the requesting IPC client disconnects halfway through.
let (reply, receive) = oneshot::channel();
self.command(
network_id,
NetCommand::SetExitVia {
via,
lifecycle,
reply,
},
)
.await?;
receive.await.map_err(|_| Error::Stopped)?
}
/// Changes a network's local broadcast policy now and after restart.
@@ -961,6 +941,13 @@ impl Agent {
)?;
}
let range = self.desired_range(network_id).await?;
let exit = crate::overlay::ExitPolicy {
offer: stored.as_ref().is_some_and(|s| s.exit_node),
via: stored
.as_ref()
.and_then(|s| s.exit_via)
.and_then(|bytes| EndpointId::from_bytes(&bytes).ok()),
};
if let Some(range) = range {
let conflicts = self.inner.routes.overlapping_networks(network_id, range);
if !conflicts.is_empty() {
@@ -970,19 +957,20 @@ impl Agent {
conflicts: conflicts.into_iter().map(|(id, _)| id).collect(),
});
}
self.inner
.routes
.set_network(
network_id,
crate::overlay::NetworkRoutes {
range: Some(range),
..Default::default()
},
)
.map_err(|err| Error::Rejected {
reason: err.to_string(),
})?;
}
self.inner
.routes
.set_network(
network_id,
crate::overlay::NetworkRoutes {
range,
exit,
..Default::default()
},
)
.map_err(|err| Error::Rejected {
reason: err.to_string(),
})?;
let mut backends = Vec::new();
if let Some(discovery) = &self.inner.config.discovery {
backends.push(discovery.clone());
@@ -1006,17 +994,10 @@ impl Agent {
if !plugins.is_empty() {
self.inner.ensure_interface().await;
}
let exit_offer = stored.as_ref().is_some_and(|stored| stored.exit_node);
let exit_via = stored
.as_ref()
.and_then(|stored| stored.exit_via)
.and_then(|bytes| EndpointId::from_bytes(&bytes).ok());
let handle = network::spawn(RuntimeParams {
vips,
retirements,
broadcast,
exit_offer,
exit_via,
keys,
adapter: self.inner.adapter.clone(),
storage: self.inner.storage.clone(),
+57 -42
View File
@@ -96,7 +96,8 @@ pub(crate) enum NetCommand {
/// Send this device's internet traffic through a member, or stop.
SetExitVia {
via: Option<EndpointId>,
reply: oneshot::Sender<std::result::Result<(), String>>,
lifecycle: tokio::sync::OwnedMutexGuard<()>,
reply: oneshot::Sender<Result<()>>,
},
/// Resend this agent's announcement to every peer of this network.
Reannounce,
@@ -177,10 +178,6 @@ pub(crate) struct RuntimeParams {
pub(crate) vips: crate::vip::VipSet,
pub(crate) retirements: Vec<SignedRetirement>,
pub(crate) broadcast: bool,
/// This agent offers itself as an exit node here.
pub(crate) exit_offer: bool,
/// The member this device's internet traffic goes through.
pub(crate) exit_via: Option<EndpointId>,
pub(crate) keys: NetworkKeys,
pub(crate) adapter: EndpointAdapter,
pub(crate) storage: Storage,
@@ -689,25 +686,24 @@ impl Runtime {
let _ = reply.send(());
}
NetCommand::SetExitOffer { enabled, reply } => {
self.params.exit_offer = enabled;
self.params
.routes
.update_exit(self.network_id, |exit| exit.offer = enabled);
self.apply_exit().await;
self.reannounce();
let _ = reply.send(());
}
NetCommand::SetExitVia { via, reply } => {
let outcome = match via {
Some(peer) if !self.exit_node_online(peer) => Err(
"that member is not connected or does not offer to be an exit node"
.to_string(),
),
_ => Ok(()),
};
if outcome.is_ok() {
self.params.exit_via = via;
self.exit_via_was_online = via.is_some();
NetCommand::SetExitVia {
via,
lifecycle,
reply,
} => {
let result = self.select_exit(via).await;
drop(lifecycle);
if result.is_ok() {
self.apply_exit().await;
}
let _ = reply.send(outcome);
let _ = reply.send(result);
}
NetCommand::Recheck => self.discovery_round().await,
NetCommand::Reannounce => {
@@ -907,7 +903,12 @@ impl Runtime {
/// the relays in use. Only hosts that cannot exempt the agent from an
/// exit node's default route read it.
async fn refresh_underlay(&self) {
let Some(interface) = self.params.interface.clone() else {
let Some(interface) = self
.params
.interface
.as_ref()
.filter(|i| i.needs_underlay_targets())
else {
return;
};
let mut addresses = Vec::new();
@@ -1308,7 +1309,7 @@ impl Runtime {
"the node is online; only offline history can be forgotten".into(),
));
}
if self.params.exit_via == Some(peer) {
if self.exit_policy().via == Some(peer) {
return Err(Error::ForgetNode(
"stop using this exit node before forgetting it".into(),
));
@@ -1770,20 +1771,36 @@ impl Runtime {
}
fn exit_policy(&self) -> crate::overlay::ExitPolicy {
crate::overlay::ExitPolicy {
offer: self.params.exit_offer,
via: self.params.exit_via,
self.params
.routes
.exit_settings(self.network_id)
.map(|(policy, _)| policy)
.unwrap_or_default()
}
async fn select_exit(&mut self, via: Option<EndpointId>) -> Result<()> {
if via.is_some_and(|peer| !self.exit_node_online(peer)) {
return Err(Error::Rejected {
reason: "that member is not connected or does not offer to be an exit node".into(),
});
}
self.params
.storage
.set_exit_via(self.network_id, via.map(|peer| *peer.as_bytes()))
.await?;
self.params
.routes
.update_exit(self.network_id, |exit| exit.via = via);
self.exit_via_was_online = via.is_some();
Ok(())
}
/// Whether this agent offers to be an exit node and can really be one.
fn exit_ready(&self) -> bool {
self.params.exit_offer
&& self
.params
.interface
.as_ref()
.is_some_and(|interface| interface.exit_offer_ready(self.network_id))
self.params
.interface
.as_ref()
.is_some_and(|interface| interface.exit_offer_ready(self.network_id))
}
/// Whether a member is connected and says it is an exit node.
@@ -1793,15 +1810,12 @@ impl Runtime {
.is_some_and(|session| session.exit_node)
}
/// Puts the exit settings into the routing table and the host rules.
/// Reconciles the host rules with the shared routing table.
///
/// The routes and the rules stay as they are when the exit node in use
/// goes away: dropping them would send the traffic out the old way, in
/// the clear, without anyone having asked.
async fn apply_exit(&mut self) {
self.params
.routes
.set_exit(self.network_id, self.exit_policy());
if let Some(interface) = self.params.interface.clone() {
interface.sync_exit_rules().await;
}
@@ -1814,7 +1828,7 @@ impl Runtime {
/// Says so, once, when the exit node in use is lost or comes back.
fn watch_exit_via(&mut self) {
let Some(via) = self.params.exit_via else {
let Some(via) = self.exit_policy().via else {
return;
};
let online = self.exit_node_online(via);
@@ -1865,7 +1879,7 @@ impl Runtime {
// a packet for ourselves does not go over a tunnel.
let local = self.state.address_of(&self.local_id);
let routes = crate::overlay::NetworkRoutes {
exit: self.exit_policy(),
exit: Default::default(),
broadcast: self.broadcast_policy(),
range: Some(range),
local,
@@ -1878,7 +1892,7 @@ impl Runtime {
if self
.params
.routes
.set_network(self.network_id, routes)
.update_allocations(self.network_id, routes)
.is_err()
{
self.range_blocked
@@ -2647,6 +2661,9 @@ impl Runtime {
let snapshot = snapshot_connection(&conn);
self.sessions.insert(peer, session);
// An exit may be selected as soon as the peer advertises it, before
// the next discovery tick. Keep this transport on its physical path.
self.refresh_underlay().await;
if let Some(worker) = &self.discovery_worker {
worker.set_connected(true);
}
@@ -3246,6 +3263,7 @@ impl Runtime {
members.sort_by(|a, b| a.endpoint_id.as_bytes().cmp(b.endpoint_id.as_bytes()));
members.dedup_by(|a, b| a.endpoint_id == b.endpoint_id);
let exit = self.exit_policy();
NetworkStatus {
vips: self.params.routes.vips.statuses(self.network_id),
protocols: self
@@ -3256,12 +3274,9 @@ impl Runtime {
.collect(),
auto_start: true,
broadcast: self.params.broadcast,
exit_offer: self.params.exit_offer,
exit_via: self.params.exit_via,
exit_via_online: self
.params
.exit_via
.is_some_and(|via| self.exit_node_online(via)),
exit_offer: exit.offer,
exit_via: exit.via,
exit_via_online: exit.via.is_some_and(|via| self.exit_node_online(via)),
exit_rules: self
.params
.interface