Free Electrons

Embedded Linux Experts

   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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
/*
 *************************************************************************
 * Ralink Tech Inc.
 * 5F., No.36, Taiyuan St., Jhubei City,
 * Hsinchu County 302,
 * Taiwan, R.O.C.
 *
 * (c) Copyright 2002-2007, 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.             *
 *                                                                       *
 *************************************************************************

	Module Name:
	wpa.c

	Abstract:

	Revision History:
	Who			When			What
	--------	----------		----------------------------------------------
	Jan	Lee		03-07-22		Initial
	Paul Lin	03-11-28		Modify for supplicant
*/
#include "../rt_config.h"
/* WPA OUI */
u8 OUI_WPA_NONE_AKM[4] = { 0x00, 0x50, 0xF2, 0x00 };
u8 OUI_WPA_VERSION[4] = { 0x00, 0x50, 0xF2, 0x01 };
u8 OUI_WPA_WEP40[4] = { 0x00, 0x50, 0xF2, 0x01 };
u8 OUI_WPA_TKIP[4] = { 0x00, 0x50, 0xF2, 0x02 };
u8 OUI_WPA_CCMP[4] = { 0x00, 0x50, 0xF2, 0x04 };
u8 OUI_WPA_WEP104[4] = { 0x00, 0x50, 0xF2, 0x05 };
u8 OUI_WPA_8021X_AKM[4] = { 0x00, 0x50, 0xF2, 0x01 };
u8 OUI_WPA_PSK_AKM[4] = { 0x00, 0x50, 0xF2, 0x02 };

/* WPA2 OUI */
u8 OUI_WPA2_WEP40[4] = { 0x00, 0x0F, 0xAC, 0x01 };
u8 OUI_WPA2_TKIP[4] = { 0x00, 0x0F, 0xAC, 0x02 };
u8 OUI_WPA2_CCMP[4] = { 0x00, 0x0F, 0xAC, 0x04 };
u8 OUI_WPA2_8021X_AKM[4] = { 0x00, 0x0F, 0xAC, 0x01 };
u8 OUI_WPA2_PSK_AKM[4] = { 0x00, 0x0F, 0xAC, 0x02 };
u8 OUI_WPA2_WEP104[4] = { 0x00, 0x0F, 0xAC, 0x05 };

static void ConstructEapolKeyData(struct rt_mac_table_entry *pEntry,
				  u8 GroupKeyWepStatus,
				  u8 keyDescVer,
				  u8 MsgType,
				  u8 DefaultKeyIdx,
				  u8 * GTK,
				  u8 * RSNIE,
				  u8 RSNIE_LEN, struct rt_eapol_packet * pMsg);

static void CalculateMIC(u8 KeyDescVer,
			 u8 * PTK, struct rt_eapol_packet * pMsg);

static void WpaEAPPacketAction(struct rt_rtmp_adapter *pAd, struct rt_mlme_queue_elem *Elem);

static void WpaEAPOLASFAlertAction(struct rt_rtmp_adapter *pAd,
				   struct rt_mlme_queue_elem *Elem);

static void WpaEAPOLLogoffAction(struct rt_rtmp_adapter *pAd,
				 struct rt_mlme_queue_elem *Elem);

static void WpaEAPOLStartAction(struct rt_rtmp_adapter *pAd,
				struct rt_mlme_queue_elem *Elem);

static void WpaEAPOLKeyAction(struct rt_rtmp_adapter *pAd, struct rt_mlme_queue_elem *Elem);

/*
    ==========================================================================
    Description:
        association state machine init, including state transition and timer init
    Parameters:
        S - pointer to the association state machine
    ==========================================================================
 */
void WpaStateMachineInit(struct rt_rtmp_adapter *pAd,
			 struct rt_state_machine *S, OUT STATE_MACHINE_FUNC Trans[])
{
	StateMachineInit(S, (STATE_MACHINE_FUNC *) Trans, MAX_WPA_PTK_STATE,
			 MAX_WPA_MSG, (STATE_MACHINE_FUNC) Drop, WPA_PTK,
			 WPA_MACHINE_BASE);

	StateMachineSetAction(S, WPA_PTK, MT2_EAPPacket,
			      (STATE_MACHINE_FUNC) WpaEAPPacketAction);
	StateMachineSetAction(S, WPA_PTK, MT2_EAPOLStart,
			      (STATE_MACHINE_FUNC) WpaEAPOLStartAction);
	StateMachineSetAction(S, WPA_PTK, MT2_EAPOLLogoff,
			      (STATE_MACHINE_FUNC) WpaEAPOLLogoffAction);
	StateMachineSetAction(S, WPA_PTK, MT2_EAPOLKey,
			      (STATE_MACHINE_FUNC) WpaEAPOLKeyAction);
	StateMachineSetAction(S, WPA_PTK, MT2_EAPOLASFAlert,
			      (STATE_MACHINE_FUNC) WpaEAPOLASFAlertAction);
}

/*
    ==========================================================================
    Description:
        this is state machine function.
        When receiving EAP packets which is  for 802.1x authentication use.
        Not use in PSK case
    Return:
    ==========================================================================
*/
void WpaEAPPacketAction(struct rt_rtmp_adapter *pAd, struct rt_mlme_queue_elem *Elem)
{
}

void WpaEAPOLASFAlertAction(struct rt_rtmp_adapter *pAd, struct rt_mlme_queue_elem *Elem)
{
}

void WpaEAPOLLogoffAction(struct rt_rtmp_adapter *pAd, struct rt_mlme_queue_elem *Elem)
{
}

/*
    ==========================================================================
    Description:
       Start 4-way HS when rcv EAPOL_START which may create by our driver in assoc.c
    Return:
    ==========================================================================
*/
void WpaEAPOLStartAction(struct rt_rtmp_adapter *pAd, struct rt_mlme_queue_elem *Elem)
{
	struct rt_mac_table_entry *pEntry;
	struct rt_header_802_11 * pHeader;

	DBGPRINT(RT_DEBUG_TRACE, ("WpaEAPOLStartAction ===> \n"));

	pHeader = (struct rt_header_802_11 *) Elem->Msg;

	/*For normaol PSK, we enqueue an EAPOL-Start command to trigger the process. */
	if (Elem->MsgLen == 6)
		pEntry = MacTableLookup(pAd, Elem->Msg);
	else {
		pEntry = MacTableLookup(pAd, pHeader->Addr2);
	}

	if (pEntry) {
		DBGPRINT(RT_DEBUG_TRACE,
			 (" PortSecured(%d), WpaState(%d), AuthMode(%d), PMKID_CacheIdx(%d) \n",
			  pEntry->PortSecured, pEntry->WpaState,
			  pEntry->AuthMode, pEntry->PMKID_CacheIdx));

		if ((pEntry->PortSecured == WPA_802_1X_PORT_NOT_SECURED)
		    && (pEntry->WpaState < AS_PTKSTART)
		    && ((pEntry->AuthMode == Ndis802_11AuthModeWPAPSK)
			|| (pEntry->AuthMode == Ndis802_11AuthModeWPA2PSK)
			|| ((pEntry->AuthMode == Ndis802_11AuthModeWPA2)
			    && (pEntry->PMKID_CacheIdx != ENTRY_NOT_FOUND)))) {
			pEntry->PrivacyFilter = Ndis802_11PrivFilter8021xWEP;
			pEntry->WpaState = AS_INITPSK;
			pEntry->PortSecured = WPA_802_1X_PORT_NOT_SECURED;
			NdisZeroMemory(pEntry->R_Counter,
				       sizeof(pEntry->R_Counter));
			pEntry->ReTryCounter = PEER_MSG1_RETRY_TIMER_CTR;

			WPAStart4WayHS(pAd, pEntry, PEER_MSG1_RETRY_EXEC_INTV);
		}
	}
}

/*
    ==========================================================================
    Description:
        This is state machine function.
        When receiving EAPOL packets which is  for 802.1x key management.
        Use both in WPA, and WPAPSK case.
        In this function, further dispatch to different functions according to the received packet.  3 categories are :
          1.  normal 4-way pairwisekey and 2-way groupkey handshake
          2.  MIC error (Countermeasures attack)  report packet from STA.
          3.  Request for pairwise/group key update from STA
    Return:
    ==========================================================================
*/
void WpaEAPOLKeyAction(struct rt_rtmp_adapter *pAd, struct rt_mlme_queue_elem *Elem)
{
	struct rt_mac_table_entry *pEntry;
	struct rt_header_802_11 * pHeader;
	struct rt_eapol_packet * pEapol_packet;
	struct rt_key_info peerKeyInfo;

	DBGPRINT(RT_DEBUG_TRACE, ("WpaEAPOLKeyAction ===>\n"));

	pHeader = (struct rt_header_802_11 *) Elem->Msg;
	pEapol_packet =
	    (struct rt_eapol_packet *) & Elem->Msg[LENGTH_802_11 + LENGTH_802_1_H];

	NdisZeroMemory((u8 *)& peerKeyInfo, sizeof(peerKeyInfo));
	NdisMoveMemory((u8 *)& peerKeyInfo,
		       (u8 *)& pEapol_packet->KeyDesc.KeyInfo,
		       sizeof(struct rt_key_info));

	hex_dump("Received Eapol frame", (unsigned char *)pEapol_packet,
		 (Elem->MsgLen - LENGTH_802_11 - LENGTH_802_1_H));

	*((u16 *) & peerKeyInfo) = cpu2le16(*((u16 *) & peerKeyInfo));

	do {
		pEntry = MacTableLookup(pAd, pHeader->Addr2);

		if (!pEntry
		    || ((!pEntry->ValidAsCLI) && (!pEntry->ValidAsApCli)))
			break;

		if (pEntry->AuthMode < Ndis802_11AuthModeWPA)
			break;

		DBGPRINT(RT_DEBUG_TRACE,
			("Receive EAPoL-Key frame from STA %pMF\n",
				pEntry->Addr));

		if (((pEapol_packet->ProVer != EAPOL_VER)
		     && (pEapol_packet->ProVer != EAPOL_VER2))
		    || ((pEapol_packet->KeyDesc.Type != WPA1_KEY_DESC)
			&& (pEapol_packet->KeyDesc.Type != WPA2_KEY_DESC))) {
			DBGPRINT(RT_DEBUG_ERROR,
				 ("Key descripter does not match with WPA rule\n"));
			break;
		}
		/* The value 1 shall be used for all EAPOL-Key frames to and from a STA when */
		/* neither the group nor pairwise ciphers are CCMP for Key Descriptor 1. */
		if ((pEntry->WepStatus == Ndis802_11Encryption2Enabled)
		    && (peerKeyInfo.KeyDescVer != DESC_TYPE_TKIP)) {
			DBGPRINT(RT_DEBUG_ERROR,
				 ("Key descripter version not match(TKIP) \n"));
			break;
		}
		/* The value 2 shall be used for all EAPOL-Key frames to and from a STA when */
		/* either the pairwise or the group cipher is AES-CCMP for Key Descriptor 2. */
		else if ((pEntry->WepStatus == Ndis802_11Encryption3Enabled)
			 && (peerKeyInfo.KeyDescVer != DESC_TYPE_AES)) {
			DBGPRINT(RT_DEBUG_ERROR,
				 ("Key descripter version not match(AES) \n"));
			break;
		}
		/* Check if this STA is in class 3 state and the WPA state is started */
		if ((pEntry->Sst == SST_ASSOC)
		    && (pEntry->WpaState >= AS_INITPSK)) {
			/* Check the Key Ack (bit 7) of the Key Information to determine the Authenticator */
			/* or not. */
			/* An EAPOL-Key frame that is sent by the Supplicant in response to an EAPOL- */
			/* Key frame from the Authenticator must not have the Ack bit set. */
			if (peerKeyInfo.KeyAck == 1) {
				/* The frame is snet by Authenticator. */
				/* So the Supplicant side shall handle this. */

				if ((peerKeyInfo.Secure == 0)
				    && (peerKeyInfo.Request == 0)
				    && (peerKeyInfo.Error == 0)
				    && (peerKeyInfo.KeyType == PAIRWISEKEY)) {
					/* Process 1. the message 1 of 4-way HS in WPA or WPA2 */
					/*                        EAPOL-Key(0,0,1,0,P,0,0,ANonce,0,DataKD_M1) */
					/*                 2. the message 3 of 4-way HS in WPA */
					/*                        EAPOL-Key(0,1,1,1,P,0,KeyRSC,ANonce,MIC,DataKD_M3) */
					if (peerKeyInfo.KeyMic == 0)
						PeerPairMsg1Action(pAd, pEntry,
								   Elem);
					else
						PeerPairMsg3Action(pAd, pEntry,
								   Elem);
				} else if ((peerKeyInfo.Secure == 1)
					   && (peerKeyInfo.KeyMic == 1)
					   && (peerKeyInfo.Request == 0)
					   && (peerKeyInfo.Error == 0)) {
					/* Process 1. the message 3 of 4-way HS in WPA2 */
					/*                        EAPOL-Key(1,1,1,1,P,0,KeyRSC,ANonce,MIC,DataKD_M3) */
					/*                 2. the message 1 of group KS in WPA or WPA2 */
					/*                        EAPOL-Key(1,1,1,0,G,0,Key RSC,0, MIC,GTK[N]) */
					if (peerKeyInfo.KeyType == PAIRWISEKEY)
						PeerPairMsg3Action(pAd, pEntry,
								   Elem);
					else
						PeerGroupMsg1Action(pAd, pEntry,
								    Elem);
				}
			} else {
				/* The frame is snet by Supplicant. */
				/* So the Authenticator side shall handle this. */
				if ((peerKeyInfo.Request == 0) &&
				    (peerKeyInfo.Error == 0) &&
				    (peerKeyInfo.KeyMic == 1)) {
					if (peerKeyInfo.Secure == 0
					    && peerKeyInfo.KeyType ==
					    PAIRWISEKEY) {
						/* EAPOL-Key(0,1,0,0,P,0,0,SNonce,MIC,Data) */
						/* Process 1. message 2 of 4-way HS in WPA or WPA2 */
						/*                 2. message 4 of 4-way HS in WPA */
						if (CONV_ARRARY_TO_u16
						    (pEapol_packet->KeyDesc.
						     KeyDataLen) == 0) {
							PeerPairMsg4Action(pAd,
									   pEntry,
									   Elem);
						} else {
							PeerPairMsg2Action(pAd,
									   pEntry,
									   Elem);
						}
					} else if (peerKeyInfo.Secure == 1
						   && peerKeyInfo.KeyType ==
						   PAIRWISEKEY) {
						/* EAPOL-Key(1,1,0,0,P,0,0,0,MIC,0) */
						/* Process message 4 of 4-way HS in WPA2 */
						PeerPairMsg4Action(pAd, pEntry,
								   Elem);
					} else if (peerKeyInfo.Secure == 1
						   && peerKeyInfo.KeyType ==
						   GROUPKEY) {
						/* EAPOL-Key(1,1,0,0,G,0,0,0,MIC,0) */
						/* Process message 2 of Group key HS in WPA or WPA2 */
						PeerGroupMsg2Action(pAd, pEntry,
								    &Elem->
								    Msg
								    [LENGTH_802_11],
								    (Elem->
								     MsgLen -
								     LENGTH_802_11));
					}
				}
			}
		}
	} while (FALSE);
}

