Skip to content

A rootless Android app that boots Alpine Linux: run containers (Podman/Docker/LXC) and GUI desktop apps.

feat(avf): forward udp rules over vsock instead of dropping them

ExTv48b840c57e56
diff --git a/app/src/main/java/com/excp/podroid/engine/avf/AvfEngine.kt b/app/src/main/java/com/excp/podroid/engine/avf/AvfEngine.kt
index cc2ac03..de39d2a 100644
--- a/app/src/main/java/com/excp/podroid/engine/avf/AvfEngine.kt
+++ b/app/src/main/java/com/excp/podroid/engine/avf/AvfEngine.kt
@@ -164,8 +164,8 @@ class AvfEngine @Inject constructor(
* of DataStore rules — whichever path wins owns the forwarder.
*/
private val initialRules = mutableListOf<com.excp.podroid.data.repository.PortForwardRule>()
- /** vport → forwarder. Written via addPortForward/removePortForward only. */
- private val forwarders = java.util.concurrent.ConcurrentHashMap<Int, VsockPortForwarder>()
+ /** vsock port → forwarder. Written via addPortForward/removePortForward only. */
+ private val forwarders = java.util.concurrent.ConcurrentHashMap<Int, Forwarder>()
@Volatile private var lastSentRows = -1
@Volatile private var lastSentCols = -1
@@ -236,11 +236,7 @@ class AvfEngine @Inject constructor(
synchronized(consoleLock) { consoleBuilder.clear() }
_consoleText.value = ""
initialRules.clear()
- initialRules.addAll(portForwards.filter { it.protocol == "tcp" })
- if (portForwards.any { it.protocol != "tcp" }) {
- Log.w(TAG, "AVF supports TCP only; UDP/non-TCP rules ignored: " +
- "${portForwards.filter { it.protocol != "tcp" }}")
- }
+ initialRules.addAll(portForwards)
_state.value = VmState.Starting
_bootStage.value = "Initializing AVF..."
// Re-arm the one-shot detector so a Stop → Start cycle's second boot
@@ -472,68 +468,52 @@ class AvfEngine @Inject constructor(
override suspend fun addPortForward(rule: com.excp.podroid.data.repository.PortForwardRule) {
if (_state.value !is VmState.Running) return
val vm = vmHandle ?: return
- if (rule.protocol != "tcp") {
- Log.w(TAG, "addPortForward($rule) — AVF supports TCP only; rule ignored")
- return
- }
+ // TCP keeps vport == hostPort (unchanged); UDP is offset so a TCP and a
+ // UDP rule on the same host port don't collide on the vsock port space.
+ val vport = AvfVport.forRule(rule)
try {
- // Convention: vsock port == hostPort. The guest agent listens on
- // vsock:hostPort and splices to tcp 127.0.0.1:guestPort. Using
- // hostPort (which is unique per Android-side rule) instead of
- // guestPort avoids vsock-port collisions when two rules target
- // the same service inside the VM (e.g. 8080→80 + 9090→80).
- val fw = VsockPortForwarder(
- hostPort = rule.hostPort,
- guestVsockPort = rule.hostPort,
- vm = vm,
- scope = scope,
- )
- // putIfAbsent races safely with another concurrent addPortForward
- // for the same hostPort — only one forwarder wins; the loser closes
- // immediately so we don't leak a bound ServerSocket.
- val existing = forwarders.putIfAbsent(rule.hostPort, fw)
+ val fw: Forwarder = if (rule.protocol == "udp") {
+ VsockUdpForwarder(hostPort = rule.hostPort, guestVsockPort = vport, vm = vm, scope = scope)
+ } else {
+ VsockPortForwarder(hostPort = rule.hostPort, guestVsockPort = vport, vm = vm, scope = scope)
+ }
+ // putIfAbsent races safely with a concurrent addPortForward for the
+ // same vport — only one forwarder wins; the loser is closed here. fw
+ // hasn't been started yet, so close() is a cheap no-op, but it keeps
+ // this correct if start() ever moves above putIfAbsent.
+ val existing = forwarders.putIfAbsent(vport, fw)
if (existing != null) {
- Log.d(TAG, "addPortForward($rule) — forwarder already exists for ${rule.hostPort}")
+ Log.d(TAG, "addPortForward($rule) — forwarder already exists for vsock:$vport")
+ runCatching { fw.close() }
return
}
- // Re-check Running state before .start() so a concurrent cleanup()
- // doesn't orphan us. If state has slipped, drop the freshly-mapped
- // forwarder and don't bind the ServerSocket.
+ // Re-check Running before start() so a concurrent cleanup() doesn't
+ // orphan us. If state slipped, drop the freshly-mapped forwarder.
if (_state.value !is VmState.Running) {
- forwarders.remove(rule.hostPort)
+ forwarders.remove(vport)
return
}
fw.start()
val ctl = control
if (ctl != null) {
- ctl.addForward(rule.hostPort, "127.0.0.1", rule.guestPort)
+ ctl.addForward(vport, rule.protocol, "127.0.0.1", rule.guestPort)
} else {
- // EngineHolder's diff loop can call addPortForward the instant
- // state flips to Running, a hair before bringUpControlChannel
- // sets `control`. The host listener is already bound; the guest
- // ADD is what's at risk here. Surface it rather than dropping
- // silently. The auto-injected rules are replayed by
- // bringUpControlChannel (which sets `control` before the replay,
- // so their ADDs land); a DataStore rule that lands in this narrow
- // window is re-dispatched by EngineHolder on the next sync.
- Log.w(TAG, "addPortForward(${rule.hostPort}): control channel not up yet; " +
+ Log.w(TAG, "addPortForward(${rule.hostPort}/${rule.protocol}): control channel not up yet; " +
"guest ADD skipped (host listener is bound)")
}
- Log.i(TAG, "live forward up: 0.0.0.0:${rule.hostPort} → vsock:${rule.hostPort} → 127.0.0.1:${rule.guestPort}")
+ Log.i(TAG, "live forward up: 0.0.0.0:${rule.hostPort}/${rule.protocol} → vsock:$vport → 127.0.0.1:${rule.guestPort}")
} catch (e: Throwable) {
- // If we got past putIfAbsent before the throw, remove ourselves so
- // cleanup() doesn't try to close a half-built forwarder.
- forwarders.remove(rule.hostPort)
+ forwarders.remove(vport)
Log.w(TAG, "addPortForward($rule) failed", e)
}
}
override suspend fun removePortForward(rule: com.excp.podroid.data.repository.PortForwardRule) {
- if (rule.protocol != "tcp") return
- val fw = forwarders.remove(rule.hostPort) ?: return
+ val vport = AvfVport.forRule(rule)
+ val fw = forwarders.remove(vport) ?: return
runCatching { fw.close() }
- runCatching { control?.removeForward(rule.hostPort) }
- Log.i(TAG, "live forward down: 0.0.0.0:${rule.hostPort}")
+ runCatching { control?.removeForward(vport) }
+ Log.i(TAG, "live forward down: 0.0.0.0:${rule.hostPort}/${rule.protocol}")
}
/**
diff --git a/app/src/main/java/com/excp/podroid/engine/avf/VsockControlChannel.kt b/app/src/main/java/com/excp/podroid/engine/avf/VsockControlChannel.kt
index 147af53..b35c730 100644
--- a/app/src/main/java/com/excp/podroid/engine/avf/VsockControlChannel.kt
+++ b/app/src/main/java/com/excp/podroid/engine/avf/VsockControlChannel.kt
@@ -9,7 +9,7 @@
*
* Wire format (LF-terminated ASCII):
* RESIZE <rows> <cols>
- * ADD <vport> tcp <host> <gport>
+ * ADD <vport> <tcp|udp> <host> <gport>
* REMOVE <vport>
* PING → agent replies "PONG\n" (we ignore the reply)
* SYNC → agent runs sync(2), replies "SYNCED\n"
@@ -170,7 +170,7 @@ class VsockControlChannel(
}
fun sendResize(rows: Int, cols: Int) = sendOrQueue("RESIZE $rows $cols")
- fun addForward(vport: Int, host: String, gport: Int) = sendOrQueue("ADD $vport tcp $host $gport")
+ fun addForward(vport: Int, proto: String, host: String, gport: Int) = sendOrQueue("ADD $vport $proto $host $gport")
fun removeForward(vport: Int) = sendOrQueue("REMOVE $vport")
/**
diff --git a/app/src/main/java/com/excp/podroid/engine/avf/VsockUdpForwarder.kt b/app/src/main/java/com/excp/podroid/engine/avf/VsockUdpForwarder.kt
new file mode 100644
index 0000000..67dd408
--- /dev/null
+++ b/app/src/main/java/com/excp/podroid/engine/avf/VsockUdpForwarder.kt
@@ -0,0 +1,200 @@
+/*
+ * Podroid - Rootless Podman for Android
+ * Copyright (C) 2024-2026 Podroid contributors
+ *
+ * Per-rule UDP forwarder for the AVF backend. Binds 0.0.0.0:hostPort as a
+ * DatagramSocket and, per distinct client source address ("flow"), opens one
+ * vsock stream to the guest agent's udp listener. Datagrams are length-framed
+ * (DatagramFraming) over the stream; the guest relays them to a guest-local UDP
+ * socket. Flows are reaped after idle and capped to bound the blast radius from
+ * spoofed-source UDP floods (UDP source addresses are trivially forged on a LAN).
+ *
+ * Mirrors VsockPortForwarder's resource discipline: SupervisorJob parent for
+ * per-flow coroutines, force-close the socket in close() to unblock receive(),
+ * and AutoClose streams that each own exactly one fd (pfd / its dup).
+ */
+package com.excp.podroid.engine.avf
+
+import android.os.Build
+import android.os.ParcelFileDescriptor
+import android.util.Log
+import androidx.annotation.RequiresApi
+import kotlinx.coroutines.CoroutineScope
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.Job
+import kotlinx.coroutines.SupervisorJob
+import kotlinx.coroutines.delay
+import kotlinx.coroutines.launch
+import java.io.IOException
+import java.io.InputStream
+import java.io.OutputStream
+import java.net.DatagramPacket
+import java.net.DatagramSocket
+import java.net.InetAddress
+import java.net.InetSocketAddress
+import java.net.SocketAddress
+import java.net.SocketException
+
+@RequiresApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
+class VsockUdpForwarder(
+ private val hostPort: Int,
+ private val guestVsockPort: Int,
+ private val vm: Any,
+ private val scope: CoroutineScope,
+) : Forwarder {
+ companion object {
+ private const val TAG = "VsockUdpForwarder"
+ private const val CONNECT_ATTEMPTS = 6
+ private const val CONNECT_RETRY_MS = 250L
+ // Bounds fds/coroutines/guest processes against a spoofed-source flood.
+ private const val MAX_FLOWS = 128
+ // A flow idle this long is reaped (closing it makes the guest child EOF).
+ private const val IDLE_MS = 60_000L
+ private const val REAP_INTERVAL_MS = 15_000L
+ private const val UDP_MAX = 65535
+ }
+
+ private class Flow(
+ val out: OutputStream,
+ val input: InputStream,
+ ) {
+ lateinit var readerJob: Job
+ @Volatile var lastActivityMs = System.currentTimeMillis()
+ }
+
+ private var socket: DatagramSocket? = null
+ private val flows = java.util.concurrent.ConcurrentHashMap<SocketAddress, Flow>()
+ private val children = SupervisorJob()
+ private var recvJob: Job? = null
+ private var reaperJob: Job? = null
+ @Volatile private var closed = false
+
+ override fun start() {
+ val s = DatagramSocket(null)
+ s.reuseAddress = true
+ try {
+ s.bind(InetSocketAddress(InetAddress.getByName("0.0.0.0"), hostPort))
+ } catch (e: Throwable) {
+ runCatching { s.close() }
+ throw e
+ }
+ socket = s
+ Log.d(TAG, "listening udp 0.0.0.0:$hostPort → vsock:$guestVsockPort")
+ recvJob = scope.launch(Dispatchers.IO + children) { receiveLoop(s) }
+ reaperJob = scope.launch(Dispatchers.IO + children) { reapLoop() }
+ }
+
+ private suspend fun receiveLoop(s: DatagramSocket) {
+ val buf = ByteArray(UDP_MAX)
+ while (!closed) {
+ val pkt = DatagramPacket(buf, buf.size)
+ try {
+ s.receive(pkt)
+ } catch (_: SocketException) {
+ break // socket closed by close()
+ } catch (_: IOException) {
+ continue
+ }
+ val src = pkt.socketAddress
+ val payload = pkt.data.copyOfRange(pkt.offset, pkt.offset + pkt.length)
+ val flow = flows[src] ?: openFlow(src, s) ?: continue
+ flow.lastActivityMs = System.currentTimeMillis()
+ try {
+ flow.out.write(DatagramFraming.encode(payload, payload.size))
+ flow.out.flush()
+ } catch (_: IOException) {
+ closeFlow(src)
+ }
+ }
+ }
+
+ /** Opens a vsock flow for [src]. Returns null if capped or the connect failed. */
+ private suspend fun openFlow(src: SocketAddress, s: DatagramSocket): Flow? {
+ if (flows.size >= MAX_FLOWS) {
+ Log.w(TAG, "flow cap ($MAX_FLOWS) reached on udp:$hostPort; dropping $src")
+ return null
+ }
+ val pfd = connectVsockWithRetry() ?: return null
+ val pfdOut = runCatching { pfd.dup() }.getOrNull()
+ if (pfdOut == null) {
+ runCatching { pfd.close() }
+ return null
+ }
+ // read side owns `pfd`; write side owns its dup. Each AutoClose stream
+ // closes exactly one fd.
+ val input = ParcelFileDescriptor.AutoCloseInputStream(pfd)
+ val out = ParcelFileDescriptor.AutoCloseOutputStream(pfdOut)
+ val flow = Flow(out, input)
+ flow.readerJob = scope.launch(Dispatchers.IO + children) {
+ try {
+ while (!closed) {
+ val reply = DatagramFraming.readFrame(input) ?: break
+ flows[src]?.lastActivityMs = System.currentTimeMillis()
+ runCatching { s.send(DatagramPacket(reply, reply.size, src)) }
+ }
+ } catch (_: IOException) {
+ // guest closed the flow / torn down — normal
+ } finally {
+ // Remove + close only if we still own the map entry, so a
+ // cancelled loser-reader (lost putIfAbsent) can never evict the
+ // winner's flow. closeFlow() is the authoritative key-based path.
+ if (flows.remove(src, flow)) {
+ runCatching { out.close() }
+ runCatching { input.close() }
+ }
+ }
+ }
+ val existing = flows.putIfAbsent(src, flow)
+ if (existing != null) {
+ // Lost the race; tear ours down. The reader's finally will no-op its
+ // value-matched remove (winner owns the entry), so close here.
+ flow.readerJob.cancel()
+ runCatching { out.close() }
+ runCatching { input.close() }
+ return existing
+ }
+ return flow
+ }
+
+ private suspend fun connectVsockWithRetry(): ParcelFileDescriptor? {
+ var lastCause: Throwable? = null
+ repeat(CONNECT_ATTEMPTS) { attempt ->
+ if (closed) return null
+ val pfd = runCatching { AvfReflect.connectVsock(vm, guestVsockPort.toLong()) }
+ .getOrElse { lastCause = it.cause ?: it; null }
+ if (pfd != null) return pfd
+ if (attempt < CONNECT_ATTEMPTS - 1) delay(CONNECT_RETRY_MS)
+ }
+ val cause = lastCause
+ Log.w(TAG, "connectVsock($guestVsockPort) failed after $CONNECT_ATTEMPTS attempts: " +
+ "${cause?.javaClass?.simpleName}: ${cause?.message ?: "(no message)"}")
+ return null
+ }
+
+ private fun closeFlow(src: SocketAddress) {
+ val flow = flows.remove(src) ?: return
+ flow.readerJob.cancel()
+ runCatching { flow.out.close() }
+ runCatching { flow.input.close() }
+ }
+
+ private suspend fun reapLoop() {
+ while (!closed) {
+ delay(REAP_INTERVAL_MS)
+ val now = System.currentTimeMillis()
+ flows.entries
+ .filter { now - it.value.lastActivityMs > IDLE_MS }
+ .forEach { closeFlow(it.key) }
+ }
+ }
+
+ override fun close() {
+ if (closed) return
+ closed = true
+ runCatching { socket?.close() } // unblocks receive()
+ runCatching { recvJob?.cancel() }
+ runCatching { reaperJob?.cancel() }
+ flows.keys.toList().forEach { closeFlow(it) }
+ runCatching { children.cancel() }
+ }
+}