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.
23 #include "hardware_legacy/wifi.h"
24 #include "libwpa_client/wpa_ctrl.h"
26 #define LOG_TAG "WifiHW"
27 #include "cutils/log.h"
28 #include "cutils/memory.h"
29 #include "cutils/misc.h"
30 #include "cutils/properties.h"
31 #include "private/android_filesystem_config.h"
32 #ifdef HAVE_LIBC_SYSTEM_PROPERTIES
33 #define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
34 #include <sys/_system_properties.h>
37 static struct wpa_ctrl *ctrl_conn;
38 static struct wpa_ctrl *monitor_conn;
41 extern int ifc_init();
42 extern void ifc_close();
43 extern char *dhcp_lasterror();
44 extern void get_dhcp_info();
45 extern int init_module(void *, unsigned long, const char *);
46 extern int delete_module(const char *, unsigned int);
48 static char iface[PROPERTY_VALUE_MAX];
49 // TODO: use new ANDROID_SOCKET mechanism, once support for multiple
52 #ifndef WIFI_DRIVER_MODULE_ARG
53 #define WIFI_DRIVER_MODULE_ARG ""
55 #ifndef WIFI_FIRMWARE_LOADER
56 #define WIFI_FIRMWARE_LOADER ""
58 #define WIFI_TEST_INTERFACE "sta"
60 #ifndef WIFI_DRIVER_FW_PATH_STA
61 #define WIFI_DRIVER_FW_PATH_STA NULL
63 #ifndef WIFI_DRIVER_FW_PATH_AP
64 #define WIFI_DRIVER_FW_PATH_AP NULL
66 #ifndef WIFI_DRIVER_FW_PATH_P2P
67 #define WIFI_DRIVER_FW_PATH_P2P NULL
70 #ifndef WIFI_DRIVER_FW_PATH_PARAM
71 #define WIFI_DRIVER_FW_PATH_PARAM "/sys/module/wlan/parameters/fwpath"
74 #define WIFI_DRIVER_LOADER_DELAY 1000000
76 static const char IFACE_DIR[] = "/data/system/wpa_supplicant";
77 #ifdef WIFI_DRIVER_MODULE_PATH
78 static const char DRIVER_MODULE_NAME[] = WIFI_DRIVER_MODULE_NAME;
79 static const char DRIVER_MODULE_TAG[] = WIFI_DRIVER_MODULE_NAME " ";
80 static const char DRIVER_MODULE_PATH[] = WIFI_DRIVER_MODULE_PATH;
81 static const char DRIVER_MODULE_ARG[] = WIFI_DRIVER_MODULE_ARG;
83 static const char FIRMWARE_LOADER[] = WIFI_FIRMWARE_LOADER;
84 static const char DRIVER_PROP_NAME[] = "wlan.driver.status";
85 static const char SUPPLICANT_NAME[] = "wpa_supplicant";
86 static const char SUPP_PROP_NAME[] = "init.svc.wpa_supplicant";
87 static const char SUPP_CONFIG_TEMPLATE[]= "/system/etc/wifi/wpa_supplicant.conf";
88 static const char SUPP_CONFIG_FILE[] = "/data/misc/wifi/wpa_supplicant.conf";
89 static const char MODULE_FILE[] = "/proc/modules";
91 static const char SUPP_ENTROPY_FILE[] = WIFI_ENTROPY_FILE;
92 static unsigned char dummy_key[20] = { 0x11, 0xbe, 0x33, 0x43, 0x35,
93 0x68, 0x47, 0x84, 0x99, 0xa9,
94 0x2b, 0x1c, 0xd3, 0xee, 0xff,
95 0xf1, 0xe2, 0xf3, 0xf4, 0xf5 };
97 static int insmod(const char *filename, const char *args)
103 module = load_file(filename, &size);
107 ret = init_module(module, size, args);
114 static int rmmod(const char *modname)
119 while (maxtry-- > 0) {
120 ret = delete_module(modname, O_NONBLOCK | O_EXCL);
121 if (ret < 0 && errno == EAGAIN)
128 LOGD("Unable to unload driver module \"%s\": %s\n",
129 modname, strerror(errno));
133 int do_dhcp_request(int *ipaddr, int *gateway, int *mask,
134 int *dns1, int *dns2, int *server, int *lease) {
135 /* For test driver, always report success */
136 if (strcmp(iface, WIFI_TEST_INTERFACE) == 0)
142 if (do_dhcp(iface) < 0) {
147 get_dhcp_info(ipaddr, gateway, mask, dns1, dns2, server, lease);
151 const char *get_dhcp_error_string() {
152 return dhcp_lasterror();
155 int is_wifi_driver_loaded() {
156 char driver_status[PROPERTY_VALUE_MAX];
157 #ifdef WIFI_DRIVER_MODULE_PATH
159 char line[sizeof(DRIVER_MODULE_TAG)+10];
162 if (!property_get(DRIVER_PROP_NAME, driver_status, NULL)
163 || strcmp(driver_status, "ok") != 0) {
164 return 0; /* driver not loaded */
166 #ifdef WIFI_DRIVER_MODULE_PATH
168 * If the property says the driver is loaded, check to
169 * make sure that the property setting isn't just left
170 * over from a previous manual shutdown or a runtime
173 if ((proc = fopen(MODULE_FILE, "r")) == NULL) {
174 LOGW("Could not open %s: %s", MODULE_FILE, strerror(errno));
175 property_set(DRIVER_PROP_NAME, "unloaded");
178 while ((fgets(line, sizeof(line), proc)) != NULL) {
179 if (strncmp(line, DRIVER_MODULE_TAG, strlen(DRIVER_MODULE_TAG)) == 0) {
185 property_set(DRIVER_PROP_NAME, "unloaded");
192 int wifi_load_driver()
194 #ifdef WIFI_DRIVER_MODULE_PATH
195 char driver_status[PROPERTY_VALUE_MAX];
196 int count = 100; /* wait at most 20 seconds for completion */
198 if (is_wifi_driver_loaded()) {
202 if (insmod(DRIVER_MODULE_PATH, DRIVER_MODULE_ARG) < 0)
205 if (strcmp(FIRMWARE_LOADER,"") == 0) {
206 /* usleep(WIFI_DRIVER_LOADER_DELAY); */
207 property_set(DRIVER_PROP_NAME, "ok");
210 property_set("ctl.start", FIRMWARE_LOADER);
213 while (count-- > 0) {
214 if (property_get(DRIVER_PROP_NAME, driver_status, NULL)) {
215 if (strcmp(driver_status, "ok") == 0)
217 else if (strcmp(DRIVER_PROP_NAME, "failed") == 0) {
218 wifi_unload_driver();
224 property_set(DRIVER_PROP_NAME, "timeout");
225 wifi_unload_driver();
228 property_set(DRIVER_PROP_NAME, "ok");
233 int wifi_unload_driver()
235 usleep(200000); /* allow to finish interface down */
236 #ifdef WIFI_DRIVER_MODULE_PATH
237 if (rmmod(DRIVER_MODULE_NAME) == 0) {
238 int count = 20; /* wait at most 10 seconds for completion */
239 while (count-- > 0) {
240 if (!is_wifi_driver_loaded())
244 usleep(500000); /* allow card removal */
252 property_set(DRIVER_PROP_NAME, "unloaded");
257 int ensure_entropy_file_exists()
262 ret = access(SUPP_ENTROPY_FILE, R_OK|W_OK);
263 if ((ret == 0) || (errno == EACCES)) {
265 (chmod(SUPP_ENTROPY_FILE, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) != 0)) {
266 LOGE("Cannot set RW to \"%s\": %s", SUPP_ENTROPY_FILE, strerror(errno));
271 destfd = open(SUPP_ENTROPY_FILE, O_CREAT|O_RDWR, 0660);
273 LOGE("Cannot create \"%s\": %s", SUPP_ENTROPY_FILE, strerror(errno));
277 if (write(destfd, dummy_key, sizeof(dummy_key)) != sizeof(dummy_key)) {
278 LOGE("Error writing \"%s\": %s", SUPP_ENTROPY_FILE, strerror(errno));
284 /* chmod is needed because open() didn't set permisions properly */
285 if (chmod(SUPP_ENTROPY_FILE, 0660) < 0) {
286 LOGE("Error changing permissions of %s to 0660: %s",
287 SUPP_ENTROPY_FILE, strerror(errno));
288 unlink(SUPP_ENTROPY_FILE);
292 if (chown(SUPP_ENTROPY_FILE, AID_SYSTEM, AID_WIFI) < 0) {
293 LOGE("Error changing group ownership of %s to %d: %s",
294 SUPP_ENTROPY_FILE, AID_WIFI, strerror(errno));
295 unlink(SUPP_ENTROPY_FILE);
301 int ensure_config_file_exists()
304 char ifc[PROPERTY_VALUE_MAX];
312 ret = access(SUPP_CONFIG_FILE, R_OK|W_OK);
313 if ((ret == 0) || (errno == EACCES)) {
315 (chmod(SUPP_CONFIG_FILE, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) != 0)) {
316 LOGE("Cannot set RW to \"%s\": %s", SUPP_CONFIG_FILE, strerror(errno));
319 /* return if filesize is at least 10 bytes */
320 if (stat(SUPP_CONFIG_FILE, &sb) == 0 && sb.st_size > 10) {
321 pbuf = malloc(sb.st_size + PROPERTY_VALUE_MAX);
324 srcfd = open(SUPP_CONFIG_FILE, O_RDONLY);
326 LOGE("Cannot open \"%s\": %s", SUPP_CONFIG_FILE, strerror(errno));
330 nread = read(srcfd, pbuf, sb.st_size);
333 LOGE("Cannot read \"%s\": %s", SUPP_CONFIG_FILE, strerror(errno));
337 property_get("wifi.interface", ifc, WIFI_TEST_INTERFACE);
338 if ((sptr = strstr(pbuf, "ctrl_interface="))) {
339 char *iptr = sptr + strlen("ctrl_interface=");
341 int mlen = strlen(ifc);
342 if (strncmp(ifc, iptr, mlen) != 0) {
343 LOGE("ctrl_interface != %s", ifc);
344 while (((ilen + (iptr - pbuf)) < nread) && (iptr[ilen] != '\n'))
346 mlen = ((ilen >= mlen) ? ilen : mlen) + 1;
347 memmove(iptr + mlen, iptr + ilen + 1, nread - (iptr + ilen + 1 - pbuf));
348 memset(iptr, '\n', mlen);
349 memcpy(iptr, ifc, strlen(ifc));
350 destfd = open(SUPP_CONFIG_FILE, O_RDWR, 0660);
352 LOGE("Cannot update \"%s\": %s", SUPP_CONFIG_FILE, strerror(errno));
356 write(destfd, pbuf, nread);
363 } else if (errno != ENOENT) {
364 LOGE("Cannot access \"%s\": %s", SUPP_CONFIG_FILE, strerror(errno));
368 srcfd = open(SUPP_CONFIG_TEMPLATE, O_RDONLY);
370 LOGE("Cannot open \"%s\": %s", SUPP_CONFIG_TEMPLATE, strerror(errno));
374 destfd = open(SUPP_CONFIG_FILE, O_CREAT|O_RDWR, 0660);
377 LOGE("Cannot create \"%s\": %s", SUPP_CONFIG_FILE, strerror(errno));
381 while ((nread = read(srcfd, buf, sizeof(buf))) != 0) {
383 LOGE("Error reading \"%s\": %s", SUPP_CONFIG_TEMPLATE, strerror(errno));
386 unlink(SUPP_CONFIG_FILE);
389 write(destfd, buf, nread);
395 /* chmod is needed because open() didn't set permisions properly */
396 if (chmod(SUPP_CONFIG_FILE, 0660) < 0) {
397 LOGE("Error changing permissions of %s to 0660: %s",
398 SUPP_CONFIG_FILE, strerror(errno));
399 unlink(SUPP_CONFIG_FILE);
403 if (chown(SUPP_CONFIG_FILE, AID_SYSTEM, AID_WIFI) < 0) {
404 LOGE("Error changing group ownership of %s to %d: %s",
405 SUPP_CONFIG_FILE, AID_WIFI, strerror(errno));
406 unlink(SUPP_CONFIG_FILE);
413 * wifi_wpa_ctrl_cleanup() - Delete any local UNIX domain socket files that
414 * may be left over from clients that were previously connected to
415 * wpa_supplicant. This keeps these files from being orphaned in the
416 * event of crashes that prevented them from being removed as part
417 * of the normal orderly shutdown.
419 void wifi_wpa_ctrl_cleanup(void)
423 struct dirent *result;
426 char pathname[PATH_MAX];
428 char *local_socket_dir = CONFIG_CTRL_IFACE_CLIENT_DIR;
429 char *local_socket_prefix = CONFIG_CTRL_IFACE_CLIENT_PREFIX;
431 if ((dir = opendir(local_socket_dir)) == NULL)
434 dirnamelen = (size_t)snprintf(pathname, sizeof(pathname), "%s/", local_socket_dir);
435 if (dirnamelen >= sizeof(pathname)) {
439 namep = pathname + dirnamelen;
440 maxcopy = PATH_MAX - dirnamelen;
441 while (readdir_r(dir, &entry, &result) == 0 && result != NULL) {
442 if (strncmp(entry.d_name, local_socket_prefix, strlen(local_socket_prefix)) == 0) {
443 if (strlcpy(namep, entry.d_name, maxcopy) < maxcopy) {
451 int wifi_start_supplicant()
453 char daemon_cmd[PROPERTY_VALUE_MAX];
454 char supp_status[PROPERTY_VALUE_MAX] = {'\0'};
455 int count = 200; /* wait at most 20 seconds for completion */
456 #ifdef HAVE_LIBC_SYSTEM_PROPERTIES
461 /* Check whether already running */
462 if (property_get(SUPP_PROP_NAME, supp_status, NULL)
463 && strcmp(supp_status, "running") == 0) {
467 /* Before starting the daemon, make sure its config file exists */
468 if (ensure_config_file_exists() < 0) {
469 LOGE("Wi-Fi will not be enabled");
473 if (ensure_entropy_file_exists() < 0) {
474 LOGE("Wi-Fi entropy file was not created");
477 /* Clear out any stale socket files that might be left over. */
478 wifi_wpa_ctrl_cleanup();
480 #ifdef HAVE_LIBC_SYSTEM_PROPERTIES
482 * Get a reference to the status property, so we can distinguish
483 * the case where it goes stopped => running => stopped (i.e.,
484 * it start up, but fails right away) from the case in which
485 * it starts in the stopped state and never manages to start
488 pi = __system_property_find(SUPP_PROP_NAME);
493 property_get("wifi.interface", iface, WIFI_TEST_INTERFACE);
494 snprintf(daemon_cmd, PROPERTY_VALUE_MAX, "%s:-i%s", SUPPLICANT_NAME, iface);
495 property_set("ctl.start", daemon_cmd);
498 while (count-- > 0) {
499 #ifdef HAVE_LIBC_SYSTEM_PROPERTIES
501 pi = __system_property_find(SUPP_PROP_NAME);
504 __system_property_read(pi, NULL, supp_status);
505 if (strcmp(supp_status, "running") == 0) {
507 } else if (pi->serial != serial &&
508 strcmp(supp_status, "stopped") == 0) {
513 if (property_get(SUPP_PROP_NAME, supp_status, NULL)) {
514 if (strcmp(supp_status, "running") == 0)
523 int wifi_stop_supplicant()
525 char supp_status[PROPERTY_VALUE_MAX] = {'\0'};
526 int count = 50; /* wait at most 5 seconds for completion */
528 /* Check whether supplicant already stopped */
529 if (property_get(SUPP_PROP_NAME, supp_status, NULL)
530 && strcmp(supp_status, "stopped") == 0) {
534 property_set("ctl.stop", SUPPLICANT_NAME);
537 while (count-- > 0) {
538 if (property_get(SUPP_PROP_NAME, supp_status, NULL)) {
539 if (strcmp(supp_status, "stopped") == 0)
547 int wifi_connect_to_supplicant()
550 char supp_status[PROPERTY_VALUE_MAX] = {'\0'};
552 /* Make sure supplicant is running */
553 if (!property_get(SUPP_PROP_NAME, supp_status, NULL)
554 || strcmp(supp_status, "running") != 0) {
555 LOGE("Supplicant not running, cannot connect");
559 if (access(IFACE_DIR, F_OK) == 0) {
560 snprintf(ifname, sizeof(ifname), "%s/%s", IFACE_DIR, iface);
562 strlcpy(ifname, iface, sizeof(ifname));
565 ctrl_conn = wpa_ctrl_open(ifname);
566 if (ctrl_conn == NULL) {
567 LOGE("Unable to open connection to supplicant on \"%s\": %s",
568 ifname, strerror(errno));
571 monitor_conn = wpa_ctrl_open(ifname);
572 if (monitor_conn == NULL) {
573 wpa_ctrl_close(ctrl_conn);
577 if (wpa_ctrl_attach(monitor_conn) != 0) {
578 wpa_ctrl_close(monitor_conn);
579 wpa_ctrl_close(ctrl_conn);
580 ctrl_conn = monitor_conn = NULL;
586 int wifi_send_command(struct wpa_ctrl *ctrl, const char *cmd, char *reply, size_t *reply_len)
590 if (ctrl_conn == NULL) {
591 LOGV("Not connected to wpa_supplicant - \"%s\" command dropped.\n", cmd);
594 ret = wpa_ctrl_request(ctrl, cmd, strlen(cmd), reply, reply_len, NULL);
596 LOGD("'%s' command timed out.\n", cmd);
598 } else if (ret < 0 || strncmp(reply, "FAIL", 4) == 0) {
601 if (strncmp(cmd, "PING", 4) == 0) {
602 reply[*reply_len] = '\0';
607 int wifi_wait_for_event(char *buf, size_t buflen)
609 size_t nread = buflen - 1;
614 struct timeval *tptr;
616 if (monitor_conn == NULL) {
617 LOGD("Connection closed\n");
618 strncpy(buf, WPA_EVENT_TERMINATING " - connection closed", buflen-1);
619 buf[buflen-1] = '\0';
623 result = wpa_ctrl_recv(monitor_conn, buf, &nread);
625 LOGD("wpa_ctrl_recv failed: %s\n", strerror(errno));
626 strncpy(buf, WPA_EVENT_TERMINATING " - recv error", buflen-1);
627 buf[buflen-1] = '\0';
631 /* LOGD("wait_for_event: result=%d nread=%d string=\"%s\"\n", result, nread, buf); */
632 /* Check for EOF on the socket */
633 if (result == 0 && nread == 0) {
634 /* Fabricate an event to pass up */
635 LOGD("Received EOF on supplicant socket\n");
636 strncpy(buf, WPA_EVENT_TERMINATING " - signal 0 received", buflen-1);
637 buf[buflen-1] = '\0';
641 * Events strings are in the format
645 * where N is the message level in numerical form (0=VERBOSE, 1=DEBUG,
646 * etc.) and XXX is the event name. The level information is not useful
647 * to us, so strip it off.
650 char *match = strchr(buf, '>');
652 nread -= (match+1-buf);
653 memmove(buf, match+1, nread+1);
659 void wifi_close_supplicant_connection()
661 char supp_status[PROPERTY_VALUE_MAX] = {'\0'};
662 int count = 50; /* wait at most 5 seconds to ensure init has stopped stupplicant */
664 if (ctrl_conn != NULL) {
665 wpa_ctrl_close(ctrl_conn);
668 if (monitor_conn != NULL) {
669 wpa_ctrl_close(monitor_conn);
673 while (count-- > 0) {
674 if (property_get(SUPP_PROP_NAME, supp_status, NULL)) {
675 if (strcmp(supp_status, "stopped") == 0)
682 int wifi_command(const char *command, char *reply, size_t *reply_len)
684 return wifi_send_command(ctrl_conn, command, reply, reply_len);
687 const char *wifi_get_fw_path(int fw_type)
690 case WIFI_GET_FW_PATH_STA:
691 return WIFI_DRIVER_FW_PATH_STA;
692 case WIFI_GET_FW_PATH_AP:
693 return WIFI_DRIVER_FW_PATH_AP;
694 case WIFI_GET_FW_PATH_P2P:
695 return WIFI_DRIVER_FW_PATH_P2P;
700 int wifi_change_fw_path(const char *fwpath)
708 fd = open(WIFI_DRIVER_FW_PATH_PARAM, O_WRONLY);
710 LOGE("Failed to open wlan fw path param (%s)", strerror(errno));
713 len = strlen(fwpath) + 1;
714 if (write(fd, fwpath, len) != len) {
715 LOGE("Failed to write wlan fw path param (%s)", strerror(errno));