public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
From: David Gibson <david@gibson.dropbear.id.au>
To: passt-dev@passt.top, Stefano Brivio <sbrivio@redhat.com>
Cc: David Gibson <david@gibson.dropbear.id.au>
Subject: [PATCH v2 3/5] fwd: Rework default address logic for inbound flows
Date: Mon, 13 Jul 2026 15:38:54 +1000	[thread overview]
Message-ID: <20260713053856.1329271-4-david@gibson.dropbear.id.au> (raw)
In-Reply-To: <20260713053856.1329271-1-david@gibson.dropbear.id.au>

fwd_nat_from_host() needs to determine the guest side destination address
for the new flow.  In some cases that's controlled by the forwarding rule
or --host-lo-to-ns-lo logic, but by default we use the observed guest
address.  We need to pick the right one to match the source address,
though.

Currently this is done with similar, but not quite identical logic in the
spliced and non-spliced paths.  Introduce a new fwd_default_guest_addr()
helper to make explicit:
 * We have the same logic for splice and tap paths
 * This is a fallback path if nothing else determined the address (we
   use this default nearly all the time now, but it might change in future)
 * We're matching IP family and scope with the guest side source address

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
---
 fwd.c   | 41 +++++++++++++++++++++++++++--------------
 inany.h |  2 ++
 2 files changed, 29 insertions(+), 14 deletions(-)

diff --git a/fwd.c b/fwd.c
index 84400948..de444d9c 100644
--- a/fwd.c
+++ b/fwd.c
@@ -1005,6 +1005,24 @@ bool nat_inbound(const struct ctx *c, const union inany_addr *addr,
 	return true;
 }
 
+/**
+ * fwd_default_guest_addr() - Get appropriate guest address to send to
+ * @c:		Execution context
+ * @template:	Address to match IP version and scope of
+ *
+ * Return: the address of the guest, which best matches the IP version and scope
+ *         of @template
+ */
+static union inany_addr fwd_default_guest_addr(const struct ctx *c,
+					       const union inany_addr *template)
+{
+	if (inany_v4(template))
+		return inany_from_v4(c->ip4.addr_seen);
+	if (inany_is_linklocal6(template))
+		return inany_from_v6(c->ip6.addr_ll_seen);
+	return inany_from_v6(c->ip6.addr_seen);
+}
+
 /**
  * fwd_nat_from_host() - Determine to forward a flow from the host interface
  * @c:		Execution context
@@ -1040,13 +1058,9 @@ uint8_t fwd_nat_from_host(const struct ctx *c,
 		 */
 		if (c->host_lo_to_ns_lo && inany_is_loopback(&ini->oaddr))
 			tgt->eaddr = ini->oaddr;
-		else if (inany_v4(&ini->eaddr))
-			tgt->eaddr = inany_from_v4(c->ip4.addr_seen);
-		else
-			tgt->eaddr.a6 = c->ip6.addr_seen;
 
 		/* Let the kernel pick source address and port */
-		if (inany_v4(&tgt->eaddr))
+		if (inany_v4(&ini->eaddr))
 			tgt->oaddr = inany_any4;
 		else
 			tgt->oaddr = inany_any6;
@@ -1056,6 +1070,9 @@ uint8_t fwd_nat_from_host(const struct ctx *c,
 			/* But for UDP preserve the source port */
 			tgt->oport = ini->eport;
 
+		/* Use guest address as destination, if otherwise unspecified */
+		if (inany_is_unspecified(&tgt->eaddr))
+			tgt->eaddr = fwd_default_guest_addr(c, &tgt->oaddr);
 		return PIF_SPLICE;
 	}
 
@@ -1074,16 +1091,12 @@ uint8_t fwd_nat_from_host(const struct ctx *c,
 	}
 	tgt->oport = ini->eport;
 
-	if (!inany_is_unspecified(&rule->taddr)) {
+	if (!inany_is_unspecified(&rule->taddr))
 		tgt->eaddr = rule->taddr;
-	} else if (inany_v4(&tgt->oaddr)) {
-		tgt->eaddr = inany_from_v4(c->ip4.addr_seen);
-	} else {
-		if (inany_is_linklocal6(&tgt->oaddr))
-			tgt->eaddr.a6 = c->ip6.addr_ll_seen;
-		else
-			tgt->eaddr.a6 = c->ip6.addr_seen;
-	}
+
+	/* Use guest address as destination, if otherwise unspecified */
+	if (inany_is_unspecified(&tgt->eaddr))
+		tgt->eaddr = fwd_default_guest_addr(c, &tgt->oaddr);
 
 	return PIF_TAP;
 }
diff --git a/inany.h b/inany.h
index 5b176ccf..d0befb15 100644
--- a/inany.h
+++ b/inany.h
@@ -59,6 +59,8 @@ extern const union inany_addr inany_any4;
 
 #define inany_from_v4(a4)	\
 	((union inany_addr)INANY_INIT4((a4)))
+#define inany_from_v6(a6_)	\
+	((union inany_addr){.a6 = (a6_)})
 
 /** union sockaddr_inany - Either a sockaddr_in or a sockaddr_in6
  * @sa_family:	Address family, AF_INET or AF_INET6
-- 
2.55.0


  parent reply	other threads:[~2026-07-13  5:39 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-13  5:38 [PATCH v2 0/5] Fix bug 209 David Gibson
2026-07-13  5:38 ` [PATCH v2 1/5] fwd: Clarify semantics of --host-lo-to-ns-lo David Gibson
2026-07-13  5:38 ` [PATCH v2 2/5] udp: Validate that we have a unicast source address David Gibson
2026-07-13  5:38 ` David Gibson [this message]
2026-07-13  5:38 ` [PATCH v2 4/5] fwd: Reorder DNAPT and SNAT steps in fwd_nat_from_host() David Gibson
2026-07-13  5:38 ` [PATCH v2 5/5] fwd: Don't rewrite inbound multicast destinations 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=20260713053856.1329271-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).