# diameter — 4G EPC S6a/Gx/Gy AVPs over TCP or SCTP 3868 (`diameter`)

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

- **Full Protocol Name:** Diameter Base, Credit-Control & 3GPP S6a/Gx/Gy AAA Signaling
- **Keyword:** `diameter`
- **OSI Layer & Transport:** Layer 7 · TCP 3868 (or SCTP 3868 / PPID 46 via transport sctp)
- **Host Endpoint Rule:** Dual-Stack IPv4/IPv6
- **Standards:** RFC 6733, RFC 8506, 3GPP TS 29.272
- **Wireshark Display Filter:** `diameter`
- **Wire Encapsulation:** `Ethernet II -> [802.1Q / QinQ / MPLS] -> IPv4 / IPv6 -> TCP 3868 (or SCTP 3868 / PPID 46 via transport sctp) -> Diameter (RFC 6733)`

## Overview

4G LTE HSS and PCRF billing diameter peers are notoriously picky about 32-bit AVP padding — `diameter` aligns `Origin-Host`, `Origin-Realm`, `Session-Id`, and `Result-Code` AVPs over TCP `3868` or `transport sctp` (PPID `46`).

Diameter (`diameter`, RFC 6733 / RFC 8506 / 3GPP TS 29.272) is the telecom AAA, HSS subscriber authentication (`S6a`), policy control (`Gx` PCRF), online charging (`Gy` OCS), and IMS (`Cx`/`Sh`) signaling backbone on **TCP or SCTP port `3868`**. Every Diameter message starts with a 20-byte header (`Version = 1`, 24-bit `Message Length`, `Command Flags` `R`/`P`/`E`/`T`, 24-bit `Command-Code`, 32-bit `Application-ID`, `Hop-by-Hop ID`, and `End-to-End ID`) followed by **32-bit word-aligned Attribute-Value Pairs (AVPs)**.

FrameAssembly aligns and zero-pads every AVP (`Origin-Host` `264`, `Origin-Realm` `296`, `Destination-Host` `293`, `Destination-Realm` `283`, `Session-Id` `263`, `Result-Code` `268`, `User-Name` `1`) to 4-byte boundaries automatically and sets **SCTP Payload Protocol Identifier `46` (`Diameter`)** whenever `transport sctp` is appended.

## Minimal Quickstart (`diameter_quickstart.frasm`)

```frameassembly
host mme_s6a { ip 10.88.6.10 }
host hss_s6a { ip 10.88.6.20 }

compile {
    mme_s6a -> hss_s6a diameter cer hopid 0x1001 endid 0x2001 originhost "mme01.epc.internal" realm "epc.internal" transport sctp wait 3ms
    mme_s6a <- hss_s6a diameter cea hopid 0x1001 endid 0x2001 originhost "hss01.epc.internal" realm "epc.internal" resultcode 2001 transport sctp
}
```

### Automatic Wire Calculations

- Defaults to stateful TCP port `3868`, or switches to SCTP port `3868` (`DATA` chunk `PPID = 46`, Castagnoli CRC-32c) when `transport sctp` is added.
- Computes the exact 24-bit `Message Length` in bytes 1..3 and pads every variable-length UTF-8/DiameterIdentity AVP to 4-octet boundaries while recording unpadded length in the 24-bit `AVP Length` field.
- Sets the `R` (`0x80` Request) flag automatically on `cer`/`dwr`/`ccr`/`request` and clears `R` while appending `Result-Code` (`2001` `DIAMETER_SUCCESS`) on `cea`/`dwa`/`cca`/`response`.

## Practical Lab Example: SCTP 3868 Diameter Peer Bring-Up (CER/CEA), Credit-Control (CCR/CCA) & Watchdog (`diameter_gy_charging_sctp.frasm`)

Establishes an SCTP Diameter association between a PGW/SMF and Online Charging System (OCS), exchanges `CER`/`CEA`, authorizes subscriber quota via `CCR`/`CCA` (`appid 4`), and verifies liveness via `DWR`/`DWA`.

```frameassembly
host pgw_gy { ip 172.28.10.15 }
host ocs_gy { ip 172.28.10.80 }

compile {
    // 1. Capabilities-Exchange (Code 257, AppID 0) over SCTP 3868 (PPID 46)
    pgw_gy -> ocs_gy diameter cer hopid 0x00010001 endid 0x99000001 originhost "pgw01.epc.internal" realm "epc.internal" transport sctp wait 2ms
    pgw_gy <- ocs_gy diameter cea hopid 0x00010001 endid 0x99000001 originhost "ocs01.epc.internal" realm "epc.internal" resultcode 2001 transport sctp wait 5ms
    // 2. RFC 8506 Credit-Control Request / Answer (Code 272, AppID 4 = Diameter Credit Control)
    pgw_gy -> ocs_gy diameter ccr appid 4 hopid 0x00010002 endid 0x99000002 sessionid "pgw01.epc.internal;170001;1" originhost "pgw01.epc.internal" realm "epc.internal" dstrealm "epc.internal" user "001010123456789" transport sctp wait 4ms
    pgw_gy <- ocs_gy diameter cca appid 4 hopid 0x00010002 endid 0x99000002 sessionid "pgw01.epc.internal;170001;1" originhost "ocs01.epc.internal" realm "epc.internal" resultcode 2001 transport sctp wait 15ms
    // 3. Device-Watchdog Heartbeat (Code 280)
    pgw_gy -> ocs_gy diameter dwr hopid 0x00010003 endid 0x99000003 originhost "pgw01.epc.internal" realm "epc.internal" transport sctp wait 2ms
    pgw_gy <- ocs_gy diameter dwa hopid 0x00010003 endid 0x99000003 originhost "ocs01.epc.internal" realm "epc.internal" resultcode 2001 transport sctp
}
```

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

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

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

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

