Sub topic 5
Checksums and error detection
This chapter assumes you know what a UDP datagram and a TCP segment are, and that you have seen the header fields from the earlier chapters. Both protocols carry a 16-bit checksum field. Here you will learn what it covers, how it is computed, and what it can and cannot catch.
Why the transport layer checks for errors
Link layers such as Ethernet already protect each frame with a CRC (cyclic redundancy check). That check only covers one hop. A router can corrupt a packet in its own memory after the link CRC has passed. A software bug can also damage a packet. The transport checksum is an end-to-end check that runs on the sending and receiving hosts. It guards against damage anywhere in between.
What the checksum covers
The IPv4 header checksum protects only the IP header. The TCP and UDP checksums cover three things:
- The transport header.
- The payload.
- A pseudo-header, which is a few IP fields that are included in the calculation but never sent as part of the segment.
The pseudo-header holds the source address, the destination address, a zero byte, the protocol number and the transport length. Including it means a segment that is delivered to the wrong host, or marked with the wrong protocol, fails verification.
| Property | UDP | TCP |
|---|---|---|
| Checksum field | 16 bits | 16 bits |
| Mandatory | Optional in IPv4 (0 means "not computed"), required in IPv6 | Always |
| Covers pseudo-header | Yes | Yes |
| Action on mismatch | Datagram is dropped silently | Segment is dropped, so it is never acknowledged and the sender retransmits |
If a UDP sender computes a checksum of exactly 0, it transmits 0xFFFF instead. This keeps 0 free to mean "no checksum".
The Internet checksum algorithm
The algorithm is a 16-bit ones' complement sum:
- Split the data into 16-bit words. Pad with a zero byte if the length is odd.
- Add the words. Whenever the sum overflows 16 bits, add the carry back into the low bits ("end-around carry").
- Invert every bit of the result. This is the checksum.
The receiver sums everything again, including the checksum field. An undamaged segment gives 0xFFFF, so after inversion the result is 0.
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
payload = b"\x45\x00\x00\x1c"
csum = internet_checksum(payload)
print(hex(csum)) # 0xbae3
sent = payload + csum.to_bytes(2, "big")
print(internet_checksum(sent)) # 0: verifies
damaged = b"\x45\x01\x00\x1c" + csum.to_bytes(2, "big")
print(internet_checksum(damaged) != 0) # True: detected
# Known weakness: reordering 16-bit words goes unnoticed
print(internet_checksum(b"\x12\x34\x56\x78") ==
internet_checksum(b"\x56\x78\x12\x34")) # TrueThe last line shows the main limit. Addition does not depend on order, so swapped words, or two errors that cancel each other out, pass the check.
Common errors and gotchas
- Assuming UDP delivers damaged data. The kernel drops it before your
recvfromcall. Your program sees either a whole, valid datagram or nothing. - Assuming a passing checksum means the data is correct. The check is weak by design. It is cheap to compute, but it misses some multi-bit errors. Applications that need strong integrity add their own check, such as a CRC or a cryptographic hash, or use TLS.
- Disabling checksums on IPv4 UDP. The saved CPU time is small, and corrupted data would reach the application unnoticed.
- Confusing checksum offload with a missing checksum. Packet captures on the sending host often show "incorrect" checksums. The network card fills in the value after the capture point, so the wire carries a correct checksum.
Exercise
A UDP datagram is corrupted in transit so its checksum no longer verifies. What does the receiving application observe, and how does that differ from the same corruption in a TCP segment?
Show answer
The UDP application observes nothing. The kernel discards the datagram, and no error is reported. For TCP, the segment is also discarded, but it is never acknowledged, so the sender's retransmission timer expires and the data is sent again. The TCP application sees the data arrive correctly, only slightly delayed.
Exercise
Why does the checksum include a pseudo-header with IP addresses even though those addresses are not part of the TCP or UDP header?
Show answer
It lets the receiver detect segments delivered to the wrong host or labelled with the wrong protocol. If an IP header is corrupted so that the address or protocol changes, the checksum computed over the pseudo-header no longer matches.