Properly terminate a value string. Fixes a current Buildbot complaint.
[obnox/wireshark/wip.git] / epan / dissectors / packet-bfd.c
1 /* packet-bfd.c
2  * Routines for Bi-directional Fault Detection (BFD) message dissection
3  *
4  * Copyright 2003, Hannes Gredler <hannes@juniper.net>
5  * Copyright 2006, Balint Reczey <Balint.Reczey@ericsson.com>
6  *
7  * $Id$
8  *
9  * Ethereal - Network traffic analyzer
10  * By Gerald Combs <gerald@ethereal.com>
11  * Copyright 1998 Gerald Combs
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 <ctype.h>
35
36 #include <string.h>
37 #include <glib.h>
38 #include <epan/packet.h>
39
40 #define UDP_PORT_BFD_CONTROL 3784 /* draft-katz-ward-bfd-v4v6-1hop-00.txt */
41
42 static const value_string bfd_control_v0_diag_values[] = {
43     { 0, "No Diagnostic" },
44     { 1, "Control Detection Time Expired" },
45     { 2, "Echo Function Failed" },
46     { 3, "Neighbor Signaled Session Down" },
47     { 4, "Forwarding Plane Reset" },
48     { 5, "Path Down" },
49     { 6, "Concatenated Path Down" },
50     { 7, "Administratively Down" },
51     { 0, NULL }
52 };
53
54 static const value_string bfd_control_v1_diag_values[] = {
55     { 0, "No Diagnostic" },
56     { 1, "Control Detection Time Expired" },
57     { 2, "Echo Function Failed" },
58     { 3, "Neighbor Signaled Session Down" },
59     { 4, "Forwarding Plane Reset" },
60     { 5, "Path Down" },
61     { 6, "Concatenated Path Down" },
62     { 7, "Administratively Down" },
63     { 8, "Reverse Concatenated Path Down" },
64     { 0, NULL }
65 };
66
67 static const value_string bfd_control_sta_values[] = {
68     { 0, "AdminDown" },
69     { 1, "Down" },
70     { 2, "Init" },
71     { 3, "Up" },
72     { 0, NULL }
73 };
74
75 static gint proto_bfd = -1;
76
77 static gint hf_bfd_version = -1;
78 static gint hf_bfd_diag = -1;
79 static gint hf_bfd_sta = -1;
80 static gint hf_bfd_flags = -1;
81 static gint hf_bfd_flags_h = -1;
82 static gint hf_bfd_flags_p = -1;
83 static gint hf_bfd_flags_f = -1;
84 static gint hf_bfd_flags_c = -1;
85 static gint hf_bfd_flags_a = -1;
86 static gint hf_bfd_flags_d = -1;
87 static gint hf_bfd_flags_d_v0 = -1;
88 static gint hf_bfd_detect_time_multiplier = -1;
89 static gint hf_bfd_my_discriminator = -1;
90 static gint hf_bfd_your_discriminator = -1;
91 static gint hf_bfd_desired_min_tx_interval = -1;
92 static gint hf_bfd_required_min_rx_interval = -1;
93 static gint hf_bfd_required_min_echo_interval = -1;
94
95 static gint ett_bfd = -1;
96 static gint ett_bfd_flags = -1;
97
98 /*
99  * Control packet version 0, draft-katz-ward-bfd-01.txt
100  *
101  *     0                   1                   2                   3
102  *     0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
103  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
104  *    |Vers |  Diag   |H|D|P|F| Rsvd  |  Detect Mult  |    Length     |
105  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
106  *    |                       My Discriminator                        |
107  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
108  *    |                      Your Discriminator                       |
109  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
110  *    |                    Desired Min TX Interval                    |
111  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
112  *    |                   Required Min RX Interval                    |
113  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
114  *    |                 Required Min Echo RX Interval                 |
115  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
116  */
117
118 /*
119  * Control packet version 1, draft-ietf-bfd-base-04.txt
120  *
121  *     0                   1                   2                   3
122  *     0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
123  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
124  *    |Vers |  Diag   |Sta|P|F|C|A|D|R|  Detect Mult  |    Length     |
125  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
126  *    |                       My Discriminator                        |
127  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
128  *    |                      Your Discriminator                       |
129  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
130  *    |                    Desired Min TX Interval                    |
131  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
132  *    |                   Required Min RX Interval                    |
133  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
134  *    |                 Required Min Echo RX Interval                 |
135  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
136  *
137  *    An optional Authentication Section may be present:
138  *    Dissection is not implemented yet.
139  *     0                   1                   2                   3
140  *     0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
141  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
142  *    |   Auth Type   |   Auth Len    |    Authentication Data...     |
143  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
144  *
145  *
146  *                          
147  */
148
149 static void dissect_bfd_control(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
150 {
151     gint bfd_version = -1;
152     gint bfd_diag = -1;
153     gint bfd_sta = -1;
154     gint bfd_flags = -1;
155     gint bfd_flags_h = -1;
156     gint bfd_flags_p = -1;
157     gint bfd_flags_f = -1;
158     gint bfd_flags_c = -1;
159     gint bfd_flags_a = -1;
160     gint bfd_flags_d = -1;
161     gint bfd_flags_d_v0 = -1;
162     gint bfd_detect_time_multiplier = -1;
163     gint bfd_length = -1;
164     gint bfd_my_discriminator = -1;
165     gint bfd_your_discriminator = -1;
166     gint bfd_desired_min_tx_interval = -1;
167     gint bfd_required_min_rx_interval = -1;
168     gint bfd_required_min_echo_interval = -1;
169
170     proto_item *ti;
171     proto_tree *bfd_tree;
172     proto_tree *bfd_flags_tree;
173     
174     if (check_col(pinfo->cinfo, COL_PROTOCOL))
175         col_set_str(pinfo->cinfo, COL_PROTOCOL, "BFD Control");
176     if (check_col(pinfo->cinfo, COL_INFO))
177         col_clear(pinfo->cinfo, COL_INFO);
178
179     bfd_version = ((tvb_get_guint8(tvb, 0) & 0xe0) >> 5);
180     bfd_diag = (tvb_get_guint8(tvb, 0) & 0x1f);
181     switch (bfd_version) {
182     case 0:
183         bfd_flags = tvb_get_guint8(tvb, 1 );
184         bfd_flags_h = (tvb_get_guint8(tvb, 1) & 0x80);
185         bfd_flags_d_v0 = (tvb_get_guint8(tvb, 1) & 0x40);
186         break;
187     case 1:
188     default:
189         bfd_sta = (tvb_get_guint8(tvb, 1) & 0xc0);
190         bfd_flags = (tvb_get_guint8(tvb, 1) & 0x3e);
191         bfd_flags_p = (tvb_get_guint8(tvb, 1) & 0x20);
192         bfd_flags_f = (tvb_get_guint8(tvb, 1) & 0x10);
193         bfd_flags_c = (tvb_get_guint8(tvb, 1) & 0x08);
194         bfd_flags_a = (tvb_get_guint8(tvb, 1) & 0x04);
195         bfd_flags_d = (tvb_get_guint8(tvb, 1) & 0x02);
196         break;
197     }
198     bfd_detect_time_multiplier = tvb_get_guint8(tvb, 2);
199     bfd_length = tvb_get_guint8(tvb, 3);
200
201     bfd_my_discriminator = tvb_get_ntohl(tvb, 4);
202     bfd_your_discriminator = tvb_get_ntohl(tvb, 8);
203     bfd_desired_min_tx_interval = tvb_get_ntohl(tvb, 12);
204     bfd_required_min_rx_interval = tvb_get_ntohl(tvb, 16);
205     bfd_required_min_echo_interval = tvb_get_ntohl(tvb, 20);
206
207     if (check_col(pinfo->cinfo, COL_INFO)) {
208         switch (bfd_version) {
209         case 0:
210             col_add_fstr(pinfo->cinfo, COL_INFO, "Diag: %s, Flags: 0x%02x",
211                      val_to_str(bfd_diag, bfd_control_v0_diag_values, "UNKNOWN"),
212                      bfd_flags);
213             break;
214         case 1:
215         default:
216             col_add_fstr(pinfo->cinfo, COL_INFO, "Diag: %s, State: %s, Flags: 0x%02x",
217                      val_to_str(bfd_diag, bfd_control_v1_diag_values, "UNKNOWN"),
218                      val_to_str(bfd_sta >> 6 , bfd_control_sta_values, "UNKNOWN"),
219                      bfd_flags);
220             break;
221         }
222     }
223
224     if (tree) {
225         ti = proto_tree_add_protocol_format(tree, proto_bfd, tvb, 0, -1,
226                                             "BFD Control message");
227
228         bfd_tree = proto_item_add_subtree(ti, ett_bfd);
229
230         ti = proto_tree_add_uint(bfd_tree, hf_bfd_version, tvb, 0,
231                                  1, bfd_version << 5);
232
233         ti = proto_tree_add_uint(bfd_tree, hf_bfd_diag, tvb, 0,
234                                  1, bfd_diag);
235
236         switch (bfd_version) {
237         case 0:
238             break;
239         case 1:
240         default:
241             ti = proto_tree_add_uint(bfd_tree, hf_bfd_sta, tvb, 1,
242                                 1, bfd_sta);
243             
244             break;
245         }
246         switch (bfd_version) {
247         case 0:
248             ti = proto_tree_add_text ( bfd_tree, tvb, 1, 1, "Message Flags: 0x%02x",
249                                        bfd_flags);
250             bfd_flags_tree = proto_item_add_subtree(bfd_tree, ett_bfd_flags);
251             ti =  proto_tree_add_boolean(bfd_flags_tree, hf_bfd_flags_h, tvb, 8, 1, bfd_flags_h);
252             ti =  proto_tree_add_boolean(bfd_flags_tree, hf_bfd_flags_d_v0, tvb, 8, 1, bfd_flags_d_v0);
253             break;
254         case 1:
255         default:
256             ti = proto_tree_add_text ( bfd_tree, tvb, 1, 1, "Message Flags: 0x%02x",
257                                        bfd_flags);
258             bfd_flags_tree = proto_item_add_subtree(bfd_tree, ett_bfd_flags);
259             ti =  proto_tree_add_boolean(bfd_flags_tree, hf_bfd_flags_p, tvb, 6, 1, bfd_flags_p);
260             ti =  proto_tree_add_boolean(bfd_flags_tree, hf_bfd_flags_f, tvb, 6, 1, bfd_flags_f);
261             ti =  proto_tree_add_boolean(bfd_flags_tree, hf_bfd_flags_c, tvb, 6, 1, bfd_flags_c);
262             ti =  proto_tree_add_boolean(bfd_flags_tree, hf_bfd_flags_a, tvb, 6, 1, bfd_flags_a);
263             ti =  proto_tree_add_boolean(bfd_flags_tree, hf_bfd_flags_d, tvb, 6, 1, bfd_flags_d);
264             break;
265         }
266
267         ti = proto_tree_add_uint_format_value(bfd_tree, hf_bfd_detect_time_multiplier, tvb, 2,
268                                               1, bfd_detect_time_multiplier,
269                                               "%u (= %u ms Detection time)",
270                                               bfd_detect_time_multiplier,
271                                               bfd_detect_time_multiplier * bfd_desired_min_tx_interval/1000);
272
273         ti = proto_tree_add_text ( bfd_tree, tvb, 3, 1, "Message Length: %u Bytes", bfd_length );
274         
275         ti = proto_tree_add_uint(bfd_tree, hf_bfd_my_discriminator, tvb, 4,
276                                  4, bfd_my_discriminator);
277
278         ti = proto_tree_add_uint(bfd_tree, hf_bfd_your_discriminator, tvb, 8,
279                                  4, bfd_your_discriminator);
280
281         ti = proto_tree_add_uint_format_value(bfd_tree, hf_bfd_desired_min_tx_interval, tvb, 12,
282                                               4, bfd_desired_min_tx_interval,
283                                               "%4u ms",
284                                               bfd_desired_min_tx_interval/1000);
285
286         ti = proto_tree_add_uint_format_value(bfd_tree, hf_bfd_required_min_rx_interval, tvb, 16,
287                                               4, bfd_required_min_rx_interval,
288                                               "%4u ms",
289                                               bfd_required_min_rx_interval/1000);
290
291         ti = proto_tree_add_uint_format_value(bfd_tree, hf_bfd_required_min_echo_interval, tvb, 20,
292                                               4, bfd_required_min_echo_interval,
293                                               "%4u ms",
294                                               bfd_required_min_echo_interval/1000);
295
296     }
297     return;
298 }
299
300 /* Register the protocol with Ethereal */
301 void proto_register_bfd(void)
302 {
303
304     /* Setup list of header fields */
305     static hf_register_info hf[] = {
306         { &hf_bfd_version,
307           { "Protocol Version", "bfd.version",
308             FT_UINT8, BASE_DEC, NULL , 0xe0,
309             "", HFILL }
310         },
311         { &hf_bfd_diag,
312           { "Diagnostic Code", "bfd.diag",
313             FT_UINT8, BASE_HEX, VALS(bfd_control_v1_diag_values), 0x1f,
314             "", HFILL }
315         },
316         { &hf_bfd_sta,
317           { "Session State", "bfd.sta",
318             FT_UINT8, BASE_HEX, VALS(bfd_control_sta_values), 0xc0,
319             "", HFILL }
320         },
321         { &hf_bfd_flags,
322           { "Message Flags", "bfd.flags",
323             FT_UINT8, BASE_HEX, NULL, 0xf0,
324             "", HFILL }
325         },
326         { &hf_bfd_flags_h,
327           { "I hear you", "bfd.flags.h",
328             FT_BOOLEAN, 8, TFS(&flags_set_truth), 0x80,
329             "", HFILL }
330         },
331         { &hf_bfd_flags_d_v0,
332           { "Demand", "bfd.flags.d",
333             FT_BOOLEAN, 8, TFS(&flags_set_truth), 0x40,
334             "", HFILL }
335         },
336         { &hf_bfd_flags_p,
337           { "Poll", "bfd.flags.p",
338             FT_BOOLEAN, 6, TFS(&flags_set_truth), 0x20,
339             "", HFILL }
340         },
341         { &hf_bfd_flags_f,
342           { "Final", "bfd.flags.f",
343             FT_BOOLEAN, 6, TFS(&flags_set_truth), 0x10,
344             "", HFILL }
345         },
346         { &hf_bfd_flags_c,
347           { "Control Plane Independent", "bfd.flags.c",
348             FT_BOOLEAN, 6, TFS(&flags_set_truth), 0x08,
349             "", HFILL }
350         },
351         { &hf_bfd_flags_a,
352           { "Authentication Present", "bfd.flags.a",
353             FT_BOOLEAN, 6, TFS(&flags_set_truth), 0x04,
354             "", HFILL }
355         },
356         { &hf_bfd_flags_d,
357           { "Demand", "bfd.flags.d",
358             FT_BOOLEAN, 6, TFS(&flags_set_truth), 0x02,
359             "", HFILL }
360         },
361         { &hf_bfd_detect_time_multiplier,
362           { "Detect Time Multiplier", "bfd.detect_time_multiplier",
363             FT_UINT8, BASE_DEC, NULL, 0x0,
364             "", HFILL }
365         },
366         { &hf_bfd_my_discriminator,
367           { "My Discriminator", "bfd.my_discriminator",
368             FT_UINT32, BASE_HEX, NULL, 0x0,
369             "", HFILL }
370         },
371         { &hf_bfd_your_discriminator,
372           { "Your Discriminator", "bfd.your_discriminator",
373             FT_UINT32, BASE_HEX, NULL, 0x0,
374             "", HFILL }
375         },
376         { &hf_bfd_desired_min_tx_interval,
377           { "Desired Min TX Interval", "bfd.desired_min_tx_interval",
378             FT_UINT32, BASE_DEC, NULL, 0x0,
379             "", HFILL }
380         },
381         { &hf_bfd_required_min_rx_interval,
382           { "Required Min RX Interval", "bfd.required_min_rx_interval",
383             FT_UINT32, BASE_DEC, NULL, 0x0,
384             "", HFILL }
385         },
386         { &hf_bfd_required_min_echo_interval,
387           { "Required Min Echo Interval", "bfd.required_min_echo_interval",
388             FT_UINT32, BASE_DEC, NULL, 0x0,
389             "", HFILL }
390         },
391     };
392
393     /* Setup protocol subtree array */
394     static gint *ett[] = {
395         &ett_bfd,
396         &ett_bfd_flags
397     };
398
399     /* Register the protocol name and description */
400     proto_bfd = proto_register_protocol("Bi-directional Fault Detection Control Message",
401                                         "BFD Control",
402                                         "bfdcontrol");
403
404     /* Required function calls to register the header fields and subtrees used */
405     proto_register_field_array(proto_bfd, hf, array_length(hf));
406     proto_register_subtree_array(ett, array_length(ett));
407 }
408
409 void
410 proto_reg_handoff_bfd(void)
411 {
412     dissector_handle_t bfd_control_handle;
413
414     bfd_control_handle = create_dissector_handle(dissect_bfd_control, proto_bfd);
415     dissector_add("udp.port", UDP_PORT_BFD_CONTROL, bfd_control_handle);
416 }