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.283 2008-09-25 21:45:50 guy Exp $ (LBL)"; 34 #endif 35 36 /* 37 * tcpdump - monitor tcp/ip traffic on an ethernet. 38 * 39 * First written in 1987 by Van Jacobson, Lawrence Berkeley Laboratory. 40 * Mercilessly hacked and occasionally improved since then via the 41 * combined efforts of Van, Steve McCanne and Craig Leres of LBL. 42 */ 43 44 #ifdef HAVE_CONFIG_H 45 #include "config.h" 46 #endif 47 48 #include <tcpdump-stdinc.h> 49 50 #ifdef WIN32 51 #include "getopt.h" 52 #include "w32_fzs.h" 53 extern int strcasecmp (const char *__s1, const char *__s2); 54 extern int SIZE_BUF; 55 #define off_t long 56 #define uint UINT 57 #endif /* WIN32 */ 58 59 #ifdef HAVE_SMI_H 60 #include <smi.h> 61 #endif 62 63 #include <pcap.h> 64 #include <signal.h> 65 #include <stdio.h> 66 #include <stdlib.h> 67 #include <string.h> 68 #include <limits.h> 69 #ifndef WIN32 70 #include <sys/wait.h> 71 #include <sys/resource.h> 72 #include <pwd.h> 73 #include <grp.h> 74 #include <errno.h> 75 #endif /* WIN32 */ 76 77 78 #include "netdissect.h" 79 #include "interface.h" 80 #include "addrtoname.h" 81 #include "machdep.h" 82 #include "setsignal.h" 83 #include "gmt2local.h" 84 #include "pcap-missing.h" 85 86 #ifndef NAME_MAX 87 #define NAME_MAX 255 88 #endif 89 90 netdissect_options Gndo; 91 netdissect_options *gndo = &Gndo; 92 93 static int dflag; /* print filter code */ 94 static int Lflag; /* list available data link types and exit */ 95 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE 96 static int Jflag; /* list available time stamp types */ 97 #endif 98 static char *zflag = NULL; /* compress each savefile using a specified command (like gzip or bzip2) */ 99 100 static int infodelay; 101 static int infoprint; 102 103 char *program_name; 104 105 int32_t thiszone; /* seconds offset from gmt to local time */ 106 107 /* Forwards */ 108 static RETSIGTYPE cleanup(int); 109 static RETSIGTYPE child_cleanup(int); 110 static void usage(void) __attribute__((noreturn)); 111 static void show_dlts_and_exit(const char *device, pcap_t *pd) __attribute__((noreturn)); 112 113 static void print_packet(u_char *, const struct pcap_pkthdr *, const u_char *); 114 static void ndo_default_print(netdissect_options *, const u_char *, u_int); 115 static void dump_packet_and_trunc(u_char *, const struct pcap_pkthdr *, const u_char *); 116 static void dump_packet(u_char *, const struct pcap_pkthdr *, const u_char *); 117 static void droproot(const char *, const char *); 118 static void ndo_error(netdissect_options *ndo, const char *fmt, ...) 119 __attribute__ ((noreturn, format (printf, 2, 3))); 120 static void ndo_warning(netdissect_options *ndo, const char *fmt, ...); 121 122 #ifdef SIGINFO 123 RETSIGTYPE requestinfo(int); 124 #endif 125 126 #if defined(USE_WIN32_MM_TIMER) 127 #include <MMsystem.h> 128 static UINT timer_id; 129 static void CALLBACK verbose_stats_dump(UINT, UINT, DWORD_PTR, DWORD_PTR, DWORD_PTR); 130 #elif defined(HAVE_ALARM) 131 static void verbose_stats_dump(int sig); 132 #endif 133 134 static void info(int); 135 static u_int packets_captured; 136 137 struct printer { 138 if_printer f; 139 int type; 140 }; 141 142 143 struct ndo_printer { 144 if_ndo_printer f; 145 int type; 146 }; 147 148 149 static struct printer printers[] = { 150 { arcnet_if_print, DLT_ARCNET }, 151 #ifdef DLT_ARCNET_LINUX 152 { arcnet_linux_if_print, DLT_ARCNET_LINUX }, 153 #endif 154 { token_if_print, DLT_IEEE802 }, 155 #ifdef DLT_LANE8023 156 { lane_if_print, DLT_LANE8023 }, 157 #endif 158 #ifdef DLT_CIP 159 { cip_if_print, DLT_CIP }, 160 #endif 161 #ifdef DLT_ATM_CLIP 162 { cip_if_print, DLT_ATM_CLIP }, 163 #endif 164 { sl_if_print, DLT_SLIP }, 165 #ifdef DLT_SLIP_BSDOS 166 { sl_bsdos_if_print, DLT_SLIP_BSDOS }, 167 #endif 168 { ppp_if_print, DLT_PPP }, 169 #ifdef DLT_PPP_WITHDIRECTION 170 { ppp_if_print, DLT_PPP_WITHDIRECTION }, 171 #endif 172 #ifdef DLT_PPP_BSDOS 173 { ppp_bsdos_if_print, DLT_PPP_BSDOS }, 174 #endif 175 { fddi_if_print, DLT_FDDI }, 176 { null_if_print, DLT_NULL }, 177 #ifdef DLT_LOOP 178 { null_if_print, DLT_LOOP }, 179 #endif 180 { raw_if_print, DLT_RAW }, 181 { atm_if_print, DLT_ATM_RFC1483 }, 182 #ifdef DLT_C_HDLC 183 { chdlc_if_print, DLT_C_HDLC }, 184 #endif 185 #ifdef DLT_HDLC 186 { chdlc_if_print, DLT_HDLC }, 187 #endif 188 #ifdef DLT_PPP_SERIAL 189 { ppp_hdlc_if_print, DLT_PPP_SERIAL }, 190 #endif 191 #ifdef DLT_PPP_ETHER 192 { pppoe_if_print, DLT_PPP_ETHER }, 193 #endif 194 #ifdef DLT_LINUX_SLL 195 { sll_if_print, DLT_LINUX_SLL }, 196 #endif 197 #ifdef DLT_IEEE802_11 198 { ieee802_11_if_print, DLT_IEEE802_11}, 199 #endif 200 #ifdef DLT_LTALK 201 { ltalk_if_print, DLT_LTALK }, 202 #endif 203 #if defined(DLT_PFLOG) && defined(HAVE_NET_PFVAR_H) 204 { pflog_if_print, DLT_PFLOG }, 205 #endif 206 #ifdef DLT_FR 207 { fr_if_print, DLT_FR }, 208 #endif 209 #ifdef DLT_FRELAY 210 { fr_if_print, DLT_FRELAY }, 211 #endif 212 #ifdef DLT_SUNATM 213 { sunatm_if_print, DLT_SUNATM }, 214 #endif 215 #ifdef DLT_IP_OVER_FC 216 { ipfc_if_print, DLT_IP_OVER_FC }, 217 #endif 218 #ifdef DLT_PRISM_HEADER 219 { prism_if_print, DLT_PRISM_HEADER }, 220 #endif 221 #ifdef DLT_IEEE802_11_RADIO 222 { ieee802_11_radio_if_print, DLT_IEEE802_11_RADIO }, 223 #endif 224 #ifdef DLT_ENC 225 { enc_if_print, DLT_ENC }, 226 #endif 227 #ifdef DLT_SYMANTEC_FIREWALL 228 { symantec_if_print, DLT_SYMANTEC_FIREWALL }, 229 #endif 230 #ifdef DLT_APPLE_IP_OVER_IEEE1394 231 { ap1394_if_print, DLT_APPLE_IP_OVER_IEEE1394 }, 232 #endif 233 #ifdef DLT_IEEE802_11_RADIO_AVS 234 { ieee802_11_radio_avs_if_print, DLT_IEEE802_11_RADIO_AVS }, 235 #endif 236 #ifdef DLT_JUNIPER_ATM1 237 { juniper_atm1_print, DLT_JUNIPER_ATM1 }, 238 #endif 239 #ifdef DLT_JUNIPER_ATM2 240 { juniper_atm2_print, DLT_JUNIPER_ATM2 }, 241 #endif 242 #ifdef DLT_JUNIPER_MFR 243 { juniper_mfr_print, DLT_JUNIPER_MFR }, 244 #endif 245 #ifdef DLT_JUNIPER_MLFR 246 { juniper_mlfr_print, DLT_JUNIPER_MLFR }, 247 #endif 248 #ifdef DLT_JUNIPER_MLPPP 249 { juniper_mlppp_print, DLT_JUNIPER_MLPPP }, 250 #endif 251 #ifdef DLT_JUNIPER_PPPOE 252 { juniper_pppoe_print, DLT_JUNIPER_PPPOE }, 253 #endif 254 #ifdef DLT_JUNIPER_PPPOE_ATM 255 { juniper_pppoe_atm_print, DLT_JUNIPER_PPPOE_ATM }, 256 #endif 257 #ifdef DLT_JUNIPER_GGSN 258 { juniper_ggsn_print, DLT_JUNIPER_GGSN }, 259 #endif 260 #ifdef DLT_JUNIPER_ES 261 { juniper_es_print, DLT_JUNIPER_ES }, 262 #endif 263 #ifdef DLT_JUNIPER_MONITOR 264 { juniper_monitor_print, DLT_JUNIPER_MONITOR }, 265 #endif 266 #ifdef DLT_JUNIPER_SERVICES 267 { juniper_services_print, DLT_JUNIPER_SERVICES }, 268 #endif 269 #ifdef DLT_JUNIPER_ETHER 270 { juniper_ether_print, DLT_JUNIPER_ETHER }, 271 #endif 272 #ifdef DLT_JUNIPER_PPP 273 { juniper_ppp_print, DLT_JUNIPER_PPP }, 274 #endif 275 #ifdef DLT_JUNIPER_FRELAY 276 { juniper_frelay_print, DLT_JUNIPER_FRELAY }, 277 #endif 278 #ifdef DLT_JUNIPER_CHDLC 279 { juniper_chdlc_print, DLT_JUNIPER_CHDLC }, 280 #endif 281 #ifdef DLT_MFR 282 { mfr_if_print, DLT_MFR }, 283 #endif 284 #if defined(DLT_BLUETOOTH_HCI_H4_WITH_PHDR) && defined(HAVE_PCAP_BLUETOOTH_H) 285 { bt_if_print, DLT_BLUETOOTH_HCI_H4_WITH_PHDR}, 286 #endif 287 #ifdef HAVE_PCAP_USB_H 288 #ifdef DLT_USB_LINUX 289 { usb_linux_48_byte_print, DLT_USB_LINUX}, 290 #endif /* DLT_USB_LINUX */ 291 #ifdef DLT_USB_LINUX_MMAPPED 292 { usb_linux_64_byte_print, DLT_USB_LINUX_MMAPPED}, 293 #endif /* DLT_USB_LINUX_MMAPPED */ 294 #endif /* HAVE_PCAP_USB_H */ 295 #ifdef DLT_IPV4 296 { raw_if_print, DLT_IPV4 }, 297 #endif 298 #ifdef DLT_IPV6 299 { raw_if_print, DLT_IPV6 }, 300 #endif 301 { NULL, 0 }, 302 }; 303 304 static struct ndo_printer ndo_printers[] = { 305 { ether_if_print, DLT_EN10MB }, 306 #ifdef DLT_IPNET 307 { ipnet_if_print, DLT_IPNET }, 308 #endif 309 #ifdef DLT_IEEE802_15_4 310 { ieee802_15_4_if_print, DLT_IEEE802_15_4 }, 311 #endif 312 #ifdef DLT_IEEE802_15_4_NOFCS 313 { ieee802_15_4_if_print, DLT_IEEE802_15_4_NOFCS }, 314 #endif 315 #ifdef DLT_PPI 316 { ppi_if_print, DLT_PPI }, 317 #endif 318 #ifdef DLT_NETANALYZER 319 { netanalyzer_if_print, DLT_NETANALYZER }, 320 #endif 321 #ifdef DLT_NETANALYZER_TRANSPARENT 322 { netanalyzer_transparent_if_print, DLT_NETANALYZER_TRANSPARENT }, 323 #endif 324 { NULL, 0 }, 325 }; 326 327 if_printer 328 lookup_printer(int type) 329 { 330 struct printer *p; 331 332 for (p = printers; p->f; ++p) 333 if (type == p->type) 334 return p->f; 335 336 return NULL; 337 /* NOTREACHED */ 338 } 339 340 if_ndo_printer 341 lookup_ndo_printer(int type) 342 { 343 struct ndo_printer *p; 344 345 for (p = ndo_printers; p->f; ++p) 346 if (type == p->type) 347 return p->f; 348 349 return NULL; 350 /* NOTREACHED */ 351 } 352 353 static pcap_t *pd; 354 355 static int supports_monitor_mode; 356 357 extern int optind; 358 extern int opterr; 359 extern char *optarg; 360 361 struct print_info { 362 netdissect_options *ndo; 363 union { 364 if_printer printer; 365 if_ndo_printer ndo_printer; 366 } p; 367 int ndo_type; 368 }; 369 370 struct dump_info { 371 char *WFileName; 372 char *CurrentFileName; 373 pcap_t *pd; 374 pcap_dumper_t *p; 375 }; 376 377 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE 378 static void 379 show_tstamp_types_and_exit(const char *device, pcap_t *pd) 380 { 381 int n_tstamp_types; 382 int *tstamp_types = 0; 383 const char *tstamp_type_name; 384 int i; 385 386 n_tstamp_types = pcap_list_tstamp_types(pd, &tstamp_types); 387 if (n_tstamp_types < 0) 388 error("%s", pcap_geterr(pd)); 389 390 if (n_tstamp_types == 0) { 391 fprintf(stderr, "Time stamp type cannot be set for %s\n", 392 device); 393 exit(0); 394 } 395 fprintf(stderr, "Time stamp types for %s (use option -j to set):\n", 396 device); 397 for (i = 0; i < n_tstamp_types; i++) { 398 tstamp_type_name = pcap_tstamp_type_val_to_name(tstamp_types[i]); 399 if (tstamp_type_name != NULL) { 400 (void) fprintf(stderr, " %s (%s)\n", tstamp_type_name, 401 pcap_tstamp_type_val_to_description(tstamp_types[i])); 402 } else { 403 (void) fprintf(stderr, " %d\n", tstamp_types[i]); 404 } 405 } 406 pcap_free_tstamp_types(tstamp_types); 407 exit(0); 408 } 409 #endif 410 411 static void 412 show_dlts_and_exit(const char *device, pcap_t *pd) 413 { 414 int n_dlts; 415 int *dlts = 0; 416 const char *dlt_name; 417 418 n_dlts = pcap_list_datalinks(pd, &dlts); 419 if (n_dlts < 0) 420 error("%s", pcap_geterr(pd)); 421 else if (n_dlts == 0 || !dlts) 422 error("No data link types."); 423 424 /* 425 * If the interface is known to support monitor mode, indicate 426 * whether these are the data link types available when not in 427 * monitor mode, if -I wasn't specified, or when in monitor mode, 428 * when -I was specified (the link-layer types available in 429 * monitor mode might be different from the ones available when 430 * not in monitor mode). 431 */ 432 if (supports_monitor_mode) 433 (void) fprintf(stderr, "Data link types for %s %s (use option -y to set):\n", 434 device, 435 Iflag ? "when in monitor mode" : "when not in monitor mode"); 436 else 437 (void) fprintf(stderr, "Data link types for %s (use option -y to set):\n", 438 device); 439 440 while (--n_dlts >= 0) { 441 dlt_name = pcap_datalink_val_to_name(dlts[n_dlts]); 442 if (dlt_name != NULL) { 443 (void) fprintf(stderr, " %s (%s)", dlt_name, 444 pcap_datalink_val_to_description(dlts[n_dlts])); 445 446 /* 447 * OK, does tcpdump handle that type? 448 */ 449 if (lookup_printer(dlts[n_dlts]) == NULL 450 && lookup_ndo_printer(dlts[n_dlts]) == NULL) 451 (void) fprintf(stderr, " (printing not supported)"); 452 fprintf(stderr, "\n"); 453 } else { 454 (void) fprintf(stderr, " DLT %d (printing not supported)\n", 455 dlts[n_dlts]); 456 } 457 } 458 pcap_free_datalinks(dlts); 459 exit(0); 460 } 461 462 /* 463 * Set up flags that might or might not be supported depending on the 464 * version of libpcap we're using. 465 */ 466 #if defined(HAVE_PCAP_CREATE) || defined(WIN32) 467 #define B_FLAG "B:" 468 #define B_FLAG_USAGE " [ -B size ]" 469 #else /* defined(HAVE_PCAP_CREATE) || defined(WIN32) */ 470 #define B_FLAG 471 #define B_FLAG_USAGE 472 #endif /* defined(HAVE_PCAP_CREATE) || defined(WIN32) */ 473 474 #ifdef HAVE_PCAP_CREATE 475 #define I_FLAG "I" 476 #else /* HAVE_PCAP_CREATE */ 477 #define I_FLAG 478 #endif /* HAVE_PCAP_CREATE */ 479 480 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE 481 #define j_FLAG "j:" 482 #define j_FLAG_USAGE " [ -j tstamptype ]" 483 #define J_FLAG "J" 484 #else /* PCAP_ERROR_TSTAMP_TYPE_NOTSUP */ 485 #define j_FLAG 486 #define j_FLAG_USAGE 487 #define J_FLAG 488 #endif /* PCAP_ERROR_TSTAMP_TYPE_NOTSUP */ 489 490 #ifdef HAVE_PCAP_FINDALLDEVS 491 #ifndef HAVE_PCAP_IF_T 492 #undef HAVE_PCAP_FINDALLDEVS 493 #endif 494 #endif 495 496 #ifdef HAVE_PCAP_FINDALLDEVS 497 #define D_FLAG "D" 498 #else 499 #define D_FLAG 500 #endif 501 502 #ifdef HAVE_PCAP_DUMP_FLUSH 503 #define U_FLAG "U" 504 #else 505 #define U_FLAG 506 #endif 507 508 #ifndef WIN32 509 /* Drop root privileges and chroot if necessary */ 510 static void 511 droproot(const char *username, const char *chroot_dir) 512 { 513 struct passwd *pw = NULL; 514 515 if (chroot_dir && !username) { 516 fprintf(stderr, "tcpdump: Chroot without dropping root is insecure\n"); 517 exit(1); 518 } 519 520 pw = getpwnam(username); 521 if (pw) { 522 if (chroot_dir) { 523 if (chroot(chroot_dir) != 0 || chdir ("/") != 0) { 524 fprintf(stderr, "tcpdump: Couldn't chroot/chdir to '%.64s': %s\n", 525 chroot_dir, pcap_strerror(errno)); 526 exit(1); 527 } 528 } 529 if (initgroups(pw->pw_name, pw->pw_gid) != 0 || 530 setgid(pw->pw_gid) != 0 || setuid(pw->pw_uid) != 0) { 531 fprintf(stderr, "tcpdump: Couldn't change to '%.32s' uid=%lu gid=%lu: %s\n", 532 username, 533 (unsigned long)pw->pw_uid, 534 (unsigned long)pw->pw_gid, 535 pcap_strerror(errno)); 536 exit(1); 537 } 538 } 539 else { 540 fprintf(stderr, "tcpdump: Couldn't find user '%.32s'\n", 541 username); 542 exit(1); 543 } 544 } 545 #endif /* WIN32 */ 546 547 static int 548 getWflagChars(int x) 549 { 550 int c = 0; 551 552 x -= 1; 553 while (x > 0) { 554 c += 1; 555 x /= 10; 556 } 557 558 return c; 559 } 560 561 562 static void 563 MakeFilename(char *buffer, char *orig_name, int cnt, int max_chars) 564 { 565 char *filename = malloc(NAME_MAX + 1); 566 567 /* Process with strftime if Gflag is set. */ 568 if (Gflag != 0) { 569 struct tm *local_tm; 570 571 /* Convert Gflag_time to a usable format */ 572 if ((local_tm = localtime(&Gflag_time)) == NULL) { 573 error("MakeTimedFilename: localtime"); 574 } 575 576 /* There's no good way to detect an error in strftime since a return 577 * value of 0 isn't necessarily failure. 578 */ 579 strftime(filename, NAME_MAX, orig_name, local_tm); 580 } else { 581 strncpy(filename, orig_name, NAME_MAX); 582 } 583 584 if (cnt == 0 && max_chars == 0) 585 strncpy(buffer, filename, NAME_MAX + 1); 586 else 587 if (snprintf(buffer, NAME_MAX + 1, "%s%0*d", filename, max_chars, cnt) > NAME_MAX) 588 /* Report an error if the filename is too large */ 589 error("too many output files or filename is too long (> %d)", NAME_MAX); 590 free(filename); 591 } 592 593 static int tcpdump_printf(netdissect_options *ndo _U_, 594 const char *fmt, ...) 595 { 596 597 va_list args; 598 int ret; 599 600 va_start(args, fmt); 601 ret=vfprintf(stdout, fmt, args); 602 va_end(args); 603 604 return ret; 605 } 606 607 int 608 main(int argc, char **argv) 609 { 610 register int cnt, op, i; 611 bpf_u_int32 localnet, netmask; 612 register char *cp, *infile, *cmdbuf, *device, *RFileName, *WFileName; 613 pcap_handler callback; 614 int type; 615 struct bpf_program fcode; 616 #ifndef WIN32 617 RETSIGTYPE (*oldhandler)(int); 618 #endif 619 struct print_info printinfo; 620 struct dump_info dumpinfo; 621 u_char *pcap_userdata; 622 char ebuf[PCAP_ERRBUF_SIZE]; 623 char *username = NULL; 624 char *chroot_dir = NULL; 625 #ifdef HAVE_PCAP_FINDALLDEVS 626 pcap_if_t *devpointer; 627 int devnum; 628 #endif 629 int status; 630 #ifdef WIN32 631 if(wsockinit() != 0) return 1; 632 #endif /* WIN32 */ 633 634 jflag=-1; /* not set */ 635 gndo->ndo_Oflag=1; 636 gndo->ndo_Rflag=1; 637 gndo->ndo_dlt=-1; 638 gndo->ndo_default_print=ndo_default_print; 639 gndo->ndo_printf=tcpdump_printf; 640 gndo->ndo_error=ndo_error; 641 gndo->ndo_warning=ndo_warning; 642 gndo->ndo_snaplen = DEFAULT_SNAPLEN; 643 644 cnt = -1; 645 device = NULL; 646 infile = NULL; 647 RFileName = NULL; 648 WFileName = NULL; 649 if ((cp = strrchr(argv[0], '/')) != NULL) 650 program_name = cp + 1; 651 else 652 program_name = argv[0]; 653 654 if (abort_on_misalignment(ebuf, sizeof(ebuf)) < 0) 655 error("%s", ebuf); 656 657 #ifdef LIBSMI 658 smiInit("tcpdump"); 659 #endif 660 661 while ( 662 (op = getopt(argc, argv, "aAb" B_FLAG "c:C:d" D_FLAG "eE:fF:G:hHi:" I_FLAG j_FLAG J_FLAG "KlLm:M:nNOpqr:Rs:StT:u" U_FLAG "vw:W:xXy:Yz:Z:")) != -1) 663 switch (op) { 664 665 case 'a': 666 /* compatibility for old -a */ 667 break; 668 669 case 'A': 670 ++Aflag; 671 break; 672 673 case 'b': 674 ++bflag; 675 break; 676 677 #if defined(HAVE_PCAP_CREATE) || defined(WIN32) 678 case 'B': 679 Bflag = atoi(optarg)*1024; 680 if (Bflag <= 0) 681 error("invalid packet buffer size %s", optarg); 682 break; 683 #endif /* defined(HAVE_PCAP_CREATE) || defined(WIN32) */ 684 685 case 'c': 686 cnt = atoi(optarg); 687 if (cnt <= 0) 688 error("invalid packet count %s", optarg); 689 break; 690 691 case 'C': 692 Cflag = atoi(optarg) * 1000000; 693 if (Cflag < 0) 694 error("invalid file size %s", optarg); 695 break; 696 697 case 'd': 698 ++dflag; 699 break; 700 701 #ifdef HAVE_PCAP_FINDALLDEVS 702 case 'D': 703 if (pcap_findalldevs(&devpointer, ebuf) < 0) 704 error("%s", ebuf); 705 else { 706 for (i = 0; devpointer != 0; i++) { 707 printf("%d.%s", i+1, devpointer->name); 708 if (devpointer->description != NULL) 709 printf(" (%s)", devpointer->description); 710 printf("\n"); 711 devpointer = devpointer->next; 712 } 713 } 714 return 0; 715 #endif /* HAVE_PCAP_FINDALLDEVS */ 716 717 case 'L': 718 Lflag++; 719 break; 720 721 case 'e': 722 ++eflag; 723 break; 724 725 case 'E': 726 #ifndef HAVE_LIBCRYPTO 727 warning("crypto code not compiled in"); 728 #endif 729 gndo->ndo_espsecret = optarg; 730 break; 731 732 case 'f': 733 ++fflag; 734 break; 735 736 case 'F': 737 infile = optarg; 738 break; 739 740 case 'G': 741 Gflag = atoi(optarg); 742 if (Gflag < 0) 743 error("invalid number of seconds %s", optarg); 744 745 /* We will create one file initially. */ 746 Gflag_count = 0; 747 748 /* Grab the current time for rotation use. */ 749 if ((Gflag_time = time(NULL)) == (time_t)-1) { 750 error("main: can't get current time: %s", 751 pcap_strerror(errno)); 752 } 753 break; 754 755 case 'h': 756 usage(); 757 break; 758 759 case 'H': 760 ++Hflag; 761 break; 762 763 case 'i': 764 if (optarg[0] == '0' && optarg[1] == 0) 765 error("Invalid adapter index"); 766 767 #ifdef HAVE_PCAP_FINDALLDEVS 768 /* 769 * If the argument is a number, treat it as 770 * an index into the list of adapters, as 771 * printed by "tcpdump -D". 772 * 773 * This should be OK on UNIX systems, as interfaces 774 * shouldn't have names that begin with digits. 775 * It can be useful on Windows, where more than 776 * one interface can have the same name. 777 */ 778 if ((devnum = atoi(optarg)) != 0) { 779 if (devnum < 0) 780 error("Invalid adapter index"); 781 782 if (pcap_findalldevs(&devpointer, ebuf) < 0) 783 error("%s", ebuf); 784 else { 785 /* 786 * Look for the devnum-th entry 787 * in the list of devices 788 * (1-based). 789 */ 790 for (i = 0; 791 i < devnum-1 && devpointer != NULL; 792 i++, devpointer = devpointer->next) 793 ; 794 if (devpointer == NULL) 795 error("Invalid adapter index"); 796 } 797 device = devpointer->name; 798 break; 799 } 800 #endif /* HAVE_PCAP_FINDALLDEVS */ 801 device = optarg; 802 break; 803 804 #ifdef HAVE_PCAP_CREATE 805 case 'I': 806 ++Iflag; 807 break; 808 #endif /* HAVE_PCAP_CREATE */ 809 810 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE 811 case 'j': 812 jflag = pcap_tstamp_type_name_to_val(optarg); 813 if (jflag < 0) 814 error("invalid time stamp type %s", optarg); 815 break; 816 817 case 'J': 818 Jflag++; 819 break; 820 #endif 821 822 case 'l': 823 #ifdef WIN32 824 /* 825 * _IOLBF is the same as _IOFBF in Microsoft's C 826 * libraries; the only alternative they offer 827 * is _IONBF. 828 * 829 * XXX - this should really be checking for MSVC++, 830 * not WIN32, if, for example, MinGW has its own 831 * C library that is more UNIX-compatible. 832 */ 833 setvbuf(stdout, NULL, _IONBF, 0); 834 #else /* WIN32 */ 835 #ifdef HAVE_SETLINEBUF 836 setlinebuf(stdout); 837 #else 838 setvbuf(stdout, NULL, _IOLBF, 0); 839 #endif 840 #endif /* WIN32 */ 841 break; 842 843 case 'K': 844 ++Kflag; 845 break; 846 847 case 'm': 848 #ifdef LIBSMI 849 if (smiLoadModule(optarg) == 0) { 850 error("could not load MIB module %s", optarg); 851 } 852 sflag = 1; 853 #else 854 (void)fprintf(stderr, "%s: ignoring option `-m %s' ", 855 program_name, optarg); 856 (void)fprintf(stderr, "(no libsmi support)\n"); 857 #endif 858 break; 859 860 case 'M': 861 /* TCP-MD5 shared secret */ 862 #ifndef HAVE_LIBCRYPTO 863 warning("crypto code not compiled in"); 864 #endif 865 sigsecret = optarg; 866 break; 867 868 case 'n': 869 ++nflag; 870 break; 871 872 case 'N': 873 ++Nflag; 874 break; 875 876 case 'O': 877 Oflag = 0; 878 break; 879 880 case 'p': 881 ++pflag; 882 break; 883 884 case 'q': 885 ++qflag; 886 ++suppress_default_print; 887 break; 888 889 case 'r': 890 RFileName = optarg; 891 break; 892 893 case 'R': 894 Rflag = 0; 895 break; 896 897 case 's': { 898 char *end; 899 900 snaplen = strtol(optarg, &end, 0); 901 if (optarg == end || *end != '\0' 902 || snaplen < 0 || snaplen > MAXIMUM_SNAPLEN) 903 error("invalid snaplen %s", optarg); 904 else if (snaplen == 0) 905 snaplen = MAXIMUM_SNAPLEN; 906 break; 907 } 908 909 case 'S': 910 ++Sflag; 911 break; 912 913 case 't': 914 ++tflag; 915 break; 916 917 case 'T': 918 if (strcasecmp(optarg, "vat") == 0) 919 packettype = PT_VAT; 920 else if (strcasecmp(optarg, "wb") == 0) 921 packettype = PT_WB; 922 else if (strcasecmp(optarg, "rpc") == 0) 923 packettype = PT_RPC; 924 else if (strcasecmp(optarg, "rtp") == 0) 925 packettype = PT_RTP; 926 else if (strcasecmp(optarg, "rtcp") == 0) 927 packettype = PT_RTCP; 928 else if (strcasecmp(optarg, "snmp") == 0) 929 packettype = PT_SNMP; 930 else if (strcasecmp(optarg, "cnfp") == 0) 931 packettype = PT_CNFP; 932 else if (strcasecmp(optarg, "tftp") == 0) 933 packettype = PT_TFTP; 934 else if (strcasecmp(optarg, "aodv") == 0) 935 packettype = PT_AODV; 936 else if (strcasecmp(optarg, "carp") == 0) 937 packettype = PT_CARP; 938 else 939 error("unknown packet type `%s'", optarg); 940 break; 941 942 case 'u': 943 ++uflag; 944 break; 945 946 #ifdef HAVE_PCAP_DUMP_FLUSH 947 case 'U': 948 ++Uflag; 949 break; 950 #endif 951 952 case 'v': 953 ++vflag; 954 break; 955 956 case 'w': 957 WFileName = optarg; 958 break; 959 960 case 'W': 961 Wflag = atoi(optarg); 962 if (Wflag < 0) 963 error("invalid number of output files %s", optarg); 964 WflagChars = getWflagChars(Wflag); 965 break; 966 967 case 'x': 968 ++xflag; 969 ++suppress_default_print; 970 break; 971 972 case 'X': 973 ++Xflag; 974 ++suppress_default_print; 975 break; 976 977 case 'y': 978 gndo->ndo_dltname = optarg; 979 gndo->ndo_dlt = 980 pcap_datalink_name_to_val(gndo->ndo_dltname); 981 if (gndo->ndo_dlt < 0) 982 error("invalid data link type %s", gndo->ndo_dltname); 983 break; 984 985 #if defined(HAVE_PCAP_DEBUG) || defined(HAVE_YYDEBUG) 986 case 'Y': 987 { 988 /* Undocumented flag */ 989 #ifdef HAVE_PCAP_DEBUG 990 extern int pcap_debug; 991 pcap_debug = 1; 992 #else 993 extern int yydebug; 994 yydebug = 1; 995 #endif 996 } 997 break; 998 #endif 999 case 'z': 1000 if (optarg) { 1001 zflag = strdup(optarg); 1002 } else { 1003 usage(); 1004 /* NOTREACHED */ 1005 } 1006 break; 1007 1008 case 'Z': 1009 if (optarg) { 1010 username = strdup(optarg); 1011 } 1012 else { 1013 usage(); 1014 /* NOTREACHED */ 1015 } 1016 break; 1017 1018 default: 1019 usage(); 1020 /* NOTREACHED */ 1021 } 1022 1023 switch (tflag) { 1024 1025 case 0: /* Default */ 1026 case 4: /* Default + Date*/ 1027 thiszone = gmt2local(0); 1028 break; 1029 1030 case 1: /* No time stamp */ 1031 case 2: /* Unix timeval style */ 1032 case 3: /* Microseconds since previous packet */ 1033 case 5: /* Microseconds since first packet */ 1034 break; 1035 1036 default: /* Not supported */ 1037 error("only -t, -tt, -ttt, -tttt and -ttttt are supported"); 1038 break; 1039 } 1040 1041 #ifdef WITH_CHROOT 1042 /* if run as root, prepare for chrooting */ 1043 if (getuid() == 0 || geteuid() == 0) { 1044 /* future extensibility for cmd-line arguments */ 1045 if (!chroot_dir) 1046 chroot_dir = WITH_CHROOT; 1047 } 1048 #endif 1049 1050 #ifdef WITH_USER 1051 /* if run as root, prepare for dropping root privileges */ 1052 if (getuid() == 0 || geteuid() == 0) { 1053 /* Run with '-Z root' to restore old behaviour */ 1054 if (!username) 1055 username = WITH_USER; 1056 } 1057 #endif 1058 1059 if (RFileName != NULL) { 1060 int dlt; 1061 const char *dlt_name; 1062 1063 #ifndef WIN32 1064 /* 1065 * We don't need network access, so relinquish any set-UID 1066 * or set-GID privileges we have (if any). 1067 * 1068 * We do *not* want set-UID privileges when opening a 1069 * trace file, as that might let the user read other 1070 * people's trace files (especially if we're set-UID 1071 * root). 1072 */ 1073 if (setgid(getgid()) != 0 || setuid(getuid()) != 0 ) 1074 fprintf(stderr, "Warning: setgid/setuid failed !\n"); 1075 #endif /* WIN32 */ 1076 pd = pcap_open_offline(RFileName, ebuf); 1077 if (pd == NULL) 1078 error("%s", ebuf); 1079 dlt = pcap_datalink(pd); 1080 dlt_name = pcap_datalink_val_to_name(dlt); 1081 if (dlt_name == NULL) { 1082 fprintf(stderr, "reading from file %s, link-type %u\n", 1083 RFileName, dlt); 1084 } else { 1085 fprintf(stderr, 1086 "reading from file %s, link-type %s (%s)\n", 1087 RFileName, dlt_name, 1088 pcap_datalink_val_to_description(dlt)); 1089 } 1090 localnet = 0; 1091 netmask = 0; 1092 if (fflag != 0) 1093 error("-f and -r options are incompatible"); 1094 } else { 1095 if (device == NULL) { 1096 device = pcap_lookupdev(ebuf); 1097 if (device == NULL) 1098 error("%s", ebuf); 1099 } 1100 #ifdef WIN32 1101 if(strlen(device) == 1) //we assume that an ASCII string is always longer than 1 char 1102 { //a Unicode string has a \0 as second byte (so strlen() is 1) 1103 fprintf(stderr, "%s: listening on %ws\n", program_name, device); 1104 } 1105 else 1106 { 1107 fprintf(stderr, "%s: listening on %s\n", program_name, device); 1108 } 1109 1110 fflush(stderr); 1111 #endif /* WIN32 */ 1112 #ifdef HAVE_PCAP_CREATE 1113 pd = pcap_create(device, ebuf); 1114 if (pd == NULL) 1115 error("%s", ebuf); 1116 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE 1117 if (Jflag) 1118 show_tstamp_types_and_exit(device, pd); 1119 #endif 1120 /* 1121 * Is this an interface that supports monitor mode? 1122 */ 1123 if (pcap_can_set_rfmon(pd) == 1) 1124 supports_monitor_mode = 1; 1125 else 1126 supports_monitor_mode = 0; 1127 status = pcap_set_snaplen(pd, snaplen); 1128 if (status != 0) 1129 error("%s: Can't set snapshot length: %s", 1130 device, pcap_statustostr(status)); 1131 status = pcap_set_promisc(pd, !pflag); 1132 if (status != 0) 1133 error("%s: Can't set promiscuous mode: %s", 1134 device, pcap_statustostr(status)); 1135 if (Iflag) { 1136 status = pcap_set_rfmon(pd, 1); 1137 if (status != 0) 1138 error("%s: Can't set monitor mode: %s", 1139 device, pcap_statustostr(status)); 1140 } 1141 status = pcap_set_timeout(pd, 1000); 1142 if (status != 0) 1143 error("%s: pcap_set_timeout failed: %s", 1144 device, pcap_statustostr(status)); 1145 if (Bflag != 0) { 1146 status = pcap_set_buffer_size(pd, Bflag); 1147 if (status != 0) 1148 error("%s: Can't set buffer size: %s", 1149 device, pcap_statustostr(status)); 1150 } 1151 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE 1152 if (jflag != -1) { 1153 status = pcap_set_tstamp_type(pd, jflag); 1154 if (status < 0) 1155 error("%s: Can't set time stamp type: %s", 1156 device, pcap_statustostr(status)); 1157 } 1158 #endif 1159 status = pcap_activate(pd); 1160 if (status < 0) { 1161 /* 1162 * pcap_activate() failed. 1163 */ 1164 cp = pcap_geterr(pd); 1165 if (status == PCAP_ERROR) 1166 error("%s", cp); 1167 else if ((status == PCAP_ERROR_NO_SUCH_DEVICE || 1168 status == PCAP_ERROR_PERM_DENIED) && 1169 *cp != '\0') 1170 error("%s: %s\n(%s)", device, 1171 pcap_statustostr(status), cp); 1172 else 1173 error("%s: %s", device, 1174 pcap_statustostr(status)); 1175 } else if (status > 0) { 1176 /* 1177 * pcap_activate() succeeded, but it's warning us 1178 * of a problem it had. 1179 */ 1180 cp = pcap_geterr(pd); 1181 if (status == PCAP_WARNING) 1182 warning("%s", cp); 1183 else if (status == PCAP_WARNING_PROMISC_NOTSUP && 1184 *cp != '\0') 1185 warning("%s: %s\n(%s)", device, 1186 pcap_statustostr(status), cp); 1187 else 1188 warning("%s: %s", device, 1189 pcap_statustostr(status)); 1190 } 1191 #else 1192 *ebuf = '\0'; 1193 pd = pcap_open_live(device, snaplen, !pflag, 1000, ebuf); 1194 if (pd == NULL) 1195 error("%s", ebuf); 1196 else if (*ebuf) 1197 warning("%s", ebuf); 1198 #endif /* HAVE_PCAP_CREATE */ 1199 /* 1200 * Let user own process after socket has been opened. 1201 */ 1202 #ifndef WIN32 1203 if (setgid(getgid()) != 0 || setuid(getuid()) != 0) 1204 fprintf(stderr, "Warning: setgid/setuid failed !\n"); 1205 #endif /* WIN32 */ 1206 #if !defined(HAVE_PCAP_CREATE) && defined(WIN32) 1207 if(Bflag != 0) 1208 if(pcap_setbuff(pd, Bflag)==-1){ 1209 error("%s", pcap_geterr(pd)); 1210 } 1211 #endif /* !defined(HAVE_PCAP_CREATE) && defined(WIN32) */ 1212 if (Lflag) 1213 show_dlts_and_exit(device, pd); 1214 if (gndo->ndo_dlt >= 0) { 1215 #ifdef HAVE_PCAP_SET_DATALINK 1216 if (pcap_set_datalink(pd, gndo->ndo_dlt) < 0) 1217 error("%s", pcap_geterr(pd)); 1218 #else 1219 /* 1220 * We don't actually support changing the 1221 * data link type, so we only let them 1222 * set it to what it already is. 1223 */ 1224 if (gndo->ndo_dlt != pcap_datalink(pd)) { 1225 error("%s is not one of the DLTs supported by this device\n", 1226 gndo->ndo_dltname); 1227 } 1228 #endif 1229 (void)fprintf(stderr, "%s: data link type %s\n", 1230 program_name, gndo->ndo_dltname); 1231 (void)fflush(stderr); 1232 } 1233 i = pcap_snapshot(pd); 1234 if (snaplen < i) { 1235 warning("snaplen raised from %d to %d", snaplen, i); 1236 snaplen = i; 1237 } 1238 if (pcap_lookupnet(device, &localnet, &netmask, ebuf) < 0) { 1239 localnet = 0; 1240 netmask = 0; 1241 warning("%s", ebuf); 1242 } 1243 } 1244 if (infile) 1245 cmdbuf = read_infile(infile); 1246 else 1247 cmdbuf = copy_argv(&argv[optind]); 1248 1249 if (pcap_compile(pd, &fcode, cmdbuf, Oflag, netmask) < 0) 1250 error("%s", pcap_geterr(pd)); 1251 free(cmdbuf); 1252 if (dflag) { 1253 bpf_dump(&fcode, dflag); 1254 pcap_close(pd); 1255 exit(0); 1256 } 1257 init_addrtoname(localnet, netmask); 1258 init_checksum(); 1259 1260 #ifndef WIN32 1261 (void)setsignal(SIGPIPE, cleanup); 1262 (void)setsignal(SIGTERM, cleanup); 1263 (void)setsignal(SIGINT, cleanup); 1264 #endif /* WIN32 */ 1265 #if defined(HAVE_FORK) || defined(HAVE_VFORK) 1266 (void)setsignal(SIGCHLD, child_cleanup); 1267 #endif 1268 /* Cooperate with nohup(1) */ 1269 #ifndef WIN32 1270 if ((oldhandler = setsignal(SIGHUP, cleanup)) != SIG_DFL) 1271 (void)setsignal(SIGHUP, oldhandler); 1272 #endif /* WIN32 */ 1273 1274 #ifndef WIN32 1275 /* 1276 * If a user name was specified with "-Z", attempt to switch to 1277 * that user's UID. This would probably be used with sudo, 1278 * to allow tcpdump to be run in a special restricted 1279 * account (if you just want to allow users to open capture 1280 * devices, and can't just give users that permission, 1281 * you'd make tcpdump set-UID or set-GID). 1282 * 1283 * Tcpdump doesn't necessarily write only to one savefile; 1284 * the general only way to allow a -Z instance to write to 1285 * savefiles as the user under whose UID it's run, rather 1286 * than as the user specified with -Z, would thus be to switch 1287 * to the original user ID before opening a capture file and 1288 * then switch back to the -Z user ID after opening the savefile. 1289 * Switching to the -Z user ID only after opening the first 1290 * savefile doesn't handle the general case. 1291 */ 1292 if (getuid() == 0 || geteuid() == 0) { 1293 if (username || chroot_dir) 1294 droproot(username, chroot_dir); 1295 } 1296 #endif /* WIN32 */ 1297 1298 if (pcap_setfilter(pd, &fcode) < 0) 1299 error("%s", pcap_geterr(pd)); 1300 if (WFileName) { 1301 pcap_dumper_t *p; 1302 /* Do not exceed the default NAME_MAX for files. */ 1303 dumpinfo.CurrentFileName = (char *)malloc(NAME_MAX + 1); 1304 1305 if (dumpinfo.CurrentFileName == NULL) 1306 error("malloc of dumpinfo.CurrentFileName"); 1307 1308 /* We do not need numbering for dumpfiles if Cflag isn't set. */ 1309 if (Cflag != 0) 1310 MakeFilename(dumpinfo.CurrentFileName, WFileName, 0, WflagChars); 1311 else 1312 MakeFilename(dumpinfo.CurrentFileName, WFileName, 0, 0); 1313 1314 p = pcap_dump_open(pd, dumpinfo.CurrentFileName); 1315 if (p == NULL) 1316 error("%s", pcap_geterr(pd)); 1317 if (Cflag != 0 || Gflag != 0) { 1318 callback = dump_packet_and_trunc; 1319 dumpinfo.WFileName = WFileName; 1320 dumpinfo.pd = pd; 1321 dumpinfo.p = p; 1322 pcap_userdata = (u_char *)&dumpinfo; 1323 } else { 1324 callback = dump_packet; 1325 pcap_userdata = (u_char *)p; 1326 } 1327 #ifdef HAVE_PCAP_DUMP_FLUSH 1328 if (Uflag) 1329 pcap_dump_flush(p); 1330 #endif 1331 } else { 1332 type = pcap_datalink(pd); 1333 printinfo.ndo_type = 1; 1334 printinfo.ndo = gndo; 1335 printinfo.p.ndo_printer = lookup_ndo_printer(type); 1336 if (printinfo.p.ndo_printer == NULL) { 1337 printinfo.p.printer = lookup_printer(type); 1338 printinfo.ndo_type = 0; 1339 if (printinfo.p.printer == NULL) { 1340 gndo->ndo_dltname = pcap_datalink_val_to_name(type); 1341 if (gndo->ndo_dltname != NULL) 1342 error("packet printing is not supported for link type %s: use -w", 1343 gndo->ndo_dltname); 1344 else 1345 error("packet printing is not supported for link type %d: use -w", type); 1346 } 1347 } 1348 callback = print_packet; 1349 pcap_userdata = (u_char *)&printinfo; 1350 } 1351 1352 #ifdef SIGINFO 1353 /* 1354 * We can't get statistics when reading from a file rather 1355 * than capturing from a device. 1356 */ 1357 if (RFileName == NULL) 1358 (void)setsignal(SIGINFO, requestinfo); 1359 #endif 1360 1361 if (vflag > 0 && WFileName) { 1362 /* 1363 * When capturing to a file, "-v" means tcpdump should, 1364 * every 10 secodns, "v"erbosely report the number of 1365 * packets captured. 1366 */ 1367 #ifdef USE_WIN32_MM_TIMER 1368 /* call verbose_stats_dump() each 1000 +/-100msec */ 1369 timer_id = timeSetEvent(1000, 100, verbose_stats_dump, 0, TIME_PERIODIC); 1370 setvbuf(stderr, NULL, _IONBF, 0); 1371 #elif defined(HAVE_ALARM) 1372 (void)setsignal(SIGALRM, verbose_stats_dump); 1373 alarm(1); 1374 #endif 1375 } 1376 1377 #ifndef WIN32 1378 if (RFileName == NULL) { 1379 int dlt; 1380 const char *dlt_name; 1381 1382 if (!vflag && !WFileName) { 1383 (void)fprintf(stderr, 1384 "%s: verbose output suppressed, use -v or -vv for full protocol decode\n", 1385 program_name); 1386 } else 1387 (void)fprintf(stderr, "%s: ", program_name); 1388 dlt = pcap_datalink(pd); 1389 dlt_name = pcap_datalink_val_to_name(dlt); 1390 if (dlt_name == NULL) { 1391 (void)fprintf(stderr, "listening on %s, link-type %u, capture size %u bytes\n", 1392 device, dlt, snaplen); 1393 } else { 1394 (void)fprintf(stderr, "listening on %s, link-type %s (%s), capture size %u bytes\n", 1395 device, dlt_name, 1396 pcap_datalink_val_to_description(dlt), snaplen); 1397 } 1398 (void)fflush(stderr); 1399 } 1400 #endif /* WIN32 */ 1401 status = pcap_loop(pd, cnt, callback, pcap_userdata); 1402 if (WFileName == NULL) { 1403 /* 1404 * We're printing packets. Flush the printed output, 1405 * so it doesn't get intermingled with error output. 1406 */ 1407 if (status == -2) { 1408 /* 1409 * We got interrupted, so perhaps we didn't 1410 * manage to finish a line we were printing. 1411 * Print an extra newline, just in case. 1412 */ 1413 putchar('\n'); 1414 } 1415 (void)fflush(stdout); 1416 } 1417 if (status == -1) { 1418 /* 1419 * Error. Report it. 1420 */ 1421 (void)fprintf(stderr, "%s: pcap_loop: %s\n", 1422 program_name, pcap_geterr(pd)); 1423 } 1424 if (RFileName == NULL) { 1425 /* 1426 * We're doing a live capture. Report the capture 1427 * statistics. 1428 */ 1429 info(1); 1430 } 1431 pcap_close(pd); 1432 exit(status == -1 ? 1 : 0); 1433 } 1434 1435 /* make a clean exit on interrupts */ 1436 static RETSIGTYPE 1437 cleanup(int signo _U_) 1438 { 1439 #ifdef USE_WIN32_MM_TIMER 1440 if (timer_id) 1441 timeKillEvent(timer_id); 1442 timer_id = 0; 1443 #elif defined(HAVE_ALARM) 1444 alarm(0); 1445 #endif 1446 1447 #ifdef HAVE_PCAP_BREAKLOOP 1448 /* 1449 * We have "pcap_breakloop()"; use it, so that we do as little 1450 * as possible in the signal handler (it's probably not safe 1451 * to do anything with standard I/O streams in a signal handler - 1452 * the ANSI C standard doesn't say it is). 1453 */ 1454 pcap_breakloop(pd); 1455 #else 1456 /* 1457 * We don't have "pcap_breakloop()"; this isn't safe, but 1458 * it's the best we can do. Print the summary if we're 1459 * not reading from a savefile - i.e., if we're doing a 1460 * live capture - and exit. 1461 */ 1462 if (pd != NULL && pcap_file(pd) == NULL) { 1463 /* 1464 * We got interrupted, so perhaps we didn't 1465 * manage to finish a line we were printing. 1466 * Print an extra newline, just in case. 1467 */ 1468 putchar('\n'); 1469 (void)fflush(stdout); 1470 info(1); 1471 } 1472 exit(0); 1473 #endif 1474 } 1475 1476 /* 1477 On windows, we do not use a fork, so we do not care less about 1478 waiting a child processes to die 1479 */ 1480 #if defined(HAVE_FORK) || defined(HAVE_VFORK) 1481 static RETSIGTYPE 1482 child_cleanup(int signo _U_) 1483 { 1484 wait(NULL); 1485 } 1486 #endif /* HAVE_FORK && HAVE_VFORK */ 1487 1488 static void 1489 info(register int verbose) 1490 { 1491 struct pcap_stat stat; 1492 1493 /* 1494 * Older versions of libpcap didn't set ps_ifdrop on some 1495 * platforms; initialize it to 0 to handle that. 1496 */ 1497 stat.ps_ifdrop = 0; 1498 if (pcap_stats(pd, &stat) < 0) { 1499 (void)fprintf(stderr, "pcap_stats: %s\n", pcap_geterr(pd)); 1500 infoprint = 0; 1501 return; 1502 } 1503 1504 if (!verbose) 1505 fprintf(stderr, "%s: ", program_name); 1506 1507 (void)fprintf(stderr, "%u packet%s captured", packets_captured, 1508 PLURAL_SUFFIX(packets_captured)); 1509 if (!verbose) 1510 fputs(", ", stderr); 1511 else 1512 putc('\n', stderr); 1513 (void)fprintf(stderr, "%u packet%s received by filter", stat.ps_recv, 1514 PLURAL_SUFFIX(stat.ps_recv)); 1515 if (!verbose) 1516 fputs(", ", stderr); 1517 else 1518 putc('\n', stderr); 1519 (void)fprintf(stderr, "%u packet%s dropped by kernel", stat.ps_drop, 1520 PLURAL_SUFFIX(stat.ps_drop)); 1521 if (stat.ps_ifdrop != 0) { 1522 if (!verbose) 1523 fputs(", ", stderr); 1524 else 1525 putc('\n', stderr); 1526 (void)fprintf(stderr, "%u packet%s dropped by interface\n", 1527 stat.ps_ifdrop, PLURAL_SUFFIX(stat.ps_ifdrop)); 1528 } else 1529 putc('\n', stderr); 1530 infoprint = 0; 1531 } 1532 1533 #if defined(HAVE_FORK) || defined(HAVE_VFORK) 1534 static void 1535 compress_savefile(const char *filename) 1536 { 1537 # ifdef HAVE_FORK 1538 if (fork()) 1539 # else 1540 if (vfork()) 1541 # endif 1542 return; 1543 /* 1544 * Set to lowest priority so that this doesn't disturb the capture 1545 */ 1546 #ifdef NZERO 1547 setpriority(PRIO_PROCESS, 0, NZERO - 1); 1548 #else 1549 setpriority(PRIO_PROCESS, 0, 19); 1550 #endif 1551 if (execlp(zflag, zflag, filename, (char *)NULL) == -1) 1552 fprintf(stderr, 1553 "compress_savefile:execlp(%s, %s): %s\n", 1554 zflag, 1555 filename, 1556 strerror(errno)); 1557 # ifdef HAVE_FORK 1558 exit(1); 1559 # else 1560 _exit(1); 1561 # endif 1562 } 1563 #else /* HAVE_FORK && HAVE_VFORK */ 1564 static void 1565 compress_savefile(const char *filename) 1566 { 1567 fprintf(stderr, 1568 "compress_savefile failed. Functionality not implemented under your system\n"); 1569 } 1570 #endif /* HAVE_FORK && HAVE_VFORK */ 1571 1572 static void 1573 dump_packet_and_trunc(u_char *user, const struct pcap_pkthdr *h, const u_char *sp) 1574 { 1575 struct dump_info *dump_info; 1576 1577 ++packets_captured; 1578 1579 ++infodelay; 1580 1581 dump_info = (struct dump_info *)user; 1582 1583 /* 1584 * XXX - this won't force the file to rotate on the specified time 1585 * boundary, but it will rotate on the first packet received after the 1586 * specified Gflag number of seconds. Note: if a Gflag time boundary 1587 * and a Cflag size boundary coincide, the time rotation will occur 1588 * first thereby cancelling the Cflag boundary (since the file should 1589 * be 0). 1590 */ 1591 if (Gflag != 0) { 1592 /* Check if it is time to rotate */ 1593 time_t t; 1594 1595 /* Get the current time */ 1596 if ((t = time(NULL)) == (time_t)-1) { 1597 error("dump_and_trunc_packet: can't get current_time: %s", 1598 pcap_strerror(errno)); 1599 } 1600 1601 1602 /* If the time is greater than the specified window, rotate */ 1603 if (t - Gflag_time >= Gflag) { 1604 /* Update the Gflag_time */ 1605 Gflag_time = t; 1606 /* Update Gflag_count */ 1607 Gflag_count++; 1608 /* 1609 * Close the current file and open a new one. 1610 */ 1611 pcap_dump_close(dump_info->p); 1612 1613 /* 1614 * Compress the file we just closed, if the user asked for it 1615 */ 1616 if (zflag != NULL) 1617 compress_savefile(dump_info->CurrentFileName); 1618 1619 /* 1620 * Check to see if we've exceeded the Wflag (when 1621 * not using Cflag). 1622 */ 1623 if (Cflag == 0 && Wflag > 0 && Gflag_count >= Wflag) { 1624 (void)fprintf(stderr, "Maximum file limit reached: %d\n", 1625 Wflag); 1626 exit(0); 1627 /* NOTREACHED */ 1628 } 1629 if (dump_info->CurrentFileName != NULL) 1630 free(dump_info->CurrentFileName); 1631 /* Allocate space for max filename + \0. */ 1632 dump_info->CurrentFileName = (char *)malloc(NAME_MAX + 1); 1633 if (dump_info->CurrentFileName == NULL) 1634 error("dump_packet_and_trunc: malloc"); 1635 /* 1636 * This is always the first file in the Cflag 1637 * rotation: e.g. 0 1638 * We also don't need numbering if Cflag is not set. 1639 */ 1640 if (Cflag != 0) 1641 MakeFilename(dump_info->CurrentFileName, dump_info->WFileName, 0, 1642 WflagChars); 1643 else 1644 MakeFilename(dump_info->CurrentFileName, dump_info->WFileName, 0, 0); 1645 1646 dump_info->p = pcap_dump_open(dump_info->pd, dump_info->CurrentFileName); 1647 if (dump_info->p == NULL) 1648 error("%s", pcap_geterr(pd)); 1649 } 1650 } 1651 1652 /* 1653 * XXX - this won't prevent capture files from getting 1654 * larger than Cflag - the last packet written to the 1655 * file could put it over Cflag. 1656 */ 1657 if (Cflag != 0 && pcap_dump_ftell(dump_info->p) > Cflag) { 1658 /* 1659 * Close the current file and open a new one. 1660 */ 1661 pcap_dump_close(dump_info->p); 1662 1663 /* 1664 * Compress the file we just closed, if the user asked for it 1665 */ 1666 if (zflag != NULL) 1667 compress_savefile(dump_info->CurrentFileName); 1668 1669 Cflag_count++; 1670 if (Wflag > 0) { 1671 if (Cflag_count >= Wflag) 1672 Cflag_count = 0; 1673 } 1674 if (dump_info->CurrentFileName != NULL) 1675 free(dump_info->CurrentFileName); 1676 dump_info->CurrentFileName = (char *)malloc(NAME_MAX + 1); 1677 if (dump_info->CurrentFileName == NULL) 1678 error("dump_packet_and_trunc: malloc"); 1679 MakeFilename(dump_info->CurrentFileName, dump_info->WFileName, Cflag_count, WflagChars); 1680 dump_info->p = pcap_dump_open(dump_info->pd, dump_info->CurrentFileName); 1681 if (dump_info->p == NULL) 1682 error("%s", pcap_geterr(pd)); 1683 } 1684 1685 pcap_dump((u_char *)dump_info->p, h, sp); 1686 #ifdef HAVE_PCAP_DUMP_FLUSH 1687 if (Uflag) 1688 pcap_dump_flush(dump_info->p); 1689 #endif 1690 1691 --infodelay; 1692 if (infoprint) 1693 info(0); 1694 } 1695 1696 static void 1697 dump_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *sp) 1698 { 1699 ++packets_captured; 1700 1701 ++infodelay; 1702 1703 pcap_dump(user, h, sp); 1704 #ifdef HAVE_PCAP_DUMP_FLUSH 1705 if (Uflag) 1706 pcap_dump_flush((pcap_dumper_t *)user); 1707 #endif 1708 1709 --infodelay; 1710 if (infoprint) 1711 info(0); 1712 } 1713 1714 static void 1715 print_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *sp) 1716 { 1717 struct print_info *print_info; 1718 u_int hdrlen; 1719 1720 ++packets_captured; 1721 1722 ++infodelay; 1723 ts_print(&h->ts); 1724 1725 print_info = (struct print_info *)user; 1726 1727 /* 1728 * Some printers want to check that they're not walking off the 1729 * end of the packet. 1730 * Rather than pass it all the way down, we set this global. 1731 */ 1732 snapend = sp + h->caplen; 1733 1734 if(print_info->ndo_type) { 1735 hdrlen = (*print_info->p.ndo_printer)(print_info->ndo, h, sp); 1736 } else { 1737 hdrlen = (*print_info->p.printer)(h, sp); 1738 } 1739 1740 if (Xflag) { 1741 /* 1742 * Print the raw packet data in hex and ASCII. 1743 */ 1744 if (Xflag > 1) { 1745 /* 1746 * Include the link-layer header. 1747 */ 1748 hex_and_ascii_print("\n\t", sp, h->caplen); 1749 } else { 1750 /* 1751 * Don't include the link-layer header - and if 1752 * we have nothing past the link-layer header, 1753 * print nothing. 1754 */ 1755 if (h->caplen > hdrlen) 1756 hex_and_ascii_print("\n\t", sp + hdrlen, 1757 h->caplen - hdrlen); 1758 } 1759 } else if (xflag) { 1760 /* 1761 * Print the raw packet data in hex. 1762 */ 1763 if (xflag > 1) { 1764 /* 1765 * Include the link-layer header. 1766 */ 1767 hex_print("\n\t", sp, h->caplen); 1768 } else { 1769 /* 1770 * Don't include the link-layer header - and if 1771 * we have nothing past the link-layer header, 1772 * print nothing. 1773 */ 1774 if (h->caplen > hdrlen) 1775 hex_print("\n\t", sp + hdrlen, 1776 h->caplen - hdrlen); 1777 } 1778 } else if (Aflag) { 1779 /* 1780 * Print the raw packet data in ASCII. 1781 */ 1782 if (Aflag > 1) { 1783 /* 1784 * Include the link-layer header. 1785 */ 1786 ascii_print(sp, h->caplen); 1787 } else { 1788 /* 1789 * Don't include the link-layer header - and if 1790 * we have nothing past the link-layer header, 1791 * print nothing. 1792 */ 1793 if (h->caplen > hdrlen) 1794 ascii_print(sp + hdrlen, h->caplen - hdrlen); 1795 } 1796 } 1797 1798 putchar('\n'); 1799 1800 --infodelay; 1801 if (infoprint) 1802 info(0); 1803 } 1804 1805 #ifdef WIN32 1806 /* 1807 * XXX - there should really be libpcap calls to get the version 1808 * number as a string (the string would be generated from #defines 1809 * at run time, so that it's not generated from string constants 1810 * in the library, as, on many UNIX systems, those constants would 1811 * be statically linked into the application executable image, and 1812 * would thus reflect the version of libpcap on the system on 1813 * which the application was *linked*, not the system on which it's 1814 * *running*. 1815 * 1816 * That routine should be documented, unlike the "version[]" 1817 * string, so that UNIX vendors providing their own libpcaps 1818 * don't omit it (as a couple of vendors have...). 1819 * 1820 * Packet.dll should perhaps also export a routine to return the 1821 * version number of the Packet.dll code, to supply the 1822 * "Wpcap_version" information on Windows. 1823 */ 1824 char WDversion[]="current-cvs.tcpdump.org"; 1825 #if !defined(HAVE_GENERATED_VERSION) 1826 char version[]="current-cvs.tcpdump.org"; 1827 #endif 1828 char pcap_version[]="current-cvs.tcpdump.org"; 1829 char Wpcap_version[]="3.1"; 1830 #endif 1831 1832 /* 1833 * By default, print the specified data out in hex and ASCII. 1834 */ 1835 static void 1836 ndo_default_print(netdissect_options *ndo _U_, const u_char *bp, u_int length) 1837 { 1838 hex_and_ascii_print("\n\t", bp, length); /* pass on lf and identation string */ 1839 } 1840 1841 void 1842 default_print(const u_char *bp, u_int length) 1843 { 1844 ndo_default_print(gndo, bp, length); 1845 } 1846 1847 #ifdef SIGINFO 1848 RETSIGTYPE requestinfo(int signo _U_) 1849 { 1850 if (infodelay) 1851 ++infoprint; 1852 else 1853 info(0); 1854 } 1855 #endif 1856 1857 /* 1858 * Called once each second in verbose mode while dumping to file 1859 */ 1860 #ifdef USE_WIN32_MM_TIMER 1861 void CALLBACK verbose_stats_dump (UINT timer_id _U_, UINT msg _U_, DWORD_PTR arg _U_, 1862 DWORD_PTR dw1 _U_, DWORD_PTR dw2 _U_) 1863 { 1864 struct pcap_stat stat; 1865 1866 if (infodelay == 0 && pcap_stats(pd, &stat) >= 0) 1867 fprintf(stderr, "Got %u\r", packets_captured); 1868 } 1869 #elif defined(HAVE_ALARM) 1870 static void verbose_stats_dump(int sig _U_) 1871 { 1872 struct pcap_stat stat; 1873 1874 if (infodelay == 0 && pcap_stats(pd, &stat) >= 0) 1875 fprintf(stderr, "Got %u\r", packets_captured); 1876 alarm(1); 1877 } 1878 #endif 1879 1880 static void 1881 usage(void) 1882 { 1883 extern char version[]; 1884 #ifndef HAVE_PCAP_LIB_VERSION 1885 #if defined(WIN32) || defined(HAVE_PCAP_VERSION) 1886 extern char pcap_version[]; 1887 #else /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */ 1888 static char pcap_version[] = "unknown"; 1889 #endif /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */ 1890 #endif /* HAVE_PCAP_LIB_VERSION */ 1891 1892 #ifdef HAVE_PCAP_LIB_VERSION 1893 #ifdef WIN32 1894 (void)fprintf(stderr, "%s version %s, based on tcpdump version %s\n", program_name, WDversion, version); 1895 #else /* WIN32 */ 1896 (void)fprintf(stderr, "%s version %s\n", program_name, version); 1897 #endif /* WIN32 */ 1898 (void)fprintf(stderr, "%s\n",pcap_lib_version()); 1899 #else /* HAVE_PCAP_LIB_VERSION */ 1900 #ifdef WIN32 1901 (void)fprintf(stderr, "%s version %s, based on tcpdump version %s\n", program_name, WDversion, version); 1902 (void)fprintf(stderr, "WinPcap version %s, based on libpcap version %s\n",Wpcap_version, pcap_version); 1903 #else /* WIN32 */ 1904 (void)fprintf(stderr, "%s version %s\n", program_name, version); 1905 (void)fprintf(stderr, "libpcap version %s\n", pcap_version); 1906 #endif /* WIN32 */ 1907 #endif /* HAVE_PCAP_LIB_VERSION */ 1908 (void)fprintf(stderr, 1909 "Usage: %s [-aAbd" D_FLAG "efhH" I_FLAG J_FLAG "KlLnNOpqRStu" U_FLAG "vxX]" B_FLAG_USAGE " [ -c count ]\n", program_name); 1910 (void)fprintf(stderr, 1911 "\t\t[ -C file_size ] [ -E algo:secret ] [ -F file ] [ -G seconds ]\n"); 1912 (void)fprintf(stderr, 1913 "\t\t[ -i interface ]" j_FLAG_USAGE " [ -M secret ]\n"); 1914 (void)fprintf(stderr, 1915 "\t\t[ -r file ] [ -s snaplen ] [ -T type ] [ -w file ]\n"); 1916 (void)fprintf(stderr, 1917 "\t\t[ -W filecount ] [ -y datalinktype ] [ -z command ]\n"); 1918 (void)fprintf(stderr, 1919 "\t\t[ -Z user ] [ expression ]\n"); 1920 exit(1); 1921 } 1922 1923 1924 1925 /* VARARGS */ 1926 static void 1927 ndo_error(netdissect_options *ndo _U_, const char *fmt, ...) 1928 { 1929 va_list ap; 1930 1931 (void)fprintf(stderr, "%s: ", program_name); 1932 va_start(ap, fmt); 1933 (void)vfprintf(stderr, fmt, ap); 1934 va_end(ap); 1935 if (*fmt) { 1936 fmt += strlen(fmt); 1937 if (fmt[-1] != '\n') 1938 (void)fputc('\n', stderr); 1939 } 1940 exit(1); 1941 /* NOTREACHED */ 1942 } 1943 1944 /* VARARGS */ 1945 static void 1946 ndo_warning(netdissect_options *ndo _U_, const char *fmt, ...) 1947 { 1948 va_list ap; 1949 1950 (void)fprintf(stderr, "%s: WARNING: ", program_name); 1951 va_start(ap, fmt); 1952 (void)vfprintf(stderr, fmt, ap); 1953 va_end(ap); 1954 if (*fmt) { 1955 fmt += strlen(fmt); 1956 if (fmt[-1] != '\n') 1957 (void)fputc('\n', stderr); 1958 } 1959 } 1960