From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by passt.top (Postfix) with ESMTP id B10DB5A0269 for ; Tue, 18 Oct 2022 10:40:08 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1666082407; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=YMc6LD9vzyWLB6IwHZoqakQTbO8tLriB+KDFwckAdsI=; b=YfmbX2XRQk6t2S4+hOKpMnv4VneYUBpR+3tz8G9nWl2+T+XGu/waHJxPGmgWNLg3avbW69 6K8a0EtaaGLa3zbx+y5v9dy7QCKpcSWF35z2L7ouLp4/9vnPEDV+v6sCRioWuqIn0+Uz+Y btDXb6XTKNTODNzr/vdx6Ox/qXhG6CU= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-437-3qwlP9SuOVqUYAR2SJNqtg-1; Tue, 18 Oct 2022 04:40:04 -0400 X-MC-Unique: 3qwlP9SuOVqUYAR2SJNqtg-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 4F0D0833A19; Tue, 18 Oct 2022 08:40:04 +0000 (UTC) Received: from maya.cloud.tilaa.com (ovpn-208-31.brq.redhat.com [10.40.208.31]) by smtp.corp.redhat.com (Postfix) with ESMTPS id E8256409AE1; Tue, 18 Oct 2022 08:40:03 +0000 (UTC) Date: Tue, 18 Oct 2022 05:02:31 +0200 From: Stefano Brivio To: David Gibson Subject: Re: [PATCH 03/14] Add csum_udp6() helper for calculating UDP over IPv6 checksums Message-ID: <20221018050231.22a9f79d@elisabeth> In-Reply-To: <20221017085807.473470-4-david@gibson.dropbear.id.au> References: <20221017085807.473470-1-david@gibson.dropbear.id.au> <20221017085807.473470-4-david@gibson.dropbear.id.au> Organization: Red Hat MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Message-ID-Hash: 3IQUE55S4X4M2THZAOWIUWVML2VB7T3I X-Message-ID-Hash: 3IQUE55S4X4M2THZAOWIUWVML2VB7T3I X-MailFrom: sbrivio@redhat.com 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: passt-dev@passt.top 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: On Mon, 17 Oct 2022 19:57:56 +1100 David Gibson wrote: > Add a helper for calculating UDP checksums when used over IPv6 > For future flexibility, the new helper takes parameters for the fields in > the IPv6 pseudo-header, so an IPv6 header or pseudo-header doesn't need to > be explicitly constructed. It also allows the UDP header and payload to > be in separate buffers, although we don't use this yet. > > Signed-off-by: David Gibson > --- > checksum.c | 23 +++++++++++++++++++++++ > checksum.h | 5 +++++ > tap.c | 5 ++--- > 3 files changed, 30 insertions(+), 3 deletions(-) > > diff --git a/checksum.c b/checksum.c > index c8b6b42..0849fb1 100644 > --- a/checksum.c > +++ b/checksum.c > @@ -52,6 +52,7 @@ > #include > #include > > +#include > #include > #include > > @@ -122,6 +123,28 @@ void csum_icmp4(struct icmphdr *icmp4hr, const void *payload, size_t len) > icmp4hr->checksum = csum_unaligned(payload, len, hrsum); > } > > +/** > + * csum_udp6() - Calculate checksum for a UDP over IPv6 packet Calculate and set. > + * @udp6hr: UDP header, initialized apart from checksum -ised. > + * @payload: UDP packet payload > + * @len: Length of @payload (not including UDP header) > + */ > +void csum_udp6(struct udphdr *udp6hr, > + const struct in6_addr *saddr, > + const struct in6_addr *daddr, You could use some horizontal space. > + const void *payload, size_t len) > +{ > + /* Partial checksum for the pseudo-IPv6 header */ > + uint32_t psum = sum_16b(saddr, sizeof(*saddr)) + > + sum_16b(daddr, sizeof(*daddr)) + > + htons(len + sizeof(*udp6hr)) + htons(IPPROTO_UDP); Alignment: uint32_t psum = sum_16b(saddr, sizeof(*saddr)) + sum_16b(daddr, sizeof(*daddr)) + htons(len + sizeof(*udp6hr)) + htons(IPPROTO_UDP); > + udp6hr->check = 0; > + /* Add in partial checksum for the UDP header alone */ > + psum += sum_16b(udp6hr, sizeof(*udp6hr)); > + udp6hr->check = csum_unaligned(payload, len, psum); > +} > + > /** > * csum_icmp6() - Calculate checksum for an ICMPv6 packet > * @icmp6hr: ICMPv6 header, initialized apart from checksum > diff --git a/checksum.h b/checksum.h > index ff95cf9..1b9f48e 100644 > --- a/checksum.h > +++ b/checksum.h > @@ -6,6 +6,7 @@ > #ifndef CHECKSUM_H > #define CHECKSUM_H > > +struct udphdr; > struct icmphdr; > struct icmp6hdr; > > @@ -13,6 +14,10 @@ uint32_t sum_16b(const void *buf, size_t len); > uint16_t csum_fold(uint32_t sum); > uint16_t csum_unaligned(const void *buf, size_t len, uint32_t init); > void csum_icmp4(struct icmphdr *ih, const void *payload, size_t len); > +void csum_udp6(struct udphdr *udp6hr, > + const struct in6_addr *saddr, > + const struct in6_addr *daddr, > + const void *payload, size_t len); Use some horizontal space. > void csum_icmp6(struct icmp6hdr *ih, > const struct in6_addr *saddr, > const struct in6_addr *daddr, > diff --git a/tap.c b/tap.c > index f082901..9c197cb 100644 > --- a/tap.c > +++ b/tap.c > @@ -183,9 +183,8 @@ void tap_ip_send(const struct ctx *c, const struct in6_addr *src, uint8_t proto, > } else if (proto == IPPROTO_UDP) { > struct udphdr *uh = (struct udphdr *)(ip6h + 1); > > - uh->check = 0; > - uh->check = csum_unaligned(ip6h, len + sizeof(*ip6h), > - 0); > + csum_udp6(uh, &ip6h->saddr, &ip6h->daddr, > + uh + 1, len - sizeof(*uh)); > } else if (proto == IPPROTO_ICMPV6) { > struct icmp6hdr *ih = (struct icmp6hdr *)(ip6h + 1); > -- Stefano