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