# bacnet — UDP 47808 (0xbac0) BMS Who-Is & Property Frames (`bacnet`)

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

- **Full Protocol Name:** BACnet/IP Building Automation & Control Networks
- **Keyword:** `bacnet`
- **OSI Layer & Transport:** Layer 7 · UDP 47808 (0xbac0)
- **Host Endpoint Rule:** Dual-Stack IPv4/IPv6
- **Standards:** ASHRAE 135, ISO 16484-5
- **Wireshark Display Filter:** `bacnet || bvlc || bacapp`
- **Wire Encapsulation:** `Ethernet II -> [802.1Q / QinQ / MPLS] -> IPv4 / IPv6 -> UDP 47808 (0xbac0) -> BACnet/IP`

## Overview

Running on UDP `47808` (`0xbac0` — literally spells `BAC0` in hex!) across hospital chillers and data-center CRAC units, `bacnet` packs BVLC, NPDU, and confirmed/unconfirmed APDUs cleanly.

BACnet/IP (`bacnet`, ASHRAE 135) manages data-center chillers, CRAC cooling units, hospital HVAC controllers, elevators, and access-control panels over **UDP port `47808`** (`0xbac0`). Every BACnet/IP frame stacks three headers: the 4-byte **BVLC** (`0x81` BACnet/IP, Original-Broadcast `0x0b` or Original-Unicast `0x0a` + 16-bit total length), the **NPDU** (`0x01`), and the **APDU** carrying Unconfirmed (`whois`, `iam`) or Confirmed (`readproperty`, `writeproperty`) services.

FrameAssembly encodes context-tagged BACnet Object Identifiers (`objecttype` + 22-bit `objectinstance`), Property Identifiers (`propertyid`, such as `85` for `Present-Value`), and application-tagged values automatically.

## Minimal Quickstart (`bacnet_quickstart.frasm`)

```frameassembly
host bms_srv     { ip 10.80.10.10 }
host chiller_ahu { ip 10.80.10.55 }

compile {
    bms_srv -> chiller_ahu bacnet whois wait 3ms
    bms_srv <- chiller_ahu bacnet iam objecttype 8 objectinstance 1055 wait 3ms
    bms_srv -> chiller_ahu bacnet readproperty invokeid 1 objecttype 0 objectinstance 1 propertyid 85
}
```

### Automatic Wire Calculations

- Defaults to UDP source and destination port `47808` (`0xbac0`) and sets BVLC Function to `0x0b` (`Original-Broadcast-NPDU`) on `whois`/`iam` or `0x0a` (`Original-Unicast-NPDU`) on `readproperty`/`writeproperty`.
- Bit-packs the 10-bit `objecttype` and 22-bit `objectinstance` into the 32-bit ASHRAE 135 `BACnetObjectIdentifier` tag.
- Defaults `propertyid` to `85` (`Present-Value`) and `objecttype` to `0` (`Analog-Input`) when reading sensor points.

## Practical Lab Example: Data-Center CRAC Discovery (`Who-Is`/`I-Am`), Temperature Read & Setpoint Write (`bacnet_dc_cooling_audit.frasm`)

Discovers CRAC device instance `2001`, reads Analog-Input 1 `Present-Value` (`propertyid 85`), and tests an unauthorized `writeproperty` setpoint change.

```frameassembly
host bms_workstation { ip 192.168.90.10 }
host crac_unit_04    { ip 192.168.90.40 }

compile {
    // 1. Device discovery: Who-Is -> I-Am (Object Type 8 = Device, Instance 2001)
    bms_workstation -> crac_unit_04 bacnet whois wait 2ms
    bms_workstation <- crac_unit_04 bacnet iam objecttype 8 objectinstance 2001 wait 5ms
    // 2. Read return-air temperature (Object Type 0 = Analog-Input, Property 85 = Present-Value)
    bms_workstation -> crac_unit_04 bacnet readproperty invokeid 10 objecttype 0 objectinstance 1 propertyid 85 wait 4ms
    // 3. Write chiller setpoint (Object Type 2 = Analog-Value, Property 85 = Present-Value)
    bms_workstation -> crac_unit_04 bacnet writeproperty invokeid 11 objecttype 2 objectinstance 1 propertyid 85 value 24
}
```

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

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

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

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

## All `bacnet` Keywords & Header Options

Packet line pattern: `src_host -> dst_host bacnet [whois | iam | readproperty | writeproperty] [invokeid <0..255>] [objecttype <0..1023>] [objectinstance <number>] [propertyid <number>] [value <number>]`

| Keyword / Option | Accepted Value | Default | Effect on Packet |
| --- | --- | --- | --- |
| `whois \| iam \| readproperty \| writeproperty` | BACnet APDU service selector | `whois` | Selects Unconfirmed `Who-Is` (`8`), Unconfirmed `I-Am` (`0`), Confirmed `ReadProperty` (`12`), or Confirmed `WriteProperty` (`15`). |
| `invokeid <0..255>` | 1-byte Confirmed-Request Invoke ID | `1` | Matches Confirmed `readproperty` / `writeproperty` requests with Complex-ACK / Simple-ACK replies. |
| `objecttype <0..1023> \| objectinstance <number>` | `0` Analog-Input, `1` Analog-Output, `2` Analog-Value, `3` Binary-Input, `8` Device + 22-bit instance | `objecttype 0, objectinstance 1 (or 8 / 1000 on iam)` | Packs the 4-byte `BACnetObjectIdentifier` (`(objecttype << 22) \| objectinstance`). |
| `propertyid <number>` | `85` Present-Value, `77` Object-Name, `28` Description | `85 (Present-Value)` | Encodes Context Tag `[1]` (`Property-Identifier`) in `readproperty` / `writeproperty`. |
| `value <number>` | Unsigned value written on `writeproperty` | `0` | Encodes the `[3]` opening/closing context-tagged `Property-Value` inside `writeproperty`. |

## Cross-Layer Options Valid with `bacnet`

| 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), UDP 47808 (0xbac0)` | Overrides source or destination port and remembers the port pair automatically on `<-` replies. |

## Common Mistakes & Quick Fixes

### Place bacnet Before whois (bacnet whois, Not Standalone whois)

- **Avoid:** `bms_srv -> chiller_ahu whois`
- **Valid v3.2.2 Syntax:** `bms_srv -> chiller_ahu bacnet whois`

Standalone `whois` sends an Internet domain lookup on TCP port `43`; `bacnet whois` sends an ASHRAE 135 BACnet/IP `Who-Is` broadcast on UDP port `47808`.
