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 bug_on(ring
->layout
.tp_block_size
< ring
->layout
.tp_frame_size
);
65 bug_on((ring
->layout
.tp_block_size
% ring
->layout
.tp_frame_size
) != 0);
66 bug_on((ring
->layout
.tp_block_size
% getpagesize()) != 0);
69 void create_tx_ring(int sock
, struct ring
*ring
, int verbose
)
73 set_sockopt_tpacket(sock
);
75 ret
= setsockopt(sock
, SOL_PACKET
, PACKET_TX_RING
, &ring
->layout
,
76 sizeof(ring
->layout
));
77 if (errno
== ENOMEM
&& ring
->layout
.tp_block_nr
> 1) {
78 ring
->layout
.tp_block_nr
>>= 1;
79 ring
->layout
.tp_frame_nr
= ring
->layout
.tp_block_size
/
80 ring
->layout
.tp_frame_size
*
81 ring
->layout
.tp_block_nr
;
86 panic("Cannot allocate TX_RING!\n");
88 ring
->mm_len
= ring
->layout
.tp_block_size
* ring
->layout
.tp_block_nr
;
91 printf("TX: %.2Lf MiB, %u Frames, each %u Byte allocated\n",
92 (long double) ring
->mm_len
/ (1 << 20),
93 ring
->layout
.tp_frame_nr
, ring
->layout
.tp_frame_size
);
97 void mmap_tx_ring(int sock
, struct ring
*ring
)
99 ring
->mm_space
= mmap(0, ring
->mm_len
, PROT_READ
| PROT_WRITE
,
100 MAP_SHARED
| MAP_LOCKED
| MAP_POPULATE
, sock
, 0);
101 if (ring
->mm_space
== MAP_FAILED
) {
102 destroy_tx_ring(sock
, ring
);
103 panic("Cannot mmap TX_RING!\n");
107 void alloc_tx_ring_frames(struct ring
*ring
)
110 size_t len
= ring
->layout
.tp_frame_nr
* sizeof(*ring
->frames
);
112 ring
->frames
= xmalloc_aligned(len
, CO_CACHE_LINE_SIZE
);
113 fmemset(ring
->frames
, 0, len
);
115 for (i
= 0; i
< ring
->layout
.tp_frame_nr
; ++i
) {
116 ring
->frames
[i
].iov_len
= ring
->layout
.tp_frame_size
;
117 ring
->frames
[i
].iov_base
= ring
->mm_space
+
118 (i
* ring
->layout
.tp_frame_size
);
122 void bind_tx_ring(int sock
, struct ring
*ring
, int ifindex
)
126 fmemset(&ring
->s_ll
, 0, sizeof(ring
->s_ll
));
128 ring
->s_ll
.sll_family
= AF_PACKET
;
129 ring
->s_ll
.sll_protocol
= htons(ETH_P_ALL
);
130 ring
->s_ll
.sll_ifindex
= ifindex
;
131 ring
->s_ll
.sll_hatype
= 0;
132 ring
->s_ll
.sll_halen
= 0;
133 ring
->s_ll
.sll_pkttype
= 0;
135 ret
= bind(sock
, (struct sockaddr
*) &ring
->s_ll
, sizeof(ring
->s_ll
));
137 destroy_tx_ring(sock
, ring
);
138 panic("Cannot bind TX_RING!\n");