2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2009-2013 Daniel Borkmann.
4 * Copyright 2010 Emmanuel Roullit.
5 * Subject to the GPL, version 2.
16 #include <sys/socket.h>
17 #include <sys/types.h>
20 #include <sys/fsuid.h>
47 #include "tstamping.h"
48 #include "dissector.h"
57 char *device_in
, *device_out
, *device_trans
, *filter
, *prefix
;
58 int cpu
, rfraw
, dump
, print_mode
, dump_dir
, packet_type
;
59 unsigned long kpull
, dump_interval
, tx_bytes
, tx_packets
;
61 bool randomize
, promiscuous
, enforce
, jumbo
, dump_bpf
, hwtimestamp
, verbose
;
62 enum pcap_ops_groups pcap
;
63 enum dump_mode dump_mode
;
66 uint32_t link_type
, magic
;
67 uint32_t fanout_group
, fanout_type
;
68 uint64_t pkts_seen
, pkts_recvd
, pkts_drops
;
69 uint64_t pkts_recvd_last
, pkts_drops_last
;
72 static volatile sig_atomic_t sigint
= 0, sighup
= 0;
73 static volatile bool next_dump
= false;
74 static volatile sig_atomic_t sighup_time
= 0;
76 static const char *short_options
=
77 "d:i:o:rf:MNJt:S:k:n:b:HQmcsqXlvhF:RGAP:Vu:g:T:DBUC:K:L:w";
78 static const struct option long_options
[] = {
79 {"dev", required_argument
, NULL
, 'd'},
80 {"in", required_argument
, NULL
, 'i'},
81 {"out", required_argument
, NULL
, 'o'},
82 {"filter", required_argument
, NULL
, 'f'},
83 {"num", required_argument
, NULL
, 'n'},
84 {"type", required_argument
, NULL
, 't'},
85 {"interval", required_argument
, NULL
, 'F'},
86 {"ring-size", required_argument
, NULL
, 'S'},
87 {"kernel-pull", required_argument
, NULL
, 'k'},
88 {"bind-cpu", required_argument
, NULL
, 'b'},
89 {"prefix", required_argument
, NULL
, 'P'},
90 {"user", required_argument
, NULL
, 'u'},
91 {"group", required_argument
, NULL
, 'g'},
92 {"magic", required_argument
, NULL
, 'T'},
93 {"fanout-group", required_argument
, NULL
, 'C'},
94 {"fanout-type", required_argument
, NULL
, 'K'},
95 {"fanout-opts", required_argument
, NULL
, 'L'},
96 {"rand", no_argument
, NULL
, 'r'},
97 {"rfraw", no_argument
, NULL
, 'R'},
98 {"mmap", no_argument
, NULL
, 'm'},
99 {"sg", no_argument
, NULL
, 'G'},
100 {"clrw", no_argument
, NULL
, 'c'},
101 {"jumbo-support", no_argument
, NULL
, 'J'},
102 {"no-promisc", no_argument
, NULL
, 'M'},
103 {"no-hwtimestamp", no_argument
, NULL
, 'N'},
104 {"prio-high", no_argument
, NULL
, 'H'},
105 {"notouch-irq", no_argument
, NULL
, 'Q'},
106 {"dump-pcap-types", no_argument
, NULL
, 'D'},
107 {"dump-bpf", no_argument
, NULL
, 'B'},
108 {"silent", no_argument
, NULL
, 's'},
109 {"less", no_argument
, NULL
, 'q'},
110 {"hex", no_argument
, NULL
, 'X'},
111 {"ascii", no_argument
, NULL
, 'l'},
112 {"no-sock-mem", no_argument
, NULL
, 'A'},
113 {"update", no_argument
, NULL
, 'U'},
114 {"cooked", no_argument
, NULL
, 'w'},
115 {"verbose", no_argument
, NULL
, 'V'},
116 {"version", no_argument
, NULL
, 'v'},
117 {"help", no_argument
, NULL
, 'h'},
121 static const char *copyright
= "Please report bugs to <netsniff-ng@googlegroups.com>\n"
122 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
123 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel.roullit@gmail.com>\n"
124 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
125 "Swiss federal institute of technology (ETH Zurich)\n"
126 "License: GNU GPL version 2.0\n"
127 "This is free software: you are free to change and redistribute it.\n"
128 "There is NO WARRANTY, to the extent permitted by law.";
131 static struct itimerval itimer
;
132 static unsigned long frame_count_max
= 0, interval
= TX_KERNEL_PULL_INT
;
133 static time_t start_time
;
135 #define __pcap_io pcap_ops[ctx->pcap]
137 static void signal_handler(int number
)
147 sighup_time
= (sig_atomic_t)(time(NULL
) - start_time
);
154 static void timer_elapsed(int unused __maybe_unused
)
158 set_itimer_interval_value(&itimer
, 0, interval
);
160 ret
= pull_and_flush_tx_ring(tx_sock
);
161 if (unlikely(ret
< 0)) {
162 /* We could hit EBADF if the socket has been closed before
163 * the timer was triggered.
165 if (errno
!= EBADF
&& errno
!= ENOBUFS
)
166 panic("Flushing TX_RING failed: %s!\n", strerror(errno
));
169 setitimer(ITIMER_REAL
, &itimer
, NULL
);
172 static void timer_purge(void)
176 ret
= pull_and_flush_tx_ring_wait(tx_sock
);
177 if (unlikely(ret
< 0)) {
178 if (errno
!= EBADF
&& errno
!= ENOBUFS
)
179 panic("Flushing TX_RING failed: %s!\n", strerror(errno
));
182 set_itimer_interval_value(&itimer
, 0, 0);
183 setitimer(ITIMER_REAL
, &itimer
, NULL
);
186 static void timer_next_dump(int unused __maybe_unused
)
188 set_itimer_interval_value(&itimer
, interval
, 0);
190 setitimer(ITIMER_REAL
, &itimer
, NULL
);
193 static inline bool dump_to_pcap(struct ctx
*ctx
)
198 static void on_panic_del_rfmon(void *arg
)
200 leave_rfmon_mac80211(arg
);
203 static inline void setup_rfmon_mac80211_dev(struct ctx
*ctx
, char **rfmon_dev
)
205 ctx
->device_trans
= xstrdup(*rfmon_dev
);
208 enter_rfmon_mac80211(ctx
->device_trans
, rfmon_dev
);
209 panic_handler_add(on_panic_del_rfmon
, *rfmon_dev
);
212 static int update_rx_stats(struct ctx
*ctx
, int sock
, bool is_v3
)
214 uint64_t packets
, drops
;
217 ret
= get_rx_net_stats(sock
, &packets
, &drops
, is_v3
);
221 ctx
->pkts_recvd
+= packets
;
222 ctx
->pkts_drops
+= drops
;
223 ctx
->pkts_recvd_last
= packets
;
224 ctx
->pkts_drops_last
= drops
;
229 static void dump_rx_stats(struct ctx
*ctx
, int sock
, bool is_v3
)
231 if (update_rx_stats(ctx
, sock
, is_v3
))
234 printf("\r%12"PRIu64
" packets incoming (%"PRIu64
" unread on exit)\n",
235 is_v3
? ctx
->pkts_seen
: ctx
->pkts_recvd
,
236 is_v3
? ctx
->pkts_recvd
- ctx
->pkts_seen
: 0);
237 printf("\r%12"PRIu64
" packets passed filter\n",
238 ctx
->pkts_recvd
- ctx
->pkts_drops
);
239 printf("\r%12"PRIu64
" packets failed filter (out of space)\n",
242 if (ctx
->pkts_recvd
> 0)
243 printf("\r%12.4lf%% packet droprate\n",
244 (1.0 * ctx
->pkts_drops
/ ctx
->pkts_recvd
) * 100.0);
247 static void pcap_to_xmit(struct ctx
*ctx
)
250 int ifindex
, fd
= 0, ret
;
253 unsigned long trunced
= 0;
255 struct frame_map
*hdr
;
256 struct sock_fprog bpf_ops
;
257 struct timeval start
, end
, diff
;
260 if (!device_up_and_running(ctx
->device_out
) && !ctx
->rfraw
)
261 panic("Device not up and running!\n");
265 tx_sock
= pf_socket();
267 if (!strncmp("-", ctx
->device_in
, strlen("-"))) {
268 fd
= dup_or_die(fileno(stdin
));
269 close(fileno(stdin
));
270 if (ctx
->pcap
== PCAP_OPS_MM
)
271 ctx
->pcap
= PCAP_OPS_SG
;
273 fd
= open_or_die(ctx
->device_in
, O_RDONLY
| O_LARGEFILE
| O_NOATIME
);
276 if (__pcap_io
->init_once_pcap
)
277 __pcap_io
->init_once_pcap(true);
279 ret
= __pcap_io
->pull_fhdr_pcap(fd
, &ctx
->magic
, &ctx
->link_type
);
281 panic("Error reading pcap header!\n");
283 if (__pcap_io
->prepare_access_pcap
) {
284 ret
= __pcap_io
->prepare_access_pcap(fd
, PCAP_MODE_RD
, ctx
->jumbo
);
286 panic("Error prepare reading pcap!\n");
290 setup_rfmon_mac80211_dev(ctx
, &ctx
->device_out
);
292 if (ctx
->link_type
!= LINKTYPE_IEEE802_11
&&
293 ctx
->link_type
!= LINKTYPE_IEEE802_11_RADIOTAP
)
294 panic("Wrong linktype of pcap!\n");
297 ifindex
= device_ifindex(ctx
->device_out
);
298 size
= ring_size(ctx
->device_out
, ctx
->reserve_size
);
300 bpf_parse_rules(ctx
->filter
, &bpf_ops
, ctx
->link_type
);
302 bpf_dump_all(&bpf_ops
);
304 ring_tx_setup(&tx_ring
, tx_sock
, size
, ifindex
, ctx
->jumbo
, ctx
->verbose
);
306 dissector_init_all(ctx
->print_mode
);
308 if (ctx
->cpu
>= 0 && ifindex
> 0) {
309 int irq
= device_irq_number(ctx
->device_out
);
310 device_set_irq_affinity(irq
, ctx
->cpu
);
313 printf("IRQ: %s:%d > CPU%d\n",
314 ctx
->device_out
, irq
, ctx
->cpu
);
318 interval
= ctx
->kpull
;
320 set_itimer_interval_value(&itimer
, 0, interval
);
321 setitimer(ITIMER_REAL
, &itimer
, NULL
);
323 drop_privileges(ctx
->enforce
, ctx
->uid
, ctx
->gid
);
325 printf("Running! Hang up with ^C!\n\n");
328 bug_on(gettimeofday(&start
, NULL
));
330 while (likely(sigint
== 0)) {
331 while (user_may_pull_from_tx(tx_ring
.frames
[it
].iov_base
)) {
332 hdr
= tx_ring
.frames
[it
].iov_base
;
333 out
= ((uint8_t *) hdr
) + TPACKET2_HDRLEN
- sizeof(struct sockaddr_ll
);
336 ret
= __pcap_io
->read_pcap(fd
, &phdr
, ctx
->magic
, out
,
337 ring_frame_size(&tx_ring
));
338 if (unlikely(ret
<= 0))
341 if (ring_frame_size(&tx_ring
) <
342 pcap_get_length(&phdr
, ctx
->magic
)) {
343 pcap_set_length(&phdr
, ctx
->magic
,
344 ring_frame_size(&tx_ring
));
347 } while (ctx
->filter
&&
348 !bpf_run_filter(&bpf_ops
, out
,
349 pcap_get_length(&phdr
, ctx
->magic
)));
351 pcap_pkthdr_to_tpacket_hdr(&phdr
, ctx
->magic
, &hdr
->tp_h
, NULL
);
353 ctx
->tx_bytes
+= hdr
->tp_h
.tp_len
;;
356 show_frame_hdr(out
, hdr
->tp_h
.tp_snaplen
,
357 ctx
->link_type
, hdr
, ctx
->print_mode
,
360 dissector_entry_point(out
, hdr
->tp_h
.tp_snaplen
,
361 ctx
->link_type
, ctx
->print_mode
,
364 kernel_may_pull_from_tx(&hdr
->tp_h
);
367 if (it
>= tx_ring
.layout
.tp_frame_nr
)
370 if (unlikely(sigint
== 1))
373 if (frame_count_max
!= 0) {
374 if (ctx
->tx_packets
>= frame_count_max
) {
383 bug_on(gettimeofday(&end
, NULL
));
384 timersub(&end
, &start
, &diff
);
388 bpf_release(&bpf_ops
);
390 dissector_cleanup_all();
391 destroy_tx_ring(tx_sock
, &tx_ring
);
394 leave_rfmon_mac80211(ctx
->device_out
);
396 if (__pcap_io
->prepare_close_pcap
)
397 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_RD
);
399 if (!strncmp("-", ctx
->device_in
, strlen("-")))
400 dup2(fd
, fileno(stdin
));
407 printf("\r%12lu packets outgoing\n", ctx
->tx_packets
);
408 printf("\r%12lu packets truncated in file\n", trunced
);
409 printf("\r%12lu bytes outgoing\n", ctx
->tx_bytes
);
410 printf("\r%12lu sec, %lu usec in total\n", diff
.tv_sec
, diff
.tv_usec
);
413 static void receive_to_xmit(struct ctx
*ctx
)
417 int rx_sock
, ifindex_in
, ifindex_out
, ret
;
418 size_t size_in
, size_out
;
419 unsigned int it_in
= 0, it_out
= 0;
420 struct frame_map
*hdr_in
, *hdr_out
;
421 struct ring tx_ring
, rx_ring
;
422 struct pollfd rx_poll
;
423 struct sock_fprog bpf_ops
;
425 if (!strncmp(ctx
->device_in
, ctx
->device_out
, IFNAMSIZ
))
426 panic("Ingress/egress devices must be different!\n");
427 if (!device_up_and_running(ctx
->device_out
))
428 panic("Egress device not up and running!\n");
430 rx_sock
= pf_socket();
431 tx_sock
= pf_socket();
433 ifindex_in
= device_ifindex(ctx
->device_in
);
434 ifindex_out
= device_ifindex(ctx
->device_out
);
436 size_in
= ring_size(ctx
->device_in
, ctx
->reserve_size
);
437 size_out
= ring_size(ctx
->device_out
, ctx
->reserve_size
);
439 enable_kernel_bpf_jit_compiler();
441 bpf_parse_rules(ctx
->filter
, &bpf_ops
, ctx
->link_type
);
443 bpf_dump_all(&bpf_ops
);
444 bpf_attach_to_sock(rx_sock
, &bpf_ops
);
446 ring_rx_setup(&rx_ring
, rx_sock
, size_in
, ifindex_in
, &rx_poll
, false, ctx
->jumbo
,
447 ctx
->verbose
, ctx
->fanout_group
, ctx
->fanout_type
);
448 ring_tx_setup(&tx_ring
, tx_sock
, size_out
, ifindex_out
, ctx
->jumbo
, ctx
->verbose
);
450 dissector_init_all(ctx
->print_mode
);
452 if (ctx
->promiscuous
)
453 ifflags
= device_enter_promiscuous_mode(ctx
->device_in
);
456 interval
= ctx
->kpull
;
458 set_itimer_interval_value(&itimer
, 0, interval
);
459 setitimer(ITIMER_REAL
, &itimer
, NULL
);
461 drop_privileges(ctx
->enforce
, ctx
->uid
, ctx
->gid
);
463 printf("Running! Hang up with ^C!\n\n");
466 while (likely(sigint
== 0)) {
467 while (user_may_pull_from_rx(rx_ring
.frames
[it_in
].iov_base
)) {
468 hdr_in
= rx_ring
.frames
[it_in
].iov_base
;
469 in
= ((uint8_t *) hdr_in
) + hdr_in
->tp_h
.tp_mac
;
473 if (ctx
->packet_type
!= -1)
474 if (ctx
->packet_type
!= hdr_in
->s_ll
.sll_pkttype
)
477 hdr_out
= tx_ring
.frames
[it_out
].iov_base
;
478 out
= ((uint8_t *) hdr_out
) + TPACKET2_HDRLEN
- sizeof(struct sockaddr_ll
);
480 while (!user_may_pull_from_tx(tx_ring
.frames
[it_out
].iov_base
) &&
483 next_rnd_slot(&it_out
, &tx_ring
);
486 if (it_out
>= tx_ring
.layout
.tp_frame_nr
)
490 hdr_out
= tx_ring
.frames
[it_out
].iov_base
;
491 out
= ((uint8_t *) hdr_out
) + TPACKET2_HDRLEN
- sizeof(struct sockaddr_ll
);
494 tpacket_hdr_clone(&hdr_out
->tp_h
, &hdr_in
->tp_h
);
495 fmemcpy(out
, in
, hdr_in
->tp_h
.tp_len
);
497 kernel_may_pull_from_tx(&hdr_out
->tp_h
);
499 next_rnd_slot(&it_out
, &tx_ring
);
502 if (it_out
>= tx_ring
.layout
.tp_frame_nr
)
506 show_frame_hdr(in
, hdr_in
->tp_h
.tp_snaplen
,
507 ctx
->link_type
, hdr_in
, ctx
->print_mode
,
510 dissector_entry_point(in
, hdr_in
->tp_h
.tp_snaplen
,
511 ctx
->link_type
, ctx
->print_mode
,
514 if (frame_count_max
!= 0) {
515 if (ctx
->pkts_seen
>= frame_count_max
) {
522 kernel_may_pull_from_rx(&hdr_in
->tp_h
);
525 if (it_in
>= rx_ring
.layout
.tp_frame_nr
)
528 if (unlikely(sigint
== 1))
532 ret
= poll(&rx_poll
, 1, -1);
533 if (unlikely(ret
< 0)) {
535 panic("Poll failed!\n");
542 dump_rx_stats(ctx
, rx_sock
, false);
544 bpf_release(&bpf_ops
);
546 dissector_cleanup_all();
548 destroy_tx_ring(tx_sock
, &tx_ring
);
549 destroy_rx_ring(rx_sock
, &rx_ring
);
551 if (ctx
->promiscuous
)
552 device_leave_promiscuous_mode(ctx
->device_in
, ifflags
);
558 static void translate_pcap_to_txf(int fdo
, uint8_t *out
, size_t len
)
560 size_t bytes_done
= 0;
563 slprintf(bout
, sizeof(bout
), "{\n ");
564 write_or_die(fdo
, bout
, strlen(bout
));
566 while (bytes_done
< len
) {
567 slprintf(bout
, sizeof(bout
), "0x%02x,", out
[bytes_done
]);
568 write_or_die(fdo
, bout
, strlen(bout
));
572 if (bytes_done
% 10 == 0) {
573 slprintf(bout
, sizeof(bout
), "\n");
574 write_or_die(fdo
, bout
, strlen(bout
));
576 if (bytes_done
< len
) {
577 slprintf(bout
, sizeof(bout
), " ");
578 write_or_die(fdo
, bout
, strlen(bout
));
580 } else if (bytes_done
< len
) {
581 slprintf(bout
, sizeof(bout
), " ");
582 write_or_die(fdo
, bout
, strlen(bout
));
585 if (bytes_done
% 10 != 0) {
586 slprintf(bout
, sizeof(bout
), "\n");
587 write_or_die(fdo
, bout
, strlen(bout
));
590 slprintf(bout
, sizeof(bout
), "}\n\n");
591 write_or_die(fdo
, bout
, strlen(bout
));
594 static void read_pcap(struct ctx
*ctx
)
597 int ret
, fd
, fdo
= 0;
598 unsigned long trunced
= 0;
601 struct sock_fprog bpf_ops
;
603 struct timeval start
, end
, diff
;
604 bool is_out_pcap
= ctx
->device_out
&& strstr(ctx
->device_out
, ".pcap");
605 const struct pcap_file_ops
*pcap_out_ops
= pcap_ops
[PCAP_OPS_RW
];
609 if (!strncmp("-", ctx
->device_in
, strlen("-"))) {
610 fd
= dup_or_die(fileno(stdin
));
611 close(fileno(stdin
));
612 if (ctx
->pcap
== PCAP_OPS_MM
)
613 ctx
->pcap
= PCAP_OPS_SG
;
615 /* O_NOATIME requires privileges, in case we don't have
616 * them, retry without them at a minor cost of updating
617 * atime in case the fs has been mounted as such.
619 fd
= open(ctx
->device_in
, O_RDONLY
| O_LARGEFILE
| O_NOATIME
);
620 if (fd
< 0 && errno
== EPERM
)
621 fd
= open_or_die(ctx
->device_in
, O_RDONLY
| O_LARGEFILE
);
623 panic("Cannot open file %s! %s.\n", ctx
->device_in
,
627 if (__pcap_io
->init_once_pcap
)
628 __pcap_io
->init_once_pcap(false);
630 ret
= __pcap_io
->pull_fhdr_pcap(fd
, &ctx
->magic
, &ctx
->link_type
);
632 panic("Error reading pcap header!\n");
634 if (__pcap_io
->prepare_access_pcap
) {
635 ret
= __pcap_io
->prepare_access_pcap(fd
, PCAP_MODE_RD
, ctx
->jumbo
);
637 panic("Error prepare reading pcap!\n");
640 fmemset(&fm
, 0, sizeof(fm
));
642 bpf_parse_rules(ctx
->filter
, &bpf_ops
, ctx
->link_type
);
644 bpf_dump_all(&bpf_ops
);
646 dissector_init_all(ctx
->print_mode
);
648 out_len
= round_up(1024 * 1024, RUNTIME_PAGE_SIZE
);
649 out
= xmalloc_aligned(out_len
, CO_CACHE_LINE_SIZE
);
651 if (ctx
->device_out
) {
652 if (!strncmp("-", ctx
->device_out
, strlen("-"))) {
653 fdo
= dup_or_die(fileno(stdout
));
654 close(fileno(stdout
));
656 fdo
= open_or_die_m(ctx
->device_out
, O_RDWR
| O_CREAT
|
657 O_TRUNC
| O_LARGEFILE
, DEFFILEMODE
);
662 ret
= pcap_out_ops
->push_fhdr_pcap(fdo
, ctx
->magic
,
665 panic("Error writing pcap header!\n");
668 drop_privileges(ctx
->enforce
, ctx
->uid
, ctx
->gid
);
670 printf("Running! Hang up with ^C!\n\n");
673 bug_on(gettimeofday(&start
, NULL
));
675 while (likely(sigint
== 0)) {
677 ret
= __pcap_io
->read_pcap(fd
, &phdr
, ctx
->magic
,
679 if (unlikely(ret
< 0))
682 if (unlikely(pcap_get_length(&phdr
, ctx
->magic
) == 0)) {
687 if (unlikely(pcap_get_length(&phdr
, ctx
->magic
) > out_len
)) {
688 pcap_set_length(&phdr
, ctx
->magic
, out_len
);
691 } while (ctx
->filter
&&
692 !bpf_run_filter(&bpf_ops
, out
,
693 pcap_get_length(&phdr
, ctx
->magic
)));
695 pcap_pkthdr_to_tpacket_hdr(&phdr
, ctx
->magic
, &fm
.tp_h
, &fm
.s_ll
);
697 ctx
->tx_bytes
+= fm
.tp_h
.tp_len
;
700 show_frame_hdr(out
, fm
.tp_h
.tp_snaplen
, ctx
->link_type
, &fm
,
701 ctx
->print_mode
, ctx
->tx_packets
);
703 dissector_entry_point(out
, fm
.tp_h
.tp_snaplen
,
704 ctx
->link_type
, ctx
->print_mode
,
708 size_t pcap_len
= pcap_get_length(&phdr
, ctx
->magic
);
709 int wlen
= pcap_out_ops
->write_pcap(fdo
, &phdr
,
712 if (unlikely(wlen
!= (int)pcap_get_total_length(&phdr
, ctx
->magic
)))
713 panic("Error writing to pcap!\n");
714 } else if (ctx
->device_out
) {
715 translate_pcap_to_txf(fdo
, out
, fm
.tp_h
.tp_snaplen
);
718 if (frame_count_max
!= 0) {
719 if (ctx
->tx_packets
>= frame_count_max
) {
727 bug_on(gettimeofday(&end
, NULL
));
728 timersub(&end
, &start
, &diff
);
730 bpf_release(&bpf_ops
);
732 dissector_cleanup_all();
734 if (__pcap_io
->prepare_close_pcap
)
735 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_RD
);
741 printf("\r%12lu packets outgoing\n", ctx
->tx_packets
);
742 printf("\r%12lu packets truncated in file\n", trunced
);
743 printf("\r%12lu bytes outgoing\n", ctx
->tx_bytes
);
744 printf("\r%12lu sec, %lu usec in total\n", diff
.tv_sec
, diff
.tv_usec
);
746 if (!strncmp("-", ctx
->device_in
, strlen("-")))
747 dup2(fd
, fileno(stdin
));
750 if (ctx
->device_out
) {
751 if (!strncmp("-", ctx
->device_out
, strlen("-")))
752 dup2(fdo
, fileno(stdout
));
757 static void finish_multi_pcap_file(struct ctx
*ctx
, int fd
)
759 __pcap_io
->fsync_pcap(fd
);
761 if (__pcap_io
->prepare_close_pcap
)
762 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_WR
);
766 fmemset(&itimer
, 0, sizeof(itimer
));
767 setitimer(ITIMER_REAL
, &itimer
, NULL
);
770 static int next_multi_pcap_file(struct ctx
*ctx
, int fd
)
776 __pcap_io
->fsync_pcap(fd
);
778 if (__pcap_io
->prepare_close_pcap
)
779 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_WR
);
783 if (sighup_time
> 0) {
784 ftime
= (time_t)(start_time
+ sighup_time
);
789 slprintf(fname
, sizeof(fname
), "%s/%s%lu.pcap", ctx
->device_out
,
790 ctx
->prefix
? : "dump-", ftime
);
792 fd
= open_or_die_m(fname
, O_RDWR
| O_CREAT
| O_TRUNC
|
793 O_LARGEFILE
, DEFFILEMODE
);
795 ret
= __pcap_io
->push_fhdr_pcap(fd
, ctx
->magic
, ctx
->link_type
);
797 panic("Error writing pcap header!\n");
799 if (__pcap_io
->prepare_access_pcap
) {
800 ret
= __pcap_io
->prepare_access_pcap(fd
, PCAP_MODE_WR
, true);
802 panic("Error prepare writing pcap!\n");
808 static void reset_interval(struct ctx
*ctx
)
810 if (ctx
->dump_mode
== DUMP_INTERVAL_TIME
) {
811 interval
= ctx
->dump_interval
;
813 set_itimer_interval_value(&itimer
, interval
, 0);
814 setitimer(ITIMER_REAL
, &itimer
, NULL
);
820 static int begin_multi_pcap_file(struct ctx
*ctx
)
827 if (ctx
->device_out
[strlen(ctx
->device_out
) - 1] == '/')
828 ctx
->device_out
[strlen(ctx
->device_out
) - 1] = 0;
830 slprintf(fname
, sizeof(fname
), "%s/%s%lu.pcap", ctx
->device_out
,
831 ctx
->prefix
? : "dump-", time(NULL
));
833 fd
= open_or_die_m(fname
, O_RDWR
| O_CREAT
| O_TRUNC
|
834 O_LARGEFILE
, DEFFILEMODE
);
836 ret
= __pcap_io
->push_fhdr_pcap(fd
, ctx
->magic
, ctx
->link_type
);
838 panic("Error writing pcap header!\n");
840 if (__pcap_io
->prepare_access_pcap
) {
841 ret
= __pcap_io
->prepare_access_pcap(fd
, PCAP_MODE_WR
, true);
843 panic("Error prepare writing pcap!\n");
851 static void finish_single_pcap_file(struct ctx
*ctx
, int fd
)
853 __pcap_io
->fsync_pcap(fd
);
855 if (__pcap_io
->prepare_close_pcap
)
856 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_WR
);
858 if (strncmp("-", ctx
->device_out
, strlen("-")))
861 dup2(fd
, fileno(stdout
));
864 static int begin_single_pcap_file(struct ctx
*ctx
)
870 if (!strncmp("-", ctx
->device_out
, strlen("-"))) {
871 fd
= dup_or_die(fileno(stdout
));
872 close(fileno(stdout
));
873 if (ctx
->pcap
== PCAP_OPS_MM
)
874 ctx
->pcap
= PCAP_OPS_SG
;
876 fd
= open_or_die_m(ctx
->device_out
,
877 O_RDWR
| O_CREAT
| O_TRUNC
|
878 O_LARGEFILE
, DEFFILEMODE
);
881 ret
= __pcap_io
->push_fhdr_pcap(fd
, ctx
->magic
, ctx
->link_type
);
883 panic("Error writing pcap header!\n");
885 if (__pcap_io
->prepare_access_pcap
) {
886 ret
= __pcap_io
->prepare_access_pcap(fd
, PCAP_MODE_WR
, true);
888 panic("Error prepare writing pcap!\n");
894 static void update_pcap_next_dump(struct ctx
*ctx
, unsigned long snaplen
,
895 int *fd
, int sock
, bool is_v3
)
897 if (!dump_to_pcap(ctx
))
900 if (ctx
->dump_mode
== DUMP_INTERVAL_SIZE
) {
902 if (interval
> ctx
->dump_interval
) {
910 printf("SIGHUP received, prematurely rotating pcap\n");
917 *fd
= next_multi_pcap_file(ctx
, *fd
);
920 if (update_rx_stats(ctx
, sock
, is_v3
))
923 if (ctx
->verbose
&& ctx
->print_mode
== PRINT_NONE
)
924 printf(".(+%"PRIu64
"/-%"PRIu64
")",
925 ctx
->pkts_recvd_last
- ctx
->pkts_drops_last
,
926 ctx
->pkts_drops_last
);
931 static void walk_t3_block(struct block_desc
*pbd
, struct ctx
*ctx
,
934 int num_pkts
= pbd
->h1
.num_pkts
, i
;
935 struct tpacket3_hdr
*hdr
;
936 struct sockaddr_ll
*sll
;
938 hdr
= (void *) ((uint8_t *) pbd
+ pbd
->h1
.offset_to_first_pkt
);
939 sll
= (void *) ((uint8_t *) hdr
+ TPACKET_ALIGN(sizeof(*hdr
)));
941 for (i
= 0; i
< num_pkts
&& likely(sigint
== 0); ++i
) {
942 uint8_t *packet
= ((uint8_t *) hdr
+ hdr
->tp_mac
);
945 if (ctx
->packet_type
!= -1)
946 if (ctx
->packet_type
!= sll
->sll_pkttype
)
951 if (dump_to_pcap(ctx
)) {
954 tpacket3_hdr_to_pcap_pkthdr(hdr
, sll
, &phdr
, ctx
->magic
);
956 ret
= __pcap_io
->write_pcap(*fd
, &phdr
, ctx
->magic
, packet
,
957 pcap_get_length(&phdr
, ctx
->magic
));
958 if (unlikely(ret
!= (int) pcap_get_total_length(&phdr
, ctx
->magic
)))
959 panic("Write error to pcap!\n");
962 __show_frame_hdr(packet
, hdr
->tp_snaplen
, ctx
->link_type
, sll
,
963 hdr
, ctx
->print_mode
, true, ctx
->pkts_seen
);
965 dissector_entry_point(packet
, hdr
->tp_snaplen
, ctx
->link_type
,
966 ctx
->print_mode
, sll
);
968 hdr
= (void *) ((uint8_t *) hdr
+ hdr
->tp_next_offset
);
969 sll
= (void *) ((uint8_t *) hdr
+ TPACKET_ALIGN(sizeof(*hdr
)));
971 if (frame_count_max
!= 0) {
972 if (unlikely(ctx
->pkts_seen
>= frame_count_max
)) {
978 update_pcap_next_dump(ctx
, hdr
->tp_snaplen
, fd
, sock
, true);
981 #endif /* HAVE_TPACKET3 */
983 static void recv_only_or_dump(struct ctx
*ctx
)
986 int sock
, ifindex
, fd
= 0, ret
;
990 struct pollfd rx_poll
;
991 struct sock_fprog bpf_ops
;
992 struct timeval start
, end
, diff
;
993 bool is_v3
= is_defined(HAVE_TPACKET3
);
995 sock
= pf_socket_type(ctx
->link_type
);
997 ifindex
= device_ifindex(ctx
->device_in
);
998 size
= ring_size(ctx
->device_in
, ctx
->reserve_size
);
1000 enable_kernel_bpf_jit_compiler();
1002 bpf_parse_rules(ctx
->filter
, &bpf_ops
, ctx
->link_type
);
1004 bpf_dump_all(&bpf_ops
);
1005 bpf_attach_to_sock(sock
, &bpf_ops
);
1007 if (ctx
->hwtimestamp
) {
1008 ret
= set_sockopt_hwtimestamp(sock
, ctx
->device_in
);
1009 if (ret
== 0 && ctx
->verbose
)
1010 printf("HW timestamping enabled\n");
1013 ring_rx_setup(&rx_ring
, sock
, size
, ifindex
, &rx_poll
, is_v3
, true,
1014 ctx
->verbose
, ctx
->fanout_group
, ctx
->fanout_type
);
1016 dissector_init_all(ctx
->print_mode
);
1018 if (ctx
->cpu
>= 0 && ifindex
> 0) {
1019 int irq
= device_irq_number(ctx
->device_in
);
1020 device_set_irq_affinity(irq
, ctx
->cpu
);
1023 printf("IRQ: %s:%d > CPU%d\n",
1024 ctx
->device_in
, irq
, ctx
->cpu
);
1027 if (ctx
->promiscuous
)
1028 ifflags
= device_enter_promiscuous_mode(ctx
->device_in
);
1030 if (dump_to_pcap(ctx
) && __pcap_io
->init_once_pcap
)
1031 __pcap_io
->init_once_pcap(true);
1033 drop_privileges(ctx
->enforce
, ctx
->uid
, ctx
->gid
);
1035 if (dump_to_pcap(ctx
)) {
1038 ret
= stat(ctx
->device_out
, &stats
);
1042 ctx
->dump_dir
= S_ISDIR(stats
.st_mode
);
1045 fd
= begin_multi_pcap_file(ctx
);
1047 fd
= begin_single_pcap_file(ctx
);
1050 printf("Running! Hang up with ^C!\n\n");
1053 bug_on(gettimeofday(&start
, NULL
));
1055 while (likely(sigint
== 0)) {
1056 #ifdef HAVE_TPACKET3
1057 struct block_desc
*pbd
;
1059 while (user_may_pull_from_rx_block((pbd
= rx_ring
.frames
[it
].iov_base
))) {
1060 walk_t3_block(pbd
, ctx
, sock
, &fd
);
1062 kernel_may_pull_from_rx_block(pbd
);
1063 it
= (it
+ 1) % rx_ring
.layout3
.tp_block_nr
;
1065 if (unlikely(sigint
== 1))
1069 while (user_may_pull_from_rx(rx_ring
.frames
[it
].iov_base
)) {
1070 struct frame_map
*hdr
= rx_ring
.frames
[it
].iov_base
;
1071 uint8_t *packet
= ((uint8_t *) hdr
) + hdr
->tp_h
.tp_mac
;
1074 if (ctx
->packet_type
!= -1)
1075 if (ctx
->packet_type
!= hdr
->s_ll
.sll_pkttype
)
1080 if (unlikely(ring_frame_size(&rx_ring
) < hdr
->tp_h
.tp_snaplen
)) {
1081 /* XXX: silently ignore for now. We used to
1082 * report them with dump_rx_stats() */
1086 if (dump_to_pcap(ctx
)) {
1087 tpacket_hdr_to_pcap_pkthdr(&hdr
->tp_h
, &hdr
->s_ll
, &phdr
, ctx
->magic
);
1089 ret
= __pcap_io
->write_pcap(fd
, &phdr
, ctx
->magic
, packet
,
1090 pcap_get_length(&phdr
, ctx
->magic
));
1091 if (unlikely(ret
!= (int) pcap_get_total_length(&phdr
, ctx
->magic
)))
1092 panic("Write error to pcap!\n");
1095 show_frame_hdr(packet
, hdr
->tp_h
.tp_snaplen
,
1096 ctx
->link_type
, hdr
, ctx
->print_mode
,
1099 dissector_entry_point(packet
, hdr
->tp_h
.tp_snaplen
,
1100 ctx
->link_type
, ctx
->print_mode
,
1103 if (frame_count_max
!= 0) {
1104 if (unlikely(ctx
->pkts_seen
>= frame_count_max
)) {
1111 kernel_may_pull_from_rx(&hdr
->tp_h
);
1112 it
= (it
+ 1) % rx_ring
.layout
.tp_frame_nr
;
1114 if (unlikely(sigint
== 1))
1117 update_pcap_next_dump(ctx
, hdr
->tp_h
.tp_snaplen
, &fd
,
1120 #endif /* HAVE_TPACKET3 */
1122 ret
= poll(&rx_poll
, 1, -1);
1123 if (unlikely(ret
< 0)) {
1125 panic("Poll failed!\n");
1129 bug_on(gettimeofday(&end
, NULL
));
1130 timersub(&end
, &start
, &diff
);
1132 dump_rx_stats(ctx
, sock
, is_v3
);
1133 printf("\r%12lu sec, %lu usec in total\n",
1134 diff
.tv_sec
, diff
.tv_usec
);
1136 bpf_release(&bpf_ops
);
1137 dissector_cleanup_all();
1138 destroy_rx_ring(sock
, &rx_ring
);
1140 if (ctx
->promiscuous
)
1141 device_leave_promiscuous_mode(ctx
->device_in
, ifflags
);
1144 leave_rfmon_mac80211(ctx
->device_in
);
1146 if (dump_to_pcap(ctx
)) {
1148 finish_multi_pcap_file(ctx
, fd
);
1150 finish_single_pcap_file(ctx
, fd
);
1156 static void init_ctx(struct ctx
*ctx
)
1158 memset(ctx
, 0, sizeof(*ctx
));
1160 ctx
->uid
= getuid();
1161 ctx
->gid
= getgid();
1164 ctx
->packet_type
= -1;
1166 ctx
->fanout_type
= PACKET_FANOUT_ROLLOVER
;
1168 ctx
->magic
= ORIGINAL_TCPDUMP_MAGIC
;
1169 ctx
->print_mode
= PRINT_NORM
;
1170 ctx
->pcap
= PCAP_OPS_SG
;
1172 ctx
->dump_mode
= DUMP_INTERVAL_TIME
;
1173 ctx
->dump_interval
= 60;
1175 ctx
->promiscuous
= true;
1176 ctx
->randomize
= false;
1177 ctx
->hwtimestamp
= true;
1179 ctx
->pkts_recvd
= 0;
1181 ctx
->pkts_drops
= 0;
1182 ctx
->pkts_recvd_last
= 0;
1183 ctx
->pkts_drops_last
= 0;
1186 static void destroy_ctx(struct ctx
*ctx
)
1188 free(ctx
->device_in
);
1189 free(ctx
->device_out
);
1190 free(ctx
->device_trans
);
1195 static void __noreturn
help(void)
1197 printf("netsniff-ng %s, the packet sniffing beast\n", VERSION_STRING
);
1198 puts("http://www.netsniff-ng.org\n\n"
1199 "Usage: netsniff-ng [options] [filter-expression]\n"
1201 " -i|-d|--dev|--in <dev|pcap|-> Input source as netdev, pcap or pcap stdin\n"
1202 " -o|--out <dev|pcap|dir|cfg|-> Output sink as netdev, pcap, directory, trafgen, or stdout\n"
1203 " -C|--fanout-group <id> Join packet fanout group\n"
1204 " -K|--fanout-type <type> Apply fanout discipline: hash|lb|cpu|rnd|roll|qm\n"
1205 " -L|--fanout-opts <opts> Additional fanout options: defrag|roll\n"
1206 " -f|--filter <bpf-file|-|expr> Use BPF filter from bpfc file/stdin or tcpdump-like expression\n"
1207 " -t|--type <type> Filter for: host|broadcast|multicast|others|outgoing\n"
1208 " -F|--interval <size|time> Dump interval if -o is a dir: <num>KiB/MiB/GiB/s/sec/min/hrs\n"
1209 " -R|--rfraw Capture or inject raw 802.11 frames\n"
1210 " -n|--num <0|uint> Number of packets until exit (def: 0)\n"
1211 " -P|--prefix <name> Prefix for pcaps stored in directory\n"
1212 " -T|--magic <pcap-magic> Pcap magic number/pcap format to store, see -D\n"
1213 " -w|--cooked Use Linux \"cooked\" header instead of link header\n"
1214 " -D|--dump-pcap-types Dump pcap types and magic numbers and quit\n"
1215 " -B|--dump-bpf Dump generated BPF assembly\n"
1216 " -r|--rand Randomize packet forwarding order (dev->dev)\n"
1217 " -M|--no-promisc No promiscuous mode for netdev\n"
1218 " -A|--no-sock-mem Don't tune core socket memory\n"
1219 " -N|--no-hwtimestamp Disable hardware time stamping\n"
1220 " -m|--mmap Mmap(2) pcap file I/O, e.g. for replaying pcaps\n"
1221 " -G|--sg Scatter/gather pcap file I/O\n"
1222 " -c|--clrw Use slower read(2)/write(2) I/O\n"
1223 " -S|--ring-size <size> Specify ring size to: <num>KiB/MiB/GiB\n"
1224 " -k|--kernel-pull <uint> Kernel pull from user interval in us (def: 10us)\n"
1225 " -J|--jumbo-support Support replay/fwd 64KB Super Jumbo Frames (def: 2048B)\n"
1226 " -b|--bind-cpu <cpu> Bind to specific CPU\n"
1227 " -u|--user <userid> Drop privileges and change to userid\n"
1228 " -g|--group <groupid> Drop privileges and change to groupid\n"
1229 " -H|--prio-high Make this high priority process\n"
1230 " -Q|--notouch-irq Do not touch IRQ CPU affinity of NIC\n"
1231 " -s|--silent Do not print captured packets\n"
1232 " -q|--less Print less-verbose packet information\n"
1233 " -X|--hex Print packet data in hex format\n"
1234 " -l|--ascii Print human-readable packet data\n"
1235 " -U|--update Update GeoIP databases\n"
1236 " -V|--verbose Be more verbose\n"
1237 " -v|--version Show version and exit\n"
1238 " -h|--help Guess what?!\n\n"
1240 " netsniff-ng --in eth0 --out dump.pcap -s -T 0xa1b2c3d4 --b 0 tcp or udp\n"
1241 " netsniff-ng --in wlan0 --rfraw --out dump.pcap --silent --bind-cpu 0\n"
1242 " netsniff-ng --in dump.pcap --mmap --out eth0 -k1000 --silent --bind-cpu 0\n"
1243 " netsniff-ng --in dump.pcap --out dump.cfg --silent --bind-cpu 0\n"
1244 " netsniff-ng --in dump.pcap --out dump2.pcap --silent tcp\n"
1245 " netsniff-ng --in eth0 --out eth1 --silent --bind-cpu 0 -J --type host\n"
1246 " netsniff-ng --in eth1 --out /opt/probe/ -s -m --interval 100MiB -b 0\n"
1247 " netsniff-ng --in vlan0 --out dump.pcap -c -u `id -u bob` -g `id -g bob`\n"
1248 " netsniff-ng --in any --filter http.bpf --jumbo-support --ascii -V\n\n"
1250 " For introducing bit errors, delays with random variation and more\n"
1251 " while replaying pcaps, make use of tc(8) with its disciplines (e.g. netem).\n");
1256 static void __noreturn
version(void)
1258 printf("netsniff-ng %s, Git id: %s\n", VERSION_LONG
, GITVERSION
);
1259 puts("the packet sniffing beast\n"
1260 "http://www.netsniff-ng.org\n");
1265 int main(int argc
, char **argv
)
1268 int c
, i
, j
, cpu_tmp
, opt_index
, ops_touched
= 0, vals
[4] = {0};
1269 bool prio_high
= false, setsockmem
= true;
1270 void (*main_loop
)(struct ctx
*ctx
) = NULL
;
1274 start_time
= time(NULL
);
1277 while ((c
= getopt_long(argc
, argv
, short_options
, long_options
,
1278 &opt_index
)) != EOF
) {
1282 ctx
.device_in
= xstrdup(optarg
);
1285 ctx
.device_out
= xstrdup(optarg
);
1288 ctx
.prefix
= xstrdup(optarg
);
1294 ctx
.randomize
= true;
1300 ctx
.magic
= (uint32_t) strtoul(optarg
, NULL
, 0);
1301 pcap_check_magic(ctx
.magic
);
1304 ctx
.filter
= xstrdup(optarg
);
1307 ctx
.promiscuous
= false;
1310 ctx
.hwtimestamp
= false;
1316 ctx
.uid
= strtoul(optarg
, NULL
, 0);
1320 ctx
.gid
= strtoul(optarg
, NULL
, 0);
1324 ctx
.fanout_group
= strtoul(optarg
, NULL
, 0);
1325 if (ctx
.fanout_group
== 0)
1326 panic("Non-zero fanout group id required!\n");
1329 if (!strncmp(optarg
, "hash", strlen("hash")))
1330 ctx
.fanout_type
= PACKET_FANOUT_HASH
;
1331 else if (!strncmp(optarg
, "lb", strlen("lb")) ||
1332 !strncmp(optarg
, "rr", strlen("rr")))
1333 ctx
.fanout_type
= PACKET_FANOUT_LB
;
1334 else if (!strncmp(optarg
, "cpu", strlen("cpu")))
1335 ctx
.fanout_type
= PACKET_FANOUT_CPU
;
1336 else if (!strncmp(optarg
, "rnd", strlen("rnd")))
1337 ctx
.fanout_type
= PACKET_FANOUT_RND
;
1338 else if (!strncmp(optarg
, "roll", strlen("roll")))
1339 ctx
.fanout_type
= PACKET_FANOUT_ROLLOVER
;
1340 else if (!strncmp(optarg
, "qm", strlen("qm")))
1341 ctx
.fanout_type
= PACKET_FANOUT_QM
;
1343 panic("Unknown fanout type!\n");
1346 if (!strncmp(optarg
, "defrag", strlen("defrag")))
1347 ctx
.fanout_type
|= PACKET_FANOUT_FLAG_DEFRAG
;
1348 else if (!strncmp(optarg
, "roll", strlen("roll")))
1349 ctx
.fanout_type
|= PACKET_FANOUT_FLAG_ROLLOVER
;
1351 panic("Unknown fanout option!\n");
1354 if (!strncmp(optarg
, "host", strlen("host")))
1355 ctx
.packet_type
= PACKET_HOST
;
1356 else if (!strncmp(optarg
, "broadcast", strlen("broadcast")))
1357 ctx
.packet_type
= PACKET_BROADCAST
;
1358 else if (!strncmp(optarg
, "multicast", strlen("multicast")))
1359 ctx
.packet_type
= PACKET_MULTICAST
;
1360 else if (!strncmp(optarg
, "others", strlen("others")))
1361 ctx
.packet_type
= PACKET_OTHERHOST
;
1362 else if (!strncmp(optarg
, "outgoing", strlen("outgoing")))
1363 ctx
.packet_type
= PACKET_OUTGOING
;
1365 ctx
.packet_type
= -1;
1369 for (j
= i
= strlen(optarg
); i
> 0; --i
) {
1370 if (!isdigit(optarg
[j
- i
]))
1375 if (!strncmp(ptr
, "KiB", strlen("KiB")))
1376 ctx
.reserve_size
= 1 << 10;
1377 else if (!strncmp(ptr
, "MiB", strlen("MiB")))
1378 ctx
.reserve_size
= 1 << 20;
1379 else if (!strncmp(ptr
, "GiB", strlen("GiB")))
1380 ctx
.reserve_size
= 1 << 30;
1382 panic("Syntax error in ring size param!\n");
1384 ctx
.reserve_size
*= strtoul(optarg
, NULL
, 0);
1387 cpu_tmp
= strtol(optarg
, NULL
, 0);
1389 cpu_affinity(cpu_tmp
);
1397 ctx
.pcap
= PCAP_OPS_RW
;
1401 ctx
.pcap
= PCAP_OPS_MM
;
1405 ctx
.pcap
= PCAP_OPS_SG
;
1412 ctx
.print_mode
= PRINT_NONE
;
1415 ctx
.print_mode
= PRINT_LESS
;
1419 (ctx
.print_mode
== PRINT_ASCII
) ?
1420 PRINT_HEX_ASCII
: PRINT_HEX
;
1424 (ctx
.print_mode
== PRINT_HEX
) ?
1425 PRINT_HEX_ASCII
: PRINT_ASCII
;
1428 ctx
.kpull
= strtoul(optarg
, NULL
, 0);
1431 frame_count_max
= strtoul(optarg
, NULL
, 0);
1435 for (j
= i
= strlen(optarg
); i
> 0; --i
) {
1436 if (!isdigit(optarg
[j
- i
]))
1441 if (!strncmp(ptr
, "KiB", strlen("KiB"))) {
1442 ctx
.dump_interval
= 1 << 10;
1443 ctx
.dump_mode
= DUMP_INTERVAL_SIZE
;
1444 } else if (!strncmp(ptr
, "MiB", strlen("MiB"))) {
1445 ctx
.dump_interval
= 1 << 20;
1446 ctx
.dump_mode
= DUMP_INTERVAL_SIZE
;
1447 } else if (!strncmp(ptr
, "GiB", strlen("GiB"))) {
1448 ctx
.dump_interval
= 1 << 30;
1449 ctx
.dump_mode
= DUMP_INTERVAL_SIZE
;
1450 } else if (!strncmp(ptr
, "sec", strlen("sec"))) {
1451 ctx
.dump_interval
= 1;
1452 ctx
.dump_mode
= DUMP_INTERVAL_TIME
;
1453 } else if (!strncmp(ptr
, "min", strlen("min"))) {
1454 ctx
.dump_interval
= 60;
1455 ctx
.dump_mode
= DUMP_INTERVAL_TIME
;
1456 } else if (!strncmp(ptr
, "hrs", strlen("hrs"))) {
1457 ctx
.dump_interval
= 60 * 60;
1458 ctx
.dump_mode
= DUMP_INTERVAL_TIME
;
1459 } else if (!strncmp(ptr
, "s", strlen("s"))) {
1460 ctx
.dump_interval
= 1;
1461 ctx
.dump_mode
= DUMP_INTERVAL_TIME
;
1463 panic("Syntax error in time/size param!\n");
1466 ctx
.dump_interval
*= strtoul(optarg
, NULL
, 0);
1472 ctx
.dump_bpf
= true;
1475 pcap_dump_type_features();
1483 ctx
.link_type
= LINKTYPE_LINUX_SLL
;
1508 panic("Option -%c requires an argument!\n",
1511 if (isprint(optopt
))
1512 printf("Unknown option character `0x%X\'!\n", optopt
);
1520 if (!ctx
.filter
&& optind
!= argc
)
1521 ctx
.filter
= argv2str(optind
, argc
, argv
);
1524 ctx
.device_in
= xstrdup("any");
1526 register_signal(SIGINT
, signal_handler
);
1527 register_signal(SIGQUIT
, signal_handler
);
1528 register_signal(SIGTERM
, signal_handler
);
1529 register_signal(SIGHUP
, signal_handler
);
1535 set_sched_status(SCHED_FIFO
, sched_get_priority_max(SCHED_FIFO
));
1538 if (device_mtu(ctx
.device_in
) || !strncmp("any", ctx
.device_in
, strlen(ctx
.device_in
))) {
1540 setup_rfmon_mac80211_dev(&ctx
, &ctx
.device_in
);
1543 ctx
.link_type
= pcap_dev_to_linktype(ctx
.device_in
);
1544 if (link_has_sll_hdr(ctx
.link_type
)) {
1545 switch (ctx
.magic
) {
1546 case ORIGINAL_TCPDUMP_MAGIC
:
1547 ctx
.magic
= ORIGINAL_TCPDUMP_MAGIC_LL
;
1549 case NSEC_TCPDUMP_MAGIC
:
1550 ctx
.magic
= NSEC_TCPDUMP_MAGIC_LL
;
1552 case ___constant_swab32(ORIGINAL_TCPDUMP_MAGIC
):
1553 ctx
.magic
= ___constant_swab32(ORIGINAL_TCPDUMP_MAGIC_LL
);
1555 case ___constant_swab32(NSEC_TCPDUMP_MAGIC
):
1556 ctx
.magic
= ___constant_swab32(NSEC_TCPDUMP_MAGIC_LL
);
1562 if (!ctx
.device_out
) {
1564 main_loop
= recv_only_or_dump
;
1565 } else if (device_mtu(ctx
.device_out
)) {
1566 register_signal_f(SIGALRM
, timer_elapsed
, SA_SIGINFO
);
1567 main_loop
= receive_to_xmit
;
1570 register_signal_f(SIGALRM
, timer_next_dump
, SA_SIGINFO
);
1571 main_loop
= recv_only_or_dump
;
1573 ctx
.pcap
= PCAP_OPS_SG
;
1576 if (ctx
.device_out
&& device_mtu(ctx
.device_out
)) {
1577 register_signal_f(SIGALRM
, timer_elapsed
, SA_SIGINFO
);
1578 main_loop
= pcap_to_xmit
;
1580 ctx
.pcap
= PCAP_OPS_MM
;
1583 main_loop
= read_pcap
;
1585 ctx
.pcap
= PCAP_OPS_SG
;
1593 set_system_socket_memory(vals
, array_size(vals
));
1598 printf("pcap file I/O method: %s\n", pcap_ops_group_to_str
[ctx
.pcap
]);
1605 reset_system_socket_memory(vals
, array_size(vals
));
1608 device_restore_irq_affinity_list();