Ethernet Frames
Ethernet (IEEE 802.3) is the dominant wired LAN technology. Data travels in frames: a short header, the payload (usually an IP packet), and a 4-byte checksum. In this lesson you build a real Ethernet II frame byte by byte and parse it back.
Frame layout
+----------+-----+-------------+-------------+-----------+-------------------+-----+
| Preamble | SFD | Destination | Source | EtherType | Payload | FCS |
| 7 bytes | 1 | MAC 6 bytes | MAC 6 bytes | 2 bytes | 46 - 1500 bytes | 4 |
+----------+-----+-------------+-------------+-----------+-------------------+-----+
(added by hardware) <------ the "frame" that software sees ------>| Field | Purpose |
|---|---|
| Preamble + SFD | 10101010…10101011: lets the receiver lock onto the signal; stripped by the NIC |
| Destination MAC | who should accept the frame (it comes first so switches can start forwarding early) |
| Source MAC | who sent it |
| EtherType | what the payload is: 0x0800 IPv4, 0x86dd IPv6, 0x0806 ARP, 0x8100 VLAN tag |
| Payload | at least 46 bytes (padded with zeros if shorter), at most 1500 (the MTU) |
| FCS | Frame Check Sequence: a CRC-32 over the header and payload |
The minimum frame (header + payload + FCS) is therefore 14 + 46 + 4 = 64 bytes, and the maximum is 14 + 1500 + 4 = 1518 bytes.
Building a frame
import struct
import zlib
ETHERTYPE_IPV4 = 0x0800
def mac_bytes(text):
return bytes.fromhex(text.replace(":", ""))
def build_frame(dst, src, ethertype, payload):
if len(payload) < 46:
payload = payload + bytes(46 - len(payload)) # zero padding
header = struct.pack("!6s6sH", mac_bytes(dst), mac_bytes(src), ethertype)
body = header + payload
fcs = struct.pack("<I", zlib.crc32(body)) # CRC-32, sent low byte first
return body + fcs
frame = build_frame("00:1a:2b:3c:4d:5e", "3c:22:fb:91:0a:7e", ETHERTYPE_IPV4, b"hello, LAN")
print(len(frame), "bytes")
for offset in range(0, len(frame), 16):
chunk = frame[offset:offset + 16]
print(f"{offset:04x} {chunk.hex(' ')}")64 bytes
0000 00 1a 2b 3c 4d 5e 3c 22 fb 91 0a 7e 08 00 68 65
0010 6c 6c 6f 2c 20 4c 41 4e 00 00 00 00 00 00 00 00
0020 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0030 00 00 00 00 00 00 00 00 00 00 00 00 24 de 6f 0a
The dump format (offset, then hex bytes) is the same one Wireshark and tcpdump -X use. Our 10-byte message was padded to 46 bytes, so the frame is the 64-byte minimum.
Note:
zlib.crc32implements exactly the CRC-32 polynomial (0x04C11DB7) that Ethernet uses. Real NICs compute and check the FCS in hardware, so packet captures usually don't even show it.
Parsing a frame
Parsing reverses the process: unpack the fixed 14-byte header, check the FCS, then interpret the payload according to the EtherType.
import struct
import zlib
ETHERTYPES = {0x0800: "IPv4", 0x86DD: "IPv6", 0x0806: "ARP", 0x8100: "802.1Q VLAN"}
frame = bytes.fromhex(
"001a2b3c4d5e3c22fb910a7e0800"
"68656c6c6f2c204c414e" + "00" * 36 +
"24de6f0a"
)
def parse_frame(frame):
dst, src, ethertype = struct.unpack("!6s6sH", frame[:14])
payload, fcs = frame[14:-4], frame[-4:]
expected = struct.pack("<I", zlib.crc32(frame[:-4]))
return {
"dst": dst.hex(":"),
"src": src.hex(":"),
"type": f"0x{ethertype:04x} ({ETHERTYPES.get(ethertype, 'unknown')})",
"payload": payload.rstrip(b"\x00"),
"fcs ok": fcs == expected,
}
for key, value in parse_frame(frame).items():
print(f"{key:<8} {value}")dst 00:1a:2b:3c:4d:5e
src 3c:22:fb:91:0a:7e
type 0x0800 (IPv4)
payload b'hello, LAN'
fcs ok True
Detecting corruption
What happens when a bit flips on the wire, say from electrical noise? The receiver recomputes the CRC and it no longer matches, so the frame is silently dropped. Ethernet does not ask for a resend: higher layers (TCP) notice the gap and retransmit.
import zlib
def fcs_ok(frame):
# A neat CRC property: the CRC of (data + its own little-endian CRC) is a fixed constant.
return zlib.crc32(frame) == 0x2144DF1C
good = bytes.fromhex("001a2b3c4d5e3c22fb910a7e0800") + b"hello, LAN" + bytes(36)
good += zlib.crc32(good).to_bytes(4, "little")
print("original frame ok:", fcs_ok(good))
for bit in [0, 100, 300, 511]:
damaged = bytearray(good)
damaged[bit // 8] ^= 1 << (bit % 8) # flip one bit
print(f"bit {bit:>3} flipped -> ok: {fcs_ok(bytes(damaged))}")
two = bytearray(good)
two[20] ^= 0x01
two[40] ^= 0x80
print("two bits flipped -> ok:", fcs_ok(bytes(two)))original frame ok: True
bit 0 flipped -> ok: False
bit 100 flipped -> ok: False
bit 300 flipped -> ok: False
bit 511 flipped -> ok: False
two bits flipped -> ok: False
CRC-32 detects every single-bit and double-bit error, every burst of up to 32 bits, and all but about one in four billion other error patterns. It is a check against accidental damage, not an attack: anyone can recompute it, so it gives no security.
Payload size and jumbo frames
def frame_sizes(payload_len):
padded = max(payload_len, 46)
frame = 14 + padded + 4
wire = 8 + frame + 12 # preamble/SFD + inter-frame gap
return padded - payload_len, frame, wire
for n in [0, 10, 46, 1000, 1500]:
pad, frame, wire = frame_sizes(n)
print(f"payload {n:>4}: padding {pad:>2}, frame {frame:>4}, wire {wire:>4} bytes")
# Frames per second at 1 Gbit/s for the largest and smallest frames
for n in [46, 1500]:
print(f"{n:>4}-byte payloads: {1e9 / 8 / frame_sizes(n)[2]:,.0f} frames/s at 1 Gbit/s")payload 0: padding 46, frame 64, wire 84 bytes
payload 10: padding 36, frame 64, wire 84 bytes
payload 46: padding 0, frame 64, wire 84 bytes
payload 1000: padding 0, frame 1018, wire 1038 bytes
payload 1500: padding 0, frame 1518, wire 1538 bytes
46-byte payloads: 1,488,095 frames/s at 1 Gbit/s
1500-byte payloads: 81,274 frames/s at 1 Gbit/s
A switch port at 1 Gbit/s must handle almost 1.5 million minimum-size frames per second. Some data centres enable jumbo frames (MTU 9000) to cut the per-frame overhead, but every device on the path must support them.
Test yourself
Which EtherType value means the payload is an IPv4 packet?
- 0x0806
- 0x0800
- 0x86dd
- 0x8100
What happens to an Ethernet frame whose FCS does not match?
- The switch fixes the error
- It is dropped
- The receiver asks the sender to resend it
- It is delivered with a warning
A host sends a 20-byte payload in an Ethernet frame. How many padding bytes are added?
- 0
- 26
- 44
- 46