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>
22 #define NETDISSECT_REWORKED
27 #include <tcpdump-stdinc.h>
29 #include "interface.h"
30 #include "addrtoname.h"
34 * RFC 3626 common header
37 * 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
38 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
39 * | Packet Length | Packet Sequence Number |
40 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
41 * | Message Type | Vtime | Message Size |
42 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
43 * | Originator Address |
44 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
45 * | Time To Live | Hop Count | Message Sequence Number |
46 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
50 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
51 * | Message Type | Vtime | Message Size |
52 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
53 * | Originator Address |
54 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
55 * | Time To Live | Hop Count | Message Sequence Number |
56 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
60 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
65 uint8_t packet_len
[2];
66 uint8_t packet_seq
[2];
69 #define OLSR_HELLO_MSG 1 /* rfc3626 */
70 #define OLSR_TC_MSG 2 /* rfc3626 */
71 #define OLSR_MID_MSG 3 /* rfc3626 */
72 #define OLSR_HNA_MSG 4 /* rfc3626 */
73 #define OLSR_POWERINFO_MSG 128
74 #define OLSR_NAMESERVICE_MSG 130
75 #define OLSR_HELLO_LQ_MSG 201 /* LQ extensions olsr.org */
76 #define OLSR_TC_LQ_MSG 202 /* LQ extensions olsr.org */
78 static const struct tok olsr_msg_values
[] = {
79 { OLSR_HELLO_MSG
, "Hello" },
80 { OLSR_TC_MSG
, "TC" },
81 { OLSR_MID_MSG
, "MID" },
82 { OLSR_HNA_MSG
, "HNA" },
83 { OLSR_POWERINFO_MSG
, "Powerinfo" },
84 { OLSR_NAMESERVICE_MSG
, "Nameservice" },
85 { OLSR_HELLO_LQ_MSG
, "Hello-LQ" },
86 { OLSR_TC_LQ_MSG
, "TC-LQ" },
94 uint8_t originator
[4];
104 uint8_t originator
[16];
116 struct olsr_hello_link
{
138 #define OLSR_EXTRACT_LINK_TYPE(link_code) (link_code & 0x3)
139 #define OLSR_EXTRACT_NEIGHBOR_TYPE(link_code) (link_code >> 2)
141 static const struct tok olsr_link_type_values
[] = {
142 { 0, "Unspecified" },
149 static const struct tok olsr_neighbor_type_values
[] = {
150 { 0, "Not-Neighbor" },
152 { 2, "Symmetric-MPR" },
156 struct olsr_lq_neighbor4
{
158 uint8_t link_quality
;
159 uint8_t neighbor_link_quality
;
163 struct olsr_lq_neighbor6
{
164 uint8_t neighbor
[16];
165 uint8_t link_quality
;
166 uint8_t neighbor_link_quality
;
171 * macro to convert the 8-bit mantissa/exponent to a double float
172 * taken from olsr.org.
174 #define VTIME_SCALE_FACTOR 0.0625
175 #define ME_TO_DOUBLE(me) \
176 (double)(VTIME_SCALE_FACTOR*(1+(double)(me>>4)/16)*(double)(1<<(me&0x0F)))
179 * print a neighbor list with LQ extensions.
182 olsr_print_lq_neighbor4(netdissect_options
*ndo
,
183 const u_char
*msg_data
, u_int hello_len
)
185 struct olsr_lq_neighbor4
*lq_neighbor
;
187 while (hello_len
>= sizeof(struct olsr_lq_neighbor4
)) {
189 lq_neighbor
= (struct olsr_lq_neighbor4
*)msg_data
;
190 if (!ND_TTEST(*lq_neighbor
))
193 ND_PRINT((ndo
, "\n\t neighbor %s, link-quality %.2lf%%"
194 ", neighbor-link-quality %.2lf%%",
195 ipaddr_string(ndo
, lq_neighbor
->neighbor
),
196 ((double)lq_neighbor
->link_quality
/2.55),
197 ((double)lq_neighbor
->neighbor_link_quality
/2.55)));
199 msg_data
+= sizeof(struct olsr_lq_neighbor4
);
200 hello_len
-= sizeof(struct olsr_lq_neighbor4
);
207 olsr_print_lq_neighbor6(netdissect_options
*ndo
,
208 const u_char
*msg_data
, u_int hello_len
)
210 struct olsr_lq_neighbor6
*lq_neighbor
;
212 while (hello_len
>= sizeof(struct olsr_lq_neighbor6
)) {
214 lq_neighbor
= (struct olsr_lq_neighbor6
*)msg_data
;
215 if (!ND_TTEST(*lq_neighbor
))
218 ND_PRINT((ndo
, "\n\t neighbor %s, link-quality %.2lf%%"
219 ", neighbor-link-quality %.2lf%%",
220 ip6addr_string(ndo
, lq_neighbor
->neighbor
),
221 ((double)lq_neighbor
->link_quality
/2.55),
222 ((double)lq_neighbor
->neighbor_link_quality
/2.55)));
224 msg_data
+= sizeof(struct olsr_lq_neighbor6
);
225 hello_len
-= sizeof(struct olsr_lq_neighbor6
);
232 * print a neighbor list.
235 olsr_print_neighbor(netdissect_options
*ndo
,
236 const u_char
*msg_data
, u_int hello_len
)
240 ND_PRINT((ndo
, "\n\t neighbor\n\t\t"));
243 while (hello_len
>= sizeof(struct in_addr
)) {
245 if (!ND_TTEST2(*msg_data
, sizeof(struct in_addr
)))
247 /* print 4 neighbors per line */
249 ND_PRINT((ndo
, "%s%s", ipaddr_string(ndo
, 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(netdissect_options
*ndo
,
261 const u_char
*pptr
, u_int length
, int is_ipv6
)
264 const struct olsr_common
*common
;
265 const struct olsr_msg4
*msg4
;
266 const struct olsr_msg6
*msg6
;
267 const struct olsr_hello
*hello
;
268 const struct olsr_hello_link
*hello_link
;
269 const struct olsr_tc
*tc
;
270 const struct olsr_hna4
*hna
;
273 u_int msg_type
, msg_len
, msg_tlen
, hello_len
;
274 uint16_t name_entry_type
, name_entry_len
;
275 u_int name_entry_padding
;
276 uint8_t link_type
, neighbor_type
;
277 const u_char
*tptr
, *msg_data
;
281 if (length
< sizeof(struct olsr_common
)) {
285 ND_TCHECK2(*tptr
, sizeof(struct olsr_common
));
287 ptr
.common
= (struct olsr_common
*)tptr
;
288 length
= min(length
, EXTRACT_16BITS(ptr
.common
->packet_len
));
290 ND_PRINT((ndo
, "OLSRv%i, seq 0x%04x, length %u",
291 (is_ipv6
== 0) ? 4 : 6,
292 EXTRACT_16BITS(ptr
.common
->packet_seq
),
295 tptr
+= sizeof(struct olsr_common
);
298 * In non-verbose mode, just print version.
300 if (ndo
->ndo_vflag
< 1) {
304 while (tptr
< (pptr
+length
)) {
307 struct olsr_msg4
*v4
;
308 struct olsr_msg6
*v6
;
310 int msg_len_valid
= 0;
312 ND_TCHECK2(*tptr
, sizeof(struct olsr_msg4
));
317 msgptr
.v6
= (struct olsr_msg6
*) tptr
;
318 msg_type
= msgptr
.v6
->msg_type
;
319 msg_len
= EXTRACT_16BITS(msgptr
.v6
->msg_len
);
320 if ((msg_len
>= sizeof (struct olsr_msg6
))
321 && (msg_len
<= length
))
324 /* infinite loop check */
325 if (msg_type
== 0 || msg_len
== 0) {
329 ND_PRINT((ndo
, "\n\t%s Message (%#04x), originator %s, ttl %u, hop %u"
330 "\n\t vtime %.3lfs, msg-seq 0x%04x, length %u%s",
331 tok2str(olsr_msg_values
, "Unknown", msg_type
),
332 msg_type
, ip6addr_string(ndo
, msgptr
.v6
->originator
),
335 ME_TO_DOUBLE(msgptr
.v6
->vtime
),
336 EXTRACT_16BITS(msgptr
.v6
->msg_seq
),
337 msg_len
, (msg_len_valid
== 0) ? " (invalid)" : ""));
338 if (!msg_len_valid
) {
342 msg_tlen
= msg_len
- sizeof(struct olsr_msg6
);
343 msg_data
= tptr
+ sizeof(struct olsr_msg6
);
345 else /* (!is_ipv6) */
348 msgptr
.v4
= (struct olsr_msg4
*) tptr
;
349 msg_type
= msgptr
.v4
->msg_type
;
350 msg_len
= EXTRACT_16BITS(msgptr
.v4
->msg_len
);
351 if ((msg_len
>= sizeof (struct olsr_msg4
))
352 && (msg_len
<= length
))
355 /* infinite loop check */
356 if (msg_type
== 0 || msg_len
== 0) {
360 ND_PRINT((ndo
, "\n\t%s Message (%#04x), originator %s, ttl %u, hop %u"
361 "\n\t vtime %.3lfs, msg-seq 0x%04x, length %u%s",
362 tok2str(olsr_msg_values
, "Unknown", msg_type
),
363 msg_type
, ipaddr_string(ndo
, msgptr
.v4
->originator
),
366 ME_TO_DOUBLE(msgptr
.v4
->vtime
),
367 EXTRACT_16BITS(msgptr
.v4
->msg_seq
),
368 msg_len
, (msg_len_valid
== 0) ? " (invalid)" : ""));
369 if (!msg_len_valid
) {
373 msg_tlen
= msg_len
- sizeof(struct olsr_msg4
);
374 msg_data
= tptr
+ sizeof(struct olsr_msg4
);
379 case OLSR_HELLO_LQ_MSG
:
380 if (msg_tlen
< sizeof(struct olsr_hello
))
382 ND_TCHECK2(*msg_data
, sizeof(struct olsr_hello
));
384 ptr
.hello
= (struct olsr_hello
*)msg_data
;
385 ND_PRINT((ndo
, "\n\t hello-time %.3lfs, MPR willingness %u",
386 ME_TO_DOUBLE(ptr
.hello
->htime
), ptr
.hello
->will
));
387 msg_data
+= sizeof(struct olsr_hello
);
388 msg_tlen
-= sizeof(struct olsr_hello
);
390 while (msg_tlen
>= sizeof(struct olsr_hello_link
)) {
391 int hello_len_valid
= 0;
396 ND_TCHECK2(*msg_data
, sizeof(struct olsr_hello_link
));
398 ptr
.hello_link
= (struct olsr_hello_link
*)msg_data
;
400 hello_len
= EXTRACT_16BITS(ptr
.hello_link
->len
);
401 link_type
= OLSR_EXTRACT_LINK_TYPE(ptr
.hello_link
->link_code
);
402 neighbor_type
= OLSR_EXTRACT_NEIGHBOR_TYPE(ptr
.hello_link
->link_code
);
404 if ((hello_len
<= msg_tlen
)
405 && (hello_len
>= sizeof(struct olsr_hello_link
)))
408 ND_PRINT((ndo
, "\n\t link-type %s, neighbor-type %s, len %u%s",
409 tok2str(olsr_link_type_values
, "Unknown", link_type
),
410 tok2str(olsr_neighbor_type_values
, "Unknown", neighbor_type
),
412 (hello_len_valid
== 0) ? " (invalid)" : ""));
414 if (hello_len_valid
== 0)
417 msg_data
+= sizeof(struct olsr_hello_link
);
418 msg_tlen
-= sizeof(struct olsr_hello_link
);
419 hello_len
-= sizeof(struct olsr_hello_link
);
421 ND_TCHECK2(*msg_data
, hello_len
);
422 if (msg_type
== OLSR_HELLO_MSG
) {
423 if (olsr_print_neighbor(ndo
, msg_data
, hello_len
) == -1)
428 if (olsr_print_lq_neighbor6(ndo
, msg_data
, hello_len
) == -1)
433 if (olsr_print_lq_neighbor4(ndo
, msg_data
, hello_len
) == -1)
438 msg_data
+= hello_len
;
439 msg_tlen
-= hello_len
;
445 if (msg_tlen
< sizeof(struct olsr_tc
))
447 ND_TCHECK2(*msg_data
, sizeof(struct olsr_tc
));
449 ptr
.tc
= (struct olsr_tc
*)msg_data
;
450 ND_PRINT((ndo
, "\n\t advertised neighbor seq 0x%04x",
451 EXTRACT_16BITS(ptr
.tc
->ans_seq
)));
452 msg_data
+= sizeof(struct olsr_tc
);
453 msg_tlen
-= sizeof(struct olsr_tc
);
455 if (msg_type
== OLSR_TC_MSG
) {
456 if (olsr_print_neighbor(ndo
, msg_data
, msg_tlen
) == -1)
461 if (olsr_print_lq_neighbor6(ndo
, msg_data
, msg_tlen
) == -1)
466 if (olsr_print_lq_neighbor4(ndo
, msg_data
, msg_tlen
) == -1)
474 size_t addr_size
= sizeof(struct in_addr
);
478 addr_size
= sizeof(struct in6_addr
);
481 while (msg_tlen
>= addr_size
) {
482 ND_TCHECK2(*msg_data
, addr_size
);
484 ND_PRINT((ndo
, "\n\t interface address %s",
485 is_ipv6
? ip6addr_string(ndo
, msg_data
) :
486 ipaddr_string(ndo
, msg_data
)));
488 ND_PRINT((ndo
, "\n\t interface address %s",
489 ipaddr_string(ndo
, msg_data
)));
492 msg_data
+= addr_size
;
493 msg_tlen
-= addr_size
;
499 ND_PRINT((ndo
, "\n\t Advertised networks (total %u)",
500 (unsigned int) (msg_tlen
/ sizeof(struct olsr_hna6
))));
505 while (msg_tlen
>= sizeof(struct olsr_hna6
)) {
506 struct olsr_hna6
*hna6
;
508 ND_TCHECK2(*msg_data
, sizeof(struct olsr_hna6
));
510 hna6
= (struct olsr_hna6
*)msg_data
;
512 ND_PRINT((ndo
, "\n\t #%i: %s/%u",
513 i
, ip6addr_string(ndo
, hna6
->network
),
514 mask62plen (hna6
->mask
)));
516 msg_data
+= sizeof(struct olsr_hna6
);
517 msg_tlen
-= sizeof(struct olsr_hna6
);
524 while (msg_tlen
>= sizeof(struct olsr_hna4
)) {
525 ND_TCHECK2(*msg_data
, sizeof(struct olsr_hna4
));
527 ptr
.hna
= (struct olsr_hna4
*)msg_data
;
529 /* print 4 prefixes per line */
530 ND_PRINT((ndo
, "%s%s/%u",
531 col
== 0 ? "\n\t " : ", ",
532 ipaddr_string(ndo
, ptr
.hna
->network
),
533 mask2plen(EXTRACT_32BITS(ptr
.hna
->mask
))));
535 msg_data
+= sizeof(struct olsr_hna4
);
536 msg_tlen
-= sizeof(struct olsr_hna4
);
543 case OLSR_NAMESERVICE_MSG
:
545 u_int name_entries
= EXTRACT_16BITS(msg_data
+2);
547 int name_entries_valid
= 0;
553 if ((name_entries
> 0)
554 && ((name_entries
* (4 + addr_size
)) <= msg_tlen
))
555 name_entries_valid
= 1;
559 ND_TCHECK2(*msg_data
, 4);
561 ND_PRINT((ndo
, "\n\t Version %u, Entries %u%s",
562 EXTRACT_16BITS(msg_data
),
563 name_entries
, (name_entries_valid
== 0) ? " (invalid)" : ""));
565 if (name_entries_valid
== 0)
571 for (i
= 0; i
< name_entries
; i
++) {
572 int name_entry_len_valid
= 0;
576 ND_TCHECK2(*msg_data
, 4);
578 name_entry_type
= EXTRACT_16BITS(msg_data
);
579 name_entry_len
= EXTRACT_16BITS(msg_data
+2);
584 if ((name_entry_len
> 0) && ((addr_size
+ name_entry_len
) <= msg_tlen
))
585 name_entry_len_valid
= 1;
587 ND_PRINT((ndo
, "\n\t #%u: type %#06x, length %u%s",
588 (unsigned int) i
, name_entry_type
,
589 name_entry_len
, (name_entry_len_valid
== 0) ? " (invalid)" : ""));
591 if (name_entry_len_valid
== 0)
594 /* 32-bit alignment */
595 name_entry_padding
= 0;
596 if (name_entry_len
%4 != 0)
597 name_entry_padding
= 4-(name_entry_len
%4);
599 if (msg_tlen
< addr_size
+ name_entry_len
+ name_entry_padding
)
602 ND_TCHECK2(*msg_data
, addr_size
+ name_entry_len
+ name_entry_padding
);
606 ND_PRINT((ndo
, ", address %s, name \"",
607 ip6addr_string(ndo
, msg_data
)));
610 ND_PRINT((ndo
, ", address %s, name \"",
611 ipaddr_string(ndo
, msg_data
)));
612 (void)fn_printn(ndo
, msg_data
+ addr_size
, name_entry_len
, NULL
);
613 ND_PRINT((ndo
, "\""));
615 msg_data
+= addr_size
+ name_entry_len
+ name_entry_padding
;
616 msg_tlen
-= addr_size
+ name_entry_len
+ name_entry_padding
;
617 } /* for (i = 0; i < name_entries; i++) */
619 } /* case OLSR_NAMESERVICE_MSG */
622 * FIXME those are the defined messages that lack a decoder
623 * you are welcome to contribute code ;-)
625 case OLSR_POWERINFO_MSG
:
627 print_unknown_data(ndo
, msg_data
, "\n\t ", msg_tlen
);
629 } /* switch (msg_type) */
631 } /* while (tptr < (pptr+length)) */
636 ND_PRINT((ndo
, "[|olsr]"));
641 * c-style: whitesmith