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 038515A0272 for ; Mon, 5 Feb 2024 07:21:01 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202312; t=1707114057; bh=h+ZZodTSkTNC1JHht/r4cB9AImiFRGGEYX4tRsxEaA8=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=LLhOO2jWUdhHaR5TuZnJmPyAocE+fX7bAbqDh2COfskktSLxT2Ao+UlGz1Y2H+m4z m1iLpHnVf2c2BmX+odVYdJz3lQiDpSC95c1xwrL9f3rXBXUsZLCIuWszoryLShjbE2 47Zme02vL7LBS33rfuHuAn0CShe8oTvpGz7rXJtQrpfodlzU0w3wQFWnLax8YeyFct wKpUDryvq19JJqmgDwhztmoXbOPT9XyVfygMcUUk4T39tiALrc7Z+/fygpqv21DhY3 KaixNMxshFnQg8CVfdsoYFE8eAzs5KbCMB6A8VAJSlvf/l2AFBkfTqgX3KGKftFIQ8 dSsO/ZhbImJTQ== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4TSx8T6xHcz4wyb; Mon, 5 Feb 2024 17:20:57 +1100 (AEDT) Date: Mon, 5 Feb 2024 17:20:49 +1100 From: David Gibson To: Laurent Vivier Subject: Re: [PATCH 07/24] ip: introduce functions to compute the header part checksum for TCP/UDP Message-ID: References: <20240202141151.3762941-1-lvivier@redhat.com> <20240202141151.3762941-8-lvivier@redhat.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="AjMGm6rwlb2YVi1u" Content-Disposition: inline In-Reply-To: <20240202141151.3762941-8-lvivier@redhat.com> Message-ID-Hash: AYBXLNTNHJTF45ECT7VB32MJGAMNKVN5 X-Message-ID-Hash: AYBXLNTNHJTF45ECT7VB32MJGAMNKVN5 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: passt-dev@passt.top 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: --AjMGm6rwlb2YVi1u Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Fri, Feb 02, 2024 at 03:11:34PM +0100, Laurent Vivier wrote: > The TCP and UDP checksums are computed using the data in the TCP/UDP > payload but also some informations in the IP header (protocol, > length, source and destination addresses). >=20 > We add two functions, proto_ipv4_header_checksum() and > proto_ipv6_header_checksum(), to compute the checksum of the IP > header part. >=20 > Signed-off-by: Laurent Vivier > --- > ip.h | 24 ++++++++++++++++++++++++ > tcp.c | 40 +++++++++++++++------------------------- > udp.c | 6 ++---- > 3 files changed, 41 insertions(+), 29 deletions(-) >=20 > diff --git a/ip.h b/ip.h > index ff7902c45a95..87cb8dd21d2e 100644 > --- a/ip.h > +++ b/ip.h > @@ -97,4 +97,28 @@ static inline uint16_t ipv4_hdr_checksum(struct iphdr = *iph, int proto) > =20 > return ~csum_fold(sum); > } > + Function comment. > +static inline uint32_t proto_ipv4_header_checksum(struct iphdr *iph, int= proto) Logic looks fine, but I don't love the name, since this isn't a complete checksum, but just an intermediate result we use to compute the final checksum. Maybe use the 'psum' name we have in some other places for "partial sum". > +{ > + uint32_t sum =3D htons(proto); > + > + sum +=3D (iph->saddr >> 16) & 0xffff; > + sum +=3D iph->saddr & 0xffff; > + sum +=3D (iph->daddr >> 16) & 0xffff; > + sum +=3D iph->daddr & 0xffff; > + sum +=3D htons(ntohs(iph->tot_len) - 20); > + > + return sum; > +} > + > +static inline uint32_t proto_ipv6_header_checksum(struct ipv6hdr *ip6h, > + int proto) > +{ > + uint32_t sum =3D htons(proto) + ip6h->payload_len; > + > + sum +=3D sum_16b(&ip6h->saddr, sizeof(ip6h->saddr)); > + sum +=3D sum_16b(&ip6h->daddr, sizeof(ip6h->daddr)); > + > + return sum; > +} > #endif /* IP_H */ > diff --git a/tcp.c b/tcp.c > index 293ab12d8c21..2fd6bc2eda53 100644 > --- a/tcp.c > +++ b/tcp.c > @@ -938,39 +938,25 @@ static void tcp_sock_set_bufsize(const struct ctx *= c, int s) > * tcp_update_check_tcp4() - Update TCP checksum from stored one > * @buf: L2 packet buffer with final IPv4 header > */ > -static void tcp_update_check_tcp4(struct tcp4_l2_buf_t *buf) > +static uint16_t tcp_update_check_tcp4(struct iphdr *iph) > { > - uint16_t tlen =3D ntohs(buf->iph.tot_len) - 20; > - uint32_t sum =3D htons(IPPROTO_TCP); > + struct tcphdr *th =3D (void *)(iph + 1); > + uint16_t tlen =3D ntohs(iph->tot_len) - 20; > + uint32_t sum =3D proto_ipv4_header_checksum(iph, IPPROTO_TCP); > =20 > - sum +=3D (buf->iph.saddr >> 16) & 0xffff; > - sum +=3D buf->iph.saddr & 0xffff; > - sum +=3D (buf->iph.daddr >> 16) & 0xffff; > - sum +=3D buf->iph.daddr & 0xffff; > - sum +=3D htons(ntohs(buf->iph.tot_len) - 20); > - > - buf->th.check =3D 0; > - buf->th.check =3D csum(&buf->th, tlen, sum); > + return csum(th, tlen, sum); > } > =20 > /** > * tcp_update_check_tcp6() - Calculate TCP checksum for IPv6 > * @buf: L2 packet buffer with final IPv6 header > */ > -static void tcp_update_check_tcp6(struct tcp6_l2_buf_t *buf) > +static uint16_t tcp_update_check_tcp6(struct ipv6hdr *ip6h) > { > - int len =3D ntohs(buf->ip6h.payload_len) + sizeof(struct ipv6hdr); > - > - buf->ip6h.hop_limit =3D IPPROTO_TCP; > - buf->ip6h.version =3D 0; > - buf->ip6h.nexthdr =3D 0; > + struct tcphdr *th =3D (void *)(ip6h + 1); > + uint32_t sum =3D proto_ipv6_header_checksum(ip6h, IPPROTO_TCP); > =20 > - buf->th.check =3D 0; > - buf->th.check =3D csum(&buf->ip6h, len, 0); > - > - buf->ip6h.hop_limit =3D 255; > - buf->ip6h.version =3D 6; > - buf->ip6h.nexthdr =3D IPPROTO_TCP; > + return csum(th, ntohs(ip6h->payload_len), sum); > } > =20 > /** > @@ -1381,7 +1367,7 @@ do { \ > =20 > SET_TCP_HEADER_COMMON_V4_V6(b, conn, seq); > =20 > - tcp_update_check_tcp4(b); > + b->th.check =3D tcp_update_check_tcp4(&b->iph); > =20 > tlen =3D tap_iov_len(c, &b->taph, ip_len); > } else { > @@ -1400,7 +1386,11 @@ do { \ > =20 > SET_TCP_HEADER_COMMON_V4_V6(b, conn, seq); > =20 > - tcp_update_check_tcp6(b); > + b->th.check =3D tcp_update_check_tcp6(&b->ip6h); > + > + b->ip6h.hop_limit =3D 255; > + b->ip6h.version =3D 6; > + b->ip6h.nexthdr =3D IPPROTO_TCP; > =20 > b->ip6h.flow_lbl[0] =3D (conn->sock >> 16) & 0xf; > b->ip6h.flow_lbl[1] =3D (conn->sock >> 8) & 0xff; > diff --git a/udp.c b/udp.c > index 6f867df81c05..96b4e6ca9a85 100644 > --- a/udp.c > +++ b/udp.c > @@ -669,10 +669,8 @@ static size_t udp_update_hdr6(const struct ctx *c, i= nt n, in_port_t dstport, > b->uh.source =3D b->s_in6.sin6_port; > b->uh.dest =3D htons(dstport); > b->uh.len =3D b->ip6h.payload_len; > - > - b->ip6h.hop_limit =3D IPPROTO_UDP; > - b->ip6h.version =3D b->ip6h.nexthdr =3D b->uh.check =3D 0; > - b->uh.check =3D csum(&b->ip6h, ip_len, 0); > + b->uh.check =3D csum(&b->uh, ntohs(b->ip6h.payload_len), > + proto_ipv6_header_checksum(&b->ip6h, IPPROTO_UDP)); > b->ip6h.version =3D 6; > b->ip6h.nexthdr =3D IPPROTO_UDP; > b->ip6h.hop_limit =3D 255; --=20 David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson --AjMGm6rwlb2YVi1u Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAEBCAAdFiEEO+dNsU4E3yXUXRK2zQJF27ox2GcFAmXAfkAACgkQzQJF27ox 2GdjrhAAkPxmXueseFD7TXHObeqVGT64HcYaYZP4utOQV6KGZXSYV25atHtieszO rtyffmaPi03PEMtCSs1oEHTwPNEwPI3sQAXMJuUbOGhjKHxOGel8s0pp2faswNPg aTvPmYTBAVxA6CHTwR1qt7RwCEvoUyvwSn9VtuKXeva7fhcaYbSKDQQ9nf5mXCCq DieLKkHnsoOjP5uMovXJQAW3eDkviepMxNb9L6ahWvuf7Ywkqnd9fcyt7JpuR9uY olcLwc0OSO+2pdy8cpmOaZIOS+MXlt4wiLYj8t7PwqZE8JVx4d4oDg2NaThbySMh LNvClMAutbt9tFNsAPJqMPap+c06yWIs7YlRQrbTO6EMHAAIbJIUE4t9aHBpL4ZS QNbM+EMY/DBd7kjHDYnOOIQiUH8FmVvk0Xe8Xu513k+qYvMkB5UgGb1ZJdASSzKs O6sHiD4p6vJ8qZILsIkoXScrikraHbKTAVWC89Yo9vtSdke9H3q7/0ocbSGRzA5t 2SJFuDRxMecbN9d0tOkcEKrOMGAJYvwlmKUNfkUASasYxkQ0G3FExQpukihOW0qw 8gJ60K0rcCRuQiA1qg4/F8AEO/RXJnGywyMe8eacE98ChyfnjPC/7lkVpylh9BrH BYvdRC9cHk8rl1X7WPztCoc+OiNmiTdG7q0k8Eg+8DRyGNEheew= =zR27 -----END PGP SIGNATURE----- --AjMGm6rwlb2YVi1u--