Include packet-wlancap.h into the tarball
[obnox/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  * $Id: packet-rip.c,v 1.33 2002/08/28 21:00:29 jmayer Exp $
7  *
8  * Ethereal - Network traffic analyzer
9  * By Gerald Combs <gerald@ethereal.com>
10  * Copyright 1998 Gerald Combs
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25  */
26
27 #include "config.h"
28
29 #include <string.h>
30 #include <glib.h>
31 #include <epan/packet.h>
32
33 #define UDP_PORT_RIP    520
34
35 #define RIPv1   1
36 #define RIPv2   2
37
38 static const value_string version_vals[] = {
39         { RIPv1, "RIPv1" },
40         { RIPv2, "RIPv2" },
41         { 0,     NULL }
42 };
43
44 static const value_string command_vals[] = {
45         { 1, "Request" },
46         { 2, "Response" },
47         { 3, "Traceon" },
48         { 4, "Traceoff" },
49         { 5, "Vendor specific (Sun)" },
50         { 0, NULL }
51 };
52
53 #define AFVAL_UNSPEC    0
54 #define AFVAL_IP        2
55
56 static const value_string family_vals[] = {
57         { AFVAL_UNSPEC, "Unspecified" },
58         { AFVAL_IP,     "IP" },
59         { 0,            NULL }
60 };
61
62 #define RIP_HEADER_LENGTH 4
63 #define RIP_ENTRY_LENGTH 20
64
65 static int proto_rip = -1;
66 static int hf_rip_command = -1;
67 static int hf_rip_version = -1;
68 static int hf_rip_routing_domain = -1;
69 static int hf_rip_ip = -1;
70 static int hf_rip_netmask = -1;
71 static int hf_rip_next_hop = -1;
72 static int hf_rip_metric = -1;
73 static int hf_rip_auth = -1;
74 static int hf_rip_auth_passwd = -1;
75 static int hf_rip_family = -1;
76 static int hf_rip_route_tag = -1;
77
78 static gint ett_rip = -1;
79 static gint ett_rip_vec = -1;
80
81 static void dissect_unspec_rip_vektor(tvbuff_t *tvb, int offset, guint8 version,
82     proto_tree *tree);
83 static void dissect_ip_rip_vektor(tvbuff_t *tvb, int offset, guint8 version,
84     proto_tree *tree);
85 static void dissect_rip_authentication(tvbuff_t *tvb, int offset,
86     proto_tree *tree);
87
88 static void
89 dissect_rip(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
90 {
91     int offset = 0;
92     proto_tree *rip_tree = NULL;
93     proto_item *ti;
94     guint8 command;
95     guint8 version;
96     guint16 family;
97
98     if (check_col(pinfo->cinfo, COL_PROTOCOL))
99         col_set_str(pinfo->cinfo, COL_PROTOCOL, "RIP");
100     if (check_col(pinfo->cinfo, COL_INFO))
101         col_clear(pinfo->cinfo, COL_INFO);
102
103     command = tvb_get_guint8(tvb, 0);
104     version = tvb_get_guint8(tvb, 1);
105
106     if (check_col(pinfo->cinfo, COL_PROTOCOL))
107         col_add_str(pinfo->cinfo, COL_PROTOCOL,
108                     val_to_str(version, version_vals, "RIP"));
109     if (check_col(pinfo->cinfo, COL_INFO))
110         col_add_str(pinfo->cinfo, COL_INFO,
111                     val_to_str(command, command_vals, "Unknown command (%u)"));
112
113     if (tree) {
114         ti = proto_tree_add_item(tree, proto_rip, tvb, 0, -1, FALSE);
115         rip_tree = proto_item_add_subtree(ti, ett_rip);
116
117         proto_tree_add_uint(rip_tree, hf_rip_command, tvb, 0, 1, command);
118         proto_tree_add_uint(rip_tree, hf_rip_version, tvb, 1, 1, version);
119         if (version == RIPv2)
120             proto_tree_add_uint(rip_tree, hf_rip_routing_domain, tvb, 2, 2,
121                         tvb_get_ntohs(tvb, 2));
122
123         /* skip header */
124         offset = RIP_HEADER_LENGTH;
125
126         /* zero or more entries */
127         while (tvb_reported_length_remaining(tvb, offset) > 0) {
128             family = tvb_get_ntohs(tvb, offset);
129             switch (family) {
130             case AFVAL_UNSPEC: /* Unspecified */
131                 /*
132                  * There should be one entry in the request, and a metric
133                  * of infinity, meaning "show the entire routing table".
134                  */
135                 dissect_unspec_rip_vektor(tvb, offset, version, rip_tree);
136                 break;
137             case AFVAL_IP: /* IP */
138                 dissect_ip_rip_vektor(tvb, offset, version, rip_tree);
139                 break;
140             case 0xFFFF:
141                 dissect_rip_authentication(tvb, offset, rip_tree);
142                 break;
143             default:
144                 proto_tree_add_text(rip_tree, tvb, offset,
145                                 RIP_ENTRY_LENGTH, "Unknown address family %u",
146                                 family);
147                 break;
148             }
149
150             offset += RIP_ENTRY_LENGTH;
151         }
152     }
153 }
154
155 static void
156 dissect_unspec_rip_vektor(tvbuff_t *tvb, int offset, guint8 version,
157                       proto_tree *tree)
158 {
159     proto_item *ti;
160     proto_tree *rip_vektor_tree;
161     guint32 metric;
162
163     metric = tvb_get_ntohl(tvb, offset+16);
164     ti = proto_tree_add_text(tree, tvb, offset,
165                              RIP_ENTRY_LENGTH, "Address not specified, Metric: %u",
166                              metric);
167     rip_vektor_tree = proto_item_add_subtree(ti, ett_rip_vec);
168
169     proto_tree_add_item(rip_vektor_tree, hf_rip_family, tvb, offset, 2, FALSE);
170     if (version == RIPv2) {
171         proto_tree_add_item(rip_vektor_tree, hf_rip_route_tag, tvb, offset+2, 2,
172                         FALSE);
173         proto_tree_add_item(rip_vektor_tree, hf_rip_netmask, tvb, offset+8, 4,
174                             FALSE);
175         proto_tree_add_item(rip_vektor_tree, hf_rip_next_hop, tvb, offset+12, 4,
176                             FALSE);
177     }
178     proto_tree_add_uint(rip_vektor_tree, hf_rip_metric, tvb,
179                         offset+16, 4, metric);
180 }
181
182 static void
183 dissect_ip_rip_vektor(tvbuff_t *tvb, int offset, guint8 version,
184                       proto_tree *tree)
185 {
186     proto_item *ti;
187     proto_tree *rip_vektor_tree;
188     guint32 metric;
189
190     metric = tvb_get_ntohl(tvb, offset+16);
191     ti = proto_tree_add_text(tree, tvb, offset,
192                              RIP_ENTRY_LENGTH, "IP Address: %s, Metric: %u",
193                              ip_to_str(tvb_get_ptr(tvb, offset+4, 4)), metric);
194     rip_vektor_tree = proto_item_add_subtree(ti, ett_rip_vec);
195
196     proto_tree_add_item(rip_vektor_tree, hf_rip_family, tvb, offset, 2, FALSE);
197     if (version == RIPv2) {
198         proto_tree_add_item(rip_vektor_tree, hf_rip_route_tag, tvb, offset+2, 2,
199                         FALSE);
200     }
201
202     proto_tree_add_item(rip_vektor_tree, hf_rip_ip, tvb, offset+4, 4, FALSE);
203
204     if (version == RIPv2) {
205         proto_tree_add_item(rip_vektor_tree, hf_rip_netmask, tvb, offset+8, 4,
206                             FALSE);
207         proto_tree_add_item(rip_vektor_tree, hf_rip_next_hop, tvb, offset+12, 4,
208                             FALSE);
209     }
210     proto_tree_add_uint(rip_vektor_tree, hf_rip_metric, tvb,
211                         offset+16, 4, metric);
212 }
213
214 static void
215 dissect_rip_authentication(tvbuff_t *tvb, int offset, proto_tree *tree)
216 {
217     proto_item *ti;
218     proto_tree *rip_authentication_tree;
219     guint16 authtype;
220
221     ti = proto_tree_add_text(tree, tvb, offset, RIP_ENTRY_LENGTH,
222                              "Authentication");
223     rip_authentication_tree = proto_item_add_subtree(ti, ett_rip_vec);
224
225     authtype = tvb_get_ntohs(tvb, offset + 2);
226     proto_tree_add_uint(rip_authentication_tree, hf_rip_auth, tvb, offset+2, 2,
227                 authtype);
228     if (authtype == 2) {
229         proto_tree_add_item(rip_authentication_tree, hf_rip_auth_passwd,
230                         tvb, offset+4, 16, FALSE);
231     }
232 }
233
234 void
235 proto_register_rip(void)
236 {
237         static hf_register_info hf[] = {
238                 { &hf_rip_command,
239                         { "Command", "rip.command", FT_UINT8, BASE_DEC,
240                         VALS(command_vals), 0, "What type of RIP Command is this", HFILL }},
241
242                 { &hf_rip_version,
243                         { "Version", "rip.version", FT_UINT8, BASE_DEC,
244                         VALS(version_vals), 0, "Version of the RIP protocol", HFILL }},
245
246                 { &hf_rip_family,
247                         { "Address Family", "rip.family", FT_UINT16, BASE_DEC,
248                         VALS(family_vals), 0, "Address family", HFILL }},
249
250                 { &hf_rip_routing_domain,
251                         { "Routing Domain", "rip.routing_domain", FT_UINT16, BASE_DEC,
252                         NULL, 0, "RIPv2 Routing Domain", HFILL }},
253
254                 { &hf_rip_ip,
255                         { "IP Address", "rip.ip", FT_IPv4, BASE_NONE,
256                         NULL, 0, "IP Address", HFILL}},
257
258                 { &hf_rip_netmask,
259                         { "Netmask", "rip.netmask", FT_IPv4, BASE_NONE,
260                         NULL, 0, "Netmask", HFILL}},
261
262                 { &hf_rip_next_hop,
263                         { "Next Hop", "rip.next_hop", FT_IPv4, BASE_NONE,
264                         NULL, 0, "Next Hop router for this route", HFILL}},
265
266                 { &hf_rip_metric,
267                         { "Metric", "rip.metric", FT_UINT16, BASE_DEC,
268                         NULL, 0, "Metric for this route", HFILL }},
269
270                 { &hf_rip_auth,
271                         { "Authentication type", "rip.auth.type", FT_UINT16, BASE_DEC,
272                         NULL, 0, "Type of authentication", HFILL }},
273
274                 { &hf_rip_auth_passwd,
275                         { "Password", "rip.auth.passwd", FT_STRING, BASE_DEC,
276                         NULL, 0, "Authentication password", HFILL }},
277
278                 { &hf_rip_route_tag,
279                         { "Route Tag", "rip.route_tag", FT_UINT16, BASE_DEC,
280                         NULL, 0, "Route Tag", HFILL }},
281
282         };
283         static gint *ett[] = {
284                 &ett_rip,
285                 &ett_rip_vec,
286         };
287
288         proto_rip = proto_register_protocol("Routing Information Protocol",
289                                 "RIP", "rip");
290         proto_register_field_array(proto_rip, hf, array_length(hf));
291         proto_register_subtree_array(ett, array_length(ett));
292 }
293
294 void
295 proto_reg_handoff_rip(void)
296 {
297         dissector_handle_t rip_handle;
298
299         rip_handle = create_dissector_handle(dissect_rip, proto_rip);
300         dissector_add("udp.port", UDP_PORT_RIP, rip_handle);
301 }