Data Link Layer Fundamentals

The data link layer is the second layer of the OSI reference model, sitting above the physical layer and below the network layer. Its core task is to provide reliable and efficient data transfer between adjacent nodes — the two nodes connected directly by a physical link.

The physical layer only transmits a raw bit stream and is not concerned with the meaning or structure of those bits. The data link layer organizes the bit stream into frames, and uses mechanisms such as error control and flow control to turn a physical link that may corrupt data into a data link that appears error-free to the network layer.

Data Link

A data link is formed when a physical link is combined with the hardware and software that implement a data transmission protocol. A data link is a logical concept, while a physical link is a physical one.

1. Framing

The IP datagrams (packets) handed down by the network layer are encapsulated into frames following a defined format. Control information is added to the head and tail of the frame so that the receiver can identify the start and end of each frame within the received bit stream.

The central problem framing must solve is: how does the receiver distinguish the boundaries of one frame inside a continuous bit stream?

2. Error Control

Noise and interference on a physical link can flip bits during transmission (a 0 becoming a 1, or a 1 becoming a 0). Error control covers two things:

  • Error detection: finding out whether transmission produced errors
  • Error correction: finding errors and repairing them automatically

Common error-detection codes include parity check and cyclic redundancy check (CRC); the common error-correction code is Hamming code.

3. Flow Control

The sender’s transmission rate is limited so that it does not exceed the receiver’s ability to receive and process, which prevents the receiver’s buffer from overflowing and losing data. The common mechanism is the sliding window protocol.

4. Reliable Transmission

Acknowledgment (ACK) and retransmission mechanisms ensure that data reaches the receiver correctly and in order. When a frame is corrupted or lost, the sender transmits it again.

5. Medium Access Control (MAC)

When several nodes share the same broadcast channel, as in Ethernet, rules are needed to govern how nodes access the channel and avoid collisions. This is the responsibility of the medium access control sublayer (the MAC sublayer).

Framing Methods

Framing works by adding special markers at the start and end of a frame so that the receiver can identify the frame boundaries. Four framing methods are commonly used.

1. Character Count

A field in the frame header states the number of bytes in the frame. By reading this count field, the receiver learns the length of the frame and therefore where the frame ends.

Advantages: simple, no padding required Disadvantages: if the count field itself is corrupted in transit, the receiver cannot identify the frame boundary correctly, and every subsequent frame becomes misaligned (frame synchronization is lost)

This method was used in DECNET’s DDCMP protocol.

2. Character Stuffing

Special control characters serve as the start and end markers of a frame. For example:

  • FLAG (a flag byte such as 0x7E) delimits the frame
  • when the data contains a byte identical to FLAG, an escape character ESC (such as 0x7D) is inserted before it
  • if the data contains ESC, another ESC is inserted before it

After receiving an ESC, the receiver knows the next byte is data rather than a control character, and deletes the ESC.

Advantages: recovery from errors is possible (finding the FLAG again restores synchronization) Disadvantages: depends on a specific character set and is therefore coupled to the character encoding; stuffing and destuffing add processing overhead

This method was used in the early BISYNC protocol and in PPP (asynchronous mode).

3. Bit Stuffing

A special bit pattern (such as 01111110, that is, six consecutive 1 bits) serves as the frame delimiter.

Sender: whenever five consecutive 1 bits appear in the data, a 0 bit is inserted after them. The data therefore never contains six consecutive 1 bits.

Receiver: whenever five consecutive 1 bits arrive, the next bit is checked:

  • if it is 0, the 0 is removed (restoring the data)
  • if it is 1, the pattern is the frame delimiter (01111110)

4. Physical Layer Violations

Encoding patterns that are unused in the physical layer encoding are used as frame delimiters. In Manchester encoding, for example, every bit has a transition in its middle (high to low means 1, low to high means 0), so the absence of a transition is a violation and can mark the start and end of a frame.

Advantages: no padding, and the data is unaffected Disadvantages: depends on a specific physical layer encoding, so it is not general-purpose

This method was used in LANs such as 802.5 Token Ring.

Error Control

Parity Check

Parity check is the simplest error-detection code. A parity bit is appended after the data bits so that the number of 1 bits in the whole codeword (data bits plus parity bit) is odd (odd parity) or even (even parity).

Odd parity: the parity bit makes the total number of 1 bits odd Even parity: the parity bit makes the total number of 1 bits even

