From: Jon Maloy <jmaloy@redhat.com>
To: sbrivio@redhat.com, jmaloy@redhat.com, passt-dev@passt.top
Cc: Gerwim Feiken <info@gerwim.nl>
Subject: [PATCH 2/2] udp: Keep sharing source ports we forward ourselves
Date: Tue, 1 Dec 2026 18:52:02 -0500 [thread overview]
Message-ID: <20261201235202.2474521-3-jmaloy@redhat.com> (raw)
In-Reply-To: <20261201235202.2474521-1-jmaloy@redhat.com>
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
next prev parent reply other threads:[~2026-12-01 23:52 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
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 [this message]
-- strict thread matches above, loose matches on Subject: below --
2026-12-01 23:37 [PATCH 0/2] udp: Don't share preserved source ports with foreign sockets Jon Maloy
2026-12-01 23:37 ` [PATCH 2/2] udp: Keep sharing source ports we forward ourselves Jon Maloy
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261201235202.2474521-3-jmaloy@redhat.com \
--to=jmaloy@redhat.com \
--cc=info@gerwim.nl \
--cc=passt-dev@passt.top \
--cc=sbrivio@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://passt.top/passt
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).