Merge branch 'release' of git://git.kernel.org/pub/scm/linux/kernel/git/lenb/linux
[firefly-linux-kernel-4.4.55.git] / drivers / staging / vt6655 / hostap.c
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  * File: hostap.c
20  *
21  * Purpose: handle hostap deamon ioctl input/out functions
22  *
23  * Author: Lyndon Chen
24  *
25  * Date: Oct. 20, 2003
26  *
27  * Functions:
28  *
29  * Revision History:
30  *
31  */
32
33 #include "hostap.h"
34 #include "iocmd.h"
35 #include "mac.h"
36 #include "card.h"
37 #include "baseband.h"
38 #include "wpactl.h"
39 #include "key.h"
40
41 #define VIAWGET_HOSTAPD_MAX_BUF_SIZE 1024
42 #define HOSTAP_CRYPT_FLAG_SET_TX_KEY BIT0
43 #define HOSTAP_CRYPT_ERR_UNKNOWN_ADDR 3
44 #define HOSTAP_CRYPT_ERR_KEY_SET_FAILED 5
45
46 /*---------------------  Static Definitions -------------------------*/
47
48 /*---------------------  Static Classes  ----------------------------*/
49
50 /*---------------------  Static Variables  --------------------------*/
51 //static int          msglevel                =MSG_LEVEL_DEBUG;
52 static int          msglevel                =MSG_LEVEL_INFO;
53
54 /*---------------------  Static Functions  --------------------------*/
55
56
57
58
59 /*---------------------  Export Variables  --------------------------*/
60
61
62 /*
63  * Description:
64  *      register net_device (AP) for hostap deamon
65  *
66  * Parameters:
67  *  In:
68  *      pDevice             -
69  *      rtnl_locked         -
70  *  Out:
71  *
72  * Return Value:
73  *
74  */
75
76 static int hostap_enable_hostapd(PSDevice pDevice, int rtnl_locked)
77 {
78     PSDevice apdev_priv;
79         struct net_device *dev = pDevice->dev;
80         int ret;
81         const struct net_device_ops apdev_netdev_ops = {
82                 .ndo_start_xmit         = pDevice->tx_80211,
83         };
84
85     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "%s: Enabling hostapd mode\n", dev->name);
86
87         pDevice->apdev = kzalloc(sizeof(struct net_device), GFP_KERNEL);
88         if (pDevice->apdev == NULL)
89                 return -ENOMEM;
90
91     apdev_priv = netdev_priv(pDevice->apdev);
92     *apdev_priv = *pDevice;
93         memcpy(pDevice->apdev->dev_addr, dev->dev_addr, ETH_ALEN);
94
95         pDevice->apdev->netdev_ops = &apdev_netdev_ops;
96
97         pDevice->apdev->type = ARPHRD_IEEE80211;
98
99         pDevice->apdev->base_addr = dev->base_addr;
100         pDevice->apdev->irq = dev->irq;
101         pDevice->apdev->mem_start = dev->mem_start;
102         pDevice->apdev->mem_end = dev->mem_end;
103         sprintf(pDevice->apdev->name, "%sap", dev->name);
104         if (rtnl_locked)
105                 ret = register_netdevice(pDevice->apdev);
106         else
107                 ret = register_netdev(pDevice->apdev);
108         if (ret) {
109                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "%s: register_netdevice(AP) failed!\n",
110                        dev->name);
111                 return -1;
112         }
113
114     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "%s: Registered netdevice %s for AP management\n",
115                dev->name, pDevice->apdev->name);
116
117     KeyvInitTable(&pDevice->sKey, pDevice->PortOffset);
118
119         return 0;
120 }
121
122 /*
123  * Description:
124  *      unregister net_device(AP)
125  *
126  * Parameters:
127  *  In:
128  *      pDevice             -
129  *      rtnl_locked         -
130  *  Out:
131  *
132  * Return Value:
133  *
134  */
135
136 static int hostap_disable_hostapd(PSDevice pDevice, int rtnl_locked)
137 {
138
139     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "%s: disabling hostapd mode\n", pDevice->dev->name);
140
141     if (pDevice->apdev && pDevice->apdev->name && pDevice->apdev->name[0]) {
142                 if (rtnl_locked)
143                         unregister_netdevice(pDevice->apdev);
144                 else
145                         unregister_netdev(pDevice->apdev);
146             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "%s: Netdevice %s unregistered\n",
147                        pDevice->dev->name, pDevice->apdev->name);
148         }
149         kfree(pDevice->apdev);
150         pDevice->apdev = NULL;
151     pDevice->bEnable8021x = false;
152     pDevice->bEnableHostWEP = false;
153     pDevice->bEncryptionEnable = false;
154
155 //4.2007-0118-03,<Add> by EinsnLiu
156 //execute some clear work
157 pDevice->pMgmt->byCSSPK=KEY_CTL_NONE;
158 pDevice->pMgmt->byCSSGK=KEY_CTL_NONE;
159 KeyvInitTable(&pDevice->sKey,pDevice->PortOffset);
160
161         return 0;
162 }
163
164
165 /*
166  * Description:
167  *      Set enable/disable hostapd mode
168  *
169  * Parameters:
170  *  In:
171  *      pDevice             -
172  *      rtnl_locked         -
173  *  Out:
174  *
175  * Return Value:
176  *
177  */
178
179 int vt6655_hostap_set_hostapd(PSDevice pDevice, int val, int rtnl_locked)
180 {
181         if (val < 0 || val > 1)
182                 return -EINVAL;
183
184         if (pDevice->bEnableHostapd == val)
185                 return 0;
186
187         pDevice->bEnableHostapd = val;
188
189         if (val)
190                 return hostap_enable_hostapd(pDevice, rtnl_locked);
191         else
192                 return hostap_disable_hostapd(pDevice, rtnl_locked);
193 }
194
195
196 /*
197  * Description:
198  *      remove station function supported for hostap deamon
199  *
200  * Parameters:
201  *  In:
202  *      pDevice   -
203  *      param     -
204  *  Out:
205  *
206  * Return Value:
207  *
208  */
209 static int hostap_remove_sta(PSDevice pDevice,
210                                      struct viawget_hostapd_param *param)
211 {
212         unsigned int uNodeIndex;
213
214
215     if (BSSDBbIsSTAInNodeDB(pDevice->pMgmt, param->sta_addr, &uNodeIndex)) {
216         BSSvRemoveOneNode(pDevice, uNodeIndex);
217     }
218     else {
219         return -ENOENT;
220     }
221         return 0;
222 }
223
224 /*
225  * Description:
226  *      add a station from hostap deamon
227  *
228  * Parameters:
229  *  In:
230  *      pDevice   -
231  *      param     -
232  *  Out:
233  *
234  * Return Value:
235  *
236  */
237 static int hostap_add_sta(PSDevice pDevice,
238                                   struct viawget_hostapd_param *param)
239 {
240     PSMgmtObject    pMgmt = pDevice->pMgmt;
241         unsigned int uNodeIndex;
242
243
244     if (!BSSDBbIsSTAInNodeDB(pMgmt, param->sta_addr, &uNodeIndex)) {
245         BSSvCreateOneNode((PSDevice)pDevice, &uNodeIndex);
246     }
247     memcpy(pMgmt->sNodeDBTable[uNodeIndex].abyMACAddr, param->sta_addr, WLAN_ADDR_LEN);
248     pMgmt->sNodeDBTable[uNodeIndex].eNodeState = NODE_ASSOC;
249     pMgmt->sNodeDBTable[uNodeIndex].wCapInfo = param->u.add_sta.capability;
250 // TODO listenInterval
251 //    pMgmt->sNodeDBTable[uNodeIndex].wListenInterval = 1;
252     pMgmt->sNodeDBTable[uNodeIndex].bPSEnable = false;
253     pMgmt->sNodeDBTable[uNodeIndex].bySuppRate = param->u.add_sta.tx_supp_rates;
254
255     // set max tx rate
256     pMgmt->sNodeDBTable[uNodeIndex].wTxDataRate =
257            pMgmt->sNodeDBTable[uNodeIndex].wMaxSuppRate;
258     // set max basic rate
259     pMgmt->sNodeDBTable[uNodeIndex].wMaxBasicRate = RATE_2M;
260     // Todo: check sta preamble, if ap can't support, set status code
261     pMgmt->sNodeDBTable[uNodeIndex].bShortPreamble =
262             WLAN_GET_CAP_INFO_SHORTPREAMBLE(pMgmt->sNodeDBTable[uNodeIndex].wCapInfo);
263
264     pMgmt->sNodeDBTable[uNodeIndex].wAID = (unsigned short)param->u.add_sta.aid;
265
266     pMgmt->sNodeDBTable[uNodeIndex].ulLastRxJiffer = jiffies;
267
268     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Add STA AID= %d \n", pMgmt->sNodeDBTable[uNodeIndex].wAID);
269     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "MAC=%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X \n",
270                param->sta_addr[0],
271                param->sta_addr[1],
272                param->sta_addr[2],
273                param->sta_addr[3],
274                param->sta_addr[4],
275                param->sta_addr[5]
276               ) ;
277     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Max Support rate = %d \n",
278                pMgmt->sNodeDBTable[uNodeIndex].wMaxSuppRate);
279
280         return 0;
281 }
282
283 /*
284  * Description:
285  *      get station info
286  *
287  * Parameters:
288  *  In:
289  *      pDevice   -
290  *      param     -
291  *  Out:
292  *
293  * Return Value:
294  *
295  */
296
297 static int hostap_get_info_sta(PSDevice pDevice,
298                                        struct viawget_hostapd_param *param)
299 {
300     PSMgmtObject    pMgmt = pDevice->pMgmt;
301         unsigned int uNodeIndex;
302
303     if (BSSDBbIsSTAInNodeDB(pMgmt, param->sta_addr, &uNodeIndex)) {
304             param->u.get_info_sta.inactive_sec =
305                 (jiffies - pMgmt->sNodeDBTable[uNodeIndex].ulLastRxJiffer) / HZ;
306
307             //param->u.get_info_sta.txexc = pMgmt->sNodeDBTable[uNodeIndex].uTxAttempts;
308         }
309         else {
310             return -ENOENT;
311         }
312
313         return 0;
314 }
315
316 /*
317  * Description:
318  *      reset txexec
319  *
320  * Parameters:
321  *  In:
322  *      pDevice   -
323  *      param     -
324  *  Out:
325  *      true, false
326  *
327  * Return Value:
328  *
329  */
330 /*
331 static int hostap_reset_txexc_sta(PSDevice pDevice,
332                                           struct viawget_hostapd_param *param)
333 {
334     PSMgmtObject    pMgmt = pDevice->pMgmt;
335         unsigned int uNodeIndex;
336
337     if (BSSDBbIsSTAInNodeDB(pMgmt, param->sta_addr, &uNodeIndex)) {
338         pMgmt->sNodeDBTable[uNodeIndex].uTxAttempts = 0;
339         }
340         else {
341             return -ENOENT;
342         }
343
344         return 0;
345 }
346 */
347
348 /*
349  * Description:
350  *      set station flag
351  *
352  * Parameters:
353  *  In:
354  *      pDevice   -
355  *      param     -
356  *  Out:
357  *
358  * Return Value:
359  *
360  */
361 static int hostap_set_flags_sta(PSDevice pDevice,
362                                         struct viawget_hostapd_param *param)
363 {
364     PSMgmtObject    pMgmt = pDevice->pMgmt;
365         unsigned int uNodeIndex;
366
367     if (BSSDBbIsSTAInNodeDB(pMgmt, param->sta_addr, &uNodeIndex)) {
368                 pMgmt->sNodeDBTable[uNodeIndex].dwFlags |= param->u.set_flags_sta.flags_or;
369                 pMgmt->sNodeDBTable[uNodeIndex].dwFlags &= param->u.set_flags_sta.flags_and;
370                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " dwFlags = %x \n",
371                             (unsigned int)pMgmt->sNodeDBTable[uNodeIndex].dwFlags);
372         }
373         else {
374             return -ENOENT;
375         }
376
377         return 0;
378 }
379
380
381
382 /*
383  * Description:
384  *      set generic element (wpa ie)
385  *
386  * Parameters:
387  *  In:
388  *      pDevice   -
389  *      param     -
390  *  Out:
391  *
392  * Return Value:
393  *
394  */
395 static int hostap_set_generic_element(PSDevice pDevice,
396                                         struct viawget_hostapd_param *param)
397 {
398     PSMgmtObject    pMgmt = pDevice->pMgmt;
399
400
401
402     memcpy( pMgmt->abyWPAIE,
403             param->u.generic_elem.data,
404             param->u.generic_elem.len
405            );
406
407     pMgmt->wWPAIELen =  param->u.generic_elem.len;
408
409     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"pMgmt->wWPAIELen = %d\n",  pMgmt->wWPAIELen);
410
411     // disable wpa
412     if (pMgmt->wWPAIELen == 0) {
413         pMgmt->eAuthenMode = WMAC_AUTH_OPEN;
414                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " No WPAIE, Disable WPA \n");
415     } else  {
416         // enable wpa
417         if ((pMgmt->abyWPAIE[0] == WLAN_EID_RSN_WPA) ||
418              (pMgmt->abyWPAIE[0] == WLAN_EID_RSN)) {
419               pMgmt->eAuthenMode = WMAC_AUTH_WPANONE;
420                DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Set WPAIE enable WPA\n");
421         } else
422             return -EINVAL;
423     }
424
425         return 0;
426 }
427
428 /*
429  * Description:
430  *      flush station nodes table.
431  *
432  * Parameters:
433  *  In:
434  *      pDevice   -
435  *  Out:
436  *
437  * Return Value:
438  *
439  */
440
441 static void hostap_flush_sta(PSDevice pDevice)
442 {
443     // reserved node index =0 for multicast node.
444     BSSvClearNodeDBTable(pDevice, 1);
445     pDevice->uAssocCount = 0;
446
447     return;
448 }
449
450 /*
451  * Description:
452  *      set each stations encryption key
453  *
454  * Parameters:
455  *  In:
456  *      pDevice   -
457  *      param     -
458  *  Out:
459  *
460  * Return Value:
461  *
462  */
463 static int hostap_set_encryption(PSDevice pDevice,
464                                        struct viawget_hostapd_param *param,
465                                        int param_len)
466 {
467     PSMgmtObject    pMgmt = pDevice->pMgmt;
468     unsigned long dwKeyIndex = 0;
469     unsigned char abyKey[MAX_KEY_LEN];
470     unsigned char abySeq[MAX_KEY_LEN];
471     NDIS_802_11_KEY_RSC   KeyRSC;
472     unsigned char byKeyDecMode = KEY_CTL_WEP;
473         int     ret = 0;
474         int     iNodeIndex = -1;
475         int     ii;
476         bool bKeyTableFull = false;
477         unsigned short wKeyCtl = 0;
478
479
480         param->u.crypt.err = 0;
481 /*
482         if (param_len !=
483             (int) ((char *) param->u.crypt.key - (char *) param) +
484             param->u.crypt.key_len)
485                 return -EINVAL;
486 */
487
488         if (param->u.crypt.alg > WPA_ALG_CCMP)
489                 return -EINVAL;
490
491
492         if ((param->u.crypt.idx > 3) || (param->u.crypt.key_len > MAX_KEY_LEN)) {
493                 param->u.crypt.err = HOSTAP_CRYPT_ERR_KEY_SET_FAILED;
494                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " HOSTAP_CRYPT_ERR_KEY_SET_FAILED\n");
495                 return -EINVAL;
496         }
497
498         if (param->sta_addr[0] == 0xff && param->sta_addr[1] == 0xff &&
499             param->sta_addr[2] == 0xff && param->sta_addr[3] == 0xff &&
500             param->sta_addr[4] == 0xff && param->sta_addr[5] == 0xff) {
501                 if (param->u.crypt.idx >= MAX_GROUP_KEY)
502                         return -EINVAL;
503         iNodeIndex = 0;
504
505         } else {
506             if (BSSDBbIsSTAInNodeDB(pMgmt, param->sta_addr, &iNodeIndex) == false) {
507                 param->u.crypt.err = HOSTAP_CRYPT_ERR_UNKNOWN_ADDR;
508             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " HOSTAP_CRYPT_ERR_UNKNOWN_ADDR\n");
509                 return -EINVAL;
510             }
511         }
512     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " hostap_set_encryption: sta_index %d \n", iNodeIndex);
513     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " hostap_set_encryption: alg %d \n", param->u.crypt.alg);
514
515         if (param->u.crypt.alg == WPA_ALG_NONE) {
516
517         if (pMgmt->sNodeDBTable[iNodeIndex].bOnFly == true) {
518             if (KeybRemoveKey(&(pDevice->sKey),
519                                 param->sta_addr,
520                                 pMgmt->sNodeDBTable[iNodeIndex].dwKeyIndex,
521                                 pDevice->PortOffset) == false) {
522                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "KeybRemoveKey fail \n");
523             }
524             pMgmt->sNodeDBTable[iNodeIndex].bOnFly = false;
525         }
526         pMgmt->sNodeDBTable[iNodeIndex].byKeyIndex = 0;
527         pMgmt->sNodeDBTable[iNodeIndex].dwKeyIndex = 0;
528         pMgmt->sNodeDBTable[iNodeIndex].uWepKeyLength = 0;
529         pMgmt->sNodeDBTable[iNodeIndex].KeyRSC = 0;
530         pMgmt->sNodeDBTable[iNodeIndex].dwTSC47_16 = 0;
531         pMgmt->sNodeDBTable[iNodeIndex].wTSC15_0 = 0;
532         pMgmt->sNodeDBTable[iNodeIndex].byCipherSuite = 0;
533         memset(&pMgmt->sNodeDBTable[iNodeIndex].abyWepKey[0],
534                 0,
535                 MAX_KEY_LEN
536                );
537
538         return ret;
539         }
540
541     memcpy(abyKey, param->u.crypt.key, param->u.crypt.key_len);
542     // copy to node key tbl
543     pMgmt->sNodeDBTable[iNodeIndex].byKeyIndex = param->u.crypt.idx;
544     pMgmt->sNodeDBTable[iNodeIndex].uWepKeyLength = param->u.crypt.key_len;
545     memcpy(&pMgmt->sNodeDBTable[iNodeIndex].abyWepKey[0],
546             param->u.crypt.key,
547             param->u.crypt.key_len
548            );
549
550     dwKeyIndex = (unsigned long)(param->u.crypt.idx);
551     if (param->u.crypt.flags & HOSTAP_CRYPT_FLAG_SET_TX_KEY) {
552         pDevice->byKeyIndex = (unsigned char)dwKeyIndex;
553         pDevice->bTransmitKey = true;
554         dwKeyIndex |= (1 << 31);
555     }
556
557         if (param->u.crypt.alg == WPA_ALG_WEP) {
558
559         if ((pDevice->bEnable8021x == false) || (iNodeIndex == 0)) {
560             KeybSetDefaultKey(&(pDevice->sKey),
561                                 dwKeyIndex & ~(BIT30 | USE_KEYRSC),
562                                 param->u.crypt.key_len,
563                                 NULL,
564                                 abyKey,
565                                 KEY_CTL_WEP,
566                                 pDevice->PortOffset,
567                                 pDevice->byLocalID);
568
569         } else {
570             // 8021x enable, individual key
571             dwKeyIndex |= (1 << 30); // set pairwise key
572             if (KeybSetKey(&(pDevice->sKey),
573                            &param->sta_addr[0],
574                            dwKeyIndex & ~(USE_KEYRSC),
575                            param->u.crypt.key_len,
576                            (PQWORD) &(KeyRSC),
577                            (unsigned char *)abyKey,
578                             KEY_CTL_WEP,
579                             pDevice->PortOffset,
580                             pDevice->byLocalID) == true) {
581
582                 pMgmt->sNodeDBTable[iNodeIndex].bOnFly = true;
583
584             } else {
585                 // Key Table Full
586                 pMgmt->sNodeDBTable[iNodeIndex].bOnFly = false;
587                 bKeyTableFull = true;
588             }
589         }
590         pDevice->eEncryptionStatus = Ndis802_11Encryption1Enabled;
591         pDevice->bEncryptionEnable = true;
592         pMgmt->byCSSPK = KEY_CTL_WEP;
593         pMgmt->byCSSGK = KEY_CTL_WEP;
594         pMgmt->sNodeDBTable[iNodeIndex].byCipherSuite = KEY_CTL_WEP;
595         pMgmt->sNodeDBTable[iNodeIndex].dwKeyIndex = dwKeyIndex;
596         return ret;
597         }
598
599         if (param->u.crypt.seq) {
600             memcpy(&abySeq, param->u.crypt.seq, 8);
601                 for (ii = 0 ; ii < 8 ; ii++) {
602                  KeyRSC |= (abySeq[ii] << (ii * 8));
603                 }
604                 dwKeyIndex |= 1 << 29;
605                 pMgmt->sNodeDBTable[iNodeIndex].KeyRSC = KeyRSC;
606         }
607
608         if (param->u.crypt.alg == WPA_ALG_TKIP) {
609             if (param->u.crypt.key_len != MAX_KEY_LEN)
610                 return -EINVAL;
611             pDevice->eEncryptionStatus = Ndis802_11Encryption2Enabled;
612         byKeyDecMode = KEY_CTL_TKIP;
613         pMgmt->byCSSPK = KEY_CTL_TKIP;
614         pMgmt->byCSSGK = KEY_CTL_TKIP;
615         }
616
617         if (param->u.crypt.alg == WPA_ALG_CCMP) {
618             if ((param->u.crypt.key_len != AES_KEY_LEN) ||
619                 (pDevice->byLocalID <= REV_ID_VT3253_A1))
620                 return -EINVAL;
621         pDevice->eEncryptionStatus = Ndis802_11Encryption3Enabled;
622         byKeyDecMode = KEY_CTL_CCMP;
623         pMgmt->byCSSPK = KEY_CTL_CCMP;
624         pMgmt->byCSSGK = KEY_CTL_CCMP;
625     }
626
627
628     if (iNodeIndex == 0) {
629        KeybSetDefaultKey(&(pDevice->sKey),
630                            dwKeyIndex,
631                            param->u.crypt.key_len,
632                            (PQWORD) &(KeyRSC),
633                            abyKey,
634                            byKeyDecMode,
635                            pDevice->PortOffset,
636                            pDevice->byLocalID);
637        pMgmt->sNodeDBTable[iNodeIndex].bOnFly = true;
638
639     } else {
640         dwKeyIndex |= (1 << 30); // set pairwise key
641         if (KeybSetKey(&(pDevice->sKey),
642                        &param->sta_addr[0],
643                        dwKeyIndex,
644                        param->u.crypt.key_len,
645                        (PQWORD) &(KeyRSC),
646                        (unsigned char *)abyKey,
647                         byKeyDecMode,
648                         pDevice->PortOffset,
649                         pDevice->byLocalID) == true) {
650
651             pMgmt->sNodeDBTable[iNodeIndex].bOnFly = true;
652
653         } else {
654             // Key Table Full
655             pMgmt->sNodeDBTable[iNodeIndex].bOnFly = false;
656             bKeyTableFull = true;
657             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " Key Table Full\n");
658         }
659
660     }
661
662     if (bKeyTableFull == true) {
663         wKeyCtl &= 0x7F00;              // clear all key control filed
664         wKeyCtl |= (byKeyDecMode << 4);
665         wKeyCtl |= (byKeyDecMode);
666         wKeyCtl |= 0x0044;              // use group key for all address
667         wKeyCtl |= 0x4000;              // disable KeyTable[MAX_KEY_TABLE-1] on-fly to genernate rx int
668         MACvSetDefaultKeyCtl(pDevice->PortOffset, wKeyCtl, MAX_KEY_TABLE-1, pDevice->byLocalID);
669     }
670
671     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " Set key sta_index= %d \n", iNodeIndex);
672     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " tx_index=%d len=%d \n", param->u.crypt.idx,
673                param->u.crypt.key_len );
674     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " key=%x-%x-%x-%x-%x-xxxxx \n",
675                pMgmt->sNodeDBTable[iNodeIndex].abyWepKey[0],
676                pMgmt->sNodeDBTable[iNodeIndex].abyWepKey[1],
677                pMgmt->sNodeDBTable[iNodeIndex].abyWepKey[2],
678                pMgmt->sNodeDBTable[iNodeIndex].abyWepKey[3],
679                pMgmt->sNodeDBTable[iNodeIndex].abyWepKey[4]
680               );
681
682         // set wep key
683     pDevice->bEncryptionEnable = true;
684     pMgmt->sNodeDBTable[iNodeIndex].byCipherSuite = byKeyDecMode;
685     pMgmt->sNodeDBTable[iNodeIndex].dwKeyIndex = dwKeyIndex;
686     pMgmt->sNodeDBTable[iNodeIndex].dwTSC47_16 = 0;
687     pMgmt->sNodeDBTable[iNodeIndex].wTSC15_0 = 0;
688
689         return ret;
690 }
691
692
693
694 /*
695  * Description:
696  *      get each stations encryption key
697  *
698  * Parameters:
699  *  In:
700  *      pDevice   -
701  *      param     -
702  *  Out:
703  *
704  * Return Value:
705  *
706  */
707 static int hostap_get_encryption(PSDevice pDevice,
708                                        struct viawget_hostapd_param *param,
709                                        int param_len)
710 {
711     PSMgmtObject    pMgmt = pDevice->pMgmt;
712         int     ret = 0;
713         int     ii;
714         int     iNodeIndex =0;
715
716
717         param->u.crypt.err = 0;
718
719         if (param->sta_addr[0] == 0xff && param->sta_addr[1] == 0xff &&
720             param->sta_addr[2] == 0xff && param->sta_addr[3] == 0xff &&
721             param->sta_addr[4] == 0xff && param->sta_addr[5] == 0xff) {
722         iNodeIndex = 0;
723         } else {
724             if (BSSDBbIsSTAInNodeDB(pMgmt, param->sta_addr, &iNodeIndex) == false) {
725                 param->u.crypt.err = HOSTAP_CRYPT_ERR_UNKNOWN_ADDR;
726             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "hostap_get_encryption: HOSTAP_CRYPT_ERR_UNKNOWN_ADDR\n");
727                 return -EINVAL;
728             }
729         }
730         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "hostap_get_encryption: %d\n", iNodeIndex);
731     memset(param->u.crypt.seq, 0, 8);
732     for (ii = 0 ; ii < 8 ; ii++) {
733         param->u.crypt.seq[ii] = (unsigned char)pMgmt->sNodeDBTable[iNodeIndex].KeyRSC >> (ii * 8);
734     }
735
736         return ret;
737 }
738
739
740 /*
741  * Description:
742  *      vt6655_hostap_ioctl main function supported for hostap deamon.
743  *
744  * Parameters:
745  *  In:
746  *      pDevice   -
747  *      iw_point  -
748  *  Out:
749  *
750  * Return Value:
751  *
752  */
753
754 int vt6655_hostap_ioctl(PSDevice pDevice, struct iw_point *p)
755 {
756         struct viawget_hostapd_param *param;
757         int ret = 0;
758         int ap_ioctl = 0;
759
760         if (p->length < sizeof(struct viawget_hostapd_param) ||
761             p->length > VIAWGET_HOSTAPD_MAX_BUF_SIZE || !p->pointer)
762                 return -EINVAL;
763
764         param = kmalloc((int)p->length, (int)GFP_KERNEL);
765         if (param == NULL)
766                 return -ENOMEM;
767
768         if (copy_from_user(param, p->pointer, p->length)) {
769                 ret = -EFAULT;
770                 goto out;
771         }
772
773         switch (param->cmd) {
774         case VIAWGET_HOSTAPD_SET_ENCRYPTION:
775             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "VIAWGET_HOSTAPD_SET_ENCRYPTION \n");
776         spin_lock_irq(&pDevice->lock);
777                 ret = hostap_set_encryption(pDevice, param, p->length);
778         spin_unlock_irq(&pDevice->lock);
779                 break;
780         case VIAWGET_HOSTAPD_GET_ENCRYPTION:
781             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "VIAWGET_HOSTAPD_GET_ENCRYPTION \n");
782         spin_lock_irq(&pDevice->lock);
783                 ret = hostap_get_encryption(pDevice, param, p->length);
784         spin_unlock_irq(&pDevice->lock);
785                 break;
786         case VIAWGET_HOSTAPD_SET_ASSOC_AP_ADDR:
787             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "VIAWGET_HOSTAPD_SET_ASSOC_AP_ADDR \n");
788                 return -EOPNOTSUPP;
789                 break;
790         case VIAWGET_HOSTAPD_FLUSH:
791             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "VIAWGET_HOSTAPD_FLUSH \n");
792         spin_lock_irq(&pDevice->lock);
793         hostap_flush_sta(pDevice);
794         spin_unlock_irq(&pDevice->lock);
795                 break;
796         case VIAWGET_HOSTAPD_ADD_STA:
797             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "VIAWGET_HOSTAPD_ADD_STA \n");
798          spin_lock_irq(&pDevice->lock);
799                  ret = hostap_add_sta(pDevice, param);
800          spin_unlock_irq(&pDevice->lock);
801                 break;
802         case VIAWGET_HOSTAPD_REMOVE_STA:
803             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "VIAWGET_HOSTAPD_REMOVE_STA \n");
804          spin_lock_irq(&pDevice->lock);
805                  ret = hostap_remove_sta(pDevice, param);
806          spin_unlock_irq(&pDevice->lock);
807                 break;
808         case VIAWGET_HOSTAPD_GET_INFO_STA:
809             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "VIAWGET_HOSTAPD_GET_INFO_STA \n");
810                  ret = hostap_get_info_sta(pDevice, param);
811                  ap_ioctl = 1;
812                 break;
813 /*
814         case VIAWGET_HOSTAPD_RESET_TXEXC_STA:
815             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "VIAWGET_HOSTAPD_RESET_TXEXC_STA \n");
816                  ret = hostap_reset_txexc_sta(pDevice, param);
817                 break;
818 */
819         case VIAWGET_HOSTAPD_SET_FLAGS_STA:
820             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "VIAWGET_HOSTAPD_SET_FLAGS_STA \n");
821                  ret = hostap_set_flags_sta(pDevice, param);
822                 break;
823
824         case VIAWGET_HOSTAPD_MLME:
825             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "VIAWGET_HOSTAPD_MLME \n");
826             return -EOPNOTSUPP;
827
828         case VIAWGET_HOSTAPD_SET_GENERIC_ELEMENT:
829             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "VIAWGET_HOSTAPD_SET_GENERIC_ELEMENT \n");
830                 ret = hostap_set_generic_element(pDevice, param);
831                 break;
832
833         case VIAWGET_HOSTAPD_SCAN_REQ:
834             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "VIAWGET_HOSTAPD_SCAN_REQ \n");
835             return -EOPNOTSUPP;
836
837         case VIAWGET_HOSTAPD_STA_CLEAR_STATS:
838             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "VIAWGET_HOSTAPD_STA_CLEAR_STATS \n");
839             return -EOPNOTSUPP;
840
841         default:
842             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "vt6655_hostap_ioctl: unknown cmd=%d\n",
843                        (int)param->cmd);
844                 return -EOPNOTSUPP;
845                 break;
846         }
847
848
849         if ((ret == 0) && ap_ioctl) {
850                 if (copy_to_user(p->pointer, param, p->length)) {
851                         ret = -EFAULT;
852                         goto out;
853                 }
854         }
855
856  out:
857         kfree(param);
858
859         return ret;
860 }
861