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"
46 char *device_in
, *device_out
, *device_trans
, *filter
, *prefix
;
47 int cpu
, rfraw
, dump
, print_mode
, dump_dir
, packet_type
, verbose
;
48 unsigned long kpull
, dump_interval
, reserve_size
, tx_bytes
, tx_packets
;
49 bool randomize
, promiscuous
, enforce
, jumbo
, dump_bpf
;
50 enum pcap_ops_groups pcap
; enum dump_mode dump_mode
;
51 uid_t uid
; gid_t gid
; uint32_t link_type
, magic
;
54 volatile sig_atomic_t sigint
= 0;
56 static volatile bool next_dump
= false;
58 static const char *short_options
= "d:i:o:rf:MJt:S:k:n:b:HQmcsqXlvhF:RGAP:Vu:g:T:DB";
59 static const struct option long_options
[] = {
60 {"dev", required_argument
, NULL
, 'd'},
61 {"in", required_argument
, NULL
, 'i'},
62 {"out", required_argument
, NULL
, 'o'},
63 {"filter", required_argument
, NULL
, 'f'},
64 {"num", required_argument
, NULL
, 'n'},
65 {"type", required_argument
, NULL
, 't'},
66 {"interval", required_argument
, NULL
, 'F'},
67 {"ring-size", required_argument
, NULL
, 'S'},
68 {"kernel-pull", required_argument
, NULL
, 'k'},
69 {"bind-cpu", required_argument
, NULL
, 'b'},
70 {"prefix", required_argument
, NULL
, 'P'},
71 {"user", required_argument
, NULL
, 'u'},
72 {"group", required_argument
, NULL
, 'g'},
73 {"magic", required_argument
, NULL
, 'T'},
74 {"rand", no_argument
, NULL
, 'r'},
75 {"rfraw", no_argument
, NULL
, 'R'},
76 {"mmap", no_argument
, NULL
, 'm'},
77 {"sg", no_argument
, NULL
, 'G'},
78 {"clrw", no_argument
, NULL
, 'c'},
79 {"jumbo-support", no_argument
, NULL
, 'J'},
80 {"no-promisc", no_argument
, NULL
, 'M'},
81 {"prio-high", no_argument
, NULL
, 'H'},
82 {"notouch-irq", no_argument
, NULL
, 'Q'},
83 {"dump-pcap-types", no_argument
, NULL
, 'D'},
84 {"dump-bpf", no_argument
, NULL
, 'B'},
85 {"silent", no_argument
, NULL
, 's'},
86 {"less", no_argument
, NULL
, 'q'},
87 {"hex", no_argument
, NULL
, 'X'},
88 {"ascii", no_argument
, NULL
, 'l'},
89 {"no-sock-mem", no_argument
, NULL
, 'A'},
90 {"update", no_argument
, NULL
, 'U'},
91 {"verbose", no_argument
, NULL
, 'V'},
92 {"version", no_argument
, NULL
, 'v'},
93 {"help", no_argument
, NULL
, 'h'},
99 static struct itimerval itimer
;
101 static unsigned long frame_count_max
= 0, interval
= TX_KERNEL_PULL_INT
;
103 #define __pcap_io pcap_ops[ctx->pcap]
105 static void signal_handler(int number
)
116 static void timer_elapsed(int unused
)
120 set_itimer_interval_value(&itimer
, 0, interval
);
122 ret
= pull_and_flush_tx_ring(tx_sock
);
123 if (unlikely(ret
< 0)) {
124 /* We could hit EBADF if the socket has been closed before
125 * the timer was triggered.
127 if (errno
!= EBADF
&& errno
!= ENOBUFS
)
128 panic("Flushing TX_RING failed: %s!\n", strerror(errno
));
131 setitimer(ITIMER_REAL
, &itimer
, NULL
);
134 static void timer_purge(void)
138 ret
= pull_and_flush_tx_ring_wait(tx_sock
);
139 if (unlikely(ret
< 0)) {
140 if (errno
!= EBADF
&& errno
!= ENOBUFS
)
141 panic("Flushing TX_RING failed: %s!\n", strerror(errno
));
144 set_itimer_interval_value(&itimer
, 0, 0);
145 setitimer(ITIMER_REAL
, &itimer
, NULL
);
149 static void timer_next_dump(int unused
)
151 set_itimer_interval_value(&itimer
, interval
, 0);
153 setitimer(ITIMER_REAL
, &itimer
, NULL
);
156 static inline bool dump_to_pcap(struct ctx
*ctx
)
161 static void pcap_to_xmit(struct ctx
*ctx
)
165 int irq
, ifindex
, fd
= 0, ret
;
166 unsigned int size
, it
= 0;
167 unsigned long trunced
= 0;
169 struct frame_map
*hdr
;
170 struct sock_fprog bpf_ops
;
171 struct timeval start
, end
, diff
;
174 if (!device_up_and_running(ctx
->device_out
) && !ctx
->rfraw
)
175 panic("Device not up and running!\n");
179 tx_sock
= pf_socket();
181 if (!strncmp("-", ctx
->device_in
, strlen("-"))) {
182 fd
= dup(fileno(stdin
));
183 close(fileno(stdin
));
184 if (ctx
->pcap
== PCAP_OPS_MM
)
185 ctx
->pcap
= PCAP_OPS_SG
;
187 fd
= open_or_die(ctx
->device_in
, O_RDONLY
| O_LARGEFILE
| O_NOATIME
);
190 if (__pcap_io
->init_once_pcap
)
191 __pcap_io
->init_once_pcap();
193 ret
= __pcap_io
->pull_fhdr_pcap(fd
, &ctx
->magic
, &ctx
->link_type
);
195 panic("Error reading pcap header!\n");
197 if (__pcap_io
->prepare_access_pcap
) {
198 ret
= __pcap_io
->prepare_access_pcap(fd
, PCAP_MODE_RD
, ctx
->jumbo
);
200 panic("Error prepare reading pcap!\n");
203 fmemset(&tx_ring
, 0, sizeof(tx_ring
));
204 fmemset(&bpf_ops
, 0, sizeof(bpf_ops
));
207 ctx
->device_trans
= xstrdup(ctx
->device_out
);
208 xfree(ctx
->device_out
);
210 enter_rfmon_mac80211(ctx
->device_trans
, &ctx
->device_out
);
211 if (ctx
->link_type
!= LINKTYPE_IEEE802_11
)
212 panic("Wrong linktype of pcap!\n");
215 ifindex
= device_ifindex(ctx
->device_out
);
217 size
= ring_size(ctx
->device_out
, ctx
->reserve_size
);
219 bpf_parse_rules(ctx
->filter
, &bpf_ops
, ctx
->link_type
);
221 bpf_dump_all(&bpf_ops
);
223 set_packet_loss_discard(tx_sock
);
225 setup_tx_ring_layout(tx_sock
, &tx_ring
, size
, ctx
->jumbo
);
226 create_tx_ring(tx_sock
, &tx_ring
, ctx
->verbose
);
227 mmap_tx_ring(tx_sock
, &tx_ring
);
228 alloc_tx_ring_frames(&tx_ring
);
229 bind_tx_ring(tx_sock
, &tx_ring
, ifindex
);
231 dissector_init_all(ctx
->print_mode
);
233 if (ctx
->cpu
>= 0 && ifindex
> 0) {
234 irq
= device_irq_number(ctx
->device_out
);
235 device_bind_irq_to_cpu(irq
, ctx
->cpu
);
238 printf("IRQ: %s:%d > CPU%d\n",
239 ctx
->device_out
, irq
, ctx
->cpu
);
243 interval
= ctx
->kpull
;
245 set_itimer_interval_value(&itimer
, 0, interval
);
246 setitimer(ITIMER_REAL
, &itimer
, NULL
);
248 drop_privileges(ctx
->enforce
, ctx
->uid
, ctx
->gid
);
250 printf("Running! Hang up with ^C!\n\n");
253 bug_on(gettimeofday(&start
, NULL
));
255 while (likely(sigint
== 0)) {
256 while (user_may_pull_from_tx(tx_ring
.frames
[it
].iov_base
)) {
257 hdr
= tx_ring
.frames
[it
].iov_base
;
258 out
= ((uint8_t *) hdr
) + TPACKET2_HDRLEN
- sizeof(struct sockaddr_ll
);
261 ret
= __pcap_io
->read_pcap(fd
, &phdr
, ctx
->magic
, out
,
262 ring_frame_size(&tx_ring
));
263 if (unlikely(ret
<= 0))
266 if (ring_frame_size(&tx_ring
) <
267 pcap_get_length(&phdr
, ctx
->magic
)) {
268 pcap_set_length(&phdr
, ctx
->magic
,
269 ring_frame_size(&tx_ring
));
272 } while (ctx
->filter
&&
273 !bpf_run_filter(&bpf_ops
, out
,
274 pcap_get_length(&phdr
, ctx
->magic
)));
276 pcap_pkthdr_to_tpacket_hdr(&phdr
, ctx
->magic
, &hdr
->tp_h
, &hdr
->s_ll
);
278 ctx
->tx_bytes
+= hdr
->tp_h
.tp_len
;;
281 show_frame_hdr(hdr
, ctx
->print_mode
);
283 dissector_entry_point(out
, hdr
->tp_h
.tp_snaplen
,
284 ctx
->link_type
, ctx
->print_mode
);
286 kernel_may_pull_from_tx(&hdr
->tp_h
);
289 if (it
>= tx_ring
.layout
.tp_frame_nr
)
292 if (unlikely(sigint
== 1))
295 if (frame_count_max
!= 0) {
296 if (ctx
->tx_packets
>= frame_count_max
) {
306 bug_on(gettimeofday(&end
, NULL
));
307 timersub(&end
, &start
, &diff
);
311 bpf_release(&bpf_ops
);
313 dissector_cleanup_all();
314 destroy_tx_ring(tx_sock
, &tx_ring
);
317 leave_rfmon_mac80211(ctx
->device_trans
, ctx
->device_out
);
319 if (__pcap_io
->prepare_close_pcap
)
320 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_RD
);
322 if (!strncmp("-", ctx
->device_in
, strlen("-")))
323 dup2(fd
, fileno(stdin
));
330 printf("\r%12lu packets outgoing\n", ctx
->tx_packets
);
331 printf("\r%12lu packets truncated in file\n", trunced
);
332 printf("\r%12lu bytes outgoing\n", ctx
->tx_bytes
);
333 printf("\r%12lu sec, %lu usec in total\n", diff
.tv_sec
, diff
.tv_usec
);
336 static void receive_to_xmit(struct ctx
*ctx
)
340 int rx_sock
, ifindex_in
, ifindex_out
;
341 unsigned int size_in
, size_out
, it_in
= 0, it_out
= 0;
342 unsigned long frame_count
= 0;
343 struct frame_map
*hdr_in
, *hdr_out
;
344 struct ring tx_ring
, rx_ring
;
345 struct pollfd rx_poll
;
346 struct sock_fprog bpf_ops
;
348 if (!strncmp(ctx
->device_in
, ctx
->device_out
, IFNAMSIZ
))
349 panic("Ingress/egress devices must be different!\n");
350 if (!device_up_and_running(ctx
->device_out
))
351 panic("Egress device not up and running!\n");
353 rx_sock
= pf_socket();
354 tx_sock
= pf_socket();
356 fmemset(&tx_ring
, 0, sizeof(tx_ring
));
357 fmemset(&rx_ring
, 0, sizeof(rx_ring
));
358 fmemset(&rx_poll
, 0, sizeof(rx_poll
));
359 fmemset(&bpf_ops
, 0, sizeof(bpf_ops
));
361 ifindex_in
= device_ifindex(ctx
->device_in
);
362 ifindex_out
= device_ifindex(ctx
->device_out
);
364 size_in
= ring_size(ctx
->device_in
, ctx
->reserve_size
);
365 size_out
= ring_size(ctx
->device_out
, ctx
->reserve_size
);
367 enable_kernel_bpf_jit_compiler();
369 bpf_parse_rules(ctx
->filter
, &bpf_ops
, ctx
->link_type
);
371 bpf_dump_all(&bpf_ops
);
372 bpf_attach_to_sock(rx_sock
, &bpf_ops
);
374 setup_rx_ring_layout(rx_sock
, &rx_ring
, size_in
, ctx
->jumbo
);
375 create_rx_ring(rx_sock
, &rx_ring
, ctx
->verbose
);
376 mmap_rx_ring(rx_sock
, &rx_ring
);
377 alloc_rx_ring_frames(&rx_ring
);
378 bind_rx_ring(rx_sock
, &rx_ring
, ifindex_in
);
379 prepare_polling(rx_sock
, &rx_poll
);
381 set_packet_loss_discard(tx_sock
);
382 setup_tx_ring_layout(tx_sock
, &tx_ring
, size_out
, ctx
->jumbo
);
383 create_tx_ring(tx_sock
, &tx_ring
, ctx
->verbose
);
384 mmap_tx_ring(tx_sock
, &tx_ring
);
385 alloc_tx_ring_frames(&tx_ring
);
386 bind_tx_ring(tx_sock
, &tx_ring
, ifindex_out
);
388 dissector_init_all(ctx
->print_mode
);
390 if (ctx
->promiscuous
)
391 ifflags
= enter_promiscuous_mode(ctx
->device_in
);
394 interval
= ctx
->kpull
;
396 set_itimer_interval_value(&itimer
, 0, interval
);
397 setitimer(ITIMER_REAL
, &itimer
, NULL
);
399 drop_privileges(ctx
->enforce
, ctx
->uid
, ctx
->gid
);
401 printf("Running! Hang up with ^C!\n\n");
404 while (likely(sigint
== 0)) {
405 while (user_may_pull_from_rx(rx_ring
.frames
[it_in
].iov_base
)) {
408 hdr_in
= rx_ring
.frames
[it_in
].iov_base
;
409 in
= ((uint8_t *) hdr_in
) + hdr_in
->tp_h
.tp_mac
;
413 if (ctx
->packet_type
!= -1)
414 if (ctx
->packet_type
!= hdr_in
->s_ll
.sll_pkttype
)
417 hdr_out
= tx_ring
.frames
[it_out
].iov_base
;
418 out
= ((uint8_t *) hdr_out
) + TPACKET2_HDRLEN
- sizeof(struct sockaddr_ll
);
420 for (; !user_may_pull_from_tx(tx_ring
.frames
[it_out
].iov_base
) &&
423 next_rnd_slot(&it_out
, &tx_ring
);
426 if (it_out
>= tx_ring
.layout
.tp_frame_nr
)
430 hdr_out
= tx_ring
.frames
[it_out
].iov_base
;
431 out
= ((uint8_t *) hdr_out
) + TPACKET2_HDRLEN
- sizeof(struct sockaddr_ll
);
434 tpacket_hdr_clone(&hdr_out
->tp_h
, &hdr_in
->tp_h
);
435 fmemcpy(out
, in
, hdr_in
->tp_h
.tp_len
);
437 kernel_may_pull_from_tx(&hdr_out
->tp_h
);
439 next_rnd_slot(&it_out
, &tx_ring
);
442 if (it_out
>= tx_ring
.layout
.tp_frame_nr
)
446 show_frame_hdr(hdr_in
, ctx
->print_mode
);
448 dissector_entry_point(in
, hdr_in
->tp_h
.tp_snaplen
,
449 ctx
->link_type
, ctx
->print_mode
);
451 if (frame_count_max
!= 0) {
452 if (frame_count
>= frame_count_max
) {
460 kernel_may_pull_from_rx(&hdr_in
->tp_h
);
463 if (it_in
>= rx_ring
.layout
.tp_frame_nr
)
466 if (unlikely(sigint
== 1))
470 poll(&rx_poll
, 1, -1);
477 sock_print_net_stats(rx_sock
, 0);
479 bpf_release(&bpf_ops
);
481 dissector_cleanup_all();
483 destroy_tx_ring(tx_sock
, &tx_ring
);
484 destroy_rx_ring(rx_sock
, &rx_ring
);
486 if (ctx
->promiscuous
)
487 leave_promiscuous_mode(ctx
->device_in
, ifflags
);
493 static void translate_pcap_to_txf(int fdo
, uint8_t *out
, size_t len
)
495 size_t bytes_done
= 0;
498 slprintf(bout
, sizeof(bout
), "{\n ");
499 write_or_die(fdo
, bout
, strlen(bout
));
501 while (bytes_done
< len
) {
502 slprintf(bout
, sizeof(bout
), "0x%02x, ", out
[bytes_done
]);
503 write_or_die(fdo
, bout
, strlen(bout
));
507 if (bytes_done
% 10 == 0) {
508 slprintf(bout
, sizeof(bout
), "\n");
509 write_or_die(fdo
, bout
, strlen(bout
));
511 if (bytes_done
< len
) {
512 slprintf(bout
, sizeof(bout
), " ");
513 write_or_die(fdo
, bout
, strlen(bout
));
517 if (bytes_done
% 10 != 0) {
518 slprintf(bout
, sizeof(bout
), "\n");
519 write_or_die(fdo
, bout
, strlen(bout
));
522 slprintf(bout
, sizeof(bout
), "}\n\n");
523 write_or_die(fdo
, bout
, strlen(bout
));
526 static void read_pcap(struct ctx
*ctx
)
530 int ret
, fd
, fdo
= 0;
531 unsigned long trunced
= 0;
534 struct sock_fprog bpf_ops
;
536 struct timeval start
, end
, diff
;
537 struct sockaddr_ll sll
;
541 if (!strncmp("-", ctx
->device_in
, strlen("-"))) {
542 fd
= dup(fileno(stdin
));
543 close(fileno(stdin
));
544 if (ctx
->pcap
== PCAP_OPS_MM
)
545 ctx
->pcap
= PCAP_OPS_SG
;
547 fd
= open_or_die(ctx
->device_in
, O_RDONLY
| O_LARGEFILE
| O_NOATIME
);
550 if (__pcap_io
->init_once_pcap
)
551 __pcap_io
->init_once_pcap();
553 ret
= __pcap_io
->pull_fhdr_pcap(fd
, &ctx
->magic
, &ctx
->link_type
);
555 panic("Error reading pcap header!\n");
557 if (__pcap_io
->prepare_access_pcap
) {
558 ret
= __pcap_io
->prepare_access_pcap(fd
, PCAP_MODE_RD
, ctx
->jumbo
);
560 panic("Error prepare reading pcap!\n");
563 fmemset(&fm
, 0, sizeof(fm
));
564 fmemset(&bpf_ops
, 0, sizeof(bpf_ops
));
566 bpf_parse_rules(ctx
->filter
, &bpf_ops
, ctx
->link_type
);
568 bpf_dump_all(&bpf_ops
);
570 dissector_init_all(ctx
->print_mode
);
572 out_len
= round_up(1024 * 1024, PAGE_SIZE
);
573 out
= xmalloc_aligned(out_len
, CO_CACHE_LINE_SIZE
);
575 if (ctx
->device_out
) {
576 if (!strncmp("-", ctx
->device_out
, strlen("-"))) {
577 fdo
= dup(fileno(stdout
));
578 close(fileno(stdout
));
580 fdo
= open_or_die_m(ctx
->device_out
, O_RDWR
| O_CREAT
|
581 O_TRUNC
| O_LARGEFILE
, DEFFILEMODE
);
585 drop_privileges(ctx
->enforce
, ctx
->uid
, ctx
->gid
);
587 printf("Running! Hang up with ^C!\n\n");
590 bug_on(gettimeofday(&start
, NULL
));
592 while (likely(sigint
== 0)) {
594 ret
= __pcap_io
->read_pcap(fd
, &phdr
, ctx
->magic
,
596 if (unlikely(ret
< 0))
599 if (unlikely(pcap_get_length(&phdr
, ctx
->magic
) == 0)) {
604 if (unlikely(pcap_get_length(&phdr
, ctx
->magic
) > out_len
)) {
605 pcap_set_length(&phdr
, ctx
->magic
, out_len
);
608 } while (ctx
->filter
&&
609 !bpf_run_filter(&bpf_ops
, out
,
610 pcap_get_length(&phdr
, ctx
->magic
)));
612 pcap_pkthdr_to_tpacket_hdr(&phdr
, ctx
->magic
, &fm
.tp_h
, &sll
);
614 ctx
->tx_bytes
+= fm
.tp_h
.tp_len
;
617 show_frame_hdr(&fm
, ctx
->print_mode
);
619 dissector_entry_point(out
, fm
.tp_h
.tp_snaplen
,
620 ctx
->link_type
, ctx
->print_mode
);
623 translate_pcap_to_txf(fdo
, out
, fm
.tp_h
.tp_snaplen
);
625 if (frame_count_max
!= 0) {
626 if (ctx
->tx_packets
>= frame_count_max
) {
635 bug_on(gettimeofday(&end
, NULL
));
636 timersub(&end
, &start
, &diff
);
638 bpf_release(&bpf_ops
);
640 dissector_cleanup_all();
642 if (__pcap_io
->prepare_close_pcap
)
643 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_RD
);
649 printf("\r%12lu packets outgoing\n", ctx
->tx_packets
);
650 printf("\r%12lu packets truncated in file\n", trunced
);
651 printf("\r%12lu bytes outgoing\n", ctx
->tx_bytes
);
652 printf("\r%12lu sec, %lu usec in total\n", diff
.tv_sec
, diff
.tv_usec
);
654 if (!strncmp("-", ctx
->device_in
, strlen("-")))
655 dup2(fd
, fileno(stdin
));
658 if (ctx
->device_out
) {
659 if (!strncmp("-", ctx
->device_out
, strlen("-")))
660 dup2(fdo
, fileno(stdout
));
665 static void finish_multi_pcap_file(struct ctx
*ctx
, int fd
)
667 __pcap_io
->fsync_pcap(fd
);
669 if (__pcap_io
->prepare_close_pcap
)
670 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_WR
);
674 fmemset(&itimer
, 0, sizeof(itimer
));
675 setitimer(ITIMER_REAL
, &itimer
, NULL
);
678 static int next_multi_pcap_file(struct ctx
*ctx
, int fd
)
683 __pcap_io
->fsync_pcap(fd
);
685 if (__pcap_io
->prepare_close_pcap
)
686 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_WR
);
690 slprintf(fname
, sizeof(fname
), "%s/%s%lu.pcap", ctx
->device_out
,
691 ctx
->prefix
? : "dump-", time(0));
693 fd
= open_or_die_m(fname
, O_RDWR
| O_CREAT
| O_TRUNC
|
694 O_LARGEFILE
, DEFFILEMODE
);
696 ret
= __pcap_io
->push_fhdr_pcap(fd
, ctx
->magic
, ctx
->link_type
);
698 panic("Error writing pcap header!\n");
700 if (__pcap_io
->prepare_access_pcap
) {
701 ret
= __pcap_io
->prepare_access_pcap(fd
, PCAP_MODE_WR
, ctx
->jumbo
);
703 panic("Error prepare writing pcap!\n");
709 static int begin_multi_pcap_file(struct ctx
*ctx
)
716 if (ctx
->device_out
[strlen(ctx
->device_out
) - 1] == '/')
717 ctx
->device_out
[strlen(ctx
->device_out
) - 1] = 0;
719 slprintf(fname
, sizeof(fname
), "%s/%s%lu.pcap", ctx
->device_out
,
720 ctx
->prefix
? : "dump-", time(0));
722 fd
= open_or_die_m(fname
, O_RDWR
| O_CREAT
| O_TRUNC
|
723 O_LARGEFILE
, DEFFILEMODE
);
725 ret
= __pcap_io
->push_fhdr_pcap(fd
, ctx
->magic
, ctx
->link_type
);
727 panic("Error writing pcap header!\n");
729 if (__pcap_io
->prepare_access_pcap
) {
730 ret
= __pcap_io
->prepare_access_pcap(fd
, PCAP_MODE_WR
, ctx
->jumbo
);
732 panic("Error prepare writing pcap!\n");
735 if (ctx
->dump_mode
== DUMP_INTERVAL_TIME
) {
736 interval
= ctx
->dump_interval
;
738 set_itimer_interval_value(&itimer
, interval
, 0);
739 setitimer(ITIMER_REAL
, &itimer
, NULL
);
747 static void finish_single_pcap_file(struct ctx
*ctx
, int fd
)
749 __pcap_io
->fsync_pcap(fd
);
751 if (__pcap_io
->prepare_close_pcap
)
752 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_WR
);
754 if (strncmp("-", ctx
->device_out
, strlen("-")))
757 dup2(fd
, fileno(stdout
));
760 static int begin_single_pcap_file(struct ctx
*ctx
)
766 if (!strncmp("-", ctx
->device_out
, strlen("-"))) {
767 fd
= dup(fileno(stdout
));
768 close(fileno(stdout
));
769 if (ctx
->pcap
== PCAP_OPS_MM
)
770 ctx
->pcap
= PCAP_OPS_SG
;
772 fd
= open_or_die_m(ctx
->device_out
,
773 O_RDWR
| O_CREAT
| O_TRUNC
|
774 O_LARGEFILE
, DEFFILEMODE
);
777 ret
= __pcap_io
->push_fhdr_pcap(fd
, ctx
->magic
, ctx
->link_type
);
779 panic("Error writing pcap header!\n");
781 if (__pcap_io
->prepare_access_pcap
) {
782 ret
= __pcap_io
->prepare_access_pcap(fd
, PCAP_MODE_WR
, ctx
->jumbo
);
784 panic("Error prepare writing pcap!\n");
790 static void print_pcap_file_stats(int sock
, struct ctx
*ctx
, unsigned long skipped
)
793 unsigned long good
, bad
;
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 good
= kstats
.tp_packets
- kstats
.tp_drops
- skipped
;
805 bad
= kstats
.tp_drops
+ skipped
;
807 printf(".(+%lu/-%lu)", good
, bad
);
812 static void recv_only_or_dump(struct ctx
*ctx
)
816 int sock
, irq
, ifindex
, fd
= 0, ret
;
817 unsigned int size
, it
= 0;
818 unsigned long frame_count
= 0, skipped
= 0;
820 struct pollfd rx_poll
;
821 struct frame_map
*hdr
;
822 struct sock_fprog bpf_ops
;
823 struct timeval start
, end
, diff
;
829 ctx
->device_trans
= xstrdup(ctx
->device_in
);
830 xfree(ctx
->device_in
);
832 enter_rfmon_mac80211(ctx
->device_trans
, &ctx
->device_in
);
833 ctx
->link_type
= LINKTYPE_IEEE802_11
;
836 fmemset(&rx_ring
, 0, sizeof(rx_ring
));
837 fmemset(&rx_poll
, 0, sizeof(rx_poll
));
838 fmemset(&bpf_ops
, 0, sizeof(bpf_ops
));
840 ifindex
= device_ifindex(ctx
->device_in
);
842 size
= ring_size(ctx
->device_in
, ctx
->reserve_size
);
844 enable_kernel_bpf_jit_compiler();
846 bpf_parse_rules(ctx
->filter
, &bpf_ops
, ctx
->link_type
);
848 bpf_dump_all(&bpf_ops
);
849 bpf_attach_to_sock(sock
, &bpf_ops
);
851 set_sockopt_hwtimestamp(sock
, ctx
->device_in
);
853 setup_rx_ring_layout(sock
, &rx_ring
, size
, ctx
->jumbo
);
854 create_rx_ring(sock
, &rx_ring
, ctx
->verbose
);
855 mmap_rx_ring(sock
, &rx_ring
);
856 alloc_rx_ring_frames(&rx_ring
);
857 bind_rx_ring(sock
, &rx_ring
, ifindex
);
859 prepare_polling(sock
, &rx_poll
);
860 dissector_init_all(ctx
->print_mode
);
862 if (ctx
->cpu
>= 0 && ifindex
> 0) {
863 irq
= device_irq_number(ctx
->device_in
);
864 device_bind_irq_to_cpu(irq
, ctx
->cpu
);
867 printf("IRQ: %s:%d > CPU%d\n",
868 ctx
->device_in
, irq
, ctx
->cpu
);
871 if (ctx
->promiscuous
)
872 ifflags
= enter_promiscuous_mode(ctx
->device_in
);
874 if (dump_to_pcap(ctx
) && __pcap_io
->init_once_pcap
)
875 __pcap_io
->init_once_pcap();
877 drop_privileges(ctx
->enforce
, ctx
->uid
, ctx
->gid
);
879 if (dump_to_pcap(ctx
)) {
883 fmemset(&stats
, 0, sizeof(stats
));
884 ret
= stat(ctx
->device_out
, &stats
);
890 ctx
->dump_dir
= S_ISDIR(stats
.st_mode
);
892 fd
= begin_multi_pcap_file(ctx
);
895 fd
= begin_single_pcap_file(ctx
);
899 printf("Running! Hang up with ^C!\n\n");
902 bug_on(gettimeofday(&start
, NULL
));
904 while (likely(sigint
== 0)) {
905 while (user_may_pull_from_rx(rx_ring
.frames
[it
].iov_base
)) {
908 hdr
= rx_ring
.frames
[it
].iov_base
;
909 packet
= ((uint8_t *) hdr
) + hdr
->tp_h
.tp_mac
;
912 if (ctx
->packet_type
!= -1)
913 if (ctx
->packet_type
!= hdr
->s_ll
.sll_pkttype
)
916 if (unlikely(ring_frame_size(&rx_ring
) < hdr
->tp_h
.tp_snaplen
)) {
921 if (dump_to_pcap(ctx
)) {
922 tpacket_hdr_to_pcap_pkthdr(&hdr
->tp_h
, &hdr
->s_ll
, &phdr
, ctx
->magic
);
924 ret
= __pcap_io
->write_pcap(fd
, &phdr
, ctx
->magic
, packet
,
925 pcap_get_length(&phdr
, ctx
->magic
));
926 if (unlikely(ret
!= pcap_get_total_length(&phdr
, ctx
->magic
)))
927 panic("Write error to pcap!\n");
930 show_frame_hdr(hdr
, ctx
->print_mode
);
932 dissector_entry_point(packet
, hdr
->tp_h
.tp_snaplen
,
933 ctx
->link_type
, ctx
->print_mode
);
935 if (frame_count_max
!= 0) {
936 if (frame_count
>= frame_count_max
) {
944 kernel_may_pull_from_rx(&hdr
->tp_h
);
947 if (it
>= rx_ring
.layout
.tp_frame_nr
)
950 if (unlikely(sigint
== 1))
953 if (dump_to_pcap(ctx
)) {
954 if (ctx
->dump_mode
== DUMP_INTERVAL_SIZE
) {
955 interval
+= hdr
->tp_h
.tp_snaplen
;
957 if (interval
> ctx
->dump_interval
) {
964 fd
= next_multi_pcap_file(ctx
, fd
);
968 print_pcap_file_stats(sock
, ctx
, skipped
);
973 poll(&rx_poll
, 1, -1);
976 bug_on(gettimeofday(&end
, NULL
));
977 timersub(&end
, &start
, &diff
);
979 if (!(ctx
->dump_dir
&& ctx
->print_mode
== PRINT_NONE
)) {
980 sock_print_net_stats(sock
, skipped
);
982 printf("\r%12lu sec, %lu usec in total\n",
983 diff
.tv_sec
, diff
.tv_usec
);
989 bpf_release(&bpf_ops
);
990 dissector_cleanup_all();
991 destroy_rx_ring(sock
, &rx_ring
);
993 if (ctx
->promiscuous
)
994 leave_promiscuous_mode(ctx
->device_in
, ifflags
);
997 leave_rfmon_mac80211(ctx
->device_trans
, ctx
->device_in
);
999 if (dump_to_pcap(ctx
)) {
1001 finish_multi_pcap_file(ctx
, fd
);
1003 finish_single_pcap_file(ctx
, fd
);
1009 static void help(void)
1011 printf("\nnetsniff-ng %s, the packet sniffing beast\n", VERSION_STRING
);
1012 puts("http://www.netsniff-ng.org\n\n"
1013 "Usage: netsniff-ng [options] [filter-expression]\n"
1015 " -i|-d|--dev|--in <dev|pcap|-> Input source as netdev, pcap or pcap stdin\n"
1016 " -o|--out <dev|pcap|dir|cfg|-> Output sink as netdev, pcap, directory, trafgen, or stdout\n"
1017 " -f|--filter <bpf-file|expr> Use BPF filter file from bpfc or tcpdump-like expression\n"
1018 " -t|--type <type> Filter for: host|broadcast|multicast|others|outgoing\n"
1019 " -F|--interval <size|time> Dump interval if -o is a dir: <num>KiB/MiB/GiB/s/sec/min/hrs\n"
1020 " -J|--jumbo-support Support for 64KB Super Jumbo Frames (def: 2048B)\n"
1021 " -R|--rfraw Capture or inject raw 802.11 frames\n"
1022 " -n|--num <0|uint> Number of packets until exit (def: 0)\n"
1023 " -P|--prefix <name> Prefix for pcaps stored in directory\n"
1024 " -T|--magic <pcap-magic> Pcap magic number/pcap format to store, see -D\n"
1025 " -D|--dump-pcap-types Dump pcap types and magic numbers and quit\n"
1026 " -B|--dump-bpf Dump generated BPF assembly\n"
1027 " -r|--rand Randomize packet forwarding order (dev->dev)\n"
1028 " -M|--no-promisc No promiscuous mode for netdev\n"
1029 " -A|--no-sock-mem Don't tune core socket memory\n"
1030 " -m|--mmap Mmap(2) pcap file i.e., for replaying pcaps\n"
1031 " -G|--sg Scatter/gather pcap file I/O\n"
1032 " -c|--clrw Use slower read(2)/write(2) I/O\n"
1033 " -S|--ring-size <size> Specify ring size to: <num>KiB/MiB/GiB\n"
1034 " -k|--kernel-pull <uint> Kernel pull from user interval in us (def: 10us)\n"
1035 " -b|--bind-cpu <cpu> Bind to specific CPU\n"
1036 " -u|--user <userid> Drop privileges and change to userid\n"
1037 " -g|--group <groupid> Drop privileges and change to groupid\n"
1038 " -H|--prio-high Make this high priority process\n"
1039 " -Q|--notouch-irq Do not touch IRQ CPU affinity of NIC\n"
1040 " -s|--silent Do not print captured packets\n"
1041 " -q|--less Print less-verbose packet information\n"
1042 " -X|--hex Print packet data in hex format\n"
1043 " -l|--ascii Print human-readable packet data\n"
1044 " -U|--update Update GeoIP databases\n"
1045 " -V|--verbose Be more verbose\n"
1046 " -v|--version Show version\n"
1047 " -h|--help Guess what?!\n\n"
1049 " netsniff-ng --in eth0 --out dump.pcap -s -T 0xa1b2c3d4 --b 0 tcp or udp\n"
1050 " netsniff-ng --in wlan0 --rfraw --out dump.pcap --silent --bind-cpu 0\n"
1051 " netsniff-ng --in dump.pcap --mmap --out eth0 -k1000 --silent --bind-cpu 0\n"
1052 " netsniff-ng --in dump.pcap --out dump.cfg --silent --bind-cpu 0\n"
1053 " netsniff-ng --in eth0 --out eth1 --silent --bind-cpu 0 --type host\n"
1054 " netsniff-ng --in eth1 --out /opt/probe/ -s -m -J --interval 100MiB -b 0\n"
1055 " netsniff-ng --in vlan0 --out dump.pcap -c -u `id -u bob` -g `id -g bob`\n"
1056 " netsniff-ng --in any --filter http.bpf --jumbo-support --ascii -V\n\n"
1058 " For introducing bit errors, delays with random variation and more\n"
1059 " while replaying pcaps, make use of tc(8) with its disciplines (e.g. netem).\n\n"
1060 "Please report bugs to <bugs@netsniff-ng.org>\n"
1061 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
1062 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel.roullit@gmail.com>\n"
1063 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1064 "Swiss federal institute of technology (ETH Zurich)\n"
1065 "License: GNU GPL version 2.0\n"
1066 "This is free software: you are free to change and redistribute it.\n"
1067 "There is NO WARRANTY, to the extent permitted by law.\n");
1071 static void version(void)
1073 printf("\nnetsniff-ng %s, the packet sniffing beast\n", VERSION_STRING
);
1074 puts("http://www.netsniff-ng.org\n\n"
1075 "Please report bugs to <bugs@netsniff-ng.org>\n"
1076 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
1077 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel.roullit@gmail.com>\n"
1078 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1079 "Swiss federal institute of technology (ETH Zurich)\n"
1080 "License: GNU GPL version 2.0\n"
1081 "This is free software: you are free to change and redistribute it.\n"
1082 "There is NO WARRANTY, to the extent permitted by law.\n");
1086 int main(int argc
, char **argv
)
1089 int c
, i
, j
, cpu_tmp
, opt_index
, ops_touched
= 0, vals
[4] = {0};
1090 bool prio_high
= false, setsockmem
= true;
1091 void (*main_loop
)(struct ctx
*ctx
) = NULL
;
1093 .link_type
= LINKTYPE_EN10MB
,
1094 .print_mode
= PRINT_NORM
,
1097 .promiscuous
= true,
1099 .pcap
= PCAP_OPS_SG
,
1100 .dump_interval
= 60,
1101 .dump_mode
= DUMP_INTERVAL_TIME
,
1104 .magic
= ORIGINAL_TCPDUMP_MAGIC
,
1109 while ((c
= getopt_long(argc
, argv
, short_options
, long_options
,
1110 &opt_index
)) != EOF
) {
1114 ctx
.device_in
= xstrdup(optarg
);
1117 ctx
.device_out
= xstrdup(optarg
);
1120 ctx
.prefix
= xstrdup(optarg
);
1123 ctx
.link_type
= LINKTYPE_IEEE802_11
;
1127 ctx
.randomize
= true;
1133 ctx
.magic
= (uint32_t) strtoul(optarg
, NULL
, 0);
1134 pcap_check_magic(ctx
.magic
);
1137 ctx
.filter
= xstrdup(optarg
);
1140 ctx
.promiscuous
= false;
1146 ctx
.uid
= strtoul(optarg
, NULL
, 0);
1150 ctx
.gid
= strtoul(optarg
, NULL
, 0);
1154 if (!strncmp(optarg
, "host", strlen("host")))
1155 ctx
.packet_type
= PACKET_HOST
;
1156 else if (!strncmp(optarg
, "broadcast", strlen("broadcast")))
1157 ctx
.packet_type
= PACKET_BROADCAST
;
1158 else if (!strncmp(optarg
, "multicast", strlen("multicast")))
1159 ctx
.packet_type
= PACKET_MULTICAST
;
1160 else if (!strncmp(optarg
, "others", strlen("others")))
1161 ctx
.packet_type
= PACKET_OTHERHOST
;
1162 else if (!strncmp(optarg
, "outgoing", strlen("outgoing")))
1163 ctx
.packet_type
= PACKET_OUTGOING
;
1165 ctx
.packet_type
= -1;
1169 ctx
.reserve_size
= 0;
1171 for (j
= i
= strlen(optarg
); i
> 0; --i
) {
1172 if (!isdigit(optarg
[j
- i
]))
1177 if (!strncmp(ptr
, "KiB", strlen("KiB")))
1178 ctx
.reserve_size
= 1 << 10;
1179 else if (!strncmp(ptr
, "MiB", strlen("MiB")))
1180 ctx
.reserve_size
= 1 << 20;
1181 else if (!strncmp(ptr
, "GiB", strlen("GiB")))
1182 ctx
.reserve_size
= 1 << 30;
1184 panic("Syntax error in ring size param!\n");
1187 ctx
.reserve_size
*= strtol(optarg
, NULL
, 0);
1190 cpu_tmp
= strtol(optarg
, NULL
, 0);
1192 cpu_affinity(cpu_tmp
);
1200 ctx
.pcap
= PCAP_OPS_RW
;
1204 ctx
.pcap
= PCAP_OPS_MM
;
1208 ctx
.pcap
= PCAP_OPS_SG
;
1215 ctx
.print_mode
= PRINT_NONE
;
1218 ctx
.print_mode
= PRINT_LESS
;
1222 (ctx
.print_mode
== PRINT_ASCII
) ?
1223 PRINT_HEX_ASCII
: PRINT_HEX
;
1227 (ctx
.print_mode
== PRINT_HEX
) ?
1228 PRINT_HEX_ASCII
: PRINT_ASCII
;
1231 ctx
.kpull
= strtol(optarg
, NULL
, 0);
1234 frame_count_max
= strtol(optarg
, NULL
, 0);
1238 ctx
.dump_interval
= 0;
1240 for (j
= i
= strlen(optarg
); i
> 0; --i
) {
1241 if (!isdigit(optarg
[j
- i
]))
1246 if (!strncmp(ptr
, "KiB", strlen("KiB"))) {
1247 ctx
.dump_interval
= 1 << 10;
1248 ctx
.dump_mode
= DUMP_INTERVAL_SIZE
;
1249 } else if (!strncmp(ptr
, "MiB", strlen("MiB"))) {
1250 ctx
.dump_interval
= 1 << 20;
1251 ctx
.dump_mode
= DUMP_INTERVAL_SIZE
;
1252 } else if (!strncmp(ptr
, "GiB", strlen("GiB"))) {
1253 ctx
.dump_interval
= 1 << 30;
1254 ctx
.dump_mode
= DUMP_INTERVAL_SIZE
;
1255 } else if (!strncmp(ptr
, "sec", strlen("sec"))) {
1256 ctx
.dump_interval
= 1;
1257 ctx
.dump_mode
= DUMP_INTERVAL_TIME
;
1258 } else if (!strncmp(ptr
, "min", strlen("min"))) {
1259 ctx
.dump_interval
= 60;
1260 ctx
.dump_mode
= DUMP_INTERVAL_TIME
;
1261 } else if (!strncmp(ptr
, "hrs", strlen("hrs"))) {
1262 ctx
.dump_interval
= 60 * 60;
1263 ctx
.dump_mode
= DUMP_INTERVAL_TIME
;
1264 } else if (!strncmp(ptr
, "s", strlen("s"))) {
1265 ctx
.dump_interval
= 1;
1266 ctx
.dump_mode
= DUMP_INTERVAL_TIME
;
1268 panic("Syntax error in time/size param!\n");
1272 ctx
.dump_interval
*= strtol(optarg
, NULL
, 0);
1278 ctx
.dump_bpf
= true;
1281 pcap_dump_type_features();
1311 panic("Option -%c requires an argument!\n",
1314 if (isprint(optopt
))
1315 printf("Unknown option character `0x%X\'!\n", optopt
);
1323 if (!ctx
.filter
&& optind
!= argc
) {
1327 for (i
= optind
; i
< argc
; ++i
) {
1328 size_t alen
= strlen(argv
[i
]) + 2;
1329 size_t flen
= ctx
.filter
? strlen(ctx
.filter
) : 0;
1331 ctx
.filter
= xrealloc(ctx
.filter
, 1, flen
+ alen
);
1332 ret
= slprintf(ctx
.filter
+ offset
, strlen(argv
[i
]) + 2, "%s ", argv
[i
]);
1334 panic("Cannot concatenate filter string!\n");
1341 ctx
.device_in
= xstrdup("any");
1343 register_signal(SIGINT
, signal_handler
);
1344 register_signal(SIGHUP
, signal_handler
);
1349 set_proc_prio(get_default_proc_prio());
1350 set_sched_status(get_default_sched_policy(), get_default_sched_prio());
1353 if (ctx
.device_in
&& (device_mtu(ctx
.device_in
) ||
1354 !strncmp("any", ctx
.device_in
, strlen(ctx
.device_in
)))) {
1355 if (!ctx
.device_out
) {
1357 main_loop
= recv_only_or_dump
;
1358 } else if (device_mtu(ctx
.device_out
)) {
1359 register_signal_f(SIGALRM
, timer_elapsed
, SA_SIGINFO
);
1360 main_loop
= receive_to_xmit
;
1363 register_signal_f(SIGALRM
, timer_next_dump
, SA_SIGINFO
);
1364 main_loop
= recv_only_or_dump
;
1366 ctx
.pcap
= PCAP_OPS_SG
;
1369 if (ctx
.device_out
&& device_mtu(ctx
.device_out
)) {
1370 register_signal_f(SIGALRM
, timer_elapsed
, SA_SIGINFO
);
1371 main_loop
= pcap_to_xmit
;
1373 ctx
.pcap
= PCAP_OPS_MM
;
1375 main_loop
= read_pcap
;
1377 ctx
.pcap
= PCAP_OPS_SG
;
1385 set_system_socket_memory(vals
, array_size(vals
));
1394 reset_system_socket_memory(vals
, array_size(vals
));
1399 free(ctx
.device_in
);
1400 free(ctx
.device_out
);
1401 free(ctx
.device_trans
);