For example, the data 1011001 contains four 1 bits:

  • odd parity: the parity bit is 1 (4+1=5, odd), and the codeword is 10110011
  • even parity: the parity bit is 0 (4+0=4, even), and the codeword is 10110010

The receiver counts the 1 bits in the received codeword; if the agreed parity is not satisfied, transmission went wrong.

Detection capability: it detects an odd number of bit errors but not an even number, so its detection capability is weak and it is generally used where requirements are modest (such as ASCII transmission).

Cyclic Redundancy Check (CRC)

CRC is the most widely used error-detection code today: it has strong detection capability and high computational efficiency, and almost every modern network protocol (Ethernet, PPP, USB, and so on) uses it.

The Basic Idea of CRC

Treat the data to be sent as the coefficients of a polynomial M(x)M(x). Choose a generator polynomial G(x)G(x) (agreed in advance by both ends), perform modulo-2 division of M(x)M(x) by G(x)G(x), and the remainder R(x)R(x) is the CRC code. The remainder R(x)R(x) is appended to the data and sent.

The receiver performs modulo-2 division of the received data (including the code) by the same G(x)G(x). A remainder of 0 means no error; a non-zero remainder means an error occurred.

The Steps of CRC Computation

Let the data be kk bits and let the generator polynomial G(x)G(x) have degree rr (that is, G(x)G(x) has r+1r+1 bits):

  1. Left shift: shift the data MM left by rr bits (append rr zero bits to the end of the data) to obtain M⋅2rM \cdot 2^r
  2. Modulo-2 division: divide M⋅2rM \cdot 2^r by GG using modulo-2 arithmetic (exclusive OR) to obtain the rr-bit remainder RR
  3. Append: append the remainder RR to the data MM, giving the transmitted codeword T=M⋅2r+RT = M \cdot 2^r + R

A CRC Computation Example

Problem: let the data be M=101001M = 101001 (6 bits) and the generator polynomial be G(x)=x3+x2+1G(x) = x^3 + x^2 + 1, that is, G=1101G = 1101 (4 bits, r=3r=3). Find the CRC code.

Solution:

  1. Shift the data left by 3 bits: M⋅23=101001000M \cdot 2^3 = 101001000
  2. Perform modulo-2 division by G=1101G = 1101:
        110101      (quotient, not needed)
    ___________
1101|101001000
     1101
     ----
      1110
      1101
      ----
       0111
       0000
       ----
        1110
        1101
        ----
         0110
         0000
         ----
          1100
          1101
          ----
           001    (remainder R = 001)
  1. The remainder is R=001R = 001, so the CRC code is 001
  2. The transmitted codeword is T=101001001T = 101001001

After the receiver obtains 101001001, division by 1101 leaves a remainder of 0, so the frame arrived without error.

Common Generator Polynomials

NameGenerator PolynomialApplication
CRC-8x8+x2+x+1x^8+x^2+x+1ATM
CRC-16x16+x15+x2+1x^{16}+x^{15}+x^2+1IBM SDLC
CRC-CCITTx16+x12+x5+1x^{16}+x^{12}+x^5+1X.25, V.41
CRC-32x32+x26+x23+x22+x16+x12+x11+x10+x8+x7+x5+x4+x2+x+1x^{32}+x^{26}+x^{23}+x^{22}+x^{16}+x^{12}+x^{11}+x^{10}+x^8+x^7+x^5+x^4+x^2+x+1Ethernet, ZIP

Hamming Code

Hamming code is an error-correction code proposed by Richard Hamming in 1950. It not only detects errors but can also correct single-bit errors.

Basic Principle

Several parity bits are inserted among the data bits, and each parity bit performs a parity check over a specific set of bits. When a single-bit error occurs, the results of several parity groups point to the position of the error, which allows the error to be located and corrected.

Encoding Rules for Hamming Code

Let the data consist of mm bits and the parity bits consist of kk bits. The following must hold:

2k≥m+k+12^k \geq m + k + 1

The parity bits are placed at the positions that are powers of two (that is, positions 1, 2, 4, 8, …), and the data bits occupy the remaining positions.

Each parity bit PiP_i (located at position 2i−12^{i-1}) checks all positions whose binary index has a 1 in the ii-th bit.

A Hamming Code Example

Problem: the data is 1011 (4 bits). Find the Hamming code.

Solution:

  1. Determine the number of parity bits: m=4m=4 and we need 2k≥4+k+12^k \geq 4+k+1. For k=3k=3, 23=8≥82^3=8 \geq 8, so the condition holds. Hence k=3k=3 parity bits, and the total length is n=4+3=7n=4+3=7.

  2. Position layout (parity bits at positions 1, 2, and 4):

