2 * Copyright 2008, The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
22 #include <sys/socket.h>
26 #include "hardware_legacy/wifi.h"
27 #include "libwpa_client/wpa_ctrl.h"
29 #define LOG_TAG "WifiHW"
30 #include "cutils/log.h"
31 #include "cutils/memory.h"
32 #include "cutils/misc.h"
33 #include "cutils/properties.h"
34 #include "private/android_filesystem_config.h"
35 #ifdef HAVE_LIBC_SYSTEM_PROPERTIES
36 #define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
37 #include <sys/_system_properties.h>
40 static struct wpa_ctrl *ctrl_conn;
41 static struct wpa_ctrl *monitor_conn;
43 /* socket pair used to exit from a blocking read */
44 static int exit_sockets[2];
47 extern int ifc_init();
48 extern void ifc_close();
49 extern char *dhcp_lasterror();
50 extern void get_dhcp_info();
51 extern int init_module(void *, unsigned long, const char *);
52 extern int delete_module(const char *, unsigned int);
53 void wifi_close_sockets();
55 static char primary_iface[PROPERTY_VALUE_MAX];
56 // TODO: use new ANDROID_SOCKET mechanism, once support for multiple
59 #ifndef WIFI_DRIVER_MODULE_ARG
60 #define WIFI_DRIVER_MODULE_ARG ""
62 #ifndef WIFI_FIRMWARE_LOADER
63 #define WIFI_FIRMWARE_LOADER ""
65 #define WIFI_TEST_INTERFACE "sta"
67 #ifndef WIFI_DRIVER_FW_PATH_STA
68 #define WIFI_DRIVER_FW_PATH_STA NULL
70 #ifndef WIFI_DRIVER_FW_PATH_AP
71 #define WIFI_DRIVER_FW_PATH_AP NULL
73 #ifndef WIFI_DRIVER_FW_PATH_P2P
74 #define WIFI_DRIVER_FW_PATH_P2P NULL
77 #ifndef WIFI_DRIVER_FW_PATH_PARAM
78 #define WIFI_DRIVER_FW_PATH_PARAM "/sys/module/wlan/parameters/fwpath"
81 #define WIFI_DRIVER_LOADER_DELAY 1000000
83 static const char IFACE_DIR[] = "/data/system/wpa_supplicant";
84 #ifdef WIFI_DRIVER_MODULE_PATH
85 static const char DRIVER_MODULE_NAME[] = WIFI_DRIVER_MODULE_NAME;
86 static const char DRIVER_MODULE_TAG[] = WIFI_DRIVER_MODULE_NAME " ";
87 static const char DRIVER_MODULE_PATH[] = WIFI_DRIVER_MODULE_PATH;
88 static const char DRIVER_MODULE_ARG[] = WIFI_DRIVER_MODULE_ARG;
90 static const char FIRMWARE_LOADER[] = WIFI_FIRMWARE_LOADER;
91 static const char DRIVER_PROP_NAME[] = "wlan.driver.status";
92 static const char SUPPLICANT_NAME[] = "wpa_supplicant";
93 static const char SUPP_PROP_NAME[] = "init.svc.wpa_supplicant";
94 static const char P2P_SUPPLICANT_NAME[] = "p2p_supplicant";
95 static const char P2P_PROP_NAME[] = "init.svc.p2p_supplicant";
96 static const char SUPP_CONFIG_TEMPLATE[]= "/system/etc/wifi/wpa_supplicant.conf";
97 static const char SUPP_CONFIG_FILE[] = "/data/misc/wifi/wpa_supplicant.conf";
98 static const char P2P_CONFIG_FILE[] = "/data/misc/wifi/p2p_supplicant.conf";
99 static const char CONTROL_IFACE_PATH[] = "/data/misc/wifi/sockets";
100 static const char MODULE_FILE[] = "/proc/modules";
102 static const char IFNAME[] = "IFNAME=";
103 #define IFNAMELEN (sizeof(IFNAME) - 1)
104 static const char WPA_EVENT_IGNORE[] = "CTRL-EVENT-IGNORE ";
106 static const char SUPP_ENTROPY_FILE[] = WIFI_ENTROPY_FILE;
107 static unsigned char dummy_key[21] = { 0x02, 0x11, 0xbe, 0x33, 0x43, 0x35,
108 0x68, 0x47, 0x84, 0x99, 0xa9, 0x2b,
109 0x1c, 0xd3, 0xee, 0xff, 0xf1, 0xe2,
112 /* Is either SUPPLICANT_NAME or P2P_SUPPLICANT_NAME */
113 static char supplicant_name[PROPERTY_VALUE_MAX];
114 /* Is either SUPP_PROP_NAME or P2P_PROP_NAME */
115 static char supplicant_prop_name[PROPERTY_KEY_MAX];
117 static int insmod(const char *filename, const char *args)
123 module = load_file(filename, &size);
127 ret = init_module(module, size, args);
134 static int rmmod(const char *modname)
139 while (maxtry-- > 0) {
140 ret = delete_module(modname, O_NONBLOCK | O_EXCL);
141 if (ret < 0 && errno == EAGAIN)
148 ALOGD("Unable to unload driver module \"%s\": %s\n",
149 modname, strerror(errno));
153 int do_dhcp_request(int *ipaddr, int *gateway, int *mask,
154 int *dns1, int *dns2, int *server, int *lease) {
155 /* For test driver, always report success */
156 if (strcmp(primary_iface, WIFI_TEST_INTERFACE) == 0)
162 if (do_dhcp(primary_iface) < 0) {
167 get_dhcp_info(ipaddr, gateway, mask, dns1, dns2, server, lease);
171 const char *get_dhcp_error_string() {
172 return dhcp_lasterror();
175 int is_wifi_driver_loaded() {
176 char driver_status[PROPERTY_VALUE_MAX];
177 #ifdef WIFI_DRIVER_MODULE_PATH
179 char line[sizeof(DRIVER_MODULE_TAG)+10];
182 if (!property_get(DRIVER_PROP_NAME, driver_status, NULL)
183 || strcmp(driver_status, "ok") != 0) {
184 return 0; /* driver not loaded */
186 #ifdef WIFI_DRIVER_MODULE_PATH
188 * If the property says the driver is loaded, check to
189 * make sure that the property setting isn't just left
190 * over from a previous manual shutdown or a runtime
193 if ((proc = fopen(MODULE_FILE, "r")) == NULL) {
194 ALOGW("Could not open %s: %s", MODULE_FILE, strerror(errno));
195 property_set(DRIVER_PROP_NAME, "unloaded");
198 while ((fgets(line, sizeof(line), proc)) != NULL) {
199 if (strncmp(line, DRIVER_MODULE_TAG, strlen(DRIVER_MODULE_TAG)) == 0) {
205 property_set(DRIVER_PROP_NAME, "unloaded");
212 int wifi_load_driver()
214 #ifdef WIFI_DRIVER_MODULE_PATH
215 char driver_status[PROPERTY_VALUE_MAX];
216 int count = 100; /* wait at most 20 seconds for completion */
218 if (is_wifi_driver_loaded()) {
222 if (insmod(DRIVER_MODULE_PATH, DRIVER_MODULE_ARG) < 0)
225 if (strcmp(FIRMWARE_LOADER,"") == 0) {
226 /* usleep(WIFI_DRIVER_LOADER_DELAY); */
227 property_set(DRIVER_PROP_NAME, "ok");
230 property_set("ctl.start", FIRMWARE_LOADER);
233 while (count-- > 0) {
234 if (property_get(DRIVER_PROP_NAME, driver_status, NULL)) {
235 if (strcmp(driver_status, "ok") == 0)
237 else if (strcmp(DRIVER_PROP_NAME, "failed") == 0) {
238 wifi_unload_driver();
244 property_set(DRIVER_PROP_NAME, "timeout");
245 wifi_unload_driver();
248 property_set(DRIVER_PROP_NAME, "ok");
253 int wifi_unload_driver()
255 usleep(200000); /* allow to finish interface down */
256 #ifdef WIFI_DRIVER_MODULE_PATH
257 if (rmmod(DRIVER_MODULE_NAME) == 0) {
258 int count = 20; /* wait at most 10 seconds for completion */
259 while (count-- > 0) {
260 if (!is_wifi_driver_loaded())
264 usleep(500000); /* allow card removal */
272 property_set(DRIVER_PROP_NAME, "unloaded");
277 int ensure_entropy_file_exists()
282 ret = access(SUPP_ENTROPY_FILE, R_OK|W_OK);
283 if ((ret == 0) || (errno == EACCES)) {
285 (chmod(SUPP_ENTROPY_FILE, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) != 0)) {
286 ALOGE("Cannot set RW to \"%s\": %s", SUPP_ENTROPY_FILE, strerror(errno));
291 destfd = TEMP_FAILURE_RETRY(open(SUPP_ENTROPY_FILE, O_CREAT|O_RDWR, 0660));
293 ALOGE("Cannot create \"%s\": %s", SUPP_ENTROPY_FILE, strerror(errno));
297 if (TEMP_FAILURE_RETRY(write(destfd, dummy_key, sizeof(dummy_key))) != sizeof(dummy_key)) {
298 ALOGE("Error writing \"%s\": %s", SUPP_ENTROPY_FILE, strerror(errno));
304 /* chmod is needed because open() didn't set permisions properly */
305 if (chmod(SUPP_ENTROPY_FILE, 0660) < 0) {
306 ALOGE("Error changing permissions of %s to 0660: %s",
307 SUPP_ENTROPY_FILE, strerror(errno));
308 unlink(SUPP_ENTROPY_FILE);
312 if (chown(SUPP_ENTROPY_FILE, AID_SYSTEM, AID_WIFI) < 0) {
313 ALOGE("Error changing group ownership of %s to %d: %s",
314 SUPP_ENTROPY_FILE, AID_WIFI, strerror(errno));
315 unlink(SUPP_ENTROPY_FILE);
321 int ensure_config_file_exists(const char *config_file)
329 ret = access(config_file, R_OK|W_OK);
330 if ((ret == 0) || (errno == EACCES)) {
332 (chmod(config_file, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) != 0)) {
333 ALOGE("Cannot set RW to \"%s\": %s", config_file, strerror(errno));
337 } else if (errno != ENOENT) {
338 ALOGE("Cannot access \"%s\": %s", config_file, strerror(errno));
342 srcfd = TEMP_FAILURE_RETRY(open(SUPP_CONFIG_TEMPLATE, O_RDONLY));
344 ALOGE("Cannot open \"%s\": %s", SUPP_CONFIG_TEMPLATE, strerror(errno));
348 destfd = TEMP_FAILURE_RETRY(open(config_file, O_CREAT|O_RDWR, 0660));
351 ALOGE("Cannot create \"%s\": %s", config_file, strerror(errno));
355 while ((nread = TEMP_FAILURE_RETRY(read(srcfd, buf, sizeof(buf)))) != 0) {
357 ALOGE("Error reading \"%s\": %s", SUPP_CONFIG_TEMPLATE, strerror(errno));
363 TEMP_FAILURE_RETRY(write(destfd, buf, nread));
369 /* chmod is needed because open() didn't set permisions properly */
370 if (chmod(config_file, 0660) < 0) {
371 ALOGE("Error changing permissions of %s to 0660: %s",
372 config_file, strerror(errno));
377 if (chown(config_file, AID_SYSTEM, AID_WIFI) < 0) {
378 ALOGE("Error changing group ownership of %s to %d: %s",
379 config_file, AID_WIFI, strerror(errno));
386 int wifi_start_supplicant(int p2p_supported)
388 char supp_status[PROPERTY_VALUE_MAX] = {'\0'};
389 int count = 200; /* wait at most 20 seconds for completion */
390 #ifdef HAVE_LIBC_SYSTEM_PROPERTIES
392 unsigned serial = 0, i;
396 strcpy(supplicant_name, P2P_SUPPLICANT_NAME);
397 strcpy(supplicant_prop_name, P2P_PROP_NAME);
399 /* Ensure p2p config file is created */
400 if (ensure_config_file_exists(P2P_CONFIG_FILE) < 0) {
401 ALOGE("Failed to create a p2p config file");
406 strcpy(supplicant_name, SUPPLICANT_NAME);
407 strcpy(supplicant_prop_name, SUPP_PROP_NAME);
410 /* Check whether already running */
411 if (property_get(supplicant_name, supp_status, NULL)
412 && strcmp(supp_status, "running") == 0) {
416 /* Before starting the daemon, make sure its config file exists */
417 if (ensure_config_file_exists(SUPP_CONFIG_FILE) < 0) {
418 ALOGE("Wi-Fi will not be enabled");
422 if (ensure_entropy_file_exists() < 0) {
423 ALOGE("Wi-Fi entropy file was not created");
426 /* Clear out any stale socket files that might be left over. */
429 /* Reset sockets used for exiting from hung state */
430 exit_sockets[0] = exit_sockets[1] = -1;
432 #ifdef HAVE_LIBC_SYSTEM_PROPERTIES
434 * Get a reference to the status property, so we can distinguish
435 * the case where it goes stopped => running => stopped (i.e.,
436 * it start up, but fails right away) from the case in which
437 * it starts in the stopped state and never manages to start
440 pi = __system_property_find(supplicant_prop_name);
442 serial = __system_property_serial(pi);
445 property_get("wifi.interface", primary_iface, WIFI_TEST_INTERFACE);
447 property_set("ctl.start", supplicant_name);
450 while (count-- > 0) {
451 #ifdef HAVE_LIBC_SYSTEM_PROPERTIES
453 pi = __system_property_find(supplicant_prop_name);
456 __system_property_read(pi, NULL, supp_status);
457 if (strcmp(supp_status, "running") == 0) {
459 } else if (__system_property_serial(pi) != serial &&
460 strcmp(supp_status, "stopped") == 0) {
465 if (property_get(supplicant_prop_name, supp_status, NULL)) {
466 if (strcmp(supp_status, "running") == 0)
475 int wifi_stop_supplicant(int p2p_supported)
477 char supp_status[PROPERTY_VALUE_MAX] = {'\0'};
478 int count = 50; /* wait at most 5 seconds for completion */
481 strcpy(supplicant_name, P2P_SUPPLICANT_NAME);
482 strcpy(supplicant_prop_name, P2P_PROP_NAME);
484 strcpy(supplicant_name, SUPPLICANT_NAME);
485 strcpy(supplicant_prop_name, SUPP_PROP_NAME);
488 /* Check whether supplicant already stopped */
489 if (property_get(supplicant_prop_name, supp_status, NULL)
490 && strcmp(supp_status, "stopped") == 0) {
494 property_set("ctl.stop", supplicant_name);
497 while (count-- > 0) {
498 if (property_get(supplicant_prop_name, supp_status, NULL)) {
499 if (strcmp(supp_status, "stopped") == 0)
504 ALOGE("Failed to stop supplicant");
508 int wifi_connect_on_socket_path(const char *path)
510 char supp_status[PROPERTY_VALUE_MAX] = {'\0'};
512 /* Make sure supplicant is running */
513 if (!property_get(supplicant_prop_name, supp_status, NULL)
514 || strcmp(supp_status, "running") != 0) {
515 ALOGE("Supplicant not running, cannot connect");
519 ctrl_conn = wpa_ctrl_open(path);
520 if (ctrl_conn == NULL) {
521 ALOGE("Unable to open connection to supplicant on \"%s\": %s",
522 path, strerror(errno));
525 monitor_conn = wpa_ctrl_open(path);
526 if (monitor_conn == NULL) {
527 wpa_ctrl_close(ctrl_conn);
531 if (wpa_ctrl_attach(monitor_conn) != 0) {
532 wpa_ctrl_close(monitor_conn);
533 wpa_ctrl_close(ctrl_conn);
534 ctrl_conn = monitor_conn = NULL;
538 if (socketpair(AF_UNIX, SOCK_STREAM, 0, exit_sockets) == -1) {
539 wpa_ctrl_close(monitor_conn);
540 wpa_ctrl_close(ctrl_conn);
541 ctrl_conn = monitor_conn = NULL;
548 /* Establishes the control and monitor socket connections on the interface */
549 int wifi_connect_to_supplicant()
551 static char path[PATH_MAX];
553 if (access(IFACE_DIR, F_OK) == 0) {
554 snprintf(path, sizeof(path), "%s/%s", IFACE_DIR, primary_iface);
556 snprintf(path, sizeof(path), "@android:wpa_%s", primary_iface);
558 return wifi_connect_on_socket_path(path);
561 int wifi_send_command(const char *cmd, char *reply, size_t *reply_len)
564 if (ctrl_conn == NULL) {
565 ALOGV("Not connected to wpa_supplicant - \"%s\" command dropped.\n", cmd);
568 ret = wpa_ctrl_request(ctrl_conn, cmd, strlen(cmd), reply, reply_len, NULL);
570 ALOGD("'%s' command timed out.\n", cmd);
571 /* unblocks the monitor receive socket for termination */
572 TEMP_FAILURE_RETRY(write(exit_sockets[0], "T", 1));
574 } else if (ret < 0 || strncmp(reply, "FAIL", 4) == 0) {
577 if (strncmp(cmd, "PING", 4) == 0) {
578 reply[*reply_len] = '\0';
583 int wifi_ctrl_recv(char *reply, size_t *reply_len)
586 int ctrlfd = wpa_ctrl_get_fd(monitor_conn);
587 struct pollfd rfds[2];
589 memset(rfds, 0, 2 * sizeof(struct pollfd));
591 rfds[0].events |= POLLIN;
592 rfds[1].fd = exit_sockets[1];
593 rfds[1].events |= POLLIN;
594 res = TEMP_FAILURE_RETRY(poll(rfds, 2, -1));
596 ALOGE("Error poll = %d", res);
599 if (rfds[0].revents & POLLIN) {
600 return wpa_ctrl_recv(monitor_conn, reply, reply_len);
603 /* it is not rfds[0], then it must be rfts[1] (i.e. the exit socket)
604 * or we timed out. In either case, this call has failed ..
609 int wifi_wait_on_socket(char *buf, size_t buflen)
611 size_t nread = buflen - 1;
613 char *match, *match2;
615 if (monitor_conn == NULL) {
616 return snprintf(buf, buflen, WPA_EVENT_TERMINATING " - connection closed");
619 result = wifi_ctrl_recv(buf, &nread);
621 /* Terminate reception on exit socket */
623 return snprintf(buf, buflen, WPA_EVENT_TERMINATING " - connection closed");
627 ALOGD("wifi_ctrl_recv failed: %s\n", strerror(errno));
628 return snprintf(buf, buflen, WPA_EVENT_TERMINATING " - recv error");
631 /* Check for EOF on the socket */
632 if (result == 0 && nread == 0) {
633 /* Fabricate an event to pass up */
634 ALOGD("Received EOF on supplicant socket\n");
635 return snprintf(buf, buflen, WPA_EVENT_TERMINATING " - signal 0 received");
638 * Events strings are in the format
640 * IFNAME=iface <N>CTRL-EVENT-XXX
644 * where N is the message level in numerical form (0=VERBOSE, 1=DEBUG,
645 * etc.) and XXX is the event name. The level information is not useful
646 * to us, so strip it off.
649 if (strncmp(buf, IFNAME, IFNAMELEN) == 0) {
650 match = strchr(buf, ' ');
652 if (match[1] == '<') {
653 match2 = strchr(match + 2, '>');
654 if (match2 != NULL) {
655 nread -= (match2 - match);
656 memmove(match + 1, match2 + 1, nread - (match - buf) + 1);
660 return snprintf(buf, buflen, "%s", WPA_EVENT_IGNORE);
662 } else if (buf[0] == '<') {
663 match = strchr(buf, '>');
665 nread -= (match + 1 - buf);
666 memmove(buf, match + 1, nread + 1);
667 ALOGV("supplicant generated event without interface - %s\n", buf);
670 /* let the event go as is! */
671 ALOGW("supplicant generated event without interface and without message level - %s\n", buf);
677 int wifi_wait_for_event(char *buf, size_t buflen)
679 return wifi_wait_on_socket(buf, buflen);
682 void wifi_close_sockets()
684 if (ctrl_conn != NULL) {
685 wpa_ctrl_close(ctrl_conn);
689 if (monitor_conn != NULL) {
690 wpa_ctrl_close(monitor_conn);
694 if (exit_sockets[0] >= 0) {
695 close(exit_sockets[0]);
696 exit_sockets[0] = -1;
699 if (exit_sockets[1] >= 0) {
700 close(exit_sockets[1]);
701 exit_sockets[1] = -1;
705 void wifi_close_supplicant_connection()
707 char supp_status[PROPERTY_VALUE_MAX] = {'\0'};
708 int count = 50; /* wait at most 5 seconds to ensure init has stopped stupplicant */
710 wifi_close_sockets();
712 while (count-- > 0) {
713 if (property_get(supplicant_prop_name, supp_status, NULL)) {
714 if (strcmp(supp_status, "stopped") == 0)
721 int wifi_command(const char *command, char *reply, size_t *reply_len)
723 return wifi_send_command(command, reply, reply_len);
726 const char *wifi_get_fw_path(int fw_type)
729 case WIFI_GET_FW_PATH_STA:
730 return WIFI_DRIVER_FW_PATH_STA;
731 case WIFI_GET_FW_PATH_AP:
732 return WIFI_DRIVER_FW_PATH_AP;
733 case WIFI_GET_FW_PATH_P2P:
734 return WIFI_DRIVER_FW_PATH_P2P;
739 int wifi_change_fw_path(const char *fwpath)
747 fd = TEMP_FAILURE_RETRY(open(WIFI_DRIVER_FW_PATH_PARAM, O_WRONLY));
749 ALOGE("Failed to open wlan fw path param (%s)", strerror(errno));
752 len = strlen(fwpath) + 1;
753 if (TEMP_FAILURE_RETRY(write(fd, fwpath, len)) != len) {
754 ALOGE("Failed to write wlan fw path param (%s)", strerror(errno));