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>
43 #include "dissector.h"
46 extern int set_sockopt_hwtimestamp(int sock
, const char *dev
);
54 char *device_in
, *device_out
, *device_trans
, *filter
, *prefix
;
55 int cpu
, rfraw
, dump
, print_mode
, dump_dir
, packet_type
, verbose
;
56 unsigned long kpull
, dump_interval
, reserve_size
, tx_bytes
, tx_packets
;
57 bool randomize
, promiscuous
, enforce
, jumbo
, dump_bpf
;
58 enum pcap_ops_groups pcap
; enum dump_mode dump_mode
;
59 uid_t uid
; gid_t gid
; uint32_t link_type
, magic
;
62 static volatile sig_atomic_t sigint
= 0;
63 static volatile bool next_dump
= false;
65 static const char *short_options
= "d:i:o:rf:MJt:S:k:n:b:HQmcsqXlvhF:RGAP:Vu:g:T:DB";
66 static const struct option long_options
[] = {
67 {"dev", required_argument
, NULL
, 'd'},
68 {"in", required_argument
, NULL
, 'i'},
69 {"out", required_argument
, NULL
, 'o'},
70 {"filter", required_argument
, NULL
, 'f'},
71 {"num", required_argument
, NULL
, 'n'},
72 {"type", required_argument
, NULL
, 't'},
73 {"interval", required_argument
, NULL
, 'F'},
74 {"ring-size", required_argument
, NULL
, 'S'},
75 {"kernel-pull", required_argument
, NULL
, 'k'},
76 {"bind-cpu", required_argument
, NULL
, 'b'},
77 {"prefix", required_argument
, NULL
, 'P'},
78 {"user", required_argument
, NULL
, 'u'},
79 {"group", required_argument
, NULL
, 'g'},
80 {"magic", required_argument
, NULL
, 'T'},
81 {"rand", no_argument
, NULL
, 'r'},
82 {"rfraw", no_argument
, NULL
, 'R'},
83 {"mmap", no_argument
, NULL
, 'm'},
84 {"sg", no_argument
, NULL
, 'G'},
85 {"clrw", no_argument
, NULL
, 'c'},
86 {"jumbo-support", no_argument
, NULL
, 'J'},
87 {"no-promisc", no_argument
, NULL
, 'M'},
88 {"prio-high", no_argument
, NULL
, 'H'},
89 {"notouch-irq", no_argument
, NULL
, 'Q'},
90 {"dump-pcap-types", no_argument
, NULL
, 'D'},
91 {"dump-bpf", no_argument
, NULL
, 'B'},
92 {"silent", no_argument
, NULL
, 's'},
93 {"less", no_argument
, NULL
, 'q'},
94 {"hex", no_argument
, NULL
, 'X'},
95 {"ascii", no_argument
, NULL
, 'l'},
96 {"no-sock-mem", no_argument
, NULL
, 'A'},
97 {"update", no_argument
, NULL
, 'U'},
98 {"verbose", no_argument
, NULL
, 'V'},
99 {"version", no_argument
, NULL
, 'v'},
100 {"help", no_argument
, NULL
, 'h'},
105 static struct itimerval itimer
;
106 static unsigned long frame_count_max
= 0, interval
= TX_KERNEL_PULL_INT
;
108 #define __pcap_io pcap_ops[ctx->pcap]
110 static void signal_handler(int number
)
121 static void timer_elapsed(int unused __maybe_unused
)
125 set_itimer_interval_value(&itimer
, 0, interval
);
127 ret
= pull_and_flush_tx_ring(tx_sock
);
128 if (unlikely(ret
< 0)) {
129 /* We could hit EBADF if the socket has been closed before
130 * the timer was triggered.
132 if (errno
!= EBADF
&& errno
!= ENOBUFS
)
133 panic("Flushing TX_RING failed: %s!\n", strerror(errno
));
136 setitimer(ITIMER_REAL
, &itimer
, NULL
);
139 static void timer_purge(void)
143 ret
= pull_and_flush_tx_ring_wait(tx_sock
);
144 if (unlikely(ret
< 0)) {
145 if (errno
!= EBADF
&& errno
!= ENOBUFS
)
146 panic("Flushing TX_RING failed: %s!\n", strerror(errno
));
149 set_itimer_interval_value(&itimer
, 0, 0);
150 setitimer(ITIMER_REAL
, &itimer
, NULL
);
153 static void timer_next_dump(int unused __maybe_unused
)
155 set_itimer_interval_value(&itimer
, interval
, 0);
157 setitimer(ITIMER_REAL
, &itimer
, NULL
);
160 static inline bool dump_to_pcap(struct ctx
*ctx
)
165 static void pcap_to_xmit(struct ctx
*ctx
)
169 int irq
, ifindex
, fd
= 0, ret
;
170 unsigned int size
, it
= 0;
171 unsigned long trunced
= 0;
173 struct frame_map
*hdr
;
174 struct sock_fprog bpf_ops
;
175 struct timeval start
, end
, diff
;
178 if (!device_up_and_running(ctx
->device_out
) && !ctx
->rfraw
)
179 panic("Device not up and running!\n");
183 tx_sock
= pf_socket();
185 if (!strncmp("-", ctx
->device_in
, strlen("-"))) {
186 fd
= dup(fileno(stdin
));
187 close(fileno(stdin
));
188 if (ctx
->pcap
== PCAP_OPS_MM
)
189 ctx
->pcap
= PCAP_OPS_SG
;
191 fd
= open_or_die(ctx
->device_in
, O_RDONLY
| O_LARGEFILE
| O_NOATIME
);
194 if (__pcap_io
->init_once_pcap
)
195 __pcap_io
->init_once_pcap();
197 ret
= __pcap_io
->pull_fhdr_pcap(fd
, &ctx
->magic
, &ctx
->link_type
);
199 panic("Error reading pcap header!\n");
201 if (__pcap_io
->prepare_access_pcap
) {
202 ret
= __pcap_io
->prepare_access_pcap(fd
, PCAP_MODE_RD
, ctx
->jumbo
);
204 panic("Error prepare reading pcap!\n");
207 fmemset(&tx_ring
, 0, sizeof(tx_ring
));
208 fmemset(&bpf_ops
, 0, sizeof(bpf_ops
));
211 ctx
->device_trans
= xstrdup(ctx
->device_out
);
212 xfree(ctx
->device_out
);
214 enter_rfmon_mac80211(ctx
->device_trans
, &ctx
->device_out
);
215 if (ctx
->link_type
!= LINKTYPE_IEEE802_11
)
216 panic("Wrong linktype of pcap!\n");
219 ifindex
= device_ifindex(ctx
->device_out
);
221 size
= ring_size(ctx
->device_out
, ctx
->reserve_size
);
223 bpf_parse_rules(ctx
->filter
, &bpf_ops
, ctx
->link_type
);
225 bpf_dump_all(&bpf_ops
);
227 set_packet_loss_discard(tx_sock
);
229 setup_tx_ring_layout(tx_sock
, &tx_ring
, size
, ctx
->jumbo
);
230 create_tx_ring(tx_sock
, &tx_ring
, ctx
->verbose
);
231 mmap_tx_ring(tx_sock
, &tx_ring
);
232 alloc_tx_ring_frames(tx_sock
, &tx_ring
);
233 bind_tx_ring(tx_sock
, &tx_ring
, ifindex
);
235 dissector_init_all(ctx
->print_mode
);
237 if (ctx
->cpu
>= 0 && ifindex
> 0) {
238 irq
= device_irq_number(ctx
->device_out
);
239 device_set_irq_affinity(irq
, ctx
->cpu
);
242 printf("IRQ: %s:%d > CPU%d\n",
243 ctx
->device_out
, irq
, ctx
->cpu
);
247 interval
= ctx
->kpull
;
249 set_itimer_interval_value(&itimer
, 0, interval
);
250 setitimer(ITIMER_REAL
, &itimer
, NULL
);
252 drop_privileges(ctx
->enforce
, ctx
->uid
, ctx
->gid
);
254 printf("Running! Hang up with ^C!\n\n");
257 bug_on(gettimeofday(&start
, NULL
));
259 while (likely(sigint
== 0)) {
260 while (user_may_pull_from_tx(tx_ring
.frames
[it
].iov_base
)) {
261 hdr
= tx_ring
.frames
[it
].iov_base
;
262 out
= ((uint8_t *) hdr
) + TPACKET2_HDRLEN
- sizeof(struct sockaddr_ll
);
265 ret
= __pcap_io
->read_pcap(fd
, &phdr
, ctx
->magic
, out
,
266 ring_frame_size(&tx_ring
));
267 if (unlikely(ret
<= 0))
270 if (ring_frame_size(&tx_ring
) <
271 pcap_get_length(&phdr
, ctx
->magic
)) {
272 pcap_set_length(&phdr
, ctx
->magic
,
273 ring_frame_size(&tx_ring
));
276 } while (ctx
->filter
&&
277 !bpf_run_filter(&bpf_ops
, out
,
278 pcap_get_length(&phdr
, ctx
->magic
)));
280 pcap_pkthdr_to_tpacket_hdr(&phdr
, ctx
->magic
, &hdr
->tp_h
, &hdr
->s_ll
);
282 ctx
->tx_bytes
+= hdr
->tp_h
.tp_len
;;
285 show_frame_hdr(hdr
, ctx
->print_mode
);
287 dissector_entry_point(out
, hdr
->tp_h
.tp_snaplen
,
288 ctx
->link_type
, ctx
->print_mode
);
290 kernel_may_pull_from_tx(&hdr
->tp_h
);
293 if (it
>= tx_ring
.layout
.tp_frame_nr
)
296 if (unlikely(sigint
== 1))
299 if (frame_count_max
!= 0) {
300 if (ctx
->tx_packets
>= frame_count_max
) {
310 bug_on(gettimeofday(&end
, NULL
));
311 timersub(&end
, &start
, &diff
);
315 bpf_release(&bpf_ops
);
317 dissector_cleanup_all();
318 destroy_tx_ring(tx_sock
, &tx_ring
);
321 leave_rfmon_mac80211(ctx
->device_trans
, ctx
->device_out
);
323 if (__pcap_io
->prepare_close_pcap
)
324 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_RD
);
326 if (!strncmp("-", ctx
->device_in
, strlen("-")))
327 dup2(fd
, fileno(stdin
));
334 printf("\r%12lu packets outgoing\n", ctx
->tx_packets
);
335 printf("\r%12lu packets truncated in file\n", trunced
);
336 printf("\r%12lu bytes outgoing\n", ctx
->tx_bytes
);
337 printf("\r%12lu sec, %lu usec in total\n", diff
.tv_sec
, diff
.tv_usec
);
340 static void receive_to_xmit(struct ctx
*ctx
)
344 int rx_sock
, ifindex_in
, ifindex_out
;
345 unsigned int size_in
, size_out
, it_in
= 0, it_out
= 0;
346 unsigned long frame_count
= 0;
347 struct frame_map
*hdr_in
, *hdr_out
;
348 struct ring tx_ring
, rx_ring
;
349 struct pollfd rx_poll
;
350 struct sock_fprog bpf_ops
;
352 if (!strncmp(ctx
->device_in
, ctx
->device_out
, IFNAMSIZ
))
353 panic("Ingress/egress devices must be different!\n");
354 if (!device_up_and_running(ctx
->device_out
))
355 panic("Egress device not up and running!\n");
357 rx_sock
= pf_socket();
358 tx_sock
= pf_socket();
360 fmemset(&tx_ring
, 0, sizeof(tx_ring
));
361 fmemset(&rx_ring
, 0, sizeof(rx_ring
));
362 fmemset(&rx_poll
, 0, sizeof(rx_poll
));
363 fmemset(&bpf_ops
, 0, sizeof(bpf_ops
));
365 ifindex_in
= device_ifindex(ctx
->device_in
);
366 ifindex_out
= device_ifindex(ctx
->device_out
);
368 size_in
= ring_size(ctx
->device_in
, ctx
->reserve_size
);
369 size_out
= ring_size(ctx
->device_out
, ctx
->reserve_size
);
371 enable_kernel_bpf_jit_compiler();
373 bpf_parse_rules(ctx
->filter
, &bpf_ops
, ctx
->link_type
);
375 bpf_dump_all(&bpf_ops
);
376 bpf_attach_to_sock(rx_sock
, &bpf_ops
);
378 setup_rx_ring_layout(rx_sock
, &rx_ring
, size_in
, ctx
->jumbo
, false);
379 create_rx_ring(rx_sock
, &rx_ring
, ctx
->verbose
);
380 mmap_rx_ring(rx_sock
, &rx_ring
);
381 alloc_rx_ring_frames(rx_sock
, &rx_ring
);
382 bind_rx_ring(rx_sock
, &rx_ring
, ifindex_in
);
383 prepare_polling(rx_sock
, &rx_poll
);
385 set_packet_loss_discard(tx_sock
);
386 setup_tx_ring_layout(tx_sock
, &tx_ring
, size_out
, ctx
->jumbo
);
387 create_tx_ring(tx_sock
, &tx_ring
, ctx
->verbose
);
388 mmap_tx_ring(tx_sock
, &tx_ring
);
389 alloc_tx_ring_frames(tx_sock
, &tx_ring
);
390 bind_tx_ring(tx_sock
, &tx_ring
, ifindex_out
);
392 dissector_init_all(ctx
->print_mode
);
394 if (ctx
->promiscuous
)
395 ifflags
= enter_promiscuous_mode(ctx
->device_in
);
398 interval
= ctx
->kpull
;
400 set_itimer_interval_value(&itimer
, 0, interval
);
401 setitimer(ITIMER_REAL
, &itimer
, NULL
);
403 drop_privileges(ctx
->enforce
, ctx
->uid
, ctx
->gid
);
405 printf("Running! Hang up with ^C!\n\n");
408 while (likely(sigint
== 0)) {
409 while (user_may_pull_from_rx(rx_ring
.frames
[it_in
].iov_base
)) {
412 hdr_in
= rx_ring
.frames
[it_in
].iov_base
;
413 in
= ((uint8_t *) hdr_in
) + hdr_in
->tp_h
.tp_mac
;
417 if (ctx
->packet_type
!= -1)
418 if (ctx
->packet_type
!= hdr_in
->s_ll
.sll_pkttype
)
421 hdr_out
= tx_ring
.frames
[it_out
].iov_base
;
422 out
= ((uint8_t *) hdr_out
) + TPACKET2_HDRLEN
- sizeof(struct sockaddr_ll
);
424 for (; !user_may_pull_from_tx(tx_ring
.frames
[it_out
].iov_base
) &&
427 next_rnd_slot(&it_out
, &tx_ring
);
430 if (it_out
>= tx_ring
.layout
.tp_frame_nr
)
434 hdr_out
= tx_ring
.frames
[it_out
].iov_base
;
435 out
= ((uint8_t *) hdr_out
) + TPACKET2_HDRLEN
- sizeof(struct sockaddr_ll
);
438 tpacket_hdr_clone(&hdr_out
->tp_h
, &hdr_in
->tp_h
);
439 fmemcpy(out
, in
, hdr_in
->tp_h
.tp_len
);
441 kernel_may_pull_from_tx(&hdr_out
->tp_h
);
443 next_rnd_slot(&it_out
, &tx_ring
);
446 if (it_out
>= tx_ring
.layout
.tp_frame_nr
)
450 show_frame_hdr(hdr_in
, ctx
->print_mode
);
452 dissector_entry_point(in
, hdr_in
->tp_h
.tp_snaplen
,
453 ctx
->link_type
, ctx
->print_mode
);
455 if (frame_count_max
!= 0) {
456 if (frame_count
>= frame_count_max
) {
464 kernel_may_pull_from_rx(&hdr_in
->tp_h
);
467 if (it_in
>= rx_ring
.layout
.tp_frame_nr
)
470 if (unlikely(sigint
== 1))
474 poll(&rx_poll
, 1, -1);
481 sock_rx_net_stats(rx_sock
);
483 bpf_release(&bpf_ops
);
485 dissector_cleanup_all();
487 destroy_tx_ring(tx_sock
, &tx_ring
);
488 destroy_rx_ring(rx_sock
, &rx_ring
);
490 if (ctx
->promiscuous
)
491 leave_promiscuous_mode(ctx
->device_in
, ifflags
);
497 static void translate_pcap_to_txf(int fdo
, uint8_t *out
, size_t len
)
499 size_t bytes_done
= 0;
502 slprintf(bout
, sizeof(bout
), "{\n ");
503 write_or_die(fdo
, bout
, strlen(bout
));
505 while (bytes_done
< len
) {
506 slprintf(bout
, sizeof(bout
), "0x%02x, ", out
[bytes_done
]);
507 write_or_die(fdo
, bout
, strlen(bout
));
511 if (bytes_done
% 10 == 0) {
512 slprintf(bout
, sizeof(bout
), "\n");
513 write_or_die(fdo
, bout
, strlen(bout
));
515 if (bytes_done
< len
) {
516 slprintf(bout
, sizeof(bout
), " ");
517 write_or_die(fdo
, bout
, strlen(bout
));
521 if (bytes_done
% 10 != 0) {
522 slprintf(bout
, sizeof(bout
), "\n");
523 write_or_die(fdo
, bout
, strlen(bout
));
526 slprintf(bout
, sizeof(bout
), "}\n\n");
527 write_or_die(fdo
, bout
, strlen(bout
));
530 static void read_pcap(struct ctx
*ctx
)
534 int ret
, fd
, fdo
= 0;
535 unsigned long trunced
= 0;
538 struct sock_fprog bpf_ops
;
540 struct timeval start
, end
, diff
;
541 struct sockaddr_ll sll
;
545 if (!strncmp("-", ctx
->device_in
, strlen("-"))) {
546 fd
= dup(fileno(stdin
));
547 close(fileno(stdin
));
548 if (ctx
->pcap
== PCAP_OPS_MM
)
549 ctx
->pcap
= PCAP_OPS_SG
;
551 fd
= open_or_die(ctx
->device_in
, O_RDONLY
| O_LARGEFILE
| O_NOATIME
);
554 if (__pcap_io
->init_once_pcap
)
555 __pcap_io
->init_once_pcap();
557 ret
= __pcap_io
->pull_fhdr_pcap(fd
, &ctx
->magic
, &ctx
->link_type
);
559 panic("Error reading pcap header!\n");
561 if (__pcap_io
->prepare_access_pcap
) {
562 ret
= __pcap_io
->prepare_access_pcap(fd
, PCAP_MODE_RD
, ctx
->jumbo
);
564 panic("Error prepare reading pcap!\n");
567 fmemset(&fm
, 0, sizeof(fm
));
568 fmemset(&bpf_ops
, 0, sizeof(bpf_ops
));
570 bpf_parse_rules(ctx
->filter
, &bpf_ops
, ctx
->link_type
);
572 bpf_dump_all(&bpf_ops
);
574 dissector_init_all(ctx
->print_mode
);
576 out_len
= round_up(1024 * 1024, PAGE_SIZE
);
577 out
= xmalloc_aligned(out_len
, CO_CACHE_LINE_SIZE
);
579 if (ctx
->device_out
) {
580 if (!strncmp("-", ctx
->device_out
, strlen("-"))) {
581 fdo
= dup(fileno(stdout
));
582 close(fileno(stdout
));
584 fdo
= open_or_die_m(ctx
->device_out
, O_RDWR
| O_CREAT
|
585 O_TRUNC
| O_LARGEFILE
, DEFFILEMODE
);
589 drop_privileges(ctx
->enforce
, ctx
->uid
, ctx
->gid
);
591 printf("Running! Hang up with ^C!\n\n");
594 bug_on(gettimeofday(&start
, NULL
));
596 while (likely(sigint
== 0)) {
598 ret
= __pcap_io
->read_pcap(fd
, &phdr
, ctx
->magic
,
600 if (unlikely(ret
< 0))
603 if (unlikely(pcap_get_length(&phdr
, ctx
->magic
) == 0)) {
608 if (unlikely(pcap_get_length(&phdr
, ctx
->magic
) > out_len
)) {
609 pcap_set_length(&phdr
, ctx
->magic
, out_len
);
612 } while (ctx
->filter
&&
613 !bpf_run_filter(&bpf_ops
, out
,
614 pcap_get_length(&phdr
, ctx
->magic
)));
616 pcap_pkthdr_to_tpacket_hdr(&phdr
, ctx
->magic
, &fm
.tp_h
, &sll
);
618 ctx
->tx_bytes
+= fm
.tp_h
.tp_len
;
621 show_frame_hdr(&fm
, ctx
->print_mode
);
623 dissector_entry_point(out
, fm
.tp_h
.tp_snaplen
,
624 ctx
->link_type
, ctx
->print_mode
);
627 translate_pcap_to_txf(fdo
, out
, fm
.tp_h
.tp_snaplen
);
629 if (frame_count_max
!= 0) {
630 if (ctx
->tx_packets
>= frame_count_max
) {
639 bug_on(gettimeofday(&end
, NULL
));
640 timersub(&end
, &start
, &diff
);
642 bpf_release(&bpf_ops
);
644 dissector_cleanup_all();
646 if (__pcap_io
->prepare_close_pcap
)
647 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_RD
);
653 printf("\r%12lu packets outgoing\n", ctx
->tx_packets
);
654 printf("\r%12lu packets truncated in file\n", trunced
);
655 printf("\r%12lu bytes outgoing\n", ctx
->tx_bytes
);
656 printf("\r%12lu sec, %lu usec in total\n", diff
.tv_sec
, diff
.tv_usec
);
658 if (!strncmp("-", ctx
->device_in
, strlen("-")))
659 dup2(fd
, fileno(stdin
));
662 if (ctx
->device_out
) {
663 if (!strncmp("-", ctx
->device_out
, strlen("-")))
664 dup2(fdo
, fileno(stdout
));
669 static void finish_multi_pcap_file(struct ctx
*ctx
, int fd
)
671 __pcap_io
->fsync_pcap(fd
);
673 if (__pcap_io
->prepare_close_pcap
)
674 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_WR
);
678 fmemset(&itimer
, 0, sizeof(itimer
));
679 setitimer(ITIMER_REAL
, &itimer
, NULL
);
682 static int next_multi_pcap_file(struct ctx
*ctx
, int fd
)
687 __pcap_io
->fsync_pcap(fd
);
689 if (__pcap_io
->prepare_close_pcap
)
690 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_WR
);
694 slprintf(fname
, sizeof(fname
), "%s/%s%lu.pcap", ctx
->device_out
,
695 ctx
->prefix
? : "dump-", time(0));
697 fd
= open_or_die_m(fname
, O_RDWR
| O_CREAT
| O_TRUNC
|
698 O_LARGEFILE
, DEFFILEMODE
);
700 ret
= __pcap_io
->push_fhdr_pcap(fd
, ctx
->magic
, ctx
->link_type
);
702 panic("Error writing pcap header!\n");
704 if (__pcap_io
->prepare_access_pcap
) {
705 ret
= __pcap_io
->prepare_access_pcap(fd
, PCAP_MODE_WR
, true);
707 panic("Error prepare writing pcap!\n");
713 static int begin_multi_pcap_file(struct ctx
*ctx
)
720 if (ctx
->device_out
[strlen(ctx
->device_out
) - 1] == '/')
721 ctx
->device_out
[strlen(ctx
->device_out
) - 1] = 0;
723 slprintf(fname
, sizeof(fname
), "%s/%s%lu.pcap", ctx
->device_out
,
724 ctx
->prefix
? : "dump-", time(0));
726 fd
= open_or_die_m(fname
, O_RDWR
| O_CREAT
| O_TRUNC
|
727 O_LARGEFILE
, DEFFILEMODE
);
729 ret
= __pcap_io
->push_fhdr_pcap(fd
, ctx
->magic
, ctx
->link_type
);
731 panic("Error writing pcap header!\n");
733 if (__pcap_io
->prepare_access_pcap
) {
734 ret
= __pcap_io
->prepare_access_pcap(fd
, PCAP_MODE_WR
, true);
736 panic("Error prepare writing pcap!\n");
739 if (ctx
->dump_mode
== DUMP_INTERVAL_TIME
) {
740 interval
= ctx
->dump_interval
;
742 set_itimer_interval_value(&itimer
, interval
, 0);
743 setitimer(ITIMER_REAL
, &itimer
, NULL
);
751 static void finish_single_pcap_file(struct ctx
*ctx
, int fd
)
753 __pcap_io
->fsync_pcap(fd
);
755 if (__pcap_io
->prepare_close_pcap
)
756 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_WR
);
758 if (strncmp("-", ctx
->device_out
, strlen("-")))
761 dup2(fd
, fileno(stdout
));
764 static int begin_single_pcap_file(struct ctx
*ctx
)
770 if (!strncmp("-", ctx
->device_out
, strlen("-"))) {
771 fd
= dup(fileno(stdout
));
772 close(fileno(stdout
));
773 if (ctx
->pcap
== PCAP_OPS_MM
)
774 ctx
->pcap
= PCAP_OPS_SG
;
776 fd
= open_or_die_m(ctx
->device_out
,
777 O_RDWR
| O_CREAT
| O_TRUNC
|
778 O_LARGEFILE
, DEFFILEMODE
);
781 ret
= __pcap_io
->push_fhdr_pcap(fd
, ctx
->magic
, ctx
->link_type
);
783 panic("Error writing pcap header!\n");
785 if (__pcap_io
->prepare_access_pcap
) {
786 ret
= __pcap_io
->prepare_access_pcap(fd
, PCAP_MODE_WR
, true);
788 panic("Error prepare writing pcap!\n");
794 static void print_pcap_file_stats(int sock
, struct ctx
*ctx
)
797 struct tpacket_stats kstats
;
798 socklen_t slen
= sizeof(kstats
);
800 fmemset(&kstats
, 0, sizeof(kstats
));
802 ret
= getsockopt(sock
, SOL_PACKET
, PACKET_STATISTICS
, &kstats
, &slen
);
804 panic("Cannot get packet statistics!\n");
806 if (ctx
->print_mode
== PRINT_NONE
) {
807 printf(".(+%u/-%u)", kstats
.tp_packets
- kstats
.tp_drops
,
813 static void walk_t3_block(struct block_desc
*pbd
, struct ctx
*ctx
,
814 int sock
, int fd
, unsigned long *frame_count
)
817 int num_pkts
= pbd
->h1
.num_pkts
, i
, ret
;
818 struct tpacket3_hdr
*hdr
;
820 struct sockaddr_ll
*sll
;
822 hdr
= (void *) ((uint8_t *) pbd
+ pbd
->h1
.offset_to_first_pkt
);
823 sll
= (void *) ((uint8_t *) hdr
+ TPACKET_ALIGN(sizeof(*hdr
)));
825 for (i
= 0; i
< num_pkts
&& likely(sigint
== 0); ++i
) {
827 packet
= ((uint8_t *) hdr
+ hdr
->tp_mac
);
829 if (ctx
->packet_type
!= -1)
830 if (ctx
->packet_type
!= sll
->sll_pkttype
)
835 if (dump_to_pcap(ctx
)) {
836 tpacket3_hdr_to_pcap_pkthdr(hdr
, sll
, &phdr
, ctx
->magic
);
838 ret
= __pcap_io
->write_pcap(fd
, &phdr
, ctx
->magic
, packet
,
839 pcap_get_length(&phdr
, ctx
->magic
));
840 if (unlikely(ret
!= pcap_get_total_length(&phdr
, ctx
->magic
)))
841 panic("Write error to pcap!\n");
844 __show_frame_hdr(sll
, hdr
, ctx
->print_mode
, true);
846 dissector_entry_point(packet
, hdr
->tp_snaplen
, ctx
->link_type
,
850 hdr
= (void *) ((uint8_t *) hdr
+ hdr
->tp_next_offset
);
851 sll
= (void *) ((uint8_t *) hdr
+ TPACKET_ALIGN(sizeof(*hdr
)));
853 if (frame_count_max
!= 0) {
854 if (unlikely(*frame_count
>= frame_count_max
)) {
860 if (dump_to_pcap(ctx
)) {
861 if (ctx
->dump_mode
== DUMP_INTERVAL_SIZE
) {
862 interval
+= hdr
->tp_snaplen
;
863 if (interval
> ctx
->dump_interval
) {
870 fd
= next_multi_pcap_file(ctx
, fd
);
873 if (unlikely(ctx
->verbose
))
874 print_pcap_file_stats(sock
, ctx
);
880 static void recv_only_or_dump(struct ctx
*ctx
)
883 int sock
, irq
, ifindex
, fd
= 0, ret
;
884 unsigned int size
, it
= 0;
886 struct pollfd rx_poll
;
887 struct sock_fprog bpf_ops
;
888 struct timeval start
, end
, diff
;
889 struct block_desc
*pbd
;
890 unsigned long frame_count
= 0;
895 ctx
->device_trans
= xstrdup(ctx
->device_in
);
896 xfree(ctx
->device_in
);
898 enter_rfmon_mac80211(ctx
->device_trans
, &ctx
->device_in
);
899 ctx
->link_type
= LINKTYPE_IEEE802_11
;
902 fmemset(&rx_ring
, 0, sizeof(rx_ring
));
903 fmemset(&rx_poll
, 0, sizeof(rx_poll
));
904 fmemset(&bpf_ops
, 0, sizeof(bpf_ops
));
906 ifindex
= device_ifindex(ctx
->device_in
);
908 size
= ring_size(ctx
->device_in
, ctx
->reserve_size
);
910 enable_kernel_bpf_jit_compiler();
912 bpf_parse_rules(ctx
->filter
, &bpf_ops
, ctx
->link_type
);
914 bpf_dump_all(&bpf_ops
);
915 bpf_attach_to_sock(sock
, &bpf_ops
);
917 set_sockopt_hwtimestamp(sock
, ctx
->device_in
);
919 setup_rx_ring_layout(sock
, &rx_ring
, size
, true, true);
920 create_rx_ring(sock
, &rx_ring
, ctx
->verbose
);
921 mmap_rx_ring(sock
, &rx_ring
);
922 alloc_rx_ring_frames(sock
, &rx_ring
);
923 bind_rx_ring(sock
, &rx_ring
, ifindex
);
925 prepare_polling(sock
, &rx_poll
);
926 dissector_init_all(ctx
->print_mode
);
928 if (ctx
->cpu
>= 0 && ifindex
> 0) {
929 irq
= device_irq_number(ctx
->device_in
);
930 device_set_irq_affinity(irq
, ctx
->cpu
);
933 printf("IRQ: %s:%d > CPU%d\n",
934 ctx
->device_in
, irq
, ctx
->cpu
);
937 if (ctx
->promiscuous
)
938 ifflags
= enter_promiscuous_mode(ctx
->device_in
);
940 if (dump_to_pcap(ctx
) && __pcap_io
->init_once_pcap
)
941 __pcap_io
->init_once_pcap();
943 drop_privileges(ctx
->enforce
, ctx
->uid
, ctx
->gid
);
945 if (dump_to_pcap(ctx
)) {
949 fmemset(&stats
, 0, sizeof(stats
));
950 ret
= stat(ctx
->device_out
, &stats
);
956 ctx
->dump_dir
= S_ISDIR(stats
.st_mode
);
958 fd
= begin_multi_pcap_file(ctx
);
961 fd
= begin_single_pcap_file(ctx
);
965 printf("Running! Hang up with ^C!\n\n");
968 bug_on(gettimeofday(&start
, NULL
));
970 while (likely(sigint
== 0)) {
971 while (user_may_pull_from_rx_block((pbd
= (void *)
972 rx_ring
.frames
[it
].iov_base
))) {
973 walk_t3_block(pbd
, ctx
, sock
, fd
, &frame_count
);
975 kernel_may_pull_from_rx_block(pbd
);
976 it
= (it
+ 1) % rx_ring
.layout3
.tp_block_nr
;
978 if (unlikely(sigint
== 1))
982 poll(&rx_poll
, 1, -1);
985 bug_on(gettimeofday(&end
, NULL
));
986 timersub(&end
, &start
, &diff
);
988 if (!(ctx
->dump_dir
&& ctx
->print_mode
== PRINT_NONE
)) {
989 sock_rx_net_stats(sock
);
991 printf("\r%12lu sec, %lu usec in total\n",
992 diff
.tv_sec
, diff
.tv_usec
);
998 bpf_release(&bpf_ops
);
999 dissector_cleanup_all();
1000 destroy_rx_ring(sock
, &rx_ring
);
1002 if (ctx
->promiscuous
)
1003 leave_promiscuous_mode(ctx
->device_in
, ifflags
);
1006 leave_rfmon_mac80211(ctx
->device_trans
, ctx
->device_in
);
1008 if (dump_to_pcap(ctx
)) {
1010 finish_multi_pcap_file(ctx
, fd
);
1012 finish_single_pcap_file(ctx
, fd
);
1018 static void __noreturn
help(void)
1020 printf("\nnetsniff-ng %s, the packet sniffing beast\n", VERSION_STRING
);
1021 puts("http://www.netsniff-ng.org\n\n"
1022 "Usage: netsniff-ng [options] [filter-expression]\n"
1024 " -i|-d|--dev|--in <dev|pcap|-> Input source as netdev, pcap or pcap stdin\n"
1025 " -o|--out <dev|pcap|dir|cfg|-> Output sink as netdev, pcap, directory, trafgen, or stdout\n"
1026 " -f|--filter <bpf-file|expr> Use BPF filter file from bpfc or tcpdump-like expression\n"
1027 " -t|--type <type> Filter for: host|broadcast|multicast|others|outgoing\n"
1028 " -F|--interval <size|time> Dump interval if -o is a dir: <num>KiB/MiB/GiB/s/sec/min/hrs\n"
1029 " -R|--rfraw Capture or inject raw 802.11 frames\n"
1030 " -n|--num <0|uint> Number of packets until exit (def: 0)\n"
1031 " -P|--prefix <name> Prefix for pcaps stored in directory\n"
1032 " -T|--magic <pcap-magic> Pcap magic number/pcap format to store, see -D\n"
1033 " -D|--dump-pcap-types Dump pcap types and magic numbers and quit\n"
1034 " -B|--dump-bpf Dump generated BPF assembly\n"
1035 " -r|--rand Randomize packet forwarding order (dev->dev)\n"
1036 " -M|--no-promisc No promiscuous mode for netdev\n"
1037 " -A|--no-sock-mem Don't tune core socket memory\n"
1038 " -m|--mmap Mmap(2) pcap file i.e., for replaying pcaps\n"
1039 " -G|--sg Scatter/gather pcap file I/O\n"
1040 " -c|--clrw Use slower read(2)/write(2) I/O\n"
1041 " -S|--ring-size <size> Specify ring size to: <num>KiB/MiB/GiB\n"
1042 " -k|--kernel-pull <uint> Kernel pull from user interval in us (def: 10us)\n"
1043 " -J|--jumbo-support Support replay/fwd 64KB Super Jumbo Frames (def: 2048B)\n"
1044 " -b|--bind-cpu <cpu> Bind to specific CPU\n"
1045 " -u|--user <userid> Drop privileges and change to userid\n"
1046 " -g|--group <groupid> Drop privileges and change to groupid\n"
1047 " -H|--prio-high Make this high priority process\n"
1048 " -Q|--notouch-irq Do not touch IRQ CPU affinity of NIC\n"
1049 " -s|--silent Do not print captured packets\n"
1050 " -q|--less Print less-verbose packet information\n"
1051 " -X|--hex Print packet data in hex format\n"
1052 " -l|--ascii Print human-readable packet data\n"
1053 " -U|--update Update GeoIP databases\n"
1054 " -V|--verbose Be more verbose\n"
1055 " -v|--version Show version and exit\n"
1056 " -h|--help Guess what?!\n\n"
1058 " netsniff-ng --in eth0 --out dump.pcap -s -T 0xa1b2c3d4 --b 0 tcp or udp\n"
1059 " netsniff-ng --in wlan0 --rfraw --out dump.pcap --silent --bind-cpu 0\n"
1060 " netsniff-ng --in dump.pcap --mmap --out eth0 -k1000 --silent --bind-cpu 0\n"
1061 " netsniff-ng --in dump.pcap --out dump.cfg --silent --bind-cpu 0\n"
1062 " netsniff-ng --in eth0 --out eth1 --silent --bind-cpu 0 -J --type host\n"
1063 " netsniff-ng --in eth1 --out /opt/probe/ -s -m --interval 100MiB -b 0\n"
1064 " netsniff-ng --in vlan0 --out dump.pcap -c -u `id -u bob` -g `id -g bob`\n"
1065 " netsniff-ng --in any --filter http.bpf --jumbo-support --ascii -V\n\n"
1067 " For introducing bit errors, delays with random variation and more\n"
1068 " while replaying pcaps, make use of tc(8) with its disciplines (e.g. netem).\n\n"
1069 "Please report bugs to <bugs@netsniff-ng.org>\n"
1070 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
1071 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel.roullit@gmail.com>\n"
1072 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1073 "Swiss federal institute of technology (ETH Zurich)\n"
1074 "License: GNU GPL version 2.0\n"
1075 "This is free software: you are free to change and redistribute it.\n"
1076 "There is NO WARRANTY, to the extent permitted by law.\n");
1080 static void __noreturn
version(void)
1082 printf("\nnetsniff-ng %s, the packet sniffing beast\n", VERSION_LONG
);
1083 puts("http://www.netsniff-ng.org\n\n"
1084 "Please report bugs to <bugs@netsniff-ng.org>\n"
1085 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
1086 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel.roullit@gmail.com>\n"
1087 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1088 "Swiss federal institute of technology (ETH Zurich)\n"
1089 "License: GNU GPL version 2.0\n"
1090 "This is free software: you are free to change and redistribute it.\n"
1091 "There is NO WARRANTY, to the extent permitted by law.\n");
1095 int main(int argc
, char **argv
)
1098 int c
, i
, j
, cpu_tmp
, opt_index
, ops_touched
= 0, vals
[4] = {0};
1099 bool prio_high
= false, setsockmem
= true;
1100 void (*main_loop
)(struct ctx
*ctx
) = NULL
;
1102 .link_type
= LINKTYPE_EN10MB
,
1103 .print_mode
= PRINT_NORM
,
1106 .promiscuous
= true,
1108 .pcap
= PCAP_OPS_SG
,
1109 .dump_interval
= 60,
1110 .dump_mode
= DUMP_INTERVAL_TIME
,
1113 .magic
= ORIGINAL_TCPDUMP_MAGIC
,
1118 while ((c
= getopt_long(argc
, argv
, short_options
, long_options
,
1119 &opt_index
)) != EOF
) {
1123 ctx
.device_in
= xstrdup(optarg
);
1126 ctx
.device_out
= xstrdup(optarg
);
1129 ctx
.prefix
= xstrdup(optarg
);
1132 ctx
.link_type
= LINKTYPE_IEEE802_11
;
1136 ctx
.randomize
= true;
1142 ctx
.magic
= (uint32_t) strtoul(optarg
, NULL
, 0);
1143 pcap_check_magic(ctx
.magic
);
1146 ctx
.filter
= xstrdup(optarg
);
1149 ctx
.promiscuous
= false;
1155 ctx
.uid
= strtoul(optarg
, NULL
, 0);
1159 ctx
.gid
= strtoul(optarg
, NULL
, 0);
1163 if (!strncmp(optarg
, "host", strlen("host")))
1164 ctx
.packet_type
= PACKET_HOST
;
1165 else if (!strncmp(optarg
, "broadcast", strlen("broadcast")))
1166 ctx
.packet_type
= PACKET_BROADCAST
;
1167 else if (!strncmp(optarg
, "multicast", strlen("multicast")))
1168 ctx
.packet_type
= PACKET_MULTICAST
;
1169 else if (!strncmp(optarg
, "others", strlen("others")))
1170 ctx
.packet_type
= PACKET_OTHERHOST
;
1171 else if (!strncmp(optarg
, "outgoing", strlen("outgoing")))
1172 ctx
.packet_type
= PACKET_OUTGOING
;
1174 ctx
.packet_type
= -1;
1178 ctx
.reserve_size
= 0;
1180 for (j
= i
= strlen(optarg
); i
> 0; --i
) {
1181 if (!isdigit(optarg
[j
- i
]))
1186 if (!strncmp(ptr
, "KiB", strlen("KiB")))
1187 ctx
.reserve_size
= 1 << 10;
1188 else if (!strncmp(ptr
, "MiB", strlen("MiB")))
1189 ctx
.reserve_size
= 1 << 20;
1190 else if (!strncmp(ptr
, "GiB", strlen("GiB")))
1191 ctx
.reserve_size
= 1 << 30;
1193 panic("Syntax error in ring size param!\n");
1196 ctx
.reserve_size
*= strtol(optarg
, NULL
, 0);
1199 cpu_tmp
= strtol(optarg
, NULL
, 0);
1201 cpu_affinity(cpu_tmp
);
1209 ctx
.pcap
= PCAP_OPS_RW
;
1213 ctx
.pcap
= PCAP_OPS_MM
;
1217 ctx
.pcap
= PCAP_OPS_SG
;
1224 ctx
.print_mode
= PRINT_NONE
;
1227 ctx
.print_mode
= PRINT_LESS
;
1231 (ctx
.print_mode
== PRINT_ASCII
) ?
1232 PRINT_HEX_ASCII
: PRINT_HEX
;
1236 (ctx
.print_mode
== PRINT_HEX
) ?
1237 PRINT_HEX_ASCII
: PRINT_ASCII
;
1240 ctx
.kpull
= strtol(optarg
, NULL
, 0);
1243 frame_count_max
= strtol(optarg
, NULL
, 0);
1247 ctx
.dump_interval
= 0;
1249 for (j
= i
= strlen(optarg
); i
> 0; --i
) {
1250 if (!isdigit(optarg
[j
- i
]))
1255 if (!strncmp(ptr
, "KiB", strlen("KiB"))) {
1256 ctx
.dump_interval
= 1 << 10;
1257 ctx
.dump_mode
= DUMP_INTERVAL_SIZE
;
1258 } else if (!strncmp(ptr
, "MiB", strlen("MiB"))) {
1259 ctx
.dump_interval
= 1 << 20;
1260 ctx
.dump_mode
= DUMP_INTERVAL_SIZE
;
1261 } else if (!strncmp(ptr
, "GiB", strlen("GiB"))) {
1262 ctx
.dump_interval
= 1 << 30;
1263 ctx
.dump_mode
= DUMP_INTERVAL_SIZE
;
1264 } else if (!strncmp(ptr
, "sec", strlen("sec"))) {
1265 ctx
.dump_interval
= 1;
1266 ctx
.dump_mode
= DUMP_INTERVAL_TIME
;
1267 } else if (!strncmp(ptr
, "min", strlen("min"))) {
1268 ctx
.dump_interval
= 60;
1269 ctx
.dump_mode
= DUMP_INTERVAL_TIME
;
1270 } else if (!strncmp(ptr
, "hrs", strlen("hrs"))) {
1271 ctx
.dump_interval
= 60 * 60;
1272 ctx
.dump_mode
= DUMP_INTERVAL_TIME
;
1273 } else if (!strncmp(ptr
, "s", strlen("s"))) {
1274 ctx
.dump_interval
= 1;
1275 ctx
.dump_mode
= DUMP_INTERVAL_TIME
;
1277 panic("Syntax error in time/size param!\n");
1281 ctx
.dump_interval
*= strtol(optarg
, NULL
, 0);
1287 ctx
.dump_bpf
= true;
1290 pcap_dump_type_features();
1320 panic("Option -%c requires an argument!\n",
1323 if (isprint(optopt
))
1324 printf("Unknown option character `0x%X\'!\n", optopt
);
1332 if (!ctx
.filter
&& optind
!= argc
) {
1336 for (i
= optind
; i
< argc
; ++i
) {
1337 size_t alen
= strlen(argv
[i
]) + 2;
1338 size_t flen
= ctx
.filter
? strlen(ctx
.filter
) : 0;
1340 ctx
.filter
= xrealloc(ctx
.filter
, 1, flen
+ alen
);
1341 ret
= slprintf(ctx
.filter
+ offset
, strlen(argv
[i
]) + 2, "%s ", argv
[i
]);
1343 panic("Cannot concatenate filter string!\n");
1350 ctx
.device_in
= xstrdup("any");
1352 register_signal(SIGINT
, signal_handler
);
1353 register_signal(SIGHUP
, signal_handler
);
1359 set_sched_status(SCHED_FIFO
, sched_get_priority_max(SCHED_FIFO
));
1362 if (ctx
.device_in
&& (device_mtu(ctx
.device_in
) ||
1363 !strncmp("any", ctx
.device_in
, strlen(ctx
.device_in
)))) {
1364 if (!ctx
.device_out
) {
1366 main_loop
= recv_only_or_dump
;
1367 } else if (device_mtu(ctx
.device_out
)) {
1368 register_signal_f(SIGALRM
, timer_elapsed
, SA_SIGINFO
);
1369 main_loop
= receive_to_xmit
;
1372 register_signal_f(SIGALRM
, timer_next_dump
, SA_SIGINFO
);
1373 main_loop
= recv_only_or_dump
;
1375 ctx
.pcap
= PCAP_OPS_SG
;
1378 if (ctx
.device_out
&& device_mtu(ctx
.device_out
)) {
1379 register_signal_f(SIGALRM
, timer_elapsed
, SA_SIGINFO
);
1380 main_loop
= pcap_to_xmit
;
1382 ctx
.pcap
= PCAP_OPS_MM
;
1384 main_loop
= read_pcap
;
1386 ctx
.pcap
= PCAP_OPS_SG
;
1394 set_system_socket_memory(vals
, array_size(vals
));
1403 reset_system_socket_memory(vals
, array_size(vals
));
1405 device_restore_irq_affinity_list();
1408 free(ctx
.device_in
);
1409 free(ctx
.device_out
);
1410 free(ctx
.device_trans
);