/*
	========================================================================

	Routine	Description:
		Copy frame from waiting queue into relative ring buffer and set
	appropriate ASIC register to kick hardware encryption before really
	sent out to air.

	Arguments:
		pAd		Pointer	to our adapter
		void *	Pointer to outgoing Ndis frame
		NumberOfFrag	Number of fragment required

	Return Value:
		None

	Note:

	========================================================================
*/
void RTMPToWirelessSta(struct rt_rtmp_adapter *pAd,
		       struct rt_mac_table_entry *pEntry,
		       u8 *pHeader802_3,
		       u32 HdrLen,
		       u8 *pData, u32 DataLen, IN BOOLEAN bClearFrame)
{
	void *pPacket;
	int Status;

	if ((!pEntry) || ((!pEntry->ValidAsCLI) && (!pEntry->ValidAsApCli)))
		return;

	do {
		/* build a NDIS packet */
		Status =
		    RTMPAllocateNdisPacket(pAd, &pPacket, pHeader802_3, HdrLen,
					   pData, DataLen);
		if (Status != NDIS_STATUS_SUCCESS)
			break;

		if (bClearFrame)
			RTMP_SET_PACKET_CLEAR_EAP_FRAME(pPacket, 1);
		else
			RTMP_SET_PACKET_CLEAR_EAP_FRAME(pPacket, 0);
		{
			RTMP_SET_PACKET_SOURCE(pPacket, PKTSRC_NDIS);

			RTMP_SET_PACKET_NET_DEVICE_MBSSID(pPacket, MAIN_MBSSID);	/* set a default value */
			if (pEntry->apidx != 0)
				RTMP_SET_PACKET_NET_DEVICE_MBSSID(pPacket,
								  pEntry->
								  apidx);

			RTMP_SET_PACKET_WCID(pPacket, (u8)pEntry->Aid);
			RTMP_SET_PACKET_MOREDATA(pPacket, FALSE);
		}

		{
			/* send out the packet */
			Status = STASendPacket(pAd, pPacket);
			if (Status == NDIS_STATUS_SUCCESS) {
				u8 Index;

				/* Dequeue one frame from TxSwQueue0..3 queue and process it */
				/* There are three place calling dequeue for TX ring. */
				/* 1. Here, right after queueing the frame. */
				/* 2. At the end of TxRingTxDone service routine. */
				/* 3. Upon NDIS call RTMPSendPackets */
				if ((!RTMP_TEST_FLAG
				     (pAd, fRTMP_ADAPTER_BSS_SCAN_IN_PROGRESS))
				    &&
				    (!RTMP_TEST_FLAG
				     (pAd, fRTMP_ADAPTER_RESET_IN_PROGRESS))) {
					for (Index = 0; Index < 5; Index++)
						if (pAd->TxSwQueue[Index].
						    Number > 0)
							RTMPDeQueuePacket(pAd,
									  FALSE,
									  Index,
									  MAX_TX_PROCESS);
				}
			}
		}

	} while (FALSE);
}

/*
    ==========================================================================
    Description:
        This is a function to initialize 4-way handshake

    Return:

    ==========================================================================
*/
void WPAStart4WayHS(struct rt_rtmp_adapter *pAd,
		    struct rt_mac_table_entry *pEntry, unsigned long TimeInterval)
{
	u8 Header802_3[14];
	struct rt_eapol_packet EAPOLPKT;
	u8 *pBssid = NULL;
	u8 group_cipher = Ndis802_11WEPDisabled;

	DBGPRINT(RT_DEBUG_TRACE, ("===> WPAStart4WayHS\n"));

	if (RTMP_TEST_FLAG
	    (pAd,
	     fRTMP_ADAPTER_RESET_IN_PROGRESS | fRTMP_ADAPTER_HALT_IN_PROGRESS))
	{
		DBGPRINT(RT_DEBUG_ERROR,
			 ("[ERROR]WPAStart4WayHS : The interface is closed...\n"));
		return;
	}

	if (pBssid == NULL) {
		DBGPRINT(RT_DEBUG_ERROR,
			 ("[ERROR]WPAStart4WayHS : No corresponding Authenticator.\n"));
		return;
	}
	/* Check the status */
	if ((pEntry->WpaState > AS_PTKSTART) || (pEntry->WpaState < AS_INITPMK)) {
		DBGPRINT(RT_DEBUG_ERROR,
			 ("[ERROR]WPAStart4WayHS : Not expect calling\n"));
		return;
	}

	/* Increment replay counter by 1 */
	ADD_ONE_To_64BIT_VAR(pEntry->R_Counter);

	/* Randomly generate ANonce */
	GenRandom(pAd, (u8 *) pBssid, pEntry->ANonce);

	/* Construct EAPoL message - Pairwise Msg 1 */
	/* EAPOL-Key(0,0,1,0,P,0,0,ANonce,0,DataKD_M1) */
	NdisZeroMemory(&EAPOLPKT, sizeof(struct rt_eapol_packet));
	ConstructEapolMsg(pEntry, group_cipher, EAPOL_PAIR_MSG_1, 0,	/* Default key index */
			  pEntry->ANonce, NULL,	/* TxRSC */
			  NULL,	/* GTK */
			  NULL,	/* RSNIE */
			  0,	/* RSNIE length */
			  &EAPOLPKT);

	/* Make outgoing frame */
	MAKE_802_3_HEADER(Header802_3, pEntry->Addr, pBssid, EAPOL);
	RTMPToWirelessSta(pAd, pEntry, Header802_3,
			  LENGTH_802_3, (u8 *)& EAPOLPKT,
			  CONV_ARRARY_TO_u16(EAPOLPKT.Body_Len) + 4,
			  (pEntry->PortSecured ==
			   WPA_802_1X_PORT_SECURED) ? FALSE : TRUE);

	/* Trigger Retry Timer */
	RTMPModTimer(&pEntry->RetryTimer, TimeInterval);

	/* Update State */
	pEntry->WpaState = AS_PTKSTART;

	DBGPRINT(RT_DEBUG_TRACE,
		 ("<=== WPAStart4WayHS: send Msg1 of 4-way \n"));

}

/*
	========================================================================

	Routine Description:
		Process Pairwise key Msg-1 of 4-way handshaking and send Msg-2

	Arguments:
		pAd			Pointer	to our adapter
		Elem		Message body

	Return Value:
		None

	Note:

	========================================================================
*/
void PeerPairMsg1Action(struct rt_rtmp_adapter *pAd,
			struct rt_mac_table_entry *pEntry, struct rt_mlme_queue_elem *Elem)
{
	u8 PTK[80];
	u8 Header802_3[14];
	struct rt_eapol_packet * pMsg1;
	u32 MsgLen;
	struct rt_eapol_packet EAPOLPKT;
	u8 *pCurrentAddr = NULL;
	u8 *pmk_ptr = NULL;
	u8 group_cipher = Ndis802_11WEPDisabled;
	u8 *rsnie_ptr = NULL;
	u8 rsnie_len = 0;

	DBGPRINT(RT_DEBUG_TRACE, ("===> PeerPairMsg1Action \n"));

	if ((!pEntry) || ((!pEntry->ValidAsCLI) && (!pEntry->ValidAsApCli)))
		return;

	if (Elem->MsgLen <
	    (LENGTH_802_11 + LENGTH_802_1_H + LENGTH_EAPOL_H +
	     sizeof(struct rt_key_descripter) - MAX_LEN_OF_RSNIE - 2))
		return;

	{
		pCurrentAddr = pAd->CurrentAddress;
		pmk_ptr = pAd->StaCfg.PMK;
		group_cipher = pAd->StaCfg.GroupCipher;
		rsnie_ptr = pAd->StaCfg.RSN_IE;
		rsnie_len = pAd->StaCfg.RSNIE_Len;
	}

	/* Store the received frame */
	pMsg1 = (struct rt_eapol_packet *) & Elem->Msg[LENGTH_802_11 + LENGTH_802_1_H];
	MsgLen = Elem->MsgLen - LENGTH_802_11 - LENGTH_802_1_H;

	/* Sanity Check peer Pairwise message 1 - Replay Counter */
	if (PeerWpaMessageSanity(pAd, pMsg1, MsgLen, EAPOL_PAIR_MSG_1, pEntry)
	    == FALSE)
		return;

	/* Store Replay counter, it will use to verify message 3 and construct message 2 */
	NdisMoveMemory(pEntry->R_Counter, pMsg1->KeyDesc.ReplayCounter,
		       LEN_KEY_DESC_REPLAY);

	/* Store ANonce */
	NdisMoveMemory(pEntry->ANonce, pMsg1->KeyDesc.KeyNonce,
		       LEN_KEY_DESC_NONCE);

	/* Generate random SNonce */
	GenRandom(pAd, (u8 *) pCurrentAddr, pEntry->SNonce);

	{
		/* Calculate PTK(ANonce, SNonce) */
		WpaDerivePTK(pAd,
			     pmk_ptr,
			     pEntry->ANonce,
			     pEntry->Addr,
			     pEntry->SNonce, pCurrentAddr, PTK, LEN_PTK);

		/* Save key to PTK entry */
		NdisMoveMemory(pEntry->PTK, PTK, LEN_PTK);
	}

	/* Update WpaState */
	pEntry->WpaState = AS_PTKINIT_NEGOTIATING;

	/* Construct EAPoL message - Pairwise Msg 2 */
	/*  EAPOL-Key(0,1,0,0,P,0,0,SNonce,MIC,DataKD_M2) */
	NdisZeroMemory(&EAPOLPKT, sizeof(struct rt_eapol_packet));
	ConstructEapolMsg(pEntry, group_cipher, EAPOL_PAIR_MSG_2, 0,	/* DefaultKeyIdx */
			  pEntry->SNonce, NULL,	/* TxRsc */
			  NULL,	/* GTK */
			  (u8 *) rsnie_ptr, rsnie_len, &EAPOLPKT);

	/* Make outgoing frame */
	MAKE_802_3_HEADER(Header802_3, pEntry->Addr, pCurrentAddr, EAPOL);

	RTMPToWirelessSta(pAd, pEntry,
			  Header802_3, sizeof(Header802_3), (u8 *)& EAPOLPKT,
			  CONV_ARRARY_TO_u16(EAPOLPKT.Body_Len) + 4, TRUE);

	DBGPRINT(RT_DEBUG_TRACE,
		 ("<=== PeerPairMsg1Action: send Msg2 of 4-way \n"));
}

/*
    ==========================================================================
    Description:
        When receiving the second packet of 4-way pairwisekey handshake.
    Return:
    ==========================================================================
*/
void PeerPairMsg2Action(struct rt_rtmp_adapter *pAd,
			struct rt_mac_table_entry *pEntry, struct rt_mlme_queue_elem *Elem)
{
	u8 PTK[80];
	BOOLEAN Cancelled;
	struct rt_header_802_11 * pHeader;
	struct rt_eapol_packet EAPOLPKT;
	struct rt_eapol_packet * pMsg2;
	u32 MsgLen;
	u8 Header802_3[LENGTH_802_3];
	u8 TxTsc[6];
	u8 *pBssid = NULL;
	u8 *pmk_ptr = NULL;
	u8 *gtk_ptr = NULL;
	u8 default_key = 0;
	u8 group_cipher = Ndis802_11WEPDisabled;
	u8 *rsnie_ptr = NULL;
	u8 rsnie_len = 0;

	DBGPRINT(RT_DEBUG_TRACE, ("===> PeerPairMsg2Action \n"));

	if ((!pEntry) || (!pEntry->ValidAsCLI))
		return;

	if (Elem->MsgLen <
	    (LENGTH_802_11 + LENGTH_802_1_H + LENGTH_EAPOL_H +
	     sizeof(struct rt_key_descripter) - MAX_LEN_OF_RSNIE - 2))
		return;

	/* check Entry in valid State */
	if (pEntry->WpaState < AS_PTKSTART)
		return;

	/* pointer to 802.11 header */
	pHeader = (struct rt_header_802_11 *) Elem->Msg;

	/* skip 802.11_header(24-byte) and LLC_header(8) */
	pMsg2 = (struct rt_eapol_packet *) & Elem->Msg[LENGTH_802_11 + LENGTH_802_1_H];
	MsgLen = Elem->MsgLen - LENGTH_802_11 - LENGTH_802_1_H;

	/* Store SNonce */
	NdisMoveMemory(pEntry->SNonce, pMsg2->KeyDesc.KeyNonce,
		       LEN_KEY_DESC_NONCE);

	{
		/* Derive PTK */
		WpaDerivePTK(pAd, (u8 *) pmk_ptr, pEntry->ANonce,	/* ANONCE */
			     (u8 *) pBssid, pEntry->SNonce,	/* SNONCE */
			     pEntry->Addr, PTK, LEN_PTK);

		NdisMoveMemory(pEntry->PTK, PTK, LEN_PTK);
	}

	/* Sanity Check peer Pairwise message 2 - Replay Counter, MIC, RSNIE */
	if (PeerWpaMessageSanity(pAd, pMsg2, MsgLen, EAPOL_PAIR_MSG_2, pEntry)
	    == FALSE)
		return;

	do {
		/* delete retry timer */
		RTMPCancelTimer(&pEntry->RetryTimer, &Cancelled);

		/* Change state */
		pEntry->WpaState = AS_PTKINIT_NEGOTIATING;

		/* Increment replay counter by 1 */
		ADD_ONE_To_64BIT_VAR(pEntry->R_Counter);

		/* Construct EAPoL message - Pairwise Msg 3 */
		NdisZeroMemory(&EAPOLPKT, sizeof(struct rt_eapol_packet));
		ConstructEapolMsg(pEntry,
				  group_cipher,
				  EAPOL_PAIR_MSG_3,
				  default_key,
				  pEntry->ANonce,
				  TxTsc,
				  (u8 *) gtk_ptr,
				  (u8 *) rsnie_ptr, rsnie_len, &EAPOLPKT);

		/* Make outgoing frame */
		MAKE_802_3_HEADER(Header802_3, pEntry->Addr, pBssid, EAPOL);
		RTMPToWirelessSta(pAd, pEntry, Header802_3, LENGTH_802_3,
				  (u8 *)& EAPOLPKT,
				  CONV_ARRARY_TO_u16(EAPOLPKT.Body_Len) + 4,
				  (pEntry->PortSecured ==
				   WPA_802_1X_PORT_SECURED) ? FALSE : TRUE);

		pEntry->ReTryCounter = PEER_MSG3_RETRY_TIMER_CTR;
		RTMPSetTimer(&pEntry->RetryTimer, PEER_MSG3_RETRY_EXEC_INTV);

		/* Update State */
		pEntry->WpaState = AS_PTKINIT_NEGOTIATING;
	} while (FALSE);

