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