]> The Tcpdump Group git mirrors - tcpdump/blob - tcpdump.c
IEEE 802.11 support, from Charlie Lenahan <[email protected]>.
[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.162 2001-06-12 05:17:18 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 { arcnet_if_print, DLT_ARCNET },
103 { ether_if_print, DLT_EN10MB },
104 { token_if_print, DLT_IEEE802 },
105 #ifdef DLT_LANE8023
106 { lane_if_print, DLT_LANE8023 },
107 #endif
108 #ifdef DLT_CIP
109 { cip_if_print, DLT_CIP },
110 #endif
111 #ifdef DLT_ATM_CLIP
112 { cip_if_print, DLT_ATM_CLIP },
113 #endif
114 { sl_if_print, DLT_SLIP },
115 { sl_bsdos_if_print, DLT_SLIP_BSDOS },
116 { ppp_if_print, DLT_PPP },
117 { ppp_bsdos_if_print, DLT_PPP_BSDOS },
118 { fddi_if_print, DLT_FDDI },
119 { null_if_print, DLT_NULL },
120 #ifdef DLT_LOOP
121 { null_if_print, DLT_LOOP },
122 #endif
123 { raw_if_print, DLT_RAW },
124 { atm_if_print, DLT_ATM_RFC1483 },
125 #ifdef DLT_C_HDLC
126 { chdlc_if_print, DLT_C_HDLC },
127 #endif
128 #ifdef DLT_HDLC
129 { chdlc_if_print, DLT_HDLC },
130 #endif
131 #ifdef DLT_PPP_SERIAL
132 { ppp_hdlc_if_print, DLT_PPP_SERIAL },
133 #endif
134 #ifdef DLT_LINUX_SLL
135 { sll_if_print, DLT_LINUX_SLL },
136 #endif
137 #ifdef DLT_IEEE802_11
138 { ieee802_11_if_print, DLT_IEEE802_11},
139 #endif
140 { NULL, 0 },
141 };
142
143 static pcap_handler
144 lookup_printer(int type)
145 {
146 struct printer *p;
147
148 for (p = printers; p->f; ++p)
149 if (type == p->type)
150 return p->f;
151
152 error("unknown data link type %d", type);
153 /* NOTREACHED */
154 }
155
156 static pcap_t *pd;
157
158 extern int optind;
159 extern int opterr;
160 extern char *optarg;
161
162 int
163 main(int argc, char **argv)
164 {
165 register int cnt, op, i;
166 bpf_u_int32 localnet, netmask;
167 register char *cp, *infile, *cmdbuf, *device, *RFileName, *WFileName;
168 pcap_handler printer;
169 struct bpf_program fcode;
170 RETSIGTYPE (*oldhandler)(int);
171 u_char *pcap_userdata;
172 char ebuf[PCAP_ERRBUF_SIZE];
173
174 cnt = -1;
175 device = NULL;
176 infile = NULL;
177 RFileName = NULL;
178 WFileName = NULL;
179 if ((cp = strrchr(argv[0], '/')) != NULL)
180 program_name = cp + 1;
181 else
182 program_name = argv[0];
183
184 if (abort_on_misalignment(ebuf, sizeof(ebuf)) < 0)
185 error("%s", ebuf);
186
187 #ifdef LIBSMI
188 smiInit("tcpdump");
189 #endif
190
191 opterr = 0;
192 while (
193 (op = getopt(argc, argv, "ac:deE:fF:i:lm:nNOpqr:Rs:StT:uvw:xXY")) != -1)
194 switch (op) {
195
196 case 'a':
197 ++aflag;
198 break;
199
200 case 'c':
201 cnt = atoi(optarg);
202 if (cnt <= 0)
203 error("invalid packet count %s", optarg);
204 break;
205
206 case 'd':
207 ++dflag;
208 break;
209
210 case 'e':
211 ++eflag;
212 break;
213
214 case 'E':
215 #ifndef HAVE_LIBCRYPTO
216 warning("crypto code not compiled in");
217 #endif
218 espsecret = optarg;
219 break;
220
221 case 'f':
222 ++fflag;
223 break;
224
225 case 'F':
226 infile = optarg;
227 break;
228
229 case 'i':
230 device = optarg;
231 break;
232
233 case 'l':
234 #ifdef HAVE_SETLINEBUF
235 setlinebuf(stdout);
236 #else
237 setvbuf(stdout, NULL, _IOLBF, 0);
238 #endif
239 break;
240
241 case 'n':
242 ++nflag;
243 break;
244
245 case 'N':
246 ++Nflag;
247 break;
248
249 case 'm':
250 #ifdef LIBSMI
251 if (smiLoadModule(optarg) == 0) {
252 error("could not load MIB module %s", optarg);
253 }
254 sflag = 1;
255 #else
256 (void)fprintf(stderr, "%s: ignoring option `-m %s' ",
257 program_name, optarg);
258 (void)fprintf(stderr, "(no libsmi support)\n");
259 #endif
260
261 case 'O':
262 Oflag = 0;
263 break;
264
265 case 'p':
266 ++pflag;
267 break;
268
269 case 'q':
270 ++qflag;
271 break;
272
273 case 'r':
274 RFileName = optarg;
275 break;
276
277 case 'R':
278 Rflag = 0;
279 break;
280
281 case 's': {
282 char *end;
283
284 snaplen = strtol(optarg, &end, 0);
285 if (optarg == end || *end != '\0'
286 || snaplen < 0 || snaplen > 65535)
287 error("invalid snaplen %s", optarg);
288 else if (snaplen == 0)
289 snaplen = 65535;
290 break;
291 }
292
293 case 'S':
294 ++Sflag;
295 break;
296
297 case 't':
298 --tflag;
299 break;
300
301 case 'T':
302 if (strcasecmp(optarg, "vat") == 0)
303 packettype = PT_VAT;
304 else if (strcasecmp(optarg, "wb") == 0)
305 packettype = PT_WB;
306 else if (strcasecmp(optarg, "rpc") == 0)
307 packettype = PT_RPC;
308 else if (strcasecmp(optarg, "rtp") == 0)
309 packettype = PT_RTP;
310 else if (strcasecmp(optarg, "rtcp") == 0)
311 packettype = PT_RTCP;
312 else if (strcasecmp(optarg, "snmp") == 0)
313 packettype = PT_SNMP;
314 else if (strcasecmp(optarg, "cnfp") == 0)
315 packettype = PT_CNFP;
316 else
317 error("unknown packet type `%s'", optarg);
318 break;
319
320 case 'u':
321 ++uflag;
322 break;
323
324 case 'v':
325 ++vflag;
326 break;
327
328 case 'w':
329 WFileName = optarg;
330 break;
331
332 case 'x':
333 ++xflag;
334 break;
335
336 case 'X':
337 ++xflag;
338 ++Xflag;
339 break;
340
341 #ifdef YYDEBUG
342 case 'Y':
343 {
344 /* Undocumented flag */
345 extern int yydebug;
346 yydebug = 1;
347 }
348 break;
349 #endif
350 default:
351 usage();
352 /* NOTREACHED */
353 }
354
355 if (aflag && nflag)
356 error("-a and -n options are incompatible");
357
358 if (tflag > 0)
359 thiszone = gmt2local(0);
360
361 if (RFileName != NULL) {
362 /*
363 * We don't need network access, so set it back to the user id.
364 * Also, this prevents the user from reading anyone's
365 * trace file.
366 */
367 setuid(getuid());
368
369 pd = pcap_open_offline(RFileName, ebuf);
370 if (pd == NULL)
371 error("%s", ebuf);
372 localnet = 0;
373 netmask = 0;
374 if (fflag != 0)
375 error("-f and -r options are incompatible");
376 } else {
377 if (device == NULL) {
378 device = pcap_lookupdev(ebuf);
379 if (device == NULL)
380 error("%s", ebuf);
381 }
382 *ebuf = '\0';
383 pd = pcap_open_live(device, snaplen, !pflag, 1000, ebuf);
384 if (pd == NULL)
385 error("%s", ebuf);
386 else if (*ebuf)
387 warning("%s", ebuf);
388 i = pcap_snapshot(pd);
389 if (snaplen < i) {
390 warning("snaplen raised from %d to %d", snaplen, i);
391 snaplen = i;
392 }
393 if (pcap_lookupnet(device, &localnet, &netmask, ebuf) < 0) {
394 localnet = 0;
395 netmask = 0;
396 warning("%s", ebuf);
397 }
398 /*
399 * Let user own process after socket has been opened.
400 */
401 setuid(getuid());
402 }
403 if (infile)
404 cmdbuf = read_infile(infile);
405 else
406 cmdbuf = copy_argv(&argv[optind]);
407
408 if (pcap_compile(pd, &fcode, cmdbuf, Oflag, netmask) < 0)
409 error("%s", pcap_geterr(pd));
410 if (dflag) {
411 bpf_dump(&fcode, dflag);
412 exit(0);
413 }
414 init_addrtoname(localnet, netmask);
415
416 (void)setsignal(SIGTERM, cleanup);
417 (void)setsignal(SIGINT, cleanup);
418 /* Cooperate with nohup(1) */
419 if ((oldhandler = setsignal(SIGHUP, cleanup)) != SIG_DFL)
420 (void)setsignal(SIGHUP, oldhandler);
421
422 if (pcap_setfilter(pd, &fcode) < 0)
423 error("%s", pcap_geterr(pd));
424 if (WFileName) {
425 pcap_dumper_t *p = pcap_dump_open(pd, WFileName);
426 if (p == NULL)
427 error("%s", pcap_geterr(pd));
428 printer = pcap_dump;
429 pcap_userdata = (u_char *)p;
430 } else {
431 printer = lookup_printer(pcap_datalink(pd));
432 pcap_userdata = 0;
433 }
434 if (RFileName == NULL) {
435 (void)fprintf(stderr, "%s: listening on %s\n",
436 program_name, device);
437 (void)fflush(stderr);
438 }
439 if (pcap_loop(pd, cnt, printer, pcap_userdata) < 0) {
440 (void)fprintf(stderr, "%s: pcap_loop: %s\n",
441 program_name, pcap_geterr(pd));
442 exit(1);
443 }
444 pcap_close(pd);
445 exit(0);
446 }
447
448 /* make a clean exit on interrupts */
449 static RETSIGTYPE
450 cleanup(int signo)
451 {
452 struct pcap_stat stat;
453
454 /* Can't print the summary if reading from a savefile */
455 if (pd != NULL && pcap_file(pd) == NULL) {
456 (void)fflush(stdout);
457 putc('\n', stderr);
458 if (pcap_stats(pd, &stat) < 0)
459 (void)fprintf(stderr, "pcap_stats: %s\n",
460 pcap_geterr(pd));
461 else {
462 (void)fprintf(stderr, "%d packets received by filter\n",
463 stat.ps_recv);
464 (void)fprintf(stderr, "%d packets dropped by kernel\n",
465 stat.ps_drop);
466 }
467 }
468 exit(0);
469 }
470
471 /* Like default_print() but data need not be aligned */
472 void
473 default_print_unaligned(register const u_char *cp, register u_int length)
474 {
475 register u_int i, s;
476 register int nshorts;
477
478 if (Xflag) {
479 ascii_print(cp, length);
480 return;
481 }
482 nshorts = (u_int) length / sizeof(u_short);
483 i = 0;
484 while (--nshorts >= 0) {
485 if ((i++ % 8) == 0)
486 (void)printf("\n\t\t\t");
487 s = *cp++;
488 (void)printf(" %02x%02x", s, *cp++);
489 }
490 if (length & 1) {
491 if ((i % 8) == 0)
492 (void)printf("\n\t\t\t");
493 (void)printf(" %02x", *cp);
494 }
495 }
496
497 /*
498 * By default, print the packet out in hex.
499 */
500 void
501 default_print(register const u_char *bp, register u_int length)
502 {
503 default_print_unaligned(bp, length);
504 }
505
506 static void
507 usage(void)
508 {
509 extern char version[];
510 extern char pcap_version[];
511
512 (void)fprintf(stderr, "%s version %s\n", program_name, version);
513 (void)fprintf(stderr, "libpcap version %s\n", pcap_version);
514 (void)fprintf(stderr,
515 "Usage: %s [-adeflnNOpqStuvxX] [-c count] [ -F file ]\n", program_name);
516 (void)fprintf(stderr,
517 "\t\t[ -i interface ] [ -r file ] [ -s snaplen ]\n");
518 (void)fprintf(stderr,
519 "\t\t[ -T type ] [ -w file ] [ expression ]\n");
520 exit(-1);
521 }