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>
28 #include "interface.h"
29 #include "addrtoname.h"
33 * RFC 3626 common header
36 * 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
37 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
38 * | Packet Length | Packet Sequence Number |
39 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
40 * | Message Type | Vtime | Message Size |
41 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
42 * | Originator Address |
43 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
44 * | Time To Live | Hop Count | Message Sequence Number |
45 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
49 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
50 * | Message Type | Vtime | Message Size |
51 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
52 * | Originator Address |
53 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
54 * | Time To Live | Hop Count | Message Sequence Number |
55 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
59 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
64 uint8_t packet_len
[2];
65 uint8_t packet_seq
[2];
68 #define OLSR_HELLO_MSG 1 /* rfc3626 */
69 #define OLSR_TC_MSG 2 /* rfc3626 */
70 #define OLSR_MID_MSG 3 /* rfc3626 */
71 #define OLSR_HNA_MSG 4 /* rfc3626 */
72 #define OLSR_POWERINFO_MSG 128
73 #define OLSR_NAMESERVICE_MSG 130
74 #define OLSR_HELLO_LQ_MSG 201 /* LQ extensions olsr.org */
75 #define OLSR_TC_LQ_MSG 202 /* LQ extensions olsr.org */
77 static const struct tok olsr_msg_values
[] = {
78 { OLSR_HELLO_MSG
, "Hello" },
79 { OLSR_TC_MSG
, "TC" },
80 { OLSR_MID_MSG
, "MID" },
81 { OLSR_HNA_MSG
, "HNA" },
82 { OLSR_POWERINFO_MSG
, "Powerinfo" },
83 { OLSR_NAMESERVICE_MSG
, "Nameservice" },
84 { OLSR_HELLO_LQ_MSG
, "Hello-LQ" },
85 { OLSR_TC_LQ_MSG
, "TC-LQ" },
93 uint8_t originator
[4];
103 uint8_t originator
[16];
115 struct olsr_hello_link
{
137 #define OLSR_EXTRACT_LINK_TYPE(link_code) (link_code & 0x3)
138 #define OLSR_EXTRACT_NEIGHBOR_TYPE(link_code) (link_code >> 2)
140 static const struct tok olsr_link_type_values
[] = {
141 { 0, "Unspecified" },
148 static const struct tok olsr_neighbor_type_values
[] = {
149 { 0, "Not-Neighbor" },
151 { 2, "Symmetric-MPR" },
155 struct olsr_lq_neighbor4
{
157 uint8_t link_quality
;
158 uint8_t neighbor_link_quality
;
162 struct olsr_lq_neighbor6
{
163 uint8_t neighbor
[16];
164 uint8_t link_quality
;
165 uint8_t neighbor_link_quality
;
170 * macro to convert the 8-bit mantissa/exponent to a double float
171 * taken from olsr.org.
173 #define VTIME_SCALE_FACTOR 0.0625
174 #define ME_TO_DOUBLE(me) \
175 (double)(VTIME_SCALE_FACTOR*(1+(double)(me>>4)/16)*(double)(1<<(me&0x0F)))
178 * print a neighbor list with LQ extensions.
181 olsr_print_lq_neighbor4(netdissect_options
*ndo
,
182 const u_char
*msg_data
, u_int hello_len
)
184 const struct olsr_lq_neighbor4
*lq_neighbor
;
186 while (hello_len
>= sizeof(struct olsr_lq_neighbor4
)) {
188 lq_neighbor
= (const struct olsr_lq_neighbor4
*)msg_data
;
189 if (!ND_TTEST(*lq_neighbor
))
192 ND_PRINT((ndo
, "\n\t neighbor %s, link-quality %.2lf%%"
193 ", neighbor-link-quality %.2lf%%",
194 ipaddr_string(ndo
, lq_neighbor
->neighbor
),
195 ((double)lq_neighbor
->link_quality
/2.55),
196 ((double)lq_neighbor
->neighbor_link_quality
/2.55)));
198 msg_data
+= sizeof(struct olsr_lq_neighbor4
);
199 hello_len
-= sizeof(struct olsr_lq_neighbor4
);
206 olsr_print_lq_neighbor6(netdissect_options
*ndo
,
207 const u_char
*msg_data
, u_int hello_len
)
209 const struct olsr_lq_neighbor6
*lq_neighbor
;
211 while (hello_len
>= sizeof(struct olsr_lq_neighbor6
)) {
213 lq_neighbor
= (const struct olsr_lq_neighbor6
*)msg_data
;
214 if (!ND_TTEST(*lq_neighbor
))
217 ND_PRINT((ndo
, "\n\t neighbor %s, link-quality %.2lf%%"
218 ", neighbor-link-quality %.2lf%%",
219 ip6addr_string(ndo
, lq_neighbor
->neighbor
),
220 ((double)lq_neighbor
->link_quality
/2.55),
221 ((double)lq_neighbor
->neighbor_link_quality
/2.55)));
223 msg_data
+= sizeof(struct olsr_lq_neighbor6
);
224 hello_len
-= sizeof(struct olsr_lq_neighbor6
);
231 * print a neighbor list.
234 olsr_print_neighbor(netdissect_options
*ndo
,
235 const u_char
*msg_data
, u_int hello_len
)
239 ND_PRINT((ndo
, "\n\t neighbor\n\t\t"));
242 while (hello_len
>= sizeof(struct in_addr
)) {
244 if (!ND_TTEST2(*msg_data
, sizeof(struct in_addr
)))
246 /* print 4 neighbors per line */
248 ND_PRINT((ndo
, "%s%s", ipaddr_string(ndo
, msg_data
),
249 neighbor
% 4 == 0 ? "\n\t\t" : " "));
251 msg_data
+= sizeof(struct in_addr
);
252 hello_len
-= sizeof(struct in_addr
);
259 olsr_print(netdissect_options
*ndo
,
260 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 uint16_t name_entry_type
, name_entry_len
;
274 u_int name_entry_padding
;
275 uint8_t link_type
, neighbor_type
;
276 const u_char
*tptr
, *msg_data
;
280 if (length
< sizeof(struct olsr_common
)) {
284 ND_TCHECK2(*tptr
, sizeof(struct olsr_common
));
286 ptr
.common
= (const struct olsr_common
*)tptr
;
287 length
= min(length
, EXTRACT_16BITS(ptr
.common
->packet_len
));
289 ND_PRINT((ndo
, "OLSRv%i, seq 0x%04x, length %u",
290 (is_ipv6
== 0) ? 4 : 6,
291 EXTRACT_16BITS(ptr
.common
->packet_seq
),
294 tptr
+= sizeof(struct olsr_common
);
297 * In non-verbose mode, just print version.
299 if (ndo
->ndo_vflag
< 1) {
303 while (tptr
< (pptr
+length
)) {
306 const struct olsr_msg4
*v4
;
307 const struct olsr_msg6
*v6
;
309 int msg_len_valid
= 0;
311 ND_TCHECK2(*tptr
, sizeof(struct olsr_msg4
));
316 msgptr
.v6
= (const struct olsr_msg6
*) tptr
;
317 msg_type
= msgptr
.v6
->msg_type
;
318 msg_len
= EXTRACT_16BITS(msgptr
.v6
->msg_len
);
319 if ((msg_len
>= sizeof (struct olsr_msg6
))
320 && (msg_len
<= length
))
323 /* infinite loop check */
324 if (msg_type
== 0 || msg_len
== 0) {
328 ND_PRINT((ndo
, "\n\t%s Message (%#04x), originator %s, ttl %u, hop %u"
329 "\n\t vtime %.3lfs, msg-seq 0x%04x, length %u%s",
330 tok2str(olsr_msg_values
, "Unknown", msg_type
),
331 msg_type
, ip6addr_string(ndo
, msgptr
.v6
->originator
),
334 ME_TO_DOUBLE(msgptr
.v6
->vtime
),
335 EXTRACT_16BITS(msgptr
.v6
->msg_seq
),
336 msg_len
, (msg_len_valid
== 0) ? " (invalid)" : ""));
337 if (!msg_len_valid
) {
341 msg_tlen
= msg_len
- sizeof(struct olsr_msg6
);
342 msg_data
= tptr
+ sizeof(struct olsr_msg6
);
344 else /* (!is_ipv6) */
347 msgptr
.v4
= (const struct olsr_msg4
*) tptr
;
348 msg_type
= msgptr
.v4
->msg_type
;
349 msg_len
= EXTRACT_16BITS(msgptr
.v4
->msg_len
);
350 if ((msg_len
>= sizeof (struct olsr_msg4
))
351 && (msg_len
<= length
))
354 /* infinite loop check */
355 if (msg_type
== 0 || msg_len
== 0) {
359 ND_PRINT((ndo
, "\n\t%s Message (%#04x), originator %s, ttl %u, hop %u"
360 "\n\t vtime %.3lfs, msg-seq 0x%04x, length %u%s",
361 tok2str(olsr_msg_values
, "Unknown", msg_type
),
362 msg_type
, ipaddr_string(ndo
, msgptr
.v4
->originator
),
365 ME_TO_DOUBLE(msgptr
.v4
->vtime
),
366 EXTRACT_16BITS(msgptr
.v4
->msg_seq
),
367 msg_len
, (msg_len_valid
== 0) ? " (invalid)" : ""));
368 if (!msg_len_valid
) {
372 msg_tlen
= msg_len
- sizeof(struct olsr_msg4
);
373 msg_data
= tptr
+ sizeof(struct olsr_msg4
);
378 case OLSR_HELLO_LQ_MSG
:
379 if (msg_tlen
< sizeof(struct olsr_hello
))
381 ND_TCHECK2(*msg_data
, sizeof(struct olsr_hello
));
383 ptr
.hello
= (const struct olsr_hello
*)msg_data
;
384 ND_PRINT((ndo
, "\n\t hello-time %.3lfs, MPR willingness %u",
385 ME_TO_DOUBLE(ptr
.hello
->htime
), ptr
.hello
->will
));
386 msg_data
+= sizeof(struct olsr_hello
);
387 msg_tlen
-= sizeof(struct olsr_hello
);
389 while (msg_tlen
>= sizeof(struct olsr_hello_link
)) {
390 int hello_len_valid
= 0;
395 ND_TCHECK2(*msg_data
, sizeof(struct olsr_hello_link
));
397 ptr
.hello_link
= (const struct olsr_hello_link
*)msg_data
;
399 hello_len
= EXTRACT_16BITS(ptr
.hello_link
->len
);
400 link_type
= OLSR_EXTRACT_LINK_TYPE(ptr
.hello_link
->link_code
);
401 neighbor_type
= OLSR_EXTRACT_NEIGHBOR_TYPE(ptr
.hello_link
->link_code
);
403 if ((hello_len
<= msg_tlen
)
404 && (hello_len
>= sizeof(struct olsr_hello_link
)))
407 ND_PRINT((ndo
, "\n\t link-type %s, neighbor-type %s, len %u%s",
408 tok2str(olsr_link_type_values
, "Unknown", link_type
),
409 tok2str(olsr_neighbor_type_values
, "Unknown", neighbor_type
),
411 (hello_len_valid
== 0) ? " (invalid)" : ""));
413 if (hello_len_valid
== 0)
416 msg_data
+= sizeof(struct olsr_hello_link
);
417 msg_tlen
-= sizeof(struct olsr_hello_link
);
418 hello_len
-= sizeof(struct olsr_hello_link
);
420 ND_TCHECK2(*msg_data
, hello_len
);
421 if (msg_type
== OLSR_HELLO_MSG
) {
422 if (olsr_print_neighbor(ndo
, msg_data
, hello_len
) == -1)
427 if (olsr_print_lq_neighbor6(ndo
, msg_data
, hello_len
) == -1)
432 if (olsr_print_lq_neighbor4(ndo
, msg_data
, hello_len
) == -1)
437 msg_data
+= hello_len
;
438 msg_tlen
-= hello_len
;
444 if (msg_tlen
< sizeof(struct olsr_tc
))
446 ND_TCHECK2(*msg_data
, sizeof(struct olsr_tc
));
448 ptr
.tc
= (const struct olsr_tc
*)msg_data
;
449 ND_PRINT((ndo
, "\n\t advertised neighbor seq 0x%04x",
450 EXTRACT_16BITS(ptr
.tc
->ans_seq
)));
451 msg_data
+= sizeof(struct olsr_tc
);
452 msg_tlen
-= sizeof(struct olsr_tc
);
454 if (msg_type
== OLSR_TC_MSG
) {
455 if (olsr_print_neighbor(ndo
, msg_data
, msg_tlen
) == -1)
460 if (olsr_print_lq_neighbor6(ndo
, msg_data
, msg_tlen
) == -1)
465 if (olsr_print_lq_neighbor4(ndo
, msg_data
, msg_tlen
) == -1)
473 size_t addr_size
= sizeof(struct in_addr
);
477 addr_size
= sizeof(struct in6_addr
);
480 while (msg_tlen
>= addr_size
) {
481 ND_TCHECK2(*msg_data
, addr_size
);
483 ND_PRINT((ndo
, "\n\t interface address %s",
484 is_ipv6
? ip6addr_string(ndo
, msg_data
) :
485 ipaddr_string(ndo
, msg_data
)));
487 ND_PRINT((ndo
, "\n\t interface address %s",
488 ipaddr_string(ndo
, msg_data
)));
491 msg_data
+= addr_size
;
492 msg_tlen
-= addr_size
;
498 ND_PRINT((ndo
, "\n\t Advertised networks (total %u)",
499 (unsigned int) (msg_tlen
/ sizeof(struct olsr_hna6
))));
504 while (msg_tlen
>= sizeof(struct olsr_hna6
)) {
505 const struct olsr_hna6
*hna6
;
507 ND_TCHECK2(*msg_data
, sizeof(struct olsr_hna6
));
509 hna6
= (const struct olsr_hna6
*)msg_data
;
511 ND_PRINT((ndo
, "\n\t #%i: %s/%u",
512 i
, ip6addr_string(ndo
, hna6
->network
),
513 mask62plen (hna6
->mask
)));
515 msg_data
+= sizeof(struct olsr_hna6
);
516 msg_tlen
-= sizeof(struct olsr_hna6
);
523 while (msg_tlen
>= sizeof(struct olsr_hna4
)) {
524 ND_TCHECK2(*msg_data
, sizeof(struct olsr_hna4
));
526 ptr
.hna
= (const struct olsr_hna4
*)msg_data
;
528 /* print 4 prefixes per line */
529 ND_PRINT((ndo
, "%s%s/%u",
530 col
== 0 ? "\n\t " : ", ",
531 ipaddr_string(ndo
, ptr
.hna
->network
),
532 mask2plen(EXTRACT_32BITS(ptr
.hna
->mask
))));
534 msg_data
+= sizeof(struct olsr_hna4
);
535 msg_tlen
-= sizeof(struct olsr_hna4
);
542 case OLSR_NAMESERVICE_MSG
:
544 u_int name_entries
= EXTRACT_16BITS(msg_data
+2);
546 int name_entries_valid
= 0;
552 if ((name_entries
> 0)
553 && ((name_entries
* (4 + addr_size
)) <= msg_tlen
))
554 name_entries_valid
= 1;
558 ND_TCHECK2(*msg_data
, 4);
560 ND_PRINT((ndo
, "\n\t Version %u, Entries %u%s",
561 EXTRACT_16BITS(msg_data
),
562 name_entries
, (name_entries_valid
== 0) ? " (invalid)" : ""));
564 if (name_entries_valid
== 0)
570 for (i
= 0; i
< name_entries
; i
++) {
571 int name_entry_len_valid
= 0;
575 ND_TCHECK2(*msg_data
, 4);
577 name_entry_type
= EXTRACT_16BITS(msg_data
);
578 name_entry_len
= EXTRACT_16BITS(msg_data
+2);
583 if ((name_entry_len
> 0) && ((addr_size
+ name_entry_len
) <= msg_tlen
))
584 name_entry_len_valid
= 1;
586 ND_PRINT((ndo
, "\n\t #%u: type %#06x, length %u%s",
587 (unsigned int) i
, name_entry_type
,
588 name_entry_len
, (name_entry_len_valid
== 0) ? " (invalid)" : ""));
590 if (name_entry_len_valid
== 0)
593 /* 32-bit alignment */
594 name_entry_padding
= 0;
595 if (name_entry_len
%4 != 0)
596 name_entry_padding
= 4-(name_entry_len
%4);
598 if (msg_tlen
< addr_size
+ name_entry_len
+ name_entry_padding
)
601 ND_TCHECK2(*msg_data
, addr_size
+ name_entry_len
+ name_entry_padding
);
605 ND_PRINT((ndo
, ", address %s, name \"",
606 ip6addr_string(ndo
, msg_data
)));
609 ND_PRINT((ndo
, ", address %s, name \"",
610 ipaddr_string(ndo
, msg_data
)));
611 (void)fn_printn(ndo
, msg_data
+ addr_size
, name_entry_len
, NULL
);
612 ND_PRINT((ndo
, "\""));
614 msg_data
+= addr_size
+ name_entry_len
+ name_entry_padding
;
615 msg_tlen
-= addr_size
+ name_entry_len
+ name_entry_padding
;
616 } /* for (i = 0; i < name_entries; i++) */
618 } /* case OLSR_NAMESERVICE_MSG */
621 * FIXME those are the defined messages that lack a decoder
622 * you are welcome to contribute code ;-)
624 case OLSR_POWERINFO_MSG
:
626 print_unknown_data(ndo
, msg_data
, "\n\t ", msg_tlen
);
628 } /* switch (msg_type) */
630 } /* while (tptr < (pptr+length)) */
635 ND_PRINT((ndo
, "[|olsr]"));
640 * c-style: whitesmith