Conversation dissectors are called through a mechanism that doesn'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.14 2001/01/22 03:33:45 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         if (check_col(pinfo->fd, COL_PROTOCOL))
96                 col_set_str(pinfo->fd, COL_PROTOCOL, "VRRP");
97         if (check_col(pinfo->fd, COL_INFO))
98                 col_clear(pinfo->fd, COL_INFO);
99         
100         ver_type = tvb_get_guint8(tvb, 0);
101         if (check_col(pinfo->fd, COL_INFO)) {
102                 col_add_fstr(pinfo->fd, COL_INFO, "%s (v%u)",
103                              "Announcement", hi_nibble(ver_type));
104         }
105
106         if (tree) {
107                 proto_item *ti, *tv;
108                 proto_tree *vrrp_tree, *ver_type_tree;
109                 guint8 priority, ip_count, auth_type, adver_int;
110                 guint16 cksum, computed_cksum;
111                 guint8 auth_buf[VRRP_AUTH_DATA_LEN+1];
112
113                 ti = proto_tree_add_item(tree, proto_vrrp, tvb, 0,
114                                          tvb_length(tvb), FALSE);
115                 vrrp_tree = proto_item_add_subtree(ti, ett_vrrp);
116
117                 tv = proto_tree_add_uint_format(vrrp_tree, hf_vrrp_ver_type,
118                                                 tvb, offset, 1, ver_type,
119                                                 "Version %u, Packet type %u (%s)",
120                                                 hi_nibble(ver_type), lo_nibble(ver_type),
121                                                 val_to_str(lo_nibble(ver_type), vrrp_type_vals, "Unknown"));
122                 ver_type_tree = proto_item_add_subtree(tv, ett_vrrp_ver_type);
123                 proto_tree_add_uint(ver_type_tree, hf_vrrp_version, tvb,
124                                     offset, 1, ver_type);
125                 proto_tree_add_uint(ver_type_tree, hf_vrrp_type, tvb, offset, 1,
126                                     ver_type);
127                 offset++;
128                 
129                 proto_tree_add_text(vrrp_tree, tvb, offset, 1,
130                                     "Virtual Router ID: %u",
131                                     tvb_get_guint8(tvb, offset));
132                 offset++;
133
134                 priority = tvb_get_guint8(tvb, offset);
135                 proto_tree_add_text(vrrp_tree, tvb, offset, 1, "Priority: %u (%s)",
136                                     priority,
137                                     val_to_str(priority, vrrp_prio_vals, "Non-default backup priority"));
138                 offset++;
139
140                 ip_count = tvb_get_guint8(tvb, offset);
141                 proto_tree_add_text(vrrp_tree, tvb, offset, 1,
142                                     "Count IP Addrs: %u", ip_count);
143                 offset++;
144
145                 auth_type = tvb_get_guint8(tvb, offset);
146                 proto_tree_add_text(vrrp_tree, tvb, offset, 1,
147                                     "Authentication Type: %u (%s)", auth_type,
148                                     val_to_str(auth_type, vrrp_auth_vals, "Unknown"));
149                 offset++;
150
151                 adver_int = tvb_get_guint8(tvb, offset);
152                 proto_tree_add_text(vrrp_tree, tvb, offset, 1,
153                                     "Advertisement Interval: %u second%s",
154                                     adver_int, plurality(adver_int, "", "s"));
155                 offset++;
156
157                 cksum = tvb_get_ntohs(tvb, offset);
158                 vrrp_len = tvb_reported_length(tvb);
159                 if (!pinfo->fragmented && 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                         cksum_vec[0].ptr = tvb_get_ptr(tvb, 0, vrrp_len);
163                         cksum_vec[0].len = vrrp_len;
164                         computed_cksum = in_cksum(&cksum_vec[0], 1);
165                         if (computed_cksum == 0) {
166                                 proto_tree_add_text(vrrp_tree, tvb, offset, 2,
167                                                     "Checksum: 0x%04x (correct)",
168                                                     cksum);
169                         } else {
170                                 proto_tree_add_text(vrrp_tree, tvb, offset, 2,
171                                                     "Checksum: 0x%04x (incorrect, should be 0x%04x)",
172                                                     cksum,
173                                                     in_cksum_shouldbe(cksum, computed_cksum));
174                         }
175                 } else {
176                         proto_tree_add_text(vrrp_tree, tvb, offset, 2,
177                                             "Checksum: 0x%04x", cksum);
178                 }
179                 offset+=2;
180
181                 while (ip_count > 0) {
182                         proto_tree_add_text(vrrp_tree, tvb, offset, 4,
183                                             "Virtual Router IP address: %s",
184                                             ip_to_str(tvb_get_ptr(tvb, offset, 4)));
185                         offset+=4;
186                         ip_count--;
187                 }
188
189                 if (auth_type != VRRP_AUTH_TYPE_SIMPLE_TEXT)
190                         return; /* Contents of the authentication data is undefined */
191
192                 tvb_get_nstringz0(tvb, offset, VRRP_AUTH_DATA_LEN, auth_buf);
193                 if (auth_buf[0] != '\0')
194                         proto_tree_add_text(vrrp_tree, tvb, offset,
195                                             VRRP_AUTH_DATA_LEN,
196                                             "Authentication string: `%s'",
197                                             auth_buf);
198                 offset+=8;
199         }
200 }
201
202 void proto_register_vrrp(void)
203 {
204         static hf_register_info hf[] = {
205                 { &hf_vrrp_ver_type,
206                   {"VRRP message version and type", "vrrp.typever",
207                    FT_UINT8, BASE_DEC, NULL, 0x0,
208                    "VRRP version and type"}},
209
210                 { &hf_vrrp_version,
211                   {"VRRP protocol version", "vrrp.version",
212                    FT_UINT8, BASE_DEC, NULL, VRRP_VERSION_MASK,
213                    "VRRP version"}},
214
215                 { &hf_vrrp_type,
216                   {"VRRP packet type", "vrrp.type",
217                    FT_UINT8, BASE_DEC, VALS(vrrp_type_vals), VRRP_TYPE_MASK,
218                    "VRRP type"}}
219         };
220
221         static gint *ett[] = {
222                 &ett_vrrp,
223                 &ett_vrrp_ver_type
224         };
225
226         proto_vrrp = proto_register_protocol("Virtual Router Redundancy Protocol",
227             "VRRP", "vrrp");
228         proto_register_field_array(proto_vrrp, hf, array_length(hf));
229         proto_register_subtree_array(ett, array_length(ett));
230
231         return;
232 }
233
234 void
235 proto_reg_handoff_vrrp(void)
236 {
237         dissector_add("ip.proto", IP_PROTO_VRRP, dissect_vrrp, proto_vrrp);
238 }