wpactl.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  1. /*
  2. * Copyright (c) 1996, 2003 VIA Networking Technologies, Inc.
  3. * All rights reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. *
  19. *
  20. * File: wpactl.c
  21. *
  22. * Purpose: handle wpa supplicant ioctl input/out functions
  23. *
  24. * Author: Lyndon Chen
  25. *
  26. * Date: Oct. 20, 2003
  27. *
  28. * Functions:
  29. *
  30. * Revision History:
  31. *
  32. */
  33. #include "wpactl.h"
  34. #include "key.h"
  35. #include "mac.h"
  36. #include "device.h"
  37. #include "wmgr.h"
  38. #include "iocmd.h"
  39. #include "iowpa.h"
  40. #include "rf.h"
  41. /*--------------------- Static Definitions -------------------------*/
  42. #define VIAWGET_WPA_MAX_BUF_SIZE 1024
  43. static const int frequency_list[] = {
  44. 2412, 2417, 2422, 2427, 2432, 2437, 2442,
  45. 2447, 2452, 2457, 2462, 2467, 2472, 2484
  46. };
  47. /*--------------------- Static Classes ----------------------------*/
  48. /*--------------------- Static Variables --------------------------*/
  49. //static int msglevel =MSG_LEVEL_DEBUG;
  50. static int msglevel =MSG_LEVEL_INFO;
  51. /*--------------------- Static Functions --------------------------*/
  52. /*--------------------- Export Variables --------------------------*/
  53. static void wpadev_setup(struct net_device *dev)
  54. {
  55. dev->type = ARPHRD_IEEE80211;
  56. dev->hard_header_len = ETH_HLEN;
  57. dev->mtu = 2048;
  58. dev->addr_len = ETH_ALEN;
  59. dev->tx_queue_len = 1000;
  60. memset(dev->broadcast,0xFF, ETH_ALEN);
  61. dev->flags = IFF_BROADCAST|IFF_MULTICAST;
  62. }
  63. /*
  64. * Description:
  65. * register netdev for wpa supplicant deamon
  66. *
  67. * Parameters:
  68. * In:
  69. * pDevice -
  70. * enable -
  71. * Out:
  72. *
  73. * Return Value:
  74. *
  75. */
  76. static int wpa_init_wpadev(PSDevice pDevice)
  77. {
  78. PSDevice wpadev_priv;
  79. struct net_device *dev = pDevice->dev;
  80. int ret=0;
  81. pDevice->wpadev = alloc_netdev(sizeof(PSDevice), "vntwpa", wpadev_setup);
  82. if (pDevice->wpadev == NULL)
  83. return -ENOMEM;
  84. wpadev_priv = netdev_priv(pDevice->wpadev);
  85. *wpadev_priv = *pDevice;
  86. memcpy(pDevice->wpadev->dev_addr, dev->dev_addr, ETH_ALEN);
  87. pDevice->wpadev->base_addr = dev->base_addr;
  88. pDevice->wpadev->irq = dev->irq;
  89. pDevice->wpadev->mem_start = dev->mem_start;
  90. pDevice->wpadev->mem_end = dev->mem_end;
  91. ret = register_netdev(pDevice->wpadev);
  92. if (ret) {
  93. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "%s: register_netdev(WPA) failed!\n",
  94. dev->name);
  95. free_netdev(pDevice->wpadev);
  96. return -1;
  97. }
  98. if (pDevice->skb == NULL) {
  99. pDevice->skb = dev_alloc_skb((int)pDevice->rx_buf_sz);
  100. if (pDevice->skb == NULL)
  101. return -ENOMEM;
  102. }
  103. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "%s: Registered netdev %s for WPA management\n",
  104. dev->name, pDevice->wpadev->name);
  105. return 0;
  106. }
  107. /*
  108. * Description:
  109. * unregister net_device (wpadev)
  110. *
  111. * Parameters:
  112. * In:
  113. * pDevice -
  114. * Out:
  115. *
  116. * Return Value:
  117. *
  118. */
  119. static int wpa_release_wpadev(PSDevice pDevice)
  120. {
  121. if (pDevice->skb) {
  122. dev_kfree_skb(pDevice->skb);
  123. pDevice->skb = NULL;
  124. }
  125. if (pDevice->wpadev) {
  126. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "%s: Netdevice %s unregistered\n",
  127. pDevice->dev->name, pDevice->wpadev->name);
  128. unregister_netdev(pDevice->wpadev);
  129. free_netdev(pDevice->wpadev);
  130. pDevice->wpadev = NULL;
  131. }
  132. return 0;
  133. }
  134. /*
  135. * Description:
  136. * Set enable/disable dev for wpa supplicant deamon
  137. *
  138. * Parameters:
  139. * In:
  140. * pDevice -
  141. * val -
  142. * Out:
  143. *
  144. * Return Value:
  145. *
  146. */
  147. int wpa_set_wpadev(PSDevice pDevice, int val)
  148. {
  149. if (val)
  150. return wpa_init_wpadev(pDevice);
  151. else
  152. return wpa_release_wpadev(pDevice);
  153. }
  154. /*
  155. * Description:
  156. * Set WPA algorithm & keys
  157. *
  158. * Parameters:
  159. * In:
  160. * pDevice -
  161. * param -
  162. * Out:
  163. *
  164. * Return Value:
  165. *
  166. */
  167. int wpa_set_keys(PSDevice pDevice, void *ctx, bool fcpfkernel)
  168. {
  169. struct viawget_wpa_param *param=ctx;
  170. PSMgmtObject pMgmt = pDevice->pMgmt;
  171. unsigned long dwKeyIndex = 0;
  172. unsigned char abyKey[MAX_KEY_LEN];
  173. unsigned char abySeq[MAX_KEY_LEN];
  174. QWORD KeyRSC;
  175. // NDIS_802_11_KEY_RSC KeyRSC;
  176. unsigned char byKeyDecMode = KEY_CTL_WEP;
  177. int ret = 0;
  178. int uu, ii;
  179. if (param->u.wpa_key.alg_name > WPA_ALG_CCMP)
  180. return -EINVAL;
  181. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "param->u.wpa_key.alg_name = %d \n", param->u.wpa_key.alg_name);
  182. if (param->u.wpa_key.alg_name == WPA_ALG_NONE) {
  183. pDevice->eEncryptionStatus = Ndis802_11EncryptionDisabled;
  184. pDevice->bEncryptionEnable = false;
  185. pDevice->byKeyIndex = 0;
  186. pDevice->bTransmitKey = false;
  187. KeyvRemoveAllWEPKey(&(pDevice->sKey), pDevice->PortOffset);
  188. for (uu=0; uu<MAX_KEY_TABLE; uu++) {
  189. MACvDisableKeyEntry(pDevice->PortOffset, uu);
  190. }
  191. return ret;
  192. }
  193. //spin_unlock_irq(&pDevice->lock);
  194. if(param->u.wpa_key.key && fcpfkernel) {
  195. memcpy(&abyKey[0], param->u.wpa_key.key, param->u.wpa_key.key_len);
  196. }
  197. else {
  198. spin_unlock_irq(&pDevice->lock);
  199. if (param->u.wpa_key.key &&
  200. copy_from_user(&abyKey[0], param->u.wpa_key.key, param->u.wpa_key.key_len)) {
  201. spin_lock_irq(&pDevice->lock);
  202. return -EINVAL;
  203. }
  204. spin_lock_irq(&pDevice->lock);
  205. }
  206. dwKeyIndex = (unsigned long)(param->u.wpa_key.key_index);
  207. if (param->u.wpa_key.alg_name == WPA_ALG_WEP) {
  208. if (dwKeyIndex > 3) {
  209. return -EINVAL;
  210. }
  211. else {
  212. if (param->u.wpa_key.set_tx) {
  213. pDevice->byKeyIndex = (unsigned char)dwKeyIndex;
  214. pDevice->bTransmitKey = true;
  215. dwKeyIndex |= (1 << 31);
  216. }
  217. KeybSetDefaultKey(&(pDevice->sKey),
  218. dwKeyIndex & ~(BIT30 | USE_KEYRSC),
  219. param->u.wpa_key.key_len,
  220. NULL,
  221. abyKey,
  222. KEY_CTL_WEP,
  223. pDevice->PortOffset,
  224. pDevice->byLocalID);
  225. }
  226. pDevice->eEncryptionStatus = Ndis802_11Encryption1Enabled;
  227. pDevice->bEncryptionEnable = true;
  228. return ret;
  229. }
  230. //spin_unlock_irq(&pDevice->lock);
  231. if(param->u.wpa_key.seq && fcpfkernel) {
  232. memcpy(&abySeq[0], param->u.wpa_key.seq, param->u.wpa_key.seq_len);
  233. }
  234. else {
  235. spin_unlock_irq(&pDevice->lock);
  236. if (param->u.wpa_key.seq &&
  237. copy_from_user(&abySeq[0], param->u.wpa_key.seq, param->u.wpa_key.seq_len)) {
  238. spin_lock_irq(&pDevice->lock);
  239. return -EINVAL;
  240. }
  241. spin_lock_irq(&pDevice->lock);
  242. }
  243. if (param->u.wpa_key.seq_len > 0) {
  244. for (ii = 0 ; ii < param->u.wpa_key.seq_len ; ii++) {
  245. if (ii < 4)
  246. LODWORD(KeyRSC) |= (abySeq[ii] << (ii * 8));
  247. else
  248. HIDWORD(KeyRSC) |= (abySeq[ii] << ((ii-4) * 8));
  249. //KeyRSC |= (abySeq[ii] << (ii * 8));
  250. }
  251. dwKeyIndex |= 1 << 29;
  252. }
  253. if (param->u.wpa_key.key_index >= MAX_GROUP_KEY) {
  254. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "return dwKeyIndex > 3\n");
  255. return -EINVAL;
  256. }
  257. if (param->u.wpa_key.alg_name == WPA_ALG_TKIP) {
  258. pDevice->eEncryptionStatus = Ndis802_11Encryption2Enabled;
  259. }
  260. if (param->u.wpa_key.alg_name == WPA_ALG_CCMP) {
  261. pDevice->eEncryptionStatus = Ndis802_11Encryption3Enabled;
  262. }
  263. if (param->u.wpa_key.set_tx)
  264. dwKeyIndex |= (1 << 31);
  265. if (pDevice->eEncryptionStatus == Ndis802_11Encryption3Enabled)
  266. byKeyDecMode = KEY_CTL_CCMP;
  267. else if (pDevice->eEncryptionStatus == Ndis802_11Encryption2Enabled)
  268. byKeyDecMode = KEY_CTL_TKIP;
  269. else
  270. byKeyDecMode = KEY_CTL_WEP;
  271. // Fix HCT test that set 256 bits KEY and Ndis802_11Encryption3Enabled
  272. if (pDevice->eEncryptionStatus == Ndis802_11Encryption3Enabled) {
  273. if (param->u.wpa_key.key_len == MAX_KEY_LEN)
  274. byKeyDecMode = KEY_CTL_TKIP;
  275. else if (param->u.wpa_key.key_len == WLAN_WEP40_KEYLEN)
  276. byKeyDecMode = KEY_CTL_WEP;
  277. else if (param->u.wpa_key.key_len == WLAN_WEP104_KEYLEN)
  278. byKeyDecMode = KEY_CTL_WEP;
  279. } else if (pDevice->eEncryptionStatus == Ndis802_11Encryption2Enabled) {
  280. if (param->u.wpa_key.key_len == WLAN_WEP40_KEYLEN)
  281. byKeyDecMode = KEY_CTL_WEP;
  282. else if (param->u.wpa_key.key_len == WLAN_WEP104_KEYLEN)
  283. byKeyDecMode = KEY_CTL_WEP;
  284. }
  285. // Check TKIP key length
  286. if ((byKeyDecMode == KEY_CTL_TKIP) &&
  287. (param->u.wpa_key.key_len != MAX_KEY_LEN)) {
  288. // TKIP Key must be 256 bits
  289. //DBG_PRN_WLAN03(("return NDIS_STATUS_INVALID_DATA - TKIP Key must be 256 bits\n"));
  290. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "return- TKIP Key must be 256 bits!\n");
  291. return -EINVAL;
  292. }
  293. // Check AES key length
  294. if ((byKeyDecMode == KEY_CTL_CCMP) &&
  295. (param->u.wpa_key.key_len != AES_KEY_LEN)) {
  296. // AES Key must be 128 bits
  297. //DBG_PRN_WLAN03(("return NDIS_STATUS_INVALID_DATA - AES Key must be 128 bits\n"));
  298. return -EINVAL;
  299. }
  300. // spin_lock_irq(&pDevice->lock);
  301. if (is_broadcast_ether_addr(&param->addr[0]) || (param->addr == NULL)) {
  302. // If is_broadcast_ether_addr, set the key as every key entry's group key.
  303. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Groupe Key Assign.\n");
  304. if ((KeybSetAllGroupKey(&(pDevice->sKey),
  305. dwKeyIndex,
  306. param->u.wpa_key.key_len,
  307. (PQWORD) &(KeyRSC),
  308. (unsigned char *)abyKey,
  309. byKeyDecMode,
  310. pDevice->PortOffset,
  311. pDevice->byLocalID) == true) &&
  312. (KeybSetDefaultKey(&(pDevice->sKey),
  313. dwKeyIndex,
  314. param->u.wpa_key.key_len,
  315. (PQWORD) &(KeyRSC),
  316. (unsigned char *)abyKey,
  317. byKeyDecMode,
  318. pDevice->PortOffset,
  319. pDevice->byLocalID) == true) ) {
  320. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "GROUP Key Assign.\n");
  321. } else {
  322. //DBG_PRN_WLAN03(("return NDIS_STATUS_INVALID_DATA -KeybSetDefaultKey Fail.0\n"));
  323. // spin_unlock_irq(&pDevice->lock);
  324. return -EINVAL;
  325. }
  326. } else {
  327. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Pairwise Key Assign.\n");
  328. // BSSID not 0xffffffffffff
  329. // Pairwise Key can't be WEP
  330. if (byKeyDecMode == KEY_CTL_WEP) {
  331. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Pairwise Key can't be WEP\n");
  332. //spin_unlock_irq(&pDevice->lock);
  333. return -EINVAL;
  334. }
  335. dwKeyIndex |= (1 << 30); // set pairwise key
  336. if (pMgmt->eConfigMode == WMAC_CONFIG_IBSS_STA) {
  337. //DBG_PRN_WLAN03(("return NDIS_STATUS_INVALID_DATA - WMAC_CONFIG_IBSS_STA\n"));
  338. //spin_unlock_irq(&pDevice->lock);
  339. return -EINVAL;
  340. }
  341. if (KeybSetKey(&(pDevice->sKey),
  342. &param->addr[0],
  343. dwKeyIndex,
  344. param->u.wpa_key.key_len,
  345. (PQWORD) &(KeyRSC),
  346. (unsigned char *)abyKey,
  347. byKeyDecMode,
  348. pDevice->PortOffset,
  349. pDevice->byLocalID) == true) {
  350. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Pairwise Key Set\n");
  351. } else {
  352. // Key Table Full
  353. if (!compare_ether_addr(&param->addr[0], pDevice->abyBSSID)) {
  354. //DBG_PRN_WLAN03(("return NDIS_STATUS_INVALID_DATA -Key Table Full.2\n"));
  355. //spin_unlock_irq(&pDevice->lock);
  356. return -EINVAL;
  357. } else {
  358. // Save Key and configure just before associate/reassociate to BSSID
  359. // we do not implement now
  360. //spin_unlock_irq(&pDevice->lock);
  361. return -EINVAL;
  362. }
  363. }
  364. } // BSSID not 0xffffffffffff
  365. if ((ret == 0) && ((param->u.wpa_key.set_tx) != 0)) {
  366. pDevice->byKeyIndex = (unsigned char)param->u.wpa_key.key_index;
  367. pDevice->bTransmitKey = true;
  368. }
  369. pDevice->bEncryptionEnable = true;
  370. //spin_unlock_irq(&pDevice->lock);
  371. /*
  372. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " key=%x-%x-%x-%x-%x-xxxxx \n",
  373. pMgmt->sNodeDBTable[iNodeIndex].abyWepKey[byKeyIndex][0],
  374. pMgmt->sNodeDBTable[iNodeIndex].abyWepKey[byKeyIndex][1],
  375. pMgmt->sNodeDBTable[iNodeIndex].abyWepKey[byKeyIndex][2],
  376. pMgmt->sNodeDBTable[iNodeIndex].abyWepKey[byKeyIndex][3],
  377. pMgmt->sNodeDBTable[iNodeIndex].abyWepKey[byKeyIndex][4]
  378. );
  379. */
  380. return ret;
  381. }
  382. /*
  383. * Description:
  384. * enable wpa auth & mode
  385. *
  386. * Parameters:
  387. * In:
  388. * pDevice -
  389. * param -
  390. * Out:
  391. *
  392. * Return Value:
  393. *
  394. */
  395. static int wpa_set_wpa(PSDevice pDevice,
  396. struct viawget_wpa_param *param)
  397. {
  398. PSMgmtObject pMgmt = pDevice->pMgmt;
  399. int ret = 0;
  400. pMgmt->eAuthenMode = WMAC_AUTH_OPEN;
  401. pMgmt->bShareKeyAlgorithm = false;
  402. return ret;
  403. }
  404. /*
  405. * Description:
  406. * set disassociate
  407. *
  408. * Parameters:
  409. * In:
  410. * pDevice -
  411. * param -
  412. * Out:
  413. *
  414. * Return Value:
  415. *
  416. */
  417. static int wpa_set_disassociate(PSDevice pDevice,
  418. struct viawget_wpa_param *param)
  419. {
  420. PSMgmtObject pMgmt = pDevice->pMgmt;
  421. int ret = 0;
  422. spin_lock_irq(&pDevice->lock);
  423. if (pDevice->bLinkPass) {
  424. if (!memcmp(param->addr, pMgmt->abyCurrBSSID, 6))
  425. bScheduleCommand((void *)pDevice, WLAN_CMD_DISASSOCIATE, NULL);
  426. }
  427. spin_unlock_irq(&pDevice->lock);
  428. return ret;
  429. }
  430. /*
  431. * Description:
  432. * enable scan process
  433. *
  434. * Parameters:
  435. * In:
  436. * pDevice -
  437. * param -
  438. * Out:
  439. *
  440. * Return Value:
  441. *
  442. */
  443. static int wpa_set_scan(PSDevice pDevice,
  444. struct viawget_wpa_param *param)
  445. {
  446. int ret = 0;
  447. spin_lock_irq(&pDevice->lock);
  448. BSSvClearBSSList((void *)pDevice, pDevice->bLinkPass);
  449. bScheduleCommand((void *) pDevice, WLAN_CMD_BSSID_SCAN, NULL);
  450. spin_unlock_irq(&pDevice->lock);
  451. return ret;
  452. }
  453. /*
  454. * Description:
  455. * get bssid
  456. *
  457. * Parameters:
  458. * In:
  459. * pDevice -
  460. * param -
  461. * Out:
  462. *
  463. * Return Value:
  464. *
  465. */
  466. static int wpa_get_bssid(PSDevice pDevice,
  467. struct viawget_wpa_param *param)
  468. {
  469. PSMgmtObject pMgmt = pDevice->pMgmt;
  470. int ret = 0;
  471. memcpy(param->u.wpa_associate.bssid, pMgmt->abyCurrBSSID , 6);
  472. return ret;
  473. }
  474. /*
  475. * Description:
  476. * get bssid
  477. *
  478. * Parameters:
  479. * In:
  480. * pDevice -
  481. * param -
  482. * Out:
  483. *
  484. * Return Value:
  485. *
  486. */
  487. static int wpa_get_ssid(PSDevice pDevice,
  488. struct viawget_wpa_param *param)
  489. {
  490. PSMgmtObject pMgmt = pDevice->pMgmt;
  491. PWLAN_IE_SSID pItemSSID;
  492. int ret = 0;
  493. pItemSSID = (PWLAN_IE_SSID)pMgmt->abyCurrSSID;
  494. memcpy(param->u.wpa_associate.ssid, pItemSSID->abySSID , pItemSSID->len);
  495. param->u.wpa_associate.ssid_len = pItemSSID->len;
  496. return ret;
  497. }
  498. /*
  499. * Description:
  500. * get scan results
  501. *
  502. * Parameters:
  503. * In:
  504. * pDevice -
  505. * param -
  506. * Out:
  507. *
  508. * Return Value:
  509. *
  510. */
  511. static int wpa_get_scan(PSDevice pDevice,
  512. struct viawget_wpa_param *param)
  513. {
  514. struct viawget_scan_result *scan_buf;
  515. PSMgmtObject pMgmt = pDevice->pMgmt;
  516. PWLAN_IE_SSID pItemSSID;
  517. PKnownBSS pBSS;
  518. unsigned char *pBuf;
  519. int ret = 0;
  520. u16 count = 0;
  521. u16 ii, jj;
  522. #if 1
  523. unsigned char *ptempBSS;
  524. ptempBSS = kmalloc(sizeof(KnownBSS), (int)GFP_ATOMIC);
  525. if (ptempBSS == NULL) {
  526. printk("bubble sort kmalloc memory fail@@@\n");
  527. ret = -ENOMEM;
  528. return ret;
  529. }
  530. for (ii = 0; ii < MAX_BSS_NUM; ii++) {
  531. for(jj=0;jj<MAX_BSS_NUM-ii-1;jj++) {
  532. if((pMgmt->sBSSList[jj].bActive!=true) ||
  533. ((pMgmt->sBSSList[jj].uRSSI>pMgmt->sBSSList[jj+1].uRSSI) &&(pMgmt->sBSSList[jj+1].bActive!=false))) {
  534. memcpy(ptempBSS,&pMgmt->sBSSList[jj],sizeof(KnownBSS));
  535. memcpy(&pMgmt->sBSSList[jj],&pMgmt->sBSSList[jj+1],sizeof(KnownBSS));
  536. memcpy(&pMgmt->sBSSList[jj+1],ptempBSS,sizeof(KnownBSS));
  537. }
  538. }
  539. }
  540. kfree(ptempBSS);
  541. // printk("bubble sort result:\n");
  542. //for (ii = 0; ii < MAX_BSS_NUM; ii++)
  543. // printk("%d [%s]:RSSI=%d\n",ii,((PWLAN_IE_SSID)(pMgmt->sBSSList[ii].abySSID))->abySSID,
  544. // pMgmt->sBSSList[ii].uRSSI);
  545. #endif
  546. //******mike:bubble sort by stronger RSSI*****//
  547. count = 0;
  548. pBSS = &(pMgmt->sBSSList[0]);
  549. for (ii = 0; ii < MAX_BSS_NUM; ii++) {
  550. pBSS = &(pMgmt->sBSSList[ii]);
  551. if (!pBSS->bActive)
  552. continue;
  553. count++;
  554. }
  555. pBuf = kcalloc(count, sizeof(struct viawget_scan_result), (int)GFP_ATOMIC);
  556. if (pBuf == NULL) {
  557. ret = -ENOMEM;
  558. return ret;
  559. }
  560. scan_buf = (struct viawget_scan_result *)pBuf;
  561. pBSS = &(pMgmt->sBSSList[0]);
  562. for (ii = 0, jj = 0; ii < MAX_BSS_NUM ; ii++) {
  563. pBSS = &(pMgmt->sBSSList[ii]);
  564. if (pBSS->bActive) {
  565. if (jj >= count)
  566. break;
  567. memcpy(scan_buf->bssid, pBSS->abyBSSID, WLAN_BSSID_LEN);
  568. pItemSSID = (PWLAN_IE_SSID)pBSS->abySSID;
  569. memcpy(scan_buf->ssid, pItemSSID->abySSID, pItemSSID->len);
  570. scan_buf->ssid_len = pItemSSID->len;
  571. scan_buf->freq = frequency_list[pBSS->uChannel-1];
  572. scan_buf->caps = pBSS->wCapInfo;
  573. //scan_buf->caps = pBSS->wCapInfo;
  574. //scan_buf->qual =
  575. //scan_buf->noise =
  576. //scan_buf->level =
  577. //scan_buf->maxrate =
  578. if (pBSS->wWPALen != 0) {
  579. scan_buf->wpa_ie_len = pBSS->wWPALen;
  580. memcpy(scan_buf->wpa_ie, pBSS->byWPAIE, pBSS->wWPALen);
  581. }
  582. if (pBSS->wRSNLen != 0) {
  583. scan_buf->rsn_ie_len = pBSS->wRSNLen;
  584. memcpy(scan_buf->rsn_ie, pBSS->byRSNIE, pBSS->wRSNLen);
  585. }
  586. scan_buf = (struct viawget_scan_result *)((unsigned char *)scan_buf + sizeof(struct viawget_scan_result));
  587. jj ++;
  588. }
  589. }
  590. if (jj < count)
  591. count = jj;
  592. if (copy_to_user(param->u.scan_results.buf, pBuf, sizeof(struct viawget_scan_result) * count)) {
  593. ret = -EFAULT;
  594. }
  595. param->u.scan_results.scan_count = count;
  596. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " param->u.scan_results.scan_count = %d\n", count)
  597. kfree(pBuf);
  598. return ret;
  599. }
  600. /*
  601. * Description:
  602. * set associate with AP
  603. *
  604. * Parameters:
  605. * In:
  606. * pDevice -
  607. * param -
  608. * Out:
  609. *
  610. * Return Value:
  611. *
  612. */
  613. static int wpa_set_associate(PSDevice pDevice,
  614. struct viawget_wpa_param *param)
  615. {
  616. PSMgmtObject pMgmt = pDevice->pMgmt;
  617. PWLAN_IE_SSID pItemSSID;
  618. unsigned char abyNullAddr[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  619. unsigned char abyWPAIE[64];
  620. int ret = 0;
  621. bool bWepEnabled=false;
  622. // set key type & algorithm
  623. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "pairwise_suite = %d\n", param->u.wpa_associate.pairwise_suite);
  624. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "group_suite = %d\n", param->u.wpa_associate.group_suite);
  625. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "key_mgmt_suite = %d\n", param->u.wpa_associate.key_mgmt_suite);
  626. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "auth_alg = %d\n", param->u.wpa_associate.auth_alg);
  627. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "mode = %d\n", param->u.wpa_associate.mode);
  628. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "wpa_ie_len = %d\n", param->u.wpa_associate.wpa_ie_len);
  629. if (param->u.wpa_associate.wpa_ie_len) {
  630. if (!param->u.wpa_associate.wpa_ie)
  631. return -EINVAL;
  632. if (param->u.wpa_associate.wpa_ie_len > sizeof(abyWPAIE))
  633. return -EINVAL;
  634. if (copy_from_user(&abyWPAIE[0], param->u.wpa_associate.wpa_ie, param->u.wpa_associate.wpa_ie_len))
  635. return -EFAULT;
  636. }
  637. if (param->u.wpa_associate.mode == 1)
  638. pMgmt->eConfigMode = WMAC_CONFIG_IBSS_STA;
  639. else
  640. pMgmt->eConfigMode = WMAC_CONFIG_ESS_STA;
  641. // set ssid
  642. memset(pMgmt->abyDesireSSID, 0, WLAN_IEHDR_LEN + WLAN_SSID_MAXLEN + 1);
  643. pItemSSID = (PWLAN_IE_SSID)pMgmt->abyDesireSSID;
  644. pItemSSID->byElementID = WLAN_EID_SSID;
  645. pItemSSID->len = param->u.wpa_associate.ssid_len;
  646. memcpy(pItemSSID->abySSID, param->u.wpa_associate.ssid, pItemSSID->len);
  647. // set bssid
  648. if (memcmp(param->u.wpa_associate.bssid, &abyNullAddr[0], 6) != 0)
  649. memcpy(pMgmt->abyDesireBSSID, param->u.wpa_associate.bssid, 6);
  650. else
  651. {
  652. bScheduleCommand((void *) pDevice, WLAN_CMD_BSSID_SCAN, pItemSSID->abySSID);
  653. }
  654. if (param->u.wpa_associate.wpa_ie_len == 0) {
  655. if (param->u.wpa_associate.auth_alg & AUTH_ALG_SHARED_KEY)
  656. pMgmt->eAuthenMode = WMAC_AUTH_SHAREKEY;
  657. else
  658. pMgmt->eAuthenMode = WMAC_AUTH_OPEN;
  659. } else if (abyWPAIE[0] == RSN_INFO_ELEM) {
  660. if (param->u.wpa_associate.key_mgmt_suite == KEY_MGMT_PSK)
  661. pMgmt->eAuthenMode = WMAC_AUTH_WPA2PSK;
  662. else
  663. pMgmt->eAuthenMode = WMAC_AUTH_WPA2;
  664. } else {
  665. if (param->u.wpa_associate.key_mgmt_suite == KEY_MGMT_WPA_NONE)
  666. pMgmt->eAuthenMode = WMAC_AUTH_WPANONE;
  667. else if (param->u.wpa_associate.key_mgmt_suite == KEY_MGMT_PSK)
  668. pMgmt->eAuthenMode = WMAC_AUTH_WPAPSK;
  669. else
  670. pMgmt->eAuthenMode = WMAC_AUTH_WPA;
  671. }
  672. switch (param->u.wpa_associate.pairwise_suite) {
  673. case CIPHER_CCMP:
  674. pDevice->eEncryptionStatus = Ndis802_11Encryption3Enabled;
  675. break;
  676. case CIPHER_TKIP:
  677. pDevice->eEncryptionStatus = Ndis802_11Encryption2Enabled;
  678. break;
  679. case CIPHER_WEP40:
  680. case CIPHER_WEP104:
  681. pDevice->eEncryptionStatus = Ndis802_11Encryption1Enabled;
  682. bWepEnabled=true;
  683. break;
  684. case CIPHER_NONE:
  685. if (param->u.wpa_associate.group_suite == CIPHER_CCMP)
  686. pDevice->eEncryptionStatus = Ndis802_11Encryption3Enabled;
  687. else
  688. pDevice->eEncryptionStatus = Ndis802_11Encryption2Enabled;
  689. break;
  690. default:
  691. pDevice->eEncryptionStatus = Ndis802_11EncryptionDisabled;
  692. }
  693. //DavidWang add for WPA_supplicant support open/share mode
  694. if (pMgmt->eAuthenMode == WMAC_AUTH_SHAREKEY) {
  695. pDevice->eEncryptionStatus = Ndis802_11Encryption1Enabled;
  696. //pMgmt->eAuthenMode = WMAC_AUTH_SHAREKEY;
  697. pMgmt->bShareKeyAlgorithm = true;
  698. }
  699. else if (pMgmt->eAuthenMode == WMAC_AUTH_OPEN) {
  700. if(!bWepEnabled) pDevice->eEncryptionStatus = Ndis802_11EncryptionDisabled;
  701. else pDevice->eEncryptionStatus = Ndis802_11Encryption1Enabled;
  702. //pMgmt->eAuthenMode = WMAC_AUTH_OPEN;
  703. //pMgmt->bShareKeyAlgorithm = false; //20080717-06,<Modify> by chester//Fix Open mode, WEP encrytion
  704. }
  705. //mike save old encryption status
  706. pDevice->eOldEncryptionStatus = pDevice->eEncryptionStatus;
  707. if (pDevice->eEncryptionStatus != Ndis802_11EncryptionDisabled)
  708. pDevice->bEncryptionEnable = true;
  709. else
  710. pDevice->bEncryptionEnable = false;
  711. if (!((pMgmt->eAuthenMode == WMAC_AUTH_SHAREKEY) ||
  712. ((pMgmt->eAuthenMode == WMAC_AUTH_OPEN) && (bWepEnabled==true))) ) //DavidWang //20080717-06,<Modify> by chester//Not to initial WEP
  713. KeyvInitTable(&pDevice->sKey, pDevice->PortOffset);
  714. spin_lock_irq(&pDevice->lock);
  715. pDevice->bLinkPass = false;
  716. memset(pMgmt->abyCurrBSSID, 0, 6);
  717. pMgmt->eCurrState = WMAC_STATE_IDLE;
  718. netif_stop_queue(pDevice->dev);
  719. //20080701-02,<Add> by Mike Liu
  720. /*******search if ap_scan=2 ,which is associating request in hidden ssid mode ****/
  721. {
  722. PKnownBSS pCurr = NULL;
  723. pCurr = BSSpSearchBSSList(pDevice,
  724. pMgmt->abyDesireBSSID,
  725. pMgmt->abyDesireSSID,
  726. pMgmt->eConfigPHYMode
  727. );
  728. if (pCurr == NULL){
  729. printk("wpa_set_associate---->hidden mode site survey before associate.......\n");
  730. bScheduleCommand((void *) pDevice, WLAN_CMD_BSSID_SCAN, pMgmt->abyDesireSSID);
  731. }
  732. }
  733. /****************************************************************/
  734. bScheduleCommand((void *) pDevice, WLAN_CMD_SSID, NULL);
  735. spin_unlock_irq(&pDevice->lock);
  736. return ret;
  737. }
  738. /*
  739. * Description:
  740. * wpa_ioctl main function supported for wpa supplicant
  741. *
  742. * Parameters:
  743. * In:
  744. * pDevice -
  745. * iw_point -
  746. * Out:
  747. *
  748. * Return Value:
  749. *
  750. */
  751. int wpa_ioctl(PSDevice pDevice, struct iw_point *p)
  752. {
  753. struct viawget_wpa_param *param;
  754. int ret = 0;
  755. int wpa_ioctl = 0;
  756. if (p->length < sizeof(struct viawget_wpa_param) ||
  757. p->length > VIAWGET_WPA_MAX_BUF_SIZE || !p->pointer)
  758. return -EINVAL;
  759. param = kmalloc((int)p->length, (int)GFP_KERNEL);
  760. if (param == NULL)
  761. return -ENOMEM;
  762. if (copy_from_user(param, p->pointer, p->length)) {
  763. ret = -EFAULT;
  764. goto out;
  765. }
  766. switch (param->cmd) {
  767. case VIAWGET_SET_WPA:
  768. ret = wpa_set_wpa(pDevice, param);
  769. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "VIAWGET_SET_WPA \n");
  770. break;
  771. case VIAWGET_SET_KEY:
  772. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "VIAWGET_SET_KEY \n");
  773. spin_lock_irq(&pDevice->lock);
  774. ret = wpa_set_keys(pDevice, param, false);
  775. spin_unlock_irq(&pDevice->lock);
  776. break;
  777. case VIAWGET_SET_SCAN:
  778. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "VIAWGET_SET_SCAN \n");
  779. ret = wpa_set_scan(pDevice, param);
  780. break;
  781. case VIAWGET_GET_SCAN:
  782. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "VIAWGET_GET_SCAN\n");
  783. ret = wpa_get_scan(pDevice, param);
  784. wpa_ioctl = 1;
  785. break;
  786. case VIAWGET_GET_SSID:
  787. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "VIAWGET_GET_SSID \n");
  788. ret = wpa_get_ssid(pDevice, param);
  789. wpa_ioctl = 1;
  790. break;
  791. case VIAWGET_GET_BSSID:
  792. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "VIAWGET_GET_BSSID \n");
  793. ret = wpa_get_bssid(pDevice, param);
  794. wpa_ioctl = 1;
  795. break;
  796. case VIAWGET_SET_ASSOCIATE:
  797. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "VIAWGET_SET_ASSOCIATE \n");
  798. ret = wpa_set_associate(pDevice, param);
  799. break;
  800. case VIAWGET_SET_DISASSOCIATE:
  801. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "VIAWGET_SET_DISASSOCIATE \n");
  802. ret = wpa_set_disassociate(pDevice, param);
  803. break;
  804. case VIAWGET_SET_DROP_UNENCRYPT:
  805. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "VIAWGET_SET_DROP_UNENCRYPT \n");
  806. break;
  807. case VIAWGET_SET_DEAUTHENTICATE:
  808. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "VIAWGET_SET_DEAUTHENTICATE \n");
  809. break;
  810. default:
  811. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "wpa_ioctl: unknown cmd=%d\n",
  812. param->cmd);
  813. return -EOPNOTSUPP;
  814. break;
  815. }
  816. if ((ret == 0) && wpa_ioctl) {
  817. if (copy_to_user(p->pointer, param, p->length)) {
  818. ret = -EFAULT;
  819. goto out;
  820. }
  821. }
  822. out:
  823. kfree(param);
  824. return ret;
  825. }