wl_linux_mon.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /*
  2. * Broadcom Dongle Host Driver (DHD), Linux monitor network interface
  3. *
  4. * Copyright (C) 1999-2014, Broadcom Corporation
  5. *
  6. * Unless you and Broadcom execute a separate written software license
  7. * agreement governing use of this software, this software is licensed to you
  8. * under the terms of the GNU General Public License version 2 (the "GPL"),
  9. * available at http://www.broadcom.com/licenses/GPLv2.php, with the
  10. * following added to such license:
  11. *
  12. * As a special exception, the copyright holders of this software give you
  13. * permission to link this software with independent modules, and to copy and
  14. * distribute the resulting executable under terms of your choice, provided that
  15. * you also meet, for each linked independent module, the terms and conditions of
  16. * the license of that module. An independent module is a module which is not
  17. * derived from this software. The special exception does not apply to any
  18. * modifications of the software.
  19. *
  20. * Notwithstanding the above, under no circumstances may you combine this
  21. * software in any way with any other Broadcom software provided under a license
  22. * other than the GPL, without Broadcom's express prior written consent.
  23. *
  24. * $Id: wl_linux_mon.c 425343 2013-09-23 23:04:47Z $
  25. */
  26. #include <osl.h>
  27. #include <linux/string.h>
  28. #include <linux/module.h>
  29. #include <linux/netdevice.h>
  30. #include <linux/etherdevice.h>
  31. #include <linux/if_arp.h>
  32. #include <linux/ieee80211.h>
  33. #include <linux/rtnetlink.h>
  34. #include <net/ieee80211_radiotap.h>
  35. #include <wlioctl.h>
  36. #include <bcmutils.h>
  37. #include <dhd_dbg.h>
  38. #include <dngl_stats.h>
  39. #include <dhd.h>
  40. typedef enum monitor_states
  41. {
  42. MONITOR_STATE_DEINIT = 0x0,
  43. MONITOR_STATE_INIT = 0x1,
  44. MONITOR_STATE_INTERFACE_ADDED = 0x2,
  45. MONITOR_STATE_INTERFACE_DELETED = 0x4
  46. } monitor_states_t;
  47. int dhd_add_monitor(char *name, struct net_device **new_ndev);
  48. extern int dhd_start_xmit(struct sk_buff *skb, struct net_device *net);
  49. int dhd_del_monitor(struct net_device *ndev);
  50. int dhd_monitor_init(void *dhd_pub);
  51. int dhd_monitor_uninit(void);
  52. /**
  53. * Local declarations and defintions (not exposed)
  54. */
  55. #ifndef DHD_MAX_IFS
  56. #define DHD_MAX_IFS 16
  57. #endif
  58. #define MON_PRINT(format, ...) printk("DHD-MON: %s " format, __func__, ##__VA_ARGS__)
  59. #define MON_TRACE MON_PRINT
  60. typedef struct monitor_interface {
  61. int radiotap_enabled;
  62. struct net_device* real_ndev; /* The real interface that the monitor is on */
  63. struct net_device* mon_ndev;
  64. } monitor_interface;
  65. typedef struct dhd_linux_monitor {
  66. void *dhd_pub;
  67. monitor_states_t monitor_state;
  68. monitor_interface mon_if[DHD_MAX_IFS];
  69. struct mutex lock; /* lock to protect mon_if */
  70. } dhd_linux_monitor_t;
  71. static dhd_linux_monitor_t g_monitor;
  72. static struct net_device* lookup_real_netdev(char *name);
  73. static monitor_interface* ndev_to_monif(struct net_device *ndev);
  74. static int dhd_mon_if_open(struct net_device *ndev);
  75. static int dhd_mon_if_stop(struct net_device *ndev);
  76. static int dhd_mon_if_subif_start_xmit(struct sk_buff *skb, struct net_device *ndev);
  77. static void dhd_mon_if_set_multicast_list(struct net_device *ndev);
  78. static int dhd_mon_if_change_mac(struct net_device *ndev, void *addr);
  79. static const struct net_device_ops dhd_mon_if_ops = {
  80. .ndo_open = dhd_mon_if_open,
  81. .ndo_stop = dhd_mon_if_stop,
  82. .ndo_start_xmit = dhd_mon_if_subif_start_xmit,
  83. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 2, 0))
  84. .ndo_set_rx_mode = dhd_mon_if_set_multicast_list,
  85. #else
  86. .ndo_set_multicast_list = dhd_mon_if_set_multicast_list,
  87. #endif
  88. .ndo_set_mac_address = dhd_mon_if_change_mac,
  89. };
  90. /**
  91. * Local static function defintions
  92. */
  93. /* Look up dhd's net device table to find a match (e.g. interface "eth0" is a match for "mon.eth0"
  94. * "p2p-eth0-0" is a match for "mon.p2p-eth0-0")
  95. */
  96. static struct net_device* lookup_real_netdev(char *name)
  97. {
  98. struct net_device *ndev_found = NULL;
  99. int i;
  100. int len = 0;
  101. int last_name_len = 0;
  102. struct net_device *ndev;
  103. /* We need to find interface "p2p-p2p-0" corresponding to monitor interface "mon-p2p-0",
  104. * Once mon iface name reaches IFNAMSIZ, it is reset to p2p0-0 and corresponding mon
  105. * iface would be mon-p2p0-0.
  106. */
  107. for (i = 0; i < DHD_MAX_IFS; i++) {
  108. ndev = dhd_idx2net(g_monitor.dhd_pub, i);
  109. /* Skip "p2p" and look for "-p2p0-x" in monitor interface name. If it
  110. * it matches, then this netdev is the corresponding real_netdev.
  111. */
  112. if (ndev && strstr(ndev->name, "p2p-p2p0")) {
  113. len = strlen("p2p");
  114. } else {
  115. /* if p2p- is not present, then the IFNAMSIZ have reached and name
  116. * would have got reset. In this casse,look for p2p0-x in mon-p2p0-x
  117. */
  118. len = 0;
  119. }
  120. if (ndev && strstr(name, (ndev->name + len))) {
  121. if (strlen(ndev->name) > last_name_len) {
  122. ndev_found = ndev;
  123. last_name_len = strlen(ndev->name);
  124. }
  125. }
  126. }
  127. return ndev_found;
  128. }
  129. static monitor_interface* ndev_to_monif(struct net_device *ndev)
  130. {
  131. int i;
  132. for (i = 0; i < DHD_MAX_IFS; i++) {
  133. if (g_monitor.mon_if[i].mon_ndev == ndev)
  134. return &g_monitor.mon_if[i];
  135. }
  136. return NULL;
  137. }
  138. static int dhd_mon_if_open(struct net_device *ndev)
  139. {
  140. int ret = 0;
  141. MON_PRINT("enter\n");
  142. return ret;
  143. }
  144. static int dhd_mon_if_stop(struct net_device *ndev)
  145. {
  146. int ret = 0;
  147. MON_PRINT("enter\n");
  148. return ret;
  149. }
  150. static int dhd_mon_if_subif_start_xmit(struct sk_buff *skb, struct net_device *ndev)
  151. {
  152. int ret = 0;
  153. int rtap_len;
  154. int qos_len = 0;
  155. int dot11_hdr_len = 24;
  156. int snap_len = 6;
  157. unsigned char *pdata;
  158. unsigned short frame_ctl;
  159. unsigned char src_mac_addr[6];
  160. unsigned char dst_mac_addr[6];
  161. struct ieee80211_hdr *dot11_hdr;
  162. struct ieee80211_radiotap_header *rtap_hdr;
  163. monitor_interface* mon_if;
  164. MON_PRINT("enter\n");
  165. mon_if = ndev_to_monif(ndev);
  166. if (mon_if == NULL || mon_if->real_ndev == NULL) {
  167. MON_PRINT(" cannot find matched net dev, skip the packet\n");
  168. goto fail;
  169. }
  170. if (unlikely(skb->len < sizeof(struct ieee80211_radiotap_header)))
  171. goto fail;
  172. rtap_hdr = (struct ieee80211_radiotap_header *)skb->data;
  173. if (unlikely(rtap_hdr->it_version))
  174. goto fail;
  175. rtap_len = ieee80211_get_radiotap_len(skb->data);
  176. if (unlikely(skb->len < rtap_len))
  177. goto fail;
  178. MON_PRINT("radiotap len (should be 14): %d\n", rtap_len);
  179. /* Skip the ratio tap header */
  180. skb_pull(skb, rtap_len);
  181. dot11_hdr = (struct ieee80211_hdr *)skb->data;
  182. frame_ctl = le16_to_cpu(dot11_hdr->frame_control);
  183. /* Check if the QoS bit is set */
  184. if ((frame_ctl & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) {
  185. /* Check if this ia a Wireless Distribution System (WDS) frame
  186. * which has 4 MAC addresses
  187. */
  188. if (dot11_hdr->frame_control & 0x0080)
  189. qos_len = 2;
  190. if ((dot11_hdr->frame_control & 0x0300) == 0x0300)
  191. dot11_hdr_len += 6;
  192. memcpy(dst_mac_addr, dot11_hdr->addr1, sizeof(dst_mac_addr));
  193. memcpy(src_mac_addr, dot11_hdr->addr2, sizeof(src_mac_addr));
  194. /* Skip the 802.11 header, QoS (if any) and SNAP, but leave spaces for
  195. * for two MAC addresses
  196. */
  197. skb_pull(skb, dot11_hdr_len + qos_len + snap_len - sizeof(src_mac_addr) * 2);
  198. pdata = (unsigned char*)skb->data;
  199. memcpy(pdata, dst_mac_addr, sizeof(dst_mac_addr));
  200. memcpy(pdata + sizeof(dst_mac_addr), src_mac_addr, sizeof(src_mac_addr));
  201. PKTSETPRIO(skb, 0);
  202. MON_PRINT("if name: %s, matched if name %s\n", ndev->name, mon_if->real_ndev->name);
  203. /* Use the real net device to transmit the packet */
  204. ret = dhd_start_xmit(skb, mon_if->real_ndev);
  205. return ret;
  206. }
  207. fail:
  208. dev_kfree_skb(skb);
  209. return 0;
  210. }
  211. static void dhd_mon_if_set_multicast_list(struct net_device *ndev)
  212. {
  213. monitor_interface* mon_if;
  214. mon_if = ndev_to_monif(ndev);
  215. if (mon_if == NULL || mon_if->real_ndev == NULL) {
  216. MON_PRINT(" cannot find matched net dev, skip the packet\n");
  217. } else {
  218. MON_PRINT("enter, if name: %s, matched if name %s\n",
  219. ndev->name, mon_if->real_ndev->name);
  220. }
  221. }
  222. static int dhd_mon_if_change_mac(struct net_device *ndev, void *addr)
  223. {
  224. int ret = 0;
  225. monitor_interface* mon_if;
  226. mon_if = ndev_to_monif(ndev);
  227. if (mon_if == NULL || mon_if->real_ndev == NULL) {
  228. MON_PRINT(" cannot find matched net dev, skip the packet\n");
  229. } else {
  230. MON_PRINT("enter, if name: %s, matched if name %s\n",
  231. ndev->name, mon_if->real_ndev->name);
  232. }
  233. return ret;
  234. }
  235. /**
  236. * Global function definitions (declared in dhd_linux_mon.h)
  237. */
  238. int dhd_add_monitor(char *name, struct net_device **new_ndev)
  239. {
  240. int i;
  241. int idx = -1;
  242. int ret = 0;
  243. struct net_device* ndev = NULL;
  244. dhd_linux_monitor_t **dhd_mon;
  245. mutex_lock(&g_monitor.lock);
  246. MON_TRACE("enter, if name: %s\n", name);
  247. if (!name || !new_ndev) {
  248. MON_PRINT("invalid parameters\n");
  249. ret = -EINVAL;
  250. goto out;
  251. }
  252. /*
  253. * Find a vacancy
  254. */
  255. for (i = 0; i < DHD_MAX_IFS; i++)
  256. if (g_monitor.mon_if[i].mon_ndev == NULL) {
  257. idx = i;
  258. break;
  259. }
  260. if (idx == -1) {
  261. MON_PRINT("exceeds maximum interfaces\n");
  262. ret = -EFAULT;
  263. goto out;
  264. }
  265. ndev = alloc_etherdev(sizeof(dhd_linux_monitor_t*));
  266. if (!ndev) {
  267. MON_PRINT("failed to allocate memory\n");
  268. ret = -ENOMEM;
  269. goto out;
  270. }
  271. ndev->type = ARPHRD_IEEE80211_RADIOTAP;
  272. strncpy(ndev->name, name, IFNAMSIZ);
  273. ndev->name[IFNAMSIZ - 1] = 0;
  274. ndev->netdev_ops = &dhd_mon_if_ops;
  275. ret = register_netdevice(ndev);
  276. if (ret) {
  277. MON_PRINT(" register_netdevice failed (%d)\n", ret);
  278. goto out;
  279. }
  280. *new_ndev = ndev;
  281. g_monitor.mon_if[idx].radiotap_enabled = TRUE;
  282. g_monitor.mon_if[idx].mon_ndev = ndev;
  283. g_monitor.mon_if[idx].real_ndev = lookup_real_netdev(name);
  284. dhd_mon = (dhd_linux_monitor_t **)netdev_priv(ndev);
  285. *dhd_mon = &g_monitor;
  286. g_monitor.monitor_state = MONITOR_STATE_INTERFACE_ADDED;
  287. MON_PRINT("net device returned: 0x%p\n", ndev);
  288. MON_PRINT("found a matched net device, name %s\n", g_monitor.mon_if[idx].real_ndev->name);
  289. out:
  290. if (ret && ndev)
  291. free_netdev(ndev);
  292. mutex_unlock(&g_monitor.lock);
  293. return ret;
  294. }
  295. int dhd_del_monitor(struct net_device *ndev)
  296. {
  297. int i;
  298. if (!ndev)
  299. return -EINVAL;
  300. mutex_lock(&g_monitor.lock);
  301. for (i = 0; i < DHD_MAX_IFS; i++) {
  302. if (g_monitor.mon_if[i].mon_ndev == ndev ||
  303. g_monitor.mon_if[i].real_ndev == ndev) {
  304. g_monitor.mon_if[i].real_ndev = NULL;
  305. unregister_netdevice(g_monitor.mon_if[i].mon_ndev);
  306. free_netdev(g_monitor.mon_if[i].mon_ndev);
  307. g_monitor.mon_if[i].mon_ndev = NULL;
  308. g_monitor.monitor_state = MONITOR_STATE_INTERFACE_DELETED;
  309. break;
  310. }
  311. }
  312. if (g_monitor.monitor_state != MONITOR_STATE_INTERFACE_DELETED)
  313. MON_PRINT("IF not found in monitor array, is this a monitor IF? 0x%p\n", ndev);
  314. mutex_unlock(&g_monitor.lock);
  315. return 0;
  316. }
  317. int dhd_monitor_init(void *dhd_pub)
  318. {
  319. if (g_monitor.monitor_state == MONITOR_STATE_DEINIT) {
  320. g_monitor.dhd_pub = dhd_pub;
  321. mutex_init(&g_monitor.lock);
  322. g_monitor.monitor_state = MONITOR_STATE_INIT;
  323. }
  324. return 0;
  325. }
  326. int dhd_monitor_uninit(void)
  327. {
  328. int i;
  329. struct net_device *ndev;
  330. mutex_lock(&g_monitor.lock);
  331. if (g_monitor.monitor_state != MONITOR_STATE_DEINIT) {
  332. for (i = 0; i < DHD_MAX_IFS; i++) {
  333. ndev = g_monitor.mon_if[i].mon_ndev;
  334. if (ndev) {
  335. unregister_netdevice(ndev);
  336. free_netdev(ndev);
  337. g_monitor.mon_if[i].real_ndev = NULL;
  338. g_monitor.mon_if[i].mon_ndev = NULL;
  339. }
  340. }
  341. g_monitor.monitor_state = MONITOR_STATE_DEINIT;
  342. }
  343. mutex_unlock(&g_monitor.lock);
  344. return 0;
  345. }