# Reproduce TCP Retransmissions, Out-of-Order & Zero-Window Stalls

> **FrameAssembly v3.2.2** · Operational Playbook (Routing, VLANs & Overlays) · Canonical URL: https://frameassembly-lang.dev/use-cases/tcp-retransmission-zero-window-pcap

Trigger Wireshark tcp.analysis.fast_retransmission, out_of_order, zero_window, and window_update flags deterministically on demand.

- **Category:** Routing, VLANs & Overlays
- **Capture Filename:** `tcp_anomalies_stall.frasm` (12 Ethernet frames)
- **Protocols Exercised:** `tcp`

## Operational Problem

When an application team blames "random network packet loss" for API timeouts—or you need to test how an L7 proxy, middlebox, or APM agent handles TCP Zero-Window stalls and out-of-order segments—your OS kernel flatly refuses to emit broken TCP sequences on command.

Linux `tc netem` can drop or reorder a random percentage of live packets, which means every capture is non-deterministic and impossible to assert against in a regression suite. Meanwhile, normal user-space sockets cannot advertise `win 0` followed 25ms later by a `win 65535` Window Update.

In FrameAssembly, automatic 4-tuple tracking handles the boring segments while explicit `seq`, `acknum`, and `win` overrides let you inject exact sequence holes, duplicate ACKs, fast retransmissions, Zero-Window probes, and `rst ack` aborts on specific lines.

## Scenario & Topology: Microservice TCP 8443 Sequence Hole, Fast Retransmit & Zero-Window Backpressure

- **Topology:** `App Client (10.44.1.15:54120) <-> Load Balancer / WAN <-> API Server (10.44.2.80:8443)`

Twelve surgical TCP segments reproduce every classic Wireshark Expert Info alert in one trace: (1) a `sackperm` + `wscale 7` 3-way handshake; (2) `CHUNK_01` at `seq 1001` followed immediately by out-of-order `CHUNK_03` at `seq 1033` (skipping `1017..1033`); (3) two server Duplicate ACKs (`acknum 1017`) triggering client Fast Retransmission of `CHUNK_02` at `seq 1017`; and (4) server receive-buffer exhaustion (`win 0`), a 1-byte client Zero-Window Probe, a `win 65535` Window Update, and `rst ack` teardown.

## FrameAssembly Workflow

Emits 12 TCP segments exercising out-of-order delivery, duplicate ACKs, fast retransmission, Zero-Window (`win 0`) + probe + Window Update, and RST.

### Adapt It for Your Network

1. Change app_client, api_server, and `dstport 8443` to match the service tuple your APM sensor or middlebox monitors.
2. Adjust `seq` offsets by exact payload byte lengths (16 bytes per `CHUNK_*` in this template) to move or widen the sequence hole.
3. Compile -> inspect `tcp.analysis.flags` in Wireshark or `tshark` -> feed into Suricata stream-reassembly or proxy tests.

## Runnable Capture Specification (`tcp_anomalies_stall.frasm`)

```frameassembly
host app_client { ip 10.44.1.15 mac 00:1b:21:44:01:15 }
host api_server { ip 10.44.2.80 mac 00:1c:73:44:02:80 }

flow tcp_retransmit_and_zerowin(c, s) {
    // 1. 3-way handshake negotiating MSS 1460, Window Scale 7, and SACK Permitted
    c -> s tcp syn srcport 54120 dstport 8443 seq 1000 win 64240 mss 1460 wscale 7 sackperm wait 1ms
    c <- s tcp syn ack seq 5000 acknum 1001 win 65535 mss 1460 wscale 7 sackperm wait 1ms
    c -> s tcp ack seq 1001 acknum 5001 win 64240 wait 1ms

    // 2. CHUNK_01 (seq 1001..1017), then CHUNK_03 arrives out-of-order at seq 1033
    c -> s tcp psh ack seq 1001 acknum 5001 win 64240 payload "CHUNK_01_16BYTES" wait 2ms
    c -> s tcp psh ack seq 1033 acknum 5001 win 64240 payload "CHUNK_03_OUT_ORD" wait 1ms

    // 3. Server sends 2 Duplicate ACKs for 1017 -> Client fast-retransmits missing CHUNK_02
    c <- s tcp ack seq 5001 acknum 1017 win 65535 wait 1ms
    c <- s tcp ack seq 5001 acknum 1017 win 65535 wait 1ms
    c -> s tcp psh ack seq 1017 acknum 5001 win 64240 payload "CHUNK_02_RETRANS" wait 2ms

    // 4. Server buffer fills (ZeroWindow win 0) -> Client 1B probe -> Window Update -> RST
    c <- s tcp ack seq 5001 acknum 1049 win 0 wait 25ms
    c -> s tcp ack seq 1049 acknum 5001 win 64240 payload "Z" wait 5ms
    c <- s tcp ack seq 5001 acknum 1050 win 65535 wait 2ms
    c <- s tcp rst ack seq 5001 acknum 1050 win 0 wait 1ms
}

compile {
    tcp_retransmit_and_zerowin(app_client, api_server)
}
```

## Equivalent CLI Commands

```bash
# 1. Preview the 12-frame TCP retransmission & Zero-Window timeline
frameassembly show tcp_anomalies_stall.frasm

# 2. Compile to .pcap and verify Wireshark tcp.analysis flags in tshark
frameassembly tcp_anomalies_stall.frasm tcp_anomalies_stall.pcap
tshark -r tcp_anomalies_stall.pcap -Y "tcp.analysis.flags"

# 3. Feed into Suricata stream engine or APM / middlebox parser tests
suricata -r tcp_anomalies_stall.pcap -k none -l ./suricata_tcp/
```

## Protocol Reference Links

- [`tcp` — Transmission Control Protocol & RFC 7323 Options](https://frameassembly-lang.dev/docs/protocols/tcp) ([Raw .md](https://frameassembly-lang.dev/docs/protocols/tcp.md))
