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
);
224 set_sockopt_hwtimestamp(tx_sock
, ctx
->device_out
);
226 setup_tx_ring_layout(tx_sock
, &tx_ring
, size
, ctx
->jumbo
);
227 create_tx_ring(tx_sock
, &tx_ring
, ctx
->verbose
);
228 mmap_tx_ring(tx_sock
, &tx_ring
);
229 alloc_tx_ring_frames(&tx_ring
);
230 bind_tx_ring(tx_sock
, &tx_ring
, ifindex
);
232 dissector_init_all(ctx
->print_mode
);
234 if (ctx
->cpu
>= 0 && ifindex
> 0) {
235 irq
= device_irq_number(ctx
->device_out
);
236 device_bind_irq_to_cpu(irq
, ctx
->cpu
);
239 printf("IRQ: %s:%d > CPU%d\n",
240 ctx
->device_out
, irq
, ctx
->cpu
);
244 interval
= ctx
->kpull
;
246 set_itimer_interval_value(&itimer
, 0, interval
);
247 setitimer(ITIMER_REAL
, &itimer
, NULL
);
249 drop_privileges(ctx
->enforce
, ctx
->uid
, ctx
->gid
);
251 printf("Running! Hang up with ^C!\n\n");
254 bug_on(gettimeofday(&start
, NULL
));
256 while (likely(sigint
== 0)) {
257 while (user_may_pull_from_tx(tx_ring
.frames
[it
].iov_base
)) {
258 hdr
= tx_ring
.frames
[it
].iov_base
;
259 out
= ((uint8_t *) hdr
) + TPACKET2_HDRLEN
- sizeof(struct sockaddr_ll
);
262 ret
= __pcap_io
->read_pcap(fd
, &phdr
, ctx
->magic
, out
,
263 ring_frame_size(&tx_ring
));
264 if (unlikely(ret
<= 0))
267 if (ring_frame_size(&tx_ring
) <
268 pcap_get_length(&phdr
, ctx
->magic
)) {
269 pcap_set_length(&phdr
, ctx
->magic
,
270 ring_frame_size(&tx_ring
));
273 } while (ctx
->filter
&&
274 !bpf_run_filter(&bpf_ops
, out
,
275 pcap_get_length(&phdr
, ctx
->magic
)));
277 pcap_pkthdr_to_tpacket_hdr(&phdr
, ctx
->magic
, &hdr
->tp_h
, &hdr
->s_ll
);
279 ctx
->tx_bytes
+= hdr
->tp_h
.tp_len
;;
282 show_frame_hdr(hdr
, ctx
->print_mode
);
284 dissector_entry_point(out
, hdr
->tp_h
.tp_snaplen
,
285 ctx
->link_type
, ctx
->print_mode
);
287 kernel_may_pull_from_tx(&hdr
->tp_h
);
290 if (it
>= tx_ring
.layout
.tp_frame_nr
)
293 if (unlikely(sigint
== 1))
296 if (frame_count_max
!= 0) {
297 if (ctx
->tx_packets
>= frame_count_max
) {
307 bug_on(gettimeofday(&end
, NULL
));
308 timersub(&end
, &start
, &diff
);
312 bpf_release(&bpf_ops
);
314 dissector_cleanup_all();
315 destroy_tx_ring(tx_sock
, &tx_ring
);
318 leave_rfmon_mac80211(ctx
->device_trans
, ctx
->device_out
);
320 if (__pcap_io
->prepare_close_pcap
)
321 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_RD
);
323 if (!strncmp("-", ctx
->device_in
, strlen("-")))
324 dup2(fd
, fileno(stdin
));
331 printf("\r%12lu packets outgoing\n", ctx
->tx_packets
);
332 printf("\r%12lu packets truncated in file\n", trunced
);
333 printf("\r%12lu bytes outgoing\n", ctx
->tx_bytes
);
334 printf("\r%12lu sec, %lu usec in total\n", diff
.tv_sec
, diff
.tv_usec
);
337 static void receive_to_xmit(struct ctx
*ctx
)
341 int rx_sock
, ifindex_in
, ifindex_out
;
342 unsigned int size_in
, size_out
, it_in
= 0, it_out
= 0;
343 unsigned long frame_count
= 0;
344 struct frame_map
*hdr_in
, *hdr_out
;
345 struct ring tx_ring
, rx_ring
;
346 struct pollfd rx_poll
;
347 struct sock_fprog bpf_ops
;
349 if (!strncmp(ctx
->device_in
, ctx
->device_out
, IFNAMSIZ
))
350 panic("Ingress/egress devices must be different!\n");
351 if (!device_up_and_running(ctx
->device_out
))
352 panic("Egress device not up and running!\n");
354 rx_sock
= pf_socket();
355 tx_sock
= pf_socket();
357 fmemset(&tx_ring
, 0, sizeof(tx_ring
));
358 fmemset(&rx_ring
, 0, sizeof(rx_ring
));
359 fmemset(&rx_poll
, 0, sizeof(rx_poll
));
360 fmemset(&bpf_ops
, 0, sizeof(bpf_ops
));
362 ifindex_in
= device_ifindex(ctx
->device_in
);
363 ifindex_out
= device_ifindex(ctx
->device_out
);
365 size_in
= ring_size(ctx
->device_in
, ctx
->reserve_size
);
366 size_out
= ring_size(ctx
->device_out
, ctx
->reserve_size
);
368 enable_kernel_bpf_jit_compiler();
370 bpf_parse_rules(ctx
->filter
, &bpf_ops
, ctx
->link_type
);
372 bpf_dump_all(&bpf_ops
);
373 bpf_attach_to_sock(rx_sock
, &bpf_ops
);
375 setup_rx_ring_layout(rx_sock
, &rx_ring
, size_in
, ctx
->jumbo
);
376 create_rx_ring(rx_sock
, &rx_ring
, ctx
->verbose
);
377 mmap_rx_ring(rx_sock
, &rx_ring
);
378 alloc_rx_ring_frames(&rx_ring
);
379 bind_rx_ring(rx_sock
, &rx_ring
, ifindex_in
);
380 prepare_polling(rx_sock
, &rx_poll
);
382 set_packet_loss_discard(tx_sock
);
383 setup_tx_ring_layout(tx_sock
, &tx_ring
, size_out
, ctx
->jumbo
);
384 create_tx_ring(tx_sock
, &tx_ring
, ctx
->verbose
);
385 mmap_tx_ring(tx_sock
, &tx_ring
);
386 alloc_tx_ring_frames(&tx_ring
);
387 bind_tx_ring(tx_sock
, &tx_ring
, ifindex_out
);
389 dissector_init_all(ctx
->print_mode
);
391 if (ctx
->promiscuous
)
392 ifflags
= enter_promiscuous_mode(ctx
->device_in
);
395 interval
= ctx
->kpull
;
397 set_itimer_interval_value(&itimer
, 0, interval
);
398 setitimer(ITIMER_REAL
, &itimer
, NULL
);
400 drop_privileges(ctx
->enforce
, ctx
->uid
, ctx
->gid
);
402 printf("Running! Hang up with ^C!\n\n");
405 while (likely(sigint
== 0)) {
406 while (user_may_pull_from_rx(rx_ring
.frames
[it_in
].iov_base
)) {
409 hdr_in
= rx_ring
.frames
[it_in
].iov_base
;
410 in
= ((uint8_t *) hdr_in
) + hdr_in
->tp_h
.tp_mac
;
414 if (ctx
->packet_type
!= -1)
415 if (ctx
->packet_type
!= hdr_in
->s_ll
.sll_pkttype
)
418 hdr_out
= tx_ring
.frames
[it_out
].iov_base
;
419 out
= ((uint8_t *) hdr_out
) + TPACKET2_HDRLEN
- sizeof(struct sockaddr_ll
);
421 for (; !user_may_pull_from_tx(tx_ring
.frames
[it_out
].iov_base
) &&
424 next_rnd_slot(&it_out
, &tx_ring
);
427 if (it_out
>= tx_ring
.layout
.tp_frame_nr
)
431 hdr_out
= tx_ring
.frames
[it_out
].iov_base
;
432 out
= ((uint8_t *) hdr_out
) + TPACKET2_HDRLEN
- sizeof(struct sockaddr_ll
);
435 tpacket_hdr_clone(&hdr_out
->tp_h
, &hdr_in
->tp_h
);
436 fmemcpy(out
, in
, hdr_in
->tp_h
.tp_len
);
438 kernel_may_pull_from_tx(&hdr_out
->tp_h
);
440 next_rnd_slot(&it_out
, &tx_ring
);
443 if (it_out
>= tx_ring
.layout
.tp_frame_nr
)
447 show_frame_hdr(hdr_in
, ctx
->print_mode
);
449 dissector_entry_point(in
, hdr_in
->tp_h
.tp_snaplen
,
450 ctx
->link_type
, ctx
->print_mode
);
452 if (frame_count_max
!= 0) {
453 if (frame_count
>= frame_count_max
) {
461 kernel_may_pull_from_rx(&hdr_in
->tp_h
);
464 if (it_in
>= rx_ring
.layout
.tp_frame_nr
)
467 if (unlikely(sigint
== 1))
471 poll(&rx_poll
, 1, -1);
478 sock_print_net_stats(rx_sock
, 0);
480 bpf_release(&bpf_ops
);
482 dissector_cleanup_all();
484 destroy_tx_ring(tx_sock
, &tx_ring
);
485 destroy_rx_ring(rx_sock
, &rx_ring
);
487 if (ctx
->promiscuous
)
488 leave_promiscuous_mode(ctx
->device_in
, ifflags
);
494 static void translate_pcap_to_txf(int fdo
, uint8_t *out
, size_t len
)
496 size_t bytes_done
= 0;
499 slprintf(bout
, sizeof(bout
), "{\n ");
500 write_or_die(fdo
, bout
, strlen(bout
));
502 while (bytes_done
< len
) {
503 slprintf(bout
, sizeof(bout
), "0x%02x, ", out
[bytes_done
]);
504 write_or_die(fdo
, bout
, strlen(bout
));
508 if (bytes_done
% 10 == 0) {
509 slprintf(bout
, sizeof(bout
), "\n");
510 write_or_die(fdo
, bout
, strlen(bout
));
512 if (bytes_done
< len
) {
513 slprintf(bout
, sizeof(bout
), " ");
514 write_or_die(fdo
, bout
, strlen(bout
));
518 if (bytes_done
% 10 != 0) {
519 slprintf(bout
, sizeof(bout
), "\n");
520 write_or_die(fdo
, bout
, strlen(bout
));
523 slprintf(bout
, sizeof(bout
), "}\n\n");
524 write_or_die(fdo
, bout
, strlen(bout
));
527 static void read_pcap(struct ctx
*ctx
)
531 int ret
, fd
, fdo
= 0;
532 unsigned long trunced
= 0;
535 struct sock_fprog bpf_ops
;
537 struct timeval start
, end
, diff
;
538 struct sockaddr_ll sll
;
542 if (!strncmp("-", ctx
->device_in
, strlen("-"))) {
543 fd
= dup(fileno(stdin
));
544 close(fileno(stdin
));
545 if (ctx
->pcap
== PCAP_OPS_MM
)
546 ctx
->pcap
= PCAP_OPS_SG
;
548 fd
= open_or_die(ctx
->device_in
, O_RDONLY
| O_LARGEFILE
| O_NOATIME
);
551 if (__pcap_io
->init_once_pcap
)
552 __pcap_io
->init_once_pcap();
554 ret
= __pcap_io
->pull_fhdr_pcap(fd
, &ctx
->magic
, &ctx
->link_type
);
556 panic("Error reading pcap header!\n");
558 if (__pcap_io
->prepare_access_pcap
) {
559 ret
= __pcap_io
->prepare_access_pcap(fd
, PCAP_MODE_RD
, ctx
->jumbo
);
561 panic("Error prepare reading pcap!\n");
564 fmemset(&fm
, 0, sizeof(fm
));
565 fmemset(&bpf_ops
, 0, sizeof(bpf_ops
));
567 bpf_parse_rules(ctx
->filter
, &bpf_ops
, ctx
->link_type
);
569 bpf_dump_all(&bpf_ops
);
571 dissector_init_all(ctx
->print_mode
);
573 out_len
= round_up(1024 * 1024, PAGE_SIZE
);
574 out
= xmalloc_aligned(out_len
, CO_CACHE_LINE_SIZE
);
576 if (ctx
->device_out
) {
577 if (!strncmp("-", ctx
->device_out
, strlen("-"))) {
578 fdo
= dup(fileno(stdout
));
579 close(fileno(stdout
));
581 fdo
= open_or_die_m(ctx
->device_out
, O_RDWR
| O_CREAT
|
582 O_TRUNC
| O_LARGEFILE
, DEFFILEMODE
);
586 drop_privileges(ctx
->enforce
, ctx
->uid
, ctx
->gid
);
588 printf("Running! Hang up with ^C!\n\n");
591 bug_on(gettimeofday(&start
, NULL
));
593 while (likely(sigint
== 0)) {
595 ret
= __pcap_io
->read_pcap(fd
, &phdr
, ctx
->magic
,
597 if (unlikely(ret
< 0))
600 if (unlikely(pcap_get_length(&phdr
, ctx
->magic
) == 0)) {
605 if (unlikely(pcap_get_length(&phdr
, ctx
->magic
) > out_len
)) {
606 pcap_set_length(&phdr
, ctx
->magic
, out_len
);
609 } while (ctx
->filter
&&
610 !bpf_run_filter(&bpf_ops
, out
,
611 pcap_get_length(&phdr
, ctx
->magic
)));
613 pcap_pkthdr_to_tpacket_hdr(&phdr
, ctx
->magic
, &fm
.tp_h
, &sll
);
615 ctx
->tx_bytes
+= fm
.tp_h
.tp_len
;
618 show_frame_hdr(&fm
, ctx
->print_mode
);
620 dissector_entry_point(out
, fm
.tp_h
.tp_snaplen
,
621 ctx
->link_type
, ctx
->print_mode
);
624 translate_pcap_to_txf(fdo
, out
, fm
.tp_h
.tp_snaplen
);
626 if (frame_count_max
!= 0) {
627 if (ctx
->tx_packets
>= frame_count_max
) {
636 bug_on(gettimeofday(&end
, NULL
));
637 timersub(&end
, &start
, &diff
);
639 bpf_release(&bpf_ops
);
641 dissector_cleanup_all();
643 if (__pcap_io
->prepare_close_pcap
)
644 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_RD
);
650 printf("\r%12lu packets outgoing\n", ctx
->tx_packets
);
651 printf("\r%12lu packets truncated in file\n", trunced
);
652 printf("\r%12lu bytes outgoing\n", ctx
->tx_bytes
);
653 printf("\r%12lu sec, %lu usec in total\n", diff
.tv_sec
, diff
.tv_usec
);
655 if (!strncmp("-", ctx
->device_in
, strlen("-")))
656 dup2(fd
, fileno(stdin
));
659 if (ctx
->device_out
) {
660 if (!strncmp("-", ctx
->device_out
, strlen("-")))
661 dup2(fdo
, fileno(stdout
));
666 static void finish_multi_pcap_file(struct ctx
*ctx
, int fd
)
668 __pcap_io
->fsync_pcap(fd
);
670 if (__pcap_io
->prepare_close_pcap
)
671 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_WR
);
675 fmemset(&itimer
, 0, sizeof(itimer
));
676 setitimer(ITIMER_REAL
, &itimer
, NULL
);
679 static int next_multi_pcap_file(struct ctx
*ctx
, int fd
)
684 __pcap_io
->fsync_pcap(fd
);
686 if (__pcap_io
->prepare_close_pcap
)
687 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_WR
);
691 slprintf(fname
, sizeof(fname
), "%s/%s%lu.pcap", ctx
->device_out
,
692 ctx
->prefix
? : "dump-", time(0));
694 fd
= open_or_die_m(fname
, O_RDWR
| O_CREAT
| O_TRUNC
|
695 O_LARGEFILE
, DEFFILEMODE
);
697 ret
= __pcap_io
->push_fhdr_pcap(fd
, ctx
->magic
, ctx
->link_type
);
699 panic("Error writing pcap header!\n");
701 if (__pcap_io
->prepare_access_pcap
) {
702 ret
= __pcap_io
->prepare_access_pcap(fd
, PCAP_MODE_WR
, ctx
->jumbo
);
704 panic("Error prepare writing pcap!\n");
710 static int begin_multi_pcap_file(struct ctx
*ctx
)
717 if (ctx
->device_out
[strlen(ctx
->device_out
) - 1] == '/')
718 ctx
->device_out
[strlen(ctx
->device_out
) - 1] = 0;
720 slprintf(fname
, sizeof(fname
), "%s/%s%lu.pcap", ctx
->device_out
,
721 ctx
->prefix
? : "dump-", time(0));
723 fd
= open_or_die_m(fname
, O_RDWR
| O_CREAT
| O_TRUNC
|
724 O_LARGEFILE
, DEFFILEMODE
);
726 ret
= __pcap_io
->push_fhdr_pcap(fd
, ctx
->magic
, ctx
->link_type
);
728 panic("Error writing pcap header!\n");
730 if (__pcap_io
->prepare_access_pcap
) {
731 ret
= __pcap_io
->prepare_access_pcap(fd
, PCAP_MODE_WR
, ctx
->jumbo
);
733 panic("Error prepare writing pcap!\n");
736 if (ctx
->dump_mode
== DUMP_INTERVAL_TIME
) {
737 interval
= ctx
->dump_interval
;
739 set_itimer_interval_value(&itimer
, interval
, 0);
740 setitimer(ITIMER_REAL
, &itimer
, NULL
);
748 static void finish_single_pcap_file(struct ctx
*ctx
, int fd
)
750 __pcap_io
->fsync_pcap(fd
);
752 if (__pcap_io
->prepare_close_pcap
)
753 __pcap_io
->prepare_close_pcap(fd
, PCAP_MODE_WR
);
755 if (strncmp("-", ctx
->device_out
, strlen("-")))
758 dup2(fd
, fileno(stdout
));
761 static int begin_single_pcap_file(struct ctx
*ctx
)
767 if (!strncmp("-", ctx
->device_out
, strlen("-"))) {
768 fd
= dup(fileno(stdout
));
769 close(fileno(stdout
));
770 if (ctx
->pcap
== PCAP_OPS_MM
)
771 ctx
->pcap
= PCAP_OPS_SG
;
773 fd
= open_or_die_m(ctx
->device_out
,
774 O_RDWR
| O_CREAT
| O_TRUNC
|
775 O_LARGEFILE
, DEFFILEMODE
);
778 ret
= __pcap_io
->push_fhdr_pcap(fd
, ctx
->magic
, ctx
->link_type
);
780 panic("Error writing pcap header!\n");
782 if (__pcap_io
->prepare_access_pcap
) {
783 ret
= __pcap_io
->prepare_access_pcap(fd
, PCAP_MODE_WR
, ctx
->jumbo
);
785 panic("Error prepare writing pcap!\n");
791 static void print_pcap_file_stats(int sock
, struct ctx
*ctx
, unsigned long skipped
)
794 unsigned long good
, bad
;
795 struct tpacket_stats kstats
;
796 socklen_t slen
= sizeof(kstats
);
798 fmemset(&kstats
, 0, sizeof(kstats
));
800 ret
= getsockopt(sock
, SOL_PACKET
, PACKET_STATISTICS
, &kstats
, &slen
);
802 panic("Cannot get packet statistics!\n");
804 if (ctx
->print_mode
== PRINT_NONE
) {
805 good
= kstats
.tp_packets
- kstats
.tp_drops
- skipped
;
806 bad
= kstats
.tp_drops
+ skipped
;
808 printf(".(+%lu/-%lu)", good
, bad
);
813 static void recv_only_or_dump(struct ctx
*ctx
)
817 int sock
, irq
, ifindex
, fd
= 0, ret
;
818 unsigned int size
, it
= 0;
819 unsigned long frame_count
= 0, skipped
= 0;
821 struct pollfd rx_poll
;
822 struct frame_map
*hdr
;
823 struct sock_fprog bpf_ops
;
824 struct timeval start
, end
, diff
;
830 ctx
->device_trans
= xstrdup(ctx
->device_in
);
831 xfree(ctx
->device_in
);
833 enter_rfmon_mac80211(ctx
->device_trans
, &ctx
->device_in
);
834 ctx
->link_type
= LINKTYPE_IEEE802_11
;
837 fmemset(&rx_ring
, 0, sizeof(rx_ring
));
838 fmemset(&rx_poll
, 0, sizeof(rx_poll
));
839 fmemset(&bpf_ops
, 0, sizeof(bpf_ops
));
841 ifindex
= device_ifindex(ctx
->device_in
);
843 size
= ring_size(ctx
->device_in
, ctx
->reserve_size
);
845 enable_kernel_bpf_jit_compiler();
847 bpf_parse_rules(ctx
->filter
, &bpf_ops
, ctx
->link_type
);
849 bpf_dump_all(&bpf_ops
);
850 bpf_attach_to_sock(sock
, &bpf_ops
);
852 set_sockopt_hwtimestamp(sock
, ctx
->device_in
);
854 setup_rx_ring_layout(sock
, &rx_ring
, size
, ctx
->jumbo
);
855 create_rx_ring(sock
, &rx_ring
, ctx
->verbose
);
856 mmap_rx_ring(sock
, &rx_ring
);
857 alloc_rx_ring_frames(&rx_ring
);
858 bind_rx_ring(sock
, &rx_ring
, ifindex
);
860 prepare_polling(sock
, &rx_poll
);
861 dissector_init_all(ctx
->print_mode
);
863 if (ctx
->cpu
>= 0 && ifindex
> 0) {
864 irq
= device_irq_number(ctx
->device_in
);
865 device_bind_irq_to_cpu(irq
, ctx
->cpu
);
868 printf("IRQ: %s:%d > CPU%d\n",
869 ctx
->device_in
, irq
, ctx
->cpu
);
872 if (ctx
->promiscuous
)
873 ifflags
= enter_promiscuous_mode(ctx
->device_in
);
875 if (dump_to_pcap(ctx
) && __pcap_io
->init_once_pcap
)
876 __pcap_io
->init_once_pcap();
878 drop_privileges(ctx
->enforce
, ctx
->uid
, ctx
->gid
);
880 if (dump_to_pcap(ctx
)) {
884 fmemset(&stats
, 0, sizeof(stats
));
885 ret
= stat(ctx
->device_out
, &stats
);
891 ctx
->dump_dir
= S_ISDIR(stats
.st_mode
);
893 fd
= begin_multi_pcap_file(ctx
);
896 fd
= begin_single_pcap_file(ctx
);
900 printf("Running! Hang up with ^C!\n\n");
903 bug_on(gettimeofday(&start
, NULL
));
905 while (likely(sigint
== 0)) {
906 while (user_may_pull_from_rx(rx_ring
.frames
[it
].iov_base
)) {
909 hdr
= rx_ring
.frames
[it
].iov_base
;
910 packet
= ((uint8_t *) hdr
) + hdr
->tp_h
.tp_mac
;
913 if (ctx
->packet_type
!= -1)
914 if (ctx
->packet_type
!= hdr
->s_ll
.sll_pkttype
)
917 if (unlikely(ring_frame_size(&rx_ring
) < hdr
->tp_h
.tp_snaplen
)) {
922 if (dump_to_pcap(ctx
)) {
923 tpacket_hdr_to_pcap_pkthdr(&hdr
->tp_h
, &hdr
->s_ll
, &phdr
, ctx
->magic
);
925 ret
= __pcap_io
->write_pcap(fd
, &phdr
, ctx
->magic
, packet
,
926 pcap_get_length(&phdr
, ctx
->magic
));
927 if (unlikely(ret
!= pcap_get_total_length(&phdr
, ctx
->magic
)))
928 panic("Write error to pcap!\n");
931 show_frame_hdr(hdr
, ctx
->print_mode
);
933 dissector_entry_point(packet
, hdr
->tp_h
.tp_snaplen
,
934 ctx
->link_type
, ctx
->print_mode
);
936 if (frame_count_max
!= 0) {
937 if (frame_count
>= frame_count_max
) {
945 kernel_may_pull_from_rx(&hdr
->tp_h
);
948 if (it
>= rx_ring
.layout
.tp_frame_nr
)
951 if (unlikely(sigint
== 1))
954 if (dump_to_pcap(ctx
)) {
955 if (ctx
->dump_mode
== DUMP_INTERVAL_SIZE
) {
956 interval
+= hdr
->tp_h
.tp_snaplen
;
958 if (interval
> ctx
->dump_interval
) {
965 fd
= next_multi_pcap_file(ctx
, fd
);
969 print_pcap_file_stats(sock
, ctx
, skipped
);
974 poll(&rx_poll
, 1, -1);
977 bug_on(gettimeofday(&end
, NULL
));
978 timersub(&end
, &start
, &diff
);
980 if (!(ctx
->dump_dir
&& ctx
->print_mode
== PRINT_NONE
)) {
981 sock_print_net_stats(sock
, skipped
);
983 printf("\r%12lu sec, %lu usec in total\n",
984 diff
.tv_sec
, diff
.tv_usec
);
990 bpf_release(&bpf_ops
);
991 dissector_cleanup_all();
992 destroy_rx_ring(sock
, &rx_ring
);
994 if (ctx
->promiscuous
)
995 leave_promiscuous_mode(ctx
->device_in
, ifflags
);
998 leave_rfmon_mac80211(ctx
->device_trans
, ctx
->device_in
);
1000 if (dump_to_pcap(ctx
)) {
1002 finish_multi_pcap_file(ctx
, fd
);
1004 finish_single_pcap_file(ctx
, fd
);
1010 static void help(void)
1012 printf("\nnetsniff-ng %s, the packet sniffing beast\n", VERSION_STRING
);
1013 puts("http://www.netsniff-ng.org\n\n"
1014 "Usage: netsniff-ng [options] [filter-expression]\n"
1016 " -i|-d|--dev|--in <dev|pcap|-> Input source as netdev, pcap or pcap stdin\n"
1017 " -o|--out <dev|pcap|dir|cfg|-> Output sink as netdev, pcap, directory, trafgen, or stdout\n"
1018 " -f|--filter <bpf-file|expr> Use BPF filter file from bpfc or tcpdump-like expression\n"
1019 " -t|--type <type> Filter for: host|broadcast|multicast|others|outgoing\n"
1020 " -F|--interval <size|time> Dump interval if -o is a dir: <num>KiB/MiB/GiB/s/sec/min/hrs\n"
1021 " -J|--jumbo-support Support for 64KB Super Jumbo Frames (def: 2048B)\n"
1022 " -R|--rfraw Capture or inject raw 802.11 frames\n"
1023 " -n|--num <0|uint> Number of packets until exit (def: 0)\n"
1024 " -P|--prefix <name> Prefix for pcaps stored in directory\n"
1025 " -T|--magic <pcap-magic> Pcap magic number/pcap format to store, see -D\n"
1026 " -D|--dump-pcap-types Dump pcap types and magic numbers and quit\n"
1027 " -B|--dump-bpf Dump generated BPF assembly\n"
1028 " -r|--rand Randomize packet forwarding order (dev->dev)\n"
1029 " -M|--no-promisc No promiscuous mode for netdev\n"
1030 " -A|--no-sock-mem Don't tune core socket memory\n"
1031 " -m|--mmap Mmap(2) pcap file i.e., for replaying pcaps\n"
1032 " -G|--sg Scatter/gather pcap file I/O\n"
1033 " -c|--clrw Use slower read(2)/write(2) I/O\n"
1034 " -S|--ring-size <size> Specify ring size to: <num>KiB/MiB/GiB\n"
1035 " -k|--kernel-pull <uint> Kernel pull from user interval in us (def: 10us)\n"
1036 " -b|--bind-cpu <cpu> Bind to specific CPU\n"
1037 " -u|--user <userid> Drop privileges and change to userid\n"
1038 " -g|--group <groupid> Drop privileges and change to groupid\n"
1039 " -H|--prio-high Make this high priority process\n"
1040 " -Q|--notouch-irq Do not touch IRQ CPU affinity of NIC\n"
1041 " -s|--silent Do not print captured packets\n"
1042 " -q|--less Print less-verbose packet information\n"
1043 " -X|--hex Print packet data in hex format\n"
1044 " -l|--ascii Print human-readable packet data\n"
1045 " -U|--update Update GeoIP databases\n"
1046 " -V|--verbose Be more verbose\n"
1047 " -v|--version Show version\n"
1048 " -h|--help Guess what?!\n\n"
1050 " netsniff-ng --in eth0 --out dump.pcap -s -T 0xa1b2c3d4 --b 0 tcp or udp\n"
1051 " netsniff-ng --in wlan0 --rfraw --out dump.pcap --silent --bind-cpu 0\n"
1052 " netsniff-ng --in dump.pcap --mmap --out eth0 -k1000 --silent --bind-cpu 0\n"
1053 " netsniff-ng --in dump.pcap --out dump.cfg --silent --bind-cpu 0\n"
1054 " netsniff-ng --in eth0 --out eth1 --silent --bind-cpu 0 --type host\n"
1055 " netsniff-ng --in eth1 --out /opt/probe/ -s -m -J --interval 100MiB -b 0\n"
1056 " netsniff-ng --in vlan0 --out dump.pcap -c -u `id -u bob` -g `id -g bob`\n"
1057 " netsniff-ng --in any --filter http.bpf --jumbo-support --ascii -V\n\n"
1059 " For introducing bit errors, delays with random variation and more\n"
1060 " while replaying pcaps, make use of tc(8) with its disciplines (e.g. netem).\n\n"
1061 "Please report bugs to <bugs@netsniff-ng.org>\n"
1062 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
1063 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel.roullit@gmail.com>\n"
1064 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1065 "Swiss federal institute of technology (ETH Zurich)\n"
1066 "License: GNU GPL version 2.0\n"
1067 "This is free software: you are free to change and redistribute it.\n"
1068 "There is NO WARRANTY, to the extent permitted by law.\n");
1072 static void version(void)
1074 printf("\nnetsniff-ng %s, the packet sniffing beast\n", VERSION_STRING
);
1075 puts("http://www.netsniff-ng.org\n\n"
1076 "Please report bugs to <bugs@netsniff-ng.org>\n"
1077 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
1078 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel.roullit@gmail.com>\n"
1079 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1080 "Swiss federal institute of technology (ETH Zurich)\n"
1081 "License: GNU GPL version 2.0\n"
1082 "This is free software: you are free to change and redistribute it.\n"
1083 "There is NO WARRANTY, to the extent permitted by law.\n");
1087 int main(int argc
, char **argv
)
1090 int c
, i
, j
, cpu_tmp
, opt_index
, ops_touched
= 0, vals
[4] = {0};
1091 bool prio_high
= false, setsockmem
= true;
1092 void (*main_loop
)(struct ctx
*ctx
) = NULL
;
1094 .link_type
= LINKTYPE_EN10MB
,
1095 .print_mode
= PRINT_NORM
,
1098 .promiscuous
= true,
1100 .pcap
= PCAP_OPS_SG
,
1101 .dump_interval
= 60,
1102 .dump_mode
= DUMP_INTERVAL_TIME
,
1105 .magic
= ORIGINAL_TCPDUMP_MAGIC
,
1110 while ((c
= getopt_long(argc
, argv
, short_options
, long_options
,
1111 &opt_index
)) != EOF
) {
1115 ctx
.device_in
= xstrdup(optarg
);
1118 ctx
.device_out
= xstrdup(optarg
);
1121 ctx
.prefix
= xstrdup(optarg
);
1124 ctx
.link_type
= LINKTYPE_IEEE802_11
;
1128 ctx
.randomize
= true;
1134 ctx
.magic
= (uint32_t) strtoul(optarg
, NULL
, 0);
1135 pcap_check_magic(ctx
.magic
);
1138 ctx
.filter
= xstrdup(optarg
);
1141 ctx
.promiscuous
= false;
1147 ctx
.uid
= strtoul(optarg
, NULL
, 0);
1151 ctx
.gid
= strtoul(optarg
, NULL
, 0);
1155 if (!strncmp(optarg
, "host", strlen("host")))
1156 ctx
.packet_type
= PACKET_HOST
;
1157 else if (!strncmp(optarg
, "broadcast", strlen("broadcast")))
1158 ctx
.packet_type
= PACKET_BROADCAST
;
1159 else if (!strncmp(optarg
, "multicast", strlen("multicast")))
1160 ctx
.packet_type
= PACKET_MULTICAST
;
1161 else if (!strncmp(optarg
, "others", strlen("others")))
1162 ctx
.packet_type
= PACKET_OTHERHOST
;
1163 else if (!strncmp(optarg
, "outgoing", strlen("outgoing")))
1164 ctx
.packet_type
= PACKET_OUTGOING
;
1166 ctx
.packet_type
= -1;
1170 ctx
.reserve_size
= 0;
1172 for (j
= i
= strlen(optarg
); i
> 0; --i
) {
1173 if (!isdigit(optarg
[j
- i
]))
1178 if (!strncmp(ptr
, "KiB", strlen("KiB")))
1179 ctx
.reserve_size
= 1 << 10;
1180 else if (!strncmp(ptr
, "MiB", strlen("MiB")))
1181 ctx
.reserve_size
= 1 << 20;
1182 else if (!strncmp(ptr
, "GiB", strlen("GiB")))
1183 ctx
.reserve_size
= 1 << 30;
1185 panic("Syntax error in ring size param!\n");
1188 ctx
.reserve_size
*= strtol(optarg
, NULL
, 0);
1191 cpu_tmp
= strtol(optarg
, NULL
, 0);
1193 cpu_affinity(cpu_tmp
);
1201 ctx
.pcap
= PCAP_OPS_RW
;
1205 ctx
.pcap
= PCAP_OPS_MM
;
1209 ctx
.pcap
= PCAP_OPS_SG
;
1216 ctx
.print_mode
= PRINT_NONE
;
1219 ctx
.print_mode
= PRINT_LESS
;
1223 (ctx
.print_mode
== PRINT_ASCII
) ?
1224 PRINT_HEX_ASCII
: PRINT_HEX
;
1228 (ctx
.print_mode
== PRINT_HEX
) ?
1229 PRINT_HEX_ASCII
: PRINT_ASCII
;
1232 ctx
.kpull
= strtol(optarg
, NULL
, 0);
1235 frame_count_max
= strtol(optarg
, NULL
, 0);
1239 ctx
.dump_interval
= 0;
1241 for (j
= i
= strlen(optarg
); i
> 0; --i
) {
1242 if (!isdigit(optarg
[j
- i
]))
1247 if (!strncmp(ptr
, "KiB", strlen("KiB"))) {
1248 ctx
.dump_interval
= 1 << 10;
1249 ctx
.dump_mode
= DUMP_INTERVAL_SIZE
;
1250 } else if (!strncmp(ptr
, "MiB", strlen("MiB"))) {
1251 ctx
.dump_interval
= 1 << 20;
1252 ctx
.dump_mode
= DUMP_INTERVAL_SIZE
;
1253 } else if (!strncmp(ptr
, "GiB", strlen("GiB"))) {
1254 ctx
.dump_interval
= 1 << 30;
1255 ctx
.dump_mode
= DUMP_INTERVAL_SIZE
;
1256 } else if (!strncmp(ptr
, "sec", strlen("sec"))) {
1257 ctx
.dump_interval
= 1;
1258 ctx
.dump_mode
= DUMP_INTERVAL_TIME
;
1259 } else if (!strncmp(ptr
, "min", strlen("min"))) {
1260 ctx
.dump_interval
= 60;
1261 ctx
.dump_mode
= DUMP_INTERVAL_TIME
;
1262 } else if (!strncmp(ptr
, "hrs", strlen("hrs"))) {
1263 ctx
.dump_interval
= 60 * 60;
1264 ctx
.dump_mode
= DUMP_INTERVAL_TIME
;
1265 } else if (!strncmp(ptr
, "s", strlen("s"))) {
1266 ctx
.dump_interval
= 1;
1267 ctx
.dump_mode
= DUMP_INTERVAL_TIME
;
1269 panic("Syntax error in time/size param!\n");
1273 ctx
.dump_interval
*= strtol(optarg
, NULL
, 0);
1279 ctx
.dump_bpf
= true;
1282 pcap_dump_type_features();
1312 panic("Option -%c requires an argument!\n",
1315 if (isprint(optopt
))
1316 printf("Unknown option character `0x%X\'!\n", optopt
);
1324 if (!ctx
.filter
&& optind
!= argc
) {
1328 for (i
= optind
; i
< argc
; ++i
) {
1329 size_t alen
= strlen(argv
[i
]) + 2;
1330 size_t flen
= ctx
.filter
? strlen(ctx
.filter
) : 0;
1332 ctx
.filter
= xrealloc(ctx
.filter
, 1, flen
+ alen
);
1333 ret
= slprintf(ctx
.filter
+ offset
, strlen(argv
[i
]) + 2, "%s ", argv
[i
]);
1335 panic("Cannot concatenate filter string!\n");
1342 ctx
.device_in
= xstrdup("any");
1344 register_signal(SIGINT
, signal_handler
);
1345 register_signal(SIGHUP
, signal_handler
);
1350 set_proc_prio(get_default_proc_prio());
1351 set_sched_status(get_default_sched_policy(), get_default_sched_prio());
1354 if (ctx
.device_in
&& (device_mtu(ctx
.device_in
) ||
1355 !strncmp("any", ctx
.device_in
, strlen(ctx
.device_in
)))) {
1356 if (!ctx
.device_out
) {
1358 main_loop
= recv_only_or_dump
;
1359 } else if (device_mtu(ctx
.device_out
)) {
1360 register_signal_f(SIGALRM
, timer_elapsed
, SA_SIGINFO
);
1361 main_loop
= receive_to_xmit
;
1364 register_signal_f(SIGALRM
, timer_next_dump
, SA_SIGINFO
);
1365 main_loop
= recv_only_or_dump
;
1367 ctx
.pcap
= PCAP_OPS_SG
;
1370 if (ctx
.device_out
&& device_mtu(ctx
.device_out
)) {
1371 register_signal_f(SIGALRM
, timer_elapsed
, SA_SIGINFO
);
1372 main_loop
= pcap_to_xmit
;
1374 ctx
.pcap
= PCAP_OPS_MM
;
1376 main_loop
= read_pcap
;
1378 ctx
.pcap
= PCAP_OPS_SG
;
1386 set_system_socket_memory(vals
, array_size(vals
));
1395 reset_system_socket_memory(vals
, array_size(vals
));
1400 free(ctx
.device_in
);
1401 free(ctx
.device_out
);
1402 free(ctx
.device_trans
);