]> The Tcpdump Group git mirrors - tcpdump/blob - print-olsr.c
Fix the pointer tests in the non-ndoified TTEST2() macro as well.
[tcpdump] / print-olsr.c
1 /*
2 * Copyright (c) 1998-2007 The TCPDUMP project
3 * Copyright (c) 2009 Florian Forster
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that: (1) source code
7 * distributions retain the above copyright notice and this paragraph
8 * in its entirety, and (2) distributions including binary code include
9 * the above copyright notice and this paragraph in its entirety in
10 * the documentation or other materials provided with the distribution.
11 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND
12 * WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT
13 * LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
14 * FOR A PARTICULAR PURPOSE.
15 *
16 * Optimized Link State Protocl (OLSR) as per rfc3626
17 *
18 * Original code by Hannes Gredler <hannes@juniper.net>
19 * IPv6 additions by Florian Forster <octo at verplant.org>
20 */
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include <tcpdump-stdinc.h>
27
28 #include <stdio.h>
29 #include <string.h>
30
31 #include "interface.h"
32 #include "addrtoname.h"
33 #include "extract.h"
34 #include "ip.h"
35
36 /*
37 * RFC 3626 common header
38 *
39 * 0 1 2 3
40 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
41 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
42 * | Packet Length | Packet Sequence Number |
43 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
44 * | Message Type | Vtime | Message Size |
45 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
46 * | Originator Address |
47 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
48 * | Time To Live | Hop Count | Message Sequence Number |
49 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
50 * | |
51 * : MESSAGE :
52 * | |
53 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
54 * | Message Type | Vtime | Message Size |
55 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
56 * | Originator Address |
57 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
58 * | Time To Live | Hop Count | Message Sequence Number |
59 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
60 * | |
61 * : MESSAGE :
62 * | |
63 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
64 * : :
65 */
66
67 struct olsr_common {
68 u_int8_t packet_len[2];
69 u_int8_t packet_seq[2];
70 };
71
72 #define OLSR_HELLO_MSG 1 /* rfc3626 */
73 #define OLSR_TC_MSG 2 /* rfc3626 */
74 #define OLSR_MID_MSG 3 /* rfc3626 */
75 #define OLSR_HNA_MSG 4 /* rfc3626 */
76 #define OLSR_POWERINFO_MSG 128
77 #define OLSR_NAMESERVICE_MSG 130
78 #define OLSR_HELLO_LQ_MSG 201 /* LQ extensions olsr.org */
79 #define OLSR_TC_LQ_MSG 202 /* LQ extensions olsr.org */
80
81 static struct tok olsr_msg_values[] = {
82 { OLSR_HELLO_MSG, "Hello" },
83 { OLSR_TC_MSG, "TC" },
84 { OLSR_MID_MSG, "MID" },
85 { OLSR_HNA_MSG, "HNA" },
86 { OLSR_POWERINFO_MSG, "Powerinfo" },
87 { OLSR_NAMESERVICE_MSG, "Nameservice" },
88 { OLSR_HELLO_LQ_MSG, "Hello-LQ" },
89 { OLSR_TC_LQ_MSG, "TC-LQ" },
90 { 0, NULL}
91 };
92
93 struct olsr_msg4 {
94 u_int8_t msg_type;
95 u_int8_t vtime;
96 u_int8_t msg_len[2];
97 u_int8_t originator[4];
98 u_int8_t ttl;
99 u_int8_t hopcount;
100 u_int8_t msg_seq[2];
101 };
102
103 struct olsr_msg6 {
104 u_int8_t msg_type;
105 u_int8_t vtime;
106 u_int8_t msg_len[2];
107 u_int8_t originator[16];
108 u_int8_t ttl;
109 u_int8_t hopcount;
110 u_int8_t msg_seq[2];
111 };
112
113 struct olsr_hello {
114 u_int8_t res[2];
115 u_int8_t htime;
116 u_int8_t will;
117 };
118
119 struct olsr_hello_link {
120 u_int8_t link_code;
121 u_int8_t res;
122 u_int8_t len[2];
123 };
124
125 struct olsr_tc {
126 u_int8_t ans_seq[2];
127 u_int8_t res[2];
128 };
129
130 struct olsr_hna4 {
131 u_int8_t network[4];
132 u_int8_t mask[4];
133 };
134
135 struct olsr_hna6 {
136 u_int8_t network[16];
137 u_int8_t mask[16];
138 };
139
140
141 #define OLSR_EXTRACT_LINK_TYPE(link_code) (link_code & 0x3)
142 #define OLSR_EXTRACT_NEIGHBOR_TYPE(link_code) (link_code >> 2)
143
144 static struct tok olsr_link_type_values[] = {
145 { 0, "Unspecified" },
146 { 1, "Asymmetric" },
147 { 2, "Symmetric" },
148 { 3, "Lost" },
149 { 0, NULL}
150 };
151
152 static struct tok olsr_neighbor_type_values[] = {
153 { 0, "Not-Neighbor" },
154 { 1, "Symmetric" },
155 { 2, "Symmetric-MPR" },
156 { 0, NULL}
157 };
158
159 struct olsr_lq_neighbor4 {
160 u_int8_t neighbor[4];
161 u_int8_t link_quality;
162 u_int8_t neighbor_link_quality;
163 u_int8_t res[2];
164 };
165
166 struct olsr_lq_neighbor6 {
167 u_int8_t neighbor[16];
168 u_int8_t link_quality;
169 u_int8_t neighbor_link_quality;
170 u_int8_t res[2];
171 };
172
173 /*
174 * macro to convert the 8-bit mantissa/exponent to a double float
175 * taken from olsr.org.
176 */
177 #define VTIME_SCALE_FACTOR 0.0625
178 #define ME_TO_DOUBLE(me) \
179 (double)(VTIME_SCALE_FACTOR*(1+(double)(me>>4)/16)*(double)(1<<(me&0x0F)))
180
181 /*
182 * print a neighbor list with LQ extensions.
183 */
184 static int
185 olsr_print_lq_neighbor4 (const u_char *msg_data, u_int hello_len)
186 {
187 struct olsr_lq_neighbor4 *lq_neighbor;
188
189 while (hello_len >= sizeof(struct olsr_lq_neighbor4)) {
190
191 lq_neighbor = (struct olsr_lq_neighbor4 *)msg_data;
192 if (!TTEST(*lq_neighbor))
193 return (-1);
194
195 printf("\n\t neighbor %s, link-quality %.2lf%%"
196 ", neighbor-link-quality %.2lf%%",
197 ipaddr_string(lq_neighbor->neighbor),
198 ((double)lq_neighbor->link_quality/2.55),
199 ((double)lq_neighbor->neighbor_link_quality/2.55));
200
201 msg_data += sizeof(struct olsr_lq_neighbor4);
202 hello_len -= sizeof(struct olsr_lq_neighbor4);
203 }
204 return (0);
205 }
206
207 #if INET6
208 static int
209 olsr_print_lq_neighbor6 (const u_char *msg_data, u_int hello_len)
210 {
211 struct olsr_lq_neighbor6 *lq_neighbor;
212
213 while (hello_len >= sizeof(struct olsr_lq_neighbor6)) {
214
215 lq_neighbor = (struct olsr_lq_neighbor6 *)msg_data;
216 if (!TTEST(*lq_neighbor))
217 return (-1);
218
219 printf("\n\t neighbor %s, link-quality %.2lf%%"
220 ", neighbor-link-quality %.2lf%%",
221 ip6addr_string(lq_neighbor->neighbor),
222 ((double)lq_neighbor->link_quality/2.55),
223 ((double)lq_neighbor->neighbor_link_quality/2.55));
224
225 msg_data += sizeof(struct olsr_lq_neighbor6);
226 hello_len -= sizeof(struct olsr_lq_neighbor6);
227 }
228 return (0);
229 }
230 #endif /* INET6 */
231
232 /*
233 * print a neighbor list.
234 */
235 static int
236 olsr_print_neighbor (const u_char *msg_data, u_int hello_len)
237 {
238 int neighbor;
239
240 printf("\n\t neighbor\n\t\t");
241 neighbor = 1;
242
243 while (hello_len >= sizeof(struct in_addr)) {
244
245 if (!TTEST2(*msg_data, sizeof(struct in_addr)))
246 return (-1);
247 /* print 4 neighbors per line */
248
249 printf("%s%s", ipaddr_string(msg_data),
250 neighbor % 4 == 0 ? "\n\t\t" : " ");
251
252 msg_data += sizeof(struct in_addr);
253 hello_len -= sizeof(struct in_addr);
254 }
255 return (0);
256 }
257
258
259 void
260 olsr_print (const u_char *pptr, u_int length, int is_ipv6)
261 {
262 union {
263 const struct olsr_common *common;
264 const struct olsr_msg4 *msg4;
265 const struct olsr_msg6 *msg6;
266 const struct olsr_hello *hello;
267 const struct olsr_hello_link *hello_link;
268 const struct olsr_tc *tc;
269 const struct olsr_hna4 *hna;
270 } ptr;
271
272 u_int msg_type, msg_len, msg_tlen, hello_len;
273 u_int16_t name_entry_type, name_entry_len;
274 u_int name_entry_padding;
275 u_int8_t link_type, neighbor_type;
276 const u_char *tptr, *msg_data;
277
278 tptr = pptr;
279
280 if (length < sizeof(struct olsr_common)) {
281 goto trunc;
282 }
283
284 if (!TTEST2(*tptr, sizeof(struct olsr_common))) {
285 goto trunc;
286 }
287
288 ptr.common = (struct olsr_common *)tptr;
289 length = MIN(length, EXTRACT_16BITS(ptr.common->packet_len));
290
291 printf("OLSRv%i, seq 0x%04x, length %u",
292 (is_ipv6 == 0) ? 4 : 6,
293 EXTRACT_16BITS(ptr.common->packet_seq),
294 length);
295
296 tptr += sizeof(struct olsr_common);
297
298 /*
299 * In non-verbose mode, just print version.
300 */
301 if (vflag < 1) {
302 return;
303 }
304
305 while (tptr < (pptr+length)) {
306 union
307 {
308 struct olsr_msg4 *v4;
309 struct olsr_msg6 *v6;
310 } msgptr;
311 int msg_len_valid = 0;
312
313 if (!TTEST2(*tptr, sizeof(struct olsr_msg4)))
314 goto trunc;
315
316 #if INET6
317 if (is_ipv6)
318 {
319 msgptr.v6 = (struct olsr_msg6 *) tptr;
320 msg_type = msgptr.v6->msg_type;
321 msg_len = EXTRACT_16BITS(msgptr.v6->msg_len);
322 if ((msg_len >= sizeof (struct olsr_msg6))
323 && (msg_len <= length))
324 msg_len_valid = 1;
325
326 /* infinite loop check */
327 if (msg_type == 0 || msg_len == 0) {
328 return;
329 }
330
331 printf("\n\t%s Message (%#04x), originator %s, ttl %u, hop %u"
332 "\n\t vtime %.3lfs, msg-seq 0x%04x, length %u%s",
333 tok2str(olsr_msg_values, "Unknown", msg_type),
334 msg_type, ip6addr_string(msgptr.v6->originator),
335 msgptr.v6->ttl,
336 msgptr.v6->hopcount,
337 ME_TO_DOUBLE(msgptr.v6->vtime),
338 EXTRACT_16BITS(msgptr.v6->msg_seq),
339 msg_len, (msg_len_valid == 0) ? " (invalid)" : "");
340 if (!msg_len_valid) {
341 return;
342 }
343
344 msg_tlen = msg_len - sizeof(struct olsr_msg6);
345 msg_data = tptr + sizeof(struct olsr_msg6);
346 }
347 else /* (!is_ipv6) */
348 #endif /* INET6 */
349 {
350 msgptr.v4 = (struct olsr_msg4 *) tptr;
351 msg_type = msgptr.v4->msg_type;
352 msg_len = EXTRACT_16BITS(msgptr.v4->msg_len);
353 if ((msg_len >= sizeof (struct olsr_msg4))
354 && (msg_len <= length))
355 msg_len_valid = 1;
356
357 /* infinite loop check */
358 if (msg_type == 0 || msg_len == 0) {
359 return;
360 }
361
362 printf("\n\t%s Message (%#04x), originator %s, ttl %u, hop %u"
363 "\n\t vtime %.3lfs, msg-seq 0x%04x, length %u%s",
364 tok2str(olsr_msg_values, "Unknown", msg_type),
365 msg_type, ipaddr_string(msgptr.v4->originator),
366 msgptr.v4->ttl,
367 msgptr.v4->hopcount,
368 ME_TO_DOUBLE(msgptr.v4->vtime),
369 EXTRACT_16BITS(msgptr.v4->msg_seq),
370 msg_len, (msg_len_valid == 0) ? " (invalid)" : "");
371 if (!msg_len_valid) {
372 return;
373 }
374
375 msg_tlen = msg_len - sizeof(struct olsr_msg4);
376 msg_data = tptr + sizeof(struct olsr_msg4);
377 }
378
379 switch (msg_type) {
380 case OLSR_HELLO_MSG:
381 case OLSR_HELLO_LQ_MSG:
382 if (msg_tlen < sizeof(struct olsr_hello))
383 goto trunc;
384 TCHECK2(*msg_data, sizeof(struct olsr_hello));
385
386 ptr.hello = (struct olsr_hello *)msg_data;
387 printf("\n\t hello-time %.3lfs, MPR willingness %u",
388 ME_TO_DOUBLE(ptr.hello->htime), ptr.hello->will);
389 msg_data += sizeof(struct olsr_hello);
390 msg_tlen -= sizeof(struct olsr_hello);
391
392 while (msg_tlen >= sizeof(struct olsr_hello_link)) {
393 int hello_len_valid = 0;
394
395 /*
396 * link-type.
397 */
398 if (!TTEST2(*msg_data, sizeof(struct olsr_hello_link)))
399 goto trunc;
400
401 ptr.hello_link = (struct olsr_hello_link *)msg_data;
402
403 hello_len = EXTRACT_16BITS(ptr.hello_link->len);
404 link_type = OLSR_EXTRACT_LINK_TYPE(ptr.hello_link->link_code);
405 neighbor_type = OLSR_EXTRACT_NEIGHBOR_TYPE(ptr.hello_link->link_code);
406
407 if ((hello_len <= msg_tlen)
408 && (hello_len >= sizeof(struct olsr_hello_link)))
409 hello_len_valid = 1;
410
411 printf("\n\t link-type %s, neighbor-type %s, len %u%s",
412 tok2str(olsr_link_type_values, "Unknown", link_type),
413 tok2str(olsr_neighbor_type_values, "Unknown", neighbor_type),
414 hello_len,
415 (hello_len_valid == 0) ? " (invalid)" : "");
416
417 if (hello_len_valid == 0)
418 break;
419
420 msg_data += sizeof(struct olsr_hello_link);
421 msg_tlen -= sizeof(struct olsr_hello_link);
422 hello_len -= sizeof(struct olsr_hello_link);
423
424 TCHECK2(*msg_data, hello_len);
425 if (msg_type == OLSR_HELLO_MSG) {
426 if (olsr_print_neighbor(msg_data, hello_len) == -1)
427 goto trunc;
428 } else {
429 #if INET6
430 if (is_ipv6) {
431 if (olsr_print_lq_neighbor6(msg_data, hello_len) == -1)
432 goto trunc;
433 } else
434 #endif
435 {
436 if (olsr_print_lq_neighbor4(msg_data, hello_len) == -1)
437 goto trunc;
438 }
439 }
440
441 msg_data += hello_len;
442 msg_tlen -= hello_len;
443 }
444 break;
445
446 case OLSR_TC_MSG:
447 case OLSR_TC_LQ_MSG:
448 if (msg_tlen < sizeof(struct olsr_tc))
449 goto trunc;
450 TCHECK2(*msg_data, sizeof(struct olsr_tc));
451
452 ptr.tc = (struct olsr_tc *)msg_data;
453 printf("\n\t advertised neighbor seq 0x%04x",
454 EXTRACT_16BITS(ptr.tc->ans_seq));
455 msg_data += sizeof(struct olsr_tc);
456 msg_tlen -= sizeof(struct olsr_tc);
457
458 if (msg_type == OLSR_TC_MSG) {
459 if (olsr_print_neighbor(msg_data, msg_tlen) == -1)
460 goto trunc;
461 } else {
462 #if INET6
463 if (is_ipv6) {
464 if (olsr_print_lq_neighbor6(msg_data, msg_tlen) == -1)
465 goto trunc;
466 } else
467 #endif
468 {
469 if (olsr_print_lq_neighbor4(msg_data, msg_tlen) == -1)
470 goto trunc;
471 }
472 }
473 break;
474
475 case OLSR_MID_MSG:
476 {
477 size_t addr_size = sizeof(struct in_addr);
478
479 #if INET6
480 if (is_ipv6)
481 addr_size = sizeof(struct in6_addr);
482 #endif
483
484 while (msg_tlen >= addr_size) {
485 if (!TTEST2(*msg_data, addr_size))
486 goto trunc;
487
488 printf("\n\t interface address %s",
489 #if INET6
490 is_ipv6 ? ip6addr_string(msg_data) :
491 #endif
492 ipaddr_string(msg_data));
493 msg_data += addr_size;
494 msg_tlen -= addr_size;
495 }
496 break;
497 }
498
499 case OLSR_HNA_MSG:
500 printf("\n\t Advertised networks (total %u)",
501 (unsigned int) (msg_tlen / sizeof(struct olsr_hna6)));
502 #if INET6
503 if (is_ipv6)
504 {
505 int i = 0;
506 while (msg_tlen >= sizeof(struct olsr_hna6)) {
507 struct olsr_hna6 *hna6;
508
509 if (!TTEST2(*msg_data, sizeof(struct olsr_hna6)))
510 goto trunc;
511
512 hna6 = (struct olsr_hna6 *)msg_data;
513
514 printf("\n\t #%i: %s/%u",
515 i, ip6addr_string(hna6->network),
516 mask62plen (hna6->mask));
517
518 msg_data += sizeof(struct olsr_hna6);
519 msg_tlen -= sizeof(struct olsr_hna6);
520 }
521 }
522 else
523 #endif
524 {
525 int col = 0;
526 while (msg_tlen >= sizeof(struct olsr_hna4)) {
527 if (!TTEST2(*msg_data, sizeof(struct olsr_hna4)))
528 goto trunc;
529
530 ptr.hna = (struct olsr_hna4 *)msg_data;
531
532 /* print 4 prefixes per line */
533 if (col == 0)
534 printf ("\n\t ");
535 else
536 printf (", ");
537
538 printf("%s/%u",
539 ipaddr_string(ptr.hna->network),
540 mask2plen(EXTRACT_32BITS(ptr.hna->mask)));
541
542 msg_data += sizeof(struct olsr_hna4);
543 msg_tlen -= sizeof(struct olsr_hna4);
544
545 col = (col + 1) % 4;
546 }
547 }
548 break;
549
550 case OLSR_NAMESERVICE_MSG:
551 {
552 u_int name_entries = EXTRACT_16BITS(msg_data+2);
553 u_int addr_size = 4;
554 int name_entries_valid = 0;
555 u_int i;
556
557 if (is_ipv6)
558 addr_size = 16;
559
560 if ((name_entries > 0)
561 && ((name_entries * (4 + addr_size)) <= msg_tlen))
562 name_entries_valid = 1;
563
564 if (msg_tlen < 4)
565 goto trunc;
566 if (!TTEST2(*msg_data, 4))
567 goto trunc;
568
569 printf("\n\t Version %u, Entries %u%s",
570 EXTRACT_16BITS(msg_data),
571 name_entries, (name_entries_valid == 0) ? " (invalid)" : "");
572
573 if (name_entries_valid == 0)
574 break;
575
576 msg_data += 4;
577 msg_tlen -= 4;
578
579 for (i = 0; i < name_entries; i++) {
580 int name_entry_len_valid = 0;
581
582 if (msg_tlen < 4)
583 break;
584 if (!TTEST2(*msg_data, 4))
585 goto trunc;
586
587 name_entry_type = EXTRACT_16BITS(msg_data);
588 name_entry_len = EXTRACT_16BITS(msg_data+2);
589
590 msg_data += 4;
591 msg_tlen -= 4;
592
593 if ((name_entry_len > 0) && ((addr_size + name_entry_len) <= msg_tlen))
594 name_entry_len_valid = 1;
595
596 printf("\n\t #%u: type %#06x, length %u%s",
597 (unsigned int) i, name_entry_type,
598 name_entry_len, (name_entry_len_valid == 0) ? " (invalid)" : "");
599
600 if (name_entry_len_valid == 0)
601 break;
602
603 /* 32-bit alignment */
604 name_entry_padding = 0;
605 if (name_entry_len%4 != 0)
606 name_entry_padding = 4-(name_entry_len%4);
607
608 if (msg_tlen < addr_size + name_entry_len + name_entry_padding)
609 goto trunc;
610
611 if (!TTEST2(*msg_data, addr_size + name_entry_len + name_entry_padding))
612 goto trunc;
613
614 #if INET6
615 if (is_ipv6)
616 printf(", address %s, name \"",
617 ip6addr_string(msg_data));
618 else
619 #endif
620 printf(", address %s, name \"",
621 ipaddr_string(msg_data));
622 fn_printn(msg_data + addr_size, name_entry_len, NULL);
623 printf("\"");
624
625 msg_data += addr_size + name_entry_len + name_entry_padding;
626 msg_tlen -= addr_size + name_entry_len + name_entry_padding;
627 } /* for (i = 0; i < name_entries; i++) */
628 break;
629 } /* case OLSR_NAMESERVICE_MSG */
630
631 /*
632 * FIXME those are the defined messages that lack a decoder
633 * you are welcome to contribute code ;-)
634 */
635 case OLSR_POWERINFO_MSG:
636 default:
637 print_unknown_data(msg_data, "\n\t ", msg_tlen);
638 break;
639 } /* switch (msg_type) */
640 tptr += msg_len;
641 } /* while (tptr < (pptr+length)) */
642
643 return;
644
645 trunc:
646 printf("[|olsr]");
647 }
648
649 /*
650 * Local Variables:
651 * c-style: whitesmith
652 * c-basic-offset: 4
653 * End:
654 */