2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2009-2013 Daniel Borkmann.
4 * Copyright 2010 Emmanuel Roullit.
5 * Subject to the GPL, version 2.
16 #include <sys/socket.h>
17 #include <sys/types.h>
20 #include <sys/fsuid.h>
46 #include "tstamping.h"
47 #include "dissector.h"
56 char *device_in
, *device_out
, *device_trans
, *filter
, *prefix
;
57 int cpu
, rfraw
, dump
, print_mode
, dump_dir
, packet_type
, verbose
;
58 unsigned long kpull
, dump_interval
, reserve_size
, tx_bytes
, tx_packets
;
59 bool randomize
, promiscuous
, enforce
, jumbo
, dump_bpf
;
60 enum pcap_ops_groups pcap
; enum dump_mode dump_mode
;
61 uid_t uid
; gid_t gid
; uint32_t link_type
, magic
;
64 static volatile sig_atomic_t sigint
= 0;
65 static volatile bool next_dump
= false;
67 static const char *short_options
= "d:i:o:rf:MJt:S:k:n:b:HQmcsqXlvhF:RGAP:Vu:g:T:DBU";
68 static const struct option long_options
[] = {
69 {"dev", required_argument
, NULL
, 'd'},
70 {"in", required_argument
, NULL
, 'i'},
71 {"out", required_argument
, NULL
, 'o'},
72 {"filter", required_argument
, NULL
, 'f'},
73 {"num", required_argument
, NULL
, 'n'},
74 {"type", required_argument
, NULL
, 't'},
75 {"interval", required_argument
, NULL
, 'F'},
76 {"ring-size", required_argument
, NULL
, 'S'},
77 {"kernel-pull", required_argument
, NULL
, 'k'},
78 {"bind-cpu", required_argument
, NULL
, 'b'},
79 {"prefix", required_argument
, NULL
, 'P'},
80 {"user", required_argument
, NULL
, 'u'},
81 {"group", required_argument
, NULL
, 'g'},
82 {"magic", required_argument
, NULL
, 'T'},
83 {"rand", no_argument
, NULL
, 'r'},
84 {"rfraw", no_argument
, NULL
, 'R'},
85 {"mmap", no_argument
, NULL
, 'm'},
86 {"sg", no_argument
, NULL
, 'G'},
87 {"clrw", no_argument
, NULL
, 'c'},
88 {"jumbo-support", no_argument
, NULL
, 'J'},
89 {"no-promisc", no_argument
, NULL
, 'M'},
90 {"prio-high", no_argument
, NULL
, 'H'},
91 {"notouch-irq", no_argument
, NULL
, 'Q'},
92 {"dump-pcap-types", no_argument
, NULL
, 'D'},
93 {"dump-bpf", no_argument
, NULL
, 'B'},
94 {"silent", no_argument
, NULL
, 's'},
95 {"less", no_argument
, NULL
, 'q'},
96 {"hex", no_argument
, NULL
, 'X'},
97 {"ascii", no_argument
, NULL
, 'l'},
98 {"no-sock-mem", no_argument
, NULL
, 'A'},
99 {"update", no_argument
, NULL
, 'U'},
100 {"verbose", no_argument
, NULL
, 'V'},
101 {"version", no_argument
, NULL
, 'v'},
102 {"help", no_argument
, NULL
, 'h'},
107 static struct itimerval itimer
;
108 static unsigned long frame_count_max
= 0, interval
= TX_KERNEL_PULL_INT
;
110 #define __pcap_io pcap_ops[ctx->pcap]
112 static void signal_handler(int number
)
123 static void timer_elapsed(int unused __maybe_unused
)
127 set_itimer_interval_value(&itimer
, 0, interval
);
129 ret
= pull_and_flush_tx_ring(tx_sock
);
130 if (unlikely(ret
< 0)) {
131 /* We could hit EBADF if the socket has been closed before
132 * the timer was triggered.
134 if (errno
!= EBADF
&& errno
!= ENOBUFS
)
135 panic("Flushing TX_RING failed: %s!\n", strerror(errno
));
138 setitimer(ITIMER_REAL
, &itimer
, NULL
);
141 static void timer_purge(void)
145 ret
= pull_and_flush_tx_ring_wait(tx_sock
);
146 if (unlikely(ret
< 0)) {
147 if (errno
!= EBADF
&& errno
!= ENOBUFS
)
148 panic("Flushing TX_RING failed: %s!\n", strerror(errno
));
151 set_itimer_interval_value(&itimer
, 0, 0);
152 setitimer(ITIMER_REAL
, &itimer
, NULL
);
155 static void timer_next_dump(int unused __maybe_unused
)
157 set_itimer_interval_value(&itimer
, interval
, 0);
159 setitimer(ITIMER_REAL
, &itimer
, NULL
);
162 static inline bool dump_to_pcap(struct ctx
*ctx
)
167 static void pcap_to_xmit(struct ctx
*ctx
)
171 int irq
, ifindex
, fd
= 0, ret
;
172 unsigned int size
, it
= 0;
173 unsigned long trunced
= 0;
175 struct frame_map
*hdr
;
176 struct sock_fprog bpf_ops
;
177 struct timeval start
, end
, diff
;
180 if (!device_up_and_running(ctx
->device_out
) && !ctx
->rfraw
)
181 panic("Device not up and running!\n");
185 tx_sock
= pf_socket();
187 if (!strncmp("-", ctx
->device_in
, strlen("-"))) {
188 fd
= dup_or_die(fileno(stdin
));
189 close(fileno(stdin
));
190 if (ctx
->pcap
== PCAP_OPS_MM
)
191 ctx
->pcap
= PCAP_OPS_SG
;
193 fd
= open_or_die(ctx
->device_in
, O_RDONLY
| O_LARGEFILE
| O_NOATIME
);
196 if (__pcap_io
->init_once_pcap
)
197 __pcap_io
->init_once_pcap();
199 ret
= __pcap_io
->pull_fhdr_pcap(fd
, &ctx
->magic
, &ctx
->link_type
);
201 panic("Error reading pcap header!\n");
203 if (__pcap_io
->prepare_access_pcap
) {
204 ret
= __pcap_io
->prepare_access_pcap(fd
, PCAP_MODE_RD
, ctx
->jumbo
);
206 panic("Error prepare reading pcap!\n");
209 fmemset(&tx_ring
, 0, sizeof(tx_ring
));
210 fmemset(&bpf_ops
, 0, sizeof(bpf_ops
));
213 ctx
->device_trans
= xstrdup(ctx
->device_out
);
214 xfree(ctx
->device_out
);
216 enter_rfmon_mac80211(ctx
->device_trans
, &ctx
->device_out
);
217 if (ctx
->link_type
!= LINKTYPE_IEEE802_11
)
218 panic("Wrong linktype of pcap!\n");
221 ifindex
= device_ifindex(ctx
->device_out
);
223 size
= ring_size(ctx
->device_out
, ctx
->reserve_size
);
225 bpf_parse_rules(ctx
->filter
, &bpf_ops
, ctx
->link_type
);
227 bpf_dump_all(&bpf_ops
);
229 set_packet_loss_discard(tx_sock
);
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_sock
, &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_set_irq_affinity(irq
, ctx
->cpu
);
244 printf("IRQ: %s:%d > CPU%d\n",
245 ctx
->device_out
, irq
, ctx
->cpu
);
249 interval
= ctx
->kpull
;
251 set_itimer_interval_value(&itimer
, 0, interval
);
252 setitimer(ITIMER_REAL
, &itimer
, NULL
);
254 drop_privileges(ctx
->enforce
, ctx
->uid
, ctx
->gid
);
256 printf("Running! Hang up with ^C!\n\n");
259 bug_on(gettimeofday(&start
, NULL
));
261 while (likely(sigint
== 0)) {
262 while (user_may_pull_from_tx(tx_ring
.frames
[it
].iov_base
)) {
263 hdr
= tx_ring
.frames
[it
].iov_base
;
264 out
= ((uint8_t *) hdr
) + TPACKET2_HDRLEN
- sizeof(struct sockaddr_ll
);
267 ret
= __pcap_io
->read_pcap(fd
, &phdr
, ctx
->magic
, out
,
268 ring_frame_size(&tx_ring
));
269 if (unlikely(ret
<= 0))
272 if (ring_frame_size(&tx_ring
) <
273 pcap_get_length(&phdr
, ctx
->magic
)) {
274 pcap_set_length(&phdr
, ctx
->magic
,
275 ring_frame_size(&tx_ring
));
278 } while (ctx
->filter
&&
279 !bpf_run_filter(&bpf_ops
, out
,
280 pcap_get_length(&phdr
, ctx
->magic
)));
282 pcap_pkthdr_to_tpacket_hdr(&phdr
, ctx
->magic
, &hdr
->tp_h
);
284 ctx
->tx_bytes
+= hdr
->tp_h
.tp_len
;;
287 show_frame_hdr(hdr
, ctx
->print_mode
);
289 dissector_entry_point(out
, hdr
->tp_h
.tp_snaplen
,
290 ctx
->link_type
, ctx
->print_mode
);
292 kernel_may_pull_from_tx(&hdr
->tp_h
);
295 if (it
>= tx_ring
.layout
.tp_frame_nr
)
298 if (unlikely(sigint
== 1))
301 if (frame_count_max
!= 0) {
302 if (ctx
->tx_packets
>= frame_count_max
) {
312 bug_on(gettimeofday(&end
, NULL
));
313 timersub(&end
, &start
, &diff
);
317 bpf_release(&bpf_ops
);
319 dissector_cleanup_all();
320 destroy_tx_ring(tx_sock
, &tx_ring
);
323 leave_rfmon_mac80211(ctx
->device_out
);
325 if (__pcap_io
->prepare_close_pcap
)
326 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_RD
);
328 if (!strncmp("-", ctx
->device_in
, strlen("-")))
329 dup2(fd
, fileno(stdin
));
336 printf("\r%12lu packets outgoing\n", ctx
->tx_packets
);
337 printf("\r%12lu packets truncated in file\n", trunced
);
338 printf("\r%12lu bytes outgoing\n", ctx
->tx_bytes
);
339 printf("\r%12lu sec, %lu usec in total\n", diff
.tv_sec
, diff
.tv_usec
);
342 static void receive_to_xmit(struct ctx
*ctx
)
346 int rx_sock
, ifindex_in
, ifindex_out
, ret
;
347 unsigned int size_in
, size_out
, it_in
= 0, it_out
= 0;
348 unsigned long frame_count
= 0;
349 struct frame_map
*hdr_in
, *hdr_out
;
350 struct ring tx_ring
, rx_ring
;
351 struct pollfd rx_poll
;
352 struct sock_fprog bpf_ops
;
354 if (!strncmp(ctx
->device_in
, ctx
->device_out
, IFNAMSIZ
))
355 panic("Ingress/egress devices must be different!\n");
356 if (!device_up_and_running(ctx
->device_out
))
357 panic("Egress device not up and running!\n");
359 rx_sock
= pf_socket();
360 tx_sock
= pf_socket();
362 fmemset(&tx_ring
, 0, sizeof(tx_ring
));
363 fmemset(&rx_ring
, 0, sizeof(rx_ring
));
364 fmemset(&rx_poll
, 0, sizeof(rx_poll
));
365 fmemset(&bpf_ops
, 0, sizeof(bpf_ops
));
367 ifindex_in
= device_ifindex(ctx
->device_in
);
368 ifindex_out
= device_ifindex(ctx
->device_out
);
370 size_in
= ring_size(ctx
->device_in
, ctx
->reserve_size
);
371 size_out
= ring_size(ctx
->device_out
, ctx
->reserve_size
);
373 enable_kernel_bpf_jit_compiler();
375 bpf_parse_rules(ctx
->filter
, &bpf_ops
, ctx
->link_type
);
377 bpf_dump_all(&bpf_ops
);
378 bpf_attach_to_sock(rx_sock
, &bpf_ops
);
380 setup_rx_ring_layout(rx_sock
, &rx_ring
, size_in
, ctx
->jumbo
, false);
381 create_rx_ring(rx_sock
, &rx_ring
, ctx
->verbose
);
382 mmap_rx_ring(rx_sock
, &rx_ring
);
383 alloc_rx_ring_frames(rx_sock
, &rx_ring
);
384 bind_rx_ring(rx_sock
, &rx_ring
, ifindex_in
);
385 prepare_polling(rx_sock
, &rx_poll
);
387 set_packet_loss_discard(tx_sock
);
388 setup_tx_ring_layout(tx_sock
, &tx_ring
, size_out
, ctx
->jumbo
);
389 create_tx_ring(tx_sock
, &tx_ring
, ctx
->verbose
);
390 mmap_tx_ring(tx_sock
, &tx_ring
);
391 alloc_tx_ring_frames(tx_sock
, &tx_ring
);
392 bind_tx_ring(tx_sock
, &tx_ring
, ifindex_out
);
394 dissector_init_all(ctx
->print_mode
);
396 if (ctx
->promiscuous
)
397 ifflags
= device_enter_promiscuous_mode(ctx
->device_in
);
400 interval
= ctx
->kpull
;
402 set_itimer_interval_value(&itimer
, 0, 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 ret
= poll(&rx_poll
, 1, -1);
477 if (unlikely(ret
< 0)) {
479 panic("Poll failed!\n");
487 sock_rx_net_stats(rx_sock
, 0);
489 bpf_release(&bpf_ops
);
491 dissector_cleanup_all();
493 destroy_tx_ring(tx_sock
, &tx_ring
);
494 destroy_rx_ring(rx_sock
, &rx_ring
);
496 if (ctx
->promiscuous
)
497 device_leave_promiscuous_mode(ctx
->device_in
, ifflags
);
503 static void translate_pcap_to_txf(int fdo
, uint8_t *out
, size_t len
)
505 size_t bytes_done
= 0;
508 slprintf(bout
, sizeof(bout
), "{\n ");
509 write_or_die(fdo
, bout
, strlen(bout
));
511 while (bytes_done
< len
) {
512 slprintf(bout
, sizeof(bout
), "0x%02x, ", out
[bytes_done
]);
513 write_or_die(fdo
, bout
, strlen(bout
));
517 if (bytes_done
% 10 == 0) {
518 slprintf(bout
, sizeof(bout
), "\n");
519 write_or_die(fdo
, bout
, strlen(bout
));
521 if (bytes_done
< len
) {
522 slprintf(bout
, sizeof(bout
), " ");
523 write_or_die(fdo
, bout
, strlen(bout
));
527 if (bytes_done
% 10 != 0) {
528 slprintf(bout
, sizeof(bout
), "\n");
529 write_or_die(fdo
, bout
, strlen(bout
));
532 slprintf(bout
, sizeof(bout
), "}\n\n");
533 write_or_die(fdo
, bout
, strlen(bout
));
536 static void read_pcap(struct ctx
*ctx
)
540 int ret
, fd
, fdo
= 0;
541 unsigned long trunced
= 0;
544 struct sock_fprog bpf_ops
;
546 struct timeval start
, end
, diff
;
550 if (!strncmp("-", ctx
->device_in
, strlen("-"))) {
551 fd
= dup_or_die(fileno(stdin
));
552 close(fileno(stdin
));
553 if (ctx
->pcap
== PCAP_OPS_MM
)
554 ctx
->pcap
= PCAP_OPS_SG
;
556 fd
= open_or_die(ctx
->device_in
, O_RDONLY
| O_LARGEFILE
| O_NOATIME
);
559 if (__pcap_io
->init_once_pcap
)
560 __pcap_io
->init_once_pcap();
562 ret
= __pcap_io
->pull_fhdr_pcap(fd
, &ctx
->magic
, &ctx
->link_type
);
564 panic("Error reading pcap header!\n");
566 if (__pcap_io
->prepare_access_pcap
) {
567 ret
= __pcap_io
->prepare_access_pcap(fd
, PCAP_MODE_RD
, ctx
->jumbo
);
569 panic("Error prepare reading pcap!\n");
572 fmemset(&fm
, 0, sizeof(fm
));
573 fmemset(&bpf_ops
, 0, sizeof(bpf_ops
));
575 bpf_parse_rules(ctx
->filter
, &bpf_ops
, ctx
->link_type
);
577 bpf_dump_all(&bpf_ops
);
579 dissector_init_all(ctx
->print_mode
);
581 out_len
= round_up(1024 * 1024, PAGE_SIZE
);
582 out
= xmalloc_aligned(out_len
, CO_CACHE_LINE_SIZE
);
584 if (ctx
->device_out
) {
585 if (!strncmp("-", ctx
->device_out
, strlen("-"))) {
586 fdo
= dup_or_die(fileno(stdout
));
587 close(fileno(stdout
));
589 fdo
= open_or_die_m(ctx
->device_out
, O_RDWR
| O_CREAT
|
590 O_TRUNC
| O_LARGEFILE
, DEFFILEMODE
);
594 drop_privileges(ctx
->enforce
, ctx
->uid
, ctx
->gid
);
596 printf("Running! Hang up with ^C!\n\n");
599 bug_on(gettimeofday(&start
, NULL
));
601 while (likely(sigint
== 0)) {
603 ret
= __pcap_io
->read_pcap(fd
, &phdr
, ctx
->magic
,
605 if (unlikely(ret
< 0))
608 if (unlikely(pcap_get_length(&phdr
, ctx
->magic
) == 0)) {
613 if (unlikely(pcap_get_length(&phdr
, ctx
->magic
) > out_len
)) {
614 pcap_set_length(&phdr
, ctx
->magic
, out_len
);
617 } while (ctx
->filter
&&
618 !bpf_run_filter(&bpf_ops
, out
,
619 pcap_get_length(&phdr
, ctx
->magic
)));
621 pcap_pkthdr_to_tpacket_hdr(&phdr
, ctx
->magic
, &fm
.tp_h
);
623 ctx
->tx_bytes
+= fm
.tp_h
.tp_len
;
626 show_frame_hdr(&fm
, ctx
->print_mode
);
628 dissector_entry_point(out
, fm
.tp_h
.tp_snaplen
,
629 ctx
->link_type
, ctx
->print_mode
);
632 translate_pcap_to_txf(fdo
, out
, fm
.tp_h
.tp_snaplen
);
634 if (frame_count_max
!= 0) {
635 if (ctx
->tx_packets
>= frame_count_max
) {
644 bug_on(gettimeofday(&end
, NULL
));
645 timersub(&end
, &start
, &diff
);
647 bpf_release(&bpf_ops
);
649 dissector_cleanup_all();
651 if (__pcap_io
->prepare_close_pcap
)
652 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_RD
);
658 printf("\r%12lu packets outgoing\n", ctx
->tx_packets
);
659 printf("\r%12lu packets truncated in file\n", trunced
);
660 printf("\r%12lu bytes outgoing\n", ctx
->tx_bytes
);
661 printf("\r%12lu sec, %lu usec in total\n", diff
.tv_sec
, diff
.tv_usec
);
663 if (!strncmp("-", ctx
->device_in
, strlen("-")))
664 dup2(fd
, fileno(stdin
));
667 if (ctx
->device_out
) {
668 if (!strncmp("-", ctx
->device_out
, strlen("-")))
669 dup2(fdo
, fileno(stdout
));
674 static void finish_multi_pcap_file(struct ctx
*ctx
, int fd
)
676 __pcap_io
->fsync_pcap(fd
);
678 if (__pcap_io
->prepare_close_pcap
)
679 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_WR
);
683 fmemset(&itimer
, 0, sizeof(itimer
));
684 setitimer(ITIMER_REAL
, &itimer
, NULL
);
687 static int next_multi_pcap_file(struct ctx
*ctx
, int fd
)
692 __pcap_io
->fsync_pcap(fd
);
694 if (__pcap_io
->prepare_close_pcap
)
695 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_WR
);
699 slprintf(fname
, sizeof(fname
), "%s/%s%lu.pcap", ctx
->device_out
,
700 ctx
->prefix
? : "dump-", time(NULL
));
702 fd
= open_or_die_m(fname
, O_RDWR
| O_CREAT
| O_TRUNC
|
703 O_LARGEFILE
, DEFFILEMODE
);
705 ret
= __pcap_io
->push_fhdr_pcap(fd
, ctx
->magic
, ctx
->link_type
);
707 panic("Error writing pcap header!\n");
709 if (__pcap_io
->prepare_access_pcap
) {
710 ret
= __pcap_io
->prepare_access_pcap(fd
, PCAP_MODE_WR
, true);
712 panic("Error prepare writing pcap!\n");
718 static int begin_multi_pcap_file(struct ctx
*ctx
)
725 if (ctx
->device_out
[strlen(ctx
->device_out
) - 1] == '/')
726 ctx
->device_out
[strlen(ctx
->device_out
) - 1] = 0;
728 slprintf(fname
, sizeof(fname
), "%s/%s%lu.pcap", ctx
->device_out
,
729 ctx
->prefix
? : "dump-", time(NULL
));
731 fd
= open_or_die_m(fname
, O_RDWR
| O_CREAT
| O_TRUNC
|
732 O_LARGEFILE
, DEFFILEMODE
);
734 ret
= __pcap_io
->push_fhdr_pcap(fd
, ctx
->magic
, ctx
->link_type
);
736 panic("Error writing pcap header!\n");
738 if (__pcap_io
->prepare_access_pcap
) {
739 ret
= __pcap_io
->prepare_access_pcap(fd
, PCAP_MODE_WR
, true);
741 panic("Error prepare writing pcap!\n");
744 if (ctx
->dump_mode
== DUMP_INTERVAL_TIME
) {
745 interval
= ctx
->dump_interval
;
747 set_itimer_interval_value(&itimer
, interval
, 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_or_die(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
, true);
793 panic("Error prepare writing pcap!\n");
799 static void print_pcap_file_stats(int sock
, struct ctx
*ctx
)
802 struct tpacket_stats kstats
;
803 socklen_t slen
= sizeof(kstats
);
805 fmemset(&kstats
, 0, sizeof(kstats
));
807 ret
= getsockopt(sock
, SOL_PACKET
, PACKET_STATISTICS
, &kstats
, &slen
);
809 panic("Cannot get packet statistics!\n");
811 if (ctx
->print_mode
== PRINT_NONE
) {
812 printf(".(+%u/-%u)", kstats
.tp_packets
- kstats
.tp_drops
,
818 static void walk_t3_block(struct block_desc
*pbd
, struct ctx
*ctx
,
819 int sock
, int *fd
, unsigned long *frame_count
)
822 int num_pkts
= pbd
->h1
.num_pkts
, i
, ret
;
823 struct tpacket3_hdr
*hdr
;
825 struct sockaddr_ll
*sll
;
827 hdr
= (void *) ((uint8_t *) pbd
+ pbd
->h1
.offset_to_first_pkt
);
828 sll
= (void *) ((uint8_t *) hdr
+ TPACKET_ALIGN(sizeof(*hdr
)));
830 for (i
= 0; i
< num_pkts
&& likely(sigint
== 0); ++i
) {
832 packet
= ((uint8_t *) hdr
+ hdr
->tp_mac
);
834 if (ctx
->packet_type
!= -1)
835 if (ctx
->packet_type
!= sll
->sll_pkttype
)
840 if (dump_to_pcap(ctx
)) {
841 tpacket3_hdr_to_pcap_pkthdr(hdr
, sll
, &phdr
, ctx
->magic
);
843 ret
= __pcap_io
->write_pcap(*fd
, &phdr
, ctx
->magic
, packet
,
844 pcap_get_length(&phdr
, ctx
->magic
));
845 if (unlikely(ret
!= (int) pcap_get_total_length(&phdr
, ctx
->magic
)))
846 panic("Write error to pcap!\n");
849 __show_frame_hdr(sll
, hdr
, ctx
->print_mode
, true);
851 dissector_entry_point(packet
, hdr
->tp_snaplen
, ctx
->link_type
,
855 hdr
= (void *) ((uint8_t *) hdr
+ hdr
->tp_next_offset
);
856 sll
= (void *) ((uint8_t *) hdr
+ TPACKET_ALIGN(sizeof(*hdr
)));
858 if (frame_count_max
!= 0) {
859 if (unlikely(*frame_count
>= frame_count_max
)) {
865 if (dump_to_pcap(ctx
)) {
866 if (ctx
->dump_mode
== DUMP_INTERVAL_SIZE
) {
867 interval
+= hdr
->tp_snaplen
;
868 if (interval
> ctx
->dump_interval
) {
875 *fd
= next_multi_pcap_file(ctx
, *fd
);
878 if (unlikely(ctx
->verbose
))
879 print_pcap_file_stats(sock
, ctx
);
885 static void recv_only_or_dump(struct ctx
*ctx
)
888 int sock
, irq
, ifindex
, fd
= 0, ret
;
889 unsigned int size
, it
= 0;
891 struct pollfd rx_poll
;
892 struct sock_fprog bpf_ops
;
893 struct timeval start
, end
, diff
;
894 struct block_desc
*pbd
;
895 unsigned long frame_count
= 0;
900 ctx
->device_trans
= xstrdup(ctx
->device_in
);
901 xfree(ctx
->device_in
);
903 enter_rfmon_mac80211(ctx
->device_trans
, &ctx
->device_in
);
904 ctx
->link_type
= LINKTYPE_IEEE802_11
;
907 fmemset(&rx_ring
, 0, sizeof(rx_ring
));
908 fmemset(&rx_poll
, 0, sizeof(rx_poll
));
909 fmemset(&bpf_ops
, 0, sizeof(bpf_ops
));
911 ifindex
= device_ifindex(ctx
->device_in
);
913 size
= ring_size(ctx
->device_in
, ctx
->reserve_size
);
915 enable_kernel_bpf_jit_compiler();
917 bpf_parse_rules(ctx
->filter
, &bpf_ops
, ctx
->link_type
);
919 bpf_dump_all(&bpf_ops
);
920 bpf_attach_to_sock(sock
, &bpf_ops
);
922 ret
= set_sockopt_hwtimestamp(sock
, ctx
->device_in
);
923 if (ret
== 0 && ctx
->verbose
)
924 printf("HW timestamping enabled\n");
926 setup_rx_ring_layout(sock
, &rx_ring
, size
, true, true);
927 create_rx_ring(sock
, &rx_ring
, ctx
->verbose
);
928 mmap_rx_ring(sock
, &rx_ring
);
929 alloc_rx_ring_frames(sock
, &rx_ring
);
930 bind_rx_ring(sock
, &rx_ring
, ifindex
);
932 prepare_polling(sock
, &rx_poll
);
933 dissector_init_all(ctx
->print_mode
);
935 if (ctx
->cpu
>= 0 && ifindex
> 0) {
936 irq
= device_irq_number(ctx
->device_in
);
937 device_set_irq_affinity(irq
, ctx
->cpu
);
940 printf("IRQ: %s:%d > CPU%d\n",
941 ctx
->device_in
, irq
, ctx
->cpu
);
944 if (ctx
->promiscuous
)
945 ifflags
= device_enter_promiscuous_mode(ctx
->device_in
);
947 if (dump_to_pcap(ctx
) && __pcap_io
->init_once_pcap
)
948 __pcap_io
->init_once_pcap();
950 drop_privileges(ctx
->enforce
, ctx
->uid
, ctx
->gid
);
952 if (dump_to_pcap(ctx
)) {
955 ret
= stat(ctx
->device_out
, &stats
);
959 ctx
->dump_dir
= S_ISDIR(stats
.st_mode
);
962 fd
= begin_multi_pcap_file(ctx
);
964 fd
= begin_single_pcap_file(ctx
);
967 printf("Running! Hang up with ^C!\n\n");
970 bug_on(gettimeofday(&start
, NULL
));
972 while (likely(sigint
== 0)) {
973 while (user_may_pull_from_rx_block((pbd
= (void *)
974 rx_ring
.frames
[it
].iov_base
))) {
975 walk_t3_block(pbd
, ctx
, sock
, &fd
, &frame_count
);
977 kernel_may_pull_from_rx_block(pbd
);
978 it
= (it
+ 1) % rx_ring
.layout3
.tp_block_nr
;
980 if (unlikely(sigint
== 1))
984 ret
= poll(&rx_poll
, 1, -1);
985 if (unlikely(ret
< 0)) {
987 panic("Poll failed!\n");
991 bug_on(gettimeofday(&end
, NULL
));
992 timersub(&end
, &start
, &diff
);
994 if (!(ctx
->dump_dir
&& ctx
->print_mode
== PRINT_NONE
)) {
995 sock_rx_net_stats(sock
, frame_count
);
997 printf("\r%12lu sec, %lu usec in total\n",
998 diff
.tv_sec
, diff
.tv_usec
);
1004 bpf_release(&bpf_ops
);
1005 dissector_cleanup_all();
1006 destroy_rx_ring(sock
, &rx_ring
);
1008 if (ctx
->promiscuous
)
1009 device_leave_promiscuous_mode(ctx
->device_in
, ifflags
);
1012 leave_rfmon_mac80211(ctx
->device_in
);
1014 if (dump_to_pcap(ctx
)) {
1016 finish_multi_pcap_file(ctx
, fd
);
1018 finish_single_pcap_file(ctx
, fd
);
1024 static void init_ctx(struct ctx
*ctx
)
1026 memset(ctx
, 0, sizeof(*ctx
));
1027 ctx
->uid
= getuid();
1028 ctx
->uid
= getgid();
1031 ctx
->packet_type
= -1;
1033 ctx
->magic
= ORIGINAL_TCPDUMP_MAGIC
;
1034 ctx
->print_mode
= PRINT_NORM
;
1035 ctx
->pcap
= PCAP_OPS_SG
;
1037 ctx
->dump_mode
= DUMP_INTERVAL_TIME
;
1038 ctx
->dump_interval
= 60;
1040 ctx
->promiscuous
= true;
1041 ctx
->randomize
= false;
1044 static void destroy_ctx(struct ctx
*ctx
)
1046 free(ctx
->device_in
);
1047 free(ctx
->device_out
);
1048 free(ctx
->device_trans
);
1053 static void __noreturn
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 " -R|--rfraw Capture or inject raw 802.11 frames\n"
1065 " -n|--num <0|uint> Number of packets until exit (def: 0)\n"
1066 " -P|--prefix <name> Prefix for pcaps stored in directory\n"
1067 " -T|--magic <pcap-magic> Pcap magic number/pcap format to store, see -D\n"
1068 " -D|--dump-pcap-types Dump pcap types and magic numbers and quit\n"
1069 " -B|--dump-bpf Dump generated BPF assembly\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 " -J|--jumbo-support Support replay/fwd 64KB Super Jumbo Frames (def: 2048B)\n"
1079 " -b|--bind-cpu <cpu> Bind to specific CPU\n"
1080 " -u|--user <userid> Drop privileges and change to userid\n"
1081 " -g|--group <groupid> Drop privileges and change to groupid\n"
1082 " -H|--prio-high Make this high priority process\n"
1083 " -Q|--notouch-irq Do not touch IRQ CPU affinity of NIC\n"
1084 " -s|--silent Do not print captured packets\n"
1085 " -q|--less Print less-verbose packet information\n"
1086 " -X|--hex Print packet data in hex format\n"
1087 " -l|--ascii Print human-readable packet data\n"
1088 " -U|--update Update GeoIP databases\n"
1089 " -V|--verbose Be more verbose\n"
1090 " -v|--version Show version and exit\n"
1091 " -h|--help Guess what?!\n\n"
1093 " netsniff-ng --in eth0 --out dump.pcap -s -T 0xa1b2c3d4 --b 0 tcp or udp\n"
1094 " netsniff-ng --in wlan0 --rfraw --out dump.pcap --silent --bind-cpu 0\n"
1095 " netsniff-ng --in dump.pcap --mmap --out eth0 -k1000 --silent --bind-cpu 0\n"
1096 " netsniff-ng --in dump.pcap --out dump.cfg --silent --bind-cpu 0\n"
1097 " netsniff-ng --in eth0 --out eth1 --silent --bind-cpu 0 -J --type host\n"
1098 " netsniff-ng --in eth1 --out /opt/probe/ -s -m --interval 100MiB -b 0\n"
1099 " netsniff-ng --in vlan0 --out dump.pcap -c -u `id -u bob` -g `id -g bob`\n"
1100 " netsniff-ng --in any --filter http.bpf --jumbo-support --ascii -V\n\n"
1102 " For introducing bit errors, delays with random variation and more\n"
1103 " while replaying pcaps, make use of tc(8) with its disciplines (e.g. netem).\n\n"
1104 "Please report bugs to <bugs@netsniff-ng.org>\n"
1105 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
1106 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel.roullit@gmail.com>\n"
1107 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1108 "Swiss federal institute of technology (ETH Zurich)\n"
1109 "License: GNU GPL version 2.0\n"
1110 "This is free software: you are free to change and redistribute it.\n"
1111 "There is NO WARRANTY, to the extent permitted by law.\n");
1115 static void __noreturn
version(void)
1117 printf("\nnetsniff-ng %s, Git id: %s\n", VERSION_LONG
, GITVERSION
);
1118 puts("the packet sniffing beast\n"
1119 "http://www.netsniff-ng.org\n\n"
1120 "Please report bugs to <bugs@netsniff-ng.org>\n"
1121 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
1122 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel.roullit@gmail.com>\n"
1123 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1124 "Swiss federal institute of technology (ETH Zurich)\n"
1125 "License: GNU GPL version 2.0\n"
1126 "This is free software: you are free to change and redistribute it.\n"
1127 "There is NO WARRANTY, to the extent permitted by law.\n");
1131 int main(int argc
, char **argv
)
1134 int c
, i
, j
, cpu_tmp
, opt_index
, ops_touched
= 0, vals
[4] = {0};
1135 bool prio_high
= false, setsockmem
= true;
1136 void (*main_loop
)(struct ctx
*ctx
) = NULL
;
1142 while ((c
= getopt_long(argc
, argv
, short_options
, long_options
,
1143 &opt_index
)) != EOF
) {
1147 ctx
.device_in
= xstrdup(optarg
);
1150 ctx
.device_out
= xstrdup(optarg
);
1153 ctx
.prefix
= xstrdup(optarg
);
1156 ctx
.link_type
= LINKTYPE_IEEE802_11
;
1160 ctx
.randomize
= true;
1166 ctx
.magic
= (uint32_t) strtoul(optarg
, NULL
, 0);
1167 pcap_check_magic(ctx
.magic
);
1170 ctx
.filter
= xstrdup(optarg
);
1173 ctx
.promiscuous
= false;
1179 ctx
.uid
= strtoul(optarg
, NULL
, 0);
1183 ctx
.gid
= strtoul(optarg
, NULL
, 0);
1187 if (!strncmp(optarg
, "host", strlen("host")))
1188 ctx
.packet_type
= PACKET_HOST
;
1189 else if (!strncmp(optarg
, "broadcast", strlen("broadcast")))
1190 ctx
.packet_type
= PACKET_BROADCAST
;
1191 else if (!strncmp(optarg
, "multicast", strlen("multicast")))
1192 ctx
.packet_type
= PACKET_MULTICAST
;
1193 else if (!strncmp(optarg
, "others", strlen("others")))
1194 ctx
.packet_type
= PACKET_OTHERHOST
;
1195 else if (!strncmp(optarg
, "outgoing", strlen("outgoing")))
1196 ctx
.packet_type
= PACKET_OUTGOING
;
1198 ctx
.packet_type
= -1;
1202 ctx
.reserve_size
= 0;
1204 for (j
= i
= strlen(optarg
); i
> 0; --i
) {
1205 if (!isdigit(optarg
[j
- i
]))
1210 if (!strncmp(ptr
, "KiB", strlen("KiB")))
1211 ctx
.reserve_size
= 1 << 10;
1212 else if (!strncmp(ptr
, "MiB", strlen("MiB")))
1213 ctx
.reserve_size
= 1 << 20;
1214 else if (!strncmp(ptr
, "GiB", strlen("GiB")))
1215 ctx
.reserve_size
= 1 << 30;
1217 panic("Syntax error in ring size param!\n");
1220 ctx
.reserve_size
*= strtol(optarg
, NULL
, 0);
1223 cpu_tmp
= strtol(optarg
, NULL
, 0);
1225 cpu_affinity(cpu_tmp
);
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 ctx
.dump_bpf
= true;
1314 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
);
1383 set_sched_status(SCHED_FIFO
, sched_get_priority_max(SCHED_FIFO
));
1386 if (ctx
.device_in
&& (device_mtu(ctx
.device_in
) ||
1387 !strncmp("any", ctx
.device_in
, strlen(ctx
.device_in
)))) {
1389 ctx
.link_type
= pcap_devtype_to_linktype(ctx
.device_in
);
1390 if (!ctx
.device_out
) {
1392 main_loop
= recv_only_or_dump
;
1393 } else if (device_mtu(ctx
.device_out
)) {
1394 register_signal_f(SIGALRM
, timer_elapsed
, SA_SIGINFO
);
1395 main_loop
= receive_to_xmit
;
1398 register_signal_f(SIGALRM
, timer_next_dump
, SA_SIGINFO
);
1399 main_loop
= recv_only_or_dump
;
1401 ctx
.pcap
= PCAP_OPS_SG
;
1404 if (ctx
.device_out
&& device_mtu(ctx
.device_out
)) {
1405 register_signal_f(SIGALRM
, timer_elapsed
, SA_SIGINFO
);
1406 main_loop
= pcap_to_xmit
;
1408 ctx
.pcap
= PCAP_OPS_MM
;
1410 main_loop
= read_pcap
;
1412 ctx
.pcap
= PCAP_OPS_SG
;
1420 set_system_socket_memory(vals
, array_size(vals
));
1429 reset_system_socket_memory(vals
, array_size(vals
));
1432 device_restore_irq_affinity_list();