# bgp — Stateful TCP 179 Peering, NLRI & 4-Byte ASN Magic (`bgp`)

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

- **Full Protocol Name:** Border Gateway Protocol 4 & 4-Byte ASN Capability
- **Keyword:** `bgp`
- **OSI Layer & Transport:** Layer 3 · TCP 179
- **Host Endpoint Rule:** Dual-Stack IPv4/IPv6
- **Standards:** RFC 4271, RFC 6793
- **Wireshark Display Filter:** `bgp`
- **Wire Encapsulation:** `Ethernet II -> [802.1Q / QinQ / MPLS] -> IPv4 / IPv6 -> BGP-4 / 4-Byte ASN (TCP 179)`

## Overview

Pass `asn 4200000001` on `bgp open` and the compiler automatically drops `AS_TRANS` (`23456`) into the legacy 16-bit slot plus RFC 6793 Capability `65`, riding right on top of stateful TCP port `179`.

Border Gateway Protocol 4 (`bgp`, RFC 4271) exchanges inter-domain and data-center leaf-spine routes over **TCP port `179`**. Every BGP message starts with a 16-byte all-ones (`ff..ff`) marker, 16-bit Message Length (`19..4096`), and 8-bit Type (`1` `open`, `2` `update`, `3` `notification`, `4` `keepalive`).

FrameAssembly tracks TCP sequence and acknowledgment numbers automatically across `tcp` and `bgp` packets—and when you pass a **4-byte ASN (`asn > 65535`, e.g. `4200000001`)** on `bgp open`, it automatically sets `AS_TRANS` (`23456`) in the 16-bit header field and adds **RFC 6793 Capability Code `65`** with the full 4-byte ASN.

## Minimal Quickstart (`bgp_quickstart.frasm`)

```frameassembly
host spine_01 { ip 10.255.0.1 }
host leaf_01  { ip 10.255.0.2 }

compile {
    spine_01 -> leaf_01 bgp open asn 65001 holdtime 90 bgpid 10.255.0.1 wait 2ms
    spine_01 <- leaf_01 bgp open asn 65101 holdtime 90 bgpid 10.255.0.2 wait 2ms
    spine_01 -> leaf_01 bgp keepalive
}
```

### Automatic Wire Calculations

- Tracks TCP sequence and acknowledgment numbers automatically on port `179` (`psh ack` flags set by default) and prepends the 16-byte `ff..ff` BGP marker.
- On `bgp open` with `asn > 65535`, writes `AS_TRANS` (`23456`) in the 16-bit My AS field and encodes Optional Parameter `2` (Capabilities) -> Capability `65` (4-octet AS) with the full 32-bit `asn`.
- On `bgp update`, encodes well-known mandatory Path Attributes (`ORIGIN = IGP`, `AS_PATH`, `NEXT_HOP`) and bit-packed CIDR `NLRI` (`prefix` / `nlri`).

## Practical Lab Example: Stateful TCP Handshake + 4-Byte ASN eBGP Peering & Route Advertisement (`bgp_4byte_asn_session.frasm`)

Establishes a GTSM-protected (`ttl 255`) TCP 179 session between two 4-byte ASN switches (`4200000001` and `4200000101`), exchanges `OPEN` + `KEEPALIVE`, and advertises `10.100.0.0/16`.

```frameassembly
host leaf_tor  { ip 172.16.255.1 }
host spine_agg { ip 172.16.255.2 }

compile {
    // 1. Stateful TCP 3-way handshake on port 179 with GTSM (ttl 255) and CS6 (dscp 48)
    leaf_tor -> spine_agg tcp syn srcport 53179 dstport 179 win 65535 mss 1460 ttl 255 dscp 48 wait 1ms
    leaf_tor <- spine_agg tcp syn ack win 65535 mss 1460 ttl 255 dscp 48 wait 1ms
    leaf_tor -> spine_agg tcp ack ttl 255 dscp 48 wait 1ms
    // 2. 4-Byte ASN OPEN negotiation (auto-encodes AS_TRANS 23456 + Capability 65)
    leaf_tor -> spine_agg bgp open asn 4200000101 holdtime 90 bgpid 10.0.1.1 ttl 255 dscp 48 wait 2ms
    leaf_tor <- spine_agg bgp open asn 4200000001 holdtime 90 bgpid 10.0.0.1 ttl 255 dscp 48 wait 2ms
    leaf_tor -> spine_agg bgp keepalive ttl 255 dscp 48 wait 1ms
    leaf_tor <- spine_agg bgp keepalive ttl 255 dscp 48 wait 2ms
    // 3. Advertise tenant CIDR 10.100.0.0/16 via BGP UPDATE
    leaf_tor -> spine_agg bgp update asn 4200000101 prefix "10.100.0.0/16" nexthop 172.16.255.1 ttl 255 dscp 48
}
```

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

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

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

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

