2 * netsniff-ng - the packet sniffing beast
3 * By Daniel Borkmann <daniel@netsniff-ng.org>
4 * Copyright 2009-2013 Daniel Borkmann.
5 * Copyright 2010 Emmanuel Roullit.
6 * Subject to the GPL, version 2.
8 * The first sniffer that invoked both, the zero-copy RX_RING as well as
9 * the zero-copy TX_RING for high-performance network I/O and scatter/gather
12 * "I knew that danger lay ahead, of course; but I did not expect to
13 * meet it in our own Shire. Can't a hobbit walk from the Water to the
14 * River in peace?" "But it is not your own Shire," said Gildor. "Others
15 * dwelt here before hobbits were; and others will dwell here again when
16 * hobbits are no more. The wide world is all about you: you can fence
17 * yourselves in, but you cannot for ever fence it out."
19 * -- The Lord of the Rings, Gildor to Frodo,
20 * Chapter 'Three is Company'.
31 #include <sys/socket.h>
32 #include <sys/types.h>
35 #include <sys/fsuid.h>
51 #include "dissector.h"
60 char *device_in
, *device_out
, *device_trans
, *filter
, *prefix
;
61 int cpu
, rfraw
, dump
, print_mode
, dump_dir
, jumbo_support
, packet_type
, verbose
;
62 unsigned long kpull
, dump_interval
, reserve_size
, tx_bytes
, tx_packets
;
63 bool randomize
, promiscuous
, enforce
;
64 enum pcap_ops_groups pcap
;
65 enum dump_mode dump_mode
;
71 volatile sig_atomic_t sigint
= 0;
73 static volatile bool next_dump
= false;
75 static const char *short_options
= "d:i:o:rf:MJt:S:k:n:b:B:HQmcsqXlvhF:RGAP:Vu:g:";
76 static const struct option long_options
[] = {
77 {"dev", required_argument
, NULL
, 'd'},
78 {"in", required_argument
, NULL
, 'i'},
79 {"out", required_argument
, NULL
, 'o'},
80 {"filter", required_argument
, NULL
, 'f'},
81 {"num", required_argument
, NULL
, 'n'},
82 {"type", required_argument
, NULL
, 't'},
83 {"interval", required_argument
, NULL
, 'F'},
84 {"ring-size", required_argument
, NULL
, 'S'},
85 {"kernel-pull", required_argument
, NULL
, 'k'},
86 {"bind-cpu", required_argument
, NULL
, 'b'},
87 {"unbind-cpu", required_argument
, NULL
, 'B'},
88 {"prefix", required_argument
, NULL
, 'P'},
89 {"user", required_argument
, NULL
, 'u'},
90 {"group", required_argument
, NULL
, 'g'},
91 {"rand", no_argument
, NULL
, 'r'},
92 {"rfraw", no_argument
, NULL
, 'R'},
93 {"mmap", no_argument
, NULL
, 'm'},
94 {"sg", no_argument
, NULL
, 'G'},
95 {"clrw", no_argument
, NULL
, 'c'},
96 {"jumbo-support", no_argument
, NULL
, 'J'},
97 {"no-promisc", no_argument
, NULL
, 'M'},
98 {"prio-high", no_argument
, NULL
, 'H'},
99 {"notouch-irq", no_argument
, NULL
, 'Q'},
100 {"silent", no_argument
, NULL
, 's'},
101 {"less", no_argument
, NULL
, 'q'},
102 {"hex", no_argument
, NULL
, 'X'},
103 {"ascii", no_argument
, NULL
, 'l'},
104 {"no-sock-mem", no_argument
, NULL
, 'A'},
105 {"verbose", no_argument
, NULL
, 'V'},
106 {"version", no_argument
, NULL
, 'v'},
107 {"help", no_argument
, NULL
, 'h'},
113 static struct itimerval itimer
;
115 static unsigned long frame_count_max
= 0, interval
= TX_KERNEL_PULL_INT
;
117 #define set_system_socket_memory(vals) \
119 if ((vals[0] = get_system_socket_mem(sock_rmem_max)) < SMEM_SUG_MAX) \
120 set_system_socket_mem(sock_rmem_max, SMEM_SUG_MAX); \
121 if ((vals[1] = get_system_socket_mem(sock_rmem_def)) < SMEM_SUG_DEF) \
122 set_system_socket_mem(sock_rmem_def, SMEM_SUG_DEF); \
123 if ((vals[2] = get_system_socket_mem(sock_wmem_max)) < SMEM_SUG_MAX) \
124 set_system_socket_mem(sock_wmem_max, SMEM_SUG_MAX); \
125 if ((vals[3] = get_system_socket_mem(sock_wmem_def)) < SMEM_SUG_DEF) \
126 set_system_socket_mem(sock_wmem_def, SMEM_SUG_DEF); \
129 #define reset_system_socket_memory(vals) \
131 set_system_socket_mem(sock_rmem_max, vals[0]); \
132 set_system_socket_mem(sock_rmem_def, vals[1]); \
133 set_system_socket_mem(sock_wmem_max, vals[2]); \
134 set_system_socket_mem(sock_wmem_def, vals[3]); \
137 #define __pcap_io pcap_ops[ctx->pcap]
139 static void signal_handler(int number
)
150 static void timer_elapsed(int unused
)
152 itimer
.it_interval
.tv_sec
= 0;
153 itimer
.it_interval
.tv_usec
= interval
;
155 itimer
.it_value
.tv_sec
= 0;
156 itimer
.it_value
.tv_usec
= interval
;
158 pull_and_flush_tx_ring(tx_sock
);
159 setitimer(ITIMER_REAL
, &itimer
, NULL
);
162 static void timer_next_dump(int unused
)
164 itimer
.it_interval
.tv_sec
= interval
;
165 itimer
.it_interval
.tv_usec
= 0;
167 itimer
.it_value
.tv_sec
= interval
;
168 itimer
.it_value
.tv_usec
= 0;
171 setitimer(ITIMER_REAL
, &itimer
, NULL
);
174 static inline bool dump_to_pcap(struct ctx
*ctx
)
179 static void pcap_to_xmit(struct ctx
*ctx
)
183 int irq
, ifindex
, fd
= 0, ret
;
184 unsigned int size
, it
= 0;
185 unsigned long trunced
= 0;
187 struct frame_map
*hdr
;
188 struct sock_fprog bpf_ops
;
189 struct timeval start
, end
, diff
;
190 struct pcap_pkthdr phdr
;
192 if (!device_up_and_running(ctx
->device_out
) && !ctx
->rfraw
)
193 panic("Device not up and running!\n");
197 tx_sock
= pf_socket();
199 fd
= open_or_die(ctx
->device_in
, O_RDONLY
| O_LARGEFILE
| O_NOATIME
);
201 ret
= __pcap_io
->pull_file_header(fd
, &ctx
->link_type
);
203 panic("Error reading pcap header!\n");
205 if (__pcap_io
->prepare_reading_pcap
) {
206 ret
= __pcap_io
->prepare_reading_pcap(fd
);
208 panic("Error prepare reading pcap!\n");
211 fmemset(&tx_ring
, 0, sizeof(tx_ring
));
212 fmemset(&bpf_ops
, 0, sizeof(bpf_ops
));
215 ctx
->device_trans
= xstrdup(ctx
->device_out
);
216 xfree(ctx
->device_out
);
218 enter_rfmon_mac80211(ctx
->device_trans
, &ctx
->device_out
);
219 if (ctx
->link_type
!= LINKTYPE_IEEE802_11
)
220 panic("Wrong linktype of pcap!\n");
223 ifindex
= device_ifindex(ctx
->device_out
);
225 size
= ring_size(ctx
->device_out
, ctx
->reserve_size
);
227 bpf_parse_rules(ctx
->filter
, &bpf_ops
);
229 set_packet_loss_discard(tx_sock
);
230 set_sockopt_hwtimestamp(tx_sock
, ctx
->device_out
);
232 setup_tx_ring_layout(tx_sock
, &tx_ring
, size
, ctx
->jumbo_support
);
233 create_tx_ring(tx_sock
, &tx_ring
, ctx
->verbose
);
234 mmap_tx_ring(tx_sock
, &tx_ring
);
235 alloc_tx_ring_frames(&tx_ring
);
236 bind_tx_ring(tx_sock
, &tx_ring
, ifindex
);
238 dissector_init_all(ctx
->print_mode
);
240 if (ctx
->cpu
>= 0 && ifindex
> 0) {
241 irq
= device_irq_number(ctx
->device_out
);
242 device_bind_irq_to_cpu(irq
, ctx
->cpu
);
245 printf("IRQ: %s:%d > CPU%d\n",
246 ctx
->device_out
, irq
, ctx
->cpu
);
250 interval
= ctx
->kpull
;
254 bpf_dump_all(&bpf_ops
);
256 printf("MD: TX %luus %s ", interval
, pcap_ops
[ctx
->pcap
]->name
);
258 printf("802.11 raw via %s ", ctx
->device_out
);
259 #ifdef _LARGEFILE64_SOURCE
266 itimer
.it_interval
.tv_sec
= 0;
267 itimer
.it_interval
.tv_usec
= interval
;
269 itimer
.it_value
.tv_sec
= 0;
270 itimer
.it_value
.tv_usec
= interval
;
272 setitimer(ITIMER_REAL
, &itimer
, NULL
);
274 drop_privileges(ctx
->enforce
, ctx
->uid
, ctx
->gid
);
276 printf("Running! Hang up with ^C!\n\n");
279 bug_on(gettimeofday(&start
, NULL
));
281 while (likely(sigint
== 0)) {
282 while (user_may_pull_from_tx(tx_ring
.frames
[it
].iov_base
)) {
283 hdr
= tx_ring
.frames
[it
].iov_base
;
285 /* Kernel assumes: data = ph.raw + po->tp_hdrlen -
286 * sizeof(struct sockaddr_ll); */
287 out
= ((uint8_t *) hdr
) + TPACKET2_HDRLEN
- sizeof(struct sockaddr_ll
);
290 ret
= __pcap_io
->read_pcap_pkt(fd
, &phdr
, out
,
291 ring_frame_size(&tx_ring
));
292 if (unlikely(ret
<= 0))
295 if (ring_frame_size(&tx_ring
) < phdr
.len
) {
296 phdr
.len
= ring_frame_size(&tx_ring
);
299 } while (ctx
->filter
&& !bpf_run_filter(&bpf_ops
, out
, phdr
.len
));
301 pcap_pkthdr_to_tpacket_hdr(&phdr
, &hdr
->tp_h
);
303 ctx
->tx_bytes
+= hdr
->tp_h
.tp_len
;;
306 show_frame_hdr(hdr
, ctx
->print_mode
, RING_MODE_EGRESS
);
308 dissector_entry_point(out
, hdr
->tp_h
.tp_snaplen
,
309 ctx
->link_type
, ctx
->print_mode
);
311 kernel_may_pull_from_tx(&hdr
->tp_h
);
314 if (it
>= tx_ring
.layout
.tp_frame_nr
)
317 if (unlikely(sigint
== 1))
320 if (frame_count_max
!= 0) {
321 if (ctx
->tx_packets
>= frame_count_max
) {
331 bug_on(gettimeofday(&end
, NULL
));
332 diff
= tv_subtract(end
, start
);
334 bpf_release(&bpf_ops
);
336 dissector_cleanup_all();
337 destroy_tx_ring(tx_sock
, &tx_ring
);
340 leave_rfmon_mac80211(ctx
->device_trans
, ctx
->device_out
);
342 if (__pcap_io
->prepare_close_pcap
)
343 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_READ
);
350 printf("\r%12lu packets outgoing\n", ctx
->tx_packets
);
351 printf("\r%12lu packets truncated in file\n", trunced
);
352 printf("\r%12lu bytes outgoing\n", ctx
->tx_bytes
);
353 printf("\r%12lu sec, %lu usec in total\n", diff
.tv_sec
, diff
.tv_usec
);
356 static void receive_to_xmit(struct ctx
*ctx
)
360 int rx_sock
, ifindex_in
, ifindex_out
;
361 unsigned int size_in
, size_out
, it_in
= 0, it_out
= 0;
362 unsigned long frame_count
= 0;
363 struct frame_map
*hdr_in
, *hdr_out
;
364 struct ring tx_ring
, rx_ring
;
365 struct pollfd rx_poll
;
366 struct sock_fprog bpf_ops
;
368 if (!strncmp(ctx
->device_in
, ctx
->device_out
, IFNAMSIZ
))
369 panic("Ingress/egress devices must be different!\n");
370 if (!device_up_and_running(ctx
->device_out
))
371 panic("Egress device not up and running!\n");
372 if (!device_up_and_running(ctx
->device_in
))
373 panic("Ingress device not up and running!\n");
375 rx_sock
= pf_socket();
376 tx_sock
= pf_socket();
378 fmemset(&tx_ring
, 0, sizeof(tx_ring
));
379 fmemset(&rx_ring
, 0, sizeof(rx_ring
));
380 fmemset(&rx_poll
, 0, sizeof(rx_poll
));
381 fmemset(&bpf_ops
, 0, sizeof(bpf_ops
));
383 ifindex_in
= device_ifindex(ctx
->device_in
);
384 ifindex_out
= device_ifindex(ctx
->device_out
);
386 size_in
= ring_size(ctx
->device_in
, ctx
->reserve_size
);
387 size_out
= ring_size(ctx
->device_out
, ctx
->reserve_size
);
389 enable_kernel_bpf_jit_compiler();
391 bpf_parse_rules(ctx
->filter
, &bpf_ops
);
392 bpf_attach_to_sock(rx_sock
, &bpf_ops
);
394 setup_rx_ring_layout(rx_sock
, &rx_ring
, size_in
, ctx
->jumbo_support
);
395 create_rx_ring(rx_sock
, &rx_ring
, ctx
->verbose
);
396 mmap_rx_ring(rx_sock
, &rx_ring
);
397 alloc_rx_ring_frames(&rx_ring
);
398 bind_rx_ring(rx_sock
, &rx_ring
, ifindex_in
);
399 prepare_polling(rx_sock
, &rx_poll
);
401 set_packet_loss_discard(tx_sock
);
402 setup_tx_ring_layout(tx_sock
, &tx_ring
, size_out
, ctx
->jumbo_support
);
403 create_tx_ring(tx_sock
, &tx_ring
, ctx
->verbose
);
404 mmap_tx_ring(tx_sock
, &tx_ring
);
405 alloc_tx_ring_frames(&tx_ring
);
406 bind_tx_ring(tx_sock
, &tx_ring
, ifindex_out
);
408 dissector_init_all(ctx
->print_mode
);
410 if (ctx
->promiscuous
)
411 ifflags
= enter_promiscuous_mode(ctx
->device_in
);
414 interval
= ctx
->kpull
;
416 itimer
.it_interval
.tv_sec
= 0;
417 itimer
.it_interval
.tv_usec
= interval
;
419 itimer
.it_value
.tv_sec
= 0;
420 itimer
.it_value
.tv_usec
= interval
;
422 setitimer(ITIMER_REAL
, &itimer
, NULL
);
426 bpf_dump_all(&bpf_ops
);
428 printf("MD: RXTX %luus\n\n", interval
);
431 drop_privileges(ctx
->enforce
, ctx
->uid
, ctx
->gid
);
433 printf("Running! Hang up with ^C!\n\n");
436 while (likely(sigint
== 0)) {
437 while (user_may_pull_from_rx(rx_ring
.frames
[it_in
].iov_base
)) {
440 hdr_in
= rx_ring
.frames
[it_in
].iov_base
;
441 in
= ((uint8_t *) hdr_in
) + hdr_in
->tp_h
.tp_mac
;
445 if (ctx
->packet_type
!= -1)
446 if (ctx
->packet_type
!= hdr_in
->s_ll
.sll_pkttype
)
449 hdr_out
= tx_ring
.frames
[it_out
].iov_base
;
450 out
= ((uint8_t *) hdr_out
) + TPACKET2_HDRLEN
- sizeof(struct sockaddr_ll
);
452 for (; !user_may_pull_from_tx(tx_ring
.frames
[it_out
].iov_base
) &&
455 next_rnd_slot(&it_out
, &tx_ring
);
458 if (it_out
>= tx_ring
.layout
.tp_frame_nr
)
462 hdr_out
= tx_ring
.frames
[it_out
].iov_base
;
463 out
= ((uint8_t *) hdr_out
) + TPACKET2_HDRLEN
- sizeof(struct sockaddr_ll
);
466 tpacket_hdr_clone(&hdr_out
->tp_h
, &hdr_in
->tp_h
);
467 fmemcpy(out
, in
, hdr_in
->tp_h
.tp_len
);
469 kernel_may_pull_from_tx(&hdr_out
->tp_h
);
471 next_rnd_slot(&it_out
, &tx_ring
);
474 if (it_out
>= tx_ring
.layout
.tp_frame_nr
)
478 show_frame_hdr(hdr_in
, ctx
->print_mode
, RING_MODE_INGRESS
);
480 dissector_entry_point(in
, hdr_in
->tp_h
.tp_snaplen
,
481 ctx
->link_type
, ctx
->print_mode
);
483 if (frame_count_max
!= 0) {
484 if (frame_count
>= frame_count_max
) {
492 kernel_may_pull_from_rx(&hdr_in
->tp_h
);
495 if (it_in
>= rx_ring
.layout
.tp_frame_nr
)
498 if (unlikely(sigint
== 1))
502 poll(&rx_poll
, 1, -1);
503 poll_error_maybe_die(rx_sock
, &rx_poll
);
508 sock_print_net_stats(rx_sock
, 0);
510 bpf_release(&bpf_ops
);
512 dissector_cleanup_all();
514 destroy_tx_ring(tx_sock
, &tx_ring
);
515 destroy_rx_ring(rx_sock
, &rx_ring
);
517 if (ctx
->promiscuous
)
518 leave_promiscuous_mode(ctx
->device_in
, ifflags
);
524 static void translate_pcap_to_txf(int fdo
, uint8_t *out
, size_t len
)
526 size_t bytes_done
= 0;
529 slprintf(bout
, sizeof(bout
), "{\n ");
530 write_or_die(fdo
, bout
, strlen(bout
));
532 while (bytes_done
< len
) {
533 slprintf(bout
, sizeof(bout
), "0x%02x, ", out
[bytes_done
]);
534 write_or_die(fdo
, bout
, strlen(bout
));
538 if (bytes_done
% 10 == 0) {
539 slprintf(bout
, sizeof(bout
), "\n");
540 write_or_die(fdo
, bout
, strlen(bout
));
542 if (bytes_done
< len
) {
543 slprintf(bout
, sizeof(bout
), " ");
544 write_or_die(fdo
, bout
, strlen(bout
));
548 if (bytes_done
% 10 != 0) {
549 slprintf(bout
, sizeof(bout
), "\n");
550 write_or_die(fdo
, bout
, strlen(bout
));
553 slprintf(bout
, sizeof(bout
), "}\n\n");
554 write_or_die(fdo
, bout
, strlen(bout
));
557 static void read_pcap(struct ctx
*ctx
)
561 int ret
, fd
, fdo
= 0;
562 unsigned long trunced
= 0;
564 struct pcap_pkthdr phdr
;
565 struct sock_fprog bpf_ops
;
567 struct timeval start
, end
, diff
;
571 fd
= open_or_die(ctx
->device_in
, O_RDONLY
| O_LARGEFILE
| O_NOATIME
);
573 ret
= __pcap_io
->pull_file_header(fd
, &ctx
->link_type
);
575 panic("Error reading pcap header!\n");
577 if (__pcap_io
->prepare_reading_pcap
) {
578 ret
= __pcap_io
->prepare_reading_pcap(fd
);
580 panic("Error prepare reading pcap!\n");
583 fmemset(&fm
, 0, sizeof(fm
));
584 fmemset(&bpf_ops
, 0, sizeof(bpf_ops
));
586 bpf_parse_rules(ctx
->filter
, &bpf_ops
);
588 dissector_init_all(ctx
->print_mode
);
590 out_len
= round_up(1024 * 1024, PAGE_SIZE
);
591 out
= xmalloc_aligned(out_len
, CO_CACHE_LINE_SIZE
);
595 bpf_dump_all(&bpf_ops
);
597 printf("MD: RD %s ", __pcap_io
->name
);
598 #ifdef _LARGEFILE64_SOURCE
606 fdo
= open_or_die_m(ctx
->device_out
, O_RDWR
| O_CREAT
|
607 O_TRUNC
| O_LARGEFILE
, DEFFILEMODE
);
609 drop_privileges(ctx
->enforce
, ctx
->uid
, ctx
->gid
);
611 printf("Running! Hang up with ^C!\n\n");
614 bug_on(gettimeofday(&start
, NULL
));
616 while (likely(sigint
== 0)) {
618 ret
= __pcap_io
->read_pcap_pkt(fd
, &phdr
, out
, out_len
);
619 if (unlikely(ret
< 0))
622 if (unlikely(phdr
.len
== 0)) {
627 if (unlikely(phdr
.len
> out_len
)) {
631 } while (ctx
->filter
&& !bpf_run_filter(&bpf_ops
, out
, phdr
.len
));
633 pcap_pkthdr_to_tpacket_hdr(&phdr
, &fm
.tp_h
);
635 ctx
->tx_bytes
+= fm
.tp_h
.tp_len
;
638 show_frame_hdr(&fm
, ctx
->print_mode
, RING_MODE_EGRESS
);
640 dissector_entry_point(out
, fm
.tp_h
.tp_snaplen
,
641 ctx
->link_type
, ctx
->print_mode
);
644 translate_pcap_to_txf(fdo
, out
, fm
.tp_h
.tp_snaplen
);
646 if (frame_count_max
!= 0) {
647 if (ctx
->tx_packets
>= frame_count_max
) {
656 bug_on(gettimeofday(&end
, NULL
));
657 diff
= tv_subtract(end
, start
);
659 bpf_release(&bpf_ops
);
661 dissector_cleanup_all();
663 if (__pcap_io
->prepare_close_pcap
)
664 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_READ
);
674 printf("\r%12lu packets outgoing\n", ctx
->tx_packets
);
675 printf("\r%12lu packets truncated in file\n", trunced
);
676 printf("\r%12lu bytes outgoing\n", ctx
->tx_bytes
);
677 printf("\r%12lu sec, %lu usec in total\n", diff
.tv_sec
, diff
.tv_usec
);
680 static void finish_multi_pcap_file(struct ctx
*ctx
, int fd
)
682 __pcap_io
->fsync_pcap(fd
);
684 if (__pcap_io
->prepare_close_pcap
)
685 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_WRITE
);
689 fmemset(&itimer
, 0, sizeof(itimer
));
690 setitimer(ITIMER_REAL
, &itimer
, NULL
);
693 static int next_multi_pcap_file(struct ctx
*ctx
, int fd
)
698 __pcap_io
->fsync_pcap(fd
);
700 if (__pcap_io
->prepare_close_pcap
)
701 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_WRITE
);
705 slprintf(fname
, sizeof(fname
), "%s/%s%lu.pcap", ctx
->device_out
,
706 ctx
->prefix
? : "dump-", time(0));
708 fd
= open_or_die_m(fname
, O_RDWR
| O_CREAT
| O_TRUNC
|
709 O_LARGEFILE
, DEFFILEMODE
);
711 ret
= __pcap_io
->push_file_header(fd
, ctx
->link_type
);
713 panic("Error writing pcap header!\n");
715 if (__pcap_io
->prepare_writing_pcap
) {
716 ret
= __pcap_io
->prepare_writing_pcap(fd
);
718 panic("Error prepare writing pcap!\n");
724 static int begin_multi_pcap_file(struct ctx
*ctx
)
731 if (ctx
->device_out
[strlen(ctx
->device_out
) - 1] == '/')
732 ctx
->device_out
[strlen(ctx
->device_out
) - 1] = 0;
734 slprintf(fname
, sizeof(fname
), "%s/%s%lu.pcap", ctx
->device_out
,
735 ctx
->prefix
? : "dump-", time(0));
737 fd
= open_or_die_m(fname
, O_RDWR
| O_CREAT
| O_TRUNC
|
738 O_LARGEFILE
, DEFFILEMODE
);
740 ret
= __pcap_io
->push_file_header(fd
, ctx
->link_type
);
742 panic("Error writing pcap header!\n");
744 if (__pcap_io
->prepare_writing_pcap
) {
745 ret
= __pcap_io
->prepare_writing_pcap(fd
);
747 panic("Error prepare writing pcap!\n");
750 if (ctx
->dump_mode
== DUMP_INTERVAL_TIME
) {
751 interval
= ctx
->dump_interval
;
753 itimer
.it_interval
.tv_sec
= interval
;
754 itimer
.it_interval
.tv_usec
= 0;
756 itimer
.it_value
.tv_sec
= interval
;
757 itimer
.it_value
.tv_usec
= 0;
759 setitimer(ITIMER_REAL
, &itimer
, NULL
);
767 static void finish_single_pcap_file(struct ctx
*ctx
, int fd
)
769 __pcap_io
->fsync_pcap(fd
);
771 if (__pcap_io
->prepare_close_pcap
)
772 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_WRITE
);
777 static int begin_single_pcap_file(struct ctx
*ctx
)
783 fd
= open_or_die_m(ctx
->device_out
, O_RDWR
| O_CREAT
| O_TRUNC
|
784 O_LARGEFILE
, DEFFILEMODE
);
786 ret
= __pcap_io
->push_file_header(fd
, ctx
->link_type
);
788 panic("Error writing pcap header!\n");
790 if (__pcap_io
->prepare_writing_pcap
) {
791 ret
= __pcap_io
->prepare_writing_pcap(fd
);
793 panic("Error prepare writing pcap!\n");
799 static void print_pcap_file_stats(int sock
, struct ctx
*ctx
, unsigned long skipped
)
801 unsigned long good
, bad
;
802 struct tpacket_stats kstats
;
803 socklen_t slen
= sizeof(kstats
);
805 fmemset(&kstats
, 0, sizeof(kstats
));
806 getsockopt(sock
, SOL_PACKET
, PACKET_STATISTICS
, &kstats
, &slen
);
808 if (ctx
->print_mode
== PRINT_NONE
) {
809 good
= kstats
.tp_packets
- kstats
.tp_drops
- skipped
;
810 bad
= kstats
.tp_drops
+ skipped
;
812 printf(".(+%lu/-%lu)", good
, bad
);
817 static void recv_only_or_dump(struct ctx
*ctx
)
821 int sock
, irq
, ifindex
, fd
= 0, ret
;
822 unsigned int size
, it
= 0;
823 unsigned long frame_count
= 0, skipped
= 0;
825 struct pollfd rx_poll
;
826 struct frame_map
*hdr
;
827 struct sock_fprog bpf_ops
;
828 struct timeval start
, end
, diff
;
829 struct pcap_pkthdr phdr
;
831 if (!device_up_and_running(ctx
->device_in
) && !ctx
->rfraw
)
832 panic("Device not up and running!\n");
837 ctx
->device_trans
= xstrdup(ctx
->device_in
);
838 xfree(ctx
->device_in
);
840 enter_rfmon_mac80211(ctx
->device_trans
, &ctx
->device_in
);
841 ctx
->link_type
= LINKTYPE_IEEE802_11
;
844 fmemset(&rx_ring
, 0, sizeof(rx_ring
));
845 fmemset(&rx_poll
, 0, sizeof(rx_poll
));
846 fmemset(&bpf_ops
, 0, sizeof(bpf_ops
));
848 ifindex
= device_ifindex(ctx
->device_in
);
850 size
= ring_size(ctx
->device_in
, ctx
->reserve_size
);
852 enable_kernel_bpf_jit_compiler();
854 bpf_parse_rules(ctx
->filter
, &bpf_ops
);
855 bpf_attach_to_sock(sock
, &bpf_ops
);
857 set_sockopt_hwtimestamp(sock
, ctx
->device_in
);
859 setup_rx_ring_layout(sock
, &rx_ring
, size
, ctx
->jumbo_support
);
860 create_rx_ring(sock
, &rx_ring
, ctx
->verbose
);
861 mmap_rx_ring(sock
, &rx_ring
);
862 alloc_rx_ring_frames(&rx_ring
);
863 bind_rx_ring(sock
, &rx_ring
, ifindex
);
865 prepare_polling(sock
, &rx_poll
);
866 dissector_init_all(ctx
->print_mode
);
868 if (ctx
->cpu
>= 0 && ifindex
> 0) {
869 irq
= device_irq_number(ctx
->device_in
);
870 device_bind_irq_to_cpu(irq
, ctx
->cpu
);
873 printf("IRQ: %s:%d > CPU%d\n",
874 ctx
->device_in
, irq
, ctx
->cpu
);
877 if (ctx
->promiscuous
)
878 ifflags
= enter_promiscuous_mode(ctx
->device_in
);
882 bpf_dump_all(&bpf_ops
);
884 printf("MD: RX %s ", ctx
->dump
? pcap_ops
[ctx
->pcap
]->name
: "");
886 printf("802.11 raw via %s ", ctx
->device_in
);
887 #ifdef _LARGEFILE64_SOURCE
894 drop_privileges(ctx
->enforce
, ctx
->uid
, ctx
->gid
);
896 if (dump_to_pcap(ctx
)) {
900 fmemset(&stats
, 0, sizeof(stats
));
901 ret
= stat(ctx
->device_out
, &stats
);
907 ctx
->dump_dir
= S_ISDIR(stats
.st_mode
);
909 fd
= begin_multi_pcap_file(ctx
);
912 fd
= begin_single_pcap_file(ctx
);
916 printf("Running! Hang up with ^C!\n\n");
919 bug_on(gettimeofday(&start
, NULL
));
921 while (likely(sigint
== 0)) {
922 while (user_may_pull_from_rx(rx_ring
.frames
[it
].iov_base
)) {
925 hdr
= rx_ring
.frames
[it
].iov_base
;
926 packet
= ((uint8_t *) hdr
) + hdr
->tp_h
.tp_mac
;
929 if (ctx
->packet_type
!= -1)
930 if (ctx
->packet_type
!= hdr
->s_ll
.sll_pkttype
)
933 if (unlikely(ring_frame_size(&rx_ring
) < hdr
->tp_h
.tp_snaplen
)) {
938 if (dump_to_pcap(ctx
)) {
939 tpacket_hdr_to_pcap_pkthdr(&hdr
->tp_h
, &phdr
);
941 ret
= __pcap_io
->write_pcap_pkt(fd
, &phdr
, packet
, phdr
.len
);
942 if (unlikely(ret
!= sizeof(phdr
) + phdr
.len
))
943 panic("Write error to pcap!\n");
946 show_frame_hdr(hdr
, ctx
->print_mode
, RING_MODE_INGRESS
);
948 dissector_entry_point(packet
, hdr
->tp_h
.tp_snaplen
,
949 ctx
->link_type
, ctx
->print_mode
);
951 if (frame_count_max
!= 0) {
952 if (frame_count
>= frame_count_max
) {
960 kernel_may_pull_from_rx(&hdr
->tp_h
);
963 if (it
>= rx_ring
.layout
.tp_frame_nr
)
966 if (unlikely(sigint
== 1))
969 if (dump_to_pcap(ctx
)) {
970 if (ctx
->dump_mode
== DUMP_INTERVAL_SIZE
) {
971 interval
+= hdr
->tp_h
.tp_snaplen
;
973 if (interval
> ctx
->dump_interval
) {
980 fd
= next_multi_pcap_file(ctx
, fd
);
984 print_pcap_file_stats(sock
, ctx
, skipped
);
989 poll(&rx_poll
, 1, -1);
990 poll_error_maybe_die(sock
, &rx_poll
);
993 bug_on(gettimeofday(&end
, NULL
));
994 diff
= tv_subtract(end
, start
);
996 if (dump_to_pcap(ctx
)) {
998 finish_multi_pcap_file(ctx
, fd
);
1000 finish_single_pcap_file(ctx
, fd
);
1003 if (!(ctx
->dump_dir
&& ctx
->print_mode
== PRINT_NONE
)) {
1004 sock_print_net_stats(sock
, skipped
);
1006 printf("\r%12lu sec, %lu usec in total\n",
1007 diff
.tv_sec
, diff
.tv_usec
);
1013 bpf_release(&bpf_ops
);
1014 dissector_cleanup_all();
1015 destroy_rx_ring(sock
, &rx_ring
);
1017 if (ctx
->promiscuous
)
1018 leave_promiscuous_mode(ctx
->device_in
, ifflags
);
1021 leave_rfmon_mac80211(ctx
->device_trans
, ctx
->device_in
);
1026 static void help(void)
1028 printf("\nnetsniff-ng %s, the packet sniffing beast\n", VERSION_STRING
);
1029 puts("http://www.netsniff-ng.org\n\n"
1030 "Usage: netsniff-ng [options]\n"
1032 " -i|-d|--dev|--in <dev|pcap> Input source as netdev or pcap\n"
1033 " -o|--out <dev|pcap|dir|txf> Output sink as netdev, pcap, directory, txf file\n"
1034 " -f|--filter <bpf-file> Use BPF filter file from bpfc\n"
1035 " -t|--type <type> Only handle packets of defined type:\n"
1036 " host|broadcast|multicast|others|outgoing\n"
1037 " -F|--interval <size/time> Dump interval in time or size if -o is a directory\n"
1038 " pcap swap spec: <num>KiB/MiB/GiB/s/sec/min/hrs\n"
1039 " -J|--jumbo-support Support for 64KB Super Jumbo Frames\n"
1040 " Default RX/TX slot: 2048Byte\n"
1041 " -R|--rfraw Capture or inject raw 802.11 frames\n"
1042 " -n|--num <uint> Number of packets until exit\n"
1043 " `-- 0 Loop until interrupted (default)\n"
1044 " `- n Send n packets and done\n"
1045 "Options for printing:\n"
1046 " -s|--silent Do not print captured packets\n"
1047 " -q|--less Print less-verbose packet information\n"
1048 " -X|--hex Print packet data in hex format\n"
1049 " -l|--ascii Print human-readable packet data\n"
1050 "Options, advanced:\n"
1051 " -P|--prefix <name> Prefix for pcaps stored in directory\n"
1052 " -r|--rand Randomize packet forwarding order\n"
1053 " -M|--no-promisc No promiscuous mode for netdev\n"
1054 " -A|--no-sock-mem Don't tune core socket memory\n"
1055 " -m|--mmap Mmap pcap file i.e., for replaying\n"
1056 " -G|--sg Scatter/gather pcap file I/O\n"
1057 " -c|--clrw Use slower read(2)/write(2) I/O\n"
1058 " -S|--ring-size <size> Manually set ring size to <size>:\n"
1059 " mmap space in KiB/MiB/GiB, e.g. \'10MiB\'\n"
1060 " -k|--kernel-pull <uint> Kernel pull from user interval in us\n"
1061 " Default is 10us where the TX_RING\n"
1062 " is populated with payload from uspace\n"
1063 " -b|--bind-cpu <cpu> Bind to specific CPU (or CPU-range)\n"
1064 " -B|--unbind-cpu <cpu> Forbid to use specific CPU (or CPU-range)\n"
1065 " -u|--user <userid> Drop privileges and change to userid\n"
1066 " -g|--group <groupid> Drop privileges and change to groupid\n"
1067 " -H|--prio-high Make this high priority process\n"
1068 " -Q|--notouch-irq Do not touch IRQ CPU affinity of NIC\n"
1069 " -V|--verbose Be more verbose\n"
1070 " -v|--version Show version\n"
1071 " -h|--help Guess what?!\n\n"
1073 " netsniff-ng --in eth0 --out dump.pcap --silent --bind-cpu 0\n"
1074 " netsniff-ng --in wlan0 --rfraw --out dump.pcap --silent --bind-cpu 0\n"
1075 " netsniff-ng --in dump.pcap --mmap --out eth0 -k1000 --silent --bind-cpu 0\n"
1076 " netsniff-ng --in dump.pcap --out dump.txf --silent --bind-cpu 0\n"
1077 " netsniff-ng --in eth0 --out eth1 --silent --bind-cpu 0 --type host\n"
1078 " netsniff-ng --in eth1 --out /opt/probe/ -s -m -J --interval 100MiB -b 0\n"
1079 " netsniff-ng --in vlan0 --out dump.pcap -c -u `id -u bob` -g `id -g bob`\n"
1080 " netsniff-ng --in any --filter http.bpf --jumbo-support --ascii -V\n\n"
1082 " This tool is targeted for network developers! You should\n"
1083 " be aware of what you are doing and what these options above\n"
1084 " mean! Use netsniff-ng's bpfc compiler for generating filter files.\n"
1085 " Further, netsniff-ng automatically enables the kernel BPF JIT\n"
1086 " if present. Txf file output is only possible if the input source\n"
1087 " is a pcap file.\n\n"
1088 "Please report bugs to <bugs@netsniff-ng.org>\n"
1089 "Copyright (C) 2009-2013 Daniel Borkmann <daniel@netsniff-ng.org>\n"
1090 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel@netsniff-ng.org>\n"
1091 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1092 "License: GNU GPL version 2.0\n"
1093 "This is free software: you are free to change and redistribute it.\n"
1094 "There is NO WARRANTY, to the extent permitted by law.\n");
1098 static void version(void)
1100 printf("\nnetsniff-ng %s, the packet sniffing beast\n", VERSION_STRING
);
1101 puts("http://www.netsniff-ng.org\n\n"
1102 "Please report bugs to <bugs@netsniff-ng.org>\n"
1103 "Copyright (C) 2009-2013 Daniel Borkmann <daniel@netsniff-ng.org>\n"
1104 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel@netsniff-ng.org>\n"
1105 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1106 "License: GNU GPL version 2.0\n"
1107 "This is free software: you are free to change and redistribute it.\n"
1108 "There is NO WARRANTY, to the extent permitted by law.\n");
1112 static void header(void)
1114 printf("%s%s%s\n", colorize_start(bold
), "netsniff-ng " VERSION_STRING
, colorize_end());
1117 int main(int argc
, char **argv
)
1120 int c
, i
, j
, opt_index
, ops_touched
= 0, vals
[4] = {0};
1121 bool prio_high
= false, setsockmem
= true;
1122 void (*main_loop
)(struct ctx
*ctx
) = NULL
;
1124 .link_type
= LINKTYPE_EN10MB
,
1125 .print_mode
= PRINT_NORM
,
1128 .promiscuous
= true,
1130 .pcap
= PCAP_OPS_SG
,
1131 .dump_interval
= 60,
1132 .dump_mode
= DUMP_INTERVAL_TIME
,
1139 while ((c
= getopt_long(argc
, argv
, short_options
, long_options
,
1140 &opt_index
)) != EOF
) {
1144 ctx
.device_in
= xstrdup(optarg
);
1147 ctx
.device_out
= xstrdup(optarg
);
1150 ctx
.prefix
= xstrdup(optarg
);
1153 ctx
.link_type
= LINKTYPE_IEEE802_11
;
1157 ctx
.randomize
= true;
1160 ctx
.jumbo_support
= 1;
1163 ctx
.filter
= xstrdup(optarg
);
1166 ctx
.promiscuous
= false;
1172 ctx
.uid
= strtoul(optarg
, NULL
, 0);
1176 ctx
.gid
= strtoul(optarg
, NULL
, 0);
1180 if (!strncmp(optarg
, "host", strlen("host")))
1181 ctx
.packet_type
= PACKET_HOST
;
1182 else if (!strncmp(optarg
, "broadcast", strlen("broadcast")))
1183 ctx
.packet_type
= PACKET_BROADCAST
;
1184 else if (!strncmp(optarg
, "multicast", strlen("multicast")))
1185 ctx
.packet_type
= PACKET_MULTICAST
;
1186 else if (!strncmp(optarg
, "others", strlen("others")))
1187 ctx
.packet_type
= PACKET_OTHERHOST
;
1188 else if (!strncmp(optarg
, "outgoing", strlen("outgoing")))
1189 ctx
.packet_type
= PACKET_OUTGOING
;
1191 ctx
.packet_type
= -1;
1195 ctx
.reserve_size
= 0;
1197 for (j
= i
= strlen(optarg
); i
> 0; --i
) {
1198 if (!isdigit(optarg
[j
- i
]))
1203 if (!strncmp(ptr
, "KiB", strlen("KiB")))
1204 ctx
.reserve_size
= 1 << 10;
1205 else if (!strncmp(ptr
, "MiB", strlen("MiB")))
1206 ctx
.reserve_size
= 1 << 20;
1207 else if (!strncmp(ptr
, "GiB", strlen("GiB")))
1208 ctx
.reserve_size
= 1 << 30;
1210 panic("Syntax error in ring size param!\n");
1213 ctx
.reserve_size
*= strtol(optarg
, NULL
, 0);
1216 set_cpu_affinity(optarg
, 0);
1217 /* Take the first CPU for rebinding the IRQ */
1219 ctx
.cpu
= strtol(optarg
, NULL
, 0);
1222 set_cpu_affinity(optarg
, 1);
1228 ctx
.pcap
= PCAP_OPS_RW
;
1232 ctx
.pcap
= PCAP_OPS_MMAP
;
1236 ctx
.pcap
= PCAP_OPS_SG
;
1243 ctx
.print_mode
= PRINT_NONE
;
1246 ctx
.print_mode
= PRINT_LESS
;
1250 (ctx
.print_mode
== PRINT_ASCII
) ?
1251 PRINT_HEX_ASCII
: PRINT_HEX
;
1255 (ctx
.print_mode
== PRINT_HEX
) ?
1256 PRINT_HEX_ASCII
: PRINT_ASCII
;
1259 ctx
.kpull
= strtol(optarg
, NULL
, 0);
1262 frame_count_max
= strtol(optarg
, NULL
, 0);
1266 ctx
.dump_interval
= 0;
1268 for (j
= i
= strlen(optarg
); i
> 0; --i
) {
1269 if (!isdigit(optarg
[j
- i
]))
1274 if (!strncmp(ptr
, "KiB", strlen("KiB"))) {
1275 ctx
.dump_interval
= 1 << 10;
1276 ctx
.dump_mode
= DUMP_INTERVAL_SIZE
;
1277 } else if (!strncmp(ptr
, "MiB", strlen("MiB"))) {
1278 ctx
.dump_interval
= 1 << 20;
1279 ctx
.dump_mode
= DUMP_INTERVAL_SIZE
;
1280 } else if (!strncmp(ptr
, "GiB", strlen("GiB"))) {
1281 ctx
.dump_interval
= 1 << 30;
1282 ctx
.dump_mode
= DUMP_INTERVAL_SIZE
;
1283 } else if (!strncmp(ptr
, "sec", strlen("sec"))) {
1284 ctx
.dump_interval
= 1;
1285 ctx
.dump_mode
= DUMP_INTERVAL_TIME
;
1286 } else if (!strncmp(ptr
, "min", strlen("min"))) {
1287 ctx
.dump_interval
= 60;
1288 ctx
.dump_mode
= DUMP_INTERVAL_TIME
;
1289 } else if (!strncmp(ptr
, "hrs", strlen("hrs"))) {
1290 ctx
.dump_interval
= 60 * 60;
1291 ctx
.dump_mode
= DUMP_INTERVAL_TIME
;
1292 } else if (!strncmp(ptr
, "s", strlen("s"))) {
1293 ctx
.dump_interval
= 1;
1294 ctx
.dump_mode
= DUMP_INTERVAL_TIME
;
1296 panic("Syntax error in time/size param!\n");
1300 ctx
.dump_interval
*= strtol(optarg
, NULL
, 0);
1328 panic("Option -%c requires an argument!\n",
1331 if (isprint(optopt
))
1332 whine("Unknown option character "
1333 "`0x%X\'!\n", optopt
);
1342 ctx
.device_in
= xstrdup("any");
1344 register_signal(SIGINT
, signal_handler
);
1345 register_signal(SIGHUP
, signal_handler
);
1352 set_proc_prio(get_default_proc_prio());
1353 set_sched_status(get_default_sched_policy(), get_default_sched_prio());
1356 if (ctx
.device_in
&& (device_mtu(ctx
.device_in
) ||
1357 !strncmp("any", ctx
.device_in
, strlen(ctx
.device_in
)))) {
1358 if (!ctx
.device_out
) {
1360 main_loop
= recv_only_or_dump
;
1361 } else if (device_mtu(ctx
.device_out
)) {
1362 register_signal_f(SIGALRM
, timer_elapsed
, SA_SIGINFO
);
1363 main_loop
= receive_to_xmit
;
1366 register_signal_f(SIGALRM
, timer_next_dump
, SA_SIGINFO
);
1367 main_loop
= recv_only_or_dump
;
1369 ctx
.pcap
= PCAP_OPS_SG
;
1372 if (ctx
.device_out
&& device_mtu(ctx
.device_out
)) {
1373 register_signal_f(SIGALRM
, timer_elapsed
, SA_SIGINFO
);
1374 main_loop
= pcap_to_xmit
;
1376 ctx
.pcap
= PCAP_OPS_MMAP
;
1378 main_loop
= read_pcap
;
1380 ctx
.pcap
= PCAP_OPS_SG
;
1386 init_pcap(ctx
.pcap
, ctx
.jumbo_support
);
1389 set_system_socket_memory(vals
);
1396 reset_system_socket_memory(vals
);
1401 free(ctx
.device_in
);
1402 free(ctx
.device_out
);
1403 free(ctx
.device_trans
);