Tvbuffify the VRRP dissector, and add code to check the checksum.
[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.12 2001/01/06 05:43:13 guy Exp $
8  *
9  * Ethereal - Network traffic analyzer
10  * By Gerald Combs <gerald@zing.org>
11  * Copyright 1998 Gerald Combs
12  *
13  * 
14  * This program is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU General Public License
16  * as published by the Free Software Foundation; either version 2
17  * of the License, or (at your option) any later version.
18  * 
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  * 
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
27  */
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #ifdef HAVE_SYS_TYPES_H
34 # include <sys/types.h>
35 #endif
36
37 #ifdef HAVE_NETINET_IN_H
38 #include <netinet/in.h>
39 #endif
40
41 #include <string.h>
42 #include <glib.h>
43 #include "packet.h"
44 #include "packet-ip.h"
45 #include "in_cksum.h"
46
47 static gint proto_vrrp = -1;
48 static gint ett_vrrp = -1;
49 static gint ett_vrrp_ver_type = -1;
50
51 static gint hf_vrrp_ver_type = -1;
52 static gint hf_vrrp_version = -1;
53 static gint hf_vrrp_type = -1;
54
55 #define VRRP_VERSION_MASK 0xf0
56 #define VRRP_TYPE_MASK 0x0f
57 #define VRRP_AUTH_DATA_LEN 8
58
59 #define VRRP_TYPE_ADVERTISEMENT 1
60 static const value_string vrrp_type_vals[] = {
61         {VRRP_TYPE_ADVERTISEMENT, "Advertisement"},
62         {0, NULL}
63 };
64
65 #define VRRP_AUTH_TYPE_NONE 0
66 #define VRRP_AUTH_TYPE_SIMPLE_TEXT 1
67 #define VRRP_AUTH_TYPE_IP_AUTH_HDR 2
68 static const value_string vrrp_auth_vals[] = {
69         {VRRP_AUTH_TYPE_NONE,        "No Authentication"},
70         {VRRP_AUTH_TYPE_SIMPLE_TEXT, "Simple Text Authentication"},
71         {VRRP_AUTH_TYPE_IP_AUTH_HDR, "IP Authentication Header"},
72         {0,                          NULL}
73 };
74
75 #define VRRP_PRIORITY_MASTER_STOPPING 0
76 /* Values between 1 and 254 inclusive are for backup VRRP routers */
77 #define VRRP_PRIORITY_DEFAULT 100
78 #define VRRP_PRIORITY_OWNER 255
79 static const value_string vrrp_prio_vals[] = {
80         {VRRP_PRIORITY_MASTER_STOPPING,  "Current Master has stopped participating in VRRP"},
81         {VRRP_PRIORITY_DEFAULT,          "Default priority for a backup VRRP router"},
82         {VRRP_PRIORITY_OWNER,            "This VRRP router owns the virtual router's IP address(es)"},
83         {0,                              NULL }
84 };
85
86
87 static void
88 dissect_vrrp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
89 {
90         int offset = 0;
91         gint vrrp_len;
92         guint8  ver_type;
93         vec_t cksum_vec[1];
94
95         CHECK_DISPLAY_AS_DATA(proto_vrrp, tvb, pinfo, tree);
96
97         pinfo->current_proto = "VRRP";
98
99         if (check_col(pinfo->fd, COL_PROTOCOL))
100                 col_set_str(pinfo->fd, COL_PROTOCOL, "VRRP");
101         if (check_col(pinfo->fd, COL_INFO))
102                 col_clear(pinfo->fd, COL_INFO);
103         
104         ver_type = tvb_get_guint8(tvb, 0);
105         if (check_col(pinfo->fd, COL_INFO)) {
106                 col_add_fstr(pinfo->fd, COL_INFO, "%s (v%u)",
107                              "Announcement", hi_nibble(ver_type));
108         }
109
110         if (tree) {
111                 proto_item *ti, *tv;
112                 proto_tree *vrrp_tree, *ver_type_tree;
113                 guint8 priority, ip_count, auth_type, adver_int;
114                 guint16 cksum, computed_cksum;
115                 guint8 auth_buf[VRRP_AUTH_DATA_LEN+1];
116
117                 ti = proto_tree_add_item(tree, proto_vrrp, tvb, 0,
118                                          tvb_length(tvb), FALSE);
119                 vrrp_tree = proto_item_add_subtree(ti, ett_vrrp);
120
121                 tv = proto_tree_add_uint_format(vrrp_tree, hf_vrrp_ver_type,
122                                                 tvb, offset, 1, ver_type,
123                                                 "Version %u, Packet type %u (%s)",
124                                                 hi_nibble(ver_type), lo_nibble(ver_type),
125                                                 val_to_str(lo_nibble(ver_type), vrrp_type_vals, "Unknown"));
126                 ver_type_tree = proto_item_add_subtree(tv, ett_vrrp_ver_type);
127                 proto_tree_add_uint(ver_type_tree, hf_vrrp_version, tvb,
128                                     offset, 1, ver_type);
129                 proto_tree_add_uint(ver_type_tree, hf_vrrp_type, tvb, offset, 1,
130                                     ver_type);
131                 offset++;
132                 
133                 proto_tree_add_text(vrrp_tree, tvb, offset, 1,
134                                     "Virtual Router ID: %u",
135                                     tvb_get_guint8(tvb, offset));
136                 offset++;
137
138                 priority = tvb_get_guint8(tvb, offset);
139                 proto_tree_add_text(vrrp_tree, tvb, offset, 1, "Priority: %u (%s)",
140                                     priority,
141                                     val_to_str(priority, vrrp_prio_vals, "Non-default backup priority"));
142                 offset++;
143
144                 ip_count = tvb_get_guint8(tvb, offset);
145                 proto_tree_add_text(vrrp_tree, tvb, offset, 1,
146                                     "Count IP Addrs: %u", ip_count);
147                 offset++;
148
149                 auth_type = tvb_get_guint8(tvb, offset);
150                 proto_tree_add_text(vrrp_tree, tvb, offset, 1,
151                                     "Authentication Type: %u (%s)", auth_type,
152                                     val_to_str(auth_type, vrrp_auth_vals, "Unknown"));
153                 offset++;
154
155                 adver_int = tvb_get_guint8(tvb, offset);
156                 proto_tree_add_text(vrrp_tree, tvb, offset, 1,
157                                     "Advertisement Interval: %u second%s",
158                                     adver_int, plurality(adver_int, "", "s"));
159                 offset++;
160
161                 cksum = tvb_get_ntohs(tvb, offset);
162                 vrrp_len = tvb_reported_length(tvb);
163                 if (!pinfo->fragmented && tvb_length(tvb) >= vrrp_len) {
164                         /* The packet isn't part of a fragmented datagram
165                            and isn't truncated, so we can checksum it. */
166                         cksum_vec[0].ptr = tvb_get_ptr(tvb, 0, vrrp_len);
167                         cksum_vec[0].len = vrrp_len;
168                         computed_cksum = in_cksum(&cksum_vec[0], 1);
169                         if (computed_cksum == 0) {
170                                 proto_tree_add_text(vrrp_tree, tvb, offset, 2,
171                                                     "Checksum: 0x%04x (correct)",
172                                                     cksum);
173                         } else {
174                                 proto_tree_add_text(vrrp_tree, tvb, offset, 2,
175                                                     "Checksum: 0x%04x (incorrect, should be 0x%04x)",
176                                                     cksum,
177                                                     in_cksum_shouldbe(cksum, computed_cksum));
178                         }
179                 } else {
180                         proto_tree_add_text(vrrp_tree, tvb, offset, 2,
181                                             "Checksum: 0x%04x", cksum);
182                 }
183                 offset+=2;
184
185                 while (ip_count > 0) {
186                         proto_tree_add_text(vrrp_tree, tvb, offset, 4,
187                                             "Virtual Router IP address: %s",
188                                             ip_to_str(tvb_get_ptr(tvb, offset, 4)));
189                         offset+=4;
190                         ip_count--;
191                 }
192
193                 if (auth_type != VRRP_AUTH_TYPE_SIMPLE_TEXT)
194                         return; /* Contents of the authentication data is undefined */
195
196                 tvb_get_nstringz0(tvb, offset, VRRP_AUTH_DATA_LEN, auth_buf);
197                 if (auth_buf[0] != '\0')
198                         proto_tree_add_text(vrrp_tree, tvb, offset,
199                                             VRRP_AUTH_DATA_LEN,
200                                             "Authentication string: `%s'",
201                                             auth_buf);
202                 offset+=8;
203         }
204 }
205
206 void proto_register_vrrp(void)
207 {
208         static hf_register_info hf[] = {
209                 { &hf_vrrp_ver_type,
210                   {"VRRP message version and type", "vrrp.typever",
211                    FT_UINT8, BASE_DEC, NULL, 0x0,
212                    "VRRP version and type"}},
213
214                 { &hf_vrrp_version,
215                   {"VRRP protocol version", "vrrp.version",
216                    FT_UINT8, BASE_DEC, NULL, VRRP_VERSION_MASK,
217                    "VRRP version"}},
218
219                 { &hf_vrrp_type,
220                   {"VRRP packet type", "vrrp.type",
221                    FT_UINT8, BASE_DEC, VALS(vrrp_type_vals), VRRP_TYPE_MASK,
222                    "VRRP type"}}
223         };
224
225         static gint *ett[] = {
226                 &ett_vrrp,
227                 &ett_vrrp_ver_type
228         };
229
230         proto_vrrp = proto_register_protocol("Virtual Router Redundancy Protocol",
231             "VRRP", "vrrp");
232         proto_register_field_array(proto_vrrp, hf, array_length(hf));
233         proto_register_subtree_array(ett, array_length(ett));
234
235         return;
236 }
237
238 void
239 proto_reg_handoff_vrrp(void)
240 {
241         dissector_add("ip.proto", IP_PROTO_VRRP, dissect_vrrp);
242 }