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