Update manuf to current IEEE entries.
[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.24 2002/08/28 21:00:16 jmayer 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 #include <string.h>
35 #include <glib.h>
36 #include <epan/packet.h>
37
38 static gint proto_hsrp = -1;
39
40 static gint hf_hsrp_version = -1;
41 static gint hf_hsrp_opcode = -1;
42 static gint hf_hsrp_state = -1;
43 static gint hf_hsrp_hellotime = -1;
44 static gint hf_hsrp_holdtime = -1;
45 static gint hf_hsrp_priority = -1;
46 static gint hf_hsrp_group = -1;
47 static gint hf_hsrp_reserved = -1;
48 static gint hf_hsrp_auth_data = -1;
49 static gint hf_hsrp_virt_ip_addr = -1;
50
51 static gint ett_hsrp = -1;
52
53 #define UDP_PORT_HSRP   1985
54
55 struct hsrp_packet {          /* Multicast to 224.0.0.2, TTL 1, UDP, port 1985 */
56         guint8  version;      /* RFC2281 describes version 0 */
57         guint8  opcode;
58         guint8  state;
59 #define HSRP_DEFAULT_HELLOTIME 3
60         guint8  hellotime;    /* In seconds */
61 #define HSRP_DEFAULT_HOLDTIME 10
62         guint8  holdtime;     /* In seconds */
63         guint8  priority;     /* Higher is stronger, highest IP address tie-breaker */
64         guint8  group;        /* Identifies the standby group */
65         guint8  reserved;
66         guint8  auth_data[8]; /* Clear-text password, recommended default is `cisco' */
67         guint32 virt_ip_addr; /* The virtual IP address used by this group */
68 };
69
70
71 #define HSRP_OPCODE_HELLO  0
72 #define HSRP_OPCODE_COUP   1
73 #define HSRP_OPCODE_RESIGN 2
74 static const value_string hsrp_opcode_vals[] = {
75         {HSRP_OPCODE_HELLO,  "Hello"},
76         {HSRP_OPCODE_COUP,   "Coup"},
77         {HSRP_OPCODE_RESIGN, "Resign"},
78         {0, NULL},
79 };
80
81 #define HSRP_STATE_INITIAL  0
82 #define HSRP_STATE_LEARN    1
83 #define HSRP_STATE_LISTEN   2
84 #define HSRP_STATE_SPEAK    4
85 #define HSRP_STATE_STANDBY  8
86 #define HSRP_STATE_ACTIVE  16
87 static const value_string hsrp_state_vals[] = {
88         {HSRP_STATE_INITIAL, "Initial"},
89         {HSRP_STATE_LEARN,   "Learn"},
90         {HSRP_STATE_LISTEN,  "Listen"},
91         {HSRP_STATE_SPEAK,   "Speak"},
92         {HSRP_STATE_STANDBY, "Standby"},
93         {HSRP_STATE_ACTIVE,  "Active"},
94         {0, NULL},
95 };
96
97 static void
98 dissect_hsrp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
99 {
100         guint8 opcode, state;
101
102         if (check_col(pinfo->cinfo, COL_PROTOCOL))
103                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "HSRP");
104         if (check_col(pinfo->cinfo, COL_INFO))
105                 col_clear(pinfo->cinfo, COL_INFO);
106
107         opcode = tvb_get_guint8(tvb, 1);
108         state = tvb_get_guint8(tvb, 2);
109         if (check_col(pinfo->cinfo, COL_INFO)) {
110                 col_add_fstr(pinfo->cinfo, COL_INFO, "%s (state %s)",
111                              val_to_str(opcode, hsrp_opcode_vals, "Unknown"),
112                              val_to_str(state, hsrp_state_vals, "Unknown"));
113         }
114
115         if (tree) {
116                 proto_item *ti;
117                 proto_tree *hsrp_tree;
118                 int offset;
119                 guint8 hellotime, holdtime;
120                 guint8 auth_buf[8 + 1];
121
122                 offset = 0;
123                 ti = proto_tree_add_item(tree, proto_hsrp, tvb, offset, -1, FALSE);
124                 hsrp_tree = proto_item_add_subtree(ti, ett_hsrp);
125
126                 proto_tree_add_item(hsrp_tree, hf_hsrp_version, tvb, offset, 1, FALSE);
127                 offset++;
128                 proto_tree_add_uint(hsrp_tree, hf_hsrp_opcode, tvb, offset, 1, opcode);
129                 offset++;
130                 proto_tree_add_uint(hsrp_tree, hf_hsrp_state, tvb, offset, 1, state);
131                 offset++;
132                 hellotime = tvb_get_guint8(tvb, offset);
133                 proto_tree_add_uint_format(hsrp_tree, hf_hsrp_hellotime, tvb, offset, 1, hellotime,
134                                            "Hellotime: %sDefault (%u)",
135                                            (hellotime == HSRP_DEFAULT_HELLOTIME) ? "" : "Non-",
136                                            hellotime);
137                 offset++;
138                 holdtime = tvb_get_guint8(tvb, offset);
139                 proto_tree_add_uint_format(hsrp_tree, hf_hsrp_holdtime, tvb, offset, 1, holdtime,
140                                            "Holdtime: %sDefault (%u)",
141                                            (holdtime == HSRP_DEFAULT_HOLDTIME) ? "" : "Non-",
142                                            holdtime);
143                 offset++;
144                 proto_tree_add_item(hsrp_tree, hf_hsrp_priority, tvb, offset, 1, FALSE);
145                 offset++;
146                 proto_tree_add_item(hsrp_tree, hf_hsrp_group, tvb, offset, 1, FALSE);
147                 offset++;
148                 proto_tree_add_item(hsrp_tree, hf_hsrp_reserved, tvb, offset, 1, FALSE);
149                 offset++;
150                 tvb_memcpy(tvb, auth_buf, offset, 8);
151                 auth_buf[sizeof auth_buf - 1] = '\0';
152                 proto_tree_add_string_format(hsrp_tree, hf_hsrp_auth_data, tvb, offset, 8, auth_buf,
153                                              "Authentication Data: %sDefault (%s)",
154                                              (tvb_strneql(tvb, offset, "cisco", strlen("cisco"))) == 0 ? "" : "Non-",
155                                              auth_buf);
156                 offset += 8;
157                 proto_tree_add_item(hsrp_tree, hf_hsrp_virt_ip_addr, tvb, offset, 4, FALSE);
158                 offset += 4;
159
160         }
161
162         return;
163 }
164
165 void proto_register_hsrp(void)
166 {
167         static hf_register_info hf[] = {
168                 { &hf_hsrp_version,
169                   { "Version", "hsrp.version",
170                     FT_UINT8, BASE_DEC, NULL, 0x0,
171                     "The version of the HSRP messages", HFILL }},
172
173                 { &hf_hsrp_opcode,
174                   { "Op Code", "hsrp.opcode",
175                     FT_UINT8, BASE_DEC, VALS(hsrp_opcode_vals), 0x0,
176                     "The type of message contained in this packet", HFILL }},
177
178                 { &hf_hsrp_state,
179                   { "State", "hsrp.state",
180                     FT_UINT8, BASE_DEC, VALS(hsrp_state_vals), 0x0,
181                     "The current state of the router sending the message", HFILL }},
182
183                 { &hf_hsrp_hellotime,
184                   { "Hellotime", "hsrp.hellotime",
185                     FT_UINT8, BASE_DEC, NULL, 0x0,
186                     "The approximate period between the Hello messages that the router sends", HFILL }},
187
188                 { &hf_hsrp_holdtime,
189                   { "Holdtime", "hsrp.holdtime",
190                     FT_UINT8, BASE_DEC, NULL, 0x0,
191                     "Time that the current Hello message should be considered valid", HFILL }},
192
193                 { &hf_hsrp_priority,
194                   { "Priority", "hsrp.priority",
195                     FT_UINT8, BASE_DEC, NULL, 0x0,
196                     "Used to elect the active and standby routers. Numerically higher priority wins vote", HFILL }},
197
198                 { &hf_hsrp_group,
199                   { "Group", "hsrp.group",
200                     FT_UINT8, BASE_DEC, NULL, 0x0,
201                     "This field identifies the standby group", HFILL }},
202
203                 { &hf_hsrp_reserved,
204                   { "Reserved", "hsrp.reserved",
205                     FT_UINT8, BASE_DEC, NULL, 0x0,
206                     "Reserved", HFILL }},
207
208                 { &hf_hsrp_auth_data,
209                   { "Authentication Data", "hsrp.auth_data",
210                     FT_STRING, 0, NULL, 0x0,
211                     "Contains a clear-text 8 character reused password", HFILL }},
212
213                 { &hf_hsrp_virt_ip_addr,
214                   { "Virtual IP Address", "hsrp.virt_ip",
215                     FT_IPv4, 0, NULL, 0x0,
216                     "The virtual IP address used by this group", HFILL }},
217
218         };
219
220         static gint *ett[] = {
221                 &ett_hsrp,
222         };
223
224         proto_hsrp = proto_register_protocol("Cisco Hot Standby Router Protocol",
225             "HSRP", "hsrp");
226         proto_register_field_array(proto_hsrp, hf, array_length(hf));
227         proto_register_subtree_array(ett, array_length(ett));
228
229         return;
230 }
231
232 void
233 proto_reg_handoff_hsrp(void)
234 {
235         dissector_handle_t hsrp_handle;
236
237         hsrp_handle = create_dissector_handle(dissect_hsrp, proto_hsrp);
238         dissector_add("udp.port", UDP_PORT_HSRP, hsrp_handle);
239 }