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.213 2003-08-06 06:49:41 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
);
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
;
321 DWORD dwWindowsMajorVersion
;
322 u_int UserBufferSize
=1000000;
326 dwVersion
=GetVersion(); /* get the OS version */
327 dwWindowsMajorVersion
= (DWORD
)(LOBYTE(LOWORD(dwVersion
)));
328 if(wsockinit()!=0) return 1;
336 if ((cp
= strrchr(argv
[0], '/')) != NULL
)
337 program_name
= cp
+ 1;
339 program_name
= argv
[0];
341 if (abort_on_misalignment(ebuf
, sizeof(ebuf
)) < 0)
350 (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)
354 /* compatibility for old -a */
365 UserBufferSize
= atoi(optarg
)*1024;
366 if (UserBufferSize
< 0)
367 error("invalid packet buffer size %s", optarg
);
374 error("invalid packet count %s", optarg
);
378 Cflag
= atoi(optarg
) * 1000000;
380 error("invalid file size %s", optarg
);
387 #ifdef HAVE_PCAP_FINDALLDEVS
389 if (pcap_findalldevs(&devpointer
, ebuf
) < 0)
392 for (i
= 0; devpointer
!= 0; i
++) {
393 printf("%d.%s", i
+1, devpointer
->name
);
394 if (devpointer
->description
!= NULL
)
395 printf(" (%s)", devpointer
->description
);
397 devpointer
= devpointer
->next
;
401 #endif /* HAVE_PCAP_FINDALLDEVS */
412 #ifndef HAVE_LIBCRYPTO
413 warning("crypto code not compiled in");
427 if (optarg
[0] == '0' && optarg
[1] == 0)
428 error("Invalid adapter index");
430 #ifdef HAVE_PCAP_FINDALLDEVS
432 * If the argument is a number, treat it as
433 * an index into the list of adapters, as
434 * printed by "tcpdump -D".
436 * This should be OK on UNIX systems, as interfaces
437 * shouldn't have names that begin with digits.
438 * It can be useful on Windows, where more than
439 * one interface can have the same name.
441 if ((devnum
= atoi(optarg
)) != 0) {
443 error("Invalid adapter index");
445 if (pcap_findalldevs(&devpointer
, ebuf
) < 0)
448 for (i
= 0; i
< devnum
-1; i
++){
449 devpointer
= devpointer
->next
;
450 if (devpointer
== NULL
)
451 error("Invalid adapter index");
454 device
= devpointer
->name
;
457 #endif /* HAVE_PCAP_FINDALLDEVS */
462 #ifdef HAVE_SETLINEBUF
465 setvbuf(stdout
, NULL
, _IOLBF
, 0);
479 if (smiLoadModule(optarg
) == 0) {
480 error("could not load MIB module %s", optarg
);
484 (void)fprintf(stderr
, "%s: ignoring option `-m %s' ",
485 program_name
, optarg
);
486 (void)fprintf(stderr
, "(no libsmi support)\n");
512 snaplen
= strtol(optarg
, &end
, 0);
513 if (optarg
== end
|| *end
!= '\0'
514 || snaplen
< 0 || snaplen
> 65535)
515 error("invalid snaplen %s", optarg
);
516 else if (snaplen
== 0)
530 if (strcasecmp(optarg
, "vat") == 0)
532 else if (strcasecmp(optarg
, "wb") == 0)
534 else if (strcasecmp(optarg
, "rpc") == 0)
536 else if (strcasecmp(optarg
, "rtp") == 0)
538 else if (strcasecmp(optarg
, "rtcp") == 0)
539 packettype
= PT_RTCP
;
540 else if (strcasecmp(optarg
, "snmp") == 0)
541 packettype
= PT_SNMP
;
542 else if (strcasecmp(optarg
, "cnfp") == 0)
543 packettype
= PT_CNFP
;
544 else if (strcasecmp(optarg
, "tftp") == 0)
545 packettype
= PT_TFTP
;
546 else if (strcasecmp(optarg
, "aodv") == 0)
547 packettype
= PT_AODV
;
549 error("unknown packet type `%s'", optarg
);
556 #ifdef HAVE_PCAP_DUMP_FLUSH
581 dlt
= pcap_datalink_name_to_val(dlt_name
);
583 error("invalid data link type %s", dlt_name
);
586 #if defined(HAVE_PCAP_DEBUG) || defined(HAVE_YYDEBUG)
589 /* Undocumented flag */
590 #ifdef HAVE_PCAP_DEBUG
591 extern int pcap_debug
;
606 thiszone
= gmt2local(0);
608 if (RFileName
!= NULL
) {
610 const char *dlt_name
;
614 * We don't need network access, so relinquish any set-UID
615 * or set-GID privileges we have (if any).
617 * We do *not* want set-UID privileges when opening a
618 * trace file, as that might let the user read other
619 * people's trace files (especially if we're set-UID
624 pd
= pcap_open_offline(RFileName
, ebuf
);
627 dlt
= pcap_datalink(pd
);
628 dlt_name
= pcap_datalink_val_to_name(dlt
);
629 if (dlt_name
== NULL
)
631 printf("reading from file %s, link-type %u (%s)\n",
632 RFileName
, dlt
, dlt_name
);
636 error("-f and -r options are incompatible");
638 if (device
== NULL
) {
639 device
= pcap_lookupdev(ebuf
);
644 PrintCapBegins(program_name
,device
);
647 pd
= pcap_open_live(device
, snaplen
, !pflag
, 1000, ebuf
);
653 if(UserBufferSize
!= 1000000)
654 if(pcap_setbuff(pd
, UserBufferSize
)==-1){
655 error("%s", pcap_geterr(pd
));
659 show_dlts_and_exit(pd
);
661 #ifdef HAVE_PCAP_SET_DATALINK
662 if (pcap_set_datalink(pd
, dlt
) < 0)
663 error("%s", pcap_geterr(pd
));
666 * We don't actually support changing the
667 * data link type, so we only let them
668 * set it to what it already is.
670 if (dlt
!= pcap_datalink(pd
)) {
671 error("%s is not one of the DLTs supported by this device\n",
675 (void)fprintf(stderr
, "%s: data link type %s\n",
676 program_name
, dlt_name
);
677 (void)fflush(stderr
);
679 i
= pcap_snapshot(pd
);
681 warning("snaplen raised from %d to %d", snaplen
, i
);
684 if (pcap_lookupnet(device
, &localnet
, &netmask
, ebuf
) < 0) {
690 * Let user own process after socket has been opened.
697 cmdbuf
= read_infile(infile
);
699 cmdbuf
= copy_argv(&argv
[optind
]);
701 if (pcap_compile(pd
, &fcode
, cmdbuf
, Oflag
, netmask
) < 0)
702 error("%s", pcap_geterr(pd
));
704 bpf_dump(&fcode
, dflag
);
708 init_addrtoname(localnet
, netmask
);
711 (void)setsignal(SIGPIPE
, cleanup
);
713 (void)setsignal(SIGTERM
, cleanup
);
714 (void)setsignal(SIGINT
, cleanup
);
715 /* Cooperate with nohup(1) */
717 if ((oldhandler
= setsignal(SIGHUP
, cleanup
)) != SIG_DFL
)
718 (void)setsignal(SIGHUP
, oldhandler
);
721 if (pcap_setfilter(pd
, &fcode
) < 0)
722 error("%s", pcap_geterr(pd
));
724 pcap_dumper_t
*p
= pcap_dump_open(pd
, WFileName
);
726 error("%s", pcap_geterr(pd
));
728 callback
= dump_packet_and_trunc
;
729 dumpinfo
.WFileName
= WFileName
;
732 pcap_userdata
= (u_char
*)&dumpinfo
;
734 callback
= dump_packet
;
735 pcap_userdata
= (u_char
*)p
;
738 type
= pcap_datalink(pd
);
739 printinfo
.printer
= lookup_printer(type
);
740 if (printinfo
.printer
== NULL
) {
741 dlt_name
= pcap_datalink_val_to_name(type
);
742 if (dlt_name
!= NULL
)
743 error("unsupported data link type %s", dlt_name
);
745 error("unsupported data link type %d", type
);
747 callback
= print_packet
;
748 pcap_userdata
= (u_char
*)&printinfo
;
751 (void)setsignal(SIGINFO
, requestinfo
);
754 if (RFileName
== NULL
) {
756 const char *dlt_name
;
758 if (!vflag
&& !WFileName
) {
759 (void)fprintf(stderr
,
760 "%s: verbose output suppressed, use -v or -vv for full protocol decode\n",
763 (void)fprintf(stderr
, "%s: ", program_name
);
764 dlt
= pcap_datalink(pd
);
765 dlt_name
= pcap_datalink_val_to_name(dlt
);
766 if (dlt_name
== NULL
)
768 (void)fprintf(stderr
, "listening on %s, link-type %u (%s), capture size %u bytes\n",
769 device
, dlt
, dlt_name
, snaplen
);
770 (void)fflush(stderr
);
773 if (pcap_loop(pd
, cnt
, callback
, pcap_userdata
) < 0) {
774 (void)fprintf(stderr
, "%s: pcap_loop: %s\n",
775 program_name
, pcap_geterr(pd
));
780 if (RFileName
== NULL
)
786 /* make a clean exit on interrupts */
791 /* Can't print the summary if reading from a savefile */
792 if (pd
!= NULL
&& pcap_file(pd
) == NULL
) {
793 (void)fflush(stdout
);
802 info(register int verbose
)
804 struct pcap_stat stat
;
806 if (pcap_stats(pd
, &stat
) < 0) {
807 (void)fprintf(stderr
, "pcap_stats: %s\n", pcap_geterr(pd
));
812 fprintf(stderr
, "%s: ", program_name
);
814 (void)fprintf(stderr
, "%d packets received by filter", stat
.ps_recv
);
819 (void)fprintf(stderr
, "%d packets dropped by kernel\n", stat
.ps_drop
);
828 for (i
= 0, j
= strlen(s
) - 1; i
< j
; i
++, j
--) {
837 swebitoa(unsigned int n
, char *s
)
843 s
[i
++] = n
% 10 + '0';
844 } while ((n
/= 10) > 0);
851 dump_packet_and_trunc(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
853 struct dump_info
*dump_info
;
859 dump_info
= (struct dump_info
*)user
;
862 * XXX - this won't prevent capture files from getting
863 * larger than Cflag - the last packet written to the
864 * file could put it over Cflag.
866 if (ftell((FILE *)dump_info
->p
) > Cflag
) {
867 name
= (char *) malloc(strlen(dump_info
->WFileName
) + 4);
869 error("dump_packet_and_trunc: malloc");
870 strcpy(name
, dump_info
->WFileName
);
871 swebitoa(cnt
, name
+ strlen(dump_info
->WFileName
));
873 pcap_dump_close(dump_info
->p
);
874 dump_info
->p
= pcap_dump_open(dump_info
->pd
, name
);
876 if (dump_info
->p
== NULL
)
877 error("%s", pcap_geterr(pd
));
880 pcap_dump((u_char
*)dump_info
->p
, h
, sp
);
881 #ifdef HAVE_PCAP_DUMP_FLUSH
883 pcap_dump_flush(dump_info
->p
);
892 dump_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
896 pcap_dump(user
, h
, sp
);
897 #ifdef HAVE_PCAP_DUMP_FLUSH
899 pcap_dump_flush((pcap_dumper_t
*)user
);
908 print_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
910 struct print_info
*print_info
;
916 print_info
= (struct print_info
*)user
;
919 * Some printers want to check that they're not walking off the
921 * Rather than pass it all the way down, we set this global.
923 snapend
= sp
+ h
->caplen
;
925 hdrlen
= (*print_info
->printer
)(h
, sp
);
928 * Print the raw packet data.
932 * Include the link-layer header.
934 default_print_unaligned(sp
, h
->caplen
);
937 * Don't include the link-layer header - and if
938 * we have nothing past the link-layer header,
941 if (h
->caplen
> hdrlen
)
942 default_print_unaligned(sp
+ hdrlen
,
954 /* Like default_print() but data need not be aligned */
956 default_print_unaligned(register const u_char
*cp
, register u_int length
)
959 register int nshorts
;
962 ascii_print(cp
, length
);
965 nshorts
= (u_int
) length
/ sizeof(u_short
);
967 while (--nshorts
>= 0) {
969 (void)printf("\n\t\t\t");
971 (void)printf(" %02x%02x", s
, *cp
++);
975 (void)printf("\n\t\t\t");
976 (void)printf(" %02x", *cp
);
982 * XXX - there should really be libpcap calls to get the version
983 * number as a string (the string would be generated from #defines
984 * at run time, so that it's not generated from string constants
985 * in the library, as, on many UNIX systems, those constants would
986 * be statically linked into the application executable image, and
987 * would thus reflect the version of libpcap on the system on
988 * which the application was *linked*, not the system on which it's
991 * That routine should be documented, unlike the "version[]"
992 * string, so that UNIX vendors providing their own libpcaps
993 * don't omit it (as a couple of vendors have...).
995 * Packet.dll should perhaps also export a routine to return the
996 * version number of the Packet.dll code, to supply the
997 * "Wpcap_version" information on Windows.
999 char WDversion
[]="current-cvs.tcpdump.org";
1000 char version
[]="current-cvs.tcpdump.org";
1001 char pcap_version
[]="current-cvs.tcpdump.org";
1002 char Wpcap_version
[]="3.0 alpha";
1006 * By default, print the specified data out in hex.
1009 default_print(register const u_char
*bp
, register u_int length
)
1011 default_print_unaligned(bp
, length
);
1015 RETSIGTYPE
requestinfo(int signo _U_
)
1027 extern char version
[];
1028 #ifndef HAVE_PCAP_LIB_VERSION
1029 #if defined(WIN32) || defined(HAVE_PCAP_VERSION)
1030 extern char pcap_version
[];
1031 #else /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
1032 static char pcap_version
[] = "unknown";
1033 #endif /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
1034 #endif /* HAVE_PCAP_LIB_VERSION */
1036 #ifdef HAVE_PCAP_LIB_VERSION
1037 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
1038 (void)fprintf(stderr
, "%s\n", pcap_lib_version());
1039 #else /* HAVE_PCAP_LIB_VERSION */
1041 (void)fprintf(stderr
, "%s version %s, based on tcpdump version %s\n", program_name
, WDversion
, version
);
1042 (void)fprintf(stderr
, "WinPcap version %s, based on libpcap version %s\n",Wpcap_version
, pcap_version
);
1044 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
1045 (void)fprintf(stderr
, "libpcap version %s\n", pcap_version
);
1047 #endif /* HAVE_PCAP_LIB_VERSION */
1048 (void)fprintf(stderr
,
1049 "Usage: %s [-aAd" D_FLAG
"eflLnNOpqRStu" U_FLAG
"vxX]" B_FLAG_USAGE
" [-c count] [ -C file_size ]\n", program_name
);
1050 (void)fprintf(stderr
,
1051 "\t\t[ -E algo:secret ] [ -F file ] [ -i interface ] [ -r file ]\n");
1052 (void)fprintf(stderr
,
1053 "\t\t[ -s snaplen ] [ -T type ] [ -w file ] [ -y datalinktype ]\n");
1054 (void)fprintf(stderr
,
1055 "\t\t[ expression ]\n");