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.207 2003-05-22 16:33:07 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 #ifdef DLT_ARCNET_LINUX
136 { arcnet_linux_if_print
, DLT_ARCNET_LINUX
},
138 { ether_if_print
, DLT_EN10MB
},
139 { token_if_print
, DLT_IEEE802
},
141 { lane_if_print
, DLT_LANE8023
},
144 { cip_if_print
, DLT_CIP
},
147 { cip_if_print
, DLT_ATM_CLIP
},
149 { sl_if_print
, DLT_SLIP
},
150 #ifdef DLT_SLIP_BSDOS
151 { sl_bsdos_if_print
, DLT_SLIP_BSDOS
},
153 { ppp_if_print
, DLT_PPP
},
155 { ppp_bsdos_if_print
, DLT_PPP_BSDOS
},
157 { fddi_if_print
, DLT_FDDI
},
158 { null_if_print
, DLT_NULL
},
160 { null_if_print
, DLT_LOOP
},
162 { raw_if_print
, DLT_RAW
},
163 { atm_if_print
, DLT_ATM_RFC1483
},
165 { chdlc_if_print
, DLT_C_HDLC
},
168 { chdlc_if_print
, DLT_HDLC
},
170 #ifdef DLT_PPP_SERIAL
171 { ppp_hdlc_if_print
, DLT_PPP_SERIAL
},
174 { pppoe_if_print
, DLT_PPP_ETHER
},
177 { sll_if_print
, DLT_LINUX_SLL
},
179 #ifdef DLT_IEEE802_11
180 { ieee802_11_if_print
, DLT_IEEE802_11
},
183 { ltalk_if_print
, DLT_LTALK
},
186 { pflog_if_print
, DLT_PFLOG
},
189 { fr_if_print
, DLT_FR
},
192 { fr_if_print
, DLT_FRELAY
},
195 { sunatm_if_print
, DLT_SUNATM
},
197 #ifdef DLT_IP_OVER_FC
198 { ipfc_if_print
, DLT_IP_OVER_FC
},
200 #ifdef DLT_PRISM_HEADER
201 { prism_if_print
, DLT_PRISM_HEADER
},
203 #ifdef DLT_IEEE802_11_RADIO
204 { ieee802_11_radio_if_print
, DLT_IEEE802_11_RADIO
},
207 { enc_if_print
, DLT_ENC
},
213 lookup_printer(int type
)
217 for (p
= printers
; p
->f
; ++p
)
242 show_dlts_and_exit(pcap_t
*pd
)
246 const char *dlt_name
;
248 n_dlts
= pcap_list_datalinks(pd
, &dlts
);
250 error("%s", pcap_geterr(pd
));
251 else if (n_dlts
== 0 || !dlts
)
252 error("No data link types.");
254 (void) fprintf(stderr
, "Data link types (use option -y to set):\n");
256 while (--n_dlts
>= 0) {
257 dlt_name
= pcap_datalink_val_to_name(dlts
[n_dlts
]);
258 if (dlt_name
!= NULL
) {
259 (void) fprintf(stderr
, " %s", dlt_name
);
262 * OK, does tcpdump handle that type?
264 if (lookup_printer(dlts
[n_dlts
]) == NULL
)
265 (void) fprintf(stderr
, " (not supported)");
268 (void) fprintf(stderr
, " DLT %d (not supported)\n",
277 * Set up flags that might or might not be supported depending on the
278 * version of libpcap we're using.
282 #define B_FLAG_USAGE " [ -B size ]"
288 #ifdef HAVE_PCAP_FINDALLDEVS
294 #ifdef HAVE_PCAP_DUMP_FLUSH
301 main(int argc
, char **argv
)
303 register int cnt
, op
, i
;
304 bpf_u_int32 localnet
, netmask
;
305 register char *cp
, *infile
, *cmdbuf
, *device
, *RFileName
, *WFileName
;
306 pcap_handler callback
;
308 struct bpf_program fcode
;
310 RETSIGTYPE (*oldhandler
)(int);
312 struct print_info printinfo
;
313 struct dump_info dumpinfo
;
314 u_char
*pcap_userdata
;
315 char ebuf
[PCAP_ERRBUF_SIZE
];
316 #ifdef HAVE_PCAP_FINDALLDEVS
317 pcap_if_t
*devpointer
;
322 DWORD dwWindowsMajorVersion
;
323 u_int UserBufferSize
=1000000;
327 dwVersion
=GetVersion(); /* get the OS version */
328 dwWindowsMajorVersion
= (DWORD
)(LOBYTE(LOWORD(dwVersion
)));
329 if(wsockinit()!=0) return 1;
337 if ((cp
= strrchr(argv
[0], '/')) != NULL
)
338 program_name
= cp
+ 1;
340 program_name
= argv
[0];
342 if (abort_on_misalignment(ebuf
, sizeof(ebuf
)) < 0)
351 (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)
366 UserBufferSize
= atoi(optarg
)*1024;
367 if (UserBufferSize
< 0)
368 error("invalid packet buffer size %s", optarg
);
375 error("invalid packet count %s", optarg
);
379 Cflag
= atoi(optarg
) * 1000000;
381 error("invalid file size %s", optarg
);
388 #ifdef HAVE_PCAP_FINDALLDEVS
390 if (pcap_findalldevs(&devpointer
, ebuf
) < 0)
393 for (i
= 0; devpointer
!= 0; i
++) {
394 printf("%d.%s", i
+1, devpointer
->name
);
395 if (devpointer
->description
!= NULL
)
396 printf(" (%s)", devpointer
->description
);
398 devpointer
= devpointer
->next
;
402 #endif /* HAVE_PCAP_FINDALLDEVS */
413 #ifndef HAVE_LIBCRYPTO
414 warning("crypto code not compiled in");
428 if (optarg
[0] == '0' && optarg
[1] == 0)
429 error("Invalid adapter index");
431 #ifdef HAVE_PCAP_FINDALLDEVS
433 * If the argument is a number, treat it as
434 * an index into the list of adapters, as
435 * printed by "tcpdump -D".
437 * This should be OK on UNIX systems, as interfaces
438 * shouldn't have names that begin with digits.
439 * It can be useful on Windows, where more than
440 * one interface can have the same name.
442 if ((devnum
= atoi(optarg
)) != 0) {
444 error("Invalid adapter index");
446 if (pcap_findalldevs(&devpointer
, ebuf
) < 0)
449 for (i
= 0; i
< devnum
-1; i
++){
450 devpointer
= devpointer
->next
;
451 if (devpointer
== NULL
)
452 error("Invalid adapter index");
455 device
= devpointer
->name
;
458 #endif /* HAVE_PCAP_FINDALLDEVS */
463 #ifdef HAVE_SETLINEBUF
466 setvbuf(stdout
, NULL
, _IOLBF
, 0);
480 if (smiLoadModule(optarg
) == 0) {
481 error("could not load MIB module %s", optarg
);
485 (void)fprintf(stderr
, "%s: ignoring option `-m %s' ",
486 program_name
, optarg
);
487 (void)fprintf(stderr
, "(no libsmi support)\n");
513 snaplen
= strtol(optarg
, &end
, 0);
514 if (optarg
== end
|| *end
!= '\0'
515 || snaplen
< 0 || snaplen
> 65535)
516 error("invalid snaplen %s", optarg
);
517 else if (snaplen
== 0)
531 if (strcasecmp(optarg
, "vat") == 0)
533 else if (strcasecmp(optarg
, "wb") == 0)
535 else if (strcasecmp(optarg
, "rpc") == 0)
537 else if (strcasecmp(optarg
, "rtp") == 0)
539 else if (strcasecmp(optarg
, "rtcp") == 0)
540 packettype
= PT_RTCP
;
541 else if (strcasecmp(optarg
, "snmp") == 0)
542 packettype
= PT_SNMP
;
543 else if (strcasecmp(optarg
, "cnfp") == 0)
544 packettype
= PT_CNFP
;
545 else if (strcasecmp(optarg
, "tftp") == 0)
546 packettype
= PT_TFTP
;
548 error("unknown packet type `%s'", optarg
);
555 #ifdef HAVE_PCAP_DUMP_FLUSH
580 dlt
= pcap_datalink_name_to_val(dlt_name
);
582 error("invalid data link type %s", dlt_name
);
585 #if defined(HAVE_PCAP_DEBUG) || defined(HAVE_YYDEBUG)
588 /* Undocumented flag */
589 #ifdef HAVE_PCAP_DEBUG
590 extern int pcap_debug
;
605 error("-a and -n options are incompatible");
608 thiszone
= gmt2local(0);
610 if (RFileName
!= NULL
) {
612 * We don't need network access, so set it back to the user id.
613 * Also, this prevents the user from reading anyone's
620 pd
= pcap_open_offline(RFileName
, ebuf
);
621 printf("reading from file %s, link-type %u\n",
629 error("-f and -r options are incompatible");
631 if (device
== NULL
) {
632 device
= pcap_lookupdev(ebuf
);
637 PrintCapBegins(program_name
,device
);
640 pd
= pcap_open_live(device
, snaplen
, !pflag
, 1000, ebuf
);
646 if(UserBufferSize
!= 1000000)
647 if(pcap_setbuff(pd
, UserBufferSize
)==-1){
648 error("%s", pcap_geterr(pd
));
652 show_dlts_and_exit(pd
);
654 #ifdef HAVE_PCAP_SET_DATALINK
655 if (pcap_set_datalink(pd
, dlt
) < 0)
656 error("%s", pcap_geterr(pd
));
659 * We don't actually support changing the
660 * data link type, so we only let them
661 * set it to what it already is.
663 if (dlt
!= pcap_datalink(pd
)) {
664 error("%s is not one of the DLTs supported by this device\n",
668 (void)fprintf(stderr
, "%s: data link type %s\n",
669 program_name
, dlt_name
);
670 (void)fflush(stderr
);
672 i
= pcap_snapshot(pd
);
674 warning("snaplen raised from %d to %d", snaplen
, i
);
677 if (pcap_lookupnet(device
, &localnet
, &netmask
, ebuf
) < 0) {
683 * Let user own process after socket has been opened.
690 cmdbuf
= read_infile(infile
);
692 cmdbuf
= copy_argv(&argv
[optind
]);
694 if (pcap_compile(pd
, &fcode
, cmdbuf
, Oflag
, netmask
) < 0)
695 error("%s", pcap_geterr(pd
));
697 bpf_dump(&fcode
, dflag
);
701 init_addrtoname(localnet
, netmask
);
703 (void)setsignal(SIGTERM
, cleanup
);
704 (void)setsignal(SIGINT
, cleanup
);
705 /* Cooperate with nohup(1) */
707 if ((oldhandler
= setsignal(SIGHUP
, cleanup
)) != SIG_DFL
)
708 (void)setsignal(SIGHUP
, oldhandler
);
711 if (pcap_setfilter(pd
, &fcode
) < 0)
712 error("%s", pcap_geterr(pd
));
714 pcap_dumper_t
*p
= pcap_dump_open(pd
, WFileName
);
716 error("%s", pcap_geterr(pd
));
718 callback
= dump_packet_and_trunc
;
719 dumpinfo
.WFileName
= WFileName
;
722 pcap_userdata
= (u_char
*)&dumpinfo
;
724 callback
= dump_packet
;
725 pcap_userdata
= (u_char
*)p
;
728 type
= pcap_datalink(pd
);
729 printinfo
.printer
= lookup_printer(type
);
730 if (printinfo
.printer
== NULL
) {
731 dlt_name
= pcap_datalink_val_to_name(type
);
732 if (dlt_name
!= NULL
)
733 error("unsupported data link type %s", dlt_name
);
735 error("unsupported data link type %d", type
);
737 callback
= print_packet
;
738 pcap_userdata
= (u_char
*)&printinfo
;
741 (void)setsignal(SIGINFO
, requestinfo
);
744 if (RFileName
== NULL
) {
745 if (!vflag
&& !WFileName
) {
746 (void)fprintf(stderr
,
747 "%s: verbose output suppressed, use -v or -vv for full protocol decode\n",
750 (void)fprintf(stderr
, "%s: ", program_name
);
751 (void)fprintf(stderr
, "listening on %s, link-type %u, capture size %u bytes\n",
752 device
, type
, snaplen
);
753 (void)fflush(stderr
);
756 if (pcap_loop(pd
, cnt
, callback
, pcap_userdata
) < 0) {
757 (void)fprintf(stderr
, "%s: pcap_loop: %s\n",
758 program_name
, pcap_geterr(pd
));
763 if (RFileName
== NULL
)
769 /* make a clean exit on interrupts */
774 /* Can't print the summary if reading from a savefile */
775 if (pd
!= NULL
&& pcap_file(pd
) == NULL
) {
776 (void)fflush(stdout
);
785 info(register int verbose
)
787 struct pcap_stat stat
;
789 if (pcap_stats(pd
, &stat
) < 0) {
790 (void)fprintf(stderr
, "pcap_stats: %s\n", pcap_geterr(pd
));
795 fprintf(stderr
, "%s: ", program_name
);
797 (void)fprintf(stderr
, "%d packets received by filter", stat
.ps_recv
);
802 (void)fprintf(stderr
, "%d packets dropped by kernel\n", stat
.ps_drop
);
811 for (i
= 0, j
= strlen(s
) - 1; i
< j
; i
++, j
--) {
820 swebitoa(unsigned int n
, char *s
)
826 s
[i
++] = n
% 10 + '0';
827 } while ((n
/= 10) > 0);
834 dump_packet_and_trunc(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
836 struct dump_info
*dump_info
;
842 dump_info
= (struct dump_info
*)user
;
845 * XXX - this won't prevent capture files from getting
846 * larger than Cflag - the last packet written to the
847 * file could put it over Cflag.
849 if (ftell((FILE *)dump_info
->p
) > Cflag
) {
850 name
= (char *) malloc(strlen(dump_info
->WFileName
) + 4);
852 error("dump_packet_and_trunc: malloc");
853 strcpy(name
, dump_info
->WFileName
);
854 swebitoa(cnt
, name
+ strlen(dump_info
->WFileName
));
856 pcap_dump_close(dump_info
->p
);
857 dump_info
->p
= pcap_dump_open(dump_info
->pd
, name
);
859 if (dump_info
->p
== NULL
)
860 error("%s", pcap_geterr(pd
));
863 pcap_dump((u_char
*)dump_info
->p
, h
, sp
);
864 #ifdef HAVE_PCAP_DUMP_FLUSH
866 pcap_dump_flush(dump_info
->p
);
875 dump_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
879 pcap_dump(user
, h
, sp
);
880 #ifdef HAVE_PCAP_DUMP_FLUSH
882 pcap_dump_flush((pcap_dumper_t
*)user
);
891 print_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
893 struct print_info
*print_info
;
899 print_info
= (struct print_info
*)user
;
902 * Some printers want to check that they're not walking off the
904 * Rather than pass it all the way down, we set this global.
906 snapend
= sp
+ h
->caplen
;
908 hdrlen
= (*print_info
->printer
)(h
, sp
);
911 * Print the raw packet data.
915 * Include the link-layer header.
917 default_print_unaligned(sp
, h
->caplen
);
920 * Don't include the link-layer header - and if
921 * we have nothing past the link-layer header,
924 if (h
->caplen
> hdrlen
)
925 default_print_unaligned(sp
+ hdrlen
,
937 /* Like default_print() but data need not be aligned */
939 default_print_unaligned(register const u_char
*cp
, register u_int length
)
942 register int nshorts
;
945 ascii_print(cp
, length
);
948 nshorts
= (u_int
) length
/ sizeof(u_short
);
950 while (--nshorts
>= 0) {
952 (void)printf("\n\t\t\t");
954 (void)printf(" %02x%02x", s
, *cp
++);
958 (void)printf("\n\t\t\t");
959 (void)printf(" %02x", *cp
);
965 * XXX - there should really be libpcap calls to get the version
966 * number as a string (the string would be generated from #defines
967 * at run time, so that it's not generated from string constants
968 * in the library, as, on many UNIX systems, those constants would
969 * be statically linked into the application executable image, and
970 * would thus reflect the version of libpcap on the system on
971 * which the application was *linked*, not the system on which it's
974 * That routine should be documented, unlike the "version[]"
975 * string, so that UNIX vendors providing their own libpcaps
976 * don't omit it (as a couple of vendors have...).
978 * Packet.dll should perhaps also export a routine to return the
979 * version number of the Packet.dll code, to supply the
980 * "Wpcap_version" information on Windows.
982 char WDversion
[]="current-cvs.tcpdump.org";
983 char version
[]="current-cvs.tcpdump.org";
984 char pcap_version
[]="current-cvs.tcpdump.org";
985 char Wpcap_version
[]="3.0 alpha";
989 * By default, print the specified data out in hex.
992 default_print(register const u_char
*bp
, register u_int length
)
994 default_print_unaligned(bp
, length
);
998 RETSIGTYPE
requestinfo(int signo _U_
)
1010 extern char version
[];
1011 #ifndef HAVE_PCAP_LIB_VERSION
1012 #if defined(WIN32) || defined(HAVE_PCAP_VERSION)
1013 extern char pcap_version
[];
1014 #else /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
1015 static char pcap_version
[] = "unknown";
1016 #endif /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
1017 #endif /* HAVE_PCAP_LIB_VERSION */
1019 #ifdef HAVE_PCAP_LIB_VERSION
1020 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
1021 (void)fprintf(stderr
, "%s\n", pcap_lib_version());
1022 #else /* HAVE_PCAP_LIB_VERSION */
1024 (void)fprintf(stderr
, "%s version %s, based on tcpdump version %s\n", program_name
, WDversion
, version
);
1025 (void)fprintf(stderr
, "WinPcap version %s, based on libpcap version %s\n",Wpcap_version
, pcap_version
);
1027 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
1028 (void)fprintf(stderr
, "libpcap version %s\n", pcap_version
);
1030 #endif /* HAVE_PCAP_LIB_VERSION */
1031 (void)fprintf(stderr
,
1032 "Usage: %s [-aAd" D_FLAG
"eflLnNOpqRStu" U_FLAG
"vxX]" B_FLAG_USAGE
" [-c count] [ -C file_size ]\n", program_name
);
1033 (void)fprintf(stderr
,
1034 "\t\t[ -E algo:secret ] [ -F file ] [ -i interface ] [ -r file ]\n");
1035 (void)fprintf(stderr
,
1036 "\t\t[ -s snaplen ] [ -T type ] [ -w file ] [ -y datalinktype ]\n");
1037 (void)fprintf(stderr
,
1038 "\t\t[ expression ]\n");