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