# bmp — TCP 1790 Real-Time BGP Adj-RIB-In Collector Streams (`bmp`)

> **FrameAssembly v3.3.0** · Protocol 45 of 81 · Canonical URL: https://frameassembly-lang.dev/docs/protocols/bmp

- **Full Protocol Name:** BGP Monitoring Protocol v3
- **Keyword:** `bmp`
- **OSI Layer & Transport:** Layer 3 · TCP 1790
- **Host Endpoint Rule:** Dual-Stack IPv4/IPv6
- **Standards:** RFC 7854, RFC 8671, RFC 9069
- **Wireshark Display Filter:** `bmp`
- **Wire Encapsulation:** `Ethernet II -> [802.1Q / QinQ / MPLS] -> IPv4 / IPv6 -> BMP v3 (RFC 7854) (TCP 1790)`

## Overview

Feed live-looking BGP `Adj-RIB-In` telemetry into `pmacct`, `GoBMP`, or `Akvorado` over TCP `1790` without touching production peering routers — `bmp` packs the 42-byte Per-Peer Header and embedded BGP PDUs automatically.

BGP Monitoring Protocol (`bmp`, **TCP `1790`**, RFC 7854) streams real-time pre-policy and post-policy `Adj-RIB-In` route updates, peer session state transitions, and prefix-drop counters from core/edge routers to route collectors such as **pmacct**, **GoBMP**, **OpenBMP**, **Akvorado**, and **Kentik**. Every BMP frame begins with a 6-byte Common Header (`Version = 3`, 32-bit `Message Length`, `Message Type 0..6`), followed by a 42-byte **Per-Peer Header** on peer-scoped messages (`routemon`, `statsreport`, `peerdown`, `peerup`) or Information TLVs on session-scoped messages (`initiation`, `termination`).

FrameAssembly tracks TCP port `1790` sequence numbers automatically, formats IPv4-mapped or native 16-byte IPv6 peer addresses inside the 42-byte Per-Peer Header, and embeds valid BGP `OPEN` / `UPDATE` frames inside `peerup` and `routemon` messages so Wireshark `bmp` dissects the full hierarchy.

## Minimal Quickstart (`bmp_quickstart.frasm`)

```frameassembly
host edge_rtr     { ip 192.0.2.1 }
host bmp_collector { ip 192.0.2.100 }

compile {
    edge_rtr -> bmp_collector tcp syn srcport 51790 dstport 1790 win 65535 mss 1460 wait 1ms
    edge_rtr <- bmp_collector tcp syn ack win 65535 mss 1460 wait 1ms
    edge_rtr -> bmp_collector tcp ack wait 1ms
    edge_rtr -> bmp_collector bmp initiation sysname "edge-rtr-01" sysdesc "FRRouting 10.1" wait 2ms
    edge_rtr -> bmp_collector bmp peerup peerip 198.51.100.2 asn 64512 bgpid 198.51.100.2
}
```

### Automatic Wire Calculations

- Defaults to stateful TCP `dstport 1790` and computes the 32-bit big-endian BMP `Message Length` across the 6-byte Common Header, 42-byte Per-Peer Header, and embedded BGP PDU or TLVs.
- Sets the `V` flag (`0x80`) automatically in the Per-Peer Header when `peerip` is an IPv6 address, or encodes an IPv4 address in the lower 4 bytes of the 16-byte Peer Address field.
- Populates Type `1` (`sysDescr`) and Type `2` (`sysName`) Information TLVs on `initiation` frames and embeds valid RFC 4271 BGP framing inside `peerup` and `routemon`.

## Practical Lab Example: Full BMP v3 Telemetry Export: Initiation, eBGP Peer-Up, Route Monitoring & Peer-Down (`bmp_collector_stream.frasm`)

Models a border router opening a TCP `1790` session to an OpenBMP/pmacct collector, reporting `initiation` metadata, bringing up eBGP peer `AS 65100`, exporting a `routemon` RIB update plus `statsreport`, and logging a `peerdown` event.

