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 #ifdef HAVE_PCAP_FTELL64
144 static int64_t Cflag
; /* rotate dump files after this many bytes */
146 static long Cflag
; /* rotate dump files after this many bytes */
148 static int Cflag_count
; /* Keep track of which file number we're writing */
149 static int Dflag
; /* list available devices and exit */
151 * This is exported because, in some versions of libpcap, if libpcap
152 * is built with optimizer debugging code (which is *NOT* the default
153 * configuration!), the library *imports*(!) a variable named dflag,
154 * under the expectation that tcpdump is exporting it, to govern
155 * how much debugging information to print when optimizing
156 * the generated BPF code.
158 * This is a horrible hack; newer versions of libpcap don't import
159 * dflag but, instead, *if* built with optimizer debugging code,
160 * *export* a routine to set that flag.
162 int dflag
; /* print filter code */
163 static int Gflag
; /* rotate dump files after this many seconds */
164 static int Gflag_count
; /* number of files created with Gflag rotation */
165 static time_t Gflag_time
; /* The last time_t the dump file was rotated. */
166 static int Lflag
; /* list available data link types and exit */
167 static int Iflag
; /* rfmon (monitor) mode */
168 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
169 static int Jflag
; /* list available time stamp types */
171 static int jflag
= -1; /* packet time stamp source */
172 static int pflag
; /* don't go promiscuous */
173 #ifdef HAVE_PCAP_SETDIRECTION
174 static int Qflag
= -1; /* restrict captured packet by send/receive direction */
176 static int Uflag
; /* "unbuffered" output of dump files */
177 static int Wflag
; /* recycle output files after this number of files */
178 static int WflagChars
;
179 static char *zflag
= NULL
; /* compress each savefile using a specified command (like gzip or bzip2) */
180 static int immediate_mode
;
182 static int infodelay
;
183 static int infoprint
;
188 cap_channel_t
*capdns
;
192 static NORETURN
void error(FORMAT_STRING(const char *), ...) PRINTFLIKE(1, 2);
193 static void warning(FORMAT_STRING(const char *), ...) PRINTFLIKE(1, 2);
194 static NORETURN
void exit_tcpdump(int);
195 static RETSIGTYPE
cleanup(int);
196 static RETSIGTYPE
child_cleanup(int);
197 static void print_version(void);
198 static void print_usage(void);
199 static NORETURN
void show_tstamp_types_and_exit(pcap_t
*, const char *device
);
200 static NORETURN
void show_dlts_and_exit(pcap_t
*, const char *device
);
201 #ifdef HAVE_PCAP_FINDALLDEVS
202 static NORETURN
void show_devices_and_exit(void);
205 static void print_packet(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
206 static void dump_packet_and_trunc(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
207 static void dump_packet(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
208 static void droproot(const char *, const char *);
210 #ifdef SIGNAL_REQ_INFO
211 RETSIGTYPE
requestinfo(int);
214 #if defined(USE_WIN32_MM_TIMER)
215 #include <MMsystem.h>
216 static UINT timer_id
;
217 static void CALLBACK
verbose_stats_dump(UINT
, UINT
, DWORD_PTR
, DWORD_PTR
, DWORD_PTR
);
218 #elif defined(HAVE_ALARM)
219 static void verbose_stats_dump(int sig
);
222 static void info(int);
223 static u_int packets_captured
;
225 #ifdef HAVE_PCAP_FINDALLDEVS
226 static const struct tok status_flags
[] = {
228 { PCAP_IF_UP
, "Up" },
230 #ifdef PCAP_IF_RUNNING
231 { PCAP_IF_RUNNING
, "Running" },
233 { PCAP_IF_LOOPBACK
, "Loopback" },
240 static int supports_monitor_mode
;
248 char *CurrentFileName
;
256 #if defined(HAVE_PCAP_SET_PARSER_DEBUG)
258 * We have pcap_set_parser_debug() in libpcap; declare it (it's not declared
259 * by any libpcap header, because it's a special hack, only available if
260 * libpcap was configured to include it, and only intended for use by
261 * libpcap developers trying to debug the parser for filter expressions).
264 __declspec(dllimport
)
268 void pcap_set_parser_debug(int);
269 #elif defined(HAVE_PCAP_DEBUG) || defined(HAVE_YYDEBUG)
271 * We don't have pcap_set_parser_debug() in libpcap, but we do have
272 * pcap_debug or yydebug. Make a local version of pcap_set_parser_debug()
273 * to set the flag, and define HAVE_PCAP_SET_PARSER_DEBUG.
276 pcap_set_parser_debug(int value
)
278 #ifdef HAVE_PCAP_DEBUG
279 extern int pcap_debug
;
282 #else /* HAVE_PCAP_DEBUG */
286 #endif /* HAVE_PCAP_DEBUG */
289 #define HAVE_PCAP_SET_PARSER_DEBUG
292 #if defined(HAVE_PCAP_SET_OPTIMIZER_DEBUG)
294 * We have pcap_set_optimizer_debug() in libpcap; declare it (it's not declared
295 * by any libpcap header, because it's a special hack, only available if
296 * libpcap was configured to include it, and only intended for use by
297 * libpcap developers trying to debug the optimizer for filter expressions).
300 __declspec(dllimport
)
304 void pcap_set_optimizer_debug(int);
309 error(const char *fmt
, ...)
313 (void)fprintf(stderr
, "%s: ", program_name
);
315 (void)vfprintf(stderr
, fmt
, ap
);
320 (void)fputc('\n', stderr
);
328 warning(const char *fmt
, ...)
332 (void)fprintf(stderr
, "%s: WARNING: ", program_name
);
334 (void)vfprintf(stderr
, fmt
, ap
);
339 (void)fputc('\n', stderr
);
344 exit_tcpdump(int status
)
350 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
352 show_tstamp_types_and_exit(pcap_t
*pc
, const char *device
)
355 int *tstamp_types
= 0;
356 const char *tstamp_type_name
;
359 n_tstamp_types
= pcap_list_tstamp_types(pc
, &tstamp_types
);
360 if (n_tstamp_types
< 0)
361 error("%s", pcap_geterr(pc
));
363 if (n_tstamp_types
== 0) {
364 fprintf(stderr
, "Time stamp type cannot be set for %s\n",
368 fprintf(stderr
, "Time stamp types for %s (use option -j to set):\n",
370 for (i
= 0; i
< n_tstamp_types
; i
++) {
371 tstamp_type_name
= pcap_tstamp_type_val_to_name(tstamp_types
[i
]);
372 if (tstamp_type_name
!= NULL
) {
373 (void) fprintf(stderr
, " %s (%s)\n", tstamp_type_name
,
374 pcap_tstamp_type_val_to_description(tstamp_types
[i
]));
376 (void) fprintf(stderr
, " %d\n", tstamp_types
[i
]);
379 pcap_free_tstamp_types(tstamp_types
);
385 show_dlts_and_exit(pcap_t
*pc
, const char *device
)
389 const char *dlt_name
;
391 n_dlts
= pcap_list_datalinks(pc
, &dlts
);
393 error("%s", pcap_geterr(pc
));
394 else if (n_dlts
== 0 || !dlts
)
395 error("No data link types.");
398 * If the interface is known to support monitor mode, indicate
399 * whether these are the data link types available when not in
400 * monitor mode, if -I wasn't specified, or when in monitor mode,
401 * when -I was specified (the link-layer types available in
402 * monitor mode might be different from the ones available when
403 * not in monitor mode).
405 if (supports_monitor_mode
)
406 (void) fprintf(stderr
, "Data link types for %s %s (use option -y to set):\n",
408 Iflag
? "when in monitor mode" : "when not in monitor mode");
410 (void) fprintf(stderr
, "Data link types for %s (use option -y to set):\n",
413 for (i
= 0; i
< n_dlts
; i
++) {
414 dlt_name
= pcap_datalink_val_to_name(dlts
[i
]);
415 if (dlt_name
!= NULL
) {
416 (void) fprintf(stderr
, " %s (%s)", dlt_name
,
417 pcap_datalink_val_to_description(dlts
[i
]));
420 * OK, does tcpdump handle that type?
422 if (!has_printer(dlts
[i
]))
423 (void) fprintf(stderr
, " (printing not supported)");
424 fprintf(stderr
, "\n");
426 (void) fprintf(stderr
, " DLT %d (printing not supported)\n",
430 #ifdef HAVE_PCAP_FREE_DATALINKS
431 pcap_free_datalinks(dlts
);
436 #ifdef HAVE_PCAP_FINDALLDEVS
438 show_devices_and_exit (void)
440 pcap_if_t
*dev
, *devlist
;
441 char ebuf
[PCAP_ERRBUF_SIZE
];
444 if (pcap_findalldevs(&devlist
, ebuf
) < 0)
446 for (i
= 0, dev
= devlist
; dev
!= NULL
; i
++, dev
= dev
->next
) {
447 printf("%d.%s", i
+1, dev
->name
);
448 if (dev
->description
!= NULL
)
449 printf(" (%s)", dev
->description
);
451 printf(" [%s]", bittok2str(status_flags
, "none", dev
->flags
));
454 pcap_freealldevs(devlist
);
457 #endif /* HAVE_PCAP_FINDALLDEVS */
462 * Note that there we use all letters for short options except for g, k,
463 * o, and P, and those are used by other versions of tcpdump, and we should
464 * only use them for the same purposes that the other versions of tcpdump
467 * OS X tcpdump uses -g to force non--v output for IP to be on one
468 * line, making it more "g"repable;
470 * OS X tcpdump uses -k to specify that packet comments in pcap-ng files
473 * OpenBSD tcpdump uses -o to indicate that OS fingerprinting should be done
474 * for hosts sending TCP SYN packets;
476 * OS X tcpdump uses -P to indicate that -w should write pcap-ng rather
479 * OS X tcpdump also uses -Q to specify expressions that match packet
480 * metadata, including but not limited to the packet direction.
481 * The expression syntax is different from a simple "in|out|inout",
482 * and those expressions aren't accepted by OS X tcpdump, but the
483 * equivalents would be "in" = "dir=in", "out" = "dir=out", and
484 * "inout" = "dir=in or dir=out", and the parser could conceivably
485 * special-case "in", "out", and "inout" as expressions for backwards
486 * compatibility, so all is not (yet) lost.
490 * Set up flags that might or might not be supported depending on the
491 * version of libpcap we're using.
493 #if defined(HAVE_PCAP_CREATE) || defined(_WIN32)
495 #define B_FLAG_USAGE " [ -B size ]"
496 #else /* defined(HAVE_PCAP_CREATE) || defined(_WIN32) */
499 #endif /* defined(HAVE_PCAP_CREATE) || defined(_WIN32) */
501 #ifdef HAVE_PCAP_CREATE
503 #else /* HAVE_PCAP_CREATE */
505 #endif /* HAVE_PCAP_CREATE */
507 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
509 #define j_FLAG_USAGE " [ -j tstamptype ]"
511 #else /* PCAP_ERROR_TSTAMP_TYPE_NOTSUP */
515 #endif /* PCAP_ERROR_TSTAMP_TYPE_NOTSUP */
517 #ifdef HAVE_PCAP_FINDALLDEVS
523 #ifdef HAVE_PCAP_DUMP_FLUSH
529 #ifdef HAVE_PCAP_SETDIRECTION
535 #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:#"
540 * We do not currently have long options corresponding to all short
541 * options; we should probably pick appropriate option names for them.
543 * However, the short options where the number of times the option is
544 * specified matters, such as -v and -d and -t, should probably not
545 * just map to a long option, as saying
547 * tcpdump --verbose --verbose
549 * doesn't make sense; it should be --verbosity={N} or something such
552 * For long options with no corresponding short options, we define values
553 * outside the range of ASCII graphic characters, make that the last
554 * component of the entry for the long option, and have a case for that
555 * option in the switch statement.
557 #define OPTION_VERSION 128
558 #define OPTION_TSTAMP_PRECISION 129
559 #define OPTION_IMMEDIATE_MODE 130
561 static const struct option longopts
[] = {
562 #if defined(HAVE_PCAP_CREATE) || defined(_WIN32)
563 { "buffer-size", required_argument
, NULL
, 'B' },
565 { "list-interfaces", no_argument
, NULL
, 'D' },
566 { "help", no_argument
, NULL
, 'h' },
567 { "interface", required_argument
, NULL
, 'i' },
568 #ifdef HAVE_PCAP_CREATE
569 { "monitor-mode", no_argument
, NULL
, 'I' },
571 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
572 { "time-stamp-type", required_argument
, NULL
, 'j' },
573 { "list-time-stamp-types", no_argument
, NULL
, 'J' },
575 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
576 { "time-stamp-precision", required_argument
, NULL
, OPTION_TSTAMP_PRECISION
},
578 { "dont-verify-checksums", no_argument
, NULL
, 'K' },
579 { "list-data-link-types", no_argument
, NULL
, 'L' },
580 { "no-optimize", no_argument
, NULL
, 'O' },
581 { "no-promiscuous-mode", no_argument
, NULL
, 'p' },
582 #ifdef HAVE_PCAP_SETDIRECTION
583 { "direction", required_argument
, NULL
, 'Q' },
585 { "snapshot-length", required_argument
, NULL
, 's' },
586 { "absolute-tcp-sequence-numbers", no_argument
, NULL
, 'S' },
587 #ifdef HAVE_PCAP_DUMP_FLUSH
588 { "packet-buffered", no_argument
, NULL
, 'U' },
590 { "linktype", required_argument
, NULL
, 'y' },
591 #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
592 { "immediate-mode", no_argument
, NULL
, OPTION_IMMEDIATE_MODE
},
594 #ifdef HAVE_PCAP_SET_PARSER_DEBUG
595 { "debug-filter-parser", no_argument
, NULL
, 'Y' },
597 { "relinquish-privileges", required_argument
, NULL
, 'Z' },
598 { "number", no_argument
, NULL
, '#' },
599 { "version", no_argument
, NULL
, OPTION_VERSION
},
604 /* Drop root privileges and chroot if necessary */
606 droproot(const char *username
, const char *chroot_dir
)
608 struct passwd
*pw
= NULL
;
610 if (chroot_dir
&& !username
) {
611 fprintf(stderr
, "%s: Chroot without dropping root is insecure\n",
616 pw
= getpwnam(username
);
619 if (chroot(chroot_dir
) != 0 || chdir ("/") != 0) {
620 fprintf(stderr
, "%s: Couldn't chroot/chdir to '%.64s': %s\n",
621 program_name
, chroot_dir
, pcap_strerror(errno
));
625 #ifdef HAVE_LIBCAP_NG
627 int ret
= capng_change_id(pw
->pw_uid
, pw
->pw_gid
, CAPNG_NO_FLAG
);
629 fprintf(stderr
, "error : ret %d\n", ret
);
631 fprintf(stderr
, "dropped privs to %s\n", username
);
635 if (initgroups(pw
->pw_name
, pw
->pw_gid
) != 0 ||
636 setgid(pw
->pw_gid
) != 0 || setuid(pw
->pw_uid
) != 0) {
637 fprintf(stderr
, "%s: Couldn't change to '%.32s' uid=%lu gid=%lu: %s\n",
638 program_name
, username
,
639 (unsigned long)pw
->pw_uid
,
640 (unsigned long)pw
->pw_gid
,
641 pcap_strerror(errno
));
645 fprintf(stderr
, "dropped privs to %s\n", username
);
647 #endif /* HAVE_LIBCAP_NG */
650 fprintf(stderr
, "%s: Couldn't find user '%.32s'\n",
651 program_name
, username
);
654 #ifdef HAVE_LIBCAP_NG
655 /* We don't need CAP_SETUID, CAP_SETGID and CAP_SYS_CHROOT any more. */
658 CAPNG_EFFECTIVE
| CAPNG_PERMITTED
,
663 capng_apply(CAPNG_SELECT_BOTH
);
664 #endif /* HAVE_LIBCAP_NG */
685 MakeFilename(char *buffer
, char *orig_name
, int cnt
, int max_chars
)
687 char *filename
= malloc(PATH_MAX
+ 1);
688 if (filename
== NULL
)
689 error("Makefilename: malloc");
691 /* Process with strftime if Gflag is set. */
695 /* Convert Gflag_time to a usable format */
696 if ((local_tm
= localtime(&Gflag_time
)) == NULL
) {
697 error("MakeTimedFilename: localtime");
700 /* There's no good way to detect an error in strftime since a return
701 * value of 0 isn't necessarily failure.
703 strftime(filename
, PATH_MAX
, orig_name
, local_tm
);
705 strncpy(filename
, orig_name
, PATH_MAX
);
708 if (cnt
== 0 && max_chars
== 0)
709 strncpy(buffer
, filename
, PATH_MAX
+ 1);
711 if (snprintf(buffer
, PATH_MAX
+ 1, "%s%0*d", filename
, max_chars
, cnt
) > PATH_MAX
)
712 /* Report an error if the filename is too large */
713 error("too many output files or filename is too long (> %d)", PATH_MAX
);
718 get_next_file(FILE *VFile
, char *ptr
)
722 ret
= fgets(ptr
, PATH_MAX
, VFile
);
726 if (ptr
[strlen(ptr
) - 1] == '\n')
727 ptr
[strlen(ptr
) - 1] = '\0';
733 static cap_channel_t
*
736 cap_channel_t
*capcas
, *capdnsloc
;
737 const char *types
[1];
742 error("unable to create casper process");
743 capdnsloc
= cap_service_open(capcas
, "system.dns");
744 /* Casper capability no longer needed. */
746 if (capdnsloc
== NULL
)
747 error("unable to open system.dns service");
748 /* Limit system.dns to reverse DNS lookups. */
750 if (cap_dns_type_limit(capdnsloc
, types
, 1) < 0)
751 error("unable to limit access to system.dns service");
752 families
[0] = AF_INET
;
753 families
[1] = AF_INET6
;
754 if (cap_dns_family_limit(capdnsloc
, families
, 2) < 0)
755 error("unable to limit access to system.dns service");
759 #endif /* HAVE_CASPER */
761 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
763 tstamp_precision_from_string(const char *precision
)
765 if (strncmp(precision
, "nano", strlen("nano")) == 0)
766 return PCAP_TSTAMP_PRECISION_NANO
;
768 if (strncmp(precision
, "micro", strlen("micro")) == 0)
769 return PCAP_TSTAMP_PRECISION_MICRO
;
775 tstamp_precision_to_string(int precision
)
779 case PCAP_TSTAMP_PRECISION_MICRO
:
782 case PCAP_TSTAMP_PRECISION_NANO
:
793 * Ensure that, on a dump file's descriptor, we have all the rights
794 * necessary to make the standard I/O library work with an fdopen()ed
795 * FILE * from that descriptor.
797 * A long time ago, in a galaxy far far away, AT&T decided that, instead
798 * of providing separate APIs for getting and setting the FD_ flags on a
799 * descriptor, getting and setting the O_ flags on a descriptor, and
800 * locking files, they'd throw them all into a kitchen-sink fcntl() call
801 * along the lines of ioctl(), the fact that ioctl() operations are
802 * largely specific to particular character devices but fcntl() operations
803 * are either generic to all descriptors or generic to all descriptors for
804 * regular files nonwithstanding.
806 * The Capsicum people decided that fine-grained control of descriptor
807 * operations was required, so that you need to grant permission for
808 * reading, writing, seeking, and fcntl-ing. The latter, courtesy of
809 * AT&T's decision, means that "fcntl-ing" isn't a thing, but a motley
810 * collection of things, so there are *individual* fcntls for which
811 * permission needs to be granted.
813 * The FreeBSD standard I/O people implemented some optimizations that
814 * requires that the standard I/O routines be able to determine whether
815 * the descriptor for the FILE * is open append-only or not; as that
816 * descriptor could have come from an open() rather than an fopen(),
817 * that requires that it be able to do an F_GETFL fcntl() to read
820 * Tcpdump uses ftell() to determine how much data has been written
821 * to a file in order to, when used with -C, determine when it's time
822 * to rotate capture files. ftell() therefore needs to do an lseek()
823 * to find out the file offset and must, thanks to the aforementioned
824 * optimization, also know whether the descriptor is open append-only
827 * The net result of all the above is that we need to grant CAP_SEEK,
828 * CAP_WRITE, and CAP_FCNTL with the CAP_FCNTL_GETFL subcapability.
830 * Perhaps this is the universe's way of saying that either
832 * 1) there needs to be an fopenat() call and a pcap_dump_openat() call
833 * using it, so that Capsicum-capable tcpdump wouldn't need to do
838 * 2) there needs to be a cap_fdopen() call in the FreeBSD standard
839 * I/O library that knows what rights are needed by the standard
840 * I/O library, based on the open mode, and assigns them, perhaps
841 * with an additional argument indicating, for example, whether
842 * seeking should be allowed, so that tcpdump doesn't need to know
843 * what the standard I/O library happens to require this week.
846 set_dumper_capsicum_rights(pcap_dumper_t
*p
)
848 int fd
= fileno(pcap_dump_file(p
));
851 cap_rights_init(&rights
, CAP_SEEK
, CAP_WRITE
, CAP_FCNTL
);
852 if (cap_rights_limit(fd
, &rights
) < 0 && errno
!= ENOSYS
) {
853 error("unable to limit dump descriptor");
855 if (cap_fcntls_limit(fd
, CAP_FCNTL_GETFL
) < 0 && errno
!= ENOSYS
) {
856 error("unable to limit dump descriptor fcntls");
862 * Copy arg vector into a new buffer, concatenating arguments with spaces.
865 copy_argv(register char **argv
)
868 register u_int len
= 0;
877 len
+= strlen(*p
++) + 1;
879 buf
= (char *)malloc(len
);
881 error("copy_argv: malloc");
885 while ((src
= *p
++) != NULL
) {
886 while ((*dst
++ = *src
++) != '\0')
896 * On Windows, we need to open the file in binary mode, so that
897 * we get all the bytes specified by the size we get from "fstat()".
898 * On UNIX, that's not necessary. O_BINARY is defined on Windows;
899 * we define it as 0 if it's not defined, so it does nothing.
906 read_infile(char *fname
)
908 register int i
, fd
, cc
;
912 fd
= open(fname
, O_RDONLY
|O_BINARY
);
914 error("can't open %s: %s", fname
, pcap_strerror(errno
));
916 if (fstat(fd
, &buf
) < 0)
917 error("can't stat %s: %s", fname
, pcap_strerror(errno
));
919 cp
= malloc((u_int
)buf
.st_size
+ 1);
921 error("malloc(%d) for %s: %s", (u_int
)buf
.st_size
+ 1,
922 fname
, pcap_strerror(errno
));
923 cc
= read(fd
, cp
, (u_int
)buf
.st_size
);
925 error("read %s: %s", fname
, pcap_strerror(errno
));
926 if (cc
!= buf
.st_size
)
927 error("short read %s (%d != %d)", fname
, cc
, (int)buf
.st_size
);
930 /* replace "# comment" with spaces */
931 for (i
= 0; i
< cc
; i
++) {
933 while (i
< cc
&& cp
[i
] != '\n')
940 #ifdef HAVE_PCAP_FINDALLDEVS
942 parse_interface_number(const char *device
)
947 devnum
= strtol(device
, &end
, 10);
948 if (device
!= end
&& *end
== '\0') {
950 * It's all-numeric, but is it a valid number?
954 * No, it's not an ordinal.
956 error("Invalid adapter index");
961 * It's not all-numeric; return -1, so our caller
969 find_interface_by_number(long devnum
)
971 pcap_if_t
*dev
, *devlist
;
973 char ebuf
[PCAP_ERRBUF_SIZE
];
976 if (pcap_findalldevs(&devlist
, ebuf
) < 0)
979 * Look for the devnum-th entry in the list of devices (1-based).
981 for (i
= 0, dev
= devlist
; i
< devnum
-1 && dev
!= NULL
;
982 i
++, dev
= dev
->next
)
985 error("Invalid adapter index");
986 device
= strdup(dev
->name
);
987 pcap_freealldevs(devlist
);
993 open_interface(const char *device
, netdissect_options
*ndo
, char *ebuf
)
996 #ifdef HAVE_PCAP_CREATE
1001 #ifdef HAVE_PCAP_CREATE
1002 pc
= pcap_create(device
, ebuf
);
1005 * If this failed with "No such device", that means
1006 * the interface doesn't exist; return NULL, so that
1007 * the caller can see whether the device name is
1008 * actually an interface index.
1010 if (strstr(ebuf
, "No such device") != NULL
)
1014 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
1016 show_tstamp_types_and_exit(pc
, device
);
1018 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
1019 status
= pcap_set_tstamp_precision(pc
, ndo
->ndo_tstamp_precision
);
1021 error("%s: Can't set %ssecond time stamp precision: %s",
1023 tstamp_precision_to_string(ndo
->ndo_tstamp_precision
),
1024 pcap_statustostr(status
));
1027 #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
1028 if (immediate_mode
) {
1029 status
= pcap_set_immediate_mode(pc
, 1);
1031 error("%s: Can't set immediate mode: %s",
1033 pcap_statustostr(status
));
1037 * Is this an interface that supports monitor mode?
1039 if (pcap_can_set_rfmon(pc
) == 1)
1040 supports_monitor_mode
= 1;
1042 supports_monitor_mode
= 0;
1043 if (ndo
->ndo_snaplen
!= 0) {
1045 * A snapshot length was explicitly specified;
1048 status
= pcap_set_snaplen(pc
, ndo
->ndo_snaplen
);
1050 error("%s: Can't set snapshot length: %s",
1051 device
, pcap_statustostr(status
));
1053 status
= pcap_set_promisc(pc
, !pflag
);
1055 error("%s: Can't set promiscuous mode: %s",
1056 device
, pcap_statustostr(status
));
1058 status
= pcap_set_rfmon(pc
, 1);
1060 error("%s: Can't set monitor mode: %s",
1061 device
, pcap_statustostr(status
));
1063 status
= pcap_set_timeout(pc
, 1000);
1065 error("%s: pcap_set_timeout failed: %s",
1066 device
, pcap_statustostr(status
));
1068 status
= pcap_set_buffer_size(pc
, Bflag
);
1070 error("%s: Can't set buffer size: %s",
1071 device
, pcap_statustostr(status
));
1073 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
1075 status
= pcap_set_tstamp_type(pc
, jflag
);
1077 error("%s: Can't set time stamp type: %s",
1078 device
, pcap_statustostr(status
));
1081 status
= pcap_activate(pc
);
1084 * pcap_activate() failed.
1086 cp
= pcap_geterr(pc
);
1087 if (status
== PCAP_ERROR
)
1089 else if (status
== PCAP_ERROR_NO_SUCH_DEVICE
) {
1091 * Return an error for our caller to handle.
1093 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "%s: %s\n(%s)",
1094 device
, pcap_statustostr(status
), cp
);
1097 } else if (status
== PCAP_ERROR_PERM_DENIED
&& *cp
!= '\0')
1098 error("%s: %s\n(%s)", device
,
1099 pcap_statustostr(status
), cp
);
1101 else if (status
== PCAP_ERROR_RFMON_NOTSUP
&&
1102 strncmp(device
, "wlan", 4) == 0) {
1103 char parent
[8], newdev
[8];
1105 size_t s
= sizeof(parent
);
1107 snprintf(sysctl
, sizeof(sysctl
),
1108 "net.wlan.%d.%%parent", atoi(device
+ 4));
1109 sysctlbyname(sysctl
, parent
, &s
, NULL
, 0);
1110 strlcpy(newdev
, device
, sizeof(newdev
));
1111 /* Suggest a new wlan device. */
1112 /* FIXME: incrementing the index this way is not going to work well
1113 * when the index is 9 or greater but the only consequence in this
1114 * specific case would be an error message that looks a bit odd.
1116 newdev
[strlen(newdev
)-1]++;
1117 error("%s is not a monitor mode VAP\n"
1118 "To create a new monitor mode VAP use:\n"
1119 " ifconfig %s create wlandev %s wlanmode monitor\n"
1120 "and use %s as the tcpdump interface",
1121 device
, newdev
, parent
, newdev
);
1125 error("%s: %s", device
,
1126 pcap_statustostr(status
));
1127 } else if (status
> 0) {
1129 * pcap_activate() succeeded, but it's warning us
1130 * of a problem it had.
1132 cp
= pcap_geterr(pc
);
1133 if (status
== PCAP_WARNING
)
1135 else if (status
== PCAP_WARNING_PROMISC_NOTSUP
&&
1137 warning("%s: %s\n(%s)", device
,
1138 pcap_statustostr(status
), cp
);
1140 warning("%s: %s", device
,
1141 pcap_statustostr(status
));
1143 #ifdef HAVE_PCAP_SETDIRECTION
1145 status
= pcap_setdirection(pc
, Qflag
);
1147 error("%s: pcap_setdirection() failed: %s",
1148 device
, pcap_geterr(pc
));
1150 #endif /* HAVE_PCAP_SETDIRECTION */
1151 #else /* HAVE_PCAP_CREATE */
1154 * If no snapshot length was specified, or a length of 0 was
1155 * specified, default to 256KB.
1157 if (ndo
->ndo_snaplen
== 0)
1158 ndo
->ndo_snaplen
= 262144;
1159 pc
= pcap_open_live(device
, ndo
->ndo_snaplen
, !pflag
, 1000, ebuf
);
1162 * If this failed with "No such device", that means
1163 * the interface doesn't exist; return NULL, so that
1164 * the caller can see whether the device name is
1165 * actually an interface index.
1167 if (strstr(ebuf
, "No such device") != NULL
)
1172 warning("%s", ebuf
);
1173 #endif /* HAVE_PCAP_CREATE */
1179 main(int argc
, char **argv
)
1181 register int cnt
, op
, i
;
1182 bpf_u_int32 localnet
=0 , netmask
= 0;
1183 int timezone_offset
= 0;
1184 register char *cp
, *infile
, *cmdbuf
, *device
, *RFileName
, *VFileName
, *WFileName
;
1186 pcap_handler callback
;
1188 const char *dlt_name
;
1189 struct bpf_program fcode
;
1191 RETSIGTYPE (*oldhandler
)(int);
1193 struct dump_info dumpinfo
;
1194 u_char
*pcap_userdata
;
1195 char ebuf
[PCAP_ERRBUF_SIZE
];
1196 char VFileLine
[PATH_MAX
+ 1];
1197 char *username
= NULL
;
1198 char *chroot_dir
= NULL
;
1201 #ifdef HAVE_PCAP_FINDALLDEVS
1207 #ifdef HAVE_CAPSICUM
1208 cap_rights_t rights
;
1210 #endif /* HAVE_CAPSICUM */
1211 int Oflag
= 1; /* run filter code optimizer */
1213 const char *yflag_dlt_name
= NULL
;
1215 netdissect_options Ndo
;
1216 netdissect_options
*ndo
= &Ndo
;
1219 * Initialize the netdissect code.
1221 if (nd_init(ebuf
, sizeof ebuf
) == -1)
1224 memset(ndo
, 0, sizeof(*ndo
));
1225 ndo_set_function_pointers(ndo
);
1235 if ((cp
= strrchr(argv
[0], '/')) != NULL
)
1236 ndo
->program_name
= program_name
= cp
+ 1;
1238 ndo
->program_name
= program_name
= argv
[0];
1241 if (pcap_wsockinit() != 0)
1242 error("Attempting to initialize Winsock failed");
1246 * On platforms where the CPU doesn't support unaligned loads,
1247 * force unaligned accesses to abort with SIGBUS, rather than
1248 * being fixed up (slowly) by the OS kernel; on those platforms,
1249 * misaligned accesses are bugs, and we want tcpdump to crash so
1250 * that the bugs are reported.
1252 if (abort_on_misalignment(ebuf
, sizeof(ebuf
)) < 0)
1256 (op
= getopt_long(argc
, argv
, SHORTOPTS
, longopts
, NULL
)) != -1)
1260 /* compatibility for old -a */
1271 #if defined(HAVE_PCAP_CREATE) || defined(_WIN32)
1273 Bflag
= atoi(optarg
)*1024;
1275 error("invalid packet buffer size %s", optarg
);
1277 #endif /* defined(HAVE_PCAP_CREATE) || defined(_WIN32) */
1282 error("invalid packet count %s", optarg
);
1287 #ifdef HAVE_PCAP_FTELL64
1288 Cflag
= strtoint64_t(optarg
, &endp
, 10);
1290 Cflag
= strtol(optarg
, &endp
, 10);
1292 if (endp
== optarg
|| *endp
!= '\0' || errno
!= 0
1294 error("invalid file size %s", optarg
);
1296 * Will multiplying it by 1000000 overflow?
1298 #ifdef HAVE_PCAP_FTELL64
1299 if (Cflag
> INT64_T_CONSTANT(0x7fffffffffffffffU
) / 1000000)
1301 if (Cflag
> LONG_MAX
/ 1000000)
1303 error("file size %s is too large", optarg
);
1324 #ifndef HAVE_LIBCRYPTO
1325 warning("crypto code not compiled in");
1327 ndo
->ndo_espsecret
= optarg
;
1339 Gflag
= atoi(optarg
);
1341 error("invalid number of seconds %s", optarg
);
1343 /* We will create one file initially. */
1346 /* Grab the current time for rotation use. */
1347 if ((Gflag_time
= time(NULL
)) == (time_t)-1) {
1348 error("main: can't get current time: %s",
1349 pcap_strerror(errno
));
1366 #ifdef HAVE_PCAP_CREATE
1370 #endif /* HAVE_PCAP_CREATE */
1372 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
1374 jflag
= pcap_tstamp_type_name_to_val(optarg
);
1376 error("invalid time stamp type %s", optarg
);
1387 * _IOLBF is the same as _IOFBF in Microsoft's C
1388 * libraries; the only alternative they offer
1391 * XXX - this should really be checking for MSVC++,
1392 * not _WIN32, if, for example, MinGW has its own
1393 * C library that is more UNIX-compatible.
1395 setvbuf(stdout
, NULL
, _IONBF
, 0);
1397 #ifdef HAVE_SETLINEBUF
1400 setvbuf(stdout
, NULL
, _IOLBF
, 0);
1410 if (nd_have_smi_support()) {
1411 if (nd_load_smi_module(optarg
, ebuf
, sizeof ebuf
) == -1)
1414 (void)fprintf(stderr
, "%s: ignoring option `-m %s' ",
1415 program_name
, optarg
);
1416 (void)fprintf(stderr
, "(no libsmi support)\n");
1421 /* TCP-MD5 shared secret */
1422 #ifndef HAVE_LIBCRYPTO
1423 warning("crypto code not compiled in");
1425 ndo
->ndo_sigsecret
= optarg
;
1446 ++ndo
->ndo_suppress_default_print
;
1449 #ifdef HAVE_PCAP_SETDIRECTION
1451 if (ascii_strcasecmp(optarg
, "in") == 0)
1453 else if (ascii_strcasecmp(optarg
, "out") == 0)
1455 else if (ascii_strcasecmp(optarg
, "inout") == 0)
1456 Qflag
= PCAP_D_INOUT
;
1458 error("unknown capture direction `%s'", optarg
);
1460 #endif /* HAVE_PCAP_SETDIRECTION */
1467 ndo
->ndo_snaplen
= strtol(optarg
, &end
, 0);
1468 if (optarg
== end
|| *end
!= '\0'
1469 || ndo
->ndo_snaplen
< 0 || ndo
->ndo_snaplen
> MAXIMUM_SNAPLEN
)
1470 error("invalid snaplen %s", optarg
);
1482 if (ascii_strcasecmp(optarg
, "vat") == 0)
1483 ndo
->ndo_packettype
= PT_VAT
;
1484 else if (ascii_strcasecmp(optarg
, "wb") == 0)
1485 ndo
->ndo_packettype
= PT_WB
;
1486 else if (ascii_strcasecmp(optarg
, "rpc") == 0)
1487 ndo
->ndo_packettype
= PT_RPC
;
1488 else if (ascii_strcasecmp(optarg
, "rtp") == 0)
1489 ndo
->ndo_packettype
= PT_RTP
;
1490 else if (ascii_strcasecmp(optarg
, "rtcp") == 0)
1491 ndo
->ndo_packettype
= PT_RTCP
;
1492 else if (ascii_strcasecmp(optarg
, "snmp") == 0)
1493 ndo
->ndo_packettype
= PT_SNMP
;
1494 else if (ascii_strcasecmp(optarg
, "cnfp") == 0)
1495 ndo
->ndo_packettype
= PT_CNFP
;
1496 else if (ascii_strcasecmp(optarg
, "tftp") == 0)
1497 ndo
->ndo_packettype
= PT_TFTP
;
1498 else if (ascii_strcasecmp(optarg
, "aodv") == 0)
1499 ndo
->ndo_packettype
= PT_AODV
;
1500 else if (ascii_strcasecmp(optarg
, "carp") == 0)
1501 ndo
->ndo_packettype
= PT_CARP
;
1502 else if (ascii_strcasecmp(optarg
, "radius") == 0)
1503 ndo
->ndo_packettype
= PT_RADIUS
;
1504 else if (ascii_strcasecmp(optarg
, "zmtp1") == 0)
1505 ndo
->ndo_packettype
= PT_ZMTP1
;
1506 else if (ascii_strcasecmp(optarg
, "vxlan") == 0)
1507 ndo
->ndo_packettype
= PT_VXLAN
;
1508 else if (ascii_strcasecmp(optarg
, "pgm") == 0)
1509 ndo
->ndo_packettype
= PT_PGM
;
1510 else if (ascii_strcasecmp(optarg
, "pgm_zmtp1") == 0)
1511 ndo
->ndo_packettype
= PT_PGM_ZMTP1
;
1512 else if (ascii_strcasecmp(optarg
, "lmp") == 0)
1513 ndo
->ndo_packettype
= PT_LMP
;
1514 else if (ascii_strcasecmp(optarg
, "resp") == 0)
1515 ndo
->ndo_packettype
= PT_RESP
;
1517 error("unknown packet type `%s'", optarg
);
1524 #ifdef HAVE_PCAP_DUMP_FLUSH
1543 Wflag
= atoi(optarg
);
1545 error("invalid number of output files %s", optarg
);
1546 WflagChars
= getWflagChars(Wflag
);
1551 ++ndo
->ndo_suppress_default_print
;
1556 ++ndo
->ndo_suppress_default_print
;
1560 yflag_dlt_name
= optarg
;
1562 pcap_datalink_name_to_val(yflag_dlt_name
);
1564 error("invalid data link type %s", yflag_dlt_name
);
1567 #ifdef HAVE_PCAP_SET_PARSER_DEBUG
1570 /* Undocumented flag */
1571 pcap_set_parser_debug(1);
1584 ndo
->ndo_packet_number
= 1;
1587 case OPTION_VERSION
:
1592 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
1593 case OPTION_TSTAMP_PRECISION
:
1594 ndo
->ndo_tstamp_precision
= tstamp_precision_from_string(optarg
);
1595 if (ndo
->ndo_tstamp_precision
< 0)
1596 error("unsupported time stamp precision");
1600 #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
1601 case OPTION_IMMEDIATE_MODE
:
1612 #ifdef HAVE_PCAP_FINDALLDEVS
1614 show_devices_and_exit();
1617 switch (ndo
->ndo_tflag
) {
1619 case 0: /* Default */
1620 case 4: /* Default + Date*/
1621 timezone_offset
= gmt2local(0);
1624 case 1: /* No time stamp */
1625 case 2: /* Unix timeval style */
1626 case 3: /* Microseconds since previous packet */
1627 case 5: /* Microseconds since first packet */
1630 default: /* Not supported */
1631 error("only -t, -tt, -ttt, -tttt and -ttttt are supported");
1635 if (ndo
->ndo_fflag
!= 0 && (VFileName
!= NULL
|| RFileName
!= NULL
))
1636 error("-f can not be used with -V or -r");
1638 if (VFileName
!= NULL
&& RFileName
!= NULL
)
1639 error("-V and -r are mutually exclusive.");
1641 #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
1643 * If we're printing dissected packets to the standard output
1644 * rather than saving raw packets to a file, and the standard
1645 * output is a terminal, use immediate mode, as the user's
1646 * probably expecting to see packets pop up immediately.
1648 if (WFileName
== NULL
&& isatty(1))
1653 /* if run as root, prepare for chrooting */
1654 if (getuid() == 0 || geteuid() == 0) {
1655 /* future extensibility for cmd-line arguments */
1657 chroot_dir
= WITH_CHROOT
;
1662 /* if run as root, prepare for dropping root privileges */
1663 if (getuid() == 0 || geteuid() == 0) {
1664 /* Run with '-Z root' to restore old behaviour */
1666 username
= WITH_USER
;
1670 if (RFileName
!= NULL
|| VFileName
!= NULL
) {
1672 * If RFileName is non-null, it's the pathname of a
1673 * savefile to read. If VFileName is non-null, it's
1674 * the pathname of a file containing a list of pathnames
1675 * (one per line) of savefiles to read.
1677 * In either case, we're reading a savefile, not doing
1682 * We don't need network access, so relinquish any set-UID
1683 * or set-GID privileges we have (if any).
1685 * We do *not* want set-UID privileges when opening a
1686 * trace file, as that might let the user read other
1687 * people's trace files (especially if we're set-UID
1690 if (setgid(getgid()) != 0 || setuid(getuid()) != 0 )
1691 fprintf(stderr
, "Warning: setgid/setuid failed !\n");
1693 if (VFileName
!= NULL
) {
1694 if (VFileName
[0] == '-' && VFileName
[1] == '\0')
1697 VFile
= fopen(VFileName
, "r");
1700 error("Unable to open file: %s\n", pcap_strerror(errno
));
1702 ret
= get_next_file(VFile
, VFileLine
);
1704 error("Nothing in %s\n", VFileName
);
1705 RFileName
= VFileLine
;
1708 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
1709 pd
= pcap_open_offline_with_tstamp_precision(RFileName
,
1710 ndo
->ndo_tstamp_precision
, ebuf
);
1712 pd
= pcap_open_offline(RFileName
, ebuf
);
1717 #ifdef HAVE_CAPSICUM
1718 cap_rights_init(&rights
, CAP_READ
);
1719 if (cap_rights_limit(fileno(pcap_file(pd
)), &rights
) < 0 &&
1721 error("unable to limit pcap descriptor");
1724 dlt
= pcap_datalink(pd
);
1725 dlt_name
= pcap_datalink_val_to_name(dlt
);
1726 if (dlt_name
== NULL
) {
1727 fprintf(stderr
, "reading from file %s, link-type %u\n",
1731 "reading from file %s, link-type %s (%s)\n",
1732 RFileName
, dlt_name
,
1733 pcap_datalink_val_to_description(dlt
));
1737 * We're doing a live capture.
1739 if (device
== NULL
) {
1741 * No interface was specified. Pick one.
1743 #ifdef HAVE_PCAP_FINDALLDEVS
1745 * Find the list of interfaces, and pick
1746 * the first interface.
1748 if (pcap_findalldevs(&devlist
, ebuf
) == -1)
1750 if (devlist
== NULL
)
1751 error("no interfaces available for capture");
1752 device
= strdup(devlist
->name
);
1753 pcap_freealldevs(devlist
);
1754 #else /* HAVE_PCAP_FINDALLDEVS */
1756 * Use whatever interface pcap_lookupdev()
1759 device
= pcap_lookupdev(ebuf
);
1766 * Try to open the interface with the specified name.
1768 pd
= open_interface(device
, ndo
, ebuf
);
1771 * That failed. If we can get a list of
1772 * interfaces, and the interface name
1773 * is purely numeric, try to use it as
1774 * a 1-based index in the list of
1777 #ifdef HAVE_PCAP_FINDALLDEVS
1778 devnum
= parse_interface_number(device
);
1781 * It's not a number; just report
1782 * the open error and fail.
1788 * OK, it's a number; try to find the
1789 * interface with that index, and try
1792 * find_interface_by_number() exits if it
1793 * couldn't be found.
1795 device
= find_interface_by_number(devnum
);
1796 pd
= open_interface(device
, ndo
, ebuf
);
1799 #else /* HAVE_PCAP_FINDALLDEVS */
1801 * We can't get a list of interfaces; just
1805 #endif /* HAVE_PCAP_FINDALLDEVS */
1809 * Let user own process after capture device has
1813 if (setgid(getgid()) != 0 || setuid(getuid()) != 0)
1814 fprintf(stderr
, "Warning: setgid/setuid failed !\n");
1816 #if !defined(HAVE_PCAP_CREATE) && defined(_WIN32)
1818 if(pcap_setbuff(pd
, Bflag
)==-1){
1819 error("%s", pcap_geterr(pd
));
1821 #endif /* !defined(HAVE_PCAP_CREATE) && defined(_WIN32) */
1823 show_dlts_and_exit(pd
, device
);
1824 if (yflag_dlt
>= 0) {
1825 #ifdef HAVE_PCAP_SET_DATALINK
1826 if (pcap_set_datalink(pd
, yflag_dlt
) < 0)
1827 error("%s", pcap_geterr(pd
));
1830 * We don't actually support changing the
1831 * data link type, so we only let them
1832 * set it to what it already is.
1834 if (yflag_dlt
!= pcap_datalink(pd
)) {
1835 error("%s is not one of the DLTs supported by this device\n",
1839 (void)fprintf(stderr
, "%s: data link type %s\n",
1840 program_name
, yflag_dlt_name
);
1841 (void)fflush(stderr
);
1843 i
= pcap_snapshot(pd
);
1844 if (ndo
->ndo_snaplen
< i
) {
1845 if (ndo
->ndo_snaplen
!= 0)
1846 warning("snaplen raised from %d to %d", ndo
->ndo_snaplen
, i
);
1847 ndo
->ndo_snaplen
= i
;
1848 } else if (ndo
->ndo_snaplen
> i
) {
1849 warning("snaplen lowered from %d to %d", ndo
->ndo_snaplen
, i
);
1850 ndo
->ndo_snaplen
= i
;
1852 if(ndo
->ndo_fflag
!= 0) {
1853 if (pcap_lookupnet(device
, &localnet
, &netmask
, ebuf
) < 0) {
1854 warning("foreign (-f) flag used but: %s", ebuf
);
1860 cmdbuf
= read_infile(infile
);
1862 cmdbuf
= copy_argv(&argv
[optind
]);
1864 #ifdef HAVE_PCAP_SET_OPTIMIZER_DEBUG
1865 pcap_set_optimizer_debug(dflag
);
1867 if (pcap_compile(pd
, &fcode
, cmdbuf
, Oflag
, netmask
) < 0)
1868 error("%s", pcap_geterr(pd
));
1870 bpf_dump(&fcode
, dflag
);
1873 pcap_freecode(&fcode
);
1878 if (!ndo
->ndo_nflag
)
1879 capdns
= capdns_setup();
1880 #endif /* HAVE_CASPER */
1882 init_print(ndo
, localnet
, netmask
, timezone_offset
);
1885 (void)setsignal(SIGPIPE
, cleanup
);
1886 (void)setsignal(SIGTERM
, cleanup
);
1887 (void)setsignal(SIGINT
, cleanup
);
1889 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
1890 (void)setsignal(SIGCHLD
, child_cleanup
);
1892 /* Cooperate with nohup(1) */
1894 if ((oldhandler
= setsignal(SIGHUP
, cleanup
)) != SIG_DFL
)
1895 (void)setsignal(SIGHUP
, oldhandler
);
1900 * If a user name was specified with "-Z", attempt to switch to
1901 * that user's UID. This would probably be used with sudo,
1902 * to allow tcpdump to be run in a special restricted
1903 * account (if you just want to allow users to open capture
1904 * devices, and can't just give users that permission,
1905 * you'd make tcpdump set-UID or set-GID).
1907 * Tcpdump doesn't necessarily write only to one savefile;
1908 * the general only way to allow a -Z instance to write to
1909 * savefiles as the user under whose UID it's run, rather
1910 * than as the user specified with -Z, would thus be to switch
1911 * to the original user ID before opening a capture file and
1912 * then switch back to the -Z user ID after opening the savefile.
1913 * Switching to the -Z user ID only after opening the first
1914 * savefile doesn't handle the general case.
1917 if (getuid() == 0 || geteuid() == 0) {
1918 #ifdef HAVE_LIBCAP_NG
1919 /* Initialize capng */
1920 capng_clear(CAPNG_SELECT_BOTH
);
1924 CAPNG_PERMITTED
| CAPNG_EFFECTIVE
,
1932 CAPNG_PERMITTED
| CAPNG_EFFECTIVE
,
1940 CAPNG_PERMITTED
| CAPNG_EFFECTIVE
,
1944 capng_apply(CAPNG_SELECT_BOTH
);
1945 #endif /* HAVE_LIBCAP_NG */
1946 if (username
|| chroot_dir
)
1947 droproot(username
, chroot_dir
);
1952 if (pcap_setfilter(pd
, &fcode
) < 0)
1953 error("%s", pcap_geterr(pd
));
1954 #ifdef HAVE_CAPSICUM
1955 if (RFileName
== NULL
&& VFileName
== NULL
) {
1956 static const unsigned long cmds
[] = { BIOCGSTATS
, BIOCROTZBUF
};
1959 * The various libpcap devices use a combination of
1960 * read (bpf), ioctl (bpf, netmap), poll (netmap)
1961 * so we add the relevant access rights.
1963 cap_rights_init(&rights
, CAP_IOCTL
, CAP_READ
, CAP_EVENT
);
1964 if (cap_rights_limit(pcap_fileno(pd
), &rights
) < 0 &&
1966 error("unable to limit pcap descriptor");
1968 if (cap_ioctls_limit(pcap_fileno(pd
), cmds
,
1969 sizeof(cmds
) / sizeof(cmds
[0])) < 0 && errno
!= ENOSYS
) {
1970 error("unable to limit ioctls on pcap descriptor");
1976 /* Do not exceed the default PATH_MAX for files. */
1977 dumpinfo
.CurrentFileName
= (char *)malloc(PATH_MAX
+ 1);
1979 if (dumpinfo
.CurrentFileName
== NULL
)
1980 error("malloc of dumpinfo.CurrentFileName");
1982 /* We do not need numbering for dumpfiles if Cflag isn't set. */
1984 MakeFilename(dumpinfo
.CurrentFileName
, WFileName
, 0, WflagChars
);
1986 MakeFilename(dumpinfo
.CurrentFileName
, WFileName
, 0, 0);
1988 p
= pcap_dump_open(pd
, dumpinfo
.CurrentFileName
);
1989 #ifdef HAVE_LIBCAP_NG
1990 /* Give up CAP_DAC_OVERRIDE capability.
1991 * Only allow it to be restored if the -C or -G flag have been
1992 * set since we may need to create more files later on.
1996 (Cflag
|| Gflag
? 0 : CAPNG_PERMITTED
)
2000 capng_apply(CAPNG_SELECT_BOTH
);
2001 #endif /* HAVE_LIBCAP_NG */
2003 error("%s", pcap_geterr(pd
));
2004 #ifdef HAVE_CAPSICUM
2005 set_dumper_capsicum_rights(p
);
2007 if (Cflag
!= 0 || Gflag
!= 0) {
2008 #ifdef HAVE_CAPSICUM
2009 dumpinfo
.WFileName
= strdup(basename(WFileName
));
2010 if (dumpinfo
.WFileName
== NULL
) {
2011 error("Unable to allocate memory for file %s",
2014 dumpinfo
.dirfd
= open(dirname(WFileName
),
2015 O_DIRECTORY
| O_RDONLY
);
2016 if (dumpinfo
.dirfd
< 0) {
2017 error("unable to open directory %s",
2018 dirname(WFileName
));
2020 cap_rights_init(&rights
, CAP_CREATE
, CAP_FCNTL
,
2021 CAP_FTRUNCATE
, CAP_LOOKUP
, CAP_SEEK
, CAP_WRITE
);
2022 if (cap_rights_limit(dumpinfo
.dirfd
, &rights
) < 0 &&
2024 error("unable to limit directory rights");
2026 if (cap_fcntls_limit(dumpinfo
.dirfd
, CAP_FCNTL_GETFL
) < 0 &&
2028 error("unable to limit dump descriptor fcntls");
2030 #else /* !HAVE_CAPSICUM */
2031 dumpinfo
.WFileName
= WFileName
;
2033 callback
= dump_packet_and_trunc
;
2036 pcap_userdata
= (u_char
*)&dumpinfo
;
2038 callback
= dump_packet
;
2039 pcap_userdata
= (u_char
*)p
;
2041 #ifdef HAVE_PCAP_DUMP_FLUSH
2046 dlt
= pcap_datalink(pd
);
2047 ndo
->ndo_if_printer
= get_if_printer(ndo
, dlt
);
2048 callback
= print_packet
;
2049 pcap_userdata
= (u_char
*)ndo
;
2052 #ifdef SIGNAL_REQ_INFO
2054 * We can't get statistics when reading from a file rather
2055 * than capturing from a device.
2057 if (RFileName
== NULL
)
2058 (void)setsignal(SIGNAL_REQ_INFO
, requestinfo
);
2061 if (ndo
->ndo_vflag
> 0 && WFileName
) {
2063 * When capturing to a file, "-v" means tcpdump should,
2064 * every 10 seconds, "v"erbosely report the number of
2067 #ifdef USE_WIN32_MM_TIMER
2068 /* call verbose_stats_dump() each 1000 +/-100msec */
2069 timer_id
= timeSetEvent(1000, 100, verbose_stats_dump
, 0, TIME_PERIODIC
);
2070 setvbuf(stderr
, NULL
, _IONBF
, 0);
2071 #elif defined(HAVE_ALARM)
2072 (void)setsignal(SIGALRM
, verbose_stats_dump
);
2077 if (RFileName
== NULL
) {
2079 * Live capture (if -V was specified, we set RFileName
2080 * to a file from the -V file). Print a message to
2081 * the standard error on UN*X.
2083 if (!ndo
->ndo_vflag
&& !WFileName
) {
2084 (void)fprintf(stderr
,
2085 "%s: verbose output suppressed, use -v or -vv for full protocol decode\n",
2088 (void)fprintf(stderr
, "%s: ", program_name
);
2089 dlt
= pcap_datalink(pd
);
2090 dlt_name
= pcap_datalink_val_to_name(dlt
);
2091 if (dlt_name
== NULL
) {
2092 (void)fprintf(stderr
, "listening on %s, link-type %u, capture size %u bytes\n",
2093 device
, dlt
, ndo
->ndo_snaplen
);
2095 (void)fprintf(stderr
, "listening on %s, link-type %s (%s), capture size %u bytes\n",
2097 pcap_datalink_val_to_description(dlt
), ndo
->ndo_snaplen
);
2099 (void)fflush(stderr
);
2102 #ifdef HAVE_CAPSICUM
2103 cansandbox
= (VFileName
== NULL
&& zflag
== NULL
);
2105 cansandbox
= (cansandbox
&& (ndo
->ndo_nflag
|| capdns
!= NULL
));
2107 cansandbox
= (cansandbox
&& ndo
->ndo_nflag
);
2108 #endif /* HAVE_CASPER */
2109 if (cansandbox
&& cap_enter() < 0 && errno
!= ENOSYS
)
2110 error("unable to enter the capability mode");
2111 #endif /* HAVE_CAPSICUM */
2114 status
= pcap_loop(pd
, cnt
, callback
, pcap_userdata
);
2115 if (WFileName
== NULL
) {
2117 * We're printing packets. Flush the printed output,
2118 * so it doesn't get intermingled with error output.
2122 * We got interrupted, so perhaps we didn't
2123 * manage to finish a line we were printing.
2124 * Print an extra newline, just in case.
2128 (void)fflush(stdout
);
2132 * We got interrupted. If we are reading multiple
2133 * files (via -V) set these so that we stop.
2142 (void)fprintf(stderr
, "%s: pcap_loop: %s\n",
2143 program_name
, pcap_geterr(pd
));
2145 if (RFileName
== NULL
) {
2147 * We're doing a live capture. Report the capture
2153 if (VFileName
!= NULL
) {
2154 ret
= get_next_file(VFile
, VFileLine
);
2158 RFileName
= VFileLine
;
2159 pd
= pcap_open_offline(RFileName
, ebuf
);
2162 #ifdef HAVE_CAPSICUM
2163 cap_rights_init(&rights
, CAP_READ
);
2164 if (cap_rights_limit(fileno(pcap_file(pd
)),
2165 &rights
) < 0 && errno
!= ENOSYS
) {
2166 error("unable to limit pcap descriptor");
2169 new_dlt
= pcap_datalink(pd
);
2170 if (new_dlt
!= dlt
) {
2172 * The new file has a different
2173 * link-layer header type from the
2176 if (WFileName
!= NULL
) {
2178 * We're writing raw packets
2179 * that match the filter to
2180 * a pcap file. pcap files
2181 * don't support multiple
2182 * different link-layer
2183 * header types, so we fail
2186 error("%s: new dlt does not match original", RFileName
);
2190 * We're printing the decoded packets;
2191 * switch to the new DLT.
2193 * To do that, we need to change
2194 * the printer, change the DLT name,
2195 * and recompile the filter with
2199 ndo
->ndo_if_printer
= get_if_printer(ndo
, dlt
);
2200 if (pcap_compile(pd
, &fcode
, cmdbuf
, Oflag
, netmask
) < 0)
2201 error("%s", pcap_geterr(pd
));
2205 * Set the filter on the new file.
2207 if (pcap_setfilter(pd
, &fcode
) < 0)
2208 error("%s", pcap_geterr(pd
));
2211 * Report the new file.
2213 dlt_name
= pcap_datalink_val_to_name(dlt
);
2214 if (dlt_name
== NULL
) {
2215 fprintf(stderr
, "reading from file %s, link-type %u\n",
2219 "reading from file %s, link-type %s (%s)\n",
2220 RFileName
, dlt_name
,
2221 pcap_datalink_val_to_description(dlt
));
2226 while (ret
!= NULL
);
2229 pcap_freecode(&fcode
);
2230 exit_tcpdump(status
== -1 ? 1 : 0);
2233 /* make a clean exit on interrupts */
2235 cleanup(int signo _U_
)
2237 #ifdef USE_WIN32_MM_TIMER
2239 timeKillEvent(timer_id
);
2241 #elif defined(HAVE_ALARM)
2245 #ifdef HAVE_PCAP_BREAKLOOP
2247 * We have "pcap_breakloop()"; use it, so that we do as little
2248 * as possible in the signal handler (it's probably not safe
2249 * to do anything with standard I/O streams in a signal handler -
2250 * the ANSI C standard doesn't say it is).
2255 * We don't have "pcap_breakloop()"; this isn't safe, but
2256 * it's the best we can do. Print the summary if we're
2257 * not reading from a savefile - i.e., if we're doing a
2258 * live capture - and exit.
2260 if (pd
!= NULL
&& pcap_file(pd
) == NULL
) {
2262 * We got interrupted, so perhaps we didn't
2263 * manage to finish a line we were printing.
2264 * Print an extra newline, just in case.
2267 (void)fflush(stdout
);
2275 On windows, we do not use a fork, so we do not care less about
2276 waiting a child processes to die
2278 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
2280 child_cleanup(int signo _U_
)
2284 #endif /* HAVE_FORK && HAVE_VFORK */
2287 info(register int verbose
)
2289 struct pcap_stat stats
;
2292 * Older versions of libpcap didn't set ps_ifdrop on some
2293 * platforms; initialize it to 0 to handle that.
2295 stats
.ps_ifdrop
= 0;
2296 if (pcap_stats(pd
, &stats
) < 0) {
2297 (void)fprintf(stderr
, "pcap_stats: %s\n", pcap_geterr(pd
));
2303 fprintf(stderr
, "%s: ", program_name
);
2305 (void)fprintf(stderr
, "%u packet%s captured", packets_captured
,
2306 PLURAL_SUFFIX(packets_captured
));
2308 fputs(", ", stderr
);
2311 (void)fprintf(stderr
, "%u packet%s received by filter", stats
.ps_recv
,
2312 PLURAL_SUFFIX(stats
.ps_recv
));
2314 fputs(", ", stderr
);
2317 (void)fprintf(stderr
, "%u packet%s dropped by kernel", stats
.ps_drop
,
2318 PLURAL_SUFFIX(stats
.ps_drop
));
2319 if (stats
.ps_ifdrop
!= 0) {
2321 fputs(", ", stderr
);
2324 (void)fprintf(stderr
, "%u packet%s dropped by interface\n",
2325 stats
.ps_ifdrop
, PLURAL_SUFFIX(stats
.ps_ifdrop
));
2331 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
2333 #define fork_subprocess() fork()
2335 #define fork_subprocess() vfork()
2338 compress_savefile(const char *filename
)
2342 child
= fork_subprocess();
2345 "compress_savefile: fork failed: %s\n",
2346 pcap_strerror(errno
));
2350 /* Parent process. */
2356 * Set to lowest priority so that this doesn't disturb the capture.
2359 setpriority(PRIO_PROCESS
, 0, NZERO
- 1);
2361 setpriority(PRIO_PROCESS
, 0, 19);
2363 if (execlp(zflag
, zflag
, filename
, (char *)NULL
) == -1)
2365 "compress_savefile: execlp(%s, %s) failed: %s\n",
2368 pcap_strerror(errno
));
2375 #else /* HAVE_FORK && HAVE_VFORK */
2377 compress_savefile(const char *filename
)
2380 "compress_savefile failed. Functionality not implemented under your system\n");
2382 #endif /* HAVE_FORK && HAVE_VFORK */
2385 dump_packet_and_trunc(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
2387 struct dump_info
*dump_info
;
2393 dump_info
= (struct dump_info
*)user
;
2396 * XXX - this won't force the file to rotate on the specified time
2397 * boundary, but it will rotate on the first packet received after the
2398 * specified Gflag number of seconds. Note: if a Gflag time boundary
2399 * and a Cflag size boundary coincide, the time rotation will occur
2400 * first thereby cancelling the Cflag boundary (since the file should
2404 /* Check if it is time to rotate */
2407 /* Get the current time */
2408 if ((t
= time(NULL
)) == (time_t)-1) {
2409 error("dump_and_trunc_packet: can't get current_time: %s",
2410 pcap_strerror(errno
));
2414 /* If the time is greater than the specified window, rotate */
2415 if (t
- Gflag_time
>= Gflag
) {
2416 #ifdef HAVE_CAPSICUM
2421 /* Update the Gflag_time */
2423 /* Update Gflag_count */
2426 * Close the current file and open a new one.
2428 pcap_dump_close(dump_info
->p
);
2431 * Compress the file we just closed, if the user asked for it
2434 compress_savefile(dump_info
->CurrentFileName
);
2437 * Check to see if we've exceeded the Wflag (when
2440 if (Cflag
== 0 && Wflag
> 0 && Gflag_count
>= Wflag
) {
2441 (void)fprintf(stderr
, "Maximum file limit reached: %d\n",
2447 if (dump_info
->CurrentFileName
!= NULL
)
2448 free(dump_info
->CurrentFileName
);
2449 /* Allocate space for max filename + \0. */
2450 dump_info
->CurrentFileName
= (char *)malloc(PATH_MAX
+ 1);
2451 if (dump_info
->CurrentFileName
== NULL
)
2452 error("dump_packet_and_trunc: malloc");
2454 * Gflag was set otherwise we wouldn't be here. Reset the count
2455 * so multiple files would end with 1,2,3 in the filename.
2456 * The counting is handled with the -C flow after this.
2461 * This is always the first file in the Cflag
2463 * We also don't need numbering if Cflag is not set.
2466 MakeFilename(dump_info
->CurrentFileName
, dump_info
->WFileName
, 0,
2469 MakeFilename(dump_info
->CurrentFileName
, dump_info
->WFileName
, 0, 0);
2471 #ifdef HAVE_LIBCAP_NG
2472 capng_update(CAPNG_ADD
, CAPNG_EFFECTIVE
, CAP_DAC_OVERRIDE
);
2473 capng_apply(CAPNG_SELECT_BOTH
);
2474 #endif /* HAVE_LIBCAP_NG */
2475 #ifdef HAVE_CAPSICUM
2476 fd
= openat(dump_info
->dirfd
,
2477 dump_info
->CurrentFileName
,
2478 O_CREAT
| O_WRONLY
| O_TRUNC
, 0644);
2480 error("unable to open file %s",
2481 dump_info
->CurrentFileName
);
2483 fp
= fdopen(fd
, "w");
2485 error("unable to fdopen file %s",
2486 dump_info
->CurrentFileName
);
2488 dump_info
->p
= pcap_dump_fopen(dump_info
->pd
, fp
);
2489 #else /* !HAVE_CAPSICUM */
2490 dump_info
->p
= pcap_dump_open(dump_info
->pd
, dump_info
->CurrentFileName
);
2492 #ifdef HAVE_LIBCAP_NG
2493 capng_update(CAPNG_DROP
, CAPNG_EFFECTIVE
, CAP_DAC_OVERRIDE
);
2494 capng_apply(CAPNG_SELECT_BOTH
);
2495 #endif /* HAVE_LIBCAP_NG */
2496 if (dump_info
->p
== NULL
)
2497 error("%s", pcap_geterr(pd
));
2498 #ifdef HAVE_CAPSICUM
2499 set_dumper_capsicum_rights(dump_info
->p
);
2505 * XXX - this won't prevent capture files from getting
2506 * larger than Cflag - the last packet written to the
2507 * file could put it over Cflag.
2510 #ifdef HAVE_PCAP_FTELL64
2511 int64_t size
= pcap_dump_ftell64(dump_info
->p
);
2514 * XXX - this only handles a Cflag value > 2^31-1 on
2515 * LP64 platforms; to handle ILP32 (32-bit UN*X and
2516 * Windows) or LLP64 (64-bit Windows) would require
2517 * a version of libpcap with pcap_dump_ftell64().
2519 long size
= pcap_dump_ftell(dump_info
->p
);
2523 error("ftell fails on output file");
2525 #ifdef HAVE_CAPSICUM
2531 * Close the current file and open a new one.
2533 pcap_dump_close(dump_info
->p
);
2536 * Compress the file we just closed, if the user
2540 compress_savefile(dump_info
->CurrentFileName
);
2544 if (Cflag_count
>= Wflag
)
2547 if (dump_info
->CurrentFileName
!= NULL
)
2548 free(dump_info
->CurrentFileName
);
2549 dump_info
->CurrentFileName
= (char *)malloc(PATH_MAX
+ 1);
2550 if (dump_info
->CurrentFileName
== NULL
)
2551 error("dump_packet_and_trunc: malloc");
2552 MakeFilename(dump_info
->CurrentFileName
, dump_info
->WFileName
, Cflag_count
, WflagChars
);
2553 #ifdef HAVE_LIBCAP_NG
2554 capng_update(CAPNG_ADD
, CAPNG_EFFECTIVE
, CAP_DAC_OVERRIDE
);
2555 capng_apply(CAPNG_SELECT_BOTH
);
2556 #endif /* HAVE_LIBCAP_NG */
2557 #ifdef HAVE_CAPSICUM
2558 fd
= openat(dump_info
->dirfd
, dump_info
->CurrentFileName
,
2559 O_CREAT
| O_WRONLY
| O_TRUNC
, 0644);
2561 error("unable to open file %s",
2562 dump_info
->CurrentFileName
);
2564 fp
= fdopen(fd
, "w");
2566 error("unable to fdopen file %s",
2567 dump_info
->CurrentFileName
);
2569 dump_info
->p
= pcap_dump_fopen(dump_info
->pd
, fp
);
2570 #else /* !HAVE_CAPSICUM */
2571 dump_info
->p
= pcap_dump_open(dump_info
->pd
, dump_info
->CurrentFileName
);
2573 #ifdef HAVE_LIBCAP_NG
2574 capng_update(CAPNG_DROP
, CAPNG_EFFECTIVE
, CAP_DAC_OVERRIDE
);
2575 capng_apply(CAPNG_SELECT_BOTH
);
2576 #endif /* HAVE_LIBCAP_NG */
2577 if (dump_info
->p
== NULL
)
2578 error("%s", pcap_geterr(pd
));
2579 #ifdef HAVE_CAPSICUM
2580 set_dumper_capsicum_rights(dump_info
->p
);
2585 pcap_dump((u_char
*)dump_info
->p
, h
, sp
);
2586 #ifdef HAVE_PCAP_DUMP_FLUSH
2588 pcap_dump_flush(dump_info
->p
);
2597 dump_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
2603 pcap_dump(user
, h
, sp
);
2604 #ifdef HAVE_PCAP_DUMP_FLUSH
2606 pcap_dump_flush((pcap_dumper_t
*)user
);
2615 print_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
2621 pretty_print_packet((netdissect_options
*)user
, h
, sp
, packets_captured
);
2630 * XXX - there should really be libpcap calls to get the version
2631 * number as a string (the string would be generated from #defines
2632 * at run time, so that it's not generated from string constants
2633 * in the library, as, on many UNIX systems, those constants would
2634 * be statically linked into the application executable image, and
2635 * would thus reflect the version of libpcap on the system on
2636 * which the application was *linked*, not the system on which it's
2639 * That routine should be documented, unlike the "version[]"
2640 * string, so that UNIX vendors providing their own libpcaps
2641 * don't omit it (as a couple of vendors have...).
2643 * Packet.dll should perhaps also export a routine to return the
2644 * version number of the Packet.dll code, to supply the
2645 * "Wpcap_version" information on Windows.
2647 char WDversion
[]="current-git.tcpdump.org";
2648 #if !defined(HAVE_GENERATED_VERSION)
2649 char version
[]="current-git.tcpdump.org";
2651 char pcap_version
[]="current-git.tcpdump.org";
2652 char Wpcap_version
[]="3.1";
2655 #ifdef SIGNAL_REQ_INFO
2656 RETSIGTYPE
requestinfo(int signo _U_
)
2666 * Called once each second in verbose mode while dumping to file
2668 #ifdef USE_WIN32_MM_TIMER
2669 void CALLBACK
verbose_stats_dump (UINT timer_id _U_
, UINT msg _U_
, DWORD_PTR arg _U_
,
2670 DWORD_PTR dw1 _U_
, DWORD_PTR dw2 _U_
)
2673 fprintf(stderr
, "Got %u\r", packets_captured
);
2675 #elif defined(HAVE_ALARM)
2676 static void verbose_stats_dump(int sig _U_
)
2679 fprintf(stderr
, "Got %u\r", packets_captured
);
2684 USES_APPLE_DEPRECATED_API
2688 extern char version
[];
2689 #ifndef HAVE_PCAP_LIB_VERSION
2690 #if defined(_WIN32) || defined(HAVE_PCAP_VERSION)
2691 extern char pcap_version
[];
2692 #else /* defined(_WIN32) || defined(HAVE_PCAP_VERSION) */
2693 static char pcap_version
[] = "unknown";
2694 #endif /* defined(_WIN32) || defined(HAVE_PCAP_VERSION) */
2695 #endif /* HAVE_PCAP_LIB_VERSION */
2696 const char *smi_version_string
;
2698 #ifdef HAVE_PCAP_LIB_VERSION
2700 (void)fprintf(stderr
, "%s version %s, based on tcpdump version %s\n", program_name
, WDversion
, version
);
2702 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
2704 (void)fprintf(stderr
, "%s\n",pcap_lib_version());
2705 #else /* HAVE_PCAP_LIB_VERSION */
2707 (void)fprintf(stderr
, "%s version %s, based on tcpdump version %s\n", program_name
, WDversion
, version
);
2708 (void)fprintf(stderr
, "WinPcap version %s, based on libpcap version %s\n",Wpcap_version
, pcap_version
);
2710 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
2711 (void)fprintf(stderr
, "libpcap version %s\n", pcap_version
);
2713 #endif /* HAVE_PCAP_LIB_VERSION */
2715 #if defined(HAVE_LIBCRYPTO) && defined(SSLEAY_VERSION)
2716 (void)fprintf (stderr
, "%s\n", SSLeay_version(SSLEAY_VERSION
));
2719 smi_version_string
= nd_smi_version_string();
2720 if (smi_version_string
!= NULL
)
2721 (void)fprintf (stderr
, "SMI-library: %s\n", smi_version_string
);
2723 #if defined(__SANITIZE_ADDRESS__)
2724 (void)fprintf (stderr
, "Compiled with AddressSanitizer/GCC.\n");
2725 #elif defined(__has_feature)
2726 # if __has_feature(address_sanitizer)
2727 (void)fprintf (stderr
, "Compiled with AddressSanitizer/CLang.\n");
2729 #endif /* __SANITIZE_ADDRESS__ or __has_feature */
2737 (void)fprintf(stderr
,
2738 "Usage: %s [-aAbd" D_FLAG
"efhH" I_FLAG J_FLAG
"KlLnNOpqStu" U_FLAG
"vxX#]" B_FLAG_USAGE
" [ -c count ]\n", program_name
);
2739 (void)fprintf(stderr
,
2740 "\t\t[ -C file_size ] [ -E algo:secret ] [ -F file ] [ -G seconds ]\n");
2741 (void)fprintf(stderr
,
2742 "\t\t[ -i interface ]" j_FLAG_USAGE
" [ -M secret ] [ --number ]\n");
2743 #ifdef HAVE_PCAP_SETDIRECTION
2744 (void)fprintf(stderr
,
2745 "\t\t[ -Q in|out|inout ]\n");
2747 (void)fprintf(stderr
,
2748 "\t\t[ -r file ] [ -s snaplen ] ");
2749 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
2750 (void)fprintf(stderr
, "[ --time-stamp-precision precision ]\n");
2751 (void)fprintf(stderr
,
2754 #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
2755 (void)fprintf(stderr
, "[ --immediate-mode ] ");
2757 (void)fprintf(stderr
, "[ -T type ] [ --version ] [ -V file ]\n");
2758 (void)fprintf(stderr
,
2759 "\t\t[ -w file ] [ -W filecount ] [ -y datalinktype ] [ -z postrotate-command ]\n");
2760 (void)fprintf(stderr
,
2761 "\t\t[ -Z user ] [ expression ]\n");
2765 * c-style: whitesmith