Staging: Add pristine upstream vt6655 driver sources
[pandora-kernel.git] / drivers / staging / vt6655 / key.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  *
20  * File: key.c
21  *
22  * Purpose: Implement functions for 802.11i Key management
23  *
24  * Author: Jerry Chen
25  *
26  * Date: May 29, 2003
27  *
28  * Functions:
29  *      KeyvInitTable - Init Key management table
30  *      KeybGetKey - Get Key from table
31  *      KeybSetKey - Set Key to table
32  *      KeybRemoveKey - Remove Key from table
33  *      KeybGetTransmitKey - Get Transmit Key from table
34  *
35  * Revision History:
36  *
37  */
38
39
40 #if !defined(__TMACRO_H__)
41 #include "tmacro.h"
42 #endif
43 #if !defined(__TBIT_H__)
44 #include "tbit.h"
45 #endif
46 #if !defined(__KEY_H__)
47 #include "key.h"
48 #endif
49 #if !defined(__UMEM_H__)
50 #include "umem.h"
51 #endif
52 #if !defined(__MAC_H__)
53 #include "mac.h"
54 #endif
55
56
57 /*---------------------  Static Definitions -------------------------*/
58
59 /*---------------------  Static Classes  ----------------------------*/
60
61 /*---------------------  Static Variables  --------------------------*/
62 static int          msglevel                =MSG_LEVEL_INFO;
63 //static int          msglevel                =MSG_LEVEL_DEBUG;
64 /*---------------------  Static Functions  --------------------------*/
65
66 /*---------------------  Export Variables  --------------------------*/
67
68 /*---------------------  Static Definitions -------------------------*/
69
70 /*---------------------  Static Classes  ----------------------------*/
71
72 /*---------------------  Static Variables  --------------------------*/
73
74 /*---------------------  Static Functions  --------------------------*/
75 static VOID
76 s_vCheckKeyTableValid (PSKeyManagement pTable, DWORD_PTR dwIoBase)
77 {
78     int i;
79
80     for (i=0;i<MAX_KEY_TABLE;i++) {
81         if ((pTable->KeyTable[i].bInUse == TRUE) &&
82             (pTable->KeyTable[i].PairwiseKey.bKeyValid == FALSE) &&
83             (pTable->KeyTable[i].GroupKey[0].bKeyValid == FALSE) &&
84             (pTable->KeyTable[i].GroupKey[1].bKeyValid == FALSE) &&
85             (pTable->KeyTable[i].GroupKey[2].bKeyValid == FALSE) &&
86             (pTable->KeyTable[i].GroupKey[3].bKeyValid == FALSE)
87             ) {
88             pTable->KeyTable[i].bInUse = FALSE;
89             pTable->KeyTable[i].wKeyCtl = 0;
90             pTable->KeyTable[i].bSoftWEP = FALSE;
91             MACvDisableKeyEntry(dwIoBase, i);
92         }
93     }
94 }
95
96
97 /*---------------------  Export Functions  --------------------------*/
98
99
100 /*
101  * Description: Init Key management table
102  *
103  * Parameters:
104  *  In:
105  *      pTable          - Pointer to Key table
106  *  Out:
107  *      none
108  *
109  * Return Value: none
110  *
111  */
112 VOID KeyvInitTable (PSKeyManagement pTable, DWORD_PTR dwIoBase)
113 {
114     int i;
115     int jj;
116
117     for (i=0;i<MAX_KEY_TABLE;i++) {
118         pTable->KeyTable[i].bInUse = FALSE;
119         pTable->KeyTable[i].PairwiseKey.bKeyValid = FALSE;
120         pTable->KeyTable[i].PairwiseKey.pvKeyTable = (PVOID)&pTable->KeyTable[i];
121         for (jj=0; jj < MAX_GROUP_KEY; jj++) {
122             pTable->KeyTable[i].GroupKey[jj].bKeyValid = FALSE;
123             pTable->KeyTable[i].GroupKey[jj].pvKeyTable = (PVOID)&pTable->KeyTable[i];
124         }
125         pTable->KeyTable[i].wKeyCtl = 0;
126         pTable->KeyTable[i].dwGTKeyIndex = 0;
127         pTable->KeyTable[i].bSoftWEP = FALSE;
128         MACvDisableKeyEntry(dwIoBase, i);
129     }
130 }
131
132
133 /*
134  * Description: Get Key from table
135  *
136  * Parameters:
137  *  In:
138  *      pTable          - Pointer to Key table
139  *      pbyBSSID        - BSSID of Key
140  *      dwKeyIndex      - Key Index (0xFFFFFFFF means pairwise key)
141  *  Out:
142  *      pKey            - Key return
143  *
144  * Return Value: TRUE if found otherwise FALSE
145  *
146  */
147 BOOL KeybGetKey (
148     IN  PSKeyManagement pTable,
149     IN  PBYTE           pbyBSSID,
150     IN  DWORD           dwKeyIndex,
151     OUT PSKeyItem       *pKey
152     )
153 {
154     int i;
155
156     DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO"KeybGetKey() \n");
157
158     *pKey = NULL;
159     for (i=0;i<MAX_KEY_TABLE;i++) {
160         if ((pTable->KeyTable[i].bInUse == TRUE) &&
161             IS_ETH_ADDRESS_EQUAL(pTable->KeyTable[i].abyBSSID,pbyBSSID)) {
162             if (dwKeyIndex == 0xFFFFFFFF) {
163                 if (pTable->KeyTable[i].PairwiseKey.bKeyValid == TRUE) {
164                     *pKey = &(pTable->KeyTable[i].PairwiseKey);
165                     return (TRUE);
166                 }
167                 else {
168                     return (FALSE);
169                 }
170             } else if (dwKeyIndex < MAX_GROUP_KEY) {
171                 if (pTable->KeyTable[i].GroupKey[dwKeyIndex].bKeyValid == TRUE) {
172                     *pKey = &(pTable->KeyTable[i].GroupKey[dwKeyIndex]);
173                     return (TRUE);
174                 }
175                 else {
176                     return (FALSE);
177                 }
178             }
179             else {
180                 return (FALSE);
181             }
182         }
183     }
184     return (FALSE);
185 }
186
187
188 /*
189  * Description: Set Key to table
190  *
191  * Parameters:
192  *  In:
193  *      pTable          - Pointer to Key table
194  *      pbyBSSID        - BSSID of Key
195  *      dwKeyIndex      - Key index (reference to NDIS DDK)
196  *      uKeyLength      - Key length
197  *      KeyRSC          - Key RSC
198  *      pbyKey          - Pointer to key
199  *  Out:
200  *      none
201  *
202  * Return Value: TRUE if success otherwise FALSE
203  *
204  */
205 BOOL KeybSetKey (
206     PSKeyManagement pTable,
207     PBYTE           pbyBSSID,
208     DWORD           dwKeyIndex,
209     ULONG           uKeyLength,
210     PQWORD          pKeyRSC,
211     PBYTE           pbyKey,
212     BYTE            byKeyDecMode,
213     DWORD_PTR       dwIoBase,
214     BYTE            byLocalID
215     )
216 {
217     int         i,j;
218     UINT        ii;
219     PSKeyItem   pKey;
220     UINT        uKeyIdx;
221
222     DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Enter KeybSetKey: %lX\n", dwKeyIndex);
223
224     j = (MAX_KEY_TABLE-1);
225     for (i=0;i<(MAX_KEY_TABLE-1);i++) {
226         if ((pTable->KeyTable[i].bInUse == FALSE) &&
227             (j == (MAX_KEY_TABLE-1))) {
228             // found empty table
229             j = i;
230         }
231         if ((pTable->KeyTable[i].bInUse == TRUE) &&
232             IS_ETH_ADDRESS_EQUAL(pTable->KeyTable[i].abyBSSID,pbyBSSID)) {
233             // found table already exist
234             if ((dwKeyIndex & PAIRWISE_KEY) != 0) {
235                 // Pairwise key
236                 pKey = &(pTable->KeyTable[i].PairwiseKey);
237                 pTable->KeyTable[i].wKeyCtl &= 0xFFF0;          // clear pairwise key control filed
238                 pTable->KeyTable[i].wKeyCtl |= byKeyDecMode;
239                 uKeyIdx = 4;                                    // use HW key entry 4 for pairwise key
240             } else {
241                 // Group key
242                 if ((dwKeyIndex & 0x000000FF) >= MAX_GROUP_KEY)
243                     return (FALSE);
244                 pKey = &(pTable->KeyTable[i].GroupKey[dwKeyIndex & 0x000000FF]);
245                 if ((dwKeyIndex & TRANSMIT_KEY) != 0)  {
246                     // Group transmit key
247                     pTable->KeyTable[i].dwGTKeyIndex = dwKeyIndex;
248                     DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Group transmit key(R)[%lX]: %d\n", pTable->KeyTable[i].dwGTKeyIndex, i);
249                 }
250                 pTable->KeyTable[i].wKeyCtl &= 0xFF0F;          // clear group key control filed
251                 pTable->KeyTable[i].wKeyCtl |= (byKeyDecMode << 4);
252                 pTable->KeyTable[i].wKeyCtl |= 0x0040;          // use group key for group address
253                 uKeyIdx = (dwKeyIndex & 0x000000FF);
254             }
255             pTable->KeyTable[i].wKeyCtl |= 0x8000;              // enable on-fly
256
257             pKey->bKeyValid = TRUE;
258             pKey->uKeyLength = uKeyLength;
259             pKey->dwKeyIndex = dwKeyIndex;
260             pKey->byCipherSuite = byKeyDecMode;
261             MEMvCopy(pKey->abyKey, pbyKey, uKeyLength);
262             if (byKeyDecMode == KEY_CTL_WEP) {
263                 if (uKeyLength == WLAN_WEP40_KEYLEN)
264                     pKey->abyKey[15] &= 0x7F;
265                 if (uKeyLength == WLAN_WEP104_KEYLEN)
266                     pKey->abyKey[15] |= 0x80;
267             }
268             MACvSetKeyEntry(dwIoBase, pTable->KeyTable[i].wKeyCtl, i, uKeyIdx, pbyBSSID, (PDWORD)pKey->abyKey, byLocalID);
269
270             if ((dwKeyIndex & USE_KEYRSC) == 0) {
271                 // RSC set by NIC
272                 ZERO_MEMORY(&(pKey->KeyRSC), sizeof(QWORD));
273             }
274             else {
275                 MEMvCopy(&(pKey->KeyRSC), pKeyRSC,  sizeof(QWORD));
276             }
277             pKey->dwTSC47_16 = 0;
278             pKey->wTSC15_0 = 0;
279
280             DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO"KeybSetKey(R): \n");
281             DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO"pKey->bKeyValid: %d\n ", pKey->bKeyValid);
282             //DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO"pKey->uKeyLength: %d\n ", pKey->uKeyLength);
283             DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO"pKey->abyKey: ");
284             for (ii = 0; ii < pKey->uKeyLength; ii++) {
285                 DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO "%02x ", pKey->abyKey[ii]);
286             }
287             DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO"\n");
288
289             DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO"pKey->dwTSC47_16: %lx\n ", pKey->dwTSC47_16);
290             DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO"pKey->wTSC15_0: %x\n ", pKey->wTSC15_0);
291             DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO"pKey->dwKeyIndex: %lx\n ", pKey->dwKeyIndex);
292
293             return (TRUE);
294         }
295     }
296     if (j < (MAX_KEY_TABLE-1)) {
297         MEMvCopy(pTable->KeyTable[j].abyBSSID,pbyBSSID,U_ETHER_ADDR_LEN);
298         pTable->KeyTable[j].bInUse = TRUE;
299         if ((dwKeyIndex & PAIRWISE_KEY) != 0)  {
300             // Pairwise key
301             pKey = &(pTable->KeyTable[j].PairwiseKey);
302             pTable->KeyTable[j].wKeyCtl &= 0xFFF0;          // clear pairwise key control filed
303             pTable->KeyTable[j].wKeyCtl |= byKeyDecMode;
304             uKeyIdx = 4;                                    // use HW key entry 4 for pairwise key
305         } else {
306             // Group key
307             if ((dwKeyIndex & 0x000000FF) >= MAX_GROUP_KEY)
308                 return (FALSE);
309             pKey = &(pTable->KeyTable[j].GroupKey[dwKeyIndex & 0x000000FF]);
310             if ((dwKeyIndex & TRANSMIT_KEY) != 0)  {
311                 // Group transmit key
312                 pTable->KeyTable[j].dwGTKeyIndex = dwKeyIndex;
313                 DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Group transmit key(N)[%lX]: %d\n", pTable->KeyTable[j].dwGTKeyIndex, j);
314             }
315             pTable->KeyTable[j].wKeyCtl &= 0xFF0F;          // clear group key control filed
316             pTable->KeyTable[j].wKeyCtl |= (byKeyDecMode << 4);
317             pTable->KeyTable[j].wKeyCtl |= 0x0040;          // use group key for group address
318             uKeyIdx = (dwKeyIndex & 0x000000FF);
319         }
320         pTable->KeyTable[j].wKeyCtl |= 0x8000;              // enable on-fly
321
322         pKey->bKeyValid = TRUE;
323         pKey->uKeyLength = uKeyLength;
324         pKey->dwKeyIndex = dwKeyIndex;
325         pKey->byCipherSuite = byKeyDecMode;
326         MEMvCopy(pKey->abyKey, pbyKey, uKeyLength);
327         if (byKeyDecMode == KEY_CTL_WEP) {
328             if (uKeyLength == WLAN_WEP40_KEYLEN)
329                 pKey->abyKey[15] &= 0x7F;
330             if (uKeyLength == WLAN_WEP104_KEYLEN)
331                 pKey->abyKey[15] |= 0x80;
332         }
333         MACvSetKeyEntry(dwIoBase, pTable->KeyTable[j].wKeyCtl, j, uKeyIdx, pbyBSSID, (PDWORD)pKey->abyKey, byLocalID);
334
335         if ((dwKeyIndex & USE_KEYRSC) == 0) {
336             // RSC set by NIC
337             ZERO_MEMORY(&(pKey->KeyRSC), sizeof(QWORD));
338         }
339         else {
340             MEMvCopy(&(pKey->KeyRSC), pKeyRSC,  sizeof(QWORD));
341         }
342         pKey->dwTSC47_16 = 0;
343         pKey->wTSC15_0 = 0;
344
345         DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO"KeybSetKey(N): \n");
346         DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO"pKey->bKeyValid: %d\n ", pKey->bKeyValid);
347         DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO"pKey->uKeyLength: %d\n ", (int)pKey->uKeyLength);
348         DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO"pKey->abyKey: ");
349         for (ii = 0; ii < pKey->uKeyLength; ii++) {
350             DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO "%02x ", pKey->abyKey[ii]);
351         }
352         DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO"\n");
353
354         DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO"pKey->dwTSC47_16: %lx\n ", pKey->dwTSC47_16);
355         DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO"pKey->wTSC15_0: %x\n ", pKey->wTSC15_0);
356         DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO"pKey->dwKeyIndex: %lx\n ", pKey->dwKeyIndex);
357
358         return (TRUE);
359     }
360     return (FALSE);
361 }
362
363
364 /*
365  * Description: Remove Key from table
366  *
367  * Parameters:
368  *  In:
369  *      pTable          - Pointer to Key table
370  *      pbyBSSID        - BSSID of Key
371  *      dwKeyIndex      - Key Index (reference to NDIS DDK)
372  *  Out:
373  *      none
374  *
375  * Return Value: TRUE if success otherwise FALSE
376  *
377  */
378 BOOL KeybRemoveKey (
379     PSKeyManagement pTable,
380     PBYTE           pbyBSSID,
381     DWORD           dwKeyIndex,
382     DWORD_PTR       dwIoBase
383     )
384 {
385     int  i;
386
387     if (IS_BROADCAST_ADDRESS(pbyBSSID)) {
388         // dealte all key
389         if ((dwKeyIndex & PAIRWISE_KEY) != 0) {
390             for (i=0;i<MAX_KEY_TABLE;i++) {
391                 pTable->KeyTable[i].PairwiseKey.bKeyValid = FALSE;
392             }
393             s_vCheckKeyTableValid(pTable, dwIoBase);
394             return TRUE;
395         }
396         else if ((dwKeyIndex & 0x000000FF) < MAX_GROUP_KEY) {
397             for (i=0;i<MAX_KEY_TABLE;i++) {
398                 pTable->KeyTable[i].GroupKey[dwKeyIndex & 0x000000FF].bKeyValid = FALSE;
399                 if ((dwKeyIndex & 0x7FFFFFFF) == (pTable->KeyTable[i].dwGTKeyIndex & 0x7FFFFFFF)) {
400                     // remove Group transmit key
401                     pTable->KeyTable[i].dwGTKeyIndex = 0;
402                 }
403             }
404             s_vCheckKeyTableValid(pTable, dwIoBase);
405             return TRUE;
406         }
407         else {
408             return FALSE;
409         }
410     }
411
412     for (i=0;i<MAX_KEY_TABLE;i++) {
413         if ((pTable->KeyTable[i].bInUse == TRUE) &&
414             IS_ETH_ADDRESS_EQUAL(pTable->KeyTable[i].abyBSSID,pbyBSSID)) {
415             if ((dwKeyIndex & PAIRWISE_KEY) != 0) {
416                 pTable->KeyTable[i].PairwiseKey.bKeyValid = FALSE;
417                 s_vCheckKeyTableValid(pTable, dwIoBase);
418                 return (TRUE);
419             }
420             else if ((dwKeyIndex & 0x000000FF) < MAX_GROUP_KEY) {
421                 pTable->KeyTable[i].GroupKey[dwKeyIndex & 0x000000FF].bKeyValid = FALSE;
422                 if ((dwKeyIndex & 0x7FFFFFFF) == (pTable->KeyTable[i].dwGTKeyIndex & 0x7FFFFFFF)) {
423                     // remove Group transmit key
424                     pTable->KeyTable[i].dwGTKeyIndex = 0;
425                 }
426                 s_vCheckKeyTableValid(pTable, dwIoBase);
427                 return (TRUE);
428             }
429             else {
430                 return (FALSE);
431             }
432         }
433     }
434     return (FALSE);
435 }
436
437
438 /*
439  * Description: Remove Key from table
440  *
441  * Parameters:
442  *  In:
443  *      pTable          - Pointer to Key table
444  *      pbyBSSID        - BSSID of Key
445  *  Out:
446  *      none
447  *
448  * Return Value: TRUE if success otherwise FALSE
449  *
450  */
451 BOOL KeybRemoveAllKey (
452     PSKeyManagement pTable,
453     PBYTE           pbyBSSID,
454     DWORD_PTR       dwIoBase
455     )
456 {
457     int  i,u;
458
459     for (i=0;i<MAX_KEY_TABLE;i++) {
460         if ((pTable->KeyTable[i].bInUse == TRUE) &&
461             IS_ETH_ADDRESS_EQUAL(pTable->KeyTable[i].abyBSSID,pbyBSSID)) {
462             pTable->KeyTable[i].PairwiseKey.bKeyValid = FALSE;
463             for(u=0;u<MAX_GROUP_KEY;u++) {
464                 pTable->KeyTable[i].GroupKey[u].bKeyValid = FALSE;
465             }
466             pTable->KeyTable[i].dwGTKeyIndex = 0;
467             s_vCheckKeyTableValid(pTable, dwIoBase);
468             return (TRUE);
469         }
470     }
471     return (FALSE);
472 }
473
474 /*
475  * Description: Remove WEP Key from table
476  *
477  * Parameters:
478  *  In:
479  *      pTable          - Pointer to Key table
480  *  Out:
481  *      none
482  *
483  * Return Value: TRUE if success otherwise FALSE
484  *
485  */
486 VOID KeyvRemoveWEPKey (
487     PSKeyManagement pTable,
488     DWORD           dwKeyIndex,
489     DWORD_PTR       dwIoBase
490     )
491 {
492
493    if ((dwKeyIndex & 0x000000FF) < MAX_GROUP_KEY) {
494         if (pTable->KeyTable[MAX_KEY_TABLE-1].bInUse == TRUE) {
495             if (pTable->KeyTable[MAX_KEY_TABLE-1].GroupKey[dwKeyIndex & 0x000000FF].byCipherSuite == KEY_CTL_WEP) {
496                 pTable->KeyTable[MAX_KEY_TABLE-1].GroupKey[dwKeyIndex & 0x000000FF].bKeyValid = FALSE;
497                 if ((dwKeyIndex & 0x7FFFFFFF) == (pTable->KeyTable[MAX_KEY_TABLE-1].dwGTKeyIndex & 0x7FFFFFFF)) {
498                     // remove Group transmit key
499                     pTable->KeyTable[MAX_KEY_TABLE-1].dwGTKeyIndex = 0;
500                 }
501             }
502         }
503         s_vCheckKeyTableValid(pTable, dwIoBase);
504     }
505     return;
506 }
507
508 VOID KeyvRemoveAllWEPKey (
509     PSKeyManagement pTable,
510     DWORD_PTR       dwIoBase
511     )
512 {
513     int i;
514
515     for(i=0;i<MAX_GROUP_KEY;i++) {
516         KeyvRemoveWEPKey(pTable, i, dwIoBase);
517     }
518 }
519
520 /*
521  * Description: Get Transmit Key from table
522  *
523  * Parameters:
524  *  In:
525  *      pTable          - Pointer to Key table
526  *      pbyBSSID        - BSSID of Key
527  *  Out:
528  *      pKey            - Key return
529  *
530  * Return Value: TRUE if found otherwise FALSE
531  *
532  */
533 BOOL KeybGetTransmitKey (
534     IN  PSKeyManagement pTable,
535     IN  PBYTE           pbyBSSID,
536     IN  DWORD           dwKeyType,
537     OUT PSKeyItem       *pKey
538     )
539 {
540     int i, ii;
541
542     *pKey = NULL;
543     for (i=0;i<MAX_KEY_TABLE;i++) {
544         if ((pTable->KeyTable[i].bInUse == TRUE) &&
545             IS_ETH_ADDRESS_EQUAL(pTable->KeyTable[i].abyBSSID,pbyBSSID)) {
546
547             if (dwKeyType == PAIRWISE_KEY) {
548
549                 if (pTable->KeyTable[i].PairwiseKey.bKeyValid == TRUE) {
550                     *pKey = &(pTable->KeyTable[i].PairwiseKey);
551
552                     DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO"KeybGetTransmitKey:");
553                     DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO"PAIRWISE_KEY: KeyTable.abyBSSID: ");
554                     for (ii = 0; ii < 6; ii++) {
555                         DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO"%x ", pTable->KeyTable[i].abyBSSID[ii]);
556                     }
557                     DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO"\n");
558
559
560                     return (TRUE);
561                 }
562                 else {
563                     DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO"PairwiseKey.bKeyValid == FALSE\n");
564                     return (FALSE);
565                 }
566             } // End of Type == PAIRWISE
567             else {
568                 if (pTable->KeyTable[i].dwGTKeyIndex == 0) {
569                     DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO"ERROR: dwGTKeyIndex == 0 !!!\n");
570                     return FALSE;
571                 }
572                 if (pTable->KeyTable[i].GroupKey[(pTable->KeyTable[i].dwGTKeyIndex&0x000000FF)].bKeyValid == TRUE) {
573                     *pKey = &(pTable->KeyTable[i].GroupKey[(pTable->KeyTable[i].dwGTKeyIndex&0x000000FF)]);
574
575                         DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO"KeybGetTransmitKey:");
576                         DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO"GROUP_KEY: KeyTable.abyBSSID\n");
577                         for (ii = 0; ii < 6; ii++) {
578                             DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO"%x ", pTable->KeyTable[i].abyBSSID[ii]);
579                         }
580                         DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO"\n");
581                         DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO"dwGTKeyIndex: %lX\n", pTable->KeyTable[i].dwGTKeyIndex);
582
583                     return (TRUE);
584                 }
585                 else {
586                     DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO"GroupKey.bKeyValid == FALSE\n");
587                     return (FALSE);
588                 }
589             } // End of Type = GROUP
590         } // BSSID match
591     }
592     DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO"ERROR: NO Match BSSID !!! ");
593     for (ii = 0; ii < 6; ii++) {
594         DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO"%02x ", *(pbyBSSID+ii));
595     }
596     DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO"\n");
597     return (FALSE);
598 }
599
600
601 /*
602  * Description: Check Pairewise Key
603  *
604  * Parameters:
605  *  In:
606  *      pTable          - Pointer to Key table
607  *  Out:
608  *      none
609  *
610  * Return Value: TRUE if found otherwise FALSE
611  *
612  */
613 BOOL KeybCheckPairewiseKey (
614     IN  PSKeyManagement pTable,
615     OUT PSKeyItem       *pKey
616     )
617 {
618     int i;
619
620     *pKey = NULL;
621     for (i=0;i<MAX_KEY_TABLE;i++) {
622         if ((pTable->KeyTable[i].bInUse == TRUE) &&
623             (pTable->KeyTable[i].PairwiseKey.bKeyValid == TRUE)) {
624             *pKey = &(pTable->KeyTable[i].PairwiseKey);
625             return (TRUE);
626         }
627     }
628     return (FALSE);
629 }
630
631 /*
632  * Description: Set Key to table
633  *
634  * Parameters:
635  *  In:
636  *      pTable          - Pointer to Key table
637  *      dwKeyIndex      - Key index (reference to NDIS DDK)
638  *      uKeyLength      - Key length
639  *      KeyRSC          - Key RSC
640  *      pbyKey          - Pointer to key
641  *  Out:
642  *      none
643  *
644  * Return Value: TRUE if success otherwise FALSE
645  *
646  */
647 BOOL KeybSetDefaultKey (
648     PSKeyManagement pTable,
649     DWORD           dwKeyIndex,
650     ULONG           uKeyLength,
651     PQWORD          pKeyRSC,
652     PBYTE           pbyKey,
653     BYTE            byKeyDecMode,
654     DWORD_PTR       dwIoBase,
655     BYTE            byLocalID
656     )
657 {
658     UINT        ii;
659     PSKeyItem   pKey;
660     UINT        uKeyIdx;
661
662     DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Enter KeybSetDefaultKey: %1x, %d \n", (int)dwKeyIndex, (int)uKeyLength);
663
664
665     if ((dwKeyIndex & PAIRWISE_KEY) != 0) {                  // Pairwise key
666         return (FALSE);
667     } else if ((dwKeyIndex & 0x000000FF) >= MAX_GROUP_KEY) {
668         return (FALSE);
669     }
670
671     pTable->KeyTable[MAX_KEY_TABLE-1].bInUse = TRUE;
672     for(ii=0;ii<U_ETHER_ADDR_LEN;ii++)
673         pTable->KeyTable[MAX_KEY_TABLE-1].abyBSSID[ii] = 0xFF;
674
675     // Group key
676     pKey = &(pTable->KeyTable[MAX_KEY_TABLE-1].GroupKey[dwKeyIndex & 0x000000FF]);
677     if ((dwKeyIndex & TRANSMIT_KEY) != 0)  {
678         // Group transmit key
679         pTable->KeyTable[MAX_KEY_TABLE-1].dwGTKeyIndex = dwKeyIndex;
680         DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Group transmit key(R)[%lX]: %d\n", pTable->KeyTable[MAX_KEY_TABLE-1].dwGTKeyIndex, MAX_KEY_TABLE-1);
681
682     }
683     pTable->KeyTable[MAX_KEY_TABLE-1].wKeyCtl &= 0x7F00;          // clear all key control filed
684     pTable->KeyTable[MAX_KEY_TABLE-1].wKeyCtl |= (byKeyDecMode << 4);
685     pTable->KeyTable[MAX_KEY_TABLE-1].wKeyCtl |= (byKeyDecMode);
686     pTable->KeyTable[MAX_KEY_TABLE-1].wKeyCtl |= 0x0044;          // use group key for all address
687     uKeyIdx = (dwKeyIndex & 0x000000FF);
688
689     if ((uKeyLength == WLAN_WEP232_KEYLEN) &&
690         (byKeyDecMode == KEY_CTL_WEP)) {
691         pTable->KeyTable[MAX_KEY_TABLE-1].wKeyCtl |= 0x4000;              // disable on-fly disable address match
692         pTable->KeyTable[MAX_KEY_TABLE-1].bSoftWEP = TRUE;
693     } else {
694         if (pTable->KeyTable[MAX_KEY_TABLE-1].bSoftWEP == FALSE)
695             pTable->KeyTable[MAX_KEY_TABLE-1].wKeyCtl |= 0xC000;          // enable on-fly disable address match
696     }
697
698     pKey->bKeyValid = TRUE;
699     pKey->uKeyLength = uKeyLength;
700     pKey->dwKeyIndex = dwKeyIndex;
701     pKey->byCipherSuite = byKeyDecMode;
702     MEMvCopy(pKey->abyKey, pbyKey, uKeyLength);
703     if (byKeyDecMode == KEY_CTL_WEP) {
704         if (uKeyLength == WLAN_WEP40_KEYLEN)
705             pKey->abyKey[15] &= 0x7F;
706         if (uKeyLength == WLAN_WEP104_KEYLEN)
707             pKey->abyKey[15] |= 0x80;
708     }
709     MACvSetKeyEntry(dwIoBase, pTable->KeyTable[MAX_KEY_TABLE-1].wKeyCtl, MAX_KEY_TABLE-1, uKeyIdx, pTable->KeyTable[MAX_KEY_TABLE-1].abyBSSID, (PDWORD)pKey->abyKey, byLocalID);
710
711     if ((dwKeyIndex & USE_KEYRSC) == 0) {
712         // RSC set by NIC
713         ZERO_MEMORY(&(pKey->KeyRSC), sizeof(QWORD));
714     } else {
715         MEMvCopy(&(pKey->KeyRSC), pKeyRSC,  sizeof(QWORD));
716     }
717     pKey->dwTSC47_16 = 0;
718     pKey->wTSC15_0 = 0;
719
720
721     DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO"KeybSetKey(R): \n");
722     DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO"pKey->bKeyValid: %d\n", pKey->bKeyValid);
723     DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO"pKey->uKeyLength: %d\n", (int)pKey->uKeyLength);
724     DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO"pKey->abyKey: \n");
725     for (ii = 0; ii < pKey->uKeyLength; ii++) {
726         DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO"%x", pKey->abyKey[ii]);
727     }
728     DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO"\n");
729
730     DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO"pKey->dwTSC47_16: %lx\n", pKey->dwTSC47_16);
731     DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO"pKey->wTSC15_0: %x\n", pKey->wTSC15_0);
732     DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO"pKey->dwKeyIndex: %lx\n", pKey->dwKeyIndex);
733
734     return (TRUE);
735 }
736
737
738 /*
739  * Description: Set Key to table
740  *
741  * Parameters:
742  *  In:
743  *      pTable          - Pointer to Key table
744  *      dwKeyIndex      - Key index (reference to NDIS DDK)
745  *      uKeyLength      - Key length
746  *      KeyRSC          - Key RSC
747  *      pbyKey          - Pointer to key
748  *  Out:
749  *      none
750  *
751  * Return Value: TRUE if success otherwise FALSE
752  *
753  */
754 BOOL KeybSetAllGroupKey (
755     PSKeyManagement pTable,
756     DWORD           dwKeyIndex,
757     ULONG           uKeyLength,
758     PQWORD          pKeyRSC,
759     PBYTE           pbyKey,
760     BYTE            byKeyDecMode,
761     DWORD_PTR       dwIoBase,
762     BYTE            byLocalID
763     )
764 {
765     int         i;
766     UINT        ii;
767     PSKeyItem   pKey;
768     UINT        uKeyIdx;
769
770     DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Enter KeybSetAllGroupKey: %lX\n", dwKeyIndex);
771
772
773     if ((dwKeyIndex & PAIRWISE_KEY) != 0) {                  // Pairwise key
774         return (FALSE);
775     } else if ((dwKeyIndex & 0x000000FF) >= MAX_GROUP_KEY) {
776         return (FALSE);
777     }
778
779     for (i=0; i < MAX_KEY_TABLE-1; i++) {
780         if (pTable->KeyTable[i].bInUse == TRUE) {
781             // found table already exist
782             // Group key
783             pKey = &(pTable->KeyTable[i].GroupKey[dwKeyIndex & 0x000000FF]);
784             if ((dwKeyIndex & TRANSMIT_KEY) != 0)  {
785                 // Group transmit key
786                 pTable->KeyTable[i].dwGTKeyIndex = dwKeyIndex;
787                 DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Group transmit key(R)[%lX]: %d\n", pTable->KeyTable[i].dwGTKeyIndex, i);
788
789             }
790             pTable->KeyTable[i].wKeyCtl &= 0xFF0F;          // clear group key control filed
791             pTable->KeyTable[i].wKeyCtl |= (byKeyDecMode << 4);
792             pTable->KeyTable[i].wKeyCtl |= 0x0040;          // use group key for group address
793             uKeyIdx = (dwKeyIndex & 0x000000FF);
794
795             pTable->KeyTable[i].wKeyCtl |= 0x8000;              // enable on-fly
796
797             pKey->bKeyValid = TRUE;
798             pKey->uKeyLength = uKeyLength;
799             pKey->dwKeyIndex = dwKeyIndex;
800             pKey->byCipherSuite = byKeyDecMode;
801             MEMvCopy(pKey->abyKey, pbyKey, uKeyLength);
802             if (byKeyDecMode == KEY_CTL_WEP) {
803                 if (uKeyLength == WLAN_WEP40_KEYLEN)
804                     pKey->abyKey[15] &= 0x7F;
805                 if (uKeyLength == WLAN_WEP104_KEYLEN)
806                     pKey->abyKey[15] |= 0x80;
807             }
808             MACvSetKeyEntry(dwIoBase, pTable->KeyTable[i].wKeyCtl, i, uKeyIdx, pTable->KeyTable[i].abyBSSID, (PDWORD)pKey->abyKey, byLocalID);
809
810             if ((dwKeyIndex & USE_KEYRSC) == 0) {
811                 // RSC set by NIC
812                 ZERO_MEMORY(&(pKey->KeyRSC), sizeof(QWORD));
813             }
814             else {
815                 MEMvCopy(&(pKey->KeyRSC), pKeyRSC,  sizeof(QWORD));
816             }
817             pKey->dwTSC47_16 = 0;
818             pKey->wTSC15_0 = 0;
819
820             DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO"KeybSetKey(R): \n");
821             DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO"pKey->bKeyValid: %d\n ", pKey->bKeyValid);
822             DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO"pKey->uKeyLength: %d\n ", (int)pKey->uKeyLength);
823             DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO"pKey->abyKey: ");
824             for (ii = 0; ii < pKey->uKeyLength; ii++) {
825                 DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO"%02x ", pKey->abyKey[ii]);
826             }
827             DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO"\n");
828
829             //DBG_PRN_GRP12(("pKey->dwTSC47_16: %lX\n ", pKey->dwTSC47_16));
830             //DBG_PRN_GRP12(("pKey->wTSC15_0: %X\n ", pKey->wTSC15_0));
831             //DBG_PRN_GRP12(("pKey->dwKeyIndex: %lX\n ", pKey->dwKeyIndex));
832
833         } // (pTable->KeyTable[i].bInUse == TRUE)
834     }
835     return (TRUE);
836 }