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.193 2002-12-19 09:39:17 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 */
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 { ether_if_print
, DLT_EN10MB
},
135 { token_if_print
, DLT_IEEE802
},
137 { lane_if_print
, DLT_LANE8023
},
140 { cip_if_print
, DLT_CIP
},
143 { cip_if_print
, DLT_ATM_CLIP
},
145 { sl_if_print
, DLT_SLIP
},
146 { sl_bsdos_if_print
, DLT_SLIP_BSDOS
},
147 { ppp_if_print
, DLT_PPP
},
148 { ppp_bsdos_if_print
, DLT_PPP_BSDOS
},
149 { fddi_if_print
, DLT_FDDI
},
150 { null_if_print
, DLT_NULL
},
152 { null_if_print
, DLT_LOOP
},
154 { raw_if_print
, DLT_RAW
},
155 { atm_if_print
, DLT_ATM_RFC1483
},
157 { chdlc_if_print
, DLT_C_HDLC
},
160 { chdlc_if_print
, DLT_HDLC
},
162 #ifdef DLT_PPP_SERIAL
163 { ppp_hdlc_if_print
, DLT_PPP_SERIAL
},
166 { pppoe_if_print
, DLT_PPP_ETHER
},
169 { sll_if_print
, DLT_LINUX_SLL
},
171 #ifdef DLT_IEEE802_11
172 { ieee802_11_if_print
, DLT_IEEE802_11
},
175 { ltalk_if_print
, DLT_LTALK
},
178 { pflog_if_print
, DLT_PFLOG
},
181 { fr_if_print
, DLT_FR
},
184 { fr_if_print
, DLT_FRELAY
},
187 { sunatm_if_print
, DLT_SUNATM
},
189 #ifdef DLT_IP_OVER_FC
190 { ipfc_if_print
, DLT_IP_OVER_FC
},
192 #ifdef DLT_PRISM_HEADER
193 { prism_if_print
, DLT_PRISM_HEADER
},
195 #ifdef DLT_IEEE802_11_RADIO
196 { ieee802_11_radio_if_print
, DLT_IEEE802_11_RADIO
},
202 lookup_printer(int type
)
206 for (p
= printers
; p
->f
; ++p
)
231 show_dlts_and_exit(pcap_t
*pd
)
235 const char *dlt_name
;
237 n_dlts
= pcap_list_datalinks(pd
, &dlts
);
239 error("%s", pcap_geterr(pd
));
240 else if (n_dlts
== 0 || !dlts
)
241 error("No data link types.");
243 (void) fprintf(stderr
, "Data link types (use option -y to set):\n");
245 while (--n_dlts
>= 0) {
246 dlt_name
= pcap_datalink_val_to_name(dlts
[n_dlts
]);
247 if (dlt_name
!= NULL
) {
248 (void) fprintf(stderr
, " %s", dlt_name
);
251 * OK, does tcpdump handle that type?
253 if (lookup_printer(dlts
[n_dlts
]) == NULL
)
254 (void) fprintf(stderr
, " (not supported)");
257 (void) fprintf(stderr
, " DLT %d (not supported)\n",
266 main(int argc
, char **argv
)
268 register int cnt
, op
, i
;
269 bpf_u_int32 localnet
, netmask
;
270 register char *cp
, *infile
, *cmdbuf
, *device
, *RFileName
, *WFileName
;
271 pcap_handler callback
;
273 struct bpf_program fcode
;
275 RETSIGTYPE (*oldhandler
)(int);
277 struct print_info printinfo
;
278 struct dump_info dumpinfo
;
279 u_char
*pcap_userdata
;
280 char ebuf
[PCAP_ERRBUF_SIZE
];
281 #ifdef HAVE_PCAP_FINDALLDEVS
282 pcap_if_t
*devpointer
;
287 DWORD dwWindowsMajorVersion
;
288 u_int UserBufferSize
=1000000;
292 dwVersion
=GetVersion(); /* get the OS version */
293 dwWindowsMajorVersion
= (DWORD
)(LOBYTE(LOWORD(dwVersion
)));
294 if(wsockinit()!=0) return 1;
302 if ((cp
= strrchr(argv
[0], '/')) != NULL
)
303 program_name
= cp
+ 1;
305 program_name
= argv
[0];
307 if (abort_on_misalignment(ebuf
, sizeof(ebuf
)) < 0)
317 (op
= getopt(argc
, argv
, "aAB:c:C:dDeE:fF:i:lLm:nNOpqr:Rs:StT:uvw:xXy:Y")) != -1)
319 #ifdef HAVE_PCAP_FINDALLDEVS
320 (op
= getopt(argc
, argv
, "aAc:C:dDeE:fF:i:lLm:nNOpqr:Rs:StT:uvw:xXy:Y")) != -1)
321 #else /* HAVE_PCAP_FINDALLDEVS */
322 (op
= getopt(argc
, argv
, "aAc:C:deE:fF:i:lLm:nNOpqr:Rs:StT:uvw:xXy:Y")) != -1)
323 #endif /* HAVE_PCAP_FINDALLDEVS */
339 UserBufferSize
= atoi(optarg
)*1024;
340 if (UserBufferSize
< 0)
341 error("invalid packet buffer size %s", optarg
);
348 error("invalid packet count %s", optarg
);
352 Cflag
= atoi(optarg
) * 1000000;
354 error("invalid file size %s", optarg
);
361 #ifdef HAVE_PCAP_FINDALLDEVS
363 if (pcap_findalldevs(&devpointer
, ebuf
) < 0)
366 for (i
= 0; devpointer
!= 0; i
++) {
367 printf("%d.%s", i
+1, devpointer
->name
);
368 if (devpointer
->description
!= NULL
)
369 printf(" (%s)", devpointer
->description
);
371 devpointer
= devpointer
->next
;
375 #endif /* HAVE_PCAP_FINDALLDEVS */
386 #ifndef HAVE_LIBCRYPTO
387 warning("crypto code not compiled in");
401 if (optarg
[0] == '0' && optarg
[1] == 0)
402 error("Invalid adapter index");
404 #ifdef HAVE_PCAP_FINDALLDEVS
406 * If the argument is a number, treat it as
407 * an index into the list of adapters, as
408 * printed by "tcpdump -D".
410 * This should be OK on UNIX systems, as interfaces
411 * shouldn't have names that begin with digits.
412 * It can be useful on Windows, where more than
413 * one interface can have the same name.
415 if ((devnum
= atoi(optarg
)) != 0) {
417 error("Invalid adapter index");
419 if (pcap_findalldevs(&devpointer
, ebuf
) < 0)
422 for (i
= 0; i
< devnum
-1; i
++){
423 devpointer
= devpointer
->next
;
424 if (devpointer
== NULL
)
425 error("Invalid adapter index");
428 device
= devpointer
->name
;
431 #endif /* HAVE_PCAP_FINDALLDEVS */
436 #ifdef HAVE_SETLINEBUF
439 setvbuf(stdout
, NULL
, _IOLBF
, 0);
453 if (smiLoadModule(optarg
) == 0) {
454 error("could not load MIB module %s", optarg
);
458 (void)fprintf(stderr
, "%s: ignoring option `-m %s' ",
459 program_name
, optarg
);
460 (void)fprintf(stderr
, "(no libsmi support)\n");
486 snaplen
= strtol(optarg
, &end
, 0);
487 if (optarg
== end
|| *end
!= '\0'
488 || snaplen
< 0 || snaplen
> 65535)
489 error("invalid snaplen %s", optarg
);
490 else if (snaplen
== 0)
504 if (strcasecmp(optarg
, "vat") == 0)
506 else if (strcasecmp(optarg
, "wb") == 0)
508 else if (strcasecmp(optarg
, "rpc") == 0)
510 else if (strcasecmp(optarg
, "rtp") == 0)
512 else if (strcasecmp(optarg
, "rtcp") == 0)
513 packettype
= PT_RTCP
;
514 else if (strcasecmp(optarg
, "snmp") == 0)
515 packettype
= PT_SNMP
;
516 else if (strcasecmp(optarg
, "cnfp") == 0)
517 packettype
= PT_CNFP
;
519 error("unknown packet type `%s'", optarg
);
545 dlt
= pcap_datalink_name_to_val(dlt_name
);
547 error("invalid data link type %s", dlt_name
);
550 #if defined(HAVE_PCAP_DEBUG) || defined(HAVE_YYDEBUG)
553 /* Undocumented flag */
554 #ifdef HAVE_PCAP_DEBUG
555 extern int pcap_debug
;
570 error("-a and -n options are incompatible");
573 thiszone
= gmt2local(0);
575 if (RFileName
!= NULL
) {
577 * We don't need network access, so set it back to the user id.
578 * Also, this prevents the user from reading anyone's
585 pd
= pcap_open_offline(RFileName
, ebuf
);
591 error("-f and -r options are incompatible");
593 if (device
== NULL
) {
594 device
= pcap_lookupdev(ebuf
);
599 PrintCapBegins(program_name
,device
);
602 pd
= pcap_open_live(device
, snaplen
, !pflag
, 1000, ebuf
);
608 if(UserBufferSize
!= 1000000)
609 if(pcap_setbuff(pd
, UserBufferSize
)==-1){
610 error("%s", pcap_geterr(pd
));
614 show_dlts_and_exit(pd
);
616 #ifdef HAVE_PCAP_SET_DATALINK
617 if (pcap_set_datalink(pd
, dlt
) < 0)
618 error("%s", pcap_geterr(pd
));
621 * We don't actually support changing the
622 * data link type, so we only let them
623 * set it to what it already is.
625 if (dlt
!= pcap_datalink(pd
)) {
626 error("%s is not one of the DLTs supported by this device\n",
630 (void)fprintf(stderr
, "%s: data link type %s\n",
631 program_name
, dlt_name
);
632 (void)fflush(stderr
);
634 i
= pcap_snapshot(pd
);
636 warning("snaplen raised from %d to %d", snaplen
, i
);
639 if (pcap_lookupnet(device
, &localnet
, &netmask
, ebuf
) < 0) {
645 * Let user own process after socket has been opened.
652 cmdbuf
= read_infile(infile
);
654 cmdbuf
= copy_argv(&argv
[optind
]);
656 if (pcap_compile(pd
, &fcode
, cmdbuf
, Oflag
, netmask
) < 0)
657 error("%s", pcap_geterr(pd
));
659 bpf_dump(&fcode
, dflag
);
663 init_addrtoname(localnet
, netmask
);
665 (void)setsignal(SIGTERM
, cleanup
);
666 (void)setsignal(SIGINT
, cleanup
);
667 /* Cooperate with nohup(1) */
669 if ((oldhandler
= setsignal(SIGHUP
, cleanup
)) != SIG_DFL
)
670 (void)setsignal(SIGHUP
, oldhandler
);
673 if (pcap_setfilter(pd
, &fcode
) < 0)
674 error("%s", pcap_geterr(pd
));
676 pcap_dumper_t
*p
= pcap_dump_open(pd
, WFileName
);
678 error("%s", pcap_geterr(pd
));
680 callback
= dump_packet_and_trunc
;
681 dumpinfo
.WFileName
= WFileName
;
684 pcap_userdata
= (u_char
*)&dumpinfo
;
686 callback
= dump_packet
;
687 pcap_userdata
= (u_char
*)p
;
690 type
= pcap_datalink(pd
);
691 printinfo
.printer
= lookup_printer(type
);
692 if (printinfo
.printer
== NULL
) {
693 dlt_name
= pcap_datalink_val_to_name(type
);
694 if (dlt_name
!= NULL
)
695 error("unsupported data link type %s", dlt_name
);
697 error("unsupported data link type %d", type
);
699 callback
= print_packet
;
700 pcap_userdata
= (u_char
*)&printinfo
;
703 (void)setsignal(SIGINFO
, requestinfo
);
706 if (RFileName
== NULL
) {
707 (void)fprintf(stderr
, "%s: listening on %s\n",
708 program_name
, device
);
709 (void)fflush(stderr
);
712 if (pcap_loop(pd
, cnt
, callback
, pcap_userdata
) < 0) {
713 (void)fprintf(stderr
, "%s: pcap_loop: %s\n",
714 program_name
, pcap_geterr(pd
));
719 if (RFileName
== NULL
)
725 /* make a clean exit on interrupts */
730 /* Can't print the summary if reading from a savefile */
731 if (pd
!= NULL
&& pcap_file(pd
) == NULL
) {
732 (void)fflush(stdout
);
741 info(register int verbose
)
743 struct pcap_stat stat
;
745 if (pcap_stats(pd
, &stat
) < 0) {
746 (void)fprintf(stderr
, "pcap_stats: %s\n", pcap_geterr(pd
));
751 fprintf(stderr
, "%s: ", program_name
);
753 (void)fprintf(stderr
, "%d packets received by filter", stat
.ps_recv
);
758 (void)fprintf(stderr
, "%d packets dropped by kernel\n", stat
.ps_drop
);
767 for (i
= 0, j
= strlen(s
) - 1; i
< j
; i
++, j
--) {
776 swebitoa(unsigned int n
, char *s
)
782 s
[i
++] = n
% 10 + '0';
783 } while ((n
/= 10) > 0);
790 dump_packet_and_trunc(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
792 struct dump_info
*dump_info
;
798 dump_info
= (struct dump_info
*)user
;
801 * XXX - this won't prevent capture files from getting
802 * larger than Cflag - the last packet written to the
803 * file could put it over Cflag.
805 if (ftell((FILE *)dump_info
->p
) > Cflag
) {
806 name
= (char *) malloc(strlen(dump_info
->WFileName
) + 4);
808 error("dump_packet_and_trunc: malloc");
809 strcpy(name
, dump_info
->WFileName
);
810 swebitoa(cnt
, name
+ strlen(dump_info
->WFileName
));
812 pcap_dump_close(dump_info
->p
);
813 dump_info
->p
= pcap_dump_open(dump_info
->pd
, name
);
815 if (dump_info
->p
== NULL
)
816 error("%s", pcap_geterr(pd
));
819 pcap_dump((u_char
*)dump_info
->p
, h
, sp
);
827 dump_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
831 pcap_dump(user
, h
, sp
);
839 print_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
841 struct print_info
*print_info
;
847 print_info
= (struct print_info
*)user
;
850 * Some printers want to check that they're not walking off the
852 * Rather than pass it all the way down, we set this global.
854 snapend
= sp
+ h
->caplen
;
856 hdrlen
= (*print_info
->printer
)(h
, sp
);
859 * Print the raw packet data.
863 * Include the link-layer header.
865 default_print_unaligned(sp
, h
->caplen
);
868 * Don't include the link-layer header - and if
869 * we have nothing past the link-layer header,
872 if (h
->caplen
> hdrlen
)
873 default_print_unaligned(sp
+ hdrlen
,
885 /* Like default_print() but data need not be aligned */
887 default_print_unaligned(register const u_char
*cp
, register u_int length
)
890 register int nshorts
;
893 ascii_print(cp
, length
);
896 nshorts
= (u_int
) length
/ sizeof(u_short
);
898 while (--nshorts
>= 0) {
900 (void)printf("\n\t\t\t");
902 (void)printf(" %02x%02x", s
, *cp
++);
906 (void)printf("\n\t\t\t");
907 (void)printf(" %02x", *cp
);
913 * XXX - there should really be libpcap calls to get the version
914 * number as a string (the string would be generated from #defines
915 * at run time, so that it's not generated from string constants
916 * in the library, as, on many UNIX systems, those constants would
917 * be statically linked into the application executable image, and
918 * would thus reflect the version of libpcap on the system on
919 * which the application was *linked*, not the system on which it's
922 * That routine should be documented, unlike the "version[]"
923 * string, so that UNIX vendors providing their own libpcaps
924 * don't omit it (as a couple of vendors have...).
926 * Packet.dll should perhaps also export a routine to return the
927 * version number of the Packet.dll code, to supply the
928 * "Wpcap_version" information on Windows.
930 char WDversion
[]="current-cvs.tcpdump.org";
931 char version
[]="current-cvs.tcpdump.org";
932 char pcap_version
[]="current-cvs.tcpdump.org";
933 char Wpcap_version
[]="3.0 alpha";
937 * By default, print the specified data out in hex.
940 default_print(register const u_char
*bp
, register u_int length
)
942 default_print_unaligned(bp
, length
);
946 RETSIGTYPE
requestinfo(int signo _U_
)
958 extern char version
[];
959 #if defined(WIN32) || defined(HAVE_PCAP_VERSION)
960 extern char pcap_version
[];
962 static char pcap_version
[] = "unknown";
966 (void)fprintf(stderr
, "%s version %s, based on tcpdump version %s\n", program_name
, WDversion
, version
);
967 (void)fprintf(stderr
, "WinPcap version %s, based on libpcap version %s\n",Wpcap_version
, pcap_version
);
969 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
970 (void)fprintf(stderr
, "libpcap version %s\n", pcap_version
);
972 (void)fprintf(stderr
,
974 "Usage: %s [-aAdDeflLnNOpqRStuvxX] [-B size] [-c count] [ -C file_size ]\n", program_name
);
976 #ifdef HAVE_PCAP_FINDALLDEVS
977 "Usage: %s [-aAdDeflLnNOpqRStuvxX] [-c count] [ -C file_size ]\n", program_name
);
978 #else /* HAVE_PCAP_FINDALLDEVS */
979 "Usage: %s [-aAdeflLnNOpqRStuvxX] [-c count] [ -C file_size ]\n", program_name
);
980 #endif /* HAVE_PCAP_FINDALLDEVS */
982 (void)fprintf(stderr
,
983 "\t\t[ -E algo:secret ] [ -F file ] [ -i interface ] [ -r file ]\n");
984 (void)fprintf(stderr
,
985 "\t\t[ -s snaplen ] [ -T type ] [ -w file ] [ -y datalinktype ]\n");
986 (void)fprintf(stderr
,
987 "\t\t[ expression ]\n");