Position1234567
ContentP1P2D1P3D2D3D4
Data??1?011
  1. Compute the parity bits (even parity):

    • P1P_1 (position 1) covers positions 1, 3, 5, 7: P1⊕D1⊕D2⊕D4=0P_1 \oplus D_1 \oplus D_2 \oplus D_4 = 0, that is, P1⊕1⊕0⊕1=0P_1 \oplus 1 \oplus 0 \oplus 1 = 0, so P1=0P_1 = 0
    • P2P_2 (position 2) covers positions 2, 3, 6, 7: P2⊕D1⊕D3⊕D4=0P_2 \oplus D_1 \oplus D_3 \oplus D_4 = 0, that is, P2⊕1⊕1⊕1=0P_2 \oplus 1 \oplus 1 \oplus 1 = 0, so P2=1P_2 = 1
    • P3P_3 (position 4) covers positions 4, 5, 6, 7: P3⊕D2⊕D3⊕D4=0P_3 \oplus D_2 \oplus D_3 \oplus D_4 = 0, that is, P3⊕0⊕1⊕1=0P_3 \oplus 0 \oplus 1 \oplus 1 = 0, so P3=0P_3 = 0
  2. The Hamming code is: 0 1 1 0 0 1 1 (positions 1 through 7)

Error Correction with Hamming Code

After the receiver obtains the codeword, it checks each parity group and combines the results into a binary number (the correction factor). A correction factor of 0 means there is no error. A non-zero correction factor is the position number of the erroneous bit, and inverting that bit corrects the error.

For example, suppose 0110011 is received and bit 5 is corrupted, giving 0110111:

  • Parity group 1 (positions 1, 3, 5, 7): 0⊕1⊕1⊕1=10 \oplus 1 \oplus 1 \oplus 1 = 1 (error)
  • Parity group 2 (positions 2, 3, 6, 7): 1⊕1⊕1⊕1=01 \oplus 1 \oplus 1 \oplus 1 = 0 (correct)
  • Parity group 3 (positions 4, 5, 6, 7): 0⊕1⊕1⊕1=10 \oplus 1 \oplus 1 \oplus 1 = 1 (error)
  • Correction factor = P3P2P1=1012=5P_3 P_2 P_1 = 101_2 = 5, so bit 5 is in error and is corrected by inversion.

Worked Examples

Example 1: CRC Computation

