# vlan — 802.1Q Trunk Tags (0x8100) Without NIC Stripping (`vlan`)

> **FrameAssembly v3.2.2** · Protocol 2 of 81 · Canonical URL: https://frameassembly-lang.dev/docs/protocols/vlan

- **Full Protocol Name:** IEEE 802.1Q Virtual LAN Tagging & 802.1p QoS
- **Keyword:** `vlan`
- **OSI Layer & Transport:** Layer 2 · TPID 0x8100 (VID 1)
- **Host Endpoint Rule:** MAC-Only or IP
- **Standards:** IEEE 802.1Q, IEEE 802.1p
- **Wireshark Display Filter:** `vlan`
- **Wire Encapsulation:** `Ethernet II -> 802.1Q Tag (0x8100)`

## Overview

Laptop OSes love silently stripping `0x8100` tags before Wireshark ever sees them — writing `vlan 120 pcp 5` bakes the exact 4-byte 802.1Q tag straight into your `.pcap` bytes.

IEEE 802.1Q (`vlan`) inserts a 4-byte tag header (`TPID 0x8100` + 3-bit Priority Code Point `pcp` + 1-bit Drop Eligible Indicator `dei` + 12-bit VLAN Identifier `vid`) between the Ethernet source MAC and the encapsulated EtherType.

In FrameAssembly v3.2.0, `vlan` serves a dual role: you can emit standalone Layer 2 802.1Q frames between MAC-only switch ports (`sw1 -> sw2 vlan 100 pcp 5 payload "keepalive"`), or append `vlan <0..4095> [pcp <0..7>] [dei <0|1>]` as a cross-layer modifier onto any of the other 80 protocols.

## Minimal Quickstart (`vlan_quickstart.frasm`)

```frameassembly
host access_sw { mac 00:1b:21:aa:00:01 }
host core_sw   { mac 00:1b:21:bb:00:02 }

compile {
    access_sw -> core_sw vlan 100
}
```

### Automatic Wire Calculations

- Inserts outer TPID `0x8100` and packs `pcp` (3 bits), `dei` (1 bit), and `vid` (12 bits) into the big-endian 16-bit Tag Control Information (TCI) word.
- Emits default inner EtherType `0x0800` when used standalone (overridable via `ethertype <0..65535>`) or preserves the exact inner protocol EtherType (`0x0806`, `0x0800`, `0x86dd`, `0x8847`) when used as a cross-layer modifier.
- Validates `vid` (`0..=4095`), `pcp` (`0..=7`), and `dei` (`0..=1`) at compile time before writing `.pcap` or `.pcapng` bytes.

## Practical Lab Example: Multi-VLAN Trunk Capture Mixing Standalone L2 Probes & Voice/Routing CoS (`vlan_trunk_qos.frasm`)

Demonstrates standalone MAC-only 802.1Q frames alongside VLAN-tagged ARP, VoIP RTP (`pcp 5`, `dscp 46`), and network-control OSPF (`pcp 6`) traffic on a single switch trunk.

```frameassembly
host sw_leaf  { mac 00:1c:73:10:00:01 }
host sw_spine { mac 00:1c:73:20:00:01 }
host phone_01 { ip 10.50.20.11 mac 00:1c:73:50:00:11 }
host pbx_core { ip 10.50.20.10 mac 00:1c:73:50:00:10 }

compile {
    // Standalone L2 VLAN probe on management VLAN 99
    sw_leaf -> sw_spine vlan 99 pcp 2 dei 0 ethertype 0x88b5 payload "trunk-health" wait 1ms
    // Voice VLAN 20: ARP + CoS 5 / EF RTP media packet
    phone_01 -> pbx_core arp request vlan 20 pcp 5 wait 1ms
    phone_01 <- pbx_core arp reply vlan 20 pcp 5 wait 2ms
    phone_01 -> pbx_core rtp pt 0 seq 1 timestamp 160 ssrc 0x10203040 vlan 20 pcp 5 dscp 46 payload "g711-ulaw-sample" wait 2ms
    // Routing VLAN 30: Priority 6 OSPF Hello
    phone_01 -> pbx_core ospf hello routerid 10.50.20.11 areaid 0 vlan 30 pcp 6
}
```

