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
;
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:B:HQmcsqXlvhF:RGAP:Vu:g:T:D";
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 {"magic", required_argument
, NULL
, 'T'},
89 {"rand", no_argument
, NULL
, 'r'},
90 {"rfraw", no_argument
, NULL
, 'R'},
91 {"mmap", no_argument
, NULL
, 'm'},
92 {"sg", no_argument
, NULL
, 'G'},
93 {"clrw", no_argument
, NULL
, 'c'},
94 {"jumbo-support", no_argument
, NULL
, 'J'},
95 {"no-promisc", no_argument
, NULL
, 'M'},
96 {"prio-high", no_argument
, NULL
, 'H'},
97 {"notouch-irq", no_argument
, NULL
, 'Q'},
98 {"dump-pcap-types", no_argument
, NULL
, 'D'},
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 set_system_socket_memory(vals) \
118 if ((vals[0] = get_system_socket_mem(sock_rmem_max)) < SMEM_SUG_MAX) \
119 set_system_socket_mem(sock_rmem_max, SMEM_SUG_MAX); \
120 if ((vals[1] = get_system_socket_mem(sock_rmem_def)) < SMEM_SUG_DEF) \
121 set_system_socket_mem(sock_rmem_def, SMEM_SUG_DEF); \
122 if ((vals[2] = get_system_socket_mem(sock_wmem_max)) < SMEM_SUG_MAX) \
123 set_system_socket_mem(sock_wmem_max, SMEM_SUG_MAX); \
124 if ((vals[3] = get_system_socket_mem(sock_wmem_def)) < SMEM_SUG_DEF) \
125 set_system_socket_mem(sock_wmem_def, SMEM_SUG_DEF); \
128 #define reset_system_socket_memory(vals) \
130 set_system_socket_mem(sock_rmem_max, vals[0]); \
131 set_system_socket_mem(sock_rmem_def, vals[1]); \
132 set_system_socket_mem(sock_wmem_max, vals[2]); \
133 set_system_socket_mem(sock_wmem_def, vals[3]); \
136 #define __pcap_io pcap_ops[ctx->pcap]
138 static void signal_handler(int number
)
149 static void timer_elapsed(int unused
)
151 itimer
.it_interval
.tv_sec
= 0;
152 itimer
.it_interval
.tv_usec
= interval
;
154 itimer
.it_value
.tv_sec
= 0;
155 itimer
.it_value
.tv_usec
= interval
;
157 pull_and_flush_tx_ring(tx_sock
);
158 setitimer(ITIMER_REAL
, &itimer
, NULL
);
161 static void timer_next_dump(int unused
)
163 itimer
.it_interval
.tv_sec
= interval
;
164 itimer
.it_interval
.tv_usec
= 0;
166 itimer
.it_value
.tv_sec
= interval
;
167 itimer
.it_value
.tv_usec
= 0;
170 setitimer(ITIMER_REAL
, &itimer
, NULL
);
173 static inline bool dump_to_pcap(struct ctx
*ctx
)
178 static void pcap_to_xmit(struct ctx
*ctx
)
182 int irq
, ifindex
, fd
= 0, ret
;
183 unsigned int size
, it
= 0;
184 unsigned long trunced
= 0;
186 struct frame_map
*hdr
;
187 struct sock_fprog bpf_ops
;
188 struct timeval start
, end
, diff
;
191 if (!device_up_and_running(ctx
->device_out
) && !ctx
->rfraw
)
192 panic("Device not up and running!\n");
196 tx_sock
= pf_socket();
198 fd
= open_or_die(ctx
->device_in
, O_RDONLY
| O_LARGEFILE
| O_NOATIME
);
200 ret
= __pcap_io
->pull_fhdr_pcap(fd
, &ctx
->magic
, &ctx
->link_type
);
202 panic("Error reading pcap header!\n");
204 if (__pcap_io
->prepare_access_pcap
) {
205 ret
= __pcap_io
->prepare_access_pcap(fd
, PCAP_MODE_RD
, ctx
->jumbo
);
207 panic("Error prepare reading pcap!\n");
210 fmemset(&tx_ring
, 0, sizeof(tx_ring
));
211 fmemset(&bpf_ops
, 0, sizeof(bpf_ops
));
214 ctx
->device_trans
= xstrdup(ctx
->device_out
);
215 xfree(ctx
->device_out
);
217 enter_rfmon_mac80211(ctx
->device_trans
, &ctx
->device_out
);
218 if (ctx
->link_type
!= LINKTYPE_IEEE802_11
)
219 panic("Wrong linktype of pcap!\n");
222 ifindex
= device_ifindex(ctx
->device_out
);
224 size
= ring_size(ctx
->device_out
, ctx
->reserve_size
);
226 bpf_parse_rules(ctx
->device_out
, ctx
->filter
, &bpf_ops
);
228 set_packet_loss_discard(tx_sock
);
229 set_sockopt_hwtimestamp(tx_sock
, ctx
->device_out
);
231 setup_tx_ring_layout(tx_sock
, &tx_ring
, size
, ctx
->jumbo
);
232 create_tx_ring(tx_sock
, &tx_ring
, ctx
->verbose
);
233 mmap_tx_ring(tx_sock
, &tx_ring
);
234 alloc_tx_ring_frames(&tx_ring
);
235 bind_tx_ring(tx_sock
, &tx_ring
, ifindex
);
237 dissector_init_all(ctx
->print_mode
);
239 if (ctx
->cpu
>= 0 && ifindex
> 0) {
240 irq
= device_irq_number(ctx
->device_out
);
241 device_bind_irq_to_cpu(irq
, ctx
->cpu
);
244 printf("IRQ: %s:%d > CPU%d\n",
245 ctx
->device_out
, irq
, ctx
->cpu
);
249 interval
= ctx
->kpull
;
253 bpf_dump_all(&bpf_ops
);
255 printf("MD: TX %luus %s ", interval
, pcap_ops_group_to_str
[ctx
->pcap
]);
257 printf("802.11 raw via %s ", ctx
->device_out
);
258 #ifdef _LARGEFILE64_SOURCE
265 itimer
.it_interval
.tv_sec
= 0;
266 itimer
.it_interval
.tv_usec
= interval
;
268 itimer
.it_value
.tv_sec
= 0;
269 itimer
.it_value
.tv_usec
= interval
;
271 setitimer(ITIMER_REAL
, &itimer
, NULL
);
273 drop_privileges(ctx
->enforce
, ctx
->uid
, ctx
->gid
);
275 printf("Running! Hang up with ^C!\n\n");
278 bug_on(gettimeofday(&start
, NULL
));
280 while (likely(sigint
== 0)) {
281 while (user_may_pull_from_tx(tx_ring
.frames
[it
].iov_base
)) {
282 hdr
= tx_ring
.frames
[it
].iov_base
;
283 out
= ((uint8_t *) hdr
) + TPACKET2_HDRLEN
- sizeof(struct sockaddr_ll
);
286 ret
= __pcap_io
->read_pcap(fd
, &phdr
, ctx
->magic
, out
,
287 ring_frame_size(&tx_ring
));
288 if (unlikely(ret
<= 0))
291 if (ring_frame_size(&tx_ring
) <
292 pcap_get_length(&phdr
, ctx
->magic
)) {
293 pcap_set_length(&phdr
, ctx
->magic
,
294 ring_frame_size(&tx_ring
));
297 } while (ctx
->filter
&&
298 !bpf_run_filter(&bpf_ops
, out
,
299 pcap_get_length(&phdr
, ctx
->magic
)));
301 pcap_pkthdr_to_tpacket_hdr(&phdr
, ctx
->magic
, &hdr
->tp_h
, &hdr
->s_ll
);
303 ctx
->tx_bytes
+= hdr
->tp_h
.tp_len
;;
306 show_frame_hdr(hdr
, ctx
->print_mode
, RING_MODE_EGRESS
);
308 dissector_entry_point(out
, hdr
->tp_h
.tp_snaplen
,
309 ctx
->link_type
, ctx
->print_mode
);
311 kernel_may_pull_from_tx(&hdr
->tp_h
);
314 if (it
>= tx_ring
.layout
.tp_frame_nr
)
317 if (unlikely(sigint
== 1))
320 if (frame_count_max
!= 0) {
321 if (ctx
->tx_packets
>= frame_count_max
) {
331 bug_on(gettimeofday(&end
, NULL
));
332 diff
= tv_subtract(end
, start
);
334 bpf_release(&bpf_ops
);
336 dissector_cleanup_all();
337 destroy_tx_ring(tx_sock
, &tx_ring
);
340 leave_rfmon_mac80211(ctx
->device_trans
, ctx
->device_out
);
342 if (__pcap_io
->prepare_close_pcap
)
343 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_RD
);
350 printf("\r%12lu packets outgoing\n", ctx
->tx_packets
);
351 printf("\r%12lu packets truncated in file\n", trunced
);
352 printf("\r%12lu bytes outgoing\n", ctx
->tx_bytes
);
353 printf("\r%12lu sec, %lu usec in total\n", diff
.tv_sec
, diff
.tv_usec
);
356 static void receive_to_xmit(struct ctx
*ctx
)
360 int rx_sock
, ifindex_in
, ifindex_out
;
361 unsigned int size_in
, size_out
, it_in
= 0, it_out
= 0;
362 unsigned long frame_count
= 0;
363 struct frame_map
*hdr_in
, *hdr_out
;
364 struct ring tx_ring
, rx_ring
;
365 struct pollfd rx_poll
;
366 struct sock_fprog bpf_ops
;
368 if (!strncmp(ctx
->device_in
, ctx
->device_out
, IFNAMSIZ
))
369 panic("Ingress/egress devices must be different!\n");
370 if (!device_up_and_running(ctx
->device_out
))
371 panic("Egress device not up and running!\n");
372 if (!device_up_and_running(ctx
->device_in
))
373 panic("Ingress device not up and running!\n");
375 rx_sock
= pf_socket();
376 tx_sock
= pf_socket();
378 fmemset(&tx_ring
, 0, sizeof(tx_ring
));
379 fmemset(&rx_ring
, 0, sizeof(rx_ring
));
380 fmemset(&rx_poll
, 0, sizeof(rx_poll
));
381 fmemset(&bpf_ops
, 0, sizeof(bpf_ops
));
383 ifindex_in
= device_ifindex(ctx
->device_in
);
384 ifindex_out
= device_ifindex(ctx
->device_out
);
386 size_in
= ring_size(ctx
->device_in
, ctx
->reserve_size
);
387 size_out
= ring_size(ctx
->device_out
, ctx
->reserve_size
);
389 enable_kernel_bpf_jit_compiler();
391 bpf_parse_rules(ctx
->device_in
, ctx
->filter
, &bpf_ops
);
392 bpf_attach_to_sock(rx_sock
, &bpf_ops
);
394 setup_rx_ring_layout(rx_sock
, &rx_ring
, size_in
, ctx
->jumbo
);
395 create_rx_ring(rx_sock
, &rx_ring
, ctx
->verbose
);
396 mmap_rx_ring(rx_sock
, &rx_ring
);
397 alloc_rx_ring_frames(&rx_ring
);
398 bind_rx_ring(rx_sock
, &rx_ring
, ifindex_in
);
399 prepare_polling(rx_sock
, &rx_poll
);
401 set_packet_loss_discard(tx_sock
);
402 setup_tx_ring_layout(tx_sock
, &tx_ring
, size_out
, ctx
->jumbo
);
403 create_tx_ring(tx_sock
, &tx_ring
, ctx
->verbose
);
404 mmap_tx_ring(tx_sock
, &tx_ring
);
405 alloc_tx_ring_frames(&tx_ring
);
406 bind_tx_ring(tx_sock
, &tx_ring
, ifindex_out
);
408 dissector_init_all(ctx
->print_mode
);
410 if (ctx
->promiscuous
)
411 ifflags
= enter_promiscuous_mode(ctx
->device_in
);
414 interval
= ctx
->kpull
;
416 itimer
.it_interval
.tv_sec
= 0;
417 itimer
.it_interval
.tv_usec
= interval
;
419 itimer
.it_value
.tv_sec
= 0;
420 itimer
.it_value
.tv_usec
= interval
;
422 setitimer(ITIMER_REAL
, &itimer
, NULL
);
426 bpf_dump_all(&bpf_ops
);
428 printf("MD: RXTX %luus\n\n", interval
);
431 drop_privileges(ctx
->enforce
, ctx
->uid
, ctx
->gid
);
433 printf("Running! Hang up with ^C!\n\n");
436 while (likely(sigint
== 0)) {
437 while (user_may_pull_from_rx(rx_ring
.frames
[it_in
].iov_base
)) {
440 hdr_in
= rx_ring
.frames
[it_in
].iov_base
;
441 in
= ((uint8_t *) hdr_in
) + hdr_in
->tp_h
.tp_mac
;
445 if (ctx
->packet_type
!= -1)
446 if (ctx
->packet_type
!= hdr_in
->s_ll
.sll_pkttype
)
449 hdr_out
= tx_ring
.frames
[it_out
].iov_base
;
450 out
= ((uint8_t *) hdr_out
) + TPACKET2_HDRLEN
- sizeof(struct sockaddr_ll
);
452 for (; !user_may_pull_from_tx(tx_ring
.frames
[it_out
].iov_base
) &&
455 next_rnd_slot(&it_out
, &tx_ring
);
458 if (it_out
>= tx_ring
.layout
.tp_frame_nr
)
462 hdr_out
= tx_ring
.frames
[it_out
].iov_base
;
463 out
= ((uint8_t *) hdr_out
) + TPACKET2_HDRLEN
- sizeof(struct sockaddr_ll
);
466 tpacket_hdr_clone(&hdr_out
->tp_h
, &hdr_in
->tp_h
);
467 fmemcpy(out
, in
, hdr_in
->tp_h
.tp_len
);
469 kernel_may_pull_from_tx(&hdr_out
->tp_h
);
471 next_rnd_slot(&it_out
, &tx_ring
);
474 if (it_out
>= tx_ring
.layout
.tp_frame_nr
)
478 show_frame_hdr(hdr_in
, ctx
->print_mode
, RING_MODE_INGRESS
);
480 dissector_entry_point(in
, hdr_in
->tp_h
.tp_snaplen
,
481 ctx
->link_type
, ctx
->print_mode
);
483 if (frame_count_max
!= 0) {
484 if (frame_count
>= frame_count_max
) {
492 kernel_may_pull_from_rx(&hdr_in
->tp_h
);
495 if (it_in
>= rx_ring
.layout
.tp_frame_nr
)
498 if (unlikely(sigint
== 1))
502 poll(&rx_poll
, 1, -1);
503 poll_error_maybe_die(rx_sock
, &rx_poll
);
508 sock_print_net_stats(rx_sock
, 0);
510 bpf_release(&bpf_ops
);
512 dissector_cleanup_all();
514 destroy_tx_ring(tx_sock
, &tx_ring
);
515 destroy_rx_ring(rx_sock
, &rx_ring
);
517 if (ctx
->promiscuous
)
518 leave_promiscuous_mode(ctx
->device_in
, ifflags
);
524 static void translate_pcap_to_txf(int fdo
, uint8_t *out
, size_t len
)
526 size_t bytes_done
= 0;
529 slprintf(bout
, sizeof(bout
), "{\n ");
530 write_or_die(fdo
, bout
, strlen(bout
));
532 while (bytes_done
< len
) {
533 slprintf(bout
, sizeof(bout
), "0x%02x, ", out
[bytes_done
]);
534 write_or_die(fdo
, bout
, strlen(bout
));
538 if (bytes_done
% 10 == 0) {
539 slprintf(bout
, sizeof(bout
), "\n");
540 write_or_die(fdo
, bout
, strlen(bout
));
542 if (bytes_done
< len
) {
543 slprintf(bout
, sizeof(bout
), " ");
544 write_or_die(fdo
, bout
, strlen(bout
));
548 if (bytes_done
% 10 != 0) {
549 slprintf(bout
, sizeof(bout
), "\n");
550 write_or_die(fdo
, bout
, strlen(bout
));
553 slprintf(bout
, sizeof(bout
), "}\n\n");
554 write_or_die(fdo
, bout
, strlen(bout
));
557 static void read_pcap(struct ctx
*ctx
)
561 int ret
, fd
, fdo
= 0;
562 unsigned long trunced
= 0;
565 struct sock_fprog bpf_ops
;
567 struct timeval start
, end
, diff
;
568 struct sockaddr_ll sll
;
572 fd
= open_or_die(ctx
->device_in
, O_RDONLY
| O_LARGEFILE
| O_NOATIME
);
574 ret
= __pcap_io
->pull_fhdr_pcap(fd
, &ctx
->magic
, &ctx
->link_type
);
576 panic("Error reading pcap header!\n");
578 if (__pcap_io
->prepare_access_pcap
) {
579 ret
= __pcap_io
->prepare_access_pcap(fd
, PCAP_MODE_RD
, ctx
->jumbo
);
581 panic("Error prepare reading pcap!\n");
584 fmemset(&fm
, 0, sizeof(fm
));
585 fmemset(&bpf_ops
, 0, sizeof(bpf_ops
));
587 bpf_parse_rules("any", ctx
->filter
, &bpf_ops
);
589 dissector_init_all(ctx
->print_mode
);
591 out_len
= round_up(1024 * 1024, PAGE_SIZE
);
592 out
= xmalloc_aligned(out_len
, CO_CACHE_LINE_SIZE
);
596 bpf_dump_all(&bpf_ops
);
598 printf("MD: RD %s ", pcap_ops_group_to_str
[ctx
->pcap
]);
599 #ifdef _LARGEFILE64_SOURCE
607 fdo
= open_or_die_m(ctx
->device_out
, O_RDWR
| O_CREAT
|
608 O_TRUNC
| O_LARGEFILE
, DEFFILEMODE
);
610 drop_privileges(ctx
->enforce
, ctx
->uid
, ctx
->gid
);
612 printf("Running! Hang up with ^C!\n\n");
615 bug_on(gettimeofday(&start
, NULL
));
617 while (likely(sigint
== 0)) {
619 ret
= __pcap_io
->read_pcap(fd
, &phdr
, ctx
->magic
,
621 if (unlikely(ret
< 0))
624 if (unlikely(pcap_get_length(&phdr
, ctx
->magic
) == 0)) {
629 if (unlikely(pcap_get_length(&phdr
, ctx
->magic
) > out_len
)) {
630 pcap_set_length(&phdr
, ctx
->magic
, out_len
);
633 } while (ctx
->filter
&&
634 !bpf_run_filter(&bpf_ops
, out
,
635 pcap_get_length(&phdr
, ctx
->magic
)));
637 pcap_pkthdr_to_tpacket_hdr(&phdr
, ctx
->magic
, &fm
.tp_h
, &sll
);
639 ctx
->tx_bytes
+= fm
.tp_h
.tp_len
;
642 show_frame_hdr(&fm
, ctx
->print_mode
, RING_MODE_EGRESS
);
644 dissector_entry_point(out
, fm
.tp_h
.tp_snaplen
,
645 ctx
->link_type
, ctx
->print_mode
);
648 translate_pcap_to_txf(fdo
, out
, fm
.tp_h
.tp_snaplen
);
650 if (frame_count_max
!= 0) {
651 if (ctx
->tx_packets
>= frame_count_max
) {
660 bug_on(gettimeofday(&end
, NULL
));
661 diff
= tv_subtract(end
, start
);
663 bpf_release(&bpf_ops
);
665 dissector_cleanup_all();
667 if (__pcap_io
->prepare_close_pcap
)
668 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_RD
);
678 printf("\r%12lu packets outgoing\n", ctx
->tx_packets
);
679 printf("\r%12lu packets truncated in file\n", trunced
);
680 printf("\r%12lu bytes outgoing\n", ctx
->tx_bytes
);
681 printf("\r%12lu sec, %lu usec in total\n", diff
.tv_sec
, diff
.tv_usec
);
684 static void finish_multi_pcap_file(struct ctx
*ctx
, int fd
)
686 __pcap_io
->fsync_pcap(fd
);
688 if (__pcap_io
->prepare_close_pcap
)
689 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_WR
);
693 fmemset(&itimer
, 0, sizeof(itimer
));
694 setitimer(ITIMER_REAL
, &itimer
, NULL
);
697 static int next_multi_pcap_file(struct ctx
*ctx
, int fd
)
702 __pcap_io
->fsync_pcap(fd
);
704 if (__pcap_io
->prepare_close_pcap
)
705 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_WR
);
709 slprintf(fname
, sizeof(fname
), "%s/%s%lu.pcap", ctx
->device_out
,
710 ctx
->prefix
? : "dump-", time(0));
712 fd
= open_or_die_m(fname
, O_RDWR
| O_CREAT
| O_TRUNC
|
713 O_LARGEFILE
, DEFFILEMODE
);
715 ret
= __pcap_io
->push_fhdr_pcap(fd
, ctx
->magic
, ctx
->link_type
);
717 panic("Error writing pcap header!\n");
719 if (__pcap_io
->prepare_access_pcap
) {
720 ret
= __pcap_io
->prepare_access_pcap(fd
, PCAP_MODE_WR
, ctx
->jumbo
);
722 panic("Error prepare writing pcap!\n");
728 static int begin_multi_pcap_file(struct ctx
*ctx
)
735 if (ctx
->device_out
[strlen(ctx
->device_out
) - 1] == '/')
736 ctx
->device_out
[strlen(ctx
->device_out
) - 1] = 0;
738 slprintf(fname
, sizeof(fname
), "%s/%s%lu.pcap", ctx
->device_out
,
739 ctx
->prefix
? : "dump-", time(0));
741 fd
= open_or_die_m(fname
, O_RDWR
| O_CREAT
| O_TRUNC
|
742 O_LARGEFILE
, DEFFILEMODE
);
744 ret
= __pcap_io
->push_fhdr_pcap(fd
, ctx
->magic
, ctx
->link_type
);
746 panic("Error writing pcap header!\n");
748 if (__pcap_io
->prepare_access_pcap
) {
749 ret
= __pcap_io
->prepare_access_pcap(fd
, PCAP_MODE_WR
, ctx
->jumbo
);
751 panic("Error prepare writing pcap!\n");
754 if (ctx
->dump_mode
== DUMP_INTERVAL_TIME
) {
755 interval
= ctx
->dump_interval
;
757 itimer
.it_interval
.tv_sec
= interval
;
758 itimer
.it_interval
.tv_usec
= 0;
760 itimer
.it_value
.tv_sec
= interval
;
761 itimer
.it_value
.tv_usec
= 0;
763 setitimer(ITIMER_REAL
, &itimer
, NULL
);
771 static void finish_single_pcap_file(struct ctx
*ctx
, int fd
)
773 __pcap_io
->fsync_pcap(fd
);
775 if (__pcap_io
->prepare_close_pcap
)
776 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_WR
);
781 static int begin_single_pcap_file(struct ctx
*ctx
)
787 fd
= open_or_die_m(ctx
->device_out
, O_RDWR
| O_CREAT
| O_TRUNC
|
788 O_LARGEFILE
, DEFFILEMODE
);
790 ret
= __pcap_io
->push_fhdr_pcap(fd
, ctx
->magic
, ctx
->link_type
);
792 panic("Error writing pcap header!\n");
794 if (__pcap_io
->prepare_access_pcap
) {
795 ret
= __pcap_io
->prepare_access_pcap(fd
, PCAP_MODE_WR
, ctx
->jumbo
);
797 panic("Error prepare writing pcap!\n");
803 static void print_pcap_file_stats(int sock
, struct ctx
*ctx
, unsigned long skipped
)
805 unsigned long good
, bad
;
806 struct tpacket_stats kstats
;
807 socklen_t slen
= sizeof(kstats
);
809 fmemset(&kstats
, 0, sizeof(kstats
));
810 getsockopt(sock
, SOL_PACKET
, PACKET_STATISTICS
, &kstats
, &slen
);
812 if (ctx
->print_mode
== PRINT_NONE
) {
813 good
= kstats
.tp_packets
- kstats
.tp_drops
- skipped
;
814 bad
= kstats
.tp_drops
+ skipped
;
816 printf(".(+%lu/-%lu)", good
, bad
);
821 static void recv_only_or_dump(struct ctx
*ctx
)
825 int sock
, irq
, ifindex
, fd
= 0, ret
;
826 unsigned int size
, it
= 0;
827 unsigned long frame_count
= 0, skipped
= 0;
829 struct pollfd rx_poll
;
830 struct frame_map
*hdr
;
831 struct sock_fprog bpf_ops
;
832 struct timeval start
, end
, diff
;
835 if (!device_up_and_running(ctx
->device_in
) && !ctx
->rfraw
)
836 panic("Device not up and running!\n");
841 ctx
->device_trans
= xstrdup(ctx
->device_in
);
842 xfree(ctx
->device_in
);
844 enter_rfmon_mac80211(ctx
->device_trans
, &ctx
->device_in
);
845 ctx
->link_type
= LINKTYPE_IEEE802_11
;
848 fmemset(&rx_ring
, 0, sizeof(rx_ring
));
849 fmemset(&rx_poll
, 0, sizeof(rx_poll
));
850 fmemset(&bpf_ops
, 0, sizeof(bpf_ops
));
852 ifindex
= device_ifindex(ctx
->device_in
);
854 size
= ring_size(ctx
->device_in
, ctx
->reserve_size
);
856 enable_kernel_bpf_jit_compiler();
858 bpf_parse_rules(ctx
->device_in
, ctx
->filter
, &bpf_ops
);
859 bpf_attach_to_sock(sock
, &bpf_ops
);
861 set_sockopt_hwtimestamp(sock
, ctx
->device_in
);
863 setup_rx_ring_layout(sock
, &rx_ring
, size
, ctx
->jumbo
);
864 create_rx_ring(sock
, &rx_ring
, ctx
->verbose
);
865 mmap_rx_ring(sock
, &rx_ring
);
866 alloc_rx_ring_frames(&rx_ring
);
867 bind_rx_ring(sock
, &rx_ring
, ifindex
);
869 prepare_polling(sock
, &rx_poll
);
870 dissector_init_all(ctx
->print_mode
);
872 if (ctx
->cpu
>= 0 && ifindex
> 0) {
873 irq
= device_irq_number(ctx
->device_in
);
874 device_bind_irq_to_cpu(irq
, ctx
->cpu
);
877 printf("IRQ: %s:%d > CPU%d\n",
878 ctx
->device_in
, irq
, ctx
->cpu
);
881 if (ctx
->promiscuous
)
882 ifflags
= enter_promiscuous_mode(ctx
->device_in
);
886 bpf_dump_all(&bpf_ops
);
888 printf("MD: RX %s ", ctx
->dump
? pcap_ops_group_to_str
[ctx
->pcap
] : "");
890 printf("802.11 raw via %s ", ctx
->device_in
);
891 #ifdef _LARGEFILE64_SOURCE
898 drop_privileges(ctx
->enforce
, ctx
->uid
, ctx
->gid
);
900 if (dump_to_pcap(ctx
)) {
904 fmemset(&stats
, 0, sizeof(stats
));
905 ret
= stat(ctx
->device_out
, &stats
);
911 ctx
->dump_dir
= S_ISDIR(stats
.st_mode
);
913 fd
= begin_multi_pcap_file(ctx
);
916 fd
= begin_single_pcap_file(ctx
);
920 printf("Running! Hang up with ^C!\n\n");
923 bug_on(gettimeofday(&start
, NULL
));
925 while (likely(sigint
== 0)) {
926 while (user_may_pull_from_rx(rx_ring
.frames
[it
].iov_base
)) {
929 hdr
= rx_ring
.frames
[it
].iov_base
;
930 packet
= ((uint8_t *) hdr
) + hdr
->tp_h
.tp_mac
;
933 if (ctx
->packet_type
!= -1)
934 if (ctx
->packet_type
!= hdr
->s_ll
.sll_pkttype
)
937 if (unlikely(ring_frame_size(&rx_ring
) < hdr
->tp_h
.tp_snaplen
)) {
942 if (dump_to_pcap(ctx
)) {
943 tpacket_hdr_to_pcap_pkthdr(&hdr
->tp_h
, &hdr
->s_ll
, &phdr
, ctx
->magic
);
945 ret
= __pcap_io
->write_pcap(fd
, &phdr
, ctx
->magic
, packet
,
946 pcap_get_length(&phdr
, ctx
->magic
));
947 if (unlikely(ret
!= pcap_get_total_length(&phdr
, ctx
->magic
)))
948 panic("Write error to pcap!\n");
951 show_frame_hdr(hdr
, ctx
->print_mode
, RING_MODE_INGRESS
);
953 dissector_entry_point(packet
, hdr
->tp_h
.tp_snaplen
,
954 ctx
->link_type
, ctx
->print_mode
);
956 if (frame_count_max
!= 0) {
957 if (frame_count
>= frame_count_max
) {
965 kernel_may_pull_from_rx(&hdr
->tp_h
);
968 if (it
>= rx_ring
.layout
.tp_frame_nr
)
971 if (unlikely(sigint
== 1))
974 if (dump_to_pcap(ctx
)) {
975 if (ctx
->dump_mode
== DUMP_INTERVAL_SIZE
) {
976 interval
+= hdr
->tp_h
.tp_snaplen
;
978 if (interval
> ctx
->dump_interval
) {
985 fd
= next_multi_pcap_file(ctx
, fd
);
989 print_pcap_file_stats(sock
, ctx
, skipped
);
994 poll(&rx_poll
, 1, -1);
995 poll_error_maybe_die(sock
, &rx_poll
);
998 bug_on(gettimeofday(&end
, NULL
));
999 diff
= tv_subtract(end
, start
);
1001 if (dump_to_pcap(ctx
)) {
1003 finish_multi_pcap_file(ctx
, fd
);
1005 finish_single_pcap_file(ctx
, fd
);
1008 if (!(ctx
->dump_dir
&& ctx
->print_mode
== PRINT_NONE
)) {
1009 sock_print_net_stats(sock
, skipped
);
1011 printf("\r%12lu sec, %lu usec in total\n",
1012 diff
.tv_sec
, diff
.tv_usec
);
1018 bpf_release(&bpf_ops
);
1019 dissector_cleanup_all();
1020 destroy_rx_ring(sock
, &rx_ring
);
1022 if (ctx
->promiscuous
)
1023 leave_promiscuous_mode(ctx
->device_in
, ifflags
);
1026 leave_rfmon_mac80211(ctx
->device_trans
, ctx
->device_in
);
1031 static void help(void)
1033 printf("\nnetsniff-ng %s, the packet sniffing beast\n", VERSION_STRING
);
1034 puts("http://www.netsniff-ng.org\n\n"
1035 "Usage: netsniff-ng [options] [filter-expression]\n"
1037 " -i|-d|--dev|--in <dev|pcap> Input source as netdev or pcap\n"
1038 " -o|--out <dev|pcap|dir|cfg> Output sink as netdev, pcap, directory, trafgen file\n"
1039 " -f|--filter <bpf-file|expr> Use BPF filter file from bpfc or tcpdump-like expression\n"
1040 " -t|--type <type> Only handle packets of defined type:\n"
1041 " host|broadcast|multicast|others|outgoing\n"
1042 " -F|--interval <size/time> Dump interval in time or size if -o is a directory\n"
1043 " pcap swap spec: <num>KiB/MiB/GiB/s/sec/min/hrs\n"
1044 " -J|--jumbo-support Support for 64KB Super Jumbo Frames\n"
1045 " Default RX/TX slot: 2048Byte\n"
1046 " -R|--rfraw Capture or inject raw 802.11 frames\n"
1047 " -n|--num <uint> Number of packets until exit\n"
1048 " `-- 0 Loop until interrupted (default)\n"
1049 " `- n Send n packets and done\n"
1050 "Options for printing:\n"
1051 " -s|--silent Do not print captured packets\n"
1052 " -q|--less Print less-verbose packet information\n"
1053 " -X|--hex Print packet data in hex format\n"
1054 " -l|--ascii Print human-readable packet data\n"
1055 "Options, advanced:\n"
1056 " -P|--prefix <name> Prefix for pcaps stored in directory\n"
1057 " -T|--magic <pcap-magic> Pcap magic number/pcap type to process\n"
1058 " -r|--rand Randomize packet forwarding order\n"
1059 " -M|--no-promisc No promiscuous mode for netdev\n"
1060 " -A|--no-sock-mem Don't tune core socket memory\n"
1061 " -m|--mmap Mmap pcap file i.e., for replaying\n"
1062 " -G|--sg Scatter/gather pcap file I/O\n"
1063 " -c|--clrw Use slower read(2)/write(2) I/O\n"
1064 " -S|--ring-size <size> Manually set ring size to <size>:\n"
1065 " mmap space in KiB/MiB/GiB, e.g. \'10MiB\'\n"
1066 " -k|--kernel-pull <uint> Kernel pull from user interval in us\n"
1067 " Default is 10us where the TX_RING\n"
1068 " is populated with payload from uspace\n"
1069 " -b|--bind-cpu <cpu> Bind to specific CPU (or CPU-range)\n"
1070 " -B|--unbind-cpu <cpu> Forbid to use specific CPU (or CPU-range)\n"
1071 " -u|--user <userid> Drop privileges and change to userid\n"
1072 " -g|--group <groupid> Drop privileges and change to groupid\n"
1073 " -H|--prio-high Make this high priority process\n"
1074 " -Q|--notouch-irq Do not touch IRQ CPU affinity of NIC\n"
1075 " -V|--verbose Be more verbose\n"
1076 " -D|--dump-pcap-types Dump pcap types and magic numbers\n"
1077 " -v|--version Show version\n"
1078 " -h|--help Guess what?!\n\n"
1080 " netsniff-ng --in eth0 --out dump.pcap --silent -T 0xa1b2c3d4 --bind-cpu 0\n"
1081 " netsniff-ng --in wlan0 --rfraw --out dump.pcap --silent --bind-cpu 0\n"
1082 " netsniff-ng --in dump.pcap --mmap --out eth0 -k1000 --silent --bind-cpu 0\n"
1083 " netsniff-ng --in dump.pcap --out dump.cfg --silent --bind-cpu 0\n"
1084 " netsniff-ng --in eth0 --out eth1 --silent --bind-cpu 0 --type host\n"
1085 " netsniff-ng --in eth1 --out /opt/probe/ -s -m -J --interval 100MiB -b 0\n"
1086 " netsniff-ng --in vlan0 --out dump.pcap -c -u `id -u bob` -g `id -g bob`\n"
1087 " netsniff-ng --in any --filter http.bpf --jumbo-support --ascii -V\n\n"
1088 "Please report bugs to <bugs@netsniff-ng.org>\n"
1089 "Copyright (C) 2009-2013 Daniel Borkmann <daniel@netsniff-ng.org>\n"
1090 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel@netsniff-ng.org>\n"
1091 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1092 "License: GNU GPL version 2.0\n"
1093 "This is free software: you are free to change and redistribute it.\n"
1094 "There is NO WARRANTY, to the extent permitted by law.\n");
1098 static void version(void)
1100 printf("\nnetsniff-ng %s, the packet sniffing beast\n", VERSION_STRING
);
1101 puts("http://www.netsniff-ng.org\n\n"
1102 "Please report bugs to <bugs@netsniff-ng.org>\n"
1103 "Copyright (C) 2009-2013 Daniel Borkmann <daniel@netsniff-ng.org>\n"
1104 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel@netsniff-ng.org>\n"
1105 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1106 "License: GNU GPL version 2.0\n"
1107 "This is free software: you are free to change and redistribute it.\n"
1108 "There is NO WARRANTY, to the extent permitted by law.\n");
1112 static void header(void)
1114 printf("%s%s%s\n", colorize_start(bold
), "netsniff-ng " VERSION_STRING
, colorize_end());
1117 int main(int argc
, char **argv
)
1120 int c
, i
, j
, opt_index
, ops_touched
= 0, vals
[4] = {0};
1121 bool prio_high
= false, setsockmem
= true;
1122 void (*main_loop
)(struct ctx
*ctx
) = NULL
;
1124 .link_type
= LINKTYPE_EN10MB
,
1125 .print_mode
= PRINT_NORM
,
1128 .promiscuous
= true,
1130 .pcap
= PCAP_OPS_SG
,
1131 .dump_interval
= 60,
1132 .dump_mode
= DUMP_INTERVAL_TIME
,
1135 .magic
= ORIGINAL_TCPDUMP_MAGIC
,
1140 while ((c
= getopt_long(argc
, argv
, short_options
, long_options
,
1141 &opt_index
)) != EOF
) {
1145 ctx
.device_in
= xstrdup(optarg
);
1148 ctx
.device_out
= xstrdup(optarg
);
1151 ctx
.prefix
= xstrdup(optarg
);
1154 ctx
.link_type
= LINKTYPE_IEEE802_11
;
1158 ctx
.randomize
= true;
1164 ctx
.magic
= (uint32_t) strtoul(optarg
, NULL
, 0);
1165 pcap_check_magic(ctx
.magic
);
1168 ctx
.filter
= xstrdup(optarg
);
1171 ctx
.promiscuous
= false;
1177 ctx
.uid
= strtoul(optarg
, NULL
, 0);
1181 ctx
.gid
= strtoul(optarg
, NULL
, 0);
1185 if (!strncmp(optarg
, "host", strlen("host")))
1186 ctx
.packet_type
= PACKET_HOST
;
1187 else if (!strncmp(optarg
, "broadcast", strlen("broadcast")))
1188 ctx
.packet_type
= PACKET_BROADCAST
;
1189 else if (!strncmp(optarg
, "multicast", strlen("multicast")))
1190 ctx
.packet_type
= PACKET_MULTICAST
;
1191 else if (!strncmp(optarg
, "others", strlen("others")))
1192 ctx
.packet_type
= PACKET_OTHERHOST
;
1193 else if (!strncmp(optarg
, "outgoing", strlen("outgoing")))
1194 ctx
.packet_type
= PACKET_OUTGOING
;
1196 ctx
.packet_type
= -1;
1200 ctx
.reserve_size
= 0;
1202 for (j
= i
= strlen(optarg
); i
> 0; --i
) {
1203 if (!isdigit(optarg
[j
- i
]))
1208 if (!strncmp(ptr
, "KiB", strlen("KiB")))
1209 ctx
.reserve_size
= 1 << 10;
1210 else if (!strncmp(ptr
, "MiB", strlen("MiB")))
1211 ctx
.reserve_size
= 1 << 20;
1212 else if (!strncmp(ptr
, "GiB", strlen("GiB")))
1213 ctx
.reserve_size
= 1 << 30;
1215 panic("Syntax error in ring size param!\n");
1218 ctx
.reserve_size
*= strtol(optarg
, NULL
, 0);
1221 set_cpu_affinity(optarg
, 0);
1222 /* Take the first CPU for rebinding the IRQ */
1224 ctx
.cpu
= strtol(optarg
, NULL
, 0);
1227 set_cpu_affinity(optarg
, 1);
1233 ctx
.pcap
= PCAP_OPS_RW
;
1237 ctx
.pcap
= PCAP_OPS_MM
;
1241 ctx
.pcap
= PCAP_OPS_SG
;
1248 ctx
.print_mode
= PRINT_NONE
;
1251 ctx
.print_mode
= PRINT_LESS
;
1255 (ctx
.print_mode
== PRINT_ASCII
) ?
1256 PRINT_HEX_ASCII
: PRINT_HEX
;
1260 (ctx
.print_mode
== PRINT_HEX
) ?
1261 PRINT_HEX_ASCII
: PRINT_ASCII
;
1264 ctx
.kpull
= strtol(optarg
, NULL
, 0);
1267 frame_count_max
= strtol(optarg
, NULL
, 0);
1271 ctx
.dump_interval
= 0;
1273 for (j
= i
= strlen(optarg
); i
> 0; --i
) {
1274 if (!isdigit(optarg
[j
- i
]))
1279 if (!strncmp(ptr
, "KiB", strlen("KiB"))) {
1280 ctx
.dump_interval
= 1 << 10;
1281 ctx
.dump_mode
= DUMP_INTERVAL_SIZE
;
1282 } else if (!strncmp(ptr
, "MiB", strlen("MiB"))) {
1283 ctx
.dump_interval
= 1 << 20;
1284 ctx
.dump_mode
= DUMP_INTERVAL_SIZE
;
1285 } else if (!strncmp(ptr
, "GiB", strlen("GiB"))) {
1286 ctx
.dump_interval
= 1 << 30;
1287 ctx
.dump_mode
= DUMP_INTERVAL_SIZE
;
1288 } else if (!strncmp(ptr
, "sec", strlen("sec"))) {
1289 ctx
.dump_interval
= 1;
1290 ctx
.dump_mode
= DUMP_INTERVAL_TIME
;
1291 } else if (!strncmp(ptr
, "min", strlen("min"))) {
1292 ctx
.dump_interval
= 60;
1293 ctx
.dump_mode
= DUMP_INTERVAL_TIME
;
1294 } else if (!strncmp(ptr
, "hrs", strlen("hrs"))) {
1295 ctx
.dump_interval
= 60 * 60;
1296 ctx
.dump_mode
= DUMP_INTERVAL_TIME
;
1297 } else if (!strncmp(ptr
, "s", strlen("s"))) {
1298 ctx
.dump_interval
= 1;
1299 ctx
.dump_mode
= DUMP_INTERVAL_TIME
;
1301 panic("Syntax error in time/size param!\n");
1305 ctx
.dump_interval
*= strtol(optarg
, NULL
, 0);
1311 pcap_dump_type_features();
1338 panic("Option -%c requires an argument!\n",
1341 if (isprint(optopt
))
1342 whine("Unknown option character "
1343 "`0x%X\'!\n", optopt
);
1351 if (!ctx
.filter
&& optind
!= argc
) {
1355 for (i
= optind
; i
< argc
; ++i
) {
1356 size_t alen
= strlen(argv
[i
]) + 2;
1357 size_t flen
= ctx
.filter
? strlen(ctx
.filter
) : 0;
1359 ctx
.filter
= xrealloc(ctx
.filter
, 1, flen
+ alen
);
1360 ret
= slprintf(ctx
.filter
+ offset
, strlen(argv
[i
]) + 2, "%s ", argv
[i
]);
1362 panic("Cannot concatenate filter string!\n");
1369 ctx
.device_in
= xstrdup("any");
1371 register_signal(SIGINT
, signal_handler
);
1372 register_signal(SIGHUP
, signal_handler
);
1379 set_proc_prio(get_default_proc_prio());
1380 set_sched_status(get_default_sched_policy(), get_default_sched_prio());
1383 if (ctx
.device_in
&& (device_mtu(ctx
.device_in
) ||
1384 !strncmp("any", ctx
.device_in
, strlen(ctx
.device_in
)))) {
1385 if (!ctx
.device_out
) {
1387 main_loop
= recv_only_or_dump
;
1388 } else if (device_mtu(ctx
.device_out
)) {
1389 register_signal_f(SIGALRM
, timer_elapsed
, SA_SIGINFO
);
1390 main_loop
= receive_to_xmit
;
1393 register_signal_f(SIGALRM
, timer_next_dump
, SA_SIGINFO
);
1394 main_loop
= recv_only_or_dump
;
1396 ctx
.pcap
= PCAP_OPS_SG
;
1399 if (ctx
.device_out
&& device_mtu(ctx
.device_out
)) {
1400 register_signal_f(SIGALRM
, timer_elapsed
, SA_SIGINFO
);
1401 main_loop
= pcap_to_xmit
;
1403 ctx
.pcap
= PCAP_OPS_MM
;
1405 main_loop
= read_pcap
;
1407 ctx
.pcap
= PCAP_OPS_SG
;
1414 set_system_socket_memory(vals
);
1421 reset_system_socket_memory(vals
);
1425 free(ctx
.device_in
);
1426 free(ctx
.device_out
);
1427 free(ctx
.device_trans
);