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 405675A026A for ; Tue, 18 Oct 2022 10:40:10 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1666082409; 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=YH19c3uGpvZVB7kU9Oz4cIDUP54tk1B7a82DHbjq9fY=; b=M21XxkqgvJkqrj1B4WpFlDKFjvH043mB80mfjdUviF5UX3E47FCsgyhVwqFsuiOVcc6sXp Tm0jKgwDdlyn369NgaGypsDNoUHknwQn87fImUT1qg9aCfKorPB3MEOEjaxtcE40KSuJC/ nBSrlqgl1aHjwZ5iIJYYmoL0mBDimio= 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-505-JkOkcRZVMt-W0SgFQSOv8g-1; Tue, 18 Oct 2022 04:40:05 -0400 X-MC-Unique: JkOkcRZVMt-W0SgFQSOv8g-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 60E05186E39A; Tue, 18 Oct 2022 08:40:05 +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 2CA7FC15BA5; Tue, 18 Oct 2022 08:40:05 +0000 (UTC) Date: Tue, 18 Oct 2022 05:01:51 +0200 From: Stefano Brivio To: David Gibson Subject: Re: [PATCH 02/14] Add csum_icmp4() helper for calculating ICMPv4 checksums Message-ID: <20221018050151.5739f1ad@elisabeth> In-Reply-To: <20221017085807.473470-3-david@gibson.dropbear.id.au> References: <20221017085807.473470-1-david@gibson.dropbear.id.au> <20221017085807.473470-3-david@gibson.dropbear.id.au> Organization: Red Hat MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Message-ID-Hash: 4QMJMV3IMWKQYPMD3IWULOBMHTZUMT2G X-Message-ID-Hash: 4QMJMV3IMWKQYPMD3IWULOBMHTZUMT2G 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:55 +1100 David Gibson wrote: > Although tap_ip_send() is currently the only place calculating ICMPv4 > checksums, create a helper function for symmetry with ICMPv6. For future > flexibility it allows the ICMPv6 header and payload to be in separate > buffers. > > Signed-off-by: David Gibson > --- > checksum.c | 15 +++++++++++++++ > checksum.h | 2 ++ > tap.c | 4 +--- > 3 files changed, 18 insertions(+), 3 deletions(-) > > diff --git a/checksum.c b/checksum.c > index 0e207c8..c8b6b42 100644 > --- a/checksum.c > +++ b/checksum.c > @@ -52,6 +52,7 @@ > #include > #include > > +#include > #include > > /** > @@ -107,6 +108,20 @@ uint16_t csum_unaligned(const void *buf, size_t len, uint32_t init) > return (uint16_t)~csum_fold(sum_16b(buf, len) + init); > } > > +/** > + * csum_icmp4() - Calculate checksum for an ICMPv4 packet "Calculate and set"? By the way, there's no such thing as ICMPv4 -- it's ICMP. > + * @icmp4hr: ICMPv4 header, initialized apart from checksum ...-ised, if you respin. For consistency, I would call this 'ih'. > + * @payload: ICMPv4 packet payload > + * @len: Length of @payload (not including ICMPv4 header) > + */ > +void csum_icmp4(struct icmphdr *icmp4hr, const void *payload, size_t len) I guess csum_icmp() is preferable. Indeed, for TCP and UDP 'tcp4' and 'udp4' make sense because those are the same protocols over IPv4 and IPv6. > +{ > + /* Partial checksum for ICMPv4 header alone */ > + uint32_t hrsum = sum_16b(icmp4hr, sizeof(*icmp4hr)); A white line would be nice here. I would call this psum (same as in csum_icmp6()) or hdrsum, 'hr' isn't really used for "header" elsewhere. > + icmp4hr->checksum = 0; > + icmp4hr->checksum = csum_unaligned(payload, len, hrsum); > +} > + > /** > * csum_icmp6() - Calculate checksum for an ICMPv6 packet > * @icmp6hr: ICMPv6 header, initialized apart from checksum > diff --git a/checksum.h b/checksum.h > index 2c72200..ff95cf9 100644 > --- a/checksum.h > +++ b/checksum.h > @@ -6,11 +6,13 @@ > #ifndef CHECKSUM_H > #define CHECKSUM_H > > +struct icmphdr; > 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_icmp4(struct icmphdr *ih, const void *payload, size_t len); > 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 aafc92b..f082901 100644 > --- a/tap.c > +++ b/tap.c > @@ -148,9 +148,7 @@ void tap_ip_send(const struct ctx *c, const struct in6_addr *src, uint8_t proto, > uh->check = 0; > } else if (iph->protocol == IPPROTO_ICMP) { > struct icmphdr *ih = (struct icmphdr *)(iph + 1); > - > - ih->checksum = 0; > - ih->checksum = csum_unaligned(ih, len, 0); > + csum_icmp4(ih, ih + 1, len - sizeof(*ih)); > } > > if (tap_send(c, buf, len + sizeof(*iph) + sizeof(*eh), 1) < 0) -- Stefano