2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2009, 2010 Daniel Borkmann.
4 * Copyright 2014, 2015 Tobias Klauser.
5 * Subject to the GPL, version 2.
12 * "I love the smell of 10GbE in the morning. Smells like ... victory."
13 * - W. Richard Stevens, "Secret Teachings of the UNIX Environment"
18 #include <linux/if_packet.h>
19 #include <linux/socket.h>
20 #include <linux/sockios.h>
21 #include <sys/ioctl.h>
31 # define POLLRDNORM 0x0040
35 struct tpacket_hdr
*h1
;
36 struct tpacket2_hdr
*h2
;
38 struct tpacket3_hdr
*h3
;
44 #define tpacket_uhdr(hdr, member, v3) \
45 ((v3) ? ((hdr).h3)->member : ((hdr).h2)->member)
47 #define tpacket_uhdr(hdr, member, v3) \
49 #endif /* HAVE_TPACKET3 */
51 static inline uint16_t tpacket_uhdr_vlan_tci(union tpacket_uhdr
*hdr
, bool v3
)
55 return hdr
->h3
->hv1
.tp_vlan_tci
;
60 static inline uint16_t tpacket_uhdr_vlan_proto(union tpacket_uhdr
*hdr
, bool v3
)
62 #if defined(HAVE_TPACKET3) && defined(TP_STATUS_VLAN_TPID_VALID)
64 return hdr
->h3
->hv1
.tp_vlan_tpid
;
69 static inline bool tpacket_has_vlan_info(union tpacket_uhdr
*hdr
)
71 uint32_t valid
= TP_STATUS_VLAN_VALID
;
73 #ifdef TP_STATUS_VLAN_TPID_VALID
74 valid
|= TP_STATUS_VLAN_TPID_VALID
;
77 return tpacket_uhdr(*hdr
, tp_status
, true) & valid
;
81 struct tpacket2_hdr tp_h __aligned_tpacket
;
82 struct sockaddr_ll s_ll
__align_tpacket(sizeof(struct tpacket2_hdr
));
88 uint32_t offset_to_priv
;
89 struct tpacket_hdr_v1 h1
;
97 struct sockaddr_ll s_ll
;
99 struct tpacket_req layout
;
101 struct tpacket_req3 layout3
;
107 static inline void next_rnd_slot(unsigned int *it
, struct ring
*ring
)
109 *it
= rand() % ring
->layout
.tp_frame_nr
;
112 static inline size_t ring_size(const char *ifname
, size_t size
)
118 * Device bitrate in bytes times two as ring size.
119 * Fallback => ~ 64,00 MB
120 * 10 MBit => ~ 2,38 MB
121 * 54 MBit => ~ 12,88 MB
122 * 100 MBit => ~ 23,84 MB
123 * 300 MBit => ~ 71,52 MB
124 * 1.000 MBit => ~ 238,42 MB
125 * 10.000 MBit => ~ 2.384.18 MB
127 size
= device_bitrate(ifname
);
128 size
= (size
* 1000000) / 8;
133 return round_up_cacheline(size
);
136 static inline unsigned int ring_frame_size(struct ring
*ring
)
138 return ring
->layout
.tp_frame_size
;
141 static inline void ring_verify_layout(struct ring
*ring
)
143 bug_on(ring
->layout
.tp_block_size
< ring
->layout
.tp_frame_size
);
144 bug_on((ring
->layout
.tp_block_size
% ring
->layout
.tp_frame_size
) != 0);
145 bug_on((ring
->layout
.tp_block_size
% RUNTIME_PAGE_SIZE
) != 0);
148 static inline void shrink_ring_layout_generic(struct ring
*ring
)
150 ring
->layout
.tp_block_nr
>>= 1;
151 ring
->layout
.tp_frame_nr
= ring
->layout
.tp_block_size
/
152 ring
->layout
.tp_frame_size
*
153 ring
->layout
.tp_block_nr
;
156 static inline void tpacket_hdr_clone(struct tpacket2_hdr
*thdrd
,
157 struct tpacket2_hdr
*thdrs
)
159 thdrd
->tp_sec
= thdrs
->tp_sec
;
160 thdrd
->tp_nsec
= thdrs
->tp_nsec
;
161 thdrd
->tp_snaplen
= thdrs
->tp_snaplen
;
162 thdrd
->tp_len
= thdrs
->tp_len
;
165 static inline void prepare_polling(int sock
, struct pollfd
*pfd
)
167 memset(pfd
, 0, sizeof(*pfd
));
170 pfd
->events
= POLLIN
| POLLRDNORM
| POLLERR
;
173 static inline void __set_sockopt_tpacket(int sock
, int val
)
175 int ret
= setsockopt(sock
, SOL_PACKET
, PACKET_VERSION
, &val
, sizeof(val
));
177 panic("Cannot set tpacketv2!\n");
180 static inline void set_sockopt_tpacket_v2(int sock
)
182 __set_sockopt_tpacket(sock
, TPACKET_V2
);
186 static inline void set_sockopt_tpacket_v3(int sock
)
188 __set_sockopt_tpacket(sock
, TPACKET_V3
);
191 static inline void set_sockopt_tpacket_v3(int sock __maybe_unused
)
196 static inline int get_sockopt_tpacket(int sock
)
199 socklen_t len
= sizeof(val
);
201 ret
= getsockopt(sock
, SOL_PACKET
, PACKET_VERSION
, &val
, &len
);
203 panic("Cannot get tpacket version!\n");
208 extern void setup_ring_layout_generic(struct ring
*ring
, size_t size
,
210 extern void mmap_ring_generic(int sock
, struct ring
*ring
);
211 extern void alloc_ring_frames_generic(struct ring
*ring
, size_t num
, size_t size
);
212 extern void bind_ring_generic(int sock
, struct ring
*ring
, int ifindex
, bool tx_only
);