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