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.
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
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.
21 * Support for splitting captures into multiple files with a maximum
25 * Seth Webster <swebster@sst.ll.mit.edu>
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.283 2008-09-25 21:45:50 guy Exp $ (LBL)";
37 * tcpdump - monitor tcp/ip traffic on an ethernet.
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.
48 #include <tcpdump-stdinc.h>
53 extern int strcasecmp (const char *__s1
, const char *__s2
);
71 #include <sys/resource.h>
78 #include "netdissect.h"
79 #include "interface.h"
80 #include "addrtoname.h"
82 #include "setsignal.h"
83 #include "gmt2local.h"
84 #include "pcap-missing.h"
90 netdissect_options Gndo
;
91 netdissect_options
*gndo
= &Gndo
;
93 int dflag
; /* print filter code */
94 int Lflag
; /* list available data link types and exit */
95 char *zflag
= NULL
; /* compress each savefile using a specified command (like gzip or bzip2) */
102 int32_t thiszone
; /* seconds offset from gmt to local time */
105 static RETSIGTYPE
cleanup(int);
106 static RETSIGTYPE
child_cleanup(int);
107 static void usage(void) __attribute__((noreturn
));
108 static void show_dlts_and_exit(pcap_t
*pd
) __attribute__((noreturn
));
110 static void print_packet(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
111 static void ndo_default_print(netdissect_options
*, const u_char
*, u_int
);
112 static void dump_packet_and_trunc(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
113 static void dump_packet(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
114 static void droproot(const char *, const char *);
115 static void ndo_error(netdissect_options
*ndo
, const char *fmt
, ...);
116 static void ndo_warning(netdissect_options
*ndo
, const char *fmt
, ...);
119 RETSIGTYPE
requestinfo(int);
122 #if defined(USE_WIN32_MM_TIMER)
123 #include <MMsystem.h>
124 static UINT timer_id
;
125 static void CALLBACK
verbose_stats_dump(UINT
, UINT
, DWORD_PTR
, DWORD_PTR
, DWORD_PTR
);
126 #elif defined(HAVE_ALARM)
127 static void verbose_stats_dump(int sig
);
130 static void info(int);
131 static u_int packets_captured
;
133 typedef u_int (*if_printer
)(const struct pcap_pkthdr
*, const u_char
*);
140 static struct printer printers
[] = {
141 { arcnet_if_print
, DLT_ARCNET
},
142 #ifdef DLT_ARCNET_LINUX
143 { arcnet_linux_if_print
, DLT_ARCNET_LINUX
},
145 { ether_if_print
, DLT_EN10MB
},
146 { token_if_print
, DLT_IEEE802
},
148 { lane_if_print
, DLT_LANE8023
},
151 { cip_if_print
, DLT_CIP
},
154 { cip_if_print
, DLT_ATM_CLIP
},
156 { sl_if_print
, DLT_SLIP
},
157 #ifdef DLT_SLIP_BSDOS
158 { sl_bsdos_if_print
, DLT_SLIP_BSDOS
},
160 { ppp_if_print
, DLT_PPP
},
161 #ifdef DLT_PPP_WITHDIRECTION
162 { ppp_if_print
, DLT_PPP_WITHDIRECTION
},
165 { ppp_bsdos_if_print
, DLT_PPP_BSDOS
},
167 { fddi_if_print
, DLT_FDDI
},
168 { null_if_print
, DLT_NULL
},
170 { null_if_print
, DLT_LOOP
},
172 { raw_if_print
, DLT_RAW
},
173 { atm_if_print
, DLT_ATM_RFC1483
},
175 { chdlc_if_print
, DLT_C_HDLC
},
178 { chdlc_if_print
, DLT_HDLC
},
180 #ifdef DLT_PPP_SERIAL
181 { ppp_hdlc_if_print
, DLT_PPP_SERIAL
},
184 { pppoe_if_print
, DLT_PPP_ETHER
},
187 { sll_if_print
, DLT_LINUX_SLL
},
189 #ifdef DLT_IEEE802_11
190 { ieee802_11_if_print
, DLT_IEEE802_11
},
193 { ltalk_if_print
, DLT_LTALK
},
195 #if defined(DLT_PFLOG) && defined(HAVE_NET_PFVAR_H)
196 { pflog_if_print
, DLT_PFLOG
},
199 { fr_if_print
, DLT_FR
},
202 { fr_if_print
, DLT_FRELAY
},
205 { sunatm_if_print
, DLT_SUNATM
},
207 #ifdef DLT_IP_OVER_FC
208 { ipfc_if_print
, DLT_IP_OVER_FC
},
210 #ifdef DLT_PRISM_HEADER
211 { prism_if_print
, DLT_PRISM_HEADER
},
213 #ifdef DLT_IEEE802_11_RADIO
214 { ieee802_11_radio_if_print
, DLT_IEEE802_11_RADIO
},
217 { enc_if_print
, DLT_ENC
},
219 #ifdef DLT_SYMANTEC_FIREWALL
220 { symantec_if_print
, DLT_SYMANTEC_FIREWALL
},
222 #ifdef DLT_APPLE_IP_OVER_IEEE1394
223 { ap1394_if_print
, DLT_APPLE_IP_OVER_IEEE1394
},
225 #ifdef DLT_IEEE802_11_RADIO_AVS
226 { ieee802_11_radio_avs_if_print
, DLT_IEEE802_11_RADIO_AVS
},
228 #ifdef DLT_JUNIPER_ATM1
229 { juniper_atm1_print
, DLT_JUNIPER_ATM1
},
231 #ifdef DLT_JUNIPER_ATM2
232 { juniper_atm2_print
, DLT_JUNIPER_ATM2
},
234 #ifdef DLT_JUNIPER_MFR
235 { juniper_mfr_print
, DLT_JUNIPER_MFR
},
237 #ifdef DLT_JUNIPER_MLFR
238 { juniper_mlfr_print
, DLT_JUNIPER_MLFR
},
240 #ifdef DLT_JUNIPER_MLPPP
241 { juniper_mlppp_print
, DLT_JUNIPER_MLPPP
},
243 #ifdef DLT_JUNIPER_PPPOE
244 { juniper_pppoe_print
, DLT_JUNIPER_PPPOE
},
246 #ifdef DLT_JUNIPER_PPPOE_ATM
247 { juniper_pppoe_atm_print
, DLT_JUNIPER_PPPOE_ATM
},
249 #ifdef DLT_JUNIPER_GGSN
250 { juniper_ggsn_print
, DLT_JUNIPER_GGSN
},
252 #ifdef DLT_JUNIPER_ES
253 { juniper_es_print
, DLT_JUNIPER_ES
},
255 #ifdef DLT_JUNIPER_MONITOR
256 { juniper_monitor_print
, DLT_JUNIPER_MONITOR
},
258 #ifdef DLT_JUNIPER_SERVICES
259 { juniper_services_print
, DLT_JUNIPER_SERVICES
},
261 #ifdef DLT_JUNIPER_ETHER
262 { juniper_ether_print
, DLT_JUNIPER_ETHER
},
264 #ifdef DLT_JUNIPER_PPP
265 { juniper_ppp_print
, DLT_JUNIPER_PPP
},
267 #ifdef DLT_JUNIPER_FRELAY
268 { juniper_frelay_print
, DLT_JUNIPER_FRELAY
},
270 #ifdef DLT_JUNIPER_CHDLC
271 { juniper_chdlc_print
, DLT_JUNIPER_CHDLC
},
274 { mfr_if_print
, DLT_MFR
},
276 #if defined(DLT_BLUETOOTH_HCI_H4_WITH_PHDR) && defined(HAVE_PCAP_BLUETOOTH_H)
277 { bt_if_print
, DLT_BLUETOOTH_HCI_H4_WITH_PHDR
},
283 lookup_printer(int type
)
287 for (p
= printers
; p
->f
; ++p
)
307 char *CurrentFileName
;
313 show_dlts_and_exit(pcap_t
*pd
)
317 const char *dlt_name
;
319 n_dlts
= pcap_list_datalinks(pd
, &dlts
);
321 error("%s", pcap_geterr(pd
));
322 else if (n_dlts
== 0 || !dlts
)
323 error("No data link types.");
325 (void) fprintf(stderr
, "Data link types (use option -y to set):\n");
327 while (--n_dlts
>= 0) {
328 dlt_name
= pcap_datalink_val_to_name(dlts
[n_dlts
]);
329 if (dlt_name
!= NULL
) {
330 (void) fprintf(stderr
, " %s (%s)", dlt_name
,
331 pcap_datalink_val_to_description(dlts
[n_dlts
]));
334 * OK, does tcpdump handle that type?
336 if (lookup_printer(dlts
[n_dlts
]) == NULL
)
337 (void) fprintf(stderr
, " (printing not supported)");
340 (void) fprintf(stderr
, " DLT %d (printing not supported)\n",
349 * Set up flags that might or might not be supported depending on the
350 * version of libpcap we're using.
352 #if defined(HAVE_PCAP_CREATE) || defined(WIN32)
354 #define B_FLAG_USAGE " [ -B size ]"
355 #else /* defined(HAVE_PCAP_CREATE) || defined(WIN32) */
358 #endif /* defined(HAVE_PCAP_CREATE) || defined(WIN32) */
360 #ifdef HAVE_PCAP_CREATE
362 #else /* HAVE_PCAP_CREATE */
364 #endif /* HAVE_PCAP_CREATE */
366 #ifdef HAVE_PCAP_FINDALLDEVS
367 #ifndef HAVE_PCAP_IF_T
368 #undef HAVE_PCAP_FINDALLDEVS
372 #ifdef HAVE_PCAP_FINDALLDEVS
378 #ifdef HAVE_PCAP_DUMP_FLUSH
385 /* Drop root privileges and chroot if necessary */
387 droproot(const char *username
, const char *chroot_dir
)
389 struct passwd
*pw
= NULL
;
391 if (chroot_dir
&& !username
) {
392 fprintf(stderr
, "tcpdump: Chroot without dropping root is insecure\n");
396 pw
= getpwnam(username
);
399 if (chroot(chroot_dir
) != 0 || chdir ("/") != 0) {
400 fprintf(stderr
, "tcpdump: Couldn't chroot/chdir to '%.64s': %s\n",
401 chroot_dir
, pcap_strerror(errno
));
405 if (initgroups(pw
->pw_name
, pw
->pw_gid
) != 0 ||
406 setgid(pw
->pw_gid
) != 0 || setuid(pw
->pw_uid
) != 0) {
407 fprintf(stderr
, "tcpdump: Couldn't change to '%.32s' uid=%lu gid=%lu: %s\n",
409 (unsigned long)pw
->pw_uid
,
410 (unsigned long)pw
->pw_gid
,
411 pcap_strerror(errno
));
416 fprintf(stderr
, "tcpdump: Couldn't find user '%.32s'\n",
439 MakeFilename(char *buffer
, char *orig_name
, int cnt
, int max_chars
)
441 char *filename
= malloc(NAME_MAX
+ 1);
443 /* Process with strftime if Gflag is set. */
447 /* Convert Gflag_time to a usable format */
448 if ((local_tm
= localtime(&Gflag_time
)) == NULL
) {
449 error("MakeTimedFilename: localtime");
452 /* There's no good way to detect an error in strftime since a return
453 * value of 0 isn't necessarily failure.
455 strftime(filename
, NAME_MAX
, orig_name
, local_tm
);
457 strncpy(filename
, orig_name
, NAME_MAX
);
460 if (cnt
== 0 && max_chars
== 0)
461 strncpy(buffer
, filename
, NAME_MAX
+ 1);
463 if (snprintf(buffer
, NAME_MAX
+ 1, "%s%0*d", filename
, max_chars
, cnt
) > NAME_MAX
)
464 /* Report an error if the filename is too large */
465 error("too many output files or filename is too long (> %d)", NAME_MAX
);
469 static int tcpdump_printf(netdissect_options
*ndo _U_
,
470 const char *fmt
, ...)
477 ret
=vfprintf(stdout
, fmt
, args
);
484 main(int argc
, char **argv
)
486 register int cnt
, op
, i
;
487 bpf_u_int32 localnet
, netmask
;
488 register char *cp
, *infile
, *cmdbuf
, *device
, *RFileName
, *WFileName
;
489 pcap_handler callback
;
491 struct bpf_program fcode
;
493 RETSIGTYPE (*oldhandler
)(int);
495 struct print_info printinfo
;
496 struct dump_info dumpinfo
;
497 u_char
*pcap_userdata
;
498 char ebuf
[PCAP_ERRBUF_SIZE
];
499 char *username
= NULL
;
500 char *chroot_dir
= NULL
;
501 #ifdef HAVE_PCAP_FINDALLDEVS
502 pcap_if_t
*devpointer
;
507 if(wsockinit() != 0) return 1;
513 gndo
->ndo_default_print
=ndo_default_print
;
514 gndo
->ndo_printf
=tcpdump_printf
;
515 gndo
->ndo_error
=ndo_error
;
516 gndo
->ndo_warning
=ndo_warning
;
517 gndo
->ndo_snaplen
= DEFAULT_SNAPLEN
;
524 if ((cp
= strrchr(argv
[0], '/')) != NULL
)
525 program_name
= cp
+ 1;
527 program_name
= argv
[0];
529 if (abort_on_misalignment(ebuf
, sizeof(ebuf
)) < 0)
538 (op
= getopt(argc
, argv
, "aA" B_FLAG
"c:C:d" D_FLAG
"eE:fF:G:i:" I_FLAG
"KlLm:M:nNOpqr:Rs:StT:u" U_FLAG
"vw:W:xXy:Yz:Z:")) != -1)
542 /* compatibility for old -a */
552 #if defined(HAVE_PCAP_CREATE) || defined(WIN32)
554 Bflag
= atoi(optarg
)*1024;
556 error("invalid packet buffer size %s", optarg
);
558 #endif /* defined(HAVE_PCAP_CREATE) || defined(WIN32) */
563 error("invalid packet count %s", optarg
);
567 Cflag
= atoi(optarg
) * 1000000;
569 error("invalid file size %s", optarg
);
576 #ifdef HAVE_PCAP_FINDALLDEVS
578 if (pcap_findalldevs(&devpointer
, ebuf
) < 0)
581 for (i
= 0; devpointer
!= 0; i
++) {
582 printf("%d.%s", i
+1, devpointer
->name
);
583 if (devpointer
->description
!= NULL
)
584 printf(" (%s)", devpointer
->description
);
586 devpointer
= devpointer
->next
;
590 #endif /* HAVE_PCAP_FINDALLDEVS */
601 #ifndef HAVE_LIBCRYPTO
602 warning("crypto code not compiled in");
604 gndo
->ndo_espsecret
= optarg
;
616 Gflag
= atoi(optarg
);
618 error("invalid number of seconds %s", optarg
);
620 /* We will create one file initially. */
623 /* Grab the current time for rotation use. */
624 if ((Gflag_time
= time(NULL
)) == (time_t)-1) {
625 error("main: can't get current time: %s",
626 pcap_strerror(errno
));
631 if (optarg
[0] == '0' && optarg
[1] == 0)
632 error("Invalid adapter index");
634 #ifdef HAVE_PCAP_FINDALLDEVS
636 * If the argument is a number, treat it as
637 * an index into the list of adapters, as
638 * printed by "tcpdump -D".
640 * This should be OK on UNIX systems, as interfaces
641 * shouldn't have names that begin with digits.
642 * It can be useful on Windows, where more than
643 * one interface can have the same name.
645 if ((devnum
= atoi(optarg
)) != 0) {
647 error("Invalid adapter index");
649 if (pcap_findalldevs(&devpointer
, ebuf
) < 0)
652 for (i
= 0; i
< devnum
-1; i
++){
653 devpointer
= devpointer
->next
;
654 if (devpointer
== NULL
)
655 error("Invalid adapter index");
658 device
= devpointer
->name
;
661 #endif /* HAVE_PCAP_FINDALLDEVS */
665 #ifdef HAVE_PCAP_CREATE
669 #endif /* HAVE_PCAP_CREATE */
674 * _IOLBF is the same as _IOFBF in Microsoft's C
675 * libraries; the only alternative they offer
678 * XXX - this should really be checking for MSVC++,
679 * not WIN32, if, for example, MinGW has its own
680 * C library that is more UNIX-compatible.
682 setvbuf(stdout
, NULL
, _IONBF
, 0);
684 #ifdef HAVE_SETLINEBUF
687 setvbuf(stdout
, NULL
, _IOLBF
, 0);
698 if (smiLoadModule(optarg
) == 0) {
699 error("could not load MIB module %s", optarg
);
703 (void)fprintf(stderr
, "%s: ignoring option `-m %s' ",
704 program_name
, optarg
);
705 (void)fprintf(stderr
, "(no libsmi support)\n");
710 /* TCP-MD5 shared secret */
711 #ifndef HAVE_LIBCRYPTO
712 warning("crypto code not compiled in");
735 ++suppress_default_print
;
749 snaplen
= strtol(optarg
, &end
, 0);
750 if (optarg
== end
|| *end
!= '\0'
751 || snaplen
< 0 || snaplen
> MAXIMUM_SNAPLEN
)
752 error("invalid snaplen %s", optarg
);
753 else if (snaplen
== 0)
754 snaplen
= MAXIMUM_SNAPLEN
;
767 if (strcasecmp(optarg
, "vat") == 0)
769 else if (strcasecmp(optarg
, "wb") == 0)
771 else if (strcasecmp(optarg
, "rpc") == 0)
773 else if (strcasecmp(optarg
, "rtp") == 0)
775 else if (strcasecmp(optarg
, "rtcp") == 0)
776 packettype
= PT_RTCP
;
777 else if (strcasecmp(optarg
, "snmp") == 0)
778 packettype
= PT_SNMP
;
779 else if (strcasecmp(optarg
, "cnfp") == 0)
780 packettype
= PT_CNFP
;
781 else if (strcasecmp(optarg
, "tftp") == 0)
782 packettype
= PT_TFTP
;
783 else if (strcasecmp(optarg
, "aodv") == 0)
784 packettype
= PT_AODV
;
786 error("unknown packet type `%s'", optarg
);
793 #ifdef HAVE_PCAP_DUMP_FLUSH
808 Wflag
= atoi(optarg
);
810 error("invalid number of output files %s", optarg
);
811 WflagChars
= getWflagChars(Wflag
);
816 ++suppress_default_print
;
821 ++suppress_default_print
;
825 gndo
->ndo_dltname
= optarg
;
827 pcap_datalink_name_to_val(gndo
->ndo_dltname
);
828 if (gndo
->ndo_dlt
< 0)
829 error("invalid data link type %s", gndo
->ndo_dltname
);
832 #if defined(HAVE_PCAP_DEBUG) || defined(HAVE_YYDEBUG)
835 /* Undocumented flag */
836 #ifdef HAVE_PCAP_DEBUG
837 extern int pcap_debug
;
848 zflag
= strdup(optarg
);
857 username
= strdup(optarg
);
872 case 0: /* Default */
873 case 4: /* Default + Date*/
874 thiszone
= gmt2local(0);
877 case 1: /* No time stamp */
878 case 2: /* Unix timeval style */
879 case 3: /* Microseconds since previous packet */
880 case 5: /* Microseconds since first packet */
883 default: /* Not supported */
884 error("only -t, -tt, -ttt, -tttt and -ttttt are supported");
889 /* if run as root, prepare for chrooting */
890 if (getuid() == 0 || geteuid() == 0) {
891 /* future extensibility for cmd-line arguments */
893 chroot_dir
= WITH_CHROOT
;
898 /* if run as root, prepare for dropping root privileges */
899 if (getuid() == 0 || geteuid() == 0) {
900 /* Run with '-Z root' to restore old behaviour */
902 username
= WITH_USER
;
906 if (RFileName
!= NULL
) {
908 const char *dlt_name
;
912 * We don't need network access, so relinquish any set-UID
913 * or set-GID privileges we have (if any).
915 * We do *not* want set-UID privileges when opening a
916 * trace file, as that might let the user read other
917 * people's trace files (especially if we're set-UID
920 if (setgid(getgid()) != 0 || setuid(getuid()) != 0 )
921 fprintf(stderr
, "Warning: setgid/setuid failed !\n");
923 pd
= pcap_open_offline(RFileName
, ebuf
);
926 dlt
= pcap_datalink(pd
);
927 dlt_name
= pcap_datalink_val_to_name(dlt
);
928 if (dlt_name
== NULL
) {
929 fprintf(stderr
, "reading from file %s, link-type %u\n",
933 "reading from file %s, link-type %s (%s)\n",
935 pcap_datalink_val_to_description(dlt
));
940 error("-f and -r options are incompatible");
942 if (device
== NULL
) {
943 device
= pcap_lookupdev(ebuf
);
948 if(strlen(device
) == 1) //we assume that an ASCII string is always longer than 1 char
949 { //a Unicode string has a \0 as second byte (so strlen() is 1)
950 fprintf(stderr
, "%s: listening on %ws\n", program_name
, device
);
954 fprintf(stderr
, "%s: listening on %s\n", program_name
, device
);
959 #ifdef HAVE_PCAP_CREATE
960 pd
= pcap_create(device
, ebuf
);
963 status
= pcap_set_snaplen(pd
, snaplen
);
965 error("%s: pcap_set_snaplen failed: %s",
966 device
, pcap_statustostr(status
));
967 status
= pcap_set_promisc(pd
, !pflag
);
969 error("%s: pcap_set_promisc failed: %s",
970 device
, pcap_statustostr(status
));
972 status
= pcap_set_rfmon(pd
, 1);
974 error("%s: pcap_set_rfmon failed: %s",
975 device
, pcap_statustostr(status
));
977 status
= pcap_set_timeout(pd
, 1000);
979 error("%s: pcap_set_timeout failed: %s",
980 device
, pcap_statustostr(status
));
982 status
= pcap_set_buffer_size(pd
, Bflag
);
984 error("%s: pcap_set_buffer_size failed: %s",
985 device
, pcap_statustostr(status
));
987 status
= pcap_activate(pd
);
990 * pcap_activate() failed.
992 cp
= pcap_geterr(pd
);
993 if (status
== PCAP_ERROR
)
995 else if ((status
== PCAP_ERROR_NO_SUCH_DEVICE
||
996 status
== PCAP_ERROR_PERM_DENIED
) &&
998 error("%s: %s\n(%s)", device
,
999 pcap_statustostr(status
), cp
);
1001 error("%s: %s", device
,
1002 pcap_statustostr(status
));
1003 } else if (status
> 0) {
1005 * pcap_activate() succeeded, but it's warning us
1006 * of a problem it had.
1008 cp
= pcap_geterr(pd
);
1009 if (status
== PCAP_WARNING
)
1011 else if (status
== PCAP_WARNING_PROMISC_NOTSUP
&&
1013 warning("%s: %s\n(%s)", device
,
1014 pcap_statustostr(status
), cp
);
1016 warning("%s: %s", device
,
1017 pcap_statustostr(status
));
1021 pd
= pcap_open_live(device
, snaplen
, !pflag
, 1000, ebuf
);
1025 warning("%s", ebuf
);
1026 #endif /* HAVE_PCAP_CREATE */
1028 * Let user own process after socket has been opened.
1031 if (setgid(getgid()) != 0 || setuid(getuid()) != 0)
1032 fprintf(stderr
, "Warning: setgid/setuid failed !\n");
1034 #if !defined(HAVE_PCAP_CREATE) && defined(WIN32)
1036 if(pcap_setbuff(pd
, Bflag
)==-1){
1037 error("%s", pcap_geterr(pd
));
1039 #endif /* !defined(HAVE_PCAP_CREATE) && defined(WIN32) */
1041 show_dlts_and_exit(pd
);
1042 if (gndo
->ndo_dlt
>= 0) {
1043 #ifdef HAVE_PCAP_SET_DATALINK
1044 if (pcap_set_datalink(pd
, gndo
->ndo_dlt
) < 0)
1045 error("%s", pcap_geterr(pd
));
1048 * We don't actually support changing the
1049 * data link type, so we only let them
1050 * set it to what it already is.
1052 if (gndo
->ndo_dlt
!= pcap_datalink(pd
)) {
1053 error("%s is not one of the DLTs supported by this device\n",
1057 (void)fprintf(stderr
, "%s: data link type %s\n",
1058 program_name
, gndo
->ndo_dltname
);
1059 (void)fflush(stderr
);
1061 i
= pcap_snapshot(pd
);
1063 warning("snaplen raised from %d to %d", snaplen
, i
);
1066 if (pcap_lookupnet(device
, &localnet
, &netmask
, ebuf
) < 0) {
1069 warning("%s", ebuf
);
1073 cmdbuf
= read_infile(infile
);
1075 cmdbuf
= copy_argv(&argv
[optind
]);
1077 if (pcap_compile(pd
, &fcode
, cmdbuf
, Oflag
, netmask
) < 0)
1078 error("%s", pcap_geterr(pd
));
1080 bpf_dump(&fcode
, dflag
);
1084 init_addrtoname(localnet
, netmask
);
1088 (void)setsignal(SIGPIPE
, cleanup
);
1089 (void)setsignal(SIGTERM
, cleanup
);
1090 (void)setsignal(SIGINT
, cleanup
);
1091 (void)setsignal(SIGCHLD
, child_cleanup
);
1093 /* Cooperate with nohup(1) */
1095 if ((oldhandler
= setsignal(SIGHUP
, cleanup
)) != SIG_DFL
)
1096 (void)setsignal(SIGHUP
, oldhandler
);
1099 if (pcap_setfilter(pd
, &fcode
) < 0)
1100 error("%s", pcap_geterr(pd
));
1103 /* Do not exceed the default NAME_MAX for files. */
1104 dumpinfo
.CurrentFileName
= (char *)malloc(NAME_MAX
+ 1);
1106 if (dumpinfo
.CurrentFileName
== NULL
)
1107 error("malloc of dumpinfo.CurrentFileName");
1109 /* We do not need numbering for dumpfiles if Cflag isn't set. */
1111 MakeFilename(dumpinfo
.CurrentFileName
, WFileName
, 0, WflagChars
);
1113 MakeFilename(dumpinfo
.CurrentFileName
, WFileName
, 0, 0);
1115 p
= pcap_dump_open(pd
, dumpinfo
.CurrentFileName
);
1117 error("%s", pcap_geterr(pd
));
1118 if (Cflag
!= 0 || Gflag
!= 0) {
1119 callback
= dump_packet_and_trunc
;
1120 dumpinfo
.WFileName
= WFileName
;
1123 pcap_userdata
= (u_char
*)&dumpinfo
;
1125 callback
= dump_packet
;
1126 pcap_userdata
= (u_char
*)p
;
1129 type
= pcap_datalink(pd
);
1130 printinfo
.printer
= lookup_printer(type
);
1131 if (printinfo
.printer
== NULL
) {
1132 gndo
->ndo_dltname
= pcap_datalink_val_to_name(type
);
1133 if (gndo
->ndo_dltname
!= NULL
)
1134 error("unsupported data link type %s",
1137 error("unsupported data link type %d", type
);
1139 callback
= print_packet
;
1140 pcap_userdata
= (u_char
*)&printinfo
;
1144 * We cannot do this earlier, because we want to be able to open
1145 * the file (if done) for writing before giving up permissions.
1147 if (getuid() == 0 || geteuid() == 0) {
1148 if (username
|| chroot_dir
)
1149 droproot(username
, chroot_dir
);
1154 * We can't get statistics when reading from a file rather
1155 * than capturing from a device.
1157 if (RFileName
== NULL
)
1158 (void)setsignal(SIGINFO
, requestinfo
);
1161 if (vflag
> 0 && WFileName
) {
1163 * When capturing to a file, "-v" means tcpdump should,
1164 * every 10 secodns, "v"erbosely report the number of
1167 #ifdef USE_WIN32_MM_TIMER
1168 /* call verbose_stats_dump() each 1000 +/-100msec */
1169 timer_id
= timeSetEvent(1000, 100, verbose_stats_dump
, 0, TIME_PERIODIC
);
1170 setvbuf(stderr
, NULL
, _IONBF
, 0);
1171 #elif defined(HAVE_ALARM)
1172 (void)setsignal(SIGALRM
, verbose_stats_dump
);
1178 if (RFileName
== NULL
) {
1180 const char *dlt_name
;
1182 if (!vflag
&& !WFileName
) {
1183 (void)fprintf(stderr
,
1184 "%s: verbose output suppressed, use -v or -vv for full protocol decode\n",
1187 (void)fprintf(stderr
, "%s: ", program_name
);
1188 dlt
= pcap_datalink(pd
);
1189 dlt_name
= pcap_datalink_val_to_name(dlt
);
1190 if (dlt_name
== NULL
) {
1191 (void)fprintf(stderr
, "listening on %s, link-type %u, capture size %u bytes\n",
1192 device
, dlt
, snaplen
);
1194 (void)fprintf(stderr
, "listening on %s, link-type %s (%s), capture size %u bytes\n",
1196 pcap_datalink_val_to_description(dlt
), snaplen
);
1198 (void)fflush(stderr
);
1201 status
= pcap_loop(pd
, cnt
, callback
, pcap_userdata
);
1202 if (WFileName
== NULL
) {
1204 * We're printing packets. Flush the printed output,
1205 * so it doesn't get intermingled with error output.
1209 * We got interrupted, so perhaps we didn't
1210 * manage to finish a line we were printing.
1211 * Print an extra newline, just in case.
1215 (void)fflush(stdout
);
1221 (void)fprintf(stderr
, "%s: pcap_loop: %s\n",
1222 program_name
, pcap_geterr(pd
));
1224 if (RFileName
== NULL
) {
1226 * We're doing a live capture. Report the capture
1232 exit(status
== -1 ? 1 : 0);
1235 /* make a clean exit on interrupts */
1237 cleanup(int signo _U_
)
1239 #ifdef USE_WIN32_MM_TIMER
1241 timeKillEvent(timer_id
);
1243 #elif defined(HAVE_ALARM)
1247 #ifdef HAVE_PCAP_BREAKLOOP
1249 * We have "pcap_breakloop()"; use it, so that we do as little
1250 * as possible in the signal handler (it's probably not safe
1251 * to do anything with standard I/O streams in a signal handler -
1252 * the ANSI C standard doesn't say it is).
1257 * We don't have "pcap_breakloop()"; this isn't safe, but
1258 * it's the best we can do. Print the summary if we're
1259 * not reading from a savefile - i.e., if we're doing a
1260 * live capture - and exit.
1262 if (pd
!= NULL
&& pcap_file(pd
) == NULL
) {
1264 * We got interrupted, so perhaps we didn't
1265 * manage to finish a line we were printing.
1266 * Print an extra newline, just in case.
1269 (void)fflush(stdout
);
1277 On windows, we do not use a fork, so we do not care less about
1278 waiting a child processes to die
1282 child_cleanup(int signo _U_
)
1289 info(register int verbose
)
1291 struct pcap_stat stat
;
1293 if (pcap_stats(pd
, &stat
) < 0) {
1294 (void)fprintf(stderr
, "pcap_stats: %s\n", pcap_geterr(pd
));
1300 fprintf(stderr
, "%s: ", program_name
);
1302 (void)fprintf(stderr
, "%u packets captured", packets_captured
);
1304 fputs(", ", stderr
);
1307 (void)fprintf(stderr
, "%d packets received by filter", stat
.ps_recv
);
1309 fputs(", ", stderr
);
1312 (void)fprintf(stderr
, "%d packets dropped by kernel\n", stat
.ps_drop
);
1318 compress_savefile(const char *filename
)
1323 * Set to lowest priority so that this doesn't disturb the capture
1326 setpriority(PRIO_PROCESS
, 0, NZERO
- 1);
1328 setpriority(PRIO_PROCESS
, 0, 19);
1330 if (execlp(zflag
, zflag
, filename
, NULL
) == -1)
1332 "compress_savefile:execlp(%s, %s): %s\n",
1339 compress_savefile(const char *filename
)
1342 "compress_savefile failed. Functionality not implemented under windows\n");
1347 dump_packet_and_trunc(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
1349 struct dump_info
*dump_info
;
1355 dump_info
= (struct dump_info
*)user
;
1358 * XXX - this won't force the file to rotate on the specified time
1359 * boundary, but it will rotate on the first packet received after the
1360 * specified Gflag number of seconds. Note: if a Gflag time boundary
1361 * and a Cflag size boundary coincide, the time rotation will occur
1362 * first thereby cancelling the Cflag boundary (since the file should
1366 /* Check if it is time to rotate */
1369 /* Get the current time */
1370 if ((t
= time(NULL
)) == (time_t)-1) {
1371 error("dump_and_trunc_packet: can't get current_time: %s",
1372 pcap_strerror(errno
));
1376 /* If the time is greater than the specified window, rotate */
1377 if (t
- Gflag_time
>= Gflag
) {
1378 /* Update the Gflag_time */
1380 /* Update Gflag_count */
1383 * Close the current file and open a new one.
1385 pcap_dump_close(dump_info
->p
);
1388 * Compress the file we just closed, if the user asked for it
1391 compress_savefile(dump_info
->CurrentFileName
);
1394 * Check to see if we've exceeded the Wflag (when
1397 if (Cflag
== 0 && Wflag
> 0 && Gflag_count
>= Wflag
) {
1398 (void)fprintf(stderr
, "Maximum file limit reached: %d\n",
1403 if (dump_info
->CurrentFileName
!= NULL
)
1404 free(dump_info
->CurrentFileName
);
1405 /* Allocate space for max filename + \0. */
1406 dump_info
->CurrentFileName
= (char *)malloc(NAME_MAX
+ 1);
1407 if (dump_info
->CurrentFileName
== NULL
)
1408 error("dump_packet_and_trunc: malloc");
1410 * This is always the first file in the Cflag
1412 * We also don't need numbering if Cflag is not set.
1415 MakeFilename(dump_info
->CurrentFileName
, dump_info
->WFileName
, 0,
1418 MakeFilename(dump_info
->CurrentFileName
, dump_info
->WFileName
, 0, 0);
1420 dump_info
->p
= pcap_dump_open(dump_info
->pd
, dump_info
->CurrentFileName
);
1421 if (dump_info
->p
== NULL
)
1422 error("%s", pcap_geterr(pd
));
1427 * XXX - this won't prevent capture files from getting
1428 * larger than Cflag - the last packet written to the
1429 * file could put it over Cflag.
1431 if (Cflag
!= 0 && pcap_dump_ftell(dump_info
->p
) > Cflag
) {
1433 * Close the current file and open a new one.
1435 pcap_dump_close(dump_info
->p
);
1438 * Compress the file we just closed, if the user asked for it
1441 compress_savefile(dump_info
->CurrentFileName
);
1445 if (Cflag_count
>= Wflag
)
1448 if (dump_info
->CurrentFileName
!= NULL
)
1449 free(dump_info
->CurrentFileName
);
1450 dump_info
->CurrentFileName
= (char *)malloc(NAME_MAX
+ 1);
1451 if (dump_info
->CurrentFileName
== NULL
)
1452 error("dump_packet_and_trunc: malloc");
1453 MakeFilename(dump_info
->CurrentFileName
, dump_info
->WFileName
, Cflag_count
, WflagChars
);
1454 dump_info
->p
= pcap_dump_open(dump_info
->pd
, dump_info
->CurrentFileName
);
1455 if (dump_info
->p
== NULL
)
1456 error("%s", pcap_geterr(pd
));
1459 pcap_dump((u_char
*)dump_info
->p
, h
, sp
);
1460 #ifdef HAVE_PCAP_DUMP_FLUSH
1462 pcap_dump_flush(dump_info
->p
);
1471 dump_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
1477 pcap_dump(user
, h
, sp
);
1478 #ifdef HAVE_PCAP_DUMP_FLUSH
1480 pcap_dump_flush((pcap_dumper_t
*)user
);
1489 print_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
1491 struct print_info
*print_info
;
1499 print_info
= (struct print_info
*)user
;
1502 * Some printers want to check that they're not walking off the
1503 * end of the packet.
1504 * Rather than pass it all the way down, we set this global.
1506 snapend
= sp
+ h
->caplen
;
1508 hdrlen
= (*print_info
->printer
)(h
, sp
);
1511 * Print the raw packet data in hex and ASCII.
1515 * Include the link-layer header.
1517 hex_and_ascii_print("\n\t", sp
, h
->caplen
);
1520 * Don't include the link-layer header - and if
1521 * we have nothing past the link-layer header,
1524 if (h
->caplen
> hdrlen
)
1525 hex_and_ascii_print("\n\t", sp
+ hdrlen
,
1526 h
->caplen
- hdrlen
);
1530 * Print the raw packet data in hex.
1534 * Include the link-layer header.
1536 hex_print("\n\t", sp
, h
->caplen
);
1539 * Don't include the link-layer header - and if
1540 * we have nothing past the link-layer header,
1543 if (h
->caplen
> hdrlen
)
1544 hex_print("\n\t", sp
+ hdrlen
,
1545 h
->caplen
- hdrlen
);
1549 * Print the raw packet data in ASCII.
1553 * Include the link-layer header.
1555 ascii_print(sp
, h
->caplen
);
1558 * Don't include the link-layer header - and if
1559 * we have nothing past the link-layer header,
1562 if (h
->caplen
> hdrlen
)
1563 ascii_print(sp
+ hdrlen
, h
->caplen
- hdrlen
);
1576 * XXX - there should really be libpcap calls to get the version
1577 * number as a string (the string would be generated from #defines
1578 * at run time, so that it's not generated from string constants
1579 * in the library, as, on many UNIX systems, those constants would
1580 * be statically linked into the application executable image, and
1581 * would thus reflect the version of libpcap on the system on
1582 * which the application was *linked*, not the system on which it's
1585 * That routine should be documented, unlike the "version[]"
1586 * string, so that UNIX vendors providing their own libpcaps
1587 * don't omit it (as a couple of vendors have...).
1589 * Packet.dll should perhaps also export a routine to return the
1590 * version number of the Packet.dll code, to supply the
1591 * "Wpcap_version" information on Windows.
1593 char WDversion
[]="current-cvs.tcpdump.org";
1594 #if !defined(HAVE_GENERATED_VERSION)
1595 char version
[]="current-cvs.tcpdump.org";
1597 char pcap_version
[]="current-cvs.tcpdump.org";
1598 char Wpcap_version
[]="3.1";
1602 * By default, print the specified data out in hex and ASCII.
1605 ndo_default_print(netdissect_options
*ndo _U_
, const u_char
*bp
, u_int length
)
1607 hex_and_ascii_print("\n\t", bp
, length
); /* pass on lf and identation string */
1611 default_print(const u_char
*bp
, u_int length
)
1613 ndo_default_print(gndo
, bp
, length
);
1617 RETSIGTYPE
requestinfo(int signo _U_
)
1627 * Called once each second in verbose mode while dumping to file
1629 #ifdef USE_WIN32_MM_TIMER
1630 void CALLBACK
verbose_stats_dump (UINT timer_id _U_
, UINT msg _U_
, DWORD_PTR arg _U_
,
1631 DWORD_PTR dw1 _U_
, DWORD_PTR dw2 _U_
)
1633 struct pcap_stat stat
;
1635 if (infodelay
== 0 && pcap_stats(pd
, &stat
) >= 0)
1636 fprintf(stderr
, "Got %u\r", packets_captured
);
1638 #elif defined(HAVE_ALARM)
1639 static void verbose_stats_dump(int sig _U_
)
1641 struct pcap_stat stat
;
1643 if (infodelay
== 0 && pcap_stats(pd
, &stat
) >= 0)
1644 fprintf(stderr
, "Got %u\r", packets_captured
);
1652 extern char version
[];
1653 #ifndef HAVE_PCAP_LIB_VERSION
1654 #if defined(WIN32) || defined(HAVE_PCAP_VERSION)
1655 extern char pcap_version
[];
1656 #else /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
1657 static char pcap_version
[] = "unknown";
1658 #endif /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
1659 #endif /* HAVE_PCAP_LIB_VERSION */
1661 #ifdef HAVE_PCAP_LIB_VERSION
1663 (void)fprintf(stderr
, "%s version %s, based on tcpdump version %s\n", program_name
, WDversion
, version
);
1665 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
1667 (void)fprintf(stderr
, "%s\n",pcap_lib_version());
1668 #else /* HAVE_PCAP_LIB_VERSION */
1670 (void)fprintf(stderr
, "%s version %s, based on tcpdump version %s\n", program_name
, WDversion
, version
);
1671 (void)fprintf(stderr
, "WinPcap version %s, based on libpcap version %s\n",Wpcap_version
, pcap_version
);
1673 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
1674 (void)fprintf(stderr
, "libpcap version %s\n", pcap_version
);
1676 #endif /* HAVE_PCAP_LIB_VERSION */
1677 (void)fprintf(stderr
,
1678 "Usage: %s [-aAd" D_FLAG
"ef" I_FLAG
"KlLnNOpqRStu" U_FLAG
"vxX]" B_FLAG_USAGE
" [ -c count ]\n", program_name
);
1679 (void)fprintf(stderr
,
1680 "\t\t[ -C file_size ] [ -E algo:secret ] [ -F file ] [ -G seconds ]\n");
1681 (void)fprintf(stderr
,
1682 "\t\t[ -i interface ] [ -M secret ] [ -r file ]\n");
1683 (void)fprintf(stderr
,
1684 "\t\t[ -s snaplen ] [ -T type ] [ -w file ] [ -W filecount ]\n");
1685 (void)fprintf(stderr
,
1686 "\t\t[ -y datalinktype ] [ -z command ] [ -Z user ]\n");
1687 (void)fprintf(stderr
,
1688 "\t\t[ expression ]\n");
1696 ndo_error(netdissect_options
*ndo _U_
, const char *fmt
, ...)
1700 (void)fprintf(stderr
, "%s: ", program_name
);
1702 (void)vfprintf(stderr
, fmt
, ap
);
1706 if (fmt
[-1] != '\n')
1707 (void)fputc('\n', stderr
);
1715 ndo_warning(netdissect_options
*ndo _U_
, const char *fmt
, ...)
1719 (void)fprintf(stderr
, "%s: WARNING: ", program_name
);
1721 (void)vfprintf(stderr
, fmt
, ap
);
1725 if (fmt
[-1] != '\n')
1726 (void)fputc('\n', stderr
);