NetBSD Problem Report #5380

Received: (qmail 17404 invoked from network); 1 May 1998 04:00:39 -0000
Message-Id: <199805010408.VAA11068@skank.juniper.net>
Date: Thu, 30 Apr 1998 21:08:02 -0700 (PDT)
From: dennis@juniper.net
Reply-To: dennis@juniper.net
To: gnats-bugs@gnats.netbsd.org
Subject: bad incremental checksum fixup in netinet/ip_flow.c
X-Send-Pr-Version: 3.95

>Number:         5380
>Category:       kern
>Synopsis:       bad incremental checksum fixup in netinet/ip_flow.c
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    kern-bug-people
>State:          closed
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Thu Apr 30 21:05:00 +0000 1998
>Closed-Date:    Mon May 04 05:39:18 +0000 1998
>Last-Modified:  Mon May 04 05:39:29 +0000 1998
>Originator:     Dennis Ferguson
>Release:        NetBSD-current April 30 1998
>Organization:
Juniper Networks
>Environment:

>Description:
In netinet/ip_flow.c, the following code fragment is used to fix up
the header checksum after a ttl decrement:

	ip->ip_ttl -= IPTTLDEC;
#if BYTE_ORDER == LITTLE_ENDIAN
	sum = ip->ip_sum + IPTTLDEC;
#endif
#if BYTE_ORDER == BIG_ENDIAN
	sum = ip->ip_sum + (IPTTLDEC << 8);
	sum = (sum & 0xFFFF) + (sum >> 16);
#endif
	if (sum > 0x10000)		/* add in carry if needed */
		sum++;
	ip->ip_sum = sum;		/* bit 16 is dropped */

This is bad in a number of ways.  Assuming IPTTLDEC is 1 (though
the problems are independent of this):

(1) If the incoming checksum is 0xfffe on a little endian machine,
    or 0xfeff on a big endian machine, the code will generate a
    checksum of 0xffff.  While this is not strictly incorrect, note
    that in_cksum() always avoids this and will always produce the
    equivalent 0x0000 in this case.  The reason it is poor form to
    generate a 0xffff header checksum is that it can cause systems
    which do the header checksum check by recomputing it and comparing
    to the value in the header to fail.

(2) If the incoming checksum is 0xffff the updated checksum will be
    incorrect in the little endian case.  The updated checksum will
    be 0x0000, when it should be 0x0001.

