staging: vt6656: Remove WPA_SUPPLICANT_DRIVER_WEXT_SUPPORT macro
[firefly-linux-kernel-4.4.55.git] / drivers / staging / vt6656 / wmgr.c
1 /*
2  * Copyright (c) 1996, 2003 VIA Networking Technologies, Inc.
3  * All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  *
20  * File: wmgr.c
21  *
22  * Purpose: Handles the 802.11 management functions
23  *
24  * Author: Lyndon Chen
25  *
26  * Date: May 8, 2002
27  *
28  * Functions:
29  *      nsMgrObjectInitial - Initialize Management Objet data structure
30  *      vMgrObjectReset - Reset Management Object data structure
31  *      vMgrAssocBeginSta - Start associate function
32  *      vMgrReAssocBeginSta - Start reassociate function
33  *      vMgrDisassocBeginSta - Start disassociate function
34  *      s_vMgrRxAssocRequest - Handle Rcv associate_request
35  *      s_vMgrRxAssocResponse - Handle Rcv associate_response
36  *      vMrgAuthenBeginSta - Start authentication function
37  *      vMgrDeAuthenDeginSta - Start deauthentication function
38  *      s_vMgrRxAuthentication - Handle Rcv authentication
39  *      s_vMgrRxAuthenSequence_1 - Handle Rcv authentication sequence 1
40  *      s_vMgrRxAuthenSequence_2 - Handle Rcv authentication sequence 2
41  *      s_vMgrRxAuthenSequence_3 - Handle Rcv authentication sequence 3
42  *      s_vMgrRxAuthenSequence_4 - Handle Rcv authentication sequence 4
43  *      s_vMgrRxDisassociation - Handle Rcv disassociation
44  *      s_vMgrRxBeacon - Handle Rcv Beacon
45  *      vMgrCreateOwnIBSS - Create ad_hoc IBSS or AP BSS
46  *      vMgrJoinBSSBegin - Join BSS function
47  *      s_vMgrSynchBSS - Synch & adopt BSS parameters
48  *      s_MgrMakeBeacon - Create Baecon frame
49  *      s_MgrMakeProbeResponse - Create Probe Response frame
50  *      s_MgrMakeAssocRequest - Create Associate Request frame
51  *      s_MgrMakeReAssocRequest - Create ReAssociate Request frame
52  *      s_vMgrRxProbeResponse - Handle Rcv probe_response
53  *      s_vMrgRxProbeRequest - Handle Rcv probe_request
54  *      bMgrPrepareBeaconToSend - Prepare Beacon frame
55  *      s_vMgrLogStatus - Log 802.11 Status
56  *      vMgrRxManagePacket - Rcv management frame dispatch function
57  *      s_vMgrFormatTIM- Assembler TIM field of beacon
58  *      vMgrTimerInit- Initial 1-sec and command call back funtions
59  *
60  * Revision History:
61  *
62  */
63
64 #include "tmacro.h"
65 #include "desc.h"
66 #include "device.h"
67 #include "card.h"
68 #include "80211hdr.h"
69 #include "80211mgr.h"
70 #include "wmgr.h"
71 #include "wcmd.h"
72 #include "mac.h"
73 #include "bssdb.h"
74 #include "power.h"
75 #include "datarate.h"
76 #include "baseband.h"
77 #include "rxtx.h"
78 #include "wpa.h"
79 #include "rf.h"
80 #include "iowpa.h"
81 #include "control.h"
82 #include "rndis.h"
83
84 /*---------------------  Static Definitions -------------------------*/
85
86
87
88 /*---------------------  Static Classes  ----------------------------*/
89
90 /*---------------------  Static Variables  --------------------------*/
91 static int          msglevel                =MSG_LEVEL_INFO;
92 //static int          msglevel                =MSG_LEVEL_DEBUG;
93
94 /*---------------------  Static Functions  --------------------------*/
95
96 static BOOL ChannelExceedZoneType(
97      PSDevice pDevice,
98      BYTE byCurrChannel
99     );
100
101 // Association/diassociation functions
102 static
103 PSTxMgmtPacket
104 s_MgrMakeAssocRequest(
105      PSDevice pDevice,
106      PSMgmtObject pMgmt,
107      PBYTE pDAddr,
108      WORD wCurrCapInfo,
109      WORD wListenInterval,
110      PWLAN_IE_SSID pCurrSSID,
111      PWLAN_IE_SUPP_RATES pCurrRates,
112      PWLAN_IE_SUPP_RATES pCurrExtSuppRates
113     );
114
115 static
116 void
117 s_vMgrRxAssocRequest(
118      PSDevice pDevice,
119      PSMgmtObject pMgmt,
120      PSRxMgmtPacket pRxPacket,
121      unsigned int  uNodeIndex
122     );
123
124 static
125 PSTxMgmtPacket
126 s_MgrMakeReAssocRequest(
127      PSDevice pDevice,
128      PSMgmtObject pMgmt,
129      PBYTE pDAddr,
130      WORD wCurrCapInfo,
131      WORD wListenInterval,
132      PWLAN_IE_SSID pCurrSSID,
133      PWLAN_IE_SUPP_RATES pCurrRates,
134      PWLAN_IE_SUPP_RATES pCurrExtSuppRates
135     );
136
137 static
138 void
139 s_vMgrRxAssocResponse(
140      PSDevice pDevice,
141      PSMgmtObject pMgmt,
142      PSRxMgmtPacket pRxPacket,
143      BOOL bReAssocType
144     );
145
146 static
147 void
148 s_vMgrRxDisassociation(
149      PSDevice pDevice,
150      PSMgmtObject pMgmt,
151      PSRxMgmtPacket pRxPacket
152     );
153
154 // Authentication/deauthen functions
155 static
156 void
157 s_vMgrRxAuthenSequence_1(
158      PSDevice pDevice,
159      PSMgmtObject pMgmt,
160      PWLAN_FR_AUTHEN pFrame
161     );
162
163 static
164 void
165 s_vMgrRxAuthenSequence_2(
166      PSDevice pDevice,
167      PSMgmtObject pMgmt,
168      PWLAN_FR_AUTHEN pFrame
169     );
170
171 static
172 void
173 s_vMgrRxAuthenSequence_3(
174      PSDevice pDevice,
175      PSMgmtObject pMgmt,
176      PWLAN_FR_AUTHEN pFrame
177     );
178
179 static
180 void
181 s_vMgrRxAuthenSequence_4(
182      PSDevice pDevice,
183      PSMgmtObject pMgmt,
184      PWLAN_FR_AUTHEN pFrame
185     );
186
187 static
188 void
189 s_vMgrRxAuthentication(
190      PSDevice pDevice,
191      PSMgmtObject pMgmt,
192      PSRxMgmtPacket pRxPacket
193     );
194
195 static
196 void
197 s_vMgrRxDeauthentication(
198      PSDevice pDevice,
199      PSMgmtObject pMgmt,
200      PSRxMgmtPacket pRxPacket
201     );
202
203 // Scan functions
204 // probe request/response functions
205 static
206 void
207 s_vMgrRxProbeRequest(
208      PSDevice pDevice,
209      PSMgmtObject pMgmt,
210      PSRxMgmtPacket pRxPacket
211     );
212
213 static
214 void
215 s_vMgrRxProbeResponse(
216      PSDevice pDevice,
217      PSMgmtObject pMgmt,
218      PSRxMgmtPacket pRxPacket
219     );
220
221 // beacon functions
222 static
223 void
224 s_vMgrRxBeacon(
225      PSDevice pDevice,
226      PSMgmtObject pMgmt,
227      PSRxMgmtPacket pRxPacket,
228      BOOL bInScan
229     );
230
231 static
232 void
233 s_vMgrFormatTIM(
234      PSMgmtObject pMgmt,
235      PWLAN_IE_TIM pTIM
236     );
237
238 static
239 PSTxMgmtPacket
240 s_MgrMakeBeacon(
241      PSDevice pDevice,
242      PSMgmtObject pMgmt,
243      WORD wCurrCapInfo,
244      WORD wCurrBeaconPeriod,
245      unsigned int uCurrChannel,
246      WORD wCurrATIMWinodw,
247      PWLAN_IE_SSID pCurrSSID,
248      PBYTE pCurrBSSID,
249      PWLAN_IE_SUPP_RATES pCurrSuppRates,
250      PWLAN_IE_SUPP_RATES pCurrExtSuppRates
251     );
252
253
254 // Association response
255 static
256 PSTxMgmtPacket
257 s_MgrMakeAssocResponse(
258      PSDevice pDevice,
259      PSMgmtObject pMgmt,
260      WORD wCurrCapInfo,
261      WORD wAssocStatus,
262      WORD wAssocAID,
263      PBYTE pDstAddr,
264      PWLAN_IE_SUPP_RATES pCurrSuppRates,
265      PWLAN_IE_SUPP_RATES pCurrExtSuppRates
266     );
267
268 // ReAssociation response
269 static
270 PSTxMgmtPacket
271 s_MgrMakeReAssocResponse(
272      PSDevice pDevice,
273      PSMgmtObject pMgmt,
274      WORD wCurrCapInfo,
275      WORD wAssocStatus,
276      WORD wAssocAID,
277      PBYTE pDstAddr,
278      PWLAN_IE_SUPP_RATES pCurrSuppRates,
279      PWLAN_IE_SUPP_RATES pCurrExtSuppRates
280     );
281
282 // Probe response
283 static
284 PSTxMgmtPacket
285 s_MgrMakeProbeResponse(
286      PSDevice pDevice,
287      PSMgmtObject pMgmt,
288      WORD wCurrCapInfo,
289      WORD wCurrBeaconPeriod,
290      unsigned int uCurrChannel,
291      WORD wCurrATIMWinodw,
292      PBYTE pDstAddr,
293      PWLAN_IE_SSID pCurrSSID,
294      PBYTE pCurrBSSID,
295      PWLAN_IE_SUPP_RATES pCurrSuppRates,
296      PWLAN_IE_SUPP_RATES pCurrExtSuppRates,
297      BYTE byPHYType
298     );
299
300 // received status
301 static
302 void
303 s_vMgrLogStatus(
304      PSMgmtObject pMgmt,
305      WORD wStatus
306     );
307
308
309 static
310 void
311 s_vMgrSynchBSS (
312      PSDevice      pDevice,
313      unsigned int          uBSSMode,
314      PKnownBSS     pCurr,
315      PCMD_STATUS  pStatus
316     );
317
318
319 static BOOL
320 s_bCipherMatch (
321      PKnownBSS                        pBSSNode,
322      NDIS_802_11_ENCRYPTION_STATUS    EncStatus,
323      PBYTE                           pbyCCSPK,
324      PBYTE                           pbyCCSGK
325     );
326
327  static void  Encyption_Rebuild(
328      PSDevice pDevice,
329      PKnownBSS pCurr
330  );
331
332 /*---------------------  Export Variables  --------------------------*/
333
334 /*---------------------  Export Functions  --------------------------*/
335
336 /*+
337  *
338  * Routine Description:
339  *    Allocates and initializes the Management object.
340  *
341  * Return Value:
342  *    Ndis_staus.
343  *
344 -*/
345
346 void vMgrObjectInit(void *hDeviceContext)
347 {
348     PSDevice     pDevice = (PSDevice)hDeviceContext;
349     PSMgmtObject    pMgmt = &(pDevice->sMgmtObj);
350     int ii;
351
352
353     pMgmt->pbyPSPacketPool = &pMgmt->byPSPacketPool[0];
354     pMgmt->pbyMgmtPacketPool = &pMgmt->byMgmtPacketPool[0];
355     pMgmt->uCurrChannel = pDevice->uChannel;
356     for (ii = 0; ii < WLAN_BSSID_LEN; ii++)
357         pMgmt->abyDesireBSSID[ii] = 0xFF;
358
359     pMgmt->sAssocInfo.AssocInfo.Length = sizeof(NDIS_802_11_ASSOCIATION_INFORMATION);
360     //memset(pMgmt->abyDesireSSID, 0, WLAN_IEHDR_LEN + WLAN_SSID_MAXLEN +1);
361     pMgmt->byCSSPK = KEY_CTL_NONE;
362     pMgmt->byCSSGK = KEY_CTL_NONE;
363     pMgmt->wIBSSBeaconPeriod = DEFAULT_IBSS_BI;
364     BSSvClearBSSList((void *) pDevice, FALSE);
365
366     init_timer(&pMgmt->sTimerSecondCallback);
367     pMgmt->sTimerSecondCallback.data = (unsigned long)pDevice;
368     pMgmt->sTimerSecondCallback.function = (TimerFunction)BSSvSecondCallBack;
369     pMgmt->sTimerSecondCallback.expires = RUN_AT(HZ);
370
371     init_timer(&pDevice->sTimerCommand);
372     pDevice->sTimerCommand.data = (unsigned long)pDevice;
373     pDevice->sTimerCommand.function = (TimerFunction)vRunCommand;
374     pDevice->sTimerCommand.expires = RUN_AT(HZ);
375
376     init_timer(&pDevice->sTimerTxData);
377     pDevice->sTimerTxData.data = (unsigned long)pDevice;
378     pDevice->sTimerTxData.function = (TimerFunction)BSSvSecondTxData;
379     pDevice->sTimerTxData.expires = RUN_AT(10*HZ);      //10s callback
380     pDevice->fTxDataInSleep = FALSE;
381     pDevice->IsTxDataTrigger = FALSE;
382     pDevice->nTxDataTimeCout = 0;
383
384     pDevice->cbFreeCmdQueue = CMD_Q_SIZE;
385     pDevice->uCmdDequeueIdx = 0;
386     pDevice->uCmdEnqueueIdx = 0;
387     pDevice->eCommandState = WLAN_CMD_IDLE;
388     pDevice->bCmdRunning = FALSE;
389     pDevice->bCmdClear = FALSE;
390
391     return;
392 }
393
394 /*+
395  *
396  * Routine Description:
397  *    Start the station association procedure.  Namely, send an
398  *    association request frame to the AP.
399  *
400  * Return Value:
401  *    None.
402  *
403 -*/
404
405 void vMgrAssocBeginSta(void *hDeviceContext,
406                        PSMgmtObject pMgmt,
407                        PCMD_STATUS pStatus)
408 {
409     PSDevice             pDevice = (PSDevice)hDeviceContext;
410     PSTxMgmtPacket          pTxPacket;
411
412
413     pMgmt->wCurrCapInfo = 0;
414     pMgmt->wCurrCapInfo |= WLAN_SET_CAP_INFO_ESS(1);
415     if (pDevice->bEncryptionEnable) {
416         pMgmt->wCurrCapInfo |= WLAN_SET_CAP_INFO_PRIVACY(1);
417     }
418     // always allow receive short preamble
419     //if (pDevice->byPreambleType == 1) {
420     //    pMgmt->wCurrCapInfo |= WLAN_SET_CAP_INFO_SHORTPREAMBLE(1);
421     //}
422     pMgmt->wCurrCapInfo |= WLAN_SET_CAP_INFO_SHORTPREAMBLE(1);
423     if (pMgmt->wListenInterval == 0)
424         pMgmt->wListenInterval = 1;    // at least one.
425
426     // ERP Phy (802.11g) should support short preamble.
427     if (pMgmt->eCurrentPHYMode == PHY_TYPE_11G) {
428         pMgmt->wCurrCapInfo |= WLAN_SET_CAP_INFO_SHORTPREAMBLE(1);
429         if (pDevice->bShortSlotTime == TRUE)
430             pMgmt->wCurrCapInfo |= WLAN_SET_CAP_INFO_SHORTSLOTTIME(1);
431
432     } else if (pMgmt->eCurrentPHYMode == PHY_TYPE_11B) {
433         if (pDevice->byPreambleType == 1) {
434             pMgmt->wCurrCapInfo |= WLAN_SET_CAP_INFO_SHORTPREAMBLE(1);
435         }
436     }
437     if (pMgmt->b11hEnable == TRUE)
438         pMgmt->wCurrCapInfo |= WLAN_SET_CAP_INFO_SPECTRUMMNG(1);
439
440     // build an assocreq frame and send it
441     pTxPacket = s_MgrMakeAssocRequest
442                 (
443                   pDevice,
444                   pMgmt,
445                   pMgmt->abyCurrBSSID,
446                   pMgmt->wCurrCapInfo,
447                   pMgmt->wListenInterval,
448                   (PWLAN_IE_SSID)pMgmt->abyCurrSSID,
449                   (PWLAN_IE_SUPP_RATES)pMgmt->abyCurrSuppRates,
450                   (PWLAN_IE_SUPP_RATES)pMgmt->abyCurrExtSuppRates
451                 );
452
453     if (pTxPacket != NULL ){
454         // send the frame
455         *pStatus = csMgmt_xmit(pDevice, pTxPacket);
456         if (*pStatus == CMD_STATUS_PENDING) {
457             pMgmt->eCurrState = WMAC_STATE_ASSOCPENDING;
458             *pStatus = CMD_STATUS_SUCCESS;
459         }
460     }
461     else
462         *pStatus = CMD_STATUS_RESOURCES;
463
464     return ;
465 }
466
467
468 /*+
469  *
470  * Routine Description:
471  *    Start the station re-association procedure.
472  *
473  * Return Value:
474  *    None.
475  *
476 -*/
477
478 void vMgrReAssocBeginSta(void *hDeviceContext,
479                          PSMgmtObject pMgmt,
480                          PCMD_STATUS pStatus)
481 {
482     PSDevice             pDevice = (PSDevice)hDeviceContext;
483     PSTxMgmtPacket          pTxPacket;
484
485
486
487     pMgmt->wCurrCapInfo = 0;
488     pMgmt->wCurrCapInfo |= WLAN_SET_CAP_INFO_ESS(1);
489     if (pDevice->bEncryptionEnable) {
490         pMgmt->wCurrCapInfo |= WLAN_SET_CAP_INFO_PRIVACY(1);
491     }
492
493     //if (pDevice->byPreambleType == 1) {
494     //    pMgmt->wCurrCapInfo |= WLAN_SET_CAP_INFO_SHORTPREAMBLE(1);
495     //}
496     pMgmt->wCurrCapInfo |= WLAN_SET_CAP_INFO_SHORTPREAMBLE(1);
497
498     if (pMgmt->wListenInterval == 0)
499         pMgmt->wListenInterval = 1;    // at least one.
500
501
502     // ERP Phy (802.11g) should support short preamble.
503     if (pMgmt->eCurrentPHYMode == PHY_TYPE_11G) {
504         pMgmt->wCurrCapInfo |= WLAN_SET_CAP_INFO_SHORTPREAMBLE(1);
505       if (pDevice->bShortSlotTime == TRUE)
506           pMgmt->wCurrCapInfo |= WLAN_SET_CAP_INFO_SHORTSLOTTIME(1);
507
508     } else if (pMgmt->eCurrentPHYMode == PHY_TYPE_11B) {
509         if (pDevice->byPreambleType == 1) {
510             pMgmt->wCurrCapInfo |= WLAN_SET_CAP_INFO_SHORTPREAMBLE(1);
511         }
512     }
513     if (pMgmt->b11hEnable == TRUE)
514         pMgmt->wCurrCapInfo |= WLAN_SET_CAP_INFO_SPECTRUMMNG(1);
515
516
517     pTxPacket = s_MgrMakeReAssocRequest
518                 (
519                   pDevice,
520                   pMgmt,
521                   pMgmt->abyCurrBSSID,
522                   pMgmt->wCurrCapInfo,
523                   pMgmt->wListenInterval,
524                   (PWLAN_IE_SSID)pMgmt->abyCurrSSID,
525                   (PWLAN_IE_SUPP_RATES)pMgmt->abyCurrSuppRates,
526                   (PWLAN_IE_SUPP_RATES)pMgmt->abyCurrExtSuppRates
527                 );
528
529     if (pTxPacket != NULL ){
530         // send the frame
531         *pStatus = csMgmt_xmit(pDevice, pTxPacket);
532         if (*pStatus != CMD_STATUS_PENDING) {
533             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Mgt:Reassociation tx failed.\n");
534         }
535         else {
536             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Mgt:Reassociation tx sending.\n");
537         }
538     }
539
540
541     return ;
542 }
543
544 /*+
545  *
546  * Routine Description:
547  *    Send an dis-association request frame to the AP.
548  *
549  * Return Value:
550  *    None.
551  *
552 -*/
553
554 void vMgrDisassocBeginSta(void *hDeviceContext,
555                           PSMgmtObject pMgmt,
556                           PBYTE  abyDestAddress,
557                           WORD    wReason,
558                           PCMD_STATUS pStatus)
559 {
560     PSDevice            pDevice = (PSDevice)hDeviceContext;
561     PSTxMgmtPacket      pTxPacket = NULL;
562     WLAN_FR_DISASSOC    sFrame;
563
564     pTxPacket = (PSTxMgmtPacket)pMgmt->pbyMgmtPacketPool;
565     memset(pTxPacket, 0, sizeof(STxMgmtPacket) + WLAN_DISASSOC_FR_MAXLEN);
566     pTxPacket->p80211Header = (PUWLAN_80211HDR)((PBYTE)pTxPacket + sizeof(STxMgmtPacket));
567
568     // Setup the sFrame structure
569     sFrame.pBuf = (PBYTE)pTxPacket->p80211Header;
570     sFrame.len = WLAN_DISASSOC_FR_MAXLEN;
571
572     // format fixed field frame structure
573     vMgrEncodeDisassociation(&sFrame);
574
575     // Setup the header
576     sFrame.pHdr->sA3.wFrameCtl = cpu_to_le16(
577         (
578         WLAN_SET_FC_FTYPE(WLAN_TYPE_MGR) |
579         WLAN_SET_FC_FSTYPE(WLAN_FSTYPE_DISASSOC)
580         ));
581
582     memcpy( sFrame.pHdr->sA3.abyAddr1, abyDestAddress, WLAN_ADDR_LEN);
583     memcpy( sFrame.pHdr->sA3.abyAddr2, pMgmt->abyMACAddr, WLAN_ADDR_LEN);
584     memcpy( sFrame.pHdr->sA3.abyAddr3, pMgmt->abyCurrBSSID, WLAN_BSSID_LEN);
585
586     // Set reason code
587     *(sFrame.pwReason) = cpu_to_le16(wReason);
588     pTxPacket->cbMPDULen = sFrame.len;
589     pTxPacket->cbPayloadLen = sFrame.len - WLAN_HDR_ADDR3_LEN;
590
591     // send the frame
592     *pStatus = csMgmt_xmit(pDevice, pTxPacket);
593     if (*pStatus == CMD_STATUS_PENDING) {
594         pMgmt->eCurrState = WMAC_STATE_IDLE;
595         *pStatus = CMD_STATUS_SUCCESS;
596     }
597
598     return;
599 }
600
601
602
603 /*+
604  *
605  * Routine Description:(AP function)
606  *    Handle incoming station association request frames.
607  *
608  * Return Value:
609  *    None.
610  *
611 -*/
612
613 static
614 void
615 s_vMgrRxAssocRequest(
616      PSDevice pDevice,
617      PSMgmtObject pMgmt,
618      PSRxMgmtPacket pRxPacket,
619      unsigned int uNodeIndex
620     )
621 {
622     WLAN_FR_ASSOCREQ    sFrame;
623     CMD_STATUS          Status;
624     PSTxMgmtPacket      pTxPacket;
625     WORD                wAssocStatus = 0;
626     WORD                wAssocAID = 0;
627     unsigned int                uRateLen = WLAN_RATES_MAXLEN;
628     BYTE                abyCurrSuppRates[WLAN_IEHDR_LEN + WLAN_RATES_MAXLEN + 1];
629     BYTE                abyCurrExtSuppRates[WLAN_IEHDR_LEN + WLAN_RATES_MAXLEN + 1];
630
631
632     if (pMgmt->eCurrMode != WMAC_MODE_ESS_AP)
633         return;
634     //  node index not found
635     if (!uNodeIndex)
636         return;
637
638     //check if node is authenticated
639     //decode the frame
640     memset(&sFrame, 0, sizeof(WLAN_FR_ASSOCREQ));
641     memset(abyCurrSuppRates, 0, WLAN_IEHDR_LEN + WLAN_RATES_MAXLEN + 1);
642     memset(abyCurrExtSuppRates, 0, WLAN_IEHDR_LEN + WLAN_RATES_MAXLEN + 1);
643     sFrame.len = pRxPacket->cbMPDULen;
644     sFrame.pBuf = (PBYTE)pRxPacket->p80211Header;
645
646     vMgrDecodeAssocRequest(&sFrame);
647
648     if (pMgmt->sNodeDBTable[uNodeIndex].eNodeState >= NODE_AUTH) {
649         pMgmt->sNodeDBTable[uNodeIndex].eNodeState = NODE_ASSOC;
650         pMgmt->sNodeDBTable[uNodeIndex].wCapInfo = cpu_to_le16(*sFrame.pwCapInfo);
651         pMgmt->sNodeDBTable[uNodeIndex].wListenInterval = cpu_to_le16(*sFrame.pwListenInterval);
652         pMgmt->sNodeDBTable[uNodeIndex].bPSEnable =
653                 WLAN_GET_FC_PWRMGT(sFrame.pHdr->sA3.wFrameCtl) ? TRUE : FALSE;
654         // Todo: check sta basic rate, if ap can't support, set status code
655         if (pDevice->byBBType == BB_TYPE_11B) {
656             uRateLen = WLAN_RATES_MAXLEN_11B;
657         }
658         abyCurrSuppRates[0] = WLAN_EID_SUPP_RATES;
659         abyCurrSuppRates[1] = RATEuSetIE((PWLAN_IE_SUPP_RATES)sFrame.pSuppRates,
660                                          (PWLAN_IE_SUPP_RATES)abyCurrSuppRates,
661                                          uRateLen);
662         abyCurrExtSuppRates[0] = WLAN_EID_EXTSUPP_RATES;
663         if (pDevice->byBBType == BB_TYPE_11G) {
664             abyCurrExtSuppRates[1] = RATEuSetIE((PWLAN_IE_SUPP_RATES)sFrame.pExtSuppRates,
665                                                 (PWLAN_IE_SUPP_RATES)abyCurrExtSuppRates,
666                                                 uRateLen);
667         } else {
668             abyCurrExtSuppRates[1] = 0;
669         }
670
671
672         RATEvParseMaxRate((void *)pDevice,
673                            (PWLAN_IE_SUPP_RATES)abyCurrSuppRates,
674                            (PWLAN_IE_SUPP_RATES)abyCurrExtSuppRates,
675                            FALSE, // do not change our basic rate
676                            &(pMgmt->sNodeDBTable[uNodeIndex].wMaxBasicRate),
677                            &(pMgmt->sNodeDBTable[uNodeIndex].wMaxSuppRate),
678                            &(pMgmt->sNodeDBTable[uNodeIndex].wSuppRate),
679                            &(pMgmt->sNodeDBTable[uNodeIndex].byTopCCKBasicRate),
680                            &(pMgmt->sNodeDBTable[uNodeIndex].byTopOFDMBasicRate)
681                           );
682
683         // set max tx rate
684         pMgmt->sNodeDBTable[uNodeIndex].wTxDataRate =
685                 pMgmt->sNodeDBTable[uNodeIndex].wMaxSuppRate;
686         // Todo: check sta preamble, if ap can't support, set status code
687         pMgmt->sNodeDBTable[uNodeIndex].bShortPreamble =
688                 WLAN_GET_CAP_INFO_SHORTPREAMBLE(*sFrame.pwCapInfo);
689         pMgmt->sNodeDBTable[uNodeIndex].bShortSlotTime =
690                 WLAN_GET_CAP_INFO_SHORTSLOTTIME(*sFrame.pwCapInfo);
691         pMgmt->sNodeDBTable[uNodeIndex].wAID = (WORD)uNodeIndex;
692         wAssocStatus = WLAN_MGMT_STATUS_SUCCESS;
693         wAssocAID = (WORD)uNodeIndex;
694         // check if ERP support
695         if(pMgmt->sNodeDBTable[uNodeIndex].wMaxSuppRate > RATE_11M)
696            pMgmt->sNodeDBTable[uNodeIndex].bERPExist = TRUE;
697
698         if (pMgmt->sNodeDBTable[uNodeIndex].wMaxSuppRate <= RATE_11M) {
699             // B only STA join
700             pDevice->bProtectMode = TRUE;
701             pDevice->bNonERPPresent = TRUE;
702         }
703         if (pMgmt->sNodeDBTable[uNodeIndex].bShortPreamble == FALSE) {
704             pDevice->bBarkerPreambleMd = TRUE;
705         }
706
707         DBG_PRT(MSG_LEVEL_INFO, KERN_INFO "Associate AID= %d \n", wAssocAID);
708         DBG_PRT(MSG_LEVEL_INFO, KERN_INFO "MAC=%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X \n",
709                    sFrame.pHdr->sA3.abyAddr2[0],
710                    sFrame.pHdr->sA3.abyAddr2[1],
711                    sFrame.pHdr->sA3.abyAddr2[2],
712                    sFrame.pHdr->sA3.abyAddr2[3],
713                    sFrame.pHdr->sA3.abyAddr2[4],
714                    sFrame.pHdr->sA3.abyAddr2[5]
715                   ) ;
716         DBG_PRT(MSG_LEVEL_INFO, KERN_INFO "Max Support rate = %d \n",
717                    pMgmt->sNodeDBTable[uNodeIndex].wMaxSuppRate);
718     }
719
720
721     // assoc response reply..
722     pTxPacket = s_MgrMakeAssocResponse
723                 (
724                   pDevice,
725                   pMgmt,
726                   pMgmt->wCurrCapInfo,
727                   wAssocStatus,
728                   wAssocAID,
729                   sFrame.pHdr->sA3.abyAddr2,
730                   (PWLAN_IE_SUPP_RATES)pMgmt->abyCurrSuppRates,
731                   (PWLAN_IE_SUPP_RATES)pMgmt->abyCurrExtSuppRates
732                 );
733     if (pTxPacket != NULL ){
734
735         if (pDevice->bEnableHostapd) {
736             return;
737         }
738         /* send the frame */
739         Status = csMgmt_xmit(pDevice, pTxPacket);
740         if (Status != CMD_STATUS_PENDING) {
741             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Mgt:Assoc response tx failed\n");
742         }
743         else {
744             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Mgt:Assoc response tx sending..\n");
745         }
746
747     }
748
749     return;
750 }
751
752
753 /*+
754  *
755  * Description:(AP function)
756  *      Handle incoming station re-association request frames.
757  *
758  * Parameters:
759  *  In:
760  *      pMgmt           - Management Object structure
761  *      pRxPacket       - Received Packet
762  *  Out:
763  *      none
764  *
765  * Return Value: None.
766  *
767 -*/
768
769 static
770 void
771 s_vMgrRxReAssocRequest(
772      PSDevice pDevice,
773      PSMgmtObject pMgmt,
774      PSRxMgmtPacket pRxPacket,
775      unsigned int uNodeIndex
776     )
777 {
778     WLAN_FR_REASSOCREQ    sFrame;
779     CMD_STATUS          Status;
780     PSTxMgmtPacket      pTxPacket;
781     WORD                wAssocStatus = 0;
782     WORD                wAssocAID = 0;
783     unsigned int                uRateLen = WLAN_RATES_MAXLEN;
784     BYTE                abyCurrSuppRates[WLAN_IEHDR_LEN + WLAN_RATES_MAXLEN + 1];
785     BYTE                abyCurrExtSuppRates[WLAN_IEHDR_LEN + WLAN_RATES_MAXLEN + 1];
786
787     if (pMgmt->eCurrMode != WMAC_MODE_ESS_AP)
788         return;
789     //  node index not found
790     if (!uNodeIndex)
791         return;
792     //check if node is authenticated
793     //decode the frame
794     memset(&sFrame, 0, sizeof(WLAN_FR_REASSOCREQ));
795     sFrame.len = pRxPacket->cbMPDULen;
796     sFrame.pBuf = (PBYTE)pRxPacket->p80211Header;
797     vMgrDecodeReassocRequest(&sFrame);
798
799     if (pMgmt->sNodeDBTable[uNodeIndex].eNodeState >= NODE_AUTH) {
800         pMgmt->sNodeDBTable[uNodeIndex].eNodeState = NODE_ASSOC;
801         pMgmt->sNodeDBTable[uNodeIndex].wCapInfo = cpu_to_le16(*sFrame.pwCapInfo);
802         pMgmt->sNodeDBTable[uNodeIndex].wListenInterval = cpu_to_le16(*sFrame.pwListenInterval);
803         pMgmt->sNodeDBTable[uNodeIndex].bPSEnable =
804                 WLAN_GET_FC_PWRMGT(sFrame.pHdr->sA3.wFrameCtl) ? TRUE : FALSE;
805         // Todo: check sta basic rate, if ap can't support, set status code
806
807         if (pDevice->byBBType == BB_TYPE_11B) {
808             uRateLen = WLAN_RATES_MAXLEN_11B;
809         }
810
811         abyCurrSuppRates[0] = WLAN_EID_SUPP_RATES;
812         abyCurrSuppRates[1] = RATEuSetIE((PWLAN_IE_SUPP_RATES)sFrame.pSuppRates,
813                                          (PWLAN_IE_SUPP_RATES)abyCurrSuppRates,
814                                          uRateLen);
815         abyCurrExtSuppRates[0] = WLAN_EID_EXTSUPP_RATES;
816         if (pDevice->byBBType == BB_TYPE_11G) {
817             abyCurrExtSuppRates[1] = RATEuSetIE((PWLAN_IE_SUPP_RATES)sFrame.pExtSuppRates,
818                                                 (PWLAN_IE_SUPP_RATES)abyCurrExtSuppRates,
819                                                 uRateLen);
820         } else {
821             abyCurrExtSuppRates[1] = 0;
822         }
823
824
825         RATEvParseMaxRate((void *)pDevice,
826                           (PWLAN_IE_SUPP_RATES)abyCurrSuppRates,
827                           (PWLAN_IE_SUPP_RATES)abyCurrExtSuppRates,
828                            FALSE, // do not change our basic rate
829                            &(pMgmt->sNodeDBTable[uNodeIndex].wMaxBasicRate),
830                            &(pMgmt->sNodeDBTable[uNodeIndex].wMaxSuppRate),
831                            &(pMgmt->sNodeDBTable[uNodeIndex].wSuppRate),
832                            &(pMgmt->sNodeDBTable[uNodeIndex].byTopCCKBasicRate),
833                            &(pMgmt->sNodeDBTable[uNodeIndex].byTopOFDMBasicRate)
834                           );
835
836         // set max tx rate
837         pMgmt->sNodeDBTable[uNodeIndex].wTxDataRate =
838                 pMgmt->sNodeDBTable[uNodeIndex].wMaxSuppRate;
839         // Todo: check sta preamble, if ap can't support, set status code
840         pMgmt->sNodeDBTable[uNodeIndex].bShortPreamble =
841                 WLAN_GET_CAP_INFO_SHORTPREAMBLE(*sFrame.pwCapInfo);
842         pMgmt->sNodeDBTable[uNodeIndex].bShortSlotTime =
843                 WLAN_GET_CAP_INFO_SHORTSLOTTIME(*sFrame.pwCapInfo);
844         pMgmt->sNodeDBTable[uNodeIndex].wAID = (WORD)uNodeIndex;
845         wAssocStatus = WLAN_MGMT_STATUS_SUCCESS;
846         wAssocAID = (WORD)uNodeIndex;
847
848         // if suppurt ERP
849         if(pMgmt->sNodeDBTable[uNodeIndex].wMaxSuppRate > RATE_11M)
850            pMgmt->sNodeDBTable[uNodeIndex].bERPExist = TRUE;
851
852         if (pMgmt->sNodeDBTable[uNodeIndex].wMaxSuppRate <= RATE_11M) {
853             // B only STA join
854             pDevice->bProtectMode = TRUE;
855             pDevice->bNonERPPresent = TRUE;
856         }
857         if (pMgmt->sNodeDBTable[uNodeIndex].bShortPreamble == FALSE) {
858             pDevice->bBarkerPreambleMd = TRUE;
859         }
860
861         DBG_PRT(MSG_LEVEL_INFO, KERN_INFO "Rx ReAssociate AID= %d \n", wAssocAID);
862         DBG_PRT(MSG_LEVEL_INFO, KERN_INFO "MAC=%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X \n",
863                    sFrame.pHdr->sA3.abyAddr2[0],
864                    sFrame.pHdr->sA3.abyAddr2[1],
865                    sFrame.pHdr->sA3.abyAddr2[2],
866                    sFrame.pHdr->sA3.abyAddr2[3],
867                    sFrame.pHdr->sA3.abyAddr2[4],
868                    sFrame.pHdr->sA3.abyAddr2[5]
869                   ) ;
870         DBG_PRT(MSG_LEVEL_INFO, KERN_INFO "Max Support rate = %d \n",
871                    pMgmt->sNodeDBTable[uNodeIndex].wMaxSuppRate);
872
873     }
874
875
876     // assoc response reply..
877     pTxPacket = s_MgrMakeReAssocResponse
878                 (
879                   pDevice,
880                   pMgmt,
881                   pMgmt->wCurrCapInfo,
882                   wAssocStatus,
883                   wAssocAID,
884                   sFrame.pHdr->sA3.abyAddr2,
885                   (PWLAN_IE_SUPP_RATES)pMgmt->abyCurrSuppRates,
886                   (PWLAN_IE_SUPP_RATES)pMgmt->abyCurrExtSuppRates
887                 );
888
889     if (pTxPacket != NULL ){
890         /* send the frame */
891         if (pDevice->bEnableHostapd) {
892             return;
893         }
894         Status = csMgmt_xmit(pDevice, pTxPacket);
895         if (Status != CMD_STATUS_PENDING) {
896             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Mgt:ReAssoc response tx failed\n");
897         }
898         else {
899             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Mgt:ReAssoc response tx sending..\n");
900         }
901     }
902     return;
903 }
904
905
906 /*+
907  *
908  * Routine Description:
909  *    Handle incoming association response frames.
910  *
911  * Return Value:
912  *    None.
913  *
914 -*/
915
916 static
917 void
918 s_vMgrRxAssocResponse(
919      PSDevice pDevice,
920      PSMgmtObject pMgmt,
921      PSRxMgmtPacket pRxPacket,
922      BOOL bReAssocType
923     )
924 {
925     WLAN_FR_ASSOCRESP   sFrame;
926     PWLAN_IE_SSID   pItemSSID;
927     PBYTE   pbyIEs;
928     viawget_wpa_header *wpahdr;
929
930
931
932     if (pMgmt->eCurrState == WMAC_STATE_ASSOCPENDING ||
933          pMgmt->eCurrState == WMAC_STATE_ASSOC) {
934
935         sFrame.len = pRxPacket->cbMPDULen;
936         sFrame.pBuf = (PBYTE)pRxPacket->p80211Header;
937         // decode the frame
938         vMgrDecodeAssocResponse(&sFrame);
939         if ((sFrame.pwCapInfo == NULL)
940             || (sFrame.pwStatus == NULL)
941             || (sFrame.pwAid == NULL)
942             || (sFrame.pSuppRates == NULL)) {
943                 DBG_PORT80(0xCC);
944                 return;
945         }
946
947         pMgmt->sAssocInfo.AssocInfo.ResponseFixedIEs.Capabilities = *(sFrame.pwCapInfo);
948         pMgmt->sAssocInfo.AssocInfo.ResponseFixedIEs.StatusCode = *(sFrame.pwStatus);
949         pMgmt->sAssocInfo.AssocInfo.ResponseFixedIEs.AssociationId = *(sFrame.pwAid);
950         pMgmt->sAssocInfo.AssocInfo.AvailableResponseFixedIEs |= 0x07;
951
952         pMgmt->sAssocInfo.AssocInfo.ResponseIELength = sFrame.len - 24 - 6;
953         pMgmt->sAssocInfo.AssocInfo.OffsetResponseIEs = pMgmt->sAssocInfo.AssocInfo.OffsetRequestIEs + pMgmt->sAssocInfo.AssocInfo.RequestIELength;
954         pbyIEs = pMgmt->sAssocInfo.abyIEs;
955         pbyIEs += pMgmt->sAssocInfo.AssocInfo.RequestIELength;
956         memcpy(pbyIEs, (sFrame.pBuf + 24 +6), pMgmt->sAssocInfo.AssocInfo.ResponseIELength);
957
958         // save values and set current BSS state
959         if (cpu_to_le16((*(sFrame.pwStatus))) == WLAN_MGMT_STATUS_SUCCESS ){
960             // set AID
961             pMgmt->wCurrAID = cpu_to_le16((*(sFrame.pwAid)));
962             if ( (pMgmt->wCurrAID >> 14) != (BIT0 | BIT1) )
963             {
964                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "AID from AP, has two msb clear.\n");
965             }
966             DBG_PRT(MSG_LEVEL_INFO, KERN_INFO "Association Successful, AID=%d.\n", pMgmt->wCurrAID & ~(BIT14|BIT15));
967             pMgmt->eCurrState = WMAC_STATE_ASSOC;
968             BSSvUpdateAPNode((void *) pDevice,
969                              sFrame.pwCapInfo,
970                              sFrame.pSuppRates,
971                              sFrame.pExtSuppRates);
972             pItemSSID = (PWLAN_IE_SSID)pMgmt->abyCurrSSID;
973             DBG_PRT(MSG_LEVEL_INFO, KERN_INFO "Link with AP(SSID): %s\n", pItemSSID->abySSID);
974             pDevice->bLinkPass = TRUE;
975             ControlvMaskByte(pDevice,MESSAGE_REQUEST_MACREG,MAC_REG_PAPEDELAY,LEDSTS_STS,LEDSTS_INTER);
976             if ((pDevice->bWPADEVUp) && (pDevice->skb != NULL)) {
977                if(skb_tailroom(pDevice->skb) <(sizeof(viawget_wpa_header)+pMgmt->sAssocInfo.AssocInfo.ResponseIELength+
978                                                                          pMgmt->sAssocInfo.AssocInfo.RequestIELength)) {    //data room not enough
979                      dev_kfree_skb(pDevice->skb);
980                    pDevice->skb = dev_alloc_skb((int)pDevice->rx_buf_sz);
981                 }
982                 wpahdr = (viawget_wpa_header *)pDevice->skb->data;
983                 wpahdr->type = VIAWGET_ASSOC_MSG;
984                 wpahdr->resp_ie_len = pMgmt->sAssocInfo.AssocInfo.ResponseIELength;
985                 wpahdr->req_ie_len = pMgmt->sAssocInfo.AssocInfo.RequestIELength;
986                 memcpy(pDevice->skb->data + sizeof(viawget_wpa_header), pMgmt->sAssocInfo.abyIEs, wpahdr->req_ie_len);
987                 memcpy(pDevice->skb->data + sizeof(viawget_wpa_header) + wpahdr->req_ie_len,
988                        pbyIEs,
989                        wpahdr->resp_ie_len
990                        );
991                 skb_put(pDevice->skb, sizeof(viawget_wpa_header) + wpahdr->resp_ie_len + wpahdr->req_ie_len);
992                 pDevice->skb->dev = pDevice->wpadev;
993                 skb_reset_mac_header(pDevice->skb);
994                 pDevice->skb->pkt_type = PACKET_HOST;
995                 pDevice->skb->protocol = htons(ETH_P_802_2);
996                 memset(pDevice->skb->cb, 0, sizeof(pDevice->skb->cb));
997                 netif_rx(pDevice->skb);
998                 pDevice->skb = dev_alloc_skb((int)pDevice->rx_buf_sz);
999             }
1000
1001         //if(pDevice->bWPASuppWextEnabled == TRUE)
1002            {
1003                 BYTE buf[512];
1004                 size_t len;
1005                 union iwreq_data  wrqu;
1006                 int we_event;
1007
1008                 memset(buf, 0, 512);
1009
1010                 len = pMgmt->sAssocInfo.AssocInfo.RequestIELength;
1011                 if(len) {
1012                         memcpy(buf, pMgmt->sAssocInfo.abyIEs, len);
1013                         memset(&wrqu, 0, sizeof (wrqu));
1014                         wrqu.data.length = len;
1015                         we_event = IWEVASSOCREQIE;
1016                         PRINT_K("wireless_send_event--->IWEVASSOCREQIE\n");
1017                         wireless_send_event(pDevice->dev, we_event, &wrqu, buf);
1018                 }
1019
1020                 memset(buf, 0, 512);
1021                 len = pMgmt->sAssocInfo.AssocInfo.ResponseIELength;
1022
1023                 if(len) {
1024                         memcpy(buf, pbyIEs, len);
1025                         memset(&wrqu, 0, sizeof (wrqu));
1026                         wrqu.data.length = len;
1027                         we_event = IWEVASSOCRESPIE;
1028                         PRINT_K("wireless_send_event--->IWEVASSOCRESPIE\n");
1029                         wireless_send_event(pDevice->dev, we_event, &wrqu, buf);
1030                 }
1031
1032            memset(&wrqu, 0, sizeof (wrqu));
1033         memcpy(wrqu.ap_addr.sa_data, &pMgmt->abyCurrBSSID[0], ETH_ALEN);
1034         wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1035            PRINT_K("wireless_send_event--->SIOCGIWAP(associated)\n");
1036         wireless_send_event(pDevice->dev, SIOCGIWAP, &wrqu, NULL);
1037
1038         }
1039
1040         }
1041         else {
1042             if (bReAssocType) {
1043                 pMgmt->eCurrState = WMAC_STATE_IDLE;
1044             }
1045             else {
1046                 // jump back to the auth state and indicate the error
1047                 pMgmt->eCurrState = WMAC_STATE_AUTH;
1048             }
1049             s_vMgrLogStatus(pMgmt,cpu_to_le16((*(sFrame.pwStatus))));
1050         }
1051
1052     }
1053
1054 //need clear flags related to Networkmanager
1055               pDevice->bwextstep0 = FALSE;
1056               pDevice->bwextstep1 = FALSE;
1057               pDevice->bwextstep2 = FALSE;
1058               pDevice->bwextstep3 = FALSE;
1059               pDevice->bWPASuppWextEnabled = FALSE;
1060
1061 if(pMgmt->eCurrState == WMAC_STATE_ASSOC)
1062       timer_expire(pDevice->sTimerCommand, 0);
1063
1064     return;
1065 }
1066
1067 /*+
1068  *
1069  * Routine Description:
1070  *    Start the station authentication procedure.  Namely, send an
1071  *    authentication frame to the AP.
1072  *
1073  * Return Value:
1074  *    None.
1075  *
1076 -*/
1077
1078 void vMgrAuthenBeginSta(void *hDeviceContext,
1079                         PSMgmtObject  pMgmt,
1080                         PCMD_STATUS pStatus)
1081 {
1082     PSDevice     pDevice = (PSDevice)hDeviceContext;
1083     WLAN_FR_AUTHEN  sFrame;
1084     PSTxMgmtPacket  pTxPacket = NULL;
1085
1086     pTxPacket = (PSTxMgmtPacket)pMgmt->pbyMgmtPacketPool;
1087     memset(pTxPacket, 0, sizeof(STxMgmtPacket) + WLAN_AUTHEN_FR_MAXLEN);
1088     pTxPacket->p80211Header = (PUWLAN_80211HDR)((PBYTE)pTxPacket + sizeof(STxMgmtPacket));
1089     sFrame.pBuf = (PBYTE)pTxPacket->p80211Header;
1090     sFrame.len = WLAN_AUTHEN_FR_MAXLEN;
1091     vMgrEncodeAuthen(&sFrame);
1092     /* insert values */
1093     sFrame.pHdr->sA3.wFrameCtl = cpu_to_le16(
1094         (
1095         WLAN_SET_FC_FTYPE(WLAN_TYPE_MGR) |
1096         WLAN_SET_FC_FSTYPE(WLAN_FSTYPE_AUTHEN)
1097         ));
1098     memcpy( sFrame.pHdr->sA3.abyAddr1, pMgmt->abyCurrBSSID, WLAN_ADDR_LEN);
1099     memcpy( sFrame.pHdr->sA3.abyAddr2, pMgmt->abyMACAddr, WLAN_ADDR_LEN);
1100     memcpy( sFrame.pHdr->sA3.abyAddr3, pMgmt->abyCurrBSSID, WLAN_BSSID_LEN);
1101     if (pMgmt->bShareKeyAlgorithm)
1102         *(sFrame.pwAuthAlgorithm) = cpu_to_le16(WLAN_AUTH_ALG_SHAREDKEY);
1103     else
1104         *(sFrame.pwAuthAlgorithm) = cpu_to_le16(WLAN_AUTH_ALG_OPENSYSTEM);
1105
1106     *(sFrame.pwAuthSequence) = cpu_to_le16(1);
1107     /* Adjust the length fields */
1108     pTxPacket->cbMPDULen = sFrame.len;
1109     pTxPacket->cbPayloadLen = sFrame.len - WLAN_HDR_ADDR3_LEN;
1110
1111     *pStatus = csMgmt_xmit(pDevice, pTxPacket);
1112     if (*pStatus == CMD_STATUS_PENDING){
1113         pMgmt->eCurrState = WMAC_STATE_AUTHPENDING;
1114         *pStatus = CMD_STATUS_SUCCESS;
1115     }
1116
1117     return ;
1118 }
1119
1120 /*+
1121  *
1122  * Routine Description:
1123  *    Start the station(AP) deauthentication procedure.  Namely, send an
1124  *    deauthentication frame to the AP or Sta.
1125  *
1126  * Return Value:
1127  *    None.
1128  *
1129 -*/
1130
1131 void vMgrDeAuthenBeginSta(void *hDeviceContext,
1132                           PSMgmtObject pMgmt,
1133                           PBYTE abyDestAddress,
1134                           WORD wReason,
1135                           PCMD_STATUS pStatus)
1136 {
1137     PSDevice            pDevice = (PSDevice)hDeviceContext;
1138     WLAN_FR_DEAUTHEN    sFrame;
1139     PSTxMgmtPacket      pTxPacket = NULL;
1140
1141
1142     pTxPacket = (PSTxMgmtPacket)pMgmt->pbyMgmtPacketPool;
1143     memset(pTxPacket, 0, sizeof(STxMgmtPacket) + WLAN_DEAUTHEN_FR_MAXLEN);
1144     pTxPacket->p80211Header = (PUWLAN_80211HDR)((PBYTE)pTxPacket + sizeof(STxMgmtPacket));
1145     sFrame.pBuf = (PBYTE)pTxPacket->p80211Header;
1146     sFrame.len = WLAN_DEAUTHEN_FR_MAXLEN;
1147     vMgrEncodeDeauthen(&sFrame);
1148     /* insert values */
1149     sFrame.pHdr->sA3.wFrameCtl = cpu_to_le16(
1150         (
1151         WLAN_SET_FC_FTYPE(WLAN_TYPE_MGR) |
1152         WLAN_SET_FC_FSTYPE(WLAN_FSTYPE_DEAUTHEN)
1153         ));
1154
1155     memcpy( sFrame.pHdr->sA3.abyAddr1, abyDestAddress, WLAN_ADDR_LEN);
1156     memcpy( sFrame.pHdr->sA3.abyAddr2, pMgmt->abyMACAddr, WLAN_ADDR_LEN);
1157     memcpy( sFrame.pHdr->sA3.abyAddr3, pMgmt->abyCurrBSSID, WLAN_BSSID_LEN);
1158
1159     *(sFrame.pwReason) = cpu_to_le16(wReason);       // deauthen. bcs left BSS
1160     /* Adjust the length fields */
1161     pTxPacket->cbMPDULen = sFrame.len;
1162     pTxPacket->cbPayloadLen = sFrame.len - WLAN_HDR_ADDR3_LEN;
1163
1164     *pStatus = csMgmt_xmit(pDevice, pTxPacket);
1165     if (*pStatus == CMD_STATUS_PENDING){
1166         *pStatus = CMD_STATUS_SUCCESS;
1167     }
1168
1169
1170     return ;
1171 }
1172
1173
1174 /*+
1175  *
1176  * Routine Description:
1177  *    Handle incoming authentication frames.
1178  *
1179  * Return Value:
1180  *    None.
1181  *
1182 -*/
1183
1184 static
1185 void
1186 s_vMgrRxAuthentication(
1187      PSDevice pDevice,
1188      PSMgmtObject pMgmt,
1189      PSRxMgmtPacket pRxPacket
1190     )
1191 {
1192     WLAN_FR_AUTHEN  sFrame;
1193
1194     // we better be an AP or a STA in AUTHPENDING otherwise ignore
1195     if (!(pMgmt->eCurrMode == WMAC_MODE_ESS_AP ||
1196           pMgmt->eCurrState == WMAC_STATE_AUTHPENDING)) {
1197         return;
1198     }
1199
1200     // decode the frame
1201     sFrame.len = pRxPacket->cbMPDULen;
1202     sFrame.pBuf = (PBYTE)pRxPacket->p80211Header;
1203     vMgrDecodeAuthen(&sFrame);
1204     switch (cpu_to_le16((*(sFrame.pwAuthSequence )))){
1205         case 1:
1206             //AP funciton
1207             s_vMgrRxAuthenSequence_1(pDevice,pMgmt, &sFrame);
1208             break;
1209         case 2:
1210             s_vMgrRxAuthenSequence_2(pDevice, pMgmt, &sFrame);
1211             break;
1212         case 3:
1213             //AP funciton
1214             s_vMgrRxAuthenSequence_3(pDevice, pMgmt, &sFrame);
1215             break;
1216         case 4:
1217             s_vMgrRxAuthenSequence_4(pDevice, pMgmt, &sFrame);
1218             break;
1219         default:
1220             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Auth Sequence error, seq = %d\n",
1221                         cpu_to_le16((*(sFrame.pwAuthSequence))));
1222             break;
1223     }
1224     return;
1225 }
1226
1227
1228
1229 /*+
1230  *
1231  * Routine Description:
1232  *   Handles incoming authen frames with sequence 1.  Currently
1233  *   assumes we're an AP.  So far, no one appears to use authentication
1234  *   in Ad-Hoc mode.
1235  *
1236  * Return Value:
1237  *    None.
1238  *
1239 -*/
1240
1241
1242 static
1243 void
1244 s_vMgrRxAuthenSequence_1(
1245      PSDevice pDevice,
1246      PSMgmtObject pMgmt,
1247      PWLAN_FR_AUTHEN pFrame
1248      )
1249 {
1250     PSTxMgmtPacket      pTxPacket = NULL;
1251     unsigned int                uNodeIndex;
1252     WLAN_FR_AUTHEN      sFrame;
1253     PSKeyItem           pTransmitKey;
1254
1255     // Insert a Node entry
1256     if (!BSSbIsSTAInNodeDB(pDevice, pFrame->pHdr->sA3.abyAddr2, &uNodeIndex)) {
1257         BSSvCreateOneNode((PSDevice)pDevice, &uNodeIndex);
1258         memcpy(pMgmt->sNodeDBTable[uNodeIndex].abyMACAddr, pFrame->pHdr->sA3.abyAddr2,
1259                WLAN_ADDR_LEN);
1260     }
1261
1262     if (pMgmt->bShareKeyAlgorithm) {
1263         pMgmt->sNodeDBTable[uNodeIndex].eNodeState = NODE_KNOWN;
1264         pMgmt->sNodeDBTable[uNodeIndex].byAuthSequence = 1;
1265     }
1266     else {
1267         pMgmt->sNodeDBTable[uNodeIndex].eNodeState = NODE_AUTH;
1268     }
1269
1270     // send auth reply
1271     pTxPacket = (PSTxMgmtPacket)pMgmt->pbyMgmtPacketPool;
1272     memset(pTxPacket, 0, sizeof(STxMgmtPacket) + WLAN_AUTHEN_FR_MAXLEN);
1273     pTxPacket->p80211Header = (PUWLAN_80211HDR)((PBYTE)pTxPacket + sizeof(STxMgmtPacket));
1274     sFrame.pBuf = (PBYTE)pTxPacket->p80211Header;
1275     sFrame.len = WLAN_AUTHEN_FR_MAXLEN;
1276     // format buffer structure
1277     vMgrEncodeAuthen(&sFrame);
1278     // insert values
1279     sFrame.pHdr->sA3.wFrameCtl = cpu_to_le16(
1280          (
1281          WLAN_SET_FC_FTYPE(WLAN_TYPE_MGR) |
1282          WLAN_SET_FC_FSTYPE(WLAN_FSTYPE_AUTHEN)|
1283          WLAN_SET_FC_ISWEP(0)
1284          ));
1285     memcpy( sFrame.pHdr->sA3.abyAddr1, pFrame->pHdr->sA3.abyAddr2, WLAN_ADDR_LEN);
1286     memcpy( sFrame.pHdr->sA3.abyAddr2, pMgmt->abyMACAddr, WLAN_ADDR_LEN);
1287     memcpy( sFrame.pHdr->sA3.abyAddr3, pMgmt->abyCurrBSSID, WLAN_BSSID_LEN);
1288     *(sFrame.pwAuthAlgorithm) = *(pFrame->pwAuthAlgorithm);
1289     *(sFrame.pwAuthSequence) = cpu_to_le16(2);
1290
1291     if (cpu_to_le16(*(pFrame->pwAuthAlgorithm)) == WLAN_AUTH_ALG_SHAREDKEY) {
1292         if (pMgmt->bShareKeyAlgorithm)
1293             *(sFrame.pwStatus) = cpu_to_le16(WLAN_MGMT_STATUS_SUCCESS);
1294         else
1295             *(sFrame.pwStatus) = cpu_to_le16(WLAN_MGMT_STATUS_UNSUPPORTED_AUTHALG);
1296     }
1297     else {
1298         if (pMgmt->bShareKeyAlgorithm)
1299             *(sFrame.pwStatus) = cpu_to_le16(WLAN_MGMT_STATUS_UNSUPPORTED_AUTHALG);
1300         else
1301             *(sFrame.pwStatus) = cpu_to_le16(WLAN_MGMT_STATUS_SUCCESS);
1302     }
1303
1304     if (pMgmt->bShareKeyAlgorithm &&
1305         (cpu_to_le16(*(sFrame.pwStatus)) == WLAN_MGMT_STATUS_SUCCESS)) {
1306
1307         sFrame.pChallenge = (PWLAN_IE_CHALLENGE)(sFrame.pBuf + sFrame.len);
1308         sFrame.len += WLAN_CHALLENGE_IE_LEN;
1309         sFrame.pChallenge->byElementID = WLAN_EID_CHALLENGE;
1310         sFrame.pChallenge->len = WLAN_CHALLENGE_LEN;
1311         memset(pMgmt->abyChallenge, 0, WLAN_CHALLENGE_LEN);
1312         // get group key
1313         if(KeybGetTransmitKey(&(pDevice->sKey), pDevice->abyBroadcastAddr, GROUP_KEY, &pTransmitKey) == TRUE) {
1314             rc4_init(&pDevice->SBox, pDevice->abyPRNG, pTransmitKey->uKeyLength+3);
1315             rc4_encrypt(&pDevice->SBox, pMgmt->abyChallenge, pMgmt->abyChallenge, WLAN_CHALLENGE_LEN);
1316         }
1317         memcpy(sFrame.pChallenge->abyChallenge, pMgmt->abyChallenge , WLAN_CHALLENGE_LEN);
1318     }
1319
1320     /* Adjust the length fields */
1321     pTxPacket->cbMPDULen = sFrame.len;
1322     pTxPacket->cbPayloadLen = sFrame.len - WLAN_HDR_ADDR3_LEN;
1323     // send the frame
1324     if (pDevice->bEnableHostapd) {
1325         return;
1326     }
1327     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Mgt:Authreq_reply sequence_1 tx.. \n");
1328     if (csMgmt_xmit(pDevice, pTxPacket) != CMD_STATUS_PENDING) {
1329         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Mgt:Authreq_reply sequence_1 tx failed.\n");
1330     }
1331     return;
1332 }
1333
1334
1335
1336 /*+
1337  *
1338  * Routine Description:
1339  *   Handles incoming auth frames with sequence number 2.  Currently
1340  *   assumes we're a station.
1341  *
1342  *
1343  * Return Value:
1344  *    None.
1345  *
1346 -*/
1347
1348 static
1349 void
1350 s_vMgrRxAuthenSequence_2(
1351      PSDevice pDevice,
1352      PSMgmtObject pMgmt,
1353      PWLAN_FR_AUTHEN pFrame
1354     )
1355 {
1356     WLAN_FR_AUTHEN      sFrame;
1357     PSTxMgmtPacket      pTxPacket = NULL;
1358
1359
1360     switch (cpu_to_le16((*(pFrame->pwAuthAlgorithm))))
1361     {
1362         case WLAN_AUTH_ALG_OPENSYSTEM:
1363             if ( cpu_to_le16((*(pFrame->pwStatus))) == WLAN_MGMT_STATUS_SUCCESS ){
1364                 DBG_PRT(MSG_LEVEL_INFO, KERN_INFO "802.11 Authen (OPEN) Successful.\n");
1365                 pMgmt->eCurrState = WMAC_STATE_AUTH;
1366                timer_expire(pDevice->sTimerCommand, 0);
1367             }
1368             else {
1369                 DBG_PRT(MSG_LEVEL_INFO, KERN_INFO "802.11 Authen (OPEN) Failed.\n");
1370                 s_vMgrLogStatus(pMgmt, cpu_to_le16((*(pFrame->pwStatus))));
1371                 pMgmt->eCurrState = WMAC_STATE_IDLE;
1372             }
1373             if (pDevice->eCommandState == WLAN_AUTHENTICATE_WAIT) {
1374                 /* spin_unlock_irq(&pDevice->lock);
1375                    vCommandTimerWait((void *) pDevice, 0);
1376                    spin_lock_irq(&pDevice->lock); */
1377             }
1378             break;
1379
1380         case WLAN_AUTH_ALG_SHAREDKEY:
1381
1382             if (cpu_to_le16((*(pFrame->pwStatus))) == WLAN_MGMT_STATUS_SUCCESS) {
1383                 pTxPacket = (PSTxMgmtPacket)pMgmt->pbyMgmtPacketPool;
1384                 memset(pTxPacket, 0, sizeof(STxMgmtPacket) + WLAN_AUTHEN_FR_MAXLEN);
1385                 pTxPacket->p80211Header = (PUWLAN_80211HDR)((PBYTE)pTxPacket + sizeof(STxMgmtPacket));
1386                 sFrame.pBuf = (PBYTE)pTxPacket->p80211Header;
1387                 sFrame.len = WLAN_AUTHEN_FR_MAXLEN;
1388                 // format buffer structure
1389                 vMgrEncodeAuthen(&sFrame);
1390                 // insert values
1391                 sFrame.pHdr->sA3.wFrameCtl = cpu_to_le16(
1392                      (
1393                      WLAN_SET_FC_FTYPE(WLAN_TYPE_MGR) |
1394                      WLAN_SET_FC_FSTYPE(WLAN_FSTYPE_AUTHEN)|
1395                      WLAN_SET_FC_ISWEP(1)
1396                      ));
1397                 memcpy( sFrame.pHdr->sA3.abyAddr1, pMgmt->abyCurrBSSID, WLAN_BSSID_LEN);
1398                 memcpy( sFrame.pHdr->sA3.abyAddr2, pMgmt->abyMACAddr, WLAN_ADDR_LEN);
1399                 memcpy( sFrame.pHdr->sA3.abyAddr3, pMgmt->abyCurrBSSID, WLAN_BSSID_LEN);
1400                 *(sFrame.pwAuthAlgorithm) = *(pFrame->pwAuthAlgorithm);
1401                 *(sFrame.pwAuthSequence) = cpu_to_le16(3);
1402                 *(sFrame.pwStatus) = cpu_to_le16(WLAN_MGMT_STATUS_SUCCESS);
1403                 sFrame.pChallenge = (PWLAN_IE_CHALLENGE)(sFrame.pBuf + sFrame.len);
1404                 sFrame.len += WLAN_CHALLENGE_IE_LEN;
1405                 sFrame.pChallenge->byElementID = WLAN_EID_CHALLENGE;
1406                 sFrame.pChallenge->len = WLAN_CHALLENGE_LEN;
1407                 memcpy( sFrame.pChallenge->abyChallenge, pFrame->pChallenge->abyChallenge, WLAN_CHALLENGE_LEN);
1408                 // Adjust the length fields
1409                 pTxPacket->cbMPDULen = sFrame.len;
1410                 pTxPacket->cbPayloadLen = sFrame.len - WLAN_HDR_ADDR3_LEN;
1411                 // send the frame
1412                 if (csMgmt_xmit(pDevice, pTxPacket) != CMD_STATUS_PENDING) {
1413                     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Mgt:Auth_reply sequence_2 tx failed.\n");
1414                 }
1415                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Mgt:Auth_reply sequence_2 tx ...\n");
1416             }
1417             else {
1418                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Mgt:rx Auth_reply sequence_2 status error ...\n");
1419                 if ( pDevice->eCommandState == WLAN_AUTHENTICATE_WAIT ) {
1420                         /* spin_unlock_irq(&pDevice->lock);
1421                            vCommandTimerWait((void *) pDevice, 0);
1422                            spin_lock_irq(&pDevice->lock); */
1423                 }
1424                 s_vMgrLogStatus(pMgmt, cpu_to_le16((*(pFrame->pwStatus))));
1425             }
1426             break;
1427         default:
1428             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Mgt: rx auth.seq = 2 unknown AuthAlgorithm=%d\n", cpu_to_le16((*(pFrame->pwAuthAlgorithm))));
1429             break;
1430     }
1431     return;
1432 }
1433
1434
1435
1436 /*+
1437  *
1438  * Routine Description:
1439  *   Handles incoming authen frames with sequence 3.  Currently
1440  *   assumes we're an AP.  This function assumes the frame has
1441  *   already been successfully decrypted.
1442  *
1443  *
1444  * Return Value:
1445  *    None.
1446  *
1447 -*/
1448
1449 static
1450 void
1451 s_vMgrRxAuthenSequence_3(
1452      PSDevice pDevice,
1453      PSMgmtObject pMgmt,
1454      PWLAN_FR_AUTHEN pFrame
1455     )
1456 {
1457     PSTxMgmtPacket      pTxPacket = NULL;
1458     unsigned int                uStatusCode = 0 ;
1459     unsigned int                uNodeIndex = 0;
1460     WLAN_FR_AUTHEN      sFrame;
1461
1462     if (!WLAN_GET_FC_ISWEP(pFrame->pHdr->sA3.wFrameCtl)) {
1463         uStatusCode = WLAN_MGMT_STATUS_CHALLENGE_FAIL;
1464         goto reply;
1465     }
1466     if (BSSbIsSTAInNodeDB(pDevice, pFrame->pHdr->sA3.abyAddr2, &uNodeIndex)) {
1467          if (pMgmt->sNodeDBTable[uNodeIndex].byAuthSequence != 1) {
1468             uStatusCode = WLAN_MGMT_STATUS_RX_AUTH_NOSEQ;
1469             goto reply;
1470          }
1471          if (memcmp(pMgmt->abyChallenge, pFrame->pChallenge->abyChallenge, WLAN_CHALLENGE_LEN) != 0) {
1472             uStatusCode = WLAN_MGMT_STATUS_CHALLENGE_FAIL;
1473             goto reply;
1474          }
1475     }
1476     else {
1477         uStatusCode = WLAN_MGMT_STATUS_UNSPEC_FAILURE;
1478         goto reply;
1479     }
1480
1481     if (uNodeIndex) {
1482         pMgmt->sNodeDBTable[uNodeIndex].eNodeState = NODE_AUTH;
1483         pMgmt->sNodeDBTable[uNodeIndex].byAuthSequence = 0;
1484     }
1485     uStatusCode = WLAN_MGMT_STATUS_SUCCESS;
1486     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Challenge text check ok..\n");
1487
1488 reply:
1489     // send auth reply
1490     pTxPacket = (PSTxMgmtPacket)pMgmt->pbyMgmtPacketPool;
1491     memset(pTxPacket, 0, sizeof(STxMgmtPacket) + WLAN_AUTHEN_FR_MAXLEN);
1492     pTxPacket->p80211Header = (PUWLAN_80211HDR)((PBYTE)pTxPacket + sizeof(STxMgmtPacket));
1493     sFrame.pBuf = (PBYTE)pTxPacket->p80211Header;
1494     sFrame.len = WLAN_AUTHEN_FR_MAXLEN;
1495     // format buffer structure
1496     vMgrEncodeAuthen(&sFrame);
1497     /* insert values */
1498     sFrame.pHdr->sA3.wFrameCtl = cpu_to_le16(
1499          (
1500          WLAN_SET_FC_FTYPE(WLAN_TYPE_MGR) |
1501          WLAN_SET_FC_FSTYPE(WLAN_FSTYPE_AUTHEN)|
1502          WLAN_SET_FC_ISWEP(0)
1503          ));
1504     memcpy( sFrame.pHdr->sA3.abyAddr1, pFrame->pHdr->sA3.abyAddr2, WLAN_ADDR_LEN);
1505     memcpy( sFrame.pHdr->sA3.abyAddr2, pMgmt->abyMACAddr, WLAN_ADDR_LEN);
1506     memcpy( sFrame.pHdr->sA3.abyAddr3, pMgmt->abyCurrBSSID, WLAN_BSSID_LEN);
1507     *(sFrame.pwAuthAlgorithm) = *(pFrame->pwAuthAlgorithm);
1508     *(sFrame.pwAuthSequence) = cpu_to_le16(4);
1509     *(sFrame.pwStatus) = cpu_to_le16(uStatusCode);
1510
1511     /* Adjust the length fields */
1512     pTxPacket->cbMPDULen = sFrame.len;
1513     pTxPacket->cbPayloadLen = sFrame.len - WLAN_HDR_ADDR3_LEN;
1514     // send the frame
1515     if (pDevice->bEnableHostapd) {
1516         return;
1517     }
1518     if (csMgmt_xmit(pDevice, pTxPacket) != CMD_STATUS_PENDING) {
1519         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Mgt:Authreq_reply sequence_4 tx failed.\n");
1520     }
1521     return;
1522
1523 }
1524
1525
1526
1527 /*+
1528  *
1529  * Routine Description:
1530  *   Handles incoming authen frames with sequence 4
1531  *
1532  *
1533  * Return Value:
1534  *    None.
1535  *
1536 -*/
1537 static
1538 void
1539 s_vMgrRxAuthenSequence_4(
1540      PSDevice pDevice,
1541      PSMgmtObject pMgmt,
1542      PWLAN_FR_AUTHEN pFrame
1543     )
1544 {
1545
1546     if ( cpu_to_le16((*(pFrame->pwStatus))) == WLAN_MGMT_STATUS_SUCCESS ){
1547         DBG_PRT(MSG_LEVEL_INFO, KERN_INFO "802.11 Authen (SHAREDKEY) Successful.\n");
1548         pMgmt->eCurrState = WMAC_STATE_AUTH;
1549         timer_expire(pDevice->sTimerCommand, 0);
1550     }
1551     else{
1552         DBG_PRT(MSG_LEVEL_INFO, KERN_INFO "802.11 Authen (SHAREDKEY) Failed.\n");
1553         s_vMgrLogStatus(pMgmt, cpu_to_le16((*(pFrame->pwStatus))) );
1554         pMgmt->eCurrState = WMAC_STATE_IDLE;
1555     }
1556
1557     if ( pDevice->eCommandState == WLAN_AUTHENTICATE_WAIT ) {
1558         /* spin_unlock_irq(&pDevice->lock);
1559            vCommandTimerWait((void *) pDevice, 0);
1560            spin_lock_irq(&pDevice->lock); */
1561     }
1562 }
1563
1564 /*+
1565  *
1566  * Routine Description:
1567  *   Handles incoming disassociation frames
1568  *
1569  *
1570  * Return Value:
1571  *    None.
1572  *
1573 -*/
1574
1575 static
1576 void
1577 s_vMgrRxDisassociation(
1578      PSDevice pDevice,
1579      PSMgmtObject pMgmt,
1580      PSRxMgmtPacket pRxPacket
1581     )
1582 {
1583     WLAN_FR_DISASSOC    sFrame;
1584     unsigned int        uNodeIndex = 0;
1585     CMD_STATUS          CmdStatus;
1586     viawget_wpa_header *wpahdr;
1587
1588     if ( pMgmt->eCurrMode == WMAC_MODE_ESS_AP ){
1589         // if is acting an AP..
1590         // a STA is leaving this BSS..
1591         sFrame.len = pRxPacket->cbMPDULen;
1592         sFrame.pBuf = (PBYTE)pRxPacket->p80211Header;
1593         if (BSSbIsSTAInNodeDB(pDevice, pRxPacket->p80211Header->sA3.abyAddr2, &uNodeIndex)) {
1594             BSSvRemoveOneNode(pDevice, uNodeIndex);
1595         }
1596         else {
1597             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Rx disassoc, sta not found\n");
1598         }
1599     }
1600     else if (pMgmt->eCurrMode == WMAC_MODE_ESS_STA ){
1601         sFrame.len = pRxPacket->cbMPDULen;
1602         sFrame.pBuf = (PBYTE)pRxPacket->p80211Header;
1603         vMgrDecodeDisassociation(&sFrame);
1604         DBG_PRT(MSG_LEVEL_NOTICE, KERN_INFO "AP disassociated me, reason=%d.\n", cpu_to_le16(*(sFrame.pwReason)));
1605
1606           pDevice->fWPA_Authened = FALSE;
1607         if ((pDevice->bWPADEVUp) && (pDevice->skb != NULL)) {
1608              wpahdr = (viawget_wpa_header *)pDevice->skb->data;
1609              wpahdr->type = VIAWGET_DISASSOC_MSG;
1610              wpahdr->resp_ie_len = 0;
1611              wpahdr->req_ie_len = 0;
1612              skb_put(pDevice->skb, sizeof(viawget_wpa_header));
1613              pDevice->skb->dev = pDevice->wpadev;
1614              skb_reset_mac_header(pDevice->skb);
1615              pDevice->skb->pkt_type = PACKET_HOST;
1616              pDevice->skb->protocol = htons(ETH_P_802_2);
1617              memset(pDevice->skb->cb, 0, sizeof(pDevice->skb->cb));
1618              netif_rx(pDevice->skb);
1619              pDevice->skb = dev_alloc_skb((int)pDevice->rx_buf_sz);
1620          }
1621
1622         //TODO: do something let upper layer know or
1623         //try to send associate packet again because of inactivity timeout
1624         if (pMgmt->eCurrState == WMAC_STATE_ASSOC) {
1625                 pDevice->bLinkPass = FALSE;
1626                 pMgmt->sNodeDBTable[0].bActive = FALSE;
1627                pDevice->byReAssocCount = 0;
1628                 pMgmt->eCurrState = WMAC_STATE_AUTH;  // jump back to the auth state!
1629                 pDevice->eCommandState = WLAN_ASSOCIATE_WAIT;
1630             vMgrReAssocBeginSta((PSDevice)pDevice, pMgmt, &CmdStatus);
1631               if(CmdStatus == CMD_STATUS_PENDING) {
1632                   pDevice->byReAssocCount ++;
1633                   return;       //mike add: you'll retry for many times, so it cann't be regarded as disconnected!
1634               }
1635         }
1636
1637   // if(pDevice->bWPASuppWextEnabled == TRUE)
1638       {
1639         union iwreq_data  wrqu;
1640         memset(&wrqu, 0, sizeof (wrqu));
1641         wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1642         PRINT_K("wireless_send_event--->SIOCGIWAP(disassociated)\n");
1643         wireless_send_event(pDevice->dev, SIOCGIWAP, &wrqu, NULL);
1644      }
1645     }
1646     /* else, ignore it */
1647
1648     return;
1649 }
1650
1651
1652 /*+
1653  *
1654  * Routine Description:
1655  *   Handles incoming deauthentication frames
1656  *
1657  *
1658  * Return Value:
1659  *    None.
1660  *
1661 -*/
1662
1663 static
1664 void
1665 s_vMgrRxDeauthentication(
1666      PSDevice pDevice,
1667      PSMgmtObject pMgmt,
1668      PSRxMgmtPacket pRxPacket
1669     )
1670 {
1671     WLAN_FR_DEAUTHEN    sFrame;
1672     unsigned int        uNodeIndex = 0;
1673     viawget_wpa_header *wpahdr;
1674
1675
1676     if (pMgmt->eCurrMode == WMAC_MODE_ESS_AP ){
1677         //Todo:
1678         // if is acting an AP..
1679         // a STA is leaving this BSS..
1680         sFrame.len = pRxPacket->cbMPDULen;
1681         sFrame.pBuf = (PBYTE)pRxPacket->p80211Header;
1682         if (BSSbIsSTAInNodeDB(pDevice, pRxPacket->p80211Header->sA3.abyAddr2, &uNodeIndex)) {
1683             BSSvRemoveOneNode(pDevice, uNodeIndex);
1684         }
1685         else {
1686             DBG_PRT(MSG_LEVEL_NOTICE, KERN_INFO "Rx deauth, sta not found\n");
1687         }
1688     }
1689     else {
1690         if (pMgmt->eCurrMode == WMAC_MODE_ESS_STA ) {
1691             sFrame.len = pRxPacket->cbMPDULen;
1692             sFrame.pBuf = (PBYTE)pRxPacket->p80211Header;
1693             vMgrDecodeDeauthen(&sFrame);
1694            pDevice->fWPA_Authened = FALSE;
1695             DBG_PRT(MSG_LEVEL_NOTICE, KERN_INFO  "AP deauthed me, reason=%d.\n", cpu_to_le16((*(sFrame.pwReason))));
1696             // TODO: update BSS list for specific BSSID if pre-authentication case
1697             if (!compare_ether_addr(sFrame.pHdr->sA3.abyAddr3,
1698                                     pMgmt->abyCurrBSSID)) {
1699                 if (pMgmt->eCurrState >= WMAC_STATE_AUTHPENDING) {
1700                     pMgmt->sNodeDBTable[0].bActive = FALSE;
1701                     pMgmt->eCurrMode = WMAC_MODE_STANDBY;
1702                     pMgmt->eCurrState = WMAC_STATE_IDLE;
1703                     netif_stop_queue(pDevice->dev);
1704                     pDevice->bLinkPass = FALSE;
1705                     ControlvMaskByte(pDevice,MESSAGE_REQUEST_MACREG,MAC_REG_PAPEDELAY,LEDSTS_STS,LEDSTS_SLOW);
1706                 }
1707             }
1708
1709             if ((pDevice->bWPADEVUp) && (pDevice->skb != NULL)) {
1710                  wpahdr = (viawget_wpa_header *)pDevice->skb->data;
1711                  wpahdr->type = VIAWGET_DISASSOC_MSG;
1712                  wpahdr->resp_ie_len = 0;
1713                  wpahdr->req_ie_len = 0;
1714                  skb_put(pDevice->skb, sizeof(viawget_wpa_header));
1715                  pDevice->skb->dev = pDevice->wpadev;
1716                  skb_reset_mac_header(pDevice->skb);
1717                  pDevice->skb->pkt_type = PACKET_HOST;
1718                  pDevice->skb->protocol = htons(ETH_P_802_2);
1719                  memset(pDevice->skb->cb, 0, sizeof(pDevice->skb->cb));
1720                  netif_rx(pDevice->skb);
1721                  pDevice->skb = dev_alloc_skb((int)pDevice->rx_buf_sz);
1722            }
1723
1724   // if(pDevice->bWPASuppWextEnabled == TRUE)
1725       {
1726         union iwreq_data  wrqu;
1727         memset(&wrqu, 0, sizeof (wrqu));
1728         wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1729         PRINT_K("wireless_send_event--->SIOCGIWAP(disauthen)\n");
1730         wireless_send_event(pDevice->dev, SIOCGIWAP, &wrqu, NULL);
1731      }
1732
1733         }
1734         /* else, ignore it.  TODO: IBSS authentication service
1735             would be implemented here */
1736     };
1737     return;
1738 }
1739
1740 /*+
1741  *
1742  * Routine Description:
1743  * check if current channel is match ZoneType.
1744  *for USA:1~11;
1745  *      Japan:1~13;
1746  *      Europe:1~13
1747  * Return Value:
1748  *               True:exceed;
1749  *                False:normal case
1750 -*/
1751 static BOOL
1752 ChannelExceedZoneType(
1753      PSDevice pDevice,
1754      BYTE byCurrChannel
1755     )
1756 {
1757   BOOL exceed=FALSE;
1758
1759   switch(pDevice->byZoneType) {
1760         case 0x00:                  //USA:1~11
1761                      if((byCurrChannel<1) ||(byCurrChannel>11))
1762                         exceed = TRUE;
1763                  break;
1764         case 0x01:                  //Japan:1~13
1765         case 0x02:                  //Europe:1~13
1766                      if((byCurrChannel<1) ||(byCurrChannel>13))
1767                         exceed = TRUE;
1768                  break;
1769         default:                    //reserve for other zonetype
1770                 break;
1771   }
1772
1773   return exceed;
1774 }
1775
1776 /*+
1777  *
1778  * Routine Description:
1779  *   Handles and analysis incoming beacon frames.
1780  *
1781  *
1782  * Return Value:
1783  *    None.
1784  *
1785 -*/
1786
1787 static
1788 void
1789 s_vMgrRxBeacon(
1790      PSDevice pDevice,
1791      PSMgmtObject pMgmt,
1792      PSRxMgmtPacket pRxPacket,
1793      BOOL bInScan
1794     )
1795 {
1796
1797     PKnownBSS           pBSSList;
1798     WLAN_FR_BEACON      sFrame;
1799     QWORD               qwTSFOffset;
1800     BOOL                bIsBSSIDEqual = FALSE;
1801     BOOL                bIsSSIDEqual = FALSE;
1802     BOOL                bTSFLargeDiff = FALSE;
1803     BOOL                bTSFOffsetPostive = FALSE;
1804     BOOL                bUpdateTSF = FALSE;
1805     BOOL                bIsAPBeacon = FALSE;
1806     BOOL                bIsChannelEqual = FALSE;
1807     unsigned int                uLocateByteIndex;
1808     BYTE                byTIMBitOn = 0;
1809     WORD                wAIDNumber = 0;
1810     unsigned int                uNodeIndex;
1811     QWORD               qwTimestamp, qwLocalTSF;
1812     QWORD               qwCurrTSF;
1813     WORD                wStartIndex = 0;
1814     WORD                wAIDIndex = 0;
1815     BYTE                byCurrChannel = pRxPacket->byRxChannel;
1816     ERPObject           sERP;
1817     unsigned int                uRateLen = WLAN_RATES_MAXLEN;
1818     BOOL                bChannelHit = FALSE;
1819     BYTE                byOldPreambleType;
1820
1821
1822
1823      if (pMgmt->eCurrMode == WMAC_MODE_ESS_AP)
1824         return;
1825
1826     memset(&sFrame, 0, sizeof(WLAN_FR_BEACON));
1827     sFrame.len = pRxPacket->cbMPDULen;
1828     sFrame.pBuf = (PBYTE)pRxPacket->p80211Header;
1829
1830     // decode the beacon frame
1831     vMgrDecodeBeacon(&sFrame);
1832
1833     if ((sFrame.pwBeaconInterval == NULL)
1834         || (sFrame.pwCapInfo == NULL)
1835         || (sFrame.pSSID == NULL)
1836         || (sFrame.pSuppRates == NULL)) {
1837
1838         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Rx beacon frame error\n");
1839         return;
1840     }
1841
1842     if( byCurrChannel > CB_MAX_CHANNEL_24G )
1843     {
1844         if (sFrame.pDSParms != NULL) {
1845             if (byCurrChannel == RFaby11aChannelIndex[sFrame.pDSParms->byCurrChannel-1])
1846                 bChannelHit = TRUE;
1847             byCurrChannel = RFaby11aChannelIndex[sFrame.pDSParms->byCurrChannel-1];
1848         } else {
1849             bChannelHit = TRUE;
1850         }
1851
1852     } else {
1853         if (sFrame.pDSParms != NULL) {
1854             if (byCurrChannel == sFrame.pDSParms->byCurrChannel)
1855                 bChannelHit = TRUE;
1856             byCurrChannel = sFrame.pDSParms->byCurrChannel;
1857         } else {
1858             bChannelHit = TRUE;
1859         }
1860     }
1861
1862 if(ChannelExceedZoneType(pDevice,byCurrChannel)==TRUE)
1863       return;
1864
1865     if (sFrame.pERP != NULL) {
1866         sERP.byERP = sFrame.pERP->byContext;
1867         sERP.bERPExist = TRUE;
1868
1869     } else {
1870         sERP.bERPExist = FALSE;
1871         sERP.byERP = 0;
1872     }
1873
1874     pBSSList = BSSpAddrIsInBSSList((void *) pDevice,
1875                                    sFrame.pHdr->sA3.abyAddr3,
1876                                    sFrame.pSSID);
1877     if (pBSSList == NULL) {
1878         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Beacon/insert: RxChannel = : %d\n", byCurrChannel);
1879         BSSbInsertToBSSList((void *) pDevice,
1880                             sFrame.pHdr->sA3.abyAddr3,
1881                             *sFrame.pqwTimestamp,
1882                             *sFrame.pwBeaconInterval,
1883                             *sFrame.pwCapInfo,
1884                             byCurrChannel,
1885                             sFrame.pSSID,
1886                             sFrame.pSuppRates,
1887                             sFrame.pExtSuppRates,
1888                             &sERP,
1889                             sFrame.pRSN,
1890                             sFrame.pRSNWPA,
1891                             sFrame.pIE_Country,
1892                             sFrame.pIE_Quiet,
1893                             sFrame.len - WLAN_HDR_ADDR3_LEN,
1894                             sFrame.pHdr->sA4.abyAddr4,   // payload of beacon
1895                             (void *) pRxPacket);
1896     }
1897     else {
1898 //        DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"update bcn: RxChannel = : %d\n", byCurrChannel);
1899         BSSbUpdateToBSSList((void *) pDevice,
1900                             *sFrame.pqwTimestamp,
1901                             *sFrame.pwBeaconInterval,
1902                             *sFrame.pwCapInfo,
1903                             byCurrChannel,
1904                             bChannelHit,
1905                             sFrame.pSSID,
1906                             sFrame.pSuppRates,
1907                             sFrame.pExtSuppRates,
1908                             &sERP,
1909                             sFrame.pRSN,
1910                             sFrame.pRSNWPA,
1911                             sFrame.pIE_Country,
1912                             sFrame.pIE_Quiet,
1913                             pBSSList,
1914                             sFrame.len - WLAN_HDR_ADDR3_LEN,
1915                             sFrame.pHdr->sA4.abyAddr4,   // payload of probresponse
1916                             (void *) pRxPacket);
1917
1918     }
1919
1920     if (bInScan) {
1921         return;
1922     }
1923
1924     if(byCurrChannel == (BYTE)pMgmt->uCurrChannel)
1925        bIsChannelEqual = TRUE;
1926
1927     if (bIsChannelEqual && (pMgmt->eCurrMode == WMAC_MODE_ESS_AP)) {
1928
1929         // if rx beacon without ERP field
1930         if (sERP.bERPExist) {
1931             if (WLAN_GET_ERP_USE_PROTECTION(sERP.byERP)){
1932                 pDevice->byERPFlag |= WLAN_SET_ERP_USE_PROTECTION(1);
1933                 pDevice->wUseProtectCntDown = USE_PROTECT_PERIOD;
1934             }
1935         }
1936         else {
1937             pDevice->byERPFlag |= WLAN_SET_ERP_USE_PROTECTION(1);
1938             pDevice->wUseProtectCntDown = USE_PROTECT_PERIOD;
1939         }
1940
1941         if (pMgmt->eCurrMode == WMAC_MODE_IBSS_STA) {
1942             if(!WLAN_GET_CAP_INFO_SHORTPREAMBLE(*sFrame.pwCapInfo))
1943                 pDevice->byERPFlag |= WLAN_SET_ERP_BARKER_MODE(1);
1944             if(!sERP.bERPExist)
1945                 pDevice->byERPFlag |= WLAN_SET_ERP_NONERP_PRESENT(1);
1946         }
1947     }
1948
1949     // check if BSSID the same
1950     if (memcmp(sFrame.pHdr->sA3.abyAddr3,
1951                pMgmt->abyCurrBSSID,
1952                WLAN_BSSID_LEN) == 0) {
1953
1954         bIsBSSIDEqual = TRUE;
1955         pDevice->uCurrRSSI = pRxPacket->uRSSI;
1956         pDevice->byCurrSQ = pRxPacket->bySQ;
1957         if (pMgmt->sNodeDBTable[0].uInActiveCount != 0) {
1958             pMgmt->sNodeDBTable[0].uInActiveCount = 0;
1959             //DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"BCN:Wake Count= [%d]\n", pMgmt->wCountToWakeUp);
1960         }
1961     }
1962     // check if SSID the same
1963     if (sFrame.pSSID->len == ((PWLAN_IE_SSID)pMgmt->abyCurrSSID)->len) {
1964         if (memcmp(sFrame.pSSID->abySSID,
1965                    ((PWLAN_IE_SSID)pMgmt->abyCurrSSID)->abySSID,
1966                    sFrame.pSSID->len
1967                    ) == 0) {
1968             bIsSSIDEqual = TRUE;
1969         }
1970     }
1971
1972     if ((WLAN_GET_CAP_INFO_ESS(*sFrame.pwCapInfo)== TRUE) &&
1973         (bIsBSSIDEqual == TRUE) &&
1974         (bIsSSIDEqual == TRUE) &&
1975         (pMgmt->eCurrMode == WMAC_MODE_ESS_STA) &&
1976         (pMgmt->eCurrState == WMAC_STATE_ASSOC)) {
1977         // add state check to prevent reconnect fail since we'll receive Beacon
1978
1979         bIsAPBeacon = TRUE;
1980         if (pBSSList != NULL) {
1981
1982                 // Sync ERP field
1983                 if ((pBSSList->sERP.bERPExist == TRUE) && (pDevice->byBBType == BB_TYPE_11G)) {
1984                     if ((pBSSList->sERP.byERP & WLAN_EID_ERP_USE_PROTECTION) != pDevice->bProtectMode) {//0000 0010
1985                         pDevice->bProtectMode = (pBSSList->sERP.byERP & WLAN_EID_ERP_USE_PROTECTION);
1986                         if (pDevice->bProtectMode) {
1987                             MACvEnableProtectMD(pDevice);
1988                         } else {
1989                             MACvDisableProtectMD(pDevice);
1990                         }
1991                         vUpdateIFS(pDevice);
1992                     }
1993                     if ((pBSSList->sERP.byERP & WLAN_EID_ERP_NONERP_PRESENT) != pDevice->bNonERPPresent) {//0000 0001
1994                         pDevice->bNonERPPresent = (pBSSList->sERP.byERP & WLAN_EID_ERP_USE_PROTECTION);
1995                     }
1996                     if ((pBSSList->sERP.byERP & WLAN_EID_ERP_BARKER_MODE) != pDevice->bBarkerPreambleMd) {//0000 0100
1997                         pDevice->bBarkerPreambleMd = (pBSSList->sERP.byERP & WLAN_EID_ERP_BARKER_MODE);
1998                         //BarkerPreambleMd has higher priority than shortPreamble bit in Cap
1999                         if (pDevice->bBarkerPreambleMd) {
2000                             MACvEnableBarkerPreambleMd(pDevice);
2001                         } else {
2002                             MACvDisableBarkerPreambleMd(pDevice);
2003                         }
2004                     }
2005                 }
2006                 // Sync Short Slot Time
2007                 if (WLAN_GET_CAP_INFO_SHORTSLOTTIME(pBSSList->wCapInfo) != pDevice->bShortSlotTime) {
2008                     BOOL    bShortSlotTime;
2009
2010                     bShortSlotTime = WLAN_GET_CAP_INFO_SHORTSLOTTIME(pBSSList->wCapInfo);
2011                     //DBG_PRN_WLAN05(("Set Short Slot Time: %d\n", pDevice->bShortSlotTime));
2012                     //Kyle check if it is OK to set G.
2013                     if (pDevice->byBBType == BB_TYPE_11A) {
2014                         bShortSlotTime = TRUE;
2015                     }
2016                     else if (pDevice->byBBType == BB_TYPE_11B) {
2017                         bShortSlotTime = FALSE;
2018                     }
2019                     if (bShortSlotTime != pDevice->bShortSlotTime) {
2020                         pDevice->bShortSlotTime = bShortSlotTime;
2021                         BBvSetShortSlotTime(pDevice);
2022                         vUpdateIFS(pDevice);
2023                     }
2024                 }
2025
2026                 //
2027                 // Preamble may change dynamically
2028                 //
2029                 byOldPreambleType = pDevice->byPreambleType;
2030                 if (WLAN_GET_CAP_INFO_SHORTPREAMBLE(pBSSList->wCapInfo)) {
2031                     pDevice->byPreambleType = pDevice->byShortPreamble;
2032                 }
2033                 else {
2034                     pDevice->byPreambleType = 0;
2035                 }
2036                 if (pDevice->byPreambleType != byOldPreambleType)
2037                     CARDvSetRSPINF(pDevice, (BYTE)pDevice->byBBType);
2038             //
2039             // Basic Rate Set may change dynamically
2040             //
2041             if (pBSSList->eNetworkTypeInUse == PHY_TYPE_11B) {
2042                 uRateLen = WLAN_RATES_MAXLEN_11B;
2043             }
2044             pMgmt->abyCurrSuppRates[1] = RATEuSetIE((PWLAN_IE_SUPP_RATES)pBSSList->abySuppRates,
2045                                                     (PWLAN_IE_SUPP_RATES)pMgmt->abyCurrSuppRates,
2046                                                     uRateLen);
2047             pMgmt->abyCurrExtSuppRates[1] = RATEuSetIE((PWLAN_IE_SUPP_RATES)pBSSList->abyExtSuppRates,
2048                                                     (PWLAN_IE_SUPP_RATES)pMgmt->abyCurrExtSuppRates,
2049                                                     uRateLen);
2050             RATEvParseMaxRate((void *)pDevice,
2051                                (PWLAN_IE_SUPP_RATES)pMgmt->abyCurrSuppRates,
2052                                (PWLAN_IE_SUPP_RATES)pMgmt->abyCurrExtSuppRates,
2053                                TRUE,
2054                                &(pMgmt->sNodeDBTable[0].wMaxBasicRate),
2055                                &(pMgmt->sNodeDBTable[0].wMaxSuppRate),
2056                                &(pMgmt->sNodeDBTable[0].wSuppRate),
2057                                &(pMgmt->sNodeDBTable[0].byTopCCKBasicRate),
2058                                &(pMgmt->sNodeDBTable[0].byTopOFDMBasicRate)
2059                               );
2060
2061         }
2062     }
2063
2064 //    DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Beacon 2 \n");
2065     // check if CF field exisit
2066     if (WLAN_GET_CAP_INFO_ESS(*sFrame.pwCapInfo)) {
2067         if (sFrame.pCFParms->wCFPDurRemaining > 0) {
2068             // TODO: deal with CFP period to set NAV
2069         }
2070     }
2071
2072     HIDWORD(qwTimestamp) = cpu_to_le32(HIDWORD(*sFrame.pqwTimestamp));
2073     LODWORD(qwTimestamp) = cpu_to_le32(LODWORD(*sFrame.pqwTimestamp));
2074     HIDWORD(qwLocalTSF) = HIDWORD(pRxPacket->qwLocalTSF);
2075     LODWORD(qwLocalTSF) = LODWORD(pRxPacket->qwLocalTSF);
2076
2077     // check if beacon TSF larger or small than our local TSF
2078     if (HIDWORD(qwTimestamp) == HIDWORD(qwLocalTSF)) {
2079         if (LODWORD(qwTimestamp) >= LODWORD(qwLocalTSF)) {
2080             bTSFOffsetPostive = TRUE;
2081         }
2082         else {
2083             bTSFOffsetPostive = FALSE;
2084         }
2085     }
2086     else if (HIDWORD(qwTimestamp) > HIDWORD(qwLocalTSF)) {
2087         bTSFOffsetPostive = TRUE;
2088     }
2089     else if (HIDWORD(qwTimestamp) < HIDWORD(qwLocalTSF)) {
2090         bTSFOffsetPostive = FALSE;
2091     }
2092
2093     if (bTSFOffsetPostive) {
2094         qwTSFOffset = CARDqGetTSFOffset(pRxPacket->byRxRate, (qwTimestamp), (qwLocalTSF));
2095     }
2096     else {
2097         qwTSFOffset = CARDqGetTSFOffset(pRxPacket->byRxRate, (qwLocalTSF), (qwTimestamp));
2098     }
2099
2100     if (HIDWORD(qwTSFOffset) != 0 ||
2101         (LODWORD(qwTSFOffset) > TRIVIAL_SYNC_DIFFERENCE )) {
2102          bTSFLargeDiff = TRUE;
2103     }
2104
2105
2106     // if infra mode
2107     if (bIsAPBeacon == TRUE) {
2108
2109         // Infra mode: Local TSF always follow AP's TSF if Difference huge.
2110         if (bTSFLargeDiff)
2111             bUpdateTSF = TRUE;
2112
2113         if ((pDevice->bEnablePSMode == TRUE) && (sFrame.pTIM)) {
2114
2115                 /* deal with DTIM, analysis TIM */
2116             pMgmt->bMulticastTIM = WLAN_MGMT_IS_MULTICAST_TIM(sFrame.pTIM->byBitMapCtl) ? TRUE : FALSE ;
2117             pMgmt->byDTIMCount = sFrame.pTIM->byDTIMCount;
2118             pMgmt->byDTIMPeriod = sFrame.pTIM->byDTIMPeriod;
2119             wAIDNumber = pMgmt->wCurrAID & ~(BIT14|BIT15);
2120
2121             // check if AID in TIM field bit on
2122             // wStartIndex = N1
2123             wStartIndex = WLAN_MGMT_GET_TIM_OFFSET(sFrame.pTIM->byBitMapCtl) << 1;
2124             // AIDIndex = N2
2125             wAIDIndex = (wAIDNumber >> 3);
2126             if ((wAIDNumber > 0) && (wAIDIndex >= wStartIndex)) {
2127                 uLocateByteIndex = wAIDIndex - wStartIndex;
2128                 // len = byDTIMCount + byDTIMPeriod + byDTIMPeriod + byVirtBitMap[0~250]
2129                 if (sFrame.pTIM->len >= (uLocateByteIndex + 4)) {
2130                     byTIMBitOn  = (0x01) << ((wAIDNumber) % 8);
2131                     pMgmt->bInTIM = sFrame.pTIM->byVirtBitMap[uLocateByteIndex] & byTIMBitOn ? TRUE : FALSE;
2132                 }
2133                 else {
2134                     pMgmt->bInTIM = FALSE;
2135                 };
2136             }
2137             else {
2138                 pMgmt->bInTIM = FALSE;
2139             };
2140
2141             if (pMgmt->bInTIM ||
2142                 (pMgmt->bMulticastTIM && (pMgmt->byDTIMCount == 0))) {
2143                 pMgmt->bInTIMWake = TRUE;
2144                 // send out ps-poll packet
2145 //                DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "BCN:In TIM\n");
2146                 if (pMgmt->bInTIM) {
2147                     PSvSendPSPOLL((PSDevice)pDevice);
2148 //                    DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "BCN:PS-POLL sent..\n");
2149                 }
2150
2151             }
2152             else {
2153                 pMgmt->bInTIMWake = FALSE;
2154                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "BCN: Not In TIM..\n");
2155                 if (pDevice->bPWBitOn == FALSE) {
2156                     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "BCN: Send Null Packet\n");
2157                     if (PSbSendNullPacket(pDevice))
2158                         pDevice->bPWBitOn = TRUE;
2159                 }
2160                 if(PSbConsiderPowerDown(pDevice, FALSE, FALSE)) {
2161                    DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "BCN: Power down now...\n");
2162                 }
2163             }
2164
2165         }
2166
2167     }
2168     // if adhoc mode
2169     if ((pMgmt->eCurrMode == WMAC_MODE_IBSS_STA) && !bIsAPBeacon && bIsChannelEqual) {
2170         if (bIsBSSIDEqual) {
2171             // Use sNodeDBTable[0].uInActiveCount as IBSS beacons received count.
2172                     if (pMgmt->sNodeDBTable[0].uInActiveCount != 0)
2173                             pMgmt->sNodeDBTable[0].uInActiveCount = 0;
2174
2175             // adhoc mode:TSF updated only when beacon larger then local TSF
2176             if (bTSFLargeDiff && bTSFOffsetPostive &&
2177                 (pMgmt->eCurrState == WMAC_STATE_JOINTED))
2178                 bUpdateTSF = TRUE;
2179
2180             // During dpc, already in spinlocked.
2181             if (BSSbIsSTAInNodeDB(pDevice, sFrame.pHdr->sA3.abyAddr2, &uNodeIndex)) {
2182
2183                 // Update the STA, (Technically the Beacons of all the IBSS nodes
2184                         // should be identical, but that's not happening in practice.
2185                 pMgmt->abyCurrSuppRates[1] = RATEuSetIE((PWLAN_IE_SUPP_RATES)sFrame.pSuppRates,
2186                                                         (PWLAN_IE_SUPP_RATES)pMgmt->abyCurrSuppRates,
2187                                                         WLAN_RATES_MAXLEN_11B);
2188                 RATEvParseMaxRate((void *)pDevice,
2189                                    (PWLAN_IE_SUPP_RATES)pMgmt->abyCurrSuppRates,
2190                                    NULL,
2191                                    TRUE,
2192                                    &(pMgmt->sNodeDBTable[uNodeIndex].wMaxBasicRate),
2193                                    &(pMgmt->sNodeDBTable[uNodeIndex].wMaxSuppRate),
2194                                    &(pMgmt->sNodeDBTable[uNodeIndex].wSuppRate),
2195                                    &(pMgmt->sNodeDBTable[uNodeIndex].byTopCCKBasicRate),
2196                                    &(pMgmt->sNodeDBTable[uNodeIndex].byTopOFDMBasicRate)
2197                                   );
2198                 pMgmt->sNodeDBTable[uNodeIndex].bShortPreamble = WLAN_GET_CAP_INFO_SHORTPREAMBLE(*sFrame.pwCapInfo);
2199                 pMgmt->sNodeDBTable[uNodeIndex].bShortSlotTime = WLAN_GET_CAP_INFO_SHORTSLOTTIME(*sFrame.pwCapInfo);
2200                 pMgmt->sNodeDBTable[uNodeIndex].uInActiveCount = 0;
2201             }
2202             else {
2203                 // Todo, initial Node content
2204                 BSSvCreateOneNode((PSDevice)pDevice, &uNodeIndex);
2205
2206                 pMgmt->abyCurrSuppRates[1] = RATEuSetIE((PWLAN_IE_SUPP_RATES)sFrame.pSuppRates,
2207                                                         (PWLAN_IE_SUPP_RATES)pMgmt->abyCurrSuppRates,
2208                                                         WLAN_RATES_MAXLEN_11B);
2209                 RATEvParseMaxRate((void *)pDevice,
2210                                    (PWLAN_IE_SUPP_RATES)pMgmt->abyCurrSuppRates,
2211                                    NULL,
2212                                    TRUE,
2213                                    &(pMgmt->sNodeDBTable[uNodeIndex].wMaxBasicRate),
2214                                    &(pMgmt->sNodeDBTable[uNodeIndex].wMaxSuppRate),
2215                                    &(pMgmt->sNodeDBTable[uNodeIndex].wSuppRate),
2216                                    &(pMgmt->sNodeDBTable[uNodeIndex].byTopCCKBasicRate),
2217                                    &(pMgmt->sNodeDBTable[uNodeIndex].byTopOFDMBasicRate)
2218                                  );
2219
2220                 memcpy(pMgmt->sNodeDBTable[uNodeIndex].abyMACAddr, sFrame.pHdr->sA3.abyAddr2, WLAN_ADDR_LEN);
2221                 pMgmt->sNodeDBTable[uNodeIndex].bShortPreamble = WLAN_GET_CAP_INFO_SHORTPREAMBLE(*sFrame.pwCapInfo);
2222                 pMgmt->sNodeDBTable[uNodeIndex].wTxDataRate = pMgmt->sNodeDBTable[uNodeIndex].wMaxSuppRate;
2223 /*
2224                 pMgmt->sNodeDBTable[uNodeIndex].bShortSlotTime = WLAN_GET_CAP_INFO_SHORTSLOTTIME(*sFrame.pwCapInfo);
2225                 if(pMgmt->sNodeDBTable[uNodeIndex].wMaxSuppRate > RATE_11M)
2226                        pMgmt->sNodeDBTable[uNodeIndex].bERPExist = TRUE;
2227 */
2228             }
2229
2230             // if other stations jointed, indicate connect to upper layer..
2231             if (pMgmt->eCurrState == WMAC_STATE_STARTED) {
2232                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Current IBSS State: [Started]........to: [Jointed] \n");
2233                 pMgmt->eCurrState = WMAC_STATE_JOINTED;
2234                 pDevice->bLinkPass = TRUE;
2235                 ControlvMaskByte(pDevice,MESSAGE_REQUEST_MACREG,MAC_REG_PAPEDELAY,LEDSTS_STS,LEDSTS_INTER);
2236                 if (netif_queue_stopped(pDevice->dev)){
2237                     netif_wake_queue(pDevice->dev);
2238                 }
2239                 pMgmt->sNodeDBTable[0].bActive = TRUE;
2240                 pMgmt->sNodeDBTable[0].uInActiveCount = 0;
2241
2242             }
2243         }
2244         else if (bIsSSIDEqual) {
2245
2246             // See other adhoc sta with the same SSID but BSSID is different.
2247             // adpot this vars only when TSF larger then us.
2248             if (bTSFLargeDiff && bTSFOffsetPostive) {
2249                  // we don't support ATIM under adhoc mode
2250                // if ( sFrame.pIBSSParms->wATIMWindow == 0) {
2251                      // adpot this vars
2252                      // TODO: check sFrame cap if privacy on, and support rate syn
2253                      memcpy(pMgmt->abyCurrBSSID, sFrame.pHdr->sA3.abyAddr3, WLAN_BSSID_LEN);
2254                      memcpy(pDevice->abyBSSID, pMgmt->abyCurrBSSID, WLAN_BSSID_LEN);
2255                      pMgmt->wCurrATIMWindow = cpu_to_le16(sFrame.pIBSSParms->wATIMWindow);
2256                      pMgmt->wCurrBeaconPeriod = cpu_to_le16(*sFrame.pwBeaconInterval);
2257                      pMgmt->abyCurrSuppRates[1] = RATEuSetIE((PWLAN_IE_SUPP_RATES)sFrame.pSuppRates,
2258                                                       (PWLAN_IE_SUPP_RATES)pMgmt->abyCurrSuppRates,
2259                                                       WLAN_RATES_MAXLEN_11B);
2260                      // set HW beacon interval and re-synchronizing....
2261                      DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Rejoining to Other Adhoc group with same SSID........\n");
2262
2263                      MACvWriteBeaconInterval(pDevice, pMgmt->wCurrBeaconPeriod);
2264                      CARDvAdjustTSF(pDevice, pRxPacket->byRxRate, qwTimestamp, pRxPacket->qwLocalTSF);
2265                      CARDvUpdateNextTBTT(pDevice, qwTimestamp, pMgmt->wCurrBeaconPeriod);
2266
2267                      // Turn off bssid filter to avoid filter others adhoc station which bssid is different.
2268                      MACvWriteBSSIDAddress(pDevice, pMgmt->abyCurrBSSID);
2269
2270                     byOldPreambleType = pDevice->byPreambleType;
2271                     if (WLAN_GET_CAP_INFO_SHORTPREAMBLE(*sFrame.pwCapInfo)) {
2272                         pDevice->byPreambleType = pDevice->byShortPreamble;
2273                     }
2274                     else {
2275                         pDevice->byPreambleType = 0;
2276                     }
2277                     if (pDevice->byPreambleType != byOldPreambleType)
2278                         CARDvSetRSPINF(pDevice, (BYTE)pDevice->byBBType);
2279
2280
2281                      // MACvRegBitsOff(pDevice->PortOffset, MAC_REG_RCR, RCR_BSSID);
2282                      // set highest basic rate
2283                      // s_vSetHighestBasicRate(pDevice, (PWLAN_IE_SUPP_RATES)pMgmt->abyCurrSuppRates);
2284                      // Prepare beacon frame
2285                         bMgrPrepareBeaconToSend((void *) pDevice, pMgmt);
2286               //  }
2287             }
2288         }
2289     }
2290     // endian issue ???
2291     // Update TSF
2292     if (bUpdateTSF) {
2293         CARDbGetCurrentTSF(pDevice, &qwCurrTSF);
2294         CARDvAdjustTSF(pDevice, pRxPacket->byRxRate, qwTimestamp , pRxPacket->qwLocalTSF);
2295         CARDbGetCurrentTSF(pDevice, &qwCurrTSF);
2296         CARDvUpdateNextTBTT(pDevice, qwTimestamp, pMgmt->wCurrBeaconPeriod);
2297     }
2298
2299     return;
2300 }
2301
2302 /*+
2303  *
2304  * Routine Description:
2305  *   Instructs the hw to create a bss using the supplied
2306  *   attributes. Note that this implementation only supports Ad-Hoc
2307  *   BSS creation.
2308  *
2309  *
2310  * Return Value:
2311  *    CMD_STATUS
2312  *
2313 -*/
2314
2315 void vMgrCreateOwnIBSS(void *hDeviceContext,
2316                        PCMD_STATUS pStatus)
2317 {
2318     PSDevice            pDevice = (PSDevice)hDeviceContext;
2319     PSMgmtObject        pMgmt = &(pDevice->sMgmtObj);
2320     WORD                wMaxBasicRate;
2321     WORD                wMaxSuppRate;
2322     BYTE                byTopCCKBasicRate;
2323     BYTE                byTopOFDMBasicRate;
2324     QWORD               qwCurrTSF;
2325     unsigned int                ii;
2326     BYTE    abyRATE[] = {0x82, 0x84, 0x8B, 0x96, 0x24, 0x30, 0x48, 0x6C, 0x0C, 0x12, 0x18, 0x60};
2327     BYTE    abyCCK_RATE[] = {0x82, 0x84, 0x8B, 0x96};
2328     BYTE    abyOFDM_RATE[] = {0x0C, 0x12, 0x18, 0x24, 0x30, 0x48, 0x60, 0x6C};
2329     WORD                wSuppRate;
2330
2331
2332
2333     HIDWORD(qwCurrTSF) = 0;
2334     LODWORD(qwCurrTSF) = 0;
2335
2336     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Create Basic Service Set .......\n");
2337
2338     if (pMgmt->eConfigMode == WMAC_CONFIG_IBSS_STA) {
2339         if ((pMgmt->eAuthenMode == WMAC_AUTH_WPANONE) &&
2340             (pDevice->eEncryptionStatus != Ndis802_11Encryption2Enabled) &&
2341             (pDevice->eEncryptionStatus != Ndis802_11Encryption3Enabled)) {
2342             // encryption mode error
2343             *pStatus = CMD_STATUS_FAILURE;
2344             return;
2345         }
2346     }
2347
2348     pMgmt->abyCurrSuppRates[0] = WLAN_EID_SUPP_RATES;
2349     pMgmt->abyCurrExtSuppRates[0] = WLAN_EID_EXTSUPP_RATES;
2350
2351     if (pMgmt->eConfigMode == WMAC_CONFIG_AP) {
2352         pMgmt->eCurrentPHYMode = pMgmt->byAPBBType;
2353     } else {
2354         if (pDevice->byBBType == BB_TYPE_11G)
2355             pMgmt->eCurrentPHYMode = PHY_TYPE_11G;
2356         if (pDevice->byBBType == BB_TYPE_11B)
2357             pMgmt->eCurrentPHYMode = PHY_TYPE_11B;
2358         if (pDevice->byBBType == BB_TYPE_11A)
2359             pMgmt->eCurrentPHYMode = PHY_TYPE_11A;
2360     }
2361
2362     if (pMgmt->eCurrentPHYMode != PHY_TYPE_11A) {
2363         pMgmt->abyCurrSuppRates[1] = WLAN_RATES_MAXLEN_11B;
2364         pMgmt->abyCurrExtSuppRates[1] = 0;
2365         for (ii = 0; ii < 4; ii++)
2366             pMgmt->abyCurrSuppRates[2+ii] = abyRATE[ii];
2367     } else {
2368         pMgmt->abyCurrSuppRates[1] = 8;
2369         pMgmt->abyCurrExtSuppRates[1] = 0;
2370         for (ii = 0; ii < 8; ii++)
2371             pMgmt->abyCurrSuppRates[2+ii] = abyRATE[ii];
2372     }
2373
2374
2375     if (pMgmt->eCurrentPHYMode == PHY_TYPE_11G) {
2376         pMgmt->abyCurrSuppRates[1] = 8;
2377         pMgmt->abyCurrExtSuppRates[1] = 4;
2378         for (ii = 0; ii < 4; ii++)
2379             pMgmt->abyCurrSuppRates[2+ii] =  abyCCK_RATE[ii];
2380         for (ii = 4; ii < 8; ii++)
2381             pMgmt->abyCurrSuppRates[2+ii] =  abyOFDM_RATE[ii-4];
2382         for (ii = 0; ii < 4; ii++)
2383             pMgmt->abyCurrExtSuppRates[2+ii] =  abyOFDM_RATE[ii+4];
2384     }
2385
2386
2387     // Disable Protect Mode
2388     pDevice->bProtectMode = 0;
2389     MACvDisableProtectMD(pDevice);
2390
2391     pDevice->bBarkerPreambleMd = 0;
2392     MACvDisableBarkerPreambleMd(pDevice);
2393
2394     // Kyle Test 2003.11.04
2395
2396     // set HW beacon interval
2397     if (pMgmt->wIBSSBeaconPeriod == 0)
2398         pMgmt->wIBSSBeaconPeriod = DEFAULT_IBSS_BI;
2399     MACvWriteBeaconInterval(pDevice, pMgmt->wIBSSBeaconPeriod);
2400
2401     CARDbGetCurrentTSF(pDevice, &qwCurrTSF);
2402     // clear TSF counter
2403     CARDbClearCurrentTSF(pDevice);
2404
2405     // enable TSF counter
2406     MACvRegBitsOn(pDevice,MAC_REG_TFTCTL,TFTCTL_TSFCNTREN);
2407     // set Next TBTT
2408     CARDvSetFirstNextTBTT(pDevice, pMgmt->wIBSSBeaconPeriod);
2409
2410     pMgmt->uIBSSChannel = pDevice->uChannel;
2411
2412     if (pMgmt->uIBSSChannel == 0)
2413         pMgmt->uIBSSChannel = DEFAULT_IBSS_CHANNEL;
2414
2415     // set channel and clear NAV
2416     CARDbSetMediaChannel(pDevice, pMgmt->uIBSSChannel);
2417     pMgmt->uCurrChannel = pMgmt->uIBSSChannel;
2418
2419     pDevice->byPreambleType = pDevice->byShortPreamble;
2420
2421     // set basic rate
2422
2423     RATEvParseMaxRate((void *)pDevice,
2424                       (PWLAN_IE_SUPP_RATES)pMgmt->abyCurrSuppRates,
2425                       (PWLAN_IE_SUPP_RATES)pMgmt->abyCurrExtSuppRates, TRUE,
2426                       &wMaxBasicRate, &wMaxSuppRate, &wSuppRate,
2427                       &byTopCCKBasicRate, &byTopOFDMBasicRate);
2428
2429
2430
2431     if (pDevice->byBBType == BB_TYPE_11A) {
2432         pDevice->bShortSlotTime = TRUE;
2433     } else {
2434         pDevice->bShortSlotTime = FALSE;
2435     }
2436     BBvSetShortSlotTime(pDevice);
2437     // vUpdateIFS() use pDevice->bShortSlotTime as parameter so it must be called
2438     // after setting ShortSlotTime.
2439     // CARDvSetBSSMode call vUpdateIFS()
2440     CARDvSetBSSMode(pDevice);
2441
2442     if (pMgmt->eConfigMode == WMAC_CONFIG_AP) {
2443         MACvRegBitsOn(pDevice, MAC_REG_HOSTCR, HOSTCR_AP);
2444         pMgmt->eCurrMode = WMAC_MODE_ESS_AP;
2445     }
2446
2447     if (pMgmt->eConfigMode == WMAC_CONFIG_IBSS_STA) {
2448         MACvRegBitsOn(pDevice, MAC_REG_HOSTCR, HOSTCR_ADHOC);
2449         pMgmt->eCurrMode = WMAC_MODE_IBSS_STA;
2450     }
2451
2452     // Adopt pre-configured IBSS vars to current vars
2453     pMgmt->eCurrState = WMAC_STATE_STARTED;
2454     pMgmt->wCurrBeaconPeriod = pMgmt->wIBSSBeaconPeriod;
2455     pMgmt->uCurrChannel = pMgmt->uIBSSChannel;
2456     pMgmt->wCurrATIMWindow = pMgmt->wIBSSATIMWindow;
2457     pDevice->uCurrRSSI = 0;
2458     pDevice->byCurrSQ = 0;
2459
2460     memcpy(pMgmt->abyDesireSSID,pMgmt->abyAdHocSSID,
2461                       ((PWLAN_IE_SSID)pMgmt->abyAdHocSSID)->len + WLAN_IEHDR_LEN);
2462
2463     memset(pMgmt->abyCurrSSID, 0, WLAN_IEHDR_LEN + WLAN_SSID_MAXLEN + 1);
2464     memcpy(pMgmt->abyCurrSSID,
2465            pMgmt->abyDesireSSID,
2466            ((PWLAN_IE_SSID)pMgmt->abyDesireSSID)->len + WLAN_IEHDR_LEN
2467           );
2468
2469     if (pMgmt->eCurrMode == WMAC_MODE_ESS_AP) {
2470         // AP mode BSSID = MAC addr
2471         memcpy(pMgmt->abyCurrBSSID, pMgmt->abyMACAddr, WLAN_ADDR_LEN);
2472                 DBG_PRT(MSG_LEVEL_INFO, KERN_INFO"AP beacon created BSSID:"
2473                         "%pM\n", pMgmt->abyCurrBSSID);
2474     }
2475
2476     if (pMgmt->eCurrMode == WMAC_MODE_IBSS_STA) {
2477
2478         // BSSID selected must be randomized as spec 11.1.3
2479         pMgmt->abyCurrBSSID[5] = (BYTE) (LODWORD(qwCurrTSF)& 0x000000ff);
2480         pMgmt->abyCurrBSSID[4] = (BYTE)((LODWORD(qwCurrTSF)& 0x0000ff00) >> 8);
2481         pMgmt->abyCurrBSSID[3] = (BYTE)((LODWORD(qwCurrTSF)& 0x00ff0000) >> 16);
2482         pMgmt->abyCurrBSSID[2] = (BYTE)((LODWORD(qwCurrTSF)& 0x00000ff0) >> 4);
2483         pMgmt->abyCurrBSSID[1] = (BYTE)((LODWORD(qwCurrTSF)& 0x000ff000) >> 12);
2484         pMgmt->abyCurrBSSID[0] = (BYTE)((LODWORD(qwCurrTSF)& 0x0ff00000) >> 20);
2485         pMgmt->abyCurrBSSID[5] ^= pMgmt->abyMACAddr[0];
2486         pMgmt->abyCurrBSSID[4] ^= pMgmt->abyMACAddr[1];
2487         pMgmt->abyCurrBSSID[3] ^= pMgmt->abyMACAddr[2];
2488         pMgmt->abyCurrBSSID[2] ^= pMgmt->abyMACAddr[3];
2489         pMgmt->abyCurrBSSID[1] ^= pMgmt->abyMACAddr[4];
2490         pMgmt->abyCurrBSSID[0] ^= pMgmt->abyMACAddr[5];
2491         pMgmt->abyCurrBSSID[0] &= ~IEEE_ADDR_GROUP;
2492         pMgmt->abyCurrBSSID[0] |= IEEE_ADDR_UNIVERSAL;
2493
2494
2495                 DBG_PRT(MSG_LEVEL_INFO, KERN_INFO"Adhoc beacon created bssid:"
2496                         "%pM\n", pMgmt->abyCurrBSSID);
2497     }
2498
2499     // set BSSID filter
2500     MACvWriteBSSIDAddress(pDevice, pMgmt->abyCurrBSSID);
2501     memcpy(pDevice->abyBSSID, pMgmt->abyCurrBSSID, WLAN_ADDR_LEN);
2502
2503     MACvRegBitsOn(pDevice, MAC_REG_RCR, RCR_BSSID);
2504     pDevice->byRxMode |= RCR_BSSID;
2505     pMgmt->bCurrBSSIDFilterOn = TRUE;
2506
2507     // Set Capability Info
2508     pMgmt->wCurrCapInfo = 0;
2509
2510     if (pMgmt->eCurrMode == WMAC_MODE_ESS_AP) {
2511         pMgmt->wCurrCapInfo |= WLAN_SET_CAP_INFO_ESS(1);
2512         pMgmt->byDTIMPeriod = DEFAULT_DTIM_PERIOD;
2513         pMgmt->byDTIMCount = pMgmt->byDTIMPeriod - 1;
2514         pDevice->eOPMode = OP_MODE_AP;
2515     }
2516
2517     if (pMgmt->eCurrMode == WMAC_MODE_IBSS_STA) {
2518         pMgmt->wCurrCapInfo |= WLAN_SET_CAP_INFO_IBSS(1);
2519         pDevice->eOPMode = OP_MODE_ADHOC;
2520     }
2521
2522     if (pDevice->bEncryptionEnable) {
2523         pMgmt->wCurrCapInfo |= WLAN_SET_CAP_INFO_PRIVACY(1);
2524         if (pMgmt->eAuthenMode == WMAC_AUTH_WPANONE) {
2525             if (pDevice->eEncryptionStatus == Ndis802_11Encryption3Enabled) {
2526                 pMgmt->byCSSPK = KEY_CTL_CCMP;
2527                 pMgmt->byCSSGK = KEY_CTL_CCMP;
2528             } else if (pDevice->eEncryptionStatus == Ndis802_11Encryption2Enabled) {
2529                 pMgmt->byCSSPK = KEY_CTL_TKIP;
2530                 pMgmt->byCSSGK = KEY_CTL_TKIP;
2531             } else {
2532                 pMgmt->byCSSPK = KEY_CTL_NONE;
2533                 pMgmt->byCSSGK = KEY_CTL_WEP;
2534             }
2535         } else {
2536             pMgmt->byCSSPK = KEY_CTL_WEP;
2537             pMgmt->byCSSGK = KEY_CTL_WEP;
2538         }
2539     }
2540
2541     pMgmt->byERPContext = 0;
2542
2543     if (pDevice->byPreambleType == 1) {
2544         pMgmt->wCurrCapInfo |= WLAN_SET_CAP_INFO_SHORTPREAMBLE(1);
2545     } else {
2546         pMgmt->wCurrCapInfo &= (~WLAN_SET_CAP_INFO_SHORTPREAMBLE(1));
2547     }
2548
2549     pMgmt->eCurrState = WMAC_STATE_STARTED;
2550     // Prepare beacon to send
2551     if (bMgrPrepareBeaconToSend((void *) pDevice, pMgmt))
2552         *pStatus = CMD_STATUS_SUCCESS;
2553
2554     return;
2555 }
2556
2557 /*+
2558  *
2559  * Routine Description:
2560  *   Instructs wmac to join a bss using the supplied attributes.
2561  *   The arguments may the BSSID or SSID and the rest of the
2562  *   attributes are obtained from the scan result of known bss list.
2563  *
2564  *
2565  * Return Value:
2566  *    None.
2567  *
2568 -*/
2569
2570 void vMgrJoinBSSBegin(void *hDeviceContext, PCMD_STATUS pStatus)
2571 {
2572     PSDevice     pDevice = (PSDevice)hDeviceContext;
2573     PSMgmtObject    pMgmt = &(pDevice->sMgmtObj);
2574     PKnownBSS       pCurr = NULL;
2575     unsigned int            ii, uu;
2576     PWLAN_IE_SUPP_RATES pItemRates = NULL;
2577     PWLAN_IE_SUPP_RATES pItemExtRates = NULL;
2578     PWLAN_IE_SSID   pItemSSID;
2579     unsigned int            uRateLen = WLAN_RATES_MAXLEN;
2580     WORD            wMaxBasicRate = RATE_1M;
2581     WORD            wMaxSuppRate = RATE_1M;
2582     WORD            wSuppRate;
2583     BYTE            byTopCCKBasicRate = RATE_1M;
2584     BYTE            byTopOFDMBasicRate = RATE_1M;
2585     BOOL            bShortSlotTime = FALSE;
2586
2587
2588     for (ii = 0; ii < MAX_BSS_NUM; ii++) {
2589         if (pMgmt->sBSSList[ii].bActive == TRUE)
2590             break;
2591     }
2592
2593     if (ii == MAX_BSS_NUM) {
2594        *pStatus = CMD_STATUS_RESOURCES;
2595         DBG_PRT(MSG_LEVEL_NOTICE, KERN_INFO "BSS finding:BSS list is empty.\n");
2596        return;
2597     }
2598
2599     // memset(pMgmt->abyDesireBSSID, 0,  WLAN_BSSID_LEN);
2600     // Search known BSS list for prefer BSSID or SSID
2601
2602     pCurr = BSSpSearchBSSList(pDevice,
2603                               pMgmt->abyDesireBSSID,
2604                               pMgmt->abyDesireSSID,
2605                               pDevice->eConfigPHYMode
2606                               );
2607
2608     if (pCurr == NULL){
2609        *pStatus = CMD_STATUS_RESOURCES;
2610        pItemSSID = (PWLAN_IE_SSID)pMgmt->abyDesireSSID;
2611        DBG_PRT(MSG_LEVEL_NOTICE, KERN_INFO "Scanning [%s] not found, disconnected !\n", pItemSSID->abySSID);
2612        return;
2613     }
2614
2615     DBG_PRT(MSG_LEVEL_NOTICE, KERN_INFO "AP(BSS) finding:Found a AP(BSS)..\n");
2616
2617     if (WLAN_GET_CAP_INFO_ESS(cpu_to_le16(pCurr->wCapInfo))){
2618
2619         if ((pMgmt->eAuthenMode == WMAC_AUTH_WPA) ||
2620             (pMgmt->eAuthenMode == WMAC_AUTH_WPAPSK)) {
2621                 /*
2622             if (pDevice->eEncryptionStatus == Ndis802_11Encryption2Enabled) {
2623                 if (WPA_SearchRSN(0, WPA_TKIP, pCurr) == FALSE) {
2624                     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"No match RSN info. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
2625                     // encryption mode error
2626                     pMgmt->eCurrState = WMAC_STATE_IDLE;
2627                     return;
2628                 }
2629             } else if (pDevice->eEncryptionStatus == Ndis802_11Encryption3Enabled) {
2630                 if (WPA_SearchRSN(0, WPA_AESCCMP, pCurr) == FALSE) {
2631                     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"No match RSN info. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
2632                     // encryption mode error
2633                     pMgmt->eCurrState = WMAC_STATE_IDLE;
2634                     return;
2635                 }
2636             }
2637 */
2638         }
2639
2640         //if(pDevice->bWPASuppWextEnabled == TRUE)
2641             Encyption_Rebuild(pDevice, pCurr);
2642
2643         // Infrastructure BSS
2644         s_vMgrSynchBSS(pDevice,
2645                        WMAC_MODE_ESS_STA,
2646                        pCurr,
2647                        pStatus
2648                        );
2649
2650         if (*pStatus == CMD_STATUS_SUCCESS){
2651
2652             // Adopt this BSS state vars in Mgmt Object
2653             pMgmt->uCurrChannel = pCurr->uChannel;
2654
2655             memset(pMgmt->abyCurrSuppRates, 0 , WLAN_IEHDR_LEN + WLAN_RATES_MAXLEN + 1);
2656             memset(pMgmt->abyCurrExtSuppRates, 0 , WLAN_IEHDR_LEN + WLAN_RATES_MAXLEN + 1);
2657
2658             if (pCurr->eNetworkTypeInUse == PHY_TYPE_11B) {
2659                 uRateLen = WLAN_RATES_MAXLEN_11B;
2660             }
2661
2662             pItemRates = (PWLAN_IE_SUPP_RATES)pMgmt->abyCurrSuppRates;
2663             pItemExtRates = (PWLAN_IE_SUPP_RATES)pMgmt->abyCurrExtSuppRates;
2664
2665             // Parse Support Rate IE
2666             pItemRates->byElementID = WLAN_EID_SUPP_RATES;
2667             pItemRates->len = RATEuSetIE((PWLAN_IE_SUPP_RATES)pCurr->abySuppRates,
2668                                          pItemRates,
2669                                          uRateLen);
2670
2671             // Parse Extension Support Rate IE
2672             pItemExtRates->byElementID = WLAN_EID_EXTSUPP_RATES;
2673             pItemExtRates->len = RATEuSetIE((PWLAN_IE_SUPP_RATES)pCurr->abyExtSuppRates,
2674                                             pItemExtRates,
2675                                             uRateLen);
2676             // Stuffing Rate IE
2677             if ((pItemExtRates->len > 0) && (pItemRates->len < 8)) {
2678                 for (ii = 0; ii < (unsigned int) (8 - pItemRates->len); ) {
2679                         pItemRates->abyRates[pItemRates->len + ii] =
2680                                 pItemExtRates->abyRates[ii];
2681                         ii++;
2682                     if (pItemExtRates->len <= ii)
2683                         break;
2684                 }
2685                 pItemRates->len += (BYTE)ii;
2686                 if (pItemExtRates->len - ii > 0) {
2687                     pItemExtRates->len -= (BYTE)ii;
2688                     for (uu = 0; uu < pItemExtRates->len; uu ++) {
2689                         pItemExtRates->abyRates[uu] = pItemExtRates->abyRates[uu + ii];
2690                     }
2691                 } else {
2692                     pItemExtRates->len = 0;
2693                 }
2694             }
2695
2696             RATEvParseMaxRate((void *)pDevice, pItemRates, pItemExtRates, TRUE,
2697                               &wMaxBasicRate, &wMaxSuppRate, &wSuppRate,
2698                               &byTopCCKBasicRate, &byTopOFDMBasicRate);
2699             vUpdateIFS(pDevice);
2700             // TODO: deal with if wCapInfo the privacy is on, but station WEP is off
2701             // TODO: deal with if wCapInfo the PS-Pollable is on.
2702             pMgmt->wCurrBeaconPeriod = pCurr->wBeaconInterval;
2703             memset(pMgmt->abyCurrSSID, 0, WLAN_IEHDR_LEN + WLAN_SSID_MAXLEN + 1);
2704             memcpy(pMgmt->abyCurrBSSID, pCurr->abyBSSID, WLAN_BSSID_LEN);
2705             memcpy(pMgmt->abyCurrSSID, pCurr->abySSID, WLAN_IEHDR_LEN + WLAN_SSID_MAXLEN + 1);
2706
2707             pMgmt->eCurrMode = WMAC_MODE_ESS_STA;
2708
2709             pMgmt->eCurrState = WMAC_STATE_JOINTED;
2710             // Adopt BSS state in Adapter Device Object
2711             pDevice->eOPMode = OP_MODE_INFRASTRUCTURE;
2712             memcpy(pDevice->abyBSSID, pCurr->abyBSSID, WLAN_BSSID_LEN);
2713
2714             // Add current BSS to Candidate list
2715             // This should only work for WPA2 BSS, and WPA2 BSS check must be done before.
2716             if (pMgmt->eAuthenMode == WMAC_AUTH_WPA2) {
2717                 BOOL bResult = bAdd_PMKID_Candidate((void *) pDevice,
2718                                                     pMgmt->abyCurrBSSID,
2719                                                     &pCurr->sRSNCapObj);
2720                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"bAdd_PMKID_Candidate: 1(%d)\n", bResult);
2721                 if (bResult == FALSE) {
2722                         vFlush_PMKID_Candidate((void *) pDevice);
2723                         DBG_PRT(MSG_LEVEL_DEBUG,
2724                                 KERN_INFO "vFlush_PMKID_Candidate: 4\n");
2725                         bAdd_PMKID_Candidate((void *) pDevice,
2726                                              pMgmt->abyCurrBSSID,
2727                                              &pCurr->sRSNCapObj);
2728                 }
2729             }
2730
2731             // Preamble type auto-switch: if AP can receive short-preamble cap,
2732             // we can turn on too.
2733             if (WLAN_GET_CAP_INFO_SHORTPREAMBLE(pCurr->wCapInfo)) {
2734                 pDevice->byPreambleType = pDevice->byShortPreamble;
2735             }
2736             else {
2737                 pDevice->byPreambleType = 0;
2738             }
2739             // Change PreambleType must set RSPINF again
2740             CARDvSetRSPINF(pDevice, (BYTE)pDevice->byBBType);
2741
2742             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Join ESS\n");
2743
2744             if (pCurr->eNetworkTypeInUse == PHY_TYPE_11G) {
2745
2746                 if ((pCurr->sERP.byERP & WLAN_EID_ERP_USE_PROTECTION) != pDevice->bProtectMode) {//0000 0010
2747                     pDevice->bProtectMode = (pCurr->sERP.byERP & WLAN_EID_ERP_USE_PROTECTION);
2748                     if (pDevice->bProtectMode) {
2749                         MACvEnableProtectMD(pDevice);
2750                     } else {
2751                         MACvDisableProtectMD(pDevice);
2752                     }
2753                     vUpdateIFS(pDevice);
2754                 }
2755                 if ((pCurr->sERP.byERP & WLAN_EID_ERP_NONERP_PRESENT) != pDevice->bNonERPPresent) {//0000 0001
2756                     pDevice->bNonERPPresent = (pCurr->sERP.byERP & WLAN_EID_ERP_USE_PROTECTION);
2757                 }
2758                 if ((pCurr->sERP.byERP & WLAN_EID_ERP_BARKER_MODE) != pDevice->bBarkerPreambleMd) {//0000 0100
2759                     pDevice->bBarkerPreambleMd = (pCurr->sERP.byERP & WLAN_EID_ERP_BARKER_MODE);
2760                     //BarkerPreambleMd has higher priority than shortPreamble bit in Cap
2761                     if (pDevice->bBarkerPreambleMd) {
2762                         MACvEnableBarkerPreambleMd(pDevice);
2763                     } else {
2764                         MACvDisableBarkerPreambleMd(pDevice);
2765                     }
2766                 }
2767             }
2768             //DBG_PRN_WLAN05(("wCapInfo: %X\n", pCurr->wCapInfo));
2769             if (WLAN_GET_CAP_INFO_SHORTSLOTTIME(pCurr->wCapInfo) != pDevice->bShortSlotTime) {
2770                 if (pDevice->byBBType == BB_TYPE_11A) {
2771                     bShortSlotTime = TRUE;
2772                 }
2773                 else if (pDevice->byBBType == BB_TYPE_11B) {
2774                     bShortSlotTime = FALSE;
2775                 }
2776                 else {
2777                     bShortSlotTime = WLAN_GET_CAP_INFO_SHORTSLOTTIME(pCurr->wCapInfo);
2778                 }
2779                 //DBG_PRN_WLAN05(("Set Short Slot Time: %d\n", pDevice->bShortSlotTime));
2780                 if (bShortSlotTime != pDevice->bShortSlotTime) {
2781                     pDevice->bShortSlotTime = bShortSlotTime;
2782                     BBvSetShortSlotTime(pDevice);
2783                     vUpdateIFS(pDevice);
2784                 }
2785             }
2786
2787             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"End of Join AP -- A/B/G Action\n");
2788         }
2789         else {
2790             pMgmt->eCurrState = WMAC_STATE_IDLE;
2791         };
2792
2793
2794      }
2795      else {
2796         // ad-hoc mode BSS
2797         if (pMgmt->eAuthenMode == WMAC_AUTH_WPANONE) {
2798
2799             if (pDevice->eEncryptionStatus == Ndis802_11Encryption2Enabled) {
2800 /*
2801                 if (WPA_SearchRSN(0, WPA_TKIP, pCurr) == FALSE) {
2802                     // encryption mode error
2803                     pMgmt->eCurrState = WMAC_STATE_IDLE;
2804                     return;
2805                 }
2806 */
2807             } else if (pDevice->eEncryptionStatus == Ndis802_11Encryption3Enabled) {
2808 /*
2809                 if (WPA_SearchRSN(0, WPA_AESCCMP, pCurr) == FALSE) {
2810                     // encryption mode error
2811                     pMgmt->eCurrState = WMAC_STATE_IDLE;
2812                     return;
2813                 }
2814 */
2815             } else {
2816                 // encryption mode error
2817                 pMgmt->eCurrState = WMAC_STATE_IDLE;
2818                 return;
2819             }
2820         }
2821
2822         s_vMgrSynchBSS(pDevice,
2823                        WMAC_MODE_IBSS_STA,
2824                        pCurr,
2825                        pStatus
2826                        );
2827
2828         if (*pStatus == CMD_STATUS_SUCCESS){
2829             // Adopt this BSS state vars in Mgmt Object
2830             // TODO: check if CapInfo privacy on, but we don't..
2831             pMgmt->uCurrChannel = pCurr->uChannel;
2832
2833
2834             // Parse Support Rate IE
2835             pMgmt->abyCurrSuppRates[0] = WLAN_EID_SUPP_RATES;
2836             pMgmt->abyCurrSuppRates[1] = RATEuSetIE((PWLAN_IE_SUPP_RATES)pCurr->abySuppRates,
2837                                                     (PWLAN_IE_SUPP_RATES)pMgmt->abyCurrSuppRates,
2838                                                     WLAN_RATES_MAXLEN_11B);
2839             // set basic rate
2840             RATEvParseMaxRate((void *)pDevice,
2841                               (PWLAN_IE_SUPP_RATES)pMgmt->abyCurrSuppRates,
2842                               NULL, TRUE, &wMaxBasicRate, &wMaxSuppRate, &wSuppRate,
2843                               &byTopCCKBasicRate, &byTopOFDMBasicRate);
2844             vUpdateIFS(pDevice);
2845             pMgmt->wCurrCapInfo = pCurr->wCapInfo;
2846             pMgmt->wCurrBeaconPeriod = pCurr->wBeaconInterval;
2847             memset(pMgmt->abyCurrSSID, 0, WLAN_IEHDR_LEN + WLAN_SSID_MAXLEN);
2848             memcpy(pMgmt->abyCurrBSSID, pCurr->abyBSSID, WLAN_BSSID_LEN);
2849             memcpy(pMgmt->abyCurrSSID, pCurr->abySSID, WLAN_IEHDR_LEN + WLAN_SSID_MAXLEN);
2850 //          pMgmt->wCurrATIMWindow = pCurr->wATIMWindow;
2851             pMgmt->eCurrMode = WMAC_MODE_IBSS_STA;
2852             pMgmt->eCurrState = WMAC_STATE_STARTED;
2853             // Adopt BSS state in Adapter Device Object
2854             pDevice->eOPMode = OP_MODE_ADHOC;
2855             pDevice->bLinkPass = TRUE;
2856             ControlvMaskByte(pDevice,MESSAGE_REQUEST_MACREG,MAC_REG_PAPEDELAY,LEDSTS_STS,LEDSTS_INTER);
2857             memcpy(pDevice->abyBSSID, pCurr->abyBSSID, WLAN_BSSID_LEN);
2858
2859                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Join IBSS ok:%pM\n",
2860                         pMgmt->abyCurrBSSID);
2861             // Preamble type auto-switch: if AP can receive short-preamble cap,
2862             // and if registry setting is short preamble we can turn on too.
2863
2864             if (WLAN_GET_CAP_INFO_SHORTPREAMBLE(pCurr->wCapInfo)) {
2865                 pDevice->byPreambleType = pDevice->byShortPreamble;
2866             }
2867             else {
2868                 pDevice->byPreambleType = 0;
2869             }
2870             // Change PreambleType must set RSPINF again
2871             CARDvSetRSPINF(pDevice, (BYTE)pDevice->byBBType);
2872
2873             // Prepare beacon
2874                 bMgrPrepareBeaconToSend((void *) pDevice, pMgmt);
2875         }
2876         else {
2877             pMgmt->eCurrState = WMAC_STATE_IDLE;
2878         };
2879      };
2880     return;
2881 }
2882
2883
2884
2885 /*+
2886  *
2887  * Routine Description:
2888  * Set HW to synchronize a specific BSS from known BSS list.
2889  *
2890  *
2891  * Return Value:
2892  *    PCM_STATUS
2893  *
2894 -*/
2895 static
2896 void
2897 s_vMgrSynchBSS (
2898      PSDevice      pDevice,
2899      unsigned int          uBSSMode,
2900      PKnownBSS     pCurr,
2901      PCMD_STATUS  pStatus
2902     )
2903 {
2904     PSMgmtObject  pMgmt = &(pDevice->sMgmtObj);
2905                                                      //1M,   2M,   5M,   11M,  18M,  24M,  36M,  54M
2906     BYTE abyCurrSuppRatesG[] = {WLAN_EID_SUPP_RATES, 8, 0x02, 0x04, 0x0B, 0x16, 0x24, 0x30, 0x48, 0x6C};
2907     BYTE abyCurrExtSuppRatesG[] = {WLAN_EID_EXTSUPP_RATES, 4, 0x0C, 0x12, 0x18, 0x60};
2908                                                            //6M,   9M,   12M,  48M
2909     BYTE abyCurrSuppRatesA[] = {WLAN_EID_SUPP_RATES, 8, 0x0C, 0x12, 0x18, 0x24, 0x30, 0x48, 0x60, 0x6C};
2910     BYTE abyCurrSuppRatesB[] = {WLAN_EID_SUPP_RATES, 4, 0x02, 0x04, 0x0B, 0x16};
2911
2912
2913     *pStatus = CMD_STATUS_FAILURE;
2914
2915     if (s_bCipherMatch(pCurr,
2916                        pDevice->eEncryptionStatus,
2917                        &(pMgmt->byCSSPK),
2918                        &(pMgmt->byCSSGK)) == FALSE) {
2919         DBG_PRT(MSG_LEVEL_NOTICE, KERN_INFO "s_bCipherMatch Fail .......\n");
2920         return;
2921     }
2922
2923     pMgmt->pCurrBSS = pCurr;
2924
2925     // if previous mode is IBSS.
2926     if(pMgmt->eCurrMode == WMAC_MODE_IBSS_STA) {
2927         MACvRegBitsOff(pDevice, MAC_REG_TCR, TCR_AUTOBCNTX);
2928     }
2929
2930     // Init the BSS informations
2931     pDevice->bCCK = TRUE;
2932     pDevice->bProtectMode = FALSE;
2933     MACvDisableProtectMD(pDevice);
2934     pDevice->bBarkerPreambleMd = FALSE;
2935     MACvDisableBarkerPreambleMd(pDevice);
2936     pDevice->bNonERPPresent = FALSE;
2937     pDevice->byPreambleType = 0;
2938     pDevice->wBasicRate = 0;
2939     // Set Basic Rate
2940     CARDbAddBasicRate((void *)pDevice, RATE_1M);
2941
2942     // calculate TSF offset
2943     // TSF Offset = Received Timestamp TSF - Marked Local's TSF
2944     CARDvAdjustTSF(pDevice, pCurr->byRxRate, pCurr->qwBSSTimestamp, pCurr->qwLocalTSF);
2945
2946     // set HW beacon interval
2947     MACvWriteBeaconInterval(pDevice, pCurr->wBeaconInterval);
2948
2949     // set Next TBTT
2950     // Next TBTT = ((local_current_TSF / beacon_interval) + 1 ) * beacon_interval
2951     CARDvSetFirstNextTBTT(pDevice, pCurr->wBeaconInterval);
2952
2953     // set BSSID
2954     MACvWriteBSSIDAddress(pDevice, pCurr->abyBSSID);
2955
2956     memcpy(pMgmt->abyCurrBSSID, pCurr->abyBSSID, 6);
2957
2958         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Sync:set CurrBSSID address = "
2959                 "%pM\n", pMgmt->abyCurrBSSID);
2960
2961     if (pCurr->eNetworkTypeInUse == PHY_TYPE_11A) {
2962         if ((pDevice->eConfigPHYMode == PHY_TYPE_11A) ||
2963             (pDevice->eConfigPHYMode == PHY_TYPE_AUTO)) {
2964             pDevice->byBBType = BB_TYPE_11A;
2965             pMgmt->eCurrentPHYMode = PHY_TYPE_11A;
2966             pDevice->bShortSlotTime = TRUE;
2967             BBvSetShortSlotTime(pDevice);
2968             CARDvSetBSSMode(pDevice);
2969         } else {
2970             return;
2971         }
2972     } else if (pCurr->eNetworkTypeInUse == PHY_TYPE_11B) {
2973         if ((pDevice->eConfigPHYMode == PHY_TYPE_11B) ||
2974             (pDevice->eConfigPHYMode == PHY_TYPE_11G) ||
2975             (pDevice->eConfigPHYMode == PHY_TYPE_AUTO)) {
2976             pDevice->byBBType = BB_TYPE_11B;
2977             pMgmt->eCurrentPHYMode = PHY_TYPE_11B;
2978             pDevice->bShortSlotTime = FALSE;
2979             BBvSetShortSlotTime(pDevice);
2980             CARDvSetBSSMode(pDevice);
2981         } else {
2982             return;
2983         }
2984     } else {
2985         if ((pDevice->eConfigPHYMode == PHY_TYPE_11G) ||
2986             (pDevice->eConfigPHYMode == PHY_TYPE_AUTO)) {
2987             pDevice->byBBType = BB_TYPE_11G;
2988             pMgmt->eCurrentPHYMode = PHY_TYPE_11G;
2989             pDevice->bShortSlotTime = TRUE;
2990             BBvSetShortSlotTime(pDevice);
2991             CARDvSetBSSMode(pDevice);
2992         } else if (pDevice->eConfigPHYMode == PHY_TYPE_11B) {
2993             pDevice->byBBType = BB_TYPE_11B;
2994             pDevice->bShortSlotTime = FALSE;
2995             BBvSetShortSlotTime(pDevice);
2996             CARDvSetBSSMode(pDevice);
2997         } else {
2998             return;
2999         }
3000     }
3001
3002     if (uBSSMode == WMAC_MODE_ESS_STA) {
3003         MACvRegBitsOff(pDevice, MAC_REG_HOSTCR, HOSTCR_ADHOC);
3004         MACvRegBitsOn(pDevice, MAC_REG_RCR, RCR_BSSID);
3005         pDevice->byRxMode |= RCR_BSSID;
3006         pMgmt->bCurrBSSIDFilterOn = TRUE;
3007     }
3008
3009     // set channel and clear NAV
3010     CARDbSetMediaChannel(pDevice, pCurr->uChannel);
3011     pMgmt->uCurrChannel = pCurr->uChannel;
3012     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "<----s_bSynchBSS Set Channel [%d]\n", pCurr->uChannel);
3013
3014     if ((pDevice->bUpdateBBVGA) &&
3015         (pDevice->byBBVGACurrent != pDevice->abyBBVGA[0])) {
3016         pDevice->byBBVGACurrent = pDevice->abyBBVGA[0];
3017         BBvSetVGAGainOffset(pDevice, pDevice->byBBVGACurrent);
3018         BBvSetShortSlotTime(pDevice);
3019     }
3020     //
3021     // Notes:
3022     // 1. In Ad-hoc mode : check if received others beacon as jointed indication,
3023     //    otherwise we will start own IBSS.
3024     // 2. In Infra mode : Supposed we already synchronized with AP right now.
3025
3026     if (uBSSMode == WMAC_MODE_IBSS_STA) {
3027         MACvRegBitsOn(pDevice, MAC_REG_HOSTCR, HOSTCR_ADHOC);
3028         MACvRegBitsOn(pDevice, MAC_REG_RCR, RCR_BSSID);
3029         pDevice->byRxMode |= RCR_BSSID;
3030         pMgmt->bCurrBSSIDFilterOn = TRUE;
3031     }
3032
3033     if (pDevice->byBBType == BB_TYPE_11A) {
3034         memcpy(pMgmt->abyCurrSuppRates, &abyCurrSuppRatesA[0], sizeof(abyCurrSuppRatesA));
3035         pMgmt->abyCurrExtSuppRates[1] = 0;
3036     } else if (pDevice->byBBType == BB_TYPE_11B) {
3037         memcpy(pMgmt->abyCurrSuppRates, &abyCurrSuppRatesB[0], sizeof(abyCurrSuppRatesB));
3038         pMgmt->abyCurrExtSuppRates[1] = 0;
3039     } else {
3040         memcpy(pMgmt->abyCurrSuppRates, &abyCurrSuppRatesG[0], sizeof(abyCurrSuppRatesG));
3041         memcpy(pMgmt->abyCurrExtSuppRates, &abyCurrExtSuppRatesG[0], sizeof(abyCurrExtSuppRatesG));
3042     }
3043     pMgmt->byERPContext = pCurr->sERP.byERP;
3044
3045     *pStatus = CMD_STATUS_SUCCESS;
3046
3047     return;
3048 };
3049
3050
3051 //mike add: fix NetworkManager 0.7.0 hidden ssid mode in WPA encryption
3052 //                   ,need reset eAuthenMode and eEncryptionStatus
3053  static void  Encyption_Rebuild(
3054      PSDevice pDevice,
3055      PKnownBSS pCurr
3056  )
3057  {
3058   PSMgmtObject  pMgmt = &(pDevice->sMgmtObj);
3059   /* unsigned int ii, uSameBssidNum=0; */
3060
3061   //   if( uSameBssidNum>=2) {   //we only check AP in hidden sssid  mode
3062         if ((pMgmt->eAuthenMode == WMAC_AUTH_WPAPSK) ||           //networkmanager 0.7.0 does not give the pairwise-key selsection,
3063              (pMgmt->eAuthenMode == WMAC_AUTH_WPA2PSK)) {         // so we need re-selsect it according to real pairwise-key info.
3064                if(pCurr->bWPAValid == TRUE)  {   //WPA-PSK
3065                           pMgmt->eAuthenMode = WMAC_AUTH_WPAPSK;
3066                     if(pCurr->abyPKType[0] == WPA_TKIP) {
3067                         pDevice->eEncryptionStatus = Ndis802_11Encryption2Enabled;    //TKIP
3068                         PRINT_K("Encyption_Rebuild--->ssid reset config to [WPAPSK-TKIP]\n");
3069                       }
3070                    else if(pCurr->abyPKType[0] == WPA_AESCCMP) {
3071                         pDevice->eEncryptionStatus = Ndis802_11Encryption3Enabled;    //AES
3072                           PRINT_K("Encyption_Rebuild--->ssid reset config to [WPAPSK-AES]\n");
3073                      }
3074                 }
3075                else if(pCurr->bWPA2Valid == TRUE) {  //WPA2-PSK
3076                          pMgmt->eAuthenMode = WMAC_AUTH_WPA2PSK;
3077                        if(pCurr->abyCSSPK[0] == WLAN_11i_CSS_TKIP) {
3078                            pDevice->eEncryptionStatus = Ndis802_11Encryption2Enabled;     //TKIP
3079                              PRINT_K("Encyption_Rebuild--->ssid reset config to [WPA2PSK-TKIP]\n");
3080                         }
3081                        else if(pCurr->abyCSSPK[0] == WLAN_11i_CSS_CCMP) {
3082                            pDevice->eEncryptionStatus = Ndis802_11Encryption3Enabled;    //AES
3083                             PRINT_K("Encyption_Rebuild--->ssid reset config to [WPA2PSK-AES]\n");
3084                         }
3085                 }
3086               }
3087         //  }
3088       return;
3089  }
3090
3091
3092 /*+
3093  *
3094  * Routine Description:
3095  *  Format TIM field
3096  *
3097  *
3098  * Return Value:
3099  *    void
3100  *
3101 -*/
3102
3103 static
3104 void
3105 s_vMgrFormatTIM(
3106      PSMgmtObject pMgmt,
3107      PWLAN_IE_TIM pTIM
3108     )
3109 {
3110     BYTE        byMask[8] = {1, 2, 4, 8, 0x10, 0x20, 0x40, 0x80};
3111     BYTE        byMap;
3112     unsigned int        ii, jj;
3113     BOOL        bStartFound = FALSE;
3114     BOOL        bMulticast = FALSE;
3115     WORD        wStartIndex = 0;
3116     WORD        wEndIndex = 0;
3117
3118
3119     // Find size of partial virtual bitmap
3120     for (ii = 0; ii < (MAX_NODE_NUM + 1); ii++) {
3121         byMap = pMgmt->abyPSTxMap[ii];
3122         if (!ii) {
3123             // Mask out the broadcast bit which is indicated separately.
3124             bMulticast = (byMap & byMask[0]) != 0;
3125             if(bMulticast) {
3126                pMgmt->sNodeDBTable[0].bRxPSPoll = TRUE;
3127             }
3128             byMap = 0;
3129         }
3130         if (byMap) {
3131             if (!bStartFound) {
3132                 bStartFound = TRUE;
3133                 wStartIndex = (WORD)ii;
3134             }
3135             wEndIndex = (WORD)ii;
3136         }
3137     }
3138
3139
3140     // Round start index down to nearest even number
3141     wStartIndex &=  ~BIT0;
3142
3143     // Round end index up to nearest even number
3144     wEndIndex = ((wEndIndex + 1) & ~BIT0);
3145
3146     // Size of element payload
3147
3148     pTIM->len =  3 + (wEndIndex - wStartIndex) + 1;
3149
3150     // Fill in the Fixed parts of the TIM
3151     pTIM->byDTIMCount = pMgmt->byDTIMCount;
3152     pTIM->byDTIMPeriod = pMgmt->byDTIMPeriod;
3153     pTIM->byBitMapCtl = (bMulticast ? TIM_MULTICAST_MASK : 0) |
3154         (((wStartIndex >> 1) << 1) & TIM_BITMAPOFFSET_MASK);
3155
3156     // Append variable part of TIM
3157
3158     for (ii = wStartIndex, jj =0 ; ii <= wEndIndex; ii++, jj++) {
3159          pTIM->byVirtBitMap[jj] = pMgmt->abyPSTxMap[ii];
3160     }
3161
3162     // Aid = 0 don't used.
3163     pTIM->byVirtBitMap[0]  &= ~BIT0;
3164 }
3165
3166
3167 /*+
3168  *
3169  * Routine Description:
3170  *  Constructs an Beacon frame( Ad-hoc mode)
3171  *
3172  *
3173  * Return Value:
3174  *    PTR to frame; or NULL on allocation failure
3175  *
3176 -*/
3177
3178 static
3179 PSTxMgmtPacket
3180 s_MgrMakeBeacon(
3181      PSDevice pDevice,
3182      PSMgmtObject pMgmt,
3183      WORD wCurrCapInfo,
3184      WORD wCurrBeaconPeriod,
3185      unsigned int uCurrChannel,
3186      WORD wCurrATIMWinodw,
3187      PWLAN_IE_SSID pCurrSSID,
3188      PBYTE pCurrBSSID,
3189      PWLAN_IE_SUPP_RATES pCurrSuppRates,
3190      PWLAN_IE_SUPP_RATES pCurrExtSuppRates
3191     )
3192 {
3193     PSTxMgmtPacket      pTxPacket = NULL;
3194     WLAN_FR_BEACON      sFrame;
3195     BYTE                abyBroadcastAddr[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
3196
3197
3198     // prepare beacon frame
3199     pTxPacket = (PSTxMgmtPacket)pMgmt->pbyMgmtPacketPool;
3200     memset(pTxPacket, 0, sizeof(STxMgmtPacket) + WLAN_BEACON_FR_MAXLEN);
3201     pTxPacket->p80211Header = (PUWLAN_80211HDR)((PBYTE)pTxPacket + sizeof(STxMgmtPacket));
3202     // Setup the sFrame structure.
3203     sFrame.pBuf = (PBYTE)pTxPacket->p80211Header;
3204     sFrame.len = WLAN_BEACON_FR_MAXLEN;
3205     vMgrEncodeBeacon(&sFrame);
3206     // Setup the header
3207     sFrame.pHdr->sA3.wFrameCtl = cpu_to_le16(
3208         (
3209         WLAN_SET_FC_FTYPE(WLAN_TYPE_MGR) |
3210         WLAN_SET_FC_FSTYPE(WLAN_FSTYPE_BEACON)
3211         ));
3212
3213     if (pDevice->bEnablePSMode) {
3214         sFrame.pHdr->sA3.wFrameCtl |= cpu_to_le16((WORD)WLAN_SET_FC_PWRMGT(1));
3215     }
3216
3217     memcpy( sFrame.pHdr->sA3.abyAddr1, abyBroadcastAddr, WLAN_ADDR_LEN);
3218     memcpy( sFrame.pHdr->sA3.abyAddr2, pMgmt->abyMACAddr, WLAN_ADDR_LEN);
3219     memcpy( sFrame.pHdr->sA3.abyAddr3, pCurrBSSID, WLAN_BSSID_LEN);
3220     *sFrame.pwBeaconInterval = cpu_to_le16(wCurrBeaconPeriod);
3221     *sFrame.pwCapInfo = cpu_to_le16(wCurrCapInfo);
3222     // Copy SSID
3223     sFrame.pSSID = (PWLAN_IE_SSID)(sFrame.pBuf + sFrame.len);
3224     sFrame.len += ((PWLAN_IE_SSID)pMgmt->abyCurrSSID)->len + WLAN_IEHDR_LEN;
3225     memcpy(sFrame.pSSID,
3226              pCurrSSID,
3227              ((PWLAN_IE_SSID)pCurrSSID)->len + WLAN_IEHDR_LEN
3228             );
3229     // Copy the rate set
3230     sFrame.pSuppRates = (PWLAN_IE_SUPP_RATES)(sFrame.pBuf + sFrame.len);
3231     sFrame.len += ((PWLAN_IE_SUPP_RATES)pCurrSuppRates)->len + WLAN_IEHDR_LEN;
3232     memcpy(sFrame.pSuppRates,
3233            pCurrSuppRates,
3234            ((PWLAN_IE_SUPP_RATES)pCurrSuppRates)->len + WLAN_IEHDR_LEN
3235           );
3236     // DS parameter
3237     if (pDevice->byBBType != BB_TYPE_11A) {
3238         sFrame.pDSParms = (PWLAN_IE_DS_PARMS)(sFrame.pBuf + sFrame.len);
3239         sFrame.len += (1) + WLAN_IEHDR_LEN;
3240         sFrame.pDSParms->byElementID = WLAN_EID_DS_PARMS;
3241         sFrame.pDSParms->len = 1;
3242         sFrame.pDSParms->byCurrChannel = (BYTE)uCurrChannel;
3243     }
3244     // TIM field
3245     if (pMgmt->eCurrMode == WMAC_MODE_ESS_AP) {
3246         sFrame.pTIM = (PWLAN_IE_TIM)(sFrame.pBuf + sFrame.len);
3247         sFrame.pTIM->byElementID = WLAN_EID_TIM;
3248         s_vMgrFormatTIM(pMgmt, sFrame.pTIM);
3249         sFrame.len += (WLAN_IEHDR_LEN + sFrame.pTIM->len);
3250     }
3251
3252     if (pMgmt->eCurrMode == WMAC_MODE_IBSS_STA) {
3253
3254         // IBSS parameter
3255         sFrame.pIBSSParms = (PWLAN_IE_IBSS_PARMS)(sFrame.pBuf + sFrame.len);
3256         sFrame.len += (2) + WLAN_IEHDR_LEN;
3257         sFrame.pIBSSParms->byElementID = WLAN_EID_IBSS_PARMS;
3258         sFrame.pIBSSParms->len = 2;
3259         sFrame.pIBSSParms->wATIMWindow = wCurrATIMWinodw;
3260         if (pMgmt->eAuthenMode == WMAC_AUTH_WPANONE) {
3261             /* RSN parameter */
3262             sFrame.pRSNWPA = (PWLAN_IE_RSN_EXT)(sFrame.pBuf + sFrame.len);
3263             sFrame.pRSNWPA->byElementID = WLAN_EID_RSN_WPA;
3264             sFrame.pRSNWPA->len = 12;
3265             sFrame.pRSNWPA->abyOUI[0] = 0x00;
3266             sFrame.pRSNWPA->abyOUI[1] = 0x50;
3267             sFrame.pRSNWPA->abyOUI[2] = 0xf2;
3268             sFrame.pRSNWPA->abyOUI[3] = 0x01;
3269             sFrame.pRSNWPA->wVersion = 1;
3270             sFrame.pRSNWPA->abyMulticast[0] = 0x00;
3271             sFrame.pRSNWPA->abyMulticast[1] = 0x50;
3272             sFrame.pRSNWPA->abyMulticast[2] = 0xf2;
3273             if (pDevice->eEncryptionStatus == Ndis802_11Encryption3Enabled)
3274                 sFrame.pRSNWPA->abyMulticast[3] = 0x04;//AES
3275             else if (pDevice->eEncryptionStatus == Ndis802_11Encryption2Enabled)
3276                 sFrame.pRSNWPA->abyMulticast[3] = 0x02;//TKIP
3277             else if (pDevice->eEncryptionStatus == Ndis802_11Encryption1Enabled)
3278                 sFrame.pRSNWPA->abyMulticast[3] = 0x01;//WEP40
3279             else
3280                 sFrame.pRSNWPA->abyMulticast[3] = 0x00;//NONE
3281
3282             // Pairwise Key Cipher Suite
3283             sFrame.pRSNWPA->wPKCount = 0;
3284             // Auth Key Management Suite
3285             *((PWORD)(sFrame.pBuf + sFrame.len + sFrame.pRSNWPA->len))=0;
3286             sFrame.pRSNWPA->len +=2;
3287
3288             // RSN Capabilites
3289             *((PWORD)(sFrame.pBuf + sFrame.len + sFrame.pRSNWPA->len))=0;
3290             sFrame.pRSNWPA->len +=2;
3291             sFrame.len += sFrame.pRSNWPA->len + WLAN_IEHDR_LEN;
3292         }
3293     }
3294
3295
3296     if (pMgmt->eCurrentPHYMode == PHY_TYPE_11G) {
3297         sFrame.pERP = (PWLAN_IE_ERP)(sFrame.pBuf + sFrame.len);
3298         sFrame.len += 1 + WLAN_IEHDR_LEN;
3299         sFrame.pERP->byElementID = WLAN_EID_ERP;
3300         sFrame.pERP->len = 1;
3301         sFrame.pERP->byContext = 0;
3302         if (pDevice->bProtectMode == TRUE)
3303             sFrame.pERP->byContext |= WLAN_EID_ERP_USE_PROTECTION;
3304         if (pDevice->bNonERPPresent == TRUE)
3305             sFrame.pERP->byContext |= WLAN_EID_ERP_NONERP_PRESENT;
3306         if (pDevice->bBarkerPreambleMd == TRUE)
3307             sFrame.pERP->byContext |= WLAN_EID_ERP_BARKER_MODE;
3308     }
3309     if (((PWLAN_IE_SUPP_RATES)pCurrExtSuppRates)->len != 0) {
3310         sFrame.pExtSuppRates = (PWLAN_IE_SUPP_RATES)(sFrame.pBuf + sFrame.len);
3311         sFrame.len += ((PWLAN_IE_SUPP_RATES)pCurrExtSuppRates)->len + WLAN_IEHDR_LEN;
3312         memcpy(sFrame.pExtSuppRates,
3313              pCurrExtSuppRates,
3314              ((PWLAN_IE_SUPP_RATES)pCurrExtSuppRates)->len + WLAN_IEHDR_LEN
3315              );
3316     }
3317     // hostapd wpa/wpa2 IE
3318     if ((pMgmt->eCurrMode == WMAC_MODE_ESS_AP) && (pDevice->bEnableHostapd == TRUE)) {
3319          if (pMgmt->eAuthenMode == WMAC_AUTH_WPANONE) {
3320              if (pMgmt->wWPAIELen != 0) {
3321                  sFrame.pRSN = (PWLAN_IE_RSN)(sFrame.pBuf + sFrame.len);
3322                  memcpy(sFrame.pRSN, pMgmt->abyWPAIE, pMgmt->wWPAIELen);
3323                  sFrame.len += pMgmt->wWPAIELen;
3324              }
3325          }
3326     }
3327
3328     /* Adjust the length fields */
3329     pTxPacket->cbMPDULen = sFrame.len;
3330     pTxPacket->cbPayloadLen = sFrame.len - WLAN_HDR_ADDR3_LEN;
3331
3332     return pTxPacket;
3333 }
3334
3335
3336
3337
3338
3339 /*+
3340  *
3341  * Routine Description:
3342  *  Constructs an Prob-response frame
3343  *
3344  *
3345  * Return Value:
3346  *    PTR to frame; or NULL on allocation failure
3347  *
3348 -*/
3349
3350
3351
3352
3353 PSTxMgmtPacket
3354 s_MgrMakeProbeResponse(
3355      PSDevice pDevice,
3356      PSMgmtObject pMgmt,
3357      WORD wCurrCapInfo,
3358      WORD wCurrBeaconPeriod,
3359      unsigned int uCurrChannel,
3360      WORD wCurrATIMWinodw,
3361      PBYTE pDstAddr,
3362      PWLAN_IE_SSID pCurrSSID,
3363      PBYTE pCurrBSSID,
3364      PWLAN_IE_SUPP_RATES pCurrSuppRates,
3365      PWLAN_IE_SUPP_RATES pCurrExtSuppRates,
3366      BYTE byPHYType
3367     )
3368 {
3369     PSTxMgmtPacket      pTxPacket = NULL;
3370     WLAN_FR_PROBERESP   sFrame;
3371
3372
3373
3374     pTxPacket = (PSTxMgmtPacket)pMgmt->pbyMgmtPacketPool;
3375     memset(pTxPacket, 0, sizeof(STxMgmtPacket) + WLAN_PROBERESP_FR_MAXLEN);
3376     pTxPacket->p80211Header = (PUWLAN_80211HDR)((PBYTE)pTxPacket + sizeof(STxMgmtPacket));
3377     // Setup the sFrame structure.
3378     sFrame.pBuf = (PBYTE)pTxPacket->p80211Header;
3379     sFrame.len = WLAN_PROBERESP_FR_MAXLEN;
3380     vMgrEncodeProbeResponse(&sFrame);
3381     // Setup the header
3382     sFrame.pHdr->sA3.wFrameCtl = cpu_to_le16(
3383         (
3384         WLAN_SET_FC_FTYPE(WLAN_TYPE_MGR) |
3385         WLAN_SET_FC_FSTYPE(WLAN_FSTYPE_PROBERESP)
3386         ));
3387     memcpy( sFrame.pHdr->sA3.abyAddr1, pDstAddr, WLAN_ADDR_LEN);
3388     memcpy( sFrame.pHdr->sA3.abyAddr2, pMgmt->abyMACAddr, WLAN_ADDR_LEN);
3389     memcpy( sFrame.pHdr->sA3.abyAddr3, pCurrBSSID, WLAN_BSSID_LEN);
3390     *sFrame.pwBeaconInterval = cpu_to_le16(wCurrBeaconPeriod);
3391     *sFrame.pwCapInfo = cpu_to_le16(wCurrCapInfo);
3392
3393     if (byPHYType == BB_TYPE_11B) {
3394         *sFrame.pwCapInfo &= cpu_to_le16((WORD)~(WLAN_SET_CAP_INFO_SHORTSLOTTIME(1)));
3395     }
3396
3397     // Copy SSID
3398     sFrame.pSSID = (PWLAN_IE_SSID)(sFrame.pBuf + sFrame.len);
3399     sFrame.len += ((PWLAN_IE_SSID)pMgmt->abyCurrSSID)->len + WLAN_IEHDR_LEN;
3400     memcpy(sFrame.pSSID,
3401            pCurrSSID,
3402            ((PWLAN_IE_SSID)pCurrSSID)->len + WLAN_IEHDR_LEN
3403            );
3404     // Copy the rate set
3405     sFrame.pSuppRates = (PWLAN_IE_SUPP_RATES)(sFrame.pBuf + sFrame.len);
3406
3407     sFrame.len += ((PWLAN_IE_SUPP_RATES)pCurrSuppRates)->len + WLAN_IEHDR_LEN;
3408     memcpy(sFrame.pSuppRates,
3409            pCurrSuppRates,
3410            ((PWLAN_IE_SUPP_RATES)pCurrSuppRates)->len + WLAN_IEHDR_LEN
3411           );
3412
3413     // DS parameter
3414     if (pDevice->byBBType != BB_TYPE_11A) {
3415         sFrame.pDSParms = (PWLAN_IE_DS_PARMS)(sFrame.pBuf + sFrame.len);
3416         sFrame.len += (1) + WLAN_IEHDR_LEN;
3417         sFrame.pDSParms->byElementID = WLAN_EID_DS_PARMS;
3418         sFrame.pDSParms->len = 1;
3419         sFrame.pDSParms->byCurrChannel = (BYTE)uCurrChannel;
3420     }
3421
3422     if (pMgmt->eCurrMode != WMAC_MODE_ESS_AP) {
3423         // IBSS parameter
3424         sFrame.pIBSSParms = (PWLAN_IE_IBSS_PARMS)(sFrame.pBuf + sFrame.len);
3425         sFrame.len += (2) + WLAN_IEHDR_LEN;
3426         sFrame.pIBSSParms->byElementID = WLAN_EID_IBSS_PARMS;
3427         sFrame.pIBSSParms->len = 2;
3428         sFrame.pIBSSParms->wATIMWindow = 0;
3429     }
3430     if (pDevice->byBBType == BB_TYPE_11G) {
3431         sFrame.pERP = (PWLAN_IE_ERP)(sFrame.pBuf + sFrame.len);
3432         sFrame.len += 1 + WLAN_IEHDR_LEN;
3433         sFrame.pERP->byElementID = WLAN_EID_ERP;
3434         sFrame.pERP->len = 1;
3435         sFrame.pERP->byContext = 0;
3436         if (pDevice->bProtectMode == TRUE)
3437             sFrame.pERP->byContext |= WLAN_EID_ERP_USE_PROTECTION;
3438         if (pDevice->bNonERPPresent == TRUE)
3439             sFrame.pERP->byContext |= WLAN_EID_ERP_NONERP_PRESENT;
3440         if (pDevice->bBarkerPreambleMd == TRUE)
3441             sFrame.pERP->byContext |= WLAN_EID_ERP_BARKER_MODE;
3442     }
3443
3444     if (((PWLAN_IE_SUPP_RATES)pCurrExtSuppRates)->len != 0) {
3445         sFrame.pExtSuppRates = (PWLAN_IE_SUPP_RATES)(sFrame.pBuf + sFrame.len);
3446         sFrame.len += ((PWLAN_IE_SUPP_RATES)pCurrExtSuppRates)->len + WLAN_IEHDR_LEN;
3447         memcpy(sFrame.pExtSuppRates,
3448              pCurrExtSuppRates,
3449              ((PWLAN_IE_SUPP_RATES)pCurrExtSuppRates)->len + WLAN_IEHDR_LEN
3450              );
3451     }
3452
3453     // hostapd wpa/wpa2 IE
3454     if ((pMgmt->eCurrMode == WMAC_MODE_ESS_AP) && (pDevice->bEnableHostapd == TRUE)) {
3455          if (pMgmt->eAuthenMode == WMAC_AUTH_WPANONE) {
3456              if (pMgmt->wWPAIELen != 0) {
3457                  sFrame.pRSN = (PWLAN_IE_RSN)(sFrame.pBuf + sFrame.len);
3458                  memcpy(sFrame.pRSN, pMgmt->abyWPAIE, pMgmt->wWPAIELen);
3459                  sFrame.len += pMgmt->wWPAIELen;
3460              }
3461          }
3462     }
3463
3464     // Adjust the length fields
3465     pTxPacket->cbMPDULen = sFrame.len;
3466     pTxPacket->cbPayloadLen = sFrame.len - WLAN_HDR_ADDR3_LEN;
3467
3468     return pTxPacket;
3469 }
3470
3471
3472
3473 /*+
3474  *
3475  * Routine Description:
3476  *  Constructs an association request frame
3477  *
3478  *
3479  * Return Value:
3480  *    A ptr to frame or NULL on allocation failue
3481  *
3482 -*/
3483
3484
3485 PSTxMgmtPacket
3486 s_MgrMakeAssocRequest(
3487      PSDevice pDevice,
3488      PSMgmtObject pMgmt,
3489      PBYTE pDAddr,
3490      WORD wCurrCapInfo,
3491      WORD wListenInterval,
3492      PWLAN_IE_SSID pCurrSSID,
3493      PWLAN_IE_SUPP_RATES pCurrRates,
3494      PWLAN_IE_SUPP_RATES pCurrExtSuppRates
3495     )
3496 {
3497     PSTxMgmtPacket      pTxPacket = NULL;
3498     WLAN_FR_ASSOCREQ    sFrame;
3499     PBYTE               pbyIEs;
3500     PBYTE               pbyRSN;
3501
3502
3503     pTxPacket = (PSTxMgmtPacket)pMgmt->pbyMgmtPacketPool;
3504     memset(pTxPacket, 0, sizeof(STxMgmtPacket) + WLAN_ASSOCREQ_FR_MAXLEN);
3505     pTxPacket->p80211Header = (PUWLAN_80211HDR)((PBYTE)pTxPacket + sizeof(STxMgmtPacket));
3506     // Setup the sFrame structure.
3507     sFrame.pBuf = (PBYTE)pTxPacket->p80211Header;
3508     sFrame.len = WLAN_ASSOCREQ_FR_MAXLEN;
3509     // format fixed field frame structure
3510     vMgrEncodeAssocRequest(&sFrame);
3511     // Setup the header
3512     sFrame.pHdr->sA3.wFrameCtl = cpu_to_le16(
3513         (
3514         WLAN_SET_FC_FTYPE(WLAN_TYPE_MGR) |
3515         WLAN_SET_FC_FSTYPE(WLAN_FSTYPE_ASSOCREQ)
3516         ));
3517     memcpy( sFrame.pHdr->sA3.abyAddr1, pDAddr, WLAN_ADDR_LEN);
3518     memcpy( sFrame.pHdr->sA3.abyAddr2, pMgmt->abyMACAddr, WLAN_ADDR_LEN);
3519     memcpy( sFrame.pHdr->sA3.abyAddr3, pMgmt->abyCurrBSSID, WLAN_BSSID_LEN);
3520
3521     // Set the capability and listen interval
3522     *(sFrame.pwCapInfo) = cpu_to_le16(wCurrCapInfo);
3523     *(sFrame.pwListenInterval) = cpu_to_le16(wListenInterval);
3524
3525     // sFrame.len point to end of fixed field
3526     sFrame.pSSID = (PWLAN_IE_SSID)(sFrame.pBuf + sFrame.len);
3527     sFrame.len += pCurrSSID->len + WLAN_IEHDR_LEN;
3528     memcpy(sFrame.pSSID, pCurrSSID, pCurrSSID->len + WLAN_IEHDR_LEN);
3529
3530     pMgmt->sAssocInfo.AssocInfo.RequestIELength = pCurrSSID->len + WLAN_IEHDR_LEN;
3531     pMgmt->sAssocInfo.AssocInfo.OffsetRequestIEs = sizeof(NDIS_802_11_ASSOCIATION_INFORMATION);
3532     pbyIEs = pMgmt->sAssocInfo.abyIEs;
3533     memcpy(pbyIEs, pCurrSSID, pCurrSSID->len + WLAN_IEHDR_LEN);
3534     pbyIEs += pCurrSSID->len + WLAN_IEHDR_LEN;
3535
3536     // Copy the rate set
3537     sFrame.pSuppRates = (PWLAN_IE_SUPP_RATES)(sFrame.pBuf + sFrame.len);
3538     if ((pDevice->byBBType == BB_TYPE_11B) && (pCurrRates->len > 4))
3539         sFrame.len += 4 + WLAN_IEHDR_LEN;
3540     else
3541         sFrame.len += pCurrRates->len + WLAN_IEHDR_LEN;
3542     memcpy(sFrame.pSuppRates, pCurrRates, pCurrRates->len + WLAN_IEHDR_LEN);
3543
3544     // Copy the extension rate set
3545     if ((pDevice->byBBType == BB_TYPE_11G) && (pCurrExtSuppRates->len > 0)) {
3546         sFrame.pExtSuppRates = (PWLAN_IE_SUPP_RATES)(sFrame.pBuf + sFrame.len);
3547         sFrame.len += pCurrExtSuppRates->len + WLAN_IEHDR_LEN;
3548         memcpy(sFrame.pExtSuppRates, pCurrExtSuppRates, pCurrExtSuppRates->len + WLAN_IEHDR_LEN);
3549     }
3550
3551     pMgmt->sAssocInfo.AssocInfo.RequestIELength += pCurrRates->len + WLAN_IEHDR_LEN;
3552     memcpy(pbyIEs, pCurrRates, pCurrRates->len + WLAN_IEHDR_LEN);
3553     pbyIEs += pCurrRates->len + WLAN_IEHDR_LEN;
3554
3555
3556     if (((pMgmt->eAuthenMode == WMAC_AUTH_WPA) ||
3557          (pMgmt->eAuthenMode == WMAC_AUTH_WPAPSK) ||
3558          (pMgmt->eAuthenMode == WMAC_AUTH_WPANONE)) &&
3559         (pMgmt->pCurrBSS != NULL)) {
3560         /* WPA IE */
3561         sFrame.pRSNWPA = (PWLAN_IE_RSN_EXT)(sFrame.pBuf + sFrame.len);
3562         sFrame.pRSNWPA->byElementID = WLAN_EID_RSN_WPA;
3563         sFrame.pRSNWPA->len = 16;
3564         sFrame.pRSNWPA->abyOUI[0] = 0x00;
3565         sFrame.pRSNWPA->abyOUI[1] = 0x50;
3566         sFrame.pRSNWPA->abyOUI[2] = 0xf2;
3567         sFrame.pRSNWPA->abyOUI[3] = 0x01;
3568         sFrame.pRSNWPA->wVersion = 1;
3569         //Group Key Cipher Suite
3570         sFrame.pRSNWPA->abyMulticast[0] = 0x00;
3571         sFrame.pRSNWPA->abyMulticast[1] = 0x50;
3572         sFrame.pRSNWPA->abyMulticast[2] = 0xf2;
3573         if (pMgmt->byCSSGK == KEY_CTL_WEP) {
3574             sFrame.pRSNWPA->abyMulticast[3] = pMgmt->pCurrBSS->byGKType;
3575         } else if (pMgmt->byCSSGK == KEY_CTL_TKIP) {
3576             sFrame.pRSNWPA->abyMulticast[3] = WPA_TKIP;
3577         } else if (pMgmt->byCSSGK == KEY_CTL_CCMP) {
3578             sFrame.pRSNWPA->abyMulticast[3] = WPA_AESCCMP;
3579         } else {
3580             sFrame.pRSNWPA->abyMulticast[3] = WPA_NONE;
3581         }
3582         // Pairwise Key Cipher Suite
3583         sFrame.pRSNWPA->wPKCount = 1;
3584         sFrame.pRSNWPA->PKSList[0].abyOUI[0] = 0x00;
3585         sFrame.pRSNWPA->PKSList[0].abyOUI[1] = 0x50;
3586         sFrame.pRSNWPA->PKSList[0].abyOUI[2] = 0xf2;
3587         if (pMgmt->byCSSPK == KEY_CTL_TKIP) {
3588             sFrame.pRSNWPA->PKSList[0].abyOUI[3] = WPA_TKIP;
3589         } else if (pMgmt->byCSSPK == KEY_CTL_CCMP) {
3590             sFrame.pRSNWPA->PKSList[0].abyOUI[3] = WPA_AESCCMP;
3591         } else {
3592             sFrame.pRSNWPA->PKSList[0].abyOUI[3] = WPA_NONE;
3593         }
3594         // Auth Key Management Suite
3595         pbyRSN = (PBYTE)(sFrame.pBuf + sFrame.len + 2 + sFrame.pRSNWPA->len);
3596         *pbyRSN++=0x01;
3597         *pbyRSN++=0x00;
3598         *pbyRSN++=0x00;
3599
3600         *pbyRSN++=0x50;
3601         *pbyRSN++=0xf2;
3602         if (pMgmt->eAuthenMode == WMAC_AUTH_WPAPSK) {
3603             *pbyRSN++=WPA_AUTH_PSK;
3604         }
3605         else if (pMgmt->eAuthenMode == WMAC_AUTH_WPA) {
3606             *pbyRSN++=WPA_AUTH_IEEE802_1X;
3607         }
3608         else {
3609             *pbyRSN++=WPA_NONE;
3610         }
3611
3612         sFrame.pRSNWPA->len +=6;
3613
3614         // RSN Capabilites
3615
3616         *pbyRSN++=0x00;
3617         *pbyRSN++=0x00;
3618         sFrame.pRSNWPA->len +=2;
3619
3620         sFrame.len += sFrame.pRSNWPA->len + WLAN_IEHDR_LEN;
3621         // copy to AssocInfo. for OID_802_11_ASSOCIATION_INFORMATION
3622         pMgmt->sAssocInfo.AssocInfo.RequestIELength += sFrame.pRSNWPA->len + WLAN_IEHDR_LEN;
3623         memcpy(pbyIEs, sFrame.pRSNWPA, sFrame.pRSNWPA->len + WLAN_IEHDR_LEN);
3624         pbyIEs += sFrame.pRSNWPA->len + WLAN_IEHDR_LEN;
3625
3626     } else if (((pMgmt->eAuthenMode == WMAC_AUTH_WPA2) ||
3627                 (pMgmt->eAuthenMode == WMAC_AUTH_WPA2PSK)) &&
3628                (pMgmt->pCurrBSS != NULL)) {
3629         unsigned int ii;
3630         PWORD               pwPMKID;
3631
3632         // WPA IE
3633         sFrame.pRSN = (PWLAN_IE_RSN)(sFrame.pBuf + sFrame.len);
3634         sFrame.pRSN->byElementID = WLAN_EID_RSN;
3635         sFrame.pRSN->len = 6; //Version(2)+GK(4)
3636         sFrame.pRSN->wVersion = 1;
3637         //Group Key Cipher Suite
3638         sFrame.pRSN->abyRSN[0] = 0x00;
3639         sFrame.pRSN->abyRSN[1] = 0x0F;
3640         sFrame.pRSN->abyRSN[2] = 0xAC;
3641         if (pMgmt->byCSSGK == KEY_CTL_WEP) {
3642             sFrame.pRSN->abyRSN[3] = pMgmt->pCurrBSS->byCSSGK;
3643         } else if (pMgmt->byCSSGK == KEY_CTL_TKIP) {
3644             sFrame.pRSN->abyRSN[3] = WLAN_11i_CSS_TKIP;
3645         } else if (pMgmt->byCSSGK == KEY_CTL_CCMP) {
3646             sFrame.pRSN->abyRSN[3] = WLAN_11i_CSS_CCMP;
3647         } else {
3648             sFrame.pRSN->abyRSN[3] = WLAN_11i_CSS_UNKNOWN;
3649         }
3650
3651         // Pairwise Key Cipher Suite
3652         sFrame.pRSN->abyRSN[4] = 1;
3653         sFrame.pRSN->abyRSN[5] = 0;
3654         sFrame.pRSN->abyRSN[6] = 0x00;
3655         sFrame.pRSN->abyRSN[7] = 0x0F;
3656         sFrame.pRSN->abyRSN[8] = 0xAC;
3657         if (pMgmt->byCSSPK == KEY_CTL_TKIP) {
3658             sFrame.pRSN->abyRSN[9] = WLAN_11i_CSS_TKIP;
3659         } else if (pMgmt->byCSSPK == KEY_CTL_CCMP) {
3660             sFrame.pRSN->abyRSN[9] = WLAN_11i_CSS_CCMP;
3661         } else if (pMgmt->byCSSPK == KEY_CTL_NONE) {
3662             sFrame.pRSN->abyRSN[9] = WLAN_11i_CSS_USE_GROUP;
3663         } else {
3664             sFrame.pRSN->abyRSN[9] = WLAN_11i_CSS_UNKNOWN;
3665         }
3666         sFrame.pRSN->len += 6;
3667
3668         // Auth Key Management Suite
3669         sFrame.pRSN->abyRSN[10] = 1;
3670         sFrame.pRSN->abyRSN[11] = 0;
3671         sFrame.pRSN->abyRSN[12] = 0x00;
3672         sFrame.pRSN->abyRSN[13] = 0x0F;
3673         sFrame.pRSN->abyRSN[14] = 0xAC;
3674         if (pMgmt->eAuthenMode == WMAC_AUTH_WPA2PSK) {
3675             sFrame.pRSN->abyRSN[15] = WLAN_11i_AKMSS_PSK;
3676         } else if (pMgmt->eAuthenMode == WMAC_AUTH_WPA2) {
3677             sFrame.pRSN->abyRSN[15] = WLAN_11i_AKMSS_802_1X;
3678         } else {
3679             sFrame.pRSN->abyRSN[15] = WLAN_11i_AKMSS_UNKNOWN;
3680         }
3681         sFrame.pRSN->len +=6;
3682
3683         // RSN Capabilites
3684         if (pMgmt->pCurrBSS->sRSNCapObj.bRSNCapExist == TRUE) {
3685             memcpy(&sFrame.pRSN->abyRSN[16], &pMgmt->pCurrBSS->sRSNCapObj.wRSNCap, 2);
3686         } else {
3687             sFrame.pRSN->abyRSN[16] = 0;
3688             sFrame.pRSN->abyRSN[17] = 0;
3689         }
3690         sFrame.pRSN->len +=2;
3691
3692         if ((pDevice->gsPMKID.BSSIDInfoCount > 0) && (pDevice->bRoaming == TRUE) && (pMgmt->eAuthenMode == WMAC_AUTH_WPA2)) {
3693             // RSN PMKID
3694             pbyRSN = &sFrame.pRSN->abyRSN[18];
3695             pwPMKID = (PWORD)pbyRSN; // Point to PMKID count
3696             *pwPMKID = 0;            // Initialize PMKID count
3697             pbyRSN += 2;             // Point to PMKID list
3698         for (ii = 0; ii < pDevice->gsPMKID.BSSIDInfoCount; ii++) {
3699                 if (!memcmp(&pDevice->gsPMKID.BSSIDInfo[ii].BSSID[0],
3700                              pMgmt->abyCurrBSSID,
3701                              ETH_ALEN)) {
3702                         (*pwPMKID)++;
3703                         memcpy(pbyRSN,
3704                                pDevice->gsPMKID.BSSIDInfo[ii].PMKID,
3705                                16);
3706                         pbyRSN += 16;
3707                 }
3708         }
3709             if (*pwPMKID != 0) {
3710                 sFrame.pRSN->len += (2 + (*pwPMKID)*16);
3711             }
3712         }
3713
3714         sFrame.len += sFrame.pRSN->len + WLAN_IEHDR_LEN;
3715         // copy to AssocInfo. for OID_802_11_ASSOCIATION_INFORMATION
3716         pMgmt->sAssocInfo.AssocInfo.RequestIELength += sFrame.pRSN->len + WLAN_IEHDR_LEN;
3717         memcpy(pbyIEs, sFrame.pRSN, sFrame.pRSN->len + WLAN_IEHDR_LEN);
3718         pbyIEs += sFrame.pRSN->len + WLAN_IEHDR_LEN;
3719     }
3720
3721
3722     // Adjust the length fields
3723     pTxPacket->cbMPDULen = sFrame.len;
3724     pTxPacket->cbPayloadLen = sFrame.len - WLAN_HDR_ADDR3_LEN;
3725     return pTxPacket;
3726 }
3727
3728
3729
3730
3731
3732
3733
3734
3735 /*+
3736  *
3737  * Routine Description:
3738  *  Constructs an re-association request frame
3739  *
3740  *
3741  * Return Value:
3742  *    A ptr to frame or NULL on allocation failure
3743  *
3744 -*/
3745
3746
3747 PSTxMgmtPacket
3748 s_MgrMakeReAssocRequest(
3749      PSDevice pDevice,
3750      PSMgmtObject pMgmt,
3751      PBYTE pDAddr,
3752      WORD wCurrCapInfo,
3753      WORD wListenInterval,
3754      PWLAN_IE_SSID pCurrSSID,
3755      PWLAN_IE_SUPP_RATES pCurrRates,
3756      PWLAN_IE_SUPP_RATES pCurrExtSuppRates
3757     )
3758 {
3759     PSTxMgmtPacket      pTxPacket = NULL;
3760     WLAN_FR_REASSOCREQ  sFrame;
3761     PBYTE               pbyIEs;
3762     PBYTE               pbyRSN;
3763
3764
3765     pTxPacket = (PSTxMgmtPacket)pMgmt->pbyMgmtPacketPool;
3766     memset( pTxPacket, 0, sizeof(STxMgmtPacket) + WLAN_REASSOCREQ_FR_MAXLEN);
3767     pTxPacket->p80211Header = (PUWLAN_80211HDR)((PBYTE)pTxPacket + sizeof(STxMgmtPacket));
3768     /* Setup the sFrame structure. */
3769     sFrame.pBuf = (PBYTE)pTxPacket->p80211Header;
3770     sFrame.len = WLAN_REASSOCREQ_FR_MAXLEN;
3771
3772     // format fixed field frame structure
3773     vMgrEncodeReassocRequest(&sFrame);
3774
3775     /* Setup the header */
3776     sFrame.pHdr->sA3.wFrameCtl = cpu_to_le16(
3777         (
3778         WLAN_SET_FC_FTYPE(WLAN_TYPE_MGR) |
3779         WLAN_SET_FC_FSTYPE(WLAN_FSTYPE_REASSOCREQ)
3780         ));
3781     memcpy( sFrame.pHdr->sA3.abyAddr1, pDAddr, WLAN_ADDR_LEN);
3782     memcpy( sFrame.pHdr->sA3.abyAddr2, pMgmt->abyMACAddr, WLAN_ADDR_LEN);
3783     memcpy( sFrame.pHdr->sA3.abyAddr3, pMgmt->abyCurrBSSID, WLAN_BSSID_LEN);
3784
3785     /* Set the capability and listen interval */
3786     *(sFrame.pwCapInfo) = cpu_to_le16(wCurrCapInfo);
3787     *(sFrame.pwListenInterval) = cpu_to_le16(wListenInterval);
3788
3789     memcpy(sFrame.pAddrCurrAP, pMgmt->abyCurrBSSID, WLAN_BSSID_LEN);
3790     /* Copy the SSID */
3791     /* sFrame.len point to end of fixed field */
3792     sFrame.pSSID = (PWLAN_IE_SSID)(sFrame.pBuf + sFrame.len);
3793     sFrame.len += pCurrSSID->len + WLAN_IEHDR_LEN;
3794     memcpy(sFrame.pSSID, pCurrSSID, pCurrSSID->len + WLAN_IEHDR_LEN);
3795
3796     pMgmt->sAssocInfo.AssocInfo.RequestIELength = pCurrSSID->len + WLAN_IEHDR_LEN;
3797     pMgmt->sAssocInfo.AssocInfo.OffsetRequestIEs = sizeof(NDIS_802_11_ASSOCIATION_INFORMATION);
3798     pbyIEs = pMgmt->sAssocInfo.abyIEs;
3799     memcpy(pbyIEs, pCurrSSID, pCurrSSID->len + WLAN_IEHDR_LEN);
3800     pbyIEs += pCurrSSID->len + WLAN_IEHDR_LEN;
3801
3802     /* Copy the rate set */
3803     /* sFrame.len point to end of SSID */
3804     sFrame.pSuppRates = (PWLAN_IE_SUPP_RATES)(sFrame.pBuf + sFrame.len);
3805     sFrame.len += pCurrRates->len + WLAN_IEHDR_LEN;
3806     memcpy(sFrame.pSuppRates, pCurrRates, pCurrRates->len + WLAN_IEHDR_LEN);
3807
3808     // Copy the extension rate set
3809     if ((pMgmt->eCurrentPHYMode == PHY_TYPE_11G) && (pCurrExtSuppRates->len > 0)) {
3810         sFrame.pExtSuppRates = (PWLAN_IE_SUPP_RATES)(sFrame.pBuf + sFrame.len);
3811         sFrame.len += pCurrExtSuppRates->len + WLAN_IEHDR_LEN;
3812         memcpy(sFrame.pExtSuppRates, pCurrExtSuppRates, pCurrExtSuppRates->len + WLAN_IEHDR_LEN);
3813     }
3814
3815     pMgmt->sAssocInfo.AssocInfo.RequestIELength += pCurrRates->len + WLAN_IEHDR_LEN;
3816     memcpy(pbyIEs, pCurrRates, pCurrRates->len + WLAN_IEHDR_LEN);
3817     pbyIEs += pCurrRates->len + WLAN_IEHDR_LEN;
3818
3819     if (((pMgmt->eAuthenMode == WMAC_AUTH_WPA) ||
3820          (pMgmt->eAuthenMode == WMAC_AUTH_WPAPSK) ||
3821          (pMgmt->eAuthenMode == WMAC_AUTH_WPANONE)) &&
3822         (pMgmt->pCurrBSS != NULL)) {
3823         /* WPA IE */
3824         sFrame.pRSNWPA = (PWLAN_IE_RSN_EXT)(sFrame.pBuf + sFrame.len);
3825         sFrame.pRSNWPA->byElementID = WLAN_EID_RSN_WPA;
3826         sFrame.pRSNWPA->len = 16;
3827         sFrame.pRSNWPA->abyOUI[0] = 0x00;
3828         sFrame.pRSNWPA->abyOUI[1] = 0x50;
3829         sFrame.pRSNWPA->abyOUI[2] = 0xf2;
3830         sFrame.pRSNWPA->abyOUI[3] = 0x01;
3831         sFrame.pRSNWPA->wVersion = 1;
3832         //Group Key Cipher Suite
3833         sFrame.pRSNWPA->abyMulticast[0] = 0x00;
3834         sFrame.pRSNWPA->abyMulticast[1] = 0x50;
3835         sFrame.pRSNWPA->abyMulticast[2] = 0xf2;
3836         if (pMgmt->byCSSGK == KEY_CTL_WEP) {
3837             sFrame.pRSNWPA->abyMulticast[3] = pMgmt->pCurrBSS->byGKType;
3838         } else if (pMgmt->byCSSGK == KEY_CTL_TKIP) {
3839             sFrame.pRSNWPA->abyMulticast[3] = WPA_TKIP;
3840         } else if (pMgmt->byCSSGK == KEY_CTL_CCMP) {
3841             sFrame.pRSNWPA->abyMulticast[3] = WPA_AESCCMP;
3842         } else {
3843             sFrame.pRSNWPA->abyMulticast[3] = WPA_NONE;
3844         }
3845         // Pairwise Key Cipher Suite
3846         sFrame.pRSNWPA->wPKCount = 1;
3847         sFrame.pRSNWPA->PKSList[0].abyOUI[0] = 0x00;
3848         sFrame.pRSNWPA->PKSList[0].abyOUI[1] = 0x50;
3849         sFrame.pRSNWPA->PKSList[0].abyOUI[2] = 0xf2;
3850         if (pMgmt->byCSSPK == KEY_CTL_TKIP) {
3851             sFrame.pRSNWPA->PKSList[0].abyOUI[3] = WPA_TKIP;
3852         } else if (pMgmt->byCSSPK == KEY_CTL_CCMP) {
3853             sFrame.pRSNWPA->PKSList[0].abyOUI[3] = WPA_AESCCMP;
3854         } else {
3855             sFrame.pRSNWPA->PKSList[0].abyOUI[3] = WPA_NONE;
3856         }
3857         // Auth Key Management Suite
3858         pbyRSN = (PBYTE)(sFrame.pBuf + sFrame.len + 2 + sFrame.pRSNWPA->len);
3859         *pbyRSN++=0x01;
3860         *pbyRSN++=0x00;
3861         *pbyRSN++=0x00;
3862
3863         *pbyRSN++=0x50;
3864         *pbyRSN++=0xf2;
3865         if (pMgmt->eAuthenMode == WMAC_AUTH_WPAPSK) {
3866             *pbyRSN++=WPA_AUTH_PSK;
3867         } else if (pMgmt->eAuthenMode == WMAC_AUTH_WPA) {
3868             *pbyRSN++=WPA_AUTH_IEEE802_1X;
3869         } else {
3870             *pbyRSN++=WPA_NONE;
3871         }
3872
3873         sFrame.pRSNWPA->len +=6;
3874
3875         // RSN Capabilites
3876         *pbyRSN++=0x00;
3877         *pbyRSN++=0x00;
3878         sFrame.pRSNWPA->len +=2;
3879
3880         sFrame.len += sFrame.pRSNWPA->len + WLAN_IEHDR_LEN;
3881         // copy to AssocInfo. for OID_802_11_ASSOCIATION_INFORMATION
3882         pMgmt->sAssocInfo.AssocInfo.RequestIELength += sFrame.pRSNWPA->len + WLAN_IEHDR_LEN;
3883         memcpy(pbyIEs, sFrame.pRSNWPA, sFrame.pRSNWPA->len + WLAN_IEHDR_LEN);
3884         pbyIEs += sFrame.pRSNWPA->len + WLAN_IEHDR_LEN;
3885
3886     } else if (((pMgmt->eAuthenMode == WMAC_AUTH_WPA2) ||
3887                 (pMgmt->eAuthenMode == WMAC_AUTH_WPA2PSK)) &&
3888                (pMgmt->pCurrBSS != NULL)) {
3889         unsigned int ii;
3890         PWORD               pwPMKID;
3891
3892         /* WPA IE */
3893         sFrame.pRSN = (PWLAN_IE_RSN)(sFrame.pBuf + sFrame.len);
3894         sFrame.pRSN->byElementID = WLAN_EID_RSN;
3895         sFrame.pRSN->len = 6; //Version(2)+GK(4)
3896         sFrame.pRSN->wVersion = 1;
3897         //Group Key Cipher Suite
3898         sFrame.pRSN->abyRSN[0] = 0x00;
3899         sFrame.pRSN->abyRSN[1] = 0x0F;
3900         sFrame.pRSN->abyRSN[2] = 0xAC;
3901         if (pMgmt->byCSSGK == KEY_CTL_WEP) {
3902             sFrame.pRSN->abyRSN[3] = pMgmt->pCurrBSS->byCSSGK;
3903         } else if (pMgmt->byCSSGK == KEY_CTL_TKIP) {
3904             sFrame.pRSN->abyRSN[3] = WLAN_11i_CSS_TKIP;
3905         } else if (pMgmt->byCSSGK == KEY_CTL_CCMP) {
3906             sFrame.pRSN->abyRSN[3] = WLAN_11i_CSS_CCMP;
3907         } else {
3908             sFrame.pRSN->abyRSN[3] = WLAN_11i_CSS_UNKNOWN;
3909         }
3910
3911         // Pairwise Key Cipher Suite
3912         sFrame.pRSN->abyRSN[4] = 1;
3913         sFrame.pRSN->abyRSN[5] = 0;
3914         sFrame.pRSN->abyRSN[6] = 0x00;
3915         sFrame.pRSN->abyRSN[7] = 0x0F;
3916         sFrame.pRSN->abyRSN[8] = 0xAC;
3917         if (pMgmt->byCSSPK == KEY_CTL_TKIP) {
3918             sFrame.pRSN->abyRSN[9] = WLAN_11i_CSS_TKIP;
3919         } else if (pMgmt->byCSSPK == KEY_CTL_CCMP) {
3920             sFrame.pRSN->abyRSN[9] = WLAN_11i_CSS_CCMP;
3921         } else if (pMgmt->byCSSPK == KEY_CTL_NONE) {
3922             sFrame.pRSN->abyRSN[9] = WLAN_11i_CSS_USE_GROUP;
3923         } else {
3924             sFrame.pRSN->abyRSN[9] = WLAN_11i_CSS_UNKNOWN;
3925         }
3926         sFrame.pRSN->len += 6;
3927
3928         // Auth Key Management Suite
3929         sFrame.pRSN->abyRSN[10] = 1;
3930         sFrame.pRSN->abyRSN[11] = 0;
3931         sFrame.pRSN->abyRSN[12] = 0x00;
3932         sFrame.pRSN->abyRSN[13] = 0x0F;
3933         sFrame.pRSN->abyRSN[14] = 0xAC;
3934         if (pMgmt->eAuthenMode == WMAC_AUTH_WPA2PSK) {
3935             sFrame.pRSN->abyRSN[15] = WLAN_11i_AKMSS_PSK;
3936         } else if (pMgmt->eAuthenMode == WMAC_AUTH_WPA2) {
3937             sFrame.pRSN->abyRSN[15] = WLAN_11i_AKMSS_802_1X;
3938         } else {
3939             sFrame.pRSN->abyRSN[15] = WLAN_11i_AKMSS_UNKNOWN;
3940         }
3941         sFrame.pRSN->len +=6;
3942
3943         // RSN Capabilites
3944         if (pMgmt->pCurrBSS->sRSNCapObj.bRSNCapExist == TRUE) {
3945             memcpy(&sFrame.pRSN->abyRSN[16], &pMgmt->pCurrBSS->sRSNCapObj.wRSNCap, 2);
3946         } else {
3947             sFrame.pRSN->abyRSN[16] = 0;
3948             sFrame.pRSN->abyRSN[17] = 0;
3949         }
3950         sFrame.pRSN->len +=2;
3951
3952         if ((pDevice->gsPMKID.BSSIDInfoCount > 0) && (pDevice->bRoaming == TRUE) && (pMgmt->eAuthenMode == WMAC_AUTH_WPA2)) {
3953             // RSN PMKID
3954             pbyRSN = &sFrame.pRSN->abyRSN[18];
3955             pwPMKID = (PWORD)pbyRSN; // Point to PMKID count
3956             *pwPMKID = 0;            // Initialize PMKID count
3957             pbyRSN += 2;             // Point to PMKID list
3958             for (ii = 0; ii < pDevice->gsPMKID.BSSIDInfoCount; ii++) {
3959                 if (!memcmp(&pDevice->gsPMKID.BSSIDInfo[ii].BSSID[0],
3960                             pMgmt->abyCurrBSSID,
3961                             ETH_ALEN)) {
3962                         (*pwPMKID)++;
3963                         memcpy(pbyRSN,
3964                                pDevice->gsPMKID.BSSIDInfo[ii].PMKID,
3965                                16);
3966                         pbyRSN += 16;
3967                 }
3968             }
3969             if (*pwPMKID != 0) {
3970                 sFrame.pRSN->len += (2 + (*pwPMKID)*16);
3971             }
3972         }
3973
3974         sFrame.len += sFrame.pRSN->len + WLAN_IEHDR_LEN;
3975         // copy to AssocInfo. for OID_802_11_ASSOCIATION_INFORMATION
3976         pMgmt->sAssocInfo.AssocInfo.RequestIELength += sFrame.pRSN->len + WLAN_IEHDR_LEN;
3977         memcpy(pbyIEs, sFrame.pRSN, sFrame.pRSN->len + WLAN_IEHDR_LEN);
3978         pbyIEs += sFrame.pRSN->len + WLAN_IEHDR_LEN;
3979     }
3980
3981
3982
3983     /* Adjust the length fields */
3984     pTxPacket->cbMPDULen = sFrame.len;
3985     pTxPacket->cbPayloadLen = sFrame.len - WLAN_HDR_ADDR3_LEN;
3986
3987     return pTxPacket;
3988 }
3989
3990 /*+
3991  *
3992  * Routine Description:
3993  *  Constructs an assoc-response frame
3994  *
3995  *
3996  * Return Value:
3997  *    PTR to frame; or NULL on allocation failure
3998  *
3999 -*/
4000
4001 PSTxMgmtPacket
4002 s_MgrMakeAssocResponse(
4003      PSDevice pDevice,
4004      PSMgmtObject pMgmt,
4005      WORD wCurrCapInfo,
4006      WORD wAssocStatus,
4007      WORD wAssocAID,
4008      PBYTE pDstAddr,
4009      PWLAN_IE_SUPP_RATES pCurrSuppRates,
4010      PWLAN_IE_SUPP_RATES pCurrExtSuppRates
4011     )
4012 {
4013     PSTxMgmtPacket      pTxPacket = NULL;
4014     WLAN_FR_ASSOCRESP   sFrame;
4015
4016
4017     pTxPacket = (PSTxMgmtPacket)pMgmt->pbyMgmtPacketPool;
4018     memset(pTxPacket, 0, sizeof(STxMgmtPacket) + WLAN_ASSOCREQ_FR_MAXLEN);
4019     pTxPacket->p80211Header = (PUWLAN_80211HDR)((PBYTE)pTxPacket + sizeof(STxMgmtPacket));
4020     // Setup the sFrame structure
4021     sFrame.pBuf = (PBYTE)pTxPacket->p80211Header;
4022     sFrame.len = WLAN_REASSOCRESP_FR_MAXLEN;
4023     vMgrEncodeAssocResponse(&sFrame);
4024     // Setup the header
4025     sFrame.pHdr->sA3.wFrameCtl = cpu_to_le16(
4026         (
4027         WLAN_SET_FC_FTYPE(WLAN_TYPE_MGR) |
4028         WLAN_SET_FC_FSTYPE(WLAN_FSTYPE_ASSOCRESP)
4029         ));
4030     memcpy( sFrame.pHdr->sA3.abyAddr1, pDstAddr, WLAN_ADDR_LEN);
4031     memcpy( sFrame.pHdr->sA3.abyAddr2, pMgmt->abyMACAddr, WLAN_ADDR_LEN);
4032     memcpy( sFrame.pHdr->sA3.abyAddr3, pMgmt->abyCurrBSSID, WLAN_BSSID_LEN);
4033
4034     *sFrame.pwCapInfo = cpu_to_le16(wCurrCapInfo);
4035     *sFrame.pwStatus = cpu_to_le16(wAssocStatus);
4036     *sFrame.pwAid = cpu_to_le16((WORD)(wAssocAID | BIT14 | BIT15));
4037
4038     // Copy the rate set
4039     sFrame.pSuppRates = (PWLAN_IE_SUPP_RATES)(sFrame.pBuf + sFrame.len);
4040     sFrame.len += ((PWLAN_IE_SUPP_RATES)pCurrSuppRates)->len + WLAN_IEHDR_LEN;
4041     memcpy(sFrame.pSuppRates,
4042            pCurrSuppRates,
4043            ((PWLAN_IE_SUPP_RATES)pCurrSuppRates)->len + WLAN_IEHDR_LEN
4044           );
4045
4046     if (((PWLAN_IE_SUPP_RATES)pCurrExtSuppRates)->len != 0) {
4047         sFrame.pExtSuppRates = (PWLAN_IE_SUPP_RATES)(sFrame.pBuf + sFrame.len);
4048         sFrame.len += ((PWLAN_IE_SUPP_RATES)pCurrExtSuppRates)->len + WLAN_IEHDR_LEN;
4049         memcpy(sFrame.pExtSuppRates,
4050              pCurrExtSuppRates,
4051              ((PWLAN_IE_SUPP_RATES)pCurrExtSuppRates)->len + WLAN_IEHDR_LEN
4052              );
4053     }
4054
4055     // Adjust the length fields
4056     pTxPacket->cbMPDULen = sFrame.len;
4057     pTxPacket->cbPayloadLen = sFrame.len - WLAN_HDR_ADDR3_LEN;
4058
4059     return pTxPacket;
4060 }
4061
4062
4063 /*+
4064  *
4065  * Routine Description:
4066  *  Constructs an reassoc-response frame
4067  *
4068  *
4069  * Return Value:
4070  *    PTR to frame; or NULL on allocation failure
4071  *
4072 -*/
4073
4074
4075 PSTxMgmtPacket
4076 s_MgrMakeReAssocResponse(
4077      PSDevice pDevice,
4078      PSMgmtObject pMgmt,
4079      WORD wCurrCapInfo,
4080      WORD wAssocStatus,
4081      WORD wAssocAID,
4082      PBYTE pDstAddr,
4083      PWLAN_IE_SUPP_RATES pCurrSuppRates,
4084      PWLAN_IE_SUPP_RATES pCurrExtSuppRates
4085     )
4086 {
4087     PSTxMgmtPacket      pTxPacket = NULL;
4088     WLAN_FR_REASSOCRESP   sFrame;
4089
4090
4091     pTxPacket = (PSTxMgmtPacket)pMgmt->pbyMgmtPacketPool;
4092     memset(pTxPacket, 0, sizeof(STxMgmtPacket) + WLAN_ASSOCREQ_FR_MAXLEN);
4093     pTxPacket->p80211Header = (PUWLAN_80211HDR)((PBYTE)pTxPacket + sizeof(STxMgmtPacket));
4094     // Setup the sFrame structure
4095     sFrame.pBuf = (PBYTE)pTxPacket->p80211Header;
4096     sFrame.len = WLAN_REASSOCRESP_FR_MAXLEN;
4097     vMgrEncodeReassocResponse(&sFrame);
4098     // Setup the header
4099     sFrame.pHdr->sA3.wFrameCtl = cpu_to_le16(
4100         (
4101         WLAN_SET_FC_FTYPE(WLAN_TYPE_MGR) |
4102         WLAN_SET_FC_FSTYPE(WLAN_FSTYPE_REASSOCRESP)
4103         ));
4104     memcpy( sFrame.pHdr->sA3.abyAddr1, pDstAddr, WLAN_ADDR_LEN);
4105     memcpy( sFrame.pHdr->sA3.abyAddr2, pMgmt->abyMACAddr, WLAN_ADDR_LEN);
4106     memcpy( sFrame.pHdr->sA3.abyAddr3, pMgmt->abyCurrBSSID, WLAN_BSSID_LEN);
4107
4108     *sFrame.pwCapInfo = cpu_to_le16(wCurrCapInfo);
4109     *sFrame.pwStatus = cpu_to_le16(wAssocStatus);
4110     *sFrame.pwAid = cpu_to_le16((WORD)(wAssocAID | BIT14 | BIT15));
4111
4112     // Copy the rate set
4113     sFrame.pSuppRates = (PWLAN_IE_SUPP_RATES)(sFrame.pBuf + sFrame.len);
4114     sFrame.len += ((PWLAN_IE_SUPP_RATES)pCurrSuppRates)->len + WLAN_IEHDR_LEN;
4115     memcpy(sFrame.pSuppRates,
4116              pCurrSuppRates,
4117              ((PWLAN_IE_SUPP_RATES)pCurrSuppRates)->len + WLAN_IEHDR_LEN
4118              );
4119
4120     if (((PWLAN_IE_SUPP_RATES)pCurrExtSuppRates)->len != 0) {
4121         sFrame.pExtSuppRates = (PWLAN_IE_SUPP_RATES)(sFrame.pBuf + sFrame.len);
4122         sFrame.len += ((PWLAN_IE_SUPP_RATES)pCurrExtSuppRates)->len + WLAN_IEHDR_LEN;
4123         memcpy(sFrame.pExtSuppRates,
4124              pCurrExtSuppRates,
4125              ((PWLAN_IE_SUPP_RATES)pCurrExtSuppRates)->len + WLAN_IEHDR_LEN
4126              );
4127     }
4128
4129     // Adjust the length fields
4130     pTxPacket->cbMPDULen = sFrame.len;
4131     pTxPacket->cbPayloadLen = sFrame.len - WLAN_HDR_ADDR3_LEN;
4132
4133     return pTxPacket;
4134 }
4135
4136
4137 /*+
4138  *
4139  * Routine Description:
4140  *  Handles probe response management frames.
4141  *
4142  *
4143  * Return Value:
4144  *    none.
4145  *
4146 -*/
4147
4148 static
4149 void
4150 s_vMgrRxProbeResponse(
4151      PSDevice pDevice,
4152      PSMgmtObject pMgmt,
4153      PSRxMgmtPacket pRxPacket
4154     )
4155 {
4156     PKnownBSS           pBSSList = NULL;
4157     WLAN_FR_PROBERESP   sFrame;
4158     BYTE                byCurrChannel = pRxPacket->byRxChannel;
4159     ERPObject           sERP;
4160     BOOL                bChannelHit = TRUE;
4161
4162
4163     memset(&sFrame, 0, sizeof(WLAN_FR_PROBERESP));
4164     // decode the frame
4165     sFrame.len = pRxPacket->cbMPDULen;
4166     sFrame.pBuf = (PBYTE)pRxPacket->p80211Header;
4167     vMgrDecodeProbeResponse(&sFrame);
4168
4169     if ((sFrame.pqwTimestamp == NULL)
4170         || (sFrame.pwBeaconInterval == NULL)
4171         || (sFrame.pwCapInfo == NULL)
4172         || (sFrame.pSSID == NULL)
4173         || (sFrame.pSuppRates == NULL)) {
4174
4175         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Probe resp:Fail addr:[%p]\n",
4176                 pRxPacket->p80211Header);
4177         DBG_PORT80(0xCC);
4178         return;
4179     }
4180
4181     if(sFrame.pSSID->len == 0)
4182        DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Rx Probe resp: SSID len = 0 \n");
4183
4184
4185     //{{ RobertYu:20050201, 11a  byCurrChannel != sFrame.pDSParms->byCurrChannel mapping
4186     if( byCurrChannel > CB_MAX_CHANNEL_24G )
4187     {
4188         if (sFrame.pDSParms) {
4189                 if (byCurrChannel ==
4190                     RFaby11aChannelIndex[sFrame.pDSParms->byCurrChannel-1])
4191                         bChannelHit = TRUE;
4192                 byCurrChannel =
4193                         RFaby11aChannelIndex[sFrame.pDSParms->byCurrChannel-1];
4194         } else {
4195                 bChannelHit = TRUE;
4196         }
4197     } else {
4198         if (sFrame.pDSParms) {
4199                 if (byCurrChannel == sFrame.pDSParms->byCurrChannel)
4200                         bChannelHit = TRUE;
4201                 byCurrChannel = sFrame.pDSParms->byCurrChannel;
4202         } else {
4203                 bChannelHit = TRUE;
4204         }
4205     }
4206     //RobertYu:20050201
4207
4208 if(ChannelExceedZoneType(pDevice,byCurrChannel)==TRUE)
4209       return;
4210
4211     if (sFrame.pERP) {
4212         sERP.byERP = sFrame.pERP->byContext;
4213         sERP.bERPExist = TRUE;
4214     } else {
4215         sERP.bERPExist = FALSE;
4216         sERP.byERP = 0;
4217     }
4218
4219
4220     // update or insert the bss
4221     pBSSList = BSSpAddrIsInBSSList((void *) pDevice,
4222                                    sFrame.pHdr->sA3.abyAddr3,
4223                                    sFrame.pSSID);
4224     if (pBSSList) {
4225         BSSbUpdateToBSSList((void *) pDevice,
4226                             *sFrame.pqwTimestamp,
4227                             *sFrame.pwBeaconInterval,
4228                             *sFrame.pwCapInfo,
4229                             byCurrChannel,
4230                             bChannelHit,
4231                             sFrame.pSSID,
4232                             sFrame.pSuppRates,
4233                             sFrame.pExtSuppRates,
4234                             &sERP,
4235                             sFrame.pRSN,
4236                             sFrame.pRSNWPA,
4237                             sFrame.pIE_Country,
4238                             sFrame.pIE_Quiet,
4239                             pBSSList,
4240                             sFrame.len - WLAN_HDR_ADDR3_LEN,
4241                             /* payload of probresponse */
4242                             sFrame.pHdr->sA4.abyAddr4,
4243                             (void *) pRxPacket);
4244     } else {
4245         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Probe resp/insert: RxChannel = : %d\n", byCurrChannel);
4246         BSSbInsertToBSSList((void *) pDevice,
4247                             sFrame.pHdr->sA3.abyAddr3,
4248                             *sFrame.pqwTimestamp,
4249                             *sFrame.pwBeaconInterval,
4250                             *sFrame.pwCapInfo,
4251                             byCurrChannel,
4252                             sFrame.pSSID,
4253                             sFrame.pSuppRates,
4254                             sFrame.pExtSuppRates,
4255                             &sERP,
4256                             sFrame.pRSN,
4257                             sFrame.pRSNWPA,
4258                             sFrame.pIE_Country,
4259                             sFrame.pIE_Quiet,
4260                             sFrame.len - WLAN_HDR_ADDR3_LEN,
4261                             sFrame.pHdr->sA4.abyAddr4,   /* payload of beacon */
4262                             (void *) pRxPacket);
4263     }
4264     return;
4265
4266 }
4267
4268 /*+
4269  *
4270  * Routine Description:(AP)or(Ad-hoc STA)
4271  *  Handles probe request management frames.
4272  *
4273  *
4274  * Return Value:
4275  *    none.
4276  *
4277 -*/
4278
4279
4280 static
4281 void
4282 s_vMgrRxProbeRequest(
4283      PSDevice pDevice,
4284      PSMgmtObject pMgmt,
4285      PSRxMgmtPacket pRxPacket
4286     )
4287 {
4288     WLAN_FR_PROBEREQ    sFrame;
4289     CMD_STATUS          Status;
4290     PSTxMgmtPacket      pTxPacket;
4291     BYTE                byPHYType = BB_TYPE_11B;
4292
4293     // STA in Ad-hoc mode: when latest TBTT beacon transmit success,
4294     // STA have to response this request.
4295     if ((pMgmt->eCurrMode == WMAC_MODE_ESS_AP) ||
4296         ((pMgmt->eCurrMode == WMAC_MODE_IBSS_STA) && pDevice->bBeaconSent)) {
4297
4298         memset(&sFrame, 0, sizeof(WLAN_FR_PROBEREQ));
4299         // decode the frame
4300         sFrame.len = pRxPacket->cbMPDULen;
4301         sFrame.pBuf = (PBYTE)pRxPacket->p80211Header;
4302         vMgrDecodeProbeRequest(&sFrame);
4303 /*
4304         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Probe request rx:MAC addr:%pM\n",
4305                 sFrame.pHdr->sA3.abyAddr2);
4306 */
4307         if (sFrame.pSSID->len != 0) {
4308             if (sFrame.pSSID->len != ((PWLAN_IE_SSID)pMgmt->abyCurrSSID)->len)
4309                 return;
4310             if (memcmp(sFrame.pSSID->abySSID,
4311                        ((PWLAN_IE_SSID)pMgmt->abyCurrSSID)->abySSID,
4312                        ((PWLAN_IE_SSID)pMgmt->abyCurrSSID)->len) != 0) {
4313                        return;
4314             }
4315         }
4316
4317         if ((sFrame.pSuppRates->len > 4) || (sFrame.pExtSuppRates != NULL)) {
4318             byPHYType = BB_TYPE_11G;
4319         }
4320
4321         // Probe response reply..
4322         pTxPacket = s_MgrMakeProbeResponse
4323                     (
4324                       pDevice,
4325                       pMgmt,
4326                       pMgmt->wCurrCapInfo,
4327                       pMgmt->wCurrBeaconPeriod,
4328                       pMgmt->uCurrChannel,
4329                       0,
4330                       sFrame.pHdr->sA3.abyAddr2,
4331                       (PWLAN_IE_SSID)pMgmt->abyCurrSSID,
4332                       (PBYTE)pMgmt->abyCurrBSSID,
4333                       (PWLAN_IE_SUPP_RATES)pMgmt->abyCurrSuppRates,
4334                       (PWLAN_IE_SUPP_RATES)pMgmt->abyCurrExtSuppRates,
4335                        byPHYType
4336                     );
4337         if (pTxPacket != NULL ){
4338             /* send the frame */
4339             Status = csMgmt_xmit(pDevice, pTxPacket);
4340             if (Status != CMD_STATUS_PENDING) {
4341                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Mgt:Probe response tx failed\n");
4342             }
4343             else {
4344 //                DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Mgt:Probe response tx sending..\n");
4345             }
4346         }
4347     }
4348
4349     return;
4350 }
4351
4352 /*+
4353  *
4354  * Routine Description:
4355  *
4356  *  Entry point for the reception and handling of 802.11 management
4357  *  frames. Makes a determination of the frame type and then calls
4358  *  the appropriate function.
4359  *
4360  *
4361  * Return Value:
4362  *    none.
4363  *
4364 -*/
4365
4366 void vMgrRxManagePacket(void *hDeviceContext,
4367                         PSMgmtObject pMgmt,
4368                         PSRxMgmtPacket pRxPacket)
4369 {
4370     PSDevice    pDevice = (PSDevice)hDeviceContext;
4371     BOOL        bInScan = FALSE;
4372     unsigned int        uNodeIndex = 0;
4373     NODE_STATE  eNodeState = 0;
4374     CMD_STATUS  Status;
4375
4376
4377     if (pMgmt->eCurrMode == WMAC_MODE_ESS_AP) {
4378         if (BSSbIsSTAInNodeDB(pDevice, pRxPacket->p80211Header->sA3.abyAddr2, &uNodeIndex))
4379             eNodeState = pMgmt->sNodeDBTable[uNodeIndex].eNodeState;
4380     }
4381
4382     switch( WLAN_GET_FC_FSTYPE((pRxPacket->p80211Header->sA3.wFrameCtl)) ){
4383
4384         case WLAN_FSTYPE_ASSOCREQ:
4385             // Frame Clase = 2
4386             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "rx assocreq\n");
4387             if ((pMgmt->eCurrMode == WMAC_MODE_ESS_AP) &&
4388                 (eNodeState < NODE_AUTH)) {
4389                 // send deauth notification
4390                 // reason = (6) class 2 received from nonauth sta
4391                 vMgrDeAuthenBeginSta(pDevice,
4392                                      pMgmt,
4393                                      pRxPacket->p80211Header->sA3.abyAddr2,
4394                                      (6),
4395                                      &Status
4396                                      );
4397                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "wmgr: send vMgrDeAuthenBeginSta 1\n");
4398             }
4399             else {
4400                 s_vMgrRxAssocRequest(pDevice, pMgmt, pRxPacket, uNodeIndex);
4401             }
4402             break;
4403
4404         case WLAN_FSTYPE_ASSOCRESP:
4405             // Frame Clase = 2
4406             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "rx assocresp1\n");
4407             s_vMgrRxAssocResponse(pDevice, pMgmt, pRxPacket, FALSE);
4408             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "rx assocresp2\n");
4409             break;
4410
4411         case WLAN_FSTYPE_REASSOCREQ:
4412             // Frame Clase = 2
4413             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "rx reassocreq\n");
4414             // Todo: reassoc
4415             if ((pMgmt->eCurrMode == WMAC_MODE_ESS_AP) &&
4416                (eNodeState < NODE_AUTH)) {
4417                 // send deauth notification
4418                 // reason = (6) class 2 received from nonauth sta
4419                 vMgrDeAuthenBeginSta(pDevice,
4420                                      pMgmt,
4421                                      pRxPacket->p80211Header->sA3.abyAddr2,
4422                                      (6),
4423                                      &Status
4424                                      );
4425                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "wmgr: send vMgrDeAuthenBeginSta 2\n");
4426
4427             }
4428             s_vMgrRxReAssocRequest(pDevice, pMgmt, pRxPacket, uNodeIndex);
4429             break;
4430
4431         case WLAN_FSTYPE_REASSOCRESP:
4432             // Frame Clase = 2
4433             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "rx reassocresp\n");
4434             s_vMgrRxAssocResponse(pDevice, pMgmt, pRxPacket, TRUE);
4435             break;
4436
4437         case WLAN_FSTYPE_PROBEREQ:
4438             // Frame Clase = 0
4439             //DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "rx probereq\n");
4440             s_vMgrRxProbeRequest(pDevice, pMgmt, pRxPacket);
4441             break;
4442
4443         case WLAN_FSTYPE_PROBERESP:
4444             // Frame Clase = 0
4445             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "rx proberesp\n");
4446
4447             s_vMgrRxProbeResponse(pDevice, pMgmt, pRxPacket);
4448             break;
4449
4450         case WLAN_FSTYPE_BEACON:
4451             // Frame Clase = 0
4452             //DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "rx beacon\n");
4453             if (pMgmt->eScanState != WMAC_NO_SCANNING) {
4454                 bInScan = TRUE;
4455             }
4456             s_vMgrRxBeacon(pDevice, pMgmt, pRxPacket, bInScan);
4457             break;
4458
4459         case WLAN_FSTYPE_ATIM:
4460             // Frame Clase = 1
4461             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "rx atim\n");
4462             break;
4463
4464         case WLAN_FSTYPE_DISASSOC:
4465             // Frame Clase = 2
4466             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "rx disassoc\n");
4467             if ((pMgmt->eCurrMode == WMAC_MODE_ESS_AP) &&
4468                 (eNodeState < NODE_AUTH)) {
4469                 // send deauth notification
4470                 // reason = (6) class 2 received from nonauth sta
4471                 vMgrDeAuthenBeginSta(pDevice,
4472                                      pMgmt,
4473                                      pRxPacket->p80211Header->sA3.abyAddr2,
4474                                      (6),
4475                                      &Status
4476                                      );
4477                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "wmgr: send vMgrDeAuthenBeginSta 3\n");
4478             }
4479             s_vMgrRxDisassociation(pDevice, pMgmt, pRxPacket);
4480             break;
4481
4482         case WLAN_FSTYPE_AUTHEN:
4483             // Frame Clase = 1
4484             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO  "rx authen\n");
4485             s_vMgrRxAuthentication(pDevice, pMgmt, pRxPacket);
4486             break;
4487
4488         case WLAN_FSTYPE_DEAUTHEN:
4489             // Frame Clase = 1
4490             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "rx deauthen\n");
4491             s_vMgrRxDeauthentication(pDevice, pMgmt, pRxPacket);
4492             break;
4493
4494         default:
4495             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "rx unknown mgmt\n");
4496     }
4497
4498     return;
4499 }
4500
4501 /*+
4502  *
4503  * Routine Description:
4504  *
4505  *
4506  *  Prepare beacon to send
4507  *
4508  * Return Value:
4509  *    TRUE if success; FALSE if failed.
4510  *
4511 -*/
4512 BOOL bMgrPrepareBeaconToSend(void *hDeviceContext, PSMgmtObject pMgmt)
4513 {
4514     PSDevice            pDevice = (PSDevice)hDeviceContext;
4515     PSTxMgmtPacket      pTxPacket;
4516
4517 //    pDevice->bBeaconBufReady = FALSE;
4518     if (pDevice->bEncryptionEnable || pDevice->bEnable8021x){
4519         pMgmt->wCurrCapInfo |= WLAN_SET_CAP_INFO_PRIVACY(1);
4520     }
4521     else {
4522         pMgmt->wCurrCapInfo &= ~WLAN_SET_CAP_INFO_PRIVACY(1);
4523     }
4524     pTxPacket = s_MgrMakeBeacon
4525                 (
4526                   pDevice,
4527                   pMgmt,
4528                   pMgmt->wCurrCapInfo,
4529                   pMgmt->wCurrBeaconPeriod,
4530                   pMgmt->uCurrChannel,
4531                   pMgmt->wCurrATIMWindow, //0,
4532                   (PWLAN_IE_SSID)pMgmt->abyCurrSSID,
4533                   (PBYTE)pMgmt->abyCurrBSSID,
4534                   (PWLAN_IE_SUPP_RATES)pMgmt->abyCurrSuppRates,
4535                   (PWLAN_IE_SUPP_RATES)pMgmt->abyCurrExtSuppRates
4536                 );
4537
4538     if ((pMgmt->eCurrMode == WMAC_MODE_IBSS_STA) &&
4539         (pMgmt->abyCurrBSSID[0] == 0))
4540         return FALSE;
4541
4542     csBeacon_xmit(pDevice, pTxPacket);
4543     MACvRegBitsOn(pDevice, MAC_REG_TCR, TCR_AUTOBCNTX);
4544
4545     return TRUE;
4546 }
4547
4548
4549
4550
4551 /*+
4552  *
4553  * Routine Description:
4554  *
4555  *  Log a warning message based on the contents of the Status
4556  *  Code field of an 802.11 management frame.  Defines are
4557  *  derived from 802.11-1997 SPEC.
4558  *
4559  * Return Value:
4560  *    none.
4561  *
4562 -*/
4563 static
4564 void
4565 s_vMgrLogStatus(
4566      PSMgmtObject pMgmt,
4567      WORD  wStatus
4568     )
4569 {
4570     switch( wStatus ){
4571         case WLAN_MGMT_STATUS_UNSPEC_FAILURE:
4572             DBG_PRT(MSG_LEVEL_NOTICE, KERN_INFO "Status code == Unspecified error.\n");
4573             break;
4574         case WLAN_MGMT_STATUS_CAPS_UNSUPPORTED:
4575             DBG_PRT(MSG_LEVEL_NOTICE, KERN_INFO "Status code == Can't support all requested capabilities.\n");
4576             break;
4577         case WLAN_MGMT_STATUS_REASSOC_NO_ASSOC:
4578             DBG_PRT(MSG_LEVEL_NOTICE, KERN_INFO "Status code == Reassoc denied, can't confirm original Association.\n");
4579             break;
4580         case WLAN_MGMT_STATUS_ASSOC_DENIED_UNSPEC:
4581             DBG_PRT(MSG_LEVEL_NOTICE, KERN_INFO "Status code == Assoc denied, undefine in spec\n");
4582             break;
4583         case WLAN_MGMT_STATUS_UNSUPPORTED_AUTHALG:
4584             DBG_PRT(MSG_LEVEL_NOTICE, KERN_INFO "Status code == Peer doesn't support authen algorithm.\n");
4585             break;
4586         case WLAN_MGMT_STATUS_RX_AUTH_NOSEQ:
4587             DBG_PRT(MSG_LEVEL_NOTICE, KERN_INFO "Status code == Authen frame received out of sequence.\n");
4588             break;
4589         case WLAN_MGMT_STATUS_CHALLENGE_FAIL:
4590             DBG_PRT(MSG_LEVEL_NOTICE, KERN_INFO "Status code == Authen rejected, challenge  failure.\n");
4591             break;
4592         case WLAN_MGMT_STATUS_AUTH_TIMEOUT:
4593             DBG_PRT(MSG_LEVEL_NOTICE, KERN_INFO "Status code == Authen rejected, timeout waiting for next frame.\n");
4594             break;
4595         case WLAN_MGMT_STATUS_ASSOC_DENIED_BUSY:
4596             DBG_PRT(MSG_LEVEL_NOTICE, KERN_INFO "Status code == Assoc denied, AP too busy.\n");
4597             break;
4598         case WLAN_MGMT_STATUS_ASSOC_DENIED_RATES:
4599             DBG_PRT(MSG_LEVEL_NOTICE, KERN_INFO "Status code == Assoc denied, we haven't enough basic rates.\n");
4600             break;
4601         case WLAN_MGMT_STATUS_ASSOC_DENIED_SHORTPREAMBLE:
4602             DBG_PRT(MSG_LEVEL_NOTICE, KERN_INFO "Status code == Assoc denied, we do not support short preamble.\n");
4603             break;
4604         case WLAN_MGMT_STATUS_ASSOC_DENIED_PBCC:
4605             DBG_PRT(MSG_LEVEL_NOTICE, KERN_INFO "Status code == Assoc denied, we do not support PBCC.\n");
4606             break;
4607         case WLAN_MGMT_STATUS_ASSOC_DENIED_AGILITY:
4608             DBG_PRT(MSG_LEVEL_NOTICE, KERN_INFO "Status code == Assoc denied, we do not support channel agility.\n");
4609             break;
4610         default:
4611             DBG_PRT(MSG_LEVEL_NOTICE, KERN_INFO "Unknown status code %d.\n", wStatus);
4612             break;
4613     }
4614 }
4615
4616 /*
4617  *
4618  * Description:
4619  *    Add BSSID in PMKID Candidate list.
4620  *
4621  * Parameters:
4622  *  In:
4623  *      hDeviceContext - device structure point
4624  *      pbyBSSID - BSSID address for adding
4625  *      wRSNCap - BSS's RSN capability
4626  *  Out:
4627  *      none
4628  *
4629  * Return Value: none.
4630  *
4631 -*/
4632
4633 BOOL bAdd_PMKID_Candidate(void *hDeviceContext,
4634                           PBYTE pbyBSSID,
4635                           PSRSNCapObject psRSNCapObj)
4636 {
4637     PSDevice         pDevice = (PSDevice)hDeviceContext;
4638     PPMKID_CANDIDATE pCandidateList;
4639     unsigned int             ii = 0;
4640
4641     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"bAdd_PMKID_Candidate START: (%d)\n", (int)pDevice->gsPMKIDCandidate.NumCandidates);
4642
4643     if ((pDevice == NULL) || (pbyBSSID == NULL) || (psRSNCapObj == NULL))
4644         return FALSE;
4645
4646     if (pDevice->gsPMKIDCandidate.NumCandidates >= MAX_PMKIDLIST)
4647         return FALSE;
4648
4649
4650
4651     // Update Old Candidate
4652     for (ii = 0; ii < pDevice->gsPMKIDCandidate.NumCandidates; ii++) {
4653         pCandidateList = &pDevice->gsPMKIDCandidate.CandidateList[ii];
4654         if (!memcmp(pCandidateList->BSSID, pbyBSSID, ETH_ALEN)) {
4655                 if ((psRSNCapObj->bRSNCapExist == TRUE)
4656                     && (psRSNCapObj->wRSNCap & BIT0)) {
4657                         pCandidateList->Flags |=
4658                                 NDIS_802_11_PMKID_CANDIDATE_PREAUTH_ENABLED;
4659                 } else {
4660                         pCandidateList->Flags &=
4661                                 ~(NDIS_802_11_PMKID_CANDIDATE_PREAUTH_ENABLED);
4662                 }
4663             return TRUE;
4664         }
4665     }
4666
4667     // New Candidate
4668     pCandidateList = &pDevice->gsPMKIDCandidate.CandidateList[pDevice->gsPMKIDCandidate.NumCandidates];
4669     if ((psRSNCapObj->bRSNCapExist == TRUE) && (psRSNCapObj->wRSNCap & BIT0)) {
4670         pCandidateList->Flags |= NDIS_802_11_PMKID_CANDIDATE_PREAUTH_ENABLED;
4671     } else {
4672         pCandidateList->Flags &= ~(NDIS_802_11_PMKID_CANDIDATE_PREAUTH_ENABLED);
4673     }
4674     memcpy(pCandidateList->BSSID, pbyBSSID, ETH_ALEN);
4675     pDevice->gsPMKIDCandidate.NumCandidates++;
4676     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"NumCandidates:%d\n", (int)pDevice->gsPMKIDCandidate.NumCandidates);
4677     return TRUE;
4678 }
4679
4680 /*
4681  *
4682  * Description:
4683  *    Flush PMKID Candidate list.
4684  *
4685  * Parameters:
4686  *  In:
4687  *      hDeviceContext - device structure point
4688  *  Out:
4689  *      none
4690  *
4691  * Return Value: none.
4692  *
4693 -*/
4694
4695 void vFlush_PMKID_Candidate(void *hDeviceContext)
4696 {
4697     PSDevice        pDevice = (PSDevice)hDeviceContext;
4698
4699     if (pDevice == NULL)
4700         return;
4701
4702     memset(&pDevice->gsPMKIDCandidate, 0, sizeof(SPMKIDCandidateEvent));
4703 }
4704
4705 static BOOL
4706 s_bCipherMatch (
4707      PKnownBSS                        pBSSNode,
4708      NDIS_802_11_ENCRYPTION_STATUS    EncStatus,
4709      PBYTE                           pbyCCSPK,
4710      PBYTE                           pbyCCSGK
4711     )
4712 {
4713     BYTE byMulticastCipher = KEY_CTL_INVALID;
4714     BYTE byCipherMask = 0x00;
4715     int i;
4716
4717     if (pBSSNode == NULL)
4718         return FALSE;
4719
4720     // check cap. of BSS
4721     if ((WLAN_GET_CAP_INFO_PRIVACY(pBSSNode->wCapInfo) != 0) &&
4722          (EncStatus == Ndis802_11Encryption1Enabled)) {
4723         // default is WEP only
4724         byMulticastCipher = KEY_CTL_WEP;
4725     }
4726
4727     if ((WLAN_GET_CAP_INFO_PRIVACY(pBSSNode->wCapInfo) != 0) &&
4728         (pBSSNode->bWPA2Valid == TRUE) &&
4729
4730         ((EncStatus == Ndis802_11Encryption3Enabled) ||
4731          (EncStatus == Ndis802_11Encryption2Enabled))) {
4732         //WPA2
4733         // check Group Key Cipher
4734         if ((pBSSNode->byCSSGK == WLAN_11i_CSS_WEP40) ||
4735             (pBSSNode->byCSSGK == WLAN_11i_CSS_WEP104)) {
4736             byMulticastCipher = KEY_CTL_WEP;
4737         } else if (pBSSNode->byCSSGK == WLAN_11i_CSS_TKIP) {
4738             byMulticastCipher = KEY_CTL_TKIP;
4739         } else if (pBSSNode->byCSSGK == WLAN_11i_CSS_CCMP) {
4740             byMulticastCipher = KEY_CTL_CCMP;
4741         } else {
4742             byMulticastCipher = KEY_CTL_INVALID;
4743         }
4744
4745         /* check Pairwise Key Cipher */
4746         for (i = 0; i < pBSSNode->wCSSPKCount; i++) {
4747                 if ((pBSSNode->abyCSSPK[i] == WLAN_11i_CSS_WEP40) ||
4748                     (pBSSNode->abyCSSPK[i] == WLAN_11i_CSS_WEP104)) {
4749                         /* this should not happen as defined 802.11i */
4750                         byCipherMask |= 0x01;
4751                 } else if (pBSSNode->abyCSSPK[i] == WLAN_11i_CSS_TKIP) {
4752                         byCipherMask |= 0x02;
4753                 } else if (pBSSNode->abyCSSPK[i] == WLAN_11i_CSS_CCMP) {
4754                         byCipherMask |= 0x04;
4755                 } else if (pBSSNode->abyCSSPK[i] == WLAN_11i_CSS_USE_GROUP) {
4756                         /* use group key only ignore all others */
4757                         byCipherMask = 0;
4758                         i = pBSSNode->wCSSPKCount;
4759                 }
4760         }
4761
4762     } else if ((WLAN_GET_CAP_INFO_PRIVACY(pBSSNode->wCapInfo) != 0) &&
4763                 (pBSSNode->bWPAValid == TRUE) &&
4764                 ((EncStatus == Ndis802_11Encryption2Enabled) || (EncStatus == Ndis802_11Encryption3Enabled))) {
4765         //WPA
4766         // check Group Key Cipher
4767         if ((pBSSNode->byGKType == WPA_WEP40) ||
4768             (pBSSNode->byGKType == WPA_WEP104)) {
4769             byMulticastCipher = KEY_CTL_WEP;
4770         } else if (pBSSNode->byGKType == WPA_TKIP) {
4771             byMulticastCipher = KEY_CTL_TKIP;
4772         } else if (pBSSNode->byGKType == WPA_AESCCMP) {
4773             byMulticastCipher = KEY_CTL_CCMP;
4774         } else {
4775             byMulticastCipher = KEY_CTL_INVALID;
4776         }
4777
4778         /* check Pairwise Key Cipher */
4779         for (i = 0; i < pBSSNode->wPKCount; i++) {
4780                 if (pBSSNode->abyPKType[i] == WPA_TKIP) {
4781                         byCipherMask |= 0x02;
4782                 } else if (pBSSNode->abyPKType[i] == WPA_AESCCMP) {
4783                         byCipherMask |= 0x04;
4784                 } else if (pBSSNode->abyPKType[i] == WPA_NONE) {
4785                         /* use group key only ignore all others */
4786                         byCipherMask = 0;
4787                         i = pBSSNode->wPKCount;
4788                 }
4789         }
4790     }
4791
4792     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"%d, %d, %d, %d, EncStatus:%d\n",
4793         byMulticastCipher, byCipherMask, pBSSNode->bWPAValid, pBSSNode->bWPA2Valid, EncStatus);
4794
4795     // mask our cap. with BSS
4796     if (EncStatus == Ndis802_11Encryption1Enabled) {
4797
4798         // For supporting Cisco migration mode, don't care pairwise key cipher
4799         //if ((byMulticastCipher == KEY_CTL_WEP) &&
4800         //    (byCipherMask == 0)) {
4801         if ((byMulticastCipher == KEY_CTL_WEP) &&
4802             (byCipherMask == 0)) {
4803             *pbyCCSGK = KEY_CTL_WEP;
4804             *pbyCCSPK = KEY_CTL_NONE;
4805             return TRUE;
4806         } else {
4807             return FALSE;
4808         }
4809
4810     } else if (EncStatus == Ndis802_11Encryption2Enabled) {
4811         if ((byMulticastCipher == KEY_CTL_TKIP) &&
4812             (byCipherMask == 0)) {
4813             *pbyCCSGK = KEY_CTL_TKIP;
4814             *pbyCCSPK = KEY_CTL_NONE;
4815             return TRUE;
4816         } else if ((byMulticastCipher == KEY_CTL_WEP) &&
4817                    ((byCipherMask & 0x02) != 0)) {
4818             *pbyCCSGK = KEY_CTL_WEP;
4819             *pbyCCSPK = KEY_CTL_TKIP;
4820             return TRUE;
4821         } else if ((byMulticastCipher == KEY_CTL_TKIP) &&
4822                    ((byCipherMask & 0x02) != 0)) {
4823             *pbyCCSGK = KEY_CTL_TKIP;
4824             *pbyCCSPK = KEY_CTL_TKIP;
4825             return TRUE;
4826         } else {
4827             return FALSE;
4828         }
4829     } else if (EncStatus == Ndis802_11Encryption3Enabled) {
4830         if ((byMulticastCipher == KEY_CTL_CCMP) &&
4831             (byCipherMask == 0)) {
4832             // When CCMP is enable, "Use group cipher suite" shall not be a valid option.
4833             return FALSE;
4834         } else if ((byMulticastCipher == KEY_CTL_WEP) &&
4835                    ((byCipherMask & 0x04) != 0)) {
4836             *pbyCCSGK = KEY_CTL_WEP;
4837             *pbyCCSPK = KEY_CTL_CCMP;
4838             return TRUE;
4839         } else if ((byMulticastCipher == KEY_CTL_TKIP) &&
4840                    ((byCipherMask & 0x04) != 0)) {
4841             *pbyCCSGK = KEY_CTL_TKIP;
4842             *pbyCCSPK = KEY_CTL_CCMP;
4843             return TRUE;
4844         } else if ((byMulticastCipher == KEY_CTL_CCMP) &&
4845                    ((byCipherMask & 0x04) != 0)) {
4846             *pbyCCSGK = KEY_CTL_CCMP;
4847             *pbyCCSPK = KEY_CTL_CCMP;
4848             return TRUE;
4849         } else {
4850             return FALSE;
4851         }
4852     }
4853     return TRUE;
4854 }
4855
4856