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