ieee80211.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. /******************************************************************************
  2. * ieee80211.c
  3. *
  4. * Copyright(c) 2007 - 2010 Realtek Corporation. All rights reserved.
  5. * Linux device driver for RTL8192SU
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of version 2 of the GNU General Public License as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. * more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along with
  17. * this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
  19. *
  20. * Modifications for inclusion into the Linux staging tree are
  21. * Copyright(c) 2010 Larry Finger. All rights reserved.
  22. *
  23. * Contact information:
  24. * WLAN FAE <wlanfae@realtek.com>.
  25. * Larry Finger <Larry.Finger@lwfinger.net>
  26. *
  27. ******************************************************************************/
  28. #define _IEEE80211_C
  29. #include "drv_types.h"
  30. #include "ieee80211.h"
  31. #include "wifi.h"
  32. #include "osdep_service.h"
  33. #include "wlan_bssdef.h"
  34. static const u8 WPA_OUI_TYPE[] = {0x00, 0x50, 0xf2, 1};
  35. static const u8 WPA_CIPHER_SUITE_NONE[] = {0x00, 0x50, 0xf2, 0};
  36. static const u8 WPA_CIPHER_SUITE_WEP40[] = {0x00, 0x50, 0xf2, 1};
  37. static const u8 WPA_CIPHER_SUITE_TKIP[] = {0x00, 0x50, 0xf2, 2};
  38. static const u8 WPA_CIPHER_SUITE_CCMP[] = {0x00, 0x50, 0xf2, 4};
  39. static const u8 WPA_CIPHER_SUITE_WEP104[] = {0x00, 0x50, 0xf2, 5};
  40. static const u8 RSN_CIPHER_SUITE_NONE[] = {0x00, 0x0f, 0xac, 0};
  41. static const u8 RSN_CIPHER_SUITE_WEP40[] = {0x00, 0x0f, 0xac, 1};
  42. static const u8 RSN_CIPHER_SUITE_TKIP[] = {0x00, 0x0f, 0xac, 2};
  43. static const u8 RSN_CIPHER_SUITE_CCMP[] = {0x00, 0x0f, 0xac, 4};
  44. static const u8 RSN_CIPHER_SUITE_WEP104[] = {0x00, 0x0f, 0xac, 5};
  45. /*-----------------------------------------------------------
  46. * for adhoc-master to generate ie and provide supported-rate to fw
  47. *-----------------------------------------------------------
  48. */
  49. static u8 WIFI_CCKRATES[] = {
  50. (IEEE80211_CCK_RATE_1MB | IEEE80211_BASIC_RATE_MASK),
  51. (IEEE80211_CCK_RATE_2MB | IEEE80211_BASIC_RATE_MASK),
  52. (IEEE80211_CCK_RATE_5MB | IEEE80211_BASIC_RATE_MASK),
  53. (IEEE80211_CCK_RATE_11MB | IEEE80211_BASIC_RATE_MASK)
  54. };
  55. static u8 WIFI_OFDMRATES[] = {
  56. (IEEE80211_OFDM_RATE_6MB),
  57. (IEEE80211_OFDM_RATE_9MB),
  58. (IEEE80211_OFDM_RATE_12MB),
  59. (IEEE80211_OFDM_RATE_18MB),
  60. (IEEE80211_OFDM_RATE_24MB),
  61. (IEEE80211_OFDM_RATE_36MB),
  62. (IEEE80211_OFDM_RATE_48MB),
  63. (IEEE80211_OFDM_RATE_54MB)
  64. };
  65. uint r8712_is_cckrates_included(u8 *rate)
  66. {
  67. u32 i = 0;
  68. while (rate[i] != 0) {
  69. if ((((rate[i]) & 0x7f) == 2) || (((rate[i]) & 0x7f) == 4) ||
  70. (((rate[i]) & 0x7f) == 11) || (((rate[i]) & 0x7f) == 22))
  71. return true;
  72. i++;
  73. }
  74. return false;
  75. }
  76. uint r8712_is_cckratesonly_included(u8 *rate)
  77. {
  78. u32 i = 0;
  79. while (rate[i] != 0) {
  80. if ((((rate[i]) & 0x7f) != 2) && (((rate[i]) & 0x7f) != 4) &&
  81. (((rate[i]) & 0x7f) != 11) && (((rate[i]) & 0x7f) != 22))
  82. return false;
  83. i++;
  84. }
  85. return true;
  86. }
  87. /* r8712_set_ie will update frame length */
  88. u8 *r8712_set_ie(u8 *pbuf, sint index, uint len, u8 *source, uint *frlen)
  89. {
  90. *pbuf = (u8)index;
  91. *(pbuf + 1) = (u8)len;
  92. if (len > 0)
  93. memcpy((void *)(pbuf + 2), (void *)source, len);
  94. *frlen = *frlen + (len + 2);
  95. return pbuf + len + 2;
  96. }
  97. /*----------------------------------------------------------------------------
  98. index: the information element id index, limit is the limit for search
  99. -----------------------------------------------------------------------------*/
  100. u8 *r8712_get_ie(u8 *pbuf, sint index, sint *len, sint limit)
  101. {
  102. sint tmp, i;
  103. u8 *p;
  104. if (limit < 1)
  105. return NULL;
  106. p = pbuf;
  107. i = 0;
  108. *len = 0;
  109. while (1) {
  110. if (*p == index) {
  111. *len = *(p + 1);
  112. return p;
  113. } else {
  114. tmp = *(p + 1);
  115. p += (tmp + 2);
  116. i += (tmp + 2);
  117. }
  118. if (i >= limit)
  119. break;
  120. }
  121. return NULL;
  122. }
  123. static void set_supported_rate(u8 *SupportedRates, uint mode)
  124. {
  125. memset(SupportedRates, 0, NDIS_802_11_LENGTH_RATES_EX);
  126. switch (mode) {
  127. case WIRELESS_11B:
  128. memcpy(SupportedRates, WIFI_CCKRATES,
  129. IEEE80211_CCK_RATE_LEN);
  130. break;
  131. case WIRELESS_11G:
  132. case WIRELESS_11A:
  133. memcpy(SupportedRates, WIFI_OFDMRATES,
  134. IEEE80211_NUM_OFDM_RATESLEN);
  135. break;
  136. case WIRELESS_11BG:
  137. memcpy(SupportedRates, WIFI_CCKRATES, IEEE80211_CCK_RATE_LEN);
  138. memcpy(SupportedRates + IEEE80211_CCK_RATE_LEN, WIFI_OFDMRATES,
  139. IEEE80211_NUM_OFDM_RATESLEN);
  140. break;
  141. }
  142. }
  143. static uint r8712_get_rateset_len(u8 *rateset)
  144. {
  145. uint i = 0;
  146. while (1) {
  147. if ((rateset[i]) == 0)
  148. break;
  149. if (i > 12)
  150. break;
  151. i++;
  152. }
  153. return i;
  154. }
  155. int r8712_generate_ie(struct registry_priv *pregistrypriv,
  156. struct _adapter *padapter)
  157. {
  158. int sz = 0, rateLen;
  159. struct wlan_bssid_ex *pdev_network = &pregistrypriv->dev_network;
  160. u8 *ie = pdev_network->IEs;
  161. struct ieee80211_ht_cap ht_capie;
  162. struct ieee80211_ht_addt_info ht_addt_info;
  163. unsigned char WMM_IE[] = {0x00, 0x50, 0xf2, 0x02, 0x00, 0x01, 0x00};
  164. struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
  165. struct qos_priv *pqospriv = &pmlmepriv->qospriv;
  166. /*timestamp will be inserted by hardware*/
  167. sz += 8;
  168. ie += sz;
  169. /*beacon interval : 2bytes*/
  170. *(u16 *)ie = cpu_to_le16((u16)pdev_network->Configuration.BeaconPeriod);
  171. sz += 2;
  172. ie += 2;
  173. /*capability info*/
  174. *(u16 *)ie = 0;
  175. *(u16 *)ie |= cpu_to_le16(cap_IBSS);
  176. if (pregistrypriv->preamble == PREAMBLE_SHORT)
  177. *(u16 *)ie |= cpu_to_le16(cap_ShortPremble);
  178. if (pdev_network->Privacy)
  179. *(u16 *)ie |= cpu_to_le16(cap_Privacy);
  180. sz += 2;
  181. ie += 2;
  182. /*SSID*/
  183. ie = r8712_set_ie(ie, _SSID_IE_, pdev_network->Ssid.SsidLength,
  184. pdev_network->Ssid.Ssid, &sz);
  185. /*supported rates*/
  186. set_supported_rate(pdev_network->SupportedRates,
  187. pregistrypriv->wireless_mode);
  188. rateLen = r8712_get_rateset_len(pdev_network->SupportedRates);
  189. if (rateLen > 8) {
  190. ie = r8712_set_ie(ie, _SUPPORTEDRATES_IE_, 8,
  191. pdev_network->SupportedRates, &sz);
  192. ie = r8712_set_ie(ie, _EXT_SUPPORTEDRATES_IE_, (rateLen - 8),
  193. (pdev_network->SupportedRates + 8), &sz);
  194. } else
  195. ie = r8712_set_ie(ie, _SUPPORTEDRATES_IE_,
  196. rateLen, pdev_network->SupportedRates, &sz);
  197. /*DS parameter set*/
  198. ie = r8712_set_ie(ie, _DSSET_IE_, 1,
  199. (u8 *)&(pdev_network->Configuration.DSConfig), &sz);
  200. /*IBSS Parameter Set*/
  201. ie = r8712_set_ie(ie, _IBSS_PARA_IE_, 2,
  202. (u8 *)&(pdev_network->Configuration.ATIMWindow), &sz);
  203. if (pregistrypriv->ht_enable == 1) {
  204. if (pqospriv->qos_option == 0) {
  205. ie = r8712_set_ie(ie, _VENDOR_SPECIFIC_IE_,
  206. _WMM_IE_Length_, WMM_IE, &sz);
  207. pqospriv->qos_option = 1;
  208. }
  209. memset(&ht_capie, 0, sizeof(struct ieee80211_ht_cap));
  210. ht_capie.cap_info = IEEE80211_HT_CAP_SUP_WIDTH |
  211. IEEE80211_HT_CAP_SGI_20 |
  212. IEEE80211_HT_CAP_SGI_40 |
  213. IEEE80211_HT_CAP_TX_STBC |
  214. IEEE80211_HT_CAP_MAX_AMSDU |
  215. IEEE80211_HT_CAP_DSSSCCK40;
  216. ht_capie.ampdu_params_info = (IEEE80211_HT_CAP_AMPDU_FACTOR &
  217. 0x03) | (IEEE80211_HT_CAP_AMPDU_DENSITY & 0x00);
  218. ie = r8712_set_ie(ie, _HT_CAPABILITY_IE_,
  219. sizeof(struct ieee80211_ht_cap),
  220. (unsigned char *)&ht_capie, &sz);
  221. /*add HT info ie*/
  222. memset(&ht_addt_info, 0,
  223. sizeof(struct ieee80211_ht_addt_info));
  224. /*need to add the HT additional IEs*/
  225. ht_addt_info.control_chan = pregistrypriv->channel;
  226. ie = r8712_set_ie(ie, _HT_ADD_INFO_IE_,
  227. sizeof(struct ieee80211_ht_addt_info),
  228. (unsigned char *)&ht_addt_info, &sz);
  229. }
  230. return sz;
  231. }
  232. unsigned char *r8712_get_wpa_ie(unsigned char *pie, int *wpa_ie_len, int limit)
  233. {
  234. int len;
  235. u16 val16;
  236. unsigned char wpa_oui_type[] = {0x00, 0x50, 0xf2, 0x01};
  237. u8 *pbuf = pie;
  238. while (1) {
  239. pbuf = r8712_get_ie(pbuf, _WPA_IE_ID_, &len, limit);
  240. if (pbuf) {
  241. /*check if oui matches...*/
  242. if (memcmp((pbuf + 2), wpa_oui_type,
  243. sizeof(wpa_oui_type)))
  244. goto check_next_ie;
  245. /*check version...*/
  246. memcpy((u8 *)&val16, (pbuf + 6), sizeof(val16));
  247. val16 = le16_to_cpu(val16);
  248. if (val16 != 0x0001)
  249. goto check_next_ie;
  250. *wpa_ie_len = *(pbuf + 1);
  251. return pbuf;
  252. } else {
  253. *wpa_ie_len = 0;
  254. return NULL;
  255. }
  256. check_next_ie:
  257. limit = limit - (pbuf - pie) - 2 - len;
  258. if (limit <= 0)
  259. break;
  260. pbuf += (2 + len);
  261. }
  262. *wpa_ie_len = 0;
  263. return NULL;
  264. }
  265. unsigned char *r8712_get_wpa2_ie(unsigned char *pie, int *rsn_ie_len, int limit)
  266. {
  267. return r8712_get_ie(pie, _WPA2_IE_ID_, rsn_ie_len, limit);
  268. }
  269. static int r8712_get_wpa_cipher_suite(u8 *s)
  270. {
  271. if (!memcmp(s, (void *)WPA_CIPHER_SUITE_NONE, WPA_SELECTOR_LEN))
  272. return WPA_CIPHER_NONE;
  273. if (!memcmp(s, (void *)WPA_CIPHER_SUITE_WEP40, WPA_SELECTOR_LEN))
  274. return WPA_CIPHER_WEP40;
  275. if (!memcmp(s, (void *)WPA_CIPHER_SUITE_TKIP, WPA_SELECTOR_LEN))
  276. return WPA_CIPHER_TKIP;
  277. if (!memcmp(s, (void *)WPA_CIPHER_SUITE_CCMP, WPA_SELECTOR_LEN))
  278. return WPA_CIPHER_CCMP;
  279. if (!memcmp(s, (void *)WPA_CIPHER_SUITE_WEP104, WPA_SELECTOR_LEN))
  280. return WPA_CIPHER_WEP104;
  281. return 0;
  282. }
  283. static int r8712_get_wpa2_cipher_suite(u8 *s)
  284. {
  285. if (!memcmp(s, (void *)RSN_CIPHER_SUITE_NONE, RSN_SELECTOR_LEN))
  286. return WPA_CIPHER_NONE;
  287. if (!memcmp(s, (void *)RSN_CIPHER_SUITE_WEP40, RSN_SELECTOR_LEN))
  288. return WPA_CIPHER_WEP40;
  289. if (!memcmp(s, (void *)RSN_CIPHER_SUITE_TKIP, RSN_SELECTOR_LEN))
  290. return WPA_CIPHER_TKIP;
  291. if (!memcmp(s, (void *)RSN_CIPHER_SUITE_CCMP, RSN_SELECTOR_LEN))
  292. return WPA_CIPHER_CCMP;
  293. if (!memcmp(s, (void *)RSN_CIPHER_SUITE_WEP104, RSN_SELECTOR_LEN))
  294. return WPA_CIPHER_WEP104;
  295. return 0;
  296. }
  297. int r8712_parse_wpa_ie(u8 *wpa_ie, int wpa_ie_len, int *group_cipher,
  298. int *pairwise_cipher)
  299. {
  300. int i, ret = _SUCCESS;
  301. int left, count;
  302. u8 *pos;
  303. if (wpa_ie_len <= 0) {
  304. /* No WPA IE - fail silently */
  305. return _FAIL;
  306. }
  307. if ((*wpa_ie != _WPA_IE_ID_) || (*(wpa_ie + 1) != (u8)(wpa_ie_len - 2))
  308. || (memcmp(wpa_ie + 2, (void *)WPA_OUI_TYPE, WPA_SELECTOR_LEN)))
  309. return _FAIL;
  310. pos = wpa_ie;
  311. pos += 8;
  312. left = wpa_ie_len - 8;
  313. /*group_cipher*/
  314. if (left >= WPA_SELECTOR_LEN) {
  315. *group_cipher = r8712_get_wpa_cipher_suite(pos);
  316. pos += WPA_SELECTOR_LEN;
  317. left -= WPA_SELECTOR_LEN;
  318. } else if (left > 0)
  319. return _FAIL;
  320. /*pairwise_cipher*/
  321. if (left >= 2) {
  322. count = le16_to_cpu(*(u16 *)pos);
  323. pos += 2;
  324. left -= 2;
  325. if (count == 0 || left < count * WPA_SELECTOR_LEN)
  326. return _FAIL;
  327. for (i = 0; i < count; i++) {
  328. *pairwise_cipher |= r8712_get_wpa_cipher_suite(pos);
  329. pos += WPA_SELECTOR_LEN;
  330. left -= WPA_SELECTOR_LEN;
  331. }
  332. } else if (left == 1)
  333. return _FAIL;
  334. return ret;
  335. }
  336. int r8712_parse_wpa2_ie(u8 *rsn_ie, int rsn_ie_len, int *group_cipher,
  337. int *pairwise_cipher)
  338. {
  339. int i, ret = _SUCCESS;
  340. int left, count;
  341. u8 *pos;
  342. if (rsn_ie_len <= 0) {
  343. /* No RSN IE - fail silently */
  344. return _FAIL;
  345. }
  346. if ((*rsn_ie != _WPA2_IE_ID_) || (*(rsn_ie+1) != (u8)(rsn_ie_len - 2)))
  347. return _FAIL;
  348. pos = rsn_ie;
  349. pos += 4;
  350. left = rsn_ie_len - 4;
  351. /*group_cipher*/
  352. if (left >= RSN_SELECTOR_LEN) {
  353. *group_cipher = r8712_get_wpa2_cipher_suite(pos);
  354. pos += RSN_SELECTOR_LEN;
  355. left -= RSN_SELECTOR_LEN;
  356. } else if (left > 0)
  357. return _FAIL;
  358. /*pairwise_cipher*/
  359. if (left >= 2) {
  360. count = le16_to_cpu(*(u16 *)pos);
  361. pos += 2;
  362. left -= 2;
  363. if (count == 0 || left < count * RSN_SELECTOR_LEN)
  364. return _FAIL;
  365. for (i = 0; i < count; i++) {
  366. *pairwise_cipher |= r8712_get_wpa2_cipher_suite(pos);
  367. pos += RSN_SELECTOR_LEN;
  368. left -= RSN_SELECTOR_LEN;
  369. }
  370. } else if (left == 1)
  371. return _FAIL;
  372. return ret;
  373. }
  374. int r8712_get_sec_ie(u8 *in_ie, uint in_len, u8 *rsn_ie, u16 *rsn_len,
  375. u8 *wpa_ie, u16 *wpa_len)
  376. {
  377. u8 authmode, sec_idx;
  378. u8 wpa_oui[4] = {0x0, 0x50, 0xf2, 0x01};
  379. uint cnt;
  380. /*Search required WPA or WPA2 IE and copy to sec_ie[ ]*/
  381. cnt = (_TIMESTAMP_ + _BEACON_ITERVAL_ + _CAPABILITY_);
  382. sec_idx = 0;
  383. while (cnt < in_len) {
  384. authmode = in_ie[cnt];
  385. if ((authmode == _WPA_IE_ID_) &&
  386. (!memcmp(&in_ie[cnt + 2], &wpa_oui[0], 4))) {
  387. memcpy(wpa_ie, &in_ie[cnt], in_ie[cnt + 1] + 2);
  388. *wpa_len = in_ie[cnt+1]+2;
  389. cnt += in_ie[cnt + 1] + 2; /*get next */
  390. } else {
  391. if (authmode == _WPA2_IE_ID_) {
  392. memcpy(rsn_ie, &in_ie[cnt],
  393. in_ie[cnt + 1] + 2);
  394. *rsn_len = in_ie[cnt+1] + 2;
  395. cnt += in_ie[cnt+1] + 2; /*get next*/
  396. } else
  397. cnt += in_ie[cnt+1] + 2; /*get next*/
  398. }
  399. }
  400. return *rsn_len + *wpa_len;
  401. }
  402. int r8712_get_wps_ie(u8 *in_ie, uint in_len, u8 *wps_ie, uint *wps_ielen)
  403. {
  404. int match;
  405. uint cnt;
  406. u8 eid, wps_oui[4] = {0x0, 0x50, 0xf2, 0x04};
  407. cnt = 12;
  408. match = false;
  409. while (cnt < in_len) {
  410. eid = in_ie[cnt];
  411. if ((eid == _WPA_IE_ID_) &&
  412. (!memcmp(&in_ie[cnt+2], wps_oui, 4))) {
  413. memcpy(wps_ie, &in_ie[cnt], in_ie[cnt+1]+2);
  414. *wps_ielen = in_ie[cnt+1]+2;
  415. cnt += in_ie[cnt+1]+2;
  416. match = true;
  417. break;
  418. } else
  419. cnt += in_ie[cnt+1]+2; /* goto next */
  420. }
  421. return match;
  422. }