From Graeme Hewson:
[obnox/wireshark/wip.git] / 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  *
6  * $Id: packet-bfd.c,v 1.1 2003/11/23 22:00:48 gerald Exp $
7  *
8  * Ethereal - Network traffic analyzer
9  * By Gerald Combs <gerald@ethereal.com>
10  * Copyright 1998 Gerald Combs
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <ctype.h>
34
35 #include <string.h>
36 #include <glib.h>
37 #include <epan/packet.h>
38
39 #define UDP_PORT_BFD_CONTROL 3784 /* draft-katz-ward-bfd-v4v6-1hop-00.txt */
40
41 static const value_string bfd_control_diag_values[] = {
42     { 0, "No Diagnostic" },
43     { 1, "Control Detection Time Expired" },
44     { 2, "Echo Function Failed" },
45     { 3, "Neighbor Signaled Session Down" },
46     { 4, "Forwarding Plane Reset" },
47     { 5, "Path Down" },
48     { 6, "Concatenated Path Down" },
49     { 7, "Administratively Down" },
50     { 0, NULL }
51 };
52
53 static const value_string bfd_control_flag_values[] = {
54     { 0x80,     "I Hear You" },
55     { 0x40,     "Demand" },
56     { 0x20,     "Poll" },
57     { 0x10,     "Final" },
58     { 0x08,     "Reserved" },
59     { 0x04,     "Reserved" },
60     { 0x02,     "Reserved" },
61     { 0x01,     "Reserved" },
62     { 0, NULL }
63 };
64
65 static gint proto_bfd = -1;
66
67 static gint hf_bfd_version = -1;
68 static gint hf_bfd_diag = -1;
69 static gint hf_bfd_flags = -1;
70 static gint hf_bfd_detect_time_multiplier = -1;
71 static gint hf_bfd_my_discriminator = -1;
72 static gint hf_bfd_your_discriminator = -1;
73 static gint hf_bfd_desired_min_tx_interval = -1;
74 static gint hf_bfd_required_min_rx_interval = -1;
75 static gint hf_bfd_required_min_echo_interval = -1;
76
77 static gint ett_bfd = -1;
78
79 /*
80  * Control packet, draft-katz-ward-bfd-01.txt
81  *
82  *     0                   1                   2                   3
83  *     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
84  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
85  *    |Vers |  Diag   |H|D|P|F| Rsvd  |  Detect Mult  |    Length     |
86  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
87  *    |                       My Discriminator                        |
88  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
89  *    |                      Your Discriminator                       |
90  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
91  *    |                    Desired Min TX Interval                    |
92  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
93  *    |                   Required Min RX Interval                    |
94  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
95  *    |                 Required Min Echo RX Interval                 |
96  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
97  */
98
99 static void dissect_bfd_control(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
100 {
101     gint bfd_version = -1;
102     gint bfd_diag = -1;
103     gint bfd_flags = -1;
104     gint bfd_detect_time_multiplier = -1;
105     gint bfd_length = -1;
106     gint bfd_my_discriminator = -1;
107     gint bfd_your_discriminator = -1;
108     gint bfd_desired_min_tx_interval = -1;
109     gint bfd_required_min_rx_interval = -1;
110     gint bfd_required_min_echo_interval = -1;
111
112     proto_item *ti;
113     proto_tree *bfd_tree;
114     
115     if (check_col(pinfo->cinfo, COL_PROTOCOL))
116         col_set_str(pinfo->cinfo, COL_PROTOCOL, "BFD Control");
117     if (check_col(pinfo->cinfo, COL_INFO))
118         col_clear(pinfo->cinfo, COL_INFO);
119
120     bfd_version = ((tvb_get_guint8(tvb, 0) & 0xe0) >> 5);
121     bfd_diag = (tvb_get_guint8(tvb, 0) & 0x1f);
122     bfd_flags = tvb_get_guint8(tvb, 1);
123     bfd_detect_time_multiplier = tvb_get_guint8(tvb, 2);
124     bfd_length = tvb_get_guint8(tvb, 3);
125
126     bfd_my_discriminator = tvb_get_ntohl(tvb, 4);
127     bfd_your_discriminator = tvb_get_ntohl(tvb, 8);
128     bfd_desired_min_tx_interval = tvb_get_ntohl(tvb, 12);
129     bfd_required_min_rx_interval = tvb_get_ntohl(tvb, 16);
130     bfd_required_min_echo_interval = tvb_get_ntohl(tvb, 20);
131
132     if (check_col(pinfo->cinfo, COL_INFO)) {
133         col_add_fstr(pinfo->cinfo, COL_INFO, "Diag: %s, Flags: %s",
134                      val_to_str(bfd_diag, bfd_control_diag_values, "UNKNOWN"),
135                      decode_enumerated_bitfield(bfd_flags,
136                                             0xf0,
137                                             8,
138                                             bfd_control_flag_values,
139                                             "%s"));
140     }
141
142     if (tree) {
143         ti = proto_tree_add_protocol_format(tree, proto_bfd, tvb, 0, -1,
144                                             "BFD Control message");
145
146         bfd_tree = proto_item_add_subtree(ti, ett_bfd);
147
148         ti = proto_tree_add_uint(bfd_tree, hf_bfd_version, tvb, 0,
149                                  1, bfd_version);
150
151         ti = proto_tree_add_uint(bfd_tree, hf_bfd_diag, tvb, 0,
152                                  1, bfd_diag);
153
154         ti = proto_tree_add_text ( bfd_tree, tvb, 1, 1, "Message Flags: %s",
155                                    decode_enumerated_bitfield(bfd_flags,
156                                                               0xf0,
157                                                               8,
158                                                               bfd_control_flag_values,
159                                                               "%s"));
160
161         ti = proto_tree_add_uint_format(bfd_tree, hf_bfd_detect_time_multiplier, tvb, 2,
162                                         1, bfd_detect_time_multiplier,
163                                         "%s: %u (= %u ms Detection time)",
164                                         proto_registrar_get_nth(hf_bfd_detect_time_multiplier) -> name,
165                                         bfd_detect_time_multiplier,
166                                         bfd_detect_time_multiplier * bfd_desired_min_tx_interval/1000);
167
168         ti = proto_tree_add_text ( bfd_tree, tvb, 3, 1, "Message Length: %u Bytes", bfd_length );
169         
170         ti = proto_tree_add_uint(bfd_tree, hf_bfd_my_discriminator, tvb, 4,
171                                  4, bfd_my_discriminator);
172
173         ti = proto_tree_add_uint(bfd_tree, hf_bfd_your_discriminator, tvb, 8,
174                                  4, bfd_your_discriminator);
175
176         ti = proto_tree_add_uint_format(bfd_tree, hf_bfd_desired_min_tx_interval, tvb, 12,
177                                         4, bfd_desired_min_tx_interval,
178                                         "%s: %4u ms",
179                                         proto_registrar_get_nth(hf_bfd_desired_min_tx_interval) -> name,
180                                         bfd_desired_min_tx_interval/1000);
181
182         ti = proto_tree_add_uint_format(bfd_tree, hf_bfd_required_min_rx_interval, tvb, 16,
183                                         4, bfd_required_min_rx_interval,
184                                         "%s: %4u ms",
185                                         proto_registrar_get_nth(hf_bfd_required_min_rx_interval) -> name,
186                                         bfd_required_min_rx_interval/1000);
187
188         ti = proto_tree_add_uint_format(bfd_tree, hf_bfd_required_min_echo_interval, tvb, 20,
189                                         4, bfd_required_min_echo_interval,
190                                         "%s: %4u ms",
191                                         proto_registrar_get_nth(hf_bfd_required_min_echo_interval) -> name,
192                                         bfd_required_min_echo_interval/1000);
193
194     }
195     return;
196 }
197
198 /* Register the protocol with Ethereal */
199 void proto_register_bfd(void)
200 {
201
202     /* Setup list of header fields */
203     static hf_register_info hf[] = {
204         { &hf_bfd_version,
205           { "Protocol Version", "bfd.version",
206             FT_UINT8, BASE_DEC, NULL , 0xe0,
207             "", HFILL }
208         },
209         { &hf_bfd_diag,
210           { "Diagnostic Code", "bfd.diag",
211             FT_UINT8, BASE_HEX, VALS(bfd_control_diag_values), 0x1f,
212             "", HFILL }
213         },
214         { &hf_bfd_flags,
215           { "Message Flags", "bfd.flags",
216             FT_UINT8, BASE_HEX, NULL, 0xf0,
217             "", HFILL }
218         },
219         { &hf_bfd_detect_time_multiplier,
220           { "Detect Time Multiplier", "bfd.detect_time_multiplier",
221             FT_UINT8, BASE_DEC, NULL, 0x0,
222             "", HFILL }
223         },
224         { &hf_bfd_my_discriminator,
225           { "My Discriminator", "bfd.my_discriminator",
226             FT_UINT32, BASE_HEX, NULL, 0x0,
227             "", HFILL }
228         },
229         { &hf_bfd_your_discriminator,
230           { "Your Discriminator", "bfd.your_discriminator",
231             FT_UINT32, BASE_HEX, NULL, 0x0,
232             "", HFILL }
233         },
234         { &hf_bfd_desired_min_tx_interval,
235           { "Desired Min TX Interval", "bfd.desired_min_tx_interval",
236             FT_UINT32, BASE_DEC, NULL, 0x0,
237             "", HFILL }
238         },
239         { &hf_bfd_required_min_rx_interval,
240           { "Required Min RX Interval", "bfd.required_min_rx_interval",
241             FT_UINT32, BASE_DEC, NULL, 0x0,
242             "", HFILL }
243         },
244         { &hf_bfd_required_min_echo_interval,
245           { "Required Min Echo Interval", "bfd.required_min_echo_interval",
246             FT_UINT32, BASE_DEC, NULL, 0x0,
247             "", HFILL }
248         },
249     };
250
251     /* Setup protocol subtree array */
252     static gint *ett[] = {
253         &ett_bfd,
254     };
255
256     /* Register the protocol name and description */
257     proto_bfd = proto_register_protocol("Bi-directional Fault Detection Control Message",
258                                         "BFD Control",
259                                         "bfdcontrol");
260
261     /* Required function calls to register the header fields and subtrees used */
262     proto_register_field_array(proto_bfd, hf, array_length(hf));
263     proto_register_subtree_array(ett, array_length(ett));
264 }
265
266 void
267 proto_reg_handoff_bfd(void)
268 {
269     dissector_handle_t bfd_control_handle;
270
271     bfd_control_handle = create_dissector_handle(dissect_bfd_control, proto_bfd);
272     dissector_add("udp.port", UDP_PORT_BFD_CONTROL, bfd_control_handle);
273 }