]> The Tcpdump Group git mirrors - tcpdump/blob - tcpdump.c
CVE-2017-13029/PPP: Fix a bounds check, and clean up other bounds checks.
[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) == -1)
1728 error("%s", ebuf);
1729 if (devlist == NULL)
1730 error("no interfaces available for capture");
1731 device = strdup(devlist->name);
1732 pcap_freealldevs(devlist);
1733 #else /* HAVE_PCAP_FINDALLDEVS */
1734 /*
1735 * Use whatever interface pcap_lookupdev()
1736 * chooses.
1737 */
1738 device = pcap_lookupdev(ebuf);
1739 if (device == NULL)
1740 error("%s", ebuf);
1741 #endif
1742 }
1743
1744 /*
1745 * Try to open the interface with the specified name.
1746 */
1747 pd = open_interface(device, ndo, ebuf);
1748 if (pd == NULL) {
1749 /*
1750 * That failed. If we can get a list of
1751 * interfaces, and the interface name
1752 * is purely numeric, try to use it as
1753 * a 1-based index in the list of
1754 * interfaces.
1755 */
1756 #ifdef HAVE_PCAP_FINDALLDEVS
1757 devnum = parse_interface_number(device);
1758 if (devnum == -1) {
1759 /*
1760 * It's not a number; just report
1761 * the open error and fail.
1762 */
1763 error("%s", ebuf);
1764 }
1765
1766 /*
1767 * OK, it's a number; try to find the
1768 * interface with that index, and try
1769 * to open it.
1770 *
1771 * find_interface_by_number() exits if it
1772 * couldn't be found.
1773 */
1774 device = find_interface_by_number(devnum);
1775 pd = open_interface(device, ndo, ebuf);
1776 if (pd == NULL)
1777 error("%s", ebuf);
1778 #else /* HAVE_PCAP_FINDALLDEVS */
1779 /*
1780 * We can't get a list of interfaces; just
1781 * fail.
1782 */
1783 error("%s", ebuf);
1784 #endif /* HAVE_PCAP_FINDALLDEVS */
1785 }
1786
1787 /*
1788 * Let user own process after capture device has
1789 * been opened.
1790 */
1791 #ifndef _WIN32
1792 if (setgid(getgid()) != 0 || setuid(getuid()) != 0)
1793 fprintf(stderr, "Warning: setgid/setuid failed !\n");
1794 #endif /* _WIN32 */
1795 #if !defined(HAVE_PCAP_CREATE) && defined(_WIN32)
1796 if(Bflag != 0)
1797 if(pcap_setbuff(pd, Bflag)==-1){
1798 error("%s", pcap_geterr(pd));
1799 }
1800 #endif /* !defined(HAVE_PCAP_CREATE) && defined(_WIN32) */
1801 if (Lflag)
1802 show_dlts_and_exit(pd, device);
1803 if (yflag_dlt >= 0) {
1804 #ifdef HAVE_PCAP_SET_DATALINK
1805 if (pcap_set_datalink(pd, yflag_dlt) < 0)
1806 error("%s", pcap_geterr(pd));
1807 #else
1808 /*
1809 * We don't actually support changing the
1810 * data link type, so we only let them
1811 * set it to what it already is.
1812 */
1813 if (yflag_dlt != pcap_datalink(pd)) {
1814 error("%s is not one of the DLTs supported by this device\n",
1815 yflag_dlt_name);
1816 }
1817 #endif
1818 (void)fprintf(stderr, "%s: data link type %s\n",
1819 program_name, yflag_dlt_name);
1820 (void)fflush(stderr);
1821 }
1822 i = pcap_snapshot(pd);
1823 if (ndo->ndo_snaplen < i) {
1824 if (ndo->ndo_snaplen != 0)
1825 warning("snaplen raised from %d to %d", ndo->ndo_snaplen, i);
1826 ndo->ndo_snaplen = i;
1827 } else if (ndo->ndo_snaplen > i) {
1828 warning("snaplen lowered from %d to %d", ndo->ndo_snaplen, i);
1829 ndo->ndo_snaplen = i;
1830 }
1831 if(ndo->ndo_fflag != 0) {
1832 if (pcap_lookupnet(device, &localnet, &netmask, ebuf) < 0) {
1833 warning("foreign (-f) flag used but: %s", ebuf);
1834 }
1835 }
1836
1837 }
1838 if (infile)
1839 cmdbuf = read_infile(infile);
1840 else
1841 cmdbuf = copy_argv(&argv[optind]);
1842
1843 #ifdef HAVE_PCAP_SET_OPTIMIZER_DEBUG
1844 pcap_set_optimizer_debug(dflag);
1845 #endif
1846 if (pcap_compile(pd, &fcode, cmdbuf, Oflag, netmask) < 0)
1847 error("%s", pcap_geterr(pd));
1848 if (dflag) {
1849 bpf_dump(&fcode, dflag);
1850 pcap_close(pd);
1851 free(cmdbuf);
1852 pcap_freecode(&fcode);
1853 exit_tcpdump(0);
1854 }
1855
1856 #ifdef HAVE_CASPER
1857 if (!ndo->ndo_nflag)
1858 capdns = capdns_setup();
1859 #endif /* HAVE_CASPER */
1860
1861 init_print(ndo, localnet, netmask, timezone_offset);
1862
1863 #ifndef _WIN32
1864 (void)setsignal(SIGPIPE, cleanup);
1865 (void)setsignal(SIGTERM, cleanup);
1866 (void)setsignal(SIGINT, cleanup);
1867 #endif /* _WIN32 */
1868 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
1869 (void)setsignal(SIGCHLD, child_cleanup);
1870 #endif
1871 /* Cooperate with nohup(1) */
1872 #ifndef _WIN32
1873 if ((oldhandler = setsignal(SIGHUP, cleanup)) != SIG_DFL)
1874 (void)setsignal(SIGHUP, oldhandler);
1875 #endif /* _WIN32 */
1876
1877 #ifndef _WIN32
1878 /*
1879 * If a user name was specified with "-Z", attempt to switch to
1880 * that user's UID. This would probably be used with sudo,
1881 * to allow tcpdump to be run in a special restricted
1882 * account (if you just want to allow users to open capture
1883 * devices, and can't just give users that permission,
1884 * you'd make tcpdump set-UID or set-GID).
1885 *
1886 * Tcpdump doesn't necessarily write only to one savefile;
1887 * the general only way to allow a -Z instance to write to
1888 * savefiles as the user under whose UID it's run, rather
1889 * than as the user specified with -Z, would thus be to switch
1890 * to the original user ID before opening a capture file and
1891 * then switch back to the -Z user ID after opening the savefile.
1892 * Switching to the -Z user ID only after opening the first
1893 * savefile doesn't handle the general case.
1894 */
1895
1896 if (getuid() == 0 || geteuid() == 0) {
1897 #ifdef HAVE_LIBCAP_NG
1898 /* Initialize capng */
1899 capng_clear(CAPNG_SELECT_BOTH);
1900 if (username) {
1901 capng_updatev(
1902 CAPNG_ADD,
1903 CAPNG_PERMITTED | CAPNG_EFFECTIVE,
1904 CAP_SETUID,
1905 CAP_SETGID,
1906 -1);
1907 }
1908 if (chroot_dir) {
1909 capng_update(
1910 CAPNG_ADD,
1911 CAPNG_PERMITTED | CAPNG_EFFECTIVE,
1912 CAP_SYS_CHROOT
1913 );
1914 }
1915
1916 if (WFileName) {
1917 capng_update(
1918 CAPNG_ADD,
1919 CAPNG_PERMITTED | CAPNG_EFFECTIVE,
1920 CAP_DAC_OVERRIDE
1921 );
1922 }
1923 capng_apply(CAPNG_SELECT_BOTH);
1924 #endif /* HAVE_LIBCAP_NG */
1925 if (username || chroot_dir)
1926 droproot(username, chroot_dir);
1927
1928 }
1929 #endif /* _WIN32 */
1930
1931 if (pcap_setfilter(pd, &fcode) < 0)
1932 error("%s", pcap_geterr(pd));
1933 #ifdef HAVE_CAPSICUM
1934 if (RFileName == NULL && VFileName == NULL) {
1935 static const unsigned long cmds[] = { BIOCGSTATS, BIOCROTZBUF };
1936
1937 /*
1938 * The various libpcap devices use a combination of
1939 * read (bpf), ioctl (bpf, netmap), poll (netmap)
1940 * so we add the relevant access rights.
1941 */
1942 cap_rights_init(&rights, CAP_IOCTL, CAP_READ, CAP_EVENT);
1943 if (cap_rights_limit(pcap_fileno(pd), &rights) < 0 &&
1944 errno != ENOSYS) {
1945 error("unable to limit pcap descriptor");
1946 }
1947 if (cap_ioctls_limit(pcap_fileno(pd), cmds,
1948 sizeof(cmds) / sizeof(cmds[0])) < 0 && errno != ENOSYS) {
1949 error("unable to limit ioctls on pcap descriptor");
1950 }
1951 }
1952 #endif
1953 if (WFileName) {
1954 pcap_dumper_t *p;
1955 /* Do not exceed the default PATH_MAX for files. */
1956 dumpinfo.CurrentFileName = (char *)malloc(PATH_MAX + 1);
1957
1958 if (dumpinfo.CurrentFileName == NULL)
1959 error("malloc of dumpinfo.CurrentFileName");
1960
1961 /* We do not need numbering for dumpfiles if Cflag isn't set. */
1962 if (Cflag != 0)
1963 MakeFilename(dumpinfo.CurrentFileName, WFileName, 0, WflagChars);
1964 else
1965 MakeFilename(dumpinfo.CurrentFileName, WFileName, 0, 0);
1966
1967 p = pcap_dump_open(pd, dumpinfo.CurrentFileName);
1968 #ifdef HAVE_LIBCAP_NG
1969 /* Give up CAP_DAC_OVERRIDE capability.
1970 * Only allow it to be restored if the -C or -G flag have been
1971 * set since we may need to create more files later on.
1972 */
1973 capng_update(
1974 CAPNG_DROP,
1975 (Cflag || Gflag ? 0 : CAPNG_PERMITTED)
1976 | CAPNG_EFFECTIVE,
1977 CAP_DAC_OVERRIDE
1978 );
1979 capng_apply(CAPNG_SELECT_BOTH);
1980 #endif /* HAVE_LIBCAP_NG */
1981 if (p == NULL)
1982 error("%s", pcap_geterr(pd));
1983 #ifdef HAVE_CAPSICUM
1984 set_dumper_capsicum_rights(p);
1985 #endif
1986 if (Cflag != 0 || Gflag != 0) {
1987 #ifdef HAVE_CAPSICUM
1988 dumpinfo.WFileName = strdup(basename(WFileName));
1989 if (dumpinfo.WFileName == NULL) {
1990 error("Unable to allocate memory for file %s",
1991 WFileName);
1992 }
1993 dumpinfo.dirfd = open(dirname(WFileName),
1994 O_DIRECTORY | O_RDONLY);
1995 if (dumpinfo.dirfd < 0) {
1996 error("unable to open directory %s",
1997 dirname(WFileName));
1998 }
1999 cap_rights_init(&rights, CAP_CREATE, CAP_FCNTL,
2000 CAP_FTRUNCATE, CAP_LOOKUP, CAP_SEEK, CAP_WRITE);
2001 if (cap_rights_limit(dumpinfo.dirfd, &rights) < 0 &&
2002 errno != ENOSYS) {
2003 error("unable to limit directory rights");
2004 }
2005 if (cap_fcntls_limit(dumpinfo.dirfd, CAP_FCNTL_GETFL) < 0 &&
2006 errno != ENOSYS) {
2007 error("unable to limit dump descriptor fcntls");
2008 }
2009 #else /* !HAVE_CAPSICUM */
2010 dumpinfo.WFileName = WFileName;
2011 #endif
2012 callback = dump_packet_and_trunc;
2013 dumpinfo.pd = pd;
2014 dumpinfo.p = p;
2015 pcap_userdata = (u_char *)&dumpinfo;
2016 } else {
2017 callback = dump_packet;
2018 pcap_userdata = (u_char *)p;
2019 }
2020 #ifdef HAVE_PCAP_DUMP_FLUSH
2021 if (Uflag)
2022 pcap_dump_flush(p);
2023 #endif
2024 } else {
2025 dlt = pcap_datalink(pd);
2026 ndo->ndo_if_printer = get_if_printer(ndo, dlt);
2027 callback = print_packet;
2028 pcap_userdata = (u_char *)ndo;
2029 }
2030
2031 #ifdef SIGNAL_REQ_INFO
2032 /*
2033 * We can't get statistics when reading from a file rather
2034 * than capturing from a device.
2035 */
2036 if (RFileName == NULL)
2037 (void)setsignal(SIGNAL_REQ_INFO, requestinfo);
2038 #endif
2039
2040 if (ndo->ndo_vflag > 0 && WFileName) {
2041 /*
2042 * When capturing to a file, "-v" means tcpdump should,
2043 * every 10 seconds, "v"erbosely report the number of
2044 * packets captured.
2045 */
2046 #ifdef USE_WIN32_MM_TIMER
2047 /* call verbose_stats_dump() each 1000 +/-100msec */
2048 timer_id = timeSetEvent(1000, 100, verbose_stats_dump, 0, TIME_PERIODIC);
2049 setvbuf(stderr, NULL, _IONBF, 0);
2050 #elif defined(HAVE_ALARM)
2051 (void)setsignal(SIGALRM, verbose_stats_dump);
2052 alarm(1);
2053 #endif
2054 }
2055
2056 if (RFileName == NULL) {
2057 /*
2058 * Live capture (if -V was specified, we set RFileName
2059 * to a file from the -V file). Print a message to
2060 * the standard error on UN*X.
2061 */
2062 if (!ndo->ndo_vflag && !WFileName) {
2063 (void)fprintf(stderr,
2064 "%s: verbose output suppressed, use -v or -vv for full protocol decode\n",
2065 program_name);
2066 } else
2067 (void)fprintf(stderr, "%s: ", program_name);
2068 dlt = pcap_datalink(pd);
2069 dlt_name = pcap_datalink_val_to_name(dlt);
2070 if (dlt_name == NULL) {
2071 (void)fprintf(stderr, "listening on %s, link-type %u, capture size %u bytes\n",
2072 device, dlt, ndo->ndo_snaplen);
2073 } else {
2074 (void)fprintf(stderr, "listening on %s, link-type %s (%s), capture size %u bytes\n",
2075 device, dlt_name,
2076 pcap_datalink_val_to_description(dlt), ndo->ndo_snaplen);
2077 }
2078 (void)fflush(stderr);
2079 }
2080
2081 #ifdef HAVE_CAPSICUM
2082 cansandbox = (VFileName == NULL && zflag == NULL);
2083 #ifdef HAVE_CASPER
2084 cansandbox = (cansandbox && (ndo->ndo_nflag || capdns != NULL));
2085 #else
2086 cansandbox = (cansandbox && ndo->ndo_nflag);
2087 #endif /* HAVE_CASPER */
2088 if (cansandbox && cap_enter() < 0 && errno != ENOSYS)
2089 error("unable to enter the capability mode");
2090 #endif /* HAVE_CAPSICUM */
2091
2092 do {
2093 status = pcap_loop(pd, cnt, callback, pcap_userdata);
2094 if (WFileName == NULL) {
2095 /*
2096 * We're printing packets. Flush the printed output,
2097 * so it doesn't get intermingled with error output.
2098 */
2099 if (status == -2) {
2100 /*
2101 * We got interrupted, so perhaps we didn't
2102 * manage to finish a line we were printing.
2103 * Print an extra newline, just in case.
2104 */
2105 putchar('\n');
2106 }
2107 (void)fflush(stdout);
2108 }
2109 if (status == -2) {
2110 /*
2111 * We got interrupted. If we are reading multiple
2112 * files (via -V) set these so that we stop.
2113 */
2114 VFileName = NULL;
2115 ret = NULL;
2116 }
2117 if (status == -1) {
2118 /*
2119 * Error. Report it.
2120 */
2121 (void)fprintf(stderr, "%s: pcap_loop: %s\n",
2122 program_name, pcap_geterr(pd));
2123 }
2124 if (RFileName == NULL) {
2125 /*
2126 * We're doing a live capture. Report the capture
2127 * statistics.
2128 */
2129 info(1);
2130 }
2131 pcap_close(pd);
2132 if (VFileName != NULL) {
2133 ret = get_next_file(VFile, VFileLine);
2134 if (ret) {
2135 int new_dlt;
2136
2137 RFileName = VFileLine;
2138 pd = pcap_open_offline(RFileName, ebuf);
2139 if (pd == NULL)
2140 error("%s", ebuf);
2141 #ifdef HAVE_CAPSICUM
2142 cap_rights_init(&rights, CAP_READ);
2143 if (cap_rights_limit(fileno(pcap_file(pd)),
2144 &rights) < 0 && errno != ENOSYS) {
2145 error("unable to limit pcap descriptor");
2146 }
2147 #endif
2148 new_dlt = pcap_datalink(pd);
2149 if (new_dlt != dlt) {
2150 /*
2151 * The new file has a different
2152 * link-layer header type from the
2153 * previous one.
2154 */
2155 if (WFileName != NULL) {
2156 /*
2157 * We're writing raw packets
2158 * that match the filter to
2159 * a pcap file. pcap files
2160 * don't support multiple
2161 * different link-layer
2162 * header types, so we fail
2163 * here.
2164 */
2165 error("%s: new dlt does not match original", RFileName);
2166 }
2167
2168 /*
2169 * We're printing the decoded packets;
2170 * switch to the new DLT.
2171 *
2172 * To do that, we need to change
2173 * the printer, change the DLT name,
2174 * and recompile the filter with
2175 * the new DLT.
2176 */
2177 dlt = new_dlt;
2178 ndo->ndo_if_printer = get_if_printer(ndo, dlt);
2179 if (pcap_compile(pd, &fcode, cmdbuf, Oflag, netmask) < 0)
2180 error("%s", pcap_geterr(pd));
2181 }
2182
2183 /*
2184 * Set the filter on the new file.
2185 */
2186 if (pcap_setfilter(pd, &fcode) < 0)
2187 error("%s", pcap_geterr(pd));
2188
2189 /*
2190 * Report the new file.
2191 */
2192 dlt_name = pcap_datalink_val_to_name(dlt);
2193 if (dlt_name == NULL) {
2194 fprintf(stderr, "reading from file %s, link-type %u\n",
2195 RFileName, dlt);
2196 } else {
2197 fprintf(stderr,
2198 "reading from file %s, link-type %s (%s)\n",
2199 RFileName, dlt_name,
2200 pcap_datalink_val_to_description(dlt));
2201 }
2202 }
2203 }
2204 }
2205 while (ret != NULL);
2206
2207 free(cmdbuf);
2208 pcap_freecode(&fcode);
2209 exit_tcpdump(status == -1 ? 1 : 0);
2210 }
2211
2212 /* make a clean exit on interrupts */
2213 static RETSIGTYPE
2214 cleanup(int signo _U_)
2215 {
2216 #ifdef USE_WIN32_MM_TIMER
2217 if (timer_id)
2218 timeKillEvent(timer_id);
2219 timer_id = 0;
2220 #elif defined(HAVE_ALARM)
2221 alarm(0);
2222 #endif
2223
2224 #ifdef HAVE_PCAP_BREAKLOOP
2225 /*
2226 * We have "pcap_breakloop()"; use it, so that we do as little
2227 * as possible in the signal handler (it's probably not safe
2228 * to do anything with standard I/O streams in a signal handler -
2229 * the ANSI C standard doesn't say it is).
2230 */
2231 pcap_breakloop(pd);
2232 #else
2233 /*
2234 * We don't have "pcap_breakloop()"; this isn't safe, but
2235 * it's the best we can do. Print the summary if we're
2236 * not reading from a savefile - i.e., if we're doing a
2237 * live capture - and exit.
2238 */
2239 if (pd != NULL && pcap_file(pd) == NULL) {
2240 /*
2241 * We got interrupted, so perhaps we didn't
2242 * manage to finish a line we were printing.
2243 * Print an extra newline, just in case.
2244 */
2245 putchar('\n');
2246 (void)fflush(stdout);
2247 info(1);
2248 }
2249 exit_tcpdump(0);
2250 #endif
2251 }
2252
2253 /*
2254 On windows, we do not use a fork, so we do not care less about
2255 waiting a child processes to die
2256 */
2257 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
2258 static RETSIGTYPE
2259 child_cleanup(int signo _U_)
2260 {
2261 wait(NULL);
2262 }
2263 #endif /* HAVE_FORK && HAVE_VFORK */
2264
2265 static void
2266 info(register int verbose)
2267 {
2268 struct pcap_stat stats;
2269
2270 /*
2271 * Older versions of libpcap didn't set ps_ifdrop on some
2272 * platforms; initialize it to 0 to handle that.
2273 */
2274 stats.ps_ifdrop = 0;
2275 if (pcap_stats(pd, &stats) < 0) {
2276 (void)fprintf(stderr, "pcap_stats: %s\n", pcap_geterr(pd));
2277 infoprint = 0;
2278 return;
2279 }
2280
2281 if (!verbose)
2282 fprintf(stderr, "%s: ", program_name);
2283
2284 (void)fprintf(stderr, "%u packet%s captured", packets_captured,
2285 PLURAL_SUFFIX(packets_captured));
2286 if (!verbose)
2287 fputs(", ", stderr);
2288 else
2289 putc('\n', stderr);
2290 (void)fprintf(stderr, "%u packet%s received by filter", stats.ps_recv,
2291 PLURAL_SUFFIX(stats.ps_recv));
2292 if (!verbose)
2293 fputs(", ", stderr);
2294 else
2295 putc('\n', stderr);
2296 (void)fprintf(stderr, "%u packet%s dropped by kernel", stats.ps_drop,
2297 PLURAL_SUFFIX(stats.ps_drop));
2298 if (stats.ps_ifdrop != 0) {
2299 if (!verbose)
2300 fputs(", ", stderr);
2301 else
2302 putc('\n', stderr);
2303 (void)fprintf(stderr, "%u packet%s dropped by interface\n",
2304 stats.ps_ifdrop, PLURAL_SUFFIX(stats.ps_ifdrop));
2305 } else
2306 putc('\n', stderr);
2307 infoprint = 0;
2308 }
2309
2310 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
2311 #ifdef HAVE_FORK
2312 #define fork_subprocess() fork()
2313 #else
2314 #define fork_subprocess() vfork()
2315 #endif
2316 static void
2317 compress_savefile(const char *filename)
2318 {
2319 pid_t child;
2320
2321 child = fork_subprocess();
2322 if (child == -1) {
2323 fprintf(stderr,
2324 "compress_savefile: fork failed: %s\n",
2325 pcap_strerror(errno));
2326 return;
2327 }
2328 if (child != 0) {
2329 /* Parent process. */
2330 return;
2331 }
2332
2333 /*
2334 * Child process.
2335 * Set to lowest priority so that this doesn't disturb the capture.
2336 */
2337 #ifdef NZERO
2338 setpriority(PRIO_PROCESS, 0, NZERO - 1);
2339 #else
2340 setpriority(PRIO_PROCESS, 0, 19);
2341 #endif
2342 if (execlp(zflag, zflag, filename, (char *)NULL) == -1)
2343 fprintf(stderr,
2344 "compress_savefile: execlp(%s, %s) failed: %s\n",
2345 zflag,
2346 filename,
2347 pcap_strerror(errno));
2348 #ifdef HAVE_FORK
2349 exit(1);
2350 #else
2351 _exit(1);
2352 #endif
2353 }
2354 #else /* HAVE_FORK && HAVE_VFORK */
2355 static void
2356 compress_savefile(const char *filename)
2357 {
2358 fprintf(stderr,
2359 "compress_savefile failed. Functionality not implemented under your system\n");
2360 }
2361 #endif /* HAVE_FORK && HAVE_VFORK */
2362
2363 static void
2364 dump_packet_and_trunc(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
2365 {
2366 struct dump_info *dump_info;
2367
2368 ++packets_captured;
2369
2370 ++infodelay;
2371
2372 dump_info = (struct dump_info *)user;
2373
2374 /*
2375 * XXX - this won't force the file to rotate on the specified time
2376 * boundary, but it will rotate on the first packet received after the
2377 * specified Gflag number of seconds. Note: if a Gflag time boundary
2378 * and a Cflag size boundary coincide, the time rotation will occur
2379 * first thereby cancelling the Cflag boundary (since the file should
2380 * be 0).
2381 */
2382 if (Gflag != 0) {
2383 /* Check if it is time to rotate */
2384 time_t t;
2385
2386 /* Get the current time */
2387 if ((t = time(NULL)) == (time_t)-1) {
2388 error("dump_and_trunc_packet: can't get current_time: %s",
2389 pcap_strerror(errno));
2390 }
2391
2392
2393 /* If the time is greater than the specified window, rotate */
2394 if (t - Gflag_time >= Gflag) {
2395 #ifdef HAVE_CAPSICUM
2396 FILE *fp;
2397 int fd;
2398 #endif
2399
2400 /* Update the Gflag_time */
2401 Gflag_time = t;
2402 /* Update Gflag_count */
2403 Gflag_count++;
2404 /*
2405 * Close the current file and open a new one.
2406 */
2407 pcap_dump_close(dump_info->p);
2408
2409 /*
2410 * Compress the file we just closed, if the user asked for it
2411 */
2412 if (zflag != NULL)
2413 compress_savefile(dump_info->CurrentFileName);
2414
2415 /*
2416 * Check to see if we've exceeded the Wflag (when
2417 * not using Cflag).
2418 */
2419 if (Cflag == 0 && Wflag > 0 && Gflag_count >= Wflag) {
2420 (void)fprintf(stderr, "Maximum file limit reached: %d\n",
2421 Wflag);
2422 info(1);
2423 exit_tcpdump(0);
2424 /* NOTREACHED */
2425 }
2426 if (dump_info->CurrentFileName != NULL)
2427 free(dump_info->CurrentFileName);
2428 /* Allocate space for max filename + \0. */
2429 dump_info->CurrentFileName = (char *)malloc(PATH_MAX + 1);
2430 if (dump_info->CurrentFileName == NULL)
2431 error("dump_packet_and_trunc: malloc");
2432 /*
2433 * Gflag was set otherwise we wouldn't be here. Reset the count
2434 * so multiple files would end with 1,2,3 in the filename.
2435 * The counting is handled with the -C flow after this.
2436 */
2437 Cflag_count = 0;
2438
2439 /*
2440 * This is always the first file in the Cflag
2441 * rotation: e.g. 0
2442 * We also don't need numbering if Cflag is not set.
2443 */
2444 if (Cflag != 0)
2445 MakeFilename(dump_info->CurrentFileName, dump_info->WFileName, 0,
2446 WflagChars);
2447 else
2448 MakeFilename(dump_info->CurrentFileName, dump_info->WFileName, 0, 0);
2449
2450 #ifdef HAVE_LIBCAP_NG
2451 capng_update(CAPNG_ADD, CAPNG_EFFECTIVE, CAP_DAC_OVERRIDE);
2452 capng_apply(CAPNG_SELECT_BOTH);
2453 #endif /* HAVE_LIBCAP_NG */
2454 #ifdef HAVE_CAPSICUM
2455 fd = openat(dump_info->dirfd,
2456 dump_info->CurrentFileName,
2457 O_CREAT | O_WRONLY | O_TRUNC, 0644);
2458 if (fd < 0) {
2459 error("unable to open file %s",
2460 dump_info->CurrentFileName);
2461 }
2462 fp = fdopen(fd, "w");
2463 if (fp == NULL) {
2464 error("unable to fdopen file %s",
2465 dump_info->CurrentFileName);
2466 }
2467 dump_info->p = pcap_dump_fopen(dump_info->pd, fp);
2468 #else /* !HAVE_CAPSICUM */
2469 dump_info->p = pcap_dump_open(dump_info->pd, dump_info->CurrentFileName);
2470 #endif
2471 #ifdef HAVE_LIBCAP_NG
2472 capng_update(CAPNG_DROP, CAPNG_EFFECTIVE, CAP_DAC_OVERRIDE);
2473 capng_apply(CAPNG_SELECT_BOTH);
2474 #endif /* HAVE_LIBCAP_NG */
2475 if (dump_info->p == NULL)
2476 error("%s", pcap_geterr(pd));
2477 #ifdef HAVE_CAPSICUM
2478 set_dumper_capsicum_rights(dump_info->p);
2479 #endif
2480 }
2481 }
2482
2483 /*
2484 * XXX - this won't prevent capture files from getting
2485 * larger than Cflag - the last packet written to the
2486 * file could put it over Cflag.
2487 */
2488 if (Cflag != 0) {
2489 long size = pcap_dump_ftell(dump_info->p);
2490
2491 if (size == -1)
2492 error("ftell fails on output file");
2493 if (size > Cflag) {
2494 #ifdef HAVE_CAPSICUM
2495 FILE *fp;
2496 int fd;
2497 #endif
2498
2499 /*
2500 * Close the current file and open a new one.
2501 */
2502 pcap_dump_close(dump_info->p);
2503
2504 /*
2505 * Compress the file we just closed, if the user
2506 * asked for it.
2507 */
2508 if (zflag != NULL)
2509 compress_savefile(dump_info->CurrentFileName);
2510
2511 Cflag_count++;
2512 if (Wflag > 0) {
2513 if (Cflag_count >= Wflag)
2514 Cflag_count = 0;
2515 }
2516 if (dump_info->CurrentFileName != NULL)
2517 free(dump_info->CurrentFileName);
2518 dump_info->CurrentFileName = (char *)malloc(PATH_MAX + 1);
2519 if (dump_info->CurrentFileName == NULL)
2520 error("dump_packet_and_trunc: malloc");
2521 MakeFilename(dump_info->CurrentFileName, dump_info->WFileName, Cflag_count, WflagChars);
2522 #ifdef HAVE_LIBCAP_NG
2523 capng_update(CAPNG_ADD, CAPNG_EFFECTIVE, CAP_DAC_OVERRIDE);
2524 capng_apply(CAPNG_SELECT_BOTH);
2525 #endif /* HAVE_LIBCAP_NG */
2526 #ifdef HAVE_CAPSICUM
2527 fd = openat(dump_info->dirfd, dump_info->CurrentFileName,
2528 O_CREAT | O_WRONLY | O_TRUNC, 0644);
2529 if (fd < 0) {
2530 error("unable to open file %s",
2531 dump_info->CurrentFileName);
2532 }
2533 fp = fdopen(fd, "w");
2534 if (fp == NULL) {
2535 error("unable to fdopen file %s",
2536 dump_info->CurrentFileName);
2537 }
2538 dump_info->p = pcap_dump_fopen(dump_info->pd, fp);
2539 #else /* !HAVE_CAPSICUM */
2540 dump_info->p = pcap_dump_open(dump_info->pd, dump_info->CurrentFileName);
2541 #endif
2542 #ifdef HAVE_LIBCAP_NG
2543 capng_update(CAPNG_DROP, CAPNG_EFFECTIVE, CAP_DAC_OVERRIDE);
2544 capng_apply(CAPNG_SELECT_BOTH);
2545 #endif /* HAVE_LIBCAP_NG */
2546 if (dump_info->p == NULL)
2547 error("%s", pcap_geterr(pd));
2548 #ifdef HAVE_CAPSICUM
2549 set_dumper_capsicum_rights(dump_info->p);
2550 #endif
2551 }
2552 }
2553
2554 pcap_dump((u_char *)dump_info->p, h, sp);
2555 #ifdef HAVE_PCAP_DUMP_FLUSH
2556 if (Uflag)
2557 pcap_dump_flush(dump_info->p);
2558 #endif
2559
2560 --infodelay;
2561 if (infoprint)
2562 info(0);
2563 }
2564
2565 static void
2566 dump_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
2567 {
2568 ++packets_captured;
2569
2570 ++infodelay;
2571
2572 pcap_dump(user, h, sp);
2573 #ifdef HAVE_PCAP_DUMP_FLUSH
2574 if (Uflag)
2575 pcap_dump_flush((pcap_dumper_t *)user);
2576 #endif
2577
2578 --infodelay;
2579 if (infoprint)
2580 info(0);
2581 }
2582
2583 static void
2584 print_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
2585 {
2586 ++packets_captured;
2587
2588 ++infodelay;
2589
2590 pretty_print_packet((netdissect_options *)user, h, sp, packets_captured);
2591
2592 --infodelay;
2593 if (infoprint)
2594 info(0);
2595 }
2596
2597 #ifdef _WIN32
2598 /*
2599 * XXX - there should really be libpcap calls to get the version
2600 * number as a string (the string would be generated from #defines
2601 * at run time, so that it's not generated from string constants
2602 * in the library, as, on many UNIX systems, those constants would
2603 * be statically linked into the application executable image, and
2604 * would thus reflect the version of libpcap on the system on
2605 * which the application was *linked*, not the system on which it's
2606 * *running*.
2607 *
2608 * That routine should be documented, unlike the "version[]"
2609 * string, so that UNIX vendors providing their own libpcaps
2610 * don't omit it (as a couple of vendors have...).
2611 *
2612 * Packet.dll should perhaps also export a routine to return the
2613 * version number of the Packet.dll code, to supply the
2614 * "Wpcap_version" information on Windows.
2615 */
2616 char WDversion[]="current-git.tcpdump.org";
2617 #if !defined(HAVE_GENERATED_VERSION)
2618 char version[]="current-git.tcpdump.org";
2619 #endif
2620 char pcap_version[]="current-git.tcpdump.org";
2621 char Wpcap_version[]="3.1";
2622 #endif
2623
2624 #ifdef SIGNAL_REQ_INFO
2625 RETSIGTYPE requestinfo(int signo _U_)
2626 {
2627 if (infodelay)
2628 ++infoprint;
2629 else
2630 info(0);
2631 }
2632 #endif
2633
2634 /*
2635 * Called once each second in verbose mode while dumping to file
2636 */
2637 #ifdef USE_WIN32_MM_TIMER
2638 void CALLBACK verbose_stats_dump (UINT timer_id _U_, UINT msg _U_, DWORD_PTR arg _U_,
2639 DWORD_PTR dw1 _U_, DWORD_PTR dw2 _U_)
2640 {
2641 if (infodelay == 0)
2642 fprintf(stderr, "Got %u\r", packets_captured);
2643 }
2644 #elif defined(HAVE_ALARM)
2645 static void verbose_stats_dump(int sig _U_)
2646 {
2647 if (infodelay == 0)
2648 fprintf(stderr, "Got %u\r", packets_captured);
2649 alarm(1);
2650 }
2651 #endif
2652
2653 USES_APPLE_DEPRECATED_API
2654 static void
2655 print_version(void)
2656 {
2657 extern char version[];
2658 #ifndef HAVE_PCAP_LIB_VERSION
2659 #if defined(_WIN32) || defined(HAVE_PCAP_VERSION)
2660 extern char pcap_version[];
2661 #else /* defined(_WIN32) || defined(HAVE_PCAP_VERSION) */
2662 static char pcap_version[] = "unknown";
2663 #endif /* defined(_WIN32) || defined(HAVE_PCAP_VERSION) */
2664 #endif /* HAVE_PCAP_LIB_VERSION */
2665 const char *smi_version_string;
2666
2667 #ifdef HAVE_PCAP_LIB_VERSION
2668 #ifdef _WIN32
2669 (void)fprintf(stderr, "%s version %s, based on tcpdump version %s\n", program_name, WDversion, version);
2670 #else /* _WIN32 */
2671 (void)fprintf(stderr, "%s version %s\n", program_name, version);
2672 #endif /* _WIN32 */
2673 (void)fprintf(stderr, "%s\n",pcap_lib_version());
2674 #else /* HAVE_PCAP_LIB_VERSION */
2675 #ifdef _WIN32
2676 (void)fprintf(stderr, "%s version %s, based on tcpdump version %s\n", program_name, WDversion, version);
2677 (void)fprintf(stderr, "WinPcap version %s, based on libpcap version %s\n",Wpcap_version, pcap_version);
2678 #else /* _WIN32 */
2679 (void)fprintf(stderr, "%s version %s\n", program_name, version);
2680 (void)fprintf(stderr, "libpcap version %s\n", pcap_version);
2681 #endif /* _WIN32 */
2682 #endif /* HAVE_PCAP_LIB_VERSION */
2683
2684 #if defined(HAVE_LIBCRYPTO) && defined(SSLEAY_VERSION)
2685 (void)fprintf (stderr, "%s\n", SSLeay_version(SSLEAY_VERSION));
2686 #endif
2687
2688 smi_version_string = nd_smi_version_string();
2689 if (smi_version_string != NULL)
2690 (void)fprintf (stderr, "SMI-library: %s\n", smi_version_string);
2691
2692 #if defined(__SANITIZE_ADDRESS__)
2693 (void)fprintf (stderr, "Compiled with AddressSanitizer/GCC.\n");
2694 #elif defined(__has_feature)
2695 # if __has_feature(address_sanitizer)
2696 (void)fprintf (stderr, "Compiled with AddressSanitizer/CLang.\n");
2697 # endif
2698 #endif /* __SANITIZE_ADDRESS__ or __has_feature */
2699 }
2700 USES_APPLE_RST
2701
2702 static void
2703 print_usage(void)
2704 {
2705 print_version();
2706 (void)fprintf(stderr,
2707 "Usage: %s [-aAbd" D_FLAG "efhH" I_FLAG J_FLAG "KlLnNOpqStu" U_FLAG "vxX#]" B_FLAG_USAGE " [ -c count ]\n", program_name);
2708 (void)fprintf(stderr,
2709 "\t\t[ -C file_size ] [ -E algo:secret ] [ -F file ] [ -G seconds ]\n");
2710 (void)fprintf(stderr,
2711 "\t\t[ -i interface ]" j_FLAG_USAGE " [ -M secret ] [ --number ]\n");
2712 #ifdef HAVE_PCAP_SETDIRECTION
2713 (void)fprintf(stderr,
2714 "\t\t[ -Q in|out|inout ]\n");
2715 #endif
2716 (void)fprintf(stderr,
2717 "\t\t[ -r file ] [ -s snaplen ] ");
2718 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
2719 (void)fprintf(stderr, "[ --time-stamp-precision precision ]\n");
2720 (void)fprintf(stderr,
2721 "\t\t");
2722 #endif
2723 #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
2724 (void)fprintf(stderr, "[ --immediate-mode ] ");
2725 #endif
2726 (void)fprintf(stderr, "[ -T type ] [ --version ] [ -V file ]\n");
2727 (void)fprintf(stderr,
2728 "\t\t[ -w file ] [ -W filecount ] [ -y datalinktype ] [ -z postrotate-command ]\n");
2729 (void)fprintf(stderr,
2730 "\t\t[ -Z user ] [ expression ]\n");
2731 }
2732 /*
2733 * Local Variables:
2734 * c-style: whitesmith
2735 * c-basic-offset: 8
2736 * End:
2737 */