blob: 80aa28207436682382d8267977eafb3a5d35eb86 [file] [log] [blame]
The Android Open Source Project2949f582009-03-03 19:30:46 -08001/*
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
29static const char copyright[] _U_ =
30 "@(#) Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000\n\
31The Regents of the University of California. All rights reserved.\n";
The Android Open Source Project2949f582009-03-03 19:30:46 -080032#endif
33
34/*
Elliott Hughese2e3bd12017-05-15 10:59:29 -070035 * tcpdump - dump traffic on a network
The Android Open Source Project2949f582009-03-03 19:30:46 -080036 *
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.
40 */
41
42#ifdef HAVE_CONFIG_H
43#include "config.h"
44#endif
45
Elliott Hughes892a68b2015-10-19 14:43:53 -070046/*
47 * Mac OS X may ship pcap.h from libpcap 0.6 with a libpcap based on
48 * 0.8. That means it has pcap_findalldevs() but the header doesn't
49 * define pcap_if_t, meaning that we can't actually *use* pcap_findalldevs().
50 */
51#ifdef HAVE_PCAP_FINDALLDEVS
52#ifndef HAVE_PCAP_IF_T
53#undef HAVE_PCAP_FINDALLDEVS
54#endif
55#endif
56
Elliott Hughese2e3bd12017-05-15 10:59:29 -070057#include <netdissect-stdinc.h>
The Android Open Source Project2949f582009-03-03 19:30:46 -080058
Elliott Hughese2e3bd12017-05-15 10:59:29 -070059#include <sys/stat.h>
The Android Open Source Project2949f582009-03-03 19:30:46 -080060
Elliott Hughese2e3bd12017-05-15 10:59:29 -070061#ifdef HAVE_FCNTL_H
62#include <fcntl.h>
The Android Open Source Project2949f582009-03-03 19:30:46 -080063#endif
64
Elliott Hughes892a68b2015-10-19 14:43:53 -070065#ifdef HAVE_LIBCRYPTO
66#include <openssl/crypto.h>
67#endif
68
69#ifdef HAVE_GETOPT_LONG
70#include <getopt.h>
71#else
72#include "getopt_long.h"
73#endif
74/* Capsicum-specific code requires macros from <net/bpf.h>, which will fail
75 * to compile if <pcap.h> has already been included; including the headers
76 * in the opposite order works fine.
77 */
78#ifdef HAVE_CAPSICUM
79#include <sys/capability.h>
80#include <sys/ioccom.h>
81#include <net/bpf.h>
Elliott Hughes892a68b2015-10-19 14:43:53 -070082#include <libgen.h>
83#endif /* HAVE_CAPSICUM */
The Android Open Source Project2949f582009-03-03 19:30:46 -080084#include <pcap.h>
85#include <signal.h>
86#include <stdio.h>
Elliott Hughese2e3bd12017-05-15 10:59:29 -070087#include <stdarg.h>
The Android Open Source Project2949f582009-03-03 19:30:46 -080088#include <stdlib.h>
89#include <string.h>
JP Abgrall53f17a92014-02-12 14:02:41 -080090#include <limits.h>
Elliott Hughese2e3bd12017-05-15 10:59:29 -070091#ifndef _WIN32
JP Abgrall53f17a92014-02-12 14:02:41 -080092#include <sys/wait.h>
93#include <sys/resource.h>
The Android Open Source Project2949f582009-03-03 19:30:46 -080094#include <pwd.h>
95#include <grp.h>
Elliott Hughese2e3bd12017-05-15 10:59:29 -070096#endif /* _WIN32 */
The Android Open Source Project2949f582009-03-03 19:30:46 -080097
Elliott Hughes892a68b2015-10-19 14:43:53 -070098/* capabilities convenience library */
99/* If a code depends on HAVE_LIBCAP_NG, it depends also on HAVE_CAP_NG_H.
100 * If HAVE_CAP_NG_H is not defined, undefine HAVE_LIBCAP_NG.
101 * Thus, the later tests are done only on HAVE_LIBCAP_NG.
102 */
103#ifdef HAVE_LIBCAP_NG
JP Abgrall53f17a92014-02-12 14:02:41 -0800104#ifdef HAVE_CAP_NG_H
105#include <cap-ng.h>
Elliott Hughes892a68b2015-10-19 14:43:53 -0700106#else
107#undef HAVE_LIBCAP_NG
JP Abgrall53f17a92014-02-12 14:02:41 -0800108#endif /* HAVE_CAP_NG_H */
Elliott Hughes892a68b2015-10-19 14:43:53 -0700109#endif /* HAVE_LIBCAP_NG */
JP Abgrall53f17a92014-02-12 14:02:41 -0800110
The Android Open Source Project2949f582009-03-03 19:30:46 -0800111#include "netdissect.h"
112#include "interface.h"
113#include "addrtoname.h"
114#include "machdep.h"
115#include "setsignal.h"
116#include "gmt2local.h"
117#include "pcap-missing.h"
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700118#include "ascii_strcasecmp.h"
119
120#include "print.h"
The Android Open Source Project2949f582009-03-03 19:30:46 -0800121
JP Abgrall53f17a92014-02-12 14:02:41 -0800122#ifndef PATH_MAX
123#define PATH_MAX 1024
124#endif
125
126#ifdef SIGINFO
127#define SIGNAL_REQ_INFO SIGINFO
128#elif SIGUSR1
129#define SIGNAL_REQ_INFO SIGUSR1
130#endif
131
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700132static int Bflag; /* buffer size */
Elliott Hughes9a986422017-12-19 14:49:10 -0800133static long Cflag; /* rotate dump files after this many bytes */
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700134static int Cflag_count; /* Keep track of which file number we're writing */
Elliott Hughes892a68b2015-10-19 14:43:53 -0700135static int Dflag; /* list available devices and exit */
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700136/*
137 * This is exported because, in some versions of libpcap, if libpcap
138 * is built with optimizer debugging code (which is *NOT* the default
139 * configuration!), the library *imports*(!) a variable named dflag,
140 * under the expectation that tcpdump is exporting it, to govern
141 * how much debugging information to print when optimizing
142 * the generated BPF code.
143 *
144 * This is a horrible hack; newer versions of libpcap don't import
145 * dflag but, instead, *if* built with optimizer debugging code,
146 * *export* a routine to set that flag.
147 */
148int dflag; /* print filter code */
149static int Gflag; /* rotate dump files after this many seconds */
150static int Gflag_count; /* number of files created with Gflag rotation */
151static time_t Gflag_time; /* The last time_t the dump file was rotated. */
JP Abgrall53f17a92014-02-12 14:02:41 -0800152static int Lflag; /* list available data link types and exit */
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700153static int Iflag; /* rfmon (monitor) mode */
JP Abgrall53f17a92014-02-12 14:02:41 -0800154#ifdef HAVE_PCAP_SET_TSTAMP_TYPE
155static int Jflag; /* list available time stamp types */
156#endif
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700157static int jflag = -1; /* packet time stamp source */
158static int pflag; /* don't go promiscuous */
JP Abgrall53f17a92014-02-12 14:02:41 -0800159#ifdef HAVE_PCAP_SETDIRECTION
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700160static int Qflag = -1; /* restrict captured packet by send/receive direction */
JP Abgrall53f17a92014-02-12 14:02:41 -0800161#endif
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700162static int Uflag; /* "unbuffered" output of dump files */
163static int Wflag; /* recycle output files after this number of files */
164static int WflagChars;
JP Abgrall53f17a92014-02-12 14:02:41 -0800165static char *zflag = NULL; /* compress each savefile using a specified command (like gzip or bzip2) */
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700166static int immediate_mode;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800167
168static int infodelay;
169static int infoprint;
170
171char *program_name;
172
The Android Open Source Project2949f582009-03-03 19:30:46 -0800173/* Forwards */
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700174static void error(const char *, ...)
175 __attribute__((noreturn))
176#ifdef __ATTRIBUTE___FORMAT_OK
177 __attribute__((format (printf, 1, 2)))
178#endif /* __ATTRIBUTE___FORMAT_OK */
179 ;
180static void warning(const char *, ...)
181#ifdef __ATTRIBUTE___FORMAT_OK
182 __attribute__((format (printf, 1, 2)))
183#endif /* __ATTRIBUTE___FORMAT_OK */
184 ;
185static void exit_tcpdump(int) __attribute__((noreturn));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800186static RETSIGTYPE cleanup(int);
JP Abgrall53f17a92014-02-12 14:02:41 -0800187static RETSIGTYPE child_cleanup(int);
Elliott Hughes892a68b2015-10-19 14:43:53 -0700188static void print_version(void);
189static void print_usage(void);
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700190static void show_tstamp_types_and_exit(pcap_t *, const char *device) __attribute__((noreturn));
191static void show_dlts_and_exit(pcap_t *, const char *device) __attribute__((noreturn));
192#ifdef HAVE_PCAP_FINDALLDEVS
193static void show_devices_and_exit (void) __attribute__((noreturn));
194#endif
The Android Open Source Project2949f582009-03-03 19:30:46 -0800195
196static void print_packet(u_char *, const struct pcap_pkthdr *, const u_char *);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800197static void dump_packet_and_trunc(u_char *, const struct pcap_pkthdr *, const u_char *);
198static void dump_packet(u_char *, const struct pcap_pkthdr *, const u_char *);
199static void droproot(const char *, const char *);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800200
JP Abgrall53f17a92014-02-12 14:02:41 -0800201#ifdef SIGNAL_REQ_INFO
The Android Open Source Project2949f582009-03-03 19:30:46 -0800202RETSIGTYPE requestinfo(int);
203#endif
204
205#if defined(USE_WIN32_MM_TIMER)
206 #include <MMsystem.h>
207 static UINT timer_id;
208 static void CALLBACK verbose_stats_dump(UINT, UINT, DWORD_PTR, DWORD_PTR, DWORD_PTR);
209#elif defined(HAVE_ALARM)
210 static void verbose_stats_dump(int sig);
211#endif
212
213static void info(int);
214static u_int packets_captured;
215
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700216#ifdef HAVE_PCAP_FINDALLDEVS
Elliott Hughes892a68b2015-10-19 14:43:53 -0700217static const struct tok status_flags[] = {
218#ifdef PCAP_IF_UP
219 { PCAP_IF_UP, "Up" },
JP Abgrall53f17a92014-02-12 14:02:41 -0800220#endif
Elliott Hughes892a68b2015-10-19 14:43:53 -0700221#ifdef PCAP_IF_RUNNING
222 { PCAP_IF_RUNNING, "Running" },
JP Abgrall53f17a92014-02-12 14:02:41 -0800223#endif
Elliott Hughes892a68b2015-10-19 14:43:53 -0700224 { PCAP_IF_LOOPBACK, "Loopback" },
225 { 0, NULL }
JP Abgrall53f17a92014-02-12 14:02:41 -0800226};
Elliott Hughes892a68b2015-10-19 14:43:53 -0700227#endif
228
The Android Open Source Project2949f582009-03-03 19:30:46 -0800229static pcap_t *pd;
230
JP Abgrall53f17a92014-02-12 14:02:41 -0800231static int supports_monitor_mode;
232
The Android Open Source Project2949f582009-03-03 19:30:46 -0800233extern int optind;
234extern int opterr;
235extern char *optarg;
236
The Android Open Source Project2949f582009-03-03 19:30:46 -0800237struct dump_info {
238 char *WFileName;
JP Abgrall53f17a92014-02-12 14:02:41 -0800239 char *CurrentFileName;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800240 pcap_t *pd;
241 pcap_dumper_t *p;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700242#ifdef HAVE_CAPSICUM
243 int dirfd;
244#endif
The Android Open Source Project2949f582009-03-03 19:30:46 -0800245};
246
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700247#if defined(HAVE_PCAP_SET_PARSER_DEBUG)
248/*
249 * We have pcap_set_parser_debug() in libpcap; declare it (it's not declared
250 * by any libpcap header, because it's a special hack, only available if
251 * libpcap was configured to include it, and only intended for use by
252 * libpcap developers trying to debug the parser for filter expressions).
253 */
254#ifdef _WIN32
255__declspec(dllimport)
256#else /* _WIN32 */
257extern
258#endif /* _WIN32 */
259void pcap_set_parser_debug(int);
260#elif defined(HAVE_PCAP_DEBUG) || defined(HAVE_YYDEBUG)
261/*
262 * We don't have pcap_set_parser_debug() in libpcap, but we do have
263 * pcap_debug or yydebug. Make a local version of pcap_set_parser_debug()
264 * to set the flag, and define HAVE_PCAP_SET_PARSER_DEBUG.
265 */
266static void
267pcap_set_parser_debug(int value)
268{
269#ifdef HAVE_PCAP_DEBUG
270 extern int pcap_debug;
271
272 pcap_debug = value;
273#else /* HAVE_PCAP_DEBUG */
274 extern int yydebug;
275
276 yydebug = value;
277#endif /* HAVE_PCAP_DEBUG */
278}
279
280#define HAVE_PCAP_SET_PARSER_DEBUG
281#endif
282
283#if defined(HAVE_PCAP_SET_OPTIMIZER_DEBUG)
284/*
285 * We have pcap_set_optimizer_debug() in libpcap; declare it (it's not declared
286 * by any libpcap header, because it's a special hack, only available if
287 * libpcap was configured to include it, and only intended for use by
288 * libpcap developers trying to debug the optimizer for filter expressions).
289 */
290#ifdef _WIN32
291__declspec(dllimport)
292#else /* _WIN32 */
293extern
294#endif /* _WIN32 */
295void pcap_set_optimizer_debug(int);
296#endif
297
298/* VARARGS */
299static void
300error(const char *fmt, ...)
301{
302 va_list ap;
303
304 (void)fprintf(stderr, "%s: ", program_name);
305 va_start(ap, fmt);
306 (void)vfprintf(stderr, fmt, ap);
307 va_end(ap);
308 if (*fmt) {
309 fmt += strlen(fmt);
310 if (fmt[-1] != '\n')
311 (void)fputc('\n', stderr);
312 }
313 exit_tcpdump(1);
314 /* NOTREACHED */
315}
316
317/* VARARGS */
318static void
319warning(const char *fmt, ...)
320{
321 va_list ap;
322
323 (void)fprintf(stderr, "%s: WARNING: ", program_name);
324 va_start(ap, fmt);
325 (void)vfprintf(stderr, fmt, ap);
326 va_end(ap);
327 if (*fmt) {
328 fmt += strlen(fmt);
329 if (fmt[-1] != '\n')
330 (void)fputc('\n', stderr);
331 }
332}
333
334static void
335exit_tcpdump(int status)
336{
337 nd_cleanup();
338 exit(status);
339}
340
JP Abgrall53f17a92014-02-12 14:02:41 -0800341#ifdef HAVE_PCAP_SET_TSTAMP_TYPE
The Android Open Source Project2949f582009-03-03 19:30:46 -0800342static void
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700343show_tstamp_types_and_exit(pcap_t *pc, const char *device)
JP Abgrall53f17a92014-02-12 14:02:41 -0800344{
345 int n_tstamp_types;
346 int *tstamp_types = 0;
347 const char *tstamp_type_name;
348 int i;
349
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700350 n_tstamp_types = pcap_list_tstamp_types(pc, &tstamp_types);
JP Abgrall53f17a92014-02-12 14:02:41 -0800351 if (n_tstamp_types < 0)
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700352 error("%s", pcap_geterr(pc));
JP Abgrall53f17a92014-02-12 14:02:41 -0800353
354 if (n_tstamp_types == 0) {
355 fprintf(stderr, "Time stamp type cannot be set for %s\n",
356 device);
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700357 exit_tcpdump(0);
JP Abgrall53f17a92014-02-12 14:02:41 -0800358 }
359 fprintf(stderr, "Time stamp types for %s (use option -j to set):\n",
360 device);
361 for (i = 0; i < n_tstamp_types; i++) {
362 tstamp_type_name = pcap_tstamp_type_val_to_name(tstamp_types[i]);
363 if (tstamp_type_name != NULL) {
364 (void) fprintf(stderr, " %s (%s)\n", tstamp_type_name,
365 pcap_tstamp_type_val_to_description(tstamp_types[i]));
366 } else {
367 (void) fprintf(stderr, " %d\n", tstamp_types[i]);
368 }
369 }
370 pcap_free_tstamp_types(tstamp_types);
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700371 exit_tcpdump(0);
JP Abgrall53f17a92014-02-12 14:02:41 -0800372}
373#endif
374
375static void
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700376show_dlts_and_exit(pcap_t *pc, const char *device)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800377{
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700378 int n_dlts, i;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800379 int *dlts = 0;
380 const char *dlt_name;
381
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700382 n_dlts = pcap_list_datalinks(pc, &dlts);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800383 if (n_dlts < 0)
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700384 error("%s", pcap_geterr(pc));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800385 else if (n_dlts == 0 || !dlts)
386 error("No data link types.");
387
JP Abgrall53f17a92014-02-12 14:02:41 -0800388 /*
389 * If the interface is known to support monitor mode, indicate
390 * whether these are the data link types available when not in
391 * monitor mode, if -I wasn't specified, or when in monitor mode,
392 * when -I was specified (the link-layer types available in
393 * monitor mode might be different from the ones available when
394 * not in monitor mode).
395 */
396 if (supports_monitor_mode)
397 (void) fprintf(stderr, "Data link types for %s %s (use option -y to set):\n",
398 device,
399 Iflag ? "when in monitor mode" : "when not in monitor mode");
400 else
401 (void) fprintf(stderr, "Data link types for %s (use option -y to set):\n",
402 device);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800403
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700404 for (i = 0; i < n_dlts; i++) {
405 dlt_name = pcap_datalink_val_to_name(dlts[i]);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800406 if (dlt_name != NULL) {
407 (void) fprintf(stderr, " %s (%s)", dlt_name,
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700408 pcap_datalink_val_to_description(dlts[i]));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800409
410 /*
411 * OK, does tcpdump handle that type?
412 */
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700413 if (!has_printer(dlts[i]))
JP Abgrall53f17a92014-02-12 14:02:41 -0800414 (void) fprintf(stderr, " (printing not supported)");
415 fprintf(stderr, "\n");
The Android Open Source Project2949f582009-03-03 19:30:46 -0800416 } else {
JP Abgrall53f17a92014-02-12 14:02:41 -0800417 (void) fprintf(stderr, " DLT %d (printing not supported)\n",
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700418 dlts[i]);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800419 }
420 }
JP Abgrall53f17a92014-02-12 14:02:41 -0800421#ifdef HAVE_PCAP_FREE_DATALINKS
422 pcap_free_datalinks(dlts);
423#endif
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700424 exit_tcpdump(0);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800425}
426
Elliott Hughes892a68b2015-10-19 14:43:53 -0700427#ifdef HAVE_PCAP_FINDALLDEVS
428static void
429show_devices_and_exit (void)
430{
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700431 pcap_if_t *dev, *devlist;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700432 char ebuf[PCAP_ERRBUF_SIZE];
433 int i;
434
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700435 if (pcap_findalldevs(&devlist, ebuf) < 0)
Elliott Hughes892a68b2015-10-19 14:43:53 -0700436 error("%s", ebuf);
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700437 for (i = 0, dev = devlist; dev != NULL; i++, dev = dev->next) {
438 printf("%d.%s", i+1, dev->name);
439 if (dev->description != NULL)
440 printf(" (%s)", dev->description);
441 if (dev->flags != 0)
442 printf(" [%s]", bittok2str(status_flags, "none", dev->flags));
443 printf("\n");
Elliott Hughes892a68b2015-10-19 14:43:53 -0700444 }
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700445 pcap_freealldevs(devlist);
446 exit_tcpdump(0);
Elliott Hughes892a68b2015-10-19 14:43:53 -0700447}
448#endif /* HAVE_PCAP_FINDALLDEVS */
449
450/*
451 * Short options.
452 *
453 * Note that there we use all letters for short options except for g, k,
454 * o, and P, and those are used by other versions of tcpdump, and we should
455 * only use them for the same purposes that the other versions of tcpdump
456 * use them:
457 *
458 * OS X tcpdump uses -g to force non--v output for IP to be on one
459 * line, making it more "g"repable;
460 *
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700461 * OS X tcpdump uses -k to specify that packet comments in pcap-ng files
Elliott Hughes892a68b2015-10-19 14:43:53 -0700462 * should be printed;
463 *
464 * OpenBSD tcpdump uses -o to indicate that OS fingerprinting should be done
465 * for hosts sending TCP SYN packets;
466 *
467 * OS X tcpdump uses -P to indicate that -w should write pcap-ng rather
468 * than pcap files.
469 *
470 * OS X tcpdump also uses -Q to specify expressions that match packet
471 * metadata, including but not limited to the packet direction.
472 * The expression syntax is different from a simple "in|out|inout",
473 * and those expressions aren't accepted by OS X tcpdump, but the
474 * equivalents would be "in" = "dir=in", "out" = "dir=out", and
475 * "inout" = "dir=in or dir=out", and the parser could conceivably
476 * special-case "in", "out", and "inout" as expressions for backwards
477 * compatibility, so all is not (yet) lost.
478 */
479
The Android Open Source Project2949f582009-03-03 19:30:46 -0800480/*
481 * Set up flags that might or might not be supported depending on the
482 * version of libpcap we're using.
483 */
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700484#if defined(HAVE_PCAP_CREATE) || defined(_WIN32)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800485#define B_FLAG "B:"
486#define B_FLAG_USAGE " [ -B size ]"
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700487#else /* defined(HAVE_PCAP_CREATE) || defined(_WIN32) */
The Android Open Source Project2949f582009-03-03 19:30:46 -0800488#define B_FLAG
489#define B_FLAG_USAGE
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700490#endif /* defined(HAVE_PCAP_CREATE) || defined(_WIN32) */
JP Abgrall53f17a92014-02-12 14:02:41 -0800491
492#ifdef HAVE_PCAP_CREATE
493#define I_FLAG "I"
494#else /* HAVE_PCAP_CREATE */
495#define I_FLAG
496#endif /* HAVE_PCAP_CREATE */
497
498#ifdef HAVE_PCAP_SET_TSTAMP_TYPE
499#define j_FLAG "j:"
500#define j_FLAG_USAGE " [ -j tstamptype ]"
501#define J_FLAG "J"
502#else /* PCAP_ERROR_TSTAMP_TYPE_NOTSUP */
503#define j_FLAG
504#define j_FLAG_USAGE
505#define J_FLAG
506#endif /* PCAP_ERROR_TSTAMP_TYPE_NOTSUP */
The Android Open Source Project2949f582009-03-03 19:30:46 -0800507
508#ifdef HAVE_PCAP_FINDALLDEVS
The Android Open Source Project2949f582009-03-03 19:30:46 -0800509#define D_FLAG "D"
510#else
511#define D_FLAG
512#endif
513
514#ifdef HAVE_PCAP_DUMP_FLUSH
515#define U_FLAG "U"
516#else
517#define U_FLAG
518#endif
519
JP Abgrall53f17a92014-02-12 14:02:41 -0800520#ifdef HAVE_PCAP_SETDIRECTION
JP Abgrall53f17a92014-02-12 14:02:41 -0800521#define Q_FLAG "Q:"
522#else
JP Abgrall53f17a92014-02-12 14:02:41 -0800523#define Q_FLAG
524#endif
525
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700526#define SHORTOPTS "aAb" B_FLAG "c:C:d" D_FLAG "eE:fF:G:hHi:" I_FLAG j_FLAG J_FLAG "KlLm:M:nNOpq" Q_FLAG "r:s:StT:u" U_FLAG "vV:w:W:xXy:Yz:Z:#"
Elliott Hughes892a68b2015-10-19 14:43:53 -0700527
528/*
529 * Long options.
530 *
531 * We do not currently have long options corresponding to all short
532 * options; we should probably pick appropriate option names for them.
533 *
534 * However, the short options where the number of times the option is
535 * specified matters, such as -v and -d and -t, should probably not
536 * just map to a long option, as saying
537 *
538 * tcpdump --verbose --verbose
539 *
540 * doesn't make sense; it should be --verbosity={N} or something such
541 * as that.
542 *
543 * For long options with no corresponding short options, we define values
544 * outside the range of ASCII graphic characters, make that the last
545 * component of the entry for the long option, and have a case for that
546 * option in the switch statement.
547 */
548#define OPTION_VERSION 128
549#define OPTION_TSTAMP_PRECISION 129
550#define OPTION_IMMEDIATE_MODE 130
551
552static const struct option longopts[] = {
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700553#if defined(HAVE_PCAP_CREATE) || defined(_WIN32)
Elliott Hughes892a68b2015-10-19 14:43:53 -0700554 { "buffer-size", required_argument, NULL, 'B' },
555#endif
556 { "list-interfaces", no_argument, NULL, 'D' },
557 { "help", no_argument, NULL, 'h' },
558 { "interface", required_argument, NULL, 'i' },
559#ifdef HAVE_PCAP_CREATE
560 { "monitor-mode", no_argument, NULL, 'I' },
561#endif
562#ifdef HAVE_PCAP_SET_TSTAMP_TYPE
563 { "time-stamp-type", required_argument, NULL, 'j' },
564 { "list-time-stamp-types", no_argument, NULL, 'J' },
565#endif
566#ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
567 { "time-stamp-precision", required_argument, NULL, OPTION_TSTAMP_PRECISION},
568#endif
569 { "dont-verify-checksums", no_argument, NULL, 'K' },
570 { "list-data-link-types", no_argument, NULL, 'L' },
571 { "no-optimize", no_argument, NULL, 'O' },
572 { "no-promiscuous-mode", no_argument, NULL, 'p' },
573#ifdef HAVE_PCAP_SETDIRECTION
574 { "direction", required_argument, NULL, 'Q' },
575#endif
576 { "snapshot-length", required_argument, NULL, 's' },
577 { "absolute-tcp-sequence-numbers", no_argument, NULL, 'S' },
578#ifdef HAVE_PCAP_DUMP_FLUSH
579 { "packet-buffered", no_argument, NULL, 'U' },
580#endif
581 { "linktype", required_argument, NULL, 'y' },
582#ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
583 { "immediate-mode", no_argument, NULL, OPTION_IMMEDIATE_MODE },
584#endif
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700585#ifdef HAVE_PCAP_SET_PARSER_DEBUG
Elliott Hughes892a68b2015-10-19 14:43:53 -0700586 { "debug-filter-parser", no_argument, NULL, 'Y' },
587#endif
588 { "relinquish-privileges", required_argument, NULL, 'Z' },
589 { "number", no_argument, NULL, '#' },
590 { "version", no_argument, NULL, OPTION_VERSION },
591 { NULL, 0, NULL, 0 }
592};
593
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700594#ifndef _WIN32
The Android Open Source Project2949f582009-03-03 19:30:46 -0800595/* Drop root privileges and chroot if necessary */
596static void
597droproot(const char *username, const char *chroot_dir)
598{
599 struct passwd *pw = NULL;
600
601 if (chroot_dir && !username) {
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700602 fprintf(stderr, "%s: Chroot without dropping root is insecure\n",
603 program_name);
604 exit_tcpdump(1);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800605 }
Elliott Hughes892a68b2015-10-19 14:43:53 -0700606
The Android Open Source Project2949f582009-03-03 19:30:46 -0800607 pw = getpwnam(username);
608 if (pw) {
609 if (chroot_dir) {
610 if (chroot(chroot_dir) != 0 || chdir ("/") != 0) {
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700611 fprintf(stderr, "%s: Couldn't chroot/chdir to '%.64s': %s\n",
612 program_name, chroot_dir, pcap_strerror(errno));
613 exit_tcpdump(1);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800614 }
615 }
Elliott Hughes892a68b2015-10-19 14:43:53 -0700616#ifdef HAVE_LIBCAP_NG
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700617 {
618 int ret = capng_change_id(pw->pw_uid, pw->pw_gid, CAPNG_NO_FLAG);
619 if (ret < 0) {
620 fprintf(stderr, "error : ret %d\n", ret);
621 } else {
622 fprintf(stderr, "dropped privs to %s\n", username);
623 }
Elliott Hughes892a68b2015-10-19 14:43:53 -0700624 }
JP Abgrall53f17a92014-02-12 14:02:41 -0800625#else
The Android Open Source Project2949f582009-03-03 19:30:46 -0800626 if (initgroups(pw->pw_name, pw->pw_gid) != 0 ||
627 setgid(pw->pw_gid) != 0 || setuid(pw->pw_uid) != 0) {
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700628 fprintf(stderr, "%s: Couldn't change to '%.32s' uid=%lu gid=%lu: %s\n",
629 program_name, username,
630 (unsigned long)pw->pw_uid,
631 (unsigned long)pw->pw_gid,
632 pcap_strerror(errno));
633 exit_tcpdump(1);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800634 }
Elliott Hughes892a68b2015-10-19 14:43:53 -0700635 else {
636 fprintf(stderr, "dropped privs to %s\n", username);
637 }
638#endif /* HAVE_LIBCAP_NG */
The Android Open Source Project2949f582009-03-03 19:30:46 -0800639 }
640 else {
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700641 fprintf(stderr, "%s: Couldn't find user '%.32s'\n",
642 program_name, username);
643 exit_tcpdump(1);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800644 }
Elliott Hughes892a68b2015-10-19 14:43:53 -0700645#ifdef HAVE_LIBCAP_NG
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700646 /* We don't need CAP_SETUID, CAP_SETGID and CAP_SYS_CHROOT any more. */
Elliott Hughes892a68b2015-10-19 14:43:53 -0700647 capng_updatev(
648 CAPNG_DROP,
649 CAPNG_EFFECTIVE | CAPNG_PERMITTED,
650 CAP_SETUID,
651 CAP_SETGID,
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700652 CAP_SYS_CHROOT,
Elliott Hughes892a68b2015-10-19 14:43:53 -0700653 -1);
654 capng_apply(CAPNG_SELECT_BOTH);
655#endif /* HAVE_LIBCAP_NG */
656
The Android Open Source Project2949f582009-03-03 19:30:46 -0800657}
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700658#endif /* _WIN32 */
The Android Open Source Project2949f582009-03-03 19:30:46 -0800659
660static int
661getWflagChars(int x)
662{
663 int c = 0;
664
665 x -= 1;
666 while (x > 0) {
667 c += 1;
668 x /= 10;
669 }
670
671 return c;
672}
673
674
675static void
676MakeFilename(char *buffer, char *orig_name, int cnt, int max_chars)
677{
JP Abgrall53f17a92014-02-12 14:02:41 -0800678 char *filename = malloc(PATH_MAX + 1);
679 if (filename == NULL)
680 error("Makefilename: malloc");
681
682 /* Process with strftime if Gflag is set. */
683 if (Gflag != 0) {
684 struct tm *local_tm;
685
686 /* Convert Gflag_time to a usable format */
687 if ((local_tm = localtime(&Gflag_time)) == NULL) {
688 error("MakeTimedFilename: localtime");
689 }
690
691 /* There's no good way to detect an error in strftime since a return
692 * value of 0 isn't necessarily failure.
693 */
694 strftime(filename, PATH_MAX, orig_name, local_tm);
695 } else {
696 strncpy(filename, orig_name, PATH_MAX);
697 }
698
The Android Open Source Project2949f582009-03-03 19:30:46 -0800699 if (cnt == 0 && max_chars == 0)
JP Abgrall53f17a92014-02-12 14:02:41 -0800700 strncpy(buffer, filename, PATH_MAX + 1);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800701 else
JP Abgrall53f17a92014-02-12 14:02:41 -0800702 if (snprintf(buffer, PATH_MAX + 1, "%s%0*d", filename, max_chars, cnt) > PATH_MAX)
703 /* Report an error if the filename is too large */
704 error("too many output files or filename is too long (> %d)", PATH_MAX);
705 free(filename);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800706}
707
JP Abgrall53f17a92014-02-12 14:02:41 -0800708static char *
709get_next_file(FILE *VFile, char *ptr)
710{
711 char *ret;
712
713 ret = fgets(ptr, PATH_MAX, VFile);
714 if (!ret)
715 return NULL;
716
717 if (ptr[strlen(ptr) - 1] == '\n')
718 ptr[strlen(ptr) - 1] = '\0';
719
720 return ret;
721}
722
Elliott Hughes892a68b2015-10-19 14:43:53 -0700723#ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
724static int
725tstamp_precision_from_string(const char *precision)
726{
727 if (strncmp(precision, "nano", strlen("nano")) == 0)
728 return PCAP_TSTAMP_PRECISION_NANO;
729
730 if (strncmp(precision, "micro", strlen("micro")) == 0)
731 return PCAP_TSTAMP_PRECISION_MICRO;
732
733 return -EINVAL;
734}
735
736static const char *
737tstamp_precision_to_string(int precision)
738{
739 switch (precision) {
740
741 case PCAP_TSTAMP_PRECISION_MICRO:
742 return "micro";
743
744 case PCAP_TSTAMP_PRECISION_NANO:
745 return "nano";
746
747 default:
748 return "unknown";
749 }
750}
751#endif
752
753#ifdef HAVE_CAPSICUM
754/*
755 * Ensure that, on a dump file's descriptor, we have all the rights
756 * necessary to make the standard I/O library work with an fdopen()ed
757 * FILE * from that descriptor.
758 *
759 * A long time ago, in a galaxy far far away, AT&T decided that, instead
760 * of providing separate APIs for getting and setting the FD_ flags on a
761 * descriptor, getting and setting the O_ flags on a descriptor, and
762 * locking files, they'd throw them all into a kitchen-sink fcntl() call
763 * along the lines of ioctl(), the fact that ioctl() operations are
764 * largely specific to particular character devices but fcntl() operations
765 * are either generic to all descriptors or generic to all descriptors for
766 * regular files nonwithstanding.
767 *
768 * The Capsicum people decided that fine-grained control of descriptor
769 * operations was required, so that you need to grant permission for
770 * reading, writing, seeking, and fcntl-ing. The latter, courtesy of
771 * AT&T's decision, means that "fcntl-ing" isn't a thing, but a motley
772 * collection of things, so there are *individual* fcntls for which
773 * permission needs to be granted.
774 *
775 * The FreeBSD standard I/O people implemented some optimizations that
776 * requires that the standard I/O routines be able to determine whether
777 * the descriptor for the FILE * is open append-only or not; as that
778 * descriptor could have come from an open() rather than an fopen(),
779 * that requires that it be able to do an F_GETFL fcntl() to read
780 * the O_ flags.
781 *
782 * Tcpdump uses ftell() to determine how much data has been written
783 * to a file in order to, when used with -C, determine when it's time
784 * to rotate capture files. ftell() therefore needs to do an lseek()
785 * to find out the file offset and must, thanks to the aforementioned
786 * optimization, also know whether the descriptor is open append-only
787 * or not.
788 *
789 * The net result of all the above is that we need to grant CAP_SEEK,
790 * CAP_WRITE, and CAP_FCNTL with the CAP_FCNTL_GETFL subcapability.
791 *
792 * Perhaps this is the universe's way of saying that either
793 *
794 * 1) there needs to be an fopenat() call and a pcap_dump_openat() call
795 * using it, so that Capsicum-capable tcpdump wouldn't need to do
796 * an fdopen()
797 *
798 * or
799 *
800 * 2) there needs to be a cap_fdopen() call in the FreeBSD standard
801 * I/O library that knows what rights are needed by the standard
802 * I/O library, based on the open mode, and assigns them, perhaps
803 * with an additional argument indicating, for example, whether
804 * seeking should be allowed, so that tcpdump doesn't need to know
805 * what the standard I/O library happens to require this week.
806 */
807static void
808set_dumper_capsicum_rights(pcap_dumper_t *p)
809{
810 int fd = fileno(pcap_dump_file(p));
811 cap_rights_t rights;
812
813 cap_rights_init(&rights, CAP_SEEK, CAP_WRITE, CAP_FCNTL);
814 if (cap_rights_limit(fd, &rights) < 0 && errno != ENOSYS) {
815 error("unable to limit dump descriptor");
816 }
817 if (cap_fcntls_limit(fd, CAP_FCNTL_GETFL) < 0 && errno != ENOSYS) {
818 error("unable to limit dump descriptor fcntls");
819 }
820}
821#endif
822
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700823/*
824 * Copy arg vector into a new buffer, concatenating arguments with spaces.
825 */
826static char *
827copy_argv(register char **argv)
828{
829 register char **p;
830 register u_int len = 0;
831 char *buf;
832 char *src, *dst;
833
834 p = argv;
835 if (*p == NULL)
836 return 0;
837
838 while (*p)
839 len += strlen(*p++) + 1;
840
841 buf = (char *)malloc(len);
842 if (buf == NULL)
843 error("copy_argv: malloc");
844
845 p = argv;
846 dst = buf;
847 while ((src = *p++) != NULL) {
848 while ((*dst++ = *src++) != '\0')
849 ;
850 dst[-1] = ' ';
851 }
852 dst[-1] = '\0';
853
854 return buf;
855}
856
857/*
858 * On Windows, we need to open the file in binary mode, so that
859 * we get all the bytes specified by the size we get from "fstat()".
860 * On UNIX, that's not necessary. O_BINARY is defined on Windows;
861 * we define it as 0 if it's not defined, so it does nothing.
862 */
863#ifndef O_BINARY
864#define O_BINARY 0
865#endif
866
867static char *
868read_infile(char *fname)
869{
870 register int i, fd, cc;
871 register char *cp;
872 struct stat buf;
873
874 fd = open(fname, O_RDONLY|O_BINARY);
875 if (fd < 0)
876 error("can't open %s: %s", fname, pcap_strerror(errno));
877
878 if (fstat(fd, &buf) < 0)
879 error("can't stat %s: %s", fname, pcap_strerror(errno));
880
881 cp = malloc((u_int)buf.st_size + 1);
882 if (cp == NULL)
883 error("malloc(%d) for %s: %s", (u_int)buf.st_size + 1,
884 fname, pcap_strerror(errno));
885 cc = read(fd, cp, (u_int)buf.st_size);
886 if (cc < 0)
887 error("read %s: %s", fname, pcap_strerror(errno));
888 if (cc != buf.st_size)
889 error("short read %s (%d != %d)", fname, cc, (int)buf.st_size);
890
891 close(fd);
892 /* replace "# comment" with spaces */
893 for (i = 0; i < cc; i++) {
894 if (cp[i] == '#')
895 while (i < cc && cp[i] != '\n')
896 cp[i++] = ' ';
897 }
898 cp[cc] = '\0';
899 return (cp);
900}
901
902#ifdef HAVE_PCAP_FINDALLDEVS
903static long
904parse_interface_number(const char *device)
905{
906 long devnum;
907 char *end;
908
909 devnum = strtol(device, &end, 10);
910 if (device != end && *end == '\0') {
911 /*
912 * It's all-numeric, but is it a valid number?
913 */
914 if (devnum <= 0) {
915 /*
916 * No, it's not an ordinal.
917 */
918 error("Invalid adapter index");
919 }
920 return (devnum);
921 } else {
922 /*
923 * It's not all-numeric; return -1, so our caller
924 * knows that.
925 */
926 return (-1);
927 }
928}
929
930static char *
931find_interface_by_number(long devnum)
932{
933 pcap_if_t *dev, *devlist;
934 long i;
935 char ebuf[PCAP_ERRBUF_SIZE];
936 char *device;
937
938 if (pcap_findalldevs(&devlist, ebuf) < 0)
939 error("%s", ebuf);
940 /*
941 * Look for the devnum-th entry in the list of devices (1-based).
942 */
943 for (i = 0, dev = devlist; i < devnum-1 && dev != NULL;
944 i++, dev = dev->next)
945 ;
946 if (dev == NULL)
947 error("Invalid adapter index");
948 device = strdup(dev->name);
949 pcap_freealldevs(devlist);
950 return (device);
951}
952#endif
953
954static pcap_t *
955open_interface(const char *device, netdissect_options *ndo, char *ebuf)
956{
957 pcap_t *pc;
958#ifdef HAVE_PCAP_CREATE
959 int status;
960 char *cp;
961#endif
962
963#ifdef HAVE_PCAP_CREATE
964 pc = pcap_create(device, ebuf);
965 if (pc == NULL) {
966 /*
967 * If this failed with "No such device", that means
968 * the interface doesn't exist; return NULL, so that
969 * the caller can see whether the device name is
970 * actually an interface index.
971 */
972 if (strstr(ebuf, "No such device") != NULL)
973 return (NULL);
974 error("%s", ebuf);
975 }
976#ifdef HAVE_PCAP_SET_TSTAMP_TYPE
977 if (Jflag)
978 show_tstamp_types_and_exit(pc, device);
979#endif
980#ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
981 status = pcap_set_tstamp_precision(pc, ndo->ndo_tstamp_precision);
982 if (status != 0)
983 error("%s: Can't set %ssecond time stamp precision: %s",
984 device,
985 tstamp_precision_to_string(ndo->ndo_tstamp_precision),
986 pcap_statustostr(status));
987#endif
988
989#ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
990 if (immediate_mode) {
991 status = pcap_set_immediate_mode(pc, 1);
992 if (status != 0)
993 error("%s: Can't set immediate mode: %s",
994 device,
995 pcap_statustostr(status));
996 }
997#endif
998 /*
999 * Is this an interface that supports monitor mode?
1000 */
1001 if (pcap_can_set_rfmon(pc) == 1)
1002 supports_monitor_mode = 1;
1003 else
1004 supports_monitor_mode = 0;
1005 status = pcap_set_snaplen(pc, ndo->ndo_snaplen);
1006 if (status != 0)
1007 error("%s: Can't set snapshot length: %s",
1008 device, pcap_statustostr(status));
1009 status = pcap_set_promisc(pc, !pflag);
1010 if (status != 0)
1011 error("%s: Can't set promiscuous mode: %s",
1012 device, pcap_statustostr(status));
1013 if (Iflag) {
1014 status = pcap_set_rfmon(pc, 1);
1015 if (status != 0)
1016 error("%s: Can't set monitor mode: %s",
1017 device, pcap_statustostr(status));
1018 }
1019 status = pcap_set_timeout(pc, 1000);
1020 if (status != 0)
1021 error("%s: pcap_set_timeout failed: %s",
1022 device, pcap_statustostr(status));
1023 if (Bflag != 0) {
1024 status = pcap_set_buffer_size(pc, Bflag);
1025 if (status != 0)
1026 error("%s: Can't set buffer size: %s",
1027 device, pcap_statustostr(status));
1028 }
1029#ifdef HAVE_PCAP_SET_TSTAMP_TYPE
1030 if (jflag != -1) {
1031 status = pcap_set_tstamp_type(pc, jflag);
1032 if (status < 0)
1033 error("%s: Can't set time stamp type: %s",
1034 device, pcap_statustostr(status));
1035 }
1036#endif
1037 status = pcap_activate(pc);
1038 if (status < 0) {
1039 /*
1040 * pcap_activate() failed.
1041 */
1042 cp = pcap_geterr(pc);
1043 if (status == PCAP_ERROR)
1044 error("%s", cp);
1045 else if (status == PCAP_ERROR_NO_SUCH_DEVICE) {
1046 /*
1047 * Return an error for our caller to handle.
1048 */
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001049 snprintf(ebuf, PCAP_ERRBUF_SIZE, "%s: %s\n(%s)",
1050 device, pcap_statustostr(status), cp);
Elliott Hughes9a986422017-12-19 14:49:10 -08001051 pcap_close(pc);
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001052 return (NULL);
1053 } else if (status == PCAP_ERROR_PERM_DENIED && *cp != '\0')
1054 error("%s: %s\n(%s)", device,
1055 pcap_statustostr(status), cp);
1056 else
1057 error("%s: %s", device,
1058 pcap_statustostr(status));
1059 } else if (status > 0) {
1060 /*
1061 * pcap_activate() succeeded, but it's warning us
1062 * of a problem it had.
1063 */
1064 cp = pcap_geterr(pc);
1065 if (status == PCAP_WARNING)
1066 warning("%s", cp);
1067 else if (status == PCAP_WARNING_PROMISC_NOTSUP &&
1068 *cp != '\0')
1069 warning("%s: %s\n(%s)", device,
1070 pcap_statustostr(status), cp);
1071 else
1072 warning("%s: %s", device,
1073 pcap_statustostr(status));
1074 }
1075#ifdef HAVE_PCAP_SETDIRECTION
1076 if (Qflag != -1) {
1077 status = pcap_setdirection(pc, Qflag);
1078 if (status != 0)
1079 error("%s: pcap_setdirection() failed: %s",
1080 device, pcap_geterr(pc));
1081 }
1082#endif /* HAVE_PCAP_SETDIRECTION */
1083#else /* HAVE_PCAP_CREATE */
1084 *ebuf = '\0';
1085 pc = pcap_open_live(device, ndo->ndo_snaplen, !pflag, 1000, ebuf);
1086 if (pc == NULL) {
1087 /*
1088 * If this failed with "No such device", that means
1089 * the interface doesn't exist; return NULL, so that
1090 * the caller can see whether the device name is
1091 * actually an interface index.
1092 */
1093 if (strstr(ebuf, "No such device") != NULL)
1094 return (NULL);
1095 error("%s", ebuf);
1096 }
1097 if (*ebuf)
1098 warning("%s", ebuf);
1099#endif /* HAVE_PCAP_CREATE */
1100
1101 return (pc);
1102}
1103
The Android Open Source Project2949f582009-03-03 19:30:46 -08001104int
1105main(int argc, char **argv)
1106{
1107 register int cnt, op, i;
Elliott Hughes892a68b2015-10-19 14:43:53 -07001108 bpf_u_int32 localnet =0 , netmask = 0;
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001109 int timezone_offset = 0;
JP Abgrall53f17a92014-02-12 14:02:41 -08001110 register char *cp, *infile, *cmdbuf, *device, *RFileName, *VFileName, *WFileName;
The Android Open Source Project2949f582009-03-03 19:30:46 -08001111 pcap_handler callback;
JP Abgrall53f17a92014-02-12 14:02:41 -08001112 int dlt;
JP Abgrall53f17a92014-02-12 14:02:41 -08001113 const char *dlt_name;
The Android Open Source Project2949f582009-03-03 19:30:46 -08001114 struct bpf_program fcode;
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001115#ifndef _WIN32
The Android Open Source Project2949f582009-03-03 19:30:46 -08001116 RETSIGTYPE (*oldhandler)(int);
1117#endif
The Android Open Source Project2949f582009-03-03 19:30:46 -08001118 struct dump_info dumpinfo;
1119 u_char *pcap_userdata;
1120 char ebuf[PCAP_ERRBUF_SIZE];
JP Abgrall53f17a92014-02-12 14:02:41 -08001121 char VFileLine[PATH_MAX + 1];
The Android Open Source Project2949f582009-03-03 19:30:46 -08001122 char *username = NULL;
1123 char *chroot_dir = NULL;
JP Abgrall53f17a92014-02-12 14:02:41 -08001124 char *ret = NULL;
1125 char *end;
The Android Open Source Project2949f582009-03-03 19:30:46 -08001126#ifdef HAVE_PCAP_FINDALLDEVS
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001127 pcap_if_t *devlist;
1128 long devnum;
The Android Open Source Project2949f582009-03-03 19:30:46 -08001129#endif
1130 int status;
JP Abgrall53f17a92014-02-12 14:02:41 -08001131 FILE *VFile;
Elliott Hughes892a68b2015-10-19 14:43:53 -07001132#ifdef HAVE_CAPSICUM
1133 cap_rights_t rights;
1134 int cansandbox;
1135#endif /* HAVE_CAPSICUM */
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001136 int Oflag = 1; /* run filter code optimizer */
1137 int yflag_dlt = -1;
1138 const char *yflag_dlt_name = NULL;
Elliott Hughes892a68b2015-10-19 14:43:53 -07001139
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001140 netdissect_options Ndo;
1141 netdissect_options *ndo = &Ndo;
The Android Open Source Project2949f582009-03-03 19:30:46 -08001142
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001143 /*
1144 * Initialize the netdissect code.
1145 */
1146 if (nd_init(ebuf, sizeof ebuf) == -1)
1147 error("%s", ebuf);
1148
1149 memset(ndo, 0, sizeof(*ndo));
1150 ndo_set_function_pointers(ndo);
1151 ndo->ndo_snaplen = DEFAULT_SNAPLEN;
Elliott Hughes892a68b2015-10-19 14:43:53 -07001152
The Android Open Source Project2949f582009-03-03 19:30:46 -08001153 cnt = -1;
1154 device = NULL;
1155 infile = NULL;
1156 RFileName = NULL;
JP Abgrall53f17a92014-02-12 14:02:41 -08001157 VFileName = NULL;
1158 VFile = NULL;
The Android Open Source Project2949f582009-03-03 19:30:46 -08001159 WFileName = NULL;
JP Abgrall53f17a92014-02-12 14:02:41 -08001160 dlt = -1;
The Android Open Source Project2949f582009-03-03 19:30:46 -08001161 if ((cp = strrchr(argv[0], '/')) != NULL)
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001162 ndo->program_name = program_name = cp + 1;
The Android Open Source Project2949f582009-03-03 19:30:46 -08001163 else
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001164 ndo->program_name = program_name = argv[0];
1165
1166#ifdef _WIN32
1167 if (pcap_wsockinit() != 0)
1168 error("Attempting to initialize Winsock failed");
1169#endif /* _WIN32 */
The Android Open Source Project2949f582009-03-03 19:30:46 -08001170
Elliott Hughes892a68b2015-10-19 14:43:53 -07001171 /*
1172 * On platforms where the CPU doesn't support unaligned loads,
1173 * force unaligned accesses to abort with SIGBUS, rather than
1174 * being fixed up (slowly) by the OS kernel; on those platforms,
1175 * misaligned accesses are bugs, and we want tcpdump to crash so
1176 * that the bugs are reported.
1177 */
The Android Open Source Project2949f582009-03-03 19:30:46 -08001178 if (abort_on_misalignment(ebuf, sizeof(ebuf)) < 0)
1179 error("%s", ebuf);
1180
The Android Open Source Project2949f582009-03-03 19:30:46 -08001181 while (
Elliott Hughes892a68b2015-10-19 14:43:53 -07001182 (op = getopt_long(argc, argv, SHORTOPTS, longopts, NULL)) != -1)
The Android Open Source Project2949f582009-03-03 19:30:46 -08001183 switch (op) {
1184
1185 case 'a':
1186 /* compatibility for old -a */
1187 break;
1188
1189 case 'A':
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001190 ++ndo->ndo_Aflag;
The Android Open Source Project2949f582009-03-03 19:30:46 -08001191 break;
1192
JP Abgrall53f17a92014-02-12 14:02:41 -08001193 case 'b':
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001194 ++ndo->ndo_bflag;
JP Abgrall53f17a92014-02-12 14:02:41 -08001195 break;
1196
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001197#if defined(HAVE_PCAP_CREATE) || defined(_WIN32)
The Android Open Source Project2949f582009-03-03 19:30:46 -08001198 case 'B':
JP Abgrall53f17a92014-02-12 14:02:41 -08001199 Bflag = atoi(optarg)*1024;
1200 if (Bflag <= 0)
The Android Open Source Project2949f582009-03-03 19:30:46 -08001201 error("invalid packet buffer size %s", optarg);
1202 break;
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001203#endif /* defined(HAVE_PCAP_CREATE) || defined(_WIN32) */
The Android Open Source Project2949f582009-03-03 19:30:46 -08001204
1205 case 'c':
1206 cnt = atoi(optarg);
1207 if (cnt <= 0)
1208 error("invalid packet count %s", optarg);
1209 break;
1210
1211 case 'C':
1212 Cflag = atoi(optarg) * 1000000;
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001213 if (Cflag <= 0)
The Android Open Source Project2949f582009-03-03 19:30:46 -08001214 error("invalid file size %s", optarg);
1215 break;
1216
1217 case 'd':
1218 ++dflag;
1219 break;
1220
The Android Open Source Project2949f582009-03-03 19:30:46 -08001221 case 'D':
Elliott Hughes892a68b2015-10-19 14:43:53 -07001222 Dflag++;
1223 break;
The Android Open Source Project2949f582009-03-03 19:30:46 -08001224
1225 case 'L':
1226 Lflag++;
1227 break;
1228
1229 case 'e':
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001230 ++ndo->ndo_eflag;
The Android Open Source Project2949f582009-03-03 19:30:46 -08001231 break;
1232
1233 case 'E':
1234#ifndef HAVE_LIBCRYPTO
1235 warning("crypto code not compiled in");
1236#endif
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001237 ndo->ndo_espsecret = optarg;
The Android Open Source Project2949f582009-03-03 19:30:46 -08001238 break;
1239
1240 case 'f':
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001241 ++ndo->ndo_fflag;
The Android Open Source Project2949f582009-03-03 19:30:46 -08001242 break;
1243
1244 case 'F':
1245 infile = optarg;
1246 break;
1247
JP Abgrall53f17a92014-02-12 14:02:41 -08001248 case 'G':
1249 Gflag = atoi(optarg);
1250 if (Gflag < 0)
1251 error("invalid number of seconds %s", optarg);
1252
1253 /* We will create one file initially. */
1254 Gflag_count = 0;
1255
1256 /* Grab the current time for rotation use. */
1257 if ((Gflag_time = time(NULL)) == (time_t)-1) {
1258 error("main: can't get current time: %s",
1259 pcap_strerror(errno));
1260 }
1261 break;
1262
1263 case 'h':
Elliott Hughes892a68b2015-10-19 14:43:53 -07001264 print_usage();
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001265 exit_tcpdump(0);
JP Abgrall53f17a92014-02-12 14:02:41 -08001266 break;
1267
1268 case 'H':
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001269 ++ndo->ndo_Hflag;
JP Abgrall53f17a92014-02-12 14:02:41 -08001270 break;
1271
The Android Open Source Project2949f582009-03-03 19:30:46 -08001272 case 'i':
The Android Open Source Project2949f582009-03-03 19:30:46 -08001273 device = optarg;
1274 break;
1275
JP Abgrall53f17a92014-02-12 14:02:41 -08001276#ifdef HAVE_PCAP_CREATE
1277 case 'I':
1278 ++Iflag;
1279 break;
1280#endif /* HAVE_PCAP_CREATE */
1281
1282#ifdef HAVE_PCAP_SET_TSTAMP_TYPE
1283 case 'j':
1284 jflag = pcap_tstamp_type_name_to_val(optarg);
1285 if (jflag < 0)
1286 error("invalid time stamp type %s", optarg);
1287 break;
1288
1289 case 'J':
1290 Jflag++;
1291 break;
1292#endif
1293
The Android Open Source Project2949f582009-03-03 19:30:46 -08001294 case 'l':
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001295#ifdef _WIN32
The Android Open Source Project2949f582009-03-03 19:30:46 -08001296 /*
1297 * _IOLBF is the same as _IOFBF in Microsoft's C
1298 * libraries; the only alternative they offer
1299 * is _IONBF.
1300 *
1301 * XXX - this should really be checking for MSVC++,
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001302 * not _WIN32, if, for example, MinGW has its own
The Android Open Source Project2949f582009-03-03 19:30:46 -08001303 * C library that is more UNIX-compatible.
1304 */
1305 setvbuf(stdout, NULL, _IONBF, 0);
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001306#else /* _WIN32 */
The Android Open Source Project2949f582009-03-03 19:30:46 -08001307#ifdef HAVE_SETLINEBUF
1308 setlinebuf(stdout);
1309#else
1310 setvbuf(stdout, NULL, _IOLBF, 0);
1311#endif
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001312#endif /* _WIN32 */
The Android Open Source Project2949f582009-03-03 19:30:46 -08001313 break;
1314
JP Abgrall53f17a92014-02-12 14:02:41 -08001315 case 'K':
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001316 ++ndo->ndo_Kflag;
The Android Open Source Project2949f582009-03-03 19:30:46 -08001317 break;
1318
1319 case 'm':
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001320 if (nd_have_smi_support()) {
1321 if (nd_load_smi_module(optarg, ebuf, sizeof ebuf) == -1)
1322 error("%s", ebuf);
1323 } else {
1324 (void)fprintf(stderr, "%s: ignoring option `-m %s' ",
1325 program_name, optarg);
1326 (void)fprintf(stderr, "(no libsmi support)\n");
JP Abgrall53f17a92014-02-12 14:02:41 -08001327 }
The Android Open Source Project2949f582009-03-03 19:30:46 -08001328 break;
1329
1330 case 'M':
1331 /* TCP-MD5 shared secret */
1332#ifndef HAVE_LIBCRYPTO
1333 warning("crypto code not compiled in");
1334#endif
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001335 ndo->ndo_sigsecret = optarg;
JP Abgrall53f17a92014-02-12 14:02:41 -08001336 break;
1337
1338 case 'n':
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001339 ++ndo->ndo_nflag;
JP Abgrall53f17a92014-02-12 14:02:41 -08001340 break;
1341
1342 case 'N':
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001343 ++ndo->ndo_Nflag;
The Android Open Source Project2949f582009-03-03 19:30:46 -08001344 break;
1345
1346 case 'O':
1347 Oflag = 0;
1348 break;
1349
1350 case 'p':
1351 ++pflag;
1352 break;
1353
1354 case 'q':
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001355 ++ndo->ndo_qflag;
1356 ++ndo->ndo_suppress_default_print;
The Android Open Source Project2949f582009-03-03 19:30:46 -08001357 break;
1358
Elliott Hughes892a68b2015-10-19 14:43:53 -07001359#ifdef HAVE_PCAP_SETDIRECTION
1360 case 'Q':
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001361 if (ascii_strcasecmp(optarg, "in") == 0)
Elliott Hughes892a68b2015-10-19 14:43:53 -07001362 Qflag = PCAP_D_IN;
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001363 else if (ascii_strcasecmp(optarg, "out") == 0)
Elliott Hughes892a68b2015-10-19 14:43:53 -07001364 Qflag = PCAP_D_OUT;
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001365 else if (ascii_strcasecmp(optarg, "inout") == 0)
Elliott Hughes892a68b2015-10-19 14:43:53 -07001366 Qflag = PCAP_D_INOUT;
1367 else
1368 error("unknown capture direction `%s'", optarg);
1369 break;
1370#endif /* HAVE_PCAP_SETDIRECTION */
1371
The Android Open Source Project2949f582009-03-03 19:30:46 -08001372 case 'r':
1373 RFileName = optarg;
1374 break;
1375
JP Abgrall53f17a92014-02-12 14:02:41 -08001376 case 's':
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001377 ndo->ndo_snaplen = strtol(optarg, &end, 0);
The Android Open Source Project2949f582009-03-03 19:30:46 -08001378 if (optarg == end || *end != '\0'
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001379 || ndo->ndo_snaplen < 0 || ndo->ndo_snaplen > MAXIMUM_SNAPLEN)
The Android Open Source Project2949f582009-03-03 19:30:46 -08001380 error("invalid snaplen %s", optarg);
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001381 else if (ndo->ndo_snaplen == 0)
1382 ndo->ndo_snaplen = MAXIMUM_SNAPLEN;
The Android Open Source Project2949f582009-03-03 19:30:46 -08001383 break;
The Android Open Source Project2949f582009-03-03 19:30:46 -08001384
1385 case 'S':
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001386 ++ndo->ndo_Sflag;
The Android Open Source Project2949f582009-03-03 19:30:46 -08001387 break;
1388
1389 case 't':
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001390 ++ndo->ndo_tflag;
The Android Open Source Project2949f582009-03-03 19:30:46 -08001391 break;
1392
1393 case 'T':
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001394 if (ascii_strcasecmp(optarg, "vat") == 0)
1395 ndo->ndo_packettype = PT_VAT;
1396 else if (ascii_strcasecmp(optarg, "wb") == 0)
1397 ndo->ndo_packettype = PT_WB;
1398 else if (ascii_strcasecmp(optarg, "rpc") == 0)
1399 ndo->ndo_packettype = PT_RPC;
1400 else if (ascii_strcasecmp(optarg, "rtp") == 0)
1401 ndo->ndo_packettype = PT_RTP;
1402 else if (ascii_strcasecmp(optarg, "rtcp") == 0)
1403 ndo->ndo_packettype = PT_RTCP;
1404 else if (ascii_strcasecmp(optarg, "snmp") == 0)
1405 ndo->ndo_packettype = PT_SNMP;
1406 else if (ascii_strcasecmp(optarg, "cnfp") == 0)
1407 ndo->ndo_packettype = PT_CNFP;
1408 else if (ascii_strcasecmp(optarg, "tftp") == 0)
1409 ndo->ndo_packettype = PT_TFTP;
1410 else if (ascii_strcasecmp(optarg, "aodv") == 0)
1411 ndo->ndo_packettype = PT_AODV;
1412 else if (ascii_strcasecmp(optarg, "carp") == 0)
1413 ndo->ndo_packettype = PT_CARP;
1414 else if (ascii_strcasecmp(optarg, "radius") == 0)
1415 ndo->ndo_packettype = PT_RADIUS;
1416 else if (ascii_strcasecmp(optarg, "zmtp1") == 0)
1417 ndo->ndo_packettype = PT_ZMTP1;
1418 else if (ascii_strcasecmp(optarg, "vxlan") == 0)
1419 ndo->ndo_packettype = PT_VXLAN;
1420 else if (ascii_strcasecmp(optarg, "pgm") == 0)
1421 ndo->ndo_packettype = PT_PGM;
1422 else if (ascii_strcasecmp(optarg, "pgm_zmtp1") == 0)
1423 ndo->ndo_packettype = PT_PGM_ZMTP1;
1424 else if (ascii_strcasecmp(optarg, "lmp") == 0)
1425 ndo->ndo_packettype = PT_LMP;
1426 else if (ascii_strcasecmp(optarg, "resp") == 0)
1427 ndo->ndo_packettype = PT_RESP;
The Android Open Source Project2949f582009-03-03 19:30:46 -08001428 else
1429 error("unknown packet type `%s'", optarg);
1430 break;
1431
1432 case 'u':
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001433 ++ndo->ndo_uflag;
The Android Open Source Project2949f582009-03-03 19:30:46 -08001434 break;
1435
1436#ifdef HAVE_PCAP_DUMP_FLUSH
1437 case 'U':
1438 ++Uflag;
1439 break;
1440#endif
1441
1442 case 'v':
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001443 ++ndo->ndo_vflag;
The Android Open Source Project2949f582009-03-03 19:30:46 -08001444 break;
1445
JP Abgrall53f17a92014-02-12 14:02:41 -08001446 case 'V':
1447 VFileName = optarg;
1448 break;
1449
The Android Open Source Project2949f582009-03-03 19:30:46 -08001450 case 'w':
1451 WFileName = optarg;
1452 break;
1453
1454 case 'W':
1455 Wflag = atoi(optarg);
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001456 if (Wflag <= 0)
The Android Open Source Project2949f582009-03-03 19:30:46 -08001457 error("invalid number of output files %s", optarg);
1458 WflagChars = getWflagChars(Wflag);
1459 break;
1460
1461 case 'x':
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001462 ++ndo->ndo_xflag;
1463 ++ndo->ndo_suppress_default_print;
The Android Open Source Project2949f582009-03-03 19:30:46 -08001464 break;
1465
1466 case 'X':
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001467 ++ndo->ndo_Xflag;
1468 ++ndo->ndo_suppress_default_print;
The Android Open Source Project2949f582009-03-03 19:30:46 -08001469 break;
1470
1471 case 'y':
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001472 yflag_dlt_name = optarg;
1473 yflag_dlt =
1474 pcap_datalink_name_to_val(yflag_dlt_name);
1475 if (yflag_dlt < 0)
1476 error("invalid data link type %s", yflag_dlt_name);
The Android Open Source Project2949f582009-03-03 19:30:46 -08001477 break;
1478
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001479#ifdef HAVE_PCAP_SET_PARSER_DEBUG
The Android Open Source Project2949f582009-03-03 19:30:46 -08001480 case 'Y':
1481 {
1482 /* Undocumented flag */
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001483 pcap_set_parser_debug(1);
The Android Open Source Project2949f582009-03-03 19:30:46 -08001484 }
1485 break;
1486#endif
JP Abgrall53f17a92014-02-12 14:02:41 -08001487 case 'z':
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001488 zflag = optarg;
JP Abgrall53f17a92014-02-12 14:02:41 -08001489 break;
1490
The Android Open Source Project2949f582009-03-03 19:30:46 -08001491 case 'Z':
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001492 username = optarg;
The Android Open Source Project2949f582009-03-03 19:30:46 -08001493 break;
1494
Elliott Hughes892a68b2015-10-19 14:43:53 -07001495 case '#':
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001496 ndo->ndo_packet_number = 1;
Elliott Hughes892a68b2015-10-19 14:43:53 -07001497 break;
1498
1499 case OPTION_VERSION:
1500 print_version();
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001501 exit_tcpdump(0);
Elliott Hughes892a68b2015-10-19 14:43:53 -07001502 break;
1503
1504#ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
1505 case OPTION_TSTAMP_PRECISION:
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001506 ndo->ndo_tstamp_precision = tstamp_precision_from_string(optarg);
1507 if (ndo->ndo_tstamp_precision < 0)
Elliott Hughes892a68b2015-10-19 14:43:53 -07001508 error("unsupported time stamp precision");
1509 break;
1510#endif
1511
1512#ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
1513 case OPTION_IMMEDIATE_MODE:
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001514 immediate_mode = 1;
Elliott Hughes892a68b2015-10-19 14:43:53 -07001515 break;
1516#endif
1517
The Android Open Source Project2949f582009-03-03 19:30:46 -08001518 default:
Elliott Hughes892a68b2015-10-19 14:43:53 -07001519 print_usage();
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001520 exit_tcpdump(1);
The Android Open Source Project2949f582009-03-03 19:30:46 -08001521 /* NOTREACHED */
1522 }
1523
Elliott Hughes892a68b2015-10-19 14:43:53 -07001524#ifdef HAVE_PCAP_FINDALLDEVS
1525 if (Dflag)
1526 show_devices_and_exit();
1527#endif
1528
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001529 switch (ndo->ndo_tflag) {
The Android Open Source Project2949f582009-03-03 19:30:46 -08001530
1531 case 0: /* Default */
1532 case 4: /* Default + Date*/
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001533 timezone_offset = gmt2local(0);
The Android Open Source Project2949f582009-03-03 19:30:46 -08001534 break;
1535
1536 case 1: /* No time stamp */
1537 case 2: /* Unix timeval style */
1538 case 3: /* Microseconds since previous packet */
JP Abgrall53f17a92014-02-12 14:02:41 -08001539 case 5: /* Microseconds since first packet */
The Android Open Source Project2949f582009-03-03 19:30:46 -08001540 break;
1541
1542 default: /* Not supported */
JP Abgrall53f17a92014-02-12 14:02:41 -08001543 error("only -t, -tt, -ttt, -tttt and -ttttt are supported");
The Android Open Source Project2949f582009-03-03 19:30:46 -08001544 break;
1545 }
1546
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001547 if (ndo->ndo_fflag != 0 && (VFileName != NULL || RFileName != NULL))
JP Abgrall53f17a92014-02-12 14:02:41 -08001548 error("-f can not be used with -V or -r");
1549
1550 if (VFileName != NULL && RFileName != NULL)
1551 error("-V and -r are mutually exclusive.");
1552
Elliott Hughes892a68b2015-10-19 14:43:53 -07001553#ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
1554 /*
1555 * If we're printing dissected packets to the standard output
1556 * rather than saving raw packets to a file, and the standard
1557 * output is a terminal, use immediate mode, as the user's
1558 * probably expecting to see packets pop up immediately.
1559 */
1560 if (WFileName == NULL && isatty(1))
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001561 immediate_mode = 1;
Elliott Hughes892a68b2015-10-19 14:43:53 -07001562#endif
1563
The Android Open Source Project2949f582009-03-03 19:30:46 -08001564#ifdef WITH_CHROOT
1565 /* if run as root, prepare for chrooting */
1566 if (getuid() == 0 || geteuid() == 0) {
1567 /* future extensibility for cmd-line arguments */
1568 if (!chroot_dir)
1569 chroot_dir = WITH_CHROOT;
1570 }
1571#endif
1572
1573#ifdef WITH_USER
1574 /* if run as root, prepare for dropping root privileges */
1575 if (getuid() == 0 || geteuid() == 0) {
Elliott Hughes892a68b2015-10-19 14:43:53 -07001576 /* Run with '-Z root' to restore old behaviour */
The Android Open Source Project2949f582009-03-03 19:30:46 -08001577 if (!username)
1578 username = WITH_USER;
1579 }
1580#endif
1581
JP Abgrall53f17a92014-02-12 14:02:41 -08001582 if (RFileName != NULL || VFileName != NULL) {
1583 /*
1584 * If RFileName is non-null, it's the pathname of a
1585 * savefile to read. If VFileName is non-null, it's
1586 * the pathname of a file containing a list of pathnames
1587 * (one per line) of savefiles to read.
1588 *
1589 * In either case, we're reading a savefile, not doing
1590 * a live capture.
1591 */
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001592#ifndef _WIN32
The Android Open Source Project2949f582009-03-03 19:30:46 -08001593 /*
1594 * We don't need network access, so relinquish any set-UID
1595 * or set-GID privileges we have (if any).
1596 *
1597 * We do *not* want set-UID privileges when opening a
1598 * trace file, as that might let the user read other
1599 * people's trace files (especially if we're set-UID
1600 * root).
1601 */
1602 if (setgid(getgid()) != 0 || setuid(getuid()) != 0 )
1603 fprintf(stderr, "Warning: setgid/setuid failed !\n");
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001604#endif /* _WIN32 */
JP Abgrall53f17a92014-02-12 14:02:41 -08001605 if (VFileName != NULL) {
1606 if (VFileName[0] == '-' && VFileName[1] == '\0')
1607 VFile = stdin;
1608 else
1609 VFile = fopen(VFileName, "r");
1610
1611 if (VFile == NULL)
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001612 error("Unable to open file: %s\n", pcap_strerror(errno));
JP Abgrall53f17a92014-02-12 14:02:41 -08001613
1614 ret = get_next_file(VFile, VFileLine);
1615 if (!ret)
1616 error("Nothing in %s\n", VFileName);
1617 RFileName = VFileLine;
1618 }
1619
Elliott Hughes892a68b2015-10-19 14:43:53 -07001620#ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
1621 pd = pcap_open_offline_with_tstamp_precision(RFileName,
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001622 ndo->ndo_tstamp_precision, ebuf);
Elliott Hughes892a68b2015-10-19 14:43:53 -07001623#else
The Android Open Source Project2949f582009-03-03 19:30:46 -08001624 pd = pcap_open_offline(RFileName, ebuf);
Elliott Hughes892a68b2015-10-19 14:43:53 -07001625#endif
1626
The Android Open Source Project2949f582009-03-03 19:30:46 -08001627 if (pd == NULL)
1628 error("%s", ebuf);
Elliott Hughes892a68b2015-10-19 14:43:53 -07001629#ifdef HAVE_CAPSICUM
1630 cap_rights_init(&rights, CAP_READ);
1631 if (cap_rights_limit(fileno(pcap_file(pd)), &rights) < 0 &&
1632 errno != ENOSYS) {
1633 error("unable to limit pcap descriptor");
1634 }
1635#endif
The Android Open Source Project2949f582009-03-03 19:30:46 -08001636 dlt = pcap_datalink(pd);
1637 dlt_name = pcap_datalink_val_to_name(dlt);
1638 if (dlt_name == NULL) {
1639 fprintf(stderr, "reading from file %s, link-type %u\n",
1640 RFileName, dlt);
1641 } else {
1642 fprintf(stderr,
1643 "reading from file %s, link-type %s (%s)\n",
1644 RFileName, dlt_name,
1645 pcap_datalink_val_to_description(dlt));
1646 }
The Android Open Source Project2949f582009-03-03 19:30:46 -08001647 } else {
JP Abgrall53f17a92014-02-12 14:02:41 -08001648 /*
1649 * We're doing a live capture.
1650 */
The Android Open Source Project2949f582009-03-03 19:30:46 -08001651 if (device == NULL) {
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001652 /*
1653 * No interface was specified. Pick one.
1654 */
1655#ifdef HAVE_PCAP_FINDALLDEVS
1656 /*
1657 * Find the list of interfaces, and pick
1658 * the first interface.
1659 */
1660 if (pcap_findalldevs(&devlist, ebuf) >= 0 &&
1661 devlist != NULL) {
1662 device = strdup(devlist->name);
1663 pcap_freealldevs(devlist);
1664 }
1665#else /* HAVE_PCAP_FINDALLDEVS */
1666 /*
1667 * Use whatever interface pcap_lookupdev()
1668 * chooses.
1669 */
The Android Open Source Project2949f582009-03-03 19:30:46 -08001670 device = pcap_lookupdev(ebuf);
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001671#endif
The Android Open Source Project2949f582009-03-03 19:30:46 -08001672 if (device == NULL)
1673 error("%s", ebuf);
1674 }
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001675
JP Abgrall53f17a92014-02-12 14:02:41 -08001676 /*
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001677 * Try to open the interface with the specified name.
JP Abgrall53f17a92014-02-12 14:02:41 -08001678 */
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001679 pd = open_interface(device, ndo, ebuf);
1680 if (pd == NULL) {
1681 /*
1682 * That failed. If we can get a list of
1683 * interfaces, and the interface name
1684 * is purely numeric, try to use it as
1685 * a 1-based index in the list of
1686 * interfaces.
1687 */
1688#ifdef HAVE_PCAP_FINDALLDEVS
1689 devnum = parse_interface_number(device);
1690 if (devnum == -1) {
1691 /*
1692 * It's not a number; just report
1693 * the open error and fail.
1694 */
1695 error("%s", ebuf);
1696 }
1697
1698 /*
1699 * OK, it's a number; try to find the
1700 * interface with that index, and try
1701 * to open it.
1702 *
1703 * find_interface_by_number() exits if it
1704 * couldn't be found.
1705 */
1706 device = find_interface_by_number(devnum);
1707 pd = open_interface(device, ndo, ebuf);
1708 if (pd == NULL)
1709 error("%s", ebuf);
1710#else /* HAVE_PCAP_FINDALLDEVS */
1711 /*
1712 * We can't get a list of interfaces; just
1713 * fail.
1714 */
1715 error("%s", ebuf);
1716#endif /* HAVE_PCAP_FINDALLDEVS */
The Android Open Source Project2949f582009-03-03 19:30:46 -08001717 }
1718
The Android Open Source Project2949f582009-03-03 19:30:46 -08001719 /*
1720 * Let user own process after socket has been opened.
1721 */
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001722#ifndef _WIN32
The Android Open Source Project2949f582009-03-03 19:30:46 -08001723 if (setgid(getgid()) != 0 || setuid(getuid()) != 0)
1724 fprintf(stderr, "Warning: setgid/setuid failed !\n");
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001725#endif /* _WIN32 */
1726#if !defined(HAVE_PCAP_CREATE) && defined(_WIN32)
JP Abgrall53f17a92014-02-12 14:02:41 -08001727 if(Bflag != 0)
1728 if(pcap_setbuff(pd, Bflag)==-1){
The Android Open Source Project2949f582009-03-03 19:30:46 -08001729 error("%s", pcap_geterr(pd));
1730 }
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001731#endif /* !defined(HAVE_PCAP_CREATE) && defined(_WIN32) */
The Android Open Source Project2949f582009-03-03 19:30:46 -08001732 if (Lflag)
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001733 show_dlts_and_exit(pd, device);
1734 if (yflag_dlt >= 0) {
The Android Open Source Project2949f582009-03-03 19:30:46 -08001735#ifdef HAVE_PCAP_SET_DATALINK
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001736 if (pcap_set_datalink(pd, yflag_dlt) < 0)
The Android Open Source Project2949f582009-03-03 19:30:46 -08001737 error("%s", pcap_geterr(pd));
1738#else
1739 /*
1740 * We don't actually support changing the
1741 * data link type, so we only let them
1742 * set it to what it already is.
1743 */
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001744 if (yflag_dlt != pcap_datalink(pd)) {
The Android Open Source Project2949f582009-03-03 19:30:46 -08001745 error("%s is not one of the DLTs supported by this device\n",
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001746 yflag_dlt_name);
The Android Open Source Project2949f582009-03-03 19:30:46 -08001747 }
1748#endif
1749 (void)fprintf(stderr, "%s: data link type %s\n",
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001750 program_name, yflag_dlt_name);
The Android Open Source Project2949f582009-03-03 19:30:46 -08001751 (void)fflush(stderr);
1752 }
1753 i = pcap_snapshot(pd);
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001754 if (ndo->ndo_snaplen < i) {
1755 warning("snaplen raised from %d to %d", ndo->ndo_snaplen, i);
1756 ndo->ndo_snaplen = i;
The Android Open Source Project2949f582009-03-03 19:30:46 -08001757 }
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001758 if(ndo->ndo_fflag != 0) {
Elliott Hughes892a68b2015-10-19 14:43:53 -07001759 if (pcap_lookupnet(device, &localnet, &netmask, ebuf) < 0) {
1760 warning("foreign (-f) flag used but: %s", ebuf);
1761 }
1762 }
1763
The Android Open Source Project2949f582009-03-03 19:30:46 -08001764 }
1765 if (infile)
1766 cmdbuf = read_infile(infile);
1767 else
1768 cmdbuf = copy_argv(&argv[optind]);
1769
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001770#ifdef HAVE_PCAP_SET_OPTIMIZER_DEBUG
1771 pcap_set_optimizer_debug(dflag);
1772#endif
The Android Open Source Project2949f582009-03-03 19:30:46 -08001773 if (pcap_compile(pd, &fcode, cmdbuf, Oflag, netmask) < 0)
1774 error("%s", pcap_geterr(pd));
1775 if (dflag) {
1776 bpf_dump(&fcode, dflag);
1777 pcap_close(pd);
JP Abgrall53f17a92014-02-12 14:02:41 -08001778 free(cmdbuf);
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001779 pcap_freecode(&fcode);
1780 exit_tcpdump(0);
The Android Open Source Project2949f582009-03-03 19:30:46 -08001781 }
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001782 init_print(ndo, localnet, netmask, timezone_offset);
The Android Open Source Project2949f582009-03-03 19:30:46 -08001783
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001784#ifndef _WIN32
The Android Open Source Project2949f582009-03-03 19:30:46 -08001785 (void)setsignal(SIGPIPE, cleanup);
The Android Open Source Project2949f582009-03-03 19:30:46 -08001786 (void)setsignal(SIGTERM, cleanup);
1787 (void)setsignal(SIGINT, cleanup);
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001788#endif /* _WIN32 */
JP Abgrall53f17a92014-02-12 14:02:41 -08001789#if defined(HAVE_FORK) || defined(HAVE_VFORK)
1790 (void)setsignal(SIGCHLD, child_cleanup);
1791#endif
The Android Open Source Project2949f582009-03-03 19:30:46 -08001792 /* Cooperate with nohup(1) */
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001793#ifndef _WIN32
The Android Open Source Project2949f582009-03-03 19:30:46 -08001794 if ((oldhandler = setsignal(SIGHUP, cleanup)) != SIG_DFL)
1795 (void)setsignal(SIGHUP, oldhandler);
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001796#endif /* _WIN32 */
The Android Open Source Project2949f582009-03-03 19:30:46 -08001797
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001798#ifndef _WIN32
JP Abgrall53f17a92014-02-12 14:02:41 -08001799 /*
1800 * If a user name was specified with "-Z", attempt to switch to
1801 * that user's UID. This would probably be used with sudo,
1802 * to allow tcpdump to be run in a special restricted
1803 * account (if you just want to allow users to open capture
1804 * devices, and can't just give users that permission,
1805 * you'd make tcpdump set-UID or set-GID).
1806 *
1807 * Tcpdump doesn't necessarily write only to one savefile;
1808 * the general only way to allow a -Z instance to write to
1809 * savefiles as the user under whose UID it's run, rather
1810 * than as the user specified with -Z, would thus be to switch
1811 * to the original user ID before opening a capture file and
1812 * then switch back to the -Z user ID after opening the savefile.
1813 * Switching to the -Z user ID only after opening the first
1814 * savefile doesn't handle the general case.
1815 */
1816
JP Abgrall53f17a92014-02-12 14:02:41 -08001817 if (getuid() == 0 || geteuid() == 0) {
Elliott Hughes892a68b2015-10-19 14:43:53 -07001818#ifdef HAVE_LIBCAP_NG
1819 /* Initialize capng */
1820 capng_clear(CAPNG_SELECT_BOTH);
1821 if (username) {
1822 capng_updatev(
1823 CAPNG_ADD,
1824 CAPNG_PERMITTED | CAPNG_EFFECTIVE,
1825 CAP_SETUID,
1826 CAP_SETGID,
1827 -1);
1828 }
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001829 if (chroot_dir) {
1830 capng_update(
1831 CAPNG_ADD,
1832 CAPNG_PERMITTED | CAPNG_EFFECTIVE,
1833 CAP_SYS_CHROOT
1834 );
1835 }
Elliott Hughes892a68b2015-10-19 14:43:53 -07001836
1837 if (WFileName) {
1838 capng_update(
1839 CAPNG_ADD,
1840 CAPNG_PERMITTED | CAPNG_EFFECTIVE,
1841 CAP_DAC_OVERRIDE
1842 );
1843 }
1844 capng_apply(CAPNG_SELECT_BOTH);
1845#endif /* HAVE_LIBCAP_NG */
JP Abgrall53f17a92014-02-12 14:02:41 -08001846 if (username || chroot_dir)
1847 droproot(username, chroot_dir);
1848
1849 }
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001850#endif /* _WIN32 */
JP Abgrall53f17a92014-02-12 14:02:41 -08001851
The Android Open Source Project2949f582009-03-03 19:30:46 -08001852 if (pcap_setfilter(pd, &fcode) < 0)
1853 error("%s", pcap_geterr(pd));
Elliott Hughes892a68b2015-10-19 14:43:53 -07001854#ifdef HAVE_CAPSICUM
1855 if (RFileName == NULL && VFileName == NULL) {
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001856 static const unsigned long cmds[] = { BIOCGSTATS, BIOCROTZBUF };
Elliott Hughes892a68b2015-10-19 14:43:53 -07001857
Elliott Hughes9a986422017-12-19 14:49:10 -08001858 /*
1859 * The various libpcap devices use a combination of
1860 * read (bpf), ioctl (bpf, netmap), poll (netmap)
1861 * so we add the relevant access rights.
1862 */
1863 cap_rights_init(&rights, CAP_IOCTL, CAP_READ, CAP_EVENT);
Elliott Hughes892a68b2015-10-19 14:43:53 -07001864 if (cap_rights_limit(pcap_fileno(pd), &rights) < 0 &&
1865 errno != ENOSYS) {
1866 error("unable to limit pcap descriptor");
1867 }
1868 if (cap_ioctls_limit(pcap_fileno(pd), cmds,
1869 sizeof(cmds) / sizeof(cmds[0])) < 0 && errno != ENOSYS) {
1870 error("unable to limit ioctls on pcap descriptor");
1871 }
1872 }
1873#endif
The Android Open Source Project2949f582009-03-03 19:30:46 -08001874 if (WFileName) {
1875 pcap_dumper_t *p;
JP Abgrall53f17a92014-02-12 14:02:41 -08001876 /* Do not exceed the default PATH_MAX for files. */
1877 dumpinfo.CurrentFileName = (char *)malloc(PATH_MAX + 1);
The Android Open Source Project2949f582009-03-03 19:30:46 -08001878
JP Abgrall53f17a92014-02-12 14:02:41 -08001879 if (dumpinfo.CurrentFileName == NULL)
1880 error("malloc of dumpinfo.CurrentFileName");
1881
1882 /* We do not need numbering for dumpfiles if Cflag isn't set. */
1883 if (Cflag != 0)
1884 MakeFilename(dumpinfo.CurrentFileName, WFileName, 0, WflagChars);
1885 else
1886 MakeFilename(dumpinfo.CurrentFileName, WFileName, 0, 0);
1887
1888 p = pcap_dump_open(pd, dumpinfo.CurrentFileName);
Elliott Hughes892a68b2015-10-19 14:43:53 -07001889#ifdef HAVE_LIBCAP_NG
1890 /* Give up CAP_DAC_OVERRIDE capability.
1891 * Only allow it to be restored if the -C or -G flag have been
1892 * set since we may need to create more files later on.
1893 */
1894 capng_update(
1895 CAPNG_DROP,
1896 (Cflag || Gflag ? 0 : CAPNG_PERMITTED)
1897 | CAPNG_EFFECTIVE,
1898 CAP_DAC_OVERRIDE
1899 );
1900 capng_apply(CAPNG_SELECT_BOTH);
1901#endif /* HAVE_LIBCAP_NG */
The Android Open Source Project2949f582009-03-03 19:30:46 -08001902 if (p == NULL)
1903 error("%s", pcap_geterr(pd));
Elliott Hughes892a68b2015-10-19 14:43:53 -07001904#ifdef HAVE_CAPSICUM
1905 set_dumper_capsicum_rights(p);
1906#endif
JP Abgrall53f17a92014-02-12 14:02:41 -08001907 if (Cflag != 0 || Gflag != 0) {
Elliott Hughes892a68b2015-10-19 14:43:53 -07001908#ifdef HAVE_CAPSICUM
1909 dumpinfo.WFileName = strdup(basename(WFileName));
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001910 if (dumpinfo.WFileName == NULL) {
1911 error("Unable to allocate memory for file %s",
1912 WFileName);
1913 }
Elliott Hughes892a68b2015-10-19 14:43:53 -07001914 dumpinfo.dirfd = open(dirname(WFileName),
1915 O_DIRECTORY | O_RDONLY);
1916 if (dumpinfo.dirfd < 0) {
1917 error("unable to open directory %s",
1918 dirname(WFileName));
1919 }
1920 cap_rights_init(&rights, CAP_CREATE, CAP_FCNTL,
1921 CAP_FTRUNCATE, CAP_LOOKUP, CAP_SEEK, CAP_WRITE);
1922 if (cap_rights_limit(dumpinfo.dirfd, &rights) < 0 &&
1923 errno != ENOSYS) {
1924 error("unable to limit directory rights");
1925 }
1926 if (cap_fcntls_limit(dumpinfo.dirfd, CAP_FCNTL_GETFL) < 0 &&
1927 errno != ENOSYS) {
1928 error("unable to limit dump descriptor fcntls");
1929 }
1930#else /* !HAVE_CAPSICUM */
The Android Open Source Project2949f582009-03-03 19:30:46 -08001931 dumpinfo.WFileName = WFileName;
Elliott Hughes892a68b2015-10-19 14:43:53 -07001932#endif
1933 callback = dump_packet_and_trunc;
The Android Open Source Project2949f582009-03-03 19:30:46 -08001934 dumpinfo.pd = pd;
1935 dumpinfo.p = p;
1936 pcap_userdata = (u_char *)&dumpinfo;
1937 } else {
1938 callback = dump_packet;
1939 pcap_userdata = (u_char *)p;
1940 }
JP Abgrall53f17a92014-02-12 14:02:41 -08001941#ifdef HAVE_PCAP_DUMP_FLUSH
1942 if (Uflag)
1943 pcap_dump_flush(p);
1944#endif
The Android Open Source Project2949f582009-03-03 19:30:46 -08001945 } else {
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001946 dlt = pcap_datalink(pd);
1947 ndo->ndo_if_printer = get_if_printer(ndo, dlt);
The Android Open Source Project2949f582009-03-03 19:30:46 -08001948 callback = print_packet;
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001949 pcap_userdata = (u_char *)ndo;
The Android Open Source Project2949f582009-03-03 19:30:46 -08001950 }
JP Abgrall53f17a92014-02-12 14:02:41 -08001951
1952#ifdef SIGNAL_REQ_INFO
The Android Open Source Project2949f582009-03-03 19:30:46 -08001953 /*
JP Abgrall53f17a92014-02-12 14:02:41 -08001954 * We can't get statistics when reading from a file rather
1955 * than capturing from a device.
The Android Open Source Project2949f582009-03-03 19:30:46 -08001956 */
JP Abgrall53f17a92014-02-12 14:02:41 -08001957 if (RFileName == NULL)
1958 (void)setsignal(SIGNAL_REQ_INFO, requestinfo);
The Android Open Source Project2949f582009-03-03 19:30:46 -08001959#endif
1960
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001961 if (ndo->ndo_vflag > 0 && WFileName) {
The Android Open Source Project2949f582009-03-03 19:30:46 -08001962 /*
1963 * When capturing to a file, "-v" means tcpdump should,
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001964 * every 10 seconds, "v"erbosely report the number of
The Android Open Source Project2949f582009-03-03 19:30:46 -08001965 * packets captured.
1966 */
1967#ifdef USE_WIN32_MM_TIMER
1968 /* call verbose_stats_dump() each 1000 +/-100msec */
1969 timer_id = timeSetEvent(1000, 100, verbose_stats_dump, 0, TIME_PERIODIC);
1970 setvbuf(stderr, NULL, _IONBF, 0);
1971#elif defined(HAVE_ALARM)
1972 (void)setsignal(SIGALRM, verbose_stats_dump);
1973 alarm(1);
1974#endif
1975 }
1976
The Android Open Source Project2949f582009-03-03 19:30:46 -08001977 if (RFileName == NULL) {
JP Abgrall53f17a92014-02-12 14:02:41 -08001978 /*
1979 * Live capture (if -V was specified, we set RFileName
1980 * to a file from the -V file). Print a message to
1981 * the standard error on UN*X.
1982 */
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001983 if (!ndo->ndo_vflag && !WFileName) {
The Android Open Source Project2949f582009-03-03 19:30:46 -08001984 (void)fprintf(stderr,
1985 "%s: verbose output suppressed, use -v or -vv for full protocol decode\n",
1986 program_name);
1987 } else
1988 (void)fprintf(stderr, "%s: ", program_name);
1989 dlt = pcap_datalink(pd);
1990 dlt_name = pcap_datalink_val_to_name(dlt);
1991 if (dlt_name == NULL) {
1992 (void)fprintf(stderr, "listening on %s, link-type %u, capture size %u bytes\n",
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001993 device, dlt, ndo->ndo_snaplen);
The Android Open Source Project2949f582009-03-03 19:30:46 -08001994 } else {
1995 (void)fprintf(stderr, "listening on %s, link-type %s (%s), capture size %u bytes\n",
1996 device, dlt_name,
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001997 pcap_datalink_val_to_description(dlt), ndo->ndo_snaplen);
The Android Open Source Project2949f582009-03-03 19:30:46 -08001998 }
1999 (void)fflush(stderr);
2000 }
Elliott Hughes892a68b2015-10-19 14:43:53 -07002001
2002#ifdef HAVE_CAPSICUM
Elliott Hughese2e3bd12017-05-15 10:59:29 -07002003 cansandbox = (ndo->ndo_nflag && VFileName == NULL && zflag == NULL);
Elliott Hughes892a68b2015-10-19 14:43:53 -07002004 if (cansandbox && cap_enter() < 0 && errno != ENOSYS)
2005 error("unable to enter the capability mode");
Elliott Hughes892a68b2015-10-19 14:43:53 -07002006#endif /* HAVE_CAPSICUM */
2007
JP Abgrall53f17a92014-02-12 14:02:41 -08002008 do {
2009 status = pcap_loop(pd, cnt, callback, pcap_userdata);
2010 if (WFileName == NULL) {
The Android Open Source Project2949f582009-03-03 19:30:46 -08002011 /*
JP Abgrall53f17a92014-02-12 14:02:41 -08002012 * We're printing packets. Flush the printed output,
2013 * so it doesn't get intermingled with error output.
The Android Open Source Project2949f582009-03-03 19:30:46 -08002014 */
JP Abgrall53f17a92014-02-12 14:02:41 -08002015 if (status == -2) {
2016 /*
2017 * We got interrupted, so perhaps we didn't
2018 * manage to finish a line we were printing.
2019 * Print an extra newline, just in case.
2020 */
2021 putchar('\n');
2022 }
2023 (void)fflush(stdout);
The Android Open Source Project2949f582009-03-03 19:30:46 -08002024 }
JP Abgrall53f17a92014-02-12 14:02:41 -08002025 if (status == -2) {
2026 /*
2027 * We got interrupted. If we are reading multiple
2028 * files (via -V) set these so that we stop.
2029 */
2030 VFileName = NULL;
2031 ret = NULL;
2032 }
2033 if (status == -1) {
2034 /*
2035 * Error. Report it.
2036 */
2037 (void)fprintf(stderr, "%s: pcap_loop: %s\n",
2038 program_name, pcap_geterr(pd));
2039 }
2040 if (RFileName == NULL) {
2041 /*
2042 * We're doing a live capture. Report the capture
2043 * statistics.
2044 */
2045 info(1);
2046 }
2047 pcap_close(pd);
2048 if (VFileName != NULL) {
2049 ret = get_next_file(VFile, VFileLine);
2050 if (ret) {
Elliott Hughese2e3bd12017-05-15 10:59:29 -07002051 int new_dlt;
2052
JP Abgrall53f17a92014-02-12 14:02:41 -08002053 RFileName = VFileLine;
2054 pd = pcap_open_offline(RFileName, ebuf);
2055 if (pd == NULL)
2056 error("%s", ebuf);
Elliott Hughes892a68b2015-10-19 14:43:53 -07002057#ifdef HAVE_CAPSICUM
2058 cap_rights_init(&rights, CAP_READ);
2059 if (cap_rights_limit(fileno(pcap_file(pd)),
2060 &rights) < 0 && errno != ENOSYS) {
2061 error("unable to limit pcap descriptor");
2062 }
2063#endif
JP Abgrall53f17a92014-02-12 14:02:41 -08002064 new_dlt = pcap_datalink(pd);
Elliott Hughese2e3bd12017-05-15 10:59:29 -07002065 if (new_dlt != dlt) {
2066 /*
2067 * The new file has a different
2068 * link-layer header type from the
2069 * previous one.
2070 */
2071 if (WFileName != NULL) {
2072 /*
2073 * We're writing raw packets
2074 * that match the filter to
2075 * a pcap file. pcap files
2076 * don't support multiple
2077 * different link-layer
2078 * header types, so we fail
2079 * here.
2080 */
2081 error("%s: new dlt does not match original", RFileName);
2082 }
2083
2084 /*
2085 * We're printing the decoded packets;
2086 * switch to the new DLT.
2087 *
2088 * To do that, we need to change
2089 * the printer, change the DLT name,
2090 * and recompile the filter with
2091 * the new DLT.
2092 */
2093 dlt = new_dlt;
2094 ndo->ndo_if_printer = get_if_printer(ndo, dlt);
2095 if (pcap_compile(pd, &fcode, cmdbuf, Oflag, netmask) < 0)
2096 error("%s", pcap_geterr(pd));
2097 }
2098
2099 /*
2100 * Set the filter on the new file.
2101 */
2102 if (pcap_setfilter(pd, &fcode) < 0)
2103 error("%s", pcap_geterr(pd));
2104
2105 /*
2106 * Report the new file.
2107 */
2108 dlt_name = pcap_datalink_val_to_name(dlt);
JP Abgrall53f17a92014-02-12 14:02:41 -08002109 if (dlt_name == NULL) {
2110 fprintf(stderr, "reading from file %s, link-type %u\n",
Elliott Hughese2e3bd12017-05-15 10:59:29 -07002111 RFileName, dlt);
JP Abgrall53f17a92014-02-12 14:02:41 -08002112 } else {
2113 fprintf(stderr,
2114 "reading from file %s, link-type %s (%s)\n",
Elliott Hughese2e3bd12017-05-15 10:59:29 -07002115 RFileName, dlt_name,
2116 pcap_datalink_val_to_description(dlt));
JP Abgrall53f17a92014-02-12 14:02:41 -08002117 }
JP Abgrall53f17a92014-02-12 14:02:41 -08002118 }
2119 }
The Android Open Source Project2949f582009-03-03 19:30:46 -08002120 }
JP Abgrall53f17a92014-02-12 14:02:41 -08002121 while (ret != NULL);
2122
2123 free(cmdbuf);
Elliott Hughese2e3bd12017-05-15 10:59:29 -07002124 pcap_freecode(&fcode);
2125 exit_tcpdump(status == -1 ? 1 : 0);
The Android Open Source Project2949f582009-03-03 19:30:46 -08002126}
2127
2128/* make a clean exit on interrupts */
2129static RETSIGTYPE
2130cleanup(int signo _U_)
2131{
2132#ifdef USE_WIN32_MM_TIMER
2133 if (timer_id)
2134 timeKillEvent(timer_id);
2135 timer_id = 0;
2136#elif defined(HAVE_ALARM)
2137 alarm(0);
2138#endif
2139
2140#ifdef HAVE_PCAP_BREAKLOOP
2141 /*
2142 * We have "pcap_breakloop()"; use it, so that we do as little
2143 * as possible in the signal handler (it's probably not safe
2144 * to do anything with standard I/O streams in a signal handler -
2145 * the ANSI C standard doesn't say it is).
2146 */
2147 pcap_breakloop(pd);
2148#else
2149 /*
2150 * We don't have "pcap_breakloop()"; this isn't safe, but
2151 * it's the best we can do. Print the summary if we're
2152 * not reading from a savefile - i.e., if we're doing a
2153 * live capture - and exit.
2154 */
2155 if (pd != NULL && pcap_file(pd) == NULL) {
2156 /*
2157 * We got interrupted, so perhaps we didn't
2158 * manage to finish a line we were printing.
2159 * Print an extra newline, just in case.
2160 */
2161 putchar('\n');
2162 (void)fflush(stdout);
2163 info(1);
2164 }
Elliott Hughese2e3bd12017-05-15 10:59:29 -07002165 exit_tcpdump(0);
The Android Open Source Project2949f582009-03-03 19:30:46 -08002166#endif
2167}
2168
JP Abgrall53f17a92014-02-12 14:02:41 -08002169/*
2170 On windows, we do not use a fork, so we do not care less about
2171 waiting a child processes to die
2172 */
2173#if defined(HAVE_FORK) || defined(HAVE_VFORK)
2174static RETSIGTYPE
2175child_cleanup(int signo _U_)
2176{
2177 wait(NULL);
2178}
2179#endif /* HAVE_FORK && HAVE_VFORK */
2180
The Android Open Source Project2949f582009-03-03 19:30:46 -08002181static void
2182info(register int verbose)
2183{
Elliott Hughese2e3bd12017-05-15 10:59:29 -07002184 struct pcap_stat stats;
The Android Open Source Project2949f582009-03-03 19:30:46 -08002185
JP Abgrall53f17a92014-02-12 14:02:41 -08002186 /*
2187 * Older versions of libpcap didn't set ps_ifdrop on some
2188 * platforms; initialize it to 0 to handle that.
2189 */
Elliott Hughese2e3bd12017-05-15 10:59:29 -07002190 stats.ps_ifdrop = 0;
2191 if (pcap_stats(pd, &stats) < 0) {
The Android Open Source Project2949f582009-03-03 19:30:46 -08002192 (void)fprintf(stderr, "pcap_stats: %s\n", pcap_geterr(pd));
JP Abgrall53f17a92014-02-12 14:02:41 -08002193 infoprint = 0;
The Android Open Source Project2949f582009-03-03 19:30:46 -08002194 return;
2195 }
2196
2197 if (!verbose)
2198 fprintf(stderr, "%s: ", program_name);
2199
JP Abgrall53f17a92014-02-12 14:02:41 -08002200 (void)fprintf(stderr, "%u packet%s captured", packets_captured,
2201 PLURAL_SUFFIX(packets_captured));
The Android Open Source Project2949f582009-03-03 19:30:46 -08002202 if (!verbose)
2203 fputs(", ", stderr);
2204 else
2205 putc('\n', stderr);
Elliott Hughese2e3bd12017-05-15 10:59:29 -07002206 (void)fprintf(stderr, "%u packet%s received by filter", stats.ps_recv,
2207 PLURAL_SUFFIX(stats.ps_recv));
The Android Open Source Project2949f582009-03-03 19:30:46 -08002208 if (!verbose)
2209 fputs(", ", stderr);
2210 else
2211 putc('\n', stderr);
Elliott Hughese2e3bd12017-05-15 10:59:29 -07002212 (void)fprintf(stderr, "%u packet%s dropped by kernel", stats.ps_drop,
2213 PLURAL_SUFFIX(stats.ps_drop));
2214 if (stats.ps_ifdrop != 0) {
JP Abgrall53f17a92014-02-12 14:02:41 -08002215 if (!verbose)
2216 fputs(", ", stderr);
2217 else
2218 putc('\n', stderr);
2219 (void)fprintf(stderr, "%u packet%s dropped by interface\n",
Elliott Hughese2e3bd12017-05-15 10:59:29 -07002220 stats.ps_ifdrop, PLURAL_SUFFIX(stats.ps_ifdrop));
JP Abgrall53f17a92014-02-12 14:02:41 -08002221 } else
2222 putc('\n', stderr);
The Android Open Source Project2949f582009-03-03 19:30:46 -08002223 infoprint = 0;
2224}
2225
JP Abgrall53f17a92014-02-12 14:02:41 -08002226#if defined(HAVE_FORK) || defined(HAVE_VFORK)
Elliott Hughese2e3bd12017-05-15 10:59:29 -07002227#ifdef HAVE_FORK
2228#define fork_subprocess() fork()
2229#else
2230#define fork_subprocess() vfork()
2231#endif
JP Abgrall53f17a92014-02-12 14:02:41 -08002232static void
2233compress_savefile(const char *filename)
2234{
Elliott Hughese2e3bd12017-05-15 10:59:29 -07002235 pid_t child;
2236
2237 child = fork_subprocess();
2238 if (child == -1) {
2239 fprintf(stderr,
2240 "compress_savefile: fork failed: %s\n",
2241 pcap_strerror(errno));
JP Abgrall53f17a92014-02-12 14:02:41 -08002242 return;
Elliott Hughese2e3bd12017-05-15 10:59:29 -07002243 }
2244 if (child != 0) {
2245 /* Parent process. */
2246 return;
2247 }
2248
JP Abgrall53f17a92014-02-12 14:02:41 -08002249 /*
Elliott Hughese2e3bd12017-05-15 10:59:29 -07002250 * Child process.
2251 * Set to lowest priority so that this doesn't disturb the capture.
JP Abgrall53f17a92014-02-12 14:02:41 -08002252 */
2253#ifdef NZERO
2254 setpriority(PRIO_PROCESS, 0, NZERO - 1);
2255#else
2256 setpriority(PRIO_PROCESS, 0, 19);
2257#endif
2258 if (execlp(zflag, zflag, filename, (char *)NULL) == -1)
2259 fprintf(stderr,
Elliott Hughese2e3bd12017-05-15 10:59:29 -07002260 "compress_savefile: execlp(%s, %s) failed: %s\n",
JP Abgrall53f17a92014-02-12 14:02:41 -08002261 zflag,
2262 filename,
Elliott Hughese2e3bd12017-05-15 10:59:29 -07002263 pcap_strerror(errno));
2264#ifdef HAVE_FORK
JP Abgrall53f17a92014-02-12 14:02:41 -08002265 exit(1);
Elliott Hughese2e3bd12017-05-15 10:59:29 -07002266#else
JP Abgrall53f17a92014-02-12 14:02:41 -08002267 _exit(1);
Elliott Hughese2e3bd12017-05-15 10:59:29 -07002268#endif
JP Abgrall53f17a92014-02-12 14:02:41 -08002269}
2270#else /* HAVE_FORK && HAVE_VFORK */
2271static void
2272compress_savefile(const char *filename)
2273{
2274 fprintf(stderr,
2275 "compress_savefile failed. Functionality not implemented under your system\n");
2276}
2277#endif /* HAVE_FORK && HAVE_VFORK */
2278
The Android Open Source Project2949f582009-03-03 19:30:46 -08002279static void
2280dump_packet_and_trunc(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
2281{
2282 struct dump_info *dump_info;
The Android Open Source Project2949f582009-03-03 19:30:46 -08002283
2284 ++packets_captured;
2285
2286 ++infodelay;
2287
2288 dump_info = (struct dump_info *)user;
2289
2290 /*
JP Abgrall53f17a92014-02-12 14:02:41 -08002291 * XXX - this won't force the file to rotate on the specified time
2292 * boundary, but it will rotate on the first packet received after the
2293 * specified Gflag number of seconds. Note: if a Gflag time boundary
2294 * and a Cflag size boundary coincide, the time rotation will occur
2295 * first thereby cancelling the Cflag boundary (since the file should
2296 * be 0).
2297 */
2298 if (Gflag != 0) {
2299 /* Check if it is time to rotate */
2300 time_t t;
2301
2302 /* Get the current time */
2303 if ((t = time(NULL)) == (time_t)-1) {
2304 error("dump_and_trunc_packet: can't get current_time: %s",
2305 pcap_strerror(errno));
2306 }
2307
2308
2309 /* If the time is greater than the specified window, rotate */
2310 if (t - Gflag_time >= Gflag) {
Elliott Hughes892a68b2015-10-19 14:43:53 -07002311#ifdef HAVE_CAPSICUM
2312 FILE *fp;
2313 int fd;
2314#endif
2315
JP Abgrall53f17a92014-02-12 14:02:41 -08002316 /* Update the Gflag_time */
2317 Gflag_time = t;
2318 /* Update Gflag_count */
2319 Gflag_count++;
2320 /*
2321 * Close the current file and open a new one.
2322 */
2323 pcap_dump_close(dump_info->p);
2324
2325 /*
2326 * Compress the file we just closed, if the user asked for it
2327 */
2328 if (zflag != NULL)
2329 compress_savefile(dump_info->CurrentFileName);
2330
2331 /*
2332 * Check to see if we've exceeded the Wflag (when
2333 * not using Cflag).
2334 */
2335 if (Cflag == 0 && Wflag > 0 && Gflag_count >= Wflag) {
2336 (void)fprintf(stderr, "Maximum file limit reached: %d\n",
2337 Wflag);
Elliott Hughese2e3bd12017-05-15 10:59:29 -07002338 info(1);
2339 exit_tcpdump(0);
JP Abgrall53f17a92014-02-12 14:02:41 -08002340 /* NOTREACHED */
2341 }
2342 if (dump_info->CurrentFileName != NULL)
2343 free(dump_info->CurrentFileName);
2344 /* Allocate space for max filename + \0. */
2345 dump_info->CurrentFileName = (char *)malloc(PATH_MAX + 1);
2346 if (dump_info->CurrentFileName == NULL)
2347 error("dump_packet_and_trunc: malloc");
2348 /*
Elliott Hughes892a68b2015-10-19 14:43:53 -07002349 * Gflag was set otherwise we wouldn't be here. Reset the count
2350 * so multiple files would end with 1,2,3 in the filename.
2351 * The counting is handled with the -C flow after this.
2352 */
2353 Cflag_count = 0;
2354
2355 /*
JP Abgrall53f17a92014-02-12 14:02:41 -08002356 * This is always the first file in the Cflag
2357 * rotation: e.g. 0
2358 * We also don't need numbering if Cflag is not set.
2359 */
2360 if (Cflag != 0)
2361 MakeFilename(dump_info->CurrentFileName, dump_info->WFileName, 0,
2362 WflagChars);
2363 else
2364 MakeFilename(dump_info->CurrentFileName, dump_info->WFileName, 0, 0);
2365
Elliott Hughes892a68b2015-10-19 14:43:53 -07002366#ifdef HAVE_LIBCAP_NG
JP Abgrall53f17a92014-02-12 14:02:41 -08002367 capng_update(CAPNG_ADD, CAPNG_EFFECTIVE, CAP_DAC_OVERRIDE);
Elliott Hughes892a68b2015-10-19 14:43:53 -07002368 capng_apply(CAPNG_SELECT_BOTH);
2369#endif /* HAVE_LIBCAP_NG */
2370#ifdef HAVE_CAPSICUM
2371 fd = openat(dump_info->dirfd,
2372 dump_info->CurrentFileName,
2373 O_CREAT | O_WRONLY | O_TRUNC, 0644);
2374 if (fd < 0) {
2375 error("unable to open file %s",
2376 dump_info->CurrentFileName);
2377 }
2378 fp = fdopen(fd, "w");
2379 if (fp == NULL) {
2380 error("unable to fdopen file %s",
2381 dump_info->CurrentFileName);
2382 }
2383 dump_info->p = pcap_dump_fopen(dump_info->pd, fp);
2384#else /* !HAVE_CAPSICUM */
JP Abgrall53f17a92014-02-12 14:02:41 -08002385 dump_info->p = pcap_dump_open(dump_info->pd, dump_info->CurrentFileName);
Elliott Hughes892a68b2015-10-19 14:43:53 -07002386#endif
2387#ifdef HAVE_LIBCAP_NG
JP Abgrall53f17a92014-02-12 14:02:41 -08002388 capng_update(CAPNG_DROP, CAPNG_EFFECTIVE, CAP_DAC_OVERRIDE);
Elliott Hughes892a68b2015-10-19 14:43:53 -07002389 capng_apply(CAPNG_SELECT_BOTH);
2390#endif /* HAVE_LIBCAP_NG */
JP Abgrall53f17a92014-02-12 14:02:41 -08002391 if (dump_info->p == NULL)
2392 error("%s", pcap_geterr(pd));
Elliott Hughes892a68b2015-10-19 14:43:53 -07002393#ifdef HAVE_CAPSICUM
2394 set_dumper_capsicum_rights(dump_info->p);
2395#endif
JP Abgrall53f17a92014-02-12 14:02:41 -08002396 }
2397 }
2398
2399 /*
The Android Open Source Project2949f582009-03-03 19:30:46 -08002400 * XXX - this won't prevent capture files from getting
2401 * larger than Cflag - the last packet written to the
2402 * file could put it over Cflag.
2403 */
Elliott Hughes892a68b2015-10-19 14:43:53 -07002404 if (Cflag != 0) {
2405 long size = pcap_dump_ftell(dump_info->p);
JP Abgrall53f17a92014-02-12 14:02:41 -08002406
Elliott Hughes892a68b2015-10-19 14:43:53 -07002407 if (size == -1)
2408 error("ftell fails on output file");
2409 if (size > Cflag) {
2410#ifdef HAVE_CAPSICUM
2411 FILE *fp;
2412 int fd;
2413#endif
JP Abgrall53f17a92014-02-12 14:02:41 -08002414
Elliott Hughes892a68b2015-10-19 14:43:53 -07002415 /*
2416 * Close the current file and open a new one.
2417 */
2418 pcap_dump_close(dump_info->p);
2419
2420 /*
2421 * Compress the file we just closed, if the user
2422 * asked for it.
2423 */
2424 if (zflag != NULL)
2425 compress_savefile(dump_info->CurrentFileName);
2426
2427 Cflag_count++;
2428 if (Wflag > 0) {
2429 if (Cflag_count >= Wflag)
2430 Cflag_count = 0;
2431 }
2432 if (dump_info->CurrentFileName != NULL)
2433 free(dump_info->CurrentFileName);
2434 dump_info->CurrentFileName = (char *)malloc(PATH_MAX + 1);
2435 if (dump_info->CurrentFileName == NULL)
2436 error("dump_packet_and_trunc: malloc");
2437 MakeFilename(dump_info->CurrentFileName, dump_info->WFileName, Cflag_count, WflagChars);
2438#ifdef HAVE_LIBCAP_NG
2439 capng_update(CAPNG_ADD, CAPNG_EFFECTIVE, CAP_DAC_OVERRIDE);
2440 capng_apply(CAPNG_SELECT_BOTH);
2441#endif /* HAVE_LIBCAP_NG */
2442#ifdef HAVE_CAPSICUM
2443 fd = openat(dump_info->dirfd, dump_info->CurrentFileName,
2444 O_CREAT | O_WRONLY | O_TRUNC, 0644);
2445 if (fd < 0) {
2446 error("unable to open file %s",
2447 dump_info->CurrentFileName);
2448 }
2449 fp = fdopen(fd, "w");
2450 if (fp == NULL) {
2451 error("unable to fdopen file %s",
2452 dump_info->CurrentFileName);
2453 }
2454 dump_info->p = pcap_dump_fopen(dump_info->pd, fp);
2455#else /* !HAVE_CAPSICUM */
2456 dump_info->p = pcap_dump_open(dump_info->pd, dump_info->CurrentFileName);
2457#endif
2458#ifdef HAVE_LIBCAP_NG
2459 capng_update(CAPNG_DROP, CAPNG_EFFECTIVE, CAP_DAC_OVERRIDE);
2460 capng_apply(CAPNG_SELECT_BOTH);
2461#endif /* HAVE_LIBCAP_NG */
2462 if (dump_info->p == NULL)
2463 error("%s", pcap_geterr(pd));
2464#ifdef HAVE_CAPSICUM
2465 set_dumper_capsicum_rights(dump_info->p);
2466#endif
The Android Open Source Project2949f582009-03-03 19:30:46 -08002467 }
The Android Open Source Project2949f582009-03-03 19:30:46 -08002468 }
2469
2470 pcap_dump((u_char *)dump_info->p, h, sp);
2471#ifdef HAVE_PCAP_DUMP_FLUSH
2472 if (Uflag)
2473 pcap_dump_flush(dump_info->p);
2474#endif
2475
2476 --infodelay;
2477 if (infoprint)
2478 info(0);
2479}
2480
2481static void
2482dump_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
2483{
2484 ++packets_captured;
2485
2486 ++infodelay;
2487
2488 pcap_dump(user, h, sp);
2489#ifdef HAVE_PCAP_DUMP_FLUSH
2490 if (Uflag)
2491 pcap_dump_flush((pcap_dumper_t *)user);
2492#endif
2493
2494 --infodelay;
2495 if (infoprint)
2496 info(0);
2497}
2498
2499static void
2500print_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
2501{
The Android Open Source Project2949f582009-03-03 19:30:46 -08002502 ++packets_captured;
2503
2504 ++infodelay;
The Android Open Source Project2949f582009-03-03 19:30:46 -08002505
Elliott Hughese2e3bd12017-05-15 10:59:29 -07002506 pretty_print_packet((netdissect_options *)user, h, sp, packets_captured);
The Android Open Source Project2949f582009-03-03 19:30:46 -08002507
2508 --infodelay;
2509 if (infoprint)
2510 info(0);
2511}
2512
Elliott Hughese2e3bd12017-05-15 10:59:29 -07002513#ifdef _WIN32
The Android Open Source Project2949f582009-03-03 19:30:46 -08002514 /*
2515 * XXX - there should really be libpcap calls to get the version
2516 * number as a string (the string would be generated from #defines
2517 * at run time, so that it's not generated from string constants
2518 * in the library, as, on many UNIX systems, those constants would
2519 * be statically linked into the application executable image, and
2520 * would thus reflect the version of libpcap on the system on
2521 * which the application was *linked*, not the system on which it's
2522 * *running*.
2523 *
2524 * That routine should be documented, unlike the "version[]"
2525 * string, so that UNIX vendors providing their own libpcaps
2526 * don't omit it (as a couple of vendors have...).
2527 *
2528 * Packet.dll should perhaps also export a routine to return the
2529 * version number of the Packet.dll code, to supply the
2530 * "Wpcap_version" information on Windows.
2531 */
Elliott Hughes892a68b2015-10-19 14:43:53 -07002532 char WDversion[]="current-git.tcpdump.org";
The Android Open Source Project2949f582009-03-03 19:30:46 -08002533#if !defined(HAVE_GENERATED_VERSION)
Elliott Hughes892a68b2015-10-19 14:43:53 -07002534 char version[]="current-git.tcpdump.org";
The Android Open Source Project2949f582009-03-03 19:30:46 -08002535#endif
Elliott Hughes892a68b2015-10-19 14:43:53 -07002536 char pcap_version[]="current-git.tcpdump.org";
The Android Open Source Project2949f582009-03-03 19:30:46 -08002537 char Wpcap_version[]="3.1";
2538#endif
2539
JP Abgrall53f17a92014-02-12 14:02:41 -08002540#ifdef SIGNAL_REQ_INFO
The Android Open Source Project2949f582009-03-03 19:30:46 -08002541RETSIGTYPE requestinfo(int signo _U_)
2542{
2543 if (infodelay)
2544 ++infoprint;
2545 else
2546 info(0);
2547}
2548#endif
2549
2550/*
2551 * Called once each second in verbose mode while dumping to file
2552 */
2553#ifdef USE_WIN32_MM_TIMER
2554void CALLBACK verbose_stats_dump (UINT timer_id _U_, UINT msg _U_, DWORD_PTR arg _U_,
JP Abgrall53f17a92014-02-12 14:02:41 -08002555 DWORD_PTR dw1 _U_, DWORD_PTR dw2 _U_)
The Android Open Source Project2949f582009-03-03 19:30:46 -08002556{
Elliott Hughese2e3bd12017-05-15 10:59:29 -07002557 if (infodelay == 0)
The Android Open Source Project2949f582009-03-03 19:30:46 -08002558 fprintf(stderr, "Got %u\r", packets_captured);
2559}
2560#elif defined(HAVE_ALARM)
2561static void verbose_stats_dump(int sig _U_)
2562{
Elliott Hughese2e3bd12017-05-15 10:59:29 -07002563 if (infodelay == 0)
The Android Open Source Project2949f582009-03-03 19:30:46 -08002564 fprintf(stderr, "Got %u\r", packets_captured);
2565 alarm(1);
2566}
2567#endif
2568
Elliott Hughes892a68b2015-10-19 14:43:53 -07002569USES_APPLE_DEPRECATED_API
The Android Open Source Project2949f582009-03-03 19:30:46 -08002570static void
Elliott Hughes892a68b2015-10-19 14:43:53 -07002571print_version(void)
The Android Open Source Project2949f582009-03-03 19:30:46 -08002572{
2573 extern char version[];
2574#ifndef HAVE_PCAP_LIB_VERSION
Elliott Hughese2e3bd12017-05-15 10:59:29 -07002575#if defined(_WIN32) || defined(HAVE_PCAP_VERSION)
The Android Open Source Project2949f582009-03-03 19:30:46 -08002576 extern char pcap_version[];
Elliott Hughese2e3bd12017-05-15 10:59:29 -07002577#else /* defined(_WIN32) || defined(HAVE_PCAP_VERSION) */
The Android Open Source Project2949f582009-03-03 19:30:46 -08002578 static char pcap_version[] = "unknown";
Elliott Hughese2e3bd12017-05-15 10:59:29 -07002579#endif /* defined(_WIN32) || defined(HAVE_PCAP_VERSION) */
The Android Open Source Project2949f582009-03-03 19:30:46 -08002580#endif /* HAVE_PCAP_LIB_VERSION */
Elliott Hughese2e3bd12017-05-15 10:59:29 -07002581 const char *smi_version_string;
The Android Open Source Project2949f582009-03-03 19:30:46 -08002582
2583#ifdef HAVE_PCAP_LIB_VERSION
Elliott Hughese2e3bd12017-05-15 10:59:29 -07002584#ifdef _WIN32
The Android Open Source Project2949f582009-03-03 19:30:46 -08002585 (void)fprintf(stderr, "%s version %s, based on tcpdump version %s\n", program_name, WDversion, version);
Elliott Hughese2e3bd12017-05-15 10:59:29 -07002586#else /* _WIN32 */
The Android Open Source Project2949f582009-03-03 19:30:46 -08002587 (void)fprintf(stderr, "%s version %s\n", program_name, version);
Elliott Hughese2e3bd12017-05-15 10:59:29 -07002588#endif /* _WIN32 */
The Android Open Source Project2949f582009-03-03 19:30:46 -08002589 (void)fprintf(stderr, "%s\n",pcap_lib_version());
2590#else /* HAVE_PCAP_LIB_VERSION */
Elliott Hughese2e3bd12017-05-15 10:59:29 -07002591#ifdef _WIN32
The Android Open Source Project2949f582009-03-03 19:30:46 -08002592 (void)fprintf(stderr, "%s version %s, based on tcpdump version %s\n", program_name, WDversion, version);
2593 (void)fprintf(stderr, "WinPcap version %s, based on libpcap version %s\n",Wpcap_version, pcap_version);
Elliott Hughese2e3bd12017-05-15 10:59:29 -07002594#else /* _WIN32 */
The Android Open Source Project2949f582009-03-03 19:30:46 -08002595 (void)fprintf(stderr, "%s version %s\n", program_name, version);
2596 (void)fprintf(stderr, "libpcap version %s\n", pcap_version);
Elliott Hughese2e3bd12017-05-15 10:59:29 -07002597#endif /* _WIN32 */
The Android Open Source Project2949f582009-03-03 19:30:46 -08002598#endif /* HAVE_PCAP_LIB_VERSION */
Elliott Hughes892a68b2015-10-19 14:43:53 -07002599
2600#if defined(HAVE_LIBCRYPTO) && defined(SSLEAY_VERSION)
2601 (void)fprintf (stderr, "%s\n", SSLeay_version(SSLEAY_VERSION));
2602#endif
2603
Elliott Hughese2e3bd12017-05-15 10:59:29 -07002604 smi_version_string = nd_smi_version_string();
2605 if (smi_version_string != NULL)
2606 (void)fprintf (stderr, "SMI-library: %s\n", smi_version_string);
Elliott Hughes9a986422017-12-19 14:49:10 -08002607
2608#if defined(__SANITIZE_ADDRESS__)
2609 (void)fprintf (stderr, "Compiled with AddressSanitizer/GCC.\n");
2610#elif defined(__has_feature)
2611# if __has_feature(address_sanitizer)
2612 (void)fprintf (stderr, "Compiled with AddressSanitizer/CLang.\n");
2613# endif
2614#endif /* __SANITIZE_ADDRESS__ or __has_feature */
Elliott Hughes892a68b2015-10-19 14:43:53 -07002615}
2616USES_APPLE_RST
2617
2618static void
2619print_usage(void)
2620{
2621 print_version();
The Android Open Source Project2949f582009-03-03 19:30:46 -08002622 (void)fprintf(stderr,
Elliott Hughese2e3bd12017-05-15 10:59:29 -07002623"Usage: %s [-aAbd" D_FLAG "efhH" I_FLAG J_FLAG "KlLnNOpqStu" U_FLAG "vxX#]" B_FLAG_USAGE " [ -c count ]\n", program_name);
The Android Open Source Project2949f582009-03-03 19:30:46 -08002624 (void)fprintf(stderr,
JP Abgrall53f17a92014-02-12 14:02:41 -08002625"\t\t[ -C file_size ] [ -E algo:secret ] [ -F file ] [ -G seconds ]\n");
The Android Open Source Project2949f582009-03-03 19:30:46 -08002626 (void)fprintf(stderr,
Elliott Hughes892a68b2015-10-19 14:43:53 -07002627"\t\t[ -i interface ]" j_FLAG_USAGE " [ -M secret ] [ --number ]\n");
JP Abgrall53f17a92014-02-12 14:02:41 -08002628#ifdef HAVE_PCAP_SETDIRECTION
The Android Open Source Project2949f582009-03-03 19:30:46 -08002629 (void)fprintf(stderr,
JP Abgrall53f17a92014-02-12 14:02:41 -08002630"\t\t[ -Q in|out|inout ]\n");
2631#endif
The Android Open Source Project2949f582009-03-03 19:30:46 -08002632 (void)fprintf(stderr,
Elliott Hughes892a68b2015-10-19 14:43:53 -07002633"\t\t[ -r file ] [ -s snaplen ] ");
2634#ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
2635 (void)fprintf(stderr, "[ --time-stamp-precision precision ]\n");
JP Abgrall53f17a92014-02-12 14:02:41 -08002636 (void)fprintf(stderr,
Elliott Hughes892a68b2015-10-19 14:43:53 -07002637"\t\t");
2638#endif
2639#ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
2640 (void)fprintf(stderr, "[ --immediate-mode ] ");
2641#endif
2642 (void)fprintf(stderr, "[ -T type ] [ --version ] [ -V file ]\n");
2643 (void)fprintf(stderr,
Elliott Hughese2e3bd12017-05-15 10:59:29 -07002644"\t\t[ -w file ] [ -W filecount ] [ -y datalinktype ] [ -z postrotate-command ]\n");
JP Abgrall53f17a92014-02-12 14:02:41 -08002645 (void)fprintf(stderr,
2646"\t\t[ -Z user ] [ expression ]\n");
The Android Open Source Project2949f582009-03-03 19:30:46 -08002647}
Elliott Hughes892a68b2015-10-19 14:43:53 -07002648/*
2649 * Local Variables:
2650 * c-style: whitesmith
2651 * c-basic-offset: 8
2652 * End:
2653 */