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.133.124]) by passt.top (Postfix) with ESMTP id 2793F5A0270 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=LuMsa+l3v2i9vlQ/dHQYxHKMoG19QcWssrtA9vIq+/o=; b=h0UzcoQlwLUv8j266Ebg7DM2Zz5Rzi3tGiWt6+7y5e4IVuyd53D8BHoux2ES9yxdoqKRgm xitQwfBR1se09XoTBgwfwLDgPpwDyXOit9SWIrY244Dugcj0EWiv3NXwvdWF+eZGXiNlU+ QRV3Gf4sod98/9GIDOBJzTWSvpmUe3M= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-518-Hw9bKLFgP_O482TzGvyPhQ-1; Tue, 18 Oct 2022 04:40:03 -0400 X-MC-Unique: Hw9bKLFgP_O482TzGvyPhQ-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 5D84F1C087A1; Tue, 18 Oct 2022 08:40:03 +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 23298215CDCA; Tue, 18 Oct 2022 08:40:03 +0000 (UTC) Date: Tue, 18 Oct 2022 05:03:09 +0200 From: Stefano Brivio To: David Gibson Subject: Re: [PATCH 04/14] Add csum_udp4() helper for calculating UDP over IPv4 checksums Message-ID: <20221018050309.70394436@elisabeth> In-Reply-To: <20221017085807.473470-5-david@gibson.dropbear.id.au> References: <20221017085807.473470-1-david@gibson.dropbear.id.au> <20221017085807.473470-5-david@gibson.dropbear.id.au> Organization: Red Hat MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.6 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Message-ID-Hash: IPGNXYDFBFQVNE7M56GO4SFJMAO24TYM X-Message-ID-Hash: IPGNXYDFBFQVNE7M56GO4SFJMAO24TYM 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:57 +1100 David Gibson wrote: > 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". > > Just for the heck of it, add the option (compile time disabled for now) to > calculate real UDP checksums. > > Signed-off-by: David Gibson > --- > checksum.c | 33 +++++++++++++++++++++++++++++++++ > checksum.h | 3 +++ > dhcp.c | 2 +- > tap.c | 2 +- > 4 files changed, 38 insertions(+), 2 deletions(-) > > diff --git a/checksum.c b/checksum.c > index 0849fb1..72f1cfb 100644 > --- a/checksum.c > +++ b/checksum.c > @@ -56,6 +56,11 @@ > #include > #include > > +/* 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 to 1 > + */ > +#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 len, uint32_t init) > return (uint16_t)~csum_fold(sum_16b(buf, len) + init); > } > > +/** > + * csum_udp4() - Calculate checksum for a UDP over IPv4 packet and set > + * @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 = 0; > + > + if (UDP4_REAL_CHECKSUMS) { > + /* UNTESTED: if we did want real UDPv4 checksums, this > + * is roughly what we'd need */ > + uint32_t psum = 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 += sum_16b(udp4hr, sizeof(*udp4hr)); > + udp4hr->check = 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); Horizontal space. -- Stefano