From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id BD6EE5A031A for ; Wed, 24 Jul 2024 08:21:08 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202312; t=1721802060; bh=DT2eY8j3AVjaowffzmCOySObVjlC6VDx6tBfw+dvUHw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ms20dKoiPNDD94VtKZY2nvG7TXnKqe6xn/dqOuU6tB88V80U8Hvv0J3LRnTOztQh5 hsWODQpvsDDFYQKlevdu0+Lboo98Gv6+rgIoqv+5QpgizFFLb5438htE0xCI0Fkwfd pXvMIiqdt7xtKdDKH5vt4djA3sXSTrhKkNar1IbxKdlkgrAVNJ7BRldka4HWG5HH6j RisncjIMDL9Co7nzRqD7Lw65EC7bM/uLeUhAoOOJ9pVD9gbG5HGyDO/VpjXBx1YRCJ vfUjU5cfQxaSTa1pYBYaCfVyeXQosiYJCin13wrdZV6VzN0OVBE2vCFeqtcrHtEo2H +qk+UYH2xjdCQ== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4WTP642GRqz4w2S; Wed, 24 Jul 2024 16:21:00 +1000 (AEST) From: David Gibson To: passt-dev@passt.top, Stefano Brivio Subject: [PATCH 1/2] fwd: Refactor tests in fwd_nat_from_tap() for clarity Date: Wed, 24 Jul 2024 16:20:57 +1000 Message-ID: <20240724062058.1259033-2-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240724062058.1259033-1-david@gibson.dropbear.id.au> References: <20240724062058.1259033-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: DZ4YS4QFCNCN2HQNBOG3HXUX4T4JPGHT X-Message-ID-Hash: DZ4YS4QFCNCN2HQNBOG3HXUX4T4JPGHT 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 | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/fwd.c b/fwd.c index 8c1f3d91..bd16ac42 100644 --- a/fwd.c +++ b/fwd.c @@ -169,22 +169,22 @@ 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)) + } 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