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>
80 #include <sys/sysctl.h>
82 #include <sys/ioccom.h>
86 #include <libcasper.h>
87 #include <casper/cap_dns.h>
88 #endif /* HAVE_CASPER */
89 #endif /* HAVE_CAPSICUM */
99 #include <sys/resource.h>
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.
109 #ifdef HAVE_LIBCAP_NG
113 #undef HAVE_LIBCAP_NG
114 #endif /* HAVE_CAP_NG_H */
115 #endif /* HAVE_LIBCAP_NG */
117 #include "netdissect.h"
118 #include "interface.h"
119 #include "addrtoname.h"
121 #include "setsignal.h"
122 #include "gmt2local.h"
123 #include "pcap-missing.h"
124 #include "ascii_strcasecmp.h"
129 #define PATH_MAX 1024
133 #define SIGNAL_REQ_INFO SIGINFO
135 #define SIGNAL_REQ_INFO SIGUSR1
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 */
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.
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.
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 */
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 */
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;
174 static int infodelay;
175 static int infoprint;
180 cap_channel_t *capdns;
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 */
190 static void warning(const char *, ...)
191 #ifdef __ATTRIBUTE___FORMAT_OK
192 __attribute__((format (printf, 1, 2)))
193 #endif /* __ATTRIBUTE___FORMAT_OK */
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));
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 *);
211 #ifdef SIGNAL_REQ_INFO
212 RETSIGTYPE requestinfo(int);
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);
223 static void info(int);
224 static u_int packets_captured;
226 #ifdef HAVE_PCAP_FINDALLDEVS
227 static const struct tok status_flags[] = {
229 { PCAP_IF_UP, "Up" },
231 #ifdef PCAP_IF_RUNNING
232 { PCAP_IF_RUNNING, "Running" },
234 { PCAP_IF_LOOPBACK, "Loopback" },
241 static int supports_monitor_mode;
249 char *CurrentFileName;
257 #if defined(HAVE_PCAP_SET_PARSER_DEBUG)
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).
265 __declspec(dllimport)
269 void pcap_set_parser_debug(int);
270 #elif defined(HAVE_PCAP_DEBUG) || defined(HAVE_YYDEBUG)
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.
277 pcap_set_parser_debug(int value)
279 #ifdef HAVE_PCAP_DEBUG
280 extern int pcap_debug;
283 #else /* HAVE_PCAP_DEBUG */
287 #endif /* HAVE_PCAP_DEBUG */
290 #define HAVE_PCAP_SET_PARSER_DEBUG
293 #if defined(HAVE_PCAP_SET_OPTIMIZER_DEBUG)
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).
301 __declspec(dllimport)
305 void pcap_set_optimizer_debug(int);
310 error(const char *fmt, ...)
314 (void)fprintf(stderr, "%s: ", program_name);
316 (void)vfprintf(stderr, fmt, ap);
321 (void)fputc('\n', stderr);
329 warning(const char *fmt, ...)
333 (void)fprintf(stderr, "%s: WARNING: ", program_name);
335 (void)vfprintf(stderr, fmt, ap);
340 (void)fputc('\n', stderr);
345 exit_tcpdump(int status)
351 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
353 show_tstamp_types_and_exit(pcap_t *pc, const char *device)
356 int *tstamp_types = 0;
357 const char *tstamp_type_name;
360 n_tstamp_types = pcap_list_tstamp_types(pc, &tstamp_types);
361 if (n_tstamp_types < 0)
362 error("%s", pcap_geterr(pc));
364 if (n_tstamp_types == 0) {
365 fprintf(stderr, "Time stamp type cannot be set for %s\n",
369 fprintf(stderr, "Time stamp types for %s (use option -j to set):\n",
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]));
377 (void) fprintf(stderr, " %d\n", tstamp_types[i]);
380 pcap_free_tstamp_types(tstamp_types);
386 show_dlts_and_exit(pcap_t *pc, const char *device)
390 const char *dlt_name;
392 n_dlts = pcap_list_datalinks(pc, &dlts);
394 error("%s", pcap_geterr(pc));
395 else if (n_dlts == 0 || !dlts)
396 error("No data link types.");
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).
406 if (supports_monitor_mode)
407 (void) fprintf(stderr, "Data link types for %s %s (use option -y to set):\n",
409 Iflag ? "when in monitor mode" : "when not in monitor mode");
411 (void) fprintf(stderr, "Data link types for %s (use option -y to set):\n",
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]));
421 * OK, does tcpdump handle that type?
423 if (!has_printer(dlts[i]))
424 (void) fprintf(stderr, " (printing not supported)");
425 fprintf(stderr, "\n");
427 (void) fprintf(stderr, " DLT %d (printing not supported)\n",
431 #ifdef HAVE_PCAP_FREE_DATALINKS
432 pcap_free_datalinks(dlts);
437 #ifdef HAVE_PCAP_FINDALLDEVS
439 show_devices_and_exit (void)
441 pcap_if_t *dev, *devlist;
442 char ebuf[PCAP_ERRBUF_SIZE];
445 if (pcap_findalldevs(&devlist, ebuf) < 0)
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);
452 printf(" [%s]", bittok2str(status_flags, "none", dev->flags));
455 pcap_freealldevs(devlist);
458 #endif /* HAVE_PCAP_FINDALLDEVS */
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
468 * OS X tcpdump uses -g to force non--v output for IP to be on one
469 * line, making it more "g"repable;
471 * OS X tcpdump uses -k to specify that packet comments in pcap-ng files
474 * OpenBSD tcpdump uses -o to indicate that OS fingerprinting should be done
475 * for hosts sending TCP SYN packets;
477 * OS X tcpdump uses -P to indicate that -w should write pcap-ng rather
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.
491 * Set up flags that might or might not be supported depending on the
492 * version of libpcap we're using.
494 #if defined(HAVE_PCAP_CREATE) || defined(_WIN32)
496 #define B_FLAG_USAGE " [ -B size ]"
497 #else /* defined(HAVE_PCAP_CREATE) || defined(_WIN32) */
500 #endif /* defined(HAVE_PCAP_CREATE) || defined(_WIN32) */
502 #ifdef HAVE_PCAP_CREATE
504 #else /* HAVE_PCAP_CREATE */
506 #endif /* HAVE_PCAP_CREATE */
508 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
510 #define j_FLAG_USAGE " [ -j tstamptype ]"
512 #else /* PCAP_ERROR_TSTAMP_TYPE_NOTSUP */
516 #endif /* PCAP_ERROR_TSTAMP_TYPE_NOTSUP */
518 #ifdef HAVE_PCAP_FINDALLDEVS
524 #ifdef HAVE_PCAP_DUMP_FLUSH
530 #ifdef HAVE_PCAP_SETDIRECTION
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:#"
541 * We do not currently have long options corresponding to all short
542 * options; we should probably pick appropriate option names for them.
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
548 * tcpdump --verbose --verbose
550 * doesn't make sense; it should be --verbosity={N} or something such
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.
558 #define OPTION_VERSION 128
559 #define OPTION_TSTAMP_PRECISION 129
560 #define OPTION_IMMEDIATE_MODE 130
562 static const struct option longopts[] = {
563 #if defined(HAVE_PCAP_CREATE) || defined(_WIN32)
564 { "buffer-size", required_argument, NULL, 'B' },
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' },
572 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
573 { "time-stamp-type", required_argument, NULL, 'j' },
574 { "list-time-stamp-types", no_argument, NULL, 'J' },
576 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
577 { "time-stamp-precision", required_argument, NULL, OPTION_TSTAMP_PRECISION},
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' },
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' },
591 { "linktype", required_argument, NULL, 'y' },
592 #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
593 { "immediate-mode", no_argument, NULL, OPTION_IMMEDIATE_MODE },
595 #ifdef HAVE_PCAP_SET_PARSER_DEBUG
596 { "debug-filter-parser", no_argument, NULL, 'Y' },
598 { "relinquish-privileges", required_argument, NULL, 'Z' },
599 { "number", no_argument, NULL, '#' },
600 { "version", no_argument, NULL, OPTION_VERSION },
605 /* Drop root privileges and chroot if necessary */
607 droproot(const char *username, const char *chroot_dir)
609 struct passwd *pw = NULL;
611 if (chroot_dir && !username) {
612 fprintf(stderr, "%s: Chroot without dropping root is insecure\n",
617 pw = getpwnam(username);
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));
626 #ifdef HAVE_LIBCAP_NG
628 int ret = capng_change_id(pw->pw_uid, pw->pw_gid, CAPNG_NO_FLAG);
630 fprintf(stderr, "error : ret %d\n", ret);
632 fprintf(stderr, "dropped privs to %s\n", username);
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));
646 fprintf(stderr, "dropped privs to %s\n", username);
648 #endif /* HAVE_LIBCAP_NG */
651 fprintf(stderr, "%s: Couldn't find user '%.32s'\n",
652 program_name, username);
655 #ifdef HAVE_LIBCAP_NG
656 /* We don't need CAP_SETUID, CAP_SETGID and CAP_SYS_CHROOT any more. */
659 CAPNG_EFFECTIVE | CAPNG_PERMITTED,
664 capng_apply(CAPNG_SELECT_BOTH);
665 #endif /* HAVE_LIBCAP_NG */
686 MakeFilename(char *buffer, char *orig_name, int cnt, int max_chars)
688 char *filename = malloc(PATH_MAX + 1);
689 if (filename == NULL)
690 error("Makefilename: malloc");
692 /* Process with strftime if Gflag is set. */
696 /* Convert Gflag_time to a usable format */
697 if ((local_tm = localtime(&Gflag_time)) == NULL) {
698 error("MakeTimedFilename: localtime");
701 /* There's no good way to detect an error in strftime since a return
702 * value of 0 isn't necessarily failure.
704 strftime(filename, PATH_MAX, orig_name, local_tm);
706 strncpy(filename, orig_name, PATH_MAX);
709 if (cnt == 0 && max_chars == 0)
710 strncpy(buffer, filename, PATH_MAX + 1);
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);
719 get_next_file(FILE *VFile, char *ptr)
723 ret = fgets(ptr, PATH_MAX, VFile);
727 if (ptr[strlen(ptr) - 1] == '\n')
728 ptr[strlen(ptr) - 1] = '\0';
734 static cap_channel_t *
737 cap_channel_t *capcas, *capdnsloc;
738 const char *types[1];
743 error("unable to create casper process");
744 capdnsloc = cap_service_open(capcas, "system.dns");
745 /* Casper capability no longer needed. */
747 if (capdnsloc == NULL)
748 error("unable to open system.dns service");
749 /* Limit system.dns to reverse DNS lookups. */
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");
760 #endif /* HAVE_CASPER */
762 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
764 tstamp_precision_from_string(const char *precision)
766 if (strncmp(precision, "nano", strlen("nano")) == 0)
767 return PCAP_TSTAMP_PRECISION_NANO;
769 if (strncmp(precision, "micro", strlen("micro")) == 0)
770 return PCAP_TSTAMP_PRECISION_MICRO;
776 tstamp_precision_to_string(int precision)
780 case PCAP_TSTAMP_PRECISION_MICRO:
783 case PCAP_TSTAMP_PRECISION_NANO:
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.
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.
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.
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
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
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.
831 * Perhaps this is the universe's way of saying that either
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
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.
847 set_dumper_capsicum_rights(pcap_dumper_t *p)
849 int fd = fileno(pcap_dump_file(p));
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");
856 if (cap_fcntls_limit(fd, CAP_FCNTL_GETFL) < 0 && errno != ENOSYS) {
857 error("unable to limit dump descriptor fcntls");
863 * Copy arg vector into a new buffer, concatenating arguments with spaces.
866 copy_argv(register char **argv)
869 register u_int len = 0;
878 len += strlen(*p++) + 1;
880 buf = (char *)malloc(len);
882 error("copy_argv: malloc");
886 while ((src = *p++) != NULL) {
887 while ((*dst++ = *src++) != '\0')
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.
907 read_infile(char *fname)
909 register int i, fd, cc;
913 fd = open(fname, O_RDONLY|O_BINARY);
915 error("can't open %s: %s", fname, pcap_strerror(errno));
917 if (fstat(fd, &buf) < 0)
918 error("can't stat %s: %s", fname, pcap_strerror(errno));
920 cp = malloc((u_int)buf.st_size + 1);
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);
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);
931 /* replace "# comment" with spaces */
932 for (i = 0; i < cc; i++) {
934 while (i < cc && cp[i] != '\n')
941 #ifdef HAVE_PCAP_FINDALLDEVS
943 parse_interface_number(const char *device)
948 devnum = strtol(device, &end, 10);
949 if (device != end && *end == '\0') {
951 * It's all-numeric, but is it a valid number?
955 * No, it's not an ordinal.
957 error("Invalid adapter index");
962 * It's not all-numeric; return -1, so our caller
970 find_interface_by_number(long devnum)
972 pcap_if_t *dev, *devlist;
974 char ebuf[PCAP_ERRBUF_SIZE];
977 if (pcap_findalldevs(&devlist, ebuf) < 0)
980 * Look for the devnum-th entry in the list of devices (1-based).
982 for (i = 0, dev = devlist; i < devnum-1 && dev != NULL;
983 i++, dev = dev->next)
986 error("Invalid adapter index");
987 device = strdup(dev->name);
988 pcap_freealldevs(devlist);
994 open_interface(const char *device, netdissect_options *ndo, char *ebuf)
997 #ifdef HAVE_PCAP_CREATE
1002 #ifdef HAVE_PCAP_CREATE
1003 pc = pcap_create(device, ebuf);
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.
1011 if (strstr(ebuf, "No such device") != NULL)
1015 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
1017 show_tstamp_types_and_exit(pc, device);
1019 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
1020 status = pcap_set_tstamp_precision(pc, ndo->ndo_tstamp_precision);
1022 error("%s: Can't set %ssecond time stamp precision: %s",
1024 tstamp_precision_to_string(ndo->ndo_tstamp_precision),
1025 pcap_statustostr(status));
1028 #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
1029 if (immediate_mode) {
1030 status = pcap_set_immediate_mode(pc, 1);
1032 error("%s: Can't set immediate mode: %s",
1034 pcap_statustostr(status));
1038 * Is this an interface that supports monitor mode?
1040 if (pcap_can_set_rfmon(pc) == 1)
1041 supports_monitor_mode = 1;
1043 supports_monitor_mode = 0;
1044 status = pcap_set_snaplen(pc, ndo->ndo_snaplen);
1046 error("%s: Can't set snapshot length: %s",
1047 device, pcap_statustostr(status));
1048 status = pcap_set_promisc(pc, !pflag);
1050 error("%s: Can't set promiscuous mode: %s",
1051 device, pcap_statustostr(status));
1053 status = pcap_set_rfmon(pc, 1);
1055 error("%s: Can't set monitor mode: %s",
1056 device, pcap_statustostr(status));
1058 status = pcap_set_timeout(pc, 1000);
1060 error("%s: pcap_set_timeout failed: %s",
1061 device, pcap_statustostr(status));
1063 status = pcap_set_buffer_size(pc, Bflag);
1065 error("%s: Can't set buffer size: %s",
1066 device, pcap_statustostr(status));
1068 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
1070 status = pcap_set_tstamp_type(pc, jflag);
1072 error("%s: Can't set time stamp type: %s",
1073 device, pcap_statustostr(status));
1076 status = pcap_activate(pc);
1079 * pcap_activate() failed.
1081 cp = pcap_geterr(pc);
1082 if (status == PCAP_ERROR)
1084 else if (status == PCAP_ERROR_NO_SUCH_DEVICE) {
1086 * Return an error for our caller to handle.
1088 snprintf(ebuf, PCAP_ERRBUF_SIZE, "%s: %s\n(%s)",
1089 device, pcap_statustostr(status), cp);
1092 } else if (status == PCAP_ERROR_PERM_DENIED && *cp != '\0')
1093 error("%s: %s\n(%s)", device,
1094 pcap_statustostr(status), cp);
1096 else if (status == PCAP_ERROR_RFMON_NOTSUP &&
1097 strncmp(device, "wlan", 4) == 0) {
1098 char parent[8], newdev[8];
1100 size_t s = sizeof(parent);
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);
1116 error("%s: %s", device,
1117 pcap_statustostr(status));
1118 } else if (status > 0) {
1120 * pcap_activate() succeeded, but it's warning us
1121 * of a problem it had.
1123 cp = pcap_geterr(pc);
1124 if (status == PCAP_WARNING)
1126 else if (status == PCAP_WARNING_PROMISC_NOTSUP &&
1128 warning("%s: %s\n(%s)", device,
1129 pcap_statustostr(status), cp);
1131 warning("%s: %s", device,
1132 pcap_statustostr(status));
1134 #ifdef HAVE_PCAP_SETDIRECTION
1136 status = pcap_setdirection(pc, Qflag);
1138 error("%s: pcap_setdirection() failed: %s",
1139 device, pcap_geterr(pc));
1141 #endif /* HAVE_PCAP_SETDIRECTION */
1142 #else /* HAVE_PCAP_CREATE */
1144 pc = pcap_open_live(device, ndo->ndo_snaplen, !pflag, 1000, ebuf);
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.
1152 if (strstr(ebuf, "No such device") != NULL)
1157 warning("%s", ebuf);
1158 #endif /* HAVE_PCAP_CREATE */
1164 main(int argc, char **argv)
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;
1172 const char *dlt_name;
1173 struct bpf_program fcode;
1175 RETSIGTYPE (*oldhandler)(int);
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;
1185 #ifdef HAVE_PCAP_FINDALLDEVS
1191 #ifdef HAVE_CAPSICUM
1192 cap_rights_t rights;
1194 #endif /* HAVE_CAPSICUM */
1195 int Oflag = 1; /* run filter code optimizer */
1197 const char *yflag_dlt_name = NULL;
1199 netdissect_options Ndo;
1200 netdissect_options *ndo = &Ndo;
1203 * Initialize the netdissect code.
1205 if (nd_init(ebuf, sizeof ebuf) == -1)
1208 memset(ndo, 0, sizeof(*ndo));
1209 ndo_set_function_pointers(ndo);
1210 ndo->ndo_snaplen = DEFAULT_SNAPLEN;
1220 if ((cp = strrchr(argv[0], '/')) != NULL)
1221 ndo->program_name = program_name = cp + 1;
1223 ndo->program_name = program_name = argv[0];
1226 if (pcap_wsockinit() != 0)
1227 error("Attempting to initialize Winsock failed");
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.
1237 if (abort_on_misalignment(ebuf, sizeof(ebuf)) < 0)
1241 (op = getopt_long(argc, argv, SHORTOPTS, longopts, NULL)) != -1)
1245 /* compatibility for old -a */
1256 #if defined(HAVE_PCAP_CREATE) || defined(_WIN32)
1258 Bflag = atoi(optarg)*1024;
1260 error("invalid packet buffer size %s", optarg);
1262 #endif /* defined(HAVE_PCAP_CREATE) || defined(_WIN32) */
1267 error("invalid packet count %s", optarg);
1271 Cflag = atoi(optarg) * 1000000;
1273 error("invalid file size %s", optarg);
1293 #ifndef HAVE_LIBCRYPTO
1294 warning("crypto code not compiled in");
1296 ndo->ndo_espsecret = optarg;
1308 Gflag = atoi(optarg);
1310 error("invalid number of seconds %s", optarg);
1312 /* We will create one file initially. */
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));
1335 #ifdef HAVE_PCAP_CREATE
1339 #endif /* HAVE_PCAP_CREATE */
1341 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
1343 jflag = pcap_tstamp_type_name_to_val(optarg);
1345 error("invalid time stamp type %s", optarg);
1356 * _IOLBF is the same as _IOFBF in Microsoft's C
1357 * libraries; the only alternative they offer
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.
1364 setvbuf(stdout, NULL, _IONBF, 0);
1366 #ifdef HAVE_SETLINEBUF
1369 setvbuf(stdout, NULL, _IOLBF, 0);
1379 if (nd_have_smi_support()) {
1380 if (nd_load_smi_module(optarg, ebuf, sizeof ebuf) == -1)
1383 (void)fprintf(stderr, "%s: ignoring option `-m %s' ",
1384 program_name, optarg);
1385 (void)fprintf(stderr, "(no libsmi support)\n");
1390 /* TCP-MD5 shared secret */
1391 #ifndef HAVE_LIBCRYPTO
1392 warning("crypto code not compiled in");
1394 ndo->ndo_sigsecret = optarg;
1415 ++ndo->ndo_suppress_default_print;
1418 #ifdef HAVE_PCAP_SETDIRECTION
1420 if (ascii_strcasecmp(optarg, "in") == 0)
1422 else if (ascii_strcasecmp(optarg, "out") == 0)
1424 else if (ascii_strcasecmp(optarg, "inout") == 0)
1425 Qflag = PCAP_D_INOUT;
1427 error("unknown capture direction `%s'", optarg);
1429 #endif /* HAVE_PCAP_SETDIRECTION */
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;
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;
1488 error("unknown packet type `%s'", optarg);
1495 #ifdef HAVE_PCAP_DUMP_FLUSH
1514 Wflag = atoi(optarg);
1516 error("invalid number of output files %s", optarg);
1517 WflagChars = getWflagChars(Wflag);
1522 ++ndo->ndo_suppress_default_print;
1527 ++ndo->ndo_suppress_default_print;
1531 yflag_dlt_name = optarg;
1533 pcap_datalink_name_to_val(yflag_dlt_name);
1535 error("invalid data link type %s", yflag_dlt_name);
1538 #ifdef HAVE_PCAP_SET_PARSER_DEBUG
1541 /* Undocumented flag */
1542 pcap_set_parser_debug(1);
1555 ndo->ndo_packet_number = 1;
1558 case OPTION_VERSION:
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");
1571 #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
1572 case OPTION_IMMEDIATE_MODE:
1583 #ifdef HAVE_PCAP_FINDALLDEVS
1585 show_devices_and_exit();
1588 switch (ndo->ndo_tflag) {
1590 case 0: /* Default */
1591 case 4: /* Default + Date*/
1592 timezone_offset = gmt2local(0);
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 */
1601 default: /* Not supported */
1602 error("only -t, -tt, -ttt, -tttt and -ttttt are supported");
1606 if (ndo->ndo_fflag != 0 && (VFileName != NULL || RFileName != NULL))
1607 error("-f can not be used with -V or -r");
1609 if (VFileName != NULL && RFileName != NULL)
1610 error("-V and -r are mutually exclusive.");
1612 #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
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.
1619 if (WFileName == NULL && isatty(1))
1624 /* if run as root, prepare for chrooting */
1625 if (getuid() == 0 || geteuid() == 0) {
1626 /* future extensibility for cmd-line arguments */
1628 chroot_dir = WITH_CHROOT;
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 */
1637 username = WITH_USER;
1641 if (RFileName != NULL || VFileName != NULL) {
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.
1648 * In either case, we're reading a savefile, not doing
1653 * We don't need network access, so relinquish any set-UID
1654 * or set-GID privileges we have (if any).
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
1661 if (setgid(getgid()) != 0 || setuid(getuid()) != 0 )
1662 fprintf(stderr, "Warning: setgid/setuid failed !\n");
1664 if (VFileName != NULL) {
1665 if (VFileName[0] == '-' && VFileName[1] == '\0')
1668 VFile = fopen(VFileName, "r");
1671 error("Unable to open file: %s\n", pcap_strerror(errno));
1673 ret = get_next_file(VFile, VFileLine);
1675 error("Nothing in %s\n", VFileName);
1676 RFileName = VFileLine;
1679 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
1680 pd = pcap_open_offline_with_tstamp_precision(RFileName,
1681 ndo->ndo_tstamp_precision, ebuf);
1683 pd = pcap_open_offline(RFileName, ebuf);
1688 #ifdef HAVE_CAPSICUM
1689 cap_rights_init(&rights, CAP_READ);
1690 if (cap_rights_limit(fileno(pcap_file(pd)), &rights) < 0 &&
1692 error("unable to limit pcap descriptor");
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",
1702 "reading from file %s, link-type %s (%s)\n",
1703 RFileName, dlt_name,
1704 pcap_datalink_val_to_description(dlt));
1708 * We're doing a live capture.
1710 if (device == NULL) {
1712 * No interface was specified. Pick one.
1714 #ifdef HAVE_PCAP_FINDALLDEVS
1716 * Find the list of interfaces, and pick
1717 * the first interface.
1719 if (pcap_findalldevs(&devlist, ebuf) >= 0 &&
1721 device = strdup(devlist->name);
1722 pcap_freealldevs(devlist);
1724 #else /* HAVE_PCAP_FINDALLDEVS */
1726 * Use whatever interface pcap_lookupdev()
1729 device = pcap_lookupdev(ebuf);
1736 * Try to open the interface with the specified name.
1738 pd = open_interface(device, ndo, ebuf);
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
1747 #ifdef HAVE_PCAP_FINDALLDEVS
1748 devnum = parse_interface_number(device);
1751 * It's not a number; just report
1752 * the open error and fail.
1758 * OK, it's a number; try to find the
1759 * interface with that index, and try
1762 * find_interface_by_number() exits if it
1763 * couldn't be found.
1765 device = find_interface_by_number(devnum);
1766 pd = open_interface(device, ndo, ebuf);
1769 #else /* HAVE_PCAP_FINDALLDEVS */
1771 * We can't get a list of interfaces; just
1775 #endif /* HAVE_PCAP_FINDALLDEVS */
1779 * Let user own process after socket has been opened.
1782 if (setgid(getgid()) != 0 || setuid(getuid()) != 0)
1783 fprintf(stderr, "Warning: setgid/setuid failed !\n");
1785 #if !defined(HAVE_PCAP_CREATE) && defined(_WIN32)
1787 if(pcap_setbuff(pd, Bflag)==-1){
1788 error("%s", pcap_geterr(pd));
1790 #endif /* !defined(HAVE_PCAP_CREATE) && defined(_WIN32) */
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));
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.
1803 if (yflag_dlt != pcap_datalink(pd)) {
1804 error("%s is not one of the DLTs supported by this device\n",
1808 (void)fprintf(stderr, "%s: data link type %s\n",
1809 program_name, yflag_dlt_name);
1810 (void)fflush(stderr);
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;
1817 if(ndo->ndo_fflag != 0) {
1818 if (pcap_lookupnet(device, &localnet, &netmask, ebuf) < 0) {
1819 warning("foreign (-f) flag used but: %s", ebuf);
1825 cmdbuf = read_infile(infile);
1827 cmdbuf = copy_argv(&argv[optind]);
1829 #ifdef HAVE_PCAP_SET_OPTIMIZER_DEBUG
1830 pcap_set_optimizer_debug(dflag);
1832 if (pcap_compile(pd, &fcode, cmdbuf, Oflag, netmask) < 0)
1833 error("%s", pcap_geterr(pd));
1835 bpf_dump(&fcode, dflag);
1838 pcap_freecode(&fcode);
1843 if (!ndo->ndo_nflag)
1844 capdns = capdns_setup();
1845 #endif /* HAVE_CASPER */
1847 init_print(ndo, localnet, netmask, timezone_offset);
1850 (void)setsignal(SIGPIPE, cleanup);
1851 (void)setsignal(SIGTERM, cleanup);
1852 (void)setsignal(SIGINT, cleanup);
1854 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
1855 (void)setsignal(SIGCHLD, child_cleanup);
1857 /* Cooperate with nohup(1) */
1859 if ((oldhandler = setsignal(SIGHUP, cleanup)) != SIG_DFL)
1860 (void)setsignal(SIGHUP, oldhandler);
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).
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.
1882 if (getuid() == 0 || geteuid() == 0) {
1883 #ifdef HAVE_LIBCAP_NG
1884 /* Initialize capng */
1885 capng_clear(CAPNG_SELECT_BOTH);
1889 CAPNG_PERMITTED | CAPNG_EFFECTIVE,
1897 CAPNG_PERMITTED | CAPNG_EFFECTIVE,
1905 CAPNG_PERMITTED | CAPNG_EFFECTIVE,
1909 capng_apply(CAPNG_SELECT_BOTH);
1910 #endif /* HAVE_LIBCAP_NG */
1911 if (username || chroot_dir)
1912 droproot(username, chroot_dir);
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 };
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.
1928 cap_rights_init(&rights, CAP_EVENT, CAP_IOCTL, CAP_READ);
1929 if (cap_rights_limit(pcap_fileno(pd), &rights) < 0 &&
1931 error("unable to limit pcap descriptor");
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");
1941 /* Do not exceed the default PATH_MAX for files. */
1942 dumpinfo.CurrentFileName = (char *)malloc(PATH_MAX + 1);
1944 if (dumpinfo.CurrentFileName == NULL)
1945 error("malloc of dumpinfo.CurrentFileName");
1947 /* We do not need numbering for dumpfiles if Cflag isn't set. */
1949 MakeFilename(dumpinfo.CurrentFileName, WFileName, 0, WflagChars);
1951 MakeFilename(dumpinfo.CurrentFileName, WFileName, 0, 0);
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.
1961 (Cflag || Gflag ? 0 : CAPNG_PERMITTED)
1965 capng_apply(CAPNG_SELECT_BOTH);
1966 #endif /* HAVE_LIBCAP_NG */
1968 error("%s", pcap_geterr(pd));
1969 #ifdef HAVE_CAPSICUM
1970 set_dumper_capsicum_rights(p);
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",
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));
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 &&
1989 error("unable to limit directory rights");
1991 if (cap_fcntls_limit(dumpinfo.dirfd, CAP_FCNTL_GETFL) < 0 &&
1993 error("unable to limit dump descriptor fcntls");
1995 #else /* !HAVE_CAPSICUM */
1996 dumpinfo.WFileName = WFileName;
1998 callback = dump_packet_and_trunc;
2001 pcap_userdata = (u_char *)&dumpinfo;
2003 callback = dump_packet;
2004 pcap_userdata = (u_char *)p;
2006 #ifdef HAVE_PCAP_DUMP_FLUSH
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;
2017 #ifdef SIGNAL_REQ_INFO
2019 * We can't get statistics when reading from a file rather
2020 * than capturing from a device.
2022 if (RFileName == NULL)
2023 (void)setsignal(SIGNAL_REQ_INFO, requestinfo);
2026 if (ndo->ndo_vflag > 0 && WFileName) {
2028 * When capturing to a file, "-v" means tcpdump should,
2029 * every 10 seconds, "v"erbosely report the number of
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);
2042 if (RFileName == NULL) {
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.
2048 if (!ndo->ndo_vflag && !WFileName) {
2049 (void)fprintf(stderr,
2050 "%s: verbose output suppressed, use -v or -vv for full protocol decode\n",
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);
2060 (void)fprintf(stderr, "listening on %s, link-type %s (%s), capture size %u bytes\n",
2062 pcap_datalink_val_to_description(dlt), ndo->ndo_snaplen);
2064 (void)fflush(stderr);
2067 #ifdef HAVE_CAPSICUM
2068 cansandbox = (VFileName == NULL && zflag == NULL);
2070 cansandbox = (cansandbox && (ndo->ndo_nflag || capdns != NULL));
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 */
2079 status = pcap_loop(pd, cnt, callback, pcap_userdata);
2080 if (WFileName == NULL) {
2082 * We're printing packets. Flush the printed output,
2083 * so it doesn't get intermingled with error output.
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.
2093 (void)fflush(stdout);
2097 * We got interrupted. If we are reading multiple
2098 * files (via -V) set these so that we stop.
2107 (void)fprintf(stderr, "%s: pcap_loop: %s\n",
2108 program_name, pcap_geterr(pd));
2110 if (RFileName == NULL) {
2112 * We're doing a live capture. Report the capture
2118 if (VFileName != NULL) {
2119 ret = get_next_file(VFile, VFileLine);
2123 RFileName = VFileLine;
2124 pd = pcap_open_offline(RFileName, 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");
2134 new_dlt = pcap_datalink(pd);
2135 if (new_dlt != dlt) {
2137 * The new file has a different
2138 * link-layer header type from the
2141 if (WFileName != NULL) {
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
2151 error("%s: new dlt does not match original", RFileName);
2155 * We're printing the decoded packets;
2156 * switch to the new DLT.
2158 * To do that, we need to change
2159 * the printer, change the DLT name,
2160 * and recompile the filter with
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));
2170 * Set the filter on the new file.
2172 if (pcap_setfilter(pd, &fcode) < 0)
2173 error("%s", pcap_geterr(pd));
2176 * Report the new file.
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",
2184 "reading from file %s, link-type %s (%s)\n",
2185 RFileName, dlt_name,
2186 pcap_datalink_val_to_description(dlt));
2191 while (ret != NULL);
2194 pcap_freecode(&fcode);
2195 exit_tcpdump(status == -1 ? 1 : 0);
2198 /* make a clean exit on interrupts */
2200 cleanup(int signo _U_)
2202 #ifdef USE_WIN32_MM_TIMER
2204 timeKillEvent(timer_id);
2206 #elif defined(HAVE_ALARM)
2210 #ifdef HAVE_PCAP_BREAKLOOP
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).
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.
2225 if (pd != NULL && pcap_file(pd) == NULL) {
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.
2232 (void)fflush(stdout);
2240 On windows, we do not use a fork, so we do not care less about
2241 waiting a child processes to die
2243 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
2245 child_cleanup(int signo _U_)
2249 #endif /* HAVE_FORK && HAVE_VFORK */
2252 info(register int verbose)
2254 struct pcap_stat stats;
2257 * Older versions of libpcap didn't set ps_ifdrop on some
2258 * platforms; initialize it to 0 to handle that.
2260 stats.ps_ifdrop = 0;
2261 if (pcap_stats(pd, &stats) < 0) {
2262 (void)fprintf(stderr, "pcap_stats: %s\n", pcap_geterr(pd));
2268 fprintf(stderr, "%s: ", program_name);
2270 (void)fprintf(stderr, "%u packet%s captured", packets_captured,
2271 PLURAL_SUFFIX(packets_captured));
2273 fputs(", ", stderr);
2276 (void)fprintf(stderr, "%u packet%s received by filter", stats.ps_recv,
2277 PLURAL_SUFFIX(stats.ps_recv));
2279 fputs(", ", 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) {
2286 fputs(", ", stderr);
2289 (void)fprintf(stderr, "%u packet%s dropped by interface\n",
2290 stats.ps_ifdrop, PLURAL_SUFFIX(stats.ps_ifdrop));
2296 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
2298 #define fork_subprocess() fork()
2300 #define fork_subprocess() vfork()
2303 compress_savefile(const char *filename)
2307 child = fork_subprocess();
2310 "compress_savefile: fork failed: %s\n",
2311 pcap_strerror(errno));
2315 /* Parent process. */
2321 * Set to lowest priority so that this doesn't disturb the capture.
2324 setpriority(PRIO_PROCESS, 0, NZERO - 1);
2326 setpriority(PRIO_PROCESS, 0, 19);
2328 if (execlp(zflag, zflag, filename, (char *)NULL) == -1)
2330 "compress_savefile: execlp(%s, %s) failed: %s\n",
2333 pcap_strerror(errno));
2340 #else /* HAVE_FORK && HAVE_VFORK */
2342 compress_savefile(const char *filename)
2345 "compress_savefile failed. Functionality not implemented under your system\n");
2347 #endif /* HAVE_FORK && HAVE_VFORK */
2350 dump_packet_and_trunc(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
2352 struct dump_info *dump_info;
2358 dump_info = (struct dump_info *)user;
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
2369 /* Check if it is time to rotate */
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));
2379 /* If the time is greater than the specified window, rotate */
2380 if (t - Gflag_time >= Gflag) {
2381 #ifdef HAVE_CAPSICUM
2386 /* Update the Gflag_time */
2388 /* Update Gflag_count */
2391 * Close the current file and open a new one.
2393 pcap_dump_close(dump_info->p);
2396 * Compress the file we just closed, if the user asked for it
2399 compress_savefile(dump_info->CurrentFileName);
2402 * Check to see if we've exceeded the Wflag (when
2405 if (Cflag == 0 && Wflag > 0 && Gflag_count >= Wflag) {
2406 (void)fprintf(stderr, "Maximum file limit reached: %d\n",
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");
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.
2426 * This is always the first file in the Cflag
2428 * We also don't need numbering if Cflag is not set.
2431 MakeFilename(dump_info->CurrentFileName, dump_info->WFileName, 0,
2434 MakeFilename(dump_info->CurrentFileName, dump_info->WFileName, 0, 0);
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);
2445 error("unable to open file %s",
2446 dump_info->CurrentFileName);
2448 fp = fdopen(fd, "w");
2450 error("unable to fdopen file %s",
2451 dump_info->CurrentFileName);
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);
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);
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.
2475 long size = pcap_dump_ftell(dump_info->p);
2478 error("ftell fails on output file");
2480 #ifdef HAVE_CAPSICUM
2486 * Close the current file and open a new one.
2488 pcap_dump_close(dump_info->p);
2491 * Compress the file we just closed, if the user
2495 compress_savefile(dump_info->CurrentFileName);
2499 if (Cflag_count >= Wflag)
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);
2516 error("unable to open file %s",
2517 dump_info->CurrentFileName);
2519 fp = fdopen(fd, "w");
2521 error("unable to fdopen file %s",
2522 dump_info->CurrentFileName);
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);
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);
2540 pcap_dump((u_char *)dump_info->p, h, sp);
2541 #ifdef HAVE_PCAP_DUMP_FLUSH
2543 pcap_dump_flush(dump_info->p);
2552 dump_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
2558 pcap_dump(user, h, sp);
2559 #ifdef HAVE_PCAP_DUMP_FLUSH
2561 pcap_dump_flush((pcap_dumper_t *)user);
2570 print_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
2576 pretty_print_packet((netdissect_options *)user, h, sp, packets_captured);
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
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...).
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.
2602 char WDversion[]="current-git.tcpdump.org";
2603 #if !defined(HAVE_GENERATED_VERSION)
2604 char version[]="current-git.tcpdump.org";
2606 char pcap_version[]="current-git.tcpdump.org";
2607 char Wpcap_version[]="3.1";
2610 #ifdef SIGNAL_REQ_INFO
2611 RETSIGTYPE requestinfo(int signo _U_)
2621 * Called once each second in verbose mode while dumping to file
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_)
2628 fprintf(stderr, "Got %u\r", packets_captured);
2630 #elif defined(HAVE_ALARM)
2631 static void verbose_stats_dump(int sig _U_)
2634 fprintf(stderr, "Got %u\r", packets_captured);
2639 USES_APPLE_DEPRECATED_API
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;
2653 #ifdef HAVE_PCAP_LIB_VERSION
2655 (void)fprintf(stderr, "%s version %s, based on tcpdump version %s\n", program_name, WDversion, version);
2657 (void)fprintf(stderr, "%s version %s\n", program_name, version);
2659 (void)fprintf(stderr, "%s\n",pcap_lib_version());
2660 #else /* HAVE_PCAP_LIB_VERSION */
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);
2665 (void)fprintf(stderr, "%s version %s\n", program_name, version);
2666 (void)fprintf(stderr, "libpcap version %s\n", pcap_version);
2668 #endif /* HAVE_PCAP_LIB_VERSION */
2670 #if defined(HAVE_LIBCRYPTO) && defined(SSLEAY_VERSION)
2671 (void)fprintf (stderr, "%s\n", SSLeay_version(SSLEAY_VERSION));
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);
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");
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,
2701 #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
2702 (void)fprintf(stderr, "[ --immediate-mode ] ");
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");
2712 * c-style: whitesmith