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