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";
32 static const char rcsid
[] _U_
=
33 "@(#) $Header: /tcpdump/master/tcpdump/tcpdump.c,v 1.283 2008-09-25 21:45:50 guy Exp $ (LBL)";
37 * tcpdump - monitor tcp/ip traffic on an ethernet.
39 * First written in 1987 by Van Jacobson, Lawrence Berkeley Laboratory.
40 * Mercilessly hacked and occasionally improved since then via the
41 * combined efforts of Van, Steve McCanne and Craig Leres of LBL.
48 #include <tcpdump-stdinc.h>
53 extern int strcasecmp (const char *__s1
, const char *__s2
);
71 #include <sys/resource.h>
78 #include "netdissect.h"
79 #include "interface.h"
80 #include "addrtoname.h"
82 #include "setsignal.h"
83 #include "gmt2local.h"
84 #include "pcap-missing.h"
90 netdissect_options Gndo
;
91 netdissect_options
*gndo
= &Gndo
;
93 static int dflag
; /* print filter code */
94 static int Lflag
; /* list available data link types and exit */
95 static char *zflag
= NULL
; /* compress each savefile using a specified command (like gzip or bzip2) */
102 int32_t thiszone
; /* seconds offset from gmt to local time */
105 static RETSIGTYPE
cleanup(int);
106 static RETSIGTYPE
child_cleanup(int);
107 static void usage(void) __attribute__((noreturn
));
108 static void show_dlts_and_exit(const char *device
, pcap_t
*pd
) __attribute__((noreturn
));
110 static void print_packet(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
111 static void ndo_default_print(netdissect_options
*, const u_char
*, u_int
);
112 static void dump_packet_and_trunc(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
113 static void dump_packet(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
114 static void droproot(const char *, const char *);
115 static void ndo_error(netdissect_options
*ndo
, const char *fmt
, ...);
116 static void ndo_warning(netdissect_options
*ndo
, const char *fmt
, ...);
119 RETSIGTYPE
requestinfo(int);
122 #if defined(USE_WIN32_MM_TIMER)
123 #include <MMsystem.h>
124 static UINT timer_id
;
125 static void CALLBACK
verbose_stats_dump(UINT
, UINT
, DWORD_PTR
, DWORD_PTR
, DWORD_PTR
);
126 #elif defined(HAVE_ALARM)
127 static void verbose_stats_dump(int sig
);
130 static void info(int);
131 static u_int packets_captured
;
133 typedef u_int (*if_printer
)(const struct pcap_pkthdr
*, const u_char
*);
134 typedef u_int (*if_ndo_printer
)(struct netdissect_options
*ndo
,
135 const struct pcap_pkthdr
*, const u_char
*);
149 static struct printer printers
[] = {
150 { arcnet_if_print
, DLT_ARCNET
},
151 #ifdef DLT_ARCNET_LINUX
152 { arcnet_linux_if_print
, DLT_ARCNET_LINUX
},
154 { ether_if_print
, DLT_EN10MB
},
155 { token_if_print
, DLT_IEEE802
},
157 { lane_if_print
, DLT_LANE8023
},
160 { cip_if_print
, DLT_CIP
},
163 { cip_if_print
, DLT_ATM_CLIP
},
165 { sl_if_print
, DLT_SLIP
},
166 #ifdef DLT_SLIP_BSDOS
167 { sl_bsdos_if_print
, DLT_SLIP_BSDOS
},
169 { ppp_if_print
, DLT_PPP
},
170 #ifdef DLT_PPP_WITHDIRECTION
171 { ppp_if_print
, DLT_PPP_WITHDIRECTION
},
174 { ppp_bsdos_if_print
, DLT_PPP_BSDOS
},
176 { fddi_if_print
, DLT_FDDI
},
177 { null_if_print
, DLT_NULL
},
179 { null_if_print
, DLT_LOOP
},
181 { raw_if_print
, DLT_RAW
},
182 { atm_if_print
, DLT_ATM_RFC1483
},
184 { chdlc_if_print
, DLT_C_HDLC
},
187 { chdlc_if_print
, DLT_HDLC
},
189 #ifdef DLT_PPP_SERIAL
190 { ppp_hdlc_if_print
, DLT_PPP_SERIAL
},
193 { pppoe_if_print
, DLT_PPP_ETHER
},
196 { sll_if_print
, DLT_LINUX_SLL
},
198 #ifdef DLT_IEEE802_11
199 { ieee802_11_if_print
, DLT_IEEE802_11
},
202 { ltalk_if_print
, DLT_LTALK
},
204 #if defined(DLT_PFLOG) && defined(HAVE_NET_PFVAR_H)
205 { pflog_if_print
, DLT_PFLOG
},
208 { fr_if_print
, DLT_FR
},
211 { fr_if_print
, DLT_FRELAY
},
214 { sunatm_if_print
, DLT_SUNATM
},
216 #ifdef DLT_IP_OVER_FC
217 { ipfc_if_print
, DLT_IP_OVER_FC
},
219 #ifdef DLT_PRISM_HEADER
220 { prism_if_print
, DLT_PRISM_HEADER
},
222 #ifdef DLT_IEEE802_11_RADIO
223 { ieee802_11_radio_if_print
, DLT_IEEE802_11_RADIO
},
226 { enc_if_print
, DLT_ENC
},
228 #ifdef DLT_SYMANTEC_FIREWALL
229 { symantec_if_print
, DLT_SYMANTEC_FIREWALL
},
231 #ifdef DLT_APPLE_IP_OVER_IEEE1394
232 { ap1394_if_print
, DLT_APPLE_IP_OVER_IEEE1394
},
234 #ifdef DLT_IEEE802_11_RADIO_AVS
235 { ieee802_11_radio_avs_if_print
, DLT_IEEE802_11_RADIO_AVS
},
237 #ifdef DLT_JUNIPER_ATM1
238 { juniper_atm1_print
, DLT_JUNIPER_ATM1
},
240 #ifdef DLT_JUNIPER_ATM2
241 { juniper_atm2_print
, DLT_JUNIPER_ATM2
},
243 #ifdef DLT_JUNIPER_MFR
244 { juniper_mfr_print
, DLT_JUNIPER_MFR
},
246 #ifdef DLT_JUNIPER_MLFR
247 { juniper_mlfr_print
, DLT_JUNIPER_MLFR
},
249 #ifdef DLT_JUNIPER_MLPPP
250 { juniper_mlppp_print
, DLT_JUNIPER_MLPPP
},
252 #ifdef DLT_JUNIPER_PPPOE
253 { juniper_pppoe_print
, DLT_JUNIPER_PPPOE
},
255 #ifdef DLT_JUNIPER_PPPOE_ATM
256 { juniper_pppoe_atm_print
, DLT_JUNIPER_PPPOE_ATM
},
258 #ifdef DLT_JUNIPER_GGSN
259 { juniper_ggsn_print
, DLT_JUNIPER_GGSN
},
261 #ifdef DLT_JUNIPER_ES
262 { juniper_es_print
, DLT_JUNIPER_ES
},
264 #ifdef DLT_JUNIPER_MONITOR
265 { juniper_monitor_print
, DLT_JUNIPER_MONITOR
},
267 #ifdef DLT_JUNIPER_SERVICES
268 { juniper_services_print
, DLT_JUNIPER_SERVICES
},
270 #ifdef DLT_JUNIPER_ETHER
271 { juniper_ether_print
, DLT_JUNIPER_ETHER
},
273 #ifdef DLT_JUNIPER_PPP
274 { juniper_ppp_print
, DLT_JUNIPER_PPP
},
276 #ifdef DLT_JUNIPER_FRELAY
277 { juniper_frelay_print
, DLT_JUNIPER_FRELAY
},
279 #ifdef DLT_JUNIPER_CHDLC
280 { juniper_chdlc_print
, DLT_JUNIPER_CHDLC
},
283 { mfr_if_print
, DLT_MFR
},
285 #if defined(DLT_BLUETOOTH_HCI_H4_WITH_PHDR) && defined(HAVE_PCAP_BLUETOOTH_H)
286 { bt_if_print
, DLT_BLUETOOTH_HCI_H4_WITH_PHDR
},
288 #ifdef HAVE_PCAP_USB_H
290 { usb_linux_48_byte_print
, DLT_USB_LINUX
},
291 #endif /* DLT_USB_LINUX */
292 #ifdef DLT_USB_LINUX_MMAPPED
293 { usb_linux_64_byte_print
, DLT_USB_LINUX_MMAPPED
},
294 #endif /* DLT_USB_LINUX_MMAPPED */
295 #endif /* HAVE_PCAP_USB_H */
297 { raw_if_print
, DLT_IPV4
},
300 { raw_if_print
, DLT_IPV6
},
305 static struct ndo_printer ndo_printers
[] = {
307 { ipnet_if_print
, DLT_IPNET
},
309 #ifdef DLT_IEEE802_15_4
310 { ieee802_15_4_if_print
, DLT_IEEE802_15_4
},
316 lookup_printer(int type
)
320 for (p
= printers
; p
->f
; ++p
)
328 static if_ndo_printer
329 lookup_ndo_printer(int type
)
331 struct ndo_printer
*p
;
333 for (p
= ndo_printers
; p
->f
; ++p
)
343 static int supports_monitor_mode
;
350 netdissect_options
*ndo
;
353 if_ndo_printer ndo_printer
;
360 char *CurrentFileName
;
366 show_dlts_and_exit(const char *device
, pcap_t
*pd
)
370 const char *dlt_name
;
372 n_dlts
= pcap_list_datalinks(pd
, &dlts
);
374 error("%s", pcap_geterr(pd
));
375 else if (n_dlts
== 0 || !dlts
)
376 error("No data link types.");
379 * If the interface is known to support monitor mode, indicate
380 * whether these are the data link types available when not in
381 * monitor mode, if -I wasn't specified, or when in monitor mode,
382 * when -I was specified (the link-layer types available in
383 * monitor mode might be different from the ones available when
384 * not in monitor mode).
386 if (supports_monitor_mode
)
387 (void) fprintf(stderr
, "Data link types for %s %s (use option -y to set):\n",
389 Iflag
? "when in monitor mode" : "when not in monitor mode");
391 (void) fprintf(stderr
, "Data link types for %s (use option -y to set):\n",
394 while (--n_dlts
>= 0) {
395 dlt_name
= pcap_datalink_val_to_name(dlts
[n_dlts
]);
396 if (dlt_name
!= NULL
) {
397 (void) fprintf(stderr
, " %s (%s)", dlt_name
,
398 pcap_datalink_val_to_description(dlts
[n_dlts
]));
401 * OK, does tcpdump handle that type?
403 if (lookup_printer(dlts
[n_dlts
]) == NULL
404 && lookup_ndo_printer(dlts
[n_dlts
]) == NULL
)
405 (void) fprintf(stderr
, " (printing not supported)");
408 (void) fprintf(stderr
, " DLT %d (printing not supported)\n",
417 * Set up flags that might or might not be supported depending on the
418 * version of libpcap we're using.
420 #if defined(HAVE_PCAP_CREATE) || defined(WIN32)
422 #define B_FLAG_USAGE " [ -B size ]"
423 #else /* defined(HAVE_PCAP_CREATE) || defined(WIN32) */
426 #endif /* defined(HAVE_PCAP_CREATE) || defined(WIN32) */
428 #ifdef HAVE_PCAP_CREATE
430 #else /* HAVE_PCAP_CREATE */
432 #endif /* HAVE_PCAP_CREATE */
434 #ifdef HAVE_PCAP_FINDALLDEVS
435 #ifndef HAVE_PCAP_IF_T
436 #undef HAVE_PCAP_FINDALLDEVS
440 #ifdef HAVE_PCAP_FINDALLDEVS
446 #ifdef HAVE_PCAP_DUMP_FLUSH
453 /* Drop root privileges and chroot if necessary */
455 droproot(const char *username
, const char *chroot_dir
)
457 struct passwd
*pw
= NULL
;
459 if (chroot_dir
&& !username
) {
460 fprintf(stderr
, "tcpdump: Chroot without dropping root is insecure\n");
464 pw
= getpwnam(username
);
467 if (chroot(chroot_dir
) != 0 || chdir ("/") != 0) {
468 fprintf(stderr
, "tcpdump: Couldn't chroot/chdir to '%.64s': %s\n",
469 chroot_dir
, pcap_strerror(errno
));
473 if (initgroups(pw
->pw_name
, pw
->pw_gid
) != 0 ||
474 setgid(pw
->pw_gid
) != 0 || setuid(pw
->pw_uid
) != 0) {
475 fprintf(stderr
, "tcpdump: Couldn't change to '%.32s' uid=%lu gid=%lu: %s\n",
477 (unsigned long)pw
->pw_uid
,
478 (unsigned long)pw
->pw_gid
,
479 pcap_strerror(errno
));
484 fprintf(stderr
, "tcpdump: Couldn't find user '%.32s'\n",
507 MakeFilename(char *buffer
, char *orig_name
, int cnt
, int max_chars
)
509 char *filename
= malloc(NAME_MAX
+ 1);
511 /* Process with strftime if Gflag is set. */
515 /* Convert Gflag_time to a usable format */
516 if ((local_tm
= localtime(&Gflag_time
)) == NULL
) {
517 error("MakeTimedFilename: localtime");
520 /* There's no good way to detect an error in strftime since a return
521 * value of 0 isn't necessarily failure.
523 strftime(filename
, NAME_MAX
, orig_name
, local_tm
);
525 strncpy(filename
, orig_name
, NAME_MAX
);
528 if (cnt
== 0 && max_chars
== 0)
529 strncpy(buffer
, filename
, NAME_MAX
+ 1);
531 if (snprintf(buffer
, NAME_MAX
+ 1, "%s%0*d", filename
, max_chars
, cnt
) > NAME_MAX
)
532 /* Report an error if the filename is too large */
533 error("too many output files or filename is too long (> %d)", NAME_MAX
);
537 static int tcpdump_printf(netdissect_options
*ndo _U_
,
538 const char *fmt
, ...)
545 ret
=vfprintf(stdout
, fmt
, args
);
552 main(int argc
, char **argv
)
554 register int cnt
, op
, i
;
555 bpf_u_int32 localnet
, netmask
;
556 register char *cp
, *infile
, *cmdbuf
, *device
, *RFileName
, *WFileName
;
557 pcap_handler callback
;
559 struct bpf_program fcode
;
561 RETSIGTYPE (*oldhandler
)(int);
563 struct print_info printinfo
;
564 struct dump_info dumpinfo
;
565 u_char
*pcap_userdata
;
566 char ebuf
[PCAP_ERRBUF_SIZE
];
567 char *username
= NULL
;
568 char *chroot_dir
= NULL
;
569 #ifdef HAVE_PCAP_FINDALLDEVS
570 pcap_if_t
*devpointer
;
575 if(wsockinit() != 0) return 1;
581 gndo
->ndo_default_print
=ndo_default_print
;
582 gndo
->ndo_printf
=tcpdump_printf
;
583 gndo
->ndo_error
=ndo_error
;
584 gndo
->ndo_warning
=ndo_warning
;
585 gndo
->ndo_snaplen
= DEFAULT_SNAPLEN
;
592 if ((cp
= strrchr(argv
[0], '/')) != NULL
)
593 program_name
= cp
+ 1;
595 program_name
= argv
[0];
597 if (abort_on_misalignment(ebuf
, sizeof(ebuf
)) < 0)
606 (op
= getopt(argc
, argv
, "aAb" B_FLAG
"c:C:d" D_FLAG
"eE:fF:G:i:" I_FLAG
"KlLm:M:nNOpqr:Rs:StT:u" U_FLAG
"vw:W:xXy:Yz:Z:")) != -1)
610 /* compatibility for old -a */
621 #if defined(HAVE_PCAP_CREATE) || defined(WIN32)
623 Bflag
= atoi(optarg
)*1024;
625 error("invalid packet buffer size %s", optarg
);
627 #endif /* defined(HAVE_PCAP_CREATE) || defined(WIN32) */
632 error("invalid packet count %s", optarg
);
636 Cflag
= atoi(optarg
) * 1000000;
638 error("invalid file size %s", optarg
);
645 #ifdef HAVE_PCAP_FINDALLDEVS
647 if (pcap_findalldevs(&devpointer
, ebuf
) < 0)
650 for (i
= 0; devpointer
!= 0; i
++) {
651 printf("%d.%s", i
+1, devpointer
->name
);
652 if (devpointer
->description
!= NULL
)
653 printf(" (%s)", devpointer
->description
);
655 devpointer
= devpointer
->next
;
659 #endif /* HAVE_PCAP_FINDALLDEVS */
670 #ifndef HAVE_LIBCRYPTO
671 warning("crypto code not compiled in");
673 gndo
->ndo_espsecret
= optarg
;
685 Gflag
= atoi(optarg
);
687 error("invalid number of seconds %s", optarg
);
689 /* We will create one file initially. */
692 /* Grab the current time for rotation use. */
693 if ((Gflag_time
= time(NULL
)) == (time_t)-1) {
694 error("main: can't get current time: %s",
695 pcap_strerror(errno
));
700 if (optarg
[0] == '0' && optarg
[1] == 0)
701 error("Invalid adapter index");
703 #ifdef HAVE_PCAP_FINDALLDEVS
705 * If the argument is a number, treat it as
706 * an index into the list of adapters, as
707 * printed by "tcpdump -D".
709 * This should be OK on UNIX systems, as interfaces
710 * shouldn't have names that begin with digits.
711 * It can be useful on Windows, where more than
712 * one interface can have the same name.
714 if ((devnum
= atoi(optarg
)) != 0) {
716 error("Invalid adapter index");
718 if (pcap_findalldevs(&devpointer
, ebuf
) < 0)
722 * Look for the devnum-th entry
723 * in the list of devices
727 i
< devnum
-1 && devpointer
!= NULL
;
728 i
++, devpointer
= devpointer
->next
)
730 if (devpointer
== NULL
)
731 error("Invalid adapter index");
733 device
= devpointer
->name
;
736 #endif /* HAVE_PCAP_FINDALLDEVS */
740 #ifdef HAVE_PCAP_CREATE
744 #endif /* HAVE_PCAP_CREATE */
749 * _IOLBF is the same as _IOFBF in Microsoft's C
750 * libraries; the only alternative they offer
753 * XXX - this should really be checking for MSVC++,
754 * not WIN32, if, for example, MinGW has its own
755 * C library that is more UNIX-compatible.
757 setvbuf(stdout
, NULL
, _IONBF
, 0);
759 #ifdef HAVE_SETLINEBUF
762 setvbuf(stdout
, NULL
, _IOLBF
, 0);
773 if (smiLoadModule(optarg
) == 0) {
774 error("could not load MIB module %s", optarg
);
778 (void)fprintf(stderr
, "%s: ignoring option `-m %s' ",
779 program_name
, optarg
);
780 (void)fprintf(stderr
, "(no libsmi support)\n");
785 /* TCP-MD5 shared secret */
786 #ifndef HAVE_LIBCRYPTO
787 warning("crypto code not compiled in");
810 ++suppress_default_print
;
824 snaplen
= strtol(optarg
, &end
, 0);
825 if (optarg
== end
|| *end
!= '\0'
826 || snaplen
< 0 || snaplen
> MAXIMUM_SNAPLEN
)
827 error("invalid snaplen %s", optarg
);
828 else if (snaplen
== 0)
829 snaplen
= MAXIMUM_SNAPLEN
;
842 if (strcasecmp(optarg
, "vat") == 0)
844 else if (strcasecmp(optarg
, "wb") == 0)
846 else if (strcasecmp(optarg
, "rpc") == 0)
848 else if (strcasecmp(optarg
, "rtp") == 0)
850 else if (strcasecmp(optarg
, "rtcp") == 0)
851 packettype
= PT_RTCP
;
852 else if (strcasecmp(optarg
, "snmp") == 0)
853 packettype
= PT_SNMP
;
854 else if (strcasecmp(optarg
, "cnfp") == 0)
855 packettype
= PT_CNFP
;
856 else if (strcasecmp(optarg
, "tftp") == 0)
857 packettype
= PT_TFTP
;
858 else if (strcasecmp(optarg
, "aodv") == 0)
859 packettype
= PT_AODV
;
861 error("unknown packet type `%s'", optarg
);
868 #ifdef HAVE_PCAP_DUMP_FLUSH
883 Wflag
= atoi(optarg
);
885 error("invalid number of output files %s", optarg
);
886 WflagChars
= getWflagChars(Wflag
);
891 ++suppress_default_print
;
896 ++suppress_default_print
;
900 gndo
->ndo_dltname
= optarg
;
902 pcap_datalink_name_to_val(gndo
->ndo_dltname
);
903 if (gndo
->ndo_dlt
< 0)
904 error("invalid data link type %s", gndo
->ndo_dltname
);
907 #if defined(HAVE_PCAP_DEBUG) || defined(HAVE_YYDEBUG)
910 /* Undocumented flag */
911 #ifdef HAVE_PCAP_DEBUG
912 extern int pcap_debug
;
923 zflag
= strdup(optarg
);
932 username
= strdup(optarg
);
947 case 0: /* Default */
948 case 4: /* Default + Date*/
949 thiszone
= gmt2local(0);
952 case 1: /* No time stamp */
953 case 2: /* Unix timeval style */
954 case 3: /* Microseconds since previous packet */
955 case 5: /* Microseconds since first packet */
958 default: /* Not supported */
959 error("only -t, -tt, -ttt, -tttt and -ttttt are supported");
964 /* if run as root, prepare for chrooting */
965 if (getuid() == 0 || geteuid() == 0) {
966 /* future extensibility for cmd-line arguments */
968 chroot_dir
= WITH_CHROOT
;
973 /* if run as root, prepare for dropping root privileges */
974 if (getuid() == 0 || geteuid() == 0) {
975 /* Run with '-Z root' to restore old behaviour */
977 username
= WITH_USER
;
981 if (RFileName
!= NULL
) {
983 const char *dlt_name
;
987 * We don't need network access, so relinquish any set-UID
988 * or set-GID privileges we have (if any).
990 * We do *not* want set-UID privileges when opening a
991 * trace file, as that might let the user read other
992 * people's trace files (especially if we're set-UID
995 if (setgid(getgid()) != 0 || setuid(getuid()) != 0 )
996 fprintf(stderr
, "Warning: setgid/setuid failed !\n");
998 pd
= pcap_open_offline(RFileName
, ebuf
);
1001 dlt
= pcap_datalink(pd
);
1002 dlt_name
= pcap_datalink_val_to_name(dlt
);
1003 if (dlt_name
== NULL
) {
1004 fprintf(stderr
, "reading from file %s, link-type %u\n",
1008 "reading from file %s, link-type %s (%s)\n",
1009 RFileName
, dlt_name
,
1010 pcap_datalink_val_to_description(dlt
));
1015 error("-f and -r options are incompatible");
1017 if (device
== NULL
) {
1018 device
= pcap_lookupdev(ebuf
);
1023 if(strlen(device
) == 1) //we assume that an ASCII string is always longer than 1 char
1024 { //a Unicode string has a \0 as second byte (so strlen() is 1)
1025 fprintf(stderr
, "%s: listening on %ws\n", program_name
, device
);
1029 fprintf(stderr
, "%s: listening on %s\n", program_name
, device
);
1034 #ifdef HAVE_PCAP_CREATE
1035 pd
= pcap_create(device
, ebuf
);
1039 * Is this an interface that supports monitor mode?
1041 if (pcap_can_set_rfmon(pd
) == 1)
1042 supports_monitor_mode
= 1;
1044 supports_monitor_mode
= 0;
1045 status
= pcap_set_snaplen(pd
, snaplen
);
1047 error("%s: pcap_set_snaplen failed: %s",
1048 device
, pcap_statustostr(status
));
1049 status
= pcap_set_promisc(pd
, !pflag
);
1051 error("%s: pcap_set_promisc failed: %s",
1052 device
, pcap_statustostr(status
));
1054 status
= pcap_set_rfmon(pd
, 1);
1056 error("%s: pcap_set_rfmon failed: %s",
1057 device
, pcap_statustostr(status
));
1059 status
= pcap_set_timeout(pd
, 1000);
1061 error("%s: pcap_set_timeout failed: %s",
1062 device
, pcap_statustostr(status
));
1064 status
= pcap_set_buffer_size(pd
, Bflag
);
1066 error("%s: pcap_set_buffer_size failed: %s",
1067 device
, pcap_statustostr(status
));
1069 status
= pcap_activate(pd
);
1072 * pcap_activate() failed.
1074 cp
= pcap_geterr(pd
);
1075 if (status
== PCAP_ERROR
)
1077 else if ((status
== PCAP_ERROR_NO_SUCH_DEVICE
||
1078 status
== PCAP_ERROR_PERM_DENIED
) &&
1080 error("%s: %s\n(%s)", device
,
1081 pcap_statustostr(status
), cp
);
1083 error("%s: %s", device
,
1084 pcap_statustostr(status
));
1085 } else if (status
> 0) {
1087 * pcap_activate() succeeded, but it's warning us
1088 * of a problem it had.
1090 cp
= pcap_geterr(pd
);
1091 if (status
== PCAP_WARNING
)
1093 else if (status
== PCAP_WARNING_PROMISC_NOTSUP
&&
1095 warning("%s: %s\n(%s)", device
,
1096 pcap_statustostr(status
), cp
);
1098 warning("%s: %s", device
,
1099 pcap_statustostr(status
));
1103 pd
= pcap_open_live(device
, snaplen
, !pflag
, 1000, ebuf
);
1107 warning("%s", ebuf
);
1108 #endif /* HAVE_PCAP_CREATE */
1110 * Let user own process after socket has been opened.
1113 if (setgid(getgid()) != 0 || setuid(getuid()) != 0)
1114 fprintf(stderr
, "Warning: setgid/setuid failed !\n");
1116 #if !defined(HAVE_PCAP_CREATE) && defined(WIN32)
1118 if(pcap_setbuff(pd
, Bflag
)==-1){
1119 error("%s", pcap_geterr(pd
));
1121 #endif /* !defined(HAVE_PCAP_CREATE) && defined(WIN32) */
1123 show_dlts_and_exit(device
, pd
);
1124 if (gndo
->ndo_dlt
>= 0) {
1125 #ifdef HAVE_PCAP_SET_DATALINK
1126 if (pcap_set_datalink(pd
, gndo
->ndo_dlt
) < 0)
1127 error("%s", pcap_geterr(pd
));
1130 * We don't actually support changing the
1131 * data link type, so we only let them
1132 * set it to what it already is.
1134 if (gndo
->ndo_dlt
!= pcap_datalink(pd
)) {
1135 error("%s is not one of the DLTs supported by this device\n",
1139 (void)fprintf(stderr
, "%s: data link type %s\n",
1140 program_name
, gndo
->ndo_dltname
);
1141 (void)fflush(stderr
);
1143 i
= pcap_snapshot(pd
);
1145 warning("snaplen raised from %d to %d", snaplen
, i
);
1148 if (pcap_lookupnet(device
, &localnet
, &netmask
, ebuf
) < 0) {
1151 warning("%s", ebuf
);
1155 cmdbuf
= read_infile(infile
);
1157 cmdbuf
= copy_argv(&argv
[optind
]);
1159 if (pcap_compile(pd
, &fcode
, cmdbuf
, Oflag
, netmask
) < 0)
1160 error("%s", pcap_geterr(pd
));
1163 bpf_dump(&fcode
, dflag
);
1167 init_addrtoname(localnet
, netmask
);
1171 (void)setsignal(SIGPIPE
, cleanup
);
1172 (void)setsignal(SIGTERM
, cleanup
);
1173 (void)setsignal(SIGINT
, cleanup
);
1174 (void)setsignal(SIGCHLD
, child_cleanup
);
1176 /* Cooperate with nohup(1) */
1178 if ((oldhandler
= setsignal(SIGHUP
, cleanup
)) != SIG_DFL
)
1179 (void)setsignal(SIGHUP
, oldhandler
);
1182 if (pcap_setfilter(pd
, &fcode
) < 0)
1183 error("%s", pcap_geterr(pd
));
1186 /* Do not exceed the default NAME_MAX for files. */
1187 dumpinfo
.CurrentFileName
= (char *)malloc(NAME_MAX
+ 1);
1189 if (dumpinfo
.CurrentFileName
== NULL
)
1190 error("malloc of dumpinfo.CurrentFileName");
1192 /* We do not need numbering for dumpfiles if Cflag isn't set. */
1194 MakeFilename(dumpinfo
.CurrentFileName
, WFileName
, 0, WflagChars
);
1196 MakeFilename(dumpinfo
.CurrentFileName
, WFileName
, 0, 0);
1198 p
= pcap_dump_open(pd
, dumpinfo
.CurrentFileName
);
1200 error("%s", pcap_geterr(pd
));
1201 if (Cflag
!= 0 || Gflag
!= 0) {
1202 callback
= dump_packet_and_trunc
;
1203 dumpinfo
.WFileName
= WFileName
;
1206 pcap_userdata
= (u_char
*)&dumpinfo
;
1208 callback
= dump_packet
;
1209 pcap_userdata
= (u_char
*)p
;
1212 type
= pcap_datalink(pd
);
1213 printinfo
.ndo_type
= 1;
1214 printinfo
.ndo
= gndo
;
1215 printinfo
.p
.ndo_printer
= lookup_ndo_printer(type
);
1216 if (printinfo
.p
.ndo_printer
== NULL
) {
1217 printinfo
.p
.printer
= lookup_printer(type
);
1218 printinfo
.ndo_type
= 0;
1219 if (printinfo
.p
.printer
== NULL
) {
1220 gndo
->ndo_dltname
= pcap_datalink_val_to_name(type
);
1221 if (gndo
->ndo_dltname
!= NULL
)
1222 error("packet printing is not supported for link type %s: use -w",
1225 error("packet printing is not supported for link type %d: use -w", type
);
1228 callback
= print_packet
;
1229 pcap_userdata
= (u_char
*)&printinfo
;
1233 * We cannot do this earlier, because we want to be able to open
1234 * the file (if done) for writing before giving up permissions.
1236 if (getuid() == 0 || geteuid() == 0) {
1237 if (username
|| chroot_dir
)
1238 droproot(username
, chroot_dir
);
1243 * We can't get statistics when reading from a file rather
1244 * than capturing from a device.
1246 if (RFileName
== NULL
)
1247 (void)setsignal(SIGINFO
, requestinfo
);
1250 if (vflag
> 0 && WFileName
) {
1252 * When capturing to a file, "-v" means tcpdump should,
1253 * every 10 secodns, "v"erbosely report the number of
1256 #ifdef USE_WIN32_MM_TIMER
1257 /* call verbose_stats_dump() each 1000 +/-100msec */
1258 timer_id
= timeSetEvent(1000, 100, verbose_stats_dump
, 0, TIME_PERIODIC
);
1259 setvbuf(stderr
, NULL
, _IONBF
, 0);
1260 #elif defined(HAVE_ALARM)
1261 (void)setsignal(SIGALRM
, verbose_stats_dump
);
1267 if (RFileName
== NULL
) {
1269 const char *dlt_name
;
1271 if (!vflag
&& !WFileName
) {
1272 (void)fprintf(stderr
,
1273 "%s: verbose output suppressed, use -v or -vv for full protocol decode\n",
1276 (void)fprintf(stderr
, "%s: ", program_name
);
1277 dlt
= pcap_datalink(pd
);
1278 dlt_name
= pcap_datalink_val_to_name(dlt
);
1279 if (dlt_name
== NULL
) {
1280 (void)fprintf(stderr
, "listening on %s, link-type %u, capture size %u bytes\n",
1281 device
, dlt
, snaplen
);
1283 (void)fprintf(stderr
, "listening on %s, link-type %s (%s), capture size %u bytes\n",
1285 pcap_datalink_val_to_description(dlt
), snaplen
);
1287 (void)fflush(stderr
);
1290 status
= pcap_loop(pd
, cnt
, callback
, pcap_userdata
);
1291 if (WFileName
== NULL
) {
1293 * We're printing packets. Flush the printed output,
1294 * so it doesn't get intermingled with error output.
1298 * We got interrupted, so perhaps we didn't
1299 * manage to finish a line we were printing.
1300 * Print an extra newline, just in case.
1304 (void)fflush(stdout
);
1310 (void)fprintf(stderr
, "%s: pcap_loop: %s\n",
1311 program_name
, pcap_geterr(pd
));
1313 if (RFileName
== NULL
) {
1315 * We're doing a live capture. Report the capture
1321 exit(status
== -1 ? 1 : 0);
1324 /* make a clean exit on interrupts */
1326 cleanup(int signo _U_
)
1328 #ifdef USE_WIN32_MM_TIMER
1330 timeKillEvent(timer_id
);
1332 #elif defined(HAVE_ALARM)
1336 #ifdef HAVE_PCAP_BREAKLOOP
1338 * We have "pcap_breakloop()"; use it, so that we do as little
1339 * as possible in the signal handler (it's probably not safe
1340 * to do anything with standard I/O streams in a signal handler -
1341 * the ANSI C standard doesn't say it is).
1346 * We don't have "pcap_breakloop()"; this isn't safe, but
1347 * it's the best we can do. Print the summary if we're
1348 * not reading from a savefile - i.e., if we're doing a
1349 * live capture - and exit.
1351 if (pd
!= NULL
&& pcap_file(pd
) == NULL
) {
1353 * We got interrupted, so perhaps we didn't
1354 * manage to finish a line we were printing.
1355 * Print an extra newline, just in case.
1358 (void)fflush(stdout
);
1366 On windows, we do not use a fork, so we do not care less about
1367 waiting a child processes to die
1371 child_cleanup(int signo _U_
)
1378 info(register int verbose
)
1380 struct pcap_stat stat
;
1383 * Older versions of libpcap didn't set ps_ifdrop on some
1384 * platforms; initialize it to 0 to handle that.
1387 if (pcap_stats(pd
, &stat
) < 0) {
1388 (void)fprintf(stderr
, "pcap_stats: %s\n", pcap_geterr(pd
));
1394 fprintf(stderr
, "%s: ", program_name
);
1396 (void)fprintf(stderr
, "%u packets captured", packets_captured
);
1398 fputs(", ", stderr
);
1401 (void)fprintf(stderr
, "%u packets received by filter", stat
.ps_recv
);
1403 fputs(", ", stderr
);
1406 (void)fprintf(stderr
, "%u packets dropped by kernel", stat
.ps_drop
);
1407 if (stat
.ps_ifdrop
!= 0) {
1409 fputs(", ", stderr
);
1412 (void)fprintf(stderr
, "%u packets dropped by interface\n",
1421 compress_savefile(const char *filename
)
1426 * Set to lowest priority so that this doesn't disturb the capture
1429 setpriority(PRIO_PROCESS
, 0, NZERO
- 1);
1431 setpriority(PRIO_PROCESS
, 0, 19);
1433 if (execlp(zflag
, zflag
, filename
, (char *)NULL
) == -1)
1435 "compress_savefile:execlp(%s, %s): %s\n",
1442 compress_savefile(const char *filename
)
1445 "compress_savefile failed. Functionality not implemented under windows\n");
1450 dump_packet_and_trunc(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
1452 struct dump_info
*dump_info
;
1458 dump_info
= (struct dump_info
*)user
;
1461 * XXX - this won't force the file to rotate on the specified time
1462 * boundary, but it will rotate on the first packet received after the
1463 * specified Gflag number of seconds. Note: if a Gflag time boundary
1464 * and a Cflag size boundary coincide, the time rotation will occur
1465 * first thereby cancelling the Cflag boundary (since the file should
1469 /* Check if it is time to rotate */
1472 /* Get the current time */
1473 if ((t
= time(NULL
)) == (time_t)-1) {
1474 error("dump_and_trunc_packet: can't get current_time: %s",
1475 pcap_strerror(errno
));
1479 /* If the time is greater than the specified window, rotate */
1480 if (t
- Gflag_time
>= Gflag
) {
1481 /* Update the Gflag_time */
1483 /* Update Gflag_count */
1486 * Close the current file and open a new one.
1488 pcap_dump_close(dump_info
->p
);
1491 * Compress the file we just closed, if the user asked for it
1494 compress_savefile(dump_info
->CurrentFileName
);
1497 * Check to see if we've exceeded the Wflag (when
1500 if (Cflag
== 0 && Wflag
> 0 && Gflag_count
>= Wflag
) {
1501 (void)fprintf(stderr
, "Maximum file limit reached: %d\n",
1506 if (dump_info
->CurrentFileName
!= NULL
)
1507 free(dump_info
->CurrentFileName
);
1508 /* Allocate space for max filename + \0. */
1509 dump_info
->CurrentFileName
= (char *)malloc(NAME_MAX
+ 1);
1510 if (dump_info
->CurrentFileName
== NULL
)
1511 error("dump_packet_and_trunc: malloc");
1513 * This is always the first file in the Cflag
1515 * We also don't need numbering if Cflag is not set.
1518 MakeFilename(dump_info
->CurrentFileName
, dump_info
->WFileName
, 0,
1521 MakeFilename(dump_info
->CurrentFileName
, dump_info
->WFileName
, 0, 0);
1523 dump_info
->p
= pcap_dump_open(dump_info
->pd
, dump_info
->CurrentFileName
);
1524 if (dump_info
->p
== NULL
)
1525 error("%s", pcap_geterr(pd
));
1530 * XXX - this won't prevent capture files from getting
1531 * larger than Cflag - the last packet written to the
1532 * file could put it over Cflag.
1534 if (Cflag
!= 0 && pcap_dump_ftell(dump_info
->p
) > Cflag
) {
1536 * Close the current file and open a new one.
1538 pcap_dump_close(dump_info
->p
);
1541 * Compress the file we just closed, if the user asked for it
1544 compress_savefile(dump_info
->CurrentFileName
);
1548 if (Cflag_count
>= Wflag
)
1551 if (dump_info
->CurrentFileName
!= NULL
)
1552 free(dump_info
->CurrentFileName
);
1553 dump_info
->CurrentFileName
= (char *)malloc(NAME_MAX
+ 1);
1554 if (dump_info
->CurrentFileName
== NULL
)
1555 error("dump_packet_and_trunc: malloc");
1556 MakeFilename(dump_info
->CurrentFileName
, dump_info
->WFileName
, Cflag_count
, WflagChars
);
1557 dump_info
->p
= pcap_dump_open(dump_info
->pd
, dump_info
->CurrentFileName
);
1558 if (dump_info
->p
== NULL
)
1559 error("%s", pcap_geterr(pd
));
1562 pcap_dump((u_char
*)dump_info
->p
, h
, sp
);
1563 #ifdef HAVE_PCAP_DUMP_FLUSH
1565 pcap_dump_flush(dump_info
->p
);
1574 dump_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
1580 pcap_dump(user
, h
, sp
);
1581 #ifdef HAVE_PCAP_DUMP_FLUSH
1583 pcap_dump_flush((pcap_dumper_t
*)user
);
1592 print_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
1594 struct print_info
*print_info
;
1602 print_info
= (struct print_info
*)user
;
1605 * Some printers want to check that they're not walking off the
1606 * end of the packet.
1607 * Rather than pass it all the way down, we set this global.
1609 snapend
= sp
+ h
->caplen
;
1611 if(print_info
->ndo_type
) {
1612 hdrlen
= (*print_info
->p
.ndo_printer
)(print_info
->ndo
, h
, sp
);
1614 hdrlen
= (*print_info
->p
.printer
)(h
, sp
);
1619 * Print the raw packet data in hex and ASCII.
1623 * Include the link-layer header.
1625 hex_and_ascii_print("\n\t", sp
, h
->caplen
);
1628 * Don't include the link-layer header - and if
1629 * we have nothing past the link-layer header,
1632 if (h
->caplen
> hdrlen
)
1633 hex_and_ascii_print("\n\t", sp
+ hdrlen
,
1634 h
->caplen
- hdrlen
);
1638 * Print the raw packet data in hex.
1642 * Include the link-layer header.
1644 hex_print("\n\t", sp
, h
->caplen
);
1647 * Don't include the link-layer header - and if
1648 * we have nothing past the link-layer header,
1651 if (h
->caplen
> hdrlen
)
1652 hex_print("\n\t", sp
+ hdrlen
,
1653 h
->caplen
- hdrlen
);
1657 * Print the raw packet data in ASCII.
1661 * Include the link-layer header.
1663 ascii_print(sp
, h
->caplen
);
1666 * Don't include the link-layer header - and if
1667 * we have nothing past the link-layer header,
1670 if (h
->caplen
> hdrlen
)
1671 ascii_print(sp
+ hdrlen
, h
->caplen
- hdrlen
);
1684 * XXX - there should really be libpcap calls to get the version
1685 * number as a string (the string would be generated from #defines
1686 * at run time, so that it's not generated from string constants
1687 * in the library, as, on many UNIX systems, those constants would
1688 * be statically linked into the application executable image, and
1689 * would thus reflect the version of libpcap on the system on
1690 * which the application was *linked*, not the system on which it's
1693 * That routine should be documented, unlike the "version[]"
1694 * string, so that UNIX vendors providing their own libpcaps
1695 * don't omit it (as a couple of vendors have...).
1697 * Packet.dll should perhaps also export a routine to return the
1698 * version number of the Packet.dll code, to supply the
1699 * "Wpcap_version" information on Windows.
1701 char WDversion
[]="current-cvs.tcpdump.org";
1702 #if !defined(HAVE_GENERATED_VERSION)
1703 char version
[]="current-cvs.tcpdump.org";
1705 char pcap_version
[]="current-cvs.tcpdump.org";
1706 char Wpcap_version
[]="3.1";
1710 * By default, print the specified data out in hex and ASCII.
1713 ndo_default_print(netdissect_options
*ndo _U_
, const u_char
*bp
, u_int length
)
1715 hex_and_ascii_print("\n\t", bp
, length
); /* pass on lf and identation string */
1719 default_print(const u_char
*bp
, u_int length
)
1721 ndo_default_print(gndo
, bp
, length
);
1725 RETSIGTYPE
requestinfo(int signo _U_
)
1735 * Called once each second in verbose mode while dumping to file
1737 #ifdef USE_WIN32_MM_TIMER
1738 void CALLBACK
verbose_stats_dump (UINT timer_id _U_
, UINT msg _U_
, DWORD_PTR arg _U_
,
1739 DWORD_PTR dw1 _U_
, DWORD_PTR dw2 _U_
)
1741 struct pcap_stat stat
;
1743 if (infodelay
== 0 && pcap_stats(pd
, &stat
) >= 0)
1744 fprintf(stderr
, "Got %u\r", packets_captured
);
1746 #elif defined(HAVE_ALARM)
1747 static void verbose_stats_dump(int sig _U_
)
1749 struct pcap_stat stat
;
1751 if (infodelay
== 0 && pcap_stats(pd
, &stat
) >= 0)
1752 fprintf(stderr
, "Got %u\r", packets_captured
);
1760 extern char version
[];
1761 #ifndef HAVE_PCAP_LIB_VERSION
1762 #if defined(WIN32) || defined(HAVE_PCAP_VERSION)
1763 extern char pcap_version
[];
1764 #else /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
1765 static char pcap_version
[] = "unknown";
1766 #endif /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
1767 #endif /* HAVE_PCAP_LIB_VERSION */
1769 #ifdef HAVE_PCAP_LIB_VERSION
1771 (void)fprintf(stderr
, "%s version %s, based on tcpdump version %s\n", program_name
, WDversion
, version
);
1773 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
1775 (void)fprintf(stderr
, "%s\n",pcap_lib_version());
1776 #else /* HAVE_PCAP_LIB_VERSION */
1778 (void)fprintf(stderr
, "%s version %s, based on tcpdump version %s\n", program_name
, WDversion
, version
);
1779 (void)fprintf(stderr
, "WinPcap version %s, based on libpcap version %s\n",Wpcap_version
, pcap_version
);
1781 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
1782 (void)fprintf(stderr
, "libpcap version %s\n", pcap_version
);
1784 #endif /* HAVE_PCAP_LIB_VERSION */
1785 (void)fprintf(stderr
,
1786 "Usage: %s [-aAbd" D_FLAG
"ef" I_FLAG
"KlLnNOpqRStu" U_FLAG
"vxX]" B_FLAG_USAGE
" [ -c count ]\n", program_name
);
1787 (void)fprintf(stderr
,
1788 "\t\t[ -C file_size ] [ -E algo:secret ] [ -F file ] [ -G seconds ]\n");
1789 (void)fprintf(stderr
,
1790 "\t\t[ -i interface ] [ -M secret ] [ -r file ]\n");
1791 (void)fprintf(stderr
,
1792 "\t\t[ -s snaplen ] [ -T type ] [ -w file ] [ -W filecount ]\n");
1793 (void)fprintf(stderr
,
1794 "\t\t[ -y datalinktype ] [ -z command ] [ -Z user ]\n");
1795 (void)fprintf(stderr
,
1796 "\t\t[ expression ]\n");
1804 ndo_error(netdissect_options
*ndo _U_
, const char *fmt
, ...)
1808 (void)fprintf(stderr
, "%s: ", program_name
);
1810 (void)vfprintf(stderr
, fmt
, ap
);
1814 if (fmt
[-1] != '\n')
1815 (void)fputc('\n', stderr
);
1823 ndo_warning(netdissect_options
*ndo _U_
, const char *fmt
, ...)
1827 (void)fprintf(stderr
, "%s: WARNING: ", program_name
);
1829 (void)vfprintf(stderr
, fmt
, ap
);
1833 if (fmt
[-1] != '\n')
1834 (void)fputc('\n', stderr
);