public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
From: David Gibson <david@gibson.dropbear.id.au>
To: Stefano Brivio <sbrivio@redhat.com>, passt-dev@passt.top
Cc: David Gibson <david@gibson.dropbear.id.au>
Subject: [PATCH 3/5] udp: Implement IPv6 PORT_GUA logic for IPv4 as well
Date: Wed, 17 May 2023 15:05:27 +1000	[thread overview]
Message-ID: <20230517050529.3505590-4-david@gibson.dropbear.id.au> (raw)
In-Reply-To: <20230517050529.3505590-1-david@gibson.dropbear.id.au>

For IPv6 UDP, the PORT_GUA flag is set for a port when we get a
"connection" from ip6.addr, that is from the host's global address.  An
exactly analogous situation is possible for IPv4, but we don't handle it
the same way.  In practice it will only show up if addr_seen is different
from addr, which is unusual.  Nonetheless we should handle this the same
way for IPv4 and IPv6.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
---
 udp.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/udp.c b/udp.c
index d7e1020..950d5a9 100644
--- a/udp.c
+++ b/udp.c
@@ -596,7 +596,8 @@ static size_t udp_update_hdr4(const struct ctx *c, int n, in_port_t dstport,
 	    IN4_ARE_ADDR_EQUAL(src, &c->ip4.dns_host) && src_port == 53) {
 		b->iph.saddr = c->ip4.dns_match.s_addr;
 	} else if (IN4_IS_ADDR_LOOPBACK(src) ||
-		   IN4_ARE_ADDR_EQUAL(src, &c->ip4.addr_seen)) {
+		   IN4_ARE_ADDR_EQUAL(src, &c->ip4.addr_seen) ||
+		   IN4_ARE_ADDR_EQUAL(src, &c->ip4.addr)) {
 		b->iph.saddr = c->ip4.gw.s_addr;
 		udp_tap_map[V4][src_port].ts = now->tv_sec;
 		udp_tap_map[V4][src_port].flags |= PORT_LOCAL;
@@ -606,6 +607,11 @@ static size_t udp_update_hdr4(const struct ctx *c, int n, in_port_t dstport,
 		else
 			udp_tap_map[V4][src_port].flags &= ~PORT_LOOPBACK;
 
+		if (IN4_ARE_ADDR_EQUAL(src, &c->ip4.addr))
+			udp_tap_map[V4][src_port].flags |= PORT_GUA;
+		else
+			udp_tap_map[V4][src_port].flags &= ~PORT_GUA;
+
 		bitmap_set(udp_act[V4][UDP_ACT_TAP], src_port);
 	} else {
 		b->iph.saddr = src->s_addr;
@@ -852,6 +858,8 @@ int udp_tap_handler(struct ctx *c, int af, const void *addr,
 			if (!(udp_tap_map[V4][dst].flags & PORT_LOCAL) ||
 			    (udp_tap_map[V4][dst].flags & PORT_LOOPBACK))
 				s_in.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
+			else if (udp_tap_map[V4][dst].flags & PORT_GUA)
+				s_in.sin_addr = c->ip4.addr;
 			else
 				s_in.sin_addr = c->ip4.addr_seen;
 		}
-- 
@@ -596,7 +596,8 @@ static size_t udp_update_hdr4(const struct ctx *c, int n, in_port_t dstport,
 	    IN4_ARE_ADDR_EQUAL(src, &c->ip4.dns_host) && src_port == 53) {
 		b->iph.saddr = c->ip4.dns_match.s_addr;
 	} else if (IN4_IS_ADDR_LOOPBACK(src) ||
-		   IN4_ARE_ADDR_EQUAL(src, &c->ip4.addr_seen)) {
+		   IN4_ARE_ADDR_EQUAL(src, &c->ip4.addr_seen) ||
+		   IN4_ARE_ADDR_EQUAL(src, &c->ip4.addr)) {
 		b->iph.saddr = c->ip4.gw.s_addr;
 		udp_tap_map[V4][src_port].ts = now->tv_sec;
 		udp_tap_map[V4][src_port].flags |= PORT_LOCAL;
@@ -606,6 +607,11 @@ static size_t udp_update_hdr4(const struct ctx *c, int n, in_port_t dstport,
 		else
 			udp_tap_map[V4][src_port].flags &= ~PORT_LOOPBACK;
 
+		if (IN4_ARE_ADDR_EQUAL(src, &c->ip4.addr))
+			udp_tap_map[V4][src_port].flags |= PORT_GUA;
+		else
+			udp_tap_map[V4][src_port].flags &= ~PORT_GUA;
+
 		bitmap_set(udp_act[V4][UDP_ACT_TAP], src_port);
 	} else {
 		b->iph.saddr = src->s_addr;
@@ -852,6 +858,8 @@ int udp_tap_handler(struct ctx *c, int af, const void *addr,
 			if (!(udp_tap_map[V4][dst].flags & PORT_LOCAL) ||
 			    (udp_tap_map[V4][dst].flags & PORT_LOOPBACK))
 				s_in.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
+			else if (udp_tap_map[V4][dst].flags & PORT_GUA)
+				s_in.sin_addr = c->ip4.addr;
 			else
 				s_in.sin_addr = c->ip4.addr_seen;
 		}
-- 
2.40.1


  parent reply	other threads:[~2023-05-17  5:05 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-17  5:05 [PATCH 0/5] Improvements to "connection" tracking for UDP David Gibson
2023-05-17  5:05 ` [PATCH 1/5] udp: Don't attempt to translate a 0.0.0.0 source address David Gibson
2023-05-17  5:05 ` [PATCH 2/5] udp: Small streamline to udp_update_hdr4() David Gibson
2023-05-17  5:05 ` David Gibson [this message]
2023-05-17  5:05 ` [PATCH 4/5] udp: Clarify connection tracking flags David Gibson
2023-05-17  5:05 ` [PATCH 5/5] udp: Remove PORT_ADDR_SEEN "connection" tracking mode David Gibson
2023-05-18  5:48 ` [PATCH 0/5] Improvements to "connection" tracking for UDP David Gibson

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=20230517050529.3505590-4-david@gibson.dropbear.id.au \
    --to=david@gibson.dropbear.id.au \
    --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).