Skip to content
BoKSA

13 – The Link Layer (1): Error Detection and Correction

13 – The Link Layer (1): Error Detection and Correction

Course: ICT Basic course · Level: introductory Book: Kurose & Ross, 9th ed., chapter 6.1 (introduction to the link layer) and 6.2 (error-detection and -correction techniques)

Learning goals

After this article you can:

  • describe the services of the link layer and where it is implemented;
  • explain the difference between error detection and error correction (FEC);
  • calculate single parity and two-dimensional parity bits, and correct a single-bit error with 2D parity;
  • explain the idea of the Internet checksum and CRC (you don't have to calculate CRC by hand);
  • explain where error detection/correction is used in today's networks and storage.

The link layer moves a frame from one node to the next node over a single link (host → switch, switch → router, router → router…).

1
 host ──link 1 (Wi-Fi)──► AP ──link 2 (Ethernet)──► router ──link 3 (fibre)──► router ──► …

On every link, a different link-layer protocol may be used. Analogy: a trip from Amsterdam to Rome: taxi → train → plane. Each "link" has its own transport method, but the traveller (datagram) stays the same.

Service Meaning
Framing put the datagram in a frame with header (and trailer)
Link access rules for who may send on a shared medium (MAC protocol, article 14)
Reliable delivery some links re-send lost frames themselves (Wi-Fi does this; wired Ethernet doesn't)
Error detection detect bit errors caused by noise, interference, signal loss
Error correction detect and repair errors without retransmission

Where is it implemented?

In the network interface card (NIC) or chip (Ethernet, Wi-Fi), partly in hardware and partly in the driver/firmware. The NIC computes and checks the error-detection bits at full speed in hardware.


2. The principle: EDC bits

1
2
3
 sender:    D (data) ──► add EDC (error detection/correction bits) ──► [ D | EDC ] ──► link
 receiver:  [ D' | EDC' ] ──► check: does EDC' fit D'?
                               yes → (probably) OK      no → error detected

Important: error detection is never 100% reliable. Some combinations of bit errors go unnoticed. More EDC bits = better detection, but also more overhead.


3. Parity checks

3.1 Single parity bit

Add one bit so that the total number of 1s is: - even (even parity), or - odd (odd parity).

Example (even parity):

Data Number of 1s Parity bit Sent
10010000 2 (even) 0 10010000 0
11101111 7 (odd) 1 11101111 1
00000000 0 (even) 0 00000000 0

What can it detect? - Every odd number of bit errors (1, 3, 5…). - Not an even number of errors: if 2 bits flip, the parity still looks correct. - So when the parity check succeeds, you can't be absolutely sure the data is correct. When it fails, you are sure that something went wrong (but not which bit).

3.2 Two-dimensional parity – detecting and correcting

Put the data in a matrix and add a parity bit for every row and every column (plus a corner bit).

Example (even parity), data 0011 1000 1111 1011:

1
2
3
4
5
6
 0 0 1 1 | 0
 1 0 0 0 | 1
 1 1 1 1 | 0
 1 0 1 1 | 1
 ────────┼──
 1 1 1 1 | 0     ← column parity (and corner bit)

Correcting a single-bit error: if one bit flips, exactly one row and one column have wrong parity. The bit where they cross is the wrong bit → flip it back. This is Forward Error Correction (FEC): the receiver repairs the error itself, without asking for retransmission.

Example received data:

1
2
3
4
5
6
7
 1 0 0 1 | 0   ✓ (2 ones)
 0 0 0 0 | 0   ✓
 0 0 1 1 | 1   ✗ (2 ones + parity 1 = 3 → odd → error in this row)
 1 0 1 0 | 0   ✓
 ────────┼──
 0 1 0 0 | 1
 ✓ ✗ ✓ ✓       ← column 2: 0+0+0+0 = 0 ones, but parity bit 1 → odd → error in this column
→ the error is at row 3, column 2 → flip that bit: the correct row 3 is 0 1 1 1. Check: row 3 now has 3 ones + parity 1 = even ✓, column 2 has 1 one + parity 1 = even ✓.

2D parity can detect (but not correct) any combination of two bit errors.

3.3 Hamming codes

Hamming codes (Richard Hamming, 1950) are a smarter form of parity: parity bits are placed at positions 1, 2, 4, 8, … and each covers a specific set of bits. With few extra bits they can correct any single-bit error (e.g. Hamming(7,4): 4 data bits + 3 parity bits). Extended Hamming codes (SECDED) also detect double-bit errors. They are still used in ECC memory (RAM in servers).


4. The Internet checksum

Used in the headers of IPv4, UDP and TCP (article 09).

  • The data is treated as a series of 16-bit integers.
  • They are added up with 1's complement arithmetic (a carry out of the top bit is added back at the bottom).
  • The checksum is the inverse (1's complement) of the sum.
  • The receiver adds everything including the checksum; the result must be all 1s.

Very fast to compute in software, but relatively weak: some errors (e.g. two swapped 16-bit words) are not detected. That's acceptable because the link layer already uses a much stronger CRC.


5. CRC – Cyclic Redundancy Check

CRC is the error-detection method of the link layer: Ethernet, Wi-Fi, Bluetooth, USB, 5G, and also ZIP files, PNG images and storage.

The idea (you need to be able to describe it, not calculate it):

  • Sender and receiver agree on a generator G of r+1 bits (e.g. CRC-32 in Ethernet).
  • The sender adds r CRC bits R to the data D so that the bit pattern ⟨D,R⟩ is exactly divisible by G (modulo-2 arithmetic, which is just XOR without carries).
  • The receiver divides the received bits by G. Remainder ≠ 0 → error detected.
  • A CRC with r bits detects all burst errors shorter than r+1 bits and almost all longer ones. CRC-32 (32 bits) in the Ethernet FCS (Frame Check Sequence) is very strong.
1
 Ethernet frame:  [preamble][dest MAC][src MAC][type][   data (46–1500 bytes)   ][FCS = CRC-32]

If an Ethernet NIC detects a CRC error, it silently drops the frame. Higher layers (TCP) will notice the loss and re-send.


6. Error correction in practice (updated)

Modern links use powerful FEC codes because retransmission would take too long or because the medium is very noisy:

Where Code
Wi-Fi 6/7, DVB-S2 (satellite TV), 5G data channels LDPC (Low-Density Parity-Check)
5G control channels Polar codes
4G LTE, deep-space communication Turbo codes, convolutional codes
QR codes, CDs/DVDs, Blu-ray, DSL Reed-Solomon
Fibre links ≥ 100 Gbit/s, 25G+ Ethernet RS-FEC / other FEC
Server memory ECC RAM (Hamming / SECDED)
Storage (SSD, RAID 5/6, cloud storage) LDPC, Reed-Solomon / erasure coding

When to detect + retransmit, when to correct (FEC)? - Retransmission is efficient when errors are rare and the delay is small (wired Ethernet + TCP). - FEC is better when errors are frequent (wireless), the delay is large (satellite, space) or there's no time to retransmit (live video, voice).


7. Try it yourself

  1. Even parity: which parity bit is added to 10010000, 11101111 and 00000000? Is 00110101|0 certainly correct? Is 00110101|1 certainly wrong?
  2. Make the 2D-parity scheme for the bits 1010 0110 1100 0001 (even parity). Then flip one bit and show how the receiver finds and corrects it.
  3. Calculate a checksum of a file and change one character:
    1
    2
    echo "hello world" > a.txt && crc32 a.txt && sha256sum a.txt
    echo "hello World" > a.txt && crc32 a.txt && sha256sum a.txt
    
    (crc32 is in the libarchive-zip-perl package on Debian.)
  4. Generate a QR code with error correction level H, cover part of it, and scan it with your phone. Why does it still work?
  5. In Wireshark, enable Edit → Preferences → Protocols → Ethernet → "Assume packets have FCS" (on interfaces that capture the FCS). Normally the NIC removes the FCS before Wireshark sees the frame.

Check your understanding

  1. Name four services of the link layer.
  2. What is the difference between error detection and error correction?
  3. Can single parity detect two bit errors? Why (not)?
  4. How does 2D parity correct a single-bit error?
  5. Where is the Internet checksum used, and where is CRC used?
  6. Why does Wi-Fi use FEC much more than wired Ethernet?

References

Book

  • Kurose & Ross, 9th ed., chapter 6.1–6.2

Videos

Articles


Cyber Security

Why is this important for cyber security?

  • Error detection ≠ integrity protection against attackers. Parity, checksums and CRC protect against random errors (noise), not against deliberate changes. An attacker who changes the data can simply recompute the CRC or checksum, because the algorithms are public and use no secret key. So a correct CRC does not prove the data wasn't tampered with.
  • Integrity is one of the three pillars of security (CIA). For protection against attackers you need cryptographic methods: hash functions (SHA-256), MACs/HMACs (with a secret key) and digital signatures (article 16). TLS, IPsec, WPA3 and SSH all use these.
  • Bit flips can be security problems. Rowhammer attacks deliberately cause bit flips in RAM to gain privileges. Random bit flips in DNS queries can be abused (bitsquatting: registering domains one bit different from popular names). ECC memory helps against this.
  • Corrupted backups: without proper checks, damaged backups may only be noticed when you need them, for example after a ransomware attack.

What can you do to make it safe?

Goal Use Not
detect random errors CRC, checksum –
check that a download is complete and unchanged (from a trusted source) SHA-256 hash (sha256sum) published over HTTPS MD5 or SHA-1 (broken)
protect against an attacker who can change data HMAC or authenticated encryption (AES-GCM, ChaCha20-Poly1305) CRC / checksum
prove who made the data digital signature (GPG, code signing, Sigstore) plain hash
  • Use ECC RAM in servers, and file systems with checksums (ZFS, Btrfs) for important data.
  • Test your backups regularly (restore tests + checksums).
  • Always use protocols with built-in integrity (TLS, SSH, IPsec, WPA2/3) instead of relying on link-layer CRC.

Extra references