linux_mon.c 9.7 KB

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