1 /* 2 * Copyright 2008, The Android Open Source Project 3 * 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 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 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. 15 */ 16 17 #include <stdlib.h> 18 #include <fcntl.h> 19 #include <errno.h> 20 #include <string.h> 21 22 #include "hardware_legacy/wifi.h" 23 #include "libwpa_client/wpa_ctrl.h" 24 25 #define LOG_TAG "WifiHW" 26 #include "cutils/log.h" 27 #include "cutils/memory.h" 28 #include "cutils/misc.h" 29 #include "cutils/properties.h" 30 #include "private/android_filesystem_config.h" 31 #ifdef HAVE_LIBC_SYSTEM_PROPERTIES 32 #define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_ 33 #include <sys/_system_properties.h> 34 #endif 35 36 static struct wpa_ctrl *ctrl_conn; 37 static struct wpa_ctrl *monitor_conn; 38 39 extern int do_dhcp(); 40 extern int ifc_init(); 41 extern void ifc_close(); 42 extern char *dhcp_lasterror(); 43 extern void get_dhcp_info(); 44 extern int init_module(void *, unsigned long, const char *); 45 extern int delete_module(const char *, unsigned int); 46 47 static char iface[PROPERTY_VALUE_MAX]; 48 // TODO: use new ANDROID_SOCKET mechanism, once support for multiple 49 // sockets is in 50 51 #ifndef WIFI_DRIVER_MODULE_PATH 52 #define WIFI_DRIVER_MODULE_PATH "/system/lib/modules/wlan.ko" 53 #endif 54 #ifndef WIFI_DRIVER_MODULE_NAME 55 #define WIFI_DRIVER_MODULE_NAME "wlan" 56 #endif 57 #ifndef WIFI_DRIVER_MODULE_ARG 58 #define WIFI_DRIVER_MODULE_ARG "" 59 #endif 60 #ifndef WIFI_FIRMWARE_LOADER 61 #define WIFI_FIRMWARE_LOADER "" 62 #endif 63 #define WIFI_TEST_INTERFACE "sta" 64 65 #define WIFI_DRIVER_LOADER_DELAY 1000000 66 67 static const char IFACE_DIR[] = "/data/system/wpa_supplicant"; 68 static const char DRIVER_MODULE_NAME[] = WIFI_DRIVER_MODULE_NAME; 69 static const char DRIVER_MODULE_TAG[] = WIFI_DRIVER_MODULE_NAME " "; 70 static const char DRIVER_MODULE_PATH[] = WIFI_DRIVER_MODULE_PATH; 71 static const char DRIVER_MODULE_ARG[] = WIFI_DRIVER_MODULE_ARG; 72 static const char FIRMWARE_LOADER[] = WIFI_FIRMWARE_LOADER; 73 static const char DRIVER_PROP_NAME[] = "wlan.driver.status"; 74 static const char SUPPLICANT_NAME[] = "wpa_supplicant"; 75 static const char SUPP_PROP_NAME[] = "init.svc.wpa_supplicant"; 76 static const char SUPP_CONFIG_TEMPLATE[]= "/system/etc/wifi/wpa_supplicant.conf"; 77 static const char SUPP_CONFIG_FILE[] = "/data/misc/wifi/wpa_supplicant.conf"; 78 static const char MODULE_FILE[] = "/proc/modules"; 79 80 static int insmod(const char *filename, const char *args) 81 { 82 void *module; 83 unsigned int size; 84 int ret; 85 86 module = load_file(filename, &size); 87 if (!module) 88 return -1; 89 90 ret = init_module(module, size, args); 91 92 free(module); 93 94 return ret; 95 } 96 97 static int rmmod(const char *modname) 98 { 99 int ret = -1; 100 int maxtry = 10; 101 102 while (maxtry-- > 0) { 103 ret = delete_module(modname, O_NONBLOCK | O_EXCL); 104 if (ret < 0 && errno == EAGAIN) 105 usleep(500000); 106 else 107 break; 108 } 109 110 if (ret != 0) 111 LOGD("Unable to unload driver module \"%s\": %s\n", 112 modname, strerror(errno)); 113 return ret; 114 } 115 116 int do_dhcp_request(int *ipaddr, int *gateway, int *mask, 117 int *dns1, int *dns2, int *server, int *lease) { 118 /* For test driver, always report success */ 119 if (strcmp(iface, WIFI_TEST_INTERFACE) == 0) 120 return 0; 121 122 if (ifc_init() < 0) 123 return -1; 124 125 if (do_dhcp(iface) < 0) { 126 ifc_close(); 127 return -1; 128 } 129 ifc_close(); 130 get_dhcp_info(ipaddr, gateway, mask, dns1, dns2, server, lease); 131 return 0; 132 } 133 134 const char *get_dhcp_error_string() { 135 return dhcp_lasterror(); 136 } 137 138 static int check_driver_loaded() { 139 char driver_status[PROPERTY_VALUE_MAX]; 140 FILE *proc; 141 char line[sizeof(DRIVER_MODULE_TAG)+10]; 142 143 if (!property_get(DRIVER_PROP_NAME, driver_status, NULL) 144 || strcmp(driver_status, "ok") != 0) { 145 return 0; /* driver not loaded */ 146 } 147 /* 148 * If the property says the driver is loaded, check to 149 * make sure that the property setting isn't just left 150 * over from a previous manual shutdown or a runtime 151 * crash. 152 */ 153 if ((proc = fopen(MODULE_FILE, "r")) == NULL) { 154 LOGW("Could not open %s: %s", MODULE_FILE, strerror(errno)); 155 property_set(DRIVER_PROP_NAME, "unloaded"); 156 return 0; 157 } 158 while ((fgets(line, sizeof(line), proc)) != NULL) { 159 if (strncmp(line, DRIVER_MODULE_TAG, strlen(DRIVER_MODULE_TAG)) == 0) { 160 fclose(proc); 161 return 1; 162 } 163 } 164 fclose(proc); 165 property_set(DRIVER_PROP_NAME, "unloaded"); 166 return 0; 167 } 168 169 int wifi_load_driver() 170 { 171 char driver_status[PROPERTY_VALUE_MAX]; 172 int count = 100; /* wait at most 20 seconds for completion */ 173 174 if (check_driver_loaded()) { 175 return 0; 176 } 177 178 if (insmod(DRIVER_MODULE_PATH, DRIVER_MODULE_ARG) < 0) 179 return -1; 180 181 if (strcmp(FIRMWARE_LOADER,"") == 0) { 182 usleep(WIFI_DRIVER_LOADER_DELAY); 183 property_set(DRIVER_PROP_NAME, "ok"); 184 } 185 else { 186 property_set("ctl.start", FIRMWARE_LOADER); 187 } 188 sched_yield(); 189 while (count-- > 0) { 190 if (property_get(DRIVER_PROP_NAME, driver_status, NULL)) { 191 if (strcmp(driver_status, "ok") == 0) 192 return 0; 193 else if (strcmp(DRIVER_PROP_NAME, "failed") == 0) { 194 wifi_unload_driver(); 195 return -1; 196 } 197 } 198 usleep(200000); 199 } 200 property_set(DRIVER_PROP_NAME, "timeout"); 201 wifi_unload_driver(); 202 return -1; 203 } 204 205 int wifi_unload_driver() 206 { 207 int count = 20; /* wait at most 10 seconds for completion */ 208 209 if (rmmod(DRIVER_MODULE_NAME) == 0) { 210 while (count-- > 0) { 211 if (!check_driver_loaded()) 212 break; 213 usleep(500000); 214 } 215 if (count) { 216 return 0; 217 } 218 return -1; 219 } else 220 return -1; 221 } 222 223 int ensure_config_file_exists() 224 { 225 char buf[2048]; 226 int srcfd, destfd; 227 int nread; 228 229 if (access(SUPP_CONFIG_FILE, R_OK|W_OK) == 0) { 230 return 0; 231 } else if (errno != ENOENT) { 232 LOGE("Cannot access \"%s\": %s", SUPP_CONFIG_FILE, strerror(errno)); 233 return -1; 234 } 235 236 srcfd = open(SUPP_CONFIG_TEMPLATE, O_RDONLY); 237 if (srcfd < 0) { 238 LOGE("Cannot open \"%s\": %s", SUPP_CONFIG_TEMPLATE, strerror(errno)); 239 return -1; 240 } 241 242 destfd = open(SUPP_CONFIG_FILE, O_CREAT|O_WRONLY, 0660); 243 if (destfd < 0) { 244 close(srcfd); 245 LOGE("Cannot create \"%s\": %s", SUPP_CONFIG_FILE, strerror(errno)); 246 return -1; 247 } 248 249 while ((nread = read(srcfd, buf, sizeof(buf))) != 0) { 250 if (nread < 0) { 251 LOGE("Error reading \"%s\": %s", SUPP_CONFIG_TEMPLATE, strerror(errno)); 252 close(srcfd); 253 close(destfd); 254 unlink(SUPP_CONFIG_FILE); 255 return -1; 256 } 257 write(destfd, buf, nread); 258 } 259 260 close(destfd); 261 close(srcfd); 262 263 if (chown(SUPP_CONFIG_FILE, AID_SYSTEM, AID_WIFI) < 0) { 264 LOGE("Error changing group ownership of %s to %d: %s", 265 SUPP_CONFIG_FILE, AID_WIFI, strerror(errno)); 266 unlink(SUPP_CONFIG_FILE); 267 return -1; 268 } 269 return 0; 270 } 271 272 int wifi_start_supplicant() 273 { 274 char supp_status[PROPERTY_VALUE_MAX] = {'\0'}; 275 int count = 200; /* wait at most 20 seconds for completion */ 276 #ifdef HAVE_LIBC_SYSTEM_PROPERTIES 277 const prop_info *pi; 278 unsigned serial = 0; 279 #endif 280 281 /* Check whether already running */ 282 if (property_get(SUPP_PROP_NAME, supp_status, NULL) 283 && strcmp(supp_status, "running") == 0) { 284 return 0; 285 } 286 287 /* Before starting the daemon, make sure its config file exists */ 288 if (ensure_config_file_exists() < 0) { 289 LOGE("Wi-Fi will not be enabled"); 290 return -1; 291 } 292 293 /* Clear out any stale socket files that might be left over. */ 294 wpa_ctrl_cleanup(); 295 296 #ifdef HAVE_LIBC_SYSTEM_PROPERTIES 297 /* 298 * Get a reference to the status property, so we can distinguish 299 * the case where it goes stopped => running => stopped (i.e., 300 * it start up, but fails right away) from the case in which 301 * it starts in the stopped state and never manages to start 302 * running at all. 303 */ 304 pi = __system_property_find(SUPP_PROP_NAME); 305 if (pi != NULL) { 306 serial = pi->serial; 307 } 308 #endif 309 property_set("ctl.start", SUPPLICANT_NAME); 310 sched_yield(); 311 312 while (count-- > 0) { 313 #ifdef HAVE_LIBC_SYSTEM_PROPERTIES 314 if (pi == NULL) { 315 pi = __system_property_find(SUPP_PROP_NAME); 316 } 317 if (pi != NULL) { 318 __system_property_read(pi, NULL, supp_status); 319 if (strcmp(supp_status, "running") == 0) { 320 return 0; 321 } else if (pi->serial != serial && 322 strcmp(supp_status, "stopped") == 0) { 323 return -1; 324 } 325 } 326 #else 327 if (property_get(SUPP_PROP_NAME, supp_status, NULL)) { 328 if (strcmp(supp_status, "running") == 0) 329 return 0; 330 } 331 #endif 332 usleep(100000); 333 } 334 return -1; 335 } 336 337 int wifi_stop_supplicant() 338 { 339 char supp_status[PROPERTY_VALUE_MAX] = {'\0'}; 340 int count = 50; /* wait at most 5 seconds for completion */ 341 342 /* Check whether supplicant already stopped */ 343 if (property_get(SUPP_PROP_NAME, supp_status, NULL) 344 && strcmp(supp_status, "stopped") == 0) { 345 return 0; 346 } 347 348 property_set("ctl.stop", SUPPLICANT_NAME); 349 sched_yield(); 350 351 while (count-- > 0) { 352 if (property_get(SUPP_PROP_NAME, supp_status, NULL)) { 353 if (strcmp(supp_status, "stopped") == 0) 354 return 0; 355 } 356 usleep(100000); 357 } 358 return -1; 359 } 360 361 int wifi_connect_to_supplicant() 362 { 363 char ifname[256]; 364 char supp_status[PROPERTY_VALUE_MAX] = {'\0'}; 365 366 /* Make sure supplicant is running */ 367 if (!property_get(SUPP_PROP_NAME, supp_status, NULL) 368 || strcmp(supp_status, "running") != 0) { 369 LOGE("Supplicant not running, cannot connect"); 370 return -1; 371 } 372 373 property_get("wifi.interface", iface, WIFI_TEST_INTERFACE); 374 375 if (access(IFACE_DIR, F_OK) == 0) { 376 snprintf(ifname, sizeof(ifname), "%s/%s", IFACE_DIR, iface); 377 } else { 378 strlcpy(ifname, iface, sizeof(ifname)); 379 } 380 381 ctrl_conn = wpa_ctrl_open(ifname); 382 if (ctrl_conn == NULL) { 383 LOGE("Unable to open connection to supplicant on \"%s\": %s", 384 ifname, strerror(errno)); 385 return -1; 386 } 387 monitor_conn = wpa_ctrl_open(ifname); 388 if (monitor_conn == NULL) { 389 wpa_ctrl_close(ctrl_conn); 390 ctrl_conn = NULL; 391 return -1; 392 } 393 if (wpa_ctrl_attach(monitor_conn) != 0) { 394 wpa_ctrl_close(monitor_conn); 395 wpa_ctrl_close(ctrl_conn); 396 ctrl_conn = monitor_conn = NULL; 397 return -1; 398 } 399 return 0; 400 } 401 402 int wifi_send_command(struct wpa_ctrl *ctrl, const char *cmd, char *reply, size_t *reply_len) 403 { 404 int ret; 405 406 if (ctrl_conn == NULL) { 407 LOGV("Not connected to wpa_supplicant - \"%s\" command dropped.\n", cmd); 408 return -1; 409 } 410 ret = wpa_ctrl_request(ctrl, cmd, strlen(cmd), reply, reply_len, NULL); 411 if (ret == -2) { 412 LOGD("'%s' command timed out.\n", cmd); 413 return -2; 414 } else if (ret < 0 || strncmp(reply, "FAIL", 4) == 0) { 415 return -1; 416 } 417 if (strncmp(cmd, "PING", 4) == 0) { 418 reply[*reply_len] = '\0'; 419 } 420 return 0; 421 } 422 423 int wifi_wait_for_event(char *buf, size_t buflen) 424 { 425 size_t nread = buflen - 1; 426 int fd; 427 fd_set rfds; 428 int result; 429 struct timeval tval; 430 struct timeval *tptr; 431 432 if (monitor_conn == NULL) { 433 LOGD("Connection closed\n"); 434 strncpy(buf, WPA_EVENT_TERMINATING " - connection closed", buflen-1); 435 buf[buflen-1] = '\0'; 436 return strlen(buf); 437 } 438 439 result = wpa_ctrl_recv(monitor_conn, buf, &nread); 440 if (result < 0) { 441 LOGD("wpa_ctrl_recv failed: %s\n", strerror(errno)); 442 strncpy(buf, WPA_EVENT_TERMINATING " - recv error", buflen-1); 443 buf[buflen-1] = '\0'; 444 return strlen(buf); 445 } 446 buf[nread] = '\0'; 447 /* LOGD("wait_for_event: result=%d nread=%d string=\"%s\"\n", result, nread, buf); */ 448 /* Check for EOF on the socket */ 449 if (result == 0 && nread == 0) { 450 /* Fabricate an event to pass up */ 451 LOGD("Received EOF on supplicant socket\n"); 452 strncpy(buf, WPA_EVENT_TERMINATING " - signal 0 received", buflen-1); 453 buf[buflen-1] = '\0'; 454 return strlen(buf); 455 } 456 /* 457 * Events strings are in the format 458 * 459 * <N>CTRL-EVENT-XXX 460 * 461 * where N is the message level in numerical form (0=VERBOSE, 1=DEBUG, 462 * etc.) and XXX is the event name. The level information is not useful 463 * to us, so strip it off. 464 */ 465 if (buf[0] == '<') { 466 char *match = strchr(buf, '>'); 467 if (match != NULL) { 468 nread -= (match+1-buf); 469 memmove(buf, match+1, nread+1); 470 } 471 } 472 return nread; 473 } 474 475 void wifi_close_supplicant_connection() 476 { 477 if (ctrl_conn != NULL) { 478 wpa_ctrl_close(ctrl_conn); 479 ctrl_conn = NULL; 480 } 481 if (monitor_conn != NULL) { 482 wpa_ctrl_close(monitor_conn); 483 monitor_conn = NULL; 484 } 485 } 486 487 int wifi_command(const char *command, char *reply, size_t *reply_len) 488 { 489 return wifi_send_command(ctrl_conn, command, reply, reply_len); 490 } 491