4.3.4Computer Networks

Data link layer — framing, error detection (CRC computation), MAC

2,109 words10 min readdifficulty · medium1 backlinks

1. Framing

WHY: The receiver gets a continuous stream ...10110101.... Without markers it can't tell one frame from the next. We need delimiters — but delimiters can also accidentally appear inside the data, so we need an escaping trick.

Methods

(a) Byte/character count — first field says "next NN bytes are my payload."

  • Problem: if the count field itself is corrupted, the receiver loses sync forever.

(b) Byte stuffing (flag bytes) — put a special FLAG byte at start & end (e.g. 0x7E). If FLAG or the escape byte ESC appears in data, prefix it with ESC.

(c) Bit stuffing — frame is delimited by the flag pattern 01111110. To avoid this pattern in data, the sender inserts a 0 after every 5 consecutive 1s in the data.


2. Error Detection — CRC

WHAT is GF(2) arithmetic? Coefficients are bits. Addition = subtraction = XOR (no carries!). 1+1=01+1=0. So polynomial long division uses XOR instead of subtraction.

Derivation from scratch — WHY the remainder makes it divisible

Let:

  • M(x)M(x) = message polynomial, degree m1m-1 (message has mm bits).
  • G(x)G(x) = generator, degree rr (so r+1r+1 bits).

Step 1 — make room. Multiply message by xrx^r, i.e. append rr zeros: M(x)xrM(x)\cdot x^r Why? This shifts the message left, reserving rr low-order slots for the check bits.

Step 2 — divide. Over GF(2): M(x)xr=Q(x)G(x)+R(x),degR<rM(x)\cdot x^r = Q(x)\,G(x) + R(x), \qquad \deg R < r R(x)R(x) is the CRC remainder (rr bits, the FCS = Frame Check Sequence).

Step 3 — transmit. Send: T(x)=M(x)xr+R(x)T(x) = M(x)\cdot x^r + R(x) Why is this divisible? Because over GF(2) addition = subtraction: T(x)=Q(x)G(x)+R(x)+R(x)=Q(x)G(x)+0T(x) = Q(x)G(x) + R(x) + R(x) = Q(x)G(x) + 0 since R(x)+R(x)=0R(x)+R(x)=0 (XOR). So T(x)T(x) is an exact multiple of G(x)G(x).

Worked example (full long division)

Message M=1101M = 1101, generator G=1011G = 1011 (so r=3r=3).

Why append 3 zeros? degG=3\deg G=3 \Rightarrow 3 check bits → dividend = 1101000.

1101000 ÷ 1011   (XOR division)

1101000
1011
----
0110000
 1011
 ----
 01110 0
  1011
  ----
  01010
   1011
   ----
   0001   → remainder R = 001

Step-by-step (Why each step?): we XOR GG whenever the leading bit is 1, shift, repeat until we run out of bits. Leading 0 ⇒ bring next bit down (XOR with 000).

Transmit T=1101001=1101001T = 1101\,\underline{001} = 1101001.

Receiver check: divide 1101001 by 1011 → remainder 000 ⇒ accept. If any bit flips, remainder ≠ 0 (for errors GG can catch).

Figure — Data link layer — framing, error detection (CRC computation), MAC

What errors does CRC catch? (WHY choose good GG)

An error pattern is E(x)E(x); received = T(x)+E(x)T(x)+E(x). Error is undetected iff E(x)E(x) is divisible by G(x)G(x). So we pick GG to make that rare:

  • All single-bit errors (E=xiE=x^i): caught if GG has 2\ge 2 terms.
  • All odd numbers of bit errors: caught if GG has factor (x+1)(x+1).
  • All burst errors of length r\le r: caught (a burst is xiB(x)x^i\cdot B(x) with degB<r\deg B<r; GxiG\nmid x^i and degB<degG\deg B<\deg G).

3. MAC — Medium Access Control

Two flavours:

(a) CSMA/CD (wired Ethernet): Carrier Sense (listen before talking), Multiple Access, Collision Detection (keep listening while sending; if you hear garble ≠ your bits → collision). On collision: jam + binary exponential backoff (wait random time in [0,2k1][0, 2^k-1] slots after the kk-th collision).

(b) CSMA/CA (Wi-Fi): can't reliably detect collisions on radio, so avoid them — wait random backoff before sending, use ACKs, optional RTS/CTS.


Active Recall

Recall Why does appending the CRC remainder make the frame divisible by

G(x)G(x)? Because in GF(2), ++ and - are both XOR. T=Mxr+RT=M x^r+R; since Mxr=QG+RMx^r = QG+R, we get T=QG+R+R=QGT=QG+R+R=QG, an exact multiple.

Recall In CSMA/CD why must

Lmin2BτL_{\min}\ge 2B\tau? The sender needs to still be transmitting when the worst-case collision signal (round trip 2τ2\tau) returns, so it can detect it.

Recall Bit stuffing rule?

Insert a 0 after every five consecutive 1s in the data, so the flag 01111110 can't appear in payload.

