-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathrt_linux.c
5562 lines (4418 loc) · 121 KB
/
rt_linux.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
*************************************************************************
* Ralink Tech Inc.
* 5F., No.36, Taiyuan St., Jhubei City,
* Hsinchu County 302,
* Taiwan, R.O.C.
*
* (c) Copyright 2002-2010, Ralink Technology, Inc.
*
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
* *
*************************************************************************/
#define RTMP_MODULE_OS
#define RTMP_MODULE_OS_UTIL
#include "rtmp_comm.h"
#include "rtmp_osabl.h"
#include "rt_os_util.h"
#if defined(CONFIG_RA_HW_NAT) || defined(CONFIG_RA_HW_NAT_MODULE)
#include "../../../../../../net/nat/hw_nat/ra_nat.h"
#include "../../../../../../net/nat/hw_nat/frame_engine.h"
#endif
/* TODO */
#undef RT_CONFIG_IF_OPMODE_ON_AP
#undef RT_CONFIG_IF_OPMODE_ON_STA
#if defined(CONFIG_AP_SUPPORT) && defined(CONFIG_STA_SUPPORT)
#define RT_CONFIG_IF_OPMODE_ON_AP(__OpMode) if (__OpMode == OPMODE_AP)
#define RT_CONFIG_IF_OPMODE_ON_STA(__OpMode) if (__OpMode == OPMODE_STA)
#else
#define RT_CONFIG_IF_OPMODE_ON_AP(__OpMode)
#define RT_CONFIG_IF_OPMODE_ON_STA(__OpMode)
#endif
ULONG RTDebugLevel = RT_DEBUG_ERROR;
ULONG RTDebugFunc = 0;
#ifdef OS_ABL_FUNC_SUPPORT
ULONG RTPktOffsetData = 0, RTPktOffsetLen = 0, RTPktOffsetCB = 0;
#endif /* OS_ABL_FUNC_SUPPORT */
#ifdef VENDOR_FEATURE4_SUPPORT
ULONG OS_NumOfMemAlloc = 0, OS_NumOfMemFree = 0;
#endif /* VENDOR_FEATURE4_SUPPORT */
#ifdef VENDOR_FEATURE2_SUPPORT
ULONG OS_NumOfPktAlloc = 0, OS_NumOfPktFree = 0;
#endif /* VENDOR_FEATURE2_SUPPORT */
/*
* the lock will not be used in TX/RX
* path so throughput should not be impacted
*/
BOOLEAN FlgIsUtilInit = FALSE;
OS_NDIS_SPIN_LOCK UtilSemLock;
BOOLEAN RTMP_OS_Alloc_RscOnly(VOID *pRscSrc, UINT32 RscLen);
BOOLEAN RTMP_OS_Remove_Rsc(LIST_HEADER *pRscList, VOID *pRscSrc);
/*
========================================================================
Routine Description:
Initialize something in UTIL module.
Arguments:
None
Return Value:
None
Note:
========================================================================
*/
VOID RtmpUtilInit(VOID)
{
if (FlgIsUtilInit == FALSE) {
OS_NdisAllocateSpinLock(&UtilSemLock);
FlgIsUtilInit = TRUE;
}
}
/* timeout -- ms */
static inline VOID __RTMP_SetPeriodicTimer(
IN OS_NDIS_MINIPORT_TIMER * pTimer,
IN unsigned long timeout)
{
timeout = ((timeout * OS_HZ) / 1000);
pTimer->expires = jiffies + timeout;
add_timer(pTimer);
}
/* convert NdisMInitializeTimer --> RTMP_OS_Init_Timer */
static inline VOID __RTMP_OS_Init_Timer(
IN VOID *pReserved,
IN OS_NDIS_MINIPORT_TIMER * pTimer,
IN TIMER_FUNCTION function,
IN PVOID data)
{
if (!timer_pending(pTimer)) {
init_timer(pTimer);
pTimer->data = (unsigned long)data;
pTimer->function = function;
}
}
static inline VOID __RTMP_OS_Add_Timer(
IN OS_NDIS_MINIPORT_TIMER * pTimer,
IN unsigned long timeout)
{
if (timer_pending(pTimer))
return;
timeout = ((timeout * OS_HZ) / 1000);
pTimer->expires = jiffies + timeout;
add_timer(pTimer);
}
static inline VOID __RTMP_OS_Mod_Timer(
IN OS_NDIS_MINIPORT_TIMER * pTimer,
IN unsigned long timeout)
{
timeout = ((timeout * OS_HZ) / 1000);
mod_timer(pTimer, jiffies + timeout);
}
static inline VOID __RTMP_OS_Del_Timer(
IN OS_NDIS_MINIPORT_TIMER * pTimer,
OUT BOOLEAN *pCancelled)
{
if (timer_pending(pTimer))
*pCancelled = del_timer_sync(pTimer);
else
*pCancelled = TRUE;
}
static inline VOID __RTMP_OS_Release_Timer(
IN OS_NDIS_MINIPORT_TIMER * pTimer)
{
/* nothing to do */
}
/* Unify all delay routine by using udelay */
VOID RTMPusecDelay(ULONG usec)
{
ULONG i;
for (i = 0; i < (usec / 50); i++)
udelay(50);
if (usec % 50)
udelay(usec % 50);
}
/* Unify all delay routine by using udelay */
VOID RtmpOsUsDelay(ULONG value)
{
udelay(value);
}
VOID RtmpOsMsDelay(ULONG msec)
{
mdelay(msec);
}
void RTMP_GetCurrentSystemTime(LARGE_INTEGER * time)
{
time->u.LowPart = jiffies;
}
void RTMP_GetCurrentSystemTick(ULONG *pNow)
{
*pNow = jiffies;
}
ULONG RTMPMsecsToJiffies(UINT32 m)
{
return msecs_to_jiffies(m);
}
/* pAd MUST allow to be NULL */
NDIS_STATUS os_alloc_mem(
IN VOID *pReserved,
OUT UCHAR **mem,
IN ULONG size)
{
*mem = (PUCHAR) kmalloc(size, GFP_ATOMIC);
if (*mem) {
#ifdef VENDOR_FEATURE4_SUPPORT
OS_NumOfMemAlloc++;
#endif /* VENDOR_FEATURE4_SUPPORT */
return NDIS_STATUS_SUCCESS;
} else
return NDIS_STATUS_FAILURE;
}
NDIS_STATUS os_alloc_mem_suspend(
IN VOID *pReserved,
OUT UCHAR **mem,
IN ULONG size)
{
*mem = (PUCHAR) kmalloc(size, GFP_KERNEL);
if (*mem) {
#ifdef VENDOR_FEATURE4_SUPPORT
OS_NumOfMemAlloc++;
#endif /* VENDOR_FEATURE4_SUPPORT */
return NDIS_STATUS_SUCCESS;
} else
return NDIS_STATUS_FAILURE;
}
/* pAd MUST allow to be NULL */
NDIS_STATUS os_free_mem(
IN VOID *pReserved,
IN PVOID mem)
{
ASSERT(mem);
kfree(mem);
#ifdef VENDOR_FEATURE4_SUPPORT
OS_NumOfMemFree++;
#endif /* VENDOR_FEATURE4_SUPPORT */
return NDIS_STATUS_SUCCESS;
}
#if defined(RTMP_RBUS_SUPPORT) || defined(RTMP_FLASH_SUPPORT)
/* The flag "CONFIG_RALINK_FLASH_API" is used for APSoC Linux SDK */
#ifdef CONFIG_RALINK_FLASH_API
int32_t FlashRead(
uint32_t *dst,
uint32_t *src,
uint32_t count);
int32_t FlashWrite(
uint16_t *source,
uint16_t *destination,
uint32_t numBytes);
#else /* CONFIG_RALINK_FLASH_API */
#ifdef RA_MTD_RW_BY_NUM
#if defined(CONFIG_RT2880_FLASH_32M)
#define MTD_NUM_FACTORY 5
#else
#define MTD_NUM_FACTORY 2
#endif
extern int ra_mtd_write(int num, loff_t to, size_t len, const u_char *buf);
extern int ra_mtd_read(int num, loff_t from, size_t len, u_char *buf);
#else
extern int ra_mtd_write_nm(char *name, loff_t to, size_t len, const u_char *buf);
extern int ra_mtd_read_nm(char *name, loff_t from, size_t len, u_char *buf);
#endif
#endif /* CONFIG_RALINK_FLASH_API */
void RtmpFlashRead(
UCHAR *p,
ULONG a,
ULONG b)
{
#ifdef CONFIG_RALINK_FLASH_API
FlashRead((uint32_t *) p, (uint32_t *) a, (uint32_t) b);
#else
#ifdef RA_MTD_RW_BY_NUM
ra_mtd_read(MTD_NUM_FACTORY, 0, (size_t) b, p);
#else
ra_mtd_read_nm("Factory", a&0xFFFF, (size_t) b, p);
#endif
#endif /* CONFIG_RALINK_FLASH_API */
}
void RtmpFlashWrite(
UCHAR * p,
ULONG a,
ULONG b)
{
#ifdef CONFIG_RALINK_FLASH_API
FlashWrite((uint16_t *) p, (uint16_t *) a, (uint32_t) b);
#else
#ifdef RA_MTD_RW_BY_NUM
ra_mtd_write(MTD_NUM_FACTORY, 0, (size_t) b, p);
#else
ra_mtd_write_nm("Factory", a&0xFFFF, (size_t) b, p);
#endif
#endif /* CONFIG_RALINK_FLASH_API */
}
#endif /* defined(RTMP_RBUS_SUPPORT) || defined(RTMP_FLASH_SUPPORT) */
PNDIS_PACKET RtmpOSNetPktAlloc(VOID *dummy, int size)
{
struct sk_buff *skb;
/* Add 2 more bytes for ip header alignment */
skb = dev_alloc_skb(size + 2);
if (skb != NULL)
MEM_DBG_PKT_ALLOC_INC(skb);
return ((PNDIS_PACKET) skb);
}
PNDIS_PACKET RTMP_AllocateFragPacketBuffer(VOID *dummy, ULONG len)
{
struct sk_buff *pkt;
pkt = dev_alloc_skb(len);
if (pkt == NULL) {
DBGPRINT(RT_DEBUG_ERROR,
("can't allocate frag rx %ld size packet\n", len));
}
if (pkt) {
MEM_DBG_PKT_ALLOC_INC(pkt);
RTMP_SET_PACKET_SOURCE(OSPKT_TO_RTPKT(pkt), PKTSRC_NDIS);
}
return (PNDIS_PACKET) pkt;
}
/*
The allocated NDIS PACKET must be freed via RTMPFreeNdisPacket()
*/
NDIS_STATUS RTMPAllocateNdisPacket(
IN VOID *pReserved,
OUT PNDIS_PACKET *ppPacket,
IN UCHAR *pHeader,
IN UINT HeaderLen,
IN UCHAR *pData,
IN UINT DataLen)
{
struct sk_buff *pPacket;
ASSERT(pData);
ASSERT(DataLen);
pPacket = dev_alloc_skb(HeaderLen + DataLen + RTMP_PKT_TAIL_PADDING);
if (pPacket == NULL) {
*ppPacket = NULL;
#ifdef DEBUG
printk(KERN_ERR "RTMPAllocateNdisPacket Fail\n\n");
#endif
return NDIS_STATUS_FAILURE;
}
MEM_DBG_PKT_ALLOC_INC(pPacket);
/* Clone the frame content and update the length of packet */
if (HeaderLen > 0)
NdisMoveMemory(pPacket->data, pHeader, HeaderLen);
if (DataLen > 0)
NdisMoveMemory(pPacket->data + HeaderLen, pData, DataLen);
skb_put(pPacket, HeaderLen + DataLen);
/* printk(KERN_ERR "%s : pPacket = %p, len = %d\n", __FUNCTION__, pPacket, GET_OS_PKT_LEN(pPacket));*/
RTMP_SET_PACKET_SOURCE(pPacket, PKTSRC_NDIS);
*ppPacket = (PNDIS_PACKET)pPacket;
return NDIS_STATUS_SUCCESS;
}
/*
========================================================================
Description:
This routine frees a miniport internally allocated NDIS_PACKET and its
corresponding NDIS_BUFFER and allocated memory.
========================================================================
*/
VOID RTMPFreeNdisPacket(
IN VOID *pReserved,
IN PNDIS_PACKET pPacket)
{
dev_kfree_skb_any(RTPKT_TO_OSPKT(pPacket));
MEM_DBG_PKT_FREE_INC(pPacket);
}
/* IRQL = DISPATCH_LEVEL */
/* NOTE: we do have an assumption here, that Byte0 and Byte1
* always reasid at the same scatter gather buffer
*/
NDIS_STATUS Sniff2BytesFromNdisBuffer(
IN PNDIS_BUFFER pFirstBuffer,
IN UCHAR DesiredOffset,
OUT PUCHAR pByte0,
OUT PUCHAR pByte1)
{
*pByte0 = *(PUCHAR) (pFirstBuffer + DesiredOffset);
*pByte1 = *(PUCHAR) (pFirstBuffer + DesiredOffset + 1);
return NDIS_STATUS_SUCCESS;
}
void RTMP_QueryPacketInfo(
IN PNDIS_PACKET pPacket,
OUT PACKET_INFO *info,
OUT UCHAR **pSrcBufVA,
OUT UINT *pSrcBufLen)
{
info->BufferCount = 1;
info->pFirstBuffer = (PNDIS_BUFFER) GET_OS_PKT_DATAPTR(pPacket);
info->PhysicalBufferCount = 1;
info->TotalPacketLength = GET_OS_PKT_LEN(pPacket);
*pSrcBufVA = GET_OS_PKT_DATAPTR(pPacket);
*pSrcBufLen = GET_OS_PKT_LEN(pPacket);
#ifdef TX_PKT_SG
if (RTMP_GET_PKT_SG(pPacket)) {
struct sk_buff *skb = (struct sk_buff *)pPacket;
int i, nr_frags = skb_shinfo(skb)->nr_frags;
info->BufferCount = nr_frags + 1;
info->PhysicalBufferCount = info->BufferCount;
info->sg_list[0].data = (VOID *)GET_OS_PKT_DATAPTR(pPacket);
info->sg_list[0].len = skb_headlen(skb);
for (i = 0; i < nr_frags; i++) {
skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
info->sg_list[i+1].data = ((void *) page_address(frag->page) +
frag->page_offset);
info->sg_list[i+1].len = frag->size;
}
}
#endif /* TX_PKT_SG */
}
PNDIS_PACKET DuplicatePacket(
IN PNET_DEV pNetDev,
IN PNDIS_PACKET pPacket,
IN UCHAR FromWhichBSSID)
{
struct sk_buff *skb;
PNDIS_PACKET pRetPacket = NULL;
USHORT DataSize;
UCHAR *pData;
DataSize = (USHORT) GET_OS_PKT_LEN(pPacket);
pData = (PUCHAR) GET_OS_PKT_DATAPTR(pPacket);
skb = skb_clone(RTPKT_TO_OSPKT(pPacket), MEM_ALLOC_FLAG);
if (skb) {
MEM_DBG_PKT_ALLOC_INC(skb);
skb->dev = pNetDev; /*get_netdev_from_bssid(pAd, FromWhichBSSID); */
pRetPacket = OSPKT_TO_RTPKT(skb);
}
return pRetPacket;
}
PNDIS_PACKET duplicate_pkt(
IN PNET_DEV pNetDev,
IN PUCHAR pHeader802_3,
IN UINT HdrLen,
IN PUCHAR pData,
IN ULONG DataSize,
IN UCHAR FromWhichBSSID)
{
struct sk_buff *skb;
PNDIS_PACKET pPacket = NULL;
if ((skb =
__dev_alloc_skb(HdrLen + DataSize + 2, MEM_ALLOC_FLAG)) != NULL) {
MEM_DBG_PKT_ALLOC_INC(skb);
skb_reserve(skb, 2);
NdisMoveMemory(GET_OS_PKT_DATATAIL(skb), pHeader802_3, HdrLen);
skb_put(skb, HdrLen);
NdisMoveMemory(GET_OS_PKT_DATATAIL(skb), pData, DataSize);
skb_put(skb, DataSize);
skb->dev = pNetDev; /*get_netdev_from_bssid(pAd, FromWhichBSSID); */
pPacket = OSPKT_TO_RTPKT(skb);
}
return pPacket;
}
#define TKIP_TX_MIC_SIZE 8
PNDIS_PACKET duplicate_pkt_with_TKIP_MIC(
IN VOID *pReserved,
IN PNDIS_PACKET pPacket)
{
struct sk_buff *skb, *newskb;
skb = RTPKT_TO_OSPKT(pPacket);
if (skb_tailroom(skb) < TKIP_TX_MIC_SIZE) {
/* alloc a new skb and copy the packet */
newskb = skb_copy_expand(skb, skb_headroom(skb), TKIP_TX_MIC_SIZE, GFP_ATOMIC);
dev_kfree_skb_any(skb);
MEM_DBG_PKT_FREE_INC(skb);
if (newskb == NULL) {
DBGPRINT(RT_DEBUG_ERROR, ("Extend Tx.MIC for packet failed!, dropping packet!\n"));
return NULL;
}
skb = newskb;
MEM_DBG_PKT_ALLOC_INC(skb);
}
return OSPKT_TO_RTPKT(skb);
}
/*
========================================================================
Routine Description:
Send a L2 frame to upper daemon to trigger state machine
Arguments:
pAd - pointer to our pAdapter context
Return Value:
Note:
========================================================================
*/
BOOLEAN RTMPL2FrameTxAction(
IN VOID * pCtrlBkPtr,
IN PNET_DEV pNetDev,
IN RTMP_CB_8023_PACKET_ANNOUNCE _announce_802_3_packet,
IN UCHAR apidx,
IN PUCHAR pData,
IN UINT32 data_len,
IN UCHAR OpMode)
{
struct sk_buff *skb = dev_alloc_skb(data_len + 2);
if (!skb) {
DBGPRINT(RT_DEBUG_ERROR,
("%s : Error! Can't allocate a skb.\n", __FUNCTION__));
return FALSE;
}
MEM_DBG_PKT_ALLOC_INC(skb);
/*get_netdev_from_bssid(pAd, apidx)); */
SET_OS_PKT_NETDEV(skb, pNetDev);
/* 16 byte align the IP header */
skb_reserve(skb, 2);
/* Insert the frame content */
NdisMoveMemory(GET_OS_PKT_DATAPTR(skb), pData, data_len);
/* End this frame */
skb_put(GET_OS_PKT_TYPE(skb), data_len);
DBGPRINT(RT_DEBUG_TRACE, ("%s doen\n", __FUNCTION__));
_announce_802_3_packet(pCtrlBkPtr, skb, OpMode);
return TRUE;
}
PNDIS_PACKET ExpandPacket(
IN VOID *pReserved,
IN PNDIS_PACKET pPacket,
IN UINT32 ext_head_len,
IN UINT32 ext_tail_len)
{
struct sk_buff *skb, *newskb;
skb = RTPKT_TO_OSPKT(pPacket);
if (skb_cloned(skb) || (skb_headroom(skb) < ext_head_len)
|| (skb_tailroom(skb) < ext_tail_len)) {
UINT32 head_len =
(skb_headroom(skb) <
ext_head_len) ? ext_head_len : skb_headroom(skb);
UINT32 tail_len =
(skb_tailroom(skb) <
ext_tail_len) ? ext_tail_len : skb_tailroom(skb);
/* alloc a new skb and copy the packet */
newskb = skb_copy_expand(skb, head_len, tail_len, GFP_ATOMIC);
dev_kfree_skb_any(skb);
MEM_DBG_PKT_FREE_INC(skb);
if (newskb == NULL) {
DBGPRINT(RT_DEBUG_ERROR,
("Extend Tx buffer for WPI failed!, dropping packet!\n"));
return NULL;
}
skb = newskb;
MEM_DBG_PKT_ALLOC_INC(skb);
}
return OSPKT_TO_RTPKT(skb);
}
PNDIS_PACKET ClonePacket(
IN VOID *pReserved,
IN PNDIS_PACKET pPacket,
IN PUCHAR pData,
IN ULONG DataSize)
{
struct sk_buff *pRxPkt;
struct sk_buff *pClonedPkt;
ASSERT(pPacket);
pRxPkt = RTPKT_TO_OSPKT(pPacket);
/* clone the packet */
pClonedPkt = skb_clone(pRxPkt, MEM_ALLOC_FLAG);
if (pClonedPkt) {
/* set the correct dataptr and data len */
MEM_DBG_PKT_ALLOC_INC(pClonedPkt);
pClonedPkt->dev = pRxPkt->dev;
pClonedPkt->data = pData;
pClonedPkt->len = DataSize;
SET_OS_PKT_DATATAIL(pClonedPkt, pClonedPkt->data, pClonedPkt->len);
ASSERT(DataSize < 1530);
}
return pClonedPkt;
}
VOID RtmpOsPktInit(
IN PNDIS_PACKET pNetPkt,
IN PNET_DEV pNetDev,
IN UCHAR *pData,
IN USHORT DataSize)
{
PNDIS_PACKET pRxPkt;
pRxPkt = RTPKT_TO_OSPKT(pNetPkt);
SET_OS_PKT_NETDEV(pRxPkt, pNetDev);
SET_OS_PKT_DATAPTR(pRxPkt, pData);
SET_OS_PKT_LEN(pRxPkt, DataSize);
SET_OS_PKT_DATATAIL(pRxPkt, pData, DataSize);
}
void wlan_802_11_to_802_3_packet(
IN PNET_DEV pNetDev,
IN UCHAR OpMode,
IN USHORT VLAN_VID,
IN USHORT VLAN_Priority,
IN PNDIS_PACKET pRxPacket,
IN UCHAR *pData,
IN ULONG DataSize,
IN PUCHAR pHeader802_3,
IN UCHAR FromWhichBSSID,
IN UCHAR *TPID)
{
struct sk_buff *pOSPkt;
ASSERT(pHeader802_3);
pOSPkt = RTPKT_TO_OSPKT(pRxPacket);
/*get_netdev_from_bssid(pAd, FromWhichBSSID); */
pOSPkt->dev = pNetDev;
pOSPkt->data = pData;
pOSPkt->len = DataSize;
SET_OS_PKT_DATATAIL(pOSPkt, pOSPkt->data, pOSPkt->len);
/* copy 802.3 header */
#ifdef CONFIG_STA_SUPPORT
RT_CONFIG_IF_OPMODE_ON_STA(OpMode)
{
NdisMoveMemory(skb_push(pOSPkt, LENGTH_802_3), pHeader802_3, LENGTH_802_3);
}
#endif /* CONFIG_STA_SUPPORT */
}
void hex_dump(char *str, UCHAR *pSrcBufVA, UINT SrcBufLen)
{
#ifdef DBG
unsigned char *pt;
int x;
if (RTDebugLevel < RT_DEBUG_TRACE)
return;
pt = pSrcBufVA;
printk("%s: %p, len = %d\n", str, pSrcBufVA, SrcBufLen);
for (x = 0; x < SrcBufLen; x++) {
if (x % 16 == 0)
printk("0x%04x : ", x);
printk("%02x ", ((unsigned char)pt[x]));
if (x % 16 == 15)
printk("\n");
}
printk("\n");
#endif /* DBG */
}
#ifdef SYSTEM_LOG_SUPPORT
/*
========================================================================
Routine Description:
Send log message through wireless event
Support standard iw_event with IWEVCUSTOM. It is used below.
iwreq_data.data.flags is used to store event_flag that is
defined by user. iwreq_data.data.length is the length of the
event log.
The format of the event log is composed of the entry's MAC
address and the desired log message (refer to
pWirelessEventText).
ex: 11:22:33:44:55:66 has associated successfully
p.s. The requirement of Wireless Extension is v15 or newer.
========================================================================
*/
VOID RtmpOsSendWirelessEvent(
IN VOID *pAd,
IN USHORT Event_flag,
IN PUCHAR pAddr,
IN UCHAR BssIdx,
IN CHAR Rssi,
IN RTMP_OS_SEND_WLAN_EVENT pFunc)
{
#if WIRELESS_EXT >= 15
pFunc(pAd, Event_flag, pAddr, BssIdx, Rssi);
#else
DBGPRINT(RT_DEBUG_ERROR,
("%s : The Wireless Extension MUST be v15 or newer.\n",
__FUNCTION__));
#endif /* WIRELESS_EXT >= 15 */
}
#endif /* SYSTEM_LOG_SUPPORT */
#ifdef CONFIG_STA_SUPPORT
INT32 ralinkrate[] = {
2, 4, 11, 22,
12, 18, 24, 36, 48, 72, 96, 108, 109, 110, 111, 112, /* CCK and OFDM */
13, 26, 39, 52, 78, 104, 117, 130, 26, 52, 78, 104, 156, 208, 234, 260,
39, 78, 117, 156, 234, 312, 351, 390, /* BW 20, 800ns GI, MCS 0~23 */
27, 54, 81, 108, 162, 216, 243, 270, 54, 108, 162, 216, 324, 432, 486, 540,
81, 162, 243, 324, 486, 648, 729, 810, /* BW 40, 800ns GI, MCS 0~23 */
14, 29, 43, 57, 87, 115, 130, 144, 29, 59, 87, 115, 173, 230, 260, 288,
43, 87, 130, 173, 260, 317, 390, 433, /* BW 20, 400ns GI, MCS 0~23 */
30, 60, 90, 120, 180, 240, 270, 300, 60, 120, 180, 240, 360, 480, 540, 600,
90, 180, 270, 360, 540, 720, 810, 900, /* BW 40, 400ns GI, MCS 0~23 */
13, 26, 39, 52, 78, 104, 117, 130, 156, /* AC: 20Mhz, 800ns GI, MCS: 0~8 */
27, 54, 81, 108, 162, 216, 243, 270, 324, 360, /* AC: 40Mhz, 800ns GI, MCS: 0~9 */
59, 117, 176, 234, 351, 468, 527, 585, 702, 780, /* AC: 80Mhz, 800ns GI, MCS: 0~9 */
14, 29, 43, 57, 87, 115, 130, 144, 173, /* AC: 20Mhz, 400ns GI, MCS: 0~8 */
30, 60, 90, 120, 180, 240, 270, 300, 360, 400, /* AC: 40Mhz, 400ns GI, MCS: 0~9 */
65, 130, 195, 260, 390, 520, 585, 650, 780, 867, /* AC: 80Mhz, 400ns GI, MCS: 0~9 */
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
40, 41, 42, 43, 44, 45, 46, 47 /* 3*3 */
};
UINT32 RT_RateSize = sizeof (ralinkrate);
void send_monitor_packets(IN PNET_DEV pNetDev,
IN PNDIS_PACKET pRxPacket,
IN PHEADER_802_11 pHeader,
IN UCHAR * pData,
IN USHORT DataSize,
IN UCHAR L2PAD,
IN UCHAR PHYMODE,
IN UCHAR BW,
IN UCHAR ShortGI,
IN UCHAR MCS,
IN UCHAR AMPDU,
IN UCHAR STBC,
IN UCHAR RSSI1,
IN UCHAR BssMonitorFlag11n,
IN UCHAR * pDevName,
IN UCHAR Channel,
IN UCHAR CentralChannel,
IN UINT32 MaxRssi) {
struct sk_buff *pOSPkt;
wlan_ng_prism2_header *ph;
#ifdef MONITOR_FLAG_11N_SNIFFER_SUPPORT
ETHEREAL_RADIO h,
*ph_11n33; /* for new 11n sniffer format */
#endif /* MONITOR_FLAG_11N_SNIFFER_SUPPORT */
int rate_index = 0;
USHORT header_len = 0;
UCHAR temp_header[40] = {
0};
MEM_DBG_PKT_FREE_INC(pRxPacket);
pOSPkt = RTPKT_TO_OSPKT(pRxPacket); /*pRxBlk->pRxPacket); */
pOSPkt->dev = pNetDev; /*get_netdev_from_bssid(pAd, BSS0); */
if (pHeader->FC.Type == BTYPE_DATA) {
DataSize -= LENGTH_802_11;
if ((pHeader->FC.ToDs == 1) && (pHeader->FC.FrDs == 1))
header_len = LENGTH_802_11_WITH_ADDR4;
else
header_len = LENGTH_802_11;
/* QOS */
if (pHeader->FC.SubType & 0x08) {
header_len += 2;
/* Data skip QOS contorl field */
DataSize -= 2;
}
/* Order bit: A-Ralink or HTC+ */
if (pHeader->FC.Order) {
header_len += 4;
/* Data skip HTC contorl field */
DataSize -= 4;
}
/* Copy Header */
if (header_len <= 40)
NdisMoveMemory(temp_header, pData, header_len);
/* skip HW padding */
if (L2PAD)
pData += (header_len + 2);
else
pData += header_len;
}
if (DataSize < pOSPkt->len) {
skb_trim(pOSPkt, DataSize);
} else {
skb_put(pOSPkt, (DataSize - pOSPkt->len));
}
if ((pData - pOSPkt->data) > 0) {
skb_put(pOSPkt, (pData - pOSPkt->data));
skb_pull(pOSPkt, (pData - pOSPkt->data));
}
if (skb_headroom(pOSPkt) < (sizeof (wlan_ng_prism2_header) + header_len)) {
if (pskb_expand_head(pOSPkt, (sizeof (wlan_ng_prism2_header) + header_len), 0, GFP_ATOMIC)) {
DBGPRINT(RT_DEBUG_ERROR,
("%s : Reallocate header size of sk_buff fail!\n",
__FUNCTION__));
goto err_free_sk_buff;
}
}
if (header_len > 0)
NdisMoveMemory(skb_push(pOSPkt, header_len), temp_header,
header_len);
#ifdef MONITOR_FLAG_11N_SNIFFER_SUPPORT
if (BssMonitorFlag11n == 0)
#endif /* MONITOR_FLAG_11N_SNIFFER_SUPPORT */
{
ph = (wlan_ng_prism2_header *) skb_push(pOSPkt,
sizeof(wlan_ng_prism2_header));
NdisZeroMemory(ph, sizeof(wlan_ng_prism2_header));
ph->msgcode = DIDmsg_lnxind_wlansniffrm;
ph->msglen = sizeof (wlan_ng_prism2_header);
strcpy((PSTRING) ph->devname, (PSTRING) pDevName);
ph->hosttime.did = DIDmsg_lnxind_wlansniffrm_hosttime;
ph->hosttime.status = 0;
ph->hosttime.len = 4;
ph->hosttime.data = jiffies;
ph->mactime.did = DIDmsg_lnxind_wlansniffrm_mactime;
ph->mactime.status = 0;
ph->mactime.len = 0;
ph->mactime.data = 0;
ph->istx.did = DIDmsg_lnxind_wlansniffrm_istx;
ph->istx.status = 0;
ph->istx.len = 0;
ph->istx.data = 0;
ph->channel.did = DIDmsg_lnxind_wlansniffrm_channel;
ph->channel.status = 0;
ph->channel.len = 4;
ph->channel.data = (u_int32_t) Channel;
ph->rssi.did = DIDmsg_lnxind_wlansniffrm_rssi;
ph->rssi.status = 0;
ph->rssi.len = 4;
ph->rssi.data = MaxRssi;
ph->signal.did = DIDmsg_lnxind_wlansniffrm_signal;
ph->signal.status = 0;
ph->signal.len = 4;
ph->signal.data = 0; /*rssi + noise; */
ph->noise.did = DIDmsg_lnxind_wlansniffrm_noise;
ph->noise.status = 0;
ph->noise.len = 4;
ph->noise.data = 0;
#ifdef DOT11_N_SUPPORT
if (PHYMODE >= MODE_HTMIX) {
rate_index = 12 + ((UCHAR) BW * 24) + ((UCHAR) ShortGI * 48) + ((UCHAR) MCS);
} else
#endif /* DOT11_N_SUPPORT */
if (PHYMODE == MODE_OFDM)
rate_index = (UCHAR) (MCS) + 4;
else
rate_index = (UCHAR) (MCS);
if (rate_index < 0)
rate_index = 0;
if (rate_index >= (sizeof (ralinkrate) / sizeof (ralinkrate[0])))
rate_index = (sizeof (ralinkrate) / sizeof (ralinkrate[0])) - 1;
ph->rate.did = DIDmsg_lnxind_wlansniffrm_rate;
ph->rate.status = 0;
ph->rate.len = 4;
/* real rate = ralinkrate[rate_index] / 2 */
ph->rate.data = ralinkrate[rate_index];
ph->frmlen.did = DIDmsg_lnxind_wlansniffrm_frmlen;
ph->frmlen.status = 0;
ph->frmlen.len = 4;
ph->frmlen.data = (u_int32_t) DataSize;
}
#ifdef MONITOR_FLAG_11N_SNIFFER_SUPPORT
else {
ph_11n33 = &h;
NdisZeroMemory((unsigned char *)ph_11n33,
sizeof (ETHEREAL_RADIO));
/*802.11n fields */
if (MCS > 15)
ph_11n33->Flag_80211n |= WIRESHARK_11N_FLAG_3x3;
if (PHYMODE == MODE_HTGREENFIELD)
ph_11n33->Flag_80211n |= WIRESHARK_11N_FLAG_GF;
if (BW == 1) {
ph_11n33->Flag_80211n |= WIRESHARK_11N_FLAG_BW40;
} else if (Channel < CentralChannel) {
ph_11n33->Flag_80211n |= WIRESHARK_11N_FLAG_BW20U;
} else if (Channel > CentralChannel) {
ph_11n33->Flag_80211n |= WIRESHARK_11N_FLAG_BW20D;
} else {
ph_11n33->Flag_80211n |=
(WIRESHARK_11N_FLAG_BW20U |
WIRESHARK_11N_FLAG_BW20D);
}
if (ShortGI == 1)
ph_11n33->Flag_80211n |= WIRESHARK_11N_FLAG_SGI;
if (AMPDU)
ph_11n33->Flag_80211n |= WIRESHARK_11N_FLAG_AMPDU;
if (STBC)
ph_11n33->Flag_80211n |= WIRESHARK_11N_FLAG_STBC;
ph_11n33->signal_level = (UCHAR) RSSI1;
/* data_rate is the rate index in the wireshark rate table */
if (PHYMODE >= MODE_HTMIX) {
if (MCS == 32) {