# stp — IEEE 802.1D STP & 802.1w RSTP BPDU Captures (`stp`)

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

- **Full Protocol Name:** IEEE 802.1D Spanning Tree & IEEE 802.1w Rapid Spanning Tree
- **Keyword:** `stp`
- **OSI Layer & Transport:** Layer 2 · 802.3 LLC 0x4242 -> 01:80:c2:00:00:00
- **Host Endpoint Rule:** MAC-Only or IP
- **Standards:** IEEE 802.1D, IEEE 802.1w
- **Wireshark Display Filter:** `stp`
- **Wire Encapsulation:** `Ethernet II -> STP / RSTP (802.3 LLC 0x4242 -> 01:80:c2:00:00:00)`

## Overview

Electing your laptop Root Bridge on a live corporate VLAN is a career-limiting move — crafting `stp` and `stp rstp` BPDUs into a `.pcap` lets you audit BPDU Guard without the walk of shame.

Spanning Tree Protocol (`stp`) prevents Layer 2 bridging loops by exchanging Bridge Protocol Data Units (BPDUs) encapsulated inside **IEEE 802.3 length-framed Ethernet + LLC (`DSAP=0x42, SSAP=0x42, Control=0x03`)** addressed to the Bridge Group multicast MAC `01:80:c2:00:00:00`.

FrameAssembly emits classic IEEE 802.1D Configuration BPDUs by default (`version 0x00`, 35-byte BPDU) and switches seamlessly to IEEE 802.1w Rapid Spanning Tree (`rstp`, `version 0x02`, `type 0x02`, including the mandatory Version 1 Length `0x00` byte) when the `rstp` flag is added.

## Minimal Quickstart (`stp_quickstart.frasm`)

```frameassembly
host root_sw { mac 00:1c:73:00:00:01 }
host edge_sw { mac 00:1c:73:00:00:02 }

compile {
    root_sw -> edge_sw stp rstp
}
```

### Automatic Wire Calculations

- Addresses Ethernet destination to IEEE Bridge Group multicast `01:80:c2:00:00:00` and writes the exact IEEE 802.3 Payload Length field instead of an Ethernet II EtherType.
- Prepends the 3-byte LLC header (`0x42 0x42 0x03`) and Protocol Identifier `0x0000` ahead of the Configuration or RSTP BPDU.
- Converts `maxage` (`20`), `hellotime` (`2`), and `forwarddelay` (`15`) seconds into IEEE 802.1D `1/256`-second timer units (`0x1400`, `0x0200`, `0x0f00`) automatically.

## Practical Lab Example: Normal Root RSTP Heartbeat Followed by Rogue Superior BPDU Attack (`stp_rogue_root_audit.frasm`)

Captures a legitimate distribution switch sending RSTP BPDUs (`priority 32768`), followed by a rogue access device advertising priority `4096` (`cost 0`) to trigger Root Guard / BPDU Guard.

```frameassembly
host dist_root  { mac 00:1c:73:01:00:01 }
host access_sw  { mac 00:1c:73:02:00:10 }
host rogue_port { mac 00:50:56:99:99:99 }

compile {
    // Legitimate RSTP Proposal/Learning-Forwarding BPDU from distribution root
    dist_root -> access_sw stp rstp rootid "32768:00:1c:73:01:00:01" bridgeid "32768:00:1c:73:01:00:01" portid 0x8001 cost 0 flags 0x3c wait 2s
    // Downstream access switch relaying root path cost 20000
    access_sw -> rogue_port stp rstp rootid "32768:00:1c:73:01:00:01" bridgeid "32768:00:1c:73:02:00:10" portid 0x8018 cost 20000 flags 0x3c wait 500ms
    // Rogue device injecting a superior Root ID (priority 4096, cost 0)
    rogue_port -> access_sw stp rstp rootid "4096:00:50:56:99:99:99" bridgeid "4096:00:50:56:99:99:99" portid 0x8001 cost 0 flags 0x0e
}
```

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

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

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

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

## All `stp` Keywords & Header Options

Packet line pattern: `src_host -> dst_host stp [rstp] [rootid "<text>"] [bridgeid "<text>"] [portid <0..65535>] [cost <number>] [maxage <0..65535>] [hellotime <0..65535>] [forwarddelay <0..65535>] [flags <0..255>]`

| Keyword / Option | Accepted Value | Default | Effect on Packet |
| --- | --- | --- | --- |
| `rstp` | Bare boolean flag | `Omitted (classic 802.1D v0)` | Emits IEEE 802.1w Rapid STP BPDU (`Protocol Version 2`, `BPDU Type 0x02`, plus trailing 1-byte Version 1 Length `0x00`). |
| `rootid "<text>"` | Bridge ID string ("<prio>:<mac>" or "<mac>") | `"32768:<src_mac>"` | Encodes the 8-byte Root Identifier (2-byte priority + 6-byte MAC). |
| `bridgeid "<text>"` | Bridge ID string ("<prio>:<mac>" or "<mac>") | `"32768:<src_mac>"` | Encodes the 8-byte Bridge Identifier of the transmitting switch. |
| `portid <0..65535>` | Whole number (0..65535) | `0x8001` | Sets the 2-byte Port Identifier (4-bit priority + 12-bit port number). |
| `cost <number>` | Whole number | `0` | Sets the 4-byte Root Path Cost (`20000` = 1GbE, `2000` = 10GbE). |
| `maxage <0..65535> \| hellotime <0..65535> \| forwarddelay <0..65535>` | 16-bit unsigned integers (seconds) | `maxage 20, hellotime 2, forwarddelay 15` | Encodes BPDU timers scaled to IEEE 802.1D `1/256`s increments. |
| `flags <0..255>` | 8-bit bitmask | `0 (or RSTP role bits)` | Sets Topology Change (`0x01`), Proposal (`0x02`), Port Role (`bits 2..3`), Learning (`0x10`), Forwarding (`0x20`), Agreement (`0x40`), and TCA (`0x80`). |

## Cross-Layer Options Valid with `stp`

| 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. |
| `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

### portid on stp Takes a Numeric u16, Not an Interface Name

- **Avoid:** `sw1 -> sw2 stp portid "Gi1/0/1"`
- **Valid v3.2.2 Syntax:** `sw1 -> sw2 stp rstp portid 0x8001`

Unlike `lldp portid "Gi1/0/1"`, IEEE 802.1D/1w STP encodes a 16-bit numeric Port Identifier (`0x8001` = default priority `128`, port `1`).

### Quote Priority:MAC Strings in rootid and bridgeid

- **Avoid:** `sw1 -> sw2 stp rootid 32768:00:1c:73:01:00:01`
- **Valid v3.2.2 Syntax:** `sw1 -> sw2 stp rootid "32768:00:1c:73:01:00:01"`

Composite `<priority>:<mac>` values have 7 colon-separated groups (rather than a 6-octet MAC literal) and should be enclosed in double quotes.
