2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2009, 2010 Daniel Borkmann.
4 * Subject to the GPL, version 2.
12 #include <sys/types.h>
13 #include <sys/socket.h>
14 #include <arpa/inet.h>
15 #include <linux/if_ether.h>
22 void destroy_rx_ring(int sock
, struct ring
*ring
)
26 fmemset(&ring
->layout
, 0, sizeof(ring
->layout
));
28 ret
= setsockopt(sock
, SOL_PACKET
, PACKET_RX_RING
, &ring
->layout
,
29 sizeof(ring
->layout
));
31 panic("Cannot destroy the RX_RING!\n");
33 munmap(ring
->mm_space
, ring
->mm_len
);
39 void setup_rx_ring_layout(int sock
, struct ring
*ring
, unsigned int size
,
42 fmemset(&ring
->layout
, 0, sizeof(ring
->layout
));
44 ring
->layout
.tp_block_size
= (jumbo_support
?
47 ring
->layout
.tp_frame_size
= (jumbo_support
?
48 TPACKET_ALIGNMENT
<< 12 :
49 TPACKET_ALIGNMENT
<< 7);
50 ring
->layout
.tp_block_nr
= size
/ ring
->layout
.tp_block_size
;
51 ring
->layout
.tp_frame_nr
= ring
->layout
.tp_block_size
/
52 ring
->layout
.tp_frame_size
*
53 ring
->layout
.tp_block_nr
;
55 bug_on(ring
->layout
.tp_block_size
< ring
->layout
.tp_frame_size
);
56 bug_on((ring
->layout
.tp_block_size
% ring
->layout
.tp_frame_size
) != 0);
57 bug_on((ring
->layout
.tp_block_size
% getpagesize()) != 0);
60 void create_rx_ring(int sock
, struct ring
*ring
, int verbose
)
64 set_sockopt_tpacket(sock
);
66 ret
= setsockopt(sock
, SOL_PACKET
, PACKET_RX_RING
, &ring
->layout
,
67 sizeof(ring
->layout
));
68 if (errno
== ENOMEM
&& ring
->layout
.tp_block_nr
> 1) {
69 ring
->layout
.tp_block_nr
>>= 1;
70 ring
->layout
.tp_frame_nr
= ring
->layout
.tp_block_size
/
71 ring
->layout
.tp_frame_size
*
72 ring
->layout
.tp_block_nr
;
77 panic("Cannot allocate RX_RING!\n");
79 ring
->mm_len
= ring
->layout
.tp_block_size
* ring
->layout
.tp_block_nr
;
82 printf("RX: %.2Lf MiB, %u Frames, each %u Byte allocated\n",
83 (long double) ring
->mm_len
/ (1 << 20),
84 ring
->layout
.tp_frame_nr
, ring
->layout
.tp_frame_size
);
88 void mmap_rx_ring(int sock
, struct ring
*ring
)
90 ring
->mm_space
= mmap(0, ring
->mm_len
, PROT_READ
| PROT_WRITE
,
91 MAP_SHARED
| MAP_LOCKED
| MAP_POPULATE
, sock
, 0);
92 if (ring
->mm_space
== MAP_FAILED
) {
93 destroy_rx_ring(sock
, ring
);
94 panic("Cannot mmap RX_RING!\n");
98 void alloc_rx_ring_frames(struct ring
*ring
)
101 size_t len
= ring
->layout
.tp_frame_nr
* sizeof(*ring
->frames
);
103 ring
->frames
= xmalloc_aligned(len
, CO_CACHE_LINE_SIZE
);
104 fmemset(ring
->frames
, 0, len
);
106 for (i
= 0; i
< ring
->layout
.tp_frame_nr
; ++i
) {
107 ring
->frames
[i
].iov_len
= ring
->layout
.tp_frame_size
;
108 ring
->frames
[i
].iov_base
= ring
->mm_space
+
109 (i
* ring
->layout
.tp_frame_size
);
113 void bind_rx_ring(int sock
, struct ring
*ring
, int ifindex
)
117 * The RX_RING registers itself to the networking stack with
118 * dev_add_pack(), so we have one single RX_RING for all devs
119 * otherwise you'll get the packet twice.
121 fmemset(&ring
->s_ll
, 0, sizeof(ring
->s_ll
));
123 ring
->s_ll
.sll_family
= AF_PACKET
;
124 ring
->s_ll
.sll_protocol
= htons(ETH_P_ALL
);
125 ring
->s_ll
.sll_ifindex
= ifindex
;
126 ring
->s_ll
.sll_hatype
= 0;
127 ring
->s_ll
.sll_halen
= 0;
128 ring
->s_ll
.sll_pkttype
= 0;
130 ret
= bind(sock
, (struct sockaddr
*) &ring
->s_ll
, sizeof(ring
->s_ll
));
132 destroy_rx_ring(sock
, ring
);
133 panic("Cannot bind RX_RING!\n");