Staging: rt2860: remove DOT11_N_SUPPORT ifdefs
[linux-2.6] / drivers / staging / rt2860 / common / cmm_sanity.c
1 /*
2  *************************************************************************
3  * Ralink Tech Inc.
4  * 5F., No.36, Taiyuan St., Jhubei City,
5  * Hsinchu County 302,
6  * Taiwan, R.O.C.
7  *
8  * (c) Copyright 2002-2007, Ralink Technology, Inc.
9  *
10  * This program is free software; you can redistribute it and/or modify  *
11  * it under the terms of the GNU General Public License as published by  *
12  * the Free Software Foundation; either version 2 of the License, or     *
13  * (at your option) any later version.                                   *
14  *                                                                       *
15  * This program is distributed in the hope that it will be useful,       *
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of        *
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
18  * GNU General Public License for more details.                          *
19  *                                                                       *
20  * You should have received a copy of the GNU General Public License     *
21  * along with this program; if not, write to the                         *
22  * Free Software Foundation, Inc.,                                       *
23  * 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
24  *                                                                       *
25  *************************************************************************
26
27         Module Name:
28         sanity.c
29
30         Abstract:
31
32         Revision History:
33         Who                     When                    What
34         --------        ----------              ----------------------------------------------
35         John Chang  2004-09-01      add WMM support
36 */
37 #include "../rt_config.h"
38
39
40 extern UCHAR    CISCO_OUI[];
41
42 extern UCHAR    WPA_OUI[];
43 extern UCHAR    RSN_OUI[];
44 extern UCHAR    WME_INFO_ELEM[];
45 extern UCHAR    WME_PARM_ELEM[];
46 extern UCHAR    Ccx2QosInfo[];
47 extern UCHAR    RALINK_OUI[];
48 extern UCHAR    BROADCOM_OUI[];
49 extern UCHAR    WPS_OUI[];
50
51 /*
52     ==========================================================================
53     Description:
54         MLME message sanity check
55     Return:
56         TRUE if all parameters are OK, FALSE otherwise
57
58         IRQL = DISPATCH_LEVEL
59
60     ==========================================================================
61  */
62 BOOLEAN MlmeAddBAReqSanity(
63     IN PRTMP_ADAPTER pAd,
64     IN VOID *Msg,
65     IN ULONG MsgLen,
66     OUT PUCHAR pAddr2)
67 {
68     PMLME_ADDBA_REQ_STRUCT   pInfo;
69
70     pInfo = (MLME_ADDBA_REQ_STRUCT *)Msg;
71
72     if ((MsgLen != sizeof(MLME_ADDBA_REQ_STRUCT)))
73     {
74         DBGPRINT(RT_DEBUG_TRACE, ("MlmeAddBAReqSanity fail - message lenght not correct.\n"));
75         return FALSE;
76     }
77
78     if ((pInfo->Wcid >= MAX_LEN_OF_MAC_TABLE))
79     {
80         DBGPRINT(RT_DEBUG_TRACE, ("MlmeAddBAReqSanity fail - The peer Mac is not associated yet.\n"));
81         return FALSE;
82     }
83
84     if ((pInfo->pAddr[0]&0x01) == 0x01)
85     {
86         DBGPRINT(RT_DEBUG_TRACE, ("MlmeAddBAReqSanity fail - broadcast address not support BA\n"));
87         return FALSE;
88     }
89
90     return TRUE;
91 }
92
93 /*
94     ==========================================================================
95     Description:
96         MLME message sanity check
97     Return:
98         TRUE if all parameters are OK, FALSE otherwise
99
100         IRQL = DISPATCH_LEVEL
101
102     ==========================================================================
103  */
104 BOOLEAN MlmeDelBAReqSanity(
105     IN PRTMP_ADAPTER pAd,
106     IN VOID *Msg,
107     IN ULONG MsgLen)
108 {
109         MLME_DELBA_REQ_STRUCT *pInfo;
110         pInfo = (MLME_DELBA_REQ_STRUCT *)Msg;
111
112     if ((MsgLen != sizeof(MLME_DELBA_REQ_STRUCT)))
113     {
114         DBGPRINT(RT_DEBUG_ERROR, ("MlmeDelBAReqSanity fail - message lenght not correct.\n"));
115         return FALSE;
116     }
117
118     if ((pInfo->Wcid >= MAX_LEN_OF_MAC_TABLE))
119     {
120         DBGPRINT(RT_DEBUG_ERROR, ("MlmeDelBAReqSanity fail - The peer Mac is not associated yet.\n"));
121         return FALSE;
122     }
123
124     if ((pInfo->TID & 0xf0))
125     {
126         DBGPRINT(RT_DEBUG_ERROR, ("MlmeDelBAReqSanity fail - The peer TID is incorrect.\n"));
127         return FALSE;
128     }
129
130         if (NdisEqualMemory(pAd->MacTab.Content[pInfo->Wcid].Addr, pInfo->Addr, MAC_ADDR_LEN) == 0)
131     {
132         DBGPRINT(RT_DEBUG_ERROR, ("MlmeDelBAReqSanity fail - the peer addr dosen't exist.\n"));
133         return FALSE;
134     }
135
136     return TRUE;
137 }
138
139 BOOLEAN PeerAddBAReqActionSanity(
140     IN PRTMP_ADAPTER pAd,
141     IN VOID *pMsg,
142     IN ULONG MsgLen,
143         OUT PUCHAR pAddr2)
144 {
145         PFRAME_802_11 pFrame = (PFRAME_802_11)pMsg;
146         PFRAME_ADDBA_REQ pAddFrame;
147         pAddFrame = (PFRAME_ADDBA_REQ)(pMsg);
148         if (MsgLen < (sizeof(FRAME_ADDBA_REQ)))
149         {
150                 DBGPRINT(RT_DEBUG_ERROR,("PeerAddBAReqActionSanity: ADDBA Request frame length size = %ld incorrect\n", MsgLen));
151                 return FALSE;
152         }
153         // we support immediate BA.
154         *(USHORT *)(&pAddFrame->BaParm) = cpu2le16(*(USHORT *)(&pAddFrame->BaParm));
155         pAddFrame->TimeOutValue = cpu2le16(pAddFrame->TimeOutValue);
156         pAddFrame->BaStartSeq.word = cpu2le16(pAddFrame->BaStartSeq.word);
157
158         if (pAddFrame->BaParm.BAPolicy != IMMED_BA)
159         {
160                 DBGPRINT(RT_DEBUG_ERROR,("PeerAddBAReqActionSanity: ADDBA Request Ba Policy[%d] not support\n", pAddFrame->BaParm.BAPolicy));
161                 DBGPRINT(RT_DEBUG_ERROR,("ADDBA Request. tid=%x, Bufsize=%x, AMSDUSupported=%x \n", pAddFrame->BaParm.TID, pAddFrame->BaParm.BufSize, pAddFrame->BaParm.AMSDUSupported));
162                 return FALSE;
163         }
164
165         // we support immediate BA.
166         if (pAddFrame->BaParm.TID &0xfff0)
167         {
168                 DBGPRINT(RT_DEBUG_ERROR,("PeerAddBAReqActionSanity: ADDBA Request incorrect TID = %d\n", pAddFrame->BaParm.TID));
169                 return FALSE;
170         }
171         COPY_MAC_ADDR(pAddr2, pFrame->Hdr.Addr2);
172         return TRUE;
173 }
174
175 BOOLEAN PeerAddBARspActionSanity(
176     IN PRTMP_ADAPTER pAd,
177     IN VOID *pMsg,
178     IN ULONG MsgLen)
179 {
180         PFRAME_ADDBA_RSP pAddFrame;
181
182         pAddFrame = (PFRAME_ADDBA_RSP)(pMsg);
183         if (MsgLen < (sizeof(FRAME_ADDBA_RSP)))
184         {
185                 DBGPRINT(RT_DEBUG_ERROR,("PeerAddBARspActionSanity: ADDBA Response frame length size = %ld incorrect\n", MsgLen));
186                 return FALSE;
187         }
188         // we support immediate BA.
189         *(USHORT *)(&pAddFrame->BaParm) = cpu2le16(*(USHORT *)(&pAddFrame->BaParm));
190         pAddFrame->StatusCode = cpu2le16(pAddFrame->StatusCode);
191         pAddFrame->TimeOutValue = cpu2le16(pAddFrame->TimeOutValue);
192
193         if (pAddFrame->BaParm.BAPolicy != IMMED_BA)
194         {
195                 DBGPRINT(RT_DEBUG_ERROR,("PeerAddBAReqActionSanity: ADDBA Response Ba Policy[%d] not support\n", pAddFrame->BaParm.BAPolicy));
196                 return FALSE;
197         }
198
199         // we support immediate BA.
200         if (pAddFrame->BaParm.TID &0xfff0)
201         {
202                 DBGPRINT(RT_DEBUG_ERROR,("PeerAddBARspActionSanity: ADDBA Response incorrect TID = %d\n", pAddFrame->BaParm.TID));
203                 return FALSE;
204         }
205         return TRUE;
206
207 }
208
209 BOOLEAN PeerDelBAActionSanity(
210     IN PRTMP_ADAPTER pAd,
211     IN UCHAR Wcid,
212     IN VOID *pMsg,
213     IN ULONG MsgLen )
214 {
215         //PFRAME_802_11 pFrame = (PFRAME_802_11)pMsg;
216         PFRAME_DELBA_REQ  pDelFrame;
217         if (MsgLen != (sizeof(FRAME_DELBA_REQ)))
218                 return FALSE;
219
220         if (Wcid >= MAX_LEN_OF_MAC_TABLE)
221                 return FALSE;
222
223         pDelFrame = (PFRAME_DELBA_REQ)(pMsg);
224
225         *(USHORT *)(&pDelFrame->DelbaParm) = cpu2le16(*(USHORT *)(&pDelFrame->DelbaParm));
226         pDelFrame->ReasonCode = cpu2le16(pDelFrame->ReasonCode);
227
228         if (pDelFrame->DelbaParm.TID &0xfff0)
229                 return FALSE;
230
231         return TRUE;
232 }
233
234 /*
235     ==========================================================================
236     Description:
237         MLME message sanity check
238     Return:
239         TRUE if all parameters are OK, FALSE otherwise
240
241         IRQL = DISPATCH_LEVEL
242
243     ==========================================================================
244  */
245 BOOLEAN PeerBeaconAndProbeRspSanity(
246     IN PRTMP_ADAPTER pAd,
247     IN VOID *Msg,
248     IN ULONG MsgLen,
249     IN UCHAR  MsgChannel,
250     OUT PUCHAR pAddr2,
251     OUT PUCHAR pBssid,
252     OUT CHAR Ssid[],
253     OUT UCHAR *pSsidLen,
254     OUT UCHAR *pBssType,
255     OUT USHORT *pBeaconPeriod,
256     OUT UCHAR *pChannel,
257     OUT UCHAR *pNewChannel,
258     OUT LARGE_INTEGER *pTimestamp,
259     OUT CF_PARM *pCfParm,
260     OUT USHORT *pAtimWin,
261     OUT USHORT *pCapabilityInfo,
262     OUT UCHAR *pErp,
263     OUT UCHAR *pDtimCount,
264     OUT UCHAR *pDtimPeriod,
265     OUT UCHAR *pBcastFlag,
266     OUT UCHAR *pMessageToMe,
267     OUT UCHAR SupRate[],
268     OUT UCHAR *pSupRateLen,
269     OUT UCHAR ExtRate[],
270     OUT UCHAR *pExtRateLen,
271     OUT UCHAR *pCkipFlag,
272     OUT UCHAR *pAironetCellPowerLimit,
273     OUT PEDCA_PARM       pEdcaParm,
274     OUT PQBSS_LOAD_PARM  pQbssLoad,
275     OUT PQOS_CAPABILITY_PARM pQosCapability,
276     OUT ULONG *pRalinkIe,
277     OUT UCHAR            *pHtCapabilityLen,
278     OUT UCHAR            *pPreNHtCapabilityLen,
279     OUT HT_CAPABILITY_IE *pHtCapability,
280         OUT UCHAR                *AddHtInfoLen,
281         OUT ADD_HT_INFO_IE *AddHtInfo,
282         OUT UCHAR *NewExtChannelOffset,         // Ht extension channel offset(above or below)
283     OUT USHORT *LengthVIE,
284     OUT PNDIS_802_11_VARIABLE_IEs pVIE)
285 {
286     CHAR                                *Ptr;
287         CHAR                            TimLen;
288     PFRAME_802_11               pFrame;
289     PEID_STRUCT         pEid;
290     UCHAR                               SubType;
291     UCHAR                               Sanity;
292     //UCHAR                             ECWMin, ECWMax;
293     //MAC_CSR9_STRUC            Csr9;
294     ULONG                               Length = 0;
295
296         // For some 11a AP which didn't have DS_IE, we use two conditions to decide the channel
297         //      1. If the AP is 11n enabled, then check the control channel.
298         //      2. If the AP didn't have any info about channel, use the channel we received this frame as the channel. (May inaccuracy!!)
299         UCHAR                   CtrlChannel = 0;
300
301     // Add for 3 necessary EID field check
302     Sanity = 0;
303
304     *pAtimWin = 0;
305     *pErp = 0;
306     *pDtimCount = 0;
307     *pDtimPeriod = 0;
308     *pBcastFlag = 0;
309     *pMessageToMe = 0;
310     *pExtRateLen = 0;
311     *pCkipFlag = 0;                             // Default of CkipFlag is 0
312     *pAironetCellPowerLimit = 0xFF;  // Default of AironetCellPowerLimit is 0xFF
313     *LengthVIE = 0;                                     // Set the length of VIE to init value 0
314     *pHtCapabilityLen = 0;                                      // Set the length of VIE to init value 0
315         if (pAd->OpMode == OPMODE_STA)
316                 *pPreNHtCapabilityLen = 0;                                      // Set the length of VIE to init value 0
317     *AddHtInfoLen = 0;                                  // Set the length of VIE to init value 0
318     *pRalinkIe = 0;
319     *pNewChannel = 0;
320     *NewExtChannelOffset = 0xff;        //Default 0xff means no such IE
321     pCfParm->bValid = FALSE;        // default: no IE_CF found
322     pQbssLoad->bValid = FALSE;      // default: no IE_QBSS_LOAD found
323     pEdcaParm->bValid = FALSE;      // default: no IE_EDCA_PARAMETER found
324     pQosCapability->bValid = FALSE; // default: no IE_QOS_CAPABILITY found
325
326     pFrame = (PFRAME_802_11)Msg;
327
328     // get subtype from header
329     SubType = (UCHAR)pFrame->Hdr.FC.SubType;
330
331     // get Addr2 and BSSID from header
332     COPY_MAC_ADDR(pAddr2, pFrame->Hdr.Addr2);
333     COPY_MAC_ADDR(pBssid, pFrame->Hdr.Addr3);
334
335     Ptr = pFrame->Octet;
336     Length += LENGTH_802_11;
337
338     // get timestamp from payload and advance the pointer
339     NdisMoveMemory(pTimestamp, Ptr, TIMESTAMP_LEN);
340
341         pTimestamp->u.LowPart = cpu2le32(pTimestamp->u.LowPart);
342         pTimestamp->u.HighPart = cpu2le32(pTimestamp->u.HighPart);
343
344     Ptr += TIMESTAMP_LEN;
345     Length += TIMESTAMP_LEN;
346
347     // get beacon interval from payload and advance the pointer
348     NdisMoveMemory(pBeaconPeriod, Ptr, 2);
349     Ptr += 2;
350     Length += 2;
351
352     // get capability info from payload and advance the pointer
353     NdisMoveMemory(pCapabilityInfo, Ptr, 2);
354     Ptr += 2;
355     Length += 2;
356
357     if (CAP_IS_ESS_ON(*pCapabilityInfo))
358         *pBssType = BSS_INFRA;
359     else
360         *pBssType = BSS_ADHOC;
361
362     pEid = (PEID_STRUCT) Ptr;
363
364     // get variable fields from payload and advance the pointer
365     while ((Length + 2 + pEid->Len) <= MsgLen)
366     {
367         //
368         // Secure copy VIE to VarIE[MAX_VIE_LEN] didn't overflow.
369         //
370         if ((*LengthVIE + pEid->Len + 2) >= MAX_VIE_LEN)
371         {
372             DBGPRINT(RT_DEBUG_WARN, ("PeerBeaconAndProbeRspSanity - Variable IEs out of resource [len(=%d) > MAX_VIE_LEN(=%d)]\n",
373                     (*LengthVIE + pEid->Len + 2), MAX_VIE_LEN));
374             break;
375         }
376
377         switch(pEid->Eid)
378         {
379             case IE_SSID:
380                 // Already has one SSID EID in this beacon, ignore the second one
381                 if (Sanity & 0x1)
382                     break;
383                 if(pEid->Len <= MAX_LEN_OF_SSID)
384                 {
385                     NdisMoveMemory(Ssid, pEid->Octet, pEid->Len);
386                     *pSsidLen = pEid->Len;
387                     Sanity |= 0x1;
388                 }
389                 else
390                 {
391                     DBGPRINT(RT_DEBUG_TRACE, ("PeerBeaconAndProbeRspSanity - wrong IE_SSID (len=%d)\n",pEid->Len));
392                     return FALSE;
393                 }
394                 break;
395
396             case IE_SUPP_RATES:
397                 if(pEid->Len <= MAX_LEN_OF_SUPPORTED_RATES)
398                 {
399                     Sanity |= 0x2;
400                     NdisMoveMemory(SupRate, pEid->Octet, pEid->Len);
401                     *pSupRateLen = pEid->Len;
402
403                     // TODO: 2004-09-14 not a good design here, cause it exclude extra rates
404                     // from ScanTab. We should report as is. And filter out unsupported
405                     // rates in MlmeAux.
406                     // Check against the supported rates
407                     // RTMPCheckRates(pAd, SupRate, pSupRateLen);
408                 }
409                 else
410                 {
411                     DBGPRINT(RT_DEBUG_TRACE, ("PeerBeaconAndProbeRspSanity - wrong IE_SUPP_RATES (len=%d)\n",pEid->Len));
412                     return FALSE;
413                 }
414                 break;
415
416             case IE_HT_CAP:
417                         if (pEid->Len >= SIZE_HT_CAP_IE)  //Note: allow extension.!!
418                         {
419                                 NdisMoveMemory(pHtCapability, pEid->Octet, sizeof(HT_CAPABILITY_IE));
420                                 *pHtCapabilityLen = SIZE_HT_CAP_IE;     // Nnow we only support 26 bytes.
421
422                                 *(USHORT *)(&pHtCapability->HtCapInfo) = cpu2le16(*(USHORT *)(&pHtCapability->HtCapInfo));
423                                 *(USHORT *)(&pHtCapability->ExtHtCapInfo) = cpu2le16(*(USHORT *)(&pHtCapability->ExtHtCapInfo));
424
425                                 IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
426                                 {
427                                         *pPreNHtCapabilityLen = 0;      // Nnow we only support 26 bytes.
428
429                                         Ptr = (PUCHAR) pVIE;
430                                         NdisMoveMemory(Ptr + *LengthVIE, &pEid->Eid, pEid->Len + 2);
431                                         *LengthVIE += (pEid->Len + 2);
432                                 }
433                         }
434                         else
435                         {
436                                 DBGPRINT(RT_DEBUG_WARN, ("PeerBeaconAndProbeRspSanity - wrong IE_HT_CAP. pEid->Len = %d\n", pEid->Len));
437                         }
438
439                 break;
440             case IE_ADD_HT:
441                         if (pEid->Len >= sizeof(ADD_HT_INFO_IE))
442                         {
443                                 // This IE allows extension, but we can ignore extra bytes beyond our knowledge , so only
444                                 // copy first sizeof(ADD_HT_INFO_IE)
445                                 NdisMoveMemory(AddHtInfo, pEid->Octet, sizeof(ADD_HT_INFO_IE));
446                                 *AddHtInfoLen = SIZE_ADD_HT_INFO_IE;
447
448                                 CtrlChannel = AddHtInfo->ControlChan;
449
450                                 *(USHORT *)(&AddHtInfo->AddHtInfo2) = cpu2le16(*(USHORT *)(&AddHtInfo->AddHtInfo2));
451                                 *(USHORT *)(&AddHtInfo->AddHtInfo3) = cpu2le16(*(USHORT *)(&AddHtInfo->AddHtInfo3));
452
453                                 IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
454                                 {
455                                         Ptr = (PUCHAR) pVIE;
456                                         NdisMoveMemory(Ptr + *LengthVIE, &pEid->Eid, pEid->Len + 2);
457                                         *LengthVIE += (pEid->Len + 2);
458                                 }
459                         }
460                         else
461                         {
462                                 DBGPRINT(RT_DEBUG_WARN, ("PeerBeaconAndProbeRspSanity - wrong IE_ADD_HT. \n"));
463                         }
464
465                 break;
466             case IE_SECONDARY_CH_OFFSET:
467                         if (pEid->Len == 1)
468                         {
469                                 *NewExtChannelOffset = pEid->Octet[0];
470                         }
471                         else
472                         {
473                                 DBGPRINT(RT_DEBUG_WARN, ("PeerBeaconAndProbeRspSanity - wrong IE_SECONDARY_CH_OFFSET. \n"));
474                         }
475
476                 break;
477             case IE_FH_PARM:
478                 DBGPRINT(RT_DEBUG_TRACE, ("PeerBeaconAndProbeRspSanity(IE_FH_PARM) \n"));
479                 break;
480
481             case IE_DS_PARM:
482                 if(pEid->Len == 1)
483                 {
484                     *pChannel = *pEid->Octet;
485
486                                         IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
487                                         {
488                                                 if (ChannelSanity(pAd, *pChannel) == 0)
489                                                 {
490
491                                                         return FALSE;
492                                                 }
493                                         }
494
495                     Sanity |= 0x4;
496                 }
497                 else
498                 {
499                     DBGPRINT(RT_DEBUG_TRACE, ("PeerBeaconAndProbeRspSanity - wrong IE_DS_PARM (len=%d)\n",pEid->Len));
500                     return FALSE;
501                 }
502                 break;
503
504             case IE_CF_PARM:
505                 if(pEid->Len == 6)
506                 {
507                     pCfParm->bValid = TRUE;
508                     pCfParm->CfpCount = pEid->Octet[0];
509                     pCfParm->CfpPeriod = pEid->Octet[1];
510                     pCfParm->CfpMaxDuration = pEid->Octet[2] + 256 * pEid->Octet[3];
511                     pCfParm->CfpDurRemaining = pEid->Octet[4] + 256 * pEid->Octet[5];
512                 }
513                 else
514                 {
515                     DBGPRINT(RT_DEBUG_TRACE, ("PeerBeaconAndProbeRspSanity - wrong IE_CF_PARM\n"));
516                     return FALSE;
517                 }
518                 break;
519
520             case IE_IBSS_PARM:
521                 if(pEid->Len == 2)
522                 {
523                     NdisMoveMemory(pAtimWin, pEid->Octet, pEid->Len);
524                 }
525                 else
526                 {
527                     DBGPRINT(RT_DEBUG_TRACE, ("PeerBeaconAndProbeRspSanity - wrong IE_IBSS_PARM\n"));
528                     return FALSE;
529                 }
530                 break;
531
532             case IE_TIM:
533                 if(INFRA_ON(pAd) && SubType == SUBTYPE_BEACON)
534                 {
535                     GetTimBit((PUCHAR)pEid, pAd->StaActive.Aid, &TimLen, pBcastFlag, pDtimCount, pDtimPeriod, pMessageToMe);
536                 }
537                 break;
538
539             case IE_CHANNEL_SWITCH_ANNOUNCEMENT:
540                 if(pEid->Len == 3)
541                 {
542                         *pNewChannel = pEid->Octet[1];  //extract new channel number
543                 }
544                 break;
545
546             // New for WPA
547             // CCX v2 has the same IE, we need to parse that too
548             // Wifi WMM use the same IE vale, need to parse that too
549             // case IE_WPA:
550             case IE_VENDOR_SPECIFIC:
551                 // Check the OUI version, filter out non-standard usage
552                 if (NdisEqualMemory(pEid->Octet, RALINK_OUI, 3) && (pEid->Len == 7))
553                 {
554                     //*pRalinkIe = pEid->Octet[3];
555                     if (pEid->Octet[3] != 0)
556                                         *pRalinkIe = pEid->Octet[3];
557                                 else
558                                         *pRalinkIe = 0xf0000000; // Set to non-zero value (can't set bit0-2) to represent this is Ralink Chip. So at linkup, we will set ralinkchip flag.
559                 }
560                 // This HT IE is before IEEE draft set HT IE value.2006-09-28 by Jan.
561
562                 // Other vendors had production before IE_HT_CAP value is assigned. To backward support those old-firmware AP,
563                 // Check broadcom-defiend pre-802.11nD1.0 OUI for HT related IE, including HT Capatilities IE and HT Information IE
564                 else if ((*pHtCapabilityLen == 0) && NdisEqualMemory(pEid->Octet, PRE_N_HT_OUI, 3) && (pEid->Len >= 4) && (pAd->OpMode == OPMODE_STA))
565                 {
566                     if ((pEid->Octet[3] == OUI_PREN_HT_CAP) && (pEid->Len >= 30) && (*pHtCapabilityLen == 0))
567                     {
568                         NdisMoveMemory(pHtCapability, &pEid->Octet[4], sizeof(HT_CAPABILITY_IE));
569                         *pPreNHtCapabilityLen = SIZE_HT_CAP_IE;
570                     }
571
572                     if ((pEid->Octet[3] == OUI_PREN_ADD_HT) && (pEid->Len >= 26))
573                     {
574                         NdisMoveMemory(AddHtInfo, &pEid->Octet[4], sizeof(ADD_HT_INFO_IE));
575                         *AddHtInfoLen = SIZE_ADD_HT_INFO_IE;
576                     }
577                 }
578                 else if (NdisEqualMemory(pEid->Octet, WPA_OUI, 4))
579                 {
580                     // Copy to pVIE which will report to microsoft bssid list.
581                     Ptr = (PUCHAR) pVIE;
582                     NdisMoveMemory(Ptr + *LengthVIE, &pEid->Eid, pEid->Len + 2);
583                     *LengthVIE += (pEid->Len + 2);
584                 }
585                 else if (NdisEqualMemory(pEid->Octet, WME_PARM_ELEM, 6) && (pEid->Len == 24))
586                 {
587                     PUCHAR ptr;
588                     int i;
589
590                     // parsing EDCA parameters
591                     pEdcaParm->bValid          = TRUE;
592                     pEdcaParm->bQAck           = FALSE; // pEid->Octet[0] & 0x10;
593                     pEdcaParm->bQueueRequest   = FALSE; // pEid->Octet[0] & 0x20;
594                     pEdcaParm->bTxopRequest    = FALSE; // pEid->Octet[0] & 0x40;
595                     pEdcaParm->EdcaUpdateCount = pEid->Octet[6] & 0x0f;
596                     pEdcaParm->bAPSDCapable    = (pEid->Octet[6] & 0x80) ? 1 : 0;
597                     ptr = &pEid->Octet[8];
598                     for (i=0; i<4; i++)
599                     {
600                         UCHAR aci = (*ptr & 0x60) >> 5; // b5~6 is AC INDEX
601                         pEdcaParm->bACM[aci]  = (((*ptr) & 0x10) == 0x10);   // b5 is ACM
602                         pEdcaParm->Aifsn[aci] = (*ptr) & 0x0f;               // b0~3 is AIFSN
603                         pEdcaParm->Cwmin[aci] = *(ptr+1) & 0x0f;             // b0~4 is Cwmin
604                         pEdcaParm->Cwmax[aci] = *(ptr+1) >> 4;               // b5~8 is Cwmax
605                         pEdcaParm->Txop[aci]  = *(ptr+2) + 256 * (*(ptr+3)); // in unit of 32-us
606                         ptr += 4; // point to next AC
607                     }
608                 }
609                 else if (NdisEqualMemory(pEid->Octet, WME_INFO_ELEM, 6) && (pEid->Len == 7))
610                 {
611                     // parsing EDCA parameters
612                     pEdcaParm->bValid          = TRUE;
613                     pEdcaParm->bQAck           = FALSE; // pEid->Octet[0] & 0x10;
614                     pEdcaParm->bQueueRequest   = FALSE; // pEid->Octet[0] & 0x20;
615                     pEdcaParm->bTxopRequest    = FALSE; // pEid->Octet[0] & 0x40;
616                     pEdcaParm->EdcaUpdateCount = pEid->Octet[6] & 0x0f;
617                     pEdcaParm->bAPSDCapable    = (pEid->Octet[6] & 0x80) ? 1 : 0;
618
619                     // use default EDCA parameter
620                     pEdcaParm->bACM[QID_AC_BE]  = 0;
621                     pEdcaParm->Aifsn[QID_AC_BE] = 3;
622                     pEdcaParm->Cwmin[QID_AC_BE] = CW_MIN_IN_BITS;
623                     pEdcaParm->Cwmax[QID_AC_BE] = CW_MAX_IN_BITS;
624                     pEdcaParm->Txop[QID_AC_BE]  = 0;
625
626                     pEdcaParm->bACM[QID_AC_BK]  = 0;
627                     pEdcaParm->Aifsn[QID_AC_BK] = 7;
628                     pEdcaParm->Cwmin[QID_AC_BK] = CW_MIN_IN_BITS;
629                     pEdcaParm->Cwmax[QID_AC_BK] = CW_MAX_IN_BITS;
630                     pEdcaParm->Txop[QID_AC_BK]  = 0;
631
632                     pEdcaParm->bACM[QID_AC_VI]  = 0;
633                     pEdcaParm->Aifsn[QID_AC_VI] = 2;
634                     pEdcaParm->Cwmin[QID_AC_VI] = CW_MIN_IN_BITS-1;
635                     pEdcaParm->Cwmax[QID_AC_VI] = CW_MAX_IN_BITS;
636                     pEdcaParm->Txop[QID_AC_VI]  = 96;   // AC_VI: 96*32us ~= 3ms
637
638                     pEdcaParm->bACM[QID_AC_VO]  = 0;
639                     pEdcaParm->Aifsn[QID_AC_VO] = 2;
640                     pEdcaParm->Cwmin[QID_AC_VO] = CW_MIN_IN_BITS-2;
641                     pEdcaParm->Cwmax[QID_AC_VO] = CW_MAX_IN_BITS-1;
642                     pEdcaParm->Txop[QID_AC_VO]  = 48;   // AC_VO: 48*32us ~= 1.5ms
643                 }
644                 break;
645
646             case IE_EXT_SUPP_RATES:
647                 if (pEid->Len <= MAX_LEN_OF_SUPPORTED_RATES)
648                 {
649                     NdisMoveMemory(ExtRate, pEid->Octet, pEid->Len);
650                     *pExtRateLen = pEid->Len;
651
652                     // TODO: 2004-09-14 not a good design here, cause it exclude extra rates
653                     // from ScanTab. We should report as is. And filter out unsupported
654                     // rates in MlmeAux.
655                     // Check against the supported rates
656                     // RTMPCheckRates(pAd, ExtRate, pExtRateLen);
657                 }
658                 break;
659
660             case IE_ERP:
661                 if (pEid->Len == 1)
662                 {
663                     *pErp = (UCHAR)pEid->Octet[0];
664                 }
665                 break;
666
667             case IE_AIRONET_CKIP:
668                 // 0. Check Aironet IE length, it must be larger or equal to 28
669                 // Cisco AP350 used length as 28
670                 // Cisco AP12XX used length as 30
671                 if (pEid->Len < (CKIP_NEGOTIATION_LENGTH - 2))
672                     break;
673
674                 // 1. Copy CKIP flag byte to buffer for process
675                 *pCkipFlag = *(pEid->Octet + 8);
676                 break;
677
678             case IE_AP_TX_POWER:
679                 // AP Control of Client Transmit Power
680                 //0. Check Aironet IE length, it must be 6
681                 if (pEid->Len != 0x06)
682                     break;
683
684                 // Get cell power limit in dBm
685                 if (NdisEqualMemory(pEid->Octet, CISCO_OUI, 3) == 1)
686                     *pAironetCellPowerLimit = *(pEid->Octet + 4);
687                 break;
688
689             // WPA2 & 802.11i RSN
690             case IE_RSN:
691                 // There is no OUI for version anymore, check the group cipher OUI before copying
692                 if (RTMPEqualMemory(pEid->Octet + 2, RSN_OUI, 3))
693                 {
694                     // Copy to pVIE which will report to microsoft bssid list.
695                     Ptr = (PUCHAR) pVIE;
696                     NdisMoveMemory(Ptr + *LengthVIE, &pEid->Eid, pEid->Len + 2);
697                     *LengthVIE += (pEid->Len + 2);
698                 }
699                 break;
700
701             default:
702                 break;
703         }
704
705         Length = Length + 2 + pEid->Len;  // Eid[1] + Len[1]+ content[Len]
706         pEid = (PEID_STRUCT)((UCHAR*)pEid + 2 + pEid->Len);
707     }
708
709     // For some 11a AP. it did not have the channel EID, patch here
710         IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
711         {
712                 UCHAR LatchRfChannel = MsgChannel;
713                 if ((pAd->LatchRfRegs.Channel > 14) && ((Sanity & 0x4) == 0))
714                 {
715                         if (CtrlChannel != 0)
716                                 *pChannel = CtrlChannel;
717                         else
718                                 *pChannel = LatchRfChannel;
719                         Sanity |= 0x4;
720                 }
721         }
722
723         if (Sanity != 0x7)
724         {
725                 DBGPRINT(RT_DEBUG_WARN, ("PeerBeaconAndProbeRspSanity - missing field, Sanity=0x%02x\n", Sanity));
726                 return FALSE;
727         }
728         else
729         {
730                 return TRUE;
731         }
732
733 }
734
735 /*
736     ==========================================================================
737     Description:
738         MLME message sanity check
739     Return:
740         TRUE if all parameters are OK, FALSE otherwise
741     ==========================================================================
742  */
743 BOOLEAN MlmeScanReqSanity(
744         IN PRTMP_ADAPTER pAd,
745         IN VOID *Msg,
746         IN ULONG MsgLen,
747         OUT UCHAR *pBssType,
748         OUT CHAR Ssid[],
749         OUT UCHAR *pSsidLen,
750         OUT UCHAR *pScanType)
751 {
752         MLME_SCAN_REQ_STRUCT *Info;
753
754         Info = (MLME_SCAN_REQ_STRUCT *)(Msg);
755         *pBssType = Info->BssType;
756         *pSsidLen = Info->SsidLen;
757         NdisMoveMemory(Ssid, Info->Ssid, *pSsidLen);
758         *pScanType = Info->ScanType;
759
760         if ((*pBssType == BSS_INFRA || *pBssType == BSS_ADHOC || *pBssType == BSS_ANY)
761                 && (*pScanType == SCAN_ACTIVE || *pScanType == SCAN_PASSIVE
762                 || *pScanType == SCAN_CISCO_PASSIVE || *pScanType == SCAN_CISCO_ACTIVE
763                 || *pScanType == SCAN_CISCO_CHANNEL_LOAD || *pScanType == SCAN_CISCO_NOISE
764                 ))
765         {
766                 return TRUE;
767         }
768         else
769         {
770                 DBGPRINT(RT_DEBUG_TRACE, ("MlmeScanReqSanity fail - wrong BssType or ScanType\n"));
771                 return FALSE;
772         }
773 }
774
775 // IRQL = DISPATCH_LEVEL
776 UCHAR ChannelSanity(
777     IN PRTMP_ADAPTER pAd,
778     IN UCHAR channel)
779 {
780     int i;
781
782     for (i = 0; i < pAd->ChannelListNum; i ++)
783     {
784         if (channel == pAd->ChannelList[i].Channel)
785             return 1;
786     }
787     return 0;
788 }
789
790 /*
791     ==========================================================================
792     Description:
793         MLME message sanity check
794     Return:
795         TRUE if all parameters are OK, FALSE otherwise
796
797         IRQL = DISPATCH_LEVEL
798
799     ==========================================================================
800  */
801 BOOLEAN PeerDeauthSanity(
802     IN PRTMP_ADAPTER pAd,
803     IN VOID *Msg,
804     IN ULONG MsgLen,
805     OUT PUCHAR pAddr2,
806     OUT USHORT *pReason)
807 {
808     PFRAME_802_11 pFrame = (PFRAME_802_11)Msg;
809
810     COPY_MAC_ADDR(pAddr2, pFrame->Hdr.Addr2);
811     NdisMoveMemory(pReason, &pFrame->Octet[0], 2);
812
813     return TRUE;
814 }
815
816 /*
817     ==========================================================================
818     Description:
819         MLME message sanity check
820     Return:
821         TRUE if all parameters are OK, FALSE otherwise
822
823         IRQL = DISPATCH_LEVEL
824
825     ==========================================================================
826  */
827 BOOLEAN PeerAuthSanity(
828     IN PRTMP_ADAPTER pAd,
829     IN VOID *Msg,
830     IN ULONG MsgLen,
831     OUT PUCHAR pAddr,
832     OUT USHORT *pAlg,
833     OUT USHORT *pSeq,
834     OUT USHORT *pStatus,
835     CHAR *pChlgText)
836 {
837     PFRAME_802_11 pFrame = (PFRAME_802_11)Msg;
838
839     COPY_MAC_ADDR(pAddr,   pFrame->Hdr.Addr2);
840     NdisMoveMemory(pAlg,    &pFrame->Octet[0], 2);
841     NdisMoveMemory(pSeq,    &pFrame->Octet[2], 2);
842     NdisMoveMemory(pStatus, &pFrame->Octet[4], 2);
843
844     if ((*pAlg == Ndis802_11AuthModeOpen)
845       )
846     {
847         if (*pSeq == 1 || *pSeq == 2)
848         {
849             return TRUE;
850         }
851         else
852         {
853             DBGPRINT(RT_DEBUG_TRACE, ("PeerAuthSanity fail - wrong Seg#\n"));
854             return FALSE;
855         }
856     }
857     else if (*pAlg == Ndis802_11AuthModeShared)
858     {
859         if (*pSeq == 1 || *pSeq == 4)
860         {
861             return TRUE;
862         }
863         else if (*pSeq == 2 || *pSeq == 3)
864         {
865             NdisMoveMemory(pChlgText, &pFrame->Octet[8], CIPHER_TEXT_LEN);
866             return TRUE;
867         }
868         else
869         {
870             DBGPRINT(RT_DEBUG_TRACE, ("PeerAuthSanity fail - wrong Seg#\n"));
871             return FALSE;
872         }
873     }
874     else
875     {
876         DBGPRINT(RT_DEBUG_TRACE, ("PeerAuthSanity fail - wrong algorithm\n"));
877         return FALSE;
878     }
879 }
880
881 /*
882     ==========================================================================
883     Description:
884         MLME message sanity check
885     Return:
886         TRUE if all parameters are OK, FALSE otherwise
887     ==========================================================================
888  */
889 BOOLEAN MlmeAuthReqSanity(
890     IN PRTMP_ADAPTER pAd,
891     IN VOID *Msg,
892     IN ULONG MsgLen,
893     OUT PUCHAR pAddr,
894     OUT ULONG *pTimeout,
895     OUT USHORT *pAlg)
896 {
897     MLME_AUTH_REQ_STRUCT *pInfo;
898
899     pInfo  = (MLME_AUTH_REQ_STRUCT *)Msg;
900     COPY_MAC_ADDR(pAddr, pInfo->Addr);
901     *pTimeout = pInfo->Timeout;
902     *pAlg = pInfo->Alg;
903
904     if (((*pAlg == Ndis802_11AuthModeShared) ||(*pAlg == Ndis802_11AuthModeOpen)
905         ) &&
906         ((*pAddr & 0x01) == 0))
907     {
908         return TRUE;
909     }
910     else
911     {
912         DBGPRINT(RT_DEBUG_TRACE, ("MlmeAuthReqSanity fail - wrong algorithm\n"));
913         return FALSE;
914     }
915 }
916
917 /*
918     ==========================================================================
919     Description:
920         MLME message sanity check
921     Return:
922         TRUE if all parameters are OK, FALSE otherwise
923
924         IRQL = DISPATCH_LEVEL
925
926     ==========================================================================
927  */
928 BOOLEAN MlmeAssocReqSanity(
929     IN PRTMP_ADAPTER pAd,
930     IN VOID *Msg,
931     IN ULONG MsgLen,
932     OUT PUCHAR pApAddr,
933     OUT USHORT *pCapabilityInfo,
934     OUT ULONG *pTimeout,
935     OUT USHORT *pListenIntv)
936 {
937     MLME_ASSOC_REQ_STRUCT *pInfo;
938
939     pInfo = (MLME_ASSOC_REQ_STRUCT *)Msg;
940     *pTimeout = pInfo->Timeout;                             // timeout
941     COPY_MAC_ADDR(pApAddr, pInfo->Addr);                   // AP address
942     *pCapabilityInfo = pInfo->CapabilityInfo;               // capability info
943     *pListenIntv = pInfo->ListenIntv;
944
945     return TRUE;
946 }
947
948 /*
949     ==========================================================================
950     Description:
951         MLME message sanity check
952     Return:
953         TRUE if all parameters are OK, FALSE otherwise
954
955         IRQL = DISPATCH_LEVEL
956
957     ==========================================================================
958  */
959 BOOLEAN PeerDisassocSanity(
960     IN PRTMP_ADAPTER pAd,
961     IN VOID *Msg,
962     IN ULONG MsgLen,
963     OUT PUCHAR pAddr2,
964     OUT USHORT *pReason)
965 {
966     PFRAME_802_11 pFrame = (PFRAME_802_11)Msg;
967
968     COPY_MAC_ADDR(pAddr2, pFrame->Hdr.Addr2);
969     NdisMoveMemory(pReason, &pFrame->Octet[0], 2);
970
971     return TRUE;
972 }
973
974 /*
975         ========================================================================
976         Routine Description:
977                 Sanity check NetworkType (11b, 11g or 11a)
978
979         Arguments:
980                 pBss - Pointer to BSS table.
981
982         Return Value:
983         Ndis802_11DS .......(11b)
984         Ndis802_11OFDM24....(11g)
985         Ndis802_11OFDM5.....(11a)
986
987         IRQL = DISPATCH_LEVEL
988
989         ========================================================================
990 */
991 NDIS_802_11_NETWORK_TYPE NetworkTypeInUseSanity(
992     IN PBSS_ENTRY pBss)
993 {
994         NDIS_802_11_NETWORK_TYPE        NetWorkType;
995         UCHAR                                           rate, i;
996
997         NetWorkType = Ndis802_11DS;
998
999         if (pBss->Channel <= 14)
1000         {
1001                 //
1002                 // First check support Rate.
1003                 //
1004                 for (i = 0; i < pBss->SupRateLen; i++)
1005                 {
1006                         rate = pBss->SupRate[i] & 0x7f; // Mask out basic rate set bit
1007                         if ((rate == 2) || (rate == 4) || (rate == 11) || (rate == 22))
1008                         {
1009                                 continue;
1010                         }
1011                         else
1012                         {
1013                                 //
1014                                 // Otherwise (even rate > 108) means Ndis802_11OFDM24
1015                                 //
1016                                 NetWorkType = Ndis802_11OFDM24;
1017                                 break;
1018                         }
1019                 }
1020
1021                 //
1022                 // Second check Extend Rate.
1023                 //
1024                 if (NetWorkType != Ndis802_11OFDM24)
1025                 {
1026                         for (i = 0; i < pBss->ExtRateLen; i++)
1027                         {
1028                                 rate = pBss->SupRate[i] & 0x7f; // Mask out basic rate set bit
1029                                 if ((rate == 2) || (rate == 4) || (rate == 11) || (rate == 22))
1030                                 {
1031                                         continue;
1032                                 }
1033                                 else
1034                                 {
1035                                         //
1036                                         // Otherwise (even rate > 108) means Ndis802_11OFDM24
1037                                         //
1038                                         NetWorkType = Ndis802_11OFDM24;
1039                                         break;
1040                                 }
1041                         }
1042                 }
1043         }
1044         else
1045         {
1046                 NetWorkType = Ndis802_11OFDM5;
1047         }
1048
1049     if (pBss->HtCapabilityLen != 0)
1050     {
1051         if (NetWorkType == Ndis802_11OFDM5)
1052             NetWorkType = Ndis802_11OFDM5_N;
1053         else
1054             NetWorkType = Ndis802_11OFDM24_N;
1055     }
1056
1057         return NetWorkType;
1058 }
1059
1060 /*
1061     ==========================================================================
1062     Description:
1063         WPA message sanity check
1064     Return:
1065         TRUE if all parameters are OK, FALSE otherwise
1066     ==========================================================================
1067  */
1068 BOOLEAN PeerWpaMessageSanity(
1069     IN  PRTMP_ADAPTER           pAd,
1070     IN  PEAPOL_PACKET           pMsg,
1071     IN  ULONG                           MsgLen,
1072     IN  UCHAR                           MsgType,
1073     IN  MAC_TABLE_ENTRY         *pEntry)
1074 {
1075         UCHAR                   mic[LEN_KEY_DESC_MIC], digest[80], KEYDATA[MAX_LEN_OF_RSNIE];
1076         BOOLEAN                 bReplayDiff = FALSE;
1077         BOOLEAN                 bWPA2 = FALSE;
1078         KEY_INFO                EapolKeyInfo;
1079         UCHAR                   GroupKeyIndex = 0;
1080
1081
1082         NdisZeroMemory(mic, sizeof(mic));
1083         NdisZeroMemory(digest, sizeof(digest));
1084         NdisZeroMemory(KEYDATA, sizeof(KEYDATA));
1085         NdisZeroMemory((PUCHAR)&EapolKeyInfo, sizeof(EapolKeyInfo));
1086
1087         NdisMoveMemory((PUCHAR)&EapolKeyInfo, (PUCHAR)&pMsg->KeyDesc.KeyInfo, sizeof(KEY_INFO));
1088
1089         *((USHORT *)&EapolKeyInfo) = cpu2le16(*((USHORT *)&EapolKeyInfo));
1090
1091         // Choose WPA2 or not
1092         if ((pEntry->AuthMode == Ndis802_11AuthModeWPA2) || (pEntry->AuthMode == Ndis802_11AuthModeWPA2PSK))
1093                 bWPA2 = TRUE;
1094
1095         // 0. Check MsgType
1096         if ((MsgType > EAPOL_GROUP_MSG_2) || (MsgType < EAPOL_PAIR_MSG_1))
1097         {
1098                 DBGPRINT(RT_DEBUG_ERROR, ("The message type is invalid(%d)! \n", MsgType));
1099                 return FALSE;
1100         }
1101
1102         // 1. Replay counter check
1103         if (MsgType == EAPOL_PAIR_MSG_1 || MsgType == EAPOL_PAIR_MSG_3 || MsgType == EAPOL_GROUP_MSG_1) // For supplicant
1104     {
1105         // First validate replay counter, only accept message with larger replay counter.
1106                 // Let equal pass, some AP start with all zero replay counter
1107                 UCHAR   ZeroReplay[LEN_KEY_DESC_REPLAY];
1108
1109         NdisZeroMemory(ZeroReplay, LEN_KEY_DESC_REPLAY);
1110                 if ((RTMPCompareMemory(pMsg->KeyDesc.ReplayCounter, pEntry->R_Counter, LEN_KEY_DESC_REPLAY) != 1) &&
1111                         (RTMPCompareMemory(pMsg->KeyDesc.ReplayCounter, ZeroReplay, LEN_KEY_DESC_REPLAY) != 0))
1112         {
1113                         bReplayDiff = TRUE;
1114         }
1115         }
1116         else if (MsgType == EAPOL_PAIR_MSG_2 || MsgType == EAPOL_PAIR_MSG_4 || MsgType == EAPOL_GROUP_MSG_2)    // For authenticator
1117         {
1118                 // check Replay Counter coresponds to MSG from authenticator, otherwise discard
1119         if (!NdisEqualMemory(pMsg->KeyDesc.ReplayCounter, pEntry->R_Counter, LEN_KEY_DESC_REPLAY))
1120         {
1121                         bReplayDiff = TRUE;
1122         }
1123         }
1124
1125         // Replay Counter different condition
1126         if (bReplayDiff)
1127         {
1128                 // send wireless event - for replay counter different
1129                 if (pAd->CommonCfg.bWirelessEvent)
1130                         RTMPSendWirelessEvent(pAd, IW_REPLAY_COUNTER_DIFF_EVENT_FLAG, pEntry->Addr, pEntry->apidx, 0);
1131
1132                 if (MsgType < EAPOL_GROUP_MSG_1)
1133                 {
1134                 DBGPRINT(RT_DEBUG_ERROR, ("Replay Counter Different in pairwise msg %d of 4-way handshake!\n", MsgType));
1135                 }
1136                 else
1137                 {
1138                         DBGPRINT(RT_DEBUG_ERROR, ("Replay Counter Different in group msg %d of 2-way handshake!\n", (MsgType - EAPOL_PAIR_MSG_4)));
1139                 }
1140
1141                 hex_dump("Receive replay counter ", pMsg->KeyDesc.ReplayCounter, LEN_KEY_DESC_REPLAY);
1142                 hex_dump("Current replay counter ", pEntry->R_Counter, LEN_KEY_DESC_REPLAY);
1143         return FALSE;
1144         }
1145
1146         // 2. Verify MIC except Pairwise Msg1
1147         if (MsgType != EAPOL_PAIR_MSG_1)
1148         {
1149                 UCHAR                   rcvd_mic[LEN_KEY_DESC_MIC];
1150
1151                 // Record the received MIC for check later
1152                 NdisMoveMemory(rcvd_mic, pMsg->KeyDesc.KeyMic, LEN_KEY_DESC_MIC);
1153                 NdisZeroMemory(pMsg->KeyDesc.KeyMic, LEN_KEY_DESC_MIC);
1154
1155         if (pEntry->WepStatus == Ndis802_11Encryption2Enabled)  // TKIP
1156         {
1157             hmac_md5(pEntry->PTK, LEN_EAP_MICK, (PUCHAR)pMsg, MsgLen, mic);
1158         }
1159         else if (pEntry->WepStatus == Ndis802_11Encryption3Enabled)     // AES
1160         {
1161             HMAC_SHA1((PUCHAR)pMsg, MsgLen, pEntry->PTK, LEN_EAP_MICK, digest);
1162             NdisMoveMemory(mic, digest, LEN_KEY_DESC_MIC);
1163         }
1164
1165         if (!NdisEqualMemory(rcvd_mic, mic, LEN_KEY_DESC_MIC))
1166         {
1167                         // send wireless event - for MIC different
1168                         if (pAd->CommonCfg.bWirelessEvent)
1169                                 RTMPSendWirelessEvent(pAd, IW_MIC_DIFF_EVENT_FLAG, pEntry->Addr, pEntry->apidx, 0);
1170
1171                         if (MsgType < EAPOL_GROUP_MSG_1)
1172                         {
1173                 DBGPRINT(RT_DEBUG_ERROR, ("MIC Different in pairwise msg %d of 4-way handshake!\n", MsgType));
1174                         }
1175                         else
1176                         {
1177                                 DBGPRINT(RT_DEBUG_ERROR, ("MIC Different in group msg %d of 2-way handshake!\n", (MsgType - EAPOL_PAIR_MSG_4)));
1178                         }
1179
1180                         hex_dump("Received MIC", rcvd_mic, LEN_KEY_DESC_MIC);
1181                         hex_dump("Desired  MIC", mic, LEN_KEY_DESC_MIC);
1182
1183                         return FALSE;
1184         }
1185         }
1186
1187         // Extract the context of the Key Data field if it exist
1188         // The field in pairwise_msg_2_WPA1(WPA2) & pairwise_msg_3_WPA1 is un-encrypted.
1189         // The field in group_msg_1_WPA1(WPA2) & pairwise_msg_3_WPA2 is encrypted.
1190         if (pMsg->KeyDesc.KeyDataLen[1] > 0)
1191         {
1192                 // Decrypt this field
1193                 if ((MsgType == EAPOL_PAIR_MSG_3 && bWPA2) || (MsgType == EAPOL_GROUP_MSG_1))
1194                 {
1195                         if(pEntry->WepStatus == Ndis802_11Encryption3Enabled)
1196                         {
1197                                 // AES
1198                                 AES_GTK_KEY_UNWRAP(&pEntry->PTK[16], KEYDATA, pMsg->KeyDesc.KeyDataLen[1],pMsg->KeyDesc.KeyData);
1199                         }
1200                         else
1201                         {
1202                                 INT     i;
1203                                 UCHAR   Key[32];
1204                                 // Decrypt TKIP GTK
1205                                 // Construct 32 bytes RC4 Key
1206                                 NdisMoveMemory(Key, pMsg->KeyDesc.KeyIv, 16);
1207                                 NdisMoveMemory(&Key[16], &pEntry->PTK[16], 16);
1208                                 ARCFOUR_INIT(&pAd->PrivateInfo.WEPCONTEXT, Key, 32);
1209                                 //discard first 256 bytes
1210                                 for(i = 0; i < 256; i++)
1211                                         ARCFOUR_BYTE(&pAd->PrivateInfo.WEPCONTEXT);
1212                                 // Decrypt GTK. Becareful, there is no ICV to check the result is correct or not
1213                                 ARCFOUR_DECRYPT(&pAd->PrivateInfo.WEPCONTEXT, KEYDATA, pMsg->KeyDesc.KeyData, pMsg->KeyDesc.KeyDataLen[1]);
1214                         }
1215
1216                         if (!bWPA2 && (MsgType == EAPOL_GROUP_MSG_1))
1217                                 GroupKeyIndex = EapolKeyInfo.KeyIndex;
1218
1219                 }
1220                 else if ((MsgType == EAPOL_PAIR_MSG_2) || (MsgType == EAPOL_PAIR_MSG_3 && !bWPA2))
1221                 {
1222                         NdisMoveMemory(KEYDATA, pMsg->KeyDesc.KeyData, pMsg->KeyDesc.KeyDataLen[1]);
1223                 }
1224                 else
1225                 {
1226
1227                         return TRUE;
1228                 }
1229
1230                 // Parse Key Data field to
1231                 // 1. verify RSN IE for pairwise_msg_2_WPA1(WPA2) ,pairwise_msg_3_WPA1(WPA2)
1232                 // 2. verify KDE format for pairwise_msg_3_WPA2, group_msg_1_WPA2
1233                 // 3. update shared key for pairwise_msg_3_WPA2, group_msg_1_WPA1(WPA2)
1234                 if (!RTMPParseEapolKeyData(pAd, KEYDATA, pMsg->KeyDesc.KeyDataLen[1], GroupKeyIndex, MsgType, bWPA2, pEntry))
1235                 {
1236                         return FALSE;
1237                 }
1238         }
1239
1240         return TRUE;
1241
1242 }