staging: vt6655: remove dead code
[firefly-linux-kernel-4.4.55.git] / drivers / staging / vt6655 / dpc.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  * File: dpc.c
20  *
21  * Purpose: handle dpc rx functions
22  *
23  * Author: Lyndon Chen
24  *
25  * Date: May 20, 2003
26  *
27  * Functions:
28  *      device_receive_frame - Rcv 802.11 frame function
29  *      s_bAPModeRxCtl- AP Rcv frame filer Ctl.
30  *      s_bAPModeRxData- AP Rcv data frame handle
31  *      s_bHandleRxEncryption- Rcv decrypted data via on-fly
32  *      s_bHostWepRxEncryption- Rcv encrypted data via host
33  *      s_byGetRateIdx- get rate index
34  *      s_vGetDASA- get data offset
35  *      s_vProcessRxMACHeader- Rcv 802.11 and translate to 802.3
36  *
37  * Revision History:
38  *
39  */
40
41 #include "device.h"
42 #include "rxtx.h"
43 #include "tether.h"
44 #include "card.h"
45 #include "bssdb.h"
46 #include "mac.h"
47 #include "baseband.h"
48 #include "michael.h"
49 #include "tkip.h"
50 #include "tcrc.h"
51 #include "wctl.h"
52 #include "wroute.h"
53 #include "hostap.h"
54 #include "rf.h"
55 #include "iowpa.h"
56 #include "aes_ccmp.h"
57
58 /*---------------------  Static Definitions -------------------------*/
59
60 /*---------------------  Static Classes  ----------------------------*/
61
62 /*---------------------  Static Variables  --------------------------*/
63 static int msglevel = MSG_LEVEL_INFO;
64
65 const unsigned char acbyRxRate[MAX_RATE] =
66 {2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108};
67
68 /*---------------------  Static Functions  --------------------------*/
69
70 /*---------------------  Static Definitions -------------------------*/
71
72 /*---------------------  Static Functions  --------------------------*/
73
74 static unsigned char s_byGetRateIdx(unsigned char byRate);
75
76 static void
77 s_vGetDASA(unsigned char *pbyRxBufferAddr, unsigned int *pcbHeaderSize,
78            PSEthernetHeader psEthHeader);
79
80 static void
81 s_vProcessRxMACHeader(PSDevice pDevice, unsigned char *pbyRxBufferAddr,
82                       unsigned int cbPacketSize, bool bIsWEP, bool bExtIV,
83                       unsigned int *pcbHeadSize);
84
85 static bool s_bAPModeRxCtl(
86         PSDevice pDevice,
87         unsigned char *pbyFrame,
88         int      iSANodeIndex
89 );
90
91 static bool s_bAPModeRxData(
92         PSDevice pDevice,
93         struct sk_buff *skb,
94         unsigned int FrameSize,
95         unsigned int cbHeaderOffset,
96         int      iSANodeIndex,
97         int      iDANodeIndex
98 );
99
100 static bool s_bHandleRxEncryption(
101         PSDevice     pDevice,
102         unsigned char *pbyFrame,
103         unsigned int FrameSize,
104         unsigned char *pbyRsr,
105         unsigned char *pbyNewRsr,
106         PSKeyItem   *pKeyOut,
107         bool *pbExtIV,
108         unsigned short *pwRxTSC15_0,
109         unsigned long *pdwRxTSC47_16
110 );
111
112 static bool s_bHostWepRxEncryption(
113
114         PSDevice     pDevice,
115         unsigned char *pbyFrame,
116         unsigned int FrameSize,
117         unsigned char *pbyRsr,
118         bool bOnFly,
119         PSKeyItem    pKey,
120         unsigned char *pbyNewRsr,
121         bool *pbExtIV,
122         unsigned short *pwRxTSC15_0,
123         unsigned long *pdwRxTSC47_16
124
125 );
126
127 /*---------------------  Export Variables  --------------------------*/
128
129 /*+
130  *
131  * Description:
132  *    Translate Rcv 802.11 header to 802.3 header with Rx buffer
133  *
134  * Parameters:
135  *  In:
136  *      pDevice
137  *      dwRxBufferAddr  - Address of Rcv Buffer
138  *      cbPacketSize    - Rcv Packet size
139  *      bIsWEP          - If Rcv with WEP
140  *  Out:
141  *      pcbHeaderSize   - 802.11 header size
142  *
143  * Return Value: None
144  *
145  -*/
146 static void
147 s_vProcessRxMACHeader(PSDevice pDevice, unsigned char *pbyRxBufferAddr,
148                       unsigned int cbPacketSize, bool bIsWEP, bool bExtIV,
149                       unsigned int *pcbHeadSize)
150 {
151         unsigned char *pbyRxBuffer;
152         unsigned int cbHeaderSize = 0;
153         unsigned short *pwType;
154         PS802_11Header  pMACHeader;
155         int             ii;
156
157         pMACHeader = (PS802_11Header) (pbyRxBufferAddr + cbHeaderSize);
158
159         s_vGetDASA((unsigned char *)pMACHeader, &cbHeaderSize, &pDevice->sRxEthHeader);
160
161         if (bIsWEP) {
162                 if (bExtIV) {
163                         // strip IV&ExtIV , add 8 byte
164                         cbHeaderSize += (WLAN_HDR_ADDR3_LEN + 8);
165                 } else {
166                         // strip IV , add 4 byte
167                         cbHeaderSize += (WLAN_HDR_ADDR3_LEN + 4);
168                 }
169         } else {
170                 cbHeaderSize += WLAN_HDR_ADDR3_LEN;
171         }
172
173         pbyRxBuffer = (unsigned char *)(pbyRxBufferAddr + cbHeaderSize);
174         if (ether_addr_equal(pbyRxBuffer, pDevice->abySNAP_Bridgetunnel)) {
175                 cbHeaderSize += 6;
176         } else if (ether_addr_equal(pbyRxBuffer, pDevice->abySNAP_RFC1042)) {
177                 cbHeaderSize += 6;
178                 pwType = (unsigned short *)(pbyRxBufferAddr + cbHeaderSize);
179                 if ((*pwType != TYPE_PKT_IPX) && (*pwType != cpu_to_le16(0xF380))) {
180                 } else {
181                         cbHeaderSize -= 8;
182                         pwType = (unsigned short *)(pbyRxBufferAddr + cbHeaderSize);
183                         if (bIsWEP) {
184                                 if (bExtIV) {
185                                         *pwType = htons(cbPacketSize - WLAN_HDR_ADDR3_LEN - 8);    // 8 is IV&ExtIV
186                                 } else {
187                                         *pwType = htons(cbPacketSize - WLAN_HDR_ADDR3_LEN - 4);    // 4 is IV
188                                 }
189                         } else {
190                                 *pwType = htons(cbPacketSize - WLAN_HDR_ADDR3_LEN);
191                         }
192                 }
193         } else {
194                 cbHeaderSize -= 2;
195                 pwType = (unsigned short *)(pbyRxBufferAddr + cbHeaderSize);
196                 if (bIsWEP) {
197                         if (bExtIV) {
198                                 *pwType = htons(cbPacketSize - WLAN_HDR_ADDR3_LEN - 8);    // 8 is IV&ExtIV
199                         } else {
200                                 *pwType = htons(cbPacketSize - WLAN_HDR_ADDR3_LEN - 4);    // 4 is IV
201                         }
202                 } else {
203                         *pwType = htons(cbPacketSize - WLAN_HDR_ADDR3_LEN);
204                 }
205         }
206
207         cbHeaderSize -= (ETH_ALEN * 2);
208         pbyRxBuffer = (unsigned char *)(pbyRxBufferAddr + cbHeaderSize);
209         for (ii = 0; ii < ETH_ALEN; ii++)
210                 *pbyRxBuffer++ = pDevice->sRxEthHeader.abyDstAddr[ii];
211         for (ii = 0; ii < ETH_ALEN; ii++)
212                 *pbyRxBuffer++ = pDevice->sRxEthHeader.abySrcAddr[ii];
213
214         *pcbHeadSize = cbHeaderSize;
215 }
216
217 static unsigned char s_byGetRateIdx(unsigned char byRate)
218 {
219         unsigned char byRateIdx;
220
221         for (byRateIdx = 0; byRateIdx < MAX_RATE; byRateIdx++) {
222                 if (acbyRxRate[byRateIdx % MAX_RATE] == byRate)
223                         return byRateIdx;
224         }
225         return 0;
226 }
227
228 static void
229 s_vGetDASA(unsigned char *pbyRxBufferAddr, unsigned int *pcbHeaderSize,
230            PSEthernetHeader psEthHeader)
231 {
232         unsigned int cbHeaderSize = 0;
233         PS802_11Header  pMACHeader;
234         int             ii;
235
236         pMACHeader = (PS802_11Header) (pbyRxBufferAddr + cbHeaderSize);
237
238         if ((pMACHeader->wFrameCtl & FC_TODS) == 0) {
239                 if (pMACHeader->wFrameCtl & FC_FROMDS) {
240                         for (ii = 0; ii < ETH_ALEN; ii++) {
241                                 psEthHeader->abyDstAddr[ii] = pMACHeader->abyAddr1[ii];
242                                 psEthHeader->abySrcAddr[ii] = pMACHeader->abyAddr3[ii];
243                         }
244                 } else {
245                         // IBSS mode
246                         for (ii = 0; ii < ETH_ALEN; ii++) {
247                                 psEthHeader->abyDstAddr[ii] = pMACHeader->abyAddr1[ii];
248                                 psEthHeader->abySrcAddr[ii] = pMACHeader->abyAddr2[ii];
249                         }
250                 }
251         } else {
252                 // Is AP mode..
253                 if (pMACHeader->wFrameCtl & FC_FROMDS) {
254                         for (ii = 0; ii < ETH_ALEN; ii++) {
255                                 psEthHeader->abyDstAddr[ii] = pMACHeader->abyAddr3[ii];
256                                 psEthHeader->abySrcAddr[ii] = pMACHeader->abyAddr4[ii];
257                                 cbHeaderSize += 6;
258                         }
259                 } else {
260                         for (ii = 0; ii < ETH_ALEN; ii++) {
261                                 psEthHeader->abyDstAddr[ii] = pMACHeader->abyAddr3[ii];
262                                 psEthHeader->abySrcAddr[ii] = pMACHeader->abyAddr2[ii];
263                         }
264                 }
265         }
266         *pcbHeaderSize = cbHeaderSize;
267 }
268
269 //PLICE_DEBUG ->
270
271 void    MngWorkItem(void *Context)
272 {
273         PSRxMgmtPacket                  pRxMgmtPacket;
274         PSDevice        pDevice =  (PSDevice) Context;
275
276         spin_lock_irq(&pDevice->lock);
277         while (pDevice->rxManeQueue.packet_num != 0) {
278                 pRxMgmtPacket =  DeQueue(pDevice);
279                 vMgrRxManagePacket(pDevice, pDevice->pMgmt, pRxMgmtPacket);
280         }
281         spin_unlock_irq(&pDevice->lock);
282 }
283
284 //PLICE_DEBUG<-
285
286 bool
287 device_receive_frame(
288         PSDevice pDevice,
289         PSRxDesc pCurrRD
290 )
291 {
292         PDEVICE_RD_INFO  pRDInfo = pCurrRD->pRDInfo;
293         struct net_device_stats *pStats = &pDevice->stats;
294         struct sk_buff *skb;
295         PSMgmtObject    pMgmt = pDevice->pMgmt;
296         PSRxMgmtPacket  pRxPacket = &(pDevice->pMgmt->sRxPacket);
297         PS802_11Header  p802_11Header;
298         unsigned char *pbyRsr;
299         unsigned char *pbyNewRsr;
300         unsigned char *pbyRSSI;
301         PQWORD          pqwTSFTime;
302         unsigned short *pwFrameSize;
303         unsigned char *pbyFrame;
304         bool bDeFragRx = false;
305         bool bIsWEP = false;
306         unsigned int cbHeaderOffset;
307         unsigned int FrameSize;
308         unsigned short wEtherType = 0;
309         int             iSANodeIndex = -1;
310         int             iDANodeIndex = -1;
311         unsigned int ii;
312         unsigned int cbIVOffset;
313         bool bExtIV = false;
314         unsigned char *pbyRxSts;
315         unsigned char *pbyRxRate;
316         unsigned char *pbySQ;
317         unsigned int cbHeaderSize;
318         PSKeyItem       pKey = NULL;
319         unsigned short wRxTSC15_0 = 0;
320         unsigned long dwRxTSC47_16 = 0;
321         SKeyItem        STempKey;
322         // 802.11h RPI
323         unsigned long dwDuration = 0;
324         long            ldBm = 0;
325         long            ldBmThreshold = 0;
326         PS802_11Header pMACHeader;
327         bool bRxeapol_key = false;
328
329         skb = pRDInfo->skb;
330
331 //PLICE_DEBUG->
332         pci_unmap_single(pDevice->pcid, pRDInfo->skb_dma,
333                          pDevice->rx_buf_sz, PCI_DMA_FROMDEVICE);
334 //PLICE_DEBUG<-
335         pwFrameSize = (unsigned short *)(skb->data + 2);
336         FrameSize = cpu_to_le16(pCurrRD->m_rd1RD1.wReqCount) - cpu_to_le16(pCurrRD->m_rd0RD0.wResCount);
337
338         // Max: 2312Payload + 30HD +4CRC + 2Padding + 4Len + 8TSF + 4RSR
339         // Min (ACK): 10HD +4CRC + 2Padding + 4Len + 8TSF + 4RSR
340         if ((FrameSize > 2364) || (FrameSize <= 32)) {
341                 // Frame Size error drop this packet.
342                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "---------- WRONG Length 1 \n");
343                 return false;
344         }
345
346         pbyRxSts = (unsigned char *)(skb->data);
347         pbyRxRate = (unsigned char *)(skb->data + 1);
348         pbyRsr = (unsigned char *)(skb->data + FrameSize - 1);
349         pbyRSSI = (unsigned char *)(skb->data + FrameSize - 2);
350         pbyNewRsr = (unsigned char *)(skb->data + FrameSize - 3);
351         pbySQ = (unsigned char *)(skb->data + FrameSize - 4);
352         pqwTSFTime = (PQWORD)(skb->data + FrameSize - 12);
353         pbyFrame = (unsigned char *)(skb->data + 4);
354
355         // get packet size
356         FrameSize = cpu_to_le16(*pwFrameSize);
357
358         if ((FrameSize > 2346)|(FrameSize < 14)) { // Max: 2312Payload + 30HD +4CRC
359                 // Min: 14 bytes ACK
360                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "---------- WRONG Length 2 \n");
361                 return false;
362         }
363 //PLICE_DEBUG->
364         // update receive statistic counter
365         STAvUpdateRDStatCounter(&pDevice->scStatistic,
366                                 *pbyRsr,
367                                 *pbyNewRsr,
368                                 *pbyRxRate,
369                                 pbyFrame,
370                                 FrameSize);
371
372         pMACHeader = (PS802_11Header)((unsigned char *)(skb->data) + 8);
373 //PLICE_DEBUG<-
374         if (pDevice->bMeasureInProgress) {
375                 if ((*pbyRsr & RSR_CRCOK) != 0) {
376                         pDevice->byBasicMap |= 0x01;
377                 }
378                 dwDuration = (FrameSize << 4);
379                 dwDuration /= acbyRxRate[*pbyRxRate%MAX_RATE];
380                 if (*pbyRxRate <= RATE_11M) {
381                         if (*pbyRxSts & 0x01) {
382                                 // long preamble
383                                 dwDuration += 192;
384                         } else {
385                                 // short preamble
386                                 dwDuration += 96;
387                         }
388                 } else {
389                         dwDuration += 16;
390                 }
391                 RFvRSSITodBm(pDevice, *pbyRSSI, &ldBm);
392                 ldBmThreshold = -57;
393                 for (ii = 7; ii > 0;) {
394                         if (ldBm > ldBmThreshold) {
395                                 break;
396                         }
397                         ldBmThreshold -= 5;
398                         ii--;
399                 }
400                 pDevice->dwRPIs[ii] += dwDuration;
401                 return false;
402         }
403
404         if (!is_multicast_ether_addr(pbyFrame)) {
405                 if (WCTLbIsDuplicate(&(pDevice->sDupRxCache), (PS802_11Header)(skb->data + 4))) {
406                         pDevice->s802_11Counter.FrameDuplicateCount++;
407                         return false;
408                 }
409         }
410
411         // Use for TKIP MIC
412         s_vGetDASA(skb->data+4, &cbHeaderSize, &pDevice->sRxEthHeader);
413
414         // filter packet send from myself
415         if (ether_addr_equal(pDevice->sRxEthHeader.abySrcAddr,
416                              pDevice->abyCurrentNetAddr))
417                 return false;
418
419         if ((pMgmt->eCurrMode == WMAC_MODE_ESS_AP) || (pMgmt->eCurrMode == WMAC_MODE_IBSS_STA)) {
420                 if (IS_CTL_PSPOLL(pbyFrame) || !IS_TYPE_CONTROL(pbyFrame)) {
421                         p802_11Header = (PS802_11Header)(pbyFrame);
422                         // get SA NodeIndex
423                         if (BSSDBbIsSTAInNodeDB(pMgmt, (unsigned char *)(p802_11Header->abyAddr2), &iSANodeIndex)) {
424                                 pMgmt->sNodeDBTable[iSANodeIndex].ulLastRxJiffer = jiffies;
425                                 pMgmt->sNodeDBTable[iSANodeIndex].uInActiveCount = 0;
426                         }
427                 }
428         }
429
430         if (pMgmt->eCurrMode == WMAC_MODE_ESS_AP) {
431                 if (s_bAPModeRxCtl(pDevice, pbyFrame, iSANodeIndex)) {
432                         return false;
433                 }
434         }
435
436         if (IS_FC_WEP(pbyFrame)) {
437                 bool bRxDecryOK = false;
438
439                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "rx WEP pkt\n");
440                 bIsWEP = true;
441                 if ((pDevice->bEnableHostWEP) && (iSANodeIndex >= 0)) {
442                         pKey = &STempKey;
443                         pKey->byCipherSuite = pMgmt->sNodeDBTable[iSANodeIndex].byCipherSuite;
444                         pKey->dwKeyIndex = pMgmt->sNodeDBTable[iSANodeIndex].dwKeyIndex;
445                         pKey->uKeyLength = pMgmt->sNodeDBTable[iSANodeIndex].uWepKeyLength;
446                         pKey->dwTSC47_16 = pMgmt->sNodeDBTable[iSANodeIndex].dwTSC47_16;
447                         pKey->wTSC15_0 = pMgmt->sNodeDBTable[iSANodeIndex].wTSC15_0;
448                         memcpy(pKey->abyKey,
449                                &pMgmt->sNodeDBTable[iSANodeIndex].abyWepKey[0],
450                                pKey->uKeyLength
451 );
452
453                         bRxDecryOK = s_bHostWepRxEncryption(pDevice,
454                                                             pbyFrame,
455                                                             FrameSize,
456                                                             pbyRsr,
457                                                             pMgmt->sNodeDBTable[iSANodeIndex].bOnFly,
458                                                             pKey,
459                                                             pbyNewRsr,
460                                                             &bExtIV,
461                                                             &wRxTSC15_0,
462                                                             &dwRxTSC47_16);
463                 } else {
464                         bRxDecryOK = s_bHandleRxEncryption(pDevice,
465                                                            pbyFrame,
466                                                            FrameSize,
467                                                            pbyRsr,
468                                                            pbyNewRsr,
469                                                            &pKey,
470                                                            &bExtIV,
471                                                            &wRxTSC15_0,
472                                                            &dwRxTSC47_16);
473                 }
474
475                 if (bRxDecryOK) {
476                         if ((*pbyNewRsr & NEWRSR_DECRYPTOK) == 0) {
477                                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "ICV Fail\n");
478                                 if ((pDevice->pMgmt->eAuthenMode == WMAC_AUTH_WPA) ||
479                                     (pDevice->pMgmt->eAuthenMode == WMAC_AUTH_WPAPSK) ||
480                                     (pDevice->pMgmt->eAuthenMode == WMAC_AUTH_WPANONE) ||
481                                     (pDevice->pMgmt->eAuthenMode == WMAC_AUTH_WPA2) ||
482                                     (pDevice->pMgmt->eAuthenMode == WMAC_AUTH_WPA2PSK)) {
483                                         if ((pKey != NULL) && (pKey->byCipherSuite == KEY_CTL_TKIP)) {
484                                                 pDevice->s802_11Counter.TKIPICVErrors++;
485                                         } else if ((pKey != NULL) && (pKey->byCipherSuite == KEY_CTL_CCMP)) {
486                                                 pDevice->s802_11Counter.CCMPDecryptErrors++;
487                                         }
488                                 }
489                                 return false;
490                         }
491                 } else {
492                         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "WEP Func Fail\n");
493                         return false;
494                 }
495                 if ((pKey != NULL) && (pKey->byCipherSuite == KEY_CTL_CCMP))
496                         FrameSize -= 8;         // Message Integrity Code
497                 else
498                         FrameSize -= 4;         // 4 is ICV
499         }
500
501         //
502         // RX OK
503         //
504         //remove the CRC length
505         FrameSize -= ETH_FCS_LEN;
506
507         if ((!(*pbyRsr & (RSR_ADDRBROAD | RSR_ADDRMULTI))) && // unicast address
508             (IS_FRAGMENT_PKT((skb->data+4)))
509 ) {
510                 // defragment
511                 bDeFragRx = WCTLbHandleFragment(pDevice, (PS802_11Header)(skb->data+4), FrameSize, bIsWEP, bExtIV);
512                 pDevice->s802_11Counter.ReceivedFragmentCount++;
513                 if (bDeFragRx) {
514                         // defrag complete
515                         skb = pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].skb;
516                         FrameSize = pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].cbFrameLength;
517
518                 } else {
519                         return false;
520                 }
521         }
522
523 // Management & Control frame Handle
524         if ((IS_TYPE_DATA((skb->data+4))) == false) {
525                 // Handle Control & Manage Frame
526
527                 if (IS_TYPE_MGMT((skb->data+4))) {
528                         unsigned char *pbyData1;
529                         unsigned char *pbyData2;
530
531                         pRxPacket->p80211Header = (PUWLAN_80211HDR)(skb->data+4);
532                         pRxPacket->cbMPDULen = FrameSize;
533                         pRxPacket->uRSSI = *pbyRSSI;
534                         pRxPacket->bySQ = *pbySQ;
535                         HIDWORD(pRxPacket->qwLocalTSF) = cpu_to_le32(HIDWORD(*pqwTSFTime));
536                         LODWORD(pRxPacket->qwLocalTSF) = cpu_to_le32(LODWORD(*pqwTSFTime));
537                         if (bIsWEP) {
538                                 // strip IV
539                                 pbyData1 = WLAN_HDR_A3_DATA_PTR(skb->data+4);
540                                 pbyData2 = WLAN_HDR_A3_DATA_PTR(skb->data+4) + 4;
541                                 for (ii = 0; ii < (FrameSize - 4); ii++) {
542                                         *pbyData1 = *pbyData2;
543                                         pbyData1++;
544                                         pbyData2++;
545                                 }
546                         }
547                         pRxPacket->byRxRate = s_byGetRateIdx(*pbyRxRate);
548                         pRxPacket->byRxChannel = (*pbyRxSts) >> 2;
549 //PLICE_DEBUG->
550
551 #ifdef  THREAD
552                         EnQueue(pDevice, pRxPacket);
553 #else
554
555 #ifdef  TASK_LET
556                         EnQueue(pDevice, pRxPacket);
557                         tasklet_schedule(&pDevice->RxMngWorkItem);
558 #else
559                         vMgrRxManagePacket((void *)pDevice, pDevice->pMgmt, pRxPacket);
560 #endif
561
562 #endif
563 //PLICE_DEBUG<-
564                         // hostap Deamon handle 802.11 management
565                         if (pDevice->bEnableHostapd) {
566                                 skb->dev = pDevice->apdev;
567                                 skb->data += 4;
568                                 skb->tail += 4;
569                                 skb_put(skb, FrameSize);
570                                 skb_reset_mac_header(skb);
571                                 skb->pkt_type = PACKET_OTHERHOST;
572                                 skb->protocol = htons(ETH_P_802_2);
573                                 memset(skb->cb, 0, sizeof(skb->cb));
574                                 netif_rx(skb);
575                                 return true;
576                         }
577                 }
578
579                 return false;
580         } else {
581                 if (pMgmt->eCurrMode == WMAC_MODE_ESS_AP) {
582                         //In AP mode, hw only check addr1(BSSID or RA) if equal to local MAC.
583                         if (!(*pbyRsr & RSR_BSSIDOK)) {
584                                 if (bDeFragRx) {
585                                         if (!device_alloc_frag_buf(pDevice, &pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx])) {
586                                                 DBG_PRT(MSG_LEVEL_ERR, KERN_ERR "%s: can not alloc more frag bufs\n",
587                                                         pDevice->dev->name);
588                                         }
589                                 }
590                                 return false;
591                         }
592                 } else {
593                         // discard DATA packet while not associate || BSSID error
594                         if (!pDevice->bLinkPass || !(*pbyRsr & RSR_BSSIDOK)) {
595                                 if (bDeFragRx) {
596                                         if (!device_alloc_frag_buf(pDevice, &pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx])) {
597                                                 DBG_PRT(MSG_LEVEL_ERR, KERN_ERR "%s: can not alloc more frag bufs\n",
598                                                         pDevice->dev->name);
599                                         }
600                                 }
601                                 return false;
602                         }
603                         //mike add:station mode check eapol-key challenge--->
604                         {
605                                 unsigned char Protocol_Version;    //802.1x Authentication
606                                 unsigned char Packet_Type;           //802.1x Authentication
607                                 if (bIsWEP)
608                                         cbIVOffset = 8;
609                                 else
610                                         cbIVOffset = 0;
611                                 wEtherType = (skb->data[cbIVOffset + 8 + 24 + 6] << 8) |
612                                         skb->data[cbIVOffset + 8 + 24 + 6 + 1];
613                                 Protocol_Version = skb->data[cbIVOffset + 8 + 24 + 6 + 1 + 1];
614                                 Packet_Type = skb->data[cbIVOffset + 8 + 24 + 6 + 1 + 1 + 1];
615                                 if (wEtherType == ETH_P_PAE) {         //Protocol Type in LLC-Header
616                                         if (((Protocol_Version == 1) || (Protocol_Version == 2)) &&
617                                             (Packet_Type == 3)) {  //802.1x OR eapol-key challenge frame receive
618                                                 bRxeapol_key = true;
619                                         }
620                                 }
621                         }
622                         //mike add:station mode check eapol-key challenge<---
623                 }
624         }
625
626 // Data frame Handle
627
628         if (pDevice->bEnablePSMode) {
629                 if (!IS_FC_MOREDATA((skb->data+4))) {
630                         if (pDevice->pMgmt->bInTIMWake == true)
631                                 pDevice->pMgmt->bInTIMWake = false;
632                 }
633         }
634
635         // Now it only supports 802.11g Infrastructure Mode, and support rate must up to 54 Mbps
636         if (pDevice->bDiversityEnable && (FrameSize > 50) &&
637             (pDevice->eOPMode == OP_MODE_INFRASTRUCTURE) &&
638             pDevice->bLinkPass) {
639                 BBvAntennaDiversity(pDevice, s_byGetRateIdx(*pbyRxRate), 0);
640         }
641
642         if (pDevice->byLocalID != REV_ID_VT3253_B1) {
643                 pDevice->uCurrRSSI = *pbyRSSI;
644         }
645         pDevice->byCurrSQ = *pbySQ;
646
647         if ((*pbyRSSI != 0) &&
648             (pMgmt->pCurrBSS != NULL)) {
649                 RFvRSSITodBm(pDevice, *pbyRSSI, &ldBm);
650                 // Monitor if RSSI is too strong.
651                 pMgmt->pCurrBSS->byRSSIStatCnt++;
652                 pMgmt->pCurrBSS->byRSSIStatCnt %= RSSI_STAT_COUNT;
653                 pMgmt->pCurrBSS->ldBmAverage[pMgmt->pCurrBSS->byRSSIStatCnt] = ldBm;
654                 for (ii = 0; ii < RSSI_STAT_COUNT; ii++) {
655                         if (pMgmt->pCurrBSS->ldBmAverage[ii] != 0) {
656                                 pMgmt->pCurrBSS->ldBmMAX = max(pMgmt->pCurrBSS->ldBmAverage[ii], ldBm);
657                         }
658                 }
659         }
660
661         // -----------------------------------------------
662
663         if ((pMgmt->eCurrMode == WMAC_MODE_ESS_AP) && pDevice->bEnable8021x) {
664                 unsigned char abyMacHdr[24];
665
666                 // Only 802.1x packet incoming allowed
667                 if (bIsWEP)
668                         cbIVOffset = 8;
669                 else
670                         cbIVOffset = 0;
671                 wEtherType = (skb->data[cbIVOffset + 4 + 24 + 6] << 8) |
672                         skb->data[cbIVOffset + 4 + 24 + 6 + 1];
673
674                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "wEtherType = %04x \n", wEtherType);
675                 if (wEtherType == ETH_P_PAE) {
676                         skb->dev = pDevice->apdev;
677
678                         if (bIsWEP) {
679                                 // strip IV header(8)
680                                 memcpy(&abyMacHdr[0], (skb->data + 4), 24);
681                                 memcpy((skb->data + 4 + cbIVOffset), &abyMacHdr[0], 24);
682                         }
683                         skb->data +=  (cbIVOffset + 4);
684                         skb->tail +=  (cbIVOffset + 4);
685                         skb_put(skb, FrameSize);
686                         skb_reset_mac_header(skb);
687
688                         skb->pkt_type = PACKET_OTHERHOST;
689                         skb->protocol = htons(ETH_P_802_2);
690                         memset(skb->cb, 0, sizeof(skb->cb));
691                         netif_rx(skb);
692                         return true;
693
694                 }
695                 // check if 802.1x authorized
696                 if (!(pMgmt->sNodeDBTable[iSANodeIndex].dwFlags & WLAN_STA_AUTHORIZED))
697                         return false;
698         }
699
700         if ((pKey != NULL) && (pKey->byCipherSuite == KEY_CTL_TKIP)) {
701                 if (bIsWEP) {
702                         FrameSize -= 8;  //MIC
703                 }
704         }
705
706         //--------------------------------------------------------------------------------
707         // Soft MIC
708         if ((pKey != NULL) && (pKey->byCipherSuite == KEY_CTL_TKIP)) {
709                 if (bIsWEP) {
710                         __le32 *pdwMIC_L;
711                         __le32 *pdwMIC_R;
712                         __le32 dwMIC_Priority;
713                         __le32 dwMICKey0 = 0, dwMICKey1 = 0;
714                         u32 dwLocalMIC_L = 0;
715                         u32 dwLocalMIC_R = 0;
716                         viawget_wpa_header *wpahdr;
717
718                         if (pMgmt->eCurrMode == WMAC_MODE_ESS_AP) {
719                                 dwMICKey0 = cpu_to_le32(*(u32 *)(&pKey->abyKey[24]));
720                                 dwMICKey1 = cpu_to_le32(*(u32 *)(&pKey->abyKey[28]));
721                         } else {
722                                 if (pDevice->pMgmt->eAuthenMode == WMAC_AUTH_WPANONE) {
723                                         dwMICKey0 = cpu_to_le32(*(u32 *)(&pKey->abyKey[16]));
724                                         dwMICKey1 = cpu_to_le32(*(u32 *)(&pKey->abyKey[20]));
725                                 } else if ((pKey->dwKeyIndex & BIT28) == 0) {
726                                         dwMICKey0 = cpu_to_le32(*(u32 *)(&pKey->abyKey[16]));
727                                         dwMICKey1 = cpu_to_le32(*(u32 *)(&pKey->abyKey[20]));
728                                 } else {
729                                         dwMICKey0 = cpu_to_le32(*(u32 *)(&pKey->abyKey[24]));
730                                         dwMICKey1 = cpu_to_le32(*(u32 *)(&pKey->abyKey[28]));
731                                 }
732                         }
733
734                         MIC_vInit(dwMICKey0, dwMICKey1);
735                         MIC_vAppend((unsigned char *)&(pDevice->sRxEthHeader.abyDstAddr[0]), 12);
736                         dwMIC_Priority = 0;
737                         MIC_vAppend((unsigned char *)&dwMIC_Priority, 4);
738                         // 4 is Rcv buffer header, 24 is MAC Header, and 8 is IV and Ext IV.
739                         MIC_vAppend((unsigned char *)(skb->data + 4 + WLAN_HDR_ADDR3_LEN + 8),
740                                     FrameSize - WLAN_HDR_ADDR3_LEN - 8);
741                         MIC_vGetMIC(&dwLocalMIC_L, &dwLocalMIC_R);
742                         MIC_vUnInit();
743
744                         pdwMIC_L = (__le32 *)(skb->data + 4 + FrameSize);
745                         pdwMIC_R = (__le32 *)(skb->data + 4 + FrameSize + 4);
746
747                         if ((le32_to_cpu(*pdwMIC_L) != dwLocalMIC_L) ||
748                             (le32_to_cpu(*pdwMIC_R) != dwLocalMIC_R) ||
749                             pDevice->bRxMICFail) {
750                                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "MIC comparison is fail!\n");
751                                 pDevice->bRxMICFail = false;
752                                 pDevice->s802_11Counter.TKIPLocalMICFailures++;
753                                 if (bDeFragRx) {
754                                         if (!device_alloc_frag_buf(pDevice, &pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx])) {
755                                                 DBG_PRT(MSG_LEVEL_ERR, KERN_ERR "%s: can not alloc more frag bufs\n",
756                                                         pDevice->dev->name);
757                                         }
758                                 }
759                                 //2008-0409-07, <Add> by Einsn Liu
760 #ifdef WPA_SUPPLICANT_DRIVER_WEXT_SUPPORT
761                                 //send event to wpa_supplicant
762                                 {
763                                         union iwreq_data wrqu;
764                                         struct iw_michaelmicfailure ev;
765                                         int keyidx = pbyFrame[cbHeaderSize+3] >> 6; //top two-bits
766                                         memset(&ev, 0, sizeof(ev));
767                                         ev.flags = keyidx & IW_MICFAILURE_KEY_ID;
768                                         if ((pMgmt->eCurrMode == WMAC_MODE_ESS_STA) &&
769                                             (pMgmt->eCurrState == WMAC_STATE_ASSOC) &&
770                                             (*pbyRsr & (RSR_ADDRBROAD | RSR_ADDRMULTI)) == 0) {
771                                                 ev.flags |= IW_MICFAILURE_PAIRWISE;
772                                         } else {
773                                                 ev.flags |= IW_MICFAILURE_GROUP;
774                                         }
775
776                                         ev.src_addr.sa_family = ARPHRD_ETHER;
777                                         memcpy(ev.src_addr.sa_data, pMACHeader->abyAddr2, ETH_ALEN);
778                                         memset(&wrqu, 0, sizeof(wrqu));
779                                         wrqu.data.length = sizeof(ev);
780                                         wireless_send_event(pDevice->dev, IWEVMICHAELMICFAILURE, &wrqu, (char *)&ev);
781
782                                 }
783 #endif
784
785                                 if ((pDevice->bWPADEVUp) && (pDevice->skb != NULL)) {
786                                         wpahdr = (viawget_wpa_header *)pDevice->skb->data;
787                                         if ((pDevice->pMgmt->eCurrMode == WMAC_MODE_ESS_STA) &&
788                                             (pDevice->pMgmt->eCurrState == WMAC_STATE_ASSOC) &&
789                                             (*pbyRsr & (RSR_ADDRBROAD | RSR_ADDRMULTI)) == 0) {
790                                                 wpahdr->type = VIAWGET_PTK_MIC_MSG;
791                                         } else {
792                                                 wpahdr->type = VIAWGET_GTK_MIC_MSG;
793                                         }
794                                         wpahdr->resp_ie_len = 0;
795                                         wpahdr->req_ie_len = 0;
796                                         skb_put(pDevice->skb, sizeof(viawget_wpa_header));
797                                         pDevice->skb->dev = pDevice->wpadev;
798                                         skb_reset_mac_header(pDevice->skb);
799                                         pDevice->skb->pkt_type = PACKET_HOST;
800                                         pDevice->skb->protocol = htons(ETH_P_802_2);
801                                         memset(pDevice->skb->cb, 0, sizeof(pDevice->skb->cb));
802                                         netif_rx(pDevice->skb);
803                                         pDevice->skb = dev_alloc_skb((int)pDevice->rx_buf_sz);
804                                 }
805
806                                 return false;
807
808                         }
809                 }
810         } //---end of SOFT MIC-----------------------------------------------------------------------
811
812         // ++++++++++ Reply Counter Check +++++++++++++
813
814         if ((pKey != NULL) && ((pKey->byCipherSuite == KEY_CTL_TKIP) ||
815                                (pKey->byCipherSuite == KEY_CTL_CCMP))) {
816                 if (bIsWEP) {
817                         unsigned short wLocalTSC15_0 = 0;
818                         unsigned long dwLocalTSC47_16 = 0;
819                         unsigned long long       RSC = 0;
820                         // endian issues
821                         RSC = *((unsigned long long *)&(pKey->KeyRSC));
822                         wLocalTSC15_0 = (unsigned short)RSC;
823                         dwLocalTSC47_16 = (unsigned long)(RSC>>16);
824
825                         RSC = dwRxTSC47_16;
826                         RSC <<= 16;
827                         RSC += wRxTSC15_0;
828                         memcpy(&(pKey->KeyRSC), &RSC,  sizeof(QWORD));
829
830                         if ((pDevice->sMgmtObj.eCurrMode == WMAC_MODE_ESS_STA) &&
831                             (pDevice->sMgmtObj.eCurrState == WMAC_STATE_ASSOC)) {
832                                 // check RSC
833                                 if ((wRxTSC15_0 < wLocalTSC15_0) &&
834                                     (dwRxTSC47_16 <= dwLocalTSC47_16) &&
835                                     !((dwRxTSC47_16 == 0) && (dwLocalTSC47_16 == 0xFFFFFFFF))) {
836                                         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "TSC is illegal~~!\n ");
837                                         if (pKey->byCipherSuite == KEY_CTL_TKIP)
838                                                 pDevice->s802_11Counter.TKIPReplays++;
839                                         else
840                                                 pDevice->s802_11Counter.CCMPReplays++;
841
842                                         if (bDeFragRx) {
843                                                 if (!device_alloc_frag_buf(pDevice, &pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx])) {
844                                                         DBG_PRT(MSG_LEVEL_ERR, KERN_ERR "%s: can not alloc more frag bufs\n",
845                                                                 pDevice->dev->name);
846                                                 }
847                                         }
848                                         return false;
849                                 }
850                         }
851                 }
852         } // ----- End of Reply Counter Check --------------------------
853
854         s_vProcessRxMACHeader(pDevice, (unsigned char *)(skb->data+4), FrameSize, bIsWEP, bExtIV, &cbHeaderOffset);
855         FrameSize -= cbHeaderOffset;
856         cbHeaderOffset += 4;        // 4 is Rcv buffer header
857
858         // Null data, framesize = 14
859         if (FrameSize < 15)
860                 return false;
861
862         if (pMgmt->eCurrMode == WMAC_MODE_ESS_AP) {
863                 if (!s_bAPModeRxData(pDevice,
864                                     skb,
865                                     FrameSize,
866                                     cbHeaderOffset,
867                                     iSANodeIndex,
868                                     iDANodeIndex
869 )) {
870                         if (bDeFragRx) {
871                                 if (!device_alloc_frag_buf(pDevice, &pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx])) {
872                                         DBG_PRT(MSG_LEVEL_ERR, KERN_ERR "%s: can not alloc more frag bufs\n",
873                                                 pDevice->dev->name);
874                                 }
875                         }
876                         return false;
877                 }
878         }
879
880         skb->data += cbHeaderOffset;
881         skb->tail += cbHeaderOffset;
882         skb_put(skb, FrameSize);
883         skb->protocol = eth_type_trans(skb, skb->dev);
884
885         //drop frame not met IEEE 802.3
886
887         skb->ip_summed = CHECKSUM_NONE;
888         pStats->rx_bytes += skb->len;
889         pStats->rx_packets++;
890         netif_rx(skb);
891
892         if (bDeFragRx) {
893                 if (!device_alloc_frag_buf(pDevice, &pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx])) {
894                         DBG_PRT(MSG_LEVEL_ERR, KERN_ERR "%s: can not alloc more frag bufs\n",
895                                 pDevice->dev->name);
896                 }
897                 return false;
898         }
899
900         return true;
901 }
902
903 static bool s_bAPModeRxCtl(
904         PSDevice pDevice,
905         unsigned char *pbyFrame,
906         int      iSANodeIndex
907 )
908 {
909         PS802_11Header      p802_11Header;
910         CMD_STATUS          Status;
911         PSMgmtObject        pMgmt = pDevice->pMgmt;
912
913         if (IS_CTL_PSPOLL(pbyFrame) || !IS_TYPE_CONTROL(pbyFrame)) {
914                 p802_11Header = (PS802_11Header)(pbyFrame);
915                 if (!IS_TYPE_MGMT(pbyFrame)) {
916                         // Data & PS-Poll packet
917                         // check frame class
918                         if (iSANodeIndex > 0) {
919                                 // frame class 3 fliter & checking
920                                 if (pMgmt->sNodeDBTable[iSANodeIndex].eNodeState < NODE_AUTH) {
921                                         // send deauth notification
922                                         // reason = (6) class 2 received from nonauth sta
923                                         vMgrDeAuthenBeginSta(pDevice,
924                                                              pMgmt,
925                                                              (unsigned char *)(p802_11Header->abyAddr2),
926                                                              (WLAN_MGMT_REASON_CLASS2_NONAUTH),
927                                                              &Status
928 );
929                                         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "dpc: send vMgrDeAuthenBeginSta 1\n");
930                                         return true;
931                                 }
932                                 if (pMgmt->sNodeDBTable[iSANodeIndex].eNodeState < NODE_ASSOC) {
933                                         // send deassoc notification
934                                         // reason = (7) class 3 received from nonassoc sta
935                                         vMgrDisassocBeginSta(pDevice,
936                                                              pMgmt,
937                                                              (unsigned char *)(p802_11Header->abyAddr2),
938                                                              (WLAN_MGMT_REASON_CLASS3_NONASSOC),
939                                                              &Status
940 );
941                                         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "dpc: send vMgrDisassocBeginSta 2\n");
942                                         return true;
943                                 }
944
945                                 if (pMgmt->sNodeDBTable[iSANodeIndex].bPSEnable) {
946                                         // delcare received ps-poll event
947                                         if (IS_CTL_PSPOLL(pbyFrame)) {
948                                                 pMgmt->sNodeDBTable[iSANodeIndex].bRxPSPoll = true;
949                                                 bScheduleCommand((void *)pDevice, WLAN_CMD_RX_PSPOLL, NULL);
950                                                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "dpc: WLAN_CMD_RX_PSPOLL 1\n");
951                                         } else {
952                                                 // check Data PS state
953                                                 // if PW bit off, send out all PS bufferring packets.
954                                                 if (!IS_FC_POWERMGT(pbyFrame)) {
955                                                         pMgmt->sNodeDBTable[iSANodeIndex].bPSEnable = false;
956                                                         pMgmt->sNodeDBTable[iSANodeIndex].bRxPSPoll = true;
957                                                         bScheduleCommand((void *)pDevice, WLAN_CMD_RX_PSPOLL, NULL);
958                                                         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "dpc: WLAN_CMD_RX_PSPOLL 2\n");
959                                                 }
960                                         }
961                                 } else {
962                                         if (IS_FC_POWERMGT(pbyFrame)) {
963                                                 pMgmt->sNodeDBTable[iSANodeIndex].bPSEnable = true;
964                                                 // Once if STA in PS state, enable multicast bufferring
965                                                 pMgmt->sNodeDBTable[0].bPSEnable = true;
966                                         } else {
967                                                 // clear all pending PS frame.
968                                                 if (pMgmt->sNodeDBTable[iSANodeIndex].wEnQueueCnt > 0) {
969                                                         pMgmt->sNodeDBTable[iSANodeIndex].bPSEnable = false;
970                                                         pMgmt->sNodeDBTable[iSANodeIndex].bRxPSPoll = true;
971                                                         bScheduleCommand((void *)pDevice, WLAN_CMD_RX_PSPOLL, NULL);
972                                                         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "dpc: WLAN_CMD_RX_PSPOLL 3\n");
973
974                                                 }
975                                         }
976                                 }
977                         } else {
978                                 vMgrDeAuthenBeginSta(pDevice,
979                                                      pMgmt,
980                                                      (unsigned char *)(p802_11Header->abyAddr2),
981                                                      (WLAN_MGMT_REASON_CLASS2_NONAUTH),
982                                                      &Status
983 );
984                                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "dpc: send vMgrDeAuthenBeginSta 3\n");
985                                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "BSSID:%pM\n",
986                                         p802_11Header->abyAddr3);
987                                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "ADDR2:%pM\n",
988                                         p802_11Header->abyAddr2);
989                                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "ADDR1:%pM\n",
990                                         p802_11Header->abyAddr1);
991                                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "dpc: wFrameCtl= %x\n", p802_11Header->wFrameCtl);
992                                 VNSvInPortB(pDevice->PortOffset + MAC_REG_RCR, &(pDevice->byRxMode));
993                                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "dpc:pDevice->byRxMode = %x\n", pDevice->byRxMode);
994                                 return true;
995                         }
996                 }
997         }
998         return false;
999 }
1000
1001 static bool s_bHandleRxEncryption(
1002         PSDevice     pDevice,
1003         unsigned char *pbyFrame,
1004         unsigned int FrameSize,
1005         unsigned char *pbyRsr,
1006         unsigned char *pbyNewRsr,
1007         PSKeyItem   *pKeyOut,
1008         bool *pbExtIV,
1009         unsigned short *pwRxTSC15_0,
1010         unsigned long *pdwRxTSC47_16
1011 )
1012 {
1013         unsigned int PayloadLen = FrameSize;
1014         unsigned char *pbyIV;
1015         unsigned char byKeyIdx;
1016         PSKeyItem       pKey = NULL;
1017         unsigned char byDecMode = KEY_CTL_WEP;
1018         PSMgmtObject    pMgmt = pDevice->pMgmt;
1019
1020         *pwRxTSC15_0 = 0;
1021         *pdwRxTSC47_16 = 0;
1022
1023         pbyIV = pbyFrame + WLAN_HDR_ADDR3_LEN;
1024         if (WLAN_GET_FC_TODS(*(unsigned short *)pbyFrame) &&
1025             WLAN_GET_FC_FROMDS(*(unsigned short *)pbyFrame)) {
1026                 pbyIV += 6;             // 6 is 802.11 address4
1027                 PayloadLen -= 6;
1028         }
1029         byKeyIdx = (*(pbyIV+3) & 0xc0);
1030         byKeyIdx >>= 6;
1031         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "\nKeyIdx: %d\n", byKeyIdx);
1032
1033         if ((pMgmt->eAuthenMode == WMAC_AUTH_WPA) ||
1034             (pMgmt->eAuthenMode == WMAC_AUTH_WPAPSK) ||
1035             (pMgmt->eAuthenMode == WMAC_AUTH_WPANONE) ||
1036             (pMgmt->eAuthenMode == WMAC_AUTH_WPA2) ||
1037             (pMgmt->eAuthenMode == WMAC_AUTH_WPA2PSK)) {
1038                 if (((*pbyRsr & (RSR_ADDRBROAD | RSR_ADDRMULTI)) == 0) &&
1039                     (pDevice->pMgmt->byCSSPK != KEY_CTL_NONE)) {
1040                         // unicast pkt use pairwise key
1041                         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "unicast pkt\n");
1042                         if (KeybGetKey(&(pDevice->sKey), pDevice->abyBSSID, 0xFFFFFFFF, &pKey) == true) {
1043                                 if (pDevice->pMgmt->byCSSPK == KEY_CTL_TKIP)
1044                                         byDecMode = KEY_CTL_TKIP;
1045                                 else if (pDevice->pMgmt->byCSSPK == KEY_CTL_CCMP)
1046                                         byDecMode = KEY_CTL_CCMP;
1047                         }
1048                         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "unicast pkt: %d, %p\n", byDecMode, pKey);
1049                 } else {
1050                         // use group key
1051                         KeybGetKey(&(pDevice->sKey), pDevice->abyBSSID, byKeyIdx, &pKey);
1052                         if (pDevice->pMgmt->byCSSGK == KEY_CTL_TKIP)
1053                                 byDecMode = KEY_CTL_TKIP;
1054                         else if (pDevice->pMgmt->byCSSGK == KEY_CTL_CCMP)
1055                                 byDecMode = KEY_CTL_CCMP;
1056                         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "group pkt: %d, %d, %p\n", byKeyIdx, byDecMode, pKey);
1057                 }
1058         }
1059         // our WEP only support Default Key
1060         if (pKey == NULL) {
1061                 // use default group key
1062                 KeybGetKey(&(pDevice->sKey), pDevice->abyBroadcastAddr, byKeyIdx, &pKey);
1063                 if (pDevice->pMgmt->byCSSGK == KEY_CTL_TKIP)
1064                         byDecMode = KEY_CTL_TKIP;
1065                 else if (pDevice->pMgmt->byCSSGK == KEY_CTL_CCMP)
1066                         byDecMode = KEY_CTL_CCMP;
1067         }
1068         *pKeyOut = pKey;
1069
1070         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "AES:%d %d %d\n", pDevice->pMgmt->byCSSPK, pDevice->pMgmt->byCSSGK, byDecMode);
1071
1072         if (pKey == NULL) {
1073                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "pKey == NULL\n");
1074
1075                 return false;
1076         }
1077         if (byDecMode != pKey->byCipherSuite) {
1078
1079                 *pKeyOut = NULL;
1080                 return false;
1081         }
1082         if (byDecMode == KEY_CTL_WEP) {
1083                 // handle WEP
1084                 if ((pDevice->byLocalID <= REV_ID_VT3253_A1) ||
1085                     (((PSKeyTable)(pKey->pvKeyTable))->bSoftWEP == true)) {
1086                         // Software WEP
1087                         // 1. 3253A
1088                         // 2. WEP 256
1089
1090                         PayloadLen -= (WLAN_HDR_ADDR3_LEN + 4 + 4); // 24 is 802.11 header,4 is IV, 4 is crc
1091                         memcpy(pDevice->abyPRNG, pbyIV, 3);
1092                         memcpy(pDevice->abyPRNG + 3, pKey->abyKey, pKey->uKeyLength);
1093                         rc4_init(&pDevice->SBox, pDevice->abyPRNG, pKey->uKeyLength + 3);
1094                         rc4_encrypt(&pDevice->SBox, pbyIV+4, pbyIV+4, PayloadLen);
1095
1096                         if (ETHbIsBufferCrc32Ok(pbyIV+4, PayloadLen)) {
1097                                 *pbyNewRsr |= NEWRSR_DECRYPTOK;
1098                         }
1099                 }
1100         } else if ((byDecMode == KEY_CTL_TKIP) ||
1101                    (byDecMode == KEY_CTL_CCMP)) {
1102                 // TKIP/AES
1103
1104                 PayloadLen -= (WLAN_HDR_ADDR3_LEN + 8 + 4); // 24 is 802.11 header, 8 is IV&ExtIV, 4 is crc
1105                 *pdwRxTSC47_16 = cpu_to_le32(*(unsigned long *)(pbyIV + 4));
1106                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "ExtIV: %lx\n", *pdwRxTSC47_16);
1107                 if (byDecMode == KEY_CTL_TKIP) {
1108                         *pwRxTSC15_0 = cpu_to_le16(MAKEWORD(*(pbyIV + 2), *pbyIV));
1109                 } else {
1110                         *pwRxTSC15_0 = cpu_to_le16(*(unsigned short *)pbyIV);
1111                 }
1112                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "TSC0_15: %x\n", *pwRxTSC15_0);
1113
1114                 if ((byDecMode == KEY_CTL_TKIP) &&
1115                     (pDevice->byLocalID <= REV_ID_VT3253_A1)) {
1116                         // Software TKIP
1117                         // 1. 3253 A
1118                         PS802_11Header  pMACHeader = (PS802_11Header)(pbyFrame);
1119                         TKIPvMixKey(pKey->abyKey, pMACHeader->abyAddr2, *pwRxTSC15_0, *pdwRxTSC47_16, pDevice->abyPRNG);
1120                         rc4_init(&pDevice->SBox, pDevice->abyPRNG, TKIP_KEY_LEN);
1121                         rc4_encrypt(&pDevice->SBox, pbyIV+8, pbyIV+8, PayloadLen);
1122                         if (ETHbIsBufferCrc32Ok(pbyIV+8, PayloadLen)) {
1123                                 *pbyNewRsr |= NEWRSR_DECRYPTOK;
1124                                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "ICV OK!\n");
1125                         } else {
1126                                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "ICV FAIL!!!\n");
1127                                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "PayloadLen = %d\n", PayloadLen);
1128                         }
1129                 }
1130         }// end of TKIP/AES
1131
1132         if ((*(pbyIV+3) & 0x20) != 0)
1133                 *pbExtIV = true;
1134         return true;
1135 }
1136
1137 static bool s_bHostWepRxEncryption(
1138         PSDevice     pDevice,
1139         unsigned char *pbyFrame,
1140         unsigned int FrameSize,
1141         unsigned char *pbyRsr,
1142         bool bOnFly,
1143         PSKeyItem    pKey,
1144         unsigned char *pbyNewRsr,
1145         bool *pbExtIV,
1146         unsigned short *pwRxTSC15_0,
1147         unsigned long *pdwRxTSC47_16
1148 )
1149 {
1150         unsigned int PayloadLen = FrameSize;
1151         unsigned char *pbyIV;
1152         unsigned char byKeyIdx;
1153         unsigned char byDecMode = KEY_CTL_WEP;
1154         PS802_11Header  pMACHeader;
1155
1156         *pwRxTSC15_0 = 0;
1157         *pdwRxTSC47_16 = 0;
1158
1159         pbyIV = pbyFrame + WLAN_HDR_ADDR3_LEN;
1160         if (WLAN_GET_FC_TODS(*(unsigned short *)pbyFrame) &&
1161             WLAN_GET_FC_FROMDS(*(unsigned short *)pbyFrame)) {
1162                 pbyIV += 6;             // 6 is 802.11 address4
1163                 PayloadLen -= 6;
1164         }
1165         byKeyIdx = (*(pbyIV+3) & 0xc0);
1166         byKeyIdx >>= 6;
1167         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "\nKeyIdx: %d\n", byKeyIdx);
1168
1169         if (pDevice->pMgmt->byCSSGK == KEY_CTL_TKIP)
1170                 byDecMode = KEY_CTL_TKIP;
1171         else if (pDevice->pMgmt->byCSSGK == KEY_CTL_CCMP)
1172                 byDecMode = KEY_CTL_CCMP;
1173
1174         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "AES:%d %d %d\n", pDevice->pMgmt->byCSSPK, pDevice->pMgmt->byCSSGK, byDecMode);
1175
1176         if (byDecMode != pKey->byCipherSuite)
1177                 return false;
1178
1179         if (byDecMode == KEY_CTL_WEP) {
1180                 // handle WEP
1181                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "byDecMode == KEY_CTL_WEP \n");
1182                 if ((pDevice->byLocalID <= REV_ID_VT3253_A1) ||
1183                     (((PSKeyTable)(pKey->pvKeyTable))->bSoftWEP == true) ||
1184                     !bOnFly) {
1185                         // Software WEP
1186                         // 1. 3253A
1187                         // 2. WEP 256
1188                         // 3. NotOnFly
1189
1190                         PayloadLen -= (WLAN_HDR_ADDR3_LEN + 4 + 4); // 24 is 802.11 header,4 is IV, 4 is crc
1191                         memcpy(pDevice->abyPRNG, pbyIV, 3);
1192                         memcpy(pDevice->abyPRNG + 3, pKey->abyKey, pKey->uKeyLength);
1193                         rc4_init(&pDevice->SBox, pDevice->abyPRNG, pKey->uKeyLength + 3);
1194                         rc4_encrypt(&pDevice->SBox, pbyIV+4, pbyIV+4, PayloadLen);
1195
1196                         if (ETHbIsBufferCrc32Ok(pbyIV+4, PayloadLen)) {
1197                                 *pbyNewRsr |= NEWRSR_DECRYPTOK;
1198                         }
1199                 }
1200         } else if ((byDecMode == KEY_CTL_TKIP) ||
1201                    (byDecMode == KEY_CTL_CCMP)) {
1202                 // TKIP/AES
1203
1204                 PayloadLen -= (WLAN_HDR_ADDR3_LEN + 8 + 4); // 24 is 802.11 header, 8 is IV&ExtIV, 4 is crc
1205                 *pdwRxTSC47_16 = cpu_to_le32(*(unsigned long *)(pbyIV + 4));
1206                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "ExtIV: %lx\n", *pdwRxTSC47_16);
1207
1208                 if (byDecMode == KEY_CTL_TKIP) {
1209                         *pwRxTSC15_0 = cpu_to_le16(MAKEWORD(*(pbyIV+2), *pbyIV));
1210                 } else {
1211                         *pwRxTSC15_0 = cpu_to_le16(*(unsigned short *)pbyIV);
1212                 }
1213                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "TSC0_15: %x\n", *pwRxTSC15_0);
1214
1215                 if (byDecMode == KEY_CTL_TKIP) {
1216                         if ((pDevice->byLocalID <= REV_ID_VT3253_A1) || !bOnFly) {
1217                                 // Software TKIP
1218                                 // 1. 3253 A
1219                                 // 2. NotOnFly
1220                                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "soft KEY_CTL_TKIP \n");
1221                                 pMACHeader = (PS802_11Header)(pbyFrame);
1222                                 TKIPvMixKey(pKey->abyKey, pMACHeader->abyAddr2, *pwRxTSC15_0, *pdwRxTSC47_16, pDevice->abyPRNG);
1223                                 rc4_init(&pDevice->SBox, pDevice->abyPRNG, TKIP_KEY_LEN);
1224                                 rc4_encrypt(&pDevice->SBox, pbyIV+8, pbyIV+8, PayloadLen);
1225                                 if (ETHbIsBufferCrc32Ok(pbyIV+8, PayloadLen)) {
1226                                         *pbyNewRsr |= NEWRSR_DECRYPTOK;
1227                                         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "ICV OK!\n");
1228                                 } else {
1229                                         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "ICV FAIL!!!\n");
1230                                         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "PayloadLen = %d\n", PayloadLen);
1231                                 }
1232                         }
1233                 }
1234
1235                 if (byDecMode == KEY_CTL_CCMP) {
1236                         if (!bOnFly) {
1237                                 // Software CCMP
1238                                 // NotOnFly
1239                                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "soft KEY_CTL_CCMP\n");
1240                                 if (AESbGenCCMP(pKey->abyKey, pbyFrame, FrameSize)) {
1241                                         *pbyNewRsr |= NEWRSR_DECRYPTOK;
1242                                         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "CCMP MIC compare OK!\n");
1243                                 } else {
1244                                         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "CCMP MIC fail!\n");
1245                                 }
1246                         }
1247                 }
1248
1249         }// end of TKIP/AES
1250
1251         if ((*(pbyIV+3) & 0x20) != 0)
1252                 *pbExtIV = true;
1253         return true;
1254 }
1255
1256 static bool s_bAPModeRxData(
1257         PSDevice pDevice,
1258         struct sk_buff *skb,
1259         unsigned int FrameSize,
1260         unsigned int cbHeaderOffset,
1261         int      iSANodeIndex,
1262         int      iDANodeIndex
1263 )
1264 {
1265         PSMgmtObject        pMgmt = pDevice->pMgmt;
1266         bool bRelayAndForward = false;
1267         bool bRelayOnly = false;
1268         unsigned char byMask[8] = {1, 2, 4, 8, 0x10, 0x20, 0x40, 0x80};
1269         unsigned short wAID;
1270
1271         struct sk_buff *skbcpy = NULL;
1272
1273         if (FrameSize > CB_MAX_BUF_SIZE)
1274                 return false;
1275         // check DA
1276         if (is_multicast_ether_addr((unsigned char *)(skb->data+cbHeaderOffset))) {
1277                 if (pMgmt->sNodeDBTable[0].bPSEnable) {
1278                         skbcpy = dev_alloc_skb((int)pDevice->rx_buf_sz);
1279
1280                         // if any node in PS mode, buffer packet until DTIM.
1281                         if (skbcpy == NULL) {
1282                                 DBG_PRT(MSG_LEVEL_NOTICE, KERN_INFO "relay multicast no skb available \n");
1283                         } else {
1284                                 skbcpy->dev = pDevice->dev;
1285                                 skbcpy->len = FrameSize;
1286                                 memcpy(skbcpy->data, skb->data+cbHeaderOffset, FrameSize);
1287                                 skb_queue_tail(&(pMgmt->sNodeDBTable[0].sTxPSQueue), skbcpy);
1288
1289                                 pMgmt->sNodeDBTable[0].wEnQueueCnt++;
1290                                 // set tx map
1291                                 pMgmt->abyPSTxMap[0] |= byMask[0];
1292                         }
1293                 } else {
1294                         bRelayAndForward = true;
1295                 }
1296         } else {
1297                 // check if relay
1298                 if (BSSDBbIsSTAInNodeDB(pMgmt, (unsigned char *)(skb->data+cbHeaderOffset), &iDANodeIndex)) {
1299                         if (pMgmt->sNodeDBTable[iDANodeIndex].eNodeState >= NODE_ASSOC) {
1300                                 if (pMgmt->sNodeDBTable[iDANodeIndex].bPSEnable) {
1301                                         // queue this skb until next PS tx, and then release.
1302
1303                                         skb->data += cbHeaderOffset;
1304                                         skb->tail += cbHeaderOffset;
1305                                         skb_put(skb, FrameSize);
1306                                         skb_queue_tail(&pMgmt->sNodeDBTable[iDANodeIndex].sTxPSQueue, skb);
1307                                         pMgmt->sNodeDBTable[iDANodeIndex].wEnQueueCnt++;
1308                                         wAID = pMgmt->sNodeDBTable[iDANodeIndex].wAID;
1309                                         pMgmt->abyPSTxMap[wAID >> 3] |=  byMask[wAID & 7];
1310                                         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "relay: index= %d, pMgmt->abyPSTxMap[%d]= %d\n",
1311                                                 iDANodeIndex, (wAID >> 3), pMgmt->abyPSTxMap[wAID >> 3]);
1312                                         return true;
1313                                 } else {
1314                                         bRelayOnly = true;
1315                                 }
1316                         }
1317                 }
1318         }
1319
1320         if (bRelayOnly || bRelayAndForward) {
1321                 // relay this packet right now
1322                 if (bRelayAndForward)
1323                         iDANodeIndex = 0;
1324
1325                 if ((pDevice->uAssocCount > 1) && (iDANodeIndex >= 0)) {
1326                         ROUTEbRelay(pDevice, (unsigned char *)(skb->data + cbHeaderOffset), FrameSize, (unsigned int)iDANodeIndex);
1327                 }
1328
1329                 if (bRelayOnly)
1330                         return false;
1331         }
1332         // none associate, don't forward
1333         if (pDevice->uAssocCount == 0)
1334                 return false;
1335
1336         return true;
1337 }