]> The Tcpdump Group git mirrors - tcpdump/blob - print-olsr.c
Just use u_int, even for values that fit in 16 bits; that squelches a
[tcpdump] / print-olsr.c
1 /*
2 * Copyright (c) 1998-2007 The TCPDUMP project
3 * Copyright (c) 2009 Florian Forster
4 *
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.
15 *
16 * Optimized Link State Protocl (OLSR) as per rfc3626
17 *
18 * Original code by Hannes Gredler <hannes@juniper.net>
19 * IPv6 additions by Florian Forster <octo at verplant.org>
20 */
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include <tcpdump-stdinc.h>
27
28 #include <stdio.h>
29 #include <string.h>
30
31 #include "interface.h"
32 #include "addrtoname.h"
33 #include "extract.h"
34 #include "ip.h"
35
36 /*
37 * RFC 3626 common header
38 *
39 * 0 1 2 3
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 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
50 * | |
51 * : MESSAGE :
52 * | |
53 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
54 * | Message Type | Vtime | Message Size |
55 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
56 * | Originator Address |
57 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
58 * | Time To Live | Hop Count | Message Sequence Number |
59 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
60 * | |
61 * : MESSAGE :
62 * | |
63 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
64 * : :
65 */
66
67 struct olsr_common {
68 u_int8_t packet_len[2];
69 u_int8_t packet_seq[2];
70 };
71
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 */
80
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" },
90 { 0, NULL}
91 };
92
93 struct olsr_msg4 {
94 u_int8_t msg_type;
95 u_int8_t vtime;
96 u_int8_t msg_len[2];
97 u_int8_t originator[4];
98 u_int8_t ttl;
99 u_int8_t hopcount;
100 u_int8_t msg_seq[2];
101 };
102
103 struct olsr_msg6 {
104 u_int8_t msg_type;
105 u_int8_t vtime;
106 u_int8_t msg_len[2];
107 u_int8_t originator[16];
108 u_int8_t ttl;
109 u_int8_t hopcount;
110 u_int8_t msg_seq[2];
111 };
112
113 struct olsr_hello {
114 u_int8_t res[2];
115 u_int8_t htime;
116 u_int8_t will;
117 };
118
119 struct olsr_hello_link {
120 u_int8_t link_code;
121 u_int8_t res;
122 u_int8_t len[2];
123 };
124
125 struct olsr_tc {
126 u_int8_t ans_seq[2];
127 u_int8_t res[2];
128 };
129
130 struct olsr_hna4 {
131 u_int8_t network[4];
132 u_int8_t mask[4];
133 };
134
135 struct olsr_hna6 {
136 u_int8_t network[16];
137 u_int8_t mask[16];
138 };
139
140
141 #define OLSR_EXTRACT_LINK_TYPE(link_code) (link_code & 0x3)
142 #define OLSR_EXTRACT_NEIGHBOR_TYPE(link_code) (link_code >> 2)
143
144 static struct tok olsr_link_type_values[] = {
145 { 0, "Unspecified" },
146 { 1, "Asymmetric" },
147 { 2, "Symmetric" },
148 { 3, "Lost" },
149 { 0, NULL}
150 };
151
152 static struct tok olsr_neighbor_type_values[] = {
153 { 0, "Not-Neighbor" },
154 { 1, "Symmetric" },
155 { 2, "Symmetric-MPR" },
156 { 0, NULL}
157 };
158
159 struct olsr_lq_neighbor4 {
160 u_int8_t neighbor[4];
161 u_int8_t link_quality;
162 u_int8_t neighbor_link_quality;
163 u_int8_t res[2];
164 };
165
166 struct olsr_lq_neighbor6 {
167 u_int8_t neighbor[16];
168 u_int8_t link_quality;
169 u_int8_t neighbor_link_quality;
170 u_int8_t res[2];
171 };
172
173 /*
174 * macro to convert the 8-bit mantissa/exponent to a double float
175 * taken from olsr.org.
176 */
177 #define VTIME_SCALE_FACTOR 0.0625
178 #define ME_TO_DOUBLE(me) \
179 (double)(VTIME_SCALE_FACTOR*(1+(double)(me>>4)/16)*(double)(1<<(me&0x0F)))
180
181 /*
182 * print a neighbor list with LQ extensions.
183 */
184 static void
185 olsr_print_lq_neighbor4 (const u_char *msg_data, u_int hello_len)
186 {
187 struct olsr_lq_neighbor4 *lq_neighbor;
188
189 while (hello_len >= sizeof(struct olsr_lq_neighbor4)) {
190
191 lq_neighbor = (struct olsr_lq_neighbor4 *)msg_data;
192
193 printf("\n\t neighbor %s, link-quality %.2lf%%"
194 ", neighbor-link-quality %.2lf%%",
195 ipaddr_string(lq_neighbor->neighbor),
196 ((double)lq_neighbor->link_quality/2.55),
197 ((double)lq_neighbor->neighbor_link_quality/2.55));
198
199 msg_data += sizeof(struct olsr_lq_neighbor4);
200 hello_len -= sizeof(struct olsr_lq_neighbor4);
201 }
202 }
203
204 #if INET6
205 static void
206 olsr_print_lq_neighbor6 (const u_char *msg_data, u_int hello_len)
207 {
208 struct olsr_lq_neighbor6 *lq_neighbor;
209
210 while (hello_len >= sizeof(struct olsr_lq_neighbor6)) {
211
212 lq_neighbor = (struct olsr_lq_neighbor6 *)msg_data;
213
214 printf("\n\t neighbor %s, link-quality %.2lf%%"
215 ", neighbor-link-quality %.2lf%%",
216 ip6addr_string(lq_neighbor->neighbor),
217 ((double)lq_neighbor->link_quality/2.55),
218 ((double)lq_neighbor->neighbor_link_quality/2.55));
219
220 msg_data += sizeof(struct olsr_lq_neighbor6);
221 hello_len -= sizeof(struct olsr_lq_neighbor6);
222 }
223 }
224 #endif /* INET6 */
225
226 /*
227 * print a neighbor list.
228 */
229 static void
230 olsr_print_neighbor (const u_char *msg_data, u_int hello_len)
231 {
232 int neighbor;
233
234 printf("\n\t neighbor\n\t\t");
235 neighbor = 1;
236
237 while (hello_len >= sizeof(struct in_addr)) {
238
239 /* print 4 neighbors per line */
240
241 printf("%s%s", ipaddr_string(msg_data),
242 neighbor % 4 == 0 ? "\n\t\t" : " ");
243
244 msg_data += sizeof(struct in_addr);
245 hello_len -= sizeof(struct in_addr);
246 }
247 }
248
249
250 void
251 olsr_print (const u_char *pptr, u_int length, int is_ipv6)
252 {
253 union {
254 const struct olsr_common *common;
255 const struct olsr_msg4 *msg4;
256 const struct olsr_msg6 *msg6;
257 const struct olsr_hello *hello;
258 const struct olsr_hello_link *hello_link;
259 const struct olsr_tc *tc;
260 const struct olsr_hna4 *hna;
261 } ptr;
262
263 u_int msg_type, msg_len, msg_tlen, hello_len;
264 u_int16_t name_entry_type, name_entry_len;
265 u_int8_t link_type, neighbor_type;
266 const u_char *tptr, *msg_data;
267
268 tptr = pptr;
269
270 if (length < sizeof(struct olsr_common)) {
271 goto trunc;
272 }
273
274 if (!TTEST2(*tptr, sizeof(struct olsr_common))) {
275 goto trunc;
276 }
277
278 ptr.common = (struct olsr_common *)tptr;
279 length = MIN(length, EXTRACT_16BITS(ptr.common->packet_len));
280
281 printf("OLSRv%i, seq 0x%04x, length %u",
282 (is_ipv6 == 0) ? 4 : 6,
283 EXTRACT_16BITS(ptr.common->packet_seq),
284 length);
285
286 tptr += sizeof(struct olsr_common);
287
288 /*
289 * In non-verbose mode, just print version.
290 */
291 if (vflag < 1) {
292 return;
293 }
294
295 while (tptr < (pptr+length)) {
296 union
297 {
298 struct olsr_msg4 *v4;
299 struct olsr_msg6 *v6;
300 } msgptr;
301 int msg_len_valid = 0;
302
303 if (!TTEST2(*tptr, sizeof(struct olsr_msg4)))
304 goto trunc;
305
306 #if INET6
307 if (is_ipv6)
308 {
309 msgptr.v6 = (struct olsr_msg6 *) tptr;
310 msg_type = msgptr.v6->msg_type;
311 msg_len = EXTRACT_16BITS(msgptr.v6->msg_len);
312 if ((msg_len >= sizeof (struct olsr_msg6))
313 && (msg_len <= length))
314 msg_len_valid = 1;
315
316 /* infinite loop check */
317 if (msg_type == 0 || msg_len == 0) {
318 return;
319 }
320
321 printf("\n\t%s Message (%#04x), originator %s, ttl %u, hop %u"
322 "\n\t vtime %.3lfs, msg-seq 0x%04x, length %u%s",
323 tok2str(olsr_msg_values, "Unknown", msg_type),
324 msg_type, ip6addr_string(msgptr.v6->originator),
325 msgptr.v6->ttl,
326 msgptr.v6->hopcount,
327 ME_TO_DOUBLE(msgptr.v6->vtime),
328 EXTRACT_16BITS(msgptr.v6->msg_seq),
329 msg_len, (msg_len_valid == 0) ? " (invalid)" : "");
330
331 msg_tlen = msg_len - sizeof(struct olsr_msg6);
332 msg_data = tptr + sizeof(struct olsr_msg6);
333 }
334 else /* (!is_ipv6) */
335 #endif /* INET6 */
336 {
337 msgptr.v4 = (struct olsr_msg4 *) tptr;
338 msg_type = msgptr.v4->msg_type;
339 msg_len = EXTRACT_16BITS(msgptr.v4->msg_len);
340 if ((msg_len >= sizeof (struct olsr_msg4))
341 && (msg_len <= length))
342 msg_len_valid = 1;
343
344 /* infinite loop check */
345 if (msg_type == 0 || msg_len == 0) {
346 return;
347 }
348
349 printf("\n\t%s Message (%#04x), originator %s, ttl %u, hop %u"
350 "\n\t vtime %.3lfs, msg-seq 0x%04x, length %u%s",
351 tok2str(olsr_msg_values, "Unknown", msg_type),
352 msg_type, ipaddr_string(msgptr.v4->originator),
353 msgptr.v4->ttl,
354 msgptr.v4->hopcount,
355 ME_TO_DOUBLE(msgptr.v4->vtime),
356 EXTRACT_16BITS(msgptr.v4->msg_seq),
357 msg_len, (msg_len_valid == 0) ? " (invalid)" : "");
358
359 msg_tlen = msg_len - sizeof(struct olsr_msg4);
360 msg_data = tptr + sizeof(struct olsr_msg4);
361 }
362
363 switch (msg_type) {
364 case OLSR_HELLO_MSG:
365 case OLSR_HELLO_LQ_MSG:
366 if (!TTEST2(*msg_data, sizeof(struct olsr_hello)))
367 goto trunc;
368
369 ptr.hello = (struct olsr_hello *)msg_data;
370 printf("\n\t hello-time %.3lfs, MPR willingness %u",
371 ME_TO_DOUBLE(ptr.hello->htime), ptr.hello->will);
372 msg_data += sizeof(struct olsr_hello);
373 msg_tlen -= sizeof(struct olsr_hello);
374
375 while (msg_tlen >= sizeof(struct olsr_hello_link)) {
376 int hello_len_valid = 0;
377
378 /*
379 * link-type.
380 */
381 if (!TTEST2(*msg_data, sizeof(struct olsr_hello_link)))
382 goto trunc;
383
384 ptr.hello_link = (struct olsr_hello_link *)msg_data;
385
386 hello_len = EXTRACT_16BITS(ptr.hello_link->len);
387 link_type = OLSR_EXTRACT_LINK_TYPE(ptr.hello_link->link_code);
388 neighbor_type = OLSR_EXTRACT_NEIGHBOR_TYPE(ptr.hello_link->link_code);
389
390 if ((hello_len <= msg_tlen)
391 && (hello_len >= sizeof(struct olsr_hello_link)))
392 hello_len_valid = 1;
393
394 printf("\n\t link-type %s, neighbor-type %s, len %u%s",
395 tok2str(olsr_link_type_values, "Unknown", link_type),
396 tok2str(olsr_neighbor_type_values, "Unknown", neighbor_type),
397 hello_len,
398 (hello_len_valid == 0) ? " (invalid)" : "");
399
400 if (hello_len_valid == 0)
401 break;
402
403 msg_data += sizeof(struct olsr_hello_link);
404 msg_tlen -= sizeof(struct olsr_hello_link);
405 hello_len -= sizeof(struct olsr_hello_link);
406
407 if (msg_type == OLSR_HELLO_MSG) {
408 olsr_print_neighbor(msg_data, hello_len);
409 } else {
410 #if INET6
411 if (is_ipv6)
412 olsr_print_lq_neighbor6(msg_data, hello_len);
413 else
414 #endif
415 olsr_print_lq_neighbor4(msg_data, hello_len);
416 }
417
418 msg_data += hello_len;
419 msg_tlen -= hello_len;
420 }
421 break;
422
423 case OLSR_TC_MSG:
424 case OLSR_TC_LQ_MSG:
425 if (!TTEST2(*msg_data, sizeof(struct olsr_tc)))
426 goto trunc;
427
428 ptr.tc = (struct olsr_tc *)msg_data;
429 printf("\n\t advertised neighbor seq 0x%04x",
430 EXTRACT_16BITS(ptr.tc->ans_seq));
431 msg_data += sizeof(struct olsr_tc);
432 msg_tlen -= sizeof(struct olsr_tc);
433
434 if (msg_type == OLSR_TC_MSG) {
435 olsr_print_neighbor(msg_data, msg_tlen);
436 } else {
437 #if INET6
438 if (is_ipv6)
439 olsr_print_lq_neighbor6(msg_data, msg_tlen);
440 else
441 #endif
442 olsr_print_lq_neighbor4(msg_data, msg_tlen);
443 }
444 break;
445
446 case OLSR_MID_MSG:
447 {
448 size_t addr_size = sizeof(struct in_addr);
449
450 #if INET6
451 if (is_ipv6)
452 addr_size = sizeof(struct in6_addr);
453 #endif
454
455 if (!TTEST2(*msg_data, addr_size))
456 goto trunc;
457
458 while (msg_tlen >= addr_size) {
459 printf("\n\t interface address %s",
460 #if INET6
461 is_ipv6 ? ip6addr_string(msg_data) :
462 #endif
463 ipaddr_string(msg_data));
464 msg_data += addr_size;
465 msg_tlen -= addr_size;
466 }
467 break;
468 }
469
470 case OLSR_HNA_MSG:
471 printf("\n\t Advertised networks (total %u)",
472 (unsigned int) (msg_tlen / sizeof(struct olsr_hna6)));
473 #if INET6
474 if (is_ipv6)
475 {
476 int i = 0;
477 while (msg_tlen >= sizeof(struct olsr_hna6)) {
478 struct olsr_hna6 *hna6;
479
480 if (!TTEST2(*msg_data, sizeof(struct olsr_hna6)))
481 goto trunc;
482
483 hna6 = (struct olsr_hna6 *)msg_data;
484
485 printf("\n\t #%i: %s/%u",
486 i, ip6addr_string(hna6->network),
487 mask62plen (hna6->mask));
488
489 msg_data += sizeof(struct olsr_hna6);
490 msg_tlen -= sizeof(struct olsr_hna6);
491 }
492 }
493 else
494 #endif
495 {
496 int col = 0;
497 while (msg_tlen >= sizeof(struct olsr_hna4)) {
498 if (!TTEST2(*msg_data, sizeof(struct olsr_hna4)))
499 goto trunc;
500
501 ptr.hna = (struct olsr_hna4 *)msg_data;
502
503 /* print 4 prefixes per line */
504 if (col == 0)
505 printf ("\n\t ");
506 else
507 printf (", ");
508
509 printf("%s/%u",
510 ipaddr_string(ptr.hna->network),
511 mask2plen(EXTRACT_32BITS(ptr.hna->mask)));
512
513 msg_data += sizeof(struct olsr_hna4);
514 msg_tlen -= sizeof(struct olsr_hna4);
515
516 col = (col + 1) % 4;
517 }
518 }
519 break;
520
521 case OLSR_NAMESERVICE_MSG:
522 {
523 u_int name_entries = EXTRACT_16BITS(msg_data+2);
524 u_int addr_size = 4;
525 int name_entries_valid = 0;
526 u_int i;
527
528 if (is_ipv6)
529 addr_size = 16;
530
531 if ((name_entries > 0)
532 && ((name_entries * (4 + addr_size)) <= msg_tlen))
533 name_entries_valid = 1;
534
535 printf("\n\t Version %u, Entries %u%s",
536 EXTRACT_16BITS(msg_data),
537 name_entries, (name_entries_valid == 0) ? " (invalid)" : "");
538
539 if (name_entries_valid == 0)
540 break;
541
542 msg_data += 4;
543 msg_tlen -= 4;
544
545 for (i = 0; i < name_entries; i++) {
546 int name_entry_len_valid = 0;
547
548 if (msg_tlen < 4)
549 break;
550
551 name_entry_type = EXTRACT_16BITS(msg_data);
552 name_entry_len = EXTRACT_16BITS(msg_data+2);
553
554 msg_data += 4;
555 msg_tlen -= 4;
556
557 if ((name_entry_len > 0) && ((addr_size + name_entry_len) <= msg_tlen))
558 name_entry_len_valid = 1;
559
560 printf("\n\t #%u: type %#06x, length %u%s",
561 (unsigned int) i, name_entry_type,
562 name_entry_len, (name_entry_len_valid == 0) ? " (invalid)" : "");
563
564 if (name_entry_len_valid == 0)
565 break;
566
567 {
568 char name[name_entry_len + 1];
569 memcpy (name, msg_data + addr_size, name_entry_len);
570 name[name_entry_len] = 0;
571 #if INET6
572 if (is_ipv6)
573 printf(", address %s, name \"%s\"",
574 ip6addr_string(msg_data), name);
575 else
576 #endif
577 printf(", address %s, name \"%s\"",
578 ipaddr_string(msg_data), name);
579 }
580
581 /* 32-bit alignment */
582 if (name_entry_len%4 != 0)
583 name_entry_len+=4-(name_entry_len%4);
584
585 msg_data += addr_size + name_entry_len;
586 msg_tlen -= addr_size + name_entry_len;
587 } /* for (i = 0; i < name_entries; i++) */
588 break;
589 } /* case OLSR_NAMESERVICE_MSG */
590
591 /*
592 * FIXME those are the defined messages that lack a decoder
593 * you are welcome to contribute code ;-)
594 */
595 case OLSR_POWERINFO_MSG:
596 default:
597 print_unknown_data(msg_data, "\n\t ", msg_tlen);
598 break;
599 } /* switch (msg_type) */
600 tptr += msg_len;
601 } /* while (tptr < (pptr+length)) */
602
603 return;
604
605 trunc:
606 printf("[|olsr]");
607 }
608
609 /*
610 * Local Variables:
611 * c-style: whitesmith
612 * c-basic-offset: 4
613 * End:
614 */