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