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 857E05A0276 for ; Mon, 1 May 2023 13:08:10 +0200 (CEST) Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4Q90n058mHz4x3y; Mon, 1 May 2023 21:08:04 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=201602; t=1682939284; bh=ytS3b+ZcKdv+qgIKR9xaa1qDZJ/MhRtaGMMrm1v3zaI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=G/nLIhIs8J5EvX9WoW1PmZ3InExaUYS8EuIJrPNg1dwkAwJ5KVM/58INZWqYTZ5IW O5dBpriqkylydl5laNohRelltKnqZ8yi/ewTZHukkx3ELimQE+XN0dqsYJhlnawWwh 1GvD0s8gV9/oIe38lGBhpRXnpwhHWT+yCKFZCyMA= From: David Gibson To: Stefano Brivio , passt-dev@passt.top Subject: [PATCH 1/7] udp: Simplify setting of source IPv6 address for inbound packets Date: Mon, 1 May 2023 21:06:56 +1000 Message-Id: <20230501110702.3915529-2-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230501110702.3915529-1-david@gibson.dropbear.id.au> References: <20230501110702.3915529-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: VEMPBOJXY3B76LPIS6YODMXHGPT35L4R X-Message-ID-Hash: VEMPBOJXY3B76LPIS6YODMXHGPT35L4R 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: Depending on the original source address, udp_update_hdr6() can take one of several paths to handle the cases where we need NAT or other adjustments. In each path we explicitly set the source IPv6 address in the packet buffer. Simplify this slightly to update src to point to the corrected source address in each branch, then have a common write to the packet buffer. This will enable future cleanups. To make this work we do need a slightly re-order to avoid making tests on the update 'src' when we need to test the original source. Signed-off-by: David Gibson --- udp.c | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/udp.c b/udp.c index 39c59d4..361f24c 100644 --- a/udp.c +++ b/udp.c @@ -632,35 +632,26 @@ static size_t udp_update_hdr6(const struct ctx *c, int n, in_port_t dstport, const struct timespec *now) { struct udp6_l2_buf_t *b = &udp6_l2_buf[n]; - struct in6_addr *src; - in_port_t src_port; + in_port_t src_port = ntohs(b->s_in6.sin6_port); + const struct in6_addr *src = &b->s_in6.sin6_addr; size_t ip_len; - src = &b->s_in6.sin6_addr; - src_port = ntohs(b->s_in6.sin6_port); - ip_len = udp6_l2_mh_sock[n].msg_len + sizeof(b->ip6h) + sizeof(b->uh); b->ip6h.payload_len = htons(udp6_l2_mh_sock[n].msg_len + 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; } 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; @@ -675,11 +666,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; bitmap_set(udp_act[V6][UDP_ACT_TAP], src_port); + + if (IN6_IS_ADDR_LINKLOCAL(&c->ip6.gw)) + src = &c->ip6.gw; + else + src = &c->ip6.addr_ll; } else { b->ip6h.daddr = c->ip6.addr_seen; - b->ip6h.saddr = b->s_in6.sin6_addr; } + b->ip6h.saddr = *src; + b->uh.source = b->s_in6.sin6_port; b->uh.dest = htons(dstport); b->uh.len = b->ip6h.payload_len; -- 2.40.1