]> The Tcpdump Group git mirrors - tcpdump/blob - tcpdump.c
WHOIS is a plain text protocol, why not decode it.
[tcpdump] / tcpdump.c
1 /*
2 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that: (1) source code distributions
7 * retain the above copyright notice and this paragraph in its entirety, (2)
8 * distributions including binary code include the above copyright notice and
9 * this paragraph in its entirety in the documentation or other materials
10 * provided with the distribution, and (3) all advertising materials mentioning
11 * features or use of this software display the following acknowledgement:
12 * ``This product includes software developed by the University of California,
13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14 * the University nor the names of its contributors may be used to endorse
15 * or promote products derived from this software without specific prior
16 * written permission.
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 *
21 * Support for splitting captures into multiple files with a maximum
22 * file size:
23 *
24 * Copyright (c) 2001
25 * Seth Webster <swebster@sst.ll.mit.edu>
26 */
27
28 #ifndef lint
29 static const char copyright[] _U_ =
30 "@(#) Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000\n\
31 The Regents of the University of California. All rights reserved.\n";
32 #endif
33
34 /*
35 * tcpdump - dump traffic on a network
36 *
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
46 /*
47 * Some older versions of Mac OS X may ship pcap.h from libpcap 0.6 with a
48 * libpcap based on 0.8. That means it has pcap_findalldevs() but the
49 * header doesn't define pcap_if_t, meaning that we can't actually *use*
50 * pcap_findalldevs().
51 */
52 #ifdef HAVE_PCAP_FINDALLDEVS
53 #ifndef HAVE_PCAP_IF_T
54 #undef HAVE_PCAP_FINDALLDEVS
55 #endif
56 #endif
57
58 #include <netdissect-stdinc.h>
59
60 #include <sys/stat.h>
61
62 #ifdef HAVE_FCNTL_H
63 #include <fcntl.h>
64 #endif
65
66 #ifdef HAVE_LIBCRYPTO
67 #include <openssl/crypto.h>
68 #endif
69
70 #ifdef HAVE_GETOPT_LONG
71 #include <getopt.h>
72 #else
73 #include "getopt_long.h"
74 #endif
75 /* Capsicum-specific code requires macros from <net/bpf.h>, which will fail
76 * to compile if <pcap.h> has already been included; including the headers
77 * in the opposite order works fine.
78 */
79 #ifdef HAVE_CAPSICUM
80 #include <sys/capability.h>
81 #include <sys/ioccom.h>
82 #include <net/bpf.h>
83 #include <libgen.h>
84 #ifdef HAVE_CASPER
85 #include <libcasper.h>
86 #include <casper/cap_dns.h>
87 #include <sys/nv.h>
88 #endif /* HAVE_CASPER */
89 #endif /* HAVE_CAPSICUM */
90 #include <pcap.h>
91 #include <signal.h>
92 #include <stdio.h>
93 #include <stdarg.h>
94 #include <stdlib.h>
95 #include <string.h>
96 #include <limits.h>
97 #ifndef _WIN32
98 #include <sys/wait.h>
99 #include <sys/resource.h>
100 #include <pwd.h>
101 #include <grp.h>
102 #endif /* _WIN32 */
103
104 /* capabilities convenience library */
105 /* If a code depends on HAVE_LIBCAP_NG, it depends also on HAVE_CAP_NG_H.
106 * If HAVE_CAP_NG_H is not defined, undefine HAVE_LIBCAP_NG.
107 * Thus, the later tests are done only on HAVE_LIBCAP_NG.
108 */
109 #ifdef HAVE_LIBCAP_NG
110 #ifdef HAVE_CAP_NG_H
111 #include <cap-ng.h>
112 #else
113 #undef HAVE_LIBCAP_NG
114 #endif /* HAVE_CAP_NG_H */
115 #endif /* HAVE_LIBCAP_NG */
116
117 #ifdef __FreeBSD__
118 #include <sys/sysctl.h>
119 #endif /* __FreeBSD__ */
120
121 #include "netdissect.h"
122 #include "interface.h"
123 #include "addrtoname.h"
124 #include "machdep.h"
125 #include "setsignal.h"
126 #include "gmt2local.h"
127 #include "pcap-missing.h"
128 #include "ascii_strcasecmp.h"
129
130 #include "print.h"
131
132 #ifndef PATH_MAX
133 #define PATH_MAX 1024
134 #endif
135
136 #ifdef SIGINFO
137 #define SIGNAL_REQ_INFO SIGINFO
138 #elif SIGUSR1
139 #define SIGNAL_REQ_INFO SIGUSR1
140 #endif
141
142 static int Bflag; /* buffer size */
143 #ifdef HAVE_PCAP_FTELL64
144 static int64_t Cflag; /* rotate dump files after this many bytes */
145 #else
146 static long Cflag; /* rotate dump files after this many bytes */
147 #endif
148 static int Cflag_count; /* Keep track of which file number we're writing */
149 static int Dflag; /* list available devices and exit */
150 /*
151 * This is exported because, in some versions of libpcap, if libpcap
152 * is built with optimizer debugging code (which is *NOT* the default
153 * configuration!), the library *imports*(!) a variable named dflag,
154 * under the expectation that tcpdump is exporting it, to govern
155 * how much debugging information to print when optimizing
156 * the generated BPF code.
157 *
158 * This is a horrible hack; newer versions of libpcap don't import
159 * dflag but, instead, *if* built with optimizer debugging code,
160 * *export* a routine to set that flag.
161 */
162 int dflag; /* print filter code */
163 static int Gflag; /* rotate dump files after this many seconds */
164 static int Gflag_count; /* number of files created with Gflag rotation */
165 static time_t Gflag_time; /* The last time_t the dump file was rotated. */
166 static int Lflag; /* list available data link types and exit */
167 static int Iflag; /* rfmon (monitor) mode */
168 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
169 static int Jflag; /* list available time stamp types */
170 #endif
171 static int jflag = -1; /* packet time stamp source */
172 static int pflag; /* don't go promiscuous */
173 #ifdef HAVE_PCAP_SETDIRECTION
174 static int Qflag = -1; /* restrict captured packet by send/receive direction */
175 #endif
176 static int Uflag; /* "unbuffered" output of dump files */
177 static int Wflag; /* recycle output files after this number of files */
178 static int WflagChars;
179 static char *zflag = NULL; /* compress each savefile using a specified command (like gzip or bzip2) */
180 static int immediate_mode;
181
182 static int infodelay;
183 static int infoprint;
184
185 char *program_name;
186
187 #ifdef HAVE_CASPER
188 cap_channel_t *capdns;
189 #endif
190
191 /* Forwards */
192 static NORETURN void error(FORMAT_STRING(const char *), ...) PRINTFLIKE(1, 2);
193 static void warning(FORMAT_STRING(const char *), ...) PRINTFLIKE(1, 2);
194 static NORETURN void exit_tcpdump(int);
195 static RETSIGTYPE cleanup(int);
196 static RETSIGTYPE child_cleanup(int);
197 static void print_version(void);
198 static void print_usage(void);
199 static NORETURN void show_tstamp_types_and_exit(pcap_t *, const char *device);
200 static NORETURN void show_dlts_and_exit(pcap_t *, const char *device);
201 #ifdef HAVE_PCAP_FINDALLDEVS
202 static NORETURN void show_devices_and_exit(void);
203 #endif
204
205 static void print_packet(u_char *, const struct pcap_pkthdr *, const u_char *);
206 static void dump_packet_and_trunc(u_char *, const struct pcap_pkthdr *, const u_char *);
207 static void dump_packet(u_char *, const struct pcap_pkthdr *, const u_char *);
208 static void droproot(const char *, const char *);
209
210 #ifdef SIGNAL_REQ_INFO
211 RETSIGTYPE requestinfo(int);
212 #endif
213
214 #if defined(USE_WIN32_MM_TIMER)
215 #include <MMsystem.h>
216 static UINT timer_id;
217 static void CALLBACK verbose_stats_dump(UINT, UINT, DWORD_PTR, DWORD_PTR, DWORD_PTR);
218 #elif defined(HAVE_ALARM)
219 static void verbose_stats_dump(int sig);
220 #endif
221
222 static void info(int);
223 static u_int packets_captured;
224
225 #ifdef HAVE_PCAP_FINDALLDEVS
226 static const struct tok status_flags[] = {
227 #ifdef PCAP_IF_UP
228 { PCAP_IF_UP, "Up" },
229 #endif
230 #ifdef PCAP_IF_RUNNING
231 { PCAP_IF_RUNNING, "Running" },
232 #endif
233 { PCAP_IF_LOOPBACK, "Loopback" },
234 { 0, NULL }
235 };
236 #endif
237
238 static pcap_t *pd;
239
240 static int supports_monitor_mode;
241
242 extern int optind;
243 extern int opterr;
244 extern char *optarg;
245
246 struct dump_info {
247 char *WFileName;
248 char *CurrentFileName;
249 pcap_t *pd;
250 pcap_dumper_t *p;
251 #ifdef HAVE_CAPSICUM
252 int dirfd;
253 #endif
254 };
255
256 #if defined(HAVE_PCAP_SET_PARSER_DEBUG)
257 /*
258 * We have pcap_set_parser_debug() in libpcap; declare it (it's not declared
259 * by any libpcap header, because it's a special hack, only available if
260 * libpcap was configured to include it, and only intended for use by
261 * libpcap developers trying to debug the parser for filter expressions).
262 */
263 #ifdef _WIN32
264 __declspec(dllimport)
265 #else /* _WIN32 */
266 extern
267 #endif /* _WIN32 */
268 void pcap_set_parser_debug(int);
269 #elif defined(HAVE_PCAP_DEBUG) || defined(HAVE_YYDEBUG)
270 /*
271 * We don't have pcap_set_parser_debug() in libpcap, but we do have
272 * pcap_debug or yydebug. Make a local version of pcap_set_parser_debug()
273 * to set the flag, and define HAVE_PCAP_SET_PARSER_DEBUG.
274 */
275 static void
276 pcap_set_parser_debug(int value)
277 {
278 #ifdef HAVE_PCAP_DEBUG
279 extern int pcap_debug;
280
281 pcap_debug = value;
282 #else /* HAVE_PCAP_DEBUG */
283 extern int yydebug;
284
285 yydebug = value;
286 #endif /* HAVE_PCAP_DEBUG */
287 }
288
289 #define HAVE_PCAP_SET_PARSER_DEBUG
290 #endif
291
292 #if defined(HAVE_PCAP_SET_OPTIMIZER_DEBUG)
293 /*
294 * We have pcap_set_optimizer_debug() in libpcap; declare it (it's not declared
295 * by any libpcap header, because it's a special hack, only available if
296 * libpcap was configured to include it, and only intended for use by
297 * libpcap developers trying to debug the optimizer for filter expressions).
298 */
299 #ifdef _WIN32
300 __declspec(dllimport)
301 #else /* _WIN32 */
302 extern
303 #endif /* _WIN32 */
304 void pcap_set_optimizer_debug(int);
305 #endif
306
307 /* VARARGS */
308 static void
309 error(const char *fmt, ...)
310 {
311 va_list ap;
312
313 (void)fprintf(stderr, "%s: ", program_name);
314 va_start(ap, fmt);
315 (void)vfprintf(stderr, fmt, ap);
316 va_end(ap);
317 if (*fmt) {
318 fmt += strlen(fmt);
319 if (fmt[-1] != '\n')
320 (void)fputc('\n', stderr);
321 }
322 exit_tcpdump(1);
323 /* NOTREACHED */
324 }
325
326 /* VARARGS */
327 static void
328 warning(const char *fmt, ...)
329 {
330 va_list ap;
331
332 (void)fprintf(stderr, "%s: WARNING: ", program_name);
333 va_start(ap, fmt);
334 (void)vfprintf(stderr, fmt, ap);
335 va_end(ap);
336 if (*fmt) {
337 fmt += strlen(fmt);
338 if (fmt[-1] != '\n')
339 (void)fputc('\n', stderr);
340 }
341 }
342
343 static void
344 exit_tcpdump(int status)
345 {
346 nd_cleanup();
347 exit(status);
348 }
349
350 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
351 static void
352 show_tstamp_types_and_exit(pcap_t *pc, const char *device)
353 {
354 int n_tstamp_types;
355 int *tstamp_types = 0;
356 const char *tstamp_type_name;
357 int i;
358
359 n_tstamp_types = pcap_list_tstamp_types(pc, &tstamp_types);
360 if (n_tstamp_types < 0)
361 error("%s", pcap_geterr(pc));
362
363 if (n_tstamp_types == 0) {
364 fprintf(stderr, "Time stamp type cannot be set for %s\n",
365 device);
366 exit_tcpdump(0);
367 }
368 fprintf(stderr, "Time stamp types for %s (use option -j to set):\n",
369 device);
370 for (i = 0; i < n_tstamp_types; i++) {
371 tstamp_type_name = pcap_tstamp_type_val_to_name(tstamp_types[i]);
372 if (tstamp_type_name != NULL) {
373 (void) fprintf(stderr, " %s (%s)\n", tstamp_type_name,
374 pcap_tstamp_type_val_to_description(tstamp_types[i]));
375 } else {
376 (void) fprintf(stderr, " %d\n", tstamp_types[i]);
377 }
378 }
379 pcap_free_tstamp_types(tstamp_types);
380 exit_tcpdump(0);
381 }
382 #endif
383
384 static void
385 show_dlts_and_exit(pcap_t *pc, const char *device)
386 {
387 int n_dlts, i;
388 int *dlts = 0;
389 const char *dlt_name;
390
391 n_dlts = pcap_list_datalinks(pc, &dlts);
392 if (n_dlts < 0)
393 error("%s", pcap_geterr(pc));
394 else if (n_dlts == 0 || !dlts)
395 error("No data link types.");
396
397 /*
398 * If the interface is known to support monitor mode, indicate
399 * whether these are the data link types available when not in
400 * monitor mode, if -I wasn't specified, or when in monitor mode,
401 * when -I was specified (the link-layer types available in
402 * monitor mode might be different from the ones available when
403 * not in monitor mode).
404 */
405 if (supports_monitor_mode)
406 (void) fprintf(stderr, "Data link types for %s %s (use option -y to set):\n",
407 device,
408 Iflag ? "when in monitor mode" : "when not in monitor mode");
409 else
410 (void) fprintf(stderr, "Data link types for %s (use option -y to set):\n",
411 device);
412
413 for (i = 0; i < n_dlts; i++) {
414 dlt_name = pcap_datalink_val_to_name(dlts[i]);
415 if (dlt_name != NULL) {
416 (void) fprintf(stderr, " %s (%s)", dlt_name,
417 pcap_datalink_val_to_description(dlts[i]));
418
419 /*
420 * OK, does tcpdump handle that type?
421 */
422 if (!has_printer(dlts[i]))
423 (void) fprintf(stderr, " (printing not supported)");
424 fprintf(stderr, "\n");
425 } else {
426 (void) fprintf(stderr, " DLT %d (printing not supported)\n",
427 dlts[i]);
428 }
429 }
430 #ifdef HAVE_PCAP_FREE_DATALINKS
431 pcap_free_datalinks(dlts);
432 #endif
433 exit_tcpdump(0);
434 }
435
436 #ifdef HAVE_PCAP_FINDALLDEVS
437 static void
438 show_devices_and_exit (void)
439 {
440 pcap_if_t *dev, *devlist;
441 char ebuf[PCAP_ERRBUF_SIZE];
442 int i;
443
444 if (pcap_findalldevs(&devlist, ebuf) < 0)
445 error("%s", ebuf);
446 for (i = 0, dev = devlist; dev != NULL; i++, dev = dev->next) {
447 printf("%d.%s", i+1, dev->name);
448 if (dev->description != NULL)
449 printf(" (%s)", dev->description);
450 if (dev->flags != 0)
451 printf(" [%s]", bittok2str(status_flags, "none", dev->flags));
452 printf("\n");
453 }
454 pcap_freealldevs(devlist);
455 exit_tcpdump(0);
456 }
457 #endif /* HAVE_PCAP_FINDALLDEVS */
458
459 /*
460 * Short options.
461 *
462 * Note that there we use all letters for short options except for g, k,
463 * o, and P, and those are used by other versions of tcpdump, and we should
464 * only use them for the same purposes that the other versions of tcpdump
465 * use them:
466 *
467 * OS X tcpdump uses -g to force non--v output for IP to be on one
468 * line, making it more "g"repable;
469 *
470 * OS X tcpdump uses -k to specify that packet comments in pcap-ng files
471 * should be printed;
472 *
473 * OpenBSD tcpdump uses -o to indicate that OS fingerprinting should be done
474 * for hosts sending TCP SYN packets;
475 *
476 * OS X tcpdump uses -P to indicate that -w should write pcap-ng rather
477 * than pcap files.
478 *
479 * OS X tcpdump also uses -Q to specify expressions that match packet
480 * metadata, including but not limited to the packet direction.
481 * The expression syntax is different from a simple "in|out|inout",
482 * and those expressions aren't accepted by OS X tcpdump, but the
483 * equivalents would be "in" = "dir=in", "out" = "dir=out", and
484 * "inout" = "dir=in or dir=out", and the parser could conceivably
485 * special-case "in", "out", and "inout" as expressions for backwards
486 * compatibility, so all is not (yet) lost.
487 */
488
489 /*
490 * Set up flags that might or might not be supported depending on the
491 * version of libpcap we're using.
492 */
493 #if defined(HAVE_PCAP_CREATE) || defined(_WIN32)
494 #define B_FLAG "B:"
495 #define B_FLAG_USAGE " [ -B size ]"
496 #else /* defined(HAVE_PCAP_CREATE) || defined(_WIN32) */
497 #define B_FLAG
498 #define B_FLAG_USAGE
499 #endif /* defined(HAVE_PCAP_CREATE) || defined(_WIN32) */
500
501 #ifdef HAVE_PCAP_CREATE
502 #define I_FLAG "I"
503 #else /* HAVE_PCAP_CREATE */
504 #define I_FLAG
505 #endif /* HAVE_PCAP_CREATE */
506
507 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
508 #define j_FLAG "j:"
509 #define j_FLAG_USAGE " [ -j tstamptype ]"
510 #define J_FLAG "J"
511 #else /* PCAP_ERROR_TSTAMP_TYPE_NOTSUP */
512 #define j_FLAG
513 #define j_FLAG_USAGE
514 #define J_FLAG
515 #endif /* PCAP_ERROR_TSTAMP_TYPE_NOTSUP */
516
517 #ifdef HAVE_PCAP_FINDALLDEVS
518 #define D_FLAG "D"
519 #else
520 #define D_FLAG
521 #endif
522
523 #ifdef HAVE_PCAP_DUMP_FLUSH
524 #define U_FLAG "U"
525 #else
526 #define U_FLAG
527 #endif
528
529 #ifdef HAVE_PCAP_SETDIRECTION
530 #define Q_FLAG "Q:"
531 #else
532 #define Q_FLAG
533 #endif
534
535 #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:#"
536
537 /*
538 * Long options.
539 *
540 * We do not currently have long options corresponding to all short
541 * options; we should probably pick appropriate option names for them.
542 *
543 * However, the short options where the number of times the option is
544 * specified matters, such as -v and -d and -t, should probably not
545 * just map to a long option, as saying
546 *
547 * tcpdump --verbose --verbose
548 *
549 * doesn't make sense; it should be --verbosity={N} or something such
550 * as that.
551 *
552 * For long options with no corresponding short options, we define values
553 * outside the range of ASCII graphic characters, make that the last
554 * component of the entry for the long option, and have a case for that
555 * option in the switch statement.
556 */
557 #define OPTION_VERSION 128
558 #define OPTION_TSTAMP_PRECISION 129
559 #define OPTION_IMMEDIATE_MODE 130
560
561 static const struct option longopts[] = {
562 #if defined(HAVE_PCAP_CREATE) || defined(_WIN32)
563 { "buffer-size", required_argument, NULL, 'B' },
564 #endif
565 { "list-interfaces", no_argument, NULL, 'D' },
566 { "help", no_argument, NULL, 'h' },
567 { "interface", required_argument, NULL, 'i' },
568 #ifdef HAVE_PCAP_CREATE
569 { "monitor-mode", no_argument, NULL, 'I' },
570 #endif
571 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
572 { "time-stamp-type", required_argument, NULL, 'j' },
573 { "list-time-stamp-types", no_argument, NULL, 'J' },
574 #endif
575 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
576 { "time-stamp-precision", required_argument, NULL, OPTION_TSTAMP_PRECISION},
577 #endif
578 { "dont-verify-checksums", no_argument, NULL, 'K' },
579 { "list-data-link-types", no_argument, NULL, 'L' },
580 { "no-optimize", no_argument, NULL, 'O' },
581 { "no-promiscuous-mode", no_argument, NULL, 'p' },
582 #ifdef HAVE_PCAP_SETDIRECTION
583 { "direction", required_argument, NULL, 'Q' },
584 #endif
585 { "snapshot-length", required_argument, NULL, 's' },
586 { "absolute-tcp-sequence-numbers", no_argument, NULL, 'S' },
587 #ifdef HAVE_PCAP_DUMP_FLUSH
588 { "packet-buffered", no_argument, NULL, 'U' },
589 #endif
590 { "linktype", required_argument, NULL, 'y' },
591 #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
592 { "immediate-mode", no_argument, NULL, OPTION_IMMEDIATE_MODE },
593 #endif
594 #ifdef HAVE_PCAP_SET_PARSER_DEBUG
595 { "debug-filter-parser", no_argument, NULL, 'Y' },
596 #endif
597 { "relinquish-privileges", required_argument, NULL, 'Z' },
598 { "number", no_argument, NULL, '#' },
599 { "version", no_argument, NULL, OPTION_VERSION },
600 { NULL, 0, NULL, 0 }
601 };
602
603 #ifndef _WIN32
604 /* Drop root privileges and chroot if necessary */
605 static void
606 droproot(const char *username, const char *chroot_dir)
607 {
608 struct passwd *pw = NULL;
609
610 if (chroot_dir && !username) {
611 fprintf(stderr, "%s: Chroot without dropping root is insecure\n",
612 program_name);
613 exit_tcpdump(1);
614 }
615
616 pw = getpwnam(username);
617 if (pw) {
618 if (chroot_dir) {
619 if (chroot(chroot_dir) != 0 || chdir ("/") != 0) {
620 fprintf(stderr, "%s: Couldn't chroot/chdir to '%.64s': %s\n",
621 program_name, chroot_dir, pcap_strerror(errno));
622 exit_tcpdump(1);
623 }
624 }
625 #ifdef HAVE_LIBCAP_NG
626 {
627 int ret = capng_change_id(pw->pw_uid, pw->pw_gid, CAPNG_NO_FLAG);
628 if (ret < 0) {
629 fprintf(stderr, "error : ret %d\n", ret);
630 } else {
631 fprintf(stderr, "dropped privs to %s\n", username);
632 }
633 }
634 #else
635 if (initgroups(pw->pw_name, pw->pw_gid) != 0 ||
636 setgid(pw->pw_gid) != 0 || setuid(pw->pw_uid) != 0) {
637 fprintf(stderr, "%s: Couldn't change to '%.32s' uid=%lu gid=%lu: %s\n",
638 program_name, username,
639 (unsigned long)pw->pw_uid,
640 (unsigned long)pw->pw_gid,
641 pcap_strerror(errno));
642 exit_tcpdump(1);
643 }
644 else {
645 fprintf(stderr, "dropped privs to %s\n", username);
646 }
647 #endif /* HAVE_LIBCAP_NG */
648 }
649 else {
650 fprintf(stderr, "%s: Couldn't find user '%.32s'\n",
651 program_name, username);
652 exit_tcpdump(1);
653 }
654 #ifdef HAVE_LIBCAP_NG
655 /* We don't need CAP_SETUID, CAP_SETGID and CAP_SYS_CHROOT any more. */
656 capng_updatev(
657 CAPNG_DROP,
658 CAPNG_EFFECTIVE | CAPNG_PERMITTED,
659 CAP_SETUID,
660 CAP_SETGID,
661 CAP_SYS_CHROOT,
662 -1);
663 capng_apply(CAPNG_SELECT_BOTH);
664 #endif /* HAVE_LIBCAP_NG */
665
666 }
667 #endif /* _WIN32 */
668
669 static int
670 getWflagChars(int x)
671 {
672 int c = 0;
673
674 x -= 1;
675 while (x > 0) {
676 c += 1;
677 x /= 10;
678 }
679
680 return c;
681 }
682
683
684 static void
685 MakeFilename(char *buffer, char *orig_name, int cnt, int max_chars)
686 {
687 char *filename = malloc(PATH_MAX + 1);
688 if (filename == NULL)
689 error("Makefilename: malloc");
690
691 /* Process with strftime if Gflag is set. */
692 if (Gflag != 0) {
693 struct tm *local_tm;
694
695 /* Convert Gflag_time to a usable format */
696 if ((local_tm = localtime(&Gflag_time)) == NULL) {
697 error("MakeTimedFilename: localtime");
698 }
699
700 /* There's no good way to detect an error in strftime since a return
701 * value of 0 isn't necessarily failure.
702 */
703 strftime(filename, PATH_MAX, orig_name, local_tm);
704 } else {
705 strncpy(filename, orig_name, PATH_MAX);
706 }
707
708 if (cnt == 0 && max_chars == 0)
709 strncpy(buffer, filename, PATH_MAX + 1);
710 else
711 if (snprintf(buffer, PATH_MAX + 1, "%s%0*d", filename, max_chars, cnt) > PATH_MAX)
712 /* Report an error if the filename is too large */
713 error("too many output files or filename is too long (> %d)", PATH_MAX);
714 free(filename);
715 }
716
717 static char *
718 get_next_file(FILE *VFile, char *ptr)
719 {
720 char *ret;
721
722 ret = fgets(ptr, PATH_MAX, VFile);
723 if (!ret)
724 return NULL;
725
726 if (ptr[strlen(ptr) - 1] == '\n')
727 ptr[strlen(ptr) - 1] = '\0';
728
729 return ret;
730 }
731
732 #ifdef HAVE_CASPER
733 static cap_channel_t *
734 capdns_setup(void)
735 {
736 cap_channel_t *capcas, *capdnsloc;
737 const char *types[1];
738 int families[2];
739
740 capcas = cap_init();
741 if (capcas == NULL)
742 error("unable to create casper process");
743 capdnsloc = cap_service_open(capcas, "system.dns");
744 /* Casper capability no longer needed. */
745 cap_close(capcas);
746 if (capdnsloc == NULL)
747 error("unable to open system.dns service");
748 /* Limit system.dns to reverse DNS lookups. */
749 types[0] = "ADDR";
750 if (cap_dns_type_limit(capdnsloc, types, 1) < 0)
751 error("unable to limit access to system.dns service");
752 families[0] = AF_INET;
753 families[1] = AF_INET6;
754 if (cap_dns_family_limit(capdnsloc, families, 2) < 0)
755 error("unable to limit access to system.dns service");
756
757 return (capdnsloc);
758 }
759 #endif /* HAVE_CASPER */
760
761 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
762 static int
763 tstamp_precision_from_string(const char *precision)
764 {
765 if (strncmp(precision, "nano", strlen("nano")) == 0)
766 return PCAP_TSTAMP_PRECISION_NANO;
767
768 if (strncmp(precision, "micro", strlen("micro")) == 0)
769 return PCAP_TSTAMP_PRECISION_MICRO;
770
771 return -EINVAL;
772 }
773
774 static const char *
775 tstamp_precision_to_string(int precision)
776 {
777 switch (precision) {
778
779 case PCAP_TSTAMP_PRECISION_MICRO:
780 return "micro";
781
782 case PCAP_TSTAMP_PRECISION_NANO:
783 return "nano";
784
785 default:
786 return "unknown";
787 }
788 }
789 #endif
790
791 #ifdef HAVE_CAPSICUM
792 /*
793 * Ensure that, on a dump file's descriptor, we have all the rights
794 * necessary to make the standard I/O library work with an fdopen()ed
795 * FILE * from that descriptor.
796 *
797 * A long time ago, in a galaxy far far away, AT&T decided that, instead
798 * of providing separate APIs for getting and setting the FD_ flags on a
799 * descriptor, getting and setting the O_ flags on a descriptor, and
800 * locking files, they'd throw them all into a kitchen-sink fcntl() call
801 * along the lines of ioctl(), the fact that ioctl() operations are
802 * largely specific to particular character devices but fcntl() operations
803 * are either generic to all descriptors or generic to all descriptors for
804 * regular files nonwithstanding.
805 *
806 * The Capsicum people decided that fine-grained control of descriptor
807 * operations was required, so that you need to grant permission for
808 * reading, writing, seeking, and fcntl-ing. The latter, courtesy of
809 * AT&T's decision, means that "fcntl-ing" isn't a thing, but a motley
810 * collection of things, so there are *individual* fcntls for which
811 * permission needs to be granted.
812 *
813 * The FreeBSD standard I/O people implemented some optimizations that
814 * requires that the standard I/O routines be able to determine whether
815 * the descriptor for the FILE * is open append-only or not; as that
816 * descriptor could have come from an open() rather than an fopen(),
817 * that requires that it be able to do an F_GETFL fcntl() to read
818 * the O_ flags.
819 *
820 * Tcpdump uses ftell() to determine how much data has been written
821 * to a file in order to, when used with -C, determine when it's time
822 * to rotate capture files. ftell() therefore needs to do an lseek()
823 * to find out the file offset and must, thanks to the aforementioned
824 * optimization, also know whether the descriptor is open append-only
825 * or not.
826 *
827 * The net result of all the above is that we need to grant CAP_SEEK,
828 * CAP_WRITE, and CAP_FCNTL with the CAP_FCNTL_GETFL subcapability.
829 *
830 * Perhaps this is the universe's way of saying that either
831 *
832 * 1) there needs to be an fopenat() call and a pcap_dump_openat() call
833 * using it, so that Capsicum-capable tcpdump wouldn't need to do
834 * an fdopen()
835 *
836 * or
837 *
838 * 2) there needs to be a cap_fdopen() call in the FreeBSD standard
839 * I/O library that knows what rights are needed by the standard
840 * I/O library, based on the open mode, and assigns them, perhaps
841 * with an additional argument indicating, for example, whether
842 * seeking should be allowed, so that tcpdump doesn't need to know
843 * what the standard I/O library happens to require this week.
844 */
845 static void
846 set_dumper_capsicum_rights(pcap_dumper_t *p)
847 {
848 int fd = fileno(pcap_dump_file(p));
849 cap_rights_t rights;
850
851 cap_rights_init(&rights, CAP_SEEK, CAP_WRITE, CAP_FCNTL);
852 if (cap_rights_limit(fd, &rights) < 0 && errno != ENOSYS) {
853 error("unable to limit dump descriptor");
854 }
855 if (cap_fcntls_limit(fd, CAP_FCNTL_GETFL) < 0 && errno != ENOSYS) {
856 error("unable to limit dump descriptor fcntls");
857 }
858 }
859 #endif
860
861 /*
862 * Copy arg vector into a new buffer, concatenating arguments with spaces.
863 */
864 static char *
865 copy_argv(register char **argv)
866 {
867 register char **p;
868 register u_int len = 0;
869 char *buf;
870 char *src, *dst;
871
872 p = argv;
873 if (*p == NULL)
874 return 0;
875
876 while (*p)
877 len += strlen(*p++) + 1;
878
879 buf = (char *)malloc(len);
880 if (buf == NULL)
881 error("copy_argv: malloc");
882
883 p = argv;
884 dst = buf;
885 while ((src = *p++) != NULL) {
886 while ((*dst++ = *src++) != '\0')
887 ;
888 dst[-1] = ' ';
889 }
890 dst[-1] = '\0';
891
892 return buf;
893 }
894
895 /*
896 * On Windows, we need to open the file in binary mode, so that
897 * we get all the bytes specified by the size we get from "fstat()".
898 * On UNIX, that's not necessary. O_BINARY is defined on Windows;
899 * we define it as 0 if it's not defined, so it does nothing.
900 */
901 #ifndef O_BINARY
902 #define O_BINARY 0
903 #endif
904
905 static char *
906 read_infile(char *fname)
907 {
908 register int i, fd, cc;
909 register char *cp;
910 struct stat buf;
911
912 fd = open(fname, O_RDONLY|O_BINARY);
913 if (fd < 0)
914 error("can't open %s: %s", fname, pcap_strerror(errno));
915
916 if (fstat(fd, &buf) < 0)
917 error("can't stat %s: %s", fname, pcap_strerror(errno));
918
919 cp = malloc((u_int)buf.st_size + 1);
920 if (cp == NULL)
921 error("malloc(%d) for %s: %s", (u_int)buf.st_size + 1,
922 fname, pcap_strerror(errno));
923 cc = read(fd, cp, (u_int)buf.st_size);
924 if (cc < 0)
925 error("read %s: %s", fname, pcap_strerror(errno));
926 if (cc != buf.st_size)
927 error("short read %s (%d != %d)", fname, cc, (int)buf.st_size);
928
929 close(fd);
930 /* replace "# comment" with spaces */
931 for (i = 0; i < cc; i++) {
932 if (cp[i] == '#')
933 while (i < cc && cp[i] != '\n')
934 cp[i++] = ' ';
935 }
936 cp[cc] = '\0';
937 return (cp);
938 }
939
940 #ifdef HAVE_PCAP_FINDALLDEVS
941 static long
942 parse_interface_number(const char *device)
943 {
944 long devnum;
945 char *end;
946
947 devnum = strtol(device, &end, 10);
948 if (device != end && *end == '\0') {
949 /*
950 * It's all-numeric, but is it a valid number?
951 */
952 if (devnum <= 0) {
953 /*
954 * No, it's not an ordinal.
955 */
956 error("Invalid adapter index");
957 }
958 return (devnum);
959 } else {
960 /*
961 * It's not all-numeric; return -1, so our caller
962 * knows that.
963 */
964 return (-1);
965 }
966 }
967
968 static char *
969 find_interface_by_number(long devnum)
970 {
971 pcap_if_t *dev, *devlist;
972 long i;
973 char ebuf[PCAP_ERRBUF_SIZE];
974 char *device;
975
976 if (pcap_findalldevs(&devlist, ebuf) < 0)
977 error("%s", ebuf);
978 /*
979 * Look for the devnum-th entry in the list of devices (1-based).
980 */
981 for (i = 0, dev = devlist; i < devnum-1 && dev != NULL;
982 i++, dev = dev->next)
983 ;
984 if (dev == NULL)
985 error("Invalid adapter index");
986 device = strdup(dev->name);
987 pcap_freealldevs(devlist);
988 return (device);
989 }
990 #endif
991
992 static pcap_t *
993 open_interface(const char *device, netdissect_options *ndo, char *ebuf)
994 {
995 pcap_t *pc;
996 #ifdef HAVE_PCAP_CREATE
997 int status;
998 char *cp;
999 #endif
1000
1001 #ifdef HAVE_PCAP_CREATE
1002 pc = pcap_create(device, ebuf);
1003 if (pc == NULL) {
1004 /*
1005 * If this failed with "No such device", that means
1006 * the interface doesn't exist; return NULL, so that
1007 * the caller can see whether the device name is
1008 * actually an interface index.
1009 */
1010 if (strstr(ebuf, "No such device") != NULL)
1011 return (NULL);
1012 error("%s", ebuf);
1013 }
1014 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
1015 if (Jflag)
1016 show_tstamp_types_and_exit(pc, device);
1017 #endif
1018 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
1019 status = pcap_set_tstamp_precision(pc, ndo->ndo_tstamp_precision);
1020 if (status != 0)
1021 error("%s: Can't set %ssecond time stamp precision: %s",
1022 device,
1023 tstamp_precision_to_string(ndo->ndo_tstamp_precision),
1024 pcap_statustostr(status));
1025 #endif
1026
1027 #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
1028 if (immediate_mode) {
1029 status = pcap_set_immediate_mode(pc, 1);
1030 if (status != 0)
1031 error("%s: Can't set immediate mode: %s",
1032 device,
1033 pcap_statustostr(status));
1034 }
1035 #endif
1036 /*
1037 * Is this an interface that supports monitor mode?
1038 */
1039 if (pcap_can_set_rfmon(pc) == 1)
1040 supports_monitor_mode = 1;
1041 else
1042 supports_monitor_mode = 0;
1043 if (ndo->ndo_snaplen != 0) {
1044 /*
1045 * A snapshot length was explicitly specified;
1046 * use it.
1047 */
1048 status = pcap_set_snaplen(pc, ndo->ndo_snaplen);
1049 if (status != 0)
1050 error("%s: Can't set snapshot length: %s",
1051 device, pcap_statustostr(status));
1052 }
1053 status = pcap_set_promisc(pc, !pflag);
1054 if (status != 0)
1055 error("%s: Can't set promiscuous mode: %s",
1056 device, pcap_statustostr(status));
1057 if (Iflag) {
1058 status = pcap_set_rfmon(pc, 1);
1059 if (status != 0)
1060 error("%s: Can't set monitor mode: %s",
1061 device, pcap_statustostr(status));
1062 }
1063 status = pcap_set_timeout(pc, 1000);
1064 if (status != 0)
1065 error("%s: pcap_set_timeout failed: %s",
1066 device, pcap_statustostr(status));
1067 if (Bflag != 0) {
1068 status = pcap_set_buffer_size(pc, Bflag);
1069 if (status != 0)
1070 error("%s: Can't set buffer size: %s",
1071 device, pcap_statustostr(status));
1072 }
1073 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
1074 if (jflag != -1) {
1075 status = pcap_set_tstamp_type(pc, jflag);
1076 if (status < 0)
1077 error("%s: Can't set time stamp type: %s",
1078 device, pcap_statustostr(status));
1079 }
1080 #endif
1081 status = pcap_activate(pc);
1082 if (status < 0) {
1083 /*
1084 * pcap_activate() failed.
1085 */
1086 cp = pcap_geterr(pc);
1087 if (status == PCAP_ERROR)
1088 error("%s", cp);
1089 else if (status == PCAP_ERROR_NO_SUCH_DEVICE) {
1090 /*
1091 * Return an error for our caller to handle.
1092 */
1093 snprintf(ebuf, PCAP_ERRBUF_SIZE, "%s: %s\n(%s)",
1094 device, pcap_statustostr(status), cp);
1095 pcap_close(pc);
1096 return (NULL);
1097 } else if (status == PCAP_ERROR_PERM_DENIED && *cp != '\0')
1098 error("%s: %s\n(%s)", device,
1099 pcap_statustostr(status), cp);
1100 #ifdef __FreeBSD__
1101 else if (status == PCAP_ERROR_RFMON_NOTSUP &&
1102 strncmp(device, "wlan", 4) == 0) {
1103 char parent[8], newdev[8];
1104 char sysctl[32];
1105 size_t s = sizeof(parent);
1106
1107 snprintf(sysctl, sizeof(sysctl),
1108 "net.wlan.%d.%%parent", atoi(device + 4));
1109 sysctlbyname(sysctl, parent, &s, NULL, 0);
1110 strlcpy(newdev, device, sizeof(newdev));
1111 /* Suggest a new wlan device. */
1112 /* FIXME: incrementing the index this way is not going to work well
1113 * when the index is 9 or greater but the only consequence in this
1114 * specific case would be an error message that looks a bit odd.
1115 */
1116 newdev[strlen(newdev)-1]++;
1117 error("%s is not a monitor mode VAP\n"
1118 "To create a new monitor mode VAP use:\n"
1119 " ifconfig %s create wlandev %s wlanmode monitor\n"
1120 "and use %s as the tcpdump interface",
1121 device, newdev, parent, newdev);
1122 }
1123 #endif
1124 else
1125 error("%s: %s", device,
1126 pcap_statustostr(status));
1127 } else if (status > 0) {
1128 /*
1129 * pcap_activate() succeeded, but it's warning us
1130 * of a problem it had.
1131 */
1132 cp = pcap_geterr(pc);
1133 if (status == PCAP_WARNING)
1134 warning("%s", cp);
1135 else if (status == PCAP_WARNING_PROMISC_NOTSUP &&
1136 *cp != '\0')
1137 warning("%s: %s\n(%s)", device,
1138 pcap_statustostr(status), cp);
1139 else
1140 warning("%s: %s", device,
1141 pcap_statustostr(status));
1142 }
1143 #ifdef HAVE_PCAP_SETDIRECTION
1144 if (Qflag != -1) {
1145 status = pcap_setdirection(pc, Qflag);
1146 if (status != 0)
1147 error("%s: pcap_setdirection() failed: %s",
1148 device, pcap_geterr(pc));
1149 }
1150 #endif /* HAVE_PCAP_SETDIRECTION */
1151 #else /* HAVE_PCAP_CREATE */
1152 *ebuf = '\0';
1153 /*
1154 * If no snapshot length was specified, or a length of 0 was
1155 * specified, default to 256KB.
1156 */
1157 if (ndo->ndo_snaplen == 0)
1158 ndo->ndo_snaplen = 262144;
1159 pc = pcap_open_live(device, ndo->ndo_snaplen, !pflag, 1000, ebuf);
1160 if (pc == NULL) {
1161 /*
1162 * If this failed with "No such device", that means
1163 * the interface doesn't exist; return NULL, so that
1164 * the caller can see whether the device name is
1165 * actually an interface index.
1166 */
1167 if (strstr(ebuf, "No such device") != NULL)
1168 return (NULL);
1169 error("%s", ebuf);
1170 }
1171 if (*ebuf)
1172 warning("%s", ebuf);
1173 #endif /* HAVE_PCAP_CREATE */
1174
1175 return (pc);
1176 }
1177
1178 int
1179 main(int argc, char **argv)
1180 {
1181 register int cnt, op, i;
1182 bpf_u_int32 localnet =0 , netmask = 0;
1183 int timezone_offset = 0;
1184 register char *cp, *infile, *cmdbuf, *device, *RFileName, *VFileName, *WFileName;
1185 char *endp;
1186 pcap_handler callback;
1187 int dlt;
1188 const char *dlt_name;
1189 struct bpf_program fcode;
1190 #ifndef _WIN32
1191 RETSIGTYPE (*oldhandler)(int);
1192 #endif
1193 struct dump_info dumpinfo;
1194 u_char *pcap_userdata;
1195 char ebuf[PCAP_ERRBUF_SIZE];
1196 char VFileLine[PATH_MAX + 1];
1197 char *username = NULL;
1198 char *chroot_dir = NULL;
1199 char *ret = NULL;
1200 char *end;
1201 #ifdef HAVE_PCAP_FINDALLDEVS
1202 pcap_if_t *devlist;
1203 long devnum;
1204 #endif
1205 int status;
1206 FILE *VFile;
1207 #ifdef HAVE_CAPSICUM
1208 cap_rights_t rights;
1209 int cansandbox;
1210 #endif /* HAVE_CAPSICUM */
1211 int Oflag = 1; /* run filter code optimizer */
1212 int yflag_dlt = -1;
1213 const char *yflag_dlt_name = NULL;
1214
1215 netdissect_options Ndo;
1216 netdissect_options *ndo = &Ndo;
1217
1218 /*
1219 * Initialize the netdissect code.
1220 */
1221 if (nd_init(ebuf, sizeof ebuf) == -1)
1222 error("%s", ebuf);
1223
1224 memset(ndo, 0, sizeof(*ndo));
1225 ndo_set_function_pointers(ndo);
1226
1227 cnt = -1;
1228 device = NULL;
1229 infile = NULL;
1230 RFileName = NULL;
1231 VFileName = NULL;
1232 VFile = NULL;
1233 WFileName = NULL;
1234 dlt = -1;
1235 if ((cp = strrchr(argv[0], '/')) != NULL)
1236 ndo->program_name = program_name = cp + 1;
1237 else
1238 ndo->program_name = program_name = argv[0];
1239
1240 #ifdef _WIN32
1241 if (pcap_wsockinit() != 0)
1242 error("Attempting to initialize Winsock failed");
1243 #endif /* _WIN32 */
1244
1245 /*
1246 * On platforms where the CPU doesn't support unaligned loads,
1247 * force unaligned accesses to abort with SIGBUS, rather than
1248 * being fixed up (slowly) by the OS kernel; on those platforms,
1249 * misaligned accesses are bugs, and we want tcpdump to crash so
1250 * that the bugs are reported.
1251 */
1252 if (abort_on_misalignment(ebuf, sizeof(ebuf)) < 0)
1253 error("%s", ebuf);
1254
1255 while (
1256 (op = getopt_long(argc, argv, SHORTOPTS, longopts, NULL)) != -1)
1257 switch (op) {
1258
1259 case 'a':
1260 /* compatibility for old -a */
1261 break;
1262
1263 case 'A':
1264 ++ndo->ndo_Aflag;
1265 break;
1266
1267 case 'b':
1268 ++ndo->ndo_bflag;
1269 break;
1270
1271 #if defined(HAVE_PCAP_CREATE) || defined(_WIN32)
1272 case 'B':
1273 Bflag = atoi(optarg)*1024;
1274 if (Bflag <= 0)
1275 error("invalid packet buffer size %s", optarg);
1276 break;
1277 #endif /* defined(HAVE_PCAP_CREATE) || defined(_WIN32) */
1278
1279 case 'c':
1280 cnt = atoi(optarg);
1281 if (cnt <= 0)
1282 error("invalid packet count %s", optarg);
1283 break;
1284
1285 case 'C':
1286 errno = 0;
1287 #ifdef HAVE_PCAP_FTELL64
1288 Cflag = strtoint64_t(optarg, &endp, 10);
1289 #else
1290 Cflag = strtol(optarg, &endp, 10);
1291 #endif
1292 if (endp == optarg || *endp != '\0' || errno != 0
1293 || Cflag <= 0)
1294 error("invalid file size %s", optarg);
1295 /*
1296 * Will multiplying it by 1000000 overflow?
1297 */
1298 #ifdef HAVE_PCAP_FTELL64
1299 if (Cflag > INT64_T_CONSTANT(0x7fffffffffffffffU) / 1000000)
1300 #else
1301 if (Cflag > LONG_MAX / 1000000)
1302 #endif
1303 error("file size %s is too large", optarg);
1304 Cflag *= 1000000;
1305 break;
1306
1307 case 'd':
1308 ++dflag;
1309 break;
1310
1311 case 'D':
1312 Dflag++;
1313 break;
1314
1315 case 'L':
1316 Lflag++;
1317 break;
1318
1319 case 'e':
1320 ++ndo->ndo_eflag;
1321 break;
1322
1323 case 'E':
1324 #ifndef HAVE_LIBCRYPTO
1325 warning("crypto code not compiled in");
1326 #endif
1327 ndo->ndo_espsecret = optarg;
1328 break;
1329
1330 case 'f':
1331 ++ndo->ndo_fflag;
1332 break;
1333
1334 case 'F':
1335 infile = optarg;
1336 break;
1337
1338 case 'G':
1339 Gflag = atoi(optarg);
1340 if (Gflag < 0)
1341 error("invalid number of seconds %s", optarg);
1342
1343 /* We will create one file initially. */
1344 Gflag_count = 0;
1345
1346 /* Grab the current time for rotation use. */
1347 if ((Gflag_time = time(NULL)) == (time_t)-1) {
1348 error("main: can't get current time: %s",
1349 pcap_strerror(errno));
1350 }
1351 break;
1352
1353 case 'h':
1354 print_usage();
1355 exit_tcpdump(0);
1356 break;
1357
1358 case 'H':
1359 ++ndo->ndo_Hflag;
1360 break;
1361
1362 case 'i':
1363 device = optarg;
1364 break;
1365
1366 #ifdef HAVE_PCAP_CREATE
1367 case 'I':
1368 ++Iflag;
1369 break;
1370 #endif /* HAVE_PCAP_CREATE */
1371
1372 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
1373 case 'j':
1374 jflag = pcap_tstamp_type_name_to_val(optarg);
1375 if (jflag < 0)
1376 error("invalid time stamp type %s", optarg);
1377 break;
1378
1379 case 'J':
1380 Jflag++;
1381 break;
1382 #endif
1383
1384 case 'l':
1385 #ifdef _WIN32
1386 /*
1387 * _IOLBF is the same as _IOFBF in Microsoft's C
1388 * libraries; the only alternative they offer
1389 * is _IONBF.
1390 *
1391 * XXX - this should really be checking for MSVC++,
1392 * not _WIN32, if, for example, MinGW has its own
1393 * C library that is more UNIX-compatible.
1394 */
1395 setvbuf(stdout, NULL, _IONBF, 0);
1396 #else /* _WIN32 */
1397 #ifdef HAVE_SETLINEBUF
1398 setlinebuf(stdout);
1399 #else
1400 setvbuf(stdout, NULL, _IOLBF, 0);
1401 #endif
1402 #endif /* _WIN32 */
1403 break;
1404
1405 case 'K':
1406 ++ndo->ndo_Kflag;
1407 break;
1408
1409 case 'm':
1410 if (nd_have_smi_support()) {
1411 if (nd_load_smi_module(optarg, ebuf, sizeof ebuf) == -1)
1412 error("%s", ebuf);
1413 } else {
1414 (void)fprintf(stderr, "%s: ignoring option `-m %s' ",
1415 program_name, optarg);
1416 (void)fprintf(stderr, "(no libsmi support)\n");
1417 }
1418 break;
1419
1420 case 'M':
1421 /* TCP-MD5 shared secret */
1422 #ifndef HAVE_LIBCRYPTO
1423 warning("crypto code not compiled in");
1424 #endif
1425 ndo->ndo_sigsecret = optarg;
1426 break;
1427
1428 case 'n':
1429 ++ndo->ndo_nflag;
1430 break;
1431
1432 case 'N':
1433 ++ndo->ndo_Nflag;
1434 break;
1435
1436 case 'O':
1437 Oflag = 0;
1438 break;
1439
1440 case 'p':
1441 ++pflag;
1442 break;
1443
1444 case 'q':
1445 ++ndo->ndo_qflag;
1446 ++ndo->ndo_suppress_default_print;
1447 break;
1448
1449 #ifdef HAVE_PCAP_SETDIRECTION
1450 case 'Q':
1451 if (ascii_strcasecmp(optarg, "in") == 0)
1452 Qflag = PCAP_D_IN;
1453 else if (ascii_strcasecmp(optarg, "out") == 0)
1454 Qflag = PCAP_D_OUT;
1455 else if (ascii_strcasecmp(optarg, "inout") == 0)
1456 Qflag = PCAP_D_INOUT;
1457 else
1458 error("unknown capture direction `%s'", optarg);
1459 break;
1460 #endif /* HAVE_PCAP_SETDIRECTION */
1461
1462 case 'r':
1463 RFileName = optarg;
1464 break;
1465
1466 case 's':
1467 ndo->ndo_snaplen = strtol(optarg, &end, 0);
1468 if (optarg == end || *end != '\0'
1469 || ndo->ndo_snaplen < 0 || ndo->ndo_snaplen > MAXIMUM_SNAPLEN)
1470 error("invalid snaplen %s", optarg);
1471 break;
1472
1473 case 'S':
1474 ++ndo->ndo_Sflag;
1475 break;
1476
1477 case 't':
1478 ++ndo->ndo_tflag;
1479 break;
1480
1481 case 'T':
1482 if (ascii_strcasecmp(optarg, "vat") == 0)
1483 ndo->ndo_packettype = PT_VAT;
1484 else if (ascii_strcasecmp(optarg, "wb") == 0)
1485 ndo->ndo_packettype = PT_WB;
1486 else if (ascii_strcasecmp(optarg, "rpc") == 0)
1487 ndo->ndo_packettype = PT_RPC;
1488 else if (ascii_strcasecmp(optarg, "rtp") == 0)
1489 ndo->ndo_packettype = PT_RTP;
1490 else if (ascii_strcasecmp(optarg, "rtcp") == 0)
1491 ndo->ndo_packettype = PT_RTCP;
1492 else if (ascii_strcasecmp(optarg, "snmp") == 0)
1493 ndo->ndo_packettype = PT_SNMP;
1494 else if (ascii_strcasecmp(optarg, "cnfp") == 0)
1495 ndo->ndo_packettype = PT_CNFP;
1496 else if (ascii_strcasecmp(optarg, "tftp") == 0)
1497 ndo->ndo_packettype = PT_TFTP;
1498 else if (ascii_strcasecmp(optarg, "aodv") == 0)
1499 ndo->ndo_packettype = PT_AODV;
1500 else if (ascii_strcasecmp(optarg, "carp") == 0)
1501 ndo->ndo_packettype = PT_CARP;
1502 else if (ascii_strcasecmp(optarg, "radius") == 0)
1503 ndo->ndo_packettype = PT_RADIUS;
1504 else if (ascii_strcasecmp(optarg, "zmtp1") == 0)
1505 ndo->ndo_packettype = PT_ZMTP1;
1506 else if (ascii_strcasecmp(optarg, "vxlan") == 0)
1507 ndo->ndo_packettype = PT_VXLAN;
1508 else if (ascii_strcasecmp(optarg, "pgm") == 0)
1509 ndo->ndo_packettype = PT_PGM;
1510 else if (ascii_strcasecmp(optarg, "pgm_zmtp1") == 0)
1511 ndo->ndo_packettype = PT_PGM_ZMTP1;
1512 else if (ascii_strcasecmp(optarg, "lmp") == 0)
1513 ndo->ndo_packettype = PT_LMP;
1514 else if (ascii_strcasecmp(optarg, "resp") == 0)
1515 ndo->ndo_packettype = PT_RESP;
1516 else
1517 error("unknown packet type `%s'", optarg);
1518 break;
1519
1520 case 'u':
1521 ++ndo->ndo_uflag;
1522 break;
1523
1524 #ifdef HAVE_PCAP_DUMP_FLUSH
1525 case 'U':
1526 ++Uflag;
1527 break;
1528 #endif
1529
1530 case 'v':
1531 ++ndo->ndo_vflag;
1532 break;
1533
1534 case 'V':
1535 VFileName = optarg;
1536 break;
1537
1538 case 'w':
1539 WFileName = optarg;
1540 break;
1541
1542 case 'W':
1543 Wflag = atoi(optarg);
1544 if (Wflag <= 0)
1545 error("invalid number of output files %s", optarg);
1546 WflagChars = getWflagChars(Wflag);
1547 break;
1548
1549 case 'x':
1550 ++ndo->ndo_xflag;
1551 ++ndo->ndo_suppress_default_print;
1552 break;
1553
1554 case 'X':
1555 ++ndo->ndo_Xflag;
1556 ++ndo->ndo_suppress_default_print;
1557 break;
1558
1559 case 'y':
1560 yflag_dlt_name = optarg;
1561 yflag_dlt =
1562 pcap_datalink_name_to_val(yflag_dlt_name);
1563 if (yflag_dlt < 0)
1564 error("invalid data link type %s", yflag_dlt_name);
1565 break;
1566
1567 #ifdef HAVE_PCAP_SET_PARSER_DEBUG
1568 case 'Y':
1569 {
1570 /* Undocumented flag */
1571 pcap_set_parser_debug(1);
1572 }
1573 break;
1574 #endif
1575 case 'z':
1576 zflag = optarg;
1577 break;
1578
1579 case 'Z':
1580 username = optarg;
1581 break;
1582
1583 case '#':
1584 ndo->ndo_packet_number = 1;
1585 break;
1586
1587 case OPTION_VERSION:
1588 print_version();
1589 exit_tcpdump(0);
1590 break;
1591
1592 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
1593 case OPTION_TSTAMP_PRECISION:
1594 ndo->ndo_tstamp_precision = tstamp_precision_from_string(optarg);
1595 if (ndo->ndo_tstamp_precision < 0)
1596 error("unsupported time stamp precision");
1597 break;
1598 #endif
1599
1600 #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
1601 case OPTION_IMMEDIATE_MODE:
1602 immediate_mode = 1;
1603 break;
1604 #endif
1605
1606 default:
1607 print_usage();
1608 exit_tcpdump(1);
1609 /* NOTREACHED */
1610 }
1611
1612 #ifdef HAVE_PCAP_FINDALLDEVS
1613 if (Dflag)
1614 show_devices_and_exit();
1615 #endif
1616
1617 switch (ndo->ndo_tflag) {
1618
1619 case 0: /* Default */
1620 case 4: /* Default + Date*/
1621 timezone_offset = gmt2local(0);
1622 break;
1623
1624 case 1: /* No time stamp */
1625 case 2: /* Unix timeval style */
1626 case 3: /* Microseconds since previous packet */
1627 case 5: /* Microseconds since first packet */
1628 break;
1629
1630 default: /* Not supported */
1631 error("only -t, -tt, -ttt, -tttt and -ttttt are supported");
1632 break;
1633 }
1634
1635 if (ndo->ndo_fflag != 0 && (VFileName != NULL || RFileName != NULL))
1636 error("-f can not be used with -V or -r");
1637
1638 if (VFileName != NULL && RFileName != NULL)
1639 error("-V and -r are mutually exclusive.");
1640
1641 #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
1642 /*
1643 * If we're printing dissected packets to the standard output
1644 * rather than saving raw packets to a file, and the standard
1645 * output is a terminal, use immediate mode, as the user's
1646 * probably expecting to see packets pop up immediately.
1647 */
1648 if (WFileName == NULL && isatty(1))
1649 immediate_mode = 1;
1650 #endif
1651
1652 #ifdef WITH_CHROOT
1653 /* if run as root, prepare for chrooting */
1654 if (getuid() == 0 || geteuid() == 0) {
1655 /* future extensibility for cmd-line arguments */
1656 if (!chroot_dir)
1657 chroot_dir = WITH_CHROOT;
1658 }
1659 #endif
1660
1661 #ifdef WITH_USER
1662 /* if run as root, prepare for dropping root privileges */
1663 if (getuid() == 0 || geteuid() == 0) {
1664 /* Run with '-Z root' to restore old behaviour */
1665 if (!username)
1666 username = WITH_USER;
1667 }
1668 #endif
1669
1670 if (RFileName != NULL || VFileName != NULL) {
1671 /*
1672 * If RFileName is non-null, it's the pathname of a
1673 * savefile to read. If VFileName is non-null, it's
1674 * the pathname of a file containing a list of pathnames
1675 * (one per line) of savefiles to read.
1676 *
1677 * In either case, we're reading a savefile, not doing
1678 * a live capture.
1679 */
1680 #ifndef _WIN32
1681 /*
1682 * We don't need network access, so relinquish any set-UID
1683 * or set-GID privileges we have (if any).
1684 *
1685 * We do *not* want set-UID privileges when opening a
1686 * trace file, as that might let the user read other
1687 * people's trace files (especially if we're set-UID
1688 * root).
1689 */
1690 if (setgid(getgid()) != 0 || setuid(getuid()) != 0 )
1691 fprintf(stderr, "Warning: setgid/setuid failed !\n");
1692 #endif /* _WIN32 */
1693 if (VFileName != NULL) {
1694 if (VFileName[0] == '-' && VFileName[1] == '\0')
1695 VFile = stdin;
1696 else
1697 VFile = fopen(VFileName, "r");
1698
1699 if (VFile == NULL)
1700 error("Unable to open file: %s\n", pcap_strerror(errno));
1701
1702 ret = get_next_file(VFile, VFileLine);
1703 if (!ret)
1704 error("Nothing in %s\n", VFileName);
1705 RFileName = VFileLine;
1706 }
1707
1708 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
1709 pd = pcap_open_offline_with_tstamp_precision(RFileName,
1710 ndo->ndo_tstamp_precision, ebuf);
1711 #else
1712 pd = pcap_open_offline(RFileName, ebuf);
1713 #endif
1714
1715 if (pd == NULL)
1716 error("%s", ebuf);
1717 #ifdef HAVE_CAPSICUM
1718 cap_rights_init(&rights, CAP_READ);
1719 if (cap_rights_limit(fileno(pcap_file(pd)), &rights) < 0 &&
1720 errno != ENOSYS) {
1721 error("unable to limit pcap descriptor");
1722 }
1723 #endif
1724 dlt = pcap_datalink(pd);
1725 dlt_name = pcap_datalink_val_to_name(dlt);
1726 if (dlt_name == NULL) {
1727 fprintf(stderr, "reading from file %s, link-type %u\n",
1728 RFileName, dlt);
1729 } else {
1730 fprintf(stderr,
1731 "reading from file %s, link-type %s (%s)\n",
1732 RFileName, dlt_name,
1733 pcap_datalink_val_to_description(dlt));
1734 }
1735 } else {
1736 /*
1737 * We're doing a live capture.
1738 */
1739 if (device == NULL) {
1740 /*
1741 * No interface was specified. Pick one.
1742 */
1743 #ifdef HAVE_PCAP_FINDALLDEVS
1744 /*
1745 * Find the list of interfaces, and pick
1746 * the first interface.
1747 */
1748 if (pcap_findalldevs(&devlist, ebuf) == -1)
1749 error("%s", ebuf);
1750 if (devlist == NULL)
1751 error("no interfaces available for capture");
1752 device = strdup(devlist->name);
1753 pcap_freealldevs(devlist);
1754 #else /* HAVE_PCAP_FINDALLDEVS */
1755 /*
1756 * Use whatever interface pcap_lookupdev()
1757 * chooses.
1758 */
1759 device = pcap_lookupdev(ebuf);
1760 if (device == NULL)
1761 error("%s", ebuf);
1762 #endif
1763 }
1764
1765 /*
1766 * Try to open the interface with the specified name.
1767 */
1768 pd = open_interface(device, ndo, ebuf);
1769 if (pd == NULL) {
1770 /*
1771 * That failed. If we can get a list of
1772 * interfaces, and the interface name
1773 * is purely numeric, try to use it as
1774 * a 1-based index in the list of
1775 * interfaces.
1776 */
1777 #ifdef HAVE_PCAP_FINDALLDEVS
1778 devnum = parse_interface_number(device);
1779 if (devnum == -1) {
1780 /*
1781 * It's not a number; just report
1782 * the open error and fail.
1783 */
1784 error("%s", ebuf);
1785 }
1786
1787 /*
1788 * OK, it's a number; try to find the
1789 * interface with that index, and try
1790 * to open it.
1791 *
1792 * find_interface_by_number() exits if it
1793 * couldn't be found.
1794 */
1795 device = find_interface_by_number(devnum);
1796 pd = open_interface(device, ndo, ebuf);
1797 if (pd == NULL)
1798 error("%s", ebuf);
1799 #else /* HAVE_PCAP_FINDALLDEVS */
1800 /*
1801 * We can't get a list of interfaces; just
1802 * fail.
1803 */
1804 error("%s", ebuf);
1805 #endif /* HAVE_PCAP_FINDALLDEVS */
1806 }
1807
1808 /*
1809 * Let user own process after capture device has
1810 * been opened.
1811 */
1812 #ifndef _WIN32
1813 if (setgid(getgid()) != 0 || setuid(getuid()) != 0)
1814 fprintf(stderr, "Warning: setgid/setuid failed !\n");
1815 #endif /* _WIN32 */
1816 #if !defined(HAVE_PCAP_CREATE) && defined(_WIN32)
1817 if(Bflag != 0)
1818 if(pcap_setbuff(pd, Bflag)==-1){
1819 error("%s", pcap_geterr(pd));
1820 }
1821 #endif /* !defined(HAVE_PCAP_CREATE) && defined(_WIN32) */
1822 if (Lflag)
1823 show_dlts_and_exit(pd, device);
1824 if (yflag_dlt >= 0) {
1825 #ifdef HAVE_PCAP_SET_DATALINK
1826 if (pcap_set_datalink(pd, yflag_dlt) < 0)
1827 error("%s", pcap_geterr(pd));
1828 #else
1829 /*
1830 * We don't actually support changing the
1831 * data link type, so we only let them
1832 * set it to what it already is.
1833 */
1834 if (yflag_dlt != pcap_datalink(pd)) {
1835 error("%s is not one of the DLTs supported by this device\n",
1836 yflag_dlt_name);
1837 }
1838 #endif
1839 (void)fprintf(stderr, "%s: data link type %s\n",
1840 program_name, yflag_dlt_name);
1841 (void)fflush(stderr);
1842 }
1843 i = pcap_snapshot(pd);
1844 if (ndo->ndo_snaplen < i) {
1845 if (ndo->ndo_snaplen != 0)
1846 warning("snaplen raised from %d to %d", ndo->ndo_snaplen, i);
1847 ndo->ndo_snaplen = i;
1848 } else if (ndo->ndo_snaplen > i) {
1849 warning("snaplen lowered from %d to %d", ndo->ndo_snaplen, i);
1850 ndo->ndo_snaplen = i;
1851 }
1852 if(ndo->ndo_fflag != 0) {
1853 if (pcap_lookupnet(device, &localnet, &netmask, ebuf) < 0) {
1854 warning("foreign (-f) flag used but: %s", ebuf);
1855 }
1856 }
1857
1858 }
1859 if (infile)
1860 cmdbuf = read_infile(infile);
1861 else
1862 cmdbuf = copy_argv(&argv[optind]);
1863
1864 #ifdef HAVE_PCAP_SET_OPTIMIZER_DEBUG
1865 pcap_set_optimizer_debug(dflag);
1866 #endif
1867 if (pcap_compile(pd, &fcode, cmdbuf, Oflag, netmask) < 0)
1868 error("%s", pcap_geterr(pd));
1869 if (dflag) {
1870 bpf_dump(&fcode, dflag);
1871 pcap_close(pd);
1872 free(cmdbuf);
1873 pcap_freecode(&fcode);
1874 exit_tcpdump(0);
1875 }
1876
1877 #ifdef HAVE_CASPER
1878 if (!ndo->ndo_nflag)
1879 capdns = capdns_setup();
1880 #endif /* HAVE_CASPER */
1881
1882 init_print(ndo, localnet, netmask, timezone_offset);
1883
1884 #ifndef _WIN32
1885 (void)setsignal(SIGPIPE, cleanup);
1886 (void)setsignal(SIGTERM, cleanup);
1887 (void)setsignal(SIGINT, cleanup);
1888 #endif /* _WIN32 */
1889 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
1890 (void)setsignal(SIGCHLD, child_cleanup);
1891 #endif
1892 /* Cooperate with nohup(1) */
1893 #ifndef _WIN32
1894 if ((oldhandler = setsignal(SIGHUP, cleanup)) != SIG_DFL)
1895 (void)setsignal(SIGHUP, oldhandler);
1896 #endif /* _WIN32 */
1897
1898 #ifndef _WIN32
1899 /*
1900 * If a user name was specified with "-Z", attempt to switch to
1901 * that user's UID. This would probably be used with sudo,
1902 * to allow tcpdump to be run in a special restricted
1903 * account (if you just want to allow users to open capture
1904 * devices, and can't just give users that permission,
1905 * you'd make tcpdump set-UID or set-GID).
1906 *
1907 * Tcpdump doesn't necessarily write only to one savefile;
1908 * the general only way to allow a -Z instance to write to
1909 * savefiles as the user under whose UID it's run, rather
1910 * than as the user specified with -Z, would thus be to switch
1911 * to the original user ID before opening a capture file and
1912 * then switch back to the -Z user ID after opening the savefile.
1913 * Switching to the -Z user ID only after opening the first
1914 * savefile doesn't handle the general case.
1915 */
1916
1917 if (getuid() == 0 || geteuid() == 0) {
1918 #ifdef HAVE_LIBCAP_NG
1919 /* Initialize capng */
1920 capng_clear(CAPNG_SELECT_BOTH);
1921 if (username) {
1922 capng_updatev(
1923 CAPNG_ADD,
1924 CAPNG_PERMITTED | CAPNG_EFFECTIVE,
1925 CAP_SETUID,
1926 CAP_SETGID,
1927 -1);
1928 }
1929 if (chroot_dir) {
1930 capng_update(
1931 CAPNG_ADD,
1932 CAPNG_PERMITTED | CAPNG_EFFECTIVE,
1933 CAP_SYS_CHROOT
1934 );
1935 }
1936
1937 if (WFileName) {
1938 capng_update(
1939 CAPNG_ADD,
1940 CAPNG_PERMITTED | CAPNG_EFFECTIVE,
1941 CAP_DAC_OVERRIDE
1942 );
1943 }
1944 capng_apply(CAPNG_SELECT_BOTH);
1945 #endif /* HAVE_LIBCAP_NG */
1946 if (username || chroot_dir)
1947 droproot(username, chroot_dir);
1948
1949 }
1950 #endif /* _WIN32 */
1951
1952 if (pcap_setfilter(pd, &fcode) < 0)
1953 error("%s", pcap_geterr(pd));
1954 #ifdef HAVE_CAPSICUM
1955 if (RFileName == NULL && VFileName == NULL) {
1956 static const unsigned long cmds[] = { BIOCGSTATS, BIOCROTZBUF };
1957
1958 /*
1959 * The various libpcap devices use a combination of
1960 * read (bpf), ioctl (bpf, netmap), poll (netmap)
1961 * so we add the relevant access rights.
1962 */
1963 cap_rights_init(&rights, CAP_IOCTL, CAP_READ, CAP_EVENT);
1964 if (cap_rights_limit(pcap_fileno(pd), &rights) < 0 &&
1965 errno != ENOSYS) {
1966 error("unable to limit pcap descriptor");
1967 }
1968 if (cap_ioctls_limit(pcap_fileno(pd), cmds,
1969 sizeof(cmds) / sizeof(cmds[0])) < 0 && errno != ENOSYS) {
1970 error("unable to limit ioctls on pcap descriptor");
1971 }
1972 }
1973 #endif
1974 if (WFileName) {
1975 pcap_dumper_t *p;
1976 /* Do not exceed the default PATH_MAX for files. */
1977 dumpinfo.CurrentFileName = (char *)malloc(PATH_MAX + 1);
1978
1979 if (dumpinfo.CurrentFileName == NULL)
1980 error("malloc of dumpinfo.CurrentFileName");
1981
1982 /* We do not need numbering for dumpfiles if Cflag isn't set. */
1983 if (Cflag != 0)
1984 MakeFilename(dumpinfo.CurrentFileName, WFileName, 0, WflagChars);
1985 else
1986 MakeFilename(dumpinfo.CurrentFileName, WFileName, 0, 0);
1987
1988 p = pcap_dump_open(pd, dumpinfo.CurrentFileName);
1989 #ifdef HAVE_LIBCAP_NG
1990 /* Give up CAP_DAC_OVERRIDE capability.
1991 * Only allow it to be restored if the -C or -G flag have been
1992 * set since we may need to create more files later on.
1993 */
1994 capng_update(
1995 CAPNG_DROP,
1996 (Cflag || Gflag ? 0 : CAPNG_PERMITTED)
1997 | CAPNG_EFFECTIVE,
1998 CAP_DAC_OVERRIDE
1999 );
2000 capng_apply(CAPNG_SELECT_BOTH);
2001 #endif /* HAVE_LIBCAP_NG */
2002 if (p == NULL)
2003 error("%s", pcap_geterr(pd));
2004 #ifdef HAVE_CAPSICUM
2005 set_dumper_capsicum_rights(p);
2006 #endif
2007 if (Cflag != 0 || Gflag != 0) {
2008 #ifdef HAVE_CAPSICUM
2009 dumpinfo.WFileName = strdup(basename(WFileName));
2010 if (dumpinfo.WFileName == NULL) {
2011 error("Unable to allocate memory for file %s",
2012 WFileName);
2013 }
2014 dumpinfo.dirfd = open(dirname(WFileName),
2015 O_DIRECTORY | O_RDONLY);
2016 if (dumpinfo.dirfd < 0) {
2017 error("unable to open directory %s",
2018 dirname(WFileName));
2019 }
2020 cap_rights_init(&rights, CAP_CREATE, CAP_FCNTL,
2021 CAP_FTRUNCATE, CAP_LOOKUP, CAP_SEEK, CAP_WRITE);
2022 if (cap_rights_limit(dumpinfo.dirfd, &rights) < 0 &&
2023 errno != ENOSYS) {
2024 error("unable to limit directory rights");
2025 }
2026 if (cap_fcntls_limit(dumpinfo.dirfd, CAP_FCNTL_GETFL) < 0 &&
2027 errno != ENOSYS) {
2028 error("unable to limit dump descriptor fcntls");
2029 }
2030 #else /* !HAVE_CAPSICUM */
2031 dumpinfo.WFileName = WFileName;
2032 #endif
2033 callback = dump_packet_and_trunc;
2034 dumpinfo.pd = pd;
2035 dumpinfo.p = p;
2036 pcap_userdata = (u_char *)&dumpinfo;
2037 } else {
2038 callback = dump_packet;
2039 pcap_userdata = (u_char *)p;
2040 }
2041 #ifdef HAVE_PCAP_DUMP_FLUSH
2042 if (Uflag)
2043 pcap_dump_flush(p);
2044 #endif
2045 } else {
2046 dlt = pcap_datalink(pd);
2047 ndo->ndo_if_printer = get_if_printer(ndo, dlt);
2048 callback = print_packet;
2049 pcap_userdata = (u_char *)ndo;
2050 }
2051
2052 #ifdef SIGNAL_REQ_INFO
2053 /*
2054 * We can't get statistics when reading from a file rather
2055 * than capturing from a device.
2056 */
2057 if (RFileName == NULL)
2058 (void)setsignal(SIGNAL_REQ_INFO, requestinfo);
2059 #endif
2060
2061 if (ndo->ndo_vflag > 0 && WFileName) {
2062 /*
2063 * When capturing to a file, "-v" means tcpdump should,
2064 * every 10 seconds, "v"erbosely report the number of
2065 * packets captured.
2066 */
2067 #ifdef USE_WIN32_MM_TIMER
2068 /* call verbose_stats_dump() each 1000 +/-100msec */
2069 timer_id = timeSetEvent(1000, 100, verbose_stats_dump, 0, TIME_PERIODIC);
2070 setvbuf(stderr, NULL, _IONBF, 0);
2071 #elif defined(HAVE_ALARM)
2072 (void)setsignal(SIGALRM, verbose_stats_dump);
2073 alarm(1);
2074 #endif
2075 }
2076
2077 if (RFileName == NULL) {
2078 /*
2079 * Live capture (if -V was specified, we set RFileName
2080 * to a file from the -V file). Print a message to
2081 * the standard error on UN*X.
2082 */
2083 if (!ndo->ndo_vflag && !WFileName) {
2084 (void)fprintf(stderr,
2085 "%s: verbose output suppressed, use -v or -vv for full protocol decode\n",
2086 program_name);
2087 } else
2088 (void)fprintf(stderr, "%s: ", program_name);
2089 dlt = pcap_datalink(pd);
2090 dlt_name = pcap_datalink_val_to_name(dlt);
2091 if (dlt_name == NULL) {
2092 (void)fprintf(stderr, "listening on %s, link-type %u, capture size %u bytes\n",
2093 device, dlt, ndo->ndo_snaplen);
2094 } else {
2095 (void)fprintf(stderr, "listening on %s, link-type %s (%s), capture size %u bytes\n",
2096 device, dlt_name,
2097 pcap_datalink_val_to_description(dlt), ndo->ndo_snaplen);
2098 }
2099 (void)fflush(stderr);
2100 }
2101
2102 #ifdef HAVE_CAPSICUM
2103 cansandbox = (VFileName == NULL && zflag == NULL);
2104 #ifdef HAVE_CASPER
2105 cansandbox = (cansandbox && (ndo->ndo_nflag || capdns != NULL));
2106 #else
2107 cansandbox = (cansandbox && ndo->ndo_nflag);
2108 #endif /* HAVE_CASPER */
2109 if (cansandbox && cap_enter() < 0 && errno != ENOSYS)
2110 error("unable to enter the capability mode");
2111 #endif /* HAVE_CAPSICUM */
2112
2113 do {
2114 status = pcap_loop(pd, cnt, callback, pcap_userdata);
2115 if (WFileName == NULL) {
2116 /*
2117 * We're printing packets. Flush the printed output,
2118 * so it doesn't get intermingled with error output.
2119 */
2120 if (status == -2) {
2121 /*
2122 * We got interrupted, so perhaps we didn't
2123 * manage to finish a line we were printing.
2124 * Print an extra newline, just in case.
2125 */
2126 putchar('\n');
2127 }
2128 (void)fflush(stdout);
2129 }
2130 if (status == -2) {
2131 /*
2132 * We got interrupted. If we are reading multiple
2133 * files (via -V) set these so that we stop.
2134 */
2135 VFileName = NULL;
2136 ret = NULL;
2137 }
2138 if (status == -1) {
2139 /*
2140 * Error. Report it.
2141 */
2142 (void)fprintf(stderr, "%s: pcap_loop: %s\n",
2143 program_name, pcap_geterr(pd));
2144 }
2145 if (RFileName == NULL) {
2146 /*
2147 * We're doing a live capture. Report the capture
2148 * statistics.
2149 */
2150 info(1);
2151 }
2152 pcap_close(pd);
2153 if (VFileName != NULL) {
2154 ret = get_next_file(VFile, VFileLine);
2155 if (ret) {
2156 int new_dlt;
2157
2158 RFileName = VFileLine;
2159 pd = pcap_open_offline(RFileName, ebuf);
2160 if (pd == NULL)
2161 error("%s", ebuf);
2162 #ifdef HAVE_CAPSICUM
2163 cap_rights_init(&rights, CAP_READ);
2164 if (cap_rights_limit(fileno(pcap_file(pd)),
2165 &rights) < 0 && errno != ENOSYS) {
2166 error("unable to limit pcap descriptor");
2167 }
2168 #endif
2169 new_dlt = pcap_datalink(pd);
2170 if (new_dlt != dlt) {
2171 /*
2172 * The new file has a different
2173 * link-layer header type from the
2174 * previous one.
2175 */
2176 if (WFileName != NULL) {
2177 /*
2178 * We're writing raw packets
2179 * that match the filter to
2180 * a pcap file. pcap files
2181 * don't support multiple
2182 * different link-layer
2183 * header types, so we fail
2184 * here.
2185 */
2186 error("%s: new dlt does not match original", RFileName);
2187 }
2188
2189 /*
2190 * We're printing the decoded packets;
2191 * switch to the new DLT.
2192 *
2193 * To do that, we need to change
2194 * the printer, change the DLT name,
2195 * and recompile the filter with
2196 * the new DLT.
2197 */
2198 dlt = new_dlt;
2199 ndo->ndo_if_printer = get_if_printer(ndo, dlt);
2200 if (pcap_compile(pd, &fcode, cmdbuf, Oflag, netmask) < 0)
2201 error("%s", pcap_geterr(pd));
2202 }
2203
2204 /*
2205 * Set the filter on the new file.
2206 */
2207 if (pcap_setfilter(pd, &fcode) < 0)
2208 error("%s", pcap_geterr(pd));
2209
2210 /*
2211 * Report the new file.
2212 */
2213 dlt_name = pcap_datalink_val_to_name(dlt);
2214 if (dlt_name == NULL) {
2215 fprintf(stderr, "reading from file %s, link-type %u\n",
2216 RFileName, dlt);
2217 } else {
2218 fprintf(stderr,
2219 "reading from file %s, link-type %s (%s)\n",
2220 RFileName, dlt_name,
2221 pcap_datalink_val_to_description(dlt));
2222 }
2223 }
2224 }
2225 }
2226 while (ret != NULL);
2227
2228 free(cmdbuf);
2229 pcap_freecode(&fcode);
2230 exit_tcpdump(status == -1 ? 1 : 0);
2231 }
2232
2233 /* make a clean exit on interrupts */
2234 static RETSIGTYPE
2235 cleanup(int signo _U_)
2236 {
2237 #ifdef USE_WIN32_MM_TIMER
2238 if (timer_id)
2239 timeKillEvent(timer_id);
2240 timer_id = 0;
2241 #elif defined(HAVE_ALARM)
2242 alarm(0);
2243 #endif
2244
2245 #ifdef HAVE_PCAP_BREAKLOOP
2246 /*
2247 * We have "pcap_breakloop()"; use it, so that we do as little
2248 * as possible in the signal handler (it's probably not safe
2249 * to do anything with standard I/O streams in a signal handler -
2250 * the ANSI C standard doesn't say it is).
2251 */
2252 pcap_breakloop(pd);
2253 #else
2254 /*
2255 * We don't have "pcap_breakloop()"; this isn't safe, but
2256 * it's the best we can do. Print the summary if we're
2257 * not reading from a savefile - i.e., if we're doing a
2258 * live capture - and exit.
2259 */
2260 if (pd != NULL && pcap_file(pd) == NULL) {
2261 /*
2262 * We got interrupted, so perhaps we didn't
2263 * manage to finish a line we were printing.
2264 * Print an extra newline, just in case.
2265 */
2266 putchar('\n');
2267 (void)fflush(stdout);
2268 info(1);
2269 }
2270 exit_tcpdump(0);
2271 #endif
2272 }
2273
2274 /*
2275 On windows, we do not use a fork, so we do not care less about
2276 waiting a child processes to die
2277 */
2278 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
2279 static RETSIGTYPE
2280 child_cleanup(int signo _U_)
2281 {
2282 wait(NULL);
2283 }
2284 #endif /* HAVE_FORK && HAVE_VFORK */
2285
2286 static void
2287 info(register int verbose)
2288 {
2289 struct pcap_stat stats;
2290
2291 /*
2292 * Older versions of libpcap didn't set ps_ifdrop on some
2293 * platforms; initialize it to 0 to handle that.
2294 */
2295 stats.ps_ifdrop = 0;
2296 if (pcap_stats(pd, &stats) < 0) {
2297 (void)fprintf(stderr, "pcap_stats: %s\n", pcap_geterr(pd));
2298 infoprint = 0;
2299 return;
2300 }
2301
2302 if (!verbose)
2303 fprintf(stderr, "%s: ", program_name);
2304
2305 (void)fprintf(stderr, "%u packet%s captured", packets_captured,
2306 PLURAL_SUFFIX(packets_captured));
2307 if (!verbose)
2308 fputs(", ", stderr);
2309 else
2310 putc('\n', stderr);
2311 (void)fprintf(stderr, "%u packet%s received by filter", stats.ps_recv,
2312 PLURAL_SUFFIX(stats.ps_recv));
2313 if (!verbose)
2314 fputs(", ", stderr);
2315 else
2316 putc('\n', stderr);
2317 (void)fprintf(stderr, "%u packet%s dropped by kernel", stats.ps_drop,
2318 PLURAL_SUFFIX(stats.ps_drop));
2319 if (stats.ps_ifdrop != 0) {
2320 if (!verbose)
2321 fputs(", ", stderr);
2322 else
2323 putc('\n', stderr);
2324 (void)fprintf(stderr, "%u packet%s dropped by interface\n",
2325 stats.ps_ifdrop, PLURAL_SUFFIX(stats.ps_ifdrop));
2326 } else
2327 putc('\n', stderr);
2328 infoprint = 0;
2329 }
2330
2331 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
2332 #ifdef HAVE_FORK
2333 #define fork_subprocess() fork()
2334 #else
2335 #define fork_subprocess() vfork()
2336 #endif
2337 static void
2338 compress_savefile(const char *filename)
2339 {
2340 pid_t child;
2341
2342 child = fork_subprocess();
2343 if (child == -1) {
2344 fprintf(stderr,
2345 "compress_savefile: fork failed: %s\n",
2346 pcap_strerror(errno));
2347 return;
2348 }
2349 if (child != 0) {
2350 /* Parent process. */
2351 return;
2352 }
2353
2354 /*
2355 * Child process.
2356 * Set to lowest priority so that this doesn't disturb the capture.
2357 */
2358 #ifdef NZERO
2359 setpriority(PRIO_PROCESS, 0, NZERO - 1);
2360 #else
2361 setpriority(PRIO_PROCESS, 0, 19);
2362 #endif
2363 if (execlp(zflag, zflag, filename, (char *)NULL) == -1)
2364 fprintf(stderr,
2365 "compress_savefile: execlp(%s, %s) failed: %s\n",
2366 zflag,
2367 filename,
2368 pcap_strerror(errno));
2369 #ifdef HAVE_FORK
2370 exit(1);
2371 #else
2372 _exit(1);
2373 #endif
2374 }
2375 #else /* HAVE_FORK && HAVE_VFORK */
2376 static void
2377 compress_savefile(const char *filename)
2378 {
2379 fprintf(stderr,
2380 "compress_savefile failed. Functionality not implemented under your system\n");
2381 }
2382 #endif /* HAVE_FORK && HAVE_VFORK */
2383
2384 static void
2385 dump_packet_and_trunc(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
2386 {
2387 struct dump_info *dump_info;
2388
2389 ++packets_captured;
2390
2391 ++infodelay;
2392
2393 dump_info = (struct dump_info *)user;
2394
2395 /*
2396 * XXX - this won't force the file to rotate on the specified time
2397 * boundary, but it will rotate on the first packet received after the
2398 * specified Gflag number of seconds. Note: if a Gflag time boundary
2399 * and a Cflag size boundary coincide, the time rotation will occur
2400 * first thereby cancelling the Cflag boundary (since the file should
2401 * be 0).
2402 */
2403 if (Gflag != 0) {
2404 /* Check if it is time to rotate */
2405 time_t t;
2406
2407 /* Get the current time */
2408 if ((t = time(NULL)) == (time_t)-1) {
2409 error("dump_and_trunc_packet: can't get current_time: %s",
2410 pcap_strerror(errno));
2411 }
2412
2413
2414 /* If the time is greater than the specified window, rotate */
2415 if (t - Gflag_time >= Gflag) {
2416 #ifdef HAVE_CAPSICUM
2417 FILE *fp;
2418 int fd;
2419 #endif
2420
2421 /* Update the Gflag_time */
2422 Gflag_time = t;
2423 /* Update Gflag_count */
2424 Gflag_count++;
2425 /*
2426 * Close the current file and open a new one.
2427 */
2428 pcap_dump_close(dump_info->p);
2429
2430 /*
2431 * Compress the file we just closed, if the user asked for it
2432 */
2433 if (zflag != NULL)
2434 compress_savefile(dump_info->CurrentFileName);
2435
2436 /*
2437 * Check to see if we've exceeded the Wflag (when
2438 * not using Cflag).
2439 */
2440 if (Cflag == 0 && Wflag > 0 && Gflag_count >= Wflag) {
2441 (void)fprintf(stderr, "Maximum file limit reached: %d\n",
2442 Wflag);
2443 info(1);
2444 exit_tcpdump(0);
2445 /* NOTREACHED */
2446 }
2447 if (dump_info->CurrentFileName != NULL)
2448 free(dump_info->CurrentFileName);
2449 /* Allocate space for max filename + \0. */
2450 dump_info->CurrentFileName = (char *)malloc(PATH_MAX + 1);
2451 if (dump_info->CurrentFileName == NULL)
2452 error("dump_packet_and_trunc: malloc");
2453 /*
2454 * Gflag was set otherwise we wouldn't be here. Reset the count
2455 * so multiple files would end with 1,2,3 in the filename.
2456 * The counting is handled with the -C flow after this.
2457 */
2458 Cflag_count = 0;
2459
2460 /*
2461 * This is always the first file in the Cflag
2462 * rotation: e.g. 0
2463 * We also don't need numbering if Cflag is not set.
2464 */
2465 if (Cflag != 0)
2466 MakeFilename(dump_info->CurrentFileName, dump_info->WFileName, 0,
2467 WflagChars);
2468 else
2469 MakeFilename(dump_info->CurrentFileName, dump_info->WFileName, 0, 0);
2470
2471 #ifdef HAVE_LIBCAP_NG
2472 capng_update(CAPNG_ADD, CAPNG_EFFECTIVE, CAP_DAC_OVERRIDE);
2473 capng_apply(CAPNG_SELECT_BOTH);
2474 #endif /* HAVE_LIBCAP_NG */
2475 #ifdef HAVE_CAPSICUM
2476 fd = openat(dump_info->dirfd,
2477 dump_info->CurrentFileName,
2478 O_CREAT | O_WRONLY | O_TRUNC, 0644);
2479 if (fd < 0) {
2480 error("unable to open file %s",
2481 dump_info->CurrentFileName);
2482 }
2483 fp = fdopen(fd, "w");
2484 if (fp == NULL) {
2485 error("unable to fdopen file %s",
2486 dump_info->CurrentFileName);
2487 }
2488 dump_info->p = pcap_dump_fopen(dump_info->pd, fp);
2489 #else /* !HAVE_CAPSICUM */
2490 dump_info->p = pcap_dump_open(dump_info->pd, dump_info->CurrentFileName);
2491 #endif
2492 #ifdef HAVE_LIBCAP_NG
2493 capng_update(CAPNG_DROP, CAPNG_EFFECTIVE, CAP_DAC_OVERRIDE);
2494 capng_apply(CAPNG_SELECT_BOTH);
2495 #endif /* HAVE_LIBCAP_NG */
2496 if (dump_info->p == NULL)
2497 error("%s", pcap_geterr(pd));
2498 #ifdef HAVE_CAPSICUM
2499 set_dumper_capsicum_rights(dump_info->p);
2500 #endif
2501 }
2502 }
2503
2504 /*
2505 * XXX - this won't prevent capture files from getting
2506 * larger than Cflag - the last packet written to the
2507 * file could put it over Cflag.
2508 */
2509 if (Cflag != 0) {
2510 #ifdef HAVE_PCAP_FTELL64
2511 int64_t size = pcap_dump_ftell64(dump_info->p);
2512 #else
2513 /*
2514 * XXX - this only handles a Cflag value > 2^31-1 on
2515 * LP64 platforms; to handle ILP32 (32-bit UN*X and
2516 * Windows) or LLP64 (64-bit Windows) would require
2517 * a version of libpcap with pcap_dump_ftell64().
2518 */
2519 long size = pcap_dump_ftell(dump_info->p);
2520 #endif
2521
2522 if (size == -1)
2523 error("ftell fails on output file");
2524 if (size > Cflag) {
2525 #ifdef HAVE_CAPSICUM
2526 FILE *fp;
2527 int fd;
2528 #endif
2529
2530 /*
2531 * Close the current file and open a new one.
2532 */
2533 pcap_dump_close(dump_info->p);
2534
2535 /*
2536 * Compress the file we just closed, if the user
2537 * asked for it.
2538 */
2539 if (zflag != NULL)
2540 compress_savefile(dump_info->CurrentFileName);
2541
2542 Cflag_count++;
2543 if (Wflag > 0) {
2544 if (Cflag_count >= Wflag)
2545 Cflag_count = 0;
2546 }
2547 if (dump_info->CurrentFileName != NULL)
2548 free(dump_info->CurrentFileName);
2549 dump_info->CurrentFileName = (char *)malloc(PATH_MAX + 1);
2550 if (dump_info->CurrentFileName == NULL)
2551 error("dump_packet_and_trunc: malloc");
2552 MakeFilename(dump_info->CurrentFileName, dump_info->WFileName, Cflag_count, WflagChars);
2553 #ifdef HAVE_LIBCAP_NG
2554 capng_update(CAPNG_ADD, CAPNG_EFFECTIVE, CAP_DAC_OVERRIDE);
2555 capng_apply(CAPNG_SELECT_BOTH);
2556 #endif /* HAVE_LIBCAP_NG */
2557 #ifdef HAVE_CAPSICUM
2558 fd = openat(dump_info->dirfd, dump_info->CurrentFileName,
2559 O_CREAT | O_WRONLY | O_TRUNC, 0644);
2560 if (fd < 0) {
2561 error("unable to open file %s",
2562 dump_info->CurrentFileName);
2563 }
2564 fp = fdopen(fd, "w");
2565 if (fp == NULL) {
2566 error("unable to fdopen file %s",
2567 dump_info->CurrentFileName);
2568 }
2569 dump_info->p = pcap_dump_fopen(dump_info->pd, fp);
2570 #else /* !HAVE_CAPSICUM */
2571 dump_info->p = pcap_dump_open(dump_info->pd, dump_info->CurrentFileName);
2572 #endif
2573 #ifdef HAVE_LIBCAP_NG
2574 capng_update(CAPNG_DROP, CAPNG_EFFECTIVE, CAP_DAC_OVERRIDE);
2575 capng_apply(CAPNG_SELECT_BOTH);
2576 #endif /* HAVE_LIBCAP_NG */
2577 if (dump_info->p == NULL)
2578 error("%s", pcap_geterr(pd));
2579 #ifdef HAVE_CAPSICUM
2580 set_dumper_capsicum_rights(dump_info->p);
2581 #endif
2582 }
2583 }
2584
2585 pcap_dump((u_char *)dump_info->p, h, sp);
2586 #ifdef HAVE_PCAP_DUMP_FLUSH
2587 if (Uflag)
2588 pcap_dump_flush(dump_info->p);
2589 #endif
2590
2591 --infodelay;
2592 if (infoprint)
2593 info(0);
2594 }
2595
2596 static void
2597 dump_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
2598 {
2599 ++packets_captured;
2600
2601 ++infodelay;
2602
2603 pcap_dump(user, h, sp);
2604 #ifdef HAVE_PCAP_DUMP_FLUSH
2605 if (Uflag)
2606 pcap_dump_flush((pcap_dumper_t *)user);
2607 #endif
2608
2609 --infodelay;
2610 if (infoprint)
2611 info(0);
2612 }
2613
2614 static void
2615 print_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
2616 {
2617 ++packets_captured;
2618
2619 ++infodelay;
2620
2621 pretty_print_packet((netdissect_options *)user, h, sp, packets_captured);
2622
2623 --infodelay;
2624 if (infoprint)
2625 info(0);
2626 }
2627
2628 #ifdef _WIN32
2629 /*
2630 * XXX - there should really be libpcap calls to get the version
2631 * number as a string (the string would be generated from #defines
2632 * at run time, so that it's not generated from string constants
2633 * in the library, as, on many UNIX systems, those constants would
2634 * be statically linked into the application executable image, and
2635 * would thus reflect the version of libpcap on the system on
2636 * which the application was *linked*, not the system on which it's
2637 * *running*.
2638 *
2639 * That routine should be documented, unlike the "version[]"
2640 * string, so that UNIX vendors providing their own libpcaps
2641 * don't omit it (as a couple of vendors have...).
2642 *
2643 * Packet.dll should perhaps also export a routine to return the
2644 * version number of the Packet.dll code, to supply the
2645 * "Wpcap_version" information on Windows.
2646 */
2647 char WDversion[]="current-git.tcpdump.org";
2648 #if !defined(HAVE_GENERATED_VERSION)
2649 char version[]="current-git.tcpdump.org";
2650 #endif
2651 char pcap_version[]="current-git.tcpdump.org";
2652 char Wpcap_version[]="3.1";
2653 #endif
2654
2655 #ifdef SIGNAL_REQ_INFO
2656 RETSIGTYPE requestinfo(int signo _U_)
2657 {
2658 if (infodelay)
2659 ++infoprint;
2660 else
2661 info(0);
2662 }
2663 #endif
2664
2665 /*
2666 * Called once each second in verbose mode while dumping to file
2667 */
2668 #ifdef USE_WIN32_MM_TIMER
2669 void CALLBACK verbose_stats_dump (UINT timer_id _U_, UINT msg _U_, DWORD_PTR arg _U_,
2670 DWORD_PTR dw1 _U_, DWORD_PTR dw2 _U_)
2671 {
2672 if (infodelay == 0)
2673 fprintf(stderr, "Got %u\r", packets_captured);
2674 }
2675 #elif defined(HAVE_ALARM)
2676 static void verbose_stats_dump(int sig _U_)
2677 {
2678 if (infodelay == 0)
2679 fprintf(stderr, "Got %u\r", packets_captured);
2680 alarm(1);
2681 }
2682 #endif
2683
2684 USES_APPLE_DEPRECATED_API
2685 static void
2686 print_version(void)
2687 {
2688 extern char version[];
2689 #ifndef HAVE_PCAP_LIB_VERSION
2690 #if defined(_WIN32) || defined(HAVE_PCAP_VERSION)
2691 extern char pcap_version[];
2692 #else /* defined(_WIN32) || defined(HAVE_PCAP_VERSION) */
2693 static char pcap_version[] = "unknown";
2694 #endif /* defined(_WIN32) || defined(HAVE_PCAP_VERSION) */
2695 #endif /* HAVE_PCAP_LIB_VERSION */
2696 const char *smi_version_string;
2697
2698 #ifdef HAVE_PCAP_LIB_VERSION
2699 #ifdef _WIN32
2700 (void)fprintf(stderr, "%s version %s, based on tcpdump version %s\n", program_name, WDversion, version);
2701 #else /* _WIN32 */
2702 (void)fprintf(stderr, "%s version %s\n", program_name, version);
2703 #endif /* _WIN32 */
2704 (void)fprintf(stderr, "%s\n",pcap_lib_version());
2705 #else /* HAVE_PCAP_LIB_VERSION */
2706 #ifdef _WIN32
2707 (void)fprintf(stderr, "%s version %s, based on tcpdump version %s\n", program_name, WDversion, version);
2708 (void)fprintf(stderr, "WinPcap version %s, based on libpcap version %s\n",Wpcap_version, pcap_version);
2709 #else /* _WIN32 */
2710 (void)fprintf(stderr, "%s version %s\n", program_name, version);
2711 (void)fprintf(stderr, "libpcap version %s\n", pcap_version);
2712 #endif /* _WIN32 */
2713 #endif /* HAVE_PCAP_LIB_VERSION */
2714
2715 #if defined(HAVE_LIBCRYPTO) && defined(SSLEAY_VERSION)
2716 (void)fprintf (stderr, "%s\n", SSLeay_version(SSLEAY_VERSION));
2717 #endif
2718
2719 smi_version_string = nd_smi_version_string();
2720 if (smi_version_string != NULL)
2721 (void)fprintf (stderr, "SMI-library: %s\n", smi_version_string);
2722
2723 #if defined(__SANITIZE_ADDRESS__)
2724 (void)fprintf (stderr, "Compiled with AddressSanitizer/GCC.\n");
2725 #elif defined(__has_feature)
2726 # if __has_feature(address_sanitizer)
2727 (void)fprintf (stderr, "Compiled with AddressSanitizer/CLang.\n");
2728 # endif
2729 #endif /* __SANITIZE_ADDRESS__ or __has_feature */
2730 }
2731 USES_APPLE_RST
2732
2733 static void
2734 print_usage(void)
2735 {
2736 print_version();
2737 (void)fprintf(stderr,
2738 "Usage: %s [-aAbd" D_FLAG "efhH" I_FLAG J_FLAG "KlLnNOpqStu" U_FLAG "vxX#]" B_FLAG_USAGE " [ -c count ]\n", program_name);
2739 (void)fprintf(stderr,
2740 "\t\t[ -C file_size ] [ -E algo:secret ] [ -F file ] [ -G seconds ]\n");
2741 (void)fprintf(stderr,
2742 "\t\t[ -i interface ]" j_FLAG_USAGE " [ -M secret ] [ --number ]\n");
2743 #ifdef HAVE_PCAP_SETDIRECTION
2744 (void)fprintf(stderr,
2745 "\t\t[ -Q in|out|inout ]\n");
2746 #endif
2747 (void)fprintf(stderr,
2748 "\t\t[ -r file ] [ -s snaplen ] ");
2749 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
2750 (void)fprintf(stderr, "[ --time-stamp-precision precision ]\n");
2751 (void)fprintf(stderr,
2752 "\t\t");
2753 #endif
2754 #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
2755 (void)fprintf(stderr, "[ --immediate-mode ] ");
2756 #endif
2757 (void)fprintf(stderr, "[ -T type ] [ --version ] [ -V file ]\n");
2758 (void)fprintf(stderr,
2759 "\t\t[ -w file ] [ -W filecount ] [ -y datalinktype ] [ -z postrotate-command ]\n");
2760 (void)fprintf(stderr,
2761 "\t\t[ -Z user ] [ expression ]\n");
2762 }
2763 /*
2764 * Local Variables:
2765 * c-style: whitesmith
2766 * c-basic-offset: 8
2767 * End:
2768 */