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
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
|
/*
* Wi-Fi Direct - P2P module
* Copyright (c) 2009-2010, Atheros Communications
*
* This software may be distributed under the terms of the BSD license.
* See README for more details.
*/
#include "includes.h"
#include "common.h"
#include "eloop.h"
#include "common/ieee802_11_defs.h"
#include "common/ieee802_11_common.h"
#include "common/wpa_ctrl.h"
#include "wps/wps_i.h"
#include "p2p_i.h"
#include "p2p.h"
static void p2p_state_timeout(void *eloop_ctx, void *timeout_ctx);
static void p2p_device_free(struct p2p_data *p2p, struct p2p_device *dev);
static void p2p_process_presence_req(struct p2p_data *p2p, const u8 *da,
const u8 *sa, const u8 *data, size_t len,
int rx_freq);
static void p2p_process_presence_resp(struct p2p_data *p2p, const u8 *da,
const u8 *sa, const u8 *data,
size_t len);
static void p2p_ext_listen_timeout(void *eloop_ctx, void *timeout_ctx);
static void p2p_scan_timeout(void *eloop_ctx, void *timeout_ctx);
/*
* p2p_scan recovery timeout
*
* Many drivers are using 30 second timeout on scan results. Allow a bit larger
* timeout for this to avoid hitting P2P timeout unnecessarily.
*/
#define P2P_SCAN_TIMEOUT 35
/**
* P2P_PEER_EXPIRATION_AGE - Number of seconds after which inactive peer
* entries will be removed
*/
#ifdef ANDROID_P2P
#define P2P_PEER_EXPIRATION_AGE 30
#else
#define P2P_PEER_EXPIRATION_AGE 300
#endif
#define P2P_PEER_EXPIRATION_INTERVAL (P2P_PEER_EXPIRATION_AGE / 2)
#ifdef ANDROID_P2P
int p2p_connection_in_progress(struct p2p_data *p2p)
{
int ret = 0;
switch (p2p->state) {
case P2P_CONNECT:
case P2P_CONNECT_LISTEN:
case P2P_GO_NEG:
case P2P_WAIT_PEER_CONNECT:
case P2P_WAIT_PEER_IDLE:
case P2P_PROVISIONING:
case P2P_INVITE:
case P2P_INVITE_LISTEN:
ret = 1;
break;
default:
wpa_printf(MSG_DEBUG, "p2p_connection_in_progress state %d", p2p->state);
ret = 0;
}
return ret;
}
#endif
static void p2p_expire_peers(struct p2p_data *p2p)
{
struct p2p_device *dev, *n;
struct os_time now;
size_t i;
os_get_time(&now);
dl_list_for_each_safe(dev, n, &p2p->devices, struct p2p_device, list) {
if (dev->last_seen.sec + P2P_PEER_EXPIRATION_AGE >= now.sec)
continue;
if (p2p->cfg->go_connected &&
p2p->cfg->go_connected(p2p->cfg->cb_ctx,
dev->info.p2p_device_addr)) {
/*
* We are connected as a client to a group in which the
* peer is the GO, so do not expire the peer entry.
*/
os_get_time(&dev->last_seen);
continue;
}
for (i = 0; i < p2p->num_groups; i++) {
if (p2p_group_is_client_connected(
p2p->groups[i], dev->info.p2p_device_addr))
break;
}
if (i < p2p->num_groups) {
/*
* The peer is connected as a client in a group where
* we are the GO, so do not expire the peer entry.
*/
os_get_time(&dev->last_seen);
continue;
}
#ifdef ANDROID_P2P
/* If Connection is in progress, don't expire the peer
*/
if (p2p_connection_in_progress(p2p))
continue;
#endif
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Expiring old peer "
"entry " MACSTR, MAC2STR(dev->info.p2p_device_addr));
#ifdef ANDROID_P2P
/* SD_FAIR_POLICY: Update the current sd_dev_list pointer to next device */
if(&dev->list == p2p->sd_dev_list)
p2p->sd_dev_list = dev->list.next;
#endif
dl_list_del(&dev->list);
p2p_device_free(p2p, dev);
}
}
static void p2p_expiration_timeout(void *eloop_ctx, void *timeout_ctx)
{
struct p2p_data *p2p = eloop_ctx;
p2p_expire_peers(p2p);
eloop_register_timeout(P2P_PEER_EXPIRATION_INTERVAL, 0,
p2p_expiration_timeout, p2p, NULL);
}
static const char * p2p_state_txt(int state)
{
switch (state) {
case P2P_IDLE:
return "IDLE";
case P2P_SEARCH:
return "SEARCH";
case P2P_CONNECT:
return "CONNECT";
case P2P_CONNECT_LISTEN:
return "CONNECT_LISTEN";
case P2P_GO_NEG:
return "GO_NEG";
case P2P_LISTEN_ONLY:
return "LISTEN_ONLY";
case P2P_WAIT_PEER_CONNECT:
return "WAIT_PEER_CONNECT";
case P2P_WAIT_PEER_IDLE:
return "WAIT_PEER_IDLE";
case P2P_SD_DURING_FIND:
return "SD_DURING_FIND";
case P2P_PROVISIONING:
return "PROVISIONING";
case P2P_PD_DURING_FIND:
return "PD_DURING_FIND";
case P2P_INVITE:
return "INVITE";
case P2P_INVITE_LISTEN:
return "INVITE_LISTEN";
case P2P_SEARCH_WHEN_READY:
return "SEARCH_WHEN_READY";
case P2P_CONTINUE_SEARCH_WHEN_READY:
return "CONTINUE_SEARCH_WHEN_READY";
default:
return "?";
}
}
u16 p2p_get_provisioning_info(struct p2p_data *p2p, const u8 *addr)
{
struct p2p_device *dev = NULL;
if (!addr || !p2p)
return 0;
dev = p2p_get_device(p2p, addr);
if (dev)
return dev->wps_prov_info;
else
return 0;
}
void p2p_clear_provisioning_info(struct p2p_data *p2p, const u8 *addr)
{
struct p2p_device *dev = NULL;
if (!addr || !p2p)
return;
dev = p2p_get_device(p2p, addr);
if (dev)
dev->wps_prov_info = 0;
}
void p2p_set_state(struct p2p_data *p2p, int new_state)
{
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: State %s -> %s",
p2p_state_txt(p2p->state), p2p_state_txt(new_state));
p2p->state = new_state;
}
void p2p_set_timeout(struct p2p_data *p2p, unsigned int sec, unsigned int usec)
{
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: Set timeout (state=%s): %u.%06u sec",
p2p_state_txt(p2p->state), sec, usec);
eloop_cancel_timeout(p2p_state_timeout, p2p, NULL);
eloop_register_timeout(sec, usec, p2p_state_timeout, p2p, NULL);
}
void p2p_clear_timeout(struct p2p_data *p2p)
{
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Clear timeout (state=%s)",
p2p_state_txt(p2p->state));
eloop_cancel_timeout(p2p_state_timeout, p2p, NULL);
}
void p2p_go_neg_failed(struct p2p_data *p2p, struct p2p_device *peer,
int status)
{
struct p2p_go_neg_results res;
p2p_clear_timeout(p2p);
p2p_set_state(p2p, P2P_IDLE);
if (p2p->go_neg_peer)
p2p->go_neg_peer->wps_method = WPS_NOT_READY;
p2p->go_neg_peer = NULL;
os_memset(&res, 0, sizeof(res));
res.status = status;
if (peer) {
os_memcpy(res.peer_device_addr, peer->info.p2p_device_addr,
ETH_ALEN);
os_memcpy(res.peer_interface_addr, peer->intended_addr,
ETH_ALEN);
}
p2p->cfg->go_neg_completed(p2p->cfg->cb_ctx, &res);
}
static void p2p_listen_in_find(struct p2p_data *p2p)
{
unsigned int r, tu;
int freq;
struct wpabuf *ies;
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: Starting short listen state (state=%s)",
p2p_state_txt(p2p->state));
freq = p2p_channel_to_freq(p2p->cfg->country, p2p->cfg->reg_class,
p2p->cfg->channel);
if (freq < 0) {
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: Unknown regulatory class/channel");
return;
}
os_get_random((u8 *) &r, sizeof(r));
tu = (r % ((p2p->max_disc_int - p2p->min_disc_int) + 1) +
p2p->min_disc_int) * 100;
p2p->pending_listen_freq = freq;
p2p->pending_listen_sec = 0;
p2p->pending_listen_usec = 1024 * tu;
ies = p2p_build_probe_resp_ies(p2p);
if (ies == NULL)
return;
if (p2p->cfg->start_listen(p2p->cfg->cb_ctx, freq, 1024 * tu / 1000,
ies) < 0) {
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: Failed to start listen mode");
p2p->pending_listen_freq = 0;
}
wpabuf_free(ies);
}
int p2p_listen(struct p2p_data *p2p, unsigned int timeout)
{
int freq;
struct wpabuf *ies;
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: Going to listen(only) state");
freq = p2p_channel_to_freq(p2p->cfg->country, p2p->cfg->reg_class,
p2p->cfg->channel);
if (freq < 0) {
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: Unknown regulatory class/channel");
return -1;
}
p2p->pending_listen_freq = freq;
p2p->pending_listen_sec = timeout / 1000;
p2p->pending_listen_usec = (timeout % 1000) * 1000;
if (p2p->p2p_scan_running) {
if (p2p->start_after_scan == P2P_AFTER_SCAN_CONNECT) {
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: p2p_scan running - connect is already "
"pending - skip listen");
return 0;
}
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: p2p_scan running - delay start of listen state");
p2p->start_after_scan = P2P_AFTER_SCAN_LISTEN;
return 0;
}
ies = p2p_build_probe_resp_ies(p2p);
if (ies == NULL)
return -1;
if (p2p->cfg->start_listen(p2p->cfg->cb_ctx, freq, timeout, ies) < 0) {
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: Failed to start listen mode");
p2p->pending_listen_freq = 0;
wpabuf_free(ies);
return -1;
}
wpabuf_free(ies);
p2p_set_state(p2p, P2P_LISTEN_ONLY);
return 0;
}
static void p2p_device_clear_reported(struct p2p_data *p2p)
{
struct p2p_device *dev;
dl_list_for_each(dev, &p2p->devices, struct p2p_device, list)
dev->flags &= ~P2P_DEV_REPORTED;
}
/**
* p2p_get_device - Fetch a peer entry
* @p2p: P2P module context from p2p_init()
* @addr: P2P Device Address of the peer
* Returns: Pointer to the device entry or %NULL if not found
*/
struct p2p_device * p2p_get_device(struct p2p_data *p2p, const u8 *addr)
{
struct p2p_device *dev;
dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
if (os_memcmp(dev->info.p2p_device_addr, addr, ETH_ALEN) == 0)
return dev;
}
return NULL;
}
/**
* p2p_get_device_interface - Fetch a peer entry based on P2P Interface Address
* @p2p: P2P module context from p2p_init()
* @addr: P2P Interface Address of the peer
* Returns: Pointer to the device entry or %NULL if not found
*/
struct p2p_device * p2p_get_device_interface(struct p2p_data *p2p,
const u8 *addr)
{
struct p2p_device *dev;
dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
if (os_memcmp(dev->interface_addr, addr, ETH_ALEN) == 0)
return dev;
}
return NULL;
}
/**
* p2p_create_device - Create a peer entry
* @p2p: P2P module context from p2p_init()
* @addr: P2P Device Address of the peer
* Returns: Pointer to the device entry or %NULL on failure
*
* If there is already an entry for the peer, it will be returned instead of
* creating a new one.
*/
static struct p2p_device * p2p_create_device(struct p2p_data *p2p,
const u8 *addr)
{
struct p2p_device *dev, *oldest = NULL;
size_t count = 0;
dev = p2p_get_device(p2p, addr);
if (dev)
return dev;
dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
count++;
if (oldest == NULL ||
os_time_before(&dev->last_seen, &oldest->last_seen))
oldest = dev;
}
if (count + 1 > p2p->cfg->max_peers && oldest) {
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: Remove oldest peer entry to make room for a new "
"peer");
#ifdef ANDROID_P2P
/* SD_FAIR_POLICY: Update the current sd_dev_list pointer to next device */
if(&oldest->list == p2p->sd_dev_list)
p2p->sd_dev_list = oldest->list.next;
#endif
dl_list_del(&oldest->list);
p2p_device_free(p2p, oldest);
}
dev = os_zalloc(sizeof(*dev));
if (dev == NULL)
return NULL;
dl_list_add(&p2p->devices, &dev->list);
os_memcpy(dev->info.p2p_device_addr, addr, ETH_ALEN);
return dev;
}
static void p2p_copy_client_info(struct p2p_device *dev,
struct p2p_client_info *cli)
{
os_memcpy(dev->info.device_name, cli->dev_name, cli->dev_name_len);
dev->info.device_name[cli->dev_name_len] = '\0';
dev->info.dev_capab = cli->dev_capab;
dev->info.config_methods = cli->config_methods;
os_memcpy(dev->info.pri_dev_type, cli->pri_dev_type, 8);
dev->info.wps_sec_dev_type_list_len = 8 * cli->num_sec_dev_types;
os_memcpy(dev->info.wps_sec_dev_type_list, cli->sec_dev_types,
dev->info.wps_sec_dev_type_list_len);
}
static int p2p_add_group_clients(struct p2p_data *p2p, const u8 *go_dev_addr,
const u8 *go_interface_addr, int freq,
const u8 *gi, size_t gi_len)
{
struct p2p_group_info info;
size_t c;
struct p2p_device *dev;
if (gi == NULL)
return 0;
if (p2p_group_info_parse(gi, gi_len, &info) < 0)
return -1;
/*
* Clear old data for this group; if the devices are still in the
* group, the information will be restored in the loop following this.
*/
dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
if (os_memcmp(dev->member_in_go_iface, go_interface_addr,
ETH_ALEN) == 0) {
os_memset(dev->member_in_go_iface, 0, ETH_ALEN);
os_memset(dev->member_in_go_dev, 0, ETH_ALEN);
}
}
for (c = 0; c < info.num_clients; c++) {
struct p2p_client_info *cli = &info.client[c];
if (os_memcmp(cli->p2p_device_addr, p2p->cfg->dev_addr,
ETH_ALEN) == 0)
continue; /* ignore our own entry */
dev = p2p_get_device(p2p, cli->p2p_device_addr);
if (dev) {
if (dev->flags & (P2P_DEV_GROUP_CLIENT_ONLY |
P2P_DEV_PROBE_REQ_ONLY)) {
/*
* Update information since we have not
* received this directly from the client.
*/
p2p_copy_client_info(dev, cli);
} else {
/*
* Need to update P2P Client Discoverability
* flag since it is valid only in P2P Group
* Info attribute.
*/
dev->info.dev_capab &=
~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
dev->info.dev_capab |=
cli->dev_capab &
P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
}
if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
dev->flags &= ~P2P_DEV_PROBE_REQ_ONLY;
}
} else {
dev = p2p_create_device(p2p, cli->p2p_device_addr);
if (dev == NULL)
continue;
dev->flags |= P2P_DEV_GROUP_CLIENT_ONLY;
p2p_copy_client_info(dev, cli);
dev->oper_freq = freq;
p2p->cfg->dev_found(p2p->cfg->cb_ctx,
dev->info.p2p_device_addr,
&dev->info, 1);
dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
}
os_memcpy(dev->interface_addr, cli->p2p_interface_addr,
ETH_ALEN);
os_get_time(&dev->last_seen);
os_memcpy(dev->member_in_go_dev, go_dev_addr, ETH_ALEN);
os_memcpy(dev->member_in_go_iface, go_interface_addr,
ETH_ALEN);
}
return 0;
}
static void p2p_copy_wps_info(struct p2p_device *dev, int probe_req,
const struct p2p_message *msg)
{
os_memcpy(dev->info.device_name, msg->device_name,
sizeof(dev->info.device_name));
if (msg->manufacturer &&
msg->manufacturer_len < sizeof(dev->info.manufacturer)) {
os_memset(dev->info.manufacturer, 0,
sizeof(dev->info.manufacturer));
os_memcpy(dev->info.manufacturer, msg->manufacturer,
msg->manufacturer_len);
}
if (msg->model_name &&
msg->model_name_len < sizeof(dev->info.model_name)) {
os_memset(dev->info.model_name, 0,
sizeof(dev->info.model_name));
os_memcpy(dev->info.model_name, msg->model_name,
msg->model_name_len);
}
if (msg->model_number &&
msg->model_number_len < sizeof(dev->info.model_number)) {
os_memset(dev->info.model_number, 0,
sizeof(dev->info.model_number));
os_memcpy(dev->info.model_number, msg->model_number,
msg->model_number_len);
}
if (msg->serial_number &&
msg->serial_number_len < sizeof(dev->info.serial_number)) {
os_memset(dev->info.serial_number, 0,
sizeof(dev->info.serial_number));
os_memcpy(dev->info.serial_number, msg->serial_number,
msg->serial_number_len);
}
if (msg->pri_dev_type)
os_memcpy(dev->info.pri_dev_type, msg->pri_dev_type,
sizeof(dev->info.pri_dev_type));
else if (msg->wps_pri_dev_type)
os_memcpy(dev->info.pri_dev_type, msg->wps_pri_dev_type,
sizeof(dev->info.pri_dev_type));
if (msg->wps_sec_dev_type_list) {
os_memcpy(dev->info.wps_sec_dev_type_list,
msg->wps_sec_dev_type_list,
msg->wps_sec_dev_type_list_len);
dev->info.wps_sec_dev_type_list_len =
msg->wps_sec_dev_type_list_len;
}
if (msg->capability) {
/*
* P2P Client Discoverability bit is reserved in all frames
* that use this function, so do not change its value here.
*/
dev->info.dev_capab &= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
dev->info.dev_capab |= msg->capability[0] &
~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
dev->info.group_capab = msg->capability[1];
}
if (msg->ext_listen_timing) {
dev->ext_listen_period = WPA_GET_LE16(msg->ext_listen_timing);
dev->ext_listen_interval =
WPA_GET_LE16(msg->ext_listen_timing + 2);
}
if (!probe_req) {
dev->info.config_methods = msg->config_methods ?
msg->config_methods : msg->wps_config_methods;
}
}
/**
* p2p_add_device - Add peer entries based on scan results or P2P frames
* @p2p: P2P module context from p2p_init()
* @addr: Source address of Beacon or Probe Response frame (may be either
* P2P Device Address or P2P Interface Address)
* @level: Signal level (signal strength of the received frame from the peer)
* @freq: Frequency on which the Beacon or Probe Response frame was received
* @ies: IEs from the Beacon or Probe Response frame
* @ies_len: Length of ies buffer in octets
* @scan_res: Whether this was based on scan results
* Returns: 0 on success, -1 on failure
*
* If the scan result is for a GO, the clients in the group will also be added
* to the peer table. This function can also be used with some other frames
* like Provision Discovery Request that contains P2P Capability and P2P Device
* Info attributes.
*/
int p2p_add_device(struct p2p_data *p2p, const u8 *addr, int freq, int level,
const u8 *ies, size_t ies_len, int scan_res)
{
struct p2p_device *dev;
struct p2p_message msg;
const u8 *p2p_dev_addr;
int i;
os_memset(&msg, 0, sizeof(msg));
if (p2p_parse_ies(ies, ies_len, &msg)) {
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: Failed to parse P2P IE for a device entry");
p2p_parse_free(&msg);
return -1;
}
if (msg.p2p_device_addr)
p2p_dev_addr = msg.p2p_device_addr;
else if (msg.device_id)
p2p_dev_addr = msg.device_id;
else {
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: Ignore scan data without P2P Device Info or "
"P2P Device Id");
p2p_parse_free(&msg);
return -1;
}
if (!is_zero_ether_addr(p2p->peer_filter) &&
os_memcmp(p2p_dev_addr, p2p->peer_filter, ETH_ALEN) != 0) {
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Do not add peer "
"filter for " MACSTR " due to peer filter",
MAC2STR(p2p_dev_addr));
return 0;
}
dev = p2p_create_device(p2p, p2p_dev_addr);
if (dev == NULL) {
p2p_parse_free(&msg);
return -1;
}
os_get_time(&dev->last_seen);
dev->flags &= ~(P2P_DEV_PROBE_REQ_ONLY | P2P_DEV_GROUP_CLIENT_ONLY);
if (os_memcmp(addr, p2p_dev_addr, ETH_ALEN) != 0)
os_memcpy(dev->interface_addr, addr, ETH_ALEN);
if (msg.ssid &&
msg.ssid[1] <= sizeof(dev->oper_ssid) &&
(msg.ssid[1] != P2P_WILDCARD_SSID_LEN ||
os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN)
!= 0)) {
os_memcpy(dev->oper_ssid, msg.ssid + 2, msg.ssid[1]);
dev->oper_ssid_len = msg.ssid[1];
}
if (freq >= 2412 && freq <= 2484 && msg.ds_params &&
*msg.ds_params >= 1 && *msg.ds_params <= 14) {
int ds_freq;
if (*msg.ds_params == 14)
ds_freq = 2484;
else
ds_freq = 2407 + *msg.ds_params * 5;
if (freq != ds_freq) {
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: Update Listen frequency based on DS "
"Parameter Set IE: %d -> %d MHz",
freq, ds_freq);
freq = ds_freq;
}
}
if (dev->listen_freq && dev->listen_freq != freq && scan_res) {
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: Update Listen frequency based on scan "
"results (" MACSTR " %d -> %d MHz (DS param %d)",
MAC2STR(dev->info.p2p_device_addr), dev->listen_freq,
freq, msg.ds_params ? *msg.ds_params : -1);
}
if (scan_res) {
dev->listen_freq = freq;
if (msg.group_info)
dev->oper_freq = freq;
}
dev->info.level = level;
p2p_copy_wps_info(dev, 0, &msg);
for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
wpabuf_free(dev->info.wps_vendor_ext[i]);
dev->info.wps_vendor_ext[i] = NULL;
}
for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
if (msg.wps_vendor_ext[i] == NULL)
break;
dev->info.wps_vendor_ext[i] = wpabuf_alloc_copy(
msg.wps_vendor_ext[i], msg.wps_vendor_ext_len[i]);
if (dev->info.wps_vendor_ext[i] == NULL)
break;
}
if (msg.wfd_subelems) {
wpabuf_free(dev->info.wfd_subelems);
dev->info.wfd_subelems = wpabuf_dup(msg.wfd_subelems);
}
if (scan_res) {
p2p_add_group_clients(p2p, p2p_dev_addr, addr, freq,
msg.group_info, msg.group_info_len);
}
p2p_parse_free(&msg);
if (p2p_pending_sd_req(p2p, dev))
dev->flags |= P2P_DEV_SD_SCHEDULE;
if (dev->flags & P2P_DEV_REPORTED)
return 0;
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: Peer found with Listen frequency %d MHz", freq);
if (dev->flags & P2P_DEV_USER_REJECTED) {
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: Do not report rejected device");
return 0;
}
p2p->cfg->dev_found(p2p->cfg->cb_ctx, addr, &dev->info,
!(dev->flags & P2P_DEV_REPORTED_ONCE));
dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
return 0;
}
static void p2p_device_free(struct p2p_data *p2p, struct p2p_device *dev)
{
int i;
if (p2p->go_neg_peer == dev) {
/*
* If GO Negotiation is in progress, report that it has failed.
*/
p2p_go_neg_failed(p2p, dev, -1);
p2p->go_neg_peer = NULL;
}
if (p2p->invite_peer == dev)
p2p->invite_peer = NULL;
if (p2p->sd_peer == dev)
p2p->sd_peer = NULL;
if (p2p->pending_client_disc_go == dev)
p2p->pending_client_disc_go = NULL;
/* dev_lost() device, but only if it was previously dev_found() */
if (dev->flags & P2P_DEV_REPORTED_ONCE)
p2p->cfg->dev_lost(p2p->cfg->cb_ctx,
dev->info.p2p_device_addr);
for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
wpabuf_free(dev->info.wps_vendor_ext[i]);
dev->info.wps_vendor_ext[i] = NULL;
}
wpabuf_free(dev->info.wfd_subelems);
os_free(dev);
}
static int p2p_get_next_prog_freq(struct p2p_data *p2p)
{
struct p2p_channels *c;
struct p2p_reg_class *cla;
size_t cl, ch;
int found = 0;
u8 reg_class;
u8 channel;
int freq;
c = &p2p->cfg->channels;
for (cl = 0; cl < c->reg_classes; cl++) {
cla = &c->reg_class[cl];
if (cla->reg_class != p2p->last_prog_scan_class)
continue;
for (ch = 0; ch < cla->channels; ch++) {
if (cla->channel[ch] == p2p->last_prog_scan_chan) {
found = 1;
break;
}
}
if (found)
break;
}
if (!found) {
/* Start from beginning */
reg_class = c->reg_class[0].reg_class;
channel = c->reg_class[0].channel[0];
} else {
/* Pick the next channel */
ch++;
if (ch == cla->channels) {
cl++;
if (cl == c->reg_classes)
cl = 0;
ch = 0;
}
reg_class = c->reg_class[cl].reg_class;
channel = c->reg_class[cl].channel[ch];
}
freq = p2p_channel_to_freq(p2p->cfg->country, reg_class, channel);
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Next progressive search "
"channel: reg_class %u channel %u -> %d MHz",
reg_class, channel, freq);
p2p->last_prog_scan_class = reg_class;
p2p->last_prog_scan_chan = channel;
if (freq == 2412 || freq == 2437 || freq == 2462)
return 0; /* No need to add social channels */
return freq;
}
static void p2p_search(struct p2p_data *p2p)
{
int freq = 0;
enum p2p_scan_type type;
u16 pw_id = DEV_PW_DEFAULT;
int res;
if (p2p->drv_in_listen) {
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Driver is still "
"in Listen state - wait for it to end before "
"continuing");
return;
}
p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
if (p2p->find_type == P2P_FIND_PROGRESSIVE &&
(freq = p2p_get_next_prog_freq(p2p)) > 0) {
type = P2P_SCAN_SOCIAL_PLUS_ONE;
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting search "
"(+ freq %u)", freq);
} else {
type = P2P_SCAN_SOCIAL;
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting search");
}
res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, type, freq,
p2p->num_req_dev_types, p2p->req_dev_types,
p2p->find_dev_id, pw_id);
if (res < 0) {
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: Scan request failed");
p2p_continue_find(p2p);
} else if (res == 1) {
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Could not start "
"p2p_scan at this point - will try again after "
"previous scan completes");
p2p_set_state(p2p, P2P_CONTINUE_SEARCH_WHEN_READY);
} else {
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Running p2p_scan");
p2p->p2p_scan_running = 1;
eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
eloop_register_timeout(P2P_SCAN_TIMEOUT, 0, p2p_scan_timeout,
p2p, NULL);
}
}
static void p2p_find_timeout(void *eloop_ctx, void *timeout_ctx)
{
struct p2p_data *p2p = eloop_ctx;
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Find timeout -> stop");
p2p_stop_find(p2p);
}
static int p2p_run_after_scan(struct p2p_data *p2p)
{
struct p2p_device *dev;
enum p2p_after_scan op;
if (p2p->after_scan_tx) {
/* TODO: schedule p2p_run_after_scan to be called from TX
* status callback(?) */
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send pending "
"Action frame at p2p_scan completion");
p2p->cfg->send_action(p2p->cfg->cb_ctx,
p2p->after_scan_tx->freq,
p2p->after_scan_tx->dst,
p2p->after_scan_tx->src,
p2p->after_scan_tx->bssid,
(u8 *) (p2p->after_scan_tx + 1),
p2p->after_scan_tx->len,
p2p->after_scan_tx->wait_time);
os_free(p2p->after_scan_tx);
p2p->after_scan_tx = NULL;
#ifdef ANDROID_P2P
/* For SD frames, there is a scenario, where we can receive a SD request frame during p2p_scan.
* At that moment, we will send the SD response from this context. After sending the SD response,
* we need to continue p2p_find. But if we return 1 from here, p2p_find is going to be stopped.
*/
return 0;
#else
return 1;
#endif
}
op = p2p->start_after_scan;
p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
switch (op) {
case P2P_AFTER_SCAN_NOTHING:
break;
case P2P_AFTER_SCAN_LISTEN:
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Start previously "
"requested Listen state");
p2p_listen(p2p, p2p->pending_listen_sec * 1000 +
p2p->pending_listen_usec / 1000);
return 1;
case P2P_AFTER_SCAN_CONNECT:
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Start previously "
"requested connect with " MACSTR,
MAC2STR(p2p->after_scan_peer));
dev = p2p_get_device(p2p, p2p->after_scan_peer);
if (dev == NULL) {
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer not "
"known anymore");
break;
}
p2p_connect_send(p2p, dev);
return 1;
}
return 0;
}
static void p2p_scan_timeout(void *eloop_ctx, void *timeout_ctx)
{
struct p2p_data *p2p = eloop_ctx;
int running;
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan timeout "
"(running=%d)", p2p->p2p_scan_running);
running = p2p->p2p_scan_running;
/* Make sure we recover from missed scan results callback */
p2p->p2p_scan_running = 0;
if (running)
p2p_run_after_scan(p2p);
}
static void p2p_free_req_dev_types(struct p2p_data *p2p)
{
p2p->num_req_dev_types = 0;
os_free(p2p->req_dev_types);
p2p->req_dev_types = NULL;
}
int p2p_find(struct p2p_data *p2p, unsigned int timeout,
enum p2p_discovery_type type,
unsigned int num_req_dev_types, const u8 *req_dev_types,
const u8 *dev_id, unsigned int search_delay)
{
int res;
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting find (type=%d)",
type);
if (p2p->p2p_scan_running) {
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan is "
"already running");
}
p2p_free_req_dev_types(p2p);
if (req_dev_types && num_req_dev_types) {
p2p->req_dev_types = os_malloc(num_req_dev_types *
WPS_DEV_TYPE_LEN);
if (p2p->req_dev_types == NULL)
return -1;
os_memcpy(p2p->req_dev_types, req_dev_types,
num_req_dev_types * WPS_DEV_TYPE_LEN);
p2p->num_req_dev_types = num_req_dev_types;
}
if (dev_id) {
os_memcpy(p2p->find_dev_id_buf, dev_id, ETH_ALEN);
p2p->find_dev_id = p2p->find_dev_id_buf;
} else
p2p->find_dev_id = NULL;
p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
p2p_clear_timeout(p2p);
p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
p2p->find_type = type;
p2p_device_clear_reported(p2p);
p2p_set_state(p2p, P2P_SEARCH);
p2p->search_delay = search_delay;
p2p->in_search_delay = 0;
eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
p2p->last_p2p_find_timeout = timeout;
if (timeout)
eloop_register_timeout(timeout, 0, p2p_find_timeout,
p2p, NULL);
switch (type) {
case P2P_FIND_START_WITH_FULL:
case P2P_FIND_PROGRESSIVE:
res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_FULL, 0,
p2p->num_req_dev_types,
p2p->req_dev_types, dev_id,
DEV_PW_DEFAULT);
break;
case P2P_FIND_ONLY_SOCIAL:
res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_SOCIAL, 0,
p2p->num_req_dev_types,
p2p->req_dev_types, dev_id,
DEV_PW_DEFAULT);
break;
default:
return -1;
}
if (res == 0) {
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Running p2p_scan");
p2p->p2p_scan_running = 1;
eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
eloop_register_timeout(P2P_SCAN_TIMEOUT, 0, p2p_scan_timeout,
p2p, NULL);
} else if (res == 1) {
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Could not start "
"p2p_scan at this point - will try again after "
"previous scan completes");
res = 0;
p2p_set_state(p2p, P2P_SEARCH_WHEN_READY);
eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
} else {
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Failed to start "
"p2p_scan");
p2p_set_state(p2p, P2P_IDLE);
eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
}
return res;
}
#ifdef ANDROID_P2P
int p2p_search_pending(struct p2p_data *p2p)
{
if(p2p == NULL)
return 0;
if(p2p->state == P2P_SEARCH_WHEN_READY)
return 1;
return 0;
}
#endif
int p2p_other_scan_completed(struct p2p_data *p2p)
{
if (p2p->state == P2P_CONTINUE_SEARCH_WHEN_READY) {
p2p_set_state(p2p, P2P_SEARCH);
p2p_search(p2p);
return 1;
}
if (p2p->state != P2P_SEARCH_WHEN_READY)
return 0;
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting pending P2P find "
"now that previous scan was completed");
if (p2p_find(p2p, p2p->last_p2p_find_timeout, p2p->find_type,
p2p->num_req_dev_types, p2p->req_dev_types,
p2p->find_dev_id, p2p->search_delay) < 0)
return 0;
return 1;
}
void p2p_stop_find_for_freq(struct p2p_data *p2p, int freq)
{
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Stopping find");
eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
p2p_clear_timeout(p2p);
if (p2p->state == P2P_SEARCH)
wpa_msg(p2p->cfg->msg_ctx, MSG_INFO, P2P_EVENT_FIND_STOPPED);
p2p_set_state(p2p, P2P_IDLE);
p2p_free_req_dev_types(p2p);
p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
p2p->go_neg_peer = NULL;
p2p->sd_peer = NULL;
p2p->invite_peer = NULL;
p2p_stop_listen_for_freq(p2p, freq);
}
void p2p_stop_listen_for_freq(struct p2p_data *p2p, int freq)
{
if (freq > 0 && p2p->drv_in_listen == freq && p2p->in_listen) {
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Skip stop_listen "
"since we are on correct channel for response");
return;
}
if (p2p->in_listen) {
p2p->in_listen = 0;
p2p_clear_timeout(p2p);
}
if (p2p->drv_in_listen) {
/*
* The driver may not deliver callback to p2p_listen_end()
* when the operation gets canceled, so clear the internal
* variable that is tracking driver state.
*/
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Clear "
"drv_in_listen (%d)", p2p->drv_in_listen);
p2p->drv_in_listen = 0;
}
p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
}
void p2p_stop_find(struct p2p_data *p2p)
{
p2p_stop_find_for_freq(p2p, 0);
}
static int p2p_prepare_channel(struct p2p_data *p2p, unsigned int force_freq)
{
if (force_freq) {
u8 op_reg_class, op_channel;
if (p2p_freq_to_channel(p2p->cfg->country, force_freq,
&op_reg_class, &op_channel) < 0) {
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: Unsupported frequency %u MHz",
force_freq);
return -1;
}
if (!p2p_channels_includes(&p2p->cfg->channels, op_reg_class,
op_channel)) {
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: Frequency %u MHz (oper_class %u "
"channel %u) not allowed for P2P",
force_freq, op_reg_class, op_channel);
return -1;
}
p2p->op_reg_class = op_reg_class;
p2p->op_channel = op_channel;
#ifndef ANDROID_P2P
p2p->channels.reg_classes = 1;
p2p->channels.reg_class[0].channels = 1;
p2p->channels.reg_class[0].reg_class = p2p->op_reg_class;
p2p->channels.reg_class[0].channel[0] = p2p->op_channel;
#else
if(p2p->cfg->p2p_concurrency == P2P_MULTI_CHANNEL_CONCURRENT) {
/* We we are requesting for a preferred channel. But since
* are multichannel concurrent, we have to poplulate the
* p2p_channels with list of channels that we support.
*/
#ifdef ANDROID_P2P
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "Full channel list");
#endif
os_memcpy(&p2p->channels, &p2p->cfg->channels,
sizeof(struct p2p_channels));
} else {
#ifdef ANDROID_P2P
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "Single channel list %d", p2p->op_channel);
#endif
p2p->channels.reg_classes = 1;
p2p->channels.reg_class[0].channels = 1;
p2p->channels.reg_class[0].reg_class = p2p->op_reg_class;
p2p->channels.reg_class[0].channel[0] = p2p->op_channel;
}
#endif
} else {
u8 op_reg_class, op_channel;
if (!p2p->cfg->cfg_op_channel && p2p->best_freq_overall > 0 &&
p2p_supported_freq(p2p, p2p->best_freq_overall) &&
p2p_freq_to_channel(p2p->cfg->country,
p2p->best_freq_overall,
&op_reg_class, &op_channel) == 0) {
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: Select best overall channel as "
"operating channel preference");
p2p->op_reg_class = op_reg_class;
p2p->op_channel = op_channel;
} else if (!p2p->cfg->cfg_op_channel && p2p->best_freq_5 > 0 &&
p2p_supported_freq(p2p, p2p->best_freq_5) &&
p2p_freq_to_channel(p2p->cfg->country,
p2p->best_freq_5,
&op_reg_class, &op_channel) ==
0) {
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: Select best 5 GHz channel as "
"operating channel preference");
p2p->op_reg_class = op_reg_class;
p2p->op_channel = op_channel;
} else if (!p2p->cfg->cfg_op_channel &&
p2p->best_freq_24 > 0 &&
p2p_supported_freq(p2p, p2p->best_freq_24) &&
p2p_freq_to_channel(p2p->cfg->country,
p2p->best_freq_24,
&op_reg_class, &op_channel) ==
0) {
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: Select best 2.4 GHz channel as "
"operating channel preference");
p2p->op_reg_class = op_reg_class;
p2p->op_channel = op_channel;
} else {
p2p->op_reg_class = p2p->cfg->op_reg_class;
p2p->op_channel = p2p->cfg->op_channel;
}
os_memcpy(&p2p->channels, &p2p->cfg->channels,
sizeof(struct p2p_channels));
}
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: Own preference for operation channel: "
"Operating Class %u Channel %u%s",
p2p->op_reg_class, p2p->op_channel,
force_freq ? " (forced)" : "");
return 0;
}
static void p2p_set_dev_persistent(struct p2p_device *dev,
int persistent_group)
{
switch (persistent_group) {
case 0:
dev->flags &= ~(P2P_DEV_PREFER_PERSISTENT_GROUP |
P2P_DEV_PREFER_PERSISTENT_RECONN);
break;
case 1:
dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP;
dev->flags &= ~P2P_DEV_PREFER_PERSISTENT_RECONN;
break;
case 2:
dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP |
P2P_DEV_PREFER_PERSISTENT_RECONN;
break;
}
}
int p2p_connect(struct p2p_data *p2p, const u8 *peer_addr,
enum p2p_wps_method wps_method,
int go_intent, const u8 *own_interface_addr,
unsigned int force_freq, int persistent_group,
const u8 *force_ssid, size_t force_ssid_len,
int pd_before_go_neg)
{
struct p2p_device *dev;
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: Request to start group negotiation - peer=" MACSTR
" GO Intent=%d Intended Interface Address=" MACSTR
" wps_method=%d persistent_group=%d pd_before_go_neg=%d force_freq %d",
MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr),
wps_method, persistent_group, pd_before_go_neg, force_freq);
if (p2p_prepare_channel(p2p, force_freq) < 0)
return -1;
dev = p2p_get_device(p2p, peer_addr);
if (dev == NULL || (dev->flags & P2P_DEV_PROBE_REQ_ONLY)) {
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: Cannot connect to unknown P2P Device " MACSTR,
MAC2STR(peer_addr));
return -1;
}
if (dev->flags & P2P_DEV_GROUP_CLIENT_ONLY) {
if (!(dev->info.dev_capab &
P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) {
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: Cannot connect to P2P Device " MACSTR
" that is in a group and is not discoverable",
MAC2STR(peer_addr));
return -1;
}
if (dev->oper_freq <= 0) {
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: Cannot connect to P2P Device " MACSTR
" with incomplete information",
MAC2STR(peer_addr));
return -1;
}
/*
* First, try to connect directly. If the peer does not
* acknowledge frames, assume it is sleeping and use device
* discoverability via the GO at that point.
*/
}
p2p->ssid_set = 0;
if (force_ssid) {
wpa_hexdump_ascii(MSG_DEBUG, "P2P: Forced SSID",
force_ssid, force_ssid_len);
os_memcpy(p2p->ssid, force_ssid, force_ssid_len);
p2p->ssid_len = force_ssid_len;
p2p->ssid_set = 1;
}
dev->flags &= ~P2P_DEV_NOT_YET_READY;
dev->flags &= ~P2P_DEV_USER_REJECTED;
dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
if (pd_before_go_neg)
dev->flags |= P2P_DEV_PD_BEFORE_GO_NEG;
else
dev->flags &= ~P2P_DEV_PD_BEFORE_GO_NEG;
dev->connect_reqs = 0;
dev->go_neg_req_sent = 0;
dev->go_state = UNKNOWN_GO;
p2p_set_dev_persistent(dev, persistent_group);
p2p->go_intent = go_intent;
os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
if (p2p->state != P2P_IDLE)
p2p_stop_find(p2p);
if (p2p->after_scan_tx) {
/*
* We need to drop the pending frame to avoid issues with the
* new GO Negotiation, e.g., when the pending frame was from a
* previous attempt at starting a GO Negotiation.
*/
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Dropped "
"previous pending Action frame TX that was waiting "
"for p2p_scan completion");
os_free(p2p->after_scan_tx);
p2p->after_scan_tx = NULL;
}
dev->wps_method = wps_method;
dev->status = P2P_SC_SUCCESS;
if (force_freq)
dev->flags |= P2P_DEV_FORCE_FREQ;
else
dev->flags &= ~P2P_DEV_FORCE_FREQ;
if (p2p->p2p_scan_running) {
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: p2p_scan running - delay connect send");
p2p->start_after_scan = P2P_AFTER_SCAN_CONNECT;
os_memcpy(p2p->after_scan_peer, peer_addr, ETH_ALEN);
return 0;
}
p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
return p2p_connect_send(p2p, dev);
}
int p2p_authorize(struct p2p_data *p2p, const u8 *peer_addr,
enum p2p_wps_method wps_method,
int go_intent, const u8 *own_interface_addr,
unsigned int force_freq, int persistent_group,
const u8 *force_ssid, size_t force_ssid_len)
{
struct p2p_device *dev;
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: Request to authorize group negotiation - peer=" MACSTR
" GO Intent=%d Intended Interface Address=" MACSTR
" wps_method=%d persistent_group=%d",
MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr),
wps_method, persistent_group);
if (p2p_prepare_channel(p2p, force_freq) < 0)
return -1;
dev = p2p_get_device(p2p, peer_addr);
if (dev == NULL) {
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: Cannot authorize unknown P2P Device " MACSTR,
MAC2STR(peer_addr));
return -1;
}
p2p->ssid_set = 0;
if (force_ssid) {
wpa_hexdump_ascii(MSG_DEBUG, "P2P: Forced SSID",
force_ssid, force_ssid_len);
os_memcpy(p2p->ssid, force_ssid, force_ssid_len);
p2p->ssid_len = force_ssid_len;
p2p->ssid_set = 1;
}
dev->flags &= ~P2P_DEV_NOT_YET_READY;
dev->flags &= ~P2P_DEV_USER_REJECTED;
dev->go_neg_req_sent = 0;
dev->go_state = UNKNOWN_GO;
p2p_set_dev_persistent(dev, persistent_group);
p2p->go_intent = go_intent;
os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
dev->wps_method = wps_method;
dev->status = P2P_SC_SUCCESS;
if (force_freq)
dev->flags |= P2P_DEV_FORCE_FREQ;
else
dev->flags &= ~P2P_DEV_FORCE_FREQ;
return 0;
}
void p2p_add_dev_info(struct p2p_data *p2p, const u8 *addr,
struct p2p_device *dev, struct p2p_message *msg)
{
os_get_time(&dev->last_seen);
p2p_copy_wps_info(dev, 0, msg);
if (msg->listen_channel) {
int freq;
freq = p2p_channel_to_freq((char *) msg->listen_channel,
msg->listen_channel[3],
msg->listen_channel[4]);
if (freq < 0) {
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: Unknown peer Listen channel: "
"country=%c%c(0x%02x) reg_class=%u channel=%u",
msg->listen_channel[0],
msg->listen_channel[1],
msg->listen_channel[2],
msg->listen_channel[3],
msg->listen_channel[4]);
} else {
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Update "
"peer " MACSTR " Listen channel: %u -> %u MHz",
MAC2STR(dev->info.p2p_device_addr),
dev->listen_freq, freq);
dev->listen_freq = freq;
}
}
if (msg->wfd_subelems) {
wpabuf_free(dev->info.wfd_subelems);
dev->info.wfd_subelems = wpabuf_dup(msg->wfd_subelems);
}
if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
dev->flags &= ~P2P_DEV_PROBE_REQ_ONLY;
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: Completed device entry based on data from "
"GO Negotiation Request");
} else {
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: Created device entry based on GO Neg Req: "
MACSTR " dev_capab=0x%x group_capab=0x%x name='%s' "
"listen_freq=%d",
MAC2STR(dev->info.p2p_device_addr),
dev->info.dev_capab, dev->info.group_capab,
dev->info.device_name, dev->listen_freq);
}
dev->flags &= ~P2P_DEV_GROUP_CLIENT_ONLY;
if (dev->flags & P2P_DEV_USER_REJECTED) {
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: Do not report rejected device");
return;
}
p2p->cfg->dev_found(p2p->cfg->cb_ctx, addr, &dev->info,
!(dev->flags & P2P_DEV_REPORTED_ONCE));
dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
}
void p2p_build_ssid(struct p2p_data *p2p, u8 *ssid, size_t *ssid_len)
{
os_memcpy(ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
p2p_random((char *) &ssid[P2P_WILDCARD_SSID_LEN], 2);
os_memcpy(&ssid[P2P_WILDCARD_SSID_LEN + 2],
p2p->cfg->ssid_postfix, p2p->cfg->ssid_postfix_len);
*ssid_len = P2P_WILDCARD_SSID_LEN + 2 + p2p->cfg->ssid_postfix_len;
}
int p2p_go_params(struct p2p_data *p2p, struct p2p_go_neg_results *params)
{
p2p_build_ssid(p2p, params->ssid, ¶ms->ssid_len);
p2p_random(params->passphrase, 8);
return 0;
}
void p2p_go_complete(struct p2p_data *p2p, struct p2p_device *peer)
{
struct p2p_go_neg_results res;
int go = peer->go_state == LOCAL_GO;
struct p2p_channels intersection;
int freqs;
size_t i, j;
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: GO Negotiation with " MACSTR " completed (%s will be "
"GO)", MAC2STR(peer->info.p2p_device_addr),
go ? "local end" : "peer");
os_memset(&res, 0, sizeof(res));
res.role_go = go;
os_memcpy(res.peer_device_addr, peer->info.p2p_device_addr, ETH_ALEN);
os_memcpy(res.peer_interface_addr, peer->intended_addr, ETH_ALEN);
res.wps_method = peer->wps_method;
if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) {
if (peer->flags & P2P_DEV_PREFER_PERSISTENT_RECONN)
res.persistent_group = 2;
else
res.persistent_group = 1;
}
if (go) {
/* Setup AP mode for WPS provisioning */
res.freq = p2p_channel_to_freq(p2p->cfg->country,
p2p->op_reg_class,
p2p->op_channel);
os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len);
res.ssid_len = p2p->ssid_len;
p2p_random(res.passphrase, 8);
} else {
res.freq = peer->oper_freq;
if (p2p->ssid_len) {
os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len);
res.ssid_len = p2p->ssid_len;
}
}
p2p_channels_intersect(&p2p->channels, &peer->channels,
&intersection);
freqs = 0;
for (i = 0; i < intersection.reg_classes; i++) {
struct p2p_reg_class *c = &intersection.reg_class[i];
if (freqs + 1 == P2P_MAX_CHANNELS)
break;
for (j = 0; j < c->channels; j++) {
int freq;
if (freqs + 1 == P2P_MAX_CHANNELS)
break;
freq = p2p_channel_to_freq(peer->country, c->reg_class,
c->channel[j]);
if (freq < 0)
continue;
res.freq_list[freqs++] = freq;
}
}
res.peer_config_timeout = go ? peer->client_timeout : peer->go_timeout;
p2p_clear_timeout(p2p);
p2p->ssid_set = 0;
peer->go_neg_req_sent = 0;
peer->wps_method = WPS_NOT_READY;
p2p_set_state(p2p, P2P_PROVISIONING);
p2p->cfg->go_neg_completed(p2p->cfg->cb_ctx, &res);
}
static void p2p_rx_p2p_action(struct p2p_data *p2p, const u8 *sa,
const u8 *data, size_t len, int rx_freq)
{
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: RX P2P Public Action from " MACSTR, MAC2STR(sa));
wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Public Action contents", data, len);
if (len < 1)
return;
switch (data[0]) {
case P2P_GO_NEG_REQ:
p2p_process_go_neg_req(p2p, sa, data + 1, len - 1, rx_freq);
break;
case P2P_GO_NEG_RESP:
p2p_process_go_neg_resp(p2p, sa, data + 1, len - 1, rx_freq);
break;
case P2P_GO_NEG_CONF:
p2p_process_go_neg_conf(p2p, sa, data + 1, len - 1);
break;
case P2P_INVITATION_REQ:
p2p_process_invitation_req(p2p, sa, data + 1, len - 1,
rx_freq);
break;
case P2P_INVITATION_RESP:
p2p_process_invitation_resp(p2p, sa, data + 1, len - 1);
break;
case P2P_PROV_DISC_REQ:
p2p_process_prov_disc_req(p2p, sa, data + 1, len - 1, rx_freq);
break;
case P2P_PROV_DISC_RESP:
p2p_process_prov_disc_resp(p2p, sa, data + 1, len - 1);
break;
case P2P_DEV_DISC_REQ:
p2p_process_dev_disc_req(p2p, sa, data + 1, len - 1, rx_freq);
break;
case P2P_DEV_DISC_RESP:
p2p_process_dev_disc_resp(p2p, sa, data + 1, len - 1);
break;
default:
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: Unsupported P2P Public Action frame type %d",
data[0]);
break;
}
}
static void p2p_rx_action_public(struct p2p_data *p2p, const u8 *da,
const u8 *sa, const u8 *bssid, const u8 *data,
size_t len, int freq)
{
if (len < 1)
return;
switch (data[0]) {
case WLAN_PA_VENDOR_SPECIFIC:
data++;
len--;
if (len < 3)
return;
if (WPA_GET_BE24(data) != OUI_WFA)
return;
data += 3;
len -= 3;
if (len < 1)
return;
if (*data != P2P_OUI_TYPE)
return;
p2p_rx_p2p_action(p2p, sa, data + 1, len - 1, freq);
break;
case WLAN_PA_GAS_INITIAL_REQ:
p2p_rx_gas_initial_req(p2p, sa, data + 1, len - 1, freq);
break;
case WLAN_PA_GAS_INITIAL_RESP:
p2p_rx_gas_initial_resp(p2p, sa, data + 1, len - 1, freq);
break;
case WLAN_PA_GAS_COMEBACK_REQ:
p2p_rx_gas_comeback_req(p2p, sa, data + 1, len - 1, freq);
break;
case WLAN_PA_GAS_COMEBACK_RESP:
p2p_rx_gas_comeback_resp(p2p, sa, data + 1, len - 1, freq);
break;
}
}
void p2p_rx_action(struct p2p_data *p2p, const u8 *da, const u8 *sa,
const u8 *bssid, u8 category,
const u8 *data, size_t len, int freq)
{
if (category == WLAN_ACTION_PUBLIC) {
p2p_rx_action_public(p2p, da, sa, bssid, data, len, freq);
return;
}
if (category != WLAN_ACTION_VENDOR_SPECIFIC)
return;
if (len < 4)
return;
if (WPA_GET_BE24(data) != OUI_WFA)
return;
data += 3;
len -= 3;
if (*data != P2P_OUI_TYPE)
return;
data++;
len--;
/* P2P action frame */
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: RX P2P Action from " MACSTR, MAC2STR(sa));
wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Action contents", data, len);
if (len < 1)
return;
switch (data[0]) {
case P2P_NOA:
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: Received P2P Action - Notice of Absence");
/* TODO */
break;
case P2P_PRESENCE_REQ:
p2p_process_presence_req(p2p, da, sa, data + 1, len - 1, freq);
break;
case P2P_PRESENCE_RESP:
p2p_process_presence_resp(p2p, da, sa, data + 1, len - 1);
break;
case P2P_GO_DISC_REQ:
p2p_process_go_disc_req(p2p, da, sa, data + 1, len - 1, freq);
break;
default:
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: Received P2P Action - unknown type %u", data[0]);
break;
}
}
static void p2p_go_neg_start(void *eloop_ctx, void *timeout_ctx)
{
struct p2p_data *p2p = eloop_ctx;
if (p2p->go_neg_peer == NULL)
return;
p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
p2p->go_neg_peer->status = P2P_SC_SUCCESS;
p2p_connect_send(p2p, p2p->go_neg_peer);
}
static void p2p_invite_start(void *eloop_ctx, void *timeout_ctx)
{
struct p2p_data *p2p = eloop_ctx;
if (p2p->invite_peer == NULL)
return;
p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
p2p_invite_send(p2p, p2p->invite_peer, p2p->invite_go_dev_addr);
}
static void p2p_add_dev_from_probe_req(struct p2p_data *p2p, const u8 *addr,
const u8 *ie, size_t ie_len)
{
struct p2p_message msg;
struct p2p_device *dev;
os_memset(&msg, 0, sizeof(msg));
if (p2p_parse_ies(ie, ie_len, &msg) < 0 || msg.p2p_attributes == NULL)
{
p2p_parse_free(&msg);
return; /* not a P2P probe */
}
if (msg.ssid == NULL || msg.ssid[1] != P2P_WILDCARD_SSID_LEN ||
os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN)
!= 0) {
/* The Probe Request is not part of P2P Device Discovery. It is
* not known whether the source address of the frame is the P2P
* Device Address or P2P Interface Address. Do not add a new
* peer entry based on this frames.
*/
p2p_parse_free(&msg);
return;
}
dev = p2p_get_device(p2p, addr);
if (dev) {
if (dev->country[0] == 0 && msg.listen_channel)
os_memcpy(dev->country, msg.listen_channel, 3);
os_get_time(&dev->last_seen);
p2p_parse_free(&msg);
return; /* already known */
}
dev = p2p_create_device(p2p, addr);
if (dev == NULL) {
p2p_parse_free(&msg);
return;
}
os_get_time(&dev->last_seen);
dev->flags |= P2P_DEV_PROBE_REQ_ONLY;
if (msg.listen_channel) {
os_memcpy(dev->country, msg.listen_channel, 3);
dev->listen_freq = p2p_channel_to_freq(dev->country,
msg.listen_channel[3],
msg.listen_channel[4]);
}
p2p_copy_wps_info(dev, 1, &msg);
if (msg.wfd_subelems) {
wpabuf_free(dev->info.wfd_subelems);
dev->info.wfd_subelems = wpabuf_dup(msg.wfd_subelems);
}
p2p_parse_free(&msg);
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: Created device entry based on Probe Req: " MACSTR
" dev_capab=0x%x group_capab=0x%x name='%s' listen_freq=%d",
MAC2STR(dev->info.p2p_device_addr), dev->info.dev_capab,
dev->info.group_capab, dev->info.device_name,
dev->listen_freq);
}
struct p2p_device * p2p_add_dev_from_go_neg_req(struct p2p_data *p2p,
const u8 *addr,
struct p2p_message *msg)
{
struct p2p_device *dev;
dev = p2p_get_device(p2p, addr);
if (dev) {
os_get_time(&dev->last_seen);
return dev; /* already known */
}
dev = p2p_create_device(p2p, addr);
if (dev == NULL)
return NULL;
p2p_add_dev_info(p2p, addr, dev, msg);
return dev;
}
static int dev_type_match(const u8 *dev_type, const u8 *req_dev_type)
{
if (os_memcmp(dev_type, req_dev_type, WPS_DEV_TYPE_LEN) == 0)
return 1;
if (os_memcmp(dev_type, req_dev_type, 2) == 0 &&
WPA_GET_BE32(&req_dev_type[2]) == 0 &&
WPA_GET_BE16(&req_dev_type[6]) == 0)
return 1; /* Category match with wildcard OUI/sub-category */
return 0;
}
int dev_type_list_match(const u8 *dev_type, const u8 *req_dev_type[],
size_t num_req_dev_type)
{
size_t i;
for (i = 0; i < num_req_dev_type; i++) {
if (dev_type_match(dev_type, req_dev_type[i]))
return 1;
}
return 0;
}
/**
* p2p_match_dev_type - Match local device type with requested type
* @p2p: P2P module context from p2p_init()
* @wps: WPS TLVs from Probe Request frame (concatenated WPS IEs)
* Returns: 1 on match, 0 on mismatch
*
* This function can be used to match the Requested Device Type attribute in
* WPS IE with the local device types for deciding whether to reply to a Probe
* Request frame.
*/
int p2p_match_dev_type(struct p2p_data *p2p, struct wpabuf *wps)
{
struct wps_parse_attr attr;
size_t i;
if (wps_parse_msg(wps, &attr))
return 1; /* assume no Requested Device Type attributes */
if (attr.num_req_dev_type == 0)
return 1; /* no Requested Device Type attributes -> match */
if (dev_type_list_match(p2p->cfg->pri_dev_type, attr.req_dev_type,
attr.num_req_dev_type))
return 1; /* Own Primary Device Type matches */
for (i = 0; i < p2p->cfg->num_sec_dev_types; i++)
if (dev_type_list_match(p2p->cfg->sec_dev_type[i],
attr.req_dev_type,
attr.num_req_dev_type))
return 1; /* Own Secondary Device Type matches */
/* No matching device type found */
return 0;
}
struct wpabuf * p2p_build_probe_resp_ies(struct p2p_data *p2p)
{
struct wpabuf *buf;
u8 *len;
int pw_id = -1;
size_t extra = 0;
#ifdef CONFIG_WIFI_DISPLAY
if (p2p->wfd_ie_probe_resp)
extra = wpabuf_len(p2p->wfd_ie_probe_resp);
#endif /* CONFIG_WIFI_DISPLAY */
buf = wpabuf_alloc(1000 + extra);
if (buf == NULL)
return NULL;
if (p2p->go_neg_peer) {
/* Advertise immediate availability of WPS credential */
pw_id = p2p_wps_method_pw_id(p2p->go_neg_peer->wps_method);
}
p2p_build_wps_ie(p2p, buf, pw_id, 1);
#ifdef CONFIG_WIFI_DISPLAY
if (p2p->wfd_ie_probe_resp)
wpabuf_put_buf(buf, p2p->wfd_ie_probe_resp);
#endif /* CONFIG_WIFI_DISPLAY */
/* P2P IE */
len = p2p_buf_add_ie_hdr(buf);
p2p_buf_add_capability(buf, p2p->dev_capab &
~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY, 0);
if (p2p->ext_listen_interval)
p2p_buf_add_ext_listen_timing(buf, p2p->ext_listen_period,
p2p->ext_listen_interval);
p2p_buf_add_device_info(buf, p2p, NULL);
p2p_buf_update_ie_hdr(buf, len);
return buf;
}
static int is_11b(u8 rate)
{
return rate == 0x02 || rate == 0x04 || rate == 0x0b || rate == 0x16;
}
static int supp_rates_11b_only(struct ieee802_11_elems *elems)
{
int num_11b = 0, num_others = 0;
int i;
if (elems->supp_rates == NULL && elems->ext_supp_rates == NULL)
return 0;
for (i = 0; elems->supp_rates && i < elems->supp_rates_len; i++) {
if (is_11b(elems->supp_rates[i]))
num_11b++;
else
num_others++;
}
for (i = 0; elems->ext_supp_rates && i < elems->ext_supp_rates_len;
i++) {
if (is_11b(elems->ext_supp_rates[i]))
num_11b++;
else
num_others++;
}
return num_11b > 0 && num_others == 0;
}
static enum p2p_probe_req_status
p2p_reply_probe(struct p2p_data *p2p, const u8 *addr, const u8 *dst,
const u8 *bssid, const u8 *ie, size_t ie_len)
{
struct ieee802_11_elems elems;
struct wpabuf *buf;
struct ieee80211_mgmt *resp;
struct p2p_message msg;
struct wpabuf *ies;
if (!p2p->in_listen || !p2p->drv_in_listen) {
/* not in Listen state - ignore Probe Request */
return P2P_PREQ_NOT_LISTEN;
}
if (ieee802_11_parse_elems((u8 *) ie, ie_len, &elems, 0) ==
ParseFailed) {
/* Ignore invalid Probe Request frames */
return P2P_PREQ_MALFORMED;
}
if (elems.p2p == NULL) {
/* not a P2P probe - ignore it */
return P2P_PREQ_NOT_P2P;
}
if (dst && !is_broadcast_ether_addr(dst) &&
os_memcmp(dst, p2p->cfg->dev_addr, ETH_ALEN) != 0) {
/* Not sent to the broadcast address or our P2P Device Address
*/
return P2P_PREQ_NOT_PROCESSED;
}
if (bssid && !is_broadcast_ether_addr(bssid)) {
/* Not sent to the Wildcard BSSID */
return P2P_PREQ_NOT_PROCESSED;
}
if (elems.ssid == NULL || elems.ssid_len != P2P_WILDCARD_SSID_LEN ||
os_memcmp(elems.ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) !=
0) {
/* not using P2P Wildcard SSID - ignore */
return P2P_PREQ_NOT_PROCESSED;
}
if (supp_rates_11b_only(&elems)) {
/* Indicates support for 11b rates only */
return P2P_PREQ_NOT_P2P;
}
os_memset(&msg, 0, sizeof(msg));
if (p2p_parse_ies(ie, ie_len, &msg) < 0) {
/* Could not parse P2P attributes */
return P2P_PREQ_NOT_P2P;
}
if (msg.device_id &&
os_memcmp(msg.device_id, p2p->cfg->dev_addr, ETH_ALEN) != 0) {
/* Device ID did not match */
p2p_parse_free(&msg);
return P2P_PREQ_NOT_PROCESSED;
}
/* Check Requested Device Type match */
if (msg.wps_attributes &&
!p2p_match_dev_type(p2p, msg.wps_attributes)) {
/* No match with Requested Device Type */
p2p_parse_free(&msg);
return P2P_PREQ_NOT_PROCESSED;
}
p2p_parse_free(&msg);
if (!p2p->cfg->send_probe_resp) {
/* Response generated elsewhere */
return P2P_PREQ_NOT_PROCESSED;
}
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: Reply to P2P Probe Request in Listen state");
/*
* We do not really have a specific BSS that this frame is advertising,
* so build a frame that has some information in valid format. This is
* really only used for discovery purposes, not to learn exact BSS
* parameters.
*/
ies = p2p_build_probe_resp_ies(p2p);
if (ies == NULL)
return P2P_PREQ_NOT_PROCESSED;
buf = wpabuf_alloc(200 + wpabuf_len(ies));
if (buf == NULL) {
wpabuf_free(ies);
return P2P_PREQ_NOT_PROCESSED;
}
resp = NULL;
resp = wpabuf_put(buf, resp->u.probe_resp.variable - (u8 *) resp);
resp->frame_control = host_to_le16((WLAN_FC_TYPE_MGMT << 2) |
(WLAN_FC_STYPE_PROBE_RESP << 4));
os_memcpy(resp->da, addr, ETH_ALEN);
os_memcpy(resp->sa, p2p->cfg->dev_addr, ETH_ALEN);
os_memcpy(resp->bssid, p2p->cfg->dev_addr, ETH_ALEN);
resp->u.probe_resp.beacon_int = host_to_le16(100);
/* hardware or low-level driver will setup seq_ctrl and timestamp */
resp->u.probe_resp.capab_info =
host_to_le16(WLAN_CAPABILITY_SHORT_PREAMBLE |
WLAN_CAPABILITY_PRIVACY |
WLAN_CAPABILITY_SHORT_SLOT_TIME);
wpabuf_put_u8(buf, WLAN_EID_SSID);
wpabuf_put_u8(buf, P2P_WILDCARD_SSID_LEN);
wpabuf_put_data(buf, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
wpabuf_put_u8(buf, WLAN_EID_SUPP_RATES);
wpabuf_put_u8(buf, 8);
wpabuf_put_u8(buf, (60 / 5) | 0x80);
wpabuf_put_u8(buf, 90 / 5);
wpabuf_put_u8(buf, (120 / 5) | 0x80);
wpabuf_put_u8(buf, 180 / 5);
wpabuf_put_u8(buf, (240 / 5) | 0x80);
wpabuf_put_u8(buf, 360 / 5);
wpabuf_put_u8(buf, 480 / 5);
wpabuf_put_u8(buf, 540 / 5);
wpabuf_put_u8(buf, WLAN_EID_DS_PARAMS);
wpabuf_put_u8(buf, 1);
wpabuf_put_u8(buf, p2p->cfg->channel);
wpabuf_put_buf(buf, ies);
wpabuf_free(ies);
p2p->cfg->send_probe_resp(p2p->cfg->cb_ctx, buf);
wpabuf_free(buf);
return P2P_PREQ_NOT_PROCESSED;
}
enum p2p_probe_req_status
p2p_probe_req_rx(struct p2p_data *p2p, const u8 *addr, const u8 *dst,
const u8 *bssid, const u8 *ie, size_t ie_len)
{
enum p2p_probe_req_status res;
p2p_add_dev_from_probe_req(p2p, addr, ie, ie_len);
res = p2p_reply_probe(p2p, addr, dst, bssid, ie, ie_len);
if ((p2p->state == P2P_CONNECT || p2p->state == P2P_CONNECT_LISTEN) &&
p2p->go_neg_peer &&
os_memcmp(addr, p2p->go_neg_peer->info.p2p_device_addr, ETH_ALEN)
== 0) {
/* Received a Probe Request from GO Negotiation peer */
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: Found GO Negotiation peer - try to start GO "
"negotiation from timeout");
eloop_register_timeout(0, 0, p2p_go_neg_start, p2p, NULL);
return P2P_PREQ_PROCESSED;
}
if ((p2p->state == P2P_INVITE || p2p->state == P2P_INVITE_LISTEN) &&
p2p->invite_peer &&
os_memcmp(addr, p2p->invite_peer->info.p2p_device_addr, ETH_ALEN)
== 0) {
/* Received a Probe Request from Invite peer */
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: Found Invite peer - try to start Invite from "
"timeout");
eloop_register_timeout(0, 0, p2p_invite_start, p2p, NULL);
return P2P_PREQ_PROCESSED;
}
return res;
}
static int p2p_assoc_req_ie_wlan_ap(struct p2p_data *p2p, const u8 *bssid,
u8 *buf, size_t len, struct wpabuf *p2p_ie)
{
struct wpabuf *tmp;
u8 *lpos;
size_t tmplen;
int res;
u8 group_capab;
if (p2p_ie == NULL)
return 0; /* WLAN AP is not a P2P manager */
/*
* (Re)Association Request - P2P IE
* P2P Capability attribute (shall be present)
* P2P Interface attribute (present if concurrent device and
* P2P Management is enabled)
*/
tmp = wpabuf_alloc(200);
if (tmp == NULL)
return -1;
lpos = p2p_buf_add_ie_hdr(tmp);
group_capab = 0;
if (p2p->num_groups > 0) {
group_capab |= P2P_GROUP_CAPAB_GROUP_OWNER;
if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) &&
(p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED) &&
p2p->cross_connect)
group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
}
p2p_buf_add_capability(tmp, p2p->dev_capab, group_capab);
if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) &&
(p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED))
p2p_buf_add_p2p_interface(tmp, p2p);
p2p_buf_update_ie_hdr(tmp, lpos);
tmplen = wpabuf_len(tmp);
if (tmplen > len)
res = -1;
else {
os_memcpy(buf, wpabuf_head(tmp), tmplen);
res = tmplen;
}
wpabuf_free(tmp);
return res;
}
int p2p_assoc_req_ie(struct p2p_data *p2p, const u8 *bssid, u8 *buf,
size_t len, int p2p_group, struct wpabuf *p2p_ie)
{
struct wpabuf *tmp;
u8 *lpos;
struct p2p_device *peer;
size_t tmplen;
int res;
size_t extra = 0;
if (!p2p_group)
return p2p_assoc_req_ie_wlan_ap(p2p, bssid, buf, len, p2p_ie);
#ifdef CONFIG_WIFI_DISPLAY
if (p2p->wfd_ie_assoc_req)
extra = wpabuf_len(p2p->wfd_ie_assoc_req);
#endif /* CONFIG_WIFI_DISPLAY */
/*
* (Re)Association Request - P2P IE
* P2P Capability attribute (shall be present)
* Extended Listen Timing (may be present)
* P2P Device Info attribute (shall be present)
*/
tmp = wpabuf_alloc(200 + extra);
if (tmp == NULL)
return -1;
#ifdef CONFIG_WIFI_DISPLAY
if (p2p->wfd_ie_assoc_req)
wpabuf_put_buf(tmp, p2p->wfd_ie_assoc_req);
#endif /* CONFIG_WIFI_DISPLAY */
peer = bssid ? p2p_get_device(p2p, bssid) : NULL;
lpos = p2p_buf_add_ie_hdr(tmp);
p2p_buf_add_capability(tmp, p2p->dev_capab, 0);
if (p2p->ext_listen_interval)
p2p_buf_add_ext_listen_timing(tmp, p2p->ext_listen_period,
p2p->ext_listen_interval);
p2p_buf_add_device_info(tmp, p2p, peer);
p2p_buf_update_ie_hdr(tmp, lpos);
tmplen = wpabuf_len(tmp);
if (tmplen > len)
res = -1;
else {
os_memcpy(buf, wpabuf_head(tmp), tmplen);
res = tmplen;
}
wpabuf_free(tmp);
return res;
}
int p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf, char *end)
{
struct wpabuf *p2p_ie;
int ret;
p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len, P2P_IE_VENDOR_TYPE);
if (p2p_ie == NULL)
return 0;
ret = p2p_attr_text(p2p_ie, buf, end);
wpabuf_free(p2p_ie);
return ret;
}
int p2p_parse_dev_addr_in_p2p_ie(struct wpabuf *p2p_ie, u8 *dev_addr)
{
struct p2p_message msg;
os_memset(&msg, 0, sizeof(msg));
if (p2p_parse_p2p_ie(p2p_ie, &msg))
return -1;
if (msg.p2p_device_addr) {
os_memcpy(dev_addr, msg.p2p_device_addr, ETH_ALEN);
return 0;
} else if (msg.device_id) {
os_memcpy(dev_addr, msg.device_id, ETH_ALEN);
return 0;
}
return -1;
}
int p2p_parse_dev_addr(const u8 *ies, size_t ies_len, u8 *dev_addr)
{
struct wpabuf *p2p_ie;
int ret;
p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len,
P2P_IE_VENDOR_TYPE);
if (p2p_ie == NULL)
return -1;
ret = p2p_parse_dev_addr_in_p2p_ie(p2p_ie, dev_addr);
wpabuf_free(p2p_ie);
return ret;
}
static void p2p_clear_go_neg(struct p2p_data *p2p)
{
p2p->go_neg_peer = NULL;
p2p_clear_timeout(p2p);
p2p_set_state(p2p, P2P_IDLE);
}
void p2p_wps_success_cb(struct p2p_data *p2p, const u8 *mac_addr)
{
if (p2p->go_neg_peer == NULL) {
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: No pending Group Formation - "
"ignore WPS registration success notification");
return; /* No pending Group Formation */
}
if (os_memcmp(mac_addr, p2p->go_neg_peer->intended_addr, ETH_ALEN) !=
0) {
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: Ignore WPS registration success notification "
"for " MACSTR " (GO Negotiation peer " MACSTR ")",
MAC2STR(mac_addr),
MAC2STR(p2p->go_neg_peer->intended_addr));
return; /* Ignore unexpected peer address */
}
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: Group Formation completed successfully with " MACSTR,
MAC2STR(mac_addr));
p2p_clear_go_neg(p2p);
}
void p2p_group_formation_failed(struct p2p_data *p2p)
{
if (p2p->go_neg_peer == NULL) {
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: No pending Group Formation - "
"ignore group formation failure notification");
return; /* No pending Group Formation */
}
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: Group Formation failed with " MACSTR,
MAC2STR(p2p->go_neg_peer->intended_addr));
p2p_clear_go_neg(p2p);
}
struct p2p_data * p2p_init(const struct p2p_config *cfg)
{
struct p2p_data *p2p;
if (cfg->max_peers < 1)
return NULL;
p2p = os_zalloc(sizeof(*p2p) + sizeof(*cfg));
if (p2p == NULL)
return NULL;
p2p->cfg = (struct p2p_config *) (p2p + 1);
os_memcpy(p2p->cfg, cfg, sizeof(*cfg));
if (cfg->dev_name)
p2p->cfg->dev_name = os_strdup(cfg->dev_name);
if (cfg->manufacturer)
p2p->cfg->manufacturer = os_strdup(cfg->manufacturer);
if (cfg->model_name)
p2p->cfg->model_name = os_strdup(cfg->model_name);
if (cfg->model_number)
p2p->cfg->model_number = os_strdup(cfg->model_number);
if (cfg->serial_number)
p2p->cfg->serial_number = os_strdup(cfg->serial_number);
if (cfg->pref_chan) {
p2p->cfg->pref_chan = os_malloc(cfg->num_pref_chan *
sizeof(struct p2p_channel));
if (p2p->cfg->pref_chan) {
os_memcpy(p2p->cfg->pref_chan, cfg->pref_chan,
cfg->num_pref_chan *
sizeof(struct p2p_channel));
} else
p2p->cfg->num_pref_chan = 0;
}
#ifdef ANDROID_P2P
/* 100ms listen time is too less to receive the response frames in some scenarios
* increasing min listen time to 200ms.
*/
p2p->min_disc_int = 2;
/* SD_FAIR_POLICY: Initializing the SD current serviced pointer to NULL */
p2p->sd_dev_list = NULL;
#else
p2p->min_disc_int = 1;
#endif
p2p->max_disc_int = 3;
os_get_random(&p2p->next_tie_breaker, 1);
p2p->next_tie_breaker &= 0x01;
if (cfg->sd_request)
p2p->dev_capab |= P2P_DEV_CAPAB_SERVICE_DISCOVERY;
p2p->dev_capab |= P2P_DEV_CAPAB_INVITATION_PROCEDURE;
if (cfg->concurrent_operations)
p2p->dev_capab |= P2P_DEV_CAPAB_CONCURRENT_OPER;
p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
dl_list_init(&p2p->devices);
eloop_register_timeout(P2P_PEER_EXPIRATION_INTERVAL, 0,
p2p_expiration_timeout, p2p, NULL);
p2p->go_timeout = 100;
p2p->client_timeout = 20;
return p2p;
}
void p2p_deinit(struct p2p_data *p2p)
{
#ifdef CONFIG_WIFI_DISPLAY
wpabuf_free(p2p->wfd_ie_beacon);
wpabuf_free(p2p->wfd_ie_probe_req);
wpabuf_free(p2p->wfd_ie_probe_resp);
wpabuf_free(p2p->wfd_ie_assoc_req);
wpabuf_free(p2p->wfd_ie_invitation);
wpabuf_free(p2p->wfd_ie_prov_disc_req);
wpabuf_free(p2p->wfd_ie_prov_disc_resp);
wpabuf_free(p2p->wfd_ie_go_neg);
wpabuf_free(p2p->wfd_dev_info);
wpabuf_free(p2p->wfd_assoc_bssid);
wpabuf_free(p2p->wfd_coupled_sink_info);
#endif /* CONFIG_WIFI_DISPLAY */
eloop_cancel_timeout(p2p_expiration_timeout, p2p, NULL);
eloop_cancel_timeout(p2p_ext_listen_timeout, p2p, NULL);
eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
p2p_flush(p2p);
p2p_free_req_dev_types(p2p);
os_free(p2p->cfg->dev_name);
os_free(p2p->cfg->manufacturer);
os_free(p2p->cfg->model_name);
os_free(p2p->cfg->model_number);
os_free(p2p->cfg->serial_number);
os_free(p2p->cfg->pref_chan);
os_free(p2p->groups);
wpabuf_free(p2p->sd_resp);
os_free(p2p->after_scan_tx);
p2p_remove_wps_vendor_extensions(p2p);
os_free(p2p);
}
void p2p_flush(struct p2p_data *p2p)
{
struct p2p_device *dev, *prev;
p2p_stop_find(p2p);
dl_list_for_each_safe(dev, prev, &p2p->devices, struct p2p_device,
list) {
dl_list_del(&dev->list);
p2p_device_free(p2p, dev);
}
#ifdef ANDROID_P2P
/* SD_FAIR_POLICY: Initializing the SD current serviced pointer to NULL */
p2p->sd_dev_list = NULL;
#endif
p2p_free_sd_queries(p2p);
os_free(p2p->after_scan_tx);
p2p->after_scan_tx = NULL;
}
int p2p_unauthorize(struct p2p_data *p2p, const u8 *addr)
{
struct p2p_device *dev;
dev = p2p_get_device(p2p, addr);
if (dev == NULL)
return -1;
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Unauthorizing " MACSTR,
MAC2STR(addr));
if (p2p->go_neg_peer == dev)
p2p->go_neg_peer = NULL;
dev->wps_method = WPS_NOT_READY;
dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
/* Check if after_scan_tx is for this peer. If so free it */
if (p2p->after_scan_tx &&
os_memcmp(addr, p2p->after_scan_tx->dst, ETH_ALEN) == 0) {
os_free(p2p->after_scan_tx);
p2p->after_scan_tx = NULL;
}
return 0;
}
int p2p_set_dev_name(struct p2p_data *p2p, const char *dev_name)
{
os_free(p2p->cfg->dev_name);
if (dev_name) {
p2p->cfg->dev_name = os_strdup(dev_name);
if (p2p->cfg->dev_name == NULL)
return -1;
} else
p2p->cfg->dev_name = NULL;
return 0;
}
int p2p_set_manufacturer(struct p2p_data *p2p, const char *manufacturer)
{
os_free(p2p->cfg->manufacturer);
p2p->cfg->manufacturer = NULL;
if (manufacturer) {
p2p->cfg->manufacturer = os_strdup(manufacturer);
if (p2p->cfg->manufacturer == NULL)
return -1;
}
return 0;
}
int p2p_set_model_name(struct p2p_data *p2p, const char *model_name)
{
os_free(p2p->cfg->model_name);
p2p->cfg->model_name = NULL;
if (model_name) {
p2p->cfg->model_name = os_strdup(model_name);
if (p2p->cfg->model_name == NULL)
return -1;
}
return 0;
}
int p2p_set_model_number(struct p2p_data *p2p, const char *model_number)
{
os_free(p2p->cfg->model_number);
p2p->cfg->model_number = NULL;
if (model_number) {
p2p->cfg->model_number = os_strdup(model_number);
if (p2p->cfg->model_number == NULL)
return -1;
}
return 0;
}
int p2p_set_serial_number(struct p2p_data *p2p, const char *serial_number)
{
os_free(p2p->cfg->serial_number);
p2p->cfg->serial_number = NULL;
if (serial_number) {
p2p->cfg->serial_number = os_strdup(serial_number);
if (p2p->cfg->serial_number == NULL)
return -1;
}
return 0;
}
void p2p_set_config_methods(struct p2p_data *p2p, u16 config_methods)
{
p2p->cfg->config_methods = config_methods;
}
void p2p_set_uuid(struct p2p_data *p2p, const u8 *uuid)
{
os_memcpy(p2p->cfg->uuid, uuid, 16);
}
int p2p_set_pri_dev_type(struct p2p_data *p2p, const u8 *pri_dev_type)
{
os_memcpy(p2p->cfg->pri_dev_type, pri_dev_type, 8);
return 0;
}
int p2p_set_sec_dev_types(struct p2p_data *p2p, const u8 dev_types[][8],
size_t num_dev_types)
{
if (num_dev_types > P2P_SEC_DEVICE_TYPES)
num_dev_types = P2P_SEC_DEVICE_TYPES;
p2p->cfg->num_sec_dev_types = num_dev_types;
os_memcpy(p2p->cfg->sec_dev_type, dev_types, num_dev_types * 8);
return 0;
}
void p2p_remove_wps_vendor_extensions(struct p2p_data *p2p)
{
int i;
for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
wpabuf_free(p2p->wps_vendor_ext[i]);
p2p->wps_vendor_ext[i] = NULL;
}
}
int p2p_add_wps_vendor_extension(struct p2p_data *p2p,
const struct wpabuf *vendor_ext)
{
int i;
if (vendor_ext == NULL)
return -1;
for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
if (p2p->wps_vendor_ext[i] == NULL)
break;
}
if (i >= P2P_MAX_WPS_VENDOR_EXT)
return -1;
p2p->wps_vendor_ext[i] = wpabuf_dup(vendor_ext);
if (p2p->wps_vendor_ext[i] == NULL)
return -1;
return 0;
}
int p2p_set_country(struct p2p_data *p2p, const char *country)
{
os_memcpy(p2p->cfg->country, country, 3);
return 0;
}
void p2p_continue_find(struct p2p_data *p2p)
{
struct p2p_device *dev;
#ifdef ANDROID_P2P
int skip=1;
#endif
p2p_set_state(p2p, P2P_SEARCH);
dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
#ifdef ANDROID_P2P
/* SD_FAIR_POLICY: We need to give chance to all devices in the device list
* There may be a scenario, where a particular peer device have
* not registered any query response. When we send a SD request to such device,
* no response will be received. And if we continue to get probe responses from that device,
* and if that device happens to be on top in our device list,
* we will always continue to send SD requests always to that peer only.
* We will not be able to send SD requests to other devices in that case.
* This implementation keeps track of last serviced peer device.
* And then takes the next one from the device list, in the next iteration.
*/
if (p2p->sd_dev_list && p2p->sd_dev_list != &p2p->devices) {
if(skip) {
if ((&dev->list == p2p->sd_dev_list) ) {
skip = 0;
if (dev->list.next == &p2p->devices)
p2p->sd_dev_list = NULL;
}
continue;
}
}
p2p->sd_dev_list = &dev->list;
wpa_printf(MSG_DEBUG, "P2P: ### Servicing %p dev->flags 0x%x SD schedule %s devaddr " MACSTR,
p2p->sd_dev_list, dev->flags, dev->flags & P2P_DEV_SD_SCHEDULE ? "TRUE": "FALSE",
MAC2STR(dev->info.p2p_device_addr));
#endif
if (dev->flags & P2P_DEV_SD_SCHEDULE) {
if (p2p_start_sd(p2p, dev) == 0)
return;
else
break;
} else if (dev->req_config_methods &&
!(dev->flags & P2P_DEV_PD_FOR_JOIN)) {
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send "
"pending Provision Discovery Request to "
MACSTR " (config methods 0x%x)",
MAC2STR(dev->info.p2p_device_addr),
dev->req_config_methods);
if (p2p_send_prov_disc_req(p2p, dev, 0, 0) == 0)
return;
}
}
p2p_listen_in_find(p2p);
}
static void p2p_sd_cb(struct p2p_data *p2p, int success)
{
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: Service Discovery Query TX callback: success=%d",
success);
p2p->pending_action_state = P2P_NO_PENDING_ACTION;
if (!success) {
if (p2p->sd_peer) {
p2p->sd_peer->flags &= ~P2P_DEV_SD_SCHEDULE;
p2p->sd_peer = NULL;
}
p2p_continue_find(p2p);
return;
}
if (p2p->sd_peer == NULL) {
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: No SD peer entry known");
p2p_continue_find(p2p);
return;
}
/* Wait for response from the peer */
p2p_set_state(p2p, P2P_SD_DURING_FIND);
p2p_set_timeout(p2p, 0, 200000);
}
/**
* p2p_retry_pd - Retry any pending provision disc requests in IDLE state
* @p2p: P2P module context from p2p_init()
*/
static void p2p_retry_pd(struct p2p_data *p2p)
{
struct p2p_device *dev;
if (p2p->state != P2P_IDLE)
return;
/*
* Retry the prov disc req attempt only for the peer that the user had
* requested for and provided a join has not been initiated on it
* in the meantime.
*/
dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
if (os_memcmp(p2p->pending_pd_devaddr,
dev->info.p2p_device_addr, ETH_ALEN) != 0)
continue;
if (!dev->req_config_methods)
continue;
if (dev->flags & P2P_DEV_PD_FOR_JOIN)
continue;
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send "
"pending Provision Discovery Request to "
MACSTR " (config methods 0x%x)",
MAC2STR(dev->info.p2p_device_addr),
dev->req_config_methods);
p2p_send_prov_disc_req(p2p, dev, 0, 0);
return;
}
}
static void p2p_prov_disc_cb(struct p2p_data *p2p, int success)
{
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: Provision Discovery Request TX callback: success=%d",
success);
/*
* Postpone resetting the pending action state till after we actually
* time out. This allows us to take some action like notifying any
* interested parties about no response to the request.
*
* When the timer (below) goes off we check in IDLE, SEARCH, or
* LISTEN_ONLY state, which are the only allowed states to issue a PD
* requests in, if this was still pending and then raise notification.
*/
if (!success) {
p2p->pending_action_state = P2P_NO_PENDING_ACTION;
if (p2p->user_initiated_pd &&
(p2p->state == P2P_SEARCH || p2p->state == P2P_LISTEN_ONLY))
{
/* Retry request from timeout to avoid busy loops */
p2p->pending_action_state = P2P_PENDING_PD;
p2p_set_timeout(p2p, 0, 50000);
} else if (p2p->state != P2P_IDLE)
p2p_continue_find(p2p);
else if (p2p->user_initiated_pd) {
p2p->pending_action_state = P2P_PENDING_PD;
#ifdef ANDROID_P2P
p2p_set_timeout(p2p, 0, 350000);
#else
p2p_set_timeout(p2p, 0, 300000);
#endif
}
return;
}
/*
* This postponing, of resetting pending_action_state, needs to be
* done only for user initiated PD requests and not internal ones.
*/
if (p2p->user_initiated_pd)
p2p->pending_action_state = P2P_PENDING_PD;
else
p2p->pending_action_state = P2P_NO_PENDING_ACTION;
/* Wait for response from the peer */
if (p2p->state == P2P_SEARCH)
p2p_set_state(p2p, P2P_PD_DURING_FIND);
#ifdef ANDROID_P2P
p2p_set_timeout(p2p, 0, 350000);
#else
p2p_set_timeout(p2p, 0, 200000);
#endif
}
int p2p_scan_res_handler(struct p2p_data *p2p, const u8 *bssid, int freq,
int level, const u8 *ies, size_t ies_len)
{
p2p_add_device(p2p, bssid, freq, level, ies, ies_len, 1);
return 0;
}
void p2p_scan_res_handled(struct p2p_data *p2p)
{
if (!p2p->p2p_scan_running) {
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan was not "
"running, but scan results received");
}
p2p->p2p_scan_running = 0;
eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
if (p2p_run_after_scan(p2p))
return;
if (p2p->state == P2P_SEARCH)
p2p_continue_find(p2p);
}
void p2p_scan_ie(struct p2p_data *p2p, struct wpabuf *ies, const u8 *dev_id)
{
u8 *len;
#ifdef CONFIG_WIFI_DISPLAY
if (p2p->wfd_ie_probe_req)
wpabuf_put_buf(ies, p2p->wfd_ie_probe_req);
#endif /* CONFIG_WIFI_DISPLAY */
len = p2p_buf_add_ie_hdr(ies);
p2p_buf_add_capability(ies, p2p->dev_capab &
~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY, 0);
if (dev_id)
p2p_buf_add_device_id(ies, dev_id);
if (p2p->cfg->reg_class && p2p->cfg->channel)
p2p_buf_add_listen_channel(ies, p2p->cfg->country,
p2p->cfg->reg_class,
p2p->cfg->channel);
if (p2p->ext_listen_interval)
p2p_buf_add_ext_listen_timing(ies, p2p->ext_listen_period,
p2p->ext_listen_interval);
/* TODO: p2p_buf_add_operating_channel() if GO */
p2p_buf_update_ie_hdr(ies, len);
}
size_t p2p_scan_ie_buf_len(struct p2p_data *p2p)
{
size_t len = 100;
#ifdef CONFIG_WIFI_DISPLAY
if (p2p && p2p->wfd_ie_probe_req)
len += wpabuf_len(p2p->wfd_ie_probe_req);
#endif /* CONFIG_WIFI_DISPLAY */
return len;
}
int p2p_ie_text(struct wpabuf *p2p_ie, char *buf, char *end)
{
return p2p_attr_text(p2p_ie, buf, end);
}
static void p2p_go_neg_req_cb(struct p2p_data *p2p, int success)
{
struct p2p_device *dev = p2p->go_neg_peer;
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: GO Negotiation Request TX callback: success=%d",
success);
if (dev == NULL) {
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: No pending GO Negotiation");
return;
}
if (success) {
if (dev->flags & P2P_DEV_USER_REJECTED) {
p2p_set_state(p2p, P2P_IDLE);
return;
}
} else if (dev->go_neg_req_sent) {
/* Cancel the increment from p2p_connect_send() on failure */
dev->go_neg_req_sent--;
}
if (!success &&
(dev->info.dev_capab & P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY) &&
!is_zero_ether_addr(dev->member_in_go_dev)) {
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: Peer " MACSTR " did not acknowledge request - "
"try to use device discoverability through its GO",
MAC2STR(dev->info.p2p_device_addr));
p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
p2p_send_dev_disc_req(p2p, dev);
return;
}
/*
* Use P2P find, if needed, to find the other device from its listen
* channel.
*/
p2p_set_state(p2p, P2P_CONNECT);
#ifdef ANDROID_P2P
p2p_set_timeout(p2p, 0, 350000);
#else
p2p_set_timeout(p2p, 0, success ? 200000 : 100000);
#endif
}
static void p2p_go_neg_resp_cb(struct p2p_data *p2p, int success)
{
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: GO Negotiation Response TX callback: success=%d",
success);
if (!p2p->go_neg_peer && p2p->state == P2P_PROVISIONING) {
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: Ignore TX callback event - GO Negotiation is "
"not running anymore");
return;
}
p2p_set_state(p2p, P2P_CONNECT);
#ifdef ANDROID_P2P
p2p_set_timeout(p2p, 0, 350000);
#else
p2p_set_timeout(p2p, 0, 250000);
#endif
}
static void p2p_go_neg_resp_failure_cb(struct p2p_data *p2p, int success)
{
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: GO Negotiation Response (failure) TX callback: "
"success=%d", success);
if (p2p->go_neg_peer && p2p->go_neg_peer->status != P2P_SC_SUCCESS) {
p2p_go_neg_failed(p2p, p2p->go_neg_peer,
p2p->go_neg_peer->status);
}
}
static void p2p_go_neg_conf_cb(struct p2p_data *p2p,
enum p2p_send_action_result result)
{
struct p2p_device *dev;
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: GO Negotiation Confirm TX callback: result=%d",
result);
p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
if (result == P2P_SEND_ACTION_FAILED) {
p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1);
return;
}
if (result == P2P_SEND_ACTION_NO_ACK) {
/*
* It looks like the TX status for GO Negotiation Confirm is
* often showing failure even when the peer has actually
* received the frame. Since the peer may change channels
* immediately after having received the frame, we may not see
* an Ack for retries, so just dropping a single frame may
* trigger this. To allow the group formation to succeed if the
* peer did indeed receive the frame, continue regardless of
* the TX status.
*/
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: Assume GO Negotiation Confirm TX was actually "
"received by the peer even though Ack was not "
"reported");
}
dev = p2p->go_neg_peer;
if (dev == NULL)
return;
p2p_go_complete(p2p, dev);
}
void p2p_send_action_cb(struct p2p_data *p2p, unsigned int freq, const u8 *dst,
const u8 *src, const u8 *bssid,
enum p2p_send_action_result result)
{
enum p2p_pending_action_state state;
int success;
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: Action frame TX callback (state=%d freq=%u dst=" MACSTR
" src=" MACSTR " bssid=" MACSTR " result=%d",
p2p->pending_action_state, freq, MAC2STR(dst), MAC2STR(src),
MAC2STR(bssid), result);
success = result == P2P_SEND_ACTION_SUCCESS;
state = p2p->pending_action_state;
p2p->pending_action_state = P2P_NO_PENDING_ACTION;
switch (state) {
case P2P_NO_PENDING_ACTION:
break;
case P2P_PENDING_GO_NEG_REQUEST:
p2p_go_neg_req_cb(p2p, success);
break;
case P2P_PENDING_GO_NEG_RESPONSE:
p2p_go_neg_resp_cb(p2p, success);
break;
case P2P_PENDING_GO_NEG_RESPONSE_FAILURE:
p2p_go_neg_resp_failure_cb(p2p, success);
break;
case P2P_PENDING_GO_NEG_CONFIRM:
p2p_go_neg_conf_cb(p2p, result);
break;
case P2P_PENDING_SD:
p2p_sd_cb(p2p, success);
break;
case P2P_PENDING_PD:
p2p_prov_disc_cb(p2p, success);
break;
case P2P_PENDING_INVITATION_REQUEST:
p2p_invitation_req_cb(p2p, success);
break;
case P2P_PENDING_INVITATION_RESPONSE:
p2p_invitation_resp_cb(p2p, success);
break;
case P2P_PENDING_DEV_DISC_REQUEST:
p2p_dev_disc_req_cb(p2p, success);
break;
case P2P_PENDING_DEV_DISC_RESPONSE:
p2p_dev_disc_resp_cb(p2p, success);
break;
case P2P_PENDING_GO_DISC_REQ:
p2p_go_disc_req_cb(p2p, success);
break;
}
}
void p2p_listen_cb(struct p2p_data *p2p, unsigned int freq,
unsigned int duration)
{
if (freq == p2p->pending_client_disc_freq) {
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: Client discoverability remain-awake completed");
p2p->pending_client_disc_freq = 0;
return;
}
if (freq != p2p->pending_listen_freq) {
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: Unexpected listen callback for freq=%u "
"duration=%u (pending_listen_freq=%u)",
freq, duration, p2p->pending_listen_freq);
return;
}
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: Starting Listen timeout(%u,%u) on freq=%u based on "
"callback",
p2p->pending_listen_sec, p2p->pending_listen_usec,
p2p->pending_listen_freq);
p2p->in_listen = 1;
p2p->drv_in_listen = freq;
if (p2p->pending_listen_sec || p2p->pending_listen_usec) {
/*
* Add 20 msec extra wait to avoid race condition with driver
* remain-on-channel end event, i.e., give driver more time to
* complete the operation before our timeout expires.
*/
p2p_set_timeout(p2p, p2p->pending_listen_sec,
p2p->pending_listen_usec + 20000);
}
p2p->pending_listen_freq = 0;
}
int p2p_listen_end(struct p2p_data *p2p, unsigned int freq)
{
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Driver ended Listen "
"state (freq=%u)", freq);
p2p->drv_in_listen = 0;
if (p2p->in_listen)
return 0; /* Internal timeout will trigger the next step */
if (p2p->state == P2P_CONNECT_LISTEN && p2p->go_neg_peer) {
if (p2p->go_neg_peer->connect_reqs >= 120) {
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: Timeout on sending GO Negotiation "
"Request without getting response");
p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1);
return 0;
}
p2p_set_state(p2p, P2P_CONNECT);
p2p_connect_send(p2p, p2p->go_neg_peer);
return 1;
} else if (p2p->state == P2P_SEARCH) {
if (p2p->p2p_scan_running) {
/*
* Search is already in progress. This can happen if
* an Action frame RX is reported immediately after
* the end of a remain-on-channel operation and the
* response frame to that is sent using an offchannel
* operation while in p2p_find. Avoid an attempt to
* restart a scan here.
*/
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan "
"already in progress - do not try to start a "
"new one");
return 1;
}
if (p2p->pending_listen_freq) {
/*
* Better wait a bit if the driver is unable to start
* offchannel operation for some reason. p2p_search()
* will be started from internal timeout.
*/
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Listen "
"operation did not seem to start - delay "
"search phase to avoid busy loop");
p2p_set_timeout(p2p, 0, 100000);
return 1;
}
if (p2p->search_delay) {
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Delay "
"search operation by %u ms",
p2p->search_delay);
p2p_set_timeout(p2p, p2p->search_delay / 1000,
(p2p->search_delay % 1000) * 1000);
return 1;
}
p2p_search(p2p);
return 1;
}
return 0;
}
static void p2p_timeout_connect(struct p2p_data *p2p)
{
p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
if (p2p->go_neg_peer &&
(p2p->go_neg_peer->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) {
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Wait for GO "
"Negotiation Confirm timed out - assume GO "
"Negotiation failed");
p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1);
return;
}
p2p_set_state(p2p, P2P_CONNECT_LISTEN);
p2p_listen_in_find(p2p);
}
static void p2p_timeout_connect_listen(struct p2p_data *p2p)
{
if (p2p->go_neg_peer) {
if (p2p->drv_in_listen) {
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Driver is "
"still in Listen state; wait for it to "
"complete");
return;
}
if (p2p->go_neg_peer->connect_reqs >= 120) {
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: Timeout on sending GO Negotiation "
"Request without getting response");
p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1);
return;
}
p2p_set_state(p2p, P2P_CONNECT);
p2p_connect_send(p2p, p2p->go_neg_peer);
} else
p2p_set_state(p2p, P2P_IDLE);
}
static void p2p_timeout_wait_peer_connect(struct p2p_data *p2p)
{
/*
* TODO: could remain constantly in Listen state for some time if there
* are no other concurrent uses for the radio. For now, go to listen
* state once per second to give other uses a chance to use the radio.
*/
p2p_set_state(p2p, P2P_WAIT_PEER_IDLE);
p2p_set_timeout(p2p, 0, 500000);
}
static void p2p_timeout_wait_peer_idle(struct p2p_data *p2p)
{
struct p2p_device *dev = p2p->go_neg_peer;
if (dev == NULL) {
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: Unknown GO Neg peer - stop GO Neg wait");
return;
}
dev->wait_count++;
if (dev->wait_count >= 120) {
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: Timeout on waiting peer to become ready for GO "
"Negotiation");
p2p_go_neg_failed(p2p, dev, -1);
return;
}
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: Go to Listen state while waiting for the peer to become "
"ready for GO Negotiation");
p2p_set_state(p2p, P2P_WAIT_PEER_CONNECT);
p2p_listen_in_find(p2p);
}
static void p2p_timeout_sd_during_find(struct p2p_data *p2p)
{
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: Service Discovery Query timeout");
if (p2p->sd_peer) {
p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
p2p->sd_peer->flags &= ~P2P_DEV_SD_SCHEDULE;
p2p->sd_peer = NULL;
}
p2p_continue_find(p2p);
}
static void p2p_timeout_prov_disc_during_find(struct p2p_data *p2p)
{
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: Provision Discovery Request timeout");
p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
p2p_continue_find(p2p);
}
static void p2p_timeout_prov_disc_req(struct p2p_data *p2p)
{
p2p->pending_action_state = P2P_NO_PENDING_ACTION;
/*
* For user initiated PD requests that we have not gotten any responses
* for while in IDLE state, we retry them a couple of times before
* giving up.
*/
if (!p2p->user_initiated_pd)
return;
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: User initiated Provision Discovery Request timeout");
if (p2p->pd_retries) {
p2p->pd_retries--;
p2p_retry_pd(p2p);
} else {
if (p2p->cfg->prov_disc_fail)
p2p->cfg->prov_disc_fail(p2p->cfg->cb_ctx,
p2p->pending_pd_devaddr,
P2P_PROV_DISC_TIMEOUT);
p2p_reset_pending_pd(p2p);
}
}
static void p2p_timeout_invite(struct p2p_data *p2p)
{
p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
p2p_set_state(p2p, P2P_INVITE_LISTEN);
if (p2p->inv_role == P2P_INVITE_ROLE_ACTIVE_GO) {
/*
* Better remain on operating channel instead of listen channel
* when running a group.
*/
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Inviting in "
"active GO role - wait on operating channel");
p2p_set_timeout(p2p, 0, 100000);
return;
}
p2p_listen_in_find(p2p);
}
static void p2p_timeout_invite_listen(struct p2p_data *p2p)
{
if (p2p->invite_peer && p2p->invite_peer->invitation_reqs < 100) {
p2p_set_state(p2p, P2P_INVITE);
p2p_invite_send(p2p, p2p->invite_peer,
p2p->invite_go_dev_addr);
} else {
if (p2p->invite_peer) {
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: Invitation Request retry limit reached");
if (p2p->cfg->invitation_result)
p2p->cfg->invitation_result(
p2p->cfg->cb_ctx, -1, NULL);
}
p2p_set_state(p2p, P2P_IDLE);
}
}
static void p2p_state_timeout(void *eloop_ctx, void *timeout_ctx)
{
struct p2p_data *p2p = eloop_ctx;
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Timeout (state=%s)",
p2p_state_txt(p2p->state));
p2p->in_listen = 0;
switch (p2p->state) {
case P2P_IDLE:
/* Check if we timed out waiting for PD req */
if (p2p->pending_action_state == P2P_PENDING_PD)
p2p_timeout_prov_disc_req(p2p);
break;
case P2P_SEARCH:
/* Check if we timed out waiting for PD req */
if (p2p->pending_action_state == P2P_PENDING_PD)
p2p_timeout_prov_disc_req(p2p);
if (p2p->search_delay && !p2p->in_search_delay) {
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Delay "
"search operation by %u ms",
p2p->search_delay);
p2p->in_search_delay = 1;
p2p_set_timeout(p2p, p2p->search_delay / 1000,
(p2p->search_delay % 1000) * 1000);
break;
}
p2p->in_search_delay = 0;
p2p_search(p2p);
break;
case P2P_CONNECT:
p2p_timeout_connect(p2p);
break;
case P2P_CONNECT_LISTEN:
p2p_timeout_connect_listen(p2p);
break;
case P2P_GO_NEG:
break;
case P2P_LISTEN_ONLY:
/* Check if we timed out waiting for PD req */
if (p2p->pending_action_state == P2P_PENDING_PD)
p2p_timeout_prov_disc_req(p2p);
if (p2p->ext_listen_only) {
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: Extended Listen Timing - Listen State "
"completed");
p2p->ext_listen_only = 0;
p2p_set_state(p2p, P2P_IDLE);
}
break;
case P2P_WAIT_PEER_CONNECT:
p2p_timeout_wait_peer_connect(p2p);
break;
case P2P_WAIT_PEER_IDLE:
p2p_timeout_wait_peer_idle(p2p);
break;
case P2P_SD_DURING_FIND:
p2p_timeout_sd_during_find(p2p);
break;
case P2P_PROVISIONING:
break;
case P2P_PD_DURING_FIND:
p2p_timeout_prov_disc_during_find(p2p);
break;
case P2P_INVITE:
p2p_timeout_invite(p2p);
break;
case P2P_INVITE_LISTEN:
p2p_timeout_invite_listen(p2p);
break;
case P2P_SEARCH_WHEN_READY:
break;
case P2P_CONTINUE_SEARCH_WHEN_READY:
break;
}
}
int p2p_reject(struct p2p_data *p2p, const u8 *peer_addr)
{
struct p2p_device *dev;
dev = p2p_get_device(p2p, peer_addr);
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Local request to reject "
"connection attempts by peer " MACSTR, MAC2STR(peer_addr));
if (dev == NULL) {
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer " MACSTR
" unknown", MAC2STR(peer_addr));
return -1;
}
dev->status = P2P_SC_FAIL_REJECTED_BY_USER;
dev->flags |= P2P_DEV_USER_REJECTED;
return 0;
}
const char * p2p_wps_method_text(enum p2p_wps_method method)
{
switch (method) {
case WPS_NOT_READY:
return "not-ready";
case WPS_PIN_DISPLAY:
return "Display";
case WPS_PIN_KEYPAD:
return "Keypad";
case WPS_PBC:
return "PBC";
}
return "??";
}
static const char * p2p_go_state_text(enum p2p_go_state go_state)
{
switch (go_state) {
case UNKNOWN_GO:
return "unknown";
case LOCAL_GO:
return "local";
case REMOTE_GO:
return "remote";
}
return "??";
}
const struct p2p_peer_info * p2p_get_peer_info(struct p2p_data *p2p,
const u8 *addr, int next)
{
struct p2p_device *dev;
if (addr)
dev = p2p_get_device(p2p, addr);
else
dev = dl_list_first(&p2p->devices, struct p2p_device, list);
if (dev && next) {
dev = dl_list_first(&dev->list, struct p2p_device, list);
if (&dev->list == &p2p->devices)
dev = NULL;
}
if (dev == NULL)
return NULL;
return &dev->info;
}
int p2p_get_peer_info_txt(const struct p2p_peer_info *info,
char *buf, size_t buflen)
{
struct p2p_device *dev;
int res;
char *pos, *end;
struct os_time now;
if (info == NULL)
return -1;
dev = (struct p2p_device *) (((u8 *) info) -
offsetof(struct p2p_device, info));
pos = buf;
end = buf + buflen;
os_get_time(&now);
res = os_snprintf(pos, end - pos,
"age=%d\n"
"listen_freq=%d\n"
"wps_method=%s\n"
"interface_addr=" MACSTR "\n"
"member_in_go_dev=" MACSTR "\n"
"member_in_go_iface=" MACSTR "\n"
"go_neg_req_sent=%d\n"
"go_state=%s\n"
"dialog_token=%u\n"
"intended_addr=" MACSTR "\n"
"country=%c%c\n"
"oper_freq=%d\n"
"req_config_methods=0x%x\n"
"flags=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n"
"status=%d\n"
"wait_count=%u\n"
"invitation_reqs=%u\n",
(int) (now.sec - dev->last_seen.sec),
dev->listen_freq,
p2p_wps_method_text(dev->wps_method),
MAC2STR(dev->interface_addr),
MAC2STR(dev->member_in_go_dev),
MAC2STR(dev->member_in_go_iface),
dev->go_neg_req_sent,
p2p_go_state_text(dev->go_state),
dev->dialog_token,
MAC2STR(dev->intended_addr),
dev->country[0] ? dev->country[0] : '_',
dev->country[1] ? dev->country[1] : '_',
dev->oper_freq,
dev->req_config_methods,
dev->flags & P2P_DEV_PROBE_REQ_ONLY ?
"[PROBE_REQ_ONLY]" : "",
dev->flags & P2P_DEV_REPORTED ? "[REPORTED]" : "",
dev->flags & P2P_DEV_NOT_YET_READY ?
"[NOT_YET_READY]" : "",
dev->flags & P2P_DEV_SD_INFO ? "[SD_INFO]" : "",
dev->flags & P2P_DEV_SD_SCHEDULE ? "[SD_SCHEDULE]" :
"",
dev->flags & P2P_DEV_PD_PEER_DISPLAY ?
"[PD_PEER_DISPLAY]" : "",
dev->flags & P2P_DEV_PD_PEER_KEYPAD ?
"[PD_PEER_KEYPAD]" : "",
dev->flags & P2P_DEV_USER_REJECTED ?
"[USER_REJECTED]" : "",
dev->flags & P2P_DEV_PEER_WAITING_RESPONSE ?
"[PEER_WAITING_RESPONSE]" : "",
dev->flags & P2P_DEV_PREFER_PERSISTENT_GROUP ?
"[PREFER_PERSISTENT_GROUP]" : "",
dev->flags & P2P_DEV_WAIT_GO_NEG_RESPONSE ?
"[WAIT_GO_NEG_RESPONSE]" : "",
dev->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM ?
"[WAIT_GO_NEG_CONFIRM]" : "",
dev->flags & P2P_DEV_GROUP_CLIENT_ONLY ?
"[GROUP_CLIENT_ONLY]" : "",
dev->flags & P2P_DEV_FORCE_FREQ ?
"[FORCE_FREQ]" : "",
dev->flags & P2P_DEV_PD_FOR_JOIN ?
"[PD_FOR_JOIN]" : "",
dev->status,
dev->wait_count,
dev->invitation_reqs);
if (res < 0 || res >= end - pos)
return pos - buf;
pos += res;
if (dev->ext_listen_period) {
res = os_snprintf(pos, end - pos,
"ext_listen_period=%u\n"
"ext_listen_interval=%u\n",
dev->ext_listen_period,
dev->ext_listen_interval);
if (res < 0 || res >= end - pos)
return pos - buf;
pos += res;
}
if (dev->oper_ssid_len) {
res = os_snprintf(pos, end - pos,
"oper_ssid=%s\n",
wpa_ssid_txt(dev->oper_ssid,
dev->oper_ssid_len));
if (res < 0 || res >= end - pos)
return pos - buf;
pos += res;
}
#ifdef CONFIG_WIFI_DISPLAY
if (dev->info.wfd_subelems) {
res = os_snprintf(pos, end - pos, "wfd_subelems=");
if (res < 0 || res >= end - pos)
return pos - buf;
pos += res;
pos += wpa_snprintf_hex(pos, end - pos,
wpabuf_head(dev->info.wfd_subelems),
wpabuf_len(dev->info.wfd_subelems));
res = os_snprintf(pos, end - pos, "\n");
if (res < 0 || res >= end - pos)
return pos - buf;
pos += res;
}
#endif /* CONFIG_WIFI_DISPLAY */
return pos - buf;
}
int p2p_peer_known(struct p2p_data *p2p, const u8 *addr)
{
return p2p_get_device(p2p, addr) != NULL;
}
void p2p_set_client_discoverability(struct p2p_data *p2p, int enabled)
{
if (enabled) {
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Client "
"discoverability enabled");
p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
} else {
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Client "
"discoverability disabled");
p2p->dev_capab &= ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
}
}
static struct wpabuf * p2p_build_presence_req(u32 duration1, u32 interval1,
u32 duration2, u32 interval2)
{
struct wpabuf *req;
struct p2p_noa_desc desc1, desc2, *ptr1 = NULL, *ptr2 = NULL;
u8 *len;
req = wpabuf_alloc(100);
if (req == NULL)
return NULL;
if (duration1 || interval1) {
os_memset(&desc1, 0, sizeof(desc1));
desc1.count_type = 1;
desc1.duration = duration1;
desc1.interval = interval1;
ptr1 = &desc1;
if (duration2 || interval2) {
os_memset(&desc2, 0, sizeof(desc2));
desc2.count_type = 2;
desc2.duration = duration2;
desc2.interval = interval2;
ptr2 = &desc2;
}
}
p2p_buf_add_action_hdr(req, P2P_PRESENCE_REQ, 1);
len = p2p_buf_add_ie_hdr(req);
p2p_buf_add_noa(req, 0, 0, 0, ptr1, ptr2);
p2p_buf_update_ie_hdr(req, len);
return req;
}
int p2p_presence_req(struct p2p_data *p2p, const u8 *go_interface_addr,
const u8 *own_interface_addr, unsigned int freq,
u32 duration1, u32 interval1, u32 duration2,
u32 interval2)
{
struct wpabuf *req;
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send Presence Request to "
"GO " MACSTR " (own interface " MACSTR ") freq=%u dur1=%u "
"int1=%u dur2=%u int2=%u",
MAC2STR(go_interface_addr), MAC2STR(own_interface_addr),
freq, duration1, interval1, duration2, interval2);
req = p2p_build_presence_req(duration1, interval1, duration2,
interval2);
if (req == NULL)
return -1;
p2p->pending_action_state = P2P_NO_PENDING_ACTION;
if (p2p_send_action(p2p, freq, go_interface_addr, own_interface_addr,
go_interface_addr,
wpabuf_head(req), wpabuf_len(req), 200) < 0) {
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: Failed to send Action frame");
}
wpabuf_free(req);
return 0;
}
static struct wpabuf * p2p_build_presence_resp(u8 status, const u8 *noa,
size_t noa_len, u8 dialog_token)
{
struct wpabuf *resp;
u8 *len;
resp = wpabuf_alloc(100 + noa_len);
if (resp == NULL)
return NULL;
p2p_buf_add_action_hdr(resp, P2P_PRESENCE_RESP, dialog_token);
len = p2p_buf_add_ie_hdr(resp);
p2p_buf_add_status(resp, status);
if (noa) {
wpabuf_put_u8(resp, P2P_ATTR_NOTICE_OF_ABSENCE);
wpabuf_put_le16(resp, noa_len);
wpabuf_put_data(resp, noa, noa_len);
} else
p2p_buf_add_noa(resp, 0, 0, 0, NULL, NULL);
p2p_buf_update_ie_hdr(resp, len);
return resp;
}
static void p2p_process_presence_req(struct p2p_data *p2p, const u8 *da,
const u8 *sa, const u8 *data, size_t len,
int rx_freq)
{
struct p2p_message msg;
u8 status;
struct wpabuf *resp;
size_t g;
struct p2p_group *group = NULL;
int parsed = 0;
u8 noa[50];
int noa_len;
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: Received P2P Action - P2P Presence Request");
for (g = 0; g < p2p->num_groups; g++) {
if (os_memcmp(da, p2p_group_get_interface_addr(p2p->groups[g]),
ETH_ALEN) == 0) {
group = p2p->groups[g];
break;
}
}
if (group == NULL) {
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: Ignore P2P Presence Request for unknown group "
MACSTR, MAC2STR(da));
return;
}
if (p2p_parse(data, len, &msg) < 0) {
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: Failed to parse P2P Presence Request");
status = P2P_SC_FAIL_INVALID_PARAMS;
goto fail;
}
parsed = 1;
if (msg.noa == NULL) {
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: No NoA attribute in P2P Presence Request");
status = P2P_SC_FAIL_INVALID_PARAMS;
goto fail;
}
status = p2p_group_presence_req(group, sa, msg.noa, msg.noa_len);
fail:
if (p2p->cfg->get_noa)
noa_len = p2p->cfg->get_noa(p2p->cfg->cb_ctx, da, noa,
sizeof(noa));
else
noa_len = -1;
resp = p2p_build_presence_resp(status, noa_len > 0 ? noa : NULL,
noa_len > 0 ? noa_len : 0,
msg.dialog_token);
if (parsed)
p2p_parse_free(&msg);
if (resp == NULL)
return;
p2p->pending_action_state = P2P_NO_PENDING_ACTION;
if (p2p_send_action(p2p, rx_freq, sa, da, da,
wpabuf_head(resp), wpabuf_len(resp), 200) < 0) {
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: Failed to send Action frame");
}
wpabuf_free(resp);
}
static void p2p_process_presence_resp(struct p2p_data *p2p, const u8 *da,
const u8 *sa, const u8 *data, size_t len)
{
struct p2p_message msg;
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: Received P2P Action - P2P Presence Response");
if (p2p_parse(data, len, &msg) < 0) {
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: Failed to parse P2P Presence Response");
return;
}
if (msg.status == NULL || msg.noa == NULL) {
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: No Status or NoA attribute in P2P Presence "
"Response");
p2p_parse_free(&msg);
return;
}
if (*msg.status) {
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: P2P Presence Request was rejected: status %u",
*msg.status);
p2p_parse_free(&msg);
return;
}
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: P2P Presence Request was accepted");
wpa_hexdump(MSG_DEBUG, "P2P: P2P Presence Response - NoA",
msg.noa, msg.noa_len);
/* TODO: process NoA */
p2p_parse_free(&msg);
}
static void p2p_ext_listen_timeout(void *eloop_ctx, void *timeout_ctx)
{
struct p2p_data *p2p = eloop_ctx;
if (p2p->ext_listen_interval) {
/* Schedule next extended listen timeout */
eloop_register_timeout(p2p->ext_listen_interval_sec,
p2p->ext_listen_interval_usec,
p2p_ext_listen_timeout, p2p, NULL);
}
if (p2p->state == P2P_LISTEN_ONLY && p2p->ext_listen_only) {
/*
* This should not really happen, but it looks like the Listen
* command may fail is something else (e.g., a scan) was
* running at an inconvenient time. As a workaround, allow new
* Extended Listen operation to be started.
*/
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Previous "
"Extended Listen operation had not been completed - "
"try again");
p2p->ext_listen_only = 0;
p2p_set_state(p2p, P2P_IDLE);
}
if (p2p->state != P2P_IDLE) {
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Skip Extended "
"Listen timeout in active state (%s)",
p2p_state_txt(p2p->state));
return;
}
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Extended Listen timeout");
p2p->ext_listen_only = 1;
if (p2p_listen(p2p, p2p->ext_listen_period) < 0) {
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Failed to start "
"Listen state for Extended Listen Timing");
p2p->ext_listen_only = 0;
}
}
int p2p_ext_listen(struct p2p_data *p2p, unsigned int period,
unsigned int interval)
{
if (period > 65535 || interval > 65535 || period > interval ||
(period == 0 && interval > 0) || (period > 0 && interval == 0)) {
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: Invalid Extended Listen Timing request: "
"period=%u interval=%u", period, interval);
return -1;
}
eloop_cancel_timeout(p2p_ext_listen_timeout, p2p, NULL);
if (interval == 0) {
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: Disabling Extended Listen Timing");
p2p->ext_listen_period = 0;
p2p->ext_listen_interval = 0;
return 0;
}
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
"P2P: Enabling Extended Listen Timing: period %u msec, "
"interval %u msec", period, interval);
p2p->ext_listen_period = period;
p2p->ext_listen_interval = interval;
p2p->ext_listen_interval_sec = interval / 1000;
p2p->ext_listen_interval_usec = (interval % 1000) * 1000;
eloop_register_timeout(p2p->ext_listen_interval_sec,
p2p->ext_listen_interval_usec,
p2p_ext_listen_timeout, p2p, NULL);
return 0;
}
void p2p_deauth_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code,
const u8 *ie, size_t ie_len)
{
struct p2p_message msg;
if (bssid == NULL || ie == NULL)
return;
os_memset(&msg, 0, sizeof(msg));
if (p2p_parse_ies(ie, ie_len, &msg))
return;
if (msg.minor_reason_code == NULL)
return;
wpa_msg(p2p->cfg->msg_ctx, MSG_INFO,
"P2P: Deauthentication notification BSSID " MACSTR
" reason_code=%u minor_reason_code=%u",
MAC2STR(bssid), reason_code, *msg.minor_reason_code);
p2p_parse_free(&msg);
}
void p2p_disassoc_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code,
const u8 *ie, size_t ie_len)
{
struct p2p_message msg;
if (bssid == NULL || ie == NULL)
return;
os_memset(&msg, 0, sizeof(msg));
if (p2p_parse_ies(ie, ie_len, &msg))
return;
if (msg.minor_reason_code == NULL)
return;
wpa_msg(p2p->cfg->msg_ctx, MSG_INFO,
"P2P: Disassociation notification BSSID " MACSTR
" reason_code=%u minor_reason_code=%u",
MAC2STR(bssid), reason_code, *msg.minor_reason_code);
p2p_parse_free(&msg);
}
void p2p_set_managed_oper(struct p2p_data *p2p, int enabled)
{
if (enabled) {
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Managed P2P "
"Device operations enabled");
p2p->dev_capab |= P2P_DEV_CAPAB_INFRA_MANAGED;
} else {
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Managed P2P "
"Device operations disabled");
p2p->dev_capab &= ~P2P_DEV_CAPAB_INFRA_MANAGED;
}
}
int p2p_set_listen_channel(struct p2p_data *p2p, u8 reg_class, u8 channel)
{
if (p2p_channel_to_freq(p2p->cfg->country, reg_class, channel) < 0)
return -1;
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Set Listen channel: "
"reg_class %u channel %u", reg_class, channel);
p2p->cfg->reg_class = reg_class;
p2p->cfg->channel = channel;
return 0;
}
int p2p_set_ssid_postfix(struct p2p_data *p2p, const u8 *postfix, size_t len)
{
wpa_hexdump_ascii(MSG_DEBUG, "P2P: New SSID postfix", postfix, len);
if (postfix == NULL) {
p2p->cfg->ssid_postfix_len = 0;
return 0;
}
if (len > sizeof(p2p->cfg->ssid_postfix))
return -1;
os_memcpy(p2p->cfg->ssid_postfix, postfix, len);
p2p->cfg->ssid_postfix_len = len;
return 0;
}
int p2p_set_oper_channel(struct p2p_data *p2p, u8 op_reg_class, u8 op_channel,
int cfg_op_channel)
{
if (p2p_channel_to_freq(p2p->cfg->country, op_reg_class, op_channel)
< 0)
return -1;
wpa_msg(p2p->cfg->msg_ctx, MSG_INFO, "P2P: Set Operating channel: "
"reg_class %u channel %u", op_reg_class, op_channel);
p2p->cfg->op_reg_class = op_reg_class;
p2p->cfg->op_channel = op_channel;
p2p->cfg->cfg_op_channel = cfg_op_channel;
return 0;
}
int p2p_set_pref_chan(struct p2p_data *p2p, unsigned int num_pref_chan,
const struct p2p_channel *pref_chan)
{
struct p2p_channel *n;
if (pref_chan) {
n = os_malloc(num_pref_chan * sizeof(struct p2p_channel));
if (n == NULL)
return -1;
os_memcpy(n, pref_chan,
num_pref_chan * sizeof(struct p2p_channel));
} else
n = NULL;
os_free(p2p->cfg->pref_chan);
p2p->cfg->pref_chan = n;
p2p->cfg->num_pref_chan = num_pref_chan;
return 0;
}
int p2p_get_interface_addr(struct p2p_data *p2p, const u8 *dev_addr,
u8 *iface_addr)
{
struct p2p_device *dev = p2p_get_device(p2p, dev_addr);
if (dev == NULL || is_zero_ether_addr(dev->interface_addr))
return -1;
os_memcpy(iface_addr, dev->interface_addr, ETH_ALEN);
return 0;
}
int p2p_get_dev_addr(struct p2p_data *p2p, const u8 *iface_addr,
u8 *dev_addr)
{
struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr);
if (dev == NULL)
return -1;
os_memcpy(dev_addr, dev->info.p2p_device_addr, ETH_ALEN);
return 0;
}
void p2p_set_peer_filter(struct p2p_data *p2p, const u8 *addr)
{
os_memcpy(p2p->peer_filter, addr, ETH_ALEN);
if (is_zero_ether_addr(p2p->peer_filter))
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Disable peer "
"filter");
else
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Enable peer "
"filter for " MACSTR, MAC2STR(p2p->peer_filter));
}
void p2p_set_cross_connect(struct p2p_data *p2p, int enabled)
{
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Cross connection %s",
enabled ? "enabled" : "disabled");
if (p2p->cross_connect == enabled)
return;
p2p->cross_connect = enabled;
/* TODO: may need to tear down any action group where we are GO(?) */
}
int p2p_get_oper_freq(struct p2p_data *p2p, const u8 *iface_addr)
{
struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr);
if (dev == NULL)
return -1;
if (dev->oper_freq <= 0)
return -1;
return dev->oper_freq;
}
void p2p_set_intra_bss_dist(struct p2p_data *p2p, int enabled)
{
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Intra BSS distribution %s",
enabled ? "enabled" : "disabled");
p2p->cfg->p2p_intra_bss = enabled;
}
void p2p_update_channel_list(struct p2p_data *p2p, struct p2p_channels *chan)
{
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Update channel list");
os_memcpy(&p2p->cfg->channels, chan, sizeof(struct p2p_channels));
}
int p2p_send_action(struct p2p_data *p2p, unsigned int freq, const u8 *dst,
const u8 *src, const u8 *bssid, const u8 *buf,
size_t len, unsigned int wait_time)
{
if (p2p->p2p_scan_running) {
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Delay Action "
"frame TX until p2p_scan completes");
if (p2p->after_scan_tx) {
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Dropped "
"previous pending Action frame TX");
os_free(p2p->after_scan_tx);
}
p2p->after_scan_tx = os_malloc(sizeof(*p2p->after_scan_tx) +
len);
if (p2p->after_scan_tx == NULL)
return -1;
p2p->after_scan_tx->freq = freq;
os_memcpy(p2p->after_scan_tx->dst, dst, ETH_ALEN);
os_memcpy(p2p->after_scan_tx->src, src, ETH_ALEN);
os_memcpy(p2p->after_scan_tx->bssid, bssid, ETH_ALEN);
p2p->after_scan_tx->len = len;
p2p->after_scan_tx->wait_time = wait_time;
os_memcpy(p2p->after_scan_tx + 1, buf, len);
return 0;
}
return p2p->cfg->send_action(p2p->cfg->cb_ctx, freq, dst, src, bssid,
buf, len, wait_time);
}
void p2p_set_best_channels(struct p2p_data *p2p, int freq_24, int freq_5,
int freq_overall)
{
wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Best channel: 2.4 GHz: %d,"
" 5 GHz: %d, overall: %d", freq_24, freq_5, freq_overall);
p2p->best_freq_24 = freq_24;
p2p->best_freq_5 = freq_5;
p2p->best_freq_overall = freq_overall;
}
const u8 * p2p_get_go_neg_peer(struct p2p_data *p2p)
{
if (p2p == NULL || p2p->go_neg_peer == NULL)
return NULL;
return p2p->go_neg_peer->info.p2p_device_addr;
}
const struct p2p_peer_info *
p2p_get_peer_found(struct p2p_data *p2p, const u8 *addr, int next)
{
struct p2p_device *dev;
if (addr) {
dev = p2p_get_device(p2p, addr);
if (!dev)
return NULL;
if (!next) {
if (dev->flags & P2P_DEV_PROBE_REQ_ONLY)
return NULL;
return &dev->info;
} else {
do {
dev = dl_list_first(&dev->list,
struct p2p_device,
list);
if (&dev->list == &p2p->devices)
return NULL;
} while (dev->flags & P2P_DEV_PROBE_REQ_ONLY);
}
} else {
dev = dl_list_first(&p2p->devices, struct p2p_device, list);
if (!dev)
return NULL;
while (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
dev = dl_list_first(&dev->list,
struct p2p_device,
list);
if (&dev->list == &p2p->devices)
return NULL;
}
}
return &dev->info;
}
#ifdef ANDROID_P2P
int p2p_search_in_progress(struct p2p_data *p2p)
{
if (p2p == NULL)
return 0;
return p2p->state == P2P_SEARCH;
}
#endif
int p2p_in_progress(struct p2p_data *p2p)
{
if (p2p == NULL)
return 0;
if (p2p->state == P2P_SEARCH || p2p->state == P2P_SEARCH_WHEN_READY ||
p2p->state == P2P_CONTINUE_SEARCH_WHEN_READY)
return 2;
return p2p->state != P2P_IDLE && p2p->state != P2P_PROVISIONING;
}
void p2p_set_config_timeout(struct p2p_data *p2p, u8 go_timeout,
u8 client_timeout)
{
if (p2p) {
p2p->go_timeout = go_timeout;
p2p->client_timeout = client_timeout;
}
}
void p2p_increase_search_delay(struct p2p_data *p2p, unsigned int delay)
{
if (p2p && p2p->search_delay < delay)
p2p->search_delay = delay;
}
#ifdef CONFIG_WIFI_DISPLAY
static void p2p_update_wfd_ie_groups(struct p2p_data *p2p)
{
size_t g;
struct p2p_group *group;
for (g = 0; g < p2p->num_groups; g++) {
group = p2p->groups[g];
p2p_group_update_ies(group);
}
}
int p2p_set_wfd_ie_beacon(struct p2p_data *p2p, struct wpabuf *ie)
{
wpabuf_free(p2p->wfd_ie_beacon);
p2p->wfd_ie_beacon = ie;
p2p_update_wfd_ie_groups(p2p);
return 0;
}
int p2p_set_wfd_ie_probe_req(struct p2p_data *p2p, struct wpabuf *ie)
{
wpabuf_free(p2p->wfd_ie_probe_req);
p2p->wfd_ie_probe_req = ie;
return 0;
}
int p2p_set_wfd_ie_probe_resp(struct p2p_data *p2p, struct wpabuf *ie)
{
wpabuf_free(p2p->wfd_ie_probe_resp);
p2p->wfd_ie_probe_resp = ie;
p2p_update_wfd_ie_groups(p2p);
return 0;
}
int p2p_set_wfd_ie_assoc_req(struct p2p_data *p2p, struct wpabuf *ie)
{
wpabuf_free(p2p->wfd_ie_assoc_req);
p2p->wfd_ie_assoc_req = ie;
return 0;
}
int p2p_set_wfd_ie_invitation(struct p2p_data *p2p, struct wpabuf *ie)
{
wpabuf_free(p2p->wfd_ie_invitation);
p2p->wfd_ie_invitation = ie;
return 0;
}
int p2p_set_wfd_ie_prov_disc_req(struct p2p_data *p2p, struct wpabuf *ie)
{
wpabuf_free(p2p->wfd_ie_prov_disc_req);
p2p->wfd_ie_prov_disc_req = ie;
return 0;
}
int p2p_set_wfd_ie_prov_disc_resp(struct p2p_data *p2p, struct wpabuf *ie)
{
wpabuf_free(p2p->wfd_ie_prov_disc_resp);
p2p->wfd_ie_prov_disc_resp = ie;
return 0;
}
int p2p_set_wfd_ie_go_neg(struct p2p_data *p2p, struct wpabuf *ie)
{
wpabuf_free(p2p->wfd_ie_go_neg);
p2p->wfd_ie_go_neg = ie;
return 0;
}
int p2p_set_wfd_dev_info(struct p2p_data *p2p, const struct wpabuf *elem)
{
wpabuf_free(p2p->wfd_dev_info);
if (elem) {
p2p->wfd_dev_info = wpabuf_dup(elem);
if (p2p->wfd_dev_info == NULL)
return -1;
} else
p2p->wfd_dev_info = NULL;
return 0;
}
int p2p_set_wfd_assoc_bssid(struct p2p_data *p2p, const struct wpabuf *elem)
{
wpabuf_free(p2p->wfd_assoc_bssid);
if (elem) {
p2p->wfd_assoc_bssid = wpabuf_dup(elem);
if (p2p->wfd_assoc_bssid == NULL)
return -1;
} else
p2p->wfd_assoc_bssid = NULL;
return 0;
}
int p2p_set_wfd_coupled_sink_info(struct p2p_data *p2p,
const struct wpabuf *elem)
{
wpabuf_free(p2p->wfd_coupled_sink_info);
if (elem) {
p2p->wfd_coupled_sink_info = wpabuf_dup(elem);
if (p2p->wfd_coupled_sink_info == NULL)
return -1;
} else
p2p->wfd_coupled_sink_info = NULL;
return 0;
}
#endif /* CONFIG_WIFI_DISPLAY */
|