]> The Tcpdump Group git mirrors - tcpdump/blob - tcpdump.c
01bd7522c05f591bd250a75bf42447c6945d7381
[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 if (ndo->ndo_snaplen != 0) {
1049 /*
1050 * A snapshot length was explicitly specified;
1051 * use it.
1052 */
1053 status = pcap_set_snaplen(pc, ndo->ndo_snaplen);
1054 if (status != 0)
1055 error("%s: Can't set snapshot length: %s",
1056 device, pcap_statustostr(status));
1057 }
1058 status = pcap_set_promisc(pc, !pflag);
1059 if (status != 0)
1060 error("%s: Can't set promiscuous mode: %s",
1061 device, pcap_statustostr(status));
1062 if (Iflag) {
1063 status = pcap_set_rfmon(pc, 1);
1064 if (status != 0)
1065 error("%s: Can't set monitor mode: %s",
1066 device, pcap_statustostr(status));
1067 }
1068 status = pcap_set_timeout(pc, 1000);
1069 if (status != 0)
1070 error("%s: pcap_set_timeout failed: %s",
1071 device, pcap_statustostr(status));
1072 if (Bflag != 0) {
1073 status = pcap_set_buffer_size(pc, Bflag);
1074 if (status != 0)
1075 error("%s: Can't set buffer size: %s",
1076 device, pcap_statustostr(status));
1077 }
1078 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
1079 if (jflag != -1) {
1080 status = pcap_set_tstamp_type(pc, jflag);
1081 if (status < 0)
1082 error("%s: Can't set time stamp type: %s",
1083 device, pcap_statustostr(status));
1084 }
1085 #endif
1086 status = pcap_activate(pc);
1087 if (status < 0) {
1088 /*
1089 * pcap_activate() failed.
1090 */
1091 cp = pcap_geterr(pc);
1092 if (status == PCAP_ERROR)
1093 error("%s", cp);
1094 else if (status == PCAP_ERROR_NO_SUCH_DEVICE) {
1095 /*
1096 * Return an error for our caller to handle.
1097 */
1098 snprintf(ebuf, PCAP_ERRBUF_SIZE, "%s: %s\n(%s)",
1099 device, pcap_statustostr(status), cp);
1100 pcap_close(pc);
1101 return (NULL);
1102 } else if (status == PCAP_ERROR_PERM_DENIED && *cp != '\0')
1103 error("%s: %s\n(%s)", device,
1104 pcap_statustostr(status), cp);
1105 #ifdef __FreeBSD__
1106 else if (status == PCAP_ERROR_RFMON_NOTSUP &&
1107 strncmp(device, "wlan", 4) == 0) {
1108 char parent[8], newdev[8];
1109 char sysctl[32];
1110 size_t s = sizeof(parent);
1111
1112 snprintf(sysctl, sizeof(sysctl),
1113 "net.wlan.%d.%%parent", atoi(device + 4));
1114 sysctlbyname(sysctl, parent, &s, NULL, 0);
1115 strlcpy(newdev, device, sizeof(newdev));
1116 /* Suggest a new wlan device. */
1117 /* FIXME: incrementing the index this way is not going to work well
1118 * when the index is 9 or greater but the only consequence in this
1119 * specific case would be an error message that looks a bit odd.
1120 */
1121 newdev[strlen(newdev)-1]++;
1122 error("%s is not a monitor mode VAP\n"
1123 "To create a new monitor mode VAP use:\n"
1124 " ifconfig %s create wlandev %s wlanmode monitor\n"
1125 "and use %s as the tcpdump interface",
1126 device, newdev, parent, newdev);
1127 }
1128 #endif
1129 else
1130 error("%s: %s", device,
1131 pcap_statustostr(status));
1132 } else if (status > 0) {
1133 /*
1134 * pcap_activate() succeeded, but it's warning us
1135 * of a problem it had.
1136 */
1137 cp = pcap_geterr(pc);
1138 if (status == PCAP_WARNING)
1139 warning("%s", cp);
1140 else if (status == PCAP_WARNING_PROMISC_NOTSUP &&
1141 *cp != '\0')
1142 warning("%s: %s\n(%s)", device,
1143 pcap_statustostr(status), cp);
1144 else
1145 warning("%s: %s", device,
1146 pcap_statustostr(status));
1147 }
1148 #ifdef HAVE_PCAP_SETDIRECTION
1149 if (Qflag != -1) {
1150 status = pcap_setdirection(pc, Qflag);
1151 if (status != 0)
1152 error("%s: pcap_setdirection() failed: %s",
1153 device, pcap_geterr(pc));
1154 }
1155 #endif /* HAVE_PCAP_SETDIRECTION */
1156 #else /* HAVE_PCAP_CREATE */
1157 *ebuf = '\0';
1158 /*
1159 * If no snapshot length was specified, or a length of 0 was
1160 * specified, default to 256KB.
1161 */
1162 if (ndo->ndo_snaplen == 0)
1163 ndo->ndo_snaplen = 262144;
1164 pc = pcap_open_live(device, ndo->ndo_snaplen, !pflag, 1000, ebuf);
1165 if (pc == NULL) {
1166 /*
1167 * If this failed with "No such device", that means
1168 * the interface doesn't exist; return NULL, so that
1169 * the caller can see whether the device name is
1170 * actually an interface index.
1171 */
1172 if (strstr(ebuf, "No such device") != NULL)
1173 return (NULL);
1174 error("%s", ebuf);
1175 }
1176 if (*ebuf)
1177 warning("%s", ebuf);
1178 #endif /* HAVE_PCAP_CREATE */
1179
1180 return (pc);
1181 }
1182
1183 int
1184 main(int argc, char **argv)
1185 {
1186 register int cnt, op, i;
1187 bpf_u_int32 localnet =0 , netmask = 0;
1188 int timezone_offset = 0;
1189 register char *cp, *infile, *cmdbuf, *device, *RFileName, *VFileName, *WFileName;
1190 pcap_handler callback;
1191 int dlt;
1192 const char *dlt_name;
1193 struct bpf_program fcode;
1194 #ifndef _WIN32
1195 RETSIGTYPE (*oldhandler)(int);
1196 #endif
1197 struct dump_info dumpinfo;
1198 u_char *pcap_userdata;
1199 char ebuf[PCAP_ERRBUF_SIZE];
1200 char VFileLine[PATH_MAX + 1];
1201 char *username = NULL;
1202 char *chroot_dir = NULL;
1203 char *ret = NULL;
1204 char *end;
1205 #ifdef HAVE_PCAP_FINDALLDEVS
1206 pcap_if_t *devlist;
1207 long devnum;
1208 #endif
1209 int status;
1210 FILE *VFile;
1211 #ifdef HAVE_CAPSICUM
1212 cap_rights_t rights;
1213 int cansandbox;
1214 #endif /* HAVE_CAPSICUM */
1215 int Oflag = 1; /* run filter code optimizer */
1216 int yflag_dlt = -1;
1217 const char *yflag_dlt_name = NULL;
1218
1219 netdissect_options Ndo;
1220 netdissect_options *ndo = &Ndo;
1221
1222 /*
1223 * Initialize the netdissect code.
1224 */
1225 if (nd_init(ebuf, sizeof ebuf) == -1)
1226 error("%s", ebuf);
1227
1228 memset(ndo, 0, sizeof(*ndo));
1229 ndo_set_function_pointers(ndo);
1230
1231 cnt = -1;
1232 device = NULL;
1233 infile = NULL;
1234 RFileName = NULL;
1235 VFileName = NULL;
1236 VFile = NULL;
1237 WFileName = NULL;
1238 dlt = -1;
1239 if ((cp = strrchr(argv[0], '/')) != NULL)
1240 ndo->program_name = program_name = cp + 1;
1241 else
1242 ndo->program_name = program_name = argv[0];
1243
1244 #ifdef _WIN32
1245 if (pcap_wsockinit() != 0)
1246 error("Attempting to initialize Winsock failed");
1247 #endif /* _WIN32 */
1248
1249 /*
1250 * On platforms where the CPU doesn't support unaligned loads,
1251 * force unaligned accesses to abort with SIGBUS, rather than
1252 * being fixed up (slowly) by the OS kernel; on those platforms,
1253 * misaligned accesses are bugs, and we want tcpdump to crash so
1254 * that the bugs are reported.
1255 */
1256 if (abort_on_misalignment(ebuf, sizeof(ebuf)) < 0)
1257 error("%s", ebuf);
1258
1259 while (
1260 (op = getopt_long(argc, argv, SHORTOPTS, longopts, NULL)) != -1)
1261 switch (op) {
1262
1263 case 'a':
1264 /* compatibility for old -a */
1265 break;
1266
1267 case 'A':
1268 ++ndo->ndo_Aflag;
1269 break;
1270
1271 case 'b':
1272 ++ndo->ndo_bflag;
1273 break;
1274
1275 #if defined(HAVE_PCAP_CREATE) || defined(_WIN32)
1276 case 'B':
1277 Bflag = atoi(optarg)*1024;
1278 if (Bflag <= 0)
1279 error("invalid packet buffer size %s", optarg);
1280 break;
1281 #endif /* defined(HAVE_PCAP_CREATE) || defined(_WIN32) */
1282
1283 case 'c':
1284 cnt = atoi(optarg);
1285 if (cnt <= 0)
1286 error("invalid packet count %s", optarg);
1287 break;
1288
1289 case 'C':
1290 Cflag = atoi(optarg) * 1000000;
1291 if (Cflag <= 0)
1292 error("invalid file size %s", optarg);
1293 break;
1294
1295 case 'd':
1296 ++dflag;
1297 break;
1298
1299 case 'D':
1300 Dflag++;
1301 break;
1302
1303 case 'L':
1304 Lflag++;
1305 break;
1306
1307 case 'e':
1308 ++ndo->ndo_eflag;
1309 break;
1310
1311 case 'E':
1312 #ifndef HAVE_LIBCRYPTO
1313 warning("crypto code not compiled in");
1314 #endif
1315 ndo->ndo_espsecret = optarg;
1316 break;
1317
1318 case 'f':
1319 ++ndo->ndo_fflag;
1320 break;
1321
1322 case 'F':
1323 infile = optarg;
1324 break;
1325
1326 case 'G':
1327 Gflag = atoi(optarg);
1328 if (Gflag < 0)
1329 error("invalid number of seconds %s", optarg);
1330
1331 /* We will create one file initially. */
1332 Gflag_count = 0;
1333
1334 /* Grab the current time for rotation use. */
1335 if ((Gflag_time = time(NULL)) == (time_t)-1) {
1336 error("main: can't get current time: %s",
1337 pcap_strerror(errno));
1338 }
1339 break;
1340
1341 case 'h':
1342 print_usage();
1343 exit_tcpdump(0);
1344 break;
1345
1346 case 'H':
1347 ++ndo->ndo_Hflag;
1348 break;
1349
1350 case 'i':
1351 device = optarg;
1352 break;
1353
1354 #ifdef HAVE_PCAP_CREATE
1355 case 'I':
1356 ++Iflag;
1357 break;
1358 #endif /* HAVE_PCAP_CREATE */
1359
1360 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
1361 case 'j':
1362 jflag = pcap_tstamp_type_name_to_val(optarg);
1363 if (jflag < 0)
1364 error("invalid time stamp type %s", optarg);
1365 break;
1366
1367 case 'J':
1368 Jflag++;
1369 break;
1370 #endif
1371
1372 case 'l':
1373 #ifdef _WIN32
1374 /*
1375 * _IOLBF is the same as _IOFBF in Microsoft's C
1376 * libraries; the only alternative they offer
1377 * is _IONBF.
1378 *
1379 * XXX - this should really be checking for MSVC++,
1380 * not _WIN32, if, for example, MinGW has its own
1381 * C library that is more UNIX-compatible.
1382 */
1383 setvbuf(stdout, NULL, _IONBF, 0);
1384 #else /* _WIN32 */
1385 #ifdef HAVE_SETLINEBUF
1386 setlinebuf(stdout);
1387 #else
1388 setvbuf(stdout, NULL, _IOLBF, 0);
1389 #endif
1390 #endif /* _WIN32 */
1391 break;
1392
1393 case 'K':
1394 ++ndo->ndo_Kflag;
1395 break;
1396
1397 case 'm':
1398 if (nd_have_smi_support()) {
1399 if (nd_load_smi_module(optarg, ebuf, sizeof ebuf) == -1)
1400 error("%s", ebuf);
1401 } else {
1402 (void)fprintf(stderr, "%s: ignoring option `-m %s' ",
1403 program_name, optarg);
1404 (void)fprintf(stderr, "(no libsmi support)\n");
1405 }
1406 break;
1407
1408 case 'M':
1409 /* TCP-MD5 shared secret */
1410 #ifndef HAVE_LIBCRYPTO
1411 warning("crypto code not compiled in");
1412 #endif
1413 ndo->ndo_sigsecret = optarg;
1414 break;
1415
1416 case 'n':
1417 ++ndo->ndo_nflag;
1418 break;
1419
1420 case 'N':
1421 ++ndo->ndo_Nflag;
1422 break;
1423
1424 case 'O':
1425 Oflag = 0;
1426 break;
1427
1428 case 'p':
1429 ++pflag;
1430 break;
1431
1432 case 'q':
1433 ++ndo->ndo_qflag;
1434 ++ndo->ndo_suppress_default_print;
1435 break;
1436
1437 #ifdef HAVE_PCAP_SETDIRECTION
1438 case 'Q':
1439 if (ascii_strcasecmp(optarg, "in") == 0)
1440 Qflag = PCAP_D_IN;
1441 else if (ascii_strcasecmp(optarg, "out") == 0)
1442 Qflag = PCAP_D_OUT;
1443 else if (ascii_strcasecmp(optarg, "inout") == 0)
1444 Qflag = PCAP_D_INOUT;
1445 else
1446 error("unknown capture direction `%s'", optarg);
1447 break;
1448 #endif /* HAVE_PCAP_SETDIRECTION */
1449
1450 case 'r':
1451 RFileName = optarg;
1452 break;
1453
1454 case 's':
1455 ndo->ndo_snaplen = strtol(optarg, &end, 0);
1456 if (optarg == end || *end != '\0'
1457 || ndo->ndo_snaplen < 0 || ndo->ndo_snaplen > MAXIMUM_SNAPLEN)
1458 error("invalid snaplen %s", optarg);
1459 break;
1460
1461 case 'S':
1462 ++ndo->ndo_Sflag;
1463 break;
1464
1465 case 't':
1466 ++ndo->ndo_tflag;
1467 break;
1468
1469 case 'T':
1470 if (ascii_strcasecmp(optarg, "vat") == 0)
1471 ndo->ndo_packettype = PT_VAT;
1472 else if (ascii_strcasecmp(optarg, "wb") == 0)
1473 ndo->ndo_packettype = PT_WB;
1474 else if (ascii_strcasecmp(optarg, "rpc") == 0)
1475 ndo->ndo_packettype = PT_RPC;
1476 else if (ascii_strcasecmp(optarg, "rtp") == 0)
1477 ndo->ndo_packettype = PT_RTP;
1478 else if (ascii_strcasecmp(optarg, "rtcp") == 0)
1479 ndo->ndo_packettype = PT_RTCP;
1480 else if (ascii_strcasecmp(optarg, "snmp") == 0)
1481 ndo->ndo_packettype = PT_SNMP;
1482 else if (ascii_strcasecmp(optarg, "cnfp") == 0)
1483 ndo->ndo_packettype = PT_CNFP;
1484 else if (ascii_strcasecmp(optarg, "tftp") == 0)
1485 ndo->ndo_packettype = PT_TFTP;
1486 else if (ascii_strcasecmp(optarg, "aodv") == 0)
1487 ndo->ndo_packettype = PT_AODV;
1488 else if (ascii_strcasecmp(optarg, "carp") == 0)
1489 ndo->ndo_packettype = PT_CARP;
1490 else if (ascii_strcasecmp(optarg, "radius") == 0)
1491 ndo->ndo_packettype = PT_RADIUS;
1492 else if (ascii_strcasecmp(optarg, "zmtp1") == 0)
1493 ndo->ndo_packettype = PT_ZMTP1;
1494 else if (ascii_strcasecmp(optarg, "vxlan") == 0)
1495 ndo->ndo_packettype = PT_VXLAN;
1496 else if (ascii_strcasecmp(optarg, "pgm") == 0)
1497 ndo->ndo_packettype = PT_PGM;
1498 else if (ascii_strcasecmp(optarg, "pgm_zmtp1") == 0)
1499 ndo->ndo_packettype = PT_PGM_ZMTP1;
1500 else if (ascii_strcasecmp(optarg, "lmp") == 0)
1501 ndo->ndo_packettype = PT_LMP;
1502 else if (ascii_strcasecmp(optarg, "resp") == 0)
1503 ndo->ndo_packettype = PT_RESP;
1504 else
1505 error("unknown packet type `%s'", optarg);
1506 break;
1507
1508 case 'u':
1509 ++ndo->ndo_uflag;
1510 break;
1511
1512 #ifdef HAVE_PCAP_DUMP_FLUSH
1513 case 'U':
1514 ++Uflag;
1515 break;
1516 #endif
1517
1518 case 'v':
1519 ++ndo->ndo_vflag;
1520 break;
1521
1522 case 'V':
1523 VFileName = optarg;
1524 break;
1525
1526 case 'w':
1527 WFileName = optarg;
1528 break;
1529
1530 case 'W':
1531 Wflag = atoi(optarg);
1532 if (Wflag <= 0)
1533 error("invalid number of output files %s", optarg);
1534 WflagChars = getWflagChars(Wflag);
1535 break;
1536
1537 case 'x':
1538 ++ndo->ndo_xflag;
1539 ++ndo->ndo_suppress_default_print;
1540 break;
1541
1542 case 'X':
1543 ++ndo->ndo_Xflag;
1544 ++ndo->ndo_suppress_default_print;
1545 break;
1546
1547 case 'y':
1548 yflag_dlt_name = optarg;
1549 yflag_dlt =
1550 pcap_datalink_name_to_val(yflag_dlt_name);
1551 if (yflag_dlt < 0)
1552 error("invalid data link type %s", yflag_dlt_name);
1553 break;
1554
1555 #ifdef HAVE_PCAP_SET_PARSER_DEBUG
1556 case 'Y':
1557 {
1558 /* Undocumented flag */
1559 pcap_set_parser_debug(1);
1560 }
1561 break;
1562 #endif
1563 case 'z':
1564 zflag = optarg;
1565 break;
1566
1567 case 'Z':
1568 username = optarg;
1569 break;
1570
1571 case '#':
1572 ndo->ndo_packet_number = 1;
1573 break;
1574
1575 case OPTION_VERSION:
1576 print_version();
1577 exit_tcpdump(0);
1578 break;
1579
1580 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
1581 case OPTION_TSTAMP_PRECISION:
1582 ndo->ndo_tstamp_precision = tstamp_precision_from_string(optarg);
1583 if (ndo->ndo_tstamp_precision < 0)
1584 error("unsupported time stamp precision");
1585 break;
1586 #endif
1587
1588 #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
1589 case OPTION_IMMEDIATE_MODE:
1590 immediate_mode = 1;
1591 break;
1592 #endif
1593
1594 default:
1595 print_usage();
1596 exit_tcpdump(1);
1597 /* NOTREACHED */
1598 }
1599
1600 #ifdef HAVE_PCAP_FINDALLDEVS
1601 if (Dflag)
1602 show_devices_and_exit();
1603 #endif
1604
1605 switch (ndo->ndo_tflag) {
1606
1607 case 0: /* Default */
1608 case 4: /* Default + Date*/
1609 timezone_offset = gmt2local(0);
1610 break;
1611
1612 case 1: /* No time stamp */
1613 case 2: /* Unix timeval style */
1614 case 3: /* Microseconds since previous packet */
1615 case 5: /* Microseconds since first packet */
1616 break;
1617
1618 default: /* Not supported */
1619 error("only -t, -tt, -ttt, -tttt and -ttttt are supported");
1620 break;
1621 }
1622
1623 if (ndo->ndo_fflag != 0 && (VFileName != NULL || RFileName != NULL))
1624 error("-f can not be used with -V or -r");
1625
1626 if (VFileName != NULL && RFileName != NULL)
1627 error("-V and -r are mutually exclusive.");
1628
1629 #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
1630 /*
1631 * If we're printing dissected packets to the standard output
1632 * rather than saving raw packets to a file, and the standard
1633 * output is a terminal, use immediate mode, as the user's
1634 * probably expecting to see packets pop up immediately.
1635 */
1636 if (WFileName == NULL && isatty(1))
1637 immediate_mode = 1;
1638 #endif
1639
1640 #ifdef WITH_CHROOT
1641 /* if run as root, prepare for chrooting */
1642 if (getuid() == 0 || geteuid() == 0) {
1643 /* future extensibility for cmd-line arguments */
1644 if (!chroot_dir)
1645 chroot_dir = WITH_CHROOT;
1646 }
1647 #endif
1648
1649 #ifdef WITH_USER
1650 /* if run as root, prepare for dropping root privileges */
1651 if (getuid() == 0 || geteuid() == 0) {
1652 /* Run with '-Z root' to restore old behaviour */
1653 if (!username)
1654 username = WITH_USER;
1655 }
1656 #endif
1657
1658 if (RFileName != NULL || VFileName != NULL) {
1659 /*
1660 * If RFileName is non-null, it's the pathname of a
1661 * savefile to read. If VFileName is non-null, it's
1662 * the pathname of a file containing a list of pathnames
1663 * (one per line) of savefiles to read.
1664 *
1665 * In either case, we're reading a savefile, not doing
1666 * a live capture.
1667 */
1668 #ifndef _WIN32
1669 /*
1670 * We don't need network access, so relinquish any set-UID
1671 * or set-GID privileges we have (if any).
1672 *
1673 * We do *not* want set-UID privileges when opening a
1674 * trace file, as that might let the user read other
1675 * people's trace files (especially if we're set-UID
1676 * root).
1677 */
1678 if (setgid(getgid()) != 0 || setuid(getuid()) != 0 )
1679 fprintf(stderr, "Warning: setgid/setuid failed !\n");
1680 #endif /* _WIN32 */
1681 if (VFileName != NULL) {
1682 if (VFileName[0] == '-' && VFileName[1] == '\0')
1683 VFile = stdin;
1684 else
1685 VFile = fopen(VFileName, "r");
1686
1687 if (VFile == NULL)
1688 error("Unable to open file: %s\n", pcap_strerror(errno));
1689
1690 ret = get_next_file(VFile, VFileLine);
1691 if (!ret)
1692 error("Nothing in %s\n", VFileName);
1693 RFileName = VFileLine;
1694 }
1695
1696 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
1697 pd = pcap_open_offline_with_tstamp_precision(RFileName,
1698 ndo->ndo_tstamp_precision, ebuf);
1699 #else
1700 pd = pcap_open_offline(RFileName, ebuf);
1701 #endif
1702
1703 if (pd == NULL)
1704 error("%s", ebuf);
1705 #ifdef HAVE_CAPSICUM
1706 cap_rights_init(&rights, CAP_READ);
1707 if (cap_rights_limit(fileno(pcap_file(pd)), &rights) < 0 &&
1708 errno != ENOSYS) {
1709 error("unable to limit pcap descriptor");
1710 }
1711 #endif
1712 dlt = pcap_datalink(pd);
1713 dlt_name = pcap_datalink_val_to_name(dlt);
1714 if (dlt_name == NULL) {
1715 fprintf(stderr, "reading from file %s, link-type %u\n",
1716 RFileName, dlt);
1717 } else {
1718 fprintf(stderr,
1719 "reading from file %s, link-type %s (%s)\n",
1720 RFileName, dlt_name,
1721 pcap_datalink_val_to_description(dlt));
1722 }
1723 } else {
1724 /*
1725 * We're doing a live capture.
1726 */
1727 if (device == NULL) {
1728 /*
1729 * No interface was specified. Pick one.
1730 */
1731 #ifdef HAVE_PCAP_FINDALLDEVS
1732 /*
1733 * Find the list of interfaces, and pick
1734 * the first interface.
1735 */
1736 if (pcap_findalldevs(&devlist, ebuf) >= 0 &&
1737 devlist != NULL) {
1738 device = strdup(devlist->name);
1739 pcap_freealldevs(devlist);
1740 }
1741 #else /* HAVE_PCAP_FINDALLDEVS */
1742 /*
1743 * Use whatever interface pcap_lookupdev()
1744 * chooses.
1745 */
1746 device = pcap_lookupdev(ebuf);
1747 #endif
1748 if (device == NULL)
1749 error("%s", ebuf);
1750 }
1751
1752 /*
1753 * Try to open the interface with the specified name.
1754 */
1755 pd = open_interface(device, ndo, ebuf);
1756 if (pd == NULL) {
1757 /*
1758 * That failed. If we can get a list of
1759 * interfaces, and the interface name
1760 * is purely numeric, try to use it as
1761 * a 1-based index in the list of
1762 * interfaces.
1763 */
1764 #ifdef HAVE_PCAP_FINDALLDEVS
1765 devnum = parse_interface_number(device);
1766 if (devnum == -1) {
1767 /*
1768 * It's not a number; just report
1769 * the open error and fail.
1770 */
1771 error("%s", ebuf);
1772 }
1773
1774 /*
1775 * OK, it's a number; try to find the
1776 * interface with that index, and try
1777 * to open it.
1778 *
1779 * find_interface_by_number() exits if it
1780 * couldn't be found.
1781 */
1782 device = find_interface_by_number(devnum);
1783 pd = open_interface(device, ndo, ebuf);
1784 if (pd == NULL)
1785 error("%s", ebuf);
1786 #else /* HAVE_PCAP_FINDALLDEVS */
1787 /*
1788 * We can't get a list of interfaces; just
1789 * fail.
1790 */
1791 error("%s", ebuf);
1792 #endif /* HAVE_PCAP_FINDALLDEVS */
1793 }
1794
1795 /*
1796 * Let user own process after capture device has
1797 * been opened.
1798 */
1799 #ifndef _WIN32
1800 if (setgid(getgid()) != 0 || setuid(getuid()) != 0)
1801 fprintf(stderr, "Warning: setgid/setuid failed !\n");
1802 #endif /* _WIN32 */
1803 #if !defined(HAVE_PCAP_CREATE) && defined(_WIN32)
1804 if(Bflag != 0)
1805 if(pcap_setbuff(pd, Bflag)==-1){
1806 error("%s", pcap_geterr(pd));
1807 }
1808 #endif /* !defined(HAVE_PCAP_CREATE) && defined(_WIN32) */
1809 if (Lflag)
1810 show_dlts_and_exit(pd, device);
1811 if (yflag_dlt >= 0) {
1812 #ifdef HAVE_PCAP_SET_DATALINK
1813 if (pcap_set_datalink(pd, yflag_dlt) < 0)
1814 error("%s", pcap_geterr(pd));
1815 #else
1816 /*
1817 * We don't actually support changing the
1818 * data link type, so we only let them
1819 * set it to what it already is.
1820 */
1821 if (yflag_dlt != pcap_datalink(pd)) {
1822 error("%s is not one of the DLTs supported by this device\n",
1823 yflag_dlt_name);
1824 }
1825 #endif
1826 (void)fprintf(stderr, "%s: data link type %s\n",
1827 program_name, yflag_dlt_name);
1828 (void)fflush(stderr);
1829 }
1830 i = pcap_snapshot(pd);
1831 if (ndo->ndo_snaplen < i) {
1832 if (ndo->ndo_snaplen != 0)
1833 warning("snaplen raised from %d to %d", ndo->ndo_snaplen, i);
1834 ndo->ndo_snaplen = i;
1835 } else if (ndo->ndo_snaplen > i) {
1836 warning("snaplen lowered from %d to %d", ndo->ndo_snaplen, i);
1837 ndo->ndo_snaplen = i;
1838 }
1839 if(ndo->ndo_fflag != 0) {
1840 if (pcap_lookupnet(device, &localnet, &netmask, ebuf) < 0) {
1841 warning("foreign (-f) flag used but: %s", ebuf);
1842 }
1843 }
1844
1845 }
1846 if (infile)
1847 cmdbuf = read_infile(infile);
1848 else
1849 cmdbuf = copy_argv(&argv[optind]);
1850
1851 #ifdef HAVE_PCAP_SET_OPTIMIZER_DEBUG
1852 pcap_set_optimizer_debug(dflag);
1853 #endif
1854 if (pcap_compile(pd, &fcode, cmdbuf, Oflag, netmask) < 0)
1855 error("%s", pcap_geterr(pd));
1856 if (dflag) {
1857 bpf_dump(&fcode, dflag);
1858 pcap_close(pd);
1859 free(cmdbuf);
1860 pcap_freecode(&fcode);
1861 exit_tcpdump(0);
1862 }
1863
1864 #ifdef HAVE_CASPER
1865 if (!ndo->ndo_nflag)
1866 capdns = capdns_setup();
1867 #endif /* HAVE_CASPER */
1868
1869 init_print(ndo, localnet, netmask, timezone_offset);
1870
1871 #ifndef _WIN32
1872 (void)setsignal(SIGPIPE, cleanup);
1873 (void)setsignal(SIGTERM, cleanup);
1874 (void)setsignal(SIGINT, cleanup);
1875 #endif /* _WIN32 */
1876 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
1877 (void)setsignal(SIGCHLD, child_cleanup);
1878 #endif
1879 /* Cooperate with nohup(1) */
1880 #ifndef _WIN32
1881 if ((oldhandler = setsignal(SIGHUP, cleanup)) != SIG_DFL)
1882 (void)setsignal(SIGHUP, oldhandler);
1883 #endif /* _WIN32 */
1884
1885 #ifndef _WIN32
1886 /*
1887 * If a user name was specified with "-Z", attempt to switch to
1888 * that user's UID. This would probably be used with sudo,
1889 * to allow tcpdump to be run in a special restricted
1890 * account (if you just want to allow users to open capture
1891 * devices, and can't just give users that permission,
1892 * you'd make tcpdump set-UID or set-GID).
1893 *
1894 * Tcpdump doesn't necessarily write only to one savefile;
1895 * the general only way to allow a -Z instance to write to
1896 * savefiles as the user under whose UID it's run, rather
1897 * than as the user specified with -Z, would thus be to switch
1898 * to the original user ID before opening a capture file and
1899 * then switch back to the -Z user ID after opening the savefile.
1900 * Switching to the -Z user ID only after opening the first
1901 * savefile doesn't handle the general case.
1902 */
1903
1904 if (getuid() == 0 || geteuid() == 0) {
1905 #ifdef HAVE_LIBCAP_NG
1906 /* Initialize capng */
1907 capng_clear(CAPNG_SELECT_BOTH);
1908 if (username) {
1909 capng_updatev(
1910 CAPNG_ADD,
1911 CAPNG_PERMITTED | CAPNG_EFFECTIVE,
1912 CAP_SETUID,
1913 CAP_SETGID,
1914 -1);
1915 }
1916 if (chroot_dir) {
1917 capng_update(
1918 CAPNG_ADD,
1919 CAPNG_PERMITTED | CAPNG_EFFECTIVE,
1920 CAP_SYS_CHROOT
1921 );
1922 }
1923
1924 if (WFileName) {
1925 capng_update(
1926 CAPNG_ADD,
1927 CAPNG_PERMITTED | CAPNG_EFFECTIVE,
1928 CAP_DAC_OVERRIDE
1929 );
1930 }
1931 capng_apply(CAPNG_SELECT_BOTH);
1932 #endif /* HAVE_LIBCAP_NG */
1933 if (username || chroot_dir)
1934 droproot(username, chroot_dir);
1935
1936 }
1937 #endif /* _WIN32 */
1938
1939 if (pcap_setfilter(pd, &fcode) < 0)
1940 error("%s", pcap_geterr(pd));
1941 #ifdef HAVE_CAPSICUM
1942 if (RFileName == NULL && VFileName == NULL) {
1943 static const unsigned long cmds[] = { BIOCGSTATS, BIOCROTZBUF };
1944
1945 /*
1946 * The various libpcap devices use a combination of
1947 * read (bpf), ioctl (bpf, netmap), poll (netmap)
1948 * so we add the relevant access rights.
1949 */
1950 cap_rights_init(&rights, CAP_IOCTL, CAP_READ, CAP_EVENT);
1951 if (cap_rights_limit(pcap_fileno(pd), &rights) < 0 &&
1952 errno != ENOSYS) {
1953 error("unable to limit pcap descriptor");
1954 }
1955 if (cap_ioctls_limit(pcap_fileno(pd), cmds,
1956 sizeof(cmds) / sizeof(cmds[0])) < 0 && errno != ENOSYS) {
1957 error("unable to limit ioctls on pcap descriptor");
1958 }
1959 }
1960 #endif
1961 if (WFileName) {
1962 pcap_dumper_t *p;
1963 /* Do not exceed the default PATH_MAX for files. */
1964 dumpinfo.CurrentFileName = (char *)malloc(PATH_MAX + 1);
1965
1966 if (dumpinfo.CurrentFileName == NULL)
1967 error("malloc of dumpinfo.CurrentFileName");
1968
1969 /* We do not need numbering for dumpfiles if Cflag isn't set. */
1970 if (Cflag != 0)
1971 MakeFilename(dumpinfo.CurrentFileName, WFileName, 0, WflagChars);
1972 else
1973 MakeFilename(dumpinfo.CurrentFileName, WFileName, 0, 0);
1974
1975 p = pcap_dump_open(pd, dumpinfo.CurrentFileName);
1976 #ifdef HAVE_LIBCAP_NG
1977 /* Give up CAP_DAC_OVERRIDE capability.
1978 * Only allow it to be restored if the -C or -G flag have been
1979 * set since we may need to create more files later on.
1980 */
1981 capng_update(
1982 CAPNG_DROP,
1983 (Cflag || Gflag ? 0 : CAPNG_PERMITTED)
1984 | CAPNG_EFFECTIVE,
1985 CAP_DAC_OVERRIDE
1986 );
1987 capng_apply(CAPNG_SELECT_BOTH);
1988 #endif /* HAVE_LIBCAP_NG */
1989 if (p == NULL)
1990 error("%s", pcap_geterr(pd));
1991 #ifdef HAVE_CAPSICUM
1992 set_dumper_capsicum_rights(p);
1993 #endif
1994 if (Cflag != 0 || Gflag != 0) {
1995 #ifdef HAVE_CAPSICUM
1996 dumpinfo.WFileName = strdup(basename(WFileName));
1997 if (dumpinfo.WFileName == NULL) {
1998 error("Unable to allocate memory for file %s",
1999 WFileName);
2000 }
2001 dumpinfo.dirfd = open(dirname(WFileName),
2002 O_DIRECTORY | O_RDONLY);
2003 if (dumpinfo.dirfd < 0) {
2004 error("unable to open directory %s",
2005 dirname(WFileName));
2006 }
2007 cap_rights_init(&rights, CAP_CREATE, CAP_FCNTL,
2008 CAP_FTRUNCATE, CAP_LOOKUP, CAP_SEEK, CAP_WRITE);
2009 if (cap_rights_limit(dumpinfo.dirfd, &rights) < 0 &&
2010 errno != ENOSYS) {
2011 error("unable to limit directory rights");
2012 }
2013 if (cap_fcntls_limit(dumpinfo.dirfd, CAP_FCNTL_GETFL) < 0 &&
2014 errno != ENOSYS) {
2015 error("unable to limit dump descriptor fcntls");
2016 }
2017 #else /* !HAVE_CAPSICUM */
2018 dumpinfo.WFileName = WFileName;
2019 #endif
2020 callback = dump_packet_and_trunc;
2021 dumpinfo.pd = pd;
2022 dumpinfo.p = p;
2023 pcap_userdata = (u_char *)&dumpinfo;
2024 } else {
2025 callback = dump_packet;
2026 pcap_userdata = (u_char *)p;
2027 }
2028 #ifdef HAVE_PCAP_DUMP_FLUSH
2029 if (Uflag)
2030 pcap_dump_flush(p);
2031 #endif
2032 } else {
2033 dlt = pcap_datalink(pd);
2034 ndo->ndo_if_printer = get_if_printer(ndo, dlt);
2035 callback = print_packet;
2036 pcap_userdata = (u_char *)ndo;
2037 }
2038
2039 #ifdef SIGNAL_REQ_INFO
2040 /*
2041 * We can't get statistics when reading from a file rather
2042 * than capturing from a device.
2043 */
2044 if (RFileName == NULL)
2045 (void)setsignal(SIGNAL_REQ_INFO, requestinfo);
2046 #endif
2047
2048 if (ndo->ndo_vflag > 0 && WFileName) {
2049 /*
2050 * When capturing to a file, "-v" means tcpdump should,
2051 * every 10 seconds, "v"erbosely report the number of
2052 * packets captured.
2053 */
2054 #ifdef USE_WIN32_MM_TIMER
2055 /* call verbose_stats_dump() each 1000 +/-100msec */
2056 timer_id = timeSetEvent(1000, 100, verbose_stats_dump, 0, TIME_PERIODIC);
2057 setvbuf(stderr, NULL, _IONBF, 0);
2058 #elif defined(HAVE_ALARM)
2059 (void)setsignal(SIGALRM, verbose_stats_dump);
2060 alarm(1);
2061 #endif
2062 }
2063
2064 if (RFileName == NULL) {
2065 /*
2066 * Live capture (if -V was specified, we set RFileName
2067 * to a file from the -V file). Print a message to
2068 * the standard error on UN*X.
2069 */
2070 if (!ndo->ndo_vflag && !WFileName) {
2071 (void)fprintf(stderr,
2072 "%s: verbose output suppressed, use -v or -vv for full protocol decode\n",
2073 program_name);
2074 } else
2075 (void)fprintf(stderr, "%s: ", program_name);
2076 dlt = pcap_datalink(pd);
2077 dlt_name = pcap_datalink_val_to_name(dlt);
2078 if (dlt_name == NULL) {
2079 (void)fprintf(stderr, "listening on %s, link-type %u, capture size %u bytes\n",
2080 device, dlt, ndo->ndo_snaplen);
2081 } else {
2082 (void)fprintf(stderr, "listening on %s, link-type %s (%s), capture size %u bytes\n",
2083 device, dlt_name,
2084 pcap_datalink_val_to_description(dlt), ndo->ndo_snaplen);
2085 }
2086 (void)fflush(stderr);
2087 }
2088
2089 #ifdef HAVE_CAPSICUM
2090 cansandbox = (VFileName == NULL && zflag == NULL);
2091 #ifdef HAVE_CASPER
2092 cansandbox = (cansandbox && (ndo->ndo_nflag || capdns != NULL));
2093 #else
2094 cansandbox = (cansandbox && ndo->ndo_nflag);
2095 #endif /* HAVE_CASPER */
2096 if (cansandbox && cap_enter() < 0 && errno != ENOSYS)
2097 error("unable to enter the capability mode");
2098 #endif /* HAVE_CAPSICUM */
2099
2100 do {
2101 status = pcap_loop(pd, cnt, callback, pcap_userdata);
2102 if (WFileName == NULL) {
2103 /*
2104 * We're printing packets. Flush the printed output,
2105 * so it doesn't get intermingled with error output.
2106 */
2107 if (status == -2) {
2108 /*
2109 * We got interrupted, so perhaps we didn't
2110 * manage to finish a line we were printing.
2111 * Print an extra newline, just in case.
2112 */
2113 putchar('\n');
2114 }
2115 (void)fflush(stdout);
2116 }
2117 if (status == -2) {
2118 /*
2119 * We got interrupted. If we are reading multiple
2120 * files (via -V) set these so that we stop.
2121 */
2122 VFileName = NULL;
2123 ret = NULL;
2124 }
2125 if (status == -1) {
2126 /*
2127 * Error. Report it.
2128 */
2129 (void)fprintf(stderr, "%s: pcap_loop: %s\n",
2130 program_name, pcap_geterr(pd));
2131 }
2132 if (RFileName == NULL) {
2133 /*
2134 * We're doing a live capture. Report the capture
2135 * statistics.
2136 */
2137 info(1);
2138 }
2139 pcap_close(pd);
2140 if (VFileName != NULL) {
2141 ret = get_next_file(VFile, VFileLine);
2142 if (ret) {
2143 int new_dlt;
2144
2145 RFileName = VFileLine;
2146 pd = pcap_open_offline(RFileName, ebuf);
2147 if (pd == NULL)
2148 error("%s", ebuf);
2149 #ifdef HAVE_CAPSICUM
2150 cap_rights_init(&rights, CAP_READ);
2151 if (cap_rights_limit(fileno(pcap_file(pd)),
2152 &rights) < 0 && errno != ENOSYS) {
2153 error("unable to limit pcap descriptor");
2154 }
2155 #endif
2156 new_dlt = pcap_datalink(pd);
2157 if (new_dlt != dlt) {
2158 /*
2159 * The new file has a different
2160 * link-layer header type from the
2161 * previous one.
2162 */
2163 if (WFileName != NULL) {
2164 /*
2165 * We're writing raw packets
2166 * that match the filter to
2167 * a pcap file. pcap files
2168 * don't support multiple
2169 * different link-layer
2170 * header types, so we fail
2171 * here.
2172 */
2173 error("%s: new dlt does not match original", RFileName);
2174 }
2175
2176 /*
2177 * We're printing the decoded packets;
2178 * switch to the new DLT.
2179 *
2180 * To do that, we need to change
2181 * the printer, change the DLT name,
2182 * and recompile the filter with
2183 * the new DLT.
2184 */
2185 dlt = new_dlt;
2186 ndo->ndo_if_printer = get_if_printer(ndo, dlt);
2187 if (pcap_compile(pd, &fcode, cmdbuf, Oflag, netmask) < 0)
2188 error("%s", pcap_geterr(pd));
2189 }
2190
2191 /*
2192 * Set the filter on the new file.
2193 */
2194 if (pcap_setfilter(pd, &fcode) < 0)
2195 error("%s", pcap_geterr(pd));
2196
2197 /*
2198 * Report the new file.
2199 */
2200 dlt_name = pcap_datalink_val_to_name(dlt);
2201 if (dlt_name == NULL) {
2202 fprintf(stderr, "reading from file %s, link-type %u\n",
2203 RFileName, dlt);
2204 } else {
2205 fprintf(stderr,
2206 "reading from file %s, link-type %s (%s)\n",
2207 RFileName, dlt_name,
2208 pcap_datalink_val_to_description(dlt));
2209 }
2210 }
2211 }
2212 }
2213 while (ret != NULL);
2214
2215 free(cmdbuf);
2216 pcap_freecode(&fcode);
2217 exit_tcpdump(status == -1 ? 1 : 0);
2218 }
2219
2220 /* make a clean exit on interrupts */
2221 static RETSIGTYPE
2222 cleanup(int signo _U_)
2223 {
2224 #ifdef USE_WIN32_MM_TIMER
2225 if (timer_id)
2226 timeKillEvent(timer_id);
2227 timer_id = 0;
2228 #elif defined(HAVE_ALARM)
2229 alarm(0);
2230 #endif
2231
2232 #ifdef HAVE_PCAP_BREAKLOOP
2233 /*
2234 * We have "pcap_breakloop()"; use it, so that we do as little
2235 * as possible in the signal handler (it's probably not safe
2236 * to do anything with standard I/O streams in a signal handler -
2237 * the ANSI C standard doesn't say it is).
2238 */
2239 pcap_breakloop(pd);
2240 #else
2241 /*
2242 * We don't have "pcap_breakloop()"; this isn't safe, but
2243 * it's the best we can do. Print the summary if we're
2244 * not reading from a savefile - i.e., if we're doing a
2245 * live capture - and exit.
2246 */
2247 if (pd != NULL && pcap_file(pd) == NULL) {
2248 /*
2249 * We got interrupted, so perhaps we didn't
2250 * manage to finish a line we were printing.
2251 * Print an extra newline, just in case.
2252 */
2253 putchar('\n');
2254 (void)fflush(stdout);
2255 info(1);
2256 }
2257 exit_tcpdump(0);
2258 #endif
2259 }
2260
2261 /*
2262 On windows, we do not use a fork, so we do not care less about
2263 waiting a child processes to die
2264 */
2265 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
2266 static RETSIGTYPE
2267 child_cleanup(int signo _U_)
2268 {
2269 wait(NULL);
2270 }
2271 #endif /* HAVE_FORK && HAVE_VFORK */
2272
2273 static void
2274 info(register int verbose)
2275 {
2276 struct pcap_stat stats;
2277
2278 /*
2279 * Older versions of libpcap didn't set ps_ifdrop on some
2280 * platforms; initialize it to 0 to handle that.
2281 */
2282 stats.ps_ifdrop = 0;
2283 if (pcap_stats(pd, &stats) < 0) {
2284 (void)fprintf(stderr, "pcap_stats: %s\n", pcap_geterr(pd));
2285 infoprint = 0;
2286 return;
2287 }
2288
2289 if (!verbose)
2290 fprintf(stderr, "%s: ", program_name);
2291
2292 (void)fprintf(stderr, "%u packet%s captured", packets_captured,
2293 PLURAL_SUFFIX(packets_captured));
2294 if (!verbose)
2295 fputs(", ", stderr);
2296 else
2297 putc('\n', stderr);
2298 (void)fprintf(stderr, "%u packet%s received by filter", stats.ps_recv,
2299 PLURAL_SUFFIX(stats.ps_recv));
2300 if (!verbose)
2301 fputs(", ", stderr);
2302 else
2303 putc('\n', stderr);
2304 (void)fprintf(stderr, "%u packet%s dropped by kernel", stats.ps_drop,
2305 PLURAL_SUFFIX(stats.ps_drop));
2306 if (stats.ps_ifdrop != 0) {
2307 if (!verbose)
2308 fputs(", ", stderr);
2309 else
2310 putc('\n', stderr);
2311 (void)fprintf(stderr, "%u packet%s dropped by interface\n",
2312 stats.ps_ifdrop, PLURAL_SUFFIX(stats.ps_ifdrop));
2313 } else
2314 putc('\n', stderr);
2315 infoprint = 0;
2316 }
2317
2318 #if defined(HAVE_FORK) || defined(HAVE_VFORK)
2319 #ifdef HAVE_FORK
2320 #define fork_subprocess() fork()
2321 #else
2322 #define fork_subprocess() vfork()
2323 #endif
2324 static void
2325 compress_savefile(const char *filename)
2326 {
2327 pid_t child;
2328
2329 child = fork_subprocess();
2330 if (child == -1) {
2331 fprintf(stderr,
2332 "compress_savefile: fork failed: %s\n",
2333 pcap_strerror(errno));
2334 return;
2335 }
2336 if (child != 0) {
2337 /* Parent process. */
2338 return;
2339 }
2340
2341 /*
2342 * Child process.
2343 * Set to lowest priority so that this doesn't disturb the capture.
2344 */
2345 #ifdef NZERO
2346 setpriority(PRIO_PROCESS, 0, NZERO - 1);
2347 #else
2348 setpriority(PRIO_PROCESS, 0, 19);
2349 #endif
2350 if (execlp(zflag, zflag, filename, (char *)NULL) == -1)
2351 fprintf(stderr,
2352 "compress_savefile: execlp(%s, %s) failed: %s\n",
2353 zflag,
2354 filename,
2355 pcap_strerror(errno));
2356 #ifdef HAVE_FORK
2357 exit(1);
2358 #else
2359 _exit(1);
2360 #endif
2361 }
2362 #else /* HAVE_FORK && HAVE_VFORK */
2363 static void
2364 compress_savefile(const char *filename)
2365 {
2366 fprintf(stderr,
2367 "compress_savefile failed. Functionality not implemented under your system\n");
2368 }
2369 #endif /* HAVE_FORK && HAVE_VFORK */
2370
2371 static void
2372 dump_packet_and_trunc(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
2373 {
2374 struct dump_info *dump_info;
2375
2376 ++packets_captured;
2377
2378 ++infodelay;
2379
2380 dump_info = (struct dump_info *)user;
2381
2382 /*
2383 * XXX - this won't force the file to rotate on the specified time
2384 * boundary, but it will rotate on the first packet received after the
2385 * specified Gflag number of seconds. Note: if a Gflag time boundary
2386 * and a Cflag size boundary coincide, the time rotation will occur
2387 * first thereby cancelling the Cflag boundary (since the file should
2388 * be 0).
2389 */
2390 if (Gflag != 0) {
2391 /* Check if it is time to rotate */
2392 time_t t;
2393
2394 /* Get the current time */
2395 if ((t = time(NULL)) == (time_t)-1) {
2396 error("dump_and_trunc_packet: can't get current_time: %s",
2397 pcap_strerror(errno));
2398 }
2399
2400
2401 /* If the time is greater than the specified window, rotate */
2402 if (t - Gflag_time >= Gflag) {
2403 #ifdef HAVE_CAPSICUM
2404 FILE *fp;
2405 int fd;
2406 #endif
2407
2408 /* Update the Gflag_time */
2409 Gflag_time = t;
2410 /* Update Gflag_count */
2411 Gflag_count++;
2412 /*
2413 * Close the current file and open a new one.
2414 */
2415 pcap_dump_close(dump_info->p);
2416
2417 /*
2418 * Compress the file we just closed, if the user asked for it
2419 */
2420 if (zflag != NULL)
2421 compress_savefile(dump_info->CurrentFileName);
2422
2423 /*
2424 * Check to see if we've exceeded the Wflag (when
2425 * not using Cflag).
2426 */
2427 if (Cflag == 0 && Wflag > 0 && Gflag_count >= Wflag) {
2428 (void)fprintf(stderr, "Maximum file limit reached: %d\n",
2429 Wflag);
2430 info(1);
2431 exit_tcpdump(0);
2432 /* NOTREACHED */
2433 }
2434 if (dump_info->CurrentFileName != NULL)
2435 free(dump_info->CurrentFileName);
2436 /* Allocate space for max filename + \0. */
2437 dump_info->CurrentFileName = (char *)malloc(PATH_MAX + 1);
2438 if (dump_info->CurrentFileName == NULL)
2439 error("dump_packet_and_trunc: malloc");
2440 /*
2441 * Gflag was set otherwise we wouldn't be here. Reset the count
2442 * so multiple files would end with 1,2,3 in the filename.
2443 * The counting is handled with the -C flow after this.
2444 */
2445 Cflag_count = 0;
2446
2447 /*
2448 * This is always the first file in the Cflag
2449 * rotation: e.g. 0
2450 * We also don't need numbering if Cflag is not set.
2451 */
2452 if (Cflag != 0)
2453 MakeFilename(dump_info->CurrentFileName, dump_info->WFileName, 0,
2454 WflagChars);
2455 else
2456 MakeFilename(dump_info->CurrentFileName, dump_info->WFileName, 0, 0);
2457
2458 #ifdef HAVE_LIBCAP_NG
2459 capng_update(CAPNG_ADD, CAPNG_EFFECTIVE, CAP_DAC_OVERRIDE);
2460 capng_apply(CAPNG_SELECT_BOTH);
2461 #endif /* HAVE_LIBCAP_NG */
2462 #ifdef HAVE_CAPSICUM
2463 fd = openat(dump_info->dirfd,
2464 dump_info->CurrentFileName,
2465 O_CREAT | O_WRONLY | O_TRUNC, 0644);
2466 if (fd < 0) {
2467 error("unable to open file %s",
2468 dump_info->CurrentFileName);
2469 }
2470 fp = fdopen(fd, "w");
2471 if (fp == NULL) {
2472 error("unable to fdopen file %s",
2473 dump_info->CurrentFileName);
2474 }
2475 dump_info->p = pcap_dump_fopen(dump_info->pd, fp);
2476 #else /* !HAVE_CAPSICUM */
2477 dump_info->p = pcap_dump_open(dump_info->pd, dump_info->CurrentFileName);
2478 #endif
2479 #ifdef HAVE_LIBCAP_NG
2480 capng_update(CAPNG_DROP, CAPNG_EFFECTIVE, CAP_DAC_OVERRIDE);
2481 capng_apply(CAPNG_SELECT_BOTH);
2482 #endif /* HAVE_LIBCAP_NG */
2483 if (dump_info->p == NULL)
2484 error("%s", pcap_geterr(pd));
2485 #ifdef HAVE_CAPSICUM
2486 set_dumper_capsicum_rights(dump_info->p);
2487 #endif
2488 }
2489 }
2490
2491 /*
2492 * XXX - this won't prevent capture files from getting
2493 * larger than Cflag - the last packet written to the
2494 * file could put it over Cflag.
2495 */
2496 if (Cflag != 0) {
2497 long size = pcap_dump_ftell(dump_info->p);
2498
2499 if (size == -1)
2500 error("ftell fails on output file");
2501 if (size > Cflag) {
2502 #ifdef HAVE_CAPSICUM
2503 FILE *fp;
2504 int fd;
2505 #endif
2506
2507 /*
2508 * Close the current file and open a new one.
2509 */
2510 pcap_dump_close(dump_info->p);
2511
2512 /*
2513 * Compress the file we just closed, if the user
2514 * asked for it.
2515 */
2516 if (zflag != NULL)
2517 compress_savefile(dump_info->CurrentFileName);
2518
2519 Cflag_count++;
2520 if (Wflag > 0) {
2521 if (Cflag_count >= Wflag)
2522 Cflag_count = 0;
2523 }
2524 if (dump_info->CurrentFileName != NULL)
2525 free(dump_info->CurrentFileName);
2526 dump_info->CurrentFileName = (char *)malloc(PATH_MAX + 1);
2527 if (dump_info->CurrentFileName == NULL)
2528 error("dump_packet_and_trunc: malloc");
2529 MakeFilename(dump_info->CurrentFileName, dump_info->WFileName, Cflag_count, WflagChars);
2530 #ifdef HAVE_LIBCAP_NG
2531 capng_update(CAPNG_ADD, CAPNG_EFFECTIVE, CAP_DAC_OVERRIDE);
2532 capng_apply(CAPNG_SELECT_BOTH);
2533 #endif /* HAVE_LIBCAP_NG */
2534 #ifdef HAVE_CAPSICUM
2535 fd = openat(dump_info->dirfd, dump_info->CurrentFileName,
2536 O_CREAT | O_WRONLY | O_TRUNC, 0644);
2537 if (fd < 0) {
2538 error("unable to open file %s",
2539 dump_info->CurrentFileName);
2540 }
2541 fp = fdopen(fd, "w");
2542 if (fp == NULL) {
2543 error("unable to fdopen file %s",
2544 dump_info->CurrentFileName);
2545 }
2546 dump_info->p = pcap_dump_fopen(dump_info->pd, fp);
2547 #else /* !HAVE_CAPSICUM */
2548 dump_info->p = pcap_dump_open(dump_info->pd, dump_info->CurrentFileName);
2549 #endif
2550 #ifdef HAVE_LIBCAP_NG
2551 capng_update(CAPNG_DROP, CAPNG_EFFECTIVE, CAP_DAC_OVERRIDE);
2552 capng_apply(CAPNG_SELECT_BOTH);
2553 #endif /* HAVE_LIBCAP_NG */
2554 if (dump_info->p == NULL)
2555 error("%s", pcap_geterr(pd));
2556 #ifdef HAVE_CAPSICUM
2557 set_dumper_capsicum_rights(dump_info->p);
2558 #endif
2559 }
2560 }
2561
2562 pcap_dump((u_char *)dump_info->p, h, sp);
2563 #ifdef HAVE_PCAP_DUMP_FLUSH
2564 if (Uflag)
2565 pcap_dump_flush(dump_info->p);
2566 #endif
2567
2568 --infodelay;
2569 if (infoprint)
2570 info(0);
2571 }
2572
2573 static void
2574 dump_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
2575 {
2576 ++packets_captured;
2577
2578 ++infodelay;
2579
2580 pcap_dump(user, h, sp);
2581 #ifdef HAVE_PCAP_DUMP_FLUSH
2582 if (Uflag)
2583 pcap_dump_flush((pcap_dumper_t *)user);
2584 #endif
2585
2586 --infodelay;
2587 if (infoprint)
2588 info(0);
2589 }
2590
2591 static void
2592 print_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
2593 {
2594 ++packets_captured;
2595
2596 ++infodelay;
2597
2598 pretty_print_packet((netdissect_options *)user, h, sp, packets_captured);
2599
2600 --infodelay;
2601 if (infoprint)
2602 info(0);
2603 }
2604
2605 #ifdef _WIN32
2606 /*
2607 * XXX - there should really be libpcap calls to get the version
2608 * number as a string (the string would be generated from #defines
2609 * at run time, so that it's not generated from string constants
2610 * in the library, as, on many UNIX systems, those constants would
2611 * be statically linked into the application executable image, and
2612 * would thus reflect the version of libpcap on the system on
2613 * which the application was *linked*, not the system on which it's
2614 * *running*.
2615 *
2616 * That routine should be documented, unlike the "version[]"
2617 * string, so that UNIX vendors providing their own libpcaps
2618 * don't omit it (as a couple of vendors have...).
2619 *
2620 * Packet.dll should perhaps also export a routine to return the
2621 * version number of the Packet.dll code, to supply the
2622 * "Wpcap_version" information on Windows.
2623 */
2624 char WDversion[]="current-git.tcpdump.org";
2625 #if !defined(HAVE_GENERATED_VERSION)
2626 char version[]="current-git.tcpdump.org";
2627 #endif
2628 char pcap_version[]="current-git.tcpdump.org";
2629 char Wpcap_version[]="3.1";
2630 #endif
2631
2632 #ifdef SIGNAL_REQ_INFO
2633 RETSIGTYPE requestinfo(int signo _U_)
2634 {
2635 if (infodelay)
2636 ++infoprint;
2637 else
2638 info(0);
2639 }
2640 #endif
2641
2642 /*
2643 * Called once each second in verbose mode while dumping to file
2644 */
2645 #ifdef USE_WIN32_MM_TIMER
2646 void CALLBACK verbose_stats_dump (UINT timer_id _U_, UINT msg _U_, DWORD_PTR arg _U_,
2647 DWORD_PTR dw1 _U_, DWORD_PTR dw2 _U_)
2648 {
2649 if (infodelay == 0)
2650 fprintf(stderr, "Got %u\r", packets_captured);
2651 }
2652 #elif defined(HAVE_ALARM)
2653 static void verbose_stats_dump(int sig _U_)
2654 {
2655 if (infodelay == 0)
2656 fprintf(stderr, "Got %u\r", packets_captured);
2657 alarm(1);
2658 }
2659 #endif
2660
2661 USES_APPLE_DEPRECATED_API
2662 static void
2663 print_version(void)
2664 {
2665 extern char version[];
2666 #ifndef HAVE_PCAP_LIB_VERSION
2667 #if defined(_WIN32) || defined(HAVE_PCAP_VERSION)
2668 extern char pcap_version[];
2669 #else /* defined(_WIN32) || defined(HAVE_PCAP_VERSION) */
2670 static char pcap_version[] = "unknown";
2671 #endif /* defined(_WIN32) || defined(HAVE_PCAP_VERSION) */
2672 #endif /* HAVE_PCAP_LIB_VERSION */
2673 const char *smi_version_string;
2674
2675 #ifdef HAVE_PCAP_LIB_VERSION
2676 #ifdef _WIN32
2677 (void)fprintf(stderr, "%s version %s, based on tcpdump version %s\n", program_name, WDversion, version);
2678 #else /* _WIN32 */
2679 (void)fprintf(stderr, "%s version %s\n", program_name, version);
2680 #endif /* _WIN32 */
2681 (void)fprintf(stderr, "%s\n",pcap_lib_version());
2682 #else /* HAVE_PCAP_LIB_VERSION */
2683 #ifdef _WIN32
2684 (void)fprintf(stderr, "%s version %s, based on tcpdump version %s\n", program_name, WDversion, version);
2685 (void)fprintf(stderr, "WinPcap version %s, based on libpcap version %s\n",Wpcap_version, pcap_version);
2686 #else /* _WIN32 */
2687 (void)fprintf(stderr, "%s version %s\n", program_name, version);
2688 (void)fprintf(stderr, "libpcap version %s\n", pcap_version);
2689 #endif /* _WIN32 */
2690 #endif /* HAVE_PCAP_LIB_VERSION */
2691
2692 #if defined(HAVE_LIBCRYPTO) && defined(SSLEAY_VERSION)
2693 (void)fprintf (stderr, "%s\n", SSLeay_version(SSLEAY_VERSION));
2694 #endif
2695
2696 smi_version_string = nd_smi_version_string();
2697 if (smi_version_string != NULL)
2698 (void)fprintf (stderr, "SMI-library: %s\n", smi_version_string);
2699
2700 #if defined(__SANITIZE_ADDRESS__)
2701 (void)fprintf (stderr, "Compiled with AddressSanitizer/GCC.\n");
2702 #elif defined(__has_feature)
2703 # if __has_feature(address_sanitizer)
2704 (void)fprintf (stderr, "Compiled with AddressSanitizer/CLang.\n");
2705 # endif
2706 #endif /* __SANITIZE_ADDRESS__ or __has_feature */
2707 }
2708 USES_APPLE_RST
2709
2710 static void
2711 print_usage(void)
2712 {
2713 print_version();
2714 (void)fprintf(stderr,
2715 "Usage: %s [-aAbd" D_FLAG "efhH" I_FLAG J_FLAG "KlLnNOpqStu" U_FLAG "vxX#]" B_FLAG_USAGE " [ -c count ]\n", program_name);
2716 (void)fprintf(stderr,
2717 "\t\t[ -C file_size ] [ -E algo:secret ] [ -F file ] [ -G seconds ]\n");
2718 (void)fprintf(stderr,
2719 "\t\t[ -i interface ]" j_FLAG_USAGE " [ -M secret ] [ --number ]\n");
2720 #ifdef HAVE_PCAP_SETDIRECTION
2721 (void)fprintf(stderr,
2722 "\t\t[ -Q in|out|inout ]\n");
2723 #endif
2724 (void)fprintf(stderr,
2725 "\t\t[ -r file ] [ -s snaplen ] ");
2726 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
2727 (void)fprintf(stderr, "[ --time-stamp-precision precision ]\n");
2728 (void)fprintf(stderr,
2729 "\t\t");
2730 #endif
2731 #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
2732 (void)fprintf(stderr, "[ --immediate-mode ] ");
2733 #endif
2734 (void)fprintf(stderr, "[ -T type ] [ --version ] [ -V file ]\n");
2735 (void)fprintf(stderr,
2736 "\t\t[ -w file ] [ -W filecount ] [ -y datalinktype ] [ -z postrotate-command ]\n");
2737 (void)fprintf(stderr,
2738 "\t\t[ -Z user ] [ expression ]\n");
2739 }
2740 /*
2741 * Local Variables:
2742 * c-style: whitesmith
2743 * c-basic-offset: 8
2744 * End:
2745 */