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[] _U_ = 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[] _U_ = 33 "@(#) $Header: /tcpdump/master/tcpdump/tcpdump.c,v 1.253 2005/01/27 18:30:36 hannes 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 <tcpdump-stdinc.h> 51 52 #ifdef WIN32 53 #include "getopt.h" 54 #include "w32_fzs.h" 55 extern int strcasecmp (const char *__s1, const char *__s2); 56 extern int SIZE_BUF; 57 #define off_t long 58 #define uint UINT 59 #endif /* WIN32 */ 60 61 #ifdef HAVE_SMI_H 62 #include <smi.h> 63 #endif 64 65 #include <pcap.h> 66 #include <signal.h> 67 #include <stdio.h> 68 #include <stdlib.h> 69 #include <string.h> 70 #ifndef WIN32 71 #include <pwd.h> 72 #include <grp.h> 73 #include <errno.h> 74 #endif /* WIN32 */ 75 76 #include "netdissect.h" 77 #include "interface.h" 78 #include "addrtoname.h" 79 #include "machdep.h" 80 #include "setsignal.h" 81 #include "gmt2local.h" 82 #include "pcap-missing.h" 83 84 netdissect_options Gndo; 85 netdissect_options *gndo = &Gndo; 86 87 /* 88 * Define the maximum number of files for the -C flag, and how many 89 * characters can be added to a filename for the -C flag (which 90 * should be enough to handle MAX_CFLAG - 1). 91 */ 92 #define MAX_CFLAG 1000000 93 #define MAX_CFLAG_CHARS 6 94 95 int dflag; /* print filter code */ 96 int Lflag; /* list available data link types and exit */ 97 98 static int infodelay; 99 static int infoprint; 100 101 char *program_name; 102 103 int32_t thiszone; /* seconds offset from gmt to local time */ 104 105 /* Forwards */ 106 static RETSIGTYPE cleanup(int); 107 static void usage(void) __attribute__((noreturn)); 108 static void show_dlts_and_exit(pcap_t *pd) __attribute__((noreturn)); 109 110 static void print_packet(u_char *, const struct pcap_pkthdr *, const u_char *); 111 static void ndo_default_print(netdissect_options *, const u_char *, u_int); 112 static void dump_packet_and_trunc(u_char *, const struct pcap_pkthdr *, const u_char *); 113 static void dump_packet(u_char *, const struct pcap_pkthdr *, const u_char *); 114 static void droproot(const char *, const char *); 115 static void ndo_error(netdissect_options *ndo, const char *fmt, ...); 116 static void ndo_warning(netdissect_options *ndo, const char *fmt, ...); 117 118 #ifdef SIGINFO 119 RETSIGTYPE requestinfo(int); 120 #endif 121 122 #if defined(USE_WIN32_MM_TIMER) 123 #include <MMsystem.h> 124 static UINT timer_id; 125 static void CALLBACK verbose_stats_dump(UINT, UINT, DWORD_PTR, DWORD_PTR, DWORD_PTR); 126 #elif defined(HAVE_ALARM) 127 static void verbose_stats_dump(int sig); 128 #endif 129 130 static void info(int); 131 static u_int packets_captured; 132 133 typedef u_int (*if_printer)(const struct pcap_pkthdr *, const u_char *); 134 135 struct printer { 136 if_printer f; 137 int type; 138 }; 139 140 static struct printer printers[] = { 141 { arcnet_if_print, DLT_ARCNET }, 142 #ifdef DLT_ARCNET_LINUX 143 { arcnet_linux_if_print, DLT_ARCNET_LINUX }, 144 #endif 145 { ether_if_print, DLT_EN10MB }, 146 { token_if_print, DLT_IEEE802 }, 147 #ifdef DLT_LANE8023 148 { lane_if_print, DLT_LANE8023 }, 149 #endif 150 #ifdef DLT_CIP 151 { cip_if_print, DLT_CIP }, 152 #endif 153 #ifdef DLT_ATM_CLIP 154 { cip_if_print, DLT_ATM_CLIP }, 155 #endif 156 { sl_if_print, DLT_SLIP }, 157 #ifdef DLT_SLIP_BSDOS 158 { sl_bsdos_if_print, DLT_SLIP_BSDOS }, 159 #endif 160 { ppp_if_print, DLT_PPP }, 161 #ifdef DLT_PPP_WITHDIRECTION 162 { ppp_if_print, DLT_PPP_WITHDIRECTION }, 163 #endif 164 #ifdef DLT_PPP_BSDOS 165 { ppp_bsdos_if_print, DLT_PPP_BSDOS }, 166 #endif 167 { fddi_if_print, DLT_FDDI }, 168 { null_if_print, DLT_NULL }, 169 #ifdef DLT_LOOP 170 { null_if_print, DLT_LOOP }, 171 #endif 172 { raw_if_print, DLT_RAW }, 173 { atm_if_print, DLT_ATM_RFC1483 }, 174 #ifdef DLT_C_HDLC 175 { chdlc_if_print, DLT_C_HDLC }, 176 #endif 177 #ifdef DLT_HDLC 178 { chdlc_if_print, DLT_HDLC }, 179 #endif 180 #ifdef DLT_PPP_SERIAL 181 { ppp_hdlc_if_print, DLT_PPP_SERIAL }, 182 #endif 183 #ifdef DLT_PPP_ETHER 184 { pppoe_if_print, DLT_PPP_ETHER }, 185 #endif 186 #ifdef DLT_LINUX_SLL 187 { sll_if_print, DLT_LINUX_SLL }, 188 #endif 189 #ifdef DLT_IEEE802_11 190 { ieee802_11_if_print, DLT_IEEE802_11}, 191 #endif 192 #ifdef DLT_LTALK 193 { ltalk_if_print, DLT_LTALK }, 194 #endif 195 #ifdef DLT_PFLOG 196 { pflog_if_print, DLT_PFLOG }, 197 #endif 198 #ifdef DLT_FR 199 { fr_if_print, DLT_FR }, 200 #endif 201 #ifdef DLT_FRELAY 202 { fr_if_print, DLT_FRELAY }, 203 #endif 204 #ifdef DLT_SUNATM 205 { sunatm_if_print, DLT_SUNATM }, 206 #endif 207 #ifdef DLT_IP_OVER_FC 208 { ipfc_if_print, DLT_IP_OVER_FC }, 209 #endif 210 #ifdef DLT_PRISM_HEADER 211 { prism_if_print, DLT_PRISM_HEADER }, 212 #endif 213 #ifdef DLT_IEEE802_11_RADIO 214 { ieee802_11_radio_if_print, DLT_IEEE802_11_RADIO }, 215 #endif 216 #ifdef DLT_ENC 217 { enc_if_print, DLT_ENC }, 218 #endif 219 #ifdef DLT_SYMANTEC_FIREWALL 220 { symantec_if_print, DLT_SYMANTEC_FIREWALL }, 221 #endif 222 #ifdef DLT_APPLE_IP_OVER_IEEE1394 223 { ap1394_if_print, DLT_APPLE_IP_OVER_IEEE1394 }, 224 #endif 225 #ifdef DLT_JUNIPER_ATM1 226 { juniper_atm1_print, DLT_JUNIPER_ATM1 }, 227 #endif 228 #ifdef DLT_JUNIPER_ATM2 229 { juniper_atm2_print, DLT_JUNIPER_ATM2 }, 230 #endif 231 #ifdef DLT_JUNIPER_MLFR 232 { juniper_mlfr_print, DLT_JUNIPER_MLFR }, 233 #endif 234 #ifdef DLT_JUNIPER_MLPPP 235 { juniper_mlppp_print, DLT_JUNIPER_MLPPP }, 236 #endif 237 { NULL, 0 }, 238 }; 239 240 static if_printer 241 lookup_printer(int type) 242 { 243 struct printer *p; 244 245 for (p = printers; p->f; ++p) 246 if (type == p->type) 247 return p->f; 248 249 return NULL; 250 /* NOTREACHED */ 251 } 252 253 static pcap_t *pd; 254 255 extern int optind; 256 extern int opterr; 257 extern char *optarg; 258 259 struct print_info { 260 if_printer printer; 261 }; 262 263 struct dump_info { 264 char *WFileName; 265 pcap_t *pd; 266 pcap_dumper_t *p; 267 }; 268 269 static void 270 show_dlts_and_exit(pcap_t *pd) 271 { 272 int n_dlts; 273 int *dlts = 0; 274 const char *dlt_name; 275 276 n_dlts = pcap_list_datalinks(pd, &dlts); 277 if (n_dlts < 0) 278 error("%s", pcap_geterr(pd)); 279 else if (n_dlts == 0 || !dlts) 280 error("No data link types."); 281 282 (void) fprintf(stderr, "Data link types (use option -y to set):\n"); 283 284 while (--n_dlts >= 0) { 285 dlt_name = pcap_datalink_val_to_name(dlts[n_dlts]); 286 if (dlt_name != NULL) { 287 (void) fprintf(stderr, " %s (%s)", dlt_name, 288 pcap_datalink_val_to_description(dlts[n_dlts])); 289 290 /* 291 * OK, does tcpdump handle that type? 292 */ 293 if (lookup_printer(dlts[n_dlts]) == NULL) 294 (void) fprintf(stderr, " (not supported)"); 295 putchar('\n'); 296 } else { 297 (void) fprintf(stderr, " DLT %d (not supported)\n", 298 dlts[n_dlts]); 299 } 300 } 301 free(dlts); 302 exit(0); 303 } 304 305 /* 306 * Set up flags that might or might not be supported depending on the 307 * version of libpcap we're using. 308 */ 309 #ifdef WIN32 310 #define B_FLAG "B:" 311 #define B_FLAG_USAGE " [ -B size ]" 312 #else /* WIN32 */ 313 #define B_FLAG 314 #define B_FLAG_USAGE 315 #endif /* WIN32 */ 316 317 #ifdef HAVE_PCAP_FINDALLDEVS 318 #ifndef HAVE_PCAP_IF_T 319 #undef HAVE_PCAP_FINDALLDEVS 320 #endif 321 #endif 322 323 #ifdef HAVE_PCAP_FINDALLDEVS 324 #define D_FLAG "D" 325 #else 326 #define D_FLAG 327 #endif 328 329 #ifdef HAVE_PCAP_DUMP_FLUSH 330 #define U_FLAG "U" 331 #else 332 #define U_FLAG 333 #endif 334 335 #ifndef WIN32 336 /* Drop root privileges and chroot if necessary */ 337 static void 338 droproot(const char *username, const char *chroot_dir) 339 { 340 struct passwd *pw = NULL; 341 342 if (chroot_dir && !username) { 343 fprintf(stderr, "tcpdump: Chroot without dropping root is insecure\n"); 344 exit(1); 345 } 346 347 pw = getpwnam(username); 348 if (pw) { 349 if (chroot_dir) { 350 if (chroot(chroot_dir) != 0 || chdir ("/") != 0) { 351 fprintf(stderr, "tcpdump: Couldn't chroot/chdir to '%.64s': %s\n", 352 chroot_dir, pcap_strerror(errno)); 353 exit(1); 354 } 355 } 356 if (initgroups(pw->pw_name, pw->pw_gid) != 0 || 357 setgid(pw->pw_gid) != 0 || setuid(pw->pw_uid) != 0) { 358 fprintf(stderr, "tcpdump: Couldn't change to '%.32s' uid=%lu gid=%lu: %s\n", 359 username, 360 (unsigned long)pw->pw_uid, 361 (unsigned long)pw->pw_gid, 362 pcap_strerror(errno)); 363 exit(1); 364 } 365 } 366 else { 367 fprintf(stderr, "tcpdump: Couldn't find user '%.32s'\n", 368 username); 369 exit(1); 370 } 371 } 372 #endif /* WIN32 */ 373 374 static int 375 getWflagChars(int x) 376 { 377 int c = 0; 378 379 x -= 1; 380 while (x > 0) { 381 c += 1; 382 x /= 10; 383 } 384 385 return c; 386 } 387 388 389 static void 390 MakeFilename(char *buffer, char *orig_name, int cnt, int max_chars) 391 { 392 if (cnt == 0 && max_chars == 0) 393 strcpy(buffer, orig_name); 394 else 395 sprintf(buffer, "%s%0*d", orig_name, max_chars, cnt); 396 } 397 398 static int tcpdump_printf(netdissect_options *ndo _U_, 399 const char *fmt, ...) 400 { 401 402 va_list args; 403 int ret; 404 405 va_start(args, fmt); 406 ret=vfprintf(stdout, fmt, args); 407 va_end(args); 408 409 return ret; 410 } 411 412 int 413 main(int argc, char **argv) 414 { 415 register int cnt, op, i; 416 bpf_u_int32 localnet, netmask; 417 register char *cp, *infile, *cmdbuf, *device, *RFileName, *WFileName, *WFileNameAlt; 418 pcap_handler callback; 419 int type; 420 struct bpf_program fcode; 421 #ifndef WIN32 422 RETSIGTYPE (*oldhandler)(int); 423 #endif 424 struct print_info printinfo; 425 struct dump_info dumpinfo; 426 u_char *pcap_userdata; 427 char ebuf[PCAP_ERRBUF_SIZE]; 428 char *username = NULL; 429 char *chroot_dir = NULL; 430 #ifdef HAVE_PCAP_FINDALLDEVS 431 pcap_if_t *devpointer; 432 int devnum; 433 #endif 434 int status; 435 #ifdef WIN32 436 u_int UserBufferSize = 1000000; 437 if(wsockinit() != 0) return 1; 438 #endif /* WIN32 */ 439 440 gndo->ndo_Oflag=1; 441 gndo->ndo_Rflag=1; 442 gndo->ndo_dlt=-1; 443 gndo->ndo_default_print=ndo_default_print; 444 gndo->ndo_printf=tcpdump_printf; 445 gndo->ndo_error=ndo_error; 446 gndo->ndo_warning=ndo_warning; 447 gndo->ndo_snaplen = DEFAULT_SNAPLEN; 448 449 cnt = -1; 450 device = NULL; 451 infile = NULL; 452 RFileName = NULL; 453 WFileName = NULL; 454 if ((cp = strrchr(argv[0], '/')) != NULL) 455 program_name = cp + 1; 456 else 457 program_name = argv[0]; 458 459 if (abort_on_misalignment(ebuf, sizeof(ebuf)) < 0) 460 error("%s", ebuf); 461 462 #ifdef LIBSMI 463 smiInit("tcpdump"); 464 #endif 465 466 opterr = 0; 467 while ( 468 (op = getopt(argc, argv, "aA" B_FLAG "c:C:d" D_FLAG "eE:fF:i:lLm:M:nNOpqr:Rs:StT:u" U_FLAG "vw:W:xXy:YZ:")) != -1) 469 switch (op) { 470 471 case 'a': 472 /* compatibility for old -a */ 473 break; 474 475 case 'A': 476 ++xflag; 477 ++Xflag; 478 ++Aflag; 479 break; 480 481 #ifdef WIN32 482 case 'B': 483 UserBufferSize = atoi(optarg)*1024; 484 if (UserBufferSize < 0) 485 error("invalid packet buffer size %s", optarg); 486 break; 487 #endif /* WIN32 */ 488 489 case 'c': 490 cnt = atoi(optarg); 491 if (cnt <= 0) 492 error("invalid packet count %s", optarg); 493 break; 494 495 case 'C': 496 Cflag = atoi(optarg) * 1000000; 497 if (Cflag < 0) 498 error("invalid file size %s", optarg); 499 break; 500 501 case 'd': 502 ++dflag; 503 break; 504 505 #ifdef HAVE_PCAP_FINDALLDEVS 506 case 'D': 507 if (pcap_findalldevs(&devpointer, ebuf) < 0) 508 error("%s", ebuf); 509 else { 510 for (i = 0; devpointer != 0; i++) { 511 printf("%d.%s", i+1, devpointer->name); 512 if (devpointer->description != NULL) 513 printf(" (%s)", devpointer->description); 514 printf("\n"); 515 devpointer = devpointer->next; 516 } 517 } 518 return 0; 519 #endif /* HAVE_PCAP_FINDALLDEVS */ 520 521 case 'L': 522 Lflag++; 523 break; 524 525 case 'e': 526 ++eflag; 527 break; 528 529 case 'E': 530 #ifndef HAVE_LIBCRYPTO 531 warning("crypto code not compiled in"); 532 #endif 533 gndo->ndo_espsecret = optarg; 534 break; 535 536 case 'f': 537 ++fflag; 538 break; 539 540 case 'F': 541 infile = optarg; 542 break; 543 544 case 'i': 545 if (optarg[0] == '0' && optarg[1] == 0) 546 error("Invalid adapter index"); 547 548 #ifdef HAVE_PCAP_FINDALLDEVS 549 /* 550 * If the argument is a number, treat it as 551 * an index into the list of adapters, as 552 * printed by "tcpdump -D". 553 * 554 * This should be OK on UNIX systems, as interfaces 555 * shouldn't have names that begin with digits. 556 * It can be useful on Windows, where more than 557 * one interface can have the same name. 558 */ 559 if ((devnum = atoi(optarg)) != 0) { 560 if (devnum < 0) 561 error("Invalid adapter index"); 562 563 if (pcap_findalldevs(&devpointer, ebuf) < 0) 564 error("%s", ebuf); 565 else { 566 for (i = 0; i < devnum-1; i++){ 567 devpointer = devpointer->next; 568 if (devpointer == NULL) 569 error("Invalid adapter index"); 570 } 571 } 572 device = devpointer->name; 573 break; 574 } 575 #endif /* HAVE_PCAP_FINDALLDEVS */ 576 device = optarg; 577 break; 578 579 case 'l': 580 #ifdef WIN32 581 /* 582 * _IOLBF is the same as _IOFBF in Microsoft's C 583 * libraries; the only alternative they offer 584 * is _IONBF. 585 * 586 * XXX - this should really be checking for MSVC++, 587 * not WIN32, if, for example, MinGW has its own 588 * C library that is more UNIX-compatible. 589 */ 590 setvbuf(stdout, NULL, _IONBF, 0); 591 #else /* WIN32 */ 592 #ifdef HAVE_SETLINEBUF 593 setlinebuf(stdout); 594 #else 595 setvbuf(stdout, NULL, _IOLBF, 0); 596 #endif 597 #endif /* WIN32 */ 598 break; 599 600 case 'n': 601 ++nflag; 602 break; 603 604 case 'N': 605 ++Nflag; 606 break; 607 608 case 'm': 609 #ifdef LIBSMI 610 if (smiLoadModule(optarg) == 0) { 611 error("could not load MIB module %s", optarg); 612 } 613 sflag = 1; 614 #else 615 (void)fprintf(stderr, "%s: ignoring option `-m %s' ", 616 program_name, optarg); 617 (void)fprintf(stderr, "(no libsmi support)\n"); 618 #endif 619 break; 620 621 case 'M': 622 /* TCP-MD5 shared secret */ 623 #ifndef HAVE_LIBCRYPTO 624 warning("crypto code not compiled in"); 625 #endif 626 tcpmd5secret = optarg; 627 break; 628 629 case 'O': 630 Oflag = 0; 631 break; 632 633 case 'p': 634 ++pflag; 635 break; 636 637 case 'q': 638 ++qflag; 639 break; 640 641 case 'r': 642 RFileName = optarg; 643 break; 644 645 case 'R': 646 Rflag = 0; 647 break; 648 649 case 's': { 650 char *end; 651 652 snaplen = strtol(optarg, &end, 0); 653 if (optarg == end || *end != '\0' 654 || snaplen < 0 || snaplen > 65535) 655 error("invalid snaplen %s", optarg); 656 else if (snaplen == 0) 657 snaplen = 65535; 658 break; 659 } 660 661 case 'S': 662 ++Sflag; 663 break; 664 665 case 't': 666 ++tflag; 667 break; 668 669 case 'T': 670 if (strcasecmp(optarg, "vat") == 0) 671 packettype = PT_VAT; 672 else if (strcasecmp(optarg, "wb") == 0) 673 packettype = PT_WB; 674 else if (strcasecmp(optarg, "rpc") == 0) 675 packettype = PT_RPC; 676 else if (strcasecmp(optarg, "rtp") == 0) 677 packettype = PT_RTP; 678 else if (strcasecmp(optarg, "rtcp") == 0) 679 packettype = PT_RTCP; 680 else if (strcasecmp(optarg, "snmp") == 0) 681 packettype = PT_SNMP; 682 else if (strcasecmp(optarg, "cnfp") == 0) 683 packettype = PT_CNFP; 684 else if (strcasecmp(optarg, "tftp") == 0) 685 packettype = PT_TFTP; 686 else if (strcasecmp(optarg, "aodv") == 0) 687 packettype = PT_AODV; 688 else 689 error("unknown packet type `%s'", optarg); 690 break; 691 692 case 'u': 693 ++uflag; 694 break; 695 696 #ifdef HAVE_PCAP_DUMP_FLUSH 697 case 'U': 698 ++Uflag; 699 break; 700 #endif 701 702 case 'v': 703 ++vflag; 704 break; 705 706 case 'w': 707 WFileName = optarg; 708 break; 709 710 case 'W': 711 Wflag = atoi(optarg); 712 if (Wflag < 0) 713 error("invalid number of output files %s", optarg); 714 WflagChars = getWflagChars(Wflag); 715 break; 716 717 case 'x': 718 ++xflag; 719 break; 720 721 case 'X': 722 ++Xflag; 723 break; 724 725 case 'y': 726 gndo->ndo_dltname = optarg; 727 gndo->ndo_dlt = 728 pcap_datalink_name_to_val(gndo->ndo_dltname); 729 if (gndo->ndo_dlt < 0) 730 error("invalid data link type %s", gndo->ndo_dltname); 731 break; 732 733 #if defined(HAVE_PCAP_DEBUG) || defined(HAVE_YYDEBUG) 734 case 'Y': 735 { 736 /* Undocumented flag */ 737 #ifdef HAVE_PCAP_DEBUG 738 extern int pcap_debug; 739 pcap_debug = 1; 740 #else 741 extern int yydebug; 742 yydebug = 1; 743 #endif 744 } 745 break; 746 #endif 747 case 'Z': 748 if (optarg) { 749 username = strdup(optarg); 750 } 751 else { 752 usage(); 753 /* NOTREACHED */ 754 } 755 break; 756 757 default: 758 usage(); 759 /* NOTREACHED */ 760 } 761 762 switch (tflag) { 763 764 case 0: /* Default */ 765 case 4: /* Default + Date*/ 766 thiszone = gmt2local(0); 767 break; 768 769 case 1: /* No time stamp */ 770 case 2: /* Unix timeval style */ 771 case 3: /* Microseconds since previous packet */ 772 break; 773 774 default: /* Not supported */ 775 error("only -t, -tt, -ttt, and -tttt are supported"); 776 break; 777 } 778 779 #ifdef WITH_CHROOT 780 /* if run as root, prepare for chrooting */ 781 if (getuid() == 0 || geteuid() == 0) { 782 /* future extensibility for cmd-line arguments */ 783 if (!chroot_dir) 784 chroot_dir = WITH_CHROOT; 785 } 786 #endif 787 788 #ifdef WITH_USER 789 /* if run as root, prepare for dropping root privileges */ 790 if (getuid() == 0 || geteuid() == 0) { 791 /* Run with '-Z root' to restore old behaviour */ 792 if (!username) 793 username = WITH_USER; 794 } 795 #endif 796 797 if (RFileName != NULL) { 798 int dlt; 799 const char *dlt_name; 800 801 #ifndef WIN32 802 /* 803 * We don't need network access, so relinquish any set-UID 804 * or set-GID privileges we have (if any). 805 * 806 * We do *not* want set-UID privileges when opening a 807 * trace file, as that might let the user read other 808 * people's trace files (especially if we're set-UID 809 * root). 810 */ 811 if (setgid(getgid()) != 0 || setuid(getuid()) != 0 ) 812 fprintf(stderr, "Warning: setgid/setuid failed !\n"); 813 #endif /* WIN32 */ 814 pd = pcap_open_offline(RFileName, ebuf); 815 if (pd == NULL) 816 error("%s", ebuf); 817 dlt = pcap_datalink(pd); 818 dlt_name = pcap_datalink_val_to_name(dlt); 819 if (dlt_name == NULL) { 820 fprintf(stderr, "reading from file %s, link-type %u\n", 821 RFileName, dlt); 822 } else { 823 fprintf(stderr, 824 "reading from file %s, link-type %s (%s)\n", 825 RFileName, dlt_name, 826 pcap_datalink_val_to_description(dlt)); 827 } 828 localnet = 0; 829 netmask = 0; 830 if (fflag != 0) 831 error("-f and -r options are incompatible"); 832 } else { 833 if (device == NULL) { 834 device = pcap_lookupdev(ebuf); 835 if (device == NULL) 836 error("%s", ebuf); 837 } 838 #ifdef WIN32 839 if(strlen(device) == 1) //we assume that an ASCII string is always longer than 1 char 840 { //a Unicode string has a \0 as second byte (so strlen() is 1) 841 fprintf(stderr, "%s: listening on %ws\n", program_name, device); 842 } 843 else 844 { 845 fprintf(stderr, "%s: listening on %s\n", program_name, device); 846 } 847 848 fflush(stderr); 849 #endif /* WIN32 */ 850 *ebuf = '\0'; 851 pd = pcap_open_live(device, snaplen, !pflag, 1000, ebuf); 852 if (pd == NULL) 853 error("%s", ebuf); 854 else if (*ebuf) 855 warning("%s", ebuf); 856 /* 857 * Let user own process after socket has been opened. 858 */ 859 #ifndef WIN32 860 if (setgid(getgid()) != 0 || setuid(getuid()) != 0) 861 fprintf(stderr, "Warning: setgid/setuid failed !\n"); 862 #endif /* WIN32 */ 863 #ifdef WIN32 864 if(UserBufferSize != 1000000) 865 if(pcap_setbuff(pd, UserBufferSize)==-1){ 866 error("%s", pcap_geterr(pd)); 867 } 868 #endif /* WIN32 */ 869 if (Lflag) 870 show_dlts_and_exit(pd); 871 if (gndo->ndo_dlt >= 0) { 872 #ifdef HAVE_PCAP_SET_DATALINK 873 if (pcap_set_datalink(pd, gndo->ndo_dlt) < 0) 874 error("%s", pcap_geterr(pd)); 875 #else 876 /* 877 * We don't actually support changing the 878 * data link type, so we only let them 879 * set it to what it already is. 880 */ 881 if (gndo->ndo_dlt != pcap_datalink(pd)) { 882 error("%s is not one of the DLTs supported by this device\n", 883 gndo->ndo_dltname); 884 } 885 #endif 886 (void)fprintf(stderr, "%s: data link type %s\n", 887 program_name, gndo->ndo_dltname); 888 (void)fflush(stderr); 889 } 890 i = pcap_snapshot(pd); 891 if (snaplen < i) { 892 warning("snaplen raised from %d to %d", snaplen, i); 893 snaplen = i; 894 } 895 if (pcap_lookupnet(device, &localnet, &netmask, ebuf) < 0) { 896 localnet = 0; 897 netmask = 0; 898 warning("%s", ebuf); 899 } 900 } 901 if (infile) 902 cmdbuf = read_infile(infile); 903 else 904 cmdbuf = copy_argv(&argv[optind]); 905 906 if (pcap_compile(pd, &fcode, cmdbuf, Oflag, netmask) < 0) 907 error("%s", pcap_geterr(pd)); 908 if (dflag) { 909 bpf_dump(&fcode, dflag); 910 pcap_close(pd); 911 exit(0); 912 } 913 init_addrtoname(localnet, netmask); 914 915 #ifndef WIN32 916 (void)setsignal(SIGPIPE, cleanup); 917 #endif /* WIN32 */ 918 (void)setsignal(SIGTERM, cleanup); 919 (void)setsignal(SIGINT, cleanup); 920 /* Cooperate with nohup(1) */ 921 #ifndef WIN32 922 if ((oldhandler = setsignal(SIGHUP, cleanup)) != SIG_DFL) 923 (void)setsignal(SIGHUP, oldhandler); 924 #endif /* WIN32 */ 925 926 if (pcap_setfilter(pd, &fcode) < 0) 927 error("%s", pcap_geterr(pd)); 928 if (WFileName) { 929 pcap_dumper_t *p; 930 931 WFileNameAlt = (char *)malloc(strlen(WFileName) + MAX_CFLAG_CHARS + 1); 932 if (WFileNameAlt == NULL) 933 error("malloc of WFileNameAlt"); 934 MakeFilename(WFileNameAlt, WFileName, 0, WflagChars); 935 p = pcap_dump_open(pd, WFileNameAlt); 936 if (p == NULL) 937 error("%s", pcap_geterr(pd)); 938 if (Cflag != 0) { 939 callback = dump_packet_and_trunc; 940 dumpinfo.WFileName = WFileName; 941 dumpinfo.pd = pd; 942 dumpinfo.p = p; 943 pcap_userdata = (u_char *)&dumpinfo; 944 } else { 945 callback = dump_packet; 946 pcap_userdata = (u_char *)p; 947 } 948 } else { 949 type = pcap_datalink(pd); 950 printinfo.printer = lookup_printer(type); 951 if (printinfo.printer == NULL) { 952 gndo->ndo_dltname = pcap_datalink_val_to_name(type); 953 if (gndo->ndo_dltname != NULL) 954 error("unsupported data link type %s", 955 gndo->ndo_dltname); 956 else 957 error("unsupported data link type %d", type); 958 } 959 callback = print_packet; 960 pcap_userdata = (u_char *)&printinfo; 961 } 962 #ifndef WIN32 963 /* 964 * We cannot do this earlier, because we want to be able to open 965 * the file (if done) for writing before giving up permissions. 966 */ 967 if (getuid() == 0 || geteuid() == 0) { 968 if (username || chroot_dir) 969 droproot(username, chroot_dir); 970 } 971 #endif /* WIN32 */ 972 #ifdef SIGINFO 973 (void)setsignal(SIGINFO, requestinfo); 974 #endif 975 976 if (vflag > 0 && WFileName) { 977 /* 978 * When capturing to a file, "-v" means tcpdump should, 979 * every 10 secodns, "v"erbosely report the number of 980 * packets captured. 981 */ 982 #ifdef USE_WIN32_MM_TIMER 983 /* call verbose_stats_dump() each 1000 +/-100msec */ 984 timer_id = timeSetEvent(1000, 100, verbose_stats_dump, 0, TIME_PERIODIC); 985 setvbuf(stderr, NULL, _IONBF, 0); 986 #elif defined(HAVE_ALARM) 987 (void)setsignal(SIGALRM, verbose_stats_dump); 988 alarm(1); 989 #endif 990 } 991 992 #ifndef WIN32 993 if (RFileName == NULL) { 994 int dlt; 995 const char *dlt_name; 996 997 if (!vflag && !WFileName) { 998 (void)fprintf(stderr, 999 "%s: verbose output suppressed, use -v or -vv for full protocol decode\n", 1000 program_name); 1001 } else 1002 (void)fprintf(stderr, "%s: ", program_name); 1003 dlt = pcap_datalink(pd); 1004 dlt_name = pcap_datalink_val_to_name(dlt); 1005 if (dlt_name == NULL) { 1006 (void)fprintf(stderr, "listening on %s, link-type %u, capture size %u bytes\n", 1007 device, dlt, snaplen); 1008 } else { 1009 (void)fprintf(stderr, "listening on %s, link-type %s (%s), capture size %u bytes\n", 1010 device, dlt_name, 1011 pcap_datalink_val_to_description(dlt), snaplen); 1012 } 1013 (void)fflush(stderr); 1014 } 1015 #endif /* WIN32 */ 1016 status = pcap_loop(pd, cnt, callback, pcap_userdata); 1017 if (WFileName == NULL) { 1018 /* 1019 * We're printing packets. Flush the printed output, 1020 * so it doesn't get intermingled with error output. 1021 */ 1022 if (status == -2) { 1023 /* 1024 * We got interrupted, so perhaps we didn't 1025 * manage to finish a line we were printing. 1026 * Print an extra newline, just in case. 1027 */ 1028 putchar('\n'); 1029 } 1030 (void)fflush(stdout); 1031 } 1032 if (status == -1) { 1033 /* 1034 * Error. Report it. 1035 */ 1036 (void)fprintf(stderr, "%s: pcap_loop: %s\n", 1037 program_name, pcap_geterr(pd)); 1038 } 1039 if (RFileName == NULL) { 1040 /* 1041 * We're doing a live capture. Report the capture 1042 * statistics. 1043 */ 1044 info(1); 1045 } 1046 pcap_close(pd); 1047 exit(status == -1 ? 1 : 0); 1048 } 1049 1050 /* make a clean exit on interrupts */ 1051 static RETSIGTYPE 1052 cleanup(int signo _U_) 1053 { 1054 #ifdef USE_WIN32_MM_TIMER 1055 if (timer_id) 1056 timeKillEvent(timer_id); 1057 timer_id = 0; 1058 #elif defined(HAVE_ALARM) 1059 alarm(0); 1060 #endif 1061 1062 #ifdef HAVE_PCAP_BREAKLOOP 1063 /* 1064 * We have "pcap_breakloop()"; use it, so that we do as little 1065 * as possible in the signal handler (it's probably not safe 1066 * to do anything with standard I/O streams in a signal handler - 1067 * the ANSI C standard doesn't say it is). 1068 */ 1069 pcap_breakloop(pd); 1070 #else 1071 /* 1072 * We don't have "pcap_breakloop()"; this isn't safe, but 1073 * it's the best we can do. Print the summary if we're 1074 * not reading from a savefile - i.e., if we're doing a 1075 * live capture - and exit. 1076 */ 1077 if (pd != NULL && pcap_file(pd) == NULL) { 1078 /* 1079 * We got interrupted, so perhaps we didn't 1080 * manage to finish a line we were printing. 1081 * Print an extra newline, just in case. 1082 */ 1083 putchar('\n'); 1084 (void)fflush(stdout); 1085 info(1); 1086 } 1087 exit(0); 1088 #endif 1089 } 1090 1091 static void 1092 info(register int verbose) 1093 { 1094 struct pcap_stat stat; 1095 1096 if (pcap_stats(pd, &stat) < 0) { 1097 (void)fprintf(stderr, "pcap_stats: %s\n", pcap_geterr(pd)); 1098 return; 1099 } 1100 1101 if (!verbose) 1102 fprintf(stderr, "%s: ", program_name); 1103 1104 (void)fprintf(stderr, "%u packets captured", packets_captured); 1105 if (!verbose) 1106 fputs(", ", stderr); 1107 else 1108 putc('\n', stderr); 1109 (void)fprintf(stderr, "%d packets received by filter", stat.ps_recv); 1110 if (!verbose) 1111 fputs(", ", stderr); 1112 else 1113 putc('\n', stderr); 1114 (void)fprintf(stderr, "%d packets dropped by kernel\n", stat.ps_drop); 1115 infoprint = 0; 1116 } 1117 1118 static void 1119 dump_packet_and_trunc(u_char *user, const struct pcap_pkthdr *h, const u_char *sp) 1120 { 1121 struct dump_info *dump_info; 1122 char *name; 1123 1124 ++packets_captured; 1125 1126 ++infodelay; 1127 1128 dump_info = (struct dump_info *)user; 1129 1130 /* 1131 * XXX - this won't prevent capture files from getting 1132 * larger than Cflag - the last packet written to the 1133 * file could put it over Cflag. 1134 */ 1135 if (ftell((FILE *)dump_info->p) > Cflag) { 1136 /* 1137 * Close the current file and open a new one. 1138 */ 1139 pcap_dump_close(dump_info->p); 1140 Cflag_count++; 1141 if (Wflag > 0) { 1142 if (Cflag_count >= Wflag) 1143 Cflag_count = 0; 1144 } else { 1145 if (Cflag_count >= MAX_CFLAG) 1146 error("too many output files"); 1147 } 1148 name = (char *)malloc(strlen(dump_info->WFileName) + MAX_CFLAG_CHARS + 1); 1149 if (name == NULL) 1150 error("dump_packet_and_trunc: malloc"); 1151 MakeFilename(name, dump_info->WFileName, Cflag_count, WflagChars); 1152 dump_info->p = pcap_dump_open(dump_info->pd, name); 1153 free(name); 1154 if (dump_info->p == NULL) 1155 error("%s", pcap_geterr(pd)); 1156 } 1157 1158 pcap_dump((u_char *)dump_info->p, h, sp); 1159 #ifdef HAVE_PCAP_DUMP_FLUSH 1160 if (Uflag) 1161 pcap_dump_flush(dump_info->p); 1162 #endif 1163 1164 --infodelay; 1165 if (infoprint) 1166 info(0); 1167 } 1168 1169 static void 1170 dump_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *sp) 1171 { 1172 ++packets_captured; 1173 1174 ++infodelay; 1175 1176 pcap_dump(user, h, sp); 1177 #ifdef HAVE_PCAP_DUMP_FLUSH 1178 if (Uflag) 1179 pcap_dump_flush((pcap_dumper_t *)user); 1180 #endif 1181 1182 --infodelay; 1183 if (infoprint) 1184 info(0); 1185 } 1186 1187 static void 1188 print_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *sp) 1189 { 1190 struct print_info *print_info; 1191 u_int hdrlen; 1192 1193 ++packets_captured; 1194 1195 ++infodelay; 1196 ts_print(&h->ts); 1197 1198 print_info = (struct print_info *)user; 1199 1200 /* 1201 * Some printers want to check that they're not walking off the 1202 * end of the packet. 1203 * Rather than pass it all the way down, we set this global. 1204 */ 1205 snapend = sp + h->caplen; 1206 1207 hdrlen = (*print_info->printer)(h, sp); 1208 if (xflag) { 1209 /* 1210 * Print the raw packet data. 1211 */ 1212 if (xflag > 1) { 1213 /* 1214 * Include the link-layer header. 1215 */ 1216 hex_print("\n\t", sp, h->caplen); 1217 } else { 1218 /* 1219 * Don't include the link-layer header - and if 1220 * we have nothing past the link-layer header, 1221 * print nothing. 1222 */ 1223 if (h->caplen > hdrlen) 1224 hex_print("\n\t", sp + hdrlen, 1225 h->caplen - hdrlen); 1226 } 1227 } else if (Xflag) { 1228 /* 1229 * Print the raw packet data. 1230 */ 1231 if (Xflag > 1) { 1232 /* 1233 * Include the link-layer header. 1234 */ 1235 ascii_print("\n\t", sp, h->caplen); 1236 } else { 1237 /* 1238 * Don't include the link-layer header - and if 1239 * we have nothing past the link-layer header, 1240 * print nothing. 1241 */ 1242 if (h->caplen > hdrlen) 1243 ascii_print("\n\t", sp + hdrlen, 1244 h->caplen - hdrlen); 1245 } 1246 } 1247 1248 putchar('\n'); 1249 1250 --infodelay; 1251 if (infoprint) 1252 info(0); 1253 } 1254 1255 #ifdef WIN32 1256 /* 1257 * XXX - there should really be libpcap calls to get the version 1258 * number as a string (the string would be generated from #defines 1259 * at run time, so that it's not generated from string constants 1260 * in the library, as, on many UNIX systems, those constants would 1261 * be statically linked into the application executable image, and 1262 * would thus reflect the version of libpcap on the system on 1263 * which the application was *linked*, not the system on which it's 1264 * *running*. 1265 * 1266 * That routine should be documented, unlike the "version[]" 1267 * string, so that UNIX vendors providing their own libpcaps 1268 * don't omit it (as a couple of vendors have...). 1269 * 1270 * Packet.dll should perhaps also export a routine to return the 1271 * version number of the Packet.dll code, to supply the 1272 * "Wpcap_version" information on Windows. 1273 */ 1274 char WDversion[]="current-cvs.tcpdump.org"; 1275 #if !defined(HAVE_GENERATED_VERSION) 1276 char version[]="current-cvs.tcpdump.org"; 1277 #endif 1278 char pcap_version[]="current-cvs.tcpdump.org"; 1279 char Wpcap_version[]="3.1"; 1280 #endif 1281 1282 /* 1283 * By default, print the specified data out in hex. 1284 */ 1285 static void 1286 ndo_default_print(netdissect_options *ndo _U_, const u_char *bp, u_int length) 1287 { 1288 ascii_print("\n\t", bp, length); /* pass on lf and identation string */ 1289 } 1290 1291 void 1292 default_print(const u_char *bp, u_int length) 1293 { 1294 ndo_default_print(gndo, bp, length); 1295 } 1296 1297 #ifdef SIGINFO 1298 RETSIGTYPE requestinfo(int signo _U_) 1299 { 1300 if (infodelay) 1301 ++infoprint; 1302 else 1303 info(0); 1304 } 1305 #endif 1306 1307 /* 1308 * Called once each second in verbose mode while dumping to file 1309 */ 1310 #ifdef USE_WIN32_MM_TIMER 1311 void CALLBACK verbose_stats_dump (UINT timer_id _U_, UINT msg _U_, DWORD_PTR arg _U_, 1312 DWORD_PTR dw1 _U_, DWORD_PTR dw2 _U_) 1313 { 1314 struct pcap_stat stat; 1315 1316 if (infodelay == 0 && pcap_stats(pd, &stat) >= 0) 1317 fprintf(stderr, "Got %u\r", packets_captured); 1318 } 1319 #elif defined(HAVE_ALARM) 1320 static void verbose_stats_dump(int sig _U_) 1321 { 1322 struct pcap_stat stat; 1323 1324 if (infodelay == 0 && pcap_stats(pd, &stat) >= 0) 1325 fprintf(stderr, "Got %u\r", packets_captured); 1326 alarm(1); 1327 } 1328 #endif 1329 1330 static void 1331 usage(void) 1332 { 1333 extern char version[]; 1334 #ifndef HAVE_PCAP_LIB_VERSION 1335 #if defined(WIN32) || defined(HAVE_PCAP_VERSION) 1336 extern char pcap_version[]; 1337 #else /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */ 1338 static char pcap_version[] = "unknown"; 1339 #endif /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */ 1340 #endif /* HAVE_PCAP_LIB_VERSION */ 1341 1342 #ifdef HAVE_PCAP_LIB_VERSION 1343 #ifdef WIN32 1344 (void)fprintf(stderr, "%s version %s, based on tcpdump version %s\n", program_name, WDversion, version); 1345 #else /* WIN32 */ 1346 (void)fprintf(stderr, "%s version %s\n", program_name, version); 1347 #endif /* WIN32 */ 1348 (void)fprintf(stderr, "%s\n",pcap_lib_version()); 1349 #else /* HAVE_PCAP_LIB_VERSION */ 1350 #ifdef WIN32 1351 (void)fprintf(stderr, "%s version %s, based on tcpdump version %s\n", program_name, WDversion, version); 1352 (void)fprintf(stderr, "WinPcap version %s, based on libpcap version %s\n",Wpcap_version, pcap_version); 1353 #else /* WIN32 */ 1354 (void)fprintf(stderr, "%s version %s\n", program_name, version); 1355 (void)fprintf(stderr, "libpcap version %s\n", pcap_version); 1356 #endif /* WIN32 */ 1357 #endif /* HAVE_PCAP_LIB_VERSION */ 1358 (void)fprintf(stderr, 1359 "Usage: %s [-aAd" D_FLAG "eflLnNOpqRStu" U_FLAG "vxX]" B_FLAG_USAGE " [-c count] [ -C file_size ]\n", program_name); 1360 (void)fprintf(stderr, 1361 "\t\t[ -E algo:secret ] [ -F file ] [ -i interface ] [ -M secret ]\n"); 1362 (void)fprintf(stderr, 1363 "\t\t[ -r file ] [ -s snaplen ] [ -T type ] [ -w file ]\n"); 1364 (void)fprintf(stderr, 1365 "\t\t[ -W filecount ] [ -y datalinktype ] [ -Z user ]\n"); 1366 (void)fprintf(stderr, 1367 "\t\t[ expression ]\n"); 1368 exit(1); 1369 } 1370 1371 1372 1373 /* VARARGS */ 1374 static void 1375 ndo_error(netdissect_options *ndo _U_, const char *fmt, ...) 1376 { 1377 va_list ap; 1378 1379 (void)fprintf(stderr, "%s: ", program_name); 1380 va_start(ap, fmt); 1381 (void)vfprintf(stderr, fmt, ap); 1382 va_end(ap); 1383 if (*fmt) { 1384 fmt += strlen(fmt); 1385 if (fmt[-1] != '\n') 1386 (void)fputc('\n', stderr); 1387 } 1388 exit(1); 1389 /* NOTREACHED */ 1390 } 1391 1392 /* VARARGS */ 1393 static void 1394 ndo_warning(netdissect_options *ndo _U_, const char *fmt, ...) 1395 { 1396 va_list ap; 1397 1398 (void)fprintf(stderr, "%s: WARNING: ", program_name); 1399 va_start(ap, fmt); 1400 (void)vfprintf(stderr, fmt, ap); 1401 va_end(ap); 1402 if (*fmt) { 1403 fmt += strlen(fmt); 1404 if (fmt[-1] != '\n') 1405 (void)fputc('\n', stderr); 1406 } 1407 } 1408 1409