# gtpu — UDP 2152 4G S1-U & 5G N3 User-Plane Tunnels (TEID + QFI) (`gtpu`)

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

- **Full Protocol Name:** GPRS Tunneling Protocol User Plane (4G S1-U & 5G N3/N9)
- **Keyword:** `gtpu`
- **OSI Layer & Transport:** Layer 3 · UDP 2152
- **Host Endpoint Rule:** Dual-Stack IPv4/IPv6
- **Standards:** 3GPP TS 29.281, 3GPP TS 38.415
- **Wireshark Display Filter:** `gtp`
- **Wire Encapsulation:** `Ethernet II -> [802.1Q / QinQ / MPLS] -> IPv4 / IPv6 -> GTPv1-U (UDP 2152) (UDP 2152)`

## Overview

Every byte your phone downloads over 4G LTE (`S1-U`) or 5G NR (`N3`) rides inside UDP `2152` `gtpu`: pass `qfi 9` on `gtpu gpdu` and the compiler automatically attaches the `0x85` 5G NR PDU Session Container extension header!

GPRS Tunneling Protocol User Plane (`gtpu`, **UDP `2152`**, 3GPP TS 29.281 / TS 38.415) carries subscriber user-plane IP packets across **4G LTE S1-U / S5 / S8 (`eNB <-> SGW <-> PGW`)** and **5G NR N3 / N9 (`gNB <-> I-UPF <-> PSA-UPF`)** mobile core interfaces. Every GTPv1-U packet starts with an 8-byte mandatory header (`Version = 1`, `PT = 1`, `E/S/PN` extension flags, `Message Type`, `Length`, and 32-bit **Tunnel Endpoint Identifier `teid`**), optionally followed by Sequence Number (`seqnum`), N-PDU (`npdu`), and the **5G NR PDU Session Container Extension Header (`0x85`)** carrying the 6-bit **QoS Flow Identifier (`qfi 0..63`)**.

FrameAssembly automatically sets the `E` (`0x04`) Next Extension Header bit and builds the 3GPP TS 38.415 `0x85` PDU Session Container whenever `qfi` is specified, sets `S` (`0x02`) when `seqnum` is present (or on `echorequest` / `echoreply`), and encapsulates a clean inner subscriber IPv4/IPv6 packet on `gpdu` (`Message Type 0xff`).

## Minimal Quickstart (`gtpu_quickstart.frasm`)

```frameassembly
host gnb_du  { ip 172.22.10.11 }
host core_upf { ip 172.22.20.50 }

compile {
    gnb_du -> core_upf gtpu echorequest teid 0 seqnum 1 wait 1ms
    gnb_du <- core_upf gtpu echoreply teid 0 seqnum 1 wait 1ms
    gnb_du -> core_upf gtpu gpdu teid 0x10002001 qfi 9 payload "ue-uplink-pdu"
}
```

### Automatic Wire Calculations

- Defaults UDP `dstport` to `2152` and computes the 16-bit GTPv1-U `Length` field (bytes following the mandatory 8-byte header, including extension headers and inner G-PDU payload).
- Automatically sets `E=1` (`0x34` header flags) and appends the 4-byte aligned **PDU Session Container (`0x85`)** Extension Header whenever `qfi <0..63>` is provided.
- Validates at compile time that `qfi` fits in 6 bits (`0..63`) per 3GPP TS 38.415.

## Practical Lab Example: 5G N3 gNB <-> UPF Path Liveness, VoNR QFI=1 + Data QFI=9 Bearers & Xn Handover End-Marker (`gtpu_5g_n3_upf.frasm`)

Simulates GTP-U Echo path verification (`teid 0`), uplink/downlink 5G NR `gpdu` frames carrying `qfi 9` (data) and `qfi 1` (VoNR voice), and an inter-gNB handover `endmarker`.

