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 #endif 33 34 /* 35 * tcpdump - dump traffic on a network 36 * 37 * First written in 1987 by Van Jacobson, Lawrence Berkeley Laboratory. 38 * Mercilessly hacked and occasionally improved since then via the 39 * combined efforts of Van, Steve McCanne and Craig Leres of LBL. 40 */ 41 42 #ifdef HAVE_CONFIG_H 43 #include "config.h" 44 #endif 45 46 /* 47 * Mac OS X may ship pcap.h from libpcap 0.6 with a libpcap based on 48 * 0.8. That means it has pcap_findalldevs() but the header doesn't 49 * define pcap_if_t, meaning that we can't actually *use* pcap_findalldevs(). 50 */ 51 #ifdef HAVE_PCAP_FINDALLDEVS 52 #ifndef HAVE_PCAP_IF_T 53 #undef HAVE_PCAP_FINDALLDEVS 54 #endif 55 #endif 56 57 #include <netdissect-stdinc.h> 58 59 #include <sys/stat.h> 60 61 #ifdef HAVE_FCNTL_H 62 #include <fcntl.h> 63 #endif 64 65 #ifdef HAVE_LIBCRYPTO 66 #include <openssl/crypto.h> 67 #endif 68 69 #ifdef HAVE_GETOPT_LONG 70 #include <getopt.h> 71 #else 72 #include "getopt_long.h" 73 #endif 74 /* Capsicum-specific code requires macros from <net/bpf.h>, which will fail 75 * to compile if <pcap.h> has already been included; including the headers 76 * in the opposite order works fine. 77 */ 78 #ifdef HAVE_CAPSICUM 79 #include <sys/capsicum.h> 80 #include <sys/sysctl.h> 81 #include <sys/nv.h> 82 #include <sys/ioccom.h> 83 #include <net/bpf.h> 84 #include <libgen.h> 85 #ifdef HAVE_CASPER 86 #include <libcasper.h> 87 #include <casper/cap_dns.h> 88 #endif /* HAVE_CASPER */ 89 #endif /* HAVE_CAPSICUM */ 90 #include <pcap.h> 91 #include <signal.h> 92 #include <stdio.h> 93 #include <stdarg.h> 94 #include <stdlib.h> 95 #include <string.h> 96 #include <limits.h> 97 #ifndef _WIN32 98 #include <sys/wait.h> 99 #include <sys/resource.h> 100 #include <pwd.h> 101 #include <grp.h> 102 #endif /* _WIN32 */ 103 104 /* capabilities convenience library */ 105 /* If a code depends on HAVE_LIBCAP_NG, it depends also on HAVE_CAP_NG_H. 106 * If HAVE_CAP_NG_H is not defined, undefine HAVE_LIBCAP_NG. 107 * Thus, the later tests are done only on HAVE_LIBCAP_NG. 108 */ 109 #ifdef HAVE_LIBCAP_NG 110 #ifdef HAVE_CAP_NG_H 111 #include <cap-ng.h> 112 #else 113 #undef HAVE_LIBCAP_NG 114 #endif /* HAVE_CAP_NG_H */ 115 #endif /* HAVE_LIBCAP_NG */ 116 117 #include "netdissect.h" 118 #include "interface.h" 119 #include "addrtoname.h" 120 #include "machdep.h" 121 #include "setsignal.h" 122 #include "gmt2local.h" 123 #include "pcap-missing.h" 124 #include "ascii_strcasecmp.h" 125 126 #include "print.h" 127 128 #ifndef PATH_MAX 129 #define PATH_MAX 1024 130 #endif 131 132 #ifdef SIGINFO 133 #define SIGNAL_REQ_INFO SIGINFO 134 #elif SIGUSR1 135 #define SIGNAL_REQ_INFO SIGUSR1 136 #endif 137 138 static int Bflag; /* buffer size */ 139 static int Cflag; /* rotate dump files after this many bytes */ 140 static int Cflag_count; /* Keep track of which file number we're writing */ 141 static int Dflag; /* list available devices and exit */ 142 /* 143 * This is exported because, in some versions of libpcap, if libpcap 144 * is built with optimizer debugging code (which is *NOT* the default 145 * configuration!), the library *imports*(!) a variable named dflag, 146 * under the expectation that tcpdump is exporting it, to govern 147 * how much debugging information to print when optimizing 148 * the generated BPF code. 149 * 150 * This is a horrible hack; newer versions of libpcap don't import 151 * dflag but, instead, *if* built with optimizer debugging code, 152 * *export* a routine to set that flag. 153 */ 154 int dflag; /* print filter code */ 155 static int Gflag; /* rotate dump files after this many seconds */ 156 static int Gflag_count; /* number of files created with Gflag rotation */ 157 static time_t Gflag_time; /* The last time_t the dump file was rotated. */ 158 static int Lflag; /* list available data link types and exit */ 159 static int Iflag; /* rfmon (monitor) mode */ 160 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE 161 static int Jflag; /* list available time stamp types */ 162 #endif 163 static int jflag = -1; /* packet time stamp source */ 164 static int pflag; /* don't go promiscuous */ 165 #ifdef HAVE_PCAP_SETDIRECTION 166 static int Qflag = -1; /* restrict captured packet by send/receive direction */ 167 #endif 168 static int Uflag; /* "unbuffered" output of dump files */ 169 static int Wflag; /* recycle output files after this number of files */ 170 static int WflagChars; 171 static char *zflag = NULL; /* compress each savefile using a specified command (like gzip or bzip2) */ 172 static int immediate_mode; 173 174 static int infodelay; 175 static int infoprint; 176 177 char *program_name; 178 179 #ifdef HAVE_CASPER 180 cap_channel_t *capdns; 181 #endif 182 183 /* Forwards */ 184 static void error(const char *, ...) 185 __attribute__((noreturn)) 186 #ifdef __ATTRIBUTE___FORMAT_OK 187 __attribute__((format (printf, 1, 2))) 188 #endif /* __ATTRIBUTE___FORMAT_OK */ 189 ; 190 static void warning(const char *, ...) 191 #ifdef __ATTRIBUTE___FORMAT_OK 192 __attribute__((format (printf, 1, 2))) 193 #endif /* __ATTRIBUTE___FORMAT_OK */ 194 ; 195 static void exit_tcpdump(int) __attribute__((noreturn)); 196 static RETSIGTYPE cleanup(int); 197 static RETSIGTYPE child_cleanup(int); 198 static void print_version(void); 199 static void print_usage(void); 200 static void show_tstamp_types_and_exit(pcap_t *, const char *device) __attribute__((noreturn)); 201 static void show_dlts_and_exit(pcap_t *, const char *device) __attribute__((noreturn)); 202 #ifdef HAVE_PCAP_FINDALLDEVS 203 static void show_devices_and_exit (void) __attribute__((noreturn)); 204 #endif 205 206 static void print_packet(u_char *, const struct pcap_pkthdr *, const u_char *); 207 static void dump_packet_and_trunc(u_char *, const struct pcap_pkthdr *, const u_char *); 208 static void dump_packet(u_char *, const struct pcap_pkthdr *, const u_char *); 209 static void droproot(const char *, const char *); 210 211 #ifdef SIGNAL_REQ_INFO 212 RETSIGTYPE requestinfo(int); 213 #endif 214 215 #if defined(USE_WIN32_MM_TIMER) 216 #include <MMsystem.h> 217 static UINT timer_id; 218 static void CALLBACK verbose_stats_dump(UINT, UINT, DWORD_PTR, DWORD_PTR, DWORD_PTR); 219 #elif defined(HAVE_ALARM) 220 static void verbose_stats_dump(int sig); 221 #endif 222 223 static void info(int); 224 static u_int packets_captured; 225 226 #ifdef HAVE_PCAP_FINDALLDEVS 227 static const struct tok status_flags[] = { 228 #ifdef PCAP_IF_UP 229 { PCAP_IF_UP, "Up" }, 230 #endif 231 #ifdef PCAP_IF_RUNNING 232 { PCAP_IF_RUNNING, "Running" }, 233 #endif 234 { PCAP_IF_LOOPBACK, "Loopback" }, 235 { 0, NULL } 236 }; 237 #endif 238 239 static pcap_t *pd; 240 241 static int supports_monitor_mode; 242 243 extern int optind; 244 extern int opterr; 245 extern char *optarg; 246 247 struct dump_info { 248 char *WFileName; 249 char *CurrentFileName; 250 pcap_t *pd; 251 pcap_dumper_t *p; 252 #ifdef HAVE_CAPSICUM 253 int dirfd; 254 #endif 255 }; 256 257 #if defined(HAVE_PCAP_SET_PARSER_DEBUG) 258 /* 259 * We have pcap_set_parser_debug() in libpcap; declare it (it's not declared 260 * by any libpcap header, because it's a special hack, only available if 261 * libpcap was configured to include it, and only intended for use by 262 * libpcap developers trying to debug the parser for filter expressions). 263 */ 264 #ifdef _WIN32 265 __declspec(dllimport) 266 #else /* _WIN32 */ 267 extern 268 #endif /* _WIN32 */ 269 void pcap_set_parser_debug(int); 270 #elif defined(HAVE_PCAP_DEBUG) || defined(HAVE_YYDEBUG) 271 /* 272 * We don't have pcap_set_parser_debug() in libpcap, but we do have 273 * pcap_debug or yydebug. Make a local version of pcap_set_parser_debug() 274 * to set the flag, and define HAVE_PCAP_SET_PARSER_DEBUG. 275 */ 276 static void 277 pcap_set_parser_debug(int value) 278 { 279 #ifdef HAVE_PCAP_DEBUG 280 extern int pcap_debug; 281 282 pcap_debug = value; 283 #else /* HAVE_PCAP_DEBUG */ 284 extern int yydebug; 285 286 yydebug = value; 287 #endif /* HAVE_PCAP_DEBUG */ 288 } 289 290 #define HAVE_PCAP_SET_PARSER_DEBUG 291 #endif 292 293 #if defined(HAVE_PCAP_SET_OPTIMIZER_DEBUG) 294 /* 295 * We have pcap_set_optimizer_debug() in libpcap; declare it (it's not declared 296 * by any libpcap header, because it's a special hack, only available if 297 * libpcap was configured to include it, and only intended for use by 298 * libpcap developers trying to debug the optimizer for filter expressions). 299 */ 300 #ifdef _WIN32 301 __declspec(dllimport) 302 #else /* _WIN32 */ 303 extern 304 #endif /* _WIN32 */ 305 void pcap_set_optimizer_debug(int); 306 #endif 307 308 /* VARARGS */ 309 static void 310 error(const char *fmt, ...) 311 { 312 va_list ap; 313 314 (void)fprintf(stderr, "%s: ", program_name); 315 va_start(ap, fmt); 316 (void)vfprintf(stderr, fmt, ap); 317 va_end(ap); 318 if (*fmt) { 319 fmt += strlen(fmt); 320 if (fmt[-1] != '\n') 321 (void)fputc('\n', stderr); 322 } 323 exit_tcpdump(1); 324 /* NOTREACHED */ 325 } 326 327 /* VARARGS */ 328 static void 329 warning(const char *fmt, ...) 330 { 331 va_list ap; 332 333 (void)fprintf(stderr, "%s: WARNING: ", program_name); 334 va_start(ap, fmt); 335 (void)vfprintf(stderr, fmt, ap); 336 va_end(ap); 337 if (*fmt) { 338 fmt += strlen(fmt); 339 if (fmt[-1] != '\n') 340 (void)fputc('\n', stderr); 341 } 342 } 343 344 static void 345 exit_tcpdump(int status) 346 { 347 nd_cleanup(); 348 exit(status); 349 } 350 351 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE 352 static void 353 show_tstamp_types_and_exit(pcap_t *pc, const char *device) 354 { 355 int n_tstamp_types; 356 int *tstamp_types = 0; 357 const char *tstamp_type_name; 358 int i; 359 360 n_tstamp_types = pcap_list_tstamp_types(pc, &tstamp_types); 361 if (n_tstamp_types < 0) 362 error("%s", pcap_geterr(pc)); 363 364 if (n_tstamp_types == 0) { 365 fprintf(stderr, "Time stamp type cannot be set for %s\n", 366 device); 367 exit_tcpdump(0); 368 } 369 fprintf(stderr, "Time stamp types for %s (use option -j to set):\n", 370 device); 371 for (i = 0; i < n_tstamp_types; i++) { 372 tstamp_type_name = pcap_tstamp_type_val_to_name(tstamp_types[i]); 373 if (tstamp_type_name != NULL) { 374 (void) fprintf(stderr, " %s (%s)\n", tstamp_type_name, 375 pcap_tstamp_type_val_to_description(tstamp_types[i])); 376 } else { 377 (void) fprintf(stderr, " %d\n", tstamp_types[i]); 378 } 379 } 380 pcap_free_tstamp_types(tstamp_types); 381 exit_tcpdump(0); 382 } 383 #endif 384 385 static void 386 show_dlts_and_exit(pcap_t *pc, const char *device) 387 { 388 int n_dlts, i; 389 int *dlts = 0; 390 const char *dlt_name; 391 392 n_dlts = pcap_list_datalinks(pc, &dlts); 393 if (n_dlts < 0) 394 error("%s", pcap_geterr(pc)); 395 else if (n_dlts == 0 || !dlts) 396 error("No data link types."); 397 398 /* 399 * If the interface is known to support monitor mode, indicate 400 * whether these are the data link types available when not in 401 * monitor mode, if -I wasn't specified, or when in monitor mode, 402 * when -I was specified (the link-layer types available in 403 * monitor mode might be different from the ones available when 404 * not in monitor mode). 405 */ 406 if (supports_monitor_mode) 407 (void) fprintf(stderr, "Data link types for %s %s (use option -y to set):\n", 408 device, 409 Iflag ? "when in monitor mode" : "when not in monitor mode"); 410 else 411 (void) fprintf(stderr, "Data link types for %s (use option -y to set):\n", 412 device); 413 414 for (i = 0; i < n_dlts; i++) { 415 dlt_name = pcap_datalink_val_to_name(dlts[i]); 416 if (dlt_name != NULL) { 417 (void) fprintf(stderr, " %s (%s)", dlt_name, 418 pcap_datalink_val_to_description(dlts[i])); 419 420 /* 421 * OK, does tcpdump handle that type? 422 */ 423 if (!has_printer(dlts[i])) 424 (void) fprintf(stderr, " (printing not supported)"); 425 fprintf(stderr, "\n"); 426 } else { 427 (void) fprintf(stderr, " DLT %d (printing not supported)\n", 428 dlts[i]); 429 } 430 } 431 #ifdef HAVE_PCAP_FREE_DATALINKS 432 pcap_free_datalinks(dlts); 433 #endif 434 exit_tcpdump(0); 435 } 436 437 #ifdef HAVE_PCAP_FINDALLDEVS 438 static void 439 show_devices_and_exit (void) 440 { 441 pcap_if_t *dev, *devlist; 442 char ebuf[PCAP_ERRBUF_SIZE]; 443 int i; 444 445 if (pcap_findalldevs(&devlist, ebuf) < 0) 446 error("%s", ebuf); 447 for (i = 0, dev = devlist; dev != NULL; i++, dev = dev->next) { 448 printf("%d.%s", i+1, dev->name); 449 if (dev->description != NULL) 450 printf(" (%s)", dev->description); 451 if (dev->flags != 0) 452 printf(" [%s]", bittok2str(status_flags, "none", dev->flags)); 453 printf("\n"); 454 } 455 pcap_freealldevs(devlist); 456 exit_tcpdump(0); 457 } 458 #endif /* HAVE_PCAP_FINDALLDEVS */ 459 460 /* 461 * Short options. 462 * 463 * Note that there we use all letters for short options except for g, k, 464 * o, and P, and those are used by other versions of tcpdump, and we should 465 * only use them for the same purposes that the other versions of tcpdump 466 * use them: 467 * 468 * OS X tcpdump uses -g to force non--v output for IP to be on one 469 * line, making it more "g"repable; 470 * 471 * OS X tcpdump uses -k to specify that packet comments in pcap-ng files 472 * should be printed; 473 * 474 * OpenBSD tcpdump uses -o to indicate that OS fingerprinting should be done 475 * for hosts sending TCP SYN packets; 476 * 477 * OS X tcpdump uses -P to indicate that -w should write pcap-ng rather 478 * than pcap files. 479 * 480 * OS X tcpdump also uses -Q to specify expressions that match packet 481 * metadata, including but not limited to the packet direction. 482 * The expression syntax is different from a simple "in|out|inout", 483 * and those expressions aren't accepted by OS X tcpdump, but the 484 * equivalents would be "in" = "dir=in", "out" = "dir=out", and 485 * "inout" = "dir=in or dir=out", and the parser could conceivably 486 * special-case "in", "out", and "inout" as expressions for backwards 487 * compatibility, so all is not (yet) lost. 488 */ 489 490 /* 491 * Set up flags that might or might not be supported depending on the 492 * version of libpcap we're using. 493 */ 494 #if defined(HAVE_PCAP_CREATE) || defined(_WIN32) 495 #define B_FLAG "B:" 496 #define B_FLAG_USAGE " [ -B size ]" 497 #else /* defined(HAVE_PCAP_CREATE) || defined(_WIN32) */ 498 #define B_FLAG 499 #define B_FLAG_USAGE 500 #endif /* defined(HAVE_PCAP_CREATE) || defined(_WIN32) */ 501 502 #ifdef HAVE_PCAP_CREATE 503 #define I_FLAG "I" 504 #else /* HAVE_PCAP_CREATE */ 505 #define I_FLAG 506 #endif /* HAVE_PCAP_CREATE */ 507 508 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE 509 #define j_FLAG "j:" 510 #define j_FLAG_USAGE " [ -j tstamptype ]" 511 #define J_FLAG "J" 512 #else /* PCAP_ERROR_TSTAMP_TYPE_NOTSUP */ 513 #define j_FLAG 514 #define j_FLAG_USAGE 515 #define J_FLAG 516 #endif /* PCAP_ERROR_TSTAMP_TYPE_NOTSUP */ 517 518 #ifdef HAVE_PCAP_FINDALLDEVS 519 #define D_FLAG "D" 520 #else 521 #define D_FLAG 522 #endif 523 524 #ifdef HAVE_PCAP_DUMP_FLUSH 525 #define U_FLAG "U" 526 #else 527 #define U_FLAG 528 #endif 529 530 #ifdef HAVE_PCAP_SETDIRECTION 531 #define Q_FLAG "Q:" 532 #else 533 #define Q_FLAG 534 #endif 535 536 #define SHORTOPTS "aAb" B_FLAG "c:C:d" D_FLAG "eE:fF:G:hHi:" I_FLAG j_FLAG J_FLAG "KlLm:M:nNOpq" Q_FLAG "r:s:StT:u" U_FLAG "vV:w:W:xXy:Yz:Z:#" 537 538 /* 539 * Long options. 540 * 541 * We do not currently have long options corresponding to all short 542 * options; we should probably pick appropriate option names for them. 543 * 544 * However, the short options where the number of times the option is 545 * specified matters, such as -v and -d and -t, should probably not 546 * just map to a long option, as saying 547 * 548 * tcpdump --verbose --verbose 549 * 550 * doesn't make sense; it should be --verbosity={N} or something such 551 * as that. 552 * 553 * For long options with no corresponding short options, we define values 554 * outside the range of ASCII graphic characters, make that the last 555 * component of the entry for the long option, and have a case for that 556 * option in the switch statement. 557 */ 558 #define OPTION_VERSION 128 559 #define OPTION_TSTAMP_PRECISION 129 560 #define OPTION_IMMEDIATE_MODE 130 561 562 static const struct option longopts[] = { 563 #if defined(HAVE_PCAP_CREATE) || defined(_WIN32) 564 { "buffer-size", required_argument, NULL, 'B' }, 565 #endif 566 { "list-interfaces", no_argument, NULL, 'D' }, 567 { "help", no_argument, NULL, 'h' }, 568 { "interface", required_argument, NULL, 'i' }, 569 #ifdef HAVE_PCAP_CREATE 570 { "monitor-mode", no_argument, NULL, 'I' }, 571 #endif 572 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE 573 { "time-stamp-type", required_argument, NULL, 'j' }, 574 { "list-time-stamp-types", no_argument, NULL, 'J' }, 575 #endif 576 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION 577 { "time-stamp-precision", required_argument, NULL, OPTION_TSTAMP_PRECISION}, 578 #endif 579 { "dont-verify-checksums", no_argument, NULL, 'K' }, 580 { "list-data-link-types", no_argument, NULL, 'L' }, 581 { "no-optimize", no_argument, NULL, 'O' }, 582 { "no-promiscuous-mode", no_argument, NULL, 'p' }, 583 #ifdef HAVE_PCAP_SETDIRECTION 584 { "direction", required_argument, NULL, 'Q' }, 585 #endif 586 { "snapshot-length", required_argument, NULL, 's' }, 587 { "absolute-tcp-sequence-numbers", no_argument, NULL, 'S' }, 588 #ifdef HAVE_PCAP_DUMP_FLUSH 589 { "packet-buffered", no_argument, NULL, 'U' }, 590 #endif 591 { "linktype", required_argument, NULL, 'y' }, 592 #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE 593 { "immediate-mode", no_argument, NULL, OPTION_IMMEDIATE_MODE }, 594 #endif 595 #ifdef HAVE_PCAP_SET_PARSER_DEBUG 596 { "debug-filter-parser", no_argument, NULL, 'Y' }, 597 #endif 598 { "relinquish-privileges", required_argument, NULL, 'Z' }, 599 { "number", no_argument, NULL, '#' }, 600 { "version", no_argument, NULL, OPTION_VERSION }, 601 { NULL, 0, NULL, 0 } 602 }; 603 604 #ifndef _WIN32 605 /* Drop root privileges and chroot if necessary */ 606 static void 607 droproot(const char *username, const char *chroot_dir) 608 { 609 struct passwd *pw = NULL; 610 611 if (chroot_dir && !username) { 612 fprintf(stderr, "%s: Chroot without dropping root is insecure\n", 613 program_name); 614 exit_tcpdump(1); 615 } 616 617 pw = getpwnam(username); 618 if (pw) { 619 if (chroot_dir) { 620 if (chroot(chroot_dir) != 0 || chdir ("/") != 0) { 621 fprintf(stderr, "%s: Couldn't chroot/chdir to '%.64s': %s\n", 622 program_name, chroot_dir, pcap_strerror(errno)); 623 exit_tcpdump(1); 624 } 625 } 626 #ifdef HAVE_LIBCAP_NG 627 { 628 int ret = capng_change_id(pw->pw_uid, pw->pw_gid, CAPNG_NO_FLAG); 629 if (ret < 0) { 630 fprintf(stderr, "error : ret %d\n", ret); 631 } else { 632 fprintf(stderr, "dropped privs to %s\n", username); 633 } 634 } 635 #else 636 if (initgroups(pw->pw_name, pw->pw_gid) != 0 || 637 setgid(pw->pw_gid) != 0 || setuid(pw->pw_uid) != 0) { 638 fprintf(stderr, "%s: Couldn't change to '%.32s' uid=%lu gid=%lu: %s\n", 639 program_name, username, 640 (unsigned long)pw->pw_uid, 641 (unsigned long)pw->pw_gid, 642 pcap_strerror(errno)); 643 exit_tcpdump(1); 644 } 645 else { 646 fprintf(stderr, "dropped privs to %s\n", username); 647 } 648 #endif /* HAVE_LIBCAP_NG */ 649 } 650 else { 651 fprintf(stderr, "%s: Couldn't find user '%.32s'\n", 652 program_name, username); 653 exit_tcpdump(1); 654 } 655 #ifdef HAVE_LIBCAP_NG 656 /* We don't need CAP_SETUID, CAP_SETGID and CAP_SYS_CHROOT any more. */ 657 capng_updatev( 658 CAPNG_DROP, 659 CAPNG_EFFECTIVE | CAPNG_PERMITTED, 660 CAP_SETUID, 661 CAP_SETGID, 662 CAP_SYS_CHROOT, 663 -1); 664 capng_apply(CAPNG_SELECT_BOTH); 665 #endif /* HAVE_LIBCAP_NG */ 666 667 } 668 #endif /* _WIN32 */ 669 670 static int 671 getWflagChars(int x) 672 { 673 int c = 0; 674 675 x -= 1; 676 while (x > 0) { 677 c += 1; 678 x /= 10; 679 } 680 681 return c; 682 } 683 684 685 static void 686 MakeFilename(char *buffer, char *orig_name, int cnt, int max_chars) 687 { 688 char *filename = malloc(PATH_MAX + 1); 689 if (filename == NULL) 690 error("Makefilename: malloc"); 691 692 /* Process with strftime if Gflag is set. */ 693 if (Gflag != 0) { 694 struct tm *local_tm; 695 696 /* Convert Gflag_time to a usable format */ 697 if ((local_tm = localtime(&Gflag_time)) == NULL) { 698 error("MakeTimedFilename: localtime"); 699 } 700 701 /* There's no good way to detect an error in strftime since a return 702 * value of 0 isn't necessarily failure. 703 */ 704 strftime(filename, PATH_MAX, orig_name, local_tm); 705 } else { 706 strncpy(filename, orig_name, PATH_MAX); 707 } 708 709 if (cnt == 0 && max_chars == 0) 710 strncpy(buffer, filename, PATH_MAX + 1); 711 else 712 if (snprintf(buffer, PATH_MAX + 1, "%s%0*d", filename, max_chars, cnt) > PATH_MAX) 713 /* Report an error if the filename is too large */ 714 error("too many output files or filename is too long (> %d)", PATH_MAX); 715 free(filename); 716 } 717 718 static char * 719 get_next_file(FILE *VFile, char *ptr) 720 { 721 char *ret; 722 723 ret = fgets(ptr, PATH_MAX, VFile); 724 if (!ret) 725 return NULL; 726 727 if (ptr[strlen(ptr) - 1] == '\n') 728 ptr[strlen(ptr) - 1] = '\0'; 729 730 return ret; 731 } 732 733 #ifdef HAVE_CASPER 734 static cap_channel_t * 735 capdns_setup(void) 736 { 737 cap_channel_t *capcas, *capdnsloc; 738 const char *types[1]; 739 int families[2]; 740 741 capcas = cap_init(); 742 if (capcas == NULL) 743 error("unable to create casper process"); 744 capdnsloc = cap_service_open(capcas, "system.dns"); 745 /* Casper capability no longer needed. */ 746 cap_close(capcas); 747 if (capdnsloc == NULL) 748 error("unable to open system.dns service"); 749 /* Limit system.dns to reverse DNS lookups. */ 750 types[0] = "ADDR"; 751 if (cap_dns_type_limit(capdnsloc, types, 1) < 0) 752 error("unable to limit access to system.dns service"); 753 families[0] = AF_INET; 754 families[1] = AF_INET6; 755 if (cap_dns_family_limit(capdnsloc, families, 2) < 0) 756 error("unable to limit access to system.dns service"); 757 758 return (capdnsloc); 759 } 760 #endif /* HAVE_CASPER */ 761 762 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION 763 static int 764 tstamp_precision_from_string(const char *precision) 765 { 766 if (strncmp(precision, "nano", strlen("nano")) == 0) 767 return PCAP_TSTAMP_PRECISION_NANO; 768 769 if (strncmp(precision, "micro", strlen("micro")) == 0) 770 return PCAP_TSTAMP_PRECISION_MICRO; 771 772 return -EINVAL; 773 } 774 775 static const char * 776 tstamp_precision_to_string(int precision) 777 { 778 switch (precision) { 779 780 case PCAP_TSTAMP_PRECISION_MICRO: 781 return "micro"; 782 783 case PCAP_TSTAMP_PRECISION_NANO: 784 return "nano"; 785 786 default: 787 return "unknown"; 788 } 789 } 790 #endif 791 792 #ifdef HAVE_CAPSICUM 793 /* 794 * Ensure that, on a dump file's descriptor, we have all the rights 795 * necessary to make the standard I/O library work with an fdopen()ed 796 * FILE * from that descriptor. 797 * 798 * A long time ago, in a galaxy far far away, AT&T decided that, instead 799 * of providing separate APIs for getting and setting the FD_ flags on a 800 * descriptor, getting and setting the O_ flags on a descriptor, and 801 * locking files, they'd throw them all into a kitchen-sink fcntl() call 802 * along the lines of ioctl(), the fact that ioctl() operations are 803 * largely specific to particular character devices but fcntl() operations 804 * are either generic to all descriptors or generic to all descriptors for 805 * regular files nonwithstanding. 806 * 807 * The Capsicum people decided that fine-grained control of descriptor 808 * operations was required, so that you need to grant permission for 809 * reading, writing, seeking, and fcntl-ing. The latter, courtesy of 810 * AT&T's decision, means that "fcntl-ing" isn't a thing, but a motley 811 * collection of things, so there are *individual* fcntls for which 812 * permission needs to be granted. 813 * 814 * The FreeBSD standard I/O people implemented some optimizations that 815 * requires that the standard I/O routines be able to determine whether 816 * the descriptor for the FILE * is open append-only or not; as that 817 * descriptor could have come from an open() rather than an fopen(), 818 * that requires that it be able to do an F_GETFL fcntl() to read 819 * the O_ flags. 820 * 821 * Tcpdump uses ftell() to determine how much data has been written 822 * to a file in order to, when used with -C, determine when it's time 823 * to rotate capture files. ftell() therefore needs to do an lseek() 824 * to find out the file offset and must, thanks to the aforementioned 825 * optimization, also know whether the descriptor is open append-only 826 * or not. 827 * 828 * The net result of all the above is that we need to grant CAP_SEEK, 829 * CAP_WRITE, and CAP_FCNTL with the CAP_FCNTL_GETFL subcapability. 830 * 831 * Perhaps this is the universe's way of saying that either 832 * 833 * 1) there needs to be an fopenat() call and a pcap_dump_openat() call 834 * using it, so that Capsicum-capable tcpdump wouldn't need to do 835 * an fdopen() 836 * 837 * or 838 * 839 * 2) there needs to be a cap_fdopen() call in the FreeBSD standard 840 * I/O library that knows what rights are needed by the standard 841 * I/O library, based on the open mode, and assigns them, perhaps 842 * with an additional argument indicating, for example, whether 843 * seeking should be allowed, so that tcpdump doesn't need to know 844 * what the standard I/O library happens to require this week. 845 */ 846 static void 847 set_dumper_capsicum_rights(pcap_dumper_t *p) 848 { 849 int fd = fileno(pcap_dump_file(p)); 850 cap_rights_t rights; 851 852 cap_rights_init(&rights, CAP_SEEK, CAP_WRITE, CAP_FCNTL); 853 if (cap_rights_limit(fd, &rights) < 0 && errno != ENOSYS) { 854 error("unable to limit dump descriptor"); 855 } 856 if (cap_fcntls_limit(fd, CAP_FCNTL_GETFL) < 0 && errno != ENOSYS) { 857 error("unable to limit dump descriptor fcntls"); 858 } 859 } 860 #endif 861 862 /* 863 * Copy arg vector into a new buffer, concatenating arguments with spaces. 864 */ 865 static char * 866 copy_argv(register char **argv) 867 { 868 register char **p; 869 register u_int len = 0; 870 char *buf; 871 char *src, *dst; 872 873 p = argv; 874 if (*p == NULL) 875 return 0; 876 877 while (*p) 878 len += strlen(*p++) + 1; 879 880 buf = (char *)malloc(len); 881 if (buf == NULL) 882 error("copy_argv: malloc"); 883 884 p = argv; 885 dst = buf; 886 while ((src = *p++) != NULL) { 887 while ((*dst++ = *src++) != '\0') 888 ; 889 dst[-1] = ' '; 890 } 891 dst[-1] = '\0'; 892 893 return buf; 894 } 895 896 /* 897 * On Windows, we need to open the file in binary mode, so that 898 * we get all the bytes specified by the size we get from "fstat()". 899 * On UNIX, that's not necessary. O_BINARY is defined on Windows; 900 * we define it as 0 if it's not defined, so it does nothing. 901 */ 902 #ifndef O_BINARY 903 #define O_BINARY 0 904 #endif 905 906 static char * 907 read_infile(char *fname) 908 { 909 register int i, fd, cc; 910 register char *cp; 911 struct stat buf; 912 913 fd = open(fname, O_RDONLY|O_BINARY); 914 if (fd < 0) 915 error("can't open %s: %s", fname, pcap_strerror(errno)); 916 917 if (fstat(fd, &buf) < 0) 918 error("can't stat %s: %s", fname, pcap_strerror(errno)); 919 920 cp = malloc((u_int)buf.st_size + 1); 921 if (cp == NULL) 922 error("malloc(%d) for %s: %s", (u_int)buf.st_size + 1, 923 fname, pcap_strerror(errno)); 924 cc = read(fd, cp, (u_int)buf.st_size); 925 if (cc < 0) 926 error("read %s: %s", fname, pcap_strerror(errno)); 927 if (cc != buf.st_size) 928 error("short read %s (%d != %d)", fname, cc, (int)buf.st_size); 929 930 close(fd); 931 /* replace "# comment" with spaces */ 932 for (i = 0; i < cc; i++) { 933 if (cp[i] == '#') 934 while (i < cc && cp[i] != '\n') 935 cp[i++] = ' '; 936 } 937 cp[cc] = '\0'; 938 return (cp); 939 } 940 941 #ifdef HAVE_PCAP_FINDALLDEVS 942 static long 943 parse_interface_number(const char *device) 944 { 945 long devnum; 946 char *end; 947 948 devnum = strtol(device, &end, 10); 949 if (device != end && *end == '\0') { 950 /* 951 * It's all-numeric, but is it a valid number? 952 */ 953 if (devnum <= 0) { 954 /* 955 * No, it's not an ordinal. 956 */ 957 error("Invalid adapter index"); 958 } 959 return (devnum); 960 } else { 961 /* 962 * It's not all-numeric; return -1, so our caller 963 * knows that. 964 */ 965 return (-1); 966 } 967 } 968 969 static char * 970 find_interface_by_number(long devnum) 971 { 972 pcap_if_t *dev, *devlist; 973 long i; 974 char ebuf[PCAP_ERRBUF_SIZE]; 975 char *device; 976 977 if (pcap_findalldevs(&devlist, ebuf) < 0) 978 error("%s", ebuf); 979 /* 980 * Look for the devnum-th entry in the list of devices (1-based). 981 */ 982 for (i = 0, dev = devlist; i < devnum-1 && dev != NULL; 983 i++, dev = dev->next) 984 ; 985 if (dev == NULL) 986 error("Invalid adapter index"); 987 device = strdup(dev->name); 988 pcap_freealldevs(devlist); 989 return (device); 990 } 991 #endif 992 993 static pcap_t * 994 open_interface(const char *device, netdissect_options *ndo, char *ebuf) 995 { 996 pcap_t *pc; 997 #ifdef HAVE_PCAP_CREATE 998 int status; 999 char *cp; 1000 #endif 1001 1002 #ifdef HAVE_PCAP_CREATE 1003 pc = pcap_create(device, ebuf); 1004 if (pc == NULL) { 1005 /* 1006 * If this failed with "No such device", that means 1007 * the interface doesn't exist; return NULL, so that 1008 * the caller can see whether the device name is 1009 * actually an interface index. 1010 */ 1011 if (strstr(ebuf, "No such device") != NULL) 1012 return (NULL); 1013 error("%s", ebuf); 1014 } 1015 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE 1016 if (Jflag) 1017 show_tstamp_types_and_exit(pc, device); 1018 #endif 1019 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION 1020 status = pcap_set_tstamp_precision(pc, ndo->ndo_tstamp_precision); 1021 if (status != 0) 1022 error("%s: Can't set %ssecond time stamp precision: %s", 1023 device, 1024 tstamp_precision_to_string(ndo->ndo_tstamp_precision), 1025 pcap_statustostr(status)); 1026 #endif 1027 1028 #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE 1029 if (immediate_mode) { 1030 status = pcap_set_immediate_mode(pc, 1); 1031 if (status != 0) 1032 error("%s: Can't set immediate mode: %s", 1033 device, 1034 pcap_statustostr(status)); 1035 } 1036 #endif 1037 /* 1038 * Is this an interface that supports monitor mode? 1039 */ 1040 if (pcap_can_set_rfmon(pc) == 1) 1041 supports_monitor_mode = 1; 1042 else 1043 supports_monitor_mode = 0; 1044 status = pcap_set_snaplen(pc, ndo->ndo_snaplen); 1045 if (status != 0) 1046 error("%s: Can't set snapshot length: %s", 1047 device, pcap_statustostr(status)); 1048 status = pcap_set_promisc(pc, !pflag); 1049 if (status != 0) 1050 error("%s: Can't set promiscuous mode: %s", 1051 device, pcap_statustostr(status)); 1052 if (Iflag) { 1053 status = pcap_set_rfmon(pc, 1); 1054 if (status != 0) 1055 error("%s: Can't set monitor mode: %s", 1056 device, pcap_statustostr(status)); 1057 } 1058 status = pcap_set_timeout(pc, 1000); 1059 if (status != 0) 1060 error("%s: pcap_set_timeout failed: %s", 1061 device, pcap_statustostr(status)); 1062 if (Bflag != 0) { 1063 status = pcap_set_buffer_size(pc, Bflag); 1064 if (status != 0) 1065 error("%s: Can't set buffer size: %s", 1066 device, pcap_statustostr(status)); 1067 } 1068 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE 1069 if (jflag != -1) { 1070 status = pcap_set_tstamp_type(pc, jflag); 1071 if (status < 0) 1072 error("%s: Can't set time stamp type: %s", 1073 device, pcap_statustostr(status)); 1074 } 1075 #endif 1076 status = pcap_activate(pc); 1077 if (status < 0) { 1078 /* 1079 * pcap_activate() failed. 1080 */ 1081 cp = pcap_geterr(pc); 1082 if (status == PCAP_ERROR) 1083 error("%s", cp); 1084 else if (status == PCAP_ERROR_NO_SUCH_DEVICE) { 1085 /* 1086 * Return an error for our caller to handle. 1087 */ 1088 pcap_close(pc); 1089 snprintf(ebuf, PCAP_ERRBUF_SIZE, "%s: %s\n(%s)", 1090 device, pcap_statustostr(status), cp); 1091 return (NULL); 1092 } else if (status == PCAP_ERROR_PERM_DENIED && *cp != '\0') 1093 error("%s: %s\n(%s)", device, 1094 pcap_statustostr(status), cp); 1095 #ifdef __FreeBSD__ 1096 else if (status == PCAP_ERROR_RFMON_NOTSUP && 1097 strncmp(device, "wlan", 4) == 0) { 1098 char parent[8], newdev[8]; 1099 char sysctl[32]; 1100 size_t s = sizeof(parent); 1101 1102 snprintf(sysctl, sizeof(sysctl), 1103 "net.wlan.%d.%%parent", atoi(device + 4)); 1104 sysctlbyname(sysctl, parent, &s, NULL, 0); 1105 strlcpy(newdev, device, sizeof(newdev)); 1106 /* Suggest a new wlan device. */ 1107 newdev[strlen(newdev)-1]++; 1108 error("%s is not a monitor mode VAP\n" 1109 "To create a new monitor mode VAP use:\n" 1110 " ifconfig %s create wlandev %s wlanmode monitor\n" 1111 "and use %s as the tcpdump interface", 1112 device, newdev, parent, newdev); 1113 } 1114 #endif 1115 else 1116 error("%s: %s", device, 1117 pcap_statustostr(status)); 1118 } else if (status > 0) { 1119 /* 1120 * pcap_activate() succeeded, but it's warning us 1121 * of a problem it had. 1122 */ 1123 cp = pcap_geterr(pc); 1124 if (status == PCAP_WARNING) 1125 warning("%s", cp); 1126 else if (status == PCAP_WARNING_PROMISC_NOTSUP && 1127 *cp != '\0') 1128 warning("%s: %s\n(%s)", device, 1129 pcap_statustostr(status), cp); 1130 else 1131 warning("%s: %s", device, 1132 pcap_statustostr(status)); 1133 } 1134 #ifdef HAVE_PCAP_SETDIRECTION 1135 if (Qflag != -1) { 1136 status = pcap_setdirection(pc, Qflag); 1137 if (status != 0) 1138 error("%s: pcap_setdirection() failed: %s", 1139 device, pcap_geterr(pc)); 1140 } 1141 #endif /* HAVE_PCAP_SETDIRECTION */ 1142 #else /* HAVE_PCAP_CREATE */ 1143 *ebuf = '\0'; 1144 pc = pcap_open_live(device, ndo->ndo_snaplen, !pflag, 1000, ebuf); 1145 if (pc == NULL) { 1146 /* 1147 * If this failed with "No such device", that means 1148 * the interface doesn't exist; return NULL, so that 1149 * the caller can see whether the device name is 1150 * actually an interface index. 1151 */ 1152 if (strstr(ebuf, "No such device") != NULL) 1153 return (NULL); 1154 error("%s", ebuf); 1155 } 1156 if (*ebuf) 1157 warning("%s", ebuf); 1158 #endif /* HAVE_PCAP_CREATE */ 1159 1160 return (pc); 1161 } 1162 1163 int 1164 main(int argc, char **argv) 1165 { 1166 register int cnt, op, i; 1167 bpf_u_int32 localnet =0 , netmask = 0; 1168 int timezone_offset = 0; 1169 register char *cp, *infile, *cmdbuf, *device, *RFileName, *VFileName, *WFileName; 1170 pcap_handler callback; 1171 int dlt; 1172 const char *dlt_name; 1173 struct bpf_program fcode; 1174 #ifndef _WIN32 1175 RETSIGTYPE (*oldhandler)(int); 1176 #endif 1177 struct dump_info dumpinfo; 1178 u_char *pcap_userdata; 1179 char ebuf[PCAP_ERRBUF_SIZE]; 1180 char VFileLine[PATH_MAX + 1]; 1181 char *username = NULL; 1182 char *chroot_dir = NULL; 1183 char *ret = NULL; 1184 char *end; 1185 #ifdef HAVE_PCAP_FINDALLDEVS 1186 pcap_if_t *devlist; 1187 long devnum; 1188 #endif 1189 int status; 1190 FILE *VFile; 1191 #ifdef HAVE_CAPSICUM 1192 cap_rights_t rights; 1193 int cansandbox; 1194 #endif /* HAVE_CAPSICUM */ 1195 int Oflag = 1; /* run filter code optimizer */ 1196 int yflag_dlt = -1; 1197 const char *yflag_dlt_name = NULL; 1198 1199 netdissect_options Ndo; 1200 netdissect_options *ndo = &Ndo; 1201 1202 /* 1203 * Initialize the netdissect code. 1204 */ 1205 if (nd_init(ebuf, sizeof ebuf) == -1) 1206 error("%s", ebuf); 1207 1208 memset(ndo, 0, sizeof(*ndo)); 1209 ndo_set_function_pointers(ndo); 1210 ndo->ndo_snaplen = DEFAULT_SNAPLEN; 1211 1212 cnt = -1; 1213 device = NULL; 1214 infile = NULL; 1215 RFileName = NULL; 1216 VFileName = NULL; 1217 VFile = NULL; 1218 WFileName = NULL; 1219 dlt = -1; 1220 if ((cp = strrchr(argv[0], '/')) != NULL) 1221 ndo->program_name = program_name = cp + 1; 1222 else 1223 ndo->program_name = program_name = argv[0]; 1224 1225 #ifdef _WIN32 1226 if (pcap_wsockinit() != 0) 1227 error("Attempting to initialize Winsock failed"); 1228 #endif /* _WIN32 */ 1229 1230 /* 1231 * On platforms where the CPU doesn't support unaligned loads, 1232 * force unaligned accesses to abort with SIGBUS, rather than 1233 * being fixed up (slowly) by the OS kernel; on those platforms, 1234 * misaligned accesses are bugs, and we want tcpdump to crash so 1235 * that the bugs are reported. 1236 */ 1237 if (abort_on_misalignment(ebuf, sizeof(ebuf)) < 0) 1238 error("%s", ebuf); 1239 1240 while ( 1241 (op = getopt_long(argc, argv, SHORTOPTS, longopts, NULL)) != -1) 1242 switch (op) { 1243 1244 case 'a': 1245 /* compatibility for old -a */ 1246 break; 1247 1248 case 'A': 1249 ++ndo->ndo_Aflag; 1250 break; 1251 1252 case 'b': 1253 ++ndo->ndo_bflag; 1254 break; 1255 1256 #if defined(HAVE_PCAP_CREATE) || defined(_WIN32) 1257 case 'B': 1258 Bflag = atoi(optarg)*1024; 1259 if (Bflag <= 0) 1260 error("invalid packet buffer size %s", optarg); 1261 break; 1262 #endif /* defined(HAVE_PCAP_CREATE) || defined(_WIN32) */ 1263 1264 case 'c': 1265 cnt = atoi(optarg); 1266 if (cnt <= 0) 1267 error("invalid packet count %s", optarg); 1268 break; 1269 1270 case 'C': 1271 Cflag = atoi(optarg) * 1000000; 1272 if (Cflag <= 0) 1273 error("invalid file size %s", optarg); 1274 break; 1275 1276 case 'd': 1277 ++dflag; 1278 break; 1279 1280 case 'D': 1281 Dflag++; 1282 break; 1283 1284 case 'L': 1285 Lflag++; 1286 break; 1287 1288 case 'e': 1289 ++ndo->ndo_eflag; 1290 break; 1291 1292 case 'E': 1293 #ifndef HAVE_LIBCRYPTO 1294 warning("crypto code not compiled in"); 1295 #endif 1296 ndo->ndo_espsecret = optarg; 1297 break; 1298 1299 case 'f': 1300 ++ndo->ndo_fflag; 1301 break; 1302 1303 case 'F': 1304 infile = optarg; 1305 break; 1306 1307 case 'G': 1308 Gflag = atoi(optarg); 1309 if (Gflag < 0) 1310 error("invalid number of seconds %s", optarg); 1311 1312 /* We will create one file initially. */ 1313 Gflag_count = 0; 1314 1315 /* Grab the current time for rotation use. */ 1316 if ((Gflag_time = time(NULL)) == (time_t)-1) { 1317 error("main: can't get current time: %s", 1318 pcap_strerror(errno)); 1319 } 1320 break; 1321 1322 case 'h': 1323 print_usage(); 1324 exit_tcpdump(0); 1325 break; 1326 1327 case 'H': 1328 ++ndo->ndo_Hflag; 1329 break; 1330 1331 case 'i': 1332 device = optarg; 1333 break; 1334 1335 #ifdef HAVE_PCAP_CREATE 1336 case 'I': 1337 ++Iflag; 1338 break; 1339 #endif /* HAVE_PCAP_CREATE */ 1340 1341 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE 1342 case 'j': 1343 jflag = pcap_tstamp_type_name_to_val(optarg); 1344 if (jflag < 0) 1345 error("invalid time stamp type %s", optarg); 1346 break; 1347 1348 case 'J': 1349 Jflag++; 1350 break; 1351 #endif 1352 1353 case 'l': 1354 #ifdef _WIN32 1355 /* 1356 * _IOLBF is the same as _IOFBF in Microsoft's C 1357 * libraries; the only alternative they offer 1358 * is _IONBF. 1359 * 1360 * XXX - this should really be checking for MSVC++, 1361 * not _WIN32, if, for example, MinGW has its own 1362 * C library that is more UNIX-compatible. 1363 */ 1364 setvbuf(stdout, NULL, _IONBF, 0); 1365 #else /* _WIN32 */ 1366 #ifdef HAVE_SETLINEBUF 1367 setlinebuf(stdout); 1368 #else 1369 setvbuf(stdout, NULL, _IOLBF, 0); 1370 #endif 1371 #endif /* _WIN32 */ 1372 break; 1373 1374 case 'K': 1375 ++ndo->ndo_Kflag; 1376 break; 1377 1378 case 'm': 1379 if (nd_have_smi_support()) { 1380 if (nd_load_smi_module(optarg, ebuf, sizeof ebuf) == -1) 1381 error("%s", ebuf); 1382 } else { 1383 (void)fprintf(stderr, "%s: ignoring option `-m %s' ", 1384 program_name, optarg); 1385 (void)fprintf(stderr, "(no libsmi support)\n"); 1386 } 1387 break; 1388 1389 case 'M': 1390 /* TCP-MD5 shared secret */ 1391 #ifndef HAVE_LIBCRYPTO 1392 warning("crypto code not compiled in"); 1393 #endif 1394 ndo->ndo_sigsecret = optarg; 1395 break; 1396 1397 case 'n': 1398 ++ndo->ndo_nflag; 1399 break; 1400 1401 case 'N': 1402 ++ndo->ndo_Nflag; 1403 break; 1404 1405 case 'O': 1406 Oflag = 0; 1407 break; 1408 1409 case 'p': 1410 ++pflag; 1411 break; 1412 1413 case 'q': 1414 ++ndo->ndo_qflag; 1415 ++ndo->ndo_suppress_default_print; 1416 break; 1417 1418 #ifdef HAVE_PCAP_SETDIRECTION 1419 case 'Q': 1420 if (ascii_strcasecmp(optarg, "in") == 0) 1421 Qflag = PCAP_D_IN; 1422 else if (ascii_strcasecmp(optarg, "out") == 0) 1423 Qflag = PCAP_D_OUT; 1424 else if (ascii_strcasecmp(optarg, "inout") == 0) 1425 Qflag = PCAP_D_INOUT; 1426 else 1427 error("unknown capture direction `%s'", optarg); 1428 break; 1429 #endif /* HAVE_PCAP_SETDIRECTION */ 1430 1431 case 'r': 1432 RFileName = optarg; 1433 break; 1434 1435 case 's': 1436 ndo->ndo_snaplen = strtol(optarg, &end, 0); 1437 if (optarg == end || *end != '\0' 1438 || ndo->ndo_snaplen < 0 || ndo->ndo_snaplen > MAXIMUM_SNAPLEN) 1439 error("invalid snaplen %s", optarg); 1440 else if (ndo->ndo_snaplen == 0) 1441 ndo->ndo_snaplen = MAXIMUM_SNAPLEN; 1442 break; 1443 1444 case 'S': 1445 ++ndo->ndo_Sflag; 1446 break; 1447 1448 case 't': 1449 ++ndo->ndo_tflag; 1450 break; 1451 1452 case 'T': 1453 if (ascii_strcasecmp(optarg, "vat") == 0) 1454 ndo->ndo_packettype = PT_VAT; 1455 else if (ascii_strcasecmp(optarg, "wb") == 0) 1456 ndo->ndo_packettype = PT_WB; 1457 else if (ascii_strcasecmp(optarg, "rpc") == 0) 1458 ndo->ndo_packettype = PT_RPC; 1459 else if (ascii_strcasecmp(optarg, "rtp") == 0) 1460 ndo->ndo_packettype = PT_RTP; 1461 else if (ascii_strcasecmp(optarg, "rtcp") == 0) 1462 ndo->ndo_packettype = PT_RTCP; 1463 else if (ascii_strcasecmp(optarg, "snmp") == 0) 1464 ndo->ndo_packettype = PT_SNMP; 1465 else if (ascii_strcasecmp(optarg, "cnfp") == 0) 1466 ndo->ndo_packettype = PT_CNFP; 1467 else if (ascii_strcasecmp(optarg, "tftp") == 0) 1468 ndo->ndo_packettype = PT_TFTP; 1469 else if (ascii_strcasecmp(optarg, "aodv") == 0) 1470 ndo->ndo_packettype = PT_AODV; 1471 else if (ascii_strcasecmp(optarg, "carp") == 0) 1472 ndo->ndo_packettype = PT_CARP; 1473 else if (ascii_strcasecmp(optarg, "radius") == 0) 1474 ndo->ndo_packettype = PT_RADIUS; 1475 else if (ascii_strcasecmp(optarg, "zmtp1") == 0) 1476 ndo->ndo_packettype = PT_ZMTP1; 1477 else if (ascii_strcasecmp(optarg, "vxlan") == 0) 1478 ndo->ndo_packettype = PT_VXLAN; 1479 else if (ascii_strcasecmp(optarg, "pgm") == 0) 1480 ndo->ndo_packettype = PT_PGM; 1481 else if (ascii_strcasecmp(optarg, "pgm_zmtp1") == 0) 1482 ndo->ndo_packettype = PT_PGM_ZMTP1; 1483 else if (ascii_strcasecmp(optarg, "lmp") == 0) 1484 ndo->ndo_packettype = PT_LMP; 1485 else if (ascii_strcasecmp(optarg, "resp") == 0) 1486 ndo->ndo_packettype = PT_RESP; 1487 else 1488 error("unknown packet type `%s'", optarg); 1489 break; 1490 1491 case 'u': 1492 ++ndo->ndo_uflag; 1493 break; 1494 1495 #ifdef HAVE_PCAP_DUMP_FLUSH 1496 case 'U': 1497 ++Uflag; 1498 break; 1499 #endif 1500 1501 case 'v': 1502 ++ndo->ndo_vflag; 1503 break; 1504 1505 case 'V': 1506 VFileName = optarg; 1507 break; 1508 1509 case 'w': 1510 WFileName = optarg; 1511 break; 1512 1513 case 'W': 1514 Wflag = atoi(optarg); 1515 if (Wflag <= 0) 1516 error("invalid number of output files %s", optarg); 1517 WflagChars = getWflagChars(Wflag); 1518 break; 1519 1520 case 'x': 1521 ++ndo->ndo_xflag; 1522 ++ndo->ndo_suppress_default_print; 1523 break; 1524 1525 case 'X': 1526 ++ndo->ndo_Xflag; 1527 ++ndo->ndo_suppress_default_print; 1528 break; 1529 1530 case 'y': 1531 yflag_dlt_name = optarg; 1532 yflag_dlt = 1533 pcap_datalink_name_to_val(yflag_dlt_name); 1534 if (yflag_dlt < 0) 1535 error("invalid data link type %s", yflag_dlt_name); 1536 break; 1537 1538 #ifdef HAVE_PCAP_SET_PARSER_DEBUG 1539 case 'Y': 1540 { 1541 /* Undocumented flag */ 1542 pcap_set_parser_debug(1); 1543 } 1544 break; 1545 #endif 1546 case 'z': 1547 zflag = optarg; 1548 break; 1549 1550 case 'Z': 1551 username = optarg; 1552 break; 1553 1554 case '#': 1555 ndo->ndo_packet_number = 1; 1556 break; 1557 1558 case OPTION_VERSION: 1559 print_version(); 1560 exit_tcpdump(0); 1561 break; 1562 1563 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION 1564 case OPTION_TSTAMP_PRECISION: 1565 ndo->ndo_tstamp_precision = tstamp_precision_from_string(optarg); 1566 if (ndo->ndo_tstamp_precision < 0) 1567 error("unsupported time stamp precision"); 1568 break; 1569 #endif 1570 1571 #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE 1572 case OPTION_IMMEDIATE_MODE: 1573 immediate_mode = 1; 1574 break; 1575 #endif 1576 1577 default: 1578 print_usage(); 1579 exit_tcpdump(1); 1580 /* NOTREACHED */ 1581 } 1582 1583 #ifdef HAVE_PCAP_FINDALLDEVS 1584 if (Dflag) 1585 show_devices_and_exit(); 1586 #endif 1587 1588 switch (ndo->ndo_tflag) { 1589 1590 case 0: /* Default */ 1591 case 4: /* Default + Date*/ 1592 timezone_offset = gmt2local(0); 1593 break; 1594 1595 case 1: /* No time stamp */ 1596 case 2: /* Unix timeval style */ 1597 case 3: /* Microseconds since previous packet */ 1598 case 5: /* Microseconds since first packet */ 1599 break; 1600 1601 default: /* Not supported */ 1602 error("only -t, -tt, -ttt, -tttt and -ttttt are supported"); 1603 break; 1604 } 1605 1606 if (ndo->ndo_fflag != 0 && (VFileName != NULL || RFileName != NULL)) 1607 error("-f can not be used with -V or -r"); 1608 1609 if (VFileName != NULL && RFileName != NULL) 1610 error("-V and -r are mutually exclusive."); 1611 1612 #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE 1613 /* 1614 * If we're printing dissected packets to the standard output 1615 * rather than saving raw packets to a file, and the standard 1616 * output is a terminal, use immediate mode, as the user's 1617 * probably expecting to see packets pop up immediately. 1618 */ 1619 if (WFileName == NULL && isatty(1)) 1620 immediate_mode = 1; 1621 #endif 1622 1623 #ifdef WITH_CHROOT 1624 /* if run as root, prepare for chrooting */ 1625 if (getuid() == 0 || geteuid() == 0) { 1626 /* future extensibility for cmd-line arguments */ 1627 if (!chroot_dir) 1628 chroot_dir = WITH_CHROOT; 1629 } 1630 #endif 1631 1632 #ifdef WITH_USER 1633 /* if run as root, prepare for dropping root privileges */ 1634 if (getuid() == 0 || geteuid() == 0) { 1635 /* Run with '-Z root' to restore old behaviour */ 1636 if (!username) 1637 username = WITH_USER; 1638 } 1639 #endif 1640 1641 if (RFileName != NULL || VFileName != NULL) { 1642 /* 1643 * If RFileName is non-null, it's the pathname of a 1644 * savefile to read. If VFileName is non-null, it's 1645 * the pathname of a file containing a list of pathnames 1646 * (one per line) of savefiles to read. 1647 * 1648 * In either case, we're reading a savefile, not doing 1649 * a live capture. 1650 */ 1651 #ifndef _WIN32 1652 /* 1653 * We don't need network access, so relinquish any set-UID 1654 * or set-GID privileges we have (if any). 1655 * 1656 * We do *not* want set-UID privileges when opening a 1657 * trace file, as that might let the user read other 1658 * people's trace files (especially if we're set-UID 1659 * root). 1660 */ 1661 if (setgid(getgid()) != 0 || setuid(getuid()) != 0 ) 1662 fprintf(stderr, "Warning: setgid/setuid failed !\n"); 1663 #endif /* _WIN32 */ 1664 if (VFileName != NULL) { 1665 if (VFileName[0] == '-' && VFileName[1] == '\0') 1666 VFile = stdin; 1667 else 1668 VFile = fopen(VFileName, "r"); 1669 1670 if (VFile == NULL) 1671 error("Unable to open file: %s\n", pcap_strerror(errno)); 1672 1673 ret = get_next_file(VFile, VFileLine); 1674 if (!ret) 1675 error("Nothing in %s\n", VFileName); 1676 RFileName = VFileLine; 1677 } 1678 1679 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION 1680 pd = pcap_open_offline_with_tstamp_precision(RFileName, 1681 ndo->ndo_tstamp_precision, ebuf); 1682 #else 1683 pd = pcap_open_offline(RFileName, ebuf); 1684 #endif 1685 1686 if (pd == NULL) 1687 error("%s", ebuf); 1688 #ifdef HAVE_CAPSICUM 1689 cap_rights_init(&rights, CAP_READ); 1690 if (cap_rights_limit(fileno(pcap_file(pd)), &rights) < 0 && 1691 errno != ENOSYS) { 1692 error("unable to limit pcap descriptor"); 1693 } 1694 #endif 1695 dlt = pcap_datalink(pd); 1696 dlt_name = pcap_datalink_val_to_name(dlt); 1697 if (dlt_name == NULL) { 1698 fprintf(stderr, "reading from file %s, link-type %u\n", 1699 RFileName, dlt); 1700 } else { 1701 fprintf(stderr, 1702 "reading from file %s, link-type %s (%s)\n", 1703 RFileName, dlt_name, 1704 pcap_datalink_val_to_description(dlt)); 1705 } 1706 } else { 1707 /* 1708 * We're doing a live capture. 1709 */ 1710 if (device == NULL) { 1711 /* 1712 * No interface was specified. Pick one. 1713 */ 1714 #ifdef HAVE_PCAP_FINDALLDEVS 1715 /* 1716 * Find the list of interfaces, and pick 1717 * the first interface. 1718 */ 1719 if (pcap_findalldevs(&devlist, ebuf) >= 0 && 1720 devlist != NULL) { 1721 device = strdup(devlist->name); 1722 pcap_freealldevs(devlist); 1723 } 1724 #else /* HAVE_PCAP_FINDALLDEVS */ 1725 /* 1726 * Use whatever interface pcap_lookupdev() 1727 * chooses. 1728 */ 1729 device = pcap_lookupdev(ebuf); 1730 #endif 1731 if (device == NULL) 1732 error("%s", ebuf); 1733 } 1734 1735 /* 1736 * Try to open the interface with the specified name. 1737 */ 1738 pd = open_interface(device, ndo, ebuf); 1739 if (pd == NULL) { 1740 /* 1741 * That failed. If we can get a list of 1742 * interfaces, and the interface name 1743 * is purely numeric, try to use it as 1744 * a 1-based index in the list of 1745 * interfaces. 1746 */ 1747 #ifdef HAVE_PCAP_FINDALLDEVS 1748 devnum = parse_interface_number(device); 1749 if (devnum == -1) { 1750 /* 1751 * It's not a number; just report 1752 * the open error and fail. 1753 */ 1754 error("%s", ebuf); 1755 } 1756 1757 /* 1758 * OK, it's a number; try to find the 1759 * interface with that index, and try 1760 * to open it. 1761 * 1762 * find_interface_by_number() exits if it 1763 * couldn't be found. 1764 */ 1765 device = find_interface_by_number(devnum); 1766 pd = open_interface(device, ndo, ebuf); 1767 if (pd == NULL) 1768 error("%s", ebuf); 1769 #else /* HAVE_PCAP_FINDALLDEVS */ 1770 /* 1771 * We can't get a list of interfaces; just 1772 * fail. 1773 */ 1774 error("%s", ebuf); 1775 #endif /* HAVE_PCAP_FINDALLDEVS */ 1776 } 1777 1778 /* 1779 * Let user own process after socket has been opened. 1780 */ 1781 #ifndef _WIN32 1782 if (setgid(getgid()) != 0 || setuid(getuid()) != 0) 1783 fprintf(stderr, "Warning: setgid/setuid failed !\n"); 1784 #endif /* _WIN32 */ 1785 #if !defined(HAVE_PCAP_CREATE) && defined(_WIN32) 1786 if(Bflag != 0) 1787 if(pcap_setbuff(pd, Bflag)==-1){ 1788 error("%s", pcap_geterr(pd)); 1789 } 1790 #endif /* !defined(HAVE_PCAP_CREATE) && defined(_WIN32) */ 1791 if (Lflag) 1792 show_dlts_and_exit(pd, device); 1793 if (yflag_dlt >= 0) { 1794 #ifdef HAVE_PCAP_SET_DATALINK 1795 if (pcap_set_datalink(pd, yflag_dlt) < 0) 1796 error("%s", pcap_geterr(pd)); 1797 #else 1798 /* 1799 * We don't actually support changing the 1800 * data link type, so we only let them 1801 * set it to what it already is. 1802 */ 1803 if (yflag_dlt != pcap_datalink(pd)) { 1804 error("%s is not one of the DLTs supported by this device\n", 1805 yflag_dlt_name); 1806 } 1807 #endif 1808 (void)fprintf(stderr, "%s: data link type %s\n", 1809 program_name, yflag_dlt_name); 1810 (void)fflush(stderr); 1811 } 1812 i = pcap_snapshot(pd); 1813 if (ndo->ndo_snaplen < i) { 1814 warning("snaplen raised from %d to %d", ndo->ndo_snaplen, i); 1815 ndo->ndo_snaplen = i; 1816 } 1817 if(ndo->ndo_fflag != 0) { 1818 if (pcap_lookupnet(device, &localnet, &netmask, ebuf) < 0) { 1819 warning("foreign (-f) flag used but: %s", ebuf); 1820 } 1821 } 1822 1823 } 1824 if (infile) 1825 cmdbuf = read_infile(infile); 1826 else 1827 cmdbuf = copy_argv(&argv[optind]); 1828 1829 #ifdef HAVE_PCAP_SET_OPTIMIZER_DEBUG 1830 pcap_set_optimizer_debug(dflag); 1831 #endif 1832 if (pcap_compile(pd, &fcode, cmdbuf, Oflag, netmask) < 0) 1833 error("%s", pcap_geterr(pd)); 1834 if (dflag) { 1835 bpf_dump(&fcode, dflag); 1836 pcap_close(pd); 1837 free(cmdbuf); 1838 pcap_freecode(&fcode); 1839 exit_tcpdump(0); 1840 } 1841 1842 #ifdef HAVE_CASPER 1843 if (!ndo->ndo_nflag) 1844 capdns = capdns_setup(); 1845 #endif /* HAVE_CASPER */ 1846 1847 init_print(ndo, localnet, netmask, timezone_offset); 1848 1849 #ifndef _WIN32 1850 (void)setsignal(SIGPIPE, cleanup); 1851 (void)setsignal(SIGTERM, cleanup); 1852 (void)setsignal(SIGINT, cleanup); 1853 #endif /* _WIN32 */ 1854 #if defined(HAVE_FORK) || defined(HAVE_VFORK) 1855 (void)setsignal(SIGCHLD, child_cleanup); 1856 #endif 1857 /* Cooperate with nohup(1) */ 1858 #ifndef _WIN32 1859 if ((oldhandler = setsignal(SIGHUP, cleanup)) != SIG_DFL) 1860 (void)setsignal(SIGHUP, oldhandler); 1861 #endif /* _WIN32 */ 1862 1863 #ifndef _WIN32 1864 /* 1865 * If a user name was specified with "-Z", attempt to switch to 1866 * that user's UID. This would probably be used with sudo, 1867 * to allow tcpdump to be run in a special restricted 1868 * account (if you just want to allow users to open capture 1869 * devices, and can't just give users that permission, 1870 * you'd make tcpdump set-UID or set-GID). 1871 * 1872 * Tcpdump doesn't necessarily write only to one savefile; 1873 * the general only way to allow a -Z instance to write to 1874 * savefiles as the user under whose UID it's run, rather 1875 * than as the user specified with -Z, would thus be to switch 1876 * to the original user ID before opening a capture file and 1877 * then switch back to the -Z user ID after opening the savefile. 1878 * Switching to the -Z user ID only after opening the first 1879 * savefile doesn't handle the general case. 1880 */ 1881 1882 if (getuid() == 0 || geteuid() == 0) { 1883 #ifdef HAVE_LIBCAP_NG 1884 /* Initialize capng */ 1885 capng_clear(CAPNG_SELECT_BOTH); 1886 if (username) { 1887 capng_updatev( 1888 CAPNG_ADD, 1889 CAPNG_PERMITTED | CAPNG_EFFECTIVE, 1890 CAP_SETUID, 1891 CAP_SETGID, 1892 -1); 1893 } 1894 if (chroot_dir) { 1895 capng_update( 1896 CAPNG_ADD, 1897 CAPNG_PERMITTED | CAPNG_EFFECTIVE, 1898 CAP_SYS_CHROOT 1899 ); 1900 } 1901 1902 if (WFileName) { 1903 capng_update( 1904 CAPNG_ADD, 1905 CAPNG_PERMITTED | CAPNG_EFFECTIVE, 1906 CAP_DAC_OVERRIDE 1907 ); 1908 } 1909 capng_apply(CAPNG_SELECT_BOTH); 1910 #endif /* HAVE_LIBCAP_NG */ 1911 if (username || chroot_dir) 1912 droproot(username, chroot_dir); 1913 1914 } 1915 #endif /* _WIN32 */ 1916 1917 if (pcap_setfilter(pd, &fcode) < 0) 1918 error("%s", pcap_geterr(pd)); 1919 #ifdef HAVE_CAPSICUM 1920 if (RFileName == NULL && VFileName == NULL) { 1921 static const unsigned long cmds[] = { BIOCGSTATS, BIOCROTZBUF }; 1922 1923 /* 1924 * The various libpcap devices use a combination of 1925 * read (bpf), ioctl (bpf, netmap), poll (netmap). 1926 * Grant the relevant access rights, sorted by name. 1927 */ 1928 cap_rights_init(&rights, CAP_EVENT, CAP_IOCTL, CAP_READ); 1929 if (cap_rights_limit(pcap_fileno(pd), &rights) < 0 && 1930 errno != ENOSYS) { 1931 error("unable to limit pcap descriptor"); 1932 } 1933 if (cap_ioctls_limit(pcap_fileno(pd), cmds, 1934 sizeof(cmds) / sizeof(cmds[0])) < 0 && errno != ENOSYS) { 1935 error("unable to limit ioctls on pcap descriptor"); 1936 } 1937 } 1938 #endif 1939 if (WFileName) { 1940 pcap_dumper_t *p; 1941 /* Do not exceed the default PATH_MAX for files. */ 1942 dumpinfo.CurrentFileName = (char *)malloc(PATH_MAX + 1); 1943 1944 if (dumpinfo.CurrentFileName == NULL) 1945 error("malloc of dumpinfo.CurrentFileName"); 1946 1947 /* We do not need numbering for dumpfiles if Cflag isn't set. */ 1948 if (Cflag != 0) 1949 MakeFilename(dumpinfo.CurrentFileName, WFileName, 0, WflagChars); 1950 else 1951 MakeFilename(dumpinfo.CurrentFileName, WFileName, 0, 0); 1952 1953 p = pcap_dump_open(pd, dumpinfo.CurrentFileName); 1954 #ifdef HAVE_LIBCAP_NG 1955 /* Give up CAP_DAC_OVERRIDE capability. 1956 * Only allow it to be restored if the -C or -G flag have been 1957 * set since we may need to create more files later on. 1958 */ 1959 capng_update( 1960 CAPNG_DROP, 1961 (Cflag || Gflag ? 0 : CAPNG_PERMITTED) 1962 | CAPNG_EFFECTIVE, 1963 CAP_DAC_OVERRIDE 1964 ); 1965 capng_apply(CAPNG_SELECT_BOTH); 1966 #endif /* HAVE_LIBCAP_NG */ 1967 if (p == NULL) 1968 error("%s", pcap_geterr(pd)); 1969 #ifdef HAVE_CAPSICUM 1970 set_dumper_capsicum_rights(p); 1971 #endif 1972 if (Cflag != 0 || Gflag != 0) { 1973 #ifdef HAVE_CAPSICUM 1974 dumpinfo.WFileName = strdup(basename(WFileName)); 1975 if (dumpinfo.WFileName == NULL) { 1976 error("Unable to allocate memory for file %s", 1977 WFileName); 1978 } 1979 dumpinfo.dirfd = open(dirname(WFileName), 1980 O_DIRECTORY | O_RDONLY); 1981 if (dumpinfo.dirfd < 0) { 1982 error("unable to open directory %s", 1983 dirname(WFileName)); 1984 } 1985 cap_rights_init(&rights, CAP_CREATE, CAP_FCNTL, 1986 CAP_FTRUNCATE, CAP_LOOKUP, CAP_SEEK, CAP_WRITE); 1987 if (cap_rights_limit(dumpinfo.dirfd, &rights) < 0 && 1988 errno != ENOSYS) { 1989 error("unable to limit directory rights"); 1990 } 1991 if (cap_fcntls_limit(dumpinfo.dirfd, CAP_FCNTL_GETFL) < 0 && 1992 errno != ENOSYS) { 1993 error("unable to limit dump descriptor fcntls"); 1994 } 1995 #else /* !HAVE_CAPSICUM */ 1996 dumpinfo.WFileName = WFileName; 1997 #endif 1998 callback = dump_packet_and_trunc; 1999 dumpinfo.pd = pd; 2000 dumpinfo.p = p; 2001 pcap_userdata = (u_char *)&dumpinfo; 2002 } else { 2003 callback = dump_packet; 2004 pcap_userdata = (u_char *)p; 2005 } 2006 #ifdef HAVE_PCAP_DUMP_FLUSH 2007 if (Uflag) 2008 pcap_dump_flush(p); 2009 #endif 2010 } else { 2011 dlt = pcap_datalink(pd); 2012 ndo->ndo_if_printer = get_if_printer(ndo, dlt); 2013 callback = print_packet; 2014 pcap_userdata = (u_char *)ndo; 2015 } 2016 2017 #ifdef SIGNAL_REQ_INFO 2018 /* 2019 * We can't get statistics when reading from a file rather 2020 * than capturing from a device. 2021 */ 2022 if (RFileName == NULL) 2023 (void)setsignal(SIGNAL_REQ_INFO, requestinfo); 2024 #endif 2025 2026 if (ndo->ndo_vflag > 0 && WFileName) { 2027 /* 2028 * When capturing to a file, "-v" means tcpdump should, 2029 * every 10 seconds, "v"erbosely report the number of 2030 * packets captured. 2031 */ 2032 #ifdef USE_WIN32_MM_TIMER 2033 /* call verbose_stats_dump() each 1000 +/-100msec */ 2034 timer_id = timeSetEvent(1000, 100, verbose_stats_dump, 0, TIME_PERIODIC); 2035 setvbuf(stderr, NULL, _IONBF, 0); 2036 #elif defined(HAVE_ALARM) 2037 (void)setsignal(SIGALRM, verbose_stats_dump); 2038 alarm(1); 2039 #endif 2040 } 2041 2042 if (RFileName == NULL) { 2043 /* 2044 * Live capture (if -V was specified, we set RFileName 2045 * to a file from the -V file). Print a message to 2046 * the standard error on UN*X. 2047 */ 2048 if (!ndo->ndo_vflag && !WFileName) { 2049 (void)fprintf(stderr, 2050 "%s: verbose output suppressed, use -v or -vv for full protocol decode\n", 2051 program_name); 2052 } else 2053 (void)fprintf(stderr, "%s: ", program_name); 2054 dlt = pcap_datalink(pd); 2055 dlt_name = pcap_datalink_val_to_name(dlt); 2056 if (dlt_name == NULL) { 2057 (void)fprintf(stderr, "listening on %s, link-type %u, capture size %u bytes\n", 2058 device, dlt, ndo->ndo_snaplen); 2059 } else { 2060 (void)fprintf(stderr, "listening on %s, link-type %s (%s), capture size %u bytes\n", 2061 device, dlt_name, 2062 pcap_datalink_val_to_description(dlt), ndo->ndo_snaplen); 2063 } 2064 (void)fflush(stderr); 2065 } 2066 2067 #ifdef HAVE_CAPSICUM 2068 cansandbox = (VFileName == NULL && zflag == NULL); 2069 #ifdef HAVE_CASPER 2070 cansandbox = (cansandbox && (ndo->ndo_nflag || capdns != NULL)); 2071 #else 2072 cansandbox = (cansandbox && ndo->ndo_nflag); 2073 #endif /* HAVE_CASPER */ 2074 if (cansandbox && cap_enter() < 0 && errno != ENOSYS) 2075 error("unable to enter the capability mode"); 2076 #endif /* HAVE_CAPSICUM */ 2077 2078 do { 2079 status = pcap_loop(pd, cnt, callback, pcap_userdata); 2080 if (WFileName == NULL) { 2081 /* 2082 * We're printing packets. Flush the printed output, 2083 * so it doesn't get intermingled with error output. 2084 */ 2085 if (status == -2) { 2086 /* 2087 * We got interrupted, so perhaps we didn't 2088 * manage to finish a line we were printing. 2089 * Print an extra newline, just in case. 2090 */ 2091 putchar('\n'); 2092 } 2093 (void)fflush(stdout); 2094 } 2095 if (status == -2) { 2096 /* 2097 * We got interrupted. If we are reading multiple 2098 * files (via -V) set these so that we stop. 2099 */ 2100 VFileName = NULL; 2101 ret = NULL; 2102 } 2103 if (status == -1) { 2104 /* 2105 * Error. Report it. 2106 */ 2107 (void)fprintf(stderr, "%s: pcap_loop: %s\n", 2108 program_name, pcap_geterr(pd)); 2109 } 2110 if (RFileName == NULL) { 2111 /* 2112 * We're doing a live capture. Report the capture 2113 * statistics. 2114 */ 2115 info(1); 2116 } 2117 pcap_close(pd); 2118 if (VFileName != NULL) { 2119 ret = get_next_file(VFile, VFileLine); 2120 if (ret) { 2121 int new_dlt; 2122 2123 RFileName = VFileLine; 2124 pd = pcap_open_offline(RFileName, ebuf); 2125 if (pd == NULL) 2126 error("%s", ebuf); 2127 #ifdef HAVE_CAPSICUM 2128 cap_rights_init(&rights, CAP_READ); 2129 if (cap_rights_limit(fileno(pcap_file(pd)), 2130 &rights) < 0 && errno != ENOSYS) { 2131 error("unable to limit pcap descriptor"); 2132 } 2133 #endif 2134 new_dlt = pcap_datalink(pd); 2135 if (new_dlt != dlt) { 2136 /* 2137 * The new file has a different 2138 * link-layer header type from the 2139 * previous one. 2140 */ 2141 if (WFileName != NULL) { 2142 /* 2143 * We're writing raw packets 2144 * that match the filter to 2145 * a pcap file. pcap files 2146 * don't support multiple 2147 * different link-layer 2148 * header types, so we fail 2149 * here. 2150 */ 2151 error("%s: new dlt does not match original", RFileName); 2152 } 2153 2154 /* 2155 * We're printing the decoded packets; 2156 * switch to the new DLT. 2157 * 2158 * To do that, we need to change 2159 * the printer, change the DLT name, 2160 * and recompile the filter with 2161 * the new DLT. 2162 */ 2163 dlt = new_dlt; 2164 ndo->ndo_if_printer = get_if_printer(ndo, dlt); 2165 if (pcap_compile(pd, &fcode, cmdbuf, Oflag, netmask) < 0) 2166 error("%s", pcap_geterr(pd)); 2167 } 2168 2169 /* 2170 * Set the filter on the new file. 2171 */ 2172 if (pcap_setfilter(pd, &fcode) < 0) 2173 error("%s", pcap_geterr(pd)); 2174 2175 /* 2176 * Report the new file. 2177 */ 2178 dlt_name = pcap_datalink_val_to_name(dlt); 2179 if (dlt_name == NULL) { 2180 fprintf(stderr, "reading from file %s, link-type %u\n", 2181 RFileName, dlt); 2182 } else { 2183 fprintf(stderr, 2184 "reading from file %s, link-type %s (%s)\n", 2185 RFileName, dlt_name, 2186 pcap_datalink_val_to_description(dlt)); 2187 } 2188 } 2189 } 2190 } 2191 while (ret != NULL); 2192 2193 free(cmdbuf); 2194 pcap_freecode(&fcode); 2195 exit_tcpdump(status == -1 ? 1 : 0); 2196 } 2197 2198 /* make a clean exit on interrupts */ 2199 static RETSIGTYPE 2200 cleanup(int signo _U_) 2201 { 2202 #ifdef USE_WIN32_MM_TIMER 2203 if (timer_id) 2204 timeKillEvent(timer_id); 2205 timer_id = 0; 2206 #elif defined(HAVE_ALARM) 2207 alarm(0); 2208 #endif 2209 2210 #ifdef HAVE_PCAP_BREAKLOOP 2211 /* 2212 * We have "pcap_breakloop()"; use it, so that we do as little 2213 * as possible in the signal handler (it's probably not safe 2214 * to do anything with standard I/O streams in a signal handler - 2215 * the ANSI C standard doesn't say it is). 2216 */ 2217 pcap_breakloop(pd); 2218 #else 2219 /* 2220 * We don't have "pcap_breakloop()"; this isn't safe, but 2221 * it's the best we can do. Print the summary if we're 2222 * not reading from a savefile - i.e., if we're doing a 2223 * live capture - and exit. 2224 */ 2225 if (pd != NULL && pcap_file(pd) == NULL) { 2226 /* 2227 * We got interrupted, so perhaps we didn't 2228 * manage to finish a line we were printing. 2229 * Print an extra newline, just in case. 2230 */ 2231 putchar('\n'); 2232 (void)fflush(stdout); 2233 info(1); 2234 } 2235 exit_tcpdump(0); 2236 #endif 2237 } 2238 2239 /* 2240 On windows, we do not use a fork, so we do not care less about 2241 waiting a child processes to die 2242 */ 2243 #if defined(HAVE_FORK) || defined(HAVE_VFORK) 2244 static RETSIGTYPE 2245 child_cleanup(int signo _U_) 2246 { 2247 wait(NULL); 2248 } 2249 #endif /* HAVE_FORK && HAVE_VFORK */ 2250 2251 static void 2252 info(register int verbose) 2253 { 2254 struct pcap_stat stats; 2255 2256 /* 2257 * Older versions of libpcap didn't set ps_ifdrop on some 2258 * platforms; initialize it to 0 to handle that. 2259 */ 2260 stats.ps_ifdrop = 0; 2261 if (pcap_stats(pd, &stats) < 0) { 2262 (void)fprintf(stderr, "pcap_stats: %s\n", pcap_geterr(pd)); 2263 infoprint = 0; 2264 return; 2265 } 2266 2267 if (!verbose) 2268 fprintf(stderr, "%s: ", program_name); 2269 2270 (void)fprintf(stderr, "%u packet%s captured", packets_captured, 2271 PLURAL_SUFFIX(packets_captured)); 2272 if (!verbose) 2273 fputs(", ", stderr); 2274 else 2275 putc('\n', stderr); 2276 (void)fprintf(stderr, "%u packet%s received by filter", stats.ps_recv, 2277 PLURAL_SUFFIX(stats.ps_recv)); 2278 if (!verbose) 2279 fputs(", ", stderr); 2280 else 2281 putc('\n', stderr); 2282 (void)fprintf(stderr, "%u packet%s dropped by kernel", stats.ps_drop, 2283 PLURAL_SUFFIX(stats.ps_drop)); 2284 if (stats.ps_ifdrop != 0) { 2285 if (!verbose) 2286 fputs(", ", stderr); 2287 else 2288 putc('\n', stderr); 2289 (void)fprintf(stderr, "%u packet%s dropped by interface\n", 2290 stats.ps_ifdrop, PLURAL_SUFFIX(stats.ps_ifdrop)); 2291 } else 2292 putc('\n', stderr); 2293 infoprint = 0; 2294 } 2295 2296 #if defined(HAVE_FORK) || defined(HAVE_VFORK) 2297 #ifdef HAVE_FORK 2298 #define fork_subprocess() fork() 2299 #else 2300 #define fork_subprocess() vfork() 2301 #endif 2302 static void 2303 compress_savefile(const char *filename) 2304 { 2305 pid_t child; 2306 2307 child = fork_subprocess(); 2308 if (child == -1) { 2309 fprintf(stderr, 2310 "compress_savefile: fork failed: %s\n", 2311 pcap_strerror(errno)); 2312 return; 2313 } 2314 if (child != 0) { 2315 /* Parent process. */ 2316 return; 2317 } 2318 2319 /* 2320 * Child process. 2321 * Set to lowest priority so that this doesn't disturb the capture. 2322 */ 2323 #ifdef NZERO 2324 setpriority(PRIO_PROCESS, 0, NZERO - 1); 2325 #else 2326 setpriority(PRIO_PROCESS, 0, 19); 2327 #endif 2328 if (execlp(zflag, zflag, filename, (char *)NULL) == -1) 2329 fprintf(stderr, 2330 "compress_savefile: execlp(%s, %s) failed: %s\n", 2331 zflag, 2332 filename, 2333 pcap_strerror(errno)); 2334 #ifdef HAVE_FORK 2335 exit(1); 2336 #else 2337 _exit(1); 2338 #endif 2339 } 2340 #else /* HAVE_FORK && HAVE_VFORK */ 2341 static void 2342 compress_savefile(const char *filename) 2343 { 2344 fprintf(stderr, 2345 "compress_savefile failed. Functionality not implemented under your system\n"); 2346 } 2347 #endif /* HAVE_FORK && HAVE_VFORK */ 2348 2349 static void 2350 dump_packet_and_trunc(u_char *user, const struct pcap_pkthdr *h, const u_char *sp) 2351 { 2352 struct dump_info *dump_info; 2353 2354 ++packets_captured; 2355 2356 ++infodelay; 2357 2358 dump_info = (struct dump_info *)user; 2359 2360 /* 2361 * XXX - this won't force the file to rotate on the specified time 2362 * boundary, but it will rotate on the first packet received after the 2363 * specified Gflag number of seconds. Note: if a Gflag time boundary 2364 * and a Cflag size boundary coincide, the time rotation will occur 2365 * first thereby cancelling the Cflag boundary (since the file should 2366 * be 0). 2367 */ 2368 if (Gflag != 0) { 2369 /* Check if it is time to rotate */ 2370 time_t t; 2371 2372 /* Get the current time */ 2373 if ((t = time(NULL)) == (time_t)-1) { 2374 error("dump_and_trunc_packet: can't get current_time: %s", 2375 pcap_strerror(errno)); 2376 } 2377 2378 2379 /* If the time is greater than the specified window, rotate */ 2380 if (t - Gflag_time >= Gflag) { 2381 #ifdef HAVE_CAPSICUM 2382 FILE *fp; 2383 int fd; 2384 #endif 2385 2386 /* Update the Gflag_time */ 2387 Gflag_time = t; 2388 /* Update Gflag_count */ 2389 Gflag_count++; 2390 /* 2391 * Close the current file and open a new one. 2392 */ 2393 pcap_dump_close(dump_info->p); 2394 2395 /* 2396 * Compress the file we just closed, if the user asked for it 2397 */ 2398 if (zflag != NULL) 2399 compress_savefile(dump_info->CurrentFileName); 2400 2401 /* 2402 * Check to see if we've exceeded the Wflag (when 2403 * not using Cflag). 2404 */ 2405 if (Cflag == 0 && Wflag > 0 && Gflag_count >= Wflag) { 2406 (void)fprintf(stderr, "Maximum file limit reached: %d\n", 2407 Wflag); 2408 info(1); 2409 exit_tcpdump(0); 2410 /* NOTREACHED */ 2411 } 2412 if (dump_info->CurrentFileName != NULL) 2413 free(dump_info->CurrentFileName); 2414 /* Allocate space for max filename + \0. */ 2415 dump_info->CurrentFileName = (char *)malloc(PATH_MAX + 1); 2416 if (dump_info->CurrentFileName == NULL) 2417 error("dump_packet_and_trunc: malloc"); 2418 /* 2419 * Gflag was set otherwise we wouldn't be here. Reset the count 2420 * so multiple files would end with 1,2,3 in the filename. 2421 * The counting is handled with the -C flow after this. 2422 */ 2423 Cflag_count = 0; 2424 2425 /* 2426 * This is always the first file in the Cflag 2427 * rotation: e.g. 0 2428 * We also don't need numbering if Cflag is not set. 2429 */ 2430 if (Cflag != 0) 2431 MakeFilename(dump_info->CurrentFileName, dump_info->WFileName, 0, 2432 WflagChars); 2433 else 2434 MakeFilename(dump_info->CurrentFileName, dump_info->WFileName, 0, 0); 2435 2436 #ifdef HAVE_LIBCAP_NG 2437 capng_update(CAPNG_ADD, CAPNG_EFFECTIVE, CAP_DAC_OVERRIDE); 2438 capng_apply(CAPNG_SELECT_BOTH); 2439 #endif /* HAVE_LIBCAP_NG */ 2440 #ifdef HAVE_CAPSICUM 2441 fd = openat(dump_info->dirfd, 2442 dump_info->CurrentFileName, 2443 O_CREAT | O_WRONLY | O_TRUNC, 0644); 2444 if (fd < 0) { 2445 error("unable to open file %s", 2446 dump_info->CurrentFileName); 2447 } 2448 fp = fdopen(fd, "w"); 2449 if (fp == NULL) { 2450 error("unable to fdopen file %s", 2451 dump_info->CurrentFileName); 2452 } 2453 dump_info->p = pcap_dump_fopen(dump_info->pd, fp); 2454 #else /* !HAVE_CAPSICUM */ 2455 dump_info->p = pcap_dump_open(dump_info->pd, dump_info->CurrentFileName); 2456 #endif 2457 #ifdef HAVE_LIBCAP_NG 2458 capng_update(CAPNG_DROP, CAPNG_EFFECTIVE, CAP_DAC_OVERRIDE); 2459 capng_apply(CAPNG_SELECT_BOTH); 2460 #endif /* HAVE_LIBCAP_NG */ 2461 if (dump_info->p == NULL) 2462 error("%s", pcap_geterr(pd)); 2463 #ifdef HAVE_CAPSICUM 2464 set_dumper_capsicum_rights(dump_info->p); 2465 #endif 2466 } 2467 } 2468 2469 /* 2470 * XXX - this won't prevent capture files from getting 2471 * larger than Cflag - the last packet written to the 2472 * file could put it over Cflag. 2473 */ 2474 if (Cflag != 0) { 2475 long size = pcap_dump_ftell(dump_info->p); 2476 2477 if (size == -1) 2478 error("ftell fails on output file"); 2479 if (size > Cflag) { 2480 #ifdef HAVE_CAPSICUM 2481 FILE *fp; 2482 int fd; 2483 #endif 2484 2485 /* 2486 * Close the current file and open a new one. 2487 */ 2488 pcap_dump_close(dump_info->p); 2489 2490 /* 2491 * Compress the file we just closed, if the user 2492 * asked for it. 2493 */ 2494 if (zflag != NULL) 2495 compress_savefile(dump_info->CurrentFileName); 2496 2497 Cflag_count++; 2498 if (Wflag > 0) { 2499 if (Cflag_count >= Wflag) 2500 Cflag_count = 0; 2501 } 2502 if (dump_info->CurrentFileName != NULL) 2503 free(dump_info->CurrentFileName); 2504 dump_info->CurrentFileName = (char *)malloc(PATH_MAX + 1); 2505 if (dump_info->CurrentFileName == NULL) 2506 error("dump_packet_and_trunc: malloc"); 2507 MakeFilename(dump_info->CurrentFileName, dump_info->WFileName, Cflag_count, WflagChars); 2508 #ifdef HAVE_LIBCAP_NG 2509 capng_update(CAPNG_ADD, CAPNG_EFFECTIVE, CAP_DAC_OVERRIDE); 2510 capng_apply(CAPNG_SELECT_BOTH); 2511 #endif /* HAVE_LIBCAP_NG */ 2512 #ifdef HAVE_CAPSICUM 2513 fd = openat(dump_info->dirfd, dump_info->CurrentFileName, 2514 O_CREAT | O_WRONLY | O_TRUNC, 0644); 2515 if (fd < 0) { 2516 error("unable to open file %s", 2517 dump_info->CurrentFileName); 2518 } 2519 fp = fdopen(fd, "w"); 2520 if (fp == NULL) { 2521 error("unable to fdopen file %s", 2522 dump_info->CurrentFileName); 2523 } 2524 dump_info->p = pcap_dump_fopen(dump_info->pd, fp); 2525 #else /* !HAVE_CAPSICUM */ 2526 dump_info->p = pcap_dump_open(dump_info->pd, dump_info->CurrentFileName); 2527 #endif 2528 #ifdef HAVE_LIBCAP_NG 2529 capng_update(CAPNG_DROP, CAPNG_EFFECTIVE, CAP_DAC_OVERRIDE); 2530 capng_apply(CAPNG_SELECT_BOTH); 2531 #endif /* HAVE_LIBCAP_NG */ 2532 if (dump_info->p == NULL) 2533 error("%s", pcap_geterr(pd)); 2534 #ifdef HAVE_CAPSICUM 2535 set_dumper_capsicum_rights(dump_info->p); 2536 #endif 2537 } 2538 } 2539 2540 pcap_dump((u_char *)dump_info->p, h, sp); 2541 #ifdef HAVE_PCAP_DUMP_FLUSH 2542 if (Uflag) 2543 pcap_dump_flush(dump_info->p); 2544 #endif 2545 2546 --infodelay; 2547 if (infoprint) 2548 info(0); 2549 } 2550 2551 static void 2552 dump_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *sp) 2553 { 2554 ++packets_captured; 2555 2556 ++infodelay; 2557 2558 pcap_dump(user, h, sp); 2559 #ifdef HAVE_PCAP_DUMP_FLUSH 2560 if (Uflag) 2561 pcap_dump_flush((pcap_dumper_t *)user); 2562 #endif 2563 2564 --infodelay; 2565 if (infoprint) 2566 info(0); 2567 } 2568 2569 static void 2570 print_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *sp) 2571 { 2572 ++packets_captured; 2573 2574 ++infodelay; 2575 2576 pretty_print_packet((netdissect_options *)user, h, sp, packets_captured); 2577 2578 --infodelay; 2579 if (infoprint) 2580 info(0); 2581 } 2582 2583 #ifdef _WIN32 2584 /* 2585 * XXX - there should really be libpcap calls to get the version 2586 * number as a string (the string would be generated from #defines 2587 * at run time, so that it's not generated from string constants 2588 * in the library, as, on many UNIX systems, those constants would 2589 * be statically linked into the application executable image, and 2590 * would thus reflect the version of libpcap on the system on 2591 * which the application was *linked*, not the system on which it's 2592 * *running*. 2593 * 2594 * That routine should be documented, unlike the "version[]" 2595 * string, so that UNIX vendors providing their own libpcaps 2596 * don't omit it (as a couple of vendors have...). 2597 * 2598 * Packet.dll should perhaps also export a routine to return the 2599 * version number of the Packet.dll code, to supply the 2600 * "Wpcap_version" information on Windows. 2601 */ 2602 char WDversion[]="current-git.tcpdump.org"; 2603 #if !defined(HAVE_GENERATED_VERSION) 2604 char version[]="current-git.tcpdump.org"; 2605 #endif 2606 char pcap_version[]="current-git.tcpdump.org"; 2607 char Wpcap_version[]="3.1"; 2608 #endif 2609 2610 #ifdef SIGNAL_REQ_INFO 2611 RETSIGTYPE requestinfo(int signo _U_) 2612 { 2613 if (infodelay) 2614 ++infoprint; 2615 else 2616 info(0); 2617 } 2618 #endif 2619 2620 /* 2621 * Called once each second in verbose mode while dumping to file 2622 */ 2623 #ifdef USE_WIN32_MM_TIMER 2624 void CALLBACK verbose_stats_dump (UINT timer_id _U_, UINT msg _U_, DWORD_PTR arg _U_, 2625 DWORD_PTR dw1 _U_, DWORD_PTR dw2 _U_) 2626 { 2627 if (infodelay == 0) 2628 fprintf(stderr, "Got %u\r", packets_captured); 2629 } 2630 #elif defined(HAVE_ALARM) 2631 static void verbose_stats_dump(int sig _U_) 2632 { 2633 if (infodelay == 0) 2634 fprintf(stderr, "Got %u\r", packets_captured); 2635 alarm(1); 2636 } 2637 #endif 2638 2639 USES_APPLE_DEPRECATED_API 2640 static void 2641 print_version(void) 2642 { 2643 extern char version[]; 2644 #ifndef HAVE_PCAP_LIB_VERSION 2645 #if defined(_WIN32) || defined(HAVE_PCAP_VERSION) 2646 extern char pcap_version[]; 2647 #else /* defined(_WIN32) || defined(HAVE_PCAP_VERSION) */ 2648 static char pcap_version[] = "unknown"; 2649 #endif /* defined(_WIN32) || defined(HAVE_PCAP_VERSION) */ 2650 #endif /* HAVE_PCAP_LIB_VERSION */ 2651 const char *smi_version_string; 2652 2653 #ifdef HAVE_PCAP_LIB_VERSION 2654 #ifdef _WIN32 2655 (void)fprintf(stderr, "%s version %s, based on tcpdump version %s\n", program_name, WDversion, version); 2656 #else /* _WIN32 */ 2657 (void)fprintf(stderr, "%s version %s\n", program_name, version); 2658 #endif /* _WIN32 */ 2659 (void)fprintf(stderr, "%s\n",pcap_lib_version()); 2660 #else /* HAVE_PCAP_LIB_VERSION */ 2661 #ifdef _WIN32 2662 (void)fprintf(stderr, "%s version %s, based on tcpdump version %s\n", program_name, WDversion, version); 2663 (void)fprintf(stderr, "WinPcap version %s, based on libpcap version %s\n",Wpcap_version, pcap_version); 2664 #else /* _WIN32 */ 2665 (void)fprintf(stderr, "%s version %s\n", program_name, version); 2666 (void)fprintf(stderr, "libpcap version %s\n", pcap_version); 2667 #endif /* _WIN32 */ 2668 #endif /* HAVE_PCAP_LIB_VERSION */ 2669 2670 #if defined(HAVE_LIBCRYPTO) && defined(SSLEAY_VERSION) 2671 (void)fprintf (stderr, "%s\n", SSLeay_version(SSLEAY_VERSION)); 2672 #endif 2673 2674 smi_version_string = nd_smi_version_string(); 2675 if (smi_version_string != NULL) 2676 (void)fprintf (stderr, "SMI-library: %s\n", smi_version_string); 2677 } 2678 USES_APPLE_RST 2679 2680 static void 2681 print_usage(void) 2682 { 2683 print_version(); 2684 (void)fprintf(stderr, 2685 "Usage: %s [-aAbd" D_FLAG "efhH" I_FLAG J_FLAG "KlLnNOpqStu" U_FLAG "vxX#]" B_FLAG_USAGE " [ -c count ]\n", program_name); 2686 (void)fprintf(stderr, 2687 "\t\t[ -C file_size ] [ -E algo:secret ] [ -F file ] [ -G seconds ]\n"); 2688 (void)fprintf(stderr, 2689 "\t\t[ -i interface ]" j_FLAG_USAGE " [ -M secret ] [ --number ]\n"); 2690 #ifdef HAVE_PCAP_SETDIRECTION 2691 (void)fprintf(stderr, 2692 "\t\t[ -Q in|out|inout ]\n"); 2693 #endif 2694 (void)fprintf(stderr, 2695 "\t\t[ -r file ] [ -s snaplen ] "); 2696 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION 2697 (void)fprintf(stderr, "[ --time-stamp-precision precision ]\n"); 2698 (void)fprintf(stderr, 2699 "\t\t"); 2700 #endif 2701 #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE 2702 (void)fprintf(stderr, "[ --immediate-mode ] "); 2703 #endif 2704 (void)fprintf(stderr, "[ -T type ] [ --version ] [ -V file ]\n"); 2705 (void)fprintf(stderr, 2706 "\t\t[ -w file ] [ -W filecount ] [ -y datalinktype ] [ -z postrotate-command ]\n"); 2707 (void)fprintf(stderr, 2708 "\t\t[ -Z user ] [ expression ]\n"); 2709 } 2710 /* 2711 * Local Variables: 2712 * c-style: whitesmith 2713 * c-basic-offset: 8 2714 * End: 2715 */ 2716