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 5D11E5A026A for ; Tue, 18 Oct 2022 14:08:13 +0200 (CEST) Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4MsCLH4byjz4xGl; Tue, 18 Oct 2022 23:08:07 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=201602; t=1666094887; bh=dRe1St1dZf7n+ENbzxwmmnX2ZqNgS8dlInU7XB/+VU4=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=qDlgD3V5ziCrqiGs8/S+z0B+tQIarbuzJ7x2Ahn9GYFdSPhUCjIJYItANQI88BSzv abRqBQNYBJVSqWw73+nTJlsM4OeXeyRzvFL3Qc7EZmxzWV3Sj1rbt6hnbjidMwx7Qp 4PfkomtIKTIhBCIWu3B6im3dqJrLALYCv4cTbzoE= Date: Tue, 18 Oct 2022 23:06:42 +1100 From: David Gibson To: Stefano Brivio Subject: Re: [PATCH 04/14] Add csum_udp4() helper for calculating UDP over IPv4 checksums Message-ID: References: <20221017085807.473470-1-david@gibson.dropbear.id.au> <20221017085807.473470-5-david@gibson.dropbear.id.au> <20221018050309.70394436@elisabeth> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="HT0H2cEFOOKjO+4l" Content-Disposition: inline In-Reply-To: <20221018050309.70394436@elisabeth> Message-ID-Hash: KBP7PXCIQSOHXU4BQQLRXA6HOAXIMRYW X-Message-ID-Hash: KBP7PXCIQSOHXU4BQQLRXA6HOAXIMRYW 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.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: --HT0H2cEFOOKjO+4l Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Tue, Oct 18, 2022 at 05:03:09AM +0200, Stefano Brivio wrote: 11;rgb:ffff/ffff/ffff> On Mon, 17 Oct 2022 19:57:57 +1100 > David Gibson wrote: >=20 > > At least two places in passt fill in UDP over IPv4 checksums, although > > since UDP checksums are optional with IPv4 that just amounts to storing > > a 0 (in tap_ip_send()) or leaving a 0 from an earlier initialization (in > > dhcp()). For consistency, add a helper for this "calculation". > >=20 > > Just for the heck of it, add the option (compile time disabled for now)= to > > calculate real UDP checksums. > >=20 > > Signed-off-by: David Gibson > > --- > > checksum.c | 33 +++++++++++++++++++++++++++++++++ > > checksum.h | 3 +++ > > dhcp.c | 2 +- > > tap.c | 2 +- > > 4 files changed, 38 insertions(+), 2 deletions(-) > >=20 > > diff --git a/checksum.c b/checksum.c > > index 0849fb1..72f1cfb 100644 > > --- a/checksum.c > > +++ b/checksum.c > > @@ -56,6 +56,11 @@ > > #include > > #include > > =20 > > +/* Checksums are optional for UDP over IPv4, so we usually just set > > + * them to 0. Change this 1 to calculate real UDP over IPv4 checksums >=20 > to 1 Done. > > + */ > > +#define UDP4_REAL_CHECKSUMS 0 > > + > > /** > > * sum_16b() - Calculate sum of 16-bit words > > * @buf: Input buffer > > @@ -109,6 +114,34 @@ uint16_t csum_unaligned(const void *buf, size_t le= n, uint32_t init) > > return (uint16_t)~csum_fold(sum_16b(buf, len) + init); > > } > > =20 > > +/** > > + * csum_udp4() - Calculate checksum for a UDP over IPv4 packet >=20 > and set Done. > > + * @udp4hr: UDP header, initialized apart from checksum > > + * @saddr: IPv4 source address > > + * @daddr: IPv4 destination address > > + * @payload: ICMPv4 packet payload > > + * @len: Length of @payload (not including UDP) > > + */ > > +void csum_udp4(struct udphdr *udp4hr, > > + in_addr_t saddr, in_addr_t daddr, > > + const void *payload, size_t len) > > +{ > > + /* UDP checksums are optional, so don't bother */ > > + udp4hr->check =3D 0; > > + > > + if (UDP4_REAL_CHECKSUMS) { > > + /* UNTESTED: if we did want real UDPv4 checksums, this > > + * is roughly what we'd need */ > > + uint32_t psum =3D csum_fold(htonl(saddr)) > > + + csum_fold(htonl(daddr)) > > + + htons(len + sizeof(*udp4hr)) > > + + htons(IPPROTO_UDP); > > + /* Add in partial checksum for the UDP header alone */ > > + psum +=3D sum_16b(udp4hr, sizeof(*udp4hr)); > > + udp4hr->check =3D csum_unaligned(payload, len, psum); > > + } > > +} > > + > > /** > > * csum_icmp4() - Calculate checksum for an ICMPv4 packet > > * @icmp4hr: ICMPv4 header, initialized apart from checksum > > diff --git a/checksum.h b/checksum.h > > index 1b9f48e..a9502b9 100644 > > --- a/checksum.h > > +++ b/checksum.h > > @@ -13,6 +13,9 @@ struct icmp6hdr; > > 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_udp4(struct udphdr *udp4hr, > > + in_addr_t saddr, in_addr_t daddr, > > + const void *payload, size_t len); >=20 > Horizontal space. Done. --=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 --HT0H2cEFOOKjO+4l Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAEBCAAdFiEEoULxWu4/Ws0dB+XtgypY4gEwYSIFAmNOlssACgkQgypY4gEw YSIaRA//ZwYN6hXQic3Pqi88DFMDyQjzJmKYo7C9Ebvm9/OUQp15sFbr+DVWL+nZ LXceNZeA9rPcNKdCXjttnv21kNbhJLNMxCBU2tjRMaOBysx9KJ/FFkpahPkp++Dd DXyLmL76bvGRG/akDOmyt35m7QTtKW4SoO3+q1Zu/eT2zJLB2DZrYJ0i0fxWOEYY 43q6N4Fzs88K8sttJh0Us1BvzzNx3VrMLH9ZLTpN1dHZFn0Wr/yiky5ERh97oKPO 2zZCDvFHdkNc8x9Xkp+Qr12zF4S00QSsrzeerwy9Vkbf0OALaWbHbU7C5PRReRCQ z6go1yVkkIu2xZEGgVR4lYYNkufOy9CHZBWOtdy6m3RaKIzIdFiEopoWrfbfg5ew RJBImLofKRxIwtJ33VoTVfFMRo/PFv1CEUeqEzaITSRTjuMq29Rbd6zqc8fiTAUL Zy1k7/99FB/E+ZpQDff9o2nTAQ0FLMF4t1NQEvsn08KnKj28g8OWED9GXfhJrFfN pbwjiQ5FMaqSAb5hYwC9YX1sKHrdswDTztpRxmS63gpe8YiWZsoTV5dn5/4FbOOo dZ+mL7JYe1v0MRHs0HHIn/udlXyuLWyDSZ0EPBVA76dbsqe9COfGn4pGcWRuAw8j TUtONPtVVRVSA3mp8DBTowmBCVdUZTy4zyA8jt16nGyBHKDs8ok= =y/Df -----END PGP SIGNATURE----- --HT0H2cEFOOKjO+4l--