The idea: add the words, send the complement
IP, UDP and TCP all carry the same small error check, the Internet
checksum (RFC 1071). The sender cuts the bytes into 16-bit words and
adds them up. It flips every bit of the sum and sends that as the
checksum. The receiver adds the same words plus the checksum.
A number plus its complement is all ones, so an undamaged datagram always gives
0xFFFF. Any other result means something changed on the way.
One's-complement addition
The words are added with one's-complement arithmetic. It is ordinary 16-bit addition with one change: a carry out of bit 15 is not thrown away. It is added back in at bit 0. This is the end-around carry. For example 0x9727 + 0x7069 = 0x10790, 17 bits. The top 1 wraps around: 0x0790 + 1 = 0x0791 (Demo: sender computes the checksum, word 11).
Because every carry comes back, the order of the additions does not matter, and neither does when the carries are added. A fast implementation adds the words into a 32-bit register and folds the high half into the low half at the end. The receiver on the page does it that way: the twelve words add up to 0x2FFFD, and 0xFFFD + 2 = 0xFFFF. Byte order does not matter either: adding byte-swapped words gives the byte-swapped sum, so a little-endian CPU can add the words as they sit in memory and swap once at the end.
What UDP adds up: the pseudo-header
The UDP checksum (RFC 768) covers more than the UDP header and the data. In front it adds a pseudo-header: the source and destination IP addresses, a zero byte, the protocol number 17 and the UDP length. The pseudo-header is never sent. Both ends build it from the IP header. If a router or a bug delivers the datagram to the wrong address, the receiver's pseudo-header differs and the sum is wrong.
Two more rules: the checksum field itself counts as 0 while the sender computes it, and data of odd length gets one zero byte at the end for the sum only. The pad byte is not sent (Demo: odd length, pad a zero byte: "pin" adds as 0x7069 and 0x6E00, and the checksum is 0x8A70).
The receiver's check
The receiver builds the same twelve words, this time with the checksum the
sender wrote. The sender's words add up to S, and the checksum is NOT S. In
one's complement S + NOT S = 0xFFFF, so a clean datagram always gives 0xFFFF
(Demo: clean link, receiver sum FFFF). Some code checks it the other
way: it takes NOT of the total and expects 0. A datagram that fails is dropped
silently. UDP does not resend it; on Linux the InCsumErrors
counter in /proc/net/snmp goes up. One flipped bit changes the sum
by a power of two, so it is always caught (Demo: one flipped bit is
caught: "ping" arrives as "pinG", the sum is 0xFFDF).
Two zeros: 0x0000 means "no checksum"
One's complement has two ways to write zero: 0x0000 and 0xFFFF. UDP over IPv4 uses that. A checksum field of 0x0000 means "the sender did not compute a checksum", and the receiver skips the check (Demo: checksum off, error goes unseen). So when a real checksum comes out as 0x0000, the sender writes 0xFFFF instead. It is the same number in one's complement, and the receiver's sum still comes out right (Demo: checksum 0x0000 is sent as 0xFFFF: from port 40335 the words add to 0xFFFF, and the receiver's plain sum is 0x3FFFC, 0xFFFC + 3 = 0xFFFF).
IPv6 has no header checksum of its own, so it makes the UDP checksum mandatory (RFC 8200). A UDP datagram over IPv6 with a zero checksum is dropped. TCP never had the option: its checksum, with the same pseudo-header idea, is always required.
What it catches, and what it misses
| error | Internet checksum | CRC-32 |
|---|---|---|
| one flipped bit | always caught | always caught |
| two 16-bit words swapped | never caught: addition ignores order | caught |
| the same bit flipped 0 → 1 in one word and 1 → 0 in another | never caught: +2k and −2k cancel | caught |
| a word changed from 0x0000 to 0xFFFF | never caught: both are zero | caught |
| a burst of up to 15 bits | caught | caught (bursts up to 32 bits) |
| a random larger error | missed about 1 in 65,536 | missed about 1 in 4 billion |
The weak point is that a sum depends only on the values that are added, not on their order or on which word a change landed in (Demo: swapped words slip through: "ping" arrives as "ngpi", Demo: two flips in one column cancel: "ping" arrives as "pkne"; both sums are 0xFFFF). A CRC divides the bits as a polynomial, so each bit is weighted by its position and these errors change the remainder.
Why such a weak check is still used
It is cheap. A CPU adds 32 or 64 bits at a time and folds at the end, and the sum can be computed while the data is being copied. A router that lowers the IPv4 TTL can fix the header checksum without adding everything again (incremental update, RFC 1624). Most network cards now compute and check it in hardware (checksum offload). The real protection sits elsewhere: every Ethernet frame carries a CRC-32, and TLS adds a cryptographic check on top. Studies of real traffic still find damaged packets that only the TCP or UDP checksum catches, mostly from bugs in hosts and routers, where the link-layer CRC is recomputed after the damage.
See also TCP vs UDP for what UDP does with the datagram after the check, and Hamming Code for a code that can also correct the bit it finds.
What the page leaves out
The page uses one IPv4 datagram with a 3- or 4-byte payload. It leaves out the IPv4 header checksum (the same sum over the 20-byte header only), the TCP and ICMP checksums, the IPv6 pseudo-header with 128-bit addresses, UDP-Lite's partial coverage, the incremental update formula of RFC 1624, and how checksum offload and NAT rewrite the field.