PM / sleep: Asynchronous threads for suspend_noirq
[linux/fpc-iii.git] / drivers / staging / vt6656 / dpc.c
blobeca04c0c1d97d9118eb70f44d7ca20c6c60cc372
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 "dpc.h"
42 #include "device.h"
43 #include "rxtx.h"
44 #include "tether.h"
45 #include "card.h"
46 #include "bssdb.h"
47 #include "mac.h"
48 #include "baseband.h"
49 #include "michael.h"
50 #include "tkip.h"
51 #include "tcrc.h"
52 #include "wctl.h"
53 #include "hostap.h"
54 #include "rf.h"
55 #include "iowpa.h"
56 #include "aes_ccmp.h"
57 #include "datarate.h"
58 #include "usbpipe.h"
60 //static int msglevel =MSG_LEVEL_DEBUG;
61 static int msglevel =MSG_LEVEL_INFO;
63 static const u8 acbyRxRate[MAX_RATE] =
64 {2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108};
66 static u8 s_byGetRateIdx(u8 byRate);
68 static
69 void
70 s_vGetDASA(
71 u8 * pbyRxBufferAddr,
72 unsigned int *pcbHeaderSize,
73 struct ethhdr *psEthHeader
76 static void s_vProcessRxMACHeader(struct vnt_private *pDevice,
77 u8 *pbyRxBufferAddr, u32 cbPacketSize, int bIsWEP, int bExtIV,
78 u32 *pcbHeadSize);
80 static int s_bAPModeRxCtl(struct vnt_private *pDevice, u8 *pbyFrame,
81 s32 iSANodeIndex);
83 static int s_bAPModeRxData(struct vnt_private *pDevice, struct sk_buff *skb,
84 u32 FrameSize, u32 cbHeaderOffset, s32 iSANodeIndex, s32 iDANodeIndex);
86 static int s_bHandleRxEncryption(struct vnt_private *pDevice, u8 *pbyFrame,
87 u32 FrameSize, u8 *pbyRsr, u8 *pbyNewRsr, PSKeyItem *pKeyOut,
88 s32 *pbExtIV, u16 *pwRxTSC15_0, u32 *pdwRxTSC47_16);
90 static int s_bHostWepRxEncryption(struct vnt_private *pDevice, u8 *pbyFrame,
91 u32 FrameSize, u8 *pbyRsr, int bOnFly, PSKeyItem pKey, u8 *pbyNewRsr,
92 s32 *pbExtIV, u16 *pwRxTSC15_0, u32 *pdwRxTSC47_16);
94 /*+
96 * Description:
97 * Translate Rcv 802.11 header to 802.3 header with Rx buffer
99 * Parameters:
100 * In:
101 * pDevice
102 * dwRxBufferAddr - Address of Rcv Buffer
103 * cbPacketSize - Rcv Packet size
104 * bIsWEP - If Rcv with WEP
105 * Out:
106 * pcbHeaderSize - 802.11 header size
108 * Return Value: None
112 static void s_vProcessRxMACHeader(struct vnt_private *pDevice,
113 u8 *pbyRxBufferAddr, u32 cbPacketSize, int bIsWEP, int bExtIV,
114 u32 *pcbHeadSize)
116 u8 *pbyRxBuffer;
117 u32 cbHeaderSize = 0;
118 u16 *pwType;
119 struct ieee80211_hdr *pMACHeader;
120 int ii;
122 pMACHeader = (struct ieee80211_hdr *) (pbyRxBufferAddr + cbHeaderSize);
124 s_vGetDASA((u8 *)pMACHeader, &cbHeaderSize, &pDevice->sRxEthHeader);
126 if (bIsWEP) {
127 if (bExtIV) {
128 // strip IV&ExtIV , add 8 byte
129 cbHeaderSize += (WLAN_HDR_ADDR3_LEN + 8);
130 } else {
131 // strip IV , add 4 byte
132 cbHeaderSize += (WLAN_HDR_ADDR3_LEN + 4);
135 else {
136 cbHeaderSize += WLAN_HDR_ADDR3_LEN;
139 pbyRxBuffer = (u8 *) (pbyRxBufferAddr + cbHeaderSize);
140 if (ether_addr_equal(pbyRxBuffer, pDevice->abySNAP_Bridgetunnel)) {
141 cbHeaderSize += 6;
142 } else if (ether_addr_equal(pbyRxBuffer, pDevice->abySNAP_RFC1042)) {
143 cbHeaderSize += 6;
144 pwType = (u16 *) (pbyRxBufferAddr + cbHeaderSize);
145 if ((*pwType == cpu_to_be16(ETH_P_IPX)) ||
146 (*pwType == cpu_to_le16(0xF380))) {
147 cbHeaderSize -= 8;
148 pwType = (u16 *) (pbyRxBufferAddr + cbHeaderSize);
149 if (bIsWEP) {
150 if (bExtIV) {
151 *pwType = htons(cbPacketSize - WLAN_HDR_ADDR3_LEN - 8); // 8 is IV&ExtIV
152 } else {
153 *pwType = htons(cbPacketSize - WLAN_HDR_ADDR3_LEN - 4); // 4 is IV
156 else {
157 *pwType = htons(cbPacketSize - WLAN_HDR_ADDR3_LEN);
161 else {
162 cbHeaderSize -= 2;
163 pwType = (u16 *) (pbyRxBufferAddr + cbHeaderSize);
164 if (bIsWEP) {
165 if (bExtIV) {
166 *pwType = htons(cbPacketSize - WLAN_HDR_ADDR3_LEN - 8); // 8 is IV&ExtIV
167 } else {
168 *pwType = htons(cbPacketSize - WLAN_HDR_ADDR3_LEN - 4); // 4 is IV
171 else {
172 *pwType = htons(cbPacketSize - WLAN_HDR_ADDR3_LEN);
176 cbHeaderSize -= (ETH_ALEN * 2);
177 pbyRxBuffer = (u8 *) (pbyRxBufferAddr + cbHeaderSize);
178 for (ii = 0; ii < ETH_ALEN; ii++)
179 *pbyRxBuffer++ = pDevice->sRxEthHeader.h_dest[ii];
180 for (ii = 0; ii < ETH_ALEN; ii++)
181 *pbyRxBuffer++ = pDevice->sRxEthHeader.h_source[ii];
183 *pcbHeadSize = cbHeaderSize;
186 static u8 s_byGetRateIdx(u8 byRate)
188 u8 byRateIdx;
190 for (byRateIdx = 0; byRateIdx <MAX_RATE ; byRateIdx++) {
191 if (acbyRxRate[byRateIdx%MAX_RATE] == byRate)
192 return byRateIdx;
194 return 0;
197 static
198 void
199 s_vGetDASA (
200 u8 * pbyRxBufferAddr,
201 unsigned int *pcbHeaderSize,
202 struct ethhdr *psEthHeader
205 unsigned int cbHeaderSize = 0;
206 struct ieee80211_hdr *pMACHeader;
207 int ii;
209 pMACHeader = (struct ieee80211_hdr *) (pbyRxBufferAddr + cbHeaderSize);
211 if ((pMACHeader->frame_control & FC_TODS) == 0) {
212 if (pMACHeader->frame_control & FC_FROMDS) {
213 for (ii = 0; ii < ETH_ALEN; ii++) {
214 psEthHeader->h_dest[ii] =
215 pMACHeader->addr1[ii];
216 psEthHeader->h_source[ii] =
217 pMACHeader->addr3[ii];
219 } else {
220 /* IBSS mode */
221 for (ii = 0; ii < ETH_ALEN; ii++) {
222 psEthHeader->h_dest[ii] =
223 pMACHeader->addr1[ii];
224 psEthHeader->h_source[ii] =
225 pMACHeader->addr2[ii];
228 } else {
229 /* Is AP mode.. */
230 if (pMACHeader->frame_control & FC_FROMDS) {
231 for (ii = 0; ii < ETH_ALEN; ii++) {
232 psEthHeader->h_dest[ii] =
233 pMACHeader->addr3[ii];
234 psEthHeader->h_source[ii] =
235 pMACHeader->addr4[ii];
236 cbHeaderSize += 6;
238 } else {
239 for (ii = 0; ii < ETH_ALEN; ii++) {
240 psEthHeader->h_dest[ii] =
241 pMACHeader->addr3[ii];
242 psEthHeader->h_source[ii] =
243 pMACHeader->addr2[ii];
247 *pcbHeaderSize = cbHeaderSize;
250 int RXbBulkInProcessData(struct vnt_private *pDevice, struct vnt_rcb *pRCB,
251 unsigned long BytesToIndicate)
253 struct net_device_stats *pStats = &pDevice->stats;
254 struct sk_buff *skb;
255 struct vnt_manager *pMgmt = &pDevice->vnt_mgmt;
256 struct vnt_rx_mgmt *pRxPacket = &pMgmt->sRxPacket;
257 struct ieee80211_hdr *p802_11Header;
258 u8 *pbyRsr, *pbyNewRsr, *pbyRSSI, *pbyFrame;
259 u64 *pqwTSFTime;
260 u32 bDeFragRx = false;
261 u32 cbHeaderOffset, cbIVOffset;
262 u32 FrameSize;
263 u16 wEtherType = 0;
264 s32 iSANodeIndex = -1, iDANodeIndex = -1;
265 int ii;
266 u8 *pbyRxSts, *pbyRxRate, *pbySQ, *pby3SQ;
267 u32 cbHeaderSize;
268 PSKeyItem pKey = NULL;
269 u16 wRxTSC15_0 = 0;
270 u32 dwRxTSC47_16 = 0;
271 SKeyItem STempKey;
272 /* signed long ldBm = 0; */
273 int bIsWEP = false; int bExtIV = false;
274 u32 dwWbkStatus;
275 struct vnt_rcb *pRCBIndicate = pRCB;
276 u8 *pbyDAddress;
277 u16 *pwPLCP_Length;
278 u8 abyVaildRate[MAX_RATE]
279 = {2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108};
280 u16 wPLCPwithPadding;
281 struct ieee80211_hdr *pMACHeader;
282 int bRxeapol_key = false;
284 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"---------- RXbBulkInProcessData---\n");
286 skb = pRCB->skb;
288 /* [31:16]RcvByteCount ( not include 4-byte Status ) */
289 dwWbkStatus = *((u32 *)(skb->data));
290 FrameSize = dwWbkStatus >> 16;
291 FrameSize += 4;
293 if (BytesToIndicate != FrameSize) {
294 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"------- WRONG Length 1\n");
295 pStats->rx_frame_errors++;
296 return false;
299 if ((BytesToIndicate > 2372) || (BytesToIndicate <= 40)) {
300 // Frame Size error drop this packet.
301 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "---------- WRONG Length 2\n");
302 pStats->rx_frame_errors++;
303 return false;
306 pbyDAddress = (u8 *)(skb->data);
307 pbyRxSts = pbyDAddress+4;
308 pbyRxRate = pbyDAddress+5;
310 //real Frame Size = USBFrameSize -4WbkStatus - 4RxStatus - 8TSF - 4RSR - 4SQ3 - ?Padding
311 //if SQ3 the range is 24~27, if no SQ3 the range is 20~23
312 //real Frame size in PLCPLength field.
313 pwPLCP_Length = (u16 *) (pbyDAddress + 6);
314 //Fix hardware bug => PLCP_Length error
315 if ( ((BytesToIndicate - (*pwPLCP_Length)) > 27) ||
316 ((BytesToIndicate - (*pwPLCP_Length)) < 24) ||
317 (BytesToIndicate < (*pwPLCP_Length)) ) {
319 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Wrong PLCP Length %x\n", (int) *pwPLCP_Length);
320 pStats->rx_frame_errors++;
321 return false;
323 for ( ii=RATE_1M;ii<MAX_RATE;ii++) {
324 if ( *pbyRxRate == abyVaildRate[ii] ) {
325 break;
328 if ( ii==MAX_RATE ) {
329 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Wrong RxRate %x\n",(int) *pbyRxRate);
330 return false;
333 wPLCPwithPadding = ( (*pwPLCP_Length / 4) + ( (*pwPLCP_Length % 4) ? 1:0 ) ) *4;
335 pqwTSFTime = (u64 *)(pbyDAddress + 8 + wPLCPwithPadding);
336 if(pDevice->byBBType == BB_TYPE_11G) {
337 pby3SQ = pbyDAddress + 8 + wPLCPwithPadding + 12;
338 pbySQ = pby3SQ;
340 else {
341 pbySQ = pbyDAddress + 8 + wPLCPwithPadding + 8;
342 pby3SQ = pbySQ;
344 pbyNewRsr = pbyDAddress + 8 + wPLCPwithPadding + 9;
345 pbyRSSI = pbyDAddress + 8 + wPLCPwithPadding + 10;
346 pbyRsr = pbyDAddress + 8 + wPLCPwithPadding + 11;
348 FrameSize = *pwPLCP_Length;
350 pbyFrame = pbyDAddress + 8;
352 pMACHeader = (struct ieee80211_hdr *) pbyFrame;
354 //mike add: to judge if current AP is activated?
355 if ((pMgmt->eCurrMode == WMAC_MODE_STANDBY) ||
356 (pMgmt->eCurrMode == WMAC_MODE_ESS_STA)) {
357 if (pMgmt->sNodeDBTable[0].bActive) {
358 if (ether_addr_equal(pMgmt->abyCurrBSSID, pMACHeader->addr2)) {
359 if (pMgmt->sNodeDBTable[0].uInActiveCount != 0)
360 pMgmt->sNodeDBTable[0].uInActiveCount = 0;
365 if (!is_multicast_ether_addr(pMACHeader->addr1)) {
366 if (WCTLbIsDuplicate(&(pDevice->sDupRxCache), (struct ieee80211_hdr *) pbyFrame)) {
367 return false;
370 if (!ether_addr_equal(pDevice->abyCurrentNetAddr, pMACHeader->addr1)) {
371 return false;
375 // Use for TKIP MIC
376 s_vGetDASA(pbyFrame, &cbHeaderSize, &pDevice->sRxEthHeader);
378 if (ether_addr_equal((u8 *)pDevice->sRxEthHeader.h_source,
379 pDevice->abyCurrentNetAddr))
380 return false;
382 if ((pMgmt->eCurrMode == WMAC_MODE_ESS_AP) || (pMgmt->eCurrMode == WMAC_MODE_IBSS_STA)) {
383 if (IS_CTL_PSPOLL(pbyFrame) || !IS_TYPE_CONTROL(pbyFrame)) {
384 p802_11Header = (struct ieee80211_hdr *) (pbyFrame);
385 // get SA NodeIndex
386 if (BSSbIsSTAInNodeDB(pDevice, (u8 *)(p802_11Header->addr2), &iSANodeIndex)) {
387 pMgmt->sNodeDBTable[iSANodeIndex].ulLastRxJiffer = jiffies;
388 pMgmt->sNodeDBTable[iSANodeIndex].uInActiveCount = 0;
393 if (pMgmt->eCurrMode == WMAC_MODE_ESS_AP) {
394 if (s_bAPModeRxCtl(pDevice, pbyFrame, iSANodeIndex) == true) {
395 return false;
399 if (IS_FC_WEP(pbyFrame)) {
400 bool bRxDecryOK = false;
402 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"rx WEP pkt\n");
403 bIsWEP = true;
404 if ((pDevice->bEnableHostWEP) && (iSANodeIndex >= 0)) {
405 pKey = &STempKey;
406 pKey->byCipherSuite = pMgmt->sNodeDBTable[iSANodeIndex].byCipherSuite;
407 pKey->dwKeyIndex = pMgmt->sNodeDBTable[iSANodeIndex].dwKeyIndex;
408 pKey->uKeyLength = pMgmt->sNodeDBTable[iSANodeIndex].uWepKeyLength;
409 pKey->dwTSC47_16 = pMgmt->sNodeDBTable[iSANodeIndex].dwTSC47_16;
410 pKey->wTSC15_0 = pMgmt->sNodeDBTable[iSANodeIndex].wTSC15_0;
411 memcpy(pKey->abyKey,
412 &pMgmt->sNodeDBTable[iSANodeIndex].abyWepKey[0],
413 pKey->uKeyLength
416 bRxDecryOK = s_bHostWepRxEncryption(pDevice,
417 pbyFrame,
418 FrameSize,
419 pbyRsr,
420 pMgmt->sNodeDBTable[iSANodeIndex].bOnFly,
421 pKey,
422 pbyNewRsr,
423 &bExtIV,
424 &wRxTSC15_0,
425 &dwRxTSC47_16);
426 } else {
427 bRxDecryOK = s_bHandleRxEncryption(pDevice,
428 pbyFrame,
429 FrameSize,
430 pbyRsr,
431 pbyNewRsr,
432 &pKey,
433 &bExtIV,
434 &wRxTSC15_0,
435 &dwRxTSC47_16);
438 if (bRxDecryOK) {
439 if ((*pbyNewRsr & NEWRSR_DECRYPTOK) == 0) {
440 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"ICV Fail\n");
441 if ( (pMgmt->eAuthenMode == WMAC_AUTH_WPA) ||
442 (pMgmt->eAuthenMode == WMAC_AUTH_WPAPSK) ||
443 (pMgmt->eAuthenMode == WMAC_AUTH_WPANONE) ||
444 (pMgmt->eAuthenMode == WMAC_AUTH_WPA2) ||
445 (pMgmt->eAuthenMode == WMAC_AUTH_WPA2PSK)) {
447 return false;
449 } else {
450 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"WEP Func Fail\n");
451 return false;
453 if ((pKey != NULL) && (pKey->byCipherSuite == KEY_CTL_CCMP))
454 FrameSize -= 8; // Message Integrity Code
455 else
456 FrameSize -= 4; // 4 is ICV
460 // RX OK
462 /* remove the FCS/CRC length */
463 FrameSize -= ETH_FCS_LEN;
465 if ( !(*pbyRsr & (RSR_ADDRBROAD | RSR_ADDRMULTI)) && // unicast address
466 (IS_FRAGMENT_PKT((pbyFrame)))
468 // defragment
469 bDeFragRx = WCTLbHandleFragment(pDevice, (struct ieee80211_hdr *) (pbyFrame), FrameSize, bIsWEP, bExtIV);
470 if (bDeFragRx) {
471 // defrag complete
472 // TODO skb, pbyFrame
473 skb = pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].skb;
474 FrameSize = pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].cbFrameLength;
475 pbyFrame = skb->data + 8;
477 else {
478 return false;
483 // Management & Control frame Handle
485 if ((IS_TYPE_DATA((pbyFrame))) == false) {
486 // Handle Control & Manage Frame
488 if (IS_TYPE_MGMT((pbyFrame))) {
489 u8 * pbyData1;
490 u8 * pbyData2;
492 pRxPacket = &(pRCB->sMngPacket);
493 pRxPacket->p80211Header = (PUWLAN_80211HDR)(pbyFrame);
494 pRxPacket->cbMPDULen = FrameSize;
495 pRxPacket->uRSSI = *pbyRSSI;
496 pRxPacket->bySQ = *pbySQ;
497 pRxPacket->qwLocalTSF = cpu_to_le64(*pqwTSFTime);
498 if (bIsWEP) {
499 // strip IV
500 pbyData1 = WLAN_HDR_A3_DATA_PTR(pbyFrame);
501 pbyData2 = WLAN_HDR_A3_DATA_PTR(pbyFrame) + 4;
502 for (ii = 0; ii < (FrameSize - 4); ii++) {
503 *pbyData1 = *pbyData2;
504 pbyData1++;
505 pbyData2++;
509 pRxPacket->byRxRate = s_byGetRateIdx(*pbyRxRate);
511 if ( *pbyRxSts == 0 ) {
512 //Discard beacon packet which channel is 0
513 if ( (WLAN_GET_FC_FSTYPE((pRxPacket->p80211Header->sA3.wFrameCtl)) == WLAN_FSTYPE_BEACON) ||
514 (WLAN_GET_FC_FSTYPE((pRxPacket->p80211Header->sA3.wFrameCtl)) == WLAN_FSTYPE_PROBERESP) ) {
515 return false;
518 pRxPacket->byRxChannel = (*pbyRxSts) >> 2;
520 // hostap Deamon handle 802.11 management
521 if (pDevice->bEnableHostapd) {
522 skb->dev = pDevice->apdev;
523 //skb->data += 4;
524 //skb->tail += 4;
525 skb->data += 8;
526 skb->tail += 8;
527 skb_put(skb, FrameSize);
528 skb_reset_mac_header(skb);
529 skb->pkt_type = PACKET_OTHERHOST;
530 skb->protocol = htons(ETH_P_802_2);
531 memset(skb->cb, 0, sizeof(skb->cb));
532 netif_rx(skb);
533 return true;
537 // Insert the RCB in the Recv Mng list
539 EnqueueRCB(pDevice->FirstRecvMngList, pDevice->LastRecvMngList, pRCBIndicate);
540 pDevice->NumRecvMngList++;
541 if ( bDeFragRx == false) {
542 pRCB->Ref++;
544 if (pDevice->bIsRxMngWorkItemQueued == false) {
545 pDevice->bIsRxMngWorkItemQueued = true;
546 schedule_work(&pDevice->rx_mng_work_item);
550 else {
551 // Control Frame
553 return false;
555 else {
556 if (pMgmt->eCurrMode == WMAC_MODE_ESS_AP) {
557 //In AP mode, hw only check addr1(BSSID or RA) if equal to local MAC.
558 if ( !(*pbyRsr & RSR_BSSIDOK)) {
559 if (bDeFragRx) {
560 if (!device_alloc_frag_buf(pDevice, &pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx])) {
561 DBG_PRT(MSG_LEVEL_ERR,KERN_ERR "%s: can not alloc more frag bufs\n",
562 pDevice->dev->name);
565 return false;
568 else {
569 // discard DATA packet while not associate || BSSID error
570 if ((pDevice->bLinkPass == false) ||
571 !(*pbyRsr & RSR_BSSIDOK)) {
572 if (bDeFragRx) {
573 if (!device_alloc_frag_buf(pDevice, &pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx])) {
574 DBG_PRT(MSG_LEVEL_ERR,KERN_ERR "%s: can not alloc more frag bufs\n",
575 pDevice->dev->name);
578 return false;
580 //mike add:station mode check eapol-key challenge--->
582 u8 Protocol_Version; //802.1x Authentication
583 u8 Packet_Type; //802.1x Authentication
584 u8 Descriptor_type;
585 u16 Key_info;
586 if (bIsWEP)
587 cbIVOffset = 8;
588 else
589 cbIVOffset = 0;
590 wEtherType = (skb->data[cbIVOffset + 8 + 24 + 6] << 8) |
591 skb->data[cbIVOffset + 8 + 24 + 6 + 1];
592 Protocol_Version = skb->data[cbIVOffset + 8 + 24 + 6 + 1 +1];
593 Packet_Type = skb->data[cbIVOffset + 8 + 24 + 6 + 1 +1+1];
594 if (wEtherType == ETH_P_PAE) { //Protocol Type in LLC-Header
595 if(((Protocol_Version==1) ||(Protocol_Version==2)) &&
596 (Packet_Type==3)) { //802.1x OR eapol-key challenge frame receive
597 bRxeapol_key = true;
598 Descriptor_type = skb->data[cbIVOffset + 8 + 24 + 6 + 1 +1+1+1+2];
599 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] ;
600 if(Descriptor_type==2) { //RSN
601 // printk("WPA2_Rx_eapol-key_info<-----:%x\n",Key_info);
603 else if(Descriptor_type==254) {
604 // printk("WPA_Rx_eapol-key_info<-----:%x\n",Key_info);
609 //mike add:station mode check eapol-key challenge<---
613 // Data frame Handle
615 if (pDevice->bEnablePSMode) {
616 if (IS_FC_MOREDATA((pbyFrame))) {
617 if (*pbyRsr & RSR_ADDROK) {
618 //PSbSendPSPOLL((PSDevice)pDevice);
621 else {
622 if (pMgmt->bInTIMWake == true) {
623 pMgmt->bInTIMWake = false;
628 // Now it only supports 802.11g Infrastructure Mode, and support rate must up to 54 Mbps
629 if (pDevice->bDiversityEnable && (FrameSize>50) &&
630 (pDevice->eOPMode == OP_MODE_INFRASTRUCTURE) &&
631 (pDevice->bLinkPass == true)) {
632 BBvAntennaDiversity(pDevice, s_byGetRateIdx(*pbyRxRate), 0);
635 // ++++++++ For BaseBand Algorithm +++++++++++++++
636 pDevice->uCurrRSSI = *pbyRSSI;
637 pDevice->byCurrSQ = *pbySQ;
639 // todo
641 if ((*pbyRSSI != 0) &&
642 (pMgmt->pCurrBSS!=NULL)) {
643 RFvRSSITodBm(pDevice, *pbyRSSI, &ldBm);
644 // Monitor if RSSI is too strong.
645 pMgmt->pCurrBSS->byRSSIStatCnt++;
646 pMgmt->pCurrBSS->byRSSIStatCnt %= RSSI_STAT_COUNT;
647 pMgmt->pCurrBSS->ldBmAverage[pMgmt->pCurrBSS->byRSSIStatCnt] = ldBm;
648 for (ii = 0; ii < RSSI_STAT_COUNT; ii++) {
649 if (pMgmt->pCurrBSS->ldBmAverage[ii] != 0) {
650 pMgmt->pCurrBSS->ldBmMAX =
651 max(pMgmt->pCurrBSS->ldBmAverage[ii], ldBm);
657 // -----------------------------------------------
659 if ((pMgmt->eCurrMode == WMAC_MODE_ESS_AP) && (pDevice->bEnable8021x == true)){
660 u8 abyMacHdr[24];
662 // Only 802.1x packet incoming allowed
663 if (bIsWEP)
664 cbIVOffset = 8;
665 else
666 cbIVOffset = 0;
667 wEtherType = (skb->data[cbIVOffset + 8 + 24 + 6] << 8) |
668 skb->data[cbIVOffset + 8 + 24 + 6 + 1];
670 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"wEtherType = %04x \n", wEtherType);
671 if (wEtherType == ETH_P_PAE) {
672 skb->dev = pDevice->apdev;
674 if (bIsWEP == true) {
675 // strip IV header(8)
676 memcpy(&abyMacHdr[0], (skb->data + 8), 24);
677 memcpy((skb->data + 8 + cbIVOffset), &abyMacHdr[0], 24);
680 skb->data += (cbIVOffset + 8);
681 skb->tail += (cbIVOffset + 8);
682 skb_put(skb, FrameSize);
683 skb_reset_mac_header(skb);
684 skb->pkt_type = PACKET_OTHERHOST;
685 skb->protocol = htons(ETH_P_802_2);
686 memset(skb->cb, 0, sizeof(skb->cb));
687 netif_rx(skb);
688 return true;
691 // check if 802.1x authorized
692 if (!(pMgmt->sNodeDBTable[iSANodeIndex].dwFlags & WLAN_STA_AUTHORIZED))
693 return false;
696 if ((pKey != NULL) && (pKey->byCipherSuite == KEY_CTL_TKIP)) {
697 if (bIsWEP) {
698 FrameSize -= 8; //MIC
702 //--------------------------------------------------------------------------------
703 // Soft MIC
704 if ((pKey != NULL) && (pKey->byCipherSuite == KEY_CTL_TKIP)) {
705 if (bIsWEP) {
706 u32 * pdwMIC_L;
707 u32 * pdwMIC_R;
708 u32 dwMIC_Priority;
709 u32 dwMICKey0 = 0, dwMICKey1 = 0;
710 u32 dwLocalMIC_L = 0;
711 u32 dwLocalMIC_R = 0;
713 if (pMgmt->eCurrMode == WMAC_MODE_ESS_AP) {
714 dwMICKey0 = cpu_to_le32(*(u32 *)(&pKey->abyKey[24]));
715 dwMICKey1 = cpu_to_le32(*(u32 *)(&pKey->abyKey[28]));
717 else {
718 if (pMgmt->eAuthenMode == WMAC_AUTH_WPANONE) {
719 dwMICKey0 = cpu_to_le32(*(u32 *)(&pKey->abyKey[16]));
720 dwMICKey1 = cpu_to_le32(*(u32 *)(&pKey->abyKey[20]));
721 } else if ((pKey->dwKeyIndex & BIT28) == 0) {
722 dwMICKey0 = cpu_to_le32(*(u32 *)(&pKey->abyKey[16]));
723 dwMICKey1 = cpu_to_le32(*(u32 *)(&pKey->abyKey[20]));
724 } else {
725 dwMICKey0 = cpu_to_le32(*(u32 *)(&pKey->abyKey[24]));
726 dwMICKey1 = cpu_to_le32(*(u32 *)(&pKey->abyKey[28]));
730 MIC_vInit(dwMICKey0, dwMICKey1);
731 MIC_vAppend((u8 *)&(pDevice->sRxEthHeader.h_dest[0]), 12);
732 dwMIC_Priority = 0;
733 MIC_vAppend((u8 *)&dwMIC_Priority, 4);
734 // 4 is Rcv buffer header, 24 is MAC Header, and 8 is IV and Ext IV.
735 MIC_vAppend((u8 *)(skb->data + 8 + WLAN_HDR_ADDR3_LEN + 8),
736 FrameSize - WLAN_HDR_ADDR3_LEN - 8);
737 MIC_vGetMIC(&dwLocalMIC_L, &dwLocalMIC_R);
738 MIC_vUnInit();
740 pdwMIC_L = (u32 *)(skb->data + 8 + FrameSize);
741 pdwMIC_R = (u32 *)(skb->data + 8 + FrameSize + 4);
743 if ((cpu_to_le32(*pdwMIC_L) != dwLocalMIC_L) || (cpu_to_le32(*pdwMIC_R) != dwLocalMIC_R) ||
744 (pDevice->bRxMICFail == true)) {
745 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"MIC comparison is fail!\n");
746 pDevice->bRxMICFail = false;
747 if (bDeFragRx) {
748 if (!device_alloc_frag_buf(pDevice, &pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx])) {
749 DBG_PRT(MSG_LEVEL_ERR,KERN_ERR "%s: can not alloc more frag bufs\n",
750 pDevice->dev->name);
753 //send event to wpa_supplicant
754 //if(pDevice->bWPASuppWextEnabled == true)
756 union iwreq_data wrqu;
757 struct iw_michaelmicfailure ev;
758 int keyidx = pbyFrame[cbHeaderSize+3] >> 6; //top two-bits
759 memset(&ev, 0, sizeof(ev));
760 ev.flags = keyidx & IW_MICFAILURE_KEY_ID;
761 if ((pMgmt->eCurrMode == WMAC_MODE_ESS_STA) &&
762 (pMgmt->eCurrState == WMAC_STATE_ASSOC) &&
763 (*pbyRsr & (RSR_ADDRBROAD | RSR_ADDRMULTI)) == 0) {
764 ev.flags |= IW_MICFAILURE_PAIRWISE;
765 } else {
766 ev.flags |= IW_MICFAILURE_GROUP;
769 ev.src_addr.sa_family = ARPHRD_ETHER;
770 memcpy(ev.src_addr.sa_data, pMACHeader->addr2, ETH_ALEN);
771 memset(&wrqu, 0, sizeof(wrqu));
772 wrqu.data.length = sizeof(ev);
773 PRINT_K("wireless_send_event--->IWEVMICHAELMICFAILURE\n");
774 wireless_send_event(pDevice->dev, IWEVMICHAELMICFAILURE, &wrqu, (char *)&ev);
778 return false;
782 } //---end of SOFT MIC-----------------------------------------------------------------------
784 // ++++++++++ Reply Counter Check +++++++++++++
786 if ((pKey != NULL) && ((pKey->byCipherSuite == KEY_CTL_TKIP) ||
787 (pKey->byCipherSuite == KEY_CTL_CCMP))) {
788 if (bIsWEP) {
789 u16 wLocalTSC15_0 = 0;
790 u32 dwLocalTSC47_16 = 0;
791 unsigned long long RSC = 0;
792 // endian issues
793 RSC = *((unsigned long long *) &(pKey->KeyRSC));
794 wLocalTSC15_0 = (u16) RSC;
795 dwLocalTSC47_16 = (u32) (RSC>>16);
797 RSC = dwRxTSC47_16;
798 RSC <<= 16;
799 RSC += wRxTSC15_0;
800 memcpy(&(pKey->KeyRSC), &RSC, sizeof(u64));
802 if (pDevice->vnt_mgmt.eCurrMode == WMAC_MODE_ESS_STA &&
803 pDevice->vnt_mgmt.eCurrState == WMAC_STATE_ASSOC) {
804 /* check RSC */
805 if ( (wRxTSC15_0 < wLocalTSC15_0) &&
806 (dwRxTSC47_16 <= dwLocalTSC47_16) &&
807 !((dwRxTSC47_16 == 0) && (dwLocalTSC47_16 == 0xFFFFFFFF))) {
808 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"TSC is illegal~~!\n ");
810 if (bDeFragRx) {
811 if (!device_alloc_frag_buf(pDevice, &pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx])) {
812 DBG_PRT(MSG_LEVEL_ERR,KERN_ERR "%s: can not alloc more frag bufs\n",
813 pDevice->dev->name);
816 return false;
820 } // ----- End of Reply Counter Check --------------------------
822 s_vProcessRxMACHeader(pDevice, (u8 *)(skb->data+8), FrameSize, bIsWEP, bExtIV, &cbHeaderOffset);
823 FrameSize -= cbHeaderOffset;
824 cbHeaderOffset += 8; // 8 is Rcv buffer header
826 // Null data, framesize = 12
827 if (FrameSize < 12)
828 return false;
830 if (pMgmt->eCurrMode == WMAC_MODE_ESS_AP) {
831 if (s_bAPModeRxData(pDevice,
832 skb,
833 FrameSize,
834 cbHeaderOffset,
835 iSANodeIndex,
836 iDANodeIndex
837 ) == false) {
839 if (bDeFragRx) {
840 if (!device_alloc_frag_buf(pDevice, &pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx])) {
841 DBG_PRT(MSG_LEVEL_ERR,KERN_ERR "%s: can not alloc more frag bufs\n",
842 pDevice->dev->name);
845 return false;
850 skb->data += cbHeaderOffset;
851 skb->tail += cbHeaderOffset;
852 skb_put(skb, FrameSize);
853 skb->protocol=eth_type_trans(skb, skb->dev);
854 skb->ip_summed=CHECKSUM_NONE;
855 pStats->rx_bytes +=skb->len;
856 pStats->rx_packets++;
857 netif_rx(skb);
858 if (bDeFragRx) {
859 if (!device_alloc_frag_buf(pDevice, &pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx])) {
860 DBG_PRT(MSG_LEVEL_ERR,KERN_ERR "%s: can not alloc more frag bufs\n",
861 pDevice->dev->name);
863 return false;
866 return true;
869 static int s_bAPModeRxCtl(struct vnt_private *pDevice, u8 *pbyFrame,
870 s32 iSANodeIndex)
872 struct vnt_manager *pMgmt = &pDevice->vnt_mgmt;
873 struct ieee80211_hdr *p802_11Header;
874 CMD_STATUS Status;
876 if (IS_CTL_PSPOLL(pbyFrame) || !IS_TYPE_CONTROL(pbyFrame)) {
878 p802_11Header = (struct ieee80211_hdr *) (pbyFrame);
879 if (!IS_TYPE_MGMT(pbyFrame)) {
881 // Data & PS-Poll packet
882 // check frame class
883 if (iSANodeIndex > 0) {
884 // frame class 3 fliter & checking
885 if (pMgmt->sNodeDBTable[iSANodeIndex].eNodeState < NODE_AUTH) {
886 // send deauth notification
887 // reason = (6) class 2 received from nonauth sta
888 vMgrDeAuthenBeginSta(pDevice,
889 pMgmt,
890 (u8 *)(p802_11Header->addr2),
891 (WLAN_MGMT_REASON_CLASS2_NONAUTH),
892 &Status
894 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "dpc: send vMgrDeAuthenBeginSta 1\n");
895 return true;
897 if (pMgmt->sNodeDBTable[iSANodeIndex].eNodeState < NODE_ASSOC) {
898 // send deassoc notification
899 // reason = (7) class 3 received from nonassoc sta
900 vMgrDisassocBeginSta(pDevice,
901 pMgmt,
902 (u8 *)(p802_11Header->addr2),
903 (WLAN_MGMT_REASON_CLASS3_NONASSOC),
904 &Status
906 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "dpc: send vMgrDisassocBeginSta 2\n");
907 return true;
910 if (pMgmt->sNodeDBTable[iSANodeIndex].bPSEnable) {
911 // delcare received ps-poll event
912 if (IS_CTL_PSPOLL(pbyFrame)) {
913 pMgmt->sNodeDBTable[iSANodeIndex].bRxPSPoll = true;
914 bScheduleCommand((void *) pDevice,
915 WLAN_CMD_RX_PSPOLL,
916 NULL);
917 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "dpc: WLAN_CMD_RX_PSPOLL 1\n");
919 else {
920 // check Data PS state
921 // if PW bit off, send out all PS bufferring packets.
922 if (!IS_FC_POWERMGT(pbyFrame)) {
923 pMgmt->sNodeDBTable[iSANodeIndex].bPSEnable = false;
924 pMgmt->sNodeDBTable[iSANodeIndex].bRxPSPoll = true;
925 bScheduleCommand((void *) pDevice,
926 WLAN_CMD_RX_PSPOLL,
927 NULL);
928 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "dpc: WLAN_CMD_RX_PSPOLL 2\n");
932 else {
933 if (IS_FC_POWERMGT(pbyFrame)) {
934 pMgmt->sNodeDBTable[iSANodeIndex].bPSEnable = true;
935 // Once if STA in PS state, enable multicast bufferring
936 pMgmt->sNodeDBTable[0].bPSEnable = true;
938 else {
939 // clear all pending PS frame.
940 if (pMgmt->sNodeDBTable[iSANodeIndex].wEnQueueCnt > 0) {
941 pMgmt->sNodeDBTable[iSANodeIndex].bPSEnable = false;
942 pMgmt->sNodeDBTable[iSANodeIndex].bRxPSPoll = true;
943 bScheduleCommand((void *) pDevice,
944 WLAN_CMD_RX_PSPOLL,
945 NULL);
946 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "dpc: WLAN_CMD_RX_PSPOLL 3\n");
952 else {
953 vMgrDeAuthenBeginSta(pDevice,
954 pMgmt,
955 (u8 *)(p802_11Header->addr2),
956 (WLAN_MGMT_REASON_CLASS2_NONAUTH),
957 &Status
959 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "dpc: send vMgrDeAuthenBeginSta 3\n");
960 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "BSSID:%pM\n",
961 p802_11Header->addr3);
962 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "ADDR2:%pM\n",
963 p802_11Header->addr2);
964 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "ADDR1:%pM\n",
965 p802_11Header->addr1);
966 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "dpc: frame_control= %x\n", p802_11Header->frame_control);
967 return true;
971 return false;
975 static int s_bHandleRxEncryption(struct vnt_private *pDevice, u8 *pbyFrame,
976 u32 FrameSize, u8 *pbyRsr, u8 *pbyNewRsr, PSKeyItem *pKeyOut,
977 s32 *pbExtIV, u16 *pwRxTSC15_0, u32 *pdwRxTSC47_16)
979 struct vnt_manager *pMgmt = &pDevice->vnt_mgmt;
980 u32 PayloadLen = FrameSize;
981 u8 *pbyIV;
982 u8 byKeyIdx;
983 PSKeyItem pKey = NULL;
984 u8 byDecMode = KEY_CTL_WEP;
986 *pwRxTSC15_0 = 0;
987 *pdwRxTSC47_16 = 0;
989 pbyIV = pbyFrame + WLAN_HDR_ADDR3_LEN;
990 if ( WLAN_GET_FC_TODS(*(u16 *)pbyFrame) &&
991 WLAN_GET_FC_FROMDS(*(u16 *)pbyFrame) ) {
992 pbyIV += 6; // 6 is 802.11 address4
993 PayloadLen -= 6;
995 byKeyIdx = (*(pbyIV+3) & 0xc0);
996 byKeyIdx >>= 6;
997 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"\nKeyIdx: %d\n", byKeyIdx);
999 if ((pMgmt->eAuthenMode == WMAC_AUTH_WPA) ||
1000 (pMgmt->eAuthenMode == WMAC_AUTH_WPAPSK) ||
1001 (pMgmt->eAuthenMode == WMAC_AUTH_WPANONE) ||
1002 (pMgmt->eAuthenMode == WMAC_AUTH_WPA2) ||
1003 (pMgmt->eAuthenMode == WMAC_AUTH_WPA2PSK)) {
1004 if (((*pbyRsr & (RSR_ADDRBROAD | RSR_ADDRMULTI)) == 0) &&
1005 (pMgmt->byCSSPK != KEY_CTL_NONE)) {
1006 // unicast pkt use pairwise key
1007 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"unicast pkt\n");
1008 if (KeybGetKey(&(pDevice->sKey), pDevice->abyBSSID, 0xFFFFFFFF, &pKey) == true) {
1009 if (pMgmt->byCSSPK == KEY_CTL_TKIP)
1010 byDecMode = KEY_CTL_TKIP;
1011 else if (pMgmt->byCSSPK == KEY_CTL_CCMP)
1012 byDecMode = KEY_CTL_CCMP;
1014 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"unicast pkt: %d, %p\n", byDecMode, pKey);
1015 } else {
1016 // use group key
1017 KeybGetKey(&(pDevice->sKey), pDevice->abyBSSID, byKeyIdx, &pKey);
1018 if (pMgmt->byCSSGK == KEY_CTL_TKIP)
1019 byDecMode = KEY_CTL_TKIP;
1020 else if (pMgmt->byCSSGK == KEY_CTL_CCMP)
1021 byDecMode = KEY_CTL_CCMP;
1022 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"group pkt: %d, %d, %p\n", byKeyIdx, byDecMode, pKey);
1025 // our WEP only support Default Key
1026 if (pKey == NULL) {
1027 // use default group key
1028 KeybGetKey(&(pDevice->sKey), pDevice->abyBroadcastAddr, byKeyIdx, &pKey);
1029 if (pMgmt->byCSSGK == KEY_CTL_TKIP)
1030 byDecMode = KEY_CTL_TKIP;
1031 else if (pMgmt->byCSSGK == KEY_CTL_CCMP)
1032 byDecMode = KEY_CTL_CCMP;
1034 *pKeyOut = pKey;
1036 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"AES:%d %d %d\n", pMgmt->byCSSPK, pMgmt->byCSSGK, byDecMode);
1038 if (pKey == NULL) {
1039 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"pKey == NULL\n");
1040 return false;
1042 if (byDecMode != pKey->byCipherSuite) {
1043 *pKeyOut = NULL;
1044 return false;
1046 if (byDecMode == KEY_CTL_WEP) {
1047 // handle WEP
1048 if ((pDevice->byLocalID <= REV_ID_VT3253_A1) ||
1049 (((PSKeyTable)(pKey->pvKeyTable))->bSoftWEP == true)) {
1050 // Software WEP
1051 // 1. 3253A
1052 // 2. WEP 256
1054 PayloadLen -= (WLAN_HDR_ADDR3_LEN + 4 + 4); // 24 is 802.11 header,4 is IV, 4 is crc
1055 memcpy(pDevice->abyPRNG, pbyIV, 3);
1056 memcpy(pDevice->abyPRNG + 3, pKey->abyKey, pKey->uKeyLength);
1057 rc4_init(&pDevice->SBox, pDevice->abyPRNG, pKey->uKeyLength + 3);
1058 rc4_encrypt(&pDevice->SBox, pbyIV+4, pbyIV+4, PayloadLen);
1060 if (ETHbIsBufferCrc32Ok(pbyIV+4, PayloadLen)) {
1061 *pbyNewRsr |= NEWRSR_DECRYPTOK;
1064 } else if ((byDecMode == KEY_CTL_TKIP) ||
1065 (byDecMode == KEY_CTL_CCMP)) {
1066 // TKIP/AES
1068 PayloadLen -= (WLAN_HDR_ADDR3_LEN + 8 + 4); // 24 is 802.11 header, 8 is IV&ExtIV, 4 is crc
1069 *pdwRxTSC47_16 = cpu_to_le32(*(u32 *)(pbyIV + 4));
1070 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"ExtIV: %x\n", *pdwRxTSC47_16);
1071 if (byDecMode == KEY_CTL_TKIP) {
1072 *pwRxTSC15_0 = cpu_to_le16(MAKEWORD(*(pbyIV+2), *pbyIV));
1073 } else {
1074 *pwRxTSC15_0 = cpu_to_le16(*(u16 *)pbyIV);
1076 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"TSC0_15: %x\n", *pwRxTSC15_0);
1078 if ((byDecMode == KEY_CTL_TKIP) &&
1079 (pDevice->byLocalID <= REV_ID_VT3253_A1)) {
1080 // Software TKIP
1081 // 1. 3253 A
1082 struct ieee80211_hdr *pMACHeader = (struct ieee80211_hdr *) (pbyFrame);
1083 TKIPvMixKey(pKey->abyKey, pMACHeader->addr2, *pwRxTSC15_0, *pdwRxTSC47_16, pDevice->abyPRNG);
1084 rc4_init(&pDevice->SBox, pDevice->abyPRNG, TKIP_KEY_LEN);
1085 rc4_encrypt(&pDevice->SBox, pbyIV+8, pbyIV+8, PayloadLen);
1086 if (ETHbIsBufferCrc32Ok(pbyIV+8, PayloadLen)) {
1087 *pbyNewRsr |= NEWRSR_DECRYPTOK;
1088 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"ICV OK!\n");
1089 } else {
1090 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"ICV FAIL!!!\n");
1091 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"PayloadLen = %d\n", PayloadLen);
1094 }// end of TKIP/AES
1096 if ((*(pbyIV+3) & 0x20) != 0)
1097 *pbExtIV = true;
1098 return true;
1101 static int s_bHostWepRxEncryption(struct vnt_private *pDevice, u8 *pbyFrame,
1102 u32 FrameSize, u8 *pbyRsr, int bOnFly, PSKeyItem pKey, u8 *pbyNewRsr,
1103 s32 *pbExtIV, u16 *pwRxTSC15_0, u32 *pdwRxTSC47_16)
1105 struct vnt_manager *pMgmt = &pDevice->vnt_mgmt;
1106 struct ieee80211_hdr *pMACHeader;
1107 u32 PayloadLen = FrameSize;
1108 u8 *pbyIV;
1109 u8 byKeyIdx;
1110 u8 byDecMode = KEY_CTL_WEP;
1112 *pwRxTSC15_0 = 0;
1113 *pdwRxTSC47_16 = 0;
1115 pbyIV = pbyFrame + WLAN_HDR_ADDR3_LEN;
1116 if ( WLAN_GET_FC_TODS(*(u16 *)pbyFrame) &&
1117 WLAN_GET_FC_FROMDS(*(u16 *)pbyFrame) ) {
1118 pbyIV += 6; // 6 is 802.11 address4
1119 PayloadLen -= 6;
1121 byKeyIdx = (*(pbyIV+3) & 0xc0);
1122 byKeyIdx >>= 6;
1123 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"\nKeyIdx: %d\n", byKeyIdx);
1125 if (pMgmt->byCSSGK == KEY_CTL_TKIP)
1126 byDecMode = KEY_CTL_TKIP;
1127 else if (pMgmt->byCSSGK == KEY_CTL_CCMP)
1128 byDecMode = KEY_CTL_CCMP;
1130 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"AES:%d %d %d\n", pMgmt->byCSSPK, pMgmt->byCSSGK, byDecMode);
1132 if (byDecMode != pKey->byCipherSuite) {
1133 return false;
1136 if (byDecMode == KEY_CTL_WEP) {
1137 // handle WEP
1138 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"byDecMode == KEY_CTL_WEP\n");
1139 if ((pDevice->byLocalID <= REV_ID_VT3253_A1) ||
1140 (((PSKeyTable)(pKey->pvKeyTable))->bSoftWEP == true) ||
1141 (bOnFly == false)) {
1142 // Software WEP
1143 // 1. 3253A
1144 // 2. WEP 256
1145 // 3. NotOnFly
1147 PayloadLen -= (WLAN_HDR_ADDR3_LEN + 4 + 4); // 24 is 802.11 header,4 is IV, 4 is crc
1148 memcpy(pDevice->abyPRNG, pbyIV, 3);
1149 memcpy(pDevice->abyPRNG + 3, pKey->abyKey, pKey->uKeyLength);
1150 rc4_init(&pDevice->SBox, pDevice->abyPRNG, pKey->uKeyLength + 3);
1151 rc4_encrypt(&pDevice->SBox, pbyIV+4, pbyIV+4, PayloadLen);
1153 if (ETHbIsBufferCrc32Ok(pbyIV+4, PayloadLen)) {
1154 *pbyNewRsr |= NEWRSR_DECRYPTOK;
1157 } else if ((byDecMode == KEY_CTL_TKIP) ||
1158 (byDecMode == KEY_CTL_CCMP)) {
1159 // TKIP/AES
1161 PayloadLen -= (WLAN_HDR_ADDR3_LEN + 8 + 4); // 24 is 802.11 header, 8 is IV&ExtIV, 4 is crc
1162 *pdwRxTSC47_16 = cpu_to_le32(*(u32 *)(pbyIV + 4));
1163 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"ExtIV: %x\n", *pdwRxTSC47_16);
1165 if (byDecMode == KEY_CTL_TKIP) {
1166 *pwRxTSC15_0 = cpu_to_le16(MAKEWORD(*(pbyIV+2), *pbyIV));
1167 } else {
1168 *pwRxTSC15_0 = cpu_to_le16(*(u16 *)pbyIV);
1170 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"TSC0_15: %x\n", *pwRxTSC15_0);
1172 if (byDecMode == KEY_CTL_TKIP) {
1174 if ((pDevice->byLocalID <= REV_ID_VT3253_A1) || (bOnFly == false)) {
1175 // Software TKIP
1176 // 1. 3253 A
1177 // 2. NotOnFly
1178 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"soft KEY_CTL_TKIP \n");
1179 pMACHeader = (struct ieee80211_hdr *) (pbyFrame);
1180 TKIPvMixKey(pKey->abyKey, pMACHeader->addr2, *pwRxTSC15_0, *pdwRxTSC47_16, pDevice->abyPRNG);
1181 rc4_init(&pDevice->SBox, pDevice->abyPRNG, TKIP_KEY_LEN);
1182 rc4_encrypt(&pDevice->SBox, pbyIV+8, pbyIV+8, PayloadLen);
1183 if (ETHbIsBufferCrc32Ok(pbyIV+8, PayloadLen)) {
1184 *pbyNewRsr |= NEWRSR_DECRYPTOK;
1185 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"ICV OK!\n");
1186 } else {
1187 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"ICV FAIL!!!\n");
1188 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"PayloadLen = %d\n", PayloadLen);
1193 if (byDecMode == KEY_CTL_CCMP) {
1194 if (bOnFly == false) {
1195 // Software CCMP
1196 // NotOnFly
1197 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"soft KEY_CTL_CCMP\n");
1198 if (AESbGenCCMP(pKey->abyKey, pbyFrame, FrameSize)) {
1199 *pbyNewRsr |= NEWRSR_DECRYPTOK;
1200 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"CCMP MIC compare OK!\n");
1201 } else {
1202 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"CCMP MIC fail!\n");
1207 }// end of TKIP/AES
1209 if ((*(pbyIV+3) & 0x20) != 0)
1210 *pbExtIV = true;
1211 return true;
1214 static int s_bAPModeRxData(struct vnt_private *pDevice, struct sk_buff *skb,
1215 u32 FrameSize, u32 cbHeaderOffset, s32 iSANodeIndex, s32 iDANodeIndex)
1217 struct sk_buff *skbcpy;
1218 struct vnt_manager *pMgmt = &pDevice->vnt_mgmt;
1219 int bRelayAndForward = false;
1220 int bRelayOnly = false;
1221 u8 byMask[8] = {1, 2, 4, 8, 0x10, 0x20, 0x40, 0x80};
1222 u16 wAID;
1224 if (FrameSize > CB_MAX_BUF_SIZE)
1225 return false;
1226 // check DA
1227 if (is_multicast_ether_addr((u8 *)(skb->data+cbHeaderOffset))) {
1228 if (pMgmt->sNodeDBTable[0].bPSEnable) {
1230 skbcpy = dev_alloc_skb((int)pDevice->rx_buf_sz);
1232 // if any node in PS mode, buffer packet until DTIM.
1233 if (skbcpy == NULL) {
1234 DBG_PRT(MSG_LEVEL_NOTICE, KERN_INFO "relay multicast no skb available \n");
1236 else {
1237 skbcpy->dev = pDevice->dev;
1238 skbcpy->len = FrameSize;
1239 memcpy(skbcpy->data, skb->data+cbHeaderOffset, FrameSize);
1240 skb_queue_tail(&(pMgmt->sNodeDBTable[0].sTxPSQueue), skbcpy);
1241 pMgmt->sNodeDBTable[0].wEnQueueCnt++;
1242 // set tx map
1243 pMgmt->abyPSTxMap[0] |= byMask[0];
1246 else {
1247 bRelayAndForward = true;
1250 else {
1251 // check if relay
1252 if (BSSbIsSTAInNodeDB(pDevice, (u8 *)(skb->data+cbHeaderOffset), &iDANodeIndex)) {
1253 if (pMgmt->sNodeDBTable[iDANodeIndex].eNodeState >= NODE_ASSOC) {
1254 if (pMgmt->sNodeDBTable[iDANodeIndex].bPSEnable) {
1255 // queue this skb until next PS tx, and then release.
1257 skb->data += cbHeaderOffset;
1258 skb->tail += cbHeaderOffset;
1259 skb_put(skb, FrameSize);
1260 skb_queue_tail(&pMgmt->sNodeDBTable[iDANodeIndex].sTxPSQueue, skb);
1262 pMgmt->sNodeDBTable[iDANodeIndex].wEnQueueCnt++;
1263 wAID = pMgmt->sNodeDBTable[iDANodeIndex].wAID;
1264 pMgmt->abyPSTxMap[wAID >> 3] |= byMask[wAID & 7];
1265 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "relay: index= %d, pMgmt->abyPSTxMap[%d]= %d\n",
1266 iDANodeIndex, (wAID >> 3), pMgmt->abyPSTxMap[wAID >> 3]);
1267 return true;
1269 else {
1270 bRelayOnly = true;
1276 if (bRelayOnly || bRelayAndForward) {
1277 // relay this packet right now
1278 if (bRelayAndForward)
1279 iDANodeIndex = 0;
1281 if ((pDevice->uAssocCount > 1) && (iDANodeIndex >= 0)) {
1282 bRelayPacketSend(pDevice, (u8 *) (skb->data + cbHeaderOffset),
1283 FrameSize, (unsigned int) iDANodeIndex);
1286 if (bRelayOnly)
1287 return false;
1289 // none associate, don't forward
1290 if (pDevice->uAssocCount == 0)
1291 return false;
1293 return true;
1296 void RXvWorkItem(struct work_struct *work)
1298 struct vnt_private *pDevice =
1299 container_of(work, struct vnt_private, read_work_item);
1300 int ntStatus;
1301 struct vnt_rcb *pRCB = NULL;
1303 if (pDevice->Flags & fMP_DISCONNECTED)
1304 return;
1306 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"---->Rx Polling Thread\n");
1307 spin_lock_irq(&pDevice->lock);
1309 while ((pDevice->Flags & fMP_POST_READS) &&
1310 MP_IS_READY(pDevice) &&
1311 (pDevice->NumRecvFreeList != 0) ) {
1312 pRCB = pDevice->FirstRecvFreeList;
1313 pDevice->NumRecvFreeList--;
1314 DequeueRCB(pDevice->FirstRecvFreeList, pDevice->LastRecvFreeList);
1315 ntStatus = PIPEnsBulkInUsbRead(pDevice, pRCB);
1317 pDevice->bIsRxWorkItemQueued = false;
1318 spin_unlock_irq(&pDevice->lock);
1322 void RXvFreeRCB(struct vnt_rcb *pRCB, int bReAllocSkb)
1324 struct vnt_private *pDevice = pRCB->pDevice;
1326 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"---->RXvFreeRCB\n");
1328 if (bReAllocSkb == false) {
1329 kfree_skb(pRCB->skb);
1330 bReAllocSkb = true;
1333 if (bReAllocSkb == true) {
1334 pRCB->skb = dev_alloc_skb((int)pDevice->rx_buf_sz);
1335 // todo error handling
1336 if (pRCB->skb == NULL) {
1337 DBG_PRT(MSG_LEVEL_ERR,KERN_ERR" Failed to re-alloc rx skb\n");
1338 }else {
1339 pRCB->skb->dev = pDevice->dev;
1343 // Insert the RCB back in the Recv free list
1345 EnqueueRCB(pDevice->FirstRecvFreeList, pDevice->LastRecvFreeList, pRCB);
1346 pDevice->NumRecvFreeList++;
1348 if ((pDevice->Flags & fMP_POST_READS) && MP_IS_READY(pDevice) &&
1349 (pDevice->bIsRxWorkItemQueued == false) ) {
1351 pDevice->bIsRxWorkItemQueued = true;
1352 schedule_work(&pDevice->read_work_item);
1354 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"<----RXFreeRCB %d %d\n",pDevice->NumRecvFreeList, pDevice->NumRecvMngList);
1357 void RXvMngWorkItem(struct work_struct *work)
1359 struct vnt_private *pDevice =
1360 container_of(work, struct vnt_private, rx_mng_work_item);
1361 struct vnt_rcb *pRCB = NULL;
1362 struct vnt_rx_mgmt *pRxPacket;
1363 int bReAllocSkb = false;
1365 if (pDevice->Flags & fMP_DISCONNECTED)
1366 return;
1368 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"---->Rx Mng Thread\n");
1370 spin_lock_irq(&pDevice->lock);
1371 while (pDevice->NumRecvMngList!=0)
1373 pRCB = pDevice->FirstRecvMngList;
1374 pDevice->NumRecvMngList--;
1375 DequeueRCB(pDevice->FirstRecvMngList, pDevice->LastRecvMngList);
1376 if(!pRCB){
1377 break;
1379 pRxPacket = &(pRCB->sMngPacket);
1380 vMgrRxManagePacket(pDevice, &pDevice->vnt_mgmt, pRxPacket);
1381 pRCB->Ref--;
1382 if(pRCB->Ref == 0) {
1383 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"RxvFreeMng %d %d\n",pDevice->NumRecvFreeList, pDevice->NumRecvMngList);
1384 RXvFreeRCB(pRCB, bReAllocSkb);
1385 } else {
1386 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Rx Mng Only we have the right to free RCB\n");
1390 pDevice->bIsRxMngWorkItemQueued = false;
1391 spin_unlock_irq(&pDevice->lock);