# Layer 4 Transport Protocols (TCP, UDP & SCTP)

> **FrameAssembly v3.2.2** · Category 03 (Layer 4 · 3 Protocols) · Canonical URL: https://frameassembly-lang.dev/docs/protocols/categories/l4-transports

Modern stateful NGFWs, Suricata `flow:established` rules, and Zeek stream analyzers silently drop single spoofed TCP payload packets that lack a valid 3-way handshake and matching sequence/acknowledgment progression. FrameAssembly maintains a deterministic per-4-tuple TCP state machine shared across raw `tcp` frames and every TCP-based Layer 7 protocol.

Alongside stateful TCP (with padded RFC 7323 `mss`, `wscale`, `sackperm`, and `tsval`/`tsecr` options plus explicit `seq`/`win`/`rst` chaos overrides), Layer 4 includes bidirectional port-inheriting `udp` datagrams and 4G/5G telecom `sctp` chunks checksummed with reflected Castagnoli CRC-32c.

## Category Wire & Endpoint Rules

- **Shared 4-Tuple TCP State Machine:** Initial `seq` starts at `1000` per direction; `syn` and `fin` advance `seq` by `+1`; payload frames emit `psh ack` and advance `seq` by `+payload_len` while updating peer `acknum`.
- **Bidirectional Port Inheritance:** Setting `srcport` / `dstport` on `a -> b` stores the tuple so `a <- b` replies swap source and destination ports automatically without retyping them.
- **9-Protocol `transport tcp|udp` Whitelist:** Cross-layer `transport tcp` or `transport udp` is valid strictly on `dns`, `sip`, `syslog`, `kerberos`, `memcached`, `dnp3`, `modbus`, `nfs`, and `ethernetip`.
- **Reflected Castagnoli CRC-32c on SCTP:** `sctp` computes hardware-table RFC 9260 Castagnoli CRC-32c (never Adler-32) across `init`, `initack`, `cookieecho`, `cookieack`, `data`, and `heartbeat` chunks.

## Multi-Protocol Example: Stateful TCP RFC 7323 Stream, UDP Probe & Telecom SCTP NGAP (`l4_tcp_udp_sctp.frasm`)

Demonstrates a 3-way TCP handshake with RFC 7323 window scaling/SACK/timestamps followed by payload delivery, a custom UDP health datagram, and an SCTP port 38412 5G NGAP INIT + DATA exchange.

```frameassembly
host client { ip 10.50.1.10 }
host server { ip 10.50.1.20 }

compile {
    client -> server tcp syn srcport 53100 dstport 8443 win 65535 mss 1460 wscale 7 sackperm tsval 1000 tsecr 0 wait 1ms
    client <- server tcp syn ack win 65535 mss 1460 wscale 7 sackperm tsval 2000 tsecr 1000 wait 1ms
    client -> server tcp ack tsval 1001 tsecr 2000 wait 1ms
    client -> server tcp psh ack payload "PING-STREAM
" wait 2ms
    client <- server tcp ack wait 2ms
    client -> server udp srcport 54000 dstport 9125 payload "udp-health-ok" wait 2ms
    client -> server sctp init srcport 38412 dstport 38412 vtag 0 tsn 100 wait 1ms
    client <- server sctp initack vtag 0x22334455 tsn 500 wait 1ms
    client -> server sctp data vtag 0x22334455 tsn 100 stream 1 ssn 0 ppid 60 hexpayload "0015000800000000"
}
```

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

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

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

# 4. Verify dissected protocol fields in tshark
tshark -r l4_tcp_udp_sctp.pcap -Y "tcp || udp || sctp"
```

- **Wireshark Display Filter:** `tcp || udp || sctp`

## Protocols in Category 03 (3 Native Builders)

| # | Keyword | Protocol | Default Port / Proto | Endpoint Rule | Wireshark Filter | Spec URL |
| --- | --- | --- | --- | --- | --- | --- |
| 29 | `tcp` | Transmission Control Protocol & RFC 7323 Options | `IP Proto 6 (Default dstport 80)` | Dual-Stack IPv4/IPv6 | `tcp` | https://frameassembly-lang.dev/docs/protocols/tcp |
| 30 | `udp` | User Datagram Protocol | `IP Proto 17 (Default dstport 80)` | Dual-Stack IPv4/IPv6 | `udp` | https://frameassembly-lang.dev/docs/protocols/udp |
| 31 | `sctp` | Stream Control Transmission Protocol | `IP Proto 132 (38412 NGAP / 36412 S1AP)` | Dual-Stack IPv4/IPv6 | `sctp` | https://frameassembly-lang.dev/docs/protocols/sctp |
