Computer Networks chapters (5)
  1. 01Reference Models & Network Layers
  2. 02Data Link Layer & MAC Protocols
  3. 03Network Layer & IP Addressing
  4. 04Transport Layer (TCP & UDP)
  5. 05Application Layer Protocols & Security

Chapter 02 · CN

Data Link Layer & MAC Protocols

4 topics · 2 sections · beginner-friendly study notes · Updated

Share with friends:WhatsAppTelegram

Embed on your site

Free for teachers, schools and blogs. Please keep the attribution link.

Preview

The data link layer moves frames between neighbouring nodes.

This chapter covers framing and bit stuffing, error detection with parity and CRC, shared-medium access (CSMA/CD and CSMA/CA), the Ethernet frame and MAC address, and how learning switches forward frames.

CRC division, CSMA/CD frame-size and switch-table questions come up repeatedly in GATE and CCNA.

Ethernet and Wi-Fi LAN design, diagnosing duplex mismatches and broadcast storms, understanding how switches build their MAC tables.

1

Framing and bit stuffing

Framing delimits the start and end of a frame in a bit stream. In bit-oriented protocols such as HDLC the frame is delimited by the flag 01111110, and bit stuffing inserts a 0 after every five consecutive 1s in the data so the flag never appears inside the payload.

In short: Put clear start and end markers around each message and make sure the marker pattern never appears in the message itself.

Think about it

Without framing the receiver cannot tell where one frame ends and the next begins.

Methods: character count, flag bytes with byte stuffing (an escape byte before any flag/escape byte in the data), flag bits with bit stuffing, and physical-layer coding violations.

The receiver removes a 0 that follows five 1s.

Stuffing increases the frame length slightly, so frame size is variable.

The data link layer also provides flow control and, in some protocols, reliable delivery with acknowledgements (stop-and-wait, Go-Back-N, Selective Repeat).

Key points

  • Flag: Special bit pattern marking frame boundaries (01111110 in HDLC).
  • Bit stuffing: Inserting a 0 after five consecutive 1s.
  • Byte stuffing: Inserting an escape byte before flag bytes found in data.
  • Data 01111110 becomes 011111010 after bit stuffing (a 0 is inserted after the first five 1s).

Read more

2

Error Detection & CRC

Error detection adds redundant bits so the receiver can detect corrupted frames. Parity adds one bit to make the number of 1s even or odd. CRC treats the data as a polynomial, divides it by a generator polynomial using modulo-2 arithmetic and appends the remainder.

In short: Parity is a quick count check; CRC is a stronger mathematical fingerprint of the message.

Think about it

Links are noisy; unreliable frames must be detected before higher layers use them.

Single parity detects all odd-numbered bit errors but misses even-numbered ones.

Two-dimensional parity can locate and correct a single-bit error.

CRC with an r-bit remainder uses a generator of r+1 bits: append r zeros to the data, perform modulo-2 (XOR) division, replace the zeros by the remainder; the receiver divides the received codeword and accepts if the remainder is zero.

A CRC with a generator containing the factor (x+1) detects all odd errors; a generator of degree r detects all burst errors of length ≤ r, and most longer bursts.

Internet checksum (used by IP, TCP, UDP) is the 1’s complement of the 1’s complement sum of 16-bit words, cheap but weaker.

Error correction uses Hamming codes: minimum Hamming distance d detects d-1 errors and corrects floor((d-1)/2) errors; for m data bits, r check bits must satisfy 2^r ≥ m + r + 1.

Key points

  • Generator polynomial: Predefined divisor used by sender and receiver.
  • FCS: Frame check sequence: the CRC field at the end of a frame.
  • Hamming distance: Number of bit positions in which two codewords differ.
  • CRC steps: append (r) zeros → XOR long division → remainder replaces zeros → send data + remainder.
  • If the received codeword divided by the generator leaves remainder 0, no error is detected.
3

Sliding Window ARQ Protocols

Automatic Repeat reQuest (ARQ) protocols provide reliable delivery over an error-prone link using acknowledgements, timeouts and retransmission. Stop-and-Wait sends one frame and waits for its ACK. Go-Back-N (GBN) lets the sender transmit up to N frames before an ACK, with a receiver window of size 1: on an error it retransmits the lost frame and all that followed. Selective Repeat (SR) has sender and receiver windows greater than 1 and retransmits only the lost frame.

