# gtpc — 3GPP S11/N26 GTPv2-C Session & Bearer Signaling (`gtpc`)

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

- **Full Protocol Name:** GPRS Tunneling Protocol v2 Control Plane (4G S11 / S5-C)
- **Keyword:** `gtpc`
- **OSI Layer & Transport:** Layer 7 · UDP 2123
- **Host Endpoint Rule:** Dual-Stack IPv4/IPv6
- **Standards:** 3GPP TS 29.274, RFC 768
- **Wireshark Display Filter:** `gtpv2`
- **Wire Encapsulation:** `Ethernet II -> [802.1Q / QinQ / MPLS] -> IPv4 / IPv6 -> UDP 2123 -> GTPv2-C (3GPP TS 29.274)`

## Overview

Need to test S11 or S5/S8 roaming firewalls on UDP `2123` without spinning up Open5GS? `gtpc` packs 3GPP TS 29.274 GTPv2-C headers, TBCD-encoded `imsi`, DNS-label `apn`, `teid`, and `cause` IEs automatically.

GPRS Tunneling Protocol version 2 Control Plane (`gtpc`, 3GPP TS 29.274) orchestrates subscriber bearer sessions across **4G LTE Evolved Packet Core (`S11` MME <-> SGW, `S5`/`S8` SGW <-> PGW)** and **4G/5G interworking (`N26` AMF <-> MME)** interfaces on **UDP port `2123`**. Every GTPv2-C PDU starts with a Version `2` header (`Flags` with `T`-bit TEID presence, 1-byte `Message Type`, 16-bit `Message Length`, optional 32-bit `TEID`, 24-bit `Sequence Number`, and spare byte) followed by TLV Information Elements (`IMSI`, `Access Point Name`, `RAT Type`, `Cause`, `Recovery`, `PDN Address Allocation`).

FrameAssembly encodes valid BCD-packed `IMSI` digits, DNS-label-encoded `APN` strings, 24-bit sequence numbers, and automatic `T`-bit header formatting (omitting the 4-byte TEID slot on path-management `echorequest`/`echoreply` messages per TS 29.274 Section 5.1).

## Minimal Quickstart (`gtpc_quickstart.frasm`)

```frameassembly
host mme_s11 { ip 10.200.11.10 }
host sgw_s11 { ip 10.200.11.20 }

compile {
    mme_s11 -> sgw_s11 gtpc createsessionrequest teid 0x00000000 seqnum 101 imsi "001010123456789" apn "internet" rat 6 wait 4ms
    mme_s11 <- sgw_s11 gtpc createsessionresponse teid 0x10012002 seqnum 101 cause 16 addr 100.64.10.25
}
```

### Automatic Wire Calculations

- Defaults to UDP destination port `2123` (swapping ports automatically on `<-` responses) and computes the 16-bit GTPv2-C `Message Length` (excluding the initial 4 octets per 3GPP TS 29.274).
- Packs `imsi` decimal strings into TBCD octets (`0xf` filler nibble on odd-length IMSIs) and encodes `apn` labels in RFC 1035 length-prefixed wire format.
- Clears the `T` flag (8-byte header without TEID) on `echorequest` / `echoreply` and sets `T=1` (12-byte header with 32-bit `teid`) on session/bearer messages.

## Practical Lab Example: 4G LTE S11 Session Setup, S1-U Bearer Handshake & Session Teardown (`gtpc_s11_bearer_lifecycle.frasm`)

Simulates an MME and Serving Gateway running an Echo liveness check, establishing an E-UTRAN (`rat 6`) subscriber session (`cause 16` Request Accepted), modifying the bearer after eNodeB S1-U setup, and tearing down the session.

```frameassembly
host mme_01 { ip 172.22.11.5 }
host sgw_01 { ip 172.22.11.50 }

compile {
    // 1. S11 GTPv2-C Path Management Echo (T-bit = 0)
    mme_01 -> sgw_01 gtpc echorequest seqnum 1 recovery 14 wait 2ms
    mme_01 <- sgw_01 gtpc echoreply seqnum 1 recovery 8 wait 5ms
    // 2. Initial Attach: Create Session Request (initial TEID 0) & Accepted Response (cause 16)
    mme_01 -> sgw_01 gtpc createsessionrequest teid 0x00000000 seqnum 2 imsi "310260987654321" apn "ims" rat 6 dscp 46 wait 6ms
    mme_01 <- sgw_01 gtpc createsessionresponse teid 0x00a10001 seqnum 2 cause 16 addr 10.96.0.42 dscp 46 wait 4ms
    // 3. Modify Bearer after eNodeB allocates S1-U downlink TEID
    mme_01 -> sgw_01 gtpc modifybearerrequest teid 0x00b20001 seqnum 3 rat 6 wait 3ms
    mme_01 <- sgw_01 gtpc modifybearerresponse teid 0x00a10001 seqnum 3 cause 16 wait 20ms
    // 4. Detach: Delete Session Request / Response
    mme_01 -> sgw_01 gtpc deletesessionrequest teid 0x00b20001 seqnum 4 wait 3ms
    mme_01 <- sgw_01 gtpc deletesessionresponse teid 0x00a10001 seqnum 4 cause 16
}
```

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

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

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

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

