* [PATCH 1/2] udp: Don't share a preserved source port with other sockets
2026-12-01 23:52 [PATCH 0/2] udp: Don't share preserved source ports with foreign sockets Jon Maloy
@ 2026-12-01 23:52 ` Jon Maloy
2026-12-01 23:52 ` [PATCH 2/2] udp: Keep sharing source ports we forward ourselves Jon Maloy
1 sibling, 0 replies; 4+ messages in thread
From: Jon Maloy @ 2026-12-01 23:52 UTC (permalink / raw)
To: sbrivio, jmaloy, passt-dev; +Cc: Gerwim Feiken
From: Gerwim Feiken <info@gerwim.nl>
For a UDP flow initiated from the guest, passt keeps the guest's
source port on the host side (fwd_nat_from_tap()) and binds the flow
socket with SO_REUSEADDR (sock_l4_()). Several passt instances in one
network namespace -- one per VM, as a VM manager spawning passt from
one daemon does -- can then each bind the same port towards the same
destination, typically the resolver behind --dns-forward. Their
sockets share one identical 4-tuple, and the kernel delivers each
reply to only one of them: the other guest's queries go unanswered
until the flow expires, and the guest owning the receiving socket gets
answers to queries it never sent.
Bind the host side socket of a flow initiated from the tap interface
without SO_REUSEADDR, so that the bind fails if any other socket holds
the port, and fall back to a port chosen by the kernel in that case.
udp_flow_new() reads the actual port back with getsockname(), as it
already does for the address. Listening sockets, and flows initiated
from them, keep SO_REUSEADDR.
Signed-off-by: Gerwim Feiken <info@gerwim.nl>
[jmaloy: Don't pass 'exclusive' through the PIF_SPLICE path in
flowside_sock_l4(): flows initiated from the tap interface never
target it, assert that instead]
Signed-off-by: Jon Maloy <jmaloy@redhat.com>
---
flow.c | 14 +++++++++++---
flow.h | 2 +-
icmp.c | 2 +-
pif.c | 2 +-
udp_flow.c | 28 ++++++++++++++++++++++++----
util.c | 15 ++++++++++-----
util.h | 3 ++-
7 files changed, 50 insertions(+), 16 deletions(-)
diff --git a/flow.c b/flow.c
index f9fbaefe..91cabfc9 100644
--- a/flow.c
+++ b/flow.c
@@ -197,7 +197,7 @@ static int flowside_sock_splice(void *arg)
ns_enter(a->c);
- a->fd = sock_l4(a->c, a->type, a->sa, NULL);
+ a->fd = sock_l4(a->c, a->type, a->sa, NULL, false);
a->err = errno;
return 0;
@@ -208,12 +208,13 @@ static int flowside_sock_splice(void *arg)
* @type: Socket epoll type
* @pif: Interface for this socket
* @side: Flowside to create a socket for
+ * @exclusive: Don't set SO_REUSEADDR (see sock_l4_()), PIF_HOST only
*
* Return: socket fd of protocol @proto bound to our address and port from @side
* (if specified).
*/
int flowside_sock_l4(const struct ctx *c, enum epoll_type type, uint8_t pif,
- const struct flowside *side)
+ const struct flowside *side, bool exclusive)
{
const char *ifname = NULL;
union sockaddr_inany sa;
@@ -231,12 +232,19 @@ int flowside_sock_l4(const struct ctx *c, enum epoll_type type, uint8_t pif,
else if (sa.sa_family == AF_INET6)
ifname = c->ip6.ifname_out;
- return sock_l4(c, type, &sa, ifname);
+ return sock_l4(c, type, &sa, ifname, exclusive);
case PIF_SPLICE: {
struct flowside_sock_args args = {
.c = c, .type = type, .sa = &sa,
};
+
+ /* Exclusive binds are only for host-side sockets of flows
+ * initiated from the tap interface, which never target the
+ * namespace
+ */
+ assert(!exclusive);
+
NS_CALL(flowside_sock_splice, &args);
errno = args.err;
return args.fd;
diff --git a/flow.h b/flow.h
index db028455..3b37ac36 100644
--- a/flow.h
+++ b/flow.h
@@ -173,7 +173,7 @@ static inline bool flowside_eq(const struct flowside *left,
}
int flowside_sock_l4(const struct ctx *c, enum epoll_type type, uint8_t pif,
- const struct flowside *side);
+ const struct flowside *side, bool exclusive);
int flowside_connect(const struct ctx *c, int s,
uint8_t pif, const struct flowside *tgt);
diff --git a/icmp.c b/icmp.c
index 7045d5fb..8f44bdcb 100644
--- a/icmp.c
+++ b/icmp.c
@@ -205,7 +205,7 @@ static struct icmp_ping_flow *icmp_ping_new(const struct ctx *c,
pingf->seq = -1;
- pingf->sock = flowside_sock_l4(c, EPOLL_TYPE_PING, PIF_HOST, tgt);
+ pingf->sock = flowside_sock_l4(c, EPOLL_TYPE_PING, PIF_HOST, tgt, false);
if (pingf->sock < 0) {
warn("Cannot open \"ping\" socket. You might need to:");
diff --git a/pif.c b/pif.c
index 8ade587c..d0288191 100644
--- a/pif.c
+++ b/pif.c
@@ -114,7 +114,7 @@ int pif_listen(const struct ctx *c, uint8_t proto, uint8_t pif,
union sockaddr_inany sa;
pif_sockaddr(c, &sa, pif, addr, port);
- ref.fd = sock_l4(c, type, &sa, ifname);
+ ref.fd = sock_l4(c, type, &sa, ifname, false);
}
if (ref.fd < 0)
diff --git a/udp_flow.c b/udp_flow.c
index 865702ce..a9fab7ae 100644
--- a/udp_flow.c
+++ b/udp_flow.c
@@ -74,10 +74,26 @@ static int udp_flow_sock(const struct ctx *c,
{
const struct flowside *side = &uflow->f.side[sidei];
uint8_t pif = uflow->f.pif[sidei];
+ /* A guest-initiated flow keeps the guest's source port on the host
+ * side. Bind it exclusively: without SO_REUSEADDR the bind fails if
+ * any other socket in this network namespace holds the port, such as
+ * another passt instance's flow towards the same destination (typically
+ * the same DNS resolver). Two sockets with an identical 4-tuple would
+ * steal each other's replies. In that case let the kernel pick a free
+ * port instead, and udp_flow_new() records it. Listening sockets and
+ * the flows they initiate keep SO_REUSEADDR, as they must share ports.
+ */
+ bool exclusive = sidei == TGTSIDE && uflow->f.pif[INISIDE] == PIF_TAP;
int rc;
int s;
- s = flowside_sock_l4(c, EPOLL_TYPE_UDP, pif, side);
+ s = flowside_sock_l4(c, EPOLL_TYPE_UDP, pif, side, exclusive);
+ if (s == -EADDRINUSE && exclusive && side->oport) {
+ flow_dbg(uflow, "Host port %hu in use, using an ephemeral port",
+ side->oport);
+ uflow->f.side[sidei].oport = 0;
+ s = flowside_sock_l4(c, EPOLL_TYPE_UDP, pif, side, exclusive);
+ }
if (s < 0) {
flow_perror_ratelimit(uflow, now,
"Couldn't open flow specific socket");
@@ -162,13 +178,15 @@ static flow_sidx_t udp_flow_new(const struct ctx *c, union flow *flow,
goto cancel;
}
- if (uflow->s[TGTSIDE] >= 0 && inany_is_unspecified(&tgt->oaddr)) {
+ if (uflow->s[TGTSIDE] >= 0 &&
+ (inany_is_unspecified(&tgt->oaddr) || !tgt->oport)) {
/* When we target a socket, we connect() it, but might not
* always bind(), leaving the kernel to pick our address. In
* that case connect() will implicitly bind() the socket, but we
* need to determine its local address so that we can match
* reply packets back to the correct flow. Update the flow with
- * the information from getsockname() */
+ * the information from getsockname(). The same applies to the
+ * port if udp_flow_sock() had to let the kernel pick one. */
union sockaddr_inany sa;
socklen_t sl = sizeof(sa);
in_port_t port;
@@ -179,7 +197,9 @@ static flow_sidx_t udp_flow_new(const struct ctx *c, union flow *flow,
flow_perror(uflow, "Unable to determine local address");
goto cancel;
}
- if (port != tgt->oport) {
+ if (!tgt->oport) {
+ uflow->f.side[TGTSIDE].oport = port;
+ } else if (port != tgt->oport) {
flow_err_ratelimit(uflow, now, "Unexpected local port");
goto cancel;
}
diff --git a/util.c b/util.c
index ff1f19f4..053dd801 100644
--- a/util.c
+++ b/util.c
@@ -47,12 +47,14 @@ uint8_t eth_pad[ETH_ZLEN] = { 0 };
* @sa: Socket address to bind to
* @ifname: Interface for binding, NULL for any
* @v6only: If >= 0, set IPV6_V6ONLY socket option to this value
+ * @exclusive: Don't set SO_REUSEADDR, so that binding a port already bound
+ * by any other socket fails with EADDRINUSE
*
* Return: newly created socket, negative error code on failure
*/
static int sock_l4_(const struct ctx *c, enum epoll_type type,
const union sockaddr_inany *sa, const char *ifname,
- int v6only)
+ int v6only, bool exclusive)
{
sa_family_t af = sa->sa_family;
bool freebind = false;
@@ -104,7 +106,8 @@ static int sock_l4_(const struct ctx *c, enum epoll_type type,
}
}
- if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &y, sizeof(y)))
+ if (!exclusive &&
+ setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &y, sizeof(y)))
debug("Failed to set SO_REUSEADDR on socket %i", fd);
if (proto == IPPROTO_UDP) {
@@ -173,11 +176,13 @@ static int sock_l4_(const struct ctx *c, enum epoll_type type,
* @type: epoll type
* @sa: Socket address to bind to
* @ifname: Interface for binding, NULL for any
+ * @exclusive: Don't set SO_REUSEADDR (see sock_l4_())
*
* Return: newly created socket, negative error code on failure
*/
int sock_l4(const struct ctx *c, enum epoll_type type,
- const union sockaddr_inany *sa, const char *ifname)
+ const union sockaddr_inany *sa, const char *ifname,
+ bool exclusive)
{
int v6only = -1;
@@ -188,7 +193,7 @@ int sock_l4(const struct ctx *c, enum epoll_type type,
IN6_IS_ADDR_UNSPECIFIED(&sa->sa6.sin6_addr))
v6only = 1;
- return sock_l4_(c, type, sa, ifname, v6only);
+ return sock_l4_(c, type, sa, ifname, v6only, exclusive);
}
/**
@@ -215,7 +220,7 @@ int sock_l4_dualstack_any(const struct ctx *c, enum epoll_type type,
* default, but sysctl net.ipv6.bindv6only can change that, so set the
* sockopt explicitly.
*/
- return sock_l4_(c, type, &sa, ifname, 0);
+ return sock_l4_(c, type, &sa, ifname, 0, false);
}
/**
diff --git a/util.h b/util.h
index ec2d9ac3..0f187adc 100644
--- a/util.h
+++ b/util.h
@@ -147,7 +147,8 @@ struct ctx;
union sockaddr_inany;
int sock_l4(const struct ctx *c, enum epoll_type type,
- const union sockaddr_inany *sa, const char *ifname);
+ const union sockaddr_inany *sa, const char *ifname,
+ bool exclusive);
int sock_l4_dualstack_any(const struct ctx *c, enum epoll_type type,
in_port_t port, const char *ifname);
int sock_unix(char *sock_path);
--
2.56.1
^ permalink raw reply [flat|nested] 4+ messages in thread* [PATCH 2/2] udp: Keep sharing source ports we forward ourselves
2026-12-01 23:52 [PATCH 0/2] udp: Don't share preserved source ports with foreign sockets Jon Maloy
2026-12-01 23:52 ` [PATCH 1/2] udp: Don't share a preserved source port with other sockets Jon Maloy
@ 2026-12-01 23:52 ` Jon Maloy
1 sibling, 0 replies; 4+ messages in thread
From: Jon Maloy @ 2026-12-01 23:52 UTC (permalink / raw)
To: sbrivio, jmaloy, passt-dev; +Cc: Gerwim Feiken
The previous change binds the host side socket of a flow initiated from
the tap interface exclusively, and falls back to a port picked by the
kernel if the guest's source port is already in use. This fixes the
case of several passt instances sharing the same port towards the same
destination, and the case of a foreign socket holding the port, such as
the one of Podman's rootlessport process forwarding a port to the
container.
However, if we forward that port ourselves, the exclusive bind now also
fails against our own listening socket, so we stop preserving the
source port exactly where it matters: peers might reach us back at the
source port of our packets, as nodes in BitTorrent's DHT do, and an
ephemeral port doesn't accept new traffic.
Sharing a port with our own listening socket is safe: the flow socket
is connected, so it always takes precedence over the listening socket
for replies from its peer, while any other traffic still reaches the
listening socket.
If the exclusive bind fails, check if we have a listening socket for
the source port of the flow, in the forwarding table of the
corresponding interface, and if we do, bind the flow socket with
SO_REUSEADDR instead, as we did before the previous change, rather
than falling back to an ephemeral port. Check for the listening socket
itself, not just for a matching rule, so that a port we failed to bind
for listening, or one we're not currently listening on with automatic
forwarding, still results in the fallback.
Do this only on failure, so that we don't need to look up forwarding
rules for every new flow.
Link: https://bugs.passt.top/show_bug.cgi?id=223
Suggested-by: Stefano Brivio <sbrivio@redhat.com>
Signed-off-by: Jon Maloy <jmaloy@redhat.com>
---
udp_flow.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++----
1 file changed, 49 insertions(+), 4 deletions(-)
diff --git a/udp_flow.c b/udp_flow.c
index a9fab7ae..57e159fc 100644
--- a/udp_flow.c
+++ b/udp_flow.c
@@ -59,6 +59,37 @@ void udp_flow_close(const struct ctx *c, struct udp_flow *uflow)
uflow->closed = true;
}
+/**
+ * udp_flow_port_listening() - Do we have a listening socket on a flow's port?
+ * @c: Execution context
+ * @pif: Interface of the flow side
+ * @side: Flow side, giving our address and port on @pif
+ *
+ * Return: true if we're forwarding UDP from the address and port of @side on
+ * @pif, and the corresponding listening socket is actually open
+ */
+static bool udp_flow_port_listening(const struct ctx *c, uint8_t pif,
+ const struct flowside *side)
+{
+ const struct fwd_table *fwd = c->fwd[pif];
+ const struct flowside ini = {
+ .oaddr = side->oaddr,
+ .oport = side->oport,
+ };
+ const struct fwd_rule *rule;
+ unsigned idx;
+
+ if (!fwd || !side->oport)
+ return false;
+
+ rule = fwd_rule_search(fwd, &ini, IPPROTO_UDP, FWD_NO_HINT);
+ if (!rule)
+ return false;
+
+ idx = rule - fwd->rules;
+ return fwd->rulesocks[idx][side->oport - rule->first] >= 0;
+}
+
/**
* udp_flow_sock() - Create, bind and connect a flow specific UDP socket
* @c: Execution context
@@ -89,10 +120,24 @@ static int udp_flow_sock(const struct ctx *c,
s = flowside_sock_l4(c, EPOLL_TYPE_UDP, pif, side, exclusive);
if (s == -EADDRINUSE && exclusive && side->oport) {
- flow_dbg(uflow, "Host port %hu in use, using an ephemeral port",
- side->oport);
- uflow->f.side[sidei].oport = 0;
- s = flowside_sock_l4(c, EPOLL_TYPE_UDP, pif, side, exclusive);
+ if (udp_flow_port_listening(c, pif, side)) {
+ /* The port is held by our own listening socket: sharing
+ * it is safe, as the connected flow socket always takes
+ * precedence for replies from its peer, and preserving
+ * the port matters here, as peers might reach us back
+ * at the source port of our packets (e.g. BitTorrent's
+ * DHT).
+ */
+ s = flowside_sock_l4(c, EPOLL_TYPE_UDP, pif, side,
+ false);
+ } else {
+ flow_dbg(uflow,
+ "Host port %hu in use, using ephemeral port",
+ side->oport);
+ uflow->f.side[sidei].oport = 0;
+ s = flowside_sock_l4(c, EPOLL_TYPE_UDP, pif, side,
+ exclusive);
+ }
}
if (s < 0) {
flow_perror_ratelimit(uflow, now,
--
2.56.1
^ permalink raw reply [flat|nested] 4+ messages in thread