Use "gfloat" and "gdouble", rather than "float" and "double", as the
[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.31 2002/04/04 23:20:33 guy 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 #ifdef HAVE_SYS_TYPES_H
30 #include <sys/types.h>
31 #endif
32
33 #ifdef HAVE_NETINET_IN_H
34 #include <netinet/in.h>
35 #endif
36
37 #include <string.h>
38 #include <glib.h>
39 #include <epan/packet.h>
40
41 #define UDP_PORT_RIP    520
42
43 #define RIPv1   1
44 #define RIPv2   2
45
46 static const value_string version_vals[] = {
47         { RIPv1, "RIPv1" },
48         { RIPv2, "RIPv2" },
49         { 0,     NULL }
50 };
51
52 static const value_string command_vals[] = {
53         { 1, "Request" },
54         { 2, "Response" },
55         { 3, "Traceon" },
56         { 4, "Traceoff" },
57         { 5, "Vendor specific (Sun)" },
58         { 0, NULL }
59 };
60
61 #define AFVAL_UNSPEC    0
62 #define AFVAL_IP        2
63
64 static const value_string family_vals[] = {
65         { AFVAL_UNSPEC, "Unspecified" },
66         { AFVAL_IP,     "IP" },
67         { 0,            NULL }
68 };
69
70 #define RIP_HEADER_LENGTH 4
71 #define RIP_ENTRY_LENGTH 20
72
73 static int proto_rip = -1;
74 static int hf_rip_command = -1;
75 static int hf_rip_version = -1;
76 static int hf_rip_routing_domain = -1;
77 static int hf_rip_ip = -1;
78 static int hf_rip_netmask = -1;
79 static int hf_rip_next_hop = -1;
80 static int hf_rip_metric = -1;
81 static int hf_rip_auth = -1;
82 static int hf_rip_auth_passwd = -1;
83 static int hf_rip_family = -1;
84 static int hf_rip_route_tag = -1;
85
86 static gint ett_rip = -1;
87 static gint ett_rip_vec = -1;
88
89 static void dissect_unspec_rip_vektor(tvbuff_t *tvb, int offset, guint8 version,
90     proto_tree *tree);
91 static void dissect_ip_rip_vektor(tvbuff_t *tvb, int offset, guint8 version,
92     proto_tree *tree);
93 static void dissect_rip_authentication(tvbuff_t *tvb, int offset,
94     proto_tree *tree);
95
96 static void 
97 dissect_rip(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
98 {
99     int offset = 0;
100     proto_tree *rip_tree = NULL;
101     proto_item *ti;
102     guint8 command;
103     guint8 version;
104     guint16 family;
105
106     if (check_col(pinfo->cinfo, COL_PROTOCOL))
107         col_set_str(pinfo->cinfo, COL_PROTOCOL, "RIP");
108     if (check_col(pinfo->cinfo, COL_INFO))
109         col_clear(pinfo->cinfo, COL_INFO);
110
111     command = tvb_get_guint8(tvb, 0);
112     version = tvb_get_guint8(tvb, 1);
113   
114     if (check_col(pinfo->cinfo, COL_PROTOCOL))
115         col_add_str(pinfo->cinfo, COL_PROTOCOL,
116                     val_to_str(version, version_vals, "RIP"));
117     if (check_col(pinfo->cinfo, COL_INFO))
118         col_add_str(pinfo->cinfo, COL_INFO,
119                     val_to_str(command, command_vals, "Unknown command (%u)"));
120
121     if (tree) {
122         ti = proto_tree_add_item(tree, proto_rip, tvb, 0, -1, FALSE);
123         rip_tree = proto_item_add_subtree(ti, ett_rip);
124
125         proto_tree_add_uint(rip_tree, hf_rip_command, tvb, 0, 1, command);
126         proto_tree_add_uint(rip_tree, hf_rip_version, tvb, 1, 1, version);
127         if (version == RIPv2)
128             proto_tree_add_uint(rip_tree, hf_rip_routing_domain, tvb, 2, 2,
129                         tvb_get_ntohs(tvb, 2));
130
131         /* skip header */
132         offset = RIP_HEADER_LENGTH;
133
134         /* zero or more entries */
135         while (tvb_reported_length_remaining(tvb, offset) > 0) {
136             family = tvb_get_ntohs(tvb, offset);
137             switch (family) {
138             case AFVAL_UNSPEC: /* Unspecified */
139                 /*
140                  * There should be one entry in the request, and a metric
141                  * of infinity, meaning "show the entire routing table".
142                  */
143                 dissect_unspec_rip_vektor(tvb, offset, version, rip_tree);
144                 break;
145             case AFVAL_IP: /* IP */
146                 dissect_ip_rip_vektor(tvb, offset, version, rip_tree);
147                 break;
148             case 0xFFFF:
149                 dissect_rip_authentication(tvb, offset, rip_tree);
150                 break;
151             default:
152                 proto_tree_add_text(rip_tree, tvb, offset,
153                                 RIP_ENTRY_LENGTH, "Unknown address family %u",
154                                 family);
155                 break;
156             }
157
158             offset += RIP_ENTRY_LENGTH;
159         }
160     }
161 }
162
163 static void
164 dissect_unspec_rip_vektor(tvbuff_t *tvb, int offset, guint8 version,
165                       proto_tree *tree)
166 {
167     proto_item *ti;
168     proto_tree *rip_vektor_tree;
169     guint32 metric;
170
171     metric = tvb_get_ntohl(tvb, offset+16);
172     ti = proto_tree_add_text(tree, tvb, offset,
173                              RIP_ENTRY_LENGTH, "Address not specified, Metric: %u",
174                              metric);
175     rip_vektor_tree = proto_item_add_subtree(ti, ett_rip_vec);
176
177     proto_tree_add_item(rip_vektor_tree, hf_rip_family, tvb, offset, 2, FALSE);
178     if (version == RIPv2) {
179         proto_tree_add_item(rip_vektor_tree, hf_rip_route_tag, tvb, offset+2, 2,
180                         FALSE);
181         proto_tree_add_item(rip_vektor_tree, hf_rip_netmask, tvb, offset+8, 4,
182                             FALSE);
183         proto_tree_add_item(rip_vektor_tree, hf_rip_next_hop, tvb, offset+12, 4,
184                             FALSE);
185     }
186     proto_tree_add_uint(rip_vektor_tree, hf_rip_metric, tvb, 
187                         offset+16, 4, metric);
188 }
189
190 static void
191 dissect_ip_rip_vektor(tvbuff_t *tvb, int offset, guint8 version,
192                       proto_tree *tree)
193 {
194     proto_item *ti;
195     proto_tree *rip_vektor_tree;
196     guint32 metric;
197
198     metric = tvb_get_ntohl(tvb, offset+16);
199     ti = proto_tree_add_text(tree, tvb, offset,
200                              RIP_ENTRY_LENGTH, "IP Address: %s, Metric: %u",
201                              ip_to_str(tvb_get_ptr(tvb, offset+4, 4)), metric);
202     rip_vektor_tree = proto_item_add_subtree(ti, ett_rip_vec);
203
204     proto_tree_add_item(rip_vektor_tree, hf_rip_family, tvb, offset, 2, FALSE);
205     if (version == RIPv2) {
206         proto_tree_add_item(rip_vektor_tree, hf_rip_route_tag, tvb, offset+2, 2,
207                         FALSE);
208     }
209
210     proto_tree_add_item(rip_vektor_tree, hf_rip_ip, tvb, offset+4, 4, FALSE);
211
212     if (version == RIPv2) {
213         proto_tree_add_item(rip_vektor_tree, hf_rip_netmask, tvb, offset+8, 4,
214                             FALSE);
215         proto_tree_add_item(rip_vektor_tree, hf_rip_next_hop, tvb, offset+12, 4,
216                             FALSE);
217     }
218     proto_tree_add_uint(rip_vektor_tree, hf_rip_metric, tvb, 
219                         offset+16, 4, metric);
220 }
221
222 static void
223 dissect_rip_authentication(tvbuff_t *tvb, int offset, proto_tree *tree)
224 {
225     proto_item *ti;
226     proto_tree *rip_authentication_tree;
227     guint16 authtype;
228
229     ti = proto_tree_add_text(tree, tvb, offset, RIP_ENTRY_LENGTH,
230                              "Authentication");
231     rip_authentication_tree = proto_item_add_subtree(ti, ett_rip_vec);
232
233     authtype = tvb_get_ntohs(tvb, offset + 2);
234     proto_tree_add_uint(rip_authentication_tree, hf_rip_auth, tvb, offset+2, 2,
235                 authtype);
236     if (authtype == 2) {
237         proto_tree_add_item(rip_authentication_tree, hf_rip_auth_passwd,
238                         tvb, offset+4, 16, FALSE);
239     }
240 }
241
242 void
243 proto_register_rip(void)
244 {
245         static hf_register_info hf[] = {
246                 { &hf_rip_command,
247                         { "Command", "rip.command", FT_UINT8, BASE_DEC,
248                         VALS(command_vals), 0, "What type of RIP Command is this", HFILL }},
249
250                 { &hf_rip_version,
251                         { "Version", "rip.version", FT_UINT8, BASE_DEC,
252                         VALS(version_vals), 0, "Version of the RIP protocol", HFILL }},
253
254                 { &hf_rip_family,
255                         { "Address Family", "rip.family", FT_UINT16, BASE_DEC,
256                         VALS(family_vals), 0, "Address family", HFILL }},
257
258                 { &hf_rip_routing_domain,
259                         { "Routing Domain", "rip.routing_domain", FT_UINT16, BASE_DEC,
260                         NULL, 0, "RIPv2 Routing Domain", HFILL }},
261
262                 { &hf_rip_ip,
263                         { "IP Address", "rip.ip", FT_IPv4, BASE_NONE,
264                         NULL, 0, "IP Address", HFILL}},
265
266                 { &hf_rip_netmask,
267                         { "Netmask", "rip.netmask", FT_IPv4, BASE_NONE,
268                         NULL, 0, "Netmask", HFILL}},
269
270                 { &hf_rip_next_hop,
271                         { "Next Hop", "rip.next_hop", FT_IPv4, BASE_NONE,
272                         NULL, 0, "Next Hop router for this route", HFILL}},
273
274                 { &hf_rip_metric,
275                         { "Metric", "rip.metric", FT_UINT16, BASE_DEC,
276                         NULL, 0, "Metric for this route", HFILL }},
277
278                 { &hf_rip_auth,
279                         { "Authentication type", "rip.auth.type", FT_UINT16, BASE_DEC,
280                         NULL, 0, "Type of authentication", HFILL }},
281
282                 { &hf_rip_auth_passwd,
283                         { "Password", "rip.auth.passwd", FT_STRING, BASE_DEC,
284                         NULL, 0, "Authentication password", HFILL }},
285
286                 { &hf_rip_route_tag,
287                         { "Route Tag", "rip.route_tag", FT_UINT16, BASE_DEC,
288                         NULL, 0, "Route Tag", HFILL }},
289
290         };
291         static gint *ett[] = {
292                 &ett_rip,
293                 &ett_rip_vec,
294         };
295
296         proto_rip = proto_register_protocol("Routing Information Protocol",
297                                 "RIP", "rip");
298         proto_register_field_array(proto_rip, hf, array_length(hf));
299         proto_register_subtree_array(ett, array_length(ett));
300 }
301
302 void
303 proto_reg_handoff_rip(void)
304 {
305         dissector_handle_t rip_handle;
306
307         rip_handle = create_dissector_handle(dissect_rip, proto_rip);
308         dissector_add("udp.port", UDP_PORT_RIP, rip_handle);
309 }