Staging: vt665x: Clean up include files, Part 2
[safe/jmp/linux-2.6] / drivers / staging / vt6655 / wpa2.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: wpa2.c
21  *
22  * Purpose: Handles the Basic Service Set & Node Database functions
23  *
24  * Functions:
25  *
26  * Revision History:
27  *
28  * Author: Yiching Chen
29  *
30  * Date: Oct. 4, 2004
31  *
32  */
33
34 #include "wpa2.h"
35 #include "umem.h"
36 #include "device.h"
37 #include "wmgr.h"
38
39 /*---------------------  Static Definitions -------------------------*/
40 static int          msglevel                =MSG_LEVEL_INFO;
41 //static int          msglevel                =MSG_LEVEL_DEBUG;
42 /*---------------------  Static Classes  ----------------------------*/
43
44 /*---------------------  Static Variables  --------------------------*/
45
46 const BYTE abyOUIGK[4]      = { 0x00, 0x0F, 0xAC, 0x00 };
47 const BYTE abyOUIWEP40[4]   = { 0x00, 0x0F, 0xAC, 0x01 };
48 const BYTE abyOUIWEP104[4]  = { 0x00, 0x0F, 0xAC, 0x05 };
49 const BYTE abyOUITKIP[4]    = { 0x00, 0x0F, 0xAC, 0x02 };
50 const BYTE abyOUICCMP[4]    = { 0x00, 0x0F, 0xAC, 0x04 };
51
52 const BYTE abyOUI8021X[4]   = { 0x00, 0x0F, 0xAC, 0x01 };
53 const BYTE abyOUIPSK[4]     = { 0x00, 0x0F, 0xAC, 0x02 };
54
55
56 /*---------------------  Static Functions  --------------------------*/
57
58 /*---------------------  Export Variables  --------------------------*/
59
60 /*---------------------  Export Functions  --------------------------*/
61
62 /*+
63  *
64  * Description:
65  *    Clear RSN information in BSSList.
66  *
67  * Parameters:
68  *  In:
69  *      pBSSNode - BSS list.
70  *  Out:
71  *      none
72  *
73  * Return Value: none.
74  *
75 -*/
76 VOID
77 WPA2_ClearRSN (
78     IN PKnownBSS        pBSSNode
79     )
80 {
81     int ii;
82
83     pBSSNode->bWPA2Valid = FALSE;
84
85     pBSSNode->byCSSGK = WLAN_11i_CSS_CCMP;
86     for (ii=0; ii < 4; ii ++)
87         pBSSNode->abyCSSPK[ii] = WLAN_11i_CSS_CCMP;
88     pBSSNode->wCSSPKCount = 1;
89     for (ii=0; ii < 4; ii ++)
90         pBSSNode->abyAKMSSAuthType[ii] = WLAN_11i_AKMSS_802_1X;
91     pBSSNode->wAKMSSAuthCount = 1;
92     pBSSNode->sRSNCapObj.bRSNCapExist = FALSE;
93     pBSSNode->sRSNCapObj.wRSNCap = 0;
94 }
95
96 /*+
97  *
98  * Description:
99  *    Parse RSN IE.
100  *
101  * Parameters:
102  *  In:
103  *      pBSSNode - BSS list.
104  *      pRSN - Pointer to the RSN IE.
105  *  Out:
106  *      none
107  *
108  * Return Value: none.
109  *
110 -*/
111 VOID
112 WPA2vParseRSN (
113     IN PKnownBSS        pBSSNode,
114     IN PWLAN_IE_RSN     pRSN
115     )
116 {
117     int                 i, j;
118     WORD                m = 0, n = 0;
119     PBYTE               pbyOUI;
120     BOOL                bUseGK = FALSE;
121
122     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"WPA2_ParseRSN: [%d]\n", pRSN->len);
123
124     WPA2_ClearRSN(pBSSNode);
125
126     if (pRSN->len == 2) { // ver(2)
127         if ((pRSN->byElementID == WLAN_EID_RSN) && (pRSN->wVersion == 1)) {
128             pBSSNode->bWPA2Valid = TRUE;
129         }
130         return;
131     }
132
133     if (pRSN->len < 6) { // ver(2) + GK(4)
134         // invalid CSS, P802.11i/D10.0, p31
135         return;
136     }
137
138     // information element header makes sense
139     if ((pRSN->byElementID == WLAN_EID_RSN) &&
140         (pRSN->wVersion == 1)) {
141
142         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Legal 802.11i RSN\n");
143
144         pbyOUI = &(pRSN->abyRSN[0]);
145         if (MEMEqualMemory(pbyOUI, abyOUIWEP40, 4))
146             pBSSNode->byCSSGK = WLAN_11i_CSS_WEP40;
147         else if (MEMEqualMemory(pbyOUI, abyOUITKIP, 4))
148             pBSSNode->byCSSGK = WLAN_11i_CSS_TKIP;
149         else if (MEMEqualMemory(pbyOUI, abyOUICCMP, 4))
150             pBSSNode->byCSSGK = WLAN_11i_CSS_CCMP;
151         else if (MEMEqualMemory(pbyOUI, abyOUIWEP104, 4))
152             pBSSNode->byCSSGK = WLAN_11i_CSS_WEP104;
153         else if (MEMEqualMemory(pbyOUI, abyOUIGK, 4)) {
154             // invalid CSS, P802.11i/D10.0, p32
155             return;
156         } else
157             // any vendor checks here
158             pBSSNode->byCSSGK = WLAN_11i_CSS_UNKNOWN;
159
160         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"802.11i CSS: %X\n", pBSSNode->byCSSGK);
161
162         if (pRSN->len == 6) {
163             pBSSNode->bWPA2Valid = TRUE;
164             return;
165         }
166
167         if (pRSN->len >= 8) { // ver(2) + GK(4) + PK count(2)
168             pBSSNode->wCSSPKCount = *((PWORD) &(pRSN->abyRSN[4]));
169             j = 0;
170             pbyOUI = &(pRSN->abyRSN[6]);
171
172             for (i = 0; (i < pBSSNode->wCSSPKCount) && (j < sizeof(pBSSNode->abyCSSPK)/sizeof(BYTE)); i++) {
173
174                 if (pRSN->len >= 8+i*4+4) { // ver(2)+GK(4)+PKCnt(2)+PKS(4*i)
175                     if (MEMEqualMemory(pbyOUI, abyOUIGK, 4)) {
176                         pBSSNode->abyCSSPK[j++] = WLAN_11i_CSS_USE_GROUP;
177                         bUseGK = TRUE;
178                     } else if (MEMEqualMemory(pbyOUI, abyOUIWEP40, 4)) {
179                         // Invialid CSS, continue to parsing
180                     } else if (MEMEqualMemory(pbyOUI, abyOUITKIP, 4)) {
181                         if (pBSSNode->byCSSGK != WLAN_11i_CSS_CCMP)
182                             pBSSNode->abyCSSPK[j++] = WLAN_11i_CSS_TKIP;
183                         else
184                             ; // Invialid CSS, continue to parsing
185                     } else if (MEMEqualMemory(pbyOUI, abyOUICCMP, 4)) {
186                         pBSSNode->abyCSSPK[j++] = WLAN_11i_CSS_CCMP;
187                     } else if (MEMEqualMemory(pbyOUI, abyOUIWEP104, 4)) {
188                         // Invialid CSS, continue to parsing
189                     } else {
190                         // any vendor checks here
191                         pBSSNode->abyCSSPK[j++] = WLAN_11i_CSS_UNKNOWN;
192                     }
193                     pbyOUI += 4;
194                     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"abyCSSPK[%d]: %X\n", j-1, pBSSNode->abyCSSPK[j-1]);
195                 } else
196                     break;
197             } //for
198
199             if (bUseGK == TRUE) {
200                 if (j != 1) {
201                     // invalid CSS, This should be only PK CSS.
202                     return;
203                 }
204                 if (pBSSNode->byCSSGK == WLAN_11i_CSS_CCMP) {
205                     // invalid CSS, If CCMP is enable , PK can't be CSSGK.
206                     return;
207                 }
208             }
209             if ((pBSSNode->wCSSPKCount != 0) && (j == 0)) {
210                 // invalid CSS, No valid PK.
211                 return;
212             }
213             pBSSNode->wCSSPKCount = (WORD)j;
214             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"wCSSPKCount: %d\n", pBSSNode->wCSSPKCount);
215         }
216
217         m = *((PWORD) &(pRSN->abyRSN[4]));
218
219         if (pRSN->len >= 10+m*4) { // ver(2) + GK(4) + PK count(2) + PKS(4*m) + AKMSS count(2)
220             pBSSNode->wAKMSSAuthCount = *((PWORD) &(pRSN->abyRSN[6+4*m]));;
221             j = 0;
222             pbyOUI = &(pRSN->abyRSN[8+4*m]);
223             for (i = 0; (i < pBSSNode->wAKMSSAuthCount) && (j < sizeof(pBSSNode->abyAKMSSAuthType)/sizeof(BYTE)); i++) {
224                 if (pRSN->len >= 10+(m+i)*4+4) { // ver(2)+GK(4)+PKCnt(2)+PKS(4*m)+AKMSS(2)+AKS(4*i)
225                     if (MEMEqualMemory(pbyOUI, abyOUI8021X, 4))
226                         pBSSNode->abyAKMSSAuthType[j++] = WLAN_11i_AKMSS_802_1X;
227                     else if (MEMEqualMemory(pbyOUI, abyOUIPSK, 4))
228                         pBSSNode->abyAKMSSAuthType[j++] = WLAN_11i_AKMSS_PSK;
229                     else
230                         // any vendor checks here
231                         pBSSNode->abyAKMSSAuthType[j++] = WLAN_11i_AKMSS_UNKNOWN;
232                     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"abyAKMSSAuthType[%d]: %X\n", j-1, pBSSNode->abyAKMSSAuthType[j-1]);
233                 } else
234                     break;
235             }
236             pBSSNode->wAKMSSAuthCount = (WORD)j;
237             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"wAKMSSAuthCount: %d\n", pBSSNode->wAKMSSAuthCount);
238
239             n = *((PWORD) &(pRSN->abyRSN[6+4*m]));;
240             if (pRSN->len >= 12+4*m+4*n) { // ver(2)+GK(4)+PKCnt(2)+PKS(4*m)+AKMSSCnt(2)+AKMSS(4*n)+Cap(2)
241                 pBSSNode->sRSNCapObj.bRSNCapExist = TRUE;
242                 pBSSNode->sRSNCapObj.wRSNCap = *((PWORD) &(pRSN->abyRSN[8+4*m+4*n]));
243             }
244         }
245         //ignore PMKID lists bcs only (Re)Assocrequest has this field
246         pBSSNode->bWPA2Valid = TRUE;
247     }
248 }
249
250
251 /*+
252  *
253  * Description:
254  *    Set WPA IEs
255  *
256  * Parameters:
257  *  In:
258  *      pMgmtHandle - Pointer to management object
259  *  Out:
260  *      pRSNIEs     - Pointer to the RSN IE to set.
261  *
262  * Return Value: length of IEs.
263  *
264 -*/
265 UINT
266 WPA2uSetIEs(
267     IN PVOID pMgmtHandle,
268     OUT PWLAN_IE_RSN pRSNIEs
269     )
270 {
271     PSMgmtObject    pMgmt = (PSMgmtObject) pMgmtHandle;
272     PBYTE           pbyBuffer = NULL;
273     UINT            ii = 0;
274     PWORD           pwPMKID = NULL;
275
276     if (pRSNIEs == NULL) {
277         return(0);
278     }
279     if (((pMgmt->eAuthenMode == WMAC_AUTH_WPA2) ||
280          (pMgmt->eAuthenMode == WMAC_AUTH_WPA2PSK)) &&
281         (pMgmt->pCurrBSS != NULL)) {
282         /* WPA2 IE */
283         pbyBuffer = (PBYTE) pRSNIEs;
284         pRSNIEs->byElementID = WLAN_EID_RSN;
285         pRSNIEs->len = 6; //Version(2)+GK(4)
286         pRSNIEs->wVersion = 1;
287         //Group Key Cipher Suite
288         pRSNIEs->abyRSN[0] = 0x00;
289         pRSNIEs->abyRSN[1] = 0x0F;
290         pRSNIEs->abyRSN[2] = 0xAC;
291         if (pMgmt->byCSSGK == KEY_CTL_WEP) {
292             pRSNIEs->abyRSN[3] = pMgmt->pCurrBSS->byCSSGK;
293         } else if (pMgmt->byCSSGK == KEY_CTL_TKIP) {
294             pRSNIEs->abyRSN[3] = WLAN_11i_CSS_TKIP;
295         } else if (pMgmt->byCSSGK == KEY_CTL_CCMP) {
296             pRSNIEs->abyRSN[3] = WLAN_11i_CSS_CCMP;
297         } else {
298             pRSNIEs->abyRSN[3] = WLAN_11i_CSS_UNKNOWN;
299         }
300
301         // Pairwise Key Cipher Suite
302         pRSNIEs->abyRSN[4] = 1;
303         pRSNIEs->abyRSN[5] = 0;
304         pRSNIEs->abyRSN[6] = 0x00;
305         pRSNIEs->abyRSN[7] = 0x0F;
306         pRSNIEs->abyRSN[8] = 0xAC;
307         if (pMgmt->byCSSPK == KEY_CTL_TKIP) {
308             pRSNIEs->abyRSN[9] = WLAN_11i_CSS_TKIP;
309         } else if (pMgmt->byCSSPK == KEY_CTL_CCMP) {
310             pRSNIEs->abyRSN[9] = WLAN_11i_CSS_CCMP;
311         } else if (pMgmt->byCSSPK == KEY_CTL_NONE) {
312             pRSNIEs->abyRSN[9] = WLAN_11i_CSS_USE_GROUP;
313         } else {
314             pRSNIEs->abyRSN[9] = WLAN_11i_CSS_UNKNOWN;
315         }
316         pRSNIEs->len += 6;
317
318         // Auth Key Management Suite
319         pRSNIEs->abyRSN[10] = 1;
320         pRSNIEs->abyRSN[11] = 0;
321         pRSNIEs->abyRSN[12] = 0x00;
322         pRSNIEs->abyRSN[13] = 0x0F;
323         pRSNIEs->abyRSN[14] = 0xAC;
324         if (pMgmt->eAuthenMode == WMAC_AUTH_WPA2PSK) {
325             pRSNIEs->abyRSN[15] = WLAN_11i_AKMSS_PSK;
326         } else if (pMgmt->eAuthenMode == WMAC_AUTH_WPA2) {
327             pRSNIEs->abyRSN[15] = WLAN_11i_AKMSS_802_1X;
328         } else {
329             pRSNIEs->abyRSN[15] = WLAN_11i_AKMSS_UNKNOWN;
330         }
331         pRSNIEs->len +=6;
332
333         // RSN Capabilites
334         if (pMgmt->pCurrBSS->sRSNCapObj.bRSNCapExist == TRUE) {
335             MEMvCopy(&pRSNIEs->abyRSN[16], &pMgmt->pCurrBSS->sRSNCapObj.wRSNCap, 2);
336         } else {
337             pRSNIEs->abyRSN[16] = 0;
338             pRSNIEs->abyRSN[17] = 0;
339         }
340         pRSNIEs->len +=2;
341
342         if ((pMgmt->gsPMKIDCache.BSSIDInfoCount > 0) &&
343             (pMgmt->bRoaming == TRUE) &&
344             (pMgmt->eAuthenMode == WMAC_AUTH_WPA2)) {
345             // RSN PMKID
346             pwPMKID = (PWORD)(&pRSNIEs->abyRSN[18]);  // Point to PMKID count
347             *pwPMKID = 0;                               // Initialize PMKID count
348             pbyBuffer = &pRSNIEs->abyRSN[20];           // Point to PMKID list
349             for (ii = 0; ii < pMgmt->gsPMKIDCache.BSSIDInfoCount; ii++) {
350                 if (MEMEqualMemory(&pMgmt->gsPMKIDCache.BSSIDInfo[ii].abyBSSID[0], pMgmt->abyCurrBSSID, U_ETHER_ADDR_LEN)) {
351                     (*pwPMKID) ++;
352                     MEMvCopy(pbyBuffer, pMgmt->gsPMKIDCache.BSSIDInfo[ii].abyPMKID, 16);
353                     pbyBuffer += 16;
354                 }
355             }
356             if (*pwPMKID != 0) {
357                 pRSNIEs->len += (2 + (*pwPMKID)*16);
358             } else {
359                 pbyBuffer = &pRSNIEs->abyRSN[18];
360             }
361         }
362         return(pRSNIEs->len + WLAN_IEHDR_LEN);
363     }
364     return(0);
365 }