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