Sub topic 6
Inside the 802.1Q tag
This chapter assumes you know Ethernet frame layout, how a switch learns MAC addresses, and the basics of VLAN access and trunk ports. It covers what the tag contains, where it sits in the frame, and how to read it in code.
Where the tag sits
A trunk link has to tell the receiving switch which VLAN each frame belongs to. 802.1Q does this by inserting a 4-byte tag into the Ethernet header. The tag goes between the source MAC address and the original EtherType field.
An untagged frame has this order: destination MAC (6 bytes), source MAC (6 bytes), EtherType (2 bytes), payload. A tagged frame adds 4 bytes after the source MAC: destination MAC, source MAC, tag (4 bytes), EtherType, payload. Because the frame is now longer, the switch recalculates the frame check sequence (FCS) at the end.
The two tag fields
The 4-byte tag has two parts.
- TPID (Tag Protocol Identifier), 16 bits. It is always 0x8100 for an 802.1Q tag. It occupies the position where a receiver would normally expect the EtherType, so the value 0x8100 is how the receiver recognizes a tagged frame.
- TCI (Tag Control Information), 16 bits. It holds three subfields.
| Subfield | Size | Purpose |
|---|---|---|
| PCP (Priority Code Point) | 3 bits | Class of service, values 0 to 7, used for QoS |
| DEI (Drop Eligible Indicator) | 1 bit | Marks frames that may be dropped first during congestion |
| VID (VLAN ID) | 12 bits | The VLAN the frame belongs to |
A 12-bit VID gives 4096 values, 0 to 4095. VID 0 means the frame carries priority only and no VLAN. VID 4095 is reserved. VID 1 is the default VLAN on most switches. Usable VLANs are therefore 1 to 4094.
The DEI bit was called CFI (Canonical Format Indicator) in older versions of the standard. You will still see that name in some tools.
Reading a tag in code
This function reads bytes 12 to 15 of a frame. Those bytes come right after the two MAC addresses. It returns the tag fields, or None if the TPID is not 0x8100.
import struct
def parse_dot1q(frame: bytes):
"""Return the 802.1Q fields of an Ethernet frame, or None if it is untagged."""
tpid, tci = struct.unpack("!HH", frame[12:16])
if tpid != 0x8100:
return None
return {
"pcp": tci >> 13,
"dei": (tci >> 12) & 0x1,
"vid": tci & 0x0FFF,
}To test it, build a frame with Scapy. This is for a lab network.
from scapy.all import Ether, Dot1Q, IP, ICMP
frame = (
Ether(dst="ff:ff:ff:ff:ff:ff")
/ Dot1Q(vlan=10, prio=0)
/ IP(dst="192.168.10.255")
/ ICMP()
)
frame.show()Passing bytes(frame) to parse_dot1q should return a dictionary with vid equal to 10.
Common mistakes
- Reading the wrong offset. The EtherType of a tagged frame is at bytes 16 to 17, not 12 to 13. A parser written for untagged frames sees 0x8100 as the protocol and fails to decode the payload.
- Masking errors. The VID is the low 12 bits. Without
& 0x0FFF, the priority bits corrupt the VLAN number. - Capturing on the wrong interface. Many network cards strip the tag before the capture tool sees the frame. If your capture shows no tags, check the NIC offload settings before assuming the switch is not tagging.
Exercise
A captured frame has bytes 12 to 15 equal to 81 00 A0 14. What are the PCP, DEI and VLAN ID values?
Show answer
The TPID is 0x8100, so the frame is tagged. The TCI is 0xA014, which is 1010 0000 0001 0100 in binary. The top 3 bits are 101, so PCP is 5. The next bit is 0, so DEI is 0. The low 12 bits are 0x014, so the VLAN ID is 20.