Sub topic 4
Layer 2: Data Link, frames and MAC addresses
Layer 2 moves data between two devices that share the same local link. This chapter covers the Ethernet frame, MAC addresses, how a switch forwards frames, and how Layer 2 fits with Layer 3. It assumes you know what an IP address is, you know that switches and routers are different devices, and you can run commands in a Linux terminal. It also assumes you have read the earlier chapters on encapsulation and the physical layer.
What Layer 2 is responsible for
Layer 1 delivers raw bits and has no idea where they start or stop. Layer 2 turns that bit stream into discrete frames, which are the Layer 2 data unit. It handles four jobs:
- Framing. It marks where one unit of data ends and the next begins.
- Local addressing. It uses a hardware address (the MAC address) to name the sender and receiver on one link.
- Error detection. A checksum lets the receiver discard frames damaged in transit.
- Access to the medium. It decides who may transmit when several devices share the same medium.
The key word is local. A Layer 2 address is only meaningful inside one broadcast domain: one switched network, one Wi-Fi network, or one VLAN. Crossing to another network requires Layer 3 and a router. A router strips off the incoming frame, makes a routing decision using the IP packet inside, and builds a new frame for the next link.
The two sublayers
IEEE 802 standards split Layer 2 into two sublayers:
| Sublayer | Name | Role |
|---|---|---|
| Upper | LLC (Logical Link Control, 802.2) | Identifies the upper-layer protocol. Rarely visible on modern Ethernet because EtherType does this job. |
| Lower | MAC (Media Access Control) | Addressing, framing, and the rules for using the medium. Implemented in the network interface card (NIC) and its driver. |
MAC addresses
A MAC address is a 48-bit identifier, written as six bytes in hexadecimal, such as 00:11:22:33:44:55. Some tools use dashes (00-11-22-33-44-55) or dots (0011.2233.4455). The notation changes but the value does not.
The address has structure:
- First three bytes: the OUI (Organizationally Unique Identifier). The IEEE assigns an OUI to a manufacturer, so the prefix often tells you the vendor.
- Last three bytes: assigned by the manufacturer to each interface.
Two bits in the first byte carry special meaning:
- Bit 0 (least significant), the I/G bit. 0 means unicast (one recipient). 1 means group, which covers multicast and broadcast.
- Bit 1, the U/L bit. 0 means universally administered (assigned by the manufacturer). 1 means locally administered (set by software).
Take 02:42:ac:11:00:02. The first byte is 0x02, which is binary 00000010. The I/G bit is 0, so it is unicast. The U/L bit is 1, so it is locally administered. Container platforms and virtual machines commonly generate addresses like this.
Unicast, multicast, and broadcast
| Type | Example destination | Who receives it |
|---|---|---|
| Unicast | `00:11:22:33:44:55` | One interface |
| Multicast | `01:00:5e:00:00:fb` | Interfaces that have joined that group |
| Broadcast | `ff:ff:ff:ff:ff:ff` | Every device in the broadcast domain |
A NIC normally accepts only frames addressed to its own MAC, to the broadcast address, and to multicast groups it has joined. Promiscuous mode makes it accept everything it sees, which packet capture tools rely on.
<br>
The Ethernet II frame
Ethernet is the dominant Layer 2 technology on wired networks. Its most common format is Ethernet II:
| Field | Size (bytes) | Purpose |
|---|---|---|
| Preamble and SFD | 8 | Alternating bits that let the receiver synchronize its clock, then a start-of-frame delimiter. Handled by hardware and not shown in captures. |
| Destination MAC | 6 | Who the frame is for |
| Source MAC | 6 | Who sent it |
| EtherType | 2 | Which protocol is in the payload |
| Payload | 46 to 1500 | The Layer 3 packet |
| FCS | 4 | Frame Check Sequence, a CRC-32 checksum |
Common EtherType values are 0x0800 for IPv4, 0x0806 for ARP, 0x86DD for IPv6, and 0x8100 for an 802.1Q VLAN tag. EtherType is how the receiving side knows which Layer 3 handler gets the payload.
Size limits
- The header is 14 bytes (6 + 6 + 2). With the 4-byte FCS, the smallest legal frame is 64 bytes, which implies a minimum payload of 46 bytes. Shorter payloads are padded with zeros.
- The standard maximum payload is 1500 bytes. This is the MTU (maximum transmission unit) of a standard Ethernet link. With header and FCS, the largest frame is 1518 bytes, or 1522 with a VLAN tag.
- "Jumbo frames" of about 9000 bytes exist but work only if every device on the path is configured for them.
Parsing a header
The following program splits a 14-byte header into its three fields. The ! in the format string means network byte order (big-endian), 6s means six raw bytes, and H means an unsigned 16-bit number.
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 destination is the broadcast address and the EtherType 0x806 is ARP. That is the shape of an ARP request, which is covered below.
Handling VLAN tags and address types
An 802.1Q tag inserts four bytes after the source MAC. The first two are the tag marker 0x8100, and the next two hold priority bits and a 12-bit VLAN ID. The real EtherType follows. This version of the parser handles both cases and classifies the addresses using the I/G and U/L bits:
import struct
def mac(b):
return ":".join(f"{x:02x}" for x in b)
def kind(addr):
if addr == b"\xff" * 6:
return "broadcast"
return "multicast" if addr[0] & 0x01 else "unicast"
def parse(frame):
dst, src, ethertype = struct.unpack("!6s6sH", frame[:14])
offset, vlan = 14, None
if ethertype == 0x8100:
tci, ethertype = struct.unpack("!HH", frame[14:18])
vlan = tci & 0x0FFF
offset = 18
return dst, src, vlan, ethertype, frame[offset:]
frame = bytes.fromhex(
"01005e0000fb" # destination: multicast
"0242ac110002" # source: locally administered
"8100" "0064" # 802.1Q tag, VLAN 100
"0800" # EtherType: IPv4
"4500" # start of an IP packet (truncated)
)
dst, src, vlan, et, payload = parse(frame)
print(mac(dst), kind(dst))
print(mac(src), kind(src), "locally administered" if src[0] & 0x02 else "universal")
print("vlan", vlan, "ethertype", hex(et), "payload bytes", len(payload))
# 01:00:5e:00:00:fb multicast
# 02:42:ac:11:00:02 unicast locally administered
# vlan 100 ethertype 0x800 payload bytes 2Error detection with the FCS
The sender computes a CRC-32 over the header and payload and appends it as the FCS. The receiver recomputes it. If the values differ, the frame is silently dropped.
Two points matter in practice:
- The FCS detects errors. It does not correct them, and Layer 2 Ethernet does not retransmit. Recovering lost data is the job of a higher layer such as TCP.
- Rising CRC error counters on a switch port usually point to Layer 1 trouble: a bad cable, a dirty connector, or a duplex mismatch.
Switches and the MAC address table
A switch is the characteristic Layer 2 device. It keeps a MAC address table (also called a CAM table or forwarding database) that maps each MAC address to the port where it was seen. It builds the table by observation:
- Learn. For every frame that arrives, record the source MAC against the ingress port.
- Forward. Look up the destination MAC. If it is known, send the frame out of only that port.
- Flood. If the destination is unknown, broadcast, or an unjoined multicast, send the frame out of every port except the one it arrived on.
- Filter. If the destination is on the same port the frame came in on, drop it.
- Age out. Remove entries that have not been refreshed for a while (commonly 300 seconds).
You can view the table on a Linux bridge with bridge fib show. Vendor switches have their own commands.
Collision domains and broadcast domains
On old shared Ethernet (hubs), all devices were in one collision domain and used CSMA/CD (carrier sense multiple access with collision detection) to take turns. Modern switched, full-duplex links have no collisions. Each switch port is its own collision domain.
A broadcast domain is the set of devices that receive each other's broadcasts. A switch does not split it. A router or a VLAN does. Wi-Fi still shares a medium, so it uses CSMA/CA (collision avoidance) instead.
VLANs
A VLAN (virtual LAN) divides one physical switch into several separate broadcast domains. Ports are assigned to a VLAN, and links between switches (trunks) carry the 802.1Q tag so the far end knows which VLAN each frame belongs to. Traffic between VLANs must pass through a Layer 3 device.
How ARP connects Layer 3 to Layer 2
A host that wants to send an IP packet to 192.168.1.20 knows the IP address but needs a destination MAC for the frame. ARP (Address Resolution Protocol) supplies it:
- The host sends an ARP request to
ff:ff:ff:ff:ff:ff, asking who has192.168.1.20. - The owner replies by unicast with its MAC address.
- The asker caches the answer in its ARP (neighbor) table.
If the destination IP is on a different network, the host does not ARP for it. It ARPs for the default gateway and puts the gateway's MAC in the frame. The IP destination stays the remote host, while the Ethernet destination changes at every hop. This is the most important idea linking Layers 2 and 3.
ip link show # interface state, MAC address, MTU
ip neigh show # ARP/neighbor cache: IP to MAC mappings
ip route show default # which gateway will be ARPed for
bridge fib show # MAC table of a Linux bridge, if you have oneFailure modes and gotchas
- Switching loops. If two switches are connected by more than one path, broadcasts circle forever and multiply into a broadcast storm that can take down the network. Ethernet frames have no hop limit, so the loop never ends by itself. Spanning Tree Protocol (STP) prevents this by blocking redundant links.
- Duplex mismatch. One side uses full duplex while the other uses half duplex. The link comes up but is slow, with late collisions and CRC errors.
- MTU mismatch. A frame larger than a link's limit is dropped, or the IP packet must be fragmented. Symptoms are that small pings work while large transfers stall.
- MAC table overflow. An attacker flooding a switch with fake source addresses can fill its table so that it floods all traffic. Port security limits the damage.
- ARP spoofing. ARP has no authentication. A host can claim another host's IP and intercept traffic. Defenses include dynamic ARP inspection on managed switches.
- Duplicate MACs. Cloned virtual machines can end up with the same MAC on one VLAN, causing intermittent connectivity.
Summary of key points
| Idea | Takeaway |
|---|---|
| Scope | A Layer 2 address works only within one local network |
| Address | MAC is 48 bits, with an OUI and device part, plus I/G and U/L bits |
| Frame | Destination, source, EtherType, payload, then FCS |
| Switch | Learns source MACs, forwards on destination MACs, floods when unsure |
| Link to Layer 3 | ARP maps an IP address to a MAC on the local link |
Exercise
Host A (192.168.1.10) sends a packet to a web server at 203.0.113.5 on the internet. Its default gateway is 192.168.1.1. For the first frame leaving host A, what are the destination IP address and the destination MAC address? Which protocol does host A use to obtain that MAC address?
Show answer
The destination IP address is 203.0.113.5, the final server. The destination MAC address is the MAC of the default gateway (192.168.1.1), because the server is not on the local network. Host A learns the gateway's MAC with ARP: it broadcasts a request to ff:ff:ff:ff:ff:ff and the gateway replies by unicast. The router then builds a new frame with a new MAC pair for the next link, while the IP destination stays the same.
Exercise
A switch receives a frame on port 4 with source MAC 00:aa:bb:cc:dd:01 and destination MAC 00:aa:bb:cc:dd:02. The destination is not in its MAC table. What does the switch do, and what does it record?
Show answer
It records that 00:aa:bb:cc:dd:01 is reachable via port 4 (learning from the source address). Because the destination is unknown, it floods the frame out of every port except port 4. When 00:aa:bb:cc:dd:02 replies, the switch learns its port, and later frames between the two hosts are forwarded on single ports.
Exercise
The first byte of a destination MAC address is 0x01. Is the frame unicast or group-addressed, and which bit tells you?
Show answer
It is group-addressed (multicast). The least significant bit of the first byte, the I/G bit, is 1 (0x01 is binary 00000001). Only the all-ones address ff:ff:ff:ff:ff:ff is broadcast.