# iec104 — Port 2404 IEC 60870-5-104 Substation I/S/U APDUs (`iec104`)

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

- **Full Protocol Name:** IEC 60870-5-104 Substation Telecontrol Over TCP/IP
- **Keyword:** `iec104`
- **OSI Layer & Transport:** Layer 7 · TCP 2404
- **Host Endpoint Rule:** Dual-Stack IPv4/IPv6
- **Standards:** IEC 60870-5-104, IEC 60870-5-101
- **Wireshark Display Filter:** `iec60870_104`
- **Wire Encapsulation:** `Ethernet II -> [802.1Q / QinQ / MPLS] -> IPv4 / IPv6 -> TCP 2404 -> IEC 60870-5-104`

## Overview

European and Asian transmission grids run on TCP `2404` (`0x68` APCI start byte): `iec104` encodes `startdt`/`testfr` U-frames, `sframe` acknowledgments, and IEEE-754 `real` or integer ASDUs (`typeid`, `cot`, `ca`, `ioa`).

IEC 60870-5-104 (`iec104`) is the dominant transmission and distribution power-grid SCADA telecontrol protocol across Europe, Asia, and Latin America (and notorious from the 2016 **Industroyer / CrashOverride** Ukraine substation incident) on **TCP port `2404`**. Every APDU starts with **`0x68`**, an 8-bit `APDU Length`, and 4 **Application Protocol Control Information (APCI)** control octets encoding **U-Frames** (`STARTDT act/con`, `STOPDT`, `TESTFR`), **S-Frames** (Supervisory `N(R)` acknowledgments), or **I-Frames** (`N(S)`/`N(R)` sequence numbers + an IEC 60870-5-101 **ASDU**: `TypeID`, `VSQ`, `Cause of Transmission cot`, 16-bit `Common Address ca`, 24-bit `Information Object Address ioa`, and integer or IEEE 754 `real` telemetry/command values).

FrameAssembly encodes shifted little-endian `(seq << 1)` I/S-frame control fields, 16-bit `ca`, 24-bit `ioa`, and IEEE 754 single-precision floats (`real 49.98` -> `TypeID 13` `M_ME_NC_1` Short Floating Point).

## Minimal Quickstart (`iec104_quickstart.frasm`)

```frameassembly
host scada_cc { ip 10.104.1.10 }
host sub_rtu  { ip 10.104.2.50 }

compile {
    scada_cc -> sub_rtu tcp syn srcport 52404 dstport 2404 wait 1ms
    scada_cc <- sub_rtu tcp syn ack wait 1ms
    scada_cc -> sub_rtu tcp ack wait 1ms
    scada_cc -> sub_rtu iec104 startdt wait 2ms
    scada_cc <- sub_rtu iec104 startdt confirm wait 5ms
    scada_cc <- sub_rtu iec104 txseq 0 rxseq 0 typeid 13 cot 3 ca 1 ioa 1001 real 49.98
}
```

### Automatic Wire Calculations

- Defaults to stateful TCP port `2404` and formats the `0x68` Start Byte + exact `APDU Length` (`4` for U/S-frames, `4 + ASDU_len` for I-frames).
- Encodes 15-bit `txseq` and `rxseq` shifted left by 1 bit (`seq << 1`) in little-endian APCI octets per IEC 60870-5-104 Section 5.1.
- Packs 16-bit little-endian Common Address (`ca`), 24-bit little-endian Information Object Address (`ioa`), Quality Descriptor (`QDS = 0x00` Good), and 32-bit IEEE 754 little-endian floats when `real` is provided.

## Practical Lab Example: Substation Telecontrol Session: U-Frame STARTDT, Spontaneous 50Hz Float Telemetry, S-Frame & Single Command (`iec104_substation_telecontrol.frasm`)

Activates data transfer (`STARTDT act/con`), streams spontaneous grid frequency telemetry (`TypeID 13`, `49.98 Hz`), acknowledges via `S-Frame`, executes a Single Command (`TypeID 45`, `cot 6` -> `cot 7`), and exchanges `TESTFR` keepalives.

