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
[] =
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
[] =
33 "@(#) $Header: /tcpdump/master/tcpdump/tcpdump.c,v 1.214 2003-08-08 09:47:45 risso 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
);
65 #include "interface.h"
66 #include "addrtoname.h"
68 #include "setsignal.h"
69 #include "gmt2local.h"
70 #include "pcap-missing.h"
72 int dflag
; /* print filter code */
73 int eflag
; /* print ethernet header */
74 int fflag
; /* don't translate "foreign" IP address */
75 int Lflag
; /* list available data link types and exit */
76 int nflag
; /* leave addresses as numbers */
77 int Nflag
; /* remove domains from printed host names */
78 int Oflag
= 1; /* run filter code optimizer */
79 int pflag
; /* don't go promiscuous */
80 int qflag
; /* quick (shorter) output */
81 int Rflag
= 1; /* print sequence # field in AH/ESP*/
82 int sflag
= 0; /* use the libsmi to translate OIDs */
83 int Sflag
; /* print raw TCP sequence numbers */
84 int tflag
= 1; /* print packet arrival time */
85 int Uflag
= 0; /* "unbuffered" output of dump files */
86 int uflag
= 0; /* Print undecoded NFS handles */
87 int vflag
; /* verbose */
88 int xflag
; /* print packet in hex */
89 int Xflag
; /* print packet in ascii as well as hex */
90 off_t Cflag
= 0; /* rotate dump files after this many bytes */
91 int Aflag
= 0; /* print packet only in ascii observing LF, CR, TAB, SPACE */
92 int dlt
= -1; /* if != -1, ask libpcap for the DLT it names */
94 const char *dlt_name
= NULL
;
96 char *espsecret
= NULL
; /* ESP secret key */
100 static int infodelay
;
101 static int infoprint
;
105 int32_t thiszone
; /* seconds offset from gmt to local time */
108 static RETSIGTYPE
cleanup(int);
109 static void usage(void) __attribute__((noreturn
));
110 static void show_dlts_and_exit(pcap_t
*pd
) __attribute__((noreturn
));
112 static void print_packet(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
113 static void dump_packet_and_trunc(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
114 static void dump_packet(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
117 RETSIGTYPE
requestinfo(int);
120 static void info(int);
122 /* Length of saved portion of packet. */
123 int snaplen
= DEFAULT_SNAPLEN
;
125 typedef u_int (*if_printer
)(const struct pcap_pkthdr
*, const u_char
*);
132 static struct printer printers
[] = {
133 { arcnet_if_print
, DLT_ARCNET
},
134 #ifdef DLT_ARCNET_LINUX
135 { arcnet_linux_if_print
, DLT_ARCNET_LINUX
},
137 { ether_if_print
, DLT_EN10MB
},
138 { token_if_print
, DLT_IEEE802
},
140 { lane_if_print
, DLT_LANE8023
},
143 { cip_if_print
, DLT_CIP
},
146 { cip_if_print
, DLT_ATM_CLIP
},
148 { sl_if_print
, DLT_SLIP
},
149 #ifdef DLT_SLIP_BSDOS
150 { sl_bsdos_if_print
, DLT_SLIP_BSDOS
},
152 { ppp_if_print
, DLT_PPP
},
154 { ppp_bsdos_if_print
, DLT_PPP_BSDOS
},
156 { fddi_if_print
, DLT_FDDI
},
157 { null_if_print
, DLT_NULL
},
159 { null_if_print
, DLT_LOOP
},
161 { raw_if_print
, DLT_RAW
},
162 { atm_if_print
, DLT_ATM_RFC1483
},
164 { chdlc_if_print
, DLT_C_HDLC
},
167 { chdlc_if_print
, DLT_HDLC
},
169 #ifdef DLT_PPP_SERIAL
170 { ppp_hdlc_if_print
, DLT_PPP_SERIAL
},
173 { pppoe_if_print
, DLT_PPP_ETHER
},
176 { sll_if_print
, DLT_LINUX_SLL
},
178 #ifdef DLT_IEEE802_11
179 { ieee802_11_if_print
, DLT_IEEE802_11
},
182 { ltalk_if_print
, DLT_LTALK
},
185 { pflog_if_print
, DLT_PFLOG
},
188 { fr_if_print
, DLT_FR
},
191 { fr_if_print
, DLT_FRELAY
},
194 { sunatm_if_print
, DLT_SUNATM
},
196 #ifdef DLT_IP_OVER_FC
197 { ipfc_if_print
, DLT_IP_OVER_FC
},
199 #ifdef DLT_PRISM_HEADER
200 { prism_if_print
, DLT_PRISM_HEADER
},
202 #ifdef DLT_IEEE802_11_RADIO
203 { ieee802_11_radio_if_print
, DLT_IEEE802_11_RADIO
},
206 { enc_if_print
, DLT_ENC
},
212 lookup_printer(int type
)
216 for (p
= printers
; p
->f
; ++p
)
241 show_dlts_and_exit(pcap_t
*pd
)
245 const char *dlt_name
;
247 n_dlts
= pcap_list_datalinks(pd
, &dlts
);
249 error("%s", pcap_geterr(pd
));
250 else if (n_dlts
== 0 || !dlts
)
251 error("No data link types.");
253 (void) fprintf(stderr
, "Data link types (use option -y to set):\n");
255 while (--n_dlts
>= 0) {
256 dlt_name
= pcap_datalink_val_to_name(dlts
[n_dlts
]);
257 if (dlt_name
!= NULL
) {
258 (void) fprintf(stderr
, " %s", dlt_name
);
261 * OK, does tcpdump handle that type?
263 if (lookup_printer(dlts
[n_dlts
]) == NULL
)
264 (void) fprintf(stderr
, " (not supported)");
267 (void) fprintf(stderr
, " DLT %d (not supported)\n",
276 * Set up flags that might or might not be supported depending on the
277 * version of libpcap we're using.
281 #define B_FLAG_USAGE " [ -B size ]"
287 #ifdef HAVE_PCAP_FINDALLDEVS
293 #ifdef HAVE_PCAP_DUMP_FLUSH
300 main(int argc
, char **argv
)
302 register int cnt
, op
, i
;
303 bpf_u_int32 localnet
, netmask
;
304 register char *cp
, *infile
, *cmdbuf
, *device
, *RFileName
, *WFileName
;
305 pcap_handler callback
;
307 struct bpf_program fcode
;
309 RETSIGTYPE (*oldhandler
)(int);
311 struct print_info printinfo
;
312 struct dump_info dumpinfo
;
313 u_char
*pcap_userdata
;
314 char ebuf
[PCAP_ERRBUF_SIZE
];
315 #ifdef HAVE_PCAP_FINDALLDEVS
316 pcap_if_t
*devpointer
;
320 u_int UserBufferSize
= 1000000;
321 if(wsockinit() != 0) return 1;
329 if ((cp
= strrchr(argv
[0], '/')) != NULL
)
330 program_name
= cp
+ 1;
332 program_name
= argv
[0];
334 if (abort_on_misalignment(ebuf
, sizeof(ebuf
)) < 0)
343 (op
= getopt(argc
, argv
, "aA" B_FLAG
"c:C:d" D_FLAG
"eE:fF:i:lLm:nNOpqr:Rs:StT:u" U_FLAG
"vw:xXy:Y")) != -1)
347 /* compatibility for old -a */
358 UserBufferSize
= atoi(optarg
)*1024;
359 if (UserBufferSize
< 0)
360 error("invalid packet buffer size %s", optarg
);
367 error("invalid packet count %s", optarg
);
371 Cflag
= atoi(optarg
) * 1000000;
373 error("invalid file size %s", optarg
);
380 #ifdef HAVE_PCAP_FINDALLDEVS
382 if (pcap_findalldevs(&devpointer
, ebuf
) < 0)
385 for (i
= 0; devpointer
!= 0; i
++) {
386 printf("%d.%s", i
+1, devpointer
->name
);
387 if (devpointer
->description
!= NULL
)
388 printf(" (%s)", devpointer
->description
);
390 devpointer
= devpointer
->next
;
394 #endif /* HAVE_PCAP_FINDALLDEVS */
405 #ifndef HAVE_LIBCRYPTO
406 warning("crypto code not compiled in");
420 if (optarg
[0] == '0' && optarg
[1] == 0)
421 error("Invalid adapter index");
423 #ifdef HAVE_PCAP_FINDALLDEVS
425 * If the argument is a number, treat it as
426 * an index into the list of adapters, as
427 * printed by "tcpdump -D".
429 * This should be OK on UNIX systems, as interfaces
430 * shouldn't have names that begin with digits.
431 * It can be useful on Windows, where more than
432 * one interface can have the same name.
434 if ((devnum
= atoi(optarg
)) != 0) {
436 error("Invalid adapter index");
438 if (pcap_findalldevs(&devpointer
, ebuf
) < 0)
441 for (i
= 0; i
< devnum
-1; i
++){
442 devpointer
= devpointer
->next
;
443 if (devpointer
== NULL
)
444 error("Invalid adapter index");
447 device
= devpointer
->name
;
450 #endif /* HAVE_PCAP_FINDALLDEVS */
455 #ifdef HAVE_SETLINEBUF
458 setvbuf(stdout
, NULL
, _IOLBF
, 0);
472 if (smiLoadModule(optarg
) == 0) {
473 error("could not load MIB module %s", optarg
);
477 (void)fprintf(stderr
, "%s: ignoring option `-m %s' ",
478 program_name
, optarg
);
479 (void)fprintf(stderr
, "(no libsmi support)\n");
505 snaplen
= strtol(optarg
, &end
, 0);
506 if (optarg
== end
|| *end
!= '\0'
507 || snaplen
< 0 || snaplen
> 65535)
508 error("invalid snaplen %s", optarg
);
509 else if (snaplen
== 0)
523 if (strcasecmp(optarg
, "vat") == 0)
525 else if (strcasecmp(optarg
, "wb") == 0)
527 else if (strcasecmp(optarg
, "rpc") == 0)
529 else if (strcasecmp(optarg
, "rtp") == 0)
531 else if (strcasecmp(optarg
, "rtcp") == 0)
532 packettype
= PT_RTCP
;
533 else if (strcasecmp(optarg
, "snmp") == 0)
534 packettype
= PT_SNMP
;
535 else if (strcasecmp(optarg
, "cnfp") == 0)
536 packettype
= PT_CNFP
;
537 else if (strcasecmp(optarg
, "tftp") == 0)
538 packettype
= PT_TFTP
;
539 else if (strcasecmp(optarg
, "aodv") == 0)
540 packettype
= PT_AODV
;
542 error("unknown packet type `%s'", optarg
);
549 #ifdef HAVE_PCAP_DUMP_FLUSH
574 dlt
= pcap_datalink_name_to_val(dlt_name
);
576 error("invalid data link type %s", dlt_name
);
579 #if defined(HAVE_PCAP_DEBUG) || defined(HAVE_YYDEBUG)
582 /* Undocumented flag */
583 #ifdef HAVE_PCAP_DEBUG
584 extern int pcap_debug
;
599 thiszone
= gmt2local(0);
601 if (RFileName
!= NULL
) {
603 const char *dlt_name
;
607 * We don't need network access, so relinquish any set-UID
608 * or set-GID privileges we have (if any).
610 * We do *not* want set-UID privileges when opening a
611 * trace file, as that might let the user read other
612 * people's trace files (especially if we're set-UID
617 pd
= pcap_open_offline(RFileName
, ebuf
);
620 dlt
= pcap_datalink(pd
);
621 dlt_name
= pcap_datalink_val_to_name(dlt
);
622 if (dlt_name
== NULL
)
624 printf("reading from file %s, link-type %u (%s)\n",
625 RFileName
, dlt
, dlt_name
);
629 error("-f and -r options are incompatible");
631 if (device
== NULL
) {
632 device
= pcap_lookupdev(ebuf
);
637 if(IsTextUnicode(device
,
638 wcslen((short*)device
), // Device always ends with a double \0, so this way to determine its
639 // length should be always valid
642 fprintf(stderr
, "%s: listening on %ws\n", program_name
, device
);
646 fprintf(stderr
, "%s: listening on %s\n", program_name
, device
);
652 pd
= pcap_open_live(device
, snaplen
, !pflag
, 1000, ebuf
);
658 if(UserBufferSize
!= 1000000)
659 if(pcap_setbuff(pd
, UserBufferSize
)==-1){
660 error("%s", pcap_geterr(pd
));
664 show_dlts_and_exit(pd
);
666 #ifdef HAVE_PCAP_SET_DATALINK
667 if (pcap_set_datalink(pd
, dlt
) < 0)
668 error("%s", pcap_geterr(pd
));
671 * We don't actually support changing the
672 * data link type, so we only let them
673 * set it to what it already is.
675 if (dlt
!= pcap_datalink(pd
)) {
676 error("%s is not one of the DLTs supported by this device\n",
680 (void)fprintf(stderr
, "%s: data link type %s\n",
681 program_name
, dlt_name
);
682 (void)fflush(stderr
);
684 i
= pcap_snapshot(pd
);
686 warning("snaplen raised from %d to %d", snaplen
, i
);
689 if (pcap_lookupnet(device
, &localnet
, &netmask
, ebuf
) < 0) {
695 * Let user own process after socket has been opened.
702 cmdbuf
= read_infile(infile
);
704 cmdbuf
= copy_argv(&argv
[optind
]);
706 if (pcap_compile(pd
, &fcode
, cmdbuf
, Oflag
, netmask
) < 0)
707 error("%s", pcap_geterr(pd
));
709 bpf_dump(&fcode
, dflag
);
713 init_addrtoname(localnet
, netmask
);
716 (void)setsignal(SIGPIPE
, cleanup
);
718 (void)setsignal(SIGTERM
, cleanup
);
719 (void)setsignal(SIGINT
, cleanup
);
720 /* Cooperate with nohup(1) */
722 if ((oldhandler
= setsignal(SIGHUP
, cleanup
)) != SIG_DFL
)
723 (void)setsignal(SIGHUP
, oldhandler
);
726 if (pcap_setfilter(pd
, &fcode
) < 0)
727 error("%s", pcap_geterr(pd
));
729 pcap_dumper_t
*p
= pcap_dump_open(pd
, WFileName
);
731 error("%s", pcap_geterr(pd
));
733 callback
= dump_packet_and_trunc
;
734 dumpinfo
.WFileName
= WFileName
;
737 pcap_userdata
= (u_char
*)&dumpinfo
;
739 callback
= dump_packet
;
740 pcap_userdata
= (u_char
*)p
;
743 type
= pcap_datalink(pd
);
744 printinfo
.printer
= lookup_printer(type
);
745 if (printinfo
.printer
== NULL
) {
746 dlt_name
= pcap_datalink_val_to_name(type
);
747 if (dlt_name
!= NULL
)
748 error("unsupported data link type %s", dlt_name
);
750 error("unsupported data link type %d", type
);
752 callback
= print_packet
;
753 pcap_userdata
= (u_char
*)&printinfo
;
756 (void)setsignal(SIGINFO
, requestinfo
);
759 if (RFileName
== NULL
) {
761 const char *dlt_name
;
763 if (!vflag
&& !WFileName
) {
764 (void)fprintf(stderr
,
765 "%s: verbose output suppressed, use -v or -vv for full protocol decode\n",
768 (void)fprintf(stderr
, "%s: ", program_name
);
769 dlt
= pcap_datalink(pd
);
770 dlt_name
= pcap_datalink_val_to_name(dlt
);
771 if (dlt_name
== NULL
)
773 (void)fprintf(stderr
, "listening on %s, link-type %u (%s), capture size %u bytes\n",
774 device
, dlt
, dlt_name
, snaplen
);
775 (void)fflush(stderr
);
778 if (pcap_loop(pd
, cnt
, callback
, pcap_userdata
) < 0) {
779 (void)fprintf(stderr
, "%s: pcap_loop: %s\n",
780 program_name
, pcap_geterr(pd
));
785 if (RFileName
== NULL
)
791 /* make a clean exit on interrupts */
796 /* Can't print the summary if reading from a savefile */
797 if (pd
!= NULL
&& pcap_file(pd
) == NULL
) {
798 (void)fflush(stdout
);
807 info(register int verbose
)
809 struct pcap_stat stat
;
811 if (pcap_stats(pd
, &stat
) < 0) {
812 (void)fprintf(stderr
, "pcap_stats: %s\n", pcap_geterr(pd
));
817 fprintf(stderr
, "%s: ", program_name
);
819 (void)fprintf(stderr
, "%d packets received by filter", stat
.ps_recv
);
824 (void)fprintf(stderr
, "%d packets dropped by kernel\n", stat
.ps_drop
);
833 for (i
= 0, j
= strlen(s
) - 1; i
< j
; i
++, j
--) {
842 swebitoa(unsigned int n
, char *s
)
848 s
[i
++] = n
% 10 + '0';
849 } while ((n
/= 10) > 0);
856 dump_packet_and_trunc(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
858 struct dump_info
*dump_info
;
864 dump_info
= (struct dump_info
*)user
;
867 * XXX - this won't prevent capture files from getting
868 * larger than Cflag - the last packet written to the
869 * file could put it over Cflag.
871 if (ftell((FILE *)dump_info
->p
) > Cflag
) {
872 name
= (char *) malloc(strlen(dump_info
->WFileName
) + 4);
874 error("dump_packet_and_trunc: malloc");
875 strcpy(name
, dump_info
->WFileName
);
876 swebitoa(cnt
, name
+ strlen(dump_info
->WFileName
));
878 pcap_dump_close(dump_info
->p
);
879 dump_info
->p
= pcap_dump_open(dump_info
->pd
, name
);
881 if (dump_info
->p
== NULL
)
882 error("%s", pcap_geterr(pd
));
885 pcap_dump((u_char
*)dump_info
->p
, h
, sp
);
886 #ifdef HAVE_PCAP_DUMP_FLUSH
888 pcap_dump_flush(dump_info
->p
);
897 dump_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
901 pcap_dump(user
, h
, sp
);
902 #ifdef HAVE_PCAP_DUMP_FLUSH
904 pcap_dump_flush((pcap_dumper_t
*)user
);
913 print_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
915 struct print_info
*print_info
;
921 print_info
= (struct print_info
*)user
;
924 * Some printers want to check that they're not walking off the
926 * Rather than pass it all the way down, we set this global.
928 snapend
= sp
+ h
->caplen
;
930 hdrlen
= (*print_info
->printer
)(h
, sp
);
933 * Print the raw packet data.
937 * Include the link-layer header.
939 default_print_unaligned(sp
, h
->caplen
);
942 * Don't include the link-layer header - and if
943 * we have nothing past the link-layer header,
946 if (h
->caplen
> hdrlen
)
947 default_print_unaligned(sp
+ hdrlen
,
959 /* Like default_print() but data need not be aligned */
961 default_print_unaligned(register const u_char
*cp
, register u_int length
)
964 register int nshorts
;
967 ascii_print(cp
, length
);
970 nshorts
= (u_int
) length
/ sizeof(u_short
);
972 while (--nshorts
>= 0) {
974 (void)printf("\n\t\t\t");
976 (void)printf(" %02x%02x", s
, *cp
++);
980 (void)printf("\n\t\t\t");
981 (void)printf(" %02x", *cp
);
987 * XXX - there should really be libpcap calls to get the version
988 * number as a string (the string would be generated from #defines
989 * at run time, so that it's not generated from string constants
990 * in the library, as, on many UNIX systems, those constants would
991 * be statically linked into the application executable image, and
992 * would thus reflect the version of libpcap on the system on
993 * which the application was *linked*, not the system on which it's
996 * That routine should be documented, unlike the "version[]"
997 * string, so that UNIX vendors providing their own libpcaps
998 * don't omit it (as a couple of vendors have...).
1000 * Packet.dll should perhaps also export a routine to return the
1001 * version number of the Packet.dll code, to supply the
1002 * "Wpcap_version" information on Windows.
1004 char WDversion
[]="current-cvs.tcpdump.org";
1005 char version
[]="current-cvs.tcpdump.org";
1006 char pcap_version
[]="current-cvs.tcpdump.org";
1007 char Wpcap_version
[]="3.0 alpha";
1011 * By default, print the specified data out in hex.
1014 default_print(register const u_char
*bp
, register u_int length
)
1016 default_print_unaligned(bp
, length
);
1020 RETSIGTYPE
requestinfo(int signo _U_
)
1032 extern char version
[];
1033 #ifndef HAVE_PCAP_LIB_VERSION
1034 #if defined(WIN32) || defined(HAVE_PCAP_VERSION)
1035 extern char pcap_version
[];
1036 #else /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
1037 static char pcap_version
[] = "unknown";
1038 #endif /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
1039 #endif /* HAVE_PCAP_LIB_VERSION */
1041 #ifdef HAVE_PCAP_LIB_VERSION
1042 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
1043 (void)fprintf(stderr
, "%s\n", pcap_lib_version());
1044 #else /* HAVE_PCAP_LIB_VERSION */
1046 (void)fprintf(stderr
, "%s version %s, based on tcpdump version %s\n", program_name
, WDversion
, version
);
1047 (void)fprintf(stderr
, "WinPcap version %s, based on libpcap version %s\n",Wpcap_version
, pcap_version
);
1049 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
1050 (void)fprintf(stderr
, "libpcap version %s\n", pcap_version
);
1052 #endif /* HAVE_PCAP_LIB_VERSION */
1053 (void)fprintf(stderr
,
1054 "Usage: %s [-aAd" D_FLAG
"eflLnNOpqRStu" U_FLAG
"vxX]" B_FLAG_USAGE
" [-c count] [ -C file_size ]\n", program_name
);
1055 (void)fprintf(stderr
,
1056 "\t\t[ -E algo:secret ] [ -F file ] [ -i interface ] [ -r file ]\n");
1057 (void)fprintf(stderr
,
1058 "\t\t[ -s snaplen ] [ -T type ] [ -w file ] [ -y datalinktype ]\n");
1059 (void)fprintf(stderr
,
1060 "\t\t[ expression ]\n");