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