]> The Tcpdump Group git mirrors - tcpdump/blob - tcpdump.c
add support for DLT_JUNIPER_PPPOE_ATM printer
[tcpdump] / tcpdump.c
1 /*
2 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000
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 * Support for splitting captures into multiple files with a maximum
22 * file size:
23 *
24 * Copyright (c) 2001
25 * Seth Webster <swebster@sst.ll.mit.edu>
26 */
27
28 #ifndef lint
29 static const char copyright[] _U_ =
30 "@(#) Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000\n\
31 The Regents of the University of California. All rights reserved.\n";
32 static const char rcsid[] _U_ =
33 "@(#) $Header: /tcpdump/master/tcpdump/tcpdump.c,v 1.256 2005-05-12 07:10:55 hannes Exp $ (LBL)";
34 #endif
35
36 /*
37 * tcpdump - monitor tcp/ip traffic on an ethernet.
38 *
39 * First written in 1987 by Van Jacobson, Lawrence Berkeley Laboratory.
40 * Mercilessly hacked and occasionally improved since then via the
41 * combined efforts of Van, Steve McCanne and Craig Leres of LBL.
42 */
43
44 #ifdef HAVE_CONFIG_H
45 #include "config.h"
46 #endif
47
48 #include <tcpdump-stdinc.h>
49
50 #ifdef WIN32
51 #include "getopt.h"
52 #include "w32_fzs.h"
53 extern int strcasecmp (const char *__s1, const char *__s2);
54 extern int SIZE_BUF;
55 #define off_t long
56 #define uint UINT
57 #endif /* WIN32 */
58
59 #ifdef HAVE_SMI_H
60 #include <smi.h>
61 #endif
62
63 #include <pcap.h>
64 #include <signal.h>
65 #include <stdio.h>
66 #include <stdlib.h>
67 #include <string.h>
68 #ifndef WIN32
69 #include <pwd.h>
70 #include <grp.h>
71 #include <errno.h>
72 #endif /* WIN32 */
73
74 #include "netdissect.h"
75 #include "interface.h"
76 #include "addrtoname.h"
77 #include "machdep.h"
78 #include "setsignal.h"
79 #include "gmt2local.h"
80 #include "pcap-missing.h"
81
82 netdissect_options Gndo;
83 netdissect_options *gndo = &Gndo;
84
85 /*
86 * Define the maximum number of files for the -C flag, and how many
87 * characters can be added to a filename for the -C flag (which
88 * should be enough to handle MAX_CFLAG - 1).
89 */
90 #define MAX_CFLAG 1000000
91 #define MAX_CFLAG_CHARS 6
92
93 int dflag; /* print filter code */
94 int Lflag; /* list available data link types and exit */
95
96 static int infodelay;
97 static int infoprint;
98
99 char *program_name;
100
101 int32_t thiszone; /* seconds offset from gmt to local time */
102
103 /* Forwards */
104 static RETSIGTYPE cleanup(int);
105 static void usage(void) __attribute__((noreturn));
106 static void show_dlts_and_exit(pcap_t *pd) __attribute__((noreturn));
107
108 static void print_packet(u_char *, const struct pcap_pkthdr *, const u_char *);
109 static void ndo_default_print(netdissect_options *, const u_char *, u_int);
110 static void dump_packet_and_trunc(u_char *, const struct pcap_pkthdr *, const u_char *);
111 static void dump_packet(u_char *, const struct pcap_pkthdr *, const u_char *);
112 static void droproot(const char *, const char *);
113 static void ndo_error(netdissect_options *ndo, const char *fmt, ...);
114 static void ndo_warning(netdissect_options *ndo, const char *fmt, ...);
115
116 #ifdef SIGINFO
117 RETSIGTYPE requestinfo(int);
118 #endif
119
120 #if defined(USE_WIN32_MM_TIMER)
121 #include <MMsystem.h>
122 static UINT timer_id;
123 static void CALLBACK verbose_stats_dump(UINT, UINT, DWORD_PTR, DWORD_PTR, DWORD_PTR);
124 #elif defined(HAVE_ALARM)
125 static void verbose_stats_dump(int sig);
126 #endif
127
128 static void info(int);
129 static u_int packets_captured;
130
131 typedef u_int (*if_printer)(const struct pcap_pkthdr *, const u_char *);
132
133 struct printer {
134 if_printer f;
135 int type;
136 };
137
138 static struct printer printers[] = {
139 { arcnet_if_print, DLT_ARCNET },
140 #ifdef DLT_ARCNET_LINUX
141 { arcnet_linux_if_print, DLT_ARCNET_LINUX },
142 #endif
143 { ether_if_print, DLT_EN10MB },
144 { token_if_print, DLT_IEEE802 },
145 #ifdef DLT_LANE8023
146 { lane_if_print, DLT_LANE8023 },
147 #endif
148 #ifdef DLT_CIP
149 { cip_if_print, DLT_CIP },
150 #endif
151 #ifdef DLT_ATM_CLIP
152 { cip_if_print, DLT_ATM_CLIP },
153 #endif
154 { sl_if_print, DLT_SLIP },
155 #ifdef DLT_SLIP_BSDOS
156 { sl_bsdos_if_print, DLT_SLIP_BSDOS },
157 #endif
158 { ppp_if_print, DLT_PPP },
159 #ifdef DLT_PPP_WITHDIRECTION
160 { ppp_if_print, DLT_PPP_WITHDIRECTION },
161 #endif
162 #ifdef DLT_PPP_BSDOS
163 { ppp_bsdos_if_print, DLT_PPP_BSDOS },
164 #endif
165 { fddi_if_print, DLT_FDDI },
166 { null_if_print, DLT_NULL },
167 #ifdef DLT_LOOP
168 { null_if_print, DLT_LOOP },
169 #endif
170 { raw_if_print, DLT_RAW },
171 { atm_if_print, DLT_ATM_RFC1483 },
172 #ifdef DLT_C_HDLC
173 { chdlc_if_print, DLT_C_HDLC },
174 #endif
175 #ifdef DLT_HDLC
176 { chdlc_if_print, DLT_HDLC },
177 #endif
178 #ifdef DLT_PPP_SERIAL
179 { ppp_hdlc_if_print, DLT_PPP_SERIAL },
180 #endif
181 #ifdef DLT_PPP_ETHER
182 { pppoe_if_print, DLT_PPP_ETHER },
183 #endif
184 #ifdef DLT_LINUX_SLL
185 { sll_if_print, DLT_LINUX_SLL },
186 #endif
187 #ifdef DLT_IEEE802_11
188 { ieee802_11_if_print, DLT_IEEE802_11},
189 #endif
190 #ifdef DLT_LTALK
191 { ltalk_if_print, DLT_LTALK },
192 #endif
193 #ifdef DLT_PFLOG
194 { pflog_if_print, DLT_PFLOG },
195 #endif
196 #ifdef DLT_FR
197 { fr_if_print, DLT_FR },
198 #endif
199 #ifdef DLT_FRELAY
200 { fr_if_print, DLT_FRELAY },
201 #endif
202 #ifdef DLT_SUNATM
203 { sunatm_if_print, DLT_SUNATM },
204 #endif
205 #ifdef DLT_IP_OVER_FC
206 { ipfc_if_print, DLT_IP_OVER_FC },
207 #endif
208 #ifdef DLT_PRISM_HEADER
209 { prism_if_print, DLT_PRISM_HEADER },
210 #endif
211 #ifdef DLT_IEEE802_11_RADIO
212 { ieee802_11_radio_if_print, DLT_IEEE802_11_RADIO },
213 #endif
214 #ifdef DLT_ENC
215 { enc_if_print, DLT_ENC },
216 #endif
217 #ifdef DLT_SYMANTEC_FIREWALL
218 { symantec_if_print, DLT_SYMANTEC_FIREWALL },
219 #endif
220 #ifdef DLT_APPLE_IP_OVER_IEEE1394
221 { ap1394_if_print, DLT_APPLE_IP_OVER_IEEE1394 },
222 #endif
223 #ifdef DLT_JUNIPER_ATM1
224 { juniper_atm1_print, DLT_JUNIPER_ATM1 },
225 #endif
226 #ifdef DLT_JUNIPER_ATM2
227 { juniper_atm2_print, DLT_JUNIPER_ATM2 },
228 #endif
229 #ifdef DLT_JUNIPER_MFR
230 { juniper_mfr_print, DLT_JUNIPER_MFR },
231 #endif
232 #ifdef DLT_JUNIPER_MLFR
233 { juniper_mlfr_print, DLT_JUNIPER_MLFR },
234 #endif
235 #ifdef DLT_JUNIPER_MLPPP
236 { juniper_mlppp_print, DLT_JUNIPER_MLPPP },
237 #endif
238 #ifdef DLT_JUNIPER_PPPOE
239 { juniper_pppoe_print, DLT_JUNIPER_PPPOE },
240 #endif
241 #ifdef DLT_JUNIPER_PPPOE_ATM
242 { juniper_pppoe_atm_print, DLT_JUNIPER_PPPOE_ATM },
243 #endif
244 { NULL, 0 },
245 };
246
247 static if_printer
248 lookup_printer(int type)
249 {
250 struct printer *p;
251
252 for (p = printers; p->f; ++p)
253 if (type == p->type)
254 return p->f;
255
256 return NULL;
257 /* NOTREACHED */
258 }
259
260 static pcap_t *pd;
261
262 extern int optind;
263 extern int opterr;
264 extern char *optarg;
265
266 struct print_info {
267 if_printer printer;
268 };
269
270 struct dump_info {
271 char *WFileName;
272 pcap_t *pd;
273 pcap_dumper_t *p;
274 };
275
276 static void
277 show_dlts_and_exit(pcap_t *pd)
278 {
279 int n_dlts;
280 int *dlts = 0;
281 const char *dlt_name;
282
283 n_dlts = pcap_list_datalinks(pd, &dlts);
284 if (n_dlts < 0)
285 error("%s", pcap_geterr(pd));
286 else if (n_dlts == 0 || !dlts)
287 error("No data link types.");
288
289 (void) fprintf(stderr, "Data link types (use option -y to set):\n");
290
291 while (--n_dlts >= 0) {
292 dlt_name = pcap_datalink_val_to_name(dlts[n_dlts]);
293 if (dlt_name != NULL) {
294 (void) fprintf(stderr, " %s (%s)", dlt_name,
295 pcap_datalink_val_to_description(dlts[n_dlts]));
296
297 /*
298 * OK, does tcpdump handle that type?
299 */
300 if (lookup_printer(dlts[n_dlts]) == NULL)
301 (void) fprintf(stderr, " (not supported)");
302 putchar('\n');
303 } else {
304 (void) fprintf(stderr, " DLT %d (not supported)\n",
305 dlts[n_dlts]);
306 }
307 }
308 free(dlts);
309 exit(0);
310 }
311
312 /*
313 * Set up flags that might or might not be supported depending on the
314 * version of libpcap we're using.
315 */
316 #ifdef WIN32
317 #define B_FLAG "B:"
318 #define B_FLAG_USAGE " [ -B size ]"
319 #else /* WIN32 */
320 #define B_FLAG
321 #define B_FLAG_USAGE
322 #endif /* WIN32 */
323
324 #ifdef HAVE_PCAP_FINDALLDEVS
325 #ifndef HAVE_PCAP_IF_T
326 #undef HAVE_PCAP_FINDALLDEVS
327 #endif
328 #endif
329
330 #ifdef HAVE_PCAP_FINDALLDEVS
331 #define D_FLAG "D"
332 #else
333 #define D_FLAG
334 #endif
335
336 #ifdef HAVE_PCAP_DUMP_FLUSH
337 #define U_FLAG "U"
338 #else
339 #define U_FLAG
340 #endif
341
342 #ifndef WIN32
343 /* Drop root privileges and chroot if necessary */
344 static void
345 droproot(const char *username, const char *chroot_dir)
346 {
347 struct passwd *pw = NULL;
348
349 if (chroot_dir && !username) {
350 fprintf(stderr, "tcpdump: Chroot without dropping root is insecure\n");
351 exit(1);
352 }
353
354 pw = getpwnam(username);
355 if (pw) {
356 if (chroot_dir) {
357 if (chroot(chroot_dir) != 0 || chdir ("/") != 0) {
358 fprintf(stderr, "tcpdump: Couldn't chroot/chdir to '%.64s': %s\n",
359 chroot_dir, pcap_strerror(errno));
360 exit(1);
361 }
362 }
363 if (initgroups(pw->pw_name, pw->pw_gid) != 0 ||
364 setgid(pw->pw_gid) != 0 || setuid(pw->pw_uid) != 0) {
365 fprintf(stderr, "tcpdump: Couldn't change to '%.32s' uid=%lu gid=%lu: %s\n",
366 username,
367 (unsigned long)pw->pw_uid,
368 (unsigned long)pw->pw_gid,
369 pcap_strerror(errno));
370 exit(1);
371 }
372 }
373 else {
374 fprintf(stderr, "tcpdump: Couldn't find user '%.32s'\n",
375 username);
376 exit(1);
377 }
378 }
379 #endif /* WIN32 */
380
381 static int
382 getWflagChars(int x)
383 {
384 int c = 0;
385
386 x -= 1;
387 while (x > 0) {
388 c += 1;
389 x /= 10;
390 }
391
392 return c;
393 }
394
395
396 static void
397 MakeFilename(char *buffer, char *orig_name, int cnt, int max_chars)
398 {
399 if (cnt == 0 && max_chars == 0)
400 strcpy(buffer, orig_name);
401 else
402 sprintf(buffer, "%s%0*d", orig_name, max_chars, cnt);
403 }
404
405 static int tcpdump_printf(netdissect_options *ndo _U_,
406 const char *fmt, ...)
407 {
408
409 va_list args;
410 int ret;
411
412 va_start(args, fmt);
413 ret=vfprintf(stdout, fmt, args);
414 va_end(args);
415
416 return ret;
417 }
418
419 int
420 main(int argc, char **argv)
421 {
422 register int cnt, op, i;
423 bpf_u_int32 localnet, netmask;
424 register char *cp, *infile, *cmdbuf, *device, *RFileName, *WFileName, *WFileNameAlt;
425 pcap_handler callback;
426 int type;
427 struct bpf_program fcode;
428 #ifndef WIN32
429 RETSIGTYPE (*oldhandler)(int);
430 #endif
431 struct print_info printinfo;
432 struct dump_info dumpinfo;
433 u_char *pcap_userdata;
434 char ebuf[PCAP_ERRBUF_SIZE];
435 char *username = NULL;
436 char *chroot_dir = NULL;
437 #ifdef HAVE_PCAP_FINDALLDEVS
438 pcap_if_t *devpointer;
439 int devnum;
440 #endif
441 int status;
442 #ifdef WIN32
443 u_int UserBufferSize = 1000000;
444 if(wsockinit() != 0) return 1;
445 #endif /* WIN32 */
446
447 gndo->ndo_Oflag=1;
448 gndo->ndo_Rflag=1;
449 gndo->ndo_dlt=-1;
450 gndo->ndo_default_print=ndo_default_print;
451 gndo->ndo_printf=tcpdump_printf;
452 gndo->ndo_error=ndo_error;
453 gndo->ndo_warning=ndo_warning;
454 gndo->ndo_snaplen = DEFAULT_SNAPLEN;
455
456 cnt = -1;
457 device = NULL;
458 infile = NULL;
459 RFileName = NULL;
460 WFileName = NULL;
461 if ((cp = strrchr(argv[0], '/')) != NULL)
462 program_name = cp + 1;
463 else
464 program_name = argv[0];
465
466 if (abort_on_misalignment(ebuf, sizeof(ebuf)) < 0)
467 error("%s", ebuf);
468
469 #ifdef LIBSMI
470 smiInit("tcpdump");
471 #endif
472
473 opterr = 0;
474 while (
475 (op = getopt(argc, argv, "aA" B_FLAG "c:C:d" D_FLAG "eE:fF:i:lLm:M:nNOpqr:Rs:StT:u" U_FLAG "vw:W:xXy:YZ:")) != -1)
476 switch (op) {
477
478 case 'a':
479 /* compatibility for old -a */
480 break;
481
482 case 'A':
483 ++xflag;
484 ++Xflag;
485 ++Aflag;
486 break;
487
488 #ifdef WIN32
489 case 'B':
490 UserBufferSize = atoi(optarg)*1024;
491 if (UserBufferSize < 0)
492 error("invalid packet buffer size %s", optarg);
493 break;
494 #endif /* WIN32 */
495
496 case 'c':
497 cnt = atoi(optarg);
498 if (cnt <= 0)
499 error("invalid packet count %s", optarg);
500 break;
501
502 case 'C':
503 Cflag = atoi(optarg) * 1000000;
504 if (Cflag < 0)
505 error("invalid file size %s", optarg);
506 break;
507
508 case 'd':
509 ++dflag;
510 break;
511
512 #ifdef HAVE_PCAP_FINDALLDEVS
513 case 'D':
514 if (pcap_findalldevs(&devpointer, ebuf) < 0)
515 error("%s", ebuf);
516 else {
517 for (i = 0; devpointer != 0; i++) {
518 printf("%d.%s", i+1, devpointer->name);
519 if (devpointer->description != NULL)
520 printf(" (%s)", devpointer->description);
521 printf("\n");
522 devpointer = devpointer->next;
523 }
524 }
525 return 0;
526 #endif /* HAVE_PCAP_FINDALLDEVS */
527
528 case 'L':
529 Lflag++;
530 break;
531
532 case 'e':
533 ++eflag;
534 break;
535
536 case 'E':
537 #ifndef HAVE_LIBCRYPTO
538 warning("crypto code not compiled in");
539 #endif
540 gndo->ndo_espsecret = optarg;
541 break;
542
543 case 'f':
544 ++fflag;
545 break;
546
547 case 'F':
548 infile = optarg;
549 break;
550
551 case 'i':
552 if (optarg[0] == '0' && optarg[1] == 0)
553 error("Invalid adapter index");
554
555 #ifdef HAVE_PCAP_FINDALLDEVS
556 /*
557 * If the argument is a number, treat it as
558 * an index into the list of adapters, as
559 * printed by "tcpdump -D".
560 *
561 * This should be OK on UNIX systems, as interfaces
562 * shouldn't have names that begin with digits.
563 * It can be useful on Windows, where more than
564 * one interface can have the same name.
565 */
566 if ((devnum = atoi(optarg)) != 0) {
567 if (devnum < 0)
568 error("Invalid adapter index");
569
570 if (pcap_findalldevs(&devpointer, ebuf) < 0)
571 error("%s", ebuf);
572 else {
573 for (i = 0; i < devnum-1; i++){
574 devpointer = devpointer->next;
575 if (devpointer == NULL)
576 error("Invalid adapter index");
577 }
578 }
579 device = devpointer->name;
580 break;
581 }
582 #endif /* HAVE_PCAP_FINDALLDEVS */
583 device = optarg;
584 break;
585
586 case 'l':
587 #ifdef WIN32
588 /*
589 * _IOLBF is the same as _IOFBF in Microsoft's C
590 * libraries; the only alternative they offer
591 * is _IONBF.
592 *
593 * XXX - this should really be checking for MSVC++,
594 * not WIN32, if, for example, MinGW has its own
595 * C library that is more UNIX-compatible.
596 */
597 setvbuf(stdout, NULL, _IONBF, 0);
598 #else /* WIN32 */
599 #ifdef HAVE_SETLINEBUF
600 setlinebuf(stdout);
601 #else
602 setvbuf(stdout, NULL, _IOLBF, 0);
603 #endif
604 #endif /* WIN32 */
605 break;
606
607 case 'n':
608 ++nflag;
609 break;
610
611 case 'N':
612 ++Nflag;
613 break;
614
615 case 'm':
616 #ifdef LIBSMI
617 if (smiLoadModule(optarg) == 0) {
618 error("could not load MIB module %s", optarg);
619 }
620 sflag = 1;
621 #else
622 (void)fprintf(stderr, "%s: ignoring option `-m %s' ",
623 program_name, optarg);
624 (void)fprintf(stderr, "(no libsmi support)\n");
625 #endif
626 break;
627
628 case 'M':
629 /* TCP-MD5 shared secret */
630 #ifndef HAVE_LIBCRYPTO
631 warning("crypto code not compiled in");
632 #endif
633 tcpmd5secret = optarg;
634 break;
635
636 case 'O':
637 Oflag = 0;
638 break;
639
640 case 'p':
641 ++pflag;
642 break;
643
644 case 'q':
645 ++qflag;
646 break;
647
648 case 'r':
649 RFileName = optarg;
650 break;
651
652 case 'R':
653 Rflag = 0;
654 break;
655
656 case 's': {
657 char *end;
658
659 snaplen = strtol(optarg, &end, 0);
660 if (optarg == end || *end != '\0'
661 || snaplen < 0 || snaplen > 65535)
662 error("invalid snaplen %s", optarg);
663 else if (snaplen == 0)
664 snaplen = 65535;
665 break;
666 }
667
668 case 'S':
669 ++Sflag;
670 break;
671
672 case 't':
673 ++tflag;
674 break;
675
676 case 'T':
677 if (strcasecmp(optarg, "vat") == 0)
678 packettype = PT_VAT;
679 else if (strcasecmp(optarg, "wb") == 0)
680 packettype = PT_WB;
681 else if (strcasecmp(optarg, "rpc") == 0)
682 packettype = PT_RPC;
683 else if (strcasecmp(optarg, "rtp") == 0)
684 packettype = PT_RTP;
685 else if (strcasecmp(optarg, "rtcp") == 0)
686 packettype = PT_RTCP;
687 else if (strcasecmp(optarg, "snmp") == 0)
688 packettype = PT_SNMP;
689 else if (strcasecmp(optarg, "cnfp") == 0)
690 packettype = PT_CNFP;
691 else if (strcasecmp(optarg, "tftp") == 0)
692 packettype = PT_TFTP;
693 else if (strcasecmp(optarg, "aodv") == 0)
694 packettype = PT_AODV;
695 else
696 error("unknown packet type `%s'", optarg);
697 break;
698
699 case 'u':
700 ++uflag;
701 break;
702
703 #ifdef HAVE_PCAP_DUMP_FLUSH
704 case 'U':
705 ++Uflag;
706 break;
707 #endif
708
709 case 'v':
710 ++vflag;
711 break;
712
713 case 'w':
714 WFileName = optarg;
715 break;
716
717 case 'W':
718 Wflag = atoi(optarg);
719 if (Wflag < 0)
720 error("invalid number of output files %s", optarg);
721 WflagChars = getWflagChars(Wflag);
722 break;
723
724 case 'x':
725 ++xflag;
726 break;
727
728 case 'X':
729 ++Xflag;
730 break;
731
732 case 'y':
733 gndo->ndo_dltname = optarg;
734 gndo->ndo_dlt =
735 pcap_datalink_name_to_val(gndo->ndo_dltname);
736 if (gndo->ndo_dlt < 0)
737 error("invalid data link type %s", gndo->ndo_dltname);
738 break;
739
740 #if defined(HAVE_PCAP_DEBUG) || defined(HAVE_YYDEBUG)
741 case 'Y':
742 {
743 /* Undocumented flag */
744 #ifdef HAVE_PCAP_DEBUG
745 extern int pcap_debug;
746 pcap_debug = 1;
747 #else
748 extern int yydebug;
749 yydebug = 1;
750 #endif
751 }
752 break;
753 #endif
754 case 'Z':
755 if (optarg) {
756 username = strdup(optarg);
757 }
758 else {
759 usage();
760 /* NOTREACHED */
761 }
762 break;
763
764 default:
765 usage();
766 /* NOTREACHED */
767 }
768
769 switch (tflag) {
770
771 case 0: /* Default */
772 case 4: /* Default + Date*/
773 thiszone = gmt2local(0);
774 break;
775
776 case 1: /* No time stamp */
777 case 2: /* Unix timeval style */
778 case 3: /* Microseconds since previous packet */
779 break;
780
781 default: /* Not supported */
782 error("only -t, -tt, -ttt, and -tttt are supported");
783 break;
784 }
785
786 #ifdef WITH_CHROOT
787 /* if run as root, prepare for chrooting */
788 if (getuid() == 0 || geteuid() == 0) {
789 /* future extensibility for cmd-line arguments */
790 if (!chroot_dir)
791 chroot_dir = WITH_CHROOT;
792 }
793 #endif
794
795 #ifdef WITH_USER
796 /* if run as root, prepare for dropping root privileges */
797 if (getuid() == 0 || geteuid() == 0) {
798 /* Run with '-Z root' to restore old behaviour */
799 if (!username)
800 username = WITH_USER;
801 }
802 #endif
803
804 if (RFileName != NULL) {
805 int dlt;
806 const char *dlt_name;
807
808 #ifndef WIN32
809 /*
810 * We don't need network access, so relinquish any set-UID
811 * or set-GID privileges we have (if any).
812 *
813 * We do *not* want set-UID privileges when opening a
814 * trace file, as that might let the user read other
815 * people's trace files (especially if we're set-UID
816 * root).
817 */
818 if (setgid(getgid()) != 0 || setuid(getuid()) != 0 )
819 fprintf(stderr, "Warning: setgid/setuid failed !\n");
820 #endif /* WIN32 */
821 pd = pcap_open_offline(RFileName, ebuf);
822 if (pd == NULL)
823 error("%s", ebuf);
824 dlt = pcap_datalink(pd);
825 dlt_name = pcap_datalink_val_to_name(dlt);
826 if (dlt_name == NULL) {
827 fprintf(stderr, "reading from file %s, link-type %u\n",
828 RFileName, dlt);
829 } else {
830 fprintf(stderr,
831 "reading from file %s, link-type %s (%s)\n",
832 RFileName, dlt_name,
833 pcap_datalink_val_to_description(dlt));
834 }
835 localnet = 0;
836 netmask = 0;
837 if (fflag != 0)
838 error("-f and -r options are incompatible");
839 } else {
840 if (device == NULL) {
841 device = pcap_lookupdev(ebuf);
842 if (device == NULL)
843 error("%s", ebuf);
844 }
845 #ifdef WIN32
846 if(strlen(device) == 1) //we assume that an ASCII string is always longer than 1 char
847 { //a Unicode string has a \0 as second byte (so strlen() is 1)
848 fprintf(stderr, "%s: listening on %ws\n", program_name, device);
849 }
850 else
851 {
852 fprintf(stderr, "%s: listening on %s\n", program_name, device);
853 }
854
855 fflush(stderr);
856 #endif /* WIN32 */
857 *ebuf = '\0';
858 pd = pcap_open_live(device, snaplen, !pflag, 1000, ebuf);
859 if (pd == NULL)
860 error("%s", ebuf);
861 else if (*ebuf)
862 warning("%s", ebuf);
863 /*
864 * Let user own process after socket has been opened.
865 */
866 #ifndef WIN32
867 if (setgid(getgid()) != 0 || setuid(getuid()) != 0)
868 fprintf(stderr, "Warning: setgid/setuid failed !\n");
869 #endif /* WIN32 */
870 #ifdef WIN32
871 if(UserBufferSize != 1000000)
872 if(pcap_setbuff(pd, UserBufferSize)==-1){
873 error("%s", pcap_geterr(pd));
874 }
875 #endif /* WIN32 */
876 if (Lflag)
877 show_dlts_and_exit(pd);
878 if (gndo->ndo_dlt >= 0) {
879 #ifdef HAVE_PCAP_SET_DATALINK
880 if (pcap_set_datalink(pd, gndo->ndo_dlt) < 0)
881 error("%s", pcap_geterr(pd));
882 #else
883 /*
884 * We don't actually support changing the
885 * data link type, so we only let them
886 * set it to what it already is.
887 */
888 if (gndo->ndo_dlt != pcap_datalink(pd)) {
889 error("%s is not one of the DLTs supported by this device\n",
890 gndo->ndo_dltname);
891 }
892 #endif
893 (void)fprintf(stderr, "%s: data link type %s\n",
894 program_name, gndo->ndo_dltname);
895 (void)fflush(stderr);
896 }
897 i = pcap_snapshot(pd);
898 if (snaplen < i) {
899 warning("snaplen raised from %d to %d", snaplen, i);
900 snaplen = i;
901 }
902 if (pcap_lookupnet(device, &localnet, &netmask, ebuf) < 0) {
903 localnet = 0;
904 netmask = 0;
905 warning("%s", ebuf);
906 }
907 }
908 if (infile)
909 cmdbuf = read_infile(infile);
910 else
911 cmdbuf = copy_argv(&argv[optind]);
912
913 if (pcap_compile(pd, &fcode, cmdbuf, Oflag, netmask) < 0)
914 error("%s", pcap_geterr(pd));
915 if (dflag) {
916 bpf_dump(&fcode, dflag);
917 pcap_close(pd);
918 exit(0);
919 }
920 init_addrtoname(localnet, netmask);
921
922 #ifndef WIN32
923 (void)setsignal(SIGPIPE, cleanup);
924 #endif /* WIN32 */
925 (void)setsignal(SIGTERM, cleanup);
926 (void)setsignal(SIGINT, cleanup);
927 /* Cooperate with nohup(1) */
928 #ifndef WIN32
929 if ((oldhandler = setsignal(SIGHUP, cleanup)) != SIG_DFL)
930 (void)setsignal(SIGHUP, oldhandler);
931 #endif /* WIN32 */
932
933 if (pcap_setfilter(pd, &fcode) < 0)
934 error("%s", pcap_geterr(pd));
935 if (WFileName) {
936 pcap_dumper_t *p;
937
938 WFileNameAlt = (char *)malloc(strlen(WFileName) + MAX_CFLAG_CHARS + 1);
939 if (WFileNameAlt == NULL)
940 error("malloc of WFileNameAlt");
941 MakeFilename(WFileNameAlt, WFileName, 0, WflagChars);
942 p = pcap_dump_open(pd, WFileNameAlt);
943 if (p == NULL)
944 error("%s", pcap_geterr(pd));
945 if (Cflag != 0) {
946 callback = dump_packet_and_trunc;
947 dumpinfo.WFileName = WFileName;
948 dumpinfo.pd = pd;
949 dumpinfo.p = p;
950 pcap_userdata = (u_char *)&dumpinfo;
951 } else {
952 callback = dump_packet;
953 pcap_userdata = (u_char *)p;
954 }
955 } else {
956 type = pcap_datalink(pd);
957 printinfo.printer = lookup_printer(type);
958 if (printinfo.printer == NULL) {
959 gndo->ndo_dltname = pcap_datalink_val_to_name(type);
960 if (gndo->ndo_dltname != NULL)
961 error("unsupported data link type %s",
962 gndo->ndo_dltname);
963 else
964 error("unsupported data link type %d", type);
965 }
966 callback = print_packet;
967 pcap_userdata = (u_char *)&printinfo;
968 }
969 #ifndef WIN32
970 /*
971 * We cannot do this earlier, because we want to be able to open
972 * the file (if done) for writing before giving up permissions.
973 */
974 if (getuid() == 0 || geteuid() == 0) {
975 if (username || chroot_dir)
976 droproot(username, chroot_dir);
977 }
978 #endif /* WIN32 */
979 #ifdef SIGINFO
980 (void)setsignal(SIGINFO, requestinfo);
981 #endif
982
983 if (vflag > 0 && WFileName) {
984 /*
985 * When capturing to a file, "-v" means tcpdump should,
986 * every 10 secodns, "v"erbosely report the number of
987 * packets captured.
988 */
989 #ifdef USE_WIN32_MM_TIMER
990 /* call verbose_stats_dump() each 1000 +/-100msec */
991 timer_id = timeSetEvent(1000, 100, verbose_stats_dump, 0, TIME_PERIODIC);
992 setvbuf(stderr, NULL, _IONBF, 0);
993 #elif defined(HAVE_ALARM)
994 (void)setsignal(SIGALRM, verbose_stats_dump);
995 alarm(1);
996 #endif
997 }
998
999 #ifndef WIN32
1000 if (RFileName == NULL) {
1001 int dlt;
1002 const char *dlt_name;
1003
1004 if (!vflag && !WFileName) {
1005 (void)fprintf(stderr,
1006 "%s: verbose output suppressed, use -v or -vv for full protocol decode\n",
1007 program_name);
1008 } else
1009 (void)fprintf(stderr, "%s: ", program_name);
1010 dlt = pcap_datalink(pd);
1011 dlt_name = pcap_datalink_val_to_name(dlt);
1012 if (dlt_name == NULL) {
1013 (void)fprintf(stderr, "listening on %s, link-type %u, capture size %u bytes\n",
1014 device, dlt, snaplen);
1015 } else {
1016 (void)fprintf(stderr, "listening on %s, link-type %s (%s), capture size %u bytes\n",
1017 device, dlt_name,
1018 pcap_datalink_val_to_description(dlt), snaplen);
1019 }
1020 (void)fflush(stderr);
1021 }
1022 #endif /* WIN32 */
1023 status = pcap_loop(pd, cnt, callback, pcap_userdata);
1024 if (WFileName == NULL) {
1025 /*
1026 * We're printing packets. Flush the printed output,
1027 * so it doesn't get intermingled with error output.
1028 */
1029 if (status == -2) {
1030 /*
1031 * We got interrupted, so perhaps we didn't
1032 * manage to finish a line we were printing.
1033 * Print an extra newline, just in case.
1034 */
1035 putchar('\n');
1036 }
1037 (void)fflush(stdout);
1038 }
1039 if (status == -1) {
1040 /*
1041 * Error. Report it.
1042 */
1043 (void)fprintf(stderr, "%s: pcap_loop: %s\n",
1044 program_name, pcap_geterr(pd));
1045 }
1046 if (RFileName == NULL) {
1047 /*
1048 * We're doing a live capture. Report the capture
1049 * statistics.
1050 */
1051 info(1);
1052 }
1053 pcap_close(pd);
1054 exit(status == -1 ? 1 : 0);
1055 }
1056
1057 /* make a clean exit on interrupts */
1058 static RETSIGTYPE
1059 cleanup(int signo _U_)
1060 {
1061 #ifdef USE_WIN32_MM_TIMER
1062 if (timer_id)
1063 timeKillEvent(timer_id);
1064 timer_id = 0;
1065 #elif defined(HAVE_ALARM)
1066 alarm(0);
1067 #endif
1068
1069 #ifdef HAVE_PCAP_BREAKLOOP
1070 /*
1071 * We have "pcap_breakloop()"; use it, so that we do as little
1072 * as possible in the signal handler (it's probably not safe
1073 * to do anything with standard I/O streams in a signal handler -
1074 * the ANSI C standard doesn't say it is).
1075 */
1076 pcap_breakloop(pd);
1077 #else
1078 /*
1079 * We don't have "pcap_breakloop()"; this isn't safe, but
1080 * it's the best we can do. Print the summary if we're
1081 * not reading from a savefile - i.e., if we're doing a
1082 * live capture - and exit.
1083 */
1084 if (pd != NULL && pcap_file(pd) == NULL) {
1085 /*
1086 * We got interrupted, so perhaps we didn't
1087 * manage to finish a line we were printing.
1088 * Print an extra newline, just in case.
1089 */
1090 putchar('\n');
1091 (void)fflush(stdout);
1092 info(1);
1093 }
1094 exit(0);
1095 #endif
1096 }
1097
1098 static void
1099 info(register int verbose)
1100 {
1101 struct pcap_stat stat;
1102
1103 if (pcap_stats(pd, &stat) < 0) {
1104 (void)fprintf(stderr, "pcap_stats: %s\n", pcap_geterr(pd));
1105 return;
1106 }
1107
1108 if (!verbose)
1109 fprintf(stderr, "%s: ", program_name);
1110
1111 (void)fprintf(stderr, "%u packets captured", packets_captured);
1112 if (!verbose)
1113 fputs(", ", stderr);
1114 else
1115 putc('\n', stderr);
1116 (void)fprintf(stderr, "%d packets received by filter", stat.ps_recv);
1117 if (!verbose)
1118 fputs(", ", stderr);
1119 else
1120 putc('\n', stderr);
1121 (void)fprintf(stderr, "%d packets dropped by kernel\n", stat.ps_drop);
1122 infoprint = 0;
1123 }
1124
1125 static void
1126 dump_packet_and_trunc(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
1127 {
1128 struct dump_info *dump_info;
1129 char *name;
1130
1131 ++packets_captured;
1132
1133 ++infodelay;
1134
1135 dump_info = (struct dump_info *)user;
1136
1137 /*
1138 * XXX - this won't prevent capture files from getting
1139 * larger than Cflag - the last packet written to the
1140 * file could put it over Cflag.
1141 */
1142 if (ftell((FILE *)dump_info->p) > Cflag) {
1143 /*
1144 * Close the current file and open a new one.
1145 */
1146 pcap_dump_close(dump_info->p);
1147 Cflag_count++;
1148 if (Wflag > 0) {
1149 if (Cflag_count >= Wflag)
1150 Cflag_count = 0;
1151 } else {
1152 if (Cflag_count >= MAX_CFLAG)
1153 error("too many output files");
1154 }
1155 name = (char *)malloc(strlen(dump_info->WFileName) + MAX_CFLAG_CHARS + 1);
1156 if (name == NULL)
1157 error("dump_packet_and_trunc: malloc");
1158 MakeFilename(name, dump_info->WFileName, Cflag_count, WflagChars);
1159 dump_info->p = pcap_dump_open(dump_info->pd, name);
1160 free(name);
1161 if (dump_info->p == NULL)
1162 error("%s", pcap_geterr(pd));
1163 }
1164
1165 pcap_dump((u_char *)dump_info->p, h, sp);
1166 #ifdef HAVE_PCAP_DUMP_FLUSH
1167 if (Uflag)
1168 pcap_dump_flush(dump_info->p);
1169 #endif
1170
1171 --infodelay;
1172 if (infoprint)
1173 info(0);
1174 }
1175
1176 static void
1177 dump_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
1178 {
1179 ++packets_captured;
1180
1181 ++infodelay;
1182
1183 pcap_dump(user, h, sp);
1184 #ifdef HAVE_PCAP_DUMP_FLUSH
1185 if (Uflag)
1186 pcap_dump_flush((pcap_dumper_t *)user);
1187 #endif
1188
1189 --infodelay;
1190 if (infoprint)
1191 info(0);
1192 }
1193
1194 static void
1195 print_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
1196 {
1197 struct print_info *print_info;
1198 u_int hdrlen;
1199
1200 ++packets_captured;
1201
1202 ++infodelay;
1203 ts_print(&h->ts);
1204
1205 print_info = (struct print_info *)user;
1206
1207 /*
1208 * Some printers want to check that they're not walking off the
1209 * end of the packet.
1210 * Rather than pass it all the way down, we set this global.
1211 */
1212 snapend = sp + h->caplen;
1213
1214 hdrlen = (*print_info->printer)(h, sp);
1215 if (xflag) {
1216 /*
1217 * Print the raw packet data.
1218 */
1219 if (xflag > 1) {
1220 /*
1221 * Include the link-layer header.
1222 */
1223 hex_print("\n\t", sp, h->caplen);
1224 } else {
1225 /*
1226 * Don't include the link-layer header - and if
1227 * we have nothing past the link-layer header,
1228 * print nothing.
1229 */
1230 if (h->caplen > hdrlen)
1231 hex_print("\n\t", sp + hdrlen,
1232 h->caplen - hdrlen);
1233 }
1234 } else if (Xflag) {
1235 /*
1236 * Print the raw packet data.
1237 */
1238 if (Xflag > 1) {
1239 /*
1240 * Include the link-layer header.
1241 */
1242 ascii_print("\n\t", sp, h->caplen);
1243 } else {
1244 /*
1245 * Don't include the link-layer header - and if
1246 * we have nothing past the link-layer header,
1247 * print nothing.
1248 */
1249 if (h->caplen > hdrlen)
1250 ascii_print("\n\t", sp + hdrlen,
1251 h->caplen - hdrlen);
1252 }
1253 }
1254
1255 putchar('\n');
1256
1257 --infodelay;
1258 if (infoprint)
1259 info(0);
1260 }
1261
1262 #ifdef WIN32
1263 /*
1264 * XXX - there should really be libpcap calls to get the version
1265 * number as a string (the string would be generated from #defines
1266 * at run time, so that it's not generated from string constants
1267 * in the library, as, on many UNIX systems, those constants would
1268 * be statically linked into the application executable image, and
1269 * would thus reflect the version of libpcap on the system on
1270 * which the application was *linked*, not the system on which it's
1271 * *running*.
1272 *
1273 * That routine should be documented, unlike the "version[]"
1274 * string, so that UNIX vendors providing their own libpcaps
1275 * don't omit it (as a couple of vendors have...).
1276 *
1277 * Packet.dll should perhaps also export a routine to return the
1278 * version number of the Packet.dll code, to supply the
1279 * "Wpcap_version" information on Windows.
1280 */
1281 char WDversion[]="current-cvs.tcpdump.org";
1282 #if !defined(HAVE_GENERATED_VERSION)
1283 char version[]="current-cvs.tcpdump.org";
1284 #endif
1285 char pcap_version[]="current-cvs.tcpdump.org";
1286 char Wpcap_version[]="3.1";
1287 #endif
1288
1289 /*
1290 * By default, print the specified data out in hex.
1291 */
1292 static void
1293 ndo_default_print(netdissect_options *ndo _U_, const u_char *bp, u_int length)
1294 {
1295 ascii_print("\n\t", bp, length); /* pass on lf and identation string */
1296 }
1297
1298 void
1299 default_print(const u_char *bp, u_int length)
1300 {
1301 ndo_default_print(gndo, bp, length);
1302 }
1303
1304 #ifdef SIGINFO
1305 RETSIGTYPE requestinfo(int signo _U_)
1306 {
1307 if (infodelay)
1308 ++infoprint;
1309 else
1310 info(0);
1311 }
1312 #endif
1313
1314 /*
1315 * Called once each second in verbose mode while dumping to file
1316 */
1317 #ifdef USE_WIN32_MM_TIMER
1318 void CALLBACK verbose_stats_dump (UINT timer_id _U_, UINT msg _U_, DWORD_PTR arg _U_,
1319 DWORD_PTR dw1 _U_, DWORD_PTR dw2 _U_)
1320 {
1321 struct pcap_stat stat;
1322
1323 if (infodelay == 0 && pcap_stats(pd, &stat) >= 0)
1324 fprintf(stderr, "Got %u\r", packets_captured);
1325 }
1326 #elif defined(HAVE_ALARM)
1327 static void verbose_stats_dump(int sig _U_)
1328 {
1329 struct pcap_stat stat;
1330
1331 if (infodelay == 0 && pcap_stats(pd, &stat) >= 0)
1332 fprintf(stderr, "Got %u\r", packets_captured);
1333 alarm(1);
1334 }
1335 #endif
1336
1337 static void
1338 usage(void)
1339 {
1340 extern char version[];
1341 #ifndef HAVE_PCAP_LIB_VERSION
1342 #if defined(WIN32) || defined(HAVE_PCAP_VERSION)
1343 extern char pcap_version[];
1344 #else /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
1345 static char pcap_version[] = "unknown";
1346 #endif /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
1347 #endif /* HAVE_PCAP_LIB_VERSION */
1348
1349 #ifdef HAVE_PCAP_LIB_VERSION
1350 #ifdef WIN32
1351 (void)fprintf(stderr, "%s version %s, based on tcpdump version %s\n", program_name, WDversion, version);
1352 #else /* WIN32 */
1353 (void)fprintf(stderr, "%s version %s\n", program_name, version);
1354 #endif /* WIN32 */
1355 (void)fprintf(stderr, "%s\n",pcap_lib_version());
1356 #else /* HAVE_PCAP_LIB_VERSION */
1357 #ifdef WIN32
1358 (void)fprintf(stderr, "%s version %s, based on tcpdump version %s\n", program_name, WDversion, version);
1359 (void)fprintf(stderr, "WinPcap version %s, based on libpcap version %s\n",Wpcap_version, pcap_version);
1360 #else /* WIN32 */
1361 (void)fprintf(stderr, "%s version %s\n", program_name, version);
1362 (void)fprintf(stderr, "libpcap version %s\n", pcap_version);
1363 #endif /* WIN32 */
1364 #endif /* HAVE_PCAP_LIB_VERSION */
1365 (void)fprintf(stderr,
1366 "Usage: %s [-aAd" D_FLAG "eflLnNOpqRStu" U_FLAG "vxX]" B_FLAG_USAGE " [-c count] [ -C file_size ]\n", program_name);
1367 (void)fprintf(stderr,
1368 "\t\t[ -E algo:secret ] [ -F file ] [ -i interface ] [ -M secret ]\n");
1369 (void)fprintf(stderr,
1370 "\t\t[ -r file ] [ -s snaplen ] [ -T type ] [ -w file ]\n");
1371 (void)fprintf(stderr,
1372 "\t\t[ -W filecount ] [ -y datalinktype ] [ -Z user ]\n");
1373 (void)fprintf(stderr,
1374 "\t\t[ expression ]\n");
1375 exit(1);
1376 }
1377
1378
1379
1380 /* VARARGS */
1381 static void
1382 ndo_error(netdissect_options *ndo _U_, const char *fmt, ...)
1383 {
1384 va_list ap;
1385
1386 (void)fprintf(stderr, "%s: ", program_name);
1387 va_start(ap, fmt);
1388 (void)vfprintf(stderr, fmt, ap);
1389 va_end(ap);
1390 if (*fmt) {
1391 fmt += strlen(fmt);
1392 if (fmt[-1] != '\n')
1393 (void)fputc('\n', stderr);
1394 }
1395 exit(1);
1396 /* NOTREACHED */
1397 }
1398
1399 /* VARARGS */
1400 static void
1401 ndo_warning(netdissect_options *ndo _U_, const char *fmt, ...)
1402 {
1403 va_list ap;
1404
1405 (void)fprintf(stderr, "%s: WARNING: ", program_name);
1406 va_start(ap, fmt);
1407 (void)vfprintf(stderr, fmt, ap);
1408 va_end(ap);
1409 if (*fmt) {
1410 fmt += strlen(fmt);
1411 if (fmt[-1] != '\n')
1412 (void)fputc('\n', stderr);
1413 }
1414 }
1415