1 /* 2 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that: (1) source code distributions 7 * retain the above copyright notice and this paragraph in its entirety, (2) 8 * distributions including binary code include the above copyright notice and 9 * this paragraph in its entirety in the documentation or other materials 10 * provided with the distribution, and (3) all advertising materials mentioning 11 * features or use of this software display the following acknowledgement: 12 * ``This product includes software developed by the University of California, 13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of 14 * the University nor the names of its contributors may be used to endorse 15 * or promote products derived from this software without specific prior 16 * written permission. 17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED 18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF 19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 20 * 21 * Support for splitting captures into multiple files with a maximum 22 * file size: 23 * 24 * Copyright (c) 2001 25 * Seth Webster <swebster@sst.ll.mit.edu> 26 */ 27 28 #ifndef lint 29 static const char copyright[] = 30 "@(#) Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000\n\ 31 The Regents of the University of California. All rights reserved.\n"; 32 static const char rcsid[] = 33 "@(#) $Header: /tcpdump/master/tcpdump/tcpdump.c,v 1.173 2001/12/22 22:12:23 guy Exp $ (LBL)"; 34 #endif 35 36 /* $FreeBSD$ */ 37 38 /* 39 * tcpdump - monitor tcp/ip traffic on an ethernet. 40 * 41 * First written in 1987 by Van Jacobson, Lawrence Berkeley Laboratory. 42 * Mercilessly hacked and occasionally improved since then via the 43 * combined efforts of Van, Steve McCanne and Craig Leres of LBL. 44 */ 45 46 #ifdef HAVE_CONFIG_H 47 #include "config.h" 48 #endif 49 50 #include <sys/types.h> 51 #include <sys/time.h> 52 53 #include <netinet/in.h> 54 55 #include <pcap.h> 56 #include <signal.h> 57 #include <stdio.h> 58 #include <stdlib.h> 59 #include <string.h> 60 #include <unistd.h> 61 #include <ctype.h> 62 63 64 #include "interface.h" 65 #include "addrtoname.h" 66 #include "machdep.h" 67 #include "setsignal.h" 68 #include "gmt2local.h" 69 70 int aflag; /* translate network and broadcast addresses */ 71 int dflag; /* print filter code */ 72 int eflag; /* print ethernet header */ 73 int fflag; /* don't translate "foreign" IP address */ 74 int Lflag; /* list available data link types and exit */ 75 int nflag; /* leave addresses as numbers */ 76 int Nflag; /* remove domains from printed host names */ 77 int Oflag = 1; /* run filter code optimizer */ 78 int pflag; /* don't go promiscuous */ 79 int qflag; /* quick (shorter) output */ 80 int Rflag = 1; /* print sequence # field in AH/ESP*/ 81 int sflag = 0; /* use the libsmi to translate OIDs */ 82 int Sflag; /* print raw TCP sequence numbers */ 83 int tflag = 1; /* print packet arrival time */ 84 int uflag = 0; /* Print undecoded NFS handles */ 85 int vflag; /* verbose */ 86 int xflag; /* print packet in hex */ 87 int Xflag; /* print packet in ascii as well as hex */ 88 off_t Cflag = 0; /* rotate dump files after this many bytes */ 89 int dlt = -1; /* if != -1, ask libpcap for the DLT it names */ 90 91 const char *dlt_name = NULL; 92 93 char *espsecret = NULL; /* ESP secret key */ 94 95 int packettype; 96 97 int infodelay; 98 int infoprint; 99 100 char *program_name; 101 102 int32_t thiszone; /* seconds offset from gmt to local time */ 103 104 /* Forwards */ 105 static RETSIGTYPE cleanup(int); 106 static void usage(void) __attribute__((noreturn)); 107 static void show_dlts_and_exit(pcap_t *pd) __attribute__((noreturn)); 108 109 static void dump_and_trunc(u_char *, const struct pcap_pkthdr *, const u_char *); 110 111 #ifdef SIGINFO 112 RETSIGTYPE requestinfo(int); 113 #endif 114 115 /* Length of saved portion of packet. */ 116 int snaplen = DEFAULT_SNAPLEN; 117 118 struct printer { 119 pcap_handler f; 120 int type; 121 }; 122 123 static struct printer printers[] = { 124 { arcnet_if_print, DLT_ARCNET }, 125 { ether_if_print, DLT_EN10MB }, 126 { token_if_print, DLT_IEEE802 }, 127 #ifdef DLT_LANE8023 128 { lane_if_print, DLT_LANE8023 }, 129 #endif 130 #ifdef DLT_CIP 131 { cip_if_print, DLT_CIP }, 132 #endif 133 #ifdef DLT_ATM_CLIP 134 { cip_if_print, DLT_ATM_CLIP }, 135 #endif 136 { sl_if_print, DLT_SLIP }, 137 { sl_bsdos_if_print, DLT_SLIP_BSDOS }, 138 { ppp_if_print, DLT_PPP }, 139 { ppp_bsdos_if_print, DLT_PPP_BSDOS }, 140 { fddi_if_print, DLT_FDDI }, 141 { null_if_print, DLT_NULL }, 142 #ifdef DLT_LOOP 143 { null_if_print, DLT_LOOP }, 144 #endif 145 { raw_if_print, DLT_RAW }, 146 { atm_if_print, DLT_ATM_RFC1483 }, 147 #ifdef DLT_C_HDLC 148 { chdlc_if_print, DLT_C_HDLC }, 149 #endif 150 #ifdef DLT_HDLC 151 { chdlc_if_print, DLT_HDLC }, 152 #endif 153 #ifdef DLT_PPP_SERIAL 154 { ppp_hdlc_if_print, DLT_PPP_SERIAL }, 155 #endif 156 #ifdef DLT_PPP_ETHER 157 { pppoe_if_print, DLT_PPP_ETHER }, 158 #endif 159 #ifdef DLT_LINUX_SLL 160 { sll_if_print, DLT_LINUX_SLL }, 161 #endif 162 #ifdef DLT_IEEE802_11 163 { ieee802_11_if_print, DLT_IEEE802_11}, 164 #endif 165 #ifdef DLT_LTALK 166 { ltalk_if_print, DLT_LTALK }, 167 #endif 168 { NULL, 0 }, 169 }; 170 171 static pcap_handler 172 lookup_printer(int type) 173 { 174 struct printer *p; 175 176 for (p = printers; p->f; ++p) 177 if (type == p->type) 178 return p->f; 179 180 error("unknown data link type %d", type); 181 /* NOTREACHED */ 182 } 183 184 static pcap_t *pd; 185 186 extern int optind; 187 extern int opterr; 188 extern char *optarg; 189 190 struct dump_info { 191 char *WFileName; 192 pcap_t *pd; 193 pcap_dumper_t *p; 194 }; 195 196 struct dlt_choice { 197 const char* name; 198 int dlt; 199 }; 200 201 #define DLT_CHOICE(code) { #code, code } 202 #define DLT_CHOICE_SENTINEL { NULL, 0 } 203 204 struct dlt_choice dlt_choices[] = { 205 DLT_CHOICE(DLT_ARCNET), 206 DLT_CHOICE(DLT_EN10MB), 207 DLT_CHOICE(DLT_IEEE802), 208 #ifdef DLT_LANE8023 209 DLT_CHOICE(DLT_LANE8023), 210 #endif 211 #ifdef DLT_CIP 212 DLT_CHOICE(DLT_CIP), 213 #endif 214 #ifdef DLT_ATM_CLIP 215 DLT_CHOICE(DLT_ATM_CLIP), 216 #endif 217 DLT_CHOICE(DLT_SLIP), 218 DLT_CHOICE(DLT_SLIP_BSDOS), 219 DLT_CHOICE(DLT_PPP), 220 DLT_CHOICE(DLT_PPP_BSDOS), 221 DLT_CHOICE(DLT_FDDI), 222 DLT_CHOICE(DLT_NULL), 223 #ifdef DLT_LOOP 224 DLT_CHOICE(DLT_LOOP), 225 #endif 226 DLT_CHOICE(DLT_RAW), 227 DLT_CHOICE(DLT_ATM_RFC1483), 228 #ifdef DLT_C_HDLC 229 DLT_CHOICE(DLT_C_HDLC), 230 #endif 231 #ifdef DLT_HDLC 232 DLT_CHOICE(DLT_HDLC), 233 #endif 234 #ifdef DLT_PPP_SERIAL 235 DLT_CHOICE(DLT_PPP_SERIAL), 236 #endif 237 #ifdef DLT_PPP_ETHER 238 DLT_CHOICE(DLT_PPP_ETHER), 239 #endif 240 #ifdef DLT_LINUX_SLL 241 DLT_CHOICE(DLT_LINUX_SLL), 242 #endif 243 #ifdef DLT_IEEE802_11 244 DLT_CHOICE(DLT_IEEE802_11), 245 #endif 246 #ifdef DLT_LTALK 247 DLT_CHOICE(DLT_LTALK), 248 #endif 249 #ifdef DLT_PFLOG 250 DLT_CHOICE(DLT_PFLOG), 251 #endif 252 DLT_CHOICE_SENTINEL 253 }; 254 255 static void 256 show_dlts_and_exit(pcap_t *pd) 257 { 258 int i, n_dlts; 259 int *dlts = 0; 260 n_dlts = pcap_list_datalinks(pd, &dlts); 261 if (n_dlts < 0) 262 error("%s", pcap_geterr(pd)); 263 else if (n_dlts == 0 || !dlts) 264 error("No data link types."); 265 266 (void) fprintf(stderr, "Data link types (use option -y):\n"); 267 268 while (--n_dlts >= 0) { 269 for (i = 0; dlt_choices[i].name; i++) { 270 if (dlt_choices[i].dlt != dlts[n_dlts]) { 271 continue; 272 } 273 (void) fprintf(stderr, " %s\n", 274 dlt_choices[i].name + sizeof("DLT_") - 1); 275 break; 276 } 277 if (!dlt_choices[i].name) 278 fprintf(stderr, " %d (not supported)\n", dlts[n_dlts]); 279 } 280 free(dlts); 281 exit(0); 282 } 283 284 int 285 main(int argc, char **argv) 286 { 287 register int cnt, op, i; 288 bpf_u_int32 localnet, netmask; 289 register char *cp, *infile, *cmdbuf, *device, *RFileName, *WFileName; 290 pcap_handler printer; 291 struct bpf_program fcode; 292 RETSIGTYPE (*oldhandler)(int); 293 struct dump_info dumpinfo; 294 u_char *pcap_userdata; 295 char ebuf[PCAP_ERRBUF_SIZE]; 296 297 cnt = -1; 298 device = NULL; 299 infile = NULL; 300 RFileName = NULL; 301 WFileName = NULL; 302 if ((cp = strrchr(argv[0], '/')) != NULL) 303 program_name = cp + 1; 304 else 305 program_name = argv[0]; 306 307 if (abort_on_misalignment(ebuf, sizeof(ebuf)) < 0) 308 error("%s", ebuf); 309 310 #ifdef LIBSMI 311 smiInit("tcpdump"); 312 #endif 313 314 opterr = 0; 315 while ( 316 (op = getopt(argc, argv, "ac:C:deE:fF:i:lLm:nNOpqr:Rs:StT:uvw:xXy:Y")) != -1) 317 switch (op) { 318 319 case 'a': 320 ++aflag; 321 break; 322 323 case 'c': 324 cnt = atoi(optarg); 325 if (cnt <= 0) 326 error("invalid packet count %s", optarg); 327 break; 328 329 case 'C': 330 Cflag = atoi(optarg) * 1000000; 331 if (Cflag < 0) 332 error("invalid file size %s", optarg); 333 break; 334 335 case 'd': 336 ++dflag; 337 break; 338 339 case 'L': 340 Lflag++; 341 break; 342 343 case 'e': 344 ++eflag; 345 break; 346 347 case 'E': 348 #ifndef HAVE_LIBCRYPTO 349 warning("crypto code not compiled in"); 350 #endif 351 espsecret = optarg; 352 break; 353 354 case 'f': 355 ++fflag; 356 break; 357 358 case 'F': 359 infile = optarg; 360 break; 361 362 case 'i': 363 device = optarg; 364 break; 365 366 case 'l': 367 #ifdef HAVE_SETLINEBUF 368 setlinebuf(stdout); 369 #else 370 setvbuf(stdout, NULL, _IOLBF, 0); 371 #endif 372 break; 373 374 case 'n': 375 ++nflag; 376 break; 377 378 case 'N': 379 ++Nflag; 380 break; 381 382 case 'm': 383 #ifdef LIBSMI 384 if (smiLoadModule(optarg) == 0) { 385 error("could not load MIB module %s", optarg); 386 } 387 sflag = 1; 388 #else 389 (void)fprintf(stderr, "%s: ignoring option `-m %s' ", 390 program_name, optarg); 391 (void)fprintf(stderr, "(no libsmi support)\n"); 392 #endif 393 394 case 'O': 395 Oflag = 0; 396 break; 397 398 case 'p': 399 ++pflag; 400 break; 401 402 case 'q': 403 ++qflag; 404 break; 405 406 case 'r': 407 RFileName = optarg; 408 break; 409 410 case 'R': 411 Rflag = 0; 412 break; 413 414 case 's': { 415 char *end; 416 417 snaplen = strtol(optarg, &end, 0); 418 if (optarg == end || *end != '\0' 419 || snaplen < 0 || snaplen > 65535) 420 error("invalid snaplen %s", optarg); 421 else if (snaplen == 0) 422 snaplen = 65535; 423 break; 424 } 425 426 case 'S': 427 ++Sflag; 428 break; 429 430 case 't': 431 --tflag; 432 break; 433 434 case 'T': 435 if (strcasecmp(optarg, "vat") == 0) 436 packettype = PT_VAT; 437 else if (strcasecmp(optarg, "wb") == 0) 438 packettype = PT_WB; 439 else if (strcasecmp(optarg, "rpc") == 0) 440 packettype = PT_RPC; 441 else if (strcasecmp(optarg, "rtp") == 0) 442 packettype = PT_RTP; 443 else if (strcasecmp(optarg, "rtcp") == 0) 444 packettype = PT_RTCP; 445 else if (strcasecmp(optarg, "snmp") == 0) 446 packettype = PT_SNMP; 447 else if (strcasecmp(optarg, "cnfp") == 0) 448 packettype = PT_CNFP; 449 else 450 error("unknown packet type `%s'", optarg); 451 break; 452 453 case 'u': 454 ++uflag; 455 break; 456 457 case 'v': 458 ++vflag; 459 break; 460 461 case 'w': 462 WFileName = optarg; 463 break; 464 465 case 'x': 466 ++xflag; 467 break; 468 469 case 'X': 470 ++xflag; 471 ++Xflag; 472 break; 473 474 case 'y': 475 for (i = 0; dlt_choices[i].name; i++) { 476 if (!strcasecmp(dlt_choices[i].name + 477 sizeof("DLT_") - 1, 478 optarg)) { 479 dlt = dlt_choices[i].dlt; 480 dlt_name = dlt_choices[i].name; 481 break; 482 } 483 } 484 if (dlt < 0) { 485 error("invalid data link type %s", optarg); 486 } 487 break; 488 489 #ifdef YYDEBUG 490 case 'Y': 491 { 492 /* Undocumented flag */ 493 extern int yydebug; 494 yydebug = 1; 495 } 496 break; 497 #endif 498 default: 499 usage(); 500 /* NOTREACHED */ 501 } 502 503 if (aflag && nflag) 504 error("-a and -n options are incompatible"); 505 506 if (tflag > 0) 507 thiszone = gmt2local(0); 508 509 if (RFileName != NULL) { 510 /* 511 * We don't need network access, so set it back to the user id. 512 * Also, this prevents the user from reading anyone's 513 * trace file. 514 */ 515 setuid(getuid()); 516 517 pd = pcap_open_offline(RFileName, ebuf); 518 if (pd == NULL) 519 error("%s", ebuf); 520 localnet = 0; 521 netmask = 0; 522 if (fflag != 0) 523 error("-f and -r options are incompatible"); 524 } else { 525 if (device == NULL) { 526 device = pcap_lookupdev(ebuf); 527 if (device == NULL) 528 error("%s", ebuf); 529 } 530 *ebuf = '\0'; 531 pd = pcap_open_live(device, snaplen, !pflag, 1000, ebuf); 532 if (pd == NULL) 533 error("%s", ebuf); 534 else if (*ebuf) 535 warning("%s", ebuf); 536 if (Lflag) { 537 show_dlts_and_exit(pd); 538 } 539 if (dlt >= 0) { 540 if (pcap_set_datalink(pd, dlt) < 0) 541 error("%s", pcap_geterr(pd)); 542 (void)fprintf(stderr, "%s: data link type %s\n", 543 program_name, dlt_name); 544 (void)fflush(stderr); 545 } 546 i = pcap_snapshot(pd); 547 if (snaplen < i) { 548 warning("snaplen raised from %d to %d", snaplen, i); 549 snaplen = i; 550 } 551 if (pcap_lookupnet(device, &localnet, &netmask, ebuf) < 0) { 552 localnet = 0; 553 netmask = 0; 554 warning("%s", ebuf); 555 } 556 /* 557 * Let user own process after socket has been opened. 558 */ 559 setuid(getuid()); 560 } 561 if (infile) 562 cmdbuf = read_infile(infile); 563 else 564 cmdbuf = copy_argv(&argv[optind]); 565 566 if (pcap_compile(pd, &fcode, cmdbuf, Oflag, netmask) < 0) 567 error("%s", pcap_geterr(pd)); 568 if (dflag) { 569 bpf_dump(&fcode, dflag); 570 exit(0); 571 } 572 init_addrtoname(localnet, netmask); 573 574 (void)setsignal(SIGTERM, cleanup); 575 (void)setsignal(SIGINT, cleanup); 576 /* Cooperate with nohup(1) */ 577 if ((oldhandler = setsignal(SIGHUP, cleanup)) != SIG_DFL) 578 (void)setsignal(SIGHUP, oldhandler); 579 580 if (pcap_setfilter(pd, &fcode) < 0) 581 error("%s", pcap_geterr(pd)); 582 if (WFileName) { 583 pcap_dumper_t *p = pcap_dump_open(pd, WFileName); 584 if (p == NULL) 585 error("%s", pcap_geterr(pd)); 586 if (Cflag != 0) { 587 printer = dump_and_trunc; 588 dumpinfo.WFileName = WFileName; 589 dumpinfo.pd = pd; 590 dumpinfo.p = p; 591 pcap_userdata = (u_char *)&dumpinfo; 592 } else { 593 printer = pcap_dump; 594 pcap_userdata = (u_char *)p; 595 } 596 } else { 597 printer = lookup_printer(pcap_datalink(pd)); 598 pcap_userdata = 0; 599 #ifdef SIGINFO 600 (void)setsignal(SIGINFO, requestinfo); 601 #endif 602 } 603 if (RFileName == NULL) { 604 (void)fprintf(stderr, "%s: listening on %s\n", 605 program_name, device); 606 (void)fflush(stderr); 607 } 608 if (pcap_loop(pd, cnt, printer, pcap_userdata) < 0) { 609 (void)fprintf(stderr, "%s: pcap_loop: %s\n", 610 program_name, pcap_geterr(pd)); 611 exit(1); 612 } 613 if (RFileName == NULL) 614 info(1); 615 pcap_close(pd); 616 exit(0); 617 } 618 619 /* make a clean exit on interrupts */ 620 static RETSIGTYPE 621 cleanup(int signo) 622 { 623 624 /* Can't print the summary if reading from a savefile */ 625 if (pd != NULL && pcap_file(pd) == NULL) { 626 (void)fflush(stdout); 627 putc('\n', stderr); 628 info(1); 629 } 630 exit(0); 631 } 632 633 void 634 info(register int verbose) 635 { 636 struct pcap_stat stat; 637 638 if (pcap_stats(pd, &stat) < 0) { 639 (void)fprintf(stderr, "pcap_stats: %s\n", pcap_geterr(pd)); 640 return; 641 } 642 if (!verbose) 643 fprintf(stderr, "%s: ", program_name); 644 (void)fprintf(stderr, "%d packets received by filter", stat.ps_recv); 645 if (!verbose) 646 fputs(", ", stderr); 647 else 648 putc('\n', stderr); 649 (void)fprintf(stderr, "%d packets dropped by kernel\n", stat.ps_drop); 650 infoprint = 0; 651 } 652 653 static void 654 reverse(char *s) 655 { 656 int i, j, c; 657 658 for (i = 0, j = strlen(s) - 1; i < j; i++, j--) { 659 c = s[i]; 660 s[i] = s[j]; 661 s[j] = c; 662 } 663 } 664 665 666 static void 667 swebitoa(unsigned int n, char *s) 668 { 669 unsigned int i; 670 671 i = 0; 672 do { 673 s[i++] = n % 10 + '0'; 674 } while ((n /= 10) > 0); 675 676 s[i] = '\0'; 677 reverse(s); 678 } 679 680 static void 681 dump_and_trunc(u_char *user, const struct pcap_pkthdr *h, const u_char *sp) 682 { 683 struct dump_info *info; 684 static uint cnt = 2; 685 char *name; 686 687 info = (struct dump_info *)user; 688 689 /* 690 * XXX - this won't prevent capture files from getting 691 * larger than Cflag - the last packet written to the 692 * file could put it over Cflag. 693 */ 694 if (ftell((FILE *)info->p) > Cflag) { 695 name = (char *) malloc(strlen(info->WFileName) + 4); 696 if (name == NULL) 697 error("dump_and_trunc: malloc"); 698 strcpy(name, info->WFileName); 699 swebitoa(cnt, name + strlen(info->WFileName)); 700 cnt++; 701 pcap_dump_close(info->p); 702 info->p = pcap_dump_open(info->pd, name); 703 free(name); 704 if (info->p == NULL) 705 error("%s", pcap_geterr(pd)); 706 } 707 708 pcap_dump((u_char *)info->p, h, sp); 709 } 710 711 /* Like default_print() but data need not be aligned */ 712 void 713 default_print_unaligned(register const u_char *cp, register u_int length) 714 { 715 register u_int i, s; 716 register int nshorts; 717 718 if (Xflag) { 719 ascii_print(cp, length); 720 return; 721 } 722 nshorts = (u_int) length / sizeof(u_short); 723 i = 0; 724 while (--nshorts >= 0) { 725 if ((i++ % 8) == 0) 726 (void)printf("\n\t\t\t"); 727 s = *cp++; 728 (void)printf(" %02x%02x", s, *cp++); 729 } 730 if (length & 1) { 731 if ((i % 8) == 0) 732 (void)printf("\n\t\t\t"); 733 (void)printf(" %02x", *cp); 734 } 735 } 736 737 /* 738 * By default, print the packet out in hex. 739 */ 740 void 741 default_print(register const u_char *bp, register u_int length) 742 { 743 default_print_unaligned(bp, length); 744 } 745 746 #ifdef SIGINFO 747 RETSIGTYPE requestinfo(int signo) 748 { 749 if (infodelay) 750 ++infoprint; 751 else 752 info(0); 753 } 754 #endif 755 756 static void 757 usage(void) 758 { 759 extern char version[]; 760 extern char pcap_version[]; 761 762 (void)fprintf(stderr, "%s version %s\n", program_name, version); 763 (void)fprintf(stderr, "libpcap version %s\n", pcap_version); 764 (void)fprintf(stderr, 765 "Usage: %s [-adeflLnNOpqRStuvxX] [ -c count ] [ -C file_size ]\n", program_name); 766 (void)fprintf(stderr, 767 "\t\t[ -F file ] [ -i interface ] [ -r file ] [ -s snaplen ]\n"); 768 (void)fprintf(stderr, 769 "\t\t[ -T type ] [ -w file ] [ -E algo:secret ] [ -y datalinktype ]\n"); 770 (void)fprintf(stderr, 771 "\t\t[ expression ]\n"); 772 exit(1); 773 } 774