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.194 2002-12-22 00:15:28 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 aflag
; /* translate network and broadcast addresses */
73 int dflag
; /* print filter code */
74 int eflag
; /* print ethernet header */
75 int fflag
; /* don't translate "foreign" IP address */
76 int Lflag
; /* list available data link types and exit */
77 int nflag
; /* leave addresses as numbers */
78 int Nflag
; /* remove domains from printed host names */
79 int Oflag
= 1; /* run filter code optimizer */
80 int pflag
; /* don't go promiscuous */
81 int qflag
; /* quick (shorter) output */
82 int Rflag
= 1; /* print sequence # field in AH/ESP*/
83 int sflag
= 0; /* use the libsmi to translate OIDs */
84 int Sflag
; /* print raw TCP sequence numbers */
85 int tflag
= 1; /* print packet arrival time */
86 int Uflag
= 0; /* "unbuffered" output of dump files */
87 int uflag
= 0; /* Print undecoded NFS handles */
88 int vflag
; /* verbose */
89 int xflag
; /* print packet in hex */
90 int Xflag
; /* print packet in ascii as well as hex */
91 off_t Cflag
= 0; /* rotate dump files after this many bytes */
92 int Aflag
= 0; /* print packet only in ascii observing LF, CR, TAB, SPACE */
93 int dlt
= -1; /* if != -1, ask libpcap for the DLT it names */
95 const char *dlt_name
= NULL
;
97 char *espsecret
= NULL
; /* ESP secret key */
101 static int infodelay
;
102 static int infoprint
;
106 int32_t thiszone
; /* seconds offset from gmt to local time */
109 static RETSIGTYPE
cleanup(int);
110 static void usage(void) __attribute__((noreturn
));
111 static void show_dlts_and_exit(pcap_t
*pd
) __attribute__((noreturn
));
113 static void print_packet(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
114 static void dump_packet_and_trunc(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
115 static void dump_packet(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
118 RETSIGTYPE
requestinfo(int);
121 static void info(int);
123 /* Length of saved portion of packet. */
124 int snaplen
= DEFAULT_SNAPLEN
;
126 typedef u_int (*if_printer
)(const struct pcap_pkthdr
*, const u_char
*);
133 static struct printer printers
[] = {
134 { arcnet_if_print
, DLT_ARCNET
},
135 { ether_if_print
, DLT_EN10MB
},
136 { token_if_print
, DLT_IEEE802
},
138 { lane_if_print
, DLT_LANE8023
},
141 { cip_if_print
, DLT_CIP
},
144 { cip_if_print
, DLT_ATM_CLIP
},
146 { sl_if_print
, DLT_SLIP
},
147 { sl_bsdos_if_print
, DLT_SLIP_BSDOS
},
148 { ppp_if_print
, DLT_PPP
},
149 { ppp_bsdos_if_print
, DLT_PPP_BSDOS
},
150 { fddi_if_print
, DLT_FDDI
},
151 { null_if_print
, DLT_NULL
},
153 { null_if_print
, DLT_LOOP
},
155 { raw_if_print
, DLT_RAW
},
156 { atm_if_print
, DLT_ATM_RFC1483
},
158 { chdlc_if_print
, DLT_C_HDLC
},
161 { chdlc_if_print
, DLT_HDLC
},
163 #ifdef DLT_PPP_SERIAL
164 { ppp_hdlc_if_print
, DLT_PPP_SERIAL
},
167 { pppoe_if_print
, DLT_PPP_ETHER
},
170 { sll_if_print
, DLT_LINUX_SLL
},
172 #ifdef DLT_IEEE802_11
173 { ieee802_11_if_print
, DLT_IEEE802_11
},
176 { ltalk_if_print
, DLT_LTALK
},
179 { pflog_if_print
, DLT_PFLOG
},
182 { fr_if_print
, DLT_FR
},
185 { fr_if_print
, DLT_FRELAY
},
188 { sunatm_if_print
, DLT_SUNATM
},
190 #ifdef DLT_IP_OVER_FC
191 { ipfc_if_print
, DLT_IP_OVER_FC
},
193 #ifdef DLT_PRISM_HEADER
194 { prism_if_print
, DLT_PRISM_HEADER
},
196 #ifdef DLT_IEEE802_11_RADIO
197 { ieee802_11_radio_if_print
, DLT_IEEE802_11_RADIO
},
203 lookup_printer(int type
)
207 for (p
= printers
; p
->f
; ++p
)
232 show_dlts_and_exit(pcap_t
*pd
)
236 const char *dlt_name
;
238 n_dlts
= pcap_list_datalinks(pd
, &dlts
);
240 error("%s", pcap_geterr(pd
));
241 else if (n_dlts
== 0 || !dlts
)
242 error("No data link types.");
244 (void) fprintf(stderr
, "Data link types (use option -y to set):\n");
246 while (--n_dlts
>= 0) {
247 dlt_name
= pcap_datalink_val_to_name(dlts
[n_dlts
]);
248 if (dlt_name
!= NULL
) {
249 (void) fprintf(stderr
, " %s", dlt_name
);
252 * OK, does tcpdump handle that type?
254 if (lookup_printer(dlts
[n_dlts
]) == NULL
)
255 (void) fprintf(stderr
, " (not supported)");
258 (void) fprintf(stderr
, " DLT %d (not supported)\n",
267 * Set up flags that might or might not be supported depending on the
268 * version of libpcap we're using.
270 * Win32 builds are assumed to be done with the latest version of WinPcap.
274 #define B_FLAG_USAGE " [ -B size ]"
282 #ifdef HAVE_PCAP_FINDALLDEVS
288 #ifdef HAVE_PCAP_DUMP_FLUSH
297 main(int argc
, char **argv
)
299 register int cnt
, op
, i
;
300 bpf_u_int32 localnet
, netmask
;
301 register char *cp
, *infile
, *cmdbuf
, *device
, *RFileName
, *WFileName
;
302 pcap_handler callback
;
304 struct bpf_program fcode
;
306 RETSIGTYPE (*oldhandler
)(int);
308 struct print_info printinfo
;
309 struct dump_info dumpinfo
;
310 u_char
*pcap_userdata
;
311 char ebuf
[PCAP_ERRBUF_SIZE
];
312 #ifdef HAVE_PCAP_FINDALLDEVS
313 pcap_if_t
*devpointer
;
318 DWORD dwWindowsMajorVersion
;
319 u_int UserBufferSize
=1000000;
323 dwVersion
=GetVersion(); /* get the OS version */
324 dwWindowsMajorVersion
= (DWORD
)(LOBYTE(LOWORD(dwVersion
)));
325 if(wsockinit()!=0) return 1;
333 if ((cp
= strrchr(argv
[0], '/')) != NULL
)
334 program_name
= cp
+ 1;
336 program_name
= argv
[0];
338 if (abort_on_misalignment(ebuf
, sizeof(ebuf
)) < 0)
347 (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)
362 UserBufferSize
= atoi(optarg
)*1024;
363 if (UserBufferSize
< 0)
364 error("invalid packet buffer size %s", optarg
);
371 error("invalid packet count %s", optarg
);
375 Cflag
= atoi(optarg
) * 1000000;
377 error("invalid file size %s", optarg
);
384 #ifdef HAVE_PCAP_FINDALLDEVS
386 if (pcap_findalldevs(&devpointer
, ebuf
) < 0)
389 for (i
= 0; devpointer
!= 0; i
++) {
390 printf("%d.%s", i
+1, devpointer
->name
);
391 if (devpointer
->description
!= NULL
)
392 printf(" (%s)", devpointer
->description
);
394 devpointer
= devpointer
->next
;
398 #endif /* HAVE_PCAP_FINDALLDEVS */
409 #ifndef HAVE_LIBCRYPTO
410 warning("crypto code not compiled in");
424 if (optarg
[0] == '0' && optarg
[1] == 0)
425 error("Invalid adapter index");
427 #ifdef HAVE_PCAP_FINDALLDEVS
429 * If the argument is a number, treat it as
430 * an index into the list of adapters, as
431 * printed by "tcpdump -D".
433 * This should be OK on UNIX systems, as interfaces
434 * shouldn't have names that begin with digits.
435 * It can be useful on Windows, where more than
436 * one interface can have the same name.
438 if ((devnum
= atoi(optarg
)) != 0) {
440 error("Invalid adapter index");
442 if (pcap_findalldevs(&devpointer
, ebuf
) < 0)
445 for (i
= 0; i
< devnum
-1; i
++){
446 devpointer
= devpointer
->next
;
447 if (devpointer
== NULL
)
448 error("Invalid adapter index");
451 device
= devpointer
->name
;
454 #endif /* HAVE_PCAP_FINDALLDEVS */
459 #ifdef HAVE_SETLINEBUF
462 setvbuf(stdout
, NULL
, _IOLBF
, 0);
476 if (smiLoadModule(optarg
) == 0) {
477 error("could not load MIB module %s", optarg
);
481 (void)fprintf(stderr
, "%s: ignoring option `-m %s' ",
482 program_name
, optarg
);
483 (void)fprintf(stderr
, "(no libsmi support)\n");
509 snaplen
= strtol(optarg
, &end
, 0);
510 if (optarg
== end
|| *end
!= '\0'
511 || snaplen
< 0 || snaplen
> 65535)
512 error("invalid snaplen %s", optarg
);
513 else if (snaplen
== 0)
527 if (strcasecmp(optarg
, "vat") == 0)
529 else if (strcasecmp(optarg
, "wb") == 0)
531 else if (strcasecmp(optarg
, "rpc") == 0)
533 else if (strcasecmp(optarg
, "rtp") == 0)
535 else if (strcasecmp(optarg
, "rtcp") == 0)
536 packettype
= PT_RTCP
;
537 else if (strcasecmp(optarg
, "snmp") == 0)
538 packettype
= PT_SNMP
;
539 else if (strcasecmp(optarg
, "cnfp") == 0)
540 packettype
= PT_CNFP
;
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 error("-a and -n options are incompatible");
602 thiszone
= gmt2local(0);
604 if (RFileName
!= NULL
) {
606 * We don't need network access, so set it back to the user id.
607 * Also, this prevents the user from reading anyone's
614 pd
= pcap_open_offline(RFileName
, ebuf
);
620 error("-f and -r options are incompatible");
622 if (device
== NULL
) {
623 device
= pcap_lookupdev(ebuf
);
628 PrintCapBegins(program_name
,device
);
631 pd
= pcap_open_live(device
, snaplen
, !pflag
, 1000, ebuf
);
637 if(UserBufferSize
!= 1000000)
638 if(pcap_setbuff(pd
, UserBufferSize
)==-1){
639 error("%s", pcap_geterr(pd
));
643 show_dlts_and_exit(pd
);
645 #ifdef HAVE_PCAP_SET_DATALINK
646 if (pcap_set_datalink(pd
, dlt
) < 0)
647 error("%s", pcap_geterr(pd
));
650 * We don't actually support changing the
651 * data link type, so we only let them
652 * set it to what it already is.
654 if (dlt
!= pcap_datalink(pd
)) {
655 error("%s is not one of the DLTs supported by this device\n",
659 (void)fprintf(stderr
, "%s: data link type %s\n",
660 program_name
, dlt_name
);
661 (void)fflush(stderr
);
663 i
= pcap_snapshot(pd
);
665 warning("snaplen raised from %d to %d", snaplen
, i
);
668 if (pcap_lookupnet(device
, &localnet
, &netmask
, ebuf
) < 0) {
674 * Let user own process after socket has been opened.
681 cmdbuf
= read_infile(infile
);
683 cmdbuf
= copy_argv(&argv
[optind
]);
685 if (pcap_compile(pd
, &fcode
, cmdbuf
, Oflag
, netmask
) < 0)
686 error("%s", pcap_geterr(pd
));
688 bpf_dump(&fcode
, dflag
);
692 init_addrtoname(localnet
, netmask
);
694 (void)setsignal(SIGTERM
, cleanup
);
695 (void)setsignal(SIGINT
, cleanup
);
696 /* Cooperate with nohup(1) */
698 if ((oldhandler
= setsignal(SIGHUP
, cleanup
)) != SIG_DFL
)
699 (void)setsignal(SIGHUP
, oldhandler
);
702 if (pcap_setfilter(pd
, &fcode
) < 0)
703 error("%s", pcap_geterr(pd
));
705 pcap_dumper_t
*p
= pcap_dump_open(pd
, WFileName
);
707 error("%s", pcap_geterr(pd
));
709 callback
= dump_packet_and_trunc
;
710 dumpinfo
.WFileName
= WFileName
;
713 pcap_userdata
= (u_char
*)&dumpinfo
;
715 callback
= dump_packet
;
716 pcap_userdata
= (u_char
*)p
;
719 type
= pcap_datalink(pd
);
720 printinfo
.printer
= lookup_printer(type
);
721 if (printinfo
.printer
== NULL
) {
722 dlt_name
= pcap_datalink_val_to_name(type
);
723 if (dlt_name
!= NULL
)
724 error("unsupported data link type %s", dlt_name
);
726 error("unsupported data link type %d", type
);
728 callback
= print_packet
;
729 pcap_userdata
= (u_char
*)&printinfo
;
732 (void)setsignal(SIGINFO
, requestinfo
);
735 if (RFileName
== NULL
) {
736 (void)fprintf(stderr
, "%s: listening on %s\n",
737 program_name
, device
);
738 (void)fflush(stderr
);
741 if (pcap_loop(pd
, cnt
, callback
, pcap_userdata
) < 0) {
742 (void)fprintf(stderr
, "%s: pcap_loop: %s\n",
743 program_name
, pcap_geterr(pd
));
748 if (RFileName
== NULL
)
754 /* make a clean exit on interrupts */
759 /* Can't print the summary if reading from a savefile */
760 if (pd
!= NULL
&& pcap_file(pd
) == NULL
) {
761 (void)fflush(stdout
);
770 info(register int verbose
)
772 struct pcap_stat stat
;
774 if (pcap_stats(pd
, &stat
) < 0) {
775 (void)fprintf(stderr
, "pcap_stats: %s\n", pcap_geterr(pd
));
780 fprintf(stderr
, "%s: ", program_name
);
782 (void)fprintf(stderr
, "%d packets received by filter", stat
.ps_recv
);
787 (void)fprintf(stderr
, "%d packets dropped by kernel\n", stat
.ps_drop
);
796 for (i
= 0, j
= strlen(s
) - 1; i
< j
; i
++, j
--) {
805 swebitoa(unsigned int n
, char *s
)
811 s
[i
++] = n
% 10 + '0';
812 } while ((n
/= 10) > 0);
819 dump_packet_and_trunc(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
821 struct dump_info
*dump_info
;
827 dump_info
= (struct dump_info
*)user
;
830 * XXX - this won't prevent capture files from getting
831 * larger than Cflag - the last packet written to the
832 * file could put it over Cflag.
834 if (ftell((FILE *)dump_info
->p
) > Cflag
) {
835 name
= (char *) malloc(strlen(dump_info
->WFileName
) + 4);
837 error("dump_packet_and_trunc: malloc");
838 strcpy(name
, dump_info
->WFileName
);
839 swebitoa(cnt
, name
+ strlen(dump_info
->WFileName
));
841 pcap_dump_close(dump_info
->p
);
842 dump_info
->p
= pcap_dump_open(dump_info
->pd
, name
);
844 if (dump_info
->p
== NULL
)
845 error("%s", pcap_geterr(pd
));
848 pcap_dump((u_char
*)dump_info
->p
, h
, sp
);
849 #ifdef HAVE_PCAP_FINDALLDEVS
851 pcap_dump_flush(dump_info
->p
);
860 dump_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
864 pcap_dump(user
, h
, sp
);
865 #ifdef HAVE_PCAP_FINDALLDEVS
867 pcap_dump_flush((pcap_dumper_t
*)user
);
876 print_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
878 struct print_info
*print_info
;
884 print_info
= (struct print_info
*)user
;
887 * Some printers want to check that they're not walking off the
889 * Rather than pass it all the way down, we set this global.
891 snapend
= sp
+ h
->caplen
;
893 hdrlen
= (*print_info
->printer
)(h
, sp
);
896 * Print the raw packet data.
900 * Include the link-layer header.
902 default_print_unaligned(sp
, h
->caplen
);
905 * Don't include the link-layer header - and if
906 * we have nothing past the link-layer header,
909 if (h
->caplen
> hdrlen
)
910 default_print_unaligned(sp
+ hdrlen
,
922 /* Like default_print() but data need not be aligned */
924 default_print_unaligned(register const u_char
*cp
, register u_int length
)
927 register int nshorts
;
930 ascii_print(cp
, length
);
933 nshorts
= (u_int
) length
/ sizeof(u_short
);
935 while (--nshorts
>= 0) {
937 (void)printf("\n\t\t\t");
939 (void)printf(" %02x%02x", s
, *cp
++);
943 (void)printf("\n\t\t\t");
944 (void)printf(" %02x", *cp
);
950 * XXX - there should really be libpcap calls to get the version
951 * number as a string (the string would be generated from #defines
952 * at run time, so that it's not generated from string constants
953 * in the library, as, on many UNIX systems, those constants would
954 * be statically linked into the application executable image, and
955 * would thus reflect the version of libpcap on the system on
956 * which the application was *linked*, not the system on which it's
959 * That routine should be documented, unlike the "version[]"
960 * string, so that UNIX vendors providing their own libpcaps
961 * don't omit it (as a couple of vendors have...).
963 * Packet.dll should perhaps also export a routine to return the
964 * version number of the Packet.dll code, to supply the
965 * "Wpcap_version" information on Windows.
967 char WDversion
[]="current-cvs.tcpdump.org";
968 char version
[]="current-cvs.tcpdump.org";
969 char pcap_version
[]="current-cvs.tcpdump.org";
970 char Wpcap_version
[]="3.0 alpha";
974 * By default, print the specified data out in hex.
977 default_print(register const u_char
*bp
, register u_int length
)
979 default_print_unaligned(bp
, length
);
983 RETSIGTYPE
requestinfo(int signo _U_
)
995 extern char version
[];
996 #if defined(WIN32) || defined(HAVE_PCAP_VERSION)
997 extern char pcap_version
[];
999 static char pcap_version
[] = "unknown";
1003 (void)fprintf(stderr
, "%s version %s, based on tcpdump version %s\n", program_name
, WDversion
, version
);
1004 (void)fprintf(stderr
, "WinPcap version %s, based on libpcap version %s\n",Wpcap_version
, pcap_version
);
1006 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
1007 (void)fprintf(stderr
, "libpcap version %s\n", pcap_version
);
1009 (void)fprintf(stderr
,
1010 "Usage: %s [-aAd" D_FLAG
"eflLnNOpqRStu" U_FLAG
"vxXy]" B_FLAG_USAGE
" [-c count] [ -C file_size ]\n", program_name
);
1011 (void)fprintf(stderr
,
1012 "\t\t[ -E algo:secret ] [ -F file ] [ -i interface ] [ -r file ]\n");
1013 (void)fprintf(stderr
,
1014 "\t\t[ -s snaplen ] [ -T type ] [ -w file ] [ -y datalinktype ]\n");
1015 (void)fprintf(stderr
,
1016 "\t\t[ expression ]\n");