# ngap — 3GPP 5G N2 gNodeB-to-AMF ASN.1 APER over SCTP 38412 (`ngap`)

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

- **Full Protocol Name:** 5G Next Generation Application Protocol (N2 gNB <-> AMF over SCTP)
- **Keyword:** `ngap`
- **OSI Layer & Transport:** Layer 7 · SCTP 38412 (PPID 60)
- **Host Endpoint Rule:** Dual-Stack IPv4/IPv6
- **Standards:** 3GPP TS 38.413, 3GPP TS 24.501, RFC 9260
- **Wireshark Display Filter:** `ngap`
- **Wire Encapsulation:** `Ethernet II -> [802.1Q / QinQ / MPLS] -> IPv4 / IPv6 -> SCTP 38412 (PPID 60) -> 5G NGAP (3GPP TS 38.413)`

## Overview

Native SCTP `38412` (`PPID 60`) with zero external gNodeB simulator: `ngap` encodes 3GPP TS 38.413 ASN.1 Aligned PER PDUs (`ngsetuprequest`, `initialuemessage`, `plmn`, `gnbid`, `tac`, and `naspdu` hex).

Next Generation Application Protocol (`ngap`, 3GPP TS 38.413) is the **5G Standalone `N2` control-plane interface** connecting 5G **gNodeB (`gNB`)** radio base stations to the **Access and Mobility Management Function (`AMF`)** exclusively over **SCTP port `38412` (`PPID = 60`)**. Crafting valid NGAP frames manually is notoriously difficult because NGAP uses **ASN.1 Aligned Packed Encoding Rules (APER)** with sorted ProtocolIE-Container sequences, PLMN BCD nibbles, 24-bit Tracking Area Codes (`tac`), `AMF-UE-NGAP-ID` / `RAN-UE-NGAP-ID`, and embedded **5G-NAS (`3GPP TS 24.501`)** payloads.

FrameAssembly encodes genuine ASN.1 APER `InitiatingMessage`, `SuccessfulOutcome`, and `UnsuccessfulOutcome` PDUs wrapped inside Castagnoli CRC-32c SCTP `DATA` chunks (`PPID 60`), rejecting invalid TCP/UDP transport overrides at compile time.

## Minimal Quickstart (`ngap_quickstart.frasm`)

```frameassembly
host gnb_01   { ip 10.52.2.11 }
host amf_core { ip 10.52.2.100 }

compile {
    gnb_01 -> amf_core ngap ngsetuprequest gnbid 0x000101 plmn "00101" tac 1 ranname "gnb-cell-01" wait 4ms
    gnb_01 <- amf_core ngap ngsetupresponse plmn "00101" ranname "amf-region01"
}
```

### Automatic Wire Calculations

- Encapsulates every PDU inside IP Protocol `132` **SCTP (`dstport 38412`, `DATA` chunk `PPID = 60`)** with automatic Castagnoli CRC-32c and SCTP Verification Tag / TSN progression.
- Formats ASN.1 APER `NGAP-PDU` headers (`InitiatingMessage` `0x00`, `SuccessfulOutcome` `0x20`, `UnsuccessfulOutcome` `0x40`), Procedure Codes (`21` NGSetup, `15` InitialUEMessage, `4` DownlinkNASTransport, `46` UplinkNASTransport, `41` UEContextRelease), and ProtocolIE lengths.
- Encodes 5- or 6-digit `plmn` MCC/MNC strings into 3-byte 3GPP BCD format (`"00101"` -> `00 f1 10`) and 24-bit `tac` values automatically.

## Practical Lab Example: 5G Standalone N2 Call Flow:SCTP 4-Way Handshake, NGSetup, UE Registration & Release (`ngap_5g_n2_registration.frasm`)

Brings up a complete SCTP `38412` association (`INIT`/`INIT_ACK`/`COOKIE_ECHO`/`COOKIE_ACK`), completes N2 `NGSetup`, transports a 5GS Registration Request + Downlink Authentication NAS exchange, and releases the UE context.

```frameassembly
host gnb_du01 { ip 172.30.2.15 }
host amf_01   { ip 172.30.2.200 }

compile {
    // 1. Explicit 4-way SCTP association bring-up on N2 port 38412
    gnb_du01 -> amf_01 sctp init srcport 38412 dstport 38412 vtag 0 tsn 1000 wait 1ms
    gnb_du01 <- amf_01 sctp initack vtag 0x44556677 tsn 2000 wait 1ms
    gnb_du01 -> amf_01 sctp cookieecho vtag 0x8899aabb wait 1ms
    gnb_du01 <- amf_01 sctp cookieack vtag 0x44556677 wait 3ms
    // 2. Non-UE-associated NGSetup handshake (Procedure Code 21, Stream 0)
    gnb_du01 -> amf_01 ngap ngsetuprequest gnbid 1024 plmn "99970" tac 100 ranname "factory-gnb-01" wait 3ms
    gnb_du01 <- amf_01 ngap ngsetupresponse plmn "99970" ranname "private5g-amf-01" wait 8ms
    // 3. UE-associated InitialUEMessage + Downlink/Uplink 5G-NAS transport
    gnb_du01 -> amf_01 ngap initialuemessage ranueid 1 plmn "99970" tac 100 naspdu "7e004179000d0199f9070000000000000010" wait 4ms
    gnb_du01 <- amf_01 ngap downlinknastransport amfueid 5001 ranueid 1 naspdu "7e005600020000" wait 4ms
    gnb_du01 -> amf_01 ngap uplinknastransport amfueid 5001 ranueid 1 naspdu "7e00572d10aabbccddeeff00112233445566778899" wait 15ms
    // 4. UE Context Release Command & Complete (Procedure Code 41)
    gnb_du01 <- amf_01 ngap uecontextreleasecommand amfueid 5001 ranueid 1 wait 2ms
    gnb_du01 -> amf_01 ngap uecontextreleasecomplete amfueid 5001 ranueid 1
}
```

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

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

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

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

