1 /* $OpenBSD: print-ip.c,v 1.41 2014/12/03 13:22:18 mikeb Exp $ */ 2 3 /* 4 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that: (1) source code distributions 9 * retain the above copyright notice and this paragraph in its entirety, (2) 10 * distributions including binary code include the above copyright notice and 11 * this paragraph in its entirety in the documentation or other materials 12 * provided with the distribution, and (3) all advertising materials mentioning 13 * features or use of this software display the following acknowledgement: 14 * ``This product includes software developed by the University of California, 15 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of 16 * the University nor the names of its contributors may be used to endorse 17 * or promote products derived from this software without specific prior 18 * written permission. 19 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED 20 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF 21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 22 */ 23 24 #include <sys/param.h> 25 #include <sys/time.h> 26 #include <sys/socket.h> 27 28 #include <netinet/in.h> 29 #include <netinet/ip.h> 30 #include <netinet/ip_var.h> 31 #include <netinet/udp.h> 32 #include <netinet/udp_var.h> 33 #include <netinet/tcp.h> 34 35 #include <inttypes.h> 36 #include <stdio.h> 37 #include <stdlib.h> 38 #include <string.h> 39 #include <unistd.h> 40 41 #include "addrtoname.h" 42 #include "interface.h" 43 #include "extract.h" /* must come after interface.h */ 44 45 /* Compatibility */ 46 #ifndef IPPROTO_ND 47 #define IPPROTO_ND 77 48 #endif 49 50 #ifndef IN_CLASSD 51 #define IN_CLASSD(i) (((int32_t)(i) & 0xf0000000) == 0xe0000000) 52 #endif 53 54 /* Definitions required for ECN 55 for use if the OS running tcpdump does not have ECN */ 56 #ifndef IPTOS_ECT 57 #define IPTOS_ECT 0x02 /* ECN Capable Transport in IP header*/ 58 #endif 59 #ifndef IPTOS_CE 60 #define IPTOS_CE 0x01 /* ECN Cong. Experienced in IP header*/ 61 #endif 62 63 /* (following from ipmulti/mrouted/prune.h) */ 64 65 /* 66 * The packet format for a traceroute request. 67 */ 68 struct tr_query { 69 u_int tr_src; /* traceroute source */ 70 u_int tr_dst; /* traceroute destination */ 71 u_int tr_raddr; /* traceroute response address */ 72 #if BYTE_ORDER == BIG_ENDIAN 73 struct { 74 u_int ttl : 8; /* traceroute response ttl */ 75 u_int qid : 24; /* traceroute query id */ 76 } q; 77 #else 78 struct { 79 u_int qid : 24; /* traceroute query id */ 80 u_int ttl : 8; /* traceroute response ttl */ 81 } q; 82 #endif 83 }; 84 85 #define tr_rttl q.ttl 86 #define tr_qid q.qid 87 88 /* 89 * Traceroute response format. A traceroute response has a tr_query at the 90 * beginning, followed by one tr_resp for each hop taken. 91 */ 92 struct tr_resp { 93 u_int tr_qarr; /* query arrival time */ 94 u_int tr_inaddr; /* incoming interface address */ 95 u_int tr_outaddr; /* outgoing interface address */ 96 u_int tr_rmtaddr; /* parent address in source tree */ 97 u_int tr_vifin; /* input packet count on interface */ 98 u_int tr_vifout; /* output packet count on interface */ 99 u_int tr_pktcnt; /* total incoming packets for src-grp */ 100 u_char tr_rproto; /* routing proto deployed on router */ 101 u_char tr_fttl; /* ttl required to forward on outvif */ 102 u_char tr_smask; /* subnet mask for src addr */ 103 u_char tr_rflags; /* forwarding error codes */ 104 }; 105 106 /* defs within mtrace */ 107 #define TR_QUERY 1 108 #define TR_RESP 2 109 110 /* fields for tr_rflags (forwarding error codes) */ 111 #define TR_NO_ERR 0 112 #define TR_WRONG_IF 1 113 #define TR_PRUNED 2 114 #define TR_OPRUNED 3 115 #define TR_SCOPED 4 116 #define TR_NO_RTE 5 117 #define TR_NO_FWD 7 118 #define TR_NO_SPACE 0x81 119 #define TR_OLD_ROUTER 0x82 120 121 /* fields for tr_rproto (routing protocol) */ 122 #define TR_PROTO_DVMRP 1 123 #define TR_PROTO_MOSPF 2 124 #define TR_PROTO_PIM 3 125 #define TR_PROTO_CBT 4 126 127 static void print_mtrace(register const u_char *bp, register u_int len) 128 { 129 register struct tr_query *tr = (struct tr_query *)(bp + 8); 130 131 printf("mtrace %d: %s to %s reply-to %s", tr->tr_qid, 132 ipaddr_string(&tr->tr_src), ipaddr_string(&tr->tr_dst), 133 ipaddr_string(&tr->tr_raddr)); 134 if (IN_CLASSD(ntohl(tr->tr_raddr))) 135 printf(" with-ttl %d", tr->tr_rttl); 136 } 137 138 static void print_mresp(register const u_char *bp, register u_int len) 139 { 140 register struct tr_query *tr = (struct tr_query *)(bp + 8); 141 142 printf("mresp %d: %s to %s reply-to %s", tr->tr_qid, 143 ipaddr_string(&tr->tr_src), ipaddr_string(&tr->tr_dst), 144 ipaddr_string(&tr->tr_raddr)); 145 if (IN_CLASSD(ntohl(tr->tr_raddr))) 146 printf(" with-ttl %d", tr->tr_rttl); 147 } 148 149 static void 150 igmp_print(register const u_char *bp, register u_int len, 151 register const u_char *bp2) 152 { 153 register const struct ip *ip; 154 155 ip = (const struct ip *)bp2; 156 (void)printf("%s > %s: ", 157 ipaddr_string(&ip->ip_src), 158 ipaddr_string(&ip->ip_dst)); 159 160 TCHECK2(bp[0], 8); 161 switch (bp[0]) { 162 case 0x11: 163 (void)printf("igmp query"); 164 if (*(int *)&bp[4]) 165 (void)printf(" [gaddr %s]", ipaddr_string(&bp[4])); 166 if (len != 8) 167 (void)printf(" [len %d]", len); 168 break; 169 case 0x12: 170 (void)printf("igmp report %s", ipaddr_string(&bp[4])); 171 if (len != 8) 172 (void)printf(" [len %d]", len); 173 break; 174 case 0x16: 175 (void)printf("igmp nreport %s", ipaddr_string(&bp[4])); 176 break; 177 case 0x17: 178 (void)printf("igmp leave %s", ipaddr_string(&bp[4])); 179 break; 180 case 0x13: 181 (void)printf("igmp dvmrp"); 182 if (len < 8) 183 (void)printf(" [len %d]", len); 184 else 185 dvmrp_print(bp, len); 186 break; 187 case 0x14: 188 (void)printf("igmp pim"); 189 pim_print(bp, len); 190 break; 191 case 0x1e: 192 print_mresp(bp, len); 193 break; 194 case 0x1f: 195 print_mtrace(bp, len); 196 break; 197 default: 198 (void)printf("igmp-%d", bp[0] & 0xf); 199 break; 200 } 201 if ((bp[0] >> 4) != 1) 202 (void)printf(" [v%d]", bp[0] >> 4); 203 204 TCHECK2(bp[0], len); 205 if (vflag) { 206 /* Check the IGMP checksum */ 207 u_int32_t sum = 0; 208 int count; 209 const u_short *sp = (u_short *)bp; 210 211 for (count = len / 2; --count >= 0; ) 212 sum += *sp++; 213 if (len & 1) 214 sum += ntohs(*(u_char *) sp << 8); 215 while (sum >> 16) 216 sum = (sum & 0xffff) + (sum >> 16); 217 sum = 0xffff & ~sum; 218 if (sum != 0) 219 printf(" bad igmp cksum %x!", EXTRACT_16BITS(&bp[2])); 220 } 221 return; 222 trunc: 223 fputs("[|igmp]", stdout); 224 } 225 226 /* 227 * print the recorded route in an IP RR, LSRR or SSRR option. 228 */ 229 static void 230 ip_printroute(const char *type, register const u_char *cp, u_int length) 231 { 232 register u_int ptr = cp[2] - 1; 233 register u_int len; 234 235 printf(" %s{", type); 236 if ((length + 1) & 3) 237 printf(" [bad length %d]", length); 238 if (ptr < 3 || ((ptr + 1) & 3) || ptr > length + 1) 239 printf(" [bad ptr %d]", cp[2]); 240 241 type = ""; 242 for (len = 3; len < length; len += 4) { 243 if (ptr == len) 244 type = "#"; 245 printf("%s%s", type, ipaddr_string(&cp[len])); 246 type = " "; 247 } 248 printf("%s}", ptr == len? "#" : ""); 249 } 250 251 /* 252 * print IP options. 253 */ 254 static void 255 ip_optprint(register const u_char *cp, u_int length) 256 { 257 register u_int len; 258 int tt; 259 260 for (; length > 0; cp += len, length -= len) { 261 TCHECK(cp[1]); 262 tt = *cp; 263 len = (tt == IPOPT_NOP || tt == IPOPT_EOL) ? 1 : cp[1]; 264 if (len <= 0) { 265 printf("[|ip op len %d]", len); 266 return; 267 } 268 if (&cp[1] >= snapend || cp + len > snapend) { 269 printf("[|ip]"); 270 return; 271 } 272 switch (tt) { 273 274 case IPOPT_EOL: 275 printf(" EOL"); 276 if (length > 1) 277 printf("-%d", length - 1); 278 return; 279 280 case IPOPT_NOP: 281 printf(" NOP"); 282 break; 283 284 case IPOPT_TS: 285 printf(" TS{%d}", len); 286 break; 287 288 case IPOPT_SECURITY: 289 printf(" SECURITY{%d}", len); 290 break; 291 292 case IPOPT_RR: 293 printf(" RR{%d}=", len); 294 ip_printroute("RR", cp, len); 295 break; 296 297 case IPOPT_SSRR: 298 ip_printroute("SSRR", cp, len); 299 break; 300 301 case IPOPT_LSRR: 302 ip_printroute("LSRR", cp, len); 303 break; 304 305 default: 306 printf(" IPOPT-%d{%d}", cp[0], len); 307 break; 308 } 309 } 310 return; 311 312 trunc: 313 printf("[|ip]"); 314 } 315 316 /* 317 * compute an IP header checksum. 318 * don't modifiy the packet. 319 */ 320 u_short 321 in_cksum(const u_short *addr, register int len, int csum) 322 { 323 int nleft = len; 324 const u_short *w = addr; 325 u_short answer; 326 int sum = csum; 327 328 /* 329 * Our algorithm is simple, using a 32 bit accumulator (sum), 330 * we add sequential 16 bit words to it, and at the end, fold 331 * back all the carry bits from the top 16 bits into the lower 332 * 16 bits. 333 */ 334 while (nleft > 1) { 335 sum += *w++; 336 nleft -= 2; 337 } 338 if (nleft == 1) 339 sum += htons(*(u_char *)w<<8); 340 341 /* 342 * add back carry outs from top 16 bits to low 16 bits 343 */ 344 sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */ 345 sum += (sum >> 16); /* add carry */ 346 answer = ~sum; /* truncate to 16 bits */ 347 return (answer); 348 } 349 350 /* 351 * print an IP datagram. 352 */ 353 void 354 ip_print(register const u_char *bp, register u_int length) 355 { 356 register const struct ip *ip; 357 register u_int hlen, len, off; 358 register const u_char *cp; 359 const u_char *pktp = packetp; 360 const u_char *send = snapend; 361 362 ip = (const struct ip *)bp; 363 if ((u_char *)(ip + 1) > snapend) { 364 printf("[|ip]"); 365 return; 366 } 367 368 /* 369 * If the IP header is not aligned, copy into abuf. 370 * This will never happen with BPF. It does happen with raw packet 371 * dumps from -r. 372 */ 373 if ((intptr_t)ip & (sizeof(long)-1)) { 374 static u_char *abuf = NULL; 375 static int didwarn = 0; 376 int clen = snapend - bp; 377 378 if (clen > snaplen) 379 clen = snaplen; 380 if (abuf == NULL) { 381 abuf = (u_char *)malloc(snaplen); 382 if (abuf == NULL) 383 error("ip_print: malloc"); 384 } 385 memmove((char *)abuf, (char *)ip, min(length, clen)); 386 snapend = abuf + clen; 387 packetp = abuf; 388 ip = (struct ip *)abuf; 389 /* We really want libpcap to give us aligned packets */ 390 if (!didwarn) { 391 warning("compensating for unaligned libpcap packets"); 392 ++didwarn; 393 } 394 } 395 396 TCHECK(*ip); 397 if (ip->ip_v != IPVERSION) { 398 (void)printf("bad-ip-version %u", ip->ip_v); 399 goto out; 400 } 401 402 len = ntohs(ip->ip_len); 403 if (length < len) { 404 (void)printf("truncated-ip - %d bytes missing!", 405 len - length); 406 len = length; 407 } 408 409 hlen = ip->ip_hl * 4; 410 if (hlen < sizeof(struct ip) || hlen > len) { 411 (void)printf("bad-hlen %d", hlen); 412 goto out; 413 } 414 415 len -= hlen; 416 417 /* 418 * If this is fragment zero, hand it to the next higher 419 * level protocol. 420 */ 421 off = ntohs(ip->ip_off); 422 if ((off & 0x1fff) == 0) { 423 cp = (const u_char *)ip + hlen; 424 switch (ip->ip_p) { 425 426 case IPPROTO_TCP: 427 tcp_print(cp, len, (const u_char *)ip); 428 break; 429 430 case IPPROTO_UDP: 431 udp_print(cp, len, (const u_char *)ip); 432 break; 433 434 case IPPROTO_ICMP: 435 icmp_print(cp, len, (const u_char *)ip); 436 break; 437 438 #ifndef IPPROTO_IGRP 439 #define IPPROTO_IGRP 9 440 #endif 441 case IPPROTO_IGRP: 442 igrp_print(cp, len, (const u_char *)ip); 443 break; 444 445 case IPPROTO_ND: 446 (void)printf("%s > %s:", ipaddr_string(&ip->ip_src), 447 ipaddr_string(&ip->ip_dst)); 448 (void)printf(" nd %d", len); 449 break; 450 451 #ifndef IPPROTO_OSPF 452 #define IPPROTO_OSPF 89 453 #endif 454 case IPPROTO_OSPF: 455 ospf_print(cp, len, (const u_char *)ip); 456 break; 457 458 #ifndef IPPROTO_IGMP 459 #define IPPROTO_IGMP 2 460 #endif 461 case IPPROTO_IGMP: 462 igmp_print(cp, len, (const u_char *)ip); 463 break; 464 465 #ifndef IPPROTO_IPIP 466 #define IPPROTO_IPIP 4 467 #endif 468 case IPPROTO_IPIP: 469 /* ip-in-ip encapsulation */ 470 if (vflag) 471 (void)printf("%s > %s: ", 472 ipaddr_string(&ip->ip_src), 473 ipaddr_string(&ip->ip_dst)); 474 ip_print(cp, len); 475 if (! vflag) { 476 printf(" (encap)"); 477 goto out; 478 } 479 break; 480 481 #ifdef INET6 482 #ifndef IPPROTO_IPV6 483 #define IPPROTO_IPV6 484 #endif 485 case IPPROTO_IPV6: 486 /* ip6-in-ip encapsulation */ 487 if (vflag) 488 (void)printf("%s > %s: ", 489 ipaddr_string(&ip->ip_src), 490 ipaddr_string(&ip->ip_dst)); 491 ip6_print(cp, len); 492 if (! vflag) { 493 printf(" (encap)"); 494 goto out; 495 } 496 break; 497 #endif /*INET6*/ 498 499 #ifndef IPPROTO_GRE 500 #define IPPROTO_GRE 47 501 #endif 502 case IPPROTO_GRE: 503 if (vflag) 504 (void)printf("gre %s > %s: ", 505 ipaddr_string(&ip->ip_src), 506 ipaddr_string(&ip->ip_dst)); 507 /* do it */ 508 gre_print(cp, len); 509 if (! vflag) { 510 printf(" (gre encap)"); 511 goto out; 512 } 513 break; 514 515 #ifndef IPPROTO_ESP 516 #define IPPROTO_ESP 50 517 #endif 518 case IPPROTO_ESP: 519 esp_print(cp, len, (const u_char *)ip); 520 break; 521 522 #ifndef IPPROTO_AH 523 #define IPPROTO_AH 51 524 #endif 525 case IPPROTO_AH: 526 ah_print(cp, len, (const u_char *)ip); 527 break; 528 529 #ifndef IPPROTO_MOBILE 530 #define IPPROTO_MOBILE 55 531 #endif 532 case IPPROTO_MOBILE: 533 if (vflag) 534 (void)printf("mobile %s > %s: ", 535 ipaddr_string(&ip->ip_src), 536 ipaddr_string(&ip->ip_dst)); 537 mobile_print(cp, len); 538 if (! vflag) { 539 printf(" (mobile encap)"); 540 goto out; 541 } 542 break; 543 544 #ifndef IPPROTO_ETHERIP 545 #define IPPROTO_ETHERIP 97 546 #endif 547 case IPPROTO_ETHERIP: 548 etherip_print(cp, snapend - cp, len, 549 (const u_char *)ip); 550 break; 551 552 #ifndef IPPROTO_IPCOMP 553 #define IPPROTO_IPCOMP 108 554 #endif 555 case IPPROTO_IPCOMP: 556 ipcomp_print(cp, len, (const u_char *)ip); 557 break; 558 559 #ifndef IPPROTO_CARP 560 #define IPPROTO_CARP 112 561 #endif 562 case IPPROTO_CARP: 563 if (packettype == PT_VRRP) { 564 if (vflag) 565 (void)printf("vrrp %s > %s: ", 566 ipaddr_string(&ip->ip_src), 567 ipaddr_string(&ip->ip_dst)); 568 vrrp_print(cp, len, ip->ip_ttl); 569 } else { 570 if (vflag) 571 (void)printf("carp %s > %s: ", 572 ipaddr_string(&ip->ip_src), 573 ipaddr_string(&ip->ip_dst)); 574 carp_print(cp, len, ip->ip_ttl); 575 } 576 break; 577 578 #ifndef IPPROTO_PFSYNC 579 #define IPPROTO_PFSYNC 240 580 #endif 581 case IPPROTO_PFSYNC: 582 pfsync_ip_print(cp, 583 (int)(snapend - (u_char *)ip) - hlen, 584 (const u_char *)ip); 585 break; 586 587 default: 588 (void)printf("%s > %s:", ipaddr_string(&ip->ip_src), 589 ipaddr_string(&ip->ip_dst)); 590 (void)printf(" ip-proto-%d %d", ip->ip_p, len); 591 break; 592 } 593 } 594 /* 595 * for fragmented datagrams, print id:size@offset. On all 596 * but the last stick a "+". For unfragmented datagrams, note 597 * the don't fragment flag. 598 */ 599 if (off & 0x3fff) { 600 /* 601 * if this isn't the first frag, we're missing the 602 * next level protocol header. print the ip addr. 603 */ 604 if (off & 0x1fff) 605 (void)printf("%s > %s:", ipaddr_string(&ip->ip_src), 606 ipaddr_string(&ip->ip_dst)); 607 (void)printf(" (frag %d:%d@%d%s)", ntohs(ip->ip_id), len, 608 (off & 0x1fff) * 8, 609 (off & IP_MF)? "+" : ""); 610 } 611 if (off & IP_DF) 612 (void)printf(" (DF)"); 613 614 if (ip->ip_tos) { 615 (void)printf(" [tos 0x%x", (int)ip->ip_tos); 616 if (ip->ip_tos & (IPTOS_CE|IPTOS_ECT)) { 617 (void)printf(" ("); 618 if (ip->ip_tos & IPTOS_ECT) { 619 /* ECN-capable transport */ 620 putchar('E'); 621 } 622 if (ip->ip_tos & IPTOS_CE) { 623 /* _C_ongestion experienced (ECN) */ 624 putchar('C'); 625 } 626 (void)printf(")"); 627 } 628 (void)printf("]"); 629 } 630 631 if (ip->ip_ttl <= 1) 632 (void)printf(" [ttl %d]", (int)ip->ip_ttl); 633 634 if (vflag) { 635 char *sep = ""; 636 637 printf(" ("); 638 if (ip->ip_ttl > 1) { 639 (void)printf("%sttl %d", sep, (int)ip->ip_ttl); 640 sep = ", "; 641 } 642 if ((off & 0x3fff) == 0) { 643 (void)printf("%sid %d", sep, (int)ntohs(ip->ip_id)); 644 sep = ", "; 645 } 646 (void)printf("%slen %u", sep, ntohs(ip->ip_len)); 647 sep = ", "; 648 if ((u_char *)ip + hlen <= snapend) { 649 u_int16_t sum, ip_sum; 650 sum = in_cksum((const u_short *)ip, hlen, 0); 651 if (sum != 0) { 652 ip_sum = EXTRACT_16BITS(&ip->ip_sum); 653 (void)printf("%sbad ip cksum %x! -> %x", sep, ip_sum, 654 in_cksum_shouldbe(ip_sum, sum)); 655 sep = ", "; 656 } 657 } 658 if (hlen > sizeof(struct ip)) { 659 hlen -= sizeof(struct ip); 660 (void)printf("%soptlen=%d", sep, hlen); 661 ip_optprint((u_char *)(ip + 1), hlen); 662 } 663 printf(")"); 664 } 665 out: 666 packetp = pktp; 667 snapend = send; 668 return; 669 670 trunc: 671 printf("[|ip]"); 672 } 673