# qinq — Double-Tagged 802.1ad S-Tag & 802.1Q C-Tag Frames (`qinq`)

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

- **Full Protocol Name:** IEEE 802.1ad Provider Bridge Double VLAN Tagging (S-Tag + C-Tag)
- **Keyword:** `qinq`
- **OSI Layer & Transport:** Layer 2 · Outer 0x88a8 + Inner 0x8100
- **Host Endpoint Rule:** MAC-Only or IP
- **Standards:** IEEE 802.1ad, IEEE 802.1Q
- **Wireshark Display Filter:** `vlan.etype == 0x88a8 || vlan`
- **Wire Encapsulation:** `Ethernet II -> 802.1ad S-Tag (0x88a8) -> 802.1Q C-Tag (0x8100)`

## Overview

Because one VLAN tag was too mainstream for carriers: `qinq 500 innervlan 20` stacks an outer `0x88a8` S-Tag -> inner `0x8100` C-Tag on any packet line.

IEEE 802.1ad Provider Bridging (`qinq`) encapsulates customer VLAN traffic inside a carrier Service VLAN tag, emitting **two** consecutive 4-byte VLAN headers on the wire: an outer IEEE 802.1ad S-Tag (`TPID 0x88a8`) followed by an inner IEEE 802.1Q C-Tag (`TPID 0x8100`).

Like `vlan`, `qinq` works both as a standalone Layer 2 protocol on MAC-only provider switches and as an inline cross-layer modifier on any L3, L4, or L7 packet statement—allowing carrier engineers and security teams to test double-tagged BGP, PPPoE/DHCP subscriber flows, or VLAN-hopping IDS signatures in one line.

## Minimal Quickstart (`qinq_quickstart.frasm`)

```frameassembly
host pe_nni_a { mac 00:25:90:aa:00:01 }
host pe_nni_b { mac 00:25:90:bb:00:02 }

compile {
    pe_nni_a -> pe_nni_b qinq 500 innervlan 20
}
```

### Automatic Wire Calculations

- Always writes two complete 4-byte VLAN tags: outer TPID `0x88a8` (S-VLAN, default `100`) followed immediately by inner TPID `0x8100` (C-VLAN, default `innervlan 1`).
- Applies `pcp` (`0..7`) and `dei` (`0|1`) cleanly to the tag control headers and preserves the inner payload EtherType (`0x0800` IPv4, `0x86dd` IPv6, `0x0806` ARP).
- Validates both outer S-VLAN and `innervlan` C-VLAN bounds (`0..=4095`) during semantic validation.

## Practical Lab Example: Provider NNI Trunk Carrying Multiple Customer C-VLANs Inside S-VLAN 1200 (`qinq_metro_nni.frasm`)

Models a Metro Ethernet provider trunk (`S-VLAN 1200`) carrying two distinct customer tenants: Tenant A (`innervlan 100`) running BGP peering and Tenant B (`innervlan 200`) running ICMPv6.

```frameassembly
host ce_v4_a { ip 172.20.0.1 mac 00:1b:21:c1:00:01 }
host ce_v4_b { ip 172.20.0.2 mac 00:1b:21:c1:00:02 }
host ce_v6_a { ip 2001:db8:200::1 mac 00:1b:21:c2:00:01 }
host ce_v6_b { ip 2001:db8:200::2 mac 00:1b:21:c2:00:02 }

compile {
    // Tenant A: eBGP Keepalive inside S-VLAN 1200 / C-VLAN 100
    ce_v4_a -> ce_v4_b tcp syn srcport 51179 dstport 179 win 65535 mss 1452 qinq 1200 innervlan 100 pcp 6 wait 1ms
    ce_v4_a <- ce_v4_b tcp syn ack win 65535 mss 1452 qinq 1200 innervlan 100 pcp 6 wait 1ms
    ce_v4_a -> ce_v4_b tcp ack qinq 1200 innervlan 100 pcp 6 wait 1ms
    ce_v4_a -> ce_v4_b bgp keepalive qinq 1200 innervlan 100 pcp 6 wait 3ms
    // Tenant B: IPv6 Echo inside S-VLAN 1200 / C-VLAN 200
    ce_v6_a -> ce_v6_b icmp echorequest id 42 seq 1 qinq 1200 innervlan 200 pcp 3 wait 1ms
    ce_v6_a <- ce_v6_b icmp echoreply id 42 seq 1 qinq 1200 innervlan 200 pcp 3
}
```

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

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

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

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

## All `qinq` Keywords & Header Options

Packet line pattern: `src_host -> dst_host qinq [<outer_svlan 0..4095>] [innervlan <inner_cvlan 0..4095>] [pcp <0..7>] [dei <0|1>]`

| Keyword / Option | Accepted Value | Default | Effect on Packet |
| --- | --- | --- | --- |
| `qinq [<0..4095>]` | 12-bit outer S-VLAN ID (0..4095) | `100` | Sets the 12-bit Service VLAN ID inside the outer IEEE 802.1ad (`0x88a8`) header. |
| `innervlan <0..4095>` | 12-bit inner C-VLAN ID (0..4095) | `1` | Sets the 12-bit Customer VLAN ID inside the inner IEEE 802.1Q (`0x8100`) header. |
| `pcp <0..7>` | 3-bit unsigned integer (0..7) | `0` | Sets the Priority Code Point bits on the VLAN tag header. |
| `dei <0\|1>` | 1-bit flag (0 or 1) | `0` | Sets the Drop Eligible Indicator bit on the outer S-Tag. |

## Cross-Layer Options Valid with `qinq`

| 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. |
| `vlan <0..4095> [pcp <0..7>] [dei <0\|1>]` | VLAN ID 0..4095, PCP 0..7, DEI 0..1 | `Untagged` | Wraps the packet in an IEEE 802.1Q VLAN tag (`0x8100`) with optional 802.1p QoS priority. |
| `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

### Always Spell innervlan Without Underscores

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

Legacy underscored spellings such as `inner_vlan` or `c_vlan` are rejected by the v3.2.0 lexer with a self-healing diagnostic suggesting `innervlan`.

### Account for 8 Extra Header Bytes on Path MTU Tests

- **Avoid:** `Assuming 1500B Ethernet payload without baby-giant MTU`
- **Valid v3.2.2 Syntax:** `a -> b tcp syn mss 1452 qinq 500 innervlan 20`

QinQ adds 8 bytes (`0x88a8` + `0x8100`) to the Layer 2 header, increasing a standard 1514-byte Ethernet frame to 1522 bytes on carrier trunks.