The data to be sent is 1101011011 and the generator polynomial is G(x)=x4+x+1G(x) = x^4 + x + 1 (that is, $G = 10011`). Find the CRC code and the transmitted codeword.

CRCCyclic Redundancy CheckModulo-2 DivisionError Detection

Approach: shift the data left by the degree of the generator polynomial, divide modulo 2, and append the remainder.

Steps:

  1. The data is M=1101011011M = 1101011011 (10 bits) and G=10011G = 10011 (5 bits, r=4r=4)
  2. Shift the data left by 4 bits: M⋅24=11010110110000M \cdot 2^4 = 11010110110000
  3. Perform modulo-2 division by G=10011G = 10011:
11010110110000 ÷ 10011
10011
-----
 10011
 10011
 -----
  00001
  00000
  -----
   00011
   00000
   -----
    00110
    00000
    -----
     01100
     00000
     -----
      11000
      10011
      -----
       10110
       10011
       -----
        01010
        00000
        -----
         1010    (remainder R = 1010, 4 bits)
  1. The CRC code is 1010
  2. The transmitted codeword is 11010110111010

Answer: the CRC code is 1010 and the transmitted codeword is 11010110111010.

Example 2: Hamming Code Encoding

The data is 1001 (4 bits). Using even parity, find the Hamming code.

Hamming CodeError CorrectionEven ParityParity Bit Computation

Approach: place the parity bits at the powers of two, then solve each parity group for even parity.

Steps:

  1. m=4m=4, k=3k=3 (23=8≥4+3+1=82^3=8 \geq 4+3+1=8), so the total length is n=7n=7
  2. Position layout:
Position1234567
ContentP1P2D1P3D2D3D4
Data??1?001
  1. Compute the parity bits (even parity):

    • P1P_1 (positions 1, 3, 5, 7): P1⊕1⊕0⊕1=0⇒P1=0P_1 \oplus 1 \oplus 0 \oplus 1 = 0 \Rightarrow P_1 = 0
    • P2P_2 (positions 2, 3, 6, 7): P2⊕1⊕0⊕1=0⇒P2=0P_2 \oplus 1 \oplus 0 \oplus 1 = 0 \Rightarrow P_2 = 0
    • P3P_3 (positions 4, 5, 6, 7): P3⊕0⊕0⊕1=0⇒P3=1P_3 \oplus 0 \oplus 0 \oplus 1 = 0 \Rightarrow P_3 = 1
  2. The Hamming code is: 0 0 1 1 0 0 1

Answer: the Hamming code is 0011001.

Example 3: Analyzing a Framing Method

A data link layer protocol uses bit stuffing with the flag pattern 01111110. If the sender’s data is 011111101111101111110, write the actual bit sequence transmitted after bit stuffing.

Bit StuffingFramingFrame DelimitingHDLC

Approach: walk through the data and insert a 0 after every run of five consecutive 1 bits, then wrap the result in the flag pattern.

Steps:

  1. Original data: 011111101111101111110
  2. Bit stuffing rule: whenever five consecutive 1 bits appear, insert a 0 after them
  3. Process the data segment by segment:
    • 01111 → unchanged
    • next 110: the preceding 01111 (four 1 bits) plus this 1 gives five consecutive 1 bits → insert a 0 after the fifth 1 → 0111110
    • next: 1011111 → five consecutive 1 bits appear again → insert a 0 → 10111110
    • next: 01111110 → five consecutive 1 bits → insert a 0 → 011111010
  4. Stuffed data: 011111010111110011111010
  5. Add the leading and trailing flag 01111110: Actually transmitted: 01111110 011111010111110011111010 01111110

Answer: the transmitted bit sequence is 0111111001111101011111001111101001111110 (flags at both ends, stuffed data in between).


Exercises

Exercise 1

Briefly describe the main functions of the data link layer, and explain why framing is needed.

Data Link Layer FunctionsFramingError ControlFlow Control

Approach: list the five functions, then explain what the physical layer fails to provide.

Steps:

The main functions of the data link layer are:

  1. Framing: encapsulate the packets handed down by the network layer into frames, adding a header and trailer so that the receiver can identify frame boundaries within the bit stream.
  2. Error control: detect and correct transmission errors using error-detection codes (parity check, CRC) and error-correction codes (Hamming code).
  3. Flow control: control the sending rate so that the receiver’s buffer cannot overflow.
  4. Reliable transmission: use acknowledgment and retransmission to ensure that data arrives reliably.
  5. Medium access control: resolve access conflicts on a shared channel.

Why framing is needed: the physical layer transmits a raw bit stream with no structure and no boundaries. Without framing, the receiver cannot know where a piece of data begins or ends, and cannot run error detection over it. Framing organizes the bit stream into independent units (frames), which is the foundation of error control and reliable transmission.

Answer: the data link layer provides framing, error control, flow control, reliable transmission, and medium access control. Framing is needed because the physical layer provides only a raw bit stream, and framing is what makes error detection and reliable transmission possible.

Exercise 2

The data is 1101 and the generator polynomial is G(x)=x3+x+1G(x) = x^3 + x + 1 (that is, G=1011G=1011). Find the CRC code.

CRCCyclic Redundancy CheckModulo-2 Division

Approach: shift the data left by 3 bits, then divide modulo 2 by 1011.

Steps:

  1. The data is M=1101M = 1101 (4 bits) and G=1011G = 1011 (4 bits, r=3r=3)
  2. Shift left by 3 bits: 1101000
  3. Modulo-2 division:
1101000 ÷ 1011
1011
----
 1100
 1011
 ----
  1110
  1011
  ----
   101    (remainder)
  1. The remainder is R=101R = 101

Answer: the CRC code is 101.


Glossary

TermAbbreviationExplanation
Data Link LayerDLLSecond layer of the OSI model
Frame-The protocol data unit of the data link layer
Framing-Organizing a bit stream into frames
Error Control-Detecting and correcting transmission errors
Flow Control-Controlling the sending rate
Cyclic Redundancy CheckCRCA widely used error-detection code
Hamming Code-An error-correction code that fixes single-bit errors
Parity Check-The simplest error-detection code
Bit Stuffing-A framing method used by protocols such as HDLC
Medium Access ControlMACAccess control on a shared channel
Generator Polynomial-The polynomial used in CRC computation

Found an issue with this content?

Tell us what could be improved.