Sub topic 5
The Internet checksum
This chapter assumes you can add binary and hexadecimal numbers, know what XOR does, and have read the earlier chapters on parity and Hamming distance. The Internet checksum is the error check used in the IPv4 header, TCP, UDP and ICMP. It is weaker than a CRC but cheap to compute in software.
The idea
The sender treats the data as a sequence of 16-bit words, adds them, and stores a value in the packet that makes the receiver's sum come out to a fixed number. Two details make it work:
- Ones' complement addition. Add the words as ordinary integers. Whenever the sum carries out of bit 16, remove the carry and add it back at the bottom. This is called end-around carry.
- Complement at the end. The sender flips every bit of the final sum. The result is the checksum.
The receiver adds every word, including the checksum field, with the same end-around carry. If the data is intact, the sum is 0xFFFF (all ones). Complementing that gives 0, so "checksum equals 0" is the receiver's test.
The sender computes the checksum with the checksum field set to zero. If the data has an odd number of bytes, the sender pads it with one zero byte for the computation. The pad byte is not transmitted.
Worked example
Take three words: 0x1234, 0xF0F0, 0x0F0F.
0x1234 + 0xF0F0 = 0x10324. This overflows 16 bits. Remove the leading 1 and add it back:0x0324 + 1 = 0x0325.0x0325 + 0x0F0F = 0x1234. There is no carry.- The complement of
0x1234is0xEDCB. This is the checksum.
The receiver computes 0x1234 + 0xEDCB = 0xFFFF, which is all ones, so the packet is accepted.
Running it
This function sums the words, folds the carry back in after each addition, and returns the complement.
def internet_checksum(data: bytes) -> int:
if len(data) % 2:
data += b"\x00"
total = 0
for i in range(0, len(data), 2):
total += (data[i] << 8) | data[i + 1]
total = (total & 0xFFFF) + (total >> 16)
return ~total & 0xFFFF
data = bytes.fromhex("1234f0f00f0f")
c = internet_checksum(data)
print(hex(c)) # 0xedcb
packet = data + c.to_bytes(2, "big")
print(internet_checksum(packet)) # 0, so the packet is accepted
corrupt = bytearray(packet)
corrupt[0] ^= 0x01 # flip one bit
print(hex(internet_checksum(bytes(corrupt)))) # 0xfffe, so the packet is rejectedOne fold per addition is enough. The largest possible total before folding is 0xFFFF + 0xFFFF = 0x1FFFE. Folding gives 0xFFFE + 1 = 0xFFFF, which fits in 16 bits.
The packet check works because the data has an even length. For odd-length data the padding rule applies to the whole checked region.
What it detects
Any single-bit error is detected. Flipping bit k changes one word by plus or minus 2^k. That is never a multiple of 65535, so the sum changes.
Many multi-bit errors are also caught, but several error patterns pass unnoticed:
- Reordering words. Addition is commutative, so swapping two 16-bit words leaves the sum unchanged.
- Compensating errors. One bit changes 0 to 1 in one word, and the same bit position changes 1 to 0 in another word. The sum is unchanged.
- Zero words. Inserting or deleting a
0x0000word changes nothing. - Two zeros. Ones' complement arithmetic has both
0x0000and0xFFFFas zero. Replacing a0x0000word with0xFFFF, or the reverse, can go unnoticed.
Comparison with parity and CRC
| Property | Parity | Internet checksum | CRC |
|---|---|---|---|
| Check bits | 1 per block | 16 | Usually 16 or 32 |
| Operation | XOR | 16-bit ones' complement addition | Modulo-2 division |
| Single-bit errors | Detected | Detected | Detected |
| Reordered words | Often missed | Missed | Detected |
| Typical use | Small blocks, memory | IP, TCP, UDP, ICMP (software) | Ethernet, Wi-Fi (hardware) |
The Internet checksum is placed at the network and transport layers, where the code runs in software on every host and router. The data link layer already has a CRC computed in hardware. The two checks cover different failures. The CRC catches corruption on a single link. The checksum catches errors that occur inside a router, such as memory faults or bugs, after the link-level CRC has passed.
Practical details
- IPv4 covers the header only. The checksum is recomputed at every router, because the TTL field changes at each hop.
- Incremental update. A router does not re-sum the header. When one 16-bit field changes from
mtom', the new checksum isHC' = ~(~HC + ~m + m'), using ones' complement addition. This is the form given in RFC 1624. - TCP and UDP use a pseudo-header. The checksum also covers the source and destination IP addresses, the protocol number and the segment length. These fields are not in the segment itself. Including them catches a segment delivered to the wrong host.
- UDP's zero. In UDP a transmitted checksum of 0 means "no checksum was computed". If the computed value is 0, the sender transmits
0xFFFFinstead. This is possible because ones' complement has two zeros. - Byte order. The sum is the same whether you read words big-endian or little-endian, apart from a byte swap of the result. Implementations use this to process data in the machine's native order.
Using other word sizes
GATE questions often use 8-bit words. The method does not change. Add the words, wrap any carry out of the top bit back around to the bottom, and complement the sum.
Exercise
A sender uses an 8-bit ones' complement checksum on two data words, 0xB5 and 0x9C. Find the checksum, and show that the receiver's sum comes out as all ones.
Show answer
Sum: 0xB5 + 0x9C = 0x151. This overflows 8 bits, so wrap the carry: 0x51 + 1 = 0x52. The checksum is the complement of 0x52, which is 0xAD. The receiver adds 0xB5 + 0x9C + 0xAD. The first two words give 0x52 after wrapping, and 0x52 + 0xAD = 0xFF, which is all ones. The complement is 0, so the packet is accepted.
Exercise
A packet contains the words 0x0001, 0x0002 and the correct checksum. In transit the first two words are swapped, so the packet carries 0x0002, 0x0001 and the same checksum. Does the receiver detect the error? Explain what property of the checksum causes this result.
Show answer
No. The receiver's sum is 0x0002 + 0x0001 + checksum, which equals the original sum, so it is still 0xFFFF and the packet is accepted. Addition is commutative, so the checksum cannot detect reordered words. A CRC would detect this change.