From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) by passt.top (Postfix) with ESMTPS id 137735A031C for ; Wed, 24 Jul 2024 09:51:22 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202312; t=1721807474; bh=PX27kSmNdo5XpxYIrXd3PcPsvIU1+0MYnQY3M49dz7o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=FPyf3xmwytmduonQ0ksEtH/eTy1JP8ai85goBY+FneXBfo1VGZ/dq7WhlmlVc8B8v rPlOEpibl4wMoK+OHOb8QI7Ovj738w22sm6cXCT3IG8LX0eDVlj8gomI32oSH2smw9 x5nwKkmGzlajy3ESp6JRjb9kKOk05XFBK5q5rBnh190V920iQzppZDDI6pf9HaHRCI tbgIw0SgWS1P/MMKkdjGLaTRd59md+MjLY9pO7aJD/cBIuPPPrJADZYPHS/TnpoyCu 8YHXpkRygJpM/353MCUPH+3lQQc6Np/IUMGTCl4eDH7IpM5GXKoITSBjPTy18TCrAs b8o3qlOf1u24Q== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4WTR6B74CRz4x3r; Wed, 24 Jul 2024 17:51:14 +1000 (AEST) From: David Gibson To: passt-dev@passt.top, Stefano Brivio Subject: [PATCH v2 1/2] fwd: Refactor tests in fwd_nat_from_tap() for clarity Date: Wed, 24 Jul 2024 17:51:11 +1000 Message-ID: <20240724075112.1279868-2-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240724075112.1279868-1-david@gibson.dropbear.id.au> References: <20240724075112.1279868-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: VUH7HMVBSGA3CKJPF3HQIPFI56BF6ICX X-Message-ID-Hash: VUH7HMVBSGA3CKJPF3HQIPFI56BF6ICX 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: Currently, we start by handling the common case, where we don't translate the destination address, then we modify the tgt side for the special cases. In the process we do comparisons on the tentatively set fields in tgt, which obscures the fact that tgt should be an essentially pure function of ini, and risks people examining fields of tgt that are not yet initialized. To make this clearer, do all our tests on 'ini', constructing tgt from scratch on that basis. Signed-off-by: David Gibson --- fwd.c | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/fwd.c b/fwd.c index 8c1f3d91..c323aba7 100644 --- a/fwd.c +++ b/fwd.c @@ -169,21 +169,20 @@ void fwd_scan_ports_init(struct ctx *c) uint8_t fwd_nat_from_tap(const struct ctx *c, uint8_t proto, const struct flowside *ini, struct flowside *tgt) { - tgt->eaddr = ini->faddr; - tgt->eport = ini->fport; - - if (proto == IPPROTO_UDP && tgt->eport == 53 && - inany_equals4(&tgt->eaddr, &c->ip4.dns_match)) { + if (proto == IPPROTO_UDP && ini->fport == 53 && + inany_equals4(&ini->faddr, &c->ip4.dns_match)) tgt->eaddr = inany_from_v4(c->ip4.dns_host); - } else if (proto == IPPROTO_UDP && tgt->eport == 53 && - inany_equals6(&tgt->eaddr, &c->ip6.dns_match)) { + else if (proto == IPPROTO_UDP && ini->fport == 53 && + inany_equals6(&ini->faddr, &c->ip6.dns_match)) tgt->eaddr.a6 = c->ip6.dns_host; - } else if (!c->no_map_gw) { - if (inany_equals4(&tgt->eaddr, &c->ip4.gw)) - tgt->eaddr = inany_loopback4; - else if (inany_equals6(&tgt->eaddr, &c->ip6.gw)) - tgt->eaddr = inany_loopback6; - } + else if (!c->no_map_gw && inany_equals4(&ini->faddr, &c->ip4.gw)) + tgt->eaddr = inany_loopback4; + else if (!c->no_map_gw && inany_equals6(&ini->faddr, &c->ip6.gw)) + tgt->eaddr = inany_loopback6; + else + tgt->eaddr = ini->faddr; + + tgt->eport = ini->fport; /* The relevant addr_out controls the host side source address. This * may be unspecified, which allows the kernel to pick an address. -- 2.45.2