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