ieee80211.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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. */
  101. u8 *r8712_get_ie(u8 *pbuf, sint index, sint *len, sint limit)
  102. {
  103. sint tmp, i;
  104. u8 *p;
  105. if (limit < 1)
  106. return NULL;
  107. p = pbuf;
  108. i = 0;
  109. *len = 0;
  110. while (1) {
  111. if (*p == index) {
  112. *len = *(p + 1);
  113. return p;
  114. }
  115. tmp = *(p + 1);
  116. p += (tmp + 2);
  117. i += (tmp + 2);
  118. if (i >= limit)
  119. break;
  120. }
  121. return NULL;
  122. }
  123. static void set_supported_rate(u8 *rates, uint mode)
  124. {
  125. memset(rates, 0, NDIS_802_11_LENGTH_RATES_EX);
  126. switch (mode) {
  127. case WIRELESS_11B:
  128. memcpy(rates, WIFI_CCKRATES, IEEE80211_CCK_RATE_LEN);
  129. break;
  130. case WIRELESS_11G:
  131. case WIRELESS_11A:
  132. memcpy(rates, WIFI_OFDMRATES, IEEE80211_NUM_OFDM_RATESLEN);
  133. break;
  134. case WIRELESS_11BG:
  135. memcpy(rates, WIFI_CCKRATES, IEEE80211_CCK_RATE_LEN);
  136. memcpy(rates + IEEE80211_CCK_RATE_LEN, WIFI_OFDMRATES,
  137. IEEE80211_NUM_OFDM_RATESLEN);
  138. break;
  139. }
  140. }
  141. static uint r8712_get_rateset_len(u8 *rateset)
  142. {
  143. uint i = 0;
  144. while (1) {
  145. if ((rateset[i]) == 0)
  146. break;
  147. if (i > 12)
  148. break;
  149. i++;
  150. }
  151. return i;
  152. }
  153. int r8712_generate_ie(struct registry_priv *pregistrypriv)
  154. {
  155. int sz = 0, rateLen;
  156. struct wlan_bssid_ex *pdev_network = &pregistrypriv->dev_network;
  157. u8 *ie = pdev_network->IEs;
  158. /*timestamp will be inserted by hardware*/
  159. sz += 8;
  160. ie += sz;
  161. /*beacon interval : 2bytes*/
  162. *(u16 *)ie = cpu_to_le16((u16)pdev_network->Configuration.BeaconPeriod);
  163. sz += 2;
  164. ie += 2;
  165. /*capability info*/
  166. *(u16 *)ie = 0;
  167. *(u16 *)ie |= cpu_to_le16(cap_IBSS);
  168. if (pregistrypriv->preamble == PREAMBLE_SHORT)
  169. *(u16 *)ie |= cpu_to_le16(cap_ShortPremble);
  170. if (pdev_network->Privacy)
  171. *(u16 *)ie |= cpu_to_le16(cap_Privacy);
  172. sz += 2;
  173. ie += 2;
  174. /*SSID*/
  175. ie = r8712_set_ie(ie, _SSID_IE_, pdev_network->Ssid.SsidLength,
  176. pdev_network->Ssid.Ssid, &sz);
  177. /*supported rates*/
  178. set_supported_rate(pdev_network->rates, pregistrypriv->wireless_mode);
  179. rateLen = r8712_get_rateset_len(pdev_network->rates);
  180. if (rateLen > 8) {
  181. ie = r8712_set_ie(ie, _SUPPORTEDRATES_IE_, 8,
  182. pdev_network->rates, &sz);
  183. ie = r8712_set_ie(ie, _EXT_SUPPORTEDRATES_IE_, (rateLen - 8),
  184. (pdev_network->rates + 8), &sz);
  185. } else
  186. ie = r8712_set_ie(ie, _SUPPORTEDRATES_IE_,
  187. rateLen, pdev_network->rates, &sz);
  188. /*DS parameter set*/
  189. ie = r8712_set_ie(ie, _DSSET_IE_, 1,
  190. (u8 *)&(pdev_network->Configuration.DSConfig), &sz);
  191. /*IBSS Parameter Set*/
  192. ie = r8712_set_ie(ie, _IBSS_PARA_IE_, 2,
  193. (u8 *)&(pdev_network->Configuration.ATIMWindow), &sz);
  194. return sz;
  195. }
  196. unsigned char *r8712_get_wpa_ie(unsigned char *pie, int *wpa_ie_len, int limit)
  197. {
  198. int len;
  199. u16 val16;
  200. unsigned char wpa_oui_type[] = {0x00, 0x50, 0xf2, 0x01};
  201. u8 *pbuf = pie;
  202. while (1) {
  203. pbuf = r8712_get_ie(pbuf, _WPA_IE_ID_, &len, limit);
  204. if (pbuf) {
  205. /*check if oui matches...*/
  206. if (memcmp((pbuf + 2), wpa_oui_type,
  207. sizeof(wpa_oui_type)))
  208. goto check_next_ie;
  209. /*check version...*/
  210. memcpy((u8 *)&val16, (pbuf + 6), sizeof(val16));
  211. val16 = le16_to_cpu(val16);
  212. if (val16 != 0x0001)
  213. goto check_next_ie;
  214. *wpa_ie_len = *(pbuf + 1);
  215. return pbuf;
  216. }
  217. *wpa_ie_len = 0;
  218. return NULL;
  219. check_next_ie:
  220. limit = limit - (pbuf - pie) - 2 - len;
  221. if (limit <= 0)
  222. break;
  223. pbuf += (2 + len);
  224. }
  225. *wpa_ie_len = 0;
  226. return NULL;
  227. }
  228. unsigned char *r8712_get_wpa2_ie(unsigned char *pie, int *rsn_ie_len, int limit)
  229. {
  230. return r8712_get_ie(pie, _WPA2_IE_ID_, rsn_ie_len, limit);
  231. }
  232. static int r8712_get_wpa_cipher_suite(u8 *s)
  233. {
  234. if (!memcmp(s, (void *)WPA_CIPHER_SUITE_NONE, WPA_SELECTOR_LEN))
  235. return WPA_CIPHER_NONE;
  236. if (!memcmp(s, (void *)WPA_CIPHER_SUITE_WEP40, WPA_SELECTOR_LEN))
  237. return WPA_CIPHER_WEP40;
  238. if (!memcmp(s, (void *)WPA_CIPHER_SUITE_TKIP, WPA_SELECTOR_LEN))
  239. return WPA_CIPHER_TKIP;
  240. if (!memcmp(s, (void *)WPA_CIPHER_SUITE_CCMP, WPA_SELECTOR_LEN))
  241. return WPA_CIPHER_CCMP;
  242. if (!memcmp(s, (void *)WPA_CIPHER_SUITE_WEP104, WPA_SELECTOR_LEN))
  243. return WPA_CIPHER_WEP104;
  244. return 0;
  245. }
  246. static int r8712_get_wpa2_cipher_suite(u8 *s)
  247. {
  248. if (!memcmp(s, (void *)RSN_CIPHER_SUITE_NONE, RSN_SELECTOR_LEN))
  249. return WPA_CIPHER_NONE;
  250. if (!memcmp(s, (void *)RSN_CIPHER_SUITE_WEP40, RSN_SELECTOR_LEN))
  251. return WPA_CIPHER_WEP40;
  252. if (!memcmp(s, (void *)RSN_CIPHER_SUITE_TKIP, RSN_SELECTOR_LEN))
  253. return WPA_CIPHER_TKIP;
  254. if (!memcmp(s, (void *)RSN_CIPHER_SUITE_CCMP, RSN_SELECTOR_LEN))
  255. return WPA_CIPHER_CCMP;
  256. if (!memcmp(s, (void *)RSN_CIPHER_SUITE_WEP104, RSN_SELECTOR_LEN))
  257. return WPA_CIPHER_WEP104;
  258. return 0;
  259. }
  260. int r8712_parse_wpa_ie(u8 *wpa_ie, int wpa_ie_len, int *group_cipher,
  261. int *pairwise_cipher)
  262. {
  263. int i;
  264. int left, count;
  265. u8 *pos;
  266. if (wpa_ie_len <= 0) {
  267. /* No WPA IE - fail silently */
  268. return _FAIL;
  269. }
  270. if ((*wpa_ie != _WPA_IE_ID_) || (*(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(*(u16 *)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(*(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. }
  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. }