```bash
# 1. Eyeball relative timestamps, ports & wire lengths in your terminal
frameassembly show vlan_trunk_qos.frasm

# 2. Compile .frasm -> classic .pcap (tcpdump/tcpreplay/Suricata) or nanosecond .pcapng
frameassembly vlan_trunk_qos.frasm vlan_trunk_qos.pcap
frameassembly vlan_trunk_qos.frasm vlan_trunk_qos.pcapng

# 3. Round-trip .pcap <-> .frasm whenever you want to scrub or tweak an existing capture
frameassembly reverse vlan_trunk_qos.pcap vlan_trunk_qos_edited.frasm

# 4. Verify dissected protocol fields in tshark
tshark -r vlan_trunk_qos.pcap
```

## All `vlan` Keywords & Header Options

Packet line pattern: `src_host -> dst_host vlan [<vid 0..4095>] [pcp <0..7>] [dei <0|1>]`

| Keyword / Option | Accepted Value | Default | Effect on Packet |
| --- | --- | --- | --- |
| `vlan [<0..4095>]` | 12-bit unsigned integer (0..4095) | `1` | Sets the 12-bit 802.1Q VLAN Identifier (`0` = 802.1p priority-tagged frame, `1..4094` = standard VLANs, `4095` = reserved). |
| `pcp <0..7>` | 3-bit unsigned integer (0..7) | `0` | Sets the IEEE 802.1p Priority Code Point (`0` Best Effort, `5` Voice, `6` Internetwork Control, `7` Network Control). |
| `dei <0\|1>` | 1-bit flag (0 or 1) | `0` | Sets the Drop Eligible Indicator bit (formerly CFI) marking frames eligible for discard under congestion. |
| `ethertype <0..65535>` | Whole number (0..65535) (decimal or 0x hex) | `0x0800 (standalone) or inner protocol EtherType` | Overrides the 2-byte encapsulated EtherType immediately following the 802.1Q tag. |

## Cross-Layer Options Valid with `vlan`

| Option | Range / Format | Default | Effect |
| --- | --- | --- | --- |
| `wait <number><ns\|us\|ms\|s\|m>` | Time gap (ns, us, ms, s, m) | `10ms` | Advances the packet timestamp clock after sending this packet and before the next packet (`wait 0ns` sends back-to-back). |
| `payload "<text>" \| hexpayload "<hex>"` | Quoted text or hex bytes (choose one) | `Omitted` | Appends or overrides raw payload bytes (`"deadbeef"` or `"de:ad:be:ef"`) and updates packet lengths and checksums. |
| `srcmac <mac> \| dstmac <mac> \| ethertype <0..65535>` | Colon/hyphen/Cisco MAC · EtherType u16 (e.g. 0x0800) | `Host MAC / RFC multicast MAC / auto EtherType` | Overrides the 14-byte Ethernet II source MAC, destination MAC, or 16-bit EtherType for this single packet. |
| `qinq <0..4095> [innervlan <0..4095>]` | Outer S-VLAN 0..4095, Inner C-VLAN 0..4095 | `Untagged (innervlan 1 when qinq is set)` | Double-tags the packet with outer IEEE 802.1ad S-Tag (`0x88a8`) plus inner IEEE 802.1Q C-Tag (`0x8100`). |
| `mpls <0..1048575> [label <0..1048575>] [exp\|tc <0..7>] [bos <0\|1>] [mplsttl <0..255>]` | Label 0..1048575, exp/tc 0..7, bos 0..1, ttl 0..255 | `Unlabeled (at most 1 mpls tag per packet)` | Inserts a 32-bit MPLS label header (`0x8847`) ahead of the Layer 3 header. |

## Common Mistakes & Quick Fixes

### Use qinq for Double VLAN Stacking Instead of Repeating vlan

- **Avoid:** `a -> b icmp vlan 500 vlan 20`
- **Valid v3.2.2 Syntax:** `a -> b icmp qinq 500 innervlan 20`

Repeating `vlan` overwrites the single 802.1Q tag field. To emit IEEE 802.1ad S-Tag (`0x88a8`) + C-Tag (`0x8100`) double-tagging, use `qinq <svid> innervlan <cvid>`.

### 12-Bit VID Range Enforcement

- **Avoid:** `a -> b vlan 5000`
- **Valid v3.2.2 Syntax:** `a -> b vlan 4094 pcp 5`

Semantic validation enforces `0..=4095` on `vlan` VIDs and `0..=7` on `pcp`, rejecting out-of-range tags at compile time.