	DBGPRINT(RT_DEBUG_TRACE,
		 ("<=== PeerPairMsg2Action: send Msg3 of 4-way \n"));
}

/*
	========================================================================

	Routine Description:
		Process Pairwise key Msg 3 of 4-way handshaking and send Msg 4

	Arguments:
		pAd	Pointer	to our adapter
		Elem		Message body

	Return Value:
		None

	Note:

	========================================================================
*/
void PeerPairMsg3Action(struct rt_rtmp_adapter *pAd,
			struct rt_mac_table_entry *pEntry, struct rt_mlme_queue_elem *Elem)
{
	struct rt_header_802_11 * pHeader;
	u8 Header802_3[14];
	struct rt_eapol_packet EAPOLPKT;
	struct rt_eapol_packet * pMsg3;
	u32 MsgLen;
	u8 *pCurrentAddr = NULL;
	u8 group_cipher = Ndis802_11WEPDisabled;

	DBGPRINT(RT_DEBUG_TRACE, ("===> PeerPairMsg3Action \n"));

	if ((!pEntry) || ((!pEntry->ValidAsCLI) && (!pEntry->ValidAsApCli)))
		return;

	if (Elem->MsgLen <
	    (LENGTH_802_11 + LENGTH_802_1_H + LENGTH_EAPOL_H +
	     sizeof(struct rt_key_descripter) - MAX_LEN_OF_RSNIE - 2))
		return;

	{
		pCurrentAddr = pAd->CurrentAddress;
		group_cipher = pAd->StaCfg.GroupCipher;

	}

	/* Record 802.11 header & the received EAPOL packet Msg3 */
	pHeader = (struct rt_header_802_11 *) Elem->Msg;
	pMsg3 = (struct rt_eapol_packet *) & Elem->Msg[LENGTH_802_11 + LENGTH_802_1_H];
	MsgLen = Elem->MsgLen - LENGTH_802_11 - LENGTH_802_1_H;

	/* Sanity Check peer Pairwise message 3 - Replay Counter, MIC, RSNIE */
	if (PeerWpaMessageSanity(pAd, pMsg3, MsgLen, EAPOL_PAIR_MSG_3, pEntry)
	    == FALSE)
		return;

	/* Save Replay counter, it will use construct message 4 */
	NdisMoveMemory(pEntry->R_Counter, pMsg3->KeyDesc.ReplayCounter,
		       LEN_KEY_DESC_REPLAY);

	/* Double check ANonce */
	if (!NdisEqualMemory
	    (pEntry->ANonce, pMsg3->KeyDesc.KeyNonce, LEN_KEY_DESC_NONCE)) {
		return;
	}
	/* Construct EAPoL message - Pairwise Msg 4 */
	NdisZeroMemory(&EAPOLPKT, sizeof(struct rt_eapol_packet));
	ConstructEapolMsg(pEntry, group_cipher, EAPOL_PAIR_MSG_4, 0,	/* group key index not used in message 4 */
			  NULL,	/* Nonce not used in message 4 */
			  NULL,	/* TxRSC not used in message 4 */
			  NULL,	/* GTK not used in message 4 */
			  NULL,	/* RSN IE not used in message 4 */
			  0, &EAPOLPKT);

	/* Update WpaState */
	pEntry->WpaState = AS_PTKINITDONE;

	/* Update pairwise key */
	{
		struct rt_cipher_key *pSharedKey;

		pSharedKey = &pAd->SharedKey[BSS0][0];

		NdisMoveMemory(pAd->StaCfg.PTK, pEntry->PTK, LEN_PTK);

		/* Prepare pair-wise key information into shared key table */
		NdisZeroMemory(pSharedKey, sizeof(struct rt_cipher_key));
		pSharedKey->KeyLen = LEN_TKIP_EK;
		NdisMoveMemory(pSharedKey->Key, &pAd->StaCfg.PTK[32],
			       LEN_TKIP_EK);
		NdisMoveMemory(pSharedKey->RxMic, &pAd->StaCfg.PTK[48],
			       LEN_TKIP_RXMICK);
		NdisMoveMemory(pSharedKey->TxMic,
			       &pAd->StaCfg.PTK[48 + LEN_TKIP_RXMICK],
			       LEN_TKIP_TXMICK);

		/* Decide its ChiperAlg */
		if (pAd->StaCfg.PairCipher == Ndis802_11Encryption2Enabled)
			pSharedKey->CipherAlg = CIPHER_TKIP;
		else if (pAd->StaCfg.PairCipher == Ndis802_11Encryption3Enabled)
			pSharedKey->CipherAlg = CIPHER_AES;
		else
			pSharedKey->CipherAlg = CIPHER_NONE;

		/* Update these related information to struct rt_mac_table_entry */
		pEntry = &pAd->MacTab.Content[BSSID_WCID];
		NdisMoveMemory(pEntry->PairwiseKey.Key, &pAd->StaCfg.PTK[32],
			       LEN_TKIP_EK);
		NdisMoveMemory(pEntry->PairwiseKey.RxMic, &pAd->StaCfg.PTK[48],
			       LEN_TKIP_RXMICK);
		NdisMoveMemory(pEntry->PairwiseKey.TxMic,
			       &pAd->StaCfg.PTK[48 + LEN_TKIP_RXMICK],
			       LEN_TKIP_TXMICK);
		pEntry->PairwiseKey.CipherAlg = pSharedKey->CipherAlg;

		/* Update pairwise key information to ASIC Shared Key Table */
		AsicAddSharedKeyEntry(pAd,
				      BSS0,
				      0,
				      pSharedKey->CipherAlg,
				      pSharedKey->Key,
				      pSharedKey->TxMic, pSharedKey->RxMic);

		/* Update ASIC WCID attribute table and IVEIV table */
		RTMPAddWcidAttributeEntry(pAd,
					  BSS0,
					  0, pSharedKey->CipherAlg, pEntry);

	}

	/* open 802.1x port control and privacy filter */
	if (pEntry->AuthMode == Ndis802_11AuthModeWPA2PSK ||
	    pEntry->AuthMode == Ndis802_11AuthModeWPA2) {
		pEntry->PortSecured = WPA_802_1X_PORT_SECURED;
		pEntry->PrivacyFilter = Ndis802_11PrivFilterAcceptAll;

		STA_PORT_SECURED(pAd);
		/* Indicate Connected for GUI */
		pAd->IndicateMediaState = NdisMediaStateConnected;
		DBGPRINT(RT_DEBUG_TRACE,
			 ("PeerPairMsg3Action: AuthMode(%s) PairwiseCipher(%s) GroupCipher(%s) \n",
			  GetAuthMode(pEntry->AuthMode),
			  GetEncryptType(pEntry->WepStatus),
			  GetEncryptType(group_cipher)));
	} else {
	}

	/* Init 802.3 header and send out */
	MAKE_802_3_HEADER(Header802_3, pEntry->Addr, pCurrentAddr, EAPOL);
	RTMPToWirelessSta(pAd, pEntry,
			  Header802_3, sizeof(Header802_3),
			  (u8 *)& EAPOLPKT,
			  CONV_ARRARY_TO_u16(EAPOLPKT.Body_Len) + 4, TRUE);

	DBGPRINT(RT_DEBUG_TRACE,
		 ("<=== PeerPairMsg3Action: send Msg4 of 4-way \n"));
}

/*
    ==========================================================================
    Description:
        When receiving the last packet of 4-way pairwisekey handshake.
        Initialize 2-way groupkey handshake following.
    Return:
    ==========================================================================
*/
void PeerPairMsg4Action(struct rt_rtmp_adapter *pAd,
			struct rt_mac_table_entry *pEntry, struct rt_mlme_queue_elem *Elem)
{
	struct rt_eapol_packet * pMsg4;
	struct rt_header_802_11 * pHeader;
	u32 MsgLen;
	BOOLEAN Cancelled;
	u8 group_cipher = Ndis802_11WEPDisabled;

	DBGPRINT(RT_DEBUG_TRACE, ("===> PeerPairMsg4Action\n"));

	do {
		if ((!pEntry) || (!pEntry->ValidAsCLI))
			break;

		if (Elem->MsgLen <
		    (LENGTH_802_11 + LENGTH_802_1_H + LENGTH_EAPOL_H +
		     sizeof(struct rt_key_descripter) - MAX_LEN_OF_RSNIE - 2))
			break;

		if (pEntry->WpaState < AS_PTKINIT_NEGOTIATING)
			break;

		/* pointer to 802.11 header */
		pHeader = (struct rt_header_802_11 *) Elem->Msg;

		/* skip 802.11_header(24-byte) and LLC_header(8) */
		pMsg4 =
		    (struct rt_eapol_packet *) & Elem->Msg[LENGTH_802_11 + LENGTH_802_1_H];
		MsgLen = Elem->MsgLen - LENGTH_802_11 - LENGTH_802_1_H;

		/* Sanity Check peer Pairwise message 4 - Replay Counter, MIC */
		if (PeerWpaMessageSanity
		    (pAd, pMsg4, MsgLen, EAPOL_PAIR_MSG_4, pEntry) == FALSE)
			break;

		/* 3. uses the MLME.SETKEYS.request to configure PTK into MAC */
		NdisZeroMemory(&pEntry->PairwiseKey, sizeof(struct rt_cipher_key));

		/* reset IVEIV in Asic */
		AsicUpdateWCIDIVEIV(pAd, pEntry->Aid, 1, 0);

		pEntry->PairwiseKey.KeyLen = LEN_TKIP_EK;
		NdisMoveMemory(pEntry->PairwiseKey.Key, &pEntry->PTK[32],
			       LEN_TKIP_EK);
		NdisMoveMemory(pEntry->PairwiseKey.RxMic,
			       &pEntry->PTK[TKIP_AP_RXMICK_OFFSET],
			       LEN_TKIP_RXMICK);
		NdisMoveMemory(pEntry->PairwiseKey.TxMic,
			       &pEntry->PTK[TKIP_AP_TXMICK_OFFSET],
			       LEN_TKIP_TXMICK);

		/* Set pairwise key to Asic */
		{
			pEntry->PairwiseKey.CipherAlg = CIPHER_NONE;
			if (pEntry->WepStatus == Ndis802_11Encryption2Enabled)
				pEntry->PairwiseKey.CipherAlg = CIPHER_TKIP;
			else if (pEntry->WepStatus ==
				 Ndis802_11Encryption3Enabled)
				pEntry->PairwiseKey.CipherAlg = CIPHER_AES;

			/* Add Pair-wise key to Asic */
			AsicAddPairwiseKeyEntry(pAd,
						pEntry->Addr,
						(u8)pEntry->Aid,
						&pEntry->PairwiseKey);

			/* update WCID attribute table and IVEIV table for this entry */
			RTMPAddWcidAttributeEntry(pAd,
						  pEntry->apidx,
						  0,
						  pEntry->PairwiseKey.CipherAlg,
						  pEntry);
		}

		/* 4. upgrade state */
		pEntry->PrivacyFilter = Ndis802_11PrivFilterAcceptAll;
		pEntry->WpaState = AS_PTKINITDONE;
		pEntry->PortSecured = WPA_802_1X_PORT_SECURED;

		if (pEntry->AuthMode == Ndis802_11AuthModeWPA2 ||
		    pEntry->AuthMode == Ndis802_11AuthModeWPA2PSK) {
			pEntry->GTKState = REKEY_ESTABLISHED;
			RTMPCancelTimer(&pEntry->RetryTimer, &Cancelled);

			/* send wireless event - for set key done WPA2 */
			if (pAd->CommonCfg.bWirelessEvent)
				RTMPSendWirelessEvent(pAd,
						      IW_SET_KEY_DONE_WPA2_EVENT_FLAG,
						      pEntry->Addr,
						      pEntry->apidx, 0);

			DBGPRINT(RT_DEBUG_OFF,
				 ("AP SETKEYS DONE - WPA2, AuthMode(%d)=%s, WepStatus(%d)=%s, GroupWepStatus(%d)=%s\n\n",
				  pEntry->AuthMode,
				  GetAuthMode(pEntry->AuthMode),
				  pEntry->WepStatus,
				  GetEncryptType(pEntry->WepStatus),
				  group_cipher, GetEncryptType(group_cipher)));
		} else {
			/* 5. init Group 2-way handshake if necessary. */
			WPAStart2WayGroupHS(pAd, pEntry);

			pEntry->ReTryCounter = GROUP_MSG1_RETRY_TIMER_CTR;
			RTMPModTimer(&pEntry->RetryTimer,
				     PEER_MSG3_RETRY_EXEC_INTV);
		}
	} while (FALSE);

}

/*
    ==========================================================================
    Description:
        This is a function to send the first packet of 2-way groupkey handshake
    Return:

    ==========================================================================
*/
void WPAStart2WayGroupHS(struct rt_rtmp_adapter *pAd, struct rt_mac_table_entry *pEntry)
{
	u8 Header802_3[14];
	u8 TxTsc[6];
	struct rt_eapol_packet EAPOLPKT;
	u8 group_cipher = Ndis802_11WEPDisabled;
	u8 default_key = 0;
	u8 *gnonce_ptr = NULL;
	u8 *gtk_ptr = NULL;
	u8 *pBssid = NULL;

	DBGPRINT(RT_DEBUG_TRACE, ("===> WPAStart2WayGroupHS\n"));

	if ((!pEntry) || (!pEntry->ValidAsCLI))
		return;

	do {
		/* Increment replay counter by 1 */
		ADD_ONE_To_64BIT_VAR(pEntry->R_Counter);

		/* Construct EAPoL message - Group Msg 1 */
		NdisZeroMemory(&EAPOLPKT, sizeof(struct rt_eapol_packet));
		ConstructEapolMsg(pEntry,
				  group_cipher,
				  EAPOL_GROUP_MSG_1,
				  default_key,
				  (u8 *) gnonce_ptr,
				  TxTsc, (u8 *) gtk_ptr, NULL, 0, &EAPOLPKT);

		/* Make outgoing frame */
		MAKE_802_3_HEADER(Header802_3, pEntry->Addr, pBssid, EAPOL);
		RTMPToWirelessSta(pAd, pEntry,
				  Header802_3, LENGTH_802_3,
				  (u8 *)& EAPOLPKT,
				  CONV_ARRARY_TO_u16(EAPOLPKT.Body_Len) + 4,
				  FALSE);

	} while (FALSE);

	DBGPRINT(RT_DEBUG_TRACE,
		 ("<=== WPAStart2WayGroupHS : send out Group Message 1 \n"));

	return;
}

