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/capability.h>
80 #include <sys/ioccom.h>
83 #endif /* HAVE_CAPSICUM */
93 #include <sys/resource.h>
98 /* capabilities convenience library */
99 /* If a code depends on HAVE_LIBCAP_NG, it depends also on HAVE_CAP_NG_H.
100 * If HAVE_CAP_NG_H is not defined, undefine HAVE_LIBCAP_NG.
101 * Thus, the later tests are done only on HAVE_LIBCAP_NG.
103 #ifdef HAVE_LIBCAP_NG
107 #undef HAVE_LIBCAP_NG
108 #endif /* HAVE_CAP_NG_H */
109 #endif /* HAVE_LIBCAP_NG */
111 #include "netdissect.h"
112 #include "interface.h"
113 #include "addrtoname.h"
115 #include "setsignal.h"
116 #include "gmt2local.h"
117 #include "pcap-missing.h"
118 #include "ascii_strcasecmp.h"
123 #define PATH_MAX 1024
127 #define SIGNAL_REQ_INFO SIGINFO
129 #define SIGNAL_REQ_INFO SIGUSR1
132 static int Bflag
; /* buffer size */
133 static long Cflag
; /* rotate dump files after this many bytes */
134 static int Cflag_count
; /* Keep track of which file number we're writing */
135 static int Dflag
; /* list available devices and exit */
137 * This is exported because, in some versions of libpcap, if libpcap
138 * is built with optimizer debugging code (which is *NOT* the default
139 * configuration!), the library *imports*(!) a variable named dflag,
140 * under the expectation that tcpdump is exporting it, to govern
141 * how much debugging information to print when optimizing
142 * the generated BPF code.
144 * This is a horrible hack; newer versions of libpcap don't import
145 * dflag but, instead, *if* built with optimizer debugging code,
146 * *export* a routine to set that flag.
148 int dflag
; /* print filter code */
149 static int Gflag
; /* rotate dump files after this many seconds */
150 static int Gflag_count
; /* number of files created with Gflag rotation */
151 static time_t Gflag_time
; /* The last time_t the dump file was rotated. */
152 static int Lflag
; /* list available data link types and exit */
153 static int Iflag
; /* rfmon (monitor) mode */
154 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
155 static int Jflag
; /* list available time stamp types */
157 static int jflag
= -1; /* packet time stamp source */
158 static int pflag
; /* don't go promiscuous */
159 #ifdef HAVE_PCAP_SETDIRECTION
160 static int Qflag
= -1; /* restrict captured packet by send/receive direction */
162 static int Uflag
; /* "unbuffered" output of dump files */
163 static int Wflag
; /* recycle output files after this number of files */
164 static int WflagChars
;
165 static char *zflag
= NULL
; /* compress each savefile using a specified command (like gzip or bzip2) */
166 static int immediate_mode
;
168 static int infodelay
;
169 static int infoprint
;
174 static void error(FORMAT_STRING(const char *), ...) NORETURN
PRINTFLIKE(1, 2);
175 static void warning(FORMAT_STRING(const char *), ...) PRINTFLIKE(1, 2);
176 static void exit_tcpdump(int) NORETURN
;
177 static RETSIGTYPE
cleanup(int);
178 static RETSIGTYPE
child_cleanup(int);
179 static void print_version(void);
180 static void print_usage(void);
181 static void show_tstamp_types_and_exit(pcap_t
*, const char *device
) NORETURN
;
182 static void show_dlts_and_exit(pcap_t
*, const char *device
) NORETURN
;
183 #ifdef HAVE_PCAP_FINDALLDEVS
184 static void show_devices_and_exit (void) NORETURN
;
187 static void print_packet(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
188 static void dump_packet_and_trunc(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
189 static void dump_packet(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
190 static void droproot(const char *, const char *);
192 #ifdef SIGNAL_REQ_INFO
193 RETSIGTYPE
requestinfo(int);
196 #if defined(USE_WIN32_MM_TIMER)
197 #include <MMsystem.h>
198 static UINT timer_id
;
199 static void CALLBACK
verbose_stats_dump(UINT
, UINT
, DWORD_PTR
, DWORD_PTR
, DWORD_PTR
);
200 #elif defined(HAVE_ALARM)
201 static void verbose_stats_dump(int sig
);
204 static void info(int);
205 static u_int packets_captured
;
207 #ifdef HAVE_PCAP_FINDALLDEVS
208 static const struct tok status_flags
[] = {
210 { PCAP_IF_UP
, "Up" },
212 #ifdef PCAP_IF_RUNNING
213 { PCAP_IF_RUNNING
, "Running" },
215 { PCAP_IF_LOOPBACK
, "Loopback" },
222 static int supports_monitor_mode
;
230 char *CurrentFileName
;
238 #if defined(HAVE_PCAP_SET_PARSER_DEBUG)
240 * We have pcap_set_parser_debug() in libpcap; declare it (it's not declared
241 * by any libpcap header, because it's a special hack, only available if
242 * libpcap was configured to include it, and only intended for use by
243 * libpcap developers trying to debug the parser for filter expressions).
246 __declspec(dllimport
)
250 void pcap_set_parser_debug(int);
251 #elif defined(HAVE_PCAP_DEBUG) || defined(HAVE_YYDEBUG)
253 * We don't have pcap_set_parser_debug() in libpcap, but we do have
254 * pcap_debug or yydebug. Make a local version of pcap_set_parser_debug()
255 * to set the flag, and define HAVE_PCAP_SET_PARSER_DEBUG.
258 pcap_set_parser_debug(int value
)
260 #ifdef HAVE_PCAP_DEBUG
261 extern int pcap_debug
;
264 #else /* HAVE_PCAP_DEBUG */
268 #endif /* HAVE_PCAP_DEBUG */
271 #define HAVE_PCAP_SET_PARSER_DEBUG
274 #if defined(HAVE_PCAP_SET_OPTIMIZER_DEBUG)
276 * We have pcap_set_optimizer_debug() in libpcap; declare it (it's not declared
277 * by any libpcap header, because it's a special hack, only available if
278 * libpcap was configured to include it, and only intended for use by
279 * libpcap developers trying to debug the optimizer for filter expressions).
282 __declspec(dllimport
)
286 void pcap_set_optimizer_debug(int);
291 error(const char *fmt
, ...)
295 (void)fprintf(stderr
, "%s: ", program_name
);
297 (void)vfprintf(stderr
, fmt
, ap
);
302 (void)fputc('\n', stderr
);
310 warning(const char *fmt
, ...)
314 (void)fprintf(stderr
, "%s: WARNING: ", program_name
);
316 (void)vfprintf(stderr
, fmt
, ap
);
321 (void)fputc('\n', stderr
);
326 exit_tcpdump(int status
)
332 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
334 show_tstamp_types_and_exit(pcap_t
*pc
, const char *device
)
337 int *tstamp_types
= 0;
338 const char *tstamp_type_name
;
341 n_tstamp_types
= pcap_list_tstamp_types(pc
, &tstamp_types
);
342 if (n_tstamp_types
< 0)
343 error("%s", pcap_geterr(pc
));
345 if (n_tstamp_types
== 0) {
346 fprintf(stderr
, "Time stamp type cannot be set for %s\n",
350 fprintf(stderr
, "Time stamp types for %s (use option -j to set):\n",
352 for (i
= 0; i
< n_tstamp_types
; i
++) {
353 tstamp_type_name
= pcap_tstamp_type_val_to_name(tstamp_types
[i
]);
354 if (tstamp_type_name
!= NULL
) {
355 (void) fprintf(stderr
, " %s (%s)\n", tstamp_type_name
,
356 pcap_tstamp_type_val_to_description(tstamp_types
[i
]));
358 (void) fprintf(stderr
, " %d\n", tstamp_types
[i
]);
361 pcap_free_tstamp_types(tstamp_types
);
367 show_dlts_and_exit(pcap_t
*pc
, const char *device
)
371 const char *dlt_name
;
373 n_dlts
= pcap_list_datalinks(pc
, &dlts
);
375 error("%s", pcap_geterr(pc
));
376 else if (n_dlts
== 0 || !dlts
)
377 error("No data link types.");
380 * If the interface is known to support monitor mode, indicate
381 * whether these are the data link types available when not in
382 * monitor mode, if -I wasn't specified, or when in monitor mode,
383 * when -I was specified (the link-layer types available in
384 * monitor mode might be different from the ones available when
385 * not in monitor mode).
387 if (supports_monitor_mode
)
388 (void) fprintf(stderr
, "Data link types for %s %s (use option -y to set):\n",
390 Iflag
? "when in monitor mode" : "when not in monitor mode");
392 (void) fprintf(stderr
, "Data link types for %s (use option -y to set):\n",
395 for (i
= 0; i
< n_dlts
; i
++) {
396 dlt_name
= pcap_datalink_val_to_name(dlts
[i
]);
397 if (dlt_name
!= NULL
) {
398 (void) fprintf(stderr
, " %s (%s)", dlt_name
,
399 pcap_datalink_val_to_description(dlts
[i
]));
402 * OK, does tcpdump handle that type?
404 if (!has_printer(dlts
[i
]))
405 (void) fprintf(stderr
, " (printing not supported)");
406 fprintf(stderr
, "\n");
408 (void) fprintf(stderr
, " DLT %d (printing not supported)\n",
412 #ifdef HAVE_PCAP_FREE_DATALINKS
413 pcap_free_datalinks(dlts
);
418 #ifdef HAVE_PCAP_FINDALLDEVS
420 show_devices_and_exit (void)
422 pcap_if_t
*dev
, *devlist
;
423 char ebuf
[PCAP_ERRBUF_SIZE
];
426 if (pcap_findalldevs(&devlist
, ebuf
) < 0)
428 for (i
= 0, dev
= devlist
; dev
!= NULL
; i
++, dev
= dev
->next
) {
429 printf("%d.%s", i
+1, dev
->name
);
430 if (dev
->description
!= NULL
)
431 printf(" (%s)", dev
->description
);
433 printf(" [%s]", bittok2str(status_flags
, "none", dev
->flags
));
436 pcap_freealldevs(devlist
);
439 #endif /* HAVE_PCAP_FINDALLDEVS */
444 * Note that there we use all letters for short options except for g, k,
445 * o, and P, and those are used by other versions of tcpdump, and we should
446 * only use them for the same purposes that the other versions of tcpdump
449 * OS X tcpdump uses -g to force non--v output for IP to be on one
450 * line, making it more "g"repable;
452 * OS X tcpdump uses -k to specify that packet comments in pcap-ng files
455 * OpenBSD tcpdump uses -o to indicate that OS fingerprinting should be done
456 * for hosts sending TCP SYN packets;
458 * OS X tcpdump uses -P to indicate that -w should write pcap-ng rather
461 * OS X tcpdump also uses -Q to specify expressions that match packet
462 * metadata, including but not limited to the packet direction.
463 * The expression syntax is different from a simple "in|out|inout",
464 * and those expressions aren't accepted by OS X tcpdump, but the
465 * equivalents would be "in" = "dir=in", "out" = "dir=out", and
466 * "inout" = "dir=in or dir=out", and the parser could conceivably
467 * special-case "in", "out", and "inout" as expressions for backwards
468 * compatibility, so all is not (yet) lost.
472 * Set up flags that might or might not be supported depending on the
473 * version of libpcap we're using.
475 #if defined(HAVE_PCAP_CREATE) || defined(_WIN32)
477 #define B_FLAG_USAGE " [ -B size ]"
478 #else /* defined(HAVE_PCAP_CREATE) || defined(_WIN32) */
481 #endif /* defined(HAVE_PCAP_CREATE) || defined(_WIN32) */
483 #ifdef HAVE_PCAP_CREATE
485 #else /* HAVE_PCAP_CREATE */
487 #endif /* HAVE_PCAP_CREATE */
489 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
491 #define j_FLAG_USAGE " [ -j tstamptype ]"
493 #else /* PCAP_ERROR_TSTAMP_TYPE_NOTSUP */
497 #endif /* PCAP_ERROR_TSTAMP_TYPE_NOTSUP */
499 #ifdef HAVE_PCAP_FINDALLDEVS
505 #ifdef HAVE_PCAP_DUMP_FLUSH
511 #ifdef HAVE_PCAP_SETDIRECTION
517 #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:#"
522 * We do not currently have long options corresponding to all short
523 * options; we should probably pick appropriate option names for them.
525 * However, the short options where the number of times the option is
526 * specified matters, such as -v and -d and -t, should probably not
527 * just map to a long option, as saying
529 * tcpdump --verbose --verbose
531 * doesn't make sense; it should be --verbosity={N} or something such
534 * For long options with no corresponding short options, we define values
535 * outside the range of ASCII graphic characters, make that the last
536 * component of the entry for the long option, and have a case for that
537 * option in the switch statement.
539 #define OPTION_VERSION 128
540 #define OPTION_TSTAMP_PRECISION 129
541 #define OPTION_IMMEDIATE_MODE 130
543 static const struct option longopts
[] = {
544 #if defined(HAVE_PCAP_CREATE) || defined(_WIN32)
545 { "buffer-size", required_argument
, NULL
, 'B' },
547 { "list-interfaces", no_argument
, NULL
, 'D' },
548 { "help", no_argument
, NULL
, 'h' },
549 { "interface", required_argument
, NULL
, 'i' },
550 #ifdef HAVE_PCAP_CREATE
551 { "monitor-mode", no_argument
, NULL
, 'I' },
553 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
554 { "time-stamp-type", required_argument
, NULL
, 'j' },
555 { "list-time-stamp-types", no_argument
, NULL
, 'J' },
557 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
558 { "time-stamp-precision", required_argument
, NULL
, OPTION_TSTAMP_PRECISION
},
560 { "dont-verify-checksums", no_argument
, NULL
, 'K' },
561 { "list-data-link-types", no_argument
, NULL
, 'L' },
562 { "no-optimize", no_argument
, NULL
, 'O' },
563 { "no-promiscuous-mode", no_argument
, NULL
, 'p' },
564 #ifdef HAVE_PCAP_SETDIRECTION
565 { "direction", required_argument
, NULL
, 'Q' },
567 { "snapshot-length", required_argument
, NULL
, 's' },
568 { "absolute-tcp-sequence-numbers", no_argument
, NULL
, 'S' },
569 #ifdef HAVE_PCAP_DUMP_FLUSH
570 { "packet-buffered", no_argument
, NULL
, 'U' },
572 { "linktype", required_argument
, NULL
, 'y' },
573 #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
574 { "immediate-mode", no_argument
, NULL
, OPTION_IMMEDIATE_MODE
},
576 #ifdef HAVE_PCAP_SET_PARSER_DEBUG
577 { "debug-filter-parser", no_argument
, NULL
, 'Y' },
579 { "relinquish-privileges", required_argument
, NULL
, 'Z' },
580 { "number", no_argument
, NULL
, '#' },
581 { "version", no_argument
, NULL
, OPTION_VERSION
},
586 /* Drop root privileges and chroot if necessary */
588 droproot(const char *username
, const char *chroot_dir
)
590 struct passwd
*pw
= NULL
;
592 if (chroot_dir
&& !username
) {
593 fprintf(stderr
, "%s: Chroot without dropping root is insecure\n",
598 pw
= getpwnam(username
);
601 if (chroot(chroot_dir
) != 0 || chdir ("/") != 0) {
602 fprintf(stderr
, "%s: Couldn't chroot/chdir to '%.64s': %s\n",
603 program_name
, chroot_dir
, pcap_strerror(errno
));
607 #ifdef HAVE_LIBCAP_NG
609 int ret
= capng_change_id(pw
->pw_uid
, pw
->pw_gid
, CAPNG_NO_FLAG
);
611 error("capng_change_id(): return %d\n", ret
);
613 fprintf(stderr
, "dropped privs to %s\n", username
);
616 if (initgroups(pw
->pw_name
, pw
->pw_gid
) != 0 ||
617 setgid(pw
->pw_gid
) != 0 || setuid(pw
->pw_uid
) != 0) {
618 fprintf(stderr
, "%s: Couldn't change to '%.32s' uid=%lu gid=%lu: %s\n",
619 program_name
, username
,
620 (unsigned long)pw
->pw_uid
,
621 (unsigned long)pw
->pw_gid
,
622 pcap_strerror(errno
));
626 fprintf(stderr
, "dropped privs to %s\n", username
);
628 #endif /* HAVE_LIBCAP_NG */
631 fprintf(stderr
, "%s: Couldn't find user '%.32s'\n",
632 program_name
, username
);
635 #ifdef HAVE_LIBCAP_NG
636 /* We don't need CAP_SETUID, CAP_SETGID and CAP_SYS_CHROOT any more. */
639 CAPNG_EFFECTIVE
| CAPNG_PERMITTED
,
644 capng_apply(CAPNG_SELECT_BOTH
);
645 #endif /* HAVE_LIBCAP_NG */
666 MakeFilename(char *buffer
, char *orig_name
, int cnt
, int max_chars
)
668 char *filename
= malloc(PATH_MAX
+ 1);
669 if (filename
== NULL
)
670 error("Makefilename: malloc");
672 /* Process with strftime if Gflag is set. */
676 /* Convert Gflag_time to a usable format */
677 if ((local_tm
= localtime(&Gflag_time
)) == NULL
) {
678 error("MakeTimedFilename: localtime");
681 /* There's no good way to detect an error in strftime since a return
682 * value of 0 isn't necessarily failure.
684 strftime(filename
, PATH_MAX
, orig_name
, local_tm
);
686 strncpy(filename
, orig_name
, PATH_MAX
);
689 if (cnt
== 0 && max_chars
== 0)
690 strncpy(buffer
, filename
, PATH_MAX
+ 1);
692 if (snprintf(buffer
, PATH_MAX
+ 1, "%s%0*d", filename
, max_chars
, cnt
) > PATH_MAX
)
693 /* Report an error if the filename is too large */
694 error("too many output files or filename is too long (> %d)", PATH_MAX
);
699 get_next_file(FILE *VFile
, char *ptr
)
703 ret
= fgets(ptr
, PATH_MAX
, VFile
);
707 if (ptr
[strlen(ptr
) - 1] == '\n')
708 ptr
[strlen(ptr
) - 1] = '\0';
713 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
715 tstamp_precision_from_string(const char *precision
)
717 if (strncmp(precision
, "nano", strlen("nano")) == 0)
718 return PCAP_TSTAMP_PRECISION_NANO
;
720 if (strncmp(precision
, "micro", strlen("micro")) == 0)
721 return PCAP_TSTAMP_PRECISION_MICRO
;
727 tstamp_precision_to_string(int precision
)
731 case PCAP_TSTAMP_PRECISION_MICRO
:
734 case PCAP_TSTAMP_PRECISION_NANO
:
745 * Ensure that, on a dump file's descriptor, we have all the rights
746 * necessary to make the standard I/O library work with an fdopen()ed
747 * FILE * from that descriptor.
749 * A long time ago, in a galaxy far far away, AT&T decided that, instead
750 * of providing separate APIs for getting and setting the FD_ flags on a
751 * descriptor, getting and setting the O_ flags on a descriptor, and
752 * locking files, they'd throw them all into a kitchen-sink fcntl() call
753 * along the lines of ioctl(), the fact that ioctl() operations are
754 * largely specific to particular character devices but fcntl() operations
755 * are either generic to all descriptors or generic to all descriptors for
756 * regular files nonwithstanding.
758 * The Capsicum people decided that fine-grained control of descriptor
759 * operations was required, so that you need to grant permission for
760 * reading, writing, seeking, and fcntl-ing. The latter, courtesy of
761 * AT&T's decision, means that "fcntl-ing" isn't a thing, but a motley
762 * collection of things, so there are *individual* fcntls for which
763 * permission needs to be granted.
765 * The FreeBSD standard I/O people implemented some optimizations that
766 * requires that the standard I/O routines be able to determine whether
767 * the descriptor for the FILE * is open append-only or not; as that
768 * descriptor could have come from an open() rather than an fopen(),
769 * that requires that it be able to do an F_GETFL fcntl() to read
772 * Tcpdump uses ftell() to determine how much data has been written
773 * to a file in order to, when used with -C, determine when it's time
774 * to rotate capture files. ftell() therefore needs to do an lseek()
775 * to find out the file offset and must, thanks to the aforementioned
776 * optimization, also know whether the descriptor is open append-only
779 * The net result of all the above is that we need to grant CAP_SEEK,
780 * CAP_WRITE, and CAP_FCNTL with the CAP_FCNTL_GETFL subcapability.
782 * Perhaps this is the universe's way of saying that either
784 * 1) there needs to be an fopenat() call and a pcap_dump_openat() call
785 * using it, so that Capsicum-capable tcpdump wouldn't need to do
790 * 2) there needs to be a cap_fdopen() call in the FreeBSD standard
791 * I/O library that knows what rights are needed by the standard
792 * I/O library, based on the open mode, and assigns them, perhaps
793 * with an additional argument indicating, for example, whether
794 * seeking should be allowed, so that tcpdump doesn't need to know
795 * what the standard I/O library happens to require this week.
798 set_dumper_capsicum_rights(pcap_dumper_t
*p
)
800 int fd
= fileno(pcap_dump_file(p
));
803 cap_rights_init(&rights
, CAP_SEEK
, CAP_WRITE
, CAP_FCNTL
);
804 if (cap_rights_limit(fd
, &rights
) < 0 && errno
!= ENOSYS
) {
805 error("unable to limit dump descriptor");
807 if (cap_fcntls_limit(fd
, CAP_FCNTL_GETFL
) < 0 && errno
!= ENOSYS
) {
808 error("unable to limit dump descriptor fcntls");
814 * Copy arg vector into a new buffer, concatenating arguments with spaces.
817 copy_argv(register char **argv
)
820 register u_int len
= 0;
829 len
+= strlen(*p
++) + 1;
831 buf
= (char *)malloc(len
);
833 error("copy_argv: malloc");
837 while ((src
= *p
++) != NULL
) {
838 while ((*dst
++ = *src
++) != '\0')
848 * On Windows, we need to open the file in binary mode, so that
849 * we get all the bytes specified by the size we get from "fstat()".
850 * On UNIX, that's not necessary. O_BINARY is defined on Windows;
851 * we define it as 0 if it's not defined, so it does nothing.
858 read_infile(char *fname
)
860 register int i
, fd
, cc
;
864 fd
= open(fname
, O_RDONLY
|O_BINARY
);
866 error("can't open %s: %s", fname
, pcap_strerror(errno
));
868 if (fstat(fd
, &buf
) < 0)
869 error("can't stat %s: %s", fname
, pcap_strerror(errno
));
871 cp
= malloc((u_int
)buf
.st_size
+ 1);
873 error("malloc(%d) for %s: %s", (u_int
)buf
.st_size
+ 1,
874 fname
, pcap_strerror(errno
));
875 cc
= read(fd
, cp
, (u_int
)buf
.st_size
);
877 error("read %s: %s", fname
, pcap_strerror(errno
));
878 if (cc
!= buf
.st_size
)
879 error("short read %s (%d != %d)", fname
, cc
, (int)buf
.st_size
);
882 /* replace "# comment" with spaces */
883 for (i
= 0; i
< cc
; i
++) {
885 while (i
< cc
&& cp
[i
] != '\n')
892 #ifdef HAVE_PCAP_FINDALLDEVS
894 parse_interface_number(const char *device
)
899 devnum
= strtol(device
, &end
, 10);
900 if (device
!= end
&& *end
== '\0') {
902 * It's all-numeric, but is it a valid number?
906 * No, it's not an ordinal.
908 error("Invalid adapter index");
913 * It's not all-numeric; return -1, so our caller
921 find_interface_by_number(long devnum
)
923 pcap_if_t
*dev
, *devlist
;
925 char ebuf
[PCAP_ERRBUF_SIZE
];
928 if (pcap_findalldevs(&devlist
, ebuf
) < 0)
931 * Look for the devnum-th entry in the list of devices (1-based).
933 for (i
= 0, dev
= devlist
; i
< devnum
-1 && dev
!= NULL
;
934 i
++, dev
= dev
->next
)
937 error("Invalid adapter index");
938 device
= strdup(dev
->name
);
939 pcap_freealldevs(devlist
);
945 open_interface(const char *device
, netdissect_options
*ndo
, char *ebuf
)
948 #ifdef HAVE_PCAP_CREATE
953 #ifdef HAVE_PCAP_CREATE
954 pc
= pcap_create(device
, ebuf
);
957 * If this failed with "No such device", that means
958 * the interface doesn't exist; return NULL, so that
959 * the caller can see whether the device name is
960 * actually an interface index.
962 if (strstr(ebuf
, "No such device") != NULL
)
966 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
968 show_tstamp_types_and_exit(pc
, device
);
970 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
971 status
= pcap_set_tstamp_precision(pc
, ndo
->ndo_tstamp_precision
);
973 error("%s: Can't set %ssecond time stamp precision: %s",
975 tstamp_precision_to_string(ndo
->ndo_tstamp_precision
),
976 pcap_statustostr(status
));
979 #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
980 if (immediate_mode
) {
981 status
= pcap_set_immediate_mode(pc
, 1);
983 error("%s: Can't set immediate mode: %s",
985 pcap_statustostr(status
));
989 * Is this an interface that supports monitor mode?
991 if (pcap_can_set_rfmon(pc
) == 1)
992 supports_monitor_mode
= 1;
994 supports_monitor_mode
= 0;
995 status
= pcap_set_snaplen(pc
, ndo
->ndo_snaplen
);
997 error("%s: Can't set snapshot length: %s",
998 device
, pcap_statustostr(status
));
999 status
= pcap_set_promisc(pc
, !pflag
);
1001 error("%s: Can't set promiscuous mode: %s",
1002 device
, pcap_statustostr(status
));
1004 status
= pcap_set_rfmon(pc
, 1);
1006 error("%s: Can't set monitor mode: %s",
1007 device
, pcap_statustostr(status
));
1009 status
= pcap_set_timeout(pc
, 1000);
1011 error("%s: pcap_set_timeout failed: %s",
1012 device
, pcap_statustostr(status
));
1014 status
= pcap_set_buffer_size(pc
, Bflag
);
1016 error("%s: Can't set buffer size: %s",
1017 device
, pcap_statustostr(status
));
1019 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
1021 status
= pcap_set_tstamp_type(pc
, jflag
);
1023 error("%s: Can't set time stamp type: %s",
1024 device
, pcap_statustostr(status
));
1027 status
= pcap_activate(pc
);
1030 * pcap_activate() failed.
1032 cp
= pcap_geterr(pc
);
1033 if (status
== PCAP_ERROR
)
1035 else if (status
== PCAP_ERROR_NO_SUCH_DEVICE
) {
1037 * Return an error for our caller to handle.
1039 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "%s: %s\n(%s)",
1040 device
, pcap_statustostr(status
), cp
);
1043 } else if (status
== PCAP_ERROR_PERM_DENIED
&& *cp
!= '\0')
1044 error("%s: %s\n(%s)", device
,
1045 pcap_statustostr(status
), cp
);
1047 error("%s: %s", device
,
1048 pcap_statustostr(status
));
1049 } else if (status
> 0) {
1051 * pcap_activate() succeeded, but it's warning us
1052 * of a problem it had.
1054 cp
= pcap_geterr(pc
);
1055 if (status
== PCAP_WARNING
)
1057 else if (status
== PCAP_WARNING_PROMISC_NOTSUP
&&
1059 warning("%s: %s\n(%s)", device
,
1060 pcap_statustostr(status
), cp
);
1062 warning("%s: %s", device
,
1063 pcap_statustostr(status
));
1065 #ifdef HAVE_PCAP_SETDIRECTION
1067 status
= pcap_setdirection(pc
, Qflag
);
1069 error("%s: pcap_setdirection() failed: %s",
1070 device
, pcap_geterr(pc
));
1072 #endif /* HAVE_PCAP_SETDIRECTION */
1073 #else /* HAVE_PCAP_CREATE */
1075 pc
= pcap_open_live(device
, ndo
->ndo_snaplen
, !pflag
, 1000, ebuf
);
1078 * If this failed with "No such device", that means
1079 * the interface doesn't exist; return NULL, so that
1080 * the caller can see whether the device name is
1081 * actually an interface index.
1083 if (strstr(ebuf
, "No such device") != NULL
)
1088 warning("%s", ebuf
);
1089 #endif /* HAVE_PCAP_CREATE */
1095 main(int argc
, char **argv
)
1097 register int cnt
, op
, i
;
1098 bpf_u_int32 localnet
=0 , netmask
= 0;
1099 int timezone_offset
= 0;
1100 register char *cp
, *infile
, *cmdbuf
, *device
, *RFileName
, *VFileName
, *WFileName
;
1101 pcap_handler callback
;
1103 const char *dlt_name
;
1104 struct bpf_program fcode
;
1106 RETSIGTYPE (*oldhandler
)(int);
1108 struct dump_info dumpinfo
;
1109 u_char
*pcap_userdata
;
1110 char ebuf
[PCAP_ERRBUF_SIZE
];
1111 char VFileLine
[PATH_MAX
+ 1];
1112 char *username
= NULL
;
1113 char *chroot_dir
= NULL
;
1116 #ifdef HAVE_PCAP_FINDALLDEVS
1122 #ifdef HAVE_CAPSICUM
1123 cap_rights_t rights
;
1125 #endif /* HAVE_CAPSICUM */
1126 int Oflag
= 1; /* run filter code optimizer */
1128 const char *yflag_dlt_name
= NULL
;
1130 netdissect_options Ndo
;
1131 netdissect_options
*ndo
= &Ndo
;
1134 * Initialize the netdissect code.
1136 if (nd_init(ebuf
, sizeof ebuf
) == -1)
1139 memset(ndo
, 0, sizeof(*ndo
));
1140 ndo_set_function_pointers(ndo
);
1141 ndo
->ndo_snaplen
= DEFAULT_SNAPLEN
;
1151 if ((cp
= strrchr(argv
[0], '/')) != NULL
)
1152 ndo
->program_name
= program_name
= cp
+ 1;
1154 ndo
->program_name
= program_name
= argv
[0];
1157 if (pcap_wsockinit() != 0)
1158 error("Attempting to initialize Winsock failed");
1162 * On platforms where the CPU doesn't support unaligned loads,
1163 * force unaligned accesses to abort with SIGBUS, rather than
1164 * being fixed up (slowly) by the OS kernel; on those platforms,
1165 * misaligned accesses are bugs, and we want tcpdump to crash so
1166 * that the bugs are reported.
1168 if (abort_on_misalignment(ebuf
, sizeof(ebuf
)) < 0)
1172 (op
= getopt_long(argc
, argv
, SHORTOPTS
, longopts
, NULL
)) != -1)
1176 /* compatibility for old -a */
1187 #if defined(HAVE_PCAP_CREATE) || defined(_WIN32)
1189 Bflag
= atoi(optarg
)*1024;
1191 error("invalid packet buffer size %s", optarg
);
1193 #endif /* defined(HAVE_PCAP_CREATE) || defined(_WIN32) */
1198 error("invalid packet count %s", optarg
);
1202 Cflag
= atoi(optarg
) * 1000000;
1204 error("invalid file size %s", optarg
);
1224 #ifndef HAVE_LIBCRYPTO
1225 warning("crypto code not compiled in");
1227 ndo
->ndo_espsecret
= optarg
;
1239 Gflag
= atoi(optarg
);
1241 error("invalid number of seconds %s", optarg
);
1243 /* We will create one file initially. */
1246 /* Grab the current time for rotation use. */
1247 if ((Gflag_time
= time(NULL
)) == (time_t)-1) {
1248 error("main: can't get current time: %s",
1249 pcap_strerror(errno
));
1266 #ifdef HAVE_PCAP_CREATE
1270 #endif /* HAVE_PCAP_CREATE */
1272 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
1274 jflag
= pcap_tstamp_type_name_to_val(optarg
);
1276 error("invalid time stamp type %s", optarg
);
1287 * _IOLBF is the same as _IOFBF in Microsoft's C
1288 * libraries; the only alternative they offer
1291 * XXX - this should really be checking for MSVC++,
1292 * not _WIN32, if, for example, MinGW has its own
1293 * C library that is more UNIX-compatible.
1295 setvbuf(stdout
, NULL
, _IONBF
, 0);
1297 #ifdef HAVE_SETLINEBUF
1300 setvbuf(stdout
, NULL
, _IOLBF
, 0);
1310 if (nd_have_smi_support()) {
1311 if (nd_load_smi_module(optarg
, ebuf
, sizeof ebuf
) == -1)
1314 (void)fprintf(stderr
, "%s: ignoring option `-m %s' ",
1315 program_name
, optarg
);
1316 (void)fprintf(stderr
, "(no libsmi support)\n");
1321 /* TCP-MD5 shared secret */
1322 #ifndef HAVE_LIBCRYPTO
1323 warning("crypto code not compiled in");
1325 ndo
->ndo_sigsecret
= optarg
;
1346 ++ndo
->ndo_suppress_default_print
;
1349 #ifdef HAVE_PCAP_SETDIRECTION
1351 if (ascii_strcasecmp(optarg
, "in") == 0)
1353 else if (ascii_strcasecmp(optarg
, "out") == 0)
1355 else if (ascii_strcasecmp(optarg
, "inout") == 0)
1356 Qflag
= PCAP_D_INOUT
;
1358 error("unknown capture direction `%s'", optarg
);
1360 #endif /* HAVE_PCAP_SETDIRECTION */
1367 ndo
->ndo_snaplen
= strtol(optarg
, &end
, 0);
1368 if (optarg
== end
|| *end
!= '\0'
1369 || ndo
->ndo_snaplen
< 0 || ndo
->ndo_snaplen
> MAXIMUM_SNAPLEN
)
1370 error("invalid snaplen %s", optarg
);
1371 else if (ndo
->ndo_snaplen
== 0)
1372 ndo
->ndo_snaplen
= MAXIMUM_SNAPLEN
;
1384 if (ascii_strcasecmp(optarg
, "vat") == 0)
1385 ndo
->ndo_packettype
= PT_VAT
;
1386 else if (ascii_strcasecmp(optarg
, "wb") == 0)
1387 ndo
->ndo_packettype
= PT_WB
;
1388 else if (ascii_strcasecmp(optarg
, "rpc") == 0)
1389 ndo
->ndo_packettype
= PT_RPC
;
1390 else if (ascii_strcasecmp(optarg
, "rtp") == 0)
1391 ndo
->ndo_packettype
= PT_RTP
;
1392 else if (ascii_strcasecmp(optarg
, "rtcp") == 0)
1393 ndo
->ndo_packettype
= PT_RTCP
;
1394 else if (ascii_strcasecmp(optarg
, "snmp") == 0)
1395 ndo
->ndo_packettype
= PT_SNMP
;
1396 else if (ascii_strcasecmp(optarg
, "cnfp") == 0)
1397 ndo
->ndo_packettype
= PT_CNFP
;
1398 else if (ascii_strcasecmp(optarg
, "tftp") == 0)
1399 ndo
->ndo_packettype
= PT_TFTP
;
1400 else if (ascii_strcasecmp(optarg
, "aodv") == 0)
1401 ndo
->ndo_packettype
= PT_AODV
;
1402 else if (ascii_strcasecmp(optarg
, "carp") == 0)
1403 ndo
->ndo_packettype
= PT_CARP
;
1404 else if (ascii_strcasecmp(optarg
, "radius") == 0)
1405 ndo
->ndo_packettype
= PT_RADIUS
;
1406 else if (ascii_strcasecmp(optarg
, "zmtp1") == 0)
1407 ndo
->ndo_packettype
= PT_ZMTP1
;
1408 else if (ascii_strcasecmp(optarg
, "vxlan") == 0)
1409 ndo
->ndo_packettype
= PT_VXLAN
;
1410 else if (ascii_strcasecmp(optarg
, "pgm") == 0)
1411 ndo
->ndo_packettype
= PT_PGM
;
1412 else if (ascii_strcasecmp(optarg
, "pgm_zmtp1") == 0)
1413 ndo
->ndo_packettype
= PT_PGM_ZMTP1
;
1414 else if (ascii_strcasecmp(optarg
, "lmp") == 0)
1415 ndo
->ndo_packettype
= PT_LMP
;
1416 else if (ascii_strcasecmp(optarg
, "resp") == 0)
1417 ndo
->ndo_packettype
= PT_RESP
;
1419 error("unknown packet type `%s'", optarg
);
1426 #ifdef HAVE_PCAP_DUMP_FLUSH
1445 Wflag
= atoi(optarg
);
1447 error("invalid number of output files %s", optarg
);
1448 WflagChars
= getWflagChars(Wflag
);
1453 ++ndo
->ndo_suppress_default_print
;
1458 ++ndo
->ndo_suppress_default_print
;
1462 yflag_dlt_name
= optarg
;
1464 pcap_datalink_name_to_val(yflag_dlt_name
);
1466 error("invalid data link type %s", yflag_dlt_name
);
1469 #ifdef HAVE_PCAP_SET_PARSER_DEBUG
1472 /* Undocumented flag */
1473 pcap_set_parser_debug(1);
1486 ndo
->ndo_packet_number
= 1;
1489 case OPTION_VERSION
:
1494 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
1495 case OPTION_TSTAMP_PRECISION
:
1496 ndo
->ndo_tstamp_precision
= tstamp_precision_from_string(optarg
);
1497 if (ndo
->ndo_tstamp_precision
< 0)
1498 error("unsupported time stamp precision");
1502 #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
1503 case OPTION_IMMEDIATE_MODE
:
1514 #ifdef HAVE_PCAP_FINDALLDEVS
1516 show_devices_and_exit();
1519 switch (ndo
->ndo_tflag
) {
1521 case 0: /* Default */
1522 case 4: /* Default + Date*/
1523 timezone_offset
= gmt2local(0);
1526 case 1: /* No time stamp */
1527 case 2: /* Unix timeval style */
1528 case 3: /* Microseconds since previous packet */
1529 case 5: /* Microseconds since first packet */
1532 default: /* Not supported */
1533 error("only -t, -tt, -ttt, -tttt and -ttttt are supported");
1537 if (ndo
->ndo_fflag
!= 0 && (VFileName
!= NULL
|| RFileName
!= NULL
))
1538 error("-f can not be used with -V or -r");
1540 if (VFileName
!= NULL
&& RFileName
!= NULL
)
1541 error("-V and -r are mutually exclusive.");
1543 #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
1545 * If we're printing dissected packets to the standard output
1546 * rather than saving raw packets to a file, and the standard
1547 * output is a terminal, use immediate mode, as the user's
1548 * probably expecting to see packets pop up immediately.
1550 if (WFileName
== NULL
&& isatty(1))
1555 /* if run as root, prepare for chrooting */
1556 if (getuid() == 0 || geteuid() == 0) {
1557 /* future extensibility for cmd-line arguments */
1559 chroot_dir
= WITH_CHROOT
;
1564 /* if run as root, prepare for dropping root privileges */
1565 if (getuid() == 0 || geteuid() == 0) {
1566 /* Run with '-Z root' to restore old behaviour */
1568 username
= WITH_USER
;
1572 if (RFileName
!= NULL
|| VFileName
!= NULL
) {
1574 * If RFileName is non-null, it's the pathname of a
1575 * savefile to read. If VFileName is non-null, it's
1576 * the pathname of a file containing a list of pathnames
1577 * (one per line) of savefiles to read.
1579 * In either case, we're reading a savefile, not doing
1584 * We don't need network access, so relinquish any set-UID
1585 * or set-GID privileges we have (if any).
1587 * We do *not* want set-UID privileges when opening a
1588 * trace file, as that might let the user read other
1589 * people's trace files (especially if we're set-UID
1592 if (setgid(getgid()) != 0 || setuid(getuid()) != 0 )
1593 fprintf(stderr
, "Warning: setgid/setuid failed !\n");
1595 if (VFileName
!= NULL
) {
1596 if (VFileName
[0] == '-' && VFileName
[1] == '\0')
1599 VFile
= fopen(VFileName
, "r");
1602 error("Unable to open file: %s\n", pcap_strerror(errno
));
1604 ret
= get_next_file(VFile
, VFileLine
);
1606 error("Nothing in %s\n", VFileName
);
1607 RFileName
= VFileLine
;
1610 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
1611 pd
= pcap_open_offline_with_tstamp_precision(RFileName
,
1612 ndo
->ndo_tstamp_precision
, ebuf
);
1614 pd
= pcap_open_offline(RFileName
, ebuf
);
1619 #ifdef HAVE_CAPSICUM
1620 cap_rights_init(&rights
, CAP_READ
);
1621 if (cap_rights_limit(fileno(pcap_file(pd
)), &rights
) < 0 &&
1623 error("unable to limit pcap descriptor");
1626 dlt
= pcap_datalink(pd
);
1627 dlt_name
= pcap_datalink_val_to_name(dlt
);
1628 if (dlt_name
== NULL
) {
1629 fprintf(stderr
, "reading from file %s, link-type %u\n",
1633 "reading from file %s, link-type %s (%s)\n",
1634 RFileName
, dlt_name
,
1635 pcap_datalink_val_to_description(dlt
));
1639 * We're doing a live capture.
1641 if (device
== NULL
) {
1643 * No interface was specified. Pick one.
1645 #ifdef HAVE_PCAP_FINDALLDEVS
1647 * Find the list of interfaces, and pick
1648 * the first interface.
1650 if (pcap_findalldevs(&devlist
, ebuf
) >= 0 &&
1652 device
= strdup(devlist
->name
);
1653 pcap_freealldevs(devlist
);
1655 #else /* HAVE_PCAP_FINDALLDEVS */
1657 * Use whatever interface pcap_lookupdev()
1660 device
= pcap_lookupdev(ebuf
);
1667 * Try to open the interface with the specified name.
1669 pd
= open_interface(device
, ndo
, ebuf
);
1672 * That failed. If we can get a list of
1673 * interfaces, and the interface name
1674 * is purely numeric, try to use it as
1675 * a 1-based index in the list of
1678 #ifdef HAVE_PCAP_FINDALLDEVS
1679 devnum
= parse_interface_number(device
);
1682 * It's not a number; just report
1683 * the open error and fail.
1689 * OK, it's a number; try to find the
1690 * interface with that index, and try
1693 * find_interface_by_number() exits if it
1694 * couldn't be found.
1696 device
= find_interface_by_number(devnum
);
1697 pd
= open_interface(device
, ndo
, ebuf
);
1700 #else /* HAVE_PCAP_FINDALLDEVS */
1702 * We can't get a list of interfaces; just
1706 #endif /* HAVE_PCAP_FINDALLDEVS */
1710 * Let user own process after socket has been opened.
1713 if (setgid(getgid()) != 0 || setuid(getuid()) != 0)
1714 fprintf(stderr
, "Warning: setgid/setuid failed !\n");
1716 #if !defined(HAVE_PCAP_CREATE) && defined(_WIN32)
1718 if(pcap_setbuff(pd
, Bflag
)==-1){
1719 error("%s", pcap_geterr(pd
));
1721 #endif /* !defined(HAVE_PCAP_CREATE) && defined(_WIN32) */
1723 show_dlts_and_exit(pd
, device
);
1724 if (yflag_dlt
>= 0) {
1725 #ifdef HAVE_PCAP_SET_DATALINK
1726 if (pcap_set_datalink(pd
, yflag_dlt
) < 0)
1727 error("%s", pcap_geterr(pd
));
1730 * We don't actually support changing the
1731 * data link type, so we only let them
1732 * set it to what it already is.
1734 if (yflag_dlt
!= pcap_datalink(pd
)) {
1735 error("%s is not one of the DLTs supported by this device\n",
1739 (void)fprintf(stderr
, "%s: data link type %s\n",
1740 program_name
, yflag_dlt_name
);
1741 (void)fflush(stderr
);
1743 i
= pcap_snapshot(pd
);
1744 if (ndo
->ndo_snaplen
< i
) {
1745 warning("snaplen raised from %d to %d", ndo
->ndo_snaplen
, i
);
1746 ndo
->ndo_snaplen
= i
;
1748 if(ndo
->ndo_fflag
!= 0) {
1749 if (pcap_lookupnet(device
, &localnet
, &netmask
, ebuf
) < 0) {
1750 warning("foreign (-f) flag used but: %s", ebuf
);
1756 cmdbuf
= read_infile(infile
);
1758 cmdbuf
= copy_argv(&argv
[optind
]);
1760 #ifdef HAVE_PCAP_SET_OPTIMIZER_DEBUG
1761 pcap_set_optimizer_debug(dflag
);
1763 if (pcap_compile(pd
, &fcode
, cmdbuf
, Oflag
, netmask
) < 0)
1764 error("%s", pcap_geterr(pd
));
1766 bpf_dump(&fcode
, dflag
);
1769 pcap_freecode(&fcode
);
1772 init_print(ndo
, localnet
, netmask
, timezone_offset
);
1775 (void)setsignal(SIGPIPE
, cleanup
);
1776 (void)setsignal(SIGTERM
, cleanup
);
1777 (void)setsignal(SIGINT
, cleanup
);
1779 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
1780 (void)setsignal(SIGCHLD
, child_cleanup
);
1782 /* Cooperate with nohup(1) */
1784 if ((oldhandler
= setsignal(SIGHUP
, cleanup
)) != SIG_DFL
)
1785 (void)setsignal(SIGHUP
, oldhandler
);
1790 * If a user name was specified with "-Z", attempt to switch to
1791 * that user's UID. This would probably be used with sudo,
1792 * to allow tcpdump to be run in a special restricted
1793 * account (if you just want to allow users to open capture
1794 * devices, and can't just give users that permission,
1795 * you'd make tcpdump set-UID or set-GID).
1797 * Tcpdump doesn't necessarily write only to one savefile;
1798 * the general only way to allow a -Z instance to write to
1799 * savefiles as the user under whose UID it's run, rather
1800 * than as the user specified with -Z, would thus be to switch
1801 * to the original user ID before opening a capture file and
1802 * then switch back to the -Z user ID after opening the savefile.
1803 * Switching to the -Z user ID only after opening the first
1804 * savefile doesn't handle the general case.
1807 if (getuid() == 0 || geteuid() == 0) {
1808 #ifdef HAVE_LIBCAP_NG
1809 /* Initialize capng */
1810 capng_clear(CAPNG_SELECT_BOTH
);
1814 CAPNG_PERMITTED
| CAPNG_EFFECTIVE
,
1822 CAPNG_PERMITTED
| CAPNG_EFFECTIVE
,
1830 CAPNG_PERMITTED
| CAPNG_EFFECTIVE
,
1834 capng_apply(CAPNG_SELECT_BOTH
);
1835 #endif /* HAVE_LIBCAP_NG */
1836 if (username
|| chroot_dir
)
1837 droproot(username
, chroot_dir
);
1842 if (pcap_setfilter(pd
, &fcode
) < 0)
1843 error("%s", pcap_geterr(pd
));
1844 #ifdef HAVE_CAPSICUM
1845 if (RFileName
== NULL
&& VFileName
== NULL
) {
1846 static const unsigned long cmds
[] = { BIOCGSTATS
, BIOCROTZBUF
};
1849 * The various libpcap devices use a combination of
1850 * read (bpf), ioctl (bpf, netmap), poll (netmap)
1851 * so we add the relevant access rights.
1853 cap_rights_init(&rights
, CAP_IOCTL
, CAP_READ
, CAP_EVENT
);
1854 if (cap_rights_limit(pcap_fileno(pd
), &rights
) < 0 &&
1856 error("unable to limit pcap descriptor");
1858 if (cap_ioctls_limit(pcap_fileno(pd
), cmds
,
1859 sizeof(cmds
) / sizeof(cmds
[0])) < 0 && errno
!= ENOSYS
) {
1860 error("unable to limit ioctls on pcap descriptor");
1866 /* Do not exceed the default PATH_MAX for files. */
1867 dumpinfo
.CurrentFileName
= (char *)malloc(PATH_MAX
+ 1);
1869 if (dumpinfo
.CurrentFileName
== NULL
)
1870 error("malloc of dumpinfo.CurrentFileName");
1872 /* We do not need numbering for dumpfiles if Cflag isn't set. */
1874 MakeFilename(dumpinfo
.CurrentFileName
, WFileName
, 0, WflagChars
);
1876 MakeFilename(dumpinfo
.CurrentFileName
, WFileName
, 0, 0);
1878 p
= pcap_dump_open(pd
, dumpinfo
.CurrentFileName
);
1879 #ifdef HAVE_LIBCAP_NG
1880 /* Give up CAP_DAC_OVERRIDE capability.
1881 * Only allow it to be restored if the -C or -G flag have been
1882 * set since we may need to create more files later on.
1886 (Cflag
|| Gflag
? 0 : CAPNG_PERMITTED
)
1890 capng_apply(CAPNG_SELECT_BOTH
);
1891 #endif /* HAVE_LIBCAP_NG */
1893 error("%s", pcap_geterr(pd
));
1894 #ifdef HAVE_CAPSICUM
1895 set_dumper_capsicum_rights(p
);
1897 if (Cflag
!= 0 || Gflag
!= 0) {
1898 #ifdef HAVE_CAPSICUM
1899 dumpinfo
.WFileName
= strdup(basename(WFileName
));
1900 if (dumpinfo
.WFileName
== NULL
) {
1901 error("Unable to allocate memory for file %s",
1904 dumpinfo
.dirfd
= open(dirname(WFileName
),
1905 O_DIRECTORY
| O_RDONLY
);
1906 if (dumpinfo
.dirfd
< 0) {
1907 error("unable to open directory %s",
1908 dirname(WFileName
));
1910 cap_rights_init(&rights
, CAP_CREATE
, CAP_FCNTL
,
1911 CAP_FTRUNCATE
, CAP_LOOKUP
, CAP_SEEK
, CAP_WRITE
);
1912 if (cap_rights_limit(dumpinfo
.dirfd
, &rights
) < 0 &&
1914 error("unable to limit directory rights");
1916 if (cap_fcntls_limit(dumpinfo
.dirfd
, CAP_FCNTL_GETFL
) < 0 &&
1918 error("unable to limit dump descriptor fcntls");
1920 #else /* !HAVE_CAPSICUM */
1921 dumpinfo
.WFileName
= WFileName
;
1923 callback
= dump_packet_and_trunc
;
1926 pcap_userdata
= (u_char
*)&dumpinfo
;
1928 callback
= dump_packet
;
1929 pcap_userdata
= (u_char
*)p
;
1931 #ifdef HAVE_PCAP_DUMP_FLUSH
1936 dlt
= pcap_datalink(pd
);
1937 ndo
->ndo_if_printer
= get_if_printer(ndo
, dlt
);
1938 callback
= print_packet
;
1939 pcap_userdata
= (u_char
*)ndo
;
1942 #ifdef SIGNAL_REQ_INFO
1944 * We can't get statistics when reading from a file rather
1945 * than capturing from a device.
1947 if (RFileName
== NULL
)
1948 (void)setsignal(SIGNAL_REQ_INFO
, requestinfo
);
1951 if (ndo
->ndo_vflag
> 0 && WFileName
) {
1953 * When capturing to a file, "-v" means tcpdump should,
1954 * every 10 seconds, "v"erbosely report the number of
1957 #ifdef USE_WIN32_MM_TIMER
1958 /* call verbose_stats_dump() each 1000 +/-100msec */
1959 timer_id
= timeSetEvent(1000, 100, verbose_stats_dump
, 0, TIME_PERIODIC
);
1960 setvbuf(stderr
, NULL
, _IONBF
, 0);
1961 #elif defined(HAVE_ALARM)
1962 (void)setsignal(SIGALRM
, verbose_stats_dump
);
1967 if (RFileName
== NULL
) {
1969 * Live capture (if -V was specified, we set RFileName
1970 * to a file from the -V file). Print a message to
1971 * the standard error on UN*X.
1973 if (!ndo
->ndo_vflag
&& !WFileName
) {
1974 (void)fprintf(stderr
,
1975 "%s: verbose output suppressed, use -v or -vv for full protocol decode\n",
1978 (void)fprintf(stderr
, "%s: ", program_name
);
1979 dlt
= pcap_datalink(pd
);
1980 dlt_name
= pcap_datalink_val_to_name(dlt
);
1981 if (dlt_name
== NULL
) {
1982 (void)fprintf(stderr
, "listening on %s, link-type %u, capture size %u bytes\n",
1983 device
, dlt
, ndo
->ndo_snaplen
);
1985 (void)fprintf(stderr
, "listening on %s, link-type %s (%s), capture size %u bytes\n",
1987 pcap_datalink_val_to_description(dlt
), ndo
->ndo_snaplen
);
1989 (void)fflush(stderr
);
1992 #ifdef HAVE_CAPSICUM
1993 cansandbox
= (ndo
->ndo_nflag
&& VFileName
== NULL
&& zflag
== NULL
);
1994 if (cansandbox
&& cap_enter() < 0 && errno
!= ENOSYS
)
1995 error("unable to enter the capability mode");
1996 #endif /* HAVE_CAPSICUM */
1999 status
= pcap_loop(pd
, cnt
, callback
, pcap_userdata
);
2000 if (WFileName
== NULL
) {
2002 * We're printing packets. Flush the printed output,
2003 * so it doesn't get intermingled with error output.
2007 * We got interrupted, so perhaps we didn't
2008 * manage to finish a line we were printing.
2009 * Print an extra newline, just in case.
2013 (void)fflush(stdout
);
2017 * We got interrupted. If we are reading multiple
2018 * files (via -V) set these so that we stop.
2027 (void)fprintf(stderr
, "%s: pcap_loop: %s\n",
2028 program_name
, pcap_geterr(pd
));
2030 if (RFileName
== NULL
) {
2032 * We're doing a live capture. Report the capture
2038 if (VFileName
!= NULL
) {
2039 ret
= get_next_file(VFile
, VFileLine
);
2043 RFileName
= VFileLine
;
2044 pd
= pcap_open_offline(RFileName
, ebuf
);
2047 #ifdef HAVE_CAPSICUM
2048 cap_rights_init(&rights
, CAP_READ
);
2049 if (cap_rights_limit(fileno(pcap_file(pd
)),
2050 &rights
) < 0 && errno
!= ENOSYS
) {
2051 error("unable to limit pcap descriptor");
2054 new_dlt
= pcap_datalink(pd
);
2055 if (new_dlt
!= dlt
) {
2057 * The new file has a different
2058 * link-layer header type from the
2061 if (WFileName
!= NULL
) {
2063 * We're writing raw packets
2064 * that match the filter to
2065 * a pcap file. pcap files
2066 * don't support multiple
2067 * different link-layer
2068 * header types, so we fail
2071 error("%s: new dlt does not match original", RFileName
);
2075 * We're printing the decoded packets;
2076 * switch to the new DLT.
2078 * To do that, we need to change
2079 * the printer, change the DLT name,
2080 * and recompile the filter with
2084 ndo
->ndo_if_printer
= get_if_printer(ndo
, dlt
);
2085 if (pcap_compile(pd
, &fcode
, cmdbuf
, Oflag
, netmask
) < 0)
2086 error("%s", pcap_geterr(pd
));
2090 * Set the filter on the new file.
2092 if (pcap_setfilter(pd
, &fcode
) < 0)
2093 error("%s", pcap_geterr(pd
));
2096 * Report the new file.
2098 dlt_name
= pcap_datalink_val_to_name(dlt
);
2099 if (dlt_name
== NULL
) {
2100 fprintf(stderr
, "reading from file %s, link-type %u\n",
2104 "reading from file %s, link-type %s (%s)\n",
2105 RFileName
, dlt_name
,
2106 pcap_datalink_val_to_description(dlt
));
2111 while (ret
!= NULL
);
2114 pcap_freecode(&fcode
);
2115 exit_tcpdump(status
== -1 ? 1 : 0);
2118 /* make a clean exit on interrupts */
2120 cleanup(int signo _U_
)
2122 #ifdef USE_WIN32_MM_TIMER
2124 timeKillEvent(timer_id
);
2126 #elif defined(HAVE_ALARM)
2130 #ifdef HAVE_PCAP_BREAKLOOP
2132 * We have "pcap_breakloop()"; use it, so that we do as little
2133 * as possible in the signal handler (it's probably not safe
2134 * to do anything with standard I/O streams in a signal handler -
2135 * the ANSI C standard doesn't say it is).
2140 * We don't have "pcap_breakloop()"; this isn't safe, but
2141 * it's the best we can do. Print the summary if we're
2142 * not reading from a savefile - i.e., if we're doing a
2143 * live capture - and exit.
2145 if (pd
!= NULL
&& pcap_file(pd
) == NULL
) {
2147 * We got interrupted, so perhaps we didn't
2148 * manage to finish a line we were printing.
2149 * Print an extra newline, just in case.
2152 (void)fflush(stdout
);
2160 On windows, we do not use a fork, so we do not care less about
2161 waiting a child processes to die
2163 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
2165 child_cleanup(int signo _U_
)
2169 #endif /* HAVE_FORK && HAVE_VFORK */
2172 info(register int verbose
)
2174 struct pcap_stat stats
;
2177 * Older versions of libpcap didn't set ps_ifdrop on some
2178 * platforms; initialize it to 0 to handle that.
2180 stats
.ps_ifdrop
= 0;
2181 if (pcap_stats(pd
, &stats
) < 0) {
2182 (void)fprintf(stderr
, "pcap_stats: %s\n", pcap_geterr(pd
));
2188 fprintf(stderr
, "%s: ", program_name
);
2190 (void)fprintf(stderr
, "%u packet%s captured", packets_captured
,
2191 PLURAL_SUFFIX(packets_captured
));
2193 fputs(", ", stderr
);
2196 (void)fprintf(stderr
, "%u packet%s received by filter", stats
.ps_recv
,
2197 PLURAL_SUFFIX(stats
.ps_recv
));
2199 fputs(", ", stderr
);
2202 (void)fprintf(stderr
, "%u packet%s dropped by kernel", stats
.ps_drop
,
2203 PLURAL_SUFFIX(stats
.ps_drop
));
2204 if (stats
.ps_ifdrop
!= 0) {
2206 fputs(", ", stderr
);
2209 (void)fprintf(stderr
, "%u packet%s dropped by interface\n",
2210 stats
.ps_ifdrop
, PLURAL_SUFFIX(stats
.ps_ifdrop
));
2216 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
2218 #define fork_subprocess() fork()
2220 #define fork_subprocess() vfork()
2223 compress_savefile(const char *filename
)
2227 child
= fork_subprocess();
2230 "compress_savefile: fork failed: %s\n",
2231 pcap_strerror(errno
));
2235 /* Parent process. */
2241 * Set to lowest priority so that this doesn't disturb the capture.
2244 setpriority(PRIO_PROCESS
, 0, NZERO
- 1);
2246 setpriority(PRIO_PROCESS
, 0, 19);
2248 if (execlp(zflag
, zflag
, filename
, (char *)NULL
) == -1)
2250 "compress_savefile: execlp(%s, %s) failed: %s\n",
2253 pcap_strerror(errno
));
2260 #else /* HAVE_FORK && HAVE_VFORK */
2262 compress_savefile(const char *filename
)
2265 "compress_savefile failed. Functionality not implemented under your system\n");
2267 #endif /* HAVE_FORK && HAVE_VFORK */
2270 dump_packet_and_trunc(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
2272 struct dump_info
*dump_info
;
2278 dump_info
= (struct dump_info
*)user
;
2281 * XXX - this won't force the file to rotate on the specified time
2282 * boundary, but it will rotate on the first packet received after the
2283 * specified Gflag number of seconds. Note: if a Gflag time boundary
2284 * and a Cflag size boundary coincide, the time rotation will occur
2285 * first thereby cancelling the Cflag boundary (since the file should
2289 /* Check if it is time to rotate */
2292 /* Get the current time */
2293 if ((t
= time(NULL
)) == (time_t)-1) {
2294 error("dump_and_trunc_packet: can't get current_time: %s",
2295 pcap_strerror(errno
));
2299 /* If the time is greater than the specified window, rotate */
2300 if (t
- Gflag_time
>= Gflag
) {
2301 #ifdef HAVE_CAPSICUM
2306 /* Update the Gflag_time */
2308 /* Update Gflag_count */
2311 * Close the current file and open a new one.
2313 pcap_dump_close(dump_info
->p
);
2316 * Compress the file we just closed, if the user asked for it
2319 compress_savefile(dump_info
->CurrentFileName
);
2322 * Check to see if we've exceeded the Wflag (when
2325 if (Cflag
== 0 && Wflag
> 0 && Gflag_count
>= Wflag
) {
2326 (void)fprintf(stderr
, "Maximum file limit reached: %d\n",
2332 if (dump_info
->CurrentFileName
!= NULL
)
2333 free(dump_info
->CurrentFileName
);
2334 /* Allocate space for max filename + \0. */
2335 dump_info
->CurrentFileName
= (char *)malloc(PATH_MAX
+ 1);
2336 if (dump_info
->CurrentFileName
== NULL
)
2337 error("dump_packet_and_trunc: malloc");
2339 * Gflag was set otherwise we wouldn't be here. Reset the count
2340 * so multiple files would end with 1,2,3 in the filename.
2341 * The counting is handled with the -C flow after this.
2346 * This is always the first file in the Cflag
2348 * We also don't need numbering if Cflag is not set.
2351 MakeFilename(dump_info
->CurrentFileName
, dump_info
->WFileName
, 0,
2354 MakeFilename(dump_info
->CurrentFileName
, dump_info
->WFileName
, 0, 0);
2356 #ifdef HAVE_LIBCAP_NG
2357 capng_update(CAPNG_ADD
, CAPNG_EFFECTIVE
, CAP_DAC_OVERRIDE
);
2358 capng_apply(CAPNG_SELECT_BOTH
);
2359 #endif /* HAVE_LIBCAP_NG */
2360 #ifdef HAVE_CAPSICUM
2361 fd
= openat(dump_info
->dirfd
,
2362 dump_info
->CurrentFileName
,
2363 O_CREAT
| O_WRONLY
| O_TRUNC
, 0644);
2365 error("unable to open file %s",
2366 dump_info
->CurrentFileName
);
2368 fp
= fdopen(fd
, "w");
2370 error("unable to fdopen file %s",
2371 dump_info
->CurrentFileName
);
2373 dump_info
->p
= pcap_dump_fopen(dump_info
->pd
, fp
);
2374 #else /* !HAVE_CAPSICUM */
2375 dump_info
->p
= pcap_dump_open(dump_info
->pd
, dump_info
->CurrentFileName
);
2377 #ifdef HAVE_LIBCAP_NG
2378 capng_update(CAPNG_DROP
, CAPNG_EFFECTIVE
, CAP_DAC_OVERRIDE
);
2379 capng_apply(CAPNG_SELECT_BOTH
);
2380 #endif /* HAVE_LIBCAP_NG */
2381 if (dump_info
->p
== NULL
)
2382 error("%s", pcap_geterr(pd
));
2383 #ifdef HAVE_CAPSICUM
2384 set_dumper_capsicum_rights(dump_info
->p
);
2390 * XXX - this won't prevent capture files from getting
2391 * larger than Cflag - the last packet written to the
2392 * file could put it over Cflag.
2395 long size
= pcap_dump_ftell(dump_info
->p
);
2398 error("ftell fails on output file");
2400 #ifdef HAVE_CAPSICUM
2406 * Close the current file and open a new one.
2408 pcap_dump_close(dump_info
->p
);
2411 * Compress the file we just closed, if the user
2415 compress_savefile(dump_info
->CurrentFileName
);
2419 if (Cflag_count
>= Wflag
)
2422 if (dump_info
->CurrentFileName
!= NULL
)
2423 free(dump_info
->CurrentFileName
);
2424 dump_info
->CurrentFileName
= (char *)malloc(PATH_MAX
+ 1);
2425 if (dump_info
->CurrentFileName
== NULL
)
2426 error("dump_packet_and_trunc: malloc");
2427 MakeFilename(dump_info
->CurrentFileName
, dump_info
->WFileName
, Cflag_count
, WflagChars
);
2428 #ifdef HAVE_LIBCAP_NG
2429 capng_update(CAPNG_ADD
, CAPNG_EFFECTIVE
, CAP_DAC_OVERRIDE
);
2430 capng_apply(CAPNG_SELECT_BOTH
);
2431 #endif /* HAVE_LIBCAP_NG */
2432 #ifdef HAVE_CAPSICUM
2433 fd
= openat(dump_info
->dirfd
, dump_info
->CurrentFileName
,
2434 O_CREAT
| O_WRONLY
| O_TRUNC
, 0644);
2436 error("unable to open file %s",
2437 dump_info
->CurrentFileName
);
2439 fp
= fdopen(fd
, "w");
2441 error("unable to fdopen file %s",
2442 dump_info
->CurrentFileName
);
2444 dump_info
->p
= pcap_dump_fopen(dump_info
->pd
, fp
);
2445 #else /* !HAVE_CAPSICUM */
2446 dump_info
->p
= pcap_dump_open(dump_info
->pd
, dump_info
->CurrentFileName
);
2448 #ifdef HAVE_LIBCAP_NG
2449 capng_update(CAPNG_DROP
, CAPNG_EFFECTIVE
, CAP_DAC_OVERRIDE
);
2450 capng_apply(CAPNG_SELECT_BOTH
);
2451 #endif /* HAVE_LIBCAP_NG */
2452 if (dump_info
->p
== NULL
)
2453 error("%s", pcap_geterr(pd
));
2454 #ifdef HAVE_CAPSICUM
2455 set_dumper_capsicum_rights(dump_info
->p
);
2460 pcap_dump((u_char
*)dump_info
->p
, h
, sp
);
2461 #ifdef HAVE_PCAP_DUMP_FLUSH
2463 pcap_dump_flush(dump_info
->p
);
2472 dump_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
2478 pcap_dump(user
, h
, sp
);
2479 #ifdef HAVE_PCAP_DUMP_FLUSH
2481 pcap_dump_flush((pcap_dumper_t
*)user
);
2490 print_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
2496 pretty_print_packet((netdissect_options
*)user
, h
, sp
, packets_captured
);
2505 * XXX - there should really be libpcap calls to get the version
2506 * number as a string (the string would be generated from #defines
2507 * at run time, so that it's not generated from string constants
2508 * in the library, as, on many UNIX systems, those constants would
2509 * be statically linked into the application executable image, and
2510 * would thus reflect the version of libpcap on the system on
2511 * which the application was *linked*, not the system on which it's
2514 * That routine should be documented, unlike the "version[]"
2515 * string, so that UNIX vendors providing their own libpcaps
2516 * don't omit it (as a couple of vendors have...).
2518 * Packet.dll should perhaps also export a routine to return the
2519 * version number of the Packet.dll code, to supply the
2520 * "Wpcap_version" information on Windows.
2522 char WDversion
[]="current-git.tcpdump.org";
2523 #if !defined(HAVE_GENERATED_VERSION)
2524 char version
[]="current-git.tcpdump.org";
2526 char pcap_version
[]="current-git.tcpdump.org";
2527 char Wpcap_version
[]="3.1";
2530 #ifdef SIGNAL_REQ_INFO
2531 RETSIGTYPE
requestinfo(int signo _U_
)
2541 * Called once each second in verbose mode while dumping to file
2543 #ifdef USE_WIN32_MM_TIMER
2544 void CALLBACK
verbose_stats_dump (UINT timer_id _U_
, UINT msg _U_
, DWORD_PTR arg _U_
,
2545 DWORD_PTR dw1 _U_
, DWORD_PTR dw2 _U_
)
2548 fprintf(stderr
, "Got %u\r", packets_captured
);
2550 #elif defined(HAVE_ALARM)
2551 static void verbose_stats_dump(int sig _U_
)
2554 fprintf(stderr
, "Got %u\r", packets_captured
);
2559 USES_APPLE_DEPRECATED_API
2563 extern char version
[];
2564 #ifndef HAVE_PCAP_LIB_VERSION
2565 #if defined(_WIN32) || defined(HAVE_PCAP_VERSION)
2566 extern char pcap_version
[];
2567 #else /* defined(_WIN32) || defined(HAVE_PCAP_VERSION) */
2568 static char pcap_version
[] = "unknown";
2569 #endif /* defined(_WIN32) || defined(HAVE_PCAP_VERSION) */
2570 #endif /* HAVE_PCAP_LIB_VERSION */
2571 const char *smi_version_string
;
2573 #ifdef HAVE_PCAP_LIB_VERSION
2575 (void)fprintf(stderr
, "%s version %s, based on tcpdump version %s\n", program_name
, WDversion
, version
);
2577 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
2579 (void)fprintf(stderr
, "%s\n",pcap_lib_version());
2580 #else /* HAVE_PCAP_LIB_VERSION */
2582 (void)fprintf(stderr
, "%s version %s, based on tcpdump version %s\n", program_name
, WDversion
, version
);
2583 (void)fprintf(stderr
, "WinPcap version %s, based on libpcap version %s\n",Wpcap_version
, pcap_version
);
2585 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
2586 (void)fprintf(stderr
, "libpcap version %s\n", pcap_version
);
2588 #endif /* HAVE_PCAP_LIB_VERSION */
2590 #if defined(HAVE_LIBCRYPTO) && defined(SSLEAY_VERSION)
2591 (void)fprintf (stderr
, "%s\n", SSLeay_version(SSLEAY_VERSION
));
2594 smi_version_string
= nd_smi_version_string();
2595 if (smi_version_string
!= NULL
)
2596 (void)fprintf (stderr
, "SMI-library: %s\n", smi_version_string
);
2598 #if defined(__SANITIZE_ADDRESS__)
2599 (void)fprintf (stderr
, "Compiled with AddressSanitizer/GCC.\n");
2600 #elif defined(__has_feature)
2601 # if __has_feature(address_sanitizer)
2602 (void)fprintf (stderr
, "Compiled with AddressSanitizer/CLang.\n");
2604 #endif /* __SANITIZE_ADDRESS__ or __has_feature */
2612 (void)fprintf(stderr
,
2613 "Usage: %s [-aAbd" D_FLAG
"efhH" I_FLAG J_FLAG
"KlLnNOpqStu" U_FLAG
"vxX#]" B_FLAG_USAGE
" [ -c count ]\n", program_name
);
2614 (void)fprintf(stderr
,
2615 "\t\t[ -C file_size ] [ -E algo:secret ] [ -F file ] [ -G seconds ]\n");
2616 (void)fprintf(stderr
,
2617 "\t\t[ -i interface ]" j_FLAG_USAGE
" [ -M secret ] [ --number ]\n");
2618 #ifdef HAVE_PCAP_SETDIRECTION
2619 (void)fprintf(stderr
,
2620 "\t\t[ -Q in|out|inout ]\n");
2622 (void)fprintf(stderr
,
2623 "\t\t[ -r file ] [ -s snaplen ] ");
2624 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
2625 (void)fprintf(stderr
, "[ --time-stamp-precision precision ]\n");
2626 (void)fprintf(stderr
,
2629 #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
2630 (void)fprintf(stderr
, "[ --immediate-mode ] ");
2632 (void)fprintf(stderr
, "[ -T type ] [ --version ] [ -V file ]\n");
2633 (void)fprintf(stderr
,
2634 "\t\t[ -w file ] [ -W filecount ] [ -y datalinktype ] [ -z postrotate-command ]\n");
2635 (void)fprintf(stderr
,
2636 "\t\t[ -Z user ] [ expression ]\n");
2640 * c-style: whitesmith