fwd: Refactor tests in fwd_nat_from_tap() for clarity
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> Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
This commit is contained in:
parent
4a333c88d7
commit
0ada84e3f8
1 changed files with 12 additions and 13 deletions
25
fwd.c
25
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,
|
uint8_t fwd_nat_from_tap(const struct ctx *c, uint8_t proto,
|
||||||
const struct flowside *ini, struct flowside *tgt)
|
const struct flowside *ini, struct flowside *tgt)
|
||||||
{
|
{
|
||||||
tgt->eaddr = ini->faddr;
|
if (proto == IPPROTO_UDP && ini->fport == 53 &&
|
||||||
tgt->eport = ini->fport;
|
inany_equals4(&ini->faddr, &c->ip4.dns_match))
|
||||||
|
|
||||||
if (proto == IPPROTO_UDP && tgt->eport == 53 &&
|
|
||||||
inany_equals4(&tgt->eaddr, &c->ip4.dns_match)) {
|
|
||||||
tgt->eaddr = inany_from_v4(c->ip4.dns_host);
|
tgt->eaddr = inany_from_v4(c->ip4.dns_host);
|
||||||
} else if (proto == IPPROTO_UDP && tgt->eport == 53 &&
|
else if (proto == IPPROTO_UDP && ini->fport == 53 &&
|
||||||
inany_equals6(&tgt->eaddr, &c->ip6.dns_match)) {
|
inany_equals6(&ini->faddr, &c->ip6.dns_match))
|
||||||
tgt->eaddr.a6 = c->ip6.dns_host;
|
tgt->eaddr.a6 = c->ip6.dns_host;
|
||||||
} else if (!c->no_map_gw) {
|
else if (!c->no_map_gw && inany_equals4(&ini->faddr, &c->ip4.gw))
|
||||||
if (inany_equals4(&tgt->eaddr, &c->ip4.gw))
|
tgt->eaddr = inany_loopback4;
|
||||||
tgt->eaddr = inany_loopback4;
|
else if (!c->no_map_gw && inany_equals6(&ini->faddr, &c->ip6.gw))
|
||||||
else if (inany_equals6(&tgt->eaddr, &c->ip6.gw))
|
tgt->eaddr = inany_loopback6;
|
||||||
tgt->eaddr = inany_loopback6;
|
else
|
||||||
}
|
tgt->eaddr = ini->faddr;
|
||||||
|
|
||||||
|
tgt->eport = ini->fport;
|
||||||
|
|
||||||
/* The relevant addr_out controls the host side source address. This
|
/* The relevant addr_out controls the host side source address. This
|
||||||
* may be unspecified, which allows the kernel to pick an address.
|
* may be unspecified, which allows the kernel to pick an address.
|
||||||
|
|
Loading…
Reference in a new issue