## All `bgp` Keywords & Header Options

Packet line pattern: `src_host -> dst_host bgp [open | update | notification | keepalive] [asn <number>] [holdtime <0..65535>] [bgpid <ipv4>] [prefix|nlri <str|ip>] [nexthop <ip>]`

| Keyword / Option | Accepted Value | Default | Effect on Packet |
| --- | --- | --- | --- |
| `open \| update \| notification \| keepalive` | BGP message type keyword | `keepalive (Type 4)` | Selects Type `1` OPEN, Type `2` UPDATE, Type `3` NOTIFICATION (Cease error), or Type `4` 19-byte KEEPALIVE. |
| `asn <number>` | 16-bit or 32-bit Autonomous System Number | `65001` | Sets the Autonomous System in `open` (`<= 65535` direct; `> 65535` `AS_TRANS` `23456` + Capability `65`) and `AS_PATH` in `update`. |
| `holdtime <0..65535>` | Whole number (0..65535) (seconds) | `180` | Sets the BGP Hold Time in `open` (`0` or `>= 3` seconds per RFC 4271). |
| `bgpid <ipv4>` | 32-bit dotted-quad identifier | `Source IPv4 (or 1.1.1.1 on IPv6)` | Sets the 4-byte BGP Identifier in `open` messages (always 32-bit dotted-quad on both v4 and v6). |
| `prefix <str\|ip> \| nlri <str\|ip>` | CIDR string ("10.20.0.0/16") or IP literal | `"10.0.0.0/24"` | Encodes Network Layer Reachability Information (`nlri` is an exact alias for `prefix`) in `update` messages. |
| `nexthop <ip>` | IPv4 or IPv6 address literal | `Source host IP` | Sets the `NEXT_HOP` path attribute inside `bgp update` messages. |

## Cross-Layer Options Valid with `bgp`

| 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. |
| `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>]` | IPv4 ID, DF/MF flags, fragment offset 0..8191 | `ipid 1, flags 0, fragoffset 0` | Sets IPv4 Identification, Do-Not-Fragment (`df`), More-Fragments (`mf`), and fragment offset (ignored over IPv6). |
| `flowlabel <0..1048575>` | 20-bit IPv6 Flow Label (0..1048575) | `0` | Sets the 20-bit IPv6 Flow Label for ECMP routing tests (ignored when running over IPv4 hosts). |
| `srcport <0..65535> \| dstport <0..65535>` | Port number 0..65535 | `srcport 49152 (remembered), TCP 179` | Overrides source or destination port and remembers the port pair automatically on `<-` replies. |
| `seq <number> \| acknum <number> \| win\|window <0..65535> \| urgent <0..65535> \| syn \| ack \| psh \| fin \| rst \| urg \| ece \| cwr` | TCP sequence/ack numbers, window size, flags & RFC 7323 options | `psh ack, win 65535, automatic TCP seq/acknum` | Overrides automatic TCP sequence tracking, flags, or RFC 7323 options (`mss`, `wscale`, `sackperm`, `tsval`, `tsecr`). |

## Common Mistakes & Quick Fixes

### Quote CIDR Prefixes in bgp update (Never Put /24 in host Blocks)

- **Avoid:** `host r1 { ip 10.0.0.1/24 }
r1 -> r2 bgp update prefix 10.20.0.0/16`
- **Valid v3.2.2 Syntax:** `host r1 { ip 10.0.0.1 }
r1 -> r2 bgp update prefix "10.20.0.0/16"`

`host` declarations take plain IP literals without CIDR slashes, while CIDR strings in `bgp update prefix "10.20.0.0/16"` must be wrapped in double quotes.

### bgpid Is Always 32-Bit Dotted-Quad Even Over IPv6 eBGP

- **Avoid:** `r6_a -> r6_b bgp open asn 65001 bgpid 2001:db8::1`
- **Valid v3.2.2 Syntax:** `r6_a -> r6_b bgp open asn 65001 bgpid 10.0.0.1`

Per RFC 4271 and RFC 6286, the BGP Identifier is always a 4-octet 32-bit dotted-quad (`10.0.0.1`) regardless of whether TCP 179 runs over IPv4 or IPv6.
