diff options
| author | Jon Maloy <jmaloy@redhat.com> | 2026-12-01 18:52:02 -0500 |
|---|---|---|
| committer | Stefano Brivio <sbrivio@redhat.com> | 2026-12-02 22:58:25 +0100 |
| commit | ffb4d5f7f68e455cc300b4285b5bef89f6d87ec0 (patch) | |
| tree | ffc2115b9d3c38f8bf83de95beba7bb99a97a530 | |
| parent | 3f594423487c44db8d78748922cfb614b4b77518 (diff) | |
| download | passt-ffb4d5f7f68e455cc300b4285b5bef89f6d87ec0.tar passt-ffb4d5f7f68e455cc300b4285b5bef89f6d87ec0.tar.gz passt-ffb4d5f7f68e455cc300b4285b5bef89f6d87ec0.tar.bz2 passt-ffb4d5f7f68e455cc300b4285b5bef89f6d87ec0.tar.lz passt-ffb4d5f7f68e455cc300b4285b5bef89f6d87ec0.tar.xz passt-ffb4d5f7f68e455cc300b4285b5bef89f6d87ec0.tar.zst passt-ffb4d5f7f68e455cc300b4285b5bef89f6d87ec0.zip | |
udp: Keep sharing source ports we forward ourselves
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.
Reported-by: Bug C <bugchaser123@proton.me>
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>
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
| -rw-r--r-- | udp_flow.c | 53 |
1 files changed, 49 insertions, 4 deletions
@@ -60,6 +60,37 @@ void udp_flow_close(const struct ctx *c, struct udp_flow *uflow) } /** + * 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 * @uflow: UDP flow to open socket for @@ -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, |
