Sub topic 8
Link layer: the Ethernet frame
This chapter assumes you know what an IP address is, can read hexadecimal bytes, and have seen the TCP and IP headers from the earlier chapters. Here the packet gets its last wrapper before it goes onto the wire.
What the link layer does
The link layer moves data between two devices on the same local network segment, such as a laptop and its Wi-Fi router, or a server and the switch it is plugged into. It does not care about the final destination. It only delivers the frame to the next device on the same link.
A frame is the link layer's PDU (protocol data unit). It is an IP packet with a link-layer header added in front and, usually, a trailer added at the end. On wired networks the most common format is Ethernet II.
The Ethernet II header
The header is 14 bytes long and has three fields.
| Field | Size | Purpose |
|---|---|---|
| Destination MAC | 6 bytes | Hardware address of the next device on this link |
| Source MAC | 6 bytes | Hardware address of the sender on this link |
| EtherType | 2 bytes | Says which protocol is in the payload |
A MAC address is a 48-bit hardware address, written as six hex bytes such as aa:bb:cc:dd:ee:ff. The first three bytes identify the manufacturer. The address ff:ff:ff:ff:ff:ff is the broadcast address, which every device on the link accepts.
The EtherType tells the receiver what to do with the payload. Common values are 0x0800 for IPv4, 0x86DD for IPv6 and 0x0806 for ARP.
After the payload comes the frame check sequence (FCS), a 4-byte CRC (cyclic redundancy check). The receiving network card recomputes it. If it does not match, the card drops the frame. Software usually never sees the FCS, because the card adds and removes it.
Size limits
The payload of a standard Ethernet frame is at most 1500 bytes. This limit is the MTU (maximum transmission unit), and a later chapter covers it in detail. The payload is also at least 46 bytes. Shorter payloads are padded with zeros so that the frame reaches the 64-byte minimum, counting the header and FCS.
Building a frame in code
The example below wraps an IP packet in an Ethernet header, then unpacks it. Only the first part is shown here. The ip value stands in for the packet built in the earlier chapters.
import struct
ip = bytes(20) + b"payload" # placeholder for a real IPv4 packet
dst_mac = bytes.fromhex("aabbccddeeff")
src_mac = bytes.fromhex("112233445566")
# "!6s6sH" = network byte order, two 6-byte fields, one 16-bit integer
frame = struct.pack("!6s6sH", dst_mac, src_mac, 0x0800) + ip
# Decapsulate: read the first 14 bytes, keep the rest as the packet
dst, src, ethertype = struct.unpack("!6s6sH", frame[:14])
packet = frame[14:]
print(dst.hex(":"), src.hex(":"), hex(ethertype))
print(len(frame), "bytes in the frame,", len(packet), "in the payload")The output is:
aa:bb:cc:dd:ee:ff 11:22:33:44:55:66 0x800
27 bytes in the frame, 27 bytes in the payloadWait: the frame is 14 bytes longer than the packet, so the second line shows 41 bytes in the frame, 27 bytes in the payload. The packet is 20 + 7 = 27 bytes and the frame is 27 + 14 = 41 bytes. This code leaves out padding and the FCS, which the network card handles.
Common mistakes
- Assuming the MAC addresses in a frame are the addresses of the original sender and final receiver. They are only the two ends of the current link. A router replaces them at every hop, while the IP addresses stay the same.
- Forgetting byte order. Ethernet fields are big-endian, so use
!instructformats. - Expecting to see the FCS in a capture. Most capture tools do not show it, because the card strips it first.
- Counting the 14-byte header toward the 1500-byte MTU. The MTU applies to the payload only, so a full frame is 1514 bytes without the FCS.
Exercise
A host sends an IPv4 packet that is 1500 bytes long over Ethernet II. How many bytes does the frame have before the FCS is added, and which EtherType value does the header carry?
Show answer
The frame is 1514 bytes: 14 bytes of header plus the 1500-byte packet. The EtherType is 0x0800, which means IPv4.
Exercise
A packet crosses three routers on its way to a server. Which addresses change at each router, and which do not?
Show answer
The source and destination MAC addresses change, because each router builds a new frame for the next link. The source and destination IP addresses stay the same from end to end.