# knxnetip — UDP 3671 Smart Building KNXnet/IP Tunneling & cEMI (`knxnetip`)

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

- **Full Protocol Name:** KNXnet/IP Building Automation Tunneling & Group Telegrams
- **Keyword:** `knxnetip`
- **OSI Layer & Transport:** Layer 7 · UDP 3671
- **Host Endpoint Rule:** Dual-Stack IPv4/IPv6
- **Standards:** ISO/IEC 14543-3-10, EN 50090, KNX Spec v2.1 (03/08/02/04)
- **Wireshark Display Filter:** `knxip`
- **Wire Encapsulation:** `Ethernet II -> [802.1Q / QinQ / MPLS] -> IPv4 / IPv6 -> UDP 3671 -> KNXnet/IP (ISO/IEC 14543-3)`

## Overview

Commercial lighting, blinds, and HVAC actuators on smart-building VLANs speak KNXnet/IP (`0x0610` magic) on UDP `3671` — `knxnetip` encodes `connect`/`connack` channels and cEMI `read`/`write` telegrams between individual (`src`) and group (`dst`) addresses.

KNXnet/IP (`knxnetip`, ISO/IEC 14543-3 / EN 50090) is the international **smart-building, hospital HVAC, lighting, blind/access actuator, and data-center environmental BMS** standard on **UDP port `3671`**. Every KNXnet/IP frame starts with a **6-byte Header** (`Header Length = 0x06`, `Protocol Version = 0x10`, `16-bit Service Type Identifier` such as `0x0205` `CONNECT_REQUEST`, `0x0206` `CONNECT_RESPONSE`, `0x0209` `DISCONNECT_REQUEST`, `0x0420` `TUNNELLING_REQUEST`, `0x0421` `TUNNELLING_ACK`, or `0x0530` `ROUTING_INDICATION`, and `16-bit Total Length`) followed by HPAI endpoints, 4-byte Connection Headers (`channel`, `seqnum`, `status`), and **Common External Message Interface (`cEMI` `0x11` `L_Data.req`)** KNX telegrams (`16-bit Individual Source Address src`, `16-bit Group Destination Address dst`, `GroupValueRead` `0x0000` / `GroupValueWrite` `0x0080 | val`).

FrameAssembly packs exact `06 10` KNXnet/IP headers, HPAI endpoint descriptors, Tunneling Connection Headers, and cEMI group-address telegrams.

## Minimal Quickstart (`knxnetip_quickstart.frasm`)

```frameassembly
host bms_server { ip 192.168.50.10 }
host knx_gw     { ip 192.168.50.200 }

compile {
    bms_server -> knx_gw knxnetip connect wait 2ms
    bms_server <- knx_gw knxnetip connack channel 1 status 0 wait 4ms
    bms_server -> knx_gw knxnetip service "tunnelreq" channel 1 seqnum 0 src 0x1101 dst 0x0901 write value 1 wait 2ms
    bms_server <- knx_gw knxnetip service "tunnelack" channel 1 seqnum 0 status 0
}
```

### Automatic Wire Calculations

- Defaults to UDP destination port `3671`, writes the `06 10` (`Header Len 6, KNXnet/IP v1.0`) preamble, and computes the big-endian 16-bit `Total Length` automatically.
- Formats dual 8-byte IPv4 UDP `HPAI` (`Host Protocol Address Information`) + 4-byte `CRI` (`Tunnel Connection`) descriptors on `connect` (`0x0205`) and `connack` (`0x0206`).
- Constructs complete 11+-byte **cEMI `L_Data.req` (`0x11`)** payloads inside `TUNNELLING_REQUEST` (`0x0420`) and `ROUTING_INDICATION` (`0x0530`) using 16-bit KNX physical `src` (`Area.Line.Device`) and group `dst` (`Main/Middle/Sub`) addresses.

## Practical Lab Example: Hospital Lighting & HVAC KNXnet/IP Tunneling Session: Connect, GroupValueWrite, GroupValueRead & Disconnect (`knxnetip_bms_tunneling.frasm`)

Opens Tunneling Channel `7` on a KNX/IP interface (`UDP 3671`), switches Zone 1 lighting ON (`GroupValueWrite` `value 1` to `1/1/1` = `0x0901`), reads room temperature (`GroupValueRead` to `2/0/10` = `0x100a`), and disconnects cleanly.

