From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from gandalf.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id 11E025A0275 for ; Thu, 29 Feb 2024 09:42:57 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202312; t=1709196173; bh=jRCaEzwFbN01knyib0ioyCdx6HzBmQylTCTPeipRBFI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=nEpeTSn/wJA9s8HSAk9+N+137UtZZRo9k3QtKEUTi8Jowmx5slDyghOXTuZV6Wnno SRYkMkx3RpUtfnJNoLr3s/cEp2YQ5+omjeQm2J/5EEitALa9I113cz9xSiiN/E3+Qh icJzbamkU0jA7FUNKfZ9yqtmXLN/5sVuo1Cz7i/sAI8r6IxqLOHEjhgHzPYBrYqbqB 25uIxZD+md5rV27F6TvtQDuZ2Q977S9D2BTkJgNhvgPo1XsFygo30DmqiVC/HsfgMs V/faAujOY1aDTeqRxgLjKWKnUqNeqE91BqlxYEdv8jS+ViDFKryZaA2vaXnFyBX4nO BW1scKHo1mzeQ== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4Tll9926dCz4wbQ; Thu, 29 Feb 2024 19:42:53 +1100 (AEDT) From: David Gibson To: Stefano Brivio , Laurent Vivier , passt-dev@passt.top Subject: [PATCH 3/6] udp: Clarify setting of addresses inin udp_update_hdr[46]() Date: Thu, 29 Feb 2024 19:42:47 +1100 Message-ID: <20240229084250.3202450-4-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240229084250.3202450-1-david@gibson.dropbear.id.au> References: <20240229084250.3202450-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: NF6LCI4LJR6BVEDLT752W6WLGAW3THO4 X-Message-ID-Hash: NF6LCI4LJR6BVEDLT752W6WLGAW3THO4 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: Both of these functions can set the final values of the addresses in the header in several places, which can be hard to follow. Change it to use temporary locals, 'src' and 'dst' to track the addresses we're going to want then write it to the actual header in one place. This will make some subsequent changes easier. Signed-off-by: David Gibson --- udp.c | 49 ++++++++++++++++++++++--------------------------- 1 file changed, 22 insertions(+), 27 deletions(-) diff --git a/udp.c b/udp.c index e3c51bae..a07c1a62 100644 --- a/udp.c +++ b/udp.c @@ -588,18 +588,14 @@ static size_t udp_update_hdr4(const struct ctx *c, struct udp4_l2_buf_t *b, const struct timespec *now) { size_t ip_len = datalen + sizeof(b->iph) + sizeof(b->uh); + const struct in_addr *src = &b->s_in.sin_addr; in_port_t src_port = ntohs(b->s_in.sin_port); - struct in_addr *src = &b->s_in.sin_addr; - - b->iph.tot_len = htons(ip_len); - b->iph.daddr = c->ip4.addr_seen.s_addr; if (!IN4_IS_ADDR_UNSPECIFIED(&c->ip4.dns_match) && 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) || 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].flags |= PORT_LOCAL; @@ -609,12 +605,15 @@ static size_t udp_update_hdr4(const struct ctx *c, struct udp4_l2_buf_t *b, udp_tap_map[V4][src_port].flags &= ~PORT_LOOPBACK; bitmap_set(udp_act[V4][UDP_ACT_TAP], src_port); - } else { - b->iph.saddr = src->s_addr; + + src = &c->ip4.gw; } + b->iph.tot_len = htons(ip_len); + b->iph.daddr = c->ip4.addr_seen.s_addr; + b->iph.saddr = src->s_addr; udp_update_check4(b); - b->uh.source = b->s_in.sin_port; + b->uh.source = htons(src_port); b->uh.dest = htons(dstport); b->uh.len = htons(datalen + sizeof(b->uh)); @@ -636,29 +635,19 @@ static size_t udp_update_hdr6(const struct ctx *c, struct udp6_l2_buf_t *b, const struct timespec *now) { size_t ip_len = datalen + sizeof(b->ip6h) + sizeof(b->uh); + const struct in6_addr *src = &b->s_in6.sin6_addr; + const struct in6_addr *dst = &c->ip6.addr_seen; in_port_t src_port = ntohs(b->s_in6.sin6_port); - struct in6_addr *src = &b->s_in6.sin6_addr; - - b->ip6h.payload_len = htons(datalen + sizeof(b->uh)); if (IN6_IS_ADDR_LINKLOCAL(src)) { - b->ip6h.daddr = c->ip6.addr_ll_seen; - b->ip6h.saddr = b->s_in6.sin6_addr; + dst = &c->ip6.addr_ll_seen; } else if (!IN6_IS_ADDR_UNSPECIFIED(&c->ip6.dns_match) && IN6_ARE_ADDR_EQUAL(src, &c->ip6.dns_host) && src_port == 53) { - b->ip6h.daddr = c->ip6.addr_seen; - b->ip6h.saddr = c->ip6.dns_match; + src = &c->ip6.dns_match; } else if (IN6_IS_ADDR_LOOPBACK(src) || IN6_ARE_ADDR_EQUAL(src, &c->ip6.addr_seen) || 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].flags |= PORT_LOCAL; @@ -673,12 +662,18 @@ static size_t udp_update_hdr6(const struct ctx *c, struct udp6_l2_buf_t *b, udp_tap_map[V6][src_port].flags &= ~PORT_GUA; bitmap_set(udp_act[V6][UDP_ACT_TAP], src_port); - } else { - b->ip6h.daddr = c->ip6.addr_seen; - b->ip6h.saddr = b->s_in6.sin6_addr; + + dst = &c->ip6.addr_ll_seen; + if (IN6_IS_ADDR_LINKLOCAL(&c->ip6.gw)) + src = &c->ip6.gw; + else + src = &c->ip6.addr_ll; } - b->uh.source = b->s_in6.sin6_port; + b->ip6h.payload_len = htons(datalen + sizeof(b->uh)); + b->ip6h.saddr = *src; + b->ip6h.daddr = *dst; + b->uh.source = htons(src_port); b->uh.dest = htons(dstport); b->uh.len = b->ip6h.payload_len; -- 2.44.0