# rpkirtr — TCP 323 RPKI Route Origin Validation ROA Cache Sync (`rpkirtr`)

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

- **Full Protocol Name:** RPKI-Router Origin Validation Protocol
- **Keyword:** `rpkirtr`
- **OSI Layer & Transport:** Layer 3 · TCP 323
- **Host Endpoint Rule:** Dual-Stack IPv4/IPv6
- **Standards:** RFC 6810, RFC 8210
- **Wireshark Display Filter:** `rpki-rtr`
- **Wire Encapsulation:** `Ethernet II -> [802.1Q / QinQ / MPLS] -> IPv4 / IPv6 -> RPKI-RTR (TCP 323) (TCP 323)`

## Overview

Stop fat-fingered BGP route leaks at the border: `rpkirtr` streams RFC 8210 `ipv4prefix` and `ipv6prefix` Validated ROA Payloads (`prefix`, `maxlen`, `asn`) over TCP `323` just like Routinator.

RPKI-Router Protocol (`rpkirtr`, **TCP `323`**, RFC 6810 v0 / RFC 8210 v1) delivers cryptographically validated **Route Origin Authorization (ROA)** prefix-to-ASN bindings (Validated ROA Payloads, VRPs) from local RPKI relying-party caches (**Routinator**, **Cloudflare OctoRPKI**, **Fort**, **stayrtr**) to border BGP routers performing **Route Origin Validation (ROV)**. Routers send `resetquery` or `serialquery` PDUs, and the cache streams back `cacheresponse`, `ipv4prefix` / `ipv6prefix` announcements or withdrawals (`prefix`, `maxlen`, `asn`), and `endofdata`.

FrameAssembly builds exact binary RFC 8210 v1 (default) or RFC 6810 v0 PDUs—including 20-byte IPv4 Prefix PDUs, 32-byte IPv6 Prefix PDUs, 24-byte v1 `End of Data` timing headers (`refresh`, `retry`, `expire`), and structured `errorreport` diagnostics—over stateful TCP port `323`.

## Minimal Quickstart (`rpkirtr_quickstart.frasm`)

```frameassembly
host edge_bgp   { ip 192.0.2.10 }
host routinator { ip 192.0.2.53 }

compile {
    edge_bgp -> routinator rpkirtr resetquery version 1 wait 1ms
    edge_bgp <- routinator rpkirtr cacheresponse version 1 sessionid 42 wait 1ms
    edge_bgp <- routinator rpkirtr ipv4prefix version 1 announce prefix "203.0.113.0/24" maxlen 24 asn 64500 wait 1ms
    edge_bgp <- routinator rpkirtr endofdata version 1 sessionid 42 serial 100 refresh 3600 retry 600 expire 7200
}
```

### Automatic Wire Calculations

- Defaults to stateful TCP `dstport 323` and RFC 8210 `version 1` (including the 12 extra refresh/retry/expire interval bytes on v1 `endofdata` vs 12-byte v0 `endofdata`).
- Parses `prefix "<cidr>"` (`"203.0.113.0/24"` or `"2001:db8::/32"`) into Prefix Length, Max Length (`defaults to prefix_len` if `maxlen` is omitted), and binary IPv4 (4B) or IPv6 (16B) octets.
- Sets Flags byte `1` for `announce` (default) or `0` for `withdraw` on `ipv4prefix` and `ipv6prefix` PDUs.

## Practical Lab Example: Full RFC 8210 RPKI-RTR v1 Bootstrap & Incremental ROA Withdrawal Cycle (`rpkirtr_roa_sync.frasm`)

Models a border router establishing TCP `323` with a Routinator cache, executing a `resetquery` full table sync (IPv4 + IPv6 VRPs + `endofdata`), and later receiving a `serialnotify` incremental ROA `withdraw`.

```frameassembly
host peering_rtr { ip 10.30.0.1 }
host rpki_cache  { ip 10.30.0.20 }

compile {
    // 1. TCP 323 handshake between BGP router and Routinator validator
    peering_rtr -> rpki_cache tcp syn srcport 52323 dstport 323 win 65535 mss 1460 wait 1ms
    peering_rtr <- rpki_cache tcp syn ack win 65535 mss 1460 wait 1ms
    peering_rtr -> rpki_cache tcp ack wait 1ms
    // 2. Initial Reset Query -> Cache Response + IPv4/IPv6 VRPs + End of Data (serial 500)
    peering_rtr -> rpki_cache rpkirtr resetquery version 1 wait 1ms
    peering_rtr <- rpki_cache rpkirtr cacheresponse version 1 sessionid 7 wait 1ms
    peering_rtr <- rpki_cache rpkirtr ipv4prefix version 1 announce prefix "198.51.100.0/24" maxlen 24 asn 65010 wait 1ms
    peering_rtr <- rpki_cache rpkirtr ipv6prefix version 1 announce prefix "2001:db8:40::/48" maxlen 48 asn 65010 wait 1ms
    peering_rtr <- rpki_cache rpkirtr endofdata version 1 sessionid 7 serial 500 refresh 1800 retry 600 expire 7200 wait 5ms
    // 3. Cache bumps serial to 501 and withdraws stale ROA
    peering_rtr <- rpki_cache rpkirtr serialnotify version 1 sessionid 7 serial 501 wait 1ms
    peering_rtr -> rpki_cache rpkirtr serialquery version 1 sessionid 7 serial 500 wait 1ms
    peering_rtr <- rpki_cache rpkirtr cacheresponse version 1 sessionid 7 wait 1ms
    peering_rtr <- rpki_cache rpkirtr ipv4prefix version 1 withdraw prefix "198.51.100.0/24" maxlen 24 asn 65010 wait 1ms
    peering_rtr <- rpki_cache rpkirtr endofdata version 1 sessionid 7 serial 501 refresh 1800 retry 600 expire 7200
}
```

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

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

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

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

