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.173 2001/12/22 22:12:23 guy Exp $ (LBL)";
39 * tcpdump - monitor tcp/ip traffic on an ethernet.
41 * First written in 1987 by Van Jacobson, Lawrence Berkeley Laboratory.
42 * Mercilessly hacked and occasionally improved since then via the
43 * combined efforts of Van, Steve McCanne and Craig Leres of LBL.
50 #include <sys/types.h>
53 #include <netinet/in.h>
64 #include "interface.h"
65 #include "addrtoname.h"
67 #include "setsignal.h"
68 #include "gmt2local.h"
70 int aflag; /* translate network and broadcast addresses */
71 int dflag; /* print filter code */
72 int eflag; /* print ethernet header */
73 int fflag; /* don't translate "foreign" IP address */
74 int Lflag; /* list available data link types and exit */
75 int nflag; /* leave addresses as numbers */
76 int Nflag; /* remove domains from printed host names */
77 int Oflag = 1; /* run filter code optimizer */
78 int pflag; /* don't go promiscuous */
79 int qflag; /* quick (shorter) output */
80 int Rflag = 1; /* print sequence # field in AH/ESP*/
81 int sflag = 0; /* use the libsmi to translate OIDs */
82 int Sflag; /* print raw TCP sequence numbers */
83 int tflag = 1; /* print packet arrival time */
84 int uflag = 0; /* Print undecoded NFS handles */
85 int vflag; /* verbose */
86 int xflag; /* print packet in hex */
87 int Xflag; /* print packet in ascii as well as hex */
88 off_t Cflag = 0; /* rotate dump files after this many bytes */
89 int dlt = -1; /* if != -1, ask libpcap for the DLT it names */
91 const char *dlt_name = NULL;
93 char *espsecret = NULL; /* ESP secret key */
102 int32_t thiszone; /* seconds offset from gmt to local time */
105 static RETSIGTYPE cleanup(int);
106 static void usage(void) __attribute__((noreturn));
107 static void show_dlts_and_exit(pcap_t *pd) __attribute__((noreturn));
109 static void dump_and_trunc(u_char *, const struct pcap_pkthdr *, const u_char *);
112 RETSIGTYPE requestinfo(int);
115 /* Length of saved portion of packet. */
116 int snaplen = DEFAULT_SNAPLEN;
123 static struct printer printers[] = {
124 { arcnet_if_print, DLT_ARCNET },
125 { ether_if_print, DLT_EN10MB },
126 { token_if_print, DLT_IEEE802 },
128 { lane_if_print, DLT_LANE8023 },
131 { cip_if_print, DLT_CIP },
134 { cip_if_print, DLT_ATM_CLIP },
136 { sl_if_print, DLT_SLIP },
137 { sl_bsdos_if_print, DLT_SLIP_BSDOS },
138 { ppp_if_print, DLT_PPP },
139 { ppp_bsdos_if_print, DLT_PPP_BSDOS },
140 { fddi_if_print, DLT_FDDI },
141 { null_if_print, DLT_NULL },
143 { null_if_print, DLT_LOOP },
145 { raw_if_print, DLT_RAW },
146 { atm_if_print, DLT_ATM_RFC1483 },
148 { chdlc_if_print, DLT_C_HDLC },
151 { chdlc_if_print, DLT_HDLC },
153 #ifdef DLT_PPP_SERIAL
154 { ppp_hdlc_if_print, DLT_PPP_SERIAL },
157 { pppoe_if_print, DLT_PPP_ETHER },
160 { sll_if_print, DLT_LINUX_SLL },
162 #ifdef DLT_IEEE802_11
163 { ieee802_11_if_print, DLT_IEEE802_11},
166 { ltalk_if_print, DLT_LTALK },
172 lookup_printer(int type)
176 for (p = printers; p->f; ++p)
180 error("unknown data link type %d", type);
201 #define DLT_CHOICE(code) { #code, code }
202 #define DLT_CHOICE_SENTINEL { NULL, 0 }
204 struct dlt_choice dlt_choices[] = {
205 DLT_CHOICE(DLT_ARCNET),
206 DLT_CHOICE(DLT_EN10MB),
207 DLT_CHOICE(DLT_IEEE802),
209 DLT_CHOICE(DLT_LANE8023),
215 DLT_CHOICE(DLT_ATM_CLIP),
217 DLT_CHOICE(DLT_SLIP),
218 DLT_CHOICE(DLT_SLIP_BSDOS),
220 DLT_CHOICE(DLT_PPP_BSDOS),
221 DLT_CHOICE(DLT_FDDI),
222 DLT_CHOICE(DLT_NULL),
224 DLT_CHOICE(DLT_LOOP),
227 DLT_CHOICE(DLT_ATM_RFC1483),
229 DLT_CHOICE(DLT_C_HDLC),
232 DLT_CHOICE(DLT_HDLC),
234 #ifdef DLT_PPP_SERIAL
235 DLT_CHOICE(DLT_PPP_SERIAL),
238 DLT_CHOICE(DLT_PPP_ETHER),
241 DLT_CHOICE(DLT_LINUX_SLL),
243 #ifdef DLT_IEEE802_11
244 DLT_CHOICE(DLT_IEEE802_11),
247 DLT_CHOICE(DLT_LTALK),
250 DLT_CHOICE(DLT_PFLOG),
256 show_dlts_and_exit(pcap_t *pd)
260 n_dlts = pcap_list_datalinks(pd, &dlts);
262 error("%s", pcap_geterr(pd));
263 else if (n_dlts == 0 || !dlts)
264 error("No data link types.");
266 (void) fprintf(stderr, "Data link types (use option -y):\n");
268 while (--n_dlts >= 0) {
269 for (i = 0; dlt_choices[i].name; i++) {
270 if (dlt_choices[i].dlt != dlts[n_dlts]) {
273 (void) fprintf(stderr, " %s\n",
274 dlt_choices[i].name + sizeof("DLT_") - 1);
277 if (!dlt_choices[i].name)
278 fprintf(stderr, " %d (not supported)\n", dlts[n_dlts]);
285 main(int argc, char **argv)
287 register int cnt, op, i;
288 bpf_u_int32 localnet, netmask;
289 register char *cp, *infile, *cmdbuf, *device, *RFileName, *WFileName;
290 pcap_handler printer;
291 struct bpf_program fcode;
292 RETSIGTYPE (*oldhandler)(int);
293 struct dump_info dumpinfo;
294 u_char *pcap_userdata;
295 char ebuf[PCAP_ERRBUF_SIZE];
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)
316 (op = getopt(argc, argv, "ac:C:deE:fF:i:lLm:nNOpqr:Rs:StT:uvw:xXy:Y")) != -1)
326 error("invalid packet count %s", optarg);
330 Cflag = atoi(optarg) * 1000000;
332 error("invalid file size %s", optarg);
348 #ifndef HAVE_LIBCRYPTO
349 warning("crypto code not compiled in");
367 #ifdef HAVE_SETLINEBUF
370 setvbuf(stdout, NULL, _IOLBF, 0);
384 if (smiLoadModule(optarg) == 0) {
385 error("could not load MIB module %s", optarg);
389 (void)fprintf(stderr, "%s: ignoring option `-m %s' ",
390 program_name, optarg);
391 (void)fprintf(stderr, "(no libsmi support)\n");
417 snaplen = strtol(optarg, &end, 0);
418 if (optarg == end || *end != '\0'
419 || snaplen < 0 || snaplen > 65535)
420 error("invalid snaplen %s", optarg);
421 else if (snaplen == 0)
435 if (strcasecmp(optarg, "vat") == 0)
437 else if (strcasecmp(optarg, "wb") == 0)
439 else if (strcasecmp(optarg, "rpc") == 0)
441 else if (strcasecmp(optarg, "rtp") == 0)
443 else if (strcasecmp(optarg, "rtcp") == 0)
444 packettype = PT_RTCP;
445 else if (strcasecmp(optarg, "snmp") == 0)
446 packettype = PT_SNMP;
447 else if (strcasecmp(optarg, "cnfp") == 0)
448 packettype = PT_CNFP;
450 error("unknown packet type `%s'", optarg);
475 for (i = 0; dlt_choices[i].name; i++) {
476 if (!strcasecmp(dlt_choices[i].name +
479 dlt = dlt_choices[i].dlt;
480 dlt_name = dlt_choices[i].name;
485 error("invalid data link type %s", optarg);
492 /* Undocumented flag */
504 error("-a and -n options are incompatible");
507 thiszone = gmt2local(0);
509 if (RFileName != NULL) {
511 * We don't need network access, so set it back to the user id.
512 * Also, this prevents the user from reading anyone's
517 pd = pcap_open_offline(RFileName, ebuf);
523 error("-f and -r options are incompatible");
525 if (device == NULL) {
526 device = pcap_lookupdev(ebuf);
531 pd = pcap_open_live(device, snaplen, !pflag, 1000, ebuf);
537 show_dlts_and_exit(pd);
540 if (pcap_set_datalink(pd, dlt) < 0)
541 error("%s", pcap_geterr(pd));
542 (void)fprintf(stderr, "%s: data link type %s\n",
543 program_name, dlt_name);
544 (void)fflush(stderr);
546 i = pcap_snapshot(pd);
548 warning("snaplen raised from %d to %d", snaplen, i);
551 if (pcap_lookupnet(device, &localnet, &netmask, ebuf) < 0) {
557 * Let user own process after socket has been opened.
562 cmdbuf = read_infile(infile);
564 cmdbuf = copy_argv(&argv[optind]);
566 if (pcap_compile(pd, &fcode, cmdbuf, Oflag, netmask) < 0)
567 error("%s", pcap_geterr(pd));
569 bpf_dump(&fcode, dflag);
572 init_addrtoname(localnet, netmask);
574 (void)setsignal(SIGTERM, cleanup);
575 (void)setsignal(SIGINT, cleanup);
576 /* Cooperate with nohup(1) */
577 if ((oldhandler = setsignal(SIGHUP, cleanup)) != SIG_DFL)
578 (void)setsignal(SIGHUP, oldhandler);
580 if (pcap_setfilter(pd, &fcode) < 0)
581 error("%s", pcap_geterr(pd));
583 pcap_dumper_t *p = pcap_dump_open(pd, WFileName);
585 error("%s", pcap_geterr(pd));
587 printer = dump_and_trunc;
588 dumpinfo.WFileName = WFileName;
591 pcap_userdata = (u_char *)&dumpinfo;
594 pcap_userdata = (u_char *)p;
597 printer = lookup_printer(pcap_datalink(pd));
600 (void)setsignal(SIGINFO, requestinfo);
603 if (RFileName == NULL) {
604 (void)fprintf(stderr, "%s: listening on %s\n",
605 program_name, device);
606 (void)fflush(stderr);
608 if (pcap_loop(pd, cnt, printer, pcap_userdata) < 0) {
609 (void)fprintf(stderr, "%s: pcap_loop: %s\n",
610 program_name, pcap_geterr(pd));
613 if (RFileName == NULL)
619 /* make a clean exit on interrupts */
624 /* Can't print the summary if reading from a savefile */
625 if (pd != NULL && pcap_file(pd) == NULL) {
626 (void)fflush(stdout);
634 info(register int verbose)
636 struct pcap_stat stat;
638 if (pcap_stats(pd, &stat) < 0) {
639 (void)fprintf(stderr, "pcap_stats: %s\n", pcap_geterr(pd));
643 fprintf(stderr, "%s: ", program_name);
644 (void)fprintf(stderr, "%d packets received by filter", stat.ps_recv);
649 (void)fprintf(stderr, "%d packets dropped by kernel\n", stat.ps_drop);
658 for (i = 0, j = strlen(s) - 1; i < j; i++, j--) {
667 swebitoa(unsigned int n, char *s)
673 s[i++] = n % 10 + '0';
674 } while ((n /= 10) > 0);
681 dump_and_trunc(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
683 struct dump_info *info;
687 info = (struct dump_info *)user;
690 * XXX - this won't prevent capture files from getting
691 * larger than Cflag - the last packet written to the
692 * file could put it over Cflag.
694 if (ftell((FILE *)info->p) > Cflag) {
695 name = (char *) malloc(strlen(info->WFileName) + 4);
697 error("dump_and_trunc: malloc");
698 strcpy(name, info->WFileName);
699 swebitoa(cnt, name + strlen(info->WFileName));
701 pcap_dump_close(info->p);
702 info->p = pcap_dump_open(info->pd, name);
705 error("%s", pcap_geterr(pd));
708 pcap_dump((u_char *)info->p, h, sp);
711 /* Like default_print() but data need not be aligned */
713 default_print_unaligned(register const u_char *cp, register u_int length)
716 register int nshorts;
719 ascii_print(cp, length);
722 nshorts = (u_int) length / sizeof(u_short);
724 while (--nshorts >= 0) {
726 (void)printf("\n\t\t\t");
728 (void)printf(" %02x%02x", s, *cp++);
732 (void)printf("\n\t\t\t");
733 (void)printf(" %02x", *cp);
738 * By default, print the packet out in hex.
741 default_print(register const u_char *bp, register u_int length)
743 default_print_unaligned(bp, length);
747 RETSIGTYPE requestinfo(int signo)
759 extern char version[];
760 extern char pcap_version[];
762 (void)fprintf(stderr, "%s version %s\n", program_name, version);
763 (void)fprintf(stderr, "libpcap version %s\n", pcap_version);
764 (void)fprintf(stderr,
765 "Usage: %s [-adeflLnNOpqRStuvxX] [ -c count ] [ -C file_size ]\n", program_name);
766 (void)fprintf(stderr,
767 "\t\t[ -F file ] [ -i interface ] [ -r file ] [ -s snaplen ]\n");
768 (void)fprintf(stderr,
769 "\t\t[ -T type ] [ -w file ] [ -E algo:secret ] [ -y datalinktype ]\n");
770 (void)fprintf(stderr,
771 "\t\t[ expression ]\n");