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 AAC4A5A0268 for ; Wed, 16 Nov 2022 05:42:24 +0100 (CET) Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4NBr4R3Fthz4xwy; Wed, 16 Nov 2022 15:42:15 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=201602; t=1668573735; bh=TVjZLWIH0h4w0V7Tod4lH8aMHMQuXYdDt3ClfhTz5hQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=l8/NsBtaWNSFnHihKfbktVgflZQmCv+xhaP1cg/LspwpW225epGZpqk6gmMcaKBUf 3Ss0V8qaHYPmQ4Aa7Jksx7CDD/xssbQBI3sRJzmSf6RlUsPsWShFDnF27gVrg/l464 lfNHMpTYolxUMwBgBaYszcOxy31HG9XQ3UfwN6so= From: David Gibson To: passt-dev@passt.top, Stefano Brivio Subject: [PATCH 23/32] tcp: Unify initial sequence number calculation for IPv4 and IPv6 Date: Wed, 16 Nov 2022 15:42:03 +1100 Message-Id: <20221116044212.3876516-24-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221116044212.3876516-1-david@gibson.dropbear.id.au> References: <20221116044212.3876516-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: G6WD75NA4PCZTPBNRTMCCLJ6H7EGB6TI X-Message-ID-Hash: G6WD75NA4PCZTPBNRTMCCLJ6H7EGB6TI 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.3 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: tcp_seq_init() has separate paths for IPv4 and IPv6 addresses, which means we will calculate different sequence numbers for IPv4 and equivalent IPv4-mapped IPv6 addresses. Change it to treat these the same by always converting the input address into an inany_addr representation and use that to calculate the sequence number. Signed-off-by: David Gibson --- siphash.c | 1 + tcp.c | 46 ++++++++++++++++++---------------------------- 2 files changed, 19 insertions(+), 28 deletions(-) diff --git a/siphash.c b/siphash.c index 516a508..811918b 100644 --- a/siphash.c +++ b/siphash.c @@ -123,6 +123,7 @@ uint64_t siphash_8b(const uint8_t *in, const uint64_t *k) * * Return: 32 bits obtained by XORing the two halves of the 64-bit hash output */ +/* cppcheck-suppress unusedFunction */ uint32_t siphash_12b(const uint8_t *in, const uint64_t *k) { uint32_t *in32 = (uint32_t *)in; diff --git a/tcp.c b/tcp.c index f566060..d4e9838 100644 --- a/tcp.c +++ b/tcp.c @@ -1942,37 +1942,27 @@ static uint32_t tcp_seq_init(const struct ctx *c, int af, const void *addr, in_port_t dstport, in_port_t srcport, const struct timespec *now) { + union inany_addr aany; + struct { + union inany_addr src; + in_port_t srcport; + union inany_addr dst; + in_port_t dstport; + } __attribute__((__packed__)) in = { + .srcport = srcport, + .dstport = dstport, + }; uint32_t ns, seq = 0; - if (af == AF_INET) { - struct { - struct in_addr src; - in_port_t srcport; - struct in_addr dst; - in_port_t dstport; - } __attribute__((__packed__)) in = { - .src = *(struct in_addr *)addr, - .srcport = srcport, - .dst = c->ip4.addr, - .dstport = dstport, - }; - - seq = siphash_12b((uint8_t *)&in, c->tcp.hash_secret); - } else if (af == AF_INET6) { - struct { - struct in6_addr src; - in_port_t srcport; - struct in6_addr dst; - in_port_t dstport; - } __attribute__((__packed__)) in = { - .src = *(struct in6_addr *)addr, - .srcport = srcport, - .dst = c->ip6.addr, - .dstport = dstport, - }; + inany_from_af(&aany, af, addr); + in.src = aany; + if (af == AF_INET) + inany_from_af(&aany, AF_INET, &c->ip4.addr); + else + inany_from_af(&aany, AF_INET6, &c->ip6.addr); + in.dst = aany; - seq = siphash_36b((uint8_t *)&in, c->tcp.hash_secret); - } + seq = siphash_36b((uint8_t *)&in, c->tcp.hash_secret); ns = now->tv_sec * 1E9; ns += now->tv_nsec >> 5; /* 32ns ticks, overflows 32 bits every 137s */ -- 2.38.1