Layer 2: The Data Link Layer
Learn how the Data Link layer uses MAC addresses and frames to move data across a local link, and how switches forward frames.
In this lesson, you will learn to:
- By the end of this lesson, learners will be able to explain the role of MAC addresses, describe how frames are structured and checked for errors, and describe how switches forward frames on a local network.
Layer 2: The Data Link Layer
This lesson explains the Data Link layer, where frames are created and physical addressing occurs. Learners will explore MAC addresses, frame structure, error detection, switches, and the difference between Layer 2 and Layer 3 addressing.
Frames and Physical Addressing
The Data Link layer is the second layer of the OSI model. Its job is to move data across a local link, such as the connection between a computer and a switch, or between two switches in the same network. It works directly above the Physical layer.
At the Data Link layer, data from the Network layer is packaged into a frame. A frame is the Protocol Data Unit for Layer 2. It contains a header and a trailer around the packet from the Network layer.
What the Data Link layer does
The Data Link layer has several important responsibilities:
- Framing. It packages packets into frames with clear boundaries.
- Physical addressing. It adds source and destination MAC addresses.
- Error detection. It includes a trailer, often with a frame check sequence, so the receiver can detect corruption.
- Access to the medium. It helps devices share the physical link without constant collisions.
Think of the Data Link layer as the local delivery step. When you mail a letter, the Network layer decides which city to send it to. The Data Link layer is like the delivery person who moves the letter from the mailbox to the local post office. It is concerned with the current hop, not the whole journey.
MAC Addresses and Frame Structure
The most common physical address at the Data Link layer is the MAC address. MAC stands for Media Access Control. A MAC address is a 48-bit identifier usually written as six pairs of hexadecimal digits separated by colons or dashes.
Example: 00:1A:2B:3C:4D:5E
Every network interface, such as a Wi-Fi adapter or Ethernet port, has a MAC address. The address is usually assigned by the manufacturer and is intended to be unique to that interface. It is used to deliver frames on the local network.
Structure of a frame
A typical Ethernet frame contains:
- Destination MAC address. The physical address of the next device that should receive the frame.
- Source MAC address. The physical address of the device sending the frame.
- Type or length field. Identifies the protocol carried in the frame.
- Payload. The data from the Network layer, usually an IP packet.
- Frame Check Sequence. A trailer used to detect errors.
MAC addresses versus IP addresses
It is important to keep these two address types separate. A MAC address is a Layer 2 physical address. An IP address is a Layer 3 logical address.
- A MAC address identifies a device on the local link.
- An IP address identifies a device within an internetwork and is used for routing.
- A frame uses MAC addresses. A packet uses IP addresses.
When data travels across multiple routers, the source and destination IP addresses stay the same from end to end, but the source and destination MAC addresses change at each local hop. This is a key insight for understanding how the layers work together.
Switches and Local Delivery
A switch is the primary device that operates at the Data Link layer. A switch connects devices on a local network and forwards frames based on MAC addresses.
How a switch learns MAC addresses
When a switch receives a frame, it reads the source MAC address and records which port the frame came from. Over time, the switch builds a table that maps MAC addresses to ports. This is called a MAC address table or Content Addressable Memory table.
When a frame arrives destined for a known MAC address, the switch forwards it only out of the port associated with that address. If the destination is not in the table, the switch floods the frame out of all ports except the one it came from.
Why switches are efficient
Because a switch forwards frames only to the correct port, devices on different ports do not see traffic that is not meant for them. This improves performance and reduces unnecessary traffic. It is a major improvement over older hubs, which repeated every frame to every port.
Layer 2 and VLANs
Many managed switches support Virtual Local Area Networks, which create separate Layer 2 broadcast domains within the same physical switch. This is a more advanced topic, but it shows how the Data Link layer is used to segment traffic and improve security. VLANs allow a single switch to behave like multiple separate switches.
Key takeaway
The Data Link layer handles local delivery. Switches use MAC addresses to move frames within a local network. Routers at the Network layer move packets between networks. Understanding the boundary between these two layers is one of the most useful skills in networking and troubleshooting.