TCP/IP Checksum

The ones' complement method for error detection in transport and network layers.

Why Do We Need Error Detection? The Postcard Analogy

Imagine sending a handwritten postcard to a friend. Several things could go wrong during its journey. Rain could smudge the ink, making the address unreadable. A mail sorting machine could tear a corner. The message itself could be altered, changing the meaning of a sentence. When your friend receives it, they might notice something is amiss, a blurred word, a ripped edge, and realize the message might not be exactly what you wrote.

Data traveling across the internet faces similar perils. The digital "postcard," a , traverses a long and complex path through cables, routers, and switches. Along the way, electrical interference, hardware malfunctions, or software bugs can randomly flip a bit from a 0 to a 1, or a 1 to a 0. This is called data corruption.

A single flipped bit can have catastrophic consequences. It could change a number in a financial transaction, corrupt an instruction in a software update, or create a glitch in a video stream. To combat this, network protocols need a mechanism to check if the data they received is the same as the data that was sent. The TCP/IP Internet Checksum is a simple and fast method used in protocols like TCP, UDP, and IP to perform this basic integrity check. It acts as a quick "smudge test" for our digital postcards.

Core Concept: Ones Complement Arithmetic

The Internet Checksum algorithm is based on a specific type of binary math called ones complement arithmetic. To understand it, we need to cover two simple operations. For all our examples, we will assume we are working with 16-bit numbers, as this is the standard size for the checksum.

1. The Ones Complement (Bitwise Inversion)

The ones complement of a binary number is simply its bitwise inverse. You take the number and flip every single bit: every 0 becomes a 1, and every 1 becomes a 0. This is the final step in calculating the checksum value.

Example:

Original 16-bit number:

10110010 1100101010110010\ 11001010

Ones Complement (flipped):

01001101 0011010101001101\ 00110101

2. Ones Complement Addition with End-Around Carry

This is the heart of the checksum calculation. It looks like standard binary addition but with one special rule.

  1. Perform Binary Addition: Add the two 16-bit numbers together just as you would in normal binary arithmetic.
  2. Handle the Carry Bit (The Special Rule): If the sum of the 16-bit numbers is larger than what 16 bits can hold, a 17th bit, called a , is generated. In many forms of arithmetic, this bit is discarded. However, in ones complement addition, this carry bit is wrapped around and added back to the least significant bit (the rightmost bit) of the result. This is called an end-around carry.

Example of End-Around Carry:

A 16-bit sum of 0, with a carry
11111111 11110000 (65520)
+00000000 00010000 (16)
(1)
00000000 00000000
Wrap the carry bit around
Final result: 1
+1
00000000 00000001

