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>
50 #include "dissector.h"
59 char *device_in
, *device_out
, *device_trans
, *filter
, *prefix
;
60 int cpu
, rfraw
, dump
, print_mode
, dump_dir
, jumbo_support
, packet_type
, verbose
;
61 unsigned long kpull
, dump_interval
, reserve_size
, tx_bytes
, tx_packets
;
62 bool randomize
, promiscuous
;
63 enum pcap_ops_groups pcap
;
64 enum dump_mode dump_mode
;
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:V";
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 {"rand", no_argument
, NULL
, 'r'},
87 {"rfraw", no_argument
, NULL
, 'R'},
88 {"mmap", no_argument
, NULL
, 'm'},
89 {"sg", no_argument
, NULL
, 'g'},
90 {"clrw", no_argument
, NULL
, 'c'},
91 {"jumbo-support", no_argument
, NULL
, 'J'},
92 {"no-promisc", no_argument
, NULL
, 'M'},
93 {"prio-high", no_argument
, NULL
, 'H'},
94 {"notouch-irq", no_argument
, NULL
, 'Q'},
95 {"silent", no_argument
, NULL
, 's'},
96 {"less", no_argument
, NULL
, 'q'},
97 {"hex", no_argument
, NULL
, 'X'},
98 {"ascii", no_argument
, NULL
, 'l'},
99 {"no-sock-mem", no_argument
, NULL
, 'A'},
100 {"verbose", no_argument
, NULL
, 'V'},
101 {"version", no_argument
, NULL
, 'v'},
102 {"help", no_argument
, NULL
, 'h'},
108 static struct itimerval itimer
;
110 static unsigned long frame_count_max
= 0, interval
= TX_KERNEL_PULL_INT
;
112 #define set_system_socket_memory(vals) \
114 if ((vals[0] = get_system_socket_mem(sock_rmem_max)) < SMEM_SUG_MAX) \
115 set_system_socket_mem(sock_rmem_max, SMEM_SUG_MAX); \
116 if ((vals[1] = get_system_socket_mem(sock_rmem_def)) < SMEM_SUG_DEF) \
117 set_system_socket_mem(sock_rmem_def, SMEM_SUG_DEF); \
118 if ((vals[2] = get_system_socket_mem(sock_wmem_max)) < SMEM_SUG_MAX) \
119 set_system_socket_mem(sock_wmem_max, SMEM_SUG_MAX); \
120 if ((vals[3] = get_system_socket_mem(sock_wmem_def)) < SMEM_SUG_DEF) \
121 set_system_socket_mem(sock_wmem_def, SMEM_SUG_DEF); \
124 #define reset_system_socket_memory(vals) \
126 set_system_socket_mem(sock_rmem_max, vals[0]); \
127 set_system_socket_mem(sock_rmem_def, vals[1]); \
128 set_system_socket_mem(sock_wmem_max, vals[2]); \
129 set_system_socket_mem(sock_wmem_def, vals[3]); \
132 #define __pcap_io pcap_ops[ctx->pcap]
134 static void signal_handler(int number
)
145 static void timer_elapsed(int unused
)
147 itimer
.it_interval
.tv_sec
= 0;
148 itimer
.it_interval
.tv_usec
= interval
;
150 itimer
.it_value
.tv_sec
= 0;
151 itimer
.it_value
.tv_usec
= interval
;
153 pull_and_flush_tx_ring(tx_sock
);
154 setitimer(ITIMER_REAL
, &itimer
, NULL
);
157 static void timer_next_dump(int unused
)
159 itimer
.it_interval
.tv_sec
= interval
;
160 itimer
.it_interval
.tv_usec
= 0;
162 itimer
.it_value
.tv_sec
= interval
;
163 itimer
.it_value
.tv_usec
= 0;
166 setitimer(ITIMER_REAL
, &itimer
, NULL
);
169 static inline bool dump_to_pcap(struct ctx
*ctx
)
174 static void pcap_to_xmit(struct ctx
*ctx
)
178 int irq
, ifindex
, fd
= 0, ret
;
179 unsigned int size
, it
= 0;
180 unsigned long trunced
= 0;
182 struct frame_map
*hdr
;
183 struct sock_fprog bpf_ops
;
184 struct timeval start
, end
, diff
;
185 struct pcap_pkthdr phdr
;
187 if (!device_up_and_running(ctx
->device_out
) && !ctx
->rfraw
)
188 panic("Device not up and running!\n");
192 tx_sock
= pf_socket();
194 fd
= open_or_die(ctx
->device_in
, O_RDONLY
| O_LARGEFILE
| O_NOATIME
);
196 ret
= __pcap_io
->pull_file_header(fd
, &ctx
->link_type
);
198 panic("Error reading pcap header!\n");
200 if (__pcap_io
->prepare_reading_pcap
) {
201 ret
= __pcap_io
->prepare_reading_pcap(fd
);
203 panic("Error prepare reading pcap!\n");
206 fmemset(&tx_ring
, 0, sizeof(tx_ring
));
207 fmemset(&bpf_ops
, 0, sizeof(bpf_ops
));
210 ctx
->device_trans
= xstrdup(ctx
->device_out
);
211 xfree(ctx
->device_out
);
213 enter_rfmon_mac80211(ctx
->device_trans
, &ctx
->device_out
);
214 if (ctx
->link_type
!= LINKTYPE_IEEE802_11
)
215 panic("Wrong linktype of pcap!\n");
218 ifindex
= device_ifindex(ctx
->device_out
);
220 size
= ring_size(ctx
->device_out
, ctx
->reserve_size
);
222 bpf_parse_rules(ctx
->filter
, &bpf_ops
);
224 set_packet_loss_discard(tx_sock
);
225 set_sockopt_hwtimestamp(tx_sock
, ctx
->device_out
);
227 setup_tx_ring_layout(tx_sock
, &tx_ring
, size
, ctx
->jumbo_support
);
228 create_tx_ring(tx_sock
, &tx_ring
, ctx
->verbose
);
229 mmap_tx_ring(tx_sock
, &tx_ring
);
230 alloc_tx_ring_frames(&tx_ring
);
231 bind_tx_ring(tx_sock
, &tx_ring
, ifindex
);
233 dissector_init_all(ctx
->print_mode
);
235 if (ctx
->cpu
>= 0 && ifindex
> 0) {
236 irq
= device_irq_number(ctx
->device_out
);
237 device_bind_irq_to_cpu(irq
, ctx
->cpu
);
240 printf("IRQ: %s:%d > CPU%d\n",
241 ctx
->device_out
, irq
, ctx
->cpu
);
245 interval
= ctx
->kpull
;
249 bpf_dump_all(&bpf_ops
);
251 printf("MD: TX %luus %s ", interval
, pcap_ops
[ctx
->pcap
]->name
);
253 printf("802.11 raw via %s ", ctx
->device_out
);
254 #ifdef _LARGEFILE64_SOURCE
260 printf("Running! Hang up with ^C!\n\n");
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 bug_on(gettimeofday(&start
, NULL
));
273 while (likely(sigint
== 0)) {
274 while (user_may_pull_from_tx(tx_ring
.frames
[it
].iov_base
)) {
275 hdr
= tx_ring
.frames
[it
].iov_base
;
277 /* Kernel assumes: data = ph.raw + po->tp_hdrlen -
278 * sizeof(struct sockaddr_ll); */
279 out
= ((uint8_t *) hdr
) + TPACKET2_HDRLEN
- sizeof(struct sockaddr_ll
);
282 ret
= __pcap_io
->read_pcap_pkt(fd
, &phdr
, out
,
283 ring_frame_size(&tx_ring
));
284 if (unlikely(ret
<= 0))
287 if (ring_frame_size(&tx_ring
) < phdr
.len
) {
288 phdr
.len
= ring_frame_size(&tx_ring
);
291 } while (ctx
->filter
&& !bpf_run_filter(&bpf_ops
, out
, phdr
.len
));
293 pcap_pkthdr_to_tpacket_hdr(&phdr
, &hdr
->tp_h
);
295 ctx
->tx_bytes
+= hdr
->tp_h
.tp_len
;;
298 show_frame_hdr(hdr
, ctx
->print_mode
, RING_MODE_EGRESS
);
300 dissector_entry_point(out
, hdr
->tp_h
.tp_snaplen
,
301 ctx
->link_type
, ctx
->print_mode
);
303 kernel_may_pull_from_tx(&hdr
->tp_h
);
306 if (it
>= tx_ring
.layout
.tp_frame_nr
)
309 if (unlikely(sigint
== 1))
312 if (frame_count_max
!= 0) {
313 if (ctx
->tx_packets
>= frame_count_max
) {
323 bug_on(gettimeofday(&end
, NULL
));
324 diff
= tv_subtract(end
, start
);
326 bpf_release(&bpf_ops
);
328 dissector_cleanup_all();
329 destroy_tx_ring(tx_sock
, &tx_ring
);
332 leave_rfmon_mac80211(ctx
->device_trans
, ctx
->device_out
);
334 if (__pcap_io
->prepare_close_pcap
)
335 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_READ
);
342 printf("\r%12lu packets outgoing\n", ctx
->tx_packets
);
343 printf("\r%12lu packets truncated in file\n", trunced
);
344 printf("\r%12lu bytes outgoing\n", ctx
->tx_bytes
);
345 printf("\r%12lu sec, %lu usec in total\n", diff
.tv_sec
, diff
.tv_usec
);
348 static void receive_to_xmit(struct ctx
*ctx
)
352 int rx_sock
, ifindex_in
, ifindex_out
;
353 unsigned int size_in
, size_out
, it_in
= 0, it_out
= 0;
354 unsigned long frame_count
= 0;
355 struct frame_map
*hdr_in
, *hdr_out
;
356 struct ring tx_ring
, rx_ring
;
357 struct pollfd rx_poll
;
358 struct sock_fprog bpf_ops
;
360 if (!strncmp(ctx
->device_in
, ctx
->device_out
, IFNAMSIZ
))
361 panic("Ingress/egress devices must be different!\n");
362 if (!device_up_and_running(ctx
->device_out
))
363 panic("Egress device not up and running!\n");
364 if (!device_up_and_running(ctx
->device_in
))
365 panic("Ingress device not up and running!\n");
367 rx_sock
= pf_socket();
368 tx_sock
= pf_socket();
370 fmemset(&tx_ring
, 0, sizeof(tx_ring
));
371 fmemset(&rx_ring
, 0, sizeof(rx_ring
));
372 fmemset(&rx_poll
, 0, sizeof(rx_poll
));
373 fmemset(&bpf_ops
, 0, sizeof(bpf_ops
));
375 ifindex_in
= device_ifindex(ctx
->device_in
);
376 ifindex_out
= device_ifindex(ctx
->device_out
);
378 size_in
= ring_size(ctx
->device_in
, ctx
->reserve_size
);
379 size_out
= ring_size(ctx
->device_out
, ctx
->reserve_size
);
381 enable_kernel_bpf_jit_compiler();
383 bpf_parse_rules(ctx
->filter
, &bpf_ops
);
384 bpf_attach_to_sock(rx_sock
, &bpf_ops
);
386 setup_rx_ring_layout(rx_sock
, &rx_ring
, size_in
, ctx
->jumbo_support
);
387 create_rx_ring(rx_sock
, &rx_ring
, ctx
->verbose
);
388 mmap_rx_ring(rx_sock
, &rx_ring
);
389 alloc_rx_ring_frames(&rx_ring
);
390 bind_rx_ring(rx_sock
, &rx_ring
, ifindex_in
);
391 prepare_polling(rx_sock
, &rx_poll
);
393 set_packet_loss_discard(tx_sock
);
394 setup_tx_ring_layout(tx_sock
, &tx_ring
, size_out
, ctx
->jumbo_support
);
395 create_tx_ring(tx_sock
, &tx_ring
, ctx
->verbose
);
396 mmap_tx_ring(tx_sock
, &tx_ring
);
397 alloc_tx_ring_frames(&tx_ring
);
398 bind_tx_ring(tx_sock
, &tx_ring
, ifindex_out
);
400 dissector_init_all(ctx
->print_mode
);
402 if (ctx
->promiscuous
)
403 ifflags
= enter_promiscuous_mode(ctx
->device_in
);
406 interval
= ctx
->kpull
;
408 itimer
.it_interval
.tv_sec
= 0;
409 itimer
.it_interval
.tv_usec
= interval
;
411 itimer
.it_value
.tv_sec
= 0;
412 itimer
.it_value
.tv_usec
= interval
;
414 setitimer(ITIMER_REAL
, &itimer
, NULL
);
418 bpf_dump_all(&bpf_ops
);
420 printf("MD: RXTX %luus\n\n", interval
);
422 printf("Running! Hang up with ^C!\n\n");
425 while (likely(sigint
== 0)) {
426 while (user_may_pull_from_rx(rx_ring
.frames
[it_in
].iov_base
)) {
429 hdr_in
= rx_ring
.frames
[it_in
].iov_base
;
430 in
= ((uint8_t *) hdr_in
) + hdr_in
->tp_h
.tp_mac
;
434 if (ctx
->packet_type
!= -1)
435 if (ctx
->packet_type
!= hdr_in
->s_ll
.sll_pkttype
)
438 hdr_out
= tx_ring
.frames
[it_out
].iov_base
;
439 out
= ((uint8_t *) hdr_out
) + TPACKET2_HDRLEN
- sizeof(struct sockaddr_ll
);
441 for (; !user_may_pull_from_tx(tx_ring
.frames
[it_out
].iov_base
) &&
444 next_rnd_slot(&it_out
, &tx_ring
);
447 if (it_out
>= tx_ring
.layout
.tp_frame_nr
)
451 hdr_out
= tx_ring
.frames
[it_out
].iov_base
;
452 out
= ((uint8_t *) hdr_out
) + TPACKET2_HDRLEN
- sizeof(struct sockaddr_ll
);
455 tpacket_hdr_clone(&hdr_out
->tp_h
, &hdr_in
->tp_h
);
456 fmemcpy(out
, in
, hdr_in
->tp_h
.tp_len
);
458 kernel_may_pull_from_tx(&hdr_out
->tp_h
);
460 next_rnd_slot(&it_out
, &tx_ring
);
463 if (it_out
>= tx_ring
.layout
.tp_frame_nr
)
467 show_frame_hdr(hdr_in
, ctx
->print_mode
, RING_MODE_INGRESS
);
469 dissector_entry_point(in
, hdr_in
->tp_h
.tp_snaplen
,
470 ctx
->link_type
, ctx
->print_mode
);
472 if (frame_count_max
!= 0) {
473 if (frame_count
>= frame_count_max
) {
481 kernel_may_pull_from_rx(&hdr_in
->tp_h
);
484 if (it_in
>= rx_ring
.layout
.tp_frame_nr
)
487 if (unlikely(sigint
== 1))
491 poll(&rx_poll
, 1, -1);
492 poll_error_maybe_die(rx_sock
, &rx_poll
);
497 sock_print_net_stats(rx_sock
, 0);
499 bpf_release(&bpf_ops
);
501 dissector_cleanup_all();
503 destroy_tx_ring(tx_sock
, &tx_ring
);
504 destroy_rx_ring(rx_sock
, &rx_ring
);
506 if (ctx
->promiscuous
)
507 leave_promiscuous_mode(ctx
->device_in
, ifflags
);
513 static void translate_pcap_to_txf(int fdo
, uint8_t *out
, size_t len
)
515 size_t bytes_done
= 0;
518 slprintf(bout
, sizeof(bout
), "{\n ");
519 write_or_die(fdo
, bout
, strlen(bout
));
521 while (bytes_done
< len
) {
522 slprintf(bout
, sizeof(bout
), "0x%02x, ", out
[bytes_done
]);
523 write_or_die(fdo
, bout
, strlen(bout
));
527 if (bytes_done
% 10 == 0) {
528 slprintf(bout
, sizeof(bout
), "\n");
529 write_or_die(fdo
, bout
, strlen(bout
));
531 if (bytes_done
< len
) {
532 slprintf(bout
, sizeof(bout
), " ");
533 write_or_die(fdo
, bout
, strlen(bout
));
537 if (bytes_done
% 10 != 0) {
538 slprintf(bout
, sizeof(bout
), "\n");
539 write_or_die(fdo
, bout
, strlen(bout
));
542 slprintf(bout
, sizeof(bout
), "}\n\n");
543 write_or_die(fdo
, bout
, strlen(bout
));
546 static void read_pcap(struct ctx
*ctx
)
550 int ret
, fd
, fdo
= 0;
551 unsigned long trunced
= 0;
553 struct pcap_pkthdr phdr
;
554 struct sock_fprog bpf_ops
;
556 struct timeval start
, end
, diff
;
560 fd
= open_or_die(ctx
->device_in
, O_RDONLY
| O_LARGEFILE
| O_NOATIME
);
562 ret
= __pcap_io
->pull_file_header(fd
, &ctx
->link_type
);
564 panic("Error reading pcap header!\n");
566 if (__pcap_io
->prepare_reading_pcap
) {
567 ret
= __pcap_io
->prepare_reading_pcap(fd
);
569 panic("Error prepare reading pcap!\n");
572 fmemset(&fm
, 0, sizeof(fm
));
573 fmemset(&bpf_ops
, 0, sizeof(bpf_ops
));
575 bpf_parse_rules(ctx
->filter
, &bpf_ops
);
577 dissector_init_all(ctx
->print_mode
);
579 out_len
= round_up(1024 * 1024, PAGE_SIZE
);
580 out
= xmalloc_aligned(out_len
, CO_CACHE_LINE_SIZE
);
584 bpf_dump_all(&bpf_ops
);
586 printf("MD: RD %s ", __pcap_io
->name
);
587 #ifdef _LARGEFILE64_SOURCE
593 printf("Running! Hang up with ^C!\n\n");
597 fdo
= open_or_die_m(ctx
->device_out
, O_RDWR
| O_CREAT
|
598 O_TRUNC
| O_LARGEFILE
, DEFFILEMODE
);
600 bug_on(gettimeofday(&start
, NULL
));
602 while (likely(sigint
== 0)) {
604 ret
= __pcap_io
->read_pcap_pkt(fd
, &phdr
, out
, out_len
);
605 if (unlikely(ret
< 0))
608 if (unlikely(phdr
.len
== 0)) {
613 if (unlikely(phdr
.len
> out_len
)) {
617 } while (ctx
->filter
&& !bpf_run_filter(&bpf_ops
, out
, phdr
.len
));
619 pcap_pkthdr_to_tpacket_hdr(&phdr
, &fm
.tp_h
);
621 ctx
->tx_bytes
+= fm
.tp_h
.tp_len
;
624 show_frame_hdr(&fm
, ctx
->print_mode
, RING_MODE_EGRESS
);
626 dissector_entry_point(out
, fm
.tp_h
.tp_snaplen
,
627 ctx
->link_type
, ctx
->print_mode
);
630 translate_pcap_to_txf(fdo
, out
, fm
.tp_h
.tp_snaplen
);
632 if (frame_count_max
!= 0) {
633 if (ctx
->tx_packets
>= frame_count_max
) {
642 bug_on(gettimeofday(&end
, NULL
));
643 diff
= tv_subtract(end
, start
);
645 bpf_release(&bpf_ops
);
647 dissector_cleanup_all();
649 if (__pcap_io
->prepare_close_pcap
)
650 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_READ
);
660 printf("\r%12lu packets outgoing\n", ctx
->tx_packets
);
661 printf("\r%12lu packets truncated in file\n", trunced
);
662 printf("\r%12lu bytes outgoing\n", ctx
->tx_bytes
);
663 printf("\r%12lu sec, %lu usec in total\n", diff
.tv_sec
, diff
.tv_usec
);
666 static void finish_multi_pcap_file(struct ctx
*ctx
, int fd
)
668 __pcap_io
->fsync_pcap(fd
);
670 if (__pcap_io
->prepare_close_pcap
)
671 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_WRITE
);
675 fmemset(&itimer
, 0, sizeof(itimer
));
676 setitimer(ITIMER_REAL
, &itimer
, NULL
);
679 static int next_multi_pcap_file(struct ctx
*ctx
, int fd
)
684 __pcap_io
->fsync_pcap(fd
);
686 if (__pcap_io
->prepare_close_pcap
)
687 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_WRITE
);
691 slprintf(fname
, sizeof(fname
), "%s/%s%lu.pcap", ctx
->device_out
,
692 ctx
->prefix
? : "dump-", time(0));
694 fd
= open_or_die_m(fname
, O_RDWR
| O_CREAT
| O_TRUNC
|
695 O_LARGEFILE
, DEFFILEMODE
);
697 ret
= __pcap_io
->push_file_header(fd
, ctx
->link_type
);
699 panic("Error writing pcap header!\n");
701 if (__pcap_io
->prepare_writing_pcap
) {
702 ret
= __pcap_io
->prepare_writing_pcap(fd
);
704 panic("Error prepare writing pcap!\n");
710 static int begin_multi_pcap_file(struct ctx
*ctx
)
717 if (ctx
->device_out
[strlen(ctx
->device_out
) - 1] == '/')
718 ctx
->device_out
[strlen(ctx
->device_out
) - 1] = 0;
720 slprintf(fname
, sizeof(fname
), "%s/%s%lu.pcap", ctx
->device_out
,
721 ctx
->prefix
? : "dump-", time(0));
723 fd
= open_or_die_m(fname
, O_RDWR
| O_CREAT
| O_TRUNC
|
724 O_LARGEFILE
, DEFFILEMODE
);
726 ret
= __pcap_io
->push_file_header(fd
, ctx
->link_type
);
728 panic("Error writing pcap header!\n");
730 if (__pcap_io
->prepare_writing_pcap
) {
731 ret
= __pcap_io
->prepare_writing_pcap(fd
);
733 panic("Error prepare writing pcap!\n");
736 if (ctx
->dump_mode
== DUMP_INTERVAL_TIME
) {
737 interval
= ctx
->dump_interval
;
739 itimer
.it_interval
.tv_sec
= interval
;
740 itimer
.it_interval
.tv_usec
= 0;
742 itimer
.it_value
.tv_sec
= interval
;
743 itimer
.it_value
.tv_usec
= 0;
745 setitimer(ITIMER_REAL
, &itimer
, NULL
);
753 static void finish_single_pcap_file(struct ctx
*ctx
, int fd
)
755 __pcap_io
->fsync_pcap(fd
);
757 if (__pcap_io
->prepare_close_pcap
)
758 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_WRITE
);
763 static int begin_single_pcap_file(struct ctx
*ctx
)
769 fd
= open_or_die_m(ctx
->device_out
, O_RDWR
| O_CREAT
| O_TRUNC
|
770 O_LARGEFILE
, DEFFILEMODE
);
772 ret
= __pcap_io
->push_file_header(fd
, ctx
->link_type
);
774 panic("Error writing pcap header!\n");
776 if (__pcap_io
->prepare_writing_pcap
) {
777 ret
= __pcap_io
->prepare_writing_pcap(fd
);
779 panic("Error prepare writing pcap!\n");
785 static void print_pcap_file_stats(int sock
, struct ctx
*ctx
, unsigned long skipped
)
787 unsigned long good
, bad
;
788 struct tpacket_stats kstats
;
789 socklen_t slen
= sizeof(kstats
);
791 fmemset(&kstats
, 0, sizeof(kstats
));
792 getsockopt(sock
, SOL_PACKET
, PACKET_STATISTICS
, &kstats
, &slen
);
794 if (ctx
->print_mode
== PRINT_NONE
) {
795 good
= kstats
.tp_packets
- kstats
.tp_drops
- skipped
;
796 bad
= kstats
.tp_drops
+ skipped
;
798 printf(".(+%lu/-%lu)", good
, bad
);
803 static void recv_only_or_dump(struct ctx
*ctx
)
807 int sock
, irq
, ifindex
, fd
= 0, ret
;
808 unsigned int size
, it
= 0;
809 unsigned long frame_count
= 0, skipped
= 0;
811 struct pollfd rx_poll
;
812 struct frame_map
*hdr
;
813 struct sock_fprog bpf_ops
;
814 struct timeval start
, end
, diff
;
815 struct pcap_pkthdr phdr
;
817 if (!device_up_and_running(ctx
->device_in
) && !ctx
->rfraw
)
818 panic("Device not up and running!\n");
823 ctx
->device_trans
= xstrdup(ctx
->device_in
);
824 xfree(ctx
->device_in
);
826 enter_rfmon_mac80211(ctx
->device_trans
, &ctx
->device_in
);
827 ctx
->link_type
= LINKTYPE_IEEE802_11
;
830 if (dump_to_pcap(ctx
)) {
834 fmemset(&stats
, 0, sizeof(stats
));
835 ret
= stat(ctx
->device_out
, &stats
);
841 ctx
->dump_dir
= S_ISDIR(stats
.st_mode
);
843 fd
= begin_multi_pcap_file(ctx
);
846 fd
= begin_single_pcap_file(ctx
);
850 fmemset(&rx_ring
, 0, sizeof(rx_ring
));
851 fmemset(&rx_poll
, 0, sizeof(rx_poll
));
852 fmemset(&bpf_ops
, 0, sizeof(bpf_ops
));
854 ifindex
= device_ifindex(ctx
->device_in
);
856 size
= ring_size(ctx
->device_in
, ctx
->reserve_size
);
858 enable_kernel_bpf_jit_compiler();
860 bpf_parse_rules(ctx
->filter
, &bpf_ops
);
861 bpf_attach_to_sock(sock
, &bpf_ops
);
863 set_sockopt_hwtimestamp(sock
, ctx
->device_in
);
865 setup_rx_ring_layout(sock
, &rx_ring
, size
, ctx
->jumbo_support
);
866 create_rx_ring(sock
, &rx_ring
, ctx
->verbose
);
867 mmap_rx_ring(sock
, &rx_ring
);
868 alloc_rx_ring_frames(&rx_ring
);
869 bind_rx_ring(sock
, &rx_ring
, ifindex
);
871 prepare_polling(sock
, &rx_poll
);
872 dissector_init_all(ctx
->print_mode
);
874 if (ctx
->cpu
>= 0 && ifindex
> 0) {
875 irq
= device_irq_number(ctx
->device_in
);
876 device_bind_irq_to_cpu(irq
, ctx
->cpu
);
879 printf("IRQ: %s:%d > CPU%d\n",
880 ctx
->device_in
, irq
, ctx
->cpu
);
883 if (ctx
->promiscuous
)
884 ifflags
= enter_promiscuous_mode(ctx
->device_in
);
888 bpf_dump_all(&bpf_ops
);
890 printf("MD: RX %s ", ctx
->dump
? pcap_ops
[ctx
->pcap
]->name
: "");
892 printf("802.11 raw via %s ", ctx
->device_in
);
893 #ifdef _LARGEFILE64_SOURCE
899 printf("Running! Hang up with ^C!\n\n");
902 bug_on(gettimeofday(&start
, NULL
));
904 while (likely(sigint
== 0)) {
905 while (user_may_pull_from_rx(rx_ring
.frames
[it
].iov_base
)) {
908 hdr
= rx_ring
.frames
[it
].iov_base
;
909 packet
= ((uint8_t *) hdr
) + hdr
->tp_h
.tp_mac
;
912 if (ctx
->packet_type
!= -1)
913 if (ctx
->packet_type
!= hdr
->s_ll
.sll_pkttype
)
916 if (unlikely(ring_frame_size(&rx_ring
) < hdr
->tp_h
.tp_snaplen
)) {
921 if (dump_to_pcap(ctx
)) {
922 tpacket_hdr_to_pcap_pkthdr(&hdr
->tp_h
, &phdr
);
924 ret
= __pcap_io
->write_pcap_pkt(fd
, &phdr
, packet
, phdr
.len
);
925 if (unlikely(ret
!= sizeof(phdr
) + phdr
.len
))
926 panic("Write error to pcap!\n");
929 show_frame_hdr(hdr
, ctx
->print_mode
, RING_MODE_INGRESS
);
931 dissector_entry_point(packet
, hdr
->tp_h
.tp_snaplen
,
932 ctx
->link_type
, ctx
->print_mode
);
934 if (frame_count_max
!= 0) {
935 if (frame_count
>= frame_count_max
) {
943 kernel_may_pull_from_rx(&hdr
->tp_h
);
946 if (it
>= rx_ring
.layout
.tp_frame_nr
)
949 if (unlikely(sigint
== 1))
952 if (dump_to_pcap(ctx
)) {
953 if (ctx
->dump_mode
== DUMP_INTERVAL_SIZE
) {
954 interval
+= hdr
->tp_h
.tp_snaplen
;
956 if (interval
> ctx
->dump_interval
) {
963 fd
= next_multi_pcap_file(ctx
, fd
);
967 print_pcap_file_stats(sock
, ctx
, skipped
);
972 poll(&rx_poll
, 1, -1);
973 poll_error_maybe_die(sock
, &rx_poll
);
976 bug_on(gettimeofday(&end
, NULL
));
977 diff
= tv_subtract(end
, start
);
979 if (!(ctx
->dump_dir
&& ctx
->print_mode
== PRINT_NONE
)) {
980 sock_print_net_stats(sock
, skipped
);
982 printf("\r%12lu sec, %lu usec in total\n",
983 diff
.tv_sec
, diff
.tv_usec
);
989 bpf_release(&bpf_ops
);
990 dissector_cleanup_all();
991 destroy_rx_ring(sock
, &rx_ring
);
993 if (ctx
->promiscuous
)
994 leave_promiscuous_mode(ctx
->device_in
, ifflags
);
997 leave_rfmon_mac80211(ctx
->device_trans
, ctx
->device_in
);
1001 if (dump_to_pcap(ctx
)) {
1003 finish_multi_pcap_file(ctx
, fd
);
1005 finish_single_pcap_file(ctx
, fd
);
1009 static void help(void)
1011 printf("\nnetsniff-ng %s, the packet sniffing beast\n", VERSION_STRING
);
1012 puts("http://www.netsniff-ng.org\n\n"
1013 "Usage: netsniff-ng [options]\n"
1015 " -i|-d|--dev|--in <dev|pcap> Input source as netdev or pcap\n"
1016 " -o|--out <dev|pcap|dir|txf> Output sink as netdev, pcap, directory, txf file\n"
1017 " -f|--filter <bpf-file> Use BPF filter file from bpfc\n"
1018 " -t|--type <type> Only handle packets of defined type:\n"
1019 " host|broadcast|multicast|others|outgoing\n"
1020 " -F|--interval <size/time> Dump interval in time or size if -o is a directory\n"
1021 " pcap swap spec: <num>KiB/MiB/GiB/s/sec/min/hrs\n"
1022 " -J|--jumbo-support Support for 64KB Super Jumbo Frames\n"
1023 " Default RX/TX slot: 2048Byte\n"
1024 " -R|--rfraw Capture or inject raw 802.11 frames\n"
1025 " -n|--num <uint> Number of packets until exit\n"
1026 " `-- 0 Loop until interrupted (default)\n"
1027 " `- n Send n packets and done\n"
1028 "Options for printing:\n"
1029 " -s|--silent Do not print captured packets\n"
1030 " -q|--less Print less-verbose packet information\n"
1031 " -X|--hex Print packet data in hex format\n"
1032 " -l|--ascii Print human-readable packet data\n"
1033 "Options, advanced:\n"
1034 " -P|--prefix <name> Prefix for pcaps stored in directory\n"
1035 " -r|--rand Randomize packet forwarding order\n"
1036 " -M|--no-promisc No promiscuous mode for netdev\n"
1037 " -A|--no-sock-mem Don't tune core socket memory\n"
1038 " -m|--mmap Mmap pcap file i.e., for replaying\n"
1039 " -g|--sg Scatter/gather pcap file I/O\n"
1040 " -c|--clrw Use slower read(2)/write(2) I/O\n"
1041 " -S|--ring-size <size> Manually set ring size to <size>:\n"
1042 " mmap space in KiB/MiB/GiB, e.g. \'10MiB\'\n"
1043 " -k|--kernel-pull <uint> Kernel pull from user interval in us\n"
1044 " Default is 10us where the TX_RING\n"
1045 " is populated with payload from uspace\n"
1046 " -b|--bind-cpu <cpu> Bind to specific CPU (or CPU-range)\n"
1047 " -B|--unbind-cpu <cpu> Forbid to use specific CPU (or CPU-range)\n"
1048 " -H|--prio-high Make this high priority process\n"
1049 " -Q|--notouch-irq Do not touch IRQ CPU affinity of NIC\n"
1050 " -V|--verbose Be more verbose\n"
1051 " -v|--version Show version\n"
1052 " -h|--help Guess what?!\n\n"
1054 " netsniff-ng --in eth0 --out dump.pcap --silent --bind-cpu 0\n"
1055 " netsniff-ng --in wlan0 --rfraw --out dump.pcap --silent --bind-cpu 0\n"
1056 " netsniff-ng --in dump.pcap --mmap --out eth0 -k1000 --silent --bind-cpu 0\n"
1057 " netsniff-ng --in dump.pcap --out dump.txf --silent --bind-cpu 0\n"
1058 " netsniff-ng --in eth0 --out eth1 --silent --bind-cpu 0 --type host\n"
1059 " netsniff-ng --in eth1 --out /opt/probe/ -s -m -J --interval 100MiB -b 0\n"
1060 " netsniff-ng --in any --filter http.bpf --jumbo-support --ascii -V\n\n"
1062 " This tool is targeted for network developers! You should\n"
1063 " be aware of what you are doing and what these options above\n"
1064 " mean! Use netsniff-ng's bpfc compiler for generating filter files.\n"
1065 " Further, netsniff-ng automatically enables the kernel BPF JIT\n"
1066 " if present. Txf file output is only possible if the input source\n"
1067 " is a pcap file.\n\n"
1068 "Please report bugs to <bugs@netsniff-ng.org>\n"
1069 "Copyright (C) 2009-2013 Daniel Borkmann <daniel@netsniff-ng.org>\n"
1070 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel@netsniff-ng.org>\n"
1071 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1072 "License: GNU GPL version 2.0\n"
1073 "This is free software: you are free to change and redistribute it.\n"
1074 "There is NO WARRANTY, to the extent permitted by law.\n");
1078 static void version(void)
1080 printf("\nnetsniff-ng %s, the packet sniffing beast\n", VERSION_STRING
);
1081 puts("http://www.netsniff-ng.org\n\n"
1082 "Please report bugs to <bugs@netsniff-ng.org>\n"
1083 "Copyright (C) 2009-2013 Daniel Borkmann <daniel@netsniff-ng.org>\n"
1084 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel@netsniff-ng.org>\n"
1085 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1086 "License: GNU GPL version 2.0\n"
1087 "This is free software: you are free to change and redistribute it.\n"
1088 "There is NO WARRANTY, to the extent permitted by law.\n");
1092 static void header(void)
1094 printf("%s%s%s\n", colorize_start(bold
), "netsniff-ng " VERSION_STRING
, colorize_end());
1097 int main(int argc
, char **argv
)
1100 int c
, i
, j
, opt_index
, ops_touched
= 0, vals
[4] = {0};
1101 bool prio_high
= false, setsockmem
= true;
1102 void (*main_loop
)(struct ctx
*ctx
) = NULL
;
1104 .link_type
= LINKTYPE_EN10MB
,
1105 .print_mode
= PRINT_NORM
,
1108 .promiscuous
= true,
1110 .pcap
= PCAP_OPS_SG
,
1111 .dump_interval
= 60,
1112 .dump_mode
= DUMP_INTERVAL_TIME
,
1117 while ((c
= getopt_long(argc
, argv
, short_options
, long_options
,
1118 &opt_index
)) != EOF
) {
1122 ctx
.device_in
= xstrdup(optarg
);
1125 ctx
.device_out
= xstrdup(optarg
);
1128 ctx
.prefix
= xstrdup(optarg
);
1131 ctx
.link_type
= LINKTYPE_IEEE802_11
;
1135 ctx
.randomize
= true;
1138 ctx
.jumbo_support
= 1;
1141 ctx
.filter
= xstrdup(optarg
);
1144 ctx
.promiscuous
= false;
1150 if (!strncmp(optarg
, "host", strlen("host")))
1151 ctx
.packet_type
= PACKET_HOST
;
1152 else if (!strncmp(optarg
, "broadcast", strlen("broadcast")))
1153 ctx
.packet_type
= PACKET_BROADCAST
;
1154 else if (!strncmp(optarg
, "multicast", strlen("multicast")))
1155 ctx
.packet_type
= PACKET_MULTICAST
;
1156 else if (!strncmp(optarg
, "others", strlen("others")))
1157 ctx
.packet_type
= PACKET_OTHERHOST
;
1158 else if (!strncmp(optarg
, "outgoing", strlen("outgoing")))
1159 ctx
.packet_type
= PACKET_OUTGOING
;
1161 ctx
.packet_type
= -1;
1165 ctx
.reserve_size
= 0;
1167 for (j
= i
= strlen(optarg
); i
> 0; --i
) {
1168 if (!isdigit(optarg
[j
- i
]))
1173 if (!strncmp(ptr
, "KiB", strlen("KiB")))
1174 ctx
.reserve_size
= 1 << 10;
1175 else if (!strncmp(ptr
, "MiB", strlen("MiB")))
1176 ctx
.reserve_size
= 1 << 20;
1177 else if (!strncmp(ptr
, "GiB", strlen("GiB")))
1178 ctx
.reserve_size
= 1 << 30;
1180 panic("Syntax error in ring size param!\n");
1183 ctx
.reserve_size
*= strtol(optarg
, NULL
, 0);
1186 set_cpu_affinity(optarg
, 0);
1187 /* Take the first CPU for rebinding the IRQ */
1189 ctx
.cpu
= strtol(optarg
, NULL
, 0);
1192 set_cpu_affinity(optarg
, 1);
1198 ctx
.pcap
= PCAP_OPS_RW
;
1202 ctx
.pcap
= PCAP_OPS_MMAP
;
1206 ctx
.pcap
= PCAP_OPS_SG
;
1213 ctx
.print_mode
= PRINT_NONE
;
1216 ctx
.print_mode
= PRINT_LESS
;
1220 (ctx
.print_mode
== PRINT_ASCII
) ?
1221 PRINT_HEX_ASCII
: PRINT_HEX
;
1225 (ctx
.print_mode
== PRINT_HEX
) ?
1226 PRINT_HEX_ASCII
: PRINT_ASCII
;
1229 ctx
.kpull
= strtol(optarg
, NULL
, 0);
1232 frame_count_max
= strtol(optarg
, NULL
, 0);
1236 ctx
.dump_interval
= 0;
1238 for (j
= i
= strlen(optarg
); i
> 0; --i
) {
1239 if (!isdigit(optarg
[j
- i
]))
1244 if (!strncmp(ptr
, "KiB", strlen("KiB"))) {
1245 ctx
.dump_interval
= 1 << 10;
1246 ctx
.dump_mode
= DUMP_INTERVAL_SIZE
;
1247 } else if (!strncmp(ptr
, "MiB", strlen("MiB"))) {
1248 ctx
.dump_interval
= 1 << 20;
1249 ctx
.dump_mode
= DUMP_INTERVAL_SIZE
;
1250 } else if (!strncmp(ptr
, "GiB", strlen("GiB"))) {
1251 ctx
.dump_interval
= 1 << 30;
1252 ctx
.dump_mode
= DUMP_INTERVAL_SIZE
;
1253 } else if (!strncmp(ptr
, "sec", strlen("sec"))) {
1254 ctx
.dump_interval
= 1;
1255 ctx
.dump_mode
= DUMP_INTERVAL_TIME
;
1256 } else if (!strncmp(ptr
, "min", strlen("min"))) {
1257 ctx
.dump_interval
= 60;
1258 ctx
.dump_mode
= DUMP_INTERVAL_TIME
;
1259 } else if (!strncmp(ptr
, "hrs", strlen("hrs"))) {
1260 ctx
.dump_interval
= 60 * 60;
1261 ctx
.dump_mode
= DUMP_INTERVAL_TIME
;
1262 } else if (!strncmp(ptr
, "s", strlen("s"))) {
1263 ctx
.dump_interval
= 1;
1264 ctx
.dump_mode
= DUMP_INTERVAL_TIME
;
1266 panic("Syntax error in time/size param!\n");
1270 ctx
.dump_interval
*= strtol(optarg
, NULL
, 0);
1296 panic("Option -%c requires an argument!\n",
1299 if (isprint(optopt
))
1300 whine("Unknown option character "
1301 "`0x%X\'!\n", optopt
);
1310 ctx
.device_in
= xstrdup("any");
1312 register_signal(SIGINT
, signal_handler
);
1313 register_signal(SIGHUP
, signal_handler
);
1317 init_pcap(ctx
.jumbo_support
);
1321 set_proc_prio(get_default_proc_prio());
1322 set_sched_status(get_default_sched_policy(), get_default_sched_prio());
1325 if (ctx
.device_in
&& (device_mtu(ctx
.device_in
) ||
1326 !strncmp("any", ctx
.device_in
, strlen(ctx
.device_in
)))) {
1327 if (!ctx
.device_out
) {
1329 main_loop
= recv_only_or_dump
;
1330 } else if (device_mtu(ctx
.device_out
)) {
1331 register_signal_f(SIGALRM
, timer_elapsed
, SA_SIGINFO
);
1332 main_loop
= receive_to_xmit
;
1335 register_signal_f(SIGALRM
, timer_next_dump
, SA_SIGINFO
);
1336 main_loop
= recv_only_or_dump
;
1338 ctx
.pcap
= PCAP_OPS_SG
;
1341 if (ctx
.device_out
&& device_mtu(ctx
.device_out
)) {
1342 register_signal_f(SIGALRM
, timer_elapsed
, SA_SIGINFO
);
1343 main_loop
= pcap_to_xmit
;
1345 ctx
.pcap
= PCAP_OPS_MMAP
;
1347 main_loop
= read_pcap
;
1349 ctx
.pcap
= PCAP_OPS_SG
;
1356 set_system_socket_memory(vals
);
1361 reset_system_socket_memory(vals
);
1366 free(ctx
.device_in
);
1367 free(ctx
.device_out
);
1368 free(ctx
.device_trans
);