2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2009, 2010 Daniel Borkmann.
4 * Copyright 2009, 2010 Emmanuel Roullit.
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 set_packet_loss_discard(int sock
)
26 ret
= setsockopt(sock
, SOL_PACKET
, PACKET_LOSS
, (void *) &discard
,
29 panic("setsockopt: cannot set packet loss");
32 void destroy_tx_ring(int sock
, struct ring
*ring
)
36 munmap(ring
->mm_space
, ring
->mm_len
);
39 fmemset(&ring
->layout
, 0, sizeof(ring
->layout
));
40 ret
= setsockopt(sock
, SOL_PACKET
, PACKET_TX_RING
, &ring
->layout
,
41 sizeof(ring
->layout
));
43 panic("Cannot destroy the TX_RING: %s!\n", strerror(errno
));
48 void setup_tx_ring_layout(int sock
, struct ring
*ring
, unsigned int size
,
51 fmemset(&ring
->layout
, 0, sizeof(ring
->layout
));
53 ring
->layout
.tp_block_size
= (jumbo_support
?
56 ring
->layout
.tp_frame_size
= (jumbo_support
?
57 TPACKET_ALIGNMENT
<< 12 :
58 TPACKET_ALIGNMENT
<< 7);
59 ring
->layout
.tp_block_nr
= size
/ ring
->layout
.tp_block_size
;
60 ring
->layout
.tp_frame_nr
= ring
->layout
.tp_block_size
/
61 ring
->layout
.tp_frame_size
*
62 ring
->layout
.tp_block_nr
;
64 ring_verify_layout(ring
);
67 void create_tx_ring(int sock
, struct ring
*ring
, int verbose
)
71 set_sockopt_tpacket_v2(sock
);
73 ret
= setsockopt(sock
, SOL_PACKET
, PACKET_TX_RING
, &ring
->layout
,
74 sizeof(ring
->layout
));
75 if (errno
== ENOMEM
&& ring
->layout
.tp_block_nr
> 1) {
76 ring
->layout
.tp_block_nr
>>= 1;
77 ring
->layout
.tp_frame_nr
= ring
->layout
.tp_block_size
/
78 ring
->layout
.tp_frame_size
*
79 ring
->layout
.tp_block_nr
;
84 panic("Cannot allocate TX_RING!\n");
86 ring
->mm_len
= ring
->layout
.tp_block_size
* ring
->layout
.tp_block_nr
;
89 printf("TX: %.2Lf MiB, %u Frames, each %u Byte allocated\n",
90 (long double) ring
->mm_len
/ (1 << 20),
91 ring
->layout
.tp_frame_nr
, ring
->layout
.tp_frame_size
);
95 void mmap_tx_ring(int sock
, struct ring
*ring
)
97 ring
->mm_space
= mmap(0, ring
->mm_len
, PROT_READ
| PROT_WRITE
,
98 MAP_SHARED
| MAP_LOCKED
| MAP_POPULATE
, sock
, 0);
99 if (ring
->mm_space
== MAP_FAILED
) {
100 destroy_tx_ring(sock
, ring
);
101 panic("Cannot mmap TX_RING!\n");
105 void alloc_tx_ring_frames(struct ring
*ring
)
108 size_t len
= ring
->layout
.tp_frame_nr
* sizeof(*ring
->frames
);
110 ring
->frames
= xmalloc_aligned(len
, CO_CACHE_LINE_SIZE
);
111 fmemset(ring
->frames
, 0, len
);
113 for (i
= 0; i
< ring
->layout
.tp_frame_nr
; ++i
) {
114 ring
->frames
[i
].iov_len
= ring
->layout
.tp_frame_size
;
115 ring
->frames
[i
].iov_base
= ring
->mm_space
+
116 (i
* ring
->layout
.tp_frame_size
);
120 void bind_tx_ring(int sock
, struct ring
*ring
, int ifindex
)
124 fmemset(&ring
->s_ll
, 0, sizeof(ring
->s_ll
));
126 ring
->s_ll
.sll_family
= AF_PACKET
;
127 ring
->s_ll
.sll_protocol
= htons(ETH_P_ALL
);
128 ring
->s_ll
.sll_ifindex
= ifindex
;
129 ring
->s_ll
.sll_hatype
= 0;
130 ring
->s_ll
.sll_halen
= 0;
131 ring
->s_ll
.sll_pkttype
= 0;
133 ret
= bind(sock
, (struct sockaddr
*) &ring
->s_ll
, sizeof(ring
->s_ll
));
135 destroy_tx_ring(sock
, ring
);
136 panic("Cannot bind TX_RING!\n");