How the Checksum is Calculated (Sender's Side)

The sender of a packet follows a simple four-step process to generate the checksum.

  1. Step 1: Prepare the Data

    The sender takes all the data to be protected (for example, the TCP header plus the application data). A crucial part of this step is to set the checksum field within the header to all zeros (00000000 0000000000000000\ 00000000). This is because the checksum field itself is part of the data being summed, so it must start with a known, neutral value.

    If the total length of the data is an odd number of bytes, a temporary "padding" byte of all zeros is added to the end for calculation purposes. This ensures the data can be perfectly divided into 16-bit words. This padding byte is not actually transmitted with the packet.

  2. Step 2: Sum the Data

    The data is divided into 16-bit words. The sender then adds all of these words together using ones complement addition, making sure to wrap around any carry bits as described before. This process continues until all 16-bit words have been added into a single 16-bit sum.

  3. Step 3: Invert the Sum

    The final 16-bit sum from Step 2 is inverted by taking its ones complement (flipping all the bits). The resulting number is the Internet Checksum.

  4. Step 4: Insert the Checksum

    The sender places this calculated checksum value back into the now-empty checksum field in the packet header. The packet is now complete and ready for transmission across the network.

How the Checksum is Verified (Receiver's Side)

The elegance of the ones complement checksum lies in how easy it is to verify. The receiver does not need to know what the original checksum was; it just performs a single calculation on the entire received packet.

  1. Step 1: Receive the Packet

    The receiver gets the packet from the network. The packet includes the data as well as the checksum calculated by the sender, which is stored in the checksum header field.

  2. Step 2: Sum Everything

    The receiver performs the exact same process as the sender: it divides the entire received data (including the header with the sender's checksum) into 16-bit words and adds them all together using ones complement addition with end-around carry.

  3. Step 3: Check the Result

    After summing all the parts, the receiver looks at the final 16-bit sum. The magic of ones complement arithmetic ensures that:

    If there were no errors during transmission, the final sum at the receiver's end will be a 16-bit word consisting of all ones (11111111 11111111, or 0xFFFF in hexadecimal).

    If the final sum is anything other than all ones, the receiver knows that the packet was corrupted in transit. It immediately discards the packet, assuming that a higher-level protocol (like TCP) will handle the retransmission.

Worked Example with 4-bit Words

To make the concept clearer, let’s use the simplified 4-bit word example from your notes. Assume the data to be sent consists of three 4-bit words: 11011101, 10001000, and 01100110.

On the Sender's Side

  1. The sender adds the data words together using ones complement addition:
    Sum is 0101 with a carry of 1
    1101
    +1000
    (1)
    0101
    Wrap the carry around
    Intermediate sum
    +1
    0110
    Final sum. No carry this time
    0110
    +0110
    1100
  2. The final sum is 11001100.
  3. The sender calculates the checksum by inverting the final sum: checksum=∼1100=0011\text{checksum} = \sim 1100 = 0011.
  4. The sender transmits the original data followed by the checksum: 1101,1000,0110,00111101, 1000, 0110, 0011.

On the Receiver's Side (No Errors)

  1. The receiver gets the four words (1101,1000,0110,00111101, 1000, 0110, 0011) and adds them all together:
    This is the sum of the first three words, calculated above
    Add the checksum
    1100
    +0011
    1111
  2. The final sum is 11111111 (all ones). The receiver concludes the packet is free of errors and accepts it.

On the Receiver's Side (With an Error)

  1. Imagine the first word 11011101 was corrupted during transmission and became 11001100.
  2. The receiver now adds: 1100+1000+0110+00111100 + 1000 + 0110 + 0011.
    After end-around carry → 0101
    1100
    +1000
    (1)
    0100
    0101
    +0110
    1011
    1011
    +0011
    1110
  3. The final sum is 11101110, which is not all ones. The receiver detects the error and discards the packet.

Important Limitations of the Internet Checksum

The Internet Checksum is designed for speed, not perfection. It's a simple algorithm that can be implemented very efficiently in software, which was crucial in the early days of networking when processor cycles were precious. However, its simplicity comes with limitations.

The checksum is not a cryptographically secure hash function. It can fail to detect several common types of errors:

  • If two 16-bit words are swapped, the final sum remains the same.
  • If a bit in one word is flipped from 0 to 1, and a corresponding bit in another word is flipped from 1 to 0, the errors can cancel each other out, and the checksum will still verify correctly.
  • Inserting or deleting words that contain all zeros may also go undetected.

This might seem like a major flaw, but it's an acceptable trade-off. The reason is that stronger error detection mechanisms, like the , are already applied at the Data Link Layer (e.g., by Ethernet). The Internet Checksum provides an extra, lightweight layer of protection against errors that might be introduced inside routers, where the link-layer header (and its CRC) is stripped off and reassembled for the next hop. It's a fast, "good enough" check for its specific role in the network stack.

Interactive Internet Checksum (4-bit demo)

Enter 4-bit words. We compute the ones complement checksum (demo scale) and let you verify transmission.

Sender Process

  1. Zero checksum field (checksum field = 0)
  2. Add all 4-bit words, wrapping carries
  3. Invert all bits of the sum
  4. Insert result into checksum field

Receiver Verification

  1. Receiver adds all words including checksum
  2. Wraps any carries (end-around carry)
  3. All ones → ok; otherwise → error

Addition Steps (Ones Complement with End-Around Carry)

#Prev Sum+ WordAfter AddCarryAfter Wrap
100001101110101101
211011000010110110
301100110110001100
Computed Checksum
0011
Checksum inserted (ready for transmission)
[...] 0011

Receiver Verification

#AddAfter AddCarryAfter Wrap
11101110101101
21000010110110
30110110001100
40011111101111
Result is all ones (1111) – packet accepted (no detected error).
    TCP/IP Checksum | Teleinf Edu