On Windows, put Ethereal configuration files under the "Application
[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.17 2001/07/12 19:43:59 guy Exp $
8  *
9  * Ethereal - Network traffic analyzer
10  * By Gerald Combs <gerald@ethereal.com>
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 "ipproto.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 static gint hf_vrrp_virt_rtr_id = -1;
55 static gint hf_vrrp_prio = -1;
56 static gint hf_vrrp_count_ip = -1;
57 static gint hf_vrrp_auth_type = -1;
58 static gint hf_vrrp_adver_int = -1;
59 static gint hf_vrrp_ip = -1;
60
61 #define VRRP_VERSION_MASK 0xf0
62 #define VRRP_TYPE_MASK 0x0f
63 #define VRRP_AUTH_DATA_LEN 8
64
65 #define VRRP_TYPE_ADVERTISEMENT 1
66 static const value_string vrrp_type_vals[] = {
67         {VRRP_TYPE_ADVERTISEMENT, "Advertisement"},
68         {0, NULL}
69 };
70
71 #define VRRP_AUTH_TYPE_NONE 0
72 #define VRRP_AUTH_TYPE_SIMPLE_TEXT 1
73 #define VRRP_AUTH_TYPE_IP_AUTH_HDR 2
74 static const value_string vrrp_auth_vals[] = {
75         {VRRP_AUTH_TYPE_NONE,        "No Authentication"},
76         {VRRP_AUTH_TYPE_SIMPLE_TEXT, "Simple Text Authentication"},
77         {VRRP_AUTH_TYPE_IP_AUTH_HDR, "IP Authentication Header"},
78         {0,                          NULL}
79 };
80
81 #define VRRP_PRIORITY_MASTER_STOPPING 0
82 /* Values between 1 and 254 inclusive are for backup VRRP routers */
83 #define VRRP_PRIORITY_DEFAULT 100
84 #define VRRP_PRIORITY_OWNER 255
85 static const value_string vrrp_prio_vals[] = {
86         {VRRP_PRIORITY_MASTER_STOPPING,  "Current Master has stopped participating in VRRP"},
87         {VRRP_PRIORITY_DEFAULT,          "Default priority for a backup VRRP router"},
88         {VRRP_PRIORITY_OWNER,            "This VRRP router owns the virtual router's IP address(es)"},
89         {0,                              NULL }
90 };
91
92
93 static void
94 dissect_vrrp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
95 {
96         int offset = 0;
97         gint vrrp_len;
98         guint8  ver_type;
99         vec_t cksum_vec[1];
100
101         if (check_col(pinfo->fd, COL_PROTOCOL))
102                 col_set_str(pinfo->fd, COL_PROTOCOL, "VRRP");
103         if (check_col(pinfo->fd, COL_INFO))
104                 col_clear(pinfo->fd, COL_INFO);
105         
106         ver_type = tvb_get_guint8(tvb, 0);
107         if (check_col(pinfo->fd, COL_INFO)) {
108                 col_add_fstr(pinfo->fd, COL_INFO, "%s (v%u)",
109                              "Announcement", hi_nibble(ver_type));
110         }
111
112         if (tree) {
113                 proto_item *ti, *tv;
114                 proto_tree *vrrp_tree, *ver_type_tree;
115                 guint8 priority, ip_count, auth_type;
116                 guint16 cksum, computed_cksum;
117                 guint8 auth_buf[VRRP_AUTH_DATA_LEN+1];
118
119                 ti = proto_tree_add_item(tree, proto_vrrp, tvb, 0,
120                                          tvb_length(tvb), FALSE);
121                 vrrp_tree = proto_item_add_subtree(ti, ett_vrrp);
122
123                 tv = proto_tree_add_uint_format(vrrp_tree, hf_vrrp_ver_type,
124                                                 tvb, offset, 1, ver_type,
125                                                 "Version %u, Packet type %u (%s)",
126                                                 hi_nibble(ver_type), lo_nibble(ver_type),
127                                                 val_to_str(lo_nibble(ver_type), vrrp_type_vals, "Unknown"));
128                 ver_type_tree = proto_item_add_subtree(tv, ett_vrrp_ver_type);
129                 proto_tree_add_uint(ver_type_tree, hf_vrrp_version, tvb,
130                                     offset, 1, ver_type);
131                 proto_tree_add_uint(ver_type_tree, hf_vrrp_type, tvb, offset, 1,
132                                     ver_type);
133                 offset++;
134                 
135                 proto_tree_add_item(vrrp_tree, hf_vrrp_virt_rtr_id, tvb, offset, 1, FALSE);
136                 offset++;
137
138                 priority = tvb_get_guint8(tvb, offset);
139                 proto_tree_add_uint_format(vrrp_tree, hf_vrrp_prio, tvb, offset, 1, priority, "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_uint(vrrp_tree, hf_vrrp_count_ip, tvb, offset, 1, ip_count);
146                 offset++;
147
148                 auth_type = tvb_get_guint8(tvb, offset);
149                 proto_tree_add_item(vrrp_tree, hf_vrrp_auth_type, tvb, offset, 1, FALSE);
150                 offset++;
151
152                 proto_tree_add_item(vrrp_tree, hf_vrrp_adver_int, tvb, offset, 1, FALSE);
153                 offset++;
154
155                 cksum = tvb_get_ntohs(tvb, offset);
156                 vrrp_len = (gint)tvb_reported_length(tvb);
157                 if (!pinfo->fragmented && (gint)tvb_length(tvb) >= vrrp_len) {
158                         /* The packet isn't part of a fragmented datagram
159                            and isn't truncated, so we can checksum it. */
160                         cksum_vec[0].ptr = tvb_get_ptr(tvb, 0, vrrp_len);
161                         cksum_vec[0].len = vrrp_len;
162                         computed_cksum = in_cksum(&cksum_vec[0], 1);
163                         if (computed_cksum == 0) {
164                                 proto_tree_add_text(vrrp_tree, tvb, offset, 2,
165                                                     "Checksum: 0x%04x (correct)",
166                                                     cksum);
167                         } else {
168                                 proto_tree_add_text(vrrp_tree, tvb, offset, 2,
169                                                     "Checksum: 0x%04x (incorrect, should be 0x%04x)",
170                                                     cksum,
171                                                     in_cksum_shouldbe(cksum, computed_cksum));
172                         }
173                 } else {
174                         proto_tree_add_text(vrrp_tree, tvb, offset, 2,
175                                             "Checksum: 0x%04x", cksum);
176                 }
177                 offset+=2;
178
179                 while (ip_count > 0) {
180                         proto_tree_add_item(vrrp_tree, hf_vrrp_ip, tvb, offset, 4, FALSE);
181                         offset+=4;
182                         ip_count--;
183                 }
184
185                 if (auth_type != VRRP_AUTH_TYPE_SIMPLE_TEXT)
186                         return; /* Contents of the authentication data is undefined */
187
188                 tvb_get_nstringz0(tvb, offset, VRRP_AUTH_DATA_LEN, auth_buf);
189                 if (auth_buf[0] != '\0')
190                         proto_tree_add_text(vrrp_tree, tvb, offset,
191                                             VRRP_AUTH_DATA_LEN,
192                                             "Authentication string: `%s'",
193                                             auth_buf);
194                 offset+=8;
195         }
196 }
197
198 void proto_register_vrrp(void)
199 {
200         static hf_register_info hf[] = {
201                 { &hf_vrrp_ver_type,
202                   {"VRRP message version and type", "vrrp.typever",
203                    FT_UINT8, BASE_DEC, NULL, 0x0,
204                    "VRRP version and type", HFILL }},
205
206                 { &hf_vrrp_version,
207                   {"VRRP protocol version", "vrrp.version",
208                    FT_UINT8, BASE_DEC, NULL, VRRP_VERSION_MASK,
209                    "VRRP version", HFILL }},
210
211                 { &hf_vrrp_type,
212                   {"VRRP packet type", "vrrp.type",
213                    FT_UINT8, BASE_DEC, VALS(vrrp_type_vals), VRRP_TYPE_MASK,
214                    "VRRP type", HFILL }},
215
216                 { &hf_vrrp_virt_rtr_id,
217                   {"Virtual Rtr ID", "vrrp.virt_rtr_id",
218                    FT_UINT8, BASE_DEC, NULL, 0x0,
219                    "Virtual router this packet is reporting status for", HFILL }},
220
221                 { &hf_vrrp_prio,
222                   {"Priority", "vrrp.prio",
223                    FT_UINT8, BASE_DEC, NULL, 0x0,
224                    "Sending VRRP router's priority for the virtual router", HFILL }},
225
226                 { &hf_vrrp_count_ip,
227                   {"Count IP Addrs", "vrrp.count_ip_addrs",
228                    FT_UINT8, BASE_DEC, NULL, 0x0,
229                    "The number of IP addresses contained in this VRRP advertisement", HFILL }},
230
231                 { &hf_vrrp_auth_type,
232                   {"Auth Type", "vrrp.auth_type",
233                    FT_UINT8, BASE_DEC, VALS(vrrp_auth_vals), 0x0,
234                    "The authentication method being utilized", HFILL }},
235
236                 { &hf_vrrp_adver_int,
237                   {"Adver Int", "vrrp.adver_int",
238                    FT_UINT8, BASE_DEC, NULL, 0x0,
239                    "Time interval (in seconds) between ADVERTISEMENTS", HFILL }},
240
241                 { &hf_vrrp_ip,
242                   {"IP Address", "vrrp.ip_addr",
243                    FT_IPv4, 0, NULL, 0x0,
244                    "IP address associated with the virtual router", HFILL }},
245
246         };
247
248         static gint *ett[] = {
249                 &ett_vrrp,
250                 &ett_vrrp_ver_type
251         };
252
253         proto_vrrp = proto_register_protocol("Virtual Router Redundancy Protocol",
254             "VRRP", "vrrp");
255         proto_register_field_array(proto_vrrp, hf, array_length(hf));
256         proto_register_subtree_array(ett, array_length(ett));
257
258         return;
259 }
260
261 void
262 proto_reg_handoff_vrrp(void)
263 {
264         dissector_add("ip.proto", IP_PROTO_VRRP, dissect_vrrp, proto_vrrp);
265 }