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