# opcua — Port 4840 Purdue-DMZ Binary Chunks (HEL/ACK/OPN/MSG) (`opcua`)

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

- **Full Protocol Name:** OPC Unified Architecture Binary TCP Protocol
- **Keyword:** `opcua`
- **OSI Layer & Transport:** Layer 7 · TCP 4840
- **Host Endpoint Rule:** Dual-Stack IPv4/IPv6
- **Standards:** IEC 62541-6
- **Wireshark Display Filter:** `opcua`
- **Wire Encapsulation:** `Ethernet II -> [802.1Q / QinQ / MPLS] -> IPv4 / IPv6 -> TCP 4840 -> OPC UA`

## Overview

The protocol bridging OT plant networks to IT historians across the industrial DMZ on TCP `4840` — `opcua` writes little-endian `HEL -> ACK -> OPN -> MSG -> CLO` binary chunks with exact message-size fields.

OPC Unified Architecture (`opcua`, IEC 62541-6) is the cross-vendor Industry 4.0 standard used to bridge plant-floor PLCs across the **Purdue Level 3.5 Industrial DMZ** to historians, MES systems, and cloud analytics over **TCP port `4840`**. Every OPC UA Binary message begins with an 8-byte header: a 3-character ASCII `MessageType` (`"HEL"`, `"ACK"`, `"OPN"`, `"MSG"`, `"CLO"`, `"ERR"`), a 1-character `ChunkType` (`"F"` Final, `"C"` Intermediate, `"A"` Abort), and a **32-bit little-endian `MessageSize`**.

FrameAssembly calculates the little-endian `MessageSize` automatically and encodes `"HEL"` (`endpointurl`), `"ACK"` buffer parameters, `"OPN"` secure channel setup, and `"MSG"` service calls carrying `securechannelid`, `tokenid`, `seqnum`, `reqid`, and `nodeid` strings.

## Minimal Quickstart (`opcua_quickstart.frasm`)

```frameassembly
host historian { ip 10.130.35.10 }
host ua_server { ip 10.130.20.48 }

compile {
    historian -> ua_server tcp syn srcport 54840 dstport 4840 wait 1ms
    historian <- ua_server tcp syn ack wait 1ms
    historian -> ua_server tcp ack wait 1ms
    historian -> ua_server opcua msgtype "HEL" chunktype "F" endpointurl "opc.tcp://10.130.20.48:4840" wait 2ms
    historian <- ua_server opcua msgtype "ACK" chunktype "F"
}
```

### Automatic Wire Calculations

- Defaults to TCP destination port `4840` (`psh ack`), `msgtype "HEL"`, `chunktype "F"`, and calculates the 4-byte little-endian `MessageSize` across the entire OPC UA frame.
- Encodes IEC 62541-6 receive/send buffer sizes (`65535`) + length-prefixed UTF-8 `endpointurl` inside `"HEL"` and `"ACK"` frames.
- Encodes `SecureChannelId`, `SecurityTokenId`, `SequenceNumber`, `RequestId`, and ReadService `nodeid` fields inside `"OPN"`, `"MSG"`, and `"CLO"` frames.

## Practical Lab Example: Complete OPC UA Connection (`HEL`/`ACK`), `OpenSecureChannel`, `NodeId` Read & `CLO` (`opcua_idmz_historian.frasm`)

Establishes an OPC UA Binary TCP 4840 session across an Industrial DMZ, opens Secure Channel `1`, reads `"ns=2;s=Turbine1.RPM"` via `"MSG"`, and closes cleanly with `"CLO"`.

