From Steve Karg:
[obnox/wireshark/wip.git] / epan / dissectors / packet-bvlc.c
1 /* packet-bvlc.c
2  * Routines for BACnet/IP (BVLL, BVLC) dissection
3  * Copyright 2001, Hartmut Mueller <hartmut@abmlinux.org>, FH Dortmund
4  *
5  * $Id$
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
9  * Copyright 1998 Gerald Combs
10  *
11  * Copied from README.developer,v 1.23
12  *
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.
17  *
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.
22  *
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.
26  */
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <epan/prefs.h>
36 #include <epan/strutil.h>
37
38 #include <glib.h>
39
40 #include <epan/packet.h>
41
42 /* Taken from add-135a (BACnet-IP-standard paper):
43  *
44  * The default UDP port for both directed messages and broadcasts shall
45  * be X'BAC0' and all B/IP devices shall support it. In some cases,
46  * e.g., a situation where it is desirable for two groups of BACnet devices
47  * to coexist independently on the same IP subnet, the UDP port may be
48  * configured locally to a different value without it being considered
49  * a violation of this protocol.
50  */
51 static guint additional_bvlc_udp_port = 0;
52
53 static int proto_bvlc = -1;
54 static int hf_bvlc_type = -1;
55 static int hf_bvlc_function = -1;
56 static int hf_bvlc_length = -1;
57 static int hf_bvlc_result = -1;
58 static int hf_bvlc_bdt_ip = -1;
59 static int hf_bvlc_bdt_mask = -1;
60 static int hf_bvlc_bdt_port = -1;
61 static int hf_bvlc_reg_ttl = -1;
62 static int hf_bvlc_fdt_ip = -1;
63 static int hf_bvlc_fdt_port = -1;
64 static int hf_bvlc_fdt_ttl = -1;
65 static int hf_bvlc_fdt_timeout = -1;
66 static int hf_bvlc_fwd_ip = -1;
67 static int hf_bvlc_fwd_port = -1;
68
69 static dissector_handle_t data_handle;
70
71 static dissector_table_t bvlc_dissector_table;
72
73 static const value_string bvlc_function_names[] = {
74         { 0x00, "BVLC-Result", },
75         { 0x01, "Write-Broadcast-Distribution-Table", },
76         { 0x02, "Read-Broadcast-Distribution-Table", },
77         { 0x03, "Read-Broadcast-Distribution-Table-Ack", },
78         { 0x04, "Forwarded-NPDU", },
79         { 0x05, "Register-Foreign-Device", },
80         { 0x06, "Read-Foreign-Device-Table", },
81         { 0x07, "Read-Foreign-Device-Table-Ack", },
82         { 0x08, "Delete-Foreign-Device-Table-Entry", },
83         { 0x09, "Distribute-Broadcast-To-Network", },
84         { 0x0a, "Original-Unicast-NPDU", },
85         { 0x0b, "Original-Broadcast-NPDU" },
86         { 0,    NULL }
87 };
88
89 static const value_string bvlc_result_names[] = {
90         { 0x00, "Successful completion" },
91         { 0x10, "Write-Broadcast-Distribution-Table NAK" },
92         { 0x20, "Read-Broadcast-Distribution-Table NAK" },
93         { 0x30, "Register-Foreign-Device NAK" },
94         { 0x40, "Read-Foreign-Device-Table NAK" },
95         { 0x50, "Delete-Foreign-Device-Table-Entry NAK" },
96         { 0x60, "Distribute-Broadcast-To-Network NAK" },
97         { 0,    NULL }
98 };
99
100 static gint ett_bvlc = -1;
101 static gint ett_bdt = -1;
102 static gint ett_fdt = -1;
103
104 #define BACNET_IP_ANNEX_J       0x81
105
106 static const value_string bvlc_types[] = {
107         { BACNET_IP_ANNEX_J,    "BACnet/IP (Annex J)" },
108         { 0,                    NULL }
109 };
110
111 static int
112 dissect_bvlc(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
113 {
114
115         proto_item *ti;
116         proto_item *ti_bdt;
117         proto_item *ti_fdt;
118         proto_tree *bvlc_tree;
119         proto_tree *bdt_tree; /* Broadcast Distribution Table */
120         proto_tree *fdt_tree; /* Foreign Device Table */
121
122         gint offset;
123         guint8 bvlc_type;
124         guint8 bvlc_function;
125         guint16 bvlc_length;
126         guint16 packet_length;
127         guint npdu_length;
128         guint length_remaining;
129         guint16 bvlc_result;
130         tvbuff_t *next_tvb;
131
132         offset = 0;
133
134         bvlc_type =  tvb_get_guint8(tvb, offset);
135
136         /*
137          * Simple sanity check - make sure the type is one we know about.
138          */
139         if (match_strval(bvlc_type, bvlc_types) == NULL)
140                 return 0;
141
142         if (check_col(pinfo->cinfo, COL_PROTOCOL))
143                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "BVLC");
144
145         if (check_col(pinfo->cinfo, COL_INFO))
146                 col_set_str(pinfo->cinfo, COL_INFO, "BACnet Virtual Link Control");
147
148         bvlc_function = tvb_get_guint8(tvb, offset+1);
149         packet_length = tvb_get_ntohs(tvb, offset+2);
150         length_remaining = tvb_length_remaining(tvb, offset);
151         if (bvlc_function > 0x08) {
152                 /*  We have a constant header length of BVLC of 4 in every
153                  *  BVLC-packet forewarding an NPDU. Beware: Changes in the
154                  *  BACnet-IP-standard may break this.
155                  *  At the moment, no functions above 0x0b
156                  *  exist (Addendum 135a to ANSI/ASHRAE 135-1995 - BACnet)
157                  */
158                 bvlc_length = 4;
159         } else if(bvlc_function == 0x04) {
160                 /* 4 Bytes + 6 Bytes for B/IP Address of Originating Device */
161                 bvlc_length = 10;
162         } else {
163                 /*  BVLC-packets with function below 0x09 contain
164                  *  routing-level data (e.g. Broadcast Distribution)
165                  *  but no NPDU for BACnet, so bvlc_length goes up to the end
166                  *  of the captured frame.
167                  */
168                 bvlc_length = packet_length;
169         }
170
171         if (tree) {
172                 if (bvlc_length < 4) {
173                         proto_tree_add_text(tree, tvb, 2, 2,
174                                 "Bogus length: %d", bvlc_length);
175                         return tvb_length(tvb); /* XXX - reject? */
176                 }
177                 ti = proto_tree_add_item(tree, proto_bvlc, tvb, 0,
178                         bvlc_length, FALSE);
179                 bvlc_tree = proto_item_add_subtree(ti, ett_bvlc);
180                 proto_tree_add_uint(bvlc_tree, hf_bvlc_type, tvb, offset, 1,
181                         bvlc_type);
182                 offset ++;
183                 proto_tree_add_uint(bvlc_tree, hf_bvlc_function, tvb,
184                         offset, 1, bvlc_function);
185                 offset ++;
186                 if (length_remaining != packet_length)
187                         proto_tree_add_uint_format_value(bvlc_tree, hf_bvlc_length, tvb, offset,
188                                 2, bvlc_length, 
189                                 "%d of %d bytes (invalid length - expected %d bytes)",
190                                 bvlc_length, packet_length, length_remaining);
191                 else            
192                         proto_tree_add_uint_format_value(bvlc_tree, hf_bvlc_length, tvb, offset,
193                                 2, bvlc_length, "%d of %d bytes BACnet packet length",
194                                 bvlc_length, packet_length);
195                 offset += 2;
196                 switch (bvlc_function) {
197                 case 0x00: /* BVLC-Result */
198                         bvlc_result = tvb_get_ntohs(tvb, offset);
199                         /* I dont know why the result code is encoded in 4 nibbles,
200                          * but only using one: 0x00r0. Shifting left 4 bits.
201                          */
202                         /* We should bitmask the result correctly when we have a
203                         * packet to dissect, see README.developer, 1.6.2, FID */
204                         proto_tree_add_uint_format_value(bvlc_tree, hf_bvlc_result, tvb,
205                                 offset, 2, bvlc_result,"0x%04x (%s)",
206                                 bvlc_result, val_to_str(bvlc_result,
207                                         bvlc_result_names, "Unknown"));
208                         offset += 2;
209                         break;
210                 case 0x01: /* Write-Broadcast-Distribution-Table */
211                 case 0x03: /* Read-Broadcast-Distribution-Table-Ack */
212                         /* List of BDT Entries: N*10-octet */
213                         ti_bdt = proto_tree_add_item(bvlc_tree, proto_bvlc, tvb,
214                                 offset, bvlc_length-4, FALSE);
215                         bdt_tree = proto_item_add_subtree(ti_bdt, ett_bdt);
216                         /* List of BDT Entries: N*10-octet */
217                         while ((bvlc_length - offset) > 9) {
218                                 proto_tree_add_item(bdt_tree, hf_bvlc_bdt_ip,
219                                         tvb, offset, 4, FALSE);
220                                 offset += 4;
221                                 proto_tree_add_item(bdt_tree, hf_bvlc_bdt_port,
222                                         tvb, offset, 2, FALSE);
223                                 offset += 2;
224                                 proto_tree_add_item(bdt_tree,
225                                         hf_bvlc_bdt_mask, tvb, offset, 4,
226                                         FALSE);
227                                 offset += 4;
228                         }
229                         /* We check this if we get a BDT-packet somewhere */
230                         break;
231                 case 0x02: /* Read-Broadcast-Distribution-Table */
232                         /* nothing to do here */
233                         break;
234                 case 0x05: /* Register-Foreign-Device */
235                         /* Time-to-Live 2-octets T, Time-to-Live T, in seconds */
236                         proto_tree_add_item(bvlc_tree, hf_bvlc_reg_ttl,
237                                 tvb, offset, 2, FALSE);
238                         offset += 2;
239                         break;
240                 case 0x06: /* Read-Foreign-Device-Table */
241                         /* nothing to do here */
242                         break;
243                 case 0x07: /* Read-Foreign-Device-Table-Ack */
244                         /* List of FDT Entries: N*10-octet */
245                         /* N indicates the number of entries in the FDT whose
246                          * contents are being returned. Each returned entry
247                          * consists of the 6-octet B/IP address of the registrant;
248                          * the 2-octet Time-to-Live value supplied at the time of
249                          * registration; and a 2-octet value representing the
250                          * number of seconds remaining before the BBMD will purge
251                          * the registrant's FDT entry if no re-registration occurs.
252                          */
253                         ti_fdt = proto_tree_add_item(bvlc_tree, proto_bvlc, tvb,
254                                 offset, bvlc_length -4, FALSE);
255                         fdt_tree = proto_item_add_subtree(ti_fdt, ett_fdt);
256                         /* List of FDT Entries: N*10-octet */
257                         while ((bvlc_length - offset) > 9) {
258                                 proto_tree_add_item(fdt_tree, hf_bvlc_fdt_ip,
259                                         tvb, offset, 4, FALSE);
260                                 offset += 4;
261                                 proto_tree_add_item(fdt_tree, hf_bvlc_fdt_port,
262                                         tvb, offset, 2, FALSE);
263                                 offset += 2;
264                                 proto_tree_add_item(fdt_tree,
265                                         hf_bvlc_fdt_ttl, tvb, offset, 2,
266                                         FALSE);
267                                 offset += 2;
268                                 proto_tree_add_item(fdt_tree,
269                                         hf_bvlc_fdt_timeout, tvb, offset, 2,
270                                         FALSE);
271                                 offset += 2;
272                         }
273                         /* We check this if we get a FDT-packet somewhere */
274                         break;
275                 case 0x08: /* Delete-Foreign-Device-Table-Entry */
276                         /* FDT Entry:   6-octets */
277                         proto_tree_add_item(bvlc_tree, hf_bvlc_fdt_ip,
278                                 tvb, offset, 4, FALSE);
279                         offset += 4;
280                         proto_tree_add_item(bvlc_tree, hf_bvlc_fdt_port,
281                                 tvb, offset, 2, FALSE);
282                         offset += 2;
283                         break;
284                         /* We check this if we get a FDT-packet somewhere */
285                 case 0x04:      /* Forwarded-NPDU
286                                  * Why is this 0x04? It would have been a better
287                                  * idea to append all forewarded NPDUs at the
288                                  * end of the function table in the B/IP-standard!
289                                  */
290                         /* proto_tree_add_bytes_format(); */
291                         proto_tree_add_item(bvlc_tree, hf_bvlc_fwd_ip,
292                                 tvb, offset, 4, FALSE);
293                         offset += 4;
294                         proto_tree_add_item(bvlc_tree, hf_bvlc_fwd_port,
295                                 tvb, offset, 2, FALSE);
296                         offset += 2;
297                 default:/* Distribute-Broadcast-To-Network
298                          * Original-Unicast-NPDU
299                          * Original-Broadcast-NPDU
300                          * Going to the next dissector...
301                          */
302                         break;
303                 }
304
305         }
306 /* Ok, no routing information BVLC packet. Dissect as
307  * BACnet NPDU
308  */
309         npdu_length = packet_length - bvlc_length;
310         next_tvb = tvb_new_subset(tvb,bvlc_length,-1,npdu_length);
311         /* Code from Guy Harris */
312         if (!dissector_try_port(bvlc_dissector_table,
313             bvlc_function, next_tvb, pinfo, tree)) {
314                 /* Unknown function - dissect the paylod as data */
315                 call_dissector(data_handle,next_tvb, pinfo, tree);
316         }
317         return tvb_length(tvb);
318 }
319
320 void proto_reg_handoff_bvlc(void);
321
322 void
323 proto_register_bvlc(void)
324 {
325         static hf_register_info hf[] = {
326                 { &hf_bvlc_type,
327                         { "Type",           "bvlc.type",
328                         FT_UINT8, BASE_HEX, VALS(bvlc_types), 0,
329                         "Type", HFILL }
330                 },
331                 { &hf_bvlc_function,
332                         { "Function",           "bvlc.function",
333                         FT_UINT8, BASE_HEX, VALS(bvlc_function_names), 0,
334                         "BVLC Function", HFILL }
335                 },
336                 { &hf_bvlc_length,
337                         { "BVLC-Length",        "bvlc.length",
338                         FT_UINT16, BASE_DEC, NULL, 0,
339                         "Length of BVLC", HFILL }
340                 },
341                 /* We should bitmask the result correctly when we have a
342                  * packet to dissect */
343                 { &hf_bvlc_result,
344                         { "Result",           "bvlc.result",
345                         FT_UINT16, BASE_HEX, NULL, 0xffff,
346                         "Result Code", HFILL }
347                 },
348                 { &hf_bvlc_bdt_ip,
349                         { "IP",           "bvlc.bdt_ip",
350                         FT_IPv4, BASE_NONE, NULL, 0,
351                         "BDT IP", HFILL }
352                 },
353                 { &hf_bvlc_bdt_port,
354                         { "Port",           "bvlc.bdt_port",
355                         FT_UINT16, BASE_DEC, NULL, 0,
356                         "BDT Port", HFILL }
357                 },
358                 { &hf_bvlc_bdt_mask,
359                         { "Mask",           "bvlc.bdt_mask",
360                         FT_BYTES, BASE_HEX, NULL, 0,
361                         "BDT Broadcast Distribution Mask", HFILL }
362                 },
363                 { &hf_bvlc_reg_ttl,
364                         { "TTL",           "bvlc.reg_ttl",
365                         FT_UINT16, BASE_DEC, NULL, 0,
366                         "Foreign Device Time To Live", HFILL }
367                 },
368                 { &hf_bvlc_fdt_ip,
369                         { "IP",           "bvlc.fdt_ip",
370                         FT_IPv4, BASE_NONE, NULL, 0,
371                         "FDT IP", HFILL }
372                 },
373                 { &hf_bvlc_fdt_port,
374                         { "Port",           "bvlc.fdt_port",
375                         FT_UINT16, BASE_DEC, NULL, 0,
376                         "FDT Port", HFILL }
377                 },
378                 { &hf_bvlc_fdt_ttl,
379                         { "TTL",           "bvlc.fdt_ttl",
380                         FT_UINT16, BASE_DEC, NULL, 0,
381                         "Foreign Device Time To Live", HFILL }
382                 },
383                 { &hf_bvlc_fdt_timeout,
384                         { "Timeout",           "bvlc.fdt_timeout",
385                         FT_UINT16, BASE_DEC, NULL, 0,
386                         "Foreign Device Timeout (seconds)", HFILL }
387                 },
388                 { &hf_bvlc_fwd_ip,
389                         { "IP",           "bvlc.fwd_ip",
390                         FT_IPv4, BASE_NONE, NULL, 0,
391                         "FWD IP", HFILL }
392                 },
393                 { &hf_bvlc_fwd_port,
394                         { "Port",           "bvlc.fwd_port",
395                         FT_UINT16, BASE_DEC, NULL, 0,
396                         "FWD Port", HFILL }
397                 },
398         };
399
400         static gint *ett[] = {
401                 &ett_bvlc,
402                 &ett_bdt,
403                 &ett_fdt,
404         };
405
406         module_t *bvlc_module;
407
408         proto_bvlc = proto_register_protocol("BACnet Virtual Link Control",
409             "BVLC", "bvlc");
410
411         proto_register_field_array(proto_bvlc, hf, array_length(hf));
412         proto_register_subtree_array(ett, array_length(ett));
413
414         bvlc_module = prefs_register_protocol(proto_bvlc, proto_reg_handoff_bvlc);
415         prefs_register_uint_preference(bvlc_module, "additional_udp_port",
416                                         "Additional UDP port", "Set an additional UDP port, "
417                                         "besides the standard X'BAC0' (47808) port.",
418                                         10, &additional_bvlc_udp_port);
419
420         new_register_dissector("bvlc", dissect_bvlc, proto_bvlc);
421
422         bvlc_dissector_table = register_dissector_table("bvlc.function",
423             "BVLC Function", FT_UINT8, BASE_HEX);
424 }
425
426 void
427 proto_reg_handoff_bvlc(void)
428 {
429         static int bvlc_initialized = FALSE;
430         static dissector_handle_t bvlc_handle;
431         static guint bvlc_udp_port;
432         
433         if (!bvlc_initialized)
434         {
435                 bvlc_handle = find_dissector("bvlc");
436                 dissector_add("udp.port", 0xBAC0, bvlc_handle);
437                 bvlc_initialized = TRUE;
438         }
439         else
440         {
441                 dissector_delete("udp.port", bvlc_udp_port, bvlc_handle);
442         }
443
444         bvlc_udp_port = additional_bvlc_udp_port;
445         dissector_add("udp.port", bvlc_udp_port, bvlc_handle);
446
447         data_handle = find_dissector("data");
448 }