Restore the socket source address across Android mesh networks

Android VpnService routes can select the primary VPN address for traffic to a second subnet even when both aliases are installed. Keep a bounded binding at the local TUN boundary so the peer receives the source it owns in that network and the original Android socket receives the reply. Preserve native primary traffic, separate per-subnet capacity, handle fragmented IPv4 and quoted ICMP errors, and retain eligible bindings when VpnService replaces its descriptor.

Support TCP, UDP and ICMP echo. An ambiguous tuple retains its original binding. Explicitly bound traffic for other IPv4 protocols passes using the proper network address. Mesh authentication, address admission and wire formats stay unchanged.

Reproduced the wrong source on a disposable Android API 37 ARM64 emulator. After the fix, system ping and an unbound connected UDP socket both received replies across each real VPN subnet. Built both debug APKs and confirmed that only the instrumentation APK includes its helper. Eleven focused Rust checks cover reverse mapping, checksums, fragments, limits and reconfiguration.
This commit is contained in:
Aleksandr Bogomiakov committed 2026-11-25 00:03:02 +02:00
1 parent 2e83483f00
commit 972b98363d
11 files changed
+1224 -20

No files matched your search

+6 -2
View File
@@ -273,15 +273,19 @@ struct Datagram {
bytes: usize,
}
type FragmentKey = (Option<[u8; 32]>, Ipv4Addr, Ipv4Addr, u16, u8);
/// Bounded IPv4 fragment assembly for a local packet consumer.
#[derive(Debug, Default)]
pub(crate) struct Reassembly {
pub struct Reassembly {
pending: HashMap<FragmentKey, Datagram>,
}
impl Reassembly {
/// Forget incomplete datagrams after their five-second lifetime.
pub fn expire(&mut self, now: Instant) {
self.pending
.retain(|_, d| now.duration_since(d.at) < Duration::from_secs(5));
}
/// Accept one fragment or complete packet. `max` bounds pending datagrams;
/// `scope` separates authenticated senders sharing an address domain.
pub fn accept(&mut self, p: Bytes, max: usize, scope: Option<[u8; 32]>) -> Option<Bytes> {
let n = header(&p)?;
let flags = u16::from_be_bytes([p[6], p[7]]);
@@ -363,7 +367,7 @@ impl Reassembly {
}
/// Re-fragments an assembled NAT datagram for the unchanged overlay MTU.
pub(crate) fn fragments(packet: Bytes, mtu: u32) -> Vec<Bytes> {
pub fn fragments(packet: Bytes, mtu: u32) -> Vec<Bytes> {
if packet.len() <= mtu as usize {
return vec![packet];
}