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