# sctp — 4-Way Telecom Handshakes, Multi-Stream & CRC-32c (`sctp`)

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

- **Full Protocol Name:** Stream Control Transmission Protocol
- **Keyword:** `sctp`
- **OSI Layer & Transport:** Layer 4 · IP Proto 132 (38412 NGAP / 36412 S1AP)
- **Host Endpoint Rule:** Dual-Stack IPv4/IPv6
- **Standards:** RFC 9260, RFC 4960, 3GPP TS 38.412
- **Wireshark Display Filter:** `sctp`
- **Wire Encapsulation:** `Ethernet II -> [802.1Q / QinQ / MPLS] -> IPv4 / IPv6 -> SCTP (CRC-32c) (IP Proto 132 (38412 NGAP / 36412 S1AP))`

## Overview

Powering 4G S1AP and 5G NGAP telecom cores on IP Protocol `132`, `sctp` packs 32-bit padded chunks (`init`, `data`, `sack`, `heartbeat`) and computes the infamous reflected Castagnoli **CRC-32c** automatically.

Stream Control Transmission Protocol (`sctp`, IP protocol `132`, RFC 9260) is the message-oriented, multi-stream Layer 4 transport underpinning **5G Core NGAP (`38412`)**, **4G LTE S1AP (`36412`)**, **Diameter (`3868`)**, **SS7 SIGTRAN M3UA (`2905`)**, and WebRTC DataChannels. Every SCTP packet carries a 12-byte Common Header (`Source Port`, `Destination Port`, 32-bit `Verification Tag`, and 32-bit checksum) followed by chunk TLVs.

Unlike TCP and UDP, SCTP mandates the **Castagnoli CRC-32c** polynomial rather than an ones'-complement sum. FrameAssembly computes the exact hardware-table Castagnoli CRC-32c across the Common Header and 4-byte-padded chunks automatically so Wireshark and telecom core stacks validate every packet cleanly.

## Minimal Quickstart (`sctp_quickstart.frasm`)

```frameassembly
host gnb_01 { ip 10.50.1.10 }
host amf_01 { ip 10.50.1.20 }

compile {
    gnb_01 -> amf_01 sctp init srcport 38412 dstport 38412 vtag 0 tsn 1000 wait 1ms
    gnb_01 <- amf_01 sctp initack vtag 0x11223344 tsn 2000 wait 1ms
    gnb_01 -> amf_01 sctp data vtag 0x22334455 tsn 1001 stream 0 ssn 0 ppid 60 hexpayload "0015000900000100"
}
```

### Automatic Wire Calculations

- Computes the 32-bit reflected Castagnoli CRC-32c checksum across the 12-byte SCTP Common Header and all padded chunk bytes.
- Pads chunk payloads to 4-byte alignment per RFC 9260 while recording the exact unpadded length inside the Chunk Length field.
- Inherits bidirectional ports (`38412 <-> 38412`) across `->` and `<-` statements after the initial `init` packet.

## Practical Lab Example: Complete 5G N2 NGAP SCTP 4-Way Handshake, Data Transfer & Heartbeat (`sctp_5g_ngap_association.frasm`)

Models a 5G gNodeB establishing a full 4-way cookie handshake with an AMF on port 38412, transmitting an NGAP Setup Request (`ppid 60`), receiving `sack`, and exchanging `heartbeat` / `heartbeatack`.

