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
|
/*
* Copyright 2008, The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <dirent.h>
#include <sys/socket.h>
#include <unistd.h>
#include <poll.h>
#ifdef USES_TI_MAC80211
#include <dirent.h>
#include <net/if.h>
#include <netlink/genl/genl.h>
#include <netlink/genl/family.h>
#include <netlink/genl/ctrl.h>
#include <netlink/msg.h>
#include <netlink/attr.h>
#include "nl80211.h"
#endif
#include "hardware_legacy/wifi.h"
#include "libwpa_client/wpa_ctrl.h"
#define LOG_TAG "WifiHW"
#include "cutils/log.h"
#include "cutils/memory.h"
#include "cutils/misc.h"
#include "cutils/properties.h"
#include "private/android_filesystem_config.h"
#ifdef HAVE_LIBC_SYSTEM_PROPERTIES
#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
#include <sys/_system_properties.h>
#endif
/* PRIMARY refers to the connection on the primary interface
* SECONDARY refers to an optional connection on a p2p interface
*
* For concurrency, we only support one active p2p connection and
* one active STA connection at a time
*/
#define PRIMARY 0
#define SECONDARY 1
#define MAX_CONNS 2
static struct wpa_ctrl *ctrl_conn[MAX_CONNS];
static struct wpa_ctrl *monitor_conn[MAX_CONNS];
/* socket pair used to exit from a blocking read */
static int exit_sockets[MAX_CONNS][2];
extern int do_dhcp();
extern int ifc_init();
extern void ifc_close();
extern char *dhcp_lasterror();
extern void get_dhcp_info();
extern int init_module(void *, unsigned long, const char *);
extern int delete_module(const char *, unsigned int);
static int wifi_mode = 0;
static char primary_iface[PROPERTY_VALUE_MAX];
// TODO: use new ANDROID_SOCKET mechanism, once support for multiple
// sockets is in
#ifdef USES_TI_MAC80211
#define P2P_INTERFACE "p2p0"
struct nl_sock *nl_soc;
struct nl_cache *nl_cache;
struct genl_family *nl80211;
#endif
#ifndef WIFI_DRIVER_MODULE_ARG
#define WIFI_DRIVER_MODULE_ARG ""
#endif
#ifndef WIFI_DRIVER_MODULE_AP_ARG
#define WIFI_DRIVER_MODULE_AP_ARG ""
#endif
#ifndef WIFI_FIRMWARE_LOADER
#define WIFI_FIRMWARE_LOADER ""
#endif
#define WIFI_TEST_INTERFACE "sta"
#ifndef WIFI_DRIVER_FW_PATH_STA
#define WIFI_DRIVER_FW_PATH_STA NULL
#endif
#ifndef WIFI_DRIVER_FW_PATH_AP
#define WIFI_DRIVER_FW_PATH_AP NULL
#endif
#ifndef WIFI_DRIVER_FW_PATH_P2P
#define WIFI_DRIVER_FW_PATH_P2P NULL
#endif
#ifndef WIFI_DRIVER_FW_PATH_PARAM
#define WIFI_DRIVER_FW_PATH_PARAM "/sys/module/wlan/parameters/fwpath"
#endif
static const char IFACE_DIR[] = "/data/system/wpa_supplicant";
#ifdef WIFI_DRIVER_MODULE_PATH
static const char DRIVER_MODULE_NAME[] = WIFI_DRIVER_MODULE_NAME;
static const char DRIVER_MODULE_TAG[] = WIFI_DRIVER_MODULE_NAME " ";
static const char DRIVER_MODULE_PATH[] = WIFI_DRIVER_MODULE_PATH;
static const char DRIVER_MODULE_ARG[] = WIFI_DRIVER_MODULE_ARG;
static const char DRIVER_MODULE_AP_ARG[] = WIFI_DRIVER_MODULE_AP_ARG;
#endif
static const char FIRMWARE_LOADER[] = WIFI_FIRMWARE_LOADER;
static const char DRIVER_PROP_NAME[] = "wlan.driver.status";
static const char SUPPLICANT_NAME[] = "wpa_supplicant";
static const char SUPP_PROP_NAME[] = "init.svc.wpa_supplicant";
static const char P2P_SUPPLICANT_NAME[] = "p2p_supplicant";
static const char P2P_PROP_NAME[] = "init.svc.p2p_supplicant";
static const char SUPP_CONFIG_TEMPLATE[]= "/system/etc/wifi/wpa_supplicant.conf";
static const char SUPP_CONFIG_FILE[] = "/data/misc/wifi/wpa_supplicant.conf";
static const char P2P_CONFIG_FILE[] = "/data/misc/wifi/p2p_supplicant.conf";
static const char CONTROL_IFACE_PATH[] = "/data/misc/wifi/sockets";
static const char MODULE_FILE[] = "/proc/modules";
static const char SUPP_ENTROPY_FILE[] = WIFI_ENTROPY_FILE;
static unsigned char dummy_key[21] = { 0x02, 0x11, 0xbe, 0x33, 0x43, 0x35,
0x68, 0x47, 0x84, 0x99, 0xa9, 0x2b,
0x1c, 0xd3, 0xee, 0xff, 0xf1, 0xe2,
0xf3, 0xf4, 0xf5 };
/* Is either SUPPLICANT_NAME or P2P_SUPPLICANT_NAME */
static char supplicant_name[PROPERTY_VALUE_MAX];
/* Is either SUPP_PROP_NAME or P2P_PROP_NAME */
static char supplicant_prop_name[PROPERTY_KEY_MAX];
static int is_primary_interface(const char *ifname)
{
//Treat NULL as primary interface to allow control
//on STA without an interface
if (ifname == NULL || !strncmp(ifname, primary_iface, strlen(primary_iface))) {
return 1;
}
return 0;
}
#ifdef SAMSUNG_WIFI
char* get_samsung_wifi_type()
{
char buf[10];
int fd = open("/data/.cid.info", O_RDONLY);
if (fd < 0)
return NULL;
if (read(fd, buf, sizeof(buf)) < 0) {
close(fd);
return NULL;
}
close(fd);
if (strncmp(buf, "murata", 6) == 0)
return "_murata";
if (strncmp(buf, "semcove", 7) == 0)
return "_semcove";
return NULL;
}
#endif
static int insmod(const char *filename, const char *args)
{
void *module;
unsigned int size;
int ret;
module = load_file(filename, &size);
if (!module)
return -1;
ret = init_module(module, size, args);
free(module);
return ret;
}
static int rmmod(const char *modname)
{
int ret = -1;
int maxtry = 10;
while (maxtry-- > 0) {
ret = delete_module(modname, O_NONBLOCK | O_EXCL);
if (ret < 0 && errno == EAGAIN)
usleep(500000);
else
break;
}
if (ret != 0)
ALOGD("Unable to unload driver module \"%s\": %s\n",
modname, strerror(errno));
return ret;
}
int do_dhcp_request(int *ipaddr, int *gateway, int *mask,
int *dns1, int *dns2, int *server, int *lease) {
/* For test driver, always report success */
if (strcmp(primary_iface, WIFI_TEST_INTERFACE) == 0)
return 0;
if (ifc_init() < 0)
return -1;
if (do_dhcp(primary_iface) < 0) {
ifc_close();
return -1;
}
ifc_close();
get_dhcp_info(ipaddr, gateway, mask, dns1, dns2, server, lease);
return 0;
}
const char *get_dhcp_error_string() {
return dhcp_lasterror();
}
int is_wifi_driver_loaded() {
char driver_status[PROPERTY_VALUE_MAX];
#ifdef WIFI_DRIVER_MODULE_PATH
FILE *proc;
char line[sizeof(DRIVER_MODULE_TAG)+10];
#endif
if (!property_get(DRIVER_PROP_NAME, driver_status, NULL)
|| strcmp(driver_status, "ok") != 0) {
return 0; /* driver not loaded */
}
#ifdef WIFI_DRIVER_MODULE_PATH
/*
* If the property says the driver is loaded, check to
* make sure that the property setting isn't just left
* over from a previous manual shutdown or a runtime
* crash.
*/
if ((proc = fopen(MODULE_FILE, "r")) == NULL) {
ALOGW("Could not open %s: %s", MODULE_FILE, strerror(errno));
property_set(DRIVER_PROP_NAME, "unloaded");
return 0;
}
while ((fgets(line, sizeof(line), proc)) != NULL) {
if (strncmp(line, DRIVER_MODULE_TAG, strlen(DRIVER_MODULE_TAG)) == 0) {
fclose(proc);
return 1;
}
}
fclose(proc);
property_set(DRIVER_PROP_NAME, "unloaded");
return 0;
#else
return 1;
#endif
}
int wifi_load_driver()
{
#ifdef WIFI_DRIVER_MODULE_PATH
char driver_status[PROPERTY_VALUE_MAX];
int count = 100; /* wait at most 20 seconds for completion */
char module_arg2[256];
#ifdef SAMSUNG_WIFI
char* type = get_samsung_wifi_type();
if (wifi_mode == 1) {
snprintf(module_arg2, sizeof(module_arg2), "%s%s", DRIVER_MODULE_AP_ARG, type == NULL ? "" : type);
} else {
snprintf(module_arg2, sizeof(module_arg2), "%s%s", DRIVER_MODULE_ARG, type == NULL ? "" : type);
}
if (insmod(DRIVER_MODULE_PATH, module_arg2) < 0) {
#else
if (insmod(DRIVER_MODULE_PATH, DRIVER_MODULE_ARG) < 0) {
#endif
#ifdef WIFI_EXT_MODULE_NAME
rmmod(EXT_MODULE_NAME);
#endif
return -1;
}
if (strcmp(FIRMWARE_LOADER,"") == 0) {
#ifdef WIFI_DRIVER_LOADER_DELAY
usleep(WIFI_DRIVER_LOADER_DELAY);
#endif
property_set(DRIVER_PROP_NAME, "ok");
}
else {
property_set("ctl.start", FIRMWARE_LOADER);
}
sched_yield();
while (count-- > 0) {
if (property_get(DRIVER_PROP_NAME, driver_status, NULL)) {
if (strcmp(driver_status, "ok") == 0)
return 0;
else if (strcmp(DRIVER_PROP_NAME, "failed") == 0) {
wifi_unload_driver();
return -1;
}
}
usleep(200000);
}
property_set(DRIVER_PROP_NAME, "timeout");
wifi_unload_driver();
return -1;
#else
property_set(DRIVER_PROP_NAME, "ok");
return 0;
#endif
}
int wifi_unload_driver()
{
usleep(200000); /* allow to finish interface down */
#ifdef WIFI_DRIVER_MODULE_PATH
if (rmmod(DRIVER_MODULE_NAME) == 0) {
int count = 20; /* wait at most 10 seconds for completion */
while (count-- > 0) {
if (!is_wifi_driver_loaded())
break;
usleep(500000);
}
usleep(500000); /* allow card removal */
if (count) {
return 0;
}
return -1;
} else
return -1;
#else
property_set(DRIVER_PROP_NAME, "unloaded");
return 0;
#endif
}
int ensure_entropy_file_exists()
{
int ret;
int destfd;
ret = access(SUPP_ENTROPY_FILE, R_OK|W_OK);
if ((ret == 0) || (errno == EACCES)) {
if ((ret != 0) &&
(chmod(SUPP_ENTROPY_FILE, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) != 0)) {
ALOGE("Cannot set RW to \"%s\": %s", SUPP_ENTROPY_FILE, strerror(errno));
return -1;
}
return 0;
}
destfd = TEMP_FAILURE_RETRY(open(SUPP_ENTROPY_FILE, O_CREAT|O_RDWR, 0660));
if (destfd < 0) {
ALOGE("Cannot create \"%s\": %s", SUPP_ENTROPY_FILE, strerror(errno));
return -1;
}
if (TEMP_FAILURE_RETRY(write(destfd, dummy_key, sizeof(dummy_key))) != sizeof(dummy_key)) {
ALOGE("Error writing \"%s\": %s", SUPP_ENTROPY_FILE, strerror(errno));
close(destfd);
return -1;
}
close(destfd);
/* chmod is needed because open() didn't set permisions properly */
if (chmod(SUPP_ENTROPY_FILE, 0660) < 0) {
ALOGE("Error changing permissions of %s to 0660: %s",
SUPP_ENTROPY_FILE, strerror(errno));
unlink(SUPP_ENTROPY_FILE);
return -1;
}
if (chown(SUPP_ENTROPY_FILE, AID_SYSTEM, AID_WIFI) < 0) {
ALOGE("Error changing group ownership of %s to %d: %s",
SUPP_ENTROPY_FILE, AID_WIFI, strerror(errno));
unlink(SUPP_ENTROPY_FILE);
return -1;
}
return 0;
}
int update_ctrl_interface(const char *config_file) {
int srcfd, destfd;
int nread;
char ifc[PROPERTY_VALUE_MAX];
char *pbuf;
char *sptr;
struct stat sb;
if (stat(config_file, &sb) != 0)
return -1;
pbuf = malloc(sb.st_size + PROPERTY_VALUE_MAX);
if (!pbuf)
return 0;
srcfd = TEMP_FAILURE_RETRY(open(config_file, O_RDONLY));
if (srcfd < 0) {
ALOGE("Cannot open \"%s\": %s", config_file, strerror(errno));
free(pbuf);
return 0;
}
nread = TEMP_FAILURE_RETRY(read(srcfd, pbuf, sb.st_size));
close(srcfd);
if (nread < 0) {
ALOGE("Cannot read \"%s\": %s", config_file, strerror(errno));
free(pbuf);
return 0;
}
if (!strcmp(config_file, SUPP_CONFIG_FILE)) {
property_get("wifi.interface", ifc, WIFI_TEST_INTERFACE);
} else {
strcpy(ifc, CONTROL_IFACE_PATH);
}
/*
* if there is a "ctrl_interface=<value>" entry, re-write it ONLY if it is
* NOT a directory. The non-directory value option is an Android add-on
* that allows the control interface to be exchanged through an environment
* variable (initialized by the "init" program when it starts a service
* with a "socket" option).
*
* The <value> is deemed to be a directory if the "DIR=" form is used or
* the value begins with "/".
*/
if ((sptr = strstr(pbuf, "ctrl_interface=")) &&
(!strstr(pbuf, "ctrl_interface=DIR=")) &&
(!strstr(pbuf, "ctrl_interface=/"))) {
char *iptr = sptr + strlen("ctrl_interface=");
int ilen = 0;
int mlen = strlen(ifc);
int nwrite;
if (strncmp(ifc, iptr, mlen) != 0) {
ALOGE("ctrl_interface != %s", ifc);
while (((ilen + (iptr - pbuf)) < nread) && (iptr[ilen] != '\n'))
ilen++;
mlen = ((ilen >= mlen) ? ilen : mlen) + 1;
memmove(iptr + mlen, iptr + ilen + 1, nread - (iptr + ilen + 1 - pbuf));
memset(iptr, '\n', mlen);
memcpy(iptr, ifc, strlen(ifc));
destfd = TEMP_FAILURE_RETRY(open(config_file, O_RDWR, 0660));
if (destfd < 0) {
ALOGE("Cannot update \"%s\": %s", config_file, strerror(errno));
free(pbuf);
return -1;
}
TEMP_FAILURE_RETRY(write(destfd, pbuf, nread + mlen - ilen -1));
close(destfd);
}
}
free(pbuf);
return 0;
}
int ensure_config_file_exists(const char *config_file)
{
char buf[2048];
int srcfd, destfd;
struct stat sb;
int nread;
int ret;
ret = access(config_file, R_OK|W_OK);
if ((ret == 0) || (errno == EACCES)) {
if ((ret != 0) &&
(chmod(config_file, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) != 0)) {
ALOGE("Cannot set RW to \"%s\": %s", config_file, strerror(errno));
return -1;
}
/* return if filesize is at least 10 bytes */
if (stat(config_file, &sb) == 0 && sb.st_size > 10) {
return update_ctrl_interface(config_file);
}
} else if (errno != ENOENT) {
ALOGE("Cannot access \"%s\": %s", config_file, strerror(errno));
return -1;
}
srcfd = TEMP_FAILURE_RETRY(open(SUPP_CONFIG_TEMPLATE, O_RDONLY));
if (srcfd < 0) {
ALOGE("Cannot open \"%s\": %s", SUPP_CONFIG_TEMPLATE, strerror(errno));
return -1;
}
destfd = TEMP_FAILURE_RETRY(open(config_file, O_CREAT|O_RDWR, 0660));
if (destfd < 0) {
close(srcfd);
ALOGE("Cannot create \"%s\": %s", config_file, strerror(errno));
return -1;
}
while ((nread = TEMP_FAILURE_RETRY(read(srcfd, buf, sizeof(buf)))) != 0) {
if (nread < 0) {
ALOGE("Error reading \"%s\": %s", SUPP_CONFIG_TEMPLATE, strerror(errno));
close(srcfd);
close(destfd);
unlink(config_file);
return -1;
}
TEMP_FAILURE_RETRY(write(destfd, buf, nread));
}
close(destfd);
close(srcfd);
/* chmod is needed because open() didn't set permisions properly */
if (chmod(config_file, 0660) < 0) {
ALOGE("Error changing permissions of %s to 0660: %s",
config_file, strerror(errno));
unlink(config_file);
return -1;
}
if (chown(config_file, AID_SYSTEM, AID_WIFI) < 0) {
ALOGE("Error changing group ownership of %s to %d: %s",
config_file, AID_WIFI, strerror(errno));
unlink(config_file);
return -1;
}
return update_ctrl_interface(config_file);
}
/**
* wifi_wpa_ctrl_cleanup() - Delete any local UNIX domain socket files that
* may be left over from clients that were previously connected to
* wpa_supplicant. This keeps these files from being orphaned in the
* event of crashes that prevented them from being removed as part
* of the normal orderly shutdown.
*/
void wifi_wpa_ctrl_cleanup(void)
{
DIR *dir;
struct dirent entry;
struct dirent *result;
size_t dirnamelen;
size_t maxcopy;
char pathname[PATH_MAX];
char *namep;
char *local_socket_dir = CONFIG_CTRL_IFACE_CLIENT_DIR;
char *local_socket_prefix = CONFIG_CTRL_IFACE_CLIENT_PREFIX;
if ((dir = opendir(local_socket_dir)) == NULL)
return;
dirnamelen = (size_t)snprintf(pathname, sizeof(pathname), "%s/", local_socket_dir);
if (dirnamelen >= sizeof(pathname)) {
closedir(dir);
return;
}
namep = pathname + dirnamelen;
maxcopy = PATH_MAX - dirnamelen;
while (readdir_r(dir, &entry, &result) == 0 && result != NULL) {
if (strncmp(entry.d_name, local_socket_prefix, strlen(local_socket_prefix)) == 0) {
if (strlcpy(namep, entry.d_name, maxcopy) < maxcopy) {
unlink(pathname);
}
}
}
closedir(dir);
}
#ifdef USES_TI_MAC80211
static int init_nl()
{
int err;
nl_soc = nl_socket_alloc();
if (!nl_soc) {
ALOGE("Failed to allocate netlink socket.");
return -ENOMEM;
}
if (genl_connect(nl_soc)) {
ALOGE("Failed to connect to generic netlink.");
err = -ENOLINK;
goto out_handle_destroy;
}
genl_ctrl_alloc_cache(nl_soc, &nl_cache);
if (!nl_cache) {
ALOGE("Failed to allocate generic netlink cache.");
err = -ENOMEM;
goto out_handle_destroy;
}
nl80211 = genl_ctrl_search_by_name(nl_cache, "nl80211");
if (!nl80211) {
ALOGE("nl80211 not found.");
err = -ENOENT;
goto out_cache_free;
}
return 0;
out_cache_free:
nl_cache_free(nl_cache);
out_handle_destroy:
nl_socket_free(nl_soc);
return err;
}
static void deinit_nl()
{
genl_family_put(nl80211);
nl_cache_free(nl_cache);
nl_socket_free(nl_soc);
}
// ignore the "." and ".." entries
static int dir_filter(const struct dirent *name)
{
if (0 == strcmp("..", name->d_name) ||
0 == strcmp(".", name->d_name))
return 0;
return 1;
}
// lookup the only active phy
int phy_lookup()
{
char buf[200];
int fd, pos;
struct dirent **namelist;
int n, i;
n = scandir("/sys/class/ieee80211", &namelist, dir_filter,
(int (*)(const struct dirent**, const struct dirent**))alphasort);
if (n != 1) {
ALOGE("unexpected - found %d phys in /sys/class/ieee80211", n);
for (i = 0; i < n; i++)
free(namelist[i]);
free(namelist);
return -1;
}
snprintf(buf, sizeof(buf), "/sys/class/ieee80211/%s/index",
namelist[0]->d_name);
free(namelist[0]);
free(namelist);
fd = open(buf, O_RDONLY);
if (fd < 0)
return -1;
pos = read(fd, buf, sizeof(buf) - 1);
if (pos < 0) {
close(fd);
return -1;
}
buf[pos] = '\0';
close(fd);
return atoi(buf);
}
int nl_error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err, void *arg)
{
int *ret = (int *)arg;
*ret = err->error;
return NL_STOP;
}
int nl_finish_handler(struct nl_msg *msg, void *arg)
{
int *ret = (int *)arg;
*ret = 0;
return NL_SKIP;
}
int nl_ack_handler(struct nl_msg *msg, void *arg)
{
int *ret = (int *)arg;
*ret = 0;
return NL_STOP;
}
static int execute_nl_interface_cmd(const char *iface,
enum nl80211_iftype type,
uint8_t cmd)
{
struct nl_cb *cb;
struct nl_msg *msg;
int devidx = 0;
int err;
int add_interface = (cmd == NL80211_CMD_NEW_INTERFACE);
if (add_interface) {
devidx = phy_lookup();
} else {
devidx = if_nametoindex(iface);
if (devidx == 0) {
ALOGE("failed to translate ifname to idx");
return -errno;
}
}
msg = nlmsg_alloc();
if (!msg) {
ALOGE("failed to allocate netlink message");
return 2;
}
cb = nl_cb_alloc(NL_CB_DEFAULT);
if (!cb) {
ALOGE("failed to allocate netlink callbacks");
err = 2;
goto out_free_msg;
}
genlmsg_put(msg, 0, 0, genl_family_get_id(nl80211), 0, 0, cmd, 0);
if (add_interface) {
NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, devidx);
} else {
NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, devidx);
}
if (add_interface) {
NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, iface);
NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, type);
}
err = nl_send_auto_complete(nl_soc, msg);
if (err < 0)
goto out;
err = 1;
nl_cb_err(cb, NL_CB_CUSTOM, nl_error_handler, &err);
nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, nl_finish_handler, &err);
nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, nl_ack_handler, &err);
while (err > 0)
nl_recvmsgs(nl_soc, cb);
out:
nl_cb_put(cb);
out_free_msg:
nlmsg_free(msg);
return err;
nla_put_failure:
ALOGW("building message failed");
return 2;
}
int add_remove_p2p_interface(int add)
{
int ret;
ret = init_nl();
if (ret != 0)
return ret;
if (add) {
ret = execute_nl_interface_cmd(P2P_INTERFACE, NL80211_IFTYPE_STATION,
NL80211_CMD_NEW_INTERFACE);
if (ret != 0) {
ALOGE("could not add P2P interface: %d", ret);
goto cleanup;
}
} else {
ret = execute_nl_interface_cmd(P2P_INTERFACE, NL80211_IFTYPE_STATION,
NL80211_CMD_DEL_INTERFACE);
if (ret != 0) {
ALOGE("could not remove P2P interface: %d", ret);
goto cleanup;
}
}
ALOGD("added/removed p2p interface. add: %d", add);
cleanup:
deinit_nl();
return ret;
}
#endif /* USES_TI_MAC80211 */
int wifi_start_supplicant(int p2p_supported)
{
char supp_status[PROPERTY_VALUE_MAX] = {'\0'};
int count = 200; /* wait at most 20 seconds for completion */
#ifdef HAVE_LIBC_SYSTEM_PROPERTIES
const prop_info *pi;
unsigned serial = 0, i;
#endif
if (p2p_supported) {
strcpy(supplicant_name, P2P_SUPPLICANT_NAME);
strcpy(supplicant_prop_name, P2P_PROP_NAME);
/* Ensure p2p config file is created */
if (ensure_config_file_exists(P2P_CONFIG_FILE) < 0) {
ALOGE("Failed to create a p2p config file");
return -1;
}
} else {
strcpy(supplicant_name, SUPPLICANT_NAME);
strcpy(supplicant_prop_name, SUPP_PROP_NAME);
}
/* Check whether already running */
if (property_get(supplicant_name, supp_status, NULL)
&& strcmp(supp_status, "running") == 0) {
return 0;
}
/* Before starting the daemon, make sure its config file exists */
if (ensure_config_file_exists(SUPP_CONFIG_FILE) < 0) {
ALOGE("Wi-Fi will not be enabled");
return -1;
}
if (ensure_entropy_file_exists() < 0) {
ALOGE("Wi-Fi entropy file was not created");
}
#ifdef USES_TI_MAC80211
if (p2p_supported && add_remove_p2p_interface(1) < 0) {
ALOGE("Wi-Fi - could not create p2p interface");
return -1;
}
#endif
/* Clear out any stale socket files that might be left over. */
wifi_wpa_ctrl_cleanup();
/* Reset sockets used for exiting from hung state */
for (i=0; i<MAX_CONNS; i++) {
exit_sockets[i][0] = exit_sockets[i][1] = -1;
}
#ifdef HAVE_LIBC_SYSTEM_PROPERTIES
/*
* Get a reference to the status property, so we can distinguish
* the case where it goes stopped => running => stopped (i.e.,
* it start up, but fails right away) from the case in which
* it starts in the stopped state and never manages to start
* running at all.
*/
pi = __system_property_find(supplicant_prop_name);
if (pi != NULL) {
serial = pi->serial;
}
#endif
property_get("wifi.interface", primary_iface, WIFI_TEST_INTERFACE);
property_set("ctl.start", supplicant_name);
sched_yield();
while (count-- > 0) {
#ifdef HAVE_LIBC_SYSTEM_PROPERTIES
if (pi == NULL) {
pi = __system_property_find(supplicant_prop_name);
}
if (pi != NULL) {
__system_property_read(pi, NULL, supp_status);
if (strcmp(supp_status, "running") == 0) {
return 0;
} else if (pi->serial != serial &&
strcmp(supp_status, "stopped") == 0) {
return -1;
}
}
#else
if (property_get(supplicant_prop_name, supp_status, NULL)) {
if (strcmp(supp_status, "running") == 0)
return 0;
}
#endif
usleep(100000);
}
return -1;
}
int wifi_stop_supplicant(int p2p_supported)
{
char supp_status[PROPERTY_VALUE_MAX] = {'\0'};
int count = 50; /* wait at most 5 seconds for completion */
if (p2p_supported) {
strcpy(supplicant_name, P2P_SUPPLICANT_NAME);
strcpy(supplicant_prop_name, P2P_PROP_NAME);
} else {
strcpy(supplicant_name, SUPPLICANT_NAME);
strcpy(supplicant_prop_name, SUPP_PROP_NAME);
}
/* Check whether supplicant already stopped */
if (property_get(supplicant_prop_name, supp_status, NULL)
&& strcmp(supp_status, "stopped") == 0) {
return 0;
}
property_set("ctl.stop", supplicant_name);
sched_yield();
while (count-- > 0) {
if (property_get(supplicant_prop_name, supp_status, NULL)) {
if (strcmp(supp_status, "stopped") == 0)
return 0;
}
usleep(100000);
}
ALOGE("Failed to stop supplicant");
return -1;
}
int wifi_connect_on_socket_path(int index, const char *path)
{
char supp_status[PROPERTY_VALUE_MAX] = {'\0'};
/* Make sure supplicant is running */
if (!property_get(supplicant_prop_name, supp_status, NULL)
|| strcmp(supp_status, "running") != 0) {
ALOGE("Supplicant not running, cannot connect");
return -1;
}
ctrl_conn[index] = wpa_ctrl_open(path);
if (ctrl_conn[index] == NULL) {
ALOGE("Unable to open connection to supplicant on \"%s\": %s",
path, strerror(errno));
return -1;
}
monitor_conn[index] = wpa_ctrl_open(path);
if (monitor_conn[index] == NULL) {
wpa_ctrl_close(ctrl_conn[index]);
ctrl_conn[index] = NULL;
return -1;
}
if (wpa_ctrl_attach(monitor_conn[index]) != 0) {
wpa_ctrl_close(monitor_conn[index]);
wpa_ctrl_close(ctrl_conn[index]);
ctrl_conn[index] = monitor_conn[index] = NULL;
return -1;
}
if (socketpair(AF_UNIX, SOCK_STREAM, 0, exit_sockets[index]) == -1) {
wpa_ctrl_close(monitor_conn[index]);
wpa_ctrl_close(ctrl_conn[index]);
ctrl_conn[index] = monitor_conn[index] = NULL;
return -1;
}
return 0;
}
/* Establishes the control and monitor socket connections on the interface */
int wifi_connect_to_supplicant(const char *ifname)
{
char path[256];
if (is_primary_interface(ifname)) {
if (access(IFACE_DIR, F_OK) == 0) {
snprintf(path, sizeof(path), "%s/%s", IFACE_DIR, primary_iface);
} else {
strlcpy(path, primary_iface, sizeof(path));
}
return wifi_connect_on_socket_path(PRIMARY, path);
} else {
sprintf(path, "%s/%s", CONTROL_IFACE_PATH, ifname);
return wifi_connect_on_socket_path(SECONDARY, path);
}
}
int wifi_send_command(int index, const char *cmd, char *reply, size_t *reply_len)
{
int ret;
if (ctrl_conn[index] == NULL) {
ALOGV("Not connected to wpa_supplicant - \"%s\" command dropped.\n", cmd);
return -1;
}
ret = wpa_ctrl_request(ctrl_conn[index], cmd, strlen(cmd), reply, reply_len, NULL);
if (ret == -2) {
ALOGD("'%s' command timed out.\n", cmd);
/* unblocks the monitor receive socket for termination */
TEMP_FAILURE_RETRY(write(exit_sockets[index][0], "T", 1));
return -2;
} else if (ret < 0 || strncmp(reply, "FAIL", 4) == 0) {
return -1;
}
if (strncmp(cmd, "PING", 4) == 0) {
reply[*reply_len] = '\0';
}
return 0;
}
int wifi_ctrl_recv(int index, char *reply, size_t *reply_len)
{
int res;
int ctrlfd = wpa_ctrl_get_fd(monitor_conn[index]);
struct pollfd rfds[2];
memset(rfds, 0, 2 * sizeof(struct pollfd));
rfds[0].fd = ctrlfd;
rfds[0].events |= POLLIN;
rfds[1].fd = exit_sockets[index][1];
rfds[1].events |= POLLIN;
res = TEMP_FAILURE_RETRY(poll(rfds, 2, -1));
if (res < 0) {
ALOGE("Error poll = %d", res);
return res;
}
if (rfds[0].revents & POLLIN) {
return wpa_ctrl_recv(monitor_conn[index], reply, reply_len);
} else if (rfds[1].revents & POLLIN) {
/* Close only the p2p sockets on receive side
* see wifi_close_supplicant_connection()
*/
if (index == SECONDARY) {
ALOGD("close sockets %d", index);
wifi_close_sockets(index);
}
}
return -2;
}
int wifi_wait_on_socket(int index, char *buf, size_t buflen)
{
size_t nread = buflen - 1;
int fd;
fd_set rfds;
int result;
struct timeval tval;
struct timeval *tptr;
if (monitor_conn[index] == NULL) {
ALOGD("Connection closed\n");
strncpy(buf, WPA_EVENT_TERMINATING " - connection closed", buflen-1);
buf[buflen-1] = '\0';
return strlen(buf);
}
result = wifi_ctrl_recv(index, buf, &nread);
/* Terminate reception on exit socket */
if (result == -2) {
strncpy(buf, WPA_EVENT_TERMINATING " - connection closed", buflen-1);
buf[buflen-1] = '\0';
return strlen(buf);
}
if (result < 0) {
ALOGD("wifi_ctrl_recv failed: %s\n", strerror(errno));
strncpy(buf, WPA_EVENT_TERMINATING " - recv error", buflen-1);
buf[buflen-1] = '\0';
return strlen(buf);
}
buf[nread] = '\0';
/* Check for EOF on the socket */
if (result == 0 && nread == 0) {
/* Fabricate an event to pass up */
ALOGD("Received EOF on supplicant socket\n");
strncpy(buf, WPA_EVENT_TERMINATING " - signal 0 received", buflen-1);
buf[buflen-1] = '\0';
return strlen(buf);
}
/*
* Events strings are in the format
*
* <N>CTRL-EVENT-XXX
*
* where N is the message level in numerical form (0=VERBOSE, 1=DEBUG,
* etc.) and XXX is the event name. The level information is not useful
* to us, so strip it off.
*/
if (buf[0] == '<') {
char *match = strchr(buf, '>');
if (match != NULL) {
nread -= (match+1-buf);
memmove(buf, match+1, nread+1);
}
}
return nread;
}
int wifi_wait_for_event(const char *ifname, char *buf, size_t buflen)
{
if (is_primary_interface(ifname)) {
return wifi_wait_on_socket(PRIMARY, buf, buflen);
} else {
return wifi_wait_on_socket(SECONDARY, buf, buflen);
}
}
void wifi_close_sockets(int index)
{
if (ctrl_conn[index] != NULL) {
wpa_ctrl_close(ctrl_conn[index]);
ctrl_conn[index] = NULL;
}
if (monitor_conn[index] != NULL) {
wpa_ctrl_close(monitor_conn[index]);
monitor_conn[index] = NULL;
}
if (exit_sockets[index][0] >= 0) {
close(exit_sockets[index][0]);
exit_sockets[index][0] = -1;
}
if (exit_sockets[index][1] >= 0) {
close(exit_sockets[index][1]);
exit_sockets[index][1] = -1;
}
}
void wifi_close_supplicant_connection(const char *ifname)
{
char supp_status[PROPERTY_VALUE_MAX] = {'\0'};
int count = 50; /* wait at most 5 seconds to ensure init has stopped stupplicant */
if (is_primary_interface(ifname)) {
wifi_close_sockets(PRIMARY);
} else {
/* p2p socket termination needs unblocking the monitor socket
* STA connection does not need it since supplicant gets shutdown
*/
TEMP_FAILURE_RETRY(write(exit_sockets[SECONDARY][0], "T", 1));
/* p2p sockets are closed after the monitor thread
* receives the terminate on the exit socket
*/
return;
}
while (count-- > 0) {
if (property_get(supplicant_prop_name, supp_status, NULL)) {
if (strcmp(supp_status, "stopped") == 0)
return;
}
usleep(100000);
}
}
int wifi_command(const char *ifname, const char *command, char *reply, size_t *reply_len)
{
if (is_primary_interface(ifname)) {
return wifi_send_command(PRIMARY, command, reply, reply_len);
} else {
return wifi_send_command(SECONDARY, command, reply, reply_len);
}
}
const char *wifi_get_fw_path(int fw_type)
{
switch (fw_type) {
case WIFI_GET_FW_PATH_STA:
return WIFI_DRIVER_FW_PATH_STA;
case WIFI_GET_FW_PATH_AP:
return WIFI_DRIVER_FW_PATH_AP;
case WIFI_GET_FW_PATH_P2P:
return WIFI_DRIVER_FW_PATH_P2P;
}
return NULL;
}
int wifi_change_fw_path(const char *fwpath)
{
int len;
int fd;
int ret = 0;
if (!fwpath)
return ret;
fd = TEMP_FAILURE_RETRY(open(WIFI_DRIVER_FW_PATH_PARAM, O_WRONLY));
if (fd < 0) {
ALOGE("Failed to open wlan fw path param (%s)", strerror(errno));
return -1;
}
len = strlen(fwpath) + 1;
if (TEMP_FAILURE_RETRY(write(fd, fwpath, len)) != len) {
ALOGE("Failed to write wlan fw path param (%s)", strerror(errno));
ret = -1;
}
close(fd);
return ret;
}
int wifi_set_mode(int mode) {
wifi_mode = mode;
return 0;
}
|