GSM A DTAP: add UMTS EVS to supported codecs list IE
[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  * SPDX-License-Identifier: GPL-2.0-or-later
14  */
15
16 #define NEW_PROTO_TREE_API
17
18 #include "config.h"
19
20 #include <epan/packet.h>
21 #include <epan/expert.h>
22 #include <epan/prefs.h>
23 #include <epan/to_str.h>
24
25 #define UDP_PORT_RIP    520
26
27 #define RIPv1   1
28 #define RIPv2   2
29
30 void proto_register_rip(void);
31 void proto_reg_handoff_rip(void);
32
33 static const value_string version_vals[] = {
34     { RIPv1, "RIPv1" },
35     { RIPv2, "RIPv2" },
36     { 0, NULL }
37 };
38
39 static const value_string command_vals[] = {
40     { 1, "Request" },
41     { 2, "Response" },
42     { 3, "Traceon" },
43     { 4, "Traceoff" },
44     { 5, "Vendor specific (Sun)" },
45     { 0, NULL }
46 };
47
48 #define AFVAL_UNSPEC    0
49 #define AFVAL_IP        2
50
51 static const value_string family_vals[] = {
52     { AFVAL_UNSPEC, "Unspecified" },
53     { AFVAL_IP,     "IP" },
54     { 0, NULL }
55 };
56
57 #define AUTH_IP_ROUTE           1
58 #define AUTH_PASSWORD           2
59 #define AUTH_KEYED_MSG_DIGEST   3
60
61 static const value_string rip_auth_type[] = {
62     { AUTH_IP_ROUTE,         "IP Route" },
63     { AUTH_PASSWORD,         "Simple Password" },
64     { AUTH_KEYED_MSG_DIGEST, "Keyed Message Digest" },
65     { 0, NULL }
66 };
67
68 #define RIP_HEADER_LENGTH 4
69 #define RIP_ENTRY_LENGTH 20
70 #define MD5_AUTH_DATA_LEN 16
71
72 static gboolean pref_display_routing_domain = FALSE;
73
74 static dissector_handle_t rip_handle;
75
76 static header_field_info *hfi_rip = NULL;
77
78 #define RIP_HFI_INIT HFI_INIT(proto_rip)
79
80 static header_field_info hfi_rip_command RIP_HFI_INIT = {
81     "Command", "rip.command", FT_UINT8, BASE_DEC,
82     VALS(command_vals), 0, "What type of RIP Command is this", HFILL };
83
84 static header_field_info hfi_rip_version RIP_HFI_INIT = {
85     "Version", "rip.version", FT_UINT8, BASE_DEC,
86     VALS(version_vals), 0, "Version of the RIP protocol", HFILL };
87
88 static header_field_info hfi_rip_routing_domain RIP_HFI_INIT = {
89     "Routing Domain", "rip.routing_domain", FT_UINT16, BASE_DEC,
90     NULL, 0, "RIPv2 Routing Domain", HFILL };
91
92 static header_field_info hfi_rip_ip RIP_HFI_INIT = {
93     "IP Address", "rip.ip", FT_IPv4, BASE_NONE,
94     NULL, 0, NULL, HFILL};
95
96 static header_field_info hfi_rip_netmask RIP_HFI_INIT = {
97     "Netmask", "rip.netmask", FT_IPv4, BASE_NETMASK,
98     NULL, 0, NULL, HFILL};
99
100 static header_field_info hfi_rip_next_hop RIP_HFI_INIT = {
101     "Next Hop", "rip.next_hop", FT_IPv4, BASE_NONE,
102     NULL, 0, "Next Hop router for this route", HFILL};
103
104 static header_field_info hfi_rip_metric RIP_HFI_INIT = {
105     "Metric", "rip.metric", FT_UINT16, BASE_DEC,
106     NULL, 0, "Metric for this route", HFILL };
107
108 static header_field_info hfi_rip_auth RIP_HFI_INIT = {
109     "Authentication type", "rip.auth.type", FT_UINT16, BASE_DEC,
110     VALS(rip_auth_type), 0, "Type of authentication", HFILL };
111
112 static header_field_info hfi_rip_auth_passwd RIP_HFI_INIT = {
113     "Password", "rip.auth.passwd", FT_STRING, BASE_NONE,
114     NULL, 0, "Authentication password", HFILL };
115
116 static header_field_info hfi_rip_family RIP_HFI_INIT = {
117     "Address Family", "rip.family", FT_UINT16, BASE_DEC,
118     VALS(family_vals), 0, NULL, HFILL };
119
120 static header_field_info hfi_rip_route_tag RIP_HFI_INIT = {
121     "Route Tag", "rip.route_tag", FT_UINT16, BASE_DEC,
122     NULL, 0, NULL, HFILL };
123
124 static header_field_info hfi_rip_zero_padding RIP_HFI_INIT = {
125     "Zero adding", "rip.zero_padding", FT_STRING, BASE_NONE,
126     NULL, 0, "Authentication password", HFILL };
127
128 static header_field_info hfi_rip_digest_offset RIP_HFI_INIT = {
129     "Digest Offset", "rip.digest_offset", FT_UINT16, BASE_DEC,
130     NULL, 0, NULL, HFILL };
131
132 static header_field_info hfi_rip_key_id RIP_HFI_INIT = {
133     "Key ID", "rip.key_id", FT_UINT8, BASE_DEC,
134     NULL, 0, NULL, HFILL };
135
136 static header_field_info hfi_rip_auth_data_len RIP_HFI_INIT = {
137     "Auth Data Len", "rip.auth_data_len", FT_UINT8, BASE_DEC,
138     NULL, 0, NULL, HFILL };
139
140 static header_field_info hfi_rip_auth_seq_num RIP_HFI_INIT = {
141     "Seq num", "rip.seq_num", FT_UINT32, BASE_DEC,
142     NULL, 0, NULL, HFILL };
143
144 static header_field_info hfi_rip_authentication_data RIP_HFI_INIT = {
145     "Authentication Data", "rip.authentication_data", FT_BYTES, BASE_NONE,
146     NULL, 0, NULL, HFILL };
147
148 static gint ett_rip = -1;
149 static gint ett_rip_vec = -1;
150 static gint ett_auth_vec = -1;
151
152 static expert_field ei_rip_unknown_address_family = EI_INIT;
153
154 static void dissect_unspec_rip_vektor(tvbuff_t *tvb, int offset, guint8 version,
155     proto_tree *tree);
156 static void dissect_ip_rip_vektor(tvbuff_t *tvb, int offset, guint8 version,
157     proto_tree *tree);
158 static gint dissect_rip_authentication(tvbuff_t *tvb, int offset,
159     proto_tree *tree);
160
161 static int
162 dissect_rip(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
163 {
164     int         offset      = 0;
165     proto_tree *rip_tree    = NULL;
166     proto_item *ti;
167     guint8      command;
168     guint8      version;
169     guint16     family;
170     gint        trailer_len = 0;
171     gboolean    is_md5_auth = FALSE;
172
173     col_set_str(pinfo->cinfo, COL_PROTOCOL, "RIP");
174     col_clear(pinfo->cinfo, COL_INFO);
175
176     command = tvb_get_guint8(tvb, 0);
177     version = tvb_get_guint8(tvb, 1);
178
179     col_set_str(pinfo->cinfo, COL_PROTOCOL,
180                     val_to_str_const(version, version_vals, "RIP"));
181     col_add_str(pinfo->cinfo, COL_INFO,
182                     val_to_str(command, command_vals, "Unknown command (%u)"));
183
184     ti = proto_tree_add_item(tree, hfi_rip, tvb, 0, -1, ENC_NA);
185     rip_tree = proto_item_add_subtree(ti, ett_rip);
186
187     proto_tree_add_uint(rip_tree, &hfi_rip_command, tvb, 0, 1, command);
188     proto_tree_add_uint(rip_tree, &hfi_rip_version, tvb, 1, 1, version);
189     if (version == RIPv2 && pref_display_routing_domain == TRUE)
190         proto_tree_add_item(rip_tree, &hfi_rip_routing_domain, tvb, 2, 2,
191                     ENC_BIG_ENDIAN);
192
193     /* skip header */
194     offset = RIP_HEADER_LENGTH;
195
196     /* zero or more entries */
197     while (tvb_reported_length_remaining(tvb, offset) > trailer_len ) {
198         family = tvb_get_ntohs(tvb, offset);
199         switch (family) {
200         case AFVAL_UNSPEC: /* Unspecified */
201             /*
202                 * There should be one entry in the request, and a metric
203                 * of infinity, meaning "show the entire routing table".
204                 */
205             dissect_unspec_rip_vektor(tvb, offset, version, rip_tree);
206             break;
207         case AFVAL_IP: /* IP */
208             dissect_ip_rip_vektor(tvb, offset, version, rip_tree);
209             break;
210         case 0xFFFF:
211             if( offset == RIP_HEADER_LENGTH ) {
212                     trailer_len=dissect_rip_authentication(tvb, offset, rip_tree);
213                     is_md5_auth = TRUE;
214             break;
215             }
216             if(is_md5_auth && tvb_reported_length_remaining(tvb, offset) == 20)
217                     break;
218             /* Intentional fall through */ /* auth Entry MUST be the first! */
219         default:
220             proto_tree_add_expert_format(rip_tree, pinfo, &ei_rip_unknown_address_family, tvb, offset,
221                             RIP_ENTRY_LENGTH, "Unknown address family %u", family);
222             break;
223         }
224
225         offset += RIP_ENTRY_LENGTH;
226     }
227     return tvb_captured_length(tvb);
228 }
229
230 static void
231 dissect_unspec_rip_vektor(tvbuff_t *tvb, int offset, guint8 version,
232                       proto_tree *tree)
233 {
234     proto_tree *rip_vektor_tree;
235     guint32     metric;
236
237     metric = tvb_get_ntohl(tvb, offset+16);
238     rip_vektor_tree = proto_tree_add_subtree_format(tree, tvb, offset,
239                              RIP_ENTRY_LENGTH, ett_rip_vec, NULL, "Address not specified, Metric: %u",
240                              metric);
241
242     proto_tree_add_item(rip_vektor_tree, &hfi_rip_family, tvb, offset, 2, ENC_BIG_ENDIAN);
243     if (version == RIPv2) {
244         proto_tree_add_item(rip_vektor_tree, &hfi_rip_route_tag, tvb, offset+2, 2,
245                         ENC_BIG_ENDIAN);
246         proto_tree_add_item(rip_vektor_tree, &hfi_rip_netmask, tvb, offset+8, 4,
247                             ENC_BIG_ENDIAN);
248         proto_tree_add_item(rip_vektor_tree, &hfi_rip_next_hop, tvb, offset+12, 4,
249                             ENC_BIG_ENDIAN);
250     }
251     proto_tree_add_uint(rip_vektor_tree, &hfi_rip_metric, tvb,
252                         offset+16, 4, metric);
253 }
254
255 static void
256 dissect_ip_rip_vektor(tvbuff_t *tvb, int offset, guint8 version,
257                       proto_tree *tree)
258 {
259     proto_tree *rip_vektor_tree;
260     guint32     metric;
261
262     metric = tvb_get_ntohl(tvb, offset+16);
263     rip_vektor_tree = proto_tree_add_subtree_format(tree, tvb, offset,
264                              RIP_ENTRY_LENGTH, ett_rip_vec, NULL, "IP Address: %s, Metric: %u",
265                              tvb_ip_to_str(tvb, offset+4), metric);
266
267     proto_tree_add_item(rip_vektor_tree, &hfi_rip_family, tvb, offset, 2, ENC_BIG_ENDIAN);
268     if (version == RIPv2) {
269         proto_tree_add_item(rip_vektor_tree, &hfi_rip_route_tag, tvb, offset+2, 2,
270                         ENC_BIG_ENDIAN);
271     }
272
273     proto_tree_add_item(rip_vektor_tree, &hfi_rip_ip, tvb, offset+4, 4, ENC_BIG_ENDIAN);
274
275     if (version == RIPv2) {
276         proto_tree_add_item(rip_vektor_tree, &hfi_rip_netmask, tvb, offset+8, 4,
277                             ENC_BIG_ENDIAN);
278         proto_tree_add_item(rip_vektor_tree, &hfi_rip_next_hop, tvb, offset+12, 4,
279                             ENC_BIG_ENDIAN);
280     }
281     proto_tree_add_uint(rip_vektor_tree, &hfi_rip_metric, tvb,
282                         offset+16, 4, metric);
283 }
284
285 static gint
286 dissect_rip_authentication(tvbuff_t *tvb, int offset, proto_tree *tree)
287 {
288     proto_tree *rip_authentication_tree;
289     guint16     authtype;
290     guint32     digest_off, auth_data_len;
291
292     auth_data_len = 0;
293     authtype = tvb_get_ntohs(tvb, offset + 2);
294
295     rip_authentication_tree = proto_tree_add_subtree_format(tree, tvb, offset, RIP_ENTRY_LENGTH,
296                         ett_rip_vec, NULL, "Authentication: %s", val_to_str( authtype, rip_auth_type, "Unknown (%u)" ) );
297
298     proto_tree_add_uint(rip_authentication_tree, &hfi_rip_auth, tvb, offset+2, 2,
299                 authtype);
300
301     switch ( authtype ) {
302
303     case AUTH_PASSWORD: /* Plain text password */
304         proto_tree_add_item(rip_authentication_tree, &hfi_rip_auth_passwd,
305                         tvb, offset+4, 16, ENC_ASCII|ENC_NA);
306         break;
307
308     case AUTH_KEYED_MSG_DIGEST: /* Keyed MD5 rfc 2082 */
309         digest_off = tvb_get_ntohs( tvb, offset+4 );
310         proto_tree_add_item( rip_authentication_tree, &hfi_rip_digest_offset, tvb, offset+4, 2, ENC_BIG_ENDIAN);
311         proto_tree_add_item( rip_authentication_tree, &hfi_rip_key_id, tvb, offset+6, 1, ENC_NA);
312         auth_data_len = tvb_get_guint8( tvb, offset+7 );
313         proto_tree_add_item( rip_authentication_tree, &hfi_rip_auth_data_len, tvb, offset+7, 1, ENC_NA);
314         proto_tree_add_item( rip_authentication_tree, &hfi_rip_auth_seq_num, tvb, offset+8, 4, ENC_BIG_ENDIAN);
315         proto_tree_add_item( rip_authentication_tree, &hfi_rip_zero_padding, tvb, offset+12, 8, ENC_NA);
316         rip_authentication_tree = proto_tree_add_subtree( rip_authentication_tree, tvb, offset-4+digest_off,
317                         MD5_AUTH_DATA_LEN+4, ett_auth_vec, NULL, "Authentication Data Trailer" );
318         proto_tree_add_item( rip_authentication_tree, &hfi_rip_authentication_data, tvb, offset-4+digest_off+4,
319                         MD5_AUTH_DATA_LEN, ENC_NA);
320         break;
321     }
322     return auth_data_len;
323 }
324
325 void
326 proto_register_rip(void)
327 {
328 #ifndef HAVE_HFI_SECTION_INIT
329     static header_field_info *hfi[] = {
330         &hfi_rip_command,
331         &hfi_rip_version,
332         &hfi_rip_routing_domain,
333         &hfi_rip_ip,
334         &hfi_rip_netmask,
335         &hfi_rip_next_hop,
336         &hfi_rip_metric,
337         &hfi_rip_auth,
338         &hfi_rip_auth_passwd,
339         &hfi_rip_family,
340         &hfi_rip_route_tag,
341         &hfi_rip_zero_padding,
342         &hfi_rip_digest_offset,
343         &hfi_rip_key_id,
344         &hfi_rip_auth_data_len,
345         &hfi_rip_auth_seq_num,
346         &hfi_rip_authentication_data,
347     };
348 #endif /* HAVE_HFI_SECTION_INIT */
349
350     static gint *ett[] = {
351         &ett_rip,
352         &ett_rip_vec,
353         &ett_auth_vec,
354     };
355
356     static ei_register_info ei[] = {
357         { &ei_rip_unknown_address_family, { "rip.unknown_address_family", PI_PROTOCOL, PI_WARN, "Unknown address family", EXPFILL }},
358     };
359
360     expert_module_t* expert_rip;
361     module_t *rip_module;
362     int proto_rip;
363
364     proto_rip = proto_register_protocol("Routing Information Protocol", "RIP", "rip");
365     hfi_rip = proto_registrar_get_nth(proto_rip);
366
367     proto_register_fields(proto_rip, hfi, array_length(hfi));
368     proto_register_subtree_array(ett, array_length(ett));
369     expert_rip = expert_register_protocol(proto_rip);
370     expert_register_field_array(expert_rip, ei, array_length(ei));
371
372     rip_module = prefs_register_protocol(proto_rip, NULL);
373
374     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);
375
376     rip_handle = create_dissector_handle(dissect_rip, proto_rip);
377 }
378
379 void
380 proto_reg_handoff_rip(void)
381 {
382     dissector_add_uint_with_preference("udp.port", UDP_PORT_RIP, rip_handle);
383 }
384
385 /*
386  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
387  *
388  * Local variables:
389  * c-basic-offset: 4
390  * tab-width: 8
391  * indent-tabs-mode: nil
392  * End:
393  *
394  * vi: set shiftwidth=4 tabstop=8 expandtab:
395  * :indentSize=4:tabSize=8:noTabs=true:
396  */