2 * Routines for BACnet/IP (BVLL, BVLC) dissection
3 * Copyright 2001, Hartmut Mueller <hartmut@abmlinux.org>, FH Dortmund
5 * $Id: packet-bvlc.c,v 1.10 2002/06/05 00:03:06 gerald Exp $
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.
36 #include <epan/strutil.h>
38 #ifdef HAVE_SYS_TYPES_H
39 # include <sys/types.h>
42 #ifdef HAVE_NETINET_IN_H
43 # include <netinet/in.h>
48 #ifdef NEED_SNPRINTF_H
49 # include "snprintf.h"
52 #include <epan/packet.h>
54 static int proto_bvlc = -1;
55 static int hf_bvlc_type = -1;
56 static int hf_bvlc_function = -1;
57 static int hf_bvlc_length = -1;
58 static int hf_bvlc_result = -1;
59 static int hf_bvlc_bdt_ip = -1;
60 static int hf_bvlc_bdt_mask = -1;
61 static int hf_bvlc_bdt_port = -1;
62 static int hf_bvlc_reg_ttl = -1;
63 static int hf_bvlc_fdt_ip = -1;
64 static int hf_bvlc_fdt_port = -1;
65 static int hf_bvlc_fdt_ttl = -1;
66 static int hf_bvlc_fdt_timeout = -1;
67 static int hf_bvlc_fwd_ip = -1;
68 static int hf_bvlc_fwd_port = -1;
70 static dissector_handle_t data_handle;
72 static dissector_table_t bvlc_dissector_table;
74 static const value_string bvlc_function_names[] = {
75 { 0x00, "BVLC-Result", },
76 { 0x01, "Write-Broadcast-Distribution-Table", },
77 { 0x02, "Read-Broadcast-Distribution-Table", },
78 { 0x03, "Read-Broadcast-Distribution-Table-Ack", },
79 { 0x04, "Forwarded-NPDU", },
80 { 0x05, "Register-Foreign-Device", },
81 { 0x06, "Read-Foreign-Device-Table", },
82 { 0x07, "Read-Foreign-Device-Table-Ack", },
83 { 0x08, "Delete-Foreign-Device-Table-Entry", },
84 { 0x09, "Distribute-Broadcast-To-Network", },
85 { 0x0a, "Original-Unicast-NPDU", },
86 { 0x0b, "Original-Broadcast-NPDU" },
90 static const value_string bvlc_result_names[] = {
91 { 0x00, "Successful completion" },
92 { 0x10, "Write-Broadcast-Distribution-Table NAK" },
93 { 0x20, "Read-Broadcast-Distribution-Table NAK" },
94 { 0x30, "Register-Foreign-Device NAK" },
95 { 0x40, "Read-Foreign-Device-Table NAK" },
96 { 0x50, "Delete-Foreign-Device-Table-Entry NAK" },
97 { 0x60, "Distribute-Broadcast-To-Network NAK" },
101 static gint ett_bvlc = -1;
102 static gint ett_bdt = -1;
103 static gint ett_fdt = -1;
106 dissect_bvlc(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
112 proto_tree *bvlc_tree;
113 proto_tree *bdt_tree; /* Broadcast Distribution Table */
114 proto_tree *fdt_tree; /* Foreign Device Table */
118 guint8 bvlc_function;
120 guint16 packet_length;
125 if (check_col(pinfo->cinfo, COL_PROTOCOL))
126 col_set_str(pinfo->cinfo, COL_PROTOCOL, "BVLC");
128 if (check_col(pinfo->cinfo, COL_INFO))
129 col_set_str(pinfo->cinfo, COL_INFO, "BACnet Virtual Link Control");
133 bvlc_type = tvb_get_guint8(tvb, offset);
134 bvlc_function = tvb_get_guint8(tvb, offset+1);
135 packet_length = tvb_get_ntohs(tvb, offset+2);
136 if (bvlc_function > 0x08) {
137 /* We have a constant header length of BVLC of 4 in every
138 * BVLC-packet forewarding an NPDU. Beware: Changes in the
139 * BACnet-IP-standard may break this.
140 * At the moment, no functions above 0x0b
141 * exist (Addendum 135a to ANSI/ASHRAE 135-1995 - BACnet)
144 } else if(bvlc_function == 0x04) {
145 /* 4 Bytes + 6 Bytes for B/IP Address of Originating Device */
148 /* BVLC-packets with function below 0x09 contain
149 * routing-level data (e.g. Broadcast Distribution)
150 * but no NPDU for BACnet, so bvlc_length goes up to the end
151 * of the captured frame.
153 bvlc_length = packet_length;
157 ti = proto_tree_add_item(tree, proto_bvlc, tvb, 0,
159 bvlc_tree = proto_item_add_subtree(ti, ett_bvlc);
160 proto_tree_add_uint_format(bvlc_tree, hf_bvlc_type, tvb, offset, 1,
161 bvlc_type,"Type: 0x%x (Version %s)",bvlc_type,
162 (bvlc_type == 0x81)?"BACnet/IP (Annex J)":"unknown");
164 proto_tree_add_uint_format(bvlc_tree, hf_bvlc_function, tvb,
165 offset, 1, bvlc_function,"Function: 0x%02x (%s)",
166 bvlc_function, val_to_str (bvlc_function,
167 bvlc_function_names, "Unknown"));
169 proto_tree_add_uint_format(bvlc_tree, hf_bvlc_length, tvb, offset,
170 2, bvlc_length, "BVLC-Length: %d of %d bytes BACnet packet length",
171 bvlc_length, packet_length);
173 switch (bvlc_function) {
174 case 0x00: /* BVLC-Result */
175 bvlc_result = tvb_get_ntohs(tvb, offset);
176 /* I dont know why the result code is encoded in 4 nibbles,
177 * but only using one: 0x00r0. Shifting left 4 bits.
179 /* We should bitmask the result correctly when we have a
180 * packet to dissect, see README.developer, 1.6.2, FID */
181 proto_tree_add_uint_format(bvlc_tree, hf_bvlc_result, tvb,
182 offset, 2, bvlc_result,"Result: 0x%04x (%s)",
183 bvlc_result, val_to_str(bvlc_result << 4,
184 bvlc_result_names, "Unknown"));
187 case 0x01: /* Write-Broadcast-Distribution-Table */
188 case 0x03: /* Read-Broadcast-Distribution-Table-Ack */
189 /* List of BDT Entries: N*10-octet */
190 ti_bdt = proto_tree_add_item(bvlc_tree, proto_bvlc, tvb,
191 offset, bvlc_length-4, FALSE);
192 bdt_tree = proto_item_add_subtree(ti_bdt, ett_bdt);
193 /* List of BDT Entries: N*10-octet */
194 while ((bvlc_length - offset) > 9) {
195 proto_tree_add_item(bdt_tree, hf_bvlc_bdt_ip,
196 tvb, offset, 4, FALSE);
198 proto_tree_add_item(bdt_tree, hf_bvlc_bdt_port,
199 tvb, offset, 2, FALSE);
201 proto_tree_add_item(bdt_tree,
202 hf_bvlc_bdt_mask, tvb, offset, 4,
206 /* We check this if we get a BDT-packet somewhere */
208 case 0x02: /* Read-Broadcast-Distribution-Table */
209 /* nothing to do here */
211 case 0x05: /* Register-Foreign-Device */
212 /* Time-to-Live 2-octets T, Time-to-Live T, in seconds */
213 proto_tree_add_item(bvlc_tree, hf_bvlc_reg_ttl,
214 tvb, offset, 2, FALSE);
217 case 0x06: /* Read-Foreign-Device-Table */
218 /* nothing to do here */
220 case 0x07: /* Read-Foreign-Device-Table-Ack */
221 /* List of FDT Entries: N*10-octet */
222 /* N indicates the number of entries in the FDT whose
223 * contents are being returned. Each returned entry
224 * consists of the 6-octet B/IP address of the registrant;
225 * the 2-octet Time-to-Live value supplied at the time of
226 * registration; and a 2-octet value representing the
227 * number of seconds remaining before the BBMD will purge
228 * the registrant's FDT entry if no re-registration occurs.
230 ti_fdt = proto_tree_add_item(bvlc_tree, proto_bvlc, tvb,
231 offset, bvlc_length -4, FALSE);
232 fdt_tree = proto_item_add_subtree(ti_fdt, ett_fdt);
233 /* List of FDT Entries: N*10-octet */
234 while ((bvlc_length - offset) > 9) {
235 proto_tree_add_item(fdt_tree, hf_bvlc_fdt_ip,
236 tvb, offset, 4, FALSE);
238 proto_tree_add_item(fdt_tree, hf_bvlc_fdt_port,
239 tvb, offset, 2, FALSE);
241 proto_tree_add_item(fdt_tree,
242 hf_bvlc_fdt_ttl, tvb, offset, 2,
245 proto_tree_add_item(fdt_tree,
246 hf_bvlc_fdt_timeout, tvb, offset, 2,
250 /* We check this if we get a FDT-packet somewhere */
252 case 0x08: /* Delete-Foreign-Device-Table-Entry */
253 /* FDT Entry: 6-octets */
254 proto_tree_add_item(bvlc_tree, hf_bvlc_fdt_ip,
255 tvb, offset, 4, FALSE);
257 proto_tree_add_item(bvlc_tree, hf_bvlc_fdt_port,
258 tvb, offset, 2, FALSE);
261 /* We check this if we get a FDT-packet somewhere */
262 case 0x04: /* Forwarded-NPDU
263 * Why is this 0x04? It would have been a better
264 * idea to append all forewarded NPDUs at the
265 * end of the function table in the B/IP-standard!
267 /* proto_tree_add_bytes_format(); */
268 proto_tree_add_item(bvlc_tree, hf_bvlc_fwd_ip,
269 tvb, offset, 4, FALSE);
271 proto_tree_add_item(bvlc_tree, hf_bvlc_fwd_port,
272 tvb, offset, 2, FALSE);
274 default:/* Distribute-Broadcast-To-Network
275 * Original-Unicast-NPDU
276 * Original-Broadcast-NPDU
277 * Going to the next dissector...
283 /* Ok, no routing information BVLC packet. Dissect as
286 npdu_length = packet_length - bvlc_length;
287 next_tvb = tvb_new_subset(tvb,bvlc_length,-1,npdu_length);
288 /* Code from Guy Harris */
289 if (!dissector_try_port(bvlc_dissector_table,
290 bvlc_function, next_tvb, pinfo, tree)) {
291 /* Unknown function - dissect the paylod as data */
292 call_dissector(data_handle,next_tvb, pinfo, tree);
297 proto_register_bvlc(void)
299 static hf_register_info hf[] = {
301 { "Type", "bvlc.type",
302 FT_UINT8, BASE_HEX, NULL, 0,
306 { "Function", "bvlc.function",
307 FT_UINT8, BASE_HEX, NULL, 0,
308 "BLVC Function", HFILL }
311 { "Length", "bvlc.length",
312 FT_UINT16, BASE_DEC, NULL, 0,
313 "Length of BVLC", HFILL }
315 /* We should bitmask the result correctly when we have a
316 * packet to dissect */
318 { "Result", "bvlc.result",
319 FT_UINT16, BASE_HEX, NULL, 0xffff,
320 "Result Code", HFILL }
323 { "IP", "bvlc.bdt_ip",
324 FT_IPv4, BASE_NONE, NULL, 0,
328 { "Port", "bvlc.bdt_port",
329 FT_UINT16, BASE_DEC, NULL, 0,
333 { "Mask", "bvlc.bdt_mask",
334 FT_BYTES, BASE_HEX, NULL, 0,
335 "BDT Broadcast Distribution Mask", HFILL }
338 { "TTL", "bvlc.reg_ttl",
339 FT_UINT16, BASE_DEC, NULL, 0,
340 "Foreign Device Time To Live", HFILL }
343 { "IP", "bvlc.fdt_ip",
344 FT_IPv4, BASE_NONE, NULL, 0,
348 { "Port", "bvlc.fdt_port",
349 FT_UINT16, BASE_DEC, NULL, 0,
353 { "TTL", "bvlc.fdt_ttl",
354 FT_UINT16, BASE_DEC, NULL, 0,
355 "Foreign Device Time To Live", HFILL }
357 { &hf_bvlc_fdt_timeout,
358 { "Timeout", "bvlc.fdt_timeout",
359 FT_UINT16, BASE_DEC, NULL, 0,
360 "Foreign Device Timeout (seconds)", HFILL }
363 { "IP", "bvlc.fwd_ip",
364 FT_IPv4, BASE_NONE, NULL, 0,
368 { "Port", "bvlc.fwd_port",
369 FT_UINT16, BASE_DEC, NULL, 0,
374 static gint *ett[] = {
380 proto_bvlc = proto_register_protocol("BACnet Virtual Link Control",
383 proto_register_field_array(proto_bvlc, hf, array_length(hf));
384 proto_register_subtree_array(ett, array_length(ett));
386 register_dissector("bvlc", dissect_bvlc, proto_bvlc);
388 bvlc_dissector_table = register_dissector_table("bvlc.function",
389 "BVLC Function", FT_UINT8, BASE_HEX);
393 proto_reg_handoff_bvlc(void)
395 dissector_handle_t bvlc_handle;
397 bvlc_handle = find_dissector("bvlc");
398 dissector_add("udp.port", 0xBAC0, bvlc_handle);
399 data_handle = find_dissector("data");
401 /* Taken from add-135a (BACnet-IP-standard paper):
403 * The default UDP port for both directed messages and broadcasts shall
404 * be X'BAC0' and all B/IP devices shall support it. In some cases,
405 * e.g., a situation where it is desirable for two groups of BACnet devices
406 * to coexist independently on the same IP subnet, the UDP port may be
407 * configured locally to a different value without it being considered
408 * a violation of this protocol.
410 * This dissector does not analyse UDP packets other than on port 0xBAC0.
411 * If you changed your BACnet port locally, use the ethereal feature