2 * netsniff-ng - the packet sniffing beast
3 * By Daniel Borkmann <daniel@netsniff-ng.org>
4 * Copyright 2009, 2010 Daniel Borkmann.
5 * Copyright 2009, 2010 Emmanuel Roullit.
6 * Subject to the GPL, version 2.
14 #include <sys/types.h>
15 #include <sys/socket.h>
16 #include <arpa/inet.h>
17 #include <linux/if_ether.h>
24 void set_packet_loss_discard(int sock
)
27 ret
= setsockopt(sock
, SOL_PACKET
, PACKET_LOSS
, (void *) &discard
,
30 panic("setsockopt: cannot set packet loss");
33 void destroy_tx_ring(int sock
, struct ring
*ring
)
35 fmemset(&ring
->layout
, 0, sizeof(ring
->layout
));
36 setsockopt(sock
, SOL_PACKET
, PACKET_TX_RING
, &ring
->layout
,
37 sizeof(ring
->layout
));
39 munmap(ring
->mm_space
, ring
->mm_len
);
45 void setup_tx_ring_layout(int sock
, struct ring
*ring
, unsigned int size
,
48 fmemset(&ring
->layout
, 0, sizeof(ring
->layout
));
50 ring
->layout
.tp_block_size
= (jumbo_support
?
53 ring
->layout
.tp_frame_size
= (jumbo_support
?
54 TPACKET_ALIGNMENT
<< 12 :
55 TPACKET_ALIGNMENT
<< 7);
56 ring
->layout
.tp_block_nr
= size
/ ring
->layout
.tp_block_size
;
57 ring
->layout
.tp_frame_nr
= ring
->layout
.tp_block_size
/
58 ring
->layout
.tp_frame_size
*
59 ring
->layout
.tp_block_nr
;
61 bug_on(ring
->layout
.tp_block_size
< ring
->layout
.tp_frame_size
);
62 bug_on((ring
->layout
.tp_block_size
% ring
->layout
.tp_frame_size
) != 0);
63 bug_on((ring
->layout
.tp_block_size
% getpagesize()) != 0);
66 void create_tx_ring(int sock
, struct ring
*ring
)
70 set_sockopt_tpacket(sock
);
72 ret
= setsockopt(sock
, SOL_PACKET
, PACKET_TX_RING
, &ring
->layout
,
73 sizeof(ring
->layout
));
74 if (errno
== ENOMEM
&& ring
->layout
.tp_block_nr
> 1) {
75 ring
->layout
.tp_block_nr
>>= 1;
76 ring
->layout
.tp_frame_nr
= ring
->layout
.tp_block_size
/
77 ring
->layout
.tp_frame_size
*
78 ring
->layout
.tp_block_nr
;
83 panic("Cannot allocate TX_RING!\n");
85 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
);
92 void mmap_tx_ring(int sock
, struct ring
*ring
)
94 ring
->mm_space
= mmap(0, ring
->mm_len
, PROT_READ
| PROT_WRITE
,
95 MAP_SHARED
| MAP_LOCKED
| MAP_POPULATE
, sock
, 0);
96 if (ring
->mm_space
== MAP_FAILED
) {
97 destroy_tx_ring(sock
, ring
);
98 panic("Cannot mmap TX_RING!\n");
102 void alloc_tx_ring_frames(struct ring
*ring
)
105 size_t len
= ring
->layout
.tp_frame_nr
* sizeof(*ring
->frames
);
107 ring
->frames
= xmalloc_aligned(len
, CO_CACHE_LINE_SIZE
);
108 fmemset(ring
->frames
, 0, len
);
110 for (i
= 0; i
< ring
->layout
.tp_frame_nr
; ++i
) {
111 ring
->frames
[i
].iov_len
= ring
->layout
.tp_frame_size
;
112 ring
->frames
[i
].iov_base
= ring
->mm_space
+
113 (i
* ring
->layout
.tp_frame_size
);
117 void bind_tx_ring(int sock
, struct ring
*ring
, int ifindex
)
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_tx_ring(sock
, ring
);
133 panic("Cannot bind TX_RING!\n");