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>
37 #include "dissector.h"
40 extern int set_sockopt_hwtimestamp(int sock
, const char *dev
);
48 char *device_in
, *device_out
, *device_trans
, *filter
, *prefix
;
49 int cpu
, rfraw
, dump
, print_mode
, dump_dir
, packet_type
, verbose
;
50 unsigned long kpull
, dump_interval
, reserve_size
, tx_bytes
, tx_packets
;
51 bool randomize
, promiscuous
, enforce
, jumbo
, dump_bpf
;
52 enum pcap_ops_groups pcap
; enum dump_mode dump_mode
;
53 uid_t uid
; gid_t gid
; uint32_t link_type
, magic
;
56 volatile sig_atomic_t sigint
= 0;
58 static volatile bool next_dump
= false;
60 static const char *short_options
= "d:i:o:rf:MJt:S:k:n:b:HQmcsqXlvhF:RGAP:Vu:g:T:DB";
61 static const struct option long_options
[] = {
62 {"dev", required_argument
, NULL
, 'd'},
63 {"in", required_argument
, NULL
, 'i'},
64 {"out", required_argument
, NULL
, 'o'},
65 {"filter", required_argument
, NULL
, 'f'},
66 {"num", required_argument
, NULL
, 'n'},
67 {"type", required_argument
, NULL
, 't'},
68 {"interval", required_argument
, NULL
, 'F'},
69 {"ring-size", required_argument
, NULL
, 'S'},
70 {"kernel-pull", required_argument
, NULL
, 'k'},
71 {"bind-cpu", required_argument
, NULL
, 'b'},
72 {"prefix", required_argument
, NULL
, 'P'},
73 {"user", required_argument
, NULL
, 'u'},
74 {"group", required_argument
, NULL
, 'g'},
75 {"magic", required_argument
, NULL
, 'T'},
76 {"rand", no_argument
, NULL
, 'r'},
77 {"rfraw", no_argument
, NULL
, 'R'},
78 {"mmap", no_argument
, NULL
, 'm'},
79 {"sg", no_argument
, NULL
, 'G'},
80 {"clrw", no_argument
, NULL
, 'c'},
81 {"jumbo-support", no_argument
, NULL
, 'J'},
82 {"no-promisc", no_argument
, NULL
, 'M'},
83 {"prio-high", no_argument
, NULL
, 'H'},
84 {"notouch-irq", no_argument
, NULL
, 'Q'},
85 {"dump-pcap-types", no_argument
, NULL
, 'D'},
86 {"dump-bpf", no_argument
, NULL
, 'B'},
87 {"silent", no_argument
, NULL
, 's'},
88 {"less", no_argument
, NULL
, 'q'},
89 {"hex", no_argument
, NULL
, 'X'},
90 {"ascii", no_argument
, NULL
, 'l'},
91 {"no-sock-mem", no_argument
, NULL
, 'A'},
92 {"update", no_argument
, NULL
, 'U'},
93 {"verbose", no_argument
, NULL
, 'V'},
94 {"version", no_argument
, NULL
, 'v'},
95 {"help", no_argument
, NULL
, 'h'},
101 static struct itimerval itimer
;
103 static unsigned long frame_count_max
= 0, interval
= TX_KERNEL_PULL_INT
;
105 #define __pcap_io pcap_ops[ctx->pcap]
107 static void signal_handler(int number
)
118 static void timer_elapsed(int unused
)
122 set_itimer_interval_value(&itimer
, 0, interval
);
124 ret
= pull_and_flush_tx_ring(tx_sock
);
125 if (unlikely(ret
< 0)) {
126 /* We could hit EBADF if the socket has been closed before
127 * the timer was triggered.
129 if (errno
!= EBADF
&& errno
!= ENOBUFS
)
130 panic("Flushing TX_RING failed: %s!\n", strerror(errno
));
133 setitimer(ITIMER_REAL
, &itimer
, NULL
);
136 static void timer_purge(void)
140 ret
= pull_and_flush_tx_ring_wait(tx_sock
);
141 if (unlikely(ret
< 0)) {
142 if (errno
!= EBADF
&& errno
!= ENOBUFS
)
143 panic("Flushing TX_RING failed: %s!\n", strerror(errno
));
146 set_itimer_interval_value(&itimer
, 0, 0);
147 setitimer(ITIMER_REAL
, &itimer
, NULL
);
151 static void timer_next_dump(int 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_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
);
377 create_rx_ring(rx_sock
, &rx_ring
, ctx
->verbose
);
378 mmap_rx_ring(rx_sock
, &rx_ring
);
379 alloc_rx_ring_frames(&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_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
, 0);
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
, ctx
->jumbo
);
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
, ctx
->jumbo
);
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
, ctx
->jumbo
);
786 panic("Error prepare writing pcap!\n");
792 static void print_pcap_file_stats(int sock
, struct ctx
*ctx
, unsigned long skipped
)
795 unsigned long good
, bad
;
796 struct tpacket_stats kstats
;
797 socklen_t slen
= sizeof(kstats
);
799 fmemset(&kstats
, 0, sizeof(kstats
));
801 ret
= getsockopt(sock
, SOL_PACKET
, PACKET_STATISTICS
, &kstats
, &slen
);
803 panic("Cannot get packet statistics!\n");
805 if (ctx
->print_mode
== PRINT_NONE
) {
806 good
= kstats
.tp_packets
- kstats
.tp_drops
- skipped
;
807 bad
= kstats
.tp_drops
+ skipped
;
809 printf(".(+%lu/-%lu)", good
, bad
);
814 static void recv_only_or_dump(struct ctx
*ctx
)
818 int sock
, irq
, ifindex
, fd
= 0, ret
;
819 unsigned int size
, it
= 0;
820 unsigned long frame_count
= 0, skipped
= 0;
822 struct pollfd rx_poll
;
823 struct frame_map
*hdr
;
824 struct sock_fprog bpf_ops
;
825 struct timeval start
, end
, diff
;
831 ctx
->device_trans
= xstrdup(ctx
->device_in
);
832 xfree(ctx
->device_in
);
834 enter_rfmon_mac80211(ctx
->device_trans
, &ctx
->device_in
);
835 ctx
->link_type
= LINKTYPE_IEEE802_11
;
838 fmemset(&rx_ring
, 0, sizeof(rx_ring
));
839 fmemset(&rx_poll
, 0, sizeof(rx_poll
));
840 fmemset(&bpf_ops
, 0, sizeof(bpf_ops
));
842 ifindex
= device_ifindex(ctx
->device_in
);
844 size
= ring_size(ctx
->device_in
, ctx
->reserve_size
);
846 enable_kernel_bpf_jit_compiler();
848 bpf_parse_rules(ctx
->filter
, &bpf_ops
, ctx
->link_type
);
850 bpf_dump_all(&bpf_ops
);
851 bpf_attach_to_sock(sock
, &bpf_ops
);
853 set_sockopt_hwtimestamp(sock
, ctx
->device_in
);
855 setup_rx_ring_layout(sock
, &rx_ring
, size
, ctx
->jumbo
);
856 create_rx_ring(sock
, &rx_ring
, ctx
->verbose
);
857 mmap_rx_ring(sock
, &rx_ring
);
858 alloc_rx_ring_frames(&rx_ring
);
859 bind_rx_ring(sock
, &rx_ring
, ifindex
);
861 prepare_polling(sock
, &rx_poll
);
862 dissector_init_all(ctx
->print_mode
);
864 if (ctx
->cpu
>= 0 && ifindex
> 0) {
865 irq
= device_irq_number(ctx
->device_in
);
866 device_bind_irq_to_cpu(irq
, ctx
->cpu
);
869 printf("IRQ: %s:%d > CPU%d\n",
870 ctx
->device_in
, irq
, ctx
->cpu
);
873 if (ctx
->promiscuous
)
874 ifflags
= enter_promiscuous_mode(ctx
->device_in
);
876 if (dump_to_pcap(ctx
) && __pcap_io
->init_once_pcap
)
877 __pcap_io
->init_once_pcap();
879 drop_privileges(ctx
->enforce
, ctx
->uid
, ctx
->gid
);
881 if (dump_to_pcap(ctx
)) {
885 fmemset(&stats
, 0, sizeof(stats
));
886 ret
= stat(ctx
->device_out
, &stats
);
892 ctx
->dump_dir
= S_ISDIR(stats
.st_mode
);
894 fd
= begin_multi_pcap_file(ctx
);
897 fd
= begin_single_pcap_file(ctx
);
901 printf("Running! Hang up with ^C!\n\n");
904 bug_on(gettimeofday(&start
, NULL
));
906 while (likely(sigint
== 0)) {
907 while (user_may_pull_from_rx(rx_ring
.frames
[it
].iov_base
)) {
910 hdr
= rx_ring
.frames
[it
].iov_base
;
911 packet
= ((uint8_t *) hdr
) + hdr
->tp_h
.tp_mac
;
914 if (ctx
->packet_type
!= -1)
915 if (ctx
->packet_type
!= hdr
->s_ll
.sll_pkttype
)
918 if (unlikely(ring_frame_size(&rx_ring
) < hdr
->tp_h
.tp_snaplen
)) {
923 if (dump_to_pcap(ctx
)) {
924 tpacket_hdr_to_pcap_pkthdr(&hdr
->tp_h
, &hdr
->s_ll
, &phdr
, ctx
->magic
);
926 ret
= __pcap_io
->write_pcap(fd
, &phdr
, ctx
->magic
, packet
,
927 pcap_get_length(&phdr
, ctx
->magic
));
928 if (unlikely(ret
!= pcap_get_total_length(&phdr
, ctx
->magic
)))
929 panic("Write error to pcap!\n");
932 show_frame_hdr(hdr
, ctx
->print_mode
);
934 dissector_entry_point(packet
, hdr
->tp_h
.tp_snaplen
,
935 ctx
->link_type
, ctx
->print_mode
);
937 if (frame_count_max
!= 0) {
938 if (frame_count
>= frame_count_max
) {
946 kernel_may_pull_from_rx(&hdr
->tp_h
);
949 if (it
>= rx_ring
.layout
.tp_frame_nr
)
952 if (unlikely(sigint
== 1))
955 if (dump_to_pcap(ctx
)) {
956 if (ctx
->dump_mode
== DUMP_INTERVAL_SIZE
) {
957 interval
+= hdr
->tp_h
.tp_snaplen
;
959 if (interval
> ctx
->dump_interval
) {
966 fd
= next_multi_pcap_file(ctx
, fd
);
970 print_pcap_file_stats(sock
, ctx
, skipped
);
975 poll(&rx_poll
, 1, -1);
978 bug_on(gettimeofday(&end
, NULL
));
979 timersub(&end
, &start
, &diff
);
981 if (!(ctx
->dump_dir
&& ctx
->print_mode
== PRINT_NONE
)) {
982 sock_print_net_stats(sock
, skipped
);
984 printf("\r%12lu sec, %lu usec in total\n",
985 diff
.tv_sec
, diff
.tv_usec
);
991 bpf_release(&bpf_ops
);
992 dissector_cleanup_all();
993 destroy_rx_ring(sock
, &rx_ring
);
995 if (ctx
->promiscuous
)
996 leave_promiscuous_mode(ctx
->device_in
, ifflags
);
999 leave_rfmon_mac80211(ctx
->device_trans
, ctx
->device_in
);
1001 if (dump_to_pcap(ctx
)) {
1003 finish_multi_pcap_file(ctx
, fd
);
1005 finish_single_pcap_file(ctx
, fd
);
1011 static void __noreturn
help(void)
1013 printf("\nnetsniff-ng %s, the packet sniffing beast\n", VERSION_STRING
);
1014 puts("http://www.netsniff-ng.org\n\n"
1015 "Usage: netsniff-ng [options] [filter-expression]\n"
1017 " -i|-d|--dev|--in <dev|pcap|-> Input source as netdev, pcap or pcap stdin\n"
1018 " -o|--out <dev|pcap|dir|cfg|-> Output sink as netdev, pcap, directory, trafgen, or stdout\n"
1019 " -f|--filter <bpf-file|expr> Use BPF filter file from bpfc or tcpdump-like expression\n"
1020 " -t|--type <type> Filter for: host|broadcast|multicast|others|outgoing\n"
1021 " -F|--interval <size|time> Dump interval if -o is a dir: <num>KiB/MiB/GiB/s/sec/min/hrs\n"
1022 " -J|--jumbo-support Support for 64KB Super Jumbo Frames (def: 2048B)\n"
1023 " -R|--rfraw Capture or inject raw 802.11 frames\n"
1024 " -n|--num <0|uint> Number of packets until exit (def: 0)\n"
1025 " -P|--prefix <name> Prefix for pcaps stored in directory\n"
1026 " -T|--magic <pcap-magic> Pcap magic number/pcap format to store, see -D\n"
1027 " -D|--dump-pcap-types Dump pcap types and magic numbers and quit\n"
1028 " -B|--dump-bpf Dump generated BPF assembly\n"
1029 " -r|--rand Randomize packet forwarding order (dev->dev)\n"
1030 " -M|--no-promisc No promiscuous mode for netdev\n"
1031 " -A|--no-sock-mem Don't tune core socket memory\n"
1032 " -m|--mmap Mmap(2) pcap file i.e., for replaying pcaps\n"
1033 " -G|--sg Scatter/gather pcap file I/O\n"
1034 " -c|--clrw Use slower read(2)/write(2) I/O\n"
1035 " -S|--ring-size <size> Specify ring size to: <num>KiB/MiB/GiB\n"
1036 " -k|--kernel-pull <uint> Kernel pull from user interval in us (def: 10us)\n"
1037 " -b|--bind-cpu <cpu> Bind to specific CPU\n"
1038 " -u|--user <userid> Drop privileges and change to userid\n"
1039 " -g|--group <groupid> Drop privileges and change to groupid\n"
1040 " -H|--prio-high Make this high priority process\n"
1041 " -Q|--notouch-irq Do not touch IRQ CPU affinity of NIC\n"
1042 " -s|--silent Do not print captured packets\n"
1043 " -q|--less Print less-verbose packet information\n"
1044 " -X|--hex Print packet data in hex format\n"
1045 " -l|--ascii Print human-readable packet data\n"
1046 " -U|--update Update GeoIP databases\n"
1047 " -V|--verbose Be more verbose\n"
1048 " -v|--version Show version and exit\n"
1049 " -h|--help Guess what?!\n\n"
1051 " netsniff-ng --in eth0 --out dump.pcap -s -T 0xa1b2c3d4 --b 0 tcp or udp\n"
1052 " netsniff-ng --in wlan0 --rfraw --out dump.pcap --silent --bind-cpu 0\n"
1053 " netsniff-ng --in dump.pcap --mmap --out eth0 -k1000 --silent --bind-cpu 0\n"
1054 " netsniff-ng --in dump.pcap --out dump.cfg --silent --bind-cpu 0\n"
1055 " netsniff-ng --in eth0 --out eth1 --silent --bind-cpu 0 --type host\n"
1056 " netsniff-ng --in eth1 --out /opt/probe/ -s -m -J --interval 100MiB -b 0\n"
1057 " netsniff-ng --in vlan0 --out dump.pcap -c -u `id -u bob` -g `id -g bob`\n"
1058 " netsniff-ng --in any --filter http.bpf --jumbo-support --ascii -V\n\n"
1060 " For introducing bit errors, delays with random variation and more\n"
1061 " while replaying pcaps, make use of tc(8) with its disciplines (e.g. netem).\n\n"
1062 "Please report bugs to <bugs@netsniff-ng.org>\n"
1063 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
1064 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel.roullit@gmail.com>\n"
1065 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1066 "Swiss federal institute of technology (ETH Zurich)\n"
1067 "License: GNU GPL version 2.0\n"
1068 "This is free software: you are free to change and redistribute it.\n"
1069 "There is NO WARRANTY, to the extent permitted by law.\n");
1073 static void __noreturn
version(void)
1075 printf("\nnetsniff-ng %s, the packet sniffing beast\n", VERSION_LONG
);
1076 puts("http://www.netsniff-ng.org\n\n"
1077 "Please report bugs to <bugs@netsniff-ng.org>\n"
1078 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
1079 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel.roullit@gmail.com>\n"
1080 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1081 "Swiss federal institute of technology (ETH Zurich)\n"
1082 "License: GNU GPL version 2.0\n"
1083 "This is free software: you are free to change and redistribute it.\n"
1084 "There is NO WARRANTY, to the extent permitted by law.\n");
1088 int main(int argc
, char **argv
)
1091 int c
, i
, j
, cpu_tmp
, opt_index
, ops_touched
= 0, vals
[4] = {0};
1092 bool prio_high
= false, setsockmem
= true;
1093 void (*main_loop
)(struct ctx
*ctx
) = NULL
;
1095 .link_type
= LINKTYPE_EN10MB
,
1096 .print_mode
= PRINT_NORM
,
1099 .promiscuous
= true,
1101 .pcap
= PCAP_OPS_SG
,
1102 .dump_interval
= 60,
1103 .dump_mode
= DUMP_INTERVAL_TIME
,
1106 .magic
= ORIGINAL_TCPDUMP_MAGIC
,
1111 while ((c
= getopt_long(argc
, argv
, short_options
, long_options
,
1112 &opt_index
)) != EOF
) {
1116 ctx
.device_in
= xstrdup(optarg
);
1119 ctx
.device_out
= xstrdup(optarg
);
1122 ctx
.prefix
= xstrdup(optarg
);
1125 ctx
.link_type
= LINKTYPE_IEEE802_11
;
1129 ctx
.randomize
= true;
1135 ctx
.magic
= (uint32_t) strtoul(optarg
, NULL
, 0);
1136 pcap_check_magic(ctx
.magic
);
1139 ctx
.filter
= xstrdup(optarg
);
1142 ctx
.promiscuous
= false;
1148 ctx
.uid
= strtoul(optarg
, NULL
, 0);
1152 ctx
.gid
= strtoul(optarg
, NULL
, 0);
1156 if (!strncmp(optarg
, "host", strlen("host")))
1157 ctx
.packet_type
= PACKET_HOST
;
1158 else if (!strncmp(optarg
, "broadcast", strlen("broadcast")))
1159 ctx
.packet_type
= PACKET_BROADCAST
;
1160 else if (!strncmp(optarg
, "multicast", strlen("multicast")))
1161 ctx
.packet_type
= PACKET_MULTICAST
;
1162 else if (!strncmp(optarg
, "others", strlen("others")))
1163 ctx
.packet_type
= PACKET_OTHERHOST
;
1164 else if (!strncmp(optarg
, "outgoing", strlen("outgoing")))
1165 ctx
.packet_type
= PACKET_OUTGOING
;
1167 ctx
.packet_type
= -1;
1171 ctx
.reserve_size
= 0;
1173 for (j
= i
= strlen(optarg
); i
> 0; --i
) {
1174 if (!isdigit(optarg
[j
- i
]))
1179 if (!strncmp(ptr
, "KiB", strlen("KiB")))
1180 ctx
.reserve_size
= 1 << 10;
1181 else if (!strncmp(ptr
, "MiB", strlen("MiB")))
1182 ctx
.reserve_size
= 1 << 20;
1183 else if (!strncmp(ptr
, "GiB", strlen("GiB")))
1184 ctx
.reserve_size
= 1 << 30;
1186 panic("Syntax error in ring size param!\n");
1189 ctx
.reserve_size
*= strtol(optarg
, NULL
, 0);
1192 cpu_tmp
= strtol(optarg
, NULL
, 0);
1194 cpu_affinity(cpu_tmp
);
1202 ctx
.pcap
= PCAP_OPS_RW
;
1206 ctx
.pcap
= PCAP_OPS_MM
;
1210 ctx
.pcap
= PCAP_OPS_SG
;
1217 ctx
.print_mode
= PRINT_NONE
;
1220 ctx
.print_mode
= PRINT_LESS
;
1224 (ctx
.print_mode
== PRINT_ASCII
) ?
1225 PRINT_HEX_ASCII
: PRINT_HEX
;
1229 (ctx
.print_mode
== PRINT_HEX
) ?
1230 PRINT_HEX_ASCII
: PRINT_ASCII
;
1233 ctx
.kpull
= strtol(optarg
, NULL
, 0);
1236 frame_count_max
= strtol(optarg
, NULL
, 0);
1240 ctx
.dump_interval
= 0;
1242 for (j
= i
= strlen(optarg
); i
> 0; --i
) {
1243 if (!isdigit(optarg
[j
- i
]))
1248 if (!strncmp(ptr
, "KiB", strlen("KiB"))) {
1249 ctx
.dump_interval
= 1 << 10;
1250 ctx
.dump_mode
= DUMP_INTERVAL_SIZE
;
1251 } else if (!strncmp(ptr
, "MiB", strlen("MiB"))) {
1252 ctx
.dump_interval
= 1 << 20;
1253 ctx
.dump_mode
= DUMP_INTERVAL_SIZE
;
1254 } else if (!strncmp(ptr
, "GiB", strlen("GiB"))) {
1255 ctx
.dump_interval
= 1 << 30;
1256 ctx
.dump_mode
= DUMP_INTERVAL_SIZE
;
1257 } else if (!strncmp(ptr
, "sec", strlen("sec"))) {
1258 ctx
.dump_interval
= 1;
1259 ctx
.dump_mode
= DUMP_INTERVAL_TIME
;
1260 } else if (!strncmp(ptr
, "min", strlen("min"))) {
1261 ctx
.dump_interval
= 60;
1262 ctx
.dump_mode
= DUMP_INTERVAL_TIME
;
1263 } else if (!strncmp(ptr
, "hrs", strlen("hrs"))) {
1264 ctx
.dump_interval
= 60 * 60;
1265 ctx
.dump_mode
= DUMP_INTERVAL_TIME
;
1266 } else if (!strncmp(ptr
, "s", strlen("s"))) {
1267 ctx
.dump_interval
= 1;
1268 ctx
.dump_mode
= DUMP_INTERVAL_TIME
;
1270 panic("Syntax error in time/size param!\n");
1274 ctx
.dump_interval
*= strtol(optarg
, NULL
, 0);
1280 ctx
.dump_bpf
= true;
1283 pcap_dump_type_features();
1313 panic("Option -%c requires an argument!\n",
1316 if (isprint(optopt
))
1317 printf("Unknown option character `0x%X\'!\n", optopt
);
1325 if (!ctx
.filter
&& optind
!= argc
) {
1329 for (i
= optind
; i
< argc
; ++i
) {
1330 size_t alen
= strlen(argv
[i
]) + 2;
1331 size_t flen
= ctx
.filter
? strlen(ctx
.filter
) : 0;
1333 ctx
.filter
= xrealloc(ctx
.filter
, 1, flen
+ alen
);
1334 ret
= slprintf(ctx
.filter
+ offset
, strlen(argv
[i
]) + 2, "%s ", argv
[i
]);
1336 panic("Cannot concatenate filter string!\n");
1343 ctx
.device_in
= xstrdup("any");
1345 register_signal(SIGINT
, signal_handler
);
1346 register_signal(SIGHUP
, signal_handler
);
1351 set_proc_prio(get_default_proc_prio());
1352 set_sched_status(get_default_sched_policy(), get_default_sched_prio());
1355 if (ctx
.device_in
&& (device_mtu(ctx
.device_in
) ||
1356 !strncmp("any", ctx
.device_in
, strlen(ctx
.device_in
)))) {
1357 if (!ctx
.device_out
) {
1359 main_loop
= recv_only_or_dump
;
1360 } else if (device_mtu(ctx
.device_out
)) {
1361 register_signal_f(SIGALRM
, timer_elapsed
, SA_SIGINFO
);
1362 main_loop
= receive_to_xmit
;
1365 register_signal_f(SIGALRM
, timer_next_dump
, SA_SIGINFO
);
1366 main_loop
= recv_only_or_dump
;
1368 ctx
.pcap
= PCAP_OPS_SG
;
1371 if (ctx
.device_out
&& device_mtu(ctx
.device_out
)) {
1372 register_signal_f(SIGALRM
, timer_elapsed
, SA_SIGINFO
);
1373 main_loop
= pcap_to_xmit
;
1375 ctx
.pcap
= PCAP_OPS_MM
;
1377 main_loop
= read_pcap
;
1379 ctx
.pcap
= PCAP_OPS_SG
;
1387 set_system_socket_memory(vals
, array_size(vals
));
1396 reset_system_socket_memory(vals
, array_size(vals
));
1401 free(ctx
.device_in
);
1402 free(ctx
.device_out
);
1403 free(ctx
.device_trans
);