Fix typo in name of ett for krb5 auth verifier.
[metze/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.7 2003/04/30 23:21:19 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 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #ifdef HAVE_STDDEF_H
31 #include <stddef.h>
32 #endif
33 #include <string.h>
34
35 #include <glib.h>
36
37 #include <epan/int-64bit.h>
38 #include <epan/packet.h>
39 #include <epan/ipv6-utils.h>
40
41 #ifndef offsetof
42 #define offsetof(type, member)  ((size_t)(&((type *)0)->member))
43 #endif
44
45 /*
46  * See
47  *
48  *      http://www.ietf.org/internet-drafts/draft-ietf-manet-aodv-13.txt
49  *
50  *      http://www.cs.ucsb.edu/~ebelding/txt/aodv6.txt
51  *
52  *      http://www.tcs.hut.fi/~anttit/manet/drafts/draft-perkins-aodv6-01.txt
53  */
54
55 #define INET6_ADDRLEN   16
56 #define UDP_PORT_AODV   654
57
58 /* Message Types */
59 #define RREQ            1
60 #define RREP            2
61 #define RERR            3
62 #define V6_RREQ         16
63 #define V6_RREP         17
64 #define V6_RERR         18
65 #define V6_RREP_ACK     19
66
67 /* Extension Types */
68 #define AODV6_EXT       1
69 #define AODV6_EXT_INT   2
70 #define AODV6_EXT_NTP   3
71
72 /* Flag bits: */
73 #define RREQ_GRAT    0x20
74 #define RREQ_REP     0x40
75 #define RREQ_JOIN    0x80
76
77 #define RREP_ACK     0x40
78 #define RREP_REP     0x80
79
80 #define RERR_NODEL   0x80
81
82 static const value_string type_vals[] = {
83     { RREQ,        "Route Request" },
84     { RREP,        "Route Reply" },
85     { RERR,        "Route Error" },
86     { V6_RREQ,     "IPv6 Route Request"},
87     { V6_RREP,     "IPv6 Route Reply"},
88     { V6_RERR,     "IPv6 Route Error"},
89     { V6_RREP_ACK, "IPv6 Route Reply Acknowledgment"},
90     { 0,           NULL }
91 };
92
93 static const value_string exttype_vals[] = {
94     { AODV6_EXT,     "None"},
95     { AODV6_EXT_INT, "Hello Interval"},
96     { AODV6_EXT_NTP, "Timestamp"},
97     { 0,             NULL}
98 };
99
100 struct aodv_rreq {
101     guint8 type;
102     guint8 flags;
103     guint8 res;
104     guint8 hop_count;
105     guint32 rreq_id;
106     guint32 dest_addr;
107     guint32 dest_seqno;
108     guint32 orig_addr;
109     guint32 orig_seqno;
110 };
111
112 struct aodv_rrep {
113     guint8 type;
114     guint8 flags;
115     guint8 prefix_sz;
116     guint8 hop_count;
117     guint32 dest_addr;
118     guint32 dest_seqno;
119     guint32 orig_addr;
120     guint32 lifetime;
121 };
122
123 struct aodv_rerr {
124     guint8 type;
125     guint8 flags;
126     guint8 res;
127     guint8 dest_count;
128     guint32 dest_addr;
129     guint32 dest_seqno;
130 };
131
132 typedef struct v6_rreq {
133     guint8 type;
134     guint8 flags;
135     guint8 res;
136     guint8 hop_count;
137     guint32 rreq_id;
138     guint32 dest_seqno;
139     guint32 orig_seqno;
140     struct e_in6_addr dest_addr;
141     struct e_in6_addr orig_addr;
142 } v6_rreq_t;
143
144 typedef struct v6_rrep {
145     guint8 type;
146     guint8 flags;
147     guint8 prefix_sz;
148     guint8 hop_count;
149     guint32 dest_seqno;
150     struct e_in6_addr dest_addr;
151     struct e_in6_addr orig_addr;
152     guint32 lifetime;
153 } v6_rrep_t;
154
155 typedef struct v6_rerr {
156     guint8 type;
157     guint8 flags;
158     guint8 res;
159     guint8 dest_count;
160     guint32 dest_seqno;
161     struct e_in6_addr dest_addr;
162 } v6_rerr_t;
163
164 typedef struct v6_rrep_ack {
165     guint8 type;
166     guint8 res;
167 } v6_rrep_ack_t;
168
169 typedef struct v6_ext {
170     guint8 type;
171     guint8 length;
172 } v6_ext_t;
173
174 /* Initialize the protocol and registered fields */
175 static int proto_aodv = -1;
176 static int hf_aodv_type = -1;
177 static int hf_aodv_flags = -1;
178 static int hf_aodv_prefix_sz = -1;
179 static int hf_aodv_hopcount = -1;
180 static int hf_aodv_rreq_id = -1;
181 static int hf_aodv_dest_ip = -1;
182 static int hf_aodv_dest_ipv6 = -1;
183 static int hf_aodv_dest_seqno = -1;
184 static int hf_aodv_orig_ip = -1;
185 static int hf_aodv_orig_ipv6 = -1;
186 static int hf_aodv_orig_seqno = -1;
187 static int hf_aodv_lifetime = -1;
188 static int hf_aodv_destcount = -1;
189 static int hf_aodv_unreach_dest_ip = -1;
190 static int hf_aodv_unreach_dest_ipv6 = -1;
191 static int hf_aodv_unreach_dest_seqno = -1;
192 static int hf_aodv_flags_rreq_join = -1;
193 static int hf_aodv_flags_rreq_repair = -1;
194 static int hf_aodv_flags_rreq_gratuitous = -1;
195 static int hf_aodv_flags_rrep_repair = -1;
196 static int hf_aodv_flags_rrep_ack = -1;
197 static int hf_aodv_flags_rerr_nodelete = -1;
198 static int hf_aodv_ext_type = -1;
199 static int hf_aodv_ext_length = -1;
200 static int hf_aodv_ext_interval = -1;
201 static int hf_aodv_ext_timestamp = -1;
202
203 /* Initialize the subtree pointers */
204 static gint ett_aodv = -1;
205 static gint ett_aodv_flags = -1;
206 static gint ett_aodv_unreach_dest = -1;
207 static gint ett_aodv_extensions = -1;
208
209 /* Code to actually dissect the packets */
210
211 static void
212 dissect_aodv6_ext(tvbuff_t * tvb, int offset, proto_tree * tree)
213 {
214     proto_tree *aodv6ext_tree;
215     proto_item *ti;
216     v6_ext_t aodv6ext, *ext;
217     char *typename;
218     int len;
219
220     if (!tree)
221         return;
222
223   again:
224     if ((int) tvb_reported_length(tvb) <= offset)
225         return;                 /* No more options left */
226
227     ext = &aodv6ext;
228     tvb_memcpy(tvb, (guint8 *) ext, offset, sizeof(*ext));
229     len = ext->length;
230
231     ti = proto_tree_add_text(tree, tvb, offset, sizeof(v6_ext_t) +
232                              len, "AODV6 Extensions");
233     aodv6ext_tree = proto_item_add_subtree(ti, ett_aodv_extensions);
234
235     if (len == 0) {
236         proto_tree_add_text(aodv6ext_tree, tvb,
237                             offset + offsetof(v6_ext_t, length), 1,
238                             "Invalid option length: %u", ext->length);
239         return;                 /* we must not try to decode this */
240     }
241
242     switch (ext->type) {
243     case AODV6_EXT_INT:
244         typename = "Hello Interval";
245         break;
246     case AODV6_EXT_NTP:
247         typename = "Timestamp";
248         break;
249     default:
250         typename = "Unknown";
251         break;
252     }
253     proto_tree_add_text(aodv6ext_tree, tvb,
254                         offset + offsetof(v6_ext_t, type), 1,
255                         "Type: %u (%s)", ext->type, typename);
256     proto_tree_add_text(aodv6ext_tree, tvb,
257                         offset + offsetof(v6_ext_t, length), 1,
258                         "Length: %u bytes", ext->length);
259
260     offset += sizeof(v6_ext_t);
261
262     switch (ext->type) {
263     case AODV6_EXT_INT:
264         proto_tree_add_uint(aodv6ext_tree, hf_aodv_ext_interval,
265                             tvb, offset, 4, tvb_get_ntohl(tvb, offset));
266         break;
267     case AODV6_EXT_NTP:
268         proto_tree_add_item(aodv6ext_tree, hf_aodv_ext_timestamp,
269                             tvb, offset, 8, FALSE);
270         break;
271     default:
272         break;
273     }
274     /* If multifield extensions appear, we need more
275      * sophisticated handler.  For now, this is okay. */
276
277     offset += ext->length;
278     goto again;
279 }
280
281 static int
282 dissect_aodv(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
283 {
284     proto_item *ti = NULL, *tj = NULL, *tk = NULL;
285     proto_tree *aodv_tree = NULL, *aodv_flags_tree = NULL,
286         *aodv_unreach_dest_tree = NULL;
287     guint8 type;
288     guint8 flags;
289     int i, extlen;
290     struct aodv_rreq rreq;
291     struct aodv_rrep rrep;
292     struct aodv_rerr rerr;
293     v6_rreq_t v6_rreq;
294     v6_rrep_t v6_rrep;
295     v6_rerr_t v6_rerr;
296
297 /* Make entries in Protocol column and Info column on summary display */
298     if (check_col(pinfo->cinfo, COL_PROTOCOL))
299         col_set_str(pinfo->cinfo, COL_PROTOCOL, "AODV");
300
301     if (check_col(pinfo->cinfo, COL_INFO))
302         col_clear(pinfo->cinfo, COL_INFO);
303
304     /* Check the type of AODV packet. */
305     type = tvb_get_guint8(tvb, 0);
306     if (match_strval(type, type_vals) == NULL) {
307         /*
308          * We assume this is not an AODV packet.
309          */
310         return 0;
311     }
312
313     if (tree) {
314         ti = proto_tree_add_protocol_format(tree, proto_aodv, tvb, 0, -1,
315             "Ad hoc On-demand Distance Vector Routing Protocol, %s",
316             val_to_str(type, type_vals, "Unknown AODV Packet Type (%u)"));
317         aodv_tree = proto_item_add_subtree(ti, ett_aodv);
318
319         proto_tree_add_uint(aodv_tree, hf_aodv_type, tvb, 0, 1, type);
320         tj = proto_tree_add_text(aodv_tree, tvb, 1, 1, "Flags:");
321         aodv_flags_tree = proto_item_add_subtree(tj, ett_aodv_flags);
322     }
323
324
325     switch (type) {
326     case RREQ:
327         flags = tvb_get_guint8(tvb, 1);
328         rreq.hop_count = tvb_get_guint8(tvb, 3);
329         rreq.rreq_id = tvb_get_ntohl(tvb, 4);
330         tvb_memcpy(tvb, (guint8 *)&rreq.dest_addr, 8, 4);
331         rreq.dest_seqno = tvb_get_ntohl(tvb, 12);
332         tvb_memcpy(tvb, (guint8 *)&rreq.orig_addr, 16, 4);
333         rreq.orig_seqno = tvb_get_ntohl(tvb, 20);
334
335         if (tree) {
336             proto_tree_add_boolean(aodv_flags_tree, hf_aodv_flags_rreq_join, tvb, 1, 1, flags);
337             proto_tree_add_boolean(aodv_flags_tree, hf_aodv_flags_rreq_repair, tvb, 1, 1, flags);
338             proto_tree_add_boolean(aodv_flags_tree, hf_aodv_flags_rreq_gratuitous, tvb, 1, 1, flags);
339             if (flags & RREQ_JOIN)
340                 proto_item_append_text(tj, " J");
341             if (flags & RREQ_REP)
342                 proto_item_append_text(tj, " R");
343             if (flags & RREQ_GRAT)
344                 proto_item_append_text(tj, " G");
345             proto_tree_add_uint(aodv_tree, hf_aodv_hopcount, tvb, 3, 1, rreq.hop_count);
346             proto_tree_add_uint(aodv_tree, hf_aodv_rreq_id, tvb, 4, 4, rreq.rreq_id);
347             proto_tree_add_ipv4(aodv_tree, hf_aodv_dest_ip, tvb, 8, 4, rreq.dest_addr);
348             proto_tree_add_uint(aodv_tree, hf_aodv_dest_seqno, tvb, 12, 4, rreq.dest_seqno);
349             proto_tree_add_ipv4(aodv_tree, hf_aodv_orig_ip, tvb, 16, 4, rreq.orig_addr);
350             proto_tree_add_uint(aodv_tree, hf_aodv_orig_seqno, tvb, 20, 4, rreq.orig_seqno);
351             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);
352         }
353
354         if (check_col(pinfo->cinfo, COL_INFO))
355             col_add_fstr(pinfo->cinfo, COL_INFO, "%s, D: %s O: %s Id=%u Hcnt=%u DSN=%u OSN=%u",
356                          val_to_str(type, type_vals,
357                                     "Unknown AODV Packet Type (%u)"),
358                          ip_to_str(tvb_get_ptr(tvb, 8, 4)),
359                          ip_to_str(tvb_get_ptr(tvb, 16, 4)),
360                          rreq.rreq_id,
361                          rreq.hop_count,
362                          rreq.dest_seqno,
363                          rreq.orig_seqno);
364
365         break;
366     case RREP:
367         flags = tvb_get_guint8(tvb, 1);
368         rrep.prefix_sz = tvb_get_guint8(tvb, 2) & 0x1F;
369         rrep.hop_count = tvb_get_guint8(tvb, 3);
370         tvb_memcpy(tvb, (guint8 *)&rrep.dest_addr, 4, 4);
371         rrep.dest_seqno = tvb_get_ntohl(tvb, 8);
372         tvb_memcpy(tvb, (guint8 *)&rrep.orig_addr, 12, 4);
373         rrep.lifetime = tvb_get_ntohl(tvb, 16);
374
375         if (tree) {
376             proto_tree_add_boolean(aodv_flags_tree, hf_aodv_flags_rrep_repair, tvb, 1, 1, flags);
377             proto_tree_add_boolean(aodv_flags_tree, hf_aodv_flags_rrep_ack, tvb, 1, 1, flags);
378             if (flags & RREP_REP)
379                 proto_item_append_text(tj, " R");
380             if (flags & RREP_ACK)
381                 proto_item_append_text(tj, " A");
382             proto_tree_add_uint(aodv_tree, hf_aodv_prefix_sz, tvb, 3, 1, rrep.prefix_sz);
383             proto_tree_add_uint(aodv_tree, hf_aodv_hopcount, tvb, 3, 1, rrep.hop_count);
384             proto_tree_add_ipv4(aodv_tree, hf_aodv_dest_ip, tvb, 4, 4, rrep.dest_addr);
385             proto_tree_add_uint(aodv_tree, hf_aodv_dest_seqno, tvb, 8, 4, rrep.dest_seqno);
386             proto_tree_add_ipv4(aodv_tree, hf_aodv_orig_ip, tvb, 12, 4, rrep.orig_addr);
387             proto_tree_add_uint(aodv_tree, hf_aodv_lifetime, tvb, 16, 4, rrep.lifetime);
388             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);
389         }
390
391         if (check_col(pinfo->cinfo, COL_INFO))
392             col_add_fstr(pinfo->cinfo, COL_INFO, "%s D: %s O: %s Hcnt=%u DSN=%u Lifetime=%u",
393                          val_to_str(type, type_vals,
394                                     "Unknown AODV Packet Type (%u)"),
395                          ip_to_str(tvb_get_ptr(tvb, 4, 4)),
396                          ip_to_str(tvb_get_ptr(tvb, 12, 4)),
397                          rrep.hop_count,
398                          rrep.dest_seqno,
399                          rrep.lifetime);
400         break;
401     case RERR:
402         flags = tvb_get_guint8(tvb, 1);
403         rerr.dest_count = tvb_get_guint8(tvb, 3);
404
405         if (tree) {
406             proto_tree_add_boolean(aodv_flags_tree, hf_aodv_flags_rerr_nodelete, tvb, 1, 1, flags);
407             if (flags & RERR_NODEL)
408                 proto_item_append_text(tj, " N");
409             proto_tree_add_uint(aodv_tree, hf_aodv_destcount, tvb, 3, 1, rerr.dest_count);
410             tk = proto_tree_add_text(aodv_tree, tvb, 4, 8*rerr.dest_count, "Unreachable Destinations:");
411
412             aodv_unreach_dest_tree = proto_item_add_subtree(tk, ett_aodv_unreach_dest);
413             for (i = 0; i < rerr.dest_count; i++) {
414                 tvb_memcpy(tvb, (guint8 *)&rerr.dest_addr, 4+8*i, 4);
415                 rerr.dest_seqno = tvb_get_ntohl(tvb, 8+8*i);
416                 proto_tree_add_ipv4(aodv_unreach_dest_tree, hf_aodv_dest_ip, tvb, 4+8*i, 4, rerr.dest_addr);
417                 proto_tree_add_uint(aodv_unreach_dest_tree, hf_aodv_dest_seqno, tvb, 8+8*i, 4, rerr.dest_seqno);
418             }
419         }
420
421         if (check_col(pinfo->cinfo, COL_INFO))
422             col_add_fstr(pinfo->cinfo, COL_INFO, "%s, Dest Count=%u",
423                          val_to_str(type, type_vals,
424                                     "Unknown AODV Packet Type (%u)"),
425                          rerr.dest_count);
426         break;
427     case V6_RREQ:
428         flags = tvb_get_guint8(tvb, offsetof(v6_rreq_t, flags));
429         v6_rreq.hop_count = tvb_get_guint8(tvb, offsetof(v6_rreq_t, hop_count));
430         v6_rreq.rreq_id = tvb_get_ntohl(tvb, offsetof(v6_rreq_t, rreq_id));
431         v6_rreq.dest_seqno = tvb_get_ntohl(tvb, offsetof(v6_rreq_t, dest_seqno));
432         v6_rreq.orig_seqno = tvb_get_ntohl(tvb, offsetof(v6_rreq_t, orig_seqno));
433         tvb_memcpy(tvb, (guint8 *) & v6_rreq.dest_addr,
434                    offsetof(v6_rreq_t, dest_addr), INET6_ADDRLEN);
435         tvb_memcpy(tvb, (guint8 *) & v6_rreq.orig_addr,
436                    offsetof(v6_rreq_t, orig_addr), INET6_ADDRLEN);
437
438         if (tree) {
439             proto_tree_add_boolean(aodv_flags_tree,
440                                    hf_aodv_flags_rreq_join, tvb,
441                                    offsetof(v6_rreq_t, flags), 1, flags);
442             proto_tree_add_boolean(aodv_flags_tree,
443                                    hf_aodv_flags_rreq_repair, tvb,
444                                    offsetof(v6_rreq_t, flags), 1, flags);
445             proto_tree_add_boolean(aodv_flags_tree,
446                                    hf_aodv_flags_rreq_gratuitous, tvb,
447                                    offsetof(v6_rreq_t, flags), 1, flags);
448             if (flags & RREQ_JOIN)
449                 proto_item_append_text(tj, " J");
450             if (flags & RREQ_REP)
451                 proto_item_append_text(tj, " R");
452             if (flags & RREQ_GRAT)
453                 proto_item_append_text(tj, " G");
454             proto_tree_add_uint(aodv_tree, hf_aodv_hopcount, tvb,
455                                 offsetof(v6_rreq_t, hop_count), 1,
456                                 v6_rreq.hop_count);
457             proto_tree_add_uint(aodv_tree, hf_aodv_rreq_id, tvb,
458                                 offsetof(v6_rreq_t, rreq_id), 4,
459                                 v6_rreq.rreq_id);
460             proto_tree_add_uint(aodv_tree, hf_aodv_dest_seqno, tvb,
461                                 offsetof(v6_rreq_t, dest_seqno), 4,
462                                 v6_rreq.dest_seqno);
463             proto_tree_add_uint(aodv_tree, hf_aodv_orig_seqno, tvb,
464                                 offsetof(v6_rreq_t, orig_seqno), 4,
465                                 v6_rreq.orig_seqno);
466             proto_tree_add_ipv6(aodv_tree, hf_aodv_dest_ipv6, tvb,
467                                 offsetof(v6_rreq_t, dest_addr),
468                                 INET6_ADDRLEN,
469                                 (guint8 *) & v6_rreq.dest_addr);
470             proto_tree_add_ipv6(aodv_tree, hf_aodv_orig_ipv6, tvb,
471                                 offsetof(v6_rreq_t, orig_addr),
472                                 INET6_ADDRLEN,
473                                 (guint8 *) & v6_rreq.orig_addr);
474             proto_item_append_text(ti,
475                                    ", Dest IP: %s, Orig IP: %s, Id=%u",
476                                    ip6_to_str(&v6_rreq.dest_addr),
477                                    ip6_to_str(&v6_rreq.orig_addr),
478                                    v6_rreq.rreq_id);
479             extlen = ((int) tvb_reported_length(tvb) - sizeof(v6_rreq_t));
480             if (extlen > 0) {
481                 dissect_aodv6_ext(tvb, sizeof(v6_rreq_t), aodv_tree);
482             }
483         }
484
485         if (check_col(pinfo->cinfo, COL_INFO))
486             col_add_fstr(pinfo->cinfo, COL_INFO,
487                          "%s, D: %s O: %s Id=%u Hcnt=%u DSN=%u OSN=%u",
488                          val_to_str(type, type_vals,
489                                     "Unknown AODV Packet Type (%u)"),
490                          ip6_to_str(&v6_rreq.dest_addr),
491                          ip6_to_str(&v6_rreq.orig_addr),
492                          v6_rreq.rreq_id,
493                          v6_rreq.hop_count, v6_rreq.dest_seqno, v6_rreq.orig_seqno);
494         break;
495     case V6_RREP:
496         flags = tvb_get_guint8(tvb, offsetof(v6_rrep_t, flags));
497         v6_rrep.prefix_sz = tvb_get_guint8(tvb, offsetof(v6_rrep_t, prefix_sz)) & 0x1F;
498         v6_rrep.hop_count = tvb_get_guint8(tvb, offsetof(v6_rrep_t, hop_count));
499         v6_rrep.dest_seqno = tvb_get_ntohl(tvb, offsetof(v6_rrep_t, dest_seqno));
500         tvb_memcpy(tvb, (guint8 *) & v6_rrep.dest_addr,
501                    offsetof(v6_rrep_t, dest_addr), INET6_ADDRLEN);
502         tvb_memcpy(tvb, (guint8 *) & v6_rrep.orig_addr,
503                    offsetof(v6_rrep_t, orig_addr), INET6_ADDRLEN);
504         v6_rrep.lifetime = tvb_get_ntohl(tvb, offsetof(v6_rrep_t, lifetime));
505
506         if (tree) {
507             proto_tree_add_boolean(aodv_flags_tree,
508                                    hf_aodv_flags_rrep_repair, tvb,
509                                    offsetof(v6_rrep_t, flags), 1, flags);
510             proto_tree_add_boolean(aodv_flags_tree,
511                                    hf_aodv_flags_rrep_ack, tvb,
512                                    offsetof(v6_rrep_t, flags), 1, flags);
513             if (flags & RREP_REP)
514                 proto_item_append_text(tj, " R");
515             if (flags & RREP_ACK)
516                 proto_item_append_text(tj, " A");
517             proto_tree_add_uint(aodv_tree, hf_aodv_prefix_sz,
518                                 tvb, offsetof(v6_rrep_t, prefix_sz), 1,
519                                 v6_rrep.prefix_sz);
520             proto_tree_add_uint(aodv_tree, hf_aodv_hopcount,
521                                 tvb, offsetof(v6_rrep_t, hop_count), 1,
522                                 v6_rrep.hop_count);
523             proto_tree_add_uint(aodv_tree, hf_aodv_dest_seqno,
524                                 tvb, offsetof(v6_rrep_t, dest_seqno), 4,
525                                 v6_rrep.dest_seqno);
526             proto_tree_add_ipv6(aodv_tree, hf_aodv_dest_ipv6,
527                                 tvb, offsetof(v6_rrep_t, dest_addr),
528                                 INET6_ADDRLEN,
529                                 (guint8 *) & v6_rrep.dest_addr);
530             proto_tree_add_ipv6(aodv_tree, hf_aodv_orig_ipv6, tvb,
531                                 offsetof(v6_rrep_t, orig_addr),
532                                 INET6_ADDRLEN,
533                                 (guint8 *) & v6_rrep.orig_addr);
534             proto_tree_add_uint(aodv_tree, hf_aodv_lifetime, tvb,
535                                 offsetof(v6_rrep_t, lifetime), 4,
536                                 v6_rrep.lifetime);
537             proto_item_append_text(ti,
538                                    ", Dest IP: %s, Orig IP: %s, Lifetime=%u",
539                                    ip6_to_str(&v6_rrep.dest_addr),
540                                    ip6_to_str(&v6_rrep.orig_addr),
541                                    v6_rrep.lifetime);
542             extlen = ((int) tvb_reported_length(tvb) - sizeof(v6_rrep_t));
543             if (extlen > 0) {
544                 dissect_aodv6_ext(tvb, sizeof(v6_rrep_t), aodv_tree);
545             }
546         }
547
548         if (check_col(pinfo->cinfo, COL_INFO))
549             col_add_fstr(pinfo->cinfo, COL_INFO,
550                          "%s D: %s O: %s Hcnt=%u DSN=%u Lifetime=%u",
551                          val_to_str(type, type_vals,
552                                     "Unknown AODV Packet Type (%u)"),
553                          ip6_to_str(&v6_rrep.dest_addr),
554                          ip6_to_str(&v6_rrep.orig_addr),
555                          v6_rrep.hop_count, v6_rrep.dest_seqno, v6_rrep.lifetime);
556         break;
557     case V6_RERR:
558         flags = tvb_get_guint8(tvb, offsetof(v6_rerr_t, flags));
559         v6_rerr.dest_count =
560             tvb_get_guint8(tvb, offsetof(v6_rerr_t, dest_count));
561
562         if (tree) {
563             proto_tree_add_boolean(aodv_flags_tree,
564                                    hf_aodv_flags_rerr_nodelete, tvb,
565                                    offsetof(v6_rerr_t, flags), 1, flags);
566             if (flags & RERR_NODEL)
567                 proto_item_append_text(tj, " N");
568             proto_tree_add_uint(aodv_tree, hf_aodv_destcount, tvb,
569                                 offsetof(v6_rerr_t, dest_count), 1,
570                                 v6_rerr.dest_count);
571             tk = proto_tree_add_text(aodv_tree, tvb,
572                                      offsetof(v6_rerr_t, dest_addr),
573                                      (4 +
574                                       INET6_ADDRLEN) * v6_rerr.dest_count,
575                                      "Unreachable Destinations");
576
577             aodv_unreach_dest_tree =
578                 proto_item_add_subtree(tk, ett_aodv_unreach_dest);
579             for (i = 0; i < v6_rerr.dest_count; i++) {
580                 v6_rerr.dest_seqno =
581                     tvb_get_ntohl(tvb, offsetof(v6_rerr_t, dest_seqno)
582                                   + (4 + INET6_ADDRLEN) * i);
583                 tvb_memcpy(tvb, (guint8 *) & v6_rerr.dest_addr,
584                            offsetof(v6_rerr_t, dest_addr)
585                            + (4 + INET6_ADDRLEN) * i, INET6_ADDRLEN);
586                 proto_tree_add_uint(aodv_unreach_dest_tree,
587                                     hf_aodv_dest_seqno, tvb,
588                                     offsetof(v6_rerr_t, dest_seqno)
589                                     + (4 + INET6_ADDRLEN) * i, 4,
590                                     v6_rerr.dest_seqno);
591                 proto_tree_add_ipv6(aodv_unreach_dest_tree,
592                                     hf_aodv_unreach_dest_ipv6, tvb,
593                                     offsetof(v6_rerr_t, dest_addr)
594                                     + (4 + INET6_ADDRLEN) * i,
595                                     INET6_ADDRLEN,
596                                     (guint8 *) & v6_rerr.dest_addr);
597             }
598         }
599
600         if (check_col(pinfo->cinfo, COL_INFO))
601             col_add_fstr(pinfo->cinfo, COL_INFO,
602                          "%s, Dest Count=%u",
603                          val_to_str(type, type_vals,
604                                     "Unknown AODV Packet Type (%u)"),
605                          v6_rerr.dest_count);
606         break;
607     case V6_RREP_ACK:
608         if (check_col(pinfo->cinfo, COL_INFO))
609             col_add_fstr(pinfo->cinfo, COL_INFO, "%s",
610                          val_to_str(type, type_vals,
611                                     "Unknown AODV Packet Type (%u)"));
612         break;
613     default:
614         proto_tree_add_text(aodv_tree, tvb, 0,
615                             1, "Unknown AODV Packet Type (%u)",
616                             type);
617     }
618
619     return tvb_length(tvb);
620 }
621
622
623 /* Register the protocol with Ethereal */
624 void
625 proto_register_aodv(void)
626 {
627     static hf_register_info hf[] = {
628         { &hf_aodv_type,
629           { "Type", "aodv.type",
630             FT_UINT8, BASE_DEC, VALS(type_vals), 0x0,
631             "AODV packet type", HFILL }
632         },
633         { &hf_aodv_flags,
634           { "Flags", "aodv.flags",
635             FT_UINT16, BASE_DEC, NULL, 0x0,
636             "Flags", HFILL }
637         },
638         { &hf_aodv_flags_rreq_join,
639           { "RREQ Join", "aodv.flags.rreq_join",
640             FT_BOOLEAN, 8, TFS(&flags_set_truth), RREQ_JOIN,
641             "", HFILL }
642         },
643         { &hf_aodv_flags_rreq_repair,
644           { "RREQ Repair", "aodv.flags.rreq_repair",
645             FT_BOOLEAN, 8, TFS(&flags_set_truth), RREQ_REP,
646             "", HFILL }
647         },
648         { &hf_aodv_flags_rreq_gratuitous,
649           { "RREQ Gratuitous", "aodv.flags.rreq_gratuitous",
650             FT_BOOLEAN, 8, TFS(&flags_set_truth), RREQ_GRAT,
651             "", HFILL }
652         },
653         { &hf_aodv_flags_rrep_repair,
654           { "RREP Repair", "aodv.flags.rrep_repair",
655             FT_BOOLEAN, 8, TFS(&flags_set_truth), RREP_REP,
656             "", HFILL }
657         },
658         { &hf_aodv_flags_rrep_ack,
659           { "RREP Acknowledgement", "aodv.flags.rrep_ack",
660             FT_BOOLEAN, 8, TFS(&flags_set_truth), RREP_ACK,
661             "", HFILL }
662         },
663         { &hf_aodv_flags_rerr_nodelete,
664           { "RERR No Delete", "aodv.flags.rerr_nodelete",
665             FT_BOOLEAN, 8, TFS(&flags_set_truth), RERR_NODEL,
666             "", HFILL }
667         },
668         { &hf_aodv_prefix_sz,
669           { "Prefix Size", "aodv.prefix_sz",
670             FT_UINT8, BASE_DEC, NULL, 0x0,
671             "Prefix Size", HFILL }
672         },
673         { &hf_aodv_hopcount,
674           { "Hop Count", "aodv.hopcount",
675             FT_UINT8, BASE_DEC, NULL, 0x0,
676             "Hop Count", HFILL }
677         },
678         { &hf_aodv_rreq_id,
679           { "RREQ Id", "aodv.rreq_id",
680             FT_UINT32, BASE_DEC, NULL, 0x0,
681             "RREQ Id", HFILL }
682         },
683         { &hf_aodv_dest_ip,
684           { "Destination IP", "aodv.dest_ip",
685             FT_IPv4, BASE_NONE, NULL, 0x0,
686             "Destination IP Address", HFILL }
687         },
688         { &hf_aodv_dest_ipv6,
689           { "Destination IPv6", "aodv.dest_ipv6",
690             FT_IPv6, BASE_NONE, NULL, 0x0,
691             "Destination IPv6 Address", HFILL}
692         },
693         { &hf_aodv_dest_seqno,
694           { "Destination Sequence Number", "aodv.dest_seqno",
695             FT_UINT32, BASE_DEC, NULL, 0x0,
696             "Destination Sequence Number", HFILL }
697         },
698         { &hf_aodv_orig_ip,
699           { "Originator IP", "aodv.orig_ip",
700             FT_IPv4, BASE_NONE, NULL, 0x0,
701             "Originator IP Address", HFILL }
702         },
703         { &hf_aodv_orig_ipv6,
704           { "Originator IPv6", "aodv.orig_ipv6",
705             FT_IPv6, BASE_NONE, NULL, 0x0,
706             "Originator IPv6 Address", HFILL}
707         },
708         { &hf_aodv_orig_seqno,
709           { "Originator Sequence Number", "aodv.orig_seqno",
710             FT_UINT32, BASE_DEC, NULL, 0x0,
711             "Originator Sequence Number", HFILL }
712         },
713         { &hf_aodv_lifetime,
714           { "Lifetime", "aodv.lifetime",
715             FT_UINT32, BASE_DEC, NULL, 0x0,
716             "Lifetime", HFILL }
717         },
718         { &hf_aodv_destcount,
719           { "Destination Count", "aodv.destcount",
720             FT_UINT8, BASE_DEC, NULL, 0x0,
721             "Unreachable Destinations Count", HFILL }
722         },
723         { &hf_aodv_unreach_dest_ip,
724           { "Unreachable Destination IP", "aodv.unreach_dest_ip",
725             FT_IPv4, BASE_NONE, NULL, 0x0,
726             "Unreachable Destination IP Address", HFILL }
727         },
728         { &hf_aodv_unreach_dest_ipv6,
729           { "Unreachable Destination IPv6", "aodv.unreach_dest_ipv6",
730             FT_IPv6, BASE_NONE, NULL, 0x0,
731             "Unreachable Destination IPv6 Address", HFILL}
732         },
733         { &hf_aodv_unreach_dest_seqno,
734           { "Unreachable Destination Sequence Number", "aodv.unreach_dest_seqno",
735             FT_UINT32, BASE_DEC, NULL, 0x0,
736             "Unreachable Destination Sequence Number", HFILL }
737         },
738         { &hf_aodv_ext_type,
739           { "Extension Type", "aodv.ext_type",
740             FT_UINT8, BASE_DEC, NULL, 0x0,
741             "Extension Format Type", HFILL}
742         },
743         { &hf_aodv_ext_length,
744           { "Extension Length", "aodv.ext_length",
745             FT_UINT8, BASE_DEC, NULL, 0x0,
746             "Extension Data Length", HFILL}
747         },
748         { &hf_aodv_ext_interval,
749           { "Hello Interval", "aodv.hello_interval",
750             FT_UINT32, BASE_DEC, NULL, 0x0,
751             "Hello Interval Extension", HFILL}
752          },
753         { &hf_aodv_ext_timestamp,
754           { "Timestamp", "aodv.timestamp",
755             FT_UINT64, BASE_DEC, NULL, 0x0,
756             "Timestamp Extension", HFILL}
757          },
758     };
759
760 /* Setup protocol subtree array */
761     static gint *ett[] = {
762         &ett_aodv,
763         &ett_aodv_flags,
764         &ett_aodv_unreach_dest,
765         &ett_aodv_extensions,
766     };
767
768 /* Register the protocol name and description */
769     proto_aodv = proto_register_protocol("Ad hoc On-demand Distance Vector Routing Protocol", "AODV", "aodv");
770
771 /* Required function calls to register the header fields and subtrees used */
772     proto_register_field_array(proto_aodv, hf, array_length(hf));
773     proto_register_subtree_array(ett, array_length(ett));
774 }
775
776
777 void
778 proto_reg_handoff_aodv(void)
779 {
780     dissector_handle_t aodv_handle;
781
782     aodv_handle = new_create_dissector_handle(dissect_aodv,
783                                               proto_aodv);
784     dissector_add("udp.port", UDP_PORT_AODV, aodv_handle);
785 }