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 static 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
;
64 /* Pass out, if this will ever change and we do crap on it! */
65 build_bug_on(offsetof(struct tpacket_req
, tp_frame_nr
) !=
66 offsetof(struct tpacket_req3
, tp_frame_nr
) &&
67 sizeof(struct tpacket_req
) !=
68 offsetof(struct tpacket_req3
, tp_retire_blk_tov
));
70 ring
->layout3
.tp_retire_blk_tov
= 100; /* 0: let kernel decide */
71 ring
->layout3
.tp_sizeof_priv
= 0;
72 ring
->layout3
.tp_feature_req_word
= 0;
74 set_sockopt_tpacket_v3(sock
);
77 #endif /* HAVE_TPACKET3 */
79 set_sockopt_tpacket_v2(sock
);
82 ring_verify_layout(ring
);
85 static void create_rx_ring(int sock
, struct ring
*ring
, bool verbose
)
89 bool v3
= get_sockopt_tpacket(sock
) == TPACKET_V3
;
90 size_t layout_size
= v3
? sizeof(ring
->layout3
) : sizeof(ring
->layout
);
93 size_t layout_size
= sizeof(ring
->layout
);
94 #endif /* HAVE_TPACKET3 */
97 ret
= setsockopt(sock
, SOL_PACKET
, PACKET_RX_RING
, &ring
->raw
,
100 if (errno
== ENOMEM
&& ring
->layout
.tp_block_nr
> 1) {
101 ring
->layout
.tp_block_nr
>>= 1;
102 ring
->layout
.tp_frame_nr
= ring
->layout
.tp_block_size
/
103 ring
->layout
.tp_frame_size
*
104 ring
->layout
.tp_block_nr
;
108 panic("Cannot allocate RX_RING!\n");
110 ring
->mm_len
= (size_t) ring
->layout
.tp_block_size
* ring
->layout
.tp_block_nr
;
114 printf("RX,V2: %.2Lf MiB, %u Frames, each %u Byte allocated\n",
115 (long double) ring
->mm_len
/ (1 << 20),
116 ring
->layout
.tp_frame_nr
, ring
->layout
.tp_frame_size
);
118 printf("RX,V3: %.2Lf MiB, %u Blocks, each %u Byte allocated\n",
119 (long double) ring
->mm_len
/ (1 << 20),
120 ring
->layout
.tp_block_nr
, ring
->layout
.tp_block_size
);
125 static void alloc_rx_ring_frames(int sock
, struct ring
*ring
)
130 bool v3
= get_sockopt_tpacket(sock
) == TPACKET_V3
;
133 num
= ring
->layout3
.tp_block_nr
;
134 size
= ring
->layout3
.tp_block_size
;
137 #endif /* HAVE_TPACKET3 */
139 num
= ring
->layout
.tp_frame_nr
;
140 size
= ring
->layout
.tp_frame_size
;
143 alloc_ring_frames_generic(ring
, num
, size
);
146 void ring_rx_setup(struct ring
*ring
, int sock
, size_t size
, int ifindex
,
147 struct pollfd
*poll
, bool v3
, bool jumbo_support
,
150 fmemset(ring
, 0, sizeof(*ring
));
151 setup_rx_ring_layout(sock
, ring
, size
, jumbo_support
, v3
);
152 create_rx_ring(sock
, ring
, verbose
);
153 mmap_ring_generic(sock
, ring
);
154 alloc_rx_ring_frames(sock
, ring
);
155 bind_ring_generic(sock
, ring
, ifindex
, false);
156 prepare_polling(sock
, poll
);
159 void sock_rx_net_stats(int sock
, unsigned long seen
)
162 bool v3
= get_sockopt_tpacket(sock
) == TPACKET_V3
;
164 struct tpacket_stats k2
;
165 struct tpacket_stats_v3 k3
;
167 socklen_t slen
= v3
? sizeof(stats
.k3
) : sizeof(stats
.k2
);
169 memset(&stats
, 0, sizeof(stats
));
170 ret
= getsockopt(sock
, SOL_PACKET
, PACKET_STATISTICS
, &stats
, &slen
);
172 uint64_t packets
= stats
.k3
.tp_packets
;
173 uint64_t drops
= stats
.k3
.tp_drops
;
175 printf("\r%12"PRIu64
" packets incoming (%"PRIu64
" unread on exit)\n",
176 v3
? (uint64_t)seen
: packets
, v3
? packets
- seen
: 0);
177 printf("\r%12"PRIu64
" packets passed filter\n", packets
- drops
);
178 printf("\r%12"PRIu64
" packets failed filter (out of space)\n", drops
);
179 if (stats
.k3
.tp_packets
> 0)
180 printf("\r%12.4lf%% packet droprate\n",
181 (1.0 * drops
/ packets
) * 100.0);