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