## All `gtpc` Keywords & Header Options

Packet line pattern: `src_host -> dst_host gtpc [echorequest | echoreply | echoresponse | createsessionrequest | createsessionresponse | modifybearerrequest | modifybearerresponse | deletesessionrequest | deletesessionresponse | msgtype "<text>"] [teid <number>] [seqnum <0..16777215>] [cause <0..255>] [imsi "<text>"] [apn "<text>"] [rat <0..255>] [recovery <0..255>] [addr <ip>]`

| Keyword / Option | Accepted Value | Default | Effect on Packet |
| --- | --- | --- | --- |
| `echorequest \| echoreply \| echoresponse \| createsessionrequest \| createsessionresponse \| modifybearerrequest \| modifybearerresponse \| deletesessionrequest \| deletesessionresponse \| msgtype "<text>"` | 3GPP TS 29.274 Message Type keyword or name | `echorequest (1)` | Sets byte 1 of the GTPv2-C header (`1` Echo Req, `2` Echo Resp, `32`/`33` Create Session, `34`/`35` Modify Bearer, `36`/`37` Delete Session). |
| `teid <number>` | 32-bit Tunnel Endpoint Identifier | `0` | Writes bytes 4..7 of the GTPv2-C header when `T=1` (`0x00000000` on initial `createsessionrequest`). |
| `seqnum <0..16777215>` | 24-bit GTPv2-C Sequence Number | `1` | Writes the big-endian 3-byte transaction Sequence Number matching requests to responses. |
| `cause <0..255>` | 8-bit GTPv2 Cause value (IE Type 2) | `16 (Request accepted) on responses` | Appends the Cause TLV IE (`16` Request accepted, `64` Context not found, `72` No resources available). |
| `imsi "<text>"` | 15-digit International Mobile Subscriber Identity string (IE Type 1) | `Omitted (or default on Create Session)` | Encodes the subscriber IMSI in 3GPP TBCD nibble-swapped format inside IE Type `1`. |
| `apn "<text>"` | Access Point Name string (IE Type 71) | `Omitted` | Appends IE Type `71` (`APN`, e.g. `"internet"` or `"ims.mnc001.mcc001.gprs"`). |
| `rat <0..255>` | 8-bit RAT Type (IE Type 82) | `6 (EUTRAN) when set` | Appends IE Type `82` (`1` UTRAN, `2` GERAN, `3` WLAN, `6` EUTRAN/LTE, `8` NR/5G). |
| `recovery <0..255>` | 8-bit Restart Counter (IE Type 3) | `Omitted` | Appends IE Type `3` (`Recovery` node restart counter) for peer reboot detection. |
| `addr <ip>` | IPv4 or IPv6 subscriber PDN address (IE Type 79 PAA) | `Omitted` | Appends IE Type `79` (`PDN Address Allocation`) with PDN Type `IPv4` (`1`) or `IPv6` (`2`). |

## Cross-Layer Options Valid with `gtpc`

| 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), UDP 2123` | Overrides source or destination port and remembers the port pair automatically on `<-` replies. |
| `transport sctp` | Optional SCTP association transport (IP Proto 132) | `UDP (UDP 2123)` | Carries this Layer 7 PDU inside a stateful RFC 9260 SCTP `DATA` chunk with automatic Verification Tag, TSN tracking, and CRC-32c. |
| `badchecksum \| nochecksum` | L4 checksum fault or UDP zero-checksum flag | `Valid RFC checksum / CRC-32c` | `badchecksum` corrupts the L4 checksum for negative firewall/NIC offload tests; `nochecksum` writes `0x0000` in UDP headers. |

## Common Mistakes & Quick Fixes

### Use gtpc (UDP 2123) for Control Signaling and gtpu (UDP 2152) for User Data

- **Avoid:** `mme -> sgw gtpu createsessionrequest teid 0x100 imsi "001010123456789"`
- **Valid v3.3.0 Syntax:** `mme -> sgw gtpc createsessionrequest teid 0x0 seqnum 1 imsi "001010123456789"`

GTPv2-C (`gtpc`, UDP `2123`) negotiates control-plane tunnels and TEIDs, whereas GTPv1-U (`gtpu`, UDP `2152`) encapsulates subscriber user-plane IP packets (`gpdu`).

### Quote 15-Digit IMSI Values That Begin with Leading Zeros

- **Avoid:** `mme -> sgw gtpc createsessionrequest imsi 001010123456789`
- **Valid v3.3.0 Syntax:** `mme -> sgw gtpc createsessionrequest imsi "001010123456789"`

Test PLMN IMSIs (`00101...`) start with leading zeros that an unquoted integer would strip; pass `imsi` as a double-quoted string so all 15 TBCD digits are preserved.
