1 /* $OpenBSD: print-ip.c,v 1.53 2020/01/24 22:46:36 procter 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 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 printf("igmp query"); 162 if (*(int *)&bp[4]) 163 printf(" [gaddr %s]", ipaddr_string(&bp[4])); 164 if (len != 8) 165 printf(" [len %d]", len); 166 break; 167 case 0x12: 168 printf("igmp report %s", ipaddr_string(&bp[4])); 169 if (len != 8) 170 printf(" [len %d]", len); 171 break; 172 case 0x16: 173 printf("igmp nreport %s", ipaddr_string(&bp[4])); 174 break; 175 case 0x17: 176 printf("igmp leave %s", ipaddr_string(&bp[4])); 177 break; 178 case 0x13: 179 printf("igmp dvmrp"); 180 if (len < 8) 181 printf(" [len %d]", len); 182 else 183 dvmrp_print(bp, len); 184 break; 185 case 0x14: 186 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 printf("igmp-%d", bp[0] & 0xf); 197 break; 198 } 199 if ((bp[0] >> 4) != 1) 200 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 printf("[|igmp]"); 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 printf("bad-ip-version %u", ip->ip_v); 360 goto out; 361 } 362 363 len = ntohs(ip->ip_len); 364 if (length < len) { 365 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 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 printf("%s > %s:", ipaddr_string(&ip->ip_src), 410 ipaddr_string(&ip->ip_dst)); 411 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 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 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 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 printf("%s > %s: ", 476 ipaddr_string(&ip->ip_src), 477 ipaddr_string(&ip->ip_dst)); 478 esp_print(cp, len, (const u_char *)ip); 479 break; 480 481 #ifndef IPPROTO_AH 482 #define IPPROTO_AH 51 483 #endif 484 case IPPROTO_AH: 485 ah_print(cp, len, (const u_char *)ip); 486 break; 487 488 #ifndef IPPROTO_MOBILE 489 #define IPPROTO_MOBILE 55 490 #endif 491 case IPPROTO_MOBILE: 492 if (vflag) 493 printf("mobile %s > %s: ", 494 ipaddr_string(&ip->ip_src), 495 ipaddr_string(&ip->ip_dst)); 496 mobile_print(cp, len); 497 if (! vflag) { 498 printf(" (mobile encap)"); 499 goto out; 500 } 501 break; 502 503 #ifndef IPPROTO_ETHERIP 504 #define IPPROTO_ETHERIP 97 505 #endif 506 case IPPROTO_ETHERIP: 507 printf("%s > %s: ", 508 ipaddr_string(&ip->ip_src), 509 ipaddr_string(&ip->ip_dst)); 510 etherip_print(cp, snapend - cp, len); 511 break; 512 513 #ifndef IPPROTO_IPCOMP 514 #define IPPROTO_IPCOMP 108 515 #endif 516 case IPPROTO_IPCOMP: 517 ipcomp_print(cp, len, (const u_char *)ip); 518 break; 519 520 #ifndef IPPROTO_CARP 521 #define IPPROTO_CARP 112 522 #endif 523 case IPPROTO_CARP: 524 if (packettype == PT_VRRP) { 525 if (vflag) 526 printf("vrrp %s > %s: ", 527 ipaddr_string(&ip->ip_src), 528 ipaddr_string(&ip->ip_dst)); 529 vrrp_print(cp, len, ip->ip_ttl); 530 } else { 531 if (vflag) 532 printf("carp %s > %s: ", 533 ipaddr_string(&ip->ip_src), 534 ipaddr_string(&ip->ip_dst)); 535 carp_print(cp, len, ip->ip_ttl); 536 } 537 break; 538 539 #ifndef IPPROTO_PFSYNC 540 #define IPPROTO_PFSYNC 240 541 #endif 542 case IPPROTO_PFSYNC: 543 pfsync_ip_print(cp, 544 (int)(snapend - (u_char *)ip) - hlen, 545 (const u_char *)ip); 546 break; 547 548 default: 549 printf("%s > %s:", 550 ipaddr_string(&ip->ip_src), 551 ipaddr_string(&ip->ip_dst)); 552 printf(" ip-proto-%d %d", ip->ip_p, len); 553 break; 554 } 555 } 556 /* 557 * for fragmented datagrams, print id:size@offset. On all 558 * but the last stick a "+". For unfragmented datagrams, note 559 * the don't fragment flag. 560 */ 561 if (off & 0x3fff) { 562 /* 563 * if this isn't the first frag, we're missing the 564 * next level protocol header. print the ip addr. 565 */ 566 if (off & 0x1fff) 567 printf("%s > %s:", 568 ipaddr_string(&ip->ip_src), 569 ipaddr_string(&ip->ip_dst)); 570 printf(" (frag %d:%d@%d%s)", 571 ntohs(ip->ip_id), len, 572 (off & 0x1fff) * 8, 573 (off & IP_MF)? "+" : ""); 574 } 575 if (off & IP_DF) 576 printf(" (DF)"); 577 578 if (ip->ip_tos) { 579 printf(" [tos 0x%x", (int)ip->ip_tos); 580 if (ip->ip_tos & (IPTOS_CE|IPTOS_ECT)) { 581 printf(" ("); 582 if (ip->ip_tos & IPTOS_ECT) { 583 /* ECN-capable transport */ 584 putchar('E'); 585 } 586 if (ip->ip_tos & IPTOS_CE) { 587 /* _C_ongestion experienced (ECN) */ 588 putchar('C'); 589 } 590 printf(")"); 591 } 592 printf("]"); 593 } 594 595 if (ip->ip_ttl <= 1) 596 printf(" [ttl %d]", (int)ip->ip_ttl); 597 598 if (vflag) { 599 char *sep = ""; 600 601 printf(" ("); 602 if (ip->ip_ttl > 1) { 603 printf("%sttl %d", sep, (int)ip->ip_ttl); 604 sep = ", "; 605 } 606 if ((off & 0x3fff) == 0) { 607 printf("%sid %d", sep, (int)ntohs(ip->ip_id)); 608 sep = ", "; 609 } 610 printf("%slen %u", sep, ntohs(ip->ip_len)); 611 sep = ", "; 612 if ((u_char *)ip + hlen <= snapend) { 613 u_int16_t sum, ip_sum; 614 sum = in_cksum((const u_short *)ip, hlen, 0); 615 if (sum != 0) { 616 ip_sum = EXTRACT_16BITS(&ip->ip_sum); 617 printf("%sbad ip cksum %x! -> %x", sep, ip_sum, 618 in_cksum_shouldbe(ip_sum, sum)); 619 sep = ", "; 620 } 621 } 622 if (hlen > sizeof(struct ip)) { 623 hlen -= sizeof(struct ip); 624 printf("%soptlen=%d", sep, hlen); 625 ip_optprint((u_char *)(ip + 1), hlen); 626 } 627 printf(")"); 628 } 629 out: 630 packetp = pktp; 631 snapend = send; 632 return; 633 634 trunc: 635 printf("[|ip]"); 636 } 637