Recall (Feynman, explain to a 12-year-old)

Imagine mailing a long sentence with no spaces. Framing = adding clear START/END marks so your friend knows where each word is. CRC = adding a little "magic number" at the end; your friend redoes the math — if it doesn't match, a letter got smudged in the mail. MAC = the rule "only one person talks at a time" so everyone in a noisy room can be understood.


What are the three jobs of the data link layer?
Framing, error detection, and medium access control (MAC).
What is bit stuffing's rule?
Insert a 0 after every 5 consecutive 1s in the data to prevent the flag 01111110 appearing in payload.
In CRC, what is rr and how many zeros do you append to the message?
r=degG(x)r=\deg G(x); append rr zeros (multiply M(x)M(x) by xrx^r).
Why is the transmitted CRC frame T(x)T(x) divisible by G(x)G(x)?
T=Mxr+RT=Mx^r+R and Mxr=QG+RMx^r=QG+R; in GF(2) R+R=0R+R=0, so T=QGT=QG (exact multiple).
What arithmetic does CRC division use?
GF(2): addition/subtraction are both XOR, no carries.
CRC error E(x) goes undetected when?
When E(x)E(x) is divisible by G(x)G(x).
Which errors does a generator with factor (x+1)(x+1) catch?
All errors with an odd number of bit flips.
Why does CSMA/CD need a minimum frame size?
So the sender is still transmitting when worst-case collision news returns after round-trip 2τ2\tau; hence Lmin2BτL_{\min}\ge 2B\tau.
What does CSMA/CD do after a collision?
Jam signal, then binary exponential backoff (random wait in [0,2k1][0,2^k-1] slots).
Why CSMA/CA instead of CD in Wi-Fi?
Radios can't reliably detect collisions while transmitting, so collisions are avoided (backoff + ACKs/RTS-CTS).
How many bits is a MAC address and is it hierarchical?
48 bits, flat (not hierarchical), used for local link delivery.
Does CRC correct errors?
No — it only detects; correction needs codes like Hamming.
CRC: message 1101, G=1011, find FCS.
Append 000 → divide 1101000 by 1011 → remainder 001; transmit 1101001.

Connections

  • Physical Layer — provides the raw bit pipe framing sits on.
  • Polynomials over GF(2) — math foundation of CRC.
  • Hamming Code — error correction vs CRC's detection.
  • Ethernet — uses CSMA/CD, 64-byte min frame, MAC addresses.
  • Network Layer (IP) — hierarchical addressing contrasted with flat MAC.
  • Stop-and-Wait & Sliding Window — reliability built atop framing + error detection.

Concept Map

lacks boundaries and error check

job 1

job 2

job 3

method

method

method

weakness

insert 0 after five 1s

treats bits as

append remainder

receiver checks

Physical layer raw bits

Data link layer

Framing

Error detection CRC

MAC who transmits

Byte count

Byte stuffing ESC before FLAG

Bit stuffing flag 01111110

Corrupt count loses sync

Flag reserved never in data

Polynomial over GF 2 XOR math

Message divisible by G x

Remainder 0 means OK

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, data link layer ka kaam hai physical layer ki "dumb bit pipe" ko useful banana. Teen kaam: framing (bits ke stream mein frame kahan start/end hota hai mark karna), error detection (koi bit flip to nahi hua noise se), aur MAC (jab ek hi wire pe bahut saare nodes hain, to kaun bolega kab). Framing ke liye flags use karte hain, par flag pattern data ke andar bhi aa sakta hai — isliye bit stuffing karte hain: har paanch consecutive 1s ke baad ek 0 ghusa do, taaki real flag 01111110 kabhi payload mein na aaye.

CRC sabse important hai. Idea simple: message ko ek polynomial samjho (GF(2) mein, yaani addition/subtraction dono XOR). Ek fixed generator G(x)G(x) choose karo jiska degree rr hai. Message ke peeche rr zeros lagao (matlab xrx^r se multiply), phir G(x)G(x) se XOR-division karo, jo remainder aaye usko zeros ki jagah laga do. Ab pura frame G(x)G(x) se exactly divisible ho jaata hai — kyunki GF(2) mein R+R=0R+R=0. Receiver bas divide karta hai: remainder 0 to OK, warna error pakka. Yaad rakho — CRC sirf detect karta hai, correct nahi.

MAC mein, wired Ethernet CSMA/CD use karta hai: pehle suno (carrier sense), phir bolo, aur bolte waqt bhi suno — agar collision sune to ruko aur random backoff. Yahin se minimum frame size ka concept aata hai: sender ko itni der transmit karna padega ki worst-case collision ki khabar round-trip 2τ2\tau mein wapas aa jaaye, isliye Lmin2BτL_{\min} \ge 2B\tau. Wi-Fi mein collision detect karna mushkil hai, isliye CSMA/CA — collision avoid karo with backoff aur ACKs. MAC address 48-bit hota hai, flat (IP jaisa hierarchical nahi), local delivery ke liye.

Go deeper — visual, from zero

Test yourself — Computer Networks

Connections