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