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 * Some older versions of Mac OS X may ship pcap.h from libpcap 0.6 with a
48 * libpcap based on 0.8. That means it has pcap_findalldevs() but the
49 * header doesn't define pcap_if_t, meaning that we can't actually *use*
52 #ifdef HAVE_PCAP_FINDALLDEVS
53 #ifndef HAVE_PCAP_IF_T
54 #undef HAVE_PCAP_FINDALLDEVS
58 #include <netdissect-stdinc.h>
67 #include <openssl/crypto.h>
70 #ifdef HAVE_GETOPT_LONG
73 #include "getopt_long.h"
75 /* Capsicum-specific code requires macros from <net/bpf.h>, which will fail
76 * to compile if <pcap.h> has already been included; including the headers
77 * in the opposite order works fine.
80 #include <sys/capability.h>
81 #include <sys/ioccom.h>
85 #include <libcasper.h>
86 #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 */
118 #include <sys/sysctl.h>
119 #endif /* __FreeBSD__ */
121 #include "netdissect.h"
122 #include "interface.h"
123 #include "addrtoname.h"
125 #include "setsignal.h"
126 #include "gmt2local.h"
127 #include "pcap-missing.h"
128 #include "ascii_strcasecmp.h"
133 #define PATH_MAX 1024
137 #define SIGNAL_REQ_INFO SIGINFO
139 #define SIGNAL_REQ_INFO SIGUSR1
142 static int Bflag
; /* buffer size */
143 static long Cflag
; /* rotate dump files after this many bytes */
144 static int Cflag_count
; /* Keep track of which file number we're writing */
145 static int Dflag
; /* list available devices and exit */
147 * This is exported because, in some versions of libpcap, if libpcap
148 * is built with optimizer debugging code (which is *NOT* the default
149 * configuration!), the library *imports*(!) a variable named dflag,
150 * under the expectation that tcpdump is exporting it, to govern
151 * how much debugging information to print when optimizing
152 * the generated BPF code.
154 * This is a horrible hack; newer versions of libpcap don't import
155 * dflag but, instead, *if* built with optimizer debugging code,
156 * *export* a routine to set that flag.
158 int dflag
; /* print filter code */
159 static int Gflag
; /* rotate dump files after this many seconds */
160 static int Gflag_count
; /* number of files created with Gflag rotation */
161 static time_t Gflag_time
; /* The last time_t the dump file was rotated. */
162 static int Lflag
; /* list available data link types and exit */
163 static int Iflag
; /* rfmon (monitor) mode */
164 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
165 static int Jflag
; /* list available time stamp types */
167 static int jflag
= -1; /* packet time stamp source */
168 static int pflag
; /* don't go promiscuous */
169 #ifdef HAVE_PCAP_SETDIRECTION
170 static int Qflag
= -1; /* restrict captured packet by send/receive direction */
172 static int Uflag
; /* "unbuffered" output of dump files */
173 static int Wflag
; /* recycle output files after this number of files */
174 static int WflagChars
;
175 static char *zflag
= NULL
; /* compress each savefile using a specified command (like gzip or bzip2) */
176 static int immediate_mode
;
178 static int infodelay
;
179 static int infoprint
;
184 cap_channel_t
*capdns
;
188 static void error(const char *, ...)
189 __attribute__((noreturn
))
190 #ifdef __ATTRIBUTE___FORMAT_OK
191 __attribute__((format (printf
, 1, 2)))
192 #endif /* __ATTRIBUTE___FORMAT_OK */
194 static void warning(const char *, ...)
195 #ifdef __ATTRIBUTE___FORMAT_OK
196 __attribute__((format (printf
, 1, 2)))
197 #endif /* __ATTRIBUTE___FORMAT_OK */
199 static void exit_tcpdump(int) __attribute__((noreturn
));
200 static RETSIGTYPE
cleanup(int);
201 static RETSIGTYPE
child_cleanup(int);
202 static void print_version(void);
203 static void print_usage(void);
204 static void show_tstamp_types_and_exit(pcap_t
*, const char *device
) __attribute__((noreturn
));
205 static void show_dlts_and_exit(pcap_t
*, const char *device
) __attribute__((noreturn
));
206 #ifdef HAVE_PCAP_FINDALLDEVS
207 static void show_devices_and_exit (void) __attribute__((noreturn
));
210 static void print_packet(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
211 static void dump_packet_and_trunc(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
212 static void dump_packet(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
213 static void droproot(const char *, const char *);
215 #ifdef SIGNAL_REQ_INFO
216 RETSIGTYPE
requestinfo(int);
219 #if defined(USE_WIN32_MM_TIMER)
220 #include <MMsystem.h>
221 static UINT timer_id
;
222 static void CALLBACK
verbose_stats_dump(UINT
, UINT
, DWORD_PTR
, DWORD_PTR
, DWORD_PTR
);
223 #elif defined(HAVE_ALARM)
224 static void verbose_stats_dump(int sig
);
227 static void info(int);
228 static u_int packets_captured
;
230 #ifdef HAVE_PCAP_FINDALLDEVS
231 static const struct tok status_flags
[] = {
233 { PCAP_IF_UP
, "Up" },
235 #ifdef PCAP_IF_RUNNING
236 { PCAP_IF_RUNNING
, "Running" },
238 { PCAP_IF_LOOPBACK
, "Loopback" },
245 static int supports_monitor_mode
;
253 char *CurrentFileName
;
261 #if defined(HAVE_PCAP_SET_PARSER_DEBUG)
263 * We have pcap_set_parser_debug() in libpcap; declare it (it's not declared
264 * by any libpcap header, because it's a special hack, only available if
265 * libpcap was configured to include it, and only intended for use by
266 * libpcap developers trying to debug the parser for filter expressions).
269 __declspec(dllimport
)
273 void pcap_set_parser_debug(int);
274 #elif defined(HAVE_PCAP_DEBUG) || defined(HAVE_YYDEBUG)
276 * We don't have pcap_set_parser_debug() in libpcap, but we do have
277 * pcap_debug or yydebug. Make a local version of pcap_set_parser_debug()
278 * to set the flag, and define HAVE_PCAP_SET_PARSER_DEBUG.
281 pcap_set_parser_debug(int value
)
283 #ifdef HAVE_PCAP_DEBUG
284 extern int pcap_debug
;
287 #else /* HAVE_PCAP_DEBUG */
291 #endif /* HAVE_PCAP_DEBUG */
294 #define HAVE_PCAP_SET_PARSER_DEBUG
297 #if defined(HAVE_PCAP_SET_OPTIMIZER_DEBUG)
299 * We have pcap_set_optimizer_debug() in libpcap; declare it (it's not declared
300 * by any libpcap header, because it's a special hack, only available if
301 * libpcap was configured to include it, and only intended for use by
302 * libpcap developers trying to debug the optimizer for filter expressions).
305 __declspec(dllimport
)
309 void pcap_set_optimizer_debug(int);
314 error(const char *fmt
, ...)
318 (void)fprintf(stderr
, "%s: ", program_name
);
320 (void)vfprintf(stderr
, fmt
, ap
);
325 (void)fputc('\n', stderr
);
333 warning(const char *fmt
, ...)
337 (void)fprintf(stderr
, "%s: WARNING: ", program_name
);
339 (void)vfprintf(stderr
, fmt
, ap
);
344 (void)fputc('\n', stderr
);
349 exit_tcpdump(int status
)
355 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
357 show_tstamp_types_and_exit(pcap_t
*pc
, const char *device
)
360 int *tstamp_types
= 0;
361 const char *tstamp_type_name
;
364 n_tstamp_types
= pcap_list_tstamp_types(pc
, &tstamp_types
);
365 if (n_tstamp_types
< 0)
366 error("%s", pcap_geterr(pc
));
368 if (n_tstamp_types
== 0) {
369 fprintf(stderr
, "Time stamp type cannot be set for %s\n",
373 fprintf(stderr
, "Time stamp types for %s (use option -j to set):\n",
375 for (i
= 0; i
< n_tstamp_types
; i
++) {
376 tstamp_type_name
= pcap_tstamp_type_val_to_name(tstamp_types
[i
]);
377 if (tstamp_type_name
!= NULL
) {
378 (void) fprintf(stderr
, " %s (%s)\n", tstamp_type_name
,
379 pcap_tstamp_type_val_to_description(tstamp_types
[i
]));
381 (void) fprintf(stderr
, " %d\n", tstamp_types
[i
]);
384 pcap_free_tstamp_types(tstamp_types
);
390 show_dlts_and_exit(pcap_t
*pc
, const char *device
)
394 const char *dlt_name
;
396 n_dlts
= pcap_list_datalinks(pc
, &dlts
);
398 error("%s", pcap_geterr(pc
));
399 else if (n_dlts
== 0 || !dlts
)
400 error("No data link types.");
403 * If the interface is known to support monitor mode, indicate
404 * whether these are the data link types available when not in
405 * monitor mode, if -I wasn't specified, or when in monitor mode,
406 * when -I was specified (the link-layer types available in
407 * monitor mode might be different from the ones available when
408 * not in monitor mode).
410 if (supports_monitor_mode
)
411 (void) fprintf(stderr
, "Data link types for %s %s (use option -y to set):\n",
413 Iflag
? "when in monitor mode" : "when not in monitor mode");
415 (void) fprintf(stderr
, "Data link types for %s (use option -y to set):\n",
418 for (i
= 0; i
< n_dlts
; i
++) {
419 dlt_name
= pcap_datalink_val_to_name(dlts
[i
]);
420 if (dlt_name
!= NULL
) {
421 (void) fprintf(stderr
, " %s (%s)", dlt_name
,
422 pcap_datalink_val_to_description(dlts
[i
]));
425 * OK, does tcpdump handle that type?
427 if (!has_printer(dlts
[i
]))
428 (void) fprintf(stderr
, " (printing not supported)");
429 fprintf(stderr
, "\n");
431 (void) fprintf(stderr
, " DLT %d (printing not supported)\n",
435 #ifdef HAVE_PCAP_FREE_DATALINKS
436 pcap_free_datalinks(dlts
);
441 #ifdef HAVE_PCAP_FINDALLDEVS
443 show_devices_and_exit (void)
445 pcap_if_t
*dev
, *devlist
;
446 char ebuf
[PCAP_ERRBUF_SIZE
];
449 if (pcap_findalldevs(&devlist
, ebuf
) < 0)
451 for (i
= 0, dev
= devlist
; dev
!= NULL
; i
++, dev
= dev
->next
) {
452 printf("%d.%s", i
+1, dev
->name
);
453 if (dev
->description
!= NULL
)
454 printf(" (%s)", dev
->description
);
456 printf(" [%s]", bittok2str(status_flags
, "none", dev
->flags
));
459 pcap_freealldevs(devlist
);
462 #endif /* HAVE_PCAP_FINDALLDEVS */
467 * Note that there we use all letters for short options except for g, k,
468 * o, and P, and those are used by other versions of tcpdump, and we should
469 * only use them for the same purposes that the other versions of tcpdump
472 * OS X tcpdump uses -g to force non--v output for IP to be on one
473 * line, making it more "g"repable;
475 * OS X tcpdump uses -k to specify that packet comments in pcap-ng files
478 * OpenBSD tcpdump uses -o to indicate that OS fingerprinting should be done
479 * for hosts sending TCP SYN packets;
481 * OS X tcpdump uses -P to indicate that -w should write pcap-ng rather
484 * OS X tcpdump also uses -Q to specify expressions that match packet
485 * metadata, including but not limited to the packet direction.
486 * The expression syntax is different from a simple "in|out|inout",
487 * and those expressions aren't accepted by OS X tcpdump, but the
488 * equivalents would be "in" = "dir=in", "out" = "dir=out", and
489 * "inout" = "dir=in or dir=out", and the parser could conceivably
490 * special-case "in", "out", and "inout" as expressions for backwards
491 * compatibility, so all is not (yet) lost.
495 * Set up flags that might or might not be supported depending on the
496 * version of libpcap we're using.
498 #if defined(HAVE_PCAP_CREATE) || defined(_WIN32)
500 #define B_FLAG_USAGE " [ -B size ]"
501 #else /* defined(HAVE_PCAP_CREATE) || defined(_WIN32) */
504 #endif /* defined(HAVE_PCAP_CREATE) || defined(_WIN32) */
506 #ifdef HAVE_PCAP_CREATE
508 #else /* HAVE_PCAP_CREATE */
510 #endif /* HAVE_PCAP_CREATE */
512 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
514 #define j_FLAG_USAGE " [ -j tstamptype ]"
516 #else /* PCAP_ERROR_TSTAMP_TYPE_NOTSUP */
520 #endif /* PCAP_ERROR_TSTAMP_TYPE_NOTSUP */
522 #ifdef HAVE_PCAP_FINDALLDEVS
528 #ifdef HAVE_PCAP_DUMP_FLUSH
534 #ifdef HAVE_PCAP_SETDIRECTION
540 #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:#"
545 * We do not currently have long options corresponding to all short
546 * options; we should probably pick appropriate option names for them.
548 * However, the short options where the number of times the option is
549 * specified matters, such as -v and -d and -t, should probably not
550 * just map to a long option, as saying
552 * tcpdump --verbose --verbose
554 * doesn't make sense; it should be --verbosity={N} or something such
557 * For long options with no corresponding short options, we define values
558 * outside the range of ASCII graphic characters, make that the last
559 * component of the entry for the long option, and have a case for that
560 * option in the switch statement.
562 #define OPTION_VERSION 128
563 #define OPTION_TSTAMP_PRECISION 129
564 #define OPTION_IMMEDIATE_MODE 130
566 static const struct option longopts
[] = {
567 #if defined(HAVE_PCAP_CREATE) || defined(_WIN32)
568 { "buffer-size", required_argument
, NULL
, 'B' },
570 { "list-interfaces", no_argument
, NULL
, 'D' },
571 { "help", no_argument
, NULL
, 'h' },
572 { "interface", required_argument
, NULL
, 'i' },
573 #ifdef HAVE_PCAP_CREATE
574 { "monitor-mode", no_argument
, NULL
, 'I' },
576 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
577 { "time-stamp-type", required_argument
, NULL
, 'j' },
578 { "list-time-stamp-types", no_argument
, NULL
, 'J' },
580 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
581 { "time-stamp-precision", required_argument
, NULL
, OPTION_TSTAMP_PRECISION
},
583 { "dont-verify-checksums", no_argument
, NULL
, 'K' },
584 { "list-data-link-types", no_argument
, NULL
, 'L' },
585 { "no-optimize", no_argument
, NULL
, 'O' },
586 { "no-promiscuous-mode", no_argument
, NULL
, 'p' },
587 #ifdef HAVE_PCAP_SETDIRECTION
588 { "direction", required_argument
, NULL
, 'Q' },
590 { "snapshot-length", required_argument
, NULL
, 's' },
591 { "absolute-tcp-sequence-numbers", no_argument
, NULL
, 'S' },
592 #ifdef HAVE_PCAP_DUMP_FLUSH
593 { "packet-buffered", no_argument
, NULL
, 'U' },
595 { "linktype", required_argument
, NULL
, 'y' },
596 #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
597 { "immediate-mode", no_argument
, NULL
, OPTION_IMMEDIATE_MODE
},
599 #ifdef HAVE_PCAP_SET_PARSER_DEBUG
600 { "debug-filter-parser", no_argument
, NULL
, 'Y' },
602 { "relinquish-privileges", required_argument
, NULL
, 'Z' },
603 { "number", no_argument
, NULL
, '#' },
604 { "version", no_argument
, NULL
, OPTION_VERSION
},
609 /* Drop root privileges and chroot if necessary */
611 droproot(const char *username
, const char *chroot_dir
)
613 struct passwd
*pw
= NULL
;
615 if (chroot_dir
&& !username
) {
616 fprintf(stderr
, "%s: Chroot without dropping root is insecure\n",
621 pw
= getpwnam(username
);
624 if (chroot(chroot_dir
) != 0 || chdir ("/") != 0) {
625 fprintf(stderr
, "%s: Couldn't chroot/chdir to '%.64s': %s\n",
626 program_name
, chroot_dir
, pcap_strerror(errno
));
630 #ifdef HAVE_LIBCAP_NG
632 int ret
= capng_change_id(pw
->pw_uid
, pw
->pw_gid
, CAPNG_NO_FLAG
);
634 fprintf(stderr
, "error : ret %d\n", ret
);
636 fprintf(stderr
, "dropped privs to %s\n", username
);
640 if (initgroups(pw
->pw_name
, pw
->pw_gid
) != 0 ||
641 setgid(pw
->pw_gid
) != 0 || setuid(pw
->pw_uid
) != 0) {
642 fprintf(stderr
, "%s: Couldn't change to '%.32s' uid=%lu gid=%lu: %s\n",
643 program_name
, username
,
644 (unsigned long)pw
->pw_uid
,
645 (unsigned long)pw
->pw_gid
,
646 pcap_strerror(errno
));
650 fprintf(stderr
, "dropped privs to %s\n", username
);
652 #endif /* HAVE_LIBCAP_NG */
655 fprintf(stderr
, "%s: Couldn't find user '%.32s'\n",
656 program_name
, username
);
659 #ifdef HAVE_LIBCAP_NG
660 /* We don't need CAP_SETUID, CAP_SETGID and CAP_SYS_CHROOT any more. */
663 CAPNG_EFFECTIVE
| CAPNG_PERMITTED
,
668 capng_apply(CAPNG_SELECT_BOTH
);
669 #endif /* HAVE_LIBCAP_NG */
690 MakeFilename(char *buffer
, char *orig_name
, int cnt
, int max_chars
)
692 char *filename
= malloc(PATH_MAX
+ 1);
693 if (filename
== NULL
)
694 error("Makefilename: malloc");
696 /* Process with strftime if Gflag is set. */
700 /* Convert Gflag_time to a usable format */
701 if ((local_tm
= localtime(&Gflag_time
)) == NULL
) {
702 error("MakeTimedFilename: localtime");
705 /* There's no good way to detect an error in strftime since a return
706 * value of 0 isn't necessarily failure.
708 strftime(filename
, PATH_MAX
, orig_name
, local_tm
);
710 strncpy(filename
, orig_name
, PATH_MAX
);
713 if (cnt
== 0 && max_chars
== 0)
714 strncpy(buffer
, filename
, PATH_MAX
+ 1);
716 if (snprintf(buffer
, PATH_MAX
+ 1, "%s%0*d", filename
, max_chars
, cnt
) > PATH_MAX
)
717 /* Report an error if the filename is too large */
718 error("too many output files or filename is too long (> %d)", PATH_MAX
);
723 get_next_file(FILE *VFile
, char *ptr
)
727 ret
= fgets(ptr
, PATH_MAX
, VFile
);
731 if (ptr
[strlen(ptr
) - 1] == '\n')
732 ptr
[strlen(ptr
) - 1] = '\0';
738 static cap_channel_t
*
741 cap_channel_t
*capcas
, *capdnsloc
;
742 const char *types
[1];
747 error("unable to create casper process");
748 capdnsloc
= cap_service_open(capcas
, "system.dns");
749 /* Casper capability no longer needed. */
751 if (capdnsloc
== NULL
)
752 error("unable to open system.dns service");
753 /* Limit system.dns to reverse DNS lookups. */
755 if (cap_dns_type_limit(capdnsloc
, types
, 1) < 0)
756 error("unable to limit access to system.dns service");
757 families
[0] = AF_INET
;
758 families
[1] = AF_INET6
;
759 if (cap_dns_family_limit(capdnsloc
, families
, 2) < 0)
760 error("unable to limit access to system.dns service");
764 #endif /* HAVE_CASPER */
766 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
768 tstamp_precision_from_string(const char *precision
)
770 if (strncmp(precision
, "nano", strlen("nano")) == 0)
771 return PCAP_TSTAMP_PRECISION_NANO
;
773 if (strncmp(precision
, "micro", strlen("micro")) == 0)
774 return PCAP_TSTAMP_PRECISION_MICRO
;
780 tstamp_precision_to_string(int precision
)
784 case PCAP_TSTAMP_PRECISION_MICRO
:
787 case PCAP_TSTAMP_PRECISION_NANO
:
798 * Ensure that, on a dump file's descriptor, we have all the rights
799 * necessary to make the standard I/O library work with an fdopen()ed
800 * FILE * from that descriptor.
802 * A long time ago, in a galaxy far far away, AT&T decided that, instead
803 * of providing separate APIs for getting and setting the FD_ flags on a
804 * descriptor, getting and setting the O_ flags on a descriptor, and
805 * locking files, they'd throw them all into a kitchen-sink fcntl() call
806 * along the lines of ioctl(), the fact that ioctl() operations are
807 * largely specific to particular character devices but fcntl() operations
808 * are either generic to all descriptors or generic to all descriptors for
809 * regular files nonwithstanding.
811 * The Capsicum people decided that fine-grained control of descriptor
812 * operations was required, so that you need to grant permission for
813 * reading, writing, seeking, and fcntl-ing. The latter, courtesy of
814 * AT&T's decision, means that "fcntl-ing" isn't a thing, but a motley
815 * collection of things, so there are *individual* fcntls for which
816 * permission needs to be granted.
818 * The FreeBSD standard I/O people implemented some optimizations that
819 * requires that the standard I/O routines be able to determine whether
820 * the descriptor for the FILE * is open append-only or not; as that
821 * descriptor could have come from an open() rather than an fopen(),
822 * that requires that it be able to do an F_GETFL fcntl() to read
825 * Tcpdump uses ftell() to determine how much data has been written
826 * to a file in order to, when used with -C, determine when it's time
827 * to rotate capture files. ftell() therefore needs to do an lseek()
828 * to find out the file offset and must, thanks to the aforementioned
829 * optimization, also know whether the descriptor is open append-only
832 * The net result of all the above is that we need to grant CAP_SEEK,
833 * CAP_WRITE, and CAP_FCNTL with the CAP_FCNTL_GETFL subcapability.
835 * Perhaps this is the universe's way of saying that either
837 * 1) there needs to be an fopenat() call and a pcap_dump_openat() call
838 * using it, so that Capsicum-capable tcpdump wouldn't need to do
843 * 2) there needs to be a cap_fdopen() call in the FreeBSD standard
844 * I/O library that knows what rights are needed by the standard
845 * I/O library, based on the open mode, and assigns them, perhaps
846 * with an additional argument indicating, for example, whether
847 * seeking should be allowed, so that tcpdump doesn't need to know
848 * what the standard I/O library happens to require this week.
851 set_dumper_capsicum_rights(pcap_dumper_t
*p
)
853 int fd
= fileno(pcap_dump_file(p
));
856 cap_rights_init(&rights
, CAP_SEEK
, CAP_WRITE
, CAP_FCNTL
);
857 if (cap_rights_limit(fd
, &rights
) < 0 && errno
!= ENOSYS
) {
858 error("unable to limit dump descriptor");
860 if (cap_fcntls_limit(fd
, CAP_FCNTL_GETFL
) < 0 && errno
!= ENOSYS
) {
861 error("unable to limit dump descriptor fcntls");
867 * Copy arg vector into a new buffer, concatenating arguments with spaces.
870 copy_argv(register char **argv
)
873 register u_int len
= 0;
882 len
+= strlen(*p
++) + 1;
884 buf
= (char *)malloc(len
);
886 error("copy_argv: malloc");
890 while ((src
= *p
++) != NULL
) {
891 while ((*dst
++ = *src
++) != '\0')
901 * On Windows, we need to open the file in binary mode, so that
902 * we get all the bytes specified by the size we get from "fstat()".
903 * On UNIX, that's not necessary. O_BINARY is defined on Windows;
904 * we define it as 0 if it's not defined, so it does nothing.
911 read_infile(char *fname
)
913 register int i
, fd
, cc
;
917 fd
= open(fname
, O_RDONLY
|O_BINARY
);
919 error("can't open %s: %s", fname
, pcap_strerror(errno
));
921 if (fstat(fd
, &buf
) < 0)
922 error("can't stat %s: %s", fname
, pcap_strerror(errno
));
924 cp
= malloc((u_int
)buf
.st_size
+ 1);
926 error("malloc(%d) for %s: %s", (u_int
)buf
.st_size
+ 1,
927 fname
, pcap_strerror(errno
));
928 cc
= read(fd
, cp
, (u_int
)buf
.st_size
);
930 error("read %s: %s", fname
, pcap_strerror(errno
));
931 if (cc
!= buf
.st_size
)
932 error("short read %s (%d != %d)", fname
, cc
, (int)buf
.st_size
);
935 /* replace "# comment" with spaces */
936 for (i
= 0; i
< cc
; i
++) {
938 while (i
< cc
&& cp
[i
] != '\n')
945 #ifdef HAVE_PCAP_FINDALLDEVS
947 parse_interface_number(const char *device
)
952 devnum
= strtol(device
, &end
, 10);
953 if (device
!= end
&& *end
== '\0') {
955 * It's all-numeric, but is it a valid number?
959 * No, it's not an ordinal.
961 error("Invalid adapter index");
966 * It's not all-numeric; return -1, so our caller
974 find_interface_by_number(long devnum
)
976 pcap_if_t
*dev
, *devlist
;
978 char ebuf
[PCAP_ERRBUF_SIZE
];
981 if (pcap_findalldevs(&devlist
, ebuf
) < 0)
984 * Look for the devnum-th entry in the list of devices (1-based).
986 for (i
= 0, dev
= devlist
; i
< devnum
-1 && dev
!= NULL
;
987 i
++, dev
= dev
->next
)
990 error("Invalid adapter index");
991 device
= strdup(dev
->name
);
992 pcap_freealldevs(devlist
);
998 open_interface(const char *device
, netdissect_options
*ndo
, char *ebuf
)
1001 #ifdef HAVE_PCAP_CREATE
1006 #ifdef HAVE_PCAP_CREATE
1007 pc
= pcap_create(device
, ebuf
);
1010 * If this failed with "No such device", that means
1011 * the interface doesn't exist; return NULL, so that
1012 * the caller can see whether the device name is
1013 * actually an interface index.
1015 if (strstr(ebuf
, "No such device") != NULL
)
1019 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
1021 show_tstamp_types_and_exit(pc
, device
);
1023 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
1024 status
= pcap_set_tstamp_precision(pc
, ndo
->ndo_tstamp_precision
);
1026 error("%s: Can't set %ssecond time stamp precision: %s",
1028 tstamp_precision_to_string(ndo
->ndo_tstamp_precision
),
1029 pcap_statustostr(status
));
1032 #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
1033 if (immediate_mode
) {
1034 status
= pcap_set_immediate_mode(pc
, 1);
1036 error("%s: Can't set immediate mode: %s",
1038 pcap_statustostr(status
));
1042 * Is this an interface that supports monitor mode?
1044 if (pcap_can_set_rfmon(pc
) == 1)
1045 supports_monitor_mode
= 1;
1047 supports_monitor_mode
= 0;
1048 if (ndo
->ndo_snaplen
!= 0) {
1050 * A snapshot length was explicitly specified;
1053 status
= pcap_set_snaplen(pc
, ndo
->ndo_snaplen
);
1055 error("%s: Can't set snapshot length: %s",
1056 device
, pcap_statustostr(status
));
1058 status
= pcap_set_promisc(pc
, !pflag
);
1060 error("%s: Can't set promiscuous mode: %s",
1061 device
, pcap_statustostr(status
));
1063 status
= pcap_set_rfmon(pc
, 1);
1065 error("%s: Can't set monitor mode: %s",
1066 device
, pcap_statustostr(status
));
1068 status
= pcap_set_timeout(pc
, 1000);
1070 error("%s: pcap_set_timeout failed: %s",
1071 device
, pcap_statustostr(status
));
1073 status
= pcap_set_buffer_size(pc
, Bflag
);
1075 error("%s: Can't set buffer size: %s",
1076 device
, pcap_statustostr(status
));
1078 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
1080 status
= pcap_set_tstamp_type(pc
, jflag
);
1082 error("%s: Can't set time stamp type: %s",
1083 device
, pcap_statustostr(status
));
1086 status
= pcap_activate(pc
);
1089 * pcap_activate() failed.
1091 cp
= pcap_geterr(pc
);
1092 if (status
== PCAP_ERROR
)
1094 else if (status
== PCAP_ERROR_NO_SUCH_DEVICE
) {
1096 * Return an error for our caller to handle.
1098 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "%s: %s\n(%s)",
1099 device
, pcap_statustostr(status
), cp
);
1102 } else if (status
== PCAP_ERROR_PERM_DENIED
&& *cp
!= '\0')
1103 error("%s: %s\n(%s)", device
,
1104 pcap_statustostr(status
), cp
);
1106 else if (status
== PCAP_ERROR_RFMON_NOTSUP
&&
1107 strncmp(device
, "wlan", 4) == 0) {
1108 char parent
[8], newdev
[8];
1110 size_t s
= sizeof(parent
);
1112 snprintf(sysctl
, sizeof(sysctl
),
1113 "net.wlan.%d.%%parent", atoi(device
+ 4));
1114 sysctlbyname(sysctl
, parent
, &s
, NULL
, 0);
1115 strlcpy(newdev
, device
, sizeof(newdev
));
1116 /* Suggest a new wlan device. */
1117 /* FIXME: incrementing the index this way is not going to work well
1118 * when the index is 9 or greater but the only consequence in this
1119 * specific case would be an error message that looks a bit odd.
1121 newdev
[strlen(newdev
)-1]++;
1122 error("%s is not a monitor mode VAP\n"
1123 "To create a new monitor mode VAP use:\n"
1124 " ifconfig %s create wlandev %s wlanmode monitor\n"
1125 "and use %s as the tcpdump interface",
1126 device
, newdev
, parent
, newdev
);
1130 error("%s: %s", device
,
1131 pcap_statustostr(status
));
1132 } else if (status
> 0) {
1134 * pcap_activate() succeeded, but it's warning us
1135 * of a problem it had.
1137 cp
= pcap_geterr(pc
);
1138 if (status
== PCAP_WARNING
)
1140 else if (status
== PCAP_WARNING_PROMISC_NOTSUP
&&
1142 warning("%s: %s\n(%s)", device
,
1143 pcap_statustostr(status
), cp
);
1145 warning("%s: %s", device
,
1146 pcap_statustostr(status
));
1148 #ifdef HAVE_PCAP_SETDIRECTION
1150 status
= pcap_setdirection(pc
, Qflag
);
1152 error("%s: pcap_setdirection() failed: %s",
1153 device
, pcap_geterr(pc
));
1155 #endif /* HAVE_PCAP_SETDIRECTION */
1156 #else /* HAVE_PCAP_CREATE */
1159 * If no snapshot length was specified, or a length of 0 was
1160 * specified, default to 256KB.
1162 if (ndo
->ndo_snaplen
== 0)
1163 ndo
->ndo_snaplen
= 262144;
1164 pc
= pcap_open_live(device
, ndo
->ndo_snaplen
, !pflag
, 1000, ebuf
);
1167 * If this failed with "No such device", that means
1168 * the interface doesn't exist; return NULL, so that
1169 * the caller can see whether the device name is
1170 * actually an interface index.
1172 if (strstr(ebuf
, "No such device") != NULL
)
1177 warning("%s", ebuf
);
1178 #endif /* HAVE_PCAP_CREATE */
1184 main(int argc
, char **argv
)
1186 register int cnt
, op
, i
;
1187 bpf_u_int32 localnet
=0 , netmask
= 0;
1188 int timezone_offset
= 0;
1189 register char *cp
, *infile
, *cmdbuf
, *device
, *RFileName
, *VFileName
, *WFileName
;
1190 pcap_handler callback
;
1192 const char *dlt_name
;
1193 struct bpf_program fcode
;
1195 RETSIGTYPE (*oldhandler
)(int);
1197 struct dump_info dumpinfo
;
1198 u_char
*pcap_userdata
;
1199 char ebuf
[PCAP_ERRBUF_SIZE
];
1200 char VFileLine
[PATH_MAX
+ 1];
1201 char *username
= NULL
;
1202 char *chroot_dir
= NULL
;
1205 #ifdef HAVE_PCAP_FINDALLDEVS
1211 #ifdef HAVE_CAPSICUM
1212 cap_rights_t rights
;
1214 #endif /* HAVE_CAPSICUM */
1215 int Oflag
= 1; /* run filter code optimizer */
1217 const char *yflag_dlt_name
= NULL
;
1219 netdissect_options Ndo
;
1220 netdissect_options
*ndo
= &Ndo
;
1223 * Initialize the netdissect code.
1225 if (nd_init(ebuf
, sizeof ebuf
) == -1)
1228 memset(ndo
, 0, sizeof(*ndo
));
1229 ndo_set_function_pointers(ndo
);
1239 if ((cp
= strrchr(argv
[0], '/')) != NULL
)
1240 ndo
->program_name
= program_name
= cp
+ 1;
1242 ndo
->program_name
= program_name
= argv
[0];
1245 if (pcap_wsockinit() != 0)
1246 error("Attempting to initialize Winsock failed");
1250 * On platforms where the CPU doesn't support unaligned loads,
1251 * force unaligned accesses to abort with SIGBUS, rather than
1252 * being fixed up (slowly) by the OS kernel; on those platforms,
1253 * misaligned accesses are bugs, and we want tcpdump to crash so
1254 * that the bugs are reported.
1256 if (abort_on_misalignment(ebuf
, sizeof(ebuf
)) < 0)
1260 (op
= getopt_long(argc
, argv
, SHORTOPTS
, longopts
, NULL
)) != -1)
1264 /* compatibility for old -a */
1275 #if defined(HAVE_PCAP_CREATE) || defined(_WIN32)
1277 Bflag
= atoi(optarg
)*1024;
1279 error("invalid packet buffer size %s", optarg
);
1281 #endif /* defined(HAVE_PCAP_CREATE) || defined(_WIN32) */
1286 error("invalid packet count %s", optarg
);
1290 Cflag
= atoi(optarg
) * 1000000;
1292 error("invalid file size %s", optarg
);
1312 #ifndef HAVE_LIBCRYPTO
1313 warning("crypto code not compiled in");
1315 ndo
->ndo_espsecret
= optarg
;
1327 Gflag
= atoi(optarg
);
1329 error("invalid number of seconds %s", optarg
);
1331 /* We will create one file initially. */
1334 /* Grab the current time for rotation use. */
1335 if ((Gflag_time
= time(NULL
)) == (time_t)-1) {
1336 error("main: can't get current time: %s",
1337 pcap_strerror(errno
));
1354 #ifdef HAVE_PCAP_CREATE
1358 #endif /* HAVE_PCAP_CREATE */
1360 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
1362 jflag
= pcap_tstamp_type_name_to_val(optarg
);
1364 error("invalid time stamp type %s", optarg
);
1375 * _IOLBF is the same as _IOFBF in Microsoft's C
1376 * libraries; the only alternative they offer
1379 * XXX - this should really be checking for MSVC++,
1380 * not _WIN32, if, for example, MinGW has its own
1381 * C library that is more UNIX-compatible.
1383 setvbuf(stdout
, NULL
, _IONBF
, 0);
1385 #ifdef HAVE_SETLINEBUF
1388 setvbuf(stdout
, NULL
, _IOLBF
, 0);
1398 if (nd_have_smi_support()) {
1399 if (nd_load_smi_module(optarg
, ebuf
, sizeof ebuf
) == -1)
1402 (void)fprintf(stderr
, "%s: ignoring option `-m %s' ",
1403 program_name
, optarg
);
1404 (void)fprintf(stderr
, "(no libsmi support)\n");
1409 /* TCP-MD5 shared secret */
1410 #ifndef HAVE_LIBCRYPTO
1411 warning("crypto code not compiled in");
1413 ndo
->ndo_sigsecret
= optarg
;
1434 ++ndo
->ndo_suppress_default_print
;
1437 #ifdef HAVE_PCAP_SETDIRECTION
1439 if (ascii_strcasecmp(optarg
, "in") == 0)
1441 else if (ascii_strcasecmp(optarg
, "out") == 0)
1443 else if (ascii_strcasecmp(optarg
, "inout") == 0)
1444 Qflag
= PCAP_D_INOUT
;
1446 error("unknown capture direction `%s'", optarg
);
1448 #endif /* HAVE_PCAP_SETDIRECTION */
1455 ndo
->ndo_snaplen
= strtol(optarg
, &end
, 0);
1456 if (optarg
== end
|| *end
!= '\0'
1457 || ndo
->ndo_snaplen
< 0 || ndo
->ndo_snaplen
> MAXIMUM_SNAPLEN
)
1458 error("invalid snaplen %s", optarg
);
1470 if (ascii_strcasecmp(optarg
, "vat") == 0)
1471 ndo
->ndo_packettype
= PT_VAT
;
1472 else if (ascii_strcasecmp(optarg
, "wb") == 0)
1473 ndo
->ndo_packettype
= PT_WB
;
1474 else if (ascii_strcasecmp(optarg
, "rpc") == 0)
1475 ndo
->ndo_packettype
= PT_RPC
;
1476 else if (ascii_strcasecmp(optarg
, "rtp") == 0)
1477 ndo
->ndo_packettype
= PT_RTP
;
1478 else if (ascii_strcasecmp(optarg
, "rtcp") == 0)
1479 ndo
->ndo_packettype
= PT_RTCP
;
1480 else if (ascii_strcasecmp(optarg
, "snmp") == 0)
1481 ndo
->ndo_packettype
= PT_SNMP
;
1482 else if (ascii_strcasecmp(optarg
, "cnfp") == 0)
1483 ndo
->ndo_packettype
= PT_CNFP
;
1484 else if (ascii_strcasecmp(optarg
, "tftp") == 0)
1485 ndo
->ndo_packettype
= PT_TFTP
;
1486 else if (ascii_strcasecmp(optarg
, "aodv") == 0)
1487 ndo
->ndo_packettype
= PT_AODV
;
1488 else if (ascii_strcasecmp(optarg
, "carp") == 0)
1489 ndo
->ndo_packettype
= PT_CARP
;
1490 else if (ascii_strcasecmp(optarg
, "radius") == 0)
1491 ndo
->ndo_packettype
= PT_RADIUS
;
1492 else if (ascii_strcasecmp(optarg
, "zmtp1") == 0)
1493 ndo
->ndo_packettype
= PT_ZMTP1
;
1494 else if (ascii_strcasecmp(optarg
, "vxlan") == 0)
1495 ndo
->ndo_packettype
= PT_VXLAN
;
1496 else if (ascii_strcasecmp(optarg
, "pgm") == 0)
1497 ndo
->ndo_packettype
= PT_PGM
;
1498 else if (ascii_strcasecmp(optarg
, "pgm_zmtp1") == 0)
1499 ndo
->ndo_packettype
= PT_PGM_ZMTP1
;
1500 else if (ascii_strcasecmp(optarg
, "lmp") == 0)
1501 ndo
->ndo_packettype
= PT_LMP
;
1502 else if (ascii_strcasecmp(optarg
, "resp") == 0)
1503 ndo
->ndo_packettype
= PT_RESP
;
1505 error("unknown packet type `%s'", optarg
);
1512 #ifdef HAVE_PCAP_DUMP_FLUSH
1531 Wflag
= atoi(optarg
);
1533 error("invalid number of output files %s", optarg
);
1534 WflagChars
= getWflagChars(Wflag
);
1539 ++ndo
->ndo_suppress_default_print
;
1544 ++ndo
->ndo_suppress_default_print
;
1548 yflag_dlt_name
= optarg
;
1550 pcap_datalink_name_to_val(yflag_dlt_name
);
1552 error("invalid data link type %s", yflag_dlt_name
);
1555 #ifdef HAVE_PCAP_SET_PARSER_DEBUG
1558 /* Undocumented flag */
1559 pcap_set_parser_debug(1);
1572 ndo
->ndo_packet_number
= 1;
1575 case OPTION_VERSION
:
1580 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
1581 case OPTION_TSTAMP_PRECISION
:
1582 ndo
->ndo_tstamp_precision
= tstamp_precision_from_string(optarg
);
1583 if (ndo
->ndo_tstamp_precision
< 0)
1584 error("unsupported time stamp precision");
1588 #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
1589 case OPTION_IMMEDIATE_MODE
:
1600 #ifdef HAVE_PCAP_FINDALLDEVS
1602 show_devices_and_exit();
1605 switch (ndo
->ndo_tflag
) {
1607 case 0: /* Default */
1608 case 4: /* Default + Date*/
1609 timezone_offset
= gmt2local(0);
1612 case 1: /* No time stamp */
1613 case 2: /* Unix timeval style */
1614 case 3: /* Microseconds since previous packet */
1615 case 5: /* Microseconds since first packet */
1618 default: /* Not supported */
1619 error("only -t, -tt, -ttt, -tttt and -ttttt are supported");
1623 if (ndo
->ndo_fflag
!= 0 && (VFileName
!= NULL
|| RFileName
!= NULL
))
1624 error("-f can not be used with -V or -r");
1626 if (VFileName
!= NULL
&& RFileName
!= NULL
)
1627 error("-V and -r are mutually exclusive.");
1629 #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
1631 * If we're printing dissected packets to the standard output
1632 * rather than saving raw packets to a file, and the standard
1633 * output is a terminal, use immediate mode, as the user's
1634 * probably expecting to see packets pop up immediately.
1636 if (WFileName
== NULL
&& isatty(1))
1641 /* if run as root, prepare for chrooting */
1642 if (getuid() == 0 || geteuid() == 0) {
1643 /* future extensibility for cmd-line arguments */
1645 chroot_dir
= WITH_CHROOT
;
1650 /* if run as root, prepare for dropping root privileges */
1651 if (getuid() == 0 || geteuid() == 0) {
1652 /* Run with '-Z root' to restore old behaviour */
1654 username
= WITH_USER
;
1658 if (RFileName
!= NULL
|| VFileName
!= NULL
) {
1660 * If RFileName is non-null, it's the pathname of a
1661 * savefile to read. If VFileName is non-null, it's
1662 * the pathname of a file containing a list of pathnames
1663 * (one per line) of savefiles to read.
1665 * In either case, we're reading a savefile, not doing
1670 * We don't need network access, so relinquish any set-UID
1671 * or set-GID privileges we have (if any).
1673 * We do *not* want set-UID privileges when opening a
1674 * trace file, as that might let the user read other
1675 * people's trace files (especially if we're set-UID
1678 if (setgid(getgid()) != 0 || setuid(getuid()) != 0 )
1679 fprintf(stderr
, "Warning: setgid/setuid failed !\n");
1681 if (VFileName
!= NULL
) {
1682 if (VFileName
[0] == '-' && VFileName
[1] == '\0')
1685 VFile
= fopen(VFileName
, "r");
1688 error("Unable to open file: %s\n", pcap_strerror(errno
));
1690 ret
= get_next_file(VFile
, VFileLine
);
1692 error("Nothing in %s\n", VFileName
);
1693 RFileName
= VFileLine
;
1696 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
1697 pd
= pcap_open_offline_with_tstamp_precision(RFileName
,
1698 ndo
->ndo_tstamp_precision
, ebuf
);
1700 pd
= pcap_open_offline(RFileName
, ebuf
);
1705 #ifdef HAVE_CAPSICUM
1706 cap_rights_init(&rights
, CAP_READ
);
1707 if (cap_rights_limit(fileno(pcap_file(pd
)), &rights
) < 0 &&
1709 error("unable to limit pcap descriptor");
1712 dlt
= pcap_datalink(pd
);
1713 dlt_name
= pcap_datalink_val_to_name(dlt
);
1714 if (dlt_name
== NULL
) {
1715 fprintf(stderr
, "reading from file %s, link-type %u\n",
1719 "reading from file %s, link-type %s (%s)\n",
1720 RFileName
, dlt_name
,
1721 pcap_datalink_val_to_description(dlt
));
1725 * We're doing a live capture.
1727 if (device
== NULL
) {
1729 * No interface was specified. Pick one.
1731 #ifdef HAVE_PCAP_FINDALLDEVS
1733 * Find the list of interfaces, and pick
1734 * the first interface.
1736 if (pcap_findalldevs(&devlist
, ebuf
) >= 0 &&
1738 device
= strdup(devlist
->name
);
1739 pcap_freealldevs(devlist
);
1741 #else /* HAVE_PCAP_FINDALLDEVS */
1743 * Use whatever interface pcap_lookupdev()
1746 device
= pcap_lookupdev(ebuf
);
1753 * Try to open the interface with the specified name.
1755 pd
= open_interface(device
, ndo
, ebuf
);
1758 * That failed. If we can get a list of
1759 * interfaces, and the interface name
1760 * is purely numeric, try to use it as
1761 * a 1-based index in the list of
1764 #ifdef HAVE_PCAP_FINDALLDEVS
1765 devnum
= parse_interface_number(device
);
1768 * It's not a number; just report
1769 * the open error and fail.
1775 * OK, it's a number; try to find the
1776 * interface with that index, and try
1779 * find_interface_by_number() exits if it
1780 * couldn't be found.
1782 device
= find_interface_by_number(devnum
);
1783 pd
= open_interface(device
, ndo
, ebuf
);
1786 #else /* HAVE_PCAP_FINDALLDEVS */
1788 * We can't get a list of interfaces; just
1792 #endif /* HAVE_PCAP_FINDALLDEVS */
1796 * Let user own process after capture device has
1800 if (setgid(getgid()) != 0 || setuid(getuid()) != 0)
1801 fprintf(stderr
, "Warning: setgid/setuid failed !\n");
1803 #if !defined(HAVE_PCAP_CREATE) && defined(_WIN32)
1805 if(pcap_setbuff(pd
, Bflag
)==-1){
1806 error("%s", pcap_geterr(pd
));
1808 #endif /* !defined(HAVE_PCAP_CREATE) && defined(_WIN32) */
1810 show_dlts_and_exit(pd
, device
);
1811 if (yflag_dlt
>= 0) {
1812 #ifdef HAVE_PCAP_SET_DATALINK
1813 if (pcap_set_datalink(pd
, yflag_dlt
) < 0)
1814 error("%s", pcap_geterr(pd
));
1817 * We don't actually support changing the
1818 * data link type, so we only let them
1819 * set it to what it already is.
1821 if (yflag_dlt
!= pcap_datalink(pd
)) {
1822 error("%s is not one of the DLTs supported by this device\n",
1826 (void)fprintf(stderr
, "%s: data link type %s\n",
1827 program_name
, yflag_dlt_name
);
1828 (void)fflush(stderr
);
1830 i
= pcap_snapshot(pd
);
1831 if (ndo
->ndo_snaplen
< i
) {
1832 if (ndo
->ndo_snaplen
!= 0)
1833 warning("snaplen raised from %d to %d", ndo
->ndo_snaplen
, i
);
1834 ndo
->ndo_snaplen
= i
;
1835 } else if (ndo
->ndo_snaplen
> i
) {
1836 warning("snaplen lowered from %d to %d", ndo
->ndo_snaplen
, i
);
1837 ndo
->ndo_snaplen
= i
;
1839 if(ndo
->ndo_fflag
!= 0) {
1840 if (pcap_lookupnet(device
, &localnet
, &netmask
, ebuf
) < 0) {
1841 warning("foreign (-f) flag used but: %s", ebuf
);
1847 cmdbuf
= read_infile(infile
);
1849 cmdbuf
= copy_argv(&argv
[optind
]);
1851 #ifdef HAVE_PCAP_SET_OPTIMIZER_DEBUG
1852 pcap_set_optimizer_debug(dflag
);
1854 if (pcap_compile(pd
, &fcode
, cmdbuf
, Oflag
, netmask
) < 0)
1855 error("%s", pcap_geterr(pd
));
1857 bpf_dump(&fcode
, dflag
);
1860 pcap_freecode(&fcode
);
1865 if (!ndo
->ndo_nflag
)
1866 capdns
= capdns_setup();
1867 #endif /* HAVE_CASPER */
1869 init_print(ndo
, localnet
, netmask
, timezone_offset
);
1872 (void)setsignal(SIGPIPE
, cleanup
);
1873 (void)setsignal(SIGTERM
, cleanup
);
1874 (void)setsignal(SIGINT
, cleanup
);
1876 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
1877 (void)setsignal(SIGCHLD
, child_cleanup
);
1879 /* Cooperate with nohup(1) */
1881 if ((oldhandler
= setsignal(SIGHUP
, cleanup
)) != SIG_DFL
)
1882 (void)setsignal(SIGHUP
, oldhandler
);
1887 * If a user name was specified with "-Z", attempt to switch to
1888 * that user's UID. This would probably be used with sudo,
1889 * to allow tcpdump to be run in a special restricted
1890 * account (if you just want to allow users to open capture
1891 * devices, and can't just give users that permission,
1892 * you'd make tcpdump set-UID or set-GID).
1894 * Tcpdump doesn't necessarily write only to one savefile;
1895 * the general only way to allow a -Z instance to write to
1896 * savefiles as the user under whose UID it's run, rather
1897 * than as the user specified with -Z, would thus be to switch
1898 * to the original user ID before opening a capture file and
1899 * then switch back to the -Z user ID after opening the savefile.
1900 * Switching to the -Z user ID only after opening the first
1901 * savefile doesn't handle the general case.
1904 if (getuid() == 0 || geteuid() == 0) {
1905 #ifdef HAVE_LIBCAP_NG
1906 /* Initialize capng */
1907 capng_clear(CAPNG_SELECT_BOTH
);
1911 CAPNG_PERMITTED
| CAPNG_EFFECTIVE
,
1919 CAPNG_PERMITTED
| CAPNG_EFFECTIVE
,
1927 CAPNG_PERMITTED
| CAPNG_EFFECTIVE
,
1931 capng_apply(CAPNG_SELECT_BOTH
);
1932 #endif /* HAVE_LIBCAP_NG */
1933 if (username
|| chroot_dir
)
1934 droproot(username
, chroot_dir
);
1939 if (pcap_setfilter(pd
, &fcode
) < 0)
1940 error("%s", pcap_geterr(pd
));
1941 #ifdef HAVE_CAPSICUM
1942 if (RFileName
== NULL
&& VFileName
== NULL
) {
1943 static const unsigned long cmds
[] = { BIOCGSTATS
, BIOCROTZBUF
};
1946 * The various libpcap devices use a combination of
1947 * read (bpf), ioctl (bpf, netmap), poll (netmap)
1948 * so we add the relevant access rights.
1950 cap_rights_init(&rights
, CAP_IOCTL
, CAP_READ
, CAP_EVENT
);
1951 if (cap_rights_limit(pcap_fileno(pd
), &rights
) < 0 &&
1953 error("unable to limit pcap descriptor");
1955 if (cap_ioctls_limit(pcap_fileno(pd
), cmds
,
1956 sizeof(cmds
) / sizeof(cmds
[0])) < 0 && errno
!= ENOSYS
) {
1957 error("unable to limit ioctls on pcap descriptor");
1963 /* Do not exceed the default PATH_MAX for files. */
1964 dumpinfo
.CurrentFileName
= (char *)malloc(PATH_MAX
+ 1);
1966 if (dumpinfo
.CurrentFileName
== NULL
)
1967 error("malloc of dumpinfo.CurrentFileName");
1969 /* We do not need numbering for dumpfiles if Cflag isn't set. */
1971 MakeFilename(dumpinfo
.CurrentFileName
, WFileName
, 0, WflagChars
);
1973 MakeFilename(dumpinfo
.CurrentFileName
, WFileName
, 0, 0);
1975 p
= pcap_dump_open(pd
, dumpinfo
.CurrentFileName
);
1976 #ifdef HAVE_LIBCAP_NG
1977 /* Give up CAP_DAC_OVERRIDE capability.
1978 * Only allow it to be restored if the -C or -G flag have been
1979 * set since we may need to create more files later on.
1983 (Cflag
|| Gflag
? 0 : CAPNG_PERMITTED
)
1987 capng_apply(CAPNG_SELECT_BOTH
);
1988 #endif /* HAVE_LIBCAP_NG */
1990 error("%s", pcap_geterr(pd
));
1991 #ifdef HAVE_CAPSICUM
1992 set_dumper_capsicum_rights(p
);
1994 if (Cflag
!= 0 || Gflag
!= 0) {
1995 #ifdef HAVE_CAPSICUM
1996 dumpinfo
.WFileName
= strdup(basename(WFileName
));
1997 if (dumpinfo
.WFileName
== NULL
) {
1998 error("Unable to allocate memory for file %s",
2001 dumpinfo
.dirfd
= open(dirname(WFileName
),
2002 O_DIRECTORY
| O_RDONLY
);
2003 if (dumpinfo
.dirfd
< 0) {
2004 error("unable to open directory %s",
2005 dirname(WFileName
));
2007 cap_rights_init(&rights
, CAP_CREATE
, CAP_FCNTL
,
2008 CAP_FTRUNCATE
, CAP_LOOKUP
, CAP_SEEK
, CAP_WRITE
);
2009 if (cap_rights_limit(dumpinfo
.dirfd
, &rights
) < 0 &&
2011 error("unable to limit directory rights");
2013 if (cap_fcntls_limit(dumpinfo
.dirfd
, CAP_FCNTL_GETFL
) < 0 &&
2015 error("unable to limit dump descriptor fcntls");
2017 #else /* !HAVE_CAPSICUM */
2018 dumpinfo
.WFileName
= WFileName
;
2020 callback
= dump_packet_and_trunc
;
2023 pcap_userdata
= (u_char
*)&dumpinfo
;
2025 callback
= dump_packet
;
2026 pcap_userdata
= (u_char
*)p
;
2028 #ifdef HAVE_PCAP_DUMP_FLUSH
2033 dlt
= pcap_datalink(pd
);
2034 ndo
->ndo_if_printer
= get_if_printer(ndo
, dlt
);
2035 callback
= print_packet
;
2036 pcap_userdata
= (u_char
*)ndo
;
2039 #ifdef SIGNAL_REQ_INFO
2041 * We can't get statistics when reading from a file rather
2042 * than capturing from a device.
2044 if (RFileName
== NULL
)
2045 (void)setsignal(SIGNAL_REQ_INFO
, requestinfo
);
2048 if (ndo
->ndo_vflag
> 0 && WFileName
) {
2050 * When capturing to a file, "-v" means tcpdump should,
2051 * every 10 seconds, "v"erbosely report the number of
2054 #ifdef USE_WIN32_MM_TIMER
2055 /* call verbose_stats_dump() each 1000 +/-100msec */
2056 timer_id
= timeSetEvent(1000, 100, verbose_stats_dump
, 0, TIME_PERIODIC
);
2057 setvbuf(stderr
, NULL
, _IONBF
, 0);
2058 #elif defined(HAVE_ALARM)
2059 (void)setsignal(SIGALRM
, verbose_stats_dump
);
2064 if (RFileName
== NULL
) {
2066 * Live capture (if -V was specified, we set RFileName
2067 * to a file from the -V file). Print a message to
2068 * the standard error on UN*X.
2070 if (!ndo
->ndo_vflag
&& !WFileName
) {
2071 (void)fprintf(stderr
,
2072 "%s: verbose output suppressed, use -v or -vv for full protocol decode\n",
2075 (void)fprintf(stderr
, "%s: ", program_name
);
2076 dlt
= pcap_datalink(pd
);
2077 dlt_name
= pcap_datalink_val_to_name(dlt
);
2078 if (dlt_name
== NULL
) {
2079 (void)fprintf(stderr
, "listening on %s, link-type %u, capture size %u bytes\n",
2080 device
, dlt
, ndo
->ndo_snaplen
);
2082 (void)fprintf(stderr
, "listening on %s, link-type %s (%s), capture size %u bytes\n",
2084 pcap_datalink_val_to_description(dlt
), ndo
->ndo_snaplen
);
2086 (void)fflush(stderr
);
2089 #ifdef HAVE_CAPSICUM
2090 cansandbox
= (VFileName
== NULL
&& zflag
== NULL
);
2092 cansandbox
= (cansandbox
&& (ndo
->ndo_nflag
|| capdns
!= NULL
));
2094 cansandbox
= (cansandbox
&& ndo
->ndo_nflag
);
2095 #endif /* HAVE_CASPER */
2096 if (cansandbox
&& cap_enter() < 0 && errno
!= ENOSYS
)
2097 error("unable to enter the capability mode");
2098 #endif /* HAVE_CAPSICUM */
2101 status
= pcap_loop(pd
, cnt
, callback
, pcap_userdata
);
2102 if (WFileName
== NULL
) {
2104 * We're printing packets. Flush the printed output,
2105 * so it doesn't get intermingled with error output.
2109 * We got interrupted, so perhaps we didn't
2110 * manage to finish a line we were printing.
2111 * Print an extra newline, just in case.
2115 (void)fflush(stdout
);
2119 * We got interrupted. If we are reading multiple
2120 * files (via -V) set these so that we stop.
2129 (void)fprintf(stderr
, "%s: pcap_loop: %s\n",
2130 program_name
, pcap_geterr(pd
));
2132 if (RFileName
== NULL
) {
2134 * We're doing a live capture. Report the capture
2140 if (VFileName
!= NULL
) {
2141 ret
= get_next_file(VFile
, VFileLine
);
2145 RFileName
= VFileLine
;
2146 pd
= pcap_open_offline(RFileName
, ebuf
);
2149 #ifdef HAVE_CAPSICUM
2150 cap_rights_init(&rights
, CAP_READ
);
2151 if (cap_rights_limit(fileno(pcap_file(pd
)),
2152 &rights
) < 0 && errno
!= ENOSYS
) {
2153 error("unable to limit pcap descriptor");
2156 new_dlt
= pcap_datalink(pd
);
2157 if (new_dlt
!= dlt
) {
2159 * The new file has a different
2160 * link-layer header type from the
2163 if (WFileName
!= NULL
) {
2165 * We're writing raw packets
2166 * that match the filter to
2167 * a pcap file. pcap files
2168 * don't support multiple
2169 * different link-layer
2170 * header types, so we fail
2173 error("%s: new dlt does not match original", RFileName
);
2177 * We're printing the decoded packets;
2178 * switch to the new DLT.
2180 * To do that, we need to change
2181 * the printer, change the DLT name,
2182 * and recompile the filter with
2186 ndo
->ndo_if_printer
= get_if_printer(ndo
, dlt
);
2187 if (pcap_compile(pd
, &fcode
, cmdbuf
, Oflag
, netmask
) < 0)
2188 error("%s", pcap_geterr(pd
));
2192 * Set the filter on the new file.
2194 if (pcap_setfilter(pd
, &fcode
) < 0)
2195 error("%s", pcap_geterr(pd
));
2198 * Report the new file.
2200 dlt_name
= pcap_datalink_val_to_name(dlt
);
2201 if (dlt_name
== NULL
) {
2202 fprintf(stderr
, "reading from file %s, link-type %u\n",
2206 "reading from file %s, link-type %s (%s)\n",
2207 RFileName
, dlt_name
,
2208 pcap_datalink_val_to_description(dlt
));
2213 while (ret
!= NULL
);
2216 pcap_freecode(&fcode
);
2217 exit_tcpdump(status
== -1 ? 1 : 0);
2220 /* make a clean exit on interrupts */
2222 cleanup(int signo _U_
)
2224 #ifdef USE_WIN32_MM_TIMER
2226 timeKillEvent(timer_id
);
2228 #elif defined(HAVE_ALARM)
2232 #ifdef HAVE_PCAP_BREAKLOOP
2234 * We have "pcap_breakloop()"; use it, so that we do as little
2235 * as possible in the signal handler (it's probably not safe
2236 * to do anything with standard I/O streams in a signal handler -
2237 * the ANSI C standard doesn't say it is).
2242 * We don't have "pcap_breakloop()"; this isn't safe, but
2243 * it's the best we can do. Print the summary if we're
2244 * not reading from a savefile - i.e., if we're doing a
2245 * live capture - and exit.
2247 if (pd
!= NULL
&& pcap_file(pd
) == NULL
) {
2249 * We got interrupted, so perhaps we didn't
2250 * manage to finish a line we were printing.
2251 * Print an extra newline, just in case.
2254 (void)fflush(stdout
);
2262 On windows, we do not use a fork, so we do not care less about
2263 waiting a child processes to die
2265 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
2267 child_cleanup(int signo _U_
)
2271 #endif /* HAVE_FORK && HAVE_VFORK */
2274 info(register int verbose
)
2276 struct pcap_stat stats
;
2279 * Older versions of libpcap didn't set ps_ifdrop on some
2280 * platforms; initialize it to 0 to handle that.
2282 stats
.ps_ifdrop
= 0;
2283 if (pcap_stats(pd
, &stats
) < 0) {
2284 (void)fprintf(stderr
, "pcap_stats: %s\n", pcap_geterr(pd
));
2290 fprintf(stderr
, "%s: ", program_name
);
2292 (void)fprintf(stderr
, "%u packet%s captured", packets_captured
,
2293 PLURAL_SUFFIX(packets_captured
));
2295 fputs(", ", stderr
);
2298 (void)fprintf(stderr
, "%u packet%s received by filter", stats
.ps_recv
,
2299 PLURAL_SUFFIX(stats
.ps_recv
));
2301 fputs(", ", stderr
);
2304 (void)fprintf(stderr
, "%u packet%s dropped by kernel", stats
.ps_drop
,
2305 PLURAL_SUFFIX(stats
.ps_drop
));
2306 if (stats
.ps_ifdrop
!= 0) {
2308 fputs(", ", stderr
);
2311 (void)fprintf(stderr
, "%u packet%s dropped by interface\n",
2312 stats
.ps_ifdrop
, PLURAL_SUFFIX(stats
.ps_ifdrop
));
2318 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
2320 #define fork_subprocess() fork()
2322 #define fork_subprocess() vfork()
2325 compress_savefile(const char *filename
)
2329 child
= fork_subprocess();
2332 "compress_savefile: fork failed: %s\n",
2333 pcap_strerror(errno
));
2337 /* Parent process. */
2343 * Set to lowest priority so that this doesn't disturb the capture.
2346 setpriority(PRIO_PROCESS
, 0, NZERO
- 1);
2348 setpriority(PRIO_PROCESS
, 0, 19);
2350 if (execlp(zflag
, zflag
, filename
, (char *)NULL
) == -1)
2352 "compress_savefile: execlp(%s, %s) failed: %s\n",
2355 pcap_strerror(errno
));
2362 #else /* HAVE_FORK && HAVE_VFORK */
2364 compress_savefile(const char *filename
)
2367 "compress_savefile failed. Functionality not implemented under your system\n");
2369 #endif /* HAVE_FORK && HAVE_VFORK */
2372 dump_packet_and_trunc(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
2374 struct dump_info
*dump_info
;
2380 dump_info
= (struct dump_info
*)user
;
2383 * XXX - this won't force the file to rotate on the specified time
2384 * boundary, but it will rotate on the first packet received after the
2385 * specified Gflag number of seconds. Note: if a Gflag time boundary
2386 * and a Cflag size boundary coincide, the time rotation will occur
2387 * first thereby cancelling the Cflag boundary (since the file should
2391 /* Check if it is time to rotate */
2394 /* Get the current time */
2395 if ((t
= time(NULL
)) == (time_t)-1) {
2396 error("dump_and_trunc_packet: can't get current_time: %s",
2397 pcap_strerror(errno
));
2401 /* If the time is greater than the specified window, rotate */
2402 if (t
- Gflag_time
>= Gflag
) {
2403 #ifdef HAVE_CAPSICUM
2408 /* Update the Gflag_time */
2410 /* Update Gflag_count */
2413 * Close the current file and open a new one.
2415 pcap_dump_close(dump_info
->p
);
2418 * Compress the file we just closed, if the user asked for it
2421 compress_savefile(dump_info
->CurrentFileName
);
2424 * Check to see if we've exceeded the Wflag (when
2427 if (Cflag
== 0 && Wflag
> 0 && Gflag_count
>= Wflag
) {
2428 (void)fprintf(stderr
, "Maximum file limit reached: %d\n",
2434 if (dump_info
->CurrentFileName
!= NULL
)
2435 free(dump_info
->CurrentFileName
);
2436 /* Allocate space for max filename + \0. */
2437 dump_info
->CurrentFileName
= (char *)malloc(PATH_MAX
+ 1);
2438 if (dump_info
->CurrentFileName
== NULL
)
2439 error("dump_packet_and_trunc: malloc");
2441 * Gflag was set otherwise we wouldn't be here. Reset the count
2442 * so multiple files would end with 1,2,3 in the filename.
2443 * The counting is handled with the -C flow after this.
2448 * This is always the first file in the Cflag
2450 * We also don't need numbering if Cflag is not set.
2453 MakeFilename(dump_info
->CurrentFileName
, dump_info
->WFileName
, 0,
2456 MakeFilename(dump_info
->CurrentFileName
, dump_info
->WFileName
, 0, 0);
2458 #ifdef HAVE_LIBCAP_NG
2459 capng_update(CAPNG_ADD
, CAPNG_EFFECTIVE
, CAP_DAC_OVERRIDE
);
2460 capng_apply(CAPNG_SELECT_BOTH
);
2461 #endif /* HAVE_LIBCAP_NG */
2462 #ifdef HAVE_CAPSICUM
2463 fd
= openat(dump_info
->dirfd
,
2464 dump_info
->CurrentFileName
,
2465 O_CREAT
| O_WRONLY
| O_TRUNC
, 0644);
2467 error("unable to open file %s",
2468 dump_info
->CurrentFileName
);
2470 fp
= fdopen(fd
, "w");
2472 error("unable to fdopen file %s",
2473 dump_info
->CurrentFileName
);
2475 dump_info
->p
= pcap_dump_fopen(dump_info
->pd
, fp
);
2476 #else /* !HAVE_CAPSICUM */
2477 dump_info
->p
= pcap_dump_open(dump_info
->pd
, dump_info
->CurrentFileName
);
2479 #ifdef HAVE_LIBCAP_NG
2480 capng_update(CAPNG_DROP
, CAPNG_EFFECTIVE
, CAP_DAC_OVERRIDE
);
2481 capng_apply(CAPNG_SELECT_BOTH
);
2482 #endif /* HAVE_LIBCAP_NG */
2483 if (dump_info
->p
== NULL
)
2484 error("%s", pcap_geterr(pd
));
2485 #ifdef HAVE_CAPSICUM
2486 set_dumper_capsicum_rights(dump_info
->p
);
2492 * XXX - this won't prevent capture files from getting
2493 * larger than Cflag - the last packet written to the
2494 * file could put it over Cflag.
2497 long size
= pcap_dump_ftell(dump_info
->p
);
2500 error("ftell fails on output file");
2502 #ifdef HAVE_CAPSICUM
2508 * Close the current file and open a new one.
2510 pcap_dump_close(dump_info
->p
);
2513 * Compress the file we just closed, if the user
2517 compress_savefile(dump_info
->CurrentFileName
);
2521 if (Cflag_count
>= Wflag
)
2524 if (dump_info
->CurrentFileName
!= NULL
)
2525 free(dump_info
->CurrentFileName
);
2526 dump_info
->CurrentFileName
= (char *)malloc(PATH_MAX
+ 1);
2527 if (dump_info
->CurrentFileName
== NULL
)
2528 error("dump_packet_and_trunc: malloc");
2529 MakeFilename(dump_info
->CurrentFileName
, dump_info
->WFileName
, Cflag_count
, WflagChars
);
2530 #ifdef HAVE_LIBCAP_NG
2531 capng_update(CAPNG_ADD
, CAPNG_EFFECTIVE
, CAP_DAC_OVERRIDE
);
2532 capng_apply(CAPNG_SELECT_BOTH
);
2533 #endif /* HAVE_LIBCAP_NG */
2534 #ifdef HAVE_CAPSICUM
2535 fd
= openat(dump_info
->dirfd
, dump_info
->CurrentFileName
,
2536 O_CREAT
| O_WRONLY
| O_TRUNC
, 0644);
2538 error("unable to open file %s",
2539 dump_info
->CurrentFileName
);
2541 fp
= fdopen(fd
, "w");
2543 error("unable to fdopen file %s",
2544 dump_info
->CurrentFileName
);
2546 dump_info
->p
= pcap_dump_fopen(dump_info
->pd
, fp
);
2547 #else /* !HAVE_CAPSICUM */
2548 dump_info
->p
= pcap_dump_open(dump_info
->pd
, dump_info
->CurrentFileName
);
2550 #ifdef HAVE_LIBCAP_NG
2551 capng_update(CAPNG_DROP
, CAPNG_EFFECTIVE
, CAP_DAC_OVERRIDE
);
2552 capng_apply(CAPNG_SELECT_BOTH
);
2553 #endif /* HAVE_LIBCAP_NG */
2554 if (dump_info
->p
== NULL
)
2555 error("%s", pcap_geterr(pd
));
2556 #ifdef HAVE_CAPSICUM
2557 set_dumper_capsicum_rights(dump_info
->p
);
2562 pcap_dump((u_char
*)dump_info
->p
, h
, sp
);
2563 #ifdef HAVE_PCAP_DUMP_FLUSH
2565 pcap_dump_flush(dump_info
->p
);
2574 dump_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
2580 pcap_dump(user
, h
, sp
);
2581 #ifdef HAVE_PCAP_DUMP_FLUSH
2583 pcap_dump_flush((pcap_dumper_t
*)user
);
2592 print_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
2598 pretty_print_packet((netdissect_options
*)user
, h
, sp
, packets_captured
);
2607 * XXX - there should really be libpcap calls to get the version
2608 * number as a string (the string would be generated from #defines
2609 * at run time, so that it's not generated from string constants
2610 * in the library, as, on many UNIX systems, those constants would
2611 * be statically linked into the application executable image, and
2612 * would thus reflect the version of libpcap on the system on
2613 * which the application was *linked*, not the system on which it's
2616 * That routine should be documented, unlike the "version[]"
2617 * string, so that UNIX vendors providing their own libpcaps
2618 * don't omit it (as a couple of vendors have...).
2620 * Packet.dll should perhaps also export a routine to return the
2621 * version number of the Packet.dll code, to supply the
2622 * "Wpcap_version" information on Windows.
2624 char WDversion
[]="current-git.tcpdump.org";
2625 #if !defined(HAVE_GENERATED_VERSION)
2626 char version
[]="current-git.tcpdump.org";
2628 char pcap_version
[]="current-git.tcpdump.org";
2629 char Wpcap_version
[]="3.1";
2632 #ifdef SIGNAL_REQ_INFO
2633 RETSIGTYPE
requestinfo(int signo _U_
)
2643 * Called once each second in verbose mode while dumping to file
2645 #ifdef USE_WIN32_MM_TIMER
2646 void CALLBACK
verbose_stats_dump (UINT timer_id _U_
, UINT msg _U_
, DWORD_PTR arg _U_
,
2647 DWORD_PTR dw1 _U_
, DWORD_PTR dw2 _U_
)
2650 fprintf(stderr
, "Got %u\r", packets_captured
);
2652 #elif defined(HAVE_ALARM)
2653 static void verbose_stats_dump(int sig _U_
)
2656 fprintf(stderr
, "Got %u\r", packets_captured
);
2661 USES_APPLE_DEPRECATED_API
2665 extern char version
[];
2666 #ifndef HAVE_PCAP_LIB_VERSION
2667 #if defined(_WIN32) || defined(HAVE_PCAP_VERSION)
2668 extern char pcap_version
[];
2669 #else /* defined(_WIN32) || defined(HAVE_PCAP_VERSION) */
2670 static char pcap_version
[] = "unknown";
2671 #endif /* defined(_WIN32) || defined(HAVE_PCAP_VERSION) */
2672 #endif /* HAVE_PCAP_LIB_VERSION */
2673 const char *smi_version_string
;
2675 #ifdef HAVE_PCAP_LIB_VERSION
2677 (void)fprintf(stderr
, "%s version %s, based on tcpdump version %s\n", program_name
, WDversion
, version
);
2679 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
2681 (void)fprintf(stderr
, "%s\n",pcap_lib_version());
2682 #else /* HAVE_PCAP_LIB_VERSION */
2684 (void)fprintf(stderr
, "%s version %s, based on tcpdump version %s\n", program_name
, WDversion
, version
);
2685 (void)fprintf(stderr
, "WinPcap version %s, based on libpcap version %s\n",Wpcap_version
, pcap_version
);
2687 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
2688 (void)fprintf(stderr
, "libpcap version %s\n", pcap_version
);
2690 #endif /* HAVE_PCAP_LIB_VERSION */
2692 #if defined(HAVE_LIBCRYPTO) && defined(SSLEAY_VERSION)
2693 (void)fprintf (stderr
, "%s\n", SSLeay_version(SSLEAY_VERSION
));
2696 smi_version_string
= nd_smi_version_string();
2697 if (smi_version_string
!= NULL
)
2698 (void)fprintf (stderr
, "SMI-library: %s\n", smi_version_string
);
2700 #if defined(__SANITIZE_ADDRESS__)
2701 (void)fprintf (stderr
, "Compiled with AddressSanitizer/GCC.\n");
2702 #elif defined(__has_feature)
2703 # if __has_feature(address_sanitizer)
2704 (void)fprintf (stderr
, "Compiled with AddressSanitizer/CLang.\n");
2706 #endif /* __SANITIZE_ADDRESS__ or __has_feature */
2714 (void)fprintf(stderr
,
2715 "Usage: %s [-aAbd" D_FLAG
"efhH" I_FLAG J_FLAG
"KlLnNOpqStu" U_FLAG
"vxX#]" B_FLAG_USAGE
" [ -c count ]\n", program_name
);
2716 (void)fprintf(stderr
,
2717 "\t\t[ -C file_size ] [ -E algo:secret ] [ -F file ] [ -G seconds ]\n");
2718 (void)fprintf(stderr
,
2719 "\t\t[ -i interface ]" j_FLAG_USAGE
" [ -M secret ] [ --number ]\n");
2720 #ifdef HAVE_PCAP_SETDIRECTION
2721 (void)fprintf(stderr
,
2722 "\t\t[ -Q in|out|inout ]\n");
2724 (void)fprintf(stderr
,
2725 "\t\t[ -r file ] [ -s snaplen ] ");
2726 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
2727 (void)fprintf(stderr
, "[ --time-stamp-precision precision ]\n");
2728 (void)fprintf(stderr
,
2731 #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
2732 (void)fprintf(stderr
, "[ --immediate-mode ] ");
2734 (void)fprintf(stderr
, "[ -T type ] [ --version ] [ -V file ]\n");
2735 (void)fprintf(stderr
,
2736 "\t\t[ -w file ] [ -W filecount ] [ -y datalinktype ] [ -z postrotate-command ]\n");
2737 (void)fprintf(stderr
,
2738 "\t\t[ -Z user ] [ expression ]\n");
2742 * c-style: whitesmith