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'},
108 static const char *copyright
= "Please report bugs to <bugs@netsniff-ng.org>\n"
109 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
110 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel.roullit@gmail.com>\n"
111 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
112 "Swiss federal institute of technology (ETH Zurich)\n"
113 "License: GNU GPL version 2.0\n"
114 "This is free software: you are free to change and redistribute it.\n"
115 "There is NO WARRANTY, to the extent permitted by law.";
118 static struct itimerval itimer
;
119 static unsigned long frame_count_max
= 0, interval
= TX_KERNEL_PULL_INT
;
121 #define __pcap_io pcap_ops[ctx->pcap]
123 static void signal_handler(int number
)
136 static void timer_elapsed(int unused __maybe_unused
)
140 set_itimer_interval_value(&itimer
, 0, interval
);
142 ret
= pull_and_flush_tx_ring(tx_sock
);
143 if (unlikely(ret
< 0)) {
144 /* We could hit EBADF if the socket has been closed before
145 * the timer was triggered.
147 if (errno
!= EBADF
&& errno
!= ENOBUFS
)
148 panic("Flushing TX_RING failed: %s!\n", strerror(errno
));
151 setitimer(ITIMER_REAL
, &itimer
, NULL
);
154 static void timer_purge(void)
158 ret
= pull_and_flush_tx_ring_wait(tx_sock
);
159 if (unlikely(ret
< 0)) {
160 if (errno
!= EBADF
&& errno
!= ENOBUFS
)
161 panic("Flushing TX_RING failed: %s!\n", strerror(errno
));
164 set_itimer_interval_value(&itimer
, 0, 0);
165 setitimer(ITIMER_REAL
, &itimer
, NULL
);
168 static void timer_next_dump(int unused __maybe_unused
)
170 set_itimer_interval_value(&itimer
, interval
, 0);
172 setitimer(ITIMER_REAL
, &itimer
, NULL
);
175 static inline bool dump_to_pcap(struct ctx
*ctx
)
180 static void pcap_to_xmit(struct ctx
*ctx
)
183 int ifindex
, fd
= 0, ret
;
186 unsigned long trunced
= 0;
188 struct frame_map
*hdr
;
189 struct sock_fprog bpf_ops
;
190 struct timeval start
, end
, diff
;
193 if (!device_up_and_running(ctx
->device_out
) && !ctx
->rfraw
)
194 panic("Device not up and running!\n");
198 tx_sock
= pf_socket();
200 if (!strncmp("-", ctx
->device_in
, strlen("-"))) {
201 fd
= dup_or_die(fileno(stdin
));
202 close(fileno(stdin
));
203 if (ctx
->pcap
== PCAP_OPS_MM
)
204 ctx
->pcap
= PCAP_OPS_SG
;
206 fd
= open_or_die(ctx
->device_in
, O_RDONLY
| O_LARGEFILE
| O_NOATIME
);
209 if (__pcap_io
->init_once_pcap
)
210 __pcap_io
->init_once_pcap();
212 ret
= __pcap_io
->pull_fhdr_pcap(fd
, &ctx
->magic
, &ctx
->link_type
);
214 panic("Error reading pcap header!\n");
216 if (__pcap_io
->prepare_access_pcap
) {
217 ret
= __pcap_io
->prepare_access_pcap(fd
, PCAP_MODE_RD
, ctx
->jumbo
);
219 panic("Error prepare reading pcap!\n");
223 ctx
->device_trans
= xstrdup(ctx
->device_out
);
224 xfree(ctx
->device_out
);
226 enter_rfmon_mac80211(ctx
->device_trans
, &ctx
->device_out
);
227 if (ctx
->link_type
!= LINKTYPE_IEEE802_11
)
228 panic("Wrong linktype of pcap!\n");
231 ifindex
= device_ifindex(ctx
->device_out
);
232 size
= ring_size(ctx
->device_out
, ctx
->reserve_size
);
234 bpf_parse_rules(ctx
->filter
, &bpf_ops
, ctx
->link_type
);
236 bpf_dump_all(&bpf_ops
);
238 ring_tx_setup(&tx_ring
, tx_sock
, size
, ifindex
, ctx
->jumbo
, ctx
->verbose
);
240 dissector_init_all(ctx
->print_mode
);
242 if (ctx
->cpu
>= 0 && ifindex
> 0) {
243 int irq
= device_irq_number(ctx
->device_out
);
244 device_set_irq_affinity(irq
, ctx
->cpu
);
247 printf("IRQ: %s:%d > CPU%d\n",
248 ctx
->device_out
, irq
, ctx
->cpu
);
252 interval
= ctx
->kpull
;
254 set_itimer_interval_value(&itimer
, 0, interval
);
255 setitimer(ITIMER_REAL
, &itimer
, NULL
);
257 drop_privileges(ctx
->enforce
, ctx
->uid
, ctx
->gid
);
259 printf("Running! Hang up with ^C!\n\n");
262 bug_on(gettimeofday(&start
, NULL
));
264 while (likely(sigint
== 0)) {
265 while (user_may_pull_from_tx(tx_ring
.frames
[it
].iov_base
)) {
266 hdr
= tx_ring
.frames
[it
].iov_base
;
267 out
= ((uint8_t *) hdr
) + TPACKET2_HDRLEN
- sizeof(struct sockaddr_ll
);
270 ret
= __pcap_io
->read_pcap(fd
, &phdr
, ctx
->magic
, out
,
271 ring_frame_size(&tx_ring
));
272 if (unlikely(ret
<= 0))
275 if (ring_frame_size(&tx_ring
) <
276 pcap_get_length(&phdr
, ctx
->magic
)) {
277 pcap_set_length(&phdr
, ctx
->magic
,
278 ring_frame_size(&tx_ring
));
281 } while (ctx
->filter
&&
282 !bpf_run_filter(&bpf_ops
, out
,
283 pcap_get_length(&phdr
, ctx
->magic
)));
285 pcap_pkthdr_to_tpacket_hdr(&phdr
, ctx
->magic
, &hdr
->tp_h
, NULL
);
287 ctx
->tx_bytes
+= hdr
->tp_h
.tp_len
;;
290 show_frame_hdr(out
, hdr
->tp_h
.tp_snaplen
,
291 ctx
->link_type
, hdr
, ctx
->print_mode
);
293 dissector_entry_point(out
, hdr
->tp_h
.tp_snaplen
,
294 ctx
->link_type
, ctx
->print_mode
);
296 kernel_may_pull_from_tx(&hdr
->tp_h
);
299 if (it
>= tx_ring
.layout
.tp_frame_nr
)
302 if (unlikely(sigint
== 1))
305 if (frame_count_max
!= 0) {
306 if (ctx
->tx_packets
>= frame_count_max
) {
315 bug_on(gettimeofday(&end
, NULL
));
316 timersub(&end
, &start
, &diff
);
320 bpf_release(&bpf_ops
);
322 dissector_cleanup_all();
323 destroy_tx_ring(tx_sock
, &tx_ring
);
326 leave_rfmon_mac80211(ctx
->device_out
);
328 if (__pcap_io
->prepare_close_pcap
)
329 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_RD
);
331 if (!strncmp("-", ctx
->device_in
, strlen("-")))
332 dup2(fd
, fileno(stdin
));
339 printf("\r%12lu packets outgoing\n", ctx
->tx_packets
);
340 printf("\r%12lu packets truncated in file\n", trunced
);
341 printf("\r%12lu bytes outgoing\n", ctx
->tx_bytes
);
342 printf("\r%12lu sec, %lu usec in total\n", diff
.tv_sec
, diff
.tv_usec
);
345 static void receive_to_xmit(struct ctx
*ctx
)
349 int rx_sock
, ifindex_in
, ifindex_out
, ret
;
350 size_t size_in
, size_out
;
351 unsigned int it_in
= 0, it_out
= 0;
352 unsigned long frame_count
= 0;
353 struct frame_map
*hdr_in
, *hdr_out
;
354 struct ring tx_ring
, rx_ring
;
355 struct pollfd rx_poll
;
356 struct sock_fprog bpf_ops
;
358 if (!strncmp(ctx
->device_in
, ctx
->device_out
, IFNAMSIZ
))
359 panic("Ingress/egress devices must be different!\n");
360 if (!device_up_and_running(ctx
->device_out
))
361 panic("Egress device not up and running!\n");
363 rx_sock
= pf_socket();
364 tx_sock
= pf_socket();
366 ifindex_in
= device_ifindex(ctx
->device_in
);
367 ifindex_out
= device_ifindex(ctx
->device_out
);
369 size_in
= ring_size(ctx
->device_in
, ctx
->reserve_size
);
370 size_out
= ring_size(ctx
->device_out
, ctx
->reserve_size
);
372 enable_kernel_bpf_jit_compiler();
374 bpf_parse_rules(ctx
->filter
, &bpf_ops
, ctx
->link_type
);
376 bpf_dump_all(&bpf_ops
);
377 bpf_attach_to_sock(rx_sock
, &bpf_ops
);
379 ring_rx_setup(&rx_ring
, rx_sock
, size_in
, ifindex_in
, &rx_poll
, false, ctx
->jumbo
, ctx
->verbose
);
380 ring_tx_setup(&tx_ring
, tx_sock
, size_out
, ifindex_out
, ctx
->jumbo
, ctx
->verbose
);
382 dissector_init_all(ctx
->print_mode
);
384 if (ctx
->promiscuous
)
385 ifflags
= device_enter_promiscuous_mode(ctx
->device_in
);
388 interval
= ctx
->kpull
;
390 set_itimer_interval_value(&itimer
, 0, interval
);
391 setitimer(ITIMER_REAL
, &itimer
, NULL
);
393 drop_privileges(ctx
->enforce
, ctx
->uid
, ctx
->gid
);
395 printf("Running! Hang up with ^C!\n\n");
398 while (likely(sigint
== 0)) {
399 while (user_may_pull_from_rx(rx_ring
.frames
[it_in
].iov_base
)) {
400 hdr_in
= rx_ring
.frames
[it_in
].iov_base
;
401 in
= ((uint8_t *) hdr_in
) + hdr_in
->tp_h
.tp_mac
;
405 if (ctx
->packet_type
!= -1)
406 if (ctx
->packet_type
!= hdr_in
->s_ll
.sll_pkttype
)
409 hdr_out
= tx_ring
.frames
[it_out
].iov_base
;
410 out
= ((uint8_t *) hdr_out
) + TPACKET2_HDRLEN
- sizeof(struct sockaddr_ll
);
412 while (!user_may_pull_from_tx(tx_ring
.frames
[it_out
].iov_base
) &&
415 next_rnd_slot(&it_out
, &tx_ring
);
418 if (it_out
>= tx_ring
.layout
.tp_frame_nr
)
422 hdr_out
= tx_ring
.frames
[it_out
].iov_base
;
423 out
= ((uint8_t *) hdr_out
) + TPACKET2_HDRLEN
- sizeof(struct sockaddr_ll
);
426 tpacket_hdr_clone(&hdr_out
->tp_h
, &hdr_in
->tp_h
);
427 fmemcpy(out
, in
, hdr_in
->tp_h
.tp_len
);
429 kernel_may_pull_from_tx(&hdr_out
->tp_h
);
431 next_rnd_slot(&it_out
, &tx_ring
);
434 if (it_out
>= tx_ring
.layout
.tp_frame_nr
)
438 show_frame_hdr(in
, hdr_in
->tp_h
.tp_snaplen
,
439 ctx
->link_type
, hdr_in
, ctx
->print_mode
);
441 dissector_entry_point(in
, hdr_in
->tp_h
.tp_snaplen
,
442 ctx
->link_type
, ctx
->print_mode
);
444 if (frame_count_max
!= 0) {
445 if (frame_count
>= frame_count_max
) {
452 kernel_may_pull_from_rx(&hdr_in
->tp_h
);
455 if (it_in
>= rx_ring
.layout
.tp_frame_nr
)
458 if (unlikely(sigint
== 1))
462 ret
= poll(&rx_poll
, 1, -1);
463 if (unlikely(ret
< 0)) {
465 panic("Poll failed!\n");
472 sock_rx_net_stats(rx_sock
, 0);
474 bpf_release(&bpf_ops
);
476 dissector_cleanup_all();
478 destroy_tx_ring(tx_sock
, &tx_ring
);
479 destroy_rx_ring(rx_sock
, &rx_ring
);
481 if (ctx
->promiscuous
)
482 device_leave_promiscuous_mode(ctx
->device_in
, ifflags
);
488 static void translate_pcap_to_txf(int fdo
, uint8_t *out
, size_t len
)
490 size_t bytes_done
= 0;
493 slprintf(bout
, sizeof(bout
), "{\n ");
494 write_or_die(fdo
, bout
, strlen(bout
));
496 while (bytes_done
< len
) {
497 slprintf(bout
, sizeof(bout
), "0x%02x, ", out
[bytes_done
]);
498 write_or_die(fdo
, bout
, strlen(bout
));
502 if (bytes_done
% 10 == 0) {
503 slprintf(bout
, sizeof(bout
), "\n");
504 write_or_die(fdo
, bout
, strlen(bout
));
506 if (bytes_done
< len
) {
507 slprintf(bout
, sizeof(bout
), " ");
508 write_or_die(fdo
, bout
, strlen(bout
));
512 if (bytes_done
% 10 != 0) {
513 slprintf(bout
, sizeof(bout
), "\n");
514 write_or_die(fdo
, bout
, strlen(bout
));
517 slprintf(bout
, sizeof(bout
), "}\n\n");
518 write_or_die(fdo
, bout
, strlen(bout
));
521 static void read_pcap(struct ctx
*ctx
)
524 int ret
, fd
, fdo
= 0;
525 unsigned long trunced
= 0;
528 struct sock_fprog bpf_ops
;
530 struct timeval start
, end
, diff
;
531 bool is_out_pcap
= ctx
->device_out
&& strstr(ctx
->device_out
, ".pcap");
532 const struct pcap_file_ops
*pcap_out_ops
= pcap_ops
[PCAP_OPS_RW
];
536 if (!strncmp("-", ctx
->device_in
, strlen("-"))) {
537 fd
= dup_or_die(fileno(stdin
));
538 close(fileno(stdin
));
539 if (ctx
->pcap
== PCAP_OPS_MM
)
540 ctx
->pcap
= PCAP_OPS_SG
;
542 fd
= open_or_die(ctx
->device_in
, O_RDONLY
| O_LARGEFILE
| O_NOATIME
);
545 if (__pcap_io
->init_once_pcap
)
546 __pcap_io
->init_once_pcap();
548 ret
= __pcap_io
->pull_fhdr_pcap(fd
, &ctx
->magic
, &ctx
->link_type
);
550 panic("Error reading pcap header!\n");
552 if (__pcap_io
->prepare_access_pcap
) {
553 ret
= __pcap_io
->prepare_access_pcap(fd
, PCAP_MODE_RD
, ctx
->jumbo
);
555 panic("Error prepare reading pcap!\n");
558 fmemset(&fm
, 0, sizeof(fm
));
560 bpf_parse_rules(ctx
->filter
, &bpf_ops
, ctx
->link_type
);
562 bpf_dump_all(&bpf_ops
);
564 dissector_init_all(ctx
->print_mode
);
566 out_len
= round_up(1024 * 1024, RUNTIME_PAGE_SIZE
);
567 out
= xmalloc_aligned(out_len
, CO_CACHE_LINE_SIZE
);
569 if (ctx
->device_out
) {
570 if (!strncmp("-", ctx
->device_out
, strlen("-"))) {
571 fdo
= dup_or_die(fileno(stdout
));
572 close(fileno(stdout
));
574 fdo
= open_or_die_m(ctx
->device_out
, O_RDWR
| O_CREAT
|
575 O_TRUNC
| O_LARGEFILE
, DEFFILEMODE
);
580 ret
= pcap_out_ops
->push_fhdr_pcap(fdo
, ctx
->magic
,
583 panic("Error writing pcap header!\n");
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
, &fm
.s_ll
);
615 ctx
->tx_bytes
+= fm
.tp_h
.tp_len
;
618 show_frame_hdr(out
, fm
.tp_h
.tp_snaplen
, ctx
->link_type
, &fm
,
621 dissector_entry_point(out
, fm
.tp_h
.tp_snaplen
,
622 ctx
->link_type
, ctx
->print_mode
);
625 size_t pcap_len
= pcap_get_length(&phdr
, ctx
->magic
);
626 int wlen
= pcap_out_ops
->write_pcap(fdo
, &phdr
,
629 if (unlikely(wlen
!= (int)pcap_get_total_length(&phdr
, ctx
->magic
)))
630 panic("Error writing to pcap!\n");
631 } else if (ctx
->device_out
) {
632 translate_pcap_to_txf(fdo
, out
, fm
.tp_h
.tp_snaplen
);
635 if (frame_count_max
!= 0) {
636 if (ctx
->tx_packets
>= frame_count_max
) {
644 bug_on(gettimeofday(&end
, NULL
));
645 timersub(&end
, &start
, &diff
);
647 bpf_release(&bpf_ops
);
649 dissector_cleanup_all();
651 if (__pcap_io
->prepare_close_pcap
)
652 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_RD
);
658 printf("\r%12lu packets outgoing\n", ctx
->tx_packets
);
659 printf("\r%12lu packets truncated in file\n", trunced
);
660 printf("\r%12lu bytes outgoing\n", ctx
->tx_bytes
);
661 printf("\r%12lu sec, %lu usec in total\n", diff
.tv_sec
, diff
.tv_usec
);
663 if (!strncmp("-", ctx
->device_in
, strlen("-")))
664 dup2(fd
, fileno(stdin
));
667 if (ctx
->device_out
) {
668 if (!strncmp("-", ctx
->device_out
, strlen("-")))
669 dup2(fdo
, fileno(stdout
));
674 static void finish_multi_pcap_file(struct ctx
*ctx
, int fd
)
676 __pcap_io
->fsync_pcap(fd
);
678 if (__pcap_io
->prepare_close_pcap
)
679 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_WR
);
683 fmemset(&itimer
, 0, sizeof(itimer
));
684 setitimer(ITIMER_REAL
, &itimer
, NULL
);
687 static int next_multi_pcap_file(struct ctx
*ctx
, int fd
)
692 __pcap_io
->fsync_pcap(fd
);
694 if (__pcap_io
->prepare_close_pcap
)
695 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_WR
);
699 slprintf(fname
, sizeof(fname
), "%s/%s%lu.pcap", ctx
->device_out
,
700 ctx
->prefix
? : "dump-", time(NULL
));
702 fd
= open_or_die_m(fname
, O_RDWR
| O_CREAT
| O_TRUNC
|
703 O_LARGEFILE
, DEFFILEMODE
);
705 ret
= __pcap_io
->push_fhdr_pcap(fd
, ctx
->magic
, ctx
->link_type
);
707 panic("Error writing pcap header!\n");
709 if (__pcap_io
->prepare_access_pcap
) {
710 ret
= __pcap_io
->prepare_access_pcap(fd
, PCAP_MODE_WR
, true);
712 panic("Error prepare writing pcap!\n");
718 static int begin_multi_pcap_file(struct ctx
*ctx
)
725 if (ctx
->device_out
[strlen(ctx
->device_out
) - 1] == '/')
726 ctx
->device_out
[strlen(ctx
->device_out
) - 1] = 0;
728 slprintf(fname
, sizeof(fname
), "%s/%s%lu.pcap", ctx
->device_out
,
729 ctx
->prefix
? : "dump-", time(NULL
));
731 fd
= open_or_die_m(fname
, O_RDWR
| O_CREAT
| O_TRUNC
|
732 O_LARGEFILE
, DEFFILEMODE
);
734 ret
= __pcap_io
->push_fhdr_pcap(fd
, ctx
->magic
, ctx
->link_type
);
736 panic("Error writing pcap header!\n");
738 if (__pcap_io
->prepare_access_pcap
) {
739 ret
= __pcap_io
->prepare_access_pcap(fd
, PCAP_MODE_WR
, true);
741 panic("Error prepare writing pcap!\n");
744 if (ctx
->dump_mode
== DUMP_INTERVAL_TIME
) {
745 interval
= ctx
->dump_interval
;
747 set_itimer_interval_value(&itimer
, interval
, 0);
748 setitimer(ITIMER_REAL
, &itimer
, NULL
);
756 static void finish_single_pcap_file(struct ctx
*ctx
, int fd
)
758 __pcap_io
->fsync_pcap(fd
);
760 if (__pcap_io
->prepare_close_pcap
)
761 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_WR
);
763 if (strncmp("-", ctx
->device_out
, strlen("-")))
766 dup2(fd
, fileno(stdout
));
769 static int begin_single_pcap_file(struct ctx
*ctx
)
775 if (!strncmp("-", ctx
->device_out
, strlen("-"))) {
776 fd
= dup_or_die(fileno(stdout
));
777 close(fileno(stdout
));
778 if (ctx
->pcap
== PCAP_OPS_MM
)
779 ctx
->pcap
= PCAP_OPS_SG
;
781 fd
= open_or_die_m(ctx
->device_out
,
782 O_RDWR
| O_CREAT
| O_TRUNC
|
783 O_LARGEFILE
, DEFFILEMODE
);
786 ret
= __pcap_io
->push_fhdr_pcap(fd
, ctx
->magic
, ctx
->link_type
);
788 panic("Error writing pcap header!\n");
790 if (__pcap_io
->prepare_access_pcap
) {
791 ret
= __pcap_io
->prepare_access_pcap(fd
, PCAP_MODE_WR
, true);
793 panic("Error prepare writing pcap!\n");
799 static void print_pcap_file_stats(int sock
, struct ctx
*ctx
)
802 struct tpacket_stats kstats
;
803 socklen_t slen
= sizeof(kstats
);
805 fmemset(&kstats
, 0, sizeof(kstats
));
807 ret
= getsockopt(sock
, SOL_PACKET
, PACKET_STATISTICS
, &kstats
, &slen
);
809 panic("Cannot get packet statistics!\n");
811 if (ctx
->print_mode
== PRINT_NONE
) {
812 printf(".(+%u/-%u)", kstats
.tp_packets
- kstats
.tp_drops
,
818 static void update_pcap_next_dump(struct ctx
*ctx
, unsigned long snaplen
, int *fd
, int sock
)
820 if (!dump_to_pcap(ctx
))
823 if (ctx
->dump_mode
== DUMP_INTERVAL_SIZE
) {
825 if (interval
> ctx
->dump_interval
) {
832 *fd
= next_multi_pcap_file(ctx
, *fd
);
836 print_pcap_file_stats(sock
, ctx
);
841 static void walk_t3_block(struct block_desc
*pbd
, struct ctx
*ctx
,
842 int sock
, int *fd
, unsigned long *frame_count
)
844 int num_pkts
= pbd
->h1
.num_pkts
, i
;
845 struct tpacket3_hdr
*hdr
;
846 struct sockaddr_ll
*sll
;
848 hdr
= (void *) ((uint8_t *) pbd
+ pbd
->h1
.offset_to_first_pkt
);
849 sll
= (void *) ((uint8_t *) hdr
+ TPACKET_ALIGN(sizeof(*hdr
)));
851 for (i
= 0; i
< num_pkts
&& likely(sigint
== 0); ++i
) {
852 uint8_t *packet
= ((uint8_t *) hdr
+ hdr
->tp_mac
);
855 if (ctx
->packet_type
!= -1)
856 if (ctx
->packet_type
!= sll
->sll_pkttype
)
861 if (dump_to_pcap(ctx
)) {
864 tpacket3_hdr_to_pcap_pkthdr(hdr
, sll
, &phdr
, ctx
->magic
);
866 ret
= __pcap_io
->write_pcap(*fd
, &phdr
, ctx
->magic
, packet
,
867 pcap_get_length(&phdr
, ctx
->magic
));
868 if (unlikely(ret
!= (int) pcap_get_total_length(&phdr
, ctx
->magic
)))
869 panic("Write error to pcap!\n");
872 __show_frame_hdr(packet
, hdr
->tp_snaplen
, ctx
->link_type
, sll
,
873 hdr
, ctx
->print_mode
, true);
875 dissector_entry_point(packet
, hdr
->tp_snaplen
, ctx
->link_type
,
878 hdr
= (void *) ((uint8_t *) hdr
+ hdr
->tp_next_offset
);
879 sll
= (void *) ((uint8_t *) hdr
+ TPACKET_ALIGN(sizeof(*hdr
)));
881 if (frame_count_max
!= 0) {
882 if (unlikely(*frame_count
>= frame_count_max
)) {
888 update_pcap_next_dump(ctx
, hdr
->tp_snaplen
, fd
, sock
);
891 #endif /* HAVE_TPACKET3 */
893 static void recv_only_or_dump(struct ctx
*ctx
)
896 int sock
, ifindex
, fd
= 0, ret
;
900 struct pollfd rx_poll
;
901 struct sock_fprog bpf_ops
;
902 struct timeval start
, end
, diff
;
903 unsigned long frame_count
= 0;
908 ctx
->device_trans
= xstrdup(ctx
->device_in
);
909 xfree(ctx
->device_in
);
911 enter_rfmon_mac80211(ctx
->device_trans
, &ctx
->device_in
);
912 ctx
->link_type
= LINKTYPE_IEEE802_11
;
915 ifindex
= device_ifindex(ctx
->device_in
);
917 size
= ring_size(ctx
->device_in
, ctx
->reserve_size
);
919 enable_kernel_bpf_jit_compiler();
921 bpf_parse_rules(ctx
->filter
, &bpf_ops
, ctx
->link_type
);
923 bpf_dump_all(&bpf_ops
);
924 bpf_attach_to_sock(sock
, &bpf_ops
);
926 if (ctx
->hwtimestamp
) {
927 ret
= set_sockopt_hwtimestamp(sock
, ctx
->device_in
);
928 if (ret
== 0 && ctx
->verbose
)
929 printf("HW timestamping enabled\n");
932 ring_rx_setup(&rx_ring
, sock
, size
, ifindex
, &rx_poll
, is_defined(HAVE_TPACKET3
), true, ctx
->verbose
);
934 dissector_init_all(ctx
->print_mode
);
936 if (ctx
->cpu
>= 0 && ifindex
> 0) {
937 int irq
= device_irq_number(ctx
->device_in
);
938 device_set_irq_affinity(irq
, ctx
->cpu
);
941 printf("IRQ: %s:%d > CPU%d\n",
942 ctx
->device_in
, irq
, ctx
->cpu
);
945 if (ctx
->promiscuous
)
946 ifflags
= device_enter_promiscuous_mode(ctx
->device_in
);
948 if (dump_to_pcap(ctx
) && __pcap_io
->init_once_pcap
)
949 __pcap_io
->init_once_pcap();
951 drop_privileges(ctx
->enforce
, ctx
->uid
, ctx
->gid
);
953 if (dump_to_pcap(ctx
)) {
956 ret
= stat(ctx
->device_out
, &stats
);
960 ctx
->dump_dir
= S_ISDIR(stats
.st_mode
);
963 fd
= begin_multi_pcap_file(ctx
);
965 fd
= begin_single_pcap_file(ctx
);
968 printf("Running! Hang up with ^C!\n\n");
971 bug_on(gettimeofday(&start
, NULL
));
973 while (likely(sigint
== 0)) {
975 struct block_desc
*pbd
;
977 while (user_may_pull_from_rx_block((pbd
= rx_ring
.frames
[it
].iov_base
))) {
978 walk_t3_block(pbd
, ctx
, sock
, &fd
, &frame_count
);
980 kernel_may_pull_from_rx_block(pbd
);
981 it
= (it
+ 1) % rx_ring
.layout3
.tp_block_nr
;
983 if (unlikely(sigint
== 1))
987 while (user_may_pull_from_rx(rx_ring
.frames
[it
].iov_base
)) {
988 struct frame_map
*hdr
= rx_ring
.frames
[it
].iov_base
;
989 uint8_t *packet
= ((uint8_t *) hdr
) + hdr
->tp_h
.tp_mac
;
992 if (ctx
->packet_type
!= -1)
993 if (ctx
->packet_type
!= hdr
->s_ll
.sll_pkttype
)
998 if (unlikely(ring_frame_size(&rx_ring
) < hdr
->tp_h
.tp_snaplen
)) {
999 /* XXX: silently ignore for now. We used to
1000 * report them with sock_rx_net_stats() */
1004 if (dump_to_pcap(ctx
)) {
1005 tpacket_hdr_to_pcap_pkthdr(&hdr
->tp_h
, &hdr
->s_ll
, &phdr
, ctx
->magic
);
1007 ret
= __pcap_io
->write_pcap(fd
, &phdr
, ctx
->magic
, packet
,
1008 pcap_get_length(&phdr
, ctx
->magic
));
1009 if (unlikely(ret
!= (int) pcap_get_total_length(&phdr
, ctx
->magic
)))
1010 panic("Write error to pcap!\n");
1013 show_frame_hdr(packet
, hdr
->tp_h
.tp_snaplen
,
1014 ctx
->link_type
, hdr
, ctx
->print_mode
);
1016 dissector_entry_point(packet
, hdr
->tp_h
.tp_snaplen
,
1017 ctx
->link_type
, ctx
->print_mode
);
1019 if (frame_count_max
!= 0) {
1020 if (unlikely(frame_count
>= frame_count_max
)) {
1027 kernel_may_pull_from_rx(&hdr
->tp_h
);
1028 it
= (it
+ 1) % rx_ring
.layout
.tp_frame_nr
;
1030 if (unlikely(sigint
== 1))
1033 update_pcap_next_dump(ctx
, hdr
->tp_h
.tp_snaplen
, &fd
, sock
);
1035 #endif /* HAVE_TPACKET3 */
1037 ret
= poll(&rx_poll
, 1, -1);
1038 if (unlikely(ret
< 0)) {
1040 panic("Poll failed!\n");
1044 bug_on(gettimeofday(&end
, NULL
));
1045 timersub(&end
, &start
, &diff
);
1047 if (!(ctx
->dump_dir
&& ctx
->print_mode
== PRINT_NONE
)) {
1048 sock_rx_net_stats(sock
, frame_count
);
1050 printf("\r%12lu sec, %lu usec in total\n",
1051 diff
.tv_sec
, diff
.tv_usec
);
1057 bpf_release(&bpf_ops
);
1058 dissector_cleanup_all();
1059 destroy_rx_ring(sock
, &rx_ring
);
1061 if (ctx
->promiscuous
)
1062 device_leave_promiscuous_mode(ctx
->device_in
, ifflags
);
1065 leave_rfmon_mac80211(ctx
->device_in
);
1067 if (dump_to_pcap(ctx
)) {
1069 finish_multi_pcap_file(ctx
, fd
);
1071 finish_single_pcap_file(ctx
, fd
);
1077 static void init_ctx(struct ctx
*ctx
)
1079 memset(ctx
, 0, sizeof(*ctx
));
1080 ctx
->uid
= getuid();
1081 ctx
->uid
= getgid();
1084 ctx
->packet_type
= -1;
1086 ctx
->magic
= ORIGINAL_TCPDUMP_MAGIC
;
1087 ctx
->print_mode
= PRINT_NORM
;
1088 ctx
->pcap
= PCAP_OPS_SG
;
1090 ctx
->dump_mode
= DUMP_INTERVAL_TIME
;
1091 ctx
->dump_interval
= 60;
1093 ctx
->promiscuous
= true;
1094 ctx
->randomize
= false;
1095 ctx
->hwtimestamp
= true;
1098 static void destroy_ctx(struct ctx
*ctx
)
1100 free(ctx
->device_in
);
1101 free(ctx
->device_out
);
1102 free(ctx
->device_trans
);
1107 static void __noreturn
help(void)
1109 printf("netsniff-ng %s, the packet sniffing beast\n", VERSION_STRING
);
1110 puts("http://www.netsniff-ng.org\n\n"
1111 "Usage: netsniff-ng [options] [filter-expression]\n"
1113 " -i|-d|--dev|--in <dev|pcap|-> Input source as netdev, pcap or pcap stdin\n"
1114 " -o|--out <dev|pcap|dir|cfg|-> Output sink as netdev, pcap, directory, trafgen, or stdout\n"
1115 " -f|--filter <bpf-file|expr> Use BPF filter file from bpfc or tcpdump-like expression\n"
1116 " -t|--type <type> Filter for: host|broadcast|multicast|others|outgoing\n"
1117 " -F|--interval <size|time> Dump interval if -o is a dir: <num>KiB/MiB/GiB/s/sec/min/hrs\n"
1118 " -R|--rfraw Capture or inject raw 802.11 frames\n"
1119 " -n|--num <0|uint> Number of packets until exit (def: 0)\n"
1120 " -P|--prefix <name> Prefix for pcaps stored in directory\n"
1121 " -T|--magic <pcap-magic> Pcap magic number/pcap format to store, see -D\n"
1122 " -D|--dump-pcap-types Dump pcap types and magic numbers and quit\n"
1123 " -B|--dump-bpf Dump generated BPF assembly\n"
1124 " -r|--rand Randomize packet forwarding order (dev->dev)\n"
1125 " -M|--no-promisc No promiscuous mode for netdev\n"
1126 " -A|--no-sock-mem Don't tune core socket memory\n"
1127 " -N|--no-hwtimestamp Disable hardware time stamping\n"
1128 " -m|--mmap Mmap(2) pcap file I/O, e.g. for replaying pcaps\n"
1129 " -G|--sg Scatter/gather pcap file I/O\n"
1130 " -c|--clrw Use slower read(2)/write(2) I/O\n"
1131 " -S|--ring-size <size> Specify ring size to: <num>KiB/MiB/GiB\n"
1132 " -k|--kernel-pull <uint> Kernel pull from user interval in us (def: 10us)\n"
1133 " -J|--jumbo-support Support replay/fwd 64KB Super Jumbo Frames (def: 2048B)\n"
1134 " -b|--bind-cpu <cpu> Bind to specific CPU\n"
1135 " -u|--user <userid> Drop privileges and change to userid\n"
1136 " -g|--group <groupid> Drop privileges and change to groupid\n"
1137 " -H|--prio-high Make this high priority process\n"
1138 " -Q|--notouch-irq Do not touch IRQ CPU affinity of NIC\n"
1139 " -s|--silent Do not print captured packets\n"
1140 " -q|--less Print less-verbose packet information\n"
1141 " -X|--hex Print packet data in hex format\n"
1142 " -l|--ascii Print human-readable packet data\n"
1143 " -U|--update Update GeoIP databases\n"
1144 " -V|--verbose Be more verbose\n"
1145 " -v|--version Show version and exit\n"
1146 " -h|--help Guess what?!\n\n"
1148 " netsniff-ng --in eth0 --out dump.pcap -s -T 0xa1b2c3d4 --b 0 tcp or udp\n"
1149 " netsniff-ng --in wlan0 --rfraw --out dump.pcap --silent --bind-cpu 0\n"
1150 " netsniff-ng --in dump.pcap --mmap --out eth0 -k1000 --silent --bind-cpu 0\n"
1151 " netsniff-ng --in dump.pcap --out dump.cfg --silent --bind-cpu 0\n"
1152 " netsniff-ng --in dump.pcap --out dump2.pcap --silent tcp\n"
1153 " netsniff-ng --in eth0 --out eth1 --silent --bind-cpu 0 -J --type host\n"
1154 " netsniff-ng --in eth1 --out /opt/probe/ -s -m --interval 100MiB -b 0\n"
1155 " netsniff-ng --in vlan0 --out dump.pcap -c -u `id -u bob` -g `id -g bob`\n"
1156 " netsniff-ng --in any --filter http.bpf --jumbo-support --ascii -V\n\n"
1158 " For introducing bit errors, delays with random variation and more\n"
1159 " while replaying pcaps, make use of tc(8) with its disciplines (e.g. netem).\n");
1164 static void __noreturn
version(void)
1166 printf("netsniff-ng %s, Git id: %s\n", VERSION_LONG
, GITVERSION
);
1167 puts("the packet sniffing beast\n"
1168 "http://www.netsniff-ng.org\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
, 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 (device_mtu(ctx
.device_in
) || !strncmp("any", ctx
.device_in
, strlen(ctx
.device_in
))) {
1429 ctx
.link_type
= pcap_devtype_to_linktype(ctx
.device_in
);
1430 if (!ctx
.device_out
) {
1432 main_loop
= recv_only_or_dump
;
1433 } else if (device_mtu(ctx
.device_out
)) {
1434 register_signal_f(SIGALRM
, timer_elapsed
, SA_SIGINFO
);
1435 main_loop
= receive_to_xmit
;
1438 register_signal_f(SIGALRM
, timer_next_dump
, SA_SIGINFO
);
1439 main_loop
= recv_only_or_dump
;
1441 ctx
.pcap
= PCAP_OPS_SG
;
1444 if (ctx
.device_out
&& device_mtu(ctx
.device_out
)) {
1445 register_signal_f(SIGALRM
, timer_elapsed
, SA_SIGINFO
);
1446 main_loop
= pcap_to_xmit
;
1448 ctx
.pcap
= PCAP_OPS_MM
;
1450 main_loop
= read_pcap
;
1452 ctx
.pcap
= PCAP_OPS_SG
;
1460 set_system_socket_memory(vals
, array_size(vals
));
1465 printf("pcap file I/O method: %s\n", pcap_ops_group_to_str
[ctx
.pcap
]);
1472 reset_system_socket_memory(vals
, array_size(vals
));
1475 device_restore_irq_affinity_list();