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 - monitor tcp/ip traffic on an ethernet.
37 * First written in 1987 by Van Jacobson, Lawrence Berkeley Laboratory.
38 * Mercilessly hacked and occasionally improved since then via the
39 * combined efforts of Van, Steve McCanne and Craig Leres of LBL.
47 * Mac OS X may ship pcap.h from libpcap 0.6 with a libpcap based on
48 * 0.8. That means it has pcap_findalldevs() but the header doesn't
49 * define pcap_if_t, meaning that we can't actually *use* pcap_findalldevs().
51 #ifdef HAVE_PCAP_FINDALLDEVS
52 #ifndef HAVE_PCAP_IF_T
53 #undef HAVE_PCAP_FINDALLDEVS
57 #include <tcpdump-stdinc.h>
61 extern int strcasecmp (const char *__s1
, const char *__s2
);
72 #include <openssl/crypto.h>
75 #ifdef HAVE_GETOPT_LONG
78 #include "getopt_long.h"
80 /* Capsicum-specific code requires macros from <net/bpf.h>, which will fail
81 * to compile if <pcap.h> has already been included; including the headers
82 * in the opposite order works fine.
85 #include <sys/capability.h>
86 #include <sys/ioccom.h>
90 #endif /* HAVE_CAPSICUM */
99 #include <sys/resource.h>
104 /* capabilities convinience library */
107 #endif /* HAVE_CAP_NG_H */
109 #include "netdissect.h"
110 #include "interface.h"
111 #include "addrtoname.h"
113 #include "setsignal.h"
114 #include "gmt2local.h"
115 #include "pcap-missing.h"
118 #define PATH_MAX 1024
122 #define SIGNAL_REQ_INFO SIGINFO
124 #define SIGNAL_REQ_INFO SIGUSR1
127 netdissect_options Gndo
;
128 netdissect_options
*gndo
= &Gndo
;
130 static int Dflag
; /* list available devices and exit */
131 static int dflag
; /* print filter code */
132 static int Lflag
; /* list available data link types and exit */
133 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
134 static int Jflag
; /* list available time stamp types */
136 #ifdef HAVE_PCAP_SETDIRECTION
137 int Qflag
= -1; /* restrict captured packet by send/receive direction */
139 static char *zflag
= NULL
; /* compress each savefile using a specified command (like gzip or bzip2) */
141 static int infodelay
;
142 static int infoprint
;
146 int32_t thiszone
; /* seconds offset from gmt to local time */
149 static RETSIGTYPE
cleanup(int);
150 static RETSIGTYPE
child_cleanup(int);
151 static void print_version(void);
152 static void print_usage(void);
153 static void show_dlts_and_exit(const char *device
, pcap_t
*pd
) __attribute__((noreturn
));
155 static void print_packet(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
156 static void ndo_default_print(netdissect_options
*, const u_char
*, u_int
);
157 static void dump_packet_and_trunc(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
158 static void dump_packet(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
159 static void droproot(const char *, const char *);
160 static void ndo_error(netdissect_options
*ndo
, const char *fmt
, ...)
161 __attribute__((noreturn
))
162 #ifdef __ATTRIBUTE___FORMAT_OK
163 __attribute__((format (printf
, 2, 3)))
164 #endif /* __ATTRIBUTE___FORMAT_OK */
166 static void ndo_warning(netdissect_options
*ndo
, const char *fmt
, ...)
167 #ifdef __ATTRIBUTE___FORMAT_OK
168 __attribute__((format (printf
, 2, 3)))
169 #endif /* __ATTRIBUTE___FORMAT_OK */
172 #ifdef SIGNAL_REQ_INFO
173 RETSIGTYPE
requestinfo(int);
176 #if defined(USE_WIN32_MM_TIMER)
177 #include <MMsystem.h>
178 static UINT timer_id
;
179 static void CALLBACK
verbose_stats_dump(UINT
, UINT
, DWORD_PTR
, DWORD_PTR
, DWORD_PTR
);
180 #elif defined(HAVE_ALARM)
181 static void verbose_stats_dump(int sig
);
184 static void info(int);
185 static u_int packets_captured
;
199 static const struct printer printers
[] = {
203 static const struct ndo_printer ndo_printers
[] = {
204 { ether_if_print
, DLT_EN10MB
},
206 { ipnet_if_print
, DLT_IPNET
},
208 #ifdef DLT_IEEE802_15_4
209 { ieee802_15_4_if_print
, DLT_IEEE802_15_4
},
211 #ifdef DLT_IEEE802_15_4_NOFCS
212 { ieee802_15_4_if_print
, DLT_IEEE802_15_4_NOFCS
},
215 { ppi_if_print
, DLT_PPI
},
217 #ifdef DLT_NETANALYZER
218 { netanalyzer_if_print
, DLT_NETANALYZER
},
220 #ifdef DLT_NETANALYZER_TRANSPARENT
221 { netanalyzer_transparent_if_print
, DLT_NETANALYZER_TRANSPARENT
},
223 #if defined(DLT_NFLOG) && defined(HAVE_PCAP_NFLOG_H)
224 { nflog_if_print
, DLT_NFLOG
},
227 { cip_if_print
, DLT_CIP
},
230 { cip_if_print
, DLT_ATM_CLIP
},
232 #ifdef DLT_IP_OVER_FC
233 { ipfc_if_print
, DLT_IP_OVER_FC
},
235 { null_if_print
, DLT_NULL
},
237 { null_if_print
, DLT_LOOP
},
239 #ifdef DLT_APPLE_IP_OVER_IEEE1394
240 { ap1394_if_print
, DLT_APPLE_IP_OVER_IEEE1394
},
242 #if defined(DLT_BLUETOOTH_HCI_H4_WITH_PHDR) && defined(HAVE_PCAP_BLUETOOTH_H)
243 { bt_if_print
, DLT_BLUETOOTH_HCI_H4_WITH_PHDR
},
246 { lane_if_print
, DLT_LANE8023
},
248 { arcnet_if_print
, DLT_ARCNET
},
249 #ifdef DLT_ARCNET_LINUX
250 { arcnet_linux_if_print
, DLT_ARCNET_LINUX
},
252 { raw_if_print
, DLT_RAW
},
254 { raw_if_print
, DLT_IPV4
},
257 { raw_if_print
, DLT_IPV6
},
259 #ifdef HAVE_PCAP_USB_H
261 { usb_linux_48_byte_print
, DLT_USB_LINUX
},
262 #endif /* DLT_USB_LINUX */
263 #ifdef DLT_USB_LINUX_MMAPPED
264 { usb_linux_64_byte_print
, DLT_USB_LINUX_MMAPPED
},
265 #endif /* DLT_USB_LINUX_MMAPPED */
266 #endif /* HAVE_PCAP_USB_H */
267 #ifdef DLT_SYMANTEC_FIREWALL
268 { symantec_if_print
, DLT_SYMANTEC_FIREWALL
},
271 { chdlc_if_print
, DLT_C_HDLC
},
274 { chdlc_if_print
, DLT_HDLC
},
277 { pppoe_if_print
, DLT_PPP_ETHER
},
279 #if defined(DLT_PFLOG) && defined(HAVE_NET_PFVAR_H)
280 { pflog_if_print
, DLT_PFLOG
},
282 { token_if_print
, DLT_IEEE802
},
283 { fddi_if_print
, DLT_FDDI
},
285 { sll_if_print
, DLT_LINUX_SLL
},
288 { fr_if_print
, DLT_FR
},
291 { fr_if_print
, DLT_FRELAY
},
294 { mfr_if_print
, DLT_MFR
},
296 { atm_if_print
, DLT_ATM_RFC1483
},
298 { sunatm_if_print
, DLT_SUNATM
},
301 { enc_if_print
, DLT_ENC
},
303 { sl_if_print
, DLT_SLIP
},
304 #ifdef DLT_SLIP_BSDOS
305 { sl_bsdos_if_print
, DLT_SLIP_BSDOS
},
308 { ltalk_if_print
, DLT_LTALK
},
310 #ifdef DLT_JUNIPER_ATM1
311 { juniper_atm1_print
, DLT_JUNIPER_ATM1
},
313 #ifdef DLT_JUNIPER_ATM2
314 { juniper_atm2_print
, DLT_JUNIPER_ATM2
},
316 #ifdef DLT_JUNIPER_MFR
317 { juniper_mfr_print
, DLT_JUNIPER_MFR
},
319 #ifdef DLT_JUNIPER_MLFR
320 { juniper_mlfr_print
, DLT_JUNIPER_MLFR
},
322 #ifdef DLT_JUNIPER_MLPPP
323 { juniper_mlppp_print
, DLT_JUNIPER_MLPPP
},
325 #ifdef DLT_JUNIPER_PPPOE
326 { juniper_pppoe_print
, DLT_JUNIPER_PPPOE
},
328 #ifdef DLT_JUNIPER_PPPOE_ATM
329 { juniper_pppoe_atm_print
, DLT_JUNIPER_PPPOE_ATM
},
331 #ifdef DLT_JUNIPER_GGSN
332 { juniper_ggsn_print
, DLT_JUNIPER_GGSN
},
334 #ifdef DLT_JUNIPER_ES
335 { juniper_es_print
, DLT_JUNIPER_ES
},
337 #ifdef DLT_JUNIPER_MONITOR
338 { juniper_monitor_print
, DLT_JUNIPER_MONITOR
},
340 #ifdef DLT_JUNIPER_SERVICES
341 { juniper_services_print
, DLT_JUNIPER_SERVICES
},
343 #ifdef DLT_JUNIPER_ETHER
344 { juniper_ether_print
, DLT_JUNIPER_ETHER
},
346 #ifdef DLT_JUNIPER_PPP
347 { juniper_ppp_print
, DLT_JUNIPER_PPP
},
349 #ifdef DLT_JUNIPER_FRELAY
350 { juniper_frelay_print
, DLT_JUNIPER_FRELAY
},
352 #ifdef DLT_JUNIPER_CHDLC
353 { juniper_chdlc_print
, DLT_JUNIPER_CHDLC
},
356 { pktap_if_print
, DLT_PKTAP
},
358 #ifdef DLT_IEEE802_11_RADIO
359 { ieee802_11_radio_if_print
, DLT_IEEE802_11_RADIO
},
361 #ifdef DLT_IEEE802_11
362 { ieee802_11_if_print
, DLT_IEEE802_11
},
364 #ifdef DLT_IEEE802_11_RADIO_AVS
365 { ieee802_11_radio_avs_if_print
, DLT_IEEE802_11_RADIO_AVS
},
367 #ifdef DLT_PRISM_HEADER
368 { prism_if_print
, DLT_PRISM_HEADER
},
370 { ppp_if_print
, DLT_PPP
},
371 #ifdef DLT_PPP_WITHDIRECTION
372 { ppp_if_print
, DLT_PPP_WITHDIRECTION
},
375 { ppp_bsdos_if_print
, DLT_PPP_BSDOS
},
377 #ifdef DLT_PPP_SERIAL
378 { ppp_hdlc_if_print
, DLT_PPP_SERIAL
},
383 static const struct tok status_flags
[] = {
385 { PCAP_IF_UP
, "Up" },
387 #ifdef PCAP_IF_RUNNING
388 { PCAP_IF_RUNNING
, "Running" },
390 { PCAP_IF_LOOPBACK
, "Loopback" },
395 lookup_printer(int type
)
397 const struct printer
*p
;
399 for (p
= printers
; p
->f
; ++p
)
408 lookup_ndo_printer(int type
)
410 const struct ndo_printer
*p
;
412 for (p
= ndo_printers
; p
->f
; ++p
)
416 #if defined(DLT_USER2) && defined(DLT_PKTAP)
418 * Apple incorrectly chose to use DLT_USER2 for their PKTAP
421 * We map DLT_PKTAP, whether it's DLT_USER2 as it is on Darwin-
422 * based OSes or the same value as LINKTYPE_PKTAP as it is on
423 * other OSes, to LINKTYPE_PKTAP, so files written with
424 * this version of libpcap for a DLT_PKTAP capture have a link-
425 * layer header type of LINKTYPE_PKTAP.
427 * However, files written on OS X Mavericks for a DLT_PKTAP
428 * capture have a link-layer header type of LINKTYPE_USER2.
429 * If we don't have a printer for DLT_USER2, and type is
430 * DLT_USER2, we look up the printer for DLT_PKTAP and use
433 if (type
== DLT_USER2
) {
434 for (p
= ndo_printers
; p
->f
; ++p
)
435 if (DLT_PKTAP
== p
->type
)
446 static int supports_monitor_mode
;
453 netdissect_options
*ndo
;
456 if_ndo_printer ndo_printer
;
463 char *CurrentFileName
;
471 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
473 show_tstamp_types_and_exit(const char *device
, pcap_t
*pd
)
476 int *tstamp_types
= 0;
477 const char *tstamp_type_name
;
480 n_tstamp_types
= pcap_list_tstamp_types(pd
, &tstamp_types
);
481 if (n_tstamp_types
< 0)
482 error("%s", pcap_geterr(pd
));
484 if (n_tstamp_types
== 0) {
485 fprintf(stderr
, "Time stamp type cannot be set for %s\n",
489 fprintf(stderr
, "Time stamp types for %s (use option -j to set):\n",
491 for (i
= 0; i
< n_tstamp_types
; i
++) {
492 tstamp_type_name
= pcap_tstamp_type_val_to_name(tstamp_types
[i
]);
493 if (tstamp_type_name
!= NULL
) {
494 (void) fprintf(stderr
, " %s (%s)\n", tstamp_type_name
,
495 pcap_tstamp_type_val_to_description(tstamp_types
[i
]));
497 (void) fprintf(stderr
, " %d\n", tstamp_types
[i
]);
500 pcap_free_tstamp_types(tstamp_types
);
506 show_dlts_and_exit(const char *device
, pcap_t
*pd
)
510 const char *dlt_name
;
512 n_dlts
= pcap_list_datalinks(pd
, &dlts
);
514 error("%s", pcap_geterr(pd
));
515 else if (n_dlts
== 0 || !dlts
)
516 error("No data link types.");
519 * If the interface is known to support monitor mode, indicate
520 * whether these are the data link types available when not in
521 * monitor mode, if -I wasn't specified, or when in monitor mode,
522 * when -I was specified (the link-layer types available in
523 * monitor mode might be different from the ones available when
524 * not in monitor mode).
526 if (supports_monitor_mode
)
527 (void) fprintf(stderr
, "Data link types for %s %s (use option -y to set):\n",
529 Iflag
? "when in monitor mode" : "when not in monitor mode");
531 (void) fprintf(stderr
, "Data link types for %s (use option -y to set):\n",
534 while (--n_dlts
>= 0) {
535 dlt_name
= pcap_datalink_val_to_name(dlts
[n_dlts
]);
536 if (dlt_name
!= NULL
) {
537 (void) fprintf(stderr
, " %s (%s)", dlt_name
,
538 pcap_datalink_val_to_description(dlts
[n_dlts
]));
541 * OK, does tcpdump handle that type?
543 if (lookup_printer(dlts
[n_dlts
]) == NULL
544 && lookup_ndo_printer(dlts
[n_dlts
]) == NULL
)
545 (void) fprintf(stderr
, " (printing not supported)");
546 fprintf(stderr
, "\n");
548 (void) fprintf(stderr
, " DLT %d (printing not supported)\n",
552 #ifdef HAVE_PCAP_FREE_DATALINKS
553 pcap_free_datalinks(dlts
);
558 #ifdef HAVE_PCAP_FINDALLDEVS
560 show_devices_and_exit (void)
562 pcap_if_t
*devpointer
;
563 char ebuf
[PCAP_ERRBUF_SIZE
];
566 if (pcap_findalldevs(&devpointer
, ebuf
) < 0)
569 for (i
= 0; devpointer
!= NULL
; i
++) {
570 printf("%d.%s", i
+1, devpointer
->name
);
571 if (devpointer
->description
!= NULL
)
572 printf(" (%s)", devpointer
->description
);
573 if (devpointer
->flags
!= 0)
574 printf(" [%s]", bittok2str(status_flags
, "none", devpointer
->flags
));
576 devpointer
= devpointer
->next
;
581 #endif /* HAVE_PCAP_FINDALLDEVS */
586 * Note that there we use all letters for short options except for g, k,
587 * o, and P, and those are used by other versions of tcpdump, and we should
588 * only use them for the same purposes that the other versions of tcpdump
591 * OS X tcpdump uses -g to force non--v output for IP to be on one
592 * line, making it more "g"repable;
594 * OS X tcpdump uses -k tospecify that packet comments in pcap-ng files
597 * OpenBSD tcpdump uses -o to indicate that OS fingerprinting should be done
598 * for hosts sending TCP SYN packets;
600 * OS X tcpdump uses -P to indicate that -w should write pcap-ng rather
605 * Set up flags that might or might not be supported depending on the
606 * version of libpcap we're using.
608 #if defined(HAVE_PCAP_CREATE) || defined(WIN32)
610 #define B_FLAG_USAGE " [ -B size ]"
611 #else /* defined(HAVE_PCAP_CREATE) || defined(WIN32) */
614 #endif /* defined(HAVE_PCAP_CREATE) || defined(WIN32) */
616 #ifdef HAVE_PCAP_CREATE
618 #else /* HAVE_PCAP_CREATE */
620 #endif /* HAVE_PCAP_CREATE */
622 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
624 #define j_FLAG_USAGE " [ -j tstamptype ]"
626 #else /* PCAP_ERROR_TSTAMP_TYPE_NOTSUP */
630 #endif /* PCAP_ERROR_TSTAMP_TYPE_NOTSUP */
632 #ifdef HAVE_PCAP_FINDALLDEVS
638 #ifdef HAVE_PCAP_DUMP_FLUSH
644 #ifdef HAVE_PCAP_SETDIRECTION
653 * We do not currently have long options corresponding to all short
654 * options; we should probably pick appropriate option names for them.
656 * However, the short options where the number of times the option is
657 * specified matters, such as -v and -d and -t, should probably not
658 * just map to a long option, as saying
660 * tcpdump --verbose --verbose
662 * doesn't make sense; it should be --verbosity={N} or something such
665 * For long options with no corresponding short options, we define values
666 * outside the range of ASCII graphic characters, make that the last
667 * component of the entry for the long option, and have a case for that
668 * option in the switch statement.
670 #define OPTION_VERSION 128
671 #define OPTION_TSTAMP_PRECISION 129
673 static const struct option longopts
[] = {
674 #if defined(HAVE_PCAP_CREATE) || defined(WIN32)
675 { "buffer-size", required_argument
, NULL
, 'B' },
677 { "list-interfaces", no_argument
, NULL
, 'D' },
678 { "help", no_argument
, NULL
, 'h' },
679 { "interface", required_argument
, NULL
, 'i' },
680 #ifdef HAVE_PCAP_CREATE
681 { "monitor-mode", no_argument
, NULL
, 'I' },
683 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
684 { "time-stamp-type", required_argument
, NULL
, 'j' },
685 { "list-time-stamp-types", no_argument
, NULL
, 'J' },
687 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
688 { "time-stamp-precision", required_argument
, NULL
, OPTION_TSTAMP_PRECISION
},
690 { "dont-verify-checksums", no_argument
, NULL
, 'K' },
691 { "list-data-link-types", no_argument
, NULL
, 'L' },
692 { "no-optimize", no_argument
, NULL
, 'O' },
693 { "no-promiscuous-mode", no_argument
, NULL
, 'p' },
694 #ifdef HAVE_PCAP_SETDIRECTION
695 { "direction", required_argument
, NULL
, 'Q' },
697 { "snapshot-length", required_argument
, NULL
, 's' },
698 { "absolute-tcp-sequence-numbers", no_argument
, NULL
, 'S' },
699 #ifdef HAVE_PCAP_DUMP_FLUSH
700 { "packet-buffered", no_argument
, NULL
, 'U' },
702 { "linktype", required_argument
, NULL
, 'y' },
703 #if defined(HAVE_PCAP_DEBUG) || defined(HAVE_YYDEBUG)
704 { "debug-filter-parser", no_argument
, NULL
, 'Y' },
706 { "relinquish-privileges", required_argument
, NULL
, 'Z' },
707 { "number", no_argument
, NULL
, '#' },
708 { "version", no_argument
, NULL
, OPTION_VERSION
},
713 /* Drop root privileges and chroot if necessary */
715 droproot(const char *username
, const char *chroot_dir
)
717 struct passwd
*pw
= NULL
;
719 if (chroot_dir
&& !username
) {
720 fprintf(stderr
, "tcpdump: Chroot without dropping root is insecure\n");
724 pw
= getpwnam(username
);
727 if (chroot(chroot_dir
) != 0 || chdir ("/") != 0) {
728 fprintf(stderr
, "tcpdump: Couldn't chroot/chdir to '%.64s': %s\n",
729 chroot_dir
, pcap_strerror(errno
));
734 int ret
= capng_change_id(pw
->pw_uid
, pw
->pw_gid
, CAPNG_NO_FLAG
);
736 fprintf(stderr
, "error : ret %d\n", ret
);
739 fprintf(stderr
, "dropped privs to %s\n", username
);
741 /* We don't need CAP_SETUID and CAP_SETGID */
744 CAPNG_EFFECTIVE
| CAPNG_PERMITTED
,
748 capng_apply(CAPNG_SELECT_BOTH
);
751 if (initgroups(pw
->pw_name
, pw
->pw_gid
) != 0 ||
752 setgid(pw
->pw_gid
) != 0 || setuid(pw
->pw_uid
) != 0) {
753 fprintf(stderr
, "tcpdump: Couldn't change to '%.32s' uid=%lu gid=%lu: %s\n",
755 (unsigned long)pw
->pw_uid
,
756 (unsigned long)pw
->pw_gid
,
757 pcap_strerror(errno
));
761 fprintf(stderr
, "dropped privs to %s\n", username
);
763 #endif /* HAVE_CAP_NG_H */
766 fprintf(stderr
, "tcpdump: Couldn't find user '%.32s'\n",
789 MakeFilename(char *buffer
, char *orig_name
, int cnt
, int max_chars
)
791 char *filename
= malloc(PATH_MAX
+ 1);
792 if (filename
== NULL
)
793 error("Makefilename: malloc");
795 /* Process with strftime if Gflag is set. */
799 /* Convert Gflag_time to a usable format */
800 if ((local_tm
= localtime(&Gflag_time
)) == NULL
) {
801 error("MakeTimedFilename: localtime");
804 /* There's no good way to detect an error in strftime since a return
805 * value of 0 isn't necessarily failure.
807 strftime(filename
, PATH_MAX
, orig_name
, local_tm
);
809 strncpy(filename
, orig_name
, PATH_MAX
);
812 if (cnt
== 0 && max_chars
== 0)
813 strncpy(buffer
, filename
, PATH_MAX
+ 1);
815 if (snprintf(buffer
, PATH_MAX
+ 1, "%s%0*d", filename
, max_chars
, cnt
) > PATH_MAX
)
816 /* Report an error if the filename is too large */
817 error("too many output files or filename is too long (> %d)", PATH_MAX
);
821 static int tcpdump_printf(netdissect_options
*ndo _U_
,
822 const char *fmt
, ...)
829 ret
=vfprintf(stdout
, fmt
, args
);
835 static struct print_info
836 get_print_info(int type
)
838 struct print_info printinfo
;
840 printinfo
.ndo_type
= 1;
841 printinfo
.ndo
= gndo
;
842 printinfo
.p
.ndo_printer
= lookup_ndo_printer(type
);
843 if (printinfo
.p
.ndo_printer
== NULL
) {
844 printinfo
.p
.printer
= lookup_printer(type
);
845 printinfo
.ndo_type
= 0;
846 if (printinfo
.p
.printer
== NULL
) {
847 gndo
->ndo_dltname
= pcap_datalink_val_to_name(type
);
848 if (gndo
->ndo_dltname
!= NULL
)
849 error("packet printing is not supported for link type %s: use -w",
852 error("packet printing is not supported for link type %d: use -w", type
);
859 get_next_file(FILE *VFile
, char *ptr
)
863 ret
= fgets(ptr
, PATH_MAX
, VFile
);
867 if (ptr
[strlen(ptr
) - 1] == '\n')
868 ptr
[strlen(ptr
) - 1] = '\0';
873 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
875 tstamp_precision_from_string(const char *precision
)
877 if (strncmp(precision
, "nano", strlen("nano")) == 0)
878 return PCAP_TSTAMP_PRECISION_NANO
;
880 if (strncmp(precision
, "micro", strlen("micro")) == 0)
881 return PCAP_TSTAMP_PRECISION_MICRO
;
887 tstamp_precision_to_string(int precision
)
891 case PCAP_TSTAMP_PRECISION_MICRO
:
894 case PCAP_TSTAMP_PRECISION_NANO
:
904 main(int argc
, char **argv
)
906 register int cnt
, op
, i
;
907 bpf_u_int32 localnet
=0 , netmask
= 0;
908 register char *cp
, *infile
, *cmdbuf
, *device
, *RFileName
, *VFileName
, *WFileName
;
909 pcap_handler callback
;
913 const char *dlt_name
;
914 struct bpf_program fcode
;
916 RETSIGTYPE (*oldhandler
)(int);
918 struct print_info printinfo
;
919 struct dump_info dumpinfo
;
920 u_char
*pcap_userdata
;
921 char ebuf
[PCAP_ERRBUF_SIZE
];
922 char VFileLine
[PATH_MAX
+ 1];
923 char *username
= NULL
;
924 char *chroot_dir
= NULL
;
927 #ifdef HAVE_PCAP_FINDALLDEVS
928 pcap_if_t
*devpointer
;
936 #endif /* HAVE_CAPSICUM */
939 if(wsockinit() != 0) return 1;
942 jflag
=-1; /* not set */
946 gndo
->ndo_default_print
=ndo_default_print
;
947 gndo
->ndo_printf
=tcpdump_printf
;
948 gndo
->ndo_error
=ndo_error
;
949 gndo
->ndo_warning
=ndo_warning
;
950 gndo
->ndo_snaplen
= DEFAULT_SNAPLEN
;
960 if ((cp
= strrchr(argv
[0], '/')) != NULL
)
961 program_name
= cp
+ 1;
963 program_name
= argv
[0];
966 * On platforms where the CPU doesn't support unaligned loads,
967 * force unaligned accesses to abort with SIGBUS, rather than
968 * being fixed up (slowly) by the OS kernel; on those platforms,
969 * misaligned accesses are bugs, and we want tcpdump to crash so
970 * that the bugs are reported.
972 if (abort_on_misalignment(ebuf
, sizeof(ebuf
)) < 0)
980 (op
= getopt_long(argc
, argv
, "aAb" B_FLAG
"c:C:d" D_FLAG
"eE:fF:G:hHi:" I_FLAG j_FLAG J_FLAG
"KlLm:M:nNOpq" Q_FLAG
"r:Rs:StT:u" U_FLAG
"vV:w:W:xXy:Yz:Z:#", longopts
, NULL
)) != -1)
984 /* compatibility for old -a */
995 #if defined(HAVE_PCAP_CREATE) || defined(WIN32)
997 Bflag
= atoi(optarg
)*1024;
999 error("invalid packet buffer size %s", optarg
);
1001 #endif /* defined(HAVE_PCAP_CREATE) || defined(WIN32) */
1006 error("invalid packet count %s", optarg
);
1010 Cflag
= atoi(optarg
) * 1000000;
1012 error("invalid file size %s", optarg
);
1032 #ifndef HAVE_LIBCRYPTO
1033 warning("crypto code not compiled in");
1035 gndo
->ndo_espsecret
= optarg
;
1047 Gflag
= atoi(optarg
);
1049 error("invalid number of seconds %s", optarg
);
1051 /* We will create one file initially. */
1054 /* Grab the current time for rotation use. */
1055 if ((Gflag_time
= time(NULL
)) == (time_t)-1) {
1056 error("main: can't get current time: %s",
1057 pcap_strerror(errno
));
1071 if (optarg
[0] == '0' && optarg
[1] == 0)
1072 error("Invalid adapter index");
1074 #ifdef HAVE_PCAP_FINDALLDEVS
1076 * If the argument is a number, treat it as
1077 * an index into the list of adapters, as
1078 * printed by "tcpdump -D".
1080 * This should be OK on UNIX systems, as interfaces
1081 * shouldn't have names that begin with digits.
1082 * It can be useful on Windows, where more than
1083 * one interface can have the same name.
1085 devnum
= strtol(optarg
, &end
, 10);
1086 if (optarg
!= end
&& *end
== '\0') {
1088 error("Invalid adapter index");
1090 if (pcap_findalldevs(&devpointer
, ebuf
) < 0)
1094 * Look for the devnum-th entry
1095 * in the list of devices
1099 i
< devnum
-1 && devpointer
!= NULL
;
1100 i
++, devpointer
= devpointer
->next
)
1102 if (devpointer
== NULL
)
1103 error("Invalid adapter index");
1105 device
= devpointer
->name
;
1108 #endif /* HAVE_PCAP_FINDALLDEVS */
1112 #ifdef HAVE_PCAP_CREATE
1116 #endif /* HAVE_PCAP_CREATE */
1118 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
1120 jflag
= pcap_tstamp_type_name_to_val(optarg
);
1122 error("invalid time stamp type %s", optarg
);
1133 * _IOLBF is the same as _IOFBF in Microsoft's C
1134 * libraries; the only alternative they offer
1137 * XXX - this should really be checking for MSVC++,
1138 * not WIN32, if, for example, MinGW has its own
1139 * C library that is more UNIX-compatible.
1141 setvbuf(stdout
, NULL
, _IONBF
, 0);
1143 #ifdef HAVE_SETLINEBUF
1146 setvbuf(stdout
, NULL
, _IOLBF
, 0);
1157 if (smiLoadModule(optarg
) == 0) {
1158 error("could not load MIB module %s", optarg
);
1162 (void)fprintf(stderr
, "%s: ignoring option `-m %s' ",
1163 program_name
, optarg
);
1164 (void)fprintf(stderr
, "(no libsmi support)\n");
1169 /* TCP-MD5 shared secret */
1170 #ifndef HAVE_LIBCRYPTO
1171 warning("crypto code not compiled in");
1194 ++suppress_default_print
;
1197 #ifdef HAVE_PCAP_SETDIRECTION
1199 if (strcasecmp(optarg
, "in") == 0)
1201 else if (strcasecmp(optarg
, "out") == 0)
1203 else if (strcasecmp(optarg
, "inout") == 0)
1204 Qflag
= PCAP_D_INOUT
;
1206 error("unknown capture direction `%s'", optarg
);
1208 #endif /* HAVE_PCAP_SETDIRECTION */
1219 snaplen
= strtol(optarg
, &end
, 0);
1220 if (optarg
== end
|| *end
!= '\0'
1221 || snaplen
< 0 || snaplen
> MAXIMUM_SNAPLEN
)
1222 error("invalid snaplen %s", optarg
);
1223 else if (snaplen
== 0)
1224 snaplen
= MAXIMUM_SNAPLEN
;
1236 if (strcasecmp(optarg
, "vat") == 0)
1237 packettype
= PT_VAT
;
1238 else if (strcasecmp(optarg
, "wb") == 0)
1240 else if (strcasecmp(optarg
, "rpc") == 0)
1241 packettype
= PT_RPC
;
1242 else if (strcasecmp(optarg
, "rtp") == 0)
1243 packettype
= PT_RTP
;
1244 else if (strcasecmp(optarg
, "rtcp") == 0)
1245 packettype
= PT_RTCP
;
1246 else if (strcasecmp(optarg
, "snmp") == 0)
1247 packettype
= PT_SNMP
;
1248 else if (strcasecmp(optarg
, "cnfp") == 0)
1249 packettype
= PT_CNFP
;
1250 else if (strcasecmp(optarg
, "tftp") == 0)
1251 packettype
= PT_TFTP
;
1252 else if (strcasecmp(optarg
, "aodv") == 0)
1253 packettype
= PT_AODV
;
1254 else if (strcasecmp(optarg
, "carp") == 0)
1255 packettype
= PT_CARP
;
1256 else if (strcasecmp(optarg
, "radius") == 0)
1257 packettype
= PT_RADIUS
;
1258 else if (strcasecmp(optarg
, "zmtp1") == 0)
1259 packettype
= PT_ZMTP1
;
1260 else if (strcasecmp(optarg
, "vxlan") == 0)
1261 packettype
= PT_VXLAN
;
1262 else if (strcasecmp(optarg
, "pgm") == 0)
1263 packettype
= PT_PGM
;
1264 else if (strcasecmp(optarg
, "pgm_zmtp1") == 0)
1265 packettype
= PT_PGM_ZMTP1
;
1266 else if (strcasecmp(optarg
, "lmp") == 0)
1267 packettype
= PT_LMP
;
1269 error("unknown packet type `%s'", optarg
);
1276 #ifdef HAVE_PCAP_DUMP_FLUSH
1295 Wflag
= atoi(optarg
);
1297 error("invalid number of output files %s", optarg
);
1298 WflagChars
= getWflagChars(Wflag
);
1303 ++suppress_default_print
;
1308 ++suppress_default_print
;
1312 gndo
->ndo_dltname
= optarg
;
1314 pcap_datalink_name_to_val(gndo
->ndo_dltname
);
1315 if (gndo
->ndo_dlt
< 0)
1316 error("invalid data link type %s", gndo
->ndo_dltname
);
1319 #if defined(HAVE_PCAP_DEBUG) || defined(HAVE_YYDEBUG)
1322 /* Undocumented flag */
1323 #ifdef HAVE_PCAP_DEBUG
1324 extern int pcap_debug
;
1334 zflag
= strdup(optarg
);
1338 username
= strdup(optarg
);
1342 gndo
->ndo_packet_number
= 1;
1345 case OPTION_VERSION
:
1350 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
1351 case OPTION_TSTAMP_PRECISION
:
1352 gndo
->ndo_tstamp_precision
= tstamp_precision_from_string(optarg
);
1353 if (gndo
->ndo_tstamp_precision
< 0)
1354 error("unsupported time stamp precision");
1364 #ifdef HAVE_PCAP_FINDALLDEVS
1366 show_devices_and_exit();
1371 case 0: /* Default */
1372 case 4: /* Default + Date*/
1373 thiszone
= gmt2local(0);
1376 case 1: /* No time stamp */
1377 case 2: /* Unix timeval style */
1378 case 3: /* Microseconds since previous packet */
1379 case 5: /* Microseconds since first packet */
1382 default: /* Not supported */
1383 error("only -t, -tt, -ttt, -tttt and -ttttt are supported");
1387 if (fflag
!= 0 && (VFileName
!= NULL
|| RFileName
!= NULL
))
1388 error("-f can not be used with -V or -r");
1390 if (VFileName
!= NULL
&& RFileName
!= NULL
)
1391 error("-V and -r are mutually exclusive.");
1394 /* if run as root, prepare for chrooting */
1395 if (getuid() == 0 || geteuid() == 0) {
1396 /* future extensibility for cmd-line arguments */
1398 chroot_dir
= WITH_CHROOT
;
1403 /* if run as root, prepare for dropping root privileges */
1404 if (getuid() == 0 || geteuid() == 0) {
1405 /* Run with '-Z root' to restore old behaviour */
1407 username
= WITH_USER
;
1411 if (RFileName
!= NULL
|| VFileName
!= NULL
) {
1413 * If RFileName is non-null, it's the pathname of a
1414 * savefile to read. If VFileName is non-null, it's
1415 * the pathname of a file containing a list of pathnames
1416 * (one per line) of savefiles to read.
1418 * In either case, we're reading a savefile, not doing
1423 * We don't need network access, so relinquish any set-UID
1424 * or set-GID privileges we have (if any).
1426 * We do *not* want set-UID privileges when opening a
1427 * trace file, as that might let the user read other
1428 * people's trace files (especially if we're set-UID
1431 if (setgid(getgid()) != 0 || setuid(getuid()) != 0 )
1432 fprintf(stderr
, "Warning: setgid/setuid failed !\n");
1434 if (VFileName
!= NULL
) {
1435 if (VFileName
[0] == '-' && VFileName
[1] == '\0')
1438 VFile
= fopen(VFileName
, "r");
1441 error("Unable to open file: %s\n", strerror(errno
));
1443 ret
= get_next_file(VFile
, VFileLine
);
1445 error("Nothing in %s\n", VFileName
);
1446 RFileName
= VFileLine
;
1449 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
1450 pd
= pcap_open_offline_with_tstamp_precision(RFileName
,
1451 gndo
->ndo_tstamp_precision
, ebuf
);
1453 pd
= pcap_open_offline(RFileName
, ebuf
);
1458 #ifdef HAVE_CAPSICUM
1459 cap_rights_init(&rights
, CAP_READ
);
1460 if (cap_rights_limit(fileno(pcap_file(pd
)), &rights
) < 0 &&
1462 error("unable to limit pcap descriptor");
1465 dlt
= pcap_datalink(pd
);
1466 dlt_name
= pcap_datalink_val_to_name(dlt
);
1467 if (dlt_name
== NULL
) {
1468 fprintf(stderr
, "reading from file %s, link-type %u\n",
1472 "reading from file %s, link-type %s (%s)\n",
1473 RFileName
, dlt_name
,
1474 pcap_datalink_val_to_description(dlt
));
1478 * We're doing a live capture.
1480 if (device
== NULL
) {
1481 device
= pcap_lookupdev(ebuf
);
1487 * Print a message to the standard error on Windows.
1488 * XXX - why do it here, with a different message?
1490 if(strlen(device
) == 1) /* we assume that an ASCII string is always longer than 1 char */
1491 { /* a Unicode string has a \0 as second byte (so strlen() is 1) */
1492 fprintf(stderr
, "%s: listening on %ws\n", program_name
, device
);
1496 fprintf(stderr
, "%s: listening on %s\n", program_name
, device
);
1501 #ifdef HAVE_PCAP_CREATE
1502 pd
= pcap_create(device
, ebuf
);
1505 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
1507 show_tstamp_types_and_exit(device
, pd
);
1509 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
1510 status
= pcap_set_tstamp_precision(pd
, gndo
->ndo_tstamp_precision
);
1512 error("%s: Can't set %ssecond time stamp precision: %s",
1514 tstamp_precision_to_string(gndo
->ndo_tstamp_precision
),
1515 pcap_statustostr(status
));
1519 * Is this an interface that supports monitor mode?
1521 if (pcap_can_set_rfmon(pd
) == 1)
1522 supports_monitor_mode
= 1;
1524 supports_monitor_mode
= 0;
1525 status
= pcap_set_snaplen(pd
, snaplen
);
1527 error("%s: Can't set snapshot length: %s",
1528 device
, pcap_statustostr(status
));
1529 status
= pcap_set_promisc(pd
, !pflag
);
1531 error("%s: Can't set promiscuous mode: %s",
1532 device
, pcap_statustostr(status
));
1534 status
= pcap_set_rfmon(pd
, 1);
1536 error("%s: Can't set monitor mode: %s",
1537 device
, pcap_statustostr(status
));
1539 status
= pcap_set_timeout(pd
, 1000);
1541 error("%s: pcap_set_timeout failed: %s",
1542 device
, pcap_statustostr(status
));
1544 status
= pcap_set_buffer_size(pd
, Bflag
);
1546 error("%s: Can't set buffer size: %s",
1547 device
, pcap_statustostr(status
));
1549 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
1551 status
= pcap_set_tstamp_type(pd
, jflag
);
1553 error("%s: Can't set time stamp type: %s",
1554 device
, pcap_statustostr(status
));
1557 status
= pcap_activate(pd
);
1560 * pcap_activate() failed.
1562 cp
= pcap_geterr(pd
);
1563 if (status
== PCAP_ERROR
)
1565 else if ((status
== PCAP_ERROR_NO_SUCH_DEVICE
||
1566 status
== PCAP_ERROR_PERM_DENIED
) &&
1568 error("%s: %s\n(%s)", device
,
1569 pcap_statustostr(status
), cp
);
1571 error("%s: %s", device
,
1572 pcap_statustostr(status
));
1573 } else if (status
> 0) {
1575 * pcap_activate() succeeded, but it's warning us
1576 * of a problem it had.
1578 cp
= pcap_geterr(pd
);
1579 if (status
== PCAP_WARNING
)
1581 else if (status
== PCAP_WARNING_PROMISC_NOTSUP
&&
1583 warning("%s: %s\n(%s)", device
,
1584 pcap_statustostr(status
), cp
);
1586 warning("%s: %s", device
,
1587 pcap_statustostr(status
));
1589 #ifdef HAVE_PCAP_SETDIRECTION
1591 status
= pcap_setdirection(pd
, Qflag
);
1593 error("%s: pcap_setdirection() failed: %s",
1594 device
, pcap_geterr(pd
));
1596 #endif /* HAVE_PCAP_SETDIRECTION */
1599 pd
= pcap_open_live(device
, snaplen
, !pflag
, 1000, ebuf
);
1603 warning("%s", ebuf
);
1604 #endif /* HAVE_PCAP_CREATE */
1606 * Let user own process after socket has been opened.
1609 if (setgid(getgid()) != 0 || setuid(getuid()) != 0)
1610 fprintf(stderr
, "Warning: setgid/setuid failed !\n");
1612 #if !defined(HAVE_PCAP_CREATE) && defined(WIN32)
1614 if(pcap_setbuff(pd
, Bflag
)==-1){
1615 error("%s", pcap_geterr(pd
));
1617 #endif /* !defined(HAVE_PCAP_CREATE) && defined(WIN32) */
1619 show_dlts_and_exit(device
, pd
);
1620 if (gndo
->ndo_dlt
>= 0) {
1621 #ifdef HAVE_PCAP_SET_DATALINK
1622 if (pcap_set_datalink(pd
, gndo
->ndo_dlt
) < 0)
1623 error("%s", pcap_geterr(pd
));
1626 * We don't actually support changing the
1627 * data link type, so we only let them
1628 * set it to what it already is.
1630 if (gndo
->ndo_dlt
!= pcap_datalink(pd
)) {
1631 error("%s is not one of the DLTs supported by this device\n",
1635 (void)fprintf(stderr
, "%s: data link type %s\n",
1636 program_name
, gndo
->ndo_dltname
);
1637 (void)fflush(stderr
);
1639 i
= pcap_snapshot(pd
);
1641 warning("snaplen raised from %d to %d", snaplen
, i
);
1645 if (pcap_lookupnet(device
, &localnet
, &netmask
, ebuf
) < 0) {
1646 warning("foreign (-f) flag used but: %s", ebuf
);
1652 cmdbuf
= read_infile(infile
);
1654 cmdbuf
= copy_argv(&argv
[optind
]);
1656 if (pcap_compile(pd
, &fcode
, cmdbuf
, Oflag
, netmask
) < 0)
1657 error("%s", pcap_geterr(pd
));
1659 bpf_dump(&fcode
, dflag
);
1664 init_addrtoname(gndo
, localnet
, netmask
);
1668 (void)setsignal(SIGPIPE
, cleanup
);
1669 (void)setsignal(SIGTERM
, cleanup
);
1670 (void)setsignal(SIGINT
, cleanup
);
1672 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
1673 (void)setsignal(SIGCHLD
, child_cleanup
);
1675 /* Cooperate with nohup(1) */
1677 if ((oldhandler
= setsignal(SIGHUP
, cleanup
)) != SIG_DFL
)
1678 (void)setsignal(SIGHUP
, oldhandler
);
1683 * If a user name was specified with "-Z", attempt to switch to
1684 * that user's UID. This would probably be used with sudo,
1685 * to allow tcpdump to be run in a special restricted
1686 * account (if you just want to allow users to open capture
1687 * devices, and can't just give users that permission,
1688 * you'd make tcpdump set-UID or set-GID).
1690 * Tcpdump doesn't necessarily write only to one savefile;
1691 * the general only way to allow a -Z instance to write to
1692 * savefiles as the user under whose UID it's run, rather
1693 * than as the user specified with -Z, would thus be to switch
1694 * to the original user ID before opening a capture file and
1695 * then switch back to the -Z user ID after opening the savefile.
1696 * Switching to the -Z user ID only after opening the first
1697 * savefile doesn't handle the general case.
1700 if (getuid() == 0 || geteuid() == 0) {
1701 #ifdef HAVE_CAP_NG_H
1702 /* Drop all capabilities from effective set */
1703 capng_clear(CAPNG_EFFECTIVE
);
1704 /* We are running as root and we will be writing to savefile */
1705 if (WFileName
&& username
) {
1706 /* Add capabilities we will need*/
1709 CAPNG_PERMITTED
| CAPNG_EFFECTIVE
,
1714 capng_apply(CAPNG_SELECT_BOTH
);
1716 #endif /* HAVE_CAP_NG_H */
1717 if (username
|| chroot_dir
)
1718 droproot(username
, chroot_dir
);
1723 if (pcap_setfilter(pd
, &fcode
) < 0)
1724 error("%s", pcap_geterr(pd
));
1725 #ifdef HAVE_CAPSICUM
1726 if (RFileName
== NULL
&& VFileName
== NULL
) {
1727 static const unsigned long cmds
[] = { BIOCGSTATS
};
1729 cap_rights_init(&rights
, CAP_IOCTL
, CAP_READ
);
1730 if (cap_rights_limit(pcap_fileno(pd
), &rights
) < 0 &&
1732 error("unable to limit pcap descriptor");
1734 if (cap_ioctls_limit(pcap_fileno(pd
), cmds
,
1735 sizeof(cmds
) / sizeof(cmds
[0])) < 0 && errno
!= ENOSYS
) {
1736 error("unable to limit ioctls on pcap descriptor");
1742 /* Do not exceed the default PATH_MAX for files. */
1743 dumpinfo
.CurrentFileName
= (char *)malloc(PATH_MAX
+ 1);
1745 if (dumpinfo
.CurrentFileName
== NULL
)
1746 error("malloc of dumpinfo.CurrentFileName");
1748 /* We do not need numbering for dumpfiles if Cflag isn't set. */
1750 MakeFilename(dumpinfo
.CurrentFileName
, WFileName
, 0, WflagChars
);
1752 MakeFilename(dumpinfo
.CurrentFileName
, WFileName
, 0, 0);
1754 p
= pcap_dump_open(pd
, dumpinfo
.CurrentFileName
);
1755 #ifdef HAVE_CAP_NG_H
1756 /* Give up capabilities, clear Effective set */
1757 capng_clear(CAPNG_EFFECTIVE
);
1760 error("%s", pcap_geterr(pd
));
1761 #ifdef HAVE_CAPSICUM
1762 cap_rights_init(&rights
, CAP_SEEK
, CAP_WRITE
);
1763 if (cap_rights_limit(fileno(pcap_dump_file(p
)), &rights
) < 0 &&
1765 error("unable to limit dump descriptor");
1768 if (Cflag
!= 0 || Gflag
!= 0) {
1769 #ifdef HAVE_CAPSICUM
1770 dumpinfo
.WFileName
= strdup(basename(WFileName
));
1771 dumpinfo
.dirfd
= open(dirname(WFileName
),
1772 O_DIRECTORY
| O_RDONLY
);
1773 if (dumpinfo
.dirfd
< 0) {
1774 error("unable to open directory %s",
1775 dirname(WFileName
));
1777 cap_rights_init(&rights
, CAP_CREATE
, CAP_FCNTL
,
1778 CAP_FTRUNCATE
, CAP_LOOKUP
, CAP_SEEK
, CAP_WRITE
);
1779 if (cap_rights_limit(dumpinfo
.dirfd
, &rights
) < 0 &&
1781 error("unable to limit directory rights");
1783 #else /* !HAVE_CAPSICUM */
1784 dumpinfo
.WFileName
= WFileName
;
1786 callback
= dump_packet_and_trunc
;
1789 pcap_userdata
= (u_char
*)&dumpinfo
;
1791 callback
= dump_packet
;
1792 pcap_userdata
= (u_char
*)p
;
1794 #ifdef HAVE_PCAP_DUMP_FLUSH
1799 type
= pcap_datalink(pd
);
1800 printinfo
= get_print_info(type
);
1801 callback
= print_packet
;
1802 pcap_userdata
= (u_char
*)&printinfo
;
1805 #ifdef SIGNAL_REQ_INFO
1807 * We can't get statistics when reading from a file rather
1808 * than capturing from a device.
1810 if (RFileName
== NULL
)
1811 (void)setsignal(SIGNAL_REQ_INFO
, requestinfo
);
1814 if (vflag
> 0 && WFileName
) {
1816 * When capturing to a file, "-v" means tcpdump should,
1817 * every 10 secodns, "v"erbosely report the number of
1820 #ifdef USE_WIN32_MM_TIMER
1821 /* call verbose_stats_dump() each 1000 +/-100msec */
1822 timer_id
= timeSetEvent(1000, 100, verbose_stats_dump
, 0, TIME_PERIODIC
);
1823 setvbuf(stderr
, NULL
, _IONBF
, 0);
1824 #elif defined(HAVE_ALARM)
1825 (void)setsignal(SIGALRM
, verbose_stats_dump
);
1831 if (RFileName
== NULL
) {
1833 * Live capture (if -V was specified, we set RFileName
1834 * to a file from the -V file). Print a message to
1835 * the standard error on UN*X.
1837 if (!vflag
&& !WFileName
) {
1838 (void)fprintf(stderr
,
1839 "%s: verbose output suppressed, use -v or -vv for full protocol decode\n",
1842 (void)fprintf(stderr
, "%s: ", program_name
);
1843 dlt
= pcap_datalink(pd
);
1844 dlt_name
= pcap_datalink_val_to_name(dlt
);
1845 if (dlt_name
== NULL
) {
1846 (void)fprintf(stderr
, "listening on %s, link-type %u, capture size %u bytes\n",
1847 device
, dlt
, snaplen
);
1849 (void)fprintf(stderr
, "listening on %s, link-type %s (%s), capture size %u bytes\n",
1851 pcap_datalink_val_to_description(dlt
), snaplen
);
1853 (void)fflush(stderr
);
1857 #ifdef HAVE_CAPSICUM
1858 cansandbox
= (nflag
&& VFileName
== NULL
&& zflag
== NULL
);
1859 if (cansandbox
&& cap_enter() < 0 && errno
!= ENOSYS
)
1860 error("unable to enter the capability mode");
1861 if (cap_sandboxed())
1862 fprintf(stderr
, "capability mode sandbox enabled\n");
1863 #endif /* HAVE_CAPSICUM */
1866 status
= pcap_loop(pd
, cnt
, callback
, pcap_userdata
);
1867 if (WFileName
== NULL
) {
1869 * We're printing packets. Flush the printed output,
1870 * so it doesn't get intermingled with error output.
1874 * We got interrupted, so perhaps we didn't
1875 * manage to finish a line we were printing.
1876 * Print an extra newline, just in case.
1880 (void)fflush(stdout
);
1884 * We got interrupted. If we are reading multiple
1885 * files (via -V) set these so that we stop.
1894 (void)fprintf(stderr
, "%s: pcap_loop: %s\n",
1895 program_name
, pcap_geterr(pd
));
1897 if (RFileName
== NULL
) {
1899 * We're doing a live capture. Report the capture
1905 if (VFileName
!= NULL
) {
1906 ret
= get_next_file(VFile
, VFileLine
);
1908 RFileName
= VFileLine
;
1909 pd
= pcap_open_offline(RFileName
, ebuf
);
1912 #ifdef HAVE_CAPSICUM
1913 cap_rights_init(&rights
, CAP_READ
);
1914 if (cap_rights_limit(fileno(pcap_file(pd
)),
1915 &rights
) < 0 && errno
!= ENOSYS
) {
1916 error("unable to limit pcap descriptor");
1919 new_dlt
= pcap_datalink(pd
);
1920 if (WFileName
&& new_dlt
!= dlt
)
1921 error("%s: new dlt does not match original", RFileName
);
1922 printinfo
= get_print_info(new_dlt
);
1923 dlt_name
= pcap_datalink_val_to_name(new_dlt
);
1924 if (dlt_name
== NULL
) {
1925 fprintf(stderr
, "reading from file %s, link-type %u\n",
1926 RFileName
, new_dlt
);
1929 "reading from file %s, link-type %s (%s)\n",
1930 RFileName
, dlt_name
,
1931 pcap_datalink_val_to_description(new_dlt
));
1933 if (pcap_compile(pd
, &fcode
, cmdbuf
, Oflag
, netmask
) < 0)
1934 error("%s", pcap_geterr(pd
));
1935 if (pcap_setfilter(pd
, &fcode
) < 0)
1936 error("%s", pcap_geterr(pd
));
1940 while (ret
!= NULL
);
1943 exit(status
== -1 ? 1 : 0);
1946 /* make a clean exit on interrupts */
1948 cleanup(int signo _U_
)
1950 #ifdef USE_WIN32_MM_TIMER
1952 timeKillEvent(timer_id
);
1954 #elif defined(HAVE_ALARM)
1958 #ifdef HAVE_PCAP_BREAKLOOP
1960 * We have "pcap_breakloop()"; use it, so that we do as little
1961 * as possible in the signal handler (it's probably not safe
1962 * to do anything with standard I/O streams in a signal handler -
1963 * the ANSI C standard doesn't say it is).
1968 * We don't have "pcap_breakloop()"; this isn't safe, but
1969 * it's the best we can do. Print the summary if we're
1970 * not reading from a savefile - i.e., if we're doing a
1971 * live capture - and exit.
1973 if (pd
!= NULL
&& pcap_file(pd
) == NULL
) {
1975 * We got interrupted, so perhaps we didn't
1976 * manage to finish a line we were printing.
1977 * Print an extra newline, just in case.
1980 (void)fflush(stdout
);
1988 On windows, we do not use a fork, so we do not care less about
1989 waiting a child processes to die
1991 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
1993 child_cleanup(int signo _U_
)
1997 #endif /* HAVE_FORK && HAVE_VFORK */
2000 info(register int verbose
)
2002 struct pcap_stat stat
;
2005 * Older versions of libpcap didn't set ps_ifdrop on some
2006 * platforms; initialize it to 0 to handle that.
2009 if (pcap_stats(pd
, &stat
) < 0) {
2010 (void)fprintf(stderr
, "pcap_stats: %s\n", pcap_geterr(pd
));
2016 fprintf(stderr
, "%s: ", program_name
);
2018 (void)fprintf(stderr
, "%u packet%s captured", packets_captured
,
2019 PLURAL_SUFFIX(packets_captured
));
2021 fputs(", ", stderr
);
2024 (void)fprintf(stderr
, "%u packet%s received by filter", stat
.ps_recv
,
2025 PLURAL_SUFFIX(stat
.ps_recv
));
2027 fputs(", ", stderr
);
2030 (void)fprintf(stderr
, "%u packet%s dropped by kernel", stat
.ps_drop
,
2031 PLURAL_SUFFIX(stat
.ps_drop
));
2032 if (stat
.ps_ifdrop
!= 0) {
2034 fputs(", ", stderr
);
2037 (void)fprintf(stderr
, "%u packet%s dropped by interface\n",
2038 stat
.ps_ifdrop
, PLURAL_SUFFIX(stat
.ps_ifdrop
));
2044 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
2046 compress_savefile(const char *filename
)
2055 * Set to lowest priority so that this doesn't disturb the capture
2058 setpriority(PRIO_PROCESS
, 0, NZERO
- 1);
2060 setpriority(PRIO_PROCESS
, 0, 19);
2062 if (execlp(zflag
, zflag
, filename
, (char *)NULL
) == -1)
2064 "compress_savefile:execlp(%s, %s): %s\n",
2074 #else /* HAVE_FORK && HAVE_VFORK */
2076 compress_savefile(const char *filename
)
2079 "compress_savefile failed. Functionality not implemented under your system\n");
2081 #endif /* HAVE_FORK && HAVE_VFORK */
2084 dump_packet_and_trunc(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
2086 struct dump_info
*dump_info
;
2087 #ifdef HAVE_CAPSICUM
2088 cap_rights_t rights
;
2095 dump_info
= (struct dump_info
*)user
;
2098 * XXX - this won't force the file to rotate on the specified time
2099 * boundary, but it will rotate on the first packet received after the
2100 * specified Gflag number of seconds. Note: if a Gflag time boundary
2101 * and a Cflag size boundary coincide, the time rotation will occur
2102 * first thereby cancelling the Cflag boundary (since the file should
2106 /* Check if it is time to rotate */
2109 /* Get the current time */
2110 if ((t
= time(NULL
)) == (time_t)-1) {
2111 error("dump_and_trunc_packet: can't get current_time: %s",
2112 pcap_strerror(errno
));
2116 /* If the time is greater than the specified window, rotate */
2117 if (t
- Gflag_time
>= Gflag
) {
2118 #ifdef HAVE_CAPSICUM
2123 /* Update the Gflag_time */
2125 /* Update Gflag_count */
2128 * Close the current file and open a new one.
2130 pcap_dump_close(dump_info
->p
);
2133 * Compress the file we just closed, if the user asked for it
2136 compress_savefile(dump_info
->CurrentFileName
);
2139 * Check to see if we've exceeded the Wflag (when
2142 if (Cflag
== 0 && Wflag
> 0 && Gflag_count
>= Wflag
) {
2143 (void)fprintf(stderr
, "Maximum file limit reached: %d\n",
2148 if (dump_info
->CurrentFileName
!= NULL
)
2149 free(dump_info
->CurrentFileName
);
2150 /* Allocate space for max filename + \0. */
2151 dump_info
->CurrentFileName
= (char *)malloc(PATH_MAX
+ 1);
2152 if (dump_info
->CurrentFileName
== NULL
)
2153 error("dump_packet_and_trunc: malloc");
2155 * Gflag was set otherwise we wouldn't be here. Reset the count
2156 * so multiple files would end with 1,2,3 in the filename.
2157 * The counting is handled with the -C flow after this.
2162 * This is always the first file in the Cflag
2164 * We also don't need numbering if Cflag is not set.
2167 MakeFilename(dump_info
->CurrentFileName
, dump_info
->WFileName
, 0,
2170 MakeFilename(dump_info
->CurrentFileName
, dump_info
->WFileName
, 0, 0);
2172 #ifdef HAVE_CAP_NG_H
2173 capng_update(CAPNG_ADD
, CAPNG_EFFECTIVE
, CAP_DAC_OVERRIDE
);
2174 capng_apply(CAPNG_EFFECTIVE
);
2175 #endif /* HAVE_CAP_NG_H */
2176 #ifdef HAVE_CAPSICUM
2177 fd
= openat(dump_info
->dirfd
,
2178 dump_info
->CurrentFileName
,
2179 O_CREAT
| O_WRONLY
| O_TRUNC
, 0644);
2181 error("unable to open file %s",
2182 dump_info
->CurrentFileName
);
2184 fp
= fdopen(fd
, "w");
2186 error("unable to fdopen file %s",
2187 dump_info
->CurrentFileName
);
2189 dump_info
->p
= pcap_dump_fopen(dump_info
->pd
, fp
);
2190 #else /* !HAVE_CAPSICUM */
2191 dump_info
->p
= pcap_dump_open(dump_info
->pd
, dump_info
->CurrentFileName
);
2193 #ifdef HAVE_CAP_NG_H
2194 capng_update(CAPNG_DROP
, CAPNG_EFFECTIVE
, CAP_DAC_OVERRIDE
);
2195 capng_apply(CAPNG_EFFECTIVE
);
2196 #endif /* HAVE_CAP_NG_H */
2197 if (dump_info
->p
== NULL
)
2198 error("%s", pcap_geterr(pd
));
2199 #ifdef HAVE_CAPSICUM
2200 cap_rights_init(&rights
, CAP_SEEK
, CAP_WRITE
);
2201 if (cap_rights_limit(fileno(pcap_dump_file(dump_info
->p
)),
2202 &rights
) < 0 && errno
!= ENOSYS
) {
2203 error("unable to limit dump descriptor");
2210 * XXX - this won't prevent capture files from getting
2211 * larger than Cflag - the last packet written to the
2212 * file could put it over Cflag.
2214 if (Cflag
!= 0 && pcap_dump_ftell(dump_info
->p
) > Cflag
) {
2215 #ifdef HAVE_CAPSICUM
2221 * Close the current file and open a new one.
2223 pcap_dump_close(dump_info
->p
);
2226 * Compress the file we just closed, if the user asked for it
2229 compress_savefile(dump_info
->CurrentFileName
);
2233 if (Cflag_count
>= Wflag
)
2236 if (dump_info
->CurrentFileName
!= NULL
)
2237 free(dump_info
->CurrentFileName
);
2238 dump_info
->CurrentFileName
= (char *)malloc(PATH_MAX
+ 1);
2239 if (dump_info
->CurrentFileName
== NULL
)
2240 error("dump_packet_and_trunc: malloc");
2241 MakeFilename(dump_info
->CurrentFileName
, dump_info
->WFileName
, Cflag_count
, WflagChars
);
2242 #ifdef HAVE_CAPSICUM
2243 fd
= openat(dump_info
->dirfd
, dump_info
->CurrentFileName
,
2244 O_CREAT
| O_WRONLY
| O_TRUNC
, 0644);
2246 error("unable to open file %s",
2247 dump_info
->CurrentFileName
);
2249 fp
= fdopen(fd
, "w");
2251 error("unable to fdopen file %s",
2252 dump_info
->CurrentFileName
);
2254 dump_info
->p
= pcap_dump_fopen(dump_info
->pd
, fp
);
2255 #else /* !HAVE_CAPSICUM */
2256 dump_info
->p
= pcap_dump_open(dump_info
->pd
, dump_info
->CurrentFileName
);
2258 if (dump_info
->p
== NULL
)
2259 error("%s", pcap_geterr(pd
));
2260 #ifdef HAVE_CAPSICUM
2261 cap_rights_init(&rights
, CAP_SEEK
, CAP_WRITE
);
2262 if (cap_rights_limit(fileno(pcap_dump_file(dump_info
->p
)),
2263 &rights
) < 0 && errno
!= ENOSYS
) {
2264 error("unable to limit dump descriptor");
2269 pcap_dump((u_char
*)dump_info
->p
, h
, sp
);
2270 #ifdef HAVE_PCAP_DUMP_FLUSH
2272 pcap_dump_flush(dump_info
->p
);
2281 dump_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
2287 pcap_dump(user
, h
, sp
);
2288 #ifdef HAVE_PCAP_DUMP_FLUSH
2290 pcap_dump_flush((pcap_dumper_t
*)user
);
2299 print_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
2301 struct print_info
*print_info
;
2303 netdissect_options
*ndo
;
2309 print_info
= (struct print_info
*)user
;
2310 ndo
= print_info
->ndo
;
2312 if(ndo
->ndo_packet_number
)
2313 ND_PRINT((ndo
, "%5u ", packets_captured
));
2315 ts_print(ndo
, &h
->ts
);
2318 * Some printers want to check that they're not walking off the
2319 * end of the packet.
2320 * Rather than pass it all the way down, we set this member
2321 * of the netdissect_options structure.
2323 ndo
->ndo_snapend
= sp
+ h
->caplen
;
2325 if(print_info
->ndo_type
) {
2326 hdrlen
= (*print_info
->p
.ndo_printer
)(print_info
->ndo
, h
, sp
);
2328 hdrlen
= (*print_info
->p
.printer
)(h
, sp
);
2332 * Restore the original snapend, as a printer might have
2335 ndo
->ndo_snapend
= sp
+ h
->caplen
;
2336 if (ndo
->ndo_Xflag
) {
2338 * Print the raw packet data in hex and ASCII.
2340 if (ndo
->ndo_Xflag
> 1) {
2342 * Include the link-layer header.
2344 hex_and_ascii_print(ndo
, "\n\t", sp
, h
->caplen
);
2347 * Don't include the link-layer header - and if
2348 * we have nothing past the link-layer header,
2351 if (h
->caplen
> hdrlen
)
2352 hex_and_ascii_print(ndo
, "\n\t", sp
+ hdrlen
,
2353 h
->caplen
- hdrlen
);
2355 } else if (ndo
->ndo_xflag
) {
2357 * Print the raw packet data in hex.
2359 if (ndo
->ndo_xflag
> 1) {
2361 * Include the link-layer header.
2363 hex_print(ndo
, "\n\t", sp
, h
->caplen
);
2366 * Don't include the link-layer header - and if
2367 * we have nothing past the link-layer header,
2370 if (h
->caplen
> hdrlen
)
2371 hex_print(ndo
, "\n\t", sp
+ hdrlen
,
2372 h
->caplen
- hdrlen
);
2374 } else if (ndo
->ndo_Aflag
) {
2376 * Print the raw packet data in ASCII.
2378 if (ndo
->ndo_Aflag
> 1) {
2380 * Include the link-layer header.
2382 ascii_print(ndo
, sp
, h
->caplen
);
2385 * Don't include the link-layer header - and if
2386 * we have nothing past the link-layer header,
2389 if (h
->caplen
> hdrlen
)
2390 ascii_print(ndo
, sp
+ hdrlen
, h
->caplen
- hdrlen
);
2403 * XXX - there should really be libpcap calls to get the version
2404 * number as a string (the string would be generated from #defines
2405 * at run time, so that it's not generated from string constants
2406 * in the library, as, on many UNIX systems, those constants would
2407 * be statically linked into the application executable image, and
2408 * would thus reflect the version of libpcap on the system on
2409 * which the application was *linked*, not the system on which it's
2412 * That routine should be documented, unlike the "version[]"
2413 * string, so that UNIX vendors providing their own libpcaps
2414 * don't omit it (as a couple of vendors have...).
2416 * Packet.dll should perhaps also export a routine to return the
2417 * version number of the Packet.dll code, to supply the
2418 * "Wpcap_version" information on Windows.
2420 char WDversion
[]="current-git.tcpdump.org";
2421 #if !defined(HAVE_GENERATED_VERSION)
2422 char version
[]="current-git.tcpdump.org";
2424 char pcap_version
[]="current-git.tcpdump.org";
2425 char Wpcap_version
[]="3.1";
2429 * By default, print the specified data out in hex and ASCII.
2432 ndo_default_print(netdissect_options
*ndo
, const u_char
*bp
, u_int length
)
2434 hex_and_ascii_print(ndo
, "\n\t", bp
, length
); /* pass on lf and indentation string */
2438 default_print(const u_char
*bp
, u_int length
)
2440 ndo_default_print(gndo
, bp
, length
);
2443 #ifdef SIGNAL_REQ_INFO
2444 RETSIGTYPE
requestinfo(int signo _U_
)
2454 * Called once each second in verbose mode while dumping to file
2456 #ifdef USE_WIN32_MM_TIMER
2457 void CALLBACK
verbose_stats_dump (UINT timer_id _U_
, UINT msg _U_
, DWORD_PTR arg _U_
,
2458 DWORD_PTR dw1 _U_
, DWORD_PTR dw2 _U_
)
2460 struct pcap_stat stat
;
2462 if (infodelay
== 0 && pcap_stats(pd
, &stat
) >= 0)
2463 fprintf(stderr
, "Got %u\r", packets_captured
);
2465 #elif defined(HAVE_ALARM)
2466 static void verbose_stats_dump(int sig _U_
)
2468 struct pcap_stat stat
;
2470 if (infodelay
== 0 && pcap_stats(pd
, &stat
) >= 0)
2471 fprintf(stderr
, "Got %u\r", packets_captured
);
2476 USES_APPLE_DEPRECATED_API
2480 extern char version
[];
2481 #ifndef HAVE_PCAP_LIB_VERSION
2482 #if defined(WIN32) || defined(HAVE_PCAP_VERSION)
2483 extern char pcap_version
[];
2484 #else /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
2485 static char pcap_version
[] = "unknown";
2486 #endif /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
2487 #endif /* HAVE_PCAP_LIB_VERSION */
2489 #ifdef HAVE_PCAP_LIB_VERSION
2491 (void)fprintf(stderr
, "%s version %s, based on tcpdump version %s\n", program_name
, WDversion
, version
);
2493 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
2495 (void)fprintf(stderr
, "%s\n",pcap_lib_version());
2496 #else /* HAVE_PCAP_LIB_VERSION */
2498 (void)fprintf(stderr
, "%s version %s, based on tcpdump version %s\n", program_name
, WDversion
, version
);
2499 (void)fprintf(stderr
, "WinPcap version %s, based on libpcap version %s\n",Wpcap_version
, pcap_version
);
2501 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
2502 (void)fprintf(stderr
, "libpcap version %s\n", pcap_version
);
2504 #endif /* HAVE_PCAP_LIB_VERSION */
2506 #if defined(HAVE_LIBCRYPTO) && defined(SSLEAY_VERSION)
2507 (void)fprintf (stderr
, "%s\n", SSLeay_version(SSLEAY_VERSION
));
2511 (void)fprintf (stderr
, "SMI-library: %s\n", smi_version_string
);
2520 (void)fprintf(stderr
,
2521 "Usage: %s [-aAbd" D_FLAG
"efhH" I_FLAG J_FLAG
"KlLnNOpqRStu" U_FLAG
"vxX#]" B_FLAG_USAGE
" [ -c count ]\n", program_name
);
2522 (void)fprintf(stderr
,
2523 "\t\t[ -C file_size ] [ -E algo:secret ] [ -F file ] [ -G seconds ]\n");
2524 (void)fprintf(stderr
,
2525 "\t\t[ -i interface ]" j_FLAG_USAGE
" [ -M secret ] [ --number ]\n");
2526 #ifdef HAVE_PCAP_SETDIRECTION
2527 (void)fprintf(stderr
,
2528 "\t\t[ -Q in|out|inout ]\n");
2530 (void)fprintf(stderr
,
2531 "\t\t[ -r file ] [ -s snaplen ] ");
2532 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
2533 (void)fprintf(stderr
, "[ --time-stamp-precision precision ]\n");
2534 (void)fprintf(stderr
,
2537 (void)fprintf(stderr
, "[ -T type ] [ --version ] [ -V file ]\n");
2538 (void)fprintf(stderr
,
2539 "\t\t[ -w file ] [ -W filecount ] [ -y datalinktype ] [ -z command ]\n");
2540 (void)fprintf(stderr
,
2541 "\t\t[ -Z user ] [ expression ]\n");
2548 ndo_error(netdissect_options
*ndo _U_
, const char *fmt
, ...)
2552 (void)fprintf(stderr
, "%s: ", program_name
);
2554 (void)vfprintf(stderr
, fmt
, ap
);
2558 if (fmt
[-1] != '\n')
2559 (void)fputc('\n', stderr
);
2567 ndo_warning(netdissect_options
*ndo _U_
, const char *fmt
, ...)
2571 (void)fprintf(stderr
, "%s: WARNING: ", program_name
);
2573 (void)vfprintf(stderr
, fmt
, ap
);
2577 if (fmt
[-1] != '\n')
2578 (void)fputc('\n', stderr
);
2583 * c-style: whitesmith