2 * netsniff-ng - the packet sniffing beast
3 * By Daniel Borkmann <daniel@netsniff-ng.org>
4 * Copyright 2009-2011 Daniel Borkmann.
5 * Copyright 2010 Emmanuel Roullit.
6 * Subject to the GPL, version 2.
8 * The first sniffer that invoked both, the zero-copy RX_RING as well as
9 * the zero-copy TX_RING for high-performance network I/O and scatter/gather
12 * "I knew that danger lay ahead, of course; but I did not expect to
13 * meet it in our own Shire. Can't a hobbit walk from the Water to the
14 * River in peace?" "But it is not your own Shire," said Gildor. "Others
15 * dwelt here before hobbits were; and others will dwell here again when
16 * hobbits are no more. The wide world is all about you: you can fence
17 * yourselves in, but you cannot for ever fence it out."
19 * -- The Lord of the Rings, Gildor to Frodo,
20 * Chapter 'Three is Company'.
31 #include <sys/socket.h>
32 #include <sys/types.h>
50 #include "dissector.h"
54 #define DUMP_INTERVAL_DEF 60
61 char *device_in
, *device_out
, *device_trans
, *filter
, *prefix
;
62 #define CPU_UNKNOWN -1
63 #define CPU_NOTOUCH -2
64 int cpu
, rfraw
, dump
, print_mode
, dump_dir
, jumbo_support
;
68 bool randomize
, promiscuous
;
69 enum pcap_ops_groups pcap
;
70 unsigned long kpull
, dump_interval
, reserve_size
;
71 enum dump_mode dump_mode
;
75 unsigned long tx_bytes
;
76 unsigned long tx_packets
;
79 volatile sig_atomic_t sigint
= 0;
81 static unsigned long frame_cnt_max
= 0, interval
= TX_KERNEL_PULL_INT
;
82 static struct itimerval itimer
;
83 static volatile bool next_dump
= false;
85 static const char *short_options
= "d:i:o:rf:MJt:S:k:n:b:B:HQmcsqXlvhF:RgAP:";
86 static const struct option long_options
[] = {
87 {"dev", required_argument
, NULL
, 'd'},
88 {"in", required_argument
, NULL
, 'i'},
89 {"out", required_argument
, NULL
, 'o'},
90 {"filter", required_argument
, NULL
, 'f'},
91 {"num", required_argument
, NULL
, 'n'},
92 {"type", required_argument
, NULL
, 't'},
93 {"interval", required_argument
, NULL
, 'F'},
94 {"ring-size", required_argument
, NULL
, 'S'},
95 {"kernel-pull", required_argument
, NULL
, 'k'},
96 {"bind-cpu", required_argument
, NULL
, 'b'},
97 {"unbind-cpu", required_argument
, NULL
, 'B'},
98 {"prefix", required_argument
, NULL
, 'P'},
99 {"rand", no_argument
, NULL
, 'r'},
100 {"rfraw", no_argument
, NULL
, 'R'},
101 {"mmap", no_argument
, NULL
, 'm'},
102 {"sg", no_argument
, NULL
, 'g'},
103 {"clrw", no_argument
, NULL
, 'c'},
104 {"jumbo-support", no_argument
, NULL
, 'J'},
105 {"no-promisc", no_argument
, NULL
, 'M'},
106 {"prio-high", no_argument
, NULL
, 'H'},
107 {"notouch-irq", no_argument
, NULL
, 'Q'},
108 {"silent", no_argument
, NULL
, 's'},
109 {"less", no_argument
, NULL
, 'q'},
110 {"hex", no_argument
, NULL
, 'X'},
111 {"ascii", no_argument
, NULL
, 'l'},
112 {"no-sock-mem", no_argument
, NULL
, 'A'},
113 {"version", no_argument
, NULL
, 'v'},
114 {"help", no_argument
, NULL
, 'h'},
118 static void signal_handler(int number
)
131 static void timer_elapsed(int number
)
133 itimer
.it_interval
.tv_sec
= 0;
134 itimer
.it_interval
.tv_usec
= interval
;
135 itimer
.it_value
.tv_sec
= 0;
136 itimer
.it_value
.tv_usec
= interval
;
138 pull_and_flush_tx_ring(tx_sock
);
139 setitimer(ITIMER_REAL
, &itimer
, NULL
);
142 static void timer_next_dump(int number
)
144 itimer
.it_interval
.tv_sec
= interval
;
145 itimer
.it_interval
.tv_usec
= 0;
146 itimer
.it_value
.tv_sec
= interval
;
147 itimer
.it_value
.tv_usec
= 0;
150 setitimer(ITIMER_REAL
, &itimer
, NULL
);
153 static void enter_mode_pcap_to_tx(struct mode
*mode
)
156 int irq
, ifindex
, fd
= 0, ret
;
157 unsigned int size
, it
= 0;
159 struct frame_map
*hdr
;
160 struct sock_fprog bpf_ops
;
161 struct tx_stats stats
;
163 unsigned long trunced
= 0;
164 struct timeval start
, end
, diff
;
166 if (!device_up_and_running(mode
->device_out
) &&
168 panic("Device not up and running!\n");
170 tx_sock
= pf_socket();
172 if (!pcap_ops
[mode
->pcap
])
173 panic("pcap group not supported!\n");
174 fd
= open_or_die(mode
->device_in
, O_RDONLY
| O_LARGEFILE
| O_NOATIME
);
175 ret
= pcap_ops
[mode
->pcap
]->pull_file_header(fd
, &mode
->link_type
);
177 panic("error reading pcap header!\n");
178 if (pcap_ops
[mode
->pcap
]->prepare_reading_pcap
) {
179 ret
= pcap_ops
[mode
->pcap
]->prepare_reading_pcap(fd
);
181 panic("error prepare reading pcap!\n");
184 fmemset(&tx_ring
, 0, sizeof(tx_ring
));
185 fmemset(&bpf_ops
, 0, sizeof(bpf_ops
));
186 fmemset(&stats
, 0, sizeof(stats
));
189 mode
->device_trans
= xstrdup(mode
->device_out
);
190 xfree(mode
->device_out
);
192 enter_rfmon_mac80211(mode
->device_trans
, &mode
->device_out
);
193 if (mode
->link_type
!= LINKTYPE_IEEE802_11
)
194 panic("Wrong linktype of pcap!\n");
197 ifindex
= device_ifindex(mode
->device_out
);
198 size
= ring_size(mode
->device_out
, mode
->reserve_size
);
200 bpf_parse_rules(mode
->filter
, &bpf_ops
);
202 set_packet_loss_discard(tx_sock
);
203 set_sockopt_hwtimestamp(tx_sock
, mode
->device_out
);
204 setup_tx_ring_layout(tx_sock
, &tx_ring
, size
, mode
->jumbo_support
);
205 create_tx_ring(tx_sock
, &tx_ring
);
206 mmap_tx_ring(tx_sock
, &tx_ring
);
207 alloc_tx_ring_frames(&tx_ring
);
208 bind_tx_ring(tx_sock
, &tx_ring
, ifindex
);
210 dissector_init_all(mode
->print_mode
);
212 if (mode
->cpu
>= 0 && ifindex
> 0) {
213 irq
= device_irq_number(mode
->device_out
);
214 device_bind_irq_to_cpu(mode
->cpu
, irq
);
215 printf("IRQ: %s:%d > CPU%d\n", mode
->device_out
, irq
,
220 interval
= mode
->kpull
;
222 itimer
.it_interval
.tv_sec
= 0;
223 itimer
.it_interval
.tv_usec
= interval
;
224 itimer
.it_value
.tv_sec
= 0;
225 itimer
.it_value
.tv_usec
= interval
;
226 setitimer(ITIMER_REAL
, &itimer
, NULL
);
229 bpf_dump_all(&bpf_ops
);
230 printf("MD: TX %luus %s ", interval
, pcap_ops
[mode
->pcap
]->name
);
232 printf("802.11 raw via %s ", mode
->device_out
);
233 #ifdef _LARGEFILE64_SOURCE
239 bug_on(gettimeofday(&start
, NULL
));
241 while (likely(sigint
== 0)) {
242 while (user_may_pull_from_tx(tx_ring
.frames
[it
].iov_base
)) {
243 struct pcap_pkthdr phdr
;
244 hdr
= tx_ring
.frames
[it
].iov_base
;
245 /* Kernel assumes: data = ph.raw + po->tp_hdrlen -
246 * sizeof(struct sockaddr_ll); */
247 out
= ((uint8_t *) hdr
) + TPACKET2_HDRLEN
-
248 sizeof(struct sockaddr_ll
);
251 memset(&phdr
, 0, sizeof(phdr
));
252 ret
= pcap_ops
[mode
->pcap
]->read_pcap_pkt(fd
, &phdr
,
253 out
, ring_frame_size(&tx_ring
));
254 if (unlikely(ret
<= 0))
256 if (ring_frame_size(&tx_ring
) < phdr
.len
) {
257 phdr
.len
= ring_frame_size(&tx_ring
);
260 } while (mode
->filter
&& !bpf_run_filter(&bpf_ops
, out
, phdr
.len
));
261 pcap_pkthdr_to_tpacket_hdr(&phdr
, &hdr
->tp_h
);
263 stats
.tx_bytes
+= hdr
->tp_h
.tp_len
;;
266 show_frame_hdr(hdr
, mode
->print_mode
, RING_MODE_EGRESS
);
267 dissector_entry_point(out
, hdr
->tp_h
.tp_snaplen
,
268 mode
->link_type
, mode
->print_mode
);
270 kernel_may_pull_from_tx(&hdr
->tp_h
);
271 next_slot(&it
, &tx_ring
);
273 if (unlikely(sigint
== 1))
275 if (frame_cnt_max
!= 0 &&
276 stats
.tx_packets
>= frame_cnt_max
) {
285 bug_on(gettimeofday(&end
, NULL
));
286 diff
= tv_subtract(end
, start
);
290 printf("\r%12lu frames outgoing\n", stats
.tx_packets
);
291 printf("\r%12lu frames truncated (larger than frame)\n", trunced
);
292 printf("\r%12lu bytes outgoing\n", stats
.tx_bytes
);
293 printf("\r%12lu sec, %lu usec in total\n", diff
.tv_sec
, diff
.tv_usec
);
295 bpf_release(&bpf_ops
);
296 dissector_cleanup_all();
297 destroy_tx_ring(tx_sock
, &tx_ring
);
300 leave_rfmon_mac80211(mode
->device_trans
, mode
->device_out
);
303 if (pcap_ops
[mode
->pcap
]->prepare_close_pcap
)
304 pcap_ops
[mode
->pcap
]->prepare_close_pcap(fd
, PCAP_MODE_READ
);
308 static void enter_mode_rx_to_tx(struct mode
*mode
)
310 int rx_sock
, ifindex_in
, ifindex_out
;
311 unsigned int size_in
, size_out
, it_in
= 0, it_out
= 0;
312 unsigned long fcnt
= 0;
315 struct frame_map
*hdr_in
, *hdr_out
;
318 struct pollfd rx_poll
;
319 struct sock_fprog bpf_ops
;
321 if (!strncmp(mode
->device_in
, mode
->device_out
,
322 strlen(mode
->device_in
)))
323 panic("Ingress/egress devices must be different!\n");
324 if (!device_up_and_running(mode
->device_out
))
325 panic("Egress device not up and running!\n");
326 if (!device_up_and_running(mode
->device_in
))
327 panic("Ingress device not up and running!\n");
329 rx_sock
= pf_socket();
330 tx_sock
= pf_socket();
332 fmemset(&tx_ring
, 0, sizeof(tx_ring
));
333 fmemset(&rx_ring
, 0, sizeof(rx_ring
));
334 fmemset(&rx_poll
, 0, sizeof(rx_poll
));
335 fmemset(&bpf_ops
, 0, sizeof(bpf_ops
));
337 ifindex_in
= device_ifindex(mode
->device_in
);
338 size_in
= ring_size(mode
->device_in
, mode
->reserve_size
);
340 ifindex_out
= device_ifindex(mode
->device_out
);
341 size_out
= ring_size(mode
->device_out
, mode
->reserve_size
);
343 enable_kernel_bpf_jit_compiler();
344 bpf_parse_rules(mode
->filter
, &bpf_ops
);
345 bpf_attach_to_sock(rx_sock
, &bpf_ops
);
347 setup_rx_ring_layout(rx_sock
, &rx_ring
, size_in
, mode
->jumbo_support
);
348 create_rx_ring(rx_sock
, &rx_ring
);
349 mmap_rx_ring(rx_sock
, &rx_ring
);
350 alloc_rx_ring_frames(&rx_ring
);
351 bind_rx_ring(rx_sock
, &rx_ring
, ifindex_in
);
352 prepare_polling(rx_sock
, &rx_poll
);
354 set_packet_loss_discard(tx_sock
);
355 setup_tx_ring_layout(tx_sock
, &tx_ring
, size_out
, mode
->jumbo_support
);
356 create_tx_ring(tx_sock
, &tx_ring
);
357 mmap_tx_ring(tx_sock
, &tx_ring
);
358 alloc_tx_ring_frames(&tx_ring
);
359 bind_tx_ring(tx_sock
, &tx_ring
, ifindex_out
);
361 mt_init_by_seed_time();
362 dissector_init_all(mode
->print_mode
);
364 if (mode
->promiscuous
== true) {
365 ifflags
= enter_promiscuous_mode(mode
->device_in
);
370 interval
= mode
->kpull
;
372 itimer
.it_interval
.tv_sec
= 0;
373 itimer
.it_interval
.tv_usec
= interval
;
374 itimer
.it_value
.tv_sec
= 0;
375 itimer
.it_value
.tv_usec
= interval
;
376 setitimer(ITIMER_REAL
, &itimer
, NULL
);
379 bpf_dump_all(&bpf_ops
);
380 printf("MD: RXTX %luus\n\n", interval
);
381 printf("Running! Hang up with ^C!\n\n");
383 while (likely(sigint
== 0)) {
384 while (user_may_pull_from_rx(rx_ring
.frames
[it_in
].iov_base
)) {
386 hdr_in
= rx_ring
.frames
[it_in
].iov_base
;
387 in
= ((uint8_t *) hdr_in
) + hdr_in
->tp_h
.tp_mac
;
389 if (mode
->packet_type
!= PACKET_ALL
)
390 if (mode
->packet_type
!= hdr_in
->s_ll
.sll_pkttype
)
393 hdr_out
= tx_ring
.frames
[it_out
].iov_base
;
394 out
= ((uint8_t *) hdr_out
) + TPACKET2_HDRLEN
-
395 sizeof(struct sockaddr_ll
);
397 for (; !user_may_pull_from_tx(tx_ring
.frames
[it_out
].iov_base
) &&
400 next_rnd_slot(&it_out
, &tx_ring
);
402 next_slot(&it_out
, &tx_ring
);
403 hdr_out
= tx_ring
.frames
[it_out
].iov_base
;
404 out
= ((uint8_t *) hdr_out
) + TPACKET2_HDRLEN
-
405 sizeof(struct sockaddr_ll
);
408 tpacket_hdr_clone(&hdr_out
->tp_h
, &hdr_in
->tp_h
);
409 fmemcpy(out
, in
, hdr_in
->tp_h
.tp_len
);
411 kernel_may_pull_from_tx(&hdr_out
->tp_h
);
413 next_rnd_slot(&it_out
, &tx_ring
);
415 next_slot(&it_out
, &tx_ring
);
417 show_frame_hdr(hdr_in
, mode
->print_mode
, RING_MODE_INGRESS
);
418 dissector_entry_point(in
, hdr_in
->tp_h
.tp_snaplen
,
419 mode
->link_type
, mode
->print_mode
);
421 if (frame_cnt_max
!= 0 && fcnt
>= frame_cnt_max
) {
428 kernel_may_pull_from_rx(&hdr_in
->tp_h
);
429 next_slot(&it_in
, &rx_ring
);
431 if (unlikely(sigint
== 1))
435 poll(&rx_poll
, 1, -1);
436 poll_error_maybe_die(rx_sock
, &rx_poll
);
441 sock_print_net_stats(rx_sock
, 0);
443 bpf_release(&bpf_ops
);
444 dissector_cleanup_all();
445 destroy_tx_ring(tx_sock
, &tx_ring
);
446 destroy_rx_ring(rx_sock
, &rx_ring
);
448 if (mode
->promiscuous
== true)
449 leave_promiscuous_mode(mode
->device_in
, ifflags
);
455 static void enter_mode_read_pcap(struct mode
*mode
)
458 int ret
, fd
, fdo
= 0;
459 struct pcap_pkthdr phdr
;
460 struct sock_fprog bpf_ops
;
461 struct tx_stats stats
;
465 unsigned long trunced
= 0;
466 struct timeval start
, end
, diff
;
468 if (!pcap_ops
[mode
->pcap
])
469 panic("pcap group not supported!\n");
470 fd
= open_or_die(mode
->device_in
, O_RDONLY
| O_LARGEFILE
| O_NOATIME
);
471 ret
= pcap_ops
[mode
->pcap
]->pull_file_header(fd
, &mode
->link_type
);
473 panic("error reading pcap header!\n");
474 if (pcap_ops
[mode
->pcap
]->prepare_reading_pcap
) {
475 ret
= pcap_ops
[mode
->pcap
]->prepare_reading_pcap(fd
);
477 panic("error prepare reading pcap!\n");
480 fmemset(&fm
, 0, sizeof(fm
));
481 fmemset(&bpf_ops
, 0, sizeof(bpf_ops
));
482 fmemset(&stats
, 0, sizeof(stats
));
484 bpf_parse_rules(mode
->filter
, &bpf_ops
);
485 dissector_init_all(mode
->print_mode
);
488 out
= xmalloc_aligned(out_len
, CO_CACHE_LINE_SIZE
);
491 bpf_dump_all(&bpf_ops
);
492 printf("MD: RD %s ", pcap_ops
[mode
->pcap
]->name
);
493 #ifdef _LARGEFILE64_SOURCE
499 if (mode
->device_out
) {
500 fdo
= open_or_die_m(mode
->device_out
, O_RDWR
| O_CREAT
|
501 O_TRUNC
| O_LARGEFILE
, DEFFILEMODE
);
504 bug_on(gettimeofday(&start
, NULL
));
506 while (likely(sigint
== 0)) {
508 memset(&phdr
, 0, sizeof(phdr
));
509 ret
= pcap_ops
[mode
->pcap
]->read_pcap_pkt(fd
, &phdr
,
511 if (unlikely(ret
< 0))
513 if (unlikely(phdr
.len
== 0)) {
517 if (unlikely(phdr
.len
> out_len
)) {
521 } while (mode
->filter
&&
522 !bpf_run_filter(&bpf_ops
, out
, phdr
.len
));
524 pcap_pkthdr_to_tpacket_hdr(&phdr
, &fm
.tp_h
);
526 stats
.tx_bytes
+= fm
.tp_h
.tp_len
;
529 show_frame_hdr(&fm
, mode
->print_mode
, RING_MODE_EGRESS
);
530 dissector_entry_point(out
, fm
.tp_h
.tp_snaplen
,
531 mode
->link_type
, mode
->print_mode
);
533 if (mode
->device_out
) {
536 slprintf(bout
, sizeof(bout
), "{\n ");
537 write_or_die(fdo
, bout
, strlen(bout
));
539 while (i
< fm
.tp_h
.tp_snaplen
) {
540 slprintf(bout
, sizeof(bout
), "0x%02x, ", out
[i
]);
541 write_or_die(fdo
, bout
, strlen(bout
));
544 slprintf(bout
, sizeof(bout
), "\n", out
[i
]);
545 write_or_die(fdo
, bout
, strlen(bout
));
546 if (i
< fm
.tp_h
.tp_snaplen
) {
547 slprintf(bout
, sizeof(bout
), " ", out
[i
]);
548 write_or_die(fdo
, bout
, strlen(bout
));
553 slprintf(bout
, sizeof(bout
), "\n");
554 write_or_die(fdo
, bout
, strlen(bout
));
556 slprintf(bout
, sizeof(bout
), "}\n\n");
557 write_or_die(fdo
, bout
, strlen(bout
));
560 if (frame_cnt_max
!= 0 &&
561 stats
.tx_packets
>= frame_cnt_max
) {
569 bug_on(gettimeofday(&end
, NULL
));
570 diff
= tv_subtract(end
, start
);
574 printf("\r%12lu frames outgoing\n", stats
.tx_packets
);
575 printf("\r%12lu frames truncated (larger than mtu)\n", trunced
);
576 printf("\r%12lu bytes outgoing\n", stats
.tx_bytes
);
577 printf("\r%12lu sec, %lu usec in total\n", diff
.tv_sec
, diff
.tv_usec
);
581 bpf_release(&bpf_ops
);
582 dissector_cleanup_all();
583 if (pcap_ops
[mode
->pcap
]->prepare_close_pcap
)
584 pcap_ops
[mode
->pcap
]->prepare_close_pcap(fd
, PCAP_MODE_READ
);
587 if (mode
->device_out
)
591 static void finish_multi_pcap_file(struct mode
*mode
, int fd
)
593 pcap_ops
[mode
->pcap
]->fsync_pcap(fd
);
594 if (pcap_ops
[mode
->pcap
]->prepare_close_pcap
)
595 pcap_ops
[mode
->pcap
]->prepare_close_pcap(fd
, PCAP_MODE_WRITE
);
598 fmemset(&itimer
, 0, sizeof(itimer
));
599 setitimer(ITIMER_REAL
, &itimer
, NULL
);
602 static int next_multi_pcap_file(struct mode
*mode
, int fd
)
607 pcap_ops
[mode
->pcap
]->fsync_pcap(fd
);
608 if (pcap_ops
[mode
->pcap
]->prepare_close_pcap
)
609 pcap_ops
[mode
->pcap
]->prepare_close_pcap(fd
, PCAP_MODE_WRITE
);
612 slprintf(tmp
, sizeof(tmp
), "%s/%s%lu.pcap",
613 mode
->device_out
, mode
->prefix
? : "dump-", time(0));
615 fd
= open_or_die_m(tmp
, O_RDWR
| O_CREAT
| O_TRUNC
| O_LARGEFILE
,
617 ret
= pcap_ops
[mode
->pcap
]->push_file_header(fd
, mode
->link_type
);
619 panic("error writing pcap header!\n");
620 if (pcap_ops
[mode
->pcap
]->prepare_writing_pcap
) {
621 ret
= pcap_ops
[mode
->pcap
]->prepare_writing_pcap(fd
);
623 panic("error prepare writing pcap!\n");
629 static int begin_multi_pcap_file(struct mode
*mode
)
634 if (!pcap_ops
[mode
->pcap
])
635 panic("pcap group not supported!\n");
636 if (mode
->device_out
[strlen(mode
->device_out
) - 1] == '/')
637 mode
->device_out
[strlen(mode
->device_out
) - 1] = 0;
639 slprintf(tmp
, sizeof(tmp
), "%s/%s%lu.pcap",
640 mode
->device_out
, mode
->prefix
? : "dump-", time(0));
642 fd
= open_or_die_m(tmp
, O_RDWR
| O_CREAT
| O_TRUNC
| O_LARGEFILE
,
644 ret
= pcap_ops
[mode
->pcap
]->push_file_header(fd
, mode
->link_type
);
646 panic("error writing pcap header!\n");
647 if (pcap_ops
[mode
->pcap
]->prepare_writing_pcap
) {
648 ret
= pcap_ops
[mode
->pcap
]->prepare_writing_pcap(fd
);
650 panic("error prepare writing pcap!\n");
653 if (mode
->dump_mode
== DUMP_INTERVAL_TIME
) {
654 interval
= mode
->dump_interval
;
656 itimer
.it_interval
.tv_sec
= interval
;
657 itimer
.it_interval
.tv_usec
= 0;
658 itimer
.it_value
.tv_sec
= interval
;
659 itimer
.it_value
.tv_usec
= 0;
661 setitimer(ITIMER_REAL
, &itimer
, NULL
);
669 static void finish_single_pcap_file(struct mode
*mode
, int fd
)
671 pcap_ops
[mode
->pcap
]->fsync_pcap(fd
);
672 if (pcap_ops
[mode
->pcap
]->prepare_close_pcap
)
673 pcap_ops
[mode
->pcap
]->prepare_close_pcap(fd
, PCAP_MODE_WRITE
);
677 static int begin_single_pcap_file(struct mode
*mode
)
681 if (!pcap_ops
[mode
->pcap
])
682 panic("pcap group not supported!\n");
683 fd
= open_or_die_m(mode
->device_out
,
684 O_RDWR
| O_CREAT
| O_TRUNC
| O_LARGEFILE
,
686 ret
= pcap_ops
[mode
->pcap
]->push_file_header(fd
, mode
->link_type
);
688 panic("error writing pcap header!\n");
689 if (pcap_ops
[mode
->pcap
]->prepare_writing_pcap
) {
690 ret
= pcap_ops
[mode
->pcap
]->prepare_writing_pcap(fd
);
692 panic("error prepare writing pcap!\n");
698 static void enter_mode_rx_only_or_dump(struct mode
*mode
)
700 int sock
, irq
, ifindex
, fd
= 0, ret
;
701 unsigned int size
, it
= 0;
702 unsigned long fcnt
= 0, skipped
= 0;
706 struct pollfd rx_poll
;
707 struct frame_map
*hdr
;
708 struct sock_fprog bpf_ops
;
709 struct timeval start
, end
, diff
;
711 if (!device_up_and_running(mode
->device_in
) &&
713 panic("Device not up and running!\n");
718 mode
->device_trans
= xstrdup(mode
->device_in
);
719 xfree(mode
->device_in
);
721 enter_rfmon_mac80211(mode
->device_trans
, &mode
->device_in
);
722 mode
->link_type
= LINKTYPE_IEEE802_11
;
729 fmemset(&tmp
, 0, sizeof(tmp
));
730 ret
= stat(mode
->device_out
, &tmp
);
735 mode
->dump_dir
= !!S_ISDIR(tmp
.st_mode
);
736 if (mode
->dump_dir
) {
737 fd
= begin_multi_pcap_file(mode
);
740 fd
= begin_single_pcap_file(mode
);
744 fmemset(&rx_ring
, 0, sizeof(rx_ring
));
745 fmemset(&rx_poll
, 0, sizeof(rx_poll
));
746 fmemset(&bpf_ops
, 0, sizeof(bpf_ops
));
748 ifindex
= device_ifindex(mode
->device_in
);
749 size
= ring_size(mode
->device_in
, mode
->reserve_size
);
751 enable_kernel_bpf_jit_compiler();
752 bpf_parse_rules(mode
->filter
, &bpf_ops
);
753 bpf_attach_to_sock(sock
, &bpf_ops
);
755 set_sockopt_hwtimestamp(sock
, mode
->device_in
);
756 setup_rx_ring_layout(sock
, &rx_ring
, size
, mode
->jumbo_support
);
757 create_rx_ring(sock
, &rx_ring
);
758 mmap_rx_ring(sock
, &rx_ring
);
759 alloc_rx_ring_frames(&rx_ring
);
760 bind_rx_ring(sock
, &rx_ring
, ifindex
);
762 prepare_polling(sock
, &rx_poll
);
763 dissector_init_all(mode
->print_mode
);
765 if (mode
->cpu
>= 0 && ifindex
> 0) {
766 irq
= device_irq_number(mode
->device_in
);
767 device_bind_irq_to_cpu(mode
->cpu
, irq
);
768 printf("IRQ: %s:%d > CPU%d\n", mode
->device_in
, irq
,
772 if (mode
->promiscuous
== true) {
773 ifflags
= enter_promiscuous_mode(mode
->device_in
);
778 bpf_dump_all(&bpf_ops
);
779 printf("MD: RX %s ", mode
->dump
? pcap_ops
[mode
->pcap
]->name
: "");
781 printf("802.11 raw via %s ", mode
->device_in
);
782 #ifdef _LARGEFILE64_SOURCE
788 bug_on(gettimeofday(&start
, NULL
));
790 while (likely(sigint
== 0)) {
791 while (user_may_pull_from_rx(rx_ring
.frames
[it
].iov_base
)) {
794 hdr
= rx_ring
.frames
[it
].iov_base
;
795 packet
= ((uint8_t *) hdr
) + hdr
->tp_h
.tp_mac
;
798 if (mode
->packet_type
!= PACKET_ALL
)
799 if (mode
->packet_type
!= hdr
->s_ll
.sll_pkttype
)
801 if (unlikely(ring_frame_size(&rx_ring
) <
802 hdr
->tp_h
.tp_snaplen
)) {
807 struct pcap_pkthdr phdr
;
808 tpacket_hdr_to_pcap_pkthdr(&hdr
->tp_h
, &phdr
);
809 ret
= pcap_ops
[mode
->pcap
]->write_pcap_pkt(fd
, &phdr
,
811 if (unlikely(ret
!= sizeof(phdr
) + phdr
.len
))
812 panic("Write error to pcap!\n");
815 show_frame_hdr(hdr
, mode
->print_mode
, RING_MODE_INGRESS
);
816 dissector_entry_point(packet
, hdr
->tp_h
.tp_snaplen
,
817 mode
->link_type
, mode
->print_mode
);
819 if (frame_cnt_max
!= 0 && fcnt
>= frame_cnt_max
) {
826 kernel_may_pull_from_rx(&hdr
->tp_h
);
827 next_slot(&it
, &rx_ring
);
829 if (unlikely(sigint
== 1))
833 if (mode
->dump_mode
== DUMP_INTERVAL_SIZE
) {
834 interval
+= hdr
->tp_h
.tp_snaplen
;
835 if (interval
> mode
->dump_interval
) {
842 struct tpacket_stats kstats
;
843 socklen_t slen
= sizeof(kstats
);
845 fmemset(&kstats
, 0, sizeof(kstats
));
846 getsockopt(sock
, SOL_PACKET
, PACKET_STATISTICS
,
849 fd
= next_multi_pcap_file(mode
, fd
);
852 if (mode
->print_mode
== FNTTYPE_PRINT_NONE
) {
853 printf(".(+%lu/-%lu)",
854 1UL * kstats
.tp_packets
-
856 skipped
, 1UL * kstats
.tp_drops
+
864 poll(&rx_poll
, 1, -1);
865 poll_error_maybe_die(sock
, &rx_poll
);
868 bug_on(gettimeofday(&end
, NULL
));
869 diff
= tv_subtract(end
, start
);
871 if (!(mode
->dump_dir
&& mode
->print_mode
== FNTTYPE_PRINT_NONE
)) {
872 sock_print_net_stats(sock
, skipped
);
873 printf("\r%12lu sec, %lu usec in total\n", diff
.tv_sec
,
880 bpf_release(&bpf_ops
);
881 dissector_cleanup_all();
882 destroy_rx_ring(sock
, &rx_ring
);
884 if (mode
->promiscuous
== true)
885 leave_promiscuous_mode(mode
->device_in
, ifflags
);
888 leave_rfmon_mac80211(mode
->device_trans
, mode
->device_in
);
894 finish_multi_pcap_file(mode
, fd
);
896 finish_single_pcap_file(mode
, fd
);
900 static void help(void)
902 printf("\nnetsniff-ng %s, the packet sniffing beast\n", VERSION_STRING
);
903 puts("http://www.netsniff-ng.org\n\n"
904 "Usage: netsniff-ng [options]\n"
906 " -i|-d|--dev|--in <dev|pcap> Input source as netdev or pcap\n"
907 " -o|--out <dev|pcap|dir|txf> Output sink as netdev, pcap, directory, txf file\n"
908 " -f|--filter <bpf-file> Use BPF filter file from bpfc\n"
909 " -t|--type <type> Only handle packets of defined type:\n"
910 " host|broadcast|multicast|others|outgoing\n"
911 " -F|--interval <size/time> Dump interval in time or size if -o is a directory\n"
912 " pcap swap spec: <num>KiB/MiB/GiB/s/sec/min/hrs\n"
913 " -J|--jumbo-support Support for 64KB Super Jumbo Frames\n"
914 " Default RX/TX slot: 2048Byte\n"
915 " -R|--rfraw Capture or inject raw 802.11 frames\n"
916 " -n|--num <uint> Number of packets until exit\n"
917 " `-- 0 Loop until interrupted (default)\n"
918 " `- n Send n packets and done\n"
919 "Options for printing:\n"
920 " -s|--silent Do not print captured packets\n"
921 " -q|--less Print less-verbose packet information\n"
922 " -X|--hex Print packet data in hex format\n"
923 " -l|--ascii Print human-readable packet data\n"
924 "Options, advanced:\n"
925 " -P|--prefix <name> Prefix for pcaps stored in directory\n"
926 " -r|--rand Randomize packet forwarding order\n"
927 " -M|--no-promisc No promiscuous mode for netdev\n"
928 " -A|--no-sock-mem Don't tune core socket memory\n"
929 " -m|--mmap Mmap pcap file i.e., for replaying\n"
930 " -g|--sg Scatter/gather pcap file I/O\n"
931 " -c|--clrw Use slower read(2)/write(2) I/O\n"
932 " -S|--ring-size <size> Manually set ring size to <size>:\n"
933 " mmap space in KiB/MiB/GiB, e.g. \'10MiB\'\n"
934 " -k|--kernel-pull <uint> Kernel pull from user interval in us\n"
935 " Default is 10us where the TX_RING\n"
936 " is populated with payload from uspace\n"
937 " -b|--bind-cpu <cpu> Bind to specific CPU (or CPU-range)\n"
938 " -B|--unbind-cpu <cpu> Forbid to use specific CPU (or CPU-range)\n"
939 " -H|--prio-high Make this high priority process\n"
940 " -Q|--notouch-irq Do not touch IRQ CPU affinity of NIC\n"
941 " -v|--version Show version\n"
942 " -h|--help Guess what?!\n\n"
944 " netsniff-ng --in eth0 --out dump.pcap --silent --bind-cpu 0\n"
945 " netsniff-ng --in wlan0 --rfraw --out dump.pcap --silent --bind-cpu 0\n"
946 " netsniff-ng --in dump.pcap --mmap --out eth0 --silent --bind-cpu 0\n"
947 " netsniff-ng --in dump.pcap --out dump.txf --silent --bind-cpu 0\n"
948 " netsniff-ng --in eth0 --out eth1 --silent --bind-cpu 0 --type host\n"
949 " netsniff-ng --in eth1 --out /opt/probe/ -s -m -J --interval 100MiB -b 0\n"
950 " netsniff-ng --in any --filter http.bpf --jumbo-support --ascii\n\n"
952 " This tool is targeted for network developers! You should\n"
953 " be aware of what you are doing and what these options above\n"
954 " mean! Use netsniff-ng's bpfc compiler for generating filter files.\n"
955 " Further, netsniff-ng automatically enables the kernel BPF JIT\n"
956 " if present. Txf file output is only possible if the input source\n"
957 " is a pcap file.\n\n"
958 "Please report bugs to <bugs@netsniff-ng.org>\n"
959 "Copyright (C) 2009-2012 Daniel Borkmann <daniel@netsniff-ng.org>\n"
960 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel@netsniff-ng.org>\n"
961 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
962 "License: GNU GPL version 2.0\n"
963 "This is free software: you are free to change and redistribute it.\n"
964 "There is NO WARRANTY, to the extent permitted by law.\n");
968 static void version(void)
970 printf("\nnetsniff-ng %s, the packet sniffing beast\n", VERSION_STRING
);
971 puts("http://www.netsniff-ng.org\n\n"
972 "Please report bugs to <bugs@netsniff-ng.org>\n"
973 "Copyright (C) 2009-2012 Daniel Borkmann <daniel@netsniff-ng.org>\n"
974 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel@netsniff-ng.org>\n"
975 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
976 "License: GNU GPL version 2.0\n"
977 "This is free software: you are free to change and redistribute it.\n"
978 "There is NO WARRANTY, to the extent permitted by law.\n");
982 static void header(void)
985 colorize_start(bold
),
986 "netsniff-ng " VERSION_STRING
,
990 int main(int argc
, char **argv
)
992 int c
, i
, j
, opt_index
, ops_touched
= 0;
995 bool prio_high
= false;
996 bool setsockmem
= true;
998 void (*enter_mode
)(struct mode
*mode
) = NULL
;
1000 fmemset(&mode
, 0, sizeof(mode
));
1001 mode
.link_type
= LINKTYPE_EN10MB
;
1002 mode
.print_mode
= FNTTYPE_PRINT_NORM
;
1003 mode
.cpu
= CPU_UNKNOWN
;
1004 mode
.packet_type
= PACKET_ALL
;
1005 mode
.promiscuous
= true;
1006 mode
.randomize
= false;
1007 mode
.pcap
= PCAP_OPS_SG
;
1008 mode
.dump_interval
= DUMP_INTERVAL_DEF
;
1009 mode
.dump_mode
= DUMP_INTERVAL_TIME
;
1011 while ((c
= getopt_long(argc
, argv
, short_options
, long_options
,
1012 &opt_index
)) != EOF
) {
1016 mode
.device_in
= xstrdup(optarg
);
1019 mode
.device_out
= xstrdup(optarg
);
1022 mode
.prefix
= xstrdup(optarg
);
1025 mode
.link_type
= LINKTYPE_IEEE802_11
;
1029 mode
.randomize
= true;
1032 mode
.jumbo_support
= 1;
1035 mode
.filter
= xstrdup(optarg
);
1038 mode
.promiscuous
= false;
1044 if (!strncmp(optarg
, "host", strlen("host")))
1045 mode
.packet_type
= PACKET_HOST
;
1046 else if (!strncmp(optarg
, "broadcast", strlen("broadcast")))
1047 mode
.packet_type
= PACKET_BROADCAST
;
1048 else if (!strncmp(optarg
, "multicast", strlen("multicast")))
1049 mode
.packet_type
= PACKET_MULTICAST
;
1050 else if (!strncmp(optarg
, "others", strlen("others")))
1051 mode
.packet_type
= PACKET_OTHERHOST
;
1052 else if (!strncmp(optarg
, "outgoing", strlen("outgoing")))
1053 mode
.packet_type
= PACKET_OUTGOING
;
1055 mode
.packet_type
= PACKET_ALL
;
1059 mode
.reserve_size
= 0;
1061 for (j
= i
= strlen(optarg
); i
> 0; --i
) {
1062 if (!isdigit(optarg
[j
- i
]))
1067 if (!strncmp(ptr
, "KiB", strlen("KiB")))
1068 mode
.reserve_size
= 1 << 10;
1069 else if (!strncmp(ptr
, "MiB", strlen("MiB")))
1070 mode
.reserve_size
= 1 << 20;
1071 else if (!strncmp(ptr
, "GiB", strlen("GiB")))
1072 mode
.reserve_size
= 1 << 30;
1074 panic("Syntax error in ring size param!\n");
1077 mode
.reserve_size
*= atoi(optarg
);
1080 set_cpu_affinity(optarg
, 0);
1081 if (mode
.cpu
!= CPU_NOTOUCH
)
1082 mode
.cpu
= atoi(optarg
);
1085 set_cpu_affinity(optarg
, 1);
1091 mode
.pcap
= PCAP_OPS_RW
;
1095 mode
.pcap
= PCAP_OPS_MMAP
;
1099 mode
.pcap
= PCAP_OPS_SG
;
1103 mode
.cpu
= CPU_NOTOUCH
;
1106 mode
.print_mode
= FNTTYPE_PRINT_NONE
;
1109 mode
.print_mode
= FNTTYPE_PRINT_LESS
;
1112 mode
.print_mode
= (mode
.print_mode
== FNTTYPE_PRINT_ASCII
) ?
1113 FNTTYPE_PRINT_HEX_ASCII
: FNTTYPE_PRINT_HEX
;
1116 mode
.print_mode
= (mode
.print_mode
== FNTTYPE_PRINT_HEX
) ?
1117 FNTTYPE_PRINT_HEX_ASCII
: FNTTYPE_PRINT_ASCII
;
1120 mode
.kpull
= (unsigned long) atol(optarg
);
1123 frame_cnt_max
= (unsigned long) atol(optarg
);
1127 mode
.dump_interval
= 0;
1129 for (j
= i
= strlen(optarg
); i
> 0; --i
) {
1130 if (!isdigit(optarg
[j
- i
]))
1135 if (!strncmp(ptr
, "KiB", strlen("KiB"))) {
1136 mode
.dump_interval
= 1 << 10;
1137 mode
.dump_mode
= DUMP_INTERVAL_SIZE
;
1138 } else if (!strncmp(ptr
, "MiB", strlen("MiB"))) {
1139 mode
.dump_interval
= 1 << 20;
1140 mode
.dump_mode
= DUMP_INTERVAL_SIZE
;
1141 } else if (!strncmp(ptr
, "GiB", strlen("GiB"))) {
1142 mode
.dump_interval
= 1 << 30;
1143 mode
.dump_mode
= DUMP_INTERVAL_SIZE
;
1144 } else if (!strncmp(ptr
, "sec", strlen("sec"))) {
1145 mode
.dump_interval
= 1;
1146 mode
.dump_mode
= DUMP_INTERVAL_TIME
;
1147 } else if (!strncmp(ptr
, "min", strlen("min"))) {
1148 mode
.dump_interval
= 60;
1149 mode
.dump_mode
= DUMP_INTERVAL_TIME
;
1150 } else if (!strncmp(ptr
, "hrs", strlen("hrs"))) {
1151 mode
.dump_interval
= 60 * 60;
1152 mode
.dump_mode
= DUMP_INTERVAL_TIME
;
1153 } else if (!strncmp(ptr
, "s", strlen("s"))) {
1154 mode
.dump_interval
= 1;
1155 mode
.dump_mode
= DUMP_INTERVAL_TIME
;
1157 panic("Syntax error in time/size param!\n");
1161 mode
.dump_interval
*= (unsigned long) atoi(optarg
);
1184 panic("Option -%c requires an argument!\n",
1187 if (isprint(optopt
))
1188 whine("Unknown option character "
1189 "`0x%X\'!\n", optopt
);
1197 if (!mode
.device_in
)
1198 mode
.device_in
= xstrdup("any");
1200 register_signal(SIGINT
, signal_handler
);
1201 register_signal(SIGHUP
, signal_handler
);
1203 init_pcap(mode
.jumbo_support
);
1207 if (prio_high
== true) {
1208 set_proc_prio(get_default_proc_prio());
1209 set_sched_status(get_default_sched_policy(),
1210 get_default_sched_prio());
1213 if (setsockmem
== true) {
1214 if ((vals
[0] = get_system_socket_mem(sock_rmem_max
)) < SMEM_SUG_MAX
)
1215 set_system_socket_mem(sock_rmem_max
, SMEM_SUG_MAX
);
1216 if ((vals
[1] = get_system_socket_mem(sock_rmem_def
)) < SMEM_SUG_DEF
)
1217 set_system_socket_mem(sock_rmem_def
, SMEM_SUG_DEF
);
1218 if ((vals
[2] = get_system_socket_mem(sock_wmem_max
)) < SMEM_SUG_MAX
)
1219 set_system_socket_mem(sock_wmem_max
, SMEM_SUG_MAX
);
1220 if ((vals
[3] = get_system_socket_mem(sock_wmem_def
)) < SMEM_SUG_DEF
)
1221 set_system_socket_mem(sock_wmem_def
, SMEM_SUG_DEF
);
1224 if (mode
.device_in
&& (device_mtu(mode
.device_in
) ||
1225 !strncmp("any", mode
.device_in
, strlen(mode
.device_in
)))) {
1226 if (!mode
.device_out
) {
1228 enter_mode
= enter_mode_rx_only_or_dump
;
1229 } else if (device_mtu(mode
.device_out
)) {
1230 register_signal_f(SIGALRM
, timer_elapsed
, SA_SIGINFO
);
1231 enter_mode
= enter_mode_rx_to_tx
;
1234 register_signal_f(SIGALRM
, timer_next_dump
, SA_SIGINFO
);
1235 enter_mode
= enter_mode_rx_only_or_dump
;
1237 mode
.pcap
= PCAP_OPS_SG
;
1240 if (mode
.device_out
&& device_mtu(mode
.device_out
)) {
1241 register_signal_f(SIGALRM
, timer_elapsed
, SA_SIGINFO
);
1242 enter_mode
= enter_mode_pcap_to_tx
;
1244 mode
.pcap
= PCAP_OPS_MMAP
;
1246 enter_mode
= enter_mode_read_pcap
;
1248 mode
.pcap
= PCAP_OPS_SG
;
1253 panic("Selection not supported!\n");
1259 if (setsockmem
== true) {
1260 set_system_socket_mem(sock_rmem_max
, vals
[0]);
1261 set_system_socket_mem(sock_rmem_def
, vals
[1]);
1262 set_system_socket_mem(sock_wmem_max
, vals
[2]);
1263 set_system_socket_mem(sock_wmem_def
, vals
[3]);
1266 free(mode
.device_in
);
1267 free(mode
.device_out
);
1268 free(mode
.device_trans
);