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.
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
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.
21 * Support for splitting captures into multiple files with a maximum
25 * Seth Webster <swebster@sst.ll.mit.edu>
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";
35 * tcpdump - dump traffic on a network
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.
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().
51 #ifdef HAVE_PCAP_FINDALLDEVS
52 #ifndef HAVE_PCAP_IF_T
53 #undef HAVE_PCAP_FINDALLDEVS
57 #include <netdissect-stdinc.h>
66 #include <openssl/crypto.h>
69 #ifdef HAVE_GETOPT_LONG
72 #include "getopt_long.h"
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.
79 #include <sys/capsicum.h>
81 #include <sys/ioccom.h>
85 #include <libcasper.h>
86 #include <casper/cap_dns.h>
87 #endif /* HAVE_CASPER */
88 #endif /* HAVE_CAPSICUM */
98 #include <sys/resource.h>
103 /* capabilities convenience library */
104 /* If a code depends on HAVE_LIBCAP_NG, it depends also on HAVE_CAP_NG_H.
105 * If HAVE_CAP_NG_H is not defined, undefine HAVE_LIBCAP_NG.
106 * Thus, the later tests are done only on HAVE_LIBCAP_NG.
108 #ifdef HAVE_LIBCAP_NG
112 #undef HAVE_LIBCAP_NG
113 #endif /* HAVE_CAP_NG_H */
114 #endif /* HAVE_LIBCAP_NG */
117 #include <sys/sysctl.h>
118 #endif /* __FreeBSD__ */
120 #include "netdissect.h"
121 #include "interface.h"
122 #include "addrtoname.h"
124 #include "setsignal.h"
125 #include "gmt2local.h"
126 #include "pcap-missing.h"
127 #include "ascii_strcasecmp.h"
132 #define PATH_MAX 1024
136 #define SIGNAL_REQ_INFO SIGINFO
138 #define SIGNAL_REQ_INFO SIGUSR1
141 static int Bflag; /* buffer size */
142 static long Cflag; /* rotate dump files after this many bytes */
143 static int Cflag_count; /* Keep track of which file number we're writing */
144 static int Dflag; /* list available devices and exit */
146 * This is exported because, in some versions of libpcap, if libpcap
147 * is built with optimizer debugging code (which is *NOT* the default
148 * configuration!), the library *imports*(!) a variable named dflag,
149 * under the expectation that tcpdump is exporting it, to govern
150 * how much debugging information to print when optimizing
151 * the generated BPF code.
153 * This is a horrible hack; newer versions of libpcap don't import
154 * dflag but, instead, *if* built with optimizer debugging code,
155 * *export* a routine to set that flag.
157 int dflag; /* print filter code */
158 static int Gflag; /* rotate dump files after this many seconds */
159 static int Gflag_count; /* number of files created with Gflag rotation */
160 static time_t Gflag_time; /* The last time_t the dump file was rotated. */
161 static int Lflag; /* list available data link types and exit */
162 static int Iflag; /* rfmon (monitor) mode */
163 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
164 static int Jflag; /* list available time stamp types */
166 static int jflag = -1; /* packet time stamp source */
167 static int pflag; /* don't go promiscuous */
168 #ifdef HAVE_PCAP_SETDIRECTION
169 static int Qflag = -1; /* restrict captured packet by send/receive direction */
171 static int Uflag; /* "unbuffered" output of dump files */
172 static int Wflag; /* recycle output files after this number of files */
173 static int WflagChars;
174 static char *zflag = NULL; /* compress each savefile using a specified command (like gzip or bzip2) */
175 static int immediate_mode;
177 static int infodelay;
178 static int infoprint;
183 cap_channel_t *capdns;
187 static void error(FORMAT_STRING(const char *), ...) NORETURN PRINTFLIKE(1, 2);
188 static void warning(FORMAT_STRING(const char *), ...) PRINTFLIKE(1, 2);
189 static void exit_tcpdump(int) NORETURN;
190 static RETSIGTYPE cleanup(int);
191 static RETSIGTYPE child_cleanup(int);
192 static void print_version(void);
193 static void print_usage(void);
194 static void show_tstamp_types_and_exit(pcap_t *, const char *device) NORETURN;
195 static void show_dlts_and_exit(pcap_t *, const char *device) NORETURN;
196 #ifdef HAVE_PCAP_FINDALLDEVS
197 static void show_devices_and_exit (void) NORETURN;
200 static void print_packet(u_char *, const struct pcap_pkthdr *, const u_char *);
201 static void dump_packet_and_trunc(u_char *, const struct pcap_pkthdr *, const u_char *);
202 static void dump_packet(u_char *, const struct pcap_pkthdr *, const u_char *);
203 static void droproot(const char *, const char *);
205 #ifdef SIGNAL_REQ_INFO
206 RETSIGTYPE requestinfo(int);
209 #if defined(USE_WIN32_MM_TIMER)
210 #include <MMsystem.h>
211 static UINT timer_id;
212 static void CALLBACK verbose_stats_dump(UINT, UINT, DWORD_PTR, DWORD_PTR, DWORD_PTR);
213 #elif defined(HAVE_ALARM)
214 static void verbose_stats_dump(int sig);
217 static void info(int);
218 static u_int packets_captured;
220 #ifdef HAVE_PCAP_FINDALLDEVS
221 static const struct tok status_flags[] = {
223 { PCAP_IF_UP, "Up" },
225 #ifdef PCAP_IF_RUNNING
226 { PCAP_IF_RUNNING, "Running" },
228 { PCAP_IF_LOOPBACK, "Loopback" },
235 static int supports_monitor_mode;
243 char *CurrentFileName;
251 #if defined(HAVE_PCAP_SET_PARSER_DEBUG)
253 * We have pcap_set_parser_debug() in libpcap; declare it (it's not declared
254 * by any libpcap header, because it's a special hack, only available if
255 * libpcap was configured to include it, and only intended for use by
256 * libpcap developers trying to debug the parser for filter expressions).
259 __declspec(dllimport)
263 void pcap_set_parser_debug(int);
264 #elif defined(HAVE_PCAP_DEBUG) || defined(HAVE_YYDEBUG)
266 * We don't have pcap_set_parser_debug() in libpcap, but we do have
267 * pcap_debug or yydebug. Make a local version of pcap_set_parser_debug()
268 * to set the flag, and define HAVE_PCAP_SET_PARSER_DEBUG.
271 pcap_set_parser_debug(int value)
273 #ifdef HAVE_PCAP_DEBUG
274 extern int pcap_debug;
277 #else /* HAVE_PCAP_DEBUG */
281 #endif /* HAVE_PCAP_DEBUG */
284 #define HAVE_PCAP_SET_PARSER_DEBUG
287 #if defined(HAVE_PCAP_SET_OPTIMIZER_DEBUG)
289 * We have pcap_set_optimizer_debug() in libpcap; declare it (it's not declared
290 * by any libpcap header, because it's a special hack, only available if
291 * libpcap was configured to include it, and only intended for use by
292 * libpcap developers trying to debug the optimizer for filter expressions).
295 __declspec(dllimport)
299 void pcap_set_optimizer_debug(int);
304 error(const char *fmt, ...)
308 (void)fprintf(stderr, "%s: ", program_name);
310 (void)vfprintf(stderr, fmt, ap);
315 (void)fputc('\n', stderr);
323 warning(const char *fmt, ...)
327 (void)fprintf(stderr, "%s: WARNING: ", program_name);
329 (void)vfprintf(stderr, fmt, ap);
334 (void)fputc('\n', stderr);
339 exit_tcpdump(int status)
345 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
347 show_tstamp_types_and_exit(pcap_t *pc, const char *device)
350 int *tstamp_types = 0;
351 const char *tstamp_type_name;
354 n_tstamp_types = pcap_list_tstamp_types(pc, &tstamp_types);
355 if (n_tstamp_types < 0)
356 error("%s", pcap_geterr(pc));
358 if (n_tstamp_types == 0) {
359 fprintf(stderr, "Time stamp type cannot be set for %s\n",
363 fprintf(stderr, "Time stamp types for %s (use option -j to set):\n",
365 for (i = 0; i < n_tstamp_types; i++) {
366 tstamp_type_name = pcap_tstamp_type_val_to_name(tstamp_types[i]);
367 if (tstamp_type_name != NULL) {
368 (void) fprintf(stderr, " %s (%s)\n", tstamp_type_name,
369 pcap_tstamp_type_val_to_description(tstamp_types[i]));
371 (void) fprintf(stderr, " %d\n", tstamp_types[i]);
374 pcap_free_tstamp_types(tstamp_types);
380 show_dlts_and_exit(pcap_t *pc, const char *device)
384 const char *dlt_name;
386 n_dlts = pcap_list_datalinks(pc, &dlts);
388 error("%s", pcap_geterr(pc));
389 else if (n_dlts == 0 || !dlts)
390 error("No data link types.");
393 * If the interface is known to support monitor mode, indicate
394 * whether these are the data link types available when not in
395 * monitor mode, if -I wasn't specified, or when in monitor mode,
396 * when -I was specified (the link-layer types available in
397 * monitor mode might be different from the ones available when
398 * not in monitor mode).
400 if (supports_monitor_mode)
401 (void) fprintf(stderr, "Data link types for %s %s (use option -y to set):\n",
403 Iflag ? "when in monitor mode" : "when not in monitor mode");
405 (void) fprintf(stderr, "Data link types for %s (use option -y to set):\n",
408 for (i = 0; i < n_dlts; i++) {
409 dlt_name = pcap_datalink_val_to_name(dlts[i]);
410 if (dlt_name != NULL) {
411 (void) fprintf(stderr, " %s (%s)", dlt_name,
412 pcap_datalink_val_to_description(dlts[i]));
415 * OK, does tcpdump handle that type?
417 if (!has_printer(dlts[i]))
418 (void) fprintf(stderr, " (printing not supported)");
419 fprintf(stderr, "\n");
421 (void) fprintf(stderr, " DLT %d (printing not supported)\n",
425 #ifdef HAVE_PCAP_FREE_DATALINKS
426 pcap_free_datalinks(dlts);
431 #ifdef HAVE_PCAP_FINDALLDEVS
433 show_devices_and_exit (void)
435 pcap_if_t *dev, *devlist;
436 char ebuf[PCAP_ERRBUF_SIZE];
439 if (pcap_findalldevs(&devlist, ebuf) < 0)
441 for (i = 0, dev = devlist; dev != NULL; i++, dev = dev->next) {
442 printf("%d.%s", i+1, dev->name);
443 if (dev->description != NULL)
444 printf(" (%s)", dev->description);
446 printf(" [%s]", bittok2str(status_flags, "none", dev->flags));
449 pcap_freealldevs(devlist);
452 #endif /* HAVE_PCAP_FINDALLDEVS */
457 * Note that there we use all letters for short options except for g, k,
458 * o, and P, and those are used by other versions of tcpdump, and we should
459 * only use them for the same purposes that the other versions of tcpdump
462 * OS X tcpdump uses -g to force non--v output for IP to be on one
463 * line, making it more "g"repable;
465 * OS X tcpdump uses -k to specify that packet comments in pcap-ng files
468 * OpenBSD tcpdump uses -o to indicate that OS fingerprinting should be done
469 * for hosts sending TCP SYN packets;
471 * OS X tcpdump uses -P to indicate that -w should write pcap-ng rather
474 * OS X tcpdump also uses -Q to specify expressions that match packet
475 * metadata, including but not limited to the packet direction.
476 * The expression syntax is different from a simple "in|out|inout",
477 * and those expressions aren't accepted by OS X tcpdump, but the
478 * equivalents would be "in" = "dir=in", "out" = "dir=out", and
479 * "inout" = "dir=in or dir=out", and the parser could conceivably
480 * special-case "in", "out", and "inout" as expressions for backwards
481 * compatibility, so all is not (yet) lost.
485 * Set up flags that might or might not be supported depending on the
486 * version of libpcap we're using.
488 #if defined(HAVE_PCAP_CREATE) || defined(_WIN32)
490 #define B_FLAG_USAGE " [ -B size ]"
491 #else /* defined(HAVE_PCAP_CREATE) || defined(_WIN32) */
494 #endif /* defined(HAVE_PCAP_CREATE) || defined(_WIN32) */
496 #ifdef HAVE_PCAP_CREATE
498 #else /* HAVE_PCAP_CREATE */
500 #endif /* HAVE_PCAP_CREATE */
502 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
504 #define j_FLAG_USAGE " [ -j tstamptype ]"
506 #else /* PCAP_ERROR_TSTAMP_TYPE_NOTSUP */
510 #endif /* PCAP_ERROR_TSTAMP_TYPE_NOTSUP */
512 #ifdef HAVE_PCAP_FINDALLDEVS
518 #ifdef HAVE_PCAP_DUMP_FLUSH
524 #ifdef HAVE_PCAP_SETDIRECTION
530 #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:#"
535 * We do not currently have long options corresponding to all short
536 * options; we should probably pick appropriate option names for them.
538 * However, the short options where the number of times the option is
539 * specified matters, such as -v and -d and -t, should probably not
540 * just map to a long option, as saying
542 * tcpdump --verbose --verbose
544 * doesn't make sense; it should be --verbosity={N} or something such
547 * For long options with no corresponding short options, we define values
548 * outside the range of ASCII graphic characters, make that the last
549 * component of the entry for the long option, and have a case for that
550 * option in the switch statement.
552 #define OPTION_VERSION 128
553 #define OPTION_TSTAMP_PRECISION 129
554 #define OPTION_IMMEDIATE_MODE 130
556 static const struct option longopts[] = {
557 #if defined(HAVE_PCAP_CREATE) || defined(_WIN32)
558 { "buffer-size", required_argument, NULL, 'B' },
560 { "list-interfaces", no_argument, NULL, 'D' },
561 { "help", no_argument, NULL, 'h' },
562 { "interface", required_argument, NULL, 'i' },
563 #ifdef HAVE_PCAP_CREATE
564 { "monitor-mode", no_argument, NULL, 'I' },
566 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
567 { "time-stamp-type", required_argument, NULL, 'j' },
568 { "list-time-stamp-types", no_argument, NULL, 'J' },
570 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
571 { "time-stamp-precision", required_argument, NULL, OPTION_TSTAMP_PRECISION},
573 { "dont-verify-checksums", no_argument, NULL, 'K' },
574 { "list-data-link-types", no_argument, NULL, 'L' },
575 { "no-optimize", no_argument, NULL, 'O' },
576 { "no-promiscuous-mode", no_argument, NULL, 'p' },
577 #ifdef HAVE_PCAP_SETDIRECTION
578 { "direction", required_argument, NULL, 'Q' },
580 { "snapshot-length", required_argument, NULL, 's' },
581 { "absolute-tcp-sequence-numbers", no_argument, NULL, 'S' },
582 #ifdef HAVE_PCAP_DUMP_FLUSH
583 { "packet-buffered", no_argument, NULL, 'U' },
585 { "linktype", required_argument, NULL, 'y' },
586 #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
587 { "immediate-mode", no_argument, NULL, OPTION_IMMEDIATE_MODE },
589 #ifdef HAVE_PCAP_SET_PARSER_DEBUG
590 { "debug-filter-parser", no_argument, NULL, 'Y' },
592 { "relinquish-privileges", required_argument, NULL, 'Z' },
593 { "number", no_argument, NULL, '#' },
594 { "version", no_argument, NULL, OPTION_VERSION },
599 /* Drop root privileges and chroot if necessary */
601 droproot(const char *username, const char *chroot_dir)
603 struct passwd *pw = NULL;
605 if (chroot_dir && !username) {
606 fprintf(stderr, "%s: Chroot without dropping root is insecure\n",
611 pw = getpwnam(username);
614 if (chroot(chroot_dir) != 0 || chdir ("/") != 0) {
615 fprintf(stderr, "%s: Couldn't chroot/chdir to '%.64s': %s\n",
616 program_name, chroot_dir, pcap_strerror(errno));
620 #ifdef HAVE_LIBCAP_NG
622 int ret = capng_change_id(pw->pw_uid, pw->pw_gid, CAPNG_NO_FLAG);
624 fprintf(stderr, "error : ret %d\n", ret);
626 fprintf(stderr, "dropped privs to %s\n", username);
630 if (initgroups(pw->pw_name, pw->pw_gid) != 0 ||
631 setgid(pw->pw_gid) != 0 || setuid(pw->pw_uid) != 0) {
632 fprintf(stderr, "%s: Couldn't change to '%.32s' uid=%lu gid=%lu: %s\n",
633 program_name, username,
634 (unsigned long)pw->pw_uid,
635 (unsigned long)pw->pw_gid,
636 pcap_strerror(errno));
640 fprintf(stderr, "dropped privs to %s\n", username);
642 #endif /* HAVE_LIBCAP_NG */
645 fprintf(stderr, "%s: Couldn't find user '%.32s'\n",
646 program_name, username);
649 #ifdef HAVE_LIBCAP_NG
650 /* We don't need CAP_SETUID, CAP_SETGID and CAP_SYS_CHROOT any more. */
653 CAPNG_EFFECTIVE | CAPNG_PERMITTED,
658 capng_apply(CAPNG_SELECT_BOTH);
659 #endif /* HAVE_LIBCAP_NG */
680 MakeFilename(char *buffer, char *orig_name, int cnt, int max_chars)
682 char *filename = malloc(PATH_MAX + 1);
683 if (filename == NULL)
684 error("Makefilename: malloc");
686 /* Process with strftime if Gflag is set. */
690 /* Convert Gflag_time to a usable format */
691 if ((local_tm = localtime(&Gflag_time)) == NULL) {
692 error("MakeTimedFilename: localtime");
695 /* There's no good way to detect an error in strftime since a return
696 * value of 0 isn't necessarily failure.
698 strftime(filename, PATH_MAX, orig_name, local_tm);
700 strncpy(filename, orig_name, PATH_MAX);
703 if (cnt == 0 && max_chars == 0)
704 strncpy(buffer, filename, PATH_MAX + 1);
706 if (snprintf(buffer, PATH_MAX + 1, "%s%0*d", filename, max_chars, cnt) > PATH_MAX)
707 /* Report an error if the filename is too large */
708 error("too many output files or filename is too long (> %d)", PATH_MAX);
713 get_next_file(FILE *VFile, char *ptr)
717 ret = fgets(ptr, PATH_MAX, VFile);
721 if (ptr[strlen(ptr) - 1] == '\n')
722 ptr[strlen(ptr) - 1] = '\0';
728 static cap_channel_t *
731 cap_channel_t *capcas, *capdnsloc;
732 const char *types[1];
737 error("unable to create casper process");
738 capdnsloc = cap_service_open(capcas, "system.dns");
739 /* Casper capability no longer needed. */
741 if (capdnsloc == NULL)
742 error("unable to open system.dns service");
743 /* Limit system.dns to reverse DNS lookups. */
745 if (cap_dns_type_limit(capdnsloc, types, 1) < 0)
746 error("unable to limit access to system.dns service");
747 families[0] = AF_INET;
748 families[1] = AF_INET6;
749 if (cap_dns_family_limit(capdnsloc, families, 2) < 0)
750 error("unable to limit access to system.dns service");
754 #endif /* HAVE_CASPER */
756 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
758 tstamp_precision_from_string(const char *precision)
760 if (strncmp(precision, "nano", strlen("nano")) == 0)
761 return PCAP_TSTAMP_PRECISION_NANO;
763 if (strncmp(precision, "micro", strlen("micro")) == 0)
764 return PCAP_TSTAMP_PRECISION_MICRO;
770 tstamp_precision_to_string(int precision)
774 case PCAP_TSTAMP_PRECISION_MICRO:
777 case PCAP_TSTAMP_PRECISION_NANO:
788 * Ensure that, on a dump file's descriptor, we have all the rights
789 * necessary to make the standard I/O library work with an fdopen()ed
790 * FILE * from that descriptor.
792 * A long time ago, in a galaxy far far away, AT&T decided that, instead
793 * of providing separate APIs for getting and setting the FD_ flags on a
794 * descriptor, getting and setting the O_ flags on a descriptor, and
795 * locking files, they'd throw them all into a kitchen-sink fcntl() call
796 * along the lines of ioctl(), the fact that ioctl() operations are
797 * largely specific to particular character devices but fcntl() operations
798 * are either generic to all descriptors or generic to all descriptors for
799 * regular files nonwithstanding.
801 * The Capsicum people decided that fine-grained control of descriptor
802 * operations was required, so that you need to grant permission for
803 * reading, writing, seeking, and fcntl-ing. The latter, courtesy of
804 * AT&T's decision, means that "fcntl-ing" isn't a thing, but a motley
805 * collection of things, so there are *individual* fcntls for which
806 * permission needs to be granted.
808 * The FreeBSD standard I/O people implemented some optimizations that
809 * requires that the standard I/O routines be able to determine whether
810 * the descriptor for the FILE * is open append-only or not; as that
811 * descriptor could have come from an open() rather than an fopen(),
812 * that requires that it be able to do an F_GETFL fcntl() to read
815 * Tcpdump uses ftell() to determine how much data has been written
816 * to a file in order to, when used with -C, determine when it's time
817 * to rotate capture files. ftell() therefore needs to do an lseek()
818 * to find out the file offset and must, thanks to the aforementioned
819 * optimization, also know whether the descriptor is open append-only
822 * The net result of all the above is that we need to grant CAP_SEEK,
823 * CAP_WRITE, and CAP_FCNTL with the CAP_FCNTL_GETFL subcapability.
825 * Perhaps this is the universe's way of saying that either
827 * 1) there needs to be an fopenat() call and a pcap_dump_openat() call
828 * using it, so that Capsicum-capable tcpdump wouldn't need to do
833 * 2) there needs to be a cap_fdopen() call in the FreeBSD standard
834 * I/O library that knows what rights are needed by the standard
835 * I/O library, based on the open mode, and assigns them, perhaps
836 * with an additional argument indicating, for example, whether
837 * seeking should be allowed, so that tcpdump doesn't need to know
838 * what the standard I/O library happens to require this week.
841 set_dumper_capsicum_rights(pcap_dumper_t *p)
843 int fd = fileno(pcap_dump_file(p));
846 cap_rights_init(&rights, CAP_SEEK, CAP_WRITE, CAP_FCNTL);
847 if (cap_rights_limit(fd, &rights) < 0 && errno != ENOSYS) {
848 error("unable to limit dump descriptor");
850 if (cap_fcntls_limit(fd, CAP_FCNTL_GETFL) < 0 && errno != ENOSYS) {
851 error("unable to limit dump descriptor fcntls");
857 * Copy arg vector into a new buffer, concatenating arguments with spaces.
860 copy_argv(register char **argv)
863 register u_int len = 0;
872 len += strlen(*p++) + 1;
874 buf = (char *)malloc(len);
876 error("copy_argv: malloc");
880 while ((src = *p++) != NULL) {
881 while ((*dst++ = *src++) != '\0')
891 * On Windows, we need to open the file in binary mode, so that
892 * we get all the bytes specified by the size we get from "fstat()".
893 * On UNIX, that's not necessary. O_BINARY is defined on Windows;
894 * we define it as 0 if it's not defined, so it does nothing.
901 read_infile(char *fname)
903 register int i, fd, cc;
907 fd = open(fname, O_RDONLY|O_BINARY);
909 error("can't open %s: %s", fname, pcap_strerror(errno));
911 if (fstat(fd, &buf) < 0)
912 error("can't stat %s: %s", fname, pcap_strerror(errno));
914 cp = malloc((u_int)buf.st_size + 1);
916 error("malloc(%d) for %s: %s", (u_int)buf.st_size + 1,
917 fname, pcap_strerror(errno));
918 cc = read(fd, cp, (u_int)buf.st_size);
920 error("read %s: %s", fname, pcap_strerror(errno));
921 if (cc != buf.st_size)
922 error("short read %s (%d != %d)", fname, cc, (int)buf.st_size);
925 /* replace "# comment" with spaces */
926 for (i = 0; i < cc; i++) {
928 while (i < cc && cp[i] != '\n')
935 #ifdef HAVE_PCAP_FINDALLDEVS
937 parse_interface_number(const char *device)
942 devnum = strtol(device, &end, 10);
943 if (device != end && *end == '\0') {
945 * It's all-numeric, but is it a valid number?
949 * No, it's not an ordinal.
951 error("Invalid adapter index");
956 * It's not all-numeric; return -1, so our caller
964 find_interface_by_number(long devnum)
966 pcap_if_t *dev, *devlist;
968 char ebuf[PCAP_ERRBUF_SIZE];
971 if (pcap_findalldevs(&devlist, ebuf) < 0)
974 * Look for the devnum-th entry in the list of devices (1-based).
976 for (i = 0, dev = devlist; i < devnum-1 && dev != NULL;
977 i++, dev = dev->next)
980 error("Invalid adapter index");
981 device = strdup(dev->name);
982 pcap_freealldevs(devlist);
988 open_interface(const char *device, netdissect_options *ndo, char *ebuf)
991 #ifdef HAVE_PCAP_CREATE
996 #ifdef HAVE_PCAP_CREATE
997 pc = pcap_create(device, ebuf);
1000 * If this failed with "No such device", that means
1001 * the interface doesn't exist; return NULL, so that
1002 * the caller can see whether the device name is
1003 * actually an interface index.
1005 if (strstr(ebuf, "No such device") != NULL)
1009 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
1011 show_tstamp_types_and_exit(pc, device);
1013 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
1014 status = pcap_set_tstamp_precision(pc, ndo->ndo_tstamp_precision);
1016 error("%s: Can't set %ssecond time stamp precision: %s",
1018 tstamp_precision_to_string(ndo->ndo_tstamp_precision),
1019 pcap_statustostr(status));
1022 #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
1023 if (immediate_mode) {
1024 status = pcap_set_immediate_mode(pc, 1);
1026 error("%s: Can't set immediate mode: %s",
1028 pcap_statustostr(status));
1032 * Is this an interface that supports monitor mode?
1034 if (pcap_can_set_rfmon(pc) == 1)
1035 supports_monitor_mode = 1;
1037 supports_monitor_mode = 0;
1038 status = pcap_set_snaplen(pc, ndo->ndo_snaplen);
1040 error("%s: Can't set snapshot length: %s",
1041 device, pcap_statustostr(status));
1042 status = pcap_set_promisc(pc, !pflag);
1044 error("%s: Can't set promiscuous mode: %s",
1045 device, pcap_statustostr(status));
1047 status = pcap_set_rfmon(pc, 1);
1049 error("%s: Can't set monitor mode: %s",
1050 device, pcap_statustostr(status));
1052 status = pcap_set_timeout(pc, 1000);
1054 error("%s: pcap_set_timeout failed: %s",
1055 device, pcap_statustostr(status));
1057 status = pcap_set_buffer_size(pc, Bflag);
1059 error("%s: Can't set buffer size: %s",
1060 device, pcap_statustostr(status));
1062 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
1064 status = pcap_set_tstamp_type(pc, jflag);
1066 error("%s: Can't set time stamp type: %s",
1067 device, pcap_statustostr(status));
1070 status = pcap_activate(pc);
1073 * pcap_activate() failed.
1075 cp = pcap_geterr(pc);
1076 if (status == PCAP_ERROR)
1078 else if (status == PCAP_ERROR_NO_SUCH_DEVICE) {
1080 * Return an error for our caller to handle.
1082 snprintf(ebuf, PCAP_ERRBUF_SIZE, "%s: %s\n(%s)",
1083 device, pcap_statustostr(status), cp);
1086 } else if (status == PCAP_ERROR_PERM_DENIED && *cp != '\0')
1087 error("%s: %s\n(%s)", device,
1088 pcap_statustostr(status), cp);
1090 else if (status == PCAP_ERROR_RFMON_NOTSUP &&
1091 strncmp(device, "wlan", 4) == 0) {
1092 char parent[8], newdev[8];
1094 size_t s = sizeof(parent);
1096 snprintf(sysctl, sizeof(sysctl),
1097 "net.wlan.%d.%%parent", atoi(device + 4));
1098 sysctlbyname(sysctl, parent, &s, NULL, 0);
1099 strlcpy(newdev, device, sizeof(newdev));
1100 /* Suggest a new wlan device. */
1101 /* FIXME: incrementing the index this way is not going to work well
1102 * when the index is 9 or greater but the only consequence in this
1103 * specific case would be an error message that looks a bit odd.
1105 newdev[strlen(newdev)-1]++;
1106 error("%s is not a monitor mode VAP\n"
1107 "To create a new monitor mode VAP use:\n"
1108 " ifconfig %s create wlandev %s wlanmode monitor\n"
1109 "and use %s as the tcpdump interface",
1110 device, newdev, parent, newdev);
1114 error("%s: %s", device,
1115 pcap_statustostr(status));
1116 } else if (status > 0) {
1118 * pcap_activate() succeeded, but it's warning us
1119 * of a problem it had.
1121 cp = pcap_geterr(pc);
1122 if (status == PCAP_WARNING)
1124 else if (status == PCAP_WARNING_PROMISC_NOTSUP &&
1126 warning("%s: %s\n(%s)", device,
1127 pcap_statustostr(status), cp);
1129 warning("%s: %s", device,
1130 pcap_statustostr(status));
1132 #ifdef HAVE_PCAP_SETDIRECTION
1134 status = pcap_setdirection(pc, Qflag);
1136 error("%s: pcap_setdirection() failed: %s",
1137 device, pcap_geterr(pc));
1139 #endif /* HAVE_PCAP_SETDIRECTION */
1140 #else /* HAVE_PCAP_CREATE */
1142 pc = pcap_open_live(device, ndo->ndo_snaplen, !pflag, 1000, ebuf);
1145 * If this failed with "No such device", that means
1146 * the interface doesn't exist; return NULL, so that
1147 * the caller can see whether the device name is
1148 * actually an interface index.
1150 if (strstr(ebuf, "No such device") != NULL)
1155 warning("%s", ebuf);
1156 #endif /* HAVE_PCAP_CREATE */
1162 main(int argc, char **argv)
1164 register int cnt, op, i;
1165 bpf_u_int32 localnet =0 , netmask = 0;
1166 int timezone_offset = 0;
1167 register char *cp, *infile, *cmdbuf, *device, *RFileName, *VFileName, *WFileName;
1168 pcap_handler callback;
1170 const char *dlt_name;
1171 struct bpf_program fcode;
1173 RETSIGTYPE (*oldhandler)(int);
1175 struct dump_info dumpinfo;
1176 u_char *pcap_userdata;
1177 char ebuf[PCAP_ERRBUF_SIZE];
1178 char VFileLine[PATH_MAX + 1];
1179 char *username = NULL;
1180 char *chroot_dir = NULL;
1183 #ifdef HAVE_PCAP_FINDALLDEVS
1189 #ifdef HAVE_CAPSICUM
1190 cap_rights_t rights;
1192 #endif /* HAVE_CAPSICUM */
1193 int Oflag = 1; /* run filter code optimizer */
1195 const char *yflag_dlt_name = NULL;
1197 netdissect_options Ndo;
1198 netdissect_options *ndo = &Ndo;
1201 * Initialize the netdissect code.
1203 if (nd_init(ebuf, sizeof ebuf) == -1)
1206 memset(ndo, 0, sizeof(*ndo));
1207 ndo_set_function_pointers(ndo);
1208 ndo->ndo_snaplen = DEFAULT_SNAPLEN;
1218 if ((cp = strrchr(argv[0], '/')) != NULL)
1219 ndo->program_name = program_name = cp + 1;
1221 ndo->program_name = program_name = argv[0];
1224 if (pcap_wsockinit() != 0)
1225 error("Attempting to initialize Winsock failed");
1229 * On platforms where the CPU doesn't support unaligned loads,
1230 * force unaligned accesses to abort with SIGBUS, rather than
1231 * being fixed up (slowly) by the OS kernel; on those platforms,
1232 * misaligned accesses are bugs, and we want tcpdump to crash so
1233 * that the bugs are reported.
1235 if (abort_on_misalignment(ebuf, sizeof(ebuf)) < 0)
1239 (op = getopt_long(argc, argv, SHORTOPTS, longopts, NULL)) != -1)
1243 /* compatibility for old -a */
1254 #if defined(HAVE_PCAP_CREATE) || defined(_WIN32)
1256 Bflag = atoi(optarg)*1024;
1258 error("invalid packet buffer size %s", optarg);
1260 #endif /* defined(HAVE_PCAP_CREATE) || defined(_WIN32) */
1265 error("invalid packet count %s", optarg);
1269 Cflag = atoi(optarg) * 1000000;
1271 error("invalid file size %s", optarg);
1291 #ifndef HAVE_LIBCRYPTO
1292 warning("crypto code not compiled in");
1294 ndo->ndo_espsecret = optarg;
1306 Gflag = atoi(optarg);
1308 error("invalid number of seconds %s", optarg);
1310 /* We will create one file initially. */
1313 /* Grab the current time for rotation use. */
1314 if ((Gflag_time = time(NULL)) == (time_t)-1) {
1315 error("main: can't get current time: %s",
1316 pcap_strerror(errno));
1333 #ifdef HAVE_PCAP_CREATE
1337 #endif /* HAVE_PCAP_CREATE */
1339 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
1341 jflag = pcap_tstamp_type_name_to_val(optarg);
1343 error("invalid time stamp type %s", optarg);
1354 * _IOLBF is the same as _IOFBF in Microsoft's C
1355 * libraries; the only alternative they offer
1358 * XXX - this should really be checking for MSVC++,
1359 * not _WIN32, if, for example, MinGW has its own
1360 * C library that is more UNIX-compatible.
1362 setvbuf(stdout, NULL, _IONBF, 0);
1364 #ifdef HAVE_SETLINEBUF
1367 setvbuf(stdout, NULL, _IOLBF, 0);
1377 if (nd_have_smi_support()) {
1378 if (nd_load_smi_module(optarg, ebuf, sizeof ebuf) == -1)
1381 (void)fprintf(stderr, "%s: ignoring option `-m %s' ",
1382 program_name, optarg);
1383 (void)fprintf(stderr, "(no libsmi support)\n");
1388 /* TCP-MD5 shared secret */
1389 #ifndef HAVE_LIBCRYPTO
1390 warning("crypto code not compiled in");
1392 ndo->ndo_sigsecret = optarg;
1413 ++ndo->ndo_suppress_default_print;
1416 #ifdef HAVE_PCAP_SETDIRECTION
1418 if (ascii_strcasecmp(optarg, "in") == 0)
1420 else if (ascii_strcasecmp(optarg, "out") == 0)
1422 else if (ascii_strcasecmp(optarg, "inout") == 0)
1423 Qflag = PCAP_D_INOUT;
1425 error("unknown capture direction `%s'", optarg);
1427 #endif /* HAVE_PCAP_SETDIRECTION */
1434 ndo->ndo_snaplen = strtol(optarg, &end, 0);
1435 if (optarg == end || *end != '\0'
1436 || ndo->ndo_snaplen < 0 || ndo->ndo_snaplen > MAXIMUM_SNAPLEN)
1437 error("invalid snaplen %s", optarg);
1438 else if (ndo->ndo_snaplen == 0)
1439 ndo->ndo_snaplen = MAXIMUM_SNAPLEN;
1451 if (ascii_strcasecmp(optarg, "vat") == 0)
1452 ndo->ndo_packettype = PT_VAT;
1453 else if (ascii_strcasecmp(optarg, "wb") == 0)
1454 ndo->ndo_packettype = PT_WB;
1455 else if (ascii_strcasecmp(optarg, "rpc") == 0)
1456 ndo->ndo_packettype = PT_RPC;
1457 else if (ascii_strcasecmp(optarg, "rtp") == 0)
1458 ndo->ndo_packettype = PT_RTP;
1459 else if (ascii_strcasecmp(optarg, "rtcp") == 0)
1460 ndo->ndo_packettype = PT_RTCP;
1461 else if (ascii_strcasecmp(optarg, "snmp") == 0)
1462 ndo->ndo_packettype = PT_SNMP;
1463 else if (ascii_strcasecmp(optarg, "cnfp") == 0)
1464 ndo->ndo_packettype = PT_CNFP;
1465 else if (ascii_strcasecmp(optarg, "tftp") == 0)
1466 ndo->ndo_packettype = PT_TFTP;
1467 else if (ascii_strcasecmp(optarg, "aodv") == 0)
1468 ndo->ndo_packettype = PT_AODV;
1469 else if (ascii_strcasecmp(optarg, "carp") == 0)
1470 ndo->ndo_packettype = PT_CARP;
1471 else if (ascii_strcasecmp(optarg, "radius") == 0)
1472 ndo->ndo_packettype = PT_RADIUS;
1473 else if (ascii_strcasecmp(optarg, "zmtp1") == 0)
1474 ndo->ndo_packettype = PT_ZMTP1;
1475 else if (ascii_strcasecmp(optarg, "vxlan") == 0)
1476 ndo->ndo_packettype = PT_VXLAN;
1477 else if (ascii_strcasecmp(optarg, "pgm") == 0)
1478 ndo->ndo_packettype = PT_PGM;
1479 else if (ascii_strcasecmp(optarg, "pgm_zmtp1") == 0)
1480 ndo->ndo_packettype = PT_PGM_ZMTP1;
1481 else if (ascii_strcasecmp(optarg, "lmp") == 0)
1482 ndo->ndo_packettype = PT_LMP;
1483 else if (ascii_strcasecmp(optarg, "resp") == 0)
1484 ndo->ndo_packettype = PT_RESP;
1486 error("unknown packet type `%s'", optarg);
1493 #ifdef HAVE_PCAP_DUMP_FLUSH
1512 Wflag = atoi(optarg);
1514 error("invalid number of output files %s", optarg);
1515 WflagChars = getWflagChars(Wflag);
1520 ++ndo->ndo_suppress_default_print;
1525 ++ndo->ndo_suppress_default_print;
1529 yflag_dlt_name = optarg;
1531 pcap_datalink_name_to_val(yflag_dlt_name);
1533 error("invalid data link type %s", yflag_dlt_name);
1536 #ifdef HAVE_PCAP_SET_PARSER_DEBUG
1539 /* Undocumented flag */
1540 pcap_set_parser_debug(1);
1553 ndo->ndo_packet_number = 1;
1556 case OPTION_VERSION:
1561 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
1562 case OPTION_TSTAMP_PRECISION:
1563 ndo->ndo_tstamp_precision = tstamp_precision_from_string(optarg);
1564 if (ndo->ndo_tstamp_precision < 0)
1565 error("unsupported time stamp precision");
1569 #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
1570 case OPTION_IMMEDIATE_MODE:
1581 #ifdef HAVE_PCAP_FINDALLDEVS
1583 show_devices_and_exit();
1586 switch (ndo->ndo_tflag) {
1588 case 0: /* Default */
1589 case 4: /* Default + Date*/
1590 timezone_offset = gmt2local(0);
1593 case 1: /* No time stamp */
1594 case 2: /* Unix timeval style */
1595 case 3: /* Microseconds since previous packet */
1596 case 5: /* Microseconds since first packet */
1599 default: /* Not supported */
1600 error("only -t, -tt, -ttt, -tttt and -ttttt are supported");
1604 if (ndo->ndo_fflag != 0 && (VFileName != NULL || RFileName != NULL))
1605 error("-f can not be used with -V or -r");
1607 if (VFileName != NULL && RFileName != NULL)
1608 error("-V and -r are mutually exclusive.");
1610 #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
1612 * If we're printing dissected packets to the standard output
1613 * rather than saving raw packets to a file, and the standard
1614 * output is a terminal, use immediate mode, as the user's
1615 * probably expecting to see packets pop up immediately.
1617 if (WFileName == NULL && isatty(1))
1622 /* if run as root, prepare for chrooting */
1623 if (getuid() == 0 || geteuid() == 0) {
1624 /* future extensibility for cmd-line arguments */
1626 chroot_dir = WITH_CHROOT;
1631 /* if run as root, prepare for dropping root privileges */
1632 if (getuid() == 0 || geteuid() == 0) {
1633 /* Run with '-Z root' to restore old behaviour */
1635 username = WITH_USER;
1639 if (RFileName != NULL || VFileName != NULL) {
1641 * If RFileName is non-null, it's the pathname of a
1642 * savefile to read. If VFileName is non-null, it's
1643 * the pathname of a file containing a list of pathnames
1644 * (one per line) of savefiles to read.
1646 * In either case, we're reading a savefile, not doing
1651 * We don't need network access, so relinquish any set-UID
1652 * or set-GID privileges we have (if any).
1654 * We do *not* want set-UID privileges when opening a
1655 * trace file, as that might let the user read other
1656 * people's trace files (especially if we're set-UID
1659 if (setgid(getgid()) != 0 || setuid(getuid()) != 0 )
1660 fprintf(stderr, "Warning: setgid/setuid failed !\n");
1662 if (VFileName != NULL) {
1663 if (VFileName[0] == '-' && VFileName[1] == '\0')
1666 VFile = fopen(VFileName, "r");
1669 error("Unable to open file: %s\n", pcap_strerror(errno));
1671 ret = get_next_file(VFile, VFileLine);
1673 error("Nothing in %s\n", VFileName);
1674 RFileName = VFileLine;
1677 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
1678 pd = pcap_open_offline_with_tstamp_precision(RFileName,
1679 ndo->ndo_tstamp_precision, ebuf);
1681 pd = pcap_open_offline(RFileName, ebuf);
1686 #ifdef HAVE_CAPSICUM
1687 cap_rights_init(&rights, CAP_READ);
1688 if (cap_rights_limit(fileno(pcap_file(pd)), &rights) < 0 &&
1690 error("unable to limit pcap descriptor");
1693 dlt = pcap_datalink(pd);
1694 dlt_name = pcap_datalink_val_to_name(dlt);
1695 if (dlt_name == NULL) {
1696 fprintf(stderr, "reading from file %s, link-type %u\n",
1700 "reading from file %s, link-type %s (%s)\n",
1701 RFileName, dlt_name,
1702 pcap_datalink_val_to_description(dlt));
1706 * We're doing a live capture.
1708 if (device == NULL) {
1710 * No interface was specified. Pick one.
1712 #ifdef HAVE_PCAP_FINDALLDEVS
1714 * Find the list of interfaces, and pick
1715 * the first interface.
1717 if (pcap_findalldevs(&devlist, ebuf) >= 0 &&
1719 device = strdup(devlist->name);
1720 pcap_freealldevs(devlist);
1722 #else /* HAVE_PCAP_FINDALLDEVS */
1724 * Use whatever interface pcap_lookupdev()
1727 device = pcap_lookupdev(ebuf);
1734 * Try to open the interface with the specified name.
1736 pd = open_interface(device, ndo, ebuf);
1739 * That failed. If we can get a list of
1740 * interfaces, and the interface name
1741 * is purely numeric, try to use it as
1742 * a 1-based index in the list of
1745 #ifdef HAVE_PCAP_FINDALLDEVS
1746 devnum = parse_interface_number(device);
1749 * It's not a number; just report
1750 * the open error and fail.
1756 * OK, it's a number; try to find the
1757 * interface with that index, and try
1760 * find_interface_by_number() exits if it
1761 * couldn't be found.
1763 device = find_interface_by_number(devnum);
1764 pd = open_interface(device, ndo, ebuf);
1767 #else /* HAVE_PCAP_FINDALLDEVS */
1769 * We can't get a list of interfaces; just
1773 #endif /* HAVE_PCAP_FINDALLDEVS */
1777 * Let user own process after socket has been opened.
1780 if (setgid(getgid()) != 0 || setuid(getuid()) != 0)
1781 fprintf(stderr, "Warning: setgid/setuid failed !\n");
1783 #if !defined(HAVE_PCAP_CREATE) && defined(_WIN32)
1785 if(pcap_setbuff(pd, Bflag)==-1){
1786 error("%s", pcap_geterr(pd));
1788 #endif /* !defined(HAVE_PCAP_CREATE) && defined(_WIN32) */
1790 show_dlts_and_exit(pd, device);
1791 if (yflag_dlt >= 0) {
1792 #ifdef HAVE_PCAP_SET_DATALINK
1793 if (pcap_set_datalink(pd, yflag_dlt) < 0)
1794 error("%s", pcap_geterr(pd));
1797 * We don't actually support changing the
1798 * data link type, so we only let them
1799 * set it to what it already is.
1801 if (yflag_dlt != pcap_datalink(pd)) {
1802 error("%s is not one of the DLTs supported by this device\n",
1806 (void)fprintf(stderr, "%s: data link type %s\n",
1807 program_name, yflag_dlt_name);
1808 (void)fflush(stderr);
1810 i = pcap_snapshot(pd);
1811 if (ndo->ndo_snaplen < i) {
1812 warning("snaplen raised from %d to %d", ndo->ndo_snaplen, i);
1813 ndo->ndo_snaplen = i;
1815 if(ndo->ndo_fflag != 0) {
1816 if (pcap_lookupnet(device, &localnet, &netmask, ebuf) < 0) {
1817 warning("foreign (-f) flag used but: %s", ebuf);
1823 cmdbuf = read_infile(infile);
1825 cmdbuf = copy_argv(&argv[optind]);
1827 #ifdef HAVE_PCAP_SET_OPTIMIZER_DEBUG
1828 pcap_set_optimizer_debug(dflag);
1830 if (pcap_compile(pd, &fcode, cmdbuf, Oflag, netmask) < 0)
1831 error("%s", pcap_geterr(pd));
1833 bpf_dump(&fcode, dflag);
1836 pcap_freecode(&fcode);
1841 if (!ndo->ndo_nflag)
1842 capdns = capdns_setup();
1843 #endif /* HAVE_CASPER */
1845 init_print(ndo, localnet, netmask, timezone_offset);
1848 (void)setsignal(SIGPIPE, cleanup);
1849 (void)setsignal(SIGTERM, cleanup);
1850 (void)setsignal(SIGINT, cleanup);
1852 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
1853 (void)setsignal(SIGCHLD, child_cleanup);
1855 /* Cooperate with nohup(1) */
1857 if ((oldhandler = setsignal(SIGHUP, cleanup)) != SIG_DFL)
1858 (void)setsignal(SIGHUP, oldhandler);
1863 * If a user name was specified with "-Z", attempt to switch to
1864 * that user's UID. This would probably be used with sudo,
1865 * to allow tcpdump to be run in a special restricted
1866 * account (if you just want to allow users to open capture
1867 * devices, and can't just give users that permission,
1868 * you'd make tcpdump set-UID or set-GID).
1870 * Tcpdump doesn't necessarily write only to one savefile;
1871 * the general only way to allow a -Z instance to write to
1872 * savefiles as the user under whose UID it's run, rather
1873 * than as the user specified with -Z, would thus be to switch
1874 * to the original user ID before opening a capture file and
1875 * then switch back to the -Z user ID after opening the savefile.
1876 * Switching to the -Z user ID only after opening the first
1877 * savefile doesn't handle the general case.
1880 if (getuid() == 0 || geteuid() == 0) {
1881 #ifdef HAVE_LIBCAP_NG
1882 /* Initialize capng */
1883 capng_clear(CAPNG_SELECT_BOTH);
1887 CAPNG_PERMITTED | CAPNG_EFFECTIVE,
1895 CAPNG_PERMITTED | CAPNG_EFFECTIVE,
1903 CAPNG_PERMITTED | CAPNG_EFFECTIVE,
1907 capng_apply(CAPNG_SELECT_BOTH);
1908 #endif /* HAVE_LIBCAP_NG */
1909 if (username || chroot_dir)
1910 droproot(username, chroot_dir);
1915 if (pcap_setfilter(pd, &fcode) < 0)
1916 error("%s", pcap_geterr(pd));
1917 #ifdef HAVE_CAPSICUM
1918 if (RFileName == NULL && VFileName == NULL) {
1919 static const unsigned long cmds[] = { BIOCGSTATS, BIOCROTZBUF };
1922 * The various libpcap devices use a combination of
1923 * read (bpf), ioctl (bpf, netmap), poll (netmap)
1924 * so we add the relevant access rights.
1926 cap_rights_init(&rights, CAP_IOCTL, CAP_READ, CAP_EVENT);
1927 if (cap_rights_limit(pcap_fileno(pd), &rights) < 0 &&
1929 error("unable to limit pcap descriptor");
1931 if (cap_ioctls_limit(pcap_fileno(pd), cmds,
1932 sizeof(cmds) / sizeof(cmds[0])) < 0 && errno != ENOSYS) {
1933 error("unable to limit ioctls on pcap descriptor");
1939 /* Do not exceed the default PATH_MAX for files. */
1940 dumpinfo.CurrentFileName = (char *)malloc(PATH_MAX + 1);
1942 if (dumpinfo.CurrentFileName == NULL)
1943 error("malloc of dumpinfo.CurrentFileName");
1945 /* We do not need numbering for dumpfiles if Cflag isn't set. */
1947 MakeFilename(dumpinfo.CurrentFileName, WFileName, 0, WflagChars);
1949 MakeFilename(dumpinfo.CurrentFileName, WFileName, 0, 0);
1951 p = pcap_dump_open(pd, dumpinfo.CurrentFileName);
1952 #ifdef HAVE_LIBCAP_NG
1953 /* Give up CAP_DAC_OVERRIDE capability.
1954 * Only allow it to be restored if the -C or -G flag have been
1955 * set since we may need to create more files later on.
1959 (Cflag || Gflag ? 0 : CAPNG_PERMITTED)
1963 capng_apply(CAPNG_SELECT_BOTH);
1964 #endif /* HAVE_LIBCAP_NG */
1966 error("%s", pcap_geterr(pd));
1967 #ifdef HAVE_CAPSICUM
1968 set_dumper_capsicum_rights(p);
1970 if (Cflag != 0 || Gflag != 0) {
1971 #ifdef HAVE_CAPSICUM
1972 dumpinfo.WFileName = strdup(basename(WFileName));
1973 if (dumpinfo.WFileName == NULL) {
1974 error("Unable to allocate memory for file %s",
1977 dumpinfo.dirfd = open(dirname(WFileName),
1978 O_DIRECTORY | O_RDONLY);
1979 if (dumpinfo.dirfd < 0) {
1980 error("unable to open directory %s",
1981 dirname(WFileName));
1983 cap_rights_init(&rights, CAP_CREATE, CAP_FCNTL,
1984 CAP_FTRUNCATE, CAP_LOOKUP, CAP_SEEK, CAP_WRITE);
1985 if (cap_rights_limit(dumpinfo.dirfd, &rights) < 0 &&
1987 error("unable to limit directory rights");
1989 if (cap_fcntls_limit(dumpinfo.dirfd, CAP_FCNTL_GETFL) < 0 &&
1991 error("unable to limit dump descriptor fcntls");
1993 #else /* !HAVE_CAPSICUM */
1994 dumpinfo.WFileName = WFileName;
1996 callback = dump_packet_and_trunc;
1999 pcap_userdata = (u_char *)&dumpinfo;
2001 callback = dump_packet;
2002 pcap_userdata = (u_char *)p;
2004 #ifdef HAVE_PCAP_DUMP_FLUSH
2009 dlt = pcap_datalink(pd);
2010 ndo->ndo_if_printer = get_if_printer(ndo, dlt);
2011 callback = print_packet;
2012 pcap_userdata = (u_char *)ndo;
2015 #ifdef SIGNAL_REQ_INFO
2017 * We can't get statistics when reading from a file rather
2018 * than capturing from a device.
2020 if (RFileName == NULL)
2021 (void)setsignal(SIGNAL_REQ_INFO, requestinfo);
2024 if (ndo->ndo_vflag > 0 && WFileName) {
2026 * When capturing to a file, "-v" means tcpdump should,
2027 * every 10 seconds, "v"erbosely report the number of
2030 #ifdef USE_WIN32_MM_TIMER
2031 /* call verbose_stats_dump() each 1000 +/-100msec */
2032 timer_id = timeSetEvent(1000, 100, verbose_stats_dump, 0, TIME_PERIODIC);
2033 setvbuf(stderr, NULL, _IONBF, 0);
2034 #elif defined(HAVE_ALARM)
2035 (void)setsignal(SIGALRM, verbose_stats_dump);
2040 if (RFileName == NULL) {
2042 * Live capture (if -V was specified, we set RFileName
2043 * to a file from the -V file). Print a message to
2044 * the standard error on UN*X.
2046 if (!ndo->ndo_vflag && !WFileName) {
2047 (void)fprintf(stderr,
2048 "%s: verbose output suppressed, use -v or -vv for full protocol decode\n",
2051 (void)fprintf(stderr, "%s: ", program_name);
2052 dlt = pcap_datalink(pd);
2053 dlt_name = pcap_datalink_val_to_name(dlt);
2054 if (dlt_name == NULL) {
2055 (void)fprintf(stderr, "listening on %s, link-type %u, capture size %u bytes\n",
2056 device, dlt, ndo->ndo_snaplen);
2058 (void)fprintf(stderr, "listening on %s, link-type %s (%s), capture size %u bytes\n",
2060 pcap_datalink_val_to_description(dlt), ndo->ndo_snaplen);
2062 (void)fflush(stderr);
2065 #ifdef HAVE_CAPSICUM
2066 cansandbox = (VFileName == NULL && zflag == NULL);
2068 cansandbox = (cansandbox && (ndo->ndo_nflag || capdns != NULL));
2070 cansandbox = (cansandbox && ndo->ndo_nflag);
2071 #endif /* HAVE_CASPER */
2072 cansandbox = (cansandbox && (pcap_fileno(pd) != -1 ||
2073 RFileName != NULL));
2075 if (cansandbox && cap_enter() < 0 && errno != ENOSYS)
2076 error("unable to enter the capability mode");
2077 #endif /* HAVE_CAPSICUM */
2080 status = pcap_loop(pd, cnt, callback, pcap_userdata);
2081 if (WFileName == NULL) {
2083 * We're printing packets. Flush the printed output,
2084 * so it doesn't get intermingled with error output.
2088 * We got interrupted, so perhaps we didn't
2089 * manage to finish a line we were printing.
2090 * Print an extra newline, just in case.
2094 (void)fflush(stdout);
2098 * We got interrupted. If we are reading multiple
2099 * files (via -V) set these so that we stop.
2108 (void)fprintf(stderr, "%s: pcap_loop: %s\n",
2109 program_name, pcap_geterr(pd));
2111 if (RFileName == NULL) {
2113 * We're doing a live capture. Report the capture
2119 if (VFileName != NULL) {
2120 ret = get_next_file(VFile, VFileLine);
2124 RFileName = VFileLine;
2125 pd = pcap_open_offline(RFileName, ebuf);
2128 #ifdef HAVE_CAPSICUM
2129 cap_rights_init(&rights, CAP_READ);
2130 if (cap_rights_limit(fileno(pcap_file(pd)),
2131 &rights) < 0 && errno != ENOSYS) {
2132 error("unable to limit pcap descriptor");
2135 new_dlt = pcap_datalink(pd);
2136 if (new_dlt != dlt) {
2138 * The new file has a different
2139 * link-layer header type from the
2142 if (WFileName != NULL) {
2144 * We're writing raw packets
2145 * that match the filter to
2146 * a pcap file. pcap files
2147 * don't support multiple
2148 * different link-layer
2149 * header types, so we fail
2152 error("%s: new dlt does not match original", RFileName);
2156 * We're printing the decoded packets;
2157 * switch to the new DLT.
2159 * To do that, we need to change
2160 * the printer, change the DLT name,
2161 * and recompile the filter with
2165 ndo->ndo_if_printer = get_if_printer(ndo, dlt);
2166 if (pcap_compile(pd, &fcode, cmdbuf, Oflag, netmask) < 0)
2167 error("%s", pcap_geterr(pd));
2171 * Set the filter on the new file.
2173 if (pcap_setfilter(pd, &fcode) < 0)
2174 error("%s", pcap_geterr(pd));
2177 * Report the new file.
2179 dlt_name = pcap_datalink_val_to_name(dlt);
2180 if (dlt_name == NULL) {
2181 fprintf(stderr, "reading from file %s, link-type %u\n",
2185 "reading from file %s, link-type %s (%s)\n",
2186 RFileName, dlt_name,
2187 pcap_datalink_val_to_description(dlt));
2192 while (ret != NULL);
2195 pcap_freecode(&fcode);
2196 exit_tcpdump(status == -1 ? 1 : 0);
2199 /* make a clean exit on interrupts */
2201 cleanup(int signo _U_)
2203 #ifdef USE_WIN32_MM_TIMER
2205 timeKillEvent(timer_id);
2207 #elif defined(HAVE_ALARM)
2211 #ifdef HAVE_PCAP_BREAKLOOP
2213 * We have "pcap_breakloop()"; use it, so that we do as little
2214 * as possible in the signal handler (it's probably not safe
2215 * to do anything with standard I/O streams in a signal handler -
2216 * the ANSI C standard doesn't say it is).
2221 * We don't have "pcap_breakloop()"; this isn't safe, but
2222 * it's the best we can do. Print the summary if we're
2223 * not reading from a savefile - i.e., if we're doing a
2224 * live capture - and exit.
2226 if (pd != NULL && pcap_file(pd) == NULL) {
2228 * We got interrupted, so perhaps we didn't
2229 * manage to finish a line we were printing.
2230 * Print an extra newline, just in case.
2233 (void)fflush(stdout);
2241 On windows, we do not use a fork, so we do not care less about
2242 waiting a child processes to die
2244 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
2246 child_cleanup(int signo _U_)
2250 #endif /* HAVE_FORK && HAVE_VFORK */
2253 info(register int verbose)
2255 struct pcap_stat stats;
2258 * Older versions of libpcap didn't set ps_ifdrop on some
2259 * platforms; initialize it to 0 to handle that.
2261 stats.ps_ifdrop = 0;
2262 if (pcap_stats(pd, &stats) < 0) {
2263 (void)fprintf(stderr, "pcap_stats: %s\n", pcap_geterr(pd));
2269 fprintf(stderr, "%s: ", program_name);
2271 (void)fprintf(stderr, "%u packet%s captured", packets_captured,
2272 PLURAL_SUFFIX(packets_captured));
2274 fputs(", ", stderr);
2277 (void)fprintf(stderr, "%u packet%s received by filter", stats.ps_recv,
2278 PLURAL_SUFFIX(stats.ps_recv));
2280 fputs(", ", stderr);
2283 (void)fprintf(stderr, "%u packet%s dropped by kernel", stats.ps_drop,
2284 PLURAL_SUFFIX(stats.ps_drop));
2285 if (stats.ps_ifdrop != 0) {
2287 fputs(", ", stderr);
2290 (void)fprintf(stderr, "%u packet%s dropped by interface\n",
2291 stats.ps_ifdrop, PLURAL_SUFFIX(stats.ps_ifdrop));
2297 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
2299 #define fork_subprocess() fork()
2301 #define fork_subprocess() vfork()
2304 compress_savefile(const char *filename)
2308 child = fork_subprocess();
2311 "compress_savefile: fork failed: %s\n",
2312 pcap_strerror(errno));
2316 /* Parent process. */
2322 * Set to lowest priority so that this doesn't disturb the capture.
2325 setpriority(PRIO_PROCESS, 0, NZERO - 1);
2327 setpriority(PRIO_PROCESS, 0, 19);
2329 if (execlp(zflag, zflag, filename, (char *)NULL) == -1)
2331 "compress_savefile: execlp(%s, %s) failed: %s\n",
2334 pcap_strerror(errno));
2341 #else /* HAVE_FORK && HAVE_VFORK */
2343 compress_savefile(const char *filename)
2346 "compress_savefile failed. Functionality not implemented under your system\n");
2348 #endif /* HAVE_FORK && HAVE_VFORK */
2351 dump_packet_and_trunc(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
2353 struct dump_info *dump_info;
2359 dump_info = (struct dump_info *)user;
2362 * XXX - this won't force the file to rotate on the specified time
2363 * boundary, but it will rotate on the first packet received after the
2364 * specified Gflag number of seconds. Note: if a Gflag time boundary
2365 * and a Cflag size boundary coincide, the time rotation will occur
2366 * first thereby cancelling the Cflag boundary (since the file should
2370 /* Check if it is time to rotate */
2373 /* Get the current time */
2374 if ((t = time(NULL)) == (time_t)-1) {
2375 error("dump_and_trunc_packet: can't get current_time: %s",
2376 pcap_strerror(errno));
2380 /* If the time is greater than the specified window, rotate */
2381 if (t - Gflag_time >= Gflag) {
2382 #ifdef HAVE_CAPSICUM
2387 /* Update the Gflag_time */
2389 /* Update Gflag_count */
2392 * Close the current file and open a new one.
2394 pcap_dump_close(dump_info->p);
2397 * Compress the file we just closed, if the user asked for it
2400 compress_savefile(dump_info->CurrentFileName);
2403 * Check to see if we've exceeded the Wflag (when
2406 if (Cflag == 0 && Wflag > 0 && Gflag_count >= Wflag) {
2407 (void)fprintf(stderr, "Maximum file limit reached: %d\n",
2413 if (dump_info->CurrentFileName != NULL)
2414 free(dump_info->CurrentFileName);
2415 /* Allocate space for max filename + \0. */
2416 dump_info->CurrentFileName = (char *)malloc(PATH_MAX + 1);
2417 if (dump_info->CurrentFileName == NULL)
2418 error("dump_packet_and_trunc: malloc");
2420 * Gflag was set otherwise we wouldn't be here. Reset the count
2421 * so multiple files would end with 1,2,3 in the filename.
2422 * The counting is handled with the -C flow after this.
2427 * This is always the first file in the Cflag
2429 * We also don't need numbering if Cflag is not set.
2432 MakeFilename(dump_info->CurrentFileName, dump_info->WFileName, 0,
2435 MakeFilename(dump_info->CurrentFileName, dump_info->WFileName, 0, 0);
2437 #ifdef HAVE_LIBCAP_NG
2438 capng_update(CAPNG_ADD, CAPNG_EFFECTIVE, CAP_DAC_OVERRIDE);
2439 capng_apply(CAPNG_SELECT_BOTH);
2440 #endif /* HAVE_LIBCAP_NG */
2441 #ifdef HAVE_CAPSICUM
2442 fd = openat(dump_info->dirfd,
2443 dump_info->CurrentFileName,
2444 O_CREAT | O_WRONLY | O_TRUNC, 0644);
2446 error("unable to open file %s",
2447 dump_info->CurrentFileName);
2449 fp = fdopen(fd, "w");
2451 error("unable to fdopen file %s",
2452 dump_info->CurrentFileName);
2454 dump_info->p = pcap_dump_fopen(dump_info->pd, fp);
2455 #else /* !HAVE_CAPSICUM */
2456 dump_info->p = pcap_dump_open(dump_info->pd, dump_info->CurrentFileName);
2458 #ifdef HAVE_LIBCAP_NG
2459 capng_update(CAPNG_DROP, CAPNG_EFFECTIVE, CAP_DAC_OVERRIDE);
2460 capng_apply(CAPNG_SELECT_BOTH);
2461 #endif /* HAVE_LIBCAP_NG */
2462 if (dump_info->p == NULL)
2463 error("%s", pcap_geterr(pd));
2464 #ifdef HAVE_CAPSICUM
2465 set_dumper_capsicum_rights(dump_info->p);
2471 * XXX - this won't prevent capture files from getting
2472 * larger than Cflag - the last packet written to the
2473 * file could put it over Cflag.
2476 long size = pcap_dump_ftell(dump_info->p);
2479 error("ftell fails on output file");
2481 #ifdef HAVE_CAPSICUM
2487 * Close the current file and open a new one.
2489 pcap_dump_close(dump_info->p);
2492 * Compress the file we just closed, if the user
2496 compress_savefile(dump_info->CurrentFileName);
2500 if (Cflag_count >= Wflag)
2503 if (dump_info->CurrentFileName != NULL)
2504 free(dump_info->CurrentFileName);
2505 dump_info->CurrentFileName = (char *)malloc(PATH_MAX + 1);
2506 if (dump_info->CurrentFileName == NULL)
2507 error("dump_packet_and_trunc: malloc");
2508 MakeFilename(dump_info->CurrentFileName, dump_info->WFileName, Cflag_count, WflagChars);
2509 #ifdef HAVE_LIBCAP_NG
2510 capng_update(CAPNG_ADD, CAPNG_EFFECTIVE, CAP_DAC_OVERRIDE);
2511 capng_apply(CAPNG_SELECT_BOTH);
2512 #endif /* HAVE_LIBCAP_NG */
2513 #ifdef HAVE_CAPSICUM
2514 fd = openat(dump_info->dirfd, dump_info->CurrentFileName,
2515 O_CREAT | O_WRONLY | O_TRUNC, 0644);
2517 error("unable to open file %s",
2518 dump_info->CurrentFileName);
2520 fp = fdopen(fd, "w");
2522 error("unable to fdopen file %s",
2523 dump_info->CurrentFileName);
2525 dump_info->p = pcap_dump_fopen(dump_info->pd, fp);
2526 #else /* !HAVE_CAPSICUM */
2527 dump_info->p = pcap_dump_open(dump_info->pd, dump_info->CurrentFileName);
2529 #ifdef HAVE_LIBCAP_NG
2530 capng_update(CAPNG_DROP, CAPNG_EFFECTIVE, CAP_DAC_OVERRIDE);
2531 capng_apply(CAPNG_SELECT_BOTH);
2532 #endif /* HAVE_LIBCAP_NG */
2533 if (dump_info->p == NULL)
2534 error("%s", pcap_geterr(pd));
2535 #ifdef HAVE_CAPSICUM
2536 set_dumper_capsicum_rights(dump_info->p);
2541 pcap_dump((u_char *)dump_info->p, h, sp);
2542 #ifdef HAVE_PCAP_DUMP_FLUSH
2544 pcap_dump_flush(dump_info->p);
2553 dump_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
2559 pcap_dump(user, h, sp);
2560 #ifdef HAVE_PCAP_DUMP_FLUSH
2562 pcap_dump_flush((pcap_dumper_t *)user);
2571 print_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
2577 pretty_print_packet((netdissect_options *)user, h, sp, packets_captured);
2586 * XXX - there should really be libpcap calls to get the version
2587 * number as a string (the string would be generated from #defines
2588 * at run time, so that it's not generated from string constants
2589 * in the library, as, on many UNIX systems, those constants would
2590 * be statically linked into the application executable image, and
2591 * would thus reflect the version of libpcap on the system on
2592 * which the application was *linked*, not the system on which it's
2595 * That routine should be documented, unlike the "version[]"
2596 * string, so that UNIX vendors providing their own libpcaps
2597 * don't omit it (as a couple of vendors have...).
2599 * Packet.dll should perhaps also export a routine to return the
2600 * version number of the Packet.dll code, to supply the
2601 * "Wpcap_version" information on Windows.
2603 char WDversion[]="current-git.tcpdump.org";
2604 #if !defined(HAVE_GENERATED_VERSION)
2605 char version[]="current-git.tcpdump.org";
2607 char pcap_version[]="current-git.tcpdump.org";
2608 char Wpcap_version[]="3.1";
2611 #ifdef SIGNAL_REQ_INFO
2612 RETSIGTYPE requestinfo(int signo _U_)
2622 * Called once each second in verbose mode while dumping to file
2624 #ifdef USE_WIN32_MM_TIMER
2625 void CALLBACK verbose_stats_dump (UINT timer_id _U_, UINT msg _U_, DWORD_PTR arg _U_,
2626 DWORD_PTR dw1 _U_, DWORD_PTR dw2 _U_)
2629 fprintf(stderr, "Got %u\r", packets_captured);
2631 #elif defined(HAVE_ALARM)
2632 static void verbose_stats_dump(int sig _U_)
2635 fprintf(stderr, "Got %u\r", packets_captured);
2640 USES_APPLE_DEPRECATED_API
2644 extern char version[];
2645 #ifndef HAVE_PCAP_LIB_VERSION
2646 #if defined(_WIN32) || defined(HAVE_PCAP_VERSION)
2647 extern char pcap_version[];
2648 #else /* defined(_WIN32) || defined(HAVE_PCAP_VERSION) */
2649 static char pcap_version[] = "unknown";
2650 #endif /* defined(_WIN32) || defined(HAVE_PCAP_VERSION) */
2651 #endif /* HAVE_PCAP_LIB_VERSION */
2652 const char *smi_version_string;
2654 #ifdef HAVE_PCAP_LIB_VERSION
2656 (void)fprintf(stderr, "%s version %s, based on tcpdump version %s\n", program_name, WDversion, version);
2658 (void)fprintf(stderr, "%s version %s\n", program_name, version);
2660 (void)fprintf(stderr, "%s\n",pcap_lib_version());
2661 #else /* HAVE_PCAP_LIB_VERSION */
2663 (void)fprintf(stderr, "%s version %s, based on tcpdump version %s\n", program_name, WDversion, version);
2664 (void)fprintf(stderr, "WinPcap version %s, based on libpcap version %s\n",Wpcap_version, pcap_version);
2666 (void)fprintf(stderr, "%s version %s\n", program_name, version);
2667 (void)fprintf(stderr, "libpcap version %s\n", pcap_version);
2669 #endif /* HAVE_PCAP_LIB_VERSION */
2671 #if defined(HAVE_LIBCRYPTO) && defined(SSLEAY_VERSION)
2672 (void)fprintf (stderr, "%s\n", SSLeay_version(SSLEAY_VERSION));
2675 smi_version_string = nd_smi_version_string();
2676 if (smi_version_string != NULL)
2677 (void)fprintf (stderr, "SMI-library: %s\n", smi_version_string);
2679 #if defined(__SANITIZE_ADDRESS__)
2680 (void)fprintf (stderr, "Compiled with AddressSanitizer/GCC.\n");
2681 #elif defined(__has_feature)
2682 # if __has_feature(address_sanitizer)
2683 (void)fprintf (stderr, "Compiled with AddressSanitizer/CLang.\n");
2685 #endif /* __SANITIZE_ADDRESS__ or __has_feature */
2693 (void)fprintf(stderr,
2694 "Usage: %s [-aAbd" D_FLAG "efhH" I_FLAG J_FLAG "KlLnNOpqStu" U_FLAG "vxX#]" B_FLAG_USAGE " [ -c count ]\n", program_name);
2695 (void)fprintf(stderr,
2696 "\t\t[ -C file_size ] [ -E algo:secret ] [ -F file ] [ -G seconds ]\n");
2697 (void)fprintf(stderr,
2698 "\t\t[ -i interface ]" j_FLAG_USAGE " [ -M secret ] [ --number ]\n");
2699 #ifdef HAVE_PCAP_SETDIRECTION
2700 (void)fprintf(stderr,
2701 "\t\t[ -Q in|out|inout ]\n");
2703 (void)fprintf(stderr,
2704 "\t\t[ -r file ] [ -s snaplen ] ");
2705 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
2706 (void)fprintf(stderr, "[ --time-stamp-precision precision ]\n");
2707 (void)fprintf(stderr,
2710 #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
2711 (void)fprintf(stderr, "[ --immediate-mode ] ");
2713 (void)fprintf(stderr, "[ -T type ] [ --version ] [ -V file ]\n");
2714 (void)fprintf(stderr,
2715 "\t\t[ -w file ] [ -W filecount ] [ -y datalinktype ] [ -z postrotate-command ]\n");
2716 (void)fprintf(stderr,
2717 "\t\t[ -Z user ] [ expression ]\n");
2721 * c-style: whitesmith