```frameassembly
host bms_ctrl  { ip 10.50.36.10 }
host knx_ip_if { ip 10.50.36.71 }

compile {
    // 1. CONNECT_REQUEST (0x0205) -> CONNECT_RESPONSE (0x0206) assigning Channel 7, Status E_NO_ERROR (0x00)
    bms_ctrl -> knx_ip_if knxnetip connect wait 2ms
    bms_ctrl <- knx_ip_if knxnetip connack channel 7 status 0 wait 5ms
    // 2. TUNNELLING_REQUEST (0x0420) cEMI GroupValueWrite: 1.1.1 (0x1101) -> 1/1/1 (0x0901) = 1 (ON)
    bms_ctrl -> knx_ip_if knxnetip service 0x0420 channel 7 seqnum 0 src 0x1101 dst 0x0901 write value 1 wait 2ms
    bms_ctrl <- knx_ip_if knxnetip service 0x0421 channel 7 seqnum 0 status 0 wait 10ms
    // 3. TUNNELLING_REQUEST (0x0420) cEMI GroupValueRead: 1.1.1 (0x1101) -> 2/0/10 (0x100a)
    bms_ctrl -> knx_ip_if knxnetip service 0x0420 channel 7 seqnum 1 src 0x1101 dst 0x100a read wait 2ms
    bms_ctrl <- knx_ip_if knxnetip service 0x0421 channel 7 seqnum 1 status 0 wait 5ms
    // 4. DISCONNECT_REQUEST (0x0209) -> DISCONNECT_RESPONSE (0x020a)
    bms_ctrl -> knx_ip_if knxnetip disconnect channel 7 wait 2ms
    bms_ctrl <- knx_ip_if knxnetip service 0x020a channel 7 status 0
}
```

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

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

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

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

## All `knxnetip` Keywords & Header Options

Packet line pattern: `src_host -> dst_host knxnetip [connect | connack | disconnect | service "<text_or_num>"] [channel <0..255>] [seqnum <0..255>] [status <0..255>] [src <0..65535>] [dst <0..65535>] [read | write] [value <number>]`

| Keyword / Option | Accepted Value | Default | Effect on Packet |
| --- | --- | --- | --- |
| `connect \| connack \| disconnect \| service "<text_or_num>"` | 16-bit KNXnet/IP Service Type Identifier (`0x0205` Connect, `0x0206` ConnResp, `0x0209` Disconnect, `0x020a` DiscResp, `0x0420` TunnelReq, `0x0421` TunnelAck, `0x0530` RoutingInd) | `service 0x0420 (TUNNELLING_REQUEST) when read/write/src/dst set, else connect (0x0205)` | Sets bytes 2..3 of the 6-byte KNXnet/IP header and builds the corresponding connection or cEMI body. |
| `channel <0..255> \| seqnum <0..255> \| status <0..255>` | 8-bit Communication Channel ID, Tunnel Sequence Counter, and Error Status (`0x00` `E_NO_ERROR`, `0x24` `E_NO_MORE_CONNECTIONS`) | `channel 1, seqnum 0, status 0` | Populates the Connection Response Header or 4-byte Tunneling Connection Header. |
| `src <0..65535> \| dst <0..65535>` | 16-bit KNX Individual Address `(Area<<12 \| Line<<8 \| Dev)` & 3-Level Group Address `(Main<<11 \| Mid<<8 \| Sub)` | `src 0x1101 (1.1.1), dst 0x0901 (1/1/1)` | Writes bytes 4..7 of the embedded cEMI `L_Data` frame. |
| `read \| write \| value <number>` | KNX Application Layer APCI (`0x0000` GroupValueRead, `0x0080` GroupValueWrite) + DPT value | `read (GroupValueRead)` | Encodes 6-bit inline DPT1/DPT5 values (`0x80 \| (val & 0x3f)`) or multi-byte APDU data in the cEMI TPDU. |

## Cross-Layer Options Valid with `knxnetip`

| 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), UDP 3671` | Overrides source or destination port and remembers the port pair automatically on `<-` replies. |
| `transport sctp` | Optional SCTP association transport (IP Proto 132) | `UDP (UDP 3671)` | Carries this Layer 7 PDU inside a stateful RFC 9260 SCTP `DATA` chunk with automatic Verification Tag, TSN tracking, and CRC-32c. |
| `badchecksum \| nochecksum` | L4 checksum fault or UDP zero-checksum flag | `Valid RFC checksum / CRC-32c` | `badchecksum` corrupts the L4 checksum for negative firewall/NIC offload tests; `nochecksum` writes `0x0000` in UDP headers. |

## Common Mistakes & Quick Fixes

### Increment Tunneling seqnum by +1 Only After TUNNELLING_ACK Matches the Current seqnum

- **Valid v3.3.0 Syntax:** `c -> gw knxnetip service 0x0420 channel 1 seqnum 0 write value 1
c <- gw knxnetip service 0x0421 channel 1 seqnum 0 status 0`

In KNXnet/IP Tunneling, `TUNNELLING_ACK (0x0421)` echoes the *exact* `seqnum` of the `TUNNELLING_REQUEST (0x0420)` being acknowledged; the next request increments `seqnum` to `1`.

### Pack 3-Level Group Addresses into 16-Bit Hex (Main<<11 | Middle<<8 | Sub)

- **Valid v3.3.0 Syntax:** `c -> gw knxnetip service 0x0420 src 0x1101 dst 0x0901 write value 1`

KNX group address `1/1/1` encodes on the wire as `(1 << 11) | (1 << 8) | 1 = 0x0901`; individual address `1.1.1` encodes as `(1 << 12) | (1 << 8) | 1 = 0x1101`.