```frameassembly
host border_pe { ip 10.250.0.11 }
host pmacct_rx { ip 10.250.0.90 }

compile {
    // 1. Router initiates TCP 1790 connection to BMP collector
    border_pe -> pmacct_rx tcp syn srcport 53790 dstport 1790 win 65535 mss 1460 wait 1ms
    border_pe <- pmacct_rx tcp syn ack win 65535 mss 1460 wait 1ms
    border_pe -> pmacct_rx tcp ack wait 1ms
    // 2. Session Initiation TLVs (sysName + sysDescr)
    border_pe -> pmacct_rx bmp initiation sysname "border-pe-fra01" sysdesc "Arista EOS 4.32" wait 2ms
    // 3. Per-peer telemetry: Peer Up -> Route Monitoring -> Stats Report -> Peer Down
    border_pe -> pmacct_rx bmp peerup peertype 0 peerip 203.0.113.9 asn 65100 bgpid 203.0.113.9 wait 2ms
    border_pe -> pmacct_rx bmp routemon peertype 0 flags 0x00 peerip 203.0.113.9 asn 65100 bgpid 203.0.113.9 wait 2ms
    border_pe -> pmacct_rx bmp statsreport peerip 203.0.113.9 asn 65100 bgpid 203.0.113.9 wait 5ms
    border_pe -> pmacct_rx bmp peerdown peerip 203.0.113.9 asn 65100 bgpid 203.0.113.9 reason 1
}
```

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

# 2. Compile .frasm -> classic .pcap, nanosecond .pcapng, or pipe via '-'
frameassembly bmp_collector_stream.frasm bmp_collector_stream.pcap
frameassembly bmp_collector_stream.frasm bmp_collector_stream.pcapng

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

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

## All `bmp` Keywords & Header Options

Packet line pattern: `src_host -> dst_host bmp [routemon | statsreport | peerdown | peerup | initiation | termination] [version <0..255>] [peertype <0..255>] [flags <0..255>] [peerip <ip>] [asn <number>] [bgpid <ipv4>] [sysname "<text>"] [sysdesc "<text>"] [reason <0..65535>]`

| Keyword / Option | Accepted Value | Default | Effect on Packet |
| --- | --- | --- | --- |
| `routemon \| statsreport \| peerdown \| peerup \| initiation \| termination` | BMP v3 message type keyword | `initiation (Type 4) or routemon (Type 0)` | Selects RFC 7854 Message Type (`0`=Route Monitoring, `1`=Stats Report, `2`=Peer Down, `3`=Peer Up, `4`=Initiation, `5`=Termination). |
| `version <0..255>` | 8-bit BMP protocol version | `3 (RFC 7854)` | Writes byte 0 of the 6-byte BMP Common Header (`3` for standard BMPv3). |
| `peertype <0..255> \| flags <0..255>` | Per-Peer Header Type & Flags | `peertype 0 (Global), flags auto (0x80 if IPv6 peerip)` | `peertype` selects Global (`0`), RD Instance (`1`), Local (`2`), or Loc-RIB (`3`); `flags` sets `V`/`L`/`A` bits (`0x40` = Post-Policy). |
| `peerip <ip> \| asn <number> \| bgpid <ipv4>` | Monitored BGP neighbor IP, 32-bit ASN & 4-byte BGP ID | `192.0.2.2, asn 65001, bgpid 192.0.2.2` | Populates the 16-byte Peer Address, 4-byte Peer AS, and 4-byte Peer BGP ID inside the 42-byte Per-Peer Header. |
| `sysname "<text>" \| sysdesc "<text>"` | Quoted UTF-8 strings | `"router", "FrameAssembly BMP"` | Encodes Information TLV Type `2` (`sysName`) and Type `1` (`sysDescr`) on `initiation` frames. |
| `reason <0..65535>` | Reason code on peerdown or termination | `1` | Sets the Peer Down Reason byte (`1..5`) on `peerdown` or 16-bit Termination Reason TLV on `termination`. |

## Cross-Layer Options Valid with `bmp`