```frameassembly
host idmz_hist { ip 172.18.35.20 }
host plant_ua  { ip 192.168.48.10 }

compile {
    idmz_hist -> plant_ua tcp syn srcport 51840 dstport 4840 win 65535 mss 1460 wait 1ms
    idmz_hist <- plant_ua tcp syn ack win 65535 mss 1460 wait 1ms
    idmz_hist -> plant_ua tcp ack wait 1ms
    // 1. Transport Hello / Acknowledge
    idmz_hist -> plant_ua opcua msgtype "HEL" chunktype "F" endpointurl "opc.tcp://192.168.48.10:4840/UA/PlantServer" wait 2ms
    idmz_hist <- plant_ua opcua msgtype "ACK" chunktype "F" wait 2ms
    // 2. OpenSecureChannel (OPN)
    idmz_hist -> plant_ua opcua msgtype "OPN" chunktype "F" securechannelid 0 seqnum 1 reqid 1 wait 2ms
    idmz_hist <- plant_ua opcua msgtype "OPN" chunktype "F" securechannelid 1 tokenid 1 seqnum 1 reqid 1 wait 3ms
    // 3. Service Read Request (MSG) for NodeId + CloseSecureChannel (CLO)
    idmz_hist -> plant_ua opcua msgtype "MSG" chunktype "F" securechannelid 1 tokenid 1 seqnum 2 reqid 2 nodeid "ns=2;s=Turbine1.RPM" wait 4ms
    idmz_hist -> plant_ua opcua msgtype "CLO" chunktype "F" securechannelid 1 tokenid 1 seqnum 3 reqid 3
}
```

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

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

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

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

## All `opcua` Keywords & Header Options

Packet line pattern: `src_host -> dst_host opcua [msgtype "<HEL|ACK|OPN|MSG|CLO|ERR>"] [chunktype "<F|C|A>"] [securechannelid <number>] [tokenid <number>] [seqnum <number>] [reqid <number>] [endpointurl "<text>"] [nodeid "<text>"]`

| Keyword / Option | Accepted Value | Default | Effect on Packet |
| --- | --- | --- | --- |
| `msgtype "<HEL\|ACK\|OPN\|MSG\|CLO\|ERR>"` | 3-character ASCII OPC UA message type | `"HEL"` | Sets bytes 0..2 (`HEL` Hello, `ACK` Acknowledge, `OPN` OpenSecureChannel, `MSG` Service Message, `CLO` Close, `ERR` Error). |
| `chunktype "<F\|C\|A>"` | `"F"` Final chunk, `"C"` Intermediate chunk, `"A"` Abort | `"F"` | Sets byte 3 (`IsFinal`) of the 8-byte OPC UA Binary header. |
| `endpointurl "<text>"` | Quoted `opc.tcp://...` endpoint URL on `"HEL"` | `"opc.tcp://localhost:4840"` | Writes the length-prefixed `EndpointUrl` string inside `"HEL"` messages. |
| `securechannelid <number> \| tokenid <number> \| seqnum <number> \| reqid <number>` | 32-bit little-endian channel, token, sequence, and request IDs | `1` | Populates the Secure Conversation and Sequence headers on `"OPN"`, `"MSG"`, and `"CLO"` frames. |
| `nodeid "<text>"` | Quoted OPC UA NodeId string (`"ns=2;s=Line1.Pressure"`) | `Omitted unless set` | Encodes a String `NodeId` inside `"MSG"` ReadRequest payloads. |

## Cross-Layer Options Valid with `opcua`

| 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), TCP 4840` | Overrides source or destination port and remembers the port pair automatically on `<-` replies. |
| `seq <number> \| acknum <number> \| win\|window <0..65535> \| urgent <0..65535> \| syn \| ack \| psh \| fin \| rst \| urg \| ece \| cwr` | TCP sequence/ack numbers, window size, flags & RFC 7323 options | `psh ack, win 65535, automatic TCP seq/acknum` | Overrides automatic TCP sequence tracking, flags, or RFC 7323 options (`mss`, `wscale`, `sackperm`, `tsval`, `tsecr`). |

## Common Mistakes & Quick Fixes

### Keep msgtype and chunktype Uppercase Inside Quotes ("HEL", "F")

- **Avoid:** `historian -> ua_server opcua hel`
- **Valid v3.2.2 Syntax:** `historian -> ua_server opcua msgtype "HEL" chunktype "F"`

OPC UA specifies message types via `msgtype "<3_CHARS>"` (`"HEL"`, `"ACK"`, `"OPN"`, `"MSG"`, `"CLO"`, `"ERR"`) rather than bare keywords.
