From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from gandalf.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id D4BD45A026E for ; Wed, 24 Apr 2024 03:05:41 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202312; t=1713920735; bh=5ycR+3kY9I+tqTOkdGwocFNJx7miRme6/7s4eA+yrmg=; h=From:To:Cc:Subject:Date:From; b=jefSjDRVz4kE4BiOx/r23/QX1ZiKj+KmOhHc/Bo7CTq69EjH/ap6r1FdDK3U7z66q B6r4D7XDK3m3UFIlf3BjkFWuhWm0M2QcDt5pPFnV6rYmXJ0HstROjAmzHJ3r68wwjX 98zQsAM/af1P4hxGPfLNQsh4VrYKeQPIbBp/k00Fxd+pcO5ri89GFfQtsN2EQrufvw P9c9TXLsmHc+jv6wXcduVKeuTVVyQtcC/KMOK7V7e90q9sNtXJ7k01/qc4+aKvVcaJ 6G/UlOdEAAbYgdov4zoQIdj8p9g1zbO5z6zJepKzkjz0aHjxKnICcr7KaM3toBVqs5 ullvr3XP8BuQg== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4VPLQ75ndDz4wcp; Wed, 24 Apr 2024 11:05:35 +1000 (AEST) From: David Gibson To: Stefano Brivio , passt-dev@passt.top Subject: [PATCH] udp: Correctly look up outbound socket with port remappings Date: Wed, 24 Apr 2024 11:05:34 +1000 Message-ID: <20240424010534.496003-1-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.44.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: CEF3WBDL4X4WRREUBJLV372KPVQEND4C X-Message-ID-Hash: CEF3WBDL4X4WRREUBJLV372KPVQEND4C X-MailFrom: dgibson@gandalf.ozlabs.org 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: David Gibson 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: Commit bb9bf0bb ("tcp, udp: Don't precompute port remappings in epoll references") changed the epoll reference for UDP sockets to include the bound port as seen by the socket itself, rather than the bound port it would be translated to on the guest side. As a side effect, it also means that udp_tap_map[] is indexed by the bound port on the host side, rather than on the guest side. This is consistent and a good idea, however we forgot to account for it when finding the correct outgoing socket for packets originating in the guest. This means that if forwarding UDP inbound with a port number change, reply packets would be misdirected. Fix this by applying the reverse mapping before looking up the socket in udp_tap_handler(). While we're at it, use 'port' directly instead of 'uref.port' in udp_sock_init(). Those now always have the same value - failing to realise that is the same error as above. Link: https://bugs.passt.top/show_bug.cgi?id=87 Fixes: bb9bf0bb ("tcp, udp: Don't precompute port remappings in epoll references") Signed-off-by: David Gibson --- udp.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/udp.c b/udp.c index 694424a5..594ea191 100644 --- a/udp.c +++ b/udp.c @@ -829,6 +829,7 @@ int udp_tap_handler(struct ctx *c, uint8_t pif, * and destination, so we can just take those from the first message. */ src = ntohs(uh->source); + src += c->udp.fwd_in.rdelta[src]; dst = ntohs(uh->dest); if (af == AF_INET) { @@ -1005,7 +1006,7 @@ int udp_sock_init(const struct ctx *c, int ns, sa_family_t af, r4 = s = sock_l4(c, AF_INET, IPPROTO_UDP, addr, ifname, port, uref.u32); - udp_tap_map[V4][uref.port].sock = s < 0 ? -1 : s; + udp_tap_map[V4][port].sock = s < 0 ? -1 : s; udp_splice_init[V4][port].sock = s < 0 ? -1 : s; } else { r4 = s = sock_l4(c, AF_INET, IPPROTO_UDP, @@ -1022,7 +1023,7 @@ int udp_sock_init(const struct ctx *c, int ns, sa_family_t af, r6 = s = sock_l4(c, AF_INET6, IPPROTO_UDP, addr, ifname, port, uref.u32); - udp_tap_map[V6][uref.port].sock = s < 0 ? -1 : s; + udp_tap_map[V6][port].sock = s < 0 ? -1 : s; udp_splice_init[V6][port].sock = s < 0 ? -1 : s; } else { r6 = s = sock_l4(c, AF_INET6, IPPROTO_UDP, -- 2.44.0