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 7E0C35A0271 for ; Fri, 1 Mar 2024 08:59:26 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1709279965; 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=fwNPnhkkLfgn6Pkl/Oi/JyASoKrIElfZQOlI4bWKBeA=; b=Zpfq1yVeJe+JlfZSWjYTAPFvC4g9NsCJZeG5GXVvl0ES3Eock0Q4zqFVB9rqrhn2RoruaM GAVtKPrIifbvjZtZc2mcon9ZJK/cEJ/EEduFpM67yxOEqAsTkQKOouzlkb3sonxcD8R5HZ sAD4zzgCdJi33Wiue79VS+MG4pUtHWk= Received: from mail-ej1-f71.google.com (mail-ej1-f71.google.com [209.85.218.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-308-_uv893oHM1mlZphHECZOEg-1; Fri, 01 Mar 2024 02:59:23 -0500 X-MC-Unique: _uv893oHM1mlZphHECZOEg-1 Received: by mail-ej1-f71.google.com with SMTP id a640c23a62f3a-a431a294ebeso134785966b.0 for ; Thu, 29 Feb 2024 23:59:23 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709279962; x=1709884762; h=content-transfer-encoding:mime-version:organization:references :in-reply-to:message-id:subject:cc:to:from:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=fwNPnhkkLfgn6Pkl/Oi/JyASoKrIElfZQOlI4bWKBeA=; b=QRM43VrXVBL9TnFreWpEVZFKKs7Mq2oTO/Fye86n+7ycEn/DwXnv+yR+3nEgSu+ML8 9WYjdJ0K0acPdrSv/TLWzEXRB0x290nur3f3Gh4xD/PvR44cONBibQCN3oA1Kq9egB1G +FH8KuhKhhkINm7IeNXhqQ1meH40A2s3GAdVnkhR5gqduZNvzYFQLzUSVTpQqhvDNwzm B5KSXGhUFTKJIns0oWb+HBdBD8uFMuUnNzE+TPvd9kSDzOO/S+bP0qWtr2yJy8lNkkrv MeO8FCiwkDObkHIsbKxS90OEYrGYFR9ZZMa3DObGu6yn6TCjMUqELP5eU2s5aI+QYbKi 0yuA== X-Forwarded-Encrypted: i=1; AJvYcCWjCWu6BoPrifz2uN+EIlyPOubhnmaJKPY7OMTOKL3dlzEliw+6JMjhmmdTGfVs1pN/LP382eEuT/CPXDzMM9Wz9d7+ X-Gm-Message-State: AOJu0YwCaWK7l5Wzqb5qPFoIf0G29h4cClL4u6Q8Cy9/oM35n/OmA6zT YzUXCJRKKaxO05VNej2Zl58itPcBPRNz2xsjev8Zp1rM+He+BRErVddomEWqYNVc5/BC70UFSKP A/16GCtm4JqH74LJqpmKQBIFViHWwy+tLbZc6oWadaGtbHwxqfw== X-Received: by 2002:a17:906:2650:b0:a3d:2243:29da with SMTP id i16-20020a170906265000b00a3d224329damr675532ejc.36.1709279962324; Thu, 29 Feb 2024 23:59:22 -0800 (PST) X-Google-Smtp-Source: AGHT+IHr+rwFhwXb001nU3953MlOO/WEBbewSdTr/uazUM7rJBOgJ0+zwuKuo4s+ajnBh7g7L8k7VQ== X-Received: by 2002:a17:906:2650:b0:a3d:2243:29da with SMTP id i16-20020a170906265000b00a3d224329damr675518ejc.36.1709279961894; Thu, 29 Feb 2024 23:59:21 -0800 (PST) Received: from maya.cloud.tilaa.com (maya.cloud.tilaa.com. [164.138.29.33]) by smtp.gmail.com with ESMTPSA id t9-20020a17090616c900b00a42e4b5ab00sm1445693ejd.41.2024.02.29.23.59.21 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 29 Feb 2024 23:59:21 -0800 (PST) Date: Fri, 1 Mar 2024 08:58:45 +0100 From: Stefano Brivio To: David Gibson Subject: Re: [PATCH v3 6/9] checksum: use csum_ip4_header() in udp.c and tcp.c Message-ID: <20240301085845.0da98048@elisabeth> In-Reply-To: References: <20240217150725.661467-1-lvivier@redhat.com> <20240217150725.661467-7-lvivier@redhat.com> <20240229172406.6a8331f1@elisabeth> Organization: Red Hat X-Mailer: Claws Mail 4.2.0 (GTK 3.24.36; x86_64-pc-linux-gnu) MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Message-ID-Hash: 7VBOWJCJZRHRGU4SY7HX2XNOTI5WV6ZJ X-Message-ID-Hash: 7VBOWJCJZRHRGU4SY7HX2XNOTI5WV6ZJ 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: Laurent Vivier , 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: On Fri, 1 Mar 2024 10:10:52 +1100 David Gibson wrote: > On Thu, Feb 29, 2024 at 05:24:06PM +0100, Stefano Brivio wrote: > > On Sat, 17 Feb 2024 16:07:22 +0100 > > Laurent Vivier wrote: > > > > > We can find the same function to compute the IPv4 header > > > checksum in tcp.c, udp.c and tap.c > > > > > > Use the function defined for tap.c, csum_ip4_header(), but > > > with the code used in tcp.c and udp.c as it doesn't need a fully > > > initialiazed IPv4 header, only protocol, tot_len, saddr and daddr. > > > > > > Signed-off-by: Laurent Vivier > > > --- > > > > > > Notes: > > > v3: > > > - function parameters provide tot_len, saddr, daddr and protocol > > > rather than an iphdr > > > > > > v2: > > > - use csum_ip4_header() from checksum.c > > > - use code from tcp.c and udp.c in csum_ip4_header() > > > - use "const struct iphfr *", check is not updated by the > > > function but by the caller. > > > > > > checksum.c | 17 +++++++++++++---- > > > checksum.h | 3 ++- > > > tap.c | 3 ++- > > > tcp.c | 24 +++--------------------- > > > udp.c | 20 ++------------------ > > > 5 files changed, 22 insertions(+), 45 deletions(-) > > > > > > diff --git a/checksum.c b/checksum.c > > > index 74e3742bc6f6..511b296a9a80 100644 > > > --- a/checksum.c > > > +++ b/checksum.c > > > @@ -57,6 +57,7 @@ > > > #include > > > > > > #include "util.h" > > > +#include "ip.h" > > > #include "checksum.h" > > > > > > /* Checksums are optional for UDP over IPv4, so we usually just set > > > @@ -116,13 +117,21 @@ uint16_t csum_fold(uint32_t sum) > > > uint16_t csum(const void *buf, size_t len, uint32_t init); > > > > > > /** > > > - * csum_ip4_header() - Calculate and set IPv4 header checksum > > > + * csum_ip4_header() - Calculate IPv4 header checksum > > > * @ip4h: IPv4 header > > > */ > > > -void csum_ip4_header(struct iphdr *ip4h) > > > +uint16_t csum_ip4_header(uint16_t tot_len, uint8_t protocol, > > > + uint32_t saddr, uint32_t daddr) > > > { > > > - ip4h->check = 0; > > > - ip4h->check = csum(ip4h, (size_t)ip4h->ihl * 4, 0); > > > + uint32_t sum = L2_BUF_IP4_PSUM(protocol); > > > > Now that we use this macro, Coverity Scan realises that it's broken: > > > > #define L2_BUF_IP4_PSUM(proto) ((uint32_t)htons_constant(0x4500) + \ > > (uint32_t)htons_constant(0xff00 | (proto))) > > > > ...but proto is eight (lower) bits, so this actually ignores 'proto'. > > Uh... how so? Oops, sorry, it's not broken, and this is a false positive due to the fact that __bswap_constant_16() (which htons_constant() resolves to, on little-endian) is defined, for example in glibc, as: #define __bswap_constant_16(x) \ ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8)) and in this case the first term of the | resolves to a constant value, 0xff, because 0xffxx >> 8 is 0xff for any value of xx. I couldn't think of a "solution", yet. -- Stefano