```frameassembly
host gnb_du { ip 172.20.10.11 }
host core_amf { ip 172.20.10.50 }

compile {
    // 1. RFC 9260 4-way association handshake (INIT must carry vtag 0)
    gnb_du -> core_amf sctp init srcport 38412 dstport 38412 vtag 0 tsn 1000 dscp 46 wait 1ms
    gnb_du <- core_amf sctp initack vtag 0xa1b2c3d4 tsn 5000 dscp 46 wait 1ms
    gnb_du -> core_amf sctp cookieecho vtag 0x55667788 dscp 46 wait 1ms
    gnb_du <- core_amf sctp cookieack vtag 0xa1b2c3d4 dscp 46 wait 2ms
    // 2. NGAP payload over Stream 0 (PPID 60 = 3GPP NGAP) + SACK acknowledgment
    gnb_du -> core_amf sctp data vtag 0x55667788 tsn 1000 stream 0 ssn 0 ppid 60 dscp 46 hexpayload "00150014000002001b00080000f110000001" wait 1ms
    gnb_du <- core_amf sctp sack vtag 0xa1b2c3d4 tsn 1000 dscp 46 wait 5ms
    // 3. Path liveness probe
    gnb_du -> core_amf sctp heartbeat vtag 0x55667788 wait 1ms
    gnb_du <- core_amf sctp heartbeatack vtag 0xa1b2c3d4
}
```

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

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

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

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

## All `sctp` Keywords & Header Options

Packet line pattern: `src_host -> dst_host sctp [chunk <type> | data | init | initack | sack | heartbeat | heartbeatack | abort | shutdown | shutdownack | cookieecho | cookieack] [vtag <number>] [tsn <number>] [stream <0..65535>] [ssn <0..65535>] [ppid <number>]`

| Keyword / Option | Accepted Value | Default | Effect on Packet |
| --- | --- | --- | --- |
| `data \| init \| initack \| sack \| heartbeat \| heartbeatack \| abort \| shutdown \| shutdownack \| cookieecho \| cookieack` | Bare chunk keyword (or `chunk <type>`) | `data (Chunk Type 0)` | Selects the RFC 9260 SCTP chunk type (`0`=DATA, `1`=INIT, `2`=INIT_ACK, `3`=SACK, `4`=HEARTBEAT, `5`=HEARTBEAT_ACK, `6`=ABORT, `7`=SHUTDOWN, `8`=SHUTDOWN_ACK, `10`=COOKIE_ECHO, `11`=COOKIE_ACK). |
| `vtag <number>` | Whole number | `0 on init, 1 otherwise` | Sets the 32-bit Verification Tag in bytes 4..7 of the SCTP Common Header (`0` mandatory on `init`). |
| `tsn <number>` | Whole number | `1` | Sets Initial TSN in `init`/`initack`, Cumulative TSN Ack in `sack`, or chunk TSN in `data`. |
| `stream <0..65535>` | Whole number (0..65535) | `0` | Sets the Stream Identifier S field inside `data` chunks. |
| `ssn <0..65535>` | Whole number (0..65535) | `0` | Sets the 16-bit Stream Sequence Number inside `data` chunks. |
| `ppid <number>` | 32-bit IANA Payload Protocol Identifier | `0` | Sets the big-endian Payload Protocol ID in `data` chunks (`60`=NGAP, `18`=S1AP, `46`=Diameter, `3`=M3UA) so Wireshark auto-dissects upper layers. |

## Cross-Layer Options Valid with `sctp`

| 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), IP Proto 132 (38412 NGAP / 36412 S1AP)` | Overrides source or destination port and remembers the port pair automatically on `<-` replies. |

## Common Mistakes & Quick Fixes

### Chunk Names Have Zero Underscores (initack, cookieecho, heartbeatack)

- **Avoid:** `gnb_du <- core_amf sctp init_ack vtag 0x11223344`
- **Valid v3.2.2 Syntax:** `gnb_du <- core_amf sctp initack vtag 0x11223344`

All SCTP chunk keywords follow FrameAssembly's `[a-z0-9]+` rule: `initack`, `cookieecho`, `cookieack`, `heartbeatack`, and `shutdownack`.

### Use tsn Instead of TCP seq on SCTP Frames

- **Avoid:** `gnb_du -> core_amf sctp data seq 1001 ppid 60`
- **Valid v3.2.2 Syntax:** `gnb_du -> core_amf sctp data tsn 1001 ppid 60`

`seq` is a TCP-only option and cannot be used on SCTP packets; use `tsn` for Transmission Sequence Numbers and `ssn` for Stream Sequence Numbers.
