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