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.189 2002-12-12 07:28:36 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
);
66 #include "interface.h"
67 #include "addrtoname.h"
69 #include "setsignal.h"
70 #include "gmt2local.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 nflag
; /* leave addresses as numbers */
77 int Nflag
; /* remove domains from printed host names */
78 int Oflag
= 1; /* run filter code optimizer */
79 int pflag
; /* don't go promiscuous */
80 int qflag
; /* quick (shorter) output */
81 int Rflag
= 1; /* print sequence # field in AH/ESP*/
82 int sflag
= 0; /* use the libsmi to translate OIDs */
83 int Sflag
; /* print raw TCP sequence numbers */
84 int tflag
= 1; /* print packet arrival time */
85 int uflag
= 0; /* Print undecoded NFS handles */
86 int vflag
; /* verbose */
87 int xflag
; /* print packet in hex */
88 int Xflag
; /* print packet in ascii as well as hex */
89 off_t Cflag
= 0; /* rotate dump files after this many bytes */
90 int Aflag
= 0; /* print packet only in ascii observing LF, CR, TAB, SPACE */
92 char *espsecret
= NULL
; /* ESP secret key */
101 int32_t thiszone
; /* seconds offset from gmt to local time */
104 static RETSIGTYPE
cleanup(int);
105 static void usage(void) __attribute__((noreturn
));
107 static void dump_packet_and_trunc(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
108 static void dump_packet(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
111 RETSIGTYPE
requestinfo(int);
114 /* Length of saved portion of packet. */
115 int snaplen
= DEFAULT_SNAPLEN
;
122 static struct printer printers
[] = {
123 { arcnet_if_print
, DLT_ARCNET
},
124 { ether_if_print
, DLT_EN10MB
},
125 { token_if_print
, DLT_IEEE802
},
127 { lane_if_print
, DLT_LANE8023
},
130 { cip_if_print
, DLT_CIP
},
133 { cip_if_print
, DLT_ATM_CLIP
},
135 { sl_if_print
, DLT_SLIP
},
136 { sl_bsdos_if_print
, DLT_SLIP_BSDOS
},
137 { ppp_if_print
, DLT_PPP
},
138 { ppp_bsdos_if_print
, DLT_PPP_BSDOS
},
139 { fddi_if_print
, DLT_FDDI
},
140 { null_if_print
, DLT_NULL
},
142 { null_if_print
, DLT_LOOP
},
144 { raw_if_print
, DLT_RAW
},
145 { atm_if_print
, DLT_ATM_RFC1483
},
147 { chdlc_if_print
, DLT_C_HDLC
},
150 { chdlc_if_print
, DLT_HDLC
},
152 #ifdef DLT_PPP_SERIAL
153 { ppp_hdlc_if_print
, DLT_PPP_SERIAL
},
156 { pppoe_if_print
, DLT_PPP_ETHER
},
159 { sll_if_print
, DLT_LINUX_SLL
},
161 #ifdef DLT_IEEE802_11
162 { ieee802_11_if_print
, DLT_IEEE802_11
},
165 { ltalk_if_print
, DLT_LTALK
},
168 { pflog_if_print
, DLT_PFLOG
},
171 { fr_if_print
, DLT_FR
},
174 { fr_if_print
, DLT_FRELAY
},
177 { sunatm_if_print
, DLT_SUNATM
},
179 #ifdef DLT_IP_OVER_FC
180 { ipfc_if_print
, DLT_IP_OVER_FC
},
182 #ifdef DLT_PRISM_HEADER
183 { prism_if_print
, DLT_PRISM_HEADER
},
185 #ifdef DLT_IEEE802_11_RADIO
186 { ieee802_11_radio_if_print
, DLT_IEEE802_11_RADIO
},
192 lookup_printer(int type
)
196 for (p
= printers
; p
->f
; ++p
)
200 error("unknown data link type %d", type
);
217 main(int argc
, char **argv
)
219 register int cnt
, op
, i
;
220 bpf_u_int32 localnet
, netmask
;
221 register char *cp
, *infile
, *cmdbuf
, *device
, *RFileName
, *WFileName
;
222 pcap_handler printer
;
223 struct bpf_program fcode
;
225 RETSIGTYPE (*oldhandler
)(int);
227 struct dump_info dumpinfo
;
228 u_char
*pcap_userdata
;
229 char ebuf
[PCAP_ERRBUF_SIZE
];
230 #ifdef HAVE_PCAP_FINDALLDEVS
231 pcap_if_t
*devpointer
;
236 DWORD dwWindowsMajorVersion
;
237 u_int UserBufferSize
=1000000;
241 dwVersion
=GetVersion(); /* get the OS version */
242 dwWindowsMajorVersion
= (DWORD
)(LOBYTE(LOWORD(dwVersion
)));
243 if(wsockinit()!=0) return 1;
251 if ((cp
= strrchr(argv
[0], '/')) != NULL
)
252 program_name
= cp
+ 1;
254 program_name
= argv
[0];
256 if (abort_on_misalignment(ebuf
, sizeof(ebuf
)) < 0)
266 (op
= getopt(argc
, argv
, "aAB:c:C:dDeE:fF:i:lm:nNOpqr:Rs:StT:uvw:xXY")) != -1)
268 #ifdef HAVE_PCAP_FINDALLDEVS
269 (op
= getopt(argc
, argv
, "aAc:C:dDeE:fF:i:lm:nNOpqr:Rs:StT:uvw:xXY")) != -1)
270 #else /* HAVE_PCAP_FINDALLDEVS */
271 (op
= getopt(argc
, argv
, "aAc:C:deE:fF:i:lm:nNOpqr:Rs:StT:uvw:xXY")) != -1)
272 #endif /* HAVE_PCAP_FINDALLDEVS */
288 UserBufferSize
= atoi(optarg
)*1024;
289 if (UserBufferSize
< 0)
290 error("invalid packet buffer size %s", optarg
);
297 error("invalid packet count %s", optarg
);
301 Cflag
= atoi(optarg
) * 1000000;
303 error("invalid file size %s", optarg
);
310 #ifdef HAVE_PCAP_FINDALLDEVS
312 if (pcap_findalldevs(&devpointer
, ebuf
) < 0)
315 for (i
= 0; devpointer
!= 0; i
++) {
316 printf("%d.%s", i
+1, devpointer
->name
);
317 if (devpointer
->description
!= NULL
)
318 printf(" (%s)", devpointer
->description
);
320 devpointer
= devpointer
->next
;
324 #endif /* HAVE_PCAP_FINDALLDEVS */
331 #ifndef HAVE_LIBCRYPTO
332 warning("crypto code not compiled in");
346 if (optarg
[0] == '0' && optarg
[1] == 0)
347 error("Invalid adapter index");
349 #ifdef HAVE_PCAP_FINDALLDEVS
351 * If the argument is a number, treat it as
352 * an index into the list of adapters, as
353 * printed by "tcpdump -D".
355 * This should be OK on UNIX systems, as interfaces
356 * shouldn't have names that begin with digits.
357 * It can be useful on Windows, where more than
358 * one interface can have the same name.
360 if ((devnum
= atoi(optarg
)) != 0) {
362 error("Invalid adapter index");
364 if (pcap_findalldevs(&devpointer
, ebuf
) < 0)
367 for (i
= 0; i
< devnum
-1; i
++){
368 devpointer
= devpointer
->next
;
369 if (devpointer
== NULL
)
370 error("Invalid adapter index");
373 device
= devpointer
->name
;
376 #endif /* HAVE_PCAP_FINDALLDEVS */
381 #ifdef HAVE_SETLINEBUF
384 setvbuf(stdout
, NULL
, _IOLBF
, 0);
398 if (smiLoadModule(optarg
) == 0) {
399 error("could not load MIB module %s", optarg
);
403 (void)fprintf(stderr
, "%s: ignoring option `-m %s' ",
404 program_name
, optarg
);
405 (void)fprintf(stderr
, "(no libsmi support)\n");
431 snaplen
= strtol(optarg
, &end
, 0);
432 if (optarg
== end
|| *end
!= '\0'
433 || snaplen
< 0 || snaplen
> 65535)
434 error("invalid snaplen %s", optarg
);
435 else if (snaplen
== 0)
449 if (strcasecmp(optarg
, "vat") == 0)
451 else if (strcasecmp(optarg
, "wb") == 0)
453 else if (strcasecmp(optarg
, "rpc") == 0)
455 else if (strcasecmp(optarg
, "rtp") == 0)
457 else if (strcasecmp(optarg
, "rtcp") == 0)
458 packettype
= PT_RTCP
;
459 else if (strcasecmp(optarg
, "snmp") == 0)
460 packettype
= PT_SNMP
;
461 else if (strcasecmp(optarg
, "cnfp") == 0)
462 packettype
= PT_CNFP
;
464 error("unknown packet type `%s'", optarg
);
488 #if defined(HAVE_PCAP_DEBUG) || defined(HAVE_YYDEBUG)
491 /* Undocumented flag */
492 #ifdef HAVE_PCAP_DEBUG
493 extern int pcap_debug
;
508 error("-a and -n options are incompatible");
511 thiszone
= gmt2local(0);
513 if (RFileName
!= NULL
) {
515 * We don't need network access, so set it back to the user id.
516 * Also, this prevents the user from reading anyone's
523 pd
= pcap_open_offline(RFileName
, ebuf
);
529 error("-f and -r options are incompatible");
531 if (device
== NULL
) {
532 device
= pcap_lookupdev(ebuf
);
537 PrintCapBegins(program_name
,device
);
540 pd
= pcap_open_live(device
, snaplen
, !pflag
, 1000, ebuf
);
546 if(UserBufferSize
!= 1000000)
547 if(pcap_setbuff(pd
, UserBufferSize
)==-1){
548 error("%s", pcap_geterr(pd
));
551 i
= pcap_snapshot(pd
);
553 warning("snaplen raised from %d to %d", snaplen
, i
);
556 if (pcap_lookupnet(device
, &localnet
, &netmask
, ebuf
) < 0) {
562 * Let user own process after socket has been opened.
569 cmdbuf
= read_infile(infile
);
571 cmdbuf
= copy_argv(&argv
[optind
]);
573 if (pcap_compile(pd
, &fcode
, cmdbuf
, Oflag
, netmask
) < 0)
574 error("%s", pcap_geterr(pd
));
576 bpf_dump(&fcode
, dflag
);
580 init_addrtoname(localnet
, netmask
);
582 (void)setsignal(SIGTERM
, cleanup
);
583 (void)setsignal(SIGINT
, cleanup
);
584 /* Cooperate with nohup(1) */
586 if ((oldhandler
= setsignal(SIGHUP
, cleanup
)) != SIG_DFL
)
587 (void)setsignal(SIGHUP
, oldhandler
);
590 if (pcap_setfilter(pd
, &fcode
) < 0)
591 error("%s", pcap_geterr(pd
));
593 pcap_dumper_t
*p
= pcap_dump_open(pd
, WFileName
);
595 error("%s", pcap_geterr(pd
));
597 printer
= dump_packet_and_trunc
;
598 dumpinfo
.WFileName
= WFileName
;
601 pcap_userdata
= (u_char
*)&dumpinfo
;
603 printer
= dump_packet
;
604 pcap_userdata
= (u_char
*)p
;
607 printer
= lookup_printer(pcap_datalink(pd
));
611 (void)setsignal(SIGINFO
, requestinfo
);
614 if (RFileName
== NULL
) {
615 (void)fprintf(stderr
, "%s: listening on %s\n",
616 program_name
, device
);
617 (void)fflush(stderr
);
620 if (pcap_loop(pd
, cnt
, printer
, pcap_userdata
) < 0) {
621 (void)fprintf(stderr
, "%s: pcap_loop: %s\n",
622 program_name
, pcap_geterr(pd
));
627 if (RFileName
== NULL
)
633 /* make a clean exit on interrupts */
638 /* Can't print the summary if reading from a savefile */
639 if (pd
!= NULL
&& pcap_file(pd
) == NULL
) {
640 (void)fflush(stdout
);
649 info(register int verbose
)
651 struct pcap_stat stat
;
653 if (pcap_stats(pd
, &stat
) < 0) {
654 (void)fprintf(stderr
, "pcap_stats: %s\n", pcap_geterr(pd
));
659 fprintf(stderr
, "%s: ", program_name
);
661 (void)fprintf(stderr
, "%d packets received by filter", stat
.ps_recv
);
666 (void)fprintf(stderr
, "%d packets dropped by kernel\n", stat
.ps_drop
);
675 for (i
= 0, j
= strlen(s
) - 1; i
< j
; i
++, j
--) {
684 swebitoa(unsigned int n
, char *s
)
690 s
[i
++] = n
% 10 + '0';
691 } while ((n
/= 10) > 0);
698 dump_packet_and_trunc(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
700 struct dump_info
*dump_info
;
706 dump_info
= (struct dump_info
*)user
;
709 * XXX - this won't prevent capture files from getting
710 * larger than Cflag - the last packet written to the
711 * file could put it over Cflag.
713 if (ftell((FILE *)dump_info
->p
) > Cflag
) {
714 name
= (char *) malloc(strlen(dump_info
->WFileName
) + 4);
716 error("dump_packet_and_trunc: malloc");
717 strcpy(name
, dump_info
->WFileName
);
718 swebitoa(cnt
, name
+ strlen(dump_info
->WFileName
));
720 pcap_dump_close(dump_info
->p
);
721 dump_info
->p
= pcap_dump_open(dump_info
->pd
, name
);
723 if (dump_info
->p
== NULL
)
724 error("%s", pcap_geterr(pd
));
727 pcap_dump((u_char
*)dump_info
->p
, h
, sp
);
735 dump_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
739 pcap_dump(user
, h
, sp
);
746 /* Like default_print() but data need not be aligned */
748 default_print_unaligned(register const u_char
*cp
, register u_int length
)
751 register int nshorts
;
754 ascii_print(cp
, length
);
757 nshorts
= (u_int
) length
/ sizeof(u_short
);
759 while (--nshorts
>= 0) {
761 (void)printf("\n\t\t\t");
763 (void)printf(" %02x%02x", s
, *cp
++);
767 (void)printf("\n\t\t\t");
768 (void)printf(" %02x", *cp
);
774 * XXX - there should really be libpcap calls to get the version
775 * number as a string (the string would be generated from #defines
776 * at run time, so that it's not generated from string constants
777 * in the library, as, on many UNIX systems, those constants would
778 * be statically linked into the application executable image, and
779 * would thus reflect the version of libpcap on the system on
780 * which the application was *linked*, not the system on which it's
783 * That routine should be documented, unlike the "version[]"
784 * string, so that UNIX vendors providing their own libpcaps
785 * don't omit it (as a couple of vendors have...).
787 * Packet.dll should perhaps also export a routine to return the
788 * version number of the Packet.dll code, to supply the
789 * "Wpcap_version" information on Windows.
791 char WDversion
[]="current-cvs.tcpdump.org";
792 char version
[]="current-cvs.tcpdump.org";
793 char pcap_version
[]="current-cvs.tcpdump.org";
794 char Wpcap_version
[]="3.0 alpha";
798 * By default, print the packet out in hex.
801 default_print(register const u_char
*bp
, register u_int length
)
803 default_print_unaligned(bp
, length
);
807 RETSIGTYPE
requestinfo(int signo _U_
)
819 extern char version
[];
820 #if defined(WIN32) || defined(HAVE_PCAP_VERSION)
821 extern char pcap_version
[];
823 static char pcap_version
[] = "unknown";
827 (void)fprintf(stderr
, "%s version %s, based on tcpdump version %s\n", program_name
, WDversion
, version
);
828 (void)fprintf(stderr
, "WinPcap version %s, based on libpcap version %s\n",Wpcap_version
, pcap_version
);
830 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
831 (void)fprintf(stderr
, "libpcap version %s\n", pcap_version
);
833 (void)fprintf(stderr
,
835 "Usage: %s [-aAdDeflnNOpqRStuvxX] [-B size] [-c count] [ -C file_size ]\n", program_name
);
837 #ifdef HAVE_PCAP_FINDALLDEVS
838 "Usage: %s [-aAdDeflnNOpqRStuvxX] [-c count] [ -C file_size ]\n", program_name
);
839 #else /* HAVE_PCAP_FINDALLDEVS */
840 "Usage: %s [-aAdeflnNOpqRStuvxX] [-c count] [ -C file_size ]\n", program_name
);
841 #endif /* HAVE_PCAP_FINDALLDEVS */
843 (void)fprintf(stderr
,
844 "\t\t[ -F file ] [ -i interface ] [ -r file ] [ -s snaplen ]\n");
845 (void)fprintf(stderr
,
846 "\t\t[ -T type ] [ -w file ] [ -E algo:secret ] [ expression ]\n");