## All `diameter` Keywords & Header Options

Packet line pattern: `src_host -> dst_host diameter [cer | cea | dwr | dwa | ccr | cca] [request | response] [cmd|command "<text_or_num>"] [flags <0..255>] [appid <number>] [hopid <number>] [endid <number>] [originhost "<text>"] [realm "<text>"] [dsthost "<text>"] [dstrealm "<text>"] [sessionid "<text>"] [resultcode <number>] [user "<text>"] [transport sctp]`

| Keyword / Option | Accepted Value | Default | Effect on Packet |
| --- | --- | --- | --- |
| `cer \| cea \| dwr \| dwa \| ccr \| cca \| request \| response \| cmd\|command "<text_or_num>"` | Diameter Command shorthand or 24-bit Command-Code (e.g. 318 ULR/ULA) | `cer (257 Request)` | Selects `257` Capabilities-Exchange (`cer`/`cea`), `280` Device-Watchdog (`dwr`/`dwa`), `272` Credit-Control (`ccr`/`cca`), or any custom 3GPP Command-Code (e.g. `cmd 316` S6a ULR, `cmd 318` AIA). |
| `appid <number>` | 32-bit Diameter Application-ID | `0 (Base) or 4 (on CCR/CCA)` | Writes bytes 8..11 (`0` Common, `4` Credit-Control, `16777251` 3GPP S6a, `16777238` 3GPP Gx). |
| `flags <0..255>` | 8-bit Command Flags bitmask (`0x80` R, `0x40` P, `0x20` E, `0x10` T) | `0x80 on Request, 0x00 on Answer` | Overrides byte 4 of the Diameter header. |
| `hopid <number> \| endid <number>` | 32-bit Hop-by-Hop and End-to-End Identifiers | `Auto-populated non-zero IDs` | Writes bytes 12..19 used by peers and DRAs to match Answers to Requests. |
| `originhost "<text>" \| realm "<text>"` | DiameterIdentity strings (AVPs 264 & 296) | `"client.example.com" / "example.com"` | Encodes mandatory `Origin-Host` (AVP `264`, M-bit `0x40`) and `Origin-Realm` (AVP `296`, M-bit `0x40`). |
| `dsthost "<text>" \| dstrealm "<text>"` | DiameterIdentity strings (AVPs 293 & 283) | `Omitted` | Appends `Destination-Host` (AVP `293`) and `Destination-Realm` (AVP `283`) for DRA realm routing. |
| `sessionid "<text>" \| user "<text>"` | UTF8String AVPs (AVP 263 Session-Id & AVP 1 User-Name) | `Omitted` | Appends `Session-Id` (`263`) and `User-Name` (`1`, IMSI/NAI) AVPs. |
| `resultcode <number>` | 32-bit Unsigned32 Result-Code (AVP 268) | `2001 (DIAMETER_SUCCESS) on Answers` | Appends AVP `268` (`2001` Success, `3002` Unable to Deliver, `5001` AVP Unsupported, `5030` User Unknown). |
| `transport sctp` | Optional SCTP transport selector | `TCP 3868` | Encapsulates the Diameter PDU inside SCTP port `3868` with `PPID = 46` instead of TCP. |

## Cross-Layer Options Valid with `diameter`

| 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 3868 (or SCTP 3868 / PPID 46 via transport sctp)` | Overrides source or destination port and remembers the port pair automatically on `<-` replies. |
| `transport sctp` | Optional SCTP association transport (IP Proto 132) | `TCP (TCP 3868 (or SCTP 3868 / PPID 46 via transport sctp))` | Carries this Layer 7 PDU inside a stateful RFC 9260 SCTP `DATA` chunk with automatic Verification Tag, TSN tracking, and CRC-32c. |
| `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

### Diameter Uses TCP or SCTP Only (Never transport udp)

- **Avoid:** `mme -> hss diameter cer originhost "mme.epc" realm "epc" transport udp`
- **Valid v3.3.0 Syntax:** `mme -> hss diameter cer originhost "mme.epc" realm "epc" transport sctp`

RFC 6733 specifies reliable stream/message transports (`TCP` default or `transport sctp`, never connectionless UDP like legacy RADIUS).

### Preserve Matching hopid and endid on Answer Frames

- **Valid v3.3.0 Syntax:** `c -> s diameter dwr hopid 101 endid 501
c <- s diameter dwa hopid 101 endid 501 resultcode 2001`

Wireshark’s `diameter` response-time analyzer pairs `CEA`/`DWA`/`CCA` answers with requests using matching `Hop-by-Hop` and `End-to-End` IDs.
