RFC 1141 INFORMATIONAL IP · CHECKSUM

Incremental Checksum: Incremental updating of the Internet checksum

Shows how to update an Internet checksum after changing a packet field.

On rfc-editor.org ↗
PUBLISHED
January 1990
AUTHORS
T. Mallory, A. Kullberg
STREAM
Legacy
BUILDS ON
RFC 1071 — Internet Checksum
UPDATED BY
RFC 1624 — Computation of the Internet Checksum via Incremental Update
ARITHMETIC
One's-complement addition
TTL DECREASE BY ONE
Add 1 or 256 to checksum field
DOI
10.17487/RFC1141
ERRATA
Reported errata

RFC text

Contents
Original text · RFC Editor View on rfc-editor.org ↗
Network Working Group                                         T. Mallory
Request for Comments: 1141                                   A. Kullberg
Obsoletes: RFC 1071                                   BBN Communications
                                                            January 1990


             Incremental Updating of the Internet Checksum

Status of this Memo

   This memo correctly describes the incremental update procedure for
   use with the standard Internet checksum.  It is intended to replace
   the description of Incremental Update in RFC 1071.  This is not a
   standard but rather, an implementation technique.  Distribution of
   this memo is unlimited.

Description

   In RFC 1071 on pages 4 and 5, there is a description of a method to
   update the IP checksum in the IP header without having to completely
   recompute the checksum.  In particular, the RFC recommends the
   following equation for computing the update checksum C' from the
   original checksum C, and the old and new values of byte m:

         C' = C + (-m) + m' = C + (m' - m)

   While the equation above is correct, it is not very useful for
   incremental updates since the equation above updates the checksum C,
   rather than the 1's complement of the checksum, ~C, which is the
   value stored in the checksum field.  In addition, it suffers because
   the notation does not clearly specify that all arithmetic, including
   the unary negation, must be performed one's complement, and so is
   difficult to use to build working code.  The useful calculation for
   2's complement machines is:

         ~C' = ~(C + (-m) + m') = ~C + (m - m') = ~C + m + ~m'

   In the oft-mentioned case of updating the IP TTL field, subtracting
   one from the TTL means ADDING 1 or 256 as appropriate to the checksum
   field in the packet, using one's complement addition.  One big-endian
   non-portable implementation in C looks like:

      unsigned long sum;
      ipptr->ttl--;                  /* decrement ttl */
      sum = ipptr->Checksum + 0x100;  /* increment checksum high byte*/
      ipptr->Checksum = (sum + (sum>>16)) /* add carry */

   This special case can be optimized in many ways: for instance, you
   can bundle updating and checking the ttl.  Compiler mileage may vary.
   Here is a more general and possibly more helpful example which
   updates the ttl by n seconds:

      UpdateTTL(iph,n)
      struct ip_hdr *ipptr;
      unsigned char n;
      {
          unsigned long sum;
          unsigned short old;

          old = ntohs(*(unsigned short *)&ipptr->ttl);
          ipptr->ttl -= n;
          sum = old + (~ntohs(*(unsigned short *)&ipptr->ttl) & 0xffff);
          sum += ntohs(ipptr->Checksum);
          sum = (sum & 0xffff) + (sum>>16);
          ipptr->Checksum = htons(sum + (sum>>16));
      }

Security Considerations

   Security issues are not addressed in this memo.

Authors' Addresses

   Tracy Mallory
   BBN Communications Corporation
   50 Moulton Street
   Cambridge, MA 02238

   Phone: (617) 873-3193

   EMail: tmallory@CCV.BBN.COM


   A. Kullberg
   BBN Communications Corporation
   50 Moulton Street
   Cambridge, MA 02238

   Phone: (617) 873-4000

   EMail:  akullberg@BBN.COM

ISO Plain Language

On this page
PROBLEMRecomputing an entire IP checksum is unnecessary when only one field changes, but the earlier update equation was difficult to apply directly to the stored checksum.
MECHANISMThe memo gives an update rule for the complement stored in the checksum field, using one's-complement arithmetic, and illustrates it with TTL changes.
STATUS TODAYThis is an implementation technique, not a standard. It is updated by RFC 1624, according to the unit metadata.

§1Why an Incremental Rule Helps

RFC 1071 describes updating an IP checksum when a field changes, instead of recomputing the entire checksum. This memo says the earlier equation updates the checksum value itself, while the packet stores its one's complement.

The earlier notation also did not make clear that all arithmetic, including negation, uses one's-complement arithmetic. The memo restates the calculation in a form that can be applied to the stored field.

The packet stores the complement of the checksum, so an update rule needs to work with that stored value. This memo also makes clear which arithmetic to use.

§1Updating the Stored Checksum

The memo gives the update rule as ~C' = ~C + m + ~m', using one's-complement addition. Here, the old and new values of the changed field adjust the complemented checksum value.

For the common case where an IP TTL decreases by one, the checksum field is adjusted by adding 1 or 256, as appropriate, with one's-complement addition.

Adjust the stored checksum using the old and new field values, with one's-complement addition. For a one-step TTL decrease, the memo says to add 1 or 256 as appropriate.

§1TTL Code Examples

The RFC shows one non-portable big-endian C example for decreasing the TTL by one and adding 0x100 to the checksum. It also gives a more general routine that decreases the TTL by a chosen amount and folds carries back into the result.

The memo says the special case can be optimized in different ways and notes that compiler behavior may vary.

The examples update a checksum when the TTL changes. One handles a decrease by one, and another handles a specified decrease.

§2Security Considerations

The memo states that it does not address security issues. It describes only a calculation for updating an existing Internet checksum after a field changes.

This memo explains checksum arithmetic only. It does not discuss security issues.