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
|
/*-
* Copyright (c) 2001 Atsushi Onoe
* Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* Alternatively, this software may be distributed under the terms of the
* GNU General Public License ("GPL") version 2 as published by the Free
* Software Foundation.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include "opt_compat.h"
/*
* IEEE 802.11 ioctl support (FreeBSD-specific)
*/
#include "opt_inet.h"
#include "opt_ipx.h"
#include <sys/endian.h>
#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/priv.h>
#include <sys/socket.h>
#include <sys/sockio.h>
#include <sys/systm.h>
#include <net/if.h>
#include <net/if_dl.h>
#include <net/if_media.h>
#include <net/ethernet.h>
#ifdef INET
#include <netinet/in.h>
#include <netinet/if_ether.h>
#endif
#ifdef IPX
#include <netipx/ipx.h>
#include <netipx/ipx_if.h>
#endif
#include <net80211/ieee80211_var.h>
#include <net80211/ieee80211_ioctl.h>
#include <dev/wi/if_wavelan_ieee.h>
#define IS_UP(_ic) \
(((_ic)->ic_ifp->if_flags & IFF_UP) && \
((_ic)->ic_ifp->if_drv_flags & IFF_DRV_RUNNING))
#define IS_UP_AUTO(_ic) \
(IS_UP(_ic) && (_ic)->ic_roaming == IEEE80211_ROAMING_AUTO)
/*
* XXX
* Wireless LAN specific configuration interface, which is compatible
* with wicontrol(8).
*/
struct wi_read_ap_args {
int i; /* result count */
struct wi_apinfo *ap; /* current entry in result buffer */
caddr_t max; /* result buffer bound */
};
static void
wi_read_ap_result(void *arg, struct ieee80211_node *ni)
{
struct ieee80211com *ic = ni->ni_ic;
struct wi_read_ap_args *sa = arg;
struct wi_apinfo *ap = sa->ap;
struct ieee80211_rateset *rs;
int j;
if ((caddr_t)(ap + 1) > sa->max)
return;
memset(ap, 0, sizeof(struct wi_apinfo));
if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
IEEE80211_ADDR_COPY(ap->bssid, ni->ni_macaddr);
ap->namelen = ic->ic_des_esslen;
if (ic->ic_des_esslen)
memcpy(ap->name, ic->ic_des_essid,
ic->ic_des_esslen);
} else {
IEEE80211_ADDR_COPY(ap->bssid, ni->ni_bssid);
ap->namelen = ni->ni_esslen;
if (ni->ni_esslen)
memcpy(ap->name, ni->ni_essid,
ni->ni_esslen);
}
ap->channel = ieee80211_chan2ieee(ic, ni->ni_chan);
ap->signal = ic->ic_node_getrssi(ni);
ap->capinfo = ni->ni_capinfo;
ap->interval = ni->ni_intval;
rs = &ni->ni_rates;
for (j = 0; j < rs->rs_nrates; j++) {
if (rs->rs_rates[j] & IEEE80211_RATE_BASIC) {
ap->rate = (rs->rs_rates[j] &
IEEE80211_RATE_VAL) * 5; /* XXX */
}
}
sa->i++;
sa->ap++;
}
struct wi_read_prism2_args {
int i; /* result count */
struct wi_scan_res *res;/* current entry in result buffer */
caddr_t max; /* result buffer bound */
};
static void
wi_read_prism2_result(void *arg, struct ieee80211_node *ni)
{
struct ieee80211com *ic = ni->ni_ic;
struct wi_read_prism2_args *sa = arg;
struct wi_scan_res *res = sa->res;
if ((caddr_t)(res + 1) > sa->max)
return;
res->wi_chan = ieee80211_chan2ieee(ic, ni->ni_chan);
res->wi_noise = 0;
res->wi_signal = ic->ic_node_getrssi(ni);
IEEE80211_ADDR_COPY(res->wi_bssid, ni->ni_bssid);
res->wi_interval = ni->ni_intval;
res->wi_capinfo = ni->ni_capinfo;
res->wi_ssid_len = ni->ni_esslen;
memcpy(res->wi_ssid, ni->ni_essid, IEEE80211_NWID_LEN);
/* NB: assumes wi_srates holds <= ni->ni_rates */
memcpy(res->wi_srates, ni->ni_rates.rs_rates,
sizeof(res->wi_srates));
if (ni->ni_rates.rs_nrates < 10)
res->wi_srates[ni->ni_rates.rs_nrates] = 0;
res->wi_rate = ni->ni_rates.rs_rates[ni->ni_txrate];
res->wi_rsvd = 0;
sa->i++;
sa->res++;
}
struct wi_read_sigcache_args {
int i; /* result count */
struct wi_sigcache *wsc;/* current entry in result buffer */
caddr_t max; /* result buffer bound */
};
static void
wi_read_sigcache(void *arg, struct ieee80211_node *ni)
{
struct ieee80211com *ic = ni->ni_ic;
struct wi_read_sigcache_args *sa = arg;
struct wi_sigcache *wsc = sa->wsc;
if ((caddr_t)(wsc + 1) > sa->max)
return;
memset(wsc, 0, sizeof(struct wi_sigcache));
IEEE80211_ADDR_COPY(wsc->macsrc, ni->ni_macaddr);
wsc->signal = ic->ic_node_getrssi(ni);
sa->wsc++;
sa->i++;
}
int
ieee80211_cfgget(struct ieee80211com *ic, u_long cmd, caddr_t data)
{
struct ifnet *ifp = ic->ic_ifp;
int i, j, error;
struct ifreq *ifr = (struct ifreq *)data;
struct wi_req wreq;
struct wi_ltv_keys *keys;
error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
if (error)
return error;
wreq.wi_len = 0;
switch (wreq.wi_type) {
case WI_RID_SERIALNO:
/* nothing appropriate */
break;
case WI_RID_NODENAME:
strcpy((char *)&wreq.wi_val[1], hostname);
wreq.wi_val[0] = htole16(strlen(hostname));
wreq.wi_len = (1 + strlen(hostname) + 1) / 2;
break;
case WI_RID_CURRENT_SSID:
if (ic->ic_state != IEEE80211_S_RUN) {
wreq.wi_val[0] = 0;
wreq.wi_len = 1;
break;
}
wreq.wi_val[0] = htole16(ic->ic_bss->ni_esslen);
memcpy(&wreq.wi_val[1], ic->ic_bss->ni_essid,
ic->ic_bss->ni_esslen);
wreq.wi_len = (1 + ic->ic_bss->ni_esslen + 1) / 2;
break;
case WI_RID_OWN_SSID:
case WI_RID_DESIRED_SSID:
wreq.wi_val[0] = htole16(ic->ic_des_esslen);
memcpy(&wreq.wi_val[1], ic->ic_des_essid, ic->ic_des_esslen);
wreq.wi_len = (1 + ic->ic_des_esslen + 1) / 2;
break;
case WI_RID_CURRENT_BSSID:
if (ic->ic_state == IEEE80211_S_RUN)
IEEE80211_ADDR_COPY(wreq.wi_val, ic->ic_bss->ni_bssid);
else
memset(wreq.wi_val, 0, IEEE80211_ADDR_LEN);
wreq.wi_len = IEEE80211_ADDR_LEN / 2;
break;
case WI_RID_CHANNEL_LIST:
memset(wreq.wi_val, 0, sizeof(wreq.wi_val));
/*
* Since channel 0 is not available for DS, channel 1
* is assigned to LSB on WaveLAN.
*/
if (ic->ic_phytype == IEEE80211_T_DS)
i = 1;
else
i = 0;
for (j = 0; i <= IEEE80211_CHAN_MAX; i++, j++)
if (isset(ic->ic_chan_active, i)) {
setbit((u_int8_t *)wreq.wi_val, j);
wreq.wi_len = j / 16 + 1;
}
break;
case WI_RID_OWN_CHNL:
wreq.wi_val[0] = htole16(
ieee80211_chan2ieee(ic, ic->ic_ibss_chan));
wreq.wi_len = 1;
break;
case WI_RID_CURRENT_CHAN:
wreq.wi_val[0] = htole16(
ieee80211_chan2ieee(ic, ic->ic_curchan));
wreq.wi_len = 1;
break;
case WI_RID_COMMS_QUALITY:
wreq.wi_val[0] = 0; /* quality */
wreq.wi_val[1] = htole16(ic->ic_node_getrssi(ic->ic_bss));
wreq.wi_val[2] = 0; /* noise */
wreq.wi_len = 3;
break;
case WI_RID_PROMISC:
wreq.wi_val[0] = htole16((ifp->if_flags & IFF_PROMISC) ? 1 : 0);
wreq.wi_len = 1;
break;
case WI_RID_PORTTYPE:
wreq.wi_val[0] = htole16(ic->ic_opmode);
wreq.wi_len = 1;
break;
case WI_RID_MAC_NODE:
IEEE80211_ADDR_COPY(wreq.wi_val, ic->ic_myaddr);
wreq.wi_len = IEEE80211_ADDR_LEN / 2;
break;
case WI_RID_TX_RATE:
if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE)
wreq.wi_val[0] = 0; /* auto */
else
wreq.wi_val[0] = htole16(
(ic->ic_sup_rates[ic->ic_curmode].rs_rates[ic->ic_fixed_rate] &
IEEE80211_RATE_VAL) / 2);
wreq.wi_len = 1;
break;
case WI_RID_CUR_TX_RATE:
wreq.wi_val[0] = htole16(
(ic->ic_bss->ni_rates.rs_rates[ic->ic_bss->ni_txrate] &
IEEE80211_RATE_VAL) / 2);
wreq.wi_len = 1;
break;
case WI_RID_RTS_THRESH:
wreq.wi_val[0] = htole16(ic->ic_rtsthreshold);
wreq.wi_len = 1;
break;
case WI_RID_CREATE_IBSS:
wreq.wi_val[0] =
htole16((ic->ic_flags & IEEE80211_F_IBSSON) ? 1 : 0);
wreq.wi_len = 1;
break;
case WI_RID_MICROWAVE_OVEN:
wreq.wi_val[0] = 0; /* no ... not supported */
wreq.wi_len = 1;
break;
case WI_RID_ROAMING_MODE:
wreq.wi_val[0] = htole16(ic->ic_roaming); /* XXX map */
wreq.wi_len = 1;
break;
case WI_RID_SYSTEM_SCALE:
wreq.wi_val[0] = htole16(1); /* low density ... not supp */
wreq.wi_len = 1;
break;
case WI_RID_PM_ENABLED:
wreq.wi_val[0] =
htole16((ic->ic_flags & IEEE80211_F_PMGTON) ? 1 : 0);
wreq.wi_len = 1;
break;
case WI_RID_MAX_SLEEP:
wreq.wi_val[0] = htole16(ic->ic_lintval);
wreq.wi_len = 1;
break;
case WI_RID_CUR_BEACON_INT:
wreq.wi_val[0] = htole16(ic->ic_bss->ni_intval);
wreq.wi_len = 1;
break;
case WI_RID_WEP_AVAIL:
wreq.wi_val[0] = htole16(1); /* always available */
wreq.wi_len = 1;
break;
case WI_RID_CNFAUTHMODE:
wreq.wi_val[0] = htole16(1); /* TODO: open system only */
wreq.wi_len = 1;
break;
case WI_RID_ENCRYPTION:
wreq.wi_val[0] =
htole16((ic->ic_flags & IEEE80211_F_PRIVACY) ? 1 : 0);
wreq.wi_len = 1;
break;
case WI_RID_TX_CRYPT_KEY:
wreq.wi_val[0] = htole16(ic->ic_def_txkey);
wreq.wi_len = 1;
break;
case WI_RID_DEFLT_CRYPT_KEYS:
keys = (struct wi_ltv_keys *)&wreq;
/* do not show keys to non-root user */
error = priv_check(curthread, PRIV_NET80211_GETKEY);
if (error) {
memset(keys, 0, sizeof(*keys));
error = 0;
break;
}
for (i = 0; i < IEEE80211_WEP_NKID; i++) {
keys->wi_keys[i].wi_keylen =
htole16(ic->ic_nw_keys[i].wk_keylen);
memcpy(keys->wi_keys[i].wi_keydat,
ic->ic_nw_keys[i].wk_key,
ic->ic_nw_keys[i].wk_keylen);
}
wreq.wi_len = sizeof(*keys) / 2;
break;
case WI_RID_MAX_DATALEN:
wreq.wi_val[0] = htole16(ic->ic_fragthreshold);
wreq.wi_len = 1;
break;
case WI_RID_IFACE_STATS:
/* XXX: should be implemented in lower drivers */
break;
case WI_RID_READ_APS:
/*
* Don't return results until active scan completes.
*/
if ((ic->ic_flags & (IEEE80211_F_SCAN|IEEE80211_F_ASCAN)) == 0) {
struct wi_read_ap_args args;
args.i = 0;
args.ap = (void *)((char *)wreq.wi_val + sizeof(i));
args.max = (void *)(&wreq + 1);
ieee80211_iterate_nodes(&ic->ic_scan,
wi_read_ap_result, &args);
memcpy(wreq.wi_val, &args.i, sizeof(args.i));
wreq.wi_len = (sizeof(int) +
sizeof(struct wi_apinfo) * args.i) / 2;
} else
error = EINPROGRESS;
break;
case WI_RID_PRISM2:
/* NB: we lie so WI_RID_SCAN_RES can include rates */
wreq.wi_val[0] = 1;
wreq.wi_len = sizeof(u_int16_t) / 2;
break;
case WI_RID_SCAN_RES: /* compatibility interface */
if ((ic->ic_flags & (IEEE80211_F_SCAN|IEEE80211_F_ASCAN)) == 0) {
struct wi_read_prism2_args args;
struct wi_scan_p2_hdr *p2;
/* NB: use Prism2 format so we can include rate info */
p2 = (struct wi_scan_p2_hdr *)wreq.wi_val;
args.i = 0;
args.res = (void *)&p2[1];
args.max = (void *)(&wreq + 1);
ieee80211_iterate_nodes(&ic->ic_scan,
wi_read_prism2_result, &args);
p2->wi_rsvd = 0;
p2->wi_reason = args.i;
wreq.wi_len = (sizeof(*p2) +
sizeof(struct wi_scan_res) * args.i) / 2;
} else
error = EINPROGRESS;
break;
case WI_RID_READ_CACHE: {
struct wi_read_sigcache_args args;
args.i = 0;
args.wsc = (struct wi_sigcache *) wreq.wi_val;
args.max = (void *)(&wreq + 1);
ieee80211_iterate_nodes(&ic->ic_scan, wi_read_sigcache, &args);
wreq.wi_len = sizeof(struct wi_sigcache) * args.i / 2;
break;
}
default:
error = EINVAL;
break;
}
if (error == 0) {
wreq.wi_len++;
error = copyout(&wreq, ifr->ifr_data, sizeof(wreq));
}
return error;
}
static int
findrate(struct ieee80211com *ic, enum ieee80211_phymode mode, int rate)
{
#define IEEERATE(_ic,_m,_i) \
((_ic)->ic_sup_rates[_m].rs_rates[_i] & IEEE80211_RATE_VAL)
int i, nrates = ic->ic_sup_rates[mode].rs_nrates;
for (i = 0; i < nrates; i++)
if (IEEERATE(ic, mode, i) == rate)
return i;
return -1;
#undef IEEERATE
}
/*
* Prepare to do a user-initiated scan for AP's. If no
* current/default channel is setup or the current channel
* is invalid then pick the first available channel from
* the active list as the place to start the scan.
*/
static int
ieee80211_setupscan(struct ieee80211com *ic, const u_int8_t chanlist[])
{
/*
* XXX don't permit a scan to be started unless we
* know the device is ready. For the moment this means
* the device is marked up as this is the required to
* initialize the hardware. It would be better to permit
* scanning prior to being up but that'll require some
* changes to the infrastructure.
*/
if (!IS_UP(ic))
return EINVAL;
memcpy(ic->ic_chan_active, chanlist, sizeof(ic->ic_chan_active));
/*
* We force the state to INIT before calling ieee80211_new_state
* to get ieee80211_begin_scan called. We really want to scan w/o
* altering the current state but that's not possible right now.
*/
/* XXX handle proberequest case */
ic->ic_state = IEEE80211_S_INIT; /* XXX bypass state machine */
return 0;
}
int
ieee80211_cfgset(struct ieee80211com *ic, u_long cmd, caddr_t data)
{
struct ifnet *ifp = ic->ic_ifp;
int i, j, len, error, rate;
struct ifreq *ifr = (struct ifreq *)data;
struct wi_ltv_keys *keys;
struct wi_req wreq;
u_char chanlist[roundup(IEEE80211_CHAN_MAX, NBBY)];
error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
if (error)
return error;
len = wreq.wi_len ? (wreq.wi_len - 1) * 2 : 0;
switch (wreq.wi_type) {
case WI_RID_SERIALNO:
case WI_RID_NODENAME:
return EPERM;
case WI_RID_CURRENT_SSID:
return EPERM;
case WI_RID_OWN_SSID:
case WI_RID_DESIRED_SSID:
if (le16toh(wreq.wi_val[0]) * 2 > len ||
le16toh(wreq.wi_val[0]) > IEEE80211_NWID_LEN) {
error = ENOSPC;
break;
}
memset(ic->ic_des_essid, 0, sizeof(ic->ic_des_essid));
ic->ic_des_esslen = le16toh(wreq.wi_val[0]) * 2;
memcpy(ic->ic_des_essid, &wreq.wi_val[1], ic->ic_des_esslen);
error = ENETRESET;
break;
case WI_RID_CURRENT_BSSID:
return EPERM;
case WI_RID_OWN_CHNL:
if (len != 2)
return EINVAL;
i = le16toh(wreq.wi_val[0]);
if (i < 0 ||
i > IEEE80211_CHAN_MAX ||
isclr(ic->ic_chan_active, i))
return EINVAL;
ic->ic_ibss_chan = &ic->ic_channels[i];
if (ic->ic_opmode == IEEE80211_M_MONITOR)
error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
else
error = ENETRESET;
break;
case WI_RID_CURRENT_CHAN:
return EPERM;
case WI_RID_COMMS_QUALITY:
return EPERM;
case WI_RID_PROMISC:
if (len != 2)
return EINVAL;
if (ifp->if_flags & IFF_PROMISC) {
if (wreq.wi_val[0] == 0) {
ifp->if_flags &= ~IFF_PROMISC;
error = ENETRESET;
}
} else {
if (wreq.wi_val[0] != 0) {
ifp->if_flags |= IFF_PROMISC;
error = ENETRESET;
}
}
break;
case WI_RID_PORTTYPE:
if (len != 2)
return EINVAL;
switch (le16toh(wreq.wi_val[0])) {
case IEEE80211_M_STA:
break;
case IEEE80211_M_IBSS:
if (!(ic->ic_caps & IEEE80211_C_IBSS))
return EINVAL;
break;
case IEEE80211_M_AHDEMO:
if (ic->ic_phytype != IEEE80211_T_DS ||
!(ic->ic_caps & IEEE80211_C_AHDEMO))
return EINVAL;
break;
case IEEE80211_M_HOSTAP:
if (!(ic->ic_caps & IEEE80211_C_HOSTAP))
return EINVAL;
break;
default:
return EINVAL;
}
if (le16toh(wreq.wi_val[0]) != ic->ic_opmode) {
ic->ic_opmode = le16toh(wreq.wi_val[0]);
error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
}
break;
#if 0
case WI_RID_MAC_NODE:
if (len != IEEE80211_ADDR_LEN)
return EINVAL;
IEEE80211_ADDR_COPY(LLADDR(ifp->if_sadl), wreq.wi_val);
/* if_init will copy lladdr into ic_myaddr */
error = ENETRESET;
break;
#endif
case WI_RID_TX_RATE:
if (len != 2)
return EINVAL;
if (wreq.wi_val[0] == 0) {
/* auto */
ic->ic_fixed_rate = IEEE80211_FIXED_RATE_NONE;
break;
}
rate = 2 * le16toh(wreq.wi_val[0]);
if (ic->ic_curmode == IEEE80211_MODE_AUTO) {
/*
* In autoselect mode search for the rate. We take
* the first instance which may not be right, but we
* are limited by the interface. Note that we also
* lock the mode to insure the rate is meaningful
* when it is used.
*/
for (j = IEEE80211_MODE_11A;
j < IEEE80211_MODE_MAX; j++) {
if ((ic->ic_modecaps & (1<<j)) == 0)
continue;
i = findrate(ic, j, rate);
if (i != -1) {
/* lock mode too */
ic->ic_curmode = j;
goto setrate;
}
}
} else {
i = findrate(ic, ic->ic_curmode, rate);
if (i != -1)
goto setrate;
}
return EINVAL;
setrate:
ic->ic_fixed_rate = i;
error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
break;
case WI_RID_CUR_TX_RATE:
return EPERM;
case WI_RID_RTS_THRESH:
if (len != 2)
return EINVAL;
if (le16toh(wreq.wi_val[0]) != IEEE80211_MAX_LEN)
return EINVAL; /* TODO: RTS */
break;
case WI_RID_CREATE_IBSS:
if (len != 2)
return EINVAL;
if (wreq.wi_val[0] != 0) {
if ((ic->ic_caps & IEEE80211_C_IBSS) == 0)
return EINVAL;
if ((ic->ic_flags & IEEE80211_F_IBSSON) == 0) {
ic->ic_flags |= IEEE80211_F_IBSSON;
if (ic->ic_opmode == IEEE80211_M_IBSS &&
ic->ic_state == IEEE80211_S_SCAN)
error = IS_UP_AUTO(ic) ? ENETRESET : 0;
}
} else {
if (ic->ic_flags & IEEE80211_F_IBSSON) {
ic->ic_flags &= ~IEEE80211_F_IBSSON;
if (ic->ic_flags & IEEE80211_F_SIBSS) {
ic->ic_flags &= ~IEEE80211_F_SIBSS;
error = IS_UP_AUTO(ic) ? ENETRESET : 0;
}
}
}
break;
case WI_RID_MICROWAVE_OVEN:
if (len != 2)
return EINVAL;
if (wreq.wi_val[0] != 0)
return EINVAL; /* not supported */
break;
case WI_RID_ROAMING_MODE:
if (len != 2)
return EINVAL;
i = le16toh(wreq.wi_val[0]);
if (i > IEEE80211_ROAMING_MANUAL)
return EINVAL; /* not supported */
ic->ic_roaming = i;
break;
case WI_RID_SYSTEM_SCALE:
if (len != 2)
return EINVAL;
if (le16toh(wreq.wi_val[0]) != 1)
return EINVAL; /* not supported */
break;
case WI_RID_PM_ENABLED:
if (len != 2)
return EINVAL;
if (wreq.wi_val[0] != 0) {
if ((ic->ic_caps & IEEE80211_C_PMGT) == 0)
return EINVAL;
if ((ic->ic_flags & IEEE80211_F_PMGTON) == 0) {
ic->ic_flags |= IEEE80211_F_PMGTON;
error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
}
} else {
if (ic->ic_flags & IEEE80211_F_PMGTON) {
ic->ic_flags &= ~IEEE80211_F_PMGTON;
error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
}
}
break;
case WI_RID_MAX_SLEEP:
if (len != 2)
return EINVAL;
ic->ic_lintval = le16toh(wreq.wi_val[0]);
if (ic->ic_flags & IEEE80211_F_PMGTON)
error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
break;
case WI_RID_CUR_BEACON_INT:
return EPERM;
case WI_RID_WEP_AVAIL:
return EPERM;
case WI_RID_CNFAUTHMODE:
if (len != 2)
return EINVAL;
i = le16toh(wreq.wi_val[0]);
if (i > IEEE80211_AUTH_WPA)
return EINVAL;
ic->ic_bss->ni_authmode = i; /* XXX ENETRESET? */
error = ENETRESET;
break;
case WI_RID_ENCRYPTION:
if (len != 2)
return EINVAL;
if (wreq.wi_val[0] != 0) {
if ((ic->ic_caps & IEEE80211_C_WEP) == 0)
return EINVAL;
if ((ic->ic_flags & IEEE80211_F_PRIVACY) == 0) {
ic->ic_flags |= IEEE80211_F_PRIVACY;
error = ENETRESET;
}
} else {
if (ic->ic_flags & IEEE80211_F_PRIVACY) {
ic->ic_flags &= ~IEEE80211_F_PRIVACY;
error = ENETRESET;
}
}
break;
case WI_RID_TX_CRYPT_KEY:
if (len != 2)
return EINVAL;
i = le16toh(wreq.wi_val[0]);
if (i >= IEEE80211_WEP_NKID)
return EINVAL;
ic->ic_def_txkey = i;
error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
break;
case WI_RID_DEFLT_CRYPT_KEYS:
if (len != sizeof(struct wi_ltv_keys))
return EINVAL;
keys = (struct wi_ltv_keys *)&wreq;
for (i = 0; i < IEEE80211_WEP_NKID; i++) {
len = le16toh(keys->wi_keys[i].wi_keylen);
if (len != 0 && len < IEEE80211_WEP_KEYLEN)
return EINVAL;
if (len > IEEE80211_KEYBUF_SIZE)
return EINVAL;
}
for (i = 0; i < IEEE80211_WEP_NKID; i++) {
struct ieee80211_key *k = &ic->ic_nw_keys[i];
len = le16toh(keys->wi_keys[i].wi_keylen);
k->wk_keylen = len;
k->wk_flags = IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV;
memset(k->wk_key, 0, sizeof(k->wk_key));
memcpy(k->wk_key, keys->wi_keys[i].wi_keydat, len);
#if 0
k->wk_type = IEEE80211_CIPHER_WEP;
#endif
}
error = ENETRESET;
break;
case WI_RID_MAX_DATALEN:
if (len != 2)
return EINVAL;
len = le16toh(wreq.wi_val[0]);
if (len < 350 /* ? */ || len > IEEE80211_MAX_LEN)
return EINVAL;
ic->ic_fragthreshold = len;
error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
break;
case WI_RID_IFACE_STATS:
error = EPERM;
break;
case WI_RID_SCAN_REQ: /* XXX wicontrol */
if (ic->ic_opmode == IEEE80211_M_HOSTAP)
break;
error = ieee80211_setupscan(ic, ic->ic_chan_avail);
if (error == 0)
error = ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
break;
case WI_RID_SCAN_APS:
if (ic->ic_opmode == IEEE80211_M_HOSTAP)
break;
len--; /* XXX: tx rate? */
/* FALLTHRU */
case WI_RID_CHANNEL_LIST:
memset(chanlist, 0, sizeof(chanlist));
/*
* Since channel 0 is not available for DS, channel 1
* is assigned to LSB on WaveLAN.
*/
if (ic->ic_phytype == IEEE80211_T_DS)
i = 1;
else
i = 0;
for (j = 0; i <= IEEE80211_CHAN_MAX; i++, j++) {
if ((j / 8) >= len)
break;
if (isclr((u_int8_t *)wreq.wi_val, j))
continue;
if (isclr(ic->ic_chan_active, i)) {
if (wreq.wi_type != WI_RID_CHANNEL_LIST)
continue;
if (isclr(ic->ic_chan_avail, i))
return EPERM;
}
setbit(chanlist, i);
}
error = ieee80211_setupscan(ic, chanlist);
if (wreq.wi_type == WI_RID_CHANNEL_LIST) {
/* NB: ignore error from ieee80211_setupscan */
error = ENETRESET;
} else if (error == 0)
error = ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
break;
default:
error = EINVAL;
break;
}
if (error == ENETRESET && !IS_UP_AUTO(ic))
error = 0;
return error;
}
static int
cap2cipher(int flag)
{
switch (flag) {
case IEEE80211_C_WEP: return IEEE80211_CIPHER_WEP;
case IEEE80211_C_AES: return IEEE80211_CIPHER_AES_OCB;
case IEEE80211_C_AES_CCM: return IEEE80211_CIPHER_AES_CCM;
case IEEE80211_C_CKIP: return IEEE80211_CIPHER_CKIP;
case IEEE80211_C_TKIP: return IEEE80211_CIPHER_TKIP;
}
return -1;
}
static int
ieee80211_ioctl_getkey(struct ieee80211com *ic, struct ieee80211req *ireq)
{
struct ieee80211_node *ni;
struct ieee80211req_key ik;
struct ieee80211_key *wk;
const struct ieee80211_cipher *cip;
u_int kid;
int error;
if (ireq->i_len != sizeof(ik))
return EINVAL;
error = copyin(ireq->i_data, &ik, sizeof(ik));
if (error)
return error;
kid = ik.ik_keyix;
if (kid == IEEE80211_KEYIX_NONE) {
ni = ieee80211_find_node(&ic->ic_sta, ik.ik_macaddr);
if (ni == NULL)
return EINVAL; /* XXX */
wk = &ni->ni_ucastkey;
} else {
if (kid >= IEEE80211_WEP_NKID)
return EINVAL;
wk = &ic->ic_nw_keys[kid];
IEEE80211_ADDR_COPY(&ik.ik_macaddr, ic->ic_bss->ni_macaddr);
ni = NULL;
}
cip = wk->wk_cipher;
ik.ik_type = cip->ic_cipher;
ik.ik_keylen = wk->wk_keylen;
ik.ik_flags = wk->wk_flags & (IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV);
if (wk->wk_keyix == ic->ic_def_txkey)
ik.ik_flags |= IEEE80211_KEY_DEFAULT;
if (priv_check(curthread, PRIV_NET80211_GETKEY) == 0) {
/* NB: only root can read key data */
ik.ik_keyrsc = wk->wk_keyrsc;
ik.ik_keytsc = wk->wk_keytsc;
memcpy(ik.ik_keydata, wk->wk_key, wk->wk_keylen);
if (cip->ic_cipher == IEEE80211_CIPHER_TKIP) {
memcpy(ik.ik_keydata+wk->wk_keylen,
wk->wk_key + IEEE80211_KEYBUF_SIZE,
IEEE80211_MICBUF_SIZE);
ik.ik_keylen += IEEE80211_MICBUF_SIZE;
}
} else {
ik.ik_keyrsc = 0;
ik.ik_keytsc = 0;
memset(ik.ik_keydata, 0, sizeof(ik.ik_keydata));
}
if (ni != NULL)
ieee80211_free_node(ni);
return copyout(&ik, ireq->i_data, sizeof(ik));
}
static int
ieee80211_ioctl_getchanlist(struct ieee80211com *ic, struct ieee80211req *ireq)
{
if (sizeof(ic->ic_chan_active) < ireq->i_len)
ireq->i_len = sizeof(ic->ic_chan_active);
return copyout(&ic->ic_chan_active, ireq->i_data, ireq->i_len);
}
static int
ieee80211_ioctl_getchaninfo(struct ieee80211com *ic, struct ieee80211req *ireq)
{
struct ieee80211req_chaninfo chans; /* XXX off stack? */
int i, space;
/*
* Since channel 0 is not available for DS, channel 1
* is assigned to LSB on WaveLAN.
*/
if (ic->ic_phytype == IEEE80211_T_DS)
i = 1;
else
i = 0;
memset(&chans, 0, sizeof(chans));
for (; i <= IEEE80211_CHAN_MAX; i++)
if (isset(ic->ic_chan_avail, i)) {
struct ieee80211_channel *c = &ic->ic_channels[i];
chans.ic_chans[chans.ic_nchans].ic_freq = c->ic_freq;
chans.ic_chans[chans.ic_nchans].ic_flags = c->ic_flags;
chans.ic_nchans++;
}
space = __offsetof(struct ieee80211req_chaninfo,
ic_chans[chans.ic_nchans]);
if (space > ireq->i_len)
space = ireq->i_len;
return copyout(&chans, ireq->i_data, space);
}
static int
ieee80211_ioctl_getwpaie(struct ieee80211com *ic, struct ieee80211req *ireq)
{
struct ieee80211_node *ni;
struct ieee80211req_wpaie wpaie;
int error;
if (ireq->i_len < IEEE80211_ADDR_LEN)
return EINVAL;
error = copyin(ireq->i_data, wpaie.wpa_macaddr, IEEE80211_ADDR_LEN);
if (error != 0)
return error;
ni = ieee80211_find_node(&ic->ic_sta, wpaie.wpa_macaddr);
if (ni == NULL)
return EINVAL; /* XXX */
memset(wpaie.wpa_ie, 0, sizeof(wpaie.wpa_ie));
if (ni->ni_wpa_ie != NULL) {
int ielen = ni->ni_wpa_ie[1] + 2;
if (ielen > sizeof(wpaie.wpa_ie))
ielen = sizeof(wpaie.wpa_ie);
memcpy(wpaie.wpa_ie, ni->ni_wpa_ie, ielen);
}
ieee80211_free_node(ni);
if (ireq->i_len > sizeof(wpaie))
ireq->i_len = sizeof(wpaie);
return copyout(&wpaie, ireq->i_data, ireq->i_len);
}
static int
ieee80211_ioctl_getstastats(struct ieee80211com *ic, struct ieee80211req *ireq)
{
struct ieee80211_node *ni;
u_int8_t macaddr[IEEE80211_ADDR_LEN];
const int off = __offsetof(struct ieee80211req_sta_stats, is_stats);
int error;
if (ireq->i_len < off)
return EINVAL;
error = copyin(ireq->i_data, macaddr, IEEE80211_ADDR_LEN);
if (error != 0)
return error;
ni = ieee80211_find_node(&ic->ic_sta, macaddr);
if (ni == NULL) {
/* XXX special-case sta-mode until bss is node in ic_sta */
if (ic->ic_opmode != IEEE80211_M_STA)
return ENOENT;
ni = ieee80211_ref_node(ic->ic_bss);
}
if (ireq->i_len > sizeof(struct ieee80211req_sta_stats))
ireq->i_len = sizeof(struct ieee80211req_sta_stats);
/* NB: copy out only the statistics */
error = copyout(&ni->ni_stats, (u_int8_t *) ireq->i_data + off,
ireq->i_len - off);
ieee80211_free_node(ni);
return error;
}
#ifdef COMPAT_FREEBSD6
#define IEEE80211_IOC_SCAN_RESULTS_OLD 24
struct scan_result_old {
u_int16_t isr_len; /* length (mult of 4) */
u_int16_t isr_freq; /* MHz */
u_int16_t isr_flags; /* channel flags */
u_int8_t isr_noise;
u_int8_t isr_rssi;
u_int8_t isr_intval; /* beacon interval */
u_int8_t isr_capinfo; /* capabilities */
u_int8_t isr_erp; /* ERP element */
u_int8_t isr_bssid[IEEE80211_ADDR_LEN];
u_int8_t isr_nrates;
u_int8_t isr_rates[IEEE80211_RATE_MAXSIZE];
u_int8_t isr_ssid_len; /* SSID length */
u_int8_t isr_ie_len; /* IE length */
u_int8_t isr_pad[5];
/* variable length SSID followed by IE data */
};
static void
old_get_scan_result(struct scan_result_old *sr,
const struct ieee80211_node *ni)
{
struct ieee80211com *ic = ni->ni_ic;
u_int ielen;
memset(sr, 0, sizeof(*sr));
sr->isr_ssid_len = ni->ni_esslen;
ielen = 0;
if (ni->ni_wpa_ie != NULL)
ielen += 2+ni->ni_wpa_ie[1];
if (ni->ni_wme_ie != NULL)
ielen += 2+ni->ni_wme_ie[1];
/* NB: beware of overflow, isr_ie_len is 8 bits */
sr->isr_ie_len = (ielen > 255 ? 0 : ielen);
sr->isr_len = sizeof(*sr) + sr->isr_ssid_len + sr->isr_ie_len;
sr->isr_len = roundup(sr->isr_len, sizeof(u_int32_t));
if (ni->ni_chan != IEEE80211_CHAN_ANYC) {
sr->isr_freq = ni->ni_chan->ic_freq;
sr->isr_flags = ni->ni_chan->ic_flags;
}
sr->isr_rssi = ic->ic_node_getrssi(ni);
sr->isr_intval = ni->ni_intval;
sr->isr_capinfo = ni->ni_capinfo;
sr->isr_erp = ni->ni_erp;
IEEE80211_ADDR_COPY(sr->isr_bssid, ni->ni_bssid);
sr->isr_nrates = ni->ni_rates.rs_nrates;
if (sr->isr_nrates > 15)
sr->isr_nrates = 15;
memcpy(sr->isr_rates, ni->ni_rates.rs_rates, sr->isr_nrates);
}
static int
old_getscanresults(struct ieee80211com *ic, struct ieee80211req *ireq)
{
union {
struct scan_result_old res;
char data[512]; /* XXX shrink? */
} u;
struct scan_result_old *sr = &u.res;
struct ieee80211_node_table *nt;
struct ieee80211_node *ni;
int error, space;
u_int8_t *p, *cp;
p = ireq->i_data;
space = ireq->i_len;
error = 0;
/* XXX locking */
nt = &ic->ic_scan;
TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
/* NB: skip pre-scan node state */
if (ni->ni_chan == IEEE80211_CHAN_ANYC)
continue;
old_get_scan_result(sr, ni);
if (sr->isr_len > sizeof(u))
continue; /* XXX */
if (space < sr->isr_len)
break;
cp = (u_int8_t *)(sr+1);
memcpy(cp, ni->ni_essid, ni->ni_esslen);
cp += ni->ni_esslen;
if (sr->isr_ie_len) {
if (ni->ni_wpa_ie != NULL) {
memcpy(cp, ni->ni_wpa_ie, 2+ni->ni_wpa_ie[1]);
cp += 2+ni->ni_wpa_ie[1];
}
if (ni->ni_wme_ie != NULL) {
memcpy(cp, ni->ni_wme_ie, 2+ni->ni_wme_ie[1]);
cp += 2+ni->ni_wme_ie[1];
}
}
error = copyout(sr, p, sr->isr_len);
if (error)
break;
p += sr->isr_len;
space -= sr->isr_len;
}
ireq->i_len -= space;
return error;
}
#endif /* COMPAT_FREEBSD6 */
struct scanresultsreq {
struct ieee80211req_scan_result *sr;
size_t space;
};
static size_t
scan_space(const struct ieee80211_node *ni, size_t *ielen)
{
size_t len;
*ielen = 0;
if (ni->ni_wpa_ie != NULL)
*ielen += 2+ni->ni_wpa_ie[1];
if (ni->ni_wme_ie != NULL)
*ielen += 2+ni->ni_wme_ie[1];
/*
* NB: ie's can be no more than 255 bytes and the max 802.11
* packet is <3Kbytes so we are sure this doesn't overflow
* 16-bits; if this is a concern we can drop the ie's.
*/
len = sizeof(struct ieee80211req_scan_result) + ni->ni_esslen + *ielen;
return roundup(len, sizeof(u_int32_t));
}
static void
get_scan_space(void *arg, struct ieee80211_node *ni)
{
struct scanresultsreq *req = arg;
size_t ielen;
req->space += scan_space(ni, &ielen);
}
static void
get_scan_result(void *arg, struct ieee80211_node *ni)
{
struct scanresultsreq *req = arg;
struct ieee80211com *ic = ni->ni_ic;
struct ieee80211req_scan_result *sr;
size_t ielen, len;
u_int8_t *cp;
len = scan_space(ni, &ielen);
if (len > req->space)
return;
sr = req->sr;
KASSERT(len <= 65535 && ielen <= 65535,
("len %zu ssid %u ie %zu", len, ni->ni_esslen, ielen));
sr->isr_len = len;
sr->isr_ssid_len = ni->ni_esslen;
sr->isr_ie_len = ielen;
if (ni->ni_chan != IEEE80211_CHAN_ANYC) {
sr->isr_freq = ni->ni_chan->ic_freq;
sr->isr_flags = ni->ni_chan->ic_flags;
}
/* XXX need to rev driver apis for signal data */
sr->isr_rssi = (int8_t) ic->ic_node_getrssi(ni);
sr->isr_intval = ni->ni_intval;
sr->isr_capinfo = ni->ni_capinfo;
sr->isr_erp = ni->ni_erp;
IEEE80211_ADDR_COPY(sr->isr_bssid, ni->ni_bssid);
sr->isr_nrates = ni->ni_rates.rs_nrates;
if (sr->isr_nrates > 15)
sr->isr_nrates = 15;
memcpy(sr->isr_rates, ni->ni_rates.rs_rates, sr->isr_nrates);
cp = (u_int8_t *)(sr+1);
memcpy(cp, ni->ni_essid, ni->ni_esslen);
cp += ni->ni_esslen;
if (sr->isr_ie_len) {
if (ni->ni_wpa_ie != NULL) {
memcpy(cp, ni->ni_wpa_ie, 2+ni->ni_wpa_ie[1]);
cp += 2+ni->ni_wpa_ie[1];
}
if (ni->ni_wme_ie != NULL) {
memcpy(cp, ni->ni_wme_ie, 2+ni->ni_wme_ie[1]);
cp += 2+ni->ni_wme_ie[1];
}
}
req->sr = (struct ieee80211req_scan_result *)(((u_int8_t *)sr) + len);
req->space -= len;
}
static int
ieee80211_ioctl_getscanresults(struct ieee80211com *ic, struct ieee80211req *ireq)
{
struct scanresultsreq req;
int error;
if (ireq->i_len < sizeof(struct scanresultsreq))
return EFAULT;
error = 0;
req.space = 0;
ieee80211_iterate_nodes(&ic->ic_scan, get_scan_space, &req);
if (req.space > ireq->i_len)
req.space = ireq->i_len;
if (req.space > 0) {
size_t space;
void *p;
space = req.space;
/* XXX M_WAITOK after driver lock released */
MALLOC(p, void *, space, M_TEMP, M_NOWAIT | M_ZERO);
if (p == NULL)
return ENOMEM;
req.sr = p;
ieee80211_iterate_nodes(&ic->ic_scan, get_scan_result, &req);
ireq->i_len = space - req.space;
error = copyout(p, ireq->i_data, ireq->i_len);
FREE(p, M_TEMP);
} else
ireq->i_len = 0;
return error;
}
struct stainforeq {
struct ieee80211com *ic;
struct ieee80211req_sta_info *si;
size_t space;
};
static size_t
sta_space(const struct ieee80211_node *ni, size_t *ielen)
{
*ielen = 0;
if (ni->ni_wpa_ie != NULL)
*ielen += 2+ni->ni_wpa_ie[1];
if (ni->ni_wme_ie != NULL)
*ielen += 2+ni->ni_wme_ie[1];
return roundup(sizeof(struct ieee80211req_sta_info) + *ielen,
sizeof(u_int32_t));
}
static void
get_sta_space(void *arg, struct ieee80211_node *ni)
{
struct stainforeq *req = arg;
struct ieee80211com *ic = ni->ni_ic;
size_t ielen;
if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
ni->ni_associd == 0) /* only associated stations */
return;
req->space += sta_space(ni, &ielen);
}
static void
get_sta_info(void *arg, struct ieee80211_node *ni)
{
struct stainforeq *req = arg;
struct ieee80211com *ic = ni->ni_ic;
struct ieee80211req_sta_info *si;
size_t ielen, len;
u_int8_t *cp;
if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
ni->ni_associd == 0) /* only associated stations */
return;
if (ni->ni_chan == IEEE80211_CHAN_ANYC) /* XXX bogus entry */
return;
len = sta_space(ni, &ielen);
if (len > req->space)
return;
si = req->si;
KASSERT(len <= 65535 && ielen <= 65535, ("len %zu ie %zu", len, ielen));
si->isi_len = len;
si->isi_ie_len = ielen;
si->isi_freq = ni->ni_chan->ic_freq;
si->isi_flags = ni->ni_chan->ic_flags;
si->isi_state = ni->ni_flags;
si->isi_authmode = ni->ni_authmode;
si->isi_rssi = ic->ic_node_getrssi(ni);
si->isi_noise = 0; /* XXX */
si->isi_capinfo = ni->ni_capinfo;
si->isi_erp = ni->ni_erp;
IEEE80211_ADDR_COPY(si->isi_macaddr, ni->ni_macaddr);
si->isi_nrates = ni->ni_rates.rs_nrates;
if (si->isi_nrates > 15)
si->isi_nrates = 15;
memcpy(si->isi_rates, ni->ni_rates.rs_rates, si->isi_nrates);
si->isi_txrate = ni->ni_txrate;
si->isi_associd = ni->ni_associd;
si->isi_txpower = ni->ni_txpower;
si->isi_vlan = ni->ni_vlan;
if (ni->ni_flags & IEEE80211_NODE_QOS) {
memcpy(si->isi_txseqs, ni->ni_txseqs, sizeof(ni->ni_txseqs));
memcpy(si->isi_rxseqs, ni->ni_rxseqs, sizeof(ni->ni_rxseqs));
} else {
si->isi_txseqs[0] = ni->ni_txseqs[0];
si->isi_rxseqs[0] = ni->ni_rxseqs[0];
}
/* NB: leave all cases in case we relax ni_associd == 0 check */
if (ieee80211_node_is_authorized(ni))
si->isi_inact = ic->ic_inact_run;
else if (ni->ni_associd != 0)
si->isi_inact = ic->ic_inact_auth;
else
si->isi_inact = ic->ic_inact_init;
si->isi_inact = (si->isi_inact - ni->ni_inact) * IEEE80211_INACT_WAIT;
cp = (u_int8_t *)(si+1);
if (ni->ni_wpa_ie != NULL) {
memcpy(cp, ni->ni_wpa_ie, 2+ni->ni_wpa_ie[1]);
cp += 2+ni->ni_wpa_ie[1];
}
if (ni->ni_wme_ie != NULL) {
memcpy(cp, ni->ni_wme_ie, 2+ni->ni_wme_ie[1]);
cp += 2+ni->ni_wme_ie[1];
}
req->si = (struct ieee80211req_sta_info *)(((u_int8_t *)si) + len);
req->space -= len;
}
static int
getstainfo_common(struct ieee80211com *ic, struct ieee80211req *ireq,
struct ieee80211_node *ni, int off)
{
struct stainforeq req;
size_t space;
void *p;
int error;
error = 0;
req.space = 0;
if (ni == NULL)
ieee80211_iterate_nodes(&ic->ic_sta, get_sta_space, &req);
else
get_sta_space(&req, ni);
if (req.space > ireq->i_len)
req.space = ireq->i_len;
if (req.space > 0) {
space = req.space;
/* XXX M_WAITOK after driver lock released */
MALLOC(p, void *, space, M_TEMP, M_NOWAIT);
if (p == NULL) {
error = ENOMEM;
goto bad;
}
req.si = p;
if (ni == NULL)
ieee80211_iterate_nodes(&ic->ic_sta, get_sta_info, &req);
else
get_sta_info(&req, ni);
ireq->i_len = space - req.space;
error = copyout(p, (u_int8_t *) ireq->i_data+off, ireq->i_len);
FREE(p, M_TEMP);
} else
ireq->i_len = 0;
bad:
if (ni != NULL)
ieee80211_free_node(ni);
return error;
}
static int
ieee80211_ioctl_getstainfo(struct ieee80211com *ic, struct ieee80211req *ireq)
{
u_int8_t macaddr[IEEE80211_ADDR_LEN];
const int off = __offsetof(struct ieee80211req_sta_req, info);
struct ieee80211_node *ni;
int error;
if (ireq->i_len < sizeof(struct ieee80211req_sta_req))
return EFAULT;
error = copyin(ireq->i_data, macaddr, IEEE80211_ADDR_LEN);
if (error != 0)
return error;
if (IEEE80211_ADDR_EQ(macaddr, ic->ic_ifp->if_broadcastaddr)) {
ni = NULL;
} else {
ni = ieee80211_find_node(&ic->ic_sta, macaddr);
if (ni == NULL) {
/* XXX special-case sta-mode until bss is in ic_sta */
if (ic->ic_opmode != IEEE80211_M_STA)
return EINVAL; /* XXX */
ni = ieee80211_ref_node(ic->ic_bss);
}
}
return getstainfo_common(ic, ireq, ni, off);
}
#ifdef COMPAT_FREEBSD6
#define IEEE80211_IOC_STA_INFO_OLD 45
static int
old_getstainfo(struct ieee80211com *ic, struct ieee80211req *ireq)
{
if (ireq->i_len < sizeof(struct ieee80211req_sta_info))
return EFAULT;
return getstainfo_common(ic, ireq, NULL, 0);
}
#endif /* COMPAT_FREEBSD6 */
static int
ieee80211_ioctl_getstatxpow(struct ieee80211com *ic, struct ieee80211req *ireq)
{
struct ieee80211_node *ni;
struct ieee80211req_sta_txpow txpow;
int error;
if (ireq->i_len != sizeof(txpow))
return EINVAL;
error = copyin(ireq->i_data, &txpow, sizeof(txpow));
if (error != 0)
return error;
ni = ieee80211_find_node(&ic->ic_sta, txpow.it_macaddr);
if (ni == NULL)
return EINVAL; /* XXX */
txpow.it_txpow = ni->ni_txpower;
error = copyout(&txpow, ireq->i_data, sizeof(txpow));
ieee80211_free_node(ni);
return error;
}
static int
ieee80211_ioctl_getwmeparam(struct ieee80211com *ic, struct ieee80211req *ireq)
{
struct ieee80211_wme_state *wme = &ic->ic_wme;
struct wmeParams *wmep;
int ac;
if ((ic->ic_caps & IEEE80211_C_WME) == 0)
return EINVAL;
ac = (ireq->i_len & IEEE80211_WMEPARAM_VAL);
if (ac >= WME_NUM_AC)
ac = WME_AC_BE;
if (ireq->i_len & IEEE80211_WMEPARAM_BSS)
wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[ac];
else
wmep = &wme->wme_wmeChanParams.cap_wmeParams[ac];
switch (ireq->i_type) {
case IEEE80211_IOC_WME_CWMIN: /* WME: CWmin */
ireq->i_val = wmep->wmep_logcwmin;
break;
case IEEE80211_IOC_WME_CWMAX: /* WME: CWmax */
ireq->i_val = wmep->wmep_logcwmax;
break;
case IEEE80211_IOC_WME_AIFS: /* WME: AIFS */
ireq->i_val = wmep->wmep_aifsn;
break;
case IEEE80211_IOC_WME_TXOPLIMIT: /* WME: txops limit */
ireq->i_val = wmep->wmep_txopLimit;
break;
case IEEE80211_IOC_WME_ACM: /* WME: ACM (bss only) */
wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[ac];
ireq->i_val = wmep->wmep_acm;
break;
case IEEE80211_IOC_WME_ACKPOLICY: /* WME: ACK policy (!bss only)*/
wmep = &wme->wme_wmeChanParams.cap_wmeParams[ac];
ireq->i_val = !wmep->wmep_noackPolicy;
break;
}
return 0;
}
static int
ieee80211_ioctl_getmaccmd(struct ieee80211com *ic, struct ieee80211req *ireq)
{
const struct ieee80211_aclator *acl = ic->ic_acl;
return (acl == NULL ? EINVAL : acl->iac_getioctl(ic, ireq));
}
/*
* When building the kernel with -O2 on the i386 architecture, gcc
* seems to want to inline this function into ieee80211_ioctl()
* (which is the only routine that calls it). When this happens,
* ieee80211_ioctl() ends up consuming an additional 2K of stack
* space. (Exactly why it needs so much is unclear.) The problem
* is that it's possible for ieee80211_ioctl() to invoke other
* routines (including driver init functions) which could then find
* themselves perilously close to exhausting the stack.
*
* To avoid this, we deliberately prevent gcc from inlining this
* routine. Another way to avoid this is to use less agressive
* optimization when compiling this file (i.e. -O instead of -O2)
* but special-casing the compilation of this one module in the
* build system would be awkward.
*/
#ifdef __GNUC__
__attribute__ ((noinline))
#endif
static int
ieee80211_ioctl_get80211(struct ieee80211com *ic, u_long cmd, struct ieee80211req *ireq)
{
const struct ieee80211_rsnparms *rsn = &ic->ic_bss->ni_rsn;
int error = 0;
u_int kid, len, m;
u_int8_t tmpkey[IEEE80211_KEYBUF_SIZE];
char tmpssid[IEEE80211_NWID_LEN];
switch (ireq->i_type) {
case IEEE80211_IOC_SSID:
switch (ic->ic_state) {
case IEEE80211_S_INIT:
case IEEE80211_S_SCAN:
ireq->i_len = ic->ic_des_esslen;
memcpy(tmpssid, ic->ic_des_essid, ireq->i_len);
break;
default:
ireq->i_len = ic->ic_bss->ni_esslen;
memcpy(tmpssid, ic->ic_bss->ni_essid,
ireq->i_len);
break;
}
error = copyout(tmpssid, ireq->i_data, ireq->i_len);
break;
case IEEE80211_IOC_NUMSSIDS:
ireq->i_val = 1;
break;
case IEEE80211_IOC_WEP:
if ((ic->ic_flags & IEEE80211_F_PRIVACY) == 0)
ireq->i_val = IEEE80211_WEP_OFF;
else if (ic->ic_flags & IEEE80211_F_DROPUNENC)
ireq->i_val = IEEE80211_WEP_ON;
else
ireq->i_val = IEEE80211_WEP_MIXED;
break;
case IEEE80211_IOC_WEPKEY:
kid = (u_int) ireq->i_val;
if (kid >= IEEE80211_WEP_NKID)
return EINVAL;
len = (u_int) ic->ic_nw_keys[kid].wk_keylen;
/* NB: only root can read WEP keys */
if (priv_check(curthread, PRIV_NET80211_GETKEY) == 0) {
bcopy(ic->ic_nw_keys[kid].wk_key, tmpkey, len);
} else {
bzero(tmpkey, len);
}
ireq->i_len = len;
error = copyout(tmpkey, ireq->i_data, len);
break;
case IEEE80211_IOC_NUMWEPKEYS:
ireq->i_val = IEEE80211_WEP_NKID;
break;
case IEEE80211_IOC_WEPTXKEY:
ireq->i_val = ic->ic_def_txkey;
break;
case IEEE80211_IOC_AUTHMODE:
if (ic->ic_flags & IEEE80211_F_WPA)
ireq->i_val = IEEE80211_AUTH_WPA;
else
ireq->i_val = ic->ic_bss->ni_authmode;
break;
case IEEE80211_IOC_CHANNEL:
ireq->i_val = ieee80211_chan2ieee(ic, ic->ic_curchan);
break;
case IEEE80211_IOC_POWERSAVE:
if (ic->ic_flags & IEEE80211_F_PMGTON)
ireq->i_val = IEEE80211_POWERSAVE_ON;
else
ireq->i_val = IEEE80211_POWERSAVE_OFF;
break;
case IEEE80211_IOC_POWERSAVESLEEP:
ireq->i_val = ic->ic_lintval;
break;
case IEEE80211_IOC_RTSTHRESHOLD:
ireq->i_val = ic->ic_rtsthreshold;
break;
case IEEE80211_IOC_PROTMODE:
ireq->i_val = ic->ic_protmode;
break;
case IEEE80211_IOC_TXPOWER:
if ((ic->ic_caps & IEEE80211_C_TXPMGT) == 0)
return EINVAL;
ireq->i_val = ic->ic_txpowlimit;
break;
case IEEE80211_IOC_MCASTCIPHER:
ireq->i_val = rsn->rsn_mcastcipher;
break;
case IEEE80211_IOC_MCASTKEYLEN:
ireq->i_val = rsn->rsn_mcastkeylen;
break;
case IEEE80211_IOC_UCASTCIPHERS:
ireq->i_val = 0;
for (m = 0x1; m != 0; m <<= 1)
if (rsn->rsn_ucastcipherset & m)
ireq->i_val |= 1<<cap2cipher(m);
break;
case IEEE80211_IOC_UCASTCIPHER:
ireq->i_val = rsn->rsn_ucastcipher;
break;
case IEEE80211_IOC_UCASTKEYLEN:
ireq->i_val = rsn->rsn_ucastkeylen;
break;
case IEEE80211_IOC_KEYMGTALGS:
ireq->i_val = rsn->rsn_keymgmtset;
break;
case IEEE80211_IOC_RSNCAPS:
ireq->i_val = rsn->rsn_caps;
break;
case IEEE80211_IOC_WPA:
switch (ic->ic_flags & IEEE80211_F_WPA) {
case IEEE80211_F_WPA1:
ireq->i_val = 1;
break;
case IEEE80211_F_WPA2:
ireq->i_val = 2;
break;
case IEEE80211_F_WPA1 | IEEE80211_F_WPA2:
ireq->i_val = 3;
break;
default:
ireq->i_val = 0;
break;
}
break;
case IEEE80211_IOC_CHANLIST:
error = ieee80211_ioctl_getchanlist(ic, ireq);
break;
case IEEE80211_IOC_ROAMING:
ireq->i_val = ic->ic_roaming;
break;
case IEEE80211_IOC_PRIVACY:
ireq->i_val = (ic->ic_flags & IEEE80211_F_PRIVACY) != 0;
break;
case IEEE80211_IOC_DROPUNENCRYPTED:
ireq->i_val = (ic->ic_flags & IEEE80211_F_DROPUNENC) != 0;
break;
case IEEE80211_IOC_COUNTERMEASURES:
ireq->i_val = (ic->ic_flags & IEEE80211_F_COUNTERM) != 0;
break;
case IEEE80211_IOC_DRIVER_CAPS:
ireq->i_val = ic->ic_caps>>16;
ireq->i_len = ic->ic_caps&0xffff;
break;
case IEEE80211_IOC_WME:
ireq->i_val = (ic->ic_flags & IEEE80211_F_WME) != 0;
break;
case IEEE80211_IOC_HIDESSID:
ireq->i_val = (ic->ic_flags & IEEE80211_F_HIDESSID) != 0;
break;
case IEEE80211_IOC_APBRIDGE:
ireq->i_val = (ic->ic_flags & IEEE80211_F_NOBRIDGE) == 0;
break;
case IEEE80211_IOC_OPTIE:
if (ic->ic_opt_ie == NULL)
return EINVAL;
/* NB: truncate, caller can check length */
if (ireq->i_len > ic->ic_opt_ie_len)
ireq->i_len = ic->ic_opt_ie_len;
error = copyout(ic->ic_opt_ie, ireq->i_data, ireq->i_len);
break;
case IEEE80211_IOC_WPAKEY:
error = ieee80211_ioctl_getkey(ic, ireq);
break;
case IEEE80211_IOC_CHANINFO:
error = ieee80211_ioctl_getchaninfo(ic, ireq);
break;
case IEEE80211_IOC_BSSID:
if (ireq->i_len != IEEE80211_ADDR_LEN)
return EINVAL;
error = copyout(ic->ic_state == IEEE80211_S_RUN ?
ic->ic_bss->ni_bssid :
ic->ic_des_bssid,
ireq->i_data, ireq->i_len);
break;
case IEEE80211_IOC_WPAIE:
error = ieee80211_ioctl_getwpaie(ic, ireq);
break;
#ifdef COMPAT_FREEBSD6
case IEEE80211_IOC_SCAN_RESULTS_OLD:
error = old_getscanresults(ic, ireq);
break;
#endif
case IEEE80211_IOC_SCAN_RESULTS:
error = ieee80211_ioctl_getscanresults(ic, ireq);
break;
case IEEE80211_IOC_STA_STATS:
error = ieee80211_ioctl_getstastats(ic, ireq);
break;
case IEEE80211_IOC_TXPOWMAX:
ireq->i_val = ic->ic_bss->ni_txpower;
break;
case IEEE80211_IOC_STA_TXPOW:
error = ieee80211_ioctl_getstatxpow(ic, ireq);
break;
#ifdef COMPAT_FREEBSD6
case IEEE80211_IOC_STA_INFO_OLD:
error = old_getstainfo(ic, ireq);
break;
#endif
case IEEE80211_IOC_STA_INFO:
error = ieee80211_ioctl_getstainfo(ic, ireq);
break;
case IEEE80211_IOC_WME_CWMIN: /* WME: CWmin */
case IEEE80211_IOC_WME_CWMAX: /* WME: CWmax */
case IEEE80211_IOC_WME_AIFS: /* WME: AIFS */
case IEEE80211_IOC_WME_TXOPLIMIT: /* WME: txops limit */
case IEEE80211_IOC_WME_ACM: /* WME: ACM (bss only) */
case IEEE80211_IOC_WME_ACKPOLICY: /* WME: ACK policy (bss only) */
error = ieee80211_ioctl_getwmeparam(ic, ireq);
break;
case IEEE80211_IOC_DTIM_PERIOD:
ireq->i_val = ic->ic_dtim_period;
break;
case IEEE80211_IOC_BEACON_INTERVAL:
/* NB: get from ic_bss for station mode */
ireq->i_val = ic->ic_bss->ni_intval;
break;
case IEEE80211_IOC_PUREG:
ireq->i_val = (ic->ic_flags & IEEE80211_F_PUREG) != 0;
break;
case IEEE80211_IOC_MCAST_RATE:
ireq->i_val = ic->ic_mcast_rate;
break;
case IEEE80211_IOC_FRAGTHRESHOLD:
ireq->i_val = ic->ic_fragthreshold;
break;
case IEEE80211_IOC_MACCMD:
error = ieee80211_ioctl_getmaccmd(ic, ireq);
break;
case IEEE80211_IOC_BURST:
ireq->i_val = (ic->ic_flags & IEEE80211_F_BURST) != 0;
break;
case IEEE80211_IOC_BMISSTHRESHOLD:
ireq->i_val = ic->ic_bmissthreshold;
break;
default:
error = EINVAL;
break;
}
return error;
}
static int
ieee80211_ioctl_setoptie(struct ieee80211com *ic, struct ieee80211req *ireq)
{
int error;
void *ie, *oie;
/*
* NB: Doing this for ap operation could be useful (e.g. for
* WPA and/or WME) except that it typically is worthless
* without being able to intervene when processing
* association response frames--so disallow it for now.
*/
if (ic->ic_opmode != IEEE80211_M_STA)
return EINVAL;
if (ireq->i_len > IEEE80211_MAX_OPT_IE)
return EINVAL;
if (ireq->i_len > 0) {
MALLOC(ie, void *, ireq->i_len, M_DEVBUF, M_NOWAIT);
if (ie == NULL)
return ENOMEM;
error = copyin(ireq->i_data, ie, ireq->i_len);
if (error) {
FREE(ie, M_DEVBUF);
return error;
}
} else {
ie = NULL;
ireq->i_len = 0;
}
/* XXX sanity check data? */
oie = ic->ic_opt_ie;
ic->ic_opt_ie = ie;
ic->ic_opt_ie_len = ireq->i_len;
if (oie != NULL)
FREE(oie, M_DEVBUF);
return 0;
}
static int
ieee80211_ioctl_setkey(struct ieee80211com *ic, struct ieee80211req *ireq)
{
struct ieee80211req_key ik;
struct ieee80211_node *ni;
struct ieee80211_key *wk;
u_int16_t kid;
int error;
if (ireq->i_len != sizeof(ik))
return EINVAL;
error = copyin(ireq->i_data, &ik, sizeof(ik));
if (error)
return error;
/* NB: cipher support is verified by ieee80211_crypt_newkey */
/* NB: this also checks ik->ik_keylen > sizeof(wk->wk_key) */
if (ik.ik_keylen > sizeof(ik.ik_keydata))
return E2BIG;
kid = ik.ik_keyix;
if (kid == IEEE80211_KEYIX_NONE) {
/* XXX unicast keys currently must be tx/rx */
if (ik.ik_flags != (IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV))
return EINVAL;
if (ic->ic_opmode == IEEE80211_M_STA) {
ni = ieee80211_ref_node(ic->ic_bss);
if (!IEEE80211_ADDR_EQ(ik.ik_macaddr, ni->ni_bssid)) {
ieee80211_free_node(ni);
return EADDRNOTAVAIL;
}
} else {
ni = ieee80211_find_node(&ic->ic_sta, ik.ik_macaddr);
if (ni == NULL)
return ENOENT;
}
wk = &ni->ni_ucastkey;
} else {
if (kid >= IEEE80211_WEP_NKID)
return EINVAL;
wk = &ic->ic_nw_keys[kid];
/*
* Global slots start off w/o any assigned key index.
* Force one here for consistency with IEEE80211_IOC_WEPKEY.
*/
if (wk->wk_keyix == IEEE80211_KEYIX_NONE)
wk->wk_keyix = kid;
ni = NULL;
}
error = 0;
ieee80211_key_update_begin(ic);
if (ieee80211_crypto_newkey(ic, ik.ik_type, ik.ik_flags, wk)) {
wk->wk_keylen = ik.ik_keylen;
/* NB: MIC presence is implied by cipher type */
if (wk->wk_keylen > IEEE80211_KEYBUF_SIZE)
wk->wk_keylen = IEEE80211_KEYBUF_SIZE;
wk->wk_keyrsc = ik.ik_keyrsc;
wk->wk_keytsc = 0; /* new key, reset */
memset(wk->wk_key, 0, sizeof(wk->wk_key));
memcpy(wk->wk_key, ik.ik_keydata, ik.ik_keylen);
if (!ieee80211_crypto_setkey(ic, wk,
ni != NULL ? ni->ni_macaddr : ik.ik_macaddr))
error = EIO;
else if ((ik.ik_flags & IEEE80211_KEY_DEFAULT))
ic->ic_def_txkey = kid;
} else
error = ENXIO;
ieee80211_key_update_end(ic);
if (ni != NULL)
ieee80211_free_node(ni);
return error;
}
static int
ieee80211_ioctl_delkey(struct ieee80211com *ic, struct ieee80211req *ireq)
{
struct ieee80211req_del_key dk;
int kid, error;
if (ireq->i_len != sizeof(dk))
return EINVAL;
error = copyin(ireq->i_data, &dk, sizeof(dk));
if (error)
return error;
kid = dk.idk_keyix;
/* XXX u_int8_t -> u_int16_t */
if (dk.idk_keyix == (u_int8_t) IEEE80211_KEYIX_NONE) {
struct ieee80211_node *ni;
if (ic->ic_opmode == IEEE80211_M_STA) {
ni = ieee80211_ref_node(ic->ic_bss);
if (!IEEE80211_ADDR_EQ(dk.idk_macaddr, ni->ni_bssid)) {
ieee80211_free_node(ni);
return EADDRNOTAVAIL;
}
} else {
ni = ieee80211_find_node(&ic->ic_sta, dk.idk_macaddr);
if (ni == NULL)
return ENOENT;
}
/* XXX error return */
ieee80211_node_delucastkey(ni);
ieee80211_free_node(ni);
} else {
if (kid >= IEEE80211_WEP_NKID)
return EINVAL;
/* XXX error return */
ieee80211_crypto_delkey(ic, &ic->ic_nw_keys[kid]);
}
return 0;
}
static void
domlme(void *arg, struct ieee80211_node *ni)
{
struct ieee80211com *ic = ni->ni_ic;
struct ieee80211req_mlme *mlme = arg;
if (ni->ni_associd != 0) {
IEEE80211_SEND_MGMT(ic, ni,
mlme->im_op == IEEE80211_MLME_DEAUTH ?
IEEE80211_FC0_SUBTYPE_DEAUTH :
IEEE80211_FC0_SUBTYPE_DISASSOC,
mlme->im_reason);
}
ieee80211_node_leave(ic, ni);
}
static int
ieee80211_ioctl_setmlme(struct ieee80211com *ic, struct ieee80211req *ireq)
{
struct ieee80211req_mlme mlme;
struct ieee80211_node *ni;
int error;
if (ireq->i_len != sizeof(mlme))
return EINVAL;
error = copyin(ireq->i_data, &mlme, sizeof(mlme));
if (error)
return error;
switch (mlme.im_op) {
case IEEE80211_MLME_ASSOC:
if (ic->ic_opmode != IEEE80211_M_STA)
return EINVAL;
/* XXX must be in S_SCAN state? */
if (mlme.im_ssid_len != 0) {
/*
* Desired ssid specified; must match both bssid and
* ssid to distinguish ap advertising multiple ssid's.
*/
ni = ieee80211_find_node_with_ssid(&ic->ic_scan,
mlme.im_macaddr,
mlme.im_ssid_len, mlme.im_ssid);
} else {
/*
* Normal case; just match bssid.
*/
ni = ieee80211_find_node(&ic->ic_scan, mlme.im_macaddr);
}
if (ni == NULL)
return EINVAL;
if (!ieee80211_sta_join(ic, ni)) {
ieee80211_free_node(ni);
return EINVAL;
}
break;
case IEEE80211_MLME_DISASSOC:
case IEEE80211_MLME_DEAUTH:
switch (ic->ic_opmode) {
case IEEE80211_M_STA:
/* XXX not quite right */
ieee80211_new_state(ic, IEEE80211_S_INIT,
mlme.im_reason);
break;
case IEEE80211_M_HOSTAP:
/* NB: the broadcast address means do 'em all */
if (!IEEE80211_ADDR_EQ(mlme.im_macaddr, ic->ic_ifp->if_broadcastaddr)) {
if ((ni = ieee80211_find_node(&ic->ic_sta,
mlme.im_macaddr)) == NULL)
return EINVAL;
domlme(&mlme, ni);
ieee80211_free_node(ni);
} else {
ieee80211_iterate_nodes(&ic->ic_sta,
domlme, &mlme);
}
break;
default:
return EINVAL;
}
break;
case IEEE80211_MLME_AUTHORIZE:
case IEEE80211_MLME_UNAUTHORIZE:
if (ic->ic_opmode != IEEE80211_M_HOSTAP)
return EINVAL;
ni = ieee80211_find_node(&ic->ic_sta, mlme.im_macaddr);
if (ni == NULL)
return EINVAL;
if (mlme.im_op == IEEE80211_MLME_AUTHORIZE)
ieee80211_node_authorize(ni);
else
ieee80211_node_unauthorize(ni);
ieee80211_free_node(ni);
break;
default:
return EINVAL;
}
return 0;
}
static int
ieee80211_ioctl_macmac(struct ieee80211com *ic, struct ieee80211req *ireq)
{
u_int8_t mac[IEEE80211_ADDR_LEN];
const struct ieee80211_aclator *acl = ic->ic_acl;
int error;
if (ireq->i_len != sizeof(mac))
return EINVAL;
error = copyin(ireq->i_data, mac, ireq->i_len);
if (error)
return error;
if (acl == NULL) {
acl = ieee80211_aclator_get("mac");
if (acl == NULL || !acl->iac_attach(ic))
return EINVAL;
ic->ic_acl = acl;
}
if (ireq->i_type == IEEE80211_IOC_ADDMAC)
acl->iac_add(ic, mac);
else
acl->iac_remove(ic, mac);
return 0;
}
static int
ieee80211_ioctl_setmaccmd(struct ieee80211com *ic, struct ieee80211req *ireq)
{
const struct ieee80211_aclator *acl = ic->ic_acl;
switch (ireq->i_val) {
case IEEE80211_MACCMD_POLICY_OPEN:
case IEEE80211_MACCMD_POLICY_ALLOW:
case IEEE80211_MACCMD_POLICY_DENY:
if (acl == NULL) {
acl = ieee80211_aclator_get("mac");
if (acl == NULL || !acl->iac_attach(ic))
return EINVAL;
ic->ic_acl = acl;
}
acl->iac_setpolicy(ic, ireq->i_val);
break;
case IEEE80211_MACCMD_FLUSH:
if (acl != NULL)
acl->iac_flush(ic);
/* NB: silently ignore when not in use */
break;
case IEEE80211_MACCMD_DETACH:
if (acl != NULL) {
ic->ic_acl = NULL;
acl->iac_detach(ic);
}
break;
default:
if (acl == NULL)
return EINVAL;
else
return acl->iac_setioctl(ic, ireq);
}
return 0;
}
static int
ieee80211_ioctl_setchanlist(struct ieee80211com *ic, struct ieee80211req *ireq)
{
struct ieee80211req_chanlist list;
u_char chanlist[IEEE80211_CHAN_BYTES];
int i, j, error;
if (ireq->i_len != sizeof(list))
return EINVAL;
error = copyin(ireq->i_data, &list, sizeof(list));
if (error)
return error;
memset(chanlist, 0, sizeof(chanlist));
/*
* Since channel 0 is not available for DS, channel 1
* is assigned to LSB on WaveLAN.
*/
if (ic->ic_phytype == IEEE80211_T_DS)
i = 1;
else
i = 0;
for (j = 0; i <= IEEE80211_CHAN_MAX; i++, j++) {
/*
* NB: silently discard unavailable channels so users
* can specify 1-255 to get all available channels.
*/
if (isset(list.ic_channels, j) && isset(ic->ic_chan_avail, i))
setbit(chanlist, i);
}
if (ic->ic_ibss_chan == NULL ||
isclr(chanlist, ieee80211_chan2ieee(ic, ic->ic_ibss_chan))) {
for (i = 0; i <= IEEE80211_CHAN_MAX; i++)
if (isset(chanlist, i)) {
ic->ic_ibss_chan = &ic->ic_channels[i];
goto found;
}
return EINVAL; /* no active channels */
found:
;
}
memcpy(ic->ic_chan_active, chanlist, sizeof(ic->ic_chan_active));
return IS_UP_AUTO(ic) ? ENETRESET : 0;
}
static int
ieee80211_ioctl_setstastats(struct ieee80211com *ic, struct ieee80211req *ireq)
{
struct ieee80211_node *ni;
u_int8_t macaddr[IEEE80211_ADDR_LEN];
int error;
/*
* NB: we could copyin ieee80211req_sta_stats so apps
* could make selective changes but that's overkill;
* just clear all stats for now.
*/
if (ireq->i_len < IEEE80211_ADDR_LEN)
return EINVAL;
error = copyin(ireq->i_data, macaddr, IEEE80211_ADDR_LEN);
if (error != 0)
return error;
ni = ieee80211_find_node(&ic->ic_sta, macaddr);
if (ni == NULL)
return EINVAL; /* XXX */
memset(&ni->ni_stats, 0, sizeof(ni->ni_stats));
ieee80211_free_node(ni);
return 0;
}
static int
ieee80211_ioctl_setstatxpow(struct ieee80211com *ic, struct ieee80211req *ireq)
{
struct ieee80211_node *ni;
struct ieee80211req_sta_txpow txpow;
int error;
if (ireq->i_len != sizeof(txpow))
return EINVAL;
error = copyin(ireq->i_data, &txpow, sizeof(txpow));
if (error != 0)
return error;
ni = ieee80211_find_node(&ic->ic_sta, txpow.it_macaddr);
if (ni == NULL)
return EINVAL; /* XXX */
ni->ni_txpower = txpow.it_txpow;
ieee80211_free_node(ni);
return error;
}
static int
ieee80211_ioctl_setwmeparam(struct ieee80211com *ic, struct ieee80211req *ireq)
{
struct ieee80211_wme_state *wme = &ic->ic_wme;
struct wmeParams *wmep, *chanp;
int isbss, ac;
if ((ic->ic_caps & IEEE80211_C_WME) == 0)
return EINVAL;
isbss = (ireq->i_len & IEEE80211_WMEPARAM_BSS);
ac = (ireq->i_len & IEEE80211_WMEPARAM_VAL);
if (ac >= WME_NUM_AC)
ac = WME_AC_BE;
if (isbss) {
chanp = &wme->wme_bssChanParams.cap_wmeParams[ac];
wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[ac];
} else {
chanp = &wme->wme_chanParams.cap_wmeParams[ac];
wmep = &wme->wme_wmeChanParams.cap_wmeParams[ac];
}
switch (ireq->i_type) {
case IEEE80211_IOC_WME_CWMIN: /* WME: CWmin */
if (isbss) {
wmep->wmep_logcwmin = ireq->i_val;
if ((wme->wme_flags & WME_F_AGGRMODE) == 0)
chanp->wmep_logcwmin = ireq->i_val;
} else {
wmep->wmep_logcwmin = chanp->wmep_logcwmin =
ireq->i_val;
}
break;
case IEEE80211_IOC_WME_CWMAX: /* WME: CWmax */
if (isbss) {
wmep->wmep_logcwmax = ireq->i_val;
if ((wme->wme_flags & WME_F_AGGRMODE) == 0)
chanp->wmep_logcwmax = ireq->i_val;
} else {
wmep->wmep_logcwmax = chanp->wmep_logcwmax =
ireq->i_val;
}
break;
case IEEE80211_IOC_WME_AIFS: /* WME: AIFS */
if (isbss) {
wmep->wmep_aifsn = ireq->i_val;
if ((wme->wme_flags & WME_F_AGGRMODE) == 0)
chanp->wmep_aifsn = ireq->i_val;
} else {
wmep->wmep_aifsn = chanp->wmep_aifsn = ireq->i_val;
}
break;
case IEEE80211_IOC_WME_TXOPLIMIT: /* WME: txops limit */
if (isbss) {
wmep->wmep_txopLimit = ireq->i_val;
if ((wme->wme_flags & WME_F_AGGRMODE) == 0)
chanp->wmep_txopLimit = ireq->i_val;
} else {
wmep->wmep_txopLimit = chanp->wmep_txopLimit =
ireq->i_val;
}
break;
case IEEE80211_IOC_WME_ACM: /* WME: ACM (bss only) */
wmep->wmep_acm = ireq->i_val;
if ((wme->wme_flags & WME_F_AGGRMODE) == 0)
chanp->wmep_acm = ireq->i_val;
break;
case IEEE80211_IOC_WME_ACKPOLICY: /* WME: ACK policy (!bss only)*/
wmep->wmep_noackPolicy = chanp->wmep_noackPolicy =
(ireq->i_val) == 0;
break;
}
ieee80211_wme_updateparams(ic);
return 0;
}
static int
cipher2cap(int cipher)
{
switch (cipher) {
case IEEE80211_CIPHER_WEP: return IEEE80211_C_WEP;
case IEEE80211_CIPHER_AES_OCB: return IEEE80211_C_AES;
case IEEE80211_CIPHER_AES_CCM: return IEEE80211_C_AES_CCM;
case IEEE80211_CIPHER_CKIP: return IEEE80211_C_CKIP;
case IEEE80211_CIPHER_TKIP: return IEEE80211_C_TKIP;
}
return 0;
}
static int
ieee80211_ioctl_set80211(struct ieee80211com *ic, u_long cmd, struct ieee80211req *ireq)
{
static const u_int8_t zerobssid[IEEE80211_ADDR_LEN];
struct ieee80211_rsnparms *rsn = &ic->ic_bss->ni_rsn;
int error;
const struct ieee80211_authenticator *auth;
u_int8_t tmpkey[IEEE80211_KEYBUF_SIZE];
char tmpssid[IEEE80211_NWID_LEN];
u_int8_t tmpbssid[IEEE80211_ADDR_LEN];
struct ieee80211_key *k;
int j, caps;
u_int kid;
error = 0;
switch (ireq->i_type) {
case IEEE80211_IOC_SSID:
if (ireq->i_val != 0 ||
ireq->i_len > IEEE80211_NWID_LEN)
return EINVAL;
error = copyin(ireq->i_data, tmpssid, ireq->i_len);
if (error)
break;
memset(ic->ic_des_essid, 0, IEEE80211_NWID_LEN);
ic->ic_des_esslen = ireq->i_len;
memcpy(ic->ic_des_essid, tmpssid, ireq->i_len);
error = ENETRESET;
break;
case IEEE80211_IOC_WEP:
switch (ireq->i_val) {
case IEEE80211_WEP_OFF:
ic->ic_flags &= ~IEEE80211_F_PRIVACY;
ic->ic_flags &= ~IEEE80211_F_DROPUNENC;
break;
case IEEE80211_WEP_ON:
ic->ic_flags |= IEEE80211_F_PRIVACY;
ic->ic_flags |= IEEE80211_F_DROPUNENC;
break;
case IEEE80211_WEP_MIXED:
ic->ic_flags |= IEEE80211_F_PRIVACY;
ic->ic_flags &= ~IEEE80211_F_DROPUNENC;
break;
}
error = ENETRESET;
break;
case IEEE80211_IOC_WEPKEY:
kid = (u_int) ireq->i_val;
if (kid >= IEEE80211_WEP_NKID)
return EINVAL;
k = &ic->ic_nw_keys[kid];
if (ireq->i_len == 0) {
/* zero-len =>'s delete any existing key */
(void) ieee80211_crypto_delkey(ic, k);
break;
}
if (ireq->i_len > sizeof(tmpkey))
return EINVAL;
memset(tmpkey, 0, sizeof(tmpkey));
error = copyin(ireq->i_data, tmpkey, ireq->i_len);
if (error)
break;
ieee80211_key_update_begin(ic);
k->wk_keyix = kid; /* NB: force fixed key id */
if (ieee80211_crypto_newkey(ic, IEEE80211_CIPHER_WEP,
IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV, k)) {
k->wk_keylen = ireq->i_len;
memcpy(k->wk_key, tmpkey, sizeof(tmpkey));
if (!ieee80211_crypto_setkey(ic, k, ic->ic_myaddr))
error = EINVAL;
} else
error = EINVAL;
ieee80211_key_update_end(ic);
if (!error) /* NB: for compatibility */
error = ENETRESET;
break;
case IEEE80211_IOC_WEPTXKEY:
kid = (u_int) ireq->i_val;
if (kid >= IEEE80211_WEP_NKID &&
(u_int16_t) kid != IEEE80211_KEYIX_NONE)
return EINVAL;
ic->ic_def_txkey = kid;
error = ENETRESET; /* push to hardware */
break;
case IEEE80211_IOC_AUTHMODE:
switch (ireq->i_val) {
case IEEE80211_AUTH_WPA:
case IEEE80211_AUTH_8021X: /* 802.1x */
case IEEE80211_AUTH_OPEN: /* open */
case IEEE80211_AUTH_SHARED: /* shared-key */
case IEEE80211_AUTH_AUTO: /* auto */
auth = ieee80211_authenticator_get(ireq->i_val);
if (auth == NULL)
return EINVAL;
break;
default:
return EINVAL;
}
switch (ireq->i_val) {
case IEEE80211_AUTH_WPA: /* WPA w/ 802.1x */
ic->ic_flags |= IEEE80211_F_PRIVACY;
ireq->i_val = IEEE80211_AUTH_8021X;
break;
case IEEE80211_AUTH_OPEN: /* open */
ic->ic_flags &= ~(IEEE80211_F_WPA|IEEE80211_F_PRIVACY);
break;
case IEEE80211_AUTH_SHARED: /* shared-key */
case IEEE80211_AUTH_8021X: /* 802.1x */
ic->ic_flags &= ~IEEE80211_F_WPA;
/* both require a key so mark the PRIVACY capability */
ic->ic_flags |= IEEE80211_F_PRIVACY;
break;
case IEEE80211_AUTH_AUTO: /* auto */
ic->ic_flags &= ~IEEE80211_F_WPA;
/* XXX PRIVACY handling? */
/* XXX what's the right way to do this? */
break;
}
/* NB: authenticator attach/detach happens on state change */
ic->ic_bss->ni_authmode = ireq->i_val;
/* XXX mixed/mode/usage? */
ic->ic_auth = auth;
error = ENETRESET;
break;
case IEEE80211_IOC_CHANNEL:
/* XXX 0xffff overflows 16-bit signed */
if (ireq->i_val == 0 ||
ireq->i_val == (int16_t) IEEE80211_CHAN_ANY)
ic->ic_des_chan = IEEE80211_CHAN_ANYC;
else if ((u_int) ireq->i_val > IEEE80211_CHAN_MAX ||
isclr(ic->ic_chan_active, ireq->i_val)) {
return EINVAL;
} else
ic->ic_ibss_chan = ic->ic_des_chan =
&ic->ic_channels[ireq->i_val];
switch (ic->ic_state) {
case IEEE80211_S_INIT:
case IEEE80211_S_SCAN:
error = ENETRESET;
break;
default:
/*
* If the desired channel has changed (to something
* other than any) and we're not already scanning,
* then kick the state machine.
*/
if (ic->ic_des_chan != IEEE80211_CHAN_ANYC &&
ic->ic_bss->ni_chan != ic->ic_des_chan &&
(ic->ic_flags & IEEE80211_F_SCAN) == 0)
error = ENETRESET;
break;
}
if (error == ENETRESET &&
ic->ic_opmode == IEEE80211_M_MONITOR) {
if (IS_UP(ic)) {
/*
* Monitor mode can switch directly.
*/
if (ic->ic_des_chan != IEEE80211_CHAN_ANYC)
ic->ic_curchan = ic->ic_des_chan;
error = ic->ic_reset(ic->ic_ifp);
} else
error = 0;
}
break;
case IEEE80211_IOC_POWERSAVE:
switch (ireq->i_val) {
case IEEE80211_POWERSAVE_OFF:
if (ic->ic_flags & IEEE80211_F_PMGTON) {
ic->ic_flags &= ~IEEE80211_F_PMGTON;
error = ENETRESET;
}
break;
case IEEE80211_POWERSAVE_ON:
if ((ic->ic_caps & IEEE80211_C_PMGT) == 0)
error = EINVAL;
else if ((ic->ic_flags & IEEE80211_F_PMGTON) == 0) {
ic->ic_flags |= IEEE80211_F_PMGTON;
error = ENETRESET;
}
break;
default:
error = EINVAL;
break;
}
if (error == ENETRESET) {
/*
* Switching in+out of power save mode
* should not require a state change.
*/
error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
}
break;
case IEEE80211_IOC_POWERSAVESLEEP:
if (ireq->i_val < 0)
return EINVAL;
ic->ic_lintval = ireq->i_val;
error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
break;
case IEEE80211_IOC_RTSTHRESHOLD:
if (!(IEEE80211_RTS_MIN <= ireq->i_val &&
ireq->i_val <= IEEE80211_RTS_MAX))
return EINVAL;
ic->ic_rtsthreshold = ireq->i_val;
error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
break;
case IEEE80211_IOC_PROTMODE:
if (ireq->i_val > IEEE80211_PROT_RTSCTS)
return EINVAL;
ic->ic_protmode = ireq->i_val;
/* NB: if not operating in 11g this can wait */
if (ic->ic_curmode == IEEE80211_MODE_11G)
error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
break;
case IEEE80211_IOC_TXPOWER:
if ((ic->ic_caps & IEEE80211_C_TXPMGT) == 0)
return EINVAL;
if (!(IEEE80211_TXPOWER_MIN < ireq->i_val &&
ireq->i_val < IEEE80211_TXPOWER_MAX))
return EINVAL;
ic->ic_txpowlimit = ireq->i_val;
error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
break;
case IEEE80211_IOC_ROAMING:
if (!(IEEE80211_ROAMING_DEVICE <= ireq->i_val &&
ireq->i_val <= IEEE80211_ROAMING_MANUAL))
return EINVAL;
ic->ic_roaming = ireq->i_val;
/* XXXX reset? */
break;
case IEEE80211_IOC_PRIVACY:
if (ireq->i_val) {
/* XXX check for key state? */
ic->ic_flags |= IEEE80211_F_PRIVACY;
} else
ic->ic_flags &= ~IEEE80211_F_PRIVACY;
break;
case IEEE80211_IOC_DROPUNENCRYPTED:
if (ireq->i_val)
ic->ic_flags |= IEEE80211_F_DROPUNENC;
else
ic->ic_flags &= ~IEEE80211_F_DROPUNENC;
break;
case IEEE80211_IOC_WPAKEY:
error = ieee80211_ioctl_setkey(ic, ireq);
break;
case IEEE80211_IOC_DELKEY:
error = ieee80211_ioctl_delkey(ic, ireq);
break;
case IEEE80211_IOC_MLME:
error = ieee80211_ioctl_setmlme(ic, ireq);
break;
case IEEE80211_IOC_OPTIE:
error = ieee80211_ioctl_setoptie(ic, ireq);
break;
case IEEE80211_IOC_COUNTERMEASURES:
if (ireq->i_val) {
if ((ic->ic_flags & IEEE80211_F_WPA) == 0)
return EINVAL;
ic->ic_flags |= IEEE80211_F_COUNTERM;
} else
ic->ic_flags &= ~IEEE80211_F_COUNTERM;
break;
case IEEE80211_IOC_WPA:
if (ireq->i_val > 3)
return EINVAL;
/* XXX verify ciphers available */
ic->ic_flags &= ~IEEE80211_F_WPA;
switch (ireq->i_val) {
case 1:
ic->ic_flags |= IEEE80211_F_WPA1;
break;
case 2:
ic->ic_flags |= IEEE80211_F_WPA2;
break;
case 3:
ic->ic_flags |= IEEE80211_F_WPA1 | IEEE80211_F_WPA2;
break;
}
error = ENETRESET; /* XXX? */
break;
case IEEE80211_IOC_WME:
if (ireq->i_val) {
if ((ic->ic_caps & IEEE80211_C_WME) == 0)
return EINVAL;
ic->ic_flags |= IEEE80211_F_WME;
} else
ic->ic_flags &= ~IEEE80211_F_WME;
error = ENETRESET; /* XXX maybe not for station? */
break;
case IEEE80211_IOC_HIDESSID:
if (ireq->i_val)
ic->ic_flags |= IEEE80211_F_HIDESSID;
else
ic->ic_flags &= ~IEEE80211_F_HIDESSID;
error = ENETRESET;
break;
case IEEE80211_IOC_APBRIDGE:
if (ireq->i_val == 0)
ic->ic_flags |= IEEE80211_F_NOBRIDGE;
else
ic->ic_flags &= ~IEEE80211_F_NOBRIDGE;
break;
case IEEE80211_IOC_MCASTCIPHER:
if ((ic->ic_caps & cipher2cap(ireq->i_val)) == 0 &&
!ieee80211_crypto_available(ireq->i_val))
return EINVAL;
rsn->rsn_mcastcipher = ireq->i_val;
error = (ic->ic_flags & IEEE80211_F_WPA) ? ENETRESET : 0;
break;
case IEEE80211_IOC_MCASTKEYLEN:
if (!(0 < ireq->i_val && ireq->i_val < IEEE80211_KEYBUF_SIZE))
return EINVAL;
/* XXX no way to verify driver capability */
rsn->rsn_mcastkeylen = ireq->i_val;
error = (ic->ic_flags & IEEE80211_F_WPA) ? ENETRESET : 0;
break;
case IEEE80211_IOC_UCASTCIPHERS:
/*
* Convert user-specified cipher set to the set
* we can support (via hardware or software).
* NB: this logic intentionally ignores unknown and
* unsupported ciphers so folks can specify 0xff or
* similar and get all available ciphers.
*/
caps = 0;
for (j = 1; j < 32; j++) /* NB: skip WEP */
if ((ireq->i_val & (1<<j)) &&
((ic->ic_caps & cipher2cap(j)) ||
ieee80211_crypto_available(j)))
caps |= 1<<j;
if (caps == 0) /* nothing available */
return EINVAL;
/* XXX verify ciphers ok for unicast use? */
/* XXX disallow if running as it'll have no effect */
rsn->rsn_ucastcipherset = caps;
error = (ic->ic_flags & IEEE80211_F_WPA) ? ENETRESET : 0;
break;
case IEEE80211_IOC_UCASTCIPHER:
if ((rsn->rsn_ucastcipherset & cipher2cap(ireq->i_val)) == 0)
return EINVAL;
rsn->rsn_ucastcipher = ireq->i_val;
break;
case IEEE80211_IOC_UCASTKEYLEN:
if (!(0 < ireq->i_val && ireq->i_val < IEEE80211_KEYBUF_SIZE))
return EINVAL;
/* XXX no way to verify driver capability */
rsn->rsn_ucastkeylen = ireq->i_val;
break;
case IEEE80211_IOC_DRIVER_CAPS:
/* NB: for testing */
ic->ic_caps = (((u_int16_t) ireq->i_val) << 16) |
((u_int16_t) ireq->i_len);
break;
case IEEE80211_IOC_KEYMGTALGS:
/* XXX check */
rsn->rsn_keymgmtset = ireq->i_val;
error = (ic->ic_flags & IEEE80211_F_WPA) ? ENETRESET : 0;
break;
case IEEE80211_IOC_RSNCAPS:
/* XXX check */
rsn->rsn_caps = ireq->i_val;
error = (ic->ic_flags & IEEE80211_F_WPA) ? ENETRESET : 0;
break;
case IEEE80211_IOC_BSSID:
if (ireq->i_len != sizeof(tmpbssid))
return EINVAL;
error = copyin(ireq->i_data, tmpbssid, ireq->i_len);
if (error)
break;
IEEE80211_ADDR_COPY(ic->ic_des_bssid, tmpbssid);
if (IEEE80211_ADDR_EQ(ic->ic_des_bssid, zerobssid))
ic->ic_flags &= ~IEEE80211_F_DESBSSID;
else
ic->ic_flags |= IEEE80211_F_DESBSSID;
error = ENETRESET;
break;
case IEEE80211_IOC_CHANLIST:
error = ieee80211_ioctl_setchanlist(ic, ireq);
break;
case IEEE80211_IOC_SCAN_REQ:
if (ic->ic_opmode == IEEE80211_M_HOSTAP) /* XXX ignore */
break;
error = ieee80211_setupscan(ic, ic->ic_chan_avail);
if (error == 0) /* XXX background scan */
error = ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
break;
case IEEE80211_IOC_ADDMAC:
case IEEE80211_IOC_DELMAC:
error = ieee80211_ioctl_macmac(ic, ireq);
break;
case IEEE80211_IOC_MACCMD:
error = ieee80211_ioctl_setmaccmd(ic, ireq);
break;
case IEEE80211_IOC_STA_STATS:
error = ieee80211_ioctl_setstastats(ic, ireq);
break;
case IEEE80211_IOC_STA_TXPOW:
error = ieee80211_ioctl_setstatxpow(ic, ireq);
break;
case IEEE80211_IOC_WME_CWMIN: /* WME: CWmin */
case IEEE80211_IOC_WME_CWMAX: /* WME: CWmax */
case IEEE80211_IOC_WME_AIFS: /* WME: AIFS */
case IEEE80211_IOC_WME_TXOPLIMIT: /* WME: txops limit */
case IEEE80211_IOC_WME_ACM: /* WME: ACM (bss only) */
case IEEE80211_IOC_WME_ACKPOLICY: /* WME: ACK policy (bss only) */
error = ieee80211_ioctl_setwmeparam(ic, ireq);
break;
case IEEE80211_IOC_DTIM_PERIOD:
if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
ic->ic_opmode != IEEE80211_M_IBSS)
return EINVAL;
if (IEEE80211_DTIM_MIN <= ireq->i_val &&
ireq->i_val <= IEEE80211_DTIM_MAX) {
ic->ic_dtim_period = ireq->i_val;
error = ENETRESET; /* requires restart */
} else
error = EINVAL;
break;
case IEEE80211_IOC_BEACON_INTERVAL:
if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
ic->ic_opmode != IEEE80211_M_IBSS)
return EINVAL;
if (IEEE80211_BINTVAL_MIN <= ireq->i_val &&
ireq->i_val <= IEEE80211_BINTVAL_MAX) {
ic->ic_bintval = ireq->i_val;
error = ENETRESET; /* requires restart */
} else
error = EINVAL;
break;
case IEEE80211_IOC_PUREG:
if (ireq->i_val)
ic->ic_flags |= IEEE80211_F_PUREG;
else
ic->ic_flags &= ~IEEE80211_F_PUREG;
/* NB: reset only if we're operating on an 11g channel */
if (ic->ic_curmode == IEEE80211_MODE_11G)
error = ENETRESET;
break;
case IEEE80211_IOC_MCAST_RATE:
ic->ic_mcast_rate = ireq->i_val & IEEE80211_RATE_VAL;
break;
case IEEE80211_IOC_FRAGTHRESHOLD:
if ((ic->ic_caps & IEEE80211_C_TXFRAG) == 0 &&
ireq->i_val != IEEE80211_FRAG_MAX)
return EINVAL;
if (!(IEEE80211_FRAG_MIN <= ireq->i_val &&
ireq->i_val <= IEEE80211_FRAG_MAX))
return EINVAL;
ic->ic_fragthreshold = ireq->i_val;
error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
break;
case IEEE80211_IOC_BURST:
if (ireq->i_val) {
if ((ic->ic_caps & IEEE80211_C_BURST) == 0)
return EINVAL;
ic->ic_flags |= IEEE80211_F_BURST;
} else
ic->ic_flags &= ~IEEE80211_F_BURST;
error = ENETRESET; /* XXX maybe not for station? */
break;
case IEEE80211_IOC_BMISSTHRESHOLD:
if (!(IEEE80211_HWBMISS_MIN <= ireq->i_val &&
ireq->i_val <= IEEE80211_HWBMISS_MAX))
return EINVAL;
ic->ic_bmissthreshold = ireq->i_val;
error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
break;
default:
error = EINVAL;
break;
}
if (error == ENETRESET && !IS_UP_AUTO(ic))
error = 0;
return error;
}
int
ieee80211_ioctl(struct ieee80211com *ic, u_long cmd, caddr_t data)
{
struct ifnet *ifp = ic->ic_ifp;
int error = 0;
struct ifreq *ifr;
struct ifaddr *ifa; /* XXX */
switch (cmd) {
case SIOCSIFMEDIA:
case SIOCGIFMEDIA:
error = ifmedia_ioctl(ifp, (struct ifreq *) data,
&ic->ic_media, cmd);
break;
case SIOCG80211:
error = ieee80211_ioctl_get80211(ic, cmd,
(struct ieee80211req *) data);
break;
case SIOCS80211:
error = priv_check(curthread, PRIV_NET80211_MANAGE);
if (error == 0)
error = ieee80211_ioctl_set80211(ic, cmd,
(struct ieee80211req *) data);
break;
case SIOCGIFGENERIC:
error = ieee80211_cfgget(ic, cmd, data);
break;
case SIOCSIFGENERIC:
error = priv_check(curthread, PRIV_NET80211_MANAGE);
if (error)
break;
error = ieee80211_cfgset(ic, cmd, data);
break;
case SIOCG80211STATS:
ifr = (struct ifreq *)data;
copyout(&ic->ic_stats, ifr->ifr_data, sizeof (ic->ic_stats));
break;
case SIOCSIFMTU:
ifr = (struct ifreq *)data;
if (!(IEEE80211_MTU_MIN <= ifr->ifr_mtu &&
ifr->ifr_mtu <= IEEE80211_MTU_MAX))
error = EINVAL;
else
ifp->if_mtu = ifr->ifr_mtu;
break;
case SIOCSIFADDR:
/*
* XXX Handle this directly so we can supress if_init calls.
* XXX This should be done in ether_ioctl but for the moment
* XXX there are too many other parts of the system that
* XXX set IFF_UP and so supress if_init being called when
* XXX it should be.
*/
ifa = (struct ifaddr *) data;
switch (ifa->ifa_addr->sa_family) {
#ifdef INET
case AF_INET:
if ((ifp->if_flags & IFF_UP) == 0) {
ifp->if_flags |= IFF_UP;
ifp->if_init(ifp->if_softc);
}
arp_ifinit(ifp, ifa);
break;
#endif
#ifdef IPX
/*
* XXX - This code is probably wrong,
* but has been copied many times.
*/
case AF_IPX: {
struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
if (ipx_nullhost(*ina))
ina->x_host = *(union ipx_host *)
IF_LLADDR(ifp);
else
bcopy((caddr_t) ina->x_host.c_host,
(caddr_t) IF_LLADDR(ifp),
ETHER_ADDR_LEN);
/* fall thru... */
}
#endif
default:
if ((ifp->if_flags & IFF_UP) == 0) {
ifp->if_flags |= IFF_UP;
ifp->if_init(ifp->if_softc);
}
break;
}
break;
default:
error = ether_ioctl(ifp, cmd, data);
break;
}
return error;
}
|