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
;
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 uint16_t name_entry_type
, name_entry_len
;
266 u_int name_entry_padding
;
267 uint8_t link_type
, neighbor_type
;
268 const u_char
*tptr
, *msg_data
;
272 if (length
< sizeof(struct olsr_common
)) {
276 ND_TCHECK2(*tptr
, sizeof(struct olsr_common
));
278 ptr
.common
= (struct olsr_common
*)tptr
;
279 length
= min(length
, EXTRACT_16BITS(ptr
.common
->packet_len
));
281 ND_PRINT((ndo
, "OLSRv%i, seq 0x%04x, length %u",
282 (is_ipv6
== 0) ? 4 : 6,
283 EXTRACT_16BITS(ptr
.common
->packet_seq
),
286 tptr
+= sizeof(struct olsr_common
);
289 * In non-verbose mode, just print version.
291 if (ndo
->ndo_vflag
< 1) {
295 while (tptr
< (pptr
+length
)) {
298 struct olsr_msg4
*v4
;
299 struct olsr_msg6
*v6
;
301 int msg_len_valid
= 0;
303 ND_TCHECK2(*tptr
, sizeof(struct olsr_msg4
));
308 msgptr
.v6
= (struct olsr_msg6
*) tptr
;
309 msg_type
= msgptr
.v6
->msg_type
;
310 msg_len
= EXTRACT_16BITS(msgptr
.v6
->msg_len
);
311 if ((msg_len
>= sizeof (struct olsr_msg6
))
312 && (msg_len
<= length
))
315 /* infinite loop check */
316 if (msg_type
== 0 || msg_len
== 0) {
320 ND_PRINT((ndo
, "\n\t%s Message (%#04x), originator %s, ttl %u, hop %u"
321 "\n\t vtime %.3lfs, msg-seq 0x%04x, length %u%s",
322 tok2str(olsr_msg_values
, "Unknown", msg_type
),
323 msg_type
, ip6addr_string(ndo
, msgptr
.v6
->originator
),
326 ME_TO_DOUBLE(msgptr
.v6
->vtime
),
327 EXTRACT_16BITS(msgptr
.v6
->msg_seq
),
328 msg_len
, (msg_len_valid
== 0) ? " (invalid)" : ""));
330 msg_tlen
= msg_len
- sizeof(struct olsr_msg6
);
331 msg_data
= tptr
+ sizeof(struct olsr_msg6
);
333 else /* (!is_ipv6) */
336 msgptr
.v4
= (struct olsr_msg4
*) tptr
;
337 msg_type
= msgptr
.v4
->msg_type
;
338 msg_len
= EXTRACT_16BITS(msgptr
.v4
->msg_len
);
339 if ((msg_len
>= sizeof (struct olsr_msg4
))
340 && (msg_len
<= length
))
343 /* infinite loop check */
344 if (msg_type
== 0 || msg_len
== 0) {
348 ND_PRINT((ndo
, "\n\t%s Message (%#04x), originator %s, ttl %u, hop %u"
349 "\n\t vtime %.3lfs, msg-seq 0x%04x, length %u%s",
350 tok2str(olsr_msg_values
, "Unknown", msg_type
),
351 msg_type
, ipaddr_string(ndo
, msgptr
.v4
->originator
),
354 ME_TO_DOUBLE(msgptr
.v4
->vtime
),
355 EXTRACT_16BITS(msgptr
.v4
->msg_seq
),
356 msg_len
, (msg_len_valid
== 0) ? " (invalid)" : ""));
358 msg_tlen
= msg_len
- sizeof(struct olsr_msg4
);
359 msg_data
= tptr
+ sizeof(struct olsr_msg4
);
364 case OLSR_HELLO_LQ_MSG
:
365 ND_TCHECK2(*msg_data
, sizeof(struct olsr_hello
));
367 ptr
.hello
= (struct olsr_hello
*)msg_data
;
368 ND_PRINT((ndo
, "\n\t hello-time %.3lfs, MPR willingness %u",
369 ME_TO_DOUBLE(ptr
.hello
->htime
), ptr
.hello
->will
));
370 msg_data
+= sizeof(struct olsr_hello
);
371 msg_tlen
-= sizeof(struct olsr_hello
);
373 while (msg_tlen
>= sizeof(struct olsr_hello_link
)) {
374 int hello_len_valid
= 0;
379 ND_TCHECK2(*msg_data
, sizeof(struct olsr_hello_link
));
381 ptr
.hello_link
= (struct olsr_hello_link
*)msg_data
;
383 hello_len
= EXTRACT_16BITS(ptr
.hello_link
->len
);
384 link_type
= OLSR_EXTRACT_LINK_TYPE(ptr
.hello_link
->link_code
);
385 neighbor_type
= OLSR_EXTRACT_NEIGHBOR_TYPE(ptr
.hello_link
->link_code
);
387 if ((hello_len
<= msg_tlen
)
388 && (hello_len
>= sizeof(struct olsr_hello_link
)))
391 ND_PRINT((ndo
, "\n\t link-type %s, neighbor-type %s, len %u%s",
392 tok2str(olsr_link_type_values
, "Unknown", link_type
),
393 tok2str(olsr_neighbor_type_values
, "Unknown", neighbor_type
),
395 (hello_len_valid
== 0) ? " (invalid)" : ""));
397 if (hello_len_valid
== 0)
400 msg_data
+= sizeof(struct olsr_hello_link
);
401 msg_tlen
-= sizeof(struct olsr_hello_link
);
402 hello_len
-= sizeof(struct olsr_hello_link
);
404 if (msg_type
== OLSR_HELLO_MSG
) {
405 olsr_print_neighbor(ndo
, msg_data
, hello_len
);
409 olsr_print_lq_neighbor6(ndo
, msg_data
, hello_len
);
412 olsr_print_lq_neighbor4(ndo
, msg_data
, hello_len
);
415 msg_data
+= hello_len
;
416 msg_tlen
-= hello_len
;
422 ND_TCHECK2(*msg_data
, sizeof(struct olsr_tc
));
424 ptr
.tc
= (struct olsr_tc
*)msg_data
;
425 ND_PRINT((ndo
, "\n\t advertised neighbor seq 0x%04x",
426 EXTRACT_16BITS(ptr
.tc
->ans_seq
)));
427 msg_data
+= sizeof(struct olsr_tc
);
428 msg_tlen
-= sizeof(struct olsr_tc
);
430 if (msg_type
== OLSR_TC_MSG
) {
431 olsr_print_neighbor(ndo
, msg_data
, msg_tlen
);
435 olsr_print_lq_neighbor6(ndo
, msg_data
, msg_tlen
);
438 olsr_print_lq_neighbor4(ndo
, msg_data
, msg_tlen
);
444 size_t addr_size
= sizeof(struct in_addr
);
448 addr_size
= sizeof(struct in6_addr
);
451 while (msg_tlen
>= addr_size
) {
452 ND_TCHECK2(*msg_data
, addr_size
);
454 ND_PRINT((ndo
, "\n\t interface address %s",
455 is_ipv6
? ip6addr_string(ndo
, msg_data
) :
456 ipaddr_string(ndo
, msg_data
)));
458 ND_PRINT((ndo
, "\n\t interface address %s",
459 ipaddr_string(ndo
, msg_data
)));
462 msg_data
+= addr_size
;
463 msg_tlen
-= addr_size
;
469 ND_PRINT((ndo
, "\n\t Advertised networks (total %u)",
470 (unsigned int) (msg_tlen
/ sizeof(struct olsr_hna6
))));
475 while (msg_tlen
>= sizeof(struct olsr_hna6
)) {
476 struct olsr_hna6
*hna6
;
478 ND_TCHECK2(*msg_data
, sizeof(struct olsr_hna6
));
480 hna6
= (struct olsr_hna6
*)msg_data
;
482 ND_PRINT((ndo
, "\n\t #%i: %s/%u",
483 i
, ip6addr_string(ndo
, hna6
->network
),
484 mask62plen (hna6
->mask
)));
486 msg_data
+= sizeof(struct olsr_hna6
);
487 msg_tlen
-= sizeof(struct olsr_hna6
);
494 while (msg_tlen
>= sizeof(struct olsr_hna4
)) {
495 ND_TCHECK2(*msg_data
, sizeof(struct olsr_hna4
));
497 ptr
.hna
= (struct olsr_hna4
*)msg_data
;
499 /* print 4 prefixes per line */
500 ND_PRINT((ndo
, "%s%s/%u",
501 col
== 0 ? "\n\t " : ", ",
502 ipaddr_string(ndo
, ptr
.hna
->network
),
503 mask2plen(EXTRACT_32BITS(ptr
.hna
->mask
))));
505 msg_data
+= sizeof(struct olsr_hna4
);
506 msg_tlen
-= sizeof(struct olsr_hna4
);
513 case OLSR_NAMESERVICE_MSG
:
515 u_int name_entries
= EXTRACT_16BITS(msg_data
+2);
517 int name_entries_valid
= 0;
523 if ((name_entries
> 0)
524 && ((name_entries
* (4 + addr_size
)) <= msg_tlen
))
525 name_entries_valid
= 1;
529 ND_TCHECK2(*msg_data
, 4);
531 ND_PRINT((ndo
, "\n\t Version %u, Entries %u%s",
532 EXTRACT_16BITS(msg_data
),
533 name_entries
, (name_entries_valid
== 0) ? " (invalid)" : ""));
535 if (name_entries_valid
== 0)
541 for (i
= 0; i
< name_entries
; i
++) {
542 int name_entry_len_valid
= 0;
546 ND_TCHECK2(*msg_data
, 4);
548 name_entry_type
= EXTRACT_16BITS(msg_data
);
549 name_entry_len
= EXTRACT_16BITS(msg_data
+2);
554 if ((name_entry_len
> 0) && ((addr_size
+ name_entry_len
) <= msg_tlen
))
555 name_entry_len_valid
= 1;
557 ND_PRINT((ndo
, "\n\t #%u: type %#06x, length %u%s",
558 (unsigned int) i
, name_entry_type
,
559 name_entry_len
, (name_entry_len_valid
== 0) ? " (invalid)" : ""));
561 if (name_entry_len_valid
== 0)
564 /* 32-bit alignment */
565 name_entry_padding
= 0;
566 if (name_entry_len
%4 != 0)
567 name_entry_padding
= 4-(name_entry_len
%4);
569 if (msg_tlen
< addr_size
+ name_entry_len
+ name_entry_padding
)
572 ND_TCHECK2(*msg_data
, addr_size
+ name_entry_len
+ name_entry_padding
);
576 ND_PRINT((ndo
, ", address %s, name \"",
577 ip6addr_string(ndo
, msg_data
)));
580 ND_PRINT((ndo
, ", address %s, name \"",
581 ipaddr_string(ndo
, msg_data
)));
582 fn_printn(ndo
, msg_data
+ addr_size
, name_entry_len
, NULL
);
583 ND_PRINT((ndo
, "\""));
585 msg_data
+= addr_size
+ name_entry_len
+ name_entry_padding
;
586 msg_tlen
-= addr_size
+ name_entry_len
+ name_entry_padding
;
587 } /* for (i = 0; i < name_entries; i++) */
589 } /* case OLSR_NAMESERVICE_MSG */
592 * FIXME those are the defined messages that lack a decoder
593 * you are welcome to contribute code ;-)
595 case OLSR_POWERINFO_MSG
:
597 print_unknown_data(ndo
, msg_data
, "\n\t ", msg_tlen
);
599 } /* switch (msg_type) */
601 } /* while (tptr < (pptr+length)) */
606 ND_PRINT((ndo
, "[|olsr]"));
611 * c-style: whitesmith