TExcelTAD

VLANs, 802.1Q Tags and Trunks

Network diagrams show "the server VLAN", "the guest VLAN", "the management VLAN" as if they were separate cables. Inside the switch they are a 12-bit number. A VLAN (virtual LAN) splits one physical switch into several broadcast domains; between switches, a trunk carries frames of many VLANs over one link, each marked with an IEEE 802.1Q tag. Security people care because VLANs are the cheapest form of segmentation: ARP spoofing, MAC flooding and rogue DHCP stay inside their own VLAN. That only holds if the tags themselves can't be forged, which is the subject of the next lesson. The basics are in VLANs.

The 4-byte tag

An 802.1Q tag is inserted between the source MAC and the EtherType:

Bytes Field Meaning
2 TPID 0x8100 "a VLAN tag follows" (sits where the EtherType would be)
2 TCI: PCP (3 bits), DEI (1 bit), VID (12 bits) priority, drop-eligible, VLAN ID 0–4095 (0 and 4095 reserved, so 1–4094 usable)
2 EtherType the real payload type (0x0800, 0x0806, …)
  • An access port belongs to one VLAN and sends and receives untagged frames; the switch adds the tag internally.
  • A trunk port carries many VLANs, tagged. One VLAN per trunk, the native VLAN, travels untagged (VLAN 1 by default on many switches).
  • 802.1ad (Q-in-Q) stacks a second tag with TPID 0x88a8 for service providers.

Building and reading a tag in Python

python · try it yourself
import struct

def tag(vid, pcp=0, dei=0):
    tci = (pcp << 13) | (dei << 12) | vid
    return struct.pack("!HH", 0x8100, tci)

def parse(frame):
    dst, src = frame[:6], frame[6:12]
    off, vids = 12, []
    while True:
        (etype,) = struct.unpack("!H", frame[off:off + 2])
        if etype in (0x8100, 0x88A8):
            (tci,) = struct.unpack("!H", frame[off + 2:off + 4])
            vids.append((tci & 0x0FFF, tci >> 13))
            off += 4
        else:
            return vids, etype

dst = bytes.fromhex("0200000a0a0a"); src = bytes.fromhex("0200000a0a05")
voice = dst + src + tag(20, pcp=5) + struct.pack("!H", 0x0800)
print("tag bytes for VLAN 20, priority 5:", tag(20, pcp=5).hex(" "))
for name, f in [("untagged", dst + src + struct.pack("!H", 0x0800)),
                ("voice", voice),
                ("double", dst + src + tag(1) + tag(20) + struct.pack("!H", 0x0800))]:
    vids, et = parse(f)
    print(f"{name:<9} tags (vid, pcp)={vids}  ethertype=0x{et:04x}")
tag bytes for VLAN 20, priority 5: 81 00 a0 14
untagged  tags (vid, pcp)=[]  ethertype=0x0800
voice     tags (vid, pcp)=[(20, 5)]  ethertype=0x0800
double    tags (vid, pcp)=[(1, 0), (20, 0)]  ethertype=0x0800

0x8100 then 0xa014: PCP 5 is 101 in the top three bits (0xa…), VID 20 is 0x014. The third frame carries two tags. Nothing in Ethernet forbids that, and the next lesson shows why it matters.

Tags in a capture

Most captures you get from a SPAN port or a host have no tags (the switch removes them). Captures from a trunk or a network tap keep them. The session writes a small capture with Scapy (fixed timestamps, lab addresses), then reads it with tcpdump; -e shows the tags and the BPF primitive vlan 20 selects one VLAN.

bash · try it yourself
python3 - <<'PY'
from scapy.all import Ether, Dot1Q, IP, UDP, Raw, wrpcap
t0 = 1768471200.0                       # 2026-01-15 10:00:00 UTC, the lab capture clock
pkts = [
    Ether(src="02:00:00:0a:0a:05", dst="02:00:00:0a:0a:0a")/Dot1Q(vlan=10)/IP(src="10.10.0.5", dst="10.10.0.10")/UDP(sport=40000, dport=514)/Raw(b"<14>web1: lab test"),
    Ether(src="02:00:00:0a:0a:21", dst="02:00:00:0a:0a:22")/Dot1Q(vlan=20, prio=5)/IP(src="10.10.20.21", dst="10.10.20.22")/UDP(sport=16384, dport=16386)/Raw(b"voice"),
    Ether(src="02:00:00:0a:0a:06", dst="02:00:00:0a:0a:0a")/Dot1Q(vlan=10)/IP(src="10.10.0.6", dst="10.10.0.10")/UDP(sport=40001, dport=514)/Raw(b"<14>web2: lab test"),
]
for i, p in enumerate(pkts):
    p.time = t0 + i * 0.5
