BACnet/IP
BACnet (ASHRAE 135, A Data Communication Protocol for Building Automation and Control Networks) is the protocol of building automation, and annex J carries it over UDP as BACnet/IP. Decoded since 11.6.0 (#159), as part of epic #102 with IEC 60870-5-104 and GOOSE. The module is packet_parser::parse::application::protocols::bacnet: it reads the three layers of a datagram, the BVLC, the NPDU and the APDU, and stops at the service data, which stays tag-encoded bytes.
Detection: its ports, then any UDP port
Two rules of the dispatch table, 27 and 45, share one probe, ProbeId::Bacnet:
#![allow(unused)] fn main() { // among the port rules, right after RADIUS port_rule(ApplicationProtocol::BacnetIp, Guard::Udp, is_bacnet_udp_port, ProbeId::Bacnet), // 47808..=47823 // in the blind cascade, after DNP3 and RADIUS, before Bitcoin rule(ApplicationProtocol::BacnetIp, Guard::Udp, ProbeId::Bacnet), }
is_bacnet_udp_port matches 47808 to 47823, 0xBAC0 to 0xBACF, on either side: 47808 is the IANA bacnet port, and the crate's source ties the fifteen others to the multiple networks of annex J (tshark registers BVLC on 47808 alone). The probe is BacnetPacket::try_from(payload).is_ok(), run on the whole payload, as every probe but the four text ones since 11.7.0 (#185): the BVLC length is compared with the real datagram. It accepts a datagram only if:
- the first byte, the BVLC type, is
0x81; - the function is one of the thirteen of annex J,
0x00to0x0C; - the BVLC length is the length of the datagram, to the byte;
- for the four functions that carry an NPDU, the NPDU has version 1, the addresses and the hop count its control byte announces, and then either a network message or an APDU of type 0 to 7 with the fixed fields of its type.
The crate considers this constrained enough to do without the port, so the same probe runs blind on any UDP datagram that no earlier rule has claimed, and there is no DecodeAsProtocol variant for BACnet: a device on another port is recognized anyway. Both rules are Guard::Udp; the same bytes in a TCP segment never get the label.
Since the two rules share the probe, a datagram on 47808 that fails it is not probed again by the blind rule: it goes on down the table, usually to "Unknown". A refusal never fills corrupted: on UDP a port is a hint, not an announcement, as for PTP. Every datagram of the corpus has 47808 on one side; the blind route is checked by moving frame 3 of iti_bacnet_test.pcap to other ports (tested):
#![allow(unused)] fn main() { #[test] fn bacnet_is_recognized_off_its_ports_and_refused_when_altered() { // Frame 3 moved to UDP 40000 -> 40001: no port rule applies, the // blind rule recognizes the datagram by its content. let mut raw = hex::decode(ITI_READ_PROPERTY_HEX).unwrap(); raw[34..38].copy_from_slice(&[0x9c, 0x40, 0x9c, 0x41]); let flow = parse(LinkType::ETHERNET, &raw).unwrap(); let transport = flow.transport.as_ref().unwrap(); assert_eq!((transport.source_port, transport.destination_port), (Some(40000), Some(40001))); assert_eq!(flow.application.as_ref().unwrap().protocol.label(), "BACnet/IP"); // One altered byte: the decoder refuses the datagram, the label is not // given, and nothing is reported as corrupted. let start = raw.len() - 17; // where the BVLC header starts for (offset, value, error) in [ (0, 0x82, BacnetParseError::InvalidBvlcType(0x82)), (3, 0x12, BacnetParseError::LengthMismatch { declared: 18, actual: 17 }), (4, 0x02, BacnetParseError::UnsupportedNpduVersion(2)), ] { let mut altered = raw.clone(); altered[start + offset] = value; assert_eq!(BacnetPacket::try_from(&altered[start..]), Err(error)); let flow = parse(LinkType::ETHERNET, &altered).unwrap(); assert_eq!(flow.application.as_ref().unwrap().protocol.label(), "Unknown"); assert!(flow.corrupted.is_none()); } } }
The datagram
#![allow(unused)] fn main() { pub struct BacnetPacket<'a> { pub bvlc: BvlcHeader, pub forwarded_from: Option<(Ipv4Addr, u16)>, // Forwarded-NPDU: the original sender pub npdu: Option<Npdu<'a>>, // for the functions that carry one pub body: &'a [u8], // after the header (and the origin) } pub struct BvlcHeader { pub function: u8, pub kind: BvlcFunction, pub length: u16, // the whole datagram } }
Every struct and enum of the module is #[non_exhaustive], and every byte field borrows the datagram: decoding allocates nothing.
| Bytes | Field | Note |
|---|---|---|
| 0 | type | 0x81; checked, not kept |
| 1 | function, kind | the code and its BvlcFunction |
| 2–3 | length | big-endian, header included: must equal the datagram |
| 4–9 | forwarded_from | Forwarded-NPDU only: IPv4 address and UDP port of the original sender |
| then | body | the rest: the NPDU, or the data of the function |
| Code | BvlcFunction | NPDU |
|---|---|---|
0x00 | Result | — |
0x01 | WriteBroadcastDistributionTable | — |
0x02 | ReadBroadcastDistributionTable | — |
0x03 | ReadBroadcastDistributionTableAck | — |
0x04 | ForwardedNpdu | after the 6-byte origin |
0x05 | RegisterForeignDevice | — |
0x06 | ReadForeignDeviceTable | — |
0x07 | ReadForeignDeviceTableAck | — |
0x08 | DeleteForeignDeviceTableEntry | — |
0x09 | DistributeBroadcastToNetwork | yes |
0x0A | OriginalUnicastNpdu | yes |
0x0B | OriginalBroadcastNpdu | yes |
0x0C | SecureBvll | — |
For the four functions that carry an NPDU, body holds the bytes npdu decodes. For the nine others npdu is None and body is their data, undecoded: a result code, a time-to-live, a table; a Secure-BVLL is recognized and left whole in body. BvlcFunction::from_code is public. A registration and its answer (tested):
#![allow(unused)] fn main() { #[test] fn bacnet_bvlc_functions_without_an_npdu() { // Frame 25: a device asks to be registered as a foreign device. let raw = hex::decode(ITI_REGISTER_FOREIGN_DEVICE_HEX).unwrap(); let flow = parse(LinkType::ETHERNET, &raw).unwrap(); assert_eq!(flow.application.as_ref().unwrap().protocol.label(), "BACnet/IP"); let payload = flow.transport.as_ref().unwrap().payload.unwrap(); let packet = BacnetPacket::try_from(payload).unwrap(); assert_eq!(packet.bvlc.kind, BvlcFunction::RegisterForeignDevice); assert_eq!(packet.bvlc.length, 6); assert_eq!(packet.npdu, None); assert_eq!(packet.body, &[0x00, 0x10]); // time-to-live, 16 seconds // Frame 26: the BVLC-Result. Its code is left in `body`, undecoded // (tshark reads 0x0030, Register-Foreign-Device NAK). let raw = hex::decode(ITI_BVLC_RESULT_HEX).unwrap(); let flow = parse(LinkType::ETHERNET, &raw).unwrap(); let payload = flow.transport.as_ref().unwrap().payload.unwrap(); let packet = BacnetPacket::try_from(payload).unwrap(); assert_eq!(packet.bvlc.kind, BvlcFunction::Result); assert_eq!(packet.npdu, None); assert_eq!(packet.body, &[0x00, 0x30]); } }
The NPDU
#![allow(unused)] fn main() { pub struct Npdu<'a> { pub version: u8, // 1 pub control: u8, // the raw byte pub destination: Option<NetworkAddress<'a>>, // DNET, DLEN, DADR pub source: Option<NetworkAddress<'a>>, // SNET, SLEN, SADR pub hop_count: Option<u8>, // present with a destination pub message: NpduMessage<'a>, } pub struct NetworkAddress<'a> { pub network: u16, pub address: &'a [u8], // MAC address, empty for a broadcast } pub enum NpduMessage<'a> { Network { message_type: u8, vendor_id: Option<u16>, data: &'a [u8] }, Apdu(Apdu<'a>), } }
The NPDU is the network layer of BACnet, the one that routes between BACnet networks. After its two fixed bytes, what follows depends on the control byte, so the fields have sizes, not offsets:
| Field | Bytes | Present when |
|---|---|---|
version | 1 | always; must be 1 |
control | 1 | always |
destination | 2 + 1 + DLEN | control bit 0x20 |
source | 2 + 1 + SLEN | control bit 0x08 |
hop_count | 1 | a destination is present |
message_type | 1 | control bit 0x80, then a 2-byte vendor_id for types 0x80 and above |
| the rest | the network message data, or the APDU |
| Control bit | Meaning | Read through |
|---|---|---|
0x80 | a network-layer message, not an APDU | NpduMessage::Network |
0x40 | reserved | not checked |
0x20 | destination specifier | destination, hop_count |
0x10 | reserved | not checked |
0x08 | source specifier | source |
0x04 | expecting reply | expecting_reply() |
0x03 | priority | priority(): 0 normal, 1 urgent, 2 critical equipment, 3 life safety |
A destination network of 65535 is a global broadcast, and an empty address a broadcast on the destination network. Network messages are typed by number only: message_type, the vendor_id of a proprietary type, and the data, raw. NpduMessage is #[non_exhaustive]: a match on it needs a _ arm. A Who-Is broadcast to every network, then a network message of the same segment (tested):
#![allow(unused)] fn main() { #[test] fn bacnet_who_is_and_a_network_message_broadcast_to_all_networks() { // Frame 10: a Who-Is for every device on every network. let raw = hex::decode(NDPI_WHO_IS_HEX).unwrap(); let flow = parse(LinkType::ETHERNET, &raw).unwrap(); assert_eq!(flow.application.as_ref().unwrap().protocol.label(), "BACnet/IP"); let payload = flow.transport.as_ref().unwrap().payload.unwrap(); let packet = BacnetPacket::try_from(payload).unwrap(); assert_eq!(packet.bvlc.kind, BvlcFunction::OriginalBroadcastNpdu); let npdu = packet.npdu.unwrap(); assert_eq!(npdu.control, 0x20); // destination specifier let destination = npdu.destination.unwrap(); assert_eq!(destination.network, 65535); // DNET 0xFFFF assert!(destination.address.is_empty()); // DLEN 0 assert_eq!(npdu.source, None); assert_eq!(npdu.hop_count, Some(255)); // present with a destination let NpduMessage::Apdu(who_is) = npdu.message else { panic!("expected an APDU, got {:?}", npdu.message); }; assert_eq!(who_is.kind, ApduType::UnconfirmedRequest); assert_eq!(who_is.invoke_id, None); // nothing to pair with assert_eq!(who_is.unconfirmed_service(), Some(UnconfirmedService::WhoIs)); assert_eq!(who_is.confirmed_service(), None); assert!(who_is.data.is_empty()); // no device instance range // Frame 22: control 0xA0, a network-layer message instead of an APDU. let raw = hex::decode(NDPI_NETWORK_MESSAGE_HEX).unwrap(); let flow = parse(LinkType::ETHERNET, &raw).unwrap(); let payload = flow.transport.as_ref().unwrap().payload.unwrap(); let packet = BacnetPacket::try_from(payload).unwrap(); let npdu = packet.npdu.unwrap(); assert_eq!((npdu.control, npdu.hop_count), (0xa0, Some(255))); let NpduMessage::Network { message_type, vendor_id, data } = npdu.message else { panic!("expected a network message, got {:?}", npdu.message); }; assert_eq!(message_type, 0x01); // I-Am-Router-To-Network, for tshark assert_eq!(vendor_id, None); // only types 0x80 and above have one assert_eq!(data, &[0x00, 0x00]); // the network list, raw } }
The APDU
#![allow(unused)] fn main() { pub struct Apdu<'a> { pub apdu_type: u8, // high nibble of the first byte pub kind: ApduType, pub flags: u8, // low nibble, as is pub invoke_id: Option<u8>, // absent from an unconfirmed request pub max_segments_and_apdu: Option<u8>, // confirmed request: the raw byte pub segment: Option<(u8, u8)>, // sequence number, window size pub service: Option<u8>, // service choice pub reason: Option<u8>, // reject or abort reason pub data: &'a [u8], // the service data, tag-encoded } }
The type says which fixed fields follow the first byte; whatever comes after them is data:
| Type | ApduType | Fields, in wire order |
|---|---|---|
| 0 | ConfirmedRequest | max_segments_and_apdu, invoke_id, segment if SEG, service |
| 1 | UnconfirmedRequest | service |
| 2 | SimpleAck | invoke_id, service |
| 3 | ComplexAck | invoke_id, segment if SEG, service |
| 4 | SegmentAck | invoke_id, segment |
| 5 | Error | invoke_id, service |
| 6 | Reject | invoke_id, reason |
| 7 | Abort | invoke_id, reason |
flags is the low nibble of the first byte: SEG 0x08 and MOR 0x04 in a ConfirmedRequest or a ComplexACK, SA 0x02 in a ConfirmedRequest, NAK 0x02 and SRV 0x01 in a SegmentACK, SRV 0x01 in an Abort. The decoder itself reads only SEG, which says whether a sequence number and a window follow; the other bits are left to the caller.
confirmed_service() names the service of a ConfirmedRequest, SimpleAck, ComplexAck or Error among the 36 of ConfirmedService (0 AcknowledgeAlarm to 35 DirectoryQuery), and unconfirmed_service() that of an UnconfirmedRequest among the 15 of UnconfirmedService (0 IAm to 14 YouAre). An unknown code is not an error: service keeps the byte and the method returns None. Both enums have a public from_code.
The last two confirmed services came in 11.7.0 (#190), at the end of the enum: 34 AuthRequest, auth-request, from addendum 135-2020cp (protocol revision 29, incorporated into ANSI/ASHRAE 135-2024), and 35 DirectoryQuery, directory-query, from addendum 135-2024cu (protocol revision 31). Before 11.7.0, from_code returned None for both codes. tshark 4.6.6 names the first, authRequest (34), and not the second. No datagram of the corpus carries either.
A readProperty, the ComplexACK that answers it, and the two new codes (tested):
#![allow(unused)] fn main() { #[test] fn bacnet_read_property_and_its_complex_ack() { // Frame 3: labelled by the port rule, then decoded from the payload. let raw = hex::decode(ITI_READ_PROPERTY_HEX).unwrap(); let flow = parse(LinkType::ETHERNET, &raw).unwrap(); assert_eq!(flow.application.as_ref().unwrap().protocol.label(), "BACnet/IP"); let transport = flow.transport.as_ref().unwrap(); assert_eq!(transport.destination_port, Some(47808)); let packet = BacnetPacket::try_from(transport.payload.unwrap()).unwrap(); assert_eq!(packet.bvlc.kind, BvlcFunction::OriginalUnicastNpdu); assert_eq!((packet.bvlc.function, packet.bvlc.length), (0x0a, 17)); // the whole datagram assert_eq!(packet.forwarded_from, None); let npdu = packet.npdu.unwrap(); assert_eq!((npdu.version, npdu.control), (1, 0x04)); assert!(npdu.expecting_reply()); assert_eq!(npdu.priority(), 0); // normal assert!(npdu.destination.is_none() && npdu.source.is_none()); assert_eq!(npdu.hop_count, None); // no destination, no hop count let NpduMessage::Apdu(request) = npdu.message else { panic!("expected an APDU, got {:?}", npdu.message); }; assert_eq!(request.kind, ApduType::ConfirmedRequest); assert_eq!(request.flags, 0); // not segmented assert_eq!(request.max_segments_and_apdu, Some(0x05)); // up to 1476 bytes, for tshark assert_eq!(request.invoke_id, Some(1)); assert_eq!(request.service, Some(12)); assert_eq!(request.confirmed_service(), Some(ConfirmedService::ReadProperty)); // The object and the property asked for, tag-encoded, left as bytes. assert_eq!(request.data, &[0x0c, 0x02, 0x3f, 0xff, 0xff, 0x19, 0x4b]); // Frame 4: the answer carries the same invoke id and service. let raw = hex::decode(ITI_COMPLEX_ACK_HEX).unwrap(); let flow = parse(LinkType::ETHERNET, &raw).unwrap(); let payload = flow.transport.as_ref().unwrap().payload.unwrap(); let packet = BacnetPacket::try_from(payload).unwrap(); let npdu = packet.npdu.unwrap(); assert!(!npdu.expecting_reply()); let NpduMessage::Apdu(ack) = npdu.message else { panic!("expected an APDU, got {:?}", npdu.message); }; assert_eq!(ack.kind, ApduType::ComplexAck); assert_eq!((ack.invoke_id, ack.service), (Some(1), Some(12))); assert_eq!(ack.max_segments_and_apdu, None); // a request field assert_eq!(ack.segment, None); assert_eq!(ack.data.len(), 14); // object and property again, then the value // Past the corpus: the codes 34 and 35 are named since 11.7.0, 36 is not. assert_eq!(ConfirmedService::from_code(34), Some(ConfirmedService::AuthRequest)); assert_eq!(ConfirmedService::from_code(35), Some(ConfirmedService::DirectoryQuery)); assert_eq!(ConfirmedService::from_code(36), None); } }
Validations
All of them are BacnetParseError, in packet_parser::errors::application::bacnet:
| Error | When |
|---|---|
Truncated { expected, actual } | a part is shorter than its fixed fields: the 4-byte BVLC header, the origin of a Forwarded-NPDU, the NPDU header, a network address, the hop count, a network message, the fields of an APDU type; both numbers count from the start of what is being read: the datagram for the BVLC header, the BVLC body for the origin, the NPDU for its header and its hop count, and otherwise the address, the network message or the APDU itself |
InvalidBvlcType(u8) | the first byte is not 0x81; BACnet/IPv6 (annex U), type 0x82 for tshark, ends here |
UnknownBvlcFunction(u8) | a function above 0x0C |
LengthMismatch { declared, actual } | a BVLC length other than the datagram's, trailing bytes included; the Ethernet padding of a short frame is not part of the datagram (frames 1 to 9 of ndpi_bacnet.pcap carry one byte of it) |
UnsupportedNpduVersion(u8) | an NPDU version other than 1 |
UnknownApduType(u8) | an APDU type above 7 |
In the pipeline, every one of them means the same thing: the label is not given, as the detection section shows.
Oracle: 45 datagrams, field by field
The golden test tests/bacnet_golden.rs runs the two captures of pcaps_exemple/protocols/bacnet/ and compares every datagram with tshark 4.6.6, frozen in tests/data/bacnet_tshark_oracle.tsv by tools/bacnet_oracle.sh: ports and payload length, BVLC function and length, NPDU version, control, destination and source networks and address lengths, hop count, network message type, APDU type, invoke id, confirmed or unconfirmed service. The 45 datagrams that carry a BVLC are all labelled "BACnet/IP"; the four empty UDP datagrams of the ITI capture stay without application.
| Capture | Datagrams | Content | Origin |
|---|---|---|---|
iti_bacnet_test.pcap | 22 of 26 frames | a host queries a Johnson Controls controller (MS-NAE4510-2): the nine readProperty of nmap's bacnet-info script, byte for byte, all on the device object, and their ComplexACK; a Read-Broadcast-Distribution-Table and a Register-Foreign-Device, both answered by a BVLC-Result | ITI ICS-Security-Tools, CC-BY-4.0 |
ndpi_bacnet.pcap | 23 | nine readProperty that Internet scanners send to seven exposed addresses of one network, then a local network: Who-Is, I-Am, an Error, two network messages | nDPI test corpus, LGPL-3.0 |
Over the 45: 27 Original-Unicast-NPDU, 14 Original-Broadcast-NPDU, two BVLC-Results, one Read-Broadcast-Distribution-Table and one Register-Foreign-Device; 18 ConfirmedRequest, 11 UnconfirmedRequest, 9 ComplexACK and one Error APDU, plus two network messages. One column is compared after a translation: tshark's bvlc.length is the size of the BVLC header it consumed (4, or 10 with the origin of a Forwarded-NPDU) when an NPDU follows, and the whole message otherwise; the test rebuilds that value, while the wire field is checked against the datagram by the decoder itself. A second test moves frame 3 of the ITI capture to ports 40000 and 40001, where only the blind rule can label it, then alters one byte at a time (BVLC type, function, length, NPDU version, APDU type): the decoder refuses each of the five, and the label goes.
A run of the 11.6.0 pipeline over the 386 403 frames that the crate's test reader gets from the captures tracked under pcaps_exemple/ gives the label "BACnet/IP" to these 45 datagrams and to nothing else: the very frames tshark dissects as BVLC. The fuzz target parse_bacnet runs nightly, on its input and on mutations of four real datagrams: whatever it decodes must have the input's length as BVLC length, a hop count exactly when it has a destination, a vendor id exactly on network message types 0x80 and above, and its body and data inside the input.
What is left out
The limits every protocol shares, reassembly and checksums among them, are on the Limits page. The ones below are this protocol's own.
- Service data stays tag-encoded bytes in
Apdu::data: object identifiers, property values, the device range of a Who-Is, the content of an I-Am. The crate decodes the envelope, not the application tags. - Network messages and BVLC control functions are typed, not decoded:
message_typeanddata, the result codes, times-to-live and tables inbody. A Secure-BVLL is recognized and left whole. - No state: no reassembly of segmented messages, no pairing of a request with its acknowledgement by invoke id. One datagram at a time, as in the rest of the crate.
- Annex J only: BACnet/IPv6 is refused as
InvalidBvlcType, and the other data links of the standard reach no decoder. - The two reserved bits of the NPDU control,
0x40and0x10, are not checked: a datagram with one of them set is decoded and labelled like any other. - Paths no capture exercises: the corpus has no segmented message, no SimpleACK, SegmentACK, Reject or Abort, no source address, no Forwarded-NPDU and no proprietary network message. These follow the standard; the last two are checked by unit tests on built bytes, the Forwarded-NPDU wrapped around frame 3.