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 # define PACKET_FANOUT 18
31 # define PACKET_FANOUT_POLICY_HASH 0
32 # define PACKET_FANOUT_POLICY_LB 1
33 # define PACKET_FANOUT_POLICY_DEFAULT PACKET_FANOUT_HASH
37 struct tpacket2_hdr tp_h __aligned_tpacket
;
38 struct sockaddr_ll s_ll
__align_tpacket(sizeof(struct tpacket2_hdr
));
45 struct tpacket_req layout
;
46 struct sockaddr_ll s_ll
;
49 static inline void next_rnd_slot(unsigned int *it
, struct ring
*ring
)
51 *it
= rand() % ring
->layout
.tp_frame_nr
;
54 #define RING_SIZE_FALLBACK (1 << 26)
56 static inline unsigned int ring_size(char *ifname
, unsigned int size
)
62 * Device bitrate in bytes times two as ring size.
63 * Fallback => ~ 64,00 MB
64 * 10 MBit => ~ 2,38 MB
65 * 54 MBit => ~ 12,88 MB
66 * 100 MBit => ~ 23,84 MB
67 * 300 MBit => ~ 71,52 MB
68 * 1.000 MBit => ~ 238,42 MB
69 * 10.000 MBit => ~ 2.384.18 MB
71 size
= device_bitrate(ifname
);
72 size
= (size
* 1000000) / 8;
75 size
= RING_SIZE_FALLBACK
;
77 return round_up_cacheline(size
);
80 static inline unsigned int ring_frame_size(struct ring
*ring
)
82 return ring
->layout
.tp_frame_size
;
85 static inline void tpacket_hdr_clone(struct tpacket2_hdr
*thdrd
,
86 struct tpacket2_hdr
*thdrs
)
88 thdrd
->tp_sec
= thdrs
->tp_sec
;
89 thdrd
->tp_nsec
= thdrs
->tp_nsec
;
90 thdrd
->tp_snaplen
= thdrs
->tp_snaplen
;
91 thdrd
->tp_len
= thdrs
->tp_len
;
95 # define POLLRDNORM 0x0040
98 # define POLLWRNORM 0x0100
101 # define POLLRDHUP 0x2000
104 #define POLL_NEXT_PKT 0
105 #define POLL_MOVE_OUT 1
107 static inline void prepare_polling(int sock
, struct pollfd
*pfd
)
109 memset(pfd
, 0, sizeof(*pfd
));
112 pfd
->events
= POLLIN
| POLLRDNORM
| POLLERR
;
115 static inline void set_sockopt_fanout(int sock
, unsigned int fanout_id
,
116 unsigned int fanout_type
)
118 unsigned int fanout_arg
= (fanout_id
| (fanout_type
<< 16));
119 int ret
= setsockopt(sock
, SOL_PACKET
, PACKET_FANOUT
, &fanout_arg
,
122 panic("No packet fanout support!\n");
125 static inline void set_sockopt_tpacket(int sock
)
127 int ret
, val
= TPACKET_V2
;
129 ret
= setsockopt(sock
, SOL_PACKET
, PACKET_VERSION
, &val
, sizeof(val
));
131 panic("Cannot set tpacketv2!\n");
134 #ifdef __WITH_HARDWARE_TIMESTAMPING
135 # include <linux/net_tstamp.h>
137 static inline void set_sockopt_hwtimestamp(int sock
, const char *dev
)
140 struct hwtstamp_config hwconfig
;
143 if (!strncmp("any", dev
, strlen("any")))
146 memset(&hwconfig
, 0, sizeof(hwconfig
));
147 hwconfig
.tx_type
= HWTSTAMP_TX_ON
;
148 hwconfig
.rx_filter
= HWTSTAMP_FILTER_ALL
;
150 memset(&ifr
, 0, sizeof(ifr
));
151 strlcpy(ifr
.ifr_name
, dev
, sizeof(ifr
.ifr_name
));
152 ifr
.ifr_data
= &hwconfig
;
154 ret
= ioctl(sock
, SIOCSHWTSTAMP
, &ifr
);
156 if (errno
== EOPNOTSUPP
)
158 panic("Cannot set timestamping: %s\n", strerror(errno
));
161 timesource
= SOF_TIMESTAMPING_RAW_HARDWARE
;
163 ret
= setsockopt(sock
, SOL_PACKET
, PACKET_TIMESTAMP
, ×ource
,
166 panic("Cannot set timestamping: %s!\n", strerror(errno
));
169 static inline void set_sockopt_hwtimestamp(int sock
, const char *dev
)