## All `ngap` Keywords & Header Options

Packet line pattern: `src_host -> dst_host ngap [ngsetuprequest | ngsetupresponse | ngsetupfailure | initialuemessage | downlinknastransport | uplinknastransport | uecontextreleasecommand | uecontextreleasecomplete | errorindication] [msgtype "<text>"] [proccode <0..255>] [criticality "<text>"] [amfueid <number>] [ranueid <number>] [plmn "<text>"] [gnbid <number>] [tac <0..16777215>] [ranname "<text>"] [naspdu "<hex>"]`

| Keyword / Option | Accepted Value | Default | Effect on Packet |
| --- | --- | --- | --- |
| `ngsetuprequest \| ngsetupresponse \| ngsetupfailure \| initialuemessage \| downlinknastransport \| uplinknastransport \| uecontextreleasecommand \| uecontextreleasecomplete \| errorindication` | 3GPP TS 38.413 NGAP Elementary Procedure | `ngsetuprequest` | Selects both the APER PDU Choice (`InitiatingMessage` / `SuccessfulOutcome` / `UnsuccessfulOutcome`) and mandatory 3GPP TS 38.413 Procedure Code + ProtocolIEs. |
| `msgtype "<text>" \| proccode <0..255> \| criticality "<text>"` | Explicit APER PDU class, ProcedureCode (0..255), and Criticality ("reject"\|"ignore"\|"notify") | `Derived from procedure keyword` | Overrides the outer ASN.1 APER `procedureCode` and `criticality` enum (`0` reject, `1` ignore, `2` notify). |
| `amfueid <number> \| ranueid <number>` | 40-bit AMF-UE-NGAP-ID (IE 10) & 32-bit RAN-UE-NGAP-ID (IE 85) | `amfueid 1, ranueid 1 on UE messages` | Encodes APER integer IEs correlating a specific UE across N2 uplink and downlink NAS messages. |
| `plmn "<text>"` | 5- or 6-digit MCC+MNC string (e.g. "00101", "310260", "99970") | `"00101"` | Packs the 3-octet 3GPP `PLMNIdentity` inside GlobalRANNodeID, BroadcastPLMNList, and UserLocationInformation IEs. |
| `gnbid <number> \| tac <0..16777215> \| ranname "<text>"` | gNB-ID integer, 24-bit Tracking Area Code, and PrintableString RANNodeName | `gnbid 1, tac 1, ranname "gNB-01"` | Populates IE `27` (`GlobalRANNodeID`), IE `102` (`SupportedTAList`), IE `121` (`UserLocationInformation`), and IE `82` (`RANNodeName`). |
| `naspdu "<hex>"` | Even-length hex byte string (IE 38 NAS-PDU) | `"7e0041" (5GS Registration stub)` | Embeds raw `3GPP TS 24.501` 5G-NAS message octets inside IE `38` (`NAS-PDU`) for full Wireshark `nas-5gs` decoding. |

## Cross-Layer Options Valid with `ngap`

| 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), SCTP 38412 (PPID 60)` | 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. |

## Common Mistakes & Quick Fixes

### ngap Is Strictly SCTP-Only (Do Not Add transport tcp or transport udp)

- **Avoid:** `gnb -> amf ngap ngsetuprequest transport tcp`
- **Valid v3.3.0 Syntax:** `gnb -> amf ngap ngsetuprequest gnbid 1 plmn "00101"`

3GPP TS 38.412 mandates SCTP (`38412`, `PPID 60`) for N2; `validate_semantics` rejects `transport tcp` and `transport udp` on `ngap` at compile time.

### Pass naspdu as Valid Hex Octets

- **Avoid:** `gnb -> amf ngap initialuemessage naspdu "registration-req"`
- **Valid v3.3.0 Syntax:** `gnb -> amf ngap initialuemessage ranueid 1 naspdu "7e0041"`

`naspdu` expects hexadecimal bytes (`"7e0041"` or `"7e:00:41"`) representing binary 3GPP TS 24.501 5G-NAS PDUs.
