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:
Ones Complement (flipped):
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.
- Perform Binary Addition: Add the two 16-bit numbers together just as you would in normal binary arithmetic.
- 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:
How the Checksum is Calculated (Sender's Side)
The sender of a packet follows a simple four-step process to generate the checksum.
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 (). 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.
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.
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.
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.
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.
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.
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: , , and .
On the Sender's Side
- The sender adds the data words together using ones complement addition:Sum is 0101 with a carry of 11101+1000(1)0101Wrap the carry aroundIntermediate sum+10110Final sum. No carry this time0110+01101100
- The final sum is .
- The sender calculates the checksum by inverting the final sum: .
- The sender transmits the original data followed by the checksum: .
On the Receiver's Side (No Errors)
- The receiver gets the four words () and adds them all together:This is the sum of the first three words, calculated aboveAdd the checksum1100+00111111
- The final sum is (all ones). The receiver concludes the packet is free of errors and accepts it.
On the Receiver's Side (With an Error)
- Imagine the first word was corrupted during transmission and became .
- The receiver now adds: .After end-around carry → 01011100+1000(1)01000101+011010111011+00111110
- The final sum is , 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
- Zero checksum field (checksum field = 0)
- Add all 4-bit words, wrapping carries
- Invert all bits of the sum
- Insert result into checksum field
Receiver Verification
- Receiver adds all words including checksum
- Wraps any carries (end-around carry)
- All ones → ok; otherwise → error
Addition Steps (Ones Complement with End-Around Carry)
| # | Prev Sum | + Word | After Add | Carry | After Wrap |
|---|---|---|---|---|---|
| 1 | 0000 | 1101 | 1101 | 0 | 1101 |
| 2 | 1101 | 1000 | 0101 | 1 | 0110 |
| 3 | 0110 | 0110 | 1100 | 0 | 1100 |
Receiver Verification
| # | Add | After Add | Carry | After Wrap |
|---|---|---|---|---|
| 1 | 1101 | 1101 | 0 | 1101 |
| 2 | 1000 | 0101 | 1 | 0110 |
| 3 | 0110 | 1100 | 0 | 1100 |
| 4 | 0011 | 1111 | 0 | 1111 |