Define some fcns & vars as static...
[metze/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 <glib.h>
33 #include <epan/packet.h>
34 #include <epan/emem.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     col_set_str(pinfo->cinfo, COL_PROTOCOL, "RIP");
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                         is_md5_auth = TRUE;
160                 break;
161                 }
162                 if(is_md5_auth && tvb_reported_length_remaining(tvb, offset) == 20)
163                         break;
164                 /* Intentional fall through: auth Entry MUST be the first! */
165             default:
166                 proto_tree_add_text(rip_tree, tvb, offset,
167                                 RIP_ENTRY_LENGTH, "Unknown address family %u",
168                                 family);
169                 break;
170             }
171
172             offset += RIP_ENTRY_LENGTH;
173         }
174     }
175 }
176
177 static void
178 dissect_unspec_rip_vektor(tvbuff_t *tvb, int offset, guint8 version,
179                       proto_tree *tree)
180 {
181     proto_item *ti;
182     proto_tree *rip_vektor_tree;
183     guint32 metric;
184
185     metric = tvb_get_ntohl(tvb, offset+16);
186     ti = proto_tree_add_text(tree, tvb, offset,
187                              RIP_ENTRY_LENGTH, "Address not specified, Metric: %u",
188                              metric);
189     rip_vektor_tree = proto_item_add_subtree(ti, ett_rip_vec);
190
191     proto_tree_add_item(rip_vektor_tree, hf_rip_family, tvb, offset, 2, FALSE);
192     if (version == RIPv2) {
193         proto_tree_add_item(rip_vektor_tree, hf_rip_route_tag, tvb, offset+2, 2,
194                         FALSE);
195         proto_tree_add_item(rip_vektor_tree, hf_rip_netmask, tvb, offset+8, 4,
196                             FALSE);
197         proto_tree_add_item(rip_vektor_tree, hf_rip_next_hop, tvb, offset+12, 4,
198                             FALSE);
199     }
200     proto_tree_add_uint(rip_vektor_tree, hf_rip_metric, tvb,
201                         offset+16, 4, metric);
202 }
203
204 static void
205 dissect_ip_rip_vektor(tvbuff_t *tvb, int offset, guint8 version,
206                       proto_tree *tree)
207 {
208     proto_item *ti;
209     proto_tree *rip_vektor_tree;
210     guint32 metric;
211
212     metric = tvb_get_ntohl(tvb, offset+16);
213     ti = proto_tree_add_text(tree, tvb, offset,
214                              RIP_ENTRY_LENGTH, "IP Address: %s, Metric: %u",
215                              ip_to_str(tvb_get_ptr(tvb, offset+4, 4)), metric);
216     rip_vektor_tree = proto_item_add_subtree(ti, ett_rip_vec);
217
218     proto_tree_add_item(rip_vektor_tree, hf_rip_family, tvb, offset, 2, FALSE);
219     if (version == RIPv2) {
220         proto_tree_add_item(rip_vektor_tree, hf_rip_route_tag, tvb, offset+2, 2,
221                         FALSE);
222     }
223
224     proto_tree_add_item(rip_vektor_tree, hf_rip_ip, tvb, offset+4, 4, FALSE);
225
226     if (version == RIPv2) {
227         proto_tree_add_item(rip_vektor_tree, hf_rip_netmask, tvb, offset+8, 4,
228                             FALSE);
229         proto_tree_add_item(rip_vektor_tree, hf_rip_next_hop, tvb, offset+12, 4,
230                             FALSE);
231     }
232     proto_tree_add_uint(rip_vektor_tree, hf_rip_metric, tvb,
233                         offset+16, 4, metric);
234 }
235
236 static gchar *
237 rip_bytestring_to_str(const guint8 *ad, guint32 len, char punct) {
238   gchar  *str=NULL;
239   guint   str_len;
240   gchar        *p;
241   int          i;
242   guint32      octet;
243   /* At least one version of Apple's C compiler/linker is buggy, causing
244      a complaint from the linker about the "literal C string section"
245      not ending with '\0' if we initialize a 16-element "char" array with
246      a 16-character string, the fact that initializing such an array with
247      such a string is perfectly legitimate ANSI C nonwithstanding, the 17th
248      '\0' byte in the string nonwithstanding. */
249   static const gchar hex_digits[16] =
250       { '0', '1', '2', '3', '4', '5', '6', '7',
251         '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
252
253   str_len=sizeof(gchar)*len*(punct?3:2);
254   str=ep_alloc(str_len);
255   len--;
256
257   p = &str[str_len];
258   *--p = '\0';
259   i = len;
260   for (;;) {
261     octet = ad[i];
262     *--p = hex_digits[octet&0xF];
263     octet >>= 4;
264     *--p = hex_digits[octet&0xF];
265     if (i == 0)
266       break;
267     if (punct)
268       *--p = punct;
269     i--;
270   }
271   return p;
272 }
273
274 static gint
275 dissect_rip_authentication(tvbuff_t *tvb, int offset, proto_tree *tree)
276 {
277     proto_item *ti;
278     proto_tree *rip_authentication_tree;
279     guint16 authtype;
280     guint32 val, digest_off, auth_data_len;
281
282     auth_data_len = 0;
283     authtype = tvb_get_ntohs(tvb, offset + 2);
284
285     ti = proto_tree_add_text(tree, tvb, offset, RIP_ENTRY_LENGTH,
286                         "Authentication: %s", val_to_str( authtype, rip_auth_type, "Unknown (%u)" ) );
287     rip_authentication_tree = proto_item_add_subtree(ti, ett_rip_vec);
288
289     proto_tree_add_uint(rip_authentication_tree, hf_rip_auth, tvb, offset+2, 2,
290                 authtype);
291
292     switch ( authtype ) {
293
294     case AUTH_PASSWORD: /* Plain text password */
295         proto_tree_add_item(rip_authentication_tree, hf_rip_auth_passwd,
296                         tvb, offset+4, 16, FALSE);
297         break;
298
299     case AUTH_KEYED_MSG_DIGEST: /* Keyed MD5 rfc 2082 */
300         digest_off = tvb_get_ntohs( tvb, offset+4 );
301         proto_tree_add_text( rip_authentication_tree, tvb, offset+4, 2,
302                         "Digest Offset: %u" , digest_off );
303         val = tvb_get_guint8( tvb, offset+6 );
304         proto_tree_add_text( rip_authentication_tree, tvb, offset+6, 1,
305                         "Key ID: %u" , val );
306         auth_data_len = tvb_get_guint8( tvb, offset+7 );
307         proto_tree_add_text( rip_authentication_tree, tvb, offset+7, 1,
308                         "Auth Data Len: %u" , auth_data_len );
309         val = tvb_get_ntohl( tvb, offset+8 );
310         proto_tree_add_text( rip_authentication_tree, tvb, offset+8, 4,
311                         "Seq num: %u" , val );
312         proto_tree_add_text( rip_authentication_tree, tvb, offset+12, 8,
313                         "Zero Padding" );
314         ti = proto_tree_add_text( rip_authentication_tree, tvb, offset-4+digest_off,
315                         MD5_AUTH_DATA_LEN+4, "Authentication Data Trailer" );
316         rip_authentication_tree = proto_item_add_subtree(ti, ett_auth_vec );
317         proto_tree_add_text( rip_authentication_tree, tvb, offset-4+digest_off+4,
318                         MD5_AUTH_DATA_LEN, "Authentication Data: %s",
319                                 rip_bytestring_to_str(
320                                         tvb_get_ptr( tvb, offset-4+digest_off+4,MD5_AUTH_DATA_LEN),
321                                         MD5_AUTH_DATA_LEN, ' '));
322         break;
323     }
324     return auth_data_len;
325 }
326
327 void
328 proto_register_rip(void)
329 {
330         static hf_register_info hf[] = {
331                 { &hf_rip_command,
332                         { "Command", "rip.command", FT_UINT8, BASE_DEC,
333                         VALS(command_vals), 0, "What type of RIP Command is this", HFILL }},
334
335                 { &hf_rip_version,
336                         { "Version", "rip.version", FT_UINT8, BASE_DEC,
337                         VALS(version_vals), 0, "Version of the RIP protocol", HFILL }},
338
339                 { &hf_rip_family,
340                         { "Address Family", "rip.family", FT_UINT16, BASE_DEC,
341                         VALS(family_vals), 0, NULL, HFILL }},
342
343                 { &hf_rip_routing_domain,
344                         { "Routing Domain", "rip.routing_domain", FT_UINT16, BASE_DEC,
345                         NULL, 0, "RIPv2 Routing Domain", HFILL }},
346
347                 { &hf_rip_ip,
348                         { "IP Address", "rip.ip", FT_IPv4, BASE_NONE,
349                         NULL, 0, NULL, HFILL}},
350
351                 { &hf_rip_netmask,
352                         { "Netmask", "rip.netmask", FT_IPv4, BASE_NONE,
353                         NULL, 0, NULL, HFILL}},
354
355                 { &hf_rip_next_hop,
356                         { "Next Hop", "rip.next_hop", FT_IPv4, BASE_NONE,
357                         NULL, 0, "Next Hop router for this route", HFILL}},
358
359                 { &hf_rip_metric,
360                         { "Metric", "rip.metric", FT_UINT16, BASE_DEC,
361                         NULL, 0, "Metric for this route", HFILL }},
362
363                 { &hf_rip_auth,
364                         { "Authentication type", "rip.auth.type", FT_UINT16, BASE_DEC,
365                         VALS(rip_auth_type), 0, "Type of authentication", HFILL }},
366
367                 { &hf_rip_auth_passwd,
368                         { "Password", "rip.auth.passwd", FT_STRING, BASE_NONE,
369                         NULL, 0, "Authentication password", HFILL }},
370
371                 { &hf_rip_route_tag,
372                         { "Route Tag", "rip.route_tag", FT_UINT16, BASE_DEC,
373                         NULL, 0, NULL, HFILL }},
374
375         };
376         static gint *ett[] = {
377                 &ett_rip,
378                 &ett_rip_vec,
379                 &ett_auth_vec,
380         };
381
382         proto_rip = proto_register_protocol("Routing Information Protocol",
383                                 "RIP", "rip");
384         proto_register_field_array(proto_rip, hf, array_length(hf));
385         proto_register_subtree_array(ett, array_length(ett));
386 }
387
388 void
389 proto_reg_handoff_rip(void)
390 {
391         dissector_handle_t rip_handle;
392
393         rip_handle = create_dissector_handle(dissect_rip, proto_rip);
394         dissector_add("udp.port", UDP_PORT_RIP, rip_handle);
395 }