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>
48 #include "opt_memcpy.h"
50 #include "dissector.h"
55 #define CPU_UNKNOWN -1
56 #define CPU_NOTOUCH -2
58 #define DUMP_INTERVAL 60
68 unsigned int reserve_size
;
72 enum pcap_ops_groups pcap
;
76 unsigned long dump_interval
;
80 unsigned long tx_bytes
;
81 unsigned long tx_packets
;
84 sig_atomic_t sigint
= 0;
87 static unsigned long frame_cnt_max
= 0;
88 static unsigned long interval
= TX_KERNEL_PULL_INT
;
89 static struct itimerval itimer
;
90 static volatile bool next_dump
= false;
92 static const char *short_options
= "d:i:o:rf:MJt:S:k:n:b:B:HQmcsqlxCXNvhF:";
94 static struct option long_options
[] = {
95 {"dev", required_argument
, 0, 'd'},
96 {"in", required_argument
, 0, 'i'},
97 {"out", required_argument
, 0, 'o'},
98 {"rand", no_argument
, 0, 'r'},
99 {"mmap", no_argument
, 0, 'm'},
100 {"clrw", no_argument
, 0, 'c'},
101 {"jumbo-support", no_argument
, 0, 'J'},
102 {"filter", required_argument
, 0, 'f'},
103 {"no-promisc", no_argument
, 0, 'M'},
104 {"num", required_argument
, 0, 'n'},
105 {"type", required_argument
, 0, 't'},
106 {"interval", required_argument
, 0, 'F'},
107 {"ring-size", required_argument
, 0, 'S'},
108 {"kernel-pull", required_argument
, 0, 'k'},
109 {"bind-cpu", required_argument
, 0, 'b'},
110 {"unbind-cpu", required_argument
, 0, 'B'},
111 {"prio-high", no_argument
, 0, 'H'},
112 {"notouch-irq", no_argument
, 0, 'Q'},
113 {"silent", no_argument
, 0, 's'},
114 {"less", no_argument
, 0, 'q'},
115 {"payload", no_argument
, 0, 'l'},
116 {"payload-hex", no_argument
, 0, 'x'},
117 {"c-style", no_argument
, 0, 'C'},
118 {"all-hex", no_argument
, 0, 'X'},
119 {"no-payload", no_argument
, 0, 'N'},
120 {"version", no_argument
, 0, 'v'},
121 {"help", no_argument
, 0, 'h'},
125 static void signal_handler(int number
)
138 static void timer_elapsed(int number
)
140 itimer
.it_interval
.tv_sec
= 0;
141 itimer
.it_interval
.tv_usec
= interval
;
142 itimer
.it_value
.tv_sec
= 0;
143 itimer
.it_value
.tv_usec
= interval
;
145 pull_and_flush_tx_ring(tx_sock
);
146 setitimer(ITIMER_REAL
, &itimer
, NULL
);
149 static void timer_next_dump(int number
)
151 itimer
.it_interval
.tv_sec
= interval
;
152 itimer
.it_interval
.tv_usec
= 0;
153 itimer
.it_value
.tv_sec
= interval
;
154 itimer
.it_value
.tv_usec
= 0;
157 setitimer(ITIMER_REAL
, &itimer
, NULL
);
160 static void enter_mode_pcap_to_tx(struct mode
*mode
)
162 int irq
, ifindex
, fd
= 0, ret
;
163 unsigned int size
, it
= 0;
165 struct frame_map
*hdr
;
166 struct sock_fprog bpf_ops
;
167 struct tx_stats stats
;
170 if (!device_up_and_running(mode
->device_out
))
171 panic("Device not up and running!\n");
174 tx_sock
= pf_socket();
176 if (!pcap_ops
[mode
->pcap
])
177 panic("pcap group not supported!\n");
178 fd
= open_or_die(mode
->device_in
, O_RDONLY
| O_LARGEFILE
| O_NOATIME
);
179 ret
= pcap_ops
[mode
->pcap
]->pull_file_header(fd
);
181 panic("error reading pcap header!\n");
182 if (pcap_ops
[mode
->pcap
]->prepare_reading_pcap
) {
183 ret
= pcap_ops
[mode
->pcap
]->prepare_reading_pcap(fd
);
185 panic("error prepare reading pcap!\n");
188 memset(&tx_ring
, 0, sizeof(tx_ring
));
189 memset(&bpf_ops
, 0, sizeof(bpf_ops
));
190 memset(&stats
, 0, sizeof(stats
));
192 ifindex
= device_ifindex(mode
->device_out
);
193 size
= ring_size(mode
->device_out
, mode
->reserve_size
);
195 bpf_parse_rules(mode
->filter
, &bpf_ops
);
197 set_packet_loss_discard(tx_sock
);
198 setup_tx_ring_layout(tx_sock
, &tx_ring
, size
, mode
->jumbo_support
);
199 create_tx_ring(tx_sock
, &tx_ring
);
200 mmap_tx_ring(tx_sock
, &tx_ring
);
201 alloc_tx_ring_frames(&tx_ring
);
202 bind_tx_ring(tx_sock
, &tx_ring
, ifindex
);
204 dissector_init_all(mode
->print_mode
);
206 if (mode
->cpu
>= 0 && ifindex
> 0) {
207 irq
= device_irq_number(mode
->device_out
);
208 device_bind_irq_to_cpu(mode
->cpu
, irq
);
209 printf("IRQ: %s:%d > CPU%d\n", mode
->device_out
, irq
,
214 interval
= mode
->kpull
;
216 itimer
.it_interval
.tv_sec
= 0;
217 itimer
.it_interval
.tv_usec
= interval
;
218 itimer
.it_value
.tv_sec
= 0;
219 itimer
.it_value
.tv_usec
= interval
;
220 setitimer(ITIMER_REAL
, &itimer
, NULL
);
223 bpf_dump_all(&bpf_ops
);
224 printf("MD: TX %luus %s\n\n", interval
, pcap_ops
[mode
->pcap
]->name
);
226 while (likely(sigint
== 0)) {
227 while (user_may_pull_from_tx(tx_ring
.frames
[it
].iov_base
)) {
228 struct pcap_pkthdr phdr
;
229 hdr
= tx_ring
.frames
[it
].iov_base
;
230 /* Kernel assumes: data = ph.raw + po->tp_hdrlen -
231 * sizeof(struct sockaddr_ll); */
232 out
= ((uint8_t *) hdr
) + TPACKET_HDRLEN
-
233 sizeof(struct sockaddr_ll
);
236 ret
= pcap_ops
[mode
->pcap
]->read_pcap_pkt(fd
, &phdr
,
237 out
, ring_frame_size(&tx_ring
));
238 if (unlikely(ret
<= 0))
240 } while (mode
->filter
&& !bpf_run_filter(&bpf_ops
, out
, phdr
.len
));
241 pcap_pkthdr_to_tpacket_hdr(&phdr
, &hdr
->tp_h
);
243 stats
.tx_bytes
+= hdr
->tp_h
.tp_len
;;
246 show_frame_hdr(hdr
, mode
->print_mode
, RING_MODE_EGRESS
);
247 dissector_entry_point(out
, hdr
->tp_h
.tp_snaplen
,
250 kernel_may_pull_from_tx(&hdr
->tp_h
);
251 next_slot(&it
, &tx_ring
);
253 if (unlikely(sigint
== 1))
255 if (frame_cnt_max
!= 0 &&
256 stats
.tx_packets
>= frame_cnt_max
) {
265 printf("\r%12lu frames outgoing\n", stats
.tx_packets
);
266 printf("\r%12lu bytes outgoing\n", stats
.tx_bytes
);
268 dissector_cleanup_all();
269 destroy_tx_ring(tx_sock
, &tx_ring
);
272 if (pcap_ops
[mode
->pcap
]->prepare_close_pcap
)
273 pcap_ops
[mode
->pcap
]->prepare_close_pcap(fd
, PCAP_MODE_READ
);
277 /* If netsniff-ngs in device is on a tap, it can efficiently filter out
278 * some interesting packets and give them to the out device for testing
279 * or debugging for instance. */
280 static void enter_mode_rx_to_tx(struct mode
*mode
)
282 int rx_sock
, ifindex_in
, ifindex_out
;
283 unsigned int size_in
, size_out
, it_in
= 0, it_out
= 0;
284 unsigned long fcnt
= 0;
287 struct frame_map
*hdr_in
, *hdr_out
;
290 struct pollfd rx_poll
;
291 struct sock_fprog bpf_ops
;
293 if (!strncmp(mode
->device_in
, mode
->device_out
,
294 strlen(mode
->device_in
)))
295 panic("Ingress/egress devices must be different!\n");
296 if (!device_up_and_running(mode
->device_out
))
297 panic("Egress device not up and running!\n");
298 if (!device_up_and_running(mode
->device_in
))
299 panic("Ingress device not up and running!\n");
302 rx_sock
= pf_socket();
303 tx_sock
= pf_socket();
305 memset(&tx_ring
, 0, sizeof(tx_ring
));
306 memset(&rx_ring
, 0, sizeof(rx_ring
));
307 memset(&rx_poll
, 0, sizeof(rx_poll
));
308 memset(&bpf_ops
, 0, sizeof(bpf_ops
));
310 ifindex_in
= device_ifindex(mode
->device_in
);
311 size_in
= ring_size(mode
->device_in
, mode
->reserve_size
);
313 ifindex_out
= device_ifindex(mode
->device_out
);
314 size_out
= ring_size(mode
->device_out
, mode
->reserve_size
);
316 enable_kernel_bpf_jit_compiler();
317 bpf_parse_rules(mode
->filter
, &bpf_ops
);
318 bpf_attach_to_sock(rx_sock
, &bpf_ops
);
320 setup_rx_ring_layout(rx_sock
, &rx_ring
, size_in
, mode
->jumbo_support
);
321 create_rx_ring(rx_sock
, &rx_ring
);
322 mmap_rx_ring(rx_sock
, &rx_ring
);
323 alloc_rx_ring_frames(&rx_ring
);
324 bind_rx_ring(rx_sock
, &rx_ring
, ifindex_in
);
325 prepare_polling(rx_sock
, &rx_poll
);
327 set_packet_loss_discard(tx_sock
);
328 setup_tx_ring_layout(tx_sock
, &tx_ring
, size_out
, mode
->jumbo_support
);
329 create_tx_ring(tx_sock
, &tx_ring
);
330 mmap_tx_ring(tx_sock
, &tx_ring
);
331 alloc_tx_ring_frames(&tx_ring
);
332 bind_tx_ring(tx_sock
, &tx_ring
, ifindex_out
);
334 mt_init_by_seed_time();
335 dissector_init_all(mode
->print_mode
);
337 if (mode
->promiscuous
== true) {
338 ifflags
= enter_promiscuous_mode(mode
->device_in
);
343 interval
= mode
->kpull
;
345 itimer
.it_interval
.tv_sec
= 0;
346 itimer
.it_interval
.tv_usec
= interval
;
347 itimer
.it_value
.tv_sec
= 0;
348 itimer
.it_value
.tv_usec
= interval
;
349 setitimer(ITIMER_REAL
, &itimer
, NULL
);
352 bpf_dump_all(&bpf_ops
);
353 printf("MD: RXTX %luus\n\n", interval
);
354 printf("Running! Hang up with ^C!\n\n");
356 while (likely(sigint
== 0)) {
357 while (user_may_pull_from_rx(rx_ring
.frames
[it_in
].iov_base
)) {
358 hdr_in
= rx_ring
.frames
[it_in
].iov_base
;
359 in
= ((uint8_t *) hdr_in
) + hdr_in
->tp_h
.tp_mac
;
361 if (mode
->packet_type
!= PACKET_ALL
)
362 if (mode
->packet_type
!= hdr_in
->s_ll
.sll_pkttype
)
365 hdr_out
= tx_ring
.frames
[it_out
].iov_base
;
366 out
= ((uint8_t *) hdr_out
) + TPACKET_HDRLEN
-
367 sizeof(struct sockaddr_ll
);
369 /* If we cannot pull, look for a different slot. */
370 for (; !user_may_pull_from_tx(tx_ring
.frames
[it_out
].iov_base
) &&
373 next_rnd_slot(&it_out
, &tx_ring
);
375 next_slot(&it_out
, &tx_ring
);
376 hdr_out
= tx_ring
.frames
[it_out
].iov_base
;
377 out
= ((uint8_t *) hdr_out
) + TPACKET_HDRLEN
-
378 sizeof(struct sockaddr_ll
);
381 tpacket_hdr_clone(&hdr_out
->tp_h
, &hdr_in
->tp_h
);
382 __memcpy(out
, in
, hdr_in
->tp_h
.tp_len
);
384 kernel_may_pull_from_tx(&hdr_out
->tp_h
);
386 next_rnd_slot(&it_out
, &tx_ring
);
388 next_slot(&it_out
, &tx_ring
);
390 /* Should actually be avoided ... */
391 show_frame_hdr(hdr_in
, mode
->print_mode
, RING_MODE_INGRESS
);
392 dissector_entry_point(in
, hdr_in
->tp_h
.tp_snaplen
,
395 if (frame_cnt_max
!= 0 && fcnt
>= frame_cnt_max
) {
400 kernel_may_pull_from_rx(&hdr_in
->tp_h
);
401 next_slot(&it_in
, &rx_ring
);
403 if (unlikely(sigint
== 1))
407 poll(&rx_poll
, 1, -1);
408 poll_error_maybe_die(rx_sock
, &rx_poll
);
411 sock_print_net_stats(rx_sock
);
413 dissector_cleanup_all();
414 destroy_tx_ring(tx_sock
, &tx_ring
);
415 destroy_rx_ring(rx_sock
, &rx_ring
);
417 if (mode
->promiscuous
== true)
418 leave_promiscuous_mode(mode
->device_in
, ifflags
);
424 static void enter_mode_read_pcap(struct mode
*mode
)
427 struct pcap_pkthdr phdr
;
428 struct sock_fprog bpf_ops
;
429 struct tx_stats stats
;
434 if (!pcap_ops
[mode
->pcap
])
435 panic("pcap group not supported!\n");
436 fd
= open_or_die(mode
->device_in
, O_RDONLY
| O_LARGEFILE
| O_NOATIME
);
437 ret
= pcap_ops
[mode
->pcap
]->pull_file_header(fd
);
439 panic("error reading pcap header!\n");
440 if (pcap_ops
[mode
->pcap
]->prepare_reading_pcap
) {
441 ret
= pcap_ops
[mode
->pcap
]->prepare_reading_pcap(fd
);
443 panic("error prepare reading pcap!\n");
446 memset(&fm
, 0, sizeof(fm
));
447 memset(&bpf_ops
, 0, sizeof(bpf_ops
));
448 memset(&stats
, 0, sizeof(stats
));
450 bpf_parse_rules(mode
->filter
, &bpf_ops
);
451 dissector_init_all(mode
->print_mode
);
453 out_len
= device_mtu("lo");
454 out
= xmalloc_aligned(out_len
, 64);
457 bpf_dump_all(&bpf_ops
);
458 printf("MD: RD %s\n\n", pcap_ops
[mode
->pcap
]->name
);
460 while (likely(sigint
== 0)) {
462 ret
= pcap_ops
[mode
->pcap
]->read_pcap_pkt(fd
, &phdr
,
464 if (unlikely(ret
<= 0))
466 } while (mode
->filter
&& !bpf_run_filter(&bpf_ops
, out
, phdr
.len
));
467 pcap_pkthdr_to_tpacket_hdr(&phdr
, &fm
.tp_h
);
469 stats
.tx_bytes
+= fm
.tp_h
.tp_len
;;
472 show_frame_hdr(&fm
, mode
->print_mode
, RING_MODE_EGRESS
);
473 dissector_entry_point(out
, fm
.tp_h
.tp_snaplen
,
476 if (frame_cnt_max
!= 0 &&
477 stats
.tx_packets
>= frame_cnt_max
) {
485 printf("\r%12lu frames outgoing\n", stats
.tx_packets
);
486 printf("\r%12lu bytes outgoing\n", stats
.tx_bytes
);
489 dissector_cleanup_all();
490 if (pcap_ops
[mode
->pcap
]->prepare_close_pcap
)
491 pcap_ops
[mode
->pcap
]->prepare_close_pcap(fd
, PCAP_MODE_READ
);
495 static void finish_multi_pcap_file(struct mode
*mode
, int fd
)
497 pcap_ops
[mode
->pcap
]->fsync_pcap(fd
);
498 if (pcap_ops
[mode
->pcap
]->prepare_close_pcap
)
499 pcap_ops
[mode
->pcap
]->prepare_close_pcap(fd
, PCAP_MODE_WRITE
);
502 memset(&itimer
, 0, sizeof(itimer
));
503 setitimer(ITIMER_REAL
, &itimer
, NULL
);
506 static int next_multi_pcap_file(struct mode
*mode
, int fd
)
511 pcap_ops
[mode
->pcap
]->fsync_pcap(fd
);
512 if (pcap_ops
[mode
->pcap
]->prepare_close_pcap
)
513 pcap_ops
[mode
->pcap
]->prepare_close_pcap(fd
, PCAP_MODE_WRITE
);
516 memset(&tmp
, 0, sizeof(tmp
));
517 snprintf(tmp
, sizeof(tmp
), "%s/%lu.pcap", mode
->device_out
, time(0));
518 tmp
[sizeof(tmp
) - 1] = 0;
520 fd
= open_or_die_m(tmp
, O_RDWR
| O_CREAT
| O_TRUNC
| O_LARGEFILE
,
522 ret
= pcap_ops
[mode
->pcap
]->push_file_header(fd
);
524 panic("error writing pcap header!\n");
525 if (pcap_ops
[mode
->pcap
]->prepare_writing_pcap
) {
526 ret
= pcap_ops
[mode
->pcap
]->prepare_writing_pcap(fd
);
528 panic("error prepare writing pcap!\n");
534 static int begin_multi_pcap_file(struct mode
*mode
)
539 if (!pcap_ops
[mode
->pcap
])
540 panic("pcap group not supported!\n");
541 if (mode
->device_out
[strlen(mode
->device_out
) - 1] == '/')
542 mode
->device_out
[strlen(mode
->device_out
) - 1] = 0;
544 memset(&tmp
, 0, sizeof(tmp
));
545 snprintf(tmp
, sizeof(tmp
), "%s/%lu.pcap", mode
->device_out
, time(0));
546 tmp
[sizeof(tmp
) - 1] = 0;
548 fd
= open_or_die_m(tmp
, O_RDWR
| O_CREAT
| O_TRUNC
| O_LARGEFILE
,
550 ret
= pcap_ops
[mode
->pcap
]->push_file_header(fd
);
552 panic("error writing pcap header!\n");
553 if (pcap_ops
[mode
->pcap
]->prepare_writing_pcap
) {
554 ret
= pcap_ops
[mode
->pcap
]->prepare_writing_pcap(fd
);
556 panic("error prepare writing pcap!\n");
559 interval
= mode
->dump_interval
;
560 itimer
.it_interval
.tv_sec
= interval
;
561 itimer
.it_interval
.tv_usec
= 0;
562 itimer
.it_value
.tv_sec
= interval
;
563 itimer
.it_value
.tv_usec
= 0;
564 setitimer(ITIMER_REAL
, &itimer
, NULL
);
569 static void finish_single_pcap_file(struct mode
*mode
, int fd
)
571 pcap_ops
[mode
->pcap
]->fsync_pcap(fd
);
572 if (pcap_ops
[mode
->pcap
]->prepare_close_pcap
)
573 pcap_ops
[mode
->pcap
]->prepare_close_pcap(fd
, PCAP_MODE_WRITE
);
577 static int begin_single_pcap_file(struct mode
*mode
)
581 if (!pcap_ops
[mode
->pcap
])
582 panic("pcap group not supported!\n");
583 fd
= open_or_die_m(mode
->device_out
,
584 O_RDWR
| O_CREAT
| O_TRUNC
| O_LARGEFILE
,
586 ret
= pcap_ops
[mode
->pcap
]->push_file_header(fd
);
588 panic("error writing pcap header!\n");
589 if (pcap_ops
[mode
->pcap
]->prepare_writing_pcap
) {
590 ret
= pcap_ops
[mode
->pcap
]->prepare_writing_pcap(fd
);
592 panic("error prepare writing pcap!\n");
598 static void enter_mode_rx_only_or_dump(struct mode
*mode
)
600 int sock
, irq
, ifindex
, fd
= 0, ret
;
601 unsigned int size
, it
= 0;
602 unsigned long fcnt
= 0;
606 struct pollfd rx_poll
;
607 struct frame_map
*hdr
;
608 struct sock_fprog bpf_ops
;
610 if (!device_up_and_running(mode
->device_in
))
611 panic("Device not up and running!\n");
618 memset(&tmp
, 0, sizeof(tmp
));
619 ret
= stat(mode
->device_out
, &tmp
);
624 mode
->dump_dir
= !!S_ISDIR(tmp
.st_mode
);
625 if (mode
->dump_dir
) {
626 fd
= begin_multi_pcap_file(mode
);
629 fd
= begin_single_pcap_file(mode
);
633 memset(&rx_ring
, 0, sizeof(rx_ring
));
634 memset(&rx_poll
, 0, sizeof(rx_poll
));
635 memset(&bpf_ops
, 0, sizeof(bpf_ops
));
637 ifindex
= device_ifindex(mode
->device_in
);
638 size
= ring_size(mode
->device_in
, mode
->reserve_size
);
640 enable_kernel_bpf_jit_compiler();
641 bpf_parse_rules(mode
->filter
, &bpf_ops
);
642 bpf_attach_to_sock(sock
, &bpf_ops
);
644 setup_rx_ring_layout(sock
, &rx_ring
, size
, mode
->jumbo_support
);
645 create_rx_ring(sock
, &rx_ring
);
646 mmap_rx_ring(sock
, &rx_ring
);
647 alloc_rx_ring_frames(&rx_ring
);
648 bind_rx_ring(sock
, &rx_ring
, ifindex
);
650 prepare_polling(sock
, &rx_poll
);
651 dissector_init_all(mode
->print_mode
);
653 if (mode
->cpu
>= 0 && ifindex
> 0) {
654 irq
= device_irq_number(mode
->device_in
);
655 device_bind_irq_to_cpu(mode
->cpu
, irq
);
656 printf("IRQ: %s:%d > CPU%d\n", mode
->device_in
, irq
,
660 if (mode
->promiscuous
== true) {
661 ifflags
= enter_promiscuous_mode(mode
->device_in
);
666 bpf_dump_all(&bpf_ops
);
667 printf("MD: RX %s\n\n", mode
->dump
? pcap_ops
[mode
->pcap
]->name
: "");
669 while (likely(sigint
== 0)) {
670 while (user_may_pull_from_rx(rx_ring
.frames
[it
].iov_base
)) {
671 hdr
= rx_ring
.frames
[it
].iov_base
;
672 packet
= ((uint8_t *) hdr
) + hdr
->tp_h
.tp_mac
;
675 if (mode
->packet_type
!= PACKET_ALL
)
676 if (mode
->packet_type
!= hdr
->s_ll
.sll_pkttype
)
678 if (unlikely(rx_ring
.layout
.tp_frame_size
<
679 hdr
->tp_h
.tp_snaplen
)) {
680 fprintf(stderr
, "Skipping too large packet! "
681 "No jumbo support selected?\n");
686 struct pcap_pkthdr phdr
;
687 tpacket_hdr_to_pcap_pkthdr(&hdr
->tp_h
, &phdr
);
688 ret
= pcap_ops
[mode
->pcap
]->write_pcap_pkt(fd
, &phdr
,
690 if (unlikely(ret
!= sizeof(phdr
) + phdr
.len
))
691 panic("Write error to pcap!\n");
694 show_frame_hdr(hdr
, mode
->print_mode
, RING_MODE_INGRESS
);
695 dissector_entry_point(packet
, hdr
->tp_h
.tp_snaplen
,
698 if (frame_cnt_max
!= 0 && fcnt
>= frame_cnt_max
) {
703 kernel_may_pull_from_rx(&hdr
->tp_h
);
704 next_slot(&it
, &rx_ring
);
706 if (unlikely(sigint
== 1))
708 if (mode
->dump
&& next_dump
) {
709 struct tpacket_stats kstats
;
710 socklen_t slen
= sizeof(kstats
);
711 memset(&kstats
, 0, sizeof(kstats
));
712 getsockopt(sock
, SOL_PACKET
, PACKET_STATISTICS
,
714 fd
= next_multi_pcap_file(mode
, fd
);
716 if (mode
->print_mode
== FNTTYPE_PRINT_NONE
) {
717 printf(".(+%u/-%u)", kstats
.tp_packets
- kstats
.tp_drops
,
724 poll(&rx_poll
, 1, -1);
725 poll_error_maybe_die(sock
, &rx_poll
);
728 if (!(mode
->dump_dir
&& mode
->print_mode
== FNTTYPE_PRINT_NONE
))
729 sock_print_net_stats(sock
);
734 dissector_cleanup_all();
735 destroy_rx_ring(sock
, &rx_ring
);
736 if (mode
->promiscuous
== true)
737 leave_promiscuous_mode(mode
->device_in
, ifflags
);
741 finish_multi_pcap_file(mode
, fd
);
743 finish_single_pcap_file(mode
, fd
);
747 static void help(void)
749 printf("\n%s %s, the packet sniffing beast\n", PROGNAME_STRING
,
751 printf("http://www.netsniff-ng.org\n\n");
752 printf("Usage: netsniff-ng [options]\n");
753 printf("Options:\n");
754 printf(" -i|-d|--dev|--in <dev|pcap> Input source as netdev or pcap\n");
755 printf(" -o|--out <dev|pcap|dir> Output sink as netdev or pcap or directory\n");
756 printf(" -f|--filter <bpf-file> Use BPF filter file from bpfc\n");
757 printf(" -t|--type <type> Only handle packets of defined type:\n");
758 printf(" host|broadcast|multicast|others|outgoing\n");
759 printf(" -F|--interval <int> Dump interval in sec if -o is a directory where\n");
760 printf(" pcap files should be stored (default: 60)\n");
761 printf(" -s|--silent Do not print captured packets\n");
762 printf(" -J|--jumbo-support Support for 64KB Super Jumbo Frames\n");
763 printf(" Default RX/TX slot: 2048Byte\n");
764 printf(" -n|--num <uint> Number of packets until exit\n");
765 printf(" `-- 0 Loop until interrupt (default)\n");
766 printf(" `- n Send n packets and done\n");
767 printf(" -r|--rand Randomize packet forwarding order\n");
768 printf(" -M|--no-promisc No promiscuous mode for netdev\n");
769 printf(" -m|--mmap Mmap pcap file i.e., for replaying\n");
770 printf(" Default: scatter/gather I/O\n");
771 printf(" -c|--clrw Instead s/g I/O use slower read/write I/O\n");
772 printf(" -S|--ring-size <size> Manually set ring size to <size>:\n");
773 printf(" mmap space in KB/MB/GB, e.g. \'10MB\'\n");
774 printf(" -k|--kernel-pull <int> Kernel pull from user interval in us\n");
775 printf(" Default is 10us where the TX_RING\n");
776 printf(" is populated with payload from uspace\n");
777 printf(" -b|--bind-cpu <cpu> Bind to specific CPU (or CPU-range)\n");
778 printf(" -B|--unbind-cpu <cpu> Forbid to use specific CPU (or CPU-range)\n");
779 printf(" -H|--prio-high Make this high priority process\n");
780 printf(" -Q|--notouch-irq Do not touch IRQ CPU affinity of NIC\n");
781 printf(" -q|--less Print less-verbose packet information\n");
782 printf(" -l|--payload Only print human-readable payload\n");
783 printf(" -x|--payload-hex Only print payload in hex format\n");
784 printf(" -C|--c-style Print full packet in trafgen/C style hex format\n");
785 printf(" -X|--all-hex Print packets in hex format\n");
786 printf(" -N|--no-payload Only print packet header\n");
787 printf(" -v|--version Show version\n");
788 printf(" -h|--help Guess what?!\n");
790 printf("Examples:\n");
791 printf(" netsniff-ng --in eth0 --out dump.pcap --silent --bind-cpu 0\n");
792 printf(" netsniff-ng --in dump.pcap --mmap --out eth0 --silent --bind-cpu 0\n");
793 printf(" netsniff-ng --in any --filter icmp.bpf --all-hex\n");
794 printf(" netsniff-ng --in eth0 --out eth1 --silent --bind-cpu 0 --type host --filter http.bpf\n");
795 printf(" netsniff-ng --in any --filter http.bpf --payload\n");
796 printf(" netsniff-ng --in wlan0 --out /opt/probe1/ --silent --interval 30 --bind-cpu 0 --jumbo-support\n");
799 printf(" This tool is targeted for network developers! You should\n");
800 printf(" be aware of what you are doing and what these options above\n");
801 printf(" mean! Use netsniff-ng's bpfc compiler for generating filter files.\n");
802 printf(" Further, netsniff-ng automatically enables the kernel BPF JIT\n");
803 printf(" if present.\n");
805 printf("Please report bugs to <bugs@netsniff-ng.org>\n");
806 printf("Copyright (C) 2009-2012 Daniel Borkmann <daniel@netsniff-ng.org>\n");
807 printf("Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel@netsniff-ng.org>\n");
808 printf("License: GNU GPL version 2\n");
809 printf("This is free software: you are free to change and redistribute it.\n");
810 printf("There is NO WARRANTY, to the extent permitted by law.\n\n");
814 static void version(void)
816 printf("\n%s %s, the packet sniffing beast\n", PROGNAME_STRING
,
818 printf("http://www.netsniff-ng.org\n\n");
819 printf("Please report bugs to <bugs@netsniff-ng.org>\n");
820 printf("Copyright (C) 2009-2012 Daniel Borkmann <daniel@netsniff-ng.org>\n");
821 printf("Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel@netsniff-ng.org>\n");
822 printf("License: GNU GPL version 2\n");
823 printf("This is free software: you are free to change and redistribute it.\n");
824 printf("There is NO WARRANTY, to the extent permitted by law.\n\n");
828 static void header(void)
830 printf("%s%s%s\n", colorize_start(bold
), PROGNAME_STRING
" "
831 VERSION_STRING
, colorize_end());
834 int main(int argc
, char **argv
)
836 int c
, i
, j
, opt_index
;
838 bool prio_high
= false;
840 void (*enter_mode
)(struct mode
*mode
) = NULL
;
842 check_for_root_maybe_die();
844 memset(&mode
, 0, sizeof(mode
));
845 mode
.link_type
= LINKTYPE_EN10MB
;
846 mode
.print_mode
= FNTTYPE_PRINT_NORM
;
847 mode
.cpu
= CPU_UNKNOWN
;
848 mode
.packet_type
= PACKET_ALL
;
849 mode
.promiscuous
= true;
850 mode
.randomize
= false;
851 mode
.pcap
= PCAP_OPS_SG
;
852 mode
.dump_interval
= DUMP_INTERVAL
;
854 while ((c
= getopt_long(argc
, argv
, short_options
, long_options
,
855 &opt_index
)) != EOF
) {
859 mode
.device_in
= xstrdup(optarg
);
862 mode
.device_out
= xstrdup(optarg
);
865 mode
.randomize
= true;
868 mode
.jumbo_support
= 1;
871 mode
.filter
= xstrdup(optarg
);
874 mode
.promiscuous
= false;
877 if (!strncmp(optarg
, "host", strlen("host")))
878 mode
.packet_type
= PACKET_HOST
;
879 else if (!strncmp(optarg
, "broadcast", strlen("broadcast")))
880 mode
.packet_type
= PACKET_BROADCAST
;
881 else if (!strncmp(optarg
, "multicast", strlen("multicast")))
882 mode
.packet_type
= PACKET_MULTICAST
;
883 else if (!strncmp(optarg
, "others", strlen("others")))
884 mode
.packet_type
= PACKET_OTHERHOST
;
885 else if (!strncmp(optarg
, "outgoing", strlen("outgoing")))
886 mode
.packet_type
= PACKET_OUTGOING
;
888 mode
.packet_type
= PACKET_ALL
;
892 mode
.reserve_size
= 0;
894 for (j
= i
= strlen(optarg
); i
> 0; --i
) {
895 if (!isdigit(optarg
[j
- i
]))
900 if (!strncmp(ptr
, "KB", strlen("KB")))
901 mode
.reserve_size
= 1 << 10;
902 else if (!strncmp(ptr
, "MB", strlen("MB")))
903 mode
.reserve_size
= 1 << 20;
904 else if (!strncmp(ptr
, "GB", strlen("GB")))
905 mode
.reserve_size
= 1 << 30;
907 panic("Syntax error in ring size param!\n");
910 mode
.reserve_size
*= atoi(optarg
);
913 set_cpu_affinity(optarg
, 0);
914 if (mode
.cpu
!= CPU_NOTOUCH
)
915 mode
.cpu
= atoi(optarg
);
918 set_cpu_affinity(optarg
, 1);
924 mode
.pcap
= PCAP_OPS_RW
;
927 mode
.pcap
= PCAP_OPS_MMAP
;
930 mode
.cpu
= CPU_NOTOUCH
;
933 mode
.print_mode
= FNTTYPE_PRINT_NONE
;
936 mode
.print_mode
= FNTTYPE_PRINT_LESS
;
939 mode
.print_mode
= FNTTYPE_PRINT_CHR1
;
942 mode
.print_mode
= FNTTYPE_PRINT_HEX1
;
945 mode
.print_mode
= FNTTYPE_PRINT_PAAC
;
948 mode
.print_mode
= FNTTYPE_PRINT_HEX2
;
951 mode
.print_mode
= FNTTYPE_PRINT_NOPA
;
954 mode
.kpull
= (unsigned long) atol(optarg
);
957 frame_cnt_max
= (unsigned long) atol(optarg
);
960 mode
.dump_interval
= (unsigned long) atol(optarg
);
982 panic("Option -%c requires an argument!\n",
986 whine("Unknown option character "
987 "`0x%X\'!\n", optopt
);
996 mode
.device_in
= xstrdup("any");
998 register_signal(SIGINT
, signal_handler
);
999 register_signal(SIGHUP
, signal_handler
);
1001 init_pcap(mode
.jumbo_support
);
1005 if (prio_high
== true) {
1006 set_proc_prio(get_default_proc_prio());
1007 set_sched_status(get_default_sched_policy(),
1008 get_default_sched_prio());
1011 if (mode
.device_in
&& (device_mtu(mode
.device_in
) ||
1012 !strncmp("any", mode
.device_in
, strlen(mode
.device_in
)))) {
1013 if (!mode
.device_out
) {
1015 enter_mode
= enter_mode_rx_only_or_dump
;
1016 } else if (device_mtu(mode
.device_out
)) {
1017 register_signal_f(SIGALRM
, timer_elapsed
, SA_SIGINFO
);
1018 enter_mode
= enter_mode_rx_to_tx
;
1021 register_signal_f(SIGALRM
, timer_next_dump
, SA_SIGINFO
);
1022 enter_mode
= enter_mode_rx_only_or_dump
;
1025 if (mode
.device_out
&& device_mtu(mode
.device_out
)) {
1026 register_signal_f(SIGALRM
, timer_elapsed
, SA_SIGINFO
);
1027 enter_mode
= enter_mode_pcap_to_tx
;
1029 enter_mode
= enter_mode_read_pcap
;
1034 panic("Selection not supported!\n");
1041 xfree(mode
.device_in
);
1042 if (mode
.device_out
)
1043 xfree(mode
.device_out
);