In short: Stop-and-wait: send one, wait. Go-Back-N: send a batch, redo everything from the first lost frame. Selective Repeat: redo only the lost frame.

Sliding window: grow/shrink a range

Think about it

Efficiency, window-size and sequence-number-bits numericals are guaranteed in GATE; the three protocols are also the core of reliable transport.

Let Tt = L/B be transmission time and Tp propagation delay; a = Tp/Tt.

Stop-and-wait efficiency η = Tt/(Tt + 2Tp) = 1/(1 + 2a).

Sliding window of size N: η = min(1, N/(1 + 2a)); full utilisation needs N ≥ 1 + 2a.

Sequence numbers with k bits: Stop-and-wait needs 1 bit (window 1, numbers 0/1); GBN max sender window = 2^k − 1 (receiver window 1); SR max sender window = receiver window = 2^(k−1) (otherwise the receiver cannot tell a new frame from a retransmitted old one).

Minimum sequence-number bits: GBN → 2^k ≥ N + 1; SR → 2^k ≥ 2N.

GBN: cumulative ACK, receiver discards out-of-order frames, so after a loss the sender goes back and retransmits from the lost frame; simple receiver, wasteful on noisy links.

SR: receiver buffers out-of-order frames, sends individual ACKs, sender times out per frame; efficient on errors but needs buffering and sorting.

Piggybacking carries ACKs in reverse data frames.

NAK can speed recovery.

With frame-loss probability p the expected number of transmissions per frame is 1/(1 − p) for stop-and-wait and Selective Repeat, while Go-Back-N is worse because each loss repeats up to a whole window.

The data-link layer uses these protocols (HDLC, PPP); TCP uses a sliding window with cumulative ACKs and selective ACK options.

Key points

  • ARQ: Automatic repeat request: error control using ACK, NAK and timeouts.
  • Cumulative ACK: ACK n confirms all frames up to n−1 (used by Go-Back-N and TCP).
  • a = Tp / Tt: Ratio of propagation delay to transmission time; decides link efficiency.
  • Piggybacking: Sending an ACK inside a data frame travelling in the opposite direction.
  • Stop-and-wait: η = 1/(1 + 2a). Link 2 Mbps, 1000-bit frames: Tt = 0.5 ms; Tp = 10 ms → a = 20 → η = 1/41 ≈ 2.4%; window for 100% = 41; GBN with N = 8 gives η = 8/41 ≈ 19.5%.
  • Window sizes: GBN sender 2^k − 1, receiver 1; SR sender = receiver = 2^(k−1). With 4-bit sequence numbers: GBN 15, SR 8.
  • GBN example: window 4, frames 0–7, frame 2 lost. Sender sends 0,1,2,3; ACKs 0,1 arrive; frame 3 is discarded (out of order); timeout for 2 → resend 2,3,4,5. Selective Repeat would resend only 2.

Example

Code
Go-Back-N sender logic:
/* Go-Back-N sender (window N, base = oldest unACKed) */
while (true) {
    if (next_seq < base + N) {         // window not full
        send(frame[next_seq]); 
        if (base == next_seq) start_timer();
        next_seq++;
    }
    on ACK(n):  base = n + 1;          // cumulative
                if (base == next_seq) stop_timer(); else start_timer();
    on TIMEOUT: start_timer();
                for (i = base; i < next_seq; i++) send(frame[i]);   // go back
}
4

ALOHA Protocols

ALOHA is a random-access MAC protocol for a shared channel. Pure ALOHA: a station transmits whenever it has a frame; if no ACK arrives it waits a random time and retransmits. Slotted ALOHA: time is divided into slots equal to the frame time and stations may start only at a slot boundary.

In short: Pure ALOHA: talk any time and hope nobody else talks. Slotted ALOHA: talk only at the tick of a clock, so collisions are all-or-nothing.

Think about it

ALOHA throughput and maximum-efficiency numericals are standard GATE questions and show why uncoordinated sharing wastes capacity.

Let G = average number of frame transmission attempts per frame time (offered load) and T = frame time.

Pure ALOHA: a frame collides if another frame starts within one frame time before or after, so the vulnerable time = 2T and the throughput S = G·e^(−2G); maximum S = 1/(2e) ≈ 0.184 (18.4%) at G = 0.5.

