]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/staging/rt2860/sta/assoc.c
Merge branch 'rcu/next' of git://git.kernel.org/pub/scm/linux/kernel/git/paulmck...
[mv-sheeva.git] / drivers / staging / rt2860 / sta / assoc.c
1 /*
2  *************************************************************************
3  * Ralink Tech Inc.
4  * 5F., No.36, Taiyuan St., Jhubei City,
5  * Hsinchu County 302,
6  * Taiwan, R.O.C.
7  *
8  * (c) Copyright 2002-2007, Ralink Technology, Inc.
9  *
10  * This program is free software; you can redistribute it and/or modify  *
11  * it under the terms of the GNU General Public License as published by  *
12  * the Free Software Foundation; either version 2 of the License, or     *
13  * (at your option) any later version.                                   *
14  *                                                                       *
15  * This program is distributed in the hope that it will be useful,       *
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of        *
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
18  * GNU General Public License for more details.                          *
19  *                                                                       *
20  * You should have received a copy of the GNU General Public License     *
21  * along with this program; if not, write to the                         *
22  * Free Software Foundation, Inc.,                                       *
23  * 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
24  *                                                                       *
25  *************************************************************************
26
27         Module Name:
28         assoc.c
29
30         Abstract:
31
32         Revision History:
33         Who                     When                    What
34         --------        ----------              ----------------------------------------------
35         John            2004-9-3                porting from RT2500
36 */
37 #include "../rt_config.h"
38
39 u8 CipherWpaTemplate[] = {
40         0xdd,                   /* WPA IE */
41         0x16,                   /* Length */
42         0x00, 0x50, 0xf2, 0x01, /* oui */
43         0x01, 0x00,             /* Version */
44         0x00, 0x50, 0xf2, 0x02, /* Multicast */
45         0x01, 0x00,             /* Number of unicast */
46         0x00, 0x50, 0xf2, 0x02, /* unicast */
47         0x01, 0x00,             /* number of authentication method */
48         0x00, 0x50, 0xf2, 0x01  /* authentication */
49 };
50
51 u8 CipherWpa2Template[] = {
52         0x30,                   /* RSN IE */
53         0x14,                   /* Length */
54         0x01, 0x00,             /* Version */
55         0x00, 0x0f, 0xac, 0x02, /* group cipher, TKIP */
56         0x01, 0x00,             /* number of pairwise */
57         0x00, 0x0f, 0xac, 0x02, /* unicast */
58         0x01, 0x00,             /* number of authentication method */
59         0x00, 0x0f, 0xac, 0x02, /* authentication */
60         0x00, 0x00,             /* RSN capability */
61 };
62
63 u8 Ccx2IeInfo[] = { 0x00, 0x40, 0x96, 0x03, 0x02 };
64
65 /*
66         ==========================================================================
67         Description:
68                 association state machine init, including state transition and timer init
69         Parameters:
70                 S - pointer to the association state machine
71
72         IRQL = PASSIVE_LEVEL
73
74         ==========================================================================
75  */
76 void AssocStateMachineInit(struct rt_rtmp_adapter *pAd,
77                            struct rt_state_machine *S, OUT STATE_MACHINE_FUNC Trans[])
78 {
79         StateMachineInit(S, Trans, MAX_ASSOC_STATE, MAX_ASSOC_MSG,
80                          (STATE_MACHINE_FUNC) Drop, ASSOC_IDLE,
81                          ASSOC_MACHINE_BASE);
82
83         /* first column */
84         StateMachineSetAction(S, ASSOC_IDLE, MT2_MLME_ASSOC_REQ,
85                               (STATE_MACHINE_FUNC) MlmeAssocReqAction);
86         StateMachineSetAction(S, ASSOC_IDLE, MT2_MLME_REASSOC_REQ,
87                               (STATE_MACHINE_FUNC) MlmeReassocReqAction);
88         StateMachineSetAction(S, ASSOC_IDLE, MT2_MLME_DISASSOC_REQ,
89                               (STATE_MACHINE_FUNC) MlmeDisassocReqAction);
90         StateMachineSetAction(S, ASSOC_IDLE, MT2_PEER_DISASSOC_REQ,
91                               (STATE_MACHINE_FUNC) PeerDisassocAction);
92
93         /* second column */
94         StateMachineSetAction(S, ASSOC_WAIT_RSP, MT2_MLME_ASSOC_REQ,
95                               (STATE_MACHINE_FUNC) InvalidStateWhenAssoc);
96         StateMachineSetAction(S, ASSOC_WAIT_RSP, MT2_MLME_REASSOC_REQ,
97                               (STATE_MACHINE_FUNC) InvalidStateWhenReassoc);
98         StateMachineSetAction(S, ASSOC_WAIT_RSP, MT2_MLME_DISASSOC_REQ,
99                               (STATE_MACHINE_FUNC)
100                               InvalidStateWhenDisassociate);
101         StateMachineSetAction(S, ASSOC_WAIT_RSP, MT2_PEER_DISASSOC_REQ,
102                               (STATE_MACHINE_FUNC) PeerDisassocAction);
103         StateMachineSetAction(S, ASSOC_WAIT_RSP, MT2_PEER_ASSOC_RSP,
104                               (STATE_MACHINE_FUNC) PeerAssocRspAction);
105         /* */
106         /* Patch 3Com AP MOde:3CRWE454G72 */
107         /* We send Assoc request frame to this AP, it always send Reassoc Rsp not Associate Rsp. */
108         /* */
109         StateMachineSetAction(S, ASSOC_WAIT_RSP, MT2_PEER_REASSOC_RSP,
110                               (STATE_MACHINE_FUNC) PeerAssocRspAction);
111         StateMachineSetAction(S, ASSOC_WAIT_RSP, MT2_ASSOC_TIMEOUT,
112                               (STATE_MACHINE_FUNC) AssocTimeoutAction);
113
114         /* third column */
115         StateMachineSetAction(S, REASSOC_WAIT_RSP, MT2_MLME_ASSOC_REQ,
116                               (STATE_MACHINE_FUNC) InvalidStateWhenAssoc);
117         StateMachineSetAction(S, REASSOC_WAIT_RSP, MT2_MLME_REASSOC_REQ,
118                               (STATE_MACHINE_FUNC) InvalidStateWhenReassoc);
119         StateMachineSetAction(S, REASSOC_WAIT_RSP, MT2_MLME_DISASSOC_REQ,
120                               (STATE_MACHINE_FUNC)
121                               InvalidStateWhenDisassociate);
122         StateMachineSetAction(S, REASSOC_WAIT_RSP, MT2_PEER_DISASSOC_REQ,
123                               (STATE_MACHINE_FUNC) PeerDisassocAction);
124         StateMachineSetAction(S, REASSOC_WAIT_RSP, MT2_PEER_REASSOC_RSP,
125                               (STATE_MACHINE_FUNC) PeerReassocRspAction);
126         /* */
127         /* Patch, AP doesn't send Reassociate Rsp frame to Station. */
128         /* */
129         StateMachineSetAction(S, REASSOC_WAIT_RSP, MT2_PEER_ASSOC_RSP,
130                               (STATE_MACHINE_FUNC) PeerReassocRspAction);
131         StateMachineSetAction(S, REASSOC_WAIT_RSP, MT2_REASSOC_TIMEOUT,
132                               (STATE_MACHINE_FUNC) ReassocTimeoutAction);
133
134         /* fourth column */
135         StateMachineSetAction(S, DISASSOC_WAIT_RSP, MT2_MLME_ASSOC_REQ,
136                               (STATE_MACHINE_FUNC) InvalidStateWhenAssoc);
137         StateMachineSetAction(S, DISASSOC_WAIT_RSP, MT2_MLME_REASSOC_REQ,
138                               (STATE_MACHINE_FUNC) InvalidStateWhenReassoc);
139         StateMachineSetAction(S, DISASSOC_WAIT_RSP, MT2_MLME_DISASSOC_REQ,
140                               (STATE_MACHINE_FUNC)
141                               InvalidStateWhenDisassociate);
142         StateMachineSetAction(S, DISASSOC_WAIT_RSP, MT2_PEER_DISASSOC_REQ,
143                               (STATE_MACHINE_FUNC) PeerDisassocAction);
144         StateMachineSetAction(S, DISASSOC_WAIT_RSP, MT2_DISASSOC_TIMEOUT,
145                               (STATE_MACHINE_FUNC) DisassocTimeoutAction);
146
147         /* initialize the timer */
148         RTMPInitTimer(pAd, &pAd->MlmeAux.AssocTimer,
149                       GET_TIMER_FUNCTION(AssocTimeout), pAd, FALSE);
150         RTMPInitTimer(pAd, &pAd->MlmeAux.ReassocTimer,
151                       GET_TIMER_FUNCTION(ReassocTimeout), pAd, FALSE);
152         RTMPInitTimer(pAd, &pAd->MlmeAux.DisassocTimer,
153                       GET_TIMER_FUNCTION(DisassocTimeout), pAd, FALSE);
154 }
155
156 /*
157         ==========================================================================
158         Description:
159                 Association timeout procedure. After association timeout, this function
160                 will be called and it will put a message into the MLME queue
161         Parameters:
162                 Standard timer parameters
163
164         IRQL = DISPATCH_LEVEL
165
166         ==========================================================================
167  */
168 void AssocTimeout(void *SystemSpecific1,
169                   void *FunctionContext,
170                   void *SystemSpecific2, void *SystemSpecific3)
171 {
172         struct rt_rtmp_adapter *pAd = (struct rt_rtmp_adapter *)FunctionContext;
173
174         /* Do nothing if the driver is starting halt state. */
175         /* This might happen when timer already been fired before cancel timer with mlmehalt */
176         if (RTMP_TEST_FLAG
177             (pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS | fRTMP_ADAPTER_NIC_NOT_EXIST))
178                 return;
179
180         MlmeEnqueue(pAd, ASSOC_STATE_MACHINE, MT2_ASSOC_TIMEOUT, 0, NULL);
181         RTMP_MLME_HANDLER(pAd);
182 }
183
184 /*
185         ==========================================================================
186         Description:
187                 Reassociation timeout procedure. After reassociation timeout, this
188                 function will be called and put a message into the MLME queue
189         Parameters:
190                 Standard timer parameters
191
192         IRQL = DISPATCH_LEVEL
193
194         ==========================================================================
195  */
196 void ReassocTimeout(void *SystemSpecific1,
197                     void *FunctionContext,
198                     void *SystemSpecific2, void *SystemSpecific3)
199 {
200         struct rt_rtmp_adapter *pAd = (struct rt_rtmp_adapter *)FunctionContext;
201
202         /* Do nothing if the driver is starting halt state. */
203         /* This might happen when timer already been fired before cancel timer with mlmehalt */
204         if (RTMP_TEST_FLAG
205             (pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS | fRTMP_ADAPTER_NIC_NOT_EXIST))
206                 return;
207
208         MlmeEnqueue(pAd, ASSOC_STATE_MACHINE, MT2_REASSOC_TIMEOUT, 0, NULL);
209         RTMP_MLME_HANDLER(pAd);
210 }
211
212 /*
213         ==========================================================================
214         Description:
215                 Disassociation timeout procedure. After disassociation timeout, this
216                 function will be called and put a message into the MLME queue
217         Parameters:
218                 Standard timer parameters
219
220         IRQL = DISPATCH_LEVEL
221
222         ==========================================================================
223  */
224 void DisassocTimeout(void *SystemSpecific1,
225                      void *FunctionContext,
226                      void *SystemSpecific2, void *SystemSpecific3)
227 {
228         struct rt_rtmp_adapter *pAd = (struct rt_rtmp_adapter *)FunctionContext;
229
230         /* Do nothing if the driver is starting halt state. */
231         /* This might happen when timer already been fired before cancel timer with mlmehalt */
232         if (RTMP_TEST_FLAG
233             (pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS | fRTMP_ADAPTER_NIC_NOT_EXIST))
234                 return;
235
236         MlmeEnqueue(pAd, ASSOC_STATE_MACHINE, MT2_DISASSOC_TIMEOUT, 0, NULL);
237         RTMP_MLME_HANDLER(pAd);
238 }
239
240 /*
241         ==========================================================================
242         Description:
243                 mlme assoc req handling procedure
244         Parameters:
245                 Adapter - Adapter pointer
246                 Elem - MLME Queue Element
247         Pre:
248                 the station has been authenticated and the following information is stored in the config
249                         -# SSID
250                         -# supported rates and their length
251                         -# listen interval (Adapter->StaCfg.default_listen_count)
252                         -# Transmit power  (Adapter->StaCfg.tx_power)
253         Post  :
254                 -# An association request frame is generated and sent to the air
255                 -# Association timer starts
256                 -# Association state -> ASSOC_WAIT_RSP
257
258         IRQL = DISPATCH_LEVEL
259
260         ==========================================================================
261  */
262 void MlmeAssocReqAction(struct rt_rtmp_adapter *pAd, struct rt_mlme_queue_elem *Elem)
263 {
264         u8 ApAddr[6];
265         struct rt_header_802_11 AssocHdr;
266         u8 WmeIe[9] =
267             { IE_VENDOR_SPECIFIC, 0x07, 0x00, 0x50, 0xf2, 0x02, 0x00, 0x01,
268        0x00 };
269         u16 ListenIntv;
270         unsigned long Timeout;
271         u16 CapabilityInfo;
272         BOOLEAN TimerCancelled;
273         u8 *pOutBuffer = NULL;
274         int NStatus;
275         unsigned long FrameLen = 0;
276         unsigned long tmp;
277         u16 VarIesOffset;
278         u16 Status;
279
280         /* Block all authentication request durning WPA block period */
281         if (pAd->StaCfg.bBlockAssoc == TRUE) {
282                 DBGPRINT(RT_DEBUG_TRACE,
283                          ("ASSOC - Block Assoc request durning WPA block period!\n"));
284                 pAd->Mlme.AssocMachine.CurrState = ASSOC_IDLE;
285                 Status = MLME_STATE_MACHINE_REJECT;
286                 MlmeEnqueue(pAd, MLME_CNTL_STATE_MACHINE, MT2_ASSOC_CONF, 2,
287                             &Status);
288         }
289         /* check sanity first */
290         else if (MlmeAssocReqSanity
291                  (pAd, Elem->Msg, Elem->MsgLen, ApAddr, &CapabilityInfo,
292                   &Timeout, &ListenIntv)) {
293                 RTMPCancelTimer(&pAd->MlmeAux.AssocTimer, &TimerCancelled);
294                 COPY_MAC_ADDR(pAd->MlmeAux.Bssid, ApAddr);
295
296                 /* Get an unused nonpaged memory */
297                 NStatus = MlmeAllocateMemory(pAd, &pOutBuffer);
298                 if (NStatus != NDIS_STATUS_SUCCESS) {
299                         DBGPRINT(RT_DEBUG_TRACE,
300                                  ("ASSOC - MlmeAssocReqAction() allocate memory failed \n"));
301                         pAd->Mlme.AssocMachine.CurrState = ASSOC_IDLE;
302                         Status = MLME_FAIL_NO_RESOURCE;
303                         MlmeEnqueue(pAd, MLME_CNTL_STATE_MACHINE,
304                                     MT2_ASSOC_CONF, 2, &Status);
305                         return;
306                 }
307                 /* Add by James 03/06/27 */
308                 pAd->StaCfg.AssocInfo.Length =
309                     sizeof(struct rt_ndis_802_11_association_information);
310                 /* Association don't need to report MAC address */
311                 pAd->StaCfg.AssocInfo.AvailableRequestFixedIEs =
312                     NDIS_802_11_AI_REQFI_CAPABILITIES |
313                     NDIS_802_11_AI_REQFI_LISTENINTERVAL;
314                 pAd->StaCfg.AssocInfo.RequestFixedIEs.Capabilities =
315                     CapabilityInfo;
316                 pAd->StaCfg.AssocInfo.RequestFixedIEs.ListenInterval =
317                     ListenIntv;
318                 /* Only reassociate need this */
319                 /*COPY_MAC_ADDR(pAd->StaCfg.AssocInfo.RequestFixedIEs.CurrentAPAddress, ApAddr); */
320                 pAd->StaCfg.AssocInfo.OffsetRequestIEs =
321                     sizeof(struct rt_ndis_802_11_association_information);
322
323                 NdisZeroMemory(pAd->StaCfg.ReqVarIEs, MAX_VIE_LEN);
324                 /* First add SSID */
325                 VarIesOffset = 0;
326                 NdisMoveMemory(pAd->StaCfg.ReqVarIEs + VarIesOffset, &SsidIe,
327                                1);
328                 VarIesOffset += 1;
329                 NdisMoveMemory(pAd->StaCfg.ReqVarIEs + VarIesOffset,
330                                &pAd->MlmeAux.SsidLen, 1);
331                 VarIesOffset += 1;
332                 NdisMoveMemory(pAd->StaCfg.ReqVarIEs + VarIesOffset,
333                                pAd->MlmeAux.Ssid, pAd->MlmeAux.SsidLen);
334                 VarIesOffset += pAd->MlmeAux.SsidLen;
335
336                 /* Second add Supported rates */
337                 NdisMoveMemory(pAd->StaCfg.ReqVarIEs + VarIesOffset, &SupRateIe,
338                                1);
339                 VarIesOffset += 1;
340                 NdisMoveMemory(pAd->StaCfg.ReqVarIEs + VarIesOffset,
341                                &pAd->MlmeAux.SupRateLen, 1);
342                 VarIesOffset += 1;
343                 NdisMoveMemory(pAd->StaCfg.ReqVarIEs + VarIesOffset,
344                                pAd->MlmeAux.SupRate, pAd->MlmeAux.SupRateLen);
345                 VarIesOffset += pAd->MlmeAux.SupRateLen;
346                 /* End Add by James */
347
348                 if ((pAd->CommonCfg.Channel > 14) &&
349                     (pAd->CommonCfg.bIEEE80211H == TRUE))
350                         CapabilityInfo |= 0x0100;
351
352                 DBGPRINT(RT_DEBUG_TRACE, ("ASSOC - Send ASSOC request...\n"));
353                 MgtMacHeaderInit(pAd, &AssocHdr, SUBTYPE_ASSOC_REQ, 0, ApAddr,
354                                  ApAddr);
355
356                 /* Build basic frame first */
357                 MakeOutgoingFrame(pOutBuffer, &FrameLen,
358                                   sizeof(struct rt_header_802_11), &AssocHdr,
359                                   2, &CapabilityInfo,
360                                   2, &ListenIntv,
361                                   1, &SsidIe,
362                                   1, &pAd->MlmeAux.SsidLen,
363                                   pAd->MlmeAux.SsidLen, pAd->MlmeAux.Ssid,
364                                   1, &SupRateIe,
365                                   1, &pAd->MlmeAux.SupRateLen,
366                                   pAd->MlmeAux.SupRateLen, pAd->MlmeAux.SupRate,
367                                   END_OF_ARGS);
368
369                 if (pAd->MlmeAux.ExtRateLen != 0) {
370                         MakeOutgoingFrame(pOutBuffer + FrameLen, &tmp,
371                                           1, &ExtRateIe,
372                                           1, &pAd->MlmeAux.ExtRateLen,
373                                           pAd->MlmeAux.ExtRateLen,
374                                           pAd->MlmeAux.ExtRate, END_OF_ARGS);
375                         FrameLen += tmp;
376                 }
377                 /* HT */
378                 if ((pAd->MlmeAux.HtCapabilityLen > 0)
379                     && (pAd->CommonCfg.PhyMode >= PHY_11ABGN_MIXED)) {
380                         unsigned long TmpLen;
381                         u8 HtLen;
382                         u8 BROADCOM[4] = { 0x0, 0x90, 0x4c, 0x33 };
383                         if (pAd->StaActive.SupportedPhyInfo.bPreNHt == TRUE) {
384                                 HtLen = SIZE_HT_CAP_IE + 4;
385                                 MakeOutgoingFrame(pOutBuffer + FrameLen,
386                                                   &TmpLen, 1, &WpaIe, 1, &HtLen,
387                                                   4, &BROADCOM[0],
388                                                   pAd->MlmeAux.HtCapabilityLen,
389                                                   &pAd->MlmeAux.HtCapability,
390                                                   END_OF_ARGS);
391                         } else {
392                                 MakeOutgoingFrame(pOutBuffer + FrameLen,
393                                                   &TmpLen, 1, &HtCapIe, 1,
394                                                   &pAd->MlmeAux.HtCapabilityLen,
395                                                   pAd->MlmeAux.HtCapabilityLen,
396                                                   &pAd->MlmeAux.HtCapability,
397                                                   END_OF_ARGS);
398                         }
399                         FrameLen += TmpLen;
400                 }
401                 /* add Ralink proprietary IE to inform AP this STA is going to use AGGREGATION or PIGGY-BACK+AGGREGATION */
402                 /* Case I: (Aggregation + Piggy-Back) */
403                 /* 1. user enable aggregation, AND */
404                 /* 2. Mac support piggy-back */
405                 /* 3. AP annouces it's PIGGY-BACK+AGGREGATION-capable in BEACON */
406                 /* Case II: (Aggregation) */
407                 /* 1. user enable aggregation, AND */
408                 /* 2. AP annouces it's AGGREGATION-capable in BEACON */
409                 if (pAd->CommonCfg.bAggregationCapable) {
410                         if ((pAd->CommonCfg.bPiggyBackCapable)
411                             && ((pAd->MlmeAux.APRalinkIe & 0x00000003) == 3)) {
412                                 unsigned long TmpLen;
413                                 u8 RalinkIe[9] =
414                                     { IE_VENDOR_SPECIFIC, 7, 0x00, 0x0c, 0x43,
415                             0x03, 0x00, 0x00, 0x00 };
416                                 MakeOutgoingFrame(pOutBuffer + FrameLen,
417                                                   &TmpLen, 9, RalinkIe,
418                                                   END_OF_ARGS);
419                                 FrameLen += TmpLen;
420                         } else if (pAd->MlmeAux.APRalinkIe & 0x00000001) {
421                                 unsigned long TmpLen;
422                                 u8 RalinkIe[9] =
423                                     { IE_VENDOR_SPECIFIC, 7, 0x00, 0x0c, 0x43,
424                             0x01, 0x00, 0x00, 0x00 };
425                                 MakeOutgoingFrame(pOutBuffer + FrameLen,
426                                                   &TmpLen, 9, RalinkIe,
427                                                   END_OF_ARGS);
428                                 FrameLen += TmpLen;
429                         }
430                 } else {
431                         unsigned long TmpLen;
432                         u8 RalinkIe[9] =
433                             { IE_VENDOR_SPECIFIC, 7, 0x00, 0x0c, 0x43, 0x06,
434                     0x00, 0x00, 0x00 };
435                         MakeOutgoingFrame(pOutBuffer + FrameLen, &TmpLen, 9,
436                                           RalinkIe, END_OF_ARGS);
437                         FrameLen += TmpLen;
438                 }
439
440                 if (pAd->MlmeAux.APEdcaParm.bValid) {
441                         if (pAd->CommonCfg.bAPSDCapable
442                             && pAd->MlmeAux.APEdcaParm.bAPSDCapable) {
443                                 struct rt_qbss_sta_info_parm QosInfo;
444
445                                 NdisZeroMemory(&QosInfo,
446                                                sizeof(struct rt_qbss_sta_info_parm));
447                                 QosInfo.UAPSD_AC_BE = pAd->CommonCfg.bAPSDAC_BE;
448                                 QosInfo.UAPSD_AC_BK = pAd->CommonCfg.bAPSDAC_BK;
449                                 QosInfo.UAPSD_AC_VI = pAd->CommonCfg.bAPSDAC_VI;
450                                 QosInfo.UAPSD_AC_VO = pAd->CommonCfg.bAPSDAC_VO;
451                                 QosInfo.MaxSPLength =
452                                     pAd->CommonCfg.MaxSPLength;
453                                 WmeIe[8] |= *(u8 *)& QosInfo;
454                         } else {
455                                 /* The Parameter Set Count is set to Â¡Â§0¡¨ in the association request frames */
456                                 /* WmeIe[8] |= (pAd->MlmeAux.APEdcaParm.EdcaUpdateCount & 0x0f); */
457                         }
458
459                         MakeOutgoingFrame(pOutBuffer + FrameLen, &tmp,
460                                           9, &WmeIe[0], END_OF_ARGS);
461                         FrameLen += tmp;
462                 }
463                 /* */
464                 /* Let WPA(#221) Element ID on the end of this association frame. */
465                 /* Otherwise some AP will fail on parsing Element ID and set status fail on Assoc Rsp. */
466                 /* For example: Put Vendor Specific IE on the front of WPA IE. */
467                 /* This happens on AP (Model No:Linksys WRK54G) */
468                 /* */
469                 if (((pAd->StaCfg.AuthMode == Ndis802_11AuthModeWPAPSK) ||
470                      (pAd->StaCfg.AuthMode == Ndis802_11AuthModeWPA2PSK) ||
471                      (pAd->StaCfg.AuthMode == Ndis802_11AuthModeWPA) ||
472                      (pAd->StaCfg.AuthMode == Ndis802_11AuthModeWPA2)
473                     )
474                     ) {
475                         u8 RSNIe = IE_WPA;
476
477                         if ((pAd->StaCfg.AuthMode == Ndis802_11AuthModeWPA2PSK)
478                             || (pAd->StaCfg.AuthMode ==
479                                 Ndis802_11AuthModeWPA2)) {
480                                 RSNIe = IE_WPA2;
481                         }
482
483                         if ((pAd->StaCfg.WpaSupplicantUP !=
484                              WPA_SUPPLICANT_ENABLE)
485                             && (pAd->StaCfg.bRSN_IE_FromWpaSupplicant == FALSE))
486                                 RTMPMakeRSNIE(pAd, pAd->StaCfg.AuthMode,
487                                               pAd->StaCfg.WepStatus, BSS0);
488
489                         /* Check for WPA PMK cache list */
490                         if (pAd->StaCfg.AuthMode == Ndis802_11AuthModeWPA2) {
491                                 int idx;
492                                 BOOLEAN FoundPMK = FALSE;
493                                 /* Search chched PMKID, append it if existed */
494                                 for (idx = 0; idx < PMKID_NO; idx++) {
495                                         if (NdisEqualMemory
496                                             (ApAddr,
497                                              &pAd->StaCfg.SavedPMK[idx].BSSID,
498                                              6)) {
499                                                 FoundPMK = TRUE;
500                                                 break;
501                                         }
502                                 }
503                                 if (FoundPMK) {
504                                         /* Set PMK number */
505                                         *(u16 *)& pAd->StaCfg.RSN_IE[pAd->
506                                                                         StaCfg.
507                                                                         RSNIE_Len]
508                                             = 1;
509                                         NdisMoveMemory(&pAd->StaCfg.
510                                                        RSN_IE[pAd->StaCfg.
511                                                               RSNIE_Len + 2],
512                                                        &pAd->StaCfg.
513                                                        SavedPMK[idx].PMKID, 16);
514                                         pAd->StaCfg.RSNIE_Len += 18;
515                                 }
516                         }
517
518                         if ((pAd->StaCfg.WpaSupplicantUP ==
519                              WPA_SUPPLICANT_ENABLE)
520                             && (pAd->StaCfg.bRSN_IE_FromWpaSupplicant ==
521                                 TRUE)) {
522                                 MakeOutgoingFrame(pOutBuffer + FrameLen, &tmp,
523                                                   pAd->StaCfg.RSNIE_Len,
524                                                   pAd->StaCfg.RSN_IE,
525                                                   END_OF_ARGS);
526                         } else {
527                                 MakeOutgoingFrame(pOutBuffer + FrameLen, &tmp,
528                                                   1, &RSNIe,
529                                                   1, &pAd->StaCfg.RSNIE_Len,
530                                                   pAd->StaCfg.RSNIE_Len,
531                                                   pAd->StaCfg.RSN_IE,
532                                                   END_OF_ARGS);
533                         }
534
535                         FrameLen += tmp;
536
537                         if ((pAd->StaCfg.WpaSupplicantUP !=
538                              WPA_SUPPLICANT_ENABLE)
539                             || (pAd->StaCfg.bRSN_IE_FromWpaSupplicant ==
540                                 FALSE)) {
541                                 /* Append Variable IE */
542                                 NdisMoveMemory(pAd->StaCfg.ReqVarIEs +
543                                                VarIesOffset, &RSNIe, 1);
544                                 VarIesOffset += 1;
545                                 NdisMoveMemory(pAd->StaCfg.ReqVarIEs +
546                                                VarIesOffset,
547                                                &pAd->StaCfg.RSNIE_Len, 1);
548                                 VarIesOffset += 1;
549                         }
550                         NdisMoveMemory(pAd->StaCfg.ReqVarIEs + VarIesOffset,
551                                        pAd->StaCfg.RSN_IE,
552                                        pAd->StaCfg.RSNIE_Len);
553                         VarIesOffset += pAd->StaCfg.RSNIE_Len;
554
555                         /* Set Variable IEs Length */
556                         pAd->StaCfg.ReqVarIELen = VarIesOffset;
557                 }
558
559                 MiniportMMRequest(pAd, 0, pOutBuffer, FrameLen);
560                 MlmeFreeMemory(pAd, pOutBuffer);
561
562                 RTMPSetTimer(&pAd->MlmeAux.AssocTimer, Timeout);
563                 pAd->Mlme.AssocMachine.CurrState = ASSOC_WAIT_RSP;
564         } else {
565                 DBGPRINT(RT_DEBUG_TRACE,
566                          ("ASSOC - MlmeAssocReqAction() sanity check failed. BUG!\n"));
567                 pAd->Mlme.AssocMachine.CurrState = ASSOC_IDLE;
568                 Status = MLME_INVALID_FORMAT;
569                 MlmeEnqueue(pAd, MLME_CNTL_STATE_MACHINE, MT2_ASSOC_CONF, 2,
570                             &Status);
571         }
572
573 }
574
575 /*
576         ==========================================================================
577         Description:
578                 mlme reassoc req handling procedure
579         Parameters:
580                 Elem -
581         Pre:
582                 -# SSID  (Adapter->StaCfg.ssid[])
583                 -# BSSID (AP address, Adapter->StaCfg.bssid)
584                 -# Supported rates (Adapter->StaCfg.supported_rates[])
585                 -# Supported rates length (Adapter->StaCfg.supported_rates_len)
586                 -# Tx power (Adapter->StaCfg.tx_power)
587
588         IRQL = DISPATCH_LEVEL
589
590         ==========================================================================
591  */
592 void MlmeReassocReqAction(struct rt_rtmp_adapter *pAd, struct rt_mlme_queue_elem *Elem)
593 {
594         u8 ApAddr[6];
595         struct rt_header_802_11 ReassocHdr;
596         u8 WmeIe[9] =
597             { IE_VENDOR_SPECIFIC, 0x07, 0x00, 0x50, 0xf2, 0x02, 0x00, 0x01,
598        0x00 };
599         u16 CapabilityInfo, ListenIntv;
600         unsigned long Timeout;
601         unsigned long FrameLen = 0;
602         BOOLEAN TimerCancelled;
603         int NStatus;
604         unsigned long tmp;
605         u8 *pOutBuffer = NULL;
606         u16 Status;
607
608         /* Block all authentication request durning WPA block period */
609         if (pAd->StaCfg.bBlockAssoc == TRUE) {
610                 DBGPRINT(RT_DEBUG_TRACE,
611                          ("ASSOC - Block ReAssoc request durning WPA block period!\n"));
612                 pAd->Mlme.AssocMachine.CurrState = ASSOC_IDLE;
613                 Status = MLME_STATE_MACHINE_REJECT;
614                 MlmeEnqueue(pAd, MLME_CNTL_STATE_MACHINE, MT2_REASSOC_CONF, 2,
615                             &Status);
616         }
617         /* the parameters are the same as the association */
618         else if (MlmeAssocReqSanity
619                  (pAd, Elem->Msg, Elem->MsgLen, ApAddr, &CapabilityInfo,
620                   &Timeout, &ListenIntv)) {
621                 RTMPCancelTimer(&pAd->MlmeAux.ReassocTimer, &TimerCancelled);
622
623                 NStatus = MlmeAllocateMemory(pAd, &pOutBuffer); /*Get an unused nonpaged memory */
624                 if (NStatus != NDIS_STATUS_SUCCESS) {
625                         DBGPRINT(RT_DEBUG_TRACE,
626                                  ("ASSOC - MlmeReassocReqAction() allocate memory failed \n"));
627                         pAd->Mlme.AssocMachine.CurrState = ASSOC_IDLE;
628                         Status = MLME_FAIL_NO_RESOURCE;
629                         MlmeEnqueue(pAd, MLME_CNTL_STATE_MACHINE,
630                                     MT2_REASSOC_CONF, 2, &Status);
631                         return;
632                 }
633
634                 COPY_MAC_ADDR(pAd->MlmeAux.Bssid, ApAddr);
635
636                 /* make frame, use bssid as the AP address?? */
637                 DBGPRINT(RT_DEBUG_TRACE,
638                          ("ASSOC - Send RE-ASSOC request...\n"));
639                 MgtMacHeaderInit(pAd, &ReassocHdr, SUBTYPE_REASSOC_REQ, 0,
640                                  ApAddr, ApAddr);
641                 MakeOutgoingFrame(pOutBuffer, &FrameLen, sizeof(struct rt_header_802_11),
642                                   &ReassocHdr, 2, &CapabilityInfo, 2,
643                                   &ListenIntv, MAC_ADDR_LEN, ApAddr, 1, &SsidIe,
644                                   1, &pAd->MlmeAux.SsidLen,
645                                   pAd->MlmeAux.SsidLen, pAd->MlmeAux.Ssid, 1,
646                                   &SupRateIe, 1, &pAd->MlmeAux.SupRateLen,
647                                   pAd->MlmeAux.SupRateLen, pAd->MlmeAux.SupRate,
648                                   END_OF_ARGS);
649
650                 if (pAd->MlmeAux.ExtRateLen != 0) {
651                         MakeOutgoingFrame(pOutBuffer + FrameLen, &tmp,
652                                           1, &ExtRateIe,
653                                           1, &pAd->MlmeAux.ExtRateLen,
654                                           pAd->MlmeAux.ExtRateLen,
655                                           pAd->MlmeAux.ExtRate, END_OF_ARGS);
656                         FrameLen += tmp;
657                 }
658
659                 if (pAd->MlmeAux.APEdcaParm.bValid) {
660                         if (pAd->CommonCfg.bAPSDCapable
661                             && pAd->MlmeAux.APEdcaParm.bAPSDCapable) {
662                                 struct rt_qbss_sta_info_parm QosInfo;
663
664                                 NdisZeroMemory(&QosInfo,
665                                                sizeof(struct rt_qbss_sta_info_parm));
666                                 QosInfo.UAPSD_AC_BE = pAd->CommonCfg.bAPSDAC_BE;
667                                 QosInfo.UAPSD_AC_BK = pAd->CommonCfg.bAPSDAC_BK;
668                                 QosInfo.UAPSD_AC_VI = pAd->CommonCfg.bAPSDAC_VI;
669                                 QosInfo.UAPSD_AC_VO = pAd->CommonCfg.bAPSDAC_VO;
670                                 QosInfo.MaxSPLength =
671                                     pAd->CommonCfg.MaxSPLength;
672                                 WmeIe[8] |= *(u8 *)& QosInfo;
673                         }
674
675                         MakeOutgoingFrame(pOutBuffer + FrameLen, &tmp,
676                                           9, &WmeIe[0], END_OF_ARGS);
677                         FrameLen += tmp;
678                 }
679                 /* HT */
680                 if ((pAd->MlmeAux.HtCapabilityLen > 0)
681                     && (pAd->CommonCfg.PhyMode >= PHY_11ABGN_MIXED)) {
682                         unsigned long TmpLen;
683                         u8 HtLen;
684                         u8 BROADCOM[4] = { 0x0, 0x90, 0x4c, 0x33 };
685                         if (pAd->StaActive.SupportedPhyInfo.bPreNHt == TRUE) {
686                                 HtLen = SIZE_HT_CAP_IE + 4;
687                                 MakeOutgoingFrame(pOutBuffer + FrameLen,
688                                                   &TmpLen, 1, &WpaIe, 1, &HtLen,
689                                                   4, &BROADCOM[0],
690                                                   pAd->MlmeAux.HtCapabilityLen,
691                                                   &pAd->MlmeAux.HtCapability,
692                                                   END_OF_ARGS);
693                         } else {
694                                 MakeOutgoingFrame(pOutBuffer + FrameLen,
695                                                   &TmpLen, 1, &HtCapIe, 1,
696                                                   &pAd->MlmeAux.HtCapabilityLen,
697                                                   pAd->MlmeAux.HtCapabilityLen,
698                                                   &pAd->MlmeAux.HtCapability,
699                                                   END_OF_ARGS);
700                         }
701                         FrameLen += TmpLen;
702                 }
703                 /* add Ralink proprietary IE to inform AP this STA is going to use AGGREGATION or PIGGY-BACK+AGGREGATION */
704                 /* Case I: (Aggregation + Piggy-Back) */
705                 /* 1. user enable aggregation, AND */
706                 /* 2. Mac support piggy-back */
707                 /* 3. AP annouces it's PIGGY-BACK+AGGREGATION-capable in BEACON */
708                 /* Case II: (Aggregation) */
709                 /* 1. user enable aggregation, AND */
710                 /* 2. AP annouces it's AGGREGATION-capable in BEACON */
711                 if (pAd->CommonCfg.bAggregationCapable) {
712                         if ((pAd->CommonCfg.bPiggyBackCapable)
713                             && ((pAd->MlmeAux.APRalinkIe & 0x00000003) == 3)) {
714                                 unsigned long TmpLen;
715                                 u8 RalinkIe[9] =
716                                     { IE_VENDOR_SPECIFIC, 7, 0x00, 0x0c, 0x43,
717                             0x03, 0x00, 0x00, 0x00 };
718                                 MakeOutgoingFrame(pOutBuffer + FrameLen,
719                                                   &TmpLen, 9, RalinkIe,
720                                                   END_OF_ARGS);
721                                 FrameLen += TmpLen;
722                         } else if (pAd->MlmeAux.APRalinkIe & 0x00000001) {
723                                 unsigned long TmpLen;
724                                 u8 RalinkIe[9] =
725                                     { IE_VENDOR_SPECIFIC, 7, 0x00, 0x0c, 0x43,
726                             0x01, 0x00, 0x00, 0x00 };
727                                 MakeOutgoingFrame(pOutBuffer + FrameLen,
728                                                   &TmpLen, 9, RalinkIe,
729                                                   END_OF_ARGS);
730                                 FrameLen += TmpLen;
731                         }
732                 } else {
733                         unsigned long TmpLen;
734                         u8 RalinkIe[9] =
735                             { IE_VENDOR_SPECIFIC, 7, 0x00, 0x0c, 0x43, 0x04,
736                     0x00, 0x00, 0x00 };
737                         MakeOutgoingFrame(pOutBuffer + FrameLen, &TmpLen, 9,
738                                           RalinkIe, END_OF_ARGS);
739                         FrameLen += TmpLen;
740                 }
741
742                 MiniportMMRequest(pAd, 0, pOutBuffer, FrameLen);
743                 MlmeFreeMemory(pAd, pOutBuffer);
744
745                 RTMPSetTimer(&pAd->MlmeAux.ReassocTimer, Timeout);      /* in mSec */
746                 pAd->Mlme.AssocMachine.CurrState = REASSOC_WAIT_RSP;
747         } else {
748                 DBGPRINT(RT_DEBUG_TRACE,
749                          ("ASSOC - MlmeReassocReqAction() sanity check failed. BUG!\n"));
750                 pAd->Mlme.AssocMachine.CurrState = ASSOC_IDLE;
751                 Status = MLME_INVALID_FORMAT;
752                 MlmeEnqueue(pAd, MLME_CNTL_STATE_MACHINE, MT2_REASSOC_CONF, 2,
753                             &Status);
754         }
755 }
756
757 /*
758         ==========================================================================
759         Description:
760                 Upper layer issues disassoc request
761         Parameters:
762                 Elem -
763
764         IRQL = PASSIVE_LEVEL
765
766         ==========================================================================
767  */
768 void MlmeDisassocReqAction(struct rt_rtmp_adapter *pAd, struct rt_mlme_queue_elem *Elem)
769 {
770         struct rt_mlme_disassoc_req *pDisassocReq;
771         struct rt_header_802_11 DisassocHdr;
772         struct rt_header_802_11 * pDisassocHdr;
773         u8 *pOutBuffer = NULL;
774         unsigned long FrameLen = 0;
775         int NStatus;
776         BOOLEAN TimerCancelled;
777         unsigned long Timeout = 500;
778         u16 Status;
779
780         /* skip sanity check */
781         pDisassocReq = (struct rt_mlme_disassoc_req *)(Elem->Msg);
782
783         NStatus = MlmeAllocateMemory(pAd, &pOutBuffer); /*Get an unused nonpaged memory */
784         if (NStatus != NDIS_STATUS_SUCCESS) {
785                 DBGPRINT(RT_DEBUG_TRACE,
786                          ("ASSOC - MlmeDisassocReqAction() allocate memory failed\n"));
787                 pAd->Mlme.AssocMachine.CurrState = ASSOC_IDLE;
788                 Status = MLME_FAIL_NO_RESOURCE;
789                 MlmeEnqueue(pAd, MLME_CNTL_STATE_MACHINE, MT2_DISASSOC_CONF, 2,
790                             &Status);
791                 return;
792         }
793
794         RTMPCancelTimer(&pAd->MlmeAux.DisassocTimer, &TimerCancelled);
795
796         DBGPRINT(RT_DEBUG_TRACE,
797                 ("ASSOC - Send DISASSOC request[BSSID::%pM (Reason=%d)\n",
798                         pDisassocReq->Addr, pDisassocReq->Reason));
799         MgtMacHeaderInit(pAd, &DisassocHdr, SUBTYPE_DISASSOC, 0, pDisassocReq->Addr, pDisassocReq->Addr);       /* patch peap ttls switching issue */
800         MakeOutgoingFrame(pOutBuffer, &FrameLen,
801                           sizeof(struct rt_header_802_11), &DisassocHdr,
802                           2, &pDisassocReq->Reason, END_OF_ARGS);
803         MiniportMMRequest(pAd, 0, pOutBuffer, FrameLen);
804
805         /* To patch Instance and Buffalo(N) AP */
806         /* Driver has to send deauth to Instance AP, but Buffalo(N) needs to send disassoc to reset Authenticator's state machine */
807         /* Therefore, we send both of them. */
808         pDisassocHdr = (struct rt_header_802_11 *) pOutBuffer;
809         pDisassocHdr->FC.SubType = SUBTYPE_DEAUTH;
810         MiniportMMRequest(pAd, 0, pOutBuffer, FrameLen);
811
812         MlmeFreeMemory(pAd, pOutBuffer);
813
814         pAd->StaCfg.DisassocReason = REASON_DISASSOC_STA_LEAVING;
815         COPY_MAC_ADDR(pAd->StaCfg.DisassocSta, pDisassocReq->Addr);
816
817         RTMPSetTimer(&pAd->MlmeAux.DisassocTimer, Timeout);     /* in mSec */
818         pAd->Mlme.AssocMachine.CurrState = DISASSOC_WAIT_RSP;
819
820         RtmpOSWrielessEventSend(pAd, SIOCGIWAP, -1, NULL, NULL, 0);
821
822 }
823
824 /*
825         ==========================================================================
826         Description:
827                 peer sends assoc rsp back
828         Parameters:
829                 Elme - MLME message containing the received frame
830
831         IRQL = DISPATCH_LEVEL
832
833         ==========================================================================
834  */
835 void PeerAssocRspAction(struct rt_rtmp_adapter *pAd, struct rt_mlme_queue_elem *Elem)
836 {
837         u16 CapabilityInfo, Status, Aid;
838         u8 SupRate[MAX_LEN_OF_SUPPORTED_RATES], SupRateLen;
839         u8 ExtRate[MAX_LEN_OF_SUPPORTED_RATES], ExtRateLen;
840         u8 Addr2[MAC_ADDR_LEN];
841         BOOLEAN TimerCancelled;
842         u8 CkipFlag;
843         struct rt_edca_parm EdcaParm;
844         struct rt_ht_capability_ie HtCapability;
845         struct rt_add_ht_info_ie AddHtInfo;     /* AP might use this additional ht info IE */
846         u8 HtCapabilityLen = 0;
847         u8 AddHtInfoLen;
848         u8 NewExtChannelOffset = 0xff;
849
850         if (PeerAssocRspSanity
851             (pAd, Elem->Msg, Elem->MsgLen, Addr2, &CapabilityInfo, &Status,
852              &Aid, SupRate, &SupRateLen, ExtRate, &ExtRateLen, &HtCapability,
853              &AddHtInfo, &HtCapabilityLen, &AddHtInfoLen, &NewExtChannelOffset,
854              &EdcaParm, &CkipFlag)) {
855                 /* The frame is for me ? */
856                 if (MAC_ADDR_EQUAL(Addr2, pAd->MlmeAux.Bssid)) {
857                         DBGPRINT(RT_DEBUG_TRACE,
858                                  ("PeerAssocRspAction():ASSOC - receive ASSOC_RSP to me (status=%d)\n",
859                                   Status));
860                         DBGPRINT(RT_DEBUG_TRACE,
861                                  ("PeerAssocRspAction():MacTable [%d].AMsduSize = %d. ClientStatusFlags = 0x%lx \n",
862                                   Elem->Wcid,
863                                   pAd->MacTab.Content[BSSID_WCID].AMsduSize,
864                                   pAd->MacTab.Content[BSSID_WCID].
865                                   ClientStatusFlags));
866                         RTMPCancelTimer(&pAd->MlmeAux.AssocTimer,
867                                         &TimerCancelled);
868
869                         if (Status == MLME_SUCCESS) {
870                                 u8 MaxSupportedRateIn500Kbps = 0;
871                                 u8 idx;
872
873                                 /* supported rates array may not be sorted. sort it and find the maximum rate */
874                                 for (idx = 0; idx < SupRateLen; idx++) {
875                                         if (MaxSupportedRateIn500Kbps <
876                                             (SupRate[idx] & 0x7f))
877                                                 MaxSupportedRateIn500Kbps =
878                                                     SupRate[idx] & 0x7f;
879                                 }
880
881                                 for (idx = 0; idx < ExtRateLen; idx++) {
882                                         if (MaxSupportedRateIn500Kbps <
883                                             (ExtRate[idx] & 0x7f))
884                                                 MaxSupportedRateIn500Kbps =
885                                                     ExtRate[idx] & 0x7f;
886                                 }
887                                 /* go to procedure listed on page 376 */
888                                 AssocPostProc(pAd, Addr2, CapabilityInfo, Aid,
889                                               SupRate, SupRateLen, ExtRate,
890                                               ExtRateLen, &EdcaParm,
891                                               &HtCapability, HtCapabilityLen,
892                                               &AddHtInfo);
893
894                                 StaAddMacTableEntry(pAd,
895                                                     &pAd->MacTab.
896                                                     Content[BSSID_WCID],
897                                                     MaxSupportedRateIn500Kbps,
898                                                     &HtCapability,
899                                                     HtCapabilityLen, &AddHtInfo,
900                                                     AddHtInfoLen,
901                                                     CapabilityInfo);
902                         }
903                         pAd->Mlme.AssocMachine.CurrState = ASSOC_IDLE;
904                         MlmeEnqueue(pAd, MLME_CNTL_STATE_MACHINE,
905                                     MT2_ASSOC_CONF, 2, &Status);
906                 }
907         } else {
908                 DBGPRINT(RT_DEBUG_TRACE,
909                          ("ASSOC - PeerAssocRspAction() sanity check fail\n"));
910         }
911 }
912
913 /*
914         ==========================================================================
915         Description:
916                 peer sends reassoc rsp
917         Parametrs:
918                 Elem - MLME message cntaining the received frame
919
920         IRQL = DISPATCH_LEVEL
921
922         ==========================================================================
923  */
924 void PeerReassocRspAction(struct rt_rtmp_adapter *pAd, struct rt_mlme_queue_elem *Elem)
925 {
926         u16 CapabilityInfo;
927         u16 Status;
928         u16 Aid;
929         u8 SupRate[MAX_LEN_OF_SUPPORTED_RATES], SupRateLen;
930         u8 ExtRate[MAX_LEN_OF_SUPPORTED_RATES], ExtRateLen;
931         u8 Addr2[MAC_ADDR_LEN];
932         u8 CkipFlag;
933         BOOLEAN TimerCancelled;
934         struct rt_edca_parm EdcaParm;
935         struct rt_ht_capability_ie HtCapability;
936         struct rt_add_ht_info_ie AddHtInfo;     /* AP might use this additional ht info IE */
937         u8 HtCapabilityLen;
938         u8 AddHtInfoLen;
939         u8 NewExtChannelOffset = 0xff;
940
941         if (PeerAssocRspSanity
942             (pAd, Elem->Msg, Elem->MsgLen, Addr2, &CapabilityInfo, &Status,
943              &Aid, SupRate, &SupRateLen, ExtRate, &ExtRateLen, &HtCapability,
944              &AddHtInfo, &HtCapabilityLen, &AddHtInfoLen, &NewExtChannelOffset,
945              &EdcaParm, &CkipFlag)) {
946                 if (MAC_ADDR_EQUAL(Addr2, pAd->MlmeAux.Bssid))  /* The frame is for me ? */
947                 {
948                         DBGPRINT(RT_DEBUG_TRACE,
949                                  ("ASSOC - receive REASSOC_RSP to me (status=%d)\n",
950                                   Status));
951                         RTMPCancelTimer(&pAd->MlmeAux.ReassocTimer,
952                                         &TimerCancelled);
953
954                         if (Status == MLME_SUCCESS) {
955                                 /* go to procedure listed on page 376 */
956                                 AssocPostProc(pAd, Addr2, CapabilityInfo, Aid,
957                                               SupRate, SupRateLen, ExtRate,
958                                               ExtRateLen, &EdcaParm,
959                                               &HtCapability, HtCapabilityLen,
960                                               &AddHtInfo);
961
962                                 {
963                                         wext_notify_event_assoc(pAd);
964                                         RtmpOSWrielessEventSend(pAd, SIOCGIWAP,
965                                                                 -1,
966                                                                 &pAd->MlmeAux.
967                                                                 Bssid[0], NULL,
968                                                                 0);
969                                 }
970
971                         }
972                         /* CkipFlag is no use for reassociate */
973                         pAd->Mlme.AssocMachine.CurrState = ASSOC_IDLE;
974                         MlmeEnqueue(pAd, MLME_CNTL_STATE_MACHINE,
975                                     MT2_REASSOC_CONF, 2, &Status);
976                 }
977         } else {
978                 DBGPRINT(RT_DEBUG_TRACE,
979                          ("ASSOC - PeerReassocRspAction() sanity check fail\n"));
980         }
981
982 }
983
984 /*
985         ==========================================================================
986         Description:
987                 procedures on IEEE 802.11/1999 p.376
988         Parametrs:
989
990         IRQL = DISPATCH_LEVEL
991
992         ==========================================================================
993  */
994 void AssocPostProc(struct rt_rtmp_adapter *pAd, u8 *pAddr2, u16 CapabilityInfo, u16 Aid, u8 SupRate[], u8 SupRateLen, u8 ExtRate[], u8 ExtRateLen, struct rt_edca_parm *pEdcaParm, struct rt_ht_capability_ie * pHtCapability, u8 HtCapabilityLen, struct rt_add_ht_info_ie * pAddHtInfo)       /* AP might use this additional ht info IE */
995 {
996         unsigned long Idx;
997
998         pAd->MlmeAux.BssType = BSS_INFRA;
999         COPY_MAC_ADDR(pAd->MlmeAux.Bssid, pAddr2);
1000         pAd->MlmeAux.Aid = Aid;
1001         pAd->MlmeAux.CapabilityInfo =
1002             CapabilityInfo & SUPPORTED_CAPABILITY_INFO;
1003
1004         /* Some HT AP might lost WMM IE. We add WMM ourselves. beacuase HT requires QoS on. */
1005         if ((HtCapabilityLen > 0) && (pEdcaParm->bValid == FALSE)) {
1006                 pEdcaParm->bValid = TRUE;
1007                 pEdcaParm->Aifsn[0] = 3;
1008                 pEdcaParm->Aifsn[1] = 7;
1009                 pEdcaParm->Aifsn[2] = 2;
1010                 pEdcaParm->Aifsn[3] = 2;
1011
1012                 pEdcaParm->Cwmin[0] = 4;
1013                 pEdcaParm->Cwmin[1] = 4;
1014                 pEdcaParm->Cwmin[2] = 3;
1015                 pEdcaParm->Cwmin[3] = 2;
1016
1017                 pEdcaParm->Cwmax[0] = 10;
1018                 pEdcaParm->Cwmax[1] = 10;
1019                 pEdcaParm->Cwmax[2] = 4;
1020                 pEdcaParm->Cwmax[3] = 3;
1021
1022                 pEdcaParm->Txop[0] = 0;
1023                 pEdcaParm->Txop[1] = 0;
1024                 pEdcaParm->Txop[2] = 96;
1025                 pEdcaParm->Txop[3] = 48;
1026
1027         }
1028
1029         NdisMoveMemory(&pAd->MlmeAux.APEdcaParm, pEdcaParm, sizeof(struct rt_edca_parm));
1030
1031         /* filter out un-supported rates */
1032         pAd->MlmeAux.SupRateLen = SupRateLen;
1033         NdisMoveMemory(pAd->MlmeAux.SupRate, SupRate, SupRateLen);
1034         RTMPCheckRates(pAd, pAd->MlmeAux.SupRate, &pAd->MlmeAux.SupRateLen);
1035
1036         /* filter out un-supported rates */
1037         pAd->MlmeAux.ExtRateLen = ExtRateLen;
1038         NdisMoveMemory(pAd->MlmeAux.ExtRate, ExtRate, ExtRateLen);
1039         RTMPCheckRates(pAd, pAd->MlmeAux.ExtRate, &pAd->MlmeAux.ExtRateLen);
1040
1041         if (HtCapabilityLen > 0) {
1042                 RTMPCheckHt(pAd, BSSID_WCID, pHtCapability, pAddHtInfo);
1043         }
1044         DBGPRINT(RT_DEBUG_TRACE,
1045                  ("AssocPostProc===>  AP.AMsduSize = %d. ClientStatusFlags = 0x%lx \n",
1046                   pAd->MacTab.Content[BSSID_WCID].AMsduSize,
1047                   pAd->MacTab.Content[BSSID_WCID].ClientStatusFlags));
1048
1049         DBGPRINT(RT_DEBUG_TRACE,
1050                  ("AssocPostProc===>    (Mmps=%d, AmsduSize=%d, )\n",
1051                   pAd->MacTab.Content[BSSID_WCID].MmpsMode,
1052                   pAd->MacTab.Content[BSSID_WCID].AMsduSize));
1053
1054         /* Set New WPA information */
1055         Idx = BssTableSearch(&pAd->ScanTab, pAddr2, pAd->MlmeAux.Channel);
1056         if (Idx == BSS_NOT_FOUND) {
1057                 DBGPRINT_ERR(("ASSOC - Can't find BSS after receiving Assoc response\n"));
1058         } else {
1059                 /* Init variable */
1060                 pAd->MacTab.Content[BSSID_WCID].RSNIE_Len = 0;
1061                 NdisZeroMemory(pAd->MacTab.Content[BSSID_WCID].RSN_IE,
1062                                MAX_LEN_OF_RSNIE);
1063
1064                 /* Store appropriate RSN_IE for WPA SM negotiation later */
1065                 if ((pAd->StaCfg.AuthMode >= Ndis802_11AuthModeWPA)
1066                     && (pAd->ScanTab.BssEntry[Idx].VarIELen != 0)) {
1067                         u8 *pVIE;
1068                         u16 len;
1069                         struct rt_eid * pEid;
1070
1071                         pVIE = pAd->ScanTab.BssEntry[Idx].VarIEs;
1072                         len = pAd->ScanTab.BssEntry[Idx].VarIELen;
1073                         /*KH need to check again */
1074                         /* Don't allow to go to sleep mode if authmode is WPA-related. */
1075                         /*This can make Authentication process more smoothly. */
1076                         RTMP_CLEAR_PSFLAG(pAd, fRTMP_PS_CAN_GO_SLEEP);
1077
1078                         while (len > 0) {
1079                                 pEid = (struct rt_eid *) pVIE;
1080                                 /* For WPA/WPAPSK */
1081                                 if ((pEid->Eid == IE_WPA)
1082                                     &&
1083                                     (NdisEqualMemory(pEid->Octet, WPA_OUI, 4))
1084                                     && (pAd->StaCfg.AuthMode ==
1085                                         Ndis802_11AuthModeWPA
1086                                         || pAd->StaCfg.AuthMode ==
1087                                         Ndis802_11AuthModeWPAPSK)) {
1088                                         NdisMoveMemory(pAd->MacTab.
1089                                                        Content[BSSID_WCID].
1090                                                        RSN_IE, pVIE,
1091                                                        (pEid->Len + 2));
1092                                         pAd->MacTab.Content[BSSID_WCID].
1093                                             RSNIE_Len = (pEid->Len + 2);
1094                                         DBGPRINT(RT_DEBUG_TRACE,
1095                                                  ("AssocPostProc===> Store RSN_IE for WPA SM negotiation \n"));
1096                                 }
1097                                 /* For WPA2/WPA2PSK */
1098                                 else if ((pEid->Eid == IE_RSN)
1099                                          &&
1100                                          (NdisEqualMemory
1101                                           (pEid->Octet + 2, RSN_OUI, 3))
1102                                          && (pAd->StaCfg.AuthMode ==
1103                                              Ndis802_11AuthModeWPA2
1104                                              || pAd->StaCfg.AuthMode ==
1105                                              Ndis802_11AuthModeWPA2PSK)) {
1106                                         NdisMoveMemory(pAd->MacTab.
1107                                                        Content[BSSID_WCID].
1108                                                        RSN_IE, pVIE,
1109                                                        (pEid->Len + 2));
1110                                         pAd->MacTab.Content[BSSID_WCID].
1111                                             RSNIE_Len = (pEid->Len + 2);
1112                                         DBGPRINT(RT_DEBUG_TRACE,
1113                                                  ("AssocPostProc===> Store RSN_IE for WPA2 SM negotiation \n"));
1114                                 }
1115
1116                                 pVIE += (pEid->Len + 2);
1117                                 len -= (pEid->Len + 2);
1118                         }
1119
1120                 }
1121
1122                 if (pAd->MacTab.Content[BSSID_WCID].RSNIE_Len == 0) {
1123                         DBGPRINT(RT_DEBUG_TRACE,
1124                                  ("AssocPostProc===> no RSN_IE \n"));
1125                 } else {
1126                         hex_dump("RSN_IE",
1127                                  pAd->MacTab.Content[BSSID_WCID].RSN_IE,
1128                                  pAd->MacTab.Content[BSSID_WCID].RSNIE_Len);
1129                 }
1130         }
1131 }
1132
1133 /*
1134         ==========================================================================
1135         Description:
1136                 left part of IEEE 802.11/1999 p.374
1137         Parameters:
1138                 Elem - MLME message containing the received frame
1139
1140         IRQL = DISPATCH_LEVEL
1141
1142         ==========================================================================
1143  */
1144 void PeerDisassocAction(struct rt_rtmp_adapter *pAd, struct rt_mlme_queue_elem *Elem)
1145 {
1146         u8 Addr2[MAC_ADDR_LEN];
1147         u16 Reason;
1148
1149         DBGPRINT(RT_DEBUG_TRACE, ("ASSOC - PeerDisassocAction()\n"));
1150         if (PeerDisassocSanity(pAd, Elem->Msg, Elem->MsgLen, Addr2, &Reason)) {
1151                 DBGPRINT(RT_DEBUG_TRACE,
1152                          ("ASSOC - PeerDisassocAction() Reason = %d\n",
1153                           Reason));
1154                 if (INFRA_ON(pAd)
1155                     && MAC_ADDR_EQUAL(pAd->CommonCfg.Bssid, Addr2)) {
1156
1157                         if (pAd->CommonCfg.bWirelessEvent) {
1158                                 RTMPSendWirelessEvent(pAd,
1159                                                       IW_DISASSOC_EVENT_FLAG,
1160                                                       pAd->MacTab.
1161                                                       Content[BSSID_WCID].Addr,
1162                                                       BSS0, 0);
1163                         }
1164
1165                         LinkDown(pAd, TRUE);
1166                         pAd->Mlme.AssocMachine.CurrState = ASSOC_IDLE;
1167
1168                         RtmpOSWrielessEventSend(pAd, SIOCGIWAP, -1, NULL, NULL,
1169                                                 0);
1170                 }
1171         } else {
1172                 DBGPRINT(RT_DEBUG_TRACE,
1173                          ("ASSOC - PeerDisassocAction() sanity check fail\n"));
1174         }
1175
1176 }
1177
1178 /*
1179         ==========================================================================
1180         Description:
1181                 what the state machine will do after assoc timeout
1182         Parameters:
1183                 Elme -
1184
1185         IRQL = DISPATCH_LEVEL
1186
1187         ==========================================================================
1188  */
1189 void AssocTimeoutAction(struct rt_rtmp_adapter *pAd, struct rt_mlme_queue_elem *Elem)
1190 {
1191         u16 Status;
1192         DBGPRINT(RT_DEBUG_TRACE, ("ASSOC - AssocTimeoutAction\n"));
1193         pAd->Mlme.AssocMachine.CurrState = ASSOC_IDLE;
1194         Status = MLME_REJ_TIMEOUT;
1195         MlmeEnqueue(pAd, MLME_CNTL_STATE_MACHINE, MT2_ASSOC_CONF, 2, &Status);
1196 }
1197
1198 /*
1199         ==========================================================================
1200         Description:
1201                 what the state machine will do after reassoc timeout
1202
1203         IRQL = DISPATCH_LEVEL
1204
1205         ==========================================================================
1206  */
1207 void ReassocTimeoutAction(struct rt_rtmp_adapter *pAd, struct rt_mlme_queue_elem *Elem)
1208 {
1209         u16 Status;
1210         DBGPRINT(RT_DEBUG_TRACE, ("ASSOC - ReassocTimeoutAction\n"));
1211         pAd->Mlme.AssocMachine.CurrState = ASSOC_IDLE;
1212         Status = MLME_REJ_TIMEOUT;
1213         MlmeEnqueue(pAd, MLME_CNTL_STATE_MACHINE, MT2_REASSOC_CONF, 2, &Status);
1214 }
1215
1216 /*
1217         ==========================================================================
1218         Description:
1219                 what the state machine will do after disassoc timeout
1220
1221         IRQL = DISPATCH_LEVEL
1222
1223         ==========================================================================
1224  */
1225 void DisassocTimeoutAction(struct rt_rtmp_adapter *pAd, struct rt_mlme_queue_elem *Elem)
1226 {
1227         u16 Status;
1228         DBGPRINT(RT_DEBUG_TRACE, ("ASSOC - DisassocTimeoutAction\n"));
1229         pAd->Mlme.AssocMachine.CurrState = ASSOC_IDLE;
1230         Status = MLME_SUCCESS;
1231         MlmeEnqueue(pAd, MLME_CNTL_STATE_MACHINE, MT2_DISASSOC_CONF, 2,
1232                     &Status);
1233 }
1234
1235 void InvalidStateWhenAssoc(struct rt_rtmp_adapter *pAd, struct rt_mlme_queue_elem *Elem)
1236 {
1237         u16 Status;
1238         DBGPRINT(RT_DEBUG_TRACE,
1239                  ("ASSOC - InvalidStateWhenAssoc(state=%ld), reset ASSOC state machine\n",
1240                   pAd->Mlme.AssocMachine.CurrState));
1241         pAd->Mlme.AssocMachine.CurrState = ASSOC_IDLE;
1242         Status = MLME_STATE_MACHINE_REJECT;
1243         MlmeEnqueue(pAd, MLME_CNTL_STATE_MACHINE, MT2_ASSOC_CONF, 2, &Status);
1244 }
1245
1246 void InvalidStateWhenReassoc(struct rt_rtmp_adapter *pAd, struct rt_mlme_queue_elem *Elem)
1247 {
1248         u16 Status;
1249         DBGPRINT(RT_DEBUG_TRACE,
1250                  ("ASSOC - InvalidStateWhenReassoc(state=%ld), reset ASSOC state machine\n",
1251                   pAd->Mlme.AssocMachine.CurrState));
1252         pAd->Mlme.AssocMachine.CurrState = ASSOC_IDLE;
1253         Status = MLME_STATE_MACHINE_REJECT;
1254         MlmeEnqueue(pAd, MLME_CNTL_STATE_MACHINE, MT2_REASSOC_CONF, 2, &Status);
1255 }
1256
1257 void InvalidStateWhenDisassociate(struct rt_rtmp_adapter *pAd,
1258                                   struct rt_mlme_queue_elem *Elem)
1259 {
1260         u16 Status;
1261         DBGPRINT(RT_DEBUG_TRACE,
1262                  ("ASSOC - InvalidStateWhenDisassoc(state=%ld), reset ASSOC state machine\n",
1263                   pAd->Mlme.AssocMachine.CurrState));
1264         pAd->Mlme.AssocMachine.CurrState = ASSOC_IDLE;
1265         Status = MLME_STATE_MACHINE_REJECT;
1266         MlmeEnqueue(pAd, MLME_CNTL_STATE_MACHINE, MT2_DISASSOC_CONF, 2,
1267                     &Status);
1268 }
1269
1270 /*
1271         ==========================================================================
1272         Description:
1273                 right part of IEEE 802.11/1999 page 374
1274         Note:
1275                 This event should never cause ASSOC state machine perform state
1276                 transition, and has no relationship with CNTL machine. So we separate
1277                 this routine as a service outside of ASSOC state transition table.
1278
1279         IRQL = DISPATCH_LEVEL
1280
1281         ==========================================================================
1282  */
1283 void Cls3errAction(struct rt_rtmp_adapter *pAd, u8 *pAddr)
1284 {
1285         struct rt_header_802_11 DisassocHdr;
1286         struct rt_header_802_11 * pDisassocHdr;
1287         u8 *pOutBuffer = NULL;
1288         unsigned long FrameLen = 0;
1289         int NStatus;
1290         u16 Reason = REASON_CLS3ERR;
1291
1292         NStatus = MlmeAllocateMemory(pAd, &pOutBuffer); /*Get an unused nonpaged memory */
1293         if (NStatus != NDIS_STATUS_SUCCESS)
1294                 return;
1295
1296         DBGPRINT(RT_DEBUG_TRACE,
1297                  ("ASSOC - Class 3 Error, Send DISASSOC frame\n"));
1298         MgtMacHeaderInit(pAd, &DisassocHdr, SUBTYPE_DISASSOC, 0, pAddr, pAd->CommonCfg.Bssid);  /* patch peap ttls switching issue */
1299         MakeOutgoingFrame(pOutBuffer, &FrameLen,
1300                           sizeof(struct rt_header_802_11), &DisassocHdr,
1301                           2, &Reason, END_OF_ARGS);
1302         MiniportMMRequest(pAd, 0, pOutBuffer, FrameLen);
1303
1304         /* To patch Instance and Buffalo(N) AP */
1305         /* Driver has to send deauth to Instance AP, but Buffalo(N) needs to send disassoc to reset Authenticator's state machine */
1306         /* Therefore, we send both of them. */
1307         pDisassocHdr = (struct rt_header_802_11 *) pOutBuffer;
1308         pDisassocHdr->FC.SubType = SUBTYPE_DEAUTH;
1309         MiniportMMRequest(pAd, 0, pOutBuffer, FrameLen);
1310
1311         MlmeFreeMemory(pAd, pOutBuffer);
1312
1313         pAd->StaCfg.DisassocReason = REASON_CLS3ERR;
1314         COPY_MAC_ADDR(pAd->StaCfg.DisassocSta, pAddr);
1315 }
1316
1317 int wext_notify_event_assoc(struct rt_rtmp_adapter *pAd)
1318 {
1319         char custom[IW_CUSTOM_MAX] = { 0 };
1320
1321         if (pAd->StaCfg.ReqVarIELen <= IW_CUSTOM_MAX) {
1322                 NdisMoveMemory(custom, pAd->StaCfg.ReqVarIEs,
1323                                pAd->StaCfg.ReqVarIELen);
1324                 RtmpOSWrielessEventSend(pAd, IWEVASSOCREQIE, -1, NULL, custom,
1325                                         pAd->StaCfg.ReqVarIELen);
1326         } else
1327                 DBGPRINT(RT_DEBUG_TRACE,
1328                          ("pAd->StaCfg.ReqVarIELen > MAX_CUSTOM_LEN\n"));
1329
1330         return 0;
1331
1332 }
1333
1334 BOOLEAN StaAddMacTableEntry(struct rt_rtmp_adapter *pAd,
1335                             struct rt_mac_table_entry *pEntry,
1336                             u8 MaxSupportedRateIn500Kbps,
1337                             struct rt_ht_capability_ie * pHtCapability,
1338                             u8 HtCapabilityLen,
1339                             struct rt_add_ht_info_ie * pAddHtInfo,
1340                             u8 AddHtInfoLen, u16 CapabilityInfo)
1341 {
1342         u8 MaxSupportedRate = RATE_11;
1343
1344         if (ADHOC_ON(pAd))
1345                 CLIENT_STATUS_CLEAR_FLAG(pEntry, fCLIENT_STATUS_WMM_CAPABLE);
1346
1347         switch (MaxSupportedRateIn500Kbps) {
1348         case 108:
1349                 MaxSupportedRate = RATE_54;
1350                 break;
1351         case 96:
1352                 MaxSupportedRate = RATE_48;
1353                 break;
1354         case 72:
1355                 MaxSupportedRate = RATE_36;
1356                 break;
1357         case 48:
1358                 MaxSupportedRate = RATE_24;
1359                 break;
1360         case 36:
1361                 MaxSupportedRate = RATE_18;
1362                 break;
1363         case 24:
1364                 MaxSupportedRate = RATE_12;
1365                 break;
1366         case 18:
1367                 MaxSupportedRate = RATE_9;
1368                 break;
1369         case 12:
1370                 MaxSupportedRate = RATE_6;
1371                 break;
1372         case 22:
1373                 MaxSupportedRate = RATE_11;
1374                 break;
1375         case 11:
1376                 MaxSupportedRate = RATE_5_5;
1377                 break;
1378         case 4:
1379                 MaxSupportedRate = RATE_2;
1380                 break;
1381         case 2:
1382                 MaxSupportedRate = RATE_1;
1383                 break;
1384         default:
1385                 MaxSupportedRate = RATE_11;
1386                 break;
1387         }
1388
1389         if ((pAd->CommonCfg.PhyMode == PHY_11G)
1390             && (MaxSupportedRate < RATE_FIRST_OFDM_RATE))
1391                 return FALSE;
1392
1393         /* 11n only */
1394         if (((pAd->CommonCfg.PhyMode == PHY_11N_2_4G)
1395              || (pAd->CommonCfg.PhyMode == PHY_11N_5G))
1396             && (HtCapabilityLen == 0))
1397                 return FALSE;
1398
1399         if (!pEntry)
1400                 return FALSE;
1401
1402         NdisAcquireSpinLock(&pAd->MacTabLock);
1403         if (pEntry) {
1404                 pEntry->PortSecured = WPA_802_1X_PORT_SECURED;
1405                 if ((MaxSupportedRate < RATE_FIRST_OFDM_RATE) ||
1406                     (pAd->CommonCfg.PhyMode == PHY_11B)) {
1407                         pEntry->RateLen = 4;
1408                         if (MaxSupportedRate >= RATE_FIRST_OFDM_RATE)
1409                                 MaxSupportedRate = RATE_11;
1410                 } else
1411                         pEntry->RateLen = 12;
1412
1413                 pEntry->MaxHTPhyMode.word = 0;
1414                 pEntry->MinHTPhyMode.word = 0;
1415                 pEntry->HTPhyMode.word = 0;
1416                 pEntry->MaxSupportedRate = MaxSupportedRate;
1417                 if (pEntry->MaxSupportedRate < RATE_FIRST_OFDM_RATE) {
1418                         pEntry->MaxHTPhyMode.field.MODE = MODE_CCK;
1419                         pEntry->MaxHTPhyMode.field.MCS =
1420                             pEntry->MaxSupportedRate;
1421                         pEntry->MinHTPhyMode.field.MODE = MODE_CCK;
1422                         pEntry->MinHTPhyMode.field.MCS =
1423                             pEntry->MaxSupportedRate;
1424                         pEntry->HTPhyMode.field.MODE = MODE_CCK;
1425                         pEntry->HTPhyMode.field.MCS = pEntry->MaxSupportedRate;
1426                 } else {
1427                         pEntry->MaxHTPhyMode.field.MODE = MODE_OFDM;
1428                         pEntry->MaxHTPhyMode.field.MCS =
1429                             OfdmRateToRxwiMCS[pEntry->MaxSupportedRate];
1430                         pEntry->MinHTPhyMode.field.MODE = MODE_OFDM;
1431                         pEntry->MinHTPhyMode.field.MCS =
1432                             OfdmRateToRxwiMCS[pEntry->MaxSupportedRate];
1433                         pEntry->HTPhyMode.field.MODE = MODE_OFDM;
1434                         pEntry->HTPhyMode.field.MCS =
1435                             OfdmRateToRxwiMCS[pEntry->MaxSupportedRate];
1436                 }
1437                 pEntry->CapabilityInfo = CapabilityInfo;
1438                 CLIENT_STATUS_CLEAR_FLAG(pEntry,
1439                                          fCLIENT_STATUS_AGGREGATION_CAPABLE);
1440                 CLIENT_STATUS_CLEAR_FLAG(pEntry,
1441                                          fCLIENT_STATUS_PIGGYBACK_CAPABLE);
1442         }
1443
1444         NdisZeroMemory(&pEntry->HTCapability, sizeof(pEntry->HTCapability));
1445         /* If this Entry supports 802.11n, upgrade to HT rate. */
1446         if ((HtCapabilityLen != 0)
1447             && (pAd->CommonCfg.PhyMode >= PHY_11ABGN_MIXED)) {
1448                 u8 j, bitmask;  /*k,bitmask; */
1449                 char i;
1450
1451                 if (ADHOC_ON(pAd))
1452                         CLIENT_STATUS_SET_FLAG(pEntry,
1453                                                fCLIENT_STATUS_WMM_CAPABLE);
1454                 if ((pHtCapability->HtCapInfo.GF)
1455                     && (pAd->CommonCfg.DesiredHtPhy.GF)) {
1456                         pEntry->MaxHTPhyMode.field.MODE = MODE_HTGREENFIELD;
1457                 } else {
1458                         pEntry->MaxHTPhyMode.field.MODE = MODE_HTMIX;
1459                         pAd->MacTab.fAnyStationNonGF = TRUE;
1460                         pAd->CommonCfg.AddHTInfo.AddHtInfo2.NonGfPresent = 1;
1461                 }
1462
1463                 if ((pHtCapability->HtCapInfo.ChannelWidth) &&
1464                     (pAd->CommonCfg.DesiredHtPhy.ChannelWidth) &&
1465                     ((pAd->StaCfg.BssType == BSS_INFRA)
1466                      || ((pAd->StaCfg.BssType == BSS_ADHOC)
1467                          && (pAddHtInfo->AddHtInfo.ExtChanOffset ==
1468                              pAd->CommonCfg.AddHTInfo.AddHtInfo.
1469                              ExtChanOffset)))) {
1470                         pEntry->MaxHTPhyMode.field.BW = BW_40;
1471                         pEntry->MaxHTPhyMode.field.ShortGI =
1472                             ((pAd->CommonCfg.DesiredHtPhy.
1473                               ShortGIfor40) & (pHtCapability->HtCapInfo.
1474                                                ShortGIfor40));
1475                 } else {
1476                         pEntry->MaxHTPhyMode.field.BW = BW_20;
1477                         pEntry->MaxHTPhyMode.field.ShortGI =
1478                             ((pAd->CommonCfg.DesiredHtPhy.
1479                               ShortGIfor20) & (pHtCapability->HtCapInfo.
1480                                                ShortGIfor20));
1481                         pAd->MacTab.fAnyStation20Only = TRUE;
1482                 }
1483
1484                 /* 3*3 */
1485                 if (pAd->MACVersion >= RALINK_2883_VERSION
1486                     && pAd->MACVersion < RALINK_3070_VERSION)
1487                         pEntry->MaxHTPhyMode.field.TxBF =
1488                             pAd->CommonCfg.RegTransmitSetting.field.TxBF;
1489
1490                 /* find max fixed rate */
1491                 for (i = 23; i >= 0; i--)       /* 3*3 */
1492                 {
1493                         j = i / 8;
1494                         bitmask = (1 << (i - (j * 8)));
1495                         if ((pAd->StaCfg.DesiredHtPhyInfo.MCSSet[j] & bitmask)
1496                             && (pHtCapability->MCSSet[j] & bitmask)) {
1497                                 pEntry->MaxHTPhyMode.field.MCS = i;
1498                                 break;
1499                         }
1500                         if (i == 0)
1501                                 break;
1502                 }
1503
1504                 if (pAd->StaCfg.DesiredTransmitSetting.field.MCS != MCS_AUTO) {
1505                         if (pAd->StaCfg.DesiredTransmitSetting.field.MCS == 32) {
1506                                 /* Fix MCS as HT Duplicated Mode */
1507                                 pEntry->MaxHTPhyMode.field.BW = 1;
1508                                 pEntry->MaxHTPhyMode.field.MODE = MODE_HTMIX;
1509                                 pEntry->MaxHTPhyMode.field.STBC = 0;
1510                                 pEntry->MaxHTPhyMode.field.ShortGI = 0;
1511                                 pEntry->MaxHTPhyMode.field.MCS = 32;
1512                         } else if (pEntry->MaxHTPhyMode.field.MCS >
1513                                    pAd->StaCfg.HTPhyMode.field.MCS) {
1514                                 /* STA supports fixed MCS */
1515                                 pEntry->MaxHTPhyMode.field.MCS =
1516                                     pAd->StaCfg.HTPhyMode.field.MCS;
1517                         }
1518                 }
1519
1520                 pEntry->MaxHTPhyMode.field.STBC =
1521                     (pHtCapability->HtCapInfo.
1522                      RxSTBC & (pAd->CommonCfg.DesiredHtPhy.TxSTBC));
1523                 pEntry->MpduDensity = pHtCapability->HtCapParm.MpduDensity;
1524                 pEntry->MaxRAmpduFactor =
1525                     pHtCapability->HtCapParm.MaxRAmpduFactor;
1526                 pEntry->MmpsMode = (u8)pHtCapability->HtCapInfo.MimoPs;
1527                 pEntry->AMsduSize = (u8)pHtCapability->HtCapInfo.AMsduSize;
1528                 pEntry->HTPhyMode.word = pEntry->MaxHTPhyMode.word;
1529
1530                 if (pAd->CommonCfg.DesiredHtPhy.AmsduEnable
1531                     && (pAd->CommonCfg.REGBACapability.field.AutoBA == FALSE))
1532                         CLIENT_STATUS_SET_FLAG(pEntry,
1533                                                fCLIENT_STATUS_AMSDU_INUSED);
1534                 if (pHtCapability->HtCapInfo.ShortGIfor20)
1535                         CLIENT_STATUS_SET_FLAG(pEntry,
1536                                                fCLIENT_STATUS_SGI20_CAPABLE);
1537                 if (pHtCapability->HtCapInfo.ShortGIfor40)
1538                         CLIENT_STATUS_SET_FLAG(pEntry,
1539                                                fCLIENT_STATUS_SGI40_CAPABLE);
1540                 if (pHtCapability->HtCapInfo.TxSTBC)
1541                         CLIENT_STATUS_SET_FLAG(pEntry,
1542                                                fCLIENT_STATUS_TxSTBC_CAPABLE);
1543                 if (pHtCapability->HtCapInfo.RxSTBC)
1544                         CLIENT_STATUS_SET_FLAG(pEntry,
1545                                                fCLIENT_STATUS_RxSTBC_CAPABLE);
1546                 if (pHtCapability->ExtHtCapInfo.PlusHTC)
1547                         CLIENT_STATUS_SET_FLAG(pEntry,
1548                                                fCLIENT_STATUS_HTC_CAPABLE);
1549                 if (pAd->CommonCfg.bRdg
1550                     && pHtCapability->ExtHtCapInfo.RDGSupport)
1551                         CLIENT_STATUS_SET_FLAG(pEntry,
1552                                                fCLIENT_STATUS_RDG_CAPABLE);
1553                 if (pHtCapability->ExtHtCapInfo.MCSFeedback == 0x03)
1554                         CLIENT_STATUS_SET_FLAG(pEntry,
1555                                                fCLIENT_STATUS_MCSFEEDBACK_CAPABLE);
1556                 NdisMoveMemory(&pEntry->HTCapability, pHtCapability,
1557                                HtCapabilityLen);
1558         } else {
1559                 pAd->MacTab.fAnyStationIsLegacy = TRUE;
1560         }
1561
1562         pEntry->HTPhyMode.word = pEntry->MaxHTPhyMode.word;
1563         pEntry->CurrTxRate = pEntry->MaxSupportedRate;
1564
1565         /* Set asic auto fall back */
1566         if (pAd->StaCfg.bAutoTxRateSwitch == TRUE) {
1567                 u8 *pTable;
1568                 u8 TableSize = 0;
1569
1570                 MlmeSelectTxRateTable(pAd, pEntry, &pTable, &TableSize,
1571                                       &pEntry->CurrTxRateIndex);
1572                 pEntry->bAutoTxRateSwitch = TRUE;
1573         } else {
1574                 pEntry->HTPhyMode.field.MODE = pAd->StaCfg.HTPhyMode.field.MODE;
1575                 pEntry->HTPhyMode.field.MCS = pAd->StaCfg.HTPhyMode.field.MCS;
1576                 pEntry->bAutoTxRateSwitch = FALSE;
1577
1578                 /* If the legacy mode is set, overwrite the transmit setting of this entry. */
1579                 RTMPUpdateLegacyTxSetting((u8)pAd->StaCfg.
1580                                           DesiredTransmitSetting.field.
1581                                           FixedTxMode, pEntry);
1582         }
1583
1584         pEntry->PortSecured = WPA_802_1X_PORT_SECURED;
1585         pEntry->Sst = SST_ASSOC;
1586         pEntry->AuthState = AS_AUTH_OPEN;
1587         pEntry->AuthMode = pAd->StaCfg.AuthMode;
1588         pEntry->WepStatus = pAd->StaCfg.WepStatus;
1589
1590         NdisReleaseSpinLock(&pAd->MacTabLock);
1591
1592         {
1593                 union iwreq_data wrqu;
1594                 wext_notify_event_assoc(pAd);
1595
1596                 memcpy(wrqu.ap_addr.sa_data, pAd->MlmeAux.Bssid, MAC_ADDR_LEN);
1597                 wireless_send_event(pAd->net_dev, SIOCGIWAP, &wrqu, NULL);
1598
1599         }
1600         return TRUE;
1601 }