/*
	========================================================================

	Routine Description:
		Process Group key 2-way handshaking

	Arguments:
		pAd	Pointer	to our adapter
		Elem		Message body

	Return Value:
		None

	Note:

	========================================================================
*/
void PeerGroupMsg1Action(struct rt_rtmp_adapter *pAd,
			 struct rt_mac_table_entry *pEntry, struct rt_mlme_queue_elem *Elem)
{
	u8 Header802_3[14];
	struct rt_eapol_packet EAPOLPKT;
	struct rt_eapol_packet * pGroup;
	u32 MsgLen;
	BOOLEAN Cancelled;
	u8 default_key = 0;
	u8 group_cipher = Ndis802_11WEPDisabled;
	u8 *pCurrentAddr = NULL;

	DBGPRINT(RT_DEBUG_TRACE, ("===> PeerGroupMsg1Action \n"));

	if ((!pEntry) || ((!pEntry->ValidAsCLI) && (!pEntry->ValidAsApCli)))
		return;

	{
		pCurrentAddr = pAd->CurrentAddress;
		group_cipher = pAd->StaCfg.GroupCipher;
		default_key = pAd->StaCfg.DefaultKeyId;
	}

	/* Process Group Message 1 frame. skip 802.11 header(24) & LLC_SNAP header(8) */
	pGroup = (struct rt_eapol_packet *) & Elem->Msg[LENGTH_802_11 + LENGTH_802_1_H];
	MsgLen = Elem->MsgLen - LENGTH_802_11 - LENGTH_802_1_H;

	/* Sanity Check peer group message 1 - Replay Counter, MIC, RSNIE */
	if (PeerWpaMessageSanity(pAd, pGroup, MsgLen, EAPOL_GROUP_MSG_1, pEntry)
	    == FALSE)
		return;

	/* delete retry timer */
	RTMPCancelTimer(&pEntry->RetryTimer, &Cancelled);

	/* Save Replay counter, it will use to construct message 2 */
	NdisMoveMemory(pEntry->R_Counter, pGroup->KeyDesc.ReplayCounter,
		       LEN_KEY_DESC_REPLAY);

	/* Construct EAPoL message - Group Msg 2 */
	NdisZeroMemory(&EAPOLPKT, sizeof(struct rt_eapol_packet));
	ConstructEapolMsg(pEntry, group_cipher, EAPOL_GROUP_MSG_2, default_key, NULL,	/* Nonce not used */
			  NULL,	/* TxRSC not used */
			  NULL,	/* GTK not used */
			  NULL,	/* RSN IE not used */
			  0, &EAPOLPKT);

	/* open 802.1x port control and privacy filter */
	pEntry->PortSecured = WPA_802_1X_PORT_SECURED;
	pEntry->PrivacyFilter = Ndis802_11PrivFilterAcceptAll;

	STA_PORT_SECURED(pAd);
	/* Indicate Connected for GUI */
	pAd->IndicateMediaState = NdisMediaStateConnected;

	DBGPRINT(RT_DEBUG_TRACE,
		 ("PeerGroupMsg1Action: AuthMode(%s) PairwiseCipher(%s) GroupCipher(%s) \n",
		  GetAuthMode(pEntry->AuthMode),
		  GetEncryptType(pEntry->WepStatus),
		  GetEncryptType(group_cipher)));

	/* init header and Fill Packet and send Msg 2 to authenticator */
	MAKE_802_3_HEADER(Header802_3, pEntry->Addr, pCurrentAddr, EAPOL);
	RTMPToWirelessSta(pAd, pEntry,
			  Header802_3, sizeof(Header802_3),
			  (u8 *)& EAPOLPKT,
			  CONV_ARRARY_TO_u16(EAPOLPKT.Body_Len) + 4, FALSE);

	DBGPRINT(RT_DEBUG_TRACE,
		 ("<=== PeerGroupMsg1Action: sned group message 2\n"));
}

/*
    ==========================================================================
    Description:
        When receiving the last packet of 2-way groupkey handshake.
    Return:
    ==========================================================================
*/
void PeerGroupMsg2Action(struct rt_rtmp_adapter *pAd,
			 struct rt_mac_table_entry *pEntry,
			 void * Msg, u32 MsgLen)
{
	u32 Len;
	u8 *pData;
	BOOLEAN Cancelled;
	struct rt_eapol_packet * pMsg2;
	u8 group_cipher = Ndis802_11WEPDisabled;

	DBGPRINT(RT_DEBUG_TRACE, ("===> PeerGroupMsg2Action \n"));

	do {
		if ((!pEntry) || (!pEntry->ValidAsCLI))
			break;

		if (MsgLen <
		    (LENGTH_802_1_H + LENGTH_EAPOL_H + sizeof(struct rt_key_descripter) -
		     MAX_LEN_OF_RSNIE - 2))
			break;

		if (pEntry->WpaState != AS_PTKINITDONE)
			break;

		pData = (u8 *)Msg;
		pMsg2 = (struct rt_eapol_packet *) (pData + LENGTH_802_1_H);
		Len = MsgLen - LENGTH_802_1_H;

		/* Sanity Check peer group message 2 - Replay Counter, MIC */
		if (PeerWpaMessageSanity
		    (pAd, pMsg2, Len, EAPOL_GROUP_MSG_2, pEntry) == FALSE)
			break;

		/* 3.  upgrade state */

		RTMPCancelTimer(&pEntry->RetryTimer, &Cancelled);
		pEntry->GTKState = REKEY_ESTABLISHED;

		if ((pEntry->AuthMode == Ndis802_11AuthModeWPA2)
		    || (pEntry->AuthMode == Ndis802_11AuthModeWPA2PSK)) {
			/* send wireless event - for set key done WPA2 */
			if (pAd->CommonCfg.bWirelessEvent)
				RTMPSendWirelessEvent(pAd,
						      IW_SET_KEY_DONE_WPA2_EVENT_FLAG,
						      pEntry->Addr,
						      pEntry->apidx, 0);

			DBGPRINT(RT_DEBUG_OFF,
				 ("AP SETKEYS DONE - WPA2, AuthMode(%d)=%s, WepStatus(%d)=%s, GroupWepStatus(%d)=%s\n\n",
				  pEntry->AuthMode,
				  GetAuthMode(pEntry->AuthMode),
				  pEntry->WepStatus,
				  GetEncryptType(pEntry->WepStatus),
				  group_cipher, GetEncryptType(group_cipher)));
		} else {
			/* send wireless event - for set key done WPA */
			if (pAd->CommonCfg.bWirelessEvent)
				RTMPSendWirelessEvent(pAd,
						      IW_SET_KEY_DONE_WPA1_EVENT_FLAG,
						      pEntry->Addr,
						      pEntry->apidx, 0);

			DBGPRINT(RT_DEBUG_OFF,
				 ("AP SETKEYS DONE - WPA1, AuthMode(%d)=%s, WepStatus(%d)=%s, GroupWepStatus(%d)=%s\n\n",
				  pEntry->AuthMode,
				  GetAuthMode(pEntry->AuthMode),
				  pEntry->WepStatus,
				  GetEncryptType(pEntry->WepStatus),
				  group_cipher, GetEncryptType(group_cipher)));
		}
	} while (FALSE);
}

/*
	========================================================================

	Routine Description:
		Classify WPA EAP message type

	Arguments:
		EAPType		Value of EAP message type
		MsgType		Internal Message definition for MLME state machine

	Return Value:
		TRUE		Found appropriate message type
		FALSE		No appropriate message type

	IRQL = DISPATCH_LEVEL

	Note:
		All these constants are defined in wpa.h
		For supplicant, there is only EAPOL Key message avaliable

	========================================================================
*/
BOOLEAN WpaMsgTypeSubst(u8 EAPType, int * MsgType)
{
	switch (EAPType) {
	case EAPPacket:
		*MsgType = MT2_EAPPacket;
		break;
	case EAPOLStart:
		*MsgType = MT2_EAPOLStart;
		break;
	case EAPOLLogoff:
		*MsgType = MT2_EAPOLLogoff;
		break;
	case EAPOLKey:
		*MsgType = MT2_EAPOLKey;
		break;
	case EAPOLASFAlert:
		*MsgType = MT2_EAPOLASFAlert;
		break;
	default:
		return FALSE;
	}
	return TRUE;
}

/*
	========================================================================

	Routine Description:
		The pseudo-random function(PRF) that hashes various inputs to
		derive a pseudo-random value. To add liveness to the pseudo-random
		value, a nonce should be one of the inputs.

		It is used to generate PTK, GTK or some specific random value.

	Arguments:
		u8	*key,		-	the key material for HMAC_SHA1 use
		int		key_len		-	the length of key
		u8	*prefix		-	a prefix label
		int		prefix_len	-	the length of the label
		u8	*data		-	a specific data with variable length
		int		data_len	-	the length of a specific data
		int		len			-	the output lenght

	Return Value:
		u8	*output		-	the calculated result

	Note:
		802.11i-2004	Annex H.3

	========================================================================
*/
void PRF(u8 * key,
	 int key_len,
	 u8 * prefix,
	 int prefix_len,
	 u8 * data, int data_len, u8 * output, int len)
{
	int i;
	u8 *input;
	int currentindex = 0;
	int total_len;

	/* Allocate memory for input */
	os_alloc_mem(NULL, (u8 **) & input, 1024);

	if (input == NULL) {
		DBGPRINT(RT_DEBUG_ERROR, ("PRF: no memory!\n"));
		return;
	}
	/* Generate concatenation input */
	NdisMoveMemory(input, prefix, prefix_len);

	/* Concatenate a single octet containing 0 */
	input[prefix_len] = 0;

	/* Concatenate specific data */
	NdisMoveMemory(&input[prefix_len + 1], data, data_len);
	total_len = prefix_len + 1 + data_len;

	/* Concatenate a single octet containing 0 */
	/* This octet shall be update later */
	input[total_len] = 0;
	total_len++;

	/* Iterate to calculate the result by hmac-sha-1 */
	/* Then concatenate to last result */
	for (i = 0; i < (len + 19) / 20; i++) {
		HMAC_SHA1(key, key_len, input, total_len, &output[currentindex],
			  SHA1_DIGEST_SIZE);
		currentindex += 20;

		/* update the last octet */
		input[total_len - 1]++;
	}
	os_free_mem(NULL, input);
}

/*
* F(P, S, c, i) = U1 xor U2 xor ... Uc
* U1 = PRF(P, S || Int(i))
* U2 = PRF(P, U1)
* Uc = PRF(P, Uc-1)
*/

static void F(char *password, unsigned char *ssid, int ssidlength,
	      int iterations, int count, unsigned char *output)
{
	unsigned char digest[36], digest1[SHA1_DIGEST_SIZE];
	int i, j;

	/* U1 = PRF(P, S || int(i)) */
	memcpy(digest, ssid, ssidlength);
	digest[ssidlength] = (unsigned char)((count >> 24) & 0xff);
	digest[ssidlength + 1] = (unsigned char)((count >> 16) & 0xff);
	digest[ssidlength + 2] = (unsigned char)((count >> 8) & 0xff);
	digest[ssidlength + 3] = (unsigned char)(count & 0xff);
	HMAC_SHA1((unsigned char *)password, (int)strlen(password), digest, ssidlength + 4, digest1, SHA1_DIGEST_SIZE);	/* for WPA update */

	/* output = U1 */
	memcpy(output, digest1, SHA1_DIGEST_SIZE);

	for (i = 1; i < iterations; i++) {
		/* Un = PRF(P, Un-1) */
		HMAC_SHA1((unsigned char *)password, (int)strlen(password), digest1, SHA1_DIGEST_SIZE, digest, SHA1_DIGEST_SIZE);	/* for WPA update */
		memcpy(digest1, digest, SHA1_DIGEST_SIZE);

		/* output = output xor Un */
		for (j = 0; j < SHA1_DIGEST_SIZE; j++) {
			output[j] ^= digest[j];
		}
	}
}

/*
* password - ascii string up to 63 characters in length
* ssid - octet string up to 32 octets
* ssidlength - length of ssid in octets
* output must be 40 octets in length and outputs 256 bits of key
*/
int PasswordHash(char *password, u8 *ssid, int ssidlength, u8 *output)
{
	if ((strlen(password) > 63) || (ssidlength > 32))
		return 0;

	F(password, ssid, ssidlength, 4096, 1, output);
	F(password, ssid, ssidlength, 4096, 2, &output[SHA1_DIGEST_SIZE]);
	return 1;
}

/*
	========================================================================

	Routine Description:
		It utilizes PRF-384 or PRF-512 to derive session-specific keys from a PMK.
		It shall be called by 4-way handshake processing.

	Arguments:
		pAd	-	pointer to our pAdapter context
		PMK		-	pointer to PMK
		ANonce	-	pointer to ANonce
		AA		-	pointer to Authenticator Address
		SNonce	-	pointer to SNonce
		SA		-	pointer to Supplicant Address
		len		-	indicate the length of PTK (octet)

	Return Value:
		Output		pointer to the PTK

	Note:
		Refer to IEEE 802.11i-2004 8.5.1.2

	========================================================================
*/
void WpaDerivePTK(struct rt_rtmp_adapter *pAd,
		  u8 * PMK,
		  u8 * ANonce,
		  u8 * AA,
		  u8 * SNonce,
		  u8 * SA, u8 * output, u32 len)
{
	u8 concatenation[76];
	u32 CurrPos = 0;
	u8 temp[32];
	u8 Prefix[] =
	    { 'P', 'a', 'i', 'r', 'w', 'i', 's', 'e', ' ', 'k', 'e', 'y', ' ',
		'e', 'x', 'p', 'a', 'n', 's', 'i', 'o', 'n'
	};

	/* initiate the concatenation input */
	NdisZeroMemory(temp, sizeof(temp));
	NdisZeroMemory(concatenation, 76);

	/* Get smaller address */
	if (RTMPCompareMemory(SA, AA, 6) == 1)
		NdisMoveMemory(concatenation, AA, 6);
	else
		NdisMoveMemory(concatenation, SA, 6);
	CurrPos += 6;

	/* Get larger address */
	if (RTMPCompareMemory(SA, AA, 6) == 1)
		NdisMoveMemory(&concatenation[CurrPos], SA, 6);
	else
		NdisMoveMemory(&concatenation[CurrPos], AA, 6);

	/* store the larger mac address for backward compatible of */
	/* ralink proprietary STA-key issue */
	NdisMoveMemory(temp, &concatenation[CurrPos], MAC_ADDR_LEN);
	CurrPos += 6;

	/* Get smaller Nonce */
	if (RTMPCompareMemory(ANonce, SNonce, 32) == 0)
		NdisMoveMemory(&concatenation[CurrPos], temp, 32);	/* patch for ralink proprietary STA-key issue */
	else if (RTMPCompareMemory(ANonce, SNonce, 32) == 1)
		NdisMoveMemory(&concatenation[CurrPos], SNonce, 32);
	else
		NdisMoveMemory(&concatenation[CurrPos], ANonce, 32);
	CurrPos += 32;

	/* Get larger Nonce */
	if (RTMPCompareMemory(ANonce, SNonce, 32) == 0)
		NdisMoveMemory(&concatenation[CurrPos], temp, 32);	/* patch for ralink proprietary STA-key issue */
	else if (RTMPCompareMemory(ANonce, SNonce, 32) == 1)
		NdisMoveMemory(&concatenation[CurrPos], ANonce, 32);
	else
		NdisMoveMemory(&concatenation[CurrPos], SNonce, 32);
	CurrPos += 32;

	hex_dump("concatenation=", concatenation, 76);

	/* Use PRF to generate PTK */
	PRF(PMK, LEN_MASTER_KEY, Prefix, 22, concatenation, 76, output, len);

}