wrpcap("trunk.pcap", pkts)
PY
tcpdump -nn -e -r trunk.pcap 2>/dev/null
echo "--- only VLAN 20:"
tcpdump -nn -r trunk.pcap 'vlan 20' 2>/dev/null
10:00:00.000000 02:00:00:0a:0a:05 > 02:00:00:0a:0a:0a, ethertype 802.1Q (0x8100), length 64: vlan 10, p 0, ethertype IPv4 (0x0800), 10.10.0.5.40000 > 10.10.0.10.514: SYSLOG user.info, length: 18
10:00:00.500000 02:00:00:0a:0a:21 > 02:00:00:0a:0a:22, ethertype 802.1Q (0x8100), length 51: vlan 20, p 5, ethertype IPv4 (0x0800), 10.10.20.21.16384 > 10.10.20.22.16386: UDP, length 5
10:00:01.000000 02:00:00:0a:0a:06 > 02:00:00:0a:0a:0a, ethertype 802.1Q (0x8100), length 64: vlan 10, p 0, ethertype IPv4 (0x0800), 10.10.0.6.40001 > 10.10.0.10.514: SYSLOG user.info, length: 18
--- only VLAN 20:
10:00:00.500000 IP 10.10.20.21.16384 > 10.10.20.22.16386: UDP, length 5

Security properties of VLANs

  • A VLAN is a broadcast domain, not a firewall. Traffic between VLANs goes through a router or layer-3 switch; if that device routes everything, the VLANs separate broadcasts and nothing else. Segmentation needs ACLs or a firewall between VLANs (see Network Security).
  • Tags are trusted only from trunks. A switch must ignore or drop tags arriving on access ports; trunks must be explicit, never negotiated with end devices.
  • Management VLAN. Switch and server management interfaces belong in a VLAN that user ports can't reach.
  • Wi-Fi maps SSIDs to VLANs the same way; a guest SSID on the corporate VLAN is a common finding.

How problems are detected

  • Tagged frames arriving on an access port, or frames with two tags anywhere at the edge (BPF vlan and vlan matches double-tagged frames).
  • Trunk negotiation frames (DTP, VTP) from user ports.
  • Hosts with IP addresses from the wrong subnet for their VLAN in DHCP or ARP logs.
  • Audits of switch configurations: every port's mode, access VLAN, trunk allowed list and native VLAN.

How problems are fixed

Set user ports to access mode explicitly, disable trunk negotiation, restrict each trunk's allowed VLANs, use an unused VLAN as the native VLAN (or tag the native VLAN), and filter between VLANs at layer 3. The next lesson explains each of these against a specific attack.

Pitfalls

  • Expecting to see tags in a capture taken on a host: the NIC or switch has usually removed them.
  • Putting servers and users in different VLANs but routing between them without filtering, then calling it segmentation.
  • Leaving everything in VLAN 1, which is also the default native and management VLAN on many switches.

Practice

1. Decode the TCI 0x600a.

0x600a = 0110 0000 0000 1010: PCP = 011 = 3, DEI = 0, VID = 0x00a = 10.

2. How many bytes longer is a tagged frame, and why can that break things?

Four bytes. A full 1500-byte payload makes a 1522-byte frame (with FCS), which older devices treat as oversized ("baby giant"). Switches support it; it matters for misconfigured MTUs and for double tags (8 bytes).

3. Write a BPF filter that matches UDP port 514 (syslog) only inside VLAN 10.

vlan 10 and udp port 514. The vlan keyword shifts the offsets of the following primitives past the tag, so the order matters: udp port 514 and vlan 10 would look for the UDP header at the untagged offset.

Test yourself

Which field of an 802.1Q tag holds the VLAN number?

  1. TPID
  2. PCP
  3. VID (12 bits)
  4. DEI

How does the native VLAN travel on an 802.1Q trunk?

  1. Double-tagged
  2. Untagged
  3. Encrypted
  4. With TPID 0x88a8

Two VLANs are routed by a layer-3 switch with no ACLs. What do they isolate?

  1. Everything
  2. Broadcast traffic such as ARP and DHCP, but not routed traffic
  3. Only TCP
  4. Nothing at all