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

@@ -0,0 +1,52 @@
package cy.hexor.tsunagi
import android.content.Context
import android.content.ContextWrapper
import android.net.ConnectivityManager
import android.net.NetworkCapabilities
import android.net.VpnService
import android.os.ParcelFileDescriptor
import android.os.SystemClock
import androidx.test.platform.app.InstrumentationRegistry
import org.junit.Test
class VpnSourceTest {
private external fun checkSockets(fd: Int, networkCount: Int)
@Test fun udpAndPingReceiveFromBothNetworksAfterAddingTheSecond() {
System.loadLibrary("vpn_source_probe")
val context = InstrumentationRegistry.getInstrumentation().targetContext
check(VpnService.prepare(context) == null) { "Grant VPN consent on the test emulator first" }
val connectivity = context.getSystemService(ConnectivityManager::class.java)
// Android requires the manifest-declared service class. Attach it under the
// application UID without starting the agent and binding this process to the underlay.
val vpn = MeshVpn()
ContextWrapper::class.java.getDeclaredMethod("attachBaseContext", Context::class.java)
.apply { isAccessible = true }.invoke(vpn, context)
MeshVpn::class.java.getDeclaredField("upstream").apply { isAccessible = true }
.set(vpn, connectivity.activeNetwork)
val hosts = listOf("198.18.234.1/24", "198.19.234.1/24")
val routes = listOf("198.18.234.0/24", "198.19.234.0/24")
fun establish(count: Int): ParcelFileDescriptor {
val fd = vpn.establishTun(hosts.take(count).joinToString(","),
routes.take(count).joinToString(","), "", 1280, false)
check(fd >= 0) { "VPN establishment failed" }
return ParcelFileDescriptor.adoptFd(fd)
}
fun checkVpn(fd: ParcelFileDescriptor, count: Int) {
val deadline = SystemClock.elapsedRealtime() + 5000
while (true) {
val active = connectivity.activeNetwork
if (connectivity.getNetworkCapabilities(active)?.hasTransport(NetworkCapabilities.TRANSPORT_VPN) == true
&& connectivity.getLinkProperties(active)?.linkAddresses?.size == count) break
check(SystemClock.elapsedRealtime() < deadline) { "Timed out waiting for VPN routes" }
SystemClock.sleep(25)
}
checkSockets(ParcelFileDescriptor.dup(fd.fileDescriptor).detachFd(), count)
}
establish(1).use { first ->
checkVpn(first, 1)
establish(2).use { both -> checkVpn(both, 2) }
}
}
}