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