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