/*
	========================================================================

	Routine Description:
		Generate random number by software.

	Arguments:
		pAd		-	pointer to our pAdapter context
		macAddr	-	pointer to local MAC address

	Return Value:

	Note:
		802.1ii-2004  Annex H.5

	========================================================================
*/
void GenRandom(struct rt_rtmp_adapter *pAd, u8 * macAddr, u8 * random)
{
	int i, curr;
	u8 local[80], KeyCounter[32];
	u8 result[80];
	unsigned long CurrentTime;
	u8 prefix[] =
	    { 'I', 'n', 'i', 't', ' ', 'C', 'o', 'u', 'n', 't', 'e', 'r' };

	/* Zero the related information */
	NdisZeroMemory(result, 80);
	NdisZeroMemory(local, 80);
	NdisZeroMemory(KeyCounter, 32);

	for (i = 0; i < 32; i++) {
		/* copy the local MAC address */
		COPY_MAC_ADDR(local, macAddr);
		curr = MAC_ADDR_LEN;

		/* concatenate the current time */
		NdisGetSystemUpTime(&CurrentTime);
		NdisMoveMemory(&local[curr], &CurrentTime, sizeof(CurrentTime));
		curr += sizeof(CurrentTime);

		/* concatenate the last result */
		NdisMoveMemory(&local[curr], result, 32);
		curr += 32;

		/* concatenate a variable */
		NdisMoveMemory(&local[curr], &i, 2);
		curr += 2;

		/* calculate the result */
		PRF(KeyCounter, 32, prefix, 12, local, curr, result, 32);
	}

	NdisMoveMemory(random, result, 32);
}

/*
	========================================================================

	Routine Description:
		Build cipher suite in RSN-IE.
		It only shall be called by RTMPMakeRSNIE.

	Arguments:
		pAd			-	pointer to our pAdapter context
	ElementID	-	indicate the WPA1 or WPA2
	WepStatus	-	indicate the encryption type
		bMixCipher	-	a boolean to indicate the pairwise cipher and group
						cipher are the same or not

	Return Value:

	Note:

	========================================================================
*/
static void RTMPMakeRsnIeCipher(struct rt_rtmp_adapter *pAd,
				u8 ElementID,
				u32 WepStatus,
				IN BOOLEAN bMixCipher,
				u8 FlexibleCipher,
				u8 *pRsnIe, u8 * rsn_len)
{
	u8 PairwiseCnt;

	*rsn_len = 0;

	/* decide WPA2 or WPA1 */
	if (ElementID == Wpa2Ie) {
		struct rt_rsnie2 *pRsnie_cipher = (struct rt_rsnie2 *)pRsnIe;

		/* Assign the verson as 1 */
		pRsnie_cipher->version = 1;

		switch (WepStatus) {
			/* TKIP mode */
		case Ndis802_11Encryption2Enabled:
			NdisMoveMemory(pRsnie_cipher->mcast, OUI_WPA2_TKIP, 4);
			pRsnie_cipher->ucount = 1;
			NdisMoveMemory(pRsnie_cipher->ucast[0].oui,
				       OUI_WPA2_TKIP, 4);
			*rsn_len = sizeof(struct rt_rsnie2);
			break;

			/* AES mode */
		case Ndis802_11Encryption3Enabled:
			if (bMixCipher)
				NdisMoveMemory(pRsnie_cipher->mcast,
					       OUI_WPA2_TKIP, 4);
			else
				NdisMoveMemory(pRsnie_cipher->mcast,
					       OUI_WPA2_CCMP, 4);
			pRsnie_cipher->ucount = 1;
			NdisMoveMemory(pRsnie_cipher->ucast[0].oui,
				       OUI_WPA2_CCMP, 4);
			*rsn_len = sizeof(struct rt_rsnie2);
			break;

			/* TKIP-AES mix mode */
		case Ndis802_11Encryption4Enabled:
			NdisMoveMemory(pRsnie_cipher->mcast, OUI_WPA2_TKIP, 4);

			PairwiseCnt = 1;
			/* Insert WPA2 TKIP as the first pairwise cipher */
			if (MIX_CIPHER_WPA2_TKIP_ON(FlexibleCipher)) {
				NdisMoveMemory(pRsnie_cipher->ucast[0].oui,
					       OUI_WPA2_TKIP, 4);
				/* Insert WPA2 AES as the secondary pairwise cipher */
				if (MIX_CIPHER_WPA2_AES_ON(FlexibleCipher)) {
					NdisMoveMemory(pRsnie_cipher->ucast[0].
						       oui + 4, OUI_WPA2_CCMP,
						       4);
					PairwiseCnt = 2;
				}
			} else {
				/* Insert WPA2 AES as the first pairwise cipher */
				NdisMoveMemory(pRsnie_cipher->ucast[0].oui,
					       OUI_WPA2_CCMP, 4);
			}

			pRsnie_cipher->ucount = PairwiseCnt;
			*rsn_len = sizeof(struct rt_rsnie2) + (4 * (PairwiseCnt - 1));
			break;
		}

		if ((pAd->OpMode == OPMODE_STA) &&
		    (pAd->StaCfg.GroupCipher != Ndis802_11Encryption2Enabled) &&
		    (pAd->StaCfg.GroupCipher != Ndis802_11Encryption3Enabled)) {
			u32 GroupCipher = pAd->StaCfg.GroupCipher;
			switch (GroupCipher) {
			case Ndis802_11GroupWEP40Enabled:
				NdisMoveMemory(pRsnie_cipher->mcast,
					       OUI_WPA2_WEP40, 4);
				break;
			case Ndis802_11GroupWEP104Enabled:
				NdisMoveMemory(pRsnie_cipher->mcast,
					       OUI_WPA2_WEP104, 4);
				break;
			}
		}
		/* swap for big-endian platform */
		pRsnie_cipher->version = cpu2le16(pRsnie_cipher->version);
		pRsnie_cipher->ucount = cpu2le16(pRsnie_cipher->ucount);
	} else {
		struct rt_rsnie *pRsnie_cipher = (struct rt_rsnie *)pRsnIe;

		/* Assign OUI and version */
		NdisMoveMemory(pRsnie_cipher->oui, OUI_WPA_VERSION, 4);
		pRsnie_cipher->version = 1;

		switch (WepStatus) {
			/* TKIP mode */
		case Ndis802_11Encryption2Enabled:
			NdisMoveMemory(pRsnie_cipher->mcast, OUI_WPA_TKIP, 4);
			pRsnie_cipher->ucount = 1;
			NdisMoveMemory(pRsnie_cipher->ucast[0].oui,
				       OUI_WPA_TKIP, 4);
			*rsn_len = sizeof(struct rt_rsnie);
			break;

			/* AES mode */
		case Ndis802_11Encryption3Enabled:
			if (bMixCipher)
				NdisMoveMemory(pRsnie_cipher->mcast,
					       OUI_WPA_TKIP, 4);
			else
				NdisMoveMemory(pRsnie_cipher->mcast,
					       OUI_WPA_CCMP, 4);
			pRsnie_cipher->ucount = 1;
			NdisMoveMemory(pRsnie_cipher->ucast[0].oui,
				       OUI_WPA_CCMP, 4);
			*rsn_len = sizeof(struct rt_rsnie);
			break;

			/* TKIP-AES mix mode */
		case Ndis802_11Encryption4Enabled:
			NdisMoveMemory(pRsnie_cipher->mcast, OUI_WPA_TKIP, 4);

			PairwiseCnt = 1;
			/* Insert WPA TKIP as the first pairwise cipher */
			if (MIX_CIPHER_WPA_TKIP_ON(FlexibleCipher)) {
				NdisMoveMemory(pRsnie_cipher->ucast[0].oui,
					       OUI_WPA_TKIP, 4);
				/* Insert WPA AES as the secondary pairwise cipher */
				if (MIX_CIPHER_WPA_AES_ON(FlexibleCipher)) {
					NdisMoveMemory(pRsnie_cipher->ucast[0].
						       oui + 4, OUI_WPA_CCMP,
						       4);
					PairwiseCnt = 2;
				}
			} else {
				/* Insert WPA AES as the first pairwise cipher */
				NdisMoveMemory(pRsnie_cipher->ucast[0].oui,
					       OUI_WPA_CCMP, 4);
			}

			pRsnie_cipher->ucount = PairwiseCnt;
			*rsn_len = sizeof(struct rt_rsnie) + (4 * (PairwiseCnt - 1));
			break;
		}

		if ((pAd->OpMode == OPMODE_STA) &&
		    (pAd->StaCfg.GroupCipher != Ndis802_11Encryption2Enabled) &&
		    (pAd->StaCfg.GroupCipher != Ndis802_11Encryption3Enabled)) {
			u32 GroupCipher = pAd->StaCfg.GroupCipher;
			switch (GroupCipher) {
			case Ndis802_11GroupWEP40Enabled:
				NdisMoveMemory(pRsnie_cipher->mcast,
					       OUI_WPA_WEP40, 4);
				break;
			case Ndis802_11GroupWEP104Enabled:
				NdisMoveMemory(pRsnie_cipher->mcast,
					       OUI_WPA_WEP104, 4);
				break;
			}
		}
		/* swap for big-endian platform */
		pRsnie_cipher->version = cpu2le16(pRsnie_cipher->version);
		pRsnie_cipher->ucount = cpu2le16(pRsnie_cipher->ucount);
	}
}

/*
	========================================================================

	Routine Description:
		Build AKM suite in RSN-IE.
		It only shall be called by RTMPMakeRSNIE.

	Arguments:
		pAd			-	pointer to our pAdapter context
	ElementID	-	indicate the WPA1 or WPA2
	AuthMode	-	indicate the authentication mode
		apidx		-	indicate the interface index

	Return Value:

	Note:

	========================================================================
*/
static void RTMPMakeRsnIeAKM(struct rt_rtmp_adapter *pAd,
			     u8 ElementID,
			     u32 AuthMode,
			     u8 apidx,
			     u8 *pRsnIe, u8 * rsn_len)
{
	struct rt_rsnie_auth *pRsnie_auth;
	u8 AkmCnt = 1;	/* default as 1 */

	pRsnie_auth = (struct rt_rsnie_auth *) (pRsnIe + (*rsn_len));

	/* decide WPA2 or WPA1 */
	if (ElementID == Wpa2Ie) {

		switch (AuthMode) {
		case Ndis802_11AuthModeWPA2:
		case Ndis802_11AuthModeWPA1WPA2:
			NdisMoveMemory(pRsnie_auth->auth[0].oui,
				       OUI_WPA2_8021X_AKM, 4);
			break;

		case Ndis802_11AuthModeWPA2PSK:
		case Ndis802_11AuthModeWPA1PSKWPA2PSK:
			NdisMoveMemory(pRsnie_auth->auth[0].oui,
				       OUI_WPA2_PSK_AKM, 4);
			break;
		default:
			AkmCnt = 0;
			break;

		}
	} else {
		switch (AuthMode) {
		case Ndis802_11AuthModeWPA:
		case Ndis802_11AuthModeWPA1WPA2:
			NdisMoveMemory(pRsnie_auth->auth[0].oui,
				       OUI_WPA_8021X_AKM, 4);
			break;

		case Ndis802_11AuthModeWPAPSK:
		case Ndis802_11AuthModeWPA1PSKWPA2PSK:
			NdisMoveMemory(pRsnie_auth->auth[0].oui,
				       OUI_WPA_PSK_AKM, 4);
			break;

		case Ndis802_11AuthModeWPANone:
			NdisMoveMemory(pRsnie_auth->auth[0].oui,
				       OUI_WPA_NONE_AKM, 4);
			break;
		default:
			AkmCnt = 0;
			break;
		}
	}

	pRsnie_auth->acount = AkmCnt;
	pRsnie_auth->acount = cpu2le16(pRsnie_auth->acount);

	/* update current RSNIE length */
	(*rsn_len) += (sizeof(struct rt_rsnie_auth) + (4 * (AkmCnt - 1)));

}

/*
	========================================================================

	Routine Description:
		Build capability in RSN-IE.
		It only shall be called by RTMPMakeRSNIE.

	Arguments:
		pAd			-	pointer to our pAdapter context
	ElementID	-	indicate the WPA1 or WPA2
		apidx		-	indicate the interface index

	Return Value:

	Note:

	========================================================================
*/
static void RTMPMakeRsnIeCap(struct rt_rtmp_adapter *pAd,
			     u8 ElementID,
			     u8 apidx,
			     u8 *pRsnIe, u8 * rsn_len)
{
	RSN_CAPABILITIES *pRSN_Cap;

	/* it could be ignored in WPA1 mode */
	if (ElementID == WpaIe)
		return;

	pRSN_Cap = (RSN_CAPABILITIES *) (pRsnIe + (*rsn_len));

	pRSN_Cap->word = cpu2le16(pRSN_Cap->word);

	(*rsn_len) += sizeof(RSN_CAPABILITIES);	/* update current RSNIE length */

}

