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