Sub topic 8
Link Layer: The Ethernet Frame
This chapter assumes you know what an IP address is, can read hexadecimal bytes, and can run commands in a terminal. It also assumes you have seen the transport and network layers in the earlier chapters: a TCP segment sits inside an IP packet. Here we look at the last wrapper, the Ethernet frame, and at what the link layer does and does not do.
What the link layer is responsible for
The link layer moves a frame between two devices that share the same physical or logical network segment. That might be a host and a switch on a cable, or a host and a Wi-Fi access point. It does not decide the full route to the destination. That is the network layer's job.
The link layer has three tasks:
- It adds addressing that is meaningful on one link only (the MAC address).
- It marks where the frame starts and ends on the medium.
- It adds a check value so that the receiver can detect damaged frames.
Ethernet is the most common wired link-layer technology, and this chapter uses Ethernet II framing, the format used by IP traffic on almost every network.
Anatomy of an Ethernet II frame
An Ethernet II frame has a 14-byte header, a payload, and a 4-byte trailer.
| Field | Size | Purpose |
|---|---|---|
| Destination MAC | 6 bytes | Link-layer address of the next device that should receive the frame |
| Source MAC | 6 bytes | Link-layer address of the sender on this link |
| EtherType | 2 bytes | Identifies the protocol inside the payload |
| Payload | 46 to 1500 bytes | The network-layer packet, for example an IPv4 packet |
| Frame check sequence (FCS) | 4 bytes | CRC-32 value used to detect corruption |
The Ethernet header is the only header in this stack that is followed by a trailer. The FCS is computed over the whole frame, from the destination MAC to the end of the payload, and is appended at the end.
MAC addresses
A MAC address is a 48-bit identifier, written as six pairs of hex digits such as aa:bb:cc:dd:ee:ff. The first three bytes are the Organizationally Unique Identifier (OUI), which identifies the manufacturer. The last three bytes are assigned by that manufacturer.
Two bits in the first byte have special meaning:
- The least significant bit of the first byte is the group bit. If it is 1, the address is multicast. The address
ff:ff:ff:ff:ff:ffis the broadcast address, and every device on the link accepts it. - The next bit is the locally administered bit. If it is 1, the address was set by software rather than burned in by the manufacturer. Virtual machines, containers, and phones that randomize their MAC for privacy often use such addresses.
A MAC address only has meaning on the local link. A router does not forward a frame as it is. It receives the frame, removes the Ethernet header, and builds a new one for the next link.
EtherType
The EtherType tells the receiver which protocol to hand the payload to. This is how the link layer knows what is inside, in the same way that the IP protocol number identifies TCP or UDP.
| EtherType | Payload |
|---|---|
| `0x0800` | IPv4 |
| `0x0806` | ARP |
| `0x86DD` | IPv6 |
| `0x8100` | 802.1Q VLAN tag follows |
Values of 1500 or less (0x05DC) were used by the older IEEE 802.3 format, where this field was a length. Values of 1536 (0x0600) and above are EtherTypes. Modern IP traffic uses Ethernet II, so you will nearly always see an EtherType.
Minimum size and padding
An Ethernet frame must be at least 64 bytes, counting the header and the FCS. That leaves a minimum payload of 46 bytes. If the packet is shorter, for example a tiny ARP message, the sender adds padding bytes after the payload. The receiver uses the length in the IP header to discard the padding. This is a common reason why a captured frame is longer than the IP packet it carries.
What you do not see: preamble and FCS
On the wire, each frame is preceded by a 7-byte preamble and a 1-byte start frame delimiter. These let the receiver synchronize its clock. The network card strips them before the operating system sees the frame. The card also checks the FCS and normally drops frames that fail it, so a capture usually shows neither the preamble nor the FCS. The Python example in this tutorial omits the FCS for the same reason.
Building a frame in code
The following program builds the frame by hand and then unpacks it again. This chapter's focus is the last wrapping step, the 14-byte Ethernet header. Look for the line that packs dst_mac, src_mac and 0x0800.
import struct
http = b"GET / HTTP/1.1\r\nHost: example.com\r\n\r\n"
# Transport: TCP header (20 bytes, no options) + application data
# Flags 0x018 = PSH + ACK. The checksum is left as 0 in this demo.
tcp = struct.pack("!HHIIHHHH", 50000, 80, 1, 0, (5 << 12) | 0x018, 65535, 0, 0) + http
# Network: IPv4 header (20 bytes, no options) + TCP segment
# Protocol 6 = TCP. The header checksum is left as 0 in this demo.
src_ip = bytes([192, 168, 1, 10])
dst_ip = bytes([203, 0, 113, 5])
ip = struct.pack("!BBHHHBBH4s4s", 0x45, 0, 20 + len(tcp), 0, 0, 64, 6, 0, src_ip, dst_ip) + tcp
# Link: Ethernet II header (14 bytes) + IP packet. EtherType 0x0800 = IPv4.
# The frame check sequence is added by the network card, so it is omitted.
dst_mac = bytes.fromhex("aabbccddeeff")
src_mac = bytes.fromhex("112233445566")
frame = struct.pack("!6s6sH", dst_mac, src_mac, 0x0800) + ip
# Decapsulate: peel the headers off in the reverse order
_, _, ethertype = struct.unpack("!6s6sH", frame[:14])
packet = frame[14:]
ihl = (packet[0] & 0x0F) * 4
(total_len,) = struct.unpack("!H", packet[2:4])
ttl, proto = packet[8], packet[9]
segment = packet[ihl:total_len]
sport, dport = struct.unpack("!HH", segment[:4])
offset = (segment[12] >> 4) * 4
data = segment[offset:]
print(hex(ethertype), ttl, proto, sport, dport)
print(data.decode())The output is:
0x800 64 6 50000 80
GET / HTTP/1.1
Host: example.com
Three points are worth noting:
- The format string
"!6s6sH"means network byte order, two 6-byte fields, and one 16-bit unsigned integer. That matches the table above. - The receiver reads the EtherType first. That value says the next 20 or more bytes are an IPv4 header.
- The slice
packet[ihl:total_len]uses the IP total length. If the frame had been padded, this slice would drop the padding bytes.
Whose MAC address is it? The next hop, not the destination
The destination MAC in a frame is the address of the next device on the current link. It is not necessarily the address of the final destination.
Suppose host A (192.168.1.10) sends to a server (203.0.113.5) on another network. The server is not on A's local network, so A sends the frame to its default gateway. The frame carries:
- Destination MAC: the gateway's MAC.
- Source MAC: A's MAC.
- Destination IP: the server's IP, unchanged.
- Source IP: A's IP, unchanged.
If you wrongly put the server's MAC in the frame, there would be no way to reach it, because the server is not on the local link. The host chooses the destination MAC by asking whether the destination IP is on its own subnet. If it is, the host needs that host's MAC. If it is not, the host needs the gateway's MAC. The next chapters cover how the host finds this MAC address with ARP.
How switches use the frame
A switch works at the link layer. It reads only the Ethernet header. It does not look inside the IP packet.
A switch keeps a table that maps MAC addresses to ports. It learns this table by watching the source MAC of arriving frames. When a frame arrives, the switch does the following:
- It records that the source MAC was seen on the arrival port.
- It looks up the destination MAC.
- If the table has an entry, it sends the frame out of that port only.
- If the destination is unknown, or is a broadcast or multicast address, it sends the frame out of all other ports.
Because switches rely on the source MAC to learn, a host that sends frames with a forged source MAC can confuse the table. This is the basis of MAC spoofing and table-flooding attacks, which is why many managed switches limit the number of MAC addresses per port.
VLAN tags
An 802.1Q VLAN tag adds 4 bytes between the source MAC and the real EtherType. The first 2 bytes are the tag protocol identifier 0x8100. The next 2 bytes carry a 3-bit priority, a 1-bit drop indicator and a 12-bit VLAN ID. After that comes the usual EtherType.
A tagged frame has an 18-byte header, so a full-size frame is 1522 bytes with the FCS. The payload limit is still 1500 bytes. If you capture on a trunk port and see 0x8100 where you expected 0x0800, this is the reason.
Common errors and gotchas
- Expecting the destination MAC to match the destination IP's owner. For off-subnet traffic it is the gateway's MAC.
- Looking for the FCS in a capture. The card removes it, and many captures also show frames before checksum offload has run, so the checksums can look wrong even on a healthy system.
- Counting the payload as 1500 bytes plus the header. The 1500-byte figure is the payload only. The whole frame is up to 1518 bytes without a VLAN tag.
- Reading a length where there is an EtherType. Any value of
0x0600or above is a protocol type. - Forgetting padding. A short packet is padded to 46 bytes. Use the IP total length, not the frame length, to find the end of the packet.
- Confusing hubs and switches. A hub repeats every frame to every port. A switch forwards by MAC address.
Seeing the frame in a capture
The -e option of tcpdump prints the link-layer header. Run this on a machine that is generating web traffic:
sudo tcpdump -i eth0 -e -nn -v -c 5 tcp port 80A line of output starts with the Ethernet fields, followed by the IP and TCP details. You should find the source MAC, the destination MAC, ethertype IPv4 (0x0800) and the frame length. If the destination MAC is not the server's, that is expected. Compare it with your default gateway's MAC, which you can list with ip neigh.
Exercise
A host at 192.168.1.10/24 sends a packet to 203.0.113.5. The default gateway is 192.168.1.1. Which MAC address goes in the destination field of the Ethernet header, and which IP address goes in the destination field of the IP header?
Show answer
The destination MAC is the gateway's (192.168.1.1's) MAC address, because 203.0.113.5 is not on the local subnet and the frame only has to reach the next hop. The destination IP stays 203.0.113.5 for the whole trip.
Exercise
A captured frame is 60 bytes long, but its IP header says the total length is 40 bytes. The frame has a 14-byte Ethernet header. What explains the difference, and what should a receiver do with the extra bytes?
Show answer
The 14-byte header plus the 40-byte packet is 54 bytes. The frame was padded with 6 bytes to reach the 60-byte minimum before the 4-byte FCS (64 bytes total). The receiver uses the IP total length to find the end of the packet and discards the padding.
Exercise
You capture on a trunk port and the EtherType field reads 0x8100 instead of 0x0800. What is in the frame, and where is the real protocol identified?
Show answer
The frame carries an 802.1Q VLAN tag. The 4-byte tag sits after the source MAC, and the real EtherType (for example 0x0800 for IPv4) follows the tag.