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
;
58 unsigned long kpull
, dump_interval
, tx_bytes
, tx_packets
;
60 bool randomize
, promiscuous
, enforce
, jumbo
, dump_bpf
, hwtimestamp
, verbose
;
61 enum pcap_ops_groups pcap
; enum dump_mode dump_mode
;
62 uid_t uid
; gid_t gid
; uint32_t link_type
, magic
;
65 static volatile sig_atomic_t sigint
= 0;
66 static volatile bool next_dump
= false;
68 static const char *short_options
= "d:i:o:rf:MNJt:S:k:n:b:HQmcsqXlvhF:RGAP:Vu:g:T:DBU";
69 static const struct option long_options
[] = {
70 {"dev", required_argument
, NULL
, 'd'},
71 {"in", required_argument
, NULL
, 'i'},
72 {"out", required_argument
, NULL
, 'o'},
73 {"filter", required_argument
, NULL
, 'f'},
74 {"num", required_argument
, NULL
, 'n'},
75 {"type", required_argument
, NULL
, 't'},
76 {"interval", required_argument
, NULL
, 'F'},
77 {"ring-size", required_argument
, NULL
, 'S'},
78 {"kernel-pull", required_argument
, NULL
, 'k'},
79 {"bind-cpu", required_argument
, NULL
, 'b'},
80 {"prefix", required_argument
, NULL
, 'P'},
81 {"user", required_argument
, NULL
, 'u'},
82 {"group", required_argument
, NULL
, 'g'},
83 {"magic", required_argument
, NULL
, 'T'},
84 {"rand", no_argument
, NULL
, 'r'},
85 {"rfraw", no_argument
, NULL
, 'R'},
86 {"mmap", no_argument
, NULL
, 'm'},
87 {"sg", no_argument
, NULL
, 'G'},
88 {"clrw", no_argument
, NULL
, 'c'},
89 {"jumbo-support", no_argument
, NULL
, 'J'},
90 {"no-promisc", no_argument
, NULL
, 'M'},
91 {"no-hwtimestamp", no_argument
, NULL
, 'N'},
92 {"prio-high", no_argument
, NULL
, 'H'},
93 {"notouch-irq", no_argument
, NULL
, 'Q'},
94 {"dump-pcap-types", no_argument
, NULL
, 'D'},
95 {"dump-bpf", no_argument
, NULL
, 'B'},
96 {"silent", no_argument
, NULL
, 's'},
97 {"less", no_argument
, NULL
, 'q'},
98 {"hex", no_argument
, NULL
, 'X'},
99 {"ascii", no_argument
, NULL
, 'l'},
100 {"no-sock-mem", no_argument
, NULL
, 'A'},
101 {"update", no_argument
, NULL
, 'U'},
102 {"verbose", no_argument
, NULL
, 'V'},
103 {"version", no_argument
, NULL
, 'v'},
104 {"help", no_argument
, NULL
, 'h'},
109 static struct itimerval itimer
;
110 static unsigned long frame_count_max
= 0, interval
= TX_KERNEL_PULL_INT
;
112 #define __pcap_io pcap_ops[ctx->pcap]
114 static void signal_handler(int number
)
127 static void timer_elapsed(int unused __maybe_unused
)
131 set_itimer_interval_value(&itimer
, 0, interval
);
133 ret
= pull_and_flush_tx_ring(tx_sock
);
134 if (unlikely(ret
< 0)) {
135 /* We could hit EBADF if the socket has been closed before
136 * the timer was triggered.
138 if (errno
!= EBADF
&& errno
!= ENOBUFS
)
139 panic("Flushing TX_RING failed: %s!\n", strerror(errno
));
142 setitimer(ITIMER_REAL
, &itimer
, NULL
);
145 static void timer_purge(void)
149 ret
= pull_and_flush_tx_ring_wait(tx_sock
);
150 if (unlikely(ret
< 0)) {
151 if (errno
!= EBADF
&& errno
!= ENOBUFS
)
152 panic("Flushing TX_RING failed: %s!\n", strerror(errno
));
155 set_itimer_interval_value(&itimer
, 0, 0);
156 setitimer(ITIMER_REAL
, &itimer
, NULL
);
159 static void timer_next_dump(int unused __maybe_unused
)
161 set_itimer_interval_value(&itimer
, interval
, 0);
163 setitimer(ITIMER_REAL
, &itimer
, NULL
);
166 static inline bool dump_to_pcap(struct ctx
*ctx
)
171 static void pcap_to_xmit(struct ctx
*ctx
)
174 int ifindex
, fd
= 0, ret
;
177 unsigned long trunced
= 0;
179 struct frame_map
*hdr
;
180 struct sock_fprog bpf_ops
;
181 struct timeval start
, end
, diff
;
184 if (!device_up_and_running(ctx
->device_out
) && !ctx
->rfraw
)
185 panic("Device not up and running!\n");
189 tx_sock
= pf_socket();
191 if (!strncmp("-", ctx
->device_in
, strlen("-"))) {
192 fd
= dup_or_die(fileno(stdin
));
193 close(fileno(stdin
));
194 if (ctx
->pcap
== PCAP_OPS_MM
)
195 ctx
->pcap
= PCAP_OPS_SG
;
197 fd
= open_or_die(ctx
->device_in
, O_RDONLY
| O_LARGEFILE
| O_NOATIME
);
200 if (__pcap_io
->init_once_pcap
)
201 __pcap_io
->init_once_pcap();
203 ret
= __pcap_io
->pull_fhdr_pcap(fd
, &ctx
->magic
, &ctx
->link_type
);
205 panic("Error reading pcap header!\n");
207 if (__pcap_io
->prepare_access_pcap
) {
208 ret
= __pcap_io
->prepare_access_pcap(fd
, PCAP_MODE_RD
, ctx
->jumbo
);
210 panic("Error prepare reading pcap!\n");
214 ctx
->device_trans
= xstrdup(ctx
->device_out
);
215 xfree(ctx
->device_out
);
217 enter_rfmon_mac80211(ctx
->device_trans
, &ctx
->device_out
);
218 if (ctx
->link_type
!= LINKTYPE_IEEE802_11
)
219 panic("Wrong linktype of pcap!\n");
222 ifindex
= device_ifindex(ctx
->device_out
);
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 ring_tx_setup(&tx_ring
, tx_sock
, size
, ifindex
, ctx
->jumbo
, ctx
->verbose
);
231 dissector_init_all(ctx
->print_mode
);
233 if (ctx
->cpu
>= 0 && ifindex
> 0) {
234 int irq
= device_irq_number(ctx
->device_out
);
235 device_set_irq_affinity(irq
, ctx
->cpu
);
238 printf("IRQ: %s:%d > CPU%d\n",
239 ctx
->device_out
, irq
, ctx
->cpu
);
243 interval
= ctx
->kpull
;
245 set_itimer_interval_value(&itimer
, 0, interval
);
246 setitimer(ITIMER_REAL
, &itimer
, NULL
);
248 drop_privileges(ctx
->enforce
, ctx
->uid
, ctx
->gid
);
250 printf("Running! Hang up with ^C!\n\n");
253 bug_on(gettimeofday(&start
, NULL
));
255 while (likely(sigint
== 0)) {
256 while (user_may_pull_from_tx(tx_ring
.frames
[it
].iov_base
)) {
257 hdr
= tx_ring
.frames
[it
].iov_base
;
258 out
= ((uint8_t *) hdr
) + TPACKET2_HDRLEN
- sizeof(struct sockaddr_ll
);
261 ret
= __pcap_io
->read_pcap(fd
, &phdr
, ctx
->magic
, out
,
262 ring_frame_size(&tx_ring
));
263 if (unlikely(ret
<= 0))
266 if (ring_frame_size(&tx_ring
) <
267 pcap_get_length(&phdr
, ctx
->magic
)) {
268 pcap_set_length(&phdr
, ctx
->magic
,
269 ring_frame_size(&tx_ring
));
272 } while (ctx
->filter
&&
273 !bpf_run_filter(&bpf_ops
, out
,
274 pcap_get_length(&phdr
, ctx
->magic
)));
276 pcap_pkthdr_to_tpacket_hdr(&phdr
, ctx
->magic
, &hdr
->tp_h
, NULL
);
278 ctx
->tx_bytes
+= hdr
->tp_h
.tp_len
;;
281 show_frame_hdr(out
, hdr
->tp_h
.tp_snaplen
,
282 ctx
->link_type
, hdr
, ctx
->print_mode
);
284 dissector_entry_point(out
, hdr
->tp_h
.tp_snaplen
,
285 ctx
->link_type
, ctx
->print_mode
);
287 kernel_may_pull_from_tx(&hdr
->tp_h
);
290 if (it
>= tx_ring
.layout
.tp_frame_nr
)
293 if (unlikely(sigint
== 1))
296 if (frame_count_max
!= 0) {
297 if (ctx
->tx_packets
>= frame_count_max
) {
306 bug_on(gettimeofday(&end
, NULL
));
307 timersub(&end
, &start
, &diff
);
311 bpf_release(&bpf_ops
);
313 dissector_cleanup_all();
314 destroy_tx_ring(tx_sock
, &tx_ring
);
317 leave_rfmon_mac80211(ctx
->device_out
);
319 if (__pcap_io
->prepare_close_pcap
)
320 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_RD
);
322 if (!strncmp("-", ctx
->device_in
, strlen("-")))
323 dup2(fd
, fileno(stdin
));
330 printf("\r%12lu packets outgoing\n", ctx
->tx_packets
);
331 printf("\r%12lu packets truncated in file\n", trunced
);
332 printf("\r%12lu bytes outgoing\n", ctx
->tx_bytes
);
333 printf("\r%12lu sec, %lu usec in total\n", diff
.tv_sec
, diff
.tv_usec
);
336 static void receive_to_xmit(struct ctx
*ctx
)
340 int rx_sock
, ifindex_in
, ifindex_out
, ret
;
341 size_t size_in
, size_out
;
342 unsigned int it_in
= 0, it_out
= 0;
343 unsigned long frame_count
= 0;
344 struct frame_map
*hdr_in
, *hdr_out
;
345 struct ring tx_ring
, rx_ring
;
346 struct pollfd rx_poll
;
347 struct sock_fprog bpf_ops
;
349 if (!strncmp(ctx
->device_in
, ctx
->device_out
, IFNAMSIZ
))
350 panic("Ingress/egress devices must be different!\n");
351 if (!device_up_and_running(ctx
->device_out
))
352 panic("Egress device not up and running!\n");
354 rx_sock
= pf_socket();
355 tx_sock
= pf_socket();
357 ifindex_in
= device_ifindex(ctx
->device_in
);
358 ifindex_out
= device_ifindex(ctx
->device_out
);
360 size_in
= ring_size(ctx
->device_in
, ctx
->reserve_size
);
361 size_out
= ring_size(ctx
->device_out
, ctx
->reserve_size
);
363 enable_kernel_bpf_jit_compiler();
365 bpf_parse_rules(ctx
->filter
, &bpf_ops
, ctx
->link_type
);
367 bpf_dump_all(&bpf_ops
);
368 bpf_attach_to_sock(rx_sock
, &bpf_ops
);
370 ring_rx_setup(&rx_ring
, rx_sock
, size_in
, ifindex_in
, &rx_poll
, false, ctx
->jumbo
, ctx
->verbose
);
371 ring_tx_setup(&tx_ring
, tx_sock
, size_out
, ifindex_out
, ctx
->jumbo
, ctx
->verbose
);
373 dissector_init_all(ctx
->print_mode
);
375 if (ctx
->promiscuous
)
376 ifflags
= device_enter_promiscuous_mode(ctx
->device_in
);
379 interval
= ctx
->kpull
;
381 set_itimer_interval_value(&itimer
, 0, interval
);
382 setitimer(ITIMER_REAL
, &itimer
, NULL
);
384 drop_privileges(ctx
->enforce
, ctx
->uid
, ctx
->gid
);
386 printf("Running! Hang up with ^C!\n\n");
389 while (likely(sigint
== 0)) {
390 while (user_may_pull_from_rx(rx_ring
.frames
[it_in
].iov_base
)) {
391 hdr_in
= rx_ring
.frames
[it_in
].iov_base
;
392 in
= ((uint8_t *) hdr_in
) + hdr_in
->tp_h
.tp_mac
;
396 if (ctx
->packet_type
!= -1)
397 if (ctx
->packet_type
!= hdr_in
->s_ll
.sll_pkttype
)
400 hdr_out
= tx_ring
.frames
[it_out
].iov_base
;
401 out
= ((uint8_t *) hdr_out
) + TPACKET2_HDRLEN
- sizeof(struct sockaddr_ll
);
403 while (!user_may_pull_from_tx(tx_ring
.frames
[it_out
].iov_base
) &&
406 next_rnd_slot(&it_out
, &tx_ring
);
409 if (it_out
>= tx_ring
.layout
.tp_frame_nr
)
413 hdr_out
= tx_ring
.frames
[it_out
].iov_base
;
414 out
= ((uint8_t *) hdr_out
) + TPACKET2_HDRLEN
- sizeof(struct sockaddr_ll
);
417 tpacket_hdr_clone(&hdr_out
->tp_h
, &hdr_in
->tp_h
);
418 fmemcpy(out
, in
, hdr_in
->tp_h
.tp_len
);
420 kernel_may_pull_from_tx(&hdr_out
->tp_h
);
422 next_rnd_slot(&it_out
, &tx_ring
);
425 if (it_out
>= tx_ring
.layout
.tp_frame_nr
)
429 show_frame_hdr(in
, hdr_in
->tp_h
.tp_snaplen
,
430 ctx
->link_type
, hdr_in
, ctx
->print_mode
);
432 dissector_entry_point(in
, hdr_in
->tp_h
.tp_snaplen
,
433 ctx
->link_type
, ctx
->print_mode
);
435 if (frame_count_max
!= 0) {
436 if (frame_count
>= frame_count_max
) {
443 kernel_may_pull_from_rx(&hdr_in
->tp_h
);
446 if (it_in
>= rx_ring
.layout
.tp_frame_nr
)
449 if (unlikely(sigint
== 1))
453 ret
= poll(&rx_poll
, 1, -1);
454 if (unlikely(ret
< 0)) {
456 panic("Poll failed!\n");
463 sock_rx_net_stats(rx_sock
, 0);
465 bpf_release(&bpf_ops
);
467 dissector_cleanup_all();
469 destroy_tx_ring(tx_sock
, &tx_ring
);
470 destroy_rx_ring(rx_sock
, &rx_ring
);
472 if (ctx
->promiscuous
)
473 device_leave_promiscuous_mode(ctx
->device_in
, ifflags
);
479 static void translate_pcap_to_txf(int fdo
, uint8_t *out
, size_t len
)
481 size_t bytes_done
= 0;
484 slprintf(bout
, sizeof(bout
), "{\n ");
485 write_or_die(fdo
, bout
, strlen(bout
));
487 while (bytes_done
< len
) {
488 slprintf(bout
, sizeof(bout
), "0x%02x, ", out
[bytes_done
]);
489 write_or_die(fdo
, bout
, strlen(bout
));
493 if (bytes_done
% 10 == 0) {
494 slprintf(bout
, sizeof(bout
), "\n");
495 write_or_die(fdo
, bout
, strlen(bout
));
497 if (bytes_done
< len
) {
498 slprintf(bout
, sizeof(bout
), " ");
499 write_or_die(fdo
, bout
, strlen(bout
));
503 if (bytes_done
% 10 != 0) {
504 slprintf(bout
, sizeof(bout
), "\n");
505 write_or_die(fdo
, bout
, strlen(bout
));
508 slprintf(bout
, sizeof(bout
), "}\n\n");
509 write_or_die(fdo
, bout
, strlen(bout
));
512 static void read_pcap(struct ctx
*ctx
)
515 int ret
, fd
, fdo
= 0;
516 unsigned long trunced
= 0;
519 struct sock_fprog bpf_ops
;
521 struct timeval start
, end
, diff
;
525 if (!strncmp("-", ctx
->device_in
, strlen("-"))) {
526 fd
= dup_or_die(fileno(stdin
));
527 close(fileno(stdin
));
528 if (ctx
->pcap
== PCAP_OPS_MM
)
529 ctx
->pcap
= PCAP_OPS_SG
;
531 fd
= open_or_die(ctx
->device_in
, O_RDONLY
| O_LARGEFILE
| O_NOATIME
);
534 if (__pcap_io
->init_once_pcap
)
535 __pcap_io
->init_once_pcap();
537 ret
= __pcap_io
->pull_fhdr_pcap(fd
, &ctx
->magic
, &ctx
->link_type
);
539 panic("Error reading pcap header!\n");
541 if (__pcap_io
->prepare_access_pcap
) {
542 ret
= __pcap_io
->prepare_access_pcap(fd
, PCAP_MODE_RD
, ctx
->jumbo
);
544 panic("Error prepare reading pcap!\n");
547 fmemset(&fm
, 0, sizeof(fm
));
549 bpf_parse_rules(ctx
->filter
, &bpf_ops
, ctx
->link_type
);
551 bpf_dump_all(&bpf_ops
);
553 dissector_init_all(ctx
->print_mode
);
555 out_len
= round_up(1024 * 1024, RUNTIME_PAGE_SIZE
);
556 out
= xmalloc_aligned(out_len
, CO_CACHE_LINE_SIZE
);
558 if (ctx
->device_out
) {
559 if (!strncmp("-", ctx
->device_out
, strlen("-"))) {
560 fdo
= dup_or_die(fileno(stdout
));
561 close(fileno(stdout
));
563 fdo
= open_or_die_m(ctx
->device_out
, O_RDWR
| O_CREAT
|
564 O_TRUNC
| O_LARGEFILE
, DEFFILEMODE
);
568 drop_privileges(ctx
->enforce
, ctx
->uid
, ctx
->gid
);
570 printf("Running! Hang up with ^C!\n\n");
573 bug_on(gettimeofday(&start
, NULL
));
575 while (likely(sigint
== 0)) {
577 ret
= __pcap_io
->read_pcap(fd
, &phdr
, ctx
->magic
,
579 if (unlikely(ret
< 0))
582 if (unlikely(pcap_get_length(&phdr
, ctx
->magic
) == 0)) {
587 if (unlikely(pcap_get_length(&phdr
, ctx
->magic
) > out_len
)) {
588 pcap_set_length(&phdr
, ctx
->magic
, out_len
);
591 } while (ctx
->filter
&&
592 !bpf_run_filter(&bpf_ops
, out
,
593 pcap_get_length(&phdr
, ctx
->magic
)));
595 pcap_pkthdr_to_tpacket_hdr(&phdr
, ctx
->magic
, &fm
.tp_h
, &fm
.s_ll
);
597 ctx
->tx_bytes
+= fm
.tp_h
.tp_len
;
600 show_frame_hdr(out
, fm
.tp_h
.tp_snaplen
, ctx
->link_type
, &fm
,
603 dissector_entry_point(out
, fm
.tp_h
.tp_snaplen
,
604 ctx
->link_type
, ctx
->print_mode
);
607 translate_pcap_to_txf(fdo
, out
, fm
.tp_h
.tp_snaplen
);
609 if (frame_count_max
!= 0) {
610 if (ctx
->tx_packets
>= frame_count_max
) {
619 bug_on(gettimeofday(&end
, NULL
));
620 timersub(&end
, &start
, &diff
);
622 bpf_release(&bpf_ops
);
624 dissector_cleanup_all();
626 if (__pcap_io
->prepare_close_pcap
)
627 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_RD
);
633 printf("\r%12lu packets outgoing\n", ctx
->tx_packets
);
634 printf("\r%12lu packets truncated in file\n", trunced
);
635 printf("\r%12lu bytes outgoing\n", ctx
->tx_bytes
);
636 printf("\r%12lu sec, %lu usec in total\n", diff
.tv_sec
, diff
.tv_usec
);
638 if (!strncmp("-", ctx
->device_in
, strlen("-")))
639 dup2(fd
, fileno(stdin
));
642 if (ctx
->device_out
) {
643 if (!strncmp("-", ctx
->device_out
, strlen("-")))
644 dup2(fdo
, fileno(stdout
));
649 static void finish_multi_pcap_file(struct ctx
*ctx
, int fd
)
651 __pcap_io
->fsync_pcap(fd
);
653 if (__pcap_io
->prepare_close_pcap
)
654 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_WR
);
658 fmemset(&itimer
, 0, sizeof(itimer
));
659 setitimer(ITIMER_REAL
, &itimer
, NULL
);
662 static int next_multi_pcap_file(struct ctx
*ctx
, int fd
)
667 __pcap_io
->fsync_pcap(fd
);
669 if (__pcap_io
->prepare_close_pcap
)
670 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_WR
);
674 slprintf(fname
, sizeof(fname
), "%s/%s%lu.pcap", ctx
->device_out
,
675 ctx
->prefix
? : "dump-", time(NULL
));
677 fd
= open_or_die_m(fname
, O_RDWR
| O_CREAT
| O_TRUNC
|
678 O_LARGEFILE
, DEFFILEMODE
);
680 ret
= __pcap_io
->push_fhdr_pcap(fd
, ctx
->magic
, ctx
->link_type
);
682 panic("Error writing pcap header!\n");
684 if (__pcap_io
->prepare_access_pcap
) {
685 ret
= __pcap_io
->prepare_access_pcap(fd
, PCAP_MODE_WR
, true);
687 panic("Error prepare writing pcap!\n");
693 static int begin_multi_pcap_file(struct ctx
*ctx
)
700 if (ctx
->device_out
[strlen(ctx
->device_out
) - 1] == '/')
701 ctx
->device_out
[strlen(ctx
->device_out
) - 1] = 0;
703 slprintf(fname
, sizeof(fname
), "%s/%s%lu.pcap", ctx
->device_out
,
704 ctx
->prefix
? : "dump-", time(NULL
));
706 fd
= open_or_die_m(fname
, O_RDWR
| O_CREAT
| O_TRUNC
|
707 O_LARGEFILE
, DEFFILEMODE
);
709 ret
= __pcap_io
->push_fhdr_pcap(fd
, ctx
->magic
, ctx
->link_type
);
711 panic("Error writing pcap header!\n");
713 if (__pcap_io
->prepare_access_pcap
) {
714 ret
= __pcap_io
->prepare_access_pcap(fd
, PCAP_MODE_WR
, true);
716 panic("Error prepare writing pcap!\n");
719 if (ctx
->dump_mode
== DUMP_INTERVAL_TIME
) {
720 interval
= ctx
->dump_interval
;
722 set_itimer_interval_value(&itimer
, interval
, 0);
723 setitimer(ITIMER_REAL
, &itimer
, NULL
);
731 static void finish_single_pcap_file(struct ctx
*ctx
, int fd
)
733 __pcap_io
->fsync_pcap(fd
);
735 if (__pcap_io
->prepare_close_pcap
)
736 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_WR
);
738 if (strncmp("-", ctx
->device_out
, strlen("-")))
741 dup2(fd
, fileno(stdout
));
744 static int begin_single_pcap_file(struct ctx
*ctx
)
750 if (!strncmp("-", ctx
->device_out
, strlen("-"))) {
751 fd
= dup_or_die(fileno(stdout
));
752 close(fileno(stdout
));
753 if (ctx
->pcap
== PCAP_OPS_MM
)
754 ctx
->pcap
= PCAP_OPS_SG
;
756 fd
= open_or_die_m(ctx
->device_out
,
757 O_RDWR
| O_CREAT
| O_TRUNC
|
758 O_LARGEFILE
, DEFFILEMODE
);
761 ret
= __pcap_io
->push_fhdr_pcap(fd
, ctx
->magic
, ctx
->link_type
);
763 panic("Error writing pcap header!\n");
765 if (__pcap_io
->prepare_access_pcap
) {
766 ret
= __pcap_io
->prepare_access_pcap(fd
, PCAP_MODE_WR
, true);
768 panic("Error prepare writing pcap!\n");
774 static void print_pcap_file_stats(int sock
, struct ctx
*ctx
)
777 struct tpacket_stats kstats
;
778 socklen_t slen
= sizeof(kstats
);
780 fmemset(&kstats
, 0, sizeof(kstats
));
782 ret
= getsockopt(sock
, SOL_PACKET
, PACKET_STATISTICS
, &kstats
, &slen
);
784 panic("Cannot get packet statistics!\n");
786 if (ctx
->print_mode
== PRINT_NONE
) {
787 printf(".(+%u/-%u)", kstats
.tp_packets
- kstats
.tp_drops
,
794 static void walk_t3_block(struct block_desc
*pbd
, struct ctx
*ctx
,
795 int sock
, int *fd
, unsigned long *frame_count
)
797 int num_pkts
= pbd
->h1
.num_pkts
, i
;
798 struct tpacket3_hdr
*hdr
;
799 struct sockaddr_ll
*sll
;
801 hdr
= (void *) ((uint8_t *) pbd
+ pbd
->h1
.offset_to_first_pkt
);
802 sll
= (void *) ((uint8_t *) hdr
+ TPACKET_ALIGN(sizeof(*hdr
)));
804 for (i
= 0; i
< num_pkts
&& likely(sigint
== 0); ++i
) {
805 uint8_t *packet
= ((uint8_t *) hdr
+ hdr
->tp_mac
);
808 if (ctx
->packet_type
!= -1)
809 if (ctx
->packet_type
!= sll
->sll_pkttype
)
814 if (dump_to_pcap(ctx
)) {
817 tpacket3_hdr_to_pcap_pkthdr(hdr
, sll
, &phdr
, ctx
->magic
);
819 ret
= __pcap_io
->write_pcap(*fd
, &phdr
, ctx
->magic
, packet
,
820 pcap_get_length(&phdr
, ctx
->magic
));
821 if (unlikely(ret
!= (int) pcap_get_total_length(&phdr
, ctx
->magic
)))
822 panic("Write error to pcap!\n");
825 __show_frame_hdr(packet
, hdr
->tp_snaplen
, ctx
->link_type
, sll
,
826 hdr
, ctx
->print_mode
, true);
828 dissector_entry_point(packet
, hdr
->tp_snaplen
, ctx
->link_type
,
831 hdr
= (void *) ((uint8_t *) hdr
+ hdr
->tp_next_offset
);
832 sll
= (void *) ((uint8_t *) hdr
+ TPACKET_ALIGN(sizeof(*hdr
)));
834 if (frame_count_max
!= 0) {
835 if (unlikely(*frame_count
>= frame_count_max
)) {
841 if (dump_to_pcap(ctx
)) {
842 if (ctx
->dump_mode
== DUMP_INTERVAL_SIZE
) {
843 interval
+= hdr
->tp_snaplen
;
844 if (interval
> ctx
->dump_interval
) {
851 *fd
= next_multi_pcap_file(ctx
, *fd
);
854 if (unlikely(ctx
->verbose
))
855 print_pcap_file_stats(sock
, ctx
);
860 #endif /* HAVE_TPACKET3 */
862 static void recv_only_or_dump(struct ctx
*ctx
)
865 int sock
, ifindex
, fd
= 0, ret
;
869 struct pollfd rx_poll
;
870 struct sock_fprog bpf_ops
;
871 struct timeval start
, end
, diff
;
872 unsigned long frame_count
= 0;
874 struct block_desc
*pbd
;
880 ctx
->device_trans
= xstrdup(ctx
->device_in
);
881 xfree(ctx
->device_in
);
883 enter_rfmon_mac80211(ctx
->device_trans
, &ctx
->device_in
);
884 ctx
->link_type
= LINKTYPE_IEEE802_11
;
887 ifindex
= device_ifindex(ctx
->device_in
);
889 size
= ring_size(ctx
->device_in
, ctx
->reserve_size
);
891 enable_kernel_bpf_jit_compiler();
893 bpf_parse_rules(ctx
->filter
, &bpf_ops
, ctx
->link_type
);
895 bpf_dump_all(&bpf_ops
);
896 bpf_attach_to_sock(sock
, &bpf_ops
);
898 if (ctx
->hwtimestamp
) {
899 ret
= set_sockopt_hwtimestamp(sock
, ctx
->device_in
);
900 if (ret
== 0 && ctx
->verbose
)
901 printf("HW timestamping enabled\n");
904 ring_rx_setup(&rx_ring
, sock
, size
, ifindex
, &rx_poll
, true, true, ctx
->verbose
);
906 dissector_init_all(ctx
->print_mode
);
908 if (ctx
->cpu
>= 0 && ifindex
> 0) {
909 int irq
= device_irq_number(ctx
->device_in
);
910 device_set_irq_affinity(irq
, ctx
->cpu
);
913 printf("IRQ: %s:%d > CPU%d\n",
914 ctx
->device_in
, irq
, ctx
->cpu
);
917 if (ctx
->promiscuous
)
918 ifflags
= device_enter_promiscuous_mode(ctx
->device_in
);
920 if (dump_to_pcap(ctx
) && __pcap_io
->init_once_pcap
)
921 __pcap_io
->init_once_pcap();
923 drop_privileges(ctx
->enforce
, ctx
->uid
, ctx
->gid
);
925 if (dump_to_pcap(ctx
)) {
928 ret
= stat(ctx
->device_out
, &stats
);
932 ctx
->dump_dir
= S_ISDIR(stats
.st_mode
);
935 fd
= begin_multi_pcap_file(ctx
);
937 fd
= begin_single_pcap_file(ctx
);
940 printf("Running! Hang up with ^C!\n\n");
943 bug_on(gettimeofday(&start
, NULL
));
945 while (likely(sigint
== 0)) {
947 while (user_may_pull_from_rx_block((pbd
= (void *)
948 rx_ring
.frames
[it
].iov_base
))) {
949 walk_t3_block(pbd
, ctx
, sock
, &fd
, &frame_count
);
951 kernel_may_pull_from_rx_block(pbd
);
952 it
= (it
+ 1) % rx_ring
.layout3
.tp_block_nr
;
954 if (unlikely(sigint
== 1))
958 while (user_may_pull_from_rx(rx_ring
.frames
[it
].iov_base
)) {
959 struct frame_map
*hdr
= rx_ring
.frames
[it
].iov_base
;
960 uint8_t *packet
= ((uint8_t *) hdr
) + hdr
->tp_h
.tp_mac
;
963 if (ctx
->packet_type
!= -1)
964 if (ctx
->packet_type
!= hdr
->s_ll
.sll_pkttype
)
969 if (unlikely(ring_frame_size(&rx_ring
) < hdr
->tp_h
.tp_snaplen
)) {
970 /* XXX: silently ignore for now. We used to
971 * report them with sock_rx_net_stats() */
975 if (dump_to_pcap(ctx
)) {
976 tpacket_hdr_to_pcap_pkthdr(&hdr
->tp_h
, &hdr
->s_ll
, &phdr
, ctx
->magic
);
978 ret
= __pcap_io
->write_pcap(fd
, &phdr
, ctx
->magic
, packet
,
979 pcap_get_length(&phdr
, ctx
->magic
));
980 if (unlikely(ret
!= (int) pcap_get_total_length(&phdr
, ctx
->magic
)))
981 panic("Write error to pcap!\n");
984 show_frame_hdr(packet
, hdr
->tp_h
.tp_snaplen
,
985 ctx
->link_type
, hdr
, ctx
->print_mode
);
987 dissector_entry_point(packet
, hdr
->tp_h
.tp_snaplen
,
988 ctx
->link_type
, ctx
->print_mode
);
990 if (frame_count_max
!= 0) {
991 if (unlikely(frame_count
>= frame_count_max
)) {
998 kernel_may_pull_from_rx(&hdr
->tp_h
);
999 it
= (it
+ 1) % rx_ring
.layout
.tp_frame_nr
;
1001 if (unlikely(sigint
== 1))
1004 if (dump_to_pcap(ctx
)) {
1005 if (ctx
->dump_mode
== DUMP_INTERVAL_SIZE
) {
1006 interval
+= hdr
->tp_h
.tp_snaplen
;
1007 if (interval
> ctx
->dump_interval
) {
1014 fd
= next_multi_pcap_file(ctx
, fd
);
1017 if (unlikely(ctx
->verbose
))
1018 print_pcap_file_stats(sock
, ctx
);
1022 #endif /* HAVE_TPACKET3 */
1024 ret
= poll(&rx_poll
, 1, -1);
1025 if (unlikely(ret
< 0)) {
1027 panic("Poll failed!\n");
1031 bug_on(gettimeofday(&end
, NULL
));
1032 timersub(&end
, &start
, &diff
);
1034 if (!(ctx
->dump_dir
&& ctx
->print_mode
== PRINT_NONE
)) {
1035 sock_rx_net_stats(sock
, frame_count
);
1037 printf("\r%12lu sec, %lu usec in total\n",
1038 diff
.tv_sec
, diff
.tv_usec
);
1044 bpf_release(&bpf_ops
);
1045 dissector_cleanup_all();
1046 destroy_rx_ring(sock
, &rx_ring
);
1048 if (ctx
->promiscuous
)
1049 device_leave_promiscuous_mode(ctx
->device_in
, ifflags
);
1052 leave_rfmon_mac80211(ctx
->device_in
);
1054 if (dump_to_pcap(ctx
)) {
1056 finish_multi_pcap_file(ctx
, fd
);
1058 finish_single_pcap_file(ctx
, fd
);
1064 static void init_ctx(struct ctx
*ctx
)
1066 memset(ctx
, 0, sizeof(*ctx
));
1067 ctx
->uid
= getuid();
1068 ctx
->uid
= getgid();
1071 ctx
->packet_type
= -1;
1073 ctx
->magic
= ORIGINAL_TCPDUMP_MAGIC
;
1074 ctx
->print_mode
= PRINT_NORM
;
1075 ctx
->pcap
= PCAP_OPS_SG
;
1077 ctx
->dump_mode
= DUMP_INTERVAL_TIME
;
1078 ctx
->dump_interval
= 60;
1080 ctx
->promiscuous
= true;
1081 ctx
->randomize
= false;
1082 ctx
->hwtimestamp
= true;
1085 static void destroy_ctx(struct ctx
*ctx
)
1087 free(ctx
->device_in
);
1088 free(ctx
->device_out
);
1089 free(ctx
->device_trans
);
1094 static void __noreturn
help(void)
1096 printf("\nnetsniff-ng %s, the packet sniffing beast\n", VERSION_STRING
);
1097 puts("http://www.netsniff-ng.org\n\n"
1098 "Usage: netsniff-ng [options] [filter-expression]\n"
1100 " -i|-d|--dev|--in <dev|pcap|-> Input source as netdev, pcap or pcap stdin\n"
1101 " -o|--out <dev|pcap|dir|cfg|-> Output sink as netdev, pcap, directory, trafgen, or stdout\n"
1102 " -f|--filter <bpf-file|expr> Use BPF filter file from bpfc or tcpdump-like expression\n"
1103 " -t|--type <type> Filter for: host|broadcast|multicast|others|outgoing\n"
1104 " -F|--interval <size|time> Dump interval if -o is a dir: <num>KiB/MiB/GiB/s/sec/min/hrs\n"
1105 " -R|--rfraw Capture or inject raw 802.11 frames\n"
1106 " -n|--num <0|uint> Number of packets until exit (def: 0)\n"
1107 " -P|--prefix <name> Prefix for pcaps stored in directory\n"
1108 " -T|--magic <pcap-magic> Pcap magic number/pcap format to store, see -D\n"
1109 " -D|--dump-pcap-types Dump pcap types and magic numbers and quit\n"
1110 " -B|--dump-bpf Dump generated BPF assembly\n"
1111 " -r|--rand Randomize packet forwarding order (dev->dev)\n"
1112 " -M|--no-promisc No promiscuous mode for netdev\n"
1113 " -A|--no-sock-mem Don't tune core socket memory\n"
1114 " -N|--no-hwtimestamp Disable hardware time stamping\n"
1115 " -m|--mmap Mmap(2) pcap file I/O, e.g. for replaying pcaps\n"
1116 " -G|--sg Scatter/gather pcap file I/O\n"
1117 " -c|--clrw Use slower read(2)/write(2) I/O\n"
1118 " -S|--ring-size <size> Specify ring size to: <num>KiB/MiB/GiB\n"
1119 " -k|--kernel-pull <uint> Kernel pull from user interval in us (def: 10us)\n"
1120 " -J|--jumbo-support Support replay/fwd 64KB Super Jumbo Frames (def: 2048B)\n"
1121 " -b|--bind-cpu <cpu> Bind to specific CPU\n"
1122 " -u|--user <userid> Drop privileges and change to userid\n"
1123 " -g|--group <groupid> Drop privileges and change to groupid\n"
1124 " -H|--prio-high Make this high priority process\n"
1125 " -Q|--notouch-irq Do not touch IRQ CPU affinity of NIC\n"
1126 " -s|--silent Do not print captured packets\n"
1127 " -q|--less Print less-verbose packet information\n"
1128 " -X|--hex Print packet data in hex format\n"
1129 " -l|--ascii Print human-readable packet data\n"
1130 " -U|--update Update GeoIP databases\n"
1131 " -V|--verbose Be more verbose\n"
1132 " -v|--version Show version and exit\n"
1133 " -h|--help Guess what?!\n\n"
1135 " netsniff-ng --in eth0 --out dump.pcap -s -T 0xa1b2c3d4 --b 0 tcp or udp\n"
1136 " netsniff-ng --in wlan0 --rfraw --out dump.pcap --silent --bind-cpu 0\n"
1137 " netsniff-ng --in dump.pcap --mmap --out eth0 -k1000 --silent --bind-cpu 0\n"
1138 " netsniff-ng --in dump.pcap --out dump.cfg --silent --bind-cpu 0\n"
1139 " netsniff-ng --in eth0 --out eth1 --silent --bind-cpu 0 -J --type host\n"
1140 " netsniff-ng --in eth1 --out /opt/probe/ -s -m --interval 100MiB -b 0\n"
1141 " netsniff-ng --in vlan0 --out dump.pcap -c -u `id -u bob` -g `id -g bob`\n"
1142 " netsniff-ng --in any --filter http.bpf --jumbo-support --ascii -V\n\n"
1144 " For introducing bit errors, delays with random variation and more\n"
1145 " while replaying pcaps, make use of tc(8) with its disciplines (e.g. netem).\n\n"
1146 "Please report bugs to <bugs@netsniff-ng.org>\n"
1147 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
1148 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel.roullit@gmail.com>\n"
1149 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1150 "Swiss federal institute of technology (ETH Zurich)\n"
1151 "License: GNU GPL version 2.0\n"
1152 "This is free software: you are free to change and redistribute it.\n"
1153 "There is NO WARRANTY, to the extent permitted by law.\n");
1157 static void __noreturn
version(void)
1159 printf("\nnetsniff-ng %s, Git id: %s\n", VERSION_LONG
, GITVERSION
);
1160 puts("the packet sniffing beast\n"
1161 "http://www.netsniff-ng.org\n\n"
1162 "Please report bugs to <bugs@netsniff-ng.org>\n"
1163 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
1164 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel.roullit@gmail.com>\n"
1165 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1166 "Swiss federal institute of technology (ETH Zurich)\n"
1167 "License: GNU GPL version 2.0\n"
1168 "This is free software: you are free to change and redistribute it.\n"
1169 "There is NO WARRANTY, to the extent permitted by law.\n");
1173 int main(int argc
, char **argv
)
1176 int c
, i
, j
, cpu_tmp
, opt_index
, ops_touched
= 0, vals
[4] = {0};
1177 bool prio_high
= false, setsockmem
= true;
1178 void (*main_loop
)(struct ctx
*ctx
) = NULL
;
1184 while ((c
= getopt_long(argc
, argv
, short_options
, long_options
,
1185 &opt_index
)) != EOF
) {
1189 ctx
.device_in
= xstrdup(optarg
);
1192 ctx
.device_out
= xstrdup(optarg
);
1195 ctx
.prefix
= xstrdup(optarg
);
1198 ctx
.link_type
= LINKTYPE_IEEE802_11
;
1202 ctx
.randomize
= true;
1208 ctx
.magic
= (uint32_t) strtoul(optarg
, NULL
, 0);
1209 pcap_check_magic(ctx
.magic
);
1212 ctx
.filter
= xstrdup(optarg
);
1215 ctx
.promiscuous
= false;
1218 ctx
.hwtimestamp
= false;
1224 ctx
.uid
= strtoul(optarg
, NULL
, 0);
1228 ctx
.gid
= strtoul(optarg
, NULL
, 0);
1232 if (!strncmp(optarg
, "host", strlen("host")))
1233 ctx
.packet_type
= PACKET_HOST
;
1234 else if (!strncmp(optarg
, "broadcast", strlen("broadcast")))
1235 ctx
.packet_type
= PACKET_BROADCAST
;
1236 else if (!strncmp(optarg
, "multicast", strlen("multicast")))
1237 ctx
.packet_type
= PACKET_MULTICAST
;
1238 else if (!strncmp(optarg
, "others", strlen("others")))
1239 ctx
.packet_type
= PACKET_OTHERHOST
;
1240 else if (!strncmp(optarg
, "outgoing", strlen("outgoing")))
1241 ctx
.packet_type
= PACKET_OUTGOING
;
1243 ctx
.packet_type
= -1;
1247 for (j
= i
= strlen(optarg
); i
> 0; --i
) {
1248 if (!isdigit(optarg
[j
- i
]))
1253 if (!strncmp(ptr
, "KiB", strlen("KiB")))
1254 ctx
.reserve_size
= 1 << 10;
1255 else if (!strncmp(ptr
, "MiB", strlen("MiB")))
1256 ctx
.reserve_size
= 1 << 20;
1257 else if (!strncmp(ptr
, "GiB", strlen("GiB")))
1258 ctx
.reserve_size
= 1 << 30;
1260 panic("Syntax error in ring size param!\n");
1262 ctx
.reserve_size
*= strtoul(optarg
, NULL
, 0);
1265 cpu_tmp
= strtol(optarg
, NULL
, 0);
1267 cpu_affinity(cpu_tmp
);
1275 ctx
.pcap
= PCAP_OPS_RW
;
1279 ctx
.pcap
= PCAP_OPS_MM
;
1283 ctx
.pcap
= PCAP_OPS_SG
;
1290 ctx
.print_mode
= PRINT_NONE
;
1293 ctx
.print_mode
= PRINT_LESS
;
1297 (ctx
.print_mode
== PRINT_ASCII
) ?
1298 PRINT_HEX_ASCII
: PRINT_HEX
;
1302 (ctx
.print_mode
== PRINT_HEX
) ?
1303 PRINT_HEX_ASCII
: PRINT_ASCII
;
1306 ctx
.kpull
= strtoul(optarg
, NULL
, 0);
1309 frame_count_max
= strtoul(optarg
, NULL
, 0);
1313 for (j
= i
= strlen(optarg
); i
> 0; --i
) {
1314 if (!isdigit(optarg
[j
- i
]))
1319 if (!strncmp(ptr
, "KiB", strlen("KiB"))) {
1320 ctx
.dump_interval
= 1 << 10;
1321 ctx
.dump_mode
= DUMP_INTERVAL_SIZE
;
1322 } else if (!strncmp(ptr
, "MiB", strlen("MiB"))) {
1323 ctx
.dump_interval
= 1 << 20;
1324 ctx
.dump_mode
= DUMP_INTERVAL_SIZE
;
1325 } else if (!strncmp(ptr
, "GiB", strlen("GiB"))) {
1326 ctx
.dump_interval
= 1 << 30;
1327 ctx
.dump_mode
= DUMP_INTERVAL_SIZE
;
1328 } else if (!strncmp(ptr
, "sec", strlen("sec"))) {
1329 ctx
.dump_interval
= 1;
1330 ctx
.dump_mode
= DUMP_INTERVAL_TIME
;
1331 } else if (!strncmp(ptr
, "min", strlen("min"))) {
1332 ctx
.dump_interval
= 60;
1333 ctx
.dump_mode
= DUMP_INTERVAL_TIME
;
1334 } else if (!strncmp(ptr
, "hrs", strlen("hrs"))) {
1335 ctx
.dump_interval
= 60 * 60;
1336 ctx
.dump_mode
= DUMP_INTERVAL_TIME
;
1337 } else if (!strncmp(ptr
, "s", strlen("s"))) {
1338 ctx
.dump_interval
= 1;
1339 ctx
.dump_mode
= DUMP_INTERVAL_TIME
;
1341 panic("Syntax error in time/size param!\n");
1344 ctx
.dump_interval
*= strtoul(optarg
, NULL
, 0);
1350 ctx
.dump_bpf
= true;
1353 pcap_dump_type_features();
1383 panic("Option -%c requires an argument!\n",
1386 if (isprint(optopt
))
1387 printf("Unknown option character `0x%X\'!\n", optopt
);
1395 if (!ctx
.filter
&& optind
!= argc
) {
1399 for (i
= optind
; i
< argc
; ++i
) {
1400 size_t alen
= strlen(argv
[i
]) + 2;
1401 size_t flen
= ctx
.filter
? strlen(ctx
.filter
) : 0;
1403 ctx
.filter
= xrealloc(ctx
.filter
, 1, flen
+ alen
);
1404 ret
= slprintf(ctx
.filter
+ offset
, strlen(argv
[i
]) + 2, "%s ", argv
[i
]);
1406 panic("Cannot concatenate filter string!\n");
1413 ctx
.device_in
= xstrdup("any");
1415 register_signal(SIGINT
, signal_handler
);
1416 register_signal(SIGQUIT
, signal_handler
);
1417 register_signal(SIGTERM
, signal_handler
);
1418 register_signal(SIGHUP
, signal_handler
);
1424 set_sched_status(SCHED_FIFO
, sched_get_priority_max(SCHED_FIFO
));
1427 if (ctx
.device_in
&& (device_mtu(ctx
.device_in
) ||
1428 !strncmp("any", ctx
.device_in
, strlen(ctx
.device_in
)))) {
1430 ctx
.link_type
= pcap_devtype_to_linktype(ctx
.device_in
);
1431 if (!ctx
.device_out
) {
1433 main_loop
= recv_only_or_dump
;
1434 } else if (device_mtu(ctx
.device_out
)) {
1435 register_signal_f(SIGALRM
, timer_elapsed
, SA_SIGINFO
);
1436 main_loop
= receive_to_xmit
;
1439 register_signal_f(SIGALRM
, timer_next_dump
, SA_SIGINFO
);
1440 main_loop
= recv_only_or_dump
;
1442 ctx
.pcap
= PCAP_OPS_SG
;
1445 if (ctx
.device_out
&& device_mtu(ctx
.device_out
)) {
1446 register_signal_f(SIGALRM
, timer_elapsed
, SA_SIGINFO
);
1447 main_loop
= pcap_to_xmit
;
1449 ctx
.pcap
= PCAP_OPS_MM
;
1451 main_loop
= read_pcap
;
1453 ctx
.pcap
= PCAP_OPS_SG
;
1461 set_system_socket_memory(vals
, array_size(vals
));
1466 printf("pcap file I/O method: %s\n", pcap_ops_group_to_str
[ctx
.pcap
]);
1473 reset_system_socket_memory(vals
, array_size(vals
));
1476 device_restore_irq_affinity_list();