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
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* This file containes all the routines to handle wireless (more
* accurately, 802.11 "WiFi" family only at this moment) operations.
* This is only phase 0 work so the handling is pretty simple.
*
* When the daemon starts up, for each WiFi interface detected, it'll
* spawn a thread doing an access point (AP) scanning. After the scans
* finish and if one of the WiFi interfaces is chosen to be active, the
* code will pop up a window showing the scan results and wait for the
* user's input on which AP to connect to and then complete the AP
* connection and IP interface set up. WEP/WPA is supported to connect to
* those APs which require it. The code also maintains a list of known
* WiFi APs in the file KNOWN_WIFI_NETS. Whenever the code successfully
* connects to an AP, the AP's ESSID/BSSID will be added to that file.
* This file is used in the following way.
*
* If the AP scan results contain one known AP, the code will automatically
* connect to that AP without asking the user. But if the detected signal
* strength of that AP is weaker than some other AP's, the code will still
* pop up a window asking for user input.
*
* If the AP scan results contain more than one known APs, the code will
* pop up a window listing those known APs only. If the user does not
* make a choice, the full list of available APs will be shown.
*
* If the AP scan results contain no known AP, the full list of available
* APs is shown and the user is asked which AP to connect to.
*
* Note that not all APs broadcast the Beacon. And some events may
* happen during the AP scan such that not all available APs are found.
* So the code also allows a user to manually input an AP's data in the
* pop up window. This allows a user to connect to the aforementioned
* "hidden" APs.
*
* The code also periodically (specified by wlan_scan_interval) checks
* for the health of the AP connection. If the signal strength of the
* connected AP drops below a threshold (specified by wireless_scan_level),
* the code will try to do another scan to find out other APs available.
* If there is currently no connected AP, a scan will also be done
* periodically to look for available APs. In both cases, if there are
* new APs, the above AP connection procedure will be performed.
*
* One limitation of the current code is that a user cannot initiate a
* WiFi APs scan manually. A manual scan can only be done when the code
* shows a pop up window asking for user input. Suppose there is no
* connected AP and periodic scan is going on. If a user wants to
* connect to a hidden AP, this is only possible if there is another
* non-hidden AP available such that after a periodic scan, the pop up
* window is shown. This will be fixed in a later phase.
*/
#include <unistd.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <strings.h>
#include <syslog.h>
#include <assert.h>
#include <limits.h>
#include <errno.h>
#include <sys/stat.h>
#include <syslog.h>
#include <pthread.h>
#include <sys/wait.h>
#include <stropts.h>
#include <sys/types.h>
#include <fcntl.h>
#include <locale.h>
#include <libintl.h>
#include <libdladm.h>
#include <libdllink.h>
#include <libinetutil.h>
#include <libgen.h>
#include "defines.h"
#include "structures.h"
#include "functions.h"
#include "variables.h"
#define WLAN_ENC(sec) \
((sec == DLADM_WLAN_SECMODE_WPA ? "WPA" : \
(sec == DLADM_WLAN_SECMODE_WEP ? "WEP" : "none")))
#define NEED_ENC(sec) \
(sec == DLADM_WLAN_SECMODE_WPA || sec == DLADM_WLAN_SECMODE_WEP)
static pthread_mutex_t wifi_mutex;
static pthread_mutexattr_t wifi_mutex_attr;
static pthread_mutex_t wifi_init_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t wifi_init_cond = PTHREAD_COND_INITIALIZER;
typedef enum {
SUCCESS = 0,
FAILURE,
TRY_AGAIN
} return_vals_t;
typedef enum {
ESSID = 0,
BSSID,
MAX_FIELDS
} known_wifi_nets_fields_t;
/*
* Is a wireless interface doing a scan currently? We only allow one
* wireless interface to do a scan at any one time. This is to
* avoid unnecessary interference. The following variable is used
* to store the interface doing the scan. It is protected by
* wifi_init_mutex.
*/
static struct interface *wifi_scan_intf = NULL;
/* used entries have non NULL memebers */
static struct wireless_lan *wlans = NULL;
static uint_t wireless_lan_count = 0; /* allocated */
static uint_t wireless_lan_used = 0; /* used entries */
static int key_string_to_secobj_value(char *, uint8_t *, uint_t *,
dladm_secobj_class_t);
static int store_key(struct wireless_lan *);
static dladm_wlan_key_t *retrieve_key(const char *, const char *,
dladm_secobj_class_t);
static boolean_t add_wlan_entry(struct interface *, char *, char *, char *,
dladm_wlan_secmode_t);
static boolean_t already_in_visited_wlan_list(const struct wireless_lan *);
static boolean_t check_wlan(struct interface *, const char *);
static boolean_t connect_or_autoconf(struct wireless_lan *, struct interface *);
static return_vals_t connect_to_new_wlan(const struct wireless_lan *, int,
struct interface *);
static boolean_t find_wlan_entry(struct interface *, char *, char *);
static void free_wireless_lan(struct wireless_lan *);
static struct wireless_lan *get_specific_lan(void);
static void get_user_key(struct wireless_lan *);
static int get_user_preference(char *const *, const char *, const char *,
struct wireless_lan **, const struct wireless_lan *);
static char **alloc_argv(int, size_t);
static void free_argv(char **);
static char **build_wlanlist_zargv(const struct wireless_lan *, int,
const char *, int, const char **, int);
static char *get_zenity_response(char *const *);
static boolean_t wlan_autoconf(struct interface *);
static int zenity_height(int);
static boolean_t get_scan_results(void *, dladm_wlan_attr_t *);
static boolean_t known_wifi_nets_lookup(const char *, const char *, char *);
#define WIRELESS_LAN_INIT_COUNT 8
struct visited_wlans_list *visited_wlan_list = NULL;
/*
* The variable wlan_scan_interval controls the interval in seconds
* between periodic scans.
*/
uint_t wlan_scan_interval = 120;
/*
* The variable wireless_scan_level specifies the lowest signal level
* when a periodic wireless scan needs to be done.
*/
dladm_wlan_strength_t wireless_scan_level = DLADM_WLAN_STRENGTH_VERY_WEAK;
/*
* Some constants for zenity args
*/
/*
* For a wlan table, command begins with 5 general args:
* cmdname (argv[0]), window type (list), title, height, and width.
*/
#define ZENITY_LIST_INIT_ARGS 5
/*
* Columns: index, ESSID, BSSID, Encryption Type, Signal Strength
*/
#define ZENITY_COLUMNS_PER_WLAN 5
/*
* Cap the length of an individual arg at 64 bytes.
* Longest args tend to be extra row strings.
*/
#define ZENITY_ARG_LEN 64
/*
* Typical zenity return buffers are index number strings
* or extra row strings; 1024 should be sufficient.
*/
#define ZENITY_RTN_BUF_SIZE 1024
/*
* Alloc an array of (cnt + 1) pointers, where the first cnt pointers
* point to an alloc'd buffer of specified len. The last pointer in
* the array is NULL.
*/
static char **
alloc_argv(int cnt, size_t buflen)
{
int i;
char **argv;
if ((argv = calloc(cnt + 1, sizeof (char *))) == NULL) {
syslog(LOG_ERR, "calloc failed: %m");
return (NULL);
}
for (i = 0; i < cnt; i++) {
if ((argv[i] = malloc(buflen)) == NULL) {
syslog(LOG_ERR, "malloc failed: %m");
free_argv(argv);
return (NULL);
}
}
argv[cnt] = NULL;
return (argv);
}
/*
* Free an argv. Assumes that the first NULL pointer encountered
* indicates the end of the array: that is, that the array is null-
* terminated and that no other elements are NULL.
*/
static void
free_argv(char **argv)
{
int i = 0;
if (argv == NULL)
return;
while (argv[i] != NULL)
free(argv[i++]);
free(argv);
}
void
init_mutexes(void)
{
(void) pthread_mutexattr_init(&wifi_mutex_attr);
(void) pthread_mutexattr_settype(&wifi_mutex_attr,
PTHREAD_MUTEX_RECURSIVE);
(void) pthread_mutex_init(&wifi_mutex, &wifi_mutex_attr);
}
/*
* wlan is expected to be non-NULL.
*/
static void
get_user_key(struct wireless_lan *wlan)
{
dladm_secobj_class_t class;
int zargc, cur;
char **zargv;
/*
* First, test if we have key stored as secobj. If so,
* no need to prompt for it.
*/
class = (wlan->sec_mode == DLADM_WLAN_SECMODE_WEP ?
DLADM_SECOBJ_CLASS_WEP : DLADM_SECOBJ_CLASS_WPA);
wlan->cooked_key = retrieve_key(wlan->essid, wlan->bssid, class);
if (wlan->cooked_key != NULL) {
dprintf("get_user_key: retrieve_key() returns non NULL");
return;
}
if (!valid_graphical_user(B_TRUE))
return;
/*
* build zenity 'entry' argv, with text hidden:
* 'zenity --entry --title=foo --text=bar --hide-text'
* Five args needed.
*/
zargc = 5;
if ((zargv = alloc_argv(zargc, ZENITY_ARG_LEN)) == NULL)
return;
cur = 0;
(void) snprintf(zargv[cur++], ZENITY_ARG_LEN, ZENITY);
(void) snprintf(zargv[cur++], ZENITY_ARG_LEN, "--entry");
(void) snprintf(zargv[cur++], ZENITY_ARG_LEN, "--title=%s",
gettext("Enter Key"));
(void) snprintf(zargv[cur++], ZENITY_ARG_LEN, "--text=%s %s",
gettext("Enter key for WiFi network"), wlan->essid);
(void) snprintf(zargv[cur++], ZENITY_ARG_LEN, "--hide-text");
wlan->raw_key = get_zenity_response(zargv);
if (wlan->raw_key == NULL) {
dprintf("get_user_key: failed to obtain user-specified key");
goto cleanup;
}
/* Store key persistently */
if (store_key(wlan) != 0) {
syslog(LOG_ERR, "get_user_key: failed to store user-specified "
"key");
}
cleanup:
free_argv(zargv);
}
static boolean_t
find_wlan_entry(struct interface *intf, char *essid, char *bssid)
{
int i;
(void) pthread_mutex_lock(&wifi_mutex);
/* Check if the new entry is already there. */
for (i = 0; i < wireless_lan_used; i++) {
/*
* Assume that essid and bssid are already NULL terminated.
* Note that we also check for the interface name here.
* If there is only one wireless interface, it should not
* matter. But if there are more than 1, then it is safer
* to use the interface which finds the AP to connect to
* it.
*/
if (strcmp(wlans[i].essid, essid) == 0 &&
strcmp(wlans[i].bssid, bssid) == 0 &&
strcmp(wlans[i].wl_if_name, intf->if_name) == 0) {
(void) pthread_mutex_unlock(&wifi_mutex);
return (B_TRUE);
}
}
(void) pthread_mutex_unlock(&wifi_mutex);
return (B_FALSE);
}
static void
free_wireless_lan(struct wireless_lan *wlp)
{
free(wlp->essid);
wlp->essid = NULL;
free(wlp->bssid);
wlp->bssid = NULL;
free(wlp->signal_strength);
wlp->signal_strength = NULL;
free(wlp->raw_key);
wlp->raw_key = NULL;
free(wlp->cooked_key);
wlp->cooked_key = NULL;
free(wlp->wl_if_name);
wlp->wl_if_name = NULL;
}
static boolean_t
add_wlan_entry(struct interface *intf, char *essid, char *bssid,
char *signal_strength, dladm_wlan_secmode_t sec)
{
int n;
(void) pthread_mutex_lock(&wifi_mutex);
if (wireless_lan_used == wireless_lan_count) {
int newcnt;
struct wireless_lan *r;
newcnt = (wireless_lan_count == 0) ?
WIRELESS_LAN_INIT_COUNT : wireless_lan_count * 2;
r = realloc(wlans, newcnt * sizeof (*wlans));
if (r == NULL) {
syslog(LOG_ERR, "add_wlan_entry: realloc failed");
(void) pthread_mutex_unlock(&wifi_mutex);
return (B_FALSE);
}
(void) memset((void *)(r + wireless_lan_count), 0,
(newcnt - wireless_lan_count) * sizeof (*r));
wireless_lan_count = newcnt;
wlans = r;
}
n = wireless_lan_used;
wlans[n].essid = strdup(essid);
wlans[n].bssid = strdup(bssid);
wlans[n].signal_strength = strdup(signal_strength);
wlans[n].wl_if_name = strdup(intf->if_name);
wlans[n].sec_mode = sec;
wlans[n].raw_key = NULL;
wlans[n].cooked_key = NULL;
if (wlans[n].essid == NULL || wlans[n].bssid == NULL ||
wlans[n].signal_strength == NULL || wlans[n].wl_if_name == NULL) {
syslog(LOG_ERR, "add_wlan_entry: strdup failed");
free_wireless_lan(&(wlans[n]));
(void) pthread_mutex_unlock(&wifi_mutex);
return (B_FALSE);
}
wireless_lan_used++;
(void) pthread_mutex_unlock(&wifi_mutex);
return (B_TRUE);
}
static void
clear_lan_entries(void)
{
int i;
(void) pthread_mutex_lock(&wifi_mutex);
for (i = 0; i < wireless_lan_used; i++)
free_wireless_lan(&(wlans[i]));
wireless_lan_used = 0;
(void) pthread_mutex_unlock(&wifi_mutex);
}
/*
* Verify if a WiFi NIC is associated with the given ESSID. If the given
* ESSID is NULL, and if the NIC is already connected, return true.
* Otherwise,
*
* 1. If the NIC is associated with the given ESSID, return true.
* 2. If the NIC is not associated with any AP, return false.
* 3. If the NIC is associated with a different AP, tell the driver
* to disassociate with it and then return false.
*/
static boolean_t
check_wlan(struct interface *intf, const char *exp_essid)
{
dladm_wlan_linkattr_t attr;
dladm_status_t status;
char cur_essid[DLADM_STRSIZE];
char errmsg[DLADM_STRSIZE];
status = dladm_wlan_get_linkattr(intf->if_linkid, &attr);
if (status != DLADM_STATUS_OK) {
dprintf("check_wlan: dladm_wlan_get_linkattr() for %s "
"failed: %s", intf->if_name,
dladm_status2str(status, errmsg));
return (B_FALSE);
}
if (attr.la_status == DLADM_WLAN_LINK_DISCONNECTED)
return (B_FALSE);
if (exp_essid == NULL)
return (B_TRUE);
(void) dladm_wlan_essid2str(&attr.la_wlan_attr.wa_essid, cur_essid);
/* Is the NIC associated with the expected one? */
if (strcmp(cur_essid, exp_essid) == 0)
return (B_TRUE);
/* Tell the driver to disassociate with the current AP. */
if (dladm_wlan_disconnect(intf->if_linkid) != DLADM_STATUS_OK) {
dprintf("check_wlan: dladm_wlan_disconnect() for %s fails",
intf->if_name);
}
return (B_FALSE);
}
/*
* Given a wireless interface, use it to scan for available networks.
*/
boolean_t
scan_wireless_nets(struct interface *intf)
{
boolean_t new_ap = B_FALSE;
dladm_status_t status;
int num_ap;
assert(intf->if_type == IF_WIRELESS);
/*
* If there is already a scan in progress, wait until the
* scan is done to avoid interference. But if the interface
* doing the scan is the same as the one requesting the new
* scan, just return.
*
* Whenever a wireless scan is in progress, all the other
* threads checking the wireless AP list should wait.
*/
(void) pthread_mutex_lock(&wifi_init_mutex);
while (wifi_scan_intf != NULL) {
dprintf("scan_wireless_nets in progress: old %s new %s",
wifi_scan_intf->if_name, intf->if_name);
if (strcmp(wifi_scan_intf->if_name, intf->if_name) == 0) {
(void) pthread_mutex_unlock(&wifi_init_mutex);
return (B_FALSE);
}
(void) pthread_cond_wait(&wifi_init_cond, &wifi_init_mutex);
}
wifi_scan_intf = intf;
(void) pthread_mutex_unlock(&wifi_init_mutex);
/*
* Since only one scan is allowed at any one time, no need to grab
* a lock in checking wireless_lan_used.
*/
num_ap = wireless_lan_used;
status = dladm_wlan_scan(intf->if_linkid, intf, get_scan_results);
if (status != DLADM_STATUS_OK)
syslog(LOG_NOTICE, "cannot scan link '%s'", intf->if_name);
else
new_ap = (wireless_lan_used > num_ap);
(void) pthread_mutex_lock(&wifi_init_mutex);
wifi_scan_intf = NULL;
(void) pthread_cond_signal(&wifi_init_cond);
(void) pthread_mutex_unlock(&wifi_init_mutex);
return (new_ap);
}
/* ARGSUSED */
static void
wireless_scan(struct interface *ifp, void *arg)
{
if (ifp->if_type == IF_WIRELESS) {
dprintf("periodic_wireless_scan: %s", ifp->if_name);
if (scan_wireless_nets(ifp)) {
dprintf("new AP added: %s", ifp->if_name);
gen_newif_event(ifp);
}
}
}
static boolean_t
get_scan_results(void *arg, dladm_wlan_attr_t *attrp)
{
dladm_wlan_secmode_t sec;
char essid_name[DLADM_STRSIZE];
char bssid_name[DLADM_STRSIZE];
char strength[DLADM_STRSIZE];
(void) dladm_wlan_essid2str(&attrp->wa_essid, essid_name);
(void) dladm_wlan_bssid2str(&attrp->wa_bssid, bssid_name);
(void) dladm_wlan_strength2str(&attrp->wa_strength, strength);
sec = attrp->wa_secmode;
/*
* Check whether ESSID is "hidden".
* If so try to substitute it with the ESSID from the
* known_wifi_nets with the same BSSID
*/
if (essid_name[0] == '\0') {
if (known_wifi_nets_lookup(essid_name, bssid_name,
essid_name)) {
dprintf("Using ESSID %s with BSSID %s",
essid_name, bssid_name);
}
}
if (!find_wlan_entry(arg, essid_name, bssid_name) &&
add_wlan_entry(arg, essid_name, bssid_name, strength, sec)) {
return (B_TRUE);
}
return (B_FALSE);
}
/* ARGSUSED */
void *
periodic_wireless_scan(void *arg)
{
datalink_id_t linkid;
/*
* No periodic scan if the "-i" option is used to change the
* interval to 0.
*/
if (wlan_scan_interval == 0)
return (NULL);
for (;;) {
int ret;
dladm_wlan_linkattr_t attr;
llp_t *cur_llp;
struct interface *ifp;
ret = poll(NULL, 0, wlan_scan_interval * MILLISEC);
/*
* We assume that once an llp is created, it will never be
* deleted in the lifetime of the process. So it is OK
* to do this assignment without a lock.
*/
cur_llp = link_layer_profile;
/*
* We do a scan if
*
* 1. There is no active profile. Or
* 2. We are now disconnected from the AP. Or
* 3. The signal strength falls below a certain specified level.
*/
if (ret == 0) {
if (cur_llp != NULL) {
if (cur_llp->llp_type != IF_WIRELESS)
continue;
if (dladm_name2info(cur_llp->llp_lname, &linkid,
NULL, NULL, NULL) != DLADM_STATUS_OK ||
dladm_wlan_get_linkattr(linkid, &attr) !=
DLADM_STATUS_OK) {
continue;
}
if (attr.la_status ==
DLADM_WLAN_LINK_CONNECTED &&
attr.la_wlan_attr.wa_strength >
wireless_scan_level) {
continue;
}
/*
* Clear the DHCP flags on this interface;
* this is a "fresh start" for the interface,
* so we should retry dhcp.
*/
ifp = get_interface(cur_llp->llp_lname);
if (ifp != NULL)
ifp->if_lflags &= ~IF_DHCPFLAGS;
/*
* Deactivate the original llp.
* If we reached this point, we either were
* not connected, or were connected with
* "very weak" signal strength; so we're
* assuming that having this llp active was
* not very useful. So we deactivate.
*/
llp_deactivate();
}
/* We should start from fresh. */
clear_lan_entries();
walk_interface(wireless_scan, NULL);
} else if (ret == -1) {
if (errno == EINTR)
continue;
syslog(LOG_INFO, "periodic_wireless_scan: poll failed");
return (NULL);
}
}
/* NOTREACHED */
return (NULL);
}
/*
* Below are functions used to handle storage/retrieval of keys
* for a given WLAN. The keys are stored/retrieved using dladm_set_secobj()
* and dladm_get_secobj().
*/
/*
* Convert key hexascii string to raw secobj value. This
* code is very similar to convert_secobj() in dladm.c, it would
* be good to have a libdladm function to convert values.
*/
static int
key_string_to_secobj_value(char *buf, uint8_t *obj_val, uint_t *obj_lenp,
dladm_secobj_class_t class)
{
size_t buf_len = strlen(buf);
dprintf("before: key_string_to_secobj_value: buf_len = %d", buf_len);
if (buf_len == 0) {
syslog(LOG_ERR,
"key_string_to_secobj_value: empty key");
return (-1);
}
if (buf[buf_len - 1] == '\n')
buf[--buf_len] = '\0';
dprintf("after: key_string_to_secobj_value: buf_len = %d", buf_len);
if (class == DLADM_SECOBJ_CLASS_WPA) {
/*
* Per IEEE802.11i spec, the Pre-shared key (PSK) length should
* be between 8 and 63.
*/
if (buf_len < 8 || buf_len > 63) {
syslog(LOG_ERR,
"key_string_to_secobj_value:"
" invalid WPA key length: buf_len = %d", buf_len);
return (-1);
}
(void) memcpy(obj_val, buf, (uint_t)buf_len);
*obj_lenp = buf_len;
return (0);
}
switch (buf_len) {
case 5: /* ASCII key sizes */
case 13:
(void) memcpy(obj_val, buf, (uint_t)buf_len);
*obj_lenp = (uint_t)buf_len;
break;
case 10:
case 26: /* Hex key sizes, not preceded by 0x */
if (hexascii_to_octet(buf, (uint_t)buf_len, obj_val, obj_lenp)
!= 0) {
syslog(LOG_ERR,
"key_string_to_secobj_value: invalid WEP key");
return (-1);
}
break;
case 12:
case 28: /* Hex key sizes, preceded by 0x */
if (strncmp(buf, "0x", 2) != 0 ||
hexascii_to_octet(buf + 2, (uint_t)buf_len - 2, obj_val,
obj_lenp) != 0) {
syslog(LOG_ERR,
"key_string_to_secobj_value: invalid WEP key");
return (-1);
}
break;
default:
syslog(LOG_ERR,
"key_string_to_secobj_value: invalid WEP key length");
return (-1);
}
return (0);
}
/*
* Print the key format into the appropriate field, then convert any ":"
* characters to ".", as ":[1-4]" is the slot indicator, which otherwise
* would trip us up. The third parameter is expected to be of size
* DLADM_SECOBJ_NAME_MAX.
*/
static void
set_key_name(const char *essid, const char *bssid, char *name, size_t nsz)
{
int i, rtn, len;
if (bssid == NULL)
rtn = snprintf(name, nsz, "nwam-%s", essid);
else
rtn = snprintf(name, nsz, "nwam-%s-%s", essid, bssid);
len = (rtn < nsz) ? rtn : nsz - 1;
for (i = 0; i < len; i++)
if (name[i] == ':')
name[i] = '.';
}
static int
store_key(struct wireless_lan *wlan)
{
uint8_t obj_val[DLADM_SECOBJ_VAL_MAX];
uint_t obj_len = sizeof (obj_val);
char obj_name[DLADM_SECOBJ_NAME_MAX];
dladm_status_t status;
char errmsg[DLADM_STRSIZE];
dladm_secobj_class_t class;
/*
* Name key object for this WLAN so it can be later retrieved
* (name is unique for each ESSID/BSSID combination).
*/
set_key_name(wlan->essid, wlan->bssid, obj_name, sizeof (obj_name));
dprintf("store_key: obj_name is %s", obj_name);
class = (wlan->sec_mode == DLADM_WLAN_SECMODE_WEP ?
DLADM_SECOBJ_CLASS_WEP : DLADM_SECOBJ_CLASS_WPA);
if (key_string_to_secobj_value(wlan->raw_key, obj_val, &obj_len,
class) != 0) {
/* above function logs internally on failure */
return (-1);
}
status = dladm_set_secobj(obj_name, class,
obj_val, obj_len,
DLADM_OPT_CREATE | DLADM_OPT_PERSIST | DLADM_OPT_ACTIVE);
if (status != DLADM_STATUS_OK) {
syslog(LOG_ERR, "store_key: could not create secure object "
"'%s' for key: %s", obj_name,
dladm_status2str(status, errmsg));
return (-1);
}
/*
* We don't really need to retrieve the key we just stored, but
* we do need to set the cooked key, and the function below takes
* care of allocating memory and setting the length and slot ID
* besides just copying the value, so it is simpler just to call
* the retrieve function instead of doing it all here.
*
* Since we just stored the key, retrieve_key() "shouldn't"
* fail. If it does fail, it's not the end of the world; a NULL
* value for wlan->cooked_key simply means this particular
* attempt to connect will fail, and alternative connection
* options will be used.
*/
wlan->cooked_key = retrieve_key(wlan->essid, wlan->bssid, class);
return (0);
}
/*
* retrieve_key returns NULL if no key was recovered from libdladm
*/
static dladm_wlan_key_t *
retrieve_key(const char *essid, const char *bssid, dladm_secobj_class_t req)
{
dladm_status_t status;
char errmsg[DLADM_STRSIZE];
dladm_wlan_key_t *cooked_key;
dladm_secobj_class_t class;
/*
* Newly-allocated key must be freed by caller, or by
* subsequent call to retrieve_key().
*/
if ((cooked_key = malloc(sizeof (dladm_wlan_key_t))) == NULL) {
syslog(LOG_ERR, "retrieve_key: malloc failed");
return (NULL);
}
/* Set name appropriately to retrieve key for this WLAN */
set_key_name(essid, bssid, cooked_key->wk_name,
DLADM_SECOBJ_NAME_MAX);
dprintf("retrieve_key: len = %d, object = %s\n",
strlen(cooked_key->wk_name), cooked_key->wk_name);
cooked_key->wk_len = DLADM_SECOBJ_NAME_MAX;
cooked_key->wk_idx = 1;
/* Try the kernel first, then fall back to persistent storage. */
status = dladm_get_secobj(cooked_key->wk_name, &class,
cooked_key->wk_val, &cooked_key->wk_len,
DLADM_OPT_ACTIVE);
if (status != DLADM_STATUS_OK) {
dprintf("retrieve_key: dladm_get_secobj(TEMP) failed: %s",
dladm_status2str(status, errmsg));
status = dladm_get_secobj(cooked_key->wk_name, &class,
cooked_key->wk_val, &cooked_key->wk_len,
DLADM_OPT_PERSIST);
}
switch (status) {
case DLADM_STATUS_OK:
dprintf("retrieve_key: dladm_get_secobj succeeded: len %d",
cooked_key->wk_len);
break;
case DLADM_STATUS_NOTFOUND:
/*
* We do not want an error in the case that the secobj
* is not found, since we then prompt for it.
*/
free(cooked_key);
return (NULL);
default:
syslog(LOG_ERR, "retrieve_key: could not get key "
"from secure object '%s': %s", cooked_key->wk_name,
dladm_status2str(status, errmsg));
free(cooked_key);
return (NULL);
}
if (class != req) { /* the key mismatch */
syslog(LOG_ERR, "retrieve_key: key type mismatch"
" from secure object '%s'", cooked_key->wk_name);
free(cooked_key);
return (NULL);
}
return (cooked_key);
}
/* Create the KNOWN_WIFI_NETS using info from the interface list. */
void
create_known_wifi_nets_file(void)
{
FILE *fp;
int dirmode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
/* Create the NWAM directory in case it does not exist. */
if (mkdir(LLPDIR, dirmode) != 0) {
if (errno != EEXIST) {
syslog(LOG_ERR, "could not create %s: %m", LLPDIR);
return;
}
}
if ((fp = fopen(KNOWN_WIFI_NETS, "a+")) == NULL) {
syslog(LOG_ERR, "could not open %s: %m", KNOWN_WIFI_NETS);
return;
}
dprintf("Creating %s", KNOWN_WIFI_NETS);
(void) fclose(fp);
}
/*
* Add an entry to known_wifi_nets file given the parameters.
*/
void
update_known_wifi_nets_file(const char *essid, const char *bssid)
{
FILE *fp;
dprintf("update_known_wifi_nets_file(%s, %s)", essid, STRING(bssid));
fp = fopen(KNOWN_WIFI_NETS, "a+");
if (fp == NULL) {
if (errno != ENOENT) {
syslog(LOG_ERR, "fopen(%s) failed: %m",
KNOWN_WIFI_NETS);
return;
}
/*
* If there is none, we should create one instead.
* For now, we will use the order of seeing each new net
* for the priority. We should have a priority field
* in the known_wifi_nets file eventually...
*/
create_known_wifi_nets_file();
fp = fopen(KNOWN_WIFI_NETS, "a");
if (fp == NULL) {
syslog(LOG_ERR, "second fopen(%s) failed: %m",
KNOWN_WIFI_NETS);
return;
}
}
/* now see if this info is already in the file */
if (!known_wifi_nets_lookup(essid, bssid, NULL)) {
/* now add this to the file */
(void) fprintf(fp, "%s\t%s\n", essid,
bssid == NULL ? "" : bssid);
}
(void) fclose(fp);
}
/*
* Check if the given AP (ESSID, BSSID pair) is on the known AP list.
* If found_essid is non-NULL and the match is found (B_TRUE is returned)
* the matched ESSID is copied out into buffer pointed by found_essid.
* The buffer is expected to be at least DLADM_STRSIZE bytes long.
*/
static boolean_t
known_wifi_nets_lookup(const char *new_essid, const char *new_bssid,
char *found_essid)
{
FILE *fp;
char line[LINE_MAX];
char *cp;
char *tok[MAX_FIELDS];
int line_num;
boolean_t found = B_FALSE;
/*
* For now the file format is:
* essid\tbssid
* (essid followed by tab followed by bssid)
*/
fp = fopen(KNOWN_WIFI_NETS, "r+");
if (fp == NULL) {
if (errno != ENOENT) {
syslog(LOG_ERR, "fopen(%s) failed: %m",
KNOWN_WIFI_NETS);
return (B_FALSE);
}
create_known_wifi_nets_file();
return (B_FALSE);
}
for (line_num = 1; fgets(line, sizeof (line), fp) != NULL; line_num++) {
cp = line;
while (isspace(*cp))
cp++;
if (*cp == '#' || *cp == '\0')
continue;
if (bufsplit(cp, MAX_FIELDS, tok) != MAX_FIELDS) {
syslog(LOG_ERR, "%s:%d: wrong number of tokens; "
"ignoring entry", KNOWN_WIFI_NETS, line_num);
continue;
}
/*
* If BSSID match is found we check ESSID, which should
* either match as well, or be an empty string.
* In latter case we'll retrieve the ESSID from known_wifi_nets
* later.
*/
if (strcmp(tok[BSSID], new_bssid) == 0) {
/*
* Got BSSID match, either ESSID was not specified,
* or it should match
*/
if (*new_essid == '\0' ||
strcmp(tok[ESSID], new_essid) == 0) {
found = B_TRUE;
break;
}
}
}
if (found) {
if (found_essid != NULL)
(void) strlcpy(found_essid, tok[ESSID], DLADM_STRSIZE);
}
(void) fclose(fp);
return (found);
}
/*
* reqlan->essid is required (i.e., cannot be NULL)
* reqlan->bssid is optional (i.e., may be NULL)
*/
boolean_t
connect_chosen_lan(struct wireless_lan *reqlan, struct interface *intf)
{
uint_t keycount;
dladm_wlan_key_t *key;
dladm_wlan_attr_t attr;
dladm_status_t status;
uint_t flags = DLADM_WLAN_CONNECT_NOSCAN;
int timeout = DLADM_WLAN_CONNECT_TIMEOUT_DEFAULT;
char errmsg[DLADM_STRSIZE];
(void) memset(&attr, 0, sizeof (attr));
/* try to apply essid selected by the user */
if (reqlan->essid == NULL)
return (B_FALSE);
dprintf("connect_chosen_lan(%s, %s, %s)", reqlan->essid,
STRING(reqlan->bssid), intf->if_name);
/* If it is already connected to the required AP, just return. */
if (check_wlan(intf, reqlan->essid))
return (B_TRUE);
if (dladm_wlan_str2essid(reqlan->essid, &attr.wa_essid) !=
DLADM_STATUS_OK) {
syslog(LOG_ERR,
"connect_chosen_lan: invalid ESSID '%s' for '%s'",
reqlan->essid, intf->if_name);
return (B_FALSE);
}
attr.wa_valid = DLADM_WLAN_ATTR_ESSID;
if (reqlan->bssid != NULL) {
if (dladm_wlan_str2bssid(reqlan->bssid, &attr.wa_bssid) !=
DLADM_STATUS_OK) {
syslog(LOG_ERR,
"connect_chosen_lan: invalid BSSID '%s' for '%s'",
reqlan->bssid, intf->if_name);
return (B_FALSE);
}
attr.wa_valid |= DLADM_WLAN_ATTR_BSSID;
}
/* First check for the key */
if (NEED_ENC(reqlan->sec_mode)) {
get_user_key(reqlan);
if (reqlan->cooked_key == NULL)
return (B_FALSE);
attr.wa_valid |= DLADM_WLAN_ATTR_SECMODE;
attr.wa_secmode = reqlan->sec_mode;
key = reqlan->cooked_key;
keycount = 1;
dprintf("connect_chosen_lan: retrieved key");
} else {
key = NULL;
keycount = 0;
}
/*
* Connect; only scan if a bssid was not specified.
* If it times out and we were trying with a bssid,
* try a second time with just the ESSID.
*/
status = dladm_wlan_connect(intf->if_linkid, &attr, timeout, key,
keycount, flags);
dprintf("connect_chosen_lan: dladm_wlan_connect returned %s",
dladm_status2str(status, errmsg));
if (status == DLADM_STATUS_TIMEDOUT && reqlan->bssid != NULL) {
syslog(LOG_INFO, "connect_chosen_lan: failed for (%s, %s), "
"trying again with just (%s)",
reqlan->essid, reqlan->bssid, reqlan->essid);
attr.wa_valid &= ~DLADM_WLAN_ATTR_BSSID;
flags = 0;
status = dladm_wlan_connect(intf->if_linkid, &attr, timeout,
key, keycount, flags);
}
if (status != DLADM_STATUS_OK) {
syslog(LOG_ERR,
"connect_chosen_lan: connect to '%s' failed on '%s': %s",
reqlan->essid, intf->if_name,
dladm_status2str(status, errmsg));
return (B_FALSE);
}
return (B_TRUE);
}
/*
* First attempt to connect to the network specified by essid.
* If that fails, attempt to connect using autoconf.
*/
static boolean_t
connect_or_autoconf(struct wireless_lan *reqlan, struct interface *intf)
{
if (!connect_chosen_lan(reqlan, intf)) {
syslog(LOG_WARNING,
"Could not connect to chosen WLAN %s, going to auto-conf",
reqlan->essid);
return (wlan_autoconf(intf));
}
return (B_TRUE);
}
/*
* The +1 is for the extra "compare" row, the 24 is for the font spacing
* and the 125 if extra for the buttons et al.
*/
static int
zenity_height(int rows)
{
return (((rows + 1) * 24) + 125);
}
/*
* Construct an arg vector for zenity which displays a list of wlans.
* Additional options may be added at the end of the list of wlans with
* the "extra_rows" arg.
*
* Parameters include:
* lanlist: a linked list of wlans; one row per list node.
* nlans: the number of nodes in lanlist.
* title: the string that should be the zenity window title.
* width: the width of the zenity window.
* extra_rows: pointer to an array of strings; each string will
* appear on its own row in the table, in the first column
* of that row.
* nrows: the number of extra row strings.
*
* A pointer to the arg vector is returned; the caller must free that
* memory using free_argv().
*/
static char **
build_wlanlist_zargv(const struct wireless_lan *lanlist, int nlans,
const char *title, int width, const char **extra_rows, int nrows)
{
int cur, i, j;
int zargc, hdrargc, wlanargc;
char **zargv;
/*
* There are three sections of arguments: the initial args, specifying
* general formatting info; the column titles (one arg per column);
* and the row data (one row per wlan, plus any extra rows; and one
* arg per column per row).
*/
hdrargc = ZENITY_LIST_INIT_ARGS + ZENITY_COLUMNS_PER_WLAN;
wlanargc = (nlans + nrows) * ZENITY_COLUMNS_PER_WLAN;
zargc = hdrargc + wlanargc;
if ((zargv = alloc_argv(zargc, ZENITY_ARG_LEN)) == NULL)
return (NULL);
/* initial args */
cur = 0;
(void) snprintf(zargv[cur++], ZENITY_ARG_LEN, ZENITY);
(void) snprintf(zargv[cur++], ZENITY_ARG_LEN, "--list");
(void) snprintf(zargv[cur++], ZENITY_ARG_LEN, "--title=%s", title);
(void) snprintf(zargv[cur++], ZENITY_ARG_LEN, "--height=%d",
zenity_height(nlans + nrows));
(void) snprintf(zargv[cur++], ZENITY_ARG_LEN, "--width=%d", width);
/* column titles */
(void) snprintf(zargv[cur++], ZENITY_ARG_LEN, "--column=#");
(void) snprintf(zargv[cur++], ZENITY_ARG_LEN, "--column=ESSID");
(void) snprintf(zargv[cur++], ZENITY_ARG_LEN, "--column=BSSID");
(void) snprintf(zargv[cur++], ZENITY_ARG_LEN, "--column=%s",
gettext("Encryption"));
(void) snprintf(zargv[cur++], ZENITY_ARG_LEN, "--column=%s",
gettext("Signal"));
/* wlan rows */
for (i = 0; i < nlans; i++) {
(void) snprintf(zargv[cur++], ZENITY_ARG_LEN, "%d", i + 1);
(void) snprintf(zargv[cur++], ZENITY_ARG_LEN, "%s",
lanlist[i].essid);
(void) snprintf(zargv[cur++], ZENITY_ARG_LEN, "%s",
lanlist[i].bssid);
(void) snprintf(zargv[cur++], ZENITY_ARG_LEN, "%s",
WLAN_ENC(lanlist[i].sec_mode));
(void) snprintf(zargv[cur++], ZENITY_ARG_LEN, "%s",
lanlist[i].signal_strength);
}
/* extra rows */
for (i = 0; i < nrows; i++) {
/* all columns are empty except the first */
(void) snprintf(zargv[cur++], ZENITY_ARG_LEN, extra_rows[i]);
for (j = 0; j < ZENITY_COLUMNS_PER_WLAN - 1; j++)
*zargv[cur++] = '\0';
}
return (zargv);
}
static return_vals_t
connect_to_new_wlan(const struct wireless_lan *lanlist, int nlans,
struct interface *intf)
{
int i, dlist_cnt;
int rtn;
char **zargv;
const char *title = gettext("Choose WiFi network you wish to activate");
const char *extra_rows[2];
struct wireless_lan *dlist, *reqlan;
boolean_t autoconf = B_FALSE;
dprintf("connect_to_new_wlan(..., %d, %s)", nlans, intf->if_name);
if (nlans == 0) {
display(gettext("No Wifi networks found; continuing in case "
"you know of any which do not broadcast."));
}
/* build list of wlans to be displayed */
if ((dlist = calloc(nlans, sizeof (struct wireless_lan))) == NULL)
return (FAILURE);
for (i = 0, dlist_cnt = 0; i < nlans; i++) {
if ((lanlist[i].essid == NULL) || (lanlist[i].bssid == NULL)) {
syslog(LOG_WARNING, "wifi list entry %d broken: "
"essid %s, bssid %s; ignoring", i,
STRING(lanlist[i].essid), STRING(lanlist[i].bssid));
continue;
}
/*
* Only use the interface which finds the AP to connect to it.
*/
if (strcmp(lanlist[i].wl_if_name, intf->if_name) != 0) {
dprintf("connect_to_new_wlan: wrong interface (%s) for "
"%s (should be %s)", intf->if_name,
lanlist[i].essid, lanlist[i].wl_if_name);
continue;
}
dlist[dlist_cnt++] = lanlist[i];
}
extra_rows[0] = gettext("Other");
extra_rows[1] = gettext("Rescan");
/* width = 500 is the result of trial-and-error testing */
zargv = build_wlanlist_zargv(dlist, dlist_cnt, title, 500, extra_rows,
sizeof (extra_rows) / sizeof (extra_rows[0]));
if (zargv == NULL) {
free(dlist);
return (FAILURE);
}
/* present list to user and get selection */
rtn = get_user_preference(zargv, extra_rows[0], extra_rows[1],
&reqlan, dlist);
switch (rtn) {
case 1:
/* user chose "other"; pop-up for specific essid */
reqlan = get_specific_lan();
break;
case 2:
/* user chose "Rescan" */
(void) scan_wireless_nets(intf);
rtn = TRY_AGAIN;
goto cleanup;
case -1:
reqlan = NULL;
break;
default:
/* common case: reqlan was set in get_user_preference() */
break;
}
if ((reqlan == NULL) || (reqlan->essid == NULL)) {
dprintf("did not get user preference; attempting autoconf");
rtn = wlan_autoconf(intf) ? SUCCESS : FAILURE;
goto cleanup;
}
dprintf("get_user_preference() returned essid %s, bssid %s, encr %s",
reqlan->essid, STRING(reqlan->bssid),
WLAN_ENC(reqlan->sec_mode));
/* set key before first time connection */
if (NEED_ENC(reqlan->sec_mode) && reqlan->raw_key == NULL &&
reqlan->cooked_key == NULL)
get_user_key(reqlan);
/*
* now attempt to connect to selection, backing
* off to autoconf if the connect fails
*/
if (connect_chosen_lan(reqlan, intf)) {
/*
* Succeeded, so add entry to known_essid_list_file;
* but first make sure the reqlan->bssid isn't empty.
*/
if (reqlan->bssid == NULL) {
dladm_status_t status;
dladm_wlan_linkattr_t attr;
char bssid[DLADM_STRSIZE];
status = dladm_wlan_get_linkattr(intf->if_linkid,
&attr);
if (status == DLADM_STATUS_OK) {
(void) dladm_wlan_bssid2str(
&attr.la_wlan_attr.wa_bssid, bssid);
reqlan->bssid = strdup(bssid);
} else {
dprintf("failed to get linkattr after "
"connecting to %s", reqlan->essid);
}
}
update_known_wifi_nets_file(reqlan->essid, reqlan->bssid);
} else {
/* failed to connect; try auto-conf */
syslog(LOG_WARNING, "Could not connect to chosen WLAN "
"%s; going to auto-conf", reqlan->essid);
autoconf = B_TRUE;
}
free_wireless_lan(reqlan);
if (autoconf)
rtn = wlan_autoconf(intf) ? SUCCESS : FAILURE;
else
rtn = SUCCESS;
cleanup:
free_argv(zargv);
free(dlist);
return (rtn);
}
struct wireless_lan *
prompt_for_visited(void)
{
int dlist_cnt;
char **zargv;
const char *title = gettext("Choose from pre-visited WiFi network");
const char *extra_rows[1];
struct wireless_lan *req_conf, *dlist;
struct visited_wlans *vlp;
struct wireless_lan *wlp;
/* build list of wlans to be displayed */
dlist = calloc(visited_wlan_list->total, sizeof (struct wireless_lan));
if (dlist == NULL) {
syslog(LOG_ERR, "prompt_for_visited: calloc failed");
return (NULL);
}
dlist_cnt = 0;
for (vlp = visited_wlan_list->head; vlp != NULL; vlp = vlp->next) {
wlp = vlp->wifi_net;
if (wlp->essid == NULL || wlp->bssid == NULL) {
dprintf("Invalid essid/bssid values");
continue;
}
dlist[dlist_cnt++] = *wlp;
}
extra_rows[0] = gettext("Select from all available WiFi networks");
/* width = 670 is the result of trial-and-error testing */
zargv = build_wlanlist_zargv(dlist, dlist_cnt, title, 670, extra_rows,
sizeof (extra_rows) / sizeof (extra_rows[0]));
if (zargv == NULL) {
free(dlist);
return (NULL);
}
/*
* If the user doesn't make a choice or something goes wrong
* (get_user_preference() returned -1), or if the user chooses
* the "select from all available" string (get_user_preference()
* returned 1), we simply return NULL: there was no selection
* made from the visited list. If the user *did* make a choice
* (get_user_preference() returned 0), return the alloc'd struct.
*/
if (get_user_preference(zargv, extra_rows[0], NULL, &req_conf,
dlist) != 0)
req_conf = NULL;
free(dlist);
free_argv(zargv);
return (req_conf);
}
static boolean_t
wlan_autoconf(struct interface *intf)
{
dladm_status_t status;
boolean_t autoconf;
if (lookup_boolean_property(OUR_PG, "autoconf", &autoconf) == 0) {
if (!autoconf)
return (B_FALSE);
}
/* If the NIC is already associated with something, just return. */
if (check_wlan(intf, NULL))
return (B_TRUE);
/*
* Do autoconf, relying on the heuristics used by dladm_wlan_connect()
* to cycle through WLANs detected in priority order, attempting
* to connect.
*/
status = dladm_wlan_connect(intf->if_linkid, NULL,
DLADM_WLAN_CONNECT_TIMEOUT_DEFAULT, NULL, 0, 0);
if (status != DLADM_STATUS_OK) {
char errmsg[DLADM_STRSIZE];
syslog(LOG_ERR,
"wlan_autoconf: dladm_wlan_connect failed for '%s': %s",
intf->if_name, dladm_status2str(status, errmsg));
return (B_FALSE);
}
return (B_TRUE);
}
/*
* Returns:
* B_TRUE if this info is already in visited_wlan_list
* B_FALSE if not
*/
static boolean_t
already_in_visited_wlan_list(const struct wireless_lan *new_wlan)
{
struct visited_wlans *vwlp;
vwlp = visited_wlan_list->head;
while (vwlp != NULL && vwlp->wifi_net != NULL) {
if (strcmp(vwlp->wifi_net->essid, new_wlan->essid) == 0) {
dprintf("%s already in visited_wlan_list",
vwlp->wifi_net->essid);
return (B_TRUE);
} else {
vwlp = vwlp->next;
}
}
return (B_FALSE);
}
static char *
get_zenity_response(char *const *zargv)
{
int pfds[2];
pid_t pid;
int status, i;
char inbuf[ZENITY_RTN_BUF_SIZE];
ssize_t n, bytes_read;
char *rtnp = NULL;
if (!valid_graphical_user(B_TRUE))
return (NULL);
if (pipe(pfds) < 0) {
syslog(LOG_ERR, "pipe() failed: %m");
return (NULL);
}
if ((pid = fork()) < 0) {
syslog(LOG_ERR, "fork() failed: %m");
return (NULL);
} else if (pid == 0) {
/*
* child: close read side of pipe, point stdout at write side
*/
(void) close(pfds[0]);
if (dup2(pfds[1], STDOUT_FILENO) < 0) {
syslog(LOG_ERR, "dup2() failed: %m");
_exit(EXIT_FAILURE);
}
(void) close(pfds[1]);
(void) execv(ZENITY, zargv);
syslog(LOG_ERR, "execv() failed: %m");
_exit(EXIT_FAILURE);
} else {
/*
* parent: close write side of pipe, read from read side
* to get zenity output from child.
*/
(void) close(pfds[1]);
(void) waitpid(pid, &status, 0);
if (WIFSIGNALED(status) || WIFSTOPPED(status)) {
i = WIFSIGNALED(status) ? WTERMSIG(status) :
WSTOPSIG(status);
syslog(LOG_ERR, "%s %s with signal %d (%s)",
ZENITY, (WIFSIGNALED(status) ? "terminated" :
"stopped"), i, strsignal(i));
return (NULL);
}
bytes_read = 0;
do {
n = read(pfds[0], inbuf + bytes_read,
sizeof (inbuf) - bytes_read);
if (n < 0) {
if (errno != EINTR) {
syslog(LOG_ERR, "read() failed: %m");
break;
}
} else {
bytes_read += n;
}
if (bytes_read == sizeof (inbuf)) {
bytes_read--;
syslog(LOG_WARNING, "get_zenity_response: too "
"much data; input read will be limited to "
"%d bytes", bytes_read);
break;
}
} while (n != 0);
(void) close(pfds[0]);
if (bytes_read == 0) {
syslog(LOG_ERR, "failed to read zenity output");
return (NULL);
}
if (inbuf[bytes_read - 1] == '\n')
inbuf[bytes_read - 1] = '\0';
else
inbuf[bytes_read] = '\0';
if ((rtnp = strdup(inbuf)) == NULL)
syslog(LOG_ERR, "get_zenity_response: strdup failed");
}
return (rtnp);
}
/*
* get_user_preference(): Present a list of essid/bssid pairs to the
* user via zenity (passed in in a pre-formatted zenity string in the
* param cmd). If there's a final list item ("Other", "Select from
* full list", etc.) that may be selected and should be differentiated,
* that item should be passed in in compare param.
*
* Four possible return values:
* -1: No response from user, or other error. *req_lan is undefined.
* 0: essid/bssid pair was selected. *req_lan has these values in
* a malloc'd buffer, which the caller is responsible for freeing.
* 1: a compare string ("Other") was given, and the user response matched
* that string. *req_lan is undefined.
* 2: a compare string ("Rescan") was given, and the user response matched
* that string. *req_lan is undefined.
*/
static int
get_user_preference(char *const *zargv, const char *compare_other,
const char *compare_rescan, struct wireless_lan **req_lan,
const struct wireless_lan *list)
{
char *response;
struct wireless_lan *wlp;
const struct wireless_lan *sel;
int answer;
assert(req_lan != NULL);
wlp = calloc(1, sizeof (struct wireless_lan));
if (wlp == NULL) {
syslog(LOG_ERR, "calloc failed");
return (-1);
}
response = get_zenity_response(zargv);
if (response == NULL) {
free(wlp);
return (-1);
}
if (compare_other != NULL && strcmp(response, compare_other) == 0) {
free(response);
free(wlp);
return (1);
}
if (compare_rescan != NULL && strcmp(response, compare_rescan) == 0) {
free(response);
free(wlp);
return (2);
}
answer = atoi(response);
if (answer <= 0) {
dprintf("%s returned invalid string", ZENITY);
free(response);
free(wlp);
return (-1);
}
sel = &list[answer - 1];
if ((wlp->essid = strdup(sel->essid)) == NULL)
goto dup_error;
if ((sel->bssid != NULL) && ((wlp->bssid = strdup(sel->bssid)) == NULL))
goto dup_error;
wlp->sec_mode = sel->sec_mode;
if ((sel->raw_key != NULL) &&
((wlp->raw_key = strdup(sel->raw_key)) == NULL))
goto dup_error;
if (sel->cooked_key != NULL) {
wlp->cooked_key = malloc(sizeof (dladm_wlan_key_t));
if (wlp->cooked_key == NULL)
goto dup_error;
*(wlp->cooked_key) = *(sel->cooked_key);
}
if ((sel->signal_strength != NULL) &&
((wlp->signal_strength = strdup(sel->signal_strength)) == NULL))
goto dup_error;
if ((sel->wl_if_name != NULL) &&
((wlp->wl_if_name = strdup(sel->wl_if_name)) == NULL))
goto dup_error;
dprintf("selected: %s, %s, %s, '%s', %s", wlp->essid,
STRING(wlp->bssid), WLAN_ENC(wlp->sec_mode),
STRING(wlp->signal_strength), STRING(wlp->wl_if_name));
free(response);
*req_lan = wlp;
return (0);
dup_error:
syslog(LOG_ERR, "get_user_preference: strdup failed");
free_wireless_lan(wlp);
free(wlp);
free(response);
return (-1);
}
/*
* Returns a pointer to an alloc'd struct wireless lan if a response
* is received from the user; only the essid will be valid in this
* case. If no response received, or other failure, returns NULL.
*/
static struct wireless_lan *
get_specific_lan(void)
{
int zargc, cur;
char **zargv;
char *response;
struct wireless_lan *wlp = NULL;
/*
* build zenity 'entry' argv to get an ESSID:
* 'zenity --entry --title=foo --text=bar'
* Four args needed.
*/
zargc = 4;
if ((zargv = alloc_argv(zargc, ZENITY_ARG_LEN)) == NULL)
return (NULL);
cur = 0;
(void) snprintf(zargv[cur++], ZENITY_ARG_LEN, ZENITY);
(void) snprintf(zargv[cur++], ZENITY_ARG_LEN, "--entry");
(void) snprintf(zargv[cur++], ZENITY_ARG_LEN, "--title=%s",
gettext("Specify WiFi Network"));
(void) snprintf(zargv[cur++], ZENITY_ARG_LEN, "--text=%s %s",
gettext("Enter"), "ESSID");
response = get_zenity_response(zargv);
if (response == NULL)
goto cleanup;
free_argv(zargv);
wlp = calloc(1, sizeof (struct wireless_lan));
if (wlp == NULL) {
syslog(LOG_ERR, "calloc failed: %m");
goto cleanup;
}
wlp->essid = response;
wlp->sec_mode = DLADM_WLAN_SECMODE_NONE;
/*
* build zenity 'list' argv to get security mode:
* 'zenity --list --title=foo --text=bar --column=baz <3 modes>'
* Eight args needed.
*/
zargc = 8;
if ((zargv = alloc_argv(zargc, ZENITY_ARG_LEN)) == NULL) {
/* assume the default security mode, "none" */
return (wlp);
}
cur = 0;
(void) snprintf(zargv[cur++], ZENITY_ARG_LEN, ZENITY);
(void) snprintf(zargv[cur++], ZENITY_ARG_LEN, "--list");
(void) snprintf(zargv[cur++], ZENITY_ARG_LEN, "--title=%s",
gettext("Security"));
(void) snprintf(zargv[cur++], ZENITY_ARG_LEN, "--text=%s",
gettext("Enter security mode"));
(void) snprintf(zargv[cur++], ZENITY_ARG_LEN, "--column=%s",
gettext("Type"));
(void) snprintf(zargv[cur++], ZENITY_ARG_LEN, "None");
(void) snprintf(zargv[cur++], ZENITY_ARG_LEN, "WEP");
(void) snprintf(zargv[cur++], ZENITY_ARG_LEN, "WPA");
response = get_zenity_response(zargv);
/* "none" was set as the default earlier */
if (response != NULL) {
if (strcmp(response, "WEP") == 0)
wlp->sec_mode = DLADM_WLAN_SECMODE_WEP;
else if (strcmp(response, "WPA") == 0)
wlp->sec_mode = DLADM_WLAN_SECMODE_WPA;
}
cleanup:
free_argv(zargv);
free(response);
return (wlp);
}
/*
* Returns:
* B_TRUE if things go well
* B_FALSE if we were unable to connect to anything
*/
boolean_t
handle_wireless_lan(struct interface *intf)
{
const struct wireless_lan *cur_wlans;
int i, num_wlans;
struct wireless_lan *req_conf = NULL;
boolean_t result;
dladm_wlan_strength_t strongest = DLADM_WLAN_STRENGTH_VERY_WEAK;
dladm_wlan_strength_t strength;
return_vals_t connect_result;
start_over:
if (visited_wlan_list == NULL) {
if ((visited_wlan_list = calloc(1,
sizeof (struct visited_wlans_list))) == NULL) {
syslog(LOG_ERR, "handle_wireless_lan: calloc failed");
return (B_FALSE);
}
}
/*
* We wait while a scan is in progress. Since we allow a user
* to initiate a re-scan, we can proceed even when no scan
* has been done to fill in the AP list.
*/
(void) pthread_mutex_lock(&wifi_init_mutex);
while (wifi_scan_intf != NULL)
(void) pthread_cond_wait(&wifi_init_cond, &wifi_init_mutex);
(void) pthread_mutex_unlock(&wifi_init_mutex);
(void) pthread_mutex_lock(&wifi_mutex);
num_wlans = wireless_lan_used;
cur_wlans = wlans;
/*
* Try to see if any of the wifi nets currently available
* has been used previously. If more than one available
* nets has been used before, then prompt user with
* all the applicable previously wifi nets, and ask which
* one to connect to.
*/
for (i = 0; i < num_wlans; i++) {
struct visited_wlans *new_wlan;
/* Find the AP with the highest signal. */
if (dladm_wlan_str2strength(cur_wlans[i].signal_strength,
&strength) != DLADM_STATUS_OK) {
continue;
}
if (strength > strongest)
strongest = strength;
if (!known_wifi_nets_lookup(cur_wlans[i].essid,
cur_wlans[i].bssid, NULL))
continue;
if (already_in_visited_wlan_list(&cur_wlans[i])) {
/* don't have to add it again */
continue;
}
/* add this to the visited_wlan_list */
dprintf("adding essid %s, bssid %s to visited list",
cur_wlans[i].essid, STRING(cur_wlans[i].bssid));
new_wlan = calloc(1, sizeof (struct visited_wlans));
if (new_wlan == NULL) {
syslog(LOG_ERR, "handle_wireless_lan: calloc failed");
result = B_FALSE;
connect_result = FAILURE;
goto all_done;
}
new_wlan->wifi_net = calloc(1, sizeof (struct wireless_lan));
if (new_wlan->wifi_net == NULL) {
free(new_wlan);
syslog(LOG_ERR, "handle_wireless_lan: calloc failed");
result = B_FALSE;
connect_result = FAILURE;
goto all_done;
}
new_wlan->wifi_net->essid = strdup(cur_wlans[i].essid);
new_wlan->wifi_net->bssid = strdup(cur_wlans[i].bssid);
new_wlan->wifi_net->raw_key = NULL;
new_wlan->wifi_net->cooked_key = NULL;
new_wlan->wifi_net->sec_mode = cur_wlans[i].sec_mode;
new_wlan->wifi_net->signal_strength =
strdup(cur_wlans[i].signal_strength);
new_wlan->wifi_net->wl_if_name =
strdup(cur_wlans[i].wl_if_name);
if (new_wlan->wifi_net->essid == NULL ||
new_wlan->wifi_net->bssid == NULL ||
new_wlan->wifi_net->signal_strength == NULL ||
new_wlan->wifi_net->wl_if_name == NULL) {
syslog(LOG_ERR, "handle_wireless_lan: strdup failed");
free_wireless_lan(new_wlan->wifi_net);
free(new_wlan->wifi_net);
free(new_wlan);
result = B_FALSE;
connect_result = FAILURE;
goto all_done;
}
new_wlan->next = visited_wlan_list->head;
visited_wlan_list->head = new_wlan;
visited_wlan_list->total++;
}
if (visited_wlan_list->total == 1) {
struct wireless_lan *target = visited_wlan_list->head->wifi_net;
/*
* only one previously visited wifi net, connect to it
* (falling back to autoconf if the connect fails) if there
* is no AP with a better signal strength.
*/
if (dladm_wlan_str2strength(target->signal_strength,
&strength) == DLADM_STATUS_OK) {
if (strength < strongest)
goto connect_any;
}
result = connect_or_autoconf(target, intf);
connect_result = result ? SUCCESS : FAILURE;
} else if (visited_wlan_list->total > 1) {
/*
* more than one previously visited wifi nets seen.
* prompt user for which one should we connect to
*/
if ((req_conf = prompt_for_visited()) != NULL) {
result = connect_or_autoconf(req_conf, intf);
connect_result = result ? SUCCESS : FAILURE;
} else {
/*
* The user didn't make a choice; offer the full list.
*/
connect_result = connect_to_new_wlan(cur_wlans,
num_wlans, intf);
result = (connect_result == SUCCESS);
}
} else {
connect_any:
/* last case, no previously visited wlan found */
connect_result = connect_to_new_wlan(cur_wlans, num_wlans,
intf);
result = (connect_result == SUCCESS);
}
all_done:
/*
* We locked down the list above; free it now that we're done.
*/
(void) pthread_mutex_unlock(&wifi_mutex);
if (visited_wlan_list != NULL) {
struct visited_wlans *vwlp = visited_wlan_list->head, *next;
for (; vwlp != NULL; vwlp = next) {
if (vwlp->wifi_net != NULL) {
free_wireless_lan(vwlp->wifi_net);
free(vwlp->wifi_net);
vwlp->wifi_net = NULL;
}
next = vwlp->next;
free(vwlp);
}
free(visited_wlan_list);
visited_wlan_list = NULL;
}
if (req_conf != NULL) {
free_wireless_lan(req_conf);
free(req_conf);
req_conf = NULL;
}
if (connect_result == TRY_AGAIN) {
dprintf("end of handle_wireless_lan() TRY_AGAIN");
goto start_over;
}
return (result);
}
|