]> The Tcpdump Group git mirrors - tcpdump/blob - tcpdump.c
086793949749bb4616dbee592b026d6fd8fe1ceb
[tcpdump] / tcpdump.c
1 /*
2 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
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
22 #ifndef lint
23 static const char copyright[] =
24 "@(#) Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997\n\
25 The Regents of the University of California. All rights reserved.\n";
26 static const char rcsid[] =
27 "@(#) $Header: /tcpdump/master/tcpdump/tcpdump.c,v 1.155 2000-10-12 03:57:13 guy Exp $ (LBL)";
28 #endif
29
30 /*
31 * tcpdump - monitor tcp/ip traffic on an ethernet.
32 *
33 * First written in 1987 by Van Jacobson, Lawrence Berkeley Laboratory.
34 * Mercilessly hacked and occasionally improved since then via the
35 * combined efforts of Van, Steve McCanne and Craig Leres of LBL.
36 */
37
38 #ifdef HAVE_CONFIG_H
39 #include "config.h"
40 #endif
41
42 #include <sys/types.h>
43 #include <sys/time.h>
44
45 #include <netinet/in.h>
46
47 #include <pcap.h>
48 #include <signal.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <unistd.h>
53 #include <ctype.h>
54
55
56 #include "interface.h"
57 #include "addrtoname.h"
58 #include "machdep.h"
59 #include "setsignal.h"
60 #include "gmt2local.h"
61
62 int aflag; /* translate network and broadcast addresses */
63 int dflag; /* print filter code */
64 int eflag; /* print ethernet header */
65 int fflag; /* don't translate "foreign" IP address */
66 int nflag; /* leave addresses as numbers */
67 int Nflag; /* remove domains from printed host names */
68 int Oflag = 1; /* run filter code optimizer */
69 int pflag; /* don't go promiscuous */
70 int qflag; /* quick (shorter) output */
71 int Rflag = 1; /* print sequence # field in AH/ESP*/
72 int sflag = 0; /* use the libsmi to translate OIDs */
73 int Sflag; /* print raw TCP sequence numbers */
74 int tflag = 1; /* print packet arrival time */
75 int uflag = 0; /* Print undecoded NFS handles */
76 int vflag; /* verbose */
77 int xflag; /* print packet in hex */
78 int Xflag; /* print packet in ascii as well as hex */
79
80 char *espsecret = NULL; /* ESP secret key */
81
82 int packettype;
83
84
85 char *program_name;
86
87 int32_t thiszone; /* seconds offset from gmt to local time */
88
89 /* Forwards */
90 static RETSIGTYPE cleanup(int);
91 static void usage(void) __attribute__((noreturn));
92
93 /* Length of saved portion of packet. */
94 int snaplen = DEFAULT_SNAPLEN;
95
96 struct printer {
97 pcap_handler f;
98 int type;
99 };
100
101 static struct printer printers[] = {
102 { ether_if_print, DLT_EN10MB },
103 { token_if_print, DLT_IEEE802 },
104 #ifdef DLT_LANE8023
105 { lane_if_print, DLT_LANE8023 },
106 #endif
107 #ifdef DLT_CIP
108 { cip_if_print, DLT_CIP },
109 #endif
110 { cip_if_print, DLT_ATM_CLIP },
111 { sl_if_print, DLT_SLIP },
112 { sl_bsdos_if_print, DLT_SLIP_BSDOS },
113 { ppp_if_print, DLT_PPP },
114 { ppp_bsdos_if_print, DLT_PPP_BSDOS },
115 { fddi_if_print, DLT_FDDI },
116 { null_if_print, DLT_NULL },
117 { raw_if_print, DLT_RAW },
118 { atm_if_print, DLT_ATM_RFC1483 },
119 { chdlc_if_print, DLT_C_HDLC },
120 { ppp_hdlc_if_print, DLT_PPP_SERIAL },
121 { NULL, 0 },
122 };
123
124 static pcap_handler
125 lookup_printer(int type)
126 {
127 struct printer *p;
128
129 for (p = printers; p->f; ++p)
130 if (type == p->type)
131 return p->f;
132
133 error("unknown data link type %d", type);
134 /* NOTREACHED */
135 }
136
137 static pcap_t *pd;
138
139 extern int optind;
140 extern int opterr;
141 extern char *optarg;
142
143 int
144 main(int argc, char **argv)
145 {
146 register int cnt, op, i;
147 bpf_u_int32 localnet, netmask;
148 register char *cp, *infile, *cmdbuf, *device, *RFileName, *WFileName;
149 pcap_handler printer;
150 struct bpf_program fcode;
151 RETSIGTYPE (*oldhandler)(int);
152 u_char *pcap_userdata;
153 char ebuf[PCAP_ERRBUF_SIZE];
154
155 cnt = -1;
156 device = NULL;
157 infile = NULL;
158 RFileName = NULL;
159 WFileName = NULL;
160 if ((cp = strrchr(argv[0], '/')) != NULL)
161 program_name = cp + 1;
162 else
163 program_name = argv[0];
164
165 if (abort_on_misalignment(ebuf, sizeof(ebuf)) < 0)
166 error("%s", ebuf);
167
168 #ifdef LIBSMI
169 smiInit("tcpdump");
170 #endif
171
172 opterr = 0;
173 while (
174 (op = getopt(argc, argv, "ac:deE:fF:i:lm:nNOpqr:Rs:StT:uvw:xXY")) != -1)
175 switch (op) {
176
177 case 'a':
178 ++aflag;
179 break;
180
181 case 'c':
182 cnt = atoi(optarg);
183 if (cnt <= 0)
184 error("invalid packet count %s", optarg);
185 break;
186
187 case 'd':
188 ++dflag;
189 break;
190
191 case 'e':
192 ++eflag;
193 break;
194
195 case 'E':
196 #ifndef HAVE_LIBCRYPTO
197 warning("crypto code not compiled in");
198 #endif
199 espsecret = optarg;
200 break;
201
202 case 'f':
203 ++fflag;
204 break;
205
206 case 'F':
207 infile = optarg;
208 break;
209
210 case 'i':
211 device = optarg;
212 break;
213
214 case 'l':
215 #ifdef HAVE_SETLINEBUF
216 setlinebuf(stdout);
217 #else
218 setvbuf(stdout, NULL, _IOLBF, 0);
219 #endif
220 break;
221
222 case 'n':
223 ++nflag;
224 break;
225
226 case 'N':
227 ++Nflag;
228 break;
229
230 case 'm':
231 #ifdef LIBSMI
232 if (smiLoadModule(optarg) == 0) {
233 error("could not load MIB module %s", optarg);
234 }
235 sflag = 1;
236 #else
237 (void)fprintf(stderr, "%s: ignoring option `-m %s' ",
238 program_name, optarg);
239 (void)fprintf(stderr, "(no libsmi support)\n");
240 #endif
241
242 case 'O':
243 Oflag = 0;
244 break;
245
246 case 'p':
247 ++pflag;
248 break;
249
250 case 'q':
251 ++qflag;
252 break;
253
254 case 'r':
255 RFileName = optarg;
256 break;
257
258 case 'R':
259 Rflag = 0;
260 break;
261
262 case 's': {
263 char *end;
264
265 snaplen = strtol(optarg, &end, 0);
266 if (optarg == end || *end != '\0'
267 || snaplen < 0 || snaplen > 65535)
268 error("invalid snaplen %s", optarg);
269 else if (snaplen == 0)
270 snaplen = 65535;
271 break;
272 }
273
274 case 'S':
275 ++Sflag;
276 break;
277
278 case 't':
279 --tflag;
280 break;
281
282 case 'T':
283 if (strcasecmp(optarg, "vat") == 0)
284 packettype = PT_VAT;
285 else if (strcasecmp(optarg, "wb") == 0)
286 packettype = PT_WB;
287 else if (strcasecmp(optarg, "rpc") == 0)
288 packettype = PT_RPC;
289 else if (strcasecmp(optarg, "rtp") == 0)
290 packettype = PT_RTP;
291 else if (strcasecmp(optarg, "rtcp") == 0)
292 packettype = PT_RTCP;
293 else if (strcasecmp(optarg, "snmp") == 0)
294 packettype = PT_SNMP;
295 else if (strcasecmp(optarg, "cnfp") == 0)
296 packettype = PT_CNFP;
297 else
298 error("unknown packet type `%s'", optarg);
299 break;
300
301 case 'u':
302 ++uflag;
303 break;
304
305 case 'v':
306 ++vflag;
307 break;
308
309 case 'w':
310 WFileName = optarg;
311 break;
312
313 case 'x':
314 ++xflag;
315 break;
316
317 case 'X':
318 ++xflag;
319 ++Xflag;
320 break;
321
322 #ifdef YYDEBUG
323 case 'Y':
324 {
325 /* Undocumented flag */
326 extern int yydebug;
327 yydebug = 1;
328 }
329 break;
330 #endif
331 default:
332 usage();
333 /* NOTREACHED */
334 }
335
336 if (aflag && nflag)
337 error("-a and -n options are incompatible");
338
339 if (tflag > 0)
340 thiszone = gmt2local(0);
341
342 if (RFileName != NULL) {
343 /*
344 * We don't need network access, so set it back to the user id.
345 * Also, this prevents the user from reading anyone's
346 * trace file.
347 */
348 setuid(getuid());
349
350 pd = pcap_open_offline(RFileName, ebuf);
351 if (pd == NULL)
352 error("%s", ebuf);
353 localnet = 0;
354 netmask = 0;
355 if (fflag != 0)
356 error("-f and -r options are incompatible");
357 } else {
358 if (device == NULL) {
359 device = pcap_lookupdev(ebuf);
360 if (device == NULL)
361 error("%s", ebuf);
362 }
363 pd = pcap_open_live(device, snaplen, !pflag, 1000, ebuf);
364 if (pd == NULL)
365 error("%s", ebuf);
366 i = pcap_snapshot(pd);
367 if (snaplen < i) {
368 warning("snaplen raised from %d to %d", snaplen, i);
369 snaplen = i;
370 }
371 if (pcap_lookupnet(device, &localnet, &netmask, ebuf) < 0) {
372 localnet = 0;
373 netmask = 0;
374 warning("%s", ebuf);
375 }
376 /*
377 * Let user own process after socket has been opened.
378 */
379 setuid(getuid());
380 }
381 if (infile)
382 cmdbuf = read_infile(infile);
383 else
384 cmdbuf = copy_argv(&argv[optind]);
385
386 if (pcap_compile(pd, &fcode, cmdbuf, Oflag, netmask) < 0)
387 error("%s", pcap_geterr(pd));
388 if (dflag) {
389 bpf_dump(&fcode, dflag);
390 exit(0);
391 }
392 init_addrtoname(localnet, netmask);
393
394 (void)setsignal(SIGTERM, cleanup);
395 (void)setsignal(SIGINT, cleanup);
396 /* Cooperate with nohup(1) */
397 if ((oldhandler = setsignal(SIGHUP, cleanup)) != SIG_DFL)
398 (void)setsignal(SIGHUP, oldhandler);
399
400 if (pcap_setfilter(pd, &fcode) < 0)
401 error("%s", pcap_geterr(pd));
402 if (WFileName) {
403 pcap_dumper_t *p = pcap_dump_open(pd, WFileName);
404 if (p == NULL)
405 error("%s", pcap_geterr(pd));
406 printer = pcap_dump;
407 pcap_userdata = (u_char *)p;
408 } else {
409 printer = lookup_printer(pcap_datalink(pd));
410 pcap_userdata = 0;
411 }
412 if (RFileName == NULL) {
413 (void)fprintf(stderr, "%s: listening on %s\n",
414 program_name, device);
415 (void)fflush(stderr);
416 }
417 if (pcap_loop(pd, cnt, printer, pcap_userdata) < 0) {
418 (void)fprintf(stderr, "%s: pcap_loop: %s\n",
419 program_name, pcap_geterr(pd));
420 exit(1);
421 }
422 pcap_close(pd);
423 exit(0);
424 }
425
426 /* make a clean exit on interrupts */
427 static RETSIGTYPE
428 cleanup(int signo)
429 {
430 struct pcap_stat stat;
431
432 /* Can't print the summary if reading from a savefile */
433 if (pd != NULL && pcap_file(pd) == NULL) {
434 (void)fflush(stdout);
435 putc('\n', stderr);
436 if (pcap_stats(pd, &stat) < 0)
437 (void)fprintf(stderr, "pcap_stats: %s\n",
438 pcap_geterr(pd));
439 else {
440 (void)fprintf(stderr, "%d packets received by filter\n",
441 stat.ps_recv);
442 (void)fprintf(stderr, "%d packets dropped by kernel\n",
443 stat.ps_drop);
444 }
445 }
446 exit(0);
447 }
448
449 /* Like default_print() but data need not be aligned */
450 void
451 default_print_unaligned(register const u_char *cp, register u_int length)
452 {
453 register u_int i, s;
454 register int nshorts;
455
456 if (Xflag) {
457 ascii_print(cp, length);
458 return;
459 }
460 nshorts = (u_int) length / sizeof(u_short);
461 i = 0;
462 while (--nshorts >= 0) {
463 if ((i++ % 8) == 0)
464 (void)printf("\n\t\t\t");
465 s = *cp++;
466 (void)printf(" %02x%02x", s, *cp++);
467 }
468 if (length & 1) {
469 if ((i % 8) == 0)
470 (void)printf("\n\t\t\t");
471 (void)printf(" %02x", *cp);
472 }
473 }
474
475 /*
476 * By default, print the packet out in hex.
477 */
478 void
479 default_print(register const u_char *bp, register u_int length)
480 {
481 default_print_unaligned(bp, length);
482 }
483
484 static void
485 usage(void)
486 {
487 extern char version[];
488 extern char pcap_version[];
489
490 (void)fprintf(stderr, "%s version %s\n", program_name, version);
491 (void)fprintf(stderr, "libpcap version %s\n", pcap_version);
492 (void)fprintf(stderr,
493 "Usage: %s [-adeflnNOpqStuvxX] [-c count] [ -F file ]\n", program_name);
494 (void)fprintf(stderr,
495 "\t\t[ -i interface ] [ -r file ] [ -s snaplen ]\n");
496 (void)fprintf(stderr,
497 "\t\t[ -T type ] [ -w file ] [ expression ]\n");
498 exit(-1);
499 }