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.198 2003-01-07 09:56:00 hannes 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.
272 #define B_FLAG_USAGE " [ -B size ]"
278 #ifdef HAVE_PCAP_FINDALLDEVS
284 #ifdef HAVE_PCAP_DUMP_FLUSH
291 main(int argc
, char **argv
)
293 register int cnt
, op
, i
;
294 bpf_u_int32 localnet
, netmask
;
295 register char *cp
, *infile
, *cmdbuf
, *device
, *RFileName
, *WFileName
;
296 pcap_handler callback
;
298 struct bpf_program fcode
;
300 RETSIGTYPE (*oldhandler
)(int);
302 struct print_info printinfo
;
303 struct dump_info dumpinfo
;
304 u_char
*pcap_userdata
;
305 char ebuf
[PCAP_ERRBUF_SIZE
];
306 #ifdef HAVE_PCAP_FINDALLDEVS
307 pcap_if_t
*devpointer
;
312 DWORD dwWindowsMajorVersion
;
313 u_int UserBufferSize
=1000000;
317 dwVersion
=GetVersion(); /* get the OS version */
318 dwWindowsMajorVersion
= (DWORD
)(LOBYTE(LOWORD(dwVersion
)));
319 if(wsockinit()!=0) return 1;
327 if ((cp
= strrchr(argv
[0], '/')) != NULL
)
328 program_name
= cp
+ 1;
330 program_name
= argv
[0];
332 if (abort_on_misalignment(ebuf
, sizeof(ebuf
)) < 0)
341 (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)
356 UserBufferSize
= atoi(optarg
)*1024;
357 if (UserBufferSize
< 0)
358 error("invalid packet buffer size %s", optarg
);
365 error("invalid packet count %s", optarg
);
369 Cflag
= atoi(optarg
) * 1000000;
371 error("invalid file size %s", optarg
);
378 #ifdef HAVE_PCAP_FINDALLDEVS
380 if (pcap_findalldevs(&devpointer
, ebuf
) < 0)
383 for (i
= 0; devpointer
!= 0; i
++) {
384 printf("%d.%s", i
+1, devpointer
->name
);
385 if (devpointer
->description
!= NULL
)
386 printf(" (%s)", devpointer
->description
);
388 devpointer
= devpointer
->next
;
392 #endif /* HAVE_PCAP_FINDALLDEVS */
403 #ifndef HAVE_LIBCRYPTO
404 warning("crypto code not compiled in");
418 if (optarg
[0] == '0' && optarg
[1] == 0)
419 error("Invalid adapter index");
421 #ifdef HAVE_PCAP_FINDALLDEVS
423 * If the argument is a number, treat it as
424 * an index into the list of adapters, as
425 * printed by "tcpdump -D".
427 * This should be OK on UNIX systems, as interfaces
428 * shouldn't have names that begin with digits.
429 * It can be useful on Windows, where more than
430 * one interface can have the same name.
432 if ((devnum
= atoi(optarg
)) != 0) {
434 error("Invalid adapter index");
436 if (pcap_findalldevs(&devpointer
, ebuf
) < 0)
439 for (i
= 0; i
< devnum
-1; i
++){
440 devpointer
= devpointer
->next
;
441 if (devpointer
== NULL
)
442 error("Invalid adapter index");
445 device
= devpointer
->name
;
448 #endif /* HAVE_PCAP_FINDALLDEVS */
453 #ifdef HAVE_SETLINEBUF
456 setvbuf(stdout
, NULL
, _IOLBF
, 0);
470 if (smiLoadModule(optarg
) == 0) {
471 error("could not load MIB module %s", optarg
);
475 (void)fprintf(stderr
, "%s: ignoring option `-m %s' ",
476 program_name
, optarg
);
477 (void)fprintf(stderr
, "(no libsmi support)\n");
503 snaplen
= strtol(optarg
, &end
, 0);
504 if (optarg
== end
|| *end
!= '\0'
505 || snaplen
< 0 || snaplen
> 65535)
506 error("invalid snaplen %s", optarg
);
507 else if (snaplen
== 0)
521 if (strcasecmp(optarg
, "vat") == 0)
523 else if (strcasecmp(optarg
, "wb") == 0)
525 else if (strcasecmp(optarg
, "rpc") == 0)
527 else if (strcasecmp(optarg
, "rtp") == 0)
529 else if (strcasecmp(optarg
, "rtcp") == 0)
530 packettype
= PT_RTCP
;
531 else if (strcasecmp(optarg
, "snmp") == 0)
532 packettype
= PT_SNMP
;
533 else if (strcasecmp(optarg
, "cnfp") == 0)
534 packettype
= PT_CNFP
;
536 error("unknown packet type `%s'", optarg
);
543 #ifdef HAVE_PCAP_DUMP_FLUSH
568 dlt
= pcap_datalink_name_to_val(dlt_name
);
570 error("invalid data link type %s", dlt_name
);
573 #if defined(HAVE_PCAP_DEBUG) || defined(HAVE_YYDEBUG)
576 /* Undocumented flag */
577 #ifdef HAVE_PCAP_DEBUG
578 extern int pcap_debug
;
593 error("-a and -n options are incompatible");
596 thiszone
= gmt2local(0);
598 if (RFileName
!= NULL
) {
600 * We don't need network access, so set it back to the user id.
601 * Also, this prevents the user from reading anyone's
608 pd
= pcap_open_offline(RFileName
, ebuf
);
614 error("-f and -r options are incompatible");
616 if (device
== NULL
) {
617 device
= pcap_lookupdev(ebuf
);
622 PrintCapBegins(program_name
,device
);
625 pd
= pcap_open_live(device
, snaplen
, !pflag
, 1000, ebuf
);
631 if(UserBufferSize
!= 1000000)
632 if(pcap_setbuff(pd
, UserBufferSize
)==-1){
633 error("%s", pcap_geterr(pd
));
637 show_dlts_and_exit(pd
);
639 #ifdef HAVE_PCAP_SET_DATALINK
640 if (pcap_set_datalink(pd
, dlt
) < 0)
641 error("%s", pcap_geterr(pd
));
644 * We don't actually support changing the
645 * data link type, so we only let them
646 * set it to what it already is.
648 if (dlt
!= pcap_datalink(pd
)) {
649 error("%s is not one of the DLTs supported by this device\n",
653 (void)fprintf(stderr
, "%s: data link type %s\n",
654 program_name
, dlt_name
);
655 (void)fflush(stderr
);
657 i
= pcap_snapshot(pd
);
659 warning("snaplen raised from %d to %d", snaplen
, i
);
662 if (pcap_lookupnet(device
, &localnet
, &netmask
, ebuf
) < 0) {
668 * Let user own process after socket has been opened.
675 cmdbuf
= read_infile(infile
);
677 cmdbuf
= copy_argv(&argv
[optind
]);
679 if (pcap_compile(pd
, &fcode
, cmdbuf
, Oflag
, netmask
) < 0)
680 error("%s", pcap_geterr(pd
));
682 bpf_dump(&fcode
, dflag
);
686 init_addrtoname(localnet
, netmask
);
688 (void)setsignal(SIGTERM
, cleanup
);
689 (void)setsignal(SIGINT
, cleanup
);
690 /* Cooperate with nohup(1) */
692 if ((oldhandler
= setsignal(SIGHUP
, cleanup
)) != SIG_DFL
)
693 (void)setsignal(SIGHUP
, oldhandler
);
696 if (pcap_setfilter(pd
, &fcode
) < 0)
697 error("%s", pcap_geterr(pd
));
699 pcap_dumper_t
*p
= pcap_dump_open(pd
, WFileName
);
701 error("%s", pcap_geterr(pd
));
703 callback
= dump_packet_and_trunc
;
704 dumpinfo
.WFileName
= WFileName
;
707 pcap_userdata
= (u_char
*)&dumpinfo
;
709 callback
= dump_packet
;
710 pcap_userdata
= (u_char
*)p
;
713 type
= pcap_datalink(pd
);
714 printinfo
.printer
= lookup_printer(type
);
715 if (printinfo
.printer
== NULL
) {
716 dlt_name
= pcap_datalink_val_to_name(type
);
717 if (dlt_name
!= NULL
)
718 error("unsupported data link type %s", dlt_name
);
720 error("unsupported data link type %d", type
);
722 callback
= print_packet
;
723 pcap_userdata
= (u_char
*)&printinfo
;
726 (void)setsignal(SIGINFO
, requestinfo
);
729 if (RFileName
== NULL
) {
731 (void)fprintf(stderr
, "%s: verbose output suppressed, use -v or -vv for full protocol decode\n",program_name
);
733 (void)fprintf(stderr
, "%s: ",program_name
);
734 (void)fprintf(stderr
, "listening on %s, capture size %u bytes\n", device
, snaplen
);
735 (void)fflush(stderr
);
738 if (pcap_loop(pd
, cnt
, callback
, pcap_userdata
) < 0) {
739 (void)fprintf(stderr
, "%s: pcap_loop: %s\n",
740 program_name
, pcap_geterr(pd
));
745 if (RFileName
== NULL
)
751 /* make a clean exit on interrupts */
756 /* Can't print the summary if reading from a savefile */
757 if (pd
!= NULL
&& pcap_file(pd
) == NULL
) {
758 (void)fflush(stdout
);
767 info(register int verbose
)
769 struct pcap_stat stat
;
771 if (pcap_stats(pd
, &stat
) < 0) {
772 (void)fprintf(stderr
, "pcap_stats: %s\n", pcap_geterr(pd
));
777 fprintf(stderr
, "%s: ", program_name
);
779 (void)fprintf(stderr
, "%d packets received by filter", stat
.ps_recv
);
784 (void)fprintf(stderr
, "%d packets dropped by kernel\n", stat
.ps_drop
);
793 for (i
= 0, j
= strlen(s
) - 1; i
< j
; i
++, j
--) {
802 swebitoa(unsigned int n
, char *s
)
808 s
[i
++] = n
% 10 + '0';
809 } while ((n
/= 10) > 0);
816 dump_packet_and_trunc(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
818 struct dump_info
*dump_info
;
824 dump_info
= (struct dump_info
*)user
;
827 * XXX - this won't prevent capture files from getting
828 * larger than Cflag - the last packet written to the
829 * file could put it over Cflag.
831 if (ftell((FILE *)dump_info
->p
) > Cflag
) {
832 name
= (char *) malloc(strlen(dump_info
->WFileName
) + 4);
834 error("dump_packet_and_trunc: malloc");
835 strcpy(name
, dump_info
->WFileName
);
836 swebitoa(cnt
, name
+ strlen(dump_info
->WFileName
));
838 pcap_dump_close(dump_info
->p
);
839 dump_info
->p
= pcap_dump_open(dump_info
->pd
, name
);
841 if (dump_info
->p
== NULL
)
842 error("%s", pcap_geterr(pd
));
845 pcap_dump((u_char
*)dump_info
->p
, h
, sp
);
846 #ifdef HAVE_PCAP_DUMP_FLUSH
848 pcap_dump_flush(dump_info
->p
);
857 dump_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
861 pcap_dump(user
, h
, sp
);
862 #ifdef HAVE_PCAP_DUMP_FLUSH
864 pcap_dump_flush((pcap_dumper_t
*)user
);
873 print_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
875 struct print_info
*print_info
;
881 print_info
= (struct print_info
*)user
;
884 * Some printers want to check that they're not walking off the
886 * Rather than pass it all the way down, we set this global.
888 snapend
= sp
+ h
->caplen
;
890 hdrlen
= (*print_info
->printer
)(h
, sp
);
893 * Print the raw packet data.
897 * Include the link-layer header.
899 default_print_unaligned(sp
, h
->caplen
);
902 * Don't include the link-layer header - and if
903 * we have nothing past the link-layer header,
906 if (h
->caplen
> hdrlen
)
907 default_print_unaligned(sp
+ hdrlen
,
919 /* Like default_print() but data need not be aligned */
921 default_print_unaligned(register const u_char
*cp
, register u_int length
)
924 register int nshorts
;
927 ascii_print(cp
, length
);
930 nshorts
= (u_int
) length
/ sizeof(u_short
);
932 while (--nshorts
>= 0) {
934 (void)printf("\n\t\t\t");
936 (void)printf(" %02x%02x", s
, *cp
++);
940 (void)printf("\n\t\t\t");
941 (void)printf(" %02x", *cp
);
947 * XXX - there should really be libpcap calls to get the version
948 * number as a string (the string would be generated from #defines
949 * at run time, so that it's not generated from string constants
950 * in the library, as, on many UNIX systems, those constants would
951 * be statically linked into the application executable image, and
952 * would thus reflect the version of libpcap on the system on
953 * which the application was *linked*, not the system on which it's
956 * That routine should be documented, unlike the "version[]"
957 * string, so that UNIX vendors providing their own libpcaps
958 * don't omit it (as a couple of vendors have...).
960 * Packet.dll should perhaps also export a routine to return the
961 * version number of the Packet.dll code, to supply the
962 * "Wpcap_version" information on Windows.
964 char WDversion
[]="current-cvs.tcpdump.org";
965 char version
[]="current-cvs.tcpdump.org";
966 char pcap_version
[]="current-cvs.tcpdump.org";
967 char Wpcap_version
[]="3.0 alpha";
971 * By default, print the specified data out in hex.
974 default_print(register const u_char
*bp
, register u_int length
)
976 default_print_unaligned(bp
, length
);
980 RETSIGTYPE
requestinfo(int signo _U_
)
992 extern char version
[];
993 #if defined(WIN32) || defined(HAVE_PCAP_VERSION)
994 extern char pcap_version
[];
996 static char pcap_version
[] = "unknown";
1000 (void)fprintf(stderr
, "%s version %s, based on tcpdump version %s\n", program_name
, WDversion
, version
);
1001 (void)fprintf(stderr
, "WinPcap version %s, based on libpcap version %s\n",Wpcap_version
, pcap_version
);
1003 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
1004 (void)fprintf(stderr
, "libpcap version %s\n", pcap_version
);
1006 (void)fprintf(stderr
,
1007 "Usage: %s [-aAd" D_FLAG
"eflLnNOpqRStu" U_FLAG
"vxXy]" B_FLAG_USAGE
" [-c count] [ -C file_size ]\n", program_name
);
1008 (void)fprintf(stderr
,
1009 "\t\t[ -E algo:secret ] [ -F file ] [ -i interface ] [ -r file ]\n");
1010 (void)fprintf(stderr
,
1011 "\t\t[ -s snaplen ] [ -T type ] [ -w file ] [ -y datalinktype ]\n");
1012 (void)fprintf(stderr
,
1013 "\t\t[ expression ]\n");