/*
	========================================================================

	Routine Description:
		Build RSN IE context. It is not included element-ID and length.

	Arguments:
		pAd			-	pointer to our pAdapter context
	AuthMode	-	indicate the authentication mode
	WepStatus	-	indicate the encryption type
		apidx		-	indicate the interface index

	Return Value:

	Note:

	========================================================================
*/
void RTMPMakeRSNIE(struct rt_rtmp_adapter *pAd,
		   u32 AuthMode, u32 WepStatus, u8 apidx)
{
	u8 *pRsnIe = NULL;	/* primary RSNIE */
	u8 *rsnielen_cur_p = 0;	/* the length of the primary RSNIE */
	u8 *rsnielen_ex_cur_p = 0;	/* the length of the secondary RSNIE */
	u8 PrimaryRsnie;
	BOOLEAN bMixCipher = FALSE;	/* indicate the pairwise and group cipher are different */
	u8 p_offset;
	WPA_MIX_PAIR_CIPHER FlexibleCipher = WPA_TKIPAES_WPA2_TKIPAES;	/* it provide the more flexible cipher combination in WPA-WPA2 and TKIPAES mode */

	rsnielen_cur_p = NULL;
	rsnielen_ex_cur_p = NULL;

	{
		{
			if (pAd->StaCfg.WpaSupplicantUP !=
			    WPA_SUPPLICANT_DISABLE) {
				if (AuthMode < Ndis802_11AuthModeWPA)
					return;
			} else {
				/* Support WPAPSK or WPA2PSK in STA-Infra mode */
				/* Support WPANone in STA-Adhoc mode */
				if ((AuthMode != Ndis802_11AuthModeWPAPSK) &&
				    (AuthMode != Ndis802_11AuthModeWPA2PSK) &&
				    (AuthMode != Ndis802_11AuthModeWPANone)
				    )
					return;
			}

			DBGPRINT(RT_DEBUG_TRACE, ("==> RTMPMakeRSNIE(STA)\n"));

			/* Zero RSNIE context */
			pAd->StaCfg.RSNIE_Len = 0;
			NdisZeroMemory(pAd->StaCfg.RSN_IE, MAX_LEN_OF_RSNIE);

			/* Pointer to RSNIE */
			rsnielen_cur_p = &pAd->StaCfg.RSNIE_Len;
			pRsnIe = pAd->StaCfg.RSN_IE;

			bMixCipher = pAd->StaCfg.bMixCipher;
		}
	}

	/* indicate primary RSNIE as WPA or WPA2 */
	if ((AuthMode == Ndis802_11AuthModeWPA) ||
	    (AuthMode == Ndis802_11AuthModeWPAPSK) ||
	    (AuthMode == Ndis802_11AuthModeWPANone) ||
	    (AuthMode == Ndis802_11AuthModeWPA1WPA2) ||
	    (AuthMode == Ndis802_11AuthModeWPA1PSKWPA2PSK))
		PrimaryRsnie = WpaIe;
	else
		PrimaryRsnie = Wpa2Ie;

	{
		/* Build the primary RSNIE */
		/* 1. insert cipher suite */
		RTMPMakeRsnIeCipher(pAd, PrimaryRsnie, WepStatus, bMixCipher,
				    FlexibleCipher, pRsnIe, &p_offset);

		/* 2. insert AKM */
		RTMPMakeRsnIeAKM(pAd, PrimaryRsnie, AuthMode, apidx, pRsnIe,
				 &p_offset);

		/* 3. insert capability */
		RTMPMakeRsnIeCap(pAd, PrimaryRsnie, apidx, pRsnIe, &p_offset);
	}

	/* 4. update the RSNIE length */
	*rsnielen_cur_p = p_offset;

	hex_dump("The primary RSNIE", pRsnIe, (*rsnielen_cur_p));

}

/*
    ==========================================================================
    Description:
		Check whether the received frame is EAP frame.

	Arguments:
		pAd				-	pointer to our pAdapter context
		pEntry			-	pointer to active entry
		pData			-	the received frame
		DataByteCount	-	the received frame's length
		FromWhichBSSID	-	indicate the interface index

    Return:
         TRUE			-	This frame is EAP frame
         FALSE			-	otherwise
    ==========================================================================
*/
BOOLEAN RTMPCheckWPAframe(struct rt_rtmp_adapter *pAd,
			  struct rt_mac_table_entry *pEntry,
			  u8 *pData,
			  unsigned long DataByteCount, u8 FromWhichBSSID)
{
	unsigned long Body_len;
	BOOLEAN Cancelled;

	if (DataByteCount < (LENGTH_802_1_H + LENGTH_EAPOL_H))
		return FALSE;

	/* Skip LLC header */
	if (NdisEqualMemory(SNAP_802_1H, pData, 6) ||
	    /* Cisco 1200 AP may send packet with SNAP_BRIDGE_TUNNEL */
	    NdisEqualMemory(SNAP_BRIDGE_TUNNEL, pData, 6)) {
		pData += 6;
	}
	/* Skip 2-bytes EAPoL type */
	if (NdisEqualMemory(EAPOL, pData, 2)) {
		pData += 2;
	} else
		return FALSE;

	switch (*(pData + 1)) {
	case EAPPacket:
		Body_len = (*(pData + 2) << 8) | (*(pData + 3));
		DBGPRINT(RT_DEBUG_TRACE,
			 ("Receive EAP-Packet frame, TYPE = 0, Length = %ld\n",
			  Body_len));
		break;
	case EAPOLStart:
		DBGPRINT(RT_DEBUG_TRACE,
			 ("Receive EAPOL-Start frame, TYPE = 1 \n"));
		if (pEntry->EnqueueEapolStartTimerRunning !=
		    EAPOL_START_DISABLE) {
			DBGPRINT(RT_DEBUG_TRACE,
				 ("Cancel the EnqueueEapolStartTimerRunning \n"));
			RTMPCancelTimer(&pEntry->EnqueueStartForPSKTimer,
					&Cancelled);
			pEntry->EnqueueEapolStartTimerRunning =
			    EAPOL_START_DISABLE;
		}
		break;
	case EAPOLLogoff:
		DBGPRINT(RT_DEBUG_TRACE,
			 ("Receive EAPOLLogoff frame, TYPE = 2 \n"));
		break;
	case EAPOLKey:
		Body_len = (*(pData + 2) << 8) | (*(pData + 3));
		DBGPRINT(RT_DEBUG_TRACE,
			 ("Receive EAPOL-Key frame, TYPE = 3, Length = %ld\n",
			  Body_len));
		break;
	case EAPOLASFAlert:
		DBGPRINT(RT_DEBUG_TRACE,
			 ("Receive EAPOLASFAlert frame, TYPE = 4 \n"));
		break;
	default:
		return FALSE;

	}
	return TRUE;
}

/*
    ==========================================================================
    Description:
		Report the EAP message type

	Arguments:
		msg		-	EAPOL_PAIR_MSG_1
					EAPOL_PAIR_MSG_2
					EAPOL_PAIR_MSG_3
					EAPOL_PAIR_MSG_4
					EAPOL_GROUP_MSG_1
					EAPOL_GROUP_MSG_2

    Return:
         message type string

    ==========================================================================
*/
char *GetEapolMsgType(char msg)
{
	if (msg == EAPOL_PAIR_MSG_1)
		return "Pairwise Message 1";
	else if (msg == EAPOL_PAIR_MSG_2)
		return "Pairwise Message 2";
	else if (msg == EAPOL_PAIR_MSG_3)
		return "Pairwise Message 3";
	else if (msg == EAPOL_PAIR_MSG_4)
		return "Pairwise Message 4";
	else if (msg == EAPOL_GROUP_MSG_1)
		return "Group Message 1";
	else if (msg == EAPOL_GROUP_MSG_2)
		return "Group Message 2";
	else
		return "Invalid Message";
}

/*
	========================================================================

	Routine Description:
    Check Sanity RSN IE of EAPoL message

	Arguments:

	Return Value:

	========================================================================
*/
BOOLEAN RTMPCheckRSNIE(struct rt_rtmp_adapter *pAd,
		       u8 *pData,
		       u8 DataLen,
		       struct rt_mac_table_entry *pEntry, u8 * Offset)
{
	u8 *pVIE;
	u8 len;
	struct rt_eid * pEid;
	BOOLEAN result = FALSE;

	pVIE = pData;
	len = DataLen;
	*Offset = 0;

	while (len > sizeof(struct rt_rsnie2)) {
		pEid = (struct rt_eid *) pVIE;
		/* WPA RSN IE */
		if ((pEid->Eid == IE_WPA)
		    && (NdisEqualMemory(pEid->Octet, WPA_OUI, 4))) {
			if ((pEntry->AuthMode == Ndis802_11AuthModeWPA
			     || pEntry->AuthMode == Ndis802_11AuthModeWPAPSK)
			    &&
			    (NdisEqualMemory
			     (pVIE, pEntry->RSN_IE, pEntry->RSNIE_Len))
			    && (pEntry->RSNIE_Len == (pEid->Len + 2))) {
				result = TRUE;
			}

			*Offset += (pEid->Len + 2);
		}
		/* WPA2 RSN IE */
		else if ((pEid->Eid == IE_RSN)
			 && (NdisEqualMemory(pEid->Octet + 2, RSN_OUI, 3))) {
			if ((pEntry->AuthMode == Ndis802_11AuthModeWPA2
			     || pEntry->AuthMode == Ndis802_11AuthModeWPA2PSK)
			    && (pEid->Eid == pEntry->RSN_IE[0])
			    && ((pEid->Len + 2) >= pEntry->RSNIE_Len)
			    &&
			    (NdisEqualMemory
			     (pEid->Octet, &pEntry->RSN_IE[2],
			      pEntry->RSNIE_Len - 2))) {

				result = TRUE;
			}

			*Offset += (pEid->Len + 2);
		} else {
			break;
		}

		pVIE += (pEid->Len + 2);
		len -= (pEid->Len + 2);
	}

	return result;

}

/*
	========================================================================

	Routine Description:
    Parse KEYDATA field.  KEYDATA[] May contain 2 RSN IE and optionally GTK.
    GTK  is encaptulated in KDE format at  p.83 802.11i D10

	Arguments:

	Return Value:

	Note:
        802.11i D10

	========================================================================
*/
BOOLEAN RTMPParseEapolKeyData(struct rt_rtmp_adapter *pAd,
			      u8 *pKeyData,
			      u8 KeyDataLen,
			      u8 GroupKeyIndex,
			      u8 MsgType,
			      IN BOOLEAN bWPA2, struct rt_mac_table_entry *pEntry)
{
	struct rt_kde_encap * pKDE = NULL;
	u8 *pMyKeyData = pKeyData;
	u8 KeyDataLength = KeyDataLen;
	u8 GTKLEN = 0;
	u8 DefaultIdx = 0;
	u8 skip_offset;

	/* Verify The RSN IE contained in pairewise_msg_2 && pairewise_msg_3 and skip it */
	if (MsgType == EAPOL_PAIR_MSG_2 || MsgType == EAPOL_PAIR_MSG_3) {
		/* Check RSN IE whether it is WPA2/WPA2PSK */
		if (!RTMPCheckRSNIE
		    (pAd, pKeyData, KeyDataLen, pEntry, &skip_offset)) {
			/* send wireless event - for RSN IE different */
			if (pAd->CommonCfg.bWirelessEvent)
				RTMPSendWirelessEvent(pAd,
						      IW_RSNIE_DIFF_EVENT_FLAG,
						      pEntry->Addr,
						      pEntry->apidx, 0);

			DBGPRINT(RT_DEBUG_ERROR,
				 ("RSN_IE Different in msg %d of 4-way handshake!\n",
				  MsgType));
			hex_dump("Receive RSN_IE ", pKeyData, KeyDataLen);
			hex_dump("Desired RSN_IE ", pEntry->RSN_IE,
				 pEntry->RSNIE_Len);

			return FALSE;
		} else {
			if (bWPA2 && MsgType == EAPOL_PAIR_MSG_3) {
				WpaShowAllsuite(pMyKeyData, skip_offset);

				/* skip RSN IE */
				pMyKeyData += skip_offset;
				KeyDataLength -= skip_offset;
				DBGPRINT(RT_DEBUG_TRACE,
					 ("RTMPParseEapolKeyData ==> WPA2/WPA2PSK RSN IE matched in Msg 3, Length(%d) \n",
					  skip_offset));
			} else
				return TRUE;
		}
	}

	DBGPRINT(RT_DEBUG_TRACE,
		 ("RTMPParseEapolKeyData ==> KeyDataLength %d without RSN_IE \n",
		  KeyDataLength));
	/*hex_dump("remain data", pMyKeyData, KeyDataLength); */

	/* Parse EKD format in pairwise_msg_3_WPA2 && group_msg_1_WPA2 */
	if (bWPA2
	    && (MsgType == EAPOL_PAIR_MSG_3 || MsgType == EAPOL_GROUP_MSG_1)) {
		if (KeyDataLength >= 8)	/* KDE format exclude GTK length */
		{
			pKDE = (struct rt_kde_encap *) pMyKeyData;

			DefaultIdx = pKDE->GTKEncap.Kid;

			/* Sanity check - KED length */
			if (KeyDataLength < (pKDE->Len + 2)) {
				DBGPRINT(RT_DEBUG_ERROR,
					 ("ERROR: The len from KDE is too short \n"));
				return FALSE;
			}
			/* Get GTK length - refer to IEEE 802.11i-2004 p.82 */
			GTKLEN = pKDE->Len - 6;
			if (GTKLEN < LEN_AES_KEY) {
				DBGPRINT(RT_DEBUG_ERROR,
					 ("ERROR: GTK Key length is too short (%d) \n",
					  GTKLEN));
				return FALSE;
			}

		} else {
			DBGPRINT(RT_DEBUG_ERROR,
				 ("ERROR: KDE format length is too short \n"));
			return FALSE;
		}

		DBGPRINT(RT_DEBUG_TRACE,
			 ("GTK in KDE format ,DefaultKeyID=%d, KeyLen=%d \n",
			  DefaultIdx, GTKLEN));
		/* skip it */
		pMyKeyData += 8;
		KeyDataLength -= 8;

	} else if (!bWPA2 && MsgType == EAPOL_GROUP_MSG_1) {
		DefaultIdx = GroupKeyIndex;
		DBGPRINT(RT_DEBUG_TRACE,
			 ("GTK DefaultKeyID=%d \n", DefaultIdx));
	}
	/* Sanity check - shared key index must be 1 ~ 3 */
	if (DefaultIdx < 1 || DefaultIdx > 3) {
		DBGPRINT(RT_DEBUG_ERROR,
			 ("ERROR: GTK Key index(%d) is invalid in %s %s \n",
			  DefaultIdx, ((bWPA2) ? "WPA2" : "WPA"),
			  GetEapolMsgType(MsgType)));
		return FALSE;
	}

	{
		struct rt_cipher_key *pSharedKey;

		/* set key material, TxMic and RxMic */
		NdisMoveMemory(pAd->StaCfg.GTK, pMyKeyData, 32);
		pAd->StaCfg.DefaultKeyId = DefaultIdx;

		pSharedKey = &pAd->SharedKey[BSS0][pAd->StaCfg.DefaultKeyId];

		/* Prepare pair-wise key information into shared key table */
		NdisZeroMemory(pSharedKey, sizeof(struct rt_cipher_key));
		pSharedKey->KeyLen = LEN_TKIP_EK;
		NdisMoveMemory(pSharedKey->Key, pAd->StaCfg.GTK, LEN_TKIP_EK);
		NdisMoveMemory(pSharedKey->RxMic, &pAd->StaCfg.GTK[16],
			       LEN_TKIP_RXMICK);
		NdisMoveMemory(pSharedKey->TxMic, &pAd->StaCfg.GTK[24],
			       LEN_TKIP_TXMICK);

		/* Update Shared Key CipherAlg */
		pSharedKey->CipherAlg = CIPHER_NONE;
		if (pAd->StaCfg.GroupCipher == Ndis802_11Encryption2Enabled)
			pSharedKey->CipherAlg = CIPHER_TKIP;
		else if (pAd->StaCfg.GroupCipher ==
			 Ndis802_11Encryption3Enabled)
			pSharedKey->CipherAlg = CIPHER_AES;
		else if (pAd->StaCfg.GroupCipher == Ndis802_11GroupWEP40Enabled)
			pSharedKey->CipherAlg = CIPHER_WEP64;
		else if (pAd->StaCfg.GroupCipher ==
			 Ndis802_11GroupWEP104Enabled)
			pSharedKey->CipherAlg = CIPHER_WEP128;

		/* Update group key information to ASIC Shared Key Table */
		AsicAddSharedKeyEntry(pAd,
				      BSS0,
				      pAd->StaCfg.DefaultKeyId,
				      pSharedKey->CipherAlg,
				      pSharedKey->Key,
				      pSharedKey->TxMic, pSharedKey->RxMic);

		/* Update ASIC WCID attribute table and IVEIV table */
		RTMPAddWcidAttributeEntry(pAd,
					  BSS0,
					  pAd->StaCfg.DefaultKeyId,
					  pSharedKey->CipherAlg, NULL);
	}

	return TRUE;

}

