Remove all $Id$ from top of file
[metze/wireshark/wip.git] / epan / dissectors / packet-rip.c
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>
5  *
6  * RFC2082 ( Keyed Message Digest Algorithm )
7  *   Emanuele Caratti  <wiz@iol.it>
8  *
9  * Wireshark - Network traffic analyzer
10  * By Gerald Combs <gerald@wireshark.org>
11  * Copyright 1998 Gerald Combs
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26  */
27
28 #define NEW_PROTO_TREE_API
29
30 #include "config.h"
31
32 #include <glib.h>
33 #include <epan/packet.h>
34 #include <epan/emem.h>
35 #include <epan/prefs.h>
36 #include <epan/to_str.h>
37
38 #define UDP_PORT_RIP    520
39
40 #define RIPv1   1
41 #define RIPv2   2
42
43 void proto_register_rip(void);
44
45 static const value_string version_vals[] = {
46         { RIPv1, "RIPv1" },
47         { RIPv2, "RIPv2" },
48         { 0,     NULL }
49 };
50
51 static const value_string command_vals[] = {
52         { 1, "Request" },
53         { 2, "Response" },
54         { 3, "Traceon" },
55         { 4, "Traceoff" },
56         { 5, "Vendor specific (Sun)" },
57         { 0, NULL }
58 };
59
60 #define AFVAL_UNSPEC    0
61 #define AFVAL_IP        2
62
63 static const value_string family_vals[] = {
64         { AFVAL_UNSPEC, "Unspecified" },
65         { AFVAL_IP,     "IP" },
66         { 0,            NULL }
67 };
68
69 #define AUTH_IP_ROUTE           1
70 #define AUTH_PASSWORD           2
71 #define AUTH_KEYED_MSG_DIGEST   3
72
73 static const value_string rip_auth_type[] = {
74         { AUTH_IP_ROUTE,                "IP Route" },
75         { AUTH_PASSWORD,                "Simple Password" },
76         { AUTH_KEYED_MSG_DIGEST,        "Keyed Message Digest" },
77         { 0,                            NULL }
78 };
79
80 #define RIP_HEADER_LENGTH 4
81 #define RIP_ENTRY_LENGTH 20
82 #define MD5_AUTH_DATA_LEN 16
83
84 static gboolean pref_display_routing_domain = FALSE;
85
86 void proto_reg_handoff_rip(void);
87
88
89 static dissector_handle_t rip_handle;
90
91 static header_field_info *hfi_rip = NULL;
92
93 #define RIP_HFI_INIT HFI_INIT(proto_rip)
94
95 static header_field_info hfi_rip_command RIP_HFI_INIT =
96         { "Command", "rip.command", FT_UINT8, BASE_DEC,
97           VALS(command_vals), 0, "What type of RIP Command is this", HFILL };
98
99 static header_field_info hfi_rip_version RIP_HFI_INIT =
100         { "Version", "rip.version", FT_UINT8, BASE_DEC,
101           VALS(version_vals), 0, "Version of the RIP protocol", HFILL };
102
103 static header_field_info hfi_rip_routing_domain RIP_HFI_INIT =
104         { "Routing Domain", "rip.routing_domain", FT_UINT16, BASE_DEC,
105           NULL, 0, "RIPv2 Routing Domain", HFILL };
106
107 static header_field_info hfi_rip_ip RIP_HFI_INIT =
108         { "IP Address", "rip.ip", FT_IPv4, BASE_NONE,
109           NULL, 0, NULL, HFILL};
110
111 static header_field_info hfi_rip_netmask RIP_HFI_INIT =
112         { "Netmask", "rip.netmask", FT_IPv4, BASE_NONE,
113           NULL, 0, NULL, HFILL};
114
115 static header_field_info hfi_rip_next_hop RIP_HFI_INIT =
116         { "Next Hop", "rip.next_hop", FT_IPv4, BASE_NONE,
117           NULL, 0, "Next Hop router for this route", HFILL};
118
119 static header_field_info hfi_rip_metric RIP_HFI_INIT =
120         { "Metric", "rip.metric", FT_UINT16, BASE_DEC,
121           NULL, 0, "Metric for this route", HFILL };
122
123 static header_field_info hfi_rip_auth RIP_HFI_INIT =
124         { "Authentication type", "rip.auth.type", FT_UINT16, BASE_DEC,
125           VALS(rip_auth_type), 0, "Type of authentication", HFILL };
126
127 static header_field_info hfi_rip_auth_passwd RIP_HFI_INIT =
128         { "Password", "rip.auth.passwd", FT_STRING, BASE_NONE,
129           NULL, 0, "Authentication password", HFILL };
130
131 static header_field_info hfi_rip_family RIP_HFI_INIT =
132         { "Address Family", "rip.family", FT_UINT16, BASE_DEC,
133           VALS(family_vals), 0, NULL, HFILL };
134
135 static header_field_info hfi_rip_route_tag RIP_HFI_INIT =
136         { "Route Tag", "rip.route_tag", FT_UINT16, BASE_DEC,
137           NULL, 0, NULL, HFILL };
138
139 static gint ett_rip = -1;
140 static gint ett_rip_vec = -1;
141 static gint ett_auth_vec = -1;
142
143 static void dissect_unspec_rip_vektor(tvbuff_t *tvb, int offset, guint8 version,
144     proto_tree *tree);
145 static void dissect_ip_rip_vektor(tvbuff_t *tvb, int offset, guint8 version,
146     proto_tree *tree);
147 static gint dissect_rip_authentication(tvbuff_t *tvb, int offset,
148     proto_tree *tree);
149
150 static void
151 dissect_rip(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
152 {
153     int offset = 0;
154     proto_tree *rip_tree = NULL;
155     proto_item *ti;
156     guint8 command;
157     guint8 version;
158     guint16 family;
159     gint trailer_len = 0;
160     gboolean is_md5_auth = FALSE;
161
162     col_set_str(pinfo->cinfo, COL_PROTOCOL, "RIP");
163     col_clear(pinfo->cinfo, COL_INFO);
164
165     command = tvb_get_guint8(tvb, 0);
166     version = tvb_get_guint8(tvb, 1);
167
168     col_set_str(pinfo->cinfo, COL_PROTOCOL,
169                     val_to_str_const(version, version_vals, "RIP"));
170     col_add_str(pinfo->cinfo, COL_INFO,
171                     val_to_str(command, command_vals, "Unknown command (%u)"));
172
173     if (tree) {
174         ti = proto_tree_add_item(tree, hfi_rip, tvb, 0, -1, ENC_NA);
175         rip_tree = proto_item_add_subtree(ti, ett_rip);
176
177         proto_tree_add_uint(rip_tree, &hfi_rip_command, tvb, 0, 1, command);
178         proto_tree_add_uint(rip_tree, &hfi_rip_version, tvb, 1, 1, version);
179         if (version == RIPv2 && pref_display_routing_domain == TRUE)
180             proto_tree_add_uint(rip_tree, &hfi_rip_routing_domain, tvb, 2, 2,
181                         tvb_get_ntohs(tvb, 2));
182
183         /* skip header */
184         offset = RIP_HEADER_LENGTH;
185
186         /* zero or more entries */
187         while (tvb_reported_length_remaining(tvb, offset) > trailer_len ) {
188             family = tvb_get_ntohs(tvb, offset);
189             switch (family) {
190             case AFVAL_UNSPEC: /* Unspecified */
191                 /*
192                  * There should be one entry in the request, and a metric
193                  * of infinity, meaning "show the entire routing table".
194                  */
195                 dissect_unspec_rip_vektor(tvb, offset, version, rip_tree);
196                 break;
197             case AFVAL_IP: /* IP */
198                 dissect_ip_rip_vektor(tvb, offset, version, rip_tree);
199                 break;
200             case 0xFFFF:
201                 if( offset == RIP_HEADER_LENGTH ) {
202                         trailer_len=dissect_rip_authentication(tvb, offset, rip_tree);
203                         is_md5_auth = TRUE;
204                 break;
205                 }
206                 if(is_md5_auth && tvb_reported_length_remaining(tvb, offset) == 20)
207                         break;
208                 /* Intentional fall through: auth Entry MUST be the first! */
209             default:
210                 proto_tree_add_text(rip_tree, tvb, offset,
211                                 RIP_ENTRY_LENGTH, "Unknown address family %u",
212                                 family);
213                 break;
214             }
215
216             offset += RIP_ENTRY_LENGTH;
217         }
218     }
219 }
220
221 static void
222 dissect_unspec_rip_vektor(tvbuff_t *tvb, int offset, guint8 version,
223                       proto_tree *tree)
224 {
225     proto_item *ti;
226     proto_tree *rip_vektor_tree;
227     guint32 metric;
228
229     metric = tvb_get_ntohl(tvb, offset+16);
230     ti = proto_tree_add_text(tree, tvb, offset,
231                              RIP_ENTRY_LENGTH, "Address not specified, Metric: %u",
232                              metric);
233     rip_vektor_tree = proto_item_add_subtree(ti, ett_rip_vec);
234
235     proto_tree_add_item(rip_vektor_tree, &hfi_rip_family, tvb, offset, 2, ENC_BIG_ENDIAN);
236     if (version == RIPv2) {
237         proto_tree_add_item(rip_vektor_tree, &hfi_rip_route_tag, tvb, offset+2, 2,
238                         ENC_BIG_ENDIAN);
239         proto_tree_add_item(rip_vektor_tree, &hfi_rip_netmask, tvb, offset+8, 4,
240                             ENC_BIG_ENDIAN);
241         proto_tree_add_item(rip_vektor_tree, &hfi_rip_next_hop, tvb, offset+12, 4,
242                             ENC_BIG_ENDIAN);
243     }
244     proto_tree_add_uint(rip_vektor_tree, &hfi_rip_metric, tvb,
245                         offset+16, 4, metric);
246 }
247
248 static void
249 dissect_ip_rip_vektor(tvbuff_t *tvb, int offset, guint8 version,
250                       proto_tree *tree)
251 {
252     proto_item *ti;
253     proto_tree *rip_vektor_tree;
254     guint32 metric;
255
256     metric = tvb_get_ntohl(tvb, offset+16);
257     ti = proto_tree_add_text(tree, tvb, offset,
258                              RIP_ENTRY_LENGTH, "IP Address: %s, Metric: %u",
259                              tvb_ip_to_str(tvb, offset+4), metric);
260     rip_vektor_tree = proto_item_add_subtree(ti, ett_rip_vec);
261
262     proto_tree_add_item(rip_vektor_tree, &hfi_rip_family, tvb, offset, 2, ENC_BIG_ENDIAN);
263     if (version == RIPv2) {
264         proto_tree_add_item(rip_vektor_tree, &hfi_rip_route_tag, tvb, offset+2, 2,
265                         ENC_BIG_ENDIAN);
266     }
267
268     proto_tree_add_item(rip_vektor_tree, &hfi_rip_ip, tvb, offset+4, 4, ENC_BIG_ENDIAN);
269
270     if (version == RIPv2) {
271         proto_tree_add_item(rip_vektor_tree, &hfi_rip_netmask, tvb, offset+8, 4,
272                             ENC_BIG_ENDIAN);
273         proto_tree_add_item(rip_vektor_tree, &hfi_rip_next_hop, tvb, offset+12, 4,
274                             ENC_BIG_ENDIAN);
275     }
276     proto_tree_add_uint(rip_vektor_tree, &hfi_rip_metric, tvb,
277                         offset+16, 4, metric);
278 }
279
280 static gint
281 dissect_rip_authentication(tvbuff_t *tvb, int offset, proto_tree *tree)
282 {
283     proto_item *ti;
284     proto_tree *rip_authentication_tree;
285     guint16 authtype;
286     guint32 val, digest_off, auth_data_len;
287
288     auth_data_len = 0;
289     authtype = tvb_get_ntohs(tvb, offset + 2);
290
291     ti = proto_tree_add_text(tree, tvb, offset, RIP_ENTRY_LENGTH,
292                         "Authentication: %s", val_to_str( authtype, rip_auth_type, "Unknown (%u)" ) );
293     rip_authentication_tree = proto_item_add_subtree(ti, ett_rip_vec);
294
295     proto_tree_add_uint(rip_authentication_tree, &hfi_rip_auth, tvb, offset+2, 2,
296                 authtype);
297
298     switch ( authtype ) {
299
300     case AUTH_PASSWORD: /* Plain text password */
301         proto_tree_add_item(rip_authentication_tree, &hfi_rip_auth_passwd,
302                         tvb, offset+4, 16, ENC_ASCII|ENC_NA);
303         break;
304
305     case AUTH_KEYED_MSG_DIGEST: /* Keyed MD5 rfc 2082 */
306         digest_off = tvb_get_ntohs( tvb, offset+4 );
307         proto_tree_add_text( rip_authentication_tree, tvb, offset+4, 2,
308                         "Digest Offset: %u" , digest_off );
309         val = tvb_get_guint8( tvb, offset+6 );
310         proto_tree_add_text( rip_authentication_tree, tvb, offset+6, 1,
311                         "Key ID: %u" , val );
312         auth_data_len = tvb_get_guint8( tvb, offset+7 );
313         proto_tree_add_text( rip_authentication_tree, tvb, offset+7, 1,
314                         "Auth Data Len: %u" , auth_data_len );
315         val = tvb_get_ntohl( tvb, offset+8 );
316         proto_tree_add_text( rip_authentication_tree, tvb, offset+8, 4,
317                         "Seq num: %u" , val );
318         proto_tree_add_text( rip_authentication_tree, tvb, offset+12, 8,
319                         "Zero Padding" );
320         ti = proto_tree_add_text( rip_authentication_tree, tvb, offset-4+digest_off,
321                         MD5_AUTH_DATA_LEN+4, "Authentication Data Trailer" );
322         rip_authentication_tree = proto_item_add_subtree(ti, ett_auth_vec );
323         proto_tree_add_text( rip_authentication_tree, tvb, offset-4+digest_off+4,
324                         MD5_AUTH_DATA_LEN, "Authentication Data: %s",
325                         tvb_bytes_to_ep_str_punct(tvb, offset-4+digest_off+4,
326                                                MD5_AUTH_DATA_LEN, ' '));
327         break;
328     }
329     return auth_data_len;
330 }
331
332 void
333 proto_register_rip(void)
334 {
335 #ifndef HAVE_HFI_SECTION_INIT
336         static header_field_info *hfi[] = {
337                 &hfi_rip_command,
338                 &hfi_rip_version,
339                 &hfi_rip_family,
340                 &hfi_rip_routing_domain,
341                 &hfi_rip_ip,
342                 &hfi_rip_netmask,
343                 &hfi_rip_next_hop,
344                 &hfi_rip_metric,
345                 &hfi_rip_auth,
346                 &hfi_rip_auth_passwd,
347                 &hfi_rip_route_tag,
348         };
349 #endif /* HAVE_HFI_SECTION_INIT */
350
351         static gint *ett[] = {
352                 &ett_rip,
353                 &ett_rip_vec,
354                 &ett_auth_vec,
355         };
356
357         module_t *rip_module;
358         int proto_rip;
359
360         proto_rip = proto_register_protocol("Routing Information Protocol",
361                                 "RIP", "rip");
362         hfi_rip = proto_registrar_get_nth(proto_rip);
363
364         proto_register_fields(proto_rip, hfi, array_length(hfi));
365         proto_register_subtree_array(ett, array_length(ett));
366
367         rip_module = prefs_register_protocol(proto_rip, proto_reg_handoff_rip);
368
369         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
371         rip_handle = create_dissector_handle(dissect_rip, proto_rip);
372 }
373
374 void
375 proto_reg_handoff_rip(void)
376 {
377         dissector_add_uint("udp.port", UDP_PORT_RIP, rip_handle);
378 }