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