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