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 static struct net_device *wilc_wfi_mon; /* global monitor netdev */
29 extern int mac_xmit(struct sk_buff *skb, struct net_device *dev);
34 u8 broadcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
36 * @brief WILC_WFI_monitor_rx
39 * @return int : Return 0 on Success
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)
52 void WILC_WFI_monitor_rx(u8 *buff, u32 size)
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;
59 PRINT_INFO(HOSTAPD_DBG, "In monitor interface receive function\n");
61 if (wilc_wfi_mon == NULL)
64 if (!netif_running(wilc_wfi_mon)) {
65 PRINT_INFO(HOSTAPD_DBG, "Monitor interface already RUNNING\n");
70 memcpy(&header, (buff - HOST_HDR_OFFSET), HOST_HDR_OFFSET);
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);
76 if (pkt_offset & IS_MANAGMEMENT_CALLBACK) {
78 /* hostapd callback mgmt frame */
80 skb = dev_alloc_skb(size + sizeof(struct wilc_wfi_radiotap_cb_hdr));
82 PRINT_INFO(HOSTAPD_DBG, "Monitor if : No memory to allocate skb");
86 memcpy(skb_put(skb, size), buff, size);
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));
91 cb_hdr->hdr.it_version = 0; /* PKTHDR_RADIOTAP_VERSION; */
93 cb_hdr->hdr.it_len = cpu_to_le16(sizeof(struct wilc_wfi_radiotap_cb_hdr));
95 cb_hdr->hdr.it_present = cpu_to_le32(
96 (1 << IEEE80211_RADIOTAP_RATE) |
97 (1 << IEEE80211_RADIOTAP_TX_FLAGS));
99 cb_hdr->rate = 5; /* txrate->bitrate / 5; */
101 if (pkt_offset & IS_MGMT_STATUS_SUCCES) {
103 cb_hdr->tx_flags = IEEE80211_RADIOTAP_F_TX_RTS;
105 cb_hdr->tx_flags = IEEE80211_RADIOTAP_F_TX_FAIL;
110 skb = dev_alloc_skb(size + sizeof(struct wilc_wfi_radiotap_hdr));
113 PRINT_INFO(HOSTAPD_DBG, "Monitor if : No memory to allocate skb");
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; */
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));
144 struct tx_complete_mon_data {
149 static void mgmt_tx_complete(void *priv, int status)
152 struct tx_complete_mon_data *pv_data = (struct tx_complete_mon_data *)priv;
153 u8 *buf = pv_data->buff;
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);
161 PRINT_INFO(HOSTAPD_DBG, "Couldn't send packet - Size = %d - Address = %p.\n", pv_data->size, pv_data->buff);
166 /* incase of fully hosting mode, the freeing will be done in response to the cfg packet */
167 kfree(pv_data->buff);
171 static int mon_mgmt_tx(struct net_device *dev, const u8 *buf, size_t len)
173 struct tx_complete_mon_data *mgmt_tx = NULL;
176 PRINT_D(HOSTAPD_DBG, "ERROR: dev == NULL\n");
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");
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");
197 memcpy(mgmt_tx->buff, buf, len);
198 wilc_wlan_txq_add_mgmt_pkt(mgmt_tx, mgmt_tx->buff, mgmt_tx->size,
201 netif_wake_queue(dev);
206 * @brief WILC_WFI_mon_xmit
209 * @return int : Return 0 on Success
214 static netdev_tx_t WILC_WFI_mon_xmit(struct sk_buff *skb,
215 struct net_device *dev)
217 u32 rtap_len, i, ret = 0;
218 struct WILC_WFI_mon_priv *mon_priv;
220 struct sk_buff *skb2;
221 struct wilc_wfi_radiotap_cb_hdr *cb_hdr;
223 if (wilc_wfi_mon == NULL)
226 mon_priv = netdev_priv(wilc_wfi_mon);
228 if (mon_priv == NULL) {
229 PRINT_ER("Monitor interface private structure is NULL\n");
234 rtap_len = ieee80211_get_radiotap_len(skb->data);
235 if (skb->len < rtap_len) {
236 PRINT_ER("Error in radiotap header\n");
239 /* skip the radiotap header */
240 PRINT_INFO(HOSTAPD_DBG, "Radiotap len: %d\n", rtap_len);
243 for (i = 0; i < rtap_len; i++)
244 PRINT_INFO(HOSTAPD_DBG, "Radiotap_hdr[%d] %02x\n", i, skb->data[i]);
246 /* Skip the ratio tap header */
247 skb_pull(skb, rtap_len);
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]);
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));
255 memcpy(skb_put(skb2, skb->len), skb->data, skb->len);
257 cb_hdr = (struct wilc_wfi_radiotap_cb_hdr *) skb_push(skb2, sizeof(*cb_hdr));
258 memset(cb_hdr, 0, sizeof(struct wilc_wfi_radiotap_cb_hdr));
260 cb_hdr->hdr.it_version = 0; /* PKTHDR_RADIOTAP_VERSION; */
262 cb_hdr->hdr.it_len = cpu_to_le16(sizeof(struct wilc_wfi_radiotap_cb_hdr));
264 cb_hdr->hdr.it_present = cpu_to_le32(
265 (1 << IEEE80211_RADIOTAP_RATE) |
266 (1 << IEEE80211_RADIOTAP_TX_FLAGS));
268 cb_hdr->rate = 5; /* txrate->bitrate / 5; */
269 cb_hdr->tx_flags = 0x0004;
271 skb2->dev = wilc_wfi_mon;
272 skb_set_mac_header(skb2, 0);
273 skb2->ip_summed = CHECKSUM_UNNECESSARY;
274 skb2->pkt_type = PACKET_OTHERHOST;
275 skb2->protocol = htons(ETH_P_802_2);
276 memset(skb2->cb, 0, sizeof(skb2->cb));
282 skb->dev = mon_priv->real_ndev;
284 PRINT_INFO(HOSTAPD_DBG, "Skipping the radiotap header\n");
288 /* actual deliver of data is device-specific, and not shown here */
289 PRINT_INFO(HOSTAPD_DBG, "SKB netdevice name = %s\n", skb->dev->name);
290 PRINT_INFO(HOSTAPD_DBG, "MONITOR real dev name = %s\n", mon_priv->real_ndev->name);
292 /* Identify if Ethernet or MAC header (data or mgmt) */
293 memcpy(srcAdd, &skb->data[10], 6);
294 memcpy(bssid, &skb->data[16], 6);
295 /* if source address and bssid fields are equal>>Mac header */
296 /*send it to mgmt frames handler */
297 if (!(memcmp(srcAdd, bssid, 6))) {
298 mon_mgmt_tx(mon_priv->real_ndev, skb->data, skb->len);
301 ret = mac_xmit(skb, mon_priv->real_ndev);
306 static const struct net_device_ops wilc_wfi_netdev_ops = {
307 .ndo_start_xmit = WILC_WFI_mon_xmit,
312 * @brief WILC_WFI_init_mon_interface
315 * @return int : Return 0 on Success
320 struct net_device *WILC_WFI_init_mon_interface(const char *name, struct net_device *real_dev)
325 struct WILC_WFI_mon_priv *priv;
327 /*If monitor interface is already initialized, return it*/
332 wilc_wfi_mon = alloc_etherdev(sizeof(struct WILC_WFI_mon_priv));
334 PRINT_ER("failed to allocate memory\n");
339 wilc_wfi_mon->type = ARPHRD_IEEE80211_RADIOTAP;
340 strncpy(wilc_wfi_mon->name, name, IFNAMSIZ);
341 wilc_wfi_mon->name[IFNAMSIZ - 1] = 0;
342 wilc_wfi_mon->netdev_ops = &wilc_wfi_netdev_ops;
344 ret = register_netdevice(wilc_wfi_mon);
346 PRINT_ER(" register_netdevice failed (%d)\n", ret);
349 priv = netdev_priv(wilc_wfi_mon);
351 PRINT_ER("private structure is NULL\n");
355 priv->real_ndev = real_dev;
361 * @brief WILC_WFI_deinit_mon_interface
364 * @return int : Return 0 on Success
369 int WILC_WFI_deinit_mon_interface(void)
371 bool rollback_lock = false;
373 if (wilc_wfi_mon != NULL) {
374 PRINT_D(HOSTAPD_DBG, "In Deinit monitor interface\n");
375 PRINT_D(HOSTAPD_DBG, "RTNL is being locked\n");
376 if (rtnl_is_locked()) {
378 rollback_lock = true;
380 PRINT_D(HOSTAPD_DBG, "Unregister netdev\n");
381 unregister_netdev(wilc_wfi_mon);
385 rollback_lock = false;