80ea29066f30a4e6276a00c4d7a96e7b17430a0b
[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.20 2001/12/10 00:25:28 guy Exp $
8  *
9  * Ethereal - Network traffic analyzer
10  * By Gerald Combs <gerald@ethereal.com>
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         {0, NULL},
87 };
88
89 #define HSRP_STATE_INITIAL  0
90 #define HSRP_STATE_LEARN    1
91 #define HSRP_STATE_LISTEN   2
92 #define HSRP_STATE_SPEAK    4
93 #define HSRP_STATE_STANDBY  8
94 #define HSRP_STATE_ACTIVE  16
95 static const value_string hsrp_state_vals[] = {
96         {HSRP_STATE_INITIAL, "Initial"},
97         {HSRP_STATE_LEARN,   "Learn"},
98         {HSRP_STATE_LISTEN,  "Listen"},
99         {HSRP_STATE_SPEAK,   "Speak"},
100         {HSRP_STATE_STANDBY, "Standby"},
101         {HSRP_STATE_ACTIVE,  "Active"},
102         {0, NULL},
103 };
104
105 static void
106 dissect_hsrp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
107 {
108         guint8 opcode, state;
109
110         if (check_col(pinfo->cinfo, COL_PROTOCOL))
111                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "HSRP");
112         if (check_col(pinfo->cinfo, COL_INFO))
113                 col_clear(pinfo->cinfo, COL_INFO);
114         
115         opcode = tvb_get_guint8(tvb, 1);
116         state = tvb_get_guint8(tvb, 2);
117         if (check_col(pinfo->cinfo, COL_INFO)) {
118                 col_add_fstr(pinfo->cinfo, 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
130                 offset = 0;
131                 ti = proto_tree_add_item(tree, proto_hsrp, tvb, offset, tvb_length(tvb), FALSE);
132                 hsrp_tree = proto_item_add_subtree(ti, ett_hsrp);
133
134                 proto_tree_add_item(hsrp_tree, hf_hsrp_version, tvb, offset, 1, FALSE);
135                 offset++;
136                 proto_tree_add_uint(hsrp_tree, hf_hsrp_opcode, tvb, offset, 1, opcode);
137                 offset++;
138                 proto_tree_add_uint(hsrp_tree, hf_hsrp_state, tvb, offset, 1, state);
139                 offset++;
140                 hellotime = tvb_get_guint8(tvb, offset);
141                 proto_tree_add_uint_format(hsrp_tree, hf_hsrp_hellotime, tvb, offset, 1, hellotime,
142                                            "Hellotime: %sDefault (%u)",
143                                            (hellotime == HSRP_DEFAULT_HELLOTIME) ? "" : "Non-",
144                                            hellotime);
145                 offset++;
146                 holdtime = tvb_get_guint8(tvb, offset);
147                 proto_tree_add_uint_format(hsrp_tree, hf_hsrp_holdtime, tvb, offset, 1, holdtime,
148                                            "Holdtime: %sDefault (%u)",
149                                            (holdtime == HSRP_DEFAULT_HOLDTIME) ? "" : "Non-",
150                                            holdtime);
151                 offset++;
152                 proto_tree_add_item(hsrp_tree, hf_hsrp_priority, tvb, offset, 1, FALSE);
153                 offset++;
154                 proto_tree_add_item(hsrp_tree, hf_hsrp_group, tvb, offset, 1, FALSE);
155                 offset++;
156                 proto_tree_add_item(hsrp_tree, hf_hsrp_reserved, tvb, offset, 1, FALSE);
157                 offset++;
158                 tvb_memcpy(tvb, auth_buf, offset, 8);
159                 auth_buf[sizeof auth_buf - 1] = '\0';
160                 proto_tree_add_string_format(hsrp_tree, hf_hsrp_auth_data, tvb, offset, 8, auth_buf,
161                                              "Authentication Data: %sDefault (%s)",
162                                              (tvb_strneql(tvb, offset, "cisco", strlen("cisco"))) == 0 ? "" : "Non-",
163                                              auth_buf);
164                 offset += 8;
165                 proto_tree_add_item(hsrp_tree, hf_hsrp_virt_ip_addr, tvb, offset, 4, FALSE);
166                 offset += 4;
167                 
168         }
169
170         return;
171 }
172
173 void proto_register_hsrp(void)
174 {
175         static hf_register_info hf[] = {
176                 { &hf_hsrp_version,
177                   { "Version", "hsrp.version",  
178                     FT_UINT8, BASE_DEC, NULL, 0x0,
179                     "The version of the HSRP messages", HFILL }},
180
181                 { &hf_hsrp_opcode,
182                   { "Op Code", "hsrp.opcode",
183                     FT_UINT8, BASE_DEC, VALS(hsrp_opcode_vals), 0x0,
184                     "The type of message contained in this packet", HFILL }},
185
186                 { &hf_hsrp_state,
187                   { "State", "hsrp.state",
188                     FT_UINT8, BASE_DEC, VALS(hsrp_state_vals), 0x0,
189                     "The current state of the router sending the message", HFILL }},
190
191                 { &hf_hsrp_hellotime,
192                   { "Hellotime", "hsrp.hellotime",
193                     FT_UINT8, BASE_DEC, NULL, 0x0,
194                     "The approximate period between the Hello messages that the router sends", HFILL }},
195
196                 { &hf_hsrp_holdtime,
197                   { "Holdtime", "hsrp.holdtime",
198                     FT_UINT8, BASE_DEC, NULL, 0x0,
199                     "Time that the current Hello message should be considered valid", HFILL }},
200
201                 { &hf_hsrp_priority,
202                   { "Priority", "hsrp.priority",
203                     FT_UINT8, BASE_DEC, NULL, 0x0,
204                     "Used to elect the active and standby routers. Numerically higher priority wins vote", HFILL }},
205
206                 { &hf_hsrp_group,
207                   { "Group", "hsrp.group",
208                     FT_UINT8, BASE_DEC, NULL, 0x0,
209                     "This field identifies the standby group", HFILL }},
210
211                 { &hf_hsrp_reserved,
212                   { "Reserved", "hsrp.reserved",
213                     FT_UINT8, BASE_DEC, NULL, 0x0,
214                     "Reserved", HFILL }},
215
216                 { &hf_hsrp_auth_data,
217                   { "Authentication Data", "hsrp.auth_data",
218                     FT_STRING, 0, NULL, 0x0,
219                     "Contains a clear-text 8 character reused password", HFILL }},
220
221                 { &hf_hsrp_virt_ip_addr,
222                   { "Virtual IP Address", "hsrp.virt_ip",
223                     FT_IPv4, 0, NULL, 0x0,
224                     "The virtual IP address used by this group", HFILL }},
225
226         };
227
228         static gint *ett[] = {
229                 &ett_hsrp,
230         };
231
232         proto_hsrp = proto_register_protocol("Cisco Hot Standby Router Protocol",
233             "HSRP", "hsrp");
234         proto_register_field_array(proto_hsrp, hf, array_length(hf));
235         proto_register_subtree_array(ett, array_length(ett));
236
237         return;
238 }
239
240 void
241 proto_reg_handoff_hsrp(void)
242 {
243         dissector_handle_t hsrp_handle;
244
245         hsrp_handle = create_dissector_handle(dissect_hsrp, proto_hsrp);
246         dissector_add("udp.port", UDP_PORT_HSRP, hsrp_handle);
247 }