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 __maybe_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
);
150 static void timer_next_dump(int unused __maybe_unused
)
152 set_itimer_interval_value(&itimer
, interval
, 0);
154 setitimer(ITIMER_REAL
, &itimer
, NULL
);
157 static inline bool dump_to_pcap(struct ctx
*ctx
)
162 static void pcap_to_xmit(struct ctx
*ctx
)
166 int irq
, ifindex
, fd
= 0, ret
;
167 unsigned int size
, it
= 0;
168 unsigned long trunced
= 0;
170 struct frame_map
*hdr
;
171 struct sock_fprog bpf_ops
;
172 struct timeval start
, end
, diff
;
175 if (!device_up_and_running(ctx
->device_out
) && !ctx
->rfraw
)
176 panic("Device not up and running!\n");
180 tx_sock
= pf_socket();
182 if (!strncmp("-", ctx
->device_in
, strlen("-"))) {
183 fd
= dup(fileno(stdin
));
184 close(fileno(stdin
));
185 if (ctx
->pcap
== PCAP_OPS_MM
)
186 ctx
->pcap
= PCAP_OPS_SG
;
188 fd
= open_or_die(ctx
->device_in
, O_RDONLY
| O_LARGEFILE
| O_NOATIME
);
191 if (__pcap_io
->init_once_pcap
)
192 __pcap_io
->init_once_pcap();
194 ret
= __pcap_io
->pull_fhdr_pcap(fd
, &ctx
->magic
, &ctx
->link_type
);
196 panic("Error reading pcap header!\n");
198 if (__pcap_io
->prepare_access_pcap
) {
199 ret
= __pcap_io
->prepare_access_pcap(fd
, PCAP_MODE_RD
, ctx
->jumbo
);
201 panic("Error prepare reading pcap!\n");
204 fmemset(&tx_ring
, 0, sizeof(tx_ring
));
205 fmemset(&bpf_ops
, 0, sizeof(bpf_ops
));
208 ctx
->device_trans
= xstrdup(ctx
->device_out
);
209 xfree(ctx
->device_out
);
211 enter_rfmon_mac80211(ctx
->device_trans
, &ctx
->device_out
);
212 if (ctx
->link_type
!= LINKTYPE_IEEE802_11
)
213 panic("Wrong linktype of pcap!\n");
216 ifindex
= device_ifindex(ctx
->device_out
);
218 size
= ring_size(ctx
->device_out
, ctx
->reserve_size
);
220 bpf_parse_rules(ctx
->filter
, &bpf_ops
, ctx
->link_type
);
222 bpf_dump_all(&bpf_ops
);
224 set_packet_loss_discard(tx_sock
);
226 setup_tx_ring_layout(tx_sock
, &tx_ring
, size
, ctx
->jumbo
);
227 create_tx_ring(tx_sock
, &tx_ring
, ctx
->verbose
);
228 mmap_tx_ring(tx_sock
, &tx_ring
);
229 alloc_tx_ring_frames(tx_sock
, &tx_ring
);
230 bind_tx_ring(tx_sock
, &tx_ring
, ifindex
);
232 dissector_init_all(ctx
->print_mode
);
234 if (ctx
->cpu
>= 0 && ifindex
> 0) {
235 irq
= device_irq_number(ctx
->device_out
);
236 device_bind_irq_to_cpu(irq
, ctx
->cpu
);
239 printf("IRQ: %s:%d > CPU%d\n",
240 ctx
->device_out
, irq
, ctx
->cpu
);
244 interval
= ctx
->kpull
;
246 set_itimer_interval_value(&itimer
, 0, interval
);
247 setitimer(ITIMER_REAL
, &itimer
, NULL
);
249 drop_privileges(ctx
->enforce
, ctx
->uid
, ctx
->gid
);
251 printf("Running! Hang up with ^C!\n\n");
254 bug_on(gettimeofday(&start
, NULL
));
256 while (likely(sigint
== 0)) {
257 while (user_may_pull_from_tx(tx_ring
.frames
[it
].iov_base
)) {
258 hdr
= tx_ring
.frames
[it
].iov_base
;
259 out
= ((uint8_t *) hdr
) + TPACKET2_HDRLEN
- sizeof(struct sockaddr_ll
);
262 ret
= __pcap_io
->read_pcap(fd
, &phdr
, ctx
->magic
, out
,
263 ring_frame_size(&tx_ring
));
264 if (unlikely(ret
<= 0))
267 if (ring_frame_size(&tx_ring
) <
268 pcap_get_length(&phdr
, ctx
->magic
)) {
269 pcap_set_length(&phdr
, ctx
->magic
,
270 ring_frame_size(&tx_ring
));
273 } while (ctx
->filter
&&
274 !bpf_run_filter(&bpf_ops
, out
,
275 pcap_get_length(&phdr
, ctx
->magic
)));
277 pcap_pkthdr_to_tpacket_hdr(&phdr
, ctx
->magic
, &hdr
->tp_h
, &hdr
->s_ll
);
279 ctx
->tx_bytes
+= hdr
->tp_h
.tp_len
;;
282 show_frame_hdr(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
) {
307 bug_on(gettimeofday(&end
, NULL
));
308 timersub(&end
, &start
, &diff
);
312 bpf_release(&bpf_ops
);
314 dissector_cleanup_all();
315 destroy_tx_ring(tx_sock
, &tx_ring
);
318 leave_rfmon_mac80211(ctx
->device_trans
, ctx
->device_out
);
320 if (__pcap_io
->prepare_close_pcap
)
321 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_RD
);
323 if (!strncmp("-", ctx
->device_in
, strlen("-")))
324 dup2(fd
, fileno(stdin
));
331 printf("\r%12lu packets outgoing\n", ctx
->tx_packets
);
332 printf("\r%12lu packets truncated in file\n", trunced
);
333 printf("\r%12lu bytes outgoing\n", ctx
->tx_bytes
);
334 printf("\r%12lu sec, %lu usec in total\n", diff
.tv_sec
, diff
.tv_usec
);
337 static void receive_to_xmit(struct ctx
*ctx
)
341 int rx_sock
, ifindex_in
, ifindex_out
;
342 unsigned int size_in
, size_out
, 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 fmemset(&tx_ring
, 0, sizeof(tx_ring
));
358 fmemset(&rx_ring
, 0, sizeof(rx_ring
));
359 fmemset(&rx_poll
, 0, sizeof(rx_poll
));
360 fmemset(&bpf_ops
, 0, sizeof(bpf_ops
));
362 ifindex_in
= device_ifindex(ctx
->device_in
);
363 ifindex_out
= device_ifindex(ctx
->device_out
);
365 size_in
= ring_size(ctx
->device_in
, ctx
->reserve_size
);
366 size_out
= ring_size(ctx
->device_out
, ctx
->reserve_size
);
368 enable_kernel_bpf_jit_compiler();
370 bpf_parse_rules(ctx
->filter
, &bpf_ops
, ctx
->link_type
);
372 bpf_dump_all(&bpf_ops
);
373 bpf_attach_to_sock(rx_sock
, &bpf_ops
);
375 setup_rx_ring_layout(rx_sock
, &rx_ring
, size_in
, ctx
->jumbo
, false);
376 create_rx_ring(rx_sock
, &rx_ring
, ctx
->verbose
);
377 mmap_rx_ring(rx_sock
, &rx_ring
);
378 alloc_rx_ring_frames(rx_sock
, &rx_ring
);
379 bind_rx_ring(rx_sock
, &rx_ring
, ifindex_in
);
380 prepare_polling(rx_sock
, &rx_poll
);
382 set_packet_loss_discard(tx_sock
);
383 setup_tx_ring_layout(tx_sock
, &tx_ring
, size_out
, ctx
->jumbo
);
384 create_tx_ring(tx_sock
, &tx_ring
, ctx
->verbose
);
385 mmap_tx_ring(tx_sock
, &tx_ring
);
386 alloc_tx_ring_frames(tx_sock
, &tx_ring
);
387 bind_tx_ring(tx_sock
, &tx_ring
, ifindex_out
);
389 dissector_init_all(ctx
->print_mode
);
391 if (ctx
->promiscuous
)
392 ifflags
= enter_promiscuous_mode(ctx
->device_in
);
395 interval
= ctx
->kpull
;
397 set_itimer_interval_value(&itimer
, 0, interval
);
398 setitimer(ITIMER_REAL
, &itimer
, NULL
);
400 drop_privileges(ctx
->enforce
, ctx
->uid
, ctx
->gid
);
402 printf("Running! Hang up with ^C!\n\n");
405 while (likely(sigint
== 0)) {
406 while (user_may_pull_from_rx(rx_ring
.frames
[it_in
].iov_base
)) {
409 hdr_in
= rx_ring
.frames
[it_in
].iov_base
;
410 in
= ((uint8_t *) hdr_in
) + hdr_in
->tp_h
.tp_mac
;
414 if (ctx
->packet_type
!= -1)
415 if (ctx
->packet_type
!= hdr_in
->s_ll
.sll_pkttype
)
418 hdr_out
= tx_ring
.frames
[it_out
].iov_base
;
419 out
= ((uint8_t *) hdr_out
) + TPACKET2_HDRLEN
- sizeof(struct sockaddr_ll
);
421 for (; !user_may_pull_from_tx(tx_ring
.frames
[it_out
].iov_base
) &&
424 next_rnd_slot(&it_out
, &tx_ring
);
427 if (it_out
>= tx_ring
.layout
.tp_frame_nr
)
431 hdr_out
= tx_ring
.frames
[it_out
].iov_base
;
432 out
= ((uint8_t *) hdr_out
) + TPACKET2_HDRLEN
- sizeof(struct sockaddr_ll
);
435 tpacket_hdr_clone(&hdr_out
->tp_h
, &hdr_in
->tp_h
);
436 fmemcpy(out
, in
, hdr_in
->tp_h
.tp_len
);
438 kernel_may_pull_from_tx(&hdr_out
->tp_h
);
440 next_rnd_slot(&it_out
, &tx_ring
);
443 if (it_out
>= tx_ring
.layout
.tp_frame_nr
)
447 show_frame_hdr(hdr_in
, ctx
->print_mode
);
449 dissector_entry_point(in
, hdr_in
->tp_h
.tp_snaplen
,
450 ctx
->link_type
, ctx
->print_mode
);
452 if (frame_count_max
!= 0) {
453 if (frame_count
>= frame_count_max
) {
461 kernel_may_pull_from_rx(&hdr_in
->tp_h
);
464 if (it_in
>= rx_ring
.layout
.tp_frame_nr
)
467 if (unlikely(sigint
== 1))
471 poll(&rx_poll
, 1, -1);
478 sock_print_net_stats(rx_sock
);
480 bpf_release(&bpf_ops
);
482 dissector_cleanup_all();
484 destroy_tx_ring(tx_sock
, &tx_ring
);
485 destroy_rx_ring(rx_sock
, &rx_ring
);
487 if (ctx
->promiscuous
)
488 leave_promiscuous_mode(ctx
->device_in
, ifflags
);
494 static void translate_pcap_to_txf(int fdo
, uint8_t *out
, size_t len
)
496 size_t bytes_done
= 0;
499 slprintf(bout
, sizeof(bout
), "{\n ");
500 write_or_die(fdo
, bout
, strlen(bout
));
502 while (bytes_done
< len
) {
503 slprintf(bout
, sizeof(bout
), "0x%02x, ", out
[bytes_done
]);
504 write_or_die(fdo
, bout
, strlen(bout
));
508 if (bytes_done
% 10 == 0) {
509 slprintf(bout
, sizeof(bout
), "\n");
510 write_or_die(fdo
, bout
, strlen(bout
));
512 if (bytes_done
< len
) {
513 slprintf(bout
, sizeof(bout
), " ");
514 write_or_die(fdo
, bout
, strlen(bout
));
518 if (bytes_done
% 10 != 0) {
519 slprintf(bout
, sizeof(bout
), "\n");
520 write_or_die(fdo
, bout
, strlen(bout
));
523 slprintf(bout
, sizeof(bout
), "}\n\n");
524 write_or_die(fdo
, bout
, strlen(bout
));
527 static void read_pcap(struct ctx
*ctx
)
531 int ret
, fd
, fdo
= 0;
532 unsigned long trunced
= 0;
535 struct sock_fprog bpf_ops
;
537 struct timeval start
, end
, diff
;
538 struct sockaddr_ll sll
;
542 if (!strncmp("-", ctx
->device_in
, strlen("-"))) {
543 fd
= dup(fileno(stdin
));
544 close(fileno(stdin
));
545 if (ctx
->pcap
== PCAP_OPS_MM
)
546 ctx
->pcap
= PCAP_OPS_SG
;
548 fd
= open_or_die(ctx
->device_in
, O_RDONLY
| O_LARGEFILE
| O_NOATIME
);
551 if (__pcap_io
->init_once_pcap
)
552 __pcap_io
->init_once_pcap();
554 ret
= __pcap_io
->pull_fhdr_pcap(fd
, &ctx
->magic
, &ctx
->link_type
);
556 panic("Error reading pcap header!\n");
558 if (__pcap_io
->prepare_access_pcap
) {
559 ret
= __pcap_io
->prepare_access_pcap(fd
, PCAP_MODE_RD
, ctx
->jumbo
);
561 panic("Error prepare reading pcap!\n");
564 fmemset(&fm
, 0, sizeof(fm
));
565 fmemset(&bpf_ops
, 0, sizeof(bpf_ops
));
567 bpf_parse_rules(ctx
->filter
, &bpf_ops
, ctx
->link_type
);
569 bpf_dump_all(&bpf_ops
);
571 dissector_init_all(ctx
->print_mode
);
573 out_len
= round_up(1024 * 1024, PAGE_SIZE
);
574 out
= xmalloc_aligned(out_len
, CO_CACHE_LINE_SIZE
);
576 if (ctx
->device_out
) {
577 if (!strncmp("-", ctx
->device_out
, strlen("-"))) {
578 fdo
= dup(fileno(stdout
));
579 close(fileno(stdout
));
581 fdo
= open_or_die_m(ctx
->device_out
, O_RDWR
| O_CREAT
|
582 O_TRUNC
| O_LARGEFILE
, DEFFILEMODE
);
586 drop_privileges(ctx
->enforce
, ctx
->uid
, ctx
->gid
);
588 printf("Running! Hang up with ^C!\n\n");
591 bug_on(gettimeofday(&start
, NULL
));
593 while (likely(sigint
== 0)) {
595 ret
= __pcap_io
->read_pcap(fd
, &phdr
, ctx
->magic
,
597 if (unlikely(ret
< 0))
600 if (unlikely(pcap_get_length(&phdr
, ctx
->magic
) == 0)) {
605 if (unlikely(pcap_get_length(&phdr
, ctx
->magic
) > out_len
)) {
606 pcap_set_length(&phdr
, ctx
->magic
, out_len
);
609 } while (ctx
->filter
&&
610 !bpf_run_filter(&bpf_ops
, out
,
611 pcap_get_length(&phdr
, ctx
->magic
)));
613 pcap_pkthdr_to_tpacket_hdr(&phdr
, ctx
->magic
, &fm
.tp_h
, &sll
);
615 ctx
->tx_bytes
+= fm
.tp_h
.tp_len
;
618 show_frame_hdr(&fm
, ctx
->print_mode
);
620 dissector_entry_point(out
, fm
.tp_h
.tp_snaplen
,
621 ctx
->link_type
, ctx
->print_mode
);
624 translate_pcap_to_txf(fdo
, out
, fm
.tp_h
.tp_snaplen
);
626 if (frame_count_max
!= 0) {
627 if (ctx
->tx_packets
>= frame_count_max
) {
636 bug_on(gettimeofday(&end
, NULL
));
637 timersub(&end
, &start
, &diff
);
639 bpf_release(&bpf_ops
);
641 dissector_cleanup_all();
643 if (__pcap_io
->prepare_close_pcap
)
644 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_RD
);
650 printf("\r%12lu packets outgoing\n", ctx
->tx_packets
);
651 printf("\r%12lu packets truncated in file\n", trunced
);
652 printf("\r%12lu bytes outgoing\n", ctx
->tx_bytes
);
653 printf("\r%12lu sec, %lu usec in total\n", diff
.tv_sec
, diff
.tv_usec
);
655 if (!strncmp("-", ctx
->device_in
, strlen("-")))
656 dup2(fd
, fileno(stdin
));
659 if (ctx
->device_out
) {
660 if (!strncmp("-", ctx
->device_out
, strlen("-")))
661 dup2(fdo
, fileno(stdout
));
666 static void finish_multi_pcap_file(struct ctx
*ctx
, int fd
)
668 __pcap_io
->fsync_pcap(fd
);
670 if (__pcap_io
->prepare_close_pcap
)
671 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_WR
);
675 fmemset(&itimer
, 0, sizeof(itimer
));
676 setitimer(ITIMER_REAL
, &itimer
, NULL
);
679 static int next_multi_pcap_file(struct ctx
*ctx
, int fd
)
684 __pcap_io
->fsync_pcap(fd
);
686 if (__pcap_io
->prepare_close_pcap
)
687 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_WR
);
691 slprintf(fname
, sizeof(fname
), "%s/%s%lu.pcap", ctx
->device_out
,
692 ctx
->prefix
? : "dump-", time(0));
694 fd
= open_or_die_m(fname
, O_RDWR
| O_CREAT
| O_TRUNC
|
695 O_LARGEFILE
, DEFFILEMODE
);
697 ret
= __pcap_io
->push_fhdr_pcap(fd
, ctx
->magic
, ctx
->link_type
);
699 panic("Error writing pcap header!\n");
701 if (__pcap_io
->prepare_access_pcap
) {
702 ret
= __pcap_io
->prepare_access_pcap(fd
, PCAP_MODE_WR
, true);
704 panic("Error prepare writing pcap!\n");
710 static int begin_multi_pcap_file(struct ctx
*ctx
)
717 if (ctx
->device_out
[strlen(ctx
->device_out
) - 1] == '/')
718 ctx
->device_out
[strlen(ctx
->device_out
) - 1] = 0;
720 slprintf(fname
, sizeof(fname
), "%s/%s%lu.pcap", ctx
->device_out
,
721 ctx
->prefix
? : "dump-", time(0));
723 fd
= open_or_die_m(fname
, O_RDWR
| O_CREAT
| O_TRUNC
|
724 O_LARGEFILE
, DEFFILEMODE
);
726 ret
= __pcap_io
->push_fhdr_pcap(fd
, ctx
->magic
, ctx
->link_type
);
728 panic("Error writing pcap header!\n");
730 if (__pcap_io
->prepare_access_pcap
) {
731 ret
= __pcap_io
->prepare_access_pcap(fd
, PCAP_MODE_WR
, true);
733 panic("Error prepare writing pcap!\n");
736 if (ctx
->dump_mode
== DUMP_INTERVAL_TIME
) {
737 interval
= ctx
->dump_interval
;
739 set_itimer_interval_value(&itimer
, interval
, 0);
740 setitimer(ITIMER_REAL
, &itimer
, NULL
);
748 static void finish_single_pcap_file(struct ctx
*ctx
, int fd
)
750 __pcap_io
->fsync_pcap(fd
);
752 if (__pcap_io
->prepare_close_pcap
)
753 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_WR
);
755 if (strncmp("-", ctx
->device_out
, strlen("-")))
758 dup2(fd
, fileno(stdout
));
761 static int begin_single_pcap_file(struct ctx
*ctx
)
767 if (!strncmp("-", ctx
->device_out
, strlen("-"))) {
768 fd
= dup(fileno(stdout
));
769 close(fileno(stdout
));
770 if (ctx
->pcap
== PCAP_OPS_MM
)
771 ctx
->pcap
= PCAP_OPS_SG
;
773 fd
= open_or_die_m(ctx
->device_out
,
774 O_RDWR
| O_CREAT
| O_TRUNC
|
775 O_LARGEFILE
, DEFFILEMODE
);
778 ret
= __pcap_io
->push_fhdr_pcap(fd
, ctx
->magic
, ctx
->link_type
);
780 panic("Error writing pcap header!\n");
782 if (__pcap_io
->prepare_access_pcap
) {
783 ret
= __pcap_io
->prepare_access_pcap(fd
, PCAP_MODE_WR
, true);
785 panic("Error prepare writing pcap!\n");
791 static void print_pcap_file_stats(int sock
, struct ctx
*ctx
)
794 struct tpacket_stats kstats
;
795 socklen_t slen
= sizeof(kstats
);
797 fmemset(&kstats
, 0, sizeof(kstats
));
799 ret
= getsockopt(sock
, SOL_PACKET
, PACKET_STATISTICS
, &kstats
, &slen
);
801 panic("Cannot get packet statistics!\n");
803 if (ctx
->print_mode
== PRINT_NONE
) {
804 printf(".(+%u/-%u)", kstats
.tp_packets
- kstats
.tp_drops
,
810 static void walk_t3_block(struct block_desc
*pbd
, struct ctx
*ctx
,
814 int num_pkts
= pbd
->h1
.num_pkts
, i
, ret
;
815 unsigned long frame_count
= 0;
816 struct tpacket3_hdr
*hdr
;
818 struct sockaddr_ll
*sll
;
820 hdr
= (void *) ((uint8_t *) pbd
+ pbd
->h1
.offset_to_first_pkt
);
821 sll
= (void *) ((uint8_t *) hdr
+ TPACKET_ALIGN(sizeof(*hdr
)));
823 for (i
= 0; i
< num_pkts
&& likely(sigint
== 0); ++i
) {
825 packet
= ((uint8_t *) hdr
+ hdr
->tp_mac
);
828 if (ctx
->packet_type
!= -1)
829 if (ctx
->packet_type
!= sll
->sll_pkttype
)
832 if (dump_to_pcap(ctx
)) {
833 tpacket3_hdr_to_pcap_pkthdr(hdr
, sll
, &phdr
, ctx
->magic
);
835 ret
= __pcap_io
->write_pcap(fd
, &phdr
, ctx
->magic
, packet
,
836 pcap_get_length(&phdr
, ctx
->magic
));
837 if (unlikely(ret
!= pcap_get_total_length(&phdr
, ctx
->magic
)))
838 panic("Write error to pcap!\n");
841 __show_frame_hdr(sll
, hdr
, ctx
->print_mode
, true);
843 dissector_entry_point(packet
, hdr
->tp_snaplen
, ctx
->link_type
,
847 hdr
= (void *) ((uint8_t *) hdr
+ hdr
->tp_next_offset
);
848 sll
= (void *) ((uint8_t *) hdr
+ TPACKET_ALIGN(sizeof(*hdr
)));
850 if (frame_count_max
!= 0) {
851 if (frame_count
>= frame_count_max
) {
857 if (dump_to_pcap(ctx
)) {
858 if (ctx
->dump_mode
== DUMP_INTERVAL_SIZE
) {
859 interval
+= hdr
->tp_snaplen
;
860 if (interval
> ctx
->dump_interval
) {
867 fd
= next_multi_pcap_file(ctx
, fd
);
870 if (unlikely(ctx
->verbose
))
871 print_pcap_file_stats(sock
, ctx
);
877 static void recv_only_or_dump(struct ctx
*ctx
)
880 int sock
, irq
, ifindex
, fd
= 0, ret
;
881 unsigned int size
, it
= 0;
883 struct pollfd rx_poll
;
884 struct sock_fprog bpf_ops
;
885 struct timeval start
, end
, diff
;
886 struct block_desc
*pbd
;
891 ctx
->device_trans
= xstrdup(ctx
->device_in
);
892 xfree(ctx
->device_in
);
894 enter_rfmon_mac80211(ctx
->device_trans
, &ctx
->device_in
);
895 ctx
->link_type
= LINKTYPE_IEEE802_11
;
898 fmemset(&rx_ring
, 0, sizeof(rx_ring
));
899 fmemset(&rx_poll
, 0, sizeof(rx_poll
));
900 fmemset(&bpf_ops
, 0, sizeof(bpf_ops
));
902 ifindex
= device_ifindex(ctx
->device_in
);
904 size
= ring_size(ctx
->device_in
, ctx
->reserve_size
);
906 enable_kernel_bpf_jit_compiler();
908 bpf_parse_rules(ctx
->filter
, &bpf_ops
, ctx
->link_type
);
910 bpf_dump_all(&bpf_ops
);
911 bpf_attach_to_sock(sock
, &bpf_ops
);
913 set_sockopt_hwtimestamp(sock
, ctx
->device_in
);
915 setup_rx_ring_layout(sock
, &rx_ring
, size
, true, true);
916 create_rx_ring(sock
, &rx_ring
, ctx
->verbose
);
917 mmap_rx_ring(sock
, &rx_ring
);
918 alloc_rx_ring_frames(sock
, &rx_ring
);
919 bind_rx_ring(sock
, &rx_ring
, ifindex
);
921 prepare_polling(sock
, &rx_poll
);
922 dissector_init_all(ctx
->print_mode
);
924 if (ctx
->cpu
>= 0 && ifindex
> 0) {
925 irq
= device_irq_number(ctx
->device_in
);
926 device_bind_irq_to_cpu(irq
, ctx
->cpu
);
929 printf("IRQ: %s:%d > CPU%d\n",
930 ctx
->device_in
, irq
, ctx
->cpu
);
933 if (ctx
->promiscuous
)
934 ifflags
= enter_promiscuous_mode(ctx
->device_in
);
936 if (dump_to_pcap(ctx
) && __pcap_io
->init_once_pcap
)
937 __pcap_io
->init_once_pcap();
939 drop_privileges(ctx
->enforce
, ctx
->uid
, ctx
->gid
);
941 if (dump_to_pcap(ctx
)) {
945 fmemset(&stats
, 0, sizeof(stats
));
946 ret
= stat(ctx
->device_out
, &stats
);
952 ctx
->dump_dir
= S_ISDIR(stats
.st_mode
);
954 fd
= begin_multi_pcap_file(ctx
);
957 fd
= begin_single_pcap_file(ctx
);
961 printf("Running! Hang up with ^C!\n\n");
964 bug_on(gettimeofday(&start
, NULL
));
966 while (likely(sigint
== 0)) {
967 while (user_may_pull_from_rx_block((pbd
= (void *)
968 rx_ring
.frames
[it
].iov_base
))) {
969 walk_t3_block(pbd
, ctx
, sock
, fd
);
971 kernel_may_pull_from_rx_block(pbd
);
972 it
= (it
+ 1) % rx_ring
.layout3
.tp_block_nr
;
974 if (unlikely(sigint
== 1))
978 poll(&rx_poll
, 1, -1);
981 bug_on(gettimeofday(&end
, NULL
));
982 timersub(&end
, &start
, &diff
);
984 if (!(ctx
->dump_dir
&& ctx
->print_mode
== PRINT_NONE
)) {
985 sock_print_net_stats(sock
);
987 printf("\r%12lu sec, %lu usec in total\n",
988 diff
.tv_sec
, diff
.tv_usec
);
994 bpf_release(&bpf_ops
);
995 dissector_cleanup_all();
996 destroy_rx_ring(sock
, &rx_ring
);
998 if (ctx
->promiscuous
)
999 leave_promiscuous_mode(ctx
->device_in
, ifflags
);
1002 leave_rfmon_mac80211(ctx
->device_trans
, ctx
->device_in
);
1004 if (dump_to_pcap(ctx
)) {
1006 finish_multi_pcap_file(ctx
, fd
);
1008 finish_single_pcap_file(ctx
, fd
);
1014 static void __noreturn
help(void)
1016 printf("\nnetsniff-ng %s, the packet sniffing beast\n", VERSION_STRING
);
1017 puts("http://www.netsniff-ng.org\n\n"
1018 "Usage: netsniff-ng [options] [filter-expression]\n"
1020 " -i|-d|--dev|--in <dev|pcap|-> Input source as netdev, pcap or pcap stdin\n"
1021 " -o|--out <dev|pcap|dir|cfg|-> Output sink as netdev, pcap, directory, trafgen, or stdout\n"
1022 " -f|--filter <bpf-file|expr> Use BPF filter file from bpfc or tcpdump-like expression\n"
1023 " -t|--type <type> Filter for: host|broadcast|multicast|others|outgoing\n"
1024 " -F|--interval <size|time> Dump interval if -o is a dir: <num>KiB/MiB/GiB/s/sec/min/hrs\n"
1025 " -R|--rfraw Capture or inject raw 802.11 frames\n"
1026 " -n|--num <0|uint> Number of packets until exit (def: 0)\n"
1027 " -P|--prefix <name> Prefix for pcaps stored in directory\n"
1028 " -T|--magic <pcap-magic> Pcap magic number/pcap format to store, see -D\n"
1029 " -D|--dump-pcap-types Dump pcap types and magic numbers and quit\n"
1030 " -B|--dump-bpf Dump generated BPF assembly\n"
1031 " -r|--rand Randomize packet forwarding order (dev->dev)\n"
1032 " -M|--no-promisc No promiscuous mode for netdev\n"
1033 " -A|--no-sock-mem Don't tune core socket memory\n"
1034 " -m|--mmap Mmap(2) pcap file i.e., for replaying pcaps\n"
1035 " -G|--sg Scatter/gather pcap file I/O\n"
1036 " -c|--clrw Use slower read(2)/write(2) I/O\n"
1037 " -S|--ring-size <size> Specify ring size to: <num>KiB/MiB/GiB\n"
1038 " -k|--kernel-pull <uint> Kernel pull from user interval in us (def: 10us)\n"
1039 " -J|--jumbo-support Support replay/fwd 64KB Super Jumbo Frames (def: 2048B)\n"
1040 " -b|--bind-cpu <cpu> Bind to specific CPU\n"
1041 " -u|--user <userid> Drop privileges and change to userid\n"
1042 " -g|--group <groupid> Drop privileges and change to groupid\n"
1043 " -H|--prio-high Make this high priority process\n"
1044 " -Q|--notouch-irq Do not touch IRQ CPU affinity of NIC\n"
1045 " -s|--silent Do not print captured packets\n"
1046 " -q|--less Print less-verbose packet information\n"
1047 " -X|--hex Print packet data in hex format\n"
1048 " -l|--ascii Print human-readable packet data\n"
1049 " -U|--update Update GeoIP databases\n"
1050 " -V|--verbose Be more verbose\n"
1051 " -v|--version Show version and exit\n"
1052 " -h|--help Guess what?!\n\n"
1054 " netsniff-ng --in eth0 --out dump.pcap -s -T 0xa1b2c3d4 --b 0 tcp or udp\n"
1055 " netsniff-ng --in wlan0 --rfraw --out dump.pcap --silent --bind-cpu 0\n"
1056 " netsniff-ng --in dump.pcap --mmap --out eth0 -k1000 --silent --bind-cpu 0\n"
1057 " netsniff-ng --in dump.pcap --out dump.cfg --silent --bind-cpu 0\n"
1058 " netsniff-ng --in eth0 --out eth1 --silent --bind-cpu 0 -J --type host\n"
1059 " netsniff-ng --in eth1 --out /opt/probe/ -s -m --interval 100MiB -b 0\n"
1060 " netsniff-ng --in vlan0 --out dump.pcap -c -u `id -u bob` -g `id -g bob`\n"
1061 " netsniff-ng --in any --filter http.bpf --jumbo-support --ascii -V\n\n"
1063 " For introducing bit errors, delays with random variation and more\n"
1064 " while replaying pcaps, make use of tc(8) with its disciplines (e.g. netem).\n\n"
1065 "Please report bugs to <bugs@netsniff-ng.org>\n"
1066 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
1067 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel.roullit@gmail.com>\n"
1068 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1069 "Swiss federal institute of technology (ETH Zurich)\n"
1070 "License: GNU GPL version 2.0\n"
1071 "This is free software: you are free to change and redistribute it.\n"
1072 "There is NO WARRANTY, to the extent permitted by law.\n");
1076 static void __noreturn
version(void)
1078 printf("\nnetsniff-ng %s, the packet sniffing beast\n", VERSION_LONG
);
1079 puts("http://www.netsniff-ng.org\n\n"
1080 "Please report bugs to <bugs@netsniff-ng.org>\n"
1081 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
1082 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel.roullit@gmail.com>\n"
1083 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1084 "Swiss federal institute of technology (ETH Zurich)\n"
1085 "License: GNU GPL version 2.0\n"
1086 "This is free software: you are free to change and redistribute it.\n"
1087 "There is NO WARRANTY, to the extent permitted by law.\n");
1091 int main(int argc
, char **argv
)
1094 int c
, i
, j
, cpu_tmp
, opt_index
, ops_touched
= 0, vals
[4] = {0};
1095 bool prio_high
= false, setsockmem
= true;
1096 void (*main_loop
)(struct ctx
*ctx
) = NULL
;
1098 .link_type
= LINKTYPE_EN10MB
,
1099 .print_mode
= PRINT_NORM
,
1102 .promiscuous
= true,
1104 .pcap
= PCAP_OPS_SG
,
1105 .dump_interval
= 60,
1106 .dump_mode
= DUMP_INTERVAL_TIME
,
1109 .magic
= ORIGINAL_TCPDUMP_MAGIC
,
1114 while ((c
= getopt_long(argc
, argv
, short_options
, long_options
,
1115 &opt_index
)) != EOF
) {
1119 ctx
.device_in
= xstrdup(optarg
);
1122 ctx
.device_out
= xstrdup(optarg
);
1125 ctx
.prefix
= xstrdup(optarg
);
1128 ctx
.link_type
= LINKTYPE_IEEE802_11
;
1132 ctx
.randomize
= true;
1138 ctx
.magic
= (uint32_t) strtoul(optarg
, NULL
, 0);
1139 pcap_check_magic(ctx
.magic
);
1142 ctx
.filter
= xstrdup(optarg
);
1145 ctx
.promiscuous
= false;
1151 ctx
.uid
= strtoul(optarg
, NULL
, 0);
1155 ctx
.gid
= strtoul(optarg
, NULL
, 0);
1159 if (!strncmp(optarg
, "host", strlen("host")))
1160 ctx
.packet_type
= PACKET_HOST
;
1161 else if (!strncmp(optarg
, "broadcast", strlen("broadcast")))
1162 ctx
.packet_type
= PACKET_BROADCAST
;
1163 else if (!strncmp(optarg
, "multicast", strlen("multicast")))
1164 ctx
.packet_type
= PACKET_MULTICAST
;
1165 else if (!strncmp(optarg
, "others", strlen("others")))
1166 ctx
.packet_type
= PACKET_OTHERHOST
;
1167 else if (!strncmp(optarg
, "outgoing", strlen("outgoing")))
1168 ctx
.packet_type
= PACKET_OUTGOING
;
1170 ctx
.packet_type
= -1;
1174 ctx
.reserve_size
= 0;
1176 for (j
= i
= strlen(optarg
); i
> 0; --i
) {
1177 if (!isdigit(optarg
[j
- i
]))
1182 if (!strncmp(ptr
, "KiB", strlen("KiB")))
1183 ctx
.reserve_size
= 1 << 10;
1184 else if (!strncmp(ptr
, "MiB", strlen("MiB")))
1185 ctx
.reserve_size
= 1 << 20;
1186 else if (!strncmp(ptr
, "GiB", strlen("GiB")))
1187 ctx
.reserve_size
= 1 << 30;
1189 panic("Syntax error in ring size param!\n");
1192 ctx
.reserve_size
*= strtol(optarg
, NULL
, 0);
1195 cpu_tmp
= strtol(optarg
, NULL
, 0);
1197 cpu_affinity(cpu_tmp
);
1205 ctx
.pcap
= PCAP_OPS_RW
;
1209 ctx
.pcap
= PCAP_OPS_MM
;
1213 ctx
.pcap
= PCAP_OPS_SG
;
1220 ctx
.print_mode
= PRINT_NONE
;
1223 ctx
.print_mode
= PRINT_LESS
;
1227 (ctx
.print_mode
== PRINT_ASCII
) ?
1228 PRINT_HEX_ASCII
: PRINT_HEX
;
1232 (ctx
.print_mode
== PRINT_HEX
) ?
1233 PRINT_HEX_ASCII
: PRINT_ASCII
;
1236 ctx
.kpull
= strtol(optarg
, NULL
, 0);
1239 frame_count_max
= strtol(optarg
, NULL
, 0);
1243 ctx
.dump_interval
= 0;
1245 for (j
= i
= strlen(optarg
); i
> 0; --i
) {
1246 if (!isdigit(optarg
[j
- i
]))
1251 if (!strncmp(ptr
, "KiB", strlen("KiB"))) {
1252 ctx
.dump_interval
= 1 << 10;
1253 ctx
.dump_mode
= DUMP_INTERVAL_SIZE
;
1254 } else if (!strncmp(ptr
, "MiB", strlen("MiB"))) {
1255 ctx
.dump_interval
= 1 << 20;
1256 ctx
.dump_mode
= DUMP_INTERVAL_SIZE
;
1257 } else if (!strncmp(ptr
, "GiB", strlen("GiB"))) {
1258 ctx
.dump_interval
= 1 << 30;
1259 ctx
.dump_mode
= DUMP_INTERVAL_SIZE
;
1260 } else if (!strncmp(ptr
, "sec", strlen("sec"))) {
1261 ctx
.dump_interval
= 1;
1262 ctx
.dump_mode
= DUMP_INTERVAL_TIME
;
1263 } else if (!strncmp(ptr
, "min", strlen("min"))) {
1264 ctx
.dump_interval
= 60;
1265 ctx
.dump_mode
= DUMP_INTERVAL_TIME
;
1266 } else if (!strncmp(ptr
, "hrs", strlen("hrs"))) {
1267 ctx
.dump_interval
= 60 * 60;
1268 ctx
.dump_mode
= DUMP_INTERVAL_TIME
;
1269 } else if (!strncmp(ptr
, "s", strlen("s"))) {
1270 ctx
.dump_interval
= 1;
1271 ctx
.dump_mode
= DUMP_INTERVAL_TIME
;
1273 panic("Syntax error in time/size param!\n");
1277 ctx
.dump_interval
*= strtol(optarg
, NULL
, 0);
1283 ctx
.dump_bpf
= true;
1286 pcap_dump_type_features();
1316 panic("Option -%c requires an argument!\n",
1319 if (isprint(optopt
))
1320 printf("Unknown option character `0x%X\'!\n", optopt
);
1328 if (!ctx
.filter
&& optind
!= argc
) {
1332 for (i
= optind
; i
< argc
; ++i
) {
1333 size_t alen
= strlen(argv
[i
]) + 2;
1334 size_t flen
= ctx
.filter
? strlen(ctx
.filter
) : 0;
1336 ctx
.filter
= xrealloc(ctx
.filter
, 1, flen
+ alen
);
1337 ret
= slprintf(ctx
.filter
+ offset
, strlen(argv
[i
]) + 2, "%s ", argv
[i
]);
1339 panic("Cannot concatenate filter string!\n");
1346 ctx
.device_in
= xstrdup("any");
1348 register_signal(SIGINT
, signal_handler
);
1349 register_signal(SIGHUP
, signal_handler
);
1354 set_proc_prio(get_default_proc_prio());
1355 set_sched_status(get_default_sched_policy(), get_default_sched_prio());
1358 if (ctx
.device_in
&& (device_mtu(ctx
.device_in
) ||
1359 !strncmp("any", ctx
.device_in
, strlen(ctx
.device_in
)))) {
1360 if (!ctx
.device_out
) {
1362 main_loop
= recv_only_or_dump
;
1363 } else if (device_mtu(ctx
.device_out
)) {
1364 register_signal_f(SIGALRM
, timer_elapsed
, SA_SIGINFO
);
1365 main_loop
= receive_to_xmit
;
1368 register_signal_f(SIGALRM
, timer_next_dump
, SA_SIGINFO
);
1369 main_loop
= recv_only_or_dump
;
1371 ctx
.pcap
= PCAP_OPS_SG
;
1374 if (ctx
.device_out
&& device_mtu(ctx
.device_out
)) {
1375 register_signal_f(SIGALRM
, timer_elapsed
, SA_SIGINFO
);
1376 main_loop
= pcap_to_xmit
;
1378 ctx
.pcap
= PCAP_OPS_MM
;
1380 main_loop
= read_pcap
;
1382 ctx
.pcap
= PCAP_OPS_SG
;
1390 set_system_socket_memory(vals
, array_size(vals
));
1399 reset_system_socket_memory(vals
, array_size(vals
));
1404 free(ctx
.device_in
);
1405 free(ctx
.device_out
);
1406 free(ctx
.device_trans
);