3 * @brief File Operations OS wrapper functionality
5 * @sa wilc_wfi_netdevice.h
9 #include "wilc_wfi_cfgoperations.h"
10 #include "linux_wlan_common.h"
11 #include "wilc_wlan_if.h"
12 #include "wilc_wlan.h"
15 struct wilc_wfi_radiotap_hdr {
16 struct ieee80211_radiotap_header hdr;
18 } __attribute__((packed));
20 struct wilc_wfi_radiotap_cb_hdr {
21 struct ieee80211_radiotap_header hdr;
25 } __attribute__((packed));
27 extern linux_wlan_t *g_linux_wlan;
29 static struct net_device *wilc_wfi_mon; /* global monitor netdev */
31 extern int mac_xmit(struct sk_buff *skb, struct net_device *dev);
36 u8 broadcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
38 * @brief WILC_WFI_monitor_rx
41 * @return int : Return 0 on Success
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)
54 void WILC_WFI_monitor_rx(u8 *buff, u32 size)
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;
61 PRINT_INFO(HOSTAPD_DBG, "In monitor interface receive function\n");
63 if (wilc_wfi_mon == NULL)
66 if (!netif_running(wilc_wfi_mon)) {
67 PRINT_INFO(HOSTAPD_DBG, "Monitor interface already RUNNING\n");
72 memcpy(&header, (buff - HOST_HDR_OFFSET), HOST_HDR_OFFSET);
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);
78 if (pkt_offset & IS_MANAGMEMENT_CALLBACK) {
80 /* hostapd callback mgmt frame */
82 skb = dev_alloc_skb(size + sizeof(struct wilc_wfi_radiotap_cb_hdr));
84 PRINT_INFO(HOSTAPD_DBG, "Monitor if : No memory to allocate skb");
88 memcpy(skb_put(skb, size), buff, size);
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));
93 cb_hdr->hdr.it_version = 0; /* PKTHDR_RADIOTAP_VERSION; */
95 cb_hdr->hdr.it_len = cpu_to_le16(sizeof(struct wilc_wfi_radiotap_cb_hdr));
97 cb_hdr->hdr.it_present = cpu_to_le32(
98 (1 << IEEE80211_RADIOTAP_RATE) |
99 (1 << IEEE80211_RADIOTAP_TX_FLAGS));
101 cb_hdr->rate = 5; /* txrate->bitrate / 5; */
103 if (pkt_offset & IS_MGMT_STATUS_SUCCES) {
105 cb_hdr->tx_flags = IEEE80211_RADIOTAP_F_TX_RTS;
107 cb_hdr->tx_flags = IEEE80211_RADIOTAP_F_TX_FAIL;
112 skb = dev_alloc_skb(size + sizeof(struct wilc_wfi_radiotap_hdr));
115 PRINT_INFO(HOSTAPD_DBG, "Monitor if : No memory to allocate skb");
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; */
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));
146 struct tx_complete_mon_data {
151 static void mgmt_tx_complete(void *priv, int status)
154 struct tx_complete_mon_data *pv_data = (struct tx_complete_mon_data *)priv;
155 u8 *buf = pv_data->buff;
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);
163 PRINT_INFO(HOSTAPD_DBG, "Couldn't send packet - Size = %d - Address = %p.\n", pv_data->size, pv_data->buff);
168 /* incase of fully hosting mode, the freeing will be done in response to the cfg packet */
169 kfree(pv_data->buff);
173 static int mon_mgmt_tx(struct net_device *dev, const u8 *buf, size_t len)
175 struct tx_complete_mon_data *mgmt_tx = NULL;
178 PRINT_D(HOSTAPD_DBG, "ERROR: dev == NULL\n");
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");
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");
199 memcpy(mgmt_tx->buff, buf, len);
200 wilc_wlan_txq_add_mgmt_pkt(mgmt_tx, mgmt_tx->buff, mgmt_tx->size,
203 netif_wake_queue(dev);
208 * @brief WILC_WFI_mon_xmit
211 * @return int : Return 0 on Success
216 static netdev_tx_t WILC_WFI_mon_xmit(struct sk_buff *skb,
217 struct net_device *dev)
219 u32 rtap_len, i, ret = 0;
220 struct WILC_WFI_mon_priv *mon_priv;
222 struct sk_buff *skb2;
223 struct wilc_wfi_radiotap_cb_hdr *cb_hdr;
225 if (wilc_wfi_mon == NULL)
228 mon_priv = netdev_priv(wilc_wfi_mon);
230 if (mon_priv == NULL) {
231 PRINT_ER("Monitor interface private structure is NULL\n");
236 rtap_len = ieee80211_get_radiotap_len(skb->data);
237 if (skb->len < rtap_len) {
238 PRINT_ER("Error in radiotap header\n");
241 /* skip the radiotap header */
242 PRINT_INFO(HOSTAPD_DBG, "Radiotap len: %d\n", rtap_len);
245 for (i = 0; i < rtap_len; i++)
246 PRINT_INFO(HOSTAPD_DBG, "Radiotap_hdr[%d] %02x\n", i, skb->data[i]);
248 /* Skip the ratio tap header */
249 skb_pull(skb, rtap_len);
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]);
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));
257 memcpy(skb_put(skb2, skb->len), skb->data, skb->len);
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));
262 cb_hdr->hdr.it_version = 0; /* PKTHDR_RADIOTAP_VERSION; */
264 cb_hdr->hdr.it_len = cpu_to_le16(sizeof(struct wilc_wfi_radiotap_cb_hdr));
266 cb_hdr->hdr.it_present = cpu_to_le32(
267 (1 << IEEE80211_RADIOTAP_RATE) |
268 (1 << IEEE80211_RADIOTAP_TX_FLAGS));
270 cb_hdr->rate = 5; /* txrate->bitrate / 5; */
271 cb_hdr->tx_flags = 0x0004;
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));
284 skb->dev = mon_priv->real_ndev;
286 PRINT_INFO(HOSTAPD_DBG, "Skipping the radiotap header\n");
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);
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);
303 ret = mac_xmit(skb, mon_priv->real_ndev);
308 static const struct net_device_ops wilc_wfi_netdev_ops = {
309 .ndo_start_xmit = WILC_WFI_mon_xmit,
314 * @brief WILC_WFI_init_mon_interface
317 * @return int : Return 0 on Success
322 struct net_device *WILC_WFI_init_mon_interface(const char *name, struct net_device *real_dev)
327 struct WILC_WFI_mon_priv *priv;
329 /*If monitor interface is already initialized, return it*/
334 wilc_wfi_mon = alloc_etherdev(sizeof(struct WILC_WFI_mon_priv));
336 PRINT_ER("failed to allocate memory\n");
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;
346 ret = register_netdevice(wilc_wfi_mon);
348 PRINT_ER(" register_netdevice failed (%d)\n", ret);
351 priv = netdev_priv(wilc_wfi_mon);
353 PRINT_ER("private structure is NULL\n");
357 priv->real_ndev = real_dev;
363 * @brief WILC_WFI_deinit_mon_interface
366 * @return int : Return 0 on Success
371 int WILC_WFI_deinit_mon_interface(void)
373 bool rollback_lock = false;
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()) {
380 rollback_lock = true;
382 PRINT_D(HOSTAPD_DBG, "Unregister netdev\n");
383 unregister_netdev(wilc_wfi_mon);
387 rollback_lock = false;