wl_linux_mon.c 11 KB

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