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