ieee80211.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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. {
  157. int sz = 0, rateLen;
  158. struct wlan_bssid_ex *pdev_network = &pregistrypriv->dev_network;
  159. u8 *ie = pdev_network->IEs;
  160. /*timestamp will be inserted by hardware*/
  161. sz += 8;
  162. ie += sz;
  163. /*beacon interval : 2bytes*/
  164. *(u16 *)ie = cpu_to_le16((u16)pdev_network->Configuration.BeaconPeriod);
  165. sz += 2;
  166. ie += 2;
  167. /*capability info*/
  168. *(u16 *)ie = 0;
  169. *(u16 *)ie |= cpu_to_le16(cap_IBSS);
  170. if (pregistrypriv->preamble == PREAMBLE_SHORT)
  171. *(u16 *)ie |= cpu_to_le16(cap_ShortPremble);
  172. if (pdev_network->Privacy)
  173. *(u16 *)ie |= cpu_to_le16(cap_Privacy);
  174. sz += 2;
  175. ie += 2;
  176. /*SSID*/
  177. ie = r8712_set_ie(ie, _SSID_IE_, pdev_network->Ssid.SsidLength,
  178. pdev_network->Ssid.Ssid, &sz);
  179. /*supported rates*/
  180. set_supported_rate(pdev_network->SupportedRates,
  181. pregistrypriv->wireless_mode);
  182. rateLen = r8712_get_rateset_len(pdev_network->SupportedRates);
  183. if (rateLen > 8) {
  184. ie = r8712_set_ie(ie, _SUPPORTEDRATES_IE_, 8,
  185. pdev_network->SupportedRates, &sz);
  186. ie = r8712_set_ie(ie, _EXT_SUPPORTEDRATES_IE_, (rateLen - 8),
  187. (pdev_network->SupportedRates + 8), &sz);
  188. } else
  189. ie = r8712_set_ie(ie, _SUPPORTEDRATES_IE_,
  190. rateLen, pdev_network->SupportedRates, &sz);
  191. /*DS parameter set*/
  192. ie = r8712_set_ie(ie, _DSSET_IE_, 1,
  193. (u8 *)&(pdev_network->Configuration.DSConfig), &sz);
  194. /*IBSS Parameter Set*/
  195. ie = r8712_set_ie(ie, _IBSS_PARA_IE_, 2,
  196. (u8 *)&(pdev_network->Configuration.ATIMWindow), &sz);
  197. return sz;
  198. }
  199. unsigned char *r8712_get_wpa_ie(unsigned char *pie, int *wpa_ie_len, int limit)
  200. {
  201. int len;
  202. u16 val16;
  203. unsigned char wpa_oui_type[] = {0x00, 0x50, 0xf2, 0x01};
  204. u8 *pbuf = pie;
  205. while (1) {
  206. pbuf = r8712_get_ie(pbuf, _WPA_IE_ID_, &len, limit);
  207. if (pbuf) {
  208. /*check if oui matches...*/
  209. if (memcmp((pbuf + 2), wpa_oui_type,
  210. sizeof(wpa_oui_type)))
  211. goto check_next_ie;
  212. /*check version...*/
  213. memcpy((u8 *)&val16, (pbuf + 6), sizeof(val16));
  214. val16 = le16_to_cpu(val16);
  215. if (val16 != 0x0001)
  216. goto check_next_ie;
  217. *wpa_ie_len = *(pbuf + 1);
  218. return pbuf;
  219. } else {
  220. *wpa_ie_len = 0;
  221. return NULL;
  222. }
  223. check_next_ie:
  224. limit = limit - (pbuf - pie) - 2 - len;
  225. if (limit <= 0)
  226. break;
  227. pbuf += (2 + len);
  228. }
  229. *wpa_ie_len = 0;
  230. return NULL;
  231. }
  232. unsigned char *r8712_get_wpa2_ie(unsigned char *pie, int *rsn_ie_len, int limit)
  233. {
  234. return r8712_get_ie(pie, _WPA2_IE_ID_, rsn_ie_len, limit);
  235. }
  236. static int r8712_get_wpa_cipher_suite(u8 *s)
  237. {
  238. if (!memcmp(s, (void *)WPA_CIPHER_SUITE_NONE, WPA_SELECTOR_LEN))
  239. return WPA_CIPHER_NONE;
  240. if (!memcmp(s, (void *)WPA_CIPHER_SUITE_WEP40, WPA_SELECTOR_LEN))
  241. return WPA_CIPHER_WEP40;
  242. if (!memcmp(s, (void *)WPA_CIPHER_SUITE_TKIP, WPA_SELECTOR_LEN))
  243. return WPA_CIPHER_TKIP;
  244. if (!memcmp(s, (void *)WPA_CIPHER_SUITE_CCMP, WPA_SELECTOR_LEN))
  245. return WPA_CIPHER_CCMP;
  246. if (!memcmp(s, (void *)WPA_CIPHER_SUITE_WEP104, WPA_SELECTOR_LEN))
  247. return WPA_CIPHER_WEP104;
  248. return 0;
  249. }
  250. static int r8712_get_wpa2_cipher_suite(u8 *s)
  251. {
  252. if (!memcmp(s, (void *)RSN_CIPHER_SUITE_NONE, RSN_SELECTOR_LEN))
  253. return WPA_CIPHER_NONE;
  254. if (!memcmp(s, (void *)RSN_CIPHER_SUITE_WEP40, RSN_SELECTOR_LEN))
  255. return WPA_CIPHER_WEP40;
  256. if (!memcmp(s, (void *)RSN_CIPHER_SUITE_TKIP, RSN_SELECTOR_LEN))
  257. return WPA_CIPHER_TKIP;
  258. if (!memcmp(s, (void *)RSN_CIPHER_SUITE_CCMP, RSN_SELECTOR_LEN))
  259. return WPA_CIPHER_CCMP;
  260. if (!memcmp(s, (void *)RSN_CIPHER_SUITE_WEP104, RSN_SELECTOR_LEN))
  261. return WPA_CIPHER_WEP104;
  262. return 0;
  263. }
  264. int r8712_parse_wpa_ie(u8 *wpa_ie, int wpa_ie_len, int *group_cipher,
  265. int *pairwise_cipher)
  266. {
  267. int i;
  268. int left, count;
  269. u8 *pos;
  270. if (wpa_ie_len <= 0) {
  271. /* No WPA IE - fail silently */
  272. return _FAIL;
  273. }
  274. if ((*wpa_ie != _WPA_IE_ID_) || (*(wpa_ie + 1) != (u8)(wpa_ie_len - 2))
  275. || (memcmp(wpa_ie + 2, (void *)WPA_OUI_TYPE, WPA_SELECTOR_LEN)))
  276. return _FAIL;
  277. pos = wpa_ie;
  278. pos += 8;
  279. left = wpa_ie_len - 8;
  280. /*group_cipher*/
  281. if (left >= WPA_SELECTOR_LEN) {
  282. *group_cipher = r8712_get_wpa_cipher_suite(pos);
  283. pos += WPA_SELECTOR_LEN;
  284. left -= WPA_SELECTOR_LEN;
  285. } else if (left > 0)
  286. return _FAIL;
  287. /*pairwise_cipher*/
  288. if (left >= 2) {
  289. count = le16_to_cpu(*(u16 *)pos);
  290. pos += 2;
  291. left -= 2;
  292. if (count == 0 || left < count * WPA_SELECTOR_LEN)
  293. return _FAIL;
  294. for (i = 0; i < count; i++) {
  295. *pairwise_cipher |= r8712_get_wpa_cipher_suite(pos);
  296. pos += WPA_SELECTOR_LEN;
  297. left -= WPA_SELECTOR_LEN;
  298. }
  299. } else if (left == 1)
  300. return _FAIL;
  301. return _SUCCESS;
  302. }
  303. int r8712_parse_wpa2_ie(u8 *rsn_ie, int rsn_ie_len, int *group_cipher,
  304. int *pairwise_cipher)
  305. {
  306. int i;
  307. int left, count;
  308. u8 *pos;
  309. if (rsn_ie_len <= 0) {
  310. /* No RSN IE - fail silently */
  311. return _FAIL;
  312. }
  313. if ((*rsn_ie != _WPA2_IE_ID_) || (*(rsn_ie+1) != (u8)(rsn_ie_len - 2)))
  314. return _FAIL;
  315. pos = rsn_ie;
  316. pos += 4;
  317. left = rsn_ie_len - 4;
  318. /*group_cipher*/
  319. if (left >= RSN_SELECTOR_LEN) {
  320. *group_cipher = r8712_get_wpa2_cipher_suite(pos);
  321. pos += RSN_SELECTOR_LEN;
  322. left -= RSN_SELECTOR_LEN;
  323. } else if (left > 0)
  324. return _FAIL;
  325. /*pairwise_cipher*/
  326. if (left >= 2) {
  327. count = le16_to_cpu(*(u16 *)pos);
  328. pos += 2;
  329. left -= 2;
  330. if (count == 0 || left < count * RSN_SELECTOR_LEN)
  331. return _FAIL;
  332. for (i = 0; i < count; i++) {
  333. *pairwise_cipher |= r8712_get_wpa2_cipher_suite(pos);
  334. pos += RSN_SELECTOR_LEN;
  335. left -= RSN_SELECTOR_LEN;
  336. }
  337. } else if (left == 1)
  338. return _FAIL;
  339. return _SUCCESS;
  340. }
  341. int r8712_get_sec_ie(u8 *in_ie, uint in_len, u8 *rsn_ie, u16 *rsn_len,
  342. u8 *wpa_ie, u16 *wpa_len)
  343. {
  344. u8 authmode, sec_idx;
  345. u8 wpa_oui[4] = {0x0, 0x50, 0xf2, 0x01};
  346. uint cnt;
  347. /*Search required WPA or WPA2 IE and copy to sec_ie[ ]*/
  348. cnt = (_TIMESTAMP_ + _BEACON_ITERVAL_ + _CAPABILITY_);
  349. sec_idx = 0;
  350. while (cnt < in_len) {
  351. authmode = in_ie[cnt];
  352. if ((authmode == _WPA_IE_ID_) &&
  353. (!memcmp(&in_ie[cnt + 2], &wpa_oui[0], 4))) {
  354. memcpy(wpa_ie, &in_ie[cnt], in_ie[cnt + 1] + 2);
  355. *wpa_len = in_ie[cnt+1]+2;
  356. cnt += in_ie[cnt + 1] + 2; /*get next */
  357. } else {
  358. if (authmode == _WPA2_IE_ID_) {
  359. memcpy(rsn_ie, &in_ie[cnt],
  360. in_ie[cnt + 1] + 2);
  361. *rsn_len = in_ie[cnt+1] + 2;
  362. cnt += in_ie[cnt+1] + 2; /*get next*/
  363. } else
  364. cnt += in_ie[cnt+1] + 2; /*get next*/
  365. }
  366. }
  367. return *rsn_len + *wpa_len;
  368. }
  369. int r8712_get_wps_ie(u8 *in_ie, uint in_len, u8 *wps_ie, uint *wps_ielen)
  370. {
  371. int match;
  372. uint cnt;
  373. u8 eid, wps_oui[4] = {0x0, 0x50, 0xf2, 0x04};
  374. cnt = 12;
  375. match = false;
  376. while (cnt < in_len) {
  377. eid = in_ie[cnt];
  378. if ((eid == _WPA_IE_ID_) &&
  379. (!memcmp(&in_ie[cnt+2], wps_oui, 4))) {
  380. memcpy(wps_ie, &in_ie[cnt], in_ie[cnt+1]+2);
  381. *wps_ielen = in_ie[cnt+1]+2;
  382. cnt += in_ie[cnt+1]+2;
  383. match = true;
  384. break;
  385. } else
  386. cnt += in_ie[cnt+1]+2; /* goto next */
  387. }
  388. return match;
  389. }