Linux Audio

Check our new training course

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