staging: wilc1000: rename pstrStatistics in wilc_get_statistics
[deliverable/linux.git] / drivers / staging / wilc1000 / linux_mon.c
1 /*!
2 * @file linux_mon.c
3 * @brief File Operations OS wrapper functionality
4 * @author mdaftedar
5 * @sa wilc_wfi_netdevice.h
6 * @date 01 MAR 2012
7 * @version 1.0
8 */
9 #include "wilc_wfi_cfgoperations.h"
10 #include "linux_wlan_common.h"
11 #include "wilc_wlan_if.h"
12 #include "wilc_wlan.h"
13
14
15 struct wilc_wfi_radiotap_hdr {
16 struct ieee80211_radiotap_header hdr;
17 u8 rate;
18 } __attribute__((packed));
19
20 struct wilc_wfi_radiotap_cb_hdr {
21 struct ieee80211_radiotap_header hdr;
22 u8 rate;
23 u8 dump;
24 u16 tx_flags;
25 } __attribute__((packed));
26
27 static struct net_device *wilc_wfi_mon; /* global monitor netdev */
28
29 static u8 srcAdd[6];
30 static u8 bssid[6];
31 static u8 broadcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
32 /**
33 * @brief WILC_WFI_monitor_rx
34 * @details
35 * @param[in]
36 * @return int : Return 0 on Success
37 * @author mdaftedar
38 * @date 12 JUL 2012
39 * @version 1.0
40 */
41
42 #define IEEE80211_RADIOTAP_F_TX_RTS 0x0004 /* used rts/cts handshake */
43 #define IEEE80211_RADIOTAP_F_TX_FAIL 0x0001 /* failed due to excessive*/
44 #define IS_MANAGMEMENT 0x100
45 #define IS_MANAGMEMENT_CALLBACK 0x080
46 #define IS_MGMT_STATUS_SUCCES 0x040
47 #define GET_PKT_OFFSET(a) (((a) >> 22) & 0x1ff)
48
49 void WILC_WFI_monitor_rx(u8 *buff, u32 size)
50 {
51 u32 header, pkt_offset;
52 struct sk_buff *skb = NULL;
53 struct wilc_wfi_radiotap_hdr *hdr;
54 struct wilc_wfi_radiotap_cb_hdr *cb_hdr;
55
56 PRINT_INFO(HOSTAPD_DBG, "In monitor interface receive function\n");
57
58 if (wilc_wfi_mon == NULL)
59 return;
60
61 if (!netif_running(wilc_wfi_mon)) {
62 PRINT_INFO(HOSTAPD_DBG, "Monitor interface already RUNNING\n");
63 return;
64 }
65
66 /* Get WILC header */
67 memcpy(&header, (buff - HOST_HDR_OFFSET), HOST_HDR_OFFSET);
68
69 /* The packet offset field conain info about what type of managment frame */
70 /* we are dealing with and ack status */
71 pkt_offset = GET_PKT_OFFSET(header);
72
73 if (pkt_offset & IS_MANAGMEMENT_CALLBACK) {
74
75 /* hostapd callback mgmt frame */
76
77 skb = dev_alloc_skb(size + sizeof(struct wilc_wfi_radiotap_cb_hdr));
78 if (skb == NULL) {
79 PRINT_INFO(HOSTAPD_DBG, "Monitor if : No memory to allocate skb");
80 return;
81 }
82
83 memcpy(skb_put(skb, size), buff, size);
84
85 cb_hdr = (struct wilc_wfi_radiotap_cb_hdr *) skb_push(skb, sizeof(*cb_hdr));
86 memset(cb_hdr, 0, sizeof(struct wilc_wfi_radiotap_cb_hdr));
87
88 cb_hdr->hdr.it_version = 0; /* PKTHDR_RADIOTAP_VERSION; */
89
90 cb_hdr->hdr.it_len = cpu_to_le16(sizeof(struct wilc_wfi_radiotap_cb_hdr));
91
92 cb_hdr->hdr.it_present = cpu_to_le32(
93 (1 << IEEE80211_RADIOTAP_RATE) |
94 (1 << IEEE80211_RADIOTAP_TX_FLAGS));
95
96 cb_hdr->rate = 5; /* txrate->bitrate / 5; */
97
98 if (pkt_offset & IS_MGMT_STATUS_SUCCES) {
99 /* success */
100 cb_hdr->tx_flags = IEEE80211_RADIOTAP_F_TX_RTS;
101 } else {
102 cb_hdr->tx_flags = IEEE80211_RADIOTAP_F_TX_FAIL;
103 }
104
105 } else {
106
107 skb = dev_alloc_skb(size + sizeof(struct wilc_wfi_radiotap_hdr));
108
109 if (skb == NULL) {
110 PRINT_INFO(HOSTAPD_DBG, "Monitor if : No memory to allocate skb");
111 return;
112 }
113
114 memcpy(skb_put(skb, size), buff, size);
115 hdr = (struct wilc_wfi_radiotap_hdr *) skb_push(skb, sizeof(*hdr));
116 memset(hdr, 0, sizeof(struct wilc_wfi_radiotap_hdr));
117 hdr->hdr.it_version = 0; /* PKTHDR_RADIOTAP_VERSION; */
118 hdr->hdr.it_len = cpu_to_le16(sizeof(struct wilc_wfi_radiotap_hdr));
119 PRINT_INFO(HOSTAPD_DBG, "Radiotap len %d\n", hdr->hdr.it_len);
120 hdr->hdr.it_present = cpu_to_le32
121 (1 << IEEE80211_RADIOTAP_RATE); /* | */
122 PRINT_INFO(HOSTAPD_DBG, "Presentflags %d\n", hdr->hdr.it_present);
123 hdr->rate = 5; /* txrate->bitrate / 5; */
124
125 }
126
127
128
129 skb->dev = wilc_wfi_mon;
130 skb_set_mac_header(skb, 0);
131 skb->ip_summed = CHECKSUM_UNNECESSARY;
132 skb->pkt_type = PACKET_OTHERHOST;
133 skb->protocol = htons(ETH_P_802_2);
134 memset(skb->cb, 0, sizeof(skb->cb));
135
136 netif_rx(skb);
137
138
139 }
140
141 struct tx_complete_mon_data {
142 int size;
143 void *buff;
144 };
145
146 static void mgmt_tx_complete(void *priv, int status)
147 {
148
149 struct tx_complete_mon_data *pv_data = (struct tx_complete_mon_data *)priv;
150 u8 *buf = pv_data->buff;
151
152
153
154 if (status == 1) {
155 if (INFO || buf[0] == 0x10 || buf[0] == 0xb0)
156 PRINT_INFO(HOSTAPD_DBG, "Packet sent successfully - Size = %d - Address = %p.\n", pv_data->size, pv_data->buff);
157 } else {
158 PRINT_INFO(HOSTAPD_DBG, "Couldn't send packet - Size = %d - Address = %p.\n", pv_data->size, pv_data->buff);
159 }
160
161
162
163 /* incase of fully hosting mode, the freeing will be done in response to the cfg packet */
164 kfree(pv_data->buff);
165
166 kfree(pv_data);
167 }
168 static int mon_mgmt_tx(struct net_device *dev, const u8 *buf, size_t len)
169 {
170 struct tx_complete_mon_data *mgmt_tx = NULL;
171
172 if (dev == NULL) {
173 PRINT_D(HOSTAPD_DBG, "ERROR: dev == NULL\n");
174 return -EFAULT;
175 }
176
177 netif_stop_queue(dev);
178 mgmt_tx = kmalloc(sizeof(struct tx_complete_mon_data), GFP_ATOMIC);
179 if (mgmt_tx == NULL) {
180 PRINT_ER("Failed to allocate memory for mgmt_tx structure\n");
181 return -EFAULT;
182 }
183
184 mgmt_tx->buff = kmalloc(len, GFP_ATOMIC);
185 if (mgmt_tx->buff == NULL) {
186 PRINT_ER("Failed to allocate memory for mgmt_tx buff\n");
187 kfree(mgmt_tx);
188 return -EFAULT;
189
190 }
191
192 mgmt_tx->size = len;
193
194 memcpy(mgmt_tx->buff, buf, len);
195 wilc_wlan_txq_add_mgmt_pkt(dev, mgmt_tx, mgmt_tx->buff, mgmt_tx->size,
196 mgmt_tx_complete);
197
198 netif_wake_queue(dev);
199 return 0;
200 }
201
202 /**
203 * @brief WILC_WFI_mon_xmit
204 * @details
205 * @param[in]
206 * @return int : Return 0 on Success
207 * @author mdaftedar
208 * @date 12 JUL 2012
209 * @version 1.0
210 */
211 static netdev_tx_t WILC_WFI_mon_xmit(struct sk_buff *skb,
212 struct net_device *dev)
213 {
214 u32 rtap_len, i, ret = 0;
215 struct WILC_WFI_mon_priv *mon_priv;
216
217 struct sk_buff *skb2;
218 struct wilc_wfi_radiotap_cb_hdr *cb_hdr;
219
220 if (wilc_wfi_mon == NULL)
221 return -EFAULT;
222
223 mon_priv = netdev_priv(wilc_wfi_mon);
224
225 if (mon_priv == NULL) {
226 PRINT_ER("Monitor interface private structure is NULL\n");
227 return -EFAULT;
228 }
229
230
231 rtap_len = ieee80211_get_radiotap_len(skb->data);
232 if (skb->len < rtap_len) {
233 PRINT_ER("Error in radiotap header\n");
234 return -1;
235 }
236 /* skip the radiotap header */
237 PRINT_INFO(HOSTAPD_DBG, "Radiotap len: %d\n", rtap_len);
238
239 if (INFO) {
240 for (i = 0; i < rtap_len; i++)
241 PRINT_INFO(HOSTAPD_DBG, "Radiotap_hdr[%d] %02x\n", i, skb->data[i]);
242 }
243 /* Skip the ratio tap header */
244 skb_pull(skb, rtap_len);
245
246 if (skb->data[0] == 0xc0)
247 PRINT_INFO(HOSTAPD_DBG, "%x:%x:%x:%x:%x%x\n", skb->data[4], skb->data[5], skb->data[6], skb->data[7], skb->data[8], skb->data[9]);
248
249 if (skb->data[0] == 0xc0 && (!(memcmp(broadcast, &skb->data[4], 6)))) {
250 skb2 = dev_alloc_skb(skb->len + sizeof(struct wilc_wfi_radiotap_cb_hdr));
251
252 memcpy(skb_put(skb2, skb->len), skb->data, skb->len);
253
254 cb_hdr = (struct wilc_wfi_radiotap_cb_hdr *) skb_push(skb2, sizeof(*cb_hdr));
255 memset(cb_hdr, 0, sizeof(struct wilc_wfi_radiotap_cb_hdr));
256
257 cb_hdr->hdr.it_version = 0; /* PKTHDR_RADIOTAP_VERSION; */
258
259 cb_hdr->hdr.it_len = cpu_to_le16(sizeof(struct wilc_wfi_radiotap_cb_hdr));
260
261 cb_hdr->hdr.it_present = cpu_to_le32(
262 (1 << IEEE80211_RADIOTAP_RATE) |
263 (1 << IEEE80211_RADIOTAP_TX_FLAGS));
264
265 cb_hdr->rate = 5; /* txrate->bitrate / 5; */
266 cb_hdr->tx_flags = 0x0004;
267
268 skb2->dev = wilc_wfi_mon;
269 skb_set_mac_header(skb2, 0);
270 skb2->ip_summed = CHECKSUM_UNNECESSARY;
271 skb2->pkt_type = PACKET_OTHERHOST;
272 skb2->protocol = htons(ETH_P_802_2);
273 memset(skb2->cb, 0, sizeof(skb2->cb));
274
275 netif_rx(skb2);
276
277 return 0;
278 }
279 skb->dev = mon_priv->real_ndev;
280
281 PRINT_INFO(HOSTAPD_DBG, "Skipping the radiotap header\n");
282
283
284
285 /* actual deliver of data is device-specific, and not shown here */
286 PRINT_INFO(HOSTAPD_DBG, "SKB netdevice name = %s\n", skb->dev->name);
287 PRINT_INFO(HOSTAPD_DBG, "MONITOR real dev name = %s\n", mon_priv->real_ndev->name);
288
289 /* Identify if Ethernet or MAC header (data or mgmt) */
290 memcpy(srcAdd, &skb->data[10], 6);
291 memcpy(bssid, &skb->data[16], 6);
292 /* if source address and bssid fields are equal>>Mac header */
293 /*send it to mgmt frames handler */
294 if (!(memcmp(srcAdd, bssid, 6))) {
295 mon_mgmt_tx(mon_priv->real_ndev, skb->data, skb->len);
296 dev_kfree_skb(skb);
297 } else
298 ret = wilc_mac_xmit(skb, mon_priv->real_ndev);
299
300 return ret;
301 }
302
303 static const struct net_device_ops wilc_wfi_netdev_ops = {
304 .ndo_start_xmit = WILC_WFI_mon_xmit,
305
306 };
307
308 /**
309 * @brief WILC_WFI_init_mon_interface
310 * @details
311 * @param[in]
312 * @return int : Return 0 on Success
313 * @author mdaftedar
314 * @date 12 JUL 2012
315 * @version 1.0
316 */
317 struct net_device *WILC_WFI_init_mon_interface(const char *name, struct net_device *real_dev)
318 {
319
320
321 u32 ret = 0;
322 struct WILC_WFI_mon_priv *priv;
323
324 /*If monitor interface is already initialized, return it*/
325 if (wilc_wfi_mon) {
326 return wilc_wfi_mon;
327 }
328
329 wilc_wfi_mon = alloc_etherdev(sizeof(struct WILC_WFI_mon_priv));
330 if (!wilc_wfi_mon) {
331 PRINT_ER("failed to allocate memory\n");
332 return NULL;
333
334 }
335
336 wilc_wfi_mon->type = ARPHRD_IEEE80211_RADIOTAP;
337 strncpy(wilc_wfi_mon->name, name, IFNAMSIZ);
338 wilc_wfi_mon->name[IFNAMSIZ - 1] = 0;
339 wilc_wfi_mon->netdev_ops = &wilc_wfi_netdev_ops;
340
341 ret = register_netdevice(wilc_wfi_mon);
342 if (ret) {
343 PRINT_ER(" register_netdevice failed (%d)\n", ret);
344 return NULL;
345 }
346 priv = netdev_priv(wilc_wfi_mon);
347 if (priv == NULL) {
348 PRINT_ER("private structure is NULL\n");
349 return NULL;
350 }
351
352 priv->real_ndev = real_dev;
353
354 return wilc_wfi_mon;
355 }
356
357 /**
358 * @brief WILC_WFI_deinit_mon_interface
359 * @details
360 * @param[in]
361 * @return int : Return 0 on Success
362 * @author mdaftedar
363 * @date 12 JUL 2012
364 * @version 1.0
365 */
366 int WILC_WFI_deinit_mon_interface(void)
367 {
368 bool rollback_lock = false;
369
370 if (wilc_wfi_mon != NULL) {
371 PRINT_D(HOSTAPD_DBG, "In Deinit monitor interface\n");
372 PRINT_D(HOSTAPD_DBG, "RTNL is being locked\n");
373 if (rtnl_is_locked()) {
374 rtnl_unlock();
375 rollback_lock = true;
376 }
377 PRINT_D(HOSTAPD_DBG, "Unregister netdev\n");
378 unregister_netdev(wilc_wfi_mon);
379
380 if (rollback_lock) {
381 rtnl_lock();
382 rollback_lock = false;
383 }
384 wilc_wfi_mon = NULL;
385 }
386 return 0;
387
388 }
This page took 0.043424 seconds and 5 git commands to generate.