2 * Copyright (c) 1998-2007 The TCPDUMP project
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that: (1) source code
6 * distributions retain the above copyright notice and this paragraph
7 * in its entirety, and (2) distributions including binary code include
8 * the above copyright notice and this paragraph in its entirety in
9 * the documentation or other materials provided with the distribution.
10 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND
11 * WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT
12 * LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
13 * FOR A PARTICULAR PURPOSE.
15 * Optimized Link State Protocl (OLSR) as per rfc3626
17 * Original code by Hannes Gredler <hannes@juniper.net>
24 #include <tcpdump-stdinc.h>
29 #include "interface.h"
30 #include "addrtoname.h"
35 * RFC 3626 common header
38 * 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
39 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
40 * | Packet Length | Packet Sequence Number |
41 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
42 * | Message Type | Vtime | Message Size |
43 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
44 * | Originator Address |
45 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
46 * | Time To Live | Hop Count | Message Sequence Number |
47 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
51 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
52 * | Message Type | Vtime | Message Size |
53 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
54 * | Originator Address |
55 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
56 * | Time To Live | Hop Count | Message Sequence Number |
57 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
61 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
66 u_int8_t packet_len[2];
67 u_int8_t packet_seq[2];
70 #define OLSR_HELLO_MSG 1 /* rfc3626 */
71 #define OLSR_TC_MSG 2 /* rfc3626 */
72 #define OLSR_MID_MSG 3 /* rfc3626 */
73 #define OLSR_HNA_MSG 4 /* rfc3626 */
74 #define OLSR_POWERINFO_MSG 128
75 #define OLSR_NAMESERVICE_MSG 130
76 #define OLSR_HELLO_LQ_MSG 201 /* LQ extensions olsr.org */
77 #define OLSR_TC_LQ_MSG 202 /* LQ extensions olsr.org */
79 static struct tok olsr_msg_values[] = {
80 { OLSR_HELLO_MSG, "Hello" },
81 { OLSR_TC_MSG, "TC" },
82 { OLSR_MID_MSG, "MID" },
83 { OLSR_HNA_MSG, "HNA" },
84 { OLSR_POWERINFO_MSG, "Powerinfo" },
85 { OLSR_NAMESERVICE_MSG, "Nameservice" },
86 { OLSR_HELLO_LQ_MSG, "Hello-LQ" },
87 { OLSR_TC_LQ_MSG, "TC-LQ" },
95 u_int8_t originator[4];
107 struct olsr_hello_link {
124 #define OLSR_EXTRACT_LINK_TYPE(link_code) (link_code & 0x3)
125 #define OLSR_EXTRACT_NEIGHBOR_TYPE(link_code) (link_code >> 2)
127 static struct tok olsr_link_type_values[] = {
128 { 0, "Unspecified" },
135 static struct tok olsr_neighbor_type_values[] = {
136 { 0, "Not-Neighbor" },
138 { 2, "Symmetric-MPR" },
142 struct olsr_lq_neighbor {
143 u_int8_t neighbor[4];
144 u_int8_t link_quality;
145 u_int8_t neighbor_link_quality;
150 * macro to convert the 8-bit mantissa/exponent to a double float
151 * taken from olsr.org.
153 #define VTIME_SCALE_FACTOR 0.0625
154 #define ME_TO_DOUBLE(me) \
155 (double)(VTIME_SCALE_FACTOR*(1+(double)(me>>4)/16)*(double)(1<<(me&0x0F)))
158 * print a neighbor list with LQ extensions.
161 olsr_print_lq_neighbor (const u_char *msg_data, u_int hello_len)
163 struct olsr_lq_neighbor *lq_neighbor;
165 while (hello_len >= sizeof(struct olsr_lq_neighbor)) {
167 lq_neighbor = (struct olsr_lq_neighbor *)msg_data;
169 printf("\n\t neighbor %s, link-quality %.2lf%%"
170 ", neighbor-link-quality %.2lf%%",
171 ipaddr_string(lq_neighbor->neighbor),
172 ((double)lq_neighbor->link_quality/2.55),
173 ((double)lq_neighbor->neighbor_link_quality/2.55));
175 msg_data += sizeof(struct olsr_lq_neighbor);
176 hello_len -= sizeof(struct olsr_lq_neighbor);
181 * print a neighbor list.
184 olsr_print_neighbor (const u_char *msg_data, u_int hello_len)
188 printf("\n\t neighbor\n\t\t");
191 while (hello_len >= sizeof(struct in_addr)) {
193 /* print 4 neighbors per line */
195 printf("%s%s", ipaddr_string(msg_data),
196 neighbor % 4 == 0 ? "\n\t\t" : " ");
198 msg_data += sizeof(struct in_addr);
199 hello_len -= sizeof(struct in_addr);
205 olsr_print (const u_char *pptr, u_int length)
208 const struct olsr_common *common;
209 const struct olsr_msg *msg;
210 const struct olsr_hello *hello;
211 const struct olsr_hello_link *hello_link;
212 const struct olsr_lq_neighbor *lq_neighbor;
213 const struct olsr_tc *tc;
214 const struct olsr_hna *hna;
217 u_int msg_type, msg_len, msg_tlen, hello_len, prefix;
218 u_int8_t link_type, neighbor_type;
219 const u_char *tptr, *msg_data;
223 if (length < sizeof(struct olsr_common)) {
227 if (!TTEST2(*tptr, sizeof(struct olsr_common))) {
231 ptr.common = (struct olsr_common *)tptr;
232 length = MIN(length, EXTRACT_16BITS(ptr.common->packet_len));
234 printf("OLSR, seq 0x%04x, length %u",
235 EXTRACT_16BITS(ptr.common->packet_seq),
238 tptr += sizeof(struct olsr_common);
241 * In non-verbose mode, just print version.
247 while (tptr < (pptr+length)) {
249 if (!TTEST2(*tptr, sizeof(struct olsr_msg)))
252 ptr.msg = (struct olsr_msg *)tptr;
254 msg_type = ptr.msg->msg_type;
255 msg_len = EXTRACT_16BITS(ptr.msg->msg_len);
257 /* infinite loop check */
258 if (msg_type == 0 || msg_len == 0) {
262 printf("\n\t%s Message (%u), originator %s, ttl %u, hop %u"
263 "\n\t vtime %.3lfs, msg-seq 0x%04x, length %u",
264 tok2str(olsr_msg_values, "Unknown", msg_type),
265 msg_type, ipaddr_string(ptr.msg->originator),
268 ME_TO_DOUBLE(ptr.msg->vtime),
269 EXTRACT_16BITS(ptr.msg->msg_seq),
272 msg_tlen = msg_len - sizeof(struct olsr_msg);
273 msg_data = tptr + sizeof(struct olsr_msg);
277 case OLSR_HELLO_LQ_MSG:
278 if (!TTEST2(*msg_data, sizeof(struct olsr_hello)))
281 ptr.hello = (struct olsr_hello *)msg_data;
282 printf("\n\t hello-time %.3lfs, MPR willingness %u",
283 ME_TO_DOUBLE(ptr.hello->htime), ptr.hello->will);
284 msg_data += sizeof(struct olsr_hello);
285 msg_tlen -= sizeof(struct olsr_hello);
287 while (msg_tlen >= sizeof(struct olsr_hello_link)) {
292 if (!TTEST2(*msg_data, sizeof(struct olsr_hello_link)))
295 ptr.hello_link = (struct olsr_hello_link *)msg_data;
297 hello_len = EXTRACT_16BITS(ptr.hello_link->len);
298 link_type = OLSR_EXTRACT_LINK_TYPE(ptr.hello_link->link_code);
299 neighbor_type = OLSR_EXTRACT_NEIGHBOR_TYPE(ptr.hello_link->link_code);
301 printf("\n\t link-type %s, neighbor-type %s, len %u",
302 tok2str(olsr_link_type_values, "Unknown", link_type),
303 tok2str(olsr_neighbor_type_values, "Unknown", neighbor_type),
306 msg_data += sizeof(struct olsr_hello_link);
307 msg_tlen -= sizeof(struct olsr_hello_link);
308 hello_len -= sizeof(struct olsr_hello_link);
310 if (msg_type == OLSR_HELLO_MSG) {
311 olsr_print_neighbor(msg_data, hello_len);
313 olsr_print_lq_neighbor(msg_data, hello_len);
316 msg_data += hello_len;
317 msg_tlen -= hello_len;
323 if (!TTEST2(*msg_data, sizeof(struct olsr_tc)))
326 ptr.tc = (struct olsr_tc *)msg_data;
327 printf("\n\t advertised neighbor seq 0x%04x",
328 EXTRACT_16BITS(ptr.tc->ans_seq));
329 msg_data += sizeof(struct olsr_tc);
330 msg_tlen -= sizeof(struct olsr_tc);
332 if (msg_type == OLSR_TC_MSG) {
333 olsr_print_neighbor(msg_data, msg_tlen);
335 olsr_print_lq_neighbor(msg_data, msg_tlen);
340 if (!TTEST2(*msg_data, sizeof(struct in_addr)))
343 while (msg_tlen >= sizeof(struct in_addr)) {
344 printf("\n\t interface address %s", ipaddr_string(msg_data));
345 msg_data += sizeof(struct in_addr);
346 msg_tlen -= sizeof(struct in_addr);
352 printf("\n\t advertised networks\n\t ");
353 while (msg_tlen >= sizeof(struct olsr_hna)) {
354 if (!TTEST2(*msg_data, sizeof(struct olsr_hna)))
357 ptr.hna = (struct olsr_hna *)msg_data;
359 /* print 4 prefixes per line */
362 ipaddr_string(ptr.hna->network),
363 mask2plen(EXTRACT_32BITS(ptr.hna->mask)),
364 prefix % 4 == 0 ? "\n\t " : " ");
366 msg_data += sizeof(struct olsr_hna);
367 msg_tlen -= sizeof(struct olsr_hna);
373 * FIXME those are the defined messages that lack a decoder
374 * you are welcome to contribute code ;-)
377 case OLSR_POWERINFO_MSG:
378 case OLSR_NAMESERVICE_MSG:
380 print_unknown_data(msg_data, "\n\t ", msg_tlen);
394 * c-style: whitesmith