GNU Linux-libre 4.4.289-gnu1
[releases.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 extern int  mac_xmit(struct sk_buff *skb, struct net_device *dev);
30
31
32 u8 srcAdd[6];
33 u8 bssid[6];
34 u8 broadcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
35 /**
36  *  @brief      WILC_WFI_monitor_rx
37  *  @details
38  *  @param[in]
39  *  @return     int : Return 0 on Success
40  *  @author     mdaftedar
41  *  @date       12 JUL 2012
42  *  @version    1.0
43  */
44
45 #define IEEE80211_RADIOTAP_F_TX_RTS     0x0004  /* used rts/cts handshake */
46 #define IEEE80211_RADIOTAP_F_TX_FAIL    0x0001  /* failed due to excessive*/
47 #define IS_MANAGMEMENT                          0x100
48 #define IS_MANAGMEMENT_CALLBACK                 0x080
49 #define IS_MGMT_STATUS_SUCCES                   0x040
50 #define GET_PKT_OFFSET(a) (((a) >> 22) & 0x1ff)
51
52 void WILC_WFI_monitor_rx(u8 *buff, u32 size)
53 {
54         u32 header, pkt_offset;
55         struct sk_buff *skb = NULL;
56         struct wilc_wfi_radiotap_hdr *hdr;
57         struct wilc_wfi_radiotap_cb_hdr *cb_hdr;
58
59         PRINT_INFO(HOSTAPD_DBG, "In monitor interface receive function\n");
60
61         if (wilc_wfi_mon == NULL)
62                 return;
63
64         if (!netif_running(wilc_wfi_mon)) {
65                 PRINT_INFO(HOSTAPD_DBG, "Monitor interface already RUNNING\n");
66                 return;
67         }
68
69         /* Get WILC header */
70         memcpy(&header, (buff - HOST_HDR_OFFSET), HOST_HDR_OFFSET);
71
72         /* The packet offset field conain info about what type of managment frame */
73         /* we are dealing with and ack status */
74         pkt_offset = GET_PKT_OFFSET(header);
75
76         if (pkt_offset & IS_MANAGMEMENT_CALLBACK) {
77
78                 /* hostapd callback mgmt frame */
79
80                 skb = dev_alloc_skb(size + sizeof(struct wilc_wfi_radiotap_cb_hdr));
81                 if (skb == NULL) {
82                         PRINT_INFO(HOSTAPD_DBG, "Monitor if : No memory to allocate skb");
83                         return;
84                 }
85
86                 memcpy(skb_put(skb, size), buff, size);
87
88                 cb_hdr = (struct wilc_wfi_radiotap_cb_hdr *) skb_push(skb, sizeof(*cb_hdr));
89                 memset(cb_hdr, 0, sizeof(struct wilc_wfi_radiotap_cb_hdr));
90
91                 cb_hdr->hdr.it_version = 0; /* PKTHDR_RADIOTAP_VERSION; */
92
93                 cb_hdr->hdr.it_len = cpu_to_le16(sizeof(struct wilc_wfi_radiotap_cb_hdr));
94
95                 cb_hdr->hdr.it_present = cpu_to_le32(
96                                 (1 << IEEE80211_RADIOTAP_RATE) |
97                                 (1 << IEEE80211_RADIOTAP_TX_FLAGS));
98
99                 cb_hdr->rate = 5; /* txrate->bitrate / 5; */
100
101                 if (pkt_offset & IS_MGMT_STATUS_SUCCES) {
102                         /* success */
103                         cb_hdr->tx_flags = IEEE80211_RADIOTAP_F_TX_RTS;
104                 } else {
105                         cb_hdr->tx_flags = IEEE80211_RADIOTAP_F_TX_FAIL;
106                 }
107
108         } else {
109
110                 skb = dev_alloc_skb(size + sizeof(struct wilc_wfi_radiotap_hdr));
111
112                 if (skb == NULL) {
113                         PRINT_INFO(HOSTAPD_DBG, "Monitor if : No memory to allocate skb");
114                         return;
115                 }
116
117                 memcpy(skb_put(skb, size), buff, size);
118                 hdr = (struct wilc_wfi_radiotap_hdr *) skb_push(skb, sizeof(*hdr));
119                 memset(hdr, 0, sizeof(struct wilc_wfi_radiotap_hdr));
120                 hdr->hdr.it_version = 0; /* PKTHDR_RADIOTAP_VERSION; */
121                 hdr->hdr.it_len = cpu_to_le16(sizeof(struct wilc_wfi_radiotap_hdr));
122                 PRINT_INFO(HOSTAPD_DBG, "Radiotap len %d\n", hdr->hdr.it_len);
123                 hdr->hdr.it_present = cpu_to_le32
124                                 (1 << IEEE80211_RADIOTAP_RATE);                   /* | */
125                 PRINT_INFO(HOSTAPD_DBG, "Presentflags %d\n", hdr->hdr.it_present);
126                 hdr->rate = 5; /* txrate->bitrate / 5; */
127
128         }
129
130
131
132         skb->dev = wilc_wfi_mon;
133         skb_set_mac_header(skb, 0);
134         skb->ip_summed = CHECKSUM_UNNECESSARY;
135         skb->pkt_type = PACKET_OTHERHOST;
136         skb->protocol = htons(ETH_P_802_2);
137         memset(skb->cb, 0, sizeof(skb->cb));
138
139         netif_rx(skb);
140
141
142 }
143
144 struct tx_complete_mon_data {
145         int size;
146         void *buff;
147 };
148
149 static void mgmt_tx_complete(void *priv, int status)
150 {
151
152         struct tx_complete_mon_data *pv_data = (struct tx_complete_mon_data *)priv;
153         u8 *buf =  pv_data->buff;
154
155
156
157         if (status == 1) {
158                 if (INFO || buf[0] == 0x10 || buf[0] == 0xb0)
159                         PRINT_INFO(HOSTAPD_DBG, "Packet sent successfully - Size = %d - Address = %p.\n", pv_data->size, pv_data->buff);
160         } else {
161                 PRINT_INFO(HOSTAPD_DBG, "Couldn't send packet - Size = %d - Address = %p.\n", pv_data->size, pv_data->buff);
162         }
163
164
165
166         /* incase of fully hosting mode, the freeing will be done in response to the cfg packet */
167         kfree(pv_data->buff);
168
169         kfree(pv_data);
170 }
171 static int mon_mgmt_tx(struct net_device *dev, const u8 *buf, size_t len)
172 {
173         struct tx_complete_mon_data *mgmt_tx = NULL;
174
175         if (dev == NULL) {
176                 PRINT_D(HOSTAPD_DBG, "ERROR: dev == NULL\n");
177                 return -EFAULT;
178         }
179
180         netif_stop_queue(dev);
181         mgmt_tx = kmalloc(sizeof(struct tx_complete_mon_data), GFP_ATOMIC);
182         if (mgmt_tx == NULL) {
183                 PRINT_ER("Failed to allocate memory for mgmt_tx structure\n");
184                 return -EFAULT;
185         }
186
187         mgmt_tx->buff = kmalloc(len, GFP_ATOMIC);
188         if (mgmt_tx->buff == NULL) {
189                 PRINT_ER("Failed to allocate memory for mgmt_tx buff\n");
190                 kfree(mgmt_tx);
191                 return -EFAULT;
192
193         }
194
195         mgmt_tx->size = len;
196
197         memcpy(mgmt_tx->buff, buf, len);
198         wilc_wlan_txq_add_mgmt_pkt(mgmt_tx, mgmt_tx->buff, mgmt_tx->size,
199                                    mgmt_tx_complete);
200
201         netif_wake_queue(dev);
202         return 0;
203 }
204
205 /**
206  *  @brief      WILC_WFI_mon_xmit
207  *  @details
208  *  @param[in]
209  *  @return     int : Return 0 on Success
210  *  @author     mdaftedar
211  *  @date       12 JUL 2012
212  *  @version    1.0
213  */
214 static netdev_tx_t WILC_WFI_mon_xmit(struct sk_buff *skb,
215                                      struct net_device *dev)
216 {
217         u32 rtap_len, i, ret = 0;
218         struct WILC_WFI_mon_priv  *mon_priv;
219
220         struct sk_buff *skb2;
221         struct wilc_wfi_radiotap_cb_hdr *cb_hdr;
222
223         if (wilc_wfi_mon == NULL)
224                 return -EFAULT;
225
226         mon_priv = netdev_priv(wilc_wfi_mon);
227
228         if (mon_priv == NULL) {
229                 PRINT_ER("Monitor interface private structure is NULL\n");
230                 return -EFAULT;
231         }
232
233
234         rtap_len = ieee80211_get_radiotap_len(skb->data);
235         if (skb->len < rtap_len) {
236                 PRINT_ER("Error in radiotap header\n");
237                 return -1;
238         }
239         /* skip the radiotap header */
240         PRINT_INFO(HOSTAPD_DBG, "Radiotap len: %d\n", rtap_len);
241
242         if (INFO) {
243                 for (i = 0; i < rtap_len; i++)
244                         PRINT_INFO(HOSTAPD_DBG, "Radiotap_hdr[%d] %02x\n", i, skb->data[i]);
245         }
246         /* Skip the ratio tap header */
247         skb_pull(skb, rtap_len);
248
249         if (skb->data[0] == 0xc0)
250                 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]);
251
252         if (skb->data[0] == 0xc0 && (!(memcmp(broadcast, &skb->data[4], 6)))) {
253                 skb2 = dev_alloc_skb(skb->len + sizeof(struct wilc_wfi_radiotap_cb_hdr));
254                 if (!skb2)
255                         return -ENOMEM;
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 }