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 NORETURN
void error(FORMAT_STRING(const char *), ...) PRINTFLIKE(1, 2);
189 static void warning(FORMAT_STRING(const char *), ...) PRINTFLIKE(1, 2);
190 static NORETURN
void exit_tcpdump(int);
191 static RETSIGTYPE
cleanup(int);
192 static RETSIGTYPE
child_cleanup(int);
193 static void print_version(void);
194 static void print_usage(void);
195 static NORETURN
void show_tstamp_types_and_exit(pcap_t
*, const char *device
);
196 static NORETURN
void show_dlts_and_exit(pcap_t
*, const char *device
);
197 #ifdef HAVE_PCAP_FINDALLDEVS
198 static NORETURN
void show_devices_and_exit(void);
201 static void print_packet(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
202 static void dump_packet_and_trunc(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
203 static void dump_packet(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
204 static void droproot(const char *, const char *);
206 #ifdef SIGNAL_REQ_INFO
207 RETSIGTYPE
requestinfo(int);
210 #if defined(USE_WIN32_MM_TIMER)
211 #include <MMsystem.h>
212 static UINT timer_id
;
213 static void CALLBACK
verbose_stats_dump(UINT
, UINT
, DWORD_PTR
, DWORD_PTR
, DWORD_PTR
);
214 #elif defined(HAVE_ALARM)
215 static void verbose_stats_dump(int sig
);
218 static void info(int);
219 static u_int packets_captured
;
221 #ifdef HAVE_PCAP_FINDALLDEVS
222 static const struct tok status_flags
[] = {
224 { PCAP_IF_UP
, "Up" },
226 #ifdef PCAP_IF_RUNNING
227 { PCAP_IF_RUNNING
, "Running" },
229 { PCAP_IF_LOOPBACK
, "Loopback" },
236 static int supports_monitor_mode
;
244 char *CurrentFileName
;
252 #if defined(HAVE_PCAP_SET_PARSER_DEBUG)
254 * We have pcap_set_parser_debug() in libpcap; declare it (it's not declared
255 * by any libpcap header, because it's a special hack, only available if
256 * libpcap was configured to include it, and only intended for use by
257 * libpcap developers trying to debug the parser for filter expressions).
260 __declspec(dllimport
)
264 void pcap_set_parser_debug(int);
265 #elif defined(HAVE_PCAP_DEBUG) || defined(HAVE_YYDEBUG)
267 * We don't have pcap_set_parser_debug() in libpcap, but we do have
268 * pcap_debug or yydebug. Make a local version of pcap_set_parser_debug()
269 * to set the flag, and define HAVE_PCAP_SET_PARSER_DEBUG.
272 pcap_set_parser_debug(int value
)
274 #ifdef HAVE_PCAP_DEBUG
275 extern int pcap_debug
;
278 #else /* HAVE_PCAP_DEBUG */
282 #endif /* HAVE_PCAP_DEBUG */
285 #define HAVE_PCAP_SET_PARSER_DEBUG
288 #if defined(HAVE_PCAP_SET_OPTIMIZER_DEBUG)
290 * We have pcap_set_optimizer_debug() in libpcap; declare it (it's not declared
291 * by any libpcap header, because it's a special hack, only available if
292 * libpcap was configured to include it, and only intended for use by
293 * libpcap developers trying to debug the optimizer for filter expressions).
296 __declspec(dllimport
)
300 void pcap_set_optimizer_debug(int);
305 error(const char *fmt
, ...)
309 (void)fprintf(stderr
, "%s: ", program_name
);
311 (void)vfprintf(stderr
, fmt
, ap
);
316 (void)fputc('\n', stderr
);
324 warning(const char *fmt
, ...)
328 (void)fprintf(stderr
, "%s: WARNING: ", program_name
);
330 (void)vfprintf(stderr
, fmt
, ap
);
335 (void)fputc('\n', stderr
);
340 exit_tcpdump(int status
)
346 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
348 show_tstamp_types_and_exit(pcap_t
*pc
, const char *device
)
351 int *tstamp_types
= 0;
352 const char *tstamp_type_name
;
355 n_tstamp_types
= pcap_list_tstamp_types(pc
, &tstamp_types
);
356 if (n_tstamp_types
< 0)
357 error("%s", pcap_geterr(pc
));
359 if (n_tstamp_types
== 0) {
360 fprintf(stderr
, "Time stamp type cannot be set for %s\n",
364 fprintf(stderr
, "Time stamp types for %s (use option -j to set):\n",
366 for (i
= 0; i
< n_tstamp_types
; i
++) {
367 tstamp_type_name
= pcap_tstamp_type_val_to_name(tstamp_types
[i
]);
368 if (tstamp_type_name
!= NULL
) {
369 (void) fprintf(stderr
, " %s (%s)\n", tstamp_type_name
,
370 pcap_tstamp_type_val_to_description(tstamp_types
[i
]));
372 (void) fprintf(stderr
, " %d\n", tstamp_types
[i
]);
375 pcap_free_tstamp_types(tstamp_types
);
381 show_dlts_and_exit(pcap_t
*pc
, const char *device
)
385 const char *dlt_name
;
387 n_dlts
= pcap_list_datalinks(pc
, &dlts
);
389 error("%s", pcap_geterr(pc
));
390 else if (n_dlts
== 0 || !dlts
)
391 error("No data link types.");
394 * If the interface is known to support monitor mode, indicate
395 * whether these are the data link types available when not in
396 * monitor mode, if -I wasn't specified, or when in monitor mode,
397 * when -I was specified (the link-layer types available in
398 * monitor mode might be different from the ones available when
399 * not in monitor mode).
401 if (supports_monitor_mode
)
402 (void) fprintf(stderr
, "Data link types for %s %s (use option -y to set):\n",
404 Iflag
? "when in monitor mode" : "when not in monitor mode");
406 (void) fprintf(stderr
, "Data link types for %s (use option -y to set):\n",
409 for (i
= 0; i
< n_dlts
; i
++) {
410 dlt_name
= pcap_datalink_val_to_name(dlts
[i
]);
411 if (dlt_name
!= NULL
) {
412 (void) fprintf(stderr
, " %s (%s)", dlt_name
,
413 pcap_datalink_val_to_description(dlts
[i
]));
416 * OK, does tcpdump handle that type?
418 if (!has_printer(dlts
[i
]))
419 (void) fprintf(stderr
, " (printing not supported)");
420 fprintf(stderr
, "\n");
422 (void) fprintf(stderr
, " DLT %d (printing not supported)\n",
426 #ifdef HAVE_PCAP_FREE_DATALINKS
427 pcap_free_datalinks(dlts
);
432 #ifdef HAVE_PCAP_FINDALLDEVS
434 show_devices_and_exit (void)
436 pcap_if_t
*dev
, *devlist
;
437 char ebuf
[PCAP_ERRBUF_SIZE
];
440 if (pcap_findalldevs(&devlist
, ebuf
) < 0)
442 for (i
= 0, dev
= devlist
; dev
!= NULL
; i
++, dev
= dev
->next
) {
443 printf("%d.%s", i
+1, dev
->name
);
444 if (dev
->description
!= NULL
)
445 printf(" (%s)", dev
->description
);
447 printf(" [%s]", bittok2str(status_flags
, "none", dev
->flags
));
450 pcap_freealldevs(devlist
);
453 #endif /* HAVE_PCAP_FINDALLDEVS */
458 * Note that there we use all letters for short options except for g, k,
459 * o, and P, and those are used by other versions of tcpdump, and we should
460 * only use them for the same purposes that the other versions of tcpdump
463 * OS X tcpdump uses -g to force non--v output for IP to be on one
464 * line, making it more "g"repable;
466 * OS X tcpdump uses -k to specify that packet comments in pcap-ng files
469 * OpenBSD tcpdump uses -o to indicate that OS fingerprinting should be done
470 * for hosts sending TCP SYN packets;
472 * OS X tcpdump uses -P to indicate that -w should write pcap-ng rather
475 * OS X tcpdump also uses -Q to specify expressions that match packet
476 * metadata, including but not limited to the packet direction.
477 * The expression syntax is different from a simple "in|out|inout",
478 * and those expressions aren't accepted by OS X tcpdump, but the
479 * equivalents would be "in" = "dir=in", "out" = "dir=out", and
480 * "inout" = "dir=in or dir=out", and the parser could conceivably
481 * special-case "in", "out", and "inout" as expressions for backwards
482 * compatibility, so all is not (yet) lost.
486 * Set up flags that might or might not be supported depending on the
487 * version of libpcap we're using.
489 #if defined(HAVE_PCAP_CREATE) || defined(_WIN32)
491 #define B_FLAG_USAGE " [ -B size ]"
492 #else /* defined(HAVE_PCAP_CREATE) || defined(_WIN32) */
495 #endif /* defined(HAVE_PCAP_CREATE) || defined(_WIN32) */
497 #ifdef HAVE_PCAP_CREATE
499 #else /* HAVE_PCAP_CREATE */
501 #endif /* HAVE_PCAP_CREATE */
503 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
505 #define j_FLAG_USAGE " [ -j tstamptype ]"
507 #else /* PCAP_ERROR_TSTAMP_TYPE_NOTSUP */
511 #endif /* PCAP_ERROR_TSTAMP_TYPE_NOTSUP */
513 #ifdef HAVE_PCAP_FINDALLDEVS
519 #ifdef HAVE_PCAP_DUMP_FLUSH
525 #ifdef HAVE_PCAP_SETDIRECTION
531 #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:#"
536 * We do not currently have long options corresponding to all short
537 * options; we should probably pick appropriate option names for them.
539 * However, the short options where the number of times the option is
540 * specified matters, such as -v and -d and -t, should probably not
541 * just map to a long option, as saying
543 * tcpdump --verbose --verbose
545 * doesn't make sense; it should be --verbosity={N} or something such
548 * For long options with no corresponding short options, we define values
549 * outside the range of ASCII graphic characters, make that the last
550 * component of the entry for the long option, and have a case for that
551 * option in the switch statement.
553 #define OPTION_VERSION 128
554 #define OPTION_TSTAMP_PRECISION 129
555 #define OPTION_IMMEDIATE_MODE 130
557 static const struct option longopts
[] = {
558 #if defined(HAVE_PCAP_CREATE) || defined(_WIN32)
559 { "buffer-size", required_argument
, NULL
, 'B' },
561 { "list-interfaces", no_argument
, NULL
, 'D' },
562 { "help", no_argument
, NULL
, 'h' },
563 { "interface", required_argument
, NULL
, 'i' },
564 #ifdef HAVE_PCAP_CREATE
565 { "monitor-mode", no_argument
, NULL
, 'I' },
567 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
568 { "time-stamp-type", required_argument
, NULL
, 'j' },
569 { "list-time-stamp-types", no_argument
, NULL
, 'J' },
571 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
572 { "time-stamp-precision", required_argument
, NULL
, OPTION_TSTAMP_PRECISION
},
574 { "dont-verify-checksums", no_argument
, NULL
, 'K' },
575 { "list-data-link-types", no_argument
, NULL
, 'L' },
576 { "no-optimize", no_argument
, NULL
, 'O' },
577 { "no-promiscuous-mode", no_argument
, NULL
, 'p' },
578 #ifdef HAVE_PCAP_SETDIRECTION
579 { "direction", required_argument
, NULL
, 'Q' },
581 { "snapshot-length", required_argument
, NULL
, 's' },
582 { "absolute-tcp-sequence-numbers", no_argument
, NULL
, 'S' },
583 #ifdef HAVE_PCAP_DUMP_FLUSH
584 { "packet-buffered", no_argument
, NULL
, 'U' },
586 { "linktype", required_argument
, NULL
, 'y' },
587 #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
588 { "immediate-mode", no_argument
, NULL
, OPTION_IMMEDIATE_MODE
},
590 #ifdef HAVE_PCAP_SET_PARSER_DEBUG
591 { "debug-filter-parser", no_argument
, NULL
, 'Y' },
593 { "relinquish-privileges", required_argument
, NULL
, 'Z' },
594 { "number", no_argument
, NULL
, '#' },
595 { "version", no_argument
, NULL
, OPTION_VERSION
},
600 /* Drop root privileges and chroot if necessary */
602 droproot(const char *username
, const char *chroot_dir
)
604 struct passwd
*pw
= NULL
;
606 if (chroot_dir
&& !username
) {
607 fprintf(stderr
, "%s: Chroot without dropping root is insecure\n",
612 pw
= getpwnam(username
);
615 if (chroot(chroot_dir
) != 0 || chdir ("/") != 0) {
616 fprintf(stderr
, "%s: Couldn't chroot/chdir to '%.64s': %s\n",
617 program_name
, chroot_dir
, pcap_strerror(errno
));
621 #ifdef HAVE_LIBCAP_NG
623 int ret
= capng_change_id(pw
->pw_uid
, pw
->pw_gid
, CAPNG_NO_FLAG
);
625 fprintf(stderr
, "error : ret %d\n", ret
);
627 fprintf(stderr
, "dropped privs to %s\n", username
);
631 if (initgroups(pw
->pw_name
, pw
->pw_gid
) != 0 ||
632 setgid(pw
->pw_gid
) != 0 || setuid(pw
->pw_uid
) != 0) {
633 fprintf(stderr
, "%s: Couldn't change to '%.32s' uid=%lu gid=%lu: %s\n",
634 program_name
, username
,
635 (unsigned long)pw
->pw_uid
,
636 (unsigned long)pw
->pw_gid
,
637 pcap_strerror(errno
));
641 fprintf(stderr
, "dropped privs to %s\n", username
);
643 #endif /* HAVE_LIBCAP_NG */
646 fprintf(stderr
, "%s: Couldn't find user '%.32s'\n",
647 program_name
, username
);
650 #ifdef HAVE_LIBCAP_NG
651 /* We don't need CAP_SETUID, CAP_SETGID and CAP_SYS_CHROOT any more. */
654 CAPNG_EFFECTIVE
| CAPNG_PERMITTED
,
659 capng_apply(CAPNG_SELECT_BOTH
);
660 #endif /* HAVE_LIBCAP_NG */
681 MakeFilename(char *buffer
, char *orig_name
, int cnt
, int max_chars
)
683 char *filename
= malloc(PATH_MAX
+ 1);
684 if (filename
== NULL
)
685 error("Makefilename: malloc");
687 /* Process with strftime if Gflag is set. */
691 /* Convert Gflag_time to a usable format */
692 if ((local_tm
= localtime(&Gflag_time
)) == NULL
) {
693 error("MakeTimedFilename: localtime");
696 /* There's no good way to detect an error in strftime since a return
697 * value of 0 isn't necessarily failure.
699 strftime(filename
, PATH_MAX
, orig_name
, local_tm
);
701 strncpy(filename
, orig_name
, PATH_MAX
);
704 if (cnt
== 0 && max_chars
== 0)
705 strncpy(buffer
, filename
, PATH_MAX
+ 1);
707 if (snprintf(buffer
, PATH_MAX
+ 1, "%s%0*d", filename
, max_chars
, cnt
) > PATH_MAX
)
708 /* Report an error if the filename is too large */
709 error("too many output files or filename is too long (> %d)", PATH_MAX
);
714 get_next_file(FILE *VFile
, char *ptr
)
718 ret
= fgets(ptr
, PATH_MAX
, VFile
);
722 if (ptr
[strlen(ptr
) - 1] == '\n')
723 ptr
[strlen(ptr
) - 1] = '\0';
729 static cap_channel_t
*
732 cap_channel_t
*capcas
, *capdnsloc
;
733 const char *types
[1];
738 error("unable to create casper process");
739 capdnsloc
= cap_service_open(capcas
, "system.dns");
740 /* Casper capability no longer needed. */
742 if (capdnsloc
== NULL
)
743 error("unable to open system.dns service");
744 /* Limit system.dns to reverse DNS lookups. */
746 if (cap_dns_type_limit(capdnsloc
, types
, 1) < 0)
747 error("unable to limit access to system.dns service");
748 families
[0] = AF_INET
;
749 families
[1] = AF_INET6
;
750 if (cap_dns_family_limit(capdnsloc
, families
, 2) < 0)
751 error("unable to limit access to system.dns service");
755 #endif /* HAVE_CASPER */
757 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
759 tstamp_precision_from_string(const char *precision
)
761 if (strncmp(precision
, "nano", strlen("nano")) == 0)
762 return PCAP_TSTAMP_PRECISION_NANO
;
764 if (strncmp(precision
, "micro", strlen("micro")) == 0)
765 return PCAP_TSTAMP_PRECISION_MICRO
;
771 tstamp_precision_to_string(int precision
)
775 case PCAP_TSTAMP_PRECISION_MICRO
:
778 case PCAP_TSTAMP_PRECISION_NANO
:
789 * Ensure that, on a dump file's descriptor, we have all the rights
790 * necessary to make the standard I/O library work with an fdopen()ed
791 * FILE * from that descriptor.
793 * A long time ago, in a galaxy far far away, AT&T decided that, instead
794 * of providing separate APIs for getting and setting the FD_ flags on a
795 * descriptor, getting and setting the O_ flags on a descriptor, and
796 * locking files, they'd throw them all into a kitchen-sink fcntl() call
797 * along the lines of ioctl(), the fact that ioctl() operations are
798 * largely specific to particular character devices but fcntl() operations
799 * are either generic to all descriptors or generic to all descriptors for
800 * regular files nonwithstanding.
802 * The Capsicum people decided that fine-grained control of descriptor
803 * operations was required, so that you need to grant permission for
804 * reading, writing, seeking, and fcntl-ing. The latter, courtesy of
805 * AT&T's decision, means that "fcntl-ing" isn't a thing, but a motley
806 * collection of things, so there are *individual* fcntls for which
807 * permission needs to be granted.
809 * The FreeBSD standard I/O people implemented some optimizations that
810 * requires that the standard I/O routines be able to determine whether
811 * the descriptor for the FILE * is open append-only or not; as that
812 * descriptor could have come from an open() rather than an fopen(),
813 * that requires that it be able to do an F_GETFL fcntl() to read
816 * Tcpdump uses ftell() to determine how much data has been written
817 * to a file in order to, when used with -C, determine when it's time
818 * to rotate capture files. ftell() therefore needs to do an lseek()
819 * to find out the file offset and must, thanks to the aforementioned
820 * optimization, also know whether the descriptor is open append-only
823 * The net result of all the above is that we need to grant CAP_SEEK,
824 * CAP_WRITE, and CAP_FCNTL with the CAP_FCNTL_GETFL subcapability.
826 * Perhaps this is the universe's way of saying that either
828 * 1) there needs to be an fopenat() call and a pcap_dump_openat() call
829 * using it, so that Capsicum-capable tcpdump wouldn't need to do
834 * 2) there needs to be a cap_fdopen() call in the FreeBSD standard
835 * I/O library that knows what rights are needed by the standard
836 * I/O library, based on the open mode, and assigns them, perhaps
837 * with an additional argument indicating, for example, whether
838 * seeking should be allowed, so that tcpdump doesn't need to know
839 * what the standard I/O library happens to require this week.
842 set_dumper_capsicum_rights(pcap_dumper_t
*p
)
844 int fd
= fileno(pcap_dump_file(p
));
847 cap_rights_init(&rights
, CAP_SEEK
, CAP_WRITE
, CAP_FCNTL
);
848 if (cap_rights_limit(fd
, &rights
) < 0 && errno
!= ENOSYS
) {
849 error("unable to limit dump descriptor");
851 if (cap_fcntls_limit(fd
, CAP_FCNTL_GETFL
) < 0 && errno
!= ENOSYS
) {
852 error("unable to limit dump descriptor fcntls");
858 * Copy arg vector into a new buffer, concatenating arguments with spaces.
861 copy_argv(register char **argv
)
864 register u_int len
= 0;
873 len
+= strlen(*p
++) + 1;
875 buf
= (char *)malloc(len
);
877 error("copy_argv: malloc");
881 while ((src
= *p
++) != NULL
) {
882 while ((*dst
++ = *src
++) != '\0')
892 * On Windows, we need to open the file in binary mode, so that
893 * we get all the bytes specified by the size we get from "fstat()".
894 * On UNIX, that's not necessary. O_BINARY is defined on Windows;
895 * we define it as 0 if it's not defined, so it does nothing.
902 read_infile(char *fname
)
904 register int i
, fd
, cc
;
908 fd
= open(fname
, O_RDONLY
|O_BINARY
);
910 error("can't open %s: %s", fname
, pcap_strerror(errno
));
912 if (fstat(fd
, &buf
) < 0)
913 error("can't stat %s: %s", fname
, pcap_strerror(errno
));
915 cp
= malloc((u_int
)buf
.st_size
+ 1);
917 error("malloc(%d) for %s: %s", (u_int
)buf
.st_size
+ 1,
918 fname
, pcap_strerror(errno
));
919 cc
= read(fd
, cp
, (u_int
)buf
.st_size
);
921 error("read %s: %s", fname
, pcap_strerror(errno
));
922 if (cc
!= buf
.st_size
)
923 error("short read %s (%d != %d)", fname
, cc
, (int)buf
.st_size
);
926 /* replace "# comment" with spaces */
927 for (i
= 0; i
< cc
; i
++) {
929 while (i
< cc
&& cp
[i
] != '\n')
936 #ifdef HAVE_PCAP_FINDALLDEVS
938 parse_interface_number(const char *device
)
943 devnum
= strtol(device
, &end
, 10);
944 if (device
!= end
&& *end
== '\0') {
946 * It's all-numeric, but is it a valid number?
950 * No, it's not an ordinal.
952 error("Invalid adapter index");
957 * It's not all-numeric; return -1, so our caller
965 find_interface_by_number(long devnum
)
967 pcap_if_t
*dev
, *devlist
;
969 char ebuf
[PCAP_ERRBUF_SIZE
];
972 if (pcap_findalldevs(&devlist
, ebuf
) < 0)
975 * Look for the devnum-th entry in the list of devices (1-based).
977 for (i
= 0, dev
= devlist
; i
< devnum
-1 && dev
!= NULL
;
978 i
++, dev
= dev
->next
)
981 error("Invalid adapter index");
982 device
= strdup(dev
->name
);
983 pcap_freealldevs(devlist
);
989 open_interface(const char *device
, netdissect_options
*ndo
, char *ebuf
)
992 #ifdef HAVE_PCAP_CREATE
997 #ifdef HAVE_PCAP_CREATE
998 pc
= pcap_create(device
, ebuf
);
1001 * If this failed with "No such device", that means
1002 * the interface doesn't exist; return NULL, so that
1003 * the caller can see whether the device name is
1004 * actually an interface index.
1006 if (strstr(ebuf
, "No such device") != NULL
)
1010 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
1012 show_tstamp_types_and_exit(pc
, device
);
1014 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
1015 status
= pcap_set_tstamp_precision(pc
, ndo
->ndo_tstamp_precision
);
1017 error("%s: Can't set %ssecond time stamp precision: %s",
1019 tstamp_precision_to_string(ndo
->ndo_tstamp_precision
),
1020 pcap_statustostr(status
));
1023 #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
1024 if (immediate_mode
) {
1025 status
= pcap_set_immediate_mode(pc
, 1);
1027 error("%s: Can't set immediate mode: %s",
1029 pcap_statustostr(status
));
1033 * Is this an interface that supports monitor mode?
1035 if (pcap_can_set_rfmon(pc
) == 1)
1036 supports_monitor_mode
= 1;
1038 supports_monitor_mode
= 0;
1039 if (ndo
->ndo_snaplen
!= 0) {
1041 * A snapshot length was explicitly specified;
1044 status
= pcap_set_snaplen(pc
, ndo
->ndo_snaplen
);
1046 error("%s: Can't set snapshot length: %s",
1047 device
, pcap_statustostr(status
));
1049 status
= pcap_set_promisc(pc
, !pflag
);
1051 error("%s: Can't set promiscuous mode: %s",
1052 device
, pcap_statustostr(status
));
1054 status
= pcap_set_rfmon(pc
, 1);
1056 error("%s: Can't set monitor mode: %s",
1057 device
, pcap_statustostr(status
));
1059 status
= pcap_set_timeout(pc
, 1000);
1061 error("%s: pcap_set_timeout failed: %s",
1062 device
, pcap_statustostr(status
));
1064 status
= pcap_set_buffer_size(pc
, Bflag
);
1066 error("%s: Can't set buffer size: %s",
1067 device
, pcap_statustostr(status
));
1069 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
1071 status
= pcap_set_tstamp_type(pc
, jflag
);
1073 error("%s: Can't set time stamp type: %s",
1074 device
, pcap_statustostr(status
));
1077 status
= pcap_activate(pc
);
1080 * pcap_activate() failed.
1082 cp
= pcap_geterr(pc
);
1083 if (status
== PCAP_ERROR
)
1085 else if (status
== PCAP_ERROR_NO_SUCH_DEVICE
) {
1087 * Return an error for our caller to handle.
1089 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "%s: %s\n(%s)",
1090 device
, pcap_statustostr(status
), cp
);
1093 } else if (status
== PCAP_ERROR_PERM_DENIED
&& *cp
!= '\0')
1094 error("%s: %s\n(%s)", device
,
1095 pcap_statustostr(status
), cp
);
1097 else if (status
== PCAP_ERROR_RFMON_NOTSUP
&&
1098 strncmp(device
, "wlan", 4) == 0) {
1099 char parent
[8], newdev
[8];
1101 size_t s
= sizeof(parent
);
1103 snprintf(sysctl
, sizeof(sysctl
),
1104 "net.wlan.%d.%%parent", atoi(device
+ 4));
1105 sysctlbyname(sysctl
, parent
, &s
, NULL
, 0);
1106 strlcpy(newdev
, device
, sizeof(newdev
));
1107 /* Suggest a new wlan device. */
1108 /* FIXME: incrementing the index this way is not going to work well
1109 * when the index is 9 or greater but the only consequence in this
1110 * specific case would be an error message that looks a bit odd.
1112 newdev
[strlen(newdev
)-1]++;
1113 error("%s is not a monitor mode VAP\n"
1114 "To create a new monitor mode VAP use:\n"
1115 " ifconfig %s create wlandev %s wlanmode monitor\n"
1116 "and use %s as the tcpdump interface",
1117 device
, newdev
, parent
, newdev
);
1121 error("%s: %s", device
,
1122 pcap_statustostr(status
));
1123 } else if (status
> 0) {
1125 * pcap_activate() succeeded, but it's warning us
1126 * of a problem it had.
1128 cp
= pcap_geterr(pc
);
1129 if (status
== PCAP_WARNING
)
1131 else if (status
== PCAP_WARNING_PROMISC_NOTSUP
&&
1133 warning("%s: %s\n(%s)", device
,
1134 pcap_statustostr(status
), cp
);
1136 warning("%s: %s", device
,
1137 pcap_statustostr(status
));
1139 #ifdef HAVE_PCAP_SETDIRECTION
1141 status
= pcap_setdirection(pc
, Qflag
);
1143 error("%s: pcap_setdirection() failed: %s",
1144 device
, pcap_geterr(pc
));
1146 #endif /* HAVE_PCAP_SETDIRECTION */
1147 #else /* HAVE_PCAP_CREATE */
1150 * If no snapshot length was specified, or a length of 0 was
1151 * specified, default to 256KB.
1153 if (ndo
->ndo_snaplen
== 0)
1154 ndo
->ndo_snaplen
= 262144;
1155 pc
= pcap_open_live(device
, ndo
->ndo_snaplen
, !pflag
, 1000, ebuf
);
1158 * If this failed with "No such device", that means
1159 * the interface doesn't exist; return NULL, so that
1160 * the caller can see whether the device name is
1161 * actually an interface index.
1163 if (strstr(ebuf
, "No such device") != NULL
)
1168 warning("%s", ebuf
);
1169 #endif /* HAVE_PCAP_CREATE */
1175 main(int argc
, char **argv
)
1177 register int cnt
, op
, i
;
1178 bpf_u_int32 localnet
=0 , netmask
= 0;
1179 int timezone_offset
= 0;
1180 register char *cp
, *infile
, *cmdbuf
, *device
, *RFileName
, *VFileName
, *WFileName
;
1181 pcap_handler callback
;
1183 const char *dlt_name
;
1184 struct bpf_program fcode
;
1186 RETSIGTYPE (*oldhandler
)(int);
1188 struct dump_info dumpinfo
;
1189 u_char
*pcap_userdata
;
1190 char ebuf
[PCAP_ERRBUF_SIZE
];
1191 char VFileLine
[PATH_MAX
+ 1];
1192 char *username
= NULL
;
1193 char *chroot_dir
= NULL
;
1196 #ifdef HAVE_PCAP_FINDALLDEVS
1202 #ifdef HAVE_CAPSICUM
1203 cap_rights_t rights
;
1205 #endif /* HAVE_CAPSICUM */
1206 int Oflag
= 1; /* run filter code optimizer */
1208 const char *yflag_dlt_name
= NULL
;
1210 netdissect_options Ndo
;
1211 netdissect_options
*ndo
= &Ndo
;
1214 * Initialize the netdissect code.
1216 if (nd_init(ebuf
, sizeof ebuf
) == -1)
1219 memset(ndo
, 0, sizeof(*ndo
));
1220 ndo_set_function_pointers(ndo
);
1230 if ((cp
= strrchr(argv
[0], '/')) != NULL
)
1231 ndo
->program_name
= program_name
= cp
+ 1;
1233 ndo
->program_name
= program_name
= argv
[0];
1236 if (pcap_wsockinit() != 0)
1237 error("Attempting to initialize Winsock failed");
1241 * On platforms where the CPU doesn't support unaligned loads,
1242 * force unaligned accesses to abort with SIGBUS, rather than
1243 * being fixed up (slowly) by the OS kernel; on those platforms,
1244 * misaligned accesses are bugs, and we want tcpdump to crash so
1245 * that the bugs are reported.
1247 if (abort_on_misalignment(ebuf
, sizeof(ebuf
)) < 0)
1251 (op
= getopt_long(argc
, argv
, SHORTOPTS
, longopts
, NULL
)) != -1)
1255 /* compatibility for old -a */
1266 #if defined(HAVE_PCAP_CREATE) || defined(_WIN32)
1268 Bflag
= atoi(optarg
)*1024;
1270 error("invalid packet buffer size %s", optarg
);
1272 #endif /* defined(HAVE_PCAP_CREATE) || defined(_WIN32) */
1277 error("invalid packet count %s", optarg
);
1281 Cflag
= atoi(optarg
) * 1000000;
1283 error("invalid file size %s", optarg
);
1303 #ifndef HAVE_LIBCRYPTO
1304 warning("crypto code not compiled in");
1306 ndo
->ndo_espsecret
= optarg
;
1318 Gflag
= atoi(optarg
);
1320 error("invalid number of seconds %s", optarg
);
1322 /* We will create one file initially. */
1325 /* Grab the current time for rotation use. */
1326 if ((Gflag_time
= time(NULL
)) == (time_t)-1) {
1327 error("main: can't get current time: %s",
1328 pcap_strerror(errno
));
1345 #ifdef HAVE_PCAP_CREATE
1349 #endif /* HAVE_PCAP_CREATE */
1351 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
1353 jflag
= pcap_tstamp_type_name_to_val(optarg
);
1355 error("invalid time stamp type %s", optarg
);
1366 * _IOLBF is the same as _IOFBF in Microsoft's C
1367 * libraries; the only alternative they offer
1370 * XXX - this should really be checking for MSVC++,
1371 * not _WIN32, if, for example, MinGW has its own
1372 * C library that is more UNIX-compatible.
1374 setvbuf(stdout
, NULL
, _IONBF
, 0);
1376 #ifdef HAVE_SETLINEBUF
1379 setvbuf(stdout
, NULL
, _IOLBF
, 0);
1389 if (nd_have_smi_support()) {
1390 if (nd_load_smi_module(optarg
, ebuf
, sizeof ebuf
) == -1)
1393 (void)fprintf(stderr
, "%s: ignoring option `-m %s' ",
1394 program_name
, optarg
);
1395 (void)fprintf(stderr
, "(no libsmi support)\n");
1400 /* TCP-MD5 shared secret */
1401 #ifndef HAVE_LIBCRYPTO
1402 warning("crypto code not compiled in");
1404 ndo
->ndo_sigsecret
= optarg
;
1425 ++ndo
->ndo_suppress_default_print
;
1428 #ifdef HAVE_PCAP_SETDIRECTION
1430 if (ascii_strcasecmp(optarg
, "in") == 0)
1432 else if (ascii_strcasecmp(optarg
, "out") == 0)
1434 else if (ascii_strcasecmp(optarg
, "inout") == 0)
1435 Qflag
= PCAP_D_INOUT
;
1437 error("unknown capture direction `%s'", optarg
);
1439 #endif /* HAVE_PCAP_SETDIRECTION */
1446 ndo
->ndo_snaplen
= strtol(optarg
, &end
, 0);
1447 if (optarg
== end
|| *end
!= '\0'
1448 || ndo
->ndo_snaplen
< 0 || ndo
->ndo_snaplen
> MAXIMUM_SNAPLEN
)
1449 error("invalid snaplen %s", optarg
);
1461 if (ascii_strcasecmp(optarg
, "vat") == 0)
1462 ndo
->ndo_packettype
= PT_VAT
;
1463 else if (ascii_strcasecmp(optarg
, "wb") == 0)
1464 ndo
->ndo_packettype
= PT_WB
;
1465 else if (ascii_strcasecmp(optarg
, "rpc") == 0)
1466 ndo
->ndo_packettype
= PT_RPC
;
1467 else if (ascii_strcasecmp(optarg
, "rtp") == 0)
1468 ndo
->ndo_packettype
= PT_RTP
;
1469 else if (ascii_strcasecmp(optarg
, "rtcp") == 0)
1470 ndo
->ndo_packettype
= PT_RTCP
;
1471 else if (ascii_strcasecmp(optarg
, "snmp") == 0)
1472 ndo
->ndo_packettype
= PT_SNMP
;
1473 else if (ascii_strcasecmp(optarg
, "cnfp") == 0)
1474 ndo
->ndo_packettype
= PT_CNFP
;
1475 else if (ascii_strcasecmp(optarg
, "tftp") == 0)
1476 ndo
->ndo_packettype
= PT_TFTP
;
1477 else if (ascii_strcasecmp(optarg
, "aodv") == 0)
1478 ndo
->ndo_packettype
= PT_AODV
;
1479 else if (ascii_strcasecmp(optarg
, "carp") == 0)
1480 ndo
->ndo_packettype
= PT_CARP
;
1481 else if (ascii_strcasecmp(optarg
, "radius") == 0)
1482 ndo
->ndo_packettype
= PT_RADIUS
;
1483 else if (ascii_strcasecmp(optarg
, "zmtp1") == 0)
1484 ndo
->ndo_packettype
= PT_ZMTP1
;
1485 else if (ascii_strcasecmp(optarg
, "vxlan") == 0)
1486 ndo
->ndo_packettype
= PT_VXLAN
;
1487 else if (ascii_strcasecmp(optarg
, "pgm") == 0)
1488 ndo
->ndo_packettype
= PT_PGM
;
1489 else if (ascii_strcasecmp(optarg
, "pgm_zmtp1") == 0)
1490 ndo
->ndo_packettype
= PT_PGM_ZMTP1
;
1491 else if (ascii_strcasecmp(optarg
, "lmp") == 0)
1492 ndo
->ndo_packettype
= PT_LMP
;
1493 else if (ascii_strcasecmp(optarg
, "resp") == 0)
1494 ndo
->ndo_packettype
= PT_RESP
;
1496 error("unknown packet type `%s'", optarg
);
1503 #ifdef HAVE_PCAP_DUMP_FLUSH
1522 Wflag
= atoi(optarg
);
1524 error("invalid number of output files %s", optarg
);
1525 WflagChars
= getWflagChars(Wflag
);
1530 ++ndo
->ndo_suppress_default_print
;
1535 ++ndo
->ndo_suppress_default_print
;
1539 yflag_dlt_name
= optarg
;
1541 pcap_datalink_name_to_val(yflag_dlt_name
);
1543 error("invalid data link type %s", yflag_dlt_name
);
1546 #ifdef HAVE_PCAP_SET_PARSER_DEBUG
1549 /* Undocumented flag */
1550 pcap_set_parser_debug(1);
1563 ndo
->ndo_packet_number
= 1;
1566 case OPTION_VERSION
:
1571 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
1572 case OPTION_TSTAMP_PRECISION
:
1573 ndo
->ndo_tstamp_precision
= tstamp_precision_from_string(optarg
);
1574 if (ndo
->ndo_tstamp_precision
< 0)
1575 error("unsupported time stamp precision");
1579 #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
1580 case OPTION_IMMEDIATE_MODE
:
1591 #ifdef HAVE_PCAP_FINDALLDEVS
1593 show_devices_and_exit();
1596 switch (ndo
->ndo_tflag
) {
1598 case 0: /* Default */
1599 case 4: /* Default + Date*/
1600 timezone_offset
= gmt2local(0);
1603 case 1: /* No time stamp */
1604 case 2: /* Unix timeval style */
1605 case 3: /* Microseconds since previous packet */
1606 case 5: /* Microseconds since first packet */
1609 default: /* Not supported */
1610 error("only -t, -tt, -ttt, -tttt and -ttttt are supported");
1614 if (ndo
->ndo_fflag
!= 0 && (VFileName
!= NULL
|| RFileName
!= NULL
))
1615 error("-f can not be used with -V or -r");
1617 if (VFileName
!= NULL
&& RFileName
!= NULL
)
1618 error("-V and -r are mutually exclusive.");
1620 #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
1622 * If we're printing dissected packets to the standard output
1623 * rather than saving raw packets to a file, and the standard
1624 * output is a terminal, use immediate mode, as the user's
1625 * probably expecting to see packets pop up immediately.
1627 if (WFileName
== NULL
&& isatty(1))
1632 /* if run as root, prepare for chrooting */
1633 if (getuid() == 0 || geteuid() == 0) {
1634 /* future extensibility for cmd-line arguments */
1636 chroot_dir
= WITH_CHROOT
;
1641 /* if run as root, prepare for dropping root privileges */
1642 if (getuid() == 0 || geteuid() == 0) {
1643 /* Run with '-Z root' to restore old behaviour */
1645 username
= WITH_USER
;
1649 if (RFileName
!= NULL
|| VFileName
!= NULL
) {
1651 * If RFileName is non-null, it's the pathname of a
1652 * savefile to read. If VFileName is non-null, it's
1653 * the pathname of a file containing a list of pathnames
1654 * (one per line) of savefiles to read.
1656 * In either case, we're reading a savefile, not doing
1661 * We don't need network access, so relinquish any set-UID
1662 * or set-GID privileges we have (if any).
1664 * We do *not* want set-UID privileges when opening a
1665 * trace file, as that might let the user read other
1666 * people's trace files (especially if we're set-UID
1669 if (setgid(getgid()) != 0 || setuid(getuid()) != 0 )
1670 fprintf(stderr
, "Warning: setgid/setuid failed !\n");
1672 if (VFileName
!= NULL
) {
1673 if (VFileName
[0] == '-' && VFileName
[1] == '\0')
1676 VFile
= fopen(VFileName
, "r");
1679 error("Unable to open file: %s\n", pcap_strerror(errno
));
1681 ret
= get_next_file(VFile
, VFileLine
);
1683 error("Nothing in %s\n", VFileName
);
1684 RFileName
= VFileLine
;
1687 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
1688 pd
= pcap_open_offline_with_tstamp_precision(RFileName
,
1689 ndo
->ndo_tstamp_precision
, ebuf
);
1691 pd
= pcap_open_offline(RFileName
, ebuf
);
1696 #ifdef HAVE_CAPSICUM
1697 cap_rights_init(&rights
, CAP_READ
);
1698 if (cap_rights_limit(fileno(pcap_file(pd
)), &rights
) < 0 &&
1700 error("unable to limit pcap descriptor");
1703 dlt
= pcap_datalink(pd
);
1704 dlt_name
= pcap_datalink_val_to_name(dlt
);
1705 if (dlt_name
== NULL
) {
1706 fprintf(stderr
, "reading from file %s, link-type %u\n",
1710 "reading from file %s, link-type %s (%s)\n",
1711 RFileName
, dlt_name
,
1712 pcap_datalink_val_to_description(dlt
));
1716 * We're doing a live capture.
1718 if (device
== NULL
) {
1720 * No interface was specified. Pick one.
1722 #ifdef HAVE_PCAP_FINDALLDEVS
1724 * Find the list of interfaces, and pick
1725 * the first interface.
1727 if (pcap_findalldevs(&devlist
, ebuf
) >= 0 &&
1729 device
= strdup(devlist
->name
);
1730 pcap_freealldevs(devlist
);
1732 #else /* HAVE_PCAP_FINDALLDEVS */
1734 * Use whatever interface pcap_lookupdev()
1737 device
= pcap_lookupdev(ebuf
);
1744 * Try to open the interface with the specified name.
1746 pd
= open_interface(device
, ndo
, ebuf
);
1749 * That failed. If we can get a list of
1750 * interfaces, and the interface name
1751 * is purely numeric, try to use it as
1752 * a 1-based index in the list of
1755 #ifdef HAVE_PCAP_FINDALLDEVS
1756 devnum
= parse_interface_number(device
);
1759 * It's not a number; just report
1760 * the open error and fail.
1766 * OK, it's a number; try to find the
1767 * interface with that index, and try
1770 * find_interface_by_number() exits if it
1771 * couldn't be found.
1773 device
= find_interface_by_number(devnum
);
1774 pd
= open_interface(device
, ndo
, ebuf
);
1777 #else /* HAVE_PCAP_FINDALLDEVS */
1779 * We can't get a list of interfaces; just
1783 #endif /* HAVE_PCAP_FINDALLDEVS */
1787 * Let user own process after capture device has
1791 if (setgid(getgid()) != 0 || setuid(getuid()) != 0)
1792 fprintf(stderr
, "Warning: setgid/setuid failed !\n");
1794 #if !defined(HAVE_PCAP_CREATE) && defined(_WIN32)
1796 if(pcap_setbuff(pd
, Bflag
)==-1){
1797 error("%s", pcap_geterr(pd
));
1799 #endif /* !defined(HAVE_PCAP_CREATE) && defined(_WIN32) */
1801 show_dlts_and_exit(pd
, device
);
1802 if (yflag_dlt
>= 0) {
1803 #ifdef HAVE_PCAP_SET_DATALINK
1804 if (pcap_set_datalink(pd
, yflag_dlt
) < 0)
1805 error("%s", pcap_geterr(pd
));
1808 * We don't actually support changing the
1809 * data link type, so we only let them
1810 * set it to what it already is.
1812 if (yflag_dlt
!= pcap_datalink(pd
)) {
1813 error("%s is not one of the DLTs supported by this device\n",
1817 (void)fprintf(stderr
, "%s: data link type %s\n",
1818 program_name
, yflag_dlt_name
);
1819 (void)fflush(stderr
);
1821 i
= pcap_snapshot(pd
);
1822 if (ndo
->ndo_snaplen
< i
) {
1823 if (ndo
->ndo_snaplen
!= 0)
1824 warning("snaplen raised from %d to %d", ndo
->ndo_snaplen
, i
);
1825 ndo
->ndo_snaplen
= i
;
1826 } else if (ndo
->ndo_snaplen
> i
) {
1827 warning("snaplen lowered from %d to %d", ndo
->ndo_snaplen
, i
);
1828 ndo
->ndo_snaplen
= i
;
1830 if(ndo
->ndo_fflag
!= 0) {
1831 if (pcap_lookupnet(device
, &localnet
, &netmask
, ebuf
) < 0) {
1832 warning("foreign (-f) flag used but: %s", ebuf
);
1838 cmdbuf
= read_infile(infile
);
1840 cmdbuf
= copy_argv(&argv
[optind
]);
1842 #ifdef HAVE_PCAP_SET_OPTIMIZER_DEBUG
1843 pcap_set_optimizer_debug(dflag
);
1845 if (pcap_compile(pd
, &fcode
, cmdbuf
, Oflag
, netmask
) < 0)
1846 error("%s", pcap_geterr(pd
));
1848 bpf_dump(&fcode
, dflag
);
1851 pcap_freecode(&fcode
);
1856 if (!ndo
->ndo_nflag
)
1857 capdns
= capdns_setup();
1858 #endif /* HAVE_CASPER */
1860 init_print(ndo
, localnet
, netmask
, timezone_offset
);
1863 (void)setsignal(SIGPIPE
, cleanup
);
1864 (void)setsignal(SIGTERM
, cleanup
);
1865 (void)setsignal(SIGINT
, cleanup
);
1867 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
1868 (void)setsignal(SIGCHLD
, child_cleanup
);
1870 /* Cooperate with nohup(1) */
1872 if ((oldhandler
= setsignal(SIGHUP
, cleanup
)) != SIG_DFL
)
1873 (void)setsignal(SIGHUP
, oldhandler
);
1878 * If a user name was specified with "-Z", attempt to switch to
1879 * that user's UID. This would probably be used with sudo,
1880 * to allow tcpdump to be run in a special restricted
1881 * account (if you just want to allow users to open capture
1882 * devices, and can't just give users that permission,
1883 * you'd make tcpdump set-UID or set-GID).
1885 * Tcpdump doesn't necessarily write only to one savefile;
1886 * the general only way to allow a -Z instance to write to
1887 * savefiles as the user under whose UID it's run, rather
1888 * than as the user specified with -Z, would thus be to switch
1889 * to the original user ID before opening a capture file and
1890 * then switch back to the -Z user ID after opening the savefile.
1891 * Switching to the -Z user ID only after opening the first
1892 * savefile doesn't handle the general case.
1895 if (getuid() == 0 || geteuid() == 0) {
1896 #ifdef HAVE_LIBCAP_NG
1897 /* Initialize capng */
1898 capng_clear(CAPNG_SELECT_BOTH
);
1902 CAPNG_PERMITTED
| CAPNG_EFFECTIVE
,
1910 CAPNG_PERMITTED
| CAPNG_EFFECTIVE
,
1918 CAPNG_PERMITTED
| CAPNG_EFFECTIVE
,
1922 capng_apply(CAPNG_SELECT_BOTH
);
1923 #endif /* HAVE_LIBCAP_NG */
1924 if (username
|| chroot_dir
)
1925 droproot(username
, chroot_dir
);
1930 if (pcap_setfilter(pd
, &fcode
) < 0)
1931 error("%s", pcap_geterr(pd
));
1932 #ifdef HAVE_CAPSICUM
1933 if (RFileName
== NULL
&& VFileName
== NULL
) {
1934 static const unsigned long cmds
[] = { BIOCGSTATS
, BIOCROTZBUF
};
1937 * The various libpcap devices use a combination of
1938 * read (bpf), ioctl (bpf, netmap), poll (netmap)
1939 * so we add the relevant access rights.
1941 cap_rights_init(&rights
, CAP_IOCTL
, CAP_READ
, CAP_EVENT
);
1942 if (cap_rights_limit(pcap_fileno(pd
), &rights
) < 0 &&
1944 error("unable to limit pcap descriptor");
1946 if (cap_ioctls_limit(pcap_fileno(pd
), cmds
,
1947 sizeof(cmds
) / sizeof(cmds
[0])) < 0 && errno
!= ENOSYS
) {
1948 error("unable to limit ioctls on pcap descriptor");
1954 /* Do not exceed the default PATH_MAX for files. */
1955 dumpinfo
.CurrentFileName
= (char *)malloc(PATH_MAX
+ 1);
1957 if (dumpinfo
.CurrentFileName
== NULL
)
1958 error("malloc of dumpinfo.CurrentFileName");
1960 /* We do not need numbering for dumpfiles if Cflag isn't set. */
1962 MakeFilename(dumpinfo
.CurrentFileName
, WFileName
, 0, WflagChars
);
1964 MakeFilename(dumpinfo
.CurrentFileName
, WFileName
, 0, 0);
1966 p
= pcap_dump_open(pd
, dumpinfo
.CurrentFileName
);
1967 #ifdef HAVE_LIBCAP_NG
1968 /* Give up CAP_DAC_OVERRIDE capability.
1969 * Only allow it to be restored if the -C or -G flag have been
1970 * set since we may need to create more files later on.
1974 (Cflag
|| Gflag
? 0 : CAPNG_PERMITTED
)
1978 capng_apply(CAPNG_SELECT_BOTH
);
1979 #endif /* HAVE_LIBCAP_NG */
1981 error("%s", pcap_geterr(pd
));
1982 #ifdef HAVE_CAPSICUM
1983 set_dumper_capsicum_rights(p
);
1985 if (Cflag
!= 0 || Gflag
!= 0) {
1986 #ifdef HAVE_CAPSICUM
1987 dumpinfo
.WFileName
= strdup(basename(WFileName
));
1988 if (dumpinfo
.WFileName
== NULL
) {
1989 error("Unable to allocate memory for file %s",
1992 dumpinfo
.dirfd
= open(dirname(WFileName
),
1993 O_DIRECTORY
| O_RDONLY
);
1994 if (dumpinfo
.dirfd
< 0) {
1995 error("unable to open directory %s",
1996 dirname(WFileName
));
1998 cap_rights_init(&rights
, CAP_CREATE
, CAP_FCNTL
,
1999 CAP_FTRUNCATE
, CAP_LOOKUP
, CAP_SEEK
, CAP_WRITE
);
2000 if (cap_rights_limit(dumpinfo
.dirfd
, &rights
) < 0 &&
2002 error("unable to limit directory rights");
2004 if (cap_fcntls_limit(dumpinfo
.dirfd
, CAP_FCNTL_GETFL
) < 0 &&
2006 error("unable to limit dump descriptor fcntls");
2008 #else /* !HAVE_CAPSICUM */
2009 dumpinfo
.WFileName
= WFileName
;
2011 callback
= dump_packet_and_trunc
;
2014 pcap_userdata
= (u_char
*)&dumpinfo
;
2016 callback
= dump_packet
;
2017 pcap_userdata
= (u_char
*)p
;
2019 #ifdef HAVE_PCAP_DUMP_FLUSH
2024 dlt
= pcap_datalink(pd
);
2025 ndo
->ndo_if_printer
= get_if_printer(ndo
, dlt
);
2026 callback
= print_packet
;
2027 pcap_userdata
= (u_char
*)ndo
;
2030 #ifdef SIGNAL_REQ_INFO
2032 * We can't get statistics when reading from a file rather
2033 * than capturing from a device.
2035 if (RFileName
== NULL
)
2036 (void)setsignal(SIGNAL_REQ_INFO
, requestinfo
);
2039 if (ndo
->ndo_vflag
> 0 && WFileName
) {
2041 * When capturing to a file, "-v" means tcpdump should,
2042 * every 10 seconds, "v"erbosely report the number of
2045 #ifdef USE_WIN32_MM_TIMER
2046 /* call verbose_stats_dump() each 1000 +/-100msec */
2047 timer_id
= timeSetEvent(1000, 100, verbose_stats_dump
, 0, TIME_PERIODIC
);
2048 setvbuf(stderr
, NULL
, _IONBF
, 0);
2049 #elif defined(HAVE_ALARM)
2050 (void)setsignal(SIGALRM
, verbose_stats_dump
);
2055 if (RFileName
== NULL
) {
2057 * Live capture (if -V was specified, we set RFileName
2058 * to a file from the -V file). Print a message to
2059 * the standard error on UN*X.
2061 if (!ndo
->ndo_vflag
&& !WFileName
) {
2062 (void)fprintf(stderr
,
2063 "%s: verbose output suppressed, use -v or -vv for full protocol decode\n",
2066 (void)fprintf(stderr
, "%s: ", program_name
);
2067 dlt
= pcap_datalink(pd
);
2068 dlt_name
= pcap_datalink_val_to_name(dlt
);
2069 if (dlt_name
== NULL
) {
2070 (void)fprintf(stderr
, "listening on %s, link-type %u, capture size %u bytes\n",
2071 device
, dlt
, ndo
->ndo_snaplen
);
2073 (void)fprintf(stderr
, "listening on %s, link-type %s (%s), capture size %u bytes\n",
2075 pcap_datalink_val_to_description(dlt
), ndo
->ndo_snaplen
);
2077 (void)fflush(stderr
);
2080 #ifdef HAVE_CAPSICUM
2081 cansandbox
= (VFileName
== NULL
&& zflag
== NULL
);
2083 cansandbox
= (cansandbox
&& (ndo
->ndo_nflag
|| capdns
!= NULL
));
2085 cansandbox
= (cansandbox
&& ndo
->ndo_nflag
);
2086 #endif /* HAVE_CASPER */
2087 if (cansandbox
&& cap_enter() < 0 && errno
!= ENOSYS
)
2088 error("unable to enter the capability mode");
2089 #endif /* HAVE_CAPSICUM */
2092 status
= pcap_loop(pd
, cnt
, callback
, pcap_userdata
);
2093 if (WFileName
== NULL
) {
2095 * We're printing packets. Flush the printed output,
2096 * so it doesn't get intermingled with error output.
2100 * We got interrupted, so perhaps we didn't
2101 * manage to finish a line we were printing.
2102 * Print an extra newline, just in case.
2106 (void)fflush(stdout
);
2110 * We got interrupted. If we are reading multiple
2111 * files (via -V) set these so that we stop.
2120 (void)fprintf(stderr
, "%s: pcap_loop: %s\n",
2121 program_name
, pcap_geterr(pd
));
2123 if (RFileName
== NULL
) {
2125 * We're doing a live capture. Report the capture
2131 if (VFileName
!= NULL
) {
2132 ret
= get_next_file(VFile
, VFileLine
);
2136 RFileName
= VFileLine
;
2137 pd
= pcap_open_offline(RFileName
, ebuf
);
2140 #ifdef HAVE_CAPSICUM
2141 cap_rights_init(&rights
, CAP_READ
);
2142 if (cap_rights_limit(fileno(pcap_file(pd
)),
2143 &rights
) < 0 && errno
!= ENOSYS
) {
2144 error("unable to limit pcap descriptor");
2147 new_dlt
= pcap_datalink(pd
);
2148 if (new_dlt
!= dlt
) {
2150 * The new file has a different
2151 * link-layer header type from the
2154 if (WFileName
!= NULL
) {
2156 * We're writing raw packets
2157 * that match the filter to
2158 * a pcap file. pcap files
2159 * don't support multiple
2160 * different link-layer
2161 * header types, so we fail
2164 error("%s: new dlt does not match original", RFileName
);
2168 * We're printing the decoded packets;
2169 * switch to the new DLT.
2171 * To do that, we need to change
2172 * the printer, change the DLT name,
2173 * and recompile the filter with
2177 ndo
->ndo_if_printer
= get_if_printer(ndo
, dlt
);
2178 if (pcap_compile(pd
, &fcode
, cmdbuf
, Oflag
, netmask
) < 0)
2179 error("%s", pcap_geterr(pd
));
2183 * Set the filter on the new file.
2185 if (pcap_setfilter(pd
, &fcode
) < 0)
2186 error("%s", pcap_geterr(pd
));
2189 * Report the new file.
2191 dlt_name
= pcap_datalink_val_to_name(dlt
);
2192 if (dlt_name
== NULL
) {
2193 fprintf(stderr
, "reading from file %s, link-type %u\n",
2197 "reading from file %s, link-type %s (%s)\n",
2198 RFileName
, dlt_name
,
2199 pcap_datalink_val_to_description(dlt
));
2204 while (ret
!= NULL
);
2207 pcap_freecode(&fcode
);
2208 exit_tcpdump(status
== -1 ? 1 : 0);
2211 /* make a clean exit on interrupts */
2213 cleanup(int signo _U_
)
2215 #ifdef USE_WIN32_MM_TIMER
2217 timeKillEvent(timer_id
);
2219 #elif defined(HAVE_ALARM)
2223 #ifdef HAVE_PCAP_BREAKLOOP
2225 * We have "pcap_breakloop()"; use it, so that we do as little
2226 * as possible in the signal handler (it's probably not safe
2227 * to do anything with standard I/O streams in a signal handler -
2228 * the ANSI C standard doesn't say it is).
2233 * We don't have "pcap_breakloop()"; this isn't safe, but
2234 * it's the best we can do. Print the summary if we're
2235 * not reading from a savefile - i.e., if we're doing a
2236 * live capture - and exit.
2238 if (pd
!= NULL
&& pcap_file(pd
) == NULL
) {
2240 * We got interrupted, so perhaps we didn't
2241 * manage to finish a line we were printing.
2242 * Print an extra newline, just in case.
2245 (void)fflush(stdout
);
2253 On windows, we do not use a fork, so we do not care less about
2254 waiting a child processes to die
2256 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
2258 child_cleanup(int signo _U_
)
2262 #endif /* HAVE_FORK && HAVE_VFORK */
2265 info(register int verbose
)
2267 struct pcap_stat stats
;
2270 * Older versions of libpcap didn't set ps_ifdrop on some
2271 * platforms; initialize it to 0 to handle that.
2273 stats
.ps_ifdrop
= 0;
2274 if (pcap_stats(pd
, &stats
) < 0) {
2275 (void)fprintf(stderr
, "pcap_stats: %s\n", pcap_geterr(pd
));
2281 fprintf(stderr
, "%s: ", program_name
);
2283 (void)fprintf(stderr
, "%u packet%s captured", packets_captured
,
2284 PLURAL_SUFFIX(packets_captured
));
2286 fputs(", ", stderr
);
2289 (void)fprintf(stderr
, "%u packet%s received by filter", stats
.ps_recv
,
2290 PLURAL_SUFFIX(stats
.ps_recv
));
2292 fputs(", ", stderr
);
2295 (void)fprintf(stderr
, "%u packet%s dropped by kernel", stats
.ps_drop
,
2296 PLURAL_SUFFIX(stats
.ps_drop
));
2297 if (stats
.ps_ifdrop
!= 0) {
2299 fputs(", ", stderr
);
2302 (void)fprintf(stderr
, "%u packet%s dropped by interface\n",
2303 stats
.ps_ifdrop
, PLURAL_SUFFIX(stats
.ps_ifdrop
));
2309 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
2311 #define fork_subprocess() fork()
2313 #define fork_subprocess() vfork()
2316 compress_savefile(const char *filename
)
2320 child
= fork_subprocess();
2323 "compress_savefile: fork failed: %s\n",
2324 pcap_strerror(errno
));
2328 /* Parent process. */
2334 * Set to lowest priority so that this doesn't disturb the capture.
2337 setpriority(PRIO_PROCESS
, 0, NZERO
- 1);
2339 setpriority(PRIO_PROCESS
, 0, 19);
2341 if (execlp(zflag
, zflag
, filename
, (char *)NULL
) == -1)
2343 "compress_savefile: execlp(%s, %s) failed: %s\n",
2346 pcap_strerror(errno
));
2353 #else /* HAVE_FORK && HAVE_VFORK */
2355 compress_savefile(const char *filename
)
2358 "compress_savefile failed. Functionality not implemented under your system\n");
2360 #endif /* HAVE_FORK && HAVE_VFORK */
2363 dump_packet_and_trunc(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
2365 struct dump_info
*dump_info
;
2371 dump_info
= (struct dump_info
*)user
;
2374 * XXX - this won't force the file to rotate on the specified time
2375 * boundary, but it will rotate on the first packet received after the
2376 * specified Gflag number of seconds. Note: if a Gflag time boundary
2377 * and a Cflag size boundary coincide, the time rotation will occur
2378 * first thereby cancelling the Cflag boundary (since the file should
2382 /* Check if it is time to rotate */
2385 /* Get the current time */
2386 if ((t
= time(NULL
)) == (time_t)-1) {
2387 error("dump_and_trunc_packet: can't get current_time: %s",
2388 pcap_strerror(errno
));
2392 /* If the time is greater than the specified window, rotate */
2393 if (t
- Gflag_time
>= Gflag
) {
2394 #ifdef HAVE_CAPSICUM
2399 /* Update the Gflag_time */
2401 /* Update Gflag_count */
2404 * Close the current file and open a new one.
2406 pcap_dump_close(dump_info
->p
);
2409 * Compress the file we just closed, if the user asked for it
2412 compress_savefile(dump_info
->CurrentFileName
);
2415 * Check to see if we've exceeded the Wflag (when
2418 if (Cflag
== 0 && Wflag
> 0 && Gflag_count
>= Wflag
) {
2419 (void)fprintf(stderr
, "Maximum file limit reached: %d\n",
2425 if (dump_info
->CurrentFileName
!= NULL
)
2426 free(dump_info
->CurrentFileName
);
2427 /* Allocate space for max filename + \0. */
2428 dump_info
->CurrentFileName
= (char *)malloc(PATH_MAX
+ 1);
2429 if (dump_info
->CurrentFileName
== NULL
)
2430 error("dump_packet_and_trunc: malloc");
2432 * Gflag was set otherwise we wouldn't be here. Reset the count
2433 * so multiple files would end with 1,2,3 in the filename.
2434 * The counting is handled with the -C flow after this.
2439 * This is always the first file in the Cflag
2441 * We also don't need numbering if Cflag is not set.
2444 MakeFilename(dump_info
->CurrentFileName
, dump_info
->WFileName
, 0,
2447 MakeFilename(dump_info
->CurrentFileName
, dump_info
->WFileName
, 0, 0);
2449 #ifdef HAVE_LIBCAP_NG
2450 capng_update(CAPNG_ADD
, CAPNG_EFFECTIVE
, CAP_DAC_OVERRIDE
);
2451 capng_apply(CAPNG_SELECT_BOTH
);
2452 #endif /* HAVE_LIBCAP_NG */
2453 #ifdef HAVE_CAPSICUM
2454 fd
= openat(dump_info
->dirfd
,
2455 dump_info
->CurrentFileName
,
2456 O_CREAT
| O_WRONLY
| O_TRUNC
, 0644);
2458 error("unable to open file %s",
2459 dump_info
->CurrentFileName
);
2461 fp
= fdopen(fd
, "w");
2463 error("unable to fdopen file %s",
2464 dump_info
->CurrentFileName
);
2466 dump_info
->p
= pcap_dump_fopen(dump_info
->pd
, fp
);
2467 #else /* !HAVE_CAPSICUM */
2468 dump_info
->p
= pcap_dump_open(dump_info
->pd
, dump_info
->CurrentFileName
);
2470 #ifdef HAVE_LIBCAP_NG
2471 capng_update(CAPNG_DROP
, CAPNG_EFFECTIVE
, CAP_DAC_OVERRIDE
);
2472 capng_apply(CAPNG_SELECT_BOTH
);
2473 #endif /* HAVE_LIBCAP_NG */
2474 if (dump_info
->p
== NULL
)
2475 error("%s", pcap_geterr(pd
));
2476 #ifdef HAVE_CAPSICUM
2477 set_dumper_capsicum_rights(dump_info
->p
);
2483 * XXX - this won't prevent capture files from getting
2484 * larger than Cflag - the last packet written to the
2485 * file could put it over Cflag.
2488 long size
= pcap_dump_ftell(dump_info
->p
);
2491 error("ftell fails on output file");
2493 #ifdef HAVE_CAPSICUM
2499 * Close the current file and open a new one.
2501 pcap_dump_close(dump_info
->p
);
2504 * Compress the file we just closed, if the user
2508 compress_savefile(dump_info
->CurrentFileName
);
2512 if (Cflag_count
>= Wflag
)
2515 if (dump_info
->CurrentFileName
!= NULL
)
2516 free(dump_info
->CurrentFileName
);
2517 dump_info
->CurrentFileName
= (char *)malloc(PATH_MAX
+ 1);
2518 if (dump_info
->CurrentFileName
== NULL
)
2519 error("dump_packet_and_trunc: malloc");
2520 MakeFilename(dump_info
->CurrentFileName
, dump_info
->WFileName
, Cflag_count
, WflagChars
);
2521 #ifdef HAVE_LIBCAP_NG
2522 capng_update(CAPNG_ADD
, CAPNG_EFFECTIVE
, CAP_DAC_OVERRIDE
);
2523 capng_apply(CAPNG_SELECT_BOTH
);
2524 #endif /* HAVE_LIBCAP_NG */
2525 #ifdef HAVE_CAPSICUM
2526 fd
= openat(dump_info
->dirfd
, dump_info
->CurrentFileName
,
2527 O_CREAT
| O_WRONLY
| O_TRUNC
, 0644);
2529 error("unable to open file %s",
2530 dump_info
->CurrentFileName
);
2532 fp
= fdopen(fd
, "w");
2534 error("unable to fdopen file %s",
2535 dump_info
->CurrentFileName
);
2537 dump_info
->p
= pcap_dump_fopen(dump_info
->pd
, fp
);
2538 #else /* !HAVE_CAPSICUM */
2539 dump_info
->p
= pcap_dump_open(dump_info
->pd
, dump_info
->CurrentFileName
);
2541 #ifdef HAVE_LIBCAP_NG
2542 capng_update(CAPNG_DROP
, CAPNG_EFFECTIVE
, CAP_DAC_OVERRIDE
);
2543 capng_apply(CAPNG_SELECT_BOTH
);
2544 #endif /* HAVE_LIBCAP_NG */
2545 if (dump_info
->p
== NULL
)
2546 error("%s", pcap_geterr(pd
));
2547 #ifdef HAVE_CAPSICUM
2548 set_dumper_capsicum_rights(dump_info
->p
);
2553 pcap_dump((u_char
*)dump_info
->p
, h
, sp
);
2554 #ifdef HAVE_PCAP_DUMP_FLUSH
2556 pcap_dump_flush(dump_info
->p
);
2565 dump_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
2571 pcap_dump(user
, h
, sp
);
2572 #ifdef HAVE_PCAP_DUMP_FLUSH
2574 pcap_dump_flush((pcap_dumper_t
*)user
);
2583 print_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
2589 pretty_print_packet((netdissect_options
*)user
, h
, sp
, packets_captured
);
2598 * XXX - there should really be libpcap calls to get the version
2599 * number as a string (the string would be generated from #defines
2600 * at run time, so that it's not generated from string constants
2601 * in the library, as, on many UNIX systems, those constants would
2602 * be statically linked into the application executable image, and
2603 * would thus reflect the version of libpcap on the system on
2604 * which the application was *linked*, not the system on which it's
2607 * That routine should be documented, unlike the "version[]"
2608 * string, so that UNIX vendors providing their own libpcaps
2609 * don't omit it (as a couple of vendors have...).
2611 * Packet.dll should perhaps also export a routine to return the
2612 * version number of the Packet.dll code, to supply the
2613 * "Wpcap_version" information on Windows.
2615 char WDversion
[]="current-git.tcpdump.org";
2616 #if !defined(HAVE_GENERATED_VERSION)
2617 char version
[]="current-git.tcpdump.org";
2619 char pcap_version
[]="current-git.tcpdump.org";
2620 char Wpcap_version
[]="3.1";
2623 #ifdef SIGNAL_REQ_INFO
2624 RETSIGTYPE
requestinfo(int signo _U_
)
2634 * Called once each second in verbose mode while dumping to file
2636 #ifdef USE_WIN32_MM_TIMER
2637 void CALLBACK
verbose_stats_dump (UINT timer_id _U_
, UINT msg _U_
, DWORD_PTR arg _U_
,
2638 DWORD_PTR dw1 _U_
, DWORD_PTR dw2 _U_
)
2641 fprintf(stderr
, "Got %u\r", packets_captured
);
2643 #elif defined(HAVE_ALARM)
2644 static void verbose_stats_dump(int sig _U_
)
2647 fprintf(stderr
, "Got %u\r", packets_captured
);
2652 USES_APPLE_DEPRECATED_API
2656 extern char version
[];
2657 #ifndef HAVE_PCAP_LIB_VERSION
2658 #if defined(_WIN32) || defined(HAVE_PCAP_VERSION)
2659 extern char pcap_version
[];
2660 #else /* defined(_WIN32) || defined(HAVE_PCAP_VERSION) */
2661 static char pcap_version
[] = "unknown";
2662 #endif /* defined(_WIN32) || defined(HAVE_PCAP_VERSION) */
2663 #endif /* HAVE_PCAP_LIB_VERSION */
2664 const char *smi_version_string
;
2666 #ifdef HAVE_PCAP_LIB_VERSION
2668 (void)fprintf(stderr
, "%s version %s, based on tcpdump version %s\n", program_name
, WDversion
, version
);
2670 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
2672 (void)fprintf(stderr
, "%s\n",pcap_lib_version());
2673 #else /* HAVE_PCAP_LIB_VERSION */
2675 (void)fprintf(stderr
, "%s version %s, based on tcpdump version %s\n", program_name
, WDversion
, version
);
2676 (void)fprintf(stderr
, "WinPcap version %s, based on libpcap version %s\n",Wpcap_version
, pcap_version
);
2678 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
2679 (void)fprintf(stderr
, "libpcap version %s\n", pcap_version
);
2681 #endif /* HAVE_PCAP_LIB_VERSION */
2683 #if defined(HAVE_LIBCRYPTO) && defined(SSLEAY_VERSION)
2684 (void)fprintf (stderr
, "%s\n", SSLeay_version(SSLEAY_VERSION
));
2687 smi_version_string
= nd_smi_version_string();
2688 if (smi_version_string
!= NULL
)
2689 (void)fprintf (stderr
, "SMI-library: %s\n", smi_version_string
);
2691 #if defined(__SANITIZE_ADDRESS__)
2692 (void)fprintf (stderr
, "Compiled with AddressSanitizer/GCC.\n");
2693 #elif defined(__has_feature)
2694 # if __has_feature(address_sanitizer)
2695 (void)fprintf (stderr
, "Compiled with AddressSanitizer/CLang.\n");
2697 #endif /* __SANITIZE_ADDRESS__ or __has_feature */
2705 (void)fprintf(stderr
,
2706 "Usage: %s [-aAbd" D_FLAG
"efhH" I_FLAG J_FLAG
"KlLnNOpqStu" U_FLAG
"vxX#]" B_FLAG_USAGE
" [ -c count ]\n", program_name
);
2707 (void)fprintf(stderr
,
2708 "\t\t[ -C file_size ] [ -E algo:secret ] [ -F file ] [ -G seconds ]\n");
2709 (void)fprintf(stderr
,
2710 "\t\t[ -i interface ]" j_FLAG_USAGE
" [ -M secret ] [ --number ]\n");
2711 #ifdef HAVE_PCAP_SETDIRECTION
2712 (void)fprintf(stderr
,
2713 "\t\t[ -Q in|out|inout ]\n");
2715 (void)fprintf(stderr
,
2716 "\t\t[ -r file ] [ -s snaplen ] ");
2717 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
2718 (void)fprintf(stderr
, "[ --time-stamp-precision precision ]\n");
2719 (void)fprintf(stderr
,
2722 #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
2723 (void)fprintf(stderr
, "[ --immediate-mode ] ");
2725 (void)fprintf(stderr
, "[ -T type ] [ --version ] [ -V file ]\n");
2726 (void)fprintf(stderr
,
2727 "\t\t[ -w file ] [ -W filecount ] [ -y datalinktype ] [ -z postrotate-command ]\n");
2728 (void)fprintf(stderr
,
2729 "\t\t[ -Z user ] [ expression ]\n");
2733 * c-style: whitesmith