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 761B45A0282 for ; Thu, 17 Nov 2022 06:59:21 +0100 (CET) Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4NCTkl4DCkz4xyG; Thu, 17 Nov 2022 16:59:11 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=201602; t=1668664751; bh=zPOfvXL3lIrOiJGwil23eTdqxRFhkt3EufycjzIM9fs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=he3UZNL1fY7XmLrQNk7x1QYDXZyzlYb3fCn8w2c+snpPBzv2xfdFA3JpwKqkD+7in yjEZIw9pYWYwc1wQphqqs7S7Z2vasT8YuCzOybCCO+J3nDhBtB/zNYI8mwX8DR/pRT ZRGm0o7G53HjBA8bSk3keRutDAicrWUVwo6tt5jg= From: David Gibson To: passt-dev@passt.top, Stefano Brivio Subject: [PATCH v2 23/32] tcp: Unify initial sequence number calculation for IPv4 and IPv6 Date: Thu, 17 Nov 2022 16:58:59 +1100 Message-Id: <20221117055908.2782981-24-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221117055908.2782981-1-david@gibson.dropbear.id.au> References: <20221117055908.2782981-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: EJOQC6XYB5UD5EDU5SEEN3O7OJOWOSBE X-Message-ID-Hash: EJOQC6XYB5UD5EDU5SEEN3O7OJOWOSBE 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 8ebd90a..ac64b81 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