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