Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp2413499rdb; Fri, 8 Dec 2023 07:29:31 -0800 (PST) X-Google-Smtp-Source: AGHT+IFiYkDNQ2XqG0QUwsmh4CUN1Nr9/+XF6W56jowdvT9T5q0rbpwg4ICVoSNWM8JLmzCIIIee X-Received: by 2002:a05:6a00:139d:b0:6ce:643d:98c2 with SMTP id t29-20020a056a00139d00b006ce643d98c2mr219692pfg.3.1702049371553; Fri, 08 Dec 2023 07:29:31 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702049371; cv=none; d=google.com; s=arc-20160816; b=evMOCQTRM5YcxzM+RhENDAZWy2F8PlK3Z/9TTOT+jhnhnPHIC79h3smGGsSedLIzQW dZAe1Q2VeNMMhDLI2zEu99aMu8l1ewXZOfh0s7jxcXcymE/kAysgGPB1Hw/oaCcq5OE/ zPq1xGdxFvHI60Rm3+s2YQZzqDEA61BZNFRIaT8rO9zTVJ6bZhIuNbp2CbYRGZ76rVio yCUWR24++dcyfu3cwnK+KI6URdyo2NAgK6S5/5WWHBwA3woO+Di1aA91TwLOoR9K4DzY 5mWVzXkn9K1ABKpd56LcDgISamJTB1O6x9adc1hb+vuUcZRZrp6VuopF6GpvoUqaFDM/ 5LSg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=zys2RTvczhHBrTilI40fr1weYiUwJQ9jBZynoNtrbro=; fh=ldq3c3V9Ed5GG5JaCS5XviCv1ZZL1NK27GlE9n5V6X0=; b=qdBQtB2B2FBE437vnnuylmxYMLtPf47EyUrf4ydizpq7PCdEVnZofw+rR8kpLzprj+ yqF1Gc7oeBQbFH3yGtwCv6qYB5QwdT+cTqyN5RgwUgcMRSLSv87tBc/xOk/+V7KsCBNm NGTNElCjZPMmILtHs/VDsphCMiBqCXD/SGYXMsfBC7G7JZcw/1oV/+EOzM39j2GLqK8S 7VO7LF0sYQaebs5LkaZIMNdDU/q+rPbkgyyRtZz4uGoR7MDyGYKNyZIvqGmojypzW6g+ x8wI5gz8JVwIwUjsmO0uw3Ek9ZIh5tRaJ+/c7F49UcSk6bxNQSI5kXPuK9lmU5NfcLEU tp3A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.org.uk header.s=zeniv-20220401 header.b=A7AsbWlP; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=zeniv.linux.org.uk Return-Path: Received: from morse.vger.email (morse.vger.email. [23.128.96.31]) by mx.google.com with ESMTPS id bz39-20020a056a02062700b005c5e24c7e8csi1862286pgb.414.2023.12.08.07.29.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Dec 2023 07:29:31 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) client-ip=23.128.96.31; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.org.uk header.s=zeniv-20220401 header.b=A7AsbWlP; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=zeniv.linux.org.uk Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id 2A1C8837CF0C; Fri, 8 Dec 2023 07:29:29 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233524AbjLHP3M (ORCPT + 99 others); Fri, 8 Dec 2023 10:29:12 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38236 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233549AbjLHP3L (ORCPT ); Fri, 8 Dec 2023 10:29:11 -0500 Received: from zeniv.linux.org.uk (zeniv.linux.org.uk [IPv6:2a03:a000:7:0:5054:ff:fe1c:15ff]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B63D6FB; Fri, 8 Dec 2023 07:29:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=linux.org.uk; s=zeniv-20220401; h=Sender:In-Reply-To:Content-Type: MIME-Version:References:Message-ID:Subject:Cc:To:From:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=zys2RTvczhHBrTilI40fr1weYiUwJQ9jBZynoNtrbro=; b=A7AsbWlPHxXQuLZcVgdzdcgsCq Y/uiZuV7gP6bvdnUaEsdtkwIgNjhyuu02fcT//c4pnl4MeS/r+Mm/PDUGhhus/EuoDCAeLPvFpK5s Tw+qMxHxrDr3Th8nUVj/gCpINgflX6CWbhc/jFZBFETBT2Q+uJdlIru/CtlacS2a+8ul+p8DOhd4P v6km0MPmNvFExOmqDKge9HyAnO+u33zS5nAHz+N83SKdo49Gb8JJ4KfHlNFM7fz5fnVCfXkeIEPbs LXfqKLMYmLGLctp4U/k+XPBATy10kNBMxYVFu+VFjnST2raLHBaemEfP8CTH8whxx6Wf9JSG+KPTv /A4WmzXA==; Received: from viro by zeniv.linux.org.uk with local (Exim 4.96 #2 (Red Hat Linux)) id 1rBcmu-008w3V-2p; Fri, 08 Dec 2023 15:29:12 +0000 Date: Fri, 8 Dec 2023 15:29:12 +0000 From: Al Viro To: David Laight Cc: "linux-arch@vger.kernel.org" , gus Gusenleitner Klaus , Al Viro , Thomas Gleixner , lkml , Ingo Molnar , "bp@alien8.de" , "dave.hansen@linux.intel.com" , "x86@kernel.org" , "David S. Miller" , "dsahern@kernel.org" , "kuba@kernel.org" , Paolo Abeni , Eric Dumazet Subject: Re: [RFC][PATCHES v2] checksum stuff Message-ID: <20231208152912.GB1674809@ZenIV> References: <20231019080615.GY800259@ZenIV> <20231021071525.GA789610@ZenIV> <20231021222203.GA800259@ZenIV> <20231022194020.GA972254@ZenIV> <20231205022100.GB1674809@ZenIV> <602ab11ffa2c4cc49bb9ecae2f0540b0@AcuMS.aculab.com> <20231206224359.GR1674809@ZenIV> <46711b57a62348059cfe798c8acea941@AcuMS.aculab.com> <20231208141712.GA1674809@ZenIV> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20231208141712.GA1674809@ZenIV> Sender: Al Viro X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Fri, 08 Dec 2023 07:29:29 -0800 (PST) On Fri, Dec 08, 2023 at 02:17:12PM +0000, Al Viro wrote: > On Fri, Dec 08, 2023 at 12:04:24PM +0000, David Laight wrote: > > I've just read RFC 792 and done some experiments. > > The kernel ICMP checksum code is just plain broken. > > > > RFC 792 is quite clear that the checksum is the 16-bit ones's > > complement of the one's complement sum of the ICMP message > > starting with the ICMP Type. > > > > The one's complement sum of 0xfffe and 0x0001 is zero (not the 0xffff > > It is not. FYI, N-bit one's complement sum is defined as > > X + Y <= MAX_N_BIT ? X + Y : X + Y - MAX_N_BIT, > > where MAX_N_BIT is 2^N - 1. > > You add them as natural numbers. If there is no carry and result > fits into N bits, that's it. If there is carry, you add it to > the lower N bits of sum. > > Discussion of properties of that operation is present e.g. in > RFC1071, titled "Computing the Internet Checksum". > > May I politely suggest that some basic understanding of the > arithmetics involved might be useful for this discussion? FWIW, "one's complement" in the name is due to the fact that this operation is how one normally implements addition of integers in one's complement representation. The operation itself is on bit vectors - you take two N-bit vectors, pad them with 0 on the left, add them as unsigned N+1-bit numbers, then add the leftmost bit (carry) to the value in the remaining N bits. Since the sum on the first step is no greater than 2^{N+1} - 2, the result of the second addition will always fit into N bits. If bit vectors and represent integers x and y with representable sum (i.e. if 2^{N-1} > x + y > -2^{N-1}), then applying this operation will produce a vector representing x + y. In case when the sum allows more than one representation (i.e. when x + y is 0), it is biased towards negative zero - the only way to get positive zero is (+0) + (+0); in particular, your example is (+1) + (-1) yielding (-0). Your bit vectors are 1111111111111110 and 0000000000000001; padding gives 01111111111111110 and 00000000000000001; the first addition - 01111111111111111, so the carry bit is 0 and result is the lower 16 bits, i.e. 1111111111111111. Had the second argument been 0000000000000011 (+3), you would get 10000000000000001 from adding padded vectors, with carry bit being 1. So the result would be 1 + 0000000000000001, i.e. 0000000000000010 ((+2), from adding (-1) and (+3)). References to 1's complement integers aside, the operation above is what is used as basis for checksum calculations. Reasons and properties are dealt with in IEN 45 (older than RFC 791/792 - TCP design is older than IP, and that's where the choice of checksum had been originally dealt with) and in RFC 1071 (which includes IEN 45 as appendix, noting that it has not been widely available).