Slotted ALOHA: vulnerable time = T, S = G·e^(−G); maximum S = 1/e ≈ 0.368 (36.8%) at G = 1, exactly double pure ALOHA.

Probability of no collision for a frame = e^(−2G) (pure) or e^(−G) (slotted) assuming Poisson arrivals.

Slotting needs clock synchronisation.

Neither senses the channel; adding carrier sensing gives CSMA, which performs much better.

Retransmission uses a random backoff to avoid repeated collisions.

Efficiency at the maximum point is also called the channel utilisation.

Applications: satellite links, early packet radio (ALOHAnet), RFID, cellular random-access channels.

Other MAC families: reservation/polling and token passing (controlled access), TDMA/FDMA/CDMA (channelisation).

Key points

  • Vulnerable time: Interval in which another transmission would collide with a frame (2T pure, T slotted).
  • Offered load G: Average number of transmission attempts per frame time, including retransmissions.
  • Throughput S: Average number of successful frames per frame time.
  • Pure ALOHA: S = G·e^(−2G), Smax = 1/(2e) ≈ 18.4% at G = 0.5. Slotted ALOHA: S = G·e^(−G), Smax = 1/e ≈ 36.8% at G = 1.

Example 1

Code
1 Mbps channel, 1000-bit frames (T = 1 ms, capacity 1000 frames/s). Pure ALOHA at G = 0.5 → S = 0.5·e^(−1) ≈ 0.184 → about 184 successful frames/s. Slotted at G = 1 → ≈ 368 frames/s.

Example 2

Code
slotted ALOHA, 10 stations, each transmits in a slot with probability p = 0.1. P(success in a slot) = 10 × 0.1 × 0.9^9 ≈ 0.387.
5

CSMA/CD & CSMA/CA

CSMA (carrier sense multiple access) listens before transmitting. CSMA/CD (collision detection, wired Ethernet) detects collisions while transmitting, aborts, sends a jam signal and retries after binary exponential backoff. CSMA/CA (collision avoidance, Wi-Fi) tries to avoid collisions using backoff timers and optional RTS/CTS.

In short: CD: listen first, shout, and if two people talk at once, stop and wait a random time. CA: wait a random time before talking since you cannot listen while speaking on radio.

Think about it

A shared medium needs rules for who may transmit and when.

Persistence variants: 1-persistent (transmit as soon as the channel is idle; Ethernet), non-persistent, p-persistent.

For CSMA/CD to detect a collision the sender must still be transmitting when the collision signal returns, so transmission time must be at least the round-trip propagation time: Tt ≥ 2·Tp, i.

e.

minimum frame size L ≥ 2·Tp·B. Classic Ethernet has a 51.2 µs slot time at 10 Mbps, giving 512 bits = 64 bytes minimum frame.

Binary exponential backoff: after the k-th collision choose a random number from 0 to 2^k - 1 slots (k capped at 10), give up after 16 attempts.

Wi-Fi cannot detect collisions because a radio cannot send and listen simultaneously and suffers from the hidden-terminal problem, hence acknowledgements, SIFS/DIFS spacing, random backoff and RTS/CTS.

Full-duplex switched Ethernet needs no CSMA/CD.

Key points

  • Slot time: Round-trip time for the maximum network (512 bit times in Ethernet).
  • Jam signal: Short signal sent after a collision to notify all stations.
  • Hidden terminal: Two stations cannot hear each other but collide at a common receiver.
  • Minimum frame size = 2 × propagation delay × bandwidth. Efficiency of CSMA/CD ≈ 1 / (1 + 6.44a), a = Tp/Tt.

Read more

6

Ethernet MAC address and frame

A MAC address is a 48-bit hardware address written as six hex bytes (e.g. 00:1A:2B:3C:4D:5E). An Ethernet II frame contains: preamble (7 bytes) and SFD (1 byte), destination MAC (6), source MAC (6), type/length (2), payload (46-1500), FCS (4).

In short: The MAC address is the NIC’s serial number on the local link; the frame is the labelled envelope carrying a packet.

Think about it

MAC addressing allows delivery on a local network independent of IP.

The first 24 bits form the OUI (vendor); the remaining 24 bits are assigned by the manufacturer.

