change a whole bunch of ethereal into wireshark
[obnox/wireshark/wip.git] / epan / dissectors / packet-rip.c
1 /* packet-rip.c
2  * Routines for RIPv1 and RIPv2 packet disassembly
3  * RFC1058, RFC2453
4  * (c) Copyright Hannes R. Boehm <hannes@boehm.org>
5  *
6  * RFC2082 ( Keyed Message Digest Algorithm )
7  *   Emanuele Caratti  <wiz@iol.it>
8  *
9  * $Id$
10  *
11  * Wireshark - Network traffic analyzer
12  * By Gerald Combs <gerald@wireshark.org>
13  * Copyright 1998 Gerald Combs
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 #include "config.h"
31
32 #include <string.h>
33 #include <glib.h>
34 #include <epan/packet.h>
35 #include <epan/emem.h>
36
37 #define UDP_PORT_RIP    520
38
39 #define RIPv1   1
40 #define RIPv2   2
41
42 static const value_string version_vals[] = {
43         { RIPv1, "RIPv1" },
44         { RIPv2, "RIPv2" },
45         { 0,     NULL }
46 };
47
48 static const value_string command_vals[] = {
49         { 1, "Request" },
50         { 2, "Response" },
51         { 3, "Traceon" },
52         { 4, "Traceoff" },
53         { 5, "Vendor specific (Sun)" },
54         { 0, NULL }
55 };
56
57 #define AFVAL_UNSPEC    0
58 #define AFVAL_IP        2
59
60 static const value_string family_vals[] = {
61         { AFVAL_UNSPEC, "Unspecified" },
62         { AFVAL_IP,     "IP" },
63         { 0,            NULL }
64 };
65
66 #define AUTH_IP_ROUTE           1
67 #define AUTH_PASSWORD           2
68 #define AUTH_KEYED_MSG_DIGEST   3
69
70 static const value_string rip_auth_type[] = {
71         { AUTH_IP_ROUTE,                "IP Route" },
72         { AUTH_PASSWORD,                "Simple Password" },
73         { AUTH_KEYED_MSG_DIGEST,        "Keyed Message Digest" },
74         { 0,                            NULL }
75 };
76
77 #define RIP_HEADER_LENGTH 4
78 #define RIP_ENTRY_LENGTH 20
79 #define MD5_AUTH_DATA_LEN 16
80
81 static int proto_rip = -1;
82 static int hf_rip_command = -1;
83 static int hf_rip_version = -1;
84 static int hf_rip_routing_domain = -1;
85 static int hf_rip_ip = -1;
86 static int hf_rip_netmask = -1;
87 static int hf_rip_next_hop = -1;
88 static int hf_rip_metric = -1;
89 static int hf_rip_auth = -1;
90 static int hf_rip_auth_passwd = -1;
91 static int hf_rip_family = -1;
92 static int hf_rip_route_tag = -1;
93
94 static gint ett_rip = -1;
95 static gint ett_rip_vec = -1;
96 static gint ett_auth_vec = -1;
97
98 static void dissect_unspec_rip_vektor(tvbuff_t *tvb, int offset, guint8 version,
99     proto_tree *tree);
100 static void dissect_ip_rip_vektor(tvbuff_t *tvb, int offset, guint8 version,
101     proto_tree *tree);
102 static gint dissect_rip_authentication(tvbuff_t *tvb, int offset,
103     proto_tree *tree);
104
105 static void
106 dissect_rip(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
107 {
108     int offset = 0;
109     proto_tree *rip_tree = NULL;
110     proto_item *ti;
111     guint8 command;
112     guint8 version;
113     guint16 family;
114     gint trailer_len = 0;
115     gboolean is_md5_auth = FALSE;
116
117     if (check_col(pinfo->cinfo, COL_PROTOCOL))
118         col_set_str(pinfo->cinfo, COL_PROTOCOL, "RIP");
119     if (check_col(pinfo->cinfo, COL_INFO))
120         col_clear(pinfo->cinfo, COL_INFO);
121
122     command = tvb_get_guint8(tvb, 0);
123     version = tvb_get_guint8(tvb, 1);
124
125     if (check_col(pinfo->cinfo, COL_PROTOCOL))
126         col_add_str(pinfo->cinfo, COL_PROTOCOL,
127                     val_to_str(version, version_vals, "RIP"));
128     if (check_col(pinfo->cinfo, COL_INFO))
129         col_add_str(pinfo->cinfo, COL_INFO,
130                     val_to_str(command, command_vals, "Unknown command (%u)"));
131
132     if (tree) {
133         ti = proto_tree_add_item(tree, proto_rip, tvb, 0, -1, FALSE);
134         rip_tree = proto_item_add_subtree(ti, ett_rip);
135
136         proto_tree_add_uint(rip_tree, hf_rip_command, tvb, 0, 1, command);
137         proto_tree_add_uint(rip_tree, hf_rip_version, tvb, 1, 1, version);
138         if (version == RIPv2)
139             proto_tree_add_uint(rip_tree, hf_rip_routing_domain, tvb, 2, 2,
140                         tvb_get_ntohs(tvb, 2));
141
142         /* skip header */
143         offset = RIP_HEADER_LENGTH;
144
145         /* zero or more entries */
146         while (tvb_reported_length_remaining(tvb, offset) > trailer_len ) {
147             family = tvb_get_ntohs(tvb, offset);
148             switch (family) {
149             case AFVAL_UNSPEC: /* Unspecified */
150                 /*
151                  * There should be one entry in the request, and a metric
152                  * of infinity, meaning "show the entire routing table".
153                  */
154                 dissect_unspec_rip_vektor(tvb, offset, version, rip_tree);
155                 break;
156             case AFVAL_IP: /* IP */
157                 dissect_ip_rip_vektor(tvb, offset, version, rip_tree);
158                 break;
159             case 0xFFFF:
160                 if( offset == RIP_HEADER_LENGTH ) {
161                         trailer_len=dissect_rip_authentication(tvb, offset, rip_tree);
162                         is_md5_auth = TRUE;
163                 break;
164                 }
165                 if(is_md5_auth && tvb_reported_length_remaining(tvb, offset) == 20)
166                         break;
167                 /* Intentional fall through: auth Entry MUST be the first! */
168             default:
169                 proto_tree_add_text(rip_tree, tvb, offset,
170                                 RIP_ENTRY_LENGTH, "Unknown address family %u",
171                                 family);
172                 break;
173             }
174
175             offset += RIP_ENTRY_LENGTH;
176         }
177     }
178 }
179
180 static void
181 dissect_unspec_rip_vektor(tvbuff_t *tvb, int offset, guint8 version,
182                       proto_tree *tree)
183 {
184     proto_item *ti;
185     proto_tree *rip_vektor_tree;
186     guint32 metric;
187
188     metric = tvb_get_ntohl(tvb, offset+16);
189     ti = proto_tree_add_text(tree, tvb, offset,
190                              RIP_ENTRY_LENGTH, "Address not specified, Metric: %u",
191                              metric);
192     rip_vektor_tree = proto_item_add_subtree(ti, ett_rip_vec);
193
194     proto_tree_add_item(rip_vektor_tree, hf_rip_family, tvb, offset, 2, FALSE);
195     if (version == RIPv2) {
196         proto_tree_add_item(rip_vektor_tree, hf_rip_route_tag, tvb, offset+2, 2,
197                         FALSE);
198         proto_tree_add_item(rip_vektor_tree, hf_rip_netmask, tvb, offset+8, 4,
199                             FALSE);
200         proto_tree_add_item(rip_vektor_tree, hf_rip_next_hop, tvb, offset+12, 4,
201                             FALSE);
202     }
203     proto_tree_add_uint(rip_vektor_tree, hf_rip_metric, tvb,
204                         offset+16, 4, metric);
205 }
206
207 static void
208 dissect_ip_rip_vektor(tvbuff_t *tvb, int offset, guint8 version,
209                       proto_tree *tree)
210 {
211     proto_item *ti;
212     proto_tree *rip_vektor_tree;
213     guint32 metric;
214
215     metric = tvb_get_ntohl(tvb, offset+16);
216     ti = proto_tree_add_text(tree, tvb, offset,
217                              RIP_ENTRY_LENGTH, "IP Address: %s, Metric: %u",
218                              ip_to_str(tvb_get_ptr(tvb, offset+4, 4)), metric);
219     rip_vektor_tree = proto_item_add_subtree(ti, ett_rip_vec);
220
221     proto_tree_add_item(rip_vektor_tree, hf_rip_family, tvb, offset, 2, FALSE);
222     if (version == RIPv2) {
223         proto_tree_add_item(rip_vektor_tree, hf_rip_route_tag, tvb, offset+2, 2,
224                         FALSE);
225     }
226
227     proto_tree_add_item(rip_vektor_tree, hf_rip_ip, tvb, offset+4, 4, FALSE);
228
229     if (version == RIPv2) {
230         proto_tree_add_item(rip_vektor_tree, hf_rip_netmask, tvb, offset+8, 4,
231                             FALSE);
232         proto_tree_add_item(rip_vektor_tree, hf_rip_next_hop, tvb, offset+12, 4,
233                             FALSE);
234     }
235     proto_tree_add_uint(rip_vektor_tree, hf_rip_metric, tvb,
236                         offset+16, 4, metric);
237 }
238
239 static gchar *
240 rip_bytestring_to_str(const guint8 *ad, guint32 len, char punct) {
241   gchar  *str=NULL;
242   guint   str_len;
243   gchar        *p;
244   int          i;
245   guint32      octet;
246   /* At least one version of Apple's C compiler/linker is buggy, causing
247      a complaint from the linker about the "literal C string section"
248      not ending with '\0' if we initialize a 16-element "char" array with
249      a 16-character string, the fact that initializing such an array with
250      such a string is perfectly legitimate ANSI C nonwithstanding, the 17th
251      '\0' byte in the string nonwithstanding. */
252   static const gchar hex_digits[16] =
253       { '0', '1', '2', '3', '4', '5', '6', '7',
254         '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
255
256   str_len=sizeof(gchar)*len*(punct?3:2);
257   str=ep_alloc(str_len);
258   len--;
259
260   p = &str[str_len];
261   *--p = '\0';
262   i = len;
263   for (;;) {
264     octet = ad[i];
265     *--p = hex_digits[octet&0xF];
266     octet >>= 4;
267     *--p = hex_digits[octet&0xF];
268     if (i == 0)
269       break;
270     if (punct)
271       *--p = punct;
272     i--;
273   }
274   return p;
275 }
276
277 static gint
278 dissect_rip_authentication(tvbuff_t *tvb, int offset, proto_tree *tree)
279 {
280     proto_item *ti;
281     proto_tree *rip_authentication_tree;
282     guint16 authtype;
283     guint32 val, digest_off, auth_data_len;
284
285     auth_data_len = 0;
286     authtype = tvb_get_ntohs(tvb, offset + 2);
287
288     ti = proto_tree_add_text(tree, tvb, offset, RIP_ENTRY_LENGTH,
289                         "Authentication: %s", val_to_str( authtype, rip_auth_type, "Unknown (%u)" ) );
290     rip_authentication_tree = proto_item_add_subtree(ti, ett_rip_vec);
291
292     proto_tree_add_uint(rip_authentication_tree, hf_rip_auth, tvb, offset+2, 2,
293                 authtype);
294
295     switch ( authtype ) {
296
297     case AUTH_PASSWORD: /* Plain text password */
298         proto_tree_add_item(rip_authentication_tree, hf_rip_auth_passwd,
299                         tvb, offset+4, 16, FALSE);
300         break;
301
302     case AUTH_KEYED_MSG_DIGEST: /* Keyed MD5 rfc 2082 */
303         digest_off = tvb_get_ntohs( tvb, offset+4 );
304         proto_tree_add_text( rip_authentication_tree, tvb, offset+4, 2,
305                         "Digest Offset: %u" , digest_off );
306         val = tvb_get_guint8( tvb, offset+6 );
307         proto_tree_add_text( rip_authentication_tree, tvb, offset+6, 1,
308                         "Key ID: %u" , val );
309         auth_data_len = tvb_get_guint8( tvb, offset+7 );
310         proto_tree_add_text( rip_authentication_tree, tvb, offset+7, 1,
311                         "Auth Data Len: %u" , auth_data_len );
312         val = tvb_get_ntohl( tvb, offset+8 );
313         proto_tree_add_text( rip_authentication_tree, tvb, offset+8, 4,
314                         "Seq num: %u" , val );
315         proto_tree_add_text( rip_authentication_tree, tvb, offset+12, 8,
316                         "Zero Padding" );
317         ti = proto_tree_add_text( rip_authentication_tree, tvb, offset-4+digest_off,
318                         MD5_AUTH_DATA_LEN+4, "Authentication Data Trailer" );
319         rip_authentication_tree = proto_item_add_subtree(ti, ett_auth_vec );
320         proto_tree_add_text( rip_authentication_tree, tvb, offset-4+digest_off+4,
321                         MD5_AUTH_DATA_LEN, "Authentication Data: %s",
322                                 rip_bytestring_to_str(
323                                         tvb_get_ptr( tvb, offset-4+digest_off+4,MD5_AUTH_DATA_LEN),
324                                         MD5_AUTH_DATA_LEN, ' '));
325         break;
326     }
327     return auth_data_len;
328 }
329
330 void
331 proto_register_rip(void)
332 {
333         static hf_register_info hf[] = {
334                 { &hf_rip_command,
335                         { "Command", "rip.command", FT_UINT8, BASE_DEC,
336                         VALS(command_vals), 0, "What type of RIP Command is this", HFILL }},
337
338                 { &hf_rip_version,
339                         { "Version", "rip.version", FT_UINT8, BASE_DEC,
340                         VALS(version_vals), 0, "Version of the RIP protocol", HFILL }},
341
342                 { &hf_rip_family,
343                         { "Address Family", "rip.family", FT_UINT16, BASE_DEC,
344                         VALS(family_vals), 0, "Address family", HFILL }},
345
346                 { &hf_rip_routing_domain,
347                         { "Routing Domain", "rip.routing_domain", FT_UINT16, BASE_DEC,
348                         NULL, 0, "RIPv2 Routing Domain", HFILL }},
349
350                 { &hf_rip_ip,
351                         { "IP Address", "rip.ip", FT_IPv4, BASE_NONE,
352                         NULL, 0, "IP Address", HFILL}},
353
354                 { &hf_rip_netmask,
355                         { "Netmask", "rip.netmask", FT_IPv4, BASE_NONE,
356                         NULL, 0, "Netmask", HFILL}},
357
358                 { &hf_rip_next_hop,
359                         { "Next Hop", "rip.next_hop", FT_IPv4, BASE_NONE,
360                         NULL, 0, "Next Hop router for this route", HFILL}},
361
362                 { &hf_rip_metric,
363                         { "Metric", "rip.metric", FT_UINT16, BASE_DEC,
364                         NULL, 0, "Metric for this route", HFILL }},
365
366                 { &hf_rip_auth,
367                         { "Authentication type", "rip.auth.type", FT_UINT16, BASE_DEC,
368                         VALS(rip_auth_type), 0, "Type of authentication", HFILL }},
369
370                 { &hf_rip_auth_passwd,
371                         { "Password", "rip.auth.passwd", FT_STRING, BASE_DEC,
372                         NULL, 0, "Authentication password", HFILL }},
373
374                 { &hf_rip_route_tag,
375                         { "Route Tag", "rip.route_tag", FT_UINT16, BASE_DEC,
376                         NULL, 0, "Route Tag", HFILL }},
377
378         };
379         static gint *ett[] = {
380                 &ett_rip,
381                 &ett_rip_vec,
382                 &ett_auth_vec,
383         };
384
385         proto_rip = proto_register_protocol("Routing Information Protocol",
386                                 "RIP", "rip");
387         proto_register_field_array(proto_rip, hf, array_length(hf));
388         proto_register_subtree_array(ett, array_length(ett));
389 }
390
391 void
392 proto_reg_handoff_rip(void)
393 {
394         dissector_handle_t rip_handle;
395
396         rip_handle = create_dissector_handle(dissect_rip, proto_rip);
397         dissector_add("udp.port", UDP_PORT_RIP, rip_handle);
398 }