Sub topic 4
Layer 2: Data Link, frames and MAC addresses
This chapter assumes you know what an IP address is and can run commands in a terminal. It also assumes you have met the idea of layers and encapsulation from earlier chapters. Here you will see how Layer 2 moves data between two devices on the same link.
What Layer 2 does
Layer 1 moves raw bits. Layer 2 gives those bits structure and delivers them to the next device on the same local network. It has four jobs:
- Framing: it groups bits into a unit called a frame, with a clear start and end.
- Addressing: it labels each frame with a source and a destination hardware address.
- Error detection: it adds a checksum so the receiver can discard damaged frames.
- Medium access: it decides who may transmit on a shared medium. Modern switched Ethernet rarely needs this.
Layer 2 only reaches devices on the same local segment. Crossing between networks is Layer 3's job.
MAC addresses
A MAC address is a 48-bit (6-byte) hardware address, written as six hex pairs such as 00:11:22:33:44:55. The first three bytes are the Organizationally Unique Identifier (OUI), which identifies the manufacturer. The last three bytes identify the device within that manufacturer's range.
Some values have special meaning:
ff:ff:ff:ff:ff:ffis the broadcast address. Every device on the segment accepts the frame.- If the lowest bit of the first byte is 1, the address is multicast.
- If the second-lowest bit of the first byte is 1, the address is locally administered. Software assigned it, which is common for virtual machines and phones that randomize their MAC.
The Ethernet II frame
Ethernet II is the frame format you will see most often.
| Field | Size | Purpose |
|---|---|---|
| Destination MAC | 6 bytes | Next device that should receive the frame |
| Source MAC | 6 bytes | Device that sent the frame |
| EtherType | 2 bytes | Upper-layer protocol, such as `0x0800` for IPv4 or `0x0806` for ARP |
| Payload | 46 to 1500 bytes | The Layer 3 packet |
| FCS | 4 bytes | Frame check sequence, a CRC checksum |
The preamble that precedes the frame on the wire is handled by the network card. Capture tools normally don't show it or the FCS.
This script parses the 14-byte header:
import struct
def mac(b):
return ":".join(f"{x:02x}" for x in b)
# 14-byte Ethernet II header: destination MAC, source MAC, EtherType
header = bytes.fromhex("ffffffffffff" "001122334455" "0806")
dst, src, ethertype = struct.unpack("!6s6sH", header)
print(mac(dst), mac(src), hex(ethertype))
# ff:ff:ff:ff:ff:ff 00:11:22:33:44:55 0x806The format string !6s6sH means network byte order, two 6-byte fields, then one unsigned 16-bit integer. The destination is the broadcast address and the EtherType 0x0806 marks ARP. The receiver reads the EtherType to decide which protocol handles the payload.
How a switch uses MAC addresses
A switch is the typical Layer 2 device. It keeps a MAC address table that maps each address to a port.
- When a frame arrives, the switch records the source MAC against the incoming port.
- If the destination MAC is in the table, the switch forwards the frame out that one port.
- If the destination is unknown, broadcast, or multicast, the switch floods the frame out every other port.
MAC and IP together
An IP address names the final destination. The MAC address names only the next hop. When your laptop sends a packet to a server on another network, the destination IP stays the same all the way. The frame's destination MAC is your default gateway's address. Each router then strips the old frame and builds a new one with fresh MAC addresses for the next link.
To find the MAC for a known IP on the local network, the host uses ARP. It broadcasts "who has this IP?" and the owner replies with its MAC. You can see the results with ip neigh show.
Common problems
- Wrong EtherType assumption: a value of
0x8100means a VLAN tag follows, so the real EtherType is four bytes later. - MTU mismatch: standard Ethernet carries at most 1500 bytes of payload. Larger packets must be fragmented or dropped.
- FCS or CRC errors: counters on a switch port that keep rising usually point to a bad cable, a faulty port, or a duplex mismatch. This is a Layer 1 cause that shows up at Layer 2.
- Duplicate MAC addresses: two devices with the same address make the switch table flip between ports, and traffic reaches the wrong device.
- Switching loops: redundant links without Spanning Tree Protocol cause broadcasts to circulate endlessly.
Exercise
A frame arrives at a switch with a destination MAC that is not in the switch's MAC table. What does the switch do, and what will it learn from the frame?
Show answer
It floods the frame out every port except the one it arrived on. It also records the frame's source MAC address against the incoming port, so later frames to that address are forwarded to one port only.
Exercise
Your laptop sends a packet to a web server in another country. Does the destination MAC in the first frame belong to the server? Explain.
Show answer
No. The destination MAC belongs to the next hop, normally your default gateway. The destination IP stays the server's address, while each router rebuilds the frame with new MAC addresses for the next link.