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