Return of the .PHONY target (aka cvsversion.h)
[metze/wireshark/wip.git] / 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: packet-rip.c,v 1.34 2003/10/18 18:46:37 guy Exp $
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
79 static int proto_rip = -1;
80 static int hf_rip_command = -1;
81 static int hf_rip_version = -1;
82 static int hf_rip_routing_domain = -1;
83 static int hf_rip_ip = -1;
84 static int hf_rip_netmask = -1;
85 static int hf_rip_next_hop = -1;
86 static int hf_rip_metric = -1;
87 static int hf_rip_auth = -1;
88 static int hf_rip_auth_passwd = -1;
89 static int hf_rip_family = -1;
90 static int hf_rip_route_tag = -1;
91
92 static gint ett_rip = -1;
93 static gint ett_rip_vec = -1;
94 static gint ett_auth_vec = -1;
95
96 static void dissect_unspec_rip_vektor(tvbuff_t *tvb, int offset, guint8 version,
97     proto_tree *tree);
98 static void dissect_ip_rip_vektor(tvbuff_t *tvb, int offset, guint8 version,
99     proto_tree *tree);
100 static gint dissect_rip_authentication(tvbuff_t *tvb, int offset,
101     proto_tree *tree);
102
103 static void
104 dissect_rip(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
105 {
106     int offset = 0;
107     proto_tree *rip_tree = NULL;
108     proto_item *ti;
109     guint8 command;
110     guint8 version;
111     guint16 family;
112     gint trailer_len = 0;
113
114     if (check_col(pinfo->cinfo, COL_PROTOCOL))
115         col_set_str(pinfo->cinfo, COL_PROTOCOL, "RIP");
116     if (check_col(pinfo->cinfo, COL_INFO))
117         col_clear(pinfo->cinfo, COL_INFO);
118
119     command = tvb_get_guint8(tvb, 0);
120     version = tvb_get_guint8(tvb, 1);
121
122     if (check_col(pinfo->cinfo, COL_PROTOCOL))
123         col_add_str(pinfo->cinfo, COL_PROTOCOL,
124                     val_to_str(version, version_vals, "RIP"));
125     if (check_col(pinfo->cinfo, COL_INFO))
126         col_add_str(pinfo->cinfo, COL_INFO,
127                     val_to_str(command, command_vals, "Unknown command (%u)"));
128
129     if (tree) {
130         ti = proto_tree_add_item(tree, proto_rip, tvb, 0, -1, FALSE);
131         rip_tree = proto_item_add_subtree(ti, ett_rip);
132
133         proto_tree_add_uint(rip_tree, hf_rip_command, tvb, 0, 1, command);
134         proto_tree_add_uint(rip_tree, hf_rip_version, tvb, 1, 1, version);
135         if (version == RIPv2)
136             proto_tree_add_uint(rip_tree, hf_rip_routing_domain, tvb, 2, 2,
137                         tvb_get_ntohs(tvb, 2));
138
139         /* skip header */
140         offset = RIP_HEADER_LENGTH;
141
142         /* zero or more entries */
143         while (tvb_reported_length_remaining(tvb, offset) > trailer_len ) {
144             family = tvb_get_ntohs(tvb, offset);
145             switch (family) {
146             case AFVAL_UNSPEC: /* Unspecified */
147                 /*
148                  * There should be one entry in the request, and a metric
149                  * of infinity, meaning "show the entire routing table".
150                  */
151                 dissect_unspec_rip_vektor(tvb, offset, version, rip_tree);
152                 break;
153             case AFVAL_IP: /* IP */
154                 dissect_ip_rip_vektor(tvb, offset, version, rip_tree);
155                 break;
156             case 0xFFFF:
157                 if( offset == RIP_HEADER_LENGTH ) {
158                         trailer_len=dissect_rip_authentication(tvb, offset, rip_tree);
159                 break;
160                 }
161                 /* Intentional fall through: auth Entry MUST be the first! */
162             default:
163                 proto_tree_add_text(rip_tree, tvb, offset,
164                                 RIP_ENTRY_LENGTH, "Unknown address family %u",
165                                 family);
166                 break;
167             }
168
169             offset += RIP_ENTRY_LENGTH;
170         }
171     }
172 }
173
174 static void
175 dissect_unspec_rip_vektor(tvbuff_t *tvb, int offset, guint8 version,
176                       proto_tree *tree)
177 {
178     proto_item *ti;
179     proto_tree *rip_vektor_tree;
180     guint32 metric;
181
182     metric = tvb_get_ntohl(tvb, offset+16);
183     ti = proto_tree_add_text(tree, tvb, offset,
184                              RIP_ENTRY_LENGTH, "Address not specified, Metric: %u",
185                              metric);
186     rip_vektor_tree = proto_item_add_subtree(ti, ett_rip_vec);
187
188     proto_tree_add_item(rip_vektor_tree, hf_rip_family, tvb, offset, 2, FALSE);
189     if (version == RIPv2) {
190         proto_tree_add_item(rip_vektor_tree, hf_rip_route_tag, tvb, offset+2, 2,
191                         FALSE);
192         proto_tree_add_item(rip_vektor_tree, hf_rip_netmask, tvb, offset+8, 4,
193                             FALSE);
194         proto_tree_add_item(rip_vektor_tree, hf_rip_next_hop, tvb, offset+12, 4,
195                             FALSE);
196     }
197     proto_tree_add_uint(rip_vektor_tree, hf_rip_metric, tvb,
198                         offset+16, 4, metric);
199 }
200
201 static void
202 dissect_ip_rip_vektor(tvbuff_t *tvb, int offset, guint8 version,
203                       proto_tree *tree)
204 {
205     proto_item *ti;
206     proto_tree *rip_vektor_tree;
207     guint32 metric;
208
209     metric = tvb_get_ntohl(tvb, offset+16);
210     ti = proto_tree_add_text(tree, tvb, offset,
211                              RIP_ENTRY_LENGTH, "IP Address: %s, Metric: %u",
212                              ip_to_str(tvb_get_ptr(tvb, offset+4, 4)), metric);
213     rip_vektor_tree = proto_item_add_subtree(ti, ett_rip_vec);
214
215     proto_tree_add_item(rip_vektor_tree, hf_rip_family, tvb, offset, 2, FALSE);
216     if (version == RIPv2) {
217         proto_tree_add_item(rip_vektor_tree, hf_rip_route_tag, tvb, offset+2, 2,
218                         FALSE);
219     }
220
221     proto_tree_add_item(rip_vektor_tree, hf_rip_ip, tvb, offset+4, 4, FALSE);
222
223     if (version == RIPv2) {
224         proto_tree_add_item(rip_vektor_tree, hf_rip_netmask, tvb, offset+8, 4,
225                             FALSE);
226         proto_tree_add_item(rip_vektor_tree, hf_rip_next_hop, tvb, offset+12, 4,
227                             FALSE);
228     }
229     proto_tree_add_uint(rip_vektor_tree, hf_rip_metric, tvb,
230                         offset+16, 4, metric);
231 }
232
233 static gchar *
234 rip_bytestring_to_str(const guint8 *ad, guint32 len, char punct) {
235   static gchar  *str=NULL;
236   static guint   str_len;
237   gchar        *p;
238   int          i;
239   guint32      octet;
240   /* At least one version of Apple's C compiler/linker is buggy, causing
241      a complaint from the linker about the "literal C string section"
242      not ending with '\0' if we initialize a 16-element "char" array with
243      a 16-character string, the fact that initializing such an array with
244      such a string is perfectly legitimate ANSI C nonwithstanding, the 17th
245      '\0' byte in the string nonwithstanding. */
246   static const gchar hex_digits[16] =
247       { '0', '1', '2', '3', '4', '5', '6', '7',
248         '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
249
250         if( !str ) {
251                 str_len=sizeof(gchar)*len*(punct?3:2);
252                 str=g_malloc(str_len);
253         } else {
254                 if( str_len < (sizeof(gchar)*len*(punct?3:2)) ) {
255                         g_free(str);
256                         str_len=sizeof(gchar)*len*(punct?3:2);
257                         str=g_malloc(str_len);
258                 }
259         }
260   len--;
261
262   p = &str[str_len];
263   *--p = '\0';
264   i = len;
265   for (;;) {
266     octet = ad[i];
267     *--p = hex_digits[octet&0xF];
268     octet >>= 4;
269     *--p = hex_digits[octet&0xF];
270     if (i == 0)
271       break;
272     if (punct)
273       *--p = punct;
274     i--;
275   }
276   return p;
277 }
278
279 static gint
280 dissect_rip_authentication(tvbuff_t *tvb, int offset, proto_tree *tree)
281 {
282     proto_item *ti;
283     proto_tree *rip_authentication_tree;
284     guint16 authtype;
285     guint32 val, digest_off, auth_data_len;
286
287     auth_data_len = 0;
288     authtype = tvb_get_ntohs(tvb, offset + 2);
289
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);
293
294     proto_tree_add_uint(rip_authentication_tree, hf_rip_auth, tvb, offset+2, 2,
295                 authtype);
296
297     switch ( authtype ) {
298
299     case AUTH_PASSWORD: /* Plain text password */
300         proto_tree_add_item(rip_authentication_tree, hf_rip_auth_passwd,
301                         tvb, offset+4, 16, FALSE);
302         break;
303
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                         auth_data_len, "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                         auth_data_len-4, "Authentication Data: %s",
324                                 rip_bytestring_to_str(
325                                         tvb_get_ptr( tvb, offset-4+digest_off+4,auth_data_len-4),
326                                         auth_data_len-4, ' '));
327         break;
328     }
329     return auth_data_len;
330 }
331
332 void
333 proto_register_rip(void)
334 {
335         static hf_register_info hf[] = {
336                 { &hf_rip_command,
337                         { "Command", "rip.command", FT_UINT8, BASE_DEC,
338                         VALS(command_vals), 0, "What type of RIP Command is this", HFILL }},
339
340                 { &hf_rip_version,
341                         { "Version", "rip.version", FT_UINT8, BASE_DEC,
342                         VALS(version_vals), 0, "Version of the RIP protocol", HFILL }},
343
344                 { &hf_rip_family,
345                         { "Address Family", "rip.family", FT_UINT16, BASE_DEC,
346                         VALS(family_vals), 0, "Address family", HFILL }},
347
348                 { &hf_rip_routing_domain,
349                         { "Routing Domain", "rip.routing_domain", FT_UINT16, BASE_DEC,
350                         NULL, 0, "RIPv2 Routing Domain", HFILL }},
351
352                 { &hf_rip_ip,
353                         { "IP Address", "rip.ip", FT_IPv4, BASE_NONE,
354                         NULL, 0, "IP Address", HFILL}},
355
356                 { &hf_rip_netmask,
357                         { "Netmask", "rip.netmask", FT_IPv4, BASE_NONE,
358                         NULL, 0, "Netmask", HFILL}},
359
360                 { &hf_rip_next_hop,
361                         { "Next Hop", "rip.next_hop", FT_IPv4, BASE_NONE,
362                         NULL, 0, "Next Hop router for this route", HFILL}},
363
364                 { &hf_rip_metric,
365                         { "Metric", "rip.metric", FT_UINT16, BASE_DEC,
366                         NULL, 0, "Metric for this route", HFILL }},
367
368                 { &hf_rip_auth,
369                         { "Authentication type", "rip.auth.type", FT_UINT16, BASE_DEC,
370                         VALS(rip_auth_type), 0, "Type of authentication", HFILL }},
371
372                 { &hf_rip_auth_passwd,
373                         { "Password", "rip.auth.passwd", FT_STRING, BASE_DEC,
374                         NULL, 0, "Authentication password", HFILL }},
375
376                 { &hf_rip_route_tag,
377                         { "Route Tag", "rip.route_tag", FT_UINT16, BASE_DEC,
378                         NULL, 0, "Route Tag", HFILL }},
379
380         };
381         static gint *ett[] = {
382                 &ett_rip,
383                 &ett_rip_vec,
384                 &ett_auth_vec,
385         };
386
387         proto_rip = proto_register_protocol("Routing Information Protocol",
388                                 "RIP", "rip");
389         proto_register_field_array(proto_rip, hf, array_length(hf));
390         proto_register_subtree_array(ett, array_length(ett));
391 }
392
393 void
394 proto_reg_handoff_rip(void)
395 {
396         dissector_handle_t rip_handle;
397
398         rip_handle = create_dissector_handle(dissect_rip, proto_rip);
399         dissector_add("udp.port", UDP_PORT_RIP, rip_handle);
400 }