2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2009, 2010 Daniel Borkmann.
4 * Subject to the GPL, version 2.
11 * "I love the smell of 10GbE in the morning. Smells like ... victory."
12 * - W. Richard Stevens, "Secret Teachings of the UNIX Environment"
17 #include <linux/if_packet.h>
18 #include <linux/socket.h>
19 #include <linux/sockios.h>
20 #include <sys/ioctl.h>
30 struct tpacket2_hdr tp_h __aligned_tpacket
;
31 struct sockaddr_ll s_ll
__align_tpacket(sizeof(struct tpacket2_hdr
));
38 struct tpacket_req layout
;
39 struct sockaddr_ll s_ll
;
42 static inline void next_rnd_slot(unsigned int *it
, struct ring
*ring
)
44 *it
= rand() % ring
->layout
.tp_frame_nr
;
47 #define RING_SIZE_FALLBACK (1 << 26)
49 static inline unsigned int ring_size(char *ifname
, unsigned int size
)
55 * Device bitrate in bytes times two as ring size.
56 * Fallback => ~ 64,00 MB
57 * 10 MBit => ~ 2,38 MB
58 * 54 MBit => ~ 12,88 MB
59 * 100 MBit => ~ 23,84 MB
60 * 300 MBit => ~ 71,52 MB
61 * 1.000 MBit => ~ 238,42 MB
62 * 10.000 MBit => ~ 2.384.18 MB
64 size
= device_bitrate(ifname
);
65 size
= (size
* 1000000) / 8;
68 size
= RING_SIZE_FALLBACK
;
70 return round_up_cacheline(size
);
73 static inline unsigned int ring_frame_size(struct ring
*ring
)
75 return ring
->layout
.tp_frame_size
;
78 static inline void tpacket_hdr_clone(struct tpacket2_hdr
*thdrd
,
79 struct tpacket2_hdr
*thdrs
)
81 thdrd
->tp_sec
= thdrs
->tp_sec
;
82 thdrd
->tp_nsec
= thdrs
->tp_nsec
;
83 thdrd
->tp_snaplen
= thdrs
->tp_snaplen
;
84 thdrd
->tp_len
= thdrs
->tp_len
;
88 # define POLLRDNORM 0x0040
91 # define POLLWRNORM 0x0100
94 # define POLLRDHUP 0x2000
97 static inline void prepare_polling(int sock
, struct pollfd
*pfd
)
99 memset(pfd
, 0, sizeof(*pfd
));
102 pfd
->events
= POLLIN
| POLLRDNORM
| POLLERR
;
105 static inline void __set_sockopt_tpacket(int sock
, int val
)
107 int ret
= setsockopt(sock
, SOL_PACKET
, PACKET_VERSION
, &val
, sizeof(val
));
109 panic("Cannot set tpacketv2!\n");
112 static inline void set_sockopt_tpacket_v2(int sock
)
114 __set_sockopt_tpacket(sock
, TPACKET_V2
);
117 static inline void set_sockopt_tpacket_v3(int sock
)
119 __set_sockopt_tpacket(sock
, TPACKET_V3
);