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