2 * Copyright (c) 1998-2007 The TCPDUMP project
3 * Copyright (c) 2009 Florian Forster
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.
16 * Optimized Link State Protocl (OLSR) as per rfc3626
18 * Original code by Hannes Gredler <hannes@juniper.net>
19 * IPv6 additions by Florian Forster <octo at verplant.org>
26 #include <tcpdump-stdinc.h>
31 #include "interface.h"
32 #include "addrtoname.h"
37 * RFC 3626 common header
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 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
53 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
54 * | Message Type | Vtime | Message Size |
55 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
56 * | Originator Address |
57 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
58 * | Time To Live | Hop Count | Message Sequence Number |
59 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
63 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
68 u_int8_t packet_len
[2];
69 u_int8_t packet_seq
[2];
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 */
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" },
97 u_int8_t originator
[4];
107 u_int8_t originator
[16];
119 struct olsr_hello_link
{
136 u_int8_t network
[16];
141 #define OLSR_EXTRACT_LINK_TYPE(link_code) (link_code & 0x3)
142 #define OLSR_EXTRACT_NEIGHBOR_TYPE(link_code) (link_code >> 2)
144 static struct tok olsr_link_type_values
[] = {
145 { 0, "Unspecified" },
152 static struct tok olsr_neighbor_type_values
[] = {
153 { 0, "Not-Neighbor" },
155 { 2, "Symmetric-MPR" },
159 struct olsr_lq_neighbor4
{
160 u_int8_t neighbor
[4];
161 u_int8_t link_quality
;
162 u_int8_t neighbor_link_quality
;
166 struct olsr_lq_neighbor6
{
167 u_int8_t neighbor
[16];
168 u_int8_t link_quality
;
169 u_int8_t neighbor_link_quality
;
174 * macro to convert the 8-bit mantissa/exponent to a double float
175 * taken from olsr.org.
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)))
182 * print a neighbor list with LQ extensions.
185 olsr_print_lq_neighbor4 (const u_char
*msg_data
, u_int hello_len
)
187 struct olsr_lq_neighbor4
*lq_neighbor
;
189 while (hello_len
>= sizeof(struct olsr_lq_neighbor4
)) {
191 lq_neighbor
= (struct olsr_lq_neighbor4
*)msg_data
;
192 if (!TTEST(*lq_neighbor
))
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));
201 msg_data
+= sizeof(struct olsr_lq_neighbor4
);
202 hello_len
-= sizeof(struct olsr_lq_neighbor4
);
209 olsr_print_lq_neighbor6 (const u_char
*msg_data
, u_int hello_len
)
211 struct olsr_lq_neighbor6
*lq_neighbor
;
213 while (hello_len
>= sizeof(struct olsr_lq_neighbor6
)) {
215 lq_neighbor
= (struct olsr_lq_neighbor6
*)msg_data
;
216 if (!TTEST(*lq_neighbor
))
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));
225 msg_data
+= sizeof(struct olsr_lq_neighbor6
);
226 hello_len
-= sizeof(struct olsr_lq_neighbor6
);
233 * print a neighbor list.
236 olsr_print_neighbor (const u_char
*msg_data
, u_int hello_len
)
240 printf("\n\t neighbor\n\t\t");
243 while (hello_len
>= sizeof(struct in_addr
)) {
245 if (!TTEST2(*msg_data
, sizeof(struct in_addr
)))
247 /* print 4 neighbors per line */
249 printf("%s%s", ipaddr_string(msg_data
),
250 neighbor
% 4 == 0 ? "\n\t\t" : " ");
252 msg_data
+= sizeof(struct in_addr
);
253 hello_len
-= sizeof(struct in_addr
);
260 olsr_print (const u_char
*pptr
, u_int length
, int is_ipv6
)
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
;
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
;
280 if (length
< sizeof(struct olsr_common
)) {
284 if (!TTEST2(*tptr
, sizeof(struct olsr_common
))) {
288 ptr
.common
= (struct olsr_common
*)tptr
;
289 length
= MIN(length
, EXTRACT_16BITS(ptr
.common
->packet_len
));
291 printf("OLSRv%i, seq 0x%04x, length %u",
292 (is_ipv6
== 0) ? 4 : 6,
293 EXTRACT_16BITS(ptr
.common
->packet_seq
),
296 tptr
+= sizeof(struct olsr_common
);
299 * In non-verbose mode, just print version.
305 while (tptr
< (pptr
+length
)) {
308 struct olsr_msg4
*v4
;
309 struct olsr_msg6
*v6
;
311 int msg_len_valid
= 0;
313 if (!TTEST2(*tptr
, sizeof(struct olsr_msg4
)))
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
))
326 /* infinite loop check */
327 if (msg_type
== 0 || msg_len
== 0) {
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
),
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
) {
344 msg_tlen
= msg_len
- sizeof(struct olsr_msg6
);
345 msg_data
= tptr
+ sizeof(struct olsr_msg6
);
347 else /* (!is_ipv6) */
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
))
357 /* infinite loop check */
358 if (msg_type
== 0 || msg_len
== 0) {
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
),
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
) {
375 msg_tlen
= msg_len
- sizeof(struct olsr_msg4
);
376 msg_data
= tptr
+ sizeof(struct olsr_msg4
);
381 case OLSR_HELLO_LQ_MSG
:
382 if (msg_tlen
< sizeof(struct olsr_hello
))
384 TCHECK2(*msg_data
, sizeof(struct olsr_hello
));
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
);
392 while (msg_tlen
>= sizeof(struct olsr_hello_link
)) {
393 int hello_len_valid
= 0;
398 if (!TTEST2(*msg_data
, sizeof(struct olsr_hello_link
)))
401 ptr
.hello_link
= (struct olsr_hello_link
*)msg_data
;
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
);
407 if ((hello_len
<= msg_tlen
)
408 && (hello_len
>= sizeof(struct olsr_hello_link
)))
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
),
415 (hello_len_valid
== 0) ? " (invalid)" : "");
417 if (hello_len_valid
== 0)
420 msg_data
+= sizeof(struct olsr_hello_link
);
421 msg_tlen
-= sizeof(struct olsr_hello_link
);
422 hello_len
-= sizeof(struct olsr_hello_link
);
424 TCHECK2(*msg_data
, hello_len
);
425 if (msg_type
== OLSR_HELLO_MSG
) {
426 if (olsr_print_neighbor(msg_data
, hello_len
) == -1)
431 if (olsr_print_lq_neighbor6(msg_data
, hello_len
) == -1)
436 if (olsr_print_lq_neighbor4(msg_data
, hello_len
) == -1)
441 msg_data
+= hello_len
;
442 msg_tlen
-= hello_len
;
448 if (msg_tlen
< sizeof(struct olsr_tc
))
450 TCHECK2(*msg_data
, sizeof(struct olsr_tc
));
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
);
458 if (msg_type
== OLSR_TC_MSG
) {
459 if (olsr_print_neighbor(msg_data
, msg_tlen
) == -1)
464 if (olsr_print_lq_neighbor6(msg_data
, msg_tlen
) == -1)
469 if (olsr_print_lq_neighbor4(msg_data
, msg_tlen
) == -1)
477 size_t addr_size
= sizeof(struct in_addr
);
481 addr_size
= sizeof(struct in6_addr
);
484 while (msg_tlen
>= addr_size
) {
485 if (!TTEST2(*msg_data
, addr_size
))
488 printf("\n\t interface address %s",
490 is_ipv6
? ip6addr_string(msg_data
) :
492 ipaddr_string(msg_data
));
493 msg_data
+= addr_size
;
494 msg_tlen
-= addr_size
;
500 printf("\n\t Advertised networks (total %u)",
501 (unsigned int) (msg_tlen
/ sizeof(struct olsr_hna6
)));
506 while (msg_tlen
>= sizeof(struct olsr_hna6
)) {
507 struct olsr_hna6
*hna6
;
509 if (!TTEST2(*msg_data
, sizeof(struct olsr_hna6
)))
512 hna6
= (struct olsr_hna6
*)msg_data
;
514 printf("\n\t #%i: %s/%u",
515 i
, ip6addr_string(hna6
->network
),
516 mask62plen (hna6
->mask
));
518 msg_data
+= sizeof(struct olsr_hna6
);
519 msg_tlen
-= sizeof(struct olsr_hna6
);
526 while (msg_tlen
>= sizeof(struct olsr_hna4
)) {
527 if (!TTEST2(*msg_data
, sizeof(struct olsr_hna4
)))
530 ptr
.hna
= (struct olsr_hna4
*)msg_data
;
532 /* print 4 prefixes per line */
539 ipaddr_string(ptr
.hna
->network
),
540 mask2plen(EXTRACT_32BITS(ptr
.hna
->mask
)));
542 msg_data
+= sizeof(struct olsr_hna4
);
543 msg_tlen
-= sizeof(struct olsr_hna4
);
550 case OLSR_NAMESERVICE_MSG
:
552 u_int name_entries
= EXTRACT_16BITS(msg_data
+2);
554 int name_entries_valid
= 0;
560 if ((name_entries
> 0)
561 && ((name_entries
* (4 + addr_size
)) <= msg_tlen
))
562 name_entries_valid
= 1;
566 if (!TTEST2(*msg_data
, 4))
569 printf("\n\t Version %u, Entries %u%s",
570 EXTRACT_16BITS(msg_data
),
571 name_entries
, (name_entries_valid
== 0) ? " (invalid)" : "");
573 if (name_entries_valid
== 0)
579 for (i
= 0; i
< name_entries
; i
++) {
580 int name_entry_len_valid
= 0;
584 if (!TTEST2(*msg_data
, 4))
587 name_entry_type
= EXTRACT_16BITS(msg_data
);
588 name_entry_len
= EXTRACT_16BITS(msg_data
+2);
593 if ((name_entry_len
> 0) && ((addr_size
+ name_entry_len
) <= msg_tlen
))
594 name_entry_len_valid
= 1;
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)" : "");
600 if (name_entry_len_valid
== 0)
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);
608 if (msg_tlen
< addr_size
+ name_entry_len
+ name_entry_padding
)
611 if (!TTEST2(*msg_data
, addr_size
+ name_entry_len
+ name_entry_padding
))
616 printf(", address %s, name \"",
617 ip6addr_string(msg_data
));
620 printf(", address %s, name \"",
621 ipaddr_string(msg_data
));
622 fn_printn(msg_data
+ addr_size
, name_entry_len
, NULL
);
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++) */
629 } /* case OLSR_NAMESERVICE_MSG */
632 * FIXME those are the defined messages that lack a decoder
633 * you are welcome to contribute code ;-)
635 case OLSR_POWERINFO_MSG
:
637 print_unknown_data(msg_data
, "\n\t ", msg_tlen
);
639 } /* switch (msg_type) */
641 } /* while (tptr < (pptr+length)) */
651 * c-style: whitesmith