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 ret
= __pcap_io
->pull_fhdr_pcap(fd
, &ctx
->magic
, &ctx
->link_type
);
192 panic("Error reading pcap header!\n");
194 if (__pcap_io
->prepare_access_pcap
) {
195 ret
= __pcap_io
->prepare_access_pcap(fd
, PCAP_MODE_RD
, ctx
->jumbo
);
197 panic("Error prepare reading pcap!\n");
200 fmemset(&tx_ring
, 0, sizeof(tx_ring
));
201 fmemset(&bpf_ops
, 0, sizeof(bpf_ops
));
204 ctx
->device_trans
= xstrdup(ctx
->device_out
);
205 xfree(ctx
->device_out
);
207 enter_rfmon_mac80211(ctx
->device_trans
, &ctx
->device_out
);
208 if (ctx
->link_type
!= LINKTYPE_IEEE802_11
)
209 panic("Wrong linktype of pcap!\n");
212 ifindex
= device_ifindex(ctx
->device_out
);
214 size
= ring_size(ctx
->device_out
, ctx
->reserve_size
);
216 bpf_parse_rules(ctx
->filter
, &bpf_ops
, ctx
->link_type
);
218 bpf_dump_all(&bpf_ops
);
220 set_packet_loss_discard(tx_sock
);
221 set_sockopt_hwtimestamp(tx_sock
, ctx
->device_out
);
223 setup_tx_ring_layout(tx_sock
, &tx_ring
, size
, ctx
->jumbo
);
224 create_tx_ring(tx_sock
, &tx_ring
, ctx
->verbose
);
225 mmap_tx_ring(tx_sock
, &tx_ring
);
226 alloc_tx_ring_frames(&tx_ring
);
227 bind_tx_ring(tx_sock
, &tx_ring
, ifindex
);
229 dissector_init_all(ctx
->print_mode
);
231 if (ctx
->cpu
>= 0 && ifindex
> 0) {
232 irq
= device_irq_number(ctx
->device_out
);
233 device_bind_irq_to_cpu(irq
, ctx
->cpu
);
236 printf("IRQ: %s:%d > CPU%d\n",
237 ctx
->device_out
, irq
, ctx
->cpu
);
241 interval
= ctx
->kpull
;
243 set_itimer_interval_value(&itimer
, 0, interval
);
244 setitimer(ITIMER_REAL
, &itimer
, NULL
);
246 drop_privileges(ctx
->enforce
, ctx
->uid
, ctx
->gid
);
248 printf("Running! Hang up with ^C!\n\n");
251 bug_on(gettimeofday(&start
, NULL
));
253 while (likely(sigint
== 0)) {
254 while (user_may_pull_from_tx(tx_ring
.frames
[it
].iov_base
)) {
255 hdr
= tx_ring
.frames
[it
].iov_base
;
256 out
= ((uint8_t *) hdr
) + TPACKET2_HDRLEN
- sizeof(struct sockaddr_ll
);
259 ret
= __pcap_io
->read_pcap(fd
, &phdr
, ctx
->magic
, out
,
260 ring_frame_size(&tx_ring
));
261 if (unlikely(ret
<= 0))
264 if (ring_frame_size(&tx_ring
) <
265 pcap_get_length(&phdr
, ctx
->magic
)) {
266 pcap_set_length(&phdr
, ctx
->magic
,
267 ring_frame_size(&tx_ring
));
270 } while (ctx
->filter
&&
271 !bpf_run_filter(&bpf_ops
, out
,
272 pcap_get_length(&phdr
, ctx
->magic
)));
274 pcap_pkthdr_to_tpacket_hdr(&phdr
, ctx
->magic
, &hdr
->tp_h
, &hdr
->s_ll
);
276 ctx
->tx_bytes
+= hdr
->tp_h
.tp_len
;;
279 show_frame_hdr(hdr
, ctx
->print_mode
);
281 dissector_entry_point(out
, hdr
->tp_h
.tp_snaplen
,
282 ctx
->link_type
, ctx
->print_mode
);
284 kernel_may_pull_from_tx(&hdr
->tp_h
);
287 if (it
>= tx_ring
.layout
.tp_frame_nr
)
290 if (unlikely(sigint
== 1))
293 if (frame_count_max
!= 0) {
294 if (ctx
->tx_packets
>= frame_count_max
) {
304 bug_on(gettimeofday(&end
, NULL
));
305 timersub(&end
, &start
, &diff
);
309 bpf_release(&bpf_ops
);
311 dissector_cleanup_all();
312 destroy_tx_ring(tx_sock
, &tx_ring
);
315 leave_rfmon_mac80211(ctx
->device_trans
, ctx
->device_out
);
317 if (__pcap_io
->prepare_close_pcap
)
318 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_RD
);
320 if (!strncmp("-", ctx
->device_in
, strlen("-")))
321 dup2(fd
, fileno(stdin
));
328 printf("\r%12lu packets outgoing\n", ctx
->tx_packets
);
329 printf("\r%12lu packets truncated in file\n", trunced
);
330 printf("\r%12lu bytes outgoing\n", ctx
->tx_bytes
);
331 printf("\r%12lu sec, %lu usec in total\n", diff
.tv_sec
, diff
.tv_usec
);
334 static void receive_to_xmit(struct ctx
*ctx
)
338 int rx_sock
, ifindex_in
, ifindex_out
;
339 unsigned int size_in
, size_out
, it_in
= 0, it_out
= 0;
340 unsigned long frame_count
= 0;
341 struct frame_map
*hdr_in
, *hdr_out
;
342 struct ring tx_ring
, rx_ring
;
343 struct pollfd rx_poll
;
344 struct sock_fprog bpf_ops
;
346 if (!strncmp(ctx
->device_in
, ctx
->device_out
, IFNAMSIZ
))
347 panic("Ingress/egress devices must be different!\n");
348 if (!device_up_and_running(ctx
->device_out
))
349 panic("Egress device not up and running!\n");
351 rx_sock
= pf_socket();
352 tx_sock
= pf_socket();
354 fmemset(&tx_ring
, 0, sizeof(tx_ring
));
355 fmemset(&rx_ring
, 0, sizeof(rx_ring
));
356 fmemset(&rx_poll
, 0, sizeof(rx_poll
));
357 fmemset(&bpf_ops
, 0, sizeof(bpf_ops
));
359 ifindex_in
= device_ifindex(ctx
->device_in
);
360 ifindex_out
= device_ifindex(ctx
->device_out
);
362 size_in
= ring_size(ctx
->device_in
, ctx
->reserve_size
);
363 size_out
= ring_size(ctx
->device_out
, ctx
->reserve_size
);
365 enable_kernel_bpf_jit_compiler();
367 bpf_parse_rules(ctx
->filter
, &bpf_ops
, ctx
->link_type
);
369 bpf_dump_all(&bpf_ops
);
370 bpf_attach_to_sock(rx_sock
, &bpf_ops
);
372 setup_rx_ring_layout(rx_sock
, &rx_ring
, size_in
, ctx
->jumbo
);
373 create_rx_ring(rx_sock
, &rx_ring
, ctx
->verbose
);
374 mmap_rx_ring(rx_sock
, &rx_ring
);
375 alloc_rx_ring_frames(&rx_ring
);
376 bind_rx_ring(rx_sock
, &rx_ring
, ifindex_in
);
377 prepare_polling(rx_sock
, &rx_poll
);
379 set_packet_loss_discard(tx_sock
);
380 setup_tx_ring_layout(tx_sock
, &tx_ring
, size_out
, ctx
->jumbo
);
381 create_tx_ring(tx_sock
, &tx_ring
, ctx
->verbose
);
382 mmap_tx_ring(tx_sock
, &tx_ring
);
383 alloc_tx_ring_frames(&tx_ring
);
384 bind_tx_ring(tx_sock
, &tx_ring
, ifindex_out
);
386 dissector_init_all(ctx
->print_mode
);
388 if (ctx
->promiscuous
)
389 ifflags
= enter_promiscuous_mode(ctx
->device_in
);
392 interval
= ctx
->kpull
;
394 set_itimer_interval_value(&itimer
, 0, interval
);
395 setitimer(ITIMER_REAL
, &itimer
, NULL
);
397 drop_privileges(ctx
->enforce
, ctx
->uid
, ctx
->gid
);
399 printf("Running! Hang up with ^C!\n\n");
402 while (likely(sigint
== 0)) {
403 while (user_may_pull_from_rx(rx_ring
.frames
[it_in
].iov_base
)) {
406 hdr_in
= rx_ring
.frames
[it_in
].iov_base
;
407 in
= ((uint8_t *) hdr_in
) + hdr_in
->tp_h
.tp_mac
;
411 if (ctx
->packet_type
!= -1)
412 if (ctx
->packet_type
!= hdr_in
->s_ll
.sll_pkttype
)
415 hdr_out
= tx_ring
.frames
[it_out
].iov_base
;
416 out
= ((uint8_t *) hdr_out
) + TPACKET2_HDRLEN
- sizeof(struct sockaddr_ll
);
418 for (; !user_may_pull_from_tx(tx_ring
.frames
[it_out
].iov_base
) &&
421 next_rnd_slot(&it_out
, &tx_ring
);
424 if (it_out
>= tx_ring
.layout
.tp_frame_nr
)
428 hdr_out
= tx_ring
.frames
[it_out
].iov_base
;
429 out
= ((uint8_t *) hdr_out
) + TPACKET2_HDRLEN
- sizeof(struct sockaddr_ll
);
432 tpacket_hdr_clone(&hdr_out
->tp_h
, &hdr_in
->tp_h
);
433 fmemcpy(out
, in
, hdr_in
->tp_h
.tp_len
);
435 kernel_may_pull_from_tx(&hdr_out
->tp_h
);
437 next_rnd_slot(&it_out
, &tx_ring
);
440 if (it_out
>= tx_ring
.layout
.tp_frame_nr
)
444 show_frame_hdr(hdr_in
, ctx
->print_mode
);
446 dissector_entry_point(in
, hdr_in
->tp_h
.tp_snaplen
,
447 ctx
->link_type
, ctx
->print_mode
);
449 if (frame_count_max
!= 0) {
450 if (frame_count
>= frame_count_max
) {
458 kernel_may_pull_from_rx(&hdr_in
->tp_h
);
461 if (it_in
>= rx_ring
.layout
.tp_frame_nr
)
464 if (unlikely(sigint
== 1))
468 poll(&rx_poll
, 1, -1);
475 sock_print_net_stats(rx_sock
, 0);
477 bpf_release(&bpf_ops
);
479 dissector_cleanup_all();
481 destroy_tx_ring(tx_sock
, &tx_ring
);
482 destroy_rx_ring(rx_sock
, &rx_ring
);
484 if (ctx
->promiscuous
)
485 leave_promiscuous_mode(ctx
->device_in
, ifflags
);
491 static void translate_pcap_to_txf(int fdo
, uint8_t *out
, size_t len
)
493 size_t bytes_done
= 0;
496 slprintf(bout
, sizeof(bout
), "{\n ");
497 write_or_die(fdo
, bout
, strlen(bout
));
499 while (bytes_done
< len
) {
500 slprintf(bout
, sizeof(bout
), "0x%02x, ", out
[bytes_done
]);
501 write_or_die(fdo
, bout
, strlen(bout
));
505 if (bytes_done
% 10 == 0) {
506 slprintf(bout
, sizeof(bout
), "\n");
507 write_or_die(fdo
, bout
, strlen(bout
));
509 if (bytes_done
< len
) {
510 slprintf(bout
, sizeof(bout
), " ");
511 write_or_die(fdo
, bout
, strlen(bout
));
515 if (bytes_done
% 10 != 0) {
516 slprintf(bout
, sizeof(bout
), "\n");
517 write_or_die(fdo
, bout
, strlen(bout
));
520 slprintf(bout
, sizeof(bout
), "}\n\n");
521 write_or_die(fdo
, bout
, strlen(bout
));
524 static void read_pcap(struct ctx
*ctx
)
528 int ret
, fd
, fdo
= 0;
529 unsigned long trunced
= 0;
532 struct sock_fprog bpf_ops
;
534 struct timeval start
, end
, diff
;
535 struct sockaddr_ll sll
;
539 if (!strncmp("-", ctx
->device_in
, strlen("-"))) {
540 fd
= dup(fileno(stdin
));
541 close(fileno(stdin
));
542 if (ctx
->pcap
== PCAP_OPS_MM
)
543 ctx
->pcap
= PCAP_OPS_SG
;
545 fd
= open_or_die(ctx
->device_in
, O_RDONLY
| O_LARGEFILE
| O_NOATIME
);
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
));
559 fmemset(&bpf_ops
, 0, sizeof(bpf_ops
));
561 bpf_parse_rules(ctx
->filter
, &bpf_ops
, ctx
->link_type
);
563 bpf_dump_all(&bpf_ops
);
565 dissector_init_all(ctx
->print_mode
);
567 out_len
= round_up(1024 * 1024, PAGE_SIZE
);
568 out
= xmalloc_aligned(out_len
, CO_CACHE_LINE_SIZE
);
570 if (ctx
->device_out
) {
571 if (!strncmp("-", ctx
->device_out
, strlen("-"))) {
572 fdo
= dup(fileno(stdout
));
573 close(fileno(stdout
));
575 fdo
= open_or_die_m(ctx
->device_out
, O_RDWR
| O_CREAT
|
576 O_TRUNC
| O_LARGEFILE
, DEFFILEMODE
);
580 drop_privileges(ctx
->enforce
, ctx
->uid
, ctx
->gid
);
582 printf("Running! Hang up with ^C!\n\n");
585 bug_on(gettimeofday(&start
, NULL
));
587 while (likely(sigint
== 0)) {
589 ret
= __pcap_io
->read_pcap(fd
, &phdr
, ctx
->magic
,
591 if (unlikely(ret
< 0))
594 if (unlikely(pcap_get_length(&phdr
, ctx
->magic
) == 0)) {
599 if (unlikely(pcap_get_length(&phdr
, ctx
->magic
) > out_len
)) {
600 pcap_set_length(&phdr
, ctx
->magic
, out_len
);
603 } while (ctx
->filter
&&
604 !bpf_run_filter(&bpf_ops
, out
,
605 pcap_get_length(&phdr
, ctx
->magic
)));
607 pcap_pkthdr_to_tpacket_hdr(&phdr
, ctx
->magic
, &fm
.tp_h
, &sll
);
609 ctx
->tx_bytes
+= fm
.tp_h
.tp_len
;
612 show_frame_hdr(&fm
, ctx
->print_mode
);
614 dissector_entry_point(out
, fm
.tp_h
.tp_snaplen
,
615 ctx
->link_type
, ctx
->print_mode
);
618 translate_pcap_to_txf(fdo
, out
, fm
.tp_h
.tp_snaplen
);
620 if (frame_count_max
!= 0) {
621 if (ctx
->tx_packets
>= frame_count_max
) {
630 bug_on(gettimeofday(&end
, NULL
));
631 timersub(&end
, &start
, &diff
);
633 bpf_release(&bpf_ops
);
635 dissector_cleanup_all();
637 if (__pcap_io
->prepare_close_pcap
)
638 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_RD
);
644 printf("\r%12lu packets outgoing\n", ctx
->tx_packets
);
645 printf("\r%12lu packets truncated in file\n", trunced
);
646 printf("\r%12lu bytes outgoing\n", ctx
->tx_bytes
);
647 printf("\r%12lu sec, %lu usec in total\n", diff
.tv_sec
, diff
.tv_usec
);
649 if (!strncmp("-", ctx
->device_in
, strlen("-")))
650 dup2(fd
, fileno(stdin
));
653 if (ctx
->device_out
) {
654 if (!strncmp("-", ctx
->device_out
, strlen("-")))
655 dup2(fdo
, fileno(stdout
));
660 static void finish_multi_pcap_file(struct ctx
*ctx
, int fd
)
662 __pcap_io
->fsync_pcap(fd
);
664 if (__pcap_io
->prepare_close_pcap
)
665 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_WR
);
669 fmemset(&itimer
, 0, sizeof(itimer
));
670 setitimer(ITIMER_REAL
, &itimer
, NULL
);
673 static int next_multi_pcap_file(struct ctx
*ctx
, int fd
)
678 __pcap_io
->fsync_pcap(fd
);
680 if (__pcap_io
->prepare_close_pcap
)
681 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_WR
);
685 slprintf(fname
, sizeof(fname
), "%s/%s%lu.pcap", ctx
->device_out
,
686 ctx
->prefix
? : "dump-", time(0));
688 fd
= open_or_die_m(fname
, O_RDWR
| O_CREAT
| O_TRUNC
|
689 O_LARGEFILE
, DEFFILEMODE
);
691 ret
= __pcap_io
->push_fhdr_pcap(fd
, ctx
->magic
, ctx
->link_type
);
693 panic("Error writing pcap header!\n");
695 if (__pcap_io
->prepare_access_pcap
) {
696 ret
= __pcap_io
->prepare_access_pcap(fd
, PCAP_MODE_WR
, ctx
->jumbo
);
698 panic("Error prepare writing pcap!\n");
704 static int begin_multi_pcap_file(struct ctx
*ctx
)
711 if (ctx
->device_out
[strlen(ctx
->device_out
) - 1] == '/')
712 ctx
->device_out
[strlen(ctx
->device_out
) - 1] = 0;
714 slprintf(fname
, sizeof(fname
), "%s/%s%lu.pcap", ctx
->device_out
,
715 ctx
->prefix
? : "dump-", time(0));
717 fd
= open_or_die_m(fname
, O_RDWR
| O_CREAT
| O_TRUNC
|
718 O_LARGEFILE
, DEFFILEMODE
);
720 ret
= __pcap_io
->push_fhdr_pcap(fd
, ctx
->magic
, ctx
->link_type
);
722 panic("Error writing pcap header!\n");
724 if (__pcap_io
->prepare_access_pcap
) {
725 ret
= __pcap_io
->prepare_access_pcap(fd
, PCAP_MODE_WR
, ctx
->jumbo
);
727 panic("Error prepare writing pcap!\n");
730 if (ctx
->dump_mode
== DUMP_INTERVAL_TIME
) {
731 interval
= ctx
->dump_interval
;
733 set_itimer_interval_value(&itimer
, interval
, 0);
734 setitimer(ITIMER_REAL
, &itimer
, NULL
);
742 static void finish_single_pcap_file(struct ctx
*ctx
, int fd
)
744 __pcap_io
->fsync_pcap(fd
);
746 if (__pcap_io
->prepare_close_pcap
)
747 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_WR
);
749 if (strncmp("-", ctx
->device_out
, strlen("-")))
752 dup2(fd
, fileno(stdout
));
755 static int begin_single_pcap_file(struct ctx
*ctx
)
761 if (!strncmp("-", ctx
->device_out
, strlen("-"))) {
762 fd
= dup(fileno(stdout
));
763 close(fileno(stdout
));
764 if (ctx
->pcap
== PCAP_OPS_MM
)
765 ctx
->pcap
= PCAP_OPS_SG
;
767 fd
= open_or_die_m(ctx
->device_out
,
768 O_RDWR
| O_CREAT
| O_TRUNC
|
769 O_LARGEFILE
, DEFFILEMODE
);
772 ret
= __pcap_io
->push_fhdr_pcap(fd
, ctx
->magic
, ctx
->link_type
);
774 panic("Error writing pcap header!\n");
776 if (__pcap_io
->prepare_access_pcap
) {
777 ret
= __pcap_io
->prepare_access_pcap(fd
, PCAP_MODE_WR
, ctx
->jumbo
);
779 panic("Error prepare writing pcap!\n");
785 static void print_pcap_file_stats(int sock
, struct ctx
*ctx
, unsigned long skipped
)
788 unsigned long good
, bad
;
789 struct tpacket_stats kstats
;
790 socklen_t slen
= sizeof(kstats
);
792 fmemset(&kstats
, 0, sizeof(kstats
));
794 ret
= getsockopt(sock
, SOL_PACKET
, PACKET_STATISTICS
, &kstats
, &slen
);
796 panic("Cannot get packet statistics!\n");
798 if (ctx
->print_mode
== PRINT_NONE
) {
799 good
= kstats
.tp_packets
- kstats
.tp_drops
- skipped
;
800 bad
= kstats
.tp_drops
+ skipped
;
802 printf(".(+%lu/-%lu)", good
, bad
);
807 static void recv_only_or_dump(struct ctx
*ctx
)
811 int sock
, irq
, ifindex
, fd
= 0, ret
;
812 unsigned int size
, it
= 0;
813 unsigned long frame_count
= 0, skipped
= 0;
815 struct pollfd rx_poll
;
816 struct frame_map
*hdr
;
817 struct sock_fprog bpf_ops
;
818 struct timeval start
, end
, diff
;
824 ctx
->device_trans
= xstrdup(ctx
->device_in
);
825 xfree(ctx
->device_in
);
827 enter_rfmon_mac80211(ctx
->device_trans
, &ctx
->device_in
);
828 ctx
->link_type
= LINKTYPE_IEEE802_11
;
831 fmemset(&rx_ring
, 0, sizeof(rx_ring
));
832 fmemset(&rx_poll
, 0, sizeof(rx_poll
));
833 fmemset(&bpf_ops
, 0, sizeof(bpf_ops
));
835 ifindex
= device_ifindex(ctx
->device_in
);
837 size
= ring_size(ctx
->device_in
, ctx
->reserve_size
);
839 enable_kernel_bpf_jit_compiler();
841 bpf_parse_rules(ctx
->filter
, &bpf_ops
, ctx
->link_type
);
843 bpf_dump_all(&bpf_ops
);
844 bpf_attach_to_sock(sock
, &bpf_ops
);
846 set_sockopt_hwtimestamp(sock
, ctx
->device_in
);
848 setup_rx_ring_layout(sock
, &rx_ring
, size
, ctx
->jumbo
);
849 create_rx_ring(sock
, &rx_ring
, ctx
->verbose
);
850 mmap_rx_ring(sock
, &rx_ring
);
851 alloc_rx_ring_frames(&rx_ring
);
852 bind_rx_ring(sock
, &rx_ring
, ifindex
);
854 prepare_polling(sock
, &rx_poll
);
855 dissector_init_all(ctx
->print_mode
);
857 if (ctx
->cpu
>= 0 && ifindex
> 0) {
858 irq
= device_irq_number(ctx
->device_in
);
859 device_bind_irq_to_cpu(irq
, ctx
->cpu
);
862 printf("IRQ: %s:%d > CPU%d\n",
863 ctx
->device_in
, irq
, ctx
->cpu
);
866 if (ctx
->promiscuous
)
867 ifflags
= enter_promiscuous_mode(ctx
->device_in
);
869 drop_privileges(ctx
->enforce
, ctx
->uid
, ctx
->gid
);
871 if (dump_to_pcap(ctx
)) {
875 fmemset(&stats
, 0, sizeof(stats
));
876 ret
= stat(ctx
->device_out
, &stats
);
882 ctx
->dump_dir
= S_ISDIR(stats
.st_mode
);
884 fd
= begin_multi_pcap_file(ctx
);
887 fd
= begin_single_pcap_file(ctx
);
891 printf("Running! Hang up with ^C!\n\n");
894 bug_on(gettimeofday(&start
, NULL
));
896 while (likely(sigint
== 0)) {
897 while (user_may_pull_from_rx(rx_ring
.frames
[it
].iov_base
)) {
900 hdr
= rx_ring
.frames
[it
].iov_base
;
901 packet
= ((uint8_t *) hdr
) + hdr
->tp_h
.tp_mac
;
904 if (ctx
->packet_type
!= -1)
905 if (ctx
->packet_type
!= hdr
->s_ll
.sll_pkttype
)
908 if (unlikely(ring_frame_size(&rx_ring
) < hdr
->tp_h
.tp_snaplen
)) {
913 if (dump_to_pcap(ctx
)) {
914 tpacket_hdr_to_pcap_pkthdr(&hdr
->tp_h
, &hdr
->s_ll
, &phdr
, ctx
->magic
);
916 ret
= __pcap_io
->write_pcap(fd
, &phdr
, ctx
->magic
, packet
,
917 pcap_get_length(&phdr
, ctx
->magic
));
918 if (unlikely(ret
!= pcap_get_total_length(&phdr
, ctx
->magic
)))
919 panic("Write error to pcap!\n");
922 show_frame_hdr(hdr
, ctx
->print_mode
);
924 dissector_entry_point(packet
, hdr
->tp_h
.tp_snaplen
,
925 ctx
->link_type
, ctx
->print_mode
);
927 if (frame_count_max
!= 0) {
928 if (frame_count
>= frame_count_max
) {
936 kernel_may_pull_from_rx(&hdr
->tp_h
);
939 if (it
>= rx_ring
.layout
.tp_frame_nr
)
942 if (unlikely(sigint
== 1))
945 if (dump_to_pcap(ctx
)) {
946 if (ctx
->dump_mode
== DUMP_INTERVAL_SIZE
) {
947 interval
+= hdr
->tp_h
.tp_snaplen
;
949 if (interval
> ctx
->dump_interval
) {
956 fd
= next_multi_pcap_file(ctx
, fd
);
960 print_pcap_file_stats(sock
, ctx
, skipped
);
965 poll(&rx_poll
, 1, -1);
968 bug_on(gettimeofday(&end
, NULL
));
969 timersub(&end
, &start
, &diff
);
971 if (!(ctx
->dump_dir
&& ctx
->print_mode
== PRINT_NONE
)) {
972 sock_print_net_stats(sock
, skipped
);
974 printf("\r%12lu sec, %lu usec in total\n",
975 diff
.tv_sec
, diff
.tv_usec
);
981 bpf_release(&bpf_ops
);
982 dissector_cleanup_all();
983 destroy_rx_ring(sock
, &rx_ring
);
985 if (ctx
->promiscuous
)
986 leave_promiscuous_mode(ctx
->device_in
, ifflags
);
989 leave_rfmon_mac80211(ctx
->device_trans
, ctx
->device_in
);
991 if (dump_to_pcap(ctx
)) {
993 finish_multi_pcap_file(ctx
, fd
);
995 finish_single_pcap_file(ctx
, fd
);
1001 static void help(void)
1003 printf("\nnetsniff-ng %s, the packet sniffing beast\n", VERSION_STRING
);
1004 puts("http://www.netsniff-ng.org\n\n"
1005 "Usage: netsniff-ng [options] [filter-expression]\n"
1007 " -i|-d|--dev|--in <dev|pcap|-> Input source as netdev, pcap or pcap stdin\n"
1008 " -o|--out <dev|pcap|dir|cfg|-> Output sink as netdev, pcap, directory, trafgen, or stdout\n"
1009 " -f|--filter <bpf-file|expr> Use BPF filter file from bpfc or tcpdump-like expression\n"
1010 " -t|--type <type> Filter for: host|broadcast|multicast|others|outgoing\n"
1011 " -F|--interval <size|time> Dump interval if -o is a dir: <num>KiB/MiB/GiB/s/sec/min/hrs\n"
1012 " -J|--jumbo-support Support for 64KB Super Jumbo Frames (def: 2048B)\n"
1013 " -R|--rfraw Capture or inject raw 802.11 frames\n"
1014 " -n|--num <0|uint> Number of packets until exit (def: 0)\n"
1015 " -P|--prefix <name> Prefix for pcaps stored in directory\n"
1016 " -T|--magic <pcap-magic> Pcap magic number/pcap format to store, see -D\n"
1017 " -D|--dump-pcap-types Dump pcap types and magic numbers and quit\n"
1018 " -B|--dump-bpf Dump generated BPF assembly\n"
1019 " -r|--rand Randomize packet forwarding order (dev->dev)\n"
1020 " -M|--no-promisc No promiscuous mode for netdev\n"
1021 " -A|--no-sock-mem Don't tune core socket memory\n"
1022 " -m|--mmap Mmap(2) pcap file i.e., for replaying pcaps\n"
1023 " -G|--sg Scatter/gather pcap file I/O\n"
1024 " -c|--clrw Use slower read(2)/write(2) I/O\n"
1025 " -S|--ring-size <size> Specify ring size to: <num>KiB/MiB/GiB\n"
1026 " -k|--kernel-pull <uint> Kernel pull from user interval in us (def: 10us)\n"
1027 " -b|--bind-cpu <cpu> Bind to specific CPU\n"
1028 " -u|--user <userid> Drop privileges and change to userid\n"
1029 " -g|--group <groupid> Drop privileges and change to groupid\n"
1030 " -H|--prio-high Make this high priority process\n"
1031 " -Q|--notouch-irq Do not touch IRQ CPU affinity of NIC\n"
1032 " -s|--silent Do not print captured packets\n"
1033 " -q|--less Print less-verbose packet information\n"
1034 " -X|--hex Print packet data in hex format\n"
1035 " -l|--ascii Print human-readable packet data\n"
1036 " -U|--update Update GeoIP databases\n"
1037 " -V|--verbose Be more verbose\n"
1038 " -v|--version Show version\n"
1039 " -h|--help Guess what?!\n\n"
1041 " netsniff-ng --in eth0 --out dump.pcap -s -T 0xa1b2c3d4 --b 0 tcp or udp\n"
1042 " netsniff-ng --in wlan0 --rfraw --out dump.pcap --silent --bind-cpu 0\n"
1043 " netsniff-ng --in dump.pcap --mmap --out eth0 -k1000 --silent --bind-cpu 0\n"
1044 " netsniff-ng --in dump.pcap --out dump.cfg --silent --bind-cpu 0\n"
1045 " netsniff-ng --in eth0 --out eth1 --silent --bind-cpu 0 --type host\n"
1046 " netsniff-ng --in eth1 --out /opt/probe/ -s -m -J --interval 100MiB -b 0\n"
1047 " netsniff-ng --in vlan0 --out dump.pcap -c -u `id -u bob` -g `id -g bob`\n"
1048 " netsniff-ng --in any --filter http.bpf --jumbo-support --ascii -V\n\n"
1050 " For introducing bit errors, delays with random variation and more\n"
1051 " while replaying pcaps, make use of tc(8) with its disciplines (e.g. netem).\n\n"
1052 "Please report bugs to <bugs@netsniff-ng.org>\n"
1053 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
1054 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel.roullit@gmail.com>\n"
1055 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1056 "Swiss federal institute of technology (ETH Zurich)\n"
1057 "License: GNU GPL version 2.0\n"
1058 "This is free software: you are free to change and redistribute it.\n"
1059 "There is NO WARRANTY, to the extent permitted by law.\n");
1063 static void version(void)
1065 printf("\nnetsniff-ng %s, the packet sniffing beast\n", VERSION_STRING
);
1066 puts("http://www.netsniff-ng.org\n\n"
1067 "Please report bugs to <bugs@netsniff-ng.org>\n"
1068 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
1069 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel.roullit@gmail.com>\n"
1070 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1071 "Swiss federal institute of technology (ETH Zurich)\n"
1072 "License: GNU GPL version 2.0\n"
1073 "This is free software: you are free to change and redistribute it.\n"
1074 "There is NO WARRANTY, to the extent permitted by law.\n");
1078 int main(int argc
, char **argv
)
1081 int c
, i
, j
, cpu_tmp
, opt_index
, ops_touched
= 0, vals
[4] = {0};
1082 bool prio_high
= false, setsockmem
= true;
1083 void (*main_loop
)(struct ctx
*ctx
) = NULL
;
1085 .link_type
= LINKTYPE_EN10MB
,
1086 .print_mode
= PRINT_NORM
,
1089 .promiscuous
= true,
1091 .pcap
= PCAP_OPS_SG
,
1092 .dump_interval
= 60,
1093 .dump_mode
= DUMP_INTERVAL_TIME
,
1096 .magic
= ORIGINAL_TCPDUMP_MAGIC
,
1101 while ((c
= getopt_long(argc
, argv
, short_options
, long_options
,
1102 &opt_index
)) != EOF
) {
1106 ctx
.device_in
= xstrdup(optarg
);
1109 ctx
.device_out
= xstrdup(optarg
);
1112 ctx
.prefix
= xstrdup(optarg
);
1115 ctx
.link_type
= LINKTYPE_IEEE802_11
;
1119 ctx
.randomize
= true;
1125 ctx
.magic
= (uint32_t) strtoul(optarg
, NULL
, 0);
1126 pcap_check_magic(ctx
.magic
);
1129 ctx
.filter
= xstrdup(optarg
);
1132 ctx
.promiscuous
= false;
1138 ctx
.uid
= strtoul(optarg
, NULL
, 0);
1142 ctx
.gid
= strtoul(optarg
, NULL
, 0);
1146 if (!strncmp(optarg
, "host", strlen("host")))
1147 ctx
.packet_type
= PACKET_HOST
;
1148 else if (!strncmp(optarg
, "broadcast", strlen("broadcast")))
1149 ctx
.packet_type
= PACKET_BROADCAST
;
1150 else if (!strncmp(optarg
, "multicast", strlen("multicast")))
1151 ctx
.packet_type
= PACKET_MULTICAST
;
1152 else if (!strncmp(optarg
, "others", strlen("others")))
1153 ctx
.packet_type
= PACKET_OTHERHOST
;
1154 else if (!strncmp(optarg
, "outgoing", strlen("outgoing")))
1155 ctx
.packet_type
= PACKET_OUTGOING
;
1157 ctx
.packet_type
= -1;
1161 ctx
.reserve_size
= 0;
1163 for (j
= i
= strlen(optarg
); i
> 0; --i
) {
1164 if (!isdigit(optarg
[j
- i
]))
1169 if (!strncmp(ptr
, "KiB", strlen("KiB")))
1170 ctx
.reserve_size
= 1 << 10;
1171 else if (!strncmp(ptr
, "MiB", strlen("MiB")))
1172 ctx
.reserve_size
= 1 << 20;
1173 else if (!strncmp(ptr
, "GiB", strlen("GiB")))
1174 ctx
.reserve_size
= 1 << 30;
1176 panic("Syntax error in ring size param!\n");
1179 ctx
.reserve_size
*= strtol(optarg
, NULL
, 0);
1182 cpu_tmp
= strtol(optarg
, NULL
, 0);
1184 cpu_affinity(cpu_tmp
);
1192 ctx
.pcap
= PCAP_OPS_RW
;
1196 ctx
.pcap
= PCAP_OPS_MM
;
1200 ctx
.pcap
= PCAP_OPS_SG
;
1207 ctx
.print_mode
= PRINT_NONE
;
1210 ctx
.print_mode
= PRINT_LESS
;
1214 (ctx
.print_mode
== PRINT_ASCII
) ?
1215 PRINT_HEX_ASCII
: PRINT_HEX
;
1219 (ctx
.print_mode
== PRINT_HEX
) ?
1220 PRINT_HEX_ASCII
: PRINT_ASCII
;
1223 ctx
.kpull
= strtol(optarg
, NULL
, 0);
1226 frame_count_max
= strtol(optarg
, NULL
, 0);
1230 ctx
.dump_interval
= 0;
1232 for (j
= i
= strlen(optarg
); i
> 0; --i
) {
1233 if (!isdigit(optarg
[j
- i
]))
1238 if (!strncmp(ptr
, "KiB", strlen("KiB"))) {
1239 ctx
.dump_interval
= 1 << 10;
1240 ctx
.dump_mode
= DUMP_INTERVAL_SIZE
;
1241 } else if (!strncmp(ptr
, "MiB", strlen("MiB"))) {
1242 ctx
.dump_interval
= 1 << 20;
1243 ctx
.dump_mode
= DUMP_INTERVAL_SIZE
;
1244 } else if (!strncmp(ptr
, "GiB", strlen("GiB"))) {
1245 ctx
.dump_interval
= 1 << 30;
1246 ctx
.dump_mode
= DUMP_INTERVAL_SIZE
;
1247 } else if (!strncmp(ptr
, "sec", strlen("sec"))) {
1248 ctx
.dump_interval
= 1;
1249 ctx
.dump_mode
= DUMP_INTERVAL_TIME
;
1250 } else if (!strncmp(ptr
, "min", strlen("min"))) {
1251 ctx
.dump_interval
= 60;
1252 ctx
.dump_mode
= DUMP_INTERVAL_TIME
;
1253 } else if (!strncmp(ptr
, "hrs", strlen("hrs"))) {
1254 ctx
.dump_interval
= 60 * 60;
1255 ctx
.dump_mode
= DUMP_INTERVAL_TIME
;
1256 } else if (!strncmp(ptr
, "s", strlen("s"))) {
1257 ctx
.dump_interval
= 1;
1258 ctx
.dump_mode
= DUMP_INTERVAL_TIME
;
1260 panic("Syntax error in time/size param!\n");
1264 ctx
.dump_interval
*= strtol(optarg
, NULL
, 0);
1270 ctx
.dump_bpf
= true;
1273 pcap_dump_type_features();
1303 panic("Option -%c requires an argument!\n",
1306 if (isprint(optopt
))
1307 printf("Unknown option character `0x%X\'!\n", optopt
);
1315 if (!ctx
.filter
&& optind
!= argc
) {
1319 for (i
= optind
; i
< argc
; ++i
) {
1320 size_t alen
= strlen(argv
[i
]) + 2;
1321 size_t flen
= ctx
.filter
? strlen(ctx
.filter
) : 0;
1323 ctx
.filter
= xrealloc(ctx
.filter
, 1, flen
+ alen
);
1324 ret
= slprintf(ctx
.filter
+ offset
, strlen(argv
[i
]) + 2, "%s ", argv
[i
]);
1326 panic("Cannot concatenate filter string!\n");
1333 ctx
.device_in
= xstrdup("any");
1335 register_signal(SIGINT
, signal_handler
);
1336 register_signal(SIGHUP
, signal_handler
);
1341 set_proc_prio(get_default_proc_prio());
1342 set_sched_status(get_default_sched_policy(), get_default_sched_prio());
1345 if (ctx
.device_in
&& (device_mtu(ctx
.device_in
) ||
1346 !strncmp("any", ctx
.device_in
, strlen(ctx
.device_in
)))) {
1347 if (!ctx
.device_out
) {
1349 main_loop
= recv_only_or_dump
;
1350 } else if (device_mtu(ctx
.device_out
)) {
1351 register_signal_f(SIGALRM
, timer_elapsed
, SA_SIGINFO
);
1352 main_loop
= receive_to_xmit
;
1355 register_signal_f(SIGALRM
, timer_next_dump
, SA_SIGINFO
);
1356 main_loop
= recv_only_or_dump
;
1358 ctx
.pcap
= PCAP_OPS_SG
;
1361 if (ctx
.device_out
&& device_mtu(ctx
.device_out
)) {
1362 register_signal_f(SIGALRM
, timer_elapsed
, SA_SIGINFO
);
1363 main_loop
= pcap_to_xmit
;
1365 ctx
.pcap
= PCAP_OPS_MM
;
1367 main_loop
= read_pcap
;
1369 ctx
.pcap
= PCAP_OPS_SG
;
1377 set_system_socket_memory(vals
, array_size(vals
));
1384 reset_system_socket_memory(vals
, array_size(vals
));
1389 free(ctx
.device_in
);
1390 free(ctx
.device_out
);
1391 free(ctx
.device_trans
);