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.212 2003-08-01 01:01:40 fenner 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
;
547 error("unknown packet type `%s'", optarg
);
554 #ifdef HAVE_PCAP_DUMP_FLUSH
579 dlt
= pcap_datalink_name_to_val(dlt_name
);
581 error("invalid data link type %s", dlt_name
);
584 #if defined(HAVE_PCAP_DEBUG) || defined(HAVE_YYDEBUG)
587 /* Undocumented flag */
588 #ifdef HAVE_PCAP_DEBUG
589 extern int pcap_debug
;
604 thiszone
= gmt2local(0);
606 if (RFileName
!= NULL
) {
608 const char *dlt_name
;
612 * We don't need network access, so relinquish any set-UID
613 * or set-GID privileges we have (if any).
615 * We do *not* want set-UID privileges when opening a
616 * trace file, as that might let the user read other
617 * people's trace files (especially if we're set-UID
622 pd
= pcap_open_offline(RFileName
, ebuf
);
625 dlt
= pcap_datalink(pd
);
626 dlt_name
= pcap_datalink_val_to_name(dlt
);
627 if (dlt_name
== NULL
)
629 printf("reading from file %s, link-type %u (%s)\n",
630 RFileName
, dlt
, dlt_name
);
634 error("-f and -r options are incompatible");
636 if (device
== NULL
) {
637 device
= pcap_lookupdev(ebuf
);
642 PrintCapBegins(program_name
,device
);
645 pd
= pcap_open_live(device
, snaplen
, !pflag
, 1000, ebuf
);
651 if(UserBufferSize
!= 1000000)
652 if(pcap_setbuff(pd
, UserBufferSize
)==-1){
653 error("%s", pcap_geterr(pd
));
657 show_dlts_and_exit(pd
);
659 #ifdef HAVE_PCAP_SET_DATALINK
660 if (pcap_set_datalink(pd
, dlt
) < 0)
661 error("%s", pcap_geterr(pd
));
664 * We don't actually support changing the
665 * data link type, so we only let them
666 * set it to what it already is.
668 if (dlt
!= pcap_datalink(pd
)) {
669 error("%s is not one of the DLTs supported by this device\n",
673 (void)fprintf(stderr
, "%s: data link type %s\n",
674 program_name
, dlt_name
);
675 (void)fflush(stderr
);
677 i
= pcap_snapshot(pd
);
679 warning("snaplen raised from %d to %d", snaplen
, i
);
682 if (pcap_lookupnet(device
, &localnet
, &netmask
, ebuf
) < 0) {
688 * Let user own process after socket has been opened.
695 cmdbuf
= read_infile(infile
);
697 cmdbuf
= copy_argv(&argv
[optind
]);
699 if (pcap_compile(pd
, &fcode
, cmdbuf
, Oflag
, netmask
) < 0)
700 error("%s", pcap_geterr(pd
));
702 bpf_dump(&fcode
, dflag
);
706 init_addrtoname(localnet
, netmask
);
709 (void)setsignal(SIGPIPE
, cleanup
);
711 (void)setsignal(SIGTERM
, cleanup
);
712 (void)setsignal(SIGINT
, cleanup
);
713 /* Cooperate with nohup(1) */
715 if ((oldhandler
= setsignal(SIGHUP
, cleanup
)) != SIG_DFL
)
716 (void)setsignal(SIGHUP
, oldhandler
);
719 if (pcap_setfilter(pd
, &fcode
) < 0)
720 error("%s", pcap_geterr(pd
));
722 pcap_dumper_t
*p
= pcap_dump_open(pd
, WFileName
);
724 error("%s", pcap_geterr(pd
));
726 callback
= dump_packet_and_trunc
;
727 dumpinfo
.WFileName
= WFileName
;
730 pcap_userdata
= (u_char
*)&dumpinfo
;
732 callback
= dump_packet
;
733 pcap_userdata
= (u_char
*)p
;
736 type
= pcap_datalink(pd
);
737 printinfo
.printer
= lookup_printer(type
);
738 if (printinfo
.printer
== NULL
) {
739 dlt_name
= pcap_datalink_val_to_name(type
);
740 if (dlt_name
!= NULL
)
741 error("unsupported data link type %s", dlt_name
);
743 error("unsupported data link type %d", type
);
745 callback
= print_packet
;
746 pcap_userdata
= (u_char
*)&printinfo
;
749 (void)setsignal(SIGINFO
, requestinfo
);
752 if (RFileName
== NULL
) {
754 const char *dlt_name
;
756 if (!vflag
&& !WFileName
) {
757 (void)fprintf(stderr
,
758 "%s: verbose output suppressed, use -v or -vv for full protocol decode\n",
761 (void)fprintf(stderr
, "%s: ", program_name
);
762 dlt
= pcap_datalink(pd
);
763 dlt_name
= pcap_datalink_val_to_name(dlt
);
764 if (dlt_name
== NULL
)
766 (void)fprintf(stderr
, "listening on %s, link-type %u (%s), capture size %u bytes\n",
767 device
, dlt
, dlt_name
, snaplen
);
768 (void)fflush(stderr
);
771 if (pcap_loop(pd
, cnt
, callback
, pcap_userdata
) < 0) {
772 (void)fprintf(stderr
, "%s: pcap_loop: %s\n",
773 program_name
, pcap_geterr(pd
));
778 if (RFileName
== NULL
)
784 /* make a clean exit on interrupts */
789 /* Can't print the summary if reading from a savefile */
790 if (pd
!= NULL
&& pcap_file(pd
) == NULL
) {
791 (void)fflush(stdout
);
800 info(register int verbose
)
802 struct pcap_stat stat
;
804 if (pcap_stats(pd
, &stat
) < 0) {
805 (void)fprintf(stderr
, "pcap_stats: %s\n", pcap_geterr(pd
));
810 fprintf(stderr
, "%s: ", program_name
);
812 (void)fprintf(stderr
, "%d packets received by filter", stat
.ps_recv
);
817 (void)fprintf(stderr
, "%d packets dropped by kernel\n", stat
.ps_drop
);
826 for (i
= 0, j
= strlen(s
) - 1; i
< j
; i
++, j
--) {
835 swebitoa(unsigned int n
, char *s
)
841 s
[i
++] = n
% 10 + '0';
842 } while ((n
/= 10) > 0);
849 dump_packet_and_trunc(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
851 struct dump_info
*dump_info
;
857 dump_info
= (struct dump_info
*)user
;
860 * XXX - this won't prevent capture files from getting
861 * larger than Cflag - the last packet written to the
862 * file could put it over Cflag.
864 if (ftell((FILE *)dump_info
->p
) > Cflag
) {
865 name
= (char *) malloc(strlen(dump_info
->WFileName
) + 4);
867 error("dump_packet_and_trunc: malloc");
868 strcpy(name
, dump_info
->WFileName
);
869 swebitoa(cnt
, name
+ strlen(dump_info
->WFileName
));
871 pcap_dump_close(dump_info
->p
);
872 dump_info
->p
= pcap_dump_open(dump_info
->pd
, name
);
874 if (dump_info
->p
== NULL
)
875 error("%s", pcap_geterr(pd
));
878 pcap_dump((u_char
*)dump_info
->p
, h
, sp
);
879 #ifdef HAVE_PCAP_DUMP_FLUSH
881 pcap_dump_flush(dump_info
->p
);
890 dump_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
894 pcap_dump(user
, h
, sp
);
895 #ifdef HAVE_PCAP_DUMP_FLUSH
897 pcap_dump_flush((pcap_dumper_t
*)user
);
906 print_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
908 struct print_info
*print_info
;
914 print_info
= (struct print_info
*)user
;
917 * Some printers want to check that they're not walking off the
919 * Rather than pass it all the way down, we set this global.
921 snapend
= sp
+ h
->caplen
;
923 hdrlen
= (*print_info
->printer
)(h
, sp
);
926 * Print the raw packet data.
930 * Include the link-layer header.
932 default_print_unaligned(sp
, h
->caplen
);
935 * Don't include the link-layer header - and if
936 * we have nothing past the link-layer header,
939 if (h
->caplen
> hdrlen
)
940 default_print_unaligned(sp
+ hdrlen
,
952 /* Like default_print() but data need not be aligned */
954 default_print_unaligned(register const u_char
*cp
, register u_int length
)
957 register int nshorts
;
960 ascii_print(cp
, length
);
963 nshorts
= (u_int
) length
/ sizeof(u_short
);
965 while (--nshorts
>= 0) {
967 (void)printf("\n\t\t\t");
969 (void)printf(" %02x%02x", s
, *cp
++);
973 (void)printf("\n\t\t\t");
974 (void)printf(" %02x", *cp
);
980 * XXX - there should really be libpcap calls to get the version
981 * number as a string (the string would be generated from #defines
982 * at run time, so that it's not generated from string constants
983 * in the library, as, on many UNIX systems, those constants would
984 * be statically linked into the application executable image, and
985 * would thus reflect the version of libpcap on the system on
986 * which the application was *linked*, not the system on which it's
989 * That routine should be documented, unlike the "version[]"
990 * string, so that UNIX vendors providing their own libpcaps
991 * don't omit it (as a couple of vendors have...).
993 * Packet.dll should perhaps also export a routine to return the
994 * version number of the Packet.dll code, to supply the
995 * "Wpcap_version" information on Windows.
997 char WDversion
[]="current-cvs.tcpdump.org";
998 char version
[]="current-cvs.tcpdump.org";
999 char pcap_version
[]="current-cvs.tcpdump.org";
1000 char Wpcap_version
[]="3.0 alpha";
1004 * By default, print the specified data out in hex.
1007 default_print(register const u_char
*bp
, register u_int length
)
1009 default_print_unaligned(bp
, length
);
1013 RETSIGTYPE
requestinfo(int signo _U_
)
1025 extern char version
[];
1026 #ifndef HAVE_PCAP_LIB_VERSION
1027 #if defined(WIN32) || defined(HAVE_PCAP_VERSION)
1028 extern char pcap_version
[];
1029 #else /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
1030 static char pcap_version
[] = "unknown";
1031 #endif /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
1032 #endif /* HAVE_PCAP_LIB_VERSION */
1034 #ifdef HAVE_PCAP_LIB_VERSION
1035 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
1036 (void)fprintf(stderr
, "%s\n", pcap_lib_version());
1037 #else /* HAVE_PCAP_LIB_VERSION */
1039 (void)fprintf(stderr
, "%s version %s, based on tcpdump version %s\n", program_name
, WDversion
, version
);
1040 (void)fprintf(stderr
, "WinPcap version %s, based on libpcap version %s\n",Wpcap_version
, pcap_version
);
1042 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
1043 (void)fprintf(stderr
, "libpcap version %s\n", pcap_version
);
1045 #endif /* HAVE_PCAP_LIB_VERSION */
1046 (void)fprintf(stderr
,
1047 "Usage: %s [-aAd" D_FLAG
"eflLnNOpqRStu" U_FLAG
"vxX]" B_FLAG_USAGE
" [-c count] [ -C file_size ]\n", program_name
);
1048 (void)fprintf(stderr
,
1049 "\t\t[ -E algo:secret ] [ -F file ] [ -i interface ] [ -r file ]\n");
1050 (void)fprintf(stderr
,
1051 "\t\t[ -s snaplen ] [ -T type ] [ -w file ] [ -y datalinktype ]\n");
1052 (void)fprintf(stderr
,
1053 "\t\t[ expression ]\n");