PageRenderTime 57ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/net/wireless/bcmdhd/src/wl/sys/wl_linux_mon.c

https://bitbucket.org/Tim1928/samsung
C | 425 lines | 308 code | 58 blank | 59 comment | 59 complexity | 59c3dc8445068234a2ec88e6cc0cf042 MD5 | raw file
  1. /*
  2. * Broadcom Dongle Host Driver (DHD), Linux monitor network interface
  3. *
  4. * Copyright (C) 1999-2011, 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: dhd_linux_mon.c 280623 2011-08-30 14:49:39Z $
  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_DEBUG 0
  59. #if MON_DEBUG
  60. #define MON_PRINT(format, ...) printk("DHD-MON: %s " format, __func__, ##__VA_ARGS__)
  61. #define MON_TRACE MON_PRINT
  62. #else
  63. #define MON_PRINT(format, ...)
  64. #define MON_TRACE MON_PRINT
  65. #endif
  66. typedef struct monitor_interface {
  67. int radiotap_enabled;
  68. struct net_device* real_ndev; /* The real interface that the monitor is on */
  69. struct net_device* mon_ndev;
  70. } monitor_interface;
  71. typedef struct dhd_linux_monitor {
  72. void *dhd_pub;
  73. monitor_states_t monitor_state;
  74. monitor_interface mon_if[DHD_MAX_IFS];
  75. struct mutex lock; /* lock to protect mon_if */
  76. } dhd_linux_monitor_t;
  77. static dhd_linux_monitor_t g_monitor;
  78. static struct net_device* lookup_real_netdev(char *name);
  79. static monitor_interface* ndev_to_monif(struct net_device *ndev);
  80. static int dhd_mon_if_open(struct net_device *ndev);
  81. static int dhd_mon_if_stop(struct net_device *ndev);
  82. static int dhd_mon_if_subif_start_xmit(struct sk_buff *skb, struct net_device *ndev);
  83. static void dhd_mon_if_set_multicast_list(struct net_device *ndev);
  84. static int dhd_mon_if_change_mac(struct net_device *ndev, void *addr);
  85. static const struct net_device_ops dhd_mon_if_ops = {
  86. .ndo_open = dhd_mon_if_open,
  87. .ndo_stop = dhd_mon_if_stop,
  88. .ndo_start_xmit = dhd_mon_if_subif_start_xmit,
  89. .ndo_set_rx_mode = dhd_mon_if_set_multicast_list,
  90. .ndo_set_mac_address = dhd_mon_if_change_mac,
  91. };
  92. /**
  93. * Local static function defintions
  94. */
  95. /* Look up dhd's net device table to find a match (e.g. interface "eth0" is a match for "mon.eth0"
  96. * "p2p-eth0-0" is a match for "mon.p2p-eth0-0")
  97. */
  98. static struct net_device* lookup_real_netdev(char *name)
  99. {
  100. struct net_device *ndev_found = NULL;
  101. int i;
  102. int len = 0;
  103. int last_name_len = 0;
  104. struct net_device *ndev;
  105. /* We need to find interface "p2p-p2p-0" corresponding to monitor interface "mon-p2p-0",
  106. * Once mon iface name reaches IFNAMSIZ, it is reset to p2p0-0 and corresponding mon
  107. * iface would be mon-p2p0-0.
  108. */
  109. for (i = 0; i < DHD_MAX_IFS; i++) {
  110. ndev = dhd_idx2net(g_monitor.dhd_pub, i);
  111. /* Skip "p2p" and look for "-p2p0-x" in monitor interface name. If it
  112. * it matches, then this netdev is the corresponding real_netdev.
  113. */
  114. if (ndev && strstr(ndev->name, "p2p-p2p0")) {
  115. len = strlen("p2p");
  116. } else {
  117. /* if p2p- is not present, then the IFNAMSIZ have reached and name
  118. * would have got reset. In this casse,look for p2p0-x in mon-p2p0-x
  119. */
  120. len = 0;
  121. }
  122. if (ndev && strstr(name, (ndev->name + len))) {
  123. if (strlen(ndev->name) > last_name_len) {
  124. ndev_found = ndev;
  125. last_name_len = strlen(ndev->name);
  126. }
  127. }
  128. }
  129. return ndev_found;
  130. }
  131. static monitor_interface* ndev_to_monif(struct net_device *ndev)
  132. {
  133. int i;
  134. for (i = 0; i < DHD_MAX_IFS; i++) {
  135. if (g_monitor.mon_if[i].mon_ndev == ndev)
  136. return &g_monitor.mon_if[i];
  137. }
  138. return NULL;
  139. }
  140. static int dhd_mon_if_open(struct net_device *ndev)
  141. {
  142. int ret = 0;
  143. MON_PRINT("enter\n");
  144. return ret;
  145. }
  146. static int dhd_mon_if_stop(struct net_device *ndev)
  147. {
  148. int ret = 0;
  149. MON_PRINT("enter\n");
  150. return ret;
  151. }
  152. static int dhd_mon_if_subif_start_xmit(struct sk_buff *skb, struct net_device *ndev)
  153. {
  154. int ret = 0;
  155. int rtap_len;
  156. int qos_len = 0;
  157. int dot11_hdr_len = 24;
  158. int snap_len = 6;
  159. unsigned char *pdata;
  160. unsigned short frame_ctl;
  161. unsigned char src_mac_addr[6];
  162. unsigned char dst_mac_addr[6];
  163. struct ieee80211_hdr *dot11_hdr;
  164. struct ieee80211_radiotap_header *rtap_hdr;
  165. monitor_interface* mon_if;
  166. MON_PRINT("enter\n");
  167. mon_if = ndev_to_monif(ndev);
  168. if (mon_if == NULL || mon_if->real_ndev == NULL) {
  169. MON_PRINT(" cannot find matched net dev, skip the packet\n");
  170. goto fail;
  171. }
  172. if (unlikely(skb->len < sizeof(struct ieee80211_radiotap_header)))
  173. goto fail;
  174. rtap_hdr = (struct ieee80211_radiotap_header *)skb->data;
  175. if (unlikely(rtap_hdr->it_version))
  176. goto fail;
  177. rtap_len = ieee80211_get_radiotap_len(skb->data);
  178. if (unlikely(skb->len < rtap_len))
  179. goto fail;
  180. MON_PRINT("radiotap len (should be 14): %d\n", rtap_len);
  181. /* Skip the ratio tap header */
  182. skb_pull(skb, rtap_len);
  183. dot11_hdr = (struct ieee80211_hdr *)skb->data;
  184. frame_ctl = le16_to_cpu(dot11_hdr->frame_control);
  185. /* Check if the QoS bit is set */
  186. if ((frame_ctl & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) {
  187. /* Check if this ia a Wireless Distribution System (WDS) frame
  188. * which has 4 MAC addresses
  189. */
  190. if (dot11_hdr->frame_control & 0x0080)
  191. qos_len = 2;
  192. if ((dot11_hdr->frame_control & 0x0300) == 0x0300)
  193. dot11_hdr_len += 6;
  194. memcpy(dst_mac_addr, dot11_hdr->addr1, sizeof(dst_mac_addr));
  195. memcpy(src_mac_addr, dot11_hdr->addr2, sizeof(src_mac_addr));
  196. /* Skip the 802.11 header, QoS (if any) and SNAP, but leave spaces for
  197. * for two MAC addresses
  198. */
  199. skb_pull(skb, dot11_hdr_len + qos_len + snap_len - sizeof(src_mac_addr) * 2);
  200. pdata = (unsigned char*)skb->data;
  201. memcpy(pdata, dst_mac_addr, sizeof(dst_mac_addr));
  202. memcpy(pdata + sizeof(dst_mac_addr), src_mac_addr, sizeof(src_mac_addr));
  203. PKTSETPRIO(skb, 0);
  204. MON_PRINT("if name: %s, matched if name %s\n", ndev->name, mon_if->real_ndev->name);
  205. /* Use the real net device to transmit the packet */
  206. ret = dhd_start_xmit(skb, mon_if->real_ndev);
  207. return ret;
  208. }
  209. fail:
  210. dev_kfree_skb(skb);
  211. return 0;
  212. }
  213. static void dhd_mon_if_set_multicast_list(struct net_device *ndev)
  214. {
  215. monitor_interface* mon_if;
  216. mon_if = ndev_to_monif(ndev);
  217. if (mon_if == NULL || mon_if->real_ndev == NULL) {
  218. MON_PRINT(" cannot find matched net dev, skip the packet\n");
  219. } else {
  220. MON_PRINT("enter, if name: %s, matched if name %s\n",
  221. ndev->name, mon_if->real_ndev->name);
  222. }
  223. }
  224. static int dhd_mon_if_change_mac(struct net_device *ndev, void *addr)
  225. {
  226. int ret = 0;
  227. monitor_interface* mon_if;
  228. mon_if = ndev_to_monif(ndev);
  229. if (mon_if == NULL || mon_if->real_ndev == NULL) {
  230. MON_PRINT(" cannot find matched net dev, skip the packet\n");
  231. } else {
  232. MON_PRINT("enter, if name: %s, matched if name %s\n",
  233. ndev->name, mon_if->real_ndev->name);
  234. }
  235. return ret;
  236. }
  237. /**
  238. * Global function definitions (declared in dhd_linux_mon.h)
  239. */
  240. int dhd_add_monitor(char *name, struct net_device **new_ndev)
  241. {
  242. int i;
  243. int idx = -1;
  244. int ret = 0;
  245. struct net_device* ndev = NULL;
  246. dhd_linux_monitor_t **dhd_mon;
  247. mutex_lock(&g_monitor.lock);
  248. MON_TRACE("enter, if name: %s\n", name);
  249. if (!name || !new_ndev) {
  250. MON_PRINT("invalid parameters\n");
  251. ret = -EINVAL;
  252. goto out;
  253. }
  254. /*
  255. * Find a vacancy
  256. */
  257. for (i = 0; i < DHD_MAX_IFS; i++)
  258. if (g_monitor.mon_if[i].mon_ndev == NULL) {
  259. idx = i;
  260. break;
  261. }
  262. if (idx == -1) {
  263. MON_PRINT("exceeds maximum interfaces\n");
  264. ret = -EFAULT;
  265. goto out;
  266. }
  267. ndev = alloc_etherdev(sizeof(dhd_linux_monitor_t*));
  268. if (!ndev) {
  269. MON_PRINT("failed to allocate memory\n");
  270. ret = -ENOMEM;
  271. goto out;
  272. }
  273. ndev->type = ARPHRD_IEEE80211_RADIOTAP;
  274. strncpy(ndev->name, name, IFNAMSIZ);
  275. ndev->name[IFNAMSIZ - 1] = 0;
  276. ndev->netdev_ops = &dhd_mon_if_ops;
  277. ret = register_netdevice(ndev);
  278. if (ret) {
  279. MON_PRINT(" register_netdevice failed (%d)\n", ret);
  280. goto out;
  281. }
  282. *new_ndev = ndev;
  283. g_monitor.mon_if[idx].radiotap_enabled = TRUE;
  284. g_monitor.mon_if[idx].mon_ndev = ndev;
  285. g_monitor.mon_if[idx].real_ndev = lookup_real_netdev(name);
  286. dhd_mon = (dhd_linux_monitor_t **)netdev_priv(ndev);
  287. *dhd_mon = &g_monitor;
  288. g_monitor.monitor_state = MONITOR_STATE_INTERFACE_ADDED;
  289. MON_PRINT("net device returned: 0x%p\n", ndev);
  290. MON_PRINT("found a matched net device, name %s\n", g_monitor.mon_if[idx].real_ndev->name);
  291. out:
  292. if (ret && ndev)
  293. free_netdev(ndev);
  294. mutex_unlock(&g_monitor.lock);
  295. return ret;
  296. }
  297. int dhd_del_monitor(struct net_device *ndev)
  298. {
  299. int i;
  300. bool rollback_lock = false;
  301. if (!ndev)
  302. return -EINVAL;
  303. mutex_lock(&g_monitor.lock);
  304. for (i = 0; i < DHD_MAX_IFS; i++) {
  305. if (g_monitor.mon_if[i].mon_ndev == ndev ||
  306. g_monitor.mon_if[i].real_ndev == ndev) {
  307. g_monitor.mon_if[i].real_ndev = NULL;
  308. if (rtnl_is_locked()) {
  309. rtnl_unlock();
  310. rollback_lock = true;
  311. }
  312. unregister_netdev(g_monitor.mon_if[i].mon_ndev);
  313. free_netdev(g_monitor.mon_if[i].mon_ndev);
  314. g_monitor.mon_if[i].mon_ndev = NULL;
  315. g_monitor.monitor_state = MONITOR_STATE_INTERFACE_DELETED;
  316. break;
  317. }
  318. }
  319. if (rollback_lock) {
  320. rtnl_lock();
  321. rollback_lock = false;
  322. }
  323. if (g_monitor.monitor_state !=
  324. MONITOR_STATE_INTERFACE_DELETED)
  325. MON_PRINT("interface not found in monitor IF array, is this a monitor IF? 0x%p\n",
  326. ndev);
  327. mutex_unlock(&g_monitor.lock);
  328. return 0;
  329. }
  330. int dhd_monitor_init(void *dhd_pub)
  331. {
  332. if (g_monitor.monitor_state == MONITOR_STATE_DEINIT) {
  333. g_monitor.dhd_pub = dhd_pub;
  334. mutex_init(&g_monitor.lock);
  335. g_monitor.monitor_state = MONITOR_STATE_INIT;
  336. }
  337. return 0;
  338. }
  339. int dhd_monitor_uninit(void)
  340. {
  341. int i;
  342. struct net_device *ndev;
  343. bool rollback_lock = false;
  344. mutex_lock(&g_monitor.lock);
  345. if (g_monitor.monitor_state != MONITOR_STATE_DEINIT) {
  346. for (i = 0; i < DHD_MAX_IFS; i++) {
  347. ndev = g_monitor.mon_if[i].mon_ndev;
  348. if (ndev) {
  349. if (rtnl_is_locked()) {
  350. rtnl_unlock();
  351. rollback_lock = true;
  352. }
  353. unregister_netdev(ndev);
  354. free_netdev(ndev);
  355. g_monitor.mon_if[i].real_ndev = NULL;
  356. g_monitor.mon_if[i].mon_ndev = NULL;
  357. if (rollback_lock) {
  358. rtnl_lock();
  359. rollback_lock = false;
  360. }
  361. }
  362. }
  363. g_monitor.monitor_state = MONITOR_STATE_DEINIT;
  364. }
  365. mutex_unlock(&g_monitor.lock);
  366. return 0;
  367. }