The least significant bit of the first byte indicates unicast (0) or multicast (1); FF:FF:FF:FF:FF:FF is broadcast.

Minimum frame (excluding preamble) is 64 bytes: 14 + 46 + 4; padding is added when the payload is smaller than 46 bytes.

Maximum frame is 1518 bytes (payload 1500 = MTU).

The type field identifies the upper protocol (0x0800 = IPv4, 0x0806 = ARP, 0x86DD = IPv6).

Receivers drop frames with a bad FCS silently.

Key points

  • OUI: Organizationally unique identifier: vendor part of a MAC address.
  • Broadcast MAC: FF:FF:FF:FF:FF:FF – received by every NIC on the segment.
  • Padding: Extra bytes added to reach the 46-byte minimum payload.
  • Ethernet frame: 6 + 6 + 2 + (46 to 1500) + 4 = 64 to 1518 bytes.
7

Switching: learning bridge operation

A switch builds a MAC address table by observing the source address of every incoming frame. It forwards a frame to the port of the destination if known, floods it to all other ports if unknown or broadcast, and filters it if the destination is on the incoming port.

In short: The switch notes which host is behind which port and sends traffic only where it needs to go.

Think about it

Switches replace hubs to give each host a dedicated collision domain.

Learning: source MAC → incoming port (entries age out after a timeout).

Forwarding: destination found → send out that port only.

Flooding: destination unknown or broadcast/multicast → send out all ports except the incoming one.

Filtering: destination on the same port as source → drop.

Switching methods: store-and-forward (checks FCS, highest latency), cut-through (forwards after reading the destination address, lowest latency, may forward bad frames), fragment-free.

Loops in redundant topologies cause broadcast storms, so Spanning Tree Protocol (STP) blocks redundant links.

VLANs split one physical switch into multiple broadcast domains, and inter-VLAN traffic needs a router or Layer-3 switch.

Key points

  • MAC table (CAM): Switch table mapping MAC addresses to ports.
  • Flooding: Sending a frame out all ports except the one it arrived on.
  • STP: Spanning Tree Protocol that removes loops by blocking ports.

Solved examples

Example 01 · Intermediate

Compute the CRC for data 100100 using generator 1101 and give the transmitted codeword.

Given Data:
  • Data = 100100
  • Generator = 1101 (degree 3)
Required To Find:

Remainder and codeword

Formula Applied:—
Step-by-Step Solution:
Step 1: Append 3 zeros: 100100000.
Step 2: Divide by 1101 using XOR: 1001 XOR 1101 = 0100; bring down bits: 1000 XOR 1101 = 0101; 1010 XOR 1101 = 0111; 1110 XOR 1101 = 0011; 0110 has a leading 0 so XOR with 0000 = 0110; 1100 XOR 1101 = 0001.
Step 3: The final remainder (last 3 bits) is 001.
Step 4: Replace the appended zeros by the remainder: codeword = 100100001.
Step 5: Check: 100100001 divided by 1101 leaves remainder 000, so the receiver accepts it.
Final Answer: CRC = 001; transmitted frame = 100100001.

Approach note: XOR only when the leading bit is 1; otherwise shift in the next bit without XOR.

Example 02 · Intermediate

A 10 Mbps CSMA/CD Ethernet has a maximum one-way propagation delay of 25.6 µs. What is the minimum frame size?

Given Data:
  • B = 10 Mbps
  • Tp = 25.6 µs
Required To Find:

Minimum frame size in bits and bytes

Formula Applied:—
Step-by-Step Solution:
Step 1: Round-trip time = 2 × 25.6 µs = 51.2 µs.
Step 2: L = B × 2Tp = 10 × 10^6 × 51.2 × 10^-6 = 512 bits.
Step 3: 512 bits / 8 = 64 bytes.
Final Answer: Minimum frame size = 512 bits = 64 bytes.

Approach note: If the bandwidth increases to 100 Mbps with the same minimum frame size, the maximum cable length shrinks by a factor of 10.

Example 03 · Basic

A switch with empty table receives: (1) frame A→B on port 1; (2) frame B→A on port 2; (3) frame C→A on port 3. Show the table and where each frame is sent.

