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