What vlan does in BPF bytecode
In a standard Ethernet frame (MAC, switch, ARP, and VLAN), the 12 bytes of MAC addresses are followed directly by EtherType (0x0800 for IPv4), and the IP header begins at byte 14. An IEEE 802.1Q tag inserts 4 bytes between them (0x8100 plus 2 bytes of priority and VLAN ID):
Standard Ethernet frame: [Dst MAC: 6][Src MAC: 6][0x0800: 2][IP header: from byte 14...]
Tagged 802.1Q frame: [Dst MAC: 6][Src MAC: 6][0x8100: 2][TCI/VLAN: 2][0x0800: 2][IP: from byte 18...]
The pcap-filter compiler reads expressions left to right. Once it hits vlan, it adds a +4 offset to every condition after it (which is why double-tagged QinQ filters read vlan 100 and vlan 200 and tcp port 80 — each vlan adds another 4 bytes).
tcpdump -d shows this directly in the generated bytecode:
# Wrong: checks byte [12] == 0x0800 first, where a tagged frame has 0x8100!
sudo tcpdump -i eth0 -d 'tcp port 80 and vlan'
# (000) ldh [12] # expects 0x0800 or 0x86dd with no offset
# ...
# (018) ldb [vlanp] # reaches the VLAN check too late
# (020) ldh [12]
# (021) jeq #0x8100 jt 24 jf 22
# Right: checks the VLAN tag first (vlanp or 0x8100), loads offset 4 into X, then reads [x + 12]
sudo tcpdump -i eth0 -d 'vlan and tcp port 80'
# (003) ldb [vlanp] # hardware-stripped tag in kernel metadata?
# (004) jeq #0x1 jt 12 jf 5
# (005) ld #0x4 # otherwise offset = 4 bytes
# ...
# (008) ldh [12]
# (009) jeq #0x8100 jt 12 jf 10
# (013) ldh [x + 12] # checks IPv4/IPv6 EtherType with the offset applied!
Linux quirk: hardware VLAN offloading
Lines (003)–(004) above contain ldb [vlanp]. Some Linux NICs have RX VLAN offloading (rx-vlan-offload) enabled: the card strips the 4-byte tag from inbound frames and hands it to the kernel separately in sk_buff metadata. Because of that, a broken 'tcp port 80 and vlan' filter can accidentally match inbound packets (their 0x0800 sits at offset 12), yet completely miss outbound tagged frames and all frames when reading a saved .pcap file.
Working patterns
# All HTTP inside any VLAN:
sudo tcpdump -nn -e -i eth0 'vlan and tcp port 80'
# Only VLAN ID 20 and a specific host:
sudo tcpdump -nn -e -i eth0 'vlan 20 and host 10.0.20.10'
# Both untagged and tagged traffic at once (untagged branch must come before vlan):
sudo tcpdump -nn -e -i eth0 '(tcp port 80) or (vlan and tcp port 80)'
# Double tag (QinQ): outer VLAN 100, inner VLAN 200:
sudo tcpdump -nn -e -i eth0 'vlan 100 and vlan 200 and tcp port 80'
Always add -e when debugging VLANs: without it, tcpdump omits vlan 20 from the printed line even when the filter matches.
If it doesn’t work
tcpdump -nn -e -i eth0 without a filter shows frames with vlan 20 and port 80, while 'tcp port 80' or 'tcp port 80 and vlan' prints nothing
- Cause
- In a tagged 802.1Q frame, byte offset
12holds EtherType0x8100, and the IP header starts 4 bytes later (18instead of14). Without a precedingvlan, the compiler looks for IPv4 (0x0800) directly at offset12. - Fix
- Put
vlanat the start of the expression:'vlan and tcp port 80'or'vlan 20 and tcp port 80'.
The same interface carries both untagged frames (native VLAN) and tagged frames, and 'vlan and tcp port 80' misses the untagged ones
- Cause
- The
vlanprimitive drops frames without an 802.1Q tag, and its+4offset shift applies to everything written after it in the same branch. - Fix
- Place the untagged branch first (before
vlan) and the tagged branch second:'(tcp port 80) or (vlan and tcp port 80)'.
How to check
Compare the bytecode of both forms on your interface — no live VLAN traffic needed:
sudo tcpdump -i eth0 -d 'vlan and tcp port 80' | head -n 16
sudo tcpdump -i eth0 -d 'tcp port 80 and vlan' | head -n 16
In the first output, the vlanp and 0x8100 checks come first and EtherType is read via [x + 12]; in the second, ldh [12] checks 0x86dd/0x800 right away with no offset.