ieee80211.c 11 KB

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