Update manuf to current IEEE entries.
[obnox/wireshark/wip.git] / packet-aodv.c
1 /* packet-aodv.c
2  * Routines for AODV dissection
3  * Copyright 2000, Erik Nordström <erik.nordstrom@it.uu.se>
4  *
5  * $Id: packet-aodv.c,v 1.6 2002/08/28 21:00:07 jmayer 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 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34 #include <glib.h>
35
36 #include <epan/packet.h>
37
38 #define UDP_PORT_AODV 654
39
40 #define RREQ 1
41 #define RREP 2
42 #define RERR 3
43
44 /* Flag bits: */
45 #define RREQ_GRAT    0x20
46 #define RREQ_REP     0x40
47 #define RREQ_JOIN    0x80
48
49 #define RREP_ACK     0x40
50 #define RREP_REP     0x80
51
52 #define RERR_NODEL   0x80
53
54 static const value_string type_vals[] = {
55     { RREQ, "RREQ" },
56     { RREP, "RREP" },
57     { RERR, "RERR" },
58     { 0, NULL }
59 };
60
61 struct aodv_rreq {
62     guint8 type;
63     guint8 flags;
64     guint8 res;
65     guint8 hop_count;
66     guint32 rreq_id;
67     guint32 dest_addr;
68     guint32 dest_seqno;
69     guint32 orig_addr;
70     guint32 orig_seqno;
71 };
72
73 struct aodv_rrep {
74     guint8 type;
75     guint8 flags;
76     guint8 prefix_sz;
77     guint8 hop_count;
78     guint32 dest_addr;
79     guint32 dest_seqno;
80     guint32 orig_addr;
81     guint32 lifetime;
82 };
83
84 struct aodv_rerr {
85     guint8 type;
86     guint8 flags;
87     guint8 res;
88     guint8 dest_count;
89     guint32 dest_addr;
90     guint32 dest_seqno;
91 };
92
93 /* Initialize the protocol and registered fields */
94 static int proto_aodv = -1;
95 static int hf_aodv_type = -1;
96 static int hf_aodv_flags = -1;
97 static int hf_aodv_prefix_sz = -1;
98 static int hf_aodv_hopcount = -1;
99 static int hf_aodv_rreq_id = -1;
100 static int hf_aodv_dest_ip = -1;
101 static int hf_aodv_dest_seqno = -1;
102 static int hf_aodv_orig_ip = -1;
103 static int hf_aodv_orig_seqno = -1;
104 static int hf_aodv_lifetime = -1;
105 static int hf_aodv_destcount = -1;
106 static int hf_aodv_unreach_dest_ip = -1;
107 static int hf_aodv_unreach_dest_seqno = -1;
108 static int hf_aodv_flags_rreq_join = -1;
109 static int hf_aodv_flags_rreq_repair = -1;
110 static int hf_aodv_flags_rreq_gratuitous = -1;
111 static int hf_aodv_flags_rrep_repair = -1;
112 static int hf_aodv_flags_rrep_ack = -1;
113 static int hf_aodv_flags_rerr_nodelete = -1;
114
115 /* Initialize the subtree pointers */
116 static gint ett_aodv = -1;
117 static gint ett_aodv_flags = -1;
118 static gint ett_aodv_unreach_dest = -1;
119
120 /* Code to actually dissect the packets */
121 static int
122 dissect_aodv(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
123 {
124     proto_item *ti = NULL, *tj = NULL, *tk = NULL;
125     proto_tree *aodv_tree = NULL, *aodv_flags_tree = NULL,
126         *aodv_unreach_dest_tree = NULL;
127     guint8 type;
128     int i;
129     struct aodv_rreq rreq;
130     struct aodv_rrep rrep;
131     struct aodv_rerr rerr;
132
133 /* Make entries in Protocol column and Info column on summary display */
134     if (check_col(pinfo->cinfo, COL_PROTOCOL))
135         col_set_str(pinfo->cinfo, COL_PROTOCOL, "AODV");
136
137     if (check_col(pinfo->cinfo, COL_INFO))
138         col_clear(pinfo->cinfo, COL_INFO);
139
140     /* Check the type of AODV packet. */
141     type = tvb_get_guint8(tvb, 0);
142     if (type < 1 || type > 3) {
143         /*
144          * We assume this is not an AODV packet.
145          */
146         return 0;
147     }
148
149     if (tree) {
150         ti = proto_tree_add_protocol_format(tree, proto_aodv, tvb, 0, -1,
151             "Ad hoc On-demand Distance Vector Routing Protocol, %s",
152             val_to_str(type, type_vals, "Unknown AODV Packet Type (%u)"));
153         aodv_tree = proto_item_add_subtree(ti, ett_aodv);
154
155         proto_tree_add_uint(aodv_tree, hf_aodv_type, tvb, 0, 1, type);
156         tj = proto_tree_add_text(aodv_tree, tvb, 1, 1, "Flags:");
157         aodv_flags_tree = proto_item_add_subtree(tj, ett_aodv_flags);
158     }
159
160
161     switch (type) {
162     case RREQ:
163         rreq.type = type;
164         rreq.flags = tvb_get_guint8(tvb, 1);
165         rreq.hop_count = tvb_get_guint8(tvb, 3);
166         rreq.rreq_id = tvb_get_ntohl(tvb, 4);
167         tvb_memcpy(tvb, (guint8 *)&rreq.dest_addr, 8, 4);
168         rreq.dest_seqno = tvb_get_ntohl(tvb, 12);
169         tvb_memcpy(tvb, (guint8 *)&rreq.orig_addr, 16, 4);
170         rreq.orig_seqno = tvb_get_ntohl(tvb, 20);
171
172         if (tree) {
173             proto_tree_add_boolean(aodv_flags_tree, hf_aodv_flags_rreq_join, tvb, 1, 1, rreq.flags);
174             proto_tree_add_boolean(aodv_flags_tree, hf_aodv_flags_rreq_repair, tvb, 1, 1, rreq.flags);
175             proto_tree_add_boolean(aodv_flags_tree, hf_aodv_flags_rreq_gratuitous, tvb, 1, 1, rreq.flags);
176             if (rreq.flags & RREQ_JOIN)
177                 proto_item_append_text(tj, " J");
178             if (rreq.flags & RREQ_REP)
179                 proto_item_append_text(tj, " R");
180             if (rreq.flags & RREQ_GRAT)
181                 proto_item_append_text(tj, " G");
182             proto_tree_add_uint(aodv_tree, hf_aodv_hopcount, tvb, 3, 1, rreq.hop_count);
183             proto_tree_add_uint(aodv_tree, hf_aodv_rreq_id, tvb, 4, 4, rreq.rreq_id);
184             proto_tree_add_ipv4(aodv_tree, hf_aodv_dest_ip, tvb, 8, 4, rreq.dest_addr);
185             proto_tree_add_uint(aodv_tree, hf_aodv_dest_seqno, tvb, 12, 4, rreq.dest_seqno);
186             proto_tree_add_ipv4(aodv_tree, hf_aodv_orig_ip, tvb, 16, 4, rreq.orig_addr);
187             proto_tree_add_uint(aodv_tree, hf_aodv_orig_seqno, tvb, 20, 4, rreq.orig_seqno);
188             proto_item_append_text(ti, ", Dest IP: %s, Orig IP: %s, Id=%u", ip_to_str(tvb_get_ptr(tvb, 8, 4)), ip_to_str(tvb_get_ptr(tvb, 16, 4)), rreq.rreq_id);
189         }
190
191         if (check_col(pinfo->cinfo, COL_INFO))
192             col_add_fstr(pinfo->cinfo, COL_INFO, "%s, D: %s O: %s Id=%u Hcnt=%u DSN=%u OSN=%u",
193                          val_to_str(type, type_vals,
194                                     "Unknown AODV Packet Type (%u)"),
195                          ip_to_str(tvb_get_ptr(tvb, 8, 4)),
196                          ip_to_str(tvb_get_ptr(tvb, 16, 4)),
197                          rreq.rreq_id,
198                          rreq.hop_count,
199                          rreq.dest_seqno,
200                          rreq.orig_seqno);
201
202         break;
203     case RREP:
204         rrep.type = type;
205         rrep.flags = tvb_get_guint8(tvb, 1);
206         rrep.prefix_sz = tvb_get_guint8(tvb, 2) & 0x1F;
207         rrep.hop_count = tvb_get_guint8(tvb, 3);
208         tvb_memcpy(tvb, (guint8 *)&rrep.dest_addr, 4, 4);
209         rrep.dest_seqno = tvb_get_ntohl(tvb, 8);
210         tvb_memcpy(tvb, (guint8 *)&rrep.orig_addr, 12, 4);
211         rrep.lifetime = tvb_get_ntohl(tvb, 16);
212
213         if (tree) {
214             proto_tree_add_boolean(aodv_flags_tree, hf_aodv_flags_rrep_repair, tvb, 1, 1, rrep.flags);
215             proto_tree_add_boolean(aodv_flags_tree, hf_aodv_flags_rrep_ack, tvb, 1, 1, rrep.flags);
216             if (rrep.flags & RREP_REP)
217                 proto_item_append_text(tj, " R");
218             if (rrep.flags & RREP_ACK)
219                 proto_item_append_text(tj, " A");
220             proto_tree_add_uint(aodv_tree, hf_aodv_prefix_sz, tvb, 3, 1, rrep.prefix_sz);
221             proto_tree_add_uint(aodv_tree, hf_aodv_hopcount, tvb, 3, 1, rrep.hop_count);
222             proto_tree_add_ipv4(aodv_tree, hf_aodv_dest_ip, tvb, 4, 4, rrep.dest_addr);
223             proto_tree_add_uint(aodv_tree, hf_aodv_dest_seqno, tvb, 8, 4, rrep.dest_seqno);
224             proto_tree_add_ipv4(aodv_tree, hf_aodv_orig_ip, tvb, 12, 4, rrep.orig_addr);
225             proto_tree_add_uint(aodv_tree, hf_aodv_lifetime, tvb, 16, 4, rrep.lifetime);
226             proto_item_append_text(ti, ", Dest IP: %s, Orig IP: %s, Lifetime=%u", ip_to_str(tvb_get_ptr(tvb, 4, 4)), ip_to_str(tvb_get_ptr(tvb, 12, 4)), rrep.lifetime);
227         }
228
229         if (check_col(pinfo->cinfo, COL_INFO))
230             col_add_fstr(pinfo->cinfo, COL_INFO, "%s D: %s O: %s Hcnt=%u DSN=%u Lifetime=%u",
231                          val_to_str(type, type_vals,
232                                     "Unknown AODV Packet Type (%u)"),
233                          ip_to_str(tvb_get_ptr(tvb, 4, 4)),
234                          ip_to_str(tvb_get_ptr(tvb, 12, 4)),
235                          rrep.hop_count,
236                          rrep.dest_seqno,
237                          rrep.lifetime);
238         break;
239     case RERR:
240         rerr.type = type;
241         rerr.flags = tvb_get_guint8(tvb, 1);
242         rerr.dest_count = tvb_get_guint8(tvb, 3);
243
244         if (tree) {
245             proto_tree_add_boolean(aodv_flags_tree, hf_aodv_flags_rerr_nodelete, tvb, 1, 1, rerr.flags);
246             if (rerr.flags & RERR_NODEL)
247                 proto_item_append_text(tj, " N");
248             proto_tree_add_uint(aodv_tree, hf_aodv_destcount, tvb, 3, 1, rerr.dest_count);
249             tk = proto_tree_add_text(aodv_tree, tvb, 4, 8*rerr.dest_count, "Unreachable Destinations:");
250
251             aodv_unreach_dest_tree = proto_item_add_subtree(tk, ett_aodv_unreach_dest);
252             for (i = 0; i < rerr.dest_count; i++) {
253                 tvb_memcpy(tvb, (guint8 *)&rerr.dest_addr, 4+8*i, 4);
254                 rerr.dest_seqno = tvb_get_ntohl(tvb, 8+8*i);
255                 proto_tree_add_ipv4(aodv_unreach_dest_tree, hf_aodv_dest_ip, tvb, 4+8*i, 4, rerr.dest_addr);
256                 proto_tree_add_uint(aodv_unreach_dest_tree, hf_aodv_dest_seqno, tvb, 8+8*i, 4, rerr.dest_seqno);
257             }
258         }
259
260         if (check_col(pinfo->cinfo, COL_INFO))
261             col_add_fstr(pinfo->cinfo, COL_INFO, "%s, Dest Count=%u",
262                          val_to_str(type, type_vals,
263                                     "Unknown AODV Packet Type (%u)"),
264                          rerr.dest_count);
265         break;
266     default:
267         proto_tree_add_text(aodv_tree, tvb, 0,
268                             1, "Unknown AODV Packet Type (%u)",
269                             type);
270     }
271
272     return tvb_length(tvb);
273 }
274
275
276 /* Register the protocol with Ethereal */
277 void
278 proto_register_aodv(void)
279 {
280     static hf_register_info hf[] = {
281         { &hf_aodv_type,
282           { "Type", "aodv.type",
283             FT_UINT8, BASE_DEC, VALS(type_vals), 0x0,
284             "AODV packet type", HFILL }
285         },
286         { &hf_aodv_flags,
287           { "Flags", "aodv.flags",
288             FT_UINT16, BASE_DEC, NULL, 0x0,
289             "Flags", HFILL }
290         },
291         { &hf_aodv_flags_rreq_join,
292           { "RREQ Join", "aodv.flags.rreq_join",
293             FT_BOOLEAN, 8, TFS(&flags_set_truth), RREQ_JOIN,
294             "", HFILL }
295         },
296         { &hf_aodv_flags_rreq_repair,
297           { "RREQ Repair", "aodv.flags.rreq_repair",
298             FT_BOOLEAN, 8, TFS(&flags_set_truth), RREQ_REP,
299             "", HFILL }
300         },
301         { &hf_aodv_flags_rreq_gratuitous,
302           { "RREQ Gratuitous", "aodv.flags.rreq_gratuitous",
303             FT_BOOLEAN, 8, TFS(&flags_set_truth), RREQ_GRAT,
304             "", HFILL }
305         },
306         { &hf_aodv_flags_rrep_repair,
307           { "RREP Repair", "aodv.flags.rrep_repair",
308             FT_BOOLEAN, 8, TFS(&flags_set_truth), RREP_REP,
309             "", HFILL }
310         },
311         { &hf_aodv_flags_rrep_ack,
312           { "RREP Acknowledgement", "aodv.flags.rrep_ack",
313             FT_BOOLEAN, 8, TFS(&flags_set_truth), RREP_ACK,
314             "", HFILL }
315         },
316         { &hf_aodv_flags_rerr_nodelete,
317           { "RERR No Delete", "aodv.flags.rerr_nodelete",
318             FT_BOOLEAN, 8, TFS(&flags_set_truth), RERR_NODEL,
319             "", HFILL }
320         },
321         { &hf_aodv_prefix_sz,
322           { "Prefix Size", "aodv.prefix_sz",
323             FT_UINT8, BASE_DEC, NULL, 0x0,
324             "Prefix_size", HFILL }
325         },
326         { &hf_aodv_hopcount,
327           { "Hop Count", "aodv.hopcount",
328             FT_UINT8, BASE_DEC, NULL, 0x0,
329             "Hop Count", HFILL }
330         },
331         { &hf_aodv_rreq_id,
332           { "RREQ Id", "aodv.rreq_id",
333             FT_UINT32, BASE_DEC, NULL, 0x0,
334             "RREQ Id", HFILL }
335         },
336         { &hf_aodv_dest_ip,
337           { "Destination IP", "aodv.dest_ip",
338             FT_IPv4, BASE_DEC, NULL, 0x0,
339             "Destination IP Address", HFILL }
340         },
341         { &hf_aodv_dest_seqno,
342           { "Destination Sequence Number", "aodv.dest_seqno",
343             FT_UINT32, BASE_DEC, NULL, 0x0,
344             "Destination Sequence Number", HFILL }
345         },
346         { &hf_aodv_orig_ip,
347           { "Originator IP", "aodv.orig_ip",
348             FT_IPv4, BASE_DEC, NULL, 0x0,
349             "Originator IP Address", HFILL }
350         },
351         { &hf_aodv_orig_seqno,
352           { "Originator Sequence Number", "aodv.orig_seqno",
353             FT_UINT32, BASE_DEC, NULL, 0x0,
354             "Originator Sequence Number", HFILL }
355         },
356         { &hf_aodv_lifetime,
357           { "Lifetime", "aodv.lifetime",
358             FT_UINT32, BASE_DEC, NULL, 0x0,
359             "Lifetime", HFILL }
360         },
361         { &hf_aodv_destcount,
362           { "Destination Count", "aodv.destcount",
363             FT_UINT8, BASE_DEC, NULL, 0x0,
364             "Unreachable Destinations Count", HFILL }
365         },
366         { &hf_aodv_unreach_dest_ip,
367           { "Unreachable Destination IP", "aodv.unreach_dest_ip",
368             FT_IPv4, BASE_DEC, NULL, 0x0,
369             "Unreachable Destination  IP Address", HFILL }
370         },
371         { &hf_aodv_unreach_dest_seqno,
372           { "Unreachable Destination Sequence Number", "aodv.unreach_dest_seqno",
373             FT_UINT32, BASE_DEC, NULL, 0x0,
374             "Unreachable Destination Sequence Number", HFILL }
375         },
376     };
377
378 /* Setup protocol subtree array */
379     static gint *ett[] = {
380         &ett_aodv,
381         &ett_aodv_flags,
382         &ett_aodv_unreach_dest,
383     };
384
385 /* Register the protocol name and description */
386     proto_aodv = proto_register_protocol("Ad hoc On-demand Distance Vector Routing Protocol", "AODV", "aodv");
387
388 /* Required function calls to register the header fields and subtrees used */
389     proto_register_field_array(proto_aodv, hf, array_length(hf));
390     proto_register_subtree_array(ett, array_length(ett));
391 }
392
393
394 void
395 proto_reg_handoff_aodv(void)
396 {
397     dissector_handle_t aodv_handle;
398
399     aodv_handle = new_create_dissector_handle(dissect_aodv,
400                                               proto_aodv);
401     dissector_add("udp.port", UDP_PORT_AODV, aodv_handle);
402 }