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
},
313 lookup_printer(int type
)
317 for (p
= printers
; p
->f
; ++p
)
325 static if_ndo_printer
326 lookup_ndo_printer(int type
)
328 struct ndo_printer
*p
;
330 for (p
= ndo_printers
; p
->f
; ++p
)
340 static int supports_monitor_mode
;
347 netdissect_options
*ndo
;
350 if_ndo_printer ndo_printer
;
357 char *CurrentFileName
;
363 show_dlts_and_exit(const char *device
, pcap_t
*pd
)
367 const char *dlt_name
;
369 n_dlts
= pcap_list_datalinks(pd
, &dlts
);
371 error("%s", pcap_geterr(pd
));
372 else if (n_dlts
== 0 || !dlts
)
373 error("No data link types.");
376 * If the interface is known to support monitor mode, indicate
377 * whether these are the data link types available when not in
378 * monitor mode, if -I wasn't specified, or when in monitor mode,
379 * when -I was specified (the link-layer types available in
380 * monitor mode might be different from the ones available when
381 * not in monitor mode).
383 if (supports_monitor_mode
)
384 (void) fprintf(stderr
, "Data link types for %s %s (use option -y to set):\n",
386 Iflag
? "when in monitor mode" : "when not in monitor mode");
388 (void) fprintf(stderr
, "Data link types for %s (use option -y to set):\n",
391 while (--n_dlts
>= 0) {
392 dlt_name
= pcap_datalink_val_to_name(dlts
[n_dlts
]);
393 if (dlt_name
!= NULL
) {
394 (void) fprintf(stderr
, " %s (%s)", dlt_name
,
395 pcap_datalink_val_to_description(dlts
[n_dlts
]));
398 * OK, does tcpdump handle that type?
400 if (lookup_printer(dlts
[n_dlts
]) == NULL
401 && lookup_ndo_printer(dlts
[n_dlts
]) == NULL
)
402 (void) fprintf(stderr
, " (printing not supported)");
403 fprintf(stderr
, "\n");
405 (void) fprintf(stderr
, " DLT %d (printing not supported)\n",
414 * Set up flags that might or might not be supported depending on the
415 * version of libpcap we're using.
417 #if defined(HAVE_PCAP_CREATE) || defined(WIN32)
419 #define B_FLAG_USAGE " [ -B size ]"
420 #else /* defined(HAVE_PCAP_CREATE) || defined(WIN32) */
423 #endif /* defined(HAVE_PCAP_CREATE) || defined(WIN32) */
425 #ifdef HAVE_PCAP_CREATE
427 #else /* HAVE_PCAP_CREATE */
429 #endif /* HAVE_PCAP_CREATE */
431 #ifdef HAVE_PCAP_FINDALLDEVS
432 #ifndef HAVE_PCAP_IF_T
433 #undef HAVE_PCAP_FINDALLDEVS
437 #ifdef HAVE_PCAP_FINDALLDEVS
443 #ifdef HAVE_PCAP_DUMP_FLUSH
450 /* Drop root privileges and chroot if necessary */
452 droproot(const char *username
, const char *chroot_dir
)
454 struct passwd
*pw
= NULL
;
456 if (chroot_dir
&& !username
) {
457 fprintf(stderr
, "tcpdump: Chroot without dropping root is insecure\n");
461 pw
= getpwnam(username
);
464 if (chroot(chroot_dir
) != 0 || chdir ("/") != 0) {
465 fprintf(stderr
, "tcpdump: Couldn't chroot/chdir to '%.64s': %s\n",
466 chroot_dir
, pcap_strerror(errno
));
470 if (initgroups(pw
->pw_name
, pw
->pw_gid
) != 0 ||
471 setgid(pw
->pw_gid
) != 0 || setuid(pw
->pw_uid
) != 0) {
472 fprintf(stderr
, "tcpdump: Couldn't change to '%.32s' uid=%lu gid=%lu: %s\n",
474 (unsigned long)pw
->pw_uid
,
475 (unsigned long)pw
->pw_gid
,
476 pcap_strerror(errno
));
481 fprintf(stderr
, "tcpdump: Couldn't find user '%.32s'\n",
504 MakeFilename(char *buffer
, char *orig_name
, int cnt
, int max_chars
)
506 char *filename
= malloc(NAME_MAX
+ 1);
508 /* Process with strftime if Gflag is set. */
512 /* Convert Gflag_time to a usable format */
513 if ((local_tm
= localtime(&Gflag_time
)) == NULL
) {
514 error("MakeTimedFilename: localtime");
517 /* There's no good way to detect an error in strftime since a return
518 * value of 0 isn't necessarily failure.
520 strftime(filename
, NAME_MAX
, orig_name
, local_tm
);
522 strncpy(filename
, orig_name
, NAME_MAX
);
525 if (cnt
== 0 && max_chars
== 0)
526 strncpy(buffer
, filename
, NAME_MAX
+ 1);
528 if (snprintf(buffer
, NAME_MAX
+ 1, "%s%0*d", filename
, max_chars
, cnt
) > NAME_MAX
)
529 /* Report an error if the filename is too large */
530 error("too many output files or filename is too long (> %d)", NAME_MAX
);
534 static int tcpdump_printf(netdissect_options
*ndo _U_
,
535 const char *fmt
, ...)
542 ret
=vfprintf(stdout
, fmt
, args
);
549 main(int argc
, char **argv
)
551 register int cnt
, op
, i
;
552 bpf_u_int32 localnet
, netmask
;
553 register char *cp
, *infile
, *cmdbuf
, *device
, *RFileName
, *WFileName
;
554 pcap_handler callback
;
556 struct bpf_program fcode
;
558 RETSIGTYPE (*oldhandler
)(int);
560 struct print_info printinfo
;
561 struct dump_info dumpinfo
;
562 u_char
*pcap_userdata
;
563 char ebuf
[PCAP_ERRBUF_SIZE
];
564 char *username
= NULL
;
565 char *chroot_dir
= NULL
;
566 #ifdef HAVE_PCAP_FINDALLDEVS
567 pcap_if_t
*devpointer
;
572 if(wsockinit() != 0) return 1;
578 gndo
->ndo_default_print
=ndo_default_print
;
579 gndo
->ndo_printf
=tcpdump_printf
;
580 gndo
->ndo_error
=ndo_error
;
581 gndo
->ndo_warning
=ndo_warning
;
582 gndo
->ndo_snaplen
= DEFAULT_SNAPLEN
;
589 if ((cp
= strrchr(argv
[0], '/')) != NULL
)
590 program_name
= cp
+ 1;
592 program_name
= argv
[0];
594 if (abort_on_misalignment(ebuf
, sizeof(ebuf
)) < 0)
603 (op
= getopt(argc
, argv
, "aAb" B_FLAG
"c:C:d" D_FLAG
"eE:fF:G:hi:" I_FLAG
"KlLm:M:nNOpqr:Rs:StT:u" U_FLAG
"vw:W:xXy:Yz:Z:")) != -1)
607 /* compatibility for old -a */
618 #if defined(HAVE_PCAP_CREATE) || defined(WIN32)
620 Bflag
= atoi(optarg
)*1024;
622 error("invalid packet buffer size %s", optarg
);
624 #endif /* defined(HAVE_PCAP_CREATE) || defined(WIN32) */
629 error("invalid packet count %s", optarg
);
633 Cflag
= atoi(optarg
) * 1000000;
635 error("invalid file size %s", optarg
);
642 #ifdef HAVE_PCAP_FINDALLDEVS
644 if (pcap_findalldevs(&devpointer
, ebuf
) < 0)
647 for (i
= 0; devpointer
!= 0; i
++) {
648 printf("%d.%s", i
+1, devpointer
->name
);
649 if (devpointer
->description
!= NULL
)
650 printf(" (%s)", devpointer
->description
);
652 devpointer
= devpointer
->next
;
656 #endif /* HAVE_PCAP_FINDALLDEVS */
667 #ifndef HAVE_LIBCRYPTO
668 warning("crypto code not compiled in");
670 gndo
->ndo_espsecret
= optarg
;
682 Gflag
= atoi(optarg
);
684 error("invalid number of seconds %s", optarg
);
686 /* We will create one file initially. */
689 /* Grab the current time for rotation use. */
690 if ((Gflag_time
= time(NULL
)) == (time_t)-1) {
691 error("main: can't get current time: %s",
692 pcap_strerror(errno
));
701 if (optarg
[0] == '0' && optarg
[1] == 0)
702 error("Invalid adapter index");
704 #ifdef HAVE_PCAP_FINDALLDEVS
706 * If the argument is a number, treat it as
707 * an index into the list of adapters, as
708 * printed by "tcpdump -D".
710 * This should be OK on UNIX systems, as interfaces
711 * shouldn't have names that begin with digits.
712 * It can be useful on Windows, where more than
713 * one interface can have the same name.
715 if ((devnum
= atoi(optarg
)) != 0) {
717 error("Invalid adapter index");
719 if (pcap_findalldevs(&devpointer
, ebuf
) < 0)
723 * Look for the devnum-th entry
724 * in the list of devices
728 i
< devnum
-1 && devpointer
!= NULL
;
729 i
++, devpointer
= devpointer
->next
)
731 if (devpointer
== NULL
)
732 error("Invalid adapter index");
734 device
= devpointer
->name
;
737 #endif /* HAVE_PCAP_FINDALLDEVS */
741 #ifdef HAVE_PCAP_CREATE
745 #endif /* HAVE_PCAP_CREATE */
750 * _IOLBF is the same as _IOFBF in Microsoft's C
751 * libraries; the only alternative they offer
754 * XXX - this should really be checking for MSVC++,
755 * not WIN32, if, for example, MinGW has its own
756 * C library that is more UNIX-compatible.
758 setvbuf(stdout
, NULL
, _IONBF
, 0);
760 #ifdef HAVE_SETLINEBUF
763 setvbuf(stdout
, NULL
, _IOLBF
, 0);
774 if (smiLoadModule(optarg
) == 0) {
775 error("could not load MIB module %s", optarg
);
779 (void)fprintf(stderr
, "%s: ignoring option `-m %s' ",
780 program_name
, optarg
);
781 (void)fprintf(stderr
, "(no libsmi support)\n");
786 /* TCP-MD5 shared secret */
787 #ifndef HAVE_LIBCRYPTO
788 warning("crypto code not compiled in");
811 ++suppress_default_print
;
825 snaplen
= strtol(optarg
, &end
, 0);
826 if (optarg
== end
|| *end
!= '\0'
827 || snaplen
< 0 || snaplen
> MAXIMUM_SNAPLEN
)
828 error("invalid snaplen %s", optarg
);
829 else if (snaplen
== 0)
830 snaplen
= MAXIMUM_SNAPLEN
;
843 if (strcasecmp(optarg
, "vat") == 0)
845 else if (strcasecmp(optarg
, "wb") == 0)
847 else if (strcasecmp(optarg
, "rpc") == 0)
849 else if (strcasecmp(optarg
, "rtp") == 0)
851 else if (strcasecmp(optarg
, "rtcp") == 0)
852 packettype
= PT_RTCP
;
853 else if (strcasecmp(optarg
, "snmp") == 0)
854 packettype
= PT_SNMP
;
855 else if (strcasecmp(optarg
, "cnfp") == 0)
856 packettype
= PT_CNFP
;
857 else if (strcasecmp(optarg
, "tftp") == 0)
858 packettype
= PT_TFTP
;
859 else if (strcasecmp(optarg
, "aodv") == 0)
860 packettype
= PT_AODV
;
862 error("unknown packet type `%s'", optarg
);
869 #ifdef HAVE_PCAP_DUMP_FLUSH
884 Wflag
= atoi(optarg
);
886 error("invalid number of output files %s", optarg
);
887 WflagChars
= getWflagChars(Wflag
);
892 ++suppress_default_print
;
897 ++suppress_default_print
;
901 gndo
->ndo_dltname
= optarg
;
903 pcap_datalink_name_to_val(gndo
->ndo_dltname
);
904 if (gndo
->ndo_dlt
< 0)
905 error("invalid data link type %s", gndo
->ndo_dltname
);
908 #if defined(HAVE_PCAP_DEBUG) || defined(HAVE_YYDEBUG)
911 /* Undocumented flag */
912 #ifdef HAVE_PCAP_DEBUG
913 extern int pcap_debug
;
924 zflag
= strdup(optarg
);
933 username
= strdup(optarg
);
948 case 0: /* Default */
949 case 4: /* Default + Date*/
950 thiszone
= gmt2local(0);
953 case 1: /* No time stamp */
954 case 2: /* Unix timeval style */
955 case 3: /* Microseconds since previous packet */
956 case 5: /* Microseconds since first packet */
959 default: /* Not supported */
960 error("only -t, -tt, -ttt, -tttt and -ttttt are supported");
965 /* if run as root, prepare for chrooting */
966 if (getuid() == 0 || geteuid() == 0) {
967 /* future extensibility for cmd-line arguments */
969 chroot_dir
= WITH_CHROOT
;
974 /* if run as root, prepare for dropping root privileges */
975 if (getuid() == 0 || geteuid() == 0) {
976 /* Run with '-Z root' to restore old behaviour */
978 username
= WITH_USER
;
982 if (RFileName
!= NULL
) {
984 const char *dlt_name
;
988 * We don't need network access, so relinquish any set-UID
989 * or set-GID privileges we have (if any).
991 * We do *not* want set-UID privileges when opening a
992 * trace file, as that might let the user read other
993 * people's trace files (especially if we're set-UID
996 if (setgid(getgid()) != 0 || setuid(getuid()) != 0 )
997 fprintf(stderr
, "Warning: setgid/setuid failed !\n");
999 pd
= pcap_open_offline(RFileName
, ebuf
);
1002 dlt
= pcap_datalink(pd
);
1003 dlt_name
= pcap_datalink_val_to_name(dlt
);
1004 if (dlt_name
== NULL
) {
1005 fprintf(stderr
, "reading from file %s, link-type %u\n",
1009 "reading from file %s, link-type %s (%s)\n",
1010 RFileName
, dlt_name
,
1011 pcap_datalink_val_to_description(dlt
));
1016 error("-f and -r options are incompatible");
1018 if (device
== NULL
) {
1019 device
= pcap_lookupdev(ebuf
);
1024 if(strlen(device
) == 1) //we assume that an ASCII string is always longer than 1 char
1025 { //a Unicode string has a \0 as second byte (so strlen() is 1)
1026 fprintf(stderr
, "%s: listening on %ws\n", program_name
, device
);
1030 fprintf(stderr
, "%s: listening on %s\n", program_name
, device
);
1035 #ifdef HAVE_PCAP_CREATE
1036 pd
= pcap_create(device
, ebuf
);
1040 * Is this an interface that supports monitor mode?
1042 if (pcap_can_set_rfmon(pd
) == 1)
1043 supports_monitor_mode
= 1;
1045 supports_monitor_mode
= 0;
1046 status
= pcap_set_snaplen(pd
, snaplen
);
1048 error("%s: pcap_set_snaplen failed: %s",
1049 device
, pcap_statustostr(status
));
1050 status
= pcap_set_promisc(pd
, !pflag
);
1052 error("%s: pcap_set_promisc failed: %s",
1053 device
, pcap_statustostr(status
));
1055 status
= pcap_set_rfmon(pd
, 1);
1057 error("%s: pcap_set_rfmon failed: %s",
1058 device
, pcap_statustostr(status
));
1060 status
= pcap_set_timeout(pd
, 1000);
1062 error("%s: pcap_set_timeout failed: %s",
1063 device
, pcap_statustostr(status
));
1065 status
= pcap_set_buffer_size(pd
, Bflag
);
1067 error("%s: pcap_set_buffer_size failed: %s",
1068 device
, pcap_statustostr(status
));
1070 status
= pcap_activate(pd
);
1073 * pcap_activate() failed.
1075 cp
= pcap_geterr(pd
);
1076 if (status
== PCAP_ERROR
)
1078 else if ((status
== PCAP_ERROR_NO_SUCH_DEVICE
||
1079 status
== PCAP_ERROR_PERM_DENIED
) &&
1081 error("%s: %s\n(%s)", device
,
1082 pcap_statustostr(status
), cp
);
1084 error("%s: %s", device
,
1085 pcap_statustostr(status
));
1086 } else if (status
> 0) {
1088 * pcap_activate() succeeded, but it's warning us
1089 * of a problem it had.
1091 cp
= pcap_geterr(pd
);
1092 if (status
== PCAP_WARNING
)
1094 else if (status
== PCAP_WARNING_PROMISC_NOTSUP
&&
1096 warning("%s: %s\n(%s)", device
,
1097 pcap_statustostr(status
), cp
);
1099 warning("%s: %s", device
,
1100 pcap_statustostr(status
));
1104 pd
= pcap_open_live(device
, snaplen
, !pflag
, 1000, ebuf
);
1108 warning("%s", ebuf
);
1109 #endif /* HAVE_PCAP_CREATE */
1111 * Let user own process after socket has been opened.
1114 if (setgid(getgid()) != 0 || setuid(getuid()) != 0)
1115 fprintf(stderr
, "Warning: setgid/setuid failed !\n");
1117 #if !defined(HAVE_PCAP_CREATE) && defined(WIN32)
1119 if(pcap_setbuff(pd
, Bflag
)==-1){
1120 error("%s", pcap_geterr(pd
));
1122 #endif /* !defined(HAVE_PCAP_CREATE) && defined(WIN32) */
1124 show_dlts_and_exit(device
, pd
);
1125 if (gndo
->ndo_dlt
>= 0) {
1126 #ifdef HAVE_PCAP_SET_DATALINK
1127 if (pcap_set_datalink(pd
, gndo
->ndo_dlt
) < 0)
1128 error("%s", pcap_geterr(pd
));
1131 * We don't actually support changing the
1132 * data link type, so we only let them
1133 * set it to what it already is.
1135 if (gndo
->ndo_dlt
!= pcap_datalink(pd
)) {
1136 error("%s is not one of the DLTs supported by this device\n",
1140 (void)fprintf(stderr
, "%s: data link type %s\n",
1141 program_name
, gndo
->ndo_dltname
);
1142 (void)fflush(stderr
);
1144 i
= pcap_snapshot(pd
);
1146 warning("snaplen raised from %d to %d", snaplen
, i
);
1149 if (pcap_lookupnet(device
, &localnet
, &netmask
, ebuf
) < 0) {
1152 warning("%s", ebuf
);
1156 cmdbuf
= read_infile(infile
);
1158 cmdbuf
= copy_argv(&argv
[optind
]);
1160 if (pcap_compile(pd
, &fcode
, cmdbuf
, Oflag
, netmask
) < 0)
1161 error("%s", pcap_geterr(pd
));
1164 bpf_dump(&fcode
, dflag
);
1168 init_addrtoname(localnet
, netmask
);
1172 (void)setsignal(SIGPIPE
, cleanup
);
1173 (void)setsignal(SIGTERM
, cleanup
);
1174 (void)setsignal(SIGINT
, cleanup
);
1175 (void)setsignal(SIGCHLD
, child_cleanup
);
1177 /* Cooperate with nohup(1) */
1179 if ((oldhandler
= setsignal(SIGHUP
, cleanup
)) != SIG_DFL
)
1180 (void)setsignal(SIGHUP
, oldhandler
);
1183 if (pcap_setfilter(pd
, &fcode
) < 0)
1184 error("%s", pcap_geterr(pd
));
1187 /* Do not exceed the default NAME_MAX for files. */
1188 dumpinfo
.CurrentFileName
= (char *)malloc(NAME_MAX
+ 1);
1190 if (dumpinfo
.CurrentFileName
== NULL
)
1191 error("malloc of dumpinfo.CurrentFileName");
1193 /* We do not need numbering for dumpfiles if Cflag isn't set. */
1195 MakeFilename(dumpinfo
.CurrentFileName
, WFileName
, 0, WflagChars
);
1197 MakeFilename(dumpinfo
.CurrentFileName
, WFileName
, 0, 0);
1199 p
= pcap_dump_open(pd
, dumpinfo
.CurrentFileName
);
1201 error("%s", pcap_geterr(pd
));
1202 if (Cflag
!= 0 || Gflag
!= 0) {
1203 callback
= dump_packet_and_trunc
;
1204 dumpinfo
.WFileName
= WFileName
;
1207 pcap_userdata
= (u_char
*)&dumpinfo
;
1209 callback
= dump_packet
;
1210 pcap_userdata
= (u_char
*)p
;
1213 type
= pcap_datalink(pd
);
1214 printinfo
.ndo_type
= 1;
1215 printinfo
.ndo
= gndo
;
1216 printinfo
.p
.ndo_printer
= lookup_ndo_printer(type
);
1217 if (printinfo
.p
.ndo_printer
== NULL
) {
1218 printinfo
.p
.printer
= lookup_printer(type
);
1219 printinfo
.ndo_type
= 0;
1220 if (printinfo
.p
.printer
== NULL
) {
1221 gndo
->ndo_dltname
= pcap_datalink_val_to_name(type
);
1222 if (gndo
->ndo_dltname
!= NULL
)
1223 error("packet printing is not supported for link type %s: use -w",
1226 error("packet printing is not supported for link type %d: use -w", type
);
1229 callback
= print_packet
;
1230 pcap_userdata
= (u_char
*)&printinfo
;
1234 * We cannot do this earlier, because we want to be able to open
1235 * the file (if done) for writing before giving up permissions.
1237 if (getuid() == 0 || geteuid() == 0) {
1238 if (username
|| chroot_dir
)
1239 droproot(username
, chroot_dir
);
1244 * We can't get statistics when reading from a file rather
1245 * than capturing from a device.
1247 if (RFileName
== NULL
)
1248 (void)setsignal(SIGINFO
, requestinfo
);
1251 if (vflag
> 0 && WFileName
) {
1253 * When capturing to a file, "-v" means tcpdump should,
1254 * every 10 secodns, "v"erbosely report the number of
1257 #ifdef USE_WIN32_MM_TIMER
1258 /* call verbose_stats_dump() each 1000 +/-100msec */
1259 timer_id
= timeSetEvent(1000, 100, verbose_stats_dump
, 0, TIME_PERIODIC
);
1260 setvbuf(stderr
, NULL
, _IONBF
, 0);
1261 #elif defined(HAVE_ALARM)
1262 (void)setsignal(SIGALRM
, verbose_stats_dump
);
1268 if (RFileName
== NULL
) {
1270 const char *dlt_name
;
1272 if (!vflag
&& !WFileName
) {
1273 (void)fprintf(stderr
,
1274 "%s: verbose output suppressed, use -v or -vv for full protocol decode\n",
1277 (void)fprintf(stderr
, "%s: ", program_name
);
1278 dlt
= pcap_datalink(pd
);
1279 dlt_name
= pcap_datalink_val_to_name(dlt
);
1280 if (dlt_name
== NULL
) {
1281 (void)fprintf(stderr
, "listening on %s, link-type %u, capture size %u bytes\n",
1282 device
, dlt
, snaplen
);
1284 (void)fprintf(stderr
, "listening on %s, link-type %s (%s), capture size %u bytes\n",
1286 pcap_datalink_val_to_description(dlt
), snaplen
);
1288 (void)fflush(stderr
);
1291 status
= pcap_loop(pd
, cnt
, callback
, pcap_userdata
);
1292 if (WFileName
== NULL
) {
1294 * We're printing packets. Flush the printed output,
1295 * so it doesn't get intermingled with error output.
1299 * We got interrupted, so perhaps we didn't
1300 * manage to finish a line we were printing.
1301 * Print an extra newline, just in case.
1305 (void)fflush(stdout
);
1311 (void)fprintf(stderr
, "%s: pcap_loop: %s\n",
1312 program_name
, pcap_geterr(pd
));
1314 if (RFileName
== NULL
) {
1316 * We're doing a live capture. Report the capture
1322 exit(status
== -1 ? 1 : 0);
1325 /* make a clean exit on interrupts */
1327 cleanup(int signo _U_
)
1329 #ifdef USE_WIN32_MM_TIMER
1331 timeKillEvent(timer_id
);
1333 #elif defined(HAVE_ALARM)
1337 #ifdef HAVE_PCAP_BREAKLOOP
1339 * We have "pcap_breakloop()"; use it, so that we do as little
1340 * as possible in the signal handler (it's probably not safe
1341 * to do anything with standard I/O streams in a signal handler -
1342 * the ANSI C standard doesn't say it is).
1347 * We don't have "pcap_breakloop()"; this isn't safe, but
1348 * it's the best we can do. Print the summary if we're
1349 * not reading from a savefile - i.e., if we're doing a
1350 * live capture - and exit.
1352 if (pd
!= NULL
&& pcap_file(pd
) == NULL
) {
1354 * We got interrupted, so perhaps we didn't
1355 * manage to finish a line we were printing.
1356 * Print an extra newline, just in case.
1359 (void)fflush(stdout
);
1367 On windows, we do not use a fork, so we do not care less about
1368 waiting a child processes to die
1372 child_cleanup(int signo _U_
)
1379 info(register int verbose
)
1381 struct pcap_stat stat
;
1384 * Older versions of libpcap didn't set ps_ifdrop on some
1385 * platforms; initialize it to 0 to handle that.
1388 if (pcap_stats(pd
, &stat
) < 0) {
1389 (void)fprintf(stderr
, "pcap_stats: %s\n", pcap_geterr(pd
));
1395 fprintf(stderr
, "%s: ", program_name
);
1397 (void)fprintf(stderr
, "%u packets captured", packets_captured
);
1399 fputs(", ", stderr
);
1402 (void)fprintf(stderr
, "%u packets received by filter", stat
.ps_recv
);
1404 fputs(", ", stderr
);
1407 (void)fprintf(stderr
, "%u packets dropped by kernel", stat
.ps_drop
);
1408 if (stat
.ps_ifdrop
!= 0) {
1410 fputs(", ", stderr
);
1413 (void)fprintf(stderr
, "%u packets dropped by interface\n",
1422 compress_savefile(const char *filename
)
1427 * Set to lowest priority so that this doesn't disturb the capture
1430 setpriority(PRIO_PROCESS
, 0, NZERO
- 1);
1432 setpriority(PRIO_PROCESS
, 0, 19);
1434 if (execlp(zflag
, zflag
, filename
, (char *)NULL
) == -1)
1436 "compress_savefile:execlp(%s, %s): %s\n",
1443 compress_savefile(const char *filename
)
1446 "compress_savefile failed. Functionality not implemented under windows\n");
1451 dump_packet_and_trunc(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
1453 struct dump_info
*dump_info
;
1459 dump_info
= (struct dump_info
*)user
;
1462 * XXX - this won't force the file to rotate on the specified time
1463 * boundary, but it will rotate on the first packet received after the
1464 * specified Gflag number of seconds. Note: if a Gflag time boundary
1465 * and a Cflag size boundary coincide, the time rotation will occur
1466 * first thereby cancelling the Cflag boundary (since the file should
1470 /* Check if it is time to rotate */
1473 /* Get the current time */
1474 if ((t
= time(NULL
)) == (time_t)-1) {
1475 error("dump_and_trunc_packet: can't get current_time: %s",
1476 pcap_strerror(errno
));
1480 /* If the time is greater than the specified window, rotate */
1481 if (t
- Gflag_time
>= Gflag
) {
1482 /* Update the Gflag_time */
1484 /* Update Gflag_count */
1487 * Close the current file and open a new one.
1489 pcap_dump_close(dump_info
->p
);
1492 * Compress the file we just closed, if the user asked for it
1495 compress_savefile(dump_info
->CurrentFileName
);
1498 * Check to see if we've exceeded the Wflag (when
1501 if (Cflag
== 0 && Wflag
> 0 && Gflag_count
>= Wflag
) {
1502 (void)fprintf(stderr
, "Maximum file limit reached: %d\n",
1507 if (dump_info
->CurrentFileName
!= NULL
)
1508 free(dump_info
->CurrentFileName
);
1509 /* Allocate space for max filename + \0. */
1510 dump_info
->CurrentFileName
= (char *)malloc(NAME_MAX
+ 1);
1511 if (dump_info
->CurrentFileName
== NULL
)
1512 error("dump_packet_and_trunc: malloc");
1514 * This is always the first file in the Cflag
1516 * We also don't need numbering if Cflag is not set.
1519 MakeFilename(dump_info
->CurrentFileName
, dump_info
->WFileName
, 0,
1522 MakeFilename(dump_info
->CurrentFileName
, dump_info
->WFileName
, 0, 0);
1524 dump_info
->p
= pcap_dump_open(dump_info
->pd
, dump_info
->CurrentFileName
);
1525 if (dump_info
->p
== NULL
)
1526 error("%s", pcap_geterr(pd
));
1531 * XXX - this won't prevent capture files from getting
1532 * larger than Cflag - the last packet written to the
1533 * file could put it over Cflag.
1535 if (Cflag
!= 0 && pcap_dump_ftell(dump_info
->p
) > Cflag
) {
1537 * Close the current file and open a new one.
1539 pcap_dump_close(dump_info
->p
);
1542 * Compress the file we just closed, if the user asked for it
1545 compress_savefile(dump_info
->CurrentFileName
);
1549 if (Cflag_count
>= Wflag
)
1552 if (dump_info
->CurrentFileName
!= NULL
)
1553 free(dump_info
->CurrentFileName
);
1554 dump_info
->CurrentFileName
= (char *)malloc(NAME_MAX
+ 1);
1555 if (dump_info
->CurrentFileName
== NULL
)
1556 error("dump_packet_and_trunc: malloc");
1557 MakeFilename(dump_info
->CurrentFileName
, dump_info
->WFileName
, Cflag_count
, WflagChars
);
1558 dump_info
->p
= pcap_dump_open(dump_info
->pd
, dump_info
->CurrentFileName
);
1559 if (dump_info
->p
== NULL
)
1560 error("%s", pcap_geterr(pd
));
1563 pcap_dump((u_char
*)dump_info
->p
, h
, sp
);
1564 #ifdef HAVE_PCAP_DUMP_FLUSH
1566 pcap_dump_flush(dump_info
->p
);
1575 dump_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
1581 pcap_dump(user
, h
, sp
);
1582 #ifdef HAVE_PCAP_DUMP_FLUSH
1584 pcap_dump_flush((pcap_dumper_t
*)user
);
1593 print_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
1595 struct print_info
*print_info
;
1603 print_info
= (struct print_info
*)user
;
1606 * Some printers want to check that they're not walking off the
1607 * end of the packet.
1608 * Rather than pass it all the way down, we set this global.
1610 snapend
= sp
+ h
->caplen
;
1612 if(print_info
->ndo_type
) {
1613 hdrlen
= (*print_info
->p
.ndo_printer
)(print_info
->ndo
, h
, sp
);
1615 hdrlen
= (*print_info
->p
.printer
)(h
, sp
);
1620 * Print the raw packet data in hex and ASCII.
1624 * Include the link-layer header.
1626 hex_and_ascii_print("\n\t", sp
, h
->caplen
);
1629 * Don't include the link-layer header - and if
1630 * we have nothing past the link-layer header,
1633 if (h
->caplen
> hdrlen
)
1634 hex_and_ascii_print("\n\t", sp
+ hdrlen
,
1635 h
->caplen
- hdrlen
);
1639 * Print the raw packet data in hex.
1643 * Include the link-layer header.
1645 hex_print("\n\t", sp
, h
->caplen
);
1648 * Don't include the link-layer header - and if
1649 * we have nothing past the link-layer header,
1652 if (h
->caplen
> hdrlen
)
1653 hex_print("\n\t", sp
+ hdrlen
,
1654 h
->caplen
- hdrlen
);
1658 * Print the raw packet data in ASCII.
1662 * Include the link-layer header.
1664 ascii_print(sp
, h
->caplen
);
1667 * Don't include the link-layer header - and if
1668 * we have nothing past the link-layer header,
1671 if (h
->caplen
> hdrlen
)
1672 ascii_print(sp
+ hdrlen
, h
->caplen
- hdrlen
);
1685 * XXX - there should really be libpcap calls to get the version
1686 * number as a string (the string would be generated from #defines
1687 * at run time, so that it's not generated from string constants
1688 * in the library, as, on many UNIX systems, those constants would
1689 * be statically linked into the application executable image, and
1690 * would thus reflect the version of libpcap on the system on
1691 * which the application was *linked*, not the system on which it's
1694 * That routine should be documented, unlike the "version[]"
1695 * string, so that UNIX vendors providing their own libpcaps
1696 * don't omit it (as a couple of vendors have...).
1698 * Packet.dll should perhaps also export a routine to return the
1699 * version number of the Packet.dll code, to supply the
1700 * "Wpcap_version" information on Windows.
1702 char WDversion
[]="current-cvs.tcpdump.org";
1703 #if !defined(HAVE_GENERATED_VERSION)
1704 char version
[]="current-cvs.tcpdump.org";
1706 char pcap_version
[]="current-cvs.tcpdump.org";
1707 char Wpcap_version
[]="3.1";
1711 * By default, print the specified data out in hex and ASCII.
1714 ndo_default_print(netdissect_options
*ndo _U_
, const u_char
*bp
, u_int length
)
1716 hex_and_ascii_print("\n\t", bp
, length
); /* pass on lf and identation string */
1720 default_print(const u_char
*bp
, u_int length
)
1722 ndo_default_print(gndo
, bp
, length
);
1726 RETSIGTYPE
requestinfo(int signo _U_
)
1736 * Called once each second in verbose mode while dumping to file
1738 #ifdef USE_WIN32_MM_TIMER
1739 void CALLBACK
verbose_stats_dump (UINT timer_id _U_
, UINT msg _U_
, DWORD_PTR arg _U_
,
1740 DWORD_PTR dw1 _U_
, DWORD_PTR dw2 _U_
)
1742 struct pcap_stat stat
;
1744 if (infodelay
== 0 && pcap_stats(pd
, &stat
) >= 0)
1745 fprintf(stderr
, "Got %u\r", packets_captured
);
1747 #elif defined(HAVE_ALARM)
1748 static void verbose_stats_dump(int sig _U_
)
1750 struct pcap_stat stat
;
1752 if (infodelay
== 0 && pcap_stats(pd
, &stat
) >= 0)
1753 fprintf(stderr
, "Got %u\r", packets_captured
);
1761 extern char version
[];
1762 #ifndef HAVE_PCAP_LIB_VERSION
1763 #if defined(WIN32) || defined(HAVE_PCAP_VERSION)
1764 extern char pcap_version
[];
1765 #else /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
1766 static char pcap_version
[] = "unknown";
1767 #endif /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
1768 #endif /* HAVE_PCAP_LIB_VERSION */
1770 #ifdef HAVE_PCAP_LIB_VERSION
1772 (void)fprintf(stderr
, "%s version %s, based on tcpdump version %s\n", program_name
, WDversion
, version
);
1774 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
1776 (void)fprintf(stderr
, "%s\n",pcap_lib_version());
1777 #else /* HAVE_PCAP_LIB_VERSION */
1779 (void)fprintf(stderr
, "%s version %s, based on tcpdump version %s\n", program_name
, WDversion
, version
);
1780 (void)fprintf(stderr
, "WinPcap version %s, based on libpcap version %s\n",Wpcap_version
, pcap_version
);
1782 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
1783 (void)fprintf(stderr
, "libpcap version %s\n", pcap_version
);
1785 #endif /* HAVE_PCAP_LIB_VERSION */
1786 (void)fprintf(stderr
,
1787 "Usage: %s [-aAbd" D_FLAG
"ef" I_FLAG
"KlLnNOpqRStu" U_FLAG
"vxX]" B_FLAG_USAGE
" [ -c count ]\n", program_name
);
1788 (void)fprintf(stderr
,
1789 "\t\t[ -C file_size ] [ -E algo:secret ] [ -F file ] [ -G seconds ]\n");
1790 (void)fprintf(stderr
,
1791 "\t\t[ -i interface ] [ -M secret ] [ -r file ]\n");
1792 (void)fprintf(stderr
,
1793 "\t\t[ -s snaplen ] [ -T type ] [ -w file ] [ -W filecount ]\n");
1794 (void)fprintf(stderr
,
1795 "\t\t[ -y datalinktype ] [ -z command ] [ -Z user ]\n");
1796 (void)fprintf(stderr
,
1797 "\t\t[ expression ]\n");
1805 ndo_error(netdissect_options
*ndo _U_
, const char *fmt
, ...)
1809 (void)fprintf(stderr
, "%s: ", program_name
);
1811 (void)vfprintf(stderr
, fmt
, ap
);
1815 if (fmt
[-1] != '\n')
1816 (void)fputc('\n', stderr
);
1824 ndo_warning(netdissect_options
*ndo _U_
, const char *fmt
, ...)
1828 (void)fprintf(stderr
, "%s: WARNING: ", program_name
);
1830 (void)vfprintf(stderr
, fmt
, ap
);
1834 if (fmt
[-1] != '\n')
1835 (void)fputc('\n', stderr
);