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: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 {"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 {"silent", no_argument
, NULL
, 's'},
99 {"less", no_argument
, NULL
, 'q'},
100 {"hex", no_argument
, NULL
, 'X'},
101 {"ascii", no_argument
, NULL
, 'l'},
102 {"no-sock-mem", no_argument
, NULL
, 'A'},
103 {"verbose", no_argument
, NULL
, 'V'},
104 {"version", no_argument
, NULL
, 'v'},
105 {"help", no_argument
, NULL
, 'h'},
111 static struct itimerval itimer
;
113 static unsigned long frame_count_max
= 0, interval
= TX_KERNEL_PULL_INT
;
115 #define __pcap_io pcap_ops[ctx->pcap]
117 static void signal_handler(int number
)
128 static void timer_elapsed(int unused
)
130 itimer
.it_interval
.tv_sec
= 0;
131 itimer
.it_interval
.tv_usec
= interval
;
133 itimer
.it_value
.tv_sec
= 0;
134 itimer
.it_value
.tv_usec
= interval
;
136 pull_and_flush_tx_ring(tx_sock
);
137 setitimer(ITIMER_REAL
, &itimer
, NULL
);
140 static void timer_next_dump(int unused
)
142 itimer
.it_interval
.tv_sec
= interval
;
143 itimer
.it_interval
.tv_usec
= 0;
145 itimer
.it_value
.tv_sec
= interval
;
146 itimer
.it_value
.tv_usec
= 0;
149 setitimer(ITIMER_REAL
, &itimer
, NULL
);
152 static inline bool dump_to_pcap(struct ctx
*ctx
)
157 static void pcap_to_xmit(struct ctx
*ctx
)
161 int irq
, ifindex
, fd
= 0, ret
;
162 unsigned int size
, it
= 0;
163 unsigned long trunced
= 0;
165 struct frame_map
*hdr
;
166 struct sock_fprog bpf_ops
;
167 struct timeval start
, end
, diff
;
170 if (!device_up_and_running(ctx
->device_out
) && !ctx
->rfraw
)
171 panic("Device not up and running!\n");
175 tx_sock
= pf_socket();
177 if (!strncmp("-", ctx
->device_in
, strlen("-"))) {
178 fd
= dup(fileno(stdin
));
179 close(fileno(stdin
));
180 if (ctx
->pcap
== PCAP_OPS_MM
)
181 ctx
->pcap
= PCAP_OPS_SG
;
183 fd
= open_or_die(ctx
->device_in
, O_RDONLY
| O_LARGEFILE
| O_NOATIME
);
186 ret
= __pcap_io
->pull_fhdr_pcap(fd
, &ctx
->magic
, &ctx
->link_type
);
188 panic("Error reading pcap header!\n");
190 if (__pcap_io
->prepare_access_pcap
) {
191 ret
= __pcap_io
->prepare_access_pcap(fd
, PCAP_MODE_RD
, ctx
->jumbo
);
193 panic("Error prepare reading pcap!\n");
196 fmemset(&tx_ring
, 0, sizeof(tx_ring
));
197 fmemset(&bpf_ops
, 0, sizeof(bpf_ops
));
200 ctx
->device_trans
= xstrdup(ctx
->device_out
);
201 xfree(ctx
->device_out
);
203 enter_rfmon_mac80211(ctx
->device_trans
, &ctx
->device_out
);
204 if (ctx
->link_type
!= LINKTYPE_IEEE802_11
)
205 panic("Wrong linktype of pcap!\n");
208 ifindex
= device_ifindex(ctx
->device_out
);
210 size
= ring_size(ctx
->device_out
, ctx
->reserve_size
);
212 bpf_parse_rules(ctx
->device_out
, ctx
->filter
, &bpf_ops
);
214 set_packet_loss_discard(tx_sock
);
215 set_sockopt_hwtimestamp(tx_sock
, ctx
->device_out
);
217 setup_tx_ring_layout(tx_sock
, &tx_ring
, size
, ctx
->jumbo
);
218 create_tx_ring(tx_sock
, &tx_ring
, ctx
->verbose
);
219 mmap_tx_ring(tx_sock
, &tx_ring
);
220 alloc_tx_ring_frames(&tx_ring
);
221 bind_tx_ring(tx_sock
, &tx_ring
, ifindex
);
223 dissector_init_all(ctx
->print_mode
);
225 if (ctx
->cpu
>= 0 && ifindex
> 0) {
226 irq
= device_irq_number(ctx
->device_out
);
227 device_bind_irq_to_cpu(irq
, ctx
->cpu
);
230 printf("IRQ: %s:%d > CPU%d\n",
231 ctx
->device_out
, irq
, ctx
->cpu
);
235 interval
= ctx
->kpull
;
239 bpf_dump_all(&bpf_ops
);
241 printf("MD: TX %luus %s ", interval
, pcap_ops_group_to_str
[ctx
->pcap
]);
243 printf("802.11 raw via %s ", ctx
->device_out
);
244 #ifdef _LARGEFILE64_SOURCE
251 itimer
.it_interval
.tv_sec
= 0;
252 itimer
.it_interval
.tv_usec
= interval
;
254 itimer
.it_value
.tv_sec
= 0;
255 itimer
.it_value
.tv_usec
= interval
;
257 setitimer(ITIMER_REAL
, &itimer
, NULL
);
259 drop_privileges(ctx
->enforce
, ctx
->uid
, ctx
->gid
);
261 printf("Running! Hang up with ^C!\n\n");
264 bug_on(gettimeofday(&start
, NULL
));
266 while (likely(sigint
== 0)) {
267 while (user_may_pull_from_tx(tx_ring
.frames
[it
].iov_base
)) {
268 hdr
= tx_ring
.frames
[it
].iov_base
;
269 out
= ((uint8_t *) hdr
) + TPACKET2_HDRLEN
- sizeof(struct sockaddr_ll
);
272 ret
= __pcap_io
->read_pcap(fd
, &phdr
, ctx
->magic
, out
,
273 ring_frame_size(&tx_ring
));
274 if (unlikely(ret
<= 0))
277 if (ring_frame_size(&tx_ring
) <
278 pcap_get_length(&phdr
, ctx
->magic
)) {
279 pcap_set_length(&phdr
, ctx
->magic
,
280 ring_frame_size(&tx_ring
));
283 } while (ctx
->filter
&&
284 !bpf_run_filter(&bpf_ops
, out
,
285 pcap_get_length(&phdr
, ctx
->magic
)));
287 pcap_pkthdr_to_tpacket_hdr(&phdr
, ctx
->magic
, &hdr
->tp_h
, &hdr
->s_ll
);
289 ctx
->tx_bytes
+= hdr
->tp_h
.tp_len
;;
292 show_frame_hdr(hdr
, ctx
->print_mode
);
294 dissector_entry_point(out
, hdr
->tp_h
.tp_snaplen
,
295 ctx
->link_type
, ctx
->print_mode
);
297 kernel_may_pull_from_tx(&hdr
->tp_h
);
300 if (it
>= tx_ring
.layout
.tp_frame_nr
)
303 if (unlikely(sigint
== 1))
306 if (frame_count_max
!= 0) {
307 if (ctx
->tx_packets
>= frame_count_max
) {
317 bug_on(gettimeofday(&end
, NULL
));
318 diff
= tv_subtract(end
, start
);
320 bpf_release(&bpf_ops
);
322 dissector_cleanup_all();
323 destroy_tx_ring(tx_sock
, &tx_ring
);
326 leave_rfmon_mac80211(ctx
->device_trans
, ctx
->device_out
);
328 if (__pcap_io
->prepare_close_pcap
)
329 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_RD
);
331 if (strncmp("-", ctx
->device_in
, strlen("-")))
334 dup2(fd
, fileno(stdin
));
340 printf("\r%12lu packets outgoing\n", ctx
->tx_packets
);
341 printf("\r%12lu packets truncated in file\n", trunced
);
342 printf("\r%12lu bytes outgoing\n", ctx
->tx_bytes
);
343 printf("\r%12lu sec, %lu usec in total\n", diff
.tv_sec
, diff
.tv_usec
);
346 static void receive_to_xmit(struct ctx
*ctx
)
350 int rx_sock
, ifindex_in
, ifindex_out
;
351 unsigned int size_in
, size_out
, it_in
= 0, it_out
= 0;
352 unsigned long frame_count
= 0;
353 struct frame_map
*hdr_in
, *hdr_out
;
354 struct ring tx_ring
, rx_ring
;
355 struct pollfd rx_poll
;
356 struct sock_fprog bpf_ops
;
358 if (!strncmp(ctx
->device_in
, ctx
->device_out
, IFNAMSIZ
))
359 panic("Ingress/egress devices must be different!\n");
360 if (!device_up_and_running(ctx
->device_out
))
361 panic("Egress device not up and running!\n");
362 if (!device_up_and_running(ctx
->device_in
))
363 panic("Ingress device not up and running!\n");
365 rx_sock
= pf_socket();
366 tx_sock
= pf_socket();
368 fmemset(&tx_ring
, 0, sizeof(tx_ring
));
369 fmemset(&rx_ring
, 0, sizeof(rx_ring
));
370 fmemset(&rx_poll
, 0, sizeof(rx_poll
));
371 fmemset(&bpf_ops
, 0, sizeof(bpf_ops
));
373 ifindex_in
= device_ifindex(ctx
->device_in
);
374 ifindex_out
= device_ifindex(ctx
->device_out
);
376 size_in
= ring_size(ctx
->device_in
, ctx
->reserve_size
);
377 size_out
= ring_size(ctx
->device_out
, ctx
->reserve_size
);
379 enable_kernel_bpf_jit_compiler();
381 bpf_parse_rules(ctx
->device_in
, ctx
->filter
, &bpf_ops
);
382 bpf_attach_to_sock(rx_sock
, &bpf_ops
);
384 setup_rx_ring_layout(rx_sock
, &rx_ring
, size_in
, ctx
->jumbo
);
385 create_rx_ring(rx_sock
, &rx_ring
, ctx
->verbose
);
386 mmap_rx_ring(rx_sock
, &rx_ring
);
387 alloc_rx_ring_frames(&rx_ring
);
388 bind_rx_ring(rx_sock
, &rx_ring
, ifindex_in
);
389 prepare_polling(rx_sock
, &rx_poll
);
391 set_packet_loss_discard(tx_sock
);
392 setup_tx_ring_layout(tx_sock
, &tx_ring
, size_out
, ctx
->jumbo
);
393 create_tx_ring(tx_sock
, &tx_ring
, ctx
->verbose
);
394 mmap_tx_ring(tx_sock
, &tx_ring
);
395 alloc_tx_ring_frames(&tx_ring
);
396 bind_tx_ring(tx_sock
, &tx_ring
, ifindex_out
);
398 dissector_init_all(ctx
->print_mode
);
400 if (ctx
->promiscuous
)
401 ifflags
= enter_promiscuous_mode(ctx
->device_in
);
404 interval
= ctx
->kpull
;
406 itimer
.it_interval
.tv_sec
= 0;
407 itimer
.it_interval
.tv_usec
= interval
;
409 itimer
.it_value
.tv_sec
= 0;
410 itimer
.it_value
.tv_usec
= interval
;
412 setitimer(ITIMER_REAL
, &itimer
, NULL
);
416 bpf_dump_all(&bpf_ops
);
418 printf("MD: RXTX %luus\n\n", interval
);
421 drop_privileges(ctx
->enforce
, ctx
->uid
, ctx
->gid
);
423 printf("Running! Hang up with ^C!\n\n");
426 while (likely(sigint
== 0)) {
427 while (user_may_pull_from_rx(rx_ring
.frames
[it_in
].iov_base
)) {
430 hdr_in
= rx_ring
.frames
[it_in
].iov_base
;
431 in
= ((uint8_t *) hdr_in
) + hdr_in
->tp_h
.tp_mac
;
435 if (ctx
->packet_type
!= -1)
436 if (ctx
->packet_type
!= hdr_in
->s_ll
.sll_pkttype
)
439 hdr_out
= tx_ring
.frames
[it_out
].iov_base
;
440 out
= ((uint8_t *) hdr_out
) + TPACKET2_HDRLEN
- sizeof(struct sockaddr_ll
);
442 for (; !user_may_pull_from_tx(tx_ring
.frames
[it_out
].iov_base
) &&
445 next_rnd_slot(&it_out
, &tx_ring
);
448 if (it_out
>= tx_ring
.layout
.tp_frame_nr
)
452 hdr_out
= tx_ring
.frames
[it_out
].iov_base
;
453 out
= ((uint8_t *) hdr_out
) + TPACKET2_HDRLEN
- sizeof(struct sockaddr_ll
);
456 tpacket_hdr_clone(&hdr_out
->tp_h
, &hdr_in
->tp_h
);
457 fmemcpy(out
, in
, hdr_in
->tp_h
.tp_len
);
459 kernel_may_pull_from_tx(&hdr_out
->tp_h
);
461 next_rnd_slot(&it_out
, &tx_ring
);
464 if (it_out
>= tx_ring
.layout
.tp_frame_nr
)
468 show_frame_hdr(hdr_in
, ctx
->print_mode
);
470 dissector_entry_point(in
, hdr_in
->tp_h
.tp_snaplen
,
471 ctx
->link_type
, ctx
->print_mode
);
473 if (frame_count_max
!= 0) {
474 if (frame_count
>= frame_count_max
) {
482 kernel_may_pull_from_rx(&hdr_in
->tp_h
);
485 if (it_in
>= rx_ring
.layout
.tp_frame_nr
)
488 if (unlikely(sigint
== 1))
492 poll(&rx_poll
, 1, -1);
493 poll_error_maybe_die(rx_sock
, &rx_poll
);
498 sock_print_net_stats(rx_sock
, 0);
500 bpf_release(&bpf_ops
);
502 dissector_cleanup_all();
504 destroy_tx_ring(tx_sock
, &tx_ring
);
505 destroy_rx_ring(rx_sock
, &rx_ring
);
507 if (ctx
->promiscuous
)
508 leave_promiscuous_mode(ctx
->device_in
, ifflags
);
514 static void translate_pcap_to_txf(int fdo
, uint8_t *out
, size_t len
)
516 size_t bytes_done
= 0;
519 slprintf(bout
, sizeof(bout
), "{\n ");
520 write_or_die(fdo
, bout
, strlen(bout
));
522 while (bytes_done
< len
) {
523 slprintf(bout
, sizeof(bout
), "0x%02x, ", out
[bytes_done
]);
524 write_or_die(fdo
, bout
, strlen(bout
));
528 if (bytes_done
% 10 == 0) {
529 slprintf(bout
, sizeof(bout
), "\n");
530 write_or_die(fdo
, bout
, strlen(bout
));
532 if (bytes_done
< len
) {
533 slprintf(bout
, sizeof(bout
), " ");
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
));
543 slprintf(bout
, sizeof(bout
), "}\n\n");
544 write_or_die(fdo
, bout
, strlen(bout
));
547 static void read_pcap(struct ctx
*ctx
)
551 int ret
, fd
, fdo
= 0;
552 unsigned long trunced
= 0;
555 struct sock_fprog bpf_ops
;
557 struct timeval start
, end
, diff
;
558 struct sockaddr_ll sll
;
562 if (!strncmp("-", ctx
->device_in
, strlen("-"))) {
563 fd
= dup(fileno(stdin
));
564 close(fileno(stdin
));
565 if (ctx
->pcap
== PCAP_OPS_MM
)
566 ctx
->pcap
= PCAP_OPS_SG
;
568 fd
= open_or_die(ctx
->device_in
, O_RDONLY
| O_LARGEFILE
| O_NOATIME
);
571 ret
= __pcap_io
->pull_fhdr_pcap(fd
, &ctx
->magic
, &ctx
->link_type
);
573 panic("Error reading pcap header!\n");
575 if (__pcap_io
->prepare_access_pcap
) {
576 ret
= __pcap_io
->prepare_access_pcap(fd
, PCAP_MODE_RD
, ctx
->jumbo
);
578 panic("Error prepare reading pcap!\n");
581 fmemset(&fm
, 0, sizeof(fm
));
582 fmemset(&bpf_ops
, 0, sizeof(bpf_ops
));
584 bpf_parse_rules("any", ctx
->filter
, &bpf_ops
);
586 dissector_init_all(ctx
->print_mode
);
588 out_len
= round_up(1024 * 1024, PAGE_SIZE
);
589 out
= xmalloc_aligned(out_len
, CO_CACHE_LINE_SIZE
);
593 bpf_dump_all(&bpf_ops
);
595 printf("MD: RD %s ", pcap_ops_group_to_str
[ctx
->pcap
]);
596 #ifdef _LARGEFILE64_SOURCE
603 if (ctx
->device_out
) {
604 if (!strncmp("-", ctx
->device_out
, strlen("-"))) {
605 fdo
= dup(fileno(stdout
));
606 close(fileno(stdout
));
608 fdo
= open_or_die_m(ctx
->device_out
, O_RDWR
| O_CREAT
|
609 O_TRUNC
| O_LARGEFILE
, DEFFILEMODE
);
613 drop_privileges(ctx
->enforce
, ctx
->uid
, ctx
->gid
);
615 printf("Running! Hang up with ^C!\n\n");
618 bug_on(gettimeofday(&start
, NULL
));
620 while (likely(sigint
== 0)) {
622 ret
= __pcap_io
->read_pcap(fd
, &phdr
, ctx
->magic
,
624 if (unlikely(ret
< 0))
627 if (unlikely(pcap_get_length(&phdr
, ctx
->magic
) == 0)) {
632 if (unlikely(pcap_get_length(&phdr
, ctx
->magic
) > out_len
)) {
633 pcap_set_length(&phdr
, ctx
->magic
, out_len
);
636 } while (ctx
->filter
&&
637 !bpf_run_filter(&bpf_ops
, out
,
638 pcap_get_length(&phdr
, ctx
->magic
)));
640 pcap_pkthdr_to_tpacket_hdr(&phdr
, ctx
->magic
, &fm
.tp_h
, &sll
);
642 ctx
->tx_bytes
+= fm
.tp_h
.tp_len
;
645 show_frame_hdr(&fm
, ctx
->print_mode
);
647 dissector_entry_point(out
, fm
.tp_h
.tp_snaplen
,
648 ctx
->link_type
, ctx
->print_mode
);
651 translate_pcap_to_txf(fdo
, out
, fm
.tp_h
.tp_snaplen
);
653 if (frame_count_max
!= 0) {
654 if (ctx
->tx_packets
>= frame_count_max
) {
663 bug_on(gettimeofday(&end
, NULL
));
664 diff
= tv_subtract(end
, start
);
666 bpf_release(&bpf_ops
);
668 dissector_cleanup_all();
670 if (__pcap_io
->prepare_close_pcap
)
671 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_RD
);
677 printf("\r%12lu packets outgoing\n", ctx
->tx_packets
);
678 printf("\r%12lu packets truncated in file\n", trunced
);
679 printf("\r%12lu bytes outgoing\n", ctx
->tx_bytes
);
680 printf("\r%12lu sec, %lu usec in total\n", diff
.tv_sec
, diff
.tv_usec
);
682 if (strncmp("-", ctx
->device_in
, strlen("-")))
685 dup2(fd
, fileno(stdin
));
687 if (ctx
->device_out
) {
688 if (strncmp("-", ctx
->device_out
, strlen("-")))
691 dup2(fdo
, fileno(stdout
));
695 static void finish_multi_pcap_file(struct ctx
*ctx
, int fd
)
697 __pcap_io
->fsync_pcap(fd
);
699 if (__pcap_io
->prepare_close_pcap
)
700 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_WR
);
704 fmemset(&itimer
, 0, sizeof(itimer
));
705 setitimer(ITIMER_REAL
, &itimer
, NULL
);
708 static int next_multi_pcap_file(struct ctx
*ctx
, int fd
)
713 __pcap_io
->fsync_pcap(fd
);
715 if (__pcap_io
->prepare_close_pcap
)
716 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_WR
);
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_fhdr_pcap(fd
, ctx
->magic
, ctx
->link_type
);
728 panic("Error writing pcap header!\n");
730 if (__pcap_io
->prepare_access_pcap
) {
731 ret
= __pcap_io
->prepare_access_pcap(fd
, PCAP_MODE_WR
, ctx
->jumbo
);
733 panic("Error prepare writing pcap!\n");
739 static int begin_multi_pcap_file(struct ctx
*ctx
)
746 if (ctx
->device_out
[strlen(ctx
->device_out
) - 1] == '/')
747 ctx
->device_out
[strlen(ctx
->device_out
) - 1] = 0;
749 slprintf(fname
, sizeof(fname
), "%s/%s%lu.pcap", ctx
->device_out
,
750 ctx
->prefix
? : "dump-", time(0));
752 fd
= open_or_die_m(fname
, O_RDWR
| O_CREAT
| O_TRUNC
|
753 O_LARGEFILE
, DEFFILEMODE
);
755 ret
= __pcap_io
->push_fhdr_pcap(fd
, ctx
->magic
, ctx
->link_type
);
757 panic("Error writing pcap header!\n");
759 if (__pcap_io
->prepare_access_pcap
) {
760 ret
= __pcap_io
->prepare_access_pcap(fd
, PCAP_MODE_WR
, ctx
->jumbo
);
762 panic("Error prepare writing pcap!\n");
765 if (ctx
->dump_mode
== DUMP_INTERVAL_TIME
) {
766 interval
= ctx
->dump_interval
;
768 itimer
.it_interval
.tv_sec
= interval
;
769 itimer
.it_interval
.tv_usec
= 0;
771 itimer
.it_value
.tv_sec
= interval
;
772 itimer
.it_value
.tv_usec
= 0;
774 setitimer(ITIMER_REAL
, &itimer
, NULL
);
782 static void finish_single_pcap_file(struct ctx
*ctx
, int fd
)
784 __pcap_io
->fsync_pcap(fd
);
786 if (__pcap_io
->prepare_close_pcap
)
787 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_WR
);
789 if (strncmp("-", ctx
->device_out
, strlen("-")))
792 dup2(fd
, fileno(stdout
));
795 static int begin_single_pcap_file(struct ctx
*ctx
)
801 if (!strncmp("-", ctx
->device_out
, strlen("-"))) {
802 fd
= dup(fileno(stdout
));
803 close(fileno(stdout
));
804 if (ctx
->pcap
== PCAP_OPS_MM
)
805 ctx
->pcap
= PCAP_OPS_SG
;
807 fd
= open_or_die_m(ctx
->device_out
,
808 O_RDWR
| O_CREAT
| O_TRUNC
|
809 O_LARGEFILE
, DEFFILEMODE
);
812 ret
= __pcap_io
->push_fhdr_pcap(fd
, ctx
->magic
, ctx
->link_type
);
814 panic("Error writing pcap header!\n");
816 if (__pcap_io
->prepare_access_pcap
) {
817 ret
= __pcap_io
->prepare_access_pcap(fd
, PCAP_MODE_WR
, ctx
->jumbo
);
819 panic("Error prepare writing pcap!\n");
825 static void print_pcap_file_stats(int sock
, struct ctx
*ctx
, unsigned long skipped
)
827 unsigned long good
, bad
;
828 struct tpacket_stats kstats
;
829 socklen_t slen
= sizeof(kstats
);
831 fmemset(&kstats
, 0, sizeof(kstats
));
832 getsockopt(sock
, SOL_PACKET
, PACKET_STATISTICS
, &kstats
, &slen
);
834 if (ctx
->print_mode
== PRINT_NONE
) {
835 good
= kstats
.tp_packets
- kstats
.tp_drops
- skipped
;
836 bad
= kstats
.tp_drops
+ skipped
;
838 printf(".(+%lu/-%lu)", good
, bad
);
843 static void recv_only_or_dump(struct ctx
*ctx
)
847 int sock
, irq
, ifindex
, fd
= 0, ret
;
848 unsigned int size
, it
= 0;
849 unsigned long frame_count
= 0, skipped
= 0;
851 struct pollfd rx_poll
;
852 struct frame_map
*hdr
;
853 struct sock_fprog bpf_ops
;
854 struct timeval start
, end
, diff
;
857 if (!device_up_and_running(ctx
->device_in
) && !ctx
->rfraw
)
858 panic("Device not up and running!\n");
863 ctx
->device_trans
= xstrdup(ctx
->device_in
);
864 xfree(ctx
->device_in
);
866 enter_rfmon_mac80211(ctx
->device_trans
, &ctx
->device_in
);
867 ctx
->link_type
= LINKTYPE_IEEE802_11
;
870 fmemset(&rx_ring
, 0, sizeof(rx_ring
));
871 fmemset(&rx_poll
, 0, sizeof(rx_poll
));
872 fmemset(&bpf_ops
, 0, sizeof(bpf_ops
));
874 ifindex
= device_ifindex(ctx
->device_in
);
876 size
= ring_size(ctx
->device_in
, ctx
->reserve_size
);
878 enable_kernel_bpf_jit_compiler();
880 bpf_parse_rules(ctx
->device_in
, ctx
->filter
, &bpf_ops
);
881 bpf_attach_to_sock(sock
, &bpf_ops
);
883 set_sockopt_hwtimestamp(sock
, ctx
->device_in
);
885 setup_rx_ring_layout(sock
, &rx_ring
, size
, ctx
->jumbo
);
886 create_rx_ring(sock
, &rx_ring
, ctx
->verbose
);
887 mmap_rx_ring(sock
, &rx_ring
);
888 alloc_rx_ring_frames(&rx_ring
);
889 bind_rx_ring(sock
, &rx_ring
, ifindex
);
891 prepare_polling(sock
, &rx_poll
);
892 dissector_init_all(ctx
->print_mode
);
894 if (ctx
->cpu
>= 0 && ifindex
> 0) {
895 irq
= device_irq_number(ctx
->device_in
);
896 device_bind_irq_to_cpu(irq
, ctx
->cpu
);
899 printf("IRQ: %s:%d > CPU%d\n",
900 ctx
->device_in
, irq
, ctx
->cpu
);
903 if (ctx
->promiscuous
)
904 ifflags
= enter_promiscuous_mode(ctx
->device_in
);
908 bpf_dump_all(&bpf_ops
);
910 printf("MD: RX %s ", ctx
->dump
? pcap_ops_group_to_str
[ctx
->pcap
] : "");
912 printf("802.11 raw via %s ", ctx
->device_in
);
913 #ifdef _LARGEFILE64_SOURCE
920 drop_privileges(ctx
->enforce
, ctx
->uid
, ctx
->gid
);
922 if (dump_to_pcap(ctx
)) {
926 fmemset(&stats
, 0, sizeof(stats
));
927 ret
= stat(ctx
->device_out
, &stats
);
933 ctx
->dump_dir
= S_ISDIR(stats
.st_mode
);
935 fd
= begin_multi_pcap_file(ctx
);
938 fd
= begin_single_pcap_file(ctx
);
942 printf("Running! Hang up with ^C!\n\n");
945 bug_on(gettimeofday(&start
, NULL
));
947 while (likely(sigint
== 0)) {
948 while (user_may_pull_from_rx(rx_ring
.frames
[it
].iov_base
)) {
951 hdr
= rx_ring
.frames
[it
].iov_base
;
952 packet
= ((uint8_t *) hdr
) + hdr
->tp_h
.tp_mac
;
955 if (ctx
->packet_type
!= -1)
956 if (ctx
->packet_type
!= hdr
->s_ll
.sll_pkttype
)
959 if (unlikely(ring_frame_size(&rx_ring
) < hdr
->tp_h
.tp_snaplen
)) {
964 if (dump_to_pcap(ctx
)) {
965 tpacket_hdr_to_pcap_pkthdr(&hdr
->tp_h
, &hdr
->s_ll
, &phdr
, ctx
->magic
);
967 ret
= __pcap_io
->write_pcap(fd
, &phdr
, ctx
->magic
, packet
,
968 pcap_get_length(&phdr
, ctx
->magic
));
969 if (unlikely(ret
!= pcap_get_total_length(&phdr
, ctx
->magic
)))
970 panic("Write error to pcap!\n");
973 show_frame_hdr(hdr
, ctx
->print_mode
);
975 dissector_entry_point(packet
, hdr
->tp_h
.tp_snaplen
,
976 ctx
->link_type
, ctx
->print_mode
);
978 if (frame_count_max
!= 0) {
979 if (frame_count
>= frame_count_max
) {
987 kernel_may_pull_from_rx(&hdr
->tp_h
);
990 if (it
>= rx_ring
.layout
.tp_frame_nr
)
993 if (unlikely(sigint
== 1))
996 if (dump_to_pcap(ctx
)) {
997 if (ctx
->dump_mode
== DUMP_INTERVAL_SIZE
) {
998 interval
+= hdr
->tp_h
.tp_snaplen
;
1000 if (interval
> ctx
->dump_interval
) {
1007 fd
= next_multi_pcap_file(ctx
, fd
);
1011 print_pcap_file_stats(sock
, ctx
, skipped
);
1016 poll(&rx_poll
, 1, -1);
1017 poll_error_maybe_die(sock
, &rx_poll
);
1020 bug_on(gettimeofday(&end
, NULL
));
1021 diff
= tv_subtract(end
, start
);
1023 if (!(ctx
->dump_dir
&& ctx
->print_mode
== PRINT_NONE
)) {
1024 sock_print_net_stats(sock
, skipped
);
1026 printf("\r%12lu sec, %lu usec in total\n",
1027 diff
.tv_sec
, diff
.tv_usec
);
1033 bpf_release(&bpf_ops
);
1034 dissector_cleanup_all();
1035 destroy_rx_ring(sock
, &rx_ring
);
1037 if (ctx
->promiscuous
)
1038 leave_promiscuous_mode(ctx
->device_in
, ifflags
);
1041 leave_rfmon_mac80211(ctx
->device_trans
, ctx
->device_in
);
1043 if (dump_to_pcap(ctx
)) {
1045 finish_multi_pcap_file(ctx
, fd
);
1047 finish_single_pcap_file(ctx
, fd
);
1053 static void help(void)
1055 printf("\nnetsniff-ng %s, the packet sniffing beast\n", VERSION_STRING
);
1056 puts("http://www.netsniff-ng.org\n\n"
1057 "Usage: netsniff-ng [options] [filter-expression]\n"
1059 " -i|-d|--dev|--in <dev|pcap|-> Input source as netdev, pcap or pcap stdin\n"
1060 " -o|--out <dev|pcap|dir|cfg|-> Output sink as netdev, pcap, directory, trafgen, or stdout\n"
1061 " -f|--filter <bpf-file|expr> Use BPF filter file from bpfc or tcpdump-like expression\n"
1062 " -t|--type <type> Filter for: host|broadcast|multicast|others|outgoing\n"
1063 " -F|--interval <size|time> Dump interval if -o is a dir: <num>KiB/MiB/GiB/s/sec/min/hrs\n"
1064 " -J|--jumbo-support Support for 64KB Super Jumbo Frames (def: 2048B)\n"
1065 " -R|--rfraw Capture or inject raw 802.11 frames\n"
1066 " -n|--num <0|uint> Number of packets until exit (def: 0)\n"
1067 " -P|--prefix <name> Prefix for pcaps stored in directory\n"
1068 " -T|--magic <pcap-magic> Pcap magic number/pcap format to store, see -D\n"
1069 " -D|--dump-pcap-types Dump pcap types and magic numbers and quit\n"
1070 " -r|--rand Randomize packet forwarding order (dev->dev)\n"
1071 " -M|--no-promisc No promiscuous mode for netdev\n"
1072 " -A|--no-sock-mem Don't tune core socket memory\n"
1073 " -m|--mmap Mmap(2) pcap file i.e., for replaying pcaps\n"
1074 " -G|--sg Scatter/gather pcap file I/O\n"
1075 " -c|--clrw Use slower read(2)/write(2) I/O\n"
1076 " -S|--ring-size <size> Specify ring size to: <num>KiB/MiB/GiB\n"
1077 " -k|--kernel-pull <uint> Kernel pull from user interval in us (def: 10us)\n"
1078 " -b|--bind-cpu <cpu> Bind to specific CPU\n"
1079 " -u|--user <userid> Drop privileges and change to userid\n"
1080 " -g|--group <groupid> Drop privileges and change to groupid\n"
1081 " -H|--prio-high Make this high priority process\n"
1082 " -Q|--notouch-irq Do not touch IRQ CPU affinity of NIC\n"
1083 " -s|--silent Do not print captured packets\n"
1084 " -q|--less Print less-verbose packet information\n"
1085 " -X|--hex Print packet data in hex format\n"
1086 " -l|--ascii Print human-readable packet data\n"
1087 " -V|--verbose Be more verbose\n"
1088 " -v|--version Show version\n"
1089 " -h|--help Guess what?!\n\n"
1091 " netsniff-ng --in eth0 --out dump.pcap -s -T 0xa1b2c3d4 --b 0 tcp or udp\n"
1092 " netsniff-ng --in wlan0 --rfraw --out dump.pcap --silent --bind-cpu 0\n"
1093 " netsniff-ng --in dump.pcap --mmap --out eth0 -k1000 --silent --bind-cpu 0\n"
1094 " netsniff-ng --in dump.pcap --out dump.cfg --silent --bind-cpu 0\n"
1095 " netsniff-ng --in eth0 --out eth1 --silent --bind-cpu 0 --type host\n"
1096 " netsniff-ng --in eth1 --out /opt/probe/ -s -m -J --interval 100MiB -b 0\n"
1097 " netsniff-ng --in vlan0 --out dump.pcap -c -u `id -u bob` -g `id -g bob`\n"
1098 " netsniff-ng --in any --filter http.bpf --jumbo-support --ascii -V\n\n"
1100 " For introducing bit errors, delays with random variation and more\n"
1101 " while replaying pcaps, make use of tc(8) with its disciplines (e.g. netem).\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 version(void)
1114 printf("\nnetsniff-ng %s, the packet sniffing beast\n", VERSION_STRING
);
1115 puts("http://www.netsniff-ng.org\n\n"
1116 "Please report bugs to <bugs@netsniff-ng.org>\n"
1117 "Copyright (C) 2009-2013 Daniel Borkmann <daniel@netsniff-ng.org>\n"
1118 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel@netsniff-ng.org>\n"
1119 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1120 "License: GNU GPL version 2.0\n"
1121 "This is free software: you are free to change and redistribute it.\n"
1122 "There is NO WARRANTY, to the extent permitted by law.\n");
1126 int main(int argc
, char **argv
)
1129 int c
, i
, j
, cpu_tmp
, opt_index
, ops_touched
= 0, vals
[4] = {0};
1130 bool prio_high
= false, setsockmem
= true;
1131 void (*main_loop
)(struct ctx
*ctx
) = NULL
;
1133 .link_type
= LINKTYPE_EN10MB
,
1134 .print_mode
= PRINT_NORM
,
1137 .promiscuous
= true,
1139 .pcap
= PCAP_OPS_SG
,
1140 .dump_interval
= 60,
1141 .dump_mode
= DUMP_INTERVAL_TIME
,
1144 .magic
= ORIGINAL_TCPDUMP_MAGIC
,
1149 while ((c
= getopt_long(argc
, argv
, short_options
, long_options
,
1150 &opt_index
)) != EOF
) {
1154 ctx
.device_in
= xstrdup(optarg
);
1157 ctx
.device_out
= xstrdup(optarg
);
1160 ctx
.prefix
= xstrdup(optarg
);
1163 ctx
.link_type
= LINKTYPE_IEEE802_11
;
1167 ctx
.randomize
= true;
1173 ctx
.magic
= (uint32_t) strtoul(optarg
, NULL
, 0);
1174 pcap_check_magic(ctx
.magic
);
1177 ctx
.filter
= xstrdup(optarg
);
1180 ctx
.promiscuous
= false;
1186 ctx
.uid
= strtoul(optarg
, NULL
, 0);
1190 ctx
.gid
= strtoul(optarg
, NULL
, 0);
1194 if (!strncmp(optarg
, "host", strlen("host")))
1195 ctx
.packet_type
= PACKET_HOST
;
1196 else if (!strncmp(optarg
, "broadcast", strlen("broadcast")))
1197 ctx
.packet_type
= PACKET_BROADCAST
;
1198 else if (!strncmp(optarg
, "multicast", strlen("multicast")))
1199 ctx
.packet_type
= PACKET_MULTICAST
;
1200 else if (!strncmp(optarg
, "others", strlen("others")))
1201 ctx
.packet_type
= PACKET_OTHERHOST
;
1202 else if (!strncmp(optarg
, "outgoing", strlen("outgoing")))
1203 ctx
.packet_type
= PACKET_OUTGOING
;
1205 ctx
.packet_type
= -1;
1209 ctx
.reserve_size
= 0;
1211 for (j
= i
= strlen(optarg
); i
> 0; --i
) {
1212 if (!isdigit(optarg
[j
- i
]))
1217 if (!strncmp(ptr
, "KiB", strlen("KiB")))
1218 ctx
.reserve_size
= 1 << 10;
1219 else if (!strncmp(ptr
, "MiB", strlen("MiB")))
1220 ctx
.reserve_size
= 1 << 20;
1221 else if (!strncmp(ptr
, "GiB", strlen("GiB")))
1222 ctx
.reserve_size
= 1 << 30;
1224 panic("Syntax error in ring size param!\n");
1227 ctx
.reserve_size
*= strtol(optarg
, NULL
, 0);
1230 cpu_tmp
= strtol(optarg
, NULL
, 0);
1232 cpu_affinity(cpu_tmp
);
1240 ctx
.pcap
= PCAP_OPS_RW
;
1244 ctx
.pcap
= PCAP_OPS_MM
;
1248 ctx
.pcap
= PCAP_OPS_SG
;
1255 ctx
.print_mode
= PRINT_NONE
;
1258 ctx
.print_mode
= PRINT_LESS
;
1262 (ctx
.print_mode
== PRINT_ASCII
) ?
1263 PRINT_HEX_ASCII
: PRINT_HEX
;
1267 (ctx
.print_mode
== PRINT_HEX
) ?
1268 PRINT_HEX_ASCII
: PRINT_ASCII
;
1271 ctx
.kpull
= strtol(optarg
, NULL
, 0);
1274 frame_count_max
= strtol(optarg
, NULL
, 0);
1278 ctx
.dump_interval
= 0;
1280 for (j
= i
= strlen(optarg
); i
> 0; --i
) {
1281 if (!isdigit(optarg
[j
- i
]))
1286 if (!strncmp(ptr
, "KiB", strlen("KiB"))) {
1287 ctx
.dump_interval
= 1 << 10;
1288 ctx
.dump_mode
= DUMP_INTERVAL_SIZE
;
1289 } else if (!strncmp(ptr
, "MiB", strlen("MiB"))) {
1290 ctx
.dump_interval
= 1 << 20;
1291 ctx
.dump_mode
= DUMP_INTERVAL_SIZE
;
1292 } else if (!strncmp(ptr
, "GiB", strlen("GiB"))) {
1293 ctx
.dump_interval
= 1 << 30;
1294 ctx
.dump_mode
= DUMP_INTERVAL_SIZE
;
1295 } else if (!strncmp(ptr
, "sec", strlen("sec"))) {
1296 ctx
.dump_interval
= 1;
1297 ctx
.dump_mode
= DUMP_INTERVAL_TIME
;
1298 } else if (!strncmp(ptr
, "min", strlen("min"))) {
1299 ctx
.dump_interval
= 60;
1300 ctx
.dump_mode
= DUMP_INTERVAL_TIME
;
1301 } else if (!strncmp(ptr
, "hrs", strlen("hrs"))) {
1302 ctx
.dump_interval
= 60 * 60;
1303 ctx
.dump_mode
= DUMP_INTERVAL_TIME
;
1304 } else if (!strncmp(ptr
, "s", strlen("s"))) {
1305 ctx
.dump_interval
= 1;
1306 ctx
.dump_mode
= DUMP_INTERVAL_TIME
;
1308 panic("Syntax error in time/size param!\n");
1312 ctx
.dump_interval
*= strtol(optarg
, NULL
, 0);
1318 pcap_dump_type_features();
1344 panic("Option -%c requires an argument!\n",
1347 if (isprint(optopt
))
1348 printf("Unknown option character `0x%X\'!\n", optopt
);
1356 if (!ctx
.filter
&& optind
!= argc
) {
1360 for (i
= optind
; i
< argc
; ++i
) {
1361 size_t alen
= strlen(argv
[i
]) + 2;
1362 size_t flen
= ctx
.filter
? strlen(ctx
.filter
) : 0;
1364 ctx
.filter
= xrealloc(ctx
.filter
, 1, flen
+ alen
);
1365 ret
= slprintf(ctx
.filter
+ offset
, strlen(argv
[i
]) + 2, "%s ", argv
[i
]);
1367 panic("Cannot concatenate filter string!\n");
1374 ctx
.device_in
= xstrdup("any");
1376 register_signal(SIGINT
, signal_handler
);
1377 register_signal(SIGHUP
, signal_handler
);
1382 set_proc_prio(get_default_proc_prio());
1383 set_sched_status(get_default_sched_policy(), get_default_sched_prio());
1386 if (ctx
.device_in
&& (device_mtu(ctx
.device_in
) ||
1387 !strncmp("any", ctx
.device_in
, strlen(ctx
.device_in
)))) {
1388 if (!ctx
.device_out
) {
1390 main_loop
= recv_only_or_dump
;
1391 } else if (device_mtu(ctx
.device_out
)) {
1392 register_signal_f(SIGALRM
, timer_elapsed
, SA_SIGINFO
);
1393 main_loop
= receive_to_xmit
;
1396 register_signal_f(SIGALRM
, timer_next_dump
, SA_SIGINFO
);
1397 main_loop
= recv_only_or_dump
;
1399 ctx
.pcap
= PCAP_OPS_SG
;
1402 if (ctx
.device_out
&& device_mtu(ctx
.device_out
)) {
1403 register_signal_f(SIGALRM
, timer_elapsed
, SA_SIGINFO
);
1404 main_loop
= pcap_to_xmit
;
1406 ctx
.pcap
= PCAP_OPS_MM
;
1408 main_loop
= read_pcap
;
1410 ctx
.pcap
= PCAP_OPS_SG
;
1417 set_system_socket_memory(vals
, array_size(vals
));
1424 reset_system_socket_memory(vals
, array_size(vals
));
1428 free(ctx
.device_in
);
1429 free(ctx
.device_out
);
1430 free(ctx
.device_trans
);