bgp route refresh/MP capability option.
[obnox/wireshark/wip.git] / packet-hsrp.c
1 /* packet-hsrp.c
2  * Routines for the Cisco Hot Standby Router Protocol (HSRP)
3  * RFC 2281
4  *
5  * Heikki Vatiainen <hessu@cs.tut.fi>
6  *
7  * $Id: packet-hsrp.c,v 1.12 2000/12/02 08:41:07 guy Exp $
8  *
9  * Ethereal - Network traffic analyzer
10  * By Gerald Combs <gerald@zing.org>
11  * Copyright 1998 Gerald Combs
12  *
13  * Copied from packet-vrrp.c
14  * 
15  * This program is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU General Public License
17  * as published by the Free Software Foundation; either version 2
18  * of the License, or (at your option) any later version.
19  * 
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU General Public License for more details.
24  * 
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
28  */
29
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33
34 #ifdef HAVE_SYS_TYPES_H
35 # include <sys/types.h>
36 #endif
37
38 #ifdef HAVE_NETINET_IN_H
39 #include <netinet/in.h>
40 #endif
41
42 #include <string.h>
43 #include <glib.h>
44 #include "packet.h"
45
46 static gint proto_hsrp = -1;
47
48 static gint hf_hsrp_version = -1;
49 static gint hf_hsrp_opcode = -1;
50 static gint hf_hsrp_state = -1;
51 static gint hf_hsrp_hellotime = -1;
52 static gint hf_hsrp_holdtime = -1;
53 static gint hf_hsrp_priority = -1;
54 static gint hf_hsrp_group = -1;
55 static gint hf_hsrp_reserved = -1;
56 static gint hf_hsrp_auth_data = -1;
57 static gint hf_hsrp_virt_ip_addr = -1;
58
59 static gint ett_hsrp = -1;
60
61 #define UDP_PORT_HSRP   1985
62
63 struct hsrp_packet {          /* Multicast to 224.0.0.2, TTL 1, UDP, port 1985 */
64         guint8  version;      /* RFC2281 describes version 0 */
65         guint8  opcode;
66         guint8  state;
67 #define HSRP_DEFAULT_HELLOTIME 3
68         guint8  hellotime;    /* In seconds */
69 #define HSRP_DEFAULT_HOLDTIME 10
70         guint8  holdtime;     /* In seconds */
71         guint8  priority;     /* Higher is stronger, highest IP address tie-breaker */
72         guint8  group;        /* Identifies the standby group */
73         guint8  reserved;
74         guint8  auth_data[8]; /* Clear-text password, recommended default is `cisco' */
75         guint32 virt_ip_addr; /* The virtual IP address used by this group */
76 };
77
78
79 #define HSRP_OPCODE_HELLO  0
80 #define HSRP_OPCODE_COUP   1
81 #define HSRP_OPCODE_RESIGN 2
82 static const value_string hsrp_opcode_vals[] = {
83         {HSRP_OPCODE_HELLO,  "Hello"},
84         {HSRP_OPCODE_COUP,   "Coup"},
85         {HSRP_OPCODE_RESIGN, "Resign"}
86 };
87
88 #define HSRP_STATE_INITIAL  0
89 #define HSRP_STATE_LEARN    1
90 #define HSRP_STATE_LISTEN   2
91 #define HSRP_STATE_SPEAK    4
92 #define HSRP_STATE_STANDBY  8
93 #define HSRP_STATE_ACTIVE  16
94 static const value_string hsrp_state_vals[] = {
95         {HSRP_STATE_INITIAL, "Initial"},
96         {HSRP_STATE_LEARN,   "Learn"},
97         {HSRP_STATE_LISTEN,  "Listen"},
98         {HSRP_STATE_SPEAK,   "Speak"},
99         {HSRP_STATE_STANDBY, "Standby"},
100         {HSRP_STATE_ACTIVE,  "Active"}
101 };
102
103 static void
104 dissect_hsrp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
105 {
106         guint8 opcode, state;
107
108         CHECK_DISPLAY_AS_DATA(proto_hsrp, tvb, pinfo, tree);
109
110         pinfo->current_proto = "HSRP";
111
112         if (check_col(pinfo->fd, COL_PROTOCOL))
113                 col_set_str(pinfo->fd, COL_PROTOCOL, "HSRP");
114         
115         opcode = tvb_get_guint8(tvb, 1);
116         state = tvb_get_guint8(tvb, 2);
117         if (check_col(pinfo->fd, COL_INFO)) {
118                 col_add_fstr(pinfo->fd, COL_INFO, "%s (state %s)",
119                              val_to_str(opcode, hsrp_opcode_vals, "Unknown"),
120                              val_to_str(state, hsrp_state_vals, "Unknown"));
121         }
122
123         if (tree) {
124                 proto_item *ti;
125                 proto_tree *hsrp_tree;
126                 int offset;
127                 guint8 hellotime, holdtime;
128                 guint8 auth_buf[8 + 1];
129                 guint32 virt_ip_addr;
130
131                 offset = 0;
132                 ti = proto_tree_add_item(tree, proto_hsrp, tvb, offset, tvb_length(tvb), FALSE);
133                 hsrp_tree = proto_item_add_subtree(ti, ett_hsrp);
134
135                 proto_tree_add_uint(hsrp_tree, hf_hsrp_version, tvb, offset, 1, tvb_get_guint8(tvb, offset));
136                 offset++;
137                 proto_tree_add_uint(hsrp_tree, hf_hsrp_opcode, tvb, offset, 1, opcode);
138                 offset++;
139                 proto_tree_add_uint(hsrp_tree, hf_hsrp_state, tvb, offset, 1, state);
140                 offset++;
141                 hellotime = tvb_get_guint8(tvb, offset);
142                 proto_tree_add_uint_format(hsrp_tree, hf_hsrp_hellotime, tvb, offset, 1, hellotime,
143                                            "Hellotime: %sDefault (%u)",
144                                            (hellotime == HSRP_DEFAULT_HELLOTIME) ? "" : "Non-",
145                                            hellotime);
146                 offset++;
147                 holdtime = tvb_get_guint8(tvb, offset);
148                 proto_tree_add_uint_format(hsrp_tree, hf_hsrp_holdtime, tvb, offset, 1, holdtime,
149                                            "Holdtime: %sDefault (%u)",
150                                            (holdtime == HSRP_DEFAULT_HOLDTIME) ? "" : "Non-",
151                                            holdtime);
152                 offset++;
153                 proto_tree_add_item(hsrp_tree, hf_hsrp_priority, tvb, offset, 1, tvb_get_guint8(tvb, offset));
154                 offset++;
155                 proto_tree_add_item(hsrp_tree, hf_hsrp_group, tvb, offset, 1, tvb_get_guint8(tvb, offset));
156                 offset++;
157                 proto_tree_add_item(hsrp_tree, hf_hsrp_reserved, tvb, offset, 1, tvb_get_guint8(tvb, offset));
158                 offset++;
159                 tvb_memcpy(tvb, auth_buf, offset, 8);
160                 auth_buf[sizeof auth_buf - 1] = '\0';
161                 proto_tree_add_string_format(hsrp_tree, hf_hsrp_auth_data, tvb, offset, 8, auth_buf,
162                                              "Authentication Data: %sDefault (%s)",
163                                              (tvb_strneql(tvb, offset, "cisco", strlen("cisco"))) == 0 ? "" : "Non-",
164                                              auth_buf);
165                 offset += 8;
166                 tvb_memcpy(tvb, (guint8 *)&virt_ip_addr, offset, 4);
167                 proto_tree_add_ipv4(hsrp_tree, hf_hsrp_virt_ip_addr, tvb, offset, 4, virt_ip_addr);
168                 offset += 4;
169                 
170         }
171
172         return;
173 }
174
175 void proto_register_hsrp(void)
176 {
177         static hf_register_info hf[] = {
178                 { &hf_hsrp_version,
179                   { "Version", "hsrp.version",  
180                     FT_UINT8, BASE_DEC, NULL, 0x0,
181                     "The version of the HSRP messages"}},
182
183                 { &hf_hsrp_opcode,
184                   { "Op Code", "hsrp.opcode",
185                     FT_UINT8, BASE_DEC, VALS(hsrp_opcode_vals), 0x0,
186                     "The type of message contained in this packet" }},
187
188                 { &hf_hsrp_state,
189                   { "State", "hsrp.state",
190                     FT_UINT8, BASE_DEC, VALS(hsrp_state_vals), 0x0,
191                     "The current state of the router sending the message" }},
192
193                 { &hf_hsrp_hellotime,
194                   { "Hellotime", "hsrp.hellotime",
195                     FT_UINT8, BASE_DEC, NULL, 0x0,
196                     "The approximate period between the Hello messages that the router sends" }},
197
198                 { &hf_hsrp_holdtime,
199                   { "Holdtime", "hsrp.holdtime",
200                     FT_UINT8, BASE_DEC, NULL, 0x0,
201                     "Time that the current Hello message should be considered valid" }},
202
203                 { &hf_hsrp_priority,
204                   { "Priority", "hsrp.priority",
205                     FT_UINT8, BASE_DEC, NULL, 0x0,
206                     "Used to elect the active and standby routers. Numerically higher priority wins vote" }},
207
208                 { &hf_hsrp_group,
209                   { "Group", "hsrp.group",
210                     FT_UINT8, BASE_DEC, NULL, 0x0,
211                     "This field identifies the standby group" }},
212
213                 { &hf_hsrp_reserved,
214                   { "Reserved", "hsrp.reserved",
215                     FT_UINT8, BASE_DEC, NULL, 0x0,
216                     "Reserved" }},
217
218                 { &hf_hsrp_auth_data,
219                   { "Authentication Data", "hsrp.auth_data",
220                     FT_STRING, 0, NULL, 0x0,
221                     "Contains a clear-text 8 character reused password" }},
222
223                 { &hf_hsrp_virt_ip_addr,
224                   { "Virtual IP Address", "hsrp.virt_ip",
225                     FT_IPv4, 0, NULL, 0x0,
226                     "The virtual IP address used by this group" }},
227
228         };
229
230         static gint *ett[] = {
231                 &ett_hsrp,
232         };
233
234         proto_hsrp = proto_register_protocol("Cisco Hot Standby Router Protocol", "hsrp");
235         proto_register_field_array(proto_hsrp, hf, array_length(hf));
236         proto_register_subtree_array(ett, array_length(ett));
237
238         return;
239 }
240
241 void
242 proto_reg_handoff_hsrp(void)
243 {
244         dissector_add("udp.port", UDP_PORT_HSRP, dissect_hsrp);
245 }