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
;
64 enum pcap_ops_groups pcap
;
65 enum dump_mode dump_mode
;
69 volatile sig_atomic_t sigint
= 0;
71 static volatile bool next_dump
= false;
73 static const char *short_options
= "d:i:o:rf:MJt:S:k:n:b:B:HQmcsqXlvhF:RGAP:Vu:g:";
74 static const struct option long_options
[] = {
75 {"dev", required_argument
, NULL
, 'd'},
76 {"in", required_argument
, NULL
, 'i'},
77 {"out", required_argument
, NULL
, 'o'},
78 {"filter", required_argument
, NULL
, 'f'},
79 {"num", required_argument
, NULL
, 'n'},
80 {"type", required_argument
, NULL
, 't'},
81 {"interval", required_argument
, NULL
, 'F'},
82 {"ring-size", required_argument
, NULL
, 'S'},
83 {"kernel-pull", required_argument
, NULL
, 'k'},
84 {"bind-cpu", required_argument
, NULL
, 'b'},
85 {"unbind-cpu", required_argument
, NULL
, 'B'},
86 {"prefix", required_argument
, NULL
, 'P'},
87 {"user", required_argument
, NULL
, 'u'},
88 {"group", required_argument
, NULL
, 'g'},
89 {"rand", no_argument
, NULL
, 'r'},
90 {"rfraw", no_argument
, NULL
, 'R'},
91 {"mmap", no_argument
, NULL
, 'm'},
92 {"sg", no_argument
, NULL
, 'G'},
93 {"clrw", no_argument
, NULL
, 'c'},
94 {"jumbo-support", no_argument
, NULL
, 'J'},
95 {"no-promisc", no_argument
, NULL
, 'M'},
96 {"prio-high", no_argument
, NULL
, 'H'},
97 {"notouch-irq", no_argument
, NULL
, 'Q'},
98 {"silent", no_argument
, NULL
, 's'},
99 {"less", no_argument
, NULL
, 'q'},
100 {"hex", no_argument
, NULL
, 'X'},
101 {"ascii", no_argument
, NULL
, 'l'},
102 {"no-sock-mem", no_argument
, NULL
, 'A'},
103 {"verbose", no_argument
, NULL
, 'V'},
104 {"version", no_argument
, NULL
, 'v'},
105 {"help", no_argument
, NULL
, 'h'},
111 static struct itimerval itimer
;
113 static unsigned long frame_count_max
= 0, interval
= TX_KERNEL_PULL_INT
;
115 #define set_system_socket_memory(vals) \
117 if ((vals[0] = get_system_socket_mem(sock_rmem_max)) < SMEM_SUG_MAX) \
118 set_system_socket_mem(sock_rmem_max, SMEM_SUG_MAX); \
119 if ((vals[1] = get_system_socket_mem(sock_rmem_def)) < SMEM_SUG_DEF) \
120 set_system_socket_mem(sock_rmem_def, SMEM_SUG_DEF); \
121 if ((vals[2] = get_system_socket_mem(sock_wmem_max)) < SMEM_SUG_MAX) \
122 set_system_socket_mem(sock_wmem_max, SMEM_SUG_MAX); \
123 if ((vals[3] = get_system_socket_mem(sock_wmem_def)) < SMEM_SUG_DEF) \
124 set_system_socket_mem(sock_wmem_def, SMEM_SUG_DEF); \
127 #define reset_system_socket_memory(vals) \
129 set_system_socket_mem(sock_rmem_max, vals[0]); \
130 set_system_socket_mem(sock_rmem_def, vals[1]); \
131 set_system_socket_mem(sock_wmem_max, vals[2]); \
132 set_system_socket_mem(sock_wmem_def, vals[3]); \
135 #define __pcap_io pcap_ops[ctx->pcap]
137 static void signal_handler(int number
)
148 static void timer_elapsed(int unused
)
150 itimer
.it_interval
.tv_sec
= 0;
151 itimer
.it_interval
.tv_usec
= interval
;
153 itimer
.it_value
.tv_sec
= 0;
154 itimer
.it_value
.tv_usec
= interval
;
156 pull_and_flush_tx_ring(tx_sock
);
157 setitimer(ITIMER_REAL
, &itimer
, NULL
);
160 static void timer_next_dump(int unused
)
162 itimer
.it_interval
.tv_sec
= interval
;
163 itimer
.it_interval
.tv_usec
= 0;
165 itimer
.it_value
.tv_sec
= interval
;
166 itimer
.it_value
.tv_usec
= 0;
169 setitimer(ITIMER_REAL
, &itimer
, NULL
);
172 static inline bool dump_to_pcap(struct ctx
*ctx
)
177 static void pcap_to_xmit(struct ctx
*ctx
)
181 int irq
, ifindex
, fd
= 0, ret
;
182 unsigned int size
, it
= 0;
183 unsigned long trunced
= 0;
185 struct frame_map
*hdr
;
186 struct sock_fprog bpf_ops
;
187 struct timeval start
, end
, diff
;
188 struct pcap_pkthdr phdr
;
190 if (!device_up_and_running(ctx
->device_out
) && !ctx
->rfraw
)
191 panic("Device not up and running!\n");
195 tx_sock
= pf_socket();
197 fd
= open_or_die(ctx
->device_in
, O_RDONLY
| O_LARGEFILE
| O_NOATIME
);
199 ret
= __pcap_io
->pull_file_header(fd
, &ctx
->link_type
);
201 panic("Error reading pcap header!\n");
203 if (__pcap_io
->prepare_reading_pcap
) {
204 ret
= __pcap_io
->prepare_reading_pcap(fd
);
206 panic("Error prepare reading pcap!\n");
209 fmemset(&tx_ring
, 0, sizeof(tx_ring
));
210 fmemset(&bpf_ops
, 0, sizeof(bpf_ops
));
213 ctx
->device_trans
= xstrdup(ctx
->device_out
);
214 xfree(ctx
->device_out
);
216 enter_rfmon_mac80211(ctx
->device_trans
, &ctx
->device_out
);
217 if (ctx
->link_type
!= LINKTYPE_IEEE802_11
)
218 panic("Wrong linktype of pcap!\n");
221 ifindex
= device_ifindex(ctx
->device_out
);
223 size
= ring_size(ctx
->device_out
, ctx
->reserve_size
);
225 bpf_parse_rules(ctx
->filter
, &bpf_ops
);
227 set_packet_loss_discard(tx_sock
);
228 set_sockopt_hwtimestamp(tx_sock
, ctx
->device_out
);
230 setup_tx_ring_layout(tx_sock
, &tx_ring
, size
, ctx
->jumbo_support
);
231 create_tx_ring(tx_sock
, &tx_ring
, ctx
->verbose
);
232 mmap_tx_ring(tx_sock
, &tx_ring
);
233 alloc_tx_ring_frames(&tx_ring
);
234 bind_tx_ring(tx_sock
, &tx_ring
, ifindex
);
236 dissector_init_all(ctx
->print_mode
);
238 if (ctx
->cpu
>= 0 && ifindex
> 0) {
239 irq
= device_irq_number(ctx
->device_out
);
240 device_bind_irq_to_cpu(irq
, ctx
->cpu
);
243 printf("IRQ: %s:%d > CPU%d\n",
244 ctx
->device_out
, irq
, ctx
->cpu
);
248 interval
= ctx
->kpull
;
252 bpf_dump_all(&bpf_ops
);
254 printf("MD: TX %luus %s ", interval
, pcap_ops
[ctx
->pcap
]->name
);
256 printf("802.11 raw via %s ", ctx
->device_out
);
257 #ifdef _LARGEFILE64_SOURCE
263 printf("Running! Hang up with ^C!\n\n");
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 bug_on(gettimeofday(&start
, NULL
));
276 while (likely(sigint
== 0)) {
277 while (user_may_pull_from_tx(tx_ring
.frames
[it
].iov_base
)) {
278 hdr
= tx_ring
.frames
[it
].iov_base
;
280 /* Kernel assumes: data = ph.raw + po->tp_hdrlen -
281 * sizeof(struct sockaddr_ll); */
282 out
= ((uint8_t *) hdr
) + TPACKET2_HDRLEN
- sizeof(struct sockaddr_ll
);
285 ret
= __pcap_io
->read_pcap_pkt(fd
, &phdr
, out
,
286 ring_frame_size(&tx_ring
));
287 if (unlikely(ret
<= 0))
290 if (ring_frame_size(&tx_ring
) < phdr
.len
) {
291 phdr
.len
= ring_frame_size(&tx_ring
);
294 } while (ctx
->filter
&& !bpf_run_filter(&bpf_ops
, out
, phdr
.len
));
296 pcap_pkthdr_to_tpacket_hdr(&phdr
, &hdr
->tp_h
);
298 ctx
->tx_bytes
+= hdr
->tp_h
.tp_len
;;
301 show_frame_hdr(hdr
, ctx
->print_mode
, RING_MODE_EGRESS
);
303 dissector_entry_point(out
, hdr
->tp_h
.tp_snaplen
,
304 ctx
->link_type
, ctx
->print_mode
);
306 kernel_may_pull_from_tx(&hdr
->tp_h
);
309 if (it
>= tx_ring
.layout
.tp_frame_nr
)
312 if (unlikely(sigint
== 1))
315 if (frame_count_max
!= 0) {
316 if (ctx
->tx_packets
>= frame_count_max
) {
326 bug_on(gettimeofday(&end
, NULL
));
327 diff
= tv_subtract(end
, start
);
329 bpf_release(&bpf_ops
);
331 dissector_cleanup_all();
332 destroy_tx_ring(tx_sock
, &tx_ring
);
335 leave_rfmon_mac80211(ctx
->device_trans
, ctx
->device_out
);
337 if (__pcap_io
->prepare_close_pcap
)
338 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_READ
);
345 printf("\r%12lu packets outgoing\n", ctx
->tx_packets
);
346 printf("\r%12lu packets truncated in file\n", trunced
);
347 printf("\r%12lu bytes outgoing\n", ctx
->tx_bytes
);
348 printf("\r%12lu sec, %lu usec in total\n", diff
.tv_sec
, diff
.tv_usec
);
351 static void receive_to_xmit(struct ctx
*ctx
)
355 int rx_sock
, ifindex_in
, ifindex_out
;
356 unsigned int size_in
, size_out
, it_in
= 0, it_out
= 0;
357 unsigned long frame_count
= 0;
358 struct frame_map
*hdr_in
, *hdr_out
;
359 struct ring tx_ring
, rx_ring
;
360 struct pollfd rx_poll
;
361 struct sock_fprog bpf_ops
;
363 if (!strncmp(ctx
->device_in
, ctx
->device_out
, IFNAMSIZ
))
364 panic("Ingress/egress devices must be different!\n");
365 if (!device_up_and_running(ctx
->device_out
))
366 panic("Egress device not up and running!\n");
367 if (!device_up_and_running(ctx
->device_in
))
368 panic("Ingress device not up and running!\n");
370 rx_sock
= pf_socket();
371 tx_sock
= pf_socket();
373 fmemset(&tx_ring
, 0, sizeof(tx_ring
));
374 fmemset(&rx_ring
, 0, sizeof(rx_ring
));
375 fmemset(&rx_poll
, 0, sizeof(rx_poll
));
376 fmemset(&bpf_ops
, 0, sizeof(bpf_ops
));
378 ifindex_in
= device_ifindex(ctx
->device_in
);
379 ifindex_out
= device_ifindex(ctx
->device_out
);
381 size_in
= ring_size(ctx
->device_in
, ctx
->reserve_size
);
382 size_out
= ring_size(ctx
->device_out
, ctx
->reserve_size
);
384 enable_kernel_bpf_jit_compiler();
386 bpf_parse_rules(ctx
->filter
, &bpf_ops
);
387 bpf_attach_to_sock(rx_sock
, &bpf_ops
);
389 setup_rx_ring_layout(rx_sock
, &rx_ring
, size_in
, ctx
->jumbo_support
);
390 create_rx_ring(rx_sock
, &rx_ring
, ctx
->verbose
);
391 mmap_rx_ring(rx_sock
, &rx_ring
);
392 alloc_rx_ring_frames(&rx_ring
);
393 bind_rx_ring(rx_sock
, &rx_ring
, ifindex_in
);
394 prepare_polling(rx_sock
, &rx_poll
);
396 set_packet_loss_discard(tx_sock
);
397 setup_tx_ring_layout(tx_sock
, &tx_ring
, size_out
, ctx
->jumbo_support
);
398 create_tx_ring(tx_sock
, &tx_ring
, ctx
->verbose
);
399 mmap_tx_ring(tx_sock
, &tx_ring
);
400 alloc_tx_ring_frames(&tx_ring
);
401 bind_tx_ring(tx_sock
, &tx_ring
, ifindex_out
);
403 dissector_init_all(ctx
->print_mode
);
405 if (ctx
->promiscuous
)
406 ifflags
= enter_promiscuous_mode(ctx
->device_in
);
409 interval
= ctx
->kpull
;
411 itimer
.it_interval
.tv_sec
= 0;
412 itimer
.it_interval
.tv_usec
= interval
;
414 itimer
.it_value
.tv_sec
= 0;
415 itimer
.it_value
.tv_usec
= interval
;
417 setitimer(ITIMER_REAL
, &itimer
, NULL
);
421 bpf_dump_all(&bpf_ops
);
423 printf("MD: RXTX %luus\n\n", interval
);
425 printf("Running! Hang up with ^C!\n\n");
428 while (likely(sigint
== 0)) {
429 while (user_may_pull_from_rx(rx_ring
.frames
[it_in
].iov_base
)) {
432 hdr_in
= rx_ring
.frames
[it_in
].iov_base
;
433 in
= ((uint8_t *) hdr_in
) + hdr_in
->tp_h
.tp_mac
;
437 if (ctx
->packet_type
!= -1)
438 if (ctx
->packet_type
!= hdr_in
->s_ll
.sll_pkttype
)
441 hdr_out
= tx_ring
.frames
[it_out
].iov_base
;
442 out
= ((uint8_t *) hdr_out
) + TPACKET2_HDRLEN
- sizeof(struct sockaddr_ll
);
444 for (; !user_may_pull_from_tx(tx_ring
.frames
[it_out
].iov_base
) &&
447 next_rnd_slot(&it_out
, &tx_ring
);
450 if (it_out
>= tx_ring
.layout
.tp_frame_nr
)
454 hdr_out
= tx_ring
.frames
[it_out
].iov_base
;
455 out
= ((uint8_t *) hdr_out
) + TPACKET2_HDRLEN
- sizeof(struct sockaddr_ll
);
458 tpacket_hdr_clone(&hdr_out
->tp_h
, &hdr_in
->tp_h
);
459 fmemcpy(out
, in
, hdr_in
->tp_h
.tp_len
);
461 kernel_may_pull_from_tx(&hdr_out
->tp_h
);
463 next_rnd_slot(&it_out
, &tx_ring
);
466 if (it_out
>= tx_ring
.layout
.tp_frame_nr
)
470 show_frame_hdr(hdr_in
, ctx
->print_mode
, RING_MODE_INGRESS
);
472 dissector_entry_point(in
, hdr_in
->tp_h
.tp_snaplen
,
473 ctx
->link_type
, ctx
->print_mode
);
475 if (frame_count_max
!= 0) {
476 if (frame_count
>= frame_count_max
) {
484 kernel_may_pull_from_rx(&hdr_in
->tp_h
);
487 if (it_in
>= rx_ring
.layout
.tp_frame_nr
)
490 if (unlikely(sigint
== 1))
494 poll(&rx_poll
, 1, -1);
495 poll_error_maybe_die(rx_sock
, &rx_poll
);
500 sock_print_net_stats(rx_sock
, 0);
502 bpf_release(&bpf_ops
);
504 dissector_cleanup_all();
506 destroy_tx_ring(tx_sock
, &tx_ring
);
507 destroy_rx_ring(rx_sock
, &rx_ring
);
509 if (ctx
->promiscuous
)
510 leave_promiscuous_mode(ctx
->device_in
, ifflags
);
516 static void translate_pcap_to_txf(int fdo
, uint8_t *out
, size_t len
)
518 size_t bytes_done
= 0;
521 slprintf(bout
, sizeof(bout
), "{\n ");
522 write_or_die(fdo
, bout
, strlen(bout
));
524 while (bytes_done
< len
) {
525 slprintf(bout
, sizeof(bout
), "0x%02x, ", out
[bytes_done
]);
526 write_or_die(fdo
, bout
, strlen(bout
));
530 if (bytes_done
% 10 == 0) {
531 slprintf(bout
, sizeof(bout
), "\n");
532 write_or_die(fdo
, bout
, strlen(bout
));
534 if (bytes_done
< len
) {
535 slprintf(bout
, sizeof(bout
), " ");
536 write_or_die(fdo
, bout
, strlen(bout
));
540 if (bytes_done
% 10 != 0) {
541 slprintf(bout
, sizeof(bout
), "\n");
542 write_or_die(fdo
, bout
, strlen(bout
));
545 slprintf(bout
, sizeof(bout
), "}\n\n");
546 write_or_die(fdo
, bout
, strlen(bout
));
549 static void read_pcap(struct ctx
*ctx
)
553 int ret
, fd
, fdo
= 0;
554 unsigned long trunced
= 0;
556 struct pcap_pkthdr phdr
;
557 struct sock_fprog bpf_ops
;
559 struct timeval start
, end
, diff
;
563 fd
= open_or_die(ctx
->device_in
, O_RDONLY
| O_LARGEFILE
| O_NOATIME
);
565 ret
= __pcap_io
->pull_file_header(fd
, &ctx
->link_type
);
567 panic("Error reading pcap header!\n");
569 if (__pcap_io
->prepare_reading_pcap
) {
570 ret
= __pcap_io
->prepare_reading_pcap(fd
);
572 panic("Error prepare reading pcap!\n");
575 fmemset(&fm
, 0, sizeof(fm
));
576 fmemset(&bpf_ops
, 0, sizeof(bpf_ops
));
578 bpf_parse_rules(ctx
->filter
, &bpf_ops
);
580 dissector_init_all(ctx
->print_mode
);
582 out_len
= round_up(1024 * 1024, PAGE_SIZE
);
583 out
= xmalloc_aligned(out_len
, CO_CACHE_LINE_SIZE
);
587 bpf_dump_all(&bpf_ops
);
589 printf("MD: RD %s ", __pcap_io
->name
);
590 #ifdef _LARGEFILE64_SOURCE
596 printf("Running! Hang up with ^C!\n\n");
600 fdo
= open_or_die_m(ctx
->device_out
, O_RDWR
| O_CREAT
|
601 O_TRUNC
| O_LARGEFILE
, DEFFILEMODE
);
603 bug_on(gettimeofday(&start
, NULL
));
605 while (likely(sigint
== 0)) {
607 ret
= __pcap_io
->read_pcap_pkt(fd
, &phdr
, out
, out_len
);
608 if (unlikely(ret
< 0))
611 if (unlikely(phdr
.len
== 0)) {
616 if (unlikely(phdr
.len
> out_len
)) {
620 } while (ctx
->filter
&& !bpf_run_filter(&bpf_ops
, out
, phdr
.len
));
622 pcap_pkthdr_to_tpacket_hdr(&phdr
, &fm
.tp_h
);
624 ctx
->tx_bytes
+= fm
.tp_h
.tp_len
;
627 show_frame_hdr(&fm
, ctx
->print_mode
, RING_MODE_EGRESS
);
629 dissector_entry_point(out
, fm
.tp_h
.tp_snaplen
,
630 ctx
->link_type
, ctx
->print_mode
);
633 translate_pcap_to_txf(fdo
, out
, fm
.tp_h
.tp_snaplen
);
635 if (frame_count_max
!= 0) {
636 if (ctx
->tx_packets
>= frame_count_max
) {
645 bug_on(gettimeofday(&end
, NULL
));
646 diff
= tv_subtract(end
, start
);
648 bpf_release(&bpf_ops
);
650 dissector_cleanup_all();
652 if (__pcap_io
->prepare_close_pcap
)
653 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_READ
);
663 printf("\r%12lu packets outgoing\n", ctx
->tx_packets
);
664 printf("\r%12lu packets truncated in file\n", trunced
);
665 printf("\r%12lu bytes outgoing\n", ctx
->tx_bytes
);
666 printf("\r%12lu sec, %lu usec in total\n", diff
.tv_sec
, diff
.tv_usec
);
669 static void finish_multi_pcap_file(struct ctx
*ctx
, int fd
)
671 __pcap_io
->fsync_pcap(fd
);
673 if (__pcap_io
->prepare_close_pcap
)
674 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_WRITE
);
678 fmemset(&itimer
, 0, sizeof(itimer
));
679 setitimer(ITIMER_REAL
, &itimer
, NULL
);
682 static int next_multi_pcap_file(struct ctx
*ctx
, int fd
)
687 __pcap_io
->fsync_pcap(fd
);
689 if (__pcap_io
->prepare_close_pcap
)
690 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_WRITE
);
694 slprintf(fname
, sizeof(fname
), "%s/%s%lu.pcap", ctx
->device_out
,
695 ctx
->prefix
? : "dump-", time(0));
697 fd
= open_or_die_m(fname
, O_RDWR
| O_CREAT
| O_TRUNC
|
698 O_LARGEFILE
, DEFFILEMODE
);
700 ret
= __pcap_io
->push_file_header(fd
, ctx
->link_type
);
702 panic("Error writing pcap header!\n");
704 if (__pcap_io
->prepare_writing_pcap
) {
705 ret
= __pcap_io
->prepare_writing_pcap(fd
);
707 panic("Error prepare writing pcap!\n");
713 static int begin_multi_pcap_file(struct ctx
*ctx
)
720 if (ctx
->device_out
[strlen(ctx
->device_out
) - 1] == '/')
721 ctx
->device_out
[strlen(ctx
->device_out
) - 1] = 0;
723 slprintf(fname
, sizeof(fname
), "%s/%s%lu.pcap", ctx
->device_out
,
724 ctx
->prefix
? : "dump-", time(0));
726 fd
= open_or_die_m(fname
, O_RDWR
| O_CREAT
| O_TRUNC
|
727 O_LARGEFILE
, DEFFILEMODE
);
729 ret
= __pcap_io
->push_file_header(fd
, ctx
->link_type
);
731 panic("Error writing pcap header!\n");
733 if (__pcap_io
->prepare_writing_pcap
) {
734 ret
= __pcap_io
->prepare_writing_pcap(fd
);
736 panic("Error prepare writing pcap!\n");
739 if (ctx
->dump_mode
== DUMP_INTERVAL_TIME
) {
740 interval
= ctx
->dump_interval
;
742 itimer
.it_interval
.tv_sec
= interval
;
743 itimer
.it_interval
.tv_usec
= 0;
745 itimer
.it_value
.tv_sec
= interval
;
746 itimer
.it_value
.tv_usec
= 0;
748 setitimer(ITIMER_REAL
, &itimer
, NULL
);
756 static void finish_single_pcap_file(struct ctx
*ctx
, int fd
)
758 __pcap_io
->fsync_pcap(fd
);
760 if (__pcap_io
->prepare_close_pcap
)
761 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_WRITE
);
766 static int begin_single_pcap_file(struct ctx
*ctx
)
772 fd
= open_or_die_m(ctx
->device_out
, O_RDWR
| O_CREAT
| O_TRUNC
|
773 O_LARGEFILE
, DEFFILEMODE
);
775 ret
= __pcap_io
->push_file_header(fd
, ctx
->link_type
);
777 panic("Error writing pcap header!\n");
779 if (__pcap_io
->prepare_writing_pcap
) {
780 ret
= __pcap_io
->prepare_writing_pcap(fd
);
782 panic("Error prepare writing pcap!\n");
788 static void print_pcap_file_stats(int sock
, struct ctx
*ctx
, unsigned long skipped
)
790 unsigned long good
, bad
;
791 struct tpacket_stats kstats
;
792 socklen_t slen
= sizeof(kstats
);
794 fmemset(&kstats
, 0, sizeof(kstats
));
795 getsockopt(sock
, SOL_PACKET
, PACKET_STATISTICS
, &kstats
, &slen
);
797 if (ctx
->print_mode
== PRINT_NONE
) {
798 good
= kstats
.tp_packets
- kstats
.tp_drops
- skipped
;
799 bad
= kstats
.tp_drops
+ skipped
;
801 printf(".(+%lu/-%lu)", good
, bad
);
806 static void recv_only_or_dump(struct ctx
*ctx
)
810 int sock
, irq
, ifindex
, fd
= 0, ret
;
811 unsigned int size
, it
= 0;
812 unsigned long frame_count
= 0, skipped
= 0;
814 struct pollfd rx_poll
;
815 struct frame_map
*hdr
;
816 struct sock_fprog bpf_ops
;
817 struct timeval start
, end
, diff
;
818 struct pcap_pkthdr phdr
;
820 if (!device_up_and_running(ctx
->device_in
) && !ctx
->rfraw
)
821 panic("Device not up and running!\n");
826 ctx
->device_trans
= xstrdup(ctx
->device_in
);
827 xfree(ctx
->device_in
);
829 enter_rfmon_mac80211(ctx
->device_trans
, &ctx
->device_in
);
830 ctx
->link_type
= LINKTYPE_IEEE802_11
;
833 if (dump_to_pcap(ctx
)) {
837 fmemset(&stats
, 0, sizeof(stats
));
838 ret
= stat(ctx
->device_out
, &stats
);
844 ctx
->dump_dir
= S_ISDIR(stats
.st_mode
);
846 fd
= begin_multi_pcap_file(ctx
);
849 fd
= begin_single_pcap_file(ctx
);
853 fmemset(&rx_ring
, 0, sizeof(rx_ring
));
854 fmemset(&rx_poll
, 0, sizeof(rx_poll
));
855 fmemset(&bpf_ops
, 0, sizeof(bpf_ops
));
857 ifindex
= device_ifindex(ctx
->device_in
);
859 size
= ring_size(ctx
->device_in
, ctx
->reserve_size
);
861 enable_kernel_bpf_jit_compiler();
863 bpf_parse_rules(ctx
->filter
, &bpf_ops
);
864 bpf_attach_to_sock(sock
, &bpf_ops
);
866 set_sockopt_hwtimestamp(sock
, ctx
->device_in
);
868 setup_rx_ring_layout(sock
, &rx_ring
, size
, ctx
->jumbo_support
);
869 create_rx_ring(sock
, &rx_ring
, ctx
->verbose
);
870 mmap_rx_ring(sock
, &rx_ring
);
871 alloc_rx_ring_frames(&rx_ring
);
872 bind_rx_ring(sock
, &rx_ring
, ifindex
);
874 prepare_polling(sock
, &rx_poll
);
875 dissector_init_all(ctx
->print_mode
);
877 if (ctx
->cpu
>= 0 && ifindex
> 0) {
878 irq
= device_irq_number(ctx
->device_in
);
879 device_bind_irq_to_cpu(irq
, ctx
->cpu
);
882 printf("IRQ: %s:%d > CPU%d\n",
883 ctx
->device_in
, irq
, ctx
->cpu
);
886 if (ctx
->promiscuous
)
887 ifflags
= enter_promiscuous_mode(ctx
->device_in
);
891 bpf_dump_all(&bpf_ops
);
893 printf("MD: RX %s ", ctx
->dump
? pcap_ops
[ctx
->pcap
]->name
: "");
895 printf("802.11 raw via %s ", ctx
->device_in
);
896 #ifdef _LARGEFILE64_SOURCE
902 printf("Running! Hang up with ^C!\n\n");
905 bug_on(gettimeofday(&start
, NULL
));
907 while (likely(sigint
== 0)) {
908 while (user_may_pull_from_rx(rx_ring
.frames
[it
].iov_base
)) {
911 hdr
= rx_ring
.frames
[it
].iov_base
;
912 packet
= ((uint8_t *) hdr
) + hdr
->tp_h
.tp_mac
;
915 if (ctx
->packet_type
!= -1)
916 if (ctx
->packet_type
!= hdr
->s_ll
.sll_pkttype
)
919 if (unlikely(ring_frame_size(&rx_ring
) < hdr
->tp_h
.tp_snaplen
)) {
924 if (dump_to_pcap(ctx
)) {
925 tpacket_hdr_to_pcap_pkthdr(&hdr
->tp_h
, &phdr
);
927 ret
= __pcap_io
->write_pcap_pkt(fd
, &phdr
, packet
, phdr
.len
);
928 if (unlikely(ret
!= sizeof(phdr
) + phdr
.len
))
929 panic("Write error to pcap!\n");
932 show_frame_hdr(hdr
, ctx
->print_mode
, RING_MODE_INGRESS
);
934 dissector_entry_point(packet
, hdr
->tp_h
.tp_snaplen
,
935 ctx
->link_type
, ctx
->print_mode
);
937 if (frame_count_max
!= 0) {
938 if (frame_count
>= frame_count_max
) {
946 kernel_may_pull_from_rx(&hdr
->tp_h
);
949 if (it
>= rx_ring
.layout
.tp_frame_nr
)
952 if (unlikely(sigint
== 1))
955 if (dump_to_pcap(ctx
)) {
956 if (ctx
->dump_mode
== DUMP_INTERVAL_SIZE
) {
957 interval
+= hdr
->tp_h
.tp_snaplen
;
959 if (interval
> ctx
->dump_interval
) {
966 fd
= next_multi_pcap_file(ctx
, fd
);
970 print_pcap_file_stats(sock
, ctx
, skipped
);
975 poll(&rx_poll
, 1, -1);
976 poll_error_maybe_die(sock
, &rx_poll
);
979 bug_on(gettimeofday(&end
, NULL
));
980 diff
= tv_subtract(end
, start
);
982 if (!(ctx
->dump_dir
&& ctx
->print_mode
== PRINT_NONE
)) {
983 sock_print_net_stats(sock
, skipped
);
985 printf("\r%12lu sec, %lu usec in total\n",
986 diff
.tv_sec
, diff
.tv_usec
);
992 bpf_release(&bpf_ops
);
993 dissector_cleanup_all();
994 destroy_rx_ring(sock
, &rx_ring
);
996 if (ctx
->promiscuous
)
997 leave_promiscuous_mode(ctx
->device_in
, ifflags
);
1000 leave_rfmon_mac80211(ctx
->device_trans
, ctx
->device_in
);
1004 if (dump_to_pcap(ctx
)) {
1006 finish_multi_pcap_file(ctx
, fd
);
1008 finish_single_pcap_file(ctx
, fd
);
1012 static void help(void)
1014 printf("\nnetsniff-ng %s, the packet sniffing beast\n", VERSION_STRING
);
1015 puts("http://www.netsniff-ng.org\n\n"
1016 "Usage: netsniff-ng [options]\n"
1018 " -i|-d|--dev|--in <dev|pcap> Input source as netdev or pcap\n"
1019 " -o|--out <dev|pcap|dir|txf> Output sink as netdev, pcap, directory, txf file\n"
1020 " -f|--filter <bpf-file> Use BPF filter file from bpfc\n"
1021 " -t|--type <type> Only handle packets of defined type:\n"
1022 " host|broadcast|multicast|others|outgoing\n"
1023 " -F|--interval <size/time> Dump interval in time or size if -o is a directory\n"
1024 " pcap swap spec: <num>KiB/MiB/GiB/s/sec/min/hrs\n"
1025 " -J|--jumbo-support Support for 64KB Super Jumbo Frames\n"
1026 " Default RX/TX slot: 2048Byte\n"
1027 " -R|--rfraw Capture or inject raw 802.11 frames\n"
1028 " -n|--num <uint> Number of packets until exit\n"
1029 " `-- 0 Loop until interrupted (default)\n"
1030 " `- n Send n packets and done\n"
1031 "Options for printing:\n"
1032 " -s|--silent Do not print captured packets\n"
1033 " -q|--less Print less-verbose packet information\n"
1034 " -X|--hex Print packet data in hex format\n"
1035 " -l|--ascii Print human-readable packet data\n"
1036 "Options, advanced:\n"
1037 " -P|--prefix <name> Prefix for pcaps stored in directory\n"
1038 " -r|--rand Randomize packet forwarding order\n"
1039 " -M|--no-promisc No promiscuous mode for netdev\n"
1040 " -A|--no-sock-mem Don't tune core socket memory\n"
1041 " -m|--mmap Mmap pcap file i.e., for replaying\n"
1042 " -G|--sg Scatter/gather pcap file I/O\n"
1043 " -c|--clrw Use slower read(2)/write(2) I/O\n"
1044 " -S|--ring-size <size> Manually set ring size to <size>:\n"
1045 " mmap space in KiB/MiB/GiB, e.g. \'10MiB\'\n"
1046 " -k|--kernel-pull <uint> Kernel pull from user interval in us\n"
1047 " Default is 10us where the TX_RING\n"
1048 " is populated with payload from uspace\n"
1049 " -b|--bind-cpu <cpu> Bind to specific CPU (or CPU-range)\n"
1050 " -B|--unbind-cpu <cpu> Forbid to use specific CPU (or CPU-range)\n"
1051 " -u|--user <userid> Drop privileges and change to userid\n"
1052 " -g|--group <groupid> Drop privileges and change to groupid\n"
1053 " -H|--prio-high Make this high priority process\n"
1054 " -Q|--notouch-irq Do not touch IRQ CPU affinity of NIC\n"
1055 " -V|--verbose Be more verbose\n"
1056 " -v|--version Show version\n"
1057 " -h|--help Guess what?!\n\n"
1059 " netsniff-ng --in eth0 --out dump.pcap --silent --bind-cpu 0\n"
1060 " netsniff-ng --in wlan0 --rfraw --out dump.pcap --silent --bind-cpu 0\n"
1061 " netsniff-ng --in dump.pcap --mmap --out eth0 -k1000 --silent --bind-cpu 0\n"
1062 " netsniff-ng --in dump.pcap --out dump.txf --silent --bind-cpu 0\n"
1063 " netsniff-ng --in eth0 --out eth1 --silent --bind-cpu 0 --type host\n"
1064 " netsniff-ng --in eth1 --out /opt/probe/ -s -m -J --interval 100MiB -b 0\n"
1065 " netsniff-ng --in vlan0 --out dump.pcap -c -u `id -u bob` -g `id -g bob`\n"
1066 " netsniff-ng --in any --filter http.bpf --jumbo-support --ascii -V\n\n"
1068 " This tool is targeted for network developers! You should\n"
1069 " be aware of what you are doing and what these options above\n"
1070 " mean! Use netsniff-ng's bpfc compiler for generating filter files.\n"
1071 " Further, netsniff-ng automatically enables the kernel BPF JIT\n"
1072 " if present. Txf file output is only possible if the input source\n"
1073 " is a pcap file.\n\n"
1074 "Please report bugs to <bugs@netsniff-ng.org>\n"
1075 "Copyright (C) 2009-2013 Daniel Borkmann <daniel@netsniff-ng.org>\n"
1076 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel@netsniff-ng.org>\n"
1077 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1078 "License: GNU GPL version 2.0\n"
1079 "This is free software: you are free to change and redistribute it.\n"
1080 "There is NO WARRANTY, to the extent permitted by law.\n");
1084 static void version(void)
1086 printf("\nnetsniff-ng %s, the packet sniffing beast\n", VERSION_STRING
);
1087 puts("http://www.netsniff-ng.org\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 header(void)
1100 printf("%s%s%s\n", colorize_start(bold
), "netsniff-ng " VERSION_STRING
, colorize_end());
1103 int main(int argc
, char **argv
)
1106 int c
, i
, j
, opt_index
, ops_touched
= 0, vals
[4] = {0};
1107 bool prio_high
= false, setsockmem
= true, enforce
= false;
1108 void (*main_loop
)(struct ctx
*ctx
) = NULL
;
1109 uid_t uid
= getuid();
1110 gid_t gid
= getgid();
1112 .link_type
= LINKTYPE_EN10MB
,
1113 .print_mode
= PRINT_NORM
,
1116 .promiscuous
= true,
1118 .pcap
= PCAP_OPS_SG
,
1119 .dump_interval
= 60,
1120 .dump_mode
= DUMP_INTERVAL_TIME
,
1125 while ((c
= getopt_long(argc
, argv
, short_options
, long_options
,
1126 &opt_index
)) != EOF
) {
1130 ctx
.device_in
= xstrdup(optarg
);
1133 ctx
.device_out
= xstrdup(optarg
);
1136 ctx
.prefix
= xstrdup(optarg
);
1139 ctx
.link_type
= LINKTYPE_IEEE802_11
;
1143 ctx
.randomize
= true;
1146 ctx
.jumbo_support
= 1;
1149 ctx
.filter
= xstrdup(optarg
);
1152 ctx
.promiscuous
= false;
1158 uid
= strtoul(optarg
, NULL
, 0);
1162 gid
= strtoul(optarg
, NULL
, 0);
1166 if (!strncmp(optarg
, "host", strlen("host")))
1167 ctx
.packet_type
= PACKET_HOST
;
1168 else if (!strncmp(optarg
, "broadcast", strlen("broadcast")))
1169 ctx
.packet_type
= PACKET_BROADCAST
;
1170 else if (!strncmp(optarg
, "multicast", strlen("multicast")))
1171 ctx
.packet_type
= PACKET_MULTICAST
;
1172 else if (!strncmp(optarg
, "others", strlen("others")))
1173 ctx
.packet_type
= PACKET_OTHERHOST
;
1174 else if (!strncmp(optarg
, "outgoing", strlen("outgoing")))
1175 ctx
.packet_type
= PACKET_OUTGOING
;
1177 ctx
.packet_type
= -1;
1181 ctx
.reserve_size
= 0;
1183 for (j
= i
= strlen(optarg
); i
> 0; --i
) {
1184 if (!isdigit(optarg
[j
- i
]))
1189 if (!strncmp(ptr
, "KiB", strlen("KiB")))
1190 ctx
.reserve_size
= 1 << 10;
1191 else if (!strncmp(ptr
, "MiB", strlen("MiB")))
1192 ctx
.reserve_size
= 1 << 20;
1193 else if (!strncmp(ptr
, "GiB", strlen("GiB")))
1194 ctx
.reserve_size
= 1 << 30;
1196 panic("Syntax error in ring size param!\n");
1199 ctx
.reserve_size
*= strtol(optarg
, NULL
, 0);
1202 set_cpu_affinity(optarg
, 0);
1203 /* Take the first CPU for rebinding the IRQ */
1205 ctx
.cpu
= strtol(optarg
, NULL
, 0);
1208 set_cpu_affinity(optarg
, 1);
1214 ctx
.pcap
= PCAP_OPS_RW
;
1218 ctx
.pcap
= PCAP_OPS_MMAP
;
1222 ctx
.pcap
= PCAP_OPS_SG
;
1229 ctx
.print_mode
= PRINT_NONE
;
1232 ctx
.print_mode
= PRINT_LESS
;
1236 (ctx
.print_mode
== PRINT_ASCII
) ?
1237 PRINT_HEX_ASCII
: PRINT_HEX
;
1241 (ctx
.print_mode
== PRINT_HEX
) ?
1242 PRINT_HEX_ASCII
: PRINT_ASCII
;
1245 ctx
.kpull
= strtol(optarg
, NULL
, 0);
1248 frame_count_max
= strtol(optarg
, NULL
, 0);
1252 ctx
.dump_interval
= 0;
1254 for (j
= i
= strlen(optarg
); i
> 0; --i
) {
1255 if (!isdigit(optarg
[j
- i
]))
1260 if (!strncmp(ptr
, "KiB", strlen("KiB"))) {
1261 ctx
.dump_interval
= 1 << 10;
1262 ctx
.dump_mode
= DUMP_INTERVAL_SIZE
;
1263 } else if (!strncmp(ptr
, "MiB", strlen("MiB"))) {
1264 ctx
.dump_interval
= 1 << 20;
1265 ctx
.dump_mode
= DUMP_INTERVAL_SIZE
;
1266 } else if (!strncmp(ptr
, "GiB", strlen("GiB"))) {
1267 ctx
.dump_interval
= 1 << 30;
1268 ctx
.dump_mode
= DUMP_INTERVAL_SIZE
;
1269 } else if (!strncmp(ptr
, "sec", strlen("sec"))) {
1270 ctx
.dump_interval
= 1;
1271 ctx
.dump_mode
= DUMP_INTERVAL_TIME
;
1272 } else if (!strncmp(ptr
, "min", strlen("min"))) {
1273 ctx
.dump_interval
= 60;
1274 ctx
.dump_mode
= DUMP_INTERVAL_TIME
;
1275 } else if (!strncmp(ptr
, "hrs", strlen("hrs"))) {
1276 ctx
.dump_interval
= 60 * 60;
1277 ctx
.dump_mode
= DUMP_INTERVAL_TIME
;
1278 } else if (!strncmp(ptr
, "s", strlen("s"))) {
1279 ctx
.dump_interval
= 1;
1280 ctx
.dump_mode
= DUMP_INTERVAL_TIME
;
1282 panic("Syntax error in time/size param!\n");
1286 ctx
.dump_interval
*= strtol(optarg
, NULL
, 0);
1314 panic("Option -%c requires an argument!\n",
1317 if (isprint(optopt
))
1318 whine("Unknown option character "
1319 "`0x%X\'!\n", optopt
);
1328 if (uid
== getuid())
1329 panic("Uid cannot be the same as the current user!\n");
1330 if (gid
== getgid())
1331 panic("Gid cannot be the same as the current user!\n");
1333 if (setgid(gid
) != 0)
1334 panic("Unable to drop group privileges: %s!\n", strerror(errno
));
1335 if (setuid(uid
) != 0)
1336 panic("Unable to drop user privileges: %s!\n", strerror(errno
));
1339 ctx
.device_in
= xstrdup("any");
1341 register_signal(SIGINT
, signal_handler
);
1342 register_signal(SIGHUP
, signal_handler
);
1346 init_pcap(ctx
.jumbo_support
);
1350 set_proc_prio(get_default_proc_prio());
1351 set_sched_status(get_default_sched_policy(), get_default_sched_prio());
1354 if (ctx
.device_in
&& (device_mtu(ctx
.device_in
) ||
1355 !strncmp("any", ctx
.device_in
, strlen(ctx
.device_in
)))) {
1356 if (!ctx
.device_out
) {
1358 main_loop
= recv_only_or_dump
;
1359 } else if (device_mtu(ctx
.device_out
)) {
1360 register_signal_f(SIGALRM
, timer_elapsed
, SA_SIGINFO
);
1361 main_loop
= receive_to_xmit
;
1364 register_signal_f(SIGALRM
, timer_next_dump
, SA_SIGINFO
);
1365 main_loop
= recv_only_or_dump
;
1367 ctx
.pcap
= PCAP_OPS_SG
;
1370 if (ctx
.device_out
&& device_mtu(ctx
.device_out
)) {
1371 register_signal_f(SIGALRM
, timer_elapsed
, SA_SIGINFO
);
1372 main_loop
= pcap_to_xmit
;
1374 ctx
.pcap
= PCAP_OPS_MMAP
;
1376 main_loop
= read_pcap
;
1378 ctx
.pcap
= PCAP_OPS_SG
;
1385 set_system_socket_memory(vals
);
1390 reset_system_socket_memory(vals
);
1395 free(ctx
.device_in
);
1396 free(ctx
.device_out
);
1397 free(ctx
.device_trans
);