1 /* 2 * Copyright (C) 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 <errno.h> 19 #include <fcntl.h> 20 #include <string.h> 21 22 #include <sys/socket.h> 23 #include <sys/stat.h> 24 #include <sys/ioctl.h> 25 #include <sys/types.h> 26 #include <sys/wait.h> 27 28 #include <netinet/in.h> 29 #include <arpa/inet.h> 30 31 #include <linux/wireless.h> 32 33 #include <openssl/evp.h> 34 #include <openssl/sha.h> 35 36 #define LOG_TAG "SoftapController" 37 #include <cutils/log.h> 38 #include <netutils/ifc.h> 39 #include <private/android_filesystem_config.h> 40 #include "wifi.h" 41 #include "ResponseCode.h" 42 43 #include "SoftapController.h" 44 45 static const char HOSTAPD_CONF_FILE[] = "/data/misc/wifi/hostapd.conf"; 46 static const char HOSTAPD_BIN_FILE[] = "/system/bin/hostapd"; 47 48 SoftapController::SoftapController() 49 : mPid(0) {} 50 51 SoftapController::~SoftapController() { 52 } 53 54 int SoftapController::startSoftap() { 55 pid_t pid = 1; 56 57 if (mPid) { 58 ALOGE("SoftAP is already running"); 59 return ResponseCode::SoftapStatusResult; 60 } 61 62 if ((pid = fork()) < 0) { 63 ALOGE("fork failed (%s)", strerror(errno)); 64 return ResponseCode::ServiceStartFailed; 65 } 66 67 if (!pid) { 68 ensure_entropy_file_exists(); 69 if (execl(HOSTAPD_BIN_FILE, HOSTAPD_BIN_FILE, 70 "-e", WIFI_ENTROPY_FILE, 71 HOSTAPD_CONF_FILE, (char *) NULL)) { 72 ALOGE("execl failed (%s)", strerror(errno)); 73 } 74 ALOGE("SoftAP failed to start"); 75 return ResponseCode::ServiceStartFailed; 76 } else { 77 mPid = pid; 78 ALOGD("SoftAP started successfully"); 79 usleep(AP_BSS_START_DELAY); 80 } 81 return ResponseCode::SoftapStatusResult; 82 } 83 84 int SoftapController::stopSoftap() { 85 86 if (mPid == 0) { 87 ALOGE("SoftAP is not running"); 88 return ResponseCode::SoftapStatusResult; 89 } 90 91 ALOGD("Stopping the SoftAP service..."); 92 kill(mPid, SIGTERM); 93 waitpid(mPid, NULL, 0); 94 95 mPid = 0; 96 ALOGD("SoftAP stopped successfully"); 97 usleep(AP_BSS_STOP_DELAY); 98 return ResponseCode::SoftapStatusResult; 99 } 100 101 bool SoftapController::isSoftapStarted() { 102 return (mPid != 0); 103 } 104 105 /* 106 * Arguments: 107 * argv[2] - wlan interface 108 * argv[3] - SSID 109 * argv[4] - Broadcast/Hidden 110 * argv[5] - Channel 111 * argv[6] - Security 112 * argv[7] - Key 113 */ 114 int SoftapController::setSoftap(int argc, char *argv[]) { 115 char psk_str[2*SHA256_DIGEST_LENGTH+1]; 116 int ret = ResponseCode::SoftapStatusResult; 117 int fd; 118 int hidden = 0; 119 int channel = AP_CHANNEL_DEFAULT; 120 char *wbuf = NULL; 121 char *fbuf = NULL; 122 123 if (argc < 5) { 124 ALOGE("Softap set is missing arguments. Please use:"); 125 ALOGE("softap <wlan iface> <SSID> <hidden/broadcast> <channel> <wpa2?-psk|open> <passphrase>"); 126 return ResponseCode::CommandSyntaxError; 127 } 128 129 if (!strcasecmp(argv[4], "hidden")) 130 hidden = 1; 131 132 if (argc >= 5) { 133 channel = atoi(argv[5]); 134 if (channel <= 0) 135 channel = AP_CHANNEL_DEFAULT; 136 } 137 138 asprintf(&wbuf, "interface=%s\ndriver=nl80211\nctrl_interface=" 139 "/data/misc/wifi/hostapd\nssid=%s\nchannel=%d\nieee80211n=1\n" 140 "hw_mode=g\nignore_broadcast_ssid=%d\nwowlan_triggers=any\n", 141 argv[2], argv[3], channel, hidden); 142 143 if (argc > 7) { 144 if (!strcmp(argv[6], "wpa-psk")) { 145 generatePsk(argv[3], argv[7], psk_str); 146 asprintf(&fbuf, "%swpa=3\nwpa_pairwise=TKIP CCMP\nwpa_psk=%s\n", wbuf, psk_str); 147 } else if (!strcmp(argv[6], "wpa2-psk")) { 148 generatePsk(argv[3], argv[7], psk_str); 149 asprintf(&fbuf, "%swpa=2\nrsn_pairwise=CCMP\nwpa_psk=%s\n", wbuf, psk_str); 150 } else if (!strcmp(argv[6], "open")) { 151 asprintf(&fbuf, "%s", wbuf); 152 } 153 } else if (argc > 6) { 154 if (!strcmp(argv[6], "open")) { 155 asprintf(&fbuf, "%s", wbuf); 156 } 157 } else { 158 asprintf(&fbuf, "%s", wbuf); 159 } 160 161 fd = open(HOSTAPD_CONF_FILE, O_CREAT | O_TRUNC | O_WRONLY | O_NOFOLLOW, 0660); 162 if (fd < 0) { 163 ALOGE("Cannot update \"%s\": %s", HOSTAPD_CONF_FILE, strerror(errno)); 164 free(wbuf); 165 free(fbuf); 166 return ResponseCode::OperationFailed; 167 } 168 if (write(fd, fbuf, strlen(fbuf)) < 0) { 169 ALOGE("Cannot write to \"%s\": %s", HOSTAPD_CONF_FILE, strerror(errno)); 170 ret = ResponseCode::OperationFailed; 171 } 172 free(wbuf); 173 free(fbuf); 174 175 /* Note: apparently open can fail to set permissions correctly at times */ 176 if (fchmod(fd, 0660) < 0) { 177 ALOGE("Error changing permissions of %s to 0660: %s", 178 HOSTAPD_CONF_FILE, strerror(errno)); 179 close(fd); 180 unlink(HOSTAPD_CONF_FILE); 181 return ResponseCode::OperationFailed; 182 } 183 184 if (fchown(fd, AID_SYSTEM, AID_WIFI) < 0) { 185 ALOGE("Error changing group ownership of %s to %d: %s", 186 HOSTAPD_CONF_FILE, AID_WIFI, strerror(errno)); 187 close(fd); 188 unlink(HOSTAPD_CONF_FILE); 189 return ResponseCode::OperationFailed; 190 } 191 192 close(fd); 193 return ret; 194 } 195 196 /* 197 * Arguments: 198 * argv[2] - interface name 199 * argv[3] - AP or P2P or STA 200 */ 201 int SoftapController::fwReloadSoftap(int argc, char *argv[]) 202 { 203 char *fwpath = NULL; 204 205 if (argc < 4) { 206 ALOGE("SoftAP fwreload is missing arguments. Please use: softap <wlan iface> <AP|P2P|STA>"); 207 return ResponseCode::CommandSyntaxError; 208 } 209 210 if (strcmp(argv[3], "AP") == 0) { 211 fwpath = (char *)wifi_get_fw_path(WIFI_GET_FW_PATH_AP); 212 } else if (strcmp(argv[3], "P2P") == 0) { 213 fwpath = (char *)wifi_get_fw_path(WIFI_GET_FW_PATH_P2P); 214 } else if (strcmp(argv[3], "STA") == 0) { 215 fwpath = (char *)wifi_get_fw_path(WIFI_GET_FW_PATH_STA); 216 } 217 if (!fwpath) 218 return ResponseCode::CommandParameterError; 219 if (wifi_change_fw_path((const char *)fwpath)) { 220 ALOGE("Softap fwReload failed"); 221 return ResponseCode::OperationFailed; 222 } 223 else { 224 ALOGD("Softap fwReload - Ok"); 225 } 226 return ResponseCode::SoftapStatusResult; 227 } 228 229 void SoftapController::generatePsk(char *ssid, char *passphrase, char *psk_str) { 230 unsigned char psk[SHA256_DIGEST_LENGTH]; 231 int j; 232 // Use the PKCS#5 PBKDF2 with 4096 iterations 233 PKCS5_PBKDF2_HMAC_SHA1(passphrase, strlen(passphrase), 234 reinterpret_cast<const unsigned char *>(ssid), strlen(ssid), 235 4096, SHA256_DIGEST_LENGTH, psk); 236 for (j=0; j < SHA256_DIGEST_LENGTH; j++) { 237 sprintf(&psk_str[j*2], "%02x", psk[j]); 238 } 239 } 240