/*
	========================================================================

	Routine Description:
		Construct EAPoL message for WPA handshaking
		Its format is below,

		+--------------------+
		| Protocol Version	 |  1 octet
		+--------------------+
		| Protocol Type		 |	1 octet
		+--------------------+
		| Body Length		 |  2 octets
		+--------------------+
		| Descriptor Type	 |	1 octet
		+--------------------+
		| Key Information    |	2 octets
		+--------------------+
		| Key Length	     |  1 octet
		+--------------------+
		| Key Repaly Counter |	8 octets
		+--------------------+
		| Key Nonce		     |  32 octets
		+--------------------+
		| Key IV			 |  16 octets
		+--------------------+
		| Key RSC			 |  8 octets
		+--------------------+
		| Key ID or Reserved |	8 octets
		+--------------------+
		| Key MIC			 |	16 octets
		+--------------------+
		| Key Data Length	 |	2 octets
		+--------------------+
		| Key Data			 |	n octets
		+--------------------+

	Arguments:
		pAd			Pointer	to our adapter

	Return Value:
		None

	Note:

	========================================================================
*/
void ConstructEapolMsg(struct rt_mac_table_entry *pEntry,
		       u8 GroupKeyWepStatus,
		       u8 MsgType,
		       u8 DefaultKeyIdx,
		       u8 * KeyNonce,
		       u8 * TxRSC,
		       u8 * GTK,
		       u8 * RSNIE,
		       u8 RSNIE_Len, struct rt_eapol_packet * pMsg)
{
	BOOLEAN bWPA2 = FALSE;
	u8 KeyDescVer;

	/* Choose WPA2 or not */
	if ((pEntry->AuthMode == Ndis802_11AuthModeWPA2) ||
	    (pEntry->AuthMode == Ndis802_11AuthModeWPA2PSK))
		bWPA2 = TRUE;

	/* Init Packet and Fill header */
	pMsg->ProVer = EAPOL_VER;
	pMsg->ProType = EAPOLKey;

	/* Default 95 bytes, the EAPoL-Key descriptor exclude Key-data field */
	SET_u16_TO_ARRARY(pMsg->Body_Len, LEN_EAPOL_KEY_MSG);

	/* Fill in EAPoL descriptor */
	if (bWPA2)
		pMsg->KeyDesc.Type = WPA2_KEY_DESC;
	else
		pMsg->KeyDesc.Type = WPA1_KEY_DESC;

	/* Key Descriptor Version (bits 0-2) specifies the key descriptor version type */
	{
		/* Fill in Key information, refer to IEEE Std 802.11i-2004 page 78 */
		/* When either the pairwise or the group cipher is AES, the DESC_TYPE_AES(2) shall be used. */
		KeyDescVer =
		    (((pEntry->WepStatus == Ndis802_11Encryption3Enabled)
		      || (GroupKeyWepStatus ==
			  Ndis802_11Encryption3Enabled)) ? (DESC_TYPE_AES)
		     : (DESC_TYPE_TKIP));
	}

	pMsg->KeyDesc.KeyInfo.KeyDescVer = KeyDescVer;

	/* Specify Key Type as Group(0) or Pairwise(1) */
	if (MsgType >= EAPOL_GROUP_MSG_1)
		pMsg->KeyDesc.KeyInfo.KeyType = GROUPKEY;
	else
		pMsg->KeyDesc.KeyInfo.KeyType = PAIRWISEKEY;

	/* Specify Key Index, only group_msg1_WPA1 */
	if (!bWPA2 && (MsgType >= EAPOL_GROUP_MSG_1))
		pMsg->KeyDesc.KeyInfo.KeyIndex = DefaultKeyIdx;

	if (MsgType == EAPOL_PAIR_MSG_3)
		pMsg->KeyDesc.KeyInfo.Install = 1;

	if ((MsgType == EAPOL_PAIR_MSG_1) || (MsgType == EAPOL_PAIR_MSG_3)
	    || (MsgType == EAPOL_GROUP_MSG_1))
		pMsg->KeyDesc.KeyInfo.KeyAck = 1;

	if (MsgType != EAPOL_PAIR_MSG_1)
		pMsg->KeyDesc.KeyInfo.KeyMic = 1;

	if ((bWPA2 && (MsgType >= EAPOL_PAIR_MSG_3)) ||
	    (!bWPA2 && (MsgType >= EAPOL_GROUP_MSG_1))) {
		pMsg->KeyDesc.KeyInfo.Secure = 1;
	}

	if (bWPA2 && ((MsgType == EAPOL_PAIR_MSG_3) ||
		      (MsgType == EAPOL_GROUP_MSG_1))) {
		pMsg->KeyDesc.KeyInfo.EKD_DL = 1;
	}
	/* key Information element has done. */
	*(u16 *) (&pMsg->KeyDesc.KeyInfo) =
	    cpu2le16(*(u16 *) (&pMsg->KeyDesc.KeyInfo));

	/* Fill in Key Length */
	{
		if (MsgType >= EAPOL_GROUP_MSG_1) {
			/* the length of group key cipher */
			pMsg->KeyDesc.KeyLength[1] =
			    ((GroupKeyWepStatus ==
			      Ndis802_11Encryption2Enabled) ? TKIP_GTK_LENGTH :
			     LEN_AES_KEY);
		} else {
			/* the length of pairwise key cipher */
			pMsg->KeyDesc.KeyLength[1] =
			    ((pEntry->WepStatus ==
			      Ndis802_11Encryption2Enabled) ? LEN_TKIP_KEY :
			     LEN_AES_KEY);
		}
	}

	/* Fill in replay counter */
	NdisMoveMemory(pMsg->KeyDesc.ReplayCounter, pEntry->R_Counter,
		       LEN_KEY_DESC_REPLAY);

	/* Fill Key Nonce field */
	/* ANonce : pairwise_msg1 & pairwise_msg3 */
	/* SNonce : pairwise_msg2 */
	/* GNonce : group_msg1_wpa1 */
	if ((MsgType <= EAPOL_PAIR_MSG_3)
	    || ((!bWPA2 && (MsgType == EAPOL_GROUP_MSG_1))))
		NdisMoveMemory(pMsg->KeyDesc.KeyNonce, KeyNonce,
			       LEN_KEY_DESC_NONCE);

	/* Fill key IV - WPA2 as 0, WPA1 as random */
	if (!bWPA2 && (MsgType == EAPOL_GROUP_MSG_1)) {
		/* Suggest IV be random number plus some number, */
		NdisMoveMemory(pMsg->KeyDesc.KeyIv, &KeyNonce[16],
			       LEN_KEY_DESC_IV);
		pMsg->KeyDesc.KeyIv[15] += 2;
	}
	/* Fill Key RSC field */
	/* It contains the RSC for the GTK being installed. */
	if ((MsgType == EAPOL_PAIR_MSG_3 && bWPA2)
	    || (MsgType == EAPOL_GROUP_MSG_1)) {
		NdisMoveMemory(pMsg->KeyDesc.KeyRsc, TxRSC, 6);
	}
	/* Clear Key MIC field for MIC calculation later */
	NdisZeroMemory(pMsg->KeyDesc.KeyMic, LEN_KEY_DESC_MIC);

	ConstructEapolKeyData(pEntry,
			      GroupKeyWepStatus,
			      KeyDescVer,
			      MsgType,
			      DefaultKeyIdx, GTK, RSNIE, RSNIE_Len, pMsg);

	/* Calculate MIC and fill in KeyMic Field except Pairwise Msg 1. */
	if (MsgType != EAPOL_PAIR_MSG_1) {
		CalculateMIC(KeyDescVer, pEntry->PTK, pMsg);
	}

	DBGPRINT(RT_DEBUG_TRACE,
		 ("===> ConstructEapolMsg for %s %s\n",
		  ((bWPA2) ? "WPA2" : "WPA"), GetEapolMsgType(MsgType)));
	DBGPRINT(RT_DEBUG_TRACE,
		 ("	     Body length = %d \n",
		  CONV_ARRARY_TO_u16(pMsg->Body_Len)));
	DBGPRINT(RT_DEBUG_TRACE,
		 ("	     Key length  = %d \n",
		  CONV_ARRARY_TO_u16(pMsg->KeyDesc.KeyLength)));

}

/*
	========================================================================

	Routine Description:
		Construct the Key Data field of EAPoL message

	Arguments:
		pAd			Pointer	to our adapter
		Elem		Message body

	Return Value:
		None

	Note:

	========================================================================
*/
void ConstructEapolKeyData(struct rt_mac_table_entry *pEntry,
			   u8 GroupKeyWepStatus,
			   u8 keyDescVer,
			   u8 MsgType,
			   u8 DefaultKeyIdx,
			   u8 * GTK,
			   u8 * RSNIE,
			   u8 RSNIE_LEN, struct rt_eapol_packet * pMsg)
{
	u8 *mpool, *Key_Data, *Rc4GTK;
	u8 ekey[(LEN_KEY_DESC_IV + LEN_EAP_EK)];
	unsigned long data_offset;
	BOOLEAN bWPA2Capable = FALSE;
	struct rt_rtmp_adapter *pAd = pEntry->pAd;
	BOOLEAN GTK_Included = FALSE;

	/* Choose WPA2 or not */
	if ((pEntry->AuthMode == Ndis802_11AuthModeWPA2) ||
	    (pEntry->AuthMode == Ndis802_11AuthModeWPA2PSK))
		bWPA2Capable = TRUE;

	if (MsgType == EAPOL_PAIR_MSG_1 ||
	    MsgType == EAPOL_PAIR_MSG_4 || MsgType == EAPOL_GROUP_MSG_2)
		return;

	/* allocate memory pool */
	os_alloc_mem(NULL, (u8 **) & mpool, 1500);

	if (mpool == NULL)
		return;

	/* Rc4GTK Len = 512 */
	Rc4GTK = (u8 *) ROUND_UP(mpool, 4);
	/* Key_Data Len = 512 */
	Key_Data = (u8 *) ROUND_UP(Rc4GTK + 512, 4);

	NdisZeroMemory(Key_Data, 512);
	SET_u16_TO_ARRARY(pMsg->KeyDesc.KeyDataLen, 0);
	data_offset = 0;

	/* Encapsulate RSNIE in pairwise_msg2 & pairwise_msg3 */
	if (RSNIE_LEN
	    && ((MsgType == EAPOL_PAIR_MSG_2)
		|| (MsgType == EAPOL_PAIR_MSG_3))) {
		u8 *pmkid_ptr = NULL;
		u8 pmkid_len = 0;

		RTMPInsertRSNIE(&Key_Data[data_offset],
				&data_offset,
				RSNIE, RSNIE_LEN, pmkid_ptr, pmkid_len);
	}

	/* Encapsulate KDE format in pairwise_msg3_WPA2 & group_msg1_WPA2 */
	if (bWPA2Capable
	    && ((MsgType == EAPOL_PAIR_MSG_3)
		|| (MsgType == EAPOL_GROUP_MSG_1))) {
		/* Key Data Encapsulation (KDE) format - 802.11i-2004  Figure-43w and Table-20h */
		Key_Data[data_offset + 0] = 0xDD;

		if (GroupKeyWepStatus == Ndis802_11Encryption3Enabled) {
			Key_Data[data_offset + 1] = 0x16;	/* 4+2+16(OUI+DataType+DataField) */
		} else {
			Key_Data[data_offset + 1] = 0x26;	/* 4+2+32(OUI+DataType+DataField) */
		}

		Key_Data[data_offset + 2] = 0x00;
		Key_Data[data_offset + 3] = 0x0F;
		Key_Data[data_offset + 4] = 0xAC;
		Key_Data[data_offset + 5] = 0x01;

		/* GTK KDE format - 802.11i-2004  Figure-43x */
		Key_Data[data_offset + 6] = (DefaultKeyIdx & 0x03);
		Key_Data[data_offset + 7] = 0x00;	/* Reserved Byte */

		data_offset += 8;
	}

	/* Encapsulate GTK */
	/* Only for pairwise_msg3_WPA2 and group_msg1 */
	if ((MsgType == EAPOL_PAIR_MSG_3 && bWPA2Capable)
	    || (MsgType == EAPOL_GROUP_MSG_1)) {
		/* Fill in GTK */
		if (GroupKeyWepStatus == Ndis802_11Encryption3Enabled) {
			NdisMoveMemory(&Key_Data[data_offset], GTK,
				       LEN_AES_KEY);
			data_offset += LEN_AES_KEY;
		} else {
			NdisMoveMemory(&Key_Data[data_offset], GTK,
				       TKIP_GTK_LENGTH);
			data_offset += TKIP_GTK_LENGTH;
		}

		GTK_Included = TRUE;
	}

	/* This whole key-data field shall be encrypted if a GTK is included. */
	/* Encrypt the data material in key data field with KEK */
	if (GTK_Included) {
		/*hex_dump("GTK_Included", Key_Data, data_offset); */

		if ((keyDescVer == DESC_TYPE_AES)) {
			u8 remainder = 0;
			u8 pad_len = 0;

			/* Key Descriptor Version 2 or 3: AES key wrap, defined in IETF RFC 3394, */
			/* shall be used to encrypt the Key Data field using the KEK field from */
			/* the derived PTK. */

			/* If the Key Data field uses the NIST AES key wrap, then the Key Data field */
			/* shall be padded before encrypting if the key data length is less than 16 */
			/* octets or if it is not a multiple of 8. The padding consists of appending */
			/* a single octet 0xdd followed by zero or more 0x00 octets. */
			if ((remainder = data_offset & 0x07) != 0) {
				int i;

				pad_len = (8 - remainder);
				Key_Data[data_offset] = 0xDD;
				for (i = 1; i < pad_len; i++)
					Key_Data[data_offset + i] = 0;

				data_offset += pad_len;
			}

			AES_GTK_KEY_WRAP(&pEntry->PTK[16], Key_Data,
					 data_offset, Rc4GTK);
			/* AES wrap function will grow 8 bytes in length */
			data_offset += 8;
		} else {
			/*      Key Descriptor Version 1: ARC4 is used to encrypt the Key Data field
			   using the KEK field from the derived PTK. */

			/* PREPARE Encrypted  "Key DATA" field.  (Encrypt GTK with RC4, usinf PTK[16]->[31] as Key, IV-field as IV) */
			/* put TxTsc in Key RSC field */
			pAd->PrivateInfo.FCSCRC32 = PPPINITFCS32;	/*Init crc32. */

			/* ekey is the contanetion of IV-field, and PTK[16]->PTK[31] */
			NdisMoveMemory(ekey, pMsg->KeyDesc.KeyIv,
				       LEN_KEY_DESC_IV);
			NdisMoveMemory(&ekey[LEN_KEY_DESC_IV], &pEntry->PTK[16],
				       LEN_EAP_EK);
			ARCFOUR_INIT(&pAd->PrivateInfo.WEPCONTEXT, ekey, sizeof(ekey));	/*INIT SBOX, KEYLEN+3(IV) */
			pAd->PrivateInfo.FCSCRC32 =
			    RTMP_CALC_FCS32(pAd->PrivateInfo.FCSCRC32, Key_Data,
					    data_offset);
			WPAARCFOUR_ENCRYPT(&pAd->PrivateInfo.WEPCONTEXT, Rc4GTK,
					   Key_Data, data_offset);
		}

		NdisMoveMemory(pMsg->KeyDesc.KeyData, Rc4GTK, data_offset);
	} else {
		NdisMoveMemory(pMsg->KeyDesc.KeyData, Key_Data, data_offset);
	}

	/* Update key data length field and total body length */
	SET_u16_TO_ARRARY(pMsg->KeyDesc.KeyDataLen, data_offset);
	INC_u16_TO_ARRARY(pMsg->Body_Len, data_offset);

	os_free_mem(NULL, mpool);

}

