2 * netsniff-ng - the packet sniffing beast
3 * By Daniel Borkmann <daniel@netsniff-ng.org>
4 * Copyright 2009, 2010 Daniel Borkmann.
5 * 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
)
25 fmemset(&ring
->layout
, 0, sizeof(ring
->layout
));
26 setsockopt(sock
, SOL_PACKET
, PACKET_RX_RING
, &ring
->layout
,
27 sizeof(ring
->layout
));
29 munmap(ring
->mm_space
, ring
->mm_len
);
35 void setup_rx_ring_layout(int sock
, struct ring
*ring
, unsigned int size
,
38 fmemset(&ring
->layout
, 0, sizeof(ring
->layout
));
40 ring
->layout
.tp_block_size
= (jumbo_support
?
43 ring
->layout
.tp_frame_size
= (jumbo_support
?
44 TPACKET_ALIGNMENT
<< 12 :
45 TPACKET_ALIGNMENT
<< 7);
46 ring
->layout
.tp_block_nr
= size
/ ring
->layout
.tp_block_size
;
47 ring
->layout
.tp_frame_nr
= ring
->layout
.tp_block_size
/
48 ring
->layout
.tp_frame_size
*
49 ring
->layout
.tp_block_nr
;
51 bug_on(ring
->layout
.tp_block_size
< ring
->layout
.tp_frame_size
);
52 bug_on((ring
->layout
.tp_block_size
% ring
->layout
.tp_frame_size
) != 0);
53 bug_on((ring
->layout
.tp_block_size
% getpagesize()) != 0);
56 void create_rx_ring(int sock
, struct ring
*ring
)
60 ret
= setsockopt(sock
, SOL_PACKET
, PACKET_RX_RING
, &ring
->layout
,
61 sizeof(ring
->layout
));
62 if (errno
== ENOMEM
&& ring
->layout
.tp_block_nr
> 1) {
63 ring
->layout
.tp_block_nr
>>= 1;
64 ring
->layout
.tp_frame_nr
= ring
->layout
.tp_block_size
/
65 ring
->layout
.tp_frame_size
*
66 ring
->layout
.tp_block_nr
;
71 panic("Cannot allocate RX_RING!\n");
73 ring
->mm_len
= ring
->layout
.tp_block_size
* ring
->layout
.tp_block_nr
;
75 printf("RX: %.2f MiB, %u Frames, each %u Byte allocated\n",
76 1.f
* ring
->mm_len
/ (1 << 20),
77 ring
->layout
.tp_frame_nr
, ring
->layout
.tp_frame_size
);
80 void mmap_rx_ring(int sock
, struct ring
*ring
)
82 ring
->mm_space
= mmap(0, ring
->mm_len
, PROT_READ
| PROT_WRITE
,
83 MAP_SHARED
| MAP_LOCKED
| MAP_POPULATE
, sock
, 0);
84 if (ring
->mm_space
== MAP_FAILED
) {
85 destroy_rx_ring(sock
, ring
);
86 panic("Cannot mmap RX_RING!\n");
90 void alloc_rx_ring_frames(struct ring
*ring
)
93 size_t len
= ring
->layout
.tp_frame_nr
* sizeof(*ring
->frames
);
95 ring
->frames
= xmalloc_aligned(len
, CO_CACHE_LINE_SIZE
);
96 fmemset(ring
->frames
, 0, len
);
98 for (i
= 0; i
< ring
->layout
.tp_frame_nr
; ++i
) {
99 ring
->frames
[i
].iov_len
= ring
->layout
.tp_frame_size
;
100 ring
->frames
[i
].iov_base
= ring
->mm_space
+
101 (i
* ring
->layout
.tp_frame_size
);
105 void bind_rx_ring(int sock
, struct ring
*ring
, int ifindex
)
109 * The RX_RING registers itself to the networking stack with
110 * dev_add_pack(), so we have one single RX_RING for all devs
111 * otherwise you'll get the packet twice.
113 fmemset(&ring
->s_ll
, 0, sizeof(ring
->s_ll
));
115 ring
->s_ll
.sll_family
= AF_PACKET
;
116 ring
->s_ll
.sll_protocol
= htons(ETH_P_ALL
);
117 ring
->s_ll
.sll_ifindex
= ifindex
;
118 ring
->s_ll
.sll_hatype
= 0;
119 ring
->s_ll
.sll_halen
= 0;
120 ring
->s_ll
.sll_pkttype
= 0;
122 ret
= bind(sock
, (struct sockaddr
*) &ring
->s_ll
, sizeof(ring
->s_ll
));
124 destroy_rx_ring(sock
, ring
);
125 panic("Cannot bind RX_RING!\n");