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.
344 lines
11 KiB
Rust
344 lines
11 KiB
Rust
//! A stable packet device over the file descriptors replaced by VpnService.
|
|
use crate::dns::{Resolver, stack::DnsPort};
|
|
use crate::plan::VpnPlan;
|
|
use bytes::Bytes;
|
|
use std::{
|
|
fmt,
|
|
fs::File,
|
|
io::{self, Read, Write},
|
|
sync::{Arc, Mutex as StdMutex},
|
|
};
|
|
use tokio::{
|
|
io::unix::AsyncFd,
|
|
sync::{Mutex, watch},
|
|
};
|
|
use tsunagi::{
|
|
BoxFuture,
|
|
overlay::{
|
|
OverlayError, TunDevice, TunFactory, TunRequest,
|
|
hostrules::{
|
|
BroadcastHostRules, BroadcastRulesPlan, BroadcastRulesReport, ExitHostPlan,
|
|
ExitHostReport, ExitHostRules, RuleOutcome,
|
|
},
|
|
source::{SourceFlows, SourceMode},
|
|
},
|
|
};
|
|
|
|
type Establish = dyn Fn(&VpnPlan) -> io::Result<File> + Send + Sync;
|
|
type BindSocket =
|
|
dyn Fn(&tokio::net::TcpSocket, std::net::Ipv4Addr) -> io::Result<()> + Send + Sync;
|
|
struct Attachment {
|
|
device: Arc<AsyncFd<File>>,
|
|
addresses: Vec<std::net::IpAddr>,
|
|
dns: DnsPort,
|
|
packets: StdMutex<Option<PacketState>>,
|
|
}
|
|
struct PacketState {
|
|
flows: SourceFlows,
|
|
// At most one reassembled IPv4 datagram's worth of remaining fragments.
|
|
pending: std::vec::IntoIter<Bytes>,
|
|
}
|
|
impl Attachment {
|
|
fn packets(&self) -> std::sync::MutexGuard<'_, Option<PacketState>> {
|
|
self.packets.lock().unwrap_or_else(|err| err.into_inner())
|
|
}
|
|
}
|
|
type Descriptor = Option<Arc<Attachment>>;
|
|
|
|
/// The only OS call is supplied by the Android service. Tokio owns the detached fd.
|
|
#[derive(Clone)]
|
|
pub struct AndroidTun(Arc<Inner>);
|
|
struct Inner {
|
|
establish: Box<Establish>,
|
|
bind: Box<BindSocket>,
|
|
resolver: Resolver,
|
|
plan: Mutex<VpnPlan>,
|
|
current: watch::Sender<Descriptor>,
|
|
}
|
|
|
|
impl fmt::Debug for AndroidTun {
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
f.write_str("AndroidTun")
|
|
}
|
|
}
|
|
impl AndroidTun {
|
|
/// Attach a nonblocking device returned by Builder.establish().
|
|
pub fn new(
|
|
resolver: Resolver,
|
|
establish: impl Fn(&VpnPlan) -> io::Result<File> + Send + Sync + 'static,
|
|
bind: impl Fn(&tokio::net::TcpSocket, std::net::Ipv4Addr) -> io::Result<()>
|
|
+ Send
|
|
+ Sync
|
|
+ 'static,
|
|
) -> Self {
|
|
Self(Arc::new(Inner {
|
|
establish: Box::new(establish),
|
|
bind: Box::new(bind),
|
|
resolver,
|
|
plan: Mutex::default(),
|
|
current: watch::channel(None).0,
|
|
}))
|
|
}
|
|
|
|
async fn update(&self, edit: impl FnOnce(&mut VpnPlan)) -> Result<(), OverlayError> {
|
|
let mut applied = self.0.plan.lock().await;
|
|
let mut next = applied.clone();
|
|
edit(&mut next);
|
|
if *applied == next {
|
|
return Ok(());
|
|
}
|
|
let dns_address = next.dns_address();
|
|
let device = if next.addresses.is_empty() {
|
|
None
|
|
} else {
|
|
let address = dns_address.ok_or_else(|| OverlayError::Unavailable(
|
|
"Mesh ranges overlap every device-local DNS address. Choose a narrower mesh range.".into()
|
|
))?;
|
|
let file = (self.0.establish)(&next)
|
|
.and_then(AsyncFd::new)
|
|
.map(Arc::new)
|
|
.map_err(|err| OverlayError::Unavailable(err.to_string()))?;
|
|
let dns = DnsPort::start(
|
|
address,
|
|
next.mtu as usize,
|
|
self.0.resolver.clone(),
|
|
file.clone(),
|
|
);
|
|
Some((file, dns))
|
|
};
|
|
// Only retire the old descriptor once establishing its replacement succeeds.
|
|
let previous = self
|
|
.0
|
|
.current
|
|
.borrow()
|
|
.clone()
|
|
.and_then(|fd| fd.packets().take());
|
|
let fd = device.map(|(file, dns)| {
|
|
let aliases = next.addresses_in_order();
|
|
let exit = match (next.full_tunnel, next.exit) {
|
|
(true, Some(std::net::IpAddr::V4(source))) => Some(source),
|
|
_ => None,
|
|
};
|
|
let flows = if let Some(mut previous) = previous {
|
|
previous.flows.reconfigure(&aliases, exit, next.mtu);
|
|
previous.flows
|
|
} else {
|
|
SourceFlows::new(&aliases, exit, next.mtu, SourceMode::Primary)
|
|
};
|
|
Arc::new(Attachment {
|
|
device: file,
|
|
addresses: next.addresses.iter().map(|a| a.addr).collect(),
|
|
dns,
|
|
packets: StdMutex::new(Some(PacketState {
|
|
flows,
|
|
pending: Vec::new().into_iter(),
|
|
})),
|
|
})
|
|
});
|
|
self.0.current.send_replace(fd);
|
|
self.0.resolver.publish(dns_address);
|
|
*applied = next;
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
struct Device {
|
|
name: String,
|
|
mtu: u32,
|
|
tun: AndroidTun,
|
|
}
|
|
|
|
impl TunDevice for Device {
|
|
fn name(&self) -> &str {
|
|
&self.name
|
|
}
|
|
fn mtu(&self) -> u32 {
|
|
self.mtu
|
|
}
|
|
fn recv(&self) -> BoxFuture<'_, Option<Bytes>> {
|
|
Box::pin(async move {
|
|
let mut rx = self.tun.0.current.subscribe();
|
|
let mut buffer = Vec::new();
|
|
loop {
|
|
let fd = rx.borrow_and_update().clone();
|
|
if let Some(fd) = fd {
|
|
if let Some(packet) = fd.packets().as_mut().and_then(|p| p.pending.next()) {
|
|
return Some(packet);
|
|
}
|
|
buffer.resize(self.mtu as usize + 1, 0);
|
|
let result = tokio::select! {
|
|
biased;
|
|
_ = rx.changed() => continue,
|
|
read = fd.device.async_io(tokio::io::Interest::READABLE, |mut file| file.read(&mut buffer)) => read,
|
|
};
|
|
if let Ok(len @ 1..) = result {
|
|
if fd.dns.intercept(&buffer[..len], &fd.addresses) {
|
|
continue;
|
|
}
|
|
buffer.truncate(len);
|
|
if let Some(state) = fd.packets().as_mut() {
|
|
let frames = state.flows.outbound(std::mem::take(&mut buffer).into());
|
|
state.pending = frames.into_iter();
|
|
if let Some(packet) = state.pending.next() {
|
|
return Some(packet);
|
|
}
|
|
}
|
|
continue;
|
|
}
|
|
}
|
|
// No networks yet, last one left, or Android revoked this fd.
|
|
// A later establish wakes the same reader on the new device.
|
|
rx.changed().await.ok()?;
|
|
}
|
|
})
|
|
}
|
|
fn send<'a>(&'a self, packet: Bytes) -> BoxFuture<'a, Result<(), OverlayError>> {
|
|
Box::pin(async move {
|
|
let mut rx = self.tun.0.current.subscribe();
|
|
let fd = rx
|
|
.borrow_and_update()
|
|
.clone()
|
|
.ok_or_else(|| OverlayError::Unavailable("VPN has no addresses".into()))?;
|
|
let packets = fd
|
|
.packets()
|
|
.as_mut()
|
|
.ok_or_else(|| OverlayError::Unavailable("VPN was reconfigured".into()))?
|
|
.flows
|
|
.inbound(packet);
|
|
for packet in packets {
|
|
let len = tokio::select! {
|
|
biased;
|
|
_ = rx.changed() => return Err(OverlayError::Unavailable("VPN was reconfigured".into())),
|
|
write = fd.device.async_io(tokio::io::Interest::WRITABLE, |mut file| file.write(&packet)) =>
|
|
write.map_err(|e| OverlayError::Other(e.to_string()))?,
|
|
};
|
|
if len != packet.len() {
|
|
return Err(OverlayError::Other("short VPN datagram write".into()));
|
|
}
|
|
}
|
|
Ok(())
|
|
})
|
|
}
|
|
}
|
|
|
|
impl TunFactory for AndroidTun {
|
|
fn name(&self) -> &str {
|
|
"Android VpnService"
|
|
}
|
|
fn bind_overlay_socket(
|
|
&self,
|
|
socket: &tokio::net::TcpSocket,
|
|
source: std::net::Ipv4Addr,
|
|
) -> io::Result<()> {
|
|
(self.0.bind)(socket, source)?;
|
|
socket.bind((source, 0).into())
|
|
}
|
|
fn assigned_addresses(&self) -> Vec<std::net::IpAddr> {
|
|
self.0
|
|
.current
|
|
.borrow()
|
|
.as_ref()
|
|
.map(|fd| fd.addresses.clone())
|
|
.unwrap_or_default()
|
|
}
|
|
fn on_host(&self) -> bool {
|
|
true
|
|
}
|
|
fn create<'a>(
|
|
&'a self,
|
|
req: TunRequest,
|
|
) -> BoxFuture<'a, Result<Arc<dyn TunDevice>, OverlayError>> {
|
|
Box::pin(async move {
|
|
let dev = Device {
|
|
name: req.name.clone(),
|
|
mtu: req.mtu,
|
|
tun: self.clone(),
|
|
};
|
|
self.reconfigure(req).await?;
|
|
Ok(Arc::new(dev) as Arc<dyn TunDevice>)
|
|
})
|
|
}
|
|
fn reconfigure<'a>(&'a self, req: TunRequest) -> BoxFuture<'a, Result<(), OverlayError>> {
|
|
Box::pin(self.update(move |plan| {
|
|
plan.addresses = req.addresses;
|
|
plan.mtu = req.mtu;
|
|
}))
|
|
}
|
|
fn host_rules(&self) -> Option<Arc<dyn BroadcastHostRules>> {
|
|
Some(Arc::new(self.clone()))
|
|
}
|
|
fn destroy<'a>(&'a self, _: &'a str) -> BoxFuture<'a, ()> {
|
|
Box::pin(async {
|
|
let _ = self.update(|plan| *plan = VpnPlan::default()).await;
|
|
})
|
|
}
|
|
}
|
|
|
|
fn outcome(result: Result<(), OverlayError>) -> RuleOutcome {
|
|
match result {
|
|
Ok(()) => RuleOutcome::Applied,
|
|
Err(error) => RuleOutcome::Failed(error.to_string()),
|
|
}
|
|
}
|
|
impl BroadcastHostRules for AndroidTun {
|
|
fn name(&self) -> &str {
|
|
"Android VpnService"
|
|
}
|
|
fn apply<'a>(&'a self, plan: &'a BroadcastRulesPlan) -> BoxFuture<'a, BroadcastRulesReport> {
|
|
Box::pin(async move {
|
|
let route = outcome(self.update(|p| p.broadcast = true).await);
|
|
BroadcastRulesReport {
|
|
plan: plan.clone(),
|
|
route,
|
|
// Android delivers accepted traffic directly through the VPN fd.
|
|
firewall: RuleOutcome::Applied,
|
|
}
|
|
})
|
|
}
|
|
fn clear<'a>(&'a self, _: &'a str) -> BoxFuture<'a, ()> {
|
|
Box::pin(async {
|
|
let _ = self.update(|p| p.broadcast = false).await;
|
|
})
|
|
}
|
|
fn exit_rules(&self) -> Option<&dyn ExitHostRules> {
|
|
Some(self)
|
|
}
|
|
}
|
|
impl ExitHostRules for AndroidTun {
|
|
fn apply<'a>(&'a self, plan: &'a ExitHostPlan) -> BoxFuture<'a, ExitHostReport> {
|
|
Box::pin(async move {
|
|
let result = self
|
|
.update(|p| {
|
|
p.full_tunnel = plan.client;
|
|
p.exit = plan.client_source.map(Into::into);
|
|
})
|
|
.await;
|
|
ExitHostReport {
|
|
offer: plan
|
|
.offer
|
|
.iter()
|
|
.map(|range| {
|
|
(
|
|
*range,
|
|
RuleOutcome::Failed("Android supports exit clients only".into()),
|
|
)
|
|
})
|
|
.collect(),
|
|
forwarding: None,
|
|
client: plan.client.then(|| outcome(result)),
|
|
}
|
|
})
|
|
}
|
|
fn clear<'a>(&'a self, _: &'a str) -> BoxFuture<'a, ()> {
|
|
Box::pin(async {
|
|
let _ = self
|
|
.update(|p| {
|
|
p.full_tunnel = false;
|
|
p.exit = None;
|
|
})
|
|
.await;
|
|
})
|
|
}
|
|
}
|