2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2009, 2010 Daniel Borkmann.
4 * Copyright 2009, 2010 Emmanuel Roullit.
5 * Copyright 2014, 2015 Tobias Klauser.
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 static 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
)
37 munmap(ring
->mm_space
, ring
->mm_len
);
40 fmemset(&ring
->layout
, 0, sizeof(ring
->layout
));
41 ret
= setsockopt(sock
, SOL_PACKET
, PACKET_TX_RING
, &ring
->layout
,
42 sizeof(ring
->layout
));
44 panic("Cannot destroy the TX_RING: %s!\n", strerror(errno
));
49 static void setup_tx_ring_layout(int sock
, struct ring
*ring
, size_t size
,
52 setup_ring_layout_generic(sock
, ring
, size
, jumbo_support
);
54 set_sockopt_tpacket_v2(sock
);
56 ring_verify_layout(ring
);
59 static void create_tx_ring(int sock
, struct ring
*ring
, bool verbose
)
63 ret
= setsockopt(sock
, SOL_PACKET
, PACKET_TX_RING
, &ring
->layout
,
64 sizeof(ring
->layout
));
66 if (errno
== ENOMEM
&& ring
->layout
.tp_block_nr
> 1) {
67 shrink_ring_layout_generic(ring
);
72 panic("Cannot allocate TX_RING!\n");
74 ring
->mm_len
= (size_t) ring
->layout
.tp_block_size
* ring
->layout
.tp_block_nr
;
77 printf("TX,V2: %.2Lf MiB, %u Frames, each %u Byte allocated\n",
78 (long double) ring
->mm_len
/ (1 << 20),
79 ring
->layout
.tp_frame_nr
, ring
->layout
.tp_frame_size
);
83 static void alloc_tx_ring_frames(int sock __maybe_unused
, struct ring
*ring
)
85 alloc_ring_frames_generic(ring
, ring
->layout
.tp_frame_nr
,
86 ring
->layout
.tp_frame_size
);
89 void ring_tx_setup(struct ring
*ring
, int sock
, size_t size
, int ifindex
,
90 bool jumbo_support
, bool verbose
)
92 fmemset(ring
, 0, sizeof(*ring
));
93 set_packet_loss_discard(sock
);
94 setup_tx_ring_layout(sock
, ring
, size
, jumbo_support
);
95 create_tx_ring(sock
, ring
, verbose
);
96 mmap_ring_generic(sock
, ring
);
97 alloc_tx_ring_frames(sock
, ring
);
98 bind_ring_generic(sock
, ring
, ifindex
, true);