PageRenderTime 65ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/staging/rtl8723au/core/rtw_ieee80211.c

http://github.com/mirrors/linux-2.6
C | 855 lines | 627 code | 138 blank | 90 comment | 193 complexity | c62cd0adf4148e5c04e3f7a898a959ee MD5 | raw file
Possible License(s): LGPL-2.0, AGPL-1.0, GPL-2.0
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2007 - 2011 Realtek Corporation. All rights reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of version 2 of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. ******************************************************************************/
  15. #define _IEEE80211_C
  16. #include <drv_types.h>
  17. #include <linux/ieee80211.h>
  18. #include <ieee80211.h>
  19. #include <wifi.h>
  20. #include <osdep_service.h>
  21. #include <wlan_bssdef.h>
  22. u8 RTW_WPA_OUI23A_TYPE[] = { 0x00, 0x50, 0xf2, 1 };
  23. u16 RTW_WPA_VERSION23A = 1;
  24. u8 WPA_AUTH_KEY_MGMT_NONE23A[] = { 0x00, 0x50, 0xf2, 0 };
  25. u8 WPA_AUTH_KEY_MGMT_UNSPEC_802_1X23A[] = { 0x00, 0x50, 0xf2, 1 };
  26. u8 WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X23A[] = { 0x00, 0x50, 0xf2, 2 };
  27. u8 WPA_CIPHER_SUITE_NONE23A[] = { 0x00, 0x50, 0xf2, 0 };
  28. u8 WPA_CIPHER_SUITE_WEP4023A[] = { 0x00, 0x50, 0xf2, 1 };
  29. u8 WPA_CIPHER_SUITE_TKIP23A[] = { 0x00, 0x50, 0xf2, 2 };
  30. u8 WPA_CIPHER_SUITE_WRAP23A[] = { 0x00, 0x50, 0xf2, 3 };
  31. u8 WPA_CIPHER_SUITE_CCMP23A[] = { 0x00, 0x50, 0xf2, 4 };
  32. u8 WPA_CIPHER_SUITE_WEP10423A[] = { 0x00, 0x50, 0xf2, 5 };
  33. u8 RSN_AUTH_KEY_MGMT_UNSPEC_802_1X23A[] = { 0x00, 0x0f, 0xac, 1 };
  34. u8 RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X23A[] = { 0x00, 0x0f, 0xac, 2 };
  35. u8 RSN_CIPHER_SUITE_NONE23A[] = { 0x00, 0x0f, 0xac, 0 };
  36. u8 RSN_CIPHER_SUITE_WEP4023A[] = { 0x00, 0x0f, 0xac, 1 };
  37. u8 RSN_CIPHER_SUITE_TKIP23A[] = { 0x00, 0x0f, 0xac, 2 };
  38. u8 RSN_CIPHER_SUITE_WRAP23A[] = { 0x00, 0x0f, 0xac, 3 };
  39. u8 RSN_CIPHER_SUITE_CCMP23A[] = { 0x00, 0x0f, 0xac, 4 };
  40. u8 RSN_CIPHER_SUITE_WEP10423A[] = { 0x00, 0x0f, 0xac, 5 };
  41. /* */
  42. /* for adhoc-master to generate ie and provide supported-rate to fw */
  43. /* */
  44. static u8 WIFI_CCKRATES[] = {
  45. IEEE80211_CCK_RATE_1MB | IEEE80211_BASIC_RATE_MASK,
  46. IEEE80211_CCK_RATE_2MB | IEEE80211_BASIC_RATE_MASK,
  47. IEEE80211_CCK_RATE_5MB | IEEE80211_BASIC_RATE_MASK,
  48. IEEE80211_CCK_RATE_11MB | IEEE80211_BASIC_RATE_MASK
  49. };
  50. static u8 WIFI_OFDMRATES[] = {
  51. IEEE80211_OFDM_RATE_6MB,
  52. IEEE80211_OFDM_RATE_9MB,
  53. IEEE80211_OFDM_RATE_12MB,
  54. IEEE80211_OFDM_RATE_18MB,
  55. IEEE80211_OFDM_RATE_24MB,
  56. IEEE80211_OFDM_RATE_36MB,
  57. IEEE80211_OFDM_RATE_48MB,
  58. IEEE80211_OFDM_RATE_54MB
  59. };
  60. int rtw_get_bit_value_from_ieee_value23a(u8 val)
  61. {
  62. unsigned char dot11_rate_table[]=
  63. {2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108, 0};
  64. int i = 0;
  65. while (dot11_rate_table[i] != 0) {
  66. if (dot11_rate_table[i] == val)
  67. return BIT(i);
  68. i++;
  69. }
  70. return 0;
  71. }
  72. static bool rtw_is_cckrates_included(u8 *rate)
  73. {
  74. u32 i = 0;
  75. while (rate[i]) {
  76. if ((rate[i] & 0x7f) == 2 || (rate[i] & 0x7f) == 4 ||
  77. (rate[i] & 0x7f) == 11 || (rate[i] & 0x7f) == 22)
  78. return true;
  79. i++;
  80. }
  81. return false;
  82. }
  83. static bool rtw_is_cckratesonly_included(u8 *rate)
  84. {
  85. u32 i = 0;
  86. while (rate[i]) {
  87. if ((rate[i] & 0x7f) != 2 && (rate[i] & 0x7f) != 4 &&
  88. (rate[i] & 0x7f) != 11 && (rate[i] & 0x7f) != 22)
  89. return false;
  90. i++;
  91. }
  92. return true;
  93. }
  94. int rtw_check_network_type23a(unsigned char *rate, int ratelen, int channel)
  95. {
  96. if (channel > 14) {
  97. if (rtw_is_cckrates_included(rate))
  98. return WIRELESS_INVALID;
  99. else
  100. return WIRELESS_11A;
  101. } else { /* could be pure B, pure G, or B/G */
  102. if (rtw_is_cckratesonly_included(rate))
  103. return WIRELESS_11B;
  104. else if (rtw_is_cckrates_included(rate))
  105. return WIRELESS_11BG;
  106. else
  107. return WIRELESS_11G;
  108. }
  109. }
  110. /* rtw_set_ie23a will update frame length */
  111. u8 *rtw_set_ie23a(u8 *pbuf, int index, uint len, const u8 *source, uint *frlen)
  112. {
  113. *pbuf = (u8)index;
  114. *(pbuf + 1) = (u8)len;
  115. if (len > 0)
  116. memcpy((void *)(pbuf + 2), (void *)source, len);
  117. *frlen = *frlen + (len + 2);
  118. return pbuf + len + 2;
  119. }
  120. inline u8 *rtw_set_ie23a_ch_switch (u8 *buf, u32 *buf_len, u8 ch_switch_mode,
  121. u8 new_ch, u8 ch_switch_cnt)
  122. {
  123. u8 ie_data[3];
  124. ie_data[0] = ch_switch_mode;
  125. ie_data[1] = new_ch;
  126. ie_data[2] = ch_switch_cnt;
  127. return rtw_set_ie23a(buf, WLAN_EID_CHANNEL_SWITCH, 3, ie_data, buf_len);
  128. }
  129. inline u8 hal_ch_offset_to_secondary_ch_offset23a(u8 ch_offset)
  130. {
  131. if (ch_offset == HAL_PRIME_CHNL_OFFSET_LOWER)
  132. return IEEE80211_HT_PARAM_CHA_SEC_BELOW;
  133. else if (ch_offset == HAL_PRIME_CHNL_OFFSET_UPPER)
  134. return IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
  135. return IEEE80211_HT_PARAM_CHA_SEC_NONE;
  136. }
  137. inline u8 *rtw_set_ie23a_secondary_ch_offset(u8 *buf, u32 *buf_len,
  138. u8 secondary_ch_offset)
  139. {
  140. return rtw_set_ie23a(buf, WLAN_EID_SECONDARY_CHANNEL_OFFSET,
  141. 1, &secondary_ch_offset, buf_len);
  142. }
  143. /*----------------------------------------------------------------------------
  144. index: the information element id index, limit is the limit for search
  145. -----------------------------------------------------------------------------*/
  146. u8 *rtw_get_ie23a(u8 *pbuf, int index, int *len, int limit)
  147. {
  148. int tmp, i;
  149. u8 *p;
  150. if (limit < 1) {
  151. return NULL;
  152. }
  153. p = pbuf;
  154. i = 0;
  155. *len = 0;
  156. while (1) {
  157. if (*p == index) {
  158. *len = *(p + 1);
  159. return p;
  160. } else {
  161. tmp = *(p + 1);
  162. p += (tmp + 2);
  163. i += (tmp + 2);
  164. }
  165. if (i >= limit)
  166. break;
  167. }
  168. return NULL;
  169. }
  170. /**
  171. * rtw_get_ie23a_ex - Search specific IE from a series of IEs
  172. * @in_ie: Address of IEs to search
  173. * @in_len: Length limit from in_ie
  174. * @eid: Element ID to match
  175. * @oui: OUI to match
  176. * @oui_len: OUI length
  177. * @ie: If not NULL and the specific IE is found, the IE will be copied
  178. * to the buf starting from the specific IE
  179. * @ielen: If not NULL and the specific IE is found, will set to the length
  180. * of the entire IE
  181. *
  182. * Returns: The address of the specific IE found, or NULL
  183. */
  184. u8 *rtw_get_ie23a_ex(u8 *in_ie, uint in_len, u8 eid, u8 *oui, u8 oui_len,
  185. u8 *ie, uint *ielen)
  186. {
  187. uint cnt;
  188. u8 *target_ie = NULL;
  189. if (ielen)
  190. *ielen = 0;
  191. if (!in_ie || in_len <= 0)
  192. return target_ie;
  193. cnt = 0;
  194. while (cnt < in_len) {
  195. if (eid == in_ie[cnt] &&
  196. (!oui || !memcmp(&in_ie[cnt+2], oui, oui_len))) {
  197. target_ie = &in_ie[cnt];
  198. if (ie)
  199. memcpy(ie, &in_ie[cnt], in_ie[cnt+1]+2);
  200. if (ielen)
  201. *ielen = in_ie[cnt+1]+2;
  202. break;
  203. } else {
  204. cnt += in_ie[cnt + 1] + 2; /* goto next */
  205. }
  206. }
  207. return target_ie;
  208. }
  209. /**
  210. * rtw_ies_remove_ie23a - Find matching IEs and remove
  211. * @ies: Address of IEs to search
  212. * @ies_len: Pointer of length of ies, will update to new length
  213. * @offset: The offset to start search
  214. * @eid: Element ID to match
  215. * @oui: OUI to match
  216. * @oui_len: OUI length
  217. *
  218. * Returns: _SUCCESS: ies is updated, _FAIL: not updated
  219. */
  220. int rtw_ies_remove_ie23a(u8 *ies, uint *ies_len, uint offset, u8 eid,
  221. u8 *oui, u8 oui_len)
  222. {
  223. int ret = _FAIL;
  224. u8 *target_ie;
  225. u32 target_ielen;
  226. u8 *start;
  227. uint search_len;
  228. if (!ies || !ies_len || *ies_len <= offset)
  229. goto exit;
  230. start = ies + offset;
  231. search_len = *ies_len - offset;
  232. while (1) {
  233. target_ie = rtw_get_ie23a_ex(start, search_len, eid, oui, oui_len,
  234. NULL, &target_ielen);
  235. if (target_ie && target_ielen) {
  236. u8 buf[MAX_IE_SZ] = {0};
  237. u8 *remain_ies = target_ie + target_ielen;
  238. uint remain_len = search_len - (remain_ies - start);
  239. memcpy(buf, remain_ies, remain_len);
  240. memcpy(target_ie, buf, remain_len);
  241. *ies_len = *ies_len - target_ielen;
  242. ret = _SUCCESS;
  243. start = target_ie;
  244. search_len = remain_len;
  245. } else {
  246. break;
  247. }
  248. }
  249. exit:
  250. return ret;
  251. }
  252. void rtw_set_supported_rate23a(u8 *SupportedRates, uint mode)
  253. {
  254. memset(SupportedRates, 0, NDIS_802_11_LENGTH_RATES_EX);
  255. switch (mode) {
  256. case WIRELESS_11B:
  257. memcpy(SupportedRates, WIFI_CCKRATES, IEEE80211_CCK_RATE_LEN);
  258. break;
  259. case WIRELESS_11G:
  260. case WIRELESS_11A:
  261. case WIRELESS_11_5N:
  262. case WIRELESS_11A_5N:/* Todo: no basic rate for ofdm ? */
  263. memcpy(SupportedRates, WIFI_OFDMRATES,
  264. IEEE80211_NUM_OFDM_RATESLEN);
  265. break;
  266. case WIRELESS_11BG:
  267. case WIRELESS_11G_24N:
  268. case WIRELESS_11_24N:
  269. case WIRELESS_11BG_24N:
  270. memcpy(SupportedRates, WIFI_CCKRATES, IEEE80211_CCK_RATE_LEN);
  271. memcpy(SupportedRates + IEEE80211_CCK_RATE_LEN, WIFI_OFDMRATES,
  272. IEEE80211_NUM_OFDM_RATESLEN);
  273. break;
  274. }
  275. }
  276. uint rtw_get_rateset_len23a(u8 *rateset)
  277. {
  278. uint i = 0;
  279. while(1) {
  280. if (rateset[i] == 0)
  281. break;
  282. if (i > 12)
  283. break;
  284. i++;
  285. }
  286. return i;
  287. }
  288. int rtw_generate_ie23a(struct registry_priv *pregistrypriv)
  289. {
  290. u8 wireless_mode;
  291. int sz = 0, rateLen;
  292. struct wlan_bssid_ex *pdev_network = &pregistrypriv->dev_network;
  293. u8 *ie = pdev_network->IEs;
  294. u16 cap;
  295. pdev_network->tsf = 0;
  296. cap = WLAN_CAPABILITY_IBSS;
  297. if (pregistrypriv->preamble == PREAMBLE_SHORT)
  298. cap |= WLAN_CAPABILITY_SHORT_PREAMBLE;
  299. if (pdev_network->Privacy)
  300. cap |= WLAN_CAPABILITY_PRIVACY;
  301. pdev_network->capability = cap;
  302. /* SSID */
  303. ie = rtw_set_ie23a(ie, WLAN_EID_SSID, pdev_network->Ssid.ssid_len,
  304. pdev_network->Ssid.ssid, &sz);
  305. /* supported rates */
  306. if (pregistrypriv->wireless_mode == WIRELESS_11ABGN) {
  307. if (pdev_network->DSConfig > 14)
  308. wireless_mode = WIRELESS_11A_5N;
  309. else
  310. wireless_mode = WIRELESS_11BG_24N;
  311. } else {
  312. wireless_mode = pregistrypriv->wireless_mode;
  313. }
  314. rtw_set_supported_rate23a(pdev_network->SupportedRates, wireless_mode) ;
  315. rateLen = rtw_get_rateset_len23a(pdev_network->SupportedRates);
  316. if (rateLen > 8) {
  317. ie = rtw_set_ie23a(ie, WLAN_EID_SUPP_RATES, 8,
  318. pdev_network->SupportedRates, &sz);
  319. /* ie = rtw_set_ie23a(ie, _EXT_SUPPORTEDRATES_IE_, (rateLen - 8), (pdev_network->SupportedRates + 8), &sz); */
  320. } else {
  321. ie = rtw_set_ie23a(ie, WLAN_EID_SUPP_RATES, rateLen,
  322. pdev_network->SupportedRates, &sz);
  323. }
  324. /* DS parameter set */
  325. ie = rtw_set_ie23a(ie, WLAN_EID_DS_PARAMS, 1,
  326. (u8 *)&pdev_network->DSConfig, &sz);
  327. /* IBSS Parameter Set */
  328. ie = rtw_set_ie23a(ie, WLAN_EID_IBSS_PARAMS, 2,
  329. (u8 *)&pdev_network->ATIMWindow, &sz);
  330. if (rateLen > 8) {
  331. ie = rtw_set_ie23a(ie, WLAN_EID_EXT_SUPP_RATES, (rateLen - 8),
  332. (pdev_network->SupportedRates + 8), &sz);
  333. }
  334. /* return _SUCCESS; */
  335. return sz;
  336. }
  337. static int rtw_get_wpa_cipher_suite(const u8 *s)
  338. {
  339. if (!memcmp(s, WPA_CIPHER_SUITE_NONE23A, WPA_SELECTOR_LEN))
  340. return WPA_CIPHER_NONE;
  341. if (!memcmp(s, WPA_CIPHER_SUITE_WEP4023A, WPA_SELECTOR_LEN))
  342. return WPA_CIPHER_WEP40;
  343. if (!memcmp(s, WPA_CIPHER_SUITE_TKIP23A, WPA_SELECTOR_LEN))
  344. return WPA_CIPHER_TKIP;
  345. if (!memcmp(s, WPA_CIPHER_SUITE_CCMP23A, WPA_SELECTOR_LEN))
  346. return WPA_CIPHER_CCMP;
  347. if (!memcmp(s, WPA_CIPHER_SUITE_WEP10423A, WPA_SELECTOR_LEN))
  348. return WPA_CIPHER_WEP104;
  349. return 0;
  350. }
  351. static int rtw_get_wpa2_cipher_suite(const u8 *s)
  352. {
  353. if (!memcmp(s, RSN_CIPHER_SUITE_NONE23A, RSN_SELECTOR_LEN))
  354. return WPA_CIPHER_NONE;
  355. if (!memcmp(s, RSN_CIPHER_SUITE_WEP4023A, RSN_SELECTOR_LEN))
  356. return WPA_CIPHER_WEP40;
  357. if (!memcmp(s, RSN_CIPHER_SUITE_TKIP23A, RSN_SELECTOR_LEN))
  358. return WPA_CIPHER_TKIP;
  359. if (!memcmp(s, RSN_CIPHER_SUITE_CCMP23A, RSN_SELECTOR_LEN))
  360. return WPA_CIPHER_CCMP;
  361. if (!memcmp(s, RSN_CIPHER_SUITE_WEP10423A, RSN_SELECTOR_LEN))
  362. return WPA_CIPHER_WEP104;
  363. return 0;
  364. }
  365. int rtw_parse_wpa_ie23a(const u8 *wpa_ie, int wpa_ie_len, int *group_cipher,
  366. int *pairwise_cipher, int *is_8021x)
  367. {
  368. int i, ret = _SUCCESS;
  369. int left, count;
  370. const u8 *pos;
  371. if (wpa_ie_len <= 0) {
  372. /* No WPA IE - fail silently */
  373. return _FAIL;
  374. }
  375. if (wpa_ie[1] != (u8)(wpa_ie_len - 2))
  376. return _FAIL;
  377. pos = wpa_ie;
  378. pos += 8;
  379. left = wpa_ie_len - 8;
  380. /* group_cipher */
  381. if (left >= WPA_SELECTOR_LEN) {
  382. *group_cipher = rtw_get_wpa_cipher_suite(pos);
  383. pos += WPA_SELECTOR_LEN;
  384. left -= WPA_SELECTOR_LEN;
  385. } else if (left > 0) {
  386. RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_,
  387. "%s: ie length mismatch, %u too much\n",
  388. __func__, left);
  389. return _FAIL;
  390. }
  391. /* pairwise_cipher */
  392. if (left >= 2) {
  393. /* count = le16_to_cpu(*(u16*)pos); */
  394. count = get_unaligned_le16(pos);
  395. pos += 2;
  396. left -= 2;
  397. if (count == 0 || left < count * WPA_SELECTOR_LEN) {
  398. RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_,
  399. "%s: ie count botch (pairwise), count %u left %u\n",
  400. __func__, count, left);
  401. return _FAIL;
  402. }
  403. for (i = 0; i < count; i++) {
  404. *pairwise_cipher |= rtw_get_wpa_cipher_suite(pos);
  405. pos += WPA_SELECTOR_LEN;
  406. left -= WPA_SELECTOR_LEN;
  407. }
  408. } else if (left == 1) {
  409. RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_,
  410. "%s: ie too short (for key mgmt)\n", __func__);
  411. return _FAIL;
  412. }
  413. if (is_8021x) {
  414. if (left >= 6) {
  415. pos += 2;
  416. if (!memcmp(pos, RTW_WPA_OUI23A_TYPE, 4)) {
  417. RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_,
  418. "%s : there has 802.1x auth\n",
  419. __func__);
  420. *is_8021x = 1;
  421. }
  422. }
  423. }
  424. return ret;
  425. }
  426. int rtw_parse_wpa2_ie23a(const u8 *rsn_ie, int rsn_ie_len, int *group_cipher,
  427. int *pairwise_cipher, int *is_8021x)
  428. {
  429. int i, ret = _SUCCESS;
  430. int left, count;
  431. const u8 *pos;
  432. u8 SUITE_1X[4] = {0x00, 0x0f, 0xac, 0x01};
  433. if (rsn_ie_len <= 0) {
  434. /* No RSN IE - fail silently */
  435. return _FAIL;
  436. }
  437. if (*rsn_ie != WLAN_EID_RSN || *(rsn_ie+1) != (u8)(rsn_ie_len - 2)) {
  438. return _FAIL;
  439. }
  440. pos = rsn_ie;
  441. pos += 4;
  442. left = rsn_ie_len - 4;
  443. /* group_cipher */
  444. if (left >= RSN_SELECTOR_LEN) {
  445. *group_cipher = rtw_get_wpa2_cipher_suite(pos);
  446. pos += RSN_SELECTOR_LEN;
  447. left -= RSN_SELECTOR_LEN;
  448. } else if (left > 0) {
  449. RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_,
  450. "%s: ie length mismatch, %u too much\n",
  451. __func__, left);
  452. return _FAIL;
  453. }
  454. /* pairwise_cipher */
  455. if (left >= 2) {
  456. /* count = le16_to_cpu(*(u16*)pos); */
  457. count = get_unaligned_le16(pos);
  458. pos += 2;
  459. left -= 2;
  460. if (count == 0 || left < count * RSN_SELECTOR_LEN) {
  461. RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_,
  462. "%s: ie count botch (pairwise), count %u left %u\n",
  463. __func__, count, left);
  464. return _FAIL;
  465. }
  466. for (i = 0; i < count; i++) {
  467. *pairwise_cipher |= rtw_get_wpa2_cipher_suite(pos);
  468. pos += RSN_SELECTOR_LEN;
  469. left -= RSN_SELECTOR_LEN;
  470. }
  471. } else if (left == 1) {
  472. RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_,
  473. "%s: ie too short (for key mgmt)\n", __func__);
  474. return _FAIL;
  475. }
  476. if (is_8021x) {
  477. if (left >= 6) {
  478. pos += 2;
  479. if (!memcmp(pos, SUITE_1X, 4)) {
  480. RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_,
  481. "%s (): there has 802.1x auth\n",
  482. __func__);
  483. *is_8021x = 1;
  484. }
  485. }
  486. }
  487. return ret;
  488. }
  489. /**
  490. * rtw_get_wps_attr23a - Search a specific WPS attribute from a given WPS IE
  491. * @wps_ie: Address of WPS IE to search
  492. * @wps_ielen: Length limit from wps_ie
  493. * @target_attr_id: The attribute ID of WPS attribute to search
  494. * @buf_attr: If not NULL and the WPS attribute is found, WPS attribute
  495. * will be copied to the buf starting from buf_attr
  496. * @len_attr: If not NULL and the WPS attribute is found, will set to the
  497. * length of the entire WPS attribute
  498. *
  499. * Returns: the address of the specific WPS attribute found, or NULL
  500. */
  501. const u8 *rtw_get_wps_attr23a(const u8 *wps_ie, uint wps_ielen,
  502. u16 target_attr_id, u8 *buf_attr, u32 *len_attr)
  503. {
  504. const u8 *attr_ptr = NULL;
  505. const u8 *target_attr_ptr = NULL;
  506. u8 wps_oui[4] = {0x00, 0x50, 0xF2, 0x04};
  507. if (len_attr)
  508. *len_attr = 0;
  509. if (wps_ie[0] != WLAN_EID_VENDOR_SPECIFIC ||
  510. memcmp(wps_ie + 2, wps_oui, 4)) {
  511. return attr_ptr;
  512. }
  513. /* 6 = 1(Element ID) + 1(Length) + 4(WPS OUI) */
  514. attr_ptr = wps_ie + 6; /* goto first attr */
  515. while (attr_ptr - wps_ie < wps_ielen) {
  516. /* 4 = 2(Attribute ID) + 2(Length) */
  517. u16 attr_id = get_unaligned_be16(attr_ptr);
  518. u16 attr_data_len = get_unaligned_be16(attr_ptr + 2);
  519. u16 attr_len = attr_data_len + 4;
  520. /* DBG_8723A("%s attr_ptr:%p, id:%u, length:%u\n", __func__, attr_ptr, attr_id, attr_data_len); */
  521. if (attr_id == target_attr_id) {
  522. target_attr_ptr = attr_ptr;
  523. if (buf_attr)
  524. memcpy(buf_attr, attr_ptr, attr_len);
  525. if (len_attr)
  526. *len_attr = attr_len;
  527. break;
  528. } else {
  529. attr_ptr += attr_len; /* goto next */
  530. }
  531. }
  532. return target_attr_ptr;
  533. }
  534. /**
  535. * rtw_get_wps_attr_content23a - Search a specific WPS attribute content
  536. * from a given WPS IE
  537. * @wps_ie: Address of WPS IE to search
  538. * @wps_ielen: Length limit from wps_ie
  539. * @target_attr_id: The attribute ID of WPS attribute to search
  540. * @buf_content: If not NULL and the WPS attribute is found, WPS attribute
  541. * content will be copied to the buf starting from buf_content
  542. * @len_content: If not NULL and the WPS attribute is found, will set to the
  543. * length of the WPS attribute content
  544. *
  545. * Returns: the address of the specific WPS attribute content found, or NULL
  546. */
  547. const u8 *rtw_get_wps_attr_content23a(const u8 *wps_ie, uint wps_ielen,
  548. u16 target_attr_id, u8 *buf_content)
  549. {
  550. const u8 *attr_ptr;
  551. u32 attr_len;
  552. attr_ptr = rtw_get_wps_attr23a(wps_ie, wps_ielen, target_attr_id,
  553. NULL, &attr_len);
  554. if (attr_ptr && attr_len) {
  555. if (buf_content)
  556. memcpy(buf_content, attr_ptr + 4, attr_len - 4);
  557. return attr_ptr + 4;
  558. }
  559. return NULL;
  560. }
  561. static int rtw_get_cipher_info(struct wlan_network *pnetwork)
  562. {
  563. const u8 *pbuf;
  564. int group_cipher = 0, pairwise_cipher = 0, is8021x = 0;
  565. int ret = _FAIL;
  566. int r, plen;
  567. char *pie;
  568. pie = pnetwork->network.IEs;
  569. plen = pnetwork->network.IELength;
  570. pbuf = cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
  571. WLAN_OUI_TYPE_MICROSOFT_WPA, pie, plen);
  572. if (pbuf && pbuf[1] > 0) {
  573. RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_,
  574. "rtw_get_cipher_info: wpa_ielen: %d\n", pbuf[1]);
  575. r = rtw_parse_wpa_ie23a(pbuf, pbuf[1] + 2, &group_cipher,
  576. &pairwise_cipher, &is8021x);
  577. if (r == _SUCCESS) {
  578. pnetwork->BcnInfo.pairwise_cipher = pairwise_cipher;
  579. pnetwork->BcnInfo.group_cipher = group_cipher;
  580. pnetwork->BcnInfo.is_8021x = is8021x;
  581. RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_,
  582. "%s: pnetwork->pairwise_cipher: %d, is_8021x is %d\n",
  583. __func__, pnetwork->BcnInfo.pairwise_cipher,
  584. pnetwork->BcnInfo.is_8021x);
  585. ret = _SUCCESS;
  586. }
  587. } else {
  588. pbuf = cfg80211_find_ie(WLAN_EID_RSN, pie, plen);
  589. if (pbuf && pbuf[1] > 0) {
  590. RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_,
  591. "get RSN IE\n");
  592. r = rtw_parse_wpa2_ie23a(pbuf, pbuf[1] + 2,
  593. &group_cipher, &pairwise_cipher,
  594. &is8021x);
  595. if (r == _SUCCESS) {
  596. RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_,
  597. "get RSN IE OK!!!\n");
  598. pnetwork->BcnInfo.pairwise_cipher =
  599. pairwise_cipher;
  600. pnetwork->BcnInfo.group_cipher = group_cipher;
  601. pnetwork->BcnInfo.is_8021x = is8021x;
  602. RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_,
  603. "%s: pnetwork->pairwise_cipher: %d,pnetwork->group_cipher is %d, is_8021x is %d\n",
  604. __func__,
  605. pnetwork->BcnInfo.pairwise_cipher,
  606. pnetwork->BcnInfo.group_cipher,
  607. pnetwork->BcnInfo.is_8021x);
  608. ret = _SUCCESS;
  609. }
  610. }
  611. }
  612. return ret;
  613. }
  614. void rtw_get_bcn_info23a(struct wlan_network *pnetwork)
  615. {
  616. u8 bencrypt = 0;
  617. int pie_len;
  618. u8 *pie;
  619. const u8 *p;
  620. if (pnetwork->network.capability & WLAN_CAPABILITY_PRIVACY) {
  621. bencrypt = 1;
  622. pnetwork->network.Privacy = 1;
  623. } else
  624. pnetwork->BcnInfo.encryp_protocol = ENCRYP_PROTOCOL_OPENSYS;
  625. RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_,
  626. "%s: ssid =%s\n", __func__, pnetwork->network.Ssid.ssid);
  627. pie = pnetwork->network.IEs;
  628. pie_len = pnetwork->network.IELength;
  629. p = cfg80211_find_ie(WLAN_EID_RSN, pie, pie_len);
  630. if (p && p[1]) {
  631. pnetwork->BcnInfo.encryp_protocol = ENCRYP_PROTOCOL_WPA2;
  632. } else if (cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
  633. WLAN_OUI_TYPE_MICROSOFT_WPA,
  634. pie, pie_len)) {
  635. pnetwork->BcnInfo.encryp_protocol = ENCRYP_PROTOCOL_WPA;
  636. } else {
  637. if (bencrypt)
  638. pnetwork->BcnInfo.encryp_protocol = ENCRYP_PROTOCOL_WEP;
  639. }
  640. RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_,
  641. "%s: pnetwork->encryp_protocol is %x\n", __func__,
  642. pnetwork->BcnInfo.encryp_protocol);
  643. RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_,
  644. "%s: pnetwork->encryp_protocol is %x\n", __func__,
  645. pnetwork->BcnInfo.encryp_protocol);
  646. rtw_get_cipher_info(pnetwork);
  647. /* get bwmode and ch_offset */
  648. }
  649. /* show MCS rate, unit: 100Kbps */
  650. u16 rtw_mcs_rate23a(u8 rf_type, u8 bw_40MHz, u8 short_GI_20, u8 short_GI_40,
  651. struct ieee80211_mcs_info *mcs)
  652. {
  653. u16 max_rate = 0;
  654. if (rf_type == RF_1T1R) {
  655. if (mcs->rx_mask[0] & BIT(7))
  656. max_rate = (bw_40MHz) ? ((short_GI_40)?1500:1350):
  657. ((short_GI_20)?722:650);
  658. else if (mcs->rx_mask[0] & BIT(6))
  659. max_rate = (bw_40MHz) ? ((short_GI_40)?1350:1215):
  660. ((short_GI_20)?650:585);
  661. else if (mcs->rx_mask[0] & BIT(5))
  662. max_rate = (bw_40MHz) ? ((short_GI_40)?1200:1080):
  663. ((short_GI_20)?578:520);
  664. else if (mcs->rx_mask[0] & BIT(4))
  665. max_rate = (bw_40MHz) ? ((short_GI_40)?900:810):
  666. ((short_GI_20)?433:390);
  667. else if (mcs->rx_mask[0] & BIT(3))
  668. max_rate = (bw_40MHz) ? ((short_GI_40)?600:540):
  669. ((short_GI_20)?289:260);
  670. else if (mcs->rx_mask[0] & BIT(2))
  671. max_rate = (bw_40MHz) ? ((short_GI_40)?450:405):
  672. ((short_GI_20)?217:195);
  673. else if (mcs->rx_mask[0] & BIT(1))
  674. max_rate = (bw_40MHz) ? ((short_GI_40)?300:270):
  675. ((short_GI_20)?144:130);
  676. else if (mcs->rx_mask[0] & BIT(0))
  677. max_rate = (bw_40MHz) ? ((short_GI_40)?150:135):
  678. ((short_GI_20)?72:65);
  679. } else {
  680. if (mcs->rx_mask[1]) {
  681. if (mcs->rx_mask[1] & BIT(7))
  682. max_rate = (bw_40MHz) ? ((short_GI_40)?3000:2700):((short_GI_20)?1444:1300);
  683. else if (mcs->rx_mask[1] & BIT(6))
  684. max_rate = (bw_40MHz) ? ((short_GI_40)?2700:2430):((short_GI_20)?1300:1170);
  685. else if (mcs->rx_mask[1] & BIT(5))
  686. max_rate = (bw_40MHz) ? ((short_GI_40)?2400:2160):((short_GI_20)?1156:1040);
  687. else if (mcs->rx_mask[1] & BIT(4))
  688. max_rate = (bw_40MHz) ? ((short_GI_40)?1800:1620):((short_GI_20)?867:780);
  689. else if (mcs->rx_mask[1] & BIT(3))
  690. max_rate = (bw_40MHz) ? ((short_GI_40)?1200:1080):((short_GI_20)?578:520);
  691. else if (mcs->rx_mask[1] & BIT(2))
  692. max_rate = (bw_40MHz) ? ((short_GI_40)?900:810):((short_GI_20)?433:390);
  693. else if (mcs->rx_mask[1] & BIT(1))
  694. max_rate = (bw_40MHz) ? ((short_GI_40)?600:540):((short_GI_20)?289:260);
  695. else if (mcs->rx_mask[1] & BIT(0))
  696. max_rate = (bw_40MHz) ? ((short_GI_40)?300:270):((short_GI_20)?144:130);
  697. } else {
  698. if (mcs->rx_mask[0] & BIT(7))
  699. max_rate = (bw_40MHz) ? ((short_GI_40)?1500:1350):((short_GI_20)?722:650);
  700. else if (mcs->rx_mask[0] & BIT(6))
  701. max_rate = (bw_40MHz) ? ((short_GI_40)?1350:1215):((short_GI_20)?650:585);
  702. else if (mcs->rx_mask[0] & BIT(5))
  703. max_rate = (bw_40MHz) ? ((short_GI_40)?1200:1080):((short_GI_20)?578:520);
  704. else if (mcs->rx_mask[0] & BIT(4))
  705. max_rate = (bw_40MHz) ? ((short_GI_40)?900:810):((short_GI_20)?433:390);
  706. else if (mcs->rx_mask[0] & BIT(3))
  707. max_rate = (bw_40MHz) ? ((short_GI_40)?600:540):((short_GI_20)?289:260);
  708. else if (mcs->rx_mask[0] & BIT(2))
  709. max_rate = (bw_40MHz) ? ((short_GI_40)?450:405):((short_GI_20)?217:195);
  710. else if (mcs->rx_mask[0] & BIT(1))
  711. max_rate = (bw_40MHz) ? ((short_GI_40)?300:270):((short_GI_20)?144:130);
  712. else if (mcs->rx_mask[0] & BIT(0))
  713. max_rate = (bw_40MHz) ? ((short_GI_40)?150:135):((short_GI_20)?72:65);
  714. }
  715. }
  716. return max_rate;
  717. }