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