From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: passt.top; dmarc=fail (p=quarantine dis=none) header.from=redhat.com Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by passt.top (Postfix) with ESMTPS id 8C3A15A0272 for ; Wed, 02 Dec 2026 00:52:11 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1796169131; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=LhspVIicpDcEaDhDPJ7xIyE1x9MLgbYXwdgtRs6Sq3s=; b=QWPTNzP/YQQCF4lEsuNUrElKgHHlMv5ip7xWmBNSY7KP4o9ZDxw8KI+yGgF1uq4cJkz27i AgLUL9P0RUSPwz0CLoIRRYVZ1VwdOeMCXTXKprYOkTo2U6iNnAYdxHVQMp0GGjsglrvtVu CJz2e7qy3QKDjeziFJ57IaKsqxHZex0= X-MC-Rep: binary re single Received: from mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-443-RbfZIbgCPhWmT--SWs0GcQ-1; Tue, 1 Dec 2026 23:52:07 +0000 X-MC-Unique: RbfZIbgCPhWmT--SWs0GcQ-1 X-Mimecast-MFC-AGG-ID: RbfZIbgCPhWmT--SWs0GcQ_1796169126 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 5F578182FA4E; Tue, 1 Dec 2026 23:52:06 +0000 (UTC) Received: from jmaloy-thinkpadp16vgen1.rmtcaqc.csb (headnet04.pony-001.prod.iad2.dc.redhat.com [10.2.32.116]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 7D4F81944EB7; Tue, 1 Dec 2026 23:52:05 +0000 (UTC) From: Jon Maloy To: sbrivio@redhat.com, jmaloy@redhat.com, passt-dev@passt.top Subject: [PATCH 2/2] udp: Keep sharing source ports we forward ourselves Date: Tue, 1 Dec 2026 18:52:02 -0500 Message-ID: <20261201235202.2474521-3-jmaloy@redhat.com> In-Reply-To: <20261201235202.2474521-1-jmaloy@redhat.com> References: <20261201235202.2474521-1-jmaloy@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: L3XPq1YX3l4nSk7Ml24oeaNltbcTlyJDTLKWgvLUUDA_1796169126 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit content-type: text/plain; charset="US-ASCII"; x-default=true Message-ID-Hash: I3SBTFGGOA6DUDCXUYQFU5C3QSDDJTKG X-Message-ID-Hash: I3SBTFGGOA6DUDCXUYQFU5C3QSDDJTKG X-MailFrom: jmaloy@redhat.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: Gerwim Feiken X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: 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 Signed-off-by: Jon Maloy --- 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