# ikev2 — RFC 7296 VPN Key Negotiation on UDP 500 & 4500 (`ikev2`)

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

- **Full Protocol Name:** Internet Key Exchange Protocol Version 2
- **Keyword:** `ikev2`
- **OSI Layer & Transport:** Layer 3 · UDP 500 -> 500 (NAT-T 4500)
- **Host Endpoint Rule:** Dual-Stack IPv4/IPv6
- **Standards:** RFC 7296, RFC 3948
- **Wireshark Display Filter:** `isakmp`
- **Wire Encapsulation:** `Ethernet II -> [802.1Q / QinQ / MPLS] -> IPv4 / IPv6 -> IKEv2 (UDP 500 -> 500 (NAT-T 4500))`

## Overview

Before a single `esp` packet flows, two VPN concentrators argue about crypto proposals over UDP `500`. `ikev2` scripts `sainit <-> auth` handshakes and NAT-T `dstport 4500` checks.

Internet Key Exchange Version 2 (`ikev2`, RFC 7296) performs mutual authentication and establishes IPsec Security Associations over **UDP `500 -> 500`** (or **UDP `4500 -> 4500`** when NAT-Traversal is detected). Every IKEv2 packet starts with a 28-byte header containing 64-bit Initiator and Responder SPIs (`initiatorspi`, `responderspi`), Next Payload type (`nextpayload` / `nextheader`), Version `0x20` (2.0), Exchange Type (`34..37`), Flags (`0x08` Initiator, `0x20` Response), Message ID (`msgid`), and 32-bit total Length.

FrameAssembly encodes valid IKEv2 headers and chained payload structures across all four RFC 7296 exchange modes (`sainit`, `auth`, `createchildsa`, `informational`).

## Minimal Quickstart (`ikev2_quickstart.frasm`)

```frameassembly
host initiator { ip 198.51.100.10 }
host responder { ip 203.0.113.20 }

compile {
    initiator -> responder ikev2 sainit initiatorspi 0xa1b2c3d4e5f60708 msgid 0 wait 3ms
    initiator <- responder ikev2 sainit initiatorspi 0xa1b2c3d4e5f60708 responderspi 0x1122334455667788 flags 0x20 msgid 0
}
```

### Automatic Wire Calculations

- Defaults both `srcport` and `dstport` to IANA IKE port `500` (avoiding ephemeral `49152` source ports blocked by strict VPN ACLs).
- Writes the 28-byte RFC 7296 IKEv2 header (`Version 0x20`, exact 32-bit `Length`, 64-bit SPIs) and defaults `responderspi` to `0` on `sainit` requests.
- Accepts exchange modes either bare (`ikev2 auth`) or prefixed (`ikev2 exchange auth`), and treats `nextheader` and `nextpayload` as exact synonyms.

## Practical Lab Example: IKEv2 SA_INIT on UDP 500 Floating to NAT-Traversal UDP 4500 + DPD Check (`ikev2_natt_dpd.frasm`)

Traces a complete IKEv2 phase: `SA_INIT` on UDP `500`, NAT-T port float to UDP `4500` for encrypted `IKE_AUTH` (`nextpayload 46`), and an `INFORMATIONAL` Dead Peer Detection exchange.