/*
	========================================================================

	Routine Description:
		Calcaulate MIC. It is used during 4-ways handsharking.

	Arguments:
		pAd			-	pointer to our pAdapter context
	PeerWepStatus	-	indicate the encryption type

	Return Value:

	Note:

	========================================================================
*/
static void CalculateMIC(u8 KeyDescVer,
			 u8 * PTK, struct rt_eapol_packet * pMsg)
{
	u8 *OutBuffer;
	unsigned long FrameLen = 0;
	u8 mic[LEN_KEY_DESC_MIC];
	u8 digest[80];

	/* allocate memory for MIC calculation */
	os_alloc_mem(NULL, (u8 **) & OutBuffer, 512);

	if (OutBuffer == NULL) {
		DBGPRINT(RT_DEBUG_ERROR, ("CalculateMIC: no memory!\n"));
		return;
	}
	/* make a frame for calculating MIC. */
	MakeOutgoingFrame(OutBuffer, &FrameLen,
			  CONV_ARRARY_TO_u16(pMsg->Body_Len) + 4, pMsg,
			  END_OF_ARGS);

	NdisZeroMemory(mic, sizeof(mic));

	/* Calculate MIC */
	if (KeyDescVer == DESC_TYPE_AES) {
		HMAC_SHA1(PTK, LEN_EAP_MICK, OutBuffer, FrameLen, digest,
			  SHA1_DIGEST_SIZE);
		NdisMoveMemory(mic, digest, LEN_KEY_DESC_MIC);
	} else {
		HMAC_MD5(PTK, LEN_EAP_MICK, OutBuffer, FrameLen, mic,
			 MD5_DIGEST_SIZE);
	}

	/* store the calculated MIC */
	NdisMoveMemory(pMsg->KeyDesc.KeyMic, mic, LEN_KEY_DESC_MIC);

	os_free_mem(NULL, OutBuffer);
}

/*
	========================================================================

	Routine Description:
		Some received frames can't decrypt by Asic, so decrypt them by software.

	Arguments:
		pAd			-	pointer to our pAdapter context
	PeerWepStatus	-	indicate the encryption type

	Return Value:
		NDIS_STATUS_SUCCESS		-	decryption successful
		NDIS_STATUS_FAILURE		-	decryption failure

	========================================================================
*/
int RTMPSoftDecryptBroadCastData(struct rt_rtmp_adapter *pAd,
					 struct rt_rx_blk *pRxBlk,
					 IN NDIS_802_11_ENCRYPTION_STATUS
					 GroupCipher, struct rt_cipher_key *pShard_key)
{
	struct rt_rxwi * pRxWI = pRxBlk->pRxWI;

	/* handle WEP decryption */
	if (GroupCipher == Ndis802_11Encryption1Enabled) {
		if (RTMPSoftDecryptWEP
		    (pAd, pRxBlk->pData, pRxWI->MPDUtotalByteCount,
		     pShard_key)) {

			/*Minus IV[4] & ICV[4] */
			pRxWI->MPDUtotalByteCount -= 8;
		} else {
			DBGPRINT(RT_DEBUG_ERROR,
				 ("ERROR : Software decrypt WEP data fails.\n"));
			/* give up this frame */
			return NDIS_STATUS_FAILURE;
		}
	}
	/* handle TKIP decryption */
	else if (GroupCipher == Ndis802_11Encryption2Enabled) {
		if (RTMPSoftDecryptTKIP
		    (pAd, pRxBlk->pData, pRxWI->MPDUtotalByteCount, 0,
		     pShard_key)) {

			/*Minus 8 bytes MIC, 8 bytes IV/EIV, 4 bytes ICV */
			pRxWI->MPDUtotalByteCount -= 20;
		} else {
			DBGPRINT(RT_DEBUG_ERROR,
				 ("ERROR : RTMPSoftDecryptTKIP Failed\n"));
			/* give up this frame */
			return NDIS_STATUS_FAILURE;
		}
	}
	/* handle AES decryption */
	else if (GroupCipher == Ndis802_11Encryption3Enabled) {
		if (RTMPSoftDecryptAES
		    (pAd, pRxBlk->pData, pRxWI->MPDUtotalByteCount,
		     pShard_key)) {

			/*8 bytes MIC, 8 bytes IV/EIV (CCMP Header) */
			pRxWI->MPDUtotalByteCount -= 16;
		} else {
			DBGPRINT(RT_DEBUG_ERROR,
				 ("ERROR : RTMPSoftDecryptAES Failed\n"));
			/* give up this frame */
			return NDIS_STATUS_FAILURE;
		}
	} else {
		/* give up this frame */
		return NDIS_STATUS_FAILURE;
	}

	return NDIS_STATUS_SUCCESS;

}

u8 *GetSuiteFromRSNIE(u8 *rsnie,
			 u32 rsnie_len, u8 type, u8 * count)
{
	struct rt_eid * pEid;
	int len;
	u8 *pBuf;
	int offset = 0;
	struct rt_rsnie_auth *pAkm;
	u16 acount;
	BOOLEAN isWPA2 = FALSE;

	pEid = (struct rt_eid *) rsnie;
	len = rsnie_len - 2;	/* exclude IE and length */
	pBuf = (u8 *)& pEid->Octet[0];

	/* set default value */
	*count = 0;

	/* Check length */
	if ((len <= 0) || (pEid->Len != len)) {
		DBGPRINT_ERR("%s : The length is invalid\n", __func__);
		return NULL;
	}
	/* Check WPA or WPA2 */
	if (pEid->Eid == IE_WPA) {
		struct rt_rsnie *pRsnie = (struct rt_rsnie *)pBuf;
		u16 ucount;

		if (len < sizeof(struct rt_rsnie)) {
			DBGPRINT_ERR("%s : The length is too short for WPA\n", __func__);
			return NULL;
		}
		/* Get the count of pairwise cipher */
		ucount = cpu2le16(pRsnie->ucount);
		if (ucount > 2) {
			DBGPRINT_ERR("%s : The count(%d) of pairwise cipher is invlaid\n", __func__, ucount);
			return NULL;
		}
		/* Get the group cipher */
		if (type == GROUP_SUITE) {
			*count = 1;
			return pRsnie->mcast;
		}
		/* Get the pairwise cipher suite */
		else if (type == PAIRWISE_SUITE) {
			DBGPRINT(RT_DEBUG_TRACE,
				 ("%s : The count of pairwise cipher is %d\n",
				  __func__, ucount));
			*count = ucount;
			return pRsnie->ucast[0].oui;
		}

		offset = sizeof(struct rt_rsnie) + (4 * (ucount - 1));

	} else if (pEid->Eid == IE_RSN) {
		struct rt_rsnie2 *pRsnie = (struct rt_rsnie2 *)pBuf;
		u16 ucount;

		isWPA2 = TRUE;

		if (len < sizeof(struct rt_rsnie2)) {
			DBGPRINT_ERR("%s : The length is too short for WPA2\n", __func__);
			return NULL;
		}
		/* Get the count of pairwise cipher */
		ucount = cpu2le16(pRsnie->ucount);
		if (ucount > 2) {
			DBGPRINT_ERR("%s : The count(%d) of pairwise cipher is invlaid\n", __func__, ucount);
			return NULL;
		}
		/* Get the group cipher */
		if (type == GROUP_SUITE) {
			*count = 1;
			return pRsnie->mcast;
		}
		/* Get the pairwise cipher suite */
		else if (type == PAIRWISE_SUITE) {
			DBGPRINT(RT_DEBUG_TRACE,
				 ("%s : The count of pairwise cipher is %d\n",
				  __func__, ucount));
			*count = ucount;
			return pRsnie->ucast[0].oui;
		}

		offset = sizeof(struct rt_rsnie2) + (4 * (ucount - 1));

	} else {
		DBGPRINT_ERR("%s : Unknown IE (%d)\n", __func__, pEid->Eid);
		return NULL;
	}

	/* skip group cipher and pairwise cipher suite */
	pBuf += offset;
	len -= offset;

	if (len < sizeof(struct rt_rsnie_auth)) {
		DBGPRINT_ERR("%s : The length of RSNIE is too short\n", __func__);
		return NULL;
	}
	/* pointer to AKM count */
	pAkm = (struct rt_rsnie_auth *)pBuf;

	/* Get the count of pairwise cipher */
	acount = cpu2le16(pAkm->acount);
	if (acount > 2) {
		DBGPRINT_ERR("%s : The count(%d) of AKM is invlaid\n", __func__, acount);
		return NULL;
	}
	/* Get the AKM suite */
	if (type == AKM_SUITE) {
		DBGPRINT(RT_DEBUG_TRACE, ("%s : The count of AKM is %d\n",
					  __func__, acount));
		*count = acount;
		return pAkm->auth[0].oui;
	}
	offset = sizeof(struct rt_rsnie_auth) + (4 * (acount - 1));

	pBuf += offset;
	len -= offset;

	/* The remaining length must larger than (RSN-Capability(2) + PMKID-Count(2) + PMKID(16~)) */
	if (len >= (sizeof(RSN_CAPABILITIES) + 2 + LEN_PMKID)) {
		/* Skip RSN capability and PMKID-Count */
		pBuf += (sizeof(RSN_CAPABILITIES) + 2);
		len -= (sizeof(RSN_CAPABILITIES) + 2);

		/* Get PMKID */
		if (type == PMKID_LIST) {
			*count = 1;
			return pBuf;
		}
	} else {
		DBGPRINT_ERR("%s : it can't get any more information beyond AKM \n", __func__);
		return NULL;
	}

	*count = 0;
	/*DBGPRINT_ERR(("%s : The type(%d) doesn't support \n", __func__, type)); */
	return NULL;

}

void WpaShowAllsuite(u8 *rsnie, u32 rsnie_len)
{
	u8 *pSuite = NULL;
	u8 count;

	hex_dump("RSNIE", rsnie, rsnie_len);

	/* group cipher */
	pSuite = GetSuiteFromRSNIE(rsnie, rsnie_len, GROUP_SUITE, &count);
	if (pSuite != NULL) {
		hex_dump("group cipher", pSuite, 4 * count);
	}
	/* pairwise cipher */
	pSuite = GetSuiteFromRSNIE(rsnie, rsnie_len, PAIRWISE_SUITE, &count);
	if (pSuite != NULL) {
		hex_dump("pairwise cipher", pSuite, 4 * count);
	}
	/* AKM */
	pSuite = GetSuiteFromRSNIE(rsnie, rsnie_len, AKM_SUITE, &count);
	if (pSuite != NULL) {
		hex_dump("AKM suite", pSuite, 4 * count);
	}
	/* PMKID */
	pSuite = GetSuiteFromRSNIE(rsnie, rsnie_len, PMKID_LIST, &count);
	if (pSuite != NULL) {
		hex_dump("PMKID", pSuite, LEN_PMKID);
	}

}

void RTMPInsertRSNIE(u8 *pFrameBuf,
		     unsigned long *pFrameLen,
		     u8 *rsnie_ptr,
		     u8 rsnie_len,
		     u8 *pmkid_ptr, u8 pmkid_len)
{
	u8 *pTmpBuf;
	unsigned long TempLen = 0;
	u8 extra_len = 0;
	u16 pmk_count = 0;
	u8 ie_num;
	u8 total_len = 0;
	u8 WPA2_OUI[3] = { 0x00, 0x0F, 0xAC };

	pTmpBuf = pFrameBuf;

	/* PMKID-List Must larger than 0 and the multiple of 16. */
	if (pmkid_len > 0 && ((pmkid_len & 0x0f) == 0)) {
		extra_len = sizeof(u16)+ pmkid_len;

		pmk_count = (pmkid_len >> 4);
		pmk_count = cpu2le16(pmk_count);
	} else {
		DBGPRINT(RT_DEBUG_WARN,
			 ("%s : The length is PMKID-List is invalid (%d), so don't insert it.\n",
			  __func__, pmkid_len));
	}

	if (rsnie_len != 0) {
		ie_num = IE_WPA;
		total_len = rsnie_len;

		if (NdisEqualMemory(rsnie_ptr + 2, WPA2_OUI, sizeof(WPA2_OUI))) {
			ie_num = IE_RSN;
			total_len += extra_len;
		}

		/* construct RSNIE body */
		MakeOutgoingFrame(pTmpBuf, &TempLen,
				  1, &ie_num,
				  1, &total_len,
				  rsnie_len, rsnie_ptr, END_OF_ARGS);

		pTmpBuf += TempLen;
		*pFrameLen = *pFrameLen + TempLen;

		if (ie_num == IE_RSN) {
			/* Insert PMKID-List field */
			if (extra_len > 0) {
				MakeOutgoingFrame(pTmpBuf, &TempLen,
						  2, &pmk_count,
						  pmkid_len, pmkid_ptr,
						  END_OF_ARGS);

				pTmpBuf += TempLen;
				*pFrameLen = *pFrameLen + TempLen;
			}
		}
	}

	return;
}