MSWSP: fix dissect_mswsp_smb()
[wireshark-wip.git] / epan / dissectors / packet-rip.c
blobb44d09373b52aabc68b95d807f2ed4553cb0b18e
1 /* packet-rip.c
2 * Routines for RIPv1 and RIPv2 packet disassembly
3 * RFC1058 (STD 34), RFC1388, RFC1723, RFC2453 (STD 56)
4 * (c) Copyright Hannes R. Boehm <hannes@boehm.org>
6 * RFC2082 ( Keyed Message Digest Algorithm )
7 * Emanuele Caratti <wiz@iol.it>
9 * $Id$
11 * Wireshark - Network traffic analyzer
12 * By Gerald Combs <gerald@wireshark.org>
13 * Copyright 1998 Gerald Combs
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
30 #define NEW_PROTO_TREE_API
32 #include "config.h"
34 #include <glib.h>
35 #include <epan/packet.h>
36 #include <epan/emem.h>
37 #include <epan/prefs.h>
39 #define UDP_PORT_RIP 520
41 #define RIPv1 1
42 #define RIPv2 2
44 static const value_string version_vals[] = {
45 { RIPv1, "RIPv1" },
46 { RIPv2, "RIPv2" },
47 { 0, NULL }
50 static const value_string command_vals[] = {
51 { 1, "Request" },
52 { 2, "Response" },
53 { 3, "Traceon" },
54 { 4, "Traceoff" },
55 { 5, "Vendor specific (Sun)" },
56 { 0, NULL }
59 #define AFVAL_UNSPEC 0
60 #define AFVAL_IP 2
62 static const value_string family_vals[] = {
63 { AFVAL_UNSPEC, "Unspecified" },
64 { AFVAL_IP, "IP" },
65 { 0, NULL }
68 #define AUTH_IP_ROUTE 1
69 #define AUTH_PASSWORD 2
70 #define AUTH_KEYED_MSG_DIGEST 3
72 static const value_string rip_auth_type[] = {
73 { AUTH_IP_ROUTE, "IP Route" },
74 { AUTH_PASSWORD, "Simple Password" },
75 { AUTH_KEYED_MSG_DIGEST, "Keyed Message Digest" },
76 { 0, NULL }
79 #define RIP_HEADER_LENGTH 4
80 #define RIP_ENTRY_LENGTH 20
81 #define MD5_AUTH_DATA_LEN 16
83 static gboolean pref_display_routing_domain = FALSE;
85 void proto_reg_handoff_rip(void);
88 static dissector_handle_t rip_handle;
90 static header_field_info *hfi_rip = NULL;
92 #define RIP_HFI_INIT HFI_INIT(proto_rip)
94 static header_field_info hfi_rip_command RIP_HFI_INIT =
95 { "Command", "rip.command", FT_UINT8, BASE_DEC,
96 VALS(command_vals), 0, "What type of RIP Command is this", HFILL };
98 static header_field_info hfi_rip_version RIP_HFI_INIT =
99 { "Version", "rip.version", FT_UINT8, BASE_DEC,
100 VALS(version_vals), 0, "Version of the RIP protocol", HFILL };
102 static header_field_info hfi_rip_routing_domain RIP_HFI_INIT =
103 { "Routing Domain", "rip.routing_domain", FT_UINT16, BASE_DEC,
104 NULL, 0, "RIPv2 Routing Domain", HFILL };
106 static header_field_info hfi_rip_ip RIP_HFI_INIT =
107 { "IP Address", "rip.ip", FT_IPv4, BASE_NONE,
108 NULL, 0, NULL, HFILL};
110 static header_field_info hfi_rip_netmask RIP_HFI_INIT =
111 { "Netmask", "rip.netmask", FT_IPv4, BASE_NONE,
112 NULL, 0, NULL, HFILL};
114 static header_field_info hfi_rip_next_hop RIP_HFI_INIT =
115 { "Next Hop", "rip.next_hop", FT_IPv4, BASE_NONE,
116 NULL, 0, "Next Hop router for this route", HFILL};
118 static header_field_info hfi_rip_metric RIP_HFI_INIT =
119 { "Metric", "rip.metric", FT_UINT16, BASE_DEC,
120 NULL, 0, "Metric for this route", HFILL };
122 static header_field_info hfi_rip_auth RIP_HFI_INIT =
123 { "Authentication type", "rip.auth.type", FT_UINT16, BASE_DEC,
124 VALS(rip_auth_type), 0, "Type of authentication", HFILL };
126 static header_field_info hfi_rip_auth_passwd RIP_HFI_INIT =
127 { "Password", "rip.auth.passwd", FT_STRING, BASE_NONE,
128 NULL, 0, "Authentication password", HFILL };
130 static header_field_info hfi_rip_family RIP_HFI_INIT =
131 { "Address Family", "rip.family", FT_UINT16, BASE_DEC,
132 VALS(family_vals), 0, NULL, HFILL };
134 static header_field_info hfi_rip_route_tag RIP_HFI_INIT =
135 { "Route Tag", "rip.route_tag", FT_UINT16, BASE_DEC,
136 NULL, 0, NULL, HFILL };
138 static gint ett_rip = -1;
139 static gint ett_rip_vec = -1;
140 static gint ett_auth_vec = -1;
142 static void dissect_unspec_rip_vektor(tvbuff_t *tvb, int offset, guint8 version,
143 proto_tree *tree);
144 static void dissect_ip_rip_vektor(tvbuff_t *tvb, int offset, guint8 version,
145 proto_tree *tree);
146 static gint dissect_rip_authentication(tvbuff_t *tvb, int offset,
147 proto_tree *tree);
149 static void
150 dissect_rip(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
152 int offset = 0;
153 proto_tree *rip_tree = NULL;
154 proto_item *ti;
155 guint8 command;
156 guint8 version;
157 guint16 family;
158 gint trailer_len = 0;
159 gboolean is_md5_auth = FALSE;
161 col_set_str(pinfo->cinfo, COL_PROTOCOL, "RIP");
162 col_clear(pinfo->cinfo, COL_INFO);
164 command = tvb_get_guint8(tvb, 0);
165 version = tvb_get_guint8(tvb, 1);
167 col_set_str(pinfo->cinfo, COL_PROTOCOL,
168 val_to_str_const(version, version_vals, "RIP"));
169 col_add_str(pinfo->cinfo, COL_INFO,
170 val_to_str(command, command_vals, "Unknown command (%u)"));
172 if (tree) {
173 ti = proto_tree_add_item(tree, hfi_rip, tvb, 0, -1, ENC_NA);
174 rip_tree = proto_item_add_subtree(ti, ett_rip);
176 proto_tree_add_uint(rip_tree, &hfi_rip_command, tvb, 0, 1, command);
177 proto_tree_add_uint(rip_tree, &hfi_rip_version, tvb, 1, 1, version);
178 if (version == RIPv2 && pref_display_routing_domain == TRUE)
179 proto_tree_add_uint(rip_tree, &hfi_rip_routing_domain, tvb, 2, 2,
180 tvb_get_ntohs(tvb, 2));
182 /* skip header */
183 offset = RIP_HEADER_LENGTH;
185 /* zero or more entries */
186 while (tvb_reported_length_remaining(tvb, offset) > trailer_len ) {
187 family = tvb_get_ntohs(tvb, offset);
188 switch (family) {
189 case AFVAL_UNSPEC: /* Unspecified */
191 * There should be one entry in the request, and a metric
192 * of infinity, meaning "show the entire routing table".
194 dissect_unspec_rip_vektor(tvb, offset, version, rip_tree);
195 break;
196 case AFVAL_IP: /* IP */
197 dissect_ip_rip_vektor(tvb, offset, version, rip_tree);
198 break;
199 case 0xFFFF:
200 if( offset == RIP_HEADER_LENGTH ) {
201 trailer_len=dissect_rip_authentication(tvb, offset, rip_tree);
202 is_md5_auth = TRUE;
203 break;
205 if(is_md5_auth && tvb_reported_length_remaining(tvb, offset) == 20)
206 break;
207 /* Intentional fall through: auth Entry MUST be the first! */
208 default:
209 proto_tree_add_text(rip_tree, tvb, offset,
210 RIP_ENTRY_LENGTH, "Unknown address family %u",
211 family);
212 break;
215 offset += RIP_ENTRY_LENGTH;
220 static void
221 dissect_unspec_rip_vektor(tvbuff_t *tvb, int offset, guint8 version,
222 proto_tree *tree)
224 proto_item *ti;
225 proto_tree *rip_vektor_tree;
226 guint32 metric;
228 metric = tvb_get_ntohl(tvb, offset+16);
229 ti = proto_tree_add_text(tree, tvb, offset,
230 RIP_ENTRY_LENGTH, "Address not specified, Metric: %u",
231 metric);
232 rip_vektor_tree = proto_item_add_subtree(ti, ett_rip_vec);
234 proto_tree_add_item(rip_vektor_tree, &hfi_rip_family, tvb, offset, 2, ENC_BIG_ENDIAN);
235 if (version == RIPv2) {
236 proto_tree_add_item(rip_vektor_tree, &hfi_rip_route_tag, tvb, offset+2, 2,
237 ENC_BIG_ENDIAN);
238 proto_tree_add_item(rip_vektor_tree, &hfi_rip_netmask, tvb, offset+8, 4,
239 ENC_BIG_ENDIAN);
240 proto_tree_add_item(rip_vektor_tree, &hfi_rip_next_hop, tvb, offset+12, 4,
241 ENC_BIG_ENDIAN);
243 proto_tree_add_uint(rip_vektor_tree, &hfi_rip_metric, tvb,
244 offset+16, 4, metric);
247 static void
248 dissect_ip_rip_vektor(tvbuff_t *tvb, int offset, guint8 version,
249 proto_tree *tree)
251 proto_item *ti;
252 proto_tree *rip_vektor_tree;
253 guint32 metric;
255 metric = tvb_get_ntohl(tvb, offset+16);
256 ti = proto_tree_add_text(tree, tvb, offset,
257 RIP_ENTRY_LENGTH, "IP Address: %s, Metric: %u",
258 tvb_ip_to_str(tvb, offset+4), metric);
259 rip_vektor_tree = proto_item_add_subtree(ti, ett_rip_vec);
261 proto_tree_add_item(rip_vektor_tree, &hfi_rip_family, tvb, offset, 2, ENC_BIG_ENDIAN);
262 if (version == RIPv2) {
263 proto_tree_add_item(rip_vektor_tree, &hfi_rip_route_tag, tvb, offset+2, 2,
264 ENC_BIG_ENDIAN);
267 proto_tree_add_item(rip_vektor_tree, &hfi_rip_ip, tvb, offset+4, 4, ENC_BIG_ENDIAN);
269 if (version == RIPv2) {
270 proto_tree_add_item(rip_vektor_tree, &hfi_rip_netmask, tvb, offset+8, 4,
271 ENC_BIG_ENDIAN);
272 proto_tree_add_item(rip_vektor_tree, &hfi_rip_next_hop, tvb, offset+12, 4,
273 ENC_BIG_ENDIAN);
275 proto_tree_add_uint(rip_vektor_tree, &hfi_rip_metric, tvb,
276 offset+16, 4, metric);
279 static gint
280 dissect_rip_authentication(tvbuff_t *tvb, int offset, proto_tree *tree)
282 proto_item *ti;
283 proto_tree *rip_authentication_tree;
284 guint16 authtype;
285 guint32 val, digest_off, auth_data_len;
287 auth_data_len = 0;
288 authtype = tvb_get_ntohs(tvb, offset + 2);
290 ti = proto_tree_add_text(tree, tvb, offset, RIP_ENTRY_LENGTH,
291 "Authentication: %s", val_to_str( authtype, rip_auth_type, "Unknown (%u)" ) );
292 rip_authentication_tree = proto_item_add_subtree(ti, ett_rip_vec);
294 proto_tree_add_uint(rip_authentication_tree, &hfi_rip_auth, tvb, offset+2, 2,
295 authtype);
297 switch ( authtype ) {
299 case AUTH_PASSWORD: /* Plain text password */
300 proto_tree_add_item(rip_authentication_tree, &hfi_rip_auth_passwd,
301 tvb, offset+4, 16, ENC_ASCII|ENC_NA);
302 break;
304 case AUTH_KEYED_MSG_DIGEST: /* Keyed MD5 rfc 2082 */
305 digest_off = tvb_get_ntohs( tvb, offset+4 );
306 proto_tree_add_text( rip_authentication_tree, tvb, offset+4, 2,
307 "Digest Offset: %u" , digest_off );
308 val = tvb_get_guint8( tvb, offset+6 );
309 proto_tree_add_text( rip_authentication_tree, tvb, offset+6, 1,
310 "Key ID: %u" , val );
311 auth_data_len = tvb_get_guint8( tvb, offset+7 );
312 proto_tree_add_text( rip_authentication_tree, tvb, offset+7, 1,
313 "Auth Data Len: %u" , auth_data_len );
314 val = tvb_get_ntohl( tvb, offset+8 );
315 proto_tree_add_text( rip_authentication_tree, tvb, offset+8, 4,
316 "Seq num: %u" , val );
317 proto_tree_add_text( rip_authentication_tree, tvb, offset+12, 8,
318 "Zero Padding" );
319 ti = proto_tree_add_text( rip_authentication_tree, tvb, offset-4+digest_off,
320 MD5_AUTH_DATA_LEN+4, "Authentication Data Trailer" );
321 rip_authentication_tree = proto_item_add_subtree(ti, ett_auth_vec );
322 proto_tree_add_text( rip_authentication_tree, tvb, offset-4+digest_off+4,
323 MD5_AUTH_DATA_LEN, "Authentication Data: %s",
324 tvb_bytes_to_str_punct(tvb, offset-4+digest_off+4,
325 MD5_AUTH_DATA_LEN, ' '));
326 break;
328 return auth_data_len;
331 void
332 proto_register_rip(void)
334 #ifndef HAVE_HFI_SECTION_INIT
335 static header_field_info *hfi[] = {
336 &hfi_rip_command,
337 &hfi_rip_version,
338 &hfi_rip_family,
339 &hfi_rip_routing_domain,
340 &hfi_rip_ip,
341 &hfi_rip_netmask,
342 &hfi_rip_next_hop,
343 &hfi_rip_metric,
344 &hfi_rip_auth,
345 &hfi_rip_auth_passwd,
346 &hfi_rip_route_tag,
348 #endif /* HAVE_HFI_SECTION_INIT */
350 static gint *ett[] = {
351 &ett_rip,
352 &ett_rip_vec,
353 &ett_auth_vec,
356 module_t *rip_module;
357 int proto_rip;
359 proto_rip = proto_register_protocol("Routing Information Protocol",
360 "RIP", "rip");
361 hfi_rip = proto_registrar_get_nth(proto_rip);
363 proto_register_fields(proto_rip, hfi, array_length(hfi));
364 proto_register_subtree_array(ett, array_length(ett));
366 rip_module = prefs_register_protocol(proto_rip, proto_reg_handoff_rip);
368 prefs_register_bool_preference(rip_module, "display_routing_domain", "Display Routing Domain field", "Display the third and forth bytes of the RIPv2 header as the Routing Domain field (introduced in RFC 1388 [January 1993] and obsolete as of RFC 1723 [November 1994])", &pref_display_routing_domain);
370 rip_handle = create_dissector_handle(dissect_rip, proto_rip);
373 void
374 proto_reg_handoff_rip(void)
376 dissector_add_uint("udp.port", UDP_PORT_RIP, rip_handle);