Given Data:
    Required To Find:

    MAC table and forwarding actions

    Formula Applied:—
    Step-by-Step Solution:
    Step 1: Frame 1 (A→B, port 1): learn A = port 1. B is unknown, so flood out ports 2, 3, 4…
    Step 2: Frame 2 (B→A, port 2): learn B = port 2. A is known on port 1, so forward only to port 1.
    Step 3: Frame 3 (C→A, port 3): learn C = port 3. A is known, so forward only to port 1.
    Step 4: Final table: A → 1, B → 2, C → 3.
    Final Answer: Table: A:1, B:2, C:3. Frame 1 is flooded; frames 2 and 3 go only to port 1.

    Approach note: Always learn from the source address first, then decide using the destination.

    Example 04 · Basic

    Apply bit stuffing to the data 0111110111111 (HDLC rule: insert 0 after five consecutive 1s).

    Given Data:
      Required To Find:

      Stuffed bit string

      Formula Applied:—
      Step-by-Step Solution:
      Step 1: Split the data into runs: 0, 11111, 0, 111111 (the last run has six 1s).
      Step 2: After the first run of five 1s insert a 0: 0 11111 [0] 0 ...
      Step 3: In the last run, after the first five 1s insert a 0, then the remaining 1 follows: ... 11111 [0] 1.
      Step 4: Joining all parts: 0 11111 0 0 11111 0 1.
      Final Answer: 011111001111101 (15 bits: the original 13 plus 2 stuffed zeros).

      Approach note: Count consecutive 1s from the left; reset the count after each inserted 0.

      Real-world applications

      Networks

      Using Data Link Layer & MAC Protocols in real projects

      Ethernet and Wi-Fi LAN design, diagnosing duplex mismatches and broadcast storms, understanding how switches build their MAC tables.

      Mechanism: Write small programs, read errors carefully, then grow complexity only after the basics are solid.

      Common mistakes

      ❌ Myth: CRC can detect every possible error.

      Why this is wrong: Some error patterns happen to be a multiple of the generator polynomial and go undetected.

      ✅ Verified Truth: CRC detects all single-bit, all burst errors up to its degree, and most others, with a small probability of failure.

      ⚠️ Exam Tip: Say "detects with very high probability" rather than "always".

      ❌ Myth: Wi-Fi uses CSMA/CD like Ethernet.

      Why this is wrong: A radio cannot transmit and listen at the same time and hidden terminals exist.

      ✅ Verified Truth: Wi-Fi uses CSMA/CA with acknowledgements and optional RTS/CTS.

      ⚠️ Exam Tip: CD = wired Ethernet, CA = wireless.

      ❌ Myth: A switch needs IP addresses to forward frames.

      Why this is wrong: Layer-2 switching uses only MAC addresses learnt from frames.

      ✅ Verified Truth: IP addresses are used by routers; switches look at MAC addresses.

      ⚠️ Exam Tip: A plain switch only needs an IP for remote management.

      Revision notes

      The data link layer moves frames between neighbouring nodes. This chapter covers framing and bit stuffing, error detection with parity and CRC, shared-medium access (CSMA/CD and CSMA/CA), the Ethernet frame and MAC address, and how learning switches forward frames.

      High-Yield Quick Revision Points

      1. Bit stuffing: 0 after five 1s; flag 01111110
      2. CRC: append r zeros, XOR division, remainder replaces zeros
      3. CSMA/CD: min frame = 2·Tp·B; slot time 51.2 µs at 10 Mbps
      4. Wi-Fi = CSMA/CA; Ethernet = CSMA/CD
      5. Ethernet frame 64–1518 bytes; payload 46–1500
      6. Switch: learn source, forward known, flood unknown/broadcast

      Practice questions

      Question 1 · MCQ (GATE)

      In bit stuffing with flag 01111110, a 0 is inserted after every:

      Question 2 · MCQ (GATE)

      A CRC generator has degree 4 (5 bits). How many zeros are appended to the data before division?

      Question 3 · MCQ (GATE)

      Minimum frame size for CSMA/CD is determined by:

      Question 4 · MCQ (GATE / CCNA)

      Ethernet minimum payload size is:

      Question 5 · MCQ (CCNA)

      A switch receives a frame with an unknown destination MAC. It will:

      Question 6 · MCQ (GATE)

      Single parity bit can detect: