4 * Copyright (c) 1998-2007 The TCPDUMP project
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that: (1) source code
8 * distributions retain the above copyright notice and this paragraph
9 * in its entirety, and (2) distributions including binary code include
10 * the above copyright notice and this paragraph in its entirety in
11 * the documentation or other materials provided with the distribution.
12 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND
13 * WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT
14 * LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
15 * FOR A PARTICULAR PURPOSE.
17 * Optimized Link State Protocl (OLSR) as per rfc3626
19 * Original code by Hannes Gredler <hannes@juniper.net>
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];
109 struct olsr_hello_link
{
126 #define OLSR_EXTRACT_LINK_TYPE(link_code) (link_code & 0x3)
127 #define OLSR_EXTRACT_NEIGHBOR_TYPE(link_code) (link_code >> 2)
129 static struct tok olsr_link_type_values
[] = {
130 { 0, "Unspecified" },
137 static struct tok olsr_neighbor_type_values
[] = {
138 { 0, "Not-Neighbor" },
140 { 2, "Symmetric-MPR" },
144 struct olsr_lq_neighbor
{
145 u_int8_t neighbor
[4];
146 u_int8_t link_quality
;
147 u_int8_t neighbor_link_quality
;
152 * macro to convert the 8-bit mantissa/exponent to a double float
153 * taken from olsr.org.
155 #define VTIME_SCALE_FACTOR 0.0625
156 #define ME_TO_DOUBLE(me) \
157 (double)(VTIME_SCALE_FACTOR*(1+(double)(me>>4)/16)*(double)(1<<(me&0x0F)))
160 * print a neighbor list with LQ extensions.
163 olsr_print_lq_neighbor (const u_char
*msg_data
, u_int hello_len
)
165 struct olsr_lq_neighbor
*lq_neighbor
;
167 while (hello_len
>= sizeof(struct olsr_lq_neighbor
)) {
169 lq_neighbor
= (struct olsr_lq_neighbor
*)msg_data
;
171 printf("\n\t neighbor %s, link-quality %.2lf%%"
172 ", neighbor-link-quality %.2lf%%",
173 ipaddr_string(lq_neighbor
->neighbor
),
174 ((double)lq_neighbor
->link_quality
/2.55),
175 ((double)lq_neighbor
->neighbor_link_quality
/2.55));
177 msg_data
+= sizeof(struct olsr_lq_neighbor
);
178 hello_len
-= sizeof(struct olsr_lq_neighbor
);
183 * print a neighbor list.
186 olsr_print_neighbor (const u_char
*msg_data
, u_int hello_len
)
190 printf("\n\t neighbor\n\t\t");
193 while (hello_len
>= sizeof(struct in_addr
)) {
195 /* print 4 neighbors per line */
197 printf("%s%s", ipaddr_string(msg_data
),
198 neighbor
% 4 == 0 ? "\n\t\t" : " ");
200 msg_data
+= sizeof(struct in_addr
);
201 hello_len
-= sizeof(struct in_addr
);
207 olsr_print (const u_char
*pptr
, u_int length
)
210 const struct olsr_common
*common
;
211 const struct olsr_msg
*msg
;
212 const struct olsr_hello
*hello
;
213 const struct olsr_hello_link
*hello_link
;
214 const struct olsr_lq_neighbor
*lq_neighbor
;
215 const struct olsr_tc
*tc
;
216 const struct olsr_hna
*hna
;
219 u_int msg_type
, msg_len
, msg_tlen
, hello_len
, prefix
;
220 u_int8_t link_type
, neighbor_type
;
221 const u_char
*tptr
, *msg_data
;
225 if (length
< sizeof(struct olsr_common
)) {
229 if (!TTEST2(*tptr
, sizeof(struct olsr_common
))) {
233 ptr
.common
= (struct olsr_common
*)tptr
;
234 length
= MIN(length
, EXTRACT_16BITS(ptr
.common
->packet_len
));
236 printf("OLSR, seq 0x%04x, length %u",
237 EXTRACT_16BITS(ptr
.common
->packet_seq
),
240 tptr
+= sizeof(struct olsr_common
);
243 * In non-verbose mode, just print version.
249 while (tptr
< (pptr
+length
)) {
251 if (!TTEST2(*tptr
, sizeof(struct olsr_msg
)))
254 ptr
.msg
= (struct olsr_msg
*)tptr
;
256 msg_type
= ptr
.msg
->msg_type
;
257 msg_len
= EXTRACT_16BITS(ptr
.msg
->msg_len
);
259 /* infinite loop check */
260 if (msg_type
== 0 || msg_len
== 0) {
264 printf("\n\t%s Message (%u), originator %s, ttl %u, hop %u"
265 "\n\t vtime %.3lfs, msg-seq 0x%04x, length %u",
266 tok2str(olsr_msg_values
, "Unknown", msg_type
),
267 msg_type
, ipaddr_string(ptr
.msg
->originator
),
270 ME_TO_DOUBLE(ptr
.msg
->vtime
),
271 EXTRACT_16BITS(ptr
.msg
->msg_seq
),
274 msg_tlen
= msg_len
- sizeof(struct olsr_msg
);
275 msg_data
= tptr
+ sizeof(struct olsr_msg
);
279 case OLSR_HELLO_LQ_MSG
:
280 if (!TTEST2(*msg_data
, sizeof(struct olsr_hello
)))
283 ptr
.hello
= (struct olsr_hello
*)msg_data
;
284 printf("\n\t hello-time %.3lfs, MPR willingness %u",
285 ME_TO_DOUBLE(ptr
.hello
->htime
), ptr
.hello
->will
);
286 msg_data
+= sizeof(struct olsr_hello
);
287 msg_tlen
-= sizeof(struct olsr_hello
);
289 while (msg_tlen
>= sizeof(struct olsr_hello_link
)) {
294 if (!TTEST2(*msg_data
, sizeof(struct olsr_hello_link
)))
297 ptr
.hello_link
= (struct olsr_hello_link
*)msg_data
;
299 hello_len
= EXTRACT_16BITS(ptr
.hello_link
->len
);
300 link_type
= OLSR_EXTRACT_LINK_TYPE(ptr
.hello_link
->link_code
);
301 neighbor_type
= OLSR_EXTRACT_NEIGHBOR_TYPE(ptr
.hello_link
->link_code
);
303 printf("\n\t link-type %s, neighbor-type %s, len %u",
304 tok2str(olsr_link_type_values
, "Unknown", link_type
),
305 tok2str(olsr_neighbor_type_values
, "Unknown", neighbor_type
),
308 msg_data
+= sizeof(struct olsr_hello_link
);
309 msg_tlen
-= sizeof(struct olsr_hello_link
);
310 hello_len
-= sizeof(struct olsr_hello_link
);
312 if (msg_type
== OLSR_HELLO_MSG
) {
313 olsr_print_neighbor(msg_data
, hello_len
);
315 olsr_print_lq_neighbor(msg_data
, hello_len
);
318 msg_data
+= hello_len
;
319 msg_tlen
-= hello_len
;
325 if (!TTEST2(*msg_data
, sizeof(struct olsr_tc
)))
328 ptr
.tc
= (struct olsr_tc
*)msg_data
;
329 printf("\n\t advertised neighbor seq 0x%04x",
330 EXTRACT_16BITS(ptr
.tc
->ans_seq
));
331 msg_data
+= sizeof(struct olsr_tc
);
332 msg_tlen
-= sizeof(struct olsr_tc
);
334 if (msg_type
== OLSR_TC_MSG
) {
335 olsr_print_neighbor(msg_data
, msg_tlen
);
337 olsr_print_lq_neighbor(msg_data
, msg_tlen
);
342 if (!TTEST2(*msg_data
, sizeof(struct in_addr
)))
345 while (msg_tlen
>= sizeof(struct in_addr
)) {
346 printf("\n\t interface address %s", ipaddr_string(msg_data
));
347 msg_data
+= sizeof(struct in_addr
);
348 msg_tlen
-= sizeof(struct in_addr
);
354 printf("\n\t advertised networks\n\t ");
355 while (msg_tlen
>= sizeof(struct olsr_hna
)) {
356 if (!TTEST2(*msg_data
, sizeof(struct olsr_hna
)))
359 ptr
.hna
= (struct olsr_hna
*)msg_data
;
361 /* print 4 prefixes per line */
364 ipaddr_string(ptr
.hna
->network
),
365 mask2plen(EXTRACT_32BITS(ptr
.hna
->mask
)),
366 prefix
% 4 == 0 ? "\n\t " : " ");
368 msg_data
+= sizeof(struct olsr_hna
);
369 msg_tlen
-= sizeof(struct olsr_hna
);
375 * FIXME those are the defined messages that lack a decoder
376 * you are welcome to contribute code ;-)
379 case OLSR_POWERINFO_MSG
:
380 case OLSR_NAMESERVICE_MSG
:
382 print_unknown_data(msg_data
, "\n\t ", msg_tlen
);
396 * c-style: whitesmith