(3) The `if (sum > 0x10000) sum++;' does nothing in the big endian
    case and fails to prevent incorrect computations in the little
    endian case.

(4) The big endian case compiles to more instructions than it needs
    to.

(5) The #if's are unnecessary given an htons() macro which compiles
    to a constant when called with a constant argument.

The same problems exist for IPTTLDEC other than 1, only the numbers
change.


>How-To-Repeat:

>Fix:
Given an htons() macro which compiles to a constant with a constant
argument the following code fragment avoids generating a 0xffff
checksum, produces a correct checksum in all cases, avoids the #if's,
and does the operation with a single comparison and addition on
machines of either byte order.

	ip->ip_ttl -= IPTTLDEC;
	if (ip->ip_sum >= htons(0xffff - (IPTTLDEC << 8))) {
		ip->ip_sum += htons(IPTTLDEC << 8) + 1;
	} else {
		ip->ip_sum += htons(IPTTLDEC << 8);
	}

(The same technique can be used to update the ICMP checksum in
an ICMP echo reply, with IPTTLDEC replaced by 3).
>Release-Note:
>Audit-Trail:

From: "Charles M. Hannum" <mycroft@mit.edu>
To: dennis@juniper.net
Cc: gnats-bugs@gnats.netbsd.org
Subject: Re: kern/5380: bad incremental checksum fixup in netinet/ip_flow.c
Date: Fri, 1 May 1998 00:31:32 -0400 (EDT)

 > (1) If the incoming checksum is 0xfffe on a little endian machine,
 >     or 0xfeff on a big endian machine, the code will generate a
 >     checksum of 0xffff.  While this is not strictly incorrect, note
 >     that in_cksum() always avoids this and will always produce the
 >     equivalent 0x0000 in this case.  The reason it is poor form to
 >     generate a 0xffff header checksum is that it can cause systems
 >     which do the header checksum check by recomputing it and comparing
 >     to the value in the header to fail.

 That's actually incorrect.  in_cksum() should generate 0xffff rather
 than 0x0000.  This is because a value of 0x0000 is used in UDP packets
 to specify that no checksum is present.

 > (2) If the incoming checksum is 0xffff the updated checksum will be
 >     incorrect in the little endian case.  The updated checksum will
 >     be 0x0000, when it should be 0x0001.
 >
 > (3) The `if (sum > 0x10000) sum++;' does nothing in the big endian
 >     case and fails to prevent incorrect computations in the little
 >     endian case.

 Both of these are actually because the comparison should have used
 `>=' rather than `>'.

 >         ip->ip_ttl -= IPTTLDEC;
 >         if (ip->ip_sum >= htons(0xffff - (IPTTLDEC << 8))) {
 >                 ip->ip_sum += htons(IPTTLDEC << 8) + 1;
 >         } else {
 >                 ip->ip_sum += htons(IPTTLDEC << 8);
 >         }

 This looks correct, except that the comparision should use `>', so
 that it generates 0xffff rather than 0x0000.


From: "Charles M. Hannum" <mycroft@mit.edu>
To: dennis@juniper.net
Cc: gnats-bugs@gnats.netbsd.org
Subject: Re: kern/5380: bad incremental checksum fixup in netinet/ip_flow.c
Date: Fri, 1 May 1998 00:42:22 -0400 (EDT)

 Hmmm.  Actually, I see that this is a special case in the UDP output
 code.  Gag.

 Looking at the code, it's pretty clear that in_cksum() may generate
 either 0xffff or 0x0000.  It doesn't really make a difference in the
 IP header, of course.

 So, in short, your code is fine.  B-)


From: "Charles M. Hannum" <mycroft@mit.edu>
To: dennis@juniper.net
Cc: gnats-bugs@gnats.netbsd.org
Subject: Re: kern/5380: bad incremental checksum fixup in netinet/ip_flow.c
Date: Fri, 1 May 1998 00:50:15 -0400 (EDT)

 Hmmm.  One more thing...

 Some things about in_cksum() occur to me:

 * The code that verifies the checksums simply checks to see that 0 is
   returned.  If 0xffff was returned, the comparison would fail and the
   packet would be rejected.

 * in_cksum() will return 0xffff iff the input was all 0s.  In practice
   all IP headers and TCP or UDP payloads *must* have a non-0 byte
   somewhere, so this isn't a bug per se, but it's kind of gross.

 * I could fix this at least in the i386 version by initializing the
   checksum to all-1s rather than all-0s.

 Any thoughts on this?  Should I do it?


From: Jeremy Cooper <jeremy@broder.com>
To: dennis@juniper.net
Cc: gnats-bugs@gnats.netbsd.org
Subject: Re: kern/5380: bad incremental checksum fixup in netinet/ip_flow.c
Date: Thu, 30 Apr 1998 21:57:29 -0700 (PDT)

 Please tell me all parties have read RFC 1141.


From: Dennis Ferguson <dennis@juniper.net>
To: "Charles M. Hannum" <mycroft@mit.edu>
Cc: gnats-bugs@gnats.netbsd.org
Subject: Re: kern/5380: bad incremental checksum fixup in netinet/ip_flow.c 
Date: Thu, 30 Apr 1998 23:33:35 -0700

 > Hmmm.  One more thing...
 > 
 > Some things about in_cksum() occur to me:
 > 
 > * The code that verifies the checksums simply checks to see that 0 is
 >   returned.  If 0xffff was returned, the comparison would fail and the
 >   packet would be rejected.
 > 
 > * in_cksum() will return 0xffff iff the input was all 0s.  In practice
 >   all IP headers and TCP or UDP payloads *must* have a non-0 byte
 >   somewhere, so this isn't a bug per se, but it's kind of gross.
 > 
 > * I could fix this at least in the i386 version by initializing the
 >   checksum to all-1s rather than all-0s.
 > 
 > Any thoughts on this?  Should I do it?

 No, I think the most conservative thing to do is leave it the way it
 is.  The checksum 0xffff should never appear except in the very special
 case that the rest of the data being checksummed is zero.  I think this
 matches the most literal rendering of the definition in RFC 791, that is

       The checksum field is the 16 bit one's complement of the one's  
       complement sum of all 16 bit words in the header.  For purposes of
       computing the checksum, the value of the checksum field is zero.

 Initalizing the checksum to all-1's would be equivalent to setting the
 checksum field to all-1's for the purposes of the computation, but the
 above explicitly chooses to set it to zero instead.  This explicitly
 makes all-0's a special case.  Also note that RFC 1071 says

    (2)  To generate a checksum, the checksum field itself is cleared,
         the 16-bit 1's complement sum is computed over the octets
         concerned, and the 1's complement of this sum is placed in the
         checksum field.

    (3)  To check a checksum, the 1's complement sum is computed over the
         same set of octets, including the checksum field.  If the result
         is all 1 bits (-0 in 1's complement arithmetic), the check
         succeeds.

 (3) implies that all-zero's data needs an all-one's checksum, and that
 one's complement summation will indeed produce an all-one's value (which
 gives you all-0's after the final complement), which makes (2)'s algorithm
 give you exactly what in_cksum() does.

 I agree that this is a hack, but I've seen at least one TCP which did
 checksum comparisons by recomputing the checksum and comparing it to the
 one in the header (I think on a 3com router, about 5 years ago), which
 only works if everyone is very anal about doing it exactly the way that
 is described.  I think in_cksum() works about right the way it is.

 Dennis Ferguson

From: mycroft@mit.edu (Charles M. Hannum)
To: Dennis Ferguson <dennis@juniper.net>
Cc: gnats-bugs@gnats.netbsd.org
Subject: Re: kern/5380: bad incremental checksum fixup in netinet/ip_flow.c
Date: 01 May 1998 04:22:02 -0400

 Dennis Ferguson <dennis@juniper.net> writes:

 > 
 > No, I think the most conservative thing to do is leave it the way it
 > is.  The checksum 0xffff should never appear except in the very special
 > case that the rest of the data being checksummed is zero.

 Which, as I said, is a case that can't ever happen in practice.  It
 doesn't matter; it's just interesting to note the ambiguity.

State-Changed-From-To: open->closed 
State-Changed-By: thorpej 
State-Changed-When: Sun May 3 22:39:18 PDT 1998 
State-Changed-Why:  
Fixed as suggested. 
>Unformatted:

NetBSD Home
NetBSD PR Database Search

(Contact us) $NetBSD: query-full-pr,v 1.51 2026/08/10 02:28:17 riastradh Exp $
$NetBSD: gnats_config.sh,v 1.10 2026/05/13 22:00:09 riastradh Exp $
Copyright © 1994-2026 The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.