public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
* [PATCH] udp: Correctly look up outbound socket with port remappings
@ 2024-04-24  1:05 David Gibson
  2024-04-24 22:01 ` Stefano Brivio
  0 siblings, 1 reply; 2+ messages in thread
From: David Gibson @ 2024-04-24  1:05 UTC (permalink / raw)
  To: Stefano Brivio, passt-dev; +Cc: David Gibson

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 <david@gibson.dropbear.id.au>
---
 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,
-- 
@@ -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


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] udp: Correctly look up outbound socket with port remappings
  2024-04-24  1:05 [PATCH] udp: Correctly look up outbound socket with port remappings David Gibson
@ 2024-04-24 22:01 ` Stefano Brivio
  0 siblings, 0 replies; 2+ messages in thread
From: Stefano Brivio @ 2024-04-24 22:01 UTC (permalink / raw)
  To: David Gibson; +Cc: passt-dev

On Wed, 24 Apr 2024 11:05:34 +1000
David Gibson <david@gibson.dropbear.id.au> wrote:

> 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 <david@gibson.dropbear.id.au>

Applied.

-- 
Stefano


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-04-24 22:02 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-04-24  1:05 [PATCH] udp: Correctly look up outbound socket with port remappings David Gibson
2024-04-24 22:01 ` Stefano Brivio

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).