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