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 1/2] fwd: Refactor tests in fwd_nat_from_tap() for clarity
Date: Wed, 24 Jul 2024 17:51:11 +1000 [thread overview]
Message-ID: <20240724075112.1279868-2-david@gibson.dropbear.id.au> (raw)
In-Reply-To: <20240724075112.1279868-1-david@gibson.dropbear.id.au>
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 <david@gibson.dropbear.id.au>
---
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.
--
@@ -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
next prev parent reply other threads:[~2024-07-24 7:51 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-07-24 7:51 [PATCH v2 0/2] Broaden DNS forwarding David Gibson
2024-07-24 7:51 ` David Gibson [this message]
2024-07-24 7:51 ` [PATCH v2 2/2] fwd: Broaden what we consider for DNS specific forwarding rules David Gibson
2024-07-24 9:41 ` Paul Holzinger
2024-07-24 14:30 ` Stefano Brivio
2024-07-25 4:44 ` David Gibson
2024-07-25 8:39 ` Paul Holzinger
2024-07-25 11:27 ` [PATCH v2 0/2] Broaden DNS forwarding Stefano Brivio
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=20240724075112.1279868-2-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).