x86/xen: resume timer irqs early
[linux/fpc-iii.git] / drivers / staging / vt6656 / dpc.c
blobea7d443b11d067c22f07f41c5c8ac7d2f6fecb03
1 /*
2 * Copyright (c) 1996, 2003 VIA Networking Technologies, Inc.
3 * All rights reserved.
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.
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.
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.
19 * File: dpc.c
21 * Purpose: handle dpc rx functions
23 * Author: Lyndon Chen
25 * Date: May 20, 2003
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
37 * Revision History:
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 "hostap.h"
53 #include "rf.h"
54 #include "iowpa.h"
55 #include "aes_ccmp.h"
56 #include "datarate.h"
57 #include "usbpipe.h"
59 //static int msglevel =MSG_LEVEL_DEBUG;
60 static int msglevel =MSG_LEVEL_INFO;
62 const u8 acbyRxRate[MAX_RATE] =
63 {2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108};
65 static u8 s_byGetRateIdx(u8 byRate);
67 static
68 void
69 s_vGetDASA(
70 u8 * pbyRxBufferAddr,
71 unsigned int *pcbHeaderSize,
72 struct ethhdr *psEthHeader
75 static void s_vProcessRxMACHeader(struct vnt_private *pDevice,
76 u8 *pbyRxBufferAddr, u32 cbPacketSize, int bIsWEP, int bExtIV,
77 u32 *pcbHeadSize);
79 static int s_bAPModeRxCtl(struct vnt_private *pDevice, u8 *pbyFrame,
80 s32 iSANodeIndex);
82 static int s_bAPModeRxData(struct vnt_private *pDevice, struct sk_buff *skb,
83 u32 FrameSize, u32 cbHeaderOffset, s32 iSANodeIndex, s32 iDANodeIndex);
85 static int s_bHandleRxEncryption(struct vnt_private *pDevice, u8 *pbyFrame,
86 u32 FrameSize, u8 *pbyRsr, u8 *pbyNewRsr, PSKeyItem *pKeyOut,
87 s32 *pbExtIV, u16 *pwRxTSC15_0, u32 *pdwRxTSC47_16);
89 static int s_bHostWepRxEncryption(struct vnt_private *pDevice, u8 *pbyFrame,
90 u32 FrameSize, u8 *pbyRsr, int bOnFly, PSKeyItem pKey, u8 *pbyNewRsr,
91 s32 *pbExtIV, u16 *pwRxTSC15_0, u32 *pdwRxTSC47_16);
93 /*+
95 * Description:
96 * Translate Rcv 802.11 header to 802.3 header with Rx buffer
98 * Parameters:
99 * In:
100 * pDevice
101 * dwRxBufferAddr - Address of Rcv Buffer
102 * cbPacketSize - Rcv Packet size
103 * bIsWEP - If Rcv with WEP
104 * Out:
105 * pcbHeaderSize - 802.11 header size
107 * Return Value: None
111 static void s_vProcessRxMACHeader(struct vnt_private *pDevice,
112 u8 *pbyRxBufferAddr, u32 cbPacketSize, int bIsWEP, int bExtIV,
113 u32 *pcbHeadSize)
115 u8 *pbyRxBuffer;
116 u32 cbHeaderSize = 0;
117 u16 *pwType;
118 struct ieee80211_hdr *pMACHeader;
119 int ii;
121 pMACHeader = (struct ieee80211_hdr *) (pbyRxBufferAddr + cbHeaderSize);
123 s_vGetDASA((u8 *)pMACHeader, &cbHeaderSize, &pDevice->sRxEthHeader);
125 if (bIsWEP) {
126 if (bExtIV) {
127 // strip IV&ExtIV , add 8 byte
128 cbHeaderSize += (WLAN_HDR_ADDR3_LEN + 8);
129 } else {
130 // strip IV , add 4 byte
131 cbHeaderSize += (WLAN_HDR_ADDR3_LEN + 4);
134 else {
135 cbHeaderSize += WLAN_HDR_ADDR3_LEN;
138 pbyRxBuffer = (u8 *) (pbyRxBufferAddr + cbHeaderSize);
139 if (!compare_ether_addr(pbyRxBuffer, &pDevice->abySNAP_Bridgetunnel[0])) {
140 cbHeaderSize += 6;
141 } else if (!compare_ether_addr(pbyRxBuffer, &pDevice->abySNAP_RFC1042[0])) {
142 cbHeaderSize += 6;
143 pwType = (u16 *) (pbyRxBufferAddr + cbHeaderSize);
144 if ((*pwType == cpu_to_be16(ETH_P_IPX)) ||
145 (*pwType == cpu_to_le16(0xF380))) {
146 cbHeaderSize -= 8;
147 pwType = (u16 *) (pbyRxBufferAddr + cbHeaderSize);
148 if (bIsWEP) {
149 if (bExtIV) {
150 *pwType = htons(cbPacketSize - WLAN_HDR_ADDR3_LEN - 8); // 8 is IV&ExtIV
151 } else {
152 *pwType = htons(cbPacketSize - WLAN_HDR_ADDR3_LEN - 4); // 4 is IV
155 else {
156 *pwType = htons(cbPacketSize - WLAN_HDR_ADDR3_LEN);
160 else {
161 cbHeaderSize -= 2;
162 pwType = (u16 *) (pbyRxBufferAddr + cbHeaderSize);
163 if (bIsWEP) {
164 if (bExtIV) {
165 *pwType = htons(cbPacketSize - WLAN_HDR_ADDR3_LEN - 8); // 8 is IV&ExtIV
166 } else {
167 *pwType = htons(cbPacketSize - WLAN_HDR_ADDR3_LEN - 4); // 4 is IV
170 else {
171 *pwType = htons(cbPacketSize - WLAN_HDR_ADDR3_LEN);
175 cbHeaderSize -= (ETH_ALEN * 2);
176 pbyRxBuffer = (u8 *) (pbyRxBufferAddr + cbHeaderSize);
177 for (ii = 0; ii < ETH_ALEN; ii++)
178 *pbyRxBuffer++ = pDevice->sRxEthHeader.h_dest[ii];
179 for (ii = 0; ii < ETH_ALEN; ii++)
180 *pbyRxBuffer++ = pDevice->sRxEthHeader.h_source[ii];
182 *pcbHeadSize = cbHeaderSize;
185 static u8 s_byGetRateIdx(u8 byRate)
187 u8 byRateIdx;
189 for (byRateIdx = 0; byRateIdx <MAX_RATE ; byRateIdx++) {
190 if (acbyRxRate[byRateIdx%MAX_RATE] == byRate)
191 return byRateIdx;
193 return 0;
196 static
197 void
198 s_vGetDASA (
199 u8 * pbyRxBufferAddr,
200 unsigned int *pcbHeaderSize,
201 struct ethhdr *psEthHeader
204 unsigned int cbHeaderSize = 0;
205 struct ieee80211_hdr *pMACHeader;
206 int ii;
208 pMACHeader = (struct ieee80211_hdr *) (pbyRxBufferAddr + cbHeaderSize);
210 if ((pMACHeader->frame_control & FC_TODS) == 0) {
211 if (pMACHeader->frame_control & FC_FROMDS) {
212 for (ii = 0; ii < ETH_ALEN; ii++) {
213 psEthHeader->h_dest[ii] =
214 pMACHeader->addr1[ii];
215 psEthHeader->h_source[ii] =
216 pMACHeader->addr3[ii];
218 } else {
219 /* IBSS mode */
220 for (ii = 0; ii < ETH_ALEN; ii++) {
221 psEthHeader->h_dest[ii] =
222 pMACHeader->addr1[ii];
223 psEthHeader->h_source[ii] =
224 pMACHeader->addr2[ii];
227 } else {
228 /* Is AP mode.. */
229 if (pMACHeader->frame_control & FC_FROMDS) {
230 for (ii = 0; ii < ETH_ALEN; ii++) {
231 psEthHeader->h_dest[ii] =
232 pMACHeader->addr3[ii];
233 psEthHeader->h_source[ii] =
234 pMACHeader->addr4[ii];
235 cbHeaderSize += 6;
237 } else {
238 for (ii = 0; ii < ETH_ALEN; ii++) {
239 psEthHeader->h_dest[ii] =
240 pMACHeader->addr3[ii];
241 psEthHeader->h_source[ii] =
242 pMACHeader->addr2[ii];
246 *pcbHeaderSize = cbHeaderSize;
249 int RXbBulkInProcessData(struct vnt_private *pDevice, struct vnt_rcb *pRCB,
250 unsigned long BytesToIndicate)
252 struct net_device_stats *pStats = &pDevice->stats;
253 struct sk_buff *skb;
254 struct vnt_manager *pMgmt = &pDevice->vnt_mgmt;
255 struct vnt_rx_mgmt *pRxPacket = &pMgmt->sRxPacket;
256 struct ieee80211_hdr *p802_11Header;
257 u8 *pbyRsr, *pbyNewRsr, *pbyRSSI, *pbyFrame;
258 u64 *pqwTSFTime;
259 u32 bDeFragRx = false;
260 u32 cbHeaderOffset, cbIVOffset;
261 u32 FrameSize;
262 u16 wEtherType = 0;
263 s32 iSANodeIndex = -1, iDANodeIndex = -1;
264 int ii;
265 u8 *pbyRxSts, *pbyRxRate, *pbySQ, *pby3SQ;
266 u32 cbHeaderSize;
267 PSKeyItem pKey = NULL;
268 u16 wRxTSC15_0 = 0;
269 u32 dwRxTSC47_16 = 0;
270 SKeyItem STempKey;
271 /* signed long ldBm = 0; */
272 int bIsWEP = false; int bExtIV = false;
273 u32 dwWbkStatus;
274 struct vnt_rcb *pRCBIndicate = pRCB;
275 u8 *pbyDAddress;
276 u16 *pwPLCP_Length;
277 u8 abyVaildRate[MAX_RATE]
278 = {2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108};
279 u16 wPLCPwithPadding;
280 struct ieee80211_hdr *pMACHeader;
281 int bRxeapol_key = false;
283 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"---------- RXbBulkInProcessData---\n");
285 skb = pRCB->skb;
287 /* [31:16]RcvByteCount ( not include 4-byte Status ) */
288 dwWbkStatus = *((u32 *)(skb->data));
289 FrameSize = dwWbkStatus >> 16;
290 FrameSize += 4;
292 if (BytesToIndicate != FrameSize) {
293 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"------- WRONG Length 1\n");
294 return false;
297 if ((BytesToIndicate > 2372) || (BytesToIndicate <= 40)) {
298 // Frame Size error drop this packet.
299 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "---------- WRONG Length 2\n");
300 return false;
303 pbyDAddress = (u8 *)(skb->data);
304 pbyRxSts = pbyDAddress+4;
305 pbyRxRate = pbyDAddress+5;
307 //real Frame Size = USBFrameSize -4WbkStatus - 4RxStatus - 8TSF - 4RSR - 4SQ3 - ?Padding
308 //if SQ3 the range is 24~27, if no SQ3 the range is 20~23
309 //real Frame size in PLCPLength field.
310 pwPLCP_Length = (u16 *) (pbyDAddress + 6);
311 //Fix hardware bug => PLCP_Length error
312 if ( ((BytesToIndicate - (*pwPLCP_Length)) > 27) ||
313 ((BytesToIndicate - (*pwPLCP_Length)) < 24) ||
314 (BytesToIndicate < (*pwPLCP_Length)) ) {
316 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Wrong PLCP Length %x\n", (int) *pwPLCP_Length);
317 return false;
319 for ( ii=RATE_1M;ii<MAX_RATE;ii++) {
320 if ( *pbyRxRate == abyVaildRate[ii] ) {
321 break;
324 if ( ii==MAX_RATE ) {
325 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Wrong RxRate %x\n",(int) *pbyRxRate);
326 return false;
329 wPLCPwithPadding = ( (*pwPLCP_Length / 4) + ( (*pwPLCP_Length % 4) ? 1:0 ) ) *4;
331 pqwTSFTime = (u64 *)(pbyDAddress + 8 + wPLCPwithPadding);
332 if(pDevice->byBBType == BB_TYPE_11G) {
333 pby3SQ = pbyDAddress + 8 + wPLCPwithPadding + 12;
334 pbySQ = pby3SQ;
336 else {
337 pbySQ = pbyDAddress + 8 + wPLCPwithPadding + 8;
338 pby3SQ = pbySQ;
340 pbyNewRsr = pbyDAddress + 8 + wPLCPwithPadding + 9;
341 pbyRSSI = pbyDAddress + 8 + wPLCPwithPadding + 10;
342 pbyRsr = pbyDAddress + 8 + wPLCPwithPadding + 11;
344 FrameSize = *pwPLCP_Length;
346 pbyFrame = pbyDAddress + 8;
347 // update receive statistic counter
349 STAvUpdateRDStatCounter(&pDevice->scStatistic,
350 *pbyRsr,
351 *pbyNewRsr,
352 *pbyRxSts,
353 *pbyRxRate,
354 pbyFrame,
355 FrameSize
358 pMACHeader = (struct ieee80211_hdr *) pbyFrame;
360 //mike add: to judge if current AP is activated?
361 if ((pMgmt->eCurrMode == WMAC_MODE_STANDBY) ||
362 (pMgmt->eCurrMode == WMAC_MODE_ESS_STA)) {
363 if (pMgmt->sNodeDBTable[0].bActive) {
364 if (!compare_ether_addr(pMgmt->abyCurrBSSID, pMACHeader->addr2)) {
365 if (pMgmt->sNodeDBTable[0].uInActiveCount != 0)
366 pMgmt->sNodeDBTable[0].uInActiveCount = 0;
371 if (!is_multicast_ether_addr(pMACHeader->addr1)) {
372 if (WCTLbIsDuplicate(&(pDevice->sDupRxCache), (struct ieee80211_hdr *) pbyFrame)) {
373 pDevice->s802_11Counter.FrameDuplicateCount++;
374 return false;
377 if (compare_ether_addr(pDevice->abyCurrentNetAddr,
378 pMACHeader->addr1)) {
379 return false;
383 // Use for TKIP MIC
384 s_vGetDASA(pbyFrame, &cbHeaderSize, &pDevice->sRxEthHeader);
386 if (!compare_ether_addr((u8 *)&(pDevice->sRxEthHeader.h_source[0]),
387 pDevice->abyCurrentNetAddr))
388 return false;
390 if ((pMgmt->eCurrMode == WMAC_MODE_ESS_AP) || (pMgmt->eCurrMode == WMAC_MODE_IBSS_STA)) {
391 if (IS_CTL_PSPOLL(pbyFrame) || !IS_TYPE_CONTROL(pbyFrame)) {
392 p802_11Header = (struct ieee80211_hdr *) (pbyFrame);
393 // get SA NodeIndex
394 if (BSSbIsSTAInNodeDB(pDevice, (u8 *)(p802_11Header->addr2), &iSANodeIndex)) {
395 pMgmt->sNodeDBTable[iSANodeIndex].ulLastRxJiffer = jiffies;
396 pMgmt->sNodeDBTable[iSANodeIndex].uInActiveCount = 0;
401 if (pMgmt->eCurrMode == WMAC_MODE_ESS_AP) {
402 if (s_bAPModeRxCtl(pDevice, pbyFrame, iSANodeIndex) == true) {
403 return false;
407 if (IS_FC_WEP(pbyFrame)) {
408 bool bRxDecryOK = false;
410 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"rx WEP pkt\n");
411 bIsWEP = true;
412 if ((pDevice->bEnableHostWEP) && (iSANodeIndex >= 0)) {
413 pKey = &STempKey;
414 pKey->byCipherSuite = pMgmt->sNodeDBTable[iSANodeIndex].byCipherSuite;
415 pKey->dwKeyIndex = pMgmt->sNodeDBTable[iSANodeIndex].dwKeyIndex;
416 pKey->uKeyLength = pMgmt->sNodeDBTable[iSANodeIndex].uWepKeyLength;
417 pKey->dwTSC47_16 = pMgmt->sNodeDBTable[iSANodeIndex].dwTSC47_16;
418 pKey->wTSC15_0 = pMgmt->sNodeDBTable[iSANodeIndex].wTSC15_0;
419 memcpy(pKey->abyKey,
420 &pMgmt->sNodeDBTable[iSANodeIndex].abyWepKey[0],
421 pKey->uKeyLength
424 bRxDecryOK = s_bHostWepRxEncryption(pDevice,
425 pbyFrame,
426 FrameSize,
427 pbyRsr,
428 pMgmt->sNodeDBTable[iSANodeIndex].bOnFly,
429 pKey,
430 pbyNewRsr,
431 &bExtIV,
432 &wRxTSC15_0,
433 &dwRxTSC47_16);
434 } else {
435 bRxDecryOK = s_bHandleRxEncryption(pDevice,
436 pbyFrame,
437 FrameSize,
438 pbyRsr,
439 pbyNewRsr,
440 &pKey,
441 &bExtIV,
442 &wRxTSC15_0,
443 &dwRxTSC47_16);
446 if (bRxDecryOK) {
447 if ((*pbyNewRsr & NEWRSR_DECRYPTOK) == 0) {
448 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"ICV Fail\n");
449 if ( (pMgmt->eAuthenMode == WMAC_AUTH_WPA) ||
450 (pMgmt->eAuthenMode == WMAC_AUTH_WPAPSK) ||
451 (pMgmt->eAuthenMode == WMAC_AUTH_WPANONE) ||
452 (pMgmt->eAuthenMode == WMAC_AUTH_WPA2) ||
453 (pMgmt->eAuthenMode == WMAC_AUTH_WPA2PSK)) {
455 if ((pKey != NULL) && (pKey->byCipherSuite == KEY_CTL_TKIP)) {
456 pDevice->s802_11Counter.TKIPICVErrors++;
457 } else if ((pKey != NULL) && (pKey->byCipherSuite == KEY_CTL_CCMP)) {
458 pDevice->s802_11Counter.CCMPDecryptErrors++;
459 } else if ((pKey != NULL) && (pKey->byCipherSuite == KEY_CTL_WEP)) {
460 // pDevice->s802_11Counter.WEPICVErrorCount.QuadPart++;
463 return false;
465 } else {
466 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"WEP Func Fail\n");
467 return false;
469 if ((pKey != NULL) && (pKey->byCipherSuite == KEY_CTL_CCMP))
470 FrameSize -= 8; // Message Integrity Code
471 else
472 FrameSize -= 4; // 4 is ICV
476 // RX OK
478 /* remove the FCS/CRC length */
479 FrameSize -= ETH_FCS_LEN;
481 if ( !(*pbyRsr & (RSR_ADDRBROAD | RSR_ADDRMULTI)) && // unicast address
482 (IS_FRAGMENT_PKT((pbyFrame)))
484 // defragment
485 bDeFragRx = WCTLbHandleFragment(pDevice, (struct ieee80211_hdr *) (pbyFrame), FrameSize, bIsWEP, bExtIV);
486 pDevice->s802_11Counter.ReceivedFragmentCount++;
487 if (bDeFragRx) {
488 // defrag complete
489 // TODO skb, pbyFrame
490 skb = pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].skb;
491 FrameSize = pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].cbFrameLength;
492 pbyFrame = skb->data + 8;
494 else {
495 return false;
500 // Management & Control frame Handle
502 if ((IS_TYPE_DATA((pbyFrame))) == false) {
503 // Handle Control & Manage Frame
505 if (IS_TYPE_MGMT((pbyFrame))) {
506 u8 * pbyData1;
507 u8 * pbyData2;
509 pRxPacket = &(pRCB->sMngPacket);
510 pRxPacket->p80211Header = (PUWLAN_80211HDR)(pbyFrame);
511 pRxPacket->cbMPDULen = FrameSize;
512 pRxPacket->uRSSI = *pbyRSSI;
513 pRxPacket->bySQ = *pbySQ;
514 pRxPacket->qwLocalTSF = cpu_to_le64(*pqwTSFTime);
515 if (bIsWEP) {
516 // strip IV
517 pbyData1 = WLAN_HDR_A3_DATA_PTR(pbyFrame);
518 pbyData2 = WLAN_HDR_A3_DATA_PTR(pbyFrame) + 4;
519 for (ii = 0; ii < (FrameSize - 4); ii++) {
520 *pbyData1 = *pbyData2;
521 pbyData1++;
522 pbyData2++;
526 pRxPacket->byRxRate = s_byGetRateIdx(*pbyRxRate);
528 if ( *pbyRxSts == 0 ) {
529 //Discard beacon packet which channel is 0
530 if ( (WLAN_GET_FC_FSTYPE((pRxPacket->p80211Header->sA3.wFrameCtl)) == WLAN_FSTYPE_BEACON) ||
531 (WLAN_GET_FC_FSTYPE((pRxPacket->p80211Header->sA3.wFrameCtl)) == WLAN_FSTYPE_PROBERESP) ) {
532 return false;
535 pRxPacket->byRxChannel = (*pbyRxSts) >> 2;
537 // hostap Deamon handle 802.11 management
538 if (pDevice->bEnableHostapd) {
539 skb->dev = pDevice->apdev;
540 //skb->data += 4;
541 //skb->tail += 4;
542 skb->data += 8;
543 skb->tail += 8;
544 skb_put(skb, FrameSize);
545 skb_reset_mac_header(skb);
546 skb->pkt_type = PACKET_OTHERHOST;
547 skb->protocol = htons(ETH_P_802_2);
548 memset(skb->cb, 0, sizeof(skb->cb));
549 netif_rx(skb);
550 return true;
554 // Insert the RCB in the Recv Mng list
556 EnqueueRCB(pDevice->FirstRecvMngList, pDevice->LastRecvMngList, pRCBIndicate);
557 pDevice->NumRecvMngList++;
558 if ( bDeFragRx == false) {
559 pRCB->Ref++;
561 if (pDevice->bIsRxMngWorkItemQueued == false) {
562 pDevice->bIsRxMngWorkItemQueued = true;
563 tasklet_schedule(&pDevice->RxMngWorkItem);
567 else {
568 // Control Frame
570 return false;
572 else {
573 if (pMgmt->eCurrMode == WMAC_MODE_ESS_AP) {
574 //In AP mode, hw only check addr1(BSSID or RA) if equal to local MAC.
575 if ( !(*pbyRsr & RSR_BSSIDOK)) {
576 if (bDeFragRx) {
577 if (!device_alloc_frag_buf(pDevice, &pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx])) {
578 DBG_PRT(MSG_LEVEL_ERR,KERN_ERR "%s: can not alloc more frag bufs\n",
579 pDevice->dev->name);
582 return false;
585 else {
586 // discard DATA packet while not associate || BSSID error
587 if ((pDevice->bLinkPass == false) ||
588 !(*pbyRsr & RSR_BSSIDOK)) {
589 if (bDeFragRx) {
590 if (!device_alloc_frag_buf(pDevice, &pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx])) {
591 DBG_PRT(MSG_LEVEL_ERR,KERN_ERR "%s: can not alloc more frag bufs\n",
592 pDevice->dev->name);
595 return false;
597 //mike add:station mode check eapol-key challenge--->
599 u8 Protocol_Version; //802.1x Authentication
600 u8 Packet_Type; //802.1x Authentication
601 u8 Descriptor_type;
602 u16 Key_info;
603 if (bIsWEP)
604 cbIVOffset = 8;
605 else
606 cbIVOffset = 0;
607 wEtherType = (skb->data[cbIVOffset + 8 + 24 + 6] << 8) |
608 skb->data[cbIVOffset + 8 + 24 + 6 + 1];
609 Protocol_Version = skb->data[cbIVOffset + 8 + 24 + 6 + 1 +1];
610 Packet_Type = skb->data[cbIVOffset + 8 + 24 + 6 + 1 +1+1];
611 if (wEtherType == ETH_P_PAE) { //Protocol Type in LLC-Header
612 if(((Protocol_Version==1) ||(Protocol_Version==2)) &&
613 (Packet_Type==3)) { //802.1x OR eapol-key challenge frame receive
614 bRxeapol_key = true;
615 Descriptor_type = skb->data[cbIVOffset + 8 + 24 + 6 + 1 +1+1+1+2];
616 Key_info = (skb->data[cbIVOffset + 8 + 24 + 6 + 1 +1+1+1+2+1]<<8) |skb->data[cbIVOffset + 8 + 24 + 6 + 1 +1+1+1+2+2] ;
617 if(Descriptor_type==2) { //RSN
618 // printk("WPA2_Rx_eapol-key_info<-----:%x\n",Key_info);
620 else if(Descriptor_type==254) {
621 // printk("WPA_Rx_eapol-key_info<-----:%x\n",Key_info);
626 //mike add:station mode check eapol-key challenge<---
630 // Data frame Handle
632 if (pDevice->bEnablePSMode) {
633 if (IS_FC_MOREDATA((pbyFrame))) {
634 if (*pbyRsr & RSR_ADDROK) {
635 //PSbSendPSPOLL((PSDevice)pDevice);
638 else {
639 if (pMgmt->bInTIMWake == true) {
640 pMgmt->bInTIMWake = false;
645 // Now it only supports 802.11g Infrastructure Mode, and support rate must up to 54 Mbps
646 if (pDevice->bDiversityEnable && (FrameSize>50) &&
647 (pDevice->eOPMode == OP_MODE_INFRASTRUCTURE) &&
648 (pDevice->bLinkPass == true)) {
649 BBvAntennaDiversity(pDevice, s_byGetRateIdx(*pbyRxRate), 0);
652 // ++++++++ For BaseBand Algorithm +++++++++++++++
653 pDevice->uCurrRSSI = *pbyRSSI;
654 pDevice->byCurrSQ = *pbySQ;
656 // todo
658 if ((*pbyRSSI != 0) &&
659 (pMgmt->pCurrBSS!=NULL)) {
660 RFvRSSITodBm(pDevice, *pbyRSSI, &ldBm);
661 // Monitor if RSSI is too strong.
662 pMgmt->pCurrBSS->byRSSIStatCnt++;
663 pMgmt->pCurrBSS->byRSSIStatCnt %= RSSI_STAT_COUNT;
664 pMgmt->pCurrBSS->ldBmAverage[pMgmt->pCurrBSS->byRSSIStatCnt] = ldBm;
665 for (ii = 0; ii < RSSI_STAT_COUNT; ii++) {
666 if (pMgmt->pCurrBSS->ldBmAverage[ii] != 0) {
667 pMgmt->pCurrBSS->ldBmMAX =
668 max(pMgmt->pCurrBSS->ldBmAverage[ii], ldBm);
674 // -----------------------------------------------
676 if ((pMgmt->eCurrMode == WMAC_MODE_ESS_AP) && (pDevice->bEnable8021x == true)){
677 u8 abyMacHdr[24];
679 // Only 802.1x packet incoming allowed
680 if (bIsWEP)
681 cbIVOffset = 8;
682 else
683 cbIVOffset = 0;
684 wEtherType = (skb->data[cbIVOffset + 8 + 24 + 6] << 8) |
685 skb->data[cbIVOffset + 8 + 24 + 6 + 1];
687 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"wEtherType = %04x \n", wEtherType);
688 if (wEtherType == ETH_P_PAE) {
689 skb->dev = pDevice->apdev;
691 if (bIsWEP == true) {
692 // strip IV header(8)
693 memcpy(&abyMacHdr[0], (skb->data + 8), 24);
694 memcpy((skb->data + 8 + cbIVOffset), &abyMacHdr[0], 24);
697 skb->data += (cbIVOffset + 8);
698 skb->tail += (cbIVOffset + 8);
699 skb_put(skb, FrameSize);
700 skb_reset_mac_header(skb);
701 skb->pkt_type = PACKET_OTHERHOST;
702 skb->protocol = htons(ETH_P_802_2);
703 memset(skb->cb, 0, sizeof(skb->cb));
704 netif_rx(skb);
705 return true;
708 // check if 802.1x authorized
709 if (!(pMgmt->sNodeDBTable[iSANodeIndex].dwFlags & WLAN_STA_AUTHORIZED))
710 return false;
713 if ((pKey != NULL) && (pKey->byCipherSuite == KEY_CTL_TKIP)) {
714 if (bIsWEP) {
715 FrameSize -= 8; //MIC
719 //--------------------------------------------------------------------------------
720 // Soft MIC
721 if ((pKey != NULL) && (pKey->byCipherSuite == KEY_CTL_TKIP)) {
722 if (bIsWEP) {
723 u32 * pdwMIC_L;
724 u32 * pdwMIC_R;
725 u32 dwMIC_Priority;
726 u32 dwMICKey0 = 0, dwMICKey1 = 0;
727 u32 dwLocalMIC_L = 0;
728 u32 dwLocalMIC_R = 0;
730 if (pMgmt->eCurrMode == WMAC_MODE_ESS_AP) {
731 dwMICKey0 = cpu_to_le32(*(u32 *)(&pKey->abyKey[24]));
732 dwMICKey1 = cpu_to_le32(*(u32 *)(&pKey->abyKey[28]));
734 else {
735 if (pMgmt->eAuthenMode == WMAC_AUTH_WPANONE) {
736 dwMICKey0 = cpu_to_le32(*(u32 *)(&pKey->abyKey[16]));
737 dwMICKey1 = cpu_to_le32(*(u32 *)(&pKey->abyKey[20]));
738 } else if ((pKey->dwKeyIndex & BIT28) == 0) {
739 dwMICKey0 = cpu_to_le32(*(u32 *)(&pKey->abyKey[16]));
740 dwMICKey1 = cpu_to_le32(*(u32 *)(&pKey->abyKey[20]));
741 } else {
742 dwMICKey0 = cpu_to_le32(*(u32 *)(&pKey->abyKey[24]));
743 dwMICKey1 = cpu_to_le32(*(u32 *)(&pKey->abyKey[28]));
747 MIC_vInit(dwMICKey0, dwMICKey1);
748 MIC_vAppend((u8 *)&(pDevice->sRxEthHeader.h_dest[0]), 12);
749 dwMIC_Priority = 0;
750 MIC_vAppend((u8 *)&dwMIC_Priority, 4);
751 // 4 is Rcv buffer header, 24 is MAC Header, and 8 is IV and Ext IV.
752 MIC_vAppend((u8 *)(skb->data + 8 + WLAN_HDR_ADDR3_LEN + 8),
753 FrameSize - WLAN_HDR_ADDR3_LEN - 8);
754 MIC_vGetMIC(&dwLocalMIC_L, &dwLocalMIC_R);
755 MIC_vUnInit();
757 pdwMIC_L = (u32 *)(skb->data + 8 + FrameSize);
758 pdwMIC_R = (u32 *)(skb->data + 8 + FrameSize + 4);
760 if ((cpu_to_le32(*pdwMIC_L) != dwLocalMIC_L) || (cpu_to_le32(*pdwMIC_R) != dwLocalMIC_R) ||
761 (pDevice->bRxMICFail == true)) {
762 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"MIC comparison is fail!\n");
763 pDevice->bRxMICFail = false;
764 //pDevice->s802_11Counter.TKIPLocalMICFailures.QuadPart++;
765 pDevice->s802_11Counter.TKIPLocalMICFailures++;
766 if (bDeFragRx) {
767 if (!device_alloc_frag_buf(pDevice, &pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx])) {
768 DBG_PRT(MSG_LEVEL_ERR,KERN_ERR "%s: can not alloc more frag bufs\n",
769 pDevice->dev->name);
772 //send event to wpa_supplicant
773 //if(pDevice->bWPASuppWextEnabled == true)
775 union iwreq_data wrqu;
776 struct iw_michaelmicfailure ev;
777 int keyidx = pbyFrame[cbHeaderSize+3] >> 6; //top two-bits
778 memset(&ev, 0, sizeof(ev));
779 ev.flags = keyidx & IW_MICFAILURE_KEY_ID;
780 if ((pMgmt->eCurrMode == WMAC_MODE_ESS_STA) &&
781 (pMgmt->eCurrState == WMAC_STATE_ASSOC) &&
782 (*pbyRsr & (RSR_ADDRBROAD | RSR_ADDRMULTI)) == 0) {
783 ev.flags |= IW_MICFAILURE_PAIRWISE;
784 } else {
785 ev.flags |= IW_MICFAILURE_GROUP;
788 ev.src_addr.sa_family = ARPHRD_ETHER;
789 memcpy(ev.src_addr.sa_data, pMACHeader->addr2, ETH_ALEN);
790 memset(&wrqu, 0, sizeof(wrqu));
791 wrqu.data.length = sizeof(ev);
792 PRINT_K("wireless_send_event--->IWEVMICHAELMICFAILURE\n");
793 wireless_send_event(pDevice->dev, IWEVMICHAELMICFAILURE, &wrqu, (char *)&ev);
797 return false;
801 } //---end of SOFT MIC-----------------------------------------------------------------------
803 // ++++++++++ Reply Counter Check +++++++++++++
805 if ((pKey != NULL) && ((pKey->byCipherSuite == KEY_CTL_TKIP) ||
806 (pKey->byCipherSuite == KEY_CTL_CCMP))) {
807 if (bIsWEP) {
808 u16 wLocalTSC15_0 = 0;
809 u32 dwLocalTSC47_16 = 0;
810 unsigned long long RSC = 0;
811 // endian issues
812 RSC = *((unsigned long long *) &(pKey->KeyRSC));
813 wLocalTSC15_0 = (u16) RSC;
814 dwLocalTSC47_16 = (u32) (RSC>>16);
816 RSC = dwRxTSC47_16;
817 RSC <<= 16;
818 RSC += wRxTSC15_0;
819 memcpy(&(pKey->KeyRSC), &RSC, sizeof(u64));
821 if (pDevice->vnt_mgmt.eCurrMode == WMAC_MODE_ESS_STA &&
822 pDevice->vnt_mgmt.eCurrState == WMAC_STATE_ASSOC) {
823 /* check RSC */
824 if ( (wRxTSC15_0 < wLocalTSC15_0) &&
825 (dwRxTSC47_16 <= dwLocalTSC47_16) &&
826 !((dwRxTSC47_16 == 0) && (dwLocalTSC47_16 == 0xFFFFFFFF))) {
827 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"TSC is illegal~~!\n ");
828 if (pKey->byCipherSuite == KEY_CTL_TKIP)
829 //pDevice->s802_11Counter.TKIPReplays.QuadPart++;
830 pDevice->s802_11Counter.TKIPReplays++;
831 else
832 //pDevice->s802_11Counter.CCMPReplays.QuadPart++;
833 pDevice->s802_11Counter.CCMPReplays++;
835 if (bDeFragRx) {
836 if (!device_alloc_frag_buf(pDevice, &pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx])) {
837 DBG_PRT(MSG_LEVEL_ERR,KERN_ERR "%s: can not alloc more frag bufs\n",
838 pDevice->dev->name);
841 return false;
845 } // ----- End of Reply Counter Check --------------------------
847 s_vProcessRxMACHeader(pDevice, (u8 *)(skb->data+8), FrameSize, bIsWEP, bExtIV, &cbHeaderOffset);
848 FrameSize -= cbHeaderOffset;
849 cbHeaderOffset += 8; // 8 is Rcv buffer header
851 // Null data, framesize = 12
852 if (FrameSize < 12)
853 return false;
855 if (pMgmt->eCurrMode == WMAC_MODE_ESS_AP) {
856 if (s_bAPModeRxData(pDevice,
857 skb,
858 FrameSize,
859 cbHeaderOffset,
860 iSANodeIndex,
861 iDANodeIndex
862 ) == false) {
864 if (bDeFragRx) {
865 if (!device_alloc_frag_buf(pDevice, &pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx])) {
866 DBG_PRT(MSG_LEVEL_ERR,KERN_ERR "%s: can not alloc more frag bufs\n",
867 pDevice->dev->name);
870 return false;
875 skb->data += cbHeaderOffset;
876 skb->tail += cbHeaderOffset;
877 skb_put(skb, FrameSize);
878 skb->protocol=eth_type_trans(skb, skb->dev);
879 skb->ip_summed=CHECKSUM_NONE;
880 pStats->rx_bytes +=skb->len;
881 pStats->rx_packets++;
882 netif_rx(skb);
883 if (bDeFragRx) {
884 if (!device_alloc_frag_buf(pDevice, &pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx])) {
885 DBG_PRT(MSG_LEVEL_ERR,KERN_ERR "%s: can not alloc more frag bufs\n",
886 pDevice->dev->name);
888 return false;
891 return true;
894 static int s_bAPModeRxCtl(struct vnt_private *pDevice, u8 *pbyFrame,
895 s32 iSANodeIndex)
897 struct vnt_manager *pMgmt = &pDevice->vnt_mgmt;
898 struct ieee80211_hdr *p802_11Header;
899 CMD_STATUS Status;
901 if (IS_CTL_PSPOLL(pbyFrame) || !IS_TYPE_CONTROL(pbyFrame)) {
903 p802_11Header = (struct ieee80211_hdr *) (pbyFrame);
904 if (!IS_TYPE_MGMT(pbyFrame)) {
906 // Data & PS-Poll packet
907 // check frame class
908 if (iSANodeIndex > 0) {
909 // frame class 3 fliter & checking
910 if (pMgmt->sNodeDBTable[iSANodeIndex].eNodeState < NODE_AUTH) {
911 // send deauth notification
912 // reason = (6) class 2 received from nonauth sta
913 vMgrDeAuthenBeginSta(pDevice,
914 pMgmt,
915 (u8 *)(p802_11Header->addr2),
916 (WLAN_MGMT_REASON_CLASS2_NONAUTH),
917 &Status
919 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "dpc: send vMgrDeAuthenBeginSta 1\n");
920 return true;
922 if (pMgmt->sNodeDBTable[iSANodeIndex].eNodeState < NODE_ASSOC) {
923 // send deassoc notification
924 // reason = (7) class 3 received from nonassoc sta
925 vMgrDisassocBeginSta(pDevice,
926 pMgmt,
927 (u8 *)(p802_11Header->addr2),
928 (WLAN_MGMT_REASON_CLASS3_NONASSOC),
929 &Status
931 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "dpc: send vMgrDisassocBeginSta 2\n");
932 return true;
935 if (pMgmt->sNodeDBTable[iSANodeIndex].bPSEnable) {
936 // delcare received ps-poll event
937 if (IS_CTL_PSPOLL(pbyFrame)) {
938 pMgmt->sNodeDBTable[iSANodeIndex].bRxPSPoll = true;
939 bScheduleCommand((void *) pDevice,
940 WLAN_CMD_RX_PSPOLL,
941 NULL);
942 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "dpc: WLAN_CMD_RX_PSPOLL 1\n");
944 else {
945 // check Data PS state
946 // if PW bit off, send out all PS bufferring packets.
947 if (!IS_FC_POWERMGT(pbyFrame)) {
948 pMgmt->sNodeDBTable[iSANodeIndex].bPSEnable = false;
949 pMgmt->sNodeDBTable[iSANodeIndex].bRxPSPoll = true;
950 bScheduleCommand((void *) pDevice,
951 WLAN_CMD_RX_PSPOLL,
952 NULL);
953 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "dpc: WLAN_CMD_RX_PSPOLL 2\n");
957 else {
958 if (IS_FC_POWERMGT(pbyFrame)) {
959 pMgmt->sNodeDBTable[iSANodeIndex].bPSEnable = true;
960 // Once if STA in PS state, enable multicast bufferring
961 pMgmt->sNodeDBTable[0].bPSEnable = true;
963 else {
964 // clear all pending PS frame.
965 if (pMgmt->sNodeDBTable[iSANodeIndex].wEnQueueCnt > 0) {
966 pMgmt->sNodeDBTable[iSANodeIndex].bPSEnable = false;
967 pMgmt->sNodeDBTable[iSANodeIndex].bRxPSPoll = true;
968 bScheduleCommand((void *) pDevice,
969 WLAN_CMD_RX_PSPOLL,
970 NULL);
971 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "dpc: WLAN_CMD_RX_PSPOLL 3\n");
977 else {
978 vMgrDeAuthenBeginSta(pDevice,
979 pMgmt,
980 (u8 *)(p802_11Header->addr2),
981 (WLAN_MGMT_REASON_CLASS2_NONAUTH),
982 &Status
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->addr3);
987 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "ADDR2:%pM\n",
988 p802_11Header->addr2);
989 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "ADDR1:%pM\n",
990 p802_11Header->addr1);
991 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "dpc: frame_control= %x\n", p802_11Header->frame_control);
992 return true;
996 return false;
1000 static int s_bHandleRxEncryption(struct vnt_private *pDevice, u8 *pbyFrame,
1001 u32 FrameSize, u8 *pbyRsr, u8 *pbyNewRsr, PSKeyItem *pKeyOut,
1002 s32 *pbExtIV, u16 *pwRxTSC15_0, u32 *pdwRxTSC47_16)
1004 struct vnt_manager *pMgmt = &pDevice->vnt_mgmt;
1005 u32 PayloadLen = FrameSize;
1006 u8 *pbyIV;
1007 u8 byKeyIdx;
1008 PSKeyItem pKey = NULL;
1009 u8 byDecMode = KEY_CTL_WEP;
1011 *pwRxTSC15_0 = 0;
1012 *pdwRxTSC47_16 = 0;
1014 pbyIV = pbyFrame + WLAN_HDR_ADDR3_LEN;
1015 if ( WLAN_GET_FC_TODS(*(u16 *)pbyFrame) &&
1016 WLAN_GET_FC_FROMDS(*(u16 *)pbyFrame) ) {
1017 pbyIV += 6; // 6 is 802.11 address4
1018 PayloadLen -= 6;
1020 byKeyIdx = (*(pbyIV+3) & 0xc0);
1021 byKeyIdx >>= 6;
1022 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"\nKeyIdx: %d\n", byKeyIdx);
1024 if ((pMgmt->eAuthenMode == WMAC_AUTH_WPA) ||
1025 (pMgmt->eAuthenMode == WMAC_AUTH_WPAPSK) ||
1026 (pMgmt->eAuthenMode == WMAC_AUTH_WPANONE) ||
1027 (pMgmt->eAuthenMode == WMAC_AUTH_WPA2) ||
1028 (pMgmt->eAuthenMode == WMAC_AUTH_WPA2PSK)) {
1029 if (((*pbyRsr & (RSR_ADDRBROAD | RSR_ADDRMULTI)) == 0) &&
1030 (pMgmt->byCSSPK != KEY_CTL_NONE)) {
1031 // unicast pkt use pairwise key
1032 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"unicast pkt\n");
1033 if (KeybGetKey(&(pDevice->sKey), pDevice->abyBSSID, 0xFFFFFFFF, &pKey) == true) {
1034 if (pMgmt->byCSSPK == KEY_CTL_TKIP)
1035 byDecMode = KEY_CTL_TKIP;
1036 else if (pMgmt->byCSSPK == KEY_CTL_CCMP)
1037 byDecMode = KEY_CTL_CCMP;
1039 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"unicast pkt: %d, %p\n", byDecMode, pKey);
1040 } else {
1041 // use group key
1042 KeybGetKey(&(pDevice->sKey), pDevice->abyBSSID, byKeyIdx, &pKey);
1043 if (pMgmt->byCSSGK == KEY_CTL_TKIP)
1044 byDecMode = KEY_CTL_TKIP;
1045 else if (pMgmt->byCSSGK == KEY_CTL_CCMP)
1046 byDecMode = KEY_CTL_CCMP;
1047 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"group pkt: %d, %d, %p\n", byKeyIdx, byDecMode, pKey);
1050 // our WEP only support Default Key
1051 if (pKey == NULL) {
1052 // use default group key
1053 KeybGetKey(&(pDevice->sKey), pDevice->abyBroadcastAddr, byKeyIdx, &pKey);
1054 if (pMgmt->byCSSGK == KEY_CTL_TKIP)
1055 byDecMode = KEY_CTL_TKIP;
1056 else if (pMgmt->byCSSGK == KEY_CTL_CCMP)
1057 byDecMode = KEY_CTL_CCMP;
1059 *pKeyOut = pKey;
1061 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"AES:%d %d %d\n", pMgmt->byCSSPK, pMgmt->byCSSGK, byDecMode);
1063 if (pKey == NULL) {
1064 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"pKey == NULL\n");
1065 if (byDecMode == KEY_CTL_WEP) {
1066 // pDevice->s802_11Counter.WEPUndecryptableCount.QuadPart++;
1067 } else if (pDevice->bLinkPass == true) {
1068 // pDevice->s802_11Counter.DecryptFailureCount.QuadPart++;
1070 return false;
1072 if (byDecMode != pKey->byCipherSuite) {
1073 if (byDecMode == KEY_CTL_WEP) {
1074 // pDevice->s802_11Counter.WEPUndecryptableCount.QuadPart++;
1075 } else if (pDevice->bLinkPass == true) {
1076 // pDevice->s802_11Counter.DecryptFailureCount.QuadPart++;
1078 *pKeyOut = NULL;
1079 return false;
1081 if (byDecMode == KEY_CTL_WEP) {
1082 // handle WEP
1083 if ((pDevice->byLocalID <= REV_ID_VT3253_A1) ||
1084 (((PSKeyTable)(pKey->pvKeyTable))->bSoftWEP == true)) {
1085 // Software WEP
1086 // 1. 3253A
1087 // 2. WEP 256
1089 PayloadLen -= (WLAN_HDR_ADDR3_LEN + 4 + 4); // 24 is 802.11 header,4 is IV, 4 is crc
1090 memcpy(pDevice->abyPRNG, pbyIV, 3);
1091 memcpy(pDevice->abyPRNG + 3, pKey->abyKey, pKey->uKeyLength);
1092 rc4_init(&pDevice->SBox, pDevice->abyPRNG, pKey->uKeyLength + 3);
1093 rc4_encrypt(&pDevice->SBox, pbyIV+4, pbyIV+4, PayloadLen);
1095 if (ETHbIsBufferCrc32Ok(pbyIV+4, PayloadLen)) {
1096 *pbyNewRsr |= NEWRSR_DECRYPTOK;
1099 } else if ((byDecMode == KEY_CTL_TKIP) ||
1100 (byDecMode == KEY_CTL_CCMP)) {
1101 // TKIP/AES
1103 PayloadLen -= (WLAN_HDR_ADDR3_LEN + 8 + 4); // 24 is 802.11 header, 8 is IV&ExtIV, 4 is crc
1104 *pdwRxTSC47_16 = cpu_to_le32(*(u32 *)(pbyIV + 4));
1105 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"ExtIV: %x\n", *pdwRxTSC47_16);
1106 if (byDecMode == KEY_CTL_TKIP) {
1107 *pwRxTSC15_0 = cpu_to_le16(MAKEWORD(*(pbyIV+2), *pbyIV));
1108 } else {
1109 *pwRxTSC15_0 = cpu_to_le16(*(u16 *)pbyIV);
1111 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"TSC0_15: %x\n", *pwRxTSC15_0);
1113 if ((byDecMode == KEY_CTL_TKIP) &&
1114 (pDevice->byLocalID <= REV_ID_VT3253_A1)) {
1115 // Software TKIP
1116 // 1. 3253 A
1117 struct ieee80211_hdr *pMACHeader = (struct ieee80211_hdr *) (pbyFrame);
1118 TKIPvMixKey(pKey->abyKey, pMACHeader->addr2, *pwRxTSC15_0, *pdwRxTSC47_16, pDevice->abyPRNG);
1119 rc4_init(&pDevice->SBox, pDevice->abyPRNG, TKIP_KEY_LEN);
1120 rc4_encrypt(&pDevice->SBox, pbyIV+8, pbyIV+8, PayloadLen);
1121 if (ETHbIsBufferCrc32Ok(pbyIV+8, PayloadLen)) {
1122 *pbyNewRsr |= NEWRSR_DECRYPTOK;
1123 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"ICV OK!\n");
1124 } else {
1125 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"ICV FAIL!!!\n");
1126 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"PayloadLen = %d\n", PayloadLen);
1129 }// end of TKIP/AES
1131 if ((*(pbyIV+3) & 0x20) != 0)
1132 *pbExtIV = true;
1133 return true;
1136 static int s_bHostWepRxEncryption(struct vnt_private *pDevice, u8 *pbyFrame,
1137 u32 FrameSize, u8 *pbyRsr, int bOnFly, PSKeyItem pKey, u8 *pbyNewRsr,
1138 s32 *pbExtIV, u16 *pwRxTSC15_0, u32 *pdwRxTSC47_16)
1140 struct vnt_manager *pMgmt = &pDevice->vnt_mgmt;
1141 struct ieee80211_hdr *pMACHeader;
1142 u32 PayloadLen = FrameSize;
1143 u8 *pbyIV;
1144 u8 byKeyIdx;
1145 u8 byDecMode = KEY_CTL_WEP;
1147 *pwRxTSC15_0 = 0;
1148 *pdwRxTSC47_16 = 0;
1150 pbyIV = pbyFrame + WLAN_HDR_ADDR3_LEN;
1151 if ( WLAN_GET_FC_TODS(*(u16 *)pbyFrame) &&
1152 WLAN_GET_FC_FROMDS(*(u16 *)pbyFrame) ) {
1153 pbyIV += 6; // 6 is 802.11 address4
1154 PayloadLen -= 6;
1156 byKeyIdx = (*(pbyIV+3) & 0xc0);
1157 byKeyIdx >>= 6;
1158 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"\nKeyIdx: %d\n", byKeyIdx);
1160 if (pMgmt->byCSSGK == KEY_CTL_TKIP)
1161 byDecMode = KEY_CTL_TKIP;
1162 else if (pMgmt->byCSSGK == KEY_CTL_CCMP)
1163 byDecMode = KEY_CTL_CCMP;
1165 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"AES:%d %d %d\n", pMgmt->byCSSPK, pMgmt->byCSSGK, byDecMode);
1167 if (byDecMode != pKey->byCipherSuite) {
1168 if (byDecMode == KEY_CTL_WEP) {
1169 // pDevice->s802_11Counter.WEPUndecryptableCount.QuadPart++;
1170 } else if (pDevice->bLinkPass == true) {
1171 // pDevice->s802_11Counter.DecryptFailureCount.QuadPart++;
1173 return false;
1176 if (byDecMode == KEY_CTL_WEP) {
1177 // handle WEP
1178 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"byDecMode == KEY_CTL_WEP\n");
1179 if ((pDevice->byLocalID <= REV_ID_VT3253_A1) ||
1180 (((PSKeyTable)(pKey->pvKeyTable))->bSoftWEP == true) ||
1181 (bOnFly == false)) {
1182 // Software WEP
1183 // 1. 3253A
1184 // 2. WEP 256
1185 // 3. NotOnFly
1187 PayloadLen -= (WLAN_HDR_ADDR3_LEN + 4 + 4); // 24 is 802.11 header,4 is IV, 4 is crc
1188 memcpy(pDevice->abyPRNG, pbyIV, 3);
1189 memcpy(pDevice->abyPRNG + 3, pKey->abyKey, pKey->uKeyLength);
1190 rc4_init(&pDevice->SBox, pDevice->abyPRNG, pKey->uKeyLength + 3);
1191 rc4_encrypt(&pDevice->SBox, pbyIV+4, pbyIV+4, PayloadLen);
1193 if (ETHbIsBufferCrc32Ok(pbyIV+4, PayloadLen)) {
1194 *pbyNewRsr |= NEWRSR_DECRYPTOK;
1197 } else if ((byDecMode == KEY_CTL_TKIP) ||
1198 (byDecMode == KEY_CTL_CCMP)) {
1199 // TKIP/AES
1201 PayloadLen -= (WLAN_HDR_ADDR3_LEN + 8 + 4); // 24 is 802.11 header, 8 is IV&ExtIV, 4 is crc
1202 *pdwRxTSC47_16 = cpu_to_le32(*(u32 *)(pbyIV + 4));
1203 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"ExtIV: %x\n", *pdwRxTSC47_16);
1205 if (byDecMode == KEY_CTL_TKIP) {
1206 *pwRxTSC15_0 = cpu_to_le16(MAKEWORD(*(pbyIV+2), *pbyIV));
1207 } else {
1208 *pwRxTSC15_0 = cpu_to_le16(*(u16 *)pbyIV);
1210 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"TSC0_15: %x\n", *pwRxTSC15_0);
1212 if (byDecMode == KEY_CTL_TKIP) {
1214 if ((pDevice->byLocalID <= REV_ID_VT3253_A1) || (bOnFly == false)) {
1215 // Software TKIP
1216 // 1. 3253 A
1217 // 2. NotOnFly
1218 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"soft KEY_CTL_TKIP \n");
1219 pMACHeader = (struct ieee80211_hdr *) (pbyFrame);
1220 TKIPvMixKey(pKey->abyKey, pMACHeader->addr2, *pwRxTSC15_0, *pdwRxTSC47_16, pDevice->abyPRNG);
1221 rc4_init(&pDevice->SBox, pDevice->abyPRNG, TKIP_KEY_LEN);
1222 rc4_encrypt(&pDevice->SBox, pbyIV+8, pbyIV+8, PayloadLen);
1223 if (ETHbIsBufferCrc32Ok(pbyIV+8, PayloadLen)) {
1224 *pbyNewRsr |= NEWRSR_DECRYPTOK;
1225 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"ICV OK!\n");
1226 } else {
1227 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"ICV FAIL!!!\n");
1228 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"PayloadLen = %d\n", PayloadLen);
1233 if (byDecMode == KEY_CTL_CCMP) {
1234 if (bOnFly == false) {
1235 // Software CCMP
1236 // NotOnFly
1237 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"soft KEY_CTL_CCMP\n");
1238 if (AESbGenCCMP(pKey->abyKey, pbyFrame, FrameSize)) {
1239 *pbyNewRsr |= NEWRSR_DECRYPTOK;
1240 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"CCMP MIC compare OK!\n");
1241 } else {
1242 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"CCMP MIC fail!\n");
1247 }// end of TKIP/AES
1249 if ((*(pbyIV+3) & 0x20) != 0)
1250 *pbExtIV = true;
1251 return true;
1254 static int s_bAPModeRxData(struct vnt_private *pDevice, struct sk_buff *skb,
1255 u32 FrameSize, u32 cbHeaderOffset, s32 iSANodeIndex, s32 iDANodeIndex)
1257 struct sk_buff *skbcpy;
1258 struct vnt_manager *pMgmt = &pDevice->vnt_mgmt;
1259 int bRelayAndForward = false;
1260 int bRelayOnly = false;
1261 u8 byMask[8] = {1, 2, 4, 8, 0x10, 0x20, 0x40, 0x80};
1262 u16 wAID;
1264 if (FrameSize > CB_MAX_BUF_SIZE)
1265 return false;
1266 // check DA
1267 if (is_multicast_ether_addr((u8 *)(skb->data+cbHeaderOffset))) {
1268 if (pMgmt->sNodeDBTable[0].bPSEnable) {
1270 skbcpy = dev_alloc_skb((int)pDevice->rx_buf_sz);
1272 // if any node in PS mode, buffer packet until DTIM.
1273 if (skbcpy == NULL) {
1274 DBG_PRT(MSG_LEVEL_NOTICE, KERN_INFO "relay multicast no skb available \n");
1276 else {
1277 skbcpy->dev = pDevice->dev;
1278 skbcpy->len = FrameSize;
1279 memcpy(skbcpy->data, skb->data+cbHeaderOffset, FrameSize);
1280 skb_queue_tail(&(pMgmt->sNodeDBTable[0].sTxPSQueue), skbcpy);
1281 pMgmt->sNodeDBTable[0].wEnQueueCnt++;
1282 // set tx map
1283 pMgmt->abyPSTxMap[0] |= byMask[0];
1286 else {
1287 bRelayAndForward = true;
1290 else {
1291 // check if relay
1292 if (BSSbIsSTAInNodeDB(pDevice, (u8 *)(skb->data+cbHeaderOffset), &iDANodeIndex)) {
1293 if (pMgmt->sNodeDBTable[iDANodeIndex].eNodeState >= NODE_ASSOC) {
1294 if (pMgmt->sNodeDBTable[iDANodeIndex].bPSEnable) {
1295 // queue this skb until next PS tx, and then release.
1297 skb->data += cbHeaderOffset;
1298 skb->tail += cbHeaderOffset;
1299 skb_put(skb, FrameSize);
1300 skb_queue_tail(&pMgmt->sNodeDBTable[iDANodeIndex].sTxPSQueue, skb);
1302 pMgmt->sNodeDBTable[iDANodeIndex].wEnQueueCnt++;
1303 wAID = pMgmt->sNodeDBTable[iDANodeIndex].wAID;
1304 pMgmt->abyPSTxMap[wAID >> 3] |= byMask[wAID & 7];
1305 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "relay: index= %d, pMgmt->abyPSTxMap[%d]= %d\n",
1306 iDANodeIndex, (wAID >> 3), pMgmt->abyPSTxMap[wAID >> 3]);
1307 return true;
1309 else {
1310 bRelayOnly = true;
1316 if (bRelayOnly || bRelayAndForward) {
1317 // relay this packet right now
1318 if (bRelayAndForward)
1319 iDANodeIndex = 0;
1321 if ((pDevice->uAssocCount > 1) && (iDANodeIndex >= 0)) {
1322 bRelayPacketSend(pDevice, (u8 *) (skb->data + cbHeaderOffset),
1323 FrameSize, (unsigned int) iDANodeIndex);
1326 if (bRelayOnly)
1327 return false;
1329 // none associate, don't forward
1330 if (pDevice->uAssocCount == 0)
1331 return false;
1333 return true;
1336 void RXvWorkItem(struct vnt_private *pDevice)
1338 int ntStatus;
1339 struct vnt_rcb *pRCB = NULL;
1341 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"---->Rx Polling Thread\n");
1342 spin_lock_irq(&pDevice->lock);
1344 while ((pDevice->Flags & fMP_POST_READS) &&
1345 MP_IS_READY(pDevice) &&
1346 (pDevice->NumRecvFreeList != 0) ) {
1347 pRCB = pDevice->FirstRecvFreeList;
1348 pDevice->NumRecvFreeList--;
1349 DequeueRCB(pDevice->FirstRecvFreeList, pDevice->LastRecvFreeList);
1350 ntStatus = PIPEnsBulkInUsbRead(pDevice, pRCB);
1352 pDevice->bIsRxWorkItemQueued = false;
1353 spin_unlock_irq(&pDevice->lock);
1357 void RXvFreeRCB(struct vnt_rcb *pRCB, int bReAllocSkb)
1359 struct vnt_private *pDevice = pRCB->pDevice;
1361 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"---->RXvFreeRCB\n");
1363 if (bReAllocSkb == false) {
1364 kfree_skb(pRCB->skb);
1365 bReAllocSkb = true;
1368 if (bReAllocSkb == true) {
1369 pRCB->skb = dev_alloc_skb((int)pDevice->rx_buf_sz);
1370 // todo error handling
1371 if (pRCB->skb == NULL) {
1372 DBG_PRT(MSG_LEVEL_ERR,KERN_ERR" Failed to re-alloc rx skb\n");
1373 }else {
1374 pRCB->skb->dev = pDevice->dev;
1378 // Insert the RCB back in the Recv free list
1380 EnqueueRCB(pDevice->FirstRecvFreeList, pDevice->LastRecvFreeList, pRCB);
1381 pDevice->NumRecvFreeList++;
1383 if ((pDevice->Flags & fMP_POST_READS) && MP_IS_READY(pDevice) &&
1384 (pDevice->bIsRxWorkItemQueued == false) ) {
1386 pDevice->bIsRxWorkItemQueued = true;
1387 tasklet_schedule(&pDevice->ReadWorkItem);
1389 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"<----RXFreeRCB %d %d\n",pDevice->NumRecvFreeList, pDevice->NumRecvMngList);
1392 void RXvMngWorkItem(struct vnt_private *pDevice)
1394 struct vnt_rcb *pRCB = NULL;
1395 struct vnt_rx_mgmt *pRxPacket;
1396 int bReAllocSkb = false;
1398 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"---->Rx Mng Thread\n");
1400 spin_lock_irq(&pDevice->lock);
1401 while (pDevice->NumRecvMngList!=0)
1403 pRCB = pDevice->FirstRecvMngList;
1404 pDevice->NumRecvMngList--;
1405 DequeueRCB(pDevice->FirstRecvMngList, pDevice->LastRecvMngList);
1406 if(!pRCB){
1407 break;
1409 pRxPacket = &(pRCB->sMngPacket);
1410 vMgrRxManagePacket(pDevice, &pDevice->vnt_mgmt, pRxPacket);
1411 pRCB->Ref--;
1412 if(pRCB->Ref == 0) {
1413 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"RxvFreeMng %d %d\n",pDevice->NumRecvFreeList, pDevice->NumRecvMngList);
1414 RXvFreeRCB(pRCB, bReAllocSkb);
1415 } else {
1416 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Rx Mng Only we have the right to free RCB\n");
1420 pDevice->bIsRxMngWorkItemQueued = false;
1421 spin_unlock_irq(&pDevice->lock);