"hex_str_to_bytes()" modifies the GByteArray supplied to it, so don't
[obnox/wireshark/wip.git] / packet-vrrp.c
1 /* packet-vrrp.c
2  * Routines for the Virtual Router Redundancy Protocol (VRRP)
3  * RFC2338
4  *
5  * Heikki Vatiainen <hessu@cs.tut.fi>
6  *
7  * $Id: packet-vrrp.c,v 1.27 2003/06/12 06:58:38 guy Exp $
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 <string.h>
33 #include <glib.h>
34 #include <epan/packet.h>
35 #include "ipproto.h"
36 #include "in_cksum.h"
37
38 static gint proto_vrrp = -1;
39 static gint ett_vrrp = -1;
40 static gint ett_vrrp_ver_type = -1;
41
42 static gint hf_vrrp_ver_type = -1;
43 static gint hf_vrrp_version = -1;
44 static gint hf_vrrp_type = -1;
45 static gint hf_vrrp_virt_rtr_id = -1;
46 static gint hf_vrrp_prio = -1;
47 static gint hf_vrrp_count_ip = -1;
48 static gint hf_vrrp_auth_type = -1;
49 static gint hf_vrrp_adver_int = -1;
50 static gint hf_vrrp_ip = -1;
51 static gint hf_vrrp_ip6 = -1;
52
53 #define VRRP_VERSION_MASK 0xf0
54 #define VRRP_TYPE_MASK 0x0f
55 #define VRRP_AUTH_DATA_LEN 8
56
57 #define VRRP_TYPE_ADVERTISEMENT 1
58 static const value_string vrrp_type_vals[] = {
59         {VRRP_TYPE_ADVERTISEMENT, "Advertisement"},
60         {0, NULL}
61 };
62
63 #define VRRP_AUTH_TYPE_NONE 0
64 #define VRRP_AUTH_TYPE_SIMPLE_TEXT 1
65 #define VRRP_AUTH_TYPE_IP_AUTH_HDR 2
66 static const value_string vrrp_auth_vals[] = {
67         {VRRP_AUTH_TYPE_NONE,        "No Authentication"},
68         {VRRP_AUTH_TYPE_SIMPLE_TEXT, "Simple Text Authentication"},
69         {VRRP_AUTH_TYPE_IP_AUTH_HDR, "IP Authentication Header"},
70         {0,                          NULL}
71 };
72
73 #define VRRP_PRIORITY_MASTER_STOPPING 0
74 /* Values between 1 and 254 inclusive are for backup VRRP routers */
75 #define VRRP_PRIORITY_DEFAULT 100
76 #define VRRP_PRIORITY_OWNER 255
77 static const value_string vrrp_prio_vals[] = {
78         {VRRP_PRIORITY_MASTER_STOPPING,  "Current Master has stopped participating in VRRP"},
79         {VRRP_PRIORITY_DEFAULT,          "Default priority for a backup VRRP router"},
80         {VRRP_PRIORITY_OWNER,            "This VRRP router owns the virtual router's IP address(es)"},
81         {0,                              NULL }
82 };
83
84
85 static void
86 dissect_vrrp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
87 {
88         int offset = 0;
89         gint vrrp_len;
90         guint8  ver_type;
91         vec_t cksum_vec[4];
92         guint32 phdr[2];
93
94         if (check_col(pinfo->cinfo, COL_PROTOCOL))
95                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "VRRP");
96         if (check_col(pinfo->cinfo, COL_INFO))
97                 col_clear(pinfo->cinfo, COL_INFO);
98
99         ver_type = tvb_get_guint8(tvb, 0);
100         if (check_col(pinfo->cinfo, COL_INFO)) {
101                 col_add_fstr(pinfo->cinfo, COL_INFO, "%s (v%u)",
102                              "Announcement", hi_nibble(ver_type));
103         }
104
105         if (tree) {
106                 proto_item *ti, *tv;
107                 proto_tree *vrrp_tree, *ver_type_tree;
108                 guint8 priority, ip_count = 0, auth_type;
109                 guint16 cksum, computed_cksum;
110                 guint8 auth_buf[VRRP_AUTH_DATA_LEN + 1];
111
112                 ti = proto_tree_add_item(tree, proto_vrrp, tvb, 0, -1, FALSE);
113                 vrrp_tree = proto_item_add_subtree(ti, ett_vrrp);
114
115                 tv = proto_tree_add_uint_format(vrrp_tree, hf_vrrp_ver_type,
116                                                 tvb, offset, 1, ver_type,
117                                                 "Version %u, Packet type %u (%s)",
118                                                 hi_nibble(ver_type), lo_nibble(ver_type),
119                                                 val_to_str(lo_nibble(ver_type), vrrp_type_vals, "Unknown"));
120                 ver_type_tree = proto_item_add_subtree(tv, ett_vrrp_ver_type);
121                 proto_tree_add_uint(ver_type_tree, hf_vrrp_version, tvb,
122                                     offset, 1, ver_type);
123                 proto_tree_add_uint(ver_type_tree, hf_vrrp_type, tvb, offset, 1,
124                                     ver_type);
125                 offset++;
126
127                 proto_tree_add_item(vrrp_tree, hf_vrrp_virt_rtr_id, tvb, offset, 1, FALSE);
128                 offset++;
129
130                 priority = tvb_get_guint8(tvb, offset);
131                 proto_tree_add_uint_format(vrrp_tree, hf_vrrp_prio, tvb, offset, 1, priority, "Priority: %u (%s)",
132                                            priority,
133                                            val_to_str(priority, vrrp_prio_vals, "Non-default backup priority"));
134                 offset++;
135
136                 switch(hi_nibble(ver_type)) {
137                 case 3:
138                         /* Skip reserve field */
139                         offset++;
140                         break;
141                 case 2:
142                 default:
143                         ip_count = tvb_get_guint8(tvb, offset);
144                         proto_tree_add_uint(vrrp_tree, hf_vrrp_count_ip, tvb,
145                             offset, 1, ip_count);
146                         offset++;
147                         break;
148                 }
149
150                 auth_type = tvb_get_guint8(tvb, offset);
151                 proto_tree_add_item(vrrp_tree, hf_vrrp_auth_type, tvb, offset, 1, FALSE);
152                 offset++;
153
154                 proto_tree_add_item(vrrp_tree, hf_vrrp_adver_int, tvb, offset, 1, FALSE);
155                 offset++;
156
157                 cksum = tvb_get_ntohs(tvb, offset);
158                 vrrp_len = (gint)tvb_reported_length(tvb);
159                 if (!pinfo->fragmented && (gint)tvb_length(tvb) >= vrrp_len) {
160                         /* The packet isn't part of a fragmented datagram
161                            and isn't truncated, so we can checksum it. */
162                         switch(hi_nibble(ver_type)) {
163                         case 3:
164                                 /* Set up the fields of the pseudo-header. */
165                                 cksum_vec[0].ptr = pinfo->src.data;
166                                 cksum_vec[0].len = pinfo->src.len;
167                                 cksum_vec[1].ptr = pinfo->dst.data;
168                                 cksum_vec[1].len = pinfo->dst.len;
169                                 cksum_vec[2].ptr = (const guint8 *)&phdr;
170                                 phdr[0] = g_htonl(vrrp_len);
171                                 phdr[1] = g_htonl(IP_PROTO_VRRP);
172                                 cksum_vec[2].len = 8;
173                                 cksum_vec[3].ptr = tvb_get_ptr(tvb, 0, vrrp_len);
174                                 cksum_vec[3].len = vrrp_len;
175                                 computed_cksum = in_cksum(cksum_vec, 4);
176                                 break;
177                         case 2:
178                         default:
179                                 cksum_vec[0].ptr = tvb_get_ptr(tvb, 0, vrrp_len);
180                                 cksum_vec[0].len = vrrp_len;
181                                 computed_cksum = in_cksum(&cksum_vec[0], 1);
182                                 break;
183                         }
184                         if (computed_cksum == 0) {
185                                 proto_tree_add_text(vrrp_tree, tvb, offset, 2,
186                                                     "Checksum: 0x%04x (correct)",
187                                                     cksum);
188                         } else {
189                                 proto_tree_add_text(vrrp_tree, tvb, offset, 2,
190                                                     "Checksum: 0x%04x (incorrect, should be 0x%04x)",
191                                                     cksum,
192                                                     in_cksum_shouldbe(cksum, computed_cksum));
193                         }
194                 } else {
195                         proto_tree_add_text(vrrp_tree, tvb, offset, 2,
196                                             "Checksum: 0x%04x", cksum);
197                 }
198                 offset+=2;
199
200                 switch(hi_nibble(ver_type)) {
201                 case 3:
202                         proto_tree_add_item(vrrp_tree, hf_vrrp_ip6, tvb, offset, 16, FALSE);
203                         offset+=16;
204                         break;
205                 case 2:
206                 default:
207                         while (ip_count > 0) {
208                                 proto_tree_add_item(vrrp_tree, hf_vrrp_ip, tvb,
209                                     offset, 4, FALSE);
210                                 offset+=4;
211                                 ip_count--;
212                         }
213                         break;
214                 }
215                 if (auth_type != VRRP_AUTH_TYPE_SIMPLE_TEXT)
216                         return; /* Contents of the authentication data is undefined */
217
218                 tvb_get_nstringz0(tvb, offset, sizeof auth_buf, auth_buf);
219                 if (auth_buf[0] != '\0')
220                         proto_tree_add_text(vrrp_tree, tvb, offset,
221                                             VRRP_AUTH_DATA_LEN,
222                                             "Authentication string: `%s'",
223                                             auth_buf);
224                 offset+=8;
225         }
226 }
227
228 void proto_register_vrrp(void)
229 {
230         static hf_register_info hf[] = {
231                 { &hf_vrrp_ver_type,
232                   {"VRRP message version and type", "vrrp.typever",
233                    FT_UINT8, BASE_DEC, NULL, 0x0,
234                    "VRRP version and type", HFILL }},
235
236                 { &hf_vrrp_version,
237                   {"VRRP protocol version", "vrrp.version",
238                    FT_UINT8, BASE_DEC, NULL, VRRP_VERSION_MASK,
239                    "VRRP version", HFILL }},
240
241                 { &hf_vrrp_type,
242                   {"VRRP packet type", "vrrp.type",
243                    FT_UINT8, BASE_DEC, VALS(vrrp_type_vals), VRRP_TYPE_MASK,
244                    "VRRP type", HFILL }},
245
246                 { &hf_vrrp_virt_rtr_id,
247                   {"Virtual Rtr ID", "vrrp.virt_rtr_id",
248                    FT_UINT8, BASE_DEC, NULL, 0x0,
249                    "Virtual router this packet is reporting status for", HFILL }},
250
251                 { &hf_vrrp_prio,
252                   {"Priority", "vrrp.prio",
253                    FT_UINT8, BASE_DEC, NULL, 0x0,
254                    "Sending VRRP router's priority for the virtual router", HFILL }},
255
256                 { &hf_vrrp_count_ip,
257                   {"Count IP Addrs", "vrrp.count_ip_addrs",
258                    FT_UINT8, BASE_DEC, NULL, 0x0,
259                    "The number of IP addresses contained in this VRRP advertisement", HFILL }},
260
261                 { &hf_vrrp_auth_type,
262                   {"Auth Type", "vrrp.auth_type",
263                    FT_UINT8, BASE_DEC, VALS(vrrp_auth_vals), 0x0,
264                    "The authentication method being utilized", HFILL }},
265
266                 { &hf_vrrp_adver_int,
267                   {"Adver Int", "vrrp.adver_int",
268                    FT_UINT8, BASE_DEC, NULL, 0x0,
269                    "Time interval (in seconds) between ADVERTISEMENTS", HFILL }},
270
271                 { &hf_vrrp_ip,
272                   {"IP Address", "vrrp.ip_addr",
273                    FT_IPv4, 0, NULL, 0x0,
274                    "IP address associated with the virtual router", HFILL }},
275
276                 { &hf_vrrp_ip6,
277                   {"IPv6 Address", "vrrp.ipv6_addr",
278                    FT_IPv6, 0, NULL, 0x0,
279                    "IPv6 address associated with the virtual router", HFILL }},
280         };
281
282         static gint *ett[] = {
283                 &ett_vrrp,
284                 &ett_vrrp_ver_type
285         };
286
287         proto_vrrp = proto_register_protocol("Virtual Router Redundancy Protocol",
288             "VRRP", "vrrp");
289         proto_register_field_array(proto_vrrp, hf, array_length(hf));
290         proto_register_subtree_array(ett, array_length(ett));
291
292         return;
293 }
294
295 void
296 proto_reg_handoff_vrrp(void)
297 {
298         dissector_handle_t vrrp_handle;
299
300         vrrp_handle = create_dissector_handle(dissect_vrrp, proto_vrrp);
301         dissector_add("ip.proto", IP_PROTO_VRRP, vrrp_handle);
302 }