Show the "negotiable/non-negotiable" flags as such.
[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.23 2002/03/28 07:40:54 itojun 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 static gint hf_vrrp_ip6 = -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[4];
100         guint32 phdr[2];
101
102         if (check_col(pinfo->cinfo, COL_PROTOCOL))
103                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "VRRP");
104         if (check_col(pinfo->cinfo, COL_INFO))
105                 col_clear(pinfo->cinfo, COL_INFO);
106         
107         ver_type = tvb_get_guint8(tvb, 0);
108         if (check_col(pinfo->cinfo, COL_INFO)) {
109                 col_add_fstr(pinfo->cinfo, COL_INFO, "%s (v%u)",
110                              "Announcement", hi_nibble(ver_type));
111         }
112
113         if (tree) {
114                 proto_item *ti, *tv;
115                 proto_tree *vrrp_tree, *ver_type_tree;
116                 guint8 priority, ip_count = 0, auth_type;
117                 guint16 cksum, computed_cksum;
118                 guint8 auth_buf[VRRP_AUTH_DATA_LEN+1];
119
120                 ti = proto_tree_add_item(tree, proto_vrrp, tvb, 0, -1, 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                 switch(hi_nibble(ver_type)) {
145                 case 3:
146                         /* Skip reserve field */
147                         offset++;
148                         break;
149                 case 2:
150                 default:
151                         ip_count = tvb_get_guint8(tvb, offset);
152                         proto_tree_add_uint(vrrp_tree, hf_vrrp_count_ip, tvb,
153                             offset, 1, ip_count);
154                         offset++;
155                         break;
156                 }
157
158                 auth_type = tvb_get_guint8(tvb, offset);
159                 proto_tree_add_item(vrrp_tree, hf_vrrp_auth_type, tvb, offset, 1, FALSE);
160                 offset++;
161
162                 proto_tree_add_item(vrrp_tree, hf_vrrp_adver_int, tvb, offset, 1, FALSE);
163                 offset++;
164
165                 cksum = tvb_get_ntohs(tvb, offset);
166                 vrrp_len = (gint)tvb_reported_length(tvb);
167                 if (!pinfo->fragmented && (gint)tvb_length(tvb) >= vrrp_len) {
168                         /* The packet isn't part of a fragmented datagram
169                            and isn't truncated, so we can checksum it. */
170                         switch(hi_nibble(ver_type)) {
171                         case 3:
172                                 /* Set up the fields of the pseudo-header. */
173                                 cksum_vec[0].ptr = pinfo->src.data;
174                                 cksum_vec[0].len = pinfo->src.len;
175                                 cksum_vec[1].ptr = pinfo->dst.data;
176                                 cksum_vec[1].len = pinfo->dst.len;
177                                 cksum_vec[2].ptr = (const guint8 *)&phdr;
178                                 phdr[0] = htonl(vrrp_len);
179                                 phdr[1] = htonl(IP_PROTO_VRRP);
180                                 cksum_vec[2].len = 8;
181                                 cksum_vec[3].ptr = tvb_get_ptr(tvb, 0, vrrp_len);
182                                 cksum_vec[3].len = vrrp_len;
183                                 computed_cksum = in_cksum(cksum_vec, 4);
184                                 break;
185                         case 2:
186                         default:
187                                 cksum_vec[0].ptr = tvb_get_ptr(tvb, 0, vrrp_len);
188                                 cksum_vec[0].len = vrrp_len;
189                                 computed_cksum = in_cksum(&cksum_vec[0], 1);
190                                 break;
191                         }
192                         if (computed_cksum == 0) {
193                                 proto_tree_add_text(vrrp_tree, tvb, offset, 2,
194                                                     "Checksum: 0x%04x (correct)",
195                                                     cksum);
196                         } else {
197                                 proto_tree_add_text(vrrp_tree, tvb, offset, 2,
198                                                     "Checksum: 0x%04x (incorrect, should be 0x%04x)",
199                                                     cksum,
200                                                     in_cksum_shouldbe(cksum, computed_cksum));
201                         }
202                 } else {
203                         proto_tree_add_text(vrrp_tree, tvb, offset, 2,
204                                             "Checksum: 0x%04x", cksum);
205                 }
206                 offset+=2;
207
208                 switch(hi_nibble(ver_type)) {
209                 case 3:
210                         proto_tree_add_item(vrrp_tree, hf_vrrp_ip6, tvb, offset, 16, FALSE);
211                         offset+=16;
212                         break;
213                 case 2:
214                 default:
215                         while (ip_count > 0) {
216                                 proto_tree_add_item(vrrp_tree, hf_vrrp_ip, tvb,
217                                     offset, 4, FALSE);
218                                 offset+=4;
219                                 ip_count--;
220                         }
221                         break;
222                 }
223                 if (auth_type != VRRP_AUTH_TYPE_SIMPLE_TEXT)
224                         return; /* Contents of the authentication data is undefined */
225
226                 tvb_get_nstringz0(tvb, offset, VRRP_AUTH_DATA_LEN, auth_buf);
227                 if (auth_buf[0] != '\0')
228                         proto_tree_add_text(vrrp_tree, tvb, offset,
229                                             VRRP_AUTH_DATA_LEN,
230                                             "Authentication string: `%s'",
231                                             auth_buf);
232                 offset+=8;
233         }
234 }
235
236 void proto_register_vrrp(void)
237 {
238         static hf_register_info hf[] = {
239                 { &hf_vrrp_ver_type,
240                   {"VRRP message version and type", "vrrp.typever",
241                    FT_UINT8, BASE_DEC, NULL, 0x0,
242                    "VRRP version and type", HFILL }},
243
244                 { &hf_vrrp_version,
245                   {"VRRP protocol version", "vrrp.version",
246                    FT_UINT8, BASE_DEC, NULL, VRRP_VERSION_MASK,
247                    "VRRP version", HFILL }},
248
249                 { &hf_vrrp_type,
250                   {"VRRP packet type", "vrrp.type",
251                    FT_UINT8, BASE_DEC, VALS(vrrp_type_vals), VRRP_TYPE_MASK,
252                    "VRRP type", HFILL }},
253
254                 { &hf_vrrp_virt_rtr_id,
255                   {"Virtual Rtr ID", "vrrp.virt_rtr_id",
256                    FT_UINT8, BASE_DEC, NULL, 0x0,
257                    "Virtual router this packet is reporting status for", HFILL }},
258
259                 { &hf_vrrp_prio,
260                   {"Priority", "vrrp.prio",
261                    FT_UINT8, BASE_DEC, NULL, 0x0,
262                    "Sending VRRP router's priority for the virtual router", HFILL }},
263
264                 { &hf_vrrp_count_ip,
265                   {"Count IP Addrs", "vrrp.count_ip_addrs",
266                    FT_UINT8, BASE_DEC, NULL, 0x0,
267                    "The number of IP addresses contained in this VRRP advertisement", HFILL }},
268
269                 { &hf_vrrp_auth_type,
270                   {"Auth Type", "vrrp.auth_type",
271                    FT_UINT8, BASE_DEC, VALS(vrrp_auth_vals), 0x0,
272                    "The authentication method being utilized", HFILL }},
273
274                 { &hf_vrrp_adver_int,
275                   {"Adver Int", "vrrp.adver_int",
276                    FT_UINT8, BASE_DEC, NULL, 0x0,
277                    "Time interval (in seconds) between ADVERTISEMENTS", HFILL }},
278
279                 { &hf_vrrp_ip,
280                   {"IP Address", "vrrp.ip_addr",
281                    FT_IPv4, 0, NULL, 0x0,
282                    "IP address associated with the virtual router", HFILL }},
283
284                 { &hf_vrrp_ip6,
285                   {"IPv6 Address", "vrrp.ipv6_addr",
286                    FT_IPv6, 0, NULL, 0x0,
287                    "IPv6 address associated with the virtual router", HFILL }},
288         };
289
290         static gint *ett[] = {
291                 &ett_vrrp,
292                 &ett_vrrp_ver_type
293         };
294
295         proto_vrrp = proto_register_protocol("Virtual Router Redundancy Protocol",
296             "VRRP", "vrrp");
297         proto_register_field_array(proto_vrrp, hf, array_length(hf));
298         proto_register_subtree_array(ett, array_length(ett));
299
300         return;
301 }
302
303 void
304 proto_reg_handoff_vrrp(void)
305 {
306         dissector_handle_t vrrp_handle;
307
308         vrrp_handle = create_dissector_handle(dissect_vrrp, proto_vrrp);
309         dissector_add("ip.proto", IP_PROTO_VRRP, vrrp_handle);
310 }