```frameassembly
host gnb_cell_01 { ip 10.45.1.11 mac 00:1c:73:45:01:11 }
host upf_psa_01  { ip 10.45.9.50 mac 00:1c:73:45:09:50 }

compile {
    // 1. 3GPP TS 29.281 Path Management Echo Request / Reply (TEID must be 0)
    gnb_cell_01 -> upf_psa_01 gtpu echorequest teid 0 seqnum 100 wait 1ms
    gnb_cell_01 <- upf_psa_01 gtpu echoreply teid 0 seqnum 100 wait 2ms
    // 2. 5G N3 Uplink & Downlink G-PDUs with PDU Session Container (0x85) QFI=9 & VoNR QFI=1
    gnb_cell_01 -> upf_psa_01 gtpu gpdu teid 0x20000101 qfi 9 dscp 18 payload "ue-https-uplink" wait 1ms
    gnb_cell_01 <- upf_psa_01 gtpu gpdu teid 0x10000101 qfi 9 dscp 18 payload "ue-https-downlink" wait 1ms
    gnb_cell_01 -> upf_psa_01 gtpu gpdu teid 0x20000101 qfi 1 seqnum 101 dscp 46 payload "ue-vonr-rtp-frame" wait 2ms
    // 3. UPF sends End Marker (0xfe) toward source gNB during Xn handover
    gnb_cell_01 <- upf_psa_01 gtpu endmarker teid 0x10000101
}
```

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

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

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

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

## All `gtpu` Keywords & Header Options

Packet line pattern: `src_host -> dst_host gtpu [gpdu | echorequest | echoreply | errorindication | endmarker] [teid <number>] [flags <0..255>] [seqnum <0..65535>] [npdu <0..255>] [qfi <0..63>]`

| Keyword / Option | Accepted Value | Default | Effect on Packet |
| --- | --- | --- | --- |
| `gpdu \| echorequest \| echoreply \| errorindication \| endmarker` | GTPv1-U message type keyword | `gpdu (0xff)` | Selects 3GPP TS 29.281 Message Type (`1`=Echo Request, `2`=Echo Response, `26`=Error Indication, `254`=End Marker, `255`=T-PDU / G-PDU). |
| `teid <number>` | 32-bit Tunnel Endpoint Identifier | `0 on echo, 1 on gpdu` | Writes the big-endian 32-bit TEID in bytes 4..7 of the GTPv1-U header. |
| `qfi <0..63>` | 6-bit 5G QoS Flow Identifier (0..63) | `Omitted (4G S1-U mode)` | Sets `E=1` and appends Next Extension Header `0x85` (5G NR PDU Session Container) carrying the 6-bit QFI. |
| `seqnum <0..65535> \| npdu <0..255>` | 16-bit GTP-U Sequence Number & 8-bit N-PDU Number | `Omitted on gpdu (seqnum 1 on echo)` | Enables the 4-byte optional GTPv1-U header extension (`S=1` / `PN=1`) and writes Sequence / N-PDU fields. |
| `flags <0..255>` | 8-bit GTPv1-U header byte 0 override | `0x30 (or 0x32/0x34 with seqnum/qfi)` | Overrides Version (`1`), Protocol Type (`PT=1`), and `E/S/PN` flag bits. |

## Cross-Layer Options Valid with `gtpu`

| 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 2152` | Overrides source or destination port and remembers the port pair automatically on `<-` replies. |
| `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

### qfi Is a 6-Bit 5G QoS Flow ID (0..63)

- **Avoid:** `gnb -> upf gtpu gpdu teid 0x1001 qfi 64`
- **Valid v3.3.0 Syntax:** `gnb -> upf gtpu gpdu teid 0x1001 qfi 9`

3GPP TS 38.415 allocates 6 bits (`0..63`) for QoS Flow Identifier inside the PDU Session Container; `qfi > 63` fails compile-time validation.

### GTPv1-U Runs on UDP 2152 (GTPv2-C Control Signaling Uses gtpc on UDP 2123)

- **Avoid:** `gnb -> mme gtpu createsessionrequest teid 0`
- **Valid v3.3.0 Syntax:** `gnb -> upf gtpu gpdu teid 0x10002001 qfi 9`

Use `gtpu` (UDP `2152`) for user-plane `gpdu`, `echorequest`, `echoreply`, `errorindication`, and `endmarker`; session creation signaling belongs to control-plane GTPv2-C.