```frameassembly
host branch_fw { ip 198.51.100.15 }
host cloud_vpn { ip 203.0.113.99 }

compile {
    // 1. IKE_SA_INIT on UDP 500 -> 500 (Next Payload 33 = Security Association)
    branch_fw -> cloud_vpn ikev2 sainit initiatorspi 0x9080706050403020 responderspi 0 msgid 0 flags 0x08 nextpayload 33 wait 4ms
    branch_fw <- cloud_vpn ikev2 sainit initiatorspi 0x9080706050403020 responderspi 0x0102030405060708 msgid 0 flags 0x20 nextpayload 33 wait 4ms
    // 2. Float to UDP 4500 for IKE_AUTH (Next Payload 46 = Encrypted & Authenticated SK)
    branch_fw -> cloud_vpn ikev2 auth srcport 4500 dstport 4500 initiatorspi 0x9080706050403020 responderspi 0x0102030405060708 msgid 1 flags 0x08 nextpayload 46 wait 5ms
    branch_fw <- cloud_vpn ikev2 auth srcport 4500 dstport 4500 initiatorspi 0x9080706050403020 responderspi 0x0102030405060708 msgid 1 flags 0x20 nextpayload 46 wait 50ms
    // 3. INFORMATIONAL Dead Peer Detection (DPD) liveness check
    branch_fw -> cloud_vpn ikev2 informational srcport 4500 dstport 4500 initiatorspi 0x9080706050403020 responderspi 0x0102030405060708 msgid 2 flags 0x08 nextpayload 46 wait 3ms
    branch_fw <- cloud_vpn ikev2 informational srcport 4500 dstport 4500 initiatorspi 0x9080706050403020 responderspi 0x0102030405060708 msgid 2 flags 0x20 nextpayload 46
}
```

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

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

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

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

## All `ikev2` Keywords & Header Options

Packet line pattern: `src_host -> dst_host ikev2 [exchange <sainit|auth|createchildsa|informational> | sainit | auth | createchildsa | informational] [initiatorspi <u64|"hex">] [responderspi <u64|"hex">] [msgid <number>] [flags <0..255>] [nextpayload|nextheader <0..255>]`

| Keyword / Option | Accepted Value | Default | Effect on Packet |
| --- | --- | --- | --- |
| `sainit \| auth \| createchildsa \| informational \| exchange <...>` | RFC 7296 Exchange Type keyword or u8 | `sainit (34)` | Sets Exchange Type: `sainit` (`34` IKE_SA_INIT), `auth` (`35` IKE_AUTH), `createchildsa` (`36` CREATE_CHILD_SA), `informational` (`37` INFORMATIONAL). |
| `initiatorspi <u64\|"hex"> \| responderspi <u64\|"hex">` | 64-bit integer or hex string | `Deterministic initiator SPI; responderspi 0 on sainit` | Populates the 8-byte Initiator SPI and 8-byte Responder SPI in the IKEv2 header. |
| `msgid <number>` | Whole number | `0` | Sets the 4-byte IKEv2 Message ID (`0` for `sainit`, `1` for initial `auth`, incrementing per request/response pair). |
| `flags <0..255>` | 8-bit bitmask | `0x08 (Initiator)` | Sets RFC 7296 Flags (`0x08` = Initiator `I`, `0x10` = Version `V`, `0x20` = Response `R`; responder replies to initiator typically use `0x20`). |
| `nextpayload <0..255> \| nextheader <0..255>` | 8-bit IKEv2 Payload Type | `33 (SA) or 0 (None)` | Sets the first chained IKEv2 Payload Type (`33` SA, `34` KE, `40` Nonce, `46` Encrypted SK, `0` None). |

## Cross-Layer Options Valid with `ikev2`

| 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), UDP 500 -> 500 (NAT-T 4500)` | Overrides source or destination port and remembers the port pair automatically on `<-` replies. |

## Common Mistakes & Quick Fixes

### Zero Underscores in Exchange Modes

- **Avoid:** `a -> b ikev2 sa_init   // or create_child_sa`
- **Valid v3.2.2 Syntax:** `a -> b ikev2 sainit    // or createchildsa`

Exchange modes follow the strict lowercase alphanumeric rule: `sainit`, `auth`, `createchildsa`, `informational`.

### Set flags 0x20 on Responder Replies

- **Valid v3.2.2 Syntax:** `a <- b ikev2 sainit initiatorspi 0x1122 responderspi 0x3344 flags 0x20`

On responder replies (`<-`), setting `flags 0x20` sets the RFC 7296 Response (`R`) bit and clears the Initiator (`I`) bit so Wireshark labels the packet `(Response)`.