## All `rpkirtr` Keywords & Header Options

Packet line pattern: `src_host -> dst_host rpkirtr [serialnotify | serialquery | resetquery | cacheresponse | ipv4prefix | ipv6prefix | endofdata | cachereset | errorreport] [version <0|1>] [sessionid <0..65535>] [serial <number>] [announce | withdraw | flags <0..255>] [prefix "<cidr>"] [maxlen <0..255>] [asn <number>] [refresh <number>] [retry <number>] [expire <number>] [errcode <0..65535>] [errmsg "<text>"]`

| Keyword / Option | Accepted Value | Default | Effect on Packet |
| --- | --- | --- | --- |
| `serialnotify \| serialquery \| resetquery \| cacheresponse \| ipv4prefix \| ipv6prefix \| endofdata \| cachereset \| errorreport` | RPKI-RTR PDU type keyword | `ipv4prefix (Type 4)` | Selects PDU Type (`0`=Serial Notify, `1`=Serial Query, `2`=Reset Query, `3`=Cache Response, `4`=IPv4 Prefix, `6`=IPv6 Prefix, `7`=End of Data, `8`=Cache Reset, `10`=Error Report). |
| `version <0\|1>` | Protocol version (0 = RFC 6810, 1 = RFC 8210) | `1 (RFC 8210)` | Sets Protocol Version in byte 0 (`1` adds `refresh`, `retry`, and `expire` timers to `endofdata`). |
| `sessionid <0..65535> \| serial <number>` | 16-bit Cache Session ID & 32-bit Serial Number | `sessionid 1, serial 1` | Tracks cache epoch (`sessionid`) and monotonic serial state across `serialnotify`, `serialquery`, `cacheresponse`, and `endofdata`. |
| `announce \| withdraw \| flags <0..255>` | ROA announcement flag (`1`) or withdrawal (`0`) | `announce (flags 1)` | Sets the `Flags` byte in `ipv4prefix` / `ipv6prefix` PDUs (`1` announce VRP, `0` withdraw VRP). |
| `prefix "<cidr>" \| maxlen <0..128> \| asn <number>` | Quoted IPv4/IPv6 CIDR, Max Length & 32-bit Origin ASN | `"192.0.2.0/24", maxlen = prefix_len, asn 65001` | Writes the Prefix Length, Max Length, IPv4/IPv6 prefix bytes, and 4-byte Origin AS Number. |
| `refresh <number> \| retry <number> \| expire <number>` | 32-bit intervals in seconds (v1 endofdata) | `refresh 3600, retry 600, expire 7200` | Populates the three RFC 8210 timing parameters at the end of `version 1` `endofdata` PDUs. |
| `errcode <0..65535> \| errmsg "<text>"` | 16-bit error code & UTF-8 diagnostic text | `errcode 0, ""` | Populates Error Code (`0..8`) and length-prefixed UTF-8 Error Text inside `errorreport` (`Type 10`) PDUs. |

## Cross-Layer Options Valid with `rpkirtr`

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

### Match ipv4prefix vs ipv6prefix to Your CIDR Address Family

- **Avoid:** `rtr <- cache rpkirtr ipv4prefix prefix "2001:db8::/32" maxlen 48 asn 65001`
- **Valid v3.3.0 Syntax:** `rtr <- cache rpkirtr ipv6prefix prefix "2001:db8::/32" maxlen 48 asn 65001`

`ipv4prefix` (`PDU Type 4`, 20 bytes) carries a 4-byte IPv4 prefix, whereas `ipv6prefix` (`PDU Type 6`, 32 bytes) carries a 16-byte IPv6 prefix.

### Keep maxlen >= CIDR Prefix Length

- **Avoid:** `rtr <- cache rpkirtr ipv4prefix prefix "203.0.113.0/24" maxlen 16 asn 64500`
- **Valid v3.3.0 Syntax:** `rtr <- cache rpkirtr ipv4prefix prefix "203.0.113.0/24" maxlen 24 asn 64500`

Per RFC 8210 Section 5.6, `Max-Length` must be greater than or equal to `Prefix-Length` (`<= 32` for IPv4, `<= 128` for IPv6) or routers reject the VRP as corrupt.
