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 uint8_t packet_len[2]; 66 uint8_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 uint8_t msg_type; 92 uint8_t vtime; 93 uint8_t msg_len[2]; 94 uint8_t originator[4]; 95 uint8_t ttl; 96 uint8_t hopcount; 97 uint8_t msg_seq[2]; 98 }; 99 100 struct olsr_msg6 { 101 uint8_t msg_type; 102 uint8_t vtime; 103 uint8_t msg_len[2]; 104 uint8_t originator[16]; 105 uint8_t ttl; 106 uint8_t hopcount; 107 uint8_t msg_seq[2]; 108 }; 109 110 struct olsr_hello { 111 uint8_t res[2]; 112 uint8_t htime; 113 uint8_t will; 114 }; 115 116 struct olsr_hello_link { 117 uint8_t link_code; 118 uint8_t res; 119 uint8_t len[2]; 120 }; 121 122 struct olsr_tc { 123 uint8_t ans_seq[2]; 124 uint8_t res[2]; 125 }; 126 127 struct olsr_hna4 { 128 uint8_t network[4]; 129 uint8_t mask[4]; 130 }; 131 132 struct olsr_hna6 { 133 uint8_t network[16]; 134 uint8_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 uint8_t neighbor[4]; 158 uint8_t link_quality; 159 uint8_t neighbor_link_quality; 160 uint8_t res[2]; 161 }; 162 163 struct olsr_lq_neighbor6 { 164 uint8_t neighbor[16]; 165 uint8_t link_quality; 166 uint8_t neighbor_link_quality; 167 uint8_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 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; 269 270 tptr = pptr; 271 272 if (length < sizeof(struct olsr_common)) { 273 goto trunc; 274 } 275 276 ND_TCHECK2(*tptr, sizeof(struct olsr_common)); 277 278 ptr.common = (struct olsr_common *)tptr; 279 length = min(length, EXTRACT_16BITS(ptr.common->packet_len)); 280 281 ND_PRINT((ndo, "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 (ndo->ndo_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 ND_TCHECK2(*tptr, sizeof(struct olsr_msg4)); 304 305 #if INET6 306 if (is_ipv6) 307 { 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)) 313 msg_len_valid = 1; 314 315 /* infinite loop check */ 316 if (msg_type == 0 || msg_len == 0) { 317 return; 318 } 319 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), 324 msgptr.v6->ttl, 325 msgptr.v6->hopcount, 326 ME_TO_DOUBLE(msgptr.v6->vtime), 327 EXTRACT_16BITS(msgptr.v6->msg_seq), 328 msg_len, (msg_len_valid == 0) ? " (invalid)" : "")); 329 330 msg_tlen = msg_len - sizeof(struct olsr_msg6); 331 msg_data = tptr + sizeof(struct olsr_msg6); 332 } 333 else /* (!is_ipv6) */ 334 #endif /* INET6 */ 335 { 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)) 341 msg_len_valid = 1; 342 343 /* infinite loop check */ 344 if (msg_type == 0 || msg_len == 0) { 345 return; 346 } 347 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), 352 msgptr.v4->ttl, 353 msgptr.v4->hopcount, 354 ME_TO_DOUBLE(msgptr.v4->vtime), 355 EXTRACT_16BITS(msgptr.v4->msg_seq), 356 msg_len, (msg_len_valid == 0) ? " (invalid)" : "")); 357 358 msg_tlen = msg_len - sizeof(struct olsr_msg4); 359 msg_data = tptr + sizeof(struct olsr_msg4); 360 } 361 362 switch (msg_type) { 363 case OLSR_HELLO_MSG: 364 case OLSR_HELLO_LQ_MSG: 365 ND_TCHECK2(*msg_data, sizeof(struct olsr_hello)); 366 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); 372 373 while (msg_tlen >= sizeof(struct olsr_hello_link)) { 374 int hello_len_valid = 0; 375 376 /* 377 * link-type. 378 */ 379 ND_TCHECK2(*msg_data, sizeof(struct olsr_hello_link)); 380 381 ptr.hello_link = (struct olsr_hello_link *)msg_data; 382 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); 386 387 if ((hello_len <= msg_tlen) 388 && (hello_len >= sizeof(struct olsr_hello_link))) 389 hello_len_valid = 1; 390 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), 394 hello_len, 395 (hello_len_valid == 0) ? " (invalid)" : "")); 396 397 if (hello_len_valid == 0) 398 break; 399 400 msg_data += sizeof(struct olsr_hello_link); 401 msg_tlen -= sizeof(struct olsr_hello_link); 402 hello_len -= sizeof(struct olsr_hello_link); 403 404 if (msg_type == OLSR_HELLO_MSG) { 405 olsr_print_neighbor(ndo, msg_data, hello_len); 406 } else { 407 #if INET6 408 if (is_ipv6) 409 olsr_print_lq_neighbor6(ndo, msg_data, hello_len); 410 else 411 #endif 412 olsr_print_lq_neighbor4(ndo, msg_data, hello_len); 413 } 414 415 msg_data += hello_len; 416 msg_tlen -= hello_len; 417 } 418 break; 419 420 case OLSR_TC_MSG: 421 case OLSR_TC_LQ_MSG: 422 ND_TCHECK2(*msg_data, sizeof(struct olsr_tc)); 423 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); 429 430 if (msg_type == OLSR_TC_MSG) { 431 olsr_print_neighbor(ndo, msg_data, msg_tlen); 432 } else { 433 #if INET6 434 if (is_ipv6) 435 olsr_print_lq_neighbor6(ndo, msg_data, msg_tlen); 436 else 437 #endif 438 olsr_print_lq_neighbor4(ndo, msg_data, msg_tlen); 439 } 440 break; 441 442 case OLSR_MID_MSG: 443 { 444 size_t addr_size = sizeof(struct in_addr); 445 446 #if INET6 447 if (is_ipv6) 448 addr_size = sizeof(struct in6_addr); 449 #endif 450 451 while (msg_tlen >= addr_size) { 452 ND_TCHECK2(*msg_data, addr_size); 453 #if INET6 454 ND_PRINT((ndo, "\n\t interface address %s", 455 is_ipv6 ? ip6addr_string(ndo, msg_data) : 456 ipaddr_string(ndo, msg_data))); 457 #else 458 ND_PRINT((ndo, "\n\t interface address %s", 459 ipaddr_string(ndo, msg_data))); 460 #endif 461 462 msg_data += addr_size; 463 msg_tlen -= addr_size; 464 } 465 break; 466 } 467 468 case OLSR_HNA_MSG: 469 ND_PRINT((ndo, "\n\t Advertised networks (total %u)", 470 (unsigned int) (msg_tlen / sizeof(struct olsr_hna6)))); 471 #if INET6 472 if (is_ipv6) 473 { 474 int i = 0; 475 while (msg_tlen >= sizeof(struct olsr_hna6)) { 476 struct olsr_hna6 *hna6; 477 478 ND_TCHECK2(*msg_data, sizeof(struct olsr_hna6)); 479 480 hna6 = (struct olsr_hna6 *)msg_data; 481 482 ND_PRINT((ndo, "\n\t #%i: %s/%u", 483 i, ip6addr_string(ndo, hna6->network), 484 mask62plen (hna6->mask))); 485 486 msg_data += sizeof(struct olsr_hna6); 487 msg_tlen -= sizeof(struct olsr_hna6); 488 } 489 } 490 else 491 #endif 492 { 493 int col = 0; 494 while (msg_tlen >= sizeof(struct olsr_hna4)) { 495 ND_TCHECK2(*msg_data, sizeof(struct olsr_hna4)); 496 497 ptr.hna = (struct olsr_hna4 *)msg_data; 498 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)))); 504 505 msg_data += sizeof(struct olsr_hna4); 506 msg_tlen -= sizeof(struct olsr_hna4); 507 508 col = (col + 1) % 4; 509 } 510 } 511 break; 512 513 case OLSR_NAMESERVICE_MSG: 514 { 515 u_int name_entries = EXTRACT_16BITS(msg_data+2); 516 u_int addr_size = 4; 517 int name_entries_valid = 0; 518 u_int i; 519 520 if (is_ipv6) 521 addr_size = 16; 522 523 if ((name_entries > 0) 524 && ((name_entries * (4 + addr_size)) <= msg_tlen)) 525 name_entries_valid = 1; 526 527 if (msg_tlen < 4) 528 goto trunc; 529 ND_TCHECK2(*msg_data, 4); 530 531 ND_PRINT((ndo, "\n\t Version %u, Entries %u%s", 532 EXTRACT_16BITS(msg_data), 533 name_entries, (name_entries_valid == 0) ? " (invalid)" : "")); 534 535 if (name_entries_valid == 0) 536 break; 537 538 msg_data += 4; 539 msg_tlen -= 4; 540 541 for (i = 0; i < name_entries; i++) { 542 int name_entry_len_valid = 0; 543 544 if (msg_tlen < 4) 545 break; 546 ND_TCHECK2(*msg_data, 4); 547 548 name_entry_type = EXTRACT_16BITS(msg_data); 549 name_entry_len = EXTRACT_16BITS(msg_data+2); 550 551 msg_data += 4; 552 msg_tlen -= 4; 553 554 if ((name_entry_len > 0) && ((addr_size + name_entry_len) <= msg_tlen)) 555 name_entry_len_valid = 1; 556 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)" : "")); 560 561 if (name_entry_len_valid == 0) 562 break; 563 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); 568 569 if (msg_tlen < addr_size + name_entry_len + name_entry_padding) 570 goto trunc; 571 572 ND_TCHECK2(*msg_data, addr_size + name_entry_len + name_entry_padding); 573 574 #if INET6 575 if (is_ipv6) 576 ND_PRINT((ndo, ", address %s, name \"", 577 ip6addr_string(ndo, msg_data))); 578 else 579 #endif 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, "\"")); 584 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++) */ 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(ndo, 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 ND_PRINT((ndo, "[|olsr]")); 607 } 608 609 /* 610 * Local Variables: 611 * c-style: whitesmith 612 * c-basic-offset: 4 613 * End: 614 */ 615