From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from gandalf.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) by passt.top (Postfix) with ESMTPS id AD2715A0279 for ; Wed, 6 Mar 2024 06:34:36 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202312; t=1709703270; bh=26ux/rFUvOjEM1iVh3wODXA6C9ZgByg34cpsh7S8uvM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=k8oLqm3VxjhKfm3SfJB9aN+W+cRYdqy5QJrzRiASDbNYLXPmlk8gBXlgBmUNW7mgc jT58v9SkyXrsfttVAqmjM3RgLoh0DKhbLVdDXTzycvX8UtLaDAFbExPaohyH1tYZO8 rxf2g5tCqDXgQRuVMVFSe1jwD4RYr5TA6As3k7F61CxcIw1oEcPsbEVxrX+haWgEKf Frq8HF6AaJoFBSJPaUeQgElSdcFQJOHb+dce+wPAt5IOSYDT+7Q5Dz0RyOZChELply 942YOxGd70waIcaHJMnGPP64kITgX8h20/wDjiPouiyN47SgkrrFMBWTC4q7KTu1dA tH/0RF8VOFEhg== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4TqLj24D3Nz4wcN; Wed, 6 Mar 2024 16:34:30 +1100 (AEDT) From: David Gibson To: Stefano Brivio , passt-dev@passt.top Subject: [PATCH v2 4/6] udp: Re-order udp_update_hdr[46] for clarity and brevity Date: Wed, 6 Mar 2024 16:34:26 +1100 Message-ID: <20240306053428.1176129-5-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240306053428.1176129-1-david@gibson.dropbear.id.au> References: <20240306053428.1176129-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: S6F74CNKHRSB4N6XHDSL4L6FDZMJPHMZ X-Message-ID-Hash: S6F74CNKHRSB4N6XHDSL4L6FDZMJPHMZ 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: The order of things in these functions is a bit odd for historical reasons. We initialise some IP header fields early, the more later after making some tests. Likewise we declare some variables without initialisation, but then unconditionally set them to values we could calculate at the start of the function. Previous cleanups have removed the reasons for some of these choices, so reorder for clarity, and where possible move the first assignment into an initialiser. Signed-off-by: David Gibson --- udp.c | 40 ++++++++++++++-------------------------- 1 file changed, 14 insertions(+), 26 deletions(-) diff --git a/udp.c b/udp.c index e20f537a..d3b9f5f9 100644 --- a/udp.c +++ b/udp.c @@ -574,17 +574,9 @@ static size_t udp_update_hdr4(const struct ctx *c, struct udp4_l2_buf_t *b, in_port_t dstport, size_t datalen, const struct timespec *now) { - const struct in_addr *src; - in_port_t srcport; - size_t ip_len; - - ip_len = datalen + sizeof(b->iph) + sizeof(b->uh); - - b->iph.tot_len = htons(ip_len); - b->iph.daddr = c->ip4.addr_seen.s_addr; - - src = &b->s_in.sin_addr; - srcport = ntohs(b->s_in.sin_port); + size_t ip_len = datalen + sizeof(b->iph) + sizeof(b->uh); + const struct in_addr *src = &b->s_in.sin_addr; + in_port_t srcport = ntohs(b->s_in.sin_port); if (!IN4_IS_ADDR_UNSPECIFIED(&c->ip4.dns_match) && IN4_ARE_ADDR_EQUAL(src, &c->ip4.dns_host) && srcport == 53) { @@ -603,10 +595,13 @@ static size_t udp_update_hdr4(const struct ctx *c, struct udp4_l2_buf_t *b, src = &c->ip4.gw; } - b->iph.saddr = src->s_addr; + b->iph.tot_len = htons(ip_len); + b->iph.daddr = c->ip4.addr_seen.s_addr; + b->iph.saddr = src->s_addr; b->iph.check = csum_ip4_header(b->iph.tot_len, IPPROTO_UDP, *src, c->ip4.addr_seen); + b->uh.source = b->s_in.sin_port; b->uh.dest = htons(dstport); b->uh.len = htons(datalen + sizeof(b->uh)); @@ -628,19 +623,10 @@ static size_t udp_update_hdr6(const struct ctx *c, struct udp6_l2_buf_t *b, in_port_t dstport, size_t datalen, const struct timespec *now) { - const struct in6_addr *src, *dst; - uint16_t payload_len; - in_port_t srcport; - size_t ip_len; - - dst = &c->ip6.addr_seen; - src = &b->s_in6.sin6_addr; - srcport = ntohs(b->s_in6.sin6_port); - - ip_len = datalen + sizeof(b->ip6h) + sizeof(b->uh); - - payload_len = datalen + sizeof(b->uh); - b->ip6h.payload_len = htons(payload_len); + const struct in6_addr *src = &b->s_in6.sin6_addr; + const struct in6_addr *dst = &c->ip6.addr_seen; + uint16_t payload_len = datalen + sizeof(b->uh); + in_port_t srcport = ntohs(b->s_in6.sin6_port); if (IN6_IS_ADDR_LINKLOCAL(src)) { dst = &c->ip6.addr_ll_seen; @@ -674,6 +660,8 @@ static size_t udp_update_hdr6(const struct ctx *c, struct udp6_l2_buf_t *b, src = &c->ip6.addr_ll; } + + b->ip6h.payload_len = htons(payload_len); b->ip6h.daddr = *dst; b->ip6h.saddr = *src; b->ip6h.version = 6; @@ -688,7 +676,7 @@ static size_t udp_update_hdr6(const struct ctx *c, struct udp6_l2_buf_t *b, proto_ipv6_header_psum(payload_len, IPPROTO_UDP, src, dst)); - return tap_iov_len(c, &b->taph, ip_len); + return tap_iov_len(c, &b->taph, payload_len + sizeof(b->ip6h)); } /** -- 2.44.0