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 4/5] fwd: Reorder DNAPT and SNAT steps in fwd_nat_from_host()
Date: Fri, 10 Jul 2026 16:56:10 +1000	[thread overview]
Message-ID: <20260710065611.530947-5-david@gibson.dropbear.id.au> (raw)
In-Reply-To: <20260710065611.530947-1-david@gibson.dropbear.id.au>

The order in which we translate source and destination addresses is a bit
unclear in fwd_nat_from_host().  Reorder things to make it clearer:
   1. Pick guest-side destination address, where options require it
   2. Pick guest-side source address (needs to be different for SPLICE and
      TAP)
   3. If (1) didn't determine destination, pick a fallback to match family
      and scope of source address from (2).

As a small bonus this lets us make step (1) common between SPLICE and TAP
paths.  The value of this is a bit dubious right now, but it will make some
future changes clearer.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
---
 fwd.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/fwd.c b/fwd.c
index 90297ef7..7d39898e 100644
--- a/fwd.c
+++ b/fwd.c
@@ -1041,8 +1041,12 @@ uint8_t fwd_nat_from_host(const struct ctx *c,
 			  const struct fwd_rule *rule, uint8_t proto,
 			  const struct flowside *ini, struct flowside *tgt)
 {
-	/* Common for spliced and non-spliced cases */
+	/* DNAPT: Common for splice and non-spliced where possible */
 	tgt->eport = rule->to + (ini->oport - rule->first);
+	if (!inany_is_unspecified(&rule->taddr))
+		tgt->eaddr = rule->taddr;
+	else if (c->host_lo_to_ns_lo && inany_is_loopback(&ini->oaddr))
+		tgt->eaddr = ini->oaddr;
 
 	/* TODO: Allow splicing with specified target address */
 	if (!c->no_splice && inany_is_unspecified(&rule->taddr) &&
@@ -1059,10 +1063,8 @@ uint8_t fwd_nat_from_host(const struct ctx *c,
 		 * In either case, let the kernel pick the source address to
 		 * match.
 		 */
-		if (c->host_lo_to_ns_lo && inany_is_loopback(&ini->oaddr))
-			tgt->eaddr = ini->oaddr;
 
-		/* Let the kernel pick source address and port */
+		/* SNAT: (implicit) let the kernel pick source addr/port */
 		if (inany_v4(&ini->eaddr))
 			tgt->oaddr = inany_any4;
 		else
@@ -1082,6 +1084,7 @@ uint8_t fwd_nat_from_host(const struct ctx *c,
 	if (c->splice_only)
 		return PIF_NONE;
 
+	/* SNAT: translate source address if necessary */
 	if (!nat_inbound(c, &ini->eaddr, &tgt->oaddr)) {
 		if (inany_v4(&ini->eaddr)) {
 			if (IN4_IS_ADDR_UNSPECIFIED(&c->ip4.our_tap_addr))
@@ -1094,9 +1097,6 @@ uint8_t fwd_nat_from_host(const struct ctx *c,
 	}
 	tgt->oport = ini->eport;
 
-	if (!inany_is_unspecified(&rule->taddr))
-		tgt->eaddr = rule->taddr;
-
 	/* Use guest address as destination, if otherwise unspecified */
 	if (inany_is_unspecified(&tgt->eaddr))
 		fwd_default_guest_addr(c, &tgt->eaddr, &tgt->oaddr);
-- 
2.55.0


  parent reply	other threads:[~2026-07-10  6:56 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-10  6:56 [PATCH 0/5] RFC: Fix bug 209 David Gibson
2026-07-10  6:56 ` [PATCH 1/5] fwd: Clarify semantics of --host-lo-to-ns-lo David Gibson
2026-07-10  6:56 ` [PATCH 2/5] udp: Validate that we have a unicast source address David Gibson
2026-07-10  6:56 ` [PATCH 3/5] fwd: Rework default address logic for inbound flows David Gibson
2026-07-10  6:56 ` David Gibson [this message]
2026-07-10  6:56 ` [PATCH 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=20260710065611.530947-5-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).