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
, int verbose
)
60 set_sockopt_tpacket(sock
);
62 ret
= setsockopt(sock
, SOL_PACKET
, PACKET_RX_RING
, &ring
->layout
,
63 sizeof(ring
->layout
));
64 if (errno
== ENOMEM
&& ring
->layout
.tp_block_nr
> 1) {
65 ring
->layout
.tp_block_nr
>>= 1;
66 ring
->layout
.tp_frame_nr
= ring
->layout
.tp_block_size
/
67 ring
->layout
.tp_frame_size
*
68 ring
->layout
.tp_block_nr
;
73 panic("Cannot allocate RX_RING!\n");
75 ring
->mm_len
= ring
->layout
.tp_block_size
* ring
->layout
.tp_block_nr
;
78 printf("RX: %.2Lf MiB, %u Frames, each %u Byte allocated\n",
79 (long double) ring
->mm_len
/ (1 << 20),
80 ring
->layout
.tp_frame_nr
, ring
->layout
.tp_frame_size
);
84 void mmap_rx_ring(int sock
, struct ring
*ring
)
86 ring
->mm_space
= mmap(0, ring
->mm_len
, PROT_READ
| PROT_WRITE
,
87 MAP_SHARED
| MAP_LOCKED
| MAP_POPULATE
, sock
, 0);
88 if (ring
->mm_space
== MAP_FAILED
) {
89 destroy_rx_ring(sock
, ring
);
90 panic("Cannot mmap RX_RING!\n");
94 void alloc_rx_ring_frames(struct ring
*ring
)
97 size_t len
= ring
->layout
.tp_frame_nr
* sizeof(*ring
->frames
);
99 ring
->frames
= xmalloc_aligned(len
, CO_CACHE_LINE_SIZE
);
100 fmemset(ring
->frames
, 0, len
);
102 for (i
= 0; i
< ring
->layout
.tp_frame_nr
; ++i
) {
103 ring
->frames
[i
].iov_len
= ring
->layout
.tp_frame_size
;
104 ring
->frames
[i
].iov_base
= ring
->mm_space
+
105 (i
* ring
->layout
.tp_frame_size
);
109 void bind_rx_ring(int sock
, struct ring
*ring
, int ifindex
)
113 * The RX_RING registers itself to the networking stack with
114 * dev_add_pack(), so we have one single RX_RING for all devs
115 * otherwise you'll get the packet twice.
117 fmemset(&ring
->s_ll
, 0, sizeof(ring
->s_ll
));
119 ring
->s_ll
.sll_family
= AF_PACKET
;
120 ring
->s_ll
.sll_protocol
= htons(ETH_P_ALL
);
121 ring
->s_ll
.sll_ifindex
= ifindex
;
122 ring
->s_ll
.sll_hatype
= 0;
123 ring
->s_ll
.sll_halen
= 0;
124 ring
->s_ll
.sll_pkttype
= 0;
126 ret
= bind(sock
, (struct sockaddr
*) &ring
->s_ll
, sizeof(ring
->s_ll
));
128 destroy_rx_ring(sock
, ring
);
129 panic("Cannot bind RX_RING!\n");