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";
35 * tcpdump - monitor tcp/ip traffic on an ethernet.
37 * First written in 1987 by Van Jacobson, Lawrence Berkeley Laboratory.
38 * Mercilessly hacked and occasionally improved since then via the
39 * combined efforts of Van, Steve McCanne and Craig Leres of LBL.
46 #include <tcpdump-stdinc.h>
51 extern int strcasecmp (const char *__s1
, const char *__s2
);
69 #include <sys/resource.h>
75 /* capabilities convinience library */
78 #endif /* HAVE_CAP_NG_H */
80 #include "netdissect.h"
81 #include "interface.h"
82 #include "addrtoname.h"
84 #include "setsignal.h"
85 #include "gmt2local.h"
86 #include "pcap-missing.h"
93 #define SIGNAL_REQ_INFO SIGINFO
95 #define SIGNAL_REQ_INFO SIGUSR1
98 netdissect_options Gndo
;
99 netdissect_options
*gndo
= &Gndo
;
101 static int Dflag
; /* list available devices and exit */
102 static int dflag
; /* print filter code */
103 static int Lflag
; /* list available data link types and exit */
104 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
105 static int Jflag
; /* list available time stamp types */
107 #ifdef HAVE_PCAP_SETDIRECTION
108 int Qflag
= -1; /* restrict captured packet by send/receive direction */
110 static char *zflag
= NULL
; /* compress each savefile using a specified command (like gzip or bzip2) */
112 static int infodelay
;
113 static int infoprint
;
117 int32_t thiszone
; /* seconds offset from gmt to local time */
120 static RETSIGTYPE
cleanup(int);
121 static RETSIGTYPE
child_cleanup(int);
122 static void usage(void) __attribute__((noreturn
));
123 static void show_dlts_and_exit(const char *device
, pcap_t
*pd
) __attribute__((noreturn
));
125 static void print_packet(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
126 static void ndo_default_print(netdissect_options
*, const u_char
*, u_int
);
127 static void dump_packet_and_trunc(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
128 static void dump_packet(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
129 static void droproot(const char *, const char *);
130 static void ndo_error(netdissect_options
*ndo
, const char *fmt
, ...)
131 __attribute__((noreturn
))
132 #ifdef __ATTRIBUTE___FORMAT_OK
133 __attribute__((format (printf
, 2, 3)))
134 #endif /* __ATTRIBUTE___FORMAT_OK */
136 static void ndo_warning(netdissect_options
*ndo
, const char *fmt
, ...)
137 #ifdef __ATTRIBUTE___FORMAT_OK
138 __attribute__((format (printf
, 2, 3)))
139 #endif /* __ATTRIBUTE___FORMAT_OK */
142 #ifdef SIGNAL_REQ_INFO
143 RETSIGTYPE
requestinfo(int);
146 #if defined(USE_WIN32_MM_TIMER)
147 #include <MMsystem.h>
148 static UINT timer_id
;
149 static void CALLBACK
verbose_stats_dump(UINT
, UINT
, DWORD_PTR
, DWORD_PTR
, DWORD_PTR
);
150 #elif defined(HAVE_ALARM)
151 static void verbose_stats_dump(int sig
);
154 static void info(int);
155 static u_int packets_captured
;
169 static const struct printer printers
[] = {
170 { ppp_if_print
, DLT_PPP
},
171 #ifdef DLT_PPP_WITHDIRECTION
172 { ppp_if_print
, DLT_PPP_WITHDIRECTION
},
175 { ppp_bsdos_if_print
, DLT_PPP_BSDOS
},
177 #ifdef DLT_PPP_SERIAL
178 { ppp_hdlc_if_print
, DLT_PPP_SERIAL
},
183 static const struct ndo_printer ndo_printers
[] = {
184 { ether_if_print
, DLT_EN10MB
},
186 { ipnet_if_print
, DLT_IPNET
},
188 #ifdef DLT_IEEE802_15_4
189 { ieee802_15_4_if_print
, DLT_IEEE802_15_4
},
191 #ifdef DLT_IEEE802_15_4_NOFCS
192 { ieee802_15_4_if_print
, DLT_IEEE802_15_4_NOFCS
},
195 { ppi_if_print
, DLT_PPI
},
197 #ifdef DLT_NETANALYZER
198 { netanalyzer_if_print
, DLT_NETANALYZER
},
200 #ifdef DLT_NETANALYZER_TRANSPARENT
201 { netanalyzer_transparent_if_print
, DLT_NETANALYZER_TRANSPARENT
},
203 #if defined(DLT_NFLOG) && defined(HAVE_PCAP_NFLOG_H)
204 { nflog_if_print
, DLT_NFLOG
},
207 { cip_if_print
, DLT_CIP
},
210 { cip_if_print
, DLT_ATM_CLIP
},
212 #ifdef DLT_IP_OVER_FC
213 { ipfc_if_print
, DLT_IP_OVER_FC
},
215 { null_if_print
, DLT_NULL
},
217 { null_if_print
, DLT_LOOP
},
219 #ifdef DLT_APPLE_IP_OVER_IEEE1394
220 { ap1394_if_print
, DLT_APPLE_IP_OVER_IEEE1394
},
222 #if defined(DLT_BLUETOOTH_HCI_H4_WITH_PHDR) && defined(HAVE_PCAP_BLUETOOTH_H)
223 { bt_if_print
, DLT_BLUETOOTH_HCI_H4_WITH_PHDR
},
226 { lane_if_print
, DLT_LANE8023
},
228 { arcnet_if_print
, DLT_ARCNET
},
229 #ifdef DLT_ARCNET_LINUX
230 { arcnet_linux_if_print
, DLT_ARCNET_LINUX
},
232 { raw_if_print
, DLT_RAW
},
234 { raw_if_print
, DLT_IPV4
},
237 { raw_if_print
, DLT_IPV6
},
239 #ifdef HAVE_PCAP_USB_H
241 { usb_linux_48_byte_print
, DLT_USB_LINUX
},
242 #endif /* DLT_USB_LINUX */
243 #ifdef DLT_USB_LINUX_MMAPPED
244 { usb_linux_64_byte_print
, DLT_USB_LINUX_MMAPPED
},
245 #endif /* DLT_USB_LINUX_MMAPPED */
246 #endif /* HAVE_PCAP_USB_H */
247 #ifdef DLT_SYMANTEC_FIREWALL
248 { symantec_if_print
, DLT_SYMANTEC_FIREWALL
},
251 { chdlc_if_print
, DLT_C_HDLC
},
254 { chdlc_if_print
, DLT_HDLC
},
257 { pppoe_if_print
, DLT_PPP_ETHER
},
259 #if defined(DLT_PFLOG) && defined(HAVE_NET_PFVAR_H)
260 { pflog_if_print
, DLT_PFLOG
},
262 { token_if_print
, DLT_IEEE802
},
263 { fddi_if_print
, DLT_FDDI
},
265 { sll_if_print
, DLT_LINUX_SLL
},
268 { fr_if_print
, DLT_FR
},
271 { fr_if_print
, DLT_FRELAY
},
274 { mfr_if_print
, DLT_MFR
},
276 { atm_if_print
, DLT_ATM_RFC1483
},
278 { sunatm_if_print
, DLT_SUNATM
},
281 { enc_if_print
, DLT_ENC
},
283 { sl_if_print
, DLT_SLIP
},
284 #ifdef DLT_SLIP_BSDOS
285 { sl_bsdos_if_print
, DLT_SLIP_BSDOS
},
288 { ltalk_if_print
, DLT_LTALK
},
290 #ifdef DLT_JUNIPER_ATM1
291 { juniper_atm1_print
, DLT_JUNIPER_ATM1
},
293 #ifdef DLT_JUNIPER_ATM2
294 { juniper_atm2_print
, DLT_JUNIPER_ATM2
},
296 #ifdef DLT_JUNIPER_MFR
297 { juniper_mfr_print
, DLT_JUNIPER_MFR
},
299 #ifdef DLT_JUNIPER_MLFR
300 { juniper_mlfr_print
, DLT_JUNIPER_MLFR
},
302 #ifdef DLT_JUNIPER_MLPPP
303 { juniper_mlppp_print
, DLT_JUNIPER_MLPPP
},
305 #ifdef DLT_JUNIPER_PPPOE
306 { juniper_pppoe_print
, DLT_JUNIPER_PPPOE
},
308 #ifdef DLT_JUNIPER_PPPOE_ATM
309 { juniper_pppoe_atm_print
, DLT_JUNIPER_PPPOE_ATM
},
311 #ifdef DLT_JUNIPER_GGSN
312 { juniper_ggsn_print
, DLT_JUNIPER_GGSN
},
314 #ifdef DLT_JUNIPER_ES
315 { juniper_es_print
, DLT_JUNIPER_ES
},
317 #ifdef DLT_JUNIPER_MONITOR
318 { juniper_monitor_print
, DLT_JUNIPER_MONITOR
},
320 #ifdef DLT_JUNIPER_SERVICES
321 { juniper_services_print
, DLT_JUNIPER_SERVICES
},
323 #ifdef DLT_JUNIPER_ETHER
324 { juniper_ether_print
, DLT_JUNIPER_ETHER
},
326 #ifdef DLT_JUNIPER_PPP
327 { juniper_ppp_print
, DLT_JUNIPER_PPP
},
329 #ifdef DLT_JUNIPER_FRELAY
330 { juniper_frelay_print
, DLT_JUNIPER_FRELAY
},
332 #ifdef DLT_JUNIPER_CHDLC
333 { juniper_chdlc_print
, DLT_JUNIPER_CHDLC
},
336 { pktap_if_print
, DLT_PKTAP
},
338 #ifdef DLT_IEEE802_11_RADIO
339 { ieee802_11_radio_if_print
, DLT_IEEE802_11_RADIO
},
341 #ifdef DLT_IEEE802_11
342 { ieee802_11_if_print
, DLT_IEEE802_11
},
344 #ifdef DLT_IEEE802_11_RADIO_AVS
345 { ieee802_11_radio_avs_if_print
, DLT_IEEE802_11_RADIO_AVS
},
347 #ifdef DLT_PRISM_HEADER
348 { prism_if_print
, DLT_PRISM_HEADER
},
353 static const struct tok status_flags
[] = {
355 { PCAP_IF_UP
, "Up" },
357 #ifdef PCAP_IF_RUNNING
358 { PCAP_IF_RUNNING
, "Running" },
360 { PCAP_IF_LOOPBACK
, "Loopback" },
365 lookup_printer(int type
)
367 const struct printer
*p
;
369 for (p
= printers
; p
->f
; ++p
)
378 lookup_ndo_printer(int type
)
380 const struct ndo_printer
*p
;
382 for (p
= ndo_printers
; p
->f
; ++p
)
386 #if defined(DLT_USER2) && defined(DLT_PKTAP)
388 * Apple incorrectly chose to use DLT_USER2 for their PKTAP
391 * We map DLT_PKTAP, whether it's DLT_USER2 as it is on Darwin-
392 * based OSes or the same value as LINKTYPE_PKTAP as it is on
393 * other OSes, to LINKTYPE_PKTAP, so files written with
394 * this version of libpcap for a DLT_PKTAP capture have a link-
395 * layer header type of LINKTYPE_PKTAP.
397 * However, files written on OS X Mavericks for a DLT_PKTAP
398 * capture have a link-layer header type of LINKTYPE_USER2.
399 * If we don't have a printer for DLT_USER2, and type is
400 * DLT_USER2, we look up the printer for DLT_PKTAP and use
403 if (type
== DLT_USER2
) {
404 for (p
= ndo_printers
; p
->f
; ++p
)
405 if (DLT_PKTAP
== p
->type
)
416 static int supports_monitor_mode
;
423 netdissect_options
*ndo
;
426 if_ndo_printer ndo_printer
;
433 char *CurrentFileName
;
438 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
440 show_tstamp_types_and_exit(const char *device
, pcap_t
*pd
)
443 int *tstamp_types
= 0;
444 const char *tstamp_type_name
;
447 n_tstamp_types
= pcap_list_tstamp_types(pd
, &tstamp_types
);
448 if (n_tstamp_types
< 0)
449 error("%s", pcap_geterr(pd
));
451 if (n_tstamp_types
== 0) {
452 fprintf(stderr
, "Time stamp type cannot be set for %s\n",
456 fprintf(stderr
, "Time stamp types for %s (use option -j to set):\n",
458 for (i
= 0; i
< n_tstamp_types
; i
++) {
459 tstamp_type_name
= pcap_tstamp_type_val_to_name(tstamp_types
[i
]);
460 if (tstamp_type_name
!= NULL
) {
461 (void) fprintf(stderr
, " %s (%s)\n", tstamp_type_name
,
462 pcap_tstamp_type_val_to_description(tstamp_types
[i
]));
464 (void) fprintf(stderr
, " %d\n", tstamp_types
[i
]);
467 pcap_free_tstamp_types(tstamp_types
);
473 show_dlts_and_exit(const char *device
, pcap_t
*pd
)
477 const char *dlt_name
;
479 n_dlts
= pcap_list_datalinks(pd
, &dlts
);
481 error("%s", pcap_geterr(pd
));
482 else if (n_dlts
== 0 || !dlts
)
483 error("No data link types.");
486 * If the interface is known to support monitor mode, indicate
487 * whether these are the data link types available when not in
488 * monitor mode, if -I wasn't specified, or when in monitor mode,
489 * when -I was specified (the link-layer types available in
490 * monitor mode might be different from the ones available when
491 * not in monitor mode).
493 if (supports_monitor_mode
)
494 (void) fprintf(stderr
, "Data link types for %s %s (use option -y to set):\n",
496 Iflag
? "when in monitor mode" : "when not in monitor mode");
498 (void) fprintf(stderr
, "Data link types for %s (use option -y to set):\n",
501 while (--n_dlts
>= 0) {
502 dlt_name
= pcap_datalink_val_to_name(dlts
[n_dlts
]);
503 if (dlt_name
!= NULL
) {
504 (void) fprintf(stderr
, " %s (%s)", dlt_name
,
505 pcap_datalink_val_to_description(dlts
[n_dlts
]));
508 * OK, does tcpdump handle that type?
510 if (lookup_printer(dlts
[n_dlts
]) == NULL
511 && lookup_ndo_printer(dlts
[n_dlts
]) == NULL
)
512 (void) fprintf(stderr
, " (printing not supported)");
513 fprintf(stderr
, "\n");
515 (void) fprintf(stderr
, " DLT %d (printing not supported)\n",
519 #ifdef HAVE_PCAP_FREE_DATALINKS
520 pcap_free_datalinks(dlts
);
525 #ifdef HAVE_PCAP_FINDALLDEVS
527 show_devices_and_exit (void)
529 pcap_if_t
*devpointer
;
530 char ebuf
[PCAP_ERRBUF_SIZE
];
533 if (pcap_findalldevs(&devpointer
, ebuf
) < 0)
536 for (i
= 0; devpointer
!= NULL
; i
++) {
537 printf("%d.%s", i
+1, devpointer
->name
);
538 if (devpointer
->description
!= NULL
)
539 printf(" (%s)", devpointer
->description
);
540 if (devpointer
->flags
!= 0)
541 printf(" [%s]", bittok2str(status_flags
, "none", devpointer
->flags
));
543 devpointer
= devpointer
->next
;
548 #endif /* HAVE_PCAP_FINDALLDEVS */
551 * Set up flags that might or might not be supported depending on the
552 * version of libpcap we're using.
554 #if defined(HAVE_PCAP_CREATE) || defined(WIN32)
556 #define B_FLAG_USAGE " [ -B size ]"
557 #else /* defined(HAVE_PCAP_CREATE) || defined(WIN32) */
560 #endif /* defined(HAVE_PCAP_CREATE) || defined(WIN32) */
562 #ifdef HAVE_PCAP_CREATE
564 #else /* HAVE_PCAP_CREATE */
566 #endif /* HAVE_PCAP_CREATE */
568 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
570 #define j_FLAG_USAGE " [ -j tstamptype ]"
572 #else /* PCAP_ERROR_TSTAMP_TYPE_NOTSUP */
576 #endif /* PCAP_ERROR_TSTAMP_TYPE_NOTSUP */
578 #ifdef HAVE_PCAP_FINDALLDEVS
579 #ifndef HAVE_PCAP_IF_T
580 #undef HAVE_PCAP_FINDALLDEVS
584 #ifdef HAVE_PCAP_FINDALLDEVS
590 #ifdef HAVE_PCAP_DUMP_FLUSH
596 #ifdef HAVE_PCAP_SETDIRECTION
603 /* Drop root privileges and chroot if necessary */
605 droproot(const char *username
, const char *chroot_dir
)
607 struct passwd
*pw
= NULL
;
609 if (chroot_dir
&& !username
) {
610 fprintf(stderr
, "tcpdump: Chroot without dropping root is insecure\n");
614 pw
= getpwnam(username
);
617 if (chroot(chroot_dir
) != 0 || chdir ("/") != 0) {
618 fprintf(stderr
, "tcpdump: Couldn't chroot/chdir to '%.64s': %s\n",
619 chroot_dir
, pcap_strerror(errno
));
624 int ret
= capng_change_id(pw
->pw_uid
, pw
->pw_gid
, CAPNG_NO_FLAG
);
626 printf("error : ret %d\n", ret
);
628 /* We don't need CAP_SETUID and CAP_SETGID */
629 capng_update(CAPNG_DROP
, CAPNG_EFFECTIVE
, CAP_SETUID
);
630 capng_update(CAPNG_DROP
, CAPNG_EFFECTIVE
, CAP_SETUID
);
631 capng_update(CAPNG_DROP
, CAPNG_PERMITTED
, CAP_SETUID
);
632 capng_update(CAPNG_DROP
, CAPNG_PERMITTED
, CAP_SETUID
);
633 capng_apply(CAPNG_SELECT_BOTH
);
636 if (initgroups(pw
->pw_name
, pw
->pw_gid
) != 0 ||
637 setgid(pw
->pw_gid
) != 0 || setuid(pw
->pw_uid
) != 0) {
638 fprintf(stderr
, "tcpdump: Couldn't change to '%.32s' uid=%lu gid=%lu: %s\n",
640 (unsigned long)pw
->pw_uid
,
641 (unsigned long)pw
->pw_gid
,
642 pcap_strerror(errno
));
645 #endif /* HAVE_CAP_NG_H */
648 fprintf(stderr
, "tcpdump: Couldn't find user '%.32s'\n",
671 MakeFilename(char *buffer
, char *orig_name
, int cnt
, int max_chars
)
673 char *filename
= malloc(PATH_MAX
+ 1);
674 if (filename
== NULL
)
675 error("Makefilename: malloc");
677 /* Process with strftime if Gflag is set. */
681 /* Convert Gflag_time to a usable format */
682 if ((local_tm
= localtime(&Gflag_time
)) == NULL
) {
683 error("MakeTimedFilename: localtime");
686 /* There's no good way to detect an error in strftime since a return
687 * value of 0 isn't necessarily failure.
689 strftime(filename
, PATH_MAX
, orig_name
, local_tm
);
691 strncpy(filename
, orig_name
, PATH_MAX
);
694 if (cnt
== 0 && max_chars
== 0)
695 strncpy(buffer
, filename
, PATH_MAX
+ 1);
697 if (snprintf(buffer
, PATH_MAX
+ 1, "%s%0*d", filename
, max_chars
, cnt
) > PATH_MAX
)
698 /* Report an error if the filename is too large */
699 error("too many output files or filename is too long (> %d)", PATH_MAX
);
703 static int tcpdump_printf(netdissect_options
*ndo _U_
,
704 const char *fmt
, ...)
711 ret
=vfprintf(stdout
, fmt
, args
);
717 static struct print_info
718 get_print_info(int type
)
720 struct print_info printinfo
;
722 printinfo
.ndo_type
= 1;
723 printinfo
.ndo
= gndo
;
724 printinfo
.p
.ndo_printer
= lookup_ndo_printer(type
);
725 if (printinfo
.p
.ndo_printer
== NULL
) {
726 printinfo
.p
.printer
= lookup_printer(type
);
727 printinfo
.ndo_type
= 0;
728 if (printinfo
.p
.printer
== NULL
) {
729 gndo
->ndo_dltname
= pcap_datalink_val_to_name(type
);
730 if (gndo
->ndo_dltname
!= NULL
)
731 error("packet printing is not supported for link type %s: use -w",
734 error("packet printing is not supported for link type %d: use -w", type
);
741 get_next_file(FILE *VFile
, char *ptr
)
745 ret
= fgets(ptr
, PATH_MAX
, VFile
);
749 if (ptr
[strlen(ptr
) - 1] == '\n')
750 ptr
[strlen(ptr
) - 1] = '\0';
756 main(int argc
, char **argv
)
758 register int cnt
, op
, i
;
759 bpf_u_int32 localnet
=0 , netmask
= 0;
760 register char *cp
, *infile
, *cmdbuf
, *device
, *RFileName
, *VFileName
, *WFileName
;
761 pcap_handler callback
;
765 const char *dlt_name
;
766 struct bpf_program fcode
;
768 RETSIGTYPE (*oldhandler
)(int);
770 struct print_info printinfo
;
771 struct dump_info dumpinfo
;
772 u_char
*pcap_userdata
;
773 char ebuf
[PCAP_ERRBUF_SIZE
];
774 char VFileLine
[PATH_MAX
+ 1];
775 char *username
= NULL
;
776 char *chroot_dir
= NULL
;
779 #ifdef HAVE_PCAP_FINDALLDEVS
780 pcap_if_t
*devpointer
;
786 if(wsockinit() != 0) return 1;
789 jflag
=-1; /* not set */
793 gndo
->ndo_default_print
=ndo_default_print
;
794 gndo
->ndo_printf
=tcpdump_printf
;
795 gndo
->ndo_error
=ndo_error
;
796 gndo
->ndo_warning
=ndo_warning
;
797 gndo
->ndo_snaplen
= DEFAULT_SNAPLEN
;
807 if ((cp
= strrchr(argv
[0], '/')) != NULL
)
808 program_name
= cp
+ 1;
810 program_name
= argv
[0];
812 if (abort_on_misalignment(ebuf
, sizeof(ebuf
)) < 0)
820 (op
= getopt(argc
, argv
, "aAb" B_FLAG
"c:C:d" D_FLAG
"eE:fF:G:hHi:" I_FLAG j_FLAG J_FLAG
"KlLm:M:nNOpq" Q_FLAG
"r:Rs:StT:u" U_FLAG
"vV:w:W:xXy:Yz:Z:")) != -1)
824 /* compatibility for old -a */
835 #if defined(HAVE_PCAP_CREATE) || defined(WIN32)
837 Bflag
= atoi(optarg
)*1024;
839 error("invalid packet buffer size %s", optarg
);
841 #endif /* defined(HAVE_PCAP_CREATE) || defined(WIN32) */
846 error("invalid packet count %s", optarg
);
850 Cflag
= atoi(optarg
) * 1000000;
852 error("invalid file size %s", optarg
);
872 #ifndef HAVE_LIBCRYPTO
873 warning("crypto code not compiled in");
875 gndo
->ndo_espsecret
= optarg
;
887 Gflag
= atoi(optarg
);
889 error("invalid number of seconds %s", optarg
);
891 /* We will create one file initially. */
894 /* Grab the current time for rotation use. */
895 if ((Gflag_time
= time(NULL
)) == (time_t)-1) {
896 error("main: can't get current time: %s",
897 pcap_strerror(errno
));
910 if (optarg
[0] == '0' && optarg
[1] == 0)
911 error("Invalid adapter index");
913 #ifdef HAVE_PCAP_FINDALLDEVS
915 * If the argument is a number, treat it as
916 * an index into the list of adapters, as
917 * printed by "tcpdump -D".
919 * This should be OK on UNIX systems, as interfaces
920 * shouldn't have names that begin with digits.
921 * It can be useful on Windows, where more than
922 * one interface can have the same name.
924 devnum
= strtol(optarg
, &end
, 10);
925 if (optarg
!= end
&& *end
== '\0') {
927 error("Invalid adapter index");
929 if (pcap_findalldevs(&devpointer
, ebuf
) < 0)
933 * Look for the devnum-th entry
934 * in the list of devices
938 i
< devnum
-1 && devpointer
!= NULL
;
939 i
++, devpointer
= devpointer
->next
)
941 if (devpointer
== NULL
)
942 error("Invalid adapter index");
944 device
= devpointer
->name
;
947 #endif /* HAVE_PCAP_FINDALLDEVS */
951 #ifdef HAVE_PCAP_CREATE
955 #endif /* HAVE_PCAP_CREATE */
957 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
959 jflag
= pcap_tstamp_type_name_to_val(optarg
);
961 error("invalid time stamp type %s", optarg
);
972 * _IOLBF is the same as _IOFBF in Microsoft's C
973 * libraries; the only alternative they offer
976 * XXX - this should really be checking for MSVC++,
977 * not WIN32, if, for example, MinGW has its own
978 * C library that is more UNIX-compatible.
980 setvbuf(stdout
, NULL
, _IONBF
, 0);
982 #ifdef HAVE_SETLINEBUF
985 setvbuf(stdout
, NULL
, _IOLBF
, 0);
996 if (smiLoadModule(optarg
) == 0) {
997 error("could not load MIB module %s", optarg
);
1001 (void)fprintf(stderr
, "%s: ignoring option `-m %s' ",
1002 program_name
, optarg
);
1003 (void)fprintf(stderr
, "(no libsmi support)\n");
1008 /* TCP-MD5 shared secret */
1009 #ifndef HAVE_LIBCRYPTO
1010 warning("crypto code not compiled in");
1033 ++suppress_default_print
;
1036 #ifdef HAVE_PCAP_SETDIRECTION
1038 if (strcasecmp(optarg
, "in") == 0)
1040 else if (strcasecmp(optarg
, "out") == 0)
1042 else if (strcasecmp(optarg
, "inout") == 0)
1043 Qflag
= PCAP_D_INOUT
;
1045 error("unknown capture direction `%s'", optarg
);
1047 #endif /* HAVE_PCAP_SETDIRECTION */
1058 snaplen
= strtol(optarg
, &end
, 0);
1059 if (optarg
== end
|| *end
!= '\0'
1060 || snaplen
< 0 || snaplen
> MAXIMUM_SNAPLEN
)
1061 error("invalid snaplen %s", optarg
);
1062 else if (snaplen
== 0)
1063 snaplen
= MAXIMUM_SNAPLEN
;
1075 if (strcasecmp(optarg
, "vat") == 0)
1076 packettype
= PT_VAT
;
1077 else if (strcasecmp(optarg
, "wb") == 0)
1079 else if (strcasecmp(optarg
, "rpc") == 0)
1080 packettype
= PT_RPC
;
1081 else if (strcasecmp(optarg
, "rtp") == 0)
1082 packettype
= PT_RTP
;
1083 else if (strcasecmp(optarg
, "rtcp") == 0)
1084 packettype
= PT_RTCP
;
1085 else if (strcasecmp(optarg
, "snmp") == 0)
1086 packettype
= PT_SNMP
;
1087 else if (strcasecmp(optarg
, "cnfp") == 0)
1088 packettype
= PT_CNFP
;
1089 else if (strcasecmp(optarg
, "tftp") == 0)
1090 packettype
= PT_TFTP
;
1091 else if (strcasecmp(optarg
, "aodv") == 0)
1092 packettype
= PT_AODV
;
1093 else if (strcasecmp(optarg
, "carp") == 0)
1094 packettype
= PT_CARP
;
1095 else if (strcasecmp(optarg
, "radius") == 0)
1096 packettype
= PT_RADIUS
;
1097 else if (strcasecmp(optarg
, "zmtp1") == 0)
1098 packettype
= PT_ZMTP1
;
1099 else if (strcasecmp(optarg
, "vxlan") == 0)
1100 packettype
= PT_VXLAN
;
1101 else if (strcasecmp(optarg
, "pgm") == 0)
1102 packettype
= PT_PGM
;
1103 else if (strcasecmp(optarg
, "pgm_zmtp1") == 0)
1104 packettype
= PT_PGM_ZMTP1
;
1105 else if (strcasecmp(optarg
, "lmp") == 0)
1106 packettype
= PT_LMP
;
1108 error("unknown packet type `%s'", optarg
);
1115 #ifdef HAVE_PCAP_DUMP_FLUSH
1134 Wflag
= atoi(optarg
);
1136 error("invalid number of output files %s", optarg
);
1137 WflagChars
= getWflagChars(Wflag
);
1142 ++suppress_default_print
;
1147 ++suppress_default_print
;
1151 gndo
->ndo_dltname
= optarg
;
1153 pcap_datalink_name_to_val(gndo
->ndo_dltname
);
1154 if (gndo
->ndo_dlt
< 0)
1155 error("invalid data link type %s", gndo
->ndo_dltname
);
1158 #if defined(HAVE_PCAP_DEBUG) || defined(HAVE_YYDEBUG)
1161 /* Undocumented flag */
1162 #ifdef HAVE_PCAP_DEBUG
1163 extern int pcap_debug
;
1173 zflag
= strdup(optarg
);
1177 username
= strdup(optarg
);
1185 #ifdef HAVE_PCAP_FINDALLDEVS
1187 show_devices_and_exit();
1192 case 0: /* Default */
1193 case 4: /* Default + Date*/
1194 thiszone
= gmt2local(0);
1197 case 1: /* No time stamp */
1198 case 2: /* Unix timeval style */
1199 case 3: /* Microseconds since previous packet */
1200 case 5: /* Microseconds since first packet */
1203 default: /* Not supported */
1204 error("only -t, -tt, -ttt, -tttt and -ttttt are supported");
1208 if (fflag
!= 0 && (VFileName
!= NULL
|| RFileName
!= NULL
))
1209 error("-f can not be used with -V or -r");
1211 if (VFileName
!= NULL
&& RFileName
!= NULL
)
1212 error("-V and -r are mutually exclusive.");
1215 /* if run as root, prepare for chrooting */
1216 if (getuid() == 0 || geteuid() == 0) {
1217 /* future extensibility for cmd-line arguments */
1219 chroot_dir
= WITH_CHROOT
;
1224 /* if run as root, prepare for dropping root privileges */
1225 if (getuid() == 0 || geteuid() == 0) {
1226 /* Run with '-Z root' to restore old behaviour */
1228 username
= WITH_USER
;
1232 if (RFileName
!= NULL
|| VFileName
!= NULL
) {
1234 * If RFileName is non-null, it's the pathname of a
1235 * savefile to read. If VFileName is non-null, it's
1236 * the pathname of a file containing a list of pathnames
1237 * (one per line) of savefiles to read.
1239 * In either case, we're reading a savefile, not doing
1244 * We don't need network access, so relinquish any set-UID
1245 * or set-GID privileges we have (if any).
1247 * We do *not* want set-UID privileges when opening a
1248 * trace file, as that might let the user read other
1249 * people's trace files (especially if we're set-UID
1252 if (setgid(getgid()) != 0 || setuid(getuid()) != 0 )
1253 fprintf(stderr
, "Warning: setgid/setuid failed !\n");
1255 if (VFileName
!= NULL
) {
1256 if (VFileName
[0] == '-' && VFileName
[1] == '\0')
1259 VFile
= fopen(VFileName
, "r");
1262 error("Unable to open file: %s\n", strerror(errno
));
1264 ret
= get_next_file(VFile
, VFileLine
);
1266 error("Nothing in %s\n", VFileName
);
1267 RFileName
= VFileLine
;
1270 pd
= pcap_open_offline(RFileName
, ebuf
);
1273 dlt
= pcap_datalink(pd
);
1274 dlt_name
= pcap_datalink_val_to_name(dlt
);
1275 if (dlt_name
== NULL
) {
1276 fprintf(stderr
, "reading from file %s, link-type %u\n",
1280 "reading from file %s, link-type %s (%s)\n",
1281 RFileName
, dlt_name
,
1282 pcap_datalink_val_to_description(dlt
));
1286 * We're doing a live capture.
1288 if (device
== NULL
) {
1289 device
= pcap_lookupdev(ebuf
);
1295 * Print a message to the standard error on Windows.
1296 * XXX - why do it here, with a different message?
1298 if(strlen(device
) == 1) /* we assume that an ASCII string is always longer than 1 char */
1299 { /* a Unicode string has a \0 as second byte (so strlen() is 1) */
1300 fprintf(stderr
, "%s: listening on %ws\n", program_name
, device
);
1304 fprintf(stderr
, "%s: listening on %s\n", program_name
, device
);
1309 #ifdef HAVE_PCAP_CREATE
1310 pd
= pcap_create(device
, ebuf
);
1313 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
1315 show_tstamp_types_and_exit(device
, pd
);
1318 * Is this an interface that supports monitor mode?
1320 if (pcap_can_set_rfmon(pd
) == 1)
1321 supports_monitor_mode
= 1;
1323 supports_monitor_mode
= 0;
1324 status
= pcap_set_snaplen(pd
, snaplen
);
1326 error("%s: Can't set snapshot length: %s",
1327 device
, pcap_statustostr(status
));
1328 status
= pcap_set_promisc(pd
, !pflag
);
1330 error("%s: Can't set promiscuous mode: %s",
1331 device
, pcap_statustostr(status
));
1333 status
= pcap_set_rfmon(pd
, 1);
1335 error("%s: Can't set monitor mode: %s",
1336 device
, pcap_statustostr(status
));
1338 status
= pcap_set_timeout(pd
, 1000);
1340 error("%s: pcap_set_timeout failed: %s",
1341 device
, pcap_statustostr(status
));
1343 status
= pcap_set_buffer_size(pd
, Bflag
);
1345 error("%s: Can't set buffer size: %s",
1346 device
, pcap_statustostr(status
));
1348 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
1350 status
= pcap_set_tstamp_type(pd
, jflag
);
1352 error("%s: Can't set time stamp type: %s",
1353 device
, pcap_statustostr(status
));
1356 status
= pcap_activate(pd
);
1359 * pcap_activate() failed.
1361 cp
= pcap_geterr(pd
);
1362 if (status
== PCAP_ERROR
)
1364 else if ((status
== PCAP_ERROR_NO_SUCH_DEVICE
||
1365 status
== PCAP_ERROR_PERM_DENIED
) &&
1367 error("%s: %s\n(%s)", device
,
1368 pcap_statustostr(status
), cp
);
1370 error("%s: %s", device
,
1371 pcap_statustostr(status
));
1372 } else if (status
> 0) {
1374 * pcap_activate() succeeded, but it's warning us
1375 * of a problem it had.
1377 cp
= pcap_geterr(pd
);
1378 if (status
== PCAP_WARNING
)
1380 else if (status
== PCAP_WARNING_PROMISC_NOTSUP
&&
1382 warning("%s: %s\n(%s)", device
,
1383 pcap_statustostr(status
), cp
);
1385 warning("%s: %s", device
,
1386 pcap_statustostr(status
));
1388 #ifdef HAVE_PCAP_SETDIRECTION
1390 status
= pcap_setdirection(pd
, Qflag
);
1392 error("%s: pcap_setdirection() failed: %s",
1393 device
, pcap_geterr(pd
));
1395 #endif /* HAVE_PCAP_SETDIRECTION */
1398 pd
= pcap_open_live(device
, snaplen
, !pflag
, 1000, ebuf
);
1402 warning("%s", ebuf
);
1403 #endif /* HAVE_PCAP_CREATE */
1405 * Let user own process after socket has been opened.
1408 if (setgid(getgid()) != 0 || setuid(getuid()) != 0)
1409 fprintf(stderr
, "Warning: setgid/setuid failed !\n");
1411 #if !defined(HAVE_PCAP_CREATE) && defined(WIN32)
1413 if(pcap_setbuff(pd
, Bflag
)==-1){
1414 error("%s", pcap_geterr(pd
));
1416 #endif /* !defined(HAVE_PCAP_CREATE) && defined(WIN32) */
1418 show_dlts_and_exit(device
, pd
);
1419 if (gndo
->ndo_dlt
>= 0) {
1420 #ifdef HAVE_PCAP_SET_DATALINK
1421 if (pcap_set_datalink(pd
, gndo
->ndo_dlt
) < 0)
1422 error("%s", pcap_geterr(pd
));
1425 * We don't actually support changing the
1426 * data link type, so we only let them
1427 * set it to what it already is.
1429 if (gndo
->ndo_dlt
!= pcap_datalink(pd
)) {
1430 error("%s is not one of the DLTs supported by this device\n",
1434 (void)fprintf(stderr
, "%s: data link type %s\n",
1435 program_name
, gndo
->ndo_dltname
);
1436 (void)fflush(stderr
);
1438 i
= pcap_snapshot(pd
);
1440 warning("snaplen raised from %d to %d", snaplen
, i
);
1444 if (pcap_lookupnet(device
, &localnet
, &netmask
, ebuf
) < 0) {
1445 warning("foreign (-f) flag used but: %s", ebuf
);
1451 cmdbuf
= read_infile(infile
);
1453 cmdbuf
= copy_argv(&argv
[optind
]);
1455 if (pcap_compile(pd
, &fcode
, cmdbuf
, Oflag
, netmask
) < 0)
1456 error("%s", pcap_geterr(pd
));
1458 bpf_dump(&fcode
, dflag
);
1463 init_addrtoname(localnet
, netmask
);
1467 (void)setsignal(SIGPIPE
, cleanup
);
1468 (void)setsignal(SIGTERM
, cleanup
);
1469 (void)setsignal(SIGINT
, cleanup
);
1471 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
1472 (void)setsignal(SIGCHLD
, child_cleanup
);
1474 /* Cooperate with nohup(1) */
1476 if ((oldhandler
= setsignal(SIGHUP
, cleanup
)) != SIG_DFL
)
1477 (void)setsignal(SIGHUP
, oldhandler
);
1482 * If a user name was specified with "-Z", attempt to switch to
1483 * that user's UID. This would probably be used with sudo,
1484 * to allow tcpdump to be run in a special restricted
1485 * account (if you just want to allow users to open capture
1486 * devices, and can't just give users that permission,
1487 * you'd make tcpdump set-UID or set-GID).
1489 * Tcpdump doesn't necessarily write only to one savefile;
1490 * the general only way to allow a -Z instance to write to
1491 * savefiles as the user under whose UID it's run, rather
1492 * than as the user specified with -Z, would thus be to switch
1493 * to the original user ID before opening a capture file and
1494 * then switch back to the -Z user ID after opening the savefile.
1495 * Switching to the -Z user ID only after opening the first
1496 * savefile doesn't handle the general case.
1499 #ifdef HAVE_CAP_NG_H
1500 /* We are running as root and we will be writing to savefile */
1501 if ((getuid() == 0 || geteuid() == 0) && WFileName
) {
1503 /* Drop all capabilities from effective set */
1504 capng_clear(CAPNG_EFFECTIVE
);
1505 /* Add capabilities we will need*/
1506 capng_update(CAPNG_ADD
, CAPNG_PERMITTED
, CAP_SETUID
);
1507 capng_update(CAPNG_ADD
, CAPNG_PERMITTED
, CAP_SETGID
);
1508 capng_update(CAPNG_ADD
, CAPNG_PERMITTED
, CAP_DAC_OVERRIDE
);
1510 capng_update(CAPNG_ADD
, CAPNG_EFFECTIVE
, CAP_SETUID
);
1511 capng_update(CAPNG_ADD
, CAPNG_EFFECTIVE
, CAP_SETGID
);
1512 capng_update(CAPNG_ADD
, CAPNG_EFFECTIVE
, CAP_DAC_OVERRIDE
);
1514 capng_apply(CAPNG_SELECT_BOTH
);
1517 #endif /* HAVE_CAP_NG_H */
1519 if (getuid() == 0 || geteuid() == 0) {
1520 if (username
|| chroot_dir
)
1521 droproot(username
, chroot_dir
);
1526 if (pcap_setfilter(pd
, &fcode
) < 0)
1527 error("%s", pcap_geterr(pd
));
1530 /* Do not exceed the default PATH_MAX for files. */
1531 dumpinfo
.CurrentFileName
= (char *)malloc(PATH_MAX
+ 1);
1533 if (dumpinfo
.CurrentFileName
== NULL
)
1534 error("malloc of dumpinfo.CurrentFileName");
1536 /* We do not need numbering for dumpfiles if Cflag isn't set. */
1538 MakeFilename(dumpinfo
.CurrentFileName
, WFileName
, 0, WflagChars
);
1540 MakeFilename(dumpinfo
.CurrentFileName
, WFileName
, 0, 0);
1542 p
= pcap_dump_open(pd
, dumpinfo
.CurrentFileName
);
1543 #ifdef HAVE_CAP_NG_H
1544 /* Give up capabilities, clear Effective set */
1545 capng_clear(CAPNG_EFFECTIVE
);
1548 error("%s", pcap_geterr(pd
));
1549 if (Cflag
!= 0 || Gflag
!= 0) {
1550 callback
= dump_packet_and_trunc
;
1551 dumpinfo
.WFileName
= WFileName
;
1554 pcap_userdata
= (u_char
*)&dumpinfo
;
1556 callback
= dump_packet
;
1557 pcap_userdata
= (u_char
*)p
;
1559 #ifdef HAVE_PCAP_DUMP_FLUSH
1564 type
= pcap_datalink(pd
);
1565 printinfo
= get_print_info(type
);
1566 callback
= print_packet
;
1567 pcap_userdata
= (u_char
*)&printinfo
;
1570 #ifdef SIGNAL_REQ_INFO
1572 * We can't get statistics when reading from a file rather
1573 * than capturing from a device.
1575 if (RFileName
== NULL
)
1576 (void)setsignal(SIGNAL_REQ_INFO
, requestinfo
);
1579 if (vflag
> 0 && WFileName
) {
1581 * When capturing to a file, "-v" means tcpdump should,
1582 * every 10 secodns, "v"erbosely report the number of
1585 #ifdef USE_WIN32_MM_TIMER
1586 /* call verbose_stats_dump() each 1000 +/-100msec */
1587 timer_id
= timeSetEvent(1000, 100, verbose_stats_dump
, 0, TIME_PERIODIC
);
1588 setvbuf(stderr
, NULL
, _IONBF
, 0);
1589 #elif defined(HAVE_ALARM)
1590 (void)setsignal(SIGALRM
, verbose_stats_dump
);
1596 if (RFileName
== NULL
) {
1598 * Live capture (if -V was specified, we set RFileName
1599 * to a file from the -V file). Print a message to
1600 * the standard error on UN*X.
1602 if (!vflag
&& !WFileName
) {
1603 (void)fprintf(stderr
,
1604 "%s: verbose output suppressed, use -v or -vv for full protocol decode\n",
1607 (void)fprintf(stderr
, "%s: ", program_name
);
1608 dlt
= pcap_datalink(pd
);
1609 dlt_name
= pcap_datalink_val_to_name(dlt
);
1610 if (dlt_name
== NULL
) {
1611 (void)fprintf(stderr
, "listening on %s, link-type %u, capture size %u bytes\n",
1612 device
, dlt
, snaplen
);
1614 (void)fprintf(stderr
, "listening on %s, link-type %s (%s), capture size %u bytes\n",
1616 pcap_datalink_val_to_description(dlt
), snaplen
);
1618 (void)fflush(stderr
);
1622 status
= pcap_loop(pd
, cnt
, callback
, pcap_userdata
);
1623 if (WFileName
== NULL
) {
1625 * We're printing packets. Flush the printed output,
1626 * so it doesn't get intermingled with error output.
1630 * We got interrupted, so perhaps we didn't
1631 * manage to finish a line we were printing.
1632 * Print an extra newline, just in case.
1636 (void)fflush(stdout
);
1640 * We got interrupted. If we are reading multiple
1641 * files (via -V) set these so that we stop.
1650 (void)fprintf(stderr
, "%s: pcap_loop: %s\n",
1651 program_name
, pcap_geterr(pd
));
1653 if (RFileName
== NULL
) {
1655 * We're doing a live capture. Report the capture
1661 if (VFileName
!= NULL
) {
1662 ret
= get_next_file(VFile
, VFileLine
);
1664 RFileName
= VFileLine
;
1665 pd
= pcap_open_offline(RFileName
, ebuf
);
1668 new_dlt
= pcap_datalink(pd
);
1669 if (WFileName
&& new_dlt
!= dlt
)
1670 error("%s: new dlt does not match original", RFileName
);
1671 printinfo
= get_print_info(new_dlt
);
1672 dlt_name
= pcap_datalink_val_to_name(new_dlt
);
1673 if (dlt_name
== NULL
) {
1674 fprintf(stderr
, "reading from file %s, link-type %u\n",
1675 RFileName
, new_dlt
);
1678 "reading from file %s, link-type %s (%s)\n",
1679 RFileName
, dlt_name
,
1680 pcap_datalink_val_to_description(new_dlt
));
1682 if (pcap_compile(pd
, &fcode
, cmdbuf
, Oflag
, netmask
) < 0)
1683 error("%s", pcap_geterr(pd
));
1684 if (pcap_setfilter(pd
, &fcode
) < 0)
1685 error("%s", pcap_geterr(pd
));
1689 while (ret
!= NULL
);
1692 exit(status
== -1 ? 1 : 0);
1695 /* make a clean exit on interrupts */
1697 cleanup(int signo _U_
)
1699 #ifdef USE_WIN32_MM_TIMER
1701 timeKillEvent(timer_id
);
1703 #elif defined(HAVE_ALARM)
1707 #ifdef HAVE_PCAP_BREAKLOOP
1709 * We have "pcap_breakloop()"; use it, so that we do as little
1710 * as possible in the signal handler (it's probably not safe
1711 * to do anything with standard I/O streams in a signal handler -
1712 * the ANSI C standard doesn't say it is).
1717 * We don't have "pcap_breakloop()"; this isn't safe, but
1718 * it's the best we can do. Print the summary if we're
1719 * not reading from a savefile - i.e., if we're doing a
1720 * live capture - and exit.
1722 if (pd
!= NULL
&& pcap_file(pd
) == NULL
) {
1724 * We got interrupted, so perhaps we didn't
1725 * manage to finish a line we were printing.
1726 * Print an extra newline, just in case.
1729 (void)fflush(stdout
);
1737 On windows, we do not use a fork, so we do not care less about
1738 waiting a child processes to die
1740 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
1742 child_cleanup(int signo _U_
)
1746 #endif /* HAVE_FORK && HAVE_VFORK */
1749 info(register int verbose
)
1751 struct pcap_stat stat
;
1754 * Older versions of libpcap didn't set ps_ifdrop on some
1755 * platforms; initialize it to 0 to handle that.
1758 if (pcap_stats(pd
, &stat
) < 0) {
1759 (void)fprintf(stderr
, "pcap_stats: %s\n", pcap_geterr(pd
));
1765 fprintf(stderr
, "%s: ", program_name
);
1767 (void)fprintf(stderr
, "%u packet%s captured", packets_captured
,
1768 PLURAL_SUFFIX(packets_captured
));
1770 fputs(", ", stderr
);
1773 (void)fprintf(stderr
, "%u packet%s received by filter", stat
.ps_recv
,
1774 PLURAL_SUFFIX(stat
.ps_recv
));
1776 fputs(", ", stderr
);
1779 (void)fprintf(stderr
, "%u packet%s dropped by kernel", stat
.ps_drop
,
1780 PLURAL_SUFFIX(stat
.ps_drop
));
1781 if (stat
.ps_ifdrop
!= 0) {
1783 fputs(", ", stderr
);
1786 (void)fprintf(stderr
, "%u packet%s dropped by interface\n",
1787 stat
.ps_ifdrop
, PLURAL_SUFFIX(stat
.ps_ifdrop
));
1793 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
1795 compress_savefile(const char *filename
)
1804 * Set to lowest priority so that this doesn't disturb the capture
1807 setpriority(PRIO_PROCESS
, 0, NZERO
- 1);
1809 setpriority(PRIO_PROCESS
, 0, 19);
1811 if (execlp(zflag
, zflag
, filename
, (char *)NULL
) == -1)
1813 "compress_savefile:execlp(%s, %s): %s\n",
1823 #else /* HAVE_FORK && HAVE_VFORK */
1825 compress_savefile(const char *filename
)
1828 "compress_savefile failed. Functionality not implemented under your system\n");
1830 #endif /* HAVE_FORK && HAVE_VFORK */
1833 dump_packet_and_trunc(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
1835 struct dump_info
*dump_info
;
1841 dump_info
= (struct dump_info
*)user
;
1844 * XXX - this won't force the file to rotate on the specified time
1845 * boundary, but it will rotate on the first packet received after the
1846 * specified Gflag number of seconds. Note: if a Gflag time boundary
1847 * and a Cflag size boundary coincide, the time rotation will occur
1848 * first thereby cancelling the Cflag boundary (since the file should
1852 /* Check if it is time to rotate */
1855 /* Get the current time */
1856 if ((t
= time(NULL
)) == (time_t)-1) {
1857 error("dump_and_trunc_packet: can't get current_time: %s",
1858 pcap_strerror(errno
));
1862 /* If the time is greater than the specified window, rotate */
1863 if (t
- Gflag_time
>= Gflag
) {
1864 /* Update the Gflag_time */
1866 /* Update Gflag_count */
1869 * Close the current file and open a new one.
1871 pcap_dump_close(dump_info
->p
);
1874 * Compress the file we just closed, if the user asked for it
1877 compress_savefile(dump_info
->CurrentFileName
);
1880 * Check to see if we've exceeded the Wflag (when
1883 if (Cflag
== 0 && Wflag
> 0 && Gflag_count
>= Wflag
) {
1884 (void)fprintf(stderr
, "Maximum file limit reached: %d\n",
1889 if (dump_info
->CurrentFileName
!= NULL
)
1890 free(dump_info
->CurrentFileName
);
1891 /* Allocate space for max filename + \0. */
1892 dump_info
->CurrentFileName
= (char *)malloc(PATH_MAX
+ 1);
1893 if (dump_info
->CurrentFileName
== NULL
)
1894 error("dump_packet_and_trunc: malloc");
1896 * Gflag was set otherwise we wouldn't be here. Reset the count
1897 * so multiple files would end with 1,2,3 in the filename.
1898 * The counting is handled with the -C flow after this.
1903 * This is always the first file in the Cflag
1905 * We also don't need numbering if Cflag is not set.
1908 MakeFilename(dump_info
->CurrentFileName
, dump_info
->WFileName
, 0,
1911 MakeFilename(dump_info
->CurrentFileName
, dump_info
->WFileName
, 0, 0);
1913 #ifdef HAVE_CAP_NG_H
1914 capng_update(CAPNG_ADD
, CAPNG_EFFECTIVE
, CAP_DAC_OVERRIDE
);
1915 capng_apply(CAPNG_EFFECTIVE
);
1916 #endif /* HAVE_CAP_NG_H */
1917 dump_info
->p
= pcap_dump_open(dump_info
->pd
, dump_info
->CurrentFileName
);
1918 #ifdef HAVE_CAP_NG_H
1919 capng_update(CAPNG_DROP
, CAPNG_EFFECTIVE
, CAP_DAC_OVERRIDE
);
1920 capng_apply(CAPNG_EFFECTIVE
);
1921 #endif /* HAVE_CAP_NG_H */
1922 if (dump_info
->p
== NULL
)
1923 error("%s", pcap_geterr(pd
));
1928 * XXX - this won't prevent capture files from getting
1929 * larger than Cflag - the last packet written to the
1930 * file could put it over Cflag.
1932 if (Cflag
!= 0 && pcap_dump_ftell(dump_info
->p
) > Cflag
) {
1934 * Close the current file and open a new one.
1936 pcap_dump_close(dump_info
->p
);
1939 * Compress the file we just closed, if the user asked for it
1942 compress_savefile(dump_info
->CurrentFileName
);
1946 if (Cflag_count
>= Wflag
)
1949 if (dump_info
->CurrentFileName
!= NULL
)
1950 free(dump_info
->CurrentFileName
);
1951 dump_info
->CurrentFileName
= (char *)malloc(PATH_MAX
+ 1);
1952 if (dump_info
->CurrentFileName
== NULL
)
1953 error("dump_packet_and_trunc: malloc");
1954 MakeFilename(dump_info
->CurrentFileName
, dump_info
->WFileName
, Cflag_count
, WflagChars
);
1955 dump_info
->p
= pcap_dump_open(dump_info
->pd
, dump_info
->CurrentFileName
);
1956 if (dump_info
->p
== NULL
)
1957 error("%s", pcap_geterr(pd
));
1960 pcap_dump((u_char
*)dump_info
->p
, h
, sp
);
1961 #ifdef HAVE_PCAP_DUMP_FLUSH
1963 pcap_dump_flush(dump_info
->p
);
1972 dump_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
1978 pcap_dump(user
, h
, sp
);
1979 #ifdef HAVE_PCAP_DUMP_FLUSH
1981 pcap_dump_flush((pcap_dumper_t
*)user
);
1990 print_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
1992 struct print_info
*print_info
;
1994 netdissect_options
*ndo
;
2001 print_info
= (struct print_info
*)user
;
2002 ndo
= print_info
->ndo
;
2005 * Some printers want to check that they're not walking off the
2006 * end of the packet.
2007 * Rather than pass it all the way down, we set this global.
2009 ndo
->ndo_snapend
= sp
+ h
->caplen
;
2011 if(print_info
->ndo_type
) {
2012 hdrlen
= (*print_info
->p
.ndo_printer
)(print_info
->ndo
, h
, sp
);
2014 hdrlen
= (*print_info
->p
.printer
)(h
, sp
);
2017 if (ndo
->ndo_Xflag
) {
2019 * Print the raw packet data in hex and ASCII.
2021 if (ndo
->ndo_Xflag
> 1) {
2023 * Include the link-layer header.
2025 hex_and_ascii_print(ndo
, "\n\t", sp
, h
->caplen
);
2028 * Don't include the link-layer header - and if
2029 * we have nothing past the link-layer header,
2032 if (h
->caplen
> hdrlen
)
2033 hex_and_ascii_print(ndo
, "\n\t", sp
+ hdrlen
,
2034 h
->caplen
- hdrlen
);
2036 } else if (ndo
->ndo_xflag
) {
2038 * Print the raw packet data in hex.
2040 if (ndo
->ndo_xflag
> 1) {
2042 * Include the link-layer header.
2044 hex_print(ndo
, "\n\t", sp
, h
->caplen
);
2047 * Don't include the link-layer header - and if
2048 * we have nothing past the link-layer header,
2051 if (h
->caplen
> hdrlen
)
2052 hex_print(ndo
, "\n\t", sp
+ hdrlen
,
2053 h
->caplen
- hdrlen
);
2055 } else if (ndo
->ndo_Aflag
) {
2057 * Print the raw packet data in ASCII.
2059 if (ndo
->ndo_Aflag
> 1) {
2061 * Include the link-layer header.
2063 ascii_print(sp
, h
->caplen
);
2066 * Don't include the link-layer header - and if
2067 * we have nothing past the link-layer header,
2070 if (h
->caplen
> hdrlen
)
2071 ascii_print(sp
+ hdrlen
, h
->caplen
- hdrlen
);
2084 * XXX - there should really be libpcap calls to get the version
2085 * number as a string (the string would be generated from #defines
2086 * at run time, so that it's not generated from string constants
2087 * in the library, as, on many UNIX systems, those constants would
2088 * be statically linked into the application executable image, and
2089 * would thus reflect the version of libpcap on the system on
2090 * which the application was *linked*, not the system on which it's
2093 * That routine should be documented, unlike the "version[]"
2094 * string, so that UNIX vendors providing their own libpcaps
2095 * don't omit it (as a couple of vendors have...).
2097 * Packet.dll should perhaps also export a routine to return the
2098 * version number of the Packet.dll code, to supply the
2099 * "Wpcap_version" information on Windows.
2101 char WDversion
[]="current-git.tcpdump.org";
2102 #if !defined(HAVE_GENERATED_VERSION)
2103 char version
[]="current-git.tcpdump.org";
2105 char pcap_version
[]="current-git.tcpdump.org";
2106 char Wpcap_version
[]="3.1";
2110 * By default, print the specified data out in hex and ASCII.
2113 ndo_default_print(netdissect_options
*ndo
, const u_char
*bp
, u_int length
)
2115 hex_and_ascii_print(ndo
, "\n\t", bp
, length
); /* pass on lf and identation string */
2119 default_print(const u_char
*bp
, u_int length
)
2121 ndo_default_print(gndo
, bp
, length
);
2124 #ifdef SIGNAL_REQ_INFO
2125 RETSIGTYPE
requestinfo(int signo _U_
)
2135 * Called once each second in verbose mode while dumping to file
2137 #ifdef USE_WIN32_MM_TIMER
2138 void CALLBACK
verbose_stats_dump (UINT timer_id _U_
, UINT msg _U_
, DWORD_PTR arg _U_
,
2139 DWORD_PTR dw1 _U_
, DWORD_PTR dw2 _U_
)
2141 struct pcap_stat stat
;
2143 if (infodelay
== 0 && pcap_stats(pd
, &stat
) >= 0)
2144 fprintf(stderr
, "Got %u\r", packets_captured
);
2146 #elif defined(HAVE_ALARM)
2147 static void verbose_stats_dump(int sig _U_
)
2149 struct pcap_stat stat
;
2151 if (infodelay
== 0 && pcap_stats(pd
, &stat
) >= 0)
2152 fprintf(stderr
, "Got %u\r", packets_captured
);
2160 extern char version
[];
2161 #ifndef HAVE_PCAP_LIB_VERSION
2162 #if defined(WIN32) || defined(HAVE_PCAP_VERSION)
2163 extern char pcap_version
[];
2164 #else /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
2165 static char pcap_version
[] = "unknown";
2166 #endif /* defined(WIN32) || defined(HAVE_PCAP_VERSION) */
2167 #endif /* HAVE_PCAP_LIB_VERSION */
2169 #ifdef HAVE_PCAP_LIB_VERSION
2171 (void)fprintf(stderr
, "%s version %s, based on tcpdump version %s\n", program_name
, WDversion
, version
);
2173 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
2175 (void)fprintf(stderr
, "%s\n",pcap_lib_version());
2176 #else /* HAVE_PCAP_LIB_VERSION */
2178 (void)fprintf(stderr
, "%s version %s, based on tcpdump version %s\n", program_name
, WDversion
, version
);
2179 (void)fprintf(stderr
, "WinPcap version %s, based on libpcap version %s\n",Wpcap_version
, pcap_version
);
2181 (void)fprintf(stderr
, "%s version %s\n", program_name
, version
);
2182 (void)fprintf(stderr
, "libpcap version %s\n", pcap_version
);
2184 #endif /* HAVE_PCAP_LIB_VERSION */
2185 (void)fprintf(stderr
,
2186 "Usage: %s [-aAbd" D_FLAG
"efhH" I_FLAG J_FLAG
"KlLnNOpqRStu" U_FLAG
"vxX]" B_FLAG_USAGE
" [ -c count ]\n", program_name
);
2187 (void)fprintf(stderr
,
2188 "\t\t[ -C file_size ] [ -E algo:secret ] [ -F file ] [ -G seconds ]\n");
2189 (void)fprintf(stderr
,
2190 "\t\t[ -i interface ]" j_FLAG_USAGE
" [ -M secret ]\n");
2191 #ifdef HAVE_PCAP_SETDIRECTION
2192 (void)fprintf(stderr
,
2193 "\t\t[ -Q in|out|inout ]\n");
2195 (void)fprintf(stderr
,
2196 "\t\t[ -r file ] [ -s snaplen ] [ -T type ] [ -V file ] [ -w file ]\n");
2197 (void)fprintf(stderr
,
2198 "\t\t[ -W filecount ] [ -y datalinktype ] [ -z command ]\n");
2199 (void)fprintf(stderr
,
2200 "\t\t[ -Z user ] [ expression ]\n");
2208 ndo_error(netdissect_options
*ndo _U_
, const char *fmt
, ...)
2212 (void)fprintf(stderr
, "%s: ", program_name
);
2214 (void)vfprintf(stderr
, fmt
, ap
);
2218 if (fmt
[-1] != '\n')
2219 (void)fputc('\n', stderr
);
2227 ndo_warning(netdissect_options
*ndo _U_
, const char *fmt
, ...)
2231 (void)fprintf(stderr
, "%s: WARNING: ", program_name
);
2233 (void)vfprintf(stderr
, fmt
, ap
);
2237 if (fmt
[-1] != '\n')
2238 (void)fputc('\n', stderr
);
2243 * c-style: whitesmith