aboutsummaryrefslogtreecommitdiffstats
path: root/packet-aodv.c
blob: 0e3c8ec7affef190162965bdbf7a0c9d51ba8b65 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
/* packet-aodv.c
 * Routines for AODV dissection
 * Copyright 2000, Erik Nordström <erik.nordstrom@it.uu.se>
 *
 * $Id: packet-aodv.c,v 1.7 2003/04/30 23:21:19 guy Exp $
 *
 * Ethereal - Network traffic analyzer
 * By Gerald Combs <gerald@ethereal.com>
 * Copyright 1998 Gerald Combs
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#ifdef HAVE_STDDEF_H
#include <stddef.h>
#endif
#include <string.h>

#include <glib.h>

#include <epan/int-64bit.h>
#include <epan/packet.h>
#include <epan/ipv6-utils.h>

#ifndef offsetof
#define	offsetof(type, member)	((size_t)(&((type *)0)->member))
#endif

/*
 * See
 *
 *	http://www.ietf.org/internet-drafts/draft-ietf-manet-aodv-13.txt
 *
 *	http://www.cs.ucsb.edu/~ebelding/txt/aodv6.txt
 *
 *	http://www.tcs.hut.fi/~anttit/manet/drafts/draft-perkins-aodv6-01.txt
 */

#define INET6_ADDRLEN	16
#define UDP_PORT_AODV	654

/* Message Types */
#define RREQ		1
#define RREP		2
#define RERR		3
#define V6_RREQ		16
#define V6_RREP		17
#define V6_RERR		18
#define V6_RREP_ACK	19

/* Extension Types */
#define AODV6_EXT	1
#define AODV6_EXT_INT	2
#define AODV6_EXT_NTP	3

/* Flag bits: */
#define RREQ_GRAT    0x20
#define RREQ_REP     0x40
#define RREQ_JOIN    0x80

#define RREP_ACK     0x40
#define RREP_REP     0x80

#define RERR_NODEL   0x80

static const value_string type_vals[] = {
    { RREQ,        "Route Request" },
    { RREP,        "Route Reply" },
    { RERR,        "Route Error" },
    { V6_RREQ,     "IPv6 Route Request"},
    { V6_RREP,     "IPv6 Route Reply"},
    { V6_RERR,     "IPv6 Route Error"},
    { V6_RREP_ACK, "IPv6 Route Reply Acknowledgment"},
    { 0,           NULL }
};

static const value_string exttype_vals[] = {
    { AODV6_EXT,     "None"},
    { AODV6_EXT_INT, "Hello Interval"},
    { AODV6_EXT_NTP, "Timestamp"},
    { 0,             NULL}
};

struct aodv_rreq {
    guint8 type;
    guint8 flags;
    guint8 res;
    guint8 hop_count;
    guint32 rreq_id;
    guint32 dest_addr;
    guint32 dest_seqno;
    guint32 orig_addr;
    guint32 orig_seqno;
};

struct aodv_rrep {
    guint8 type;
    guint8 flags;
    guint8 prefix_sz;
    guint8 hop_count;
    guint32 dest_addr;
    guint32 dest_seqno;
    guint32 orig_addr;
    guint32 lifetime;
};

struct aodv_rerr {
    guint8 type;
    guint8 flags;
    guint8 res;
    guint8 dest_count;
    guint32 dest_addr;
    guint32 dest_seqno;
};

typedef struct v6_rreq {
    guint8 type;
    guint8 flags;
    guint8 res;
    guint8 hop_count;
    guint32 rreq_id;
    guint32 dest_seqno;
    guint32 orig_seqno;
    struct e_in6_addr dest_addr;
    struct e_in6_addr orig_addr;
} v6_rreq_t;

typedef struct v6_rrep {
    guint8 type;
    guint8 flags;
    guint8 prefix_sz;
    guint8 hop_count;
    guint32 dest_seqno;
    struct e_in6_addr dest_addr;
    struct e_in6_addr orig_addr;
    guint32 lifetime;
} v6_rrep_t;

typedef struct v6_rerr {
    guint8 type;
    guint8 flags;
    guint8 res;
    guint8 dest_count;
    guint32 dest_seqno;
    struct e_in6_addr dest_addr;
} v6_rerr_t;

typedef struct v6_rrep_ack {
    guint8 type;
    guint8 res;
} v6_rrep_ack_t;

typedef struct v6_ext {
    guint8 type;
    guint8 length;
} v6_ext_t;

/* Initialize the protocol and registered fields */
static int proto_aodv = -1;
static int hf_aodv_type = -1;
static int hf_aodv_flags = -1;
static int hf_aodv_prefix_sz = -1;
static int hf_aodv_hopcount = -1;
static int hf_aodv_rreq_id = -1;
static int hf_aodv_dest_ip = -1;
static int hf_aodv_dest_ipv6 = -1;
static int hf_aodv_dest_seqno = -1;
static int hf_aodv_orig_ip = -1;
static int hf_aodv_orig_ipv6 = -1;
static int hf_aodv_orig_seqno = -1;
static int hf_aodv_lifetime = -1;
static int hf_aodv_destcount = -1;
static int hf_aodv_unreach_dest_ip = -1;
static int hf_aodv_unreach_dest_ipv6 = -1;
static int hf_aodv_unreach_dest_seqno = -1;
static int hf_aodv_flags_rreq_join = -1;
static int hf_aodv_flags_rreq_repair = -1;
static int hf_aodv_flags_rreq_gratuitous = -1;
static int hf_aodv_flags_rrep_repair = -1;
static int hf_aodv_flags_rrep_ack = -1;
static int hf_aodv_flags_rerr_nodelete = -1;
static int hf_aodv_ext_type = -1;
static int hf_aodv_ext_length = -1;
static int hf_aodv_ext_interval = -1;
static int hf_aodv_ext_timestamp = -1;

/* Initialize the subtree pointers */
static gint ett_aodv = -1;
static gint ett_aodv_flags = -1;
static gint ett_aodv_unreach_dest = -1;
static gint ett_aodv_extensions = -1;

/* Code to actually dissect the packets */

static void
dissect_aodv6_ext(tvbuff_t * tvb, int offset, proto_tree * tree)
{
    proto_tree *aodv6ext_tree;
    proto_item *ti;
    v6_ext_t aodv6ext, *ext;
    char *typename;
    int len;

    if (!tree)
	return;

  again:
    if ((int) tvb_reported_length(tvb) <= offset)
	return;			/* No more options left */

    ext = &aodv6ext;
    tvb_memcpy(tvb, (guint8 *) ext, offset, sizeof(*ext));
    len = ext->length;

    ti = proto_tree_add_text(tree, tvb, offset, sizeof(v6_ext_t) +
			     len, "AODV6 Extensions");
    aodv6ext_tree = proto_item_add_subtree(ti, ett_aodv_extensions);

    if (len == 0) {
	proto_tree_add_text(aodv6ext_tree, tvb,
			    offset + offsetof(v6_ext_t, length), 1,
			    "Invalid option length: %u", ext->length);
	return;			/* we must not try to decode this */
    }

    switch (ext->type) {
    case AODV6_EXT_INT:
	typename = "Hello Interval";
	break;
    case AODV6_EXT_NTP:
	typename = "Timestamp";
	break;
    default:
	typename = "Unknown";
	break;
    }
    proto_tree_add_text(aodv6ext_tree, tvb,
			offset + offsetof(v6_ext_t, type), 1,
			"Type: %u (%s)", ext->type, typename);
    proto_tree_add_text(aodv6ext_tree, tvb,
			offset + offsetof(v6_ext_t, length), 1,
			"Length: %u bytes", ext->length);

    offset += sizeof(v6_ext_t);

    switch (ext->type) {
    case AODV6_EXT_INT:
	proto_tree_add_uint(aodv6ext_tree, hf_aodv_ext_interval,
			    tvb, offset, 4, tvb_get_ntohl(tvb, offset));
	break;
    case AODV6_EXT_NTP:
	proto_tree_add_item(aodv6ext_tree, hf_aodv_ext_timestamp,
			    tvb, offset, 8, FALSE);
	break;
    default:
	break;
    }
    /* If multifield extensions appear, we need more
     * sophisticated handler.  For now, this is okay. */

    offset += ext->length;
    goto again;
}

static int
dissect_aodv(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
{
    proto_item *ti = NULL, *tj = NULL, *tk = NULL;
    proto_tree *aodv_tree = NULL, *aodv_flags_tree = NULL,
	*aodv_unreach_dest_tree = NULL;
    guint8 type;
    guint8 flags;
    int i, extlen;
    struct aodv_rreq rreq;
    struct aodv_rrep rrep;
    struct aodv_rerr rerr;
    v6_rreq_t v6_rreq;
    v6_rrep_t v6_rrep;
    v6_rerr_t v6_rerr;

/* Make entries in Protocol column and Info column on summary display */
    if (check_col(pinfo->cinfo, COL_PROTOCOL))
	col_set_str(pinfo->cinfo, COL_PROTOCOL, "AODV");

    if (check_col(pinfo->cinfo, COL_INFO))
	col_clear(pinfo->cinfo, COL_INFO);

    /* Check the type of AODV packet. */
    type = tvb_get_guint8(tvb, 0);
    if (match_strval(type, type_vals) == NULL) {
	/*
	 * We assume this is not an AODV packet.
	 */
	return 0;
    }

    if (tree) {
	ti = proto_tree_add_protocol_format(tree, proto_aodv, tvb, 0, -1,
	    "Ad hoc On-demand Distance Vector Routing Protocol, %s",
	    val_to_str(type, type_vals, "Unknown AODV Packet Type (%u)"));
	aodv_tree = proto_item_add_subtree(ti, ett_aodv);

	proto_tree_add_uint(aodv_tree, hf_aodv_type, tvb, 0, 1, type);
	tj = proto_tree_add_text(aodv_tree, tvb, 1, 1, "Flags:");
	aodv_flags_tree = proto_item_add_subtree(tj, ett_aodv_flags);
    }


    switch (type) {
    case RREQ:
	flags = tvb_get_guint8(tvb, 1);
	rreq.hop_count = tvb_get_guint8(tvb, 3);
	rreq.rreq_id = tvb_get_ntohl(tvb, 4);
	tvb_memcpy(tvb, (guint8 *)&rreq.dest_addr, 8, 4);
	rreq.dest_seqno = tvb_get_ntohl(tvb, 12);
	tvb_memcpy(tvb, (guint8 *)&rreq.orig_addr, 16, 4);
	rreq.orig_seqno = tvb_get_ntohl(tvb, 20);

	if (tree) {
	    proto_tree_add_boolean(aodv_flags_tree, hf_aodv_flags_rreq_join, tvb, 1, 1, flags);
	    proto_tree_add_boolean(aodv_flags_tree, hf_aodv_flags_rreq_repair, tvb, 1, 1, flags);
	    proto_tree_add_boolean(aodv_flags_tree, hf_aodv_flags_rreq_gratuitous, tvb, 1, 1, flags);
	    if (flags & RREQ_JOIN)
		proto_item_append_text(tj, " J");
	    if (flags & RREQ_REP)
		proto_item_append_text(tj, " R");
	    if (flags & RREQ_GRAT)
		proto_item_append_text(tj, " G");
	    proto_tree_add_uint(aodv_tree, hf_aodv_hopcount, tvb, 3, 1, rreq.hop_count);
	    proto_tree_add_uint(aodv_tree, hf_aodv_rreq_id, tvb, 4, 4, rreq.rreq_id);
	    proto_tree_add_ipv4(aodv_tree, hf_aodv_dest_ip, tvb, 8, 4, rreq.dest_addr);
	    proto_tree_add_uint(aodv_tree, hf_aodv_dest_seqno, tvb, 12, 4, rreq.dest_seqno);
	    proto_tree_add_ipv4(aodv_tree, hf_aodv_orig_ip, tvb, 16, 4, rreq.orig_addr);
	    proto_tree_add_uint(aodv_tree, hf_aodv_orig_seqno, tvb, 20, 4, rreq.orig_seqno);
	    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);
	}

	if (check_col(pinfo->cinfo, COL_INFO))
	    col_add_fstr(pinfo->cinfo, COL_INFO, "%s, D: %s O: %s Id=%u Hcnt=%u DSN=%u OSN=%u",
			 val_to_str(type, type_vals,
				    "Unknown AODV Packet Type (%u)"),
			 ip_to_str(tvb_get_ptr(tvb, 8, 4)),
			 ip_to_str(tvb_get_ptr(tvb, 16, 4)),
			 rreq.rreq_id,
			 rreq.hop_count,
			 rreq.dest_seqno,
			 rreq.orig_seqno);

	break;
    case RREP:
	flags = tvb_get_guint8(tvb, 1);
	rrep.prefix_sz = tvb_get_guint8(tvb, 2) & 0x1F;
	rrep.hop_count = tvb_get_guint8(tvb, 3);
	tvb_memcpy(tvb, (guint8 *)&rrep.dest_addr, 4, 4);
	rrep.dest_seqno = tvb_get_ntohl(tvb, 8);
	tvb_memcpy(tvb, (guint8 *)&rrep.orig_addr, 12, 4);
	rrep.lifetime = tvb_get_ntohl(tvb, 16);

	if (tree) {
	    proto_tree_add_boolean(aodv_flags_tree, hf_aodv_flags_rrep_repair, tvb, 1, 1, flags);
	    proto_tree_add_boolean(aodv_flags_tree, hf_aodv_flags_rrep_ack, tvb, 1, 1, flags);
	    if (flags & RREP_REP)
		proto_item_append_text(tj, " R");
	    if (flags & RREP_ACK)
		proto_item_append_text(tj, " A");
	    proto_tree_add_uint(aodv_tree, hf_aodv_prefix_sz, tvb, 3, 1, rrep.prefix_sz);
	    proto_tree_add_uint(aodv_tree, hf_aodv_hopcount, tvb, 3, 1, rrep.hop_count);
	    proto_tree_add_ipv4(aodv_tree, hf_aodv_dest_ip, tvb, 4, 4, rrep.dest_addr);
	    proto_tree_add_uint(aodv_tree, hf_aodv_dest_seqno, tvb, 8, 4, rrep.dest_seqno);
	    proto_tree_add_ipv4(aodv_tree, hf_aodv_orig_ip, tvb, 12, 4, rrep.orig_addr);
	    proto_tree_add_uint(aodv_tree, hf_aodv_lifetime, tvb, 16, 4, rrep.lifetime);
	    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);
	}

	if (check_col(pinfo->cinfo, COL_INFO))
	    col_add_fstr(pinfo->cinfo, COL_INFO, "%s D: %s O: %s Hcnt=%u DSN=%u Lifetime=%u",
			 val_to_str(type, type_vals,
				    "Unknown AODV Packet Type (%u)"),
			 ip_to_str(tvb_get_ptr(tvb, 4, 4)),
			 ip_to_str(tvb_get_ptr(tvb, 12, 4)),
			 rrep.hop_count,
			 rrep.dest_seqno,
			 rrep.lifetime);
	break;
    case RERR:
	flags = tvb_get_guint8(tvb, 1);
	rerr.dest_count = tvb_get_guint8(tvb, 3);

	if (tree) {
	    proto_tree_add_boolean(aodv_flags_tree, hf_aodv_flags_rerr_nodelete, tvb, 1, 1, flags);
	    if (flags & RERR_NODEL)
		proto_item_append_text(tj, " N");
	    proto_tree_add_uint(aodv_tree, hf_aodv_destcount, tvb, 3, 1, rerr.dest_count);
	    tk = proto_tree_add_text(aodv_tree, tvb, 4, 8*rerr.dest_count, "Unreachable Destinations:");

	    aodv_unreach_dest_tree = proto_item_add_subtree(tk, ett_aodv_unreach_dest);
	    for (i = 0; i < rerr.dest_count; i++) {
		tvb_memcpy(tvb, (guint8 *)&rerr.dest_addr, 4+8*i, 4);
		rerr.dest_seqno = tvb_get_ntohl(tvb, 8+8*i);
		proto_tree_add_ipv4(aodv_unreach_dest_tree, hf_aodv_dest_ip, tvb, 4+8*i, 4, rerr.dest_addr);
		proto_tree_add_uint(aodv_unreach_dest_tree, hf_aodv_dest_seqno, tvb, 8+8*i, 4, rerr.dest_seqno);
	    }
	}

	if (check_col(pinfo->cinfo, COL_INFO))
	    col_add_fstr(pinfo->cinfo, COL_INFO, "%s, Dest Count=%u",
			 val_to_str(type, type_vals,
				    "Unknown AODV Packet Type (%u)"),
			 rerr.dest_count);
	break;
    case V6_RREQ:
	flags = tvb_get_guint8(tvb, offsetof(v6_rreq_t, flags));
	v6_rreq.hop_count = tvb_get_guint8(tvb, offsetof(v6_rreq_t, hop_count));
	v6_rreq.rreq_id = tvb_get_ntohl(tvb, offsetof(v6_rreq_t, rreq_id));
	v6_rreq.dest_seqno = tvb_get_ntohl(tvb, offsetof(v6_rreq_t, dest_seqno));
	v6_rreq.orig_seqno = tvb_get_ntohl(tvb, offsetof(v6_rreq_t, orig_seqno));
	tvb_memcpy(tvb, (guint8 *) & v6_rreq.dest_addr,
		   offsetof(v6_rreq_t, dest_addr), INET6_ADDRLEN);
	tvb_memcpy(tvb, (guint8 *) & v6_rreq.orig_addr,
		   offsetof(v6_rreq_t, orig_addr), INET6_ADDRLEN);

	if (tree) {
	    proto_tree_add_boolean(aodv_flags_tree,
				   hf_aodv_flags_rreq_join, tvb,
				   offsetof(v6_rreq_t, flags), 1, flags);
	    proto_tree_add_boolean(aodv_flags_tree,
				   hf_aodv_flags_rreq_repair, tvb,
				   offsetof(v6_rreq_t, flags), 1, flags);
	    proto_tree_add_boolean(aodv_flags_tree,
				   hf_aodv_flags_rreq_gratuitous, tvb,
				   offsetof(v6_rreq_t, flags), 1, flags);
	    if (flags & RREQ_JOIN)
		proto_item_append_text(tj, " J");
	    if (flags & RREQ_REP)
		proto_item_append_text(tj, " R");
	    if (flags & RREQ_GRAT)
		proto_item_append_text(tj, " G");
	    proto_tree_add_uint(aodv_tree, hf_aodv_hopcount, tvb,
				offsetof(v6_rreq_t, hop_count), 1,
				v6_rreq.hop_count);
	    proto_tree_add_uint(aodv_tree, hf_aodv_rreq_id, tvb,
				offsetof(v6_rreq_t, rreq_id), 4,
				v6_rreq.rreq_id);
	    proto_tree_add_uint(aodv_tree, hf_aodv_dest_seqno, tvb,
				offsetof(v6_rreq_t, dest_seqno), 4,
				v6_rreq.dest_seqno);
	    proto_tree_add_uint(aodv_tree, hf_aodv_orig_seqno, tvb,
				offsetof(v6_rreq_t, orig_seqno), 4,
				v6_rreq.orig_seqno);
	    proto_tree_add_ipv6(aodv_tree, hf_aodv_dest_ipv6, tvb,
				offsetof(v6_rreq_t, dest_addr),
				INET6_ADDRLEN,
				(guint8 *) & v6_rreq.dest_addr);
	    proto_tree_add_ipv6(aodv_tree, hf_aodv_orig_ipv6, tvb,
				offsetof(v6_rreq_t, orig_addr),
				INET6_ADDRLEN,
				(guint8 *) & v6_rreq.orig_addr);
	    proto_item_append_text(ti,
				   ", Dest IP: %s, Orig IP: %s, Id=%u",
				   ip6_to_str(&v6_rreq.dest_addr),
				   ip6_to_str(&v6_rreq.orig_addr),
				   v6_rreq.rreq_id);
	    extlen = ((int) tvb_reported_length(tvb) - sizeof(v6_rreq_t));
	    if (extlen > 0) {
		dissect_aodv6_ext(tvb, sizeof(v6_rreq_t), aodv_tree);
	    }
	}

	if (check_col(pinfo->cinfo, COL_INFO))
	    col_add_fstr(pinfo->cinfo, COL_INFO,
			 "%s, D: %s O: %s Id=%u Hcnt=%u DSN=%u OSN=%u",
			 val_to_str(type, type_vals,
				    "Unknown AODV Packet Type (%u)"),
			 ip6_to_str(&v6_rreq.dest_addr),
			 ip6_to_str(&v6_rreq.orig_addr),
			 v6_rreq.rreq_id,
			 v6_rreq.hop_count, v6_rreq.dest_seqno, v6_rreq.orig_seqno);
	break;
    case V6_RREP:
	flags = tvb_get_guint8(tvb, offsetof(v6_rrep_t, flags));
	v6_rrep.prefix_sz = tvb_get_guint8(tvb, offsetof(v6_rrep_t, prefix_sz)) & 0x1F;
	v6_rrep.hop_count = tvb_get_guint8(tvb, offsetof(v6_rrep_t, hop_count));
	v6_rrep.dest_seqno = tvb_get_ntohl(tvb, offsetof(v6_rrep_t, dest_seqno));
	tvb_memcpy(tvb, (guint8 *) & v6_rrep.dest_addr,
		   offsetof(v6_rrep_t, dest_addr), INET6_ADDRLEN);
	tvb_memcpy(tvb, (guint8 *) & v6_rrep.orig_addr,
		   offsetof(v6_rrep_t, orig_addr), INET6_ADDRLEN);
	v6_rrep.lifetime = tvb_get_ntohl(tvb, offsetof(v6_rrep_t, lifetime));

	if (tree) {
	    proto_tree_add_boolean(aodv_flags_tree,
				   hf_aodv_flags_rrep_repair, tvb,
				   offsetof(v6_rrep_t, flags), 1, flags);
	    proto_tree_add_boolean(aodv_flags_tree,
				   hf_aodv_flags_rrep_ack, tvb,
				   offsetof(v6_rrep_t, flags), 1, flags);
	    if (flags & RREP_REP)
		proto_item_append_text(tj, " R");
	    if (flags & RREP_ACK)
		proto_item_append_text(tj, " A");
	    proto_tree_add_uint(aodv_tree, hf_aodv_prefix_sz,
				tvb, offsetof(v6_rrep_t, prefix_sz), 1,
				v6_rrep.prefix_sz);
	    proto_tree_add_uint(aodv_tree, hf_aodv_hopcount,
				tvb, offsetof(v6_rrep_t, hop_count), 1,
				v6_rrep.hop_count);
	    proto_tree_add_uint(aodv_tree, hf_aodv_dest_seqno,
				tvb, offsetof(v6_rrep_t, dest_seqno), 4,
				v6_rrep.dest_seqno);
	    proto_tree_add_ipv6(aodv_tree, hf_aodv_dest_ipv6,
				tvb, offsetof(v6_rrep_t, dest_addr),
				INET6_ADDRLEN,
				(guint8 *) & v6_rrep.dest_addr);
	    proto_tree_add_ipv6(aodv_tree, hf_aodv_orig_ipv6, tvb,
				offsetof(v6_rrep_t, orig_addr),
				INET6_ADDRLEN,
				(guint8 *) & v6_rrep.orig_addr);
	    proto_tree_add_uint(aodv_tree, hf_aodv_lifetime, tvb,
				offsetof(v6_rrep_t, lifetime), 4,
				v6_rrep.lifetime);
	    proto_item_append_text(ti,
				   ", Dest IP: %s, Orig IP: %s, Lifetime=%u",
				   ip6_to_str(&v6_rrep.dest_addr),
				   ip6_to_str(&v6_rrep.orig_addr),
				   v6_rrep.lifetime);
	    extlen = ((int) tvb_reported_length(tvb) - sizeof(v6_rrep_t));
	    if (extlen > 0) {
		dissect_aodv6_ext(tvb, sizeof(v6_rrep_t), aodv_tree);
	    }
	}

	if (check_col(pinfo->cinfo, COL_INFO))
	    col_add_fstr(pinfo->cinfo, COL_INFO,
			 "%s D: %s O: %s Hcnt=%u DSN=%u Lifetime=%u",
			 val_to_str(type, type_vals,
				    "Unknown AODV Packet Type (%u)"),
			 ip6_to_str(&v6_rrep.dest_addr),
			 ip6_to_str(&v6_rrep.orig_addr),
			 v6_rrep.hop_count, v6_rrep.dest_seqno, v6_rrep.lifetime);
	break;
    case V6_RERR:
	flags = tvb_get_guint8(tvb, offsetof(v6_rerr_t, flags));
	v6_rerr.dest_count =
	    tvb_get_guint8(tvb, offsetof(v6_rerr_t, dest_count));

	if (tree) {
	    proto_tree_add_boolean(aodv_flags_tree,
				   hf_aodv_flags_rerr_nodelete, tvb,
				   offsetof(v6_rerr_t, flags), 1, flags);
	    if (flags & RERR_NODEL)
		proto_item_append_text(tj, " N");
	    proto_tree_add_uint(aodv_tree, hf_aodv_destcount, tvb,
				offsetof(v6_rerr_t, dest_count), 1,
				v6_rerr.dest_count);
	    tk = proto_tree_add_text(aodv_tree, tvb,
				     offsetof(v6_rerr_t, dest_addr),
				     (4 +
				      INET6_ADDRLEN) * v6_rerr.dest_count,
				     "Unreachable Destinations");

	    aodv_unreach_dest_tree =
		proto_item_add_subtree(tk, ett_aodv_unreach_dest);
	    for (i = 0; i < v6_rerr.dest_count; i++) {
		v6_rerr.dest_seqno =
		    tvb_get_ntohl(tvb, offsetof(v6_rerr_t, dest_seqno)
				  + (4 + INET6_ADDRLEN) * i);
		tvb_memcpy(tvb, (guint8 *) & v6_rerr.dest_addr,
			   offsetof(v6_rerr_t, dest_addr)
			   + (4 + INET6_ADDRLEN) * i, INET6_ADDRLEN);
		proto_tree_add_uint(aodv_unreach_dest_tree,
				    hf_aodv_dest_seqno, tvb,
				    offsetof(v6_rerr_t, dest_seqno)
				    + (4 + INET6_ADDRLEN) * i, 4,
				    v6_rerr.dest_seqno);
		proto_tree_add_ipv6(aodv_unreach_dest_tree,
				    hf_aodv_unreach_dest_ipv6, tvb,
				    offsetof(v6_rerr_t, dest_addr)
				    + (4 + INET6_ADDRLEN) * i,
				    INET6_ADDRLEN,
				    (guint8 *) & v6_rerr.dest_addr);
	    }
	}

	if (check_col(pinfo->cinfo, COL_INFO))
	    col_add_fstr(pinfo->cinfo, COL_INFO,
			 "%s, Dest Count=%u",
			 val_to_str(type, type_vals,
				    "Unknown AODV Packet Type (%u)"),
			 v6_rerr.dest_count);
	break;
    case V6_RREP_ACK:
	if (check_col(pinfo->cinfo, COL_INFO))
	    col_add_fstr(pinfo->cinfo, COL_INFO, "%s",
			 val_to_str(type, type_vals,
				    "Unknown AODV Packet Type (%u)"));
	break;
    default:
	proto_tree_add_text(aodv_tree, tvb, 0,
			    1, "Unknown AODV Packet Type (%u)",
			    type);
    }

    return tvb_length(tvb);
}


/* Register the protocol with Ethereal */
void
proto_register_aodv(void)
{
    static hf_register_info hf[] = {
	{ &hf_aodv_type,
	  { "Type", "aodv.type",
	    FT_UINT8, BASE_DEC, VALS(type_vals), 0x0,
	    "AODV packet type", HFILL }
	},
	{ &hf_aodv_flags,
	  { "Flags", "aodv.flags",
	    FT_UINT16, BASE_DEC, NULL, 0x0,
	    "Flags", HFILL }
	},
	{ &hf_aodv_flags_rreq_join,
	  { "RREQ Join", "aodv.flags.rreq_join",
	    FT_BOOLEAN, 8, TFS(&flags_set_truth), RREQ_JOIN,
	    "", HFILL }
	},
	{ &hf_aodv_flags_rreq_repair,
	  { "RREQ Repair", "aodv.flags.rreq_repair",
	    FT_BOOLEAN, 8, TFS(&flags_set_truth), RREQ_REP,
	    "", HFILL }
	},
	{ &hf_aodv_flags_rreq_gratuitous,
	  { "RREQ Gratuitous", "aodv.flags.rreq_gratuitous",
	    FT_BOOLEAN, 8, TFS(&flags_set_truth), RREQ_GRAT,
	    "", HFILL }
	},
	{ &hf_aodv_flags_rrep_repair,
	  { "RREP Repair", "aodv.flags.rrep_repair",
	    FT_BOOLEAN, 8, TFS(&flags_set_truth), RREP_REP,
	    "", HFILL }
	},
	{ &hf_aodv_flags_rrep_ack,
	  { "RREP Acknowledgement", "aodv.flags.rrep_ack",
	    FT_BOOLEAN, 8, TFS(&flags_set_truth), RREP_ACK,
	    "", HFILL }
	},
	{ &hf_aodv_flags_rerr_nodelete,
	  { "RERR No Delete", "aodv.flags.rerr_nodelete",
	    FT_BOOLEAN, 8, TFS(&flags_set_truth), RERR_NODEL,
	    "", HFILL }
	},
	{ &hf_aodv_prefix_sz,
	  { "Prefix Size", "aodv.prefix_sz",
	    FT_UINT8, BASE_DEC, NULL, 0x0,
	    "Prefix Size", HFILL }
	},
	{ &hf_aodv_hopcount,
	  { "Hop Count", "aodv.hopcount",
	    FT_UINT8, BASE_DEC, NULL, 0x0,
	    "Hop Count", HFILL }
	},
	{ &hf_aodv_rreq_id,
	  { "RREQ Id", "aodv.rreq_id",
	    FT_UINT32, BASE_DEC, NULL, 0x0,
	    "RREQ Id", HFILL }
	},
	{ &hf_aodv_dest_ip,
	  { "Destination IP", "aodv.dest_ip",
	    FT_IPv4, BASE_NONE, NULL, 0x0,
	    "Destination IP Address", HFILL }
	},
	{ &hf_aodv_dest_ipv6,
	  { "Destination IPv6", "aodv.dest_ipv6",
	    FT_IPv6, BASE_NONE, NULL, 0x0,
	    "Destination IPv6 Address", HFILL}
	},
	{ &hf_aodv_dest_seqno,
	  { "Destination Sequence Number", "aodv.dest_seqno",
	    FT_UINT32, BASE_DEC, NULL, 0x0,
	    "Destination Sequence Number", HFILL }
	},
	{ &hf_aodv_orig_ip,
	  { "Originator IP", "aodv.orig_ip",
	    FT_IPv4, BASE_NONE, NULL, 0x0,
	    "Originator IP Address", HFILL }
	},
	{ &hf_aodv_orig_ipv6,
	  { "Originator IPv6", "aodv.orig_ipv6",
	    FT_IPv6, BASE_NONE, NULL, 0x0,
	    "Originator IPv6 Address", HFILL}
	},
	{ &hf_aodv_orig_seqno,
	  { "Originator Sequence Number", "aodv.orig_seqno",
	    FT_UINT32, BASE_DEC, NULL, 0x0,
	    "Originator Sequence Number", HFILL }
	},
	{ &hf_aodv_lifetime,
	  { "Lifetime", "aodv.lifetime",
	    FT_UINT32, BASE_DEC, NULL, 0x0,
	    "Lifetime", HFILL }
	},
	{ &hf_aodv_destcount,
	  { "Destination Count", "aodv.destcount",
	    FT_UINT8, BASE_DEC, NULL, 0x0,
	    "Unreachable Destinations Count", HFILL }
	},
	{ &hf_aodv_unreach_dest_ip,
	  { "Unreachable Destination IP", "aodv.unreach_dest_ip",
	    FT_IPv4, BASE_NONE, NULL, 0x0,
	    "Unreachable Destination IP Address", HFILL }
	},
	{ &hf_aodv_unreach_dest_ipv6,
	  { "Unreachable Destination IPv6", "aodv.unreach_dest_ipv6",
	    FT_IPv6, BASE_NONE, NULL, 0x0,
	    "Unreachable Destination IPv6 Address", HFILL}
	},
	{ &hf_aodv_unreach_dest_seqno,
	  { "Unreachable Destination Sequence Number", "aodv.unreach_dest_seqno",
	    FT_UINT32, BASE_DEC, NULL, 0x0,
	    "Unreachable Destination Sequence Number", HFILL }
	},
	{ &hf_aodv_ext_type,
	  { "Extension Type", "aodv.ext_type",
	    FT_UINT8, BASE_DEC, NULL, 0x0,
	    "Extension Format Type", HFILL}
	},
	{ &hf_aodv_ext_length,
	  { "Extension Length", "aodv.ext_length",
	    FT_UINT8, BASE_DEC, NULL, 0x0,
	    "Extension Data Length", HFILL}
	},
	{ &hf_aodv_ext_interval,
	  { "Hello Interval", "aodv.hello_interval",
	    FT_UINT32, BASE_DEC, NULL, 0x0,
	    "Hello Interval Extension", HFILL}
	 },
	{ &hf_aodv_ext_timestamp,
	  { "Timestamp", "aodv.timestamp",
	    FT_UINT64, BASE_DEC, NULL, 0x0,
	    "Timestamp Extension", HFILL}
	 },
    };

/* Setup protocol subtree array */
    static gint *ett[] = {
	&ett_aodv,
	&ett_aodv_flags,
	&ett_aodv_unreach_dest,
	&ett_aodv_extensions,
    };

/* Register the protocol name and description */
    proto_aodv = proto_register_protocol("Ad hoc On-demand Distance Vector Routing Protocol", "AODV", "aodv");

/* Required function calls to register the header fields and subtrees used */
    proto_register_field_array(proto_aodv, hf, array_length(hf));
    proto_register_subtree_array(ett, array_length(ett));
}


void
proto_reg_handoff_aodv(void)
{
    dissector_handle_t aodv_handle;

    aodv_handle = new_create_dissector_handle(dissect_aodv,
					      proto_aodv);
    dissector_add("udp.port", UDP_PORT_AODV, aodv_handle);
}