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