```frameassembly
host dispatch_cc { ip 172.26.1.10 }
host bay_rtu_04  { ip 172.26.4.40 }

compile {
    dispatch_cc -> bay_rtu_04 tcp syn srcport 54404 dstport 2404 wait 1ms
    dispatch_cc <- bay_rtu_04 tcp syn ack wait 1ms
    dispatch_cc -> bay_rtu_04 tcp ack wait 1ms
    // 1. U-Frame STARTDT act (0x07) & STARTDT con (0x0b)
    dispatch_cc -> bay_rtu_04 iec104 startdt wait 2ms
    dispatch_cc <- bay_rtu_04 iec104 startdt confirm wait 5ms
    // 2. I-Frame Spontaneous Measured Float (TypeID 13 M_ME_NC_1, COT 3 Spontaneous, IOA 4001 = 49.98 Hz)
    dispatch_cc <- bay_rtu_04 iec104 txseq 0 rxseq 0 typeid 13 cot 3 ca 4 ioa 4001 real 49.98 wait 2ms
    // 3. Supervisory S-Frame acknowledging RTU txseq 0 (rxseq = 1)
    dispatch_cc -> bay_rtu_04 iec104 sframe rxseq 1 wait 10ms
    // 4. Breaker Single Command (TypeID 45 C_SC_NA_1, COT 6 Activation -> COT 7 ActivationCon)
    dispatch_cc -> bay_rtu_04 iec104 txseq 0 rxseq 1 typeid 45 cot 6 ca 4 ioa 6001 value 1 wait 3ms
    dispatch_cc <- bay_rtu_04 iec104 txseq 1 rxseq 1 typeid 45 cot 7 ca 4 ioa 6001 value 1 wait 15ms
    // 5. U-Frame TESTFR act & con keepalive
    dispatch_cc -> bay_rtu_04 iec104 testfr wait 2ms
    dispatch_cc <- bay_rtu_04 iec104 testfr confirm
}
```

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

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

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

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

## All `iec104` Keywords & Header Options

Packet line pattern: `src_host -> dst_host iec104 [startdt | stopdt | testfr | sframe | msgtype "<text>"] [confirm] [txseq <0..32767>] [rxseq <0..32767>] [typeid <0..255>] [cot "<text_or_num>"] [ca <0..65535>] [ioa <0..16777215>] [value <number>] [real <decimal>]`

| Keyword / Option | Accepted Value | Default | Effect on Packet |
| --- | --- | --- | --- |
| `startdt \| stopdt \| testfr \| sframe \| msgtype "<text>" \| [confirm]` | APCI frame format (`U-Frame` STARTDT/STOPDT/TESTFR, `S-Frame`, or default `I-Frame` when ASDU fields are present) | `I-Frame (or U-Frame when startdt/stopdt/testfr is set)` | Selects U-Frame (`0x07`/`0x0b` STARTDT, `0x13`/`0x23` STOPDT, `0x43`/`0x83` TESTFR with `confirm`), S-Frame (`0x01`), or I-Frame (`N(S)`/`N(R)` + ASDU). |
| `txseq <0..32767> \| rxseq <0..32767>` | 15-bit Send `N(S)` & Receive `N(R)` sequence numbers | `txseq 0, rxseq 0` | Encodes `(txseq << 1)` and `(rxseq << 1)` in APCI control octets 1..4. |
| `typeid <0..255>` | 8-bit IEC 60870-5-101/104 Type Identification | `13 (M_ME_NC_1 when real is set) or 1 (M_SP_NA_1)` | Sets ASDU byte 0 (`1` Single-Point, `9` Normalized Meas, `13` Short Float, `36` Float w/ CP56Time2a, `45` Single Cmd, `46` Double Cmd, `100` Interrogation). |
| `cot "<text_or_num>"` | Cause of Transmission (`3` Spont, `6` Act, `7` ActCon, `10` ActTerm, `20` Interrogated) | `3 (Spontaneous)` | Writes the 16-bit Cause of Transmission field in the ASDU header. |
| `ca <0..65535> \| ioa <0..16777215>` | 16-bit Common Address of ASDU & 24-bit Information Object Address | `ca 1, ioa 1` | Writes the 2-byte little-endian station `CA` and 3-byte little-endian data-point `IOA`. |
| `value <number> \| real <decimal>` | Integer state/command byte or IEEE 754 floating-point number (e.g. `49.98`, `-12.5`) | `value 1` | Encodes Single/Double/Measured integer values (`value`) or 4-byte IEEE 754 float + QDS (`real`). |

## Cross-Layer Options Valid with `iec104`

| 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 2404` | Overrides source or destination port and remembers the port pair automatically on `<-` replies. |
| `transport sctp` | Optional SCTP association transport (IP Proto 132) | `TCP (TCP 2404)` | 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

### Combine startdt/testfr with confirm (Not response) for U-Frame Confirmations

- **Avoid:** `cc <- rtu iec104 startdt response`
- **Valid v3.3.0 Syntax:** `cc <- rtu iec104 startdt confirm`

IEC 60870-5-104 U-Frames pair Activation (`act`) with Confirmation (`con`, enabled via the `confirm` flag keyword).

### Do Not Include typeid/ioa on Pure U-Frames If You Want a 6-Byte APCI Packet

- **Avoid:** `cc -> rtu iec104 startdt ca 1 ioa 1001 real 49.98`
- **Valid v3.3.0 Syntax:** `cc -> rtu iec104 startdt
cc <- rtu iec104 typeid 13 cot 3 ca 1 ioa 1001 real 49.98`

Real substations exchange 6-byte U-Frame `STARTDT act` / `STARTDT con` packets first before transmitting ASDU I-Frames (`ca`, `ioa`, `real`).
