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
)
34 fmemset(&ring
->layout
, 0, sizeof(ring
->layout
));
35 setsockopt(sock
, SOL_PACKET
, PACKET_TX_RING
, &ring
->layout
,
36 sizeof(ring
->layout
));
38 munmap(ring
->mm_space
, ring
->mm_len
);
44 void setup_tx_ring_layout(int sock
, struct ring
*ring
, unsigned int size
,
47 fmemset(&ring
->layout
, 0, sizeof(ring
->layout
));
49 ring
->layout
.tp_block_size
= (jumbo_support
?
52 ring
->layout
.tp_frame_size
= (jumbo_support
?
53 TPACKET_ALIGNMENT
<< 12 :
54 TPACKET_ALIGNMENT
<< 7);
55 ring
->layout
.tp_block_nr
= size
/ ring
->layout
.tp_block_size
;
56 ring
->layout
.tp_frame_nr
= ring
->layout
.tp_block_size
/
57 ring
->layout
.tp_frame_size
*
58 ring
->layout
.tp_block_nr
;
60 bug_on(ring
->layout
.tp_block_size
< ring
->layout
.tp_frame_size
);
61 bug_on((ring
->layout
.tp_block_size
% ring
->layout
.tp_frame_size
) != 0);
62 bug_on((ring
->layout
.tp_block_size
% getpagesize()) != 0);
65 void create_tx_ring(int sock
, struct ring
*ring
, int verbose
)
69 set_sockopt_tpacket(sock
);
71 ret
= setsockopt(sock
, SOL_PACKET
, PACKET_TX_RING
, &ring
->layout
,
72 sizeof(ring
->layout
));
73 if (errno
== ENOMEM
&& ring
->layout
.tp_block_nr
> 1) {
74 ring
->layout
.tp_block_nr
>>= 1;
75 ring
->layout
.tp_frame_nr
= ring
->layout
.tp_block_size
/
76 ring
->layout
.tp_frame_size
*
77 ring
->layout
.tp_block_nr
;
82 panic("Cannot allocate TX_RING!\n");
84 ring
->mm_len
= ring
->layout
.tp_block_size
* ring
->layout
.tp_block_nr
;
87 printf("TX: %.2Lf MiB, %u Frames, each %u Byte allocated\n",
88 (long double) ring
->mm_len
/ (1 << 20),
89 ring
->layout
.tp_frame_nr
, ring
->layout
.tp_frame_size
);
93 void mmap_tx_ring(int sock
, struct ring
*ring
)
95 ring
->mm_space
= mmap(0, ring
->mm_len
, PROT_READ
| PROT_WRITE
,
96 MAP_SHARED
| MAP_LOCKED
| MAP_POPULATE
, sock
, 0);
97 if (ring
->mm_space
== MAP_FAILED
) {
98 destroy_tx_ring(sock
, ring
);
99 panic("Cannot mmap TX_RING!\n");
103 void alloc_tx_ring_frames(struct ring
*ring
)
106 size_t len
= ring
->layout
.tp_frame_nr
* sizeof(*ring
->frames
);
108 ring
->frames
= xmalloc_aligned(len
, CO_CACHE_LINE_SIZE
);
109 fmemset(ring
->frames
, 0, len
);
111 for (i
= 0; i
< ring
->layout
.tp_frame_nr
; ++i
) {
112 ring
->frames
[i
].iov_len
= ring
->layout
.tp_frame_size
;
113 ring
->frames
[i
].iov_base
= ring
->mm_space
+
114 (i
* ring
->layout
.tp_frame_size
);
118 void bind_tx_ring(int sock
, struct ring
*ring
, int ifindex
)
122 fmemset(&ring
->s_ll
, 0, sizeof(ring
->s_ll
));
124 ring
->s_ll
.sll_family
= AF_PACKET
;
125 ring
->s_ll
.sll_protocol
= htons(ETH_P_ALL
);
126 ring
->s_ll
.sll_ifindex
= ifindex
;
127 ring
->s_ll
.sll_hatype
= 0;
128 ring
->s_ll
.sll_halen
= 0;
129 ring
->s_ll
.sll_pkttype
= 0;
131 ret
= bind(sock
, (struct sockaddr
*) &ring
->s_ll
, sizeof(ring
->s_ll
));
133 destroy_tx_ring(sock
, ring
);
134 panic("Cannot bind TX_RING!\n");