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 int dflag
; /* print filter code */
94 int Lflag
; /* list available data link types and exit */
95 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(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
*);
140 static struct printer printers
[] = {
141 { arcnet_if_print
, DLT_ARCNET
},
142 #ifdef DLT_ARCNET_LINUX
143 { arcnet_linux_if_print
, DLT_ARCNET_LINUX
},
145 { ether_if_print
, DLT_EN10MB
},
146 { token_if_print
, DLT_IEEE802
},
148 { lane_if_print
, DLT_LANE8023
},
151 { cip_if_print
, DLT_CIP
},
154 { cip_if_print
, DLT_ATM_CLIP
},
156 { sl_if_print
, DLT_SLIP
},
157 #ifdef DLT_SLIP_BSDOS
158 { sl_bsdos_if_print
, DLT_SLIP_BSDOS
},
160 { ppp_if_print
, DLT_PPP
},
161 #ifdef DLT_PPP_WITHDIRECTION
162 { ppp_if_print
, DLT_PPP_WITHDIRECTION
},
165 { ppp_bsdos_if_print
, DLT_PPP_BSDOS
},
167 { fddi_if_print
, DLT_FDDI
},
168 { null_if_print
, DLT_NULL
},
170 { null_if_print
, DLT_LOOP
},
172 { raw_if_print
, DLT_RAW
},
173 { atm_if_print
, DLT_ATM_RFC1483
},
175 { chdlc_if_print
, DLT_C_HDLC
},
178 { chdlc_if_print
, DLT_HDLC
},
180 #ifdef DLT_PPP_SERIAL
181 { ppp_hdlc_if_print
, DLT_PPP_SERIAL
},
184 { pppoe_if_print
, DLT_PPP_ETHER
},
187 { sll_if_print
, DLT_LINUX_SLL
},
189 #ifdef DLT_IEEE802_11
190 { ieee802_11_if_print
, DLT_IEEE802_11
},
193 { ltalk_if_print
, DLT_LTALK
},
195 #if defined(DLT_PFLOG) && defined(HAVE_NET_PFVAR_H)
196 { pflog_if_print
, DLT_PFLOG
},
199 { fr_if_print
, DLT_FR
},
202 { fr_if_print
, DLT_FRELAY
},
205 { sunatm_if_print
, DLT_SUNATM
},
207 #ifdef DLT_IP_OVER_FC
208 { ipfc_if_print
, DLT_IP_OVER_FC
},
210 #ifdef DLT_PRISM_HEADER
211 { prism_if_print
, DLT_PRISM_HEADER
},
213 #ifdef DLT_IEEE802_11_RADIO
214 { ieee802_11_radio_if_print
, DLT_IEEE802_11_RADIO
},
217 { enc_if_print
, DLT_ENC
},
219 #ifdef DLT_SYMANTEC_FIREWALL
220 { symantec_if_print
, DLT_SYMANTEC_FIREWALL
},
222 #ifdef DLT_APPLE_IP_OVER_IEEE1394
223 { ap1394_if_print
, DLT_APPLE_IP_OVER_IEEE1394
},
225 #ifdef DLT_IEEE802_11_RADIO_AVS
226 { ieee802_11_radio_avs_if_print
, DLT_IEEE802_11_RADIO_AVS
},
228 #ifdef DLT_JUNIPER_ATM1
229 { juniper_atm1_print
, DLT_JUNIPER_ATM1
},
231 #ifdef DLT_JUNIPER_ATM2
232 { juniper_atm2_print
, DLT_JUNIPER_ATM2
},
234 #ifdef DLT_JUNIPER_MFR
235 { juniper_mfr_print
, DLT_JUNIPER_MFR
},
237 #ifdef DLT_JUNIPER_MLFR
238 { juniper_mlfr_print
, DLT_JUNIPER_MLFR
},
240 #ifdef DLT_JUNIPER_MLPPP
241 { juniper_mlppp_print
, DLT_JUNIPER_MLPPP
},
243 #ifdef DLT_JUNIPER_PPPOE
244 { juniper_pppoe_print
, DLT_JUNIPER_PPPOE
},
246 #ifdef DLT_JUNIPER_PPPOE_ATM
247 { juniper_pppoe_atm_print
, DLT_JUNIPER_PPPOE_ATM
},
249 #ifdef DLT_JUNIPER_GGSN
250 { juniper_ggsn_print
, DLT_JUNIPER_GGSN
},
252 #ifdef DLT_JUNIPER_ES
253 { juniper_es_print
, DLT_JUNIPER_ES
},
255 #ifdef DLT_JUNIPER_MONITOR
256 { juniper_monitor_print
, DLT_JUNIPER_MONITOR
},
258 #ifdef DLT_JUNIPER_SERVICES
259 { juniper_services_print
, DLT_JUNIPER_SERVICES
},
261 #ifdef DLT_JUNIPER_ETHER
262 { juniper_ether_print
, DLT_JUNIPER_ETHER
},
264 #ifdef DLT_JUNIPER_PPP
265 { juniper_ppp_print
, DLT_JUNIPER_PPP
},
267 #ifdef DLT_JUNIPER_FRELAY
268 { juniper_frelay_print
, DLT_JUNIPER_FRELAY
},
270 #ifdef DLT_JUNIPER_CHDLC
271 { juniper_chdlc_print
, DLT_JUNIPER_CHDLC
},
274 { mfr_if_print
, DLT_MFR
},
276 #if defined(DLT_BLUETOOTH_HCI_H4_WITH_PHDR) && defined(HAVE_PCAP_BLUETOOTH_H)
277 { bt_if_print
, DLT_BLUETOOTH_HCI_H4_WITH_PHDR
},
279 #if defined(HAVE_PCAP_USB_H) && defined(DLT_USB_LINUX)
280 { usb_linux_print
, DLT_USB_LINUX
},
282 #if defined(HAVE_PCAP_USB_H) && defined(DLT_USB_LINUX_MMAPPED)
283 { usb_linux_print
, DLT_USB_LINUX_MMAPPED
},
289 lookup_printer(int type
)
293 for (p
= printers
; p
->f
; ++p
)
313 char *CurrentFileName
;
319 show_dlts_and_exit(pcap_t
*pd
)
323 const char *dlt_name
;
325 n_dlts
= pcap_list_datalinks(pd
, &dlts
);
327 error("%s", pcap_geterr(pd
));
328 else if (n_dlts
== 0 || !dlts
)
329 error("No data link types.");
331 (void) fprintf(stderr
, "Data link types (use option -y to set):\n");
333 while (--n_dlts
>= 0) {
334 dlt_name
= pcap_datalink_val_to_name(dlts
[n_dlts
]);
335 if (dlt_name
!= NULL
) {
336 (void) fprintf(stderr
, " %s (%s)", dlt_name
,
337 pcap_datalink_val_to_description(dlts
[n_dlts
]));
340 * OK, does tcpdump handle that type?
342 if (lookup_printer(dlts
[n_dlts
]) == NULL
)
343 (void) fprintf(stderr
, " (printing not supported)");
346 (void) fprintf(stderr
, " DLT %d (printing not supported)\n",
355 * Set up flags that might or might not be supported depending on the
356 * version of libpcap we're using.
358 #if defined(HAVE_PCAP_CREATE) || defined(WIN32)
360 #define B_FLAG_USAGE " [ -B size ]"
361 #else /* defined(HAVE_PCAP_CREATE) || defined(WIN32) */
364 #endif /* defined(HAVE_PCAP_CREATE) || defined(WIN32) */
366 #ifdef HAVE_PCAP_CREATE
368 #else /* HAVE_PCAP_CREATE */
370 #endif /* HAVE_PCAP_CREATE */
372 #ifdef HAVE_PCAP_FINDALLDEVS
373 #ifndef HAVE_PCAP_IF_T
374 #undef HAVE_PCAP_FINDALLDEVS
378 #ifdef HAVE_PCAP_FINDALLDEVS
384 #ifdef HAVE_PCAP_DUMP_FLUSH
391 /* Drop root privileges and chroot if necessary */
393 droproot(const char *username
, const char *chroot_dir
)
395 struct passwd
*pw
= NULL
;
397 if (chroot_dir
&& !username
) {
398 fprintf(stderr
, "tcpdump: Chroot without dropping root is insecure\n");
402 pw
= getpwnam(username
);
405 if (chroot(chroot_dir
) != 0 || chdir ("/") != 0) {
406 fprintf(stderr
, "tcpdump: Couldn't chroot/chdir to '%.64s': %s\n",
407 chroot_dir
, pcap_strerror(errno
));
411 if (initgroups(pw
->pw_name
, pw
->pw_gid
) != 0 ||
412 setgid(pw
->pw_gid
) != 0 || setuid(pw
->pw_uid
) != 0) {
413 fprintf(stderr
, "tcpdump: Couldn't change to '%.32s' uid=%lu gid=%lu: %s\n",
415 (unsigned long)pw
->pw_uid
,
416 (unsigned long)pw
->pw_gid
,
417 pcap_strerror(errno
));
422 fprintf(stderr
, "tcpdump: Couldn't find user '%.32s'\n",
445 MakeFilename(char *buffer
, char *orig_name
, int cnt
, int max_chars
)
447 char *filename
= malloc(NAME_MAX
+ 1);
449 /* Process with strftime if Gflag is set. */
453 /* Convert Gflag_time to a usable format */
454 if ((local_tm
= localtime(&Gflag_time
)) == NULL
) {
455 error("MakeTimedFilename: localtime");
458 /* There's no good way to detect an error in strftime since a return
459 * value of 0 isn't necessarily failure.
461 strftime(filename
, NAME_MAX
, orig_name
, local_tm
);
463 strncpy(filename
, orig_name
, NAME_MAX
);
466 if (cnt
== 0 && max_chars
== 0)
467 strncpy(buffer
, filename
, NAME_MAX
+ 1);
469 if (snprintf(buffer
, NAME_MAX
+ 1, "%s%0*d", filename
, max_chars
, cnt
) > NAME_MAX
)
470 /* Report an error if the filename is too large */
471 error("too many output files or filename is too long (> %d)", NAME_MAX
);
475 static int tcpdump_printf(netdissect_options
*ndo _U_
,
476 const char *fmt
, ...)
483 ret
=vfprintf(stdout
, fmt
, args
);
490 main(int argc
, char **argv
)
492 register int cnt
, op
, i
;
493 bpf_u_int32 localnet
, netmask
;
494 register char *cp
, *infile
, *cmdbuf
, *device
, *RFileName
, *WFileName
;
495 pcap_handler callback
;
497 struct bpf_program fcode
;
499 RETSIGTYPE (*oldhandler
)(int);
501 struct print_info printinfo
;
502 struct dump_info dumpinfo
;
503 u_char
*pcap_userdata
;
504 char ebuf
[PCAP_ERRBUF_SIZE
];
505 char *username
= NULL
;
506 char *chroot_dir
= NULL
;
507 #ifdef HAVE_PCAP_FINDALLDEVS
508 pcap_if_t
*devpointer
;
513 if(wsockinit() != 0) return 1;
519 gndo
->ndo_default_print
=ndo_default_print
;
520 gndo
->ndo_printf
=tcpdump_printf
;
521 gndo
->ndo_error
=ndo_error
;
522 gndo
->ndo_warning
=ndo_warning
;
523 gndo
->ndo_snaplen
= DEFAULT_SNAPLEN
;
530 if ((cp
= strrchr(argv
[0], '/')) != NULL
)
531 program_name
= cp
+ 1;
533 program_name
= argv
[0];
535 if (abort_on_misalignment(ebuf
, sizeof(ebuf
)) < 0)
544 (op
= getopt(argc
, argv
, "aA" 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)
548 /* compatibility for old -a */
558 #if defined(HAVE_PCAP_CREATE) || defined(WIN32)
560 Bflag
= atoi(optarg
)*1024;
562 error("invalid packet buffer size %s", optarg
);
564 #endif /* defined(HAVE_PCAP_CREATE) || defined(WIN32) */
569 error("invalid packet count %s", optarg
);
573 Cflag
= atoi(optarg
) * 1000000;
575 error("invalid file size %s", optarg
);
582 #ifdef HAVE_PCAP_FINDALLDEVS
584 if (pcap_findalldevs(&devpointer
, ebuf
) < 0)
587 for (i
= 0; devpointer
!= 0; i
++) {
588 printf("%d.%s", i
+1, devpointer
->name
);
589 if (devpointer
->description
!= NULL
)
590 printf(" (%s)", devpointer
->description
);
592 devpointer
= devpointer
->next
;
596 #endif /* HAVE_PCAP_FINDALLDEVS */
607 #ifndef HAVE_LIBCRYPTO
608 warning("crypto code not compiled in");
610 gndo
->ndo_espsecret
= optarg
;
622 Gflag
= atoi(optarg
);
624 error("invalid number of seconds %s", optarg
);
626 /* We will create one file initially. */
629 /* Grab the current time for rotation use. */
630 if ((Gflag_time
= time(NULL
)) == (time_t)-1) {
631 error("main: can't get current time: %s",
632 pcap_strerror(errno
));
637 if (optarg
[0] == '0' && optarg
[1] == 0)
638 error("Invalid adapter index");
640 #ifdef HAVE_PCAP_FINDALLDEVS
642 * If the argument is a number, treat it as
643 * an index into the list of adapters, as
644 * printed by "tcpdump -D".
646 * This should be OK on UNIX systems, as interfaces
647 * shouldn't have names that begin with digits.
648 * It can be useful on Windows, where more than
649 * one interface can have the same name.
651 if ((devnum
= atoi(optarg
)) != 0) {
653 error("Invalid adapter index");
655 if (pcap_findalldevs(&devpointer
, ebuf
) < 0)
658 for (i
= 0; i
< devnum
-1; i
++){
659 devpointer
= devpointer
->next
;
660 if (devpointer
== NULL
)
661 error("Invalid adapter index");
664 device
= devpointer
->name
;
667 #endif /* HAVE_PCAP_FINDALLDEVS */
671 #ifdef HAVE_PCAP_CREATE
675 #endif /* HAVE_PCAP_CREATE */
680 * _IOLBF is the same as _IOFBF in Microsoft's C
681 * libraries; the only alternative they offer
684 * XXX - this should really be checking for MSVC++,
685 * not WIN32, if, for example, MinGW has its own
686 * C library that is more UNIX-compatible.
688 setvbuf(stdout
, NULL
, _IONBF
, 0);
690 #ifdef HAVE_SETLINEBUF
693 setvbuf(stdout
, NULL
, _IOLBF
, 0);
704 if (smiLoadModule(optarg
) == 0) {
705 error("could not load MIB module %s", optarg
);
709 (void)fprintf(stderr
, "%s: ignoring option `-m %s' ",
710 program_name
, optarg
);
711 (void)fprintf(stderr
, "(no libsmi support)\n");
716 /* TCP-MD5 shared secret */
717 #ifndef HAVE_LIBCRYPTO
718 warning("crypto code not compiled in");
741 ++suppress_default_print
;
755 snaplen
= strtol(optarg
, &end
, 0);
756 if (optarg
== end
|| *end
!= '\0'
757 || snaplen
< 0 || snaplen
> MAXIMUM_SNAPLEN
)
758 error("invalid snaplen %s", optarg
);
759 else if (snaplen
== 0)
760 snaplen
= MAXIMUM_SNAPLEN
;
773 if (strcasecmp(optarg
, "vat") == 0)
775 else if (strcasecmp(optarg
, "wb") == 0)
777 else if (strcasecmp(optarg
, "rpc") == 0)
779 else if (strcasecmp(optarg
, "rtp") == 0)
781 else if (strcasecmp(optarg
, "rtcp") == 0)
782 packettype
= PT_RTCP
;
783 else if (strcasecmp(optarg
, "snmp") == 0)
784 packettype
= PT_SNMP
;
785 else if (strcasecmp(optarg
, "cnfp") == 0)
786 packettype
= PT_CNFP
;
787 else if (strcasecmp(optarg
, "tftp") == 0)
788 packettype
= PT_TFTP
;
789 else if (strcasecmp(optarg
, "aodv") == 0)
790 packettype
= PT_AODV
;
792 error("unknown packet type `%s'", optarg
);
799 #ifdef HAVE_PCAP_DUMP_FLUSH
814 Wflag
= atoi(optarg
);
816 error("invalid number of output files %s", optarg
);
817 WflagChars
= getWflagChars(Wflag
);
822 ++suppress_default_print
;
827 ++suppress_default_print
;
831 gndo
->ndo_dltname
= optarg
;
833 pcap_datalink_name_to_val(gndo
->ndo_dltname
);
834 if (gndo
->ndo_dlt
< 0)
835 error("invalid data link type %s", gndo
->ndo_dltname
);
838 #if defined(HAVE_PCAP_DEBUG) || defined(HAVE_YYDEBUG)
841 /* Undocumented flag */
842 #ifdef HAVE_PCAP_DEBUG
843 extern int pcap_debug
;
854 zflag
= strdup(optarg
);
863 username
= strdup(optarg
);
878 case 0: /* Default */
879 case 4: /* Default + Date*/
880 thiszone
= gmt2local(0);
883 case 1: /* No time stamp */
884 case 2: /* Unix timeval style */
885 case 3: /* Microseconds since previous packet */
886 case 5: /* Microseconds since first packet */
889 default: /* Not supported */
890 error("only -t, -tt, -ttt, -tttt and -ttttt are supported");
895 /* if run as root, prepare for chrooting */
896 if (getuid() == 0 || geteuid() == 0) {
897 /* future extensibility for cmd-line arguments */
899 chroot_dir
= WITH_CHROOT
;
904 /* if run as root, prepare for dropping root privileges */
905 if (getuid() == 0 || geteuid() == 0) {
906 /* Run with '-Z root' to restore old behaviour */
908 username
= WITH_USER
;
912 if (RFileName
!= NULL
) {
914 const char *dlt_name
;
918 * We don't need network access, so relinquish any set-UID
919 * or set-GID privileges we have (if any).
921 * We do *not* want set-UID privileges when opening a
922 * trace file, as that might let the user read other
923 * people's trace files (especially if we're set-UID
926 if (setgid(getgid()) != 0 || setuid(getuid()) != 0 )
927 fprintf(stderr
, "Warning: setgid/setuid failed !\n");
929 pd
= pcap_open_offline(RFileName
, ebuf
);
932 dlt
= pcap_datalink(pd
);
933 dlt_name
= pcap_datalink_val_to_name(dlt
);
934 if (dlt_name
== NULL
) {
935 fprintf(stderr
, "reading from file %s, link-type %u\n",
939 "reading from file %s, link-type %s (%s)\n",
941 pcap_datalink_val_to_description(dlt
));
946 error("-f and -r options are incompatible");
948 if (device
== NULL
) {
949 device
= pcap_lookupdev(ebuf
);
954 if(strlen(device
) == 1) //we assume that an ASCII string is always longer than 1 char
955 { //a Unicode string has a \0 as second byte (so strlen() is 1)
956 fprintf(stderr
, "%s: listening on %ws\n", program_name
, device
);
960 fprintf(stderr
, "%s: listening on %s\n", program_name
, device
);
965 #ifdef HAVE_PCAP_CREATE
966 pd
= pcap_create(device
, ebuf
);
969 status
= pcap_set_snaplen(pd
, snaplen
);
971 error("%s: pcap_set_snaplen failed: %s",
972 device
, pcap_statustostr(status
));
973 status
= pcap_set_promisc(pd
, !pflag
);
975 error("%s: pcap_set_promisc failed: %s",
976 device
, pcap_statustostr(status
));
978 status
= pcap_set_rfmon(pd
, 1);
980 error("%s: pcap_set_rfmon failed: %s",
981 device
, pcap_statustostr(status
));
983 status
= pcap_set_timeout(pd
, 1000);
985 error("%s: pcap_set_timeout failed: %s",
986 device
, pcap_statustostr(status
));
988 status
= pcap_set_buffer_size(pd
, Bflag
);
990 error("%s: pcap_set_buffer_size failed: %s",
991 device
, pcap_statustostr(status
));
993 status
= pcap_activate(pd
);
996 * pcap_activate() failed.
998 cp
= pcap_geterr(pd
);
999 if (status
== PCAP_ERROR
)
1001 else if ((status
== PCAP_ERROR_NO_SUCH_DEVICE
||
1002 status
== PCAP_ERROR_PERM_DENIED
) &&
1004 error("%s: %s\n(%s)", device
,
1005 pcap_statustostr(status
), cp
);
1007 error("%s: %s", device
,
1008 pcap_statustostr(status
));
1009 } else if (status
> 0) {
1011 * pcap_activate() succeeded, but it's warning us
1012 * of a problem it had.
1014 cp
= pcap_geterr(pd
);
1015 if (status
== PCAP_WARNING
)
1017 else if (status
== PCAP_WARNING_PROMISC_NOTSUP
&&
1019 warning("%s: %s\n(%s)", device
,
1020 pcap_statustostr(status
), cp
);
1022 warning("%s: %s", device
,
1023 pcap_statustostr(status
));
1027 pd
= pcap_open_live(device
, snaplen
, !pflag
, 1000, ebuf
);
1031 warning("%s", ebuf
);
1032 #endif /* HAVE_PCAP_CREATE */
1034 * Let user own process after socket has been opened.
1037 if (setgid(getgid()) != 0 || setuid(getuid()) != 0)
1038 fprintf(stderr
, "Warning: setgid/setuid failed !\n");
1040 #if !defined(HAVE_PCAP_CREATE) && defined(WIN32)
1042 if(pcap_setbuff(pd
, Bflag
)==-1){
1043 error("%s", pcap_geterr(pd
));
1045 #endif /* !defined(HAVE_PCAP_CREATE) && defined(WIN32) */
1047 show_dlts_and_exit(pd
);
1048 if (gndo
->ndo_dlt
>= 0) {
1049 #ifdef HAVE_PCAP_SET_DATALINK
1050 if (pcap_set_datalink(pd
, gndo
->ndo_dlt
) < 0)
1051 error("%s", pcap_geterr(pd
));
1054 * We don't actually support changing the
1055 * data link type, so we only let them
1056 * set it to what it already is.
1058 if (gndo
->ndo_dlt
!= pcap_datalink(pd
)) {
1059 error("%s is not one of the DLTs supported by this device\n",
1063 (void)fprintf(stderr
, "%s: data link type %s\n",
1064 program_name
, gndo
->ndo_dltname
);
1065 (void)fflush(stderr
);
1067 i
= pcap_snapshot(pd
);
1069 warning("snaplen raised from %d to %d", snaplen
, i
);
1072 if (pcap_lookupnet(device
, &localnet
, &netmask
, ebuf
) < 0) {
1075 warning("%s", ebuf
);
1079 cmdbuf
= read_infile(infile
);
1081 cmdbuf
= copy_argv(&argv
[optind
]);
1083 if (pcap_compile(pd
, &fcode
, cmdbuf
, Oflag
, netmask
) < 0)
1084 error("%s", pcap_geterr(pd
));
1086 bpf_dump(&fcode
, dflag
);
1090 init_addrtoname(localnet
, netmask
);
1094 (void)setsignal(SIGPIPE
, cleanup
);
1095 (void)setsignal(SIGTERM
, cleanup
);
1096 (void)setsignal(SIGINT
, cleanup
);
1097 (void)setsignal(SIGCHLD
, child_cleanup
);
1099 /* Cooperate with nohup(1) */
1101 if ((oldhandler
= setsignal(SIGHUP
, cleanup
)) != SIG_DFL
)
1102 (void)setsignal(SIGHUP
, oldhandler
);
1105 if (pcap_setfilter(pd
, &fcode
) < 0)
1106 error("%s", pcap_geterr(pd
));
1109 /* Do not exceed the default NAME_MAX for files. */
1110 dumpinfo
.CurrentFileName
= (char *)malloc(NAME_MAX
+ 1);
1112 if (dumpinfo
.CurrentFileName
== NULL
)
1113 error("malloc of dumpinfo.CurrentFileName");
1115 /* We do not need numbering for dumpfiles if Cflag isn't set. */
1117 MakeFilename(dumpinfo
.CurrentFileName
, WFileName
, 0, WflagChars
);
1119 MakeFilename(dumpinfo
.CurrentFileName
, WFileName
, 0, 0);
1121 p
= pcap_dump_open(pd
, dumpinfo
.CurrentFileName
);
1123 error("%s", pcap_geterr(pd
));
1124 if (Cflag
!= 0 || Gflag
!= 0) {
1125 callback
= dump_packet_and_trunc
;
1126 dumpinfo
.WFileName
= WFileName
;
1129 pcap_userdata
= (u_char
*)&dumpinfo
;
1131 callback
= dump_packet
;
1132 pcap_userdata
= (u_char
*)p
;
1135 type
= pcap_datalink(pd
);
1136 printinfo
.printer
= lookup_printer(type
);
1137 if (printinfo
.printer
== NULL
) {
1138 gndo
->ndo_dltname
= pcap_datalink_val_to_name(type
);
1139 if (gndo
->ndo_dltname
!= NULL
)
1140 error("packet printing is not supported for link type %s: use -w",
1143 error("packet printing is not supported for link type %d: use -w", type
);
1145 callback
= print_packet
;
1146 pcap_userdata
= (u_char
*)&printinfo
;
1150 * We cannot do this earlier, because we want to be able to open
1151 * the file (if done) for writing before giving up permissions.
1153 if (getuid() == 0 || geteuid() == 0) {
1154 if (username
|| chroot_dir
)
1155 droproot(username
, chroot_dir
);
1160 * We can't get statistics when reading from a file rather
1161 * than capturing from a device.
1163 if (RFileName
== NULL
)
1164 (void)setsignal(SIGINFO
, requestinfo
);
1167 if (vflag
> 0 && WFileName
) {
1169 * When capturing to a file, "-v" means tcpdump should,
1170 * every 10 secodns, "v"erbosely report the number of
1173 #ifdef USE_WIN32_MM_TIMER
1174 /* call verbose_stats_dump() each 1000 +/-100msec */
1175 timer_id
= timeSetEvent(1000, 100, verbose_stats_dump
, 0, TIME_PERIODIC
);
1176 setvbuf(stderr
, NULL
, _IONBF
, 0);
1177 #elif defined(HAVE_ALARM)
1178 (void)setsignal(SIGALRM
, verbose_stats_dump
);
1184 if (RFileName
== NULL
) {
1186 const char *dlt_name
;
1188 if (!vflag
&& !WFileName
) {
1189 (void)fprintf(stderr
,
1190 "%s: verbose output suppressed, use -v or -vv for full protocol decode\n",
1193 (void)fprintf(stderr
, "%s: ", program_name
);
1194 dlt
= pcap_datalink(pd
);
1195 dlt_name
= pcap_datalink_val_to_name(dlt
);
1196 if (dlt_name
== NULL
) {
1197 (void)fprintf(stderr
, "listening on %s, link-type %u, capture size %u bytes\n",
1198 device
, dlt
, snaplen
);
1200 (void)fprintf(stderr
, "listening on %s, link-type %s (%s), capture size %u bytes\n",
1202 pcap_datalink_val_to_description(dlt
), snaplen
);
1204 (void)fflush(stderr
);
1207 status
= pcap_loop(pd
, cnt
, callback
, pcap_userdata
);
1208 if (WFileName
== NULL
) {
1210 * We're printing packets. Flush the printed output,
1211 * so it doesn't get intermingled with error output.
1215 * We got interrupted, so perhaps we didn't
1216 * manage to finish a line we were printing.
1217 * Print an extra newline, just in case.
1221 (void)fflush(stdout
);
1227 (void)fprintf(stderr
, "%s: pcap_loop: %s\n",
1228 program_name
, pcap_geterr(pd
));
1230 if (RFileName
== NULL
) {
1232 * We're doing a live capture. Report the capture
1238 exit(status
== -1 ? 1 : 0);
1241 /* make a clean exit on interrupts */
1243 cleanup(int signo _U_
)
1245 #ifdef USE_WIN32_MM_TIMER
1247 timeKillEvent(timer_id
);
1249 #elif defined(HAVE_ALARM)
1253 #ifdef HAVE_PCAP_BREAKLOOP
1255 * We have "pcap_breakloop()"; use it, so that we do as little
1256 * as possible in the signal handler (it's probably not safe
1257 * to do anything with standard I/O streams in a signal handler -
1258 * the ANSI C standard doesn't say it is).
1263 * We don't have "pcap_breakloop()"; this isn't safe, but
1264 * it's the best we can do. Print the summary if we're
1265 * not reading from a savefile - i.e., if we're doing a
1266 * live capture - and exit.
1268 if (pd
!= NULL
&& pcap_file(pd
) == NULL
) {
1270 * We got interrupted, so perhaps we didn't
1271 * manage to finish a line we were printing.
1272 * Print an extra newline, just in case.
1275 (void)fflush(stdout
);
1283 On windows, we do not use a fork, so we do not care less about
1284 waiting a child processes to die
1288 child_cleanup(int signo _U_
)
1295 info(register int verbose
)
1297 struct pcap_stat stat
;
1299 if (pcap_stats(pd
, &stat
) < 0) {
1300 (void)fprintf(stderr
, "pcap_stats: %s\n", pcap_geterr(pd
));
1306 fprintf(stderr
, "%s: ", program_name
);
1308 (void)fprintf(stderr
, "%u packets captured", packets_captured
);
1310 fputs(", ", stderr
);
1313 (void)fprintf(stderr
, "%d packets received by filter", stat
.ps_recv
);
1315 fputs(", ", stderr
);
1318 (void)fprintf(stderr
, "%d packets dropped by kernel\n", stat
.ps_drop
);
1324 compress_savefile(const char *filename
)
1329 * Set to lowest priority so that this doesn't disturb the capture
1332 setpriority(PRIO_PROCESS
, 0, NZERO
- 1);
1334 setpriority(PRIO_PROCESS
, 0, 19);
1336 if (execlp(zflag
, zflag
, filename
, NULL
) == -1)
1338 "compress_savefile:execlp(%s, %s): %s\n",
1345 compress_savefile(const char *filename
)
1348 "compress_savefile failed. Functionality not implemented under windows\n");
1353 dump_packet_and_trunc(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
1355 struct dump_info
*dump_info
;
1361 dump_info
= (struct dump_info
*)user
;
1364 * XXX - this won't force the file to rotate on the specified time
1365 * boundary, but it will rotate on the first packet received after the
1366 * specified Gflag number of seconds. Note: if a Gflag time boundary
1367 * and a Cflag size boundary coincide, the time rotation will occur
1368 * first thereby cancelling the Cflag boundary (since the file should
1372 /* Check if it is time to rotate */
1375 /* Get the current time */
1376 if ((t
= time(NULL
)) == (time_t)-1) {
1377 error("dump_and_trunc_packet: can't get current_time: %s",
1378 pcap_strerror(errno
));
1382 /* If the time is greater than the specified window, rotate */
1383 if (t
- Gflag_time
>= Gflag
) {
1384 /* Update the Gflag_time */
1386 /* Update Gflag_count */
1389 * Close the current file and open a new one.
1391 pcap_dump_close(dump_info
->p
);
1394 * Compress the file we just closed, if the user asked for it
1397 compress_savefile(dump_info
->CurrentFileName
);
1400 * Check to see if we've exceeded the Wflag (when
1403 if (Cflag
== 0 && Wflag
> 0 && Gflag_count
>= Wflag
) {
1404 (void)fprintf(stderr
, "Maximum file limit reached: %d\n",
1409 if (dump_info
->CurrentFileName
!= NULL
)
1410 free(dump_info
->CurrentFileName
);
1411 /* Allocate space for max filename + \0. */
1412 dump_info
->CurrentFileName
= (char *)malloc(NAME_MAX
+ 1);
1413 if (dump_info
->CurrentFileName
== NULL
)
1414 error("dump_packet_and_trunc: malloc");
1416 * This is always the first file in the Cflag
1418 * We also don't need numbering if Cflag is not set.
1421 MakeFilename(dump_info
->CurrentFileName
, dump_info
->WFileName
, 0,
1424 MakeFilename(dump_info
->CurrentFileName
, dump_info
->WFileName
, 0, 0);
1426 dump_info
->p
= pcap_dump_open(dump_info
->pd
, dump_info
->CurrentFileName
);
1427 if (dump_info
->p
== NULL
)
1428 error("%s", pcap_geterr(pd
));
1433 * XXX - this won't prevent capture files from getting
1434 * larger than Cflag - the last packet written to the
1435 * file could put it over Cflag.
1437 if (Cflag
!= 0 && pcap_dump_ftell(dump_info
->p
) > Cflag
) {
1439 * Close the current file and open a new one.
1441 pcap_dump_close(dump_info
->p
);
1444 * Compress the file we just closed, if the user asked for it
1447 compress_savefile(dump_info
->CurrentFileName
);
1451 if (Cflag_count
>= Wflag
)
1454 if (dump_info
->CurrentFileName
!= NULL
)
1455 free(dump_info
->CurrentFileName
);
1456 dump_info
->CurrentFileName
= (char *)malloc(NAME_MAX
+ 1);
1457 if (dump_info
->CurrentFileName
== NULL
)
1458 error("dump_packet_and_trunc: malloc");
1459 MakeFilename(dump_info
->CurrentFileName
, dump_info
->WFileName
, Cflag_count
, WflagChars
);
1460 dump_info
->p
= pcap_dump_open(dump_info
->pd
, dump_info
->CurrentFileName
);
1461 if (dump_info
->p
== NULL
)
1462 error("%s", pcap_geterr(pd
));
1465 pcap_dump((u_char
*)dump_info
->p
, h
, sp
);
1466 #ifdef HAVE_PCAP_DUMP_FLUSH
1468 pcap_dump_flush(dump_info
->p
);
1477 dump_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
1483 pcap_dump(user
, h
, sp
);
1484 #ifdef HAVE_PCAP_DUMP_FLUSH
1486 pcap_dump_flush((pcap_dumper_t
*)user
);
1495 print_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
1497 struct print_info
*print_info
;
1505 print_info
= (struct print_info
*)user
;
1508 * Some printers want to check that they're not walking off the
1509 * end of the packet.
1510 * Rather than pass it all the way down, we set this global.
1512 snapend
= sp
+ h
->caplen
;
1514 hdrlen
= (*print_info
->printer
)(h
, sp
);
1517 * Print the raw packet data in hex and ASCII.
1521 * Include the link-layer header.
1523 hex_and_ascii_print("\n\t", sp
, h
->caplen
);
1526 * Don't include the link-layer header - and if
1527 * we have nothing past the link-layer header,
1530 if (h
->caplen
> hdrlen
)
1531 hex_and_ascii_print("\n\t", sp
+ hdrlen
,
1532 h
->caplen
- hdrlen
);
1536 * Print the raw packet data in hex.
1540 * Include the link-layer header.
1542 hex_print("\n\t", sp
, h
->caplen
);
1545 * Don't include the link-layer header - and if
1546 * we have nothing past the link-layer header,
1549 if (h
->caplen
> hdrlen
)
1550 hex_print("\n\t", sp
+ hdrlen
,
1551 h
->caplen
- hdrlen
);
1555 * Print the raw packet data in ASCII.
1559 * Include the link-layer header.
1561 ascii_print(sp
, h
->caplen
);
1564 * Don't include the link-layer header - and if
1565 * we have nothing past the link-layer header,
1568 if (h
->caplen
> hdrlen
)
1569 ascii_print(sp
+ hdrlen
, h
->caplen
- hdrlen
);
1582 * XXX - there should really be libpcap calls to get the version
1583 * number as a string (the string would be generated from #defines
1584 * at run time, so that it's not generated from string constants
1585 * in the library, as, on many UNIX systems, those constants would
1586 * be statically linked into the application executable image, and
1587 * would thus reflect the version of libpcap on the system on
1588 * which the application was *linked*, not the system on which it's
1591 * That routine should be documented, unlike the "version[]"
1592 * string, so that UNIX vendors providing their own libpcaps
1593 * don't omit it (as a couple of vendors have...).
1595 * Packet.dll should perhaps also export a routine to return the
1596 * version number of the Packet.dll code, to supply the
1597 * "Wpcap_version" information on Windows.
1599 char WDversion
[]="current-cvs.tcpdump.org";
1600 #if !defined(HAVE_GENERATED_VERSION)
1601 char version
[]="current-cvs.tcpdump.org";
1603 char pcap_version
[]="current-cvs.tcpdump.org";
1604 char Wpcap_version
[]="3.1";
1608 * By default, print the specified data out in hex and ASCII.
1611 ndo_default_print(netdissect_options
*ndo _U_
, const u_char
*bp
, u_int length
)
1613 hex_and_ascii_print("\n\t", bp
, length
); /* pass on lf and identation string */
1617 default_print(const u_char
*bp
, u_int length
)
1619 ndo_default_print(gndo
, bp
, length
);
1623 RETSIGTYPE
requestinfo(int signo _U_
)
1633 * Called once each second in verbose mode while dumping to file
1635 #ifdef USE_WIN32_MM_TIMER
1636 void CALLBACK
verbose_stats_dump (UINT timer_id _U_
, UINT msg _U_
, DWORD_PTR arg _U_
,
1637 DWORD_PTR dw1 _U_
, DWORD_PTR dw2 _U_
)
1639 struct pcap_stat stat
;
1641 if (infodelay
== 0 && pcap_stats(pd
, &stat
) >= 0)
1642 fprintf(stderr
, "Got %u\r", packets_captured
);
1644 #elif defined(HAVE_ALARM)
1645 static void verbose_stats_dump(int sig _U_
)
1647 struct pcap_stat stat
;
1649 if (infodelay
== 0 && pcap_stats(pd
, &stat
) >= 0)
1650 fprintf(stderr
, "Got %u\r", packets_captured
);
1658 extern char version
[];
1659 #ifndef HAVE_PCAP_LIB_VERSION
1660 #if defined(WIN32) || defined(HAVE_PCAP_VERSION)
1661 extern char pcap_version
[];
1662 #else /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
1663 static char pcap_version
[] = "unknown";
1664 #endif /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
1665 #endif /* HAVE_PCAP_LIB_VERSION */
1667 #ifdef HAVE_PCAP_LIB_VERSION
1669 (void)fprintf(stderr
, "%s version %s, based on tcpdump version %s\n", program_name
, WDversion
, version
);
1671 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
1673 (void)fprintf(stderr
, "%s\n",pcap_lib_version());
1674 #else /* HAVE_PCAP_LIB_VERSION */
1676 (void)fprintf(stderr
, "%s version %s, based on tcpdump version %s\n", program_name
, WDversion
, version
);
1677 (void)fprintf(stderr
, "WinPcap version %s, based on libpcap version %s\n",Wpcap_version
, pcap_version
);
1679 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
1680 (void)fprintf(stderr
, "libpcap version %s\n", pcap_version
);
1682 #endif /* HAVE_PCAP_LIB_VERSION */
1683 (void)fprintf(stderr
,
1684 "Usage: %s [-aAd" D_FLAG
"ef" I_FLAG
"KlLnNOpqRStu" U_FLAG
"vxX]" B_FLAG_USAGE
" [ -c count ]\n", program_name
);
1685 (void)fprintf(stderr
,
1686 "\t\t[ -C file_size ] [ -E algo:secret ] [ -F file ] [ -G seconds ]\n");
1687 (void)fprintf(stderr
,
1688 "\t\t[ -i interface ] [ -M secret ] [ -r file ]\n");
1689 (void)fprintf(stderr
,
1690 "\t\t[ -s snaplen ] [ -T type ] [ -w file ] [ -W filecount ]\n");
1691 (void)fprintf(stderr
,
1692 "\t\t[ -y datalinktype ] [ -z command ] [ -Z user ]\n");
1693 (void)fprintf(stderr
,
1694 "\t\t[ expression ]\n");
1702 ndo_error(netdissect_options
*ndo _U_
, const char *fmt
, ...)
1706 (void)fprintf(stderr
, "%s: ", program_name
);
1708 (void)vfprintf(stderr
, fmt
, ap
);
1712 if (fmt
[-1] != '\n')
1713 (void)fputc('\n', stderr
);
1721 ndo_warning(netdissect_options
*ndo _U_
, const char *fmt
, ...)
1725 (void)fprintf(stderr
, "%s: WARNING: ", program_name
);
1727 (void)vfprintf(stderr
, fmt
, ap
);
1731 if (fmt
[-1] != '\n')
1732 (void)fputc('\n', stderr
);