2 * Routines for BACnet/IP (BVLL, BVLC) dissection
3 * Copyright 2001, Hartmut Mueller <hartmut@abmlinux.org>, FH Dortmund
7 * Ethereal - Network traffic analyzer
8 * By Gerald Combs <gerald@ethereal.com>
9 * Copyright 1998 Gerald Combs
11 * Copied from README.developer,v 1.23
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
35 #include <epan/prefs.h>
36 #include <epan/strutil.h>
40 #include <epan/packet.h>
42 static int proto_bvlc = -1;
43 static int hf_bvlc_type = -1;
44 static int hf_bvlc_function = -1;
45 static int hf_bvlc_length = -1;
46 static int hf_bvlc_result = -1;
47 static int hf_bvlc_bdt_ip = -1;
48 static int hf_bvlc_bdt_mask = -1;
49 static int hf_bvlc_bdt_port = -1;
50 static int hf_bvlc_reg_ttl = -1;
51 static int hf_bvlc_fdt_ip = -1;
52 static int hf_bvlc_fdt_port = -1;
53 static int hf_bvlc_fdt_ttl = -1;
54 static int hf_bvlc_fdt_timeout = -1;
55 static int hf_bvlc_fwd_ip = -1;
56 static int hf_bvlc_fwd_port = -1;
58 static dissector_handle_t data_handle;
60 static dissector_table_t bvlc_dissector_table;
62 static const value_string bvlc_function_names[] = {
63 { 0x00, "BVLC-Result", },
64 { 0x01, "Write-Broadcast-Distribution-Table", },
65 { 0x02, "Read-Broadcast-Distribution-Table", },
66 { 0x03, "Read-Broadcast-Distribution-Table-Ack", },
67 { 0x04, "Forwarded-NPDU", },
68 { 0x05, "Register-Foreign-Device", },
69 { 0x06, "Read-Foreign-Device-Table", },
70 { 0x07, "Read-Foreign-Device-Table-Ack", },
71 { 0x08, "Delete-Foreign-Device-Table-Entry", },
72 { 0x09, "Distribute-Broadcast-To-Network", },
73 { 0x0a, "Original-Unicast-NPDU", },
74 { 0x0b, "Original-Broadcast-NPDU" },
78 static const value_string bvlc_result_names[] = {
79 { 0x00, "Successful completion" },
80 { 0x10, "Write-Broadcast-Distribution-Table NAK" },
81 { 0x20, "Read-Broadcast-Distribution-Table NAK" },
82 { 0x30, "Register-Foreign-Device NAK" },
83 { 0x40, "Read-Foreign-Device-Table NAK" },
84 { 0x50, "Delete-Foreign-Device-Table-Entry NAK" },
85 { 0x60, "Distribute-Broadcast-To-Network NAK" },
89 static gint ett_bvlc = -1;
90 static gint ett_bdt = -1;
91 static gint ett_fdt = -1;
94 dissect_bvlc(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
100 proto_tree *bvlc_tree;
101 proto_tree *bdt_tree; /* Broadcast Distribution Table */
102 proto_tree *fdt_tree; /* Foreign Device Table */
106 guint8 bvlc_function;
108 guint16 packet_length;
110 guint length_remaining;
114 if (check_col(pinfo->cinfo, COL_PROTOCOL))
115 col_set_str(pinfo->cinfo, COL_PROTOCOL, "BVLC");
117 if (check_col(pinfo->cinfo, COL_INFO))
118 col_set_str(pinfo->cinfo, COL_INFO, "BACnet Virtual Link Control");
122 bvlc_type = tvb_get_guint8(tvb, offset);
123 bvlc_function = tvb_get_guint8(tvb, offset+1);
124 packet_length = tvb_get_ntohs(tvb, offset+2);
125 length_remaining = tvb_length_remaining(tvb, offset);
126 if (bvlc_function > 0x08) {
127 /* We have a constant header length of BVLC of 4 in every
128 * BVLC-packet forewarding an NPDU. Beware: Changes in the
129 * BACnet-IP-standard may break this.
130 * At the moment, no functions above 0x0b
131 * exist (Addendum 135a to ANSI/ASHRAE 135-1995 - BACnet)
134 } else if(bvlc_function == 0x04) {
135 /* 4 Bytes + 6 Bytes for B/IP Address of Originating Device */
138 /* BVLC-packets with function below 0x09 contain
139 * routing-level data (e.g. Broadcast Distribution)
140 * but no NPDU for BACnet, so bvlc_length goes up to the end
141 * of the captured frame.
143 bvlc_length = packet_length;
147 if (bvlc_length < 4) {
148 proto_tree_add_text(tree, tvb, 2, 2,
149 "Bogus length: %d", bvlc_length);
152 ti = proto_tree_add_item(tree, proto_bvlc, tvb, 0,
154 bvlc_tree = proto_item_add_subtree(ti, ett_bvlc);
155 proto_tree_add_uint_format(bvlc_tree, hf_bvlc_type, tvb, offset, 1,
156 bvlc_type,"Type: 0x%x (Version %s)",bvlc_type,
157 (bvlc_type == 0x81)?"BACnet/IP (Annex J)":"unknown");
159 proto_tree_add_uint_format(bvlc_tree, hf_bvlc_function, tvb,
160 offset, 1, bvlc_function,"Function: 0x%02x (%s)",
161 bvlc_function, val_to_str (bvlc_function,
162 bvlc_function_names, "Unknown"));
164 if (length_remaining != packet_length)
165 proto_tree_add_uint_format(bvlc_tree, hf_bvlc_length, tvb, offset,
167 "BVLC-Length: %d of %d bytes (invalid length - expected %d bytes)",
168 bvlc_length, packet_length, length_remaining);
170 proto_tree_add_uint_format(bvlc_tree, hf_bvlc_length, tvb, offset,
171 2, bvlc_length, "BVLC-Length: %d of %d bytes BACnet packet length",
172 bvlc_length, packet_length);
174 switch (bvlc_function) {
175 case 0x00: /* BVLC-Result */
176 bvlc_result = tvb_get_ntohs(tvb, offset);
177 /* I dont know why the result code is encoded in 4 nibbles,
178 * but only using one: 0x00r0. Shifting left 4 bits.
180 /* We should bitmask the result correctly when we have a
181 * packet to dissect, see README.developer, 1.6.2, FID */
182 proto_tree_add_uint_format(bvlc_tree, hf_bvlc_result, tvb,
183 offset, 2, bvlc_result,"Result: 0x%04x (%s)",
184 bvlc_result, val_to_str(bvlc_result << 4,
185 bvlc_result_names, "Unknown"));
188 case 0x01: /* Write-Broadcast-Distribution-Table */
189 case 0x03: /* Read-Broadcast-Distribution-Table-Ack */
190 /* List of BDT Entries: N*10-octet */
191 ti_bdt = proto_tree_add_item(bvlc_tree, proto_bvlc, tvb,
192 offset, bvlc_length-4, FALSE);
193 bdt_tree = proto_item_add_subtree(ti_bdt, ett_bdt);
194 /* List of BDT Entries: N*10-octet */
195 while ((bvlc_length - offset) > 9) {
196 proto_tree_add_item(bdt_tree, hf_bvlc_bdt_ip,
197 tvb, offset, 4, FALSE);
199 proto_tree_add_item(bdt_tree, hf_bvlc_bdt_port,
200 tvb, offset, 2, FALSE);
202 proto_tree_add_item(bdt_tree,
203 hf_bvlc_bdt_mask, tvb, offset, 4,
207 /* We check this if we get a BDT-packet somewhere */
209 case 0x02: /* Read-Broadcast-Distribution-Table */
210 /* nothing to do here */
212 case 0x05: /* Register-Foreign-Device */
213 /* Time-to-Live 2-octets T, Time-to-Live T, in seconds */
214 proto_tree_add_item(bvlc_tree, hf_bvlc_reg_ttl,
215 tvb, offset, 2, FALSE);
218 case 0x06: /* Read-Foreign-Device-Table */
219 /* nothing to do here */
221 case 0x07: /* Read-Foreign-Device-Table-Ack */
222 /* List of FDT Entries: N*10-octet */
223 /* N indicates the number of entries in the FDT whose
224 * contents are being returned. Each returned entry
225 * consists of the 6-octet B/IP address of the registrant;
226 * the 2-octet Time-to-Live value supplied at the time of
227 * registration; and a 2-octet value representing the
228 * number of seconds remaining before the BBMD will purge
229 * the registrant's FDT entry if no re-registration occurs.
231 ti_fdt = proto_tree_add_item(bvlc_tree, proto_bvlc, tvb,
232 offset, bvlc_length -4, FALSE);
233 fdt_tree = proto_item_add_subtree(ti_fdt, ett_fdt);
234 /* List of FDT Entries: N*10-octet */
235 while ((bvlc_length - offset) > 9) {
236 proto_tree_add_item(fdt_tree, hf_bvlc_fdt_ip,
237 tvb, offset, 4, FALSE);
239 proto_tree_add_item(fdt_tree, hf_bvlc_fdt_port,
240 tvb, offset, 2, FALSE);
242 proto_tree_add_item(fdt_tree,
243 hf_bvlc_fdt_ttl, tvb, offset, 2,
246 proto_tree_add_item(fdt_tree,
247 hf_bvlc_fdt_timeout, tvb, offset, 2,
251 /* We check this if we get a FDT-packet somewhere */
253 case 0x08: /* Delete-Foreign-Device-Table-Entry */
254 /* FDT Entry: 6-octets */
255 proto_tree_add_item(bvlc_tree, hf_bvlc_fdt_ip,
256 tvb, offset, 4, FALSE);
258 proto_tree_add_item(bvlc_tree, hf_bvlc_fdt_port,
259 tvb, offset, 2, FALSE);
262 /* We check this if we get a FDT-packet somewhere */
263 case 0x04: /* Forwarded-NPDU
264 * Why is this 0x04? It would have been a better
265 * idea to append all forewarded NPDUs at the
266 * end of the function table in the B/IP-standard!
268 /* proto_tree_add_bytes_format(); */
269 proto_tree_add_item(bvlc_tree, hf_bvlc_fwd_ip,
270 tvb, offset, 4, FALSE);
272 proto_tree_add_item(bvlc_tree, hf_bvlc_fwd_port,
273 tvb, offset, 2, FALSE);
275 default:/* Distribute-Broadcast-To-Network
276 * Original-Unicast-NPDU
277 * Original-Broadcast-NPDU
278 * Going to the next dissector...
284 /* Ok, no routing information BVLC packet. Dissect as
287 npdu_length = packet_length - bvlc_length;
288 next_tvb = tvb_new_subset(tvb,bvlc_length,-1,npdu_length);
289 /* Code from Guy Harris */
290 if (!dissector_try_port(bvlc_dissector_table,
291 bvlc_function, next_tvb, pinfo, tree)) {
292 /* Unknown function - dissect the paylod as data */
293 call_dissector(data_handle,next_tvb, pinfo, tree);
298 proto_register_bvlc(void)
300 static hf_register_info hf[] = {
302 { "Type", "bvlc.type",
303 FT_UINT8, BASE_HEX, NULL, 0,
307 { "Function", "bvlc.function",
308 FT_UINT8, BASE_HEX, NULL, 0,
309 "BLVC Function", HFILL }
312 { "Length", "bvlc.length",
313 FT_UINT16, BASE_DEC, NULL, 0,
314 "Length of BVLC", HFILL }
316 /* We should bitmask the result correctly when we have a
317 * packet to dissect */
319 { "Result", "bvlc.result",
320 FT_UINT16, BASE_HEX, NULL, 0xffff,
321 "Result Code", HFILL }
324 { "IP", "bvlc.bdt_ip",
325 FT_IPv4, BASE_NONE, NULL, 0,
329 { "Port", "bvlc.bdt_port",
330 FT_UINT16, BASE_DEC, NULL, 0,
334 { "Mask", "bvlc.bdt_mask",
335 FT_BYTES, BASE_HEX, NULL, 0,
336 "BDT Broadcast Distribution Mask", HFILL }
339 { "TTL", "bvlc.reg_ttl",
340 FT_UINT16, BASE_DEC, NULL, 0,
341 "Foreign Device Time To Live", HFILL }
344 { "IP", "bvlc.fdt_ip",
345 FT_IPv4, BASE_NONE, NULL, 0,
349 { "Port", "bvlc.fdt_port",
350 FT_UINT16, BASE_DEC, NULL, 0,
354 { "TTL", "bvlc.fdt_ttl",
355 FT_UINT16, BASE_DEC, NULL, 0,
356 "Foreign Device Time To Live", HFILL }
358 { &hf_bvlc_fdt_timeout,
359 { "Timeout", "bvlc.fdt_timeout",
360 FT_UINT16, BASE_DEC, NULL, 0,
361 "Foreign Device Timeout (seconds)", HFILL }
364 { "IP", "bvlc.fwd_ip",
365 FT_IPv4, BASE_NONE, NULL, 0,
369 { "Port", "bvlc.fwd_port",
370 FT_UINT16, BASE_DEC, NULL, 0,
375 static gint *ett[] = {
381 proto_bvlc = proto_register_protocol("BACnet Virtual Link Control",
384 proto_register_field_array(proto_bvlc, hf, array_length(hf));
385 proto_register_subtree_array(ett, array_length(ett));
387 register_dissector("bvlc", dissect_bvlc, proto_bvlc);
389 bvlc_dissector_table = register_dissector_table("bvlc.function",
390 "BVLC Function", FT_UINT8, BASE_HEX);
394 proto_reg_handoff_bvlc(void)
396 dissector_handle_t bvlc_handle;
398 bvlc_handle = find_dissector("bvlc");
399 dissector_add("udp.port", 0xBAC0, bvlc_handle);
400 data_handle = find_dissector("data");
402 /* Taken from add-135a (BACnet-IP-standard paper):
404 * The default UDP port for both directed messages and broadcasts shall
405 * be X'BAC0' and all B/IP devices shall support it. In some cases,
406 * e.g., a situation where it is desirable for two groups of BACnet devices
407 * to coexist independently on the same IP subnet, the UDP port may be
408 * configured locally to a different value without it being considered
409 * a violation of this protocol.
411 * This dissector does not analyse UDP packets other than on port 0xBAC0.
412 * If you changed your BACnet port locally, use the ethereal feature