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.192 2002-12-19 09:27:57 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; /* 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 */
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 dump_packet_and_trunc(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
113 static void dump_packet(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
116 RETSIGTYPE
requestinfo(int);
119 /* Length of saved portion of packet. */
120 int snaplen
= DEFAULT_SNAPLEN
;
127 static struct printer printers
[] = {
128 { arcnet_if_print
, DLT_ARCNET
},
129 { ether_if_print
, DLT_EN10MB
},
130 { token_if_print
, DLT_IEEE802
},
132 { lane_if_print
, DLT_LANE8023
},
135 { cip_if_print
, DLT_CIP
},
138 { cip_if_print
, DLT_ATM_CLIP
},
140 { sl_if_print
, DLT_SLIP
},
141 { sl_bsdos_if_print
, DLT_SLIP_BSDOS
},
142 { ppp_if_print
, DLT_PPP
},
143 { ppp_bsdos_if_print
, DLT_PPP_BSDOS
},
144 { fddi_if_print
, DLT_FDDI
},
145 { null_if_print
, DLT_NULL
},
147 { null_if_print
, DLT_LOOP
},
149 { raw_if_print
, DLT_RAW
},
150 { atm_if_print
, DLT_ATM_RFC1483
},
152 { chdlc_if_print
, DLT_C_HDLC
},
155 { chdlc_if_print
, DLT_HDLC
},
157 #ifdef DLT_PPP_SERIAL
158 { ppp_hdlc_if_print
, DLT_PPP_SERIAL
},
161 { pppoe_if_print
, DLT_PPP_ETHER
},
164 { sll_if_print
, DLT_LINUX_SLL
},
166 #ifdef DLT_IEEE802_11
167 { ieee802_11_if_print
, DLT_IEEE802_11
},
170 { ltalk_if_print
, DLT_LTALK
},
173 { pflog_if_print
, DLT_PFLOG
},
176 { fr_if_print
, DLT_FR
},
179 { fr_if_print
, DLT_FRELAY
},
182 { sunatm_if_print
, DLT_SUNATM
},
184 #ifdef DLT_IP_OVER_FC
185 { ipfc_if_print
, DLT_IP_OVER_FC
},
187 #ifdef DLT_PRISM_HEADER
188 { prism_if_print
, DLT_PRISM_HEADER
},
190 #ifdef DLT_IEEE802_11_RADIO
191 { ieee802_11_radio_if_print
, DLT_IEEE802_11_RADIO
},
197 lookup_printer(int type
)
201 for (p
= printers
; p
->f
; ++p
)
222 show_dlts_and_exit(pcap_t
*pd
)
226 const char *dlt_name
;
228 n_dlts
= pcap_list_datalinks(pd
, &dlts
);
230 error("%s", pcap_geterr(pd
));
231 else if (n_dlts
== 0 || !dlts
)
232 error("No data link types.");
234 (void) fprintf(stderr
, "Data link types (use option -y to set):\n");
236 while (--n_dlts
>= 0) {
237 dlt_name
= pcap_datalink_val_to_name(dlts
[n_dlts
]);
238 if (dlt_name
!= NULL
) {
239 (void) fprintf(stderr
, " %s", dlt_name
);
242 * OK, does tcpdump handle that type?
244 if (lookup_printer(dlts
[n_dlts
]) == NULL
)
245 (void) fprintf(stderr
, " (not supported)");
248 (void) fprintf(stderr
, " DLT %d (not supported)\n",
257 main(int argc
, char **argv
)
259 register int cnt
, op
, i
;
260 bpf_u_int32 localnet
, netmask
;
261 register char *cp
, *infile
, *cmdbuf
, *device
, *RFileName
, *WFileName
;
263 pcap_handler printer
;
264 struct bpf_program fcode
;
266 RETSIGTYPE (*oldhandler
)(int);
268 struct dump_info dumpinfo
;
269 u_char
*pcap_userdata
;
270 char ebuf
[PCAP_ERRBUF_SIZE
];
271 #ifdef HAVE_PCAP_FINDALLDEVS
272 pcap_if_t
*devpointer
;
277 DWORD dwWindowsMajorVersion
;
278 u_int UserBufferSize
=1000000;
282 dwVersion
=GetVersion(); /* get the OS version */
283 dwWindowsMajorVersion
= (DWORD
)(LOBYTE(LOWORD(dwVersion
)));
284 if(wsockinit()!=0) return 1;
292 if ((cp
= strrchr(argv
[0], '/')) != NULL
)
293 program_name
= cp
+ 1;
295 program_name
= argv
[0];
297 if (abort_on_misalignment(ebuf
, sizeof(ebuf
)) < 0)
307 (op
= getopt(argc
, argv
, "aAB:c:C:dDeE:fF:i:lLm:nNOpqr:Rs:StT:uvw:xXy:Y")) != -1)
309 #ifdef HAVE_PCAP_FINDALLDEVS
310 (op
= getopt(argc
, argv
, "aAc:C:dDeE:fF:i:lLm:nNOpqr:Rs:StT:uvw:xXy:Y")) != -1)
311 #else /* HAVE_PCAP_FINDALLDEVS */
312 (op
= getopt(argc
, argv
, "aAc:C:deE:fF:i:lLm:nNOpqr:Rs:StT:uvw:xXy:Y")) != -1)
313 #endif /* HAVE_PCAP_FINDALLDEVS */
329 UserBufferSize
= atoi(optarg
)*1024;
330 if (UserBufferSize
< 0)
331 error("invalid packet buffer size %s", optarg
);
338 error("invalid packet count %s", optarg
);
342 Cflag
= atoi(optarg
) * 1000000;
344 error("invalid file size %s", optarg
);
351 #ifdef HAVE_PCAP_FINDALLDEVS
353 if (pcap_findalldevs(&devpointer
, ebuf
) < 0)
356 for (i
= 0; devpointer
!= 0; i
++) {
357 printf("%d.%s", i
+1, devpointer
->name
);
358 if (devpointer
->description
!= NULL
)
359 printf(" (%s)", devpointer
->description
);
361 devpointer
= devpointer
->next
;
365 #endif /* HAVE_PCAP_FINDALLDEVS */
376 #ifndef HAVE_LIBCRYPTO
377 warning("crypto code not compiled in");
391 if (optarg
[0] == '0' && optarg
[1] == 0)
392 error("Invalid adapter index");
394 #ifdef HAVE_PCAP_FINDALLDEVS
396 * If the argument is a number, treat it as
397 * an index into the list of adapters, as
398 * printed by "tcpdump -D".
400 * This should be OK on UNIX systems, as interfaces
401 * shouldn't have names that begin with digits.
402 * It can be useful on Windows, where more than
403 * one interface can have the same name.
405 if ((devnum
= atoi(optarg
)) != 0) {
407 error("Invalid adapter index");
409 if (pcap_findalldevs(&devpointer
, ebuf
) < 0)
412 for (i
= 0; i
< devnum
-1; i
++){
413 devpointer
= devpointer
->next
;
414 if (devpointer
== NULL
)
415 error("Invalid adapter index");
418 device
= devpointer
->name
;
421 #endif /* HAVE_PCAP_FINDALLDEVS */
426 #ifdef HAVE_SETLINEBUF
429 setvbuf(stdout
, NULL
, _IOLBF
, 0);
443 if (smiLoadModule(optarg
) == 0) {
444 error("could not load MIB module %s", optarg
);
448 (void)fprintf(stderr
, "%s: ignoring option `-m %s' ",
449 program_name
, optarg
);
450 (void)fprintf(stderr
, "(no libsmi support)\n");
476 snaplen
= strtol(optarg
, &end
, 0);
477 if (optarg
== end
|| *end
!= '\0'
478 || snaplen
< 0 || snaplen
> 65535)
479 error("invalid snaplen %s", optarg
);
480 else if (snaplen
== 0)
494 if (strcasecmp(optarg
, "vat") == 0)
496 else if (strcasecmp(optarg
, "wb") == 0)
498 else if (strcasecmp(optarg
, "rpc") == 0)
500 else if (strcasecmp(optarg
, "rtp") == 0)
502 else if (strcasecmp(optarg
, "rtcp") == 0)
503 packettype
= PT_RTCP
;
504 else if (strcasecmp(optarg
, "snmp") == 0)
505 packettype
= PT_SNMP
;
506 else if (strcasecmp(optarg
, "cnfp") == 0)
507 packettype
= PT_CNFP
;
509 error("unknown packet type `%s'", optarg
);
535 dlt
= pcap_datalink_name_to_val(dlt_name
);
537 error("invalid data link type %s", dlt_name
);
540 #if defined(HAVE_PCAP_DEBUG) || defined(HAVE_YYDEBUG)
543 /* Undocumented flag */
544 #ifdef HAVE_PCAP_DEBUG
545 extern int pcap_debug
;
560 error("-a and -n options are incompatible");
563 thiszone
= gmt2local(0);
565 if (RFileName
!= NULL
) {
567 * We don't need network access, so set it back to the user id.
568 * Also, this prevents the user from reading anyone's
575 pd
= pcap_open_offline(RFileName
, ebuf
);
581 error("-f and -r options are incompatible");
583 if (device
== NULL
) {
584 device
= pcap_lookupdev(ebuf
);
589 PrintCapBegins(program_name
,device
);
592 pd
= pcap_open_live(device
, snaplen
, !pflag
, 1000, ebuf
);
598 if(UserBufferSize
!= 1000000)
599 if(pcap_setbuff(pd
, UserBufferSize
)==-1){
600 error("%s", pcap_geterr(pd
));
604 show_dlts_and_exit(pd
);
606 #ifdef HAVE_PCAP_SET_DATALINK
607 if (pcap_set_datalink(pd
, dlt
) < 0)
608 error("%s", pcap_geterr(pd
));
611 * We don't actually support changing the
612 * data link type, so we only let them
613 * set it to what it already is.
615 if (dlt
!= pcap_datalink(pd
)) {
616 error("%s is not one of the DLTs supported by this device\n",
620 (void)fprintf(stderr
, "%s: data link type %s\n",
621 program_name
, dlt_name
);
622 (void)fflush(stderr
);
624 i
= pcap_snapshot(pd
);
626 warning("snaplen raised from %d to %d", snaplen
, i
);
629 if (pcap_lookupnet(device
, &localnet
, &netmask
, ebuf
) < 0) {
635 * Let user own process after socket has been opened.
642 cmdbuf
= read_infile(infile
);
644 cmdbuf
= copy_argv(&argv
[optind
]);
646 if (pcap_compile(pd
, &fcode
, cmdbuf
, Oflag
, netmask
) < 0)
647 error("%s", pcap_geterr(pd
));
649 bpf_dump(&fcode
, dflag
);
653 init_addrtoname(localnet
, netmask
);
655 (void)setsignal(SIGTERM
, cleanup
);
656 (void)setsignal(SIGINT
, cleanup
);
657 /* Cooperate with nohup(1) */
659 if ((oldhandler
= setsignal(SIGHUP
, cleanup
)) != SIG_DFL
)
660 (void)setsignal(SIGHUP
, oldhandler
);
663 if (pcap_setfilter(pd
, &fcode
) < 0)
664 error("%s", pcap_geterr(pd
));
666 pcap_dumper_t
*p
= pcap_dump_open(pd
, WFileName
);
668 error("%s", pcap_geterr(pd
));
670 printer
= dump_packet_and_trunc
;
671 dumpinfo
.WFileName
= WFileName
;
674 pcap_userdata
= (u_char
*)&dumpinfo
;
676 printer
= dump_packet
;
677 pcap_userdata
= (u_char
*)p
;
680 type
= pcap_datalink(pd
);
681 printer
= lookup_printer(type
);
682 if (printer
== NULL
) {
683 dlt_name
= pcap_datalink_val_to_name(type
);
684 if (dlt_name
!= NULL
)
685 error("unsupported data link type %s", dlt_name
);
687 error("unsupported data link type %d", type
);
692 (void)setsignal(SIGINFO
, requestinfo
);
695 if (RFileName
== NULL
) {
696 (void)fprintf(stderr
, "%s: listening on %s\n",
697 program_name
, device
);
698 (void)fflush(stderr
);
701 if (pcap_loop(pd
, cnt
, printer
, pcap_userdata
) < 0) {
702 (void)fprintf(stderr
, "%s: pcap_loop: %s\n",
703 program_name
, pcap_geterr(pd
));
708 if (RFileName
== NULL
)
714 /* make a clean exit on interrupts */
719 /* Can't print the summary if reading from a savefile */
720 if (pd
!= NULL
&& pcap_file(pd
) == NULL
) {
721 (void)fflush(stdout
);
730 info(register int verbose
)
732 struct pcap_stat stat
;
734 if (pcap_stats(pd
, &stat
) < 0) {
735 (void)fprintf(stderr
, "pcap_stats: %s\n", pcap_geterr(pd
));
740 fprintf(stderr
, "%s: ", program_name
);
742 (void)fprintf(stderr
, "%d packets received by filter", stat
.ps_recv
);
747 (void)fprintf(stderr
, "%d packets dropped by kernel\n", stat
.ps_drop
);
756 for (i
= 0, j
= strlen(s
) - 1; i
< j
; i
++, j
--) {
765 swebitoa(unsigned int n
, char *s
)
771 s
[i
++] = n
% 10 + '0';
772 } while ((n
/= 10) > 0);
779 dump_packet_and_trunc(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
781 struct dump_info
*dump_info
;
787 dump_info
= (struct dump_info
*)user
;
790 * XXX - this won't prevent capture files from getting
791 * larger than Cflag - the last packet written to the
792 * file could put it over Cflag.
794 if (ftell((FILE *)dump_info
->p
) > Cflag
) {
795 name
= (char *) malloc(strlen(dump_info
->WFileName
) + 4);
797 error("dump_packet_and_trunc: malloc");
798 strcpy(name
, dump_info
->WFileName
);
799 swebitoa(cnt
, name
+ strlen(dump_info
->WFileName
));
801 pcap_dump_close(dump_info
->p
);
802 dump_info
->p
= pcap_dump_open(dump_info
->pd
, name
);
804 if (dump_info
->p
== NULL
)
805 error("%s", pcap_geterr(pd
));
808 pcap_dump((u_char
*)dump_info
->p
, h
, sp
);
816 dump_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
820 pcap_dump(user
, h
, sp
);
827 /* Like default_print() but data need not be aligned */
829 default_print_unaligned(register const u_char
*cp
, register u_int length
)
832 register int nshorts
;
835 ascii_print(cp
, length
);
838 nshorts
= (u_int
) length
/ sizeof(u_short
);
840 while (--nshorts
>= 0) {
842 (void)printf("\n\t\t\t");
844 (void)printf(" %02x%02x", s
, *cp
++);
848 (void)printf("\n\t\t\t");
849 (void)printf(" %02x", *cp
);
855 * XXX - there should really be libpcap calls to get the version
856 * number as a string (the string would be generated from #defines
857 * at run time, so that it's not generated from string constants
858 * in the library, as, on many UNIX systems, those constants would
859 * be statically linked into the application executable image, and
860 * would thus reflect the version of libpcap on the system on
861 * which the application was *linked*, not the system on which it's
864 * That routine should be documented, unlike the "version[]"
865 * string, so that UNIX vendors providing their own libpcaps
866 * don't omit it (as a couple of vendors have...).
868 * Packet.dll should perhaps also export a routine to return the
869 * version number of the Packet.dll code, to supply the
870 * "Wpcap_version" information on Windows.
872 char WDversion
[]="current-cvs.tcpdump.org";
873 char version
[]="current-cvs.tcpdump.org";
874 char pcap_version
[]="current-cvs.tcpdump.org";
875 char Wpcap_version
[]="3.0 alpha";
879 * By default, print the specified data out in hex.
882 default_print(register const u_char
*bp
, register u_int length
)
884 default_print_unaligned(bp
, length
);
888 * By default, print the packet out in hex; if eflag is set, print
889 * everything, otherwise print everything except for the link-layer
893 default_print_packet(register const u_char
*bp
, register u_int length
,
898 * Include the link-layer header.
900 default_print_unaligned(bp
, length
);
903 * Don't include the link-layer header - and if we have
904 * nothing past the link-layer header, print nothing.
906 if (length
> hdr_length
)
907 default_print_unaligned(bp
+ hdr_length
,
908 length
- hdr_length
);
913 RETSIGTYPE
requestinfo(int signo _U_
)
925 extern char version
[];
926 #if defined(WIN32) || defined(HAVE_PCAP_VERSION)
927 extern char pcap_version
[];
929 static char pcap_version
[] = "unknown";
933 (void)fprintf(stderr
, "%s version %s, based on tcpdump version %s\n", program_name
, WDversion
, version
);
934 (void)fprintf(stderr
, "WinPcap version %s, based on libpcap version %s\n",Wpcap_version
, pcap_version
);
936 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
937 (void)fprintf(stderr
, "libpcap version %s\n", pcap_version
);
939 (void)fprintf(stderr
,
941 "Usage: %s [-aAdDeflLnNOpqRStuvxX] [-B size] [-c count] [ -C file_size ]\n", program_name
);
943 #ifdef HAVE_PCAP_FINDALLDEVS
944 "Usage: %s [-aAdDeflLnNOpqRStuvxX] [-c count] [ -C file_size ]\n", program_name
);
945 #else /* HAVE_PCAP_FINDALLDEVS */
946 "Usage: %s [-aAdeflLnNOpqRStuvxX] [-c count] [ -C file_size ]\n", program_name
);
947 #endif /* HAVE_PCAP_FINDALLDEVS */
949 (void)fprintf(stderr
,
950 "\t\t[ -E algo:secret ] [ -F file ] [ -i interface ] [ -r file ]\n");
951 (void)fprintf(stderr
,
952 "\t\t[ -s snaplen ] [ -T type ] [ -w file ] [ -y datalinktype ]\n");
953 (void)fprintf(stderr
,
954 "\t\t[ expression ]\n");