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