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.182 2002-08-03 23:16: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
);
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_and_trunc(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
110 RETSIGTYPE
requestinfo(int);
113 /* Length of saved portion of packet. */
114 int snaplen
= DEFAULT_SNAPLEN
;
121 static struct printer printers
[] = {
122 { arcnet_if_print
, DLT_ARCNET
},
123 { ether_if_print
, DLT_EN10MB
},
124 { token_if_print
, DLT_IEEE802
},
126 { lane_if_print
, DLT_LANE8023
},
129 { cip_if_print
, DLT_CIP
},
132 { cip_if_print
, DLT_ATM_CLIP
},
134 { sl_if_print
, DLT_SLIP
},
135 { sl_bsdos_if_print
, DLT_SLIP_BSDOS
},
136 { ppp_if_print
, DLT_PPP
},
137 { ppp_bsdos_if_print
, DLT_PPP_BSDOS
},
138 { fddi_if_print
, DLT_FDDI
},
139 { null_if_print
, DLT_NULL
},
141 { null_if_print
, DLT_LOOP
},
143 { raw_if_print
, DLT_RAW
},
144 { atm_if_print
, DLT_ATM_RFC1483
},
146 { chdlc_if_print
, DLT_C_HDLC
},
149 { chdlc_if_print
, DLT_HDLC
},
151 #ifdef DLT_PPP_SERIAL
152 { ppp_hdlc_if_print
, DLT_PPP_SERIAL
},
155 { pppoe_if_print
, DLT_PPP_ETHER
},
158 { sll_if_print
, DLT_LINUX_SLL
},
160 #ifdef DLT_IEEE802_11
161 { ieee802_11_if_print
, DLT_IEEE802_11
},
164 { ltalk_if_print
, DLT_LTALK
},
167 { pflog_if_print
, DLT_PFLOG
},
170 { fr_if_print
, DLT_FR
},
173 { fr_if_print
, DLT_FRELAY
},
176 { sunatm_if_print
, DLT_SUNATM
},
182 lookup_printer(int type
)
186 for (p
= printers
; p
->f
; ++p
)
190 error("unknown data link type %d", type
);
207 main(int argc
, char **argv
)
209 register int cnt
, op
, i
;
210 bpf_u_int32 localnet
, netmask
;
211 register char *cp
, *infile
, *cmdbuf
, *device
, *RFileName
, *WFileName
;
212 pcap_handler printer
;
213 struct bpf_program fcode
;
215 RETSIGTYPE (*oldhandler
)(int);
217 struct dump_info dumpinfo
;
218 u_char
*pcap_userdata
;
219 char ebuf
[PCAP_ERRBUF_SIZE
];
220 #ifdef HAVE_PCAP_FINDALLDEVS
221 pcap_if_t
*devpointer
;
227 DWORD dwWindowsMajorVersion
;
228 u_int UserBufferSize
=1000000;
232 dwVersion
=GetVersion(); /* get the OS version */
233 dwWindowsMajorVersion
= (DWORD
)(LOBYTE(LOWORD(dwVersion
)));
234 if(wsockinit()!=0) return 1;
242 if ((cp
= strrchr(argv
[0], '/')) != NULL
)
243 program_name
= cp
+ 1;
245 program_name
= argv
[0];
247 if (abort_on_misalignment(ebuf
, sizeof(ebuf
)) < 0)
257 (op
= getopt(argc
, argv
, "aB:c:C:dDeE:fF:i:lm:nNOpqr:Rs:StT:uvw:xXY")) != -1)
259 #ifdef HAVE_PCAP_FINDALLDEVS
260 (op
= getopt(argc
, argv
, "ac:C:dDeE:fF:i:lm:nNOpqr:Rs:StT:uvw:xXY")) != -1)
261 #else /* HAVE_PCAP_FINDALLDEVS */
262 (op
= getopt(argc
, argv
, "ac:C:deE:fF:i:lm:nNOpqr:Rs:StT:uvw:xXY")) != -1)
263 #endif /* HAVE_PCAP_FINDALLDEVS */
279 UserBufferSize
= atoi(optarg
)*1024;
280 if (UserBufferSize
< 0)
281 error("invalid packet buffer size %s", optarg
);
288 error("invalid packet count %s", optarg
);
292 Cflag
= atoi(optarg
) * 1000000;
294 error("invalid file size %s", optarg
);
301 #ifdef HAVE_PCAP_FINDALLDEVS
303 if (pcap_findalldevs(&devpointer
, ebuf
) < 0)
306 for (i
= 0; devpointer
!= 0; i
++) {
307 printf("%d.%s", i
+1, devpointer
->name
);
308 if (devpointer
->description
!= NULL
)
309 printf(" (%s)", devpointer
->description
);
311 devpointer
= devpointer
->next
;
315 #endif /* HAVE_PCAP_FINDALLDEVS */
322 #ifndef HAVE_LIBCRYPTO
323 warning("crypto code not compiled in");
337 if (optarg
[0] == '0' && optarg
[1] == 0)
338 error("Invalid adapter index");
340 #ifdef HAVE_PCAP_FINDALLDEVS
342 * If the argument is a number, treat it as
343 * an index into the list of adapters, as
344 * printed by "tcpdump -D".
346 * This should be OK on UNIX systems, as interfaces
347 * shouldn't have names that begin with digits.
348 * It can be useful on Windows, where more than
349 * one interface can have the same name.
351 if ((devnum
= atoi(optarg
)) != 0) {
353 error("Invalid adapter index");
355 if (pcap_findalldevs(&devpointer
, ebuf
) < 0)
358 for (i
= 0; i
< devnum
-1; i
++){
359 devpointer
= devpointer
->next
;
360 if (devpointer
== NULL
)
361 error("Invalid adapter index");
364 device
= devpointer
->name
;
367 #endif /* HAVE_PCAP_FINDALLDEVS */
372 #ifdef HAVE_SETLINEBUF
375 setvbuf(stdout
, NULL
, _IOLBF
, 0);
389 if (smiLoadModule(optarg
) == 0) {
390 error("could not load MIB module %s", optarg
);
394 (void)fprintf(stderr
, "%s: ignoring option `-m %s' ",
395 program_name
, optarg
);
396 (void)fprintf(stderr
, "(no libsmi support)\n");
422 snaplen
= strtol(optarg
, &end
, 0);
423 if (optarg
== end
|| *end
!= '\0'
424 || snaplen
< 0 || snaplen
> 65535)
425 error("invalid snaplen %s", optarg
);
426 else if (snaplen
== 0)
440 if (strcasecmp(optarg
, "vat") == 0)
442 else if (strcasecmp(optarg
, "wb") == 0)
444 else if (strcasecmp(optarg
, "rpc") == 0)
446 else if (strcasecmp(optarg
, "rtp") == 0)
448 else if (strcasecmp(optarg
, "rtcp") == 0)
449 packettype
= PT_RTCP
;
450 else if (strcasecmp(optarg
, "snmp") == 0)
451 packettype
= PT_SNMP
;
452 else if (strcasecmp(optarg
, "cnfp") == 0)
453 packettype
= PT_CNFP
;
455 error("unknown packet type `%s'", optarg
);
482 /* Undocumented flag */
494 error("-a and -n options are incompatible");
497 thiszone
= gmt2local(0);
499 if (RFileName
!= NULL
) {
501 * We don't need network access, so set it back to the user id.
502 * Also, this prevents the user from reading anyone's
509 pd
= pcap_open_offline(RFileName
, ebuf
);
515 error("-f and -r options are incompatible");
517 if (device
== NULL
) {
518 device
= pcap_lookupdev(ebuf
);
525 if (!(dwVersion
>= 0x80000000 && dwWindowsMajorVersion
>= 4)) /* Windows '95 */
528 device
=(char*)SChar2WChar(device
);
531 for (ii
=0;ii
<(signed)strlen(device
);ii
++)
532 for (ii
=strlen(device
)-1;ii
>0&&(device
[ii
]==' '||device
[ii
]=='\t');ii
--)device
[ii
]='\0';
533 for (ii
=0;ii
<(signed)strlen(device
)&&(device
[ii
]==' '||device
[ii
]=='\t');ii
++);
534 strcpy(device
,device
+ii
);
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
);
582 init_addrtoname(localnet
, netmask
);
584 (void)setsignal(SIGTERM
, cleanup
);
585 (void)setsignal(SIGINT
, cleanup
);
586 /* Cooperate with nohup(1) */
588 if ((oldhandler
= setsignal(SIGHUP
, cleanup
)) != SIG_DFL
)
589 (void)setsignal(SIGHUP
, oldhandler
);
592 if (pcap_setfilter(pd
, &fcode
) < 0)
593 error("%s", pcap_geterr(pd
));
595 pcap_dumper_t
*p
= pcap_dump_open(pd
, WFileName
);
597 error("%s", pcap_geterr(pd
));
599 printer
= dump_and_trunc
;
600 dumpinfo
.WFileName
= WFileName
;
603 pcap_userdata
= (u_char
*)&dumpinfo
;
606 pcap_userdata
= (u_char
*)p
;
609 printer
= lookup_printer(pcap_datalink(pd
));
612 (void)setsignal(SIGINFO
, requestinfo
);
616 if (RFileName
== NULL
) {
617 (void)fprintf(stderr
, "%s: listening on %s\n",
618 program_name
, device
);
619 (void)fflush(stderr
);
622 if (pcap_loop(pd
, cnt
, printer
, pcap_userdata
) < 0) {
623 (void)fprintf(stderr
, "%s: pcap_loop: %s\n",
624 program_name
, pcap_geterr(pd
));
629 if (RFileName
== NULL
)
635 /* make a clean exit on interrupts */
640 /* Can't print the summary if reading from a savefile */
641 if (pd
!= NULL
&& pcap_file(pd
) == NULL
) {
642 (void)fflush(stdout
);
651 info(register int verbose
)
653 struct pcap_stat stat
;
655 if (pcap_stats(pd
, &stat
) < 0) {
656 (void)fprintf(stderr
, "pcap_stats: %s\n", pcap_geterr(pd
));
661 fprintf(stderr
, "%s: ", program_name
);
663 (void)fprintf(stderr
, "%d packets received by filter", stat
.ps_recv
);
668 (void)fprintf(stderr
, "%d packets dropped by kernel\n", stat
.ps_drop
);
677 for (i
= 0, j
= strlen(s
) - 1; i
< j
; i
++, j
--) {
686 swebitoa(unsigned int n
, char *s
)
692 s
[i
++] = n
% 10 + '0';
693 } while ((n
/= 10) > 0);
700 dump_and_trunc(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
702 struct dump_info
*info
;
706 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 *)info
->p
) > Cflag
) {
714 name
= (char *) malloc(strlen(info
->WFileName
) + 4);
716 error("dump_and_trunc: malloc");
717 strcpy(name
, info
->WFileName
);
718 swebitoa(cnt
, name
+ strlen(info
->WFileName
));
720 pcap_dump_close(info
->p
);
721 info
->p
= pcap_dump_open(info
->pd
, name
);
724 error("%s", pcap_geterr(pd
));
727 pcap_dump((u_char
*)info
->p
, h
, sp
);
730 /* Like default_print() but data need not be aligned */
732 default_print_unaligned(register const u_char
*cp
, register u_int length
)
735 register int nshorts
;
738 ascii_print(cp
, length
);
741 nshorts
= (u_int
) length
/ sizeof(u_short
);
743 while (--nshorts
>= 0) {
745 (void)printf("\n\t\t\t");
747 (void)printf(" %02x%02x", s
, *cp
++);
751 (void)printf("\n\t\t\t");
752 (void)printf(" %02x", *cp
);
757 char WDversion
[]="current-cvs.tcpdump.org";
758 char version
[]="current-cvs.tcpdump.org";
759 char pcap_version
[]="current-cvs.tcpdump.org";
760 char Wpcap_version
[]="3.0 alpha";
764 * By default, print the packet out in hex.
767 default_print(register const u_char
*bp
, register u_int length
)
769 default_print_unaligned(bp
, length
);
773 RETSIGTYPE
requestinfo(int signo
)
785 extern char version
[];
786 extern char pcap_version
[];
789 (void)fprintf(stderr
, "%s version %s, based on tcpdump version %s\n", program_name
, WDversion
, version
);
790 (void)fprintf(stderr
, "WinPcap version %s, based on libpcap version %s\n",Wpcap_version
, pcap_version
);
792 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
793 (void)fprintf(stderr
, "libpcap version %s\n", pcap_version
);
795 (void)fprintf(stderr
,
797 "Usage: %s [-adDeflnNOpqStuvxX] [-B size] [-c count] [ -C file_size ]\n", program_name
);
799 #ifdef HAVE_PCAP_FINDALLDEVS
800 "Usage: %s [-adDeflnNOpqStuvxX] [-c count] [ -C file_size ]\n", program_name
);
801 #else /* HAVE_PCAP_FINDALLDEVS */
802 "Usage: %s [-adeflnNOpqStuvxX] [-c count] [ -C file_size ]\n", program_name
);
803 #endif /* HAVE_PCAP_FINDALLDEVS */
805 (void)fprintf(stderr
,
806 "\t\t[ -F file ] [ -i interface ] [ -r file ] [ -s snaplen ]\n");
807 (void)fprintf(stderr
,
808 "\t\t[ -T type ] [ -w file ] [ -E algo:secret ] [ expression ]\n");