udp: little cleanup in udp_update_hdrX() to prepare future changes

in udp_update_hdr4():

    Assign the source address to src, either b->s_in.sin_addr,
    c->ip4.dns_match or c->ip4.gw and then set b->iph.saddr to src->s_addr.

in udp_update_hdr6():

   Assign the source address to src, either b->s_in6.sin6_addr,
   c->ip6.dns_match, c->ip6.gw or c->ip6.addr_ll.
   Assign the destination to dst, either c->ip6.addr_seen or
   &c->ip6.addr_ll_seen.
   Then set dst to b->ip6h.daddr and src to b->ip6h.saddr.

Signed-off-by: Laurent Vivier <lvivier@redhat.com>
Message-ID: <20240303135114.1023026-6-lvivier@redhat.com>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
This commit is contained in:
Laurent Vivier 2024-03-06 16:58:34 +11:00 committed by Stefano Brivio
parent 324bd46782
commit e82b4fe5fc

39
udp.c
View file

@ -588,7 +588,7 @@ static size_t udp_update_hdr4(const struct ctx *c, int n, in_port_t dstport,
const struct timespec *now) const struct timespec *now)
{ {
struct udp4_l2_buf_t *b = &udp4_l2_buf[n]; struct udp4_l2_buf_t *b = &udp4_l2_buf[n];
struct in_addr *src; const struct in_addr *src;
in_port_t src_port; in_port_t src_port;
size_t ip_len; size_t ip_len;
@ -602,10 +602,9 @@ static size_t udp_update_hdr4(const struct ctx *c, int n, in_port_t dstport,
if (!IN4_IS_ADDR_UNSPECIFIED(&c->ip4.dns_match) && if (!IN4_IS_ADDR_UNSPECIFIED(&c->ip4.dns_match) &&
IN4_ARE_ADDR_EQUAL(src, &c->ip4.dns_host) && src_port == 53) { IN4_ARE_ADDR_EQUAL(src, &c->ip4.dns_host) && src_port == 53) {
b->iph.saddr = c->ip4.dns_match.s_addr; src = &c->ip4.dns_match;
} else if (IN4_IS_ADDR_LOOPBACK(src) || } else if (IN4_IS_ADDR_LOOPBACK(src) ||
IN4_ARE_ADDR_EQUAL(src, &c->ip4.addr_seen)) { IN4_ARE_ADDR_EQUAL(src, &c->ip4.addr_seen)) {
b->iph.saddr = c->ip4.gw.s_addr;
udp_tap_map[V4][src_port].ts = now->tv_sec; udp_tap_map[V4][src_port].ts = now->tv_sec;
udp_tap_map[V4][src_port].flags |= PORT_LOCAL; udp_tap_map[V4][src_port].flags |= PORT_LOCAL;
@ -615,9 +614,10 @@ static size_t udp_update_hdr4(const struct ctx *c, int n, in_port_t dstport,
udp_tap_map[V4][src_port].flags &= ~PORT_LOOPBACK; udp_tap_map[V4][src_port].flags &= ~PORT_LOOPBACK;
bitmap_set(udp_act[V4][UDP_ACT_TAP], src_port); bitmap_set(udp_act[V4][UDP_ACT_TAP], src_port);
} else {
b->iph.saddr = src->s_addr; src = &c->ip4.gw;
} }
b->iph.saddr = src->s_addr;
udp_update_check4(b); udp_update_check4(b);
b->uh.source = b->s_in.sin_port; b->uh.source = b->s_in.sin_port;
@ -640,10 +640,11 @@ static size_t udp_update_hdr6(const struct ctx *c, int n, in_port_t dstport,
const struct timespec *now) const struct timespec *now)
{ {
struct udp6_l2_buf_t *b = &udp6_l2_buf[n]; struct udp6_l2_buf_t *b = &udp6_l2_buf[n];
struct in6_addr *src; const struct in6_addr *src, *dst;
in_port_t src_port; in_port_t src_port;
size_t ip_len; size_t ip_len;
dst = &c->ip6.addr_seen;
src = &b->s_in6.sin6_addr; src = &b->s_in6.sin6_addr;
src_port = ntohs(b->s_in6.sin6_port); src_port = ntohs(b->s_in6.sin6_port);
@ -652,23 +653,14 @@ static size_t udp_update_hdr6(const struct ctx *c, int n, in_port_t dstport,
b->ip6h.payload_len = htons(udp6_l2_mh_sock[n].msg_len + sizeof(b->uh)); b->ip6h.payload_len = htons(udp6_l2_mh_sock[n].msg_len + sizeof(b->uh));
if (IN6_IS_ADDR_LINKLOCAL(src)) { if (IN6_IS_ADDR_LINKLOCAL(src)) {
b->ip6h.daddr = c->ip6.addr_ll_seen; dst = &c->ip6.addr_ll_seen;
b->ip6h.saddr = b->s_in6.sin6_addr;
} else if (!IN6_IS_ADDR_UNSPECIFIED(&c->ip6.dns_match) && } else if (!IN6_IS_ADDR_UNSPECIFIED(&c->ip6.dns_match) &&
IN6_ARE_ADDR_EQUAL(src, &c->ip6.dns_host) && IN6_ARE_ADDR_EQUAL(src, &c->ip6.dns_host) &&
src_port == 53) { src_port == 53) {
b->ip6h.daddr = c->ip6.addr_seen; src = &c->ip6.dns_match;
b->ip6h.saddr = c->ip6.dns_match;
} else if (IN6_IS_ADDR_LOOPBACK(src) || } else if (IN6_IS_ADDR_LOOPBACK(src) ||
IN6_ARE_ADDR_EQUAL(src, &c->ip6.addr_seen) || IN6_ARE_ADDR_EQUAL(src, &c->ip6.addr_seen) ||
IN6_ARE_ADDR_EQUAL(src, &c->ip6.addr)) { IN6_ARE_ADDR_EQUAL(src, &c->ip6.addr)) {
b->ip6h.daddr = c->ip6.addr_ll_seen;
if (IN6_IS_ADDR_LINKLOCAL(&c->ip6.gw))
b->ip6h.saddr = c->ip6.gw;
else
b->ip6h.saddr = c->ip6.addr_ll;
udp_tap_map[V6][src_port].ts = now->tv_sec; udp_tap_map[V6][src_port].ts = now->tv_sec;
udp_tap_map[V6][src_port].flags |= PORT_LOCAL; udp_tap_map[V6][src_port].flags |= PORT_LOCAL;
@ -683,10 +675,17 @@ static size_t udp_update_hdr6(const struct ctx *c, int n, in_port_t dstport,
udp_tap_map[V6][src_port].flags &= ~PORT_GUA; udp_tap_map[V6][src_port].flags &= ~PORT_GUA;
bitmap_set(udp_act[V6][UDP_ACT_TAP], src_port); bitmap_set(udp_act[V6][UDP_ACT_TAP], src_port);
} else {
b->ip6h.daddr = c->ip6.addr_seen; dst = &c->ip6.addr_ll_seen;
b->ip6h.saddr = b->s_in6.sin6_addr;
if (IN6_IS_ADDR_LINKLOCAL(&c->ip6.gw))
src = &c->ip6.gw;
else
src = &c->ip6.addr_ll;
} }
b->ip6h.daddr = *dst;
b->ip6h.saddr = *src;
b->uh.source = b->s_in6.sin6_port; b->uh.source = b->s_in6.sin6_port;
b->uh.dest = htons(dstport); b->uh.dest = htons(dstport);