| 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). |
| `comment "<text>"` | Quoted UTF-8 annotation | `Omitted` | Attaches an Enhanced Packet Block `opt_comment` annotation when compiling to `.pcapng` (Wireshark `pkt_comment` filter). |
| `payload "<text>" \| hexpayload "<hex>"` | Quoted text or hex bytes (choose one) | `Omitted` | Appends or overrides raw payload bytes (`"deadbeef"`, `"de:ad:be:ef"`, or `"\x01\x02"`) and updates packet lengths and checksums. |
| `srcmac <mac> \| dstmac <mac> \| ethertype <0..65535>` | Colon/hyphen/Cisco MAC · EtherType 0..65535 (e.g. 0x0800) | `Host MAC / RFC multicast MAC / auto EtherType` | Overrides the 14-byte Ethernet II source MAC, destination MAC, or standalone Layer 2 16-bit EtherType. |
| `fcs \| badfcs` | Ethernet CRC-32 trailer flags (choose one) | `Omitted` | `fcs` appends a valid IEEE 802.3 4-byte CRC-32 Frame Check Sequence trailer; `badfcs` XOR-corrupts the CRC-32 for NIC/tap negative tests. |
| `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>]` | Repeatable label stack (0..1048575, exp/tc 0..7, bos 0..1, ttl 0..255) | `Unlabeled (auto bos=0 on outer labels, bos=1 on bottom)` | Prepends one or more 32-bit MPLS label shims (`0x8847`, e.g. `mpls 16000 mpls 24010`) ahead of Layer 2 EoMPLS or Layer 3 payloads. |
| `ttl <0..255> [dscp <0..63>] [ecn <0..3>]` | TTL/Hop Limit 0..255, DSCP 0..63, ECN 0..3 | `ttl 64, dscp 0, ecn 0` | Sets IPv4 TTL or IPv6 Hop Limit plus DiffServ (DSCP) and ECN QoS markings. |
| `ipid <0..65535> [df] [mf] [fragoffset <0..8191>] [routeralert] [badipchecksum]` | IPv4 ID, DF/MF flags, offset 0..8191, Router Alert & checksum fault | `ipid 1, flags 0, fragoffset 0, valid RFC 1071 checksum` | Sets IPv4 Identification/fragmentation, injects RFC 2113 Router Alert (`20 04 00 00`), or corrupts the 16-bit IPv4 header checksum (`badipchecksum`). |
| `flowlabel <0..1048575>` | 20-bit IPv6 Flow Label (0..1048575) | `0` | Sets the 20-bit IPv6 Flow Label for ECMP routing tests (valid on IPv6 endpoint pairs only). |
| `srcport <0..65535> \| dstport <0..65535>` | Port number 0..65535 | `srcport 49152 (remembered), TCP 1790` | Overrides source or destination port and remembers the port pair automatically on `<-` replies. |
| `badchecksum` | L4/ICMP/SCTP checksum fault injection flag | `Valid RFC checksum / CRC-32c` | Deliberately corrupts the TCP, SCTP CRC-32c, ICMP, or ICMPv6 checksum for negative IDS/firewall testing. |
| `seq <number> \| acknum <number> \| win\|window <0..65535> \| urgent <0..65535> \| syn \| ack \| psh \| fin \| rst \| urg \| ece \| cwr` | TCP sequence/ack numbers, window, flags, SACK, TCP-MD5 & TFO options | `psh ack, win 65535, automatic TCP seq/acknum` | Overrides automatic TCP sequence tracking, flags, or RFC 7323/2018/2385/7413 options (`mss`, `wscale`, `sackperm`, `sackblock`, `tsval`, `tsecr`, `tcpmd5`, `tfo`). |

## Common Mistakes & Quick Fixes

### BMP Runs Unidirectionally From Router to Collector Over TCP 1790

- **Avoid:** `Expecting BMP request/response queries from the collector`
- **Valid v3.3.0 Syntax:** `router -> collector bmp initiation sysname "rtr-01"`

Per RFC 7854 Section 3.2, the monitored router sends BMP messages unidirectionally; the collector only acknowledges at the TCP layer (`tcp syn ack` / `tcp ack`) and never transmits BMP PDUs.

### bgpid Is Always a 4-Byte Dotted-Quad Identifier

- **Avoid:** `router -> collector bmp peerup peerip 2001:db8::2 bgpid 2001:db8::2`
- **Valid v3.3.0 Syntax:** `router -> collector bmp peerup peerip 2001:db8::2 bgpid 192.0.2.2`

Even when `peerip` is an IPv6 neighbor (`2001:db8::2`), RFC 4271 / RFC 6286 BGP Identifiers (`bgpid`) are always 32-bit dotted-quad values.
