wpactl.c 27 KB

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