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
; enum dump_mode dump_mode
;
65 uid_t uid
; gid_t gid
; uint32_t link_type
;
68 volatile sig_atomic_t sigint
= 0;
70 static volatile bool next_dump
= false;
72 static const char *short_options
= "d:i:o:rf:MJt:S:k:n:b:B:HQmcsqXlvhF:RGAP:Vu:g:";
73 static const struct option long_options
[] = {
74 {"dev", required_argument
, NULL
, 'd'},
75 {"in", required_argument
, NULL
, 'i'},
76 {"out", required_argument
, NULL
, 'o'},
77 {"filter", required_argument
, NULL
, 'f'},
78 {"num", required_argument
, NULL
, 'n'},
79 {"type", required_argument
, NULL
, 't'},
80 {"interval", required_argument
, NULL
, 'F'},
81 {"ring-size", required_argument
, NULL
, 'S'},
82 {"kernel-pull", required_argument
, NULL
, 'k'},
83 {"bind-cpu", required_argument
, NULL
, 'b'},
84 {"unbind-cpu", required_argument
, NULL
, 'B'},
85 {"prefix", required_argument
, NULL
, 'P'},
86 {"user", required_argument
, NULL
, 'u'},
87 {"group", required_argument
, NULL
, 'g'},
88 {"rand", no_argument
, NULL
, 'r'},
89 {"rfraw", no_argument
, NULL
, 'R'},
90 {"mmap", no_argument
, NULL
, 'm'},
91 {"sg", no_argument
, NULL
, 'G'},
92 {"clrw", no_argument
, NULL
, 'c'},
93 {"jumbo-support", no_argument
, NULL
, 'J'},
94 {"no-promisc", no_argument
, NULL
, 'M'},
95 {"prio-high", no_argument
, NULL
, 'H'},
96 {"notouch-irq", no_argument
, NULL
, 'Q'},
97 {"silent", no_argument
, NULL
, 's'},
98 {"less", no_argument
, NULL
, 'q'},
99 {"hex", no_argument
, NULL
, 'X'},
100 {"ascii", no_argument
, NULL
, 'l'},
101 {"no-sock-mem", no_argument
, NULL
, 'A'},
102 {"verbose", no_argument
, NULL
, 'V'},
103 {"version", no_argument
, NULL
, 'v'},
104 {"help", no_argument
, NULL
, 'h'},
110 static struct itimerval itimer
;
112 static unsigned long frame_count_max
= 0, interval
= TX_KERNEL_PULL_INT
;
114 #define set_system_socket_memory(vals) \
116 if ((vals[0] = get_system_socket_mem(sock_rmem_max)) < SMEM_SUG_MAX) \
117 set_system_socket_mem(sock_rmem_max, SMEM_SUG_MAX); \
118 if ((vals[1] = get_system_socket_mem(sock_rmem_def)) < SMEM_SUG_DEF) \
119 set_system_socket_mem(sock_rmem_def, SMEM_SUG_DEF); \
120 if ((vals[2] = get_system_socket_mem(sock_wmem_max)) < SMEM_SUG_MAX) \
121 set_system_socket_mem(sock_wmem_max, SMEM_SUG_MAX); \
122 if ((vals[3] = get_system_socket_mem(sock_wmem_def)) < SMEM_SUG_DEF) \
123 set_system_socket_mem(sock_wmem_def, SMEM_SUG_DEF); \
126 #define reset_system_socket_memory(vals) \
128 set_system_socket_mem(sock_rmem_max, vals[0]); \
129 set_system_socket_mem(sock_rmem_def, vals[1]); \
130 set_system_socket_mem(sock_wmem_max, vals[2]); \
131 set_system_socket_mem(sock_wmem_def, vals[3]); \
134 #define __pcap_io pcap_ops[ctx->pcap]
136 static void signal_handler(int number
)
147 static void timer_elapsed(int unused
)
149 itimer
.it_interval
.tv_sec
= 0;
150 itimer
.it_interval
.tv_usec
= interval
;
152 itimer
.it_value
.tv_sec
= 0;
153 itimer
.it_value
.tv_usec
= interval
;
155 pull_and_flush_tx_ring(tx_sock
);
156 setitimer(ITIMER_REAL
, &itimer
, NULL
);
159 static void timer_next_dump(int unused
)
161 itimer
.it_interval
.tv_sec
= interval
;
162 itimer
.it_interval
.tv_usec
= 0;
164 itimer
.it_value
.tv_sec
= interval
;
165 itimer
.it_value
.tv_usec
= 0;
168 setitimer(ITIMER_REAL
, &itimer
, NULL
);
171 static inline bool dump_to_pcap(struct ctx
*ctx
)
176 static void pcap_to_xmit(struct ctx
*ctx
)
180 int irq
, ifindex
, fd
= 0, ret
;
181 unsigned int size
, it
= 0;
182 unsigned long trunced
= 0;
184 struct frame_map
*hdr
;
185 struct sock_fprog bpf_ops
;
186 struct timeval start
, end
, diff
;
187 struct pcap_pkthdr phdr
;
189 if (!device_up_and_running(ctx
->device_out
) && !ctx
->rfraw
)
190 panic("Device not up and running!\n");
194 tx_sock
= pf_socket();
196 fd
= open_or_die(ctx
->device_in
, O_RDONLY
| O_LARGEFILE
| O_NOATIME
);
198 ret
= __pcap_io
->pull_file_header(fd
, &ctx
->link_type
);
200 panic("Error reading pcap header!\n");
202 if (__pcap_io
->prepare_reading_pcap
) {
203 ret
= __pcap_io
->prepare_reading_pcap(fd
);
205 panic("Error prepare reading pcap!\n");
208 fmemset(&tx_ring
, 0, sizeof(tx_ring
));
209 fmemset(&bpf_ops
, 0, sizeof(bpf_ops
));
212 ctx
->device_trans
= xstrdup(ctx
->device_out
);
213 xfree(ctx
->device_out
);
215 enter_rfmon_mac80211(ctx
->device_trans
, &ctx
->device_out
);
216 if (ctx
->link_type
!= LINKTYPE_IEEE802_11
)
217 panic("Wrong linktype of pcap!\n");
220 ifindex
= device_ifindex(ctx
->device_out
);
222 size
= ring_size(ctx
->device_out
, ctx
->reserve_size
);
224 bpf_parse_rules(ctx
->device_out
, ctx
->filter
, &bpf_ops
);
226 set_packet_loss_discard(tx_sock
);
227 set_sockopt_hwtimestamp(tx_sock
, ctx
->device_out
);
229 setup_tx_ring_layout(tx_sock
, &tx_ring
, size
, ctx
->jumbo_support
);
230 create_tx_ring(tx_sock
, &tx_ring
, ctx
->verbose
);
231 mmap_tx_ring(tx_sock
, &tx_ring
);
232 alloc_tx_ring_frames(&tx_ring
);
233 bind_tx_ring(tx_sock
, &tx_ring
, ifindex
);
235 dissector_init_all(ctx
->print_mode
);
237 if (ctx
->cpu
>= 0 && ifindex
> 0) {
238 irq
= device_irq_number(ctx
->device_out
);
239 device_bind_irq_to_cpu(irq
, ctx
->cpu
);
242 printf("IRQ: %s:%d > CPU%d\n",
243 ctx
->device_out
, irq
, ctx
->cpu
);
247 interval
= ctx
->kpull
;
251 bpf_dump_all(&bpf_ops
);
253 printf("MD: TX %luus %s ", interval
, pcap_ops
[ctx
->pcap
]->name
);
255 printf("802.11 raw via %s ", ctx
->device_out
);
256 #ifdef _LARGEFILE64_SOURCE
263 itimer
.it_interval
.tv_sec
= 0;
264 itimer
.it_interval
.tv_usec
= interval
;
266 itimer
.it_value
.tv_sec
= 0;
267 itimer
.it_value
.tv_usec
= interval
;
269 setitimer(ITIMER_REAL
, &itimer
, NULL
);
271 drop_privileges(ctx
->enforce
, ctx
->uid
, ctx
->gid
);
273 printf("Running! Hang up with ^C!\n\n");
276 bug_on(gettimeofday(&start
, NULL
));
278 while (likely(sigint
== 0)) {
279 while (user_may_pull_from_tx(tx_ring
.frames
[it
].iov_base
)) {
280 hdr
= tx_ring
.frames
[it
].iov_base
;
282 /* Kernel assumes: data = ph.raw + po->tp_hdrlen -
283 * sizeof(struct sockaddr_ll); */
284 out
= ((uint8_t *) hdr
) + TPACKET2_HDRLEN
- sizeof(struct sockaddr_ll
);
287 ret
= __pcap_io
->read_pcap_pkt(fd
, &phdr
, out
,
288 ring_frame_size(&tx_ring
));
289 if (unlikely(ret
<= 0))
292 if (ring_frame_size(&tx_ring
) < phdr
.len
) {
293 phdr
.len
= ring_frame_size(&tx_ring
);
296 } while (ctx
->filter
&& !bpf_run_filter(&bpf_ops
, out
, phdr
.len
));
298 pcap_pkthdr_to_tpacket_hdr(&phdr
, &hdr
->tp_h
);
300 ctx
->tx_bytes
+= hdr
->tp_h
.tp_len
;;
303 show_frame_hdr(hdr
, ctx
->print_mode
, RING_MODE_EGRESS
);
305 dissector_entry_point(out
, hdr
->tp_h
.tp_snaplen
,
306 ctx
->link_type
, ctx
->print_mode
);
308 kernel_may_pull_from_tx(&hdr
->tp_h
);
311 if (it
>= tx_ring
.layout
.tp_frame_nr
)
314 if (unlikely(sigint
== 1))
317 if (frame_count_max
!= 0) {
318 if (ctx
->tx_packets
>= frame_count_max
) {
328 bug_on(gettimeofday(&end
, NULL
));
329 diff
= tv_subtract(end
, start
);
331 bpf_release(&bpf_ops
);
333 dissector_cleanup_all();
334 destroy_tx_ring(tx_sock
, &tx_ring
);
337 leave_rfmon_mac80211(ctx
->device_trans
, ctx
->device_out
);
339 if (__pcap_io
->prepare_close_pcap
)
340 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_READ
);
347 printf("\r%12lu packets outgoing\n", ctx
->tx_packets
);
348 printf("\r%12lu packets truncated in file\n", trunced
);
349 printf("\r%12lu bytes outgoing\n", ctx
->tx_bytes
);
350 printf("\r%12lu sec, %lu usec in total\n", diff
.tv_sec
, diff
.tv_usec
);
353 static void receive_to_xmit(struct ctx
*ctx
)
357 int rx_sock
, ifindex_in
, ifindex_out
;
358 unsigned int size_in
, size_out
, it_in
= 0, it_out
= 0;
359 unsigned long frame_count
= 0;
360 struct frame_map
*hdr_in
, *hdr_out
;
361 struct ring tx_ring
, rx_ring
;
362 struct pollfd rx_poll
;
363 struct sock_fprog bpf_ops
;
365 if (!strncmp(ctx
->device_in
, ctx
->device_out
, IFNAMSIZ
))
366 panic("Ingress/egress devices must be different!\n");
367 if (!device_up_and_running(ctx
->device_out
))
368 panic("Egress device not up and running!\n");
369 if (!device_up_and_running(ctx
->device_in
))
370 panic("Ingress device not up and running!\n");
372 rx_sock
= pf_socket();
373 tx_sock
= pf_socket();
375 fmemset(&tx_ring
, 0, sizeof(tx_ring
));
376 fmemset(&rx_ring
, 0, sizeof(rx_ring
));
377 fmemset(&rx_poll
, 0, sizeof(rx_poll
));
378 fmemset(&bpf_ops
, 0, sizeof(bpf_ops
));
380 ifindex_in
= device_ifindex(ctx
->device_in
);
381 ifindex_out
= device_ifindex(ctx
->device_out
);
383 size_in
= ring_size(ctx
->device_in
, ctx
->reserve_size
);
384 size_out
= ring_size(ctx
->device_out
, ctx
->reserve_size
);
386 enable_kernel_bpf_jit_compiler();
388 bpf_parse_rules(ctx
->device_in
, ctx
->filter
, &bpf_ops
);
389 bpf_attach_to_sock(rx_sock
, &bpf_ops
);
391 setup_rx_ring_layout(rx_sock
, &rx_ring
, size_in
, ctx
->jumbo_support
);
392 create_rx_ring(rx_sock
, &rx_ring
, ctx
->verbose
);
393 mmap_rx_ring(rx_sock
, &rx_ring
);
394 alloc_rx_ring_frames(&rx_ring
);
395 bind_rx_ring(rx_sock
, &rx_ring
, ifindex_in
);
396 prepare_polling(rx_sock
, &rx_poll
);
398 set_packet_loss_discard(tx_sock
);
399 setup_tx_ring_layout(tx_sock
, &tx_ring
, size_out
, ctx
->jumbo_support
);
400 create_tx_ring(tx_sock
, &tx_ring
, ctx
->verbose
);
401 mmap_tx_ring(tx_sock
, &tx_ring
);
402 alloc_tx_ring_frames(&tx_ring
);
403 bind_tx_ring(tx_sock
, &tx_ring
, ifindex_out
);
405 dissector_init_all(ctx
->print_mode
);
407 if (ctx
->promiscuous
)
408 ifflags
= enter_promiscuous_mode(ctx
->device_in
);
411 interval
= ctx
->kpull
;
413 itimer
.it_interval
.tv_sec
= 0;
414 itimer
.it_interval
.tv_usec
= interval
;
416 itimer
.it_value
.tv_sec
= 0;
417 itimer
.it_value
.tv_usec
= interval
;
419 setitimer(ITIMER_REAL
, &itimer
, NULL
);
423 bpf_dump_all(&bpf_ops
);
425 printf("MD: RXTX %luus\n\n", interval
);
428 drop_privileges(ctx
->enforce
, ctx
->uid
, ctx
->gid
);
430 printf("Running! Hang up with ^C!\n\n");
433 while (likely(sigint
== 0)) {
434 while (user_may_pull_from_rx(rx_ring
.frames
[it_in
].iov_base
)) {
437 hdr_in
= rx_ring
.frames
[it_in
].iov_base
;
438 in
= ((uint8_t *) hdr_in
) + hdr_in
->tp_h
.tp_mac
;
442 if (ctx
->packet_type
!= -1)
443 if (ctx
->packet_type
!= hdr_in
->s_ll
.sll_pkttype
)
446 hdr_out
= tx_ring
.frames
[it_out
].iov_base
;
447 out
= ((uint8_t *) hdr_out
) + TPACKET2_HDRLEN
- sizeof(struct sockaddr_ll
);
449 for (; !user_may_pull_from_tx(tx_ring
.frames
[it_out
].iov_base
) &&
452 next_rnd_slot(&it_out
, &tx_ring
);
455 if (it_out
>= tx_ring
.layout
.tp_frame_nr
)
459 hdr_out
= tx_ring
.frames
[it_out
].iov_base
;
460 out
= ((uint8_t *) hdr_out
) + TPACKET2_HDRLEN
- sizeof(struct sockaddr_ll
);
463 tpacket_hdr_clone(&hdr_out
->tp_h
, &hdr_in
->tp_h
);
464 fmemcpy(out
, in
, hdr_in
->tp_h
.tp_len
);
466 kernel_may_pull_from_tx(&hdr_out
->tp_h
);
468 next_rnd_slot(&it_out
, &tx_ring
);
471 if (it_out
>= tx_ring
.layout
.tp_frame_nr
)
475 show_frame_hdr(hdr_in
, ctx
->print_mode
, RING_MODE_INGRESS
);
477 dissector_entry_point(in
, hdr_in
->tp_h
.tp_snaplen
,
478 ctx
->link_type
, ctx
->print_mode
);
480 if (frame_count_max
!= 0) {
481 if (frame_count
>= frame_count_max
) {
489 kernel_may_pull_from_rx(&hdr_in
->tp_h
);
492 if (it_in
>= rx_ring
.layout
.tp_frame_nr
)
495 if (unlikely(sigint
== 1))
499 poll(&rx_poll
, 1, -1);
500 poll_error_maybe_die(rx_sock
, &rx_poll
);
505 sock_print_net_stats(rx_sock
, 0);
507 bpf_release(&bpf_ops
);
509 dissector_cleanup_all();
511 destroy_tx_ring(tx_sock
, &tx_ring
);
512 destroy_rx_ring(rx_sock
, &rx_ring
);
514 if (ctx
->promiscuous
)
515 leave_promiscuous_mode(ctx
->device_in
, ifflags
);
521 static void translate_pcap_to_txf(int fdo
, uint8_t *out
, size_t len
)
523 size_t bytes_done
= 0;
526 slprintf(bout
, sizeof(bout
), "{\n ");
527 write_or_die(fdo
, bout
, strlen(bout
));
529 while (bytes_done
< len
) {
530 slprintf(bout
, sizeof(bout
), "0x%02x, ", out
[bytes_done
]);
531 write_or_die(fdo
, bout
, strlen(bout
));
535 if (bytes_done
% 10 == 0) {
536 slprintf(bout
, sizeof(bout
), "\n");
537 write_or_die(fdo
, bout
, strlen(bout
));
539 if (bytes_done
< len
) {
540 slprintf(bout
, sizeof(bout
), " ");
541 write_or_die(fdo
, bout
, strlen(bout
));
545 if (bytes_done
% 10 != 0) {
546 slprintf(bout
, sizeof(bout
), "\n");
547 write_or_die(fdo
, bout
, strlen(bout
));
550 slprintf(bout
, sizeof(bout
), "}\n\n");
551 write_or_die(fdo
, bout
, strlen(bout
));
554 static void read_pcap(struct ctx
*ctx
)
558 int ret
, fd
, fdo
= 0;
559 unsigned long trunced
= 0;
561 struct pcap_pkthdr phdr
;
562 struct sock_fprog bpf_ops
;
564 struct timeval start
, end
, diff
;
568 fd
= open_or_die(ctx
->device_in
, O_RDONLY
| O_LARGEFILE
| O_NOATIME
);
570 ret
= __pcap_io
->pull_file_header(fd
, &ctx
->link_type
);
572 panic("Error reading pcap header!\n");
574 if (__pcap_io
->prepare_reading_pcap
) {
575 ret
= __pcap_io
->prepare_reading_pcap(fd
);
577 panic("Error prepare reading pcap!\n");
580 fmemset(&fm
, 0, sizeof(fm
));
581 fmemset(&bpf_ops
, 0, sizeof(bpf_ops
));
583 bpf_parse_rules("any", ctx
->filter
, &bpf_ops
);
585 dissector_init_all(ctx
->print_mode
);
587 out_len
= round_up(1024 * 1024, PAGE_SIZE
);
588 out
= xmalloc_aligned(out_len
, CO_CACHE_LINE_SIZE
);
592 bpf_dump_all(&bpf_ops
);
594 printf("MD: RD %s ", __pcap_io
->name
);
595 #ifdef _LARGEFILE64_SOURCE
603 fdo
= open_or_die_m(ctx
->device_out
, O_RDWR
| O_CREAT
|
604 O_TRUNC
| O_LARGEFILE
, DEFFILEMODE
);
606 drop_privileges(ctx
->enforce
, ctx
->uid
, ctx
->gid
);
608 printf("Running! Hang up with ^C!\n\n");
611 bug_on(gettimeofday(&start
, NULL
));
613 while (likely(sigint
== 0)) {
615 ret
= __pcap_io
->read_pcap_pkt(fd
, &phdr
, out
, out_len
);
616 if (unlikely(ret
< 0))
619 if (unlikely(phdr
.len
== 0)) {
624 if (unlikely(phdr
.len
> out_len
)) {
628 } while (ctx
->filter
&& !bpf_run_filter(&bpf_ops
, out
, phdr
.len
));
630 pcap_pkthdr_to_tpacket_hdr(&phdr
, &fm
.tp_h
);
632 ctx
->tx_bytes
+= fm
.tp_h
.tp_len
;
635 show_frame_hdr(&fm
, ctx
->print_mode
, RING_MODE_EGRESS
);
637 dissector_entry_point(out
, fm
.tp_h
.tp_snaplen
,
638 ctx
->link_type
, ctx
->print_mode
);
641 translate_pcap_to_txf(fdo
, out
, fm
.tp_h
.tp_snaplen
);
643 if (frame_count_max
!= 0) {
644 if (ctx
->tx_packets
>= frame_count_max
) {
653 bug_on(gettimeofday(&end
, NULL
));
654 diff
= tv_subtract(end
, start
);
656 bpf_release(&bpf_ops
);
658 dissector_cleanup_all();
660 if (__pcap_io
->prepare_close_pcap
)
661 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_READ
);
671 printf("\r%12lu packets outgoing\n", ctx
->tx_packets
);
672 printf("\r%12lu packets truncated in file\n", trunced
);
673 printf("\r%12lu bytes outgoing\n", ctx
->tx_bytes
);
674 printf("\r%12lu sec, %lu usec in total\n", diff
.tv_sec
, diff
.tv_usec
);
677 static void finish_multi_pcap_file(struct ctx
*ctx
, int fd
)
679 __pcap_io
->fsync_pcap(fd
);
681 if (__pcap_io
->prepare_close_pcap
)
682 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_WRITE
);
686 fmemset(&itimer
, 0, sizeof(itimer
));
687 setitimer(ITIMER_REAL
, &itimer
, NULL
);
690 static int next_multi_pcap_file(struct ctx
*ctx
, int fd
)
695 __pcap_io
->fsync_pcap(fd
);
697 if (__pcap_io
->prepare_close_pcap
)
698 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_WRITE
);
702 slprintf(fname
, sizeof(fname
), "%s/%s%lu.pcap", ctx
->device_out
,
703 ctx
->prefix
? : "dump-", time(0));
705 fd
= open_or_die_m(fname
, O_RDWR
| O_CREAT
| O_TRUNC
|
706 O_LARGEFILE
, DEFFILEMODE
);
708 ret
= __pcap_io
->push_file_header(fd
, ctx
->link_type
);
710 panic("Error writing pcap header!\n");
712 if (__pcap_io
->prepare_writing_pcap
) {
713 ret
= __pcap_io
->prepare_writing_pcap(fd
);
715 panic("Error prepare writing pcap!\n");
721 static int begin_multi_pcap_file(struct ctx
*ctx
)
728 if (ctx
->device_out
[strlen(ctx
->device_out
) - 1] == '/')
729 ctx
->device_out
[strlen(ctx
->device_out
) - 1] = 0;
731 slprintf(fname
, sizeof(fname
), "%s/%s%lu.pcap", ctx
->device_out
,
732 ctx
->prefix
? : "dump-", time(0));
734 fd
= open_or_die_m(fname
, O_RDWR
| O_CREAT
| O_TRUNC
|
735 O_LARGEFILE
, DEFFILEMODE
);
737 ret
= __pcap_io
->push_file_header(fd
, ctx
->link_type
);
739 panic("Error writing pcap header!\n");
741 if (__pcap_io
->prepare_writing_pcap
) {
742 ret
= __pcap_io
->prepare_writing_pcap(fd
);
744 panic("Error prepare writing pcap!\n");
747 if (ctx
->dump_mode
== DUMP_INTERVAL_TIME
) {
748 interval
= ctx
->dump_interval
;
750 itimer
.it_interval
.tv_sec
= interval
;
751 itimer
.it_interval
.tv_usec
= 0;
753 itimer
.it_value
.tv_sec
= interval
;
754 itimer
.it_value
.tv_usec
= 0;
756 setitimer(ITIMER_REAL
, &itimer
, NULL
);
764 static void finish_single_pcap_file(struct ctx
*ctx
, int fd
)
766 __pcap_io
->fsync_pcap(fd
);
768 if (__pcap_io
->prepare_close_pcap
)
769 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_WRITE
);
774 static int begin_single_pcap_file(struct ctx
*ctx
)
780 fd
= open_or_die_m(ctx
->device_out
, O_RDWR
| O_CREAT
| O_TRUNC
|
781 O_LARGEFILE
, DEFFILEMODE
);
783 ret
= __pcap_io
->push_file_header(fd
, ctx
->link_type
);
785 panic("Error writing pcap header!\n");
787 if (__pcap_io
->prepare_writing_pcap
) {
788 ret
= __pcap_io
->prepare_writing_pcap(fd
);
790 panic("Error prepare writing pcap!\n");
796 static void print_pcap_file_stats(int sock
, struct ctx
*ctx
, unsigned long skipped
)
798 unsigned long good
, bad
;
799 struct tpacket_stats kstats
;
800 socklen_t slen
= sizeof(kstats
);
802 fmemset(&kstats
, 0, sizeof(kstats
));
803 getsockopt(sock
, SOL_PACKET
, PACKET_STATISTICS
, &kstats
, &slen
);
805 if (ctx
->print_mode
== PRINT_NONE
) {
806 good
= kstats
.tp_packets
- kstats
.tp_drops
- skipped
;
807 bad
= kstats
.tp_drops
+ skipped
;
809 printf(".(+%lu/-%lu)", good
, bad
);
814 static void recv_only_or_dump(struct ctx
*ctx
)
818 int sock
, irq
, ifindex
, fd
= 0, ret
;
819 unsigned int size
, it
= 0;
820 unsigned long frame_count
= 0, skipped
= 0;
822 struct pollfd rx_poll
;
823 struct frame_map
*hdr
;
824 struct sock_fprog bpf_ops
;
825 struct timeval start
, end
, diff
;
826 struct pcap_pkthdr phdr
;
828 if (!device_up_and_running(ctx
->device_in
) && !ctx
->rfraw
)
829 panic("Device not up and running!\n");
834 ctx
->device_trans
= xstrdup(ctx
->device_in
);
835 xfree(ctx
->device_in
);
837 enter_rfmon_mac80211(ctx
->device_trans
, &ctx
->device_in
);
838 ctx
->link_type
= LINKTYPE_IEEE802_11
;
841 fmemset(&rx_ring
, 0, sizeof(rx_ring
));
842 fmemset(&rx_poll
, 0, sizeof(rx_poll
));
843 fmemset(&bpf_ops
, 0, sizeof(bpf_ops
));
845 ifindex
= device_ifindex(ctx
->device_in
);
847 size
= ring_size(ctx
->device_in
, ctx
->reserve_size
);
849 enable_kernel_bpf_jit_compiler();
851 bpf_parse_rules(ctx
->device_in
, ctx
->filter
, &bpf_ops
);
852 bpf_attach_to_sock(sock
, &bpf_ops
);
854 set_sockopt_hwtimestamp(sock
, ctx
->device_in
);
856 setup_rx_ring_layout(sock
, &rx_ring
, size
, ctx
->jumbo_support
);
857 create_rx_ring(sock
, &rx_ring
, ctx
->verbose
);
858 mmap_rx_ring(sock
, &rx_ring
);
859 alloc_rx_ring_frames(&rx_ring
);
860 bind_rx_ring(sock
, &rx_ring
, ifindex
);
862 prepare_polling(sock
, &rx_poll
);
863 dissector_init_all(ctx
->print_mode
);
865 if (ctx
->cpu
>= 0 && ifindex
> 0) {
866 irq
= device_irq_number(ctx
->device_in
);
867 device_bind_irq_to_cpu(irq
, ctx
->cpu
);
870 printf("IRQ: %s:%d > CPU%d\n",
871 ctx
->device_in
, irq
, ctx
->cpu
);
874 if (ctx
->promiscuous
)
875 ifflags
= enter_promiscuous_mode(ctx
->device_in
);
879 bpf_dump_all(&bpf_ops
);
881 printf("MD: RX %s ", ctx
->dump
? pcap_ops
[ctx
->pcap
]->name
: "");
883 printf("802.11 raw via %s ", ctx
->device_in
);
884 #ifdef _LARGEFILE64_SOURCE
891 drop_privileges(ctx
->enforce
, ctx
->uid
, ctx
->gid
);
893 if (dump_to_pcap(ctx
)) {
897 fmemset(&stats
, 0, sizeof(stats
));
898 ret
= stat(ctx
->device_out
, &stats
);
904 ctx
->dump_dir
= S_ISDIR(stats
.st_mode
);
906 fd
= begin_multi_pcap_file(ctx
);
909 fd
= begin_single_pcap_file(ctx
);
913 printf("Running! Hang up with ^C!\n\n");
916 bug_on(gettimeofday(&start
, NULL
));
918 while (likely(sigint
== 0)) {
919 while (user_may_pull_from_rx(rx_ring
.frames
[it
].iov_base
)) {
922 hdr
= rx_ring
.frames
[it
].iov_base
;
923 packet
= ((uint8_t *) hdr
) + hdr
->tp_h
.tp_mac
;
926 if (ctx
->packet_type
!= -1)
927 if (ctx
->packet_type
!= hdr
->s_ll
.sll_pkttype
)
930 if (unlikely(ring_frame_size(&rx_ring
) < hdr
->tp_h
.tp_snaplen
)) {
935 if (dump_to_pcap(ctx
)) {
936 tpacket_hdr_to_pcap_pkthdr(&hdr
->tp_h
, &phdr
);
938 ret
= __pcap_io
->write_pcap_pkt(fd
, &phdr
, packet
, phdr
.len
);
939 if (unlikely(ret
!= sizeof(phdr
) + phdr
.len
))
940 panic("Write error to pcap!\n");
943 show_frame_hdr(hdr
, ctx
->print_mode
, RING_MODE_INGRESS
);
945 dissector_entry_point(packet
, hdr
->tp_h
.tp_snaplen
,
946 ctx
->link_type
, ctx
->print_mode
);
948 if (frame_count_max
!= 0) {
949 if (frame_count
>= frame_count_max
) {
957 kernel_may_pull_from_rx(&hdr
->tp_h
);
960 if (it
>= rx_ring
.layout
.tp_frame_nr
)
963 if (unlikely(sigint
== 1))
966 if (dump_to_pcap(ctx
)) {
967 if (ctx
->dump_mode
== DUMP_INTERVAL_SIZE
) {
968 interval
+= hdr
->tp_h
.tp_snaplen
;
970 if (interval
> ctx
->dump_interval
) {
977 fd
= next_multi_pcap_file(ctx
, fd
);
981 print_pcap_file_stats(sock
, ctx
, skipped
);
986 poll(&rx_poll
, 1, -1);
987 poll_error_maybe_die(sock
, &rx_poll
);
990 bug_on(gettimeofday(&end
, NULL
));
991 diff
= tv_subtract(end
, start
);
993 if (dump_to_pcap(ctx
)) {
995 finish_multi_pcap_file(ctx
, fd
);
997 finish_single_pcap_file(ctx
, fd
);
1000 if (!(ctx
->dump_dir
&& ctx
->print_mode
== PRINT_NONE
)) {
1001 sock_print_net_stats(sock
, skipped
);
1003 printf("\r%12lu sec, %lu usec in total\n",
1004 diff
.tv_sec
, diff
.tv_usec
);
1010 bpf_release(&bpf_ops
);
1011 dissector_cleanup_all();
1012 destroy_rx_ring(sock
, &rx_ring
);
1014 if (ctx
->promiscuous
)
1015 leave_promiscuous_mode(ctx
->device_in
, ifflags
);
1018 leave_rfmon_mac80211(ctx
->device_trans
, ctx
->device_in
);
1023 static void help(void)
1025 printf("\nnetsniff-ng %s, the packet sniffing beast\n", VERSION_STRING
);
1026 puts("http://www.netsniff-ng.org\n\n"
1027 "Usage: netsniff-ng [options] [filter-expression]\n"
1029 " -i|-d|--dev|--in <dev|pcap> Input source as netdev or pcap\n"
1030 " -o|--out <dev|pcap|dir|cfg> Output sink as netdev, pcap, directory, trafgen file\n"
1031 " -f|--filter <bpf-file|expr> Use BPF filter file from bpfc or tcpdump-like expression\n"
1032 " -t|--type <type> Only handle packets of defined type:\n"
1033 " host|broadcast|multicast|others|outgoing\n"
1034 " -F|--interval <size/time> Dump interval in time or size if -o is a directory\n"
1035 " pcap swap spec: <num>KiB/MiB/GiB/s/sec/min/hrs\n"
1036 " -J|--jumbo-support Support for 64KB Super Jumbo Frames\n"
1037 " Default RX/TX slot: 2048Byte\n"
1038 " -R|--rfraw Capture or inject raw 802.11 frames\n"
1039 " -n|--num <uint> Number of packets until exit\n"
1040 " `-- 0 Loop until interrupted (default)\n"
1041 " `- n Send n packets and done\n"
1042 "Options for printing:\n"
1043 " -s|--silent Do not print captured packets\n"
1044 " -q|--less Print less-verbose packet information\n"
1045 " -X|--hex Print packet data in hex format\n"
1046 " -l|--ascii Print human-readable packet data\n"
1047 "Options, advanced:\n"
1048 " -P|--prefix <name> Prefix for pcaps stored in directory\n"
1049 " -r|--rand Randomize packet forwarding order\n"
1050 " -M|--no-promisc No promiscuous mode for netdev\n"
1051 " -A|--no-sock-mem Don't tune core socket memory\n"
1052 " -m|--mmap Mmap pcap file i.e., for replaying\n"
1053 " -G|--sg Scatter/gather pcap file I/O\n"
1054 " -c|--clrw Use slower read(2)/write(2) I/O\n"
1055 " -S|--ring-size <size> Manually set ring size to <size>:\n"
1056 " mmap space in KiB/MiB/GiB, e.g. \'10MiB\'\n"
1057 " -k|--kernel-pull <uint> Kernel pull from user interval in us\n"
1058 " Default is 10us where the TX_RING\n"
1059 " is populated with payload from uspace\n"
1060 " -b|--bind-cpu <cpu> Bind to specific CPU (or CPU-range)\n"
1061 " -B|--unbind-cpu <cpu> Forbid to use specific CPU (or CPU-range)\n"
1062 " -u|--user <userid> Drop privileges and change to userid\n"
1063 " -g|--group <groupid> Drop privileges and change to groupid\n"
1064 " -H|--prio-high Make this high priority process\n"
1065 " -Q|--notouch-irq Do not touch IRQ CPU affinity of NIC\n"
1066 " -V|--verbose Be more verbose\n"
1067 " -v|--version Show version\n"
1068 " -h|--help Guess what?!\n\n"
1070 " netsniff-ng --in eth0 --out dump.pcap --silent --bind-cpu 0\n"
1071 " netsniff-ng --in wlan0 --rfraw --out dump.pcap --silent --bind-cpu 0\n"
1072 " netsniff-ng --in dump.pcap --mmap --out eth0 -k1000 --silent --bind-cpu 0\n"
1073 " netsniff-ng --in dump.pcap --out dump.cfg --silent --bind-cpu 0\n"
1074 " netsniff-ng --in eth0 --out eth1 --silent --bind-cpu 0 --type host\n"
1075 " netsniff-ng --in eth1 --out /opt/probe/ -s -m -J --interval 100MiB -b 0\n"
1076 " netsniff-ng --in vlan0 --out dump.pcap -c -u `id -u bob` -g `id -g bob`\n"
1077 " netsniff-ng --in any --filter http.bpf --jumbo-support --ascii -V\n\n"
1078 "Please report bugs to <bugs@netsniff-ng.org>\n"
1079 "Copyright (C) 2009-2013 Daniel Borkmann <daniel@netsniff-ng.org>\n"
1080 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel@netsniff-ng.org>\n"
1081 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1082 "License: GNU GPL version 2.0\n"
1083 "This is free software: you are free to change and redistribute it.\n"
1084 "There is NO WARRANTY, to the extent permitted by law.\n");
1088 static void version(void)
1090 printf("\nnetsniff-ng %s, the packet sniffing beast\n", VERSION_STRING
);
1091 puts("http://www.netsniff-ng.org\n\n"
1092 "Please report bugs to <bugs@netsniff-ng.org>\n"
1093 "Copyright (C) 2009-2013 Daniel Borkmann <daniel@netsniff-ng.org>\n"
1094 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel@netsniff-ng.org>\n"
1095 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1096 "License: GNU GPL version 2.0\n"
1097 "This is free software: you are free to change and redistribute it.\n"
1098 "There is NO WARRANTY, to the extent permitted by law.\n");
1102 static void header(void)
1104 printf("%s%s%s\n", colorize_start(bold
), "netsniff-ng " VERSION_STRING
, colorize_end());
1107 int main(int argc
, char **argv
)
1110 int c
, i
, j
, opt_index
, ops_touched
= 0, vals
[4] = {0};
1111 bool prio_high
= false, setsockmem
= true;
1112 void (*main_loop
)(struct ctx
*ctx
) = NULL
;
1114 .link_type
= LINKTYPE_EN10MB
,
1115 .print_mode
= PRINT_NORM
,
1118 .promiscuous
= true,
1120 .pcap
= PCAP_OPS_SG
,
1121 .dump_interval
= 60,
1122 .dump_mode
= DUMP_INTERVAL_TIME
,
1129 while ((c
= getopt_long(argc
, argv
, short_options
, long_options
,
1130 &opt_index
)) != EOF
) {
1134 ctx
.device_in
= xstrdup(optarg
);
1137 ctx
.device_out
= xstrdup(optarg
);
1140 ctx
.prefix
= xstrdup(optarg
);
1143 ctx
.link_type
= LINKTYPE_IEEE802_11
;
1147 ctx
.randomize
= true;
1150 ctx
.jumbo_support
= 1;
1153 ctx
.filter
= xstrdup(optarg
);
1156 ctx
.promiscuous
= false;
1162 ctx
.uid
= strtoul(optarg
, NULL
, 0);
1166 ctx
.gid
= strtoul(optarg
, NULL
, 0);
1170 if (!strncmp(optarg
, "host", strlen("host")))
1171 ctx
.packet_type
= PACKET_HOST
;
1172 else if (!strncmp(optarg
, "broadcast", strlen("broadcast")))
1173 ctx
.packet_type
= PACKET_BROADCAST
;
1174 else if (!strncmp(optarg
, "multicast", strlen("multicast")))
1175 ctx
.packet_type
= PACKET_MULTICAST
;
1176 else if (!strncmp(optarg
, "others", strlen("others")))
1177 ctx
.packet_type
= PACKET_OTHERHOST
;
1178 else if (!strncmp(optarg
, "outgoing", strlen("outgoing")))
1179 ctx
.packet_type
= PACKET_OUTGOING
;
1181 ctx
.packet_type
= -1;
1185 ctx
.reserve_size
= 0;
1187 for (j
= i
= strlen(optarg
); i
> 0; --i
) {
1188 if (!isdigit(optarg
[j
- i
]))
1193 if (!strncmp(ptr
, "KiB", strlen("KiB")))
1194 ctx
.reserve_size
= 1 << 10;
1195 else if (!strncmp(ptr
, "MiB", strlen("MiB")))
1196 ctx
.reserve_size
= 1 << 20;
1197 else if (!strncmp(ptr
, "GiB", strlen("GiB")))
1198 ctx
.reserve_size
= 1 << 30;
1200 panic("Syntax error in ring size param!\n");
1203 ctx
.reserve_size
*= strtol(optarg
, NULL
, 0);
1206 set_cpu_affinity(optarg
, 0);
1207 /* Take the first CPU for rebinding the IRQ */
1209 ctx
.cpu
= strtol(optarg
, NULL
, 0);
1212 set_cpu_affinity(optarg
, 1);
1218 ctx
.pcap
= PCAP_OPS_RW
;
1222 ctx
.pcap
= PCAP_OPS_MMAP
;
1226 ctx
.pcap
= PCAP_OPS_SG
;
1233 ctx
.print_mode
= PRINT_NONE
;
1236 ctx
.print_mode
= PRINT_LESS
;
1240 (ctx
.print_mode
== PRINT_ASCII
) ?
1241 PRINT_HEX_ASCII
: PRINT_HEX
;
1245 (ctx
.print_mode
== PRINT_HEX
) ?
1246 PRINT_HEX_ASCII
: PRINT_ASCII
;
1249 ctx
.kpull
= strtol(optarg
, NULL
, 0);
1252 frame_count_max
= strtol(optarg
, NULL
, 0);
1256 ctx
.dump_interval
= 0;
1258 for (j
= i
= strlen(optarg
); i
> 0; --i
) {
1259 if (!isdigit(optarg
[j
- i
]))
1264 if (!strncmp(ptr
, "KiB", strlen("KiB"))) {
1265 ctx
.dump_interval
= 1 << 10;
1266 ctx
.dump_mode
= DUMP_INTERVAL_SIZE
;
1267 } else if (!strncmp(ptr
, "MiB", strlen("MiB"))) {
1268 ctx
.dump_interval
= 1 << 20;
1269 ctx
.dump_mode
= DUMP_INTERVAL_SIZE
;
1270 } else if (!strncmp(ptr
, "GiB", strlen("GiB"))) {
1271 ctx
.dump_interval
= 1 << 30;
1272 ctx
.dump_mode
= DUMP_INTERVAL_SIZE
;
1273 } else if (!strncmp(ptr
, "sec", strlen("sec"))) {
1274 ctx
.dump_interval
= 1;
1275 ctx
.dump_mode
= DUMP_INTERVAL_TIME
;
1276 } else if (!strncmp(ptr
, "min", strlen("min"))) {
1277 ctx
.dump_interval
= 60;
1278 ctx
.dump_mode
= DUMP_INTERVAL_TIME
;
1279 } else if (!strncmp(ptr
, "hrs", strlen("hrs"))) {
1280 ctx
.dump_interval
= 60 * 60;
1281 ctx
.dump_mode
= DUMP_INTERVAL_TIME
;
1282 } else if (!strncmp(ptr
, "s", strlen("s"))) {
1283 ctx
.dump_interval
= 1;
1284 ctx
.dump_mode
= DUMP_INTERVAL_TIME
;
1286 panic("Syntax error in time/size param!\n");
1290 ctx
.dump_interval
*= strtol(optarg
, NULL
, 0);
1318 panic("Option -%c requires an argument!\n",
1321 if (isprint(optopt
))
1322 whine("Unknown option character "
1323 "`0x%X\'!\n", optopt
);
1331 if (!ctx
.filter
&& optind
!= argc
) {
1335 for (i
= optind
; i
< argc
; ++i
) {
1336 size_t alen
= strlen(argv
[i
]) + 2;
1337 size_t flen
= ctx
.filter
? strlen(ctx
.filter
) : 0;
1339 ctx
.filter
= xrealloc(ctx
.filter
, 1, flen
+ alen
);
1340 ret
= slprintf(ctx
.filter
+ offset
, strlen(argv
[i
]) + 2, "%s ", argv
[i
]);
1342 panic("Cannot concatenate filter string!\n");
1349 ctx
.device_in
= xstrdup("any");
1351 register_signal(SIGINT
, signal_handler
);
1352 register_signal(SIGHUP
, signal_handler
);
1359 set_proc_prio(get_default_proc_prio());
1360 set_sched_status(get_default_sched_policy(), get_default_sched_prio());
1363 if (ctx
.device_in
&& (device_mtu(ctx
.device_in
) ||
1364 !strncmp("any", ctx
.device_in
, strlen(ctx
.device_in
)))) {
1365 if (!ctx
.device_out
) {
1367 main_loop
= recv_only_or_dump
;
1368 } else if (device_mtu(ctx
.device_out
)) {
1369 register_signal_f(SIGALRM
, timer_elapsed
, SA_SIGINFO
);
1370 main_loop
= receive_to_xmit
;
1373 register_signal_f(SIGALRM
, timer_next_dump
, SA_SIGINFO
);
1374 main_loop
= recv_only_or_dump
;
1376 ctx
.pcap
= PCAP_OPS_SG
;
1379 if (ctx
.device_out
&& device_mtu(ctx
.device_out
)) {
1380 register_signal_f(SIGALRM
, timer_elapsed
, SA_SIGINFO
);
1381 main_loop
= pcap_to_xmit
;
1383 ctx
.pcap
= PCAP_OPS_MMAP
;
1385 main_loop
= read_pcap
;
1387 ctx
.pcap
= PCAP_OPS_SG
;
1393 init_pcap(ctx
.pcap
, ctx
.jumbo_support
);
1396 set_system_socket_memory(vals
);
1403 reset_system_socket_memory(vals
);
1408 free(ctx
.device_in
);
1409 free(ctx
.device_out
);
1410 free(ctx
.device_trans
);