]> The Tcpdump Group git mirrors - tcpdump/blob - tcpdump.c
30fdce8b60feaca67c45f194bd7899b15edadae6
[tcpdump] / tcpdump.c
1 /*
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.
4 *
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
16 * written permission.
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.
20 *
21 * Support for splitting captures into multiple files with a maximum
22 * file size:
23 *
24 * Copyright (c) 2001
25 * Seth Webster <swebster@sst.ll.mit.edu>
26 */
27
28 #ifndef lint
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.178 2002-07-11 08:09:47 guy Exp $ (LBL)";
34 #endif
35
36 /*
37 * tcpdump - monitor tcp/ip traffic on an ethernet.
38 *
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.
42 */
43
44 #ifdef HAVE_CONFIG_H
45 #include "config.h"
46 #endif
47
48 #include <sys/types.h>
49 #include <sys/time.h>
50
51 #include <netinet/in.h>
52
53 #include <pcap.h>
54 #include <signal.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <unistd.h>
59 #include <ctype.h>
60
61
62 #include "interface.h"
63 #include "addrtoname.h"
64 #include "machdep.h"
65 #include "setsignal.h"
66 #include "gmt2local.h"
67
68 int aflag; /* translate network and broadcast addresses */
69 int dflag; /* print filter code */
70 int eflag; /* print ethernet header */
71 int fflag; /* don't translate "foreign" IP address */
72 int nflag; /* leave addresses as numbers */
73 int Nflag; /* remove domains from printed host names */
74 int Oflag = 1; /* run filter code optimizer */
75 int pflag; /* don't go promiscuous */
76 int qflag; /* quick (shorter) output */
77 int Rflag = 1; /* print sequence # field in AH/ESP*/
78 int sflag = 0; /* use the libsmi to translate OIDs */
79 int Sflag; /* print raw TCP sequence numbers */
80 int tflag = 1; /* print packet arrival time */
81 int uflag = 0; /* Print undecoded NFS handles */
82 int vflag; /* verbose */
83 int xflag; /* print packet in hex */
84 int Xflag; /* print packet in ascii as well as hex */
85 off_t Cflag = 0; /* rotate dump files after this many bytes */
86 int Aflag = 0; /* print packet only in ascii observing LF, CR, TAB, SPACE */
87
88 char *espsecret = NULL; /* ESP secret key */
89
90 int packettype;
91
92 int infodelay;
93 int infoprint;
94
95 char *program_name;
96
97 int32_t thiszone; /* seconds offset from gmt to local time */
98
99 /* Forwards */
100 static RETSIGTYPE cleanup(int);
101 static void usage(void) __attribute__((noreturn));
102
103 static void dump_and_trunc(u_char *, const struct pcap_pkthdr *, const u_char *);
104
105 #ifdef SIGINFO
106 RETSIGTYPE requestinfo(int);
107 #endif
108
109 /* Length of saved portion of packet. */
110 int snaplen = DEFAULT_SNAPLEN;
111
112 struct printer {
113 pcap_handler f;
114 int type;
115 };
116
117 static struct printer printers[] = {
118 { arcnet_if_print, DLT_ARCNET },
119 { ether_if_print, DLT_EN10MB },
120 { token_if_print, DLT_IEEE802 },
121 #ifdef DLT_LANE8023
122 { lane_if_print, DLT_LANE8023 },
123 #endif
124 #ifdef DLT_CIP
125 { cip_if_print, DLT_CIP },
126 #endif
127 #ifdef DLT_ATM_CLIP
128 { cip_if_print, DLT_ATM_CLIP },
129 #endif
130 { sl_if_print, DLT_SLIP },
131 { sl_bsdos_if_print, DLT_SLIP_BSDOS },
132 { ppp_if_print, DLT_PPP },
133 { ppp_bsdos_if_print, DLT_PPP_BSDOS },
134 { fddi_if_print, DLT_FDDI },
135 { null_if_print, DLT_NULL },
136 #ifdef DLT_LOOP
137 { null_if_print, DLT_LOOP },
138 #endif
139 { raw_if_print, DLT_RAW },
140 { atm_if_print, DLT_ATM_RFC1483 },
141 #ifdef DLT_C_HDLC
142 { chdlc_if_print, DLT_C_HDLC },
143 #endif
144 #ifdef DLT_HDLC
145 { chdlc_if_print, DLT_HDLC },
146 #endif
147 #ifdef DLT_PPP_SERIAL
148 { ppp_hdlc_if_print, DLT_PPP_SERIAL },
149 #endif
150 #ifdef DLT_PPP_ETHER
151 { pppoe_if_print, DLT_PPP_ETHER },
152 #endif
153 #ifdef DLT_LINUX_SLL
154 { sll_if_print, DLT_LINUX_SLL },
155 #endif
156 #ifdef DLT_IEEE802_11
157 { ieee802_11_if_print, DLT_IEEE802_11},
158 #endif
159 #ifdef DLT_LTALK
160 { ltalk_if_print, DLT_LTALK },
161 #endif
162 #ifdef DLT_PFLOG
163 { pflog_if_print, DLT_PFLOG },
164 #endif
165 #ifdef DLT_FR
166 { fr_if_print, DLT_FR },
167 #endif
168 #ifdef DLT_FRELAY
169 { fr_if_print, DLT_FRELAY },
170 #endif
171 { NULL, 0 },
172 };
173
174 static pcap_handler
175 lookup_printer(int type)
176 {
177 struct printer *p;
178
179 for (p = printers; p->f; ++p)
180 if (type == p->type)
181 return p->f;
182
183 error("unknown data link type %d", type);
184 /* NOTREACHED */
185 }
186
187 static pcap_t *pd;
188
189 extern int optind;
190 extern int opterr;
191 extern char *optarg;
192
193 struct dump_info {
194 char *WFileName;
195 pcap_t *pd;
196 pcap_dumper_t *p;
197 };
198
199 int
200 main(int argc, char **argv)
201 {
202 register int cnt, op, i;
203 bpf_u_int32 localnet, netmask;
204 register char *cp, *infile, *cmdbuf, *device, *RFileName, *WFileName;
205 pcap_handler printer;
206 struct bpf_program fcode;
207 RETSIGTYPE (*oldhandler)(int);
208 struct dump_info dumpinfo;
209 u_char *pcap_userdata;
210 char ebuf[PCAP_ERRBUF_SIZE];
211
212 cnt = -1;
213 device = NULL;
214 infile = NULL;
215 RFileName = NULL;
216 WFileName = NULL;
217 if ((cp = strrchr(argv[0], '/')) != NULL)
218 program_name = cp + 1;
219 else
220 program_name = argv[0];
221
222 if (abort_on_misalignment(ebuf, sizeof(ebuf)) < 0)
223 error("%s", ebuf);
224
225 #ifdef LIBSMI
226 smiInit("tcpdump");
227 #endif
228
229 opterr = 0;
230 while (
231 (op = getopt(argc, argv, "aAc:C:deE:fF:i:lm:nNOpqr:Rs:StT:uvw:xXY")) != -1)
232 switch (op) {
233
234 case 'a':
235 ++aflag;
236 break;
237
238 case 'A':
239 ++xflag;
240 ++Xflag;
241 ++Aflag;
242 break;
243
244 case 'c':
245 cnt = atoi(optarg);
246 if (cnt <= 0)
247 error("invalid packet count %s", optarg);
248 break;
249
250 case 'C':
251 Cflag = atoi(optarg) * 1000000;
252 if (Cflag < 0)
253 error("invalid file size %s", optarg);
254 break;
255
256 case 'd':
257 ++dflag;
258 break;
259
260 case 'e':
261 ++eflag;
262 break;
263
264 case 'E':
265 #ifndef HAVE_LIBCRYPTO
266 warning("crypto code not compiled in");
267 #endif
268 espsecret = optarg;
269 break;
270
271 case 'f':
272 ++fflag;
273 break;
274
275 case 'F':
276 infile = optarg;
277 break;
278
279 case 'i':
280 device = optarg;
281 break;
282
283 case 'l':
284 #ifdef HAVE_SETLINEBUF
285 setlinebuf(stdout);
286 #else
287 setvbuf(stdout, NULL, _IOLBF, 0);
288 #endif
289 break;
290
291 case 'n':
292 ++nflag;
293 break;
294
295 case 'N':
296 ++Nflag;
297 break;
298
299 case 'm':
300 #ifdef LIBSMI
301 if (smiLoadModule(optarg) == 0) {
302 error("could not load MIB module %s", optarg);
303 }
304 sflag = 1;
305 #else
306 (void)fprintf(stderr, "%s: ignoring option `-m %s' ",
307 program_name, optarg);
308 (void)fprintf(stderr, "(no libsmi support)\n");
309 #endif
310
311 case 'O':
312 Oflag = 0;
313 break;
314
315 case 'p':
316 ++pflag;
317 break;
318
319 case 'q':
320 ++qflag;
321 break;
322
323 case 'r':
324 RFileName = optarg;
325 break;
326
327 case 'R':
328 Rflag = 0;
329 break;
330
331 case 's': {
332 char *end;
333
334 snaplen = strtol(optarg, &end, 0);
335 if (optarg == end || *end != '\0'
336 || snaplen < 0 || snaplen > 65535)
337 error("invalid snaplen %s", optarg);
338 else if (snaplen == 0)
339 snaplen = 65535;
340 break;
341 }
342
343 case 'S':
344 ++Sflag;
345 break;
346
347 case 't':
348 --tflag;
349 break;
350
351 case 'T':
352 if (strcasecmp(optarg, "vat") == 0)
353 packettype = PT_VAT;
354 else if (strcasecmp(optarg, "wb") == 0)
355 packettype = PT_WB;
356 else if (strcasecmp(optarg, "rpc") == 0)
357 packettype = PT_RPC;
358 else if (strcasecmp(optarg, "rtp") == 0)
359 packettype = PT_RTP;
360 else if (strcasecmp(optarg, "rtcp") == 0)
361 packettype = PT_RTCP;
362 else if (strcasecmp(optarg, "snmp") == 0)
363 packettype = PT_SNMP;
364 else if (strcasecmp(optarg, "cnfp") == 0)
365 packettype = PT_CNFP;
366 else
367 error("unknown packet type `%s'", optarg);
368 break;
369
370 case 'u':
371 ++uflag;
372 break;
373
374 case 'v':
375 ++vflag;
376 break;
377
378 case 'w':
379 WFileName = optarg;
380 break;
381
382 case 'x':
383 ++xflag;
384 break;
385
386 case 'X':
387 ++xflag;
388 ++Xflag;
389 break;
390
391 #ifdef YYDEBUG
392 case 'Y':
393 {
394 /* Undocumented flag */
395 extern int yydebug;
396 yydebug = 1;
397 }
398 break;
399 #endif
400 default:
401 usage();
402 /* NOTREACHED */
403 }
404
405 if (aflag && nflag)
406 error("-a and -n options are incompatible");
407
408 if (tflag > 0)
409 thiszone = gmt2local(0);
410
411 if (RFileName != NULL) {
412 /*
413 * We don't need network access, so set it back to the user id.
414 * Also, this prevents the user from reading anyone's
415 * trace file.
416 */
417 setuid(getuid());
418
419 pd = pcap_open_offline(RFileName, ebuf);
420 if (pd == NULL)
421 error("%s", ebuf);
422 localnet = 0;
423 netmask = 0;
424 if (fflag != 0)
425 error("-f and -r options are incompatible");
426 } else {
427 if (device == NULL) {
428 device = pcap_lookupdev(ebuf);
429 if (device == NULL)
430 error("%s", ebuf);
431 }
432 *ebuf = '\0';
433 pd = pcap_open_live(device, snaplen, !pflag, 1000, ebuf);
434 if (pd == NULL)
435 error("%s", ebuf);
436 else if (*ebuf)
437 warning("%s", ebuf);
438 i = pcap_snapshot(pd);
439 if (snaplen < i) {
440 warning("snaplen raised from %d to %d", snaplen, i);
441 snaplen = i;
442 }
443 if (pcap_lookupnet(device, &localnet, &netmask, ebuf) < 0) {
444 localnet = 0;
445 netmask = 0;
446 warning("%s", ebuf);
447 }
448 /*
449 * Let user own process after socket has been opened.
450 */
451 setuid(getuid());
452 }
453 if (infile)
454 cmdbuf = read_infile(infile);
455 else
456 cmdbuf = copy_argv(&argv[optind]);
457
458 if (pcap_compile(pd, &fcode, cmdbuf, Oflag, netmask) < 0)
459 error("%s", pcap_geterr(pd));
460 if (dflag) {
461 bpf_dump(&fcode, dflag);
462 exit(0);
463 }
464 init_addrtoname(localnet, netmask);
465
466 (void)setsignal(SIGTERM, cleanup);
467 (void)setsignal(SIGINT, cleanup);
468 /* Cooperate with nohup(1) */
469 if ((oldhandler = setsignal(SIGHUP, cleanup)) != SIG_DFL)
470 (void)setsignal(SIGHUP, oldhandler);
471
472 if (pcap_setfilter(pd, &fcode) < 0)
473 error("%s", pcap_geterr(pd));
474 if (WFileName) {
475 pcap_dumper_t *p = pcap_dump_open(pd, WFileName);
476 if (p == NULL)
477 error("%s", pcap_geterr(pd));
478 if (Cflag != 0) {
479 printer = dump_and_trunc;
480 dumpinfo.WFileName = WFileName;
481 dumpinfo.pd = pd;
482 dumpinfo.p = p;
483 pcap_userdata = (u_char *)&dumpinfo;
484 } else {
485 printer = pcap_dump;
486 pcap_userdata = (u_char *)p;
487 }
488 } else {
489 printer = lookup_printer(pcap_datalink(pd));
490 pcap_userdata = 0;
491 #ifdef SIGINFO
492 (void)setsignal(SIGINFO, requestinfo);
493 #endif
494 }
495 if (RFileName == NULL) {
496 (void)fprintf(stderr, "%s: listening on %s\n",
497 program_name, device);
498 (void)fflush(stderr);
499 }
500 if (pcap_loop(pd, cnt, printer, pcap_userdata) < 0) {
501 (void)fprintf(stderr, "%s: pcap_loop: %s\n",
502 program_name, pcap_geterr(pd));
503 cleanup(0);
504 pcap_close(pd);
505 exit(1);
506 }
507 if (RFileName == NULL)
508 info(1);
509 pcap_close(pd);
510 exit(0);
511 }
512
513 /* make a clean exit on interrupts */
514 static RETSIGTYPE
515 cleanup(int signo)
516 {
517
518 /* Can't print the summary if reading from a savefile */
519 if (pd != NULL && pcap_file(pd) == NULL) {
520 (void)fflush(stdout);
521 putc('\n', stderr);
522 info(1);
523 }
524 if (signo)
525 exit(0);
526 }
527
528 void
529 info(register int verbose)
530 {
531 struct pcap_stat stat;
532
533 if (pcap_stats(pd, &stat) < 0) {
534 (void)fprintf(stderr, "pcap_stats: %s\n", pcap_geterr(pd));
535 return;
536 }
537 if (!verbose)
538 fprintf(stderr, "%s: ", program_name);
539 (void)fprintf(stderr, "%d packets received by filter", stat.ps_recv);
540 if (!verbose)
541 fputs(", ", stderr);
542 else
543 putc('\n', stderr);
544 (void)fprintf(stderr, "%d packets dropped by kernel\n", stat.ps_drop);
545 infoprint = 0;
546 }
547
548 static void
549 reverse(char *s)
550 {
551 int i, j, c;
552
553 for (i = 0, j = strlen(s) - 1; i < j; i++, j--) {
554 c = s[i];
555 s[i] = s[j];
556 s[j] = c;
557 }
558 }
559
560
561 static void
562 swebitoa(unsigned int n, char *s)
563 {
564 unsigned int i;
565
566 i = 0;
567 do {
568 s[i++] = n % 10 + '0';
569 } while ((n /= 10) > 0);
570
571 s[i] = '\0';
572 reverse(s);
573 }
574
575 static void
576 dump_and_trunc(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
577 {
578 struct dump_info *info;
579 static uint cnt = 2;
580 char *name;
581
582 info = (struct dump_info *)user;
583
584 /*
585 * XXX - this won't prevent capture files from getting
586 * larger than Cflag - the last packet written to the
587 * file could put it over Cflag.
588 */
589 if (ftell((FILE *)info->p) > Cflag) {
590 name = (char *) malloc(strlen(info->WFileName) + 4);
591 if (name == NULL)
592 error("dump_and_trunc: malloc");
593 strcpy(name, info->WFileName);
594 swebitoa(cnt, name + strlen(info->WFileName));
595 cnt++;
596 pcap_dump_close(info->p);
597 info->p = pcap_dump_open(info->pd, name);
598 free(name);
599 if (info->p == NULL)
600 error("%s", pcap_geterr(pd));
601 }
602
603 pcap_dump((u_char *)info->p, h, sp);
604 }
605
606 /* Like default_print() but data need not be aligned */
607 void
608 default_print_unaligned(register const u_char *cp, register u_int length)
609 {
610 register u_int i, s;
611 register int nshorts;
612
613 if (Xflag) {
614 ascii_print(cp, length);
615 return;
616 }
617 nshorts = (u_int) length / sizeof(u_short);
618 i = 0;
619 while (--nshorts >= 0) {
620 if ((i++ % 8) == 0)
621 (void)printf("\n\t\t\t");
622 s = *cp++;
623 (void)printf(" %02x%02x", s, *cp++);
624 }
625 if (length & 1) {
626 if ((i % 8) == 0)
627 (void)printf("\n\t\t\t");
628 (void)printf(" %02x", *cp);
629 }
630 }
631
632 /*
633 * By default, print the packet out in hex.
634 */
635 void
636 default_print(register const u_char *bp, register u_int length)
637 {
638 default_print_unaligned(bp, length);
639 }
640
641 #ifdef SIGINFO
642 RETSIGTYPE requestinfo(int signo)
643 {
644 if (infodelay)
645 ++infoprint;
646 else
647 info(0);
648 }
649 #endif
650
651 static void
652 usage(void)
653 {
654 extern char version[];
655 extern char pcap_version[];
656
657 (void)fprintf(stderr, "%s version %s\n", program_name, version);
658 (void)fprintf(stderr, "libpcap version %s\n", pcap_version);
659 (void)fprintf(stderr,
660 "Usage: %s [-aAdeflnNOpqRStuvxX] [ -c count ] [ -C file_size ]\n", program_name);
661 (void)fprintf(stderr,
662 "\t\t[ -F file ] [ -i interface ] [ -r file ] [ -s snaplen ]\n");
663 (void)fprintf(stderr,
664 "\t\t[ -T type ] [ -w file ] [ -E algo:secret ] [ expression ]\n");
665 exit(1);
666 }