]> The Tcpdump Group git mirrors - tcpdump/blob - print-aodv.c
updated version
[tcpdump] / print-aodv.c
1 /*
2 * Copyright (c) 2003 Bruce M. Simpson <bms@spc.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Bruce M. Simpson.
16 * 4. Neither the name of Bruce M. Simpson nor the names of co-
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY Bruce M. Simpson AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Bruce M. Simpson OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #ifdef HAVE_CONFIG_H
34 #include "config.h"
35 #endif
36
37 #include <netdissect-stdinc.h>
38
39 #include "netdissect.h"
40 #include "addrtoname.h"
41 #include "extract.h"
42
43
44 struct aodv_rreq {
45 uint8_t rreq_type; /* AODV message type (1) */
46 uint8_t rreq_flags; /* various flags */
47 uint8_t rreq_zero0; /* reserved, set to zero */
48 uint8_t rreq_hops; /* number of hops from originator */
49 uint32_t rreq_id; /* request ID */
50 uint32_t rreq_da; /* destination IPv4 address */
51 uint32_t rreq_ds; /* destination sequence number */
52 uint32_t rreq_oa; /* originator IPv4 address */
53 uint32_t rreq_os; /* originator sequence number */
54 };
55 struct aodv_rreq6 {
56 uint8_t rreq_type; /* AODV message type (1) */
57 uint8_t rreq_flags; /* various flags */
58 uint8_t rreq_zero0; /* reserved, set to zero */
59 uint8_t rreq_hops; /* number of hops from originator */
60 uint32_t rreq_id; /* request ID */
61 struct in6_addr rreq_da; /* destination IPv6 address */
62 uint32_t rreq_ds; /* destination sequence number */
63 struct in6_addr rreq_oa; /* originator IPv6 address */
64 uint32_t rreq_os; /* originator sequence number */
65 };
66 struct aodv_rreq6_draft_01 {
67 uint8_t rreq_type; /* AODV message type (16) */
68 uint8_t rreq_flags; /* various flags */
69 uint8_t rreq_zero0; /* reserved, set to zero */
70 uint8_t rreq_hops; /* number of hops from originator */
71 uint32_t rreq_id; /* request ID */
72 uint32_t rreq_ds; /* destination sequence number */
73 uint32_t rreq_os; /* originator sequence number */
74 struct in6_addr rreq_da; /* destination IPv6 address */
75 struct in6_addr rreq_oa; /* originator IPv6 address */
76 };
77
78 #define RREQ_JOIN 0x80 /* join (reserved for multicast */
79 #define RREQ_REPAIR 0x40 /* repair (reserved for multicast */
80 #define RREQ_GRAT 0x20 /* gratuitous RREP */
81 #define RREQ_DEST 0x10 /* destination only */
82 #define RREQ_UNKNOWN 0x08 /* unknown destination sequence num */
83 #define RREQ_FLAGS_MASK 0xF8 /* mask for rreq_flags */
84
85 struct aodv_rrep {
86 uint8_t rrep_type; /* AODV message type (2) */
87 uint8_t rrep_flags; /* various flags */
88 uint8_t rrep_ps; /* prefix size */
89 uint8_t rrep_hops; /* number of hops from o to d */
90 uint32_t rrep_da; /* destination IPv4 address */
91 uint32_t rrep_ds; /* destination sequence number */
92 uint32_t rrep_oa; /* originator IPv4 address */
93 uint32_t rrep_life; /* lifetime of this route */
94 };
95 struct aodv_rrep6 {
96 uint8_t rrep_type; /* AODV message type (2) */
97 uint8_t rrep_flags; /* various flags */
98 uint8_t rrep_ps; /* prefix size */
99 uint8_t rrep_hops; /* number of hops from o to d */
100 struct in6_addr rrep_da; /* destination IPv6 address */
101 uint32_t rrep_ds; /* destination sequence number */
102 struct in6_addr rrep_oa; /* originator IPv6 address */
103 uint32_t rrep_life; /* lifetime of this route */
104 };
105 struct aodv_rrep6_draft_01 {
106 uint8_t rrep_type; /* AODV message type (17) */
107 uint8_t rrep_flags; /* various flags */
108 uint8_t rrep_ps; /* prefix size */
109 uint8_t rrep_hops; /* number of hops from o to d */
110 uint32_t rrep_ds; /* destination sequence number */
111 struct in6_addr rrep_da; /* destination IPv6 address */
112 struct in6_addr rrep_oa; /* originator IPv6 address */
113 uint32_t rrep_life; /* lifetime of this route */
114 };
115
116 #define RREP_REPAIR 0x80 /* repair (reserved for multicast */
117 #define RREP_ACK 0x40 /* acknowledgement required */
118 #define RREP_FLAGS_MASK 0xC0 /* mask for rrep_flags */
119 #define RREP_PREFIX_MASK 0x1F /* mask for prefix size */
120
121 struct rerr_unreach {
122 uint32_t u_da; /* IPv4 address */
123 uint32_t u_ds; /* sequence number */
124 };
125 struct rerr_unreach6 {
126 struct in6_addr u_da; /* IPv6 address */
127 uint32_t u_ds; /* sequence number */
128 };
129 struct rerr_unreach6_draft_01 {
130 struct in6_addr u_da; /* IPv6 address */
131 uint32_t u_ds; /* sequence number */
132 };
133
134 struct aodv_rerr {
135 uint8_t rerr_type; /* AODV message type (3 or 18) */
136 uint8_t rerr_flags; /* various flags */
137 uint8_t rerr_zero0; /* reserved, set to zero */
138 uint8_t rerr_dc; /* destination count */
139 };
140
141 #define RERR_NODELETE 0x80 /* don't delete the link */
142 #define RERR_FLAGS_MASK 0x80 /* mask for rerr_flags */
143
144 struct aodv_rrep_ack {
145 uint8_t ra_type;
146 uint8_t ra_zero0;
147 };
148
149 #define AODV_RREQ 1 /* route request */
150 #define AODV_RREP 2 /* route response */
151 #define AODV_RERR 3 /* error report */
152 #define AODV_RREP_ACK 4 /* route response acknowledgement */
153
154 #define AODV_V6_DRAFT_01_RREQ 16 /* IPv6 route request */
155 #define AODV_V6_DRAFT_01_RREP 17 /* IPv6 route response */
156 #define AODV_V6_DRAFT_01_RERR 18 /* IPv6 error report */
157 #define AODV_V6_DRAFT_01_RREP_ACK 19 /* IPV6 route response acknowledgment */
158
159 struct aodv_ext {
160 uint8_t type; /* extension type */
161 uint8_t length; /* extension length */
162 };
163
164 struct aodv_hello {
165 struct aodv_ext eh; /* extension header */
166 uint8_t interval[4]; /* expect my next hello in
167 * (n) ms
168 * NOTE: this is not aligned */
169 };
170
171 #define AODV_EXT_HELLO 1
172
173 static void
174 aodv_extension(netdissect_options *ndo,
175 const struct aodv_ext *ep, u_int length)
176 {
177 const struct aodv_hello *ah;
178
179 switch (ep->type) {
180 case AODV_EXT_HELLO:
181 ah = (const struct aodv_hello *)(const void *)ep;
182 ND_TCHECK(*ah);
183 if (length < sizeof(struct aodv_hello))
184 goto trunc;
185 ND_PRINT((ndo, "\n\text HELLO %ld ms",
186 (unsigned long)EXTRACT_32BITS(&ah->interval)));
187 break;
188
189 default:
190 ND_PRINT((ndo, "\n\text %u %u", ep->type, ep->length));
191 break;
192 }
193 return;
194
195 trunc:
196 ND_PRINT((ndo, " [|hello]"));
197 }
198
199 static void
200 aodv_rreq(netdissect_options *ndo, const u_char *dat, u_int length)
201 {
202 u_int i;
203 const struct aodv_rreq *ap = (const struct aodv_rreq *)dat;
204
205 ND_TCHECK(*ap);
206 if (length < sizeof(*ap))
207 goto trunc;
208 ND_PRINT((ndo, " rreq %u %s%s%s%s%shops %u id 0x%08lx\n"
209 "\tdst %s seq %lu src %s seq %lu", length,
210 ap->rreq_type & RREQ_JOIN ? "[J]" : "",
211 ap->rreq_type & RREQ_REPAIR ? "[R]" : "",
212 ap->rreq_type & RREQ_GRAT ? "[G]" : "",
213 ap->rreq_type & RREQ_DEST ? "[D]" : "",
214 ap->rreq_type & RREQ_UNKNOWN ? "[U] " : " ",
215 ap->rreq_hops,
216 (unsigned long)EXTRACT_32BITS(&ap->rreq_id),
217 ipaddr_string(ndo, &ap->rreq_da),
218 (unsigned long)EXTRACT_32BITS(&ap->rreq_ds),
219 ipaddr_string(ndo, &ap->rreq_oa),
220 (unsigned long)EXTRACT_32BITS(&ap->rreq_os)));
221 i = length - sizeof(*ap);
222 if (i >= sizeof(struct aodv_ext))
223 aodv_extension(ndo, (const struct aodv_ext *)(dat + sizeof(*ap)), i);
224 return;
225
226 trunc:
227 ND_PRINT((ndo, " [|rreq"));
228 }
229
230 static void
231 aodv_rrep(netdissect_options *ndo, const u_char *dat, u_int length)
232 {
233 u_int i;
234 const struct aodv_rrep *ap = (const struct aodv_rrep *)dat;
235
236 ND_TCHECK(*ap);
237 if (length < sizeof(*ap))
238 goto trunc;
239 ND_PRINT((ndo, " rrep %u %s%sprefix %u hops %u\n"
240 "\tdst %s dseq %lu src %s %lu ms", length,
241 ap->rrep_type & RREP_REPAIR ? "[R]" : "",
242 ap->rrep_type & RREP_ACK ? "[A] " : " ",
243 ap->rrep_ps & RREP_PREFIX_MASK,
244 ap->rrep_hops,
245 ipaddr_string(ndo, &ap->rrep_da),
246 (unsigned long)EXTRACT_32BITS(&ap->rrep_ds),
247 ipaddr_string(ndo, &ap->rrep_oa),
248 (unsigned long)EXTRACT_32BITS(&ap->rrep_life)));
249 i = length - sizeof(*ap);
250 if (i >= sizeof(struct aodv_ext))
251 aodv_extension(ndo, (const struct aodv_ext *)(dat + sizeof(*ap)), i);
252 return;
253
254 trunc:
255 ND_PRINT((ndo, " [|rreq"));
256 }
257
258 static void
259 aodv_rerr(netdissect_options *ndo, const u_char *dat, u_int length)
260 {
261 u_int i, dc;
262 const struct aodv_rerr *ap = (const struct aodv_rerr *)dat;
263 const struct rerr_unreach *dp;
264
265 ND_TCHECK(*ap);
266 if (length < sizeof(*ap))
267 goto trunc;
268 ND_PRINT((ndo, " rerr %s [items %u] [%u]:",
269 ap->rerr_flags & RERR_NODELETE ? "[D]" : "",
270 ap->rerr_dc, length));
271 dp = (const struct rerr_unreach *)(dat + sizeof(*ap));
272 i = length - sizeof(*ap);
273 for (dc = ap->rerr_dc; dc != 0; dc--) {
274 ND_TCHECK(*dp);
275 if (i < sizeof(*dp))
276 goto trunc;
277 ND_PRINT((ndo, " {%s}(%ld)", ipaddr_string(ndo, &dp->u_da),
278 (unsigned long)EXTRACT_32BITS(&dp->u_ds)));
279 dp++;
280 i -= sizeof(*dp);
281 }
282 return;
283
284 trunc:
285 ND_PRINT((ndo, "[|rerr]"));
286 }
287
288 static void
289 aodv_v6_rreq(netdissect_options *ndo, const u_char *dat, u_int length)
290 {
291 u_int i;
292 const struct aodv_rreq6 *ap = (const struct aodv_rreq6 *)dat;
293
294 ND_TCHECK(*ap);
295 if (length < sizeof(*ap))
296 goto trunc;
297 ND_PRINT((ndo, " v6 rreq %u %s%s%s%s%shops %u id 0x%08lx\n"
298 "\tdst %s seq %lu src %s seq %lu", length,
299 ap->rreq_type & RREQ_JOIN ? "[J]" : "",
300 ap->rreq_type & RREQ_REPAIR ? "[R]" : "",
301 ap->rreq_type & RREQ_GRAT ? "[G]" : "",
302 ap->rreq_type & RREQ_DEST ? "[D]" : "",
303 ap->rreq_type & RREQ_UNKNOWN ? "[U] " : " ",
304 ap->rreq_hops,
305 (unsigned long)EXTRACT_32BITS(&ap->rreq_id),
306 ip6addr_string(ndo, &ap->rreq_da),
307 (unsigned long)EXTRACT_32BITS(&ap->rreq_ds),
308 ip6addr_string(ndo, &ap->rreq_oa),
309 (unsigned long)EXTRACT_32BITS(&ap->rreq_os)));
310 i = length - sizeof(*ap);
311 if (i >= sizeof(struct aodv_ext))
312 aodv_extension(ndo, (const struct aodv_ext *)(dat + sizeof(*ap)), i);
313 return;
314
315 trunc:
316 ND_PRINT((ndo, " [|rreq"));
317 }
318
319 static void
320 aodv_v6_rrep(netdissect_options *ndo, const u_char *dat, u_int length)
321 {
322 u_int i;
323 const struct aodv_rrep6 *ap = (const struct aodv_rrep6 *)dat;
324
325 ND_TCHECK(*ap);
326 if (length < sizeof(*ap))
327 goto trunc;
328 ND_PRINT((ndo, " rrep %u %s%sprefix %u hops %u\n"
329 "\tdst %s dseq %lu src %s %lu ms", length,
330 ap->rrep_type & RREP_REPAIR ? "[R]" : "",
331 ap->rrep_type & RREP_ACK ? "[A] " : " ",
332 ap->rrep_ps & RREP_PREFIX_MASK,
333 ap->rrep_hops,
334 ip6addr_string(ndo, &ap->rrep_da),
335 (unsigned long)EXTRACT_32BITS(&ap->rrep_ds),
336 ip6addr_string(ndo, &ap->rrep_oa),
337 (unsigned long)EXTRACT_32BITS(&ap->rrep_life)));
338 i = length - sizeof(*ap);
339 if (i >= sizeof(struct aodv_ext))
340 aodv_extension(ndo, (const struct aodv_ext *)(dat + sizeof(*ap)), i);
341 return;
342
343 trunc:
344 ND_PRINT((ndo, " [|rreq"));
345 }
346
347 static void
348 aodv_v6_rerr(netdissect_options *ndo, const u_char *dat, u_int length)
349 {
350 u_int i, dc;
351 const struct aodv_rerr *ap = (const struct aodv_rerr *)dat;
352 const struct rerr_unreach6 *dp6;
353
354 ND_TCHECK(*ap);
355 if (length < sizeof(*ap))
356 goto trunc;
357 ND_PRINT((ndo, " rerr %s [items %u] [%u]:",
358 ap->rerr_flags & RERR_NODELETE ? "[D]" : "",
359 ap->rerr_dc, length));
360 dp6 = (const struct rerr_unreach6 *)(const void *)(ap + 1);
361 i = length - sizeof(*ap);
362 for (dc = ap->rerr_dc; dc != 0; dc--) {
363 ND_TCHECK(*dp6);
364 if (i < sizeof(*dp6))
365 goto trunc;
366 ND_PRINT((ndo, " {%s}(%ld)", ip6addr_string(ndo, &dp6->u_da),
367 (unsigned long)EXTRACT_32BITS(&dp6->u_ds)));
368 dp6++;
369 i -= sizeof(*dp6);
370 }
371 return;
372
373 trunc:
374 ND_PRINT((ndo, "[|rerr]"));
375 }
376
377 static void
378 aodv_v6_draft_01_rreq(netdissect_options *ndo, const u_char *dat, u_int length)
379 {
380 u_int i;
381 const struct aodv_rreq6_draft_01 *ap = (const struct aodv_rreq6_draft_01 *)dat;
382
383 ND_TCHECK(*ap);
384 if (length < sizeof(*ap))
385 goto trunc;
386 ND_PRINT((ndo, " rreq %u %s%s%s%s%shops %u id 0x%08lx\n"
387 "\tdst %s seq %lu src %s seq %lu", length,
388 ap->rreq_type & RREQ_JOIN ? "[J]" : "",
389 ap->rreq_type & RREQ_REPAIR ? "[R]" : "",
390 ap->rreq_type & RREQ_GRAT ? "[G]" : "",
391 ap->rreq_type & RREQ_DEST ? "[D]" : "",
392 ap->rreq_type & RREQ_UNKNOWN ? "[U] " : " ",
393 ap->rreq_hops,
394 (unsigned long)EXTRACT_32BITS(&ap->rreq_id),
395 ip6addr_string(ndo, &ap->rreq_da),
396 (unsigned long)EXTRACT_32BITS(&ap->rreq_ds),
397 ip6addr_string(ndo, &ap->rreq_oa),
398 (unsigned long)EXTRACT_32BITS(&ap->rreq_os)));
399 i = length - sizeof(*ap);
400 if (i >= sizeof(struct aodv_ext))
401 aodv_extension(ndo, (const struct aodv_ext *)(dat + sizeof(*ap)), i);
402 return;
403
404 trunc:
405 ND_PRINT((ndo, " [|rreq"));
406 }
407
408 static void
409 aodv_v6_draft_01_rrep(netdissect_options *ndo, const u_char *dat, u_int length)
410 {
411 u_int i;
412 const struct aodv_rrep6_draft_01 *ap = (const struct aodv_rrep6_draft_01 *)dat;
413
414 ND_TCHECK(*ap);
415 if (length < sizeof(*ap))
416 goto trunc;
417 ND_PRINT((ndo, " rrep %u %s%sprefix %u hops %u\n"
418 "\tdst %s dseq %lu src %s %lu ms", length,
419 ap->rrep_type & RREP_REPAIR ? "[R]" : "",
420 ap->rrep_type & RREP_ACK ? "[A] " : " ",
421 ap->rrep_ps & RREP_PREFIX_MASK,
422 ap->rrep_hops,
423 ip6addr_string(ndo, &ap->rrep_da),
424 (unsigned long)EXTRACT_32BITS(&ap->rrep_ds),
425 ip6addr_string(ndo, &ap->rrep_oa),
426 (unsigned long)EXTRACT_32BITS(&ap->rrep_life)));
427 i = length - sizeof(*ap);
428 if (i >= sizeof(struct aodv_ext))
429 aodv_extension(ndo, (const struct aodv_ext *)(dat + sizeof(*ap)), i);
430 return;
431
432 trunc:
433 ND_PRINT((ndo, " [|rreq"));
434 }
435
436 static void
437 aodv_v6_draft_01_rerr(netdissect_options *ndo, const u_char *dat, u_int length)
438 {
439 u_int i, dc;
440 const struct aodv_rerr *ap = (const struct aodv_rerr *)dat;
441 const struct rerr_unreach6_draft_01 *dp6;
442
443 ND_TCHECK(*ap);
444 if (length < sizeof(*ap))
445 goto trunc;
446 ND_PRINT((ndo, " rerr %s [items %u] [%u]:",
447 ap->rerr_flags & RERR_NODELETE ? "[D]" : "",
448 ap->rerr_dc, length));
449 dp6 = (const struct rerr_unreach6_draft_01 *)(const void *)(ap + 1);
450 i = length - sizeof(*ap);
451 for (dc = ap->rerr_dc; dc != 0; dc--) {
452 ND_TCHECK(*dp6);
453 if (i < sizeof(*dp6))
454 goto trunc;
455 ND_PRINT((ndo, " {%s}(%ld)", ip6addr_string(ndo, &dp6->u_da),
456 (unsigned long)EXTRACT_32BITS(&dp6->u_ds)));
457 dp6++;
458 i -= sizeof(*dp6);
459 }
460 return;
461
462 trunc:
463 ND_PRINT((ndo, "[|rerr]"));
464 }
465
466 void
467 aodv_print(netdissect_options *ndo,
468 const u_char *dat, u_int length, int is_ip6)
469 {
470 uint8_t msg_type;
471
472 /*
473 * The message type is the first byte; make sure we have it
474 * and then fetch it.
475 */
476 ND_TCHECK(*dat);
477 msg_type = *dat;
478 ND_PRINT((ndo, " aodv"));
479
480 switch (msg_type) {
481
482 case AODV_RREQ:
483 if (is_ip6)
484 aodv_v6_rreq(ndo, dat, length);
485 else
486 aodv_rreq(ndo, dat, length);
487 break;
488
489 case AODV_RREP:
490 if (is_ip6)
491 aodv_v6_rrep(ndo, dat, length);
492 else
493 aodv_rrep(ndo, dat, length);
494 break;
495
496 case AODV_RERR:
497 if (is_ip6)
498 aodv_v6_rerr(ndo, dat, length);
499 else
500 aodv_rerr(ndo, dat, length);
501 break;
502
503 case AODV_RREP_ACK:
504 ND_PRINT((ndo, " rrep-ack %u", length));
505 break;
506
507 case AODV_V6_DRAFT_01_RREQ:
508 aodv_v6_draft_01_rreq(ndo, dat, length);
509 break;
510
511 case AODV_V6_DRAFT_01_RREP:
512 aodv_v6_draft_01_rrep(ndo, dat, length);
513 break;
514
515 case AODV_V6_DRAFT_01_RERR:
516 aodv_v6_draft_01_rerr(ndo, dat, length);
517 break;
518
519 case AODV_V6_DRAFT_01_RREP_ACK:
520 ND_PRINT((ndo, " rrep-ack %u", length));
521 break;
522
523 default:
524 ND_PRINT((ndo, " type %u %u", msg_type, length));
525 }
526 return;
527
528 trunc:
529 ND_PRINT((ndo, " [|aodv]"));
530 }