Trim leading and trailing white space from the capture device in the
[obnox/wireshark/wip.git] / 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: packet-bvlc.c,v 1.10 2002/06/05 00:03:06 gerald Exp $
6  *
7  * Ethereal - Network traffic analyzer
8  * By Gerald Combs <gerald@ethereal.com>
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 "prefs.h"
36 #include <epan/strutil.h>
37
38 #ifdef HAVE_SYS_TYPES_H
39 # include <sys/types.h>
40 #endif
41
42 #ifdef HAVE_NETINET_IN_H
43 # include <netinet/in.h>
44 #endif
45
46 #include <glib.h>
47
48 #ifdef NEED_SNPRINTF_H
49 # include "snprintf.h"
50 #endif
51
52 #include <epan/packet.h>
53
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;
69
70 static dissector_handle_t data_handle;
71
72 static dissector_table_t bvlc_dissector_table;
73
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" },
87         { 0,    NULL }
88 };
89
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" },
98         { 0,    NULL }
99 };
100
101 static gint ett_bvlc = -1;
102 static gint ett_bdt = -1;
103 static gint ett_fdt = -1;
104
105 static void
106 dissect_bvlc(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
107 {
108
109         proto_item *ti;
110         proto_item *ti_bdt;
111         proto_item *ti_fdt;
112         proto_tree *bvlc_tree;
113         proto_tree *bdt_tree; /* Broadcast Distribution Table */
114         proto_tree *fdt_tree; /* Foreign Device Table */
115         
116         gint offset;
117         guint8 bvlc_type;
118         guint8 bvlc_function;
119         guint16 bvlc_length;
120         guint16 packet_length;
121         guint8 npdu_length;
122         guint16 bvlc_result;
123         tvbuff_t *next_tvb;
124
125         if (check_col(pinfo->cinfo, COL_PROTOCOL))
126                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "BVLC");
127
128         if (check_col(pinfo->cinfo, COL_INFO))
129                 col_set_str(pinfo->cinfo, COL_INFO, "BACnet Virtual Link Control");
130
131         offset = 0;
132
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)
142                  */
143                 bvlc_length = 4;
144         } else if(bvlc_function == 0x04) {
145                 /* 4 Bytes + 6 Bytes for B/IP Address of Originating Device */
146                 bvlc_length = 10; 
147         } else {
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.
152                  */
153                 bvlc_length = packet_length;
154         }
155         
156         if (tree) {
157                 ti = proto_tree_add_item(tree, proto_bvlc, tvb, 0, 
158                         bvlc_length, FALSE);
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");
163                 offset ++;
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"));
168                 offset ++;
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);
172                 offset += 2;
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.
178                          */
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"));
185                         offset += 2;
186                         break;
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);
197                                 offset += 4;
198                                 proto_tree_add_item(bdt_tree, hf_bvlc_bdt_port,
199                                         tvb, offset, 2, FALSE);
200                                 offset += 2;
201                                 proto_tree_add_item(bdt_tree, 
202                                         hf_bvlc_bdt_mask, tvb, offset, 4,
203                                         FALSE);
204                                 offset += 4;
205                         } 
206                         /* We check this if we get a BDT-packet somewhere */
207                         break;
208                 case 0x02: /* Read-Broadcast-Distribution-Table */
209                         /* nothing to do here */
210                         break;
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);
215                         offset += 2;
216                         break;
217                 case 0x06: /* Read-Foreign-Device-Table */
218                         /* nothing to do here */
219                         break;
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.
229                          */
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);
237                                 offset += 4;
238                                 proto_tree_add_item(fdt_tree, hf_bvlc_fdt_port,
239                                         tvb, offset, 2, FALSE);
240                                 offset += 2;
241                                 proto_tree_add_item(fdt_tree, 
242                                         hf_bvlc_fdt_ttl, tvb, offset, 2,
243                                         FALSE);
244                                 offset += 2;
245                                 proto_tree_add_item(fdt_tree, 
246                                         hf_bvlc_fdt_timeout, tvb, offset, 2,
247                                         FALSE);
248                                 offset += 2;
249                         } 
250                         /* We check this if we get a FDT-packet somewhere */
251                         break;
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);
256                         offset += 4;
257                         proto_tree_add_item(bvlc_tree, hf_bvlc_fdt_port,
258                                 tvb, offset, 2, FALSE);
259                         offset += 2;
260                         break;
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!
266                                  */
267                         /* proto_tree_add_bytes_format(); */
268                         proto_tree_add_item(bvlc_tree, hf_bvlc_fwd_ip,
269                                 tvb, offset, 4, FALSE);
270                         offset += 4;
271                         proto_tree_add_item(bvlc_tree, hf_bvlc_fwd_port,
272                                 tvb, offset, 2, FALSE);
273                         offset += 2;
274                 default:/* Distribute-Broadcast-To-Network
275                          * Original-Unicast-NPDU
276                          * Original-Broadcast-NPDU
277                          * Going to the next dissector...
278                          */
279                         break;
280                 }
281
282         }
283 /* Ok, no routing information BVLC packet. Dissect as
284  * BACnet NPDU
285  */
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);
293         }
294 }
295
296 void
297 proto_register_bvlc(void)
298 {
299         static hf_register_info hf[] = {
300                 { &hf_bvlc_type,
301                         { "Type",           "bvlc.type",
302                         FT_UINT8, BASE_HEX, NULL, 0,
303                         "Type", HFILL }
304                 },
305                 { &hf_bvlc_function,
306                         { "Function",           "bvlc.function",
307                         FT_UINT8, BASE_HEX, NULL, 0,
308                         "BLVC Function", HFILL }
309                 },
310                 { &hf_bvlc_length,
311                         { "Length",           "bvlc.length",
312                         FT_UINT16, BASE_DEC, NULL, 0,
313                         "Length of BVLC", HFILL }
314                 },
315                 /* We should bitmask the result correctly when we have a
316                  * packet to dissect */
317                 { &hf_bvlc_result,
318                         { "Result",           "bvlc.result",
319                         FT_UINT16, BASE_HEX, NULL, 0xffff,
320                         "Result Code", HFILL }
321                 },
322                 { &hf_bvlc_bdt_ip,
323                         { "IP",           "bvlc.bdt_ip",
324                         FT_IPv4, BASE_NONE, NULL, 0,
325                         "BDT IP", HFILL }
326                 },
327                 { &hf_bvlc_bdt_port,
328                         { "Port",           "bvlc.bdt_port",
329                         FT_UINT16, BASE_DEC, NULL, 0,
330                         "BDT Port", HFILL }
331                 },
332                 { &hf_bvlc_bdt_mask,
333                         { "Mask",           "bvlc.bdt_mask",
334                         FT_BYTES, BASE_HEX, NULL, 0,
335                         "BDT Broadcast Distribution Mask", HFILL }
336                 },
337                 { &hf_bvlc_reg_ttl,
338                         { "TTL",           "bvlc.reg_ttl",
339                         FT_UINT16, BASE_DEC, NULL, 0,
340                         "Foreign Device Time To Live", HFILL }
341                 },
342                 { &hf_bvlc_fdt_ip,
343                         { "IP",           "bvlc.fdt_ip",
344                         FT_IPv4, BASE_NONE, NULL, 0,
345                         "FDT IP", HFILL }
346                 },
347                 { &hf_bvlc_fdt_port,
348                         { "Port",           "bvlc.fdt_port",
349                         FT_UINT16, BASE_DEC, NULL, 0,
350                         "FDT Port", HFILL }
351                 },
352                 { &hf_bvlc_fdt_ttl,
353                         { "TTL",           "bvlc.fdt_ttl",
354                         FT_UINT16, BASE_DEC, NULL, 0,
355                         "Foreign Device Time To Live", HFILL }
356                 },
357                 { &hf_bvlc_fdt_timeout,
358                         { "Timeout",           "bvlc.fdt_timeout",
359                         FT_UINT16, BASE_DEC, NULL, 0,
360                         "Foreign Device Timeout (seconds)", HFILL }
361                 },
362                 { &hf_bvlc_fwd_ip,
363                         { "IP",           "bvlc.fwd_ip",
364                         FT_IPv4, BASE_NONE, NULL, 0,
365                         "FWD IP", HFILL }
366                 },
367                 { &hf_bvlc_fwd_port,
368                         { "Port",           "bvlc.fwd_port",
369                         FT_UINT16, BASE_DEC, NULL, 0,
370                         "FWD Port", HFILL }
371                 },
372         };
373
374         static gint *ett[] = {
375                 &ett_bvlc,
376                 &ett_bdt,
377                 &ett_fdt,
378         };
379
380         proto_bvlc = proto_register_protocol("BACnet Virtual Link Control",
381             "BVLC", "bvlc");
382
383         proto_register_field_array(proto_bvlc, hf, array_length(hf));
384         proto_register_subtree_array(ett, array_length(ett));
385
386         register_dissector("bvlc", dissect_bvlc, proto_bvlc);
387
388         bvlc_dissector_table = register_dissector_table("bvlc.function",
389             "BVLC Function", FT_UINT8, BASE_HEX);
390 }
391
392 void
393 proto_reg_handoff_bvlc(void)
394 {
395         dissector_handle_t bvlc_handle;
396
397         bvlc_handle = find_dissector("bvlc");
398         dissector_add("udp.port", 0xBAC0, bvlc_handle);
399         data_handle = find_dissector("data");
400 }
401 /* Taken from add-135a (BACnet-IP-standard paper):
402  *
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.
409  *
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
412  * "Decode As".
413  */