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>
25 #include "hardware_legacy/wifi.h"
26 #include "libwpa_client/wpa_ctrl.h"
28 #define LOG_TAG "WifiHW"
29 #include "cutils/log.h"
30 #include "cutils/memory.h"
31 #include "cutils/misc.h"
32 #include "cutils/properties.h"
33 #include "private/android_filesystem_config.h"
34 #ifdef HAVE_LIBC_SYSTEM_PROPERTIES
35 #define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
36 #include <sys/_system_properties.h>
39 static struct wpa_ctrl *ctrl_conn;
40 static struct wpa_ctrl *monitor_conn;
41 /* socket pair used to exit from a blocking read */
42 static int exit_sockets[2] = { -1, -1 };
45 extern int ifc_init();
46 extern void ifc_close();
47 extern char *dhcp_lasterror();
48 extern void get_dhcp_info();
49 extern int init_module(void *, unsigned long, const char *);
50 extern int delete_module(const char *, unsigned int);
52 static char iface[PROPERTY_VALUE_MAX];
53 // TODO: use new ANDROID_SOCKET mechanism, once support for multiple
56 #ifndef WIFI_DRIVER_MODULE_ARG
57 #define WIFI_DRIVER_MODULE_ARG ""
59 #ifndef WIFI_FIRMWARE_LOADER
60 #define WIFI_FIRMWARE_LOADER ""
62 #define WIFI_TEST_INTERFACE "sta"
64 #ifndef WIFI_DRIVER_FW_PATH_STA
65 #define WIFI_DRIVER_FW_PATH_STA NULL
67 #ifndef WIFI_DRIVER_FW_PATH_AP
68 #define WIFI_DRIVER_FW_PATH_AP NULL
70 #ifndef WIFI_DRIVER_FW_PATH_P2P
71 #define WIFI_DRIVER_FW_PATH_P2P NULL
74 #ifndef WIFI_DRIVER_FW_PATH_PARAM
75 #define WIFI_DRIVER_FW_PATH_PARAM "/sys/module/wlan/parameters/fwpath"
78 #define WIFI_DRIVER_LOADER_DELAY 1000000
80 static const char IFACE_DIR[] = "/data/system/wpa_supplicant";
81 #ifdef WIFI_DRIVER_MODULE_PATH
82 static const char DRIVER_MODULE_NAME[] = WIFI_DRIVER_MODULE_NAME;
83 static const char DRIVER_MODULE_TAG[] = WIFI_DRIVER_MODULE_NAME " ";
84 static const char DRIVER_MODULE_PATH[] = WIFI_DRIVER_MODULE_PATH;
85 static const char DRIVER_MODULE_ARG[] = WIFI_DRIVER_MODULE_ARG;
87 static const char FIRMWARE_LOADER[] = WIFI_FIRMWARE_LOADER;
88 static const char DRIVER_PROP_NAME[] = "wlan.driver.status";
89 static const char SUPPLICANT_NAME[] = "wpa_supplicant";
90 static const char SUPP_PROP_NAME[] = "init.svc.wpa_supplicant";
91 static const char SUPP_CONFIG_TEMPLATE[]= "/system/etc/wifi/wpa_supplicant.conf";
92 static const char SUPP_CONFIG_FILE[] = "/data/misc/wifi/wpa_supplicant.conf";
93 static const char P2P_CONFIG_FILE[] = "/data/misc/wifi/p2p_supplicant.conf";
94 static const char CONTROL_IFACE_PATH[] = "/data/misc/wifi";
95 static const char MODULE_FILE[] = "/proc/modules";
97 static const char SUPP_ENTROPY_FILE[] = WIFI_ENTROPY_FILE;
98 static unsigned char dummy_key[21] = { 0x02, 0x11, 0xbe, 0x33, 0x43, 0x35,
99 0x68, 0x47, 0x84, 0x99, 0xa9, 0x2b,
100 0x1c, 0xd3, 0xee, 0xff, 0xf1, 0xe2,
103 static int insmod(const char *filename, const char *args)
109 module = load_file(filename, &size);
113 ret = init_module(module, size, args);
120 static int rmmod(const char *modname)
125 while (maxtry-- > 0) {
126 ret = delete_module(modname, O_NONBLOCK | O_EXCL);
127 if (ret < 0 && errno == EAGAIN)
134 LOGD("Unable to unload driver module \"%s\": %s\n",
135 modname, strerror(errno));
139 int do_dhcp_request(int *ipaddr, int *gateway, int *mask,
140 int *dns1, int *dns2, int *server, int *lease) {
141 /* For test driver, always report success */
142 if (strcmp(iface, WIFI_TEST_INTERFACE) == 0)
148 if (do_dhcp(iface) < 0) {
153 get_dhcp_info(ipaddr, gateway, mask, dns1, dns2, server, lease);
157 const char *get_dhcp_error_string() {
158 return dhcp_lasterror();
161 int is_wifi_driver_loaded() {
162 char driver_status[PROPERTY_VALUE_MAX];
163 #ifdef WIFI_DRIVER_MODULE_PATH
165 char line[sizeof(DRIVER_MODULE_TAG)+10];
168 if (!property_get(DRIVER_PROP_NAME, driver_status, NULL)
169 || strcmp(driver_status, "ok") != 0) {
170 return 0; /* driver not loaded */
172 #ifdef WIFI_DRIVER_MODULE_PATH
174 * If the property says the driver is loaded, check to
175 * make sure that the property setting isn't just left
176 * over from a previous manual shutdown or a runtime
179 if ((proc = fopen(MODULE_FILE, "r")) == NULL) {
180 LOGW("Could not open %s: %s", MODULE_FILE, strerror(errno));
181 property_set(DRIVER_PROP_NAME, "unloaded");
184 while ((fgets(line, sizeof(line), proc)) != NULL) {
185 if (strncmp(line, DRIVER_MODULE_TAG, strlen(DRIVER_MODULE_TAG)) == 0) {
191 property_set(DRIVER_PROP_NAME, "unloaded");
198 int wifi_load_driver()
200 #ifdef WIFI_DRIVER_MODULE_PATH
201 char driver_status[PROPERTY_VALUE_MAX];
202 int count = 100; /* wait at most 20 seconds for completion */
204 if (is_wifi_driver_loaded()) {
208 if (insmod(DRIVER_MODULE_PATH, DRIVER_MODULE_ARG) < 0)
211 if (strcmp(FIRMWARE_LOADER,"") == 0) {
212 /* usleep(WIFI_DRIVER_LOADER_DELAY); */
213 property_set(DRIVER_PROP_NAME, "ok");
216 property_set("ctl.start", FIRMWARE_LOADER);
219 while (count-- > 0) {
220 if (property_get(DRIVER_PROP_NAME, driver_status, NULL)) {
221 if (strcmp(driver_status, "ok") == 0)
223 else if (strcmp(DRIVER_PROP_NAME, "failed") == 0) {
224 wifi_unload_driver();
230 property_set(DRIVER_PROP_NAME, "timeout");
231 wifi_unload_driver();
234 property_set(DRIVER_PROP_NAME, "ok");
239 int wifi_unload_driver()
241 usleep(200000); /* allow to finish interface down */
242 #ifdef WIFI_DRIVER_MODULE_PATH
243 if (rmmod(DRIVER_MODULE_NAME) == 0) {
244 int count = 20; /* wait at most 10 seconds for completion */
245 while (count-- > 0) {
246 if (!is_wifi_driver_loaded())
250 usleep(500000); /* allow card removal */
258 property_set(DRIVER_PROP_NAME, "unloaded");
263 int ensure_entropy_file_exists()
268 ret = access(SUPP_ENTROPY_FILE, R_OK|W_OK);
269 if ((ret == 0) || (errno == EACCES)) {
271 (chmod(SUPP_ENTROPY_FILE, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) != 0)) {
272 LOGE("Cannot set RW to \"%s\": %s", SUPP_ENTROPY_FILE, strerror(errno));
277 destfd = open(SUPP_ENTROPY_FILE, O_CREAT|O_RDWR, 0660);
279 LOGE("Cannot create \"%s\": %s", SUPP_ENTROPY_FILE, strerror(errno));
283 if (write(destfd, dummy_key, sizeof(dummy_key)) != sizeof(dummy_key)) {
284 LOGE("Error writing \"%s\": %s", SUPP_ENTROPY_FILE, strerror(errno));
290 /* chmod is needed because open() didn't set permisions properly */
291 if (chmod(SUPP_ENTROPY_FILE, 0660) < 0) {
292 LOGE("Error changing permissions of %s to 0660: %s",
293 SUPP_ENTROPY_FILE, strerror(errno));
294 unlink(SUPP_ENTROPY_FILE);
298 if (chown(SUPP_ENTROPY_FILE, AID_SYSTEM, AID_WIFI) < 0) {
299 LOGE("Error changing group ownership of %s to %d: %s",
300 SUPP_ENTROPY_FILE, AID_WIFI, strerror(errno));
301 unlink(SUPP_ENTROPY_FILE);
307 int update_ctrl_interface(const char *config_file) {
311 char ifc[PROPERTY_VALUE_MAX];
316 if (stat(config_file, &sb) != 0)
319 pbuf = malloc(sb.st_size + PROPERTY_VALUE_MAX);
322 srcfd = open(config_file, O_RDONLY);
324 LOGE("Cannot open \"%s\": %s", config_file, strerror(errno));
328 nread = read(srcfd, pbuf, sb.st_size);
331 LOGE("Cannot read \"%s\": %s", config_file, strerror(errno));
336 if (!strcmp(config_file, SUPP_CONFIG_FILE)) {
337 property_get("wifi.interface", ifc, WIFI_TEST_INTERFACE);
339 strcpy(ifc, CONTROL_IFACE_PATH);
341 if ((sptr = strstr(pbuf, "ctrl_interface="))) {
342 char *iptr = sptr + strlen("ctrl_interface=");
344 int mlen = strlen(ifc);
346 if (strncmp(ifc, iptr, mlen) != 0) {
347 LOGE("ctrl_interface != %s", ifc);
348 while (((ilen + (iptr - pbuf)) < nread) && (iptr[ilen] != '\n'))
350 mlen = ((ilen >= mlen) ? ilen : mlen) + 1;
351 memmove(iptr + mlen, iptr + ilen + 1, nread - (iptr + ilen + 1 - pbuf));
352 memset(iptr, '\n', mlen);
353 memcpy(iptr, ifc, strlen(ifc));
354 destfd = open(config_file, O_RDWR, 0660);
356 LOGE("Cannot update \"%s\": %s", config_file, strerror(errno));
360 write(destfd, pbuf, nread + mlen - ilen -1);
368 int ensure_config_file_exists(const char *config_file)
376 ret = access(config_file, R_OK|W_OK);
377 if ((ret == 0) || (errno == EACCES)) {
379 (chmod(config_file, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) != 0)) {
380 LOGE("Cannot set RW to \"%s\": %s", config_file, strerror(errno));
383 /* return if filesize is at least 10 bytes */
384 if (stat(config_file, &sb) == 0 && sb.st_size > 10) {
385 return update_ctrl_interface(config_file);
387 } else if (errno != ENOENT) {
388 LOGE("Cannot access \"%s\": %s", config_file, strerror(errno));
392 srcfd = open(SUPP_CONFIG_TEMPLATE, O_RDONLY);
394 LOGE("Cannot open \"%s\": %s", SUPP_CONFIG_TEMPLATE, strerror(errno));
398 destfd = open(config_file, O_CREAT|O_RDWR, 0660);
401 LOGE("Cannot create \"%s\": %s", config_file, strerror(errno));
405 while ((nread = read(srcfd, buf, sizeof(buf))) != 0) {
407 LOGE("Error reading \"%s\": %s", SUPP_CONFIG_TEMPLATE, strerror(errno));
413 write(destfd, buf, nread);
419 /* chmod is needed because open() didn't set permisions properly */
420 if (chmod(config_file, 0660) < 0) {
421 LOGE("Error changing permissions of %s to 0660: %s",
422 config_file, strerror(errno));
427 if (chown(config_file, AID_SYSTEM, AID_WIFI) < 0) {
428 LOGE("Error changing group ownership of %s to %d: %s",
429 config_file, AID_WIFI, strerror(errno));
433 return update_ctrl_interface(config_file);
437 * wifi_wpa_ctrl_cleanup() - Delete any local UNIX domain socket files that
438 * may be left over from clients that were previously connected to
439 * wpa_supplicant. This keeps these files from being orphaned in the
440 * event of crashes that prevented them from being removed as part
441 * of the normal orderly shutdown.
443 void wifi_wpa_ctrl_cleanup(void)
447 struct dirent *result;
450 char pathname[PATH_MAX];
452 char *local_socket_dir = CONFIG_CTRL_IFACE_CLIENT_DIR;
453 char *local_socket_prefix = CONFIG_CTRL_IFACE_CLIENT_PREFIX;
455 if ((dir = opendir(local_socket_dir)) == NULL)
458 dirnamelen = (size_t)snprintf(pathname, sizeof(pathname), "%s/", local_socket_dir);
459 if (dirnamelen >= sizeof(pathname)) {
463 namep = pathname + dirnamelen;
464 maxcopy = PATH_MAX - dirnamelen;
465 while (readdir_r(dir, &entry, &result) == 0 && result != NULL) {
466 if (strncmp(entry.d_name, local_socket_prefix, strlen(local_socket_prefix)) == 0) {
467 if (strlcpy(namep, entry.d_name, maxcopy) < maxcopy) {
475 int wifi_start_supplicant_common(const char *config_file)
477 char daemon_cmd[PROPERTY_VALUE_MAX];
478 char supp_status[PROPERTY_VALUE_MAX] = {'\0'};
479 int count = 200; /* wait at most 20 seconds for completion */
480 #ifdef HAVE_LIBC_SYSTEM_PROPERTIES
485 /* Check whether already running */
486 if (property_get(SUPP_PROP_NAME, supp_status, NULL)
487 && strcmp(supp_status, "running") == 0) {
491 /* Before starting the daemon, make sure its config file exists */
492 if (ensure_config_file_exists(config_file) < 0) {
493 LOGE("Wi-Fi will not be enabled");
497 if (ensure_entropy_file_exists() < 0) {
498 LOGE("Wi-Fi entropy file was not created");
501 /* Clear out any stale socket files that might be left over. */
502 wifi_wpa_ctrl_cleanup();
504 #ifdef HAVE_LIBC_SYSTEM_PROPERTIES
506 * Get a reference to the status property, so we can distinguish
507 * the case where it goes stopped => running => stopped (i.e.,
508 * it start up, but fails right away) from the case in which
509 * it starts in the stopped state and never manages to start
512 pi = __system_property_find(SUPP_PROP_NAME);
517 property_get("wifi.interface", iface, WIFI_TEST_INTERFACE);
518 snprintf(daemon_cmd, PROPERTY_VALUE_MAX, "%s:-i%s -c%s", SUPPLICANT_NAME, iface, config_file);
519 property_set("ctl.start", daemon_cmd);
522 while (count-- > 0) {
523 #ifdef HAVE_LIBC_SYSTEM_PROPERTIES
525 pi = __system_property_find(SUPP_PROP_NAME);
528 __system_property_read(pi, NULL, supp_status);
529 if (strcmp(supp_status, "running") == 0) {
531 } else if (pi->serial != serial &&
532 strcmp(supp_status, "stopped") == 0) {
537 if (property_get(SUPP_PROP_NAME, supp_status, NULL)) {
538 if (strcmp(supp_status, "running") == 0)
547 int wifi_start_supplicant()
549 return wifi_start_supplicant_common(SUPP_CONFIG_FILE);
552 int wifi_start_p2p_supplicant()
554 return wifi_start_supplicant_common(P2P_CONFIG_FILE);
557 int wifi_stop_supplicant()
559 char supp_status[PROPERTY_VALUE_MAX] = {'\0'};
560 int count = 50; /* wait at most 5 seconds for completion */
562 /* Check whether supplicant already stopped */
563 if (property_get(SUPP_PROP_NAME, supp_status, NULL)
564 && strcmp(supp_status, "stopped") == 0) {
568 property_set("ctl.stop", SUPPLICANT_NAME);
571 while (count-- > 0) {
572 if (property_get(SUPP_PROP_NAME, supp_status, NULL)) {
573 if (strcmp(supp_status, "stopped") == 0)
581 int wifi_connect_to_supplicant()
584 char supp_status[PROPERTY_VALUE_MAX] = {'\0'};
586 /* Make sure supplicant is running */
587 if (!property_get(SUPP_PROP_NAME, supp_status, NULL)
588 || strcmp(supp_status, "running") != 0) {
589 LOGE("Supplicant not running, cannot connect");
593 if (access(IFACE_DIR, F_OK) == 0) {
594 snprintf(ifname, sizeof(ifname), "%s/%s", IFACE_DIR, iface);
596 strlcpy(ifname, iface, sizeof(ifname));
599 ctrl_conn = wpa_ctrl_open(ifname);
600 if (ctrl_conn == NULL) {
601 LOGE("Unable to open connection to supplicant on \"%s\": %s",
602 ifname, strerror(errno));
605 monitor_conn = wpa_ctrl_open(ifname);
606 if (monitor_conn == NULL) {
607 wpa_ctrl_close(ctrl_conn);
611 if (wpa_ctrl_attach(monitor_conn) != 0) {
612 wpa_ctrl_close(monitor_conn);
613 wpa_ctrl_close(ctrl_conn);
614 ctrl_conn = monitor_conn = NULL;
618 if (socketpair(AF_UNIX, SOCK_STREAM, 0, exit_sockets) == -1) {
619 wpa_ctrl_close(monitor_conn);
620 wpa_ctrl_close(ctrl_conn);
621 ctrl_conn = monitor_conn = NULL;
628 int wifi_send_command(struct wpa_ctrl *ctrl, const char *cmd, char *reply, size_t *reply_len)
632 if (ctrl_conn == NULL) {
633 LOGV("Not connected to wpa_supplicant - \"%s\" command dropped.\n", cmd);
636 ret = wpa_ctrl_request(ctrl, cmd, strlen(cmd), reply, reply_len, NULL);
638 LOGD("'%s' command timed out.\n", cmd);
639 /* unblocks the monitor receive socket for termination */
640 write(exit_sockets[0], "T", 1);
642 } else if (ret < 0 || strncmp(reply, "FAIL", 4) == 0) {
645 if (strncmp(cmd, "PING", 4) == 0) {
646 reply[*reply_len] = '\0';
651 int wifi_ctrl_recv(struct wpa_ctrl *ctrl, char *reply, size_t *reply_len)
654 int ctrlfd = wpa_ctrl_get_fd(ctrl);
655 struct pollfd rfds[2];
657 memset(rfds, 0, 2 * sizeof(struct pollfd));
659 rfds[0].events |= POLLIN;
660 rfds[1].fd = exit_sockets[1];
661 rfds[1].events |= POLLIN;
662 res = poll(rfds, 2, -1);
664 LOGE("Error poll = %d", res);
667 if (rfds[0].revents & POLLIN) {
668 return wpa_ctrl_recv(ctrl, reply, reply_len);
670 LOGD("Received on exit socket, terminate");
676 int wifi_wait_for_event(char *buf, size_t buflen)
678 size_t nread = buflen - 1;
683 struct timeval *tptr;
685 if (monitor_conn == NULL) {
686 LOGD("Connection closed\n");
687 strncpy(buf, WPA_EVENT_TERMINATING " - connection closed", buflen-1);
688 buf[buflen-1] = '\0';
692 result = wifi_ctrl_recv(monitor_conn, buf, &nread);
694 LOGD("wifi_ctrl_recv failed: %s\n", strerror(errno));
695 strncpy(buf, WPA_EVENT_TERMINATING " - recv error", buflen-1);
696 buf[buflen-1] = '\0';
700 /* LOGD("wait_for_event: result=%d nread=%d string=\"%s\"\n", result, nread, buf); */
701 /* Check for EOF on the socket */
702 if (result == 0 && nread == 0) {
703 /* Fabricate an event to pass up */
704 LOGD("Received EOF on supplicant socket\n");
705 strncpy(buf, WPA_EVENT_TERMINATING " - signal 0 received", buflen-1);
706 buf[buflen-1] = '\0';
710 * Events strings are in the format
714 * where N is the message level in numerical form (0=VERBOSE, 1=DEBUG,
715 * etc.) and XXX is the event name. The level information is not useful
716 * to us, so strip it off.
719 char *match = strchr(buf, '>');
721 nread -= (match+1-buf);
722 memmove(buf, match+1, nread+1);
728 void wifi_close_supplicant_connection()
730 char supp_status[PROPERTY_VALUE_MAX] = {'\0'};
731 int count = 50; /* wait at most 5 seconds to ensure init has stopped stupplicant */
733 if (ctrl_conn != NULL) {
734 wpa_ctrl_close(ctrl_conn);
737 if (monitor_conn != NULL) {
738 wpa_ctrl_close(monitor_conn);
742 if (exit_sockets[0] >= 0) {
743 close(exit_sockets[0]);
744 exit_sockets[0] = -1;
747 if (exit_sockets[1] >= 0) {
748 close(exit_sockets[1]);
749 exit_sockets[1] = -1;
752 while (count-- > 0) {
753 if (property_get(SUPP_PROP_NAME, supp_status, NULL)) {
754 if (strcmp(supp_status, "stopped") == 0)
761 int wifi_command(const char *command, char *reply, size_t *reply_len)
763 return wifi_send_command(ctrl_conn, command, reply, reply_len);
766 const char *wifi_get_fw_path(int fw_type)
769 case WIFI_GET_FW_PATH_STA:
770 return WIFI_DRIVER_FW_PATH_STA;
771 case WIFI_GET_FW_PATH_AP:
772 return WIFI_DRIVER_FW_PATH_AP;
773 case WIFI_GET_FW_PATH_P2P:
774 return WIFI_DRIVER_FW_PATH_P2P;
779 int wifi_change_fw_path(const char *fwpath)
787 fd = open(WIFI_DRIVER_FW_PATH_PARAM, O_WRONLY);
789 LOGE("Failed to open wlan fw path param (%s)", strerror(errno));
792 len = strlen(fwpath) + 1;
793 if (write(fd, fwpath, len) != len) {
794 LOGE("Failed to write wlan fw path param (%s)", strerror(errno));