Lesson 05 intermediate

The Data Link Layer

How layer 2 delivers frames on one link: MAC addresses, the Ethernet frame, framing, MTU, error detection with parity, checksums and CRC, error correction with Hamming codes, flow control, stop-and-wait, Go-Back-N, Selective Repeat and piggybacking.

The physical layer moves raw bits and makes no promises: bits get flipped, the receiver might be overwhelmed, and nobody knows where one message ends and the next begins. The data link layer fixes that for one link. It groups bits into frames, addresses them with MAC addresses, detects damaged frames, and on some links controls the pace and retransmits what was lost.

It has two sublayers: LLC (logical link control, the interface to layer 3) and MAC (media access control, who may transmit and when).

MAC addresses

A MAC address is 48 bits, written as six hex bytes: 3c:2c:4a:1b:9c:10.

  • The first three bytes are the OUI, the manufacturer’s ID.
  • The lowest bit of the first byte marks multicast (1) or unicast (0).
  • The next bit marks a locally administered address (1), such as a random Wi-Fi MAC or one invented for a VM.
  • ff:ff:ff:ff:ff:ff is the broadcast address: every device on the LAN accepts it.
$ ip link show wlp2s0 | grep ether
    link/ether 6e:4f:91:d2:08:a1 brd ff:ff:ff:ff:ff:ff permaddr a4:b1:c1:22:7e:05

The first byte 6e is 01101110 in binary: the second-lowest bit is 1, so this is a randomized, locally administered address. permaddr shows the real burned-in one.

The Ethernet frame

FieldSizePurpose
Preamble + start delimiter8 bytesLets the receiver lock on to the signal
Destination MAC6 bytesWho should accept it
Source MAC6 bytesWho sent it (switches learn from this)
802.1Q VLAN tag (optional)4 bytesVLAN number and priority (lesson 6)
EtherType2 bytesWhat is inside: 0x0800 IPv4, 0x86DD IPv6, 0x0806 ARP
Payload46 to 1,500 bytesThe packet from layer 3
FCS4 bytesCRC-32 checksum of the frame

The largest payload a link carries is its MTU (maximum transmission unit), 1,500 bytes on standard Ethernet. Data centers often enable jumbo frames with an MTU around 9,000 to cut per-packet overhead, but every device on the path must agree.

$ ip link show enp3s0 | grep -o 'mtu [0-9]*'
mtu 1500
$ ping -M do -s 1472 -c 1 192.168.1.1
64 bytes from 192.168.1.1: icmp_seq=1 ttl=64 time=0.41 ms
$ ping -M do -s 1473 -c 1 192.168.1.1
ping: local error: message too long, mtu=1500

-M do forbids fragmentation. 1,472 bytes of data plus 8 bytes of ICMP header plus 20 bytes of IP header is exactly 1,500, so 1,472 is the largest ping that fits.

Framing

The receiver needs to know where a frame starts and stops. Methods include:

  • Character count: a length field at the start. Simple, but one corrupted count loses sync.
  • Byte stuffing: special flag bytes mark the boundaries, and any flag byte inside the data gets an escape byte in front of it (PPP does this).
  • Bit stuffing: the flag is 01111110; the sender inserts a 0 after any five 1s in the data so the flag never appears by accident (HDLC).
  • Physical layer coding: Ethernet marks frame starts with special symbols and a preamble.

Error detection

Every method adds redundant bits the receiver can check.

  • Parity bit: one extra bit makes the count of 1s even (or odd). 1011001 has four 1s, so even parity appends 0. It catches any single-bit error but misses two flipped bits.
  • Checksum: add the data up in 16-bit words and send the result. IPv4, TCP and UDP headers use this. Cheap, but weak against some error patterns.
  • CRC (cyclic redundancy check): treat the frame as a big binary polynomial, divide by an agreed generator polynomial, and send the remainder. The receiver divides again and expects zero. Ethernet’s CRC-32 catches all burst errors up to 32 bits long and almost everything else.

A frame with a bad FCS is silently dropped. You see it only as a counter:

$ sudo ethtool -S enp3s0 | grep -iE 'crc|align'
     rx_crc_errors: 1832
     rx_align_errors: 0

A rising CRC counter is almost always a layer 1 problem: cable, connector, transceiver or electrical noise.

Error correction

Detecting is cheap; correcting without a resend needs more redundancy.

A Hamming code places parity bits at positions 1, 2, 4, 8 and so on, each covering a different set of data bits. The pattern of failed checks points straight at the flipped bit. To protect m data bits you need r parity bits where 2^r ≥ m + r + 1. For 7 data bits: r = 4, because 2^4 = 16 ≥ 7 + 4 + 1 = 12, while r = 3 gives only 8.

Forward error correction (FEC) is used where resending is expensive or slow: Wi-Fi, 5G, satellite links, 25G and faster Ethernet, and ECC memory.

Flow control and ARQ

On links where frames get lost (classically WAN serial links, today mostly Wi-Fi), the data link layer can retransmit. These ARQ (automatic repeat request) schemes are the same ideas TCP uses end to end, so they are worth knowing.

Stop-and-wait

Send one frame, wait for its acknowledgment (ACK), then send the next. If no ACK arrives before a timer expires, resend. Frames carry a 1-bit sequence number so duplicates can be spotted.

It is simple and terribly slow on long links. Example: a 1 Gbit/s link with a 30 ms round trip, sending 1,500-byte frames. Sending one frame takes 12 µs, then the sender sits idle for 30 ms. Utilization is 12 µs / 30.012 ms, about 0.04%.

Sliding window

Let the sender have many unacknowledged frames “in flight”. To fill the link above you need a window equal to the bandwidth-delay product: 1 Gbit/s × 0.03 s = 30 million bits, about 3.75 MB or roughly 2,500 frames.

SchemeSender window (m-bit sequence numbers)ReceiverOn a lost frame
Go-Back-Nup to 2^m − 1Accepts only in-order framesResends the lost frame and every frame after it
Selective Repeatup to 2^(m−1)Buffers out-of-order framesResends only the lost frame

Go-Back-N keeps the receiver simple but wastes bandwidth after a loss. Selective Repeat saves bandwidth at the cost of receiver buffers and more bookkeeping. TCP with SACK behaves much like Selective Repeat.

Piggybacking

When both sides send data, a receiver can attach its ACK to its own next data frame instead of sending a separate ACK frame. This piggybacking saves frames and bandwidth. TCP does exactly this: most segments carry both data and an acknowledgment number.

Media access control

When several devices share a medium, they need rules to avoid talking over each other:

  • CSMA/CD (collision detection): classic shared Ethernet listened, transmitted, and backed off after a collision. Full duplex switched Ethernet no longer needs it.
  • CSMA/CA (collision avoidance): Wi-Fi cannot detect collisions while transmitting, so it waits a random backoff before sending and expects an ACK for each frame.
  • Controlled access: token passing or scheduled time slots (5G and Wi-Fi 6 OFDMA schedule clients centrally).

Exercise

A ping with -M do -s 1500 fails on a standard Ethernet LAN but -s 1472 works. Why?

  1. The IP and ICMP headers add 28 bytes, and 1,472 + 28 equals the 1,500-byte MTU
  2. The switch blocks pings larger than 1,472 bytes
  3. ICMP only allows 1,472 bytes of data
  4. The MTU is 1,472 on Ethernet
Show answer

Answer: A. The MTU limits the whole IP packet. 20 bytes of IPv4 header and 8 bytes of ICMP header leave 1,472 bytes for data with fragmentation disabled.

Exercise

With Go-Back-N, frames 4 to 9 are sent and frame 5 is lost. What does the sender retransmit?

  1. Only frame 5
  2. Frames 5, 6, 7, 8 and 9
  3. Frames 4 to 9
  4. Nothing, the receiver repairs frame 5 with FEC
Show answer

Answer: B. The Go-Back-N receiver discards frames after a gap, so the sender goes back to the lost frame and resends everything from there. Selective Repeat would resend only frame 5.

Exercise

How many Hamming parity bits are needed to protect 11 data bits?

  1. 3
  2. 5
  3. 11
  4. 4
Show answer

Answer: D. Find the smallest r with 2^r ≥ 11 + r + 1. With r = 4, 16 ≥ 16 holds. With r = 3, 8 < 15 fails.

Key takeaways

  • Layer 2 frames carry MAC addresses and a CRC, and work on one link only.
  • The MTU (1,500 on Ethernet) limits the packet size; test it with ping -M do -s.
  • Parity and checksums are cheap; CRC-32 catches almost all errors; bad frames are dropped silently.
  • Hamming codes and FEC fix errors without a resend.
  • Sliding windows (Go-Back-N, Selective Repeat) keep long links full; piggybacking saves ACK frames.

FAQ

Does Ethernet retransmit lost frames?

No. Wired Ethernet drops a bad frame and moves on; higher layers, usually TCP, notice the gap and resend. Wi-Fi does retransmit at layer 2, because radio loses frames far more often.

Can two devices have the same MAC address?

They should not on the same LAN, but it happens with cloned VMs or cheap hardware. The symptom is two hosts that randomly lose connectivity as the switch keeps moving the address between ports.

Should I enable jumbo frames?

Only on a closed network where every NIC, switch and router on the path supports the same MTU, such as a storage network. A single device at 1,500 in the middle causes drops that are painful to find.

Why is my Wi-Fi MAC address different on each network?

Phones and laptops randomize the MAC per network for privacy, so shops and airports cannot track you. You can turn it off per network if your router needs a fixed address for a reservation.

↑ ↓ navigate Enter open Esc close