ie.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /*
  2. * Marvell Wireless LAN device driver: management IE handling- setting and
  3. * deleting IE.
  4. *
  5. * Copyright (C) 2012, Marvell International Ltd.
  6. *
  7. * This software file (the "File") is distributed by Marvell International
  8. * Ltd. under the terms of the GNU General Public License Version 2, June 1991
  9. * (the "License"). You may use, redistribute and/or modify this File in
  10. * accordance with the terms and conditions of the License, a copy of which
  11. * is available by writing to the Free Software Foundation, Inc.,
  12. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or on the
  13. * worldwide web at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
  14. *
  15. * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
  16. * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
  17. * ARE EXPRESSLY DISCLAIMED. The License provides additional details about
  18. * this warranty disclaimer.
  19. */
  20. #include "main.h"
  21. /* This function checks if current IE index is used by any on other interface.
  22. * Return: -1: yes, current IE index is used by someone else.
  23. * 0: no, current IE index is NOT used by other interface.
  24. */
  25. static int
  26. mwifiex_ie_index_used_by_other_intf(struct mwifiex_private *priv, u16 idx)
  27. {
  28. int i;
  29. struct mwifiex_adapter *adapter = priv->adapter;
  30. struct mwifiex_ie *ie;
  31. for (i = 0; i < adapter->priv_num; i++) {
  32. if (adapter->priv[i] != priv) {
  33. ie = &adapter->priv[i]->mgmt_ie[idx];
  34. if (ie->mgmt_subtype_mask && ie->ie_length)
  35. return -1;
  36. }
  37. }
  38. return 0;
  39. }
  40. /* Get unused IE index. This index will be used for setting new IE */
  41. static int
  42. mwifiex_ie_get_autoidx(struct mwifiex_private *priv, u16 subtype_mask,
  43. struct mwifiex_ie *ie, u16 *index)
  44. {
  45. u16 mask, len, i;
  46. for (i = 0; i < priv->adapter->max_mgmt_ie_index; i++) {
  47. mask = le16_to_cpu(priv->mgmt_ie[i].mgmt_subtype_mask);
  48. len = le16_to_cpu(ie->ie_length);
  49. if (mask == MWIFIEX_AUTO_IDX_MASK)
  50. continue;
  51. if (mask == subtype_mask) {
  52. if (len > IEEE_MAX_IE_SIZE)
  53. continue;
  54. *index = i;
  55. return 0;
  56. }
  57. if (!priv->mgmt_ie[i].ie_length) {
  58. if (mwifiex_ie_index_used_by_other_intf(priv, i))
  59. continue;
  60. *index = i;
  61. return 0;
  62. }
  63. }
  64. return -1;
  65. }
  66. /* This function prepares IE data buffer for command to be sent to FW */
  67. static int
  68. mwifiex_update_autoindex_ies(struct mwifiex_private *priv,
  69. struct mwifiex_ie_list *ie_list)
  70. {
  71. u16 travel_len, index, mask;
  72. s16 input_len;
  73. struct mwifiex_ie *ie;
  74. u8 *tmp;
  75. input_len = le16_to_cpu(ie_list->len);
  76. travel_len = sizeof(struct host_cmd_tlv);
  77. ie_list->len = 0;
  78. while (input_len > 0) {
  79. ie = (struct mwifiex_ie *)(((u8 *)ie_list) + travel_len);
  80. input_len -= le16_to_cpu(ie->ie_length) + MWIFIEX_IE_HDR_SIZE;
  81. travel_len += le16_to_cpu(ie->ie_length) + MWIFIEX_IE_HDR_SIZE;
  82. index = le16_to_cpu(ie->ie_index);
  83. mask = le16_to_cpu(ie->mgmt_subtype_mask);
  84. if (index == MWIFIEX_AUTO_IDX_MASK) {
  85. /* automatic addition */
  86. if (mwifiex_ie_get_autoidx(priv, mask, ie, &index))
  87. return -1;
  88. if (index == MWIFIEX_AUTO_IDX_MASK)
  89. return -1;
  90. tmp = (u8 *)&priv->mgmt_ie[index].ie_buffer;
  91. memcpy(tmp, &ie->ie_buffer, le16_to_cpu(ie->ie_length));
  92. priv->mgmt_ie[index].ie_length = ie->ie_length;
  93. priv->mgmt_ie[index].ie_index = cpu_to_le16(index);
  94. priv->mgmt_ie[index].mgmt_subtype_mask =
  95. cpu_to_le16(mask);
  96. ie->ie_index = cpu_to_le16(index);
  97. ie->ie_length = priv->mgmt_ie[index].ie_length;
  98. memcpy(&ie->ie_buffer, &priv->mgmt_ie[index].ie_buffer,
  99. le16_to_cpu(priv->mgmt_ie[index].ie_length));
  100. } else {
  101. if (mask != MWIFIEX_DELETE_MASK)
  102. return -1;
  103. /*
  104. * Check if this index is being used on any
  105. * other interface.
  106. */
  107. if (mwifiex_ie_index_used_by_other_intf(priv, index))
  108. return -1;
  109. ie->ie_length = 0;
  110. memcpy(&priv->mgmt_ie[index], ie,
  111. sizeof(struct mwifiex_ie));
  112. }
  113. le16_add_cpu(&ie_list->len,
  114. le16_to_cpu(priv->mgmt_ie[index].ie_length) +
  115. MWIFIEX_IE_HDR_SIZE);
  116. }
  117. if (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_UAP)
  118. return mwifiex_send_cmd_async(priv, HostCmd_CMD_UAP_SYS_CONFIG,
  119. HostCmd_ACT_GEN_SET,
  120. UAP_CUSTOM_IE_I, ie_list);
  121. return 0;
  122. }
  123. /* Copy individual custom IEs for beacon, probe response and assoc response
  124. * and prepare single structure for IE setting.
  125. * This function also updates allocated IE indices from driver.
  126. */
  127. static int
  128. mwifiex_update_uap_custom_ie(struct mwifiex_private *priv,
  129. struct mwifiex_ie *beacon_ie, u16 *beacon_idx,
  130. struct mwifiex_ie *pr_ie, u16 *probe_idx,
  131. struct mwifiex_ie *ar_ie, u16 *assoc_idx)
  132. {
  133. struct mwifiex_ie_list *ap_custom_ie;
  134. u8 *pos;
  135. u16 len;
  136. int ret;
  137. ap_custom_ie = kzalloc(sizeof(struct mwifiex_ie), GFP_KERNEL);
  138. if (!ap_custom_ie)
  139. return -ENOMEM;
  140. ap_custom_ie->type = cpu_to_le16(TLV_TYPE_MGMT_IE);
  141. pos = (u8 *)ap_custom_ie->ie_list;
  142. if (beacon_ie) {
  143. len = sizeof(struct mwifiex_ie) - IEEE_MAX_IE_SIZE +
  144. le16_to_cpu(beacon_ie->ie_length);
  145. memcpy(pos, beacon_ie, len);
  146. pos += len;
  147. le16_add_cpu(&ap_custom_ie->len, len);
  148. }
  149. if (pr_ie) {
  150. len = sizeof(struct mwifiex_ie) - IEEE_MAX_IE_SIZE +
  151. le16_to_cpu(pr_ie->ie_length);
  152. memcpy(pos, pr_ie, len);
  153. pos += len;
  154. le16_add_cpu(&ap_custom_ie->len, len);
  155. }
  156. if (ar_ie) {
  157. len = sizeof(struct mwifiex_ie) - IEEE_MAX_IE_SIZE +
  158. le16_to_cpu(ar_ie->ie_length);
  159. memcpy(pos, ar_ie, len);
  160. pos += len;
  161. le16_add_cpu(&ap_custom_ie->len, len);
  162. }
  163. ret = mwifiex_update_autoindex_ies(priv, ap_custom_ie);
  164. pos = (u8 *)(&ap_custom_ie->ie_list[0].ie_index);
  165. if (beacon_ie && *beacon_idx == MWIFIEX_AUTO_IDX_MASK) {
  166. /* save beacon ie index after auto-indexing */
  167. *beacon_idx = le16_to_cpu(ap_custom_ie->ie_list[0].ie_index);
  168. len = sizeof(*beacon_ie) - IEEE_MAX_IE_SIZE +
  169. le16_to_cpu(beacon_ie->ie_length);
  170. pos += len;
  171. }
  172. if (pr_ie && le16_to_cpu(pr_ie->ie_index) == MWIFIEX_AUTO_IDX_MASK) {
  173. /* save probe resp ie index after auto-indexing */
  174. *probe_idx = *((u16 *)pos);
  175. len = sizeof(*pr_ie) - IEEE_MAX_IE_SIZE +
  176. le16_to_cpu(pr_ie->ie_length);
  177. pos += len;
  178. }
  179. if (ar_ie && le16_to_cpu(ar_ie->ie_index) == MWIFIEX_AUTO_IDX_MASK)
  180. /* save assoc resp ie index after auto-indexing */
  181. *assoc_idx = *((u16 *)pos);
  182. kfree(ap_custom_ie);
  183. return ret;
  184. }
  185. /* This function checks if WPS IE is present in passed buffer and copies it to
  186. * mwifiex_ie structure.
  187. * Function takes pointer to struct mwifiex_ie pointer as argument.
  188. * If WPS IE is present memory is allocated for mwifiex_ie pointer and filled
  189. * in with WPS IE. Caller should take care of freeing this memory.
  190. */
  191. static int mwifiex_update_wps_ie(const u8 *ies, int ies_len,
  192. struct mwifiex_ie **ie_ptr, u16 mask)
  193. {
  194. struct ieee_types_header *wps_ie;
  195. struct mwifiex_ie *ie = NULL;
  196. const u8 *vendor_ie;
  197. vendor_ie = cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
  198. WLAN_OUI_TYPE_MICROSOFT_WPS,
  199. ies, ies_len);
  200. if (vendor_ie) {
  201. ie = kmalloc(sizeof(struct mwifiex_ie), GFP_KERNEL);
  202. if (!ie)
  203. return -ENOMEM;
  204. wps_ie = (struct ieee_types_header *)vendor_ie;
  205. memcpy(ie->ie_buffer, wps_ie, wps_ie->len + 2);
  206. ie->ie_length = cpu_to_le16(wps_ie->len + 2);
  207. ie->mgmt_subtype_mask = cpu_to_le16(mask);
  208. ie->ie_index = cpu_to_le16(MWIFIEX_AUTO_IDX_MASK);
  209. }
  210. *ie_ptr = ie;
  211. return 0;
  212. }
  213. /* This function parses beacon IEs, probe response IEs, association response IEs
  214. * from cfg80211_ap_settings->beacon and sets these IE to FW.
  215. */
  216. static int mwifiex_set_mgmt_beacon_data_ies(struct mwifiex_private *priv,
  217. struct cfg80211_beacon_data *data)
  218. {
  219. struct mwifiex_ie *beacon_ie = NULL, *pr_ie = NULL, *ar_ie = NULL;
  220. u16 beacon_idx = MWIFIEX_AUTO_IDX_MASK, pr_idx = MWIFIEX_AUTO_IDX_MASK;
  221. u16 ar_idx = MWIFIEX_AUTO_IDX_MASK;
  222. int ret = 0;
  223. if (data->beacon_ies && data->beacon_ies_len)
  224. mwifiex_update_wps_ie(data->beacon_ies, data->beacon_ies_len,
  225. &beacon_ie, MGMT_MASK_BEACON);
  226. if (data->proberesp_ies && data->proberesp_ies_len)
  227. mwifiex_update_wps_ie(data->proberesp_ies,
  228. data->proberesp_ies_len, &pr_ie,
  229. MGMT_MASK_PROBE_RESP);
  230. if (data->assocresp_ies && data->assocresp_ies_len)
  231. mwifiex_update_wps_ie(data->assocresp_ies,
  232. data->assocresp_ies_len, &ar_ie,
  233. MGMT_MASK_ASSOC_RESP |
  234. MGMT_MASK_REASSOC_RESP);
  235. if (beacon_ie || pr_ie || ar_ie) {
  236. ret = mwifiex_update_uap_custom_ie(priv, beacon_ie,
  237. &beacon_idx, pr_ie,
  238. &pr_idx, ar_ie, &ar_idx);
  239. if (ret)
  240. goto done;
  241. }
  242. priv->beacon_idx = beacon_idx;
  243. priv->proberesp_idx = pr_idx;
  244. priv->assocresp_idx = ar_idx;
  245. done:
  246. kfree(beacon_ie);
  247. kfree(pr_ie);
  248. kfree(ar_ie);
  249. return ret;
  250. }
  251. /* This function parses different IEs-tail IEs, beacon IEs, probe response IEs,
  252. * association response IEs from cfg80211_ap_settings function and sets these IE
  253. * to FW.
  254. */
  255. int mwifiex_set_mgmt_ies(struct mwifiex_private *priv,
  256. struct cfg80211_beacon_data *info)
  257. {
  258. struct mwifiex_ie *gen_ie;
  259. struct ieee_types_header *rsn_ie, *wpa_ie = NULL;
  260. u16 rsn_idx = MWIFIEX_AUTO_IDX_MASK, ie_len = 0;
  261. const u8 *vendor_ie;
  262. if (info->tail && info->tail_len) {
  263. gen_ie = kzalloc(sizeof(struct mwifiex_ie), GFP_KERNEL);
  264. if (!gen_ie)
  265. return -ENOMEM;
  266. gen_ie->ie_index = cpu_to_le16(rsn_idx);
  267. gen_ie->mgmt_subtype_mask = cpu_to_le16(MGMT_MASK_BEACON |
  268. MGMT_MASK_PROBE_RESP |
  269. MGMT_MASK_ASSOC_RESP);
  270. rsn_ie = (void *)cfg80211_find_ie(WLAN_EID_RSN,
  271. info->tail, info->tail_len);
  272. if (rsn_ie) {
  273. memcpy(gen_ie->ie_buffer, rsn_ie, rsn_ie->len + 2);
  274. ie_len = rsn_ie->len + 2;
  275. gen_ie->ie_length = cpu_to_le16(ie_len);
  276. }
  277. vendor_ie = cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
  278. WLAN_OUI_TYPE_MICROSOFT_WPA,
  279. info->tail,
  280. info->tail_len);
  281. if (vendor_ie) {
  282. wpa_ie = (struct ieee_types_header *)vendor_ie;
  283. memcpy(gen_ie->ie_buffer + ie_len,
  284. wpa_ie, wpa_ie->len + 2);
  285. ie_len += wpa_ie->len + 2;
  286. gen_ie->ie_length = cpu_to_le16(ie_len);
  287. }
  288. if (rsn_ie || wpa_ie) {
  289. if (mwifiex_update_uap_custom_ie(priv, gen_ie, &rsn_idx,
  290. NULL, NULL,
  291. NULL, NULL)) {
  292. kfree(gen_ie);
  293. return -1;
  294. }
  295. priv->rsn_idx = rsn_idx;
  296. }
  297. kfree(gen_ie);
  298. }
  299. return mwifiex_set_mgmt_beacon_data_ies(priv, info);
  300. }
  301. /* This function removes management IE set */
  302. int mwifiex_del_mgmt_ies(struct mwifiex_private *priv)
  303. {
  304. struct mwifiex_ie *beacon_ie = NULL, *pr_ie = NULL;
  305. struct mwifiex_ie *ar_ie = NULL, *rsn_ie = NULL;
  306. int ret = 0;
  307. if (priv->rsn_idx != MWIFIEX_AUTO_IDX_MASK) {
  308. rsn_ie = kmalloc(sizeof(struct mwifiex_ie), GFP_KERNEL);
  309. if (!rsn_ie)
  310. return -ENOMEM;
  311. rsn_ie->ie_index = cpu_to_le16(priv->rsn_idx);
  312. rsn_ie->mgmt_subtype_mask = cpu_to_le16(MWIFIEX_DELETE_MASK);
  313. rsn_ie->ie_length = 0;
  314. if (mwifiex_update_uap_custom_ie(priv, rsn_ie, &priv->rsn_idx,
  315. NULL, &priv->proberesp_idx,
  316. NULL, &priv->assocresp_idx)) {
  317. ret = -1;
  318. goto done;
  319. }
  320. priv->rsn_idx = MWIFIEX_AUTO_IDX_MASK;
  321. }
  322. if (priv->beacon_idx != MWIFIEX_AUTO_IDX_MASK) {
  323. beacon_ie = kmalloc(sizeof(struct mwifiex_ie), GFP_KERNEL);
  324. if (!beacon_ie) {
  325. ret = -ENOMEM;
  326. goto done;
  327. }
  328. beacon_ie->ie_index = cpu_to_le16(priv->beacon_idx);
  329. beacon_ie->mgmt_subtype_mask = cpu_to_le16(MWIFIEX_DELETE_MASK);
  330. beacon_ie->ie_length = 0;
  331. }
  332. if (priv->proberesp_idx != MWIFIEX_AUTO_IDX_MASK) {
  333. pr_ie = kmalloc(sizeof(struct mwifiex_ie), GFP_KERNEL);
  334. if (!pr_ie) {
  335. ret = -ENOMEM;
  336. goto done;
  337. }
  338. pr_ie->ie_index = cpu_to_le16(priv->proberesp_idx);
  339. pr_ie->mgmt_subtype_mask = cpu_to_le16(MWIFIEX_DELETE_MASK);
  340. pr_ie->ie_length = 0;
  341. }
  342. if (priv->assocresp_idx != MWIFIEX_AUTO_IDX_MASK) {
  343. ar_ie = kmalloc(sizeof(struct mwifiex_ie), GFP_KERNEL);
  344. if (!ar_ie) {
  345. ret = -ENOMEM;
  346. goto done;
  347. }
  348. ar_ie->ie_index = cpu_to_le16(priv->assocresp_idx);
  349. ar_ie->mgmt_subtype_mask = cpu_to_le16(MWIFIEX_DELETE_MASK);
  350. ar_ie->ie_length = 0;
  351. }
  352. if (beacon_ie || pr_ie || ar_ie)
  353. ret = mwifiex_update_uap_custom_ie(priv,
  354. beacon_ie, &priv->beacon_idx,
  355. pr_ie, &priv->proberesp_idx,
  356. ar_ie, &priv->assocresp_idx);
  357. done:
  358. kfree(beacon_ie);
  359. kfree(pr_ie);
  360. kfree(ar_ie);
  361. kfree(rsn_ie);
  362. return ret;
  363. }