2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2009, 2010 Daniel Borkmann.
4 * Subject to the GPL, version 2.
13 #include <sys/types.h>
14 #include <sys/socket.h>
15 #include <arpa/inet.h>
16 #include <linux/if_ether.h>
23 void destroy_rx_ring(int sock
, struct ring
*ring
)
26 bool v3
= get_sockopt_tpacket(sock
) == TPACKET_V3
;
28 munmap(ring
->mm_space
, ring
->mm_len
);
33 /* In general, this is freed during close(2) anyway. */
37 fmemset(&ring
->layout
, 0, sizeof(ring
->layout
));
38 ret
= setsockopt(sock
, SOL_PACKET
, PACKET_RX_RING
, &ring
->layout
,
39 sizeof(ring
->layout
));
41 panic("Cannot destroy the RX_RING: %s!\n", strerror(errno
));
44 void setup_rx_ring_layout(int sock
, struct ring
*ring
, size_t size
,
45 bool jumbo_support
, bool v3
)
47 fmemset(&ring
->layout
, 0, sizeof(ring
->layout
));
49 ring
->layout
.tp_block_size
= (jumbo_support
?
50 RUNTIME_PAGE_SIZE
<< 4 :
51 RUNTIME_PAGE_SIZE
<< 2);
53 ring
->layout
.tp_frame_size
= (jumbo_support
?
54 TPACKET_ALIGNMENT
<< 12 :
55 TPACKET_ALIGNMENT
<< 7);
57 ring
->layout
.tp_block_nr
= size
/ ring
->layout
.tp_block_size
;
58 ring
->layout
.tp_frame_nr
= ring
->layout
.tp_block_size
/
59 ring
->layout
.tp_frame_size
*
60 ring
->layout
.tp_block_nr
;
62 /* Pass out, if this will ever change and we do crap on it! */
63 build_bug_on(offsetof(struct tpacket_req
, tp_frame_nr
) !=
64 offsetof(struct tpacket_req3
, tp_frame_nr
) &&
65 sizeof(struct tpacket_req
) !=
66 offsetof(struct tpacket_req3
, tp_retire_blk_tov
));
68 ring
->layout3
.tp_retire_blk_tov
= 100; /* 0: let kernel decide */
69 ring
->layout3
.tp_sizeof_priv
= 0;
70 ring
->layout3
.tp_feature_req_word
= 0;
72 set_sockopt_tpacket_v3(sock
);
74 set_sockopt_tpacket_v2(sock
);
77 ring_verify_layout(ring
);
80 void create_rx_ring(int sock
, struct ring
*ring
, bool verbose
)
83 bool v3
= get_sockopt_tpacket(sock
) == TPACKET_V3
;
86 ret
= setsockopt(sock
, SOL_PACKET
, PACKET_RX_RING
, &ring
->raw
,
87 v3
? sizeof(ring
->layout3
) : sizeof(ring
->layout
));
89 if (errno
== ENOMEM
&& ring
->layout
.tp_block_nr
> 1) {
90 ring
->layout
.tp_block_nr
>>= 1;
91 ring
->layout
.tp_frame_nr
= ring
->layout
.tp_block_size
/
92 ring
->layout
.tp_frame_size
*
93 ring
->layout
.tp_block_nr
;
97 panic("Cannot allocate RX_RING!\n");
99 ring
->mm_len
= (size_t) ring
->layout
.tp_block_size
* ring
->layout
.tp_block_nr
;
103 printf("RX,V2: %.2Lf MiB, %u Frames, each %u Byte allocated\n",
104 (long double) ring
->mm_len
/ (1 << 20),
105 ring
->layout
.tp_frame_nr
, ring
->layout
.tp_frame_size
);
107 printf("RX,V3: %.2Lf MiB, %u Blocks, each %u Byte allocated\n",
108 (long double) ring
->mm_len
/ (1 << 20),
109 ring
->layout
.tp_block_nr
, ring
->layout
.tp_block_size
);
114 void mmap_rx_ring(int sock
, struct ring
*ring
)
116 mmap_ring_generic(sock
, ring
);
119 void alloc_rx_ring_frames(int sock
, struct ring
*ring
)
123 bool v3
= get_sockopt_tpacket(sock
) == TPACKET_V3
;
126 num
= ring
->layout3
.tp_block_nr
;
127 size
= ring
->layout3
.tp_block_size
;
129 num
= ring
->layout
.tp_frame_nr
;
130 size
= ring
->layout
.tp_frame_size
;
133 alloc_ring_frames_generic(ring
, num
, size
);
136 void bind_rx_ring(int sock
, struct ring
*ring
, int ifindex
)
138 bind_ring_generic(sock
, ring
, ifindex
, false);
141 void sock_rx_net_stats(int sock
, unsigned long seen
)
144 bool v3
= get_sockopt_tpacket(sock
) == TPACKET_V3
;
146 struct tpacket_stats k2
;
147 struct tpacket_stats_v3 k3
;
149 socklen_t slen
= v3
? sizeof(stats
.k3
) : sizeof(stats
.k2
);
151 memset(&stats
, 0, sizeof(stats
));
152 ret
= getsockopt(sock
, SOL_PACKET
, PACKET_STATISTICS
, &stats
, &slen
);
154 uint64_t packets
= stats
.k3
.tp_packets
;
155 uint64_t drops
= stats
.k3
.tp_drops
;
157 printf("\r%12"PRIu64
" packets incoming (%"PRIu64
" unread on exit)\n",
158 v3
? (uint64_t)seen
: packets
, v3
? packets
- seen
: 0);
159 printf("\r%12"PRIu64
" packets passed filter\n", packets
- drops
);
160 printf("\r%12"PRIu64
" packets failed filter (out of space)\n", drops
);
161 if (stats
.k3
.tp_packets
> 0)
162 printf("\r%12.4lf%% packet droprate\n",
163 (1.0 * drops
/ packets
) * 100.0);