ieee80211.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. * Modifications for inclusion into the Linux staging tree are
  17. * Copyright(c) 2010 Larry Finger. All rights reserved.
  18. *
  19. * Contact information:
  20. * WLAN FAE <wlanfae@realtek.com>.
  21. * Larry Finger <Larry.Finger@lwfinger.net>
  22. *
  23. ******************************************************************************/
  24. #define _IEEE80211_C
  25. #include "drv_types.h"
  26. #include "ieee80211.h"
  27. #include "wifi.h"
  28. #include "osdep_service.h"
  29. #include "wlan_bssdef.h"
  30. static const u8 WPA_OUI_TYPE[] = {0x00, 0x50, 0xf2, 1};
  31. static const u8 WPA_CIPHER_SUITE_NONE[] = {0x00, 0x50, 0xf2, 0};
  32. static const u8 WPA_CIPHER_SUITE_WEP40[] = {0x00, 0x50, 0xf2, 1};
  33. static const u8 WPA_CIPHER_SUITE_TKIP[] = {0x00, 0x50, 0xf2, 2};
  34. static const u8 WPA_CIPHER_SUITE_CCMP[] = {0x00, 0x50, 0xf2, 4};
  35. static const u8 WPA_CIPHER_SUITE_WEP104[] = {0x00, 0x50, 0xf2, 5};
  36. static const u8 RSN_CIPHER_SUITE_NONE[] = {0x00, 0x0f, 0xac, 0};
  37. static const u8 RSN_CIPHER_SUITE_WEP40[] = {0x00, 0x0f, 0xac, 1};
  38. static const u8 RSN_CIPHER_SUITE_TKIP[] = {0x00, 0x0f, 0xac, 2};
  39. static const u8 RSN_CIPHER_SUITE_CCMP[] = {0x00, 0x0f, 0xac, 4};
  40. static const u8 RSN_CIPHER_SUITE_WEP104[] = {0x00, 0x0f, 0xac, 5};
  41. /*-----------------------------------------------------------
  42. * for adhoc-master to generate ie and provide supported-rate to fw
  43. *-----------------------------------------------------------
  44. */
  45. static u8 WIFI_CCKRATES[] = {
  46. (IEEE80211_CCK_RATE_1MB | IEEE80211_BASIC_RATE_MASK),
  47. (IEEE80211_CCK_RATE_2MB | IEEE80211_BASIC_RATE_MASK),
  48. (IEEE80211_CCK_RATE_5MB | IEEE80211_BASIC_RATE_MASK),
  49. (IEEE80211_CCK_RATE_11MB | IEEE80211_BASIC_RATE_MASK)
  50. };
  51. static u8 WIFI_OFDMRATES[] = {
  52. (IEEE80211_OFDM_RATE_6MB),
  53. (IEEE80211_OFDM_RATE_9MB),
  54. (IEEE80211_OFDM_RATE_12MB),
  55. (IEEE80211_OFDM_RATE_18MB),
  56. (IEEE80211_OFDM_RATE_24MB),
  57. (IEEE80211_OFDM_RATE_36MB),
  58. (IEEE80211_OFDM_RATE_48MB),
  59. (IEEE80211_OFDM_RATE_54MB)
  60. };
  61. uint r8712_is_cckrates_included(u8 *rate)
  62. {
  63. u32 i = 0;
  64. while (rate[i] != 0) {
  65. if ((((rate[i]) & 0x7f) == 2) || (((rate[i]) & 0x7f) == 4) ||
  66. (((rate[i]) & 0x7f) == 11) || (((rate[i]) & 0x7f) == 22))
  67. return true;
  68. i++;
  69. }
  70. return false;
  71. }
  72. uint r8712_is_cckratesonly_included(u8 *rate)
  73. {
  74. u32 i = 0;
  75. while (rate[i] != 0) {
  76. if ((((rate[i]) & 0x7f) != 2) && (((rate[i]) & 0x7f) != 4) &&
  77. (((rate[i]) & 0x7f) != 11) && (((rate[i]) & 0x7f) != 22))
  78. return false;
  79. i++;
  80. }
  81. return true;
  82. }
  83. /* r8712_set_ie will update frame length */
  84. u8 *r8712_set_ie(u8 *pbuf, sint index, uint len, u8 *source, uint *frlen)
  85. {
  86. *pbuf = (u8)index;
  87. *(pbuf + 1) = (u8)len;
  88. if (len > 0)
  89. memcpy((void *)(pbuf + 2), (void *)source, len);
  90. *frlen = *frlen + (len + 2);
  91. return pbuf + len + 2;
  92. }
  93. /* ---------------------------------------------------------------------------
  94. * index: the information element id index, limit is the limit for search
  95. * ---------------------------------------------------------------------------
  96. */
  97. u8 *r8712_get_ie(u8 *pbuf, sint index, uint *len, sint limit)
  98. {
  99. sint tmp, i;
  100. u8 *p;
  101. if (limit < 1)
  102. return NULL;
  103. p = pbuf;
  104. i = 0;
  105. *len = 0;
  106. while (1) {
  107. if (*p == index) {
  108. *len = *(p + 1);
  109. return p;
  110. }
  111. tmp = *(p + 1);
  112. p += (tmp + 2);
  113. i += (tmp + 2);
  114. if (i >= limit)
  115. break;
  116. }
  117. return NULL;
  118. }
  119. static void set_supported_rate(u8 *rates, uint mode)
  120. {
  121. memset(rates, 0, NDIS_802_11_LENGTH_RATES_EX);
  122. switch (mode) {
  123. case WIRELESS_11B:
  124. memcpy(rates, WIFI_CCKRATES, IEEE80211_CCK_RATE_LEN);
  125. break;
  126. case WIRELESS_11G:
  127. case WIRELESS_11A:
  128. memcpy(rates, WIFI_OFDMRATES, IEEE80211_NUM_OFDM_RATESLEN);
  129. break;
  130. case WIRELESS_11BG:
  131. memcpy(rates, WIFI_CCKRATES, IEEE80211_CCK_RATE_LEN);
  132. memcpy(rates + IEEE80211_CCK_RATE_LEN, WIFI_OFDMRATES,
  133. IEEE80211_NUM_OFDM_RATESLEN);
  134. break;
  135. }
  136. }
  137. static uint r8712_get_rateset_len(u8 *rateset)
  138. {
  139. uint i = 0;
  140. while (1) {
  141. if ((rateset[i]) == 0)
  142. break;
  143. if (i > 12)
  144. break;
  145. i++;
  146. }
  147. return i;
  148. }
  149. int r8712_generate_ie(struct registry_priv *pregistrypriv)
  150. {
  151. int rate_len;
  152. uint sz = 0;
  153. struct wlan_bssid_ex *pdev_network = &pregistrypriv->dev_network;
  154. u8 *ie = pdev_network->IEs;
  155. u16 beaconPeriod = (u16)pdev_network->Configuration.BeaconPeriod;
  156. /*timestamp will be inserted by hardware*/
  157. sz += 8;
  158. ie += sz;
  159. /*beacon interval : 2bytes*/
  160. *(__le16 *)ie = cpu_to_le16(beaconPeriod);
  161. sz += 2;
  162. ie += 2;
  163. /*capability info*/
  164. *(u16 *)ie = 0;
  165. *(__le16 *)ie |= cpu_to_le16(cap_IBSS);
  166. if (pregistrypriv->preamble == PREAMBLE_SHORT)
  167. *(__le16 *)ie |= cpu_to_le16(cap_ShortPremble);
  168. if (pdev_network->Privacy)
  169. *(__le16 *)ie |= cpu_to_le16(cap_Privacy);
  170. sz += 2;
  171. ie += 2;
  172. /*SSID*/
  173. ie = r8712_set_ie(ie, _SSID_IE_, pdev_network->Ssid.SsidLength,
  174. pdev_network->Ssid.Ssid, &sz);
  175. /*supported rates*/
  176. set_supported_rate(pdev_network->rates, pregistrypriv->wireless_mode);
  177. rate_len = r8712_get_rateset_len(pdev_network->rates);
  178. if (rate_len > 8) {
  179. ie = r8712_set_ie(ie, _SUPPORTEDRATES_IE_, 8,
  180. pdev_network->rates, &sz);
  181. ie = r8712_set_ie(ie, _EXT_SUPPORTEDRATES_IE_, (rate_len - 8),
  182. (pdev_network->rates + 8), &sz);
  183. } else {
  184. ie = r8712_set_ie(ie, _SUPPORTEDRATES_IE_,
  185. rate_len, pdev_network->rates, &sz);
  186. }
  187. /*DS parameter set*/
  188. ie = r8712_set_ie(ie, _DSSET_IE_, 1,
  189. (u8 *)&pdev_network->Configuration.DSConfig, &sz);
  190. /*IBSS Parameter Set*/
  191. ie = r8712_set_ie(ie, _IBSS_PARA_IE_, 2,
  192. (u8 *)&pdev_network->Configuration.ATIMWindow, &sz);
  193. return sz;
  194. }
  195. unsigned char *r8712_get_wpa_ie(unsigned char *pie, uint *wpa_ie_len, int limit)
  196. {
  197. u32 len;
  198. u16 val16;
  199. unsigned char wpa_oui_type[] = {0x00, 0x50, 0xf2, 0x01};
  200. u8 *pbuf = pie;
  201. while (1) {
  202. pbuf = r8712_get_ie(pbuf, _WPA_IE_ID_, &len, limit);
  203. if (pbuf) {
  204. /*check if oui matches...*/
  205. if (memcmp((pbuf + 2), wpa_oui_type,
  206. sizeof(wpa_oui_type)))
  207. goto check_next_ie;
  208. /*check version...*/
  209. memcpy((u8 *)&val16, (pbuf + 6), sizeof(val16));
  210. le16_to_cpus(&val16);
  211. if (val16 != 0x0001)
  212. goto check_next_ie;
  213. *wpa_ie_len = *(pbuf + 1);
  214. return pbuf;
  215. }
  216. *wpa_ie_len = 0;
  217. return NULL;
  218. check_next_ie:
  219. limit = limit - (pbuf - pie) - 2 - len;
  220. if (limit <= 0)
  221. break;
  222. pbuf += (2 + len);
  223. }
  224. *wpa_ie_len = 0;
  225. return NULL;
  226. }
  227. unsigned char *r8712_get_wpa2_ie(unsigned char *pie, uint *rsn_ie_len, int limit)
  228. {
  229. return r8712_get_ie(pie, _WPA2_IE_ID_, rsn_ie_len, limit);
  230. }
  231. static int r8712_get_wpa_cipher_suite(u8 *s)
  232. {
  233. if (!memcmp(s, (void *)WPA_CIPHER_SUITE_NONE, WPA_SELECTOR_LEN))
  234. return WPA_CIPHER_NONE;
  235. if (!memcmp(s, (void *)WPA_CIPHER_SUITE_WEP40, WPA_SELECTOR_LEN))
  236. return WPA_CIPHER_WEP40;
  237. if (!memcmp(s, (void *)WPA_CIPHER_SUITE_TKIP, WPA_SELECTOR_LEN))
  238. return WPA_CIPHER_TKIP;
  239. if (!memcmp(s, (void *)WPA_CIPHER_SUITE_CCMP, WPA_SELECTOR_LEN))
  240. return WPA_CIPHER_CCMP;
  241. if (!memcmp(s, (void *)WPA_CIPHER_SUITE_WEP104, WPA_SELECTOR_LEN))
  242. return WPA_CIPHER_WEP104;
  243. return 0;
  244. }
  245. static int r8712_get_wpa2_cipher_suite(u8 *s)
  246. {
  247. if (!memcmp(s, (void *)RSN_CIPHER_SUITE_NONE, RSN_SELECTOR_LEN))
  248. return WPA_CIPHER_NONE;
  249. if (!memcmp(s, (void *)RSN_CIPHER_SUITE_WEP40, RSN_SELECTOR_LEN))
  250. return WPA_CIPHER_WEP40;
  251. if (!memcmp(s, (void *)RSN_CIPHER_SUITE_TKIP, RSN_SELECTOR_LEN))
  252. return WPA_CIPHER_TKIP;
  253. if (!memcmp(s, (void *)RSN_CIPHER_SUITE_CCMP, RSN_SELECTOR_LEN))
  254. return WPA_CIPHER_CCMP;
  255. if (!memcmp(s, (void *)RSN_CIPHER_SUITE_WEP104, RSN_SELECTOR_LEN))
  256. return WPA_CIPHER_WEP104;
  257. return 0;
  258. }
  259. int r8712_parse_wpa_ie(u8 *wpa_ie, int wpa_ie_len, int *group_cipher,
  260. int *pairwise_cipher)
  261. {
  262. int i;
  263. int left, count;
  264. u8 *pos;
  265. if (wpa_ie_len <= 0) {
  266. /* No WPA IE - fail silently */
  267. return _FAIL;
  268. }
  269. if ((*wpa_ie != _WPA_IE_ID_) ||
  270. (*(wpa_ie + 1) != (u8)(wpa_ie_len - 2)) ||
  271. (memcmp(wpa_ie + 2, (void *)WPA_OUI_TYPE, WPA_SELECTOR_LEN)))
  272. return _FAIL;
  273. pos = wpa_ie;
  274. pos += 8;
  275. left = wpa_ie_len - 8;
  276. /*group_cipher*/
  277. if (left >= WPA_SELECTOR_LEN) {
  278. *group_cipher = r8712_get_wpa_cipher_suite(pos);
  279. pos += WPA_SELECTOR_LEN;
  280. left -= WPA_SELECTOR_LEN;
  281. } else if (left > 0) {
  282. return _FAIL;
  283. }
  284. /*pairwise_cipher*/
  285. if (left >= 2) {
  286. count = le16_to_cpu(*(__le16 *)pos);
  287. pos += 2;
  288. left -= 2;
  289. if (count == 0 || left < count * WPA_SELECTOR_LEN)
  290. return _FAIL;
  291. for (i = 0; i < count; i++) {
  292. *pairwise_cipher |= r8712_get_wpa_cipher_suite(pos);
  293. pos += WPA_SELECTOR_LEN;
  294. left -= WPA_SELECTOR_LEN;
  295. }
  296. } else if (left == 1) {
  297. return _FAIL;
  298. }
  299. return _SUCCESS;
  300. }
  301. int r8712_parse_wpa2_ie(u8 *rsn_ie, int rsn_ie_len, int *group_cipher,
  302. int *pairwise_cipher)
  303. {
  304. int i;
  305. int left, count;
  306. u8 *pos;
  307. if (rsn_ie_len <= 0) {
  308. /* No RSN IE - fail silently */
  309. return _FAIL;
  310. }
  311. if ((*rsn_ie != _WPA2_IE_ID_) ||
  312. (*(rsn_ie + 1) != (u8)(rsn_ie_len - 2)))
  313. return _FAIL;
  314. pos = rsn_ie;
  315. pos += 4;
  316. left = rsn_ie_len - 4;
  317. /*group_cipher*/
  318. if (left >= RSN_SELECTOR_LEN) {
  319. *group_cipher = r8712_get_wpa2_cipher_suite(pos);
  320. pos += RSN_SELECTOR_LEN;
  321. left -= RSN_SELECTOR_LEN;
  322. } else if (left > 0) {
  323. return _FAIL;
  324. }
  325. /*pairwise_cipher*/
  326. if (left >= 2) {
  327. count = le16_to_cpu(*(__le16 *)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. }
  340. return _SUCCESS;
  341. }
  342. int r8712_get_sec_ie(u8 *in_ie, uint in_len, u8 *rsn_ie, u16 *rsn_len,
  343. u8 *wpa_ie, u16 *wpa_len)
  344. {
  345. u8 authmode;
  346. u8 wpa_oui[4] = {0x0, 0x50, 0xf2, 0x01};
  347. uint cnt;
  348. /*Search required WPA or WPA2 IE and copy to sec_ie[ ]*/
  349. cnt = _TIMESTAMP_ + _BEACON_ITERVAL_ + _CAPABILITY_;
  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. }
  368. return *rsn_len + *wpa_len;
  369. }
  370. int r8712_get_wps_ie(u8 *in_ie, uint in_len, u8 *wps_ie, uint *wps_ielen)
  371. {
  372. int match;
  373. uint cnt;
  374. u8 eid, wps_oui[4] = {0x0, 0x50, 0xf2, 0x04};
  375. cnt = 12;
  376. match = false;
  377. while (cnt < in_len) {
  378. eid = in_ie[cnt];
  379. if ((eid == _WPA_IE_ID_) &&
  380. (!memcmp(&in_ie[cnt + 2], wps_oui, 4))) {
  381. memcpy(wps_ie, &in_ie[cnt], in_ie[cnt + 1] + 2);
  382. *wps_ielen = in_ie[cnt + 1] + 2;
  383. cnt += in_ie[cnt + 1] + 2;
  384. match = true;
  385. break;
  386. }
  387. cnt += in_ie[cnt + 1] + 2; /* goto next */
  388. }
  389. return match;
  390. }