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