1
/***************************************************************************
2
* RT2x00 SourceForge Project - http://rt2x00.sourceforge.net *
4
* This program is free software; you can redistribute it and/or modify *
5
* it under the terms of the GNU General Public License as published by *
6
* the Free Software Foundation; either version 2 of the License, or *
7
* (at your option) any later version. *
9
* This program is distributed in the hope that it will be useful, *
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12
* GNU General Public License for more details. *
14
* You should have received a copy of the GNU General Public License *
15
* along with this program; if not, write to the *
16
* Free Software Foundation, Inc., *
17
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19
* Licensed under the GNU GPL *
20
* Original code supplied under license from RaLink Inc, 2004. *
21
***************************************************************************/
23
/***************************************************************************
24
* Module Name: rtusb_data.c
30
* -------- ---------- -------------------------------
31
* Name Date Modification logs
32
* Jan Lee 2005-06-01 Release
33
* RobinC 02-06-2005 RFMON Mode added
34
* MathiasK 04-07-2005 big endian fix from gglomm
35
***************************************************************************/
37
#include "rt_config.h"
39
static UCHAR PlcpSignal[12] = {
40
0, /* RATE_1 */ 1, /* RATE_2 */ 2, /* RATE_5_5 */ 3, /* RATE_11 */ // see BBP spec
41
11, /* RATE_6 */ 15, /* RATE_9 */ 10, /* RATE_12 */ 14, /* RATE_18 */ // see IEEE802.11a-1999 p.14
42
9, /* RATE_24 */ 13, /* RATE_36 */ 8, /* RATE_48 */ 12 /* RATE_54 */ }; // see IEEE802.11a-1999 p.14
43
static UINT _11G_RATES[12] = { 0, 0, 0, 0, 6, 9, 12, 18, 24, 36, 48, 54 };
44
static UCHAR SNAP_802_1H[] = {0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00};
45
static UCHAR SNAP_BRIDGE_TUNNEL[] = {0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8};
46
static UCHAR EAPOL[] = {0x88, 0x8e};
47
static UCHAR IPX[] = {0x81, 0x37};
48
static UCHAR APPLE_TALK[] = {0x80, 0xf3};
49
UCHAR ARP[]={0x08, 0x06};
50
////////////////////////////////////////////////////////////////////////////
56
// VNETMultipleSend handler is called by NDIS to transmit packets
57
// through the adapter. If there are packets in the Q and the device
58
// can accept the Tx requests initiate a transmission and queue the
59
// rest of the packets (if any...). If we can not transmit or the
60
// station is not ready we imediatelly complete the request
63
// MiniportAdapterContext Context registered with the wrapper
64
// (Ptr to to the Adapter object)
65
// PacketArray Array of Ptrs to NDIS_PACKET structs
66
// NumberOfPackets Number of packets in PacketArray
71
////////////////////////////////////////////////////////////////////////////
72
int RTUSBSendPackets(struct sk_buff *skb, struct net_device *net_dev)
75
NDIS_STATUS Status = NDIS_STATUS_SUCCESS;
76
PRT2570ADAPTER pAdapter = net_dev->priv;
78
skb->data_len = skb->len;
79
if (RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_RESET_IN_PROGRESS) ||
80
RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_REMOVE_IN_PROGRESS) ||
81
RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_RADIO_OFF) ||
82
RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_HALT_IN_PROGRESS))
84
RTUSBFreeSkbBuffer(skb);
87
// Drop packets if no associations
88
else if (!INFRA_ON(pAdapter) && !ADHOC_ON(pAdapter) &&
89
!( pAdapter->PortCfg.BssType == BSS_MONITOR && pAdapter->PortCfg.MallowRFMONTx == TRUE )
92
RTUSBFreeSkbBuffer(skb);
97
Status = RTUSBSendPacket(pAdapter, skb);
98
if (Status != NDIS_STATUS_SUCCESS)
100
// Errors before enqueue stage
101
RTUSBFreeSkbBuffer(skb);
102
DBGPRINT(RT_DEBUG_TRACE,"<---RTUSBSendPackets not dequeue\n");
106
// Dequeue one frame from SendTxWait queue and process it
107
// There are two place calling dequeue for TX ring.
108
// 1. Here, right after queueing the frame.
109
// 2. At the end of TxRingTxDone service routine.
110
if ((!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_BSS_SCAN_IN_PROGRESS)) &&
111
(!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_RADIO_OFF)) &&
112
(!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_RESET_IN_PROGRESS)) &&
113
(!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_REMOVE_IN_PROGRESS)) &&
114
(!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_HALT_IN_PROGRESS)))
116
//RTMPDeQueuePacket(pAdapter, &pAdapter->TxSwQueue0);
117
// Call dequeue without selected queue, let the subroutine select the right priority
119
RTUSBDeQueuePacket(pAdapter);
123
RTUSBKickBulkOut(pAdapter);
127
NDIS_STATUS RTUSBSendPacket(
128
IN PRT2570ADAPTER pAdapter,
129
IN struct sk_buff *skb)
133
PVOID pVirtualAddress;
134
struct sk_buff_head *pTxQueue;
139
UCHAR AccessCategory;
140
NDIS_STATUS Status = NDIS_STATUS_FAILURE;
143
// Init priority value
147
if (skb && pAdapter->PortCfg.BssType == BSS_MONITOR &&
148
pAdapter->PortCfg.MallowRFMONTx == TRUE)
150
NdisAcquireSpinLock(&pAdapter->SendTxWaitQueueLock);
151
skb_queue_tail(&pAdapter->SendTxWaitQueue, skb);
152
NdisReleaseSpinLock(&pAdapter->SendTxWaitQueueLock);
153
return (NDIS_STATUS_SUCCESS);
158
Priority = skb->priority;
159
// 802.11e/d4.4 June, 2003
162
else if (Priority == 3)
164
else if (Priority <= 5)
168
DBGPRINT(RT_DEBUG_INFO, "Priority = %d, AC = %d\n", Priority, AccessCategory);
170
// For TKIP, MIC value is treated as payload, it might be fragmented through
172
if (pAdapter->PortCfg.WepStatus == Ndis802_11Encryption2Enabled)
177
pVirtualAddress = (PVOID)skb->data;
178
// Check for virtual address allocation, it might fail !!!
179
if (pVirtualAddress == NULL)
181
DBGPRINT(RT_DEBUG_TRACE,"<---RTUSBSendPacket NULL pVirtualAddress\n");
182
// Resourece is low, system did not allocation virtual address
183
// return NDIS_STATUS_FAILURE directly to upper layer
188
// Check for multicast or broadcast (First byte of DA)
190
if ((*((PUCHAR) pVirtualAddress) & 0x01) != 0)
192
// For multicast & broadcast, there is no fragment allowed
197
// Check for payload allowed for each fragment
198
AllowFragSize = (pAdapter->PortCfg.FragmentThreshold) - LENGTH_802_11 - LENGTH_CRC;
200
// Calculate fragments required
201
NumberOfFrag = ((skb->data_len - LENGTH_802_3 + LENGTH_802_1_H) / AllowFragSize) + 1;
202
// Minus 1 if the size just match to allowable fragment size
203
if (((skb->data_len - LENGTH_802_3 + LENGTH_802_1_H) % AllowFragSize) == 0)
209
// Check for requirement of RTS
210
if (NumberOfFrag > 1)
212
// If multiple fragment required, RTS is required only for the first fragment
213
// if the fragment size large than RTS threshold
214
RTSRequired = (pAdapter->PortCfg.FragmentThreshold > pAdapter->PortCfg.RtsThreshold) ? 1 : 0;
218
RTSRequired = (skb->data_len > pAdapter->PortCfg.RtsThreshold) ? 1 : 0;
221
// RTS/CTS may also be required in order to protect OFDM frame
222
if ((pAdapter->PortCfg.TxRate >= RATE_FIRST_OFDM_RATE) && pAdapter->PortCfg.BGProtectionInUsed)
224
//DBGPRINT(RT_DEBUG_TEMP, "Number of fragments :%d , include RTS :%d\n", NumberOfFrag, NumberOfFrag + RTSRequired);
226
// Save framnet number to Ndis packet reserved field
227
RTMP_SET_PACKET_FRAGMENTS(skb, NumberOfFrag);
229
// Save RTS requirement to Ndis packet reserved field
230
RTMP_SET_PACKET_RTS(skb, RTSRequired);
232
// Make sure SendTxWait queue resource won't be used by other threads
233
NdisAcquireSpinLock(&pAdapter->SendTxWaitQueueLock);
235
pTxQueue = &pAdapter->SendTxWaitQueue;
236
if (INFRA_ON(pAdapter))
238
// In infrastructure mode, simply enqueue the packet into Tx waiting queue.
239
DBGPRINT(RT_DEBUG_INFO, "Infrastructure -> Enqueue one frame\n");
241
// Enqueue Ndis packet to end of Tx wait queue
242
skb_queue_tail(pTxQueue, skb);
243
Status = NDIS_STATUS_SUCCESS;
247
// In IBSS mode, power state of destination should be considered.
248
PsMode = PWR_ACTIVE; // Faked
249
if (PsMode == PWR_ACTIVE)
251
// Enqueue Ndis packet to end of Tx wait queue
252
skb_queue_tail(pTxQueue, skb);
253
Status = NDIS_STATUS_SUCCESS;
257
NdisReleaseSpinLock(&pAdapter->SendTxWaitQueueLock);
261
VOID RTUSBDeQueuePacket(
262
IN PRT2570ADAPTER pAdapter)
265
UCHAR FragmentRequired;
266
NDIS_STATUS Status = NDIS_STATUS_SUCCESS;
268
struct sk_buff_head *pQueue;
270
// Make sure SendTxWait queue resource won't be used by other threads
271
NdisAcquireSpinLock(&pAdapter->SendTxWaitQueueLock);
274
pQueue = &pAdapter->SendTxWaitQueue;
276
// Check queue before dequeue
277
while (!skb_queue_empty(pQueue) && (Count < MAX_TX_PROCESS))
279
// Reset is in progress, stop immediately
280
if ( RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_RESET_IN_PROGRESS) ||
281
RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_REMOVE_IN_PROGRESS) ||
282
RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_HALT_IN_PROGRESS))
287
// Dequeue the first entry from head of queue list
288
skb = skb_dequeue(pQueue);
291
// RTS or CTS-to-self for B/G protection mode has been set already.
292
// There is no need to re-do it here.
293
// Total fragment required = number of fragment + RST if required
294
FragmentRequired = RTMP_GET_PACKET_FRAGMENTS(skb) + RTMP_GET_PACKET_RTS(skb);
296
if (RTUSBFreeDescriptorRequest(pAdapter, TX_RING, FragmentRequired) == NDIS_STATUS_SUCCESS)
298
// Avaliable ring descriptors are enough for this frame
299
// Call hard transmit
300
// Nitro mode / Normal mode selection
301
NdisReleaseSpinLock(&pAdapter->SendTxWaitQueueLock);
302
if (pAdapter->PortCfg.EnableTxBurst == 1)
303
Status = RTUSBHardEncrypt(pAdapter, skb, FragmentRequired, TRUE);
305
Status = RTUSBHardEncrypt(pAdapter, skb, FragmentRequired, FALSE);
307
// Acquire the resource again, snice we may need to process it in this while-loop.
309
NdisAcquireSpinLock(&pAdapter->SendTxWaitQueueLock);
310
if (Status == NDIS_STATUS_FAILURE)
312
// Packet failed due to various Ndis Packet error
313
RTUSBFreeSkbBuffer(skb);
316
else if (Status == NDIS_STATUS_RESOURCES)
318
// Not enough free tx ring, it might happen due to free descriptor inquery might be not correct
319
// It also might change to NDIS_STATUS_FAILURE to simply drop the frame
320
// Put the frame back into head of queue
321
skb_queue_head(pQueue, skb);
328
skb_queue_head(pQueue, skb);
333
NdisReleaseSpinLock(&pAdapter->SendTxWaitQueueLock);
338
NDIS_STATUS RTUSBFreeDescriptorRequest(
339
IN PRT2570ADAPTER pAdapter,
341
IN UCHAR NumberRequired)
343
UCHAR FreeNumber = 0;
345
NDIS_STATUS Status = NDIS_STATUS_FAILURE;
350
Index = pAdapter->NextTxIndex;
353
PTX_CONTEXT pTxD = &pAdapter->TxContext[Index];
355
// While Owner bit is NIC, obviously ASIC still need it.
356
// If valid bit is TRUE, indicate that TxDone has not process yet
357
// We should not use it until TxDone finish cleanup job
358
if (pTxD->InUse == FALSE)
367
Index = (Index + 1) % TX_RING_SIZE;
368
} while (FreeNumber < NumberRequired); // Quit here ! Free number is enough !
370
if (FreeNumber >= NumberRequired)
372
Status = NDIS_STATUS_SUCCESS;
378
Index = pAdapter->NextMLMEIndex;
381
PTX_CONTEXT pTxD = &pAdapter->MLMEContext[Index];
383
// While Owner bit is NIC, obviously ASIC still need it.
384
// If valid bit is TRUE, indicate that TxDone has not process yet
385
// We should not use it until TxDone finish cleanup job
386
if (pTxD->InUse == FALSE)
396
Index = (Index + 1) % PRIO_RING_SIZE;
397
} while (FreeNumber < NumberRequired); // Quit here ! Free number is enough !
399
if (FreeNumber >= NumberRequired)
401
Status = NDIS_STATUS_SUCCESS;
416
========================================================================
428
========================================================================
430
VOID RTUSBRejectPendingPackets(
431
IN PRT2570ADAPTER pAdapter)
433
DBGPRINT_RAW(RT_DEBUG_TRACE, "--->RejectPendingPackets\n");
435
NdisAcquireSpinLock(&pAdapter->SendTxWaitQueueLock);
436
DBGPRINT_RAW(RT_DEBUG_TRACE, "Purge SendTxWaitQueue\n");
437
skb_queue_purge(&pAdapter->SendTxWaitQueue);
438
NdisReleaseSpinLock(&pAdapter->SendTxWaitQueueLock);
440
DBGPRINT_RAW(RT_DEBUG_TRACE, "<---RejectPendingPackets\n");
444
========================================================================
447
Suspend MSDU transmission
450
pAdapter Pointer to our adapter
457
========================================================================
459
VOID RTUSBSuspendMsduTransmission(
460
IN PRT2570ADAPTER pAdapter)
462
DBGPRINT(RT_DEBUG_TRACE,"SCANNING, suspend MSDU transmission ...\n");
463
RTMP_SET_FLAG(pAdapter, fRTMP_ADAPTER_BSS_SCAN_IN_PROGRESS);
467
========================================================================
470
Resume MSDU transmission
473
pAdapter Pointer to our adapter
480
========================================================================
482
VOID RTUSBResumeMsduTransmission(
483
IN PRT2570ADAPTER pAdapter)
485
DBGPRINT(RT_DEBUG_TRACE,"SCANNING, resume MSDU transmission ...\n");
486
RTMP_CLEAR_FLAG(pAdapter, fRTMP_ADAPTER_BSS_SCAN_IN_PROGRESS);
487
if ((!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_RESET_IN_PROGRESS)) &&
488
(!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_HALT_IN_PROGRESS)) &&
489
(!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_REMOVE_IN_PROGRESS)) &&
490
(!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_RADIO_OFF)))
492
// Call dequeue without selected queue, let the subroutine select the right priority
494
RTUSBDeQueuePacket(pAdapter);
498
RTUSBKickBulkOut(pAdapter);
502
========================================================================
512
========================================================================
514
USHORT RTUSBCalcDuration(
515
IN PRT2570ADAPTER pAdapter,
521
if (Rate < RATE_FIRST_OFDM_RATE) // CCK
523
if ((Rate > RATE_1) && (pAdapter->PortCfg.TxPreambleInUsed == Rt802_11PreambleShort))
524
Duration = 96; // 72+24 preamble+plcp
526
Duration = 192; // 144+48 preamble+plcp
528
Duration += (USHORT)((Size << 4) / RateIdTo500Kbps[Rate]);
529
if ((Size << 4) % RateIdTo500Kbps[Rate])
534
Duration = 20 + 6; // 16+4 preamble+plcp + Signal Extension
535
Duration += 4 * (USHORT)((11 + Size * 4) / RateIdTo500Kbps[Rate]);
536
if ((11 + Size * 4) % RateIdTo500Kbps[Rate])
540
return (USHORT)Duration;
544
========================================================================
547
Calculates the duration which is required to transmit out frames
548
with given size and specified rate.
551
pTxD Pointer to transmit descriptor
552
Ack Setting for Ack requirement bit
553
Fragment Setting for Fragment bit
554
RetryMode Setting for retry mode
555
Ifs Setting for IFS gap
556
Rate Setting for transmit rate
557
Service Setting for service
563
========================================================================
565
VOID RTUSBWriteTxDescriptor(
570
IN BOOLEAN InsTimestamp,
581
IN USHORT TxPreamble)
585
pTxD->RetryLimit = RetryLimit;
586
pTxD->MoreFrag = Fragment;
588
pTxD->Timestamp = InsTimestamp;
589
pTxD->newseq = new_seq;
591
pTxD->DataByteCnt = Length;
592
pTxD->Cipher = Cipher;
594
pTxD->CWmin = CWMin; // 2^5-1 = 31
595
pTxD->CWmax = CWMax; // 2^10 -1 = 1023
596
pTxD->Aifs = 2; // TC0: SIFS + 2*Slot + Random(CWmin,CWmax)*Slot
598
if (Rate < RATE_FIRST_OFDM_RATE)
603
// fill up PLCP SIGNAL field
604
pTxD->PlcpSignal = PlcpSignal[Rate];
605
if (((Rate == RATE_2) || (Rate == RATE_5_5) || (Rate == RATE_11)) && (TxPreamble == Rt802_11PreambleShort)) // no short preamble for RATE_1
607
pTxD->PlcpSignal |= 0x0008;
610
// fill up PLCP SERVICE field, not used for OFDM rates
611
pTxD->PlcpService = Service;
613
// file up PLCP LENGTH_LOW and LENGTH_HIGH fields
614
if (Rate < RATE_FIRST_OFDM_RATE) // 11b - RATE_1, RATE_2, RATE_5_5, RATE_11
616
if ((Rate == RATE_1) || ( Rate == RATE_2))
618
PLCPLength = PLCPLength * 8 / (Rate + 1);
622
Residual = ((PLCPLength * 16) % (11 * (1 + Rate - RATE_5_5)));
623
PLCPLength = PLCPLength * 16 / (11 * (1 + Rate - RATE_5_5));
630
if ((Residual <= (3 * (1 + Rate - RATE_5_5))) && (Residual != 0))
632
pTxD->PlcpService |= 0x80; // 11b's PLCP Length extension bit
637
pTxD->PlcpLengthHigh = PLCPLength / 256;
638
pTxD->PlcpLengthLow = PLCPLength % 256;
640
else // OFDM - RATE_6, RATE_9, RATE_12, RATE_18, RATE_24, RATE_36, RATE_48, RATE_54
642
pTxD->PlcpLengthHigh = PLCPLength / 64; // high 6-bit of total byte count
643
pTxD->PlcpLengthLow = PLCPLength % 64; // low 6-bit of total byte count
648
========================================================================
651
Calculates the duration which is required to transmit out frames
652
with given size and specified rate.
655
pTxD Pointer to transmit descriptor
656
Ack Setting for Ack requirement bit
657
Fragment Setting for Fragment bit
658
RetryMode Setting for retry mode
659
Ifs Setting for IFS gap
660
Rate Setting for transmit rate
661
Service Setting for service
667
========================================================================
669
VOID RTUSBWriteBeaconDescriptor(
675
IN USHORT TxPreamble)
679
pTxD->RetryLimit = 0;
684
pTxD->IFS = IFS_NEW_BACKOFF;
685
pTxD->DataByteCnt = Length;
688
pTxD->CWmin = BEACON_CW_IN_BITS; // 2^5-1 = 31
689
pTxD->CWmax = BEACON_CW_IN_BITS; // 2^10 -1 = 1023
690
pTxD->Aifs = 2; // TC0: SIFS + 2*Slot + Random(CWmin,CWmax)*Slot
692
if (Rate < RATE_FIRST_OFDM_RATE)
697
// fill up PLCP SIGNAL field
698
pTxD->PlcpSignal = PlcpSignal[Rate];
699
if (((Rate == RATE_2) || (Rate == RATE_5_5) || (Rate == RATE_11)) && (TxPreamble == Rt802_11PreambleShort)) // no short preamble for RATE_1
701
pTxD->PlcpSignal |= 0x0008;
704
// fill up PLCP SERVICE field, not used for OFDM rates
705
pTxD->PlcpService = Service;
707
// file up PLCP LENGTH_LOW and LENGTH_HIGH fields
708
if (Rate < RATE_FIRST_OFDM_RATE) // 11b - RATE_1, RATE_2, RATE_5_5, RATE_11
710
if ((Rate == RATE_1) || ( Rate == RATE_2))
712
PLCPLength = PLCPLength * 8 / (Rate + 1);
716
Residual = ((PLCPLength * 16) % (11 * (1 + Rate - RATE_5_5)));
717
PLCPLength = PLCPLength * 16 / (11 * (1 + Rate - RATE_5_5));
722
if ((Residual <= (3 * (1 + Rate - RATE_5_5))) && (Residual != 0))
724
pTxD->PlcpService |= 0x80; // 11b's PLCP Length extension bit
728
pTxD->PlcpLengthHigh = PLCPLength / 256;
729
pTxD->PlcpLengthLow = PLCPLength % 256;
731
else // OFDM - RATE_6, RATE_9, RATE_12, RATE_18, RATE_24, RATE_36, RATE_48, RATE_54
733
pTxD->PlcpLengthHigh = PLCPLength / 64; // high 6-bit of total byte count
734
pTxD->PlcpLengthLow = PLCPLength % 64; // low 6-bit of total byte count
739
========================================================================
742
Copy frame from waiting queue into relative ring buffer and set
743
appropriate ASIC register to kick hardware encryption before really
747
pAdapter Pointer to our adapter
748
PNDIS_PACKET Pointer to outgoing Ndis frame
749
NumberOfFrag Number of fragment required
754
IRQL = DISPATCH_LEVEL
758
========================================================================
760
NDIS_STATUS RTUSBHardEncrypt(
761
IN PRT2570ADAPTER pAdapter,
762
IN struct sk_buff *skb,
763
IN UCHAR NumberRequired,
764
IN ULONG EnableTxBurst)
766
PVOID pVirtualAddress;
767
UINT NdisBufferLength;
769
UINT TxSize, PLCPLength;
774
HEADER_802_11 Header_802_11;
777
PUCHAR pEncap = NULL;
778
PTX_CONTEXT pTxContext;
780
BOOLEAN StartOfFrame;
786
PWPA_KEY pWpaKey = (PWPA_KEY) NULL;
789
ULONG TransferBufferLength;
790
BOOLEAN MoreFragment;
791
UCHAR AckRate = RATE_2;
792
USHORT AckDuration = 0;
793
USHORT EncryptionOverhead = 0;
796
//for re-calculating the number of Fragment required.
799
UINT TotalPacketLength;
800
// To indicate cipher used for this packet
801
NDIS_802_11_ENCRYPTION_STATUS CipherSuite;
803
CipherSuite = pAdapter->PortCfg.WepStatus;
804
if (EnableTxBurst == 1)
807
FrameGap = IFS_BACKOFF; // Default frame gap mode
808
// Sequence Number is identical for all fragments belonged to the same frame
809
// Sequence is 0 - 4095
810
pAdapter->Sequence = ((pAdapter->Sequence) + 1) & (MAX_SEQ_NUMBER);
811
AckRate = pAdapter->PortCfg.ExpectedACKRate[pAdapter->PortCfg.TxRate];
812
AckDuration = RTUSBCalcDuration(pAdapter, AckRate, 14);
814
pVirtualAddress = skb->data;
815
NdisBufferLength = skb->len;
816
if(pVirtualAddress == NULL)
818
DBGPRINT(RT_DEBUG_ERROR, "Error, Null skb data buffer!!!\n");
819
return (NDIS_STATUS_FAILURE);
822
if (pAdapter->PortCfg.BssType == BSS_MONITOR && pAdapter->PortCfg.MallowRFMONTx == TRUE)
824
pTxContext = &pAdapter->TxContext[pAdapter->NextTxIndex];
825
pTxContext->InUse = TRUE;
826
pTxContext->LastOne = TRUE;
828
pAdapter->NextTxIndex++;
829
if (pAdapter->NextTxIndex >= TX_RING_SIZE)
830
pAdapter->NextTxIndex = 0;
832
pTxD = &(pTxContext->TransferBuffer->TxDesc);
833
memset(pTxD, 0, sizeof(TXD_STRUC));
834
pDest = pTxContext->TransferBuffer->WirelessPacket;
836
memcpy( pDest, skb->data, skb->len );
838
RTUSBWriteTxDescriptor(pTxD, FALSE, 0, FALSE, FALSE, TRUE, IFS_BACKOFF, skb->len, FALSE, 0, CW_MIN_IN_BITS, CW_MAX_IN_BITS, skb->len + 4, pAdapter->PortCfg.TxRate, 4, pAdapter->PortCfg.TxPreambleInUsed);
840
TransferBufferLength = skb->len + sizeof(TXD_STRUC);
841
if ((TransferBufferLength % 2) == 1)
842
TransferBufferLength++;
844
pTxContext->BulkOutSize = TransferBufferLength;
845
atomic_inc(&pAdapter->TxCount);
846
RTUSB_SET_BULK_FLAG(pAdapter, fRTUSB_BULK_OUT_DATA_FRAG);
847
RTUSBFreeSkbBuffer(skb);
848
return (NDIS_STATUS_SUCCESS);
851
if (NdisBufferLength < 14)
853
DBGPRINT_RAW(RT_DEBUG_ERROR, "RTUSBHardEncrypt --> Ndis Packet buffer error !!!\n");
854
return (NDIS_STATUS_FAILURE);
856
if ((*((PUCHAR) pVirtualAddress) & 0x01) != 0) // Multicast or Broadcast
858
INC_COUNTER(pAdapter->WlanCounters.MulticastTransmittedFrameCount);
866
// New control flag for sending DHCP & BOOTP usinf 1MB rate
867
if ((NumberRequired - RTUSB_GET_PACKET_RTS(skb)) == 1)
876
// Add 802.11x protocol check.
877
// For non-WPA network, 802.11x message should not encrypt even
878
// the privacy is on.
879
if ((memcmp(EAPOL, ((PUCHAR) pVirtualAddress) + 12, 2) == 0))
882
if (pAdapter->PortCfg.MicErrCnt >= 2)//steven:???
883
pAdapter->PortCfg.MicErrCnt++;
888
} // Initialize 802.11 header for each frame
890
// WPA 802.1x secured port control
891
if (((pAdapter->PortCfg.AuthMode == Ndis802_11AuthModeWPA) ||
892
(pAdapter->PortCfg.AuthMode == Ndis802_11AuthModeWPAPSK)) &&
893
((pAdapter->PortCfg.PortSecured == WPA_802_1X_PORT_NOT_SECURED) ||
894
(pAdapter->PortCfg.MicErrCnt >= 2)) &&
895
(EAPOLFrame == FALSE))
897
DBGPRINT_RAW(RT_DEBUG_INFO, "RTUSBHardEncrypt --> Drop packet before port secured !!!\n");
898
return (NDIS_STATUS_FAILURE);
901
memset(&Header_802_11, 0, sizeof(HEADER_802_11));
903
// Start making 802.11 frame header
905
if (INFRA_ON(pAdapter))
907
// In BSS mode, AP's address(BSSID) is the destination address of all outgoing packets
909
memcpy(&Header_802_11.Controlhead.Addr1, &pAdapter->PortCfg.Bssid, ETH_LENGTH_OF_ADDRESS);
911
memcpy(&Header_802_11.Addr3, (PUCHAR) pVirtualAddress, ETH_LENGTH_OF_ADDRESS);
912
Header_802_11.Controlhead.Frame.ToDs = 1;
917
memcpy(&Header_802_11.Controlhead.Addr1, (PUCHAR) pVirtualAddress, ETH_LENGTH_OF_ADDRESS);
919
memcpy(&Header_802_11.Addr3, &pAdapter->PortCfg.Bssid, ETH_LENGTH_OF_ADDRESS);
921
// Address 2 - SA in both infrastructure & ad-hoc modes
922
memcpy(&Header_802_11.Controlhead.Addr2, pAdapter->CurrentAddress, ETH_LENGTH_OF_ADDRESS);
924
// Header_802_11.Sequence = pAdapter->Sequence; // Sequence number
925
Header_802_11.Controlhead.Frame.Type = BTYPE_DATA; // Frame type
926
Header_802_11.Controlhead.Frame.PwrMgt = (pAdapter->PortCfg.Psm == PWR_SAVE);
928
// For the purpose to calculate duration for the second last fragment
929
RemainSize = skb->data_len - LENGTH_802_3 + LENGTH_CRC;
931
MICFrag = FALSE; // Flag to indicate MIC shall spread into two MPDUs
934
pSrc = (PUCHAR)pVirtualAddress;
935
Protocol = *(pSrc + 12) * 256 + *(pSrc + 13);
937
if (Protocol > 1500) // CHeck for LLC encaped
940
// Large than 1500 means it's a type field, and thus a D/I/X packet.
942
pEncap = SNAP_802_1H;
944
if ((memcmp(IPX, pSrc + 12, 2) == 0) ||
945
(memcmp(APPLE_TALK, pSrc + 12, 2) == 0))
947
pEncap = SNAP_BRIDGE_TUNNEL;
953
//means it's a length field, thus an 802.3 packet
954
//And we need to re-calculate the number of Fragment required.
955
TotalPacketLength = skb->data_len;
957
//means it's a length field, thus an 802.3 packet
958
//And we need to re-calculate the number of Fragment required.
960
// For TKIP, MIC value is treated as payload, it might be fragmented through
962
if (pAdapter->PortCfg.GroupCipher == Ndis802_11Encryption2Enabled)
964
TotalPacketLength = skb->data_len + 8;
967
// Check for payload allowed for each fragment
968
AllowFragSize = (pAdapter->PortCfg.FragmentThreshold) - LENGTH_802_11 - LENGTH_CRC;
970
// Calculate fragments required
971
NumberOfFrag = ((TotalPacketLength - LENGTH_802_3) / AllowFragSize) + 1;
972
// Minus 1 if the size just match to allowable fragment size
973
if (((skb->data_len - LENGTH_802_3) % AllowFragSize) == 0)
979
if (NumberOfFrag != RTUSB_GET_PACKET_FRAGMENTS(skb))
981
DBGPRINT(RT_DEBUG_TRACE, "Original fragment required = %d, new fragment required = %d\n",
982
RTUSB_GET_PACKET_FRAGMENTS(skb), NumberOfFrag);
984
// Update number of Fragment
986
RTUSB_SET_PACKET_FRAGMENTS(skb, NumberOfFrag);
987
NumberRequired = RTUSB_GET_PACKET_FRAGMENTS(skb) + RTUSB_GET_PACKET_RTS(skb);
992
// calcuate the overhead bytes that encryption algorithm may add. This
993
// affects the calculate of "duration" field
995
if ((CipherSuite == Ndis802_11Encryption1Enabled) &&
996
(pAdapter->PortCfg.SharedKey[pAdapter->PortCfg.DefaultKeyId].KeyLen != 0))
997
EncryptionOverhead = 8; // WEP: IV + ICV
998
else if (CipherSuite == Ndis802_11Encryption2Enabled)
999
EncryptionOverhead = 12; // TKIP: IV + EIV + ICV, MIC already added to TotalPacketLength
1000
else if (CipherSuite == Ndis802_11Encryption3Enabled)
1001
EncryptionOverhead = 16; // AES: IV + EIV + Hardware MIC
1003
EncryptionOverhead = 0;
1006
// Make RTS frame if required
1008
if (RTUSB_GET_PACKET_RTS(skb))
1010
PCONTROL_HEADER pControlHeader;
1012
//UINT RTSFrameSize; //used only to calculate duration
1014
DBGPRINT_RAW(RT_DEBUG_INFO, "Making RTS Frame\n");
1016
pTxContext = &pAdapter->TxContext[pAdapter->NextTxIndex];
1017
pTxContext->InUse = TRUE;
1018
pTxContext->LastOne = FALSE;
1020
// Increase & maintain Tx Ring Index
1021
pAdapter->NextTxIndex++;
1022
if (pAdapter->NextTxIndex >= TX_RING_SIZE)
1024
pAdapter->NextTxIndex = 0;
1027
pTxD = &(pTxContext->TransferBuffer->TxDesc);
1028
memset(pTxD, 0, sizeof(TXD_STRUC));
1029
pDest = pTxContext->TransferBuffer->WirelessPacket;
1031
pControlHeader = (PCONTROL_HEADER)pDest;
1032
memset(pControlHeader, 0, sizeof(CONTROL_HEADER));
1035
pControlHeader->Frame.Type = BTYPE_CNTL;
1036
if (pAdapter->PortCfg.BGProtectionInUsed == 1)
1038
pControlHeader->Frame.Subtype = SUBTYPE_CTS;
1039
memcpy(&pControlHeader->Addr1, pAdapter->CurrentAddress, ETH_LENGTH_OF_ADDRESS);
1043
pControlHeader->Frame.Subtype = SUBTYPE_RTS;
1045
if (INFRA_ON(pAdapter))
1047
memcpy(&pControlHeader->Addr1, &pAdapter->PortCfg.Bssid, ETH_LENGTH_OF_ADDRESS);
1051
memcpy(&pControlHeader->Addr1, (PUCHAR) pVirtualAddress, ETH_LENGTH_OF_ADDRESS);
1054
memcpy(&pControlHeader->Addr2, pAdapter->CurrentAddress, ETH_LENGTH_OF_ADDRESS);
1057
// Calculate duration = 2 SIFS + CTS + Data Frame size
1058
if (RTUSB_GET_PACKET_FRAGMENTS(skb) > 1)
1060
// If fragment required, size is maximum fragment size
1061
NextFragSize = pAdapter->PortCfg.FragmentThreshold;
1065
// Size should be frame with 802.11 header & CRC
1066
NextFragSize = skb->data_len + LENGTH_802_11 + LENGTH_CRC - LENGTH_802_3;
1069
NextFragSize += LENGTH_802_1_H;
1071
pControlHeader->Duration = 2 * (pAdapter->PortCfg.Dsifs)
1072
+ RTUSBCalcDuration(pAdapter, pAdapter->PortCfg.TxRate, NextFragSize + EncryptionOverhead)
1075
// Write Tx descriptor
1076
// Don't kick tx start until all frames are prepared
1077
// RTS has to set more fragment bit for fragment burst
1078
// RTS did not encrypt
1079
if (pAdapter->PortCfg.BGProtectionInUsed == 1)
1081
RTUSBWriteTxDescriptor(pTxD, FALSE, 7, FALSE, FALSE, FALSE, FrameGap, 10, FALSE, 0, CW_MIN_IN_BITS, CW_MAX_IN_BITS, 14, pAdapter->PortCfg.RtsRate, 4, pAdapter->PortCfg.TxPreambleInUsed);
1085
RTUSBWriteTxDescriptor(pTxD, FALSE, 7, TRUE, FALSE, FALSE, FrameGap, sizeof(CONTROL_HEADER), FALSE, 0, CW_MIN_IN_BITS, CW_MAX_IN_BITS, sizeof(CONTROL_HEADER) + 4, pAdapter->PortCfg.RtsRate, 4, pAdapter->PortCfg.TxPreambleInUsed);
1086
//steven:should we need this? pTxD->RTS = 1;
1089
TransferBufferLength = sizeof(CONTROL_HEADER) + sizeof(TXD_STRUC);
1090
if ((TransferBufferLength % 2) == 1)
1091
TransferBufferLength++;
1093
pTxContext->BulkOutSize = TransferBufferLength;
1097
// Increase BulkOut stanby count.
1099
atomic_inc(&pAdapter->TxCount);
1101
RTUSB_SET_BULK_FLAG(pAdapter, fRTUSB_BULK_OUT_DATA_NORMAL);
1103
// Find the WPA key, either Group or Pairwise Key//steven:according to Controlhead.Addr1 (only when AuthMode >= Ndis802_11AuthModeWPA)
1104
if (pAdapter->PortCfg.AuthMode >= Ndis802_11AuthModeWPA)
1108
pWpaKey = (PWPA_KEY) NULL;
1109
// First lokup the DA, if it's a group address, use GROUP key
1110
if (Header_802_11.Controlhead.Addr1.Octet[0] & 0x01)
1112
if (pAdapter->PortCfg.GroupKey[pAdapter->PortCfg.DefaultKeyId].KeyLen != 0)
1114
pWpaKey = (PWPA_KEY) &pAdapter->PortCfg.GroupKey[pAdapter->PortCfg.DefaultKeyId];
1115
pWpaKey->Type = GROUP_KEY;
1116
KeyID = pAdapter->PortCfg.DefaultKeyId;//for Tx descriptor
1117
DBGPRINT(RT_DEBUG_INFO, "Tx Use Group Key\n");
1120
// Try to find the Pairwise Key
1123
for (idx = 0; idx < PAIRWISE_KEY_NO; idx++)
1125
if (((memcmp(&Header_802_11.Controlhead.Addr1, pAdapter->PortCfg.PairwiseKey[idx].BssId, 6)== 0)) &&
1126
(pAdapter->PortCfg.PairwiseKey[idx].KeyLen != 0))
1128
pWpaKey = (PWPA_KEY) &pAdapter->PortCfg.PairwiseKey[idx];
1129
pWpaKey->Type = PAIRWISE_KEY;
1131
DBGPRINT(RT_DEBUG_INFO, "Tx Use Pairwise Key\n");
1135
// Use default Group Key if there is no Pairwise key present
1136
if ((pAdapter->PortCfg.GroupKey[pAdapter->PortCfg.DefaultKeyId].KeyLen != 0) && (pWpaKey == NULL))
1138
pWpaKey = (PWPA_KEY) &pAdapter->PortCfg.GroupKey[pAdapter->PortCfg.DefaultKeyId];
1139
pWpaKey->Type = GROUP_KEY;
1140
KeyID = pAdapter->PortCfg.DefaultKeyId;//for Tx descriptor
1141
DBGPRINT(RT_DEBUG_INFO, "Tx Use Group Key\n");
1146
if (pWpaKey != NULL)
1150
DBGPRINT(RT_DEBUG_INFO, "RTMPHardEncrypt TKIP Key = ");
1151
for (i = 0; i < 16; i++)
1153
DBGPRINT_RAW(RT_DEBUG_INFO, "%02x:", pWpaKey->Key[i]);
1155
DBGPRINT_RAW(RT_DEBUG_INFO, "\n");
1156
DBGPRINT(RT_DEBUG_INFO, "RTMPHardEncrypt TKIP TxMic = ");
1157
for (i = 0; i < 8; i++)
1159
DBGPRINT_RAW(RT_DEBUG_INFO, "%02x:", pWpaKey->TxMic[i]);
1161
DBGPRINT_RAW(RT_DEBUG_INFO, "\n");
1162
DBGPRINT(RT_DEBUG_INFO, "RTMPHardEncrypt TKIP TxTsc = ");
1163
for (i = 0; i < 6; i++)
1165
DBGPRINT_RAW(RT_DEBUG_INFO, "%02x:", pWpaKey->TxTsc[i]);
1167
DBGPRINT_RAW(RT_DEBUG_INFO, "\n");
1170
StartOfFrame = TRUE;
1171
// Start Copy Ndis Packet into Ring buffer.
1172
// For frame required more than one ring buffer (fragment), all ring buffers
1173
// have to be filled before kicking start tx bit.
1176
// NdisAcquireSpinLock(&pAdapter->TxRingLock);
1177
// Get the Tx Ring descriptor & Dma Buffer address
1178
pTxContext = &pAdapter->TxContext[pAdapter->NextTxIndex];
1179
pTxContext->InUse = TRUE;
1180
pTxContext->LastOne = FALSE;
1182
// Increase & maintain Tx Ring Index
1183
pAdapter->NextTxIndex++;
1184
if (pAdapter->NextTxIndex >= TX_RING_SIZE)
1186
pAdapter->NextTxIndex = 0;
1188
// NdisReleaseSpinLock(&pAdapter->TxRingLock);
1190
pTxD = &(pTxContext->TransferBuffer->TxDesc);
1191
memset(pTxD, 0, sizeof(TXD_STRUC));
1192
pDest = pTxContext->TransferBuffer->WirelessPacket;
1193
// Maximum allowable payload with one ring buffer, bound by fragment size
1194
FreeFragSize = pAdapter->PortCfg.FragmentThreshold - LENGTH_CRC;
1196
// Make fragment number & more fragment bit of 802.11 header
1197
if (StartOfFrame == TRUE)
1199
Header_802_11.Frag = 0; // Start of fragment burst / Single Frame
1203
Header_802_11.Frag++; // Rest of fragmented frames.
1206
// Turn on with no frames after this one
1207
if (NumberRequired > 1)
1212
Header_802_11.Controlhead.Frame.MoreFrag = 1;
1213
MoreFragment = TRUE;
1215
if (NumberRequired == 2)
1216
NextFragSize = RemainSize - pAdapter->PortCfg.FragmentThreshold + LENGTH_802_11 + LENGTH_802_11 + LENGTH_CRC;
1218
NextFragSize = pAdapter->PortCfg.FragmentThreshold;
1220
Header_802_11.Controlhead.Duration = 3 * pAdapter->PortCfg.Dsifs
1222
+ RTUSBCalcDuration(pAdapter, pAdapter->PortCfg.TxRate, NextFragSize + EncryptionOverhead);
1226
Header_802_11.Controlhead.Frame.MoreFrag = 0;
1227
MoreFragment = FALSE;
1229
if (Header_802_11.Controlhead.Addr1.Octet[0] & 0x01)
1231
// No ACK expected for multicast frame
1232
Header_802_11.Controlhead.Duration = 0;
1236
// ACK size is 14 include CRC, and its rate is 2Mb
1237
Header_802_11.Controlhead.Duration = pAdapter->PortCfg.Dsifs + AckDuration;
1241
// Check for WEP enable bit and prepare for software WEP
1242
if ((CipherSuite == Ndis802_11Encryption1Enabled) && (EAPOLFrame == FALSE) &&
1243
(pAdapter->PortCfg.SharedKey[pAdapter->PortCfg.DefaultKeyId].KeyLen != 0))
1245
Header_802_11.Controlhead.Frame.Wep = 1;
1248
else if ((CipherSuite == Ndis802_11Encryption2Enabled) && (pWpaKey != NULL))
1250
Header_802_11.Controlhead.Frame.Wep = 1;
1253
else if ((CipherSuite == Ndis802_11Encryption3Enabled) && (pWpaKey != NULL))
1255
Header_802_11.Controlhead.Frame.Wep = 1;
1260
Header_802_11.Controlhead.Frame.Wep = 0;
1264
// Copy 802.11 header to Tx ring buffer
1265
memcpy(pDest, &Header_802_11, sizeof(Header_802_11));
1266
pDest += sizeof(Header_802_11);
1267
FreeFragSize -= sizeof(Header_802_11);
1269
if ((CipherSuite == Ndis802_11Encryption1Enabled) && (EAPOLFrame == FALSE) &&
1270
(pAdapter->PortCfg.SharedKey[pAdapter->PortCfg.DefaultKeyId].KeyLen != 0))
1272
// Prepare IV, IV offset, Key for Hardware encryption
1275
pAdapter->PortCfg.SharedKey[pAdapter->PortCfg.DefaultKeyId].Key,
1276
pAdapter->PortCfg.DefaultKeyId,
1277
pAdapter->PortCfg.SharedKey[pAdapter->PortCfg.DefaultKeyId].KeyLen,
1278
(PUCHAR) &pTxD->Iv);
1279
KeyID = pAdapter->PortCfg.DefaultKeyId;
1280
// Set Iv offset in TxD
1281
pTxD->IvOffset = LENGTH_802_11;
1283
memcpy(pDest, &pTxD->Iv, 4);
1286
else if ((CipherSuite == Ndis802_11Encryption2Enabled) && (pWpaKey != NULL))
1291
// Prepare IV, EIV, IV offset, Key for hardware encryption
1295
pAdapter->PortCfg.DefaultKeyId, // This might cause problem when using peer key
1296
Header_802_11.Controlhead.Addr2.Octet,
1304
// Increase TxTsc value for next transmission
1305
while (++pWpaKey->TxTsc[i] == 0x0)
1313
// TODO: TSC has done one full cycle, do re-keying stuff follow specs
1314
// Should send a special event microsoft defined to request re-key
1318
memcpy(&pTxD->Iv, &Iv16, 4);
1321
memcpy(&pTxD->Eiv, &Iv32, 4);
1324
pTxD->IvOffset = LENGTH_802_11;
1326
memcpy(pDest, &Iv16, 4);
1328
memcpy(pDest, &Iv32, 4);
1332
else if ((CipherSuite == Ndis802_11Encryption3Enabled) && (pWpaKey != NULL))
1338
pTmp = (PUCHAR) &Iv16;
1339
*pTmp = pWpaKey->TxTsc[0];
1340
*(pTmp + 1) = pWpaKey->TxTsc[1];
1342
*(pTmp + 3) = (pAdapter->PortCfg.DefaultKeyId << 6) | 0x20;
1344
Iv32 = *(PULONG)(&pWpaKey->TxTsc[2]);
1346
// Increase TxTsc value for next transmission
1347
while (++pWpaKey->TxTsc[i] == 0x0)
1355
// TODO: TSC has done one full cycle, do re-keying stuff follow specs
1356
// Should send a special event microsoft defined to request re-key
1360
memcpy(&pTxD->Iv, &Iv16, 4);
1363
memcpy(&pTxD->Eiv, &Iv32, 4);
1366
pTxD->IvOffset = LENGTH_802_11;
1368
memcpy(pDest, &Iv16, 4);
1370
memcpy(pDest, &Iv32, 4);
1376
// Only the first fragment required LLC-SNAP header !!!
1378
if ((StartOfFrame == TRUE) && (Encapped == TRUE))
1380
// For WEP & no encryption required frame, just copy LLC header into buffer,
1381
// Hardware will do the encryption job.
1382
// For TKIP, we have to calculate MIC and store it first
1383
if ((CipherSuite == Ndis802_11Encryption2Enabled) && (pWpaKey != NULL))
1385
// Calculate MSDU MIC Value
1386
RTMPCalculateMICValue(pAdapter, skb, pEncap, 6, pWpaKey);
1388
// For WEP & no encryption required frame, just copy LLC header into buffer,
1389
// Hardware will do the encryption job.
1390
// For TKIP, we have to calculate MIC and store it first
1393
memcpy(pDest, pEncap, 6);
1396
// Copy protocol type
1397
pSrc = (PUCHAR) pVirtualAddress;
1398
memcpy(pDest, pSrc + 12, 2);
1401
// Exclude 802.3 header size, we will recalculate the size at
1402
// the end of fragment preparation.
1403
NdisBufferLength -= LENGTH_802_3;
1404
pSrc += LENGTH_802_3;
1405
FreeFragSize -= LENGTH_802_1_H;
1407
else if ((StartOfFrame == TRUE) && (Encapped == FALSE))
1409
if ((pAdapter->PortCfg.WepStatus == Ndis802_11Encryption2Enabled) && (pWpaKey != NULL))
1411
// Calculate MSDU MIC Value
1412
RTMPCalculateMICValue(pAdapter, skb, pEncap, 0, pWpaKey);
1415
pSrc = (PUCHAR) pVirtualAddress + LENGTH_802_3;
1416
NdisBufferLength -= LENGTH_802_3;
1419
// Start copying payload
1423
if (NdisBufferLength >= FreeFragSize)
1425
// Copy only the free fragment size, and save the pointer
1426
// of current buffer descriptor for next fragment buffer.
1427
memcpy(pDest, pSrc, FreeFragSize);
1428
BytesCopied += FreeFragSize;
1429
pSrc += FreeFragSize;
1430
pDest += FreeFragSize;
1431
NdisBufferLength -= FreeFragSize;
1436
// Copy the rest of this buffer descriptor pointed data
1437
// into ring buffer.
1438
memcpy(pDest, pSrc, NdisBufferLength);
1439
BytesCopied += NdisBufferLength;
1440
pDest += NdisBufferLength;
1441
FreeFragSize -= NdisBufferLength;
1443
// No more buffer descriptor
1444
// Add MIC value if needed
1445
if ((CipherSuite == Ndis802_11Encryption2Enabled) &&
1446
(MICFrag == FALSE) &&
1451
NdisBufferLength = 8; // Set length to MIC length
1452
DBGPRINT(RT_DEBUG_INFO, "Calculated TX MIC value = ");
1453
for (i = 0; i < 8; i++)
1455
DBGPRINT_RAW(RT_DEBUG_INFO, "%02x:", pAdapter->PrivateInfo.Tx.MIC[i]);
1457
DBGPRINT_RAW(RT_DEBUG_INFO, "\n");
1459
if (FreeFragSize >= NdisBufferLength)
1461
memcpy(pDest, pAdapter->PrivateInfo.Tx.MIC, NdisBufferLength);
1462
BytesCopied += NdisBufferLength;
1463
pDest += NdisBufferLength;
1464
FreeFragSize -= NdisBufferLength;
1465
NdisBufferLength = 0;
1466
RemainSize += 8; // Need to add MIC as payload
1470
memcpy(pDest, pAdapter->PrivateInfo.Tx.MIC, FreeFragSize);
1471
BytesCopied += FreeFragSize;
1472
pSrc = pAdapter->PrivateInfo.Tx.MIC + FreeFragSize;
1473
pDest += FreeFragSize;
1474
NdisBufferLength -= FreeFragSize;
1476
RemainSize += (8 - FreeFragSize); // Need to add MIC as payload
1479
} while (FALSE); // End of copying payload
1481
// Real packet size, No 802.1H header for fragments except the first one.
1482
if ((StartOfFrame == TRUE) && (Encapped == TRUE))
1484
TxSize = BytesCopied + LENGTH_802_11 + LENGTH_802_1_H;
1488
TxSize = BytesCopied + LENGTH_802_11;
1491
RemainSize = RemainSize - BytesCopied;
1493
if ((CipherSuite == Ndis802_11Encryption1Enabled) && (Header_802_11.Controlhead.Frame.Wep == 1))
1495
// IV + ICV which ASIC added after encryption done
1497
PLCPLength = TxSize + 8;
1499
else if ((CipherSuite == Ndis802_11Encryption2Enabled) && (pWpaKey != NULL))
1501
// IV + EIV + ICV which ASIC added after encryption done
1503
PLCPLength = TxSize + 8;
1505
else if ((CipherSuite == Ndis802_11Encryption3Enabled) && (pWpaKey != NULL))
1507
// IV + EIV + HW MIC
1509
PLCPLength = TxSize + 12;
1513
PLCPLength = TxSize + 4;
1515
DBGPRINT_RAW(RT_DEBUG_INFO, "TxSize = %d, PLCPLength = %d\n", TxSize, PLCPLength);//steven:for debug
1517
// Prepare Tx descriptors before kicking tx.
1518
// The BBP register index in Tx descriptor has to be configured too.
1519
if (Header_802_11.Controlhead.Addr1.Octet[0] & 0x01)
1521
INC_COUNTER(pAdapter->WlanCounters.MulticastTransmittedFrameCount);
1522
// Multicast, retry bit is off
1523
if (StartOfFrame == TRUE)
1525
if (RTUSB_GET_PACKET_RTS(skb) != 1)
1526
RTUSBWriteTxDescriptor(pTxD, FALSE, 0, FALSE, FALSE, TRUE, FrameGap, TxSize, Cipher, KeyID, CW_MIN_IN_BITS, CW_MAX_IN_BITS, PLCPLength, pAdapter->PortCfg.TxRate, 4, pAdapter->PortCfg.TxPreambleInUsed);
1528
RTUSBWriteTxDescriptor(pTxD, FALSE, 0, FALSE, FALSE, TRUE, FrameGap, TxSize, Cipher, KeyID, 0, 0, PLCPLength, pAdapter->PortCfg.TxRate, 4, pAdapter->PortCfg.TxPreambleInUsed);
1531
RTUSBWriteTxDescriptor(pTxD, FALSE, 0, FALSE, FALSE, FALSE, FrameGap, TxSize, Cipher, KeyID, 0, 0, PLCPLength, pAdapter->PortCfg.TxRate, 4, pAdapter->PortCfg.TxPreambleInUsed);
1535
if (StartOfFrame == TRUE)
1537
if (RTUSB_GET_PACKET_RTS(skb) != 1)
1538
RTUSBWriteTxDescriptor(pTxD, MoreFragment, 7, TRUE, FALSE, TRUE, FrameGap, TxSize, Cipher, KeyID, CW_MIN_IN_BITS, CW_MAX_IN_BITS, PLCPLength, pAdapter->PortCfg.TxRate, 4, pAdapter->PortCfg.TxPreambleInUsed);
1540
RTUSBWriteTxDescriptor(pTxD, MoreFragment, 7, TRUE, FALSE, TRUE, FrameGap, TxSize, Cipher, KeyID, 0, 0, PLCPLength, pAdapter->PortCfg.TxRate, 4, pAdapter->PortCfg.TxPreambleInUsed);
1543
RTUSBWriteTxDescriptor(pTxD, MoreFragment, 7, TRUE, FALSE, FALSE, FrameGap, TxSize, Cipher, KeyID, 0, 0, PLCPLength, pAdapter->PortCfg.TxRate, 4, pAdapter->PortCfg.TxPreambleInUsed);
1546
TransferBufferLength = TxSize + sizeof(TXD_STRUC);
1547
if ((TransferBufferLength % 2) == 1)//always bulk out even number of bytes
1548
TransferBufferLength++;
1549
if ((TransferBufferLength % pAdapter->BulkOutMaxPacketSize) == 0)
1550
TransferBufferLength += 2;
1552
pTxContext->BulkOutSize = TransferBufferLength;
1553
RTUSB_SET_BULK_FLAG(pAdapter, fRTUSB_BULK_OUT_DATA_NORMAL);
1555
// Set frame gap for the rest of fragment burst.
1556
// It won't matter if there is only one fragment (single fragment frame).
1557
StartOfFrame = FALSE;
1559
if (NumberRequired == 0)
1561
pTxContext->LastOne = TRUE;
1565
pTxContext->LastOne = FALSE;
1567
//steven:use ASIC counters to derive this count instead INC_COUNTER(pAdapter->WlanCounters.TransmittedFragmentCount);
1569
// Increase BulkOut stanby count.
1571
atomic_inc(&pAdapter->TxCount);
1572
} while (NumberRequired > 0);
1575
// Add duplicate 1mb broadcast frames
1578
if ((pAdapter->PortCfg.TxRate != RATE_1) && (Bcast_8023 == TRUE) && (SingleFrag == TRUE))
1580
PTX_CONTEXT pTmpContext;
1582
ULONG DataOffset = 0;
1584
pSrc = pTxContext->TransferBuffer->WirelessPacket;
1586
// Check the offset of the original 802.3 data packet
1588
if (CipherSuite == Ndis802_11EncryptionDisabled)
1590
else if (CipherSuite == Ndis802_11Encryption1Enabled)
1591
DataOffset += 4; //Add IV
1592
else if (CipherSuite == Ndis802_11Encryption2Enabled)
1593
DataOffset += 8; //Add EIV
1594
else if (CipherSuite == Ndis802_11Encryption3Enabled)
1595
DataOffset += 8; //Add EIV
1597
// Check for DHCP & BOOTP protocol
1598
if ((*(pSrc + 0x35 + DataOffset) != 0x44) || (*(pSrc + 0x37 + DataOffset) != 0x43))
1601
// 2054 (hex 0806) for ARP datagrams
1602
// if this packet is not ARP datagrams, then do nothing
1603
// ARP datagrams will also be duplicate at 1mb broadcast frames
1605
if (Protocol != 0x0806 )
1609
// Get the Tx Ring descriptor & Dma Buffer address
1610
pTmpContext = &pAdapter->TxContext[pAdapter->NextTxIndex];
1611
pDest = pTmpContext->TransferBuffer->WirelessPacket;
1613
if (pTmpContext->InUse == TRUE)
1614
break; //No available Tx Ring for Send 1mb broadcast frames.
1616
// Increase & maintain Tx Ring Index
1617
pAdapter->NextTxIndex++;
1618
if (pAdapter->NextTxIndex >= TX_RING_SIZE)
1620
pAdapter->NextTxIndex = 0;
1624
// Reset LastOne Tx Ring descriptor
1626
pTmpContext->InUse = TRUE;
1627
pTmpContext->LastOne = TRUE;
1629
pTmpTxD = &(pTmpContext->TransferBuffer->TxDesc);
1631
// Duplicate TxD descriptor, and we will reset the its value later.
1633
memcpy(pTmpTxD, pTxD, sizeof(TXD_STRUC));
1634
// Start coping data to new ring
1635
memcpy(pDest, pSrc, pTxContext->BulkOutSize);
1636
pTmpContext->BulkOutSize = pTxContext->BulkOutSize;
1637
RTUSBWriteTxDescriptor(pTmpTxD, FALSE, 7, TRUE, FALSE, FALSE, FrameGap, TxSize, Cipher, KeyID, 0, 0, PLCPLength, RATE_1, 4, pAdapter->PortCfg.TxPreambleInUsed);
1639
// Increase BulkOut stanby count.
1641
atomic_inc(&pAdapter->TxCount);
1642
DBGPRINT(RT_DEBUG_TRACE, "Send 1M broadcast frame!\n");
1647
// Acknowledge protocol send complete of pending packet.
1648
RTUSBFreeSkbBuffer(skb);
1649
return (NDIS_STATUS_SUCCESS);
1653
VOID RTUSBRxPacket(unsigned long data)
1654
//VOID RTUSBRxPacket(purbb_t pUrb)
1656
//PRT2570ADAPTER pAdapter = (PRT2570ADAPTER)data;
1657
purbb_t pUrb = (purbb_t)data;
1658
PRT2570ADAPTER pAdapter;
1659
PRX_CONTEXT pRxContext;
1662
PHEADER_802_11 pHeader;
1664
PUCHAR pDestMac, pSrcMac;
1667
UINT PacketSize = 0;
1670
UCHAR Header802_3[14];
1671
PWPA_KEY pWpaKey = NULL;
1672
// To indicate cipher used for this packet
1673
NDIS_802_11_ENCRYPTION_STATUS Cipher;
1674
struct sk_buff *skb;
1676
wlan_ng_prism2_header *ph;
1678
pRxContext= (PRX_CONTEXT)pUrb->context;
1679
pAdapter = pRxContext->pAdapter;
1681
if( RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_REMOVE_IN_PROGRESS) )
1686
if (pRxContext->pUrb->actual_length >= (sizeof(RXD_STRUC) + LENGTH_802_11))//blue
1688
pData = pRxContext->TransferBuffer;
1689
pManage = (PVOID) pData;
1691
pRxD = (PRXD_STRUC)(pData + pRxContext->pUrb->actual_length - sizeof(RXD_STRUC));
1692
pHeader = (PHEADER_802_11)pData;
1695
if (pRxD->DataByteCnt < 4)
1696
Status = NDIS_STATUS_FAILURE;
1699
pAdapter->PortCfg.Pss = PWR_ACTIVE;
1701
// Increase Total receive byte counter after real data received no mater any error or not
1702
pAdapter->RalinkCounters.ReceivedByteCount += (pRxD->DataByteCnt - 4);
1704
// Check for all RxD errors
1705
Status = RTMPCheckRxDescriptor(pAdapter, pRxD);
1708
/* Only recieve valid packets in to monitor mode */
1709
if (pAdapter->PortCfg.BssType == BSS_MONITOR && Status == NDIS_STATUS_SUCCESS)
1711
struct sk_buff *skb;
1712
if ((skb = __dev_alloc_skb(2048, GFP_DMA|GFP_ATOMIC)) != NULL)
1714
if (pAdapter->PortCfg.MallowRFMONTx == TRUE) {
1715
if (pAdapter->PortCfg.ForcePrismHeader != 1)
1716
goto rfmontx_80211_receive;
1718
if (pAdapter->PortCfg.ForcePrismHeader == 2)
1719
goto rfmontx_80211_receive;
1722
// setup the wlan-ng prismheader
1724
if (skb_headroom(skb) < sizeof(wlan_ng_prism2_header))
1725
pskb_expand_head(skb, sizeof(wlan_ng_prism2_header), 0, GFP_ATOMIC);
1727
ph = (wlan_ng_prism2_header *)
1728
skb_push(skb, sizeof(wlan_ng_prism2_header));
1729
memset(ph, 0, sizeof(wlan_ng_prism2_header));
1731
ph->msgcode = DIDmsg_lnxind_wlansniffrm;
1732
ph->msglen = sizeof(wlan_ng_prism2_header);
1733
strcpy(ph->devname, pAdapter->net->name);
1735
ph->hosttime.did = DIDmsg_lnxind_wlansniffrm_hosttime;
1736
ph->mactime.did = DIDmsg_lnxind_wlansniffrm_mactime;
1737
ph->channel.did = DIDmsg_lnxind_wlansniffrm_channel;
1738
ph->rssi.did = DIDmsg_lnxind_wlansniffrm_rssi;
1739
ph->signal.did = DIDmsg_lnxind_wlansniffrm_signal;
1740
ph->noise.did = DIDmsg_lnxind_wlansniffrm_noise;
1741
ph->rate.did = DIDmsg_lnxind_wlansniffrm_rate;
1742
ph->istx.did = DIDmsg_lnxind_wlansniffrm_istx;
1743
ph->frmlen.did = DIDmsg_lnxind_wlansniffrm_frmlen;
1745
ph->hosttime.len = 4;
1746
ph->mactime.len = 4;
1747
ph->channel.len = 4;
1755
ph->hosttime.data = jiffies;
1756
ph->channel.data = pAdapter->PortCfg.IbssConfig.Channel;
1757
ph->signal.data = pRxD->BBR1;
1758
ph->noise.data = pAdapter->PortCfg.LastR17Value;
1759
ph->rssi.data = ph->signal.data - ph->noise.data;
1760
ph->frmlen.data = pRxD->DataByteCnt;
1762
if (pRxD->Ofdm == 1)
1764
for (i = 4; i < 12; i++)
1765
if (pRxD->BBR0 == PlcpSignal[i])
1766
ph->rate.data = _11G_RATES[i] * 2;
1769
ph->rate.data = pRxD->BBR0 / 5;
1771
// end prismheader setup
1773
rfmontx_80211_receive:
1775
skb->dev = pAdapter->net;
1776
memcpy(skb_put(skb, pRxD->DataByteCnt-4), pData, pRxD->DataByteCnt-4);
1777
skb->mac.raw = skb->data;
1778
skb->pkt_type = PACKET_OTHERHOST;
1779
skb->protocol = htons(ETH_P_802_2);
1780
skb->ip_summed = CHECKSUM_NONE;
1784
if ((!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_RESET_IN_PROGRESS)) &&
1785
(!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_BULKIN_RESET)) &&
1786
(!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_RADIO_OFF)) &&
1787
(!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_HALT_IN_PROGRESS)) &&
1788
(!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_REMOVE_IN_PROGRESS)))
1789
RTUSBBulkReceive(pAdapter);
1793
if (Status == NDIS_STATUS_SUCCESS)
1795
// Apply packet filtering rule based on microsoft requirements.
1796
Status = RTMPApplyPacketFilter(pAdapter, pRxD, pHeader);
1799
// Add receive counters
1800
if (Status == NDIS_STATUS_SUCCESS)
1802
// Increase 802.11 counters & general receive counters
1803
INC_COUNTER(pAdapter->WlanCounters.ReceivedFragmentCount);
1807
// Increase general counters
1808
pAdapter->Counters.RxErrors++;
1812
// Check for retry bit, if this bit is on, search the cache with SA & sequence
1813
// as index, if matched, discard this frame, otherwise, update cache
1814
// This check only apply to unicast data & management frames
1815
if ((pRxD->U2M) && (Status == NDIS_STATUS_SUCCESS) && (pHeader->Controlhead.Frame.Type != BTYPE_CNTL))
1817
if (pHeader->Controlhead.Frame.Retry)
1819
if (RTMPSearchTupleCache(pAdapter, pHeader) == TRUE)
1821
// Found retry frame in tuple cache, Discard this frame / fragment
1822
// Increase 802.11 counters
1823
INC_COUNTER(pAdapter->WlanCounters.FrameDuplicateCount);
1824
DBGPRINT_RAW(RT_DEBUG_INFO, "duplicate frame\n");//steven:for debug
1825
Status = NDIS_STATUS_FAILURE;
1829
RTMPUpdateTupleCache(pAdapter, pHeader);
1832
else // Update Tuple Cache
1834
RTMPUpdateTupleCache(pAdapter, pHeader);
1838
// Check and set the cipher variable
1840
Cipher = pAdapter->PortCfg.PairCipher;
1842
Cipher = pAdapter->PortCfg.GroupCipher;
1843
Cipher = pAdapter->PortCfg.WepStatus;
1846
// Do RxD release operation for all failure frames
1848
if (Status == NDIS_STATUS_SUCCESS)
1851
// Start of main loop to parse receiving frames.
1852
// The sequence will be Type first, then subtype...
1854
switch (pHeader->Controlhead.Frame.Type)
1856
// Frame with data type
1858
// pData : Pointer skip the first 24 bytes, 802.11 HEADER
1859
pData += LENGTH_802_11;
1861
PacketSize = pRxD->DataByteCnt - LENGTH_802_11 - 4; //Minus FCS[4]. default for NoneWep.
1862
// Drop not my BSS frame
1863
if (INFRA_ON(pAdapter))
1865
// Infrastructure mode, check address 2 for BSSID
1866
if (memcmp(&pHeader->Controlhead.Addr2, &pAdapter->PortCfg.Bssid, 6) != 0)
1867
break; // Receive frame not my BSSID
1869
atomic_inc(&(pAdapter->PortCfg.DataPacketsFromAP));
1871
else // Ad-Hoc mode or Not associated
1873
// Ad-Hoc mode, check address 3 for BSSID
1874
if (memcmp(&pHeader->Addr3, &pAdapter->PortCfg.Bssid, 6) != 0)
1875
break; // Receive frame not my BSSID
1877
// Drop frame from AP while we are in Ad-hoc mode or not associated
1878
if (pHeader->Controlhead.Frame.FrDs)
1882
// Drop Null data frame, or CF with NULL data frame
1883
if ((pHeader->Controlhead.Frame.Subtype == SUBTYPE_NULL_FUNC) ||
1884
(pHeader->Controlhead.Frame.Subtype == SUBTYPE_CFACK) ||
1885
(pHeader->Controlhead.Frame.Subtype == SUBTYPE_CFPOLL) ||
1886
(pHeader->Controlhead.Frame.Subtype == SUBTYPE_CFACK_CFPOLL))
1891
// Process Broadcast & Multicast data frame
1892
if (pRxD->Bcast || pRxD->Mcast)
1894
// Multicast 802.11 Counter
1895
INC_COUNTER(pAdapter->WlanCounters.MulticastReceivedFrameCount);
1896
DBGPRINT(RT_DEBUG_INFO,"Receiving multicast frame\n");
1897
// Drop Mcast / Bcast frame with fragment bit on
1898
if (pHeader->Controlhead.Frame.MoreFrag)
1900
DBGPRINT_RAW(RT_DEBUG_ERROR,"Receiving multicast frame with fragment bit on\n");
1904
// Filter out Bcast frame which AP relayed for us
1905
if (((memcmp(&pHeader->Addr3, pAdapter->CurrentAddress, 6) == 0)) && pHeader->Controlhead.Frame.FrDs)
1908
// WEP encrypted frame
1909
if (pHeader->Controlhead.Frame.Wep)
1911
// Check our WEP setting, if no WEP turning on, just drop this frame
1912
if (Cipher == Ndis802_11Encryption1Enabled) // WEP
1918
pData = pData + 4; //Offset skip IV[4]
1919
pRxD->DataByteCnt = pRxD->DataByteCnt - 8; //Minus ICV[4] & FCS[4].
1922
PacketSize = pRxD->DataByteCnt - LENGTH_802_11 - 4; //Minus IV[4].
1924
else if (Cipher == Ndis802_11Encryption2Enabled) // TKIP
1928
DBGPRINT_RAW(RT_DEBUG_ERROR,"pRxD->CiErr\n");
1933
pData = pData + 8; //Offset skip IV[8]
1935
// the MIC is stored on the last one no more Fragment.
1936
// that is only last MPDU only need to check MIC.
1938
if (pHeader->Controlhead.Frame.MoreFrag == TRUE)
1941
pRxD->DataByteCnt = pRxD->DataByteCnt - 8; //ICV[4] &FCS[4].
1945
if (pHeader->Frag != 0)
1946
pRxD->DataByteCnt = pRxD->DataByteCnt - 8; //MIC been frag ICV[4] & FCS[4]
1948
pRxD->DataByteCnt = pRxD->DataByteCnt - 16; //Minus MIC[8] & ICV[4] &FCS[4].
1952
PacketSize = pRxD->DataByteCnt - LENGTH_802_11 - 8; //Minus IV+EIV[8].
1954
else if (Cipher == Ndis802_11Encryption3Enabled) // AES
1960
pData = pData + 8; //Offset skip RSN[8]
1961
pRxD->DataByteCnt = pRxD->DataByteCnt - 12; //Minus MIC[8] & ICV[4]
1964
PacketSize = pRxD->DataByteCnt - LENGTH_802_11 - 8; //Minus RSN[8]
1969
}//if (pRxD->Bcast || pRxD->Mcast)
1970
// Begin process unicast to me frame
1974
// Begin frame processing
1976
// DA is always address 1
1977
pDestMac = (PUCHAR) &(pHeader->Controlhead.Addr1);
1978
// Seclect SA by different mode
1979
if (INFRA_ON(pAdapter)) // For infrastructure, SA is address 3
1981
pSrcMac = (PUCHAR) &(pHeader->Addr3);
1983
else // For IBSS mode, SA is address 2
1985
pSrcMac = (PUCHAR) &(pHeader->Controlhead.Addr2);
1987
// WEP encrypted frame
1988
if (Cipher == Ndis802_11Encryption1Enabled) // WEP
1990
if (pHeader->Controlhead.Frame.Wep)
1996
pData = pData + 4; //Offset skip IV[4]
1997
pRxD->DataByteCnt = pRxD->DataByteCnt - 8; //Minus ICV[4] & FCS[4].
2000
PacketSize = pRxD->DataByteCnt - LENGTH_802_11 - 4; //Minus IV[4].
2002
else if ((pAdapter->PortCfg.PrivacyFilter == Ndis802_11PrivFilter8021xWEP) &&
2003
(pHeader->Frag == 0))
2005
// Check 802.1x frame, if not drop it.
2006
if (memcmp(EAPOL, pData + 6, 2))
2008
// Not 802.1X frames
2009
// Add error counter
2014
else if (Cipher == Ndis802_11Encryption2Enabled) // TKIP
2016
if (pHeader->Controlhead.Frame.Wep)
2020
DBGPRINT(RT_DEBUG_TEMP,"pRxD->CiErr\n");
2027
pData = pData + 8; //Offset skip IV[8]
2029
// the MIC is stored on the last one no more Fragment.
2030
// that is only last MPDU only need to check MIC.
2032
if (pHeader->Controlhead.Frame.MoreFrag == TRUE)
2035
pRxD->DataByteCnt = pRxD->DataByteCnt - 8; //ICV[4] &FCS[4].
2039
if (pHeader->Frag != 0)
2040
pRxD->DataByteCnt = pRxD->DataByteCnt - 8; //MIC been frag ICV[4] & FCS[4]
2042
pRxD->DataByteCnt = pRxD->DataByteCnt - 16; //Minus MIC[8] & ICV[4] & FCS[4].
2046
PacketSize = pRxD->DataByteCnt - LENGTH_802_11 - 8; //Minus IV+EIV[8].
2048
else if ((pAdapter->PortCfg.PrivacyFilter == Ndis802_11PrivFilter8021xWEP) &&
2049
(pHeader->Frag == 0))
2051
// Check 802.1x frame, if not drop it.
2052
if (memcmp(EAPOL, pData + 6, 2) != 0)
2054
DBGPRINT(RT_DEBUG_TEMP,"Not 802.1X frames\n");
2055
// Not 802.1X frames
2056
// Add error counter
2059
DBGPRINT(RT_DEBUG_TEMP," 802.1X EAPOL frames\n");
2062
else if (Cipher == Ndis802_11Encryption3Enabled) // AES
2064
if (pHeader->Controlhead.Frame.Wep)
2070
pData = pData + 8; //Offset skip IV[8]
2071
pRxD->DataByteCnt = pRxD->DataByteCnt - 12; //Minus MIC[8] & ICV[4]
2074
PacketSize = pRxD->DataByteCnt - LENGTH_802_11 - 8; //Minus RSN[8]
2076
else if ((pAdapter->PortCfg.PrivacyFilter == Ndis802_11PrivFilter8021xWEP) &&
2077
(pHeader->Frag == 0))
2079
// Check 802.1x frame, if not drop it.
2080
if (memcmp(EAPOL, pData + 6, 2) != 0)
2082
// Not 802.1X frames
2083
// Add error counter
2088
else if (pHeader->Controlhead.Frame.Wep)
2090
// Drop WEP frame when PrivacyInvoked is FALSE
2093
}//else if (pRxD->U2M)
2095
// The total available payload should exclude 24-byte 802.11 Header
2096
//packetSize = pRxD->DataByteCnt - LENGTH_802_11 - 4;
2098
// Find the WPA key, either Group or Pairwise Key
2099
// Although the data has been decrypted by ASIC,
2100
// driver has to calculate the RxMIC which required the key.
2101
// The failed case should not happen. If it did, drop it.
2102
if ((pAdapter->PortCfg.CipherAlg == CIPHER_TKIP) && (pHeader->Controlhead.Frame.Wep))
2106
pWpaKey = (PWPA_KEY) NULL;
2107
// First lookup the DA, if it's a group address, use GROUP key
2108
if (pRxD->Bcast || pRxD->Mcast)
2111
idx = (pRxD->Iv & 0xc0000000) >> 30;
2113
idx = (pRxD->Iv & 0x000000c0) >> 6;
2115
if ((pAdapter->PortCfg.GroupKey[idx].KeyLen != 0) &&
2116
((INFRA_ON(pAdapter) && ((memcmp(&pHeader->Controlhead.Addr2, &pAdapter->PortCfg.Bssid, 6) == 0))) ||
2117
(ADHOC_ON(pAdapter) && ((memcmp(&pHeader->Addr3, &pAdapter->PortCfg.Bssid, 6) == 0)))))
2119
pWpaKey = (PWPA_KEY) &pAdapter->PortCfg.GroupKey[idx];
2120
pWpaKey->Type = GROUP_KEY;
2121
DBGPRINT(RT_DEBUG_INFO, "Rx Use Group Key %d\n", idx);
2124
// Try to find the Pairwise Key
2127
for (idx = 0; idx < PAIRWISE_KEY_NO; idx++)
2129
if (((memcmp(&pHeader->Controlhead.Addr2, pAdapter->PortCfg.PairwiseKey[idx].BssId, 6) == 0)) &&
2130
(pAdapter->PortCfg.PairwiseKey[idx].KeyLen != 0))
2132
pWpaKey = (PWPA_KEY) &pAdapter->PortCfg.PairwiseKey[idx];
2133
pWpaKey->Type = PAIRWISE_KEY;
2134
DBGPRINT(RT_DEBUG_LOUD, "Rx Use Pairwise Key %d\n",idx);
2138
// Use default Group Key if there is no Pairwise key present
2139
if ((pWpaKey == NULL) && (pAdapter->PortCfg.GroupKey[pAdapter->PortCfg.DefaultKeyId].KeyLen != 0))
2141
pWpaKey = (PWPA_KEY) &pAdapter->PortCfg.GroupKey[pAdapter->PortCfg.DefaultKeyId];
2142
pWpaKey->Type = GROUP_KEY;
2143
DBGPRINT(RT_DEBUG_INFO, "Rx Use Group Key\n");
2147
if (pWpaKey == NULL)
2151
// DA is always address 1
2152
pDestMac = (PUCHAR) &(pHeader->Controlhead.Addr1);
2153
// Seclect SA by different mode
2154
if (INFRA_ON(pAdapter))
2156
// For infrastructure, SA is address 3
2157
pSrcMac = (PUCHAR) &(pHeader->Addr3);
2161
// For IBSS mode, SA is address 2
2162
pSrcMac = (PUCHAR) &(pHeader->Controlhead.Addr2);
2165
// Process Broadcast & Multicast data frame
2166
if (pRxD->Bcast || pRxD->Mcast)
2168
// Save encapaturation starting pointer
2171
// For TKIP frame, calculate the MIC value
2172
if ((pAdapter->PortCfg.CipherAlg == CIPHER_TKIP) && (pHeader->Controlhead.Frame.Wep))
2176
if (RTMPTkipCompareMICValue(pAdapter,
2181
PacketSize) == FALSE)
2183
DBGPRINT_RAW(RT_DEBUG_ERROR,"BroadCast/Multicast Rx MIC Value error\n");
2184
RTMPReportMicError(pAdapter, pWpaKey);
2185
Status = NDIS_STATUS_FAILURE;
2189
// Second, increase RxTsc value for next transmission
2190
while (++pWpaKey->RxTsc[i] == 0x0)
2196
// Rx TSC has done one full cycle, since re-key is done by transmitter
2197
// We did not do anything for Rx path
2199
// For WPA2 mixer mode PairCipher = AES, GroupCipher = TKIP
2200
else if ((pAdapter->PortCfg.PairCipher == Ndis802_11Encryption3Enabled) &&
2201
(pAdapter->PortCfg.GroupCipher == Ndis802_11Encryption2Enabled) &&
2202
(pHeader->Controlhead.Frame.Wep))
2204
//Use Software to decript TKIP packet.
2205
if (RTMPSoftDecryptTKIP(pAdapter, pRxContext->TransferBuffer, pRxD->DataByteCnt + 12, pAdapter->PortCfg.GroupKey))
2207
DBGPRINT(RT_DEBUG_INFO, "WPA2::RTMPSoftDecryptTKIP Complete\n");
2208
pData = pRxContext->TransferBuffer + LENGTH_802_11;
2209
PacketSize = pRxD->DataByteCnt - 8 - LENGTH_802_11; //8 bytes MIC, 4 bytes ICV
2214
// Check for encapsulataion other than RFC1042 & Bridge tunnel
2215
if ((memcmp(SNAP_802_1H, pEncap, 6) != 0) && (memcmp(SNAP_BRIDGE_TUNNEL, pEncap, 6) != 0))
2217
LLC_Len[0] = PacketSize / 256;
2218
LLC_Len[1] = PacketSize % 256;
2219
MAKE_802_3_HEADER(Header802_3, pDestMac, pSrcMac, ((PUCHAR) LLC_Len));
2223
// Remove 802.11 H header & reconstruct 802.3 header
2224
pData += (LENGTH_802_1_H - LENGTH_802_3_TYPE);
2225
// Patch for WHQl only, which did not turn on Netbios but use IPX within its payload
2226
if (((memcmp(IPX, pData, 2) == 0) || (memcmp(APPLE_TALK, pData, 2) == 0)) && (memcmp(SNAP_802_1H, pEncap, 6) == 0))
2228
LLC_Len[0] = PacketSize / 256;
2229
LLC_Len[1] = PacketSize % 256;
2230
pData = pData - LENGTH_802_1_H;
2231
MAKE_802_3_HEADER(Header802_3, pDestMac, pSrcMac, ((PUCHAR) LLC_Len));
2235
MAKE_802_3_HEADER(Header802_3, pDestMac, pSrcMac, pData);
2236
// The total available payload should exclude 24-byte 802.11 Header
2237
// and 8-byte 802.2 LLC
2238
PacketSize -= LENGTH_802_1_H;
2241
// Point to read 802.3 payload
2242
pData += LENGTH_802_3_TYPE;
2245
// For miniportTransferData
2246
pAdapter->pRxData = pData;
2248
pAdapter->PortCfg.LedCntl.fRxActivity = TRUE; // for RX ACTIVITY LED
2250
// Acknolwdge upper layer the received frame
2251
// Copy header to head of data for compatibility with older protocol
2253
//memcpy(pData - LENGTH_802_3, Header802_3, LENGTH_802_3);
2254
// Acknowledge upper layer the received frame
2255
if ((skb = dev_alloc_skb(PacketSize + LENGTH_802_3 + 2)) != NULL)
2257
skb->dev = pAdapter->net;
2258
skb_reserve(skb, 2); // 16 byte align the IP header
2259
memcpy(skb_put(skb, LENGTH_802_3), Header802_3, LENGTH_802_3);
2260
memcpy(skb_put(skb, PacketSize), pData, PacketSize);
2261
skb->protocol = eth_type_trans(skb, pAdapter->net);
2263
pAdapter->net->last_rx = jiffies;
2264
pAdapter->netstats.rx_packets++;
2266
//memset(Header802_3, 0, LENGTH_802_3);
2267
DBGPRINT_RAW(RT_DEBUG_INFO, "!!! Broadcast Ethernet rx Indicated !!!\n");
2268
} //if (pRxD->Bcast || pRxD->Mcast)
2269
// Begin process unicast to me frame
2272
// Update Rx data rate first.
2273
if (pRxD->Ofdm == 1)
2275
for (i = 4; i < 12; i++)
2277
if (pRxD->BBR0 == PlcpSignal[i])
2281
pAdapter->LastRxRate = i;
2283
else // receive CCK encoding
2285
if (pRxD->BBR0 == 10)
2286
pAdapter->LastRxRate = 0;
2287
else if (pRxD->BBR0 == 20)
2288
pAdapter->LastRxRate = 1;
2289
else if (pRxD->BBR0 == 55)
2290
pAdapter->LastRxRate = 2;
2291
else if (pRxD->BBR0 == 110)
2292
pAdapter->LastRxRate = 3;
2295
// Send PS-Poll for AP to send next data frame
2296
if ((pHeader->Controlhead.Frame.MoreData) && INFRA_ON(pAdapter) && (pAdapter->PortCfg.Psm == PWR_SAVE))
2298
//Send PS-Poll frame
2299
EnqueuePsPoll(pAdapter);
2300
DBGPRINT(RT_DEBUG_TRACE, "Sending PS-POLL\n");
2304
// Begin frame processing
2306
if (pHeader->Frag == 0) // First or Only fragment
2308
// For TKIP frame, calculate the MIC value
2309
if (pHeader->Controlhead.Frame.MoreFrag == FALSE)
2311
if ((pAdapter->PortCfg.CipherAlg == CIPHER_TKIP) && (pHeader->Controlhead.Frame.Wep))
2314
// Use Software to descrypt if transmition keyID not 0 on ADHOC mode.
2315
// Since ASIC allows hardware descrypt only KeyID=0 as their pairwisekey.
2317
// Check U2M and KeyID not pairwise key, used Software decypt
2319
KeyIdx= *((PUCHAR)(pRxContext->TransferBuffer + LENGTH_802_11 + 3));
2320
KeyIdx = KeyIdx >> 6;
2324
//Use Software to decript TKIP packet.
2325
if (RTMPSoftDecryptTKIP(pAdapter, pRxContext->TransferBuffer, pRxD->DataByteCnt + 12, pAdapter->PortCfg.GroupKey))
2327
DBGPRINT(RT_DEBUG_TEMP, "U2M Use Groupkey RTMPSoftDecryptTKIP Complete\n");
2328
pData = pRxContext->TransferBuffer + LENGTH_802_11;
2329
PacketSize = pRxD->DataByteCnt - 8 - LENGTH_802_11; //8 bytes MIC, 4 bytes ICV
2333
DBGPRINT(RT_DEBUG_TEMP, "RTMPSoftDecryptTKIP failed\n");
2339
if (RTMPTkipCompareMICValue(pAdapter,
2343
pWpaKey->RxMic,//steven:where is this from in RT2570
2344
PacketSize) == FALSE)
2346
DBGPRINT_RAW(RT_DEBUG_ERROR,"U2M Rx MIC Value error1\n");
2347
RTMPReportMicError(pAdapter, pWpaKey);
2348
Status = NDIS_STATUS_FAILURE;
2354
// Getting RxTSC from Rx descriptor
2358
// Save encapaturation starting pointer
2360
pAdapter->FragFrame.Flags &= 0xFFFFFFFE;
2362
// Check for encapsulataion other than RFC1042 & Bridge tunnel
2363
if ((memcmp(SNAP_802_1H, pEncap, 6) != 0) && (memcmp(SNAP_BRIDGE_TUNNEL, pEncap, 6) != 0))
2365
LLC_Len[0] = PacketSize / 256;
2366
LLC_Len[1] = PacketSize % 256;
2367
MAKE_802_3_HEADER(Header802_3, pDestMac, pSrcMac, ((PUCHAR) LLC_Len));
2371
// Remove 802.11 H header & reconstruct 802.3 header
2372
pData += (LENGTH_802_1_H - LENGTH_802_3_TYPE);
2373
if ((memcmp(EAPOL, pData, 2) == 0))
2375
PacketSize += LENGTH_802_11;
2376
DBGPRINT_RAW(RT_DEBUG_TEMP, "indicated packet EAPOL PacketSize%d\n", PacketSize);//steven:for debug
2377
// Enqueue this frame to MLME engine
2380
&pAdapter->Mlme.Queue,
2382
PacketSize + LENGTH_802_1_H,
2386
// Patch for WHQl only, which did not turn on Netbios but use IPX within its payload
2387
if ((((memcmp(IPX, pData, 2) == 0) || (memcmp(APPLE_TALK, pData, 2) == 0)) && memcmp(SNAP_802_1H, pEncap, 6) == 0))
2389
LLC_Len[0] = PacketSize / 256;
2390
LLC_Len[1] = PacketSize % 256;
2391
pData = pData - LENGTH_802_1_H;
2392
MAKE_802_3_HEADER(Header802_3, pDestMac, pSrcMac, ((PUCHAR) LLC_Len));
2397
MAKE_802_3_HEADER(Header802_3, pDestMac, pSrcMac, pData);
2398
// The total available payload should exclude 24-byte 802.11 Header
2399
// and 8-byte 802.2 LLC
2400
PacketSize -= LENGTH_802_1_H;
2401
memcpy(pAdapter->FragFrame.Header_LLC, pEncap, 8);
2402
pAdapter->FragFrame.Flags |= 0x01;
2405
// Point to read 802.3 payload
2406
pData += LENGTH_802_3_TYPE;
2409
// One & The only fragment
2410
if (pHeader->Controlhead.Frame.MoreFrag == FALSE)
2412
// For miniportTransferData
2413
pAdapter->pRxData = pData;
2415
pAdapter->PortCfg.LedCntl.fRxActivity = TRUE; // for RX ACTIVITY LED
2417
DBGPRINT_RAW(RT_DEBUG_INFO, "indicated packet size = %d\n", PacketSize);//steven:for debug
2418
// Acknolwdge upper layer the received frame
2419
//memcpy((PUCHAR) pData - LENGTH_802_3, Header802_3, LENGTH_802_3);
2421
if ((skb = dev_alloc_skb(PacketSize + LENGTH_802_3 + 2)) != NULL)
2423
skb->dev = pAdapter->net;
2424
skb_reserve(skb, 2); // 16 byte align the IP header
2425
memcpy(skb_put(skb, LENGTH_802_3), Header802_3, LENGTH_802_3);
2426
memcpy(skb_put(skb, PacketSize), pData, PacketSize);
2427
skb->protocol = eth_type_trans(skb, pAdapter->net);
2429
pAdapter->net->last_rx = jiffies;
2430
pAdapter->netstats.rx_packets++;
2432
// Increase general counters
2433
pAdapter->Counters.GoodReceives++;
2435
DBGPRINT_RAW(RT_DEBUG_INFO, "!!! Frame without Fragment Indicated !!!\n");
2437
// First fragment of fragmented frames
2440
memcpy(pAdapter->FragFrame.Buffer, pData, PacketSize);
2441
memcpy(pAdapter->FragFrame.Header802_3, Header802_3, LENGTH_802_3);
2442
pAdapter->FragFrame.RxSize = PacketSize;
2443
pAdapter->FragFrame.Sequence = pHeader->Sequence;
2444
pAdapter->FragFrame.LastFrag = pHeader->Frag; // Should be 0
2447
// Middle & End of fragment burst fragments
2450
// No LLC-SNAP header in except the first fragment frame
2452
if ((pHeader->Sequence != pAdapter->FragFrame.Sequence) ||
2453
(pHeader->Frag != (pAdapter->FragFrame.LastFrag + 1)))
2455
// Fragment is not the same sequence or out of fragment number order
2456
// Clear Fragment frame contents
2457
memset(&pAdapter->FragFrame, 0, sizeof(FRAGMENT_FRAME));
2458
Status = NDIS_STATUS_FAILURE;
2461
else if ((pAdapter->FragFrame.RxSize + PacketSize) > MAX_FRAME_SIZE)
2463
// Fragment frame is too large, it exeeds the maximum frame size.
2464
// We have to drop it.
2465
// Clear Fragment frame contents
2466
memset(&pAdapter->FragFrame, 0, sizeof(FRAGMENT_FRAME));
2467
Status = NDIS_STATUS_FAILURE;
2471
memcpy(&pAdapter->FragFrame.Buffer[pAdapter->FragFrame.RxSize], pData, PacketSize);
2472
pAdapter->FragFrame.RxSize += PacketSize;
2473
pData += PacketSize;
2474
pAdapter->FragFrame.LastFrag = pHeader->Frag; // Update fragment number
2477
if (pHeader->Controlhead.Frame.MoreFrag == FALSE)
2479
// For miniportTransferData
2480
pAdapter->pRxData = pAdapter->FragFrame.Buffer;
2482
pAdapter->PortCfg.LedCntl.fRxActivity = TRUE; // for RX ACTIVITY LED
2484
// For TKIP frame, calculate the MIC value
2485
if ((pAdapter->PortCfg.CipherAlg == CIPHER_TKIP) && (pHeader->Controlhead.Frame.Wep))
2487
if (pWpaKey == NULL)
2489
DBGPRINT_RAW(RT_DEBUG_ERROR,"No matched TKIP in decryption done calculate MIC routine!!!\n");
2490
Status = NDIS_STATUS_FAILURE;
2495
// For the last fragment, we also need to copy the MIC
2496
// to the end of pAdapter->FragFrame.Buffer
2497
// for RTMPTkipCompareMICValueWithLLC used.
2499
pAdapter->FragFrame.RxSize -= 8; //We need to Minus MIC[8] on Fragment case.
2501
if (pAdapter->FragFrame.Flags & 0x00000001)
2503
if (RTMPTkipCompareMICValueWithLLC(pAdapter,
2504
pAdapter->FragFrame.Header_LLC,
2505
pAdapter->FragFrame.Buffer,
2509
pAdapter->FragFrame.RxSize) == FALSE)
2511
DBGPRINT_RAW(RT_DEBUG_ERROR,"Rx MIC Value error 2\n");
2512
RTMPReportMicError(pAdapter, pWpaKey);
2513
Status = NDIS_STATUS_FAILURE;
2519
if (RTMPTkipCompareMICValue(pAdapter,
2520
pAdapter->FragFrame.Buffer,
2524
pAdapter->FragFrame.RxSize) == FALSE)
2526
DBGPRINT_RAW(RT_DEBUG_ERROR,"Rx MIC Value error 2\n");
2527
RTMPReportMicError(pAdapter, pWpaKey);
2528
Status = NDIS_STATUS_FAILURE;
2534
// Acknolwdge upper layer the received frame
2535
if ((skb = dev_alloc_skb(pAdapter->FragFrame.RxSize + LENGTH_802_3 + 2)) != NULL)
2538
skb->dev = pAdapter->net;
2539
skb_reserve(skb, 2); /* 16 byte align the IP header */
2540
memcpy(skb_put(skb, LENGTH_802_3), (PVOID) pAdapter->FragFrame.Header802_3, LENGTH_802_3);
2541
memcpy(skb_put(skb, pAdapter->FragFrame.RxSize), (PVOID) &pAdapter->FragFrame.Buffer[0], pAdapter->FragFrame.RxSize);
2542
skb->protocol = eth_type_trans(skb, pAdapter->net);
2544
pAdapter->net->last_rx = jiffies;
2545
pAdapter->netstats.rx_packets++;
2547
// Increase general counters
2548
pAdapter->Counters.GoodReceives++;
2550
// Clear Fragment frame contents
2551
//memset(&pAdapter->FragFrame, 0, sizeof(FRAGMENT_FRAME));
2552
DBGPRINT_RAW(RT_DEBUG_INFO, "!!! Frame with Fragment Indicated !!!\n");
2553
} //Last fragment //if (pHeader->Controlhead.Frame.MoreFrag == FALSE)
2554
} //Middle & End of fragment burst fragments
2555
}//else if (pRxD->U2M)
2559
// Enqueue this frame to MLME engine
2560
MlmeEnqueueForRecv(pAdapter,
2561
&pAdapter->Mlme.Queue,
2563
pRxD->DataByteCnt - 4,
2573
}//switch (pHeader->Controlhead.Frame.Type)
2574
pAdapter->RalinkCounters.RxCount ++;
2577
else if (Status == NDIS_STATUS_RESET)
2579
RTUSBEnqueueInternalCmd(pAdapter, RT_OID_USB_RESET_BULK_IN);
2584
pRxContext->InUse = FALSE;
2586
if ((!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_RESET_IN_PROGRESS)) &&
2587
(!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_BULKIN_RESET)) &&
2588
(!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_RADIO_OFF)) &&
2589
(!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_HALT_IN_PROGRESS)) &&
2590
(!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_REMOVE_IN_PROGRESS)))
2591
RTUSBBulkReceive(pAdapter);
2597
========================================================================
2599
Routine Description:
2609
========================================================================
2611
VOID RTUSBDequeueMLMEPacket(
2612
IN PRT2570ADAPTER pAdapter)
2616
DBGPRINT(RT_DEBUG_INFO, "RTUSBDequeueMLMEPacket\n");
2617
NdisAcquireSpinLock(&pAdapter->MLMEWaitQueueLock);
2618
while ((pAdapter->PopMgmtIndex != pAdapter->PushMgmtIndex) || (atomic_read(&pAdapter->MgmtQueueSize) > 0))
2620
pMgmt = &pAdapter->MgmtRing[pAdapter->PopMgmtIndex];
2622
if (RTUSBFreeDescriptorRequest(pAdapter, PRIO_RING, 1) == NDIS_STATUS_SUCCESS)
2624
atomic_dec(&pAdapter->MgmtQueueSize);
2625
pAdapter->PopMgmtIndex = (pAdapter->PopMgmtIndex + 1) % MGMT_RING_SIZE;
2626
NdisReleaseSpinLock(&pAdapter->MLMEWaitQueueLock);
2628
RTUSBMlmeHardTransmit(pAdapter, pMgmt);
2630
MlmeFreeMemory(pAdapter, pMgmt->pBuffer);
2631
pMgmt->pBuffer = NULL;
2632
pMgmt->Valid = FALSE;
2634
NdisAcquireSpinLock(&pAdapter->MLMEWaitQueueLock);
2638
DBGPRINT(RT_DEBUG_TRACE, "RTUSBDequeueMLMEPacket::PrioRingFirstIndex = %d, PrioRingTxCnt = %d, PopMgmtIndex = %d, PushMgmtIndex = %d, NextMLMEIndex = %d\n",
2639
pAdapter->PrioRingFirstIndex, pAdapter->PrioRingTxCnt,
2640
pAdapter->PopMgmtIndex, pAdapter->PushMgmtIndex, pAdapter->NextMLMEIndex);
2644
NdisReleaseSpinLock(&pAdapter->MLMEWaitQueueLock);
2648
========================================================================
2650
Routine Description:
2660
========================================================================
2662
VOID RTUSBCleanUpMLMEWaitQueue(
2663
IN PRT2570ADAPTER pAdapter)
2667
DBGPRINT(RT_DEBUG_TRACE, "--->CleanUpMLMEWaitQueue\n");
2669
NdisAcquireSpinLock(&pAdapter->MLMEWaitQueueLock);
2670
while (pAdapter->PopMgmtIndex != pAdapter->PushMgmtIndex)
2672
pMgmt = (PMGMT_STRUC)&pAdapter->MgmtRing[pAdapter->PopMgmtIndex];
2673
MlmeFreeMemory(pAdapter, pMgmt->pBuffer);
2674
pMgmt->pBuffer = NULL;
2675
pMgmt->Valid = FALSE;
2676
atomic_dec(&pAdapter->MgmtQueueSize);
2678
pAdapter->PopMgmtIndex++;
2679
if (pAdapter->PopMgmtIndex >= MGMT_RING_SIZE)
2681
pAdapter->PopMgmtIndex = 0;
2684
NdisReleaseSpinLock(&pAdapter->MLMEWaitQueueLock);
2686
DBGPRINT(RT_DEBUG_TRACE, "<---CleanUpMLMEWaitQueue\n");
2691
========================================================================
2693
Routine Description:
2694
API for MLME to transmit management frame to AP (BSS Mode)
2695
or station (IBSS Mode)
2698
pAdapter Pointer to our adapter
2699
Buffer Pointer to memory of outgoing frame
2700
Length Size of outgoing management frame
2709
========================================================================
2711
VOID MiniportMMRequest(
2712
IN PRT2570ADAPTER pAdapter,
2721
// Check management ring free avaliability
2722
NdisAcquireSpinLock(&pAdapter->MLMEWaitQueueLock);
2723
pMgmt = (PMGMT_STRUC)&pAdapter->MgmtRing[pAdapter->PushMgmtIndex];
2724
// This management cell has been occupied
2725
if (pMgmt->Valid == TRUE)
2727
NdisReleaseSpinLock(&pAdapter->MLMEWaitQueueLock);
2728
MlmeFreeMemory(pAdapter, pBuffer);
2729
pAdapter->RalinkCounters.MgmtRingFullCount++;
2730
DBGPRINT_RAW(RT_DEBUG_WARN, "MiniportMMRequest (error:: MgmtRing full)\n");
2732
// Insert this request into software managemnet ring
2735
pMgmt->pBuffer = pBuffer;
2736
pMgmt->Length = Length;
2737
pMgmt->Valid = TRUE;
2738
pAdapter->PushMgmtIndex++;
2739
atomic_inc(&pAdapter->MgmtQueueSize);
2740
if (pAdapter->PushMgmtIndex >= MGMT_RING_SIZE)
2742
pAdapter->PushMgmtIndex = 0;
2744
NdisReleaseSpinLock(&pAdapter->MLMEWaitQueueLock);
2748
DBGPRINT(RT_DEBUG_WARN, "MiniportMMRequest (error:: NULL msg)\n");
2750
RTUSBDequeueMLMEPacket(pAdapter);
2752
// If pAdapter->PrioRingTxCnt is larger than 0, this means that prio_ring have something to transmit.
2753
// Then call KickBulkOut to transmit it
2754
if (pAdapter->PrioRingTxCnt > 0)
2756
DBGPRINT(RT_DEBUG_INFO, "MiniportMMRequest::PrioRingFirstIndex = %d, PrioRingTxCnt = %d, PopMgmtIndex = %d, PushMgmtIndex = %d, NextMLMEIndex = %d\n",
2757
pAdapter->PrioRingFirstIndex, pAdapter->PrioRingTxCnt,
2758
pAdapter->PopMgmtIndex, pAdapter->PushMgmtIndex, pAdapter->NextMLMEIndex);
2760
AsicForceWakeup(pAdapter);
2761
RTUSBKickBulkOut(pAdapter);
2767
========================================================================
2769
Routine Description:
2770
Search tuple cache for receive duplicate frame from unicast frames.
2773
pAdapter Pointer to our adapter
2774
pHeader 802.11 header of receiving frame
2777
TRUE found matched tuple cache
2778
FALSE no matched found
2782
========================================================================
2784
BOOLEAN RTMPSearchTupleCache(
2785
IN PRT2570ADAPTER pAdapter,
2786
IN PHEADER_802_11 pHeader)
2790
for (Index = 0; Index < MAX_CLIENT; Index++)
2792
if (pAdapter->TupleCache[Index].Valid == FALSE)
2795
if ((memcmp(&pAdapter->TupleCache[Index].MAC, &pHeader->Controlhead.Addr2, 6)== 0) &&
2796
(pAdapter->TupleCache[Index].Sequence == pHeader->Sequence) &&
2797
(pAdapter->TupleCache[Index].Frag == pHeader->Frag))
2806
========================================================================
2808
Routine Description:
2809
Update tuple cache for new received unicast frames.
2812
pAdapter Pointer to our adapter
2813
pHeader 802.11 header of receiving frame
2820
========================================================================
2822
VOID RTMPUpdateTupleCache(
2823
IN PRT2570ADAPTER pAdapter,
2824
IN PHEADER_802_11 pHeader)
2828
for (Index = 0; Index < MAX_CLIENT; Index++)
2830
if (pAdapter->TupleCache[Index].Valid == FALSE)
2833
memcpy(&pAdapter->TupleCache[Index].MAC, &pHeader->Controlhead.Addr2, 6);
2834
pAdapter->TupleCache[Index].Sequence = pHeader->Sequence;
2835
pAdapter->TupleCache[Index].Frag = pHeader->Frag;
2836
pAdapter->TupleCache[Index].Valid = TRUE;
2837
pAdapter->TupleCacheLastUpdateIndex = Index;
2838
DBGPRINT(RT_DEBUG_INFO,"DUPCHECK - Add Entry %d, MAC=%02x:%02x:%02x:%02x:%02x:%02x\n",
2839
Index, pAdapter->TupleCache[Index].MAC.Octet[0], pAdapter->TupleCache[Index].MAC.Octet[1],
2840
pAdapter->TupleCache[Index].MAC.Octet[2], pAdapter->TupleCache[Index].MAC.Octet[3],
2841
pAdapter->TupleCache[Index].MAC.Octet[4], pAdapter->TupleCache[Index].MAC.Octet[5]);
2844
else if ((memcmp(&pAdapter->TupleCache[Index].MAC, &pHeader->Controlhead.Addr2, 6)== 0))
2847
pAdapter->TupleCache[Index].Sequence = pHeader->Sequence;
2848
pAdapter->TupleCache[Index].Frag = pHeader->Frag;
2853
// tuple cache full, replace the first inserted one (even though it may not be
2854
// least referenced one)
2855
if (Index == MAX_CLIENT)
2857
pAdapter->TupleCacheLastUpdateIndex ++;
2858
if (pAdapter->TupleCacheLastUpdateIndex >= MAX_CLIENT)
2859
pAdapter->TupleCacheLastUpdateIndex = 0;
2860
Index = pAdapter->TupleCacheLastUpdateIndex;
2862
// replace with new entry
2863
memcpy(&pAdapter->TupleCache[Index].MAC, &pHeader->Controlhead.Addr2, 6);
2864
pAdapter->TupleCache[Index].Sequence = pHeader->Sequence;
2865
pAdapter->TupleCache[Index].Frag = pHeader->Frag;
2866
pAdapter->TupleCache[Index].Valid = TRUE;
2867
DBGPRINT(RT_DEBUG_INFO,"DUPCHECK - replace Entry %d, MAC=%02x:%02x:%02x:%02x:%02x:%02x\n",
2868
Index, pAdapter->TupleCache[Index].MAC.Octet[0], pAdapter->TupleCache[Index].MAC.Octet[1],
2869
pAdapter->TupleCache[Index].MAC.Octet[2], pAdapter->TupleCache[Index].MAC.Octet[3],
2870
pAdapter->TupleCache[Index].MAC.Octet[4], pAdapter->TupleCache[Index].MAC.Octet[5]);
2875
========================================================================
2877
Routine Description:
2878
Apply packet filter policy, return NDIS_STATUS_FAILURE if this frame
2882
pAdapter Pointer to our adapter
2883
pRxD Pointer to the Rx descriptor
2884
pHeader Pointer to the 802.11 frame header
2887
NDIS_STATUS_SUCCESS Accept frame
2888
NDIS_STATUS_FAILURE Drop Frame
2891
Maganement frame should bypass this filtering rule.
2893
========================================================================
2895
NDIS_STATUS RTMPApplyPacketFilter(
2896
IN PRT2570ADAPTER pAdapter,
2898
IN PHEADER_802_11 pHeader)
2902
// 0. Management frame should bypass all these filtering rules.
2903
if (pHeader->Controlhead.Frame.Type == BTYPE_MGMT)
2905
if ((pRxD->U2M) || (pRxD->Bcast) || (pRxD->Mcast))//steven:for ASIC Bug Workaround
2906
return(NDIS_STATUS_SUCCESS);
2909
// 0.1 Drop all Rx frames if MIC countermeasures kicks in
2910
if (pAdapter->PortCfg.MicErrCnt >= 2)
2912
return(NDIS_STATUS_FAILURE);
2915
// 1. Drop unicast to me packet if NDIS_PACKET_TYPE_DIRECTED is FALSE
2918
if (pAdapter->bAcceptDirect == FALSE)
2920
DBGPRINT_RAW(RT_DEBUG_INFO, "unicast not accepted\n");//steven:for debug
2921
return(NDIS_STATUS_FAILURE);
2925
// 2. Drop broadcast packet if NDIS_PACKET_TYPE_BROADCAST is FALSE
2926
else if (pRxD->Bcast)
2928
if (pAdapter->bAcceptBroadcast == FALSE)
2930
DBGPRINT(RT_DEBUG_INFO, "broadcast not accepted\n");//steven:for debug
2931
return(NDIS_STATUS_FAILURE);
2935
// 3. Drop multicast packet if NDIS_PACKET_TYPE_ALL_MULTICAST is false
2936
// and NDIS_PACKET_TYPE_MULTICAST is false.
2937
// If NDIS_PACKET_TYPE_MULTICAST is true, but NDIS_PACKET_TYPE_ALL_MULTICAST is false.
2938
// We have to deal with multicast table lookup & drop not matched packets.
2939
else if (pRxD->Mcast)
2941
if (pAdapter->bAcceptAllMulticast == FALSE)
2943
if (pAdapter->bAcceptMulticast == FALSE)
2945
DBGPRINT_RAW(RT_DEBUG_INFO, "multicast not accepted\n");//steven:for debug
2946
return(NDIS_STATUS_FAILURE);
2950
// Selected accept multicast packet based on multicast table
2951
for (i = 0; i < pAdapter->NumberOfMcAddresses; i++)
2953
if ((memcmp(&pHeader->Controlhead.Addr1, pAdapter->McastTable[i], ETH_LENGTH_OF_ADDRESS)== 0))
2960
if (i == pAdapter->NumberOfMcAddresses)
2962
DBGPRINT(RT_DEBUG_INFO,"Drop multicast %02x:%02x:%02x:%02x:%02x:%02x\n",
2963
pHeader->Controlhead.Addr1.Octet[0], pHeader->Controlhead.Addr1.Octet[1],
2964
pHeader->Controlhead.Addr1.Octet[2], pHeader->Controlhead.Addr1.Octet[3],
2965
pHeader->Controlhead.Addr1.Octet[4], pHeader->Controlhead.Addr1.Octet[5]);
2966
DBGPRINT(RT_DEBUG_LOUD, "multicast not matched\n");
2967
return(NDIS_STATUS_FAILURE);
2971
DBGPRINT(RT_DEBUG_INFO,"Accept multicast %02x:%02x:%02x:%02x:%02x:%02x\n",
2972
pHeader->Controlhead.Addr1.Octet[0], pHeader->Controlhead.Addr1.Octet[1],
2973
pHeader->Controlhead.Addr1.Octet[2], pHeader->Controlhead.Addr1.Octet[3],
2974
pHeader->Controlhead.Addr1.Octet[4], pHeader->Controlhead.Addr1.Octet[5]);
2980
// 4. Not U2M, not Mcast, not Bcast, must be unicast to other DA.
2981
// Since we did not implement promiscuous mode, just drop this kind of packet for now.
2984
DBGPRINT_RAW(RT_DEBUG_TRACE, "not-to-me unicast\n");//steven:for debug
2985
return(NDIS_STATUS_FAILURE);
2988
return(NDIS_STATUS_SUCCESS);
2992
========================================================================
2994
Routine Description:
2995
Check Rx descriptor, return NDIS_STATUS_FAILURE if any error dound
2998
pRxD Pointer to the Rx descriptor
3001
NDIS_STATUS_SUCCESS No err
3002
NDIS_STATUS_FAILURE Error
3006
========================================================================
3008
NDIS_STATUS RTMPCheckRxDescriptor(
3009
IN PRT2570ADAPTER pAdapter,
3015
DBGPRINT_RAW(RT_DEBUG_ERROR, "pRxD->PhyErr 0x%x, 0x%x, 0x%x, 0x%x\n", *(ULONG*)pRxD, *((ULONG*)pRxD+1), *((ULONG*)pRxD+2), *((ULONG*)pRxD+3));
3016
return(NDIS_STATUS_FAILURE);
3022
DBGPRINT_RAW(RT_DEBUG_ERROR, "pRxD->Crc\n");
3023
return(NDIS_STATUS_FAILURE);
3026
// Paul 04-03 for OFDM Rx length issue
3027
if (pRxD->DataByteCnt > 1604)
3029
DBGPRINT_RAW(RT_DEBUG_ERROR, "received too long, DataByteCnt = %d\n", pRxD->DataByteCnt);
3030
return NDIS_STATUS_RESET;
3033
return(NDIS_STATUS_SUCCESS);
3037
========================================================================
3039
Routine Description:
3040
Process MIC error indication and record MIC error timer.
3043
pAdapter Pointer to our adapter
3044
pWpaKey Pointer to the WPA key structure
3051
========================================================================
3053
VOID RTMPReportMicError(
3054
IN PRT2570ADAPTER pAdapter,
3055
IN PWPA_KEY pWpaKey)
3060
NDIS_802_11_STATUS_INDICATION Status;
3061
NDIS_802_11_AUTHENTICATION_REQUEST Request;
3064
// 0. Set Status to indicate auth error
3065
Report.Status.StatusType = Ndis802_11StatusType_Authentication;
3067
// 1. Check for Group or Pairwise MIC error
3068
if (pWpaKey->Type == PAIRWISE_KEY)
3069
Report.Request.Flags = NDIS_802_11_AUTH_REQUEST_PAIRWISE_ERROR;
3071
Report.Request.Flags = NDIS_802_11_AUTH_REQUEST_GROUP_ERROR;
3073
// 2. Copy AP MAC address
3074
memcpy(Report.Request.Bssid, pWpaKey->BssId, 6);
3076
// 3. Calculate length
3077
Report.Request.Length = sizeof(NDIS_802_11_AUTHENTICATION_REQUEST);
3079
// 4. Indicate to NDIS
3080
NdisMIndicateStatus(pAdapter->AdapterHandle, NDIS_STATUS_MEDIA_SPECIFIC_INDICATION, (PVOID) &Report, sizeof(Report));
3081
NdisMIndicateStatusComplete(pAdapter->AdapterHandle);
3083
// 5. Record Last MIC error time and count
3085
if (pAdapter->PortCfg.MicErrCnt == 0)
3087
pAdapter->PortCfg.MicErrCnt++;
3088
pAdapter->PortCfg.LastMicErrorTime = Now;
3090
else if (pAdapter->PortCfg.MicErrCnt == 1)
3092
if (time_after((unsigned long)Now, (unsigned long)(pAdapter->PortCfg.LastMicErrorTime + (60 * 1000))))
3094
// Update Last MIC error time, this did not violate two MIC errors within 60 seconds
3095
pAdapter->PortCfg.LastMicErrorTime = Now;
3099
pAdapter->PortCfg.LastMicErrorTime = Now;
3100
// Violate MIC error counts, MIC countermeasures kicks in
3101
pAdapter->PortCfg.MicErrCnt++;
3102
// We shall block all reception
3103
// We shall clean all Tx ring and disassoicate from AP after next EAPOL frame
3104
RTUSBRejectPendingPackets(pAdapter);
3105
RTUSBCleanUpDataBulkOutQueue(pAdapter);
3110
// MIC error count >= 2
3111
// This should not happen
3116
========================================================================
3118
Routine Description:
3119
Copy frame from waiting queue into relative ring buffer and set
3120
appropriate ASIC register to kick hardware transmit function
3123
pAdapter Pointer to our adapter
3124
pBuffer Pointer to memory of outgoing frame
3125
Length Size of outgoing management frame
3134
========================================================================
3136
VOID RTUSBMlmeHardTransmit(
3137
IN PRT2570ADAPTER pAdapter,
3138
IN PMGMT_STRUC pMgmt)
3140
PTX_CONTEXT pMLMEContext;
3143
PHEADER_802_11 pHeader_802_11;
3144
BOOLEAN AckRequired, InsertTimestamp;
3145
ULONG TransferBufferLength;
3146
PVOID pBuffer = pMgmt->pBuffer;
3147
ULONG Length = pMgmt->Length;
3149
DBGPRINT_RAW(RT_DEBUG_INFO, "--->MlmeHardTransmit\n");
3151
pAdapter->PrioRingTxCnt++;
3153
pMLMEContext = &pAdapter->MLMEContext[pAdapter->NextMLMEIndex];
3154
pMLMEContext->InUse = TRUE;
3156
// Increase & maintain Tx Ring Index
3157
pAdapter->NextMLMEIndex++;
3158
if (pAdapter->NextMLMEIndex >= PRIO_RING_SIZE)
3160
pAdapter->NextMLMEIndex = 0;
3163
pDest = pMLMEContext->TransferBuffer->WirelessPacket;
3164
pTxD = (PTXD_STRUC)(pMLMEContext->TransferBuffer);
3165
memset(pTxD, 0, sizeof(TXD_STRUC));
3167
pHeader_802_11 = (PHEADER_802_11) pBuffer;
3168
InsertTimestamp = FALSE;
3169
if (pHeader_802_11->Controlhead.Frame.Type == BTYPE_CNTL) // must be PS-POLL
3171
AckRequired = FALSE;
3173
else // BTYPE_MGMT or BMGMT_DATA(must be NULL frame)
3175
pAdapter->Sequence = ((pAdapter->Sequence) + 1) & (MAX_SEQ_NUMBER);
3176
pHeader_802_11->Sequence = pAdapter->Sequence;
3178
if (pHeader_802_11->Controlhead.Addr1.Octet[0] & 0x01) // MULTICAST, BROADCAST
3180
INC_COUNTER(pAdapter->WlanCounters.MulticastTransmittedFrameCount);
3181
AckRequired = FALSE;
3182
pHeader_802_11->Controlhead.Duration = 0;
3187
pHeader_802_11->Controlhead.Duration = RTUSBCalcDuration(pAdapter, pAdapter->PortCfg.MlmeRate, 14);
3188
if (pHeader_802_11->Controlhead.Frame.Subtype == SUBTYPE_PROBE_RSP)
3190
InsertTimestamp = TRUE;
3195
memcpy(pDest, pBuffer, Length);
3197
// Initialize Priority Descriptor
3198
// For inter-frame gap, the number is for this frame and next frame
3199
// For MLME rate, we will fix as 2Mb to match other vendor's implement
3200
RTUSBWriteTxDescriptor(pTxD, FALSE, 0, AckRequired, InsertTimestamp,
3201
TRUE, IFS_BACKOFF, Length, FALSE, 0, CW_MIN_IN_BITS, CW_MAX_IN_BITS,
3202
Length + 4, pAdapter->PortCfg.MlmeRate, 4, pAdapter->PortCfg.TxPreambleInUsed);
3204
// Build our URB for USBD
3205
TransferBufferLength = sizeof(TXD_STRUC) + Length;
3206
if ((TransferBufferLength % 2) == 1)
3207
TransferBufferLength++;
3208
if ((TransferBufferLength % pAdapter->BulkOutMaxPacketSize) == 0)
3209
TransferBufferLength += 2;
3211
pMLMEContext->BulkOutSize = TransferBufferLength;
3212
RTUSB_SET_BULK_FLAG(pAdapter, fRTUSB_BULK_OUT_MLME);
3214
DBGPRINT(RT_DEBUG_INFO, "<---MlmeHardTransmit\n");