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>
38 #include "dissector.h"
41 extern int set_sockopt_hwtimestamp(int sock
, const char *dev
);
49 char *device_in
, *device_out
, *device_trans
, *filter
, *prefix
;
50 int cpu
, rfraw
, dump
, print_mode
, dump_dir
, packet_type
, verbose
;
51 unsigned long kpull
, dump_interval
, reserve_size
, tx_bytes
, tx_packets
;
52 bool randomize
, promiscuous
, enforce
, jumbo
, dump_bpf
;
53 enum pcap_ops_groups pcap
; enum dump_mode dump_mode
;
54 uid_t uid
; gid_t gid
; uint32_t link_type
, magic
;
57 volatile sig_atomic_t sigint
= 0;
59 static volatile bool next_dump
= false;
61 static const char *short_options
= "d:i:o:rf:MJt:S:k:n:b:HQmcsqXlvhF:RGAP:Vu:g:T:DB";
62 static const struct option long_options
[] = {
63 {"dev", required_argument
, NULL
, 'd'},
64 {"in", required_argument
, NULL
, 'i'},
65 {"out", required_argument
, NULL
, 'o'},
66 {"filter", required_argument
, NULL
, 'f'},
67 {"num", required_argument
, NULL
, 'n'},
68 {"type", required_argument
, NULL
, 't'},
69 {"interval", required_argument
, NULL
, 'F'},
70 {"ring-size", required_argument
, NULL
, 'S'},
71 {"kernel-pull", required_argument
, NULL
, 'k'},
72 {"bind-cpu", required_argument
, NULL
, 'b'},
73 {"prefix", required_argument
, NULL
, 'P'},
74 {"user", required_argument
, NULL
, 'u'},
75 {"group", required_argument
, NULL
, 'g'},
76 {"magic", required_argument
, NULL
, 'T'},
77 {"rand", no_argument
, NULL
, 'r'},
78 {"rfraw", no_argument
, NULL
, 'R'},
79 {"mmap", no_argument
, NULL
, 'm'},
80 {"sg", no_argument
, NULL
, 'G'},
81 {"clrw", no_argument
, NULL
, 'c'},
82 {"jumbo-support", no_argument
, NULL
, 'J'},
83 {"no-promisc", no_argument
, NULL
, 'M'},
84 {"prio-high", no_argument
, NULL
, 'H'},
85 {"notouch-irq", no_argument
, NULL
, 'Q'},
86 {"dump-pcap-types", no_argument
, NULL
, 'D'},
87 {"dump-bpf", no_argument
, NULL
, 'B'},
88 {"silent", no_argument
, NULL
, 's'},
89 {"less", no_argument
, NULL
, 'q'},
90 {"hex", no_argument
, NULL
, 'X'},
91 {"ascii", no_argument
, NULL
, 'l'},
92 {"no-sock-mem", no_argument
, NULL
, 'A'},
93 {"update", no_argument
, NULL
, 'U'},
94 {"verbose", no_argument
, NULL
, 'V'},
95 {"version", no_argument
, NULL
, 'v'},
96 {"help", no_argument
, NULL
, 'h'},
102 static struct itimerval itimer
;
104 static unsigned long frame_count_max
= 0, interval
= TX_KERNEL_PULL_INT
;
106 #define __pcap_io pcap_ops[ctx->pcap]
108 static void signal_handler(int number
)
119 static void timer_elapsed(int unused __maybe_unused
)
123 set_itimer_interval_value(&itimer
, 0, interval
);
125 ret
= pull_and_flush_tx_ring(tx_sock
);
126 if (unlikely(ret
< 0)) {
127 /* We could hit EBADF if the socket has been closed before
128 * the timer was triggered.
130 if (errno
!= EBADF
&& errno
!= ENOBUFS
)
131 panic("Flushing TX_RING failed: %s!\n", strerror(errno
));
134 setitimer(ITIMER_REAL
, &itimer
, NULL
);
137 static void timer_purge(void)
141 ret
= pull_and_flush_tx_ring_wait(tx_sock
);
142 if (unlikely(ret
< 0)) {
143 if (errno
!= EBADF
&& errno
!= ENOBUFS
)
144 panic("Flushing TX_RING failed: %s!\n", strerror(errno
));
147 set_itimer_interval_value(&itimer
, 0, 0);
148 setitimer(ITIMER_REAL
, &itimer
, NULL
);
151 static void timer_next_dump(int unused __maybe_unused
)
153 set_itimer_interval_value(&itimer
, interval
, 0);
155 setitimer(ITIMER_REAL
, &itimer
, NULL
);
158 static inline bool dump_to_pcap(struct ctx
*ctx
)
163 static void pcap_to_xmit(struct ctx
*ctx
)
167 int irq
, ifindex
, fd
= 0, ret
;
168 unsigned int size
, it
= 0;
169 unsigned long trunced
= 0;
171 struct frame_map
*hdr
;
172 struct sock_fprog bpf_ops
;
173 struct timeval start
, end
, diff
;
176 if (!device_up_and_running(ctx
->device_out
) && !ctx
->rfraw
)
177 panic("Device not up and running!\n");
181 tx_sock
= pf_socket();
183 if (!strncmp("-", ctx
->device_in
, strlen("-"))) {
184 fd
= dup(fileno(stdin
));
185 close(fileno(stdin
));
186 if (ctx
->pcap
== PCAP_OPS_MM
)
187 ctx
->pcap
= PCAP_OPS_SG
;
189 fd
= open_or_die(ctx
->device_in
, O_RDONLY
| O_LARGEFILE
| O_NOATIME
);
192 if (__pcap_io
->init_once_pcap
)
193 __pcap_io
->init_once_pcap();
195 ret
= __pcap_io
->pull_fhdr_pcap(fd
, &ctx
->magic
, &ctx
->link_type
);
197 panic("Error reading pcap header!\n");
199 if (__pcap_io
->prepare_access_pcap
) {
200 ret
= __pcap_io
->prepare_access_pcap(fd
, PCAP_MODE_RD
, ctx
->jumbo
);
202 panic("Error prepare reading pcap!\n");
205 fmemset(&tx_ring
, 0, sizeof(tx_ring
));
206 fmemset(&bpf_ops
, 0, sizeof(bpf_ops
));
209 ctx
->device_trans
= xstrdup(ctx
->device_out
);
210 xfree(ctx
->device_out
);
212 enter_rfmon_mac80211(ctx
->device_trans
, &ctx
->device_out
);
213 if (ctx
->link_type
!= LINKTYPE_IEEE802_11
)
214 panic("Wrong linktype of pcap!\n");
217 ifindex
= device_ifindex(ctx
->device_out
);
219 size
= ring_size(ctx
->device_out
, ctx
->reserve_size
);
221 bpf_parse_rules(ctx
->filter
, &bpf_ops
, ctx
->link_type
);
223 bpf_dump_all(&bpf_ops
);
225 set_packet_loss_discard(tx_sock
);
227 setup_tx_ring_layout(tx_sock
, &tx_ring
, size
, ctx
->jumbo
);
228 create_tx_ring(tx_sock
, &tx_ring
, ctx
->verbose
);
229 mmap_tx_ring(tx_sock
, &tx_ring
);
230 alloc_tx_ring_frames(tx_sock
, &tx_ring
);
231 bind_tx_ring(tx_sock
, &tx_ring
, ifindex
);
233 dissector_init_all(ctx
->print_mode
);
235 if (ctx
->cpu
>= 0 && ifindex
> 0) {
236 irq
= device_irq_number(ctx
->device_out
);
237 device_bind_irq_to_cpu(irq
, ctx
->cpu
);
240 printf("IRQ: %s:%d > CPU%d\n",
241 ctx
->device_out
, irq
, ctx
->cpu
);
245 interval
= ctx
->kpull
;
247 set_itimer_interval_value(&itimer
, 0, interval
);
248 setitimer(ITIMER_REAL
, &itimer
, NULL
);
250 drop_privileges(ctx
->enforce
, ctx
->uid
, ctx
->gid
);
252 printf("Running! Hang up with ^C!\n\n");
255 bug_on(gettimeofday(&start
, NULL
));
257 while (likely(sigint
== 0)) {
258 while (user_may_pull_from_tx(tx_ring
.frames
[it
].iov_base
)) {
259 hdr
= tx_ring
.frames
[it
].iov_base
;
260 out
= ((uint8_t *) hdr
) + TPACKET2_HDRLEN
- sizeof(struct sockaddr_ll
);
263 ret
= __pcap_io
->read_pcap(fd
, &phdr
, ctx
->magic
, out
,
264 ring_frame_size(&tx_ring
));
265 if (unlikely(ret
<= 0))
268 if (ring_frame_size(&tx_ring
) <
269 pcap_get_length(&phdr
, ctx
->magic
)) {
270 pcap_set_length(&phdr
, ctx
->magic
,
271 ring_frame_size(&tx_ring
));
274 } while (ctx
->filter
&&
275 !bpf_run_filter(&bpf_ops
, out
,
276 pcap_get_length(&phdr
, ctx
->magic
)));
278 pcap_pkthdr_to_tpacket_hdr(&phdr
, ctx
->magic
, &hdr
->tp_h
, &hdr
->s_ll
);
280 ctx
->tx_bytes
+= hdr
->tp_h
.tp_len
;;
283 show_frame_hdr(hdr
, ctx
->print_mode
);
285 dissector_entry_point(out
, hdr
->tp_h
.tp_snaplen
,
286 ctx
->link_type
, ctx
->print_mode
);
288 kernel_may_pull_from_tx(&hdr
->tp_h
);
291 if (it
>= tx_ring
.layout
.tp_frame_nr
)
294 if (unlikely(sigint
== 1))
297 if (frame_count_max
!= 0) {
298 if (ctx
->tx_packets
>= frame_count_max
) {
308 bug_on(gettimeofday(&end
, NULL
));
309 timersub(&end
, &start
, &diff
);
313 bpf_release(&bpf_ops
);
315 dissector_cleanup_all();
316 destroy_tx_ring(tx_sock
, &tx_ring
);
319 leave_rfmon_mac80211(ctx
->device_trans
, ctx
->device_out
);
321 if (__pcap_io
->prepare_close_pcap
)
322 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_RD
);
324 if (!strncmp("-", ctx
->device_in
, strlen("-")))
325 dup2(fd
, fileno(stdin
));
332 printf("\r%12lu packets outgoing\n", ctx
->tx_packets
);
333 printf("\r%12lu packets truncated in file\n", trunced
);
334 printf("\r%12lu bytes outgoing\n", ctx
->tx_bytes
);
335 printf("\r%12lu sec, %lu usec in total\n", diff
.tv_sec
, diff
.tv_usec
);
338 static void receive_to_xmit(struct ctx
*ctx
)
342 int rx_sock
, ifindex_in
, ifindex_out
;
343 unsigned int size_in
, size_out
, it_in
= 0, it_out
= 0;
344 unsigned long frame_count
= 0;
345 struct frame_map
*hdr_in
, *hdr_out
;
346 struct ring tx_ring
, rx_ring
;
347 struct pollfd rx_poll
;
348 struct sock_fprog bpf_ops
;
350 if (!strncmp(ctx
->device_in
, ctx
->device_out
, IFNAMSIZ
))
351 panic("Ingress/egress devices must be different!\n");
352 if (!device_up_and_running(ctx
->device_out
))
353 panic("Egress device not up and running!\n");
355 rx_sock
= pf_socket();
356 tx_sock
= pf_socket();
358 fmemset(&tx_ring
, 0, sizeof(tx_ring
));
359 fmemset(&rx_ring
, 0, sizeof(rx_ring
));
360 fmemset(&rx_poll
, 0, sizeof(rx_poll
));
361 fmemset(&bpf_ops
, 0, sizeof(bpf_ops
));
363 ifindex_in
= device_ifindex(ctx
->device_in
);
364 ifindex_out
= device_ifindex(ctx
->device_out
);
366 size_in
= ring_size(ctx
->device_in
, ctx
->reserve_size
);
367 size_out
= ring_size(ctx
->device_out
, ctx
->reserve_size
);
369 enable_kernel_bpf_jit_compiler();
371 bpf_parse_rules(ctx
->filter
, &bpf_ops
, ctx
->link_type
);
373 bpf_dump_all(&bpf_ops
);
374 bpf_attach_to_sock(rx_sock
, &bpf_ops
);
376 setup_rx_ring_layout(rx_sock
, &rx_ring
, size_in
, ctx
->jumbo
, false);
377 create_rx_ring(rx_sock
, &rx_ring
, ctx
->verbose
);
378 mmap_rx_ring(rx_sock
, &rx_ring
);
379 alloc_rx_ring_frames(rx_sock
, &rx_ring
);
380 bind_rx_ring(rx_sock
, &rx_ring
, ifindex_in
);
381 prepare_polling(rx_sock
, &rx_poll
);
383 set_packet_loss_discard(tx_sock
);
384 setup_tx_ring_layout(tx_sock
, &tx_ring
, size_out
, ctx
->jumbo
);
385 create_tx_ring(tx_sock
, &tx_ring
, ctx
->verbose
);
386 mmap_tx_ring(tx_sock
, &tx_ring
);
387 alloc_tx_ring_frames(tx_sock
, &tx_ring
);
388 bind_tx_ring(tx_sock
, &tx_ring
, ifindex_out
);
390 dissector_init_all(ctx
->print_mode
);
392 if (ctx
->promiscuous
)
393 ifflags
= enter_promiscuous_mode(ctx
->device_in
);
396 interval
= ctx
->kpull
;
398 set_itimer_interval_value(&itimer
, 0, interval
);
399 setitimer(ITIMER_REAL
, &itimer
, NULL
);
401 drop_privileges(ctx
->enforce
, ctx
->uid
, ctx
->gid
);
403 printf("Running! Hang up with ^C!\n\n");
406 while (likely(sigint
== 0)) {
407 while (user_may_pull_from_rx(rx_ring
.frames
[it_in
].iov_base
)) {
410 hdr_in
= rx_ring
.frames
[it_in
].iov_base
;
411 in
= ((uint8_t *) hdr_in
) + hdr_in
->tp_h
.tp_mac
;
415 if (ctx
->packet_type
!= -1)
416 if (ctx
->packet_type
!= hdr_in
->s_ll
.sll_pkttype
)
419 hdr_out
= tx_ring
.frames
[it_out
].iov_base
;
420 out
= ((uint8_t *) hdr_out
) + TPACKET2_HDRLEN
- sizeof(struct sockaddr_ll
);
422 for (; !user_may_pull_from_tx(tx_ring
.frames
[it_out
].iov_base
) &&
425 next_rnd_slot(&it_out
, &tx_ring
);
428 if (it_out
>= tx_ring
.layout
.tp_frame_nr
)
432 hdr_out
= tx_ring
.frames
[it_out
].iov_base
;
433 out
= ((uint8_t *) hdr_out
) + TPACKET2_HDRLEN
- sizeof(struct sockaddr_ll
);
436 tpacket_hdr_clone(&hdr_out
->tp_h
, &hdr_in
->tp_h
);
437 fmemcpy(out
, in
, hdr_in
->tp_h
.tp_len
);
439 kernel_may_pull_from_tx(&hdr_out
->tp_h
);
441 next_rnd_slot(&it_out
, &tx_ring
);
444 if (it_out
>= tx_ring
.layout
.tp_frame_nr
)
448 show_frame_hdr(hdr_in
, ctx
->print_mode
);
450 dissector_entry_point(in
, hdr_in
->tp_h
.tp_snaplen
,
451 ctx
->link_type
, ctx
->print_mode
);
453 if (frame_count_max
!= 0) {
454 if (frame_count
>= frame_count_max
) {
462 kernel_may_pull_from_rx(&hdr_in
->tp_h
);
465 if (it_in
>= rx_ring
.layout
.tp_frame_nr
)
468 if (unlikely(sigint
== 1))
472 poll(&rx_poll
, 1, -1);
479 sock_print_net_stats(rx_sock
);
481 bpf_release(&bpf_ops
);
483 dissector_cleanup_all();
485 destroy_tx_ring(tx_sock
, &tx_ring
);
486 destroy_rx_ring(rx_sock
, &rx_ring
);
488 if (ctx
->promiscuous
)
489 leave_promiscuous_mode(ctx
->device_in
, ifflags
);
495 static void translate_pcap_to_txf(int fdo
, uint8_t *out
, size_t len
)
497 size_t bytes_done
= 0;
500 slprintf(bout
, sizeof(bout
), "{\n ");
501 write_or_die(fdo
, bout
, strlen(bout
));
503 while (bytes_done
< len
) {
504 slprintf(bout
, sizeof(bout
), "0x%02x, ", out
[bytes_done
]);
505 write_or_die(fdo
, bout
, strlen(bout
));
509 if (bytes_done
% 10 == 0) {
510 slprintf(bout
, sizeof(bout
), "\n");
511 write_or_die(fdo
, bout
, strlen(bout
));
513 if (bytes_done
< len
) {
514 slprintf(bout
, sizeof(bout
), " ");
515 write_or_die(fdo
, bout
, strlen(bout
));
519 if (bytes_done
% 10 != 0) {
520 slprintf(bout
, sizeof(bout
), "\n");
521 write_or_die(fdo
, bout
, strlen(bout
));
524 slprintf(bout
, sizeof(bout
), "}\n\n");
525 write_or_die(fdo
, bout
, strlen(bout
));
528 static void read_pcap(struct ctx
*ctx
)
532 int ret
, fd
, fdo
= 0;
533 unsigned long trunced
= 0;
536 struct sock_fprog bpf_ops
;
538 struct timeval start
, end
, diff
;
539 struct sockaddr_ll sll
;
543 if (!strncmp("-", ctx
->device_in
, strlen("-"))) {
544 fd
= dup(fileno(stdin
));
545 close(fileno(stdin
));
546 if (ctx
->pcap
== PCAP_OPS_MM
)
547 ctx
->pcap
= PCAP_OPS_SG
;
549 fd
= open_or_die(ctx
->device_in
, O_RDONLY
| O_LARGEFILE
| O_NOATIME
);
552 if (__pcap_io
->init_once_pcap
)
553 __pcap_io
->init_once_pcap();
555 ret
= __pcap_io
->pull_fhdr_pcap(fd
, &ctx
->magic
, &ctx
->link_type
);
557 panic("Error reading pcap header!\n");
559 if (__pcap_io
->prepare_access_pcap
) {
560 ret
= __pcap_io
->prepare_access_pcap(fd
, PCAP_MODE_RD
, ctx
->jumbo
);
562 panic("Error prepare reading pcap!\n");
565 fmemset(&fm
, 0, sizeof(fm
));
566 fmemset(&bpf_ops
, 0, sizeof(bpf_ops
));
568 bpf_parse_rules(ctx
->filter
, &bpf_ops
, ctx
->link_type
);
570 bpf_dump_all(&bpf_ops
);
572 dissector_init_all(ctx
->print_mode
);
574 out_len
= round_up(1024 * 1024, PAGE_SIZE
);
575 out
= xmalloc_aligned(out_len
, CO_CACHE_LINE_SIZE
);
577 if (ctx
->device_out
) {
578 if (!strncmp("-", ctx
->device_out
, strlen("-"))) {
579 fdo
= dup(fileno(stdout
));
580 close(fileno(stdout
));
582 fdo
= open_or_die_m(ctx
->device_out
, O_RDWR
| O_CREAT
|
583 O_TRUNC
| O_LARGEFILE
, DEFFILEMODE
);
587 drop_privileges(ctx
->enforce
, ctx
->uid
, ctx
->gid
);
589 printf("Running! Hang up with ^C!\n\n");
592 bug_on(gettimeofday(&start
, NULL
));
594 while (likely(sigint
== 0)) {
596 ret
= __pcap_io
->read_pcap(fd
, &phdr
, ctx
->magic
,
598 if (unlikely(ret
< 0))
601 if (unlikely(pcap_get_length(&phdr
, ctx
->magic
) == 0)) {
606 if (unlikely(pcap_get_length(&phdr
, ctx
->magic
) > out_len
)) {
607 pcap_set_length(&phdr
, ctx
->magic
, out_len
);
610 } while (ctx
->filter
&&
611 !bpf_run_filter(&bpf_ops
, out
,
612 pcap_get_length(&phdr
, ctx
->magic
)));
614 pcap_pkthdr_to_tpacket_hdr(&phdr
, ctx
->magic
, &fm
.tp_h
, &sll
);
616 ctx
->tx_bytes
+= fm
.tp_h
.tp_len
;
619 show_frame_hdr(&fm
, ctx
->print_mode
);
621 dissector_entry_point(out
, fm
.tp_h
.tp_snaplen
,
622 ctx
->link_type
, ctx
->print_mode
);
625 translate_pcap_to_txf(fdo
, out
, fm
.tp_h
.tp_snaplen
);
627 if (frame_count_max
!= 0) {
628 if (ctx
->tx_packets
>= frame_count_max
) {
637 bug_on(gettimeofday(&end
, NULL
));
638 timersub(&end
, &start
, &diff
);
640 bpf_release(&bpf_ops
);
642 dissector_cleanup_all();
644 if (__pcap_io
->prepare_close_pcap
)
645 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_RD
);
651 printf("\r%12lu packets outgoing\n", ctx
->tx_packets
);
652 printf("\r%12lu packets truncated in file\n", trunced
);
653 printf("\r%12lu bytes outgoing\n", ctx
->tx_bytes
);
654 printf("\r%12lu sec, %lu usec in total\n", diff
.tv_sec
, diff
.tv_usec
);
656 if (!strncmp("-", ctx
->device_in
, strlen("-")))
657 dup2(fd
, fileno(stdin
));
660 if (ctx
->device_out
) {
661 if (!strncmp("-", ctx
->device_out
, strlen("-")))
662 dup2(fdo
, fileno(stdout
));
667 static void finish_multi_pcap_file(struct ctx
*ctx
, int fd
)
669 __pcap_io
->fsync_pcap(fd
);
671 if (__pcap_io
->prepare_close_pcap
)
672 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_WR
);
676 fmemset(&itimer
, 0, sizeof(itimer
));
677 setitimer(ITIMER_REAL
, &itimer
, NULL
);
680 static int next_multi_pcap_file(struct ctx
*ctx
, int fd
)
685 __pcap_io
->fsync_pcap(fd
);
687 if (__pcap_io
->prepare_close_pcap
)
688 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_WR
);
692 slprintf(fname
, sizeof(fname
), "%s/%s%lu.pcap", ctx
->device_out
,
693 ctx
->prefix
? : "dump-", time(0));
695 fd
= open_or_die_m(fname
, O_RDWR
| O_CREAT
| O_TRUNC
|
696 O_LARGEFILE
, DEFFILEMODE
);
698 ret
= __pcap_io
->push_fhdr_pcap(fd
, ctx
->magic
, ctx
->link_type
);
700 panic("Error writing pcap header!\n");
702 if (__pcap_io
->prepare_access_pcap
) {
703 ret
= __pcap_io
->prepare_access_pcap(fd
, PCAP_MODE_WR
, true);
705 panic("Error prepare writing pcap!\n");
711 static int begin_multi_pcap_file(struct ctx
*ctx
)
718 if (ctx
->device_out
[strlen(ctx
->device_out
) - 1] == '/')
719 ctx
->device_out
[strlen(ctx
->device_out
) - 1] = 0;
721 slprintf(fname
, sizeof(fname
), "%s/%s%lu.pcap", ctx
->device_out
,
722 ctx
->prefix
? : "dump-", time(0));
724 fd
= open_or_die_m(fname
, O_RDWR
| O_CREAT
| O_TRUNC
|
725 O_LARGEFILE
, DEFFILEMODE
);
727 ret
= __pcap_io
->push_fhdr_pcap(fd
, ctx
->magic
, ctx
->link_type
);
729 panic("Error writing pcap header!\n");
731 if (__pcap_io
->prepare_access_pcap
) {
732 ret
= __pcap_io
->prepare_access_pcap(fd
, PCAP_MODE_WR
, true);
734 panic("Error prepare writing pcap!\n");
737 if (ctx
->dump_mode
== DUMP_INTERVAL_TIME
) {
738 interval
= ctx
->dump_interval
;
740 set_itimer_interval_value(&itimer
, interval
, 0);
741 setitimer(ITIMER_REAL
, &itimer
, NULL
);
749 static void finish_single_pcap_file(struct ctx
*ctx
, int fd
)
751 __pcap_io
->fsync_pcap(fd
);
753 if (__pcap_io
->prepare_close_pcap
)
754 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_WR
);
756 if (strncmp("-", ctx
->device_out
, strlen("-")))
759 dup2(fd
, fileno(stdout
));
762 static int begin_single_pcap_file(struct ctx
*ctx
)
768 if (!strncmp("-", ctx
->device_out
, strlen("-"))) {
769 fd
= dup(fileno(stdout
));
770 close(fileno(stdout
));
771 if (ctx
->pcap
== PCAP_OPS_MM
)
772 ctx
->pcap
= PCAP_OPS_SG
;
774 fd
= open_or_die_m(ctx
->device_out
,
775 O_RDWR
| O_CREAT
| O_TRUNC
|
776 O_LARGEFILE
, DEFFILEMODE
);
779 ret
= __pcap_io
->push_fhdr_pcap(fd
, ctx
->magic
, ctx
->link_type
);
781 panic("Error writing pcap header!\n");
783 if (__pcap_io
->prepare_access_pcap
) {
784 ret
= __pcap_io
->prepare_access_pcap(fd
, PCAP_MODE_WR
, true);
786 panic("Error prepare writing pcap!\n");
792 static void print_pcap_file_stats(int sock
, struct ctx
*ctx
)
795 struct tpacket_stats kstats
;
796 socklen_t slen
= sizeof(kstats
);
798 fmemset(&kstats
, 0, sizeof(kstats
));
800 ret
= getsockopt(sock
, SOL_PACKET
, PACKET_STATISTICS
, &kstats
, &slen
);
802 panic("Cannot get packet statistics!\n");
804 if (ctx
->print_mode
== PRINT_NONE
) {
805 printf(".(+%u/-%u)", kstats
.tp_packets
- kstats
.tp_drops
,
811 static void walk_t3_block(struct block_desc
*pbd
, struct ctx
*ctx
,
815 int num_pkts
= pbd
->h1
.num_pkts
, i
, ret
;
816 unsigned long frame_count
= 0;
817 struct tpacket3_hdr
*hdr
;
819 struct sockaddr_ll
*sll
;
821 hdr
= (void *) ((uint8_t *) pbd
+ pbd
->h1
.offset_to_first_pkt
);
822 sll
= (void *) ((uint8_t *) hdr
+ TPACKET_ALIGN(sizeof(*hdr
)));
824 for (i
= 0; i
< num_pkts
&& likely(sigint
== 0); ++i
) {
826 packet
= ((uint8_t *) hdr
+ hdr
->tp_mac
);
829 if (ctx
->packet_type
!= -1)
830 if (ctx
->packet_type
!= sll
->sll_pkttype
)
833 if (dump_to_pcap(ctx
)) {
834 tpacket3_hdr_to_pcap_pkthdr(hdr
, sll
, &phdr
, ctx
->magic
);
836 ret
= __pcap_io
->write_pcap(fd
, &phdr
, ctx
->magic
, packet
,
837 pcap_get_length(&phdr
, ctx
->magic
));
838 if (unlikely(ret
!= pcap_get_total_length(&phdr
, ctx
->magic
)))
839 panic("Write error to pcap!\n");
842 __show_frame_hdr(sll
, hdr
, ctx
->print_mode
, true);
844 dissector_entry_point(packet
, hdr
->tp_snaplen
, ctx
->link_type
,
848 hdr
= (void *) ((uint8_t *) hdr
+ hdr
->tp_next_offset
);
849 sll
= (void *) ((uint8_t *) hdr
+ TPACKET_ALIGN(sizeof(*hdr
)));
851 if (frame_count_max
!= 0) {
852 if (frame_count
>= frame_count_max
) {
858 if (dump_to_pcap(ctx
)) {
859 if (ctx
->dump_mode
== DUMP_INTERVAL_SIZE
) {
860 interval
+= hdr
->tp_snaplen
;
861 if (interval
> ctx
->dump_interval
) {
868 fd
= next_multi_pcap_file(ctx
, fd
);
871 if (unlikely(ctx
->verbose
))
872 print_pcap_file_stats(sock
, ctx
);
878 static void recv_only_or_dump(struct ctx
*ctx
)
881 int sock
, irq
, ifindex
, fd
= 0, ret
;
882 unsigned int size
, it
= 0;
884 struct pollfd rx_poll
;
885 struct sock_fprog bpf_ops
;
886 struct timeval start
, end
, diff
;
887 struct block_desc
*pbd
;
892 ctx
->device_trans
= xstrdup(ctx
->device_in
);
893 xfree(ctx
->device_in
);
895 enter_rfmon_mac80211(ctx
->device_trans
, &ctx
->device_in
);
896 ctx
->link_type
= LINKTYPE_IEEE802_11
;
899 fmemset(&rx_ring
, 0, sizeof(rx_ring
));
900 fmemset(&rx_poll
, 0, sizeof(rx_poll
));
901 fmemset(&bpf_ops
, 0, sizeof(bpf_ops
));
903 ifindex
= device_ifindex(ctx
->device_in
);
905 size
= ring_size(ctx
->device_in
, ctx
->reserve_size
);
907 enable_kernel_bpf_jit_compiler();
909 bpf_parse_rules(ctx
->filter
, &bpf_ops
, ctx
->link_type
);
911 bpf_dump_all(&bpf_ops
);
912 bpf_attach_to_sock(sock
, &bpf_ops
);
914 set_sockopt_hwtimestamp(sock
, ctx
->device_in
);
916 setup_rx_ring_layout(sock
, &rx_ring
, size
, true, true);
917 create_rx_ring(sock
, &rx_ring
, ctx
->verbose
);
918 mmap_rx_ring(sock
, &rx_ring
);
919 alloc_rx_ring_frames(sock
, &rx_ring
);
920 bind_rx_ring(sock
, &rx_ring
, ifindex
);
922 prepare_polling(sock
, &rx_poll
);
923 dissector_init_all(ctx
->print_mode
);
925 if (ctx
->cpu
>= 0 && ifindex
> 0) {
926 irq
= device_irq_number(ctx
->device_in
);
927 device_bind_irq_to_cpu(irq
, ctx
->cpu
);
930 printf("IRQ: %s:%d > CPU%d\n",
931 ctx
->device_in
, irq
, ctx
->cpu
);
934 if (ctx
->promiscuous
)
935 ifflags
= enter_promiscuous_mode(ctx
->device_in
);
937 if (dump_to_pcap(ctx
) && __pcap_io
->init_once_pcap
)
938 __pcap_io
->init_once_pcap();
940 drop_privileges(ctx
->enforce
, ctx
->uid
, ctx
->gid
);
942 if (dump_to_pcap(ctx
)) {
946 fmemset(&stats
, 0, sizeof(stats
));
947 ret
= stat(ctx
->device_out
, &stats
);
953 ctx
->dump_dir
= S_ISDIR(stats
.st_mode
);
955 fd
= begin_multi_pcap_file(ctx
);
958 fd
= begin_single_pcap_file(ctx
);
962 printf("Running! Hang up with ^C!\n\n");
965 bug_on(gettimeofday(&start
, NULL
));
967 while (likely(sigint
== 0)) {
968 while (user_may_pull_from_rx_block((pbd
= (void *)
969 rx_ring
.frames
[it
].iov_base
))) {
970 walk_t3_block(pbd
, ctx
, sock
, fd
);
972 kernel_may_pull_from_rx_block(pbd
);
973 it
= (it
+ 1) % rx_ring
.layout3
.tp_block_nr
;
975 if (unlikely(sigint
== 1))
979 poll(&rx_poll
, 1, -1);
982 bug_on(gettimeofday(&end
, NULL
));
983 timersub(&end
, &start
, &diff
);
985 if (!(ctx
->dump_dir
&& ctx
->print_mode
== PRINT_NONE
)) {
986 sock_print_net_stats(sock
);
988 printf("\r%12lu sec, %lu usec in total\n",
989 diff
.tv_sec
, diff
.tv_usec
);
995 bpf_release(&bpf_ops
);
996 dissector_cleanup_all();
997 destroy_rx_ring(sock
, &rx_ring
);
999 if (ctx
->promiscuous
)
1000 leave_promiscuous_mode(ctx
->device_in
, ifflags
);
1003 leave_rfmon_mac80211(ctx
->device_trans
, ctx
->device_in
);
1005 if (dump_to_pcap(ctx
)) {
1007 finish_multi_pcap_file(ctx
, fd
);
1009 finish_single_pcap_file(ctx
, fd
);
1015 static void __noreturn
help(void)
1017 printf("\nnetsniff-ng %s, the packet sniffing beast\n", VERSION_STRING
);
1018 puts("http://www.netsniff-ng.org\n\n"
1019 "Usage: netsniff-ng [options] [filter-expression]\n"
1021 " -i|-d|--dev|--in <dev|pcap|-> Input source as netdev, pcap or pcap stdin\n"
1022 " -o|--out <dev|pcap|dir|cfg|-> Output sink as netdev, pcap, directory, trafgen, or stdout\n"
1023 " -f|--filter <bpf-file|expr> Use BPF filter file from bpfc or tcpdump-like expression\n"
1024 " -t|--type <type> Filter for: host|broadcast|multicast|others|outgoing\n"
1025 " -F|--interval <size|time> Dump interval if -o is a dir: <num>KiB/MiB/GiB/s/sec/min/hrs\n"
1026 " -R|--rfraw Capture or inject raw 802.11 frames\n"
1027 " -n|--num <0|uint> Number of packets until exit (def: 0)\n"
1028 " -P|--prefix <name> Prefix for pcaps stored in directory\n"
1029 " -T|--magic <pcap-magic> Pcap magic number/pcap format to store, see -D\n"
1030 " -D|--dump-pcap-types Dump pcap types and magic numbers and quit\n"
1031 " -B|--dump-bpf Dump generated BPF assembly\n"
1032 " -r|--rand Randomize packet forwarding order (dev->dev)\n"
1033 " -M|--no-promisc No promiscuous mode for netdev\n"
1034 " -A|--no-sock-mem Don't tune core socket memory\n"
1035 " -m|--mmap Mmap(2) pcap file i.e., for replaying pcaps\n"
1036 " -G|--sg Scatter/gather pcap file I/O\n"
1037 " -c|--clrw Use slower read(2)/write(2) I/O\n"
1038 " -S|--ring-size <size> Specify ring size to: <num>KiB/MiB/GiB\n"
1039 " -k|--kernel-pull <uint> Kernel pull from user interval in us (def: 10us)\n"
1040 " -J|--jumbo-support Support replay/fwd 64KB Super Jumbo Frames (def: 2048B)\n"
1041 " -b|--bind-cpu <cpu> Bind to specific CPU\n"
1042 " -u|--user <userid> Drop privileges and change to userid\n"
1043 " -g|--group <groupid> Drop privileges and change to groupid\n"
1044 " -H|--prio-high Make this high priority process\n"
1045 " -Q|--notouch-irq Do not touch IRQ CPU affinity of NIC\n"
1046 " -s|--silent Do not print captured packets\n"
1047 " -q|--less Print less-verbose packet information\n"
1048 " -X|--hex Print packet data in hex format\n"
1049 " -l|--ascii Print human-readable packet data\n"
1050 " -U|--update Update GeoIP databases\n"
1051 " -V|--verbose Be more verbose\n"
1052 " -v|--version Show version and exit\n"
1053 " -h|--help Guess what?!\n\n"
1055 " netsniff-ng --in eth0 --out dump.pcap -s -T 0xa1b2c3d4 --b 0 tcp or udp\n"
1056 " netsniff-ng --in wlan0 --rfraw --out dump.pcap --silent --bind-cpu 0\n"
1057 " netsniff-ng --in dump.pcap --mmap --out eth0 -k1000 --silent --bind-cpu 0\n"
1058 " netsniff-ng --in dump.pcap --out dump.cfg --silent --bind-cpu 0\n"
1059 " netsniff-ng --in eth0 --out eth1 --silent --bind-cpu 0 -J --type host\n"
1060 " netsniff-ng --in eth1 --out /opt/probe/ -s -m --interval 100MiB -b 0\n"
1061 " netsniff-ng --in vlan0 --out dump.pcap -c -u `id -u bob` -g `id -g bob`\n"
1062 " netsniff-ng --in any --filter http.bpf --jumbo-support --ascii -V\n\n"
1064 " For introducing bit errors, delays with random variation and more\n"
1065 " while replaying pcaps, make use of tc(8) with its disciplines (e.g. netem).\n\n"
1066 "Please report bugs to <bugs@netsniff-ng.org>\n"
1067 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
1068 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel.roullit@gmail.com>\n"
1069 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1070 "Swiss federal institute of technology (ETH Zurich)\n"
1071 "License: GNU GPL version 2.0\n"
1072 "This is free software: you are free to change and redistribute it.\n"
1073 "There is NO WARRANTY, to the extent permitted by law.\n");
1077 static void __noreturn
version(void)
1079 printf("\nnetsniff-ng %s, the packet sniffing beast\n", VERSION_LONG
);
1080 puts("http://www.netsniff-ng.org\n\n"
1081 "Please report bugs to <bugs@netsniff-ng.org>\n"
1082 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
1083 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel.roullit@gmail.com>\n"
1084 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1085 "Swiss federal institute of technology (ETH Zurich)\n"
1086 "License: GNU GPL version 2.0\n"
1087 "This is free software: you are free to change and redistribute it.\n"
1088 "There is NO WARRANTY, to the extent permitted by law.\n");
1092 int main(int argc
, char **argv
)
1095 int c
, i
, j
, cpu_tmp
, opt_index
, ops_touched
= 0, vals
[4] = {0};
1096 bool prio_high
= false, setsockmem
= true;
1097 void (*main_loop
)(struct ctx
*ctx
) = NULL
;
1099 .link_type
= LINKTYPE_EN10MB
,
1100 .print_mode
= PRINT_NORM
,
1103 .promiscuous
= true,
1105 .pcap
= PCAP_OPS_SG
,
1106 .dump_interval
= 60,
1107 .dump_mode
= DUMP_INTERVAL_TIME
,
1110 .magic
= ORIGINAL_TCPDUMP_MAGIC
,
1115 while ((c
= getopt_long(argc
, argv
, short_options
, long_options
,
1116 &opt_index
)) != EOF
) {
1120 ctx
.device_in
= xstrdup(optarg
);
1123 ctx
.device_out
= xstrdup(optarg
);
1126 ctx
.prefix
= xstrdup(optarg
);
1129 ctx
.link_type
= LINKTYPE_IEEE802_11
;
1133 ctx
.randomize
= true;
1139 ctx
.magic
= (uint32_t) strtoul(optarg
, NULL
, 0);
1140 pcap_check_magic(ctx
.magic
);
1143 ctx
.filter
= xstrdup(optarg
);
1146 ctx
.promiscuous
= false;
1152 ctx
.uid
= strtoul(optarg
, NULL
, 0);
1156 ctx
.gid
= strtoul(optarg
, NULL
, 0);
1160 if (!strncmp(optarg
, "host", strlen("host")))
1161 ctx
.packet_type
= PACKET_HOST
;
1162 else if (!strncmp(optarg
, "broadcast", strlen("broadcast")))
1163 ctx
.packet_type
= PACKET_BROADCAST
;
1164 else if (!strncmp(optarg
, "multicast", strlen("multicast")))
1165 ctx
.packet_type
= PACKET_MULTICAST
;
1166 else if (!strncmp(optarg
, "others", strlen("others")))
1167 ctx
.packet_type
= PACKET_OTHERHOST
;
1168 else if (!strncmp(optarg
, "outgoing", strlen("outgoing")))
1169 ctx
.packet_type
= PACKET_OUTGOING
;
1171 ctx
.packet_type
= -1;
1175 ctx
.reserve_size
= 0;
1177 for (j
= i
= strlen(optarg
); i
> 0; --i
) {
1178 if (!isdigit(optarg
[j
- i
]))
1183 if (!strncmp(ptr
, "KiB", strlen("KiB")))
1184 ctx
.reserve_size
= 1 << 10;
1185 else if (!strncmp(ptr
, "MiB", strlen("MiB")))
1186 ctx
.reserve_size
= 1 << 20;
1187 else if (!strncmp(ptr
, "GiB", strlen("GiB")))
1188 ctx
.reserve_size
= 1 << 30;
1190 panic("Syntax error in ring size param!\n");
1193 ctx
.reserve_size
*= strtol(optarg
, NULL
, 0);
1196 cpu_tmp
= strtol(optarg
, NULL
, 0);
1198 cpu_affinity(cpu_tmp
);
1206 ctx
.pcap
= PCAP_OPS_RW
;
1210 ctx
.pcap
= PCAP_OPS_MM
;
1214 ctx
.pcap
= PCAP_OPS_SG
;
1221 ctx
.print_mode
= PRINT_NONE
;
1224 ctx
.print_mode
= PRINT_LESS
;
1228 (ctx
.print_mode
== PRINT_ASCII
) ?
1229 PRINT_HEX_ASCII
: PRINT_HEX
;
1233 (ctx
.print_mode
== PRINT_HEX
) ?
1234 PRINT_HEX_ASCII
: PRINT_ASCII
;
1237 ctx
.kpull
= strtol(optarg
, NULL
, 0);
1240 frame_count_max
= strtol(optarg
, NULL
, 0);
1244 ctx
.dump_interval
= 0;
1246 for (j
= i
= strlen(optarg
); i
> 0; --i
) {
1247 if (!isdigit(optarg
[j
- i
]))
1252 if (!strncmp(ptr
, "KiB", strlen("KiB"))) {
1253 ctx
.dump_interval
= 1 << 10;
1254 ctx
.dump_mode
= DUMP_INTERVAL_SIZE
;
1255 } else if (!strncmp(ptr
, "MiB", strlen("MiB"))) {
1256 ctx
.dump_interval
= 1 << 20;
1257 ctx
.dump_mode
= DUMP_INTERVAL_SIZE
;
1258 } else if (!strncmp(ptr
, "GiB", strlen("GiB"))) {
1259 ctx
.dump_interval
= 1 << 30;
1260 ctx
.dump_mode
= DUMP_INTERVAL_SIZE
;
1261 } else if (!strncmp(ptr
, "sec", strlen("sec"))) {
1262 ctx
.dump_interval
= 1;
1263 ctx
.dump_mode
= DUMP_INTERVAL_TIME
;
1264 } else if (!strncmp(ptr
, "min", strlen("min"))) {
1265 ctx
.dump_interval
= 60;
1266 ctx
.dump_mode
= DUMP_INTERVAL_TIME
;
1267 } else if (!strncmp(ptr
, "hrs", strlen("hrs"))) {
1268 ctx
.dump_interval
= 60 * 60;
1269 ctx
.dump_mode
= DUMP_INTERVAL_TIME
;
1270 } else if (!strncmp(ptr
, "s", strlen("s"))) {
1271 ctx
.dump_interval
= 1;
1272 ctx
.dump_mode
= DUMP_INTERVAL_TIME
;
1274 panic("Syntax error in time/size param!\n");
1278 ctx
.dump_interval
*= strtol(optarg
, NULL
, 0);
1284 ctx
.dump_bpf
= true;
1287 pcap_dump_type_features();
1317 panic("Option -%c requires an argument!\n",
1320 if (isprint(optopt
))
1321 printf("Unknown option character `0x%X\'!\n", optopt
);
1329 if (!ctx
.filter
&& optind
!= argc
) {
1333 for (i
= optind
; i
< argc
; ++i
) {
1334 size_t alen
= strlen(argv
[i
]) + 2;
1335 size_t flen
= ctx
.filter
? strlen(ctx
.filter
) : 0;
1337 ctx
.filter
= xrealloc(ctx
.filter
, 1, flen
+ alen
);
1338 ret
= slprintf(ctx
.filter
+ offset
, strlen(argv
[i
]) + 2, "%s ", argv
[i
]);
1340 panic("Cannot concatenate filter string!\n");
1347 ctx
.device_in
= xstrdup("any");
1349 register_signal(SIGINT
, signal_handler
);
1350 register_signal(SIGHUP
, signal_handler
);
1355 set_proc_prio(get_default_proc_prio());
1356 set_sched_status(get_default_sched_policy(), get_default_sched_prio());
1359 if (ctx
.device_in
&& (device_mtu(ctx
.device_in
) ||
1360 !strncmp("any", ctx
.device_in
, strlen(ctx
.device_in
)))) {
1361 if (!ctx
.device_out
) {
1363 main_loop
= recv_only_or_dump
;
1364 } else if (device_mtu(ctx
.device_out
)) {
1365 register_signal_f(SIGALRM
, timer_elapsed
, SA_SIGINFO
);
1366 main_loop
= receive_to_xmit
;
1369 register_signal_f(SIGALRM
, timer_next_dump
, SA_SIGINFO
);
1370 main_loop
= recv_only_or_dump
;
1372 ctx
.pcap
= PCAP_OPS_SG
;
1375 if (ctx
.device_out
&& device_mtu(ctx
.device_out
)) {
1376 register_signal_f(SIGALRM
, timer_elapsed
, SA_SIGINFO
);
1377 main_loop
= pcap_to_xmit
;
1379 ctx
.pcap
= PCAP_OPS_MM
;
1381 main_loop
= read_pcap
;
1383 ctx
.pcap
= PCAP_OPS_SG
;
1391 set_system_socket_memory(vals
, array_size(vals
));
1400 reset_system_socket_memory(vals
, array_size(vals
));
1402 device_restore_irq_affinity_list();
1405 free(ctx
.device_in
);
1406 free(ctx
.device_out
);
1407 free(ctx
.device_trans
);