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
, packet_type
, verbose
;
62 unsigned long kpull
, dump_interval
, reserve_size
, tx_bytes
, tx_packets
;
63 bool randomize
, promiscuous
, enforce
, jumbo
, dump_bpf
;
64 enum pcap_ops_groups pcap
; enum dump_mode dump_mode
;
65 uid_t uid
; gid_t gid
; uint32_t link_type
, magic
;
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:HQmcsqXlvhF:RGAP:Vu:g:T:DB";
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 {"prefix", required_argument
, NULL
, 'P'},
85 {"user", required_argument
, NULL
, 'u'},
86 {"group", required_argument
, NULL
, 'g'},
87 {"magic", required_argument
, NULL
, 'T'},
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 {"dump-pcap-types", no_argument
, NULL
, 'D'},
98 {"dump-bpf", no_argument
, NULL
, 'B'},
99 {"silent", no_argument
, NULL
, 's'},
100 {"less", no_argument
, NULL
, 'q'},
101 {"hex", no_argument
, NULL
, 'X'},
102 {"ascii", no_argument
, NULL
, 'l'},
103 {"no-sock-mem", no_argument
, NULL
, 'A'},
104 {"verbose", no_argument
, NULL
, 'V'},
105 {"version", no_argument
, NULL
, 'v'},
106 {"help", no_argument
, NULL
, 'h'},
112 static struct itimerval itimer
;
114 static unsigned long frame_count_max
= 0, interval
= TX_KERNEL_PULL_INT
;
116 #define __pcap_io pcap_ops[ctx->pcap]
118 static void signal_handler(int number
)
129 static void timer_elapsed(int unused
)
131 itimer
.it_interval
.tv_sec
= 0;
132 itimer
.it_interval
.tv_usec
= interval
;
134 itimer
.it_value
.tv_sec
= 0;
135 itimer
.it_value
.tv_usec
= interval
;
137 pull_and_flush_tx_ring(tx_sock
);
138 setitimer(ITIMER_REAL
, &itimer
, NULL
);
141 static void timer_next_dump(int unused
)
143 itimer
.it_interval
.tv_sec
= interval
;
144 itimer
.it_interval
.tv_usec
= 0;
146 itimer
.it_value
.tv_sec
= interval
;
147 itimer
.it_value
.tv_usec
= 0;
150 setitimer(ITIMER_REAL
, &itimer
, NULL
);
153 static inline bool dump_to_pcap(struct ctx
*ctx
)
158 static void pcap_to_xmit(struct ctx
*ctx
)
162 int irq
, ifindex
, fd
= 0, ret
;
163 unsigned int size
, it
= 0;
164 unsigned long trunced
= 0;
166 struct frame_map
*hdr
;
167 struct sock_fprog bpf_ops
;
168 struct timeval start
, end
, diff
;
171 if (!device_up_and_running(ctx
->device_out
) && !ctx
->rfraw
)
172 panic("Device not up and running!\n");
176 tx_sock
= pf_socket();
178 if (!strncmp("-", ctx
->device_in
, strlen("-"))) {
179 fd
= dup(fileno(stdin
));
180 close(fileno(stdin
));
181 if (ctx
->pcap
== PCAP_OPS_MM
)
182 ctx
->pcap
= PCAP_OPS_SG
;
184 fd
= open_or_die(ctx
->device_in
, O_RDONLY
| O_LARGEFILE
| O_NOATIME
);
187 ret
= __pcap_io
->pull_fhdr_pcap(fd
, &ctx
->magic
, &ctx
->link_type
);
189 panic("Error reading pcap header!\n");
191 if (__pcap_io
->prepare_access_pcap
) {
192 ret
= __pcap_io
->prepare_access_pcap(fd
, PCAP_MODE_RD
, ctx
->jumbo
);
194 panic("Error prepare reading pcap!\n");
197 fmemset(&tx_ring
, 0, sizeof(tx_ring
));
198 fmemset(&bpf_ops
, 0, sizeof(bpf_ops
));
201 ctx
->device_trans
= xstrdup(ctx
->device_out
);
202 xfree(ctx
->device_out
);
204 enter_rfmon_mac80211(ctx
->device_trans
, &ctx
->device_out
);
205 if (ctx
->link_type
!= LINKTYPE_IEEE802_11
)
206 panic("Wrong linktype of pcap!\n");
209 ifindex
= device_ifindex(ctx
->device_out
);
211 size
= ring_size(ctx
->device_out
, ctx
->reserve_size
);
213 bpf_parse_rules(ctx
->device_out
, ctx
->filter
, &bpf_ops
);
215 bpf_dump_all(&bpf_ops
);
217 set_packet_loss_discard(tx_sock
);
218 set_sockopt_hwtimestamp(tx_sock
, ctx
->device_out
);
220 setup_tx_ring_layout(tx_sock
, &tx_ring
, size
, ctx
->jumbo
);
221 create_tx_ring(tx_sock
, &tx_ring
, ctx
->verbose
);
222 mmap_tx_ring(tx_sock
, &tx_ring
);
223 alloc_tx_ring_frames(&tx_ring
);
224 bind_tx_ring(tx_sock
, &tx_ring
, ifindex
);
226 dissector_init_all(ctx
->print_mode
);
228 if (ctx
->cpu
>= 0 && ifindex
> 0) {
229 irq
= device_irq_number(ctx
->device_out
);
230 device_bind_irq_to_cpu(irq
, ctx
->cpu
);
233 printf("IRQ: %s:%d > CPU%d\n",
234 ctx
->device_out
, irq
, ctx
->cpu
);
238 interval
= ctx
->kpull
;
240 itimer
.it_interval
.tv_sec
= 0;
241 itimer
.it_interval
.tv_usec
= interval
;
243 itimer
.it_value
.tv_sec
= 0;
244 itimer
.it_value
.tv_usec
= interval
;
246 setitimer(ITIMER_REAL
, &itimer
, NULL
);
248 drop_privileges(ctx
->enforce
, ctx
->uid
, ctx
->gid
);
250 printf("Running! Hang up with ^C!\n\n");
253 bug_on(gettimeofday(&start
, NULL
));
255 while (likely(sigint
== 0)) {
256 while (user_may_pull_from_tx(tx_ring
.frames
[it
].iov_base
)) {
257 hdr
= tx_ring
.frames
[it
].iov_base
;
258 out
= ((uint8_t *) hdr
) + TPACKET2_HDRLEN
- sizeof(struct sockaddr_ll
);
261 ret
= __pcap_io
->read_pcap(fd
, &phdr
, ctx
->magic
, out
,
262 ring_frame_size(&tx_ring
));
263 if (unlikely(ret
<= 0))
266 if (ring_frame_size(&tx_ring
) <
267 pcap_get_length(&phdr
, ctx
->magic
)) {
268 pcap_set_length(&phdr
, ctx
->magic
,
269 ring_frame_size(&tx_ring
));
272 } while (ctx
->filter
&&
273 !bpf_run_filter(&bpf_ops
, out
,
274 pcap_get_length(&phdr
, ctx
->magic
)));
276 pcap_pkthdr_to_tpacket_hdr(&phdr
, ctx
->magic
, &hdr
->tp_h
, &hdr
->s_ll
);
278 ctx
->tx_bytes
+= hdr
->tp_h
.tp_len
;;
281 show_frame_hdr(hdr
, ctx
->print_mode
);
283 dissector_entry_point(out
, hdr
->tp_h
.tp_snaplen
,
284 ctx
->link_type
, ctx
->print_mode
);
286 kernel_may_pull_from_tx(&hdr
->tp_h
);
289 if (it
>= tx_ring
.layout
.tp_frame_nr
)
292 if (unlikely(sigint
== 1))
295 if (frame_count_max
!= 0) {
296 if (ctx
->tx_packets
>= frame_count_max
) {
306 bug_on(gettimeofday(&end
, NULL
));
307 diff
= tv_subtract(end
, start
);
309 bpf_release(&bpf_ops
);
311 dissector_cleanup_all();
312 destroy_tx_ring(tx_sock
, &tx_ring
);
315 leave_rfmon_mac80211(ctx
->device_trans
, ctx
->device_out
);
317 if (__pcap_io
->prepare_close_pcap
)
318 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_RD
);
320 if (strncmp("-", ctx
->device_in
, strlen("-")))
323 dup2(fd
, fileno(stdin
));
329 printf("\r%12lu packets outgoing\n", ctx
->tx_packets
);
330 printf("\r%12lu packets truncated in file\n", trunced
);
331 printf("\r%12lu bytes outgoing\n", ctx
->tx_bytes
);
332 printf("\r%12lu sec, %lu usec in total\n", diff
.tv_sec
, diff
.tv_usec
);
335 static void receive_to_xmit(struct ctx
*ctx
)
339 int rx_sock
, ifindex_in
, ifindex_out
;
340 unsigned int size_in
, size_out
, it_in
= 0, it_out
= 0;
341 unsigned long frame_count
= 0;
342 struct frame_map
*hdr_in
, *hdr_out
;
343 struct ring tx_ring
, rx_ring
;
344 struct pollfd rx_poll
;
345 struct sock_fprog bpf_ops
;
347 if (!strncmp(ctx
->device_in
, ctx
->device_out
, IFNAMSIZ
))
348 panic("Ingress/egress devices must be different!\n");
349 if (!device_up_and_running(ctx
->device_out
))
350 panic("Egress device not up and running!\n");
351 if (!device_up_and_running(ctx
->device_in
))
352 panic("Ingress device not up and running!\n");
354 rx_sock
= pf_socket();
355 tx_sock
= pf_socket();
357 fmemset(&tx_ring
, 0, sizeof(tx_ring
));
358 fmemset(&rx_ring
, 0, sizeof(rx_ring
));
359 fmemset(&rx_poll
, 0, sizeof(rx_poll
));
360 fmemset(&bpf_ops
, 0, sizeof(bpf_ops
));
362 ifindex_in
= device_ifindex(ctx
->device_in
);
363 ifindex_out
= device_ifindex(ctx
->device_out
);
365 size_in
= ring_size(ctx
->device_in
, ctx
->reserve_size
);
366 size_out
= ring_size(ctx
->device_out
, ctx
->reserve_size
);
368 enable_kernel_bpf_jit_compiler();
370 bpf_parse_rules(ctx
->device_in
, ctx
->filter
, &bpf_ops
);
372 bpf_dump_all(&bpf_ops
);
373 bpf_attach_to_sock(rx_sock
, &bpf_ops
);
375 setup_rx_ring_layout(rx_sock
, &rx_ring
, size_in
, ctx
->jumbo
);
376 create_rx_ring(rx_sock
, &rx_ring
, ctx
->verbose
);
377 mmap_rx_ring(rx_sock
, &rx_ring
);
378 alloc_rx_ring_frames(&rx_ring
);
379 bind_rx_ring(rx_sock
, &rx_ring
, ifindex_in
);
380 prepare_polling(rx_sock
, &rx_poll
);
382 set_packet_loss_discard(tx_sock
);
383 setup_tx_ring_layout(tx_sock
, &tx_ring
, size_out
, ctx
->jumbo
);
384 create_tx_ring(tx_sock
, &tx_ring
, ctx
->verbose
);
385 mmap_tx_ring(tx_sock
, &tx_ring
);
386 alloc_tx_ring_frames(&tx_ring
);
387 bind_tx_ring(tx_sock
, &tx_ring
, ifindex_out
);
389 dissector_init_all(ctx
->print_mode
);
391 if (ctx
->promiscuous
)
392 ifflags
= enter_promiscuous_mode(ctx
->device_in
);
395 interval
= ctx
->kpull
;
397 itimer
.it_interval
.tv_sec
= 0;
398 itimer
.it_interval
.tv_usec
= interval
;
400 itimer
.it_value
.tv_sec
= 0;
401 itimer
.it_value
.tv_usec
= interval
;
403 setitimer(ITIMER_REAL
, &itimer
, NULL
);
405 drop_privileges(ctx
->enforce
, ctx
->uid
, ctx
->gid
);
407 printf("Running! Hang up with ^C!\n\n");
410 while (likely(sigint
== 0)) {
411 while (user_may_pull_from_rx(rx_ring
.frames
[it_in
].iov_base
)) {
414 hdr_in
= rx_ring
.frames
[it_in
].iov_base
;
415 in
= ((uint8_t *) hdr_in
) + hdr_in
->tp_h
.tp_mac
;
419 if (ctx
->packet_type
!= -1)
420 if (ctx
->packet_type
!= hdr_in
->s_ll
.sll_pkttype
)
423 hdr_out
= tx_ring
.frames
[it_out
].iov_base
;
424 out
= ((uint8_t *) hdr_out
) + TPACKET2_HDRLEN
- sizeof(struct sockaddr_ll
);
426 for (; !user_may_pull_from_tx(tx_ring
.frames
[it_out
].iov_base
) &&
429 next_rnd_slot(&it_out
, &tx_ring
);
432 if (it_out
>= tx_ring
.layout
.tp_frame_nr
)
436 hdr_out
= tx_ring
.frames
[it_out
].iov_base
;
437 out
= ((uint8_t *) hdr_out
) + TPACKET2_HDRLEN
- sizeof(struct sockaddr_ll
);
440 tpacket_hdr_clone(&hdr_out
->tp_h
, &hdr_in
->tp_h
);
441 fmemcpy(out
, in
, hdr_in
->tp_h
.tp_len
);
443 kernel_may_pull_from_tx(&hdr_out
->tp_h
);
445 next_rnd_slot(&it_out
, &tx_ring
);
448 if (it_out
>= tx_ring
.layout
.tp_frame_nr
)
452 show_frame_hdr(hdr_in
, ctx
->print_mode
);
454 dissector_entry_point(in
, hdr_in
->tp_h
.tp_snaplen
,
455 ctx
->link_type
, ctx
->print_mode
);
457 if (frame_count_max
!= 0) {
458 if (frame_count
>= frame_count_max
) {
466 kernel_may_pull_from_rx(&hdr_in
->tp_h
);
469 if (it_in
>= rx_ring
.layout
.tp_frame_nr
)
472 if (unlikely(sigint
== 1))
476 poll(&rx_poll
, 1, -1);
477 poll_error_maybe_die(rx_sock
, &rx_poll
);
482 sock_print_net_stats(rx_sock
, 0);
484 bpf_release(&bpf_ops
);
486 dissector_cleanup_all();
488 destroy_tx_ring(tx_sock
, &tx_ring
);
489 destroy_rx_ring(rx_sock
, &rx_ring
);
491 if (ctx
->promiscuous
)
492 leave_promiscuous_mode(ctx
->device_in
, ifflags
);
498 static void translate_pcap_to_txf(int fdo
, uint8_t *out
, size_t len
)
500 size_t bytes_done
= 0;
503 slprintf(bout
, sizeof(bout
), "{\n ");
504 write_or_die(fdo
, bout
, strlen(bout
));
506 while (bytes_done
< len
) {
507 slprintf(bout
, sizeof(bout
), "0x%02x, ", out
[bytes_done
]);
508 write_or_die(fdo
, bout
, strlen(bout
));
512 if (bytes_done
% 10 == 0) {
513 slprintf(bout
, sizeof(bout
), "\n");
514 write_or_die(fdo
, bout
, strlen(bout
));
516 if (bytes_done
< len
) {
517 slprintf(bout
, sizeof(bout
), " ");
518 write_or_die(fdo
, bout
, strlen(bout
));
522 if (bytes_done
% 10 != 0) {
523 slprintf(bout
, sizeof(bout
), "\n");
524 write_or_die(fdo
, bout
, strlen(bout
));
527 slprintf(bout
, sizeof(bout
), "}\n\n");
528 write_or_die(fdo
, bout
, strlen(bout
));
531 static void read_pcap(struct ctx
*ctx
)
535 int ret
, fd
, fdo
= 0;
536 unsigned long trunced
= 0;
539 struct sock_fprog bpf_ops
;
541 struct timeval start
, end
, diff
;
542 struct sockaddr_ll sll
;
546 if (!strncmp("-", ctx
->device_in
, strlen("-"))) {
547 fd
= dup(fileno(stdin
));
548 close(fileno(stdin
));
549 if (ctx
->pcap
== PCAP_OPS_MM
)
550 ctx
->pcap
= PCAP_OPS_SG
;
552 fd
= open_or_die(ctx
->device_in
, O_RDONLY
| O_LARGEFILE
| O_NOATIME
);
555 ret
= __pcap_io
->pull_fhdr_pcap(fd
, &ctx
->magic
, &ctx
->link_type
);
557 panic("Error reading pcap header!\n");
559 if (__pcap_io
->prepare_access_pcap
) {
560 ret
= __pcap_io
->prepare_access_pcap(fd
, PCAP_MODE_RD
, ctx
->jumbo
);
562 panic("Error prepare reading pcap!\n");
565 fmemset(&fm
, 0, sizeof(fm
));
566 fmemset(&bpf_ops
, 0, sizeof(bpf_ops
));
568 bpf_parse_rules("any", ctx
->filter
, &bpf_ops
);
570 bpf_dump_all(&bpf_ops
);
572 dissector_init_all(ctx
->print_mode
);
574 out_len
= round_up(1024 * 1024, PAGE_SIZE
);
575 out
= xmalloc_aligned(out_len
, CO_CACHE_LINE_SIZE
);
577 if (ctx
->device_out
) {
578 if (!strncmp("-", ctx
->device_out
, strlen("-"))) {
579 fdo
= dup(fileno(stdout
));
580 close(fileno(stdout
));
582 fdo
= open_or_die_m(ctx
->device_out
, O_RDWR
| O_CREAT
|
583 O_TRUNC
| O_LARGEFILE
, DEFFILEMODE
);
587 drop_privileges(ctx
->enforce
, ctx
->uid
, ctx
->gid
);
589 printf("Running! Hang up with ^C!\n\n");
592 bug_on(gettimeofday(&start
, NULL
));
594 while (likely(sigint
== 0)) {
596 ret
= __pcap_io
->read_pcap(fd
, &phdr
, ctx
->magic
,
598 if (unlikely(ret
< 0))
601 if (unlikely(pcap_get_length(&phdr
, ctx
->magic
) == 0)) {
606 if (unlikely(pcap_get_length(&phdr
, ctx
->magic
) > out_len
)) {
607 pcap_set_length(&phdr
, ctx
->magic
, out_len
);
610 } while (ctx
->filter
&&
611 !bpf_run_filter(&bpf_ops
, out
,
612 pcap_get_length(&phdr
, ctx
->magic
)));
614 pcap_pkthdr_to_tpacket_hdr(&phdr
, ctx
->magic
, &fm
.tp_h
, &sll
);
616 ctx
->tx_bytes
+= fm
.tp_h
.tp_len
;
619 show_frame_hdr(&fm
, ctx
->print_mode
);
621 dissector_entry_point(out
, fm
.tp_h
.tp_snaplen
,
622 ctx
->link_type
, ctx
->print_mode
);
625 translate_pcap_to_txf(fdo
, out
, fm
.tp_h
.tp_snaplen
);
627 if (frame_count_max
!= 0) {
628 if (ctx
->tx_packets
>= frame_count_max
) {
637 bug_on(gettimeofday(&end
, NULL
));
638 diff
= tv_subtract(end
, start
);
640 bpf_release(&bpf_ops
);
642 dissector_cleanup_all();
644 if (__pcap_io
->prepare_close_pcap
)
645 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_RD
);
651 printf("\r%12lu packets outgoing\n", ctx
->tx_packets
);
652 printf("\r%12lu packets truncated in file\n", trunced
);
653 printf("\r%12lu bytes outgoing\n", ctx
->tx_bytes
);
654 printf("\r%12lu sec, %lu usec in total\n", diff
.tv_sec
, diff
.tv_usec
);
656 if (strncmp("-", ctx
->device_in
, strlen("-")))
659 dup2(fd
, fileno(stdin
));
661 if (ctx
->device_out
) {
662 if (strncmp("-", ctx
->device_out
, strlen("-")))
665 dup2(fdo
, fileno(stdout
));
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_WR
);
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_WR
);
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_fhdr_pcap(fd
, ctx
->magic
, ctx
->link_type
);
702 panic("Error writing pcap header!\n");
704 if (__pcap_io
->prepare_access_pcap
) {
705 ret
= __pcap_io
->prepare_access_pcap(fd
, PCAP_MODE_WR
, ctx
->jumbo
);
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_fhdr_pcap(fd
, ctx
->magic
, ctx
->link_type
);
731 panic("Error writing pcap header!\n");
733 if (__pcap_io
->prepare_access_pcap
) {
734 ret
= __pcap_io
->prepare_access_pcap(fd
, PCAP_MODE_WR
, ctx
->jumbo
);
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_WR
);
763 if (strncmp("-", ctx
->device_out
, strlen("-")))
766 dup2(fd
, fileno(stdout
));
769 static int begin_single_pcap_file(struct ctx
*ctx
)
775 if (!strncmp("-", ctx
->device_out
, strlen("-"))) {
776 fd
= dup(fileno(stdout
));
777 close(fileno(stdout
));
778 if (ctx
->pcap
== PCAP_OPS_MM
)
779 ctx
->pcap
= PCAP_OPS_SG
;
781 fd
= open_or_die_m(ctx
->device_out
,
782 O_RDWR
| O_CREAT
| O_TRUNC
|
783 O_LARGEFILE
, DEFFILEMODE
);
786 ret
= __pcap_io
->push_fhdr_pcap(fd
, ctx
->magic
, ctx
->link_type
);
788 panic("Error writing pcap header!\n");
790 if (__pcap_io
->prepare_access_pcap
) {
791 ret
= __pcap_io
->prepare_access_pcap(fd
, PCAP_MODE_WR
, ctx
->jumbo
);
793 panic("Error prepare writing pcap!\n");
799 static void print_pcap_file_stats(int sock
, struct ctx
*ctx
, unsigned long skipped
)
801 unsigned long good
, bad
;
802 struct tpacket_stats kstats
;
803 socklen_t slen
= sizeof(kstats
);
805 fmemset(&kstats
, 0, sizeof(kstats
));
806 getsockopt(sock
, SOL_PACKET
, PACKET_STATISTICS
, &kstats
, &slen
);
808 if (ctx
->print_mode
== PRINT_NONE
) {
809 good
= kstats
.tp_packets
- kstats
.tp_drops
- skipped
;
810 bad
= kstats
.tp_drops
+ skipped
;
812 printf(".(+%lu/-%lu)", good
, bad
);
817 static void recv_only_or_dump(struct ctx
*ctx
)
821 int sock
, irq
, ifindex
, fd
= 0, ret
;
822 unsigned int size
, it
= 0;
823 unsigned long frame_count
= 0, skipped
= 0;
825 struct pollfd rx_poll
;
826 struct frame_map
*hdr
;
827 struct sock_fprog bpf_ops
;
828 struct timeval start
, end
, diff
;
831 if (!device_up_and_running(ctx
->device_in
) && !ctx
->rfraw
)
832 panic("Device not up and running!\n");
837 ctx
->device_trans
= xstrdup(ctx
->device_in
);
838 xfree(ctx
->device_in
);
840 enter_rfmon_mac80211(ctx
->device_trans
, &ctx
->device_in
);
841 ctx
->link_type
= LINKTYPE_IEEE802_11
;
844 fmemset(&rx_ring
, 0, sizeof(rx_ring
));
845 fmemset(&rx_poll
, 0, sizeof(rx_poll
));
846 fmemset(&bpf_ops
, 0, sizeof(bpf_ops
));
848 ifindex
= device_ifindex(ctx
->device_in
);
850 size
= ring_size(ctx
->device_in
, ctx
->reserve_size
);
852 enable_kernel_bpf_jit_compiler();
854 bpf_parse_rules(ctx
->device_in
, ctx
->filter
, &bpf_ops
);
856 bpf_dump_all(&bpf_ops
);
857 bpf_attach_to_sock(sock
, &bpf_ops
);
859 set_sockopt_hwtimestamp(sock
, ctx
->device_in
);
861 setup_rx_ring_layout(sock
, &rx_ring
, size
, ctx
->jumbo
);
862 create_rx_ring(sock
, &rx_ring
, ctx
->verbose
);
863 mmap_rx_ring(sock
, &rx_ring
);
864 alloc_rx_ring_frames(&rx_ring
);
865 bind_rx_ring(sock
, &rx_ring
, ifindex
);
867 prepare_polling(sock
, &rx_poll
);
868 dissector_init_all(ctx
->print_mode
);
870 if (ctx
->cpu
>= 0 && ifindex
> 0) {
871 irq
= device_irq_number(ctx
->device_in
);
872 device_bind_irq_to_cpu(irq
, ctx
->cpu
);
875 printf("IRQ: %s:%d > CPU%d\n",
876 ctx
->device_in
, irq
, ctx
->cpu
);
879 if (ctx
->promiscuous
)
880 ifflags
= enter_promiscuous_mode(ctx
->device_in
);
882 drop_privileges(ctx
->enforce
, ctx
->uid
, ctx
->gid
);
884 if (dump_to_pcap(ctx
)) {
888 fmemset(&stats
, 0, sizeof(stats
));
889 ret
= stat(ctx
->device_out
, &stats
);
895 ctx
->dump_dir
= S_ISDIR(stats
.st_mode
);
897 fd
= begin_multi_pcap_file(ctx
);
900 fd
= begin_single_pcap_file(ctx
);
904 printf("Running! Hang up with ^C!\n\n");
907 bug_on(gettimeofday(&start
, NULL
));
909 while (likely(sigint
== 0)) {
910 while (user_may_pull_from_rx(rx_ring
.frames
[it
].iov_base
)) {
913 hdr
= rx_ring
.frames
[it
].iov_base
;
914 packet
= ((uint8_t *) hdr
) + hdr
->tp_h
.tp_mac
;
917 if (ctx
->packet_type
!= -1)
918 if (ctx
->packet_type
!= hdr
->s_ll
.sll_pkttype
)
921 if (unlikely(ring_frame_size(&rx_ring
) < hdr
->tp_h
.tp_snaplen
)) {
926 if (dump_to_pcap(ctx
)) {
927 tpacket_hdr_to_pcap_pkthdr(&hdr
->tp_h
, &hdr
->s_ll
, &phdr
, ctx
->magic
);
929 ret
= __pcap_io
->write_pcap(fd
, &phdr
, ctx
->magic
, packet
,
930 pcap_get_length(&phdr
, ctx
->magic
));
931 if (unlikely(ret
!= pcap_get_total_length(&phdr
, ctx
->magic
)))
932 panic("Write error to pcap!\n");
935 show_frame_hdr(hdr
, ctx
->print_mode
);
937 dissector_entry_point(packet
, hdr
->tp_h
.tp_snaplen
,
938 ctx
->link_type
, ctx
->print_mode
);
940 if (frame_count_max
!= 0) {
941 if (frame_count
>= frame_count_max
) {
949 kernel_may_pull_from_rx(&hdr
->tp_h
);
952 if (it
>= rx_ring
.layout
.tp_frame_nr
)
955 if (unlikely(sigint
== 1))
958 if (dump_to_pcap(ctx
)) {
959 if (ctx
->dump_mode
== DUMP_INTERVAL_SIZE
) {
960 interval
+= hdr
->tp_h
.tp_snaplen
;
962 if (interval
> ctx
->dump_interval
) {
969 fd
= next_multi_pcap_file(ctx
, fd
);
973 print_pcap_file_stats(sock
, ctx
, skipped
);
978 poll(&rx_poll
, 1, -1);
979 poll_error_maybe_die(sock
, &rx_poll
);
982 bug_on(gettimeofday(&end
, NULL
));
983 diff
= tv_subtract(end
, start
);
985 if (!(ctx
->dump_dir
&& ctx
->print_mode
== PRINT_NONE
)) {
986 sock_print_net_stats(sock
, skipped
);
988 printf("\r%12lu sec, %lu usec in total\n",
989 diff
.tv_sec
, diff
.tv_usec
);
995 bpf_release(&bpf_ops
);
996 dissector_cleanup_all();
997 destroy_rx_ring(sock
, &rx_ring
);
999 if (ctx
->promiscuous
)
1000 leave_promiscuous_mode(ctx
->device_in
, ifflags
);
1003 leave_rfmon_mac80211(ctx
->device_trans
, ctx
->device_in
);
1005 if (dump_to_pcap(ctx
)) {
1007 finish_multi_pcap_file(ctx
, fd
);
1009 finish_single_pcap_file(ctx
, fd
);
1015 static void help(void)
1017 printf("\nnetsniff-ng %s, the packet sniffing beast\n", VERSION_STRING
);
1018 puts("http://www.netsniff-ng.org\n\n"
1019 "Usage: netsniff-ng [options] [filter-expression]\n"
1021 " -i|-d|--dev|--in <dev|pcap|-> Input source as netdev, pcap or pcap stdin\n"
1022 " -o|--out <dev|pcap|dir|cfg|-> Output sink as netdev, pcap, directory, trafgen, or stdout\n"
1023 " -f|--filter <bpf-file|expr> Use BPF filter file from bpfc or tcpdump-like expression\n"
1024 " -t|--type <type> Filter for: host|broadcast|multicast|others|outgoing\n"
1025 " -F|--interval <size|time> Dump interval if -o is a dir: <num>KiB/MiB/GiB/s/sec/min/hrs\n"
1026 " -J|--jumbo-support Support for 64KB Super Jumbo Frames (def: 2048B)\n"
1027 " -R|--rfraw Capture or inject raw 802.11 frames\n"
1028 " -n|--num <0|uint> Number of packets until exit (def: 0)\n"
1029 " -P|--prefix <name> Prefix for pcaps stored in directory\n"
1030 " -T|--magic <pcap-magic> Pcap magic number/pcap format to store, see -D\n"
1031 " -D|--dump-pcap-types Dump pcap types and magic numbers and quit\n"
1032 " -B|--dump-bpf Dump generated BPF assembly\n"
1033 " -r|--rand Randomize packet forwarding order (dev->dev)\n"
1034 " -M|--no-promisc No promiscuous mode for netdev\n"
1035 " -A|--no-sock-mem Don't tune core socket memory\n"
1036 " -m|--mmap Mmap(2) pcap file i.e., for replaying pcaps\n"
1037 " -G|--sg Scatter/gather pcap file I/O\n"
1038 " -c|--clrw Use slower read(2)/write(2) I/O\n"
1039 " -S|--ring-size <size> Specify ring size to: <num>KiB/MiB/GiB\n"
1040 " -k|--kernel-pull <uint> Kernel pull from user interval in us (def: 10us)\n"
1041 " -b|--bind-cpu <cpu> Bind to specific CPU\n"
1042 " -u|--user <userid> Drop privileges and change to userid\n"
1043 " -g|--group <groupid> Drop privileges and change to groupid\n"
1044 " -H|--prio-high Make this high priority process\n"
1045 " -Q|--notouch-irq Do not touch IRQ CPU affinity of NIC\n"
1046 " -s|--silent Do not print captured packets\n"
1047 " -q|--less Print less-verbose packet information\n"
1048 " -X|--hex Print packet data in hex format\n"
1049 " -l|--ascii Print human-readable packet data\n"
1050 " -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 -s -T 0xa1b2c3d4 --b 0 tcp or udp\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.cfg --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 vlan0 --out dump.pcap -c -u `id -u bob` -g `id -g bob`\n"
1061 " netsniff-ng --in any --filter http.bpf --jumbo-support --ascii -V\n\n"
1063 " For introducing bit errors, delays with random variation and more\n"
1064 " while replaying pcaps, make use of tc(8) with its disciplines (e.g. netem).\n\n"
1065 "Please report bugs to <bugs@netsniff-ng.org>\n"
1066 "Copyright (C) 2009-2013 Daniel Borkmann <daniel@netsniff-ng.org>\n"
1067 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel@netsniff-ng.org>\n"
1068 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1069 "License: GNU GPL version 2.0\n"
1070 "This is free software: you are free to change and redistribute it.\n"
1071 "There is NO WARRANTY, to the extent permitted by law.\n");
1075 static void version(void)
1077 printf("\nnetsniff-ng %s, the packet sniffing beast\n", VERSION_STRING
);
1078 puts("http://www.netsniff-ng.org\n\n"
1079 "Please report bugs to <bugs@netsniff-ng.org>\n"
1080 "Copyright (C) 2009-2013 Daniel Borkmann <daniel@netsniff-ng.org>\n"
1081 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel@netsniff-ng.org>\n"
1082 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1083 "License: GNU GPL version 2.0\n"
1084 "This is free software: you are free to change and redistribute it.\n"
1085 "There is NO WARRANTY, to the extent permitted by law.\n");
1089 int main(int argc
, char **argv
)
1092 int c
, i
, j
, cpu_tmp
, opt_index
, ops_touched
= 0, vals
[4] = {0};
1093 bool prio_high
= false, setsockmem
= true;
1094 void (*main_loop
)(struct ctx
*ctx
) = NULL
;
1096 .link_type
= LINKTYPE_EN10MB
,
1097 .print_mode
= PRINT_NORM
,
1100 .promiscuous
= true,
1102 .pcap
= PCAP_OPS_SG
,
1103 .dump_interval
= 60,
1104 .dump_mode
= DUMP_INTERVAL_TIME
,
1107 .magic
= ORIGINAL_TCPDUMP_MAGIC
,
1112 while ((c
= getopt_long(argc
, argv
, short_options
, long_options
,
1113 &opt_index
)) != EOF
) {
1117 ctx
.device_in
= xstrdup(optarg
);
1120 ctx
.device_out
= xstrdup(optarg
);
1123 ctx
.prefix
= xstrdup(optarg
);
1126 ctx
.link_type
= LINKTYPE_IEEE802_11
;
1130 ctx
.randomize
= true;
1136 ctx
.magic
= (uint32_t) strtoul(optarg
, NULL
, 0);
1137 pcap_check_magic(ctx
.magic
);
1140 ctx
.filter
= xstrdup(optarg
);
1143 ctx
.promiscuous
= false;
1149 ctx
.uid
= strtoul(optarg
, NULL
, 0);
1153 ctx
.gid
= strtoul(optarg
, NULL
, 0);
1157 if (!strncmp(optarg
, "host", strlen("host")))
1158 ctx
.packet_type
= PACKET_HOST
;
1159 else if (!strncmp(optarg
, "broadcast", strlen("broadcast")))
1160 ctx
.packet_type
= PACKET_BROADCAST
;
1161 else if (!strncmp(optarg
, "multicast", strlen("multicast")))
1162 ctx
.packet_type
= PACKET_MULTICAST
;
1163 else if (!strncmp(optarg
, "others", strlen("others")))
1164 ctx
.packet_type
= PACKET_OTHERHOST
;
1165 else if (!strncmp(optarg
, "outgoing", strlen("outgoing")))
1166 ctx
.packet_type
= PACKET_OUTGOING
;
1168 ctx
.packet_type
= -1;
1172 ctx
.reserve_size
= 0;
1174 for (j
= i
= strlen(optarg
); i
> 0; --i
) {
1175 if (!isdigit(optarg
[j
- i
]))
1180 if (!strncmp(ptr
, "KiB", strlen("KiB")))
1181 ctx
.reserve_size
= 1 << 10;
1182 else if (!strncmp(ptr
, "MiB", strlen("MiB")))
1183 ctx
.reserve_size
= 1 << 20;
1184 else if (!strncmp(ptr
, "GiB", strlen("GiB")))
1185 ctx
.reserve_size
= 1 << 30;
1187 panic("Syntax error in ring size param!\n");
1190 ctx
.reserve_size
*= strtol(optarg
, NULL
, 0);
1193 cpu_tmp
= strtol(optarg
, NULL
, 0);
1195 cpu_affinity(cpu_tmp
);
1203 ctx
.pcap
= PCAP_OPS_RW
;
1207 ctx
.pcap
= PCAP_OPS_MM
;
1211 ctx
.pcap
= PCAP_OPS_SG
;
1218 ctx
.print_mode
= PRINT_NONE
;
1221 ctx
.print_mode
= PRINT_LESS
;
1225 (ctx
.print_mode
== PRINT_ASCII
) ?
1226 PRINT_HEX_ASCII
: PRINT_HEX
;
1230 (ctx
.print_mode
== PRINT_HEX
) ?
1231 PRINT_HEX_ASCII
: PRINT_ASCII
;
1234 ctx
.kpull
= strtol(optarg
, NULL
, 0);
1237 frame_count_max
= strtol(optarg
, NULL
, 0);
1241 ctx
.dump_interval
= 0;
1243 for (j
= i
= strlen(optarg
); i
> 0; --i
) {
1244 if (!isdigit(optarg
[j
- i
]))
1249 if (!strncmp(ptr
, "KiB", strlen("KiB"))) {
1250 ctx
.dump_interval
= 1 << 10;
1251 ctx
.dump_mode
= DUMP_INTERVAL_SIZE
;
1252 } else if (!strncmp(ptr
, "MiB", strlen("MiB"))) {
1253 ctx
.dump_interval
= 1 << 20;
1254 ctx
.dump_mode
= DUMP_INTERVAL_SIZE
;
1255 } else if (!strncmp(ptr
, "GiB", strlen("GiB"))) {
1256 ctx
.dump_interval
= 1 << 30;
1257 ctx
.dump_mode
= DUMP_INTERVAL_SIZE
;
1258 } else if (!strncmp(ptr
, "sec", strlen("sec"))) {
1259 ctx
.dump_interval
= 1;
1260 ctx
.dump_mode
= DUMP_INTERVAL_TIME
;
1261 } else if (!strncmp(ptr
, "min", strlen("min"))) {
1262 ctx
.dump_interval
= 60;
1263 ctx
.dump_mode
= DUMP_INTERVAL_TIME
;
1264 } else if (!strncmp(ptr
, "hrs", strlen("hrs"))) {
1265 ctx
.dump_interval
= 60 * 60;
1266 ctx
.dump_mode
= DUMP_INTERVAL_TIME
;
1267 } else if (!strncmp(ptr
, "s", strlen("s"))) {
1268 ctx
.dump_interval
= 1;
1269 ctx
.dump_mode
= DUMP_INTERVAL_TIME
;
1271 panic("Syntax error in time/size param!\n");
1275 ctx
.dump_interval
*= strtol(optarg
, NULL
, 0);
1281 ctx
.dump_bpf
= true;
1284 pcap_dump_type_features();
1310 panic("Option -%c requires an argument!\n",
1313 if (isprint(optopt
))
1314 printf("Unknown option character `0x%X\'!\n", optopt
);
1322 if (!ctx
.filter
&& optind
!= argc
) {
1326 for (i
= optind
; i
< argc
; ++i
) {
1327 size_t alen
= strlen(argv
[i
]) + 2;
1328 size_t flen
= ctx
.filter
? strlen(ctx
.filter
) : 0;
1330 ctx
.filter
= xrealloc(ctx
.filter
, 1, flen
+ alen
);
1331 ret
= slprintf(ctx
.filter
+ offset
, strlen(argv
[i
]) + 2, "%s ", argv
[i
]);
1333 panic("Cannot concatenate filter string!\n");
1340 ctx
.device_in
= xstrdup("any");
1342 register_signal(SIGINT
, signal_handler
);
1343 register_signal(SIGHUP
, signal_handler
);
1348 set_proc_prio(get_default_proc_prio());
1349 set_sched_status(get_default_sched_policy(), get_default_sched_prio());
1352 if (ctx
.device_in
&& (device_mtu(ctx
.device_in
) ||
1353 !strncmp("any", ctx
.device_in
, strlen(ctx
.device_in
)))) {
1354 if (!ctx
.device_out
) {
1356 main_loop
= recv_only_or_dump
;
1357 } else if (device_mtu(ctx
.device_out
)) {
1358 register_signal_f(SIGALRM
, timer_elapsed
, SA_SIGINFO
);
1359 main_loop
= receive_to_xmit
;
1362 register_signal_f(SIGALRM
, timer_next_dump
, SA_SIGINFO
);
1363 main_loop
= recv_only_or_dump
;
1365 ctx
.pcap
= PCAP_OPS_SG
;
1368 if (ctx
.device_out
&& device_mtu(ctx
.device_out
)) {
1369 register_signal_f(SIGALRM
, timer_elapsed
, SA_SIGINFO
);
1370 main_loop
= pcap_to_xmit
;
1372 ctx
.pcap
= PCAP_OPS_MM
;
1374 main_loop
= read_pcap
;
1376 ctx
.pcap
= PCAP_OPS_SG
;
1383 set_system_socket_memory(vals
, array_size(vals
));
1390 reset_system_socket_memory(vals
, array_size(vals
));
1394 free(ctx
.device_in
);
1395 free(ctx
.device_out
);
1396 free(ctx
.device_trans
);