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 ret
= setsockopt(sock
, SOL_PACKET
, PACKET_TX_RING
, &ring
->layout
,
71 sizeof(ring
->layout
));
72 if (errno
== ENOMEM
&& ring
->layout
.tp_block_nr
> 1) {
73 ring
->layout
.tp_block_nr
>>= 1;
74 ring
->layout
.tp_frame_nr
= ring
->layout
.tp_block_size
/
75 ring
->layout
.tp_frame_size
*
76 ring
->layout
.tp_block_nr
;
81 panic("Cannot allocate TX_RING!\n");
83 ring
->mm_len
= ring
->layout
.tp_block_size
* ring
->layout
.tp_block_nr
;
85 printf("TX: %.2f MiB, %u Frames, each %u Byte allocated\n",
86 1.f
* ring
->mm_len
/ (1 << 20),
87 ring
->layout
.tp_frame_nr
, ring
->layout
.tp_frame_size
);
90 void mmap_tx_ring(int sock
, struct ring
*ring
)
92 ring
->mm_space
= mmap(0, ring
->mm_len
, PROT_READ
| PROT_WRITE
,
93 MAP_SHARED
| MAP_LOCKED
, sock
, 0);
94 if (ring
->mm_space
== MAP_FAILED
) {
95 destroy_tx_ring(sock
, ring
);
96 panic("Cannot mmap TX_RING!\n");
100 void alloc_tx_ring_frames(struct ring
*ring
)
103 size_t len
= ring
->layout
.tp_frame_nr
* sizeof(*ring
->frames
);
105 ring
->frames
= xmalloc_aligned(len
, CO_CACHE_LINE_SIZE
);
106 fmemset(ring
->frames
, 0, len
);
108 for (i
= 0; i
< ring
->layout
.tp_frame_nr
; ++i
) {
109 ring
->frames
[i
].iov_len
= ring
->layout
.tp_frame_size
;
110 ring
->frames
[i
].iov_base
= ring
->mm_space
+
111 (i
* ring
->layout
.tp_frame_size
);
115 void bind_tx_ring(int sock
, struct ring
*ring
, int ifindex
)
119 fmemset(&ring
->s_ll
, 0, sizeof(ring
->s_ll
));
121 ring
->s_ll
.sll_family
= AF_PACKET
;
122 ring
->s_ll
.sll_protocol
= htons(ETH_P_ALL
);
123 ring
->s_ll
.sll_ifindex
= ifindex
;
124 ring
->s_ll
.sll_hatype
= 0;
125 ring
->s_ll
.sll_halen
= 0;
126 ring
->s_ll
.sll_pkttype
= 0;
128 ret
= bind(sock
, (struct sockaddr
*) &ring
->s_ll
, sizeof(ring
->s_ll
));
130 destroy_tx_ring(sock
, ring
);
131 panic("Cannot bind TX_RING!\n");