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 u_int8_t packet_len
[2];
66 u_int8_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 u_int8_t originator
[4];
104 u_int8_t originator
[16];
116 struct olsr_hello_link
{
133 u_int8_t network
[16];
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
{
157 u_int8_t neighbor
[4];
158 u_int8_t link_quality
;
159 u_int8_t neighbor_link_quality
;
163 struct olsr_lq_neighbor6
{
164 u_int8_t neighbor
[16];
165 u_int8_t link_quality
;
166 u_int8_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
;
191 ND_PRINT((ndo
, "\n\t neighbor %s, link-quality %.2lf%%"
192 ", neighbor-link-quality %.2lf%%",
193 ipaddr_string(ndo
, lq_neighbor
->neighbor
),
194 ((double)lq_neighbor
->link_quality
/2.55),
195 ((double)lq_neighbor
->neighbor_link_quality
/2.55)));
197 msg_data
+= sizeof(struct olsr_lq_neighbor4
);
198 hello_len
-= sizeof(struct olsr_lq_neighbor4
);
204 olsr_print_lq_neighbor6(netdissect_options
*ndo
,
205 const u_char
*msg_data
, u_int hello_len
)
207 struct olsr_lq_neighbor6
*lq_neighbor
;
209 while (hello_len
>= sizeof(struct olsr_lq_neighbor6
)) {
211 lq_neighbor
= (struct olsr_lq_neighbor6
*)msg_data
;
213 ND_PRINT((ndo
, "\n\t neighbor %s, link-quality %.2lf%%"
214 ", neighbor-link-quality %.2lf%%",
215 ip6addr_string(ndo
, lq_neighbor
->neighbor
),
216 ((double)lq_neighbor
->link_quality
/2.55),
217 ((double)lq_neighbor
->neighbor_link_quality
/2.55)));
219 msg_data
+= sizeof(struct olsr_lq_neighbor6
);
220 hello_len
-= sizeof(struct olsr_lq_neighbor6
);
226 * print a neighbor list.
229 olsr_print_neighbor(netdissect_options
*ndo
,
230 const u_char
*msg_data
, u_int hello_len
)
234 ND_PRINT((ndo
, "\n\t neighbor\n\t\t"));
237 while (hello_len
>= sizeof(struct in_addr
)) {
239 /* print 4 neighbors per line */
241 ND_PRINT((ndo
, "%s%s", ipaddr_string(ndo
, msg_data
),
242 neighbor
% 4 == 0 ? "\n\t\t" : " "));
244 msg_data
+= sizeof(struct in_addr
);
245 hello_len
-= sizeof(struct in_addr
);
251 olsr_print(netdissect_options
*ndo
,
252 const u_char
*pptr
, u_int length
, int is_ipv6
)
255 const struct olsr_common
*common
;
256 const struct olsr_msg4
*msg4
;
257 const struct olsr_msg6
*msg6
;
258 const struct olsr_hello
*hello
;
259 const struct olsr_hello_link
*hello_link
;
260 const struct olsr_tc
*tc
;
261 const struct olsr_hna4
*hna
;
264 u_int msg_type
, msg_len
, msg_tlen
, hello_len
;
265 u_int16_t name_entry_type
, name_entry_len
;
266 u_int name_entry_padding
;
267 u_int8_t link_type
, neighbor_type
;
268 const u_char
*tptr
, *msg_data
;
272 if (length
< sizeof(struct olsr_common
)) {
276 if (!ND_TTEST2(*tptr
, sizeof(struct olsr_common
))) {
280 ptr
.common
= (struct olsr_common
*)tptr
;
281 length
= min(length
, EXTRACT_16BITS(ptr
.common
->packet_len
));
283 ND_PRINT((ndo
, "OLSRv%i, seq 0x%04x, length %u",
284 (is_ipv6
== 0) ? 4 : 6,
285 EXTRACT_16BITS(ptr
.common
->packet_seq
),
288 tptr
+= sizeof(struct olsr_common
);
291 * In non-verbose mode, just print version.
293 if (ndo
->ndo_vflag
< 1) {
297 while (tptr
< (pptr
+length
)) {
300 struct olsr_msg4
*v4
;
301 struct olsr_msg6
*v6
;
303 int msg_len_valid
= 0;
305 if (!ND_TTEST2(*tptr
, sizeof(struct olsr_msg4
)))
311 msgptr
.v6
= (struct olsr_msg6
*) tptr
;
312 msg_type
= msgptr
.v6
->msg_type
;
313 msg_len
= EXTRACT_16BITS(msgptr
.v6
->msg_len
);
314 if ((msg_len
>= sizeof (struct olsr_msg6
))
315 && (msg_len
<= length
))
318 /* infinite loop check */
319 if (msg_type
== 0 || msg_len
== 0) {
323 ND_PRINT((ndo
, "\n\t%s Message (%#04x), originator %s, ttl %u, hop %u"
324 "\n\t vtime %.3lfs, msg-seq 0x%04x, length %u%s",
325 tok2str(olsr_msg_values
, "Unknown", msg_type
),
326 msg_type
, ip6addr_string(ndo
, msgptr
.v6
->originator
),
329 ME_TO_DOUBLE(msgptr
.v6
->vtime
),
330 EXTRACT_16BITS(msgptr
.v6
->msg_seq
),
331 msg_len
, (msg_len_valid
== 0) ? " (invalid)" : ""));
333 msg_tlen
= msg_len
- sizeof(struct olsr_msg6
);
334 msg_data
= tptr
+ sizeof(struct olsr_msg6
);
336 else /* (!is_ipv6) */
339 msgptr
.v4
= (struct olsr_msg4
*) tptr
;
340 msg_type
= msgptr
.v4
->msg_type
;
341 msg_len
= EXTRACT_16BITS(msgptr
.v4
->msg_len
);
342 if ((msg_len
>= sizeof (struct olsr_msg4
))
343 && (msg_len
<= length
))
346 /* infinite loop check */
347 if (msg_type
== 0 || msg_len
== 0) {
351 ND_PRINT((ndo
, "\n\t%s Message (%#04x), originator %s, ttl %u, hop %u"
352 "\n\t vtime %.3lfs, msg-seq 0x%04x, length %u%s",
353 tok2str(olsr_msg_values
, "Unknown", msg_type
),
354 msg_type
, ipaddr_string(ndo
, msgptr
.v4
->originator
),
357 ME_TO_DOUBLE(msgptr
.v4
->vtime
),
358 EXTRACT_16BITS(msgptr
.v4
->msg_seq
),
359 msg_len
, (msg_len_valid
== 0) ? " (invalid)" : ""));
361 msg_tlen
= msg_len
- sizeof(struct olsr_msg4
);
362 msg_data
= tptr
+ sizeof(struct olsr_msg4
);
367 case OLSR_HELLO_LQ_MSG
:
368 if (!ND_TTEST2(*msg_data
, sizeof(struct olsr_hello
)))
371 ptr
.hello
= (struct olsr_hello
*)msg_data
;
372 ND_PRINT((ndo
, "\n\t hello-time %.3lfs, MPR willingness %u",
373 ME_TO_DOUBLE(ptr
.hello
->htime
), ptr
.hello
->will
));
374 msg_data
+= sizeof(struct olsr_hello
);
375 msg_tlen
-= sizeof(struct olsr_hello
);
377 while (msg_tlen
>= sizeof(struct olsr_hello_link
)) {
378 int hello_len_valid
= 0;
383 if (!ND_TTEST2(*msg_data
, sizeof(struct olsr_hello_link
)))
386 ptr
.hello_link
= (struct olsr_hello_link
*)msg_data
;
388 hello_len
= EXTRACT_16BITS(ptr
.hello_link
->len
);
389 link_type
= OLSR_EXTRACT_LINK_TYPE(ptr
.hello_link
->link_code
);
390 neighbor_type
= OLSR_EXTRACT_NEIGHBOR_TYPE(ptr
.hello_link
->link_code
);
392 if ((hello_len
<= msg_tlen
)
393 && (hello_len
>= sizeof(struct olsr_hello_link
)))
396 ND_PRINT((ndo
, "\n\t link-type %s, neighbor-type %s, len %u%s",
397 tok2str(olsr_link_type_values
, "Unknown", link_type
),
398 tok2str(olsr_neighbor_type_values
, "Unknown", neighbor_type
),
400 (hello_len_valid
== 0) ? " (invalid)" : ""));
402 if (hello_len_valid
== 0)
405 msg_data
+= sizeof(struct olsr_hello_link
);
406 msg_tlen
-= sizeof(struct olsr_hello_link
);
407 hello_len
-= sizeof(struct olsr_hello_link
);
409 if (msg_type
== OLSR_HELLO_MSG
) {
410 olsr_print_neighbor(ndo
, msg_data
, hello_len
);
414 olsr_print_lq_neighbor6(ndo
, msg_data
, hello_len
);
417 olsr_print_lq_neighbor4(ndo
, msg_data
, hello_len
);
420 msg_data
+= hello_len
;
421 msg_tlen
-= hello_len
;
427 if (!ND_TTEST2(*msg_data
, sizeof(struct olsr_tc
)))
430 ptr
.tc
= (struct olsr_tc
*)msg_data
;
431 ND_PRINT((ndo
, "\n\t advertised neighbor seq 0x%04x",
432 EXTRACT_16BITS(ptr
.tc
->ans_seq
)));
433 msg_data
+= sizeof(struct olsr_tc
);
434 msg_tlen
-= sizeof(struct olsr_tc
);
436 if (msg_type
== OLSR_TC_MSG
) {
437 olsr_print_neighbor(ndo
, msg_data
, msg_tlen
);
441 olsr_print_lq_neighbor6(ndo
, msg_data
, msg_tlen
);
444 olsr_print_lq_neighbor4(ndo
, msg_data
, msg_tlen
);
450 size_t addr_size
= sizeof(struct in_addr
);
454 addr_size
= sizeof(struct in6_addr
);
457 while (msg_tlen
>= addr_size
) {
458 if (!ND_TTEST2(*msg_data
, addr_size
))
461 ND_PRINT((ndo
, "\n\t interface address %s",
462 is_ipv6
? ip6addr_string(ndo
, msg_data
) :
463 ipaddr_string(ndo
, msg_data
)));
465 ND_PRINT((ndo
, "\n\t interface address %s",
466 ipaddr_string(ndo
, msg_data
)));
469 msg_data
+= addr_size
;
470 msg_tlen
-= addr_size
;
476 ND_PRINT((ndo
, "\n\t Advertised networks (total %u)",
477 (unsigned int) (msg_tlen
/ sizeof(struct olsr_hna6
))));
482 while (msg_tlen
>= sizeof(struct olsr_hna6
)) {
483 struct olsr_hna6
*hna6
;
485 if (!ND_TTEST2(*msg_data
, sizeof(struct olsr_hna6
)))
488 hna6
= (struct olsr_hna6
*)msg_data
;
490 ND_PRINT((ndo
, "\n\t #%i: %s/%u",
491 i
, ip6addr_string(ndo
, hna6
->network
),
492 mask62plen (hna6
->mask
)));
494 msg_data
+= sizeof(struct olsr_hna6
);
495 msg_tlen
-= sizeof(struct olsr_hna6
);
502 while (msg_tlen
>= sizeof(struct olsr_hna4
)) {
503 if (!ND_TTEST2(*msg_data
, sizeof(struct olsr_hna4
)))
506 ptr
.hna
= (struct olsr_hna4
*)msg_data
;
508 /* print 4 prefixes per line */
509 ND_PRINT((ndo
, "%s%s/%u",
510 col
== 0 ? "\n\t " : ", ",
511 ipaddr_string(ndo
, ptr
.hna
->network
),
512 mask2plen(EXTRACT_32BITS(ptr
.hna
->mask
))));
514 msg_data
+= sizeof(struct olsr_hna4
);
515 msg_tlen
-= sizeof(struct olsr_hna4
);
522 case OLSR_NAMESERVICE_MSG
:
524 u_int name_entries
= EXTRACT_16BITS(msg_data
+2);
526 int name_entries_valid
= 0;
532 if ((name_entries
> 0)
533 && ((name_entries
* (4 + addr_size
)) <= msg_tlen
))
534 name_entries_valid
= 1;
538 if (!ND_TTEST2(*msg_data
, 4))
541 ND_PRINT((ndo
, "\n\t Version %u, Entries %u%s",
542 EXTRACT_16BITS(msg_data
),
543 name_entries
, (name_entries_valid
== 0) ? " (invalid)" : ""));
545 if (name_entries_valid
== 0)
551 for (i
= 0; i
< name_entries
; i
++) {
552 int name_entry_len_valid
= 0;
556 if (!ND_TTEST2(*msg_data
, 4))
559 name_entry_type
= EXTRACT_16BITS(msg_data
);
560 name_entry_len
= EXTRACT_16BITS(msg_data
+2);
565 if ((name_entry_len
> 0) && ((addr_size
+ name_entry_len
) <= msg_tlen
))
566 name_entry_len_valid
= 1;
568 ND_PRINT((ndo
, "\n\t #%u: type %#06x, length %u%s",
569 (unsigned int) i
, name_entry_type
,
570 name_entry_len
, (name_entry_len_valid
== 0) ? " (invalid)" : ""));
572 if (name_entry_len_valid
== 0)
575 /* 32-bit alignment */
576 name_entry_padding
= 0;
577 if (name_entry_len
%4 != 0)
578 name_entry_padding
= 4-(name_entry_len
%4);
580 if (msg_tlen
< addr_size
+ name_entry_len
+ name_entry_padding
)
583 if (!ND_TTEST2(*msg_data
, addr_size
+ name_entry_len
+ name_entry_padding
))
588 ND_PRINT((ndo
, ", address %s, name \"",
589 ip6addr_string(ndo
, msg_data
)));
592 ND_PRINT((ndo
, ", address %s, name \"",
593 ipaddr_string(ndo
, msg_data
)));
594 fn_printn(msg_data
+ addr_size
, name_entry_len
, NULL
);
595 ND_PRINT((ndo
, "\""));
597 msg_data
+= addr_size
+ name_entry_len
+ name_entry_padding
;
598 msg_tlen
-= addr_size
+ name_entry_len
+ name_entry_padding
;
599 } /* for (i = 0; i < name_entries; i++) */
601 } /* case OLSR_NAMESERVICE_MSG */
604 * FIXME those are the defined messages that lack a decoder
605 * you are welcome to contribute code ;-)
607 case OLSR_POWERINFO_MSG
:
609 print_unknown_data(ndo
, msg_data
, "\n\t ", msg_tlen
);
611 } /* switch (msg_type) */
613 } /* while (tptr < (pptr+length)) */
618 ND_PRINT((ndo
, "[|olsr]"));
623 * c-style: whitesmith