forked from morrownr/88x2bu-20210702
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrtw_br_ext.c
1590 lines (1319 loc) · 45.3 KB
/
rtw_br_ext.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
/******************************************************************************
*
* Copyright(c) 2007 - 2017 Realtek Corporation.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of version 2 of the GNU General Public License as
* published by the Free Software Foundation.
*
* 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.
*
*****************************************************************************/
#define _RTW_BR_EXT_C_
#ifdef __KERNEL__
#include <linux/if_arp.h>
#include <net/ip.h>
#include <linux/version.h>
#if (LINUX_VERSION_CODE < KERNEL_VERSION(5, 15, 0))
#include <net/ipx.h>
#endif
#include <linux/atalk.h>
#include <linux/udp.h>
#include <linux/if_pppox.h>
#endif
#if 1 /* rtw_wifi_driver */
#include <drv_types.h>
#else /* rtw_wifi_driver */
#include "./8192cd_cfg.h"
#ifndef __KERNEL__
#include "./sys-support.h"
#endif
#include "./8192cd.h"
#include "./8192cd_headers.h"
#include "./8192cd_br_ext.h"
#include "./8192cd_debug.h"
#endif /* rtw_wifi_driver */
#ifdef CL_IPV6_PASS
#ifdef __KERNEL__
#include <linux/ipv6.h>
#include <linux/icmpv6.h>
#include <net/ndisc.h>
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24))
#include <net/ip6_checksum.h>
#else
#include <net/checksum.h>
#endif
#endif
#endif
#ifdef CONFIG_BR_EXT
/* #define BR_EXT_DEBUG */
#define NAT25_IPV4 01
#define NAT25_IPV6 02
#define NAT25_IPX 03
#define NAT25_APPLE 04
#define NAT25_PPPOE 05
#define RTL_RELAY_TAG_LEN (ETH_ALEN)
#define TAG_HDR_LEN 4
#define MAGIC_CODE 0x8186
#define MAGIC_CODE_LEN 2
#define WAIT_TIME_PPPOE 5 /* waiting time for pppoe server in sec */
/*-----------------------------------------------------------------
How database records network address:
0 1 2 3 4 5 6 7 8 9 10
|----|----|----|----|----|----|----|----|----|----|----|
IPv4 |type| | IP addr |
IPX |type| Net addr | Node addr |
IPX |type| Net addr |Sckt addr|
Apple |type| Network |node|
PPPoE |type| SID | AC MAC |
-----------------------------------------------------------------*/
/* Find a tag in pppoe frame and return the pointer */
static unsigned char *__nat25_find_pppoe_tag(struct pppoe_hdr *ph, unsigned short type)
{
unsigned char *cur_ptr, *start_ptr;
unsigned short tagLen, tagType;
start_ptr = cur_ptr = (unsigned char *)ph->tag;
while ((cur_ptr - start_ptr) < ntohs(ph->length)) {
/* prevent un-alignment access */
tagType = (unsigned short)((cur_ptr[0] << 8) + cur_ptr[1]);
tagLen = (unsigned short)((cur_ptr[2] << 8) + cur_ptr[3]);
if (tagType == type)
return cur_ptr;
cur_ptr = cur_ptr + TAG_HDR_LEN + tagLen;
}
return 0;
}
static int __nat25_add_pppoe_tag(struct sk_buff *skb, struct pppoe_tag *tag)
{
struct pppoe_hdr *ph = (struct pppoe_hdr *)(skb->data + ETH_HLEN);
int data_len;
data_len = tag->tag_len + TAG_HDR_LEN;
if (skb_tailroom(skb) < data_len) {
_DEBUG_ERR("skb_tailroom() failed in add SID tag!\n");
return -1;
}
skb_put(skb, data_len);
/* have a room for new tag */
memmove(((unsigned char *)ph->tag + data_len), (unsigned char *)ph->tag, ntohs(ph->length));
ph->length = htons(ntohs(ph->length) + data_len);
memcpy((unsigned char *)ph->tag, tag, data_len);
return data_len;
}
static int skb_pull_and_merge(struct sk_buff *skb, unsigned char *src, int len)
{
int tail_len;
unsigned long end, tail;
if ((src + len) > skb_tail_pointer(skb) || skb->len < len)
return -1;
tail = (unsigned long)skb_tail_pointer(skb);
end = (unsigned long)src + len;
if (tail < end)
return -1;
tail_len = (int)(tail - end);
if (tail_len > 0)
memmove(src, src + len, tail_len);
skb_trim(skb, skb->len - len);
return 0;
}
static unsigned long __nat25_timeout(_adapter *priv)
{
unsigned long timeout;
timeout = jiffies - NAT25_AGEING_TIME * HZ;
return timeout;
}
static int __nat25_has_expired(_adapter *priv,
struct nat25_network_db_entry *fdb)
{
if (time_before_eq(fdb->ageing_timer, __nat25_timeout(priv)))
return 1;
return 0;
}
static void __nat25_generate_ipv4_network_addr(unsigned char *networkAddr,
unsigned int *ipAddr)
{
memset(networkAddr, 0, MAX_NETWORK_ADDR_LEN);
networkAddr[0] = NAT25_IPV4;
memcpy(networkAddr + 7, (unsigned char *)ipAddr, 4);
}
#if (LINUX_VERSION_CODE < KERNEL_VERSION(5, 15, 0))
static __inline__ void __nat25_generate_ipx_network_addr_with_node(unsigned char *networkAddr,
unsigned int *ipxNetAddr, unsigned char *ipxNodeAddr)
{
memset(networkAddr, 0, MAX_NETWORK_ADDR_LEN);
networkAddr[0] = NAT25_IPX;
memcpy(networkAddr + 1, (unsigned char *)ipxNetAddr, 4);
memcpy(networkAddr + 5, ipxNodeAddr, 6);
}
static __inline__ void __nat25_generate_ipx_network_addr_with_socket(unsigned char *networkAddr,
unsigned int *ipxNetAddr, unsigned short *ipxSocketAddr)
{
memset(networkAddr, 0, MAX_NETWORK_ADDR_LEN);
networkAddr[0] = NAT25_IPX;
memcpy(networkAddr + 1, (unsigned char *)ipxNetAddr, 4);
memcpy(networkAddr + 5, (unsigned char *)ipxSocketAddr, 2);
}
static __inline__ void __nat25_generate_apple_network_addr(unsigned char *networkAddr,
unsigned short *network, unsigned char *node)
{
memset(networkAddr, 0, MAX_NETWORK_ADDR_LEN);
networkAddr[0] = NAT25_APPLE;
memcpy(networkAddr + 1, (unsigned char *)network, 2);
networkAddr[3] = *node;
}
#endif
static __inline__ void __nat25_generate_pppoe_network_addr(unsigned char *networkAddr,
unsigned char *ac_mac, unsigned short *sid)
{
memset(networkAddr, 0, MAX_NETWORK_ADDR_LEN);
networkAddr[0] = NAT25_PPPOE;
memcpy(networkAddr + 1, (unsigned char *)sid, 2);
memcpy(networkAddr + 3, (unsigned char *)ac_mac, 6);
}
#ifdef CL_IPV6_PASS
static void __nat25_generate_ipv6_network_addr(unsigned char *networkAddr,
unsigned int *ipAddr)
{
memset(networkAddr, 0, MAX_NETWORK_ADDR_LEN);
networkAddr[0] = NAT25_IPV6;
memcpy(networkAddr + 1, (unsigned char *)ipAddr, 16);
}
static unsigned char *scan_tlv(unsigned char *data, int len, unsigned char tag, unsigned char len8b)
{
while (len > 0) {
if (*data == tag && *(data + 1) == len8b && len >= len8b * 8)
return data + 2;
len -= (*(data + 1)) * 8;
data += (*(data + 1)) * 8;
}
return NULL;
}
static int update_nd_link_layer_addr(unsigned char *data, int len, unsigned char *replace_mac)
{
struct icmp6hdr *icmphdr = (struct icmp6hdr *)data;
unsigned char *mac;
if (icmphdr->icmp6_type == NDISC_ROUTER_SOLICITATION) {
if (len >= 8) {
mac = scan_tlv(&data[8], len - 8, 1, 1);
if (mac) {
RTW_INFO("Router Solicitation, replace MAC From: %02x:%02x:%02x:%02x:%02x:%02x, To: %02x:%02x:%02x:%02x:%02x:%02x\n",
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5],
replace_mac[0], replace_mac[1], replace_mac[2], replace_mac[3], replace_mac[4], replace_mac[5]);
memcpy(mac, replace_mac, 6);
return 1;
}
}
} else if (icmphdr->icmp6_type == NDISC_ROUTER_ADVERTISEMENT) {
if (len >= 16) {
mac = scan_tlv(&data[16], len - 16, 1, 1);
if (mac) {
RTW_INFO("Router Advertisement, replace MAC From: %02x:%02x:%02x:%02x:%02x:%02x, To: %02x:%02x:%02x:%02x:%02x:%02x\n",
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5],
replace_mac[0], replace_mac[1], replace_mac[2], replace_mac[3], replace_mac[4], replace_mac[5]);
memcpy(mac, replace_mac, 6);
return 1;
}
}
} else if (icmphdr->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION) {
if (len >= 24) {
mac = scan_tlv(&data[24], len - 24, 1, 1);
if (mac) {
RTW_INFO("Neighbor Solicitation, replace MAC From: %02x:%02x:%02x:%02x:%02x:%02x, To: %02x:%02x:%02x:%02x:%02x:%02x\n",
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5],
replace_mac[0], replace_mac[1], replace_mac[2], replace_mac[3], replace_mac[4], replace_mac[5]);
memcpy(mac, replace_mac, 6);
return 1;
}
}
} else if (icmphdr->icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
if (len >= 24) {
mac = scan_tlv(&data[24], len - 24, 2, 1);
if (mac) {
RTW_INFO("Neighbor Advertisement, replace MAC From: %02x:%02x:%02x:%02x:%02x:%02x, To: %02x:%02x:%02x:%02x:%02x:%02x\n",
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5],
replace_mac[0], replace_mac[1], replace_mac[2], replace_mac[3], replace_mac[4], replace_mac[5]);
memcpy(mac, replace_mac, 6);
return 1;
}
}
} else if (icmphdr->icmp6_type == NDISC_REDIRECT) {
if (len >= 40) {
mac = scan_tlv(&data[40], len - 40, 2, 1);
if (mac) {
RTW_INFO("Redirect, replace MAC From: %02x:%02x:%02x:%02x:%02x:%02x, To: %02x:%02x:%02x:%02x:%02x:%02x\n",
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5],
replace_mac[0], replace_mac[1], replace_mac[2], replace_mac[3], replace_mac[4], replace_mac[5]);
memcpy(mac, replace_mac, 6);
return 1;
}
}
}
return 0;
}
#ifdef SUPPORT_RX_UNI2MCAST
static void convert_ipv6_mac_to_mc(struct sk_buff *skb)
{
struct ipv6hdr *iph = (struct ipv6hdr *)(skb->data + ETH_HLEN);
unsigned char *dst_mac = skb->data;
/* dst_mac[0] = 0xff; */
/* dst_mac[1] = 0xff; */
/*modified by qinjunjie,ipv6 multicast address ix 0x33-33-xx-xx-xx-xx*/
dst_mac[0] = 0x33;
dst_mac[1] = 0x33;
memcpy(&dst_mac[2], &iph->daddr.s6_addr32[3], 4);
#if defined(__LINUX_2_6__)
/*modified by qinjunjie,warning:should not remove next line*/
skb->pkt_type = PACKET_MULTICAST;
#endif
}
#endif /* CL_IPV6_PASS */
#endif /* SUPPORT_RX_UNI2MCAST */
static int __nat25_network_hash(unsigned char *networkAddr)
{
if (networkAddr[0] == NAT25_IPV4) {
unsigned long x;
x = networkAddr[7] ^ networkAddr[8] ^ networkAddr[9] ^ networkAddr[10];
return x & (NAT25_HASH_SIZE - 1);
#if (LINUX_VERSION_CODE < KERNEL_VERSION(5, 15, 0))
} else if (networkAddr[0] == NAT25_IPX) {
unsigned long x;
x = networkAddr[1] ^ networkAddr[2] ^ networkAddr[3] ^ networkAddr[4] ^ networkAddr[5] ^
networkAddr[6] ^ networkAddr[7] ^ networkAddr[8] ^ networkAddr[9] ^ networkAddr[10];
return x & (NAT25_HASH_SIZE - 1);
} else if (networkAddr[0] == NAT25_APPLE) {
unsigned long x;
x = networkAddr[1] ^ networkAddr[2] ^ networkAddr[3];
return x & (NAT25_HASH_SIZE - 1);
#endif
} else if (networkAddr[0] == NAT25_PPPOE) {
unsigned long x;
x = networkAddr[0] ^ networkAddr[1] ^ networkAddr[2] ^ networkAddr[3] ^ networkAddr[4] ^ networkAddr[5] ^ networkAddr[6] ^ networkAddr[7] ^ networkAddr[8];
return x & (NAT25_HASH_SIZE - 1);
}
#ifdef CL_IPV6_PASS
else if (networkAddr[0] == NAT25_IPV6) {
unsigned long x;
x = networkAddr[1] ^ networkAddr[2] ^ networkAddr[3] ^ networkAddr[4] ^ networkAddr[5] ^
networkAddr[6] ^ networkAddr[7] ^ networkAddr[8] ^ networkAddr[9] ^ networkAddr[10] ^
networkAddr[11] ^ networkAddr[12] ^ networkAddr[13] ^ networkAddr[14] ^ networkAddr[15] ^
networkAddr[16];
return x & (NAT25_HASH_SIZE - 1);
}
#endif
else {
unsigned long x = 0;
int i;
for (i = 0; i < MAX_NETWORK_ADDR_LEN; i++)
x ^= networkAddr[i];
return x & (NAT25_HASH_SIZE - 1);
}
}
static void __network_hash_link(_adapter *priv,
struct nat25_network_db_entry *ent, int hash)
{
/* Caller must _enter_critical_bh already! */
/* _irqL irqL; */
/* _enter_critical_bh(&priv->br_ext_lock, &irqL); */
ent->next_hash = priv->nethash[hash];
if (ent->next_hash != NULL)
ent->next_hash->pprev_hash = &ent->next_hash;
priv->nethash[hash] = ent;
ent->pprev_hash = &priv->nethash[hash];
/* _exit_critical_bh(&priv->br_ext_lock, &irqL); */
}
static void __network_hash_unlink(struct nat25_network_db_entry *ent)
{
/* Caller must _enter_critical_bh already! */
/* _irqL irqL; */
/* _enter_critical_bh(&priv->br_ext_lock, &irqL); */
*(ent->pprev_hash) = ent->next_hash;
if (ent->next_hash != NULL)
ent->next_hash->pprev_hash = ent->pprev_hash;
ent->next_hash = NULL;
ent->pprev_hash = NULL;
/* _exit_critical_bh(&priv->br_ext_lock, &irqL); */
}
static int __nat25_db_network_lookup_and_replace(_adapter *priv,
struct sk_buff *skb, unsigned char *networkAddr)
{
struct nat25_network_db_entry *db;
_irqL irqL;
_enter_critical_bh(&priv->br_ext_lock, &irqL);
db = priv->nethash[__nat25_network_hash(networkAddr)];
while (db != NULL) {
if (!memcmp(db->networkAddr, networkAddr, MAX_NETWORK_ADDR_LEN)) {
if (!__nat25_has_expired(priv, db)) {
/* replace the destination mac address */
memcpy(skb->data, db->macAddr, ETH_ALEN);
atomic_inc(&db->use_count);
#ifdef CL_IPV6_PASS
RTW_INFO("NAT25: Lookup M:%02x%02x%02x%02x%02x%02x N:%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x"
"%02x%02x%02x%02x%02x%02x\n",
db->macAddr[0],
db->macAddr[1],
db->macAddr[2],
db->macAddr[3],
db->macAddr[4],
db->macAddr[5],
db->networkAddr[0],
db->networkAddr[1],
db->networkAddr[2],
db->networkAddr[3],
db->networkAddr[4],
db->networkAddr[5],
db->networkAddr[6],
db->networkAddr[7],
db->networkAddr[8],
db->networkAddr[9],
db->networkAddr[10],
db->networkAddr[11],
db->networkAddr[12],
db->networkAddr[13],
db->networkAddr[14],
db->networkAddr[15],
db->networkAddr[16]);
#else
RTW_INFO("NAT25: Lookup M:%02x%02x%02x%02x%02x%02x N:%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n",
db->macAddr[0],
db->macAddr[1],
db->macAddr[2],
db->macAddr[3],
db->macAddr[4],
db->macAddr[5],
db->networkAddr[0],
db->networkAddr[1],
db->networkAddr[2],
db->networkAddr[3],
db->networkAddr[4],
db->networkAddr[5],
db->networkAddr[6],
db->networkAddr[7],
db->networkAddr[8],
db->networkAddr[9],
db->networkAddr[10]);
#endif
}
_exit_critical_bh(&priv->br_ext_lock, &irqL);
return 1;
}
db = db->next_hash;
}
_exit_critical_bh(&priv->br_ext_lock, &irqL);
return 0;
}
static void __nat25_db_network_insert(_adapter *priv,
unsigned char *macAddr, unsigned char *networkAddr)
{
struct nat25_network_db_entry *db;
int hash;
_irqL irqL;
_enter_critical_bh(&priv->br_ext_lock, &irqL);
hash = __nat25_network_hash(networkAddr);
db = priv->nethash[hash];
while (db != NULL) {
if (!memcmp(db->networkAddr, networkAddr, MAX_NETWORK_ADDR_LEN)) {
memcpy(db->macAddr, macAddr, ETH_ALEN);
db->ageing_timer = jiffies;
_exit_critical_bh(&priv->br_ext_lock, &irqL);
return;
}
db = db->next_hash;
}
db = (struct nat25_network_db_entry *) rtw_malloc(sizeof(*db));
if (db == NULL) {
_exit_critical_bh(&priv->br_ext_lock, &irqL);
return;
}
memcpy(db->networkAddr, networkAddr, MAX_NETWORK_ADDR_LEN);
memcpy(db->macAddr, macAddr, ETH_ALEN);
atomic_set(&db->use_count, 1);
db->ageing_timer = jiffies;
__network_hash_link(priv, db, hash);
_exit_critical_bh(&priv->br_ext_lock, &irqL);
}
static void __nat25_db_print(_adapter *priv)
{
_irqL irqL;
_enter_critical_bh(&priv->br_ext_lock, &irqL);
#ifdef BR_EXT_DEBUG
static int counter = 0;
int i, j;
struct nat25_network_db_entry *db;
counter++;
if ((counter % 16) != 0)
return;
for (i = 0, j = 0; i < NAT25_HASH_SIZE; i++) {
db = priv->nethash[i];
while (db != NULL) {
#ifdef CL_IPV6_PASS
panic_printk("NAT25: DB(%d) H(%02d) C(%d) M:%02x%02x%02x%02x%02x%02x N:%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x"
"%02x%02x%02x%02x%02x%02x\n",
j,
i,
atomic_read(&db->use_count),
db->macAddr[0],
db->macAddr[1],
db->macAddr[2],
db->macAddr[3],
db->macAddr[4],
db->macAddr[5],
db->networkAddr[0],
db->networkAddr[1],
db->networkAddr[2],
db->networkAddr[3],
db->networkAddr[4],
db->networkAddr[5],
db->networkAddr[6],
db->networkAddr[7],
db->networkAddr[8],
db->networkAddr[9],
db->networkAddr[10],
db->networkAddr[11],
db->networkAddr[12],
db->networkAddr[13],
db->networkAddr[14],
db->networkAddr[15],
db->networkAddr[16]);
#else
panic_printk("NAT25: DB(%d) H(%02d) C(%d) M:%02x%02x%02x%02x%02x%02x N:%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n",
j,
i,
atomic_read(&db->use_count),
db->macAddr[0],
db->macAddr[1],
db->macAddr[2],
db->macAddr[3],
db->macAddr[4],
db->macAddr[5],
db->networkAddr[0],
db->networkAddr[1],
db->networkAddr[2],
db->networkAddr[3],
db->networkAddr[4],
db->networkAddr[5],
db->networkAddr[6],
db->networkAddr[7],
db->networkAddr[8],
db->networkAddr[9],
db->networkAddr[10]);
#endif
j++;
db = db->next_hash;
}
}
#endif
_exit_critical_bh(&priv->br_ext_lock, &irqL);
}
/*
* NAT2.5 interface
*/
void nat25_db_cleanup(_adapter *priv)
{
int i;
_irqL irqL;
_enter_critical_bh(&priv->br_ext_lock, &irqL);
for (i = 0; i < NAT25_HASH_SIZE; i++) {
struct nat25_network_db_entry *f;
f = priv->nethash[i];
while (f != NULL) {
struct nat25_network_db_entry *g;
g = f->next_hash;
if (priv->scdb_entry == f) {
memset(priv->scdb_mac, 0, ETH_ALEN);
memset(priv->scdb_ip, 0, 4);
priv->scdb_entry = NULL;
}
__network_hash_unlink(f);
rtw_mfree((u8 *) f, sizeof(struct nat25_network_db_entry));
f = g;
}
}
_exit_critical_bh(&priv->br_ext_lock, &irqL);
}
void nat25_db_expire(_adapter *priv)
{
int i;
_irqL irqL;
_enter_critical_bh(&priv->br_ext_lock, &irqL);
/* if(!priv->ethBrExtInfo.nat25_disable) */
{
for (i = 0; i < NAT25_HASH_SIZE; i++) {
struct nat25_network_db_entry *f;
f = priv->nethash[i];
while (f != NULL) {
struct nat25_network_db_entry *g;
g = f->next_hash;
if (__nat25_has_expired(priv, f)) {
if (atomic_dec_and_test(&f->use_count)) {
#ifdef BR_EXT_DEBUG
#ifdef CL_IPV6_PASS
panic_printk("NAT25 Expire H(%02d) M:%02x%02x%02x%02x%02x%02x N:%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x"
"%02x%02x%02x%02x%02x%02x\n",
i,
f->macAddr[0],
f->macAddr[1],
f->macAddr[2],
f->macAddr[3],
f->macAddr[4],
f->macAddr[5],
f->networkAddr[0],
f->networkAddr[1],
f->networkAddr[2],
f->networkAddr[3],
f->networkAddr[4],
f->networkAddr[5],
f->networkAddr[6],
f->networkAddr[7],
f->networkAddr[8],
f->networkAddr[9],
f->networkAddr[10],
f->networkAddr[11],
f->networkAddr[12],
f->networkAddr[13],
f->networkAddr[14],
f->networkAddr[15],
f->networkAddr[16]);
#else
panic_printk("NAT25 Expire H(%02d) M:%02x%02x%02x%02x%02x%02x N:%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n",
i,
f->macAddr[0],
f->macAddr[1],
f->macAddr[2],
f->macAddr[3],
f->macAddr[4],
f->macAddr[5],
f->networkAddr[0],
f->networkAddr[1],
f->networkAddr[2],
f->networkAddr[3],
f->networkAddr[4],
f->networkAddr[5],
f->networkAddr[6],
f->networkAddr[7],
f->networkAddr[8],
f->networkAddr[9],
f->networkAddr[10]);
#endif
#endif
if (priv->scdb_entry == f) {
memset(priv->scdb_mac, 0, ETH_ALEN);
memset(priv->scdb_ip, 0, 4);
priv->scdb_entry = NULL;
}
__network_hash_unlink(f);
rtw_mfree((u8 *) f, sizeof(struct nat25_network_db_entry));
}
}
f = g;
}
}
}
_exit_critical_bh(&priv->br_ext_lock, &irqL);
}
#ifdef SUPPORT_TX_MCAST2UNI
static int checkIPMcAndReplace(_adapter *priv, struct sk_buff *skb, unsigned int *dst_ip)
{
struct stat_info *pstat;
struct list_head *phead, *plist;
int i;
phead = &priv->asoc_list;
plist = phead->next;
while (plist != phead) {
pstat = list_entry(plist, struct stat_info, asoc_list);
plist = plist->next;
if (pstat->ipmc_num == 0)
continue;
for (i = 0; i < MAX_IP_MC_ENTRY; i++) {
if (pstat->ipmc[i].used && !memcmp(&pstat->ipmc[i].mcmac[3], ((unsigned char *)dst_ip) + 1, 3)) {
memcpy(skb->data, pstat->ipmc[i].mcmac, ETH_ALEN);
return 1;
}
}
}
return 0;
}
#endif
int nat25_db_handle(_adapter *priv, struct sk_buff *skb, int method)
{
unsigned short protocol;
unsigned char networkAddr[MAX_NETWORK_ADDR_LEN];
if (skb == NULL)
return -1;
if ((method <= NAT25_MIN) || (method >= NAT25_MAX))
return -1;
protocol = *((unsigned short *)(skb->data + 2 * ETH_ALEN));
/*---------------------------------------------------*/
/* Handle IP frame */
/*---------------------------------------------------*/
if (protocol == __constant_htons(ETH_P_IP)) {
struct iphdr *iph = (struct iphdr *)(skb->data + ETH_HLEN);
if (((unsigned char *)(iph) + (iph->ihl << 2)) >= (skb->data + ETH_HLEN + skb->len)) {
DEBUG_WARN("NAT25: malformed IP packet !\n");
return -1;
}
switch (method) {
case NAT25_CHECK:
return -1;
case NAT25_INSERT: {
/* some muticast with source IP is all zero, maybe other case is illegal */
/* in class A, B, C, host address is all zero or all one is illegal */
if (iph->saddr == 0)
return 0;
RTW_INFO("NAT25: Insert IP, SA=%08x, DA=%08x\n", iph->saddr, iph->daddr);
__nat25_generate_ipv4_network_addr(networkAddr, &iph->saddr);
/* record source IP address and , source mac address into db */
__nat25_db_network_insert(priv, skb->data + ETH_ALEN, networkAddr);
__nat25_db_print(priv);
}
return 0;
case NAT25_LOOKUP: {
RTW_INFO("NAT25: Lookup IP, SA=%08x, DA=%08x\n", iph->saddr, iph->daddr);
#ifdef SUPPORT_TX_MCAST2UNI
if (priv->pshare->rf_ft_var.mc2u_disable ||
((((OPMODE & (WIFI_STATION_STATE | WIFI_ASOC_STATE))
== (WIFI_STATION_STATE | WIFI_ASOC_STATE)) &&
!checkIPMcAndReplace(priv, skb, &iph->daddr)) ||
(OPMODE & WIFI_ADHOC_STATE)))
#endif
{
__nat25_generate_ipv4_network_addr(networkAddr, &iph->daddr);
if (!__nat25_db_network_lookup_and_replace(priv, skb, networkAddr)) {
if (*((unsigned char *)&iph->daddr + 3) == 0xff) {
/* L2 is unicast but L3 is broadcast, make L2 bacome broadcast */
RTW_INFO("NAT25: Set DA as boardcast\n");
memset(skb->data, 0xff, ETH_ALEN);
} else {
/* forward unknow IP packet to upper TCP/IP */
RTW_INFO("NAT25: Replace DA with BR's MAC\n");
if ((*(u32 *)priv->br_mac) == 0 && (*(u16 *)(priv->br_mac + 4)) == 0) {
void netdev_br_init(struct net_device *netdev);
printk("Re-init netdev_br_init() due to br_mac==0!\n");
netdev_br_init(priv->pnetdev);
}
memcpy(skb->data, priv->br_mac, ETH_ALEN);
}
}
}
}
return 0;
default:
return -1;
}
}
/*---------------------------------------------------*/
/* Handle ARP frame */
/*---------------------------------------------------*/
else if (protocol == __constant_htons(ETH_P_ARP)) {
struct arphdr *arp = (struct arphdr *)(skb->data + ETH_HLEN);
unsigned char *arp_ptr = (unsigned char *)(arp + 1);
unsigned int *sender, *target;
if (arp->ar_pro != __constant_htons(ETH_P_IP)) {
DEBUG_WARN("NAT25: arp protocol unknown (%4x)!\n", htons(arp->ar_pro));
return -1;
}
switch (method) {
case NAT25_CHECK:
return 0; /* skb_copy for all ARP frame */
case NAT25_INSERT: {
RTW_INFO("NAT25: Insert ARP, MAC=%02x%02x%02x%02x%02x%02x\n", arp_ptr[0],
arp_ptr[1], arp_ptr[2], arp_ptr[3], arp_ptr[4], arp_ptr[5]);
/* change to ARP sender mac address to wlan STA address */
memcpy(arp_ptr, GET_MY_HWADDR(priv), ETH_ALEN);
arp_ptr += arp->ar_hln;
sender = (unsigned int *)arp_ptr;
__nat25_generate_ipv4_network_addr(networkAddr, sender);
__nat25_db_network_insert(priv, skb->data + ETH_ALEN, networkAddr);
__nat25_db_print(priv);
}
return 0;
case NAT25_LOOKUP: {
RTW_INFO("NAT25: Lookup ARP\n");
arp_ptr += arp->ar_hln;
sender = (unsigned int *)arp_ptr;
arp_ptr += (arp->ar_hln + arp->ar_pln);
target = (unsigned int *)arp_ptr;
__nat25_generate_ipv4_network_addr(networkAddr, target);
__nat25_db_network_lookup_and_replace(priv, skb, networkAddr);
/* change to ARP target mac address to Lookup result */
arp_ptr = (unsigned char *)(arp + 1);
arp_ptr += (arp->ar_hln + arp->ar_pln);
memcpy(arp_ptr, skb->data, ETH_ALEN);
}
return 0;
default:
return -1;
}
}
#if (LINUX_VERSION_CODE < KERNEL_VERSION(5, 15, 0))
/*---------------------------------------------------*/
/* Handle IPX and Apple Talk frame */
/*---------------------------------------------------*/
else if ((protocol == __constant_htons(ETH_P_IPX)) ||
(protocol == __constant_htons(ETH_P_ATALK)) ||
(protocol == __constant_htons(ETH_P_AARP))) {
unsigned char ipx_header[2] = {0xFF, 0xFF};
struct ipxhdr *ipx = NULL;
struct elapaarp *ea = NULL;
struct ddpehdr *ddp = NULL;
unsigned char *framePtr = skb->data + ETH_HLEN;
if (protocol == __constant_htons(ETH_P_IPX)) {
RTW_INFO("NAT25: Protocol=IPX (Ethernet II)\n");
ipx = (struct ipxhdr *)framePtr;
} else { /* if(protocol <= __constant_htons(ETH_FRAME_LEN)) */
if (!memcmp(ipx_header, framePtr, 2)) {
RTW_INFO("NAT25: Protocol=IPX (Ethernet 802.3)\n");
ipx = (struct ipxhdr *)framePtr;
} else {
unsigned char ipx_8022_type = 0xE0;
unsigned char snap_8022_type = 0xAA;
if (*framePtr == snap_8022_type) {
unsigned char ipx_snap_id[5] = {0x0, 0x0, 0x0, 0x81, 0x37}; /* IPX SNAP ID */
unsigned char aarp_snap_id[5] = {0x00, 0x00, 0x00, 0x80, 0xF3}; /* Apple Talk AARP SNAP ID */
unsigned char ddp_snap_id[5] = {0x08, 0x00, 0x07, 0x80, 0x9B}; /* Apple Talk DDP SNAP ID */
framePtr += 3; /* eliminate the 802.2 header */
if (!memcmp(ipx_snap_id, framePtr, 5)) {
framePtr += 5; /* eliminate the SNAP header */
RTW_INFO("NAT25: Protocol=IPX (Ethernet SNAP)\n");
ipx = (struct ipxhdr *)framePtr;
} else if (!memcmp(aarp_snap_id, framePtr, 5)) {
framePtr += 5; /* eliminate the SNAP header */
ea = (struct elapaarp *)framePtr;
} else if (!memcmp(ddp_snap_id, framePtr, 5)) {
framePtr += 5; /* eliminate the SNAP header */
ddp = (struct ddpehdr *)framePtr;
} else {
DEBUG_WARN("NAT25: Protocol=Ethernet SNAP %02x%02x%02x%02x%02x\n", framePtr[0],
framePtr[1], framePtr[2], framePtr[3], framePtr[4]);
return -1;
}
} else if (*framePtr == ipx_8022_type) {
framePtr += 3; /* eliminate the 802.2 header */
if (!memcmp(ipx_header, framePtr, 2)) {
RTW_INFO("NAT25: Protocol=IPX (Ethernet 802.2)\n");
ipx = (struct ipxhdr *)framePtr;
} else
return -1;
}
}
}
/* IPX */
if (ipx != NULL) {
switch (method) {
case NAT25_CHECK:
if (!memcmp(skb->data + ETH_ALEN, ipx->ipx_source.node, ETH_ALEN)) {
RTW_INFO("NAT25: Check IPX skb_copy\n");
return 0;
}
return -1;
case NAT25_INSERT: {
RTW_INFO("NAT25: Insert IPX, Dest=%08x,%02x%02x%02x%02x%02x%02x,%04x Source=%08x,%02x%02x%02x%02x%02x%02x,%04x\n",
ipx->ipx_dest.net,
ipx->ipx_dest.node[0],
ipx->ipx_dest.node[1],
ipx->ipx_dest.node[2],
ipx->ipx_dest.node[3],
ipx->ipx_dest.node[4],
ipx->ipx_dest.node[5],
ipx->ipx_dest.sock,
ipx->ipx_source.net,
ipx->ipx_source.node[0],
ipx->ipx_source.node[1],
ipx->ipx_source.node[2],
ipx->ipx_source.node[3],
ipx->ipx_source.node[4],
ipx->ipx_source.node[5],
ipx->ipx_source.sock);
if (!memcmp(skb->data + ETH_ALEN, ipx->ipx_source.node, ETH_ALEN)) {
RTW_INFO("NAT25: Use IPX Net, and Socket as network addr\n");
__nat25_generate_ipx_network_addr_with_socket(networkAddr, &ipx->ipx_source.net, &ipx->ipx_source.sock);
/* change IPX source node addr to wlan STA address */
memcpy(ipx->ipx_source.node, GET_MY_HWADDR(priv), ETH_ALEN);
} else
__nat25_generate_ipx_network_addr_with_node(networkAddr, &ipx->ipx_source.net, ipx->ipx_source.node);
__nat25_db_network_insert(priv, skb->data + ETH_ALEN, networkAddr);