]> The Tcpdump Group git mirrors - tcpdump/blob - print-olsr.c
Netdissectify the to-name resolution routines.
[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 #define NETDISSECT_REWORKED
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <tcpdump-stdinc.h>
28
29 #include "interface.h"
30 #include "addrtoname.h"
31 #include "extract.h"
32
33 /*
34 * RFC 3626 common header
35 *
36 * 0 1 2 3
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 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
47 * | |
48 * : MESSAGE :
49 * | |
50 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
51 * | Message Type | Vtime | Message Size |
52 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
53 * | Originator Address |
54 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
55 * | Time To Live | Hop Count | Message Sequence Number |
56 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
57 * | |
58 * : MESSAGE :
59 * | |
60 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
61 * : :
62 */
63
64 struct olsr_common {
65 u_int8_t packet_len[2];
66 u_int8_t packet_seq[2];
67 };
68
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 */
77
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" },
87 { 0, NULL}
88 };
89
90 struct olsr_msg4 {
91 u_int8_t msg_type;
92 u_int8_t vtime;
93 u_int8_t msg_len[2];
94 u_int8_t originator[4];
95 u_int8_t ttl;
96 u_int8_t hopcount;
97 u_int8_t msg_seq[2];
98 };
99
100 struct olsr_msg6 {
101 u_int8_t msg_type;
102 u_int8_t vtime;
103 u_int8_t msg_len[2];
104 u_int8_t originator[16];
105 u_int8_t ttl;
106 u_int8_t hopcount;
107 u_int8_t msg_seq[2];
108 };
109
110 struct olsr_hello {
111 u_int8_t res[2];
112 u_int8_t htime;
113 u_int8_t will;
114 };
115
116 struct olsr_hello_link {
117 u_int8_t link_code;
118 u_int8_t res;
119 u_int8_t len[2];
120 };
121
122 struct olsr_tc {
123 u_int8_t ans_seq[2];
124 u_int8_t res[2];
125 };
126
127 struct olsr_hna4 {
128 u_int8_t network[4];
129 u_int8_t mask[4];
130 };
131
132 struct olsr_hna6 {
133 u_int8_t network[16];
134 u_int8_t mask[16];
135 };
136
137
138 #define OLSR_EXTRACT_LINK_TYPE(link_code) (link_code & 0x3)
139 #define OLSR_EXTRACT_NEIGHBOR_TYPE(link_code) (link_code >> 2)
140
141 static const struct tok olsr_link_type_values[] = {
142 { 0, "Unspecified" },
143 { 1, "Asymmetric" },
144 { 2, "Symmetric" },
145 { 3, "Lost" },
146 { 0, NULL}
147 };
148
149 static const struct tok olsr_neighbor_type_values[] = {
150 { 0, "Not-Neighbor" },
151 { 1, "Symmetric" },
152 { 2, "Symmetric-MPR" },
153 { 0, NULL}
154 };
155
156 struct olsr_lq_neighbor4 {
157 u_int8_t neighbor[4];
158 u_int8_t link_quality;
159 u_int8_t neighbor_link_quality;
160 u_int8_t res[2];
161 };
162
163 struct olsr_lq_neighbor6 {
164 u_int8_t neighbor[16];
165 u_int8_t link_quality;
166 u_int8_t neighbor_link_quality;
167 u_int8_t res[2];
168 };
169
170 /*
171 * macro to convert the 8-bit mantissa/exponent to a double float
172 * taken from olsr.org.
173 */
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)))
177
178 /*
179 * print a neighbor list with LQ extensions.
180 */
181 static void
182 olsr_print_lq_neighbor4(netdissect_options *ndo,
183 const u_char *msg_data, u_int hello_len)
184 {
185 struct olsr_lq_neighbor4 *lq_neighbor;
186
187 while (hello_len >= sizeof(struct olsr_lq_neighbor4)) {
188
189 lq_neighbor = (struct olsr_lq_neighbor4 *)msg_data;
190
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)));
196
197 msg_data += sizeof(struct olsr_lq_neighbor4);
198 hello_len -= sizeof(struct olsr_lq_neighbor4);
199 }
200 }
201
202 #if INET6
203 static void
204 olsr_print_lq_neighbor6(netdissect_options *ndo,
205 const u_char *msg_data, u_int hello_len)
206 {
207 struct olsr_lq_neighbor6 *lq_neighbor;
208
209 while (hello_len >= sizeof(struct olsr_lq_neighbor6)) {
210
211 lq_neighbor = (struct olsr_lq_neighbor6 *)msg_data;
212
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)));
218
219 msg_data += sizeof(struct olsr_lq_neighbor6);
220 hello_len -= sizeof(struct olsr_lq_neighbor6);
221 }
222 }
223 #endif /* INET6 */
224
225 /*
226 * print a neighbor list.
227 */
228 static void
229 olsr_print_neighbor(netdissect_options *ndo,
230 const u_char *msg_data, u_int hello_len)
231 {
232 int neighbor;
233
234 ND_PRINT((ndo, "\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 ND_PRINT((ndo, "%s%s", ipaddr_string(ndo, 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(netdissect_options *ndo,
252 const u_char *pptr, u_int length, int is_ipv6)
253 {
254 union {
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;
262 } ptr;
263
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;
269
270 tptr = pptr;
271
272 if (length < sizeof(struct olsr_common)) {
273 goto trunc;
274 }
275
276 if (!ND_TTEST2(*tptr, sizeof(struct olsr_common))) {
277 goto trunc;
278 }
279
280 ptr.common = (struct olsr_common *)tptr;
281 length = min(length, EXTRACT_16BITS(ptr.common->packet_len));
282
283 ND_PRINT((ndo, "OLSRv%i, seq 0x%04x, length %u",
284 (is_ipv6 == 0) ? 4 : 6,
285 EXTRACT_16BITS(ptr.common->packet_seq),
286 length));
287
288 tptr += sizeof(struct olsr_common);
289
290 /*
291 * In non-verbose mode, just print version.
292 */
293 if (ndo->ndo_vflag < 1) {
294 return;
295 }
296
297 while (tptr < (pptr+length)) {
298 union
299 {
300 struct olsr_msg4 *v4;
301 struct olsr_msg6 *v6;
302 } msgptr;
303 int msg_len_valid = 0;
304
305 if (!ND_TTEST2(*tptr, sizeof(struct olsr_msg4)))
306 goto trunc;
307
308 #if INET6
309 if (is_ipv6)
310 {
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))
316 msg_len_valid = 1;
317
318 /* infinite loop check */
319 if (msg_type == 0 || msg_len == 0) {
320 return;
321 }
322
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),
327 msgptr.v6->ttl,
328 msgptr.v6->hopcount,
329 ME_TO_DOUBLE(msgptr.v6->vtime),
330 EXTRACT_16BITS(msgptr.v6->msg_seq),
331 msg_len, (msg_len_valid == 0) ? " (invalid)" : ""));
332
333 msg_tlen = msg_len - sizeof(struct olsr_msg6);
334 msg_data = tptr + sizeof(struct olsr_msg6);
335 }
336 else /* (!is_ipv6) */
337 #endif /* INET6 */
338 {
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))
344 msg_len_valid = 1;
345
346 /* infinite loop check */
347 if (msg_type == 0 || msg_len == 0) {
348 return;
349 }
350
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),
355 msgptr.v4->ttl,
356 msgptr.v4->hopcount,
357 ME_TO_DOUBLE(msgptr.v4->vtime),
358 EXTRACT_16BITS(msgptr.v4->msg_seq),
359 msg_len, (msg_len_valid == 0) ? " (invalid)" : ""));
360
361 msg_tlen = msg_len - sizeof(struct olsr_msg4);
362 msg_data = tptr + sizeof(struct olsr_msg4);
363 }
364
365 switch (msg_type) {
366 case OLSR_HELLO_MSG:
367 case OLSR_HELLO_LQ_MSG:
368 if (!ND_TTEST2(*msg_data, sizeof(struct olsr_hello)))
369 goto trunc;
370
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);
376
377 while (msg_tlen >= sizeof(struct olsr_hello_link)) {
378 int hello_len_valid = 0;
379
380 /*
381 * link-type.
382 */
383 if (!ND_TTEST2(*msg_data, sizeof(struct olsr_hello_link)))
384 goto trunc;
385
386 ptr.hello_link = (struct olsr_hello_link *)msg_data;
387
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);
391
392 if ((hello_len <= msg_tlen)
393 && (hello_len >= sizeof(struct olsr_hello_link)))
394 hello_len_valid = 1;
395
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),
399 hello_len,
400 (hello_len_valid == 0) ? " (invalid)" : ""));
401
402 if (hello_len_valid == 0)
403 break;
404
405 msg_data += sizeof(struct olsr_hello_link);
406 msg_tlen -= sizeof(struct olsr_hello_link);
407 hello_len -= sizeof(struct olsr_hello_link);
408
409 if (msg_type == OLSR_HELLO_MSG) {
410 olsr_print_neighbor(ndo, msg_data, hello_len);
411 } else {
412 #if INET6
413 if (is_ipv6)
414 olsr_print_lq_neighbor6(ndo, msg_data, hello_len);
415 else
416 #endif
417 olsr_print_lq_neighbor4(ndo, msg_data, hello_len);
418 }
419
420 msg_data += hello_len;
421 msg_tlen -= hello_len;
422 }
423 break;
424
425 case OLSR_TC_MSG:
426 case OLSR_TC_LQ_MSG:
427 if (!ND_TTEST2(*msg_data, sizeof(struct olsr_tc)))
428 goto trunc;
429
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);
435
436 if (msg_type == OLSR_TC_MSG) {
437 olsr_print_neighbor(ndo, msg_data, msg_tlen);
438 } else {
439 #if INET6
440 if (is_ipv6)
441 olsr_print_lq_neighbor6(ndo, msg_data, msg_tlen);
442 else
443 #endif
444 olsr_print_lq_neighbor4(ndo, msg_data, msg_tlen);
445 }
446 break;
447
448 case OLSR_MID_MSG:
449 {
450 size_t addr_size = sizeof(struct in_addr);
451
452 #if INET6
453 if (is_ipv6)
454 addr_size = sizeof(struct in6_addr);
455 #endif
456
457 while (msg_tlen >= addr_size) {
458 if (!ND_TTEST2(*msg_data, addr_size))
459 goto trunc;
460 #if INET6
461 ND_PRINT((ndo, "\n\t interface address %s",
462 is_ipv6 ? ip6addr_string(ndo, msg_data) :
463 ipaddr_string(ndo, msg_data)));
464 #else
465 ND_PRINT((ndo, "\n\t interface address %s",
466 ipaddr_string(ndo, msg_data)));
467 #endif
468
469 msg_data += addr_size;
470 msg_tlen -= addr_size;
471 }
472 break;
473 }
474
475 case OLSR_HNA_MSG:
476 ND_PRINT((ndo, "\n\t Advertised networks (total %u)",
477 (unsigned int) (msg_tlen / sizeof(struct olsr_hna6))));
478 #if INET6
479 if (is_ipv6)
480 {
481 int i = 0;
482 while (msg_tlen >= sizeof(struct olsr_hna6)) {
483 struct olsr_hna6 *hna6;
484
485 if (!ND_TTEST2(*msg_data, sizeof(struct olsr_hna6)))
486 goto trunc;
487
488 hna6 = (struct olsr_hna6 *)msg_data;
489
490 ND_PRINT((ndo, "\n\t #%i: %s/%u",
491 i, ip6addr_string(ndo, hna6->network),
492 mask62plen (hna6->mask)));
493
494 msg_data += sizeof(struct olsr_hna6);
495 msg_tlen -= sizeof(struct olsr_hna6);
496 }
497 }
498 else
499 #endif
500 {
501 int col = 0;
502 while (msg_tlen >= sizeof(struct olsr_hna4)) {
503 if (!ND_TTEST2(*msg_data, sizeof(struct olsr_hna4)))
504 goto trunc;
505
506 ptr.hna = (struct olsr_hna4 *)msg_data;
507
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))));
513
514 msg_data += sizeof(struct olsr_hna4);
515 msg_tlen -= sizeof(struct olsr_hna4);
516
517 col = (col + 1) % 4;
518 }
519 }
520 break;
521
522 case OLSR_NAMESERVICE_MSG:
523 {
524 u_int name_entries = EXTRACT_16BITS(msg_data+2);
525 u_int addr_size = 4;
526 int name_entries_valid = 0;
527 u_int i;
528
529 if (is_ipv6)
530 addr_size = 16;
531
532 if ((name_entries > 0)
533 && ((name_entries * (4 + addr_size)) <= msg_tlen))
534 name_entries_valid = 1;
535
536 if (msg_tlen < 4)
537 goto trunc;
538 if (!ND_TTEST2(*msg_data, 4))
539 goto trunc;
540
541 ND_PRINT((ndo, "\n\t Version %u, Entries %u%s",
542 EXTRACT_16BITS(msg_data),
543 name_entries, (name_entries_valid == 0) ? " (invalid)" : ""));
544
545 if (name_entries_valid == 0)
546 break;
547
548 msg_data += 4;
549 msg_tlen -= 4;
550
551 for (i = 0; i < name_entries; i++) {
552 int name_entry_len_valid = 0;
553
554 if (msg_tlen < 4)
555 break;
556 if (!ND_TTEST2(*msg_data, 4))
557 goto trunc;
558
559 name_entry_type = EXTRACT_16BITS(msg_data);
560 name_entry_len = EXTRACT_16BITS(msg_data+2);
561
562 msg_data += 4;
563 msg_tlen -= 4;
564
565 if ((name_entry_len > 0) && ((addr_size + name_entry_len) <= msg_tlen))
566 name_entry_len_valid = 1;
567
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)" : ""));
571
572 if (name_entry_len_valid == 0)
573 break;
574
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);
579
580 if (msg_tlen < addr_size + name_entry_len + name_entry_padding)
581 goto trunc;
582
583 if (!ND_TTEST2(*msg_data, addr_size + name_entry_len + name_entry_padding))
584 goto trunc;
585
586 #if INET6
587 if (is_ipv6)
588 ND_PRINT((ndo, ", address %s, name \"",
589 ip6addr_string(ndo, msg_data)));
590 else
591 #endif
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, "\""));
596
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++) */
600 break;
601 } /* case OLSR_NAMESERVICE_MSG */
602
603 /*
604 * FIXME those are the defined messages that lack a decoder
605 * you are welcome to contribute code ;-)
606 */
607 case OLSR_POWERINFO_MSG:
608 default:
609 print_unknown_data(ndo, msg_data, "\n\t ", msg_tlen);
610 break;
611 } /* switch (msg_type) */
612 tptr += msg_len;
613 } /* while (tptr < (pptr+length)) */
614
615 return;
616
617 trunc:
618 ND_PRINT((ndo, "[|olsr]"));
619 }
620
621 /*
622 * Local Variables:
623 * c-style: whitesmith
624 * c-basic-offset: 4
625 * End:
626 */