Dmitry Shmidt | 5af38c3 | 2010-02-10 11:10:39 -0800 | [diff] [blame] | 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> |
Olivier Bailly | ff2c0d8 | 2010-11-17 11:45:07 -0800 | [diff] [blame] | 20 | #include <string.h> |
Dmitry Shmidt | 5af38c3 | 2010-02-10 11:10:39 -0800 | [diff] [blame] | 21 | |
| 22 | #include <sys/socket.h> |
| 23 | #include <sys/stat.h> |
Olivier Bailly | ff2c0d8 | 2010-11-17 11:45:07 -0800 | [diff] [blame] | 24 | #include <sys/ioctl.h> |
Dmitry Shmidt | 5af38c3 | 2010-02-10 11:10:39 -0800 | [diff] [blame] | 25 | #include <sys/types.h> |
| 26 | #include <sys/wait.h> |
| 27 | |
| 28 | #include <netinet/in.h> |
| 29 | #include <arpa/inet.h> |
| 30 | |
Dmitry Shmidt | 84c65a6 | 2010-03-03 13:14:30 -0800 | [diff] [blame] | 31 | #include <linux/wireless.h> |
| 32 | |
Kenny Root | a2d7e3e | 2010-03-15 14:26:36 -0700 | [diff] [blame] | 33 | #include <openssl/evp.h> |
| 34 | #include <openssl/sha.h> |
| 35 | |
Dmitry Shmidt | 5af38c3 | 2010-02-10 11:10:39 -0800 | [diff] [blame] | 36 | #define LOG_TAG "SoftapController" |
Elliott Hughes | bbd5626 | 2015-12-04 15:45:10 -0800 | [diff] [blame] | 37 | #include <android-base/file.h> |
| 38 | #include <android-base/stringprintf.h> |
Dmitry Shmidt | 5af38c3 | 2010-02-10 11:10:39 -0800 | [diff] [blame] | 39 | #include <cutils/log.h> |
Dmitry Shmidt | 389f8d1 | 2011-07-21 15:16:04 -0700 | [diff] [blame] | 40 | #include <netutils/ifc.h> |
Irfan Sheriff | 78dcb76 | 2011-07-22 15:20:21 -0700 | [diff] [blame] | 41 | #include <private/android_filesystem_config.h> |
Dmitry Shmidt | fe15b63 | 2011-07-19 13:55:25 -0700 | [diff] [blame] | 42 | #include "wifi.h" |
Sasha Levitskiy | 25753d5 | 2013-01-10 12:48:39 -0800 | [diff] [blame] | 43 | #include "ResponseCode.h" |
Dmitry Shmidt | 5af38c3 | 2010-02-10 11:10:39 -0800 | [diff] [blame] | 44 | |
| 45 | #include "SoftapController.h" |
Ashwini Patil | 508f10f | 2015-08-07 18:05:03 +0530 | [diff] [blame] | 46 | #include <dirent.h> |
Dmitry Shmidt | 5af38c3 | 2010-02-10 11:10:39 -0800 | [diff] [blame] | 47 | |
Dan Albert | 5407e14 | 2015-03-16 10:05:59 -0700 | [diff] [blame] | 48 | using android::base::StringPrintf; |
| 49 | using android::base::WriteStringToFile; |
| 50 | |
Nalla Kartheek | cad2dc8 | 2016-08-31 12:26:14 +0530 | [diff] [blame] | 51 | #ifdef LIBWPA_CLIENT_EXISTS |
| 52 | #include <dirent.h> |
| 53 | #include "wpa_ctrl.h" |
| 54 | #endif |
| 55 | |
Lalit Kansara | d1dcf20 | 2016-12-20 01:37:02 +0530 | [diff] [blame] | 56 | std:: string hostapd_unix_file(StringPrintf("/data/misc/wifi/hostapd/wlan0")); |
Nalla Kartheek | 71feca6 | 2016-12-08 15:35:51 +0530 | [diff] [blame] | 57 | #ifdef LIBWPA_CLIENT_EXISTS |
Nalla Kartheek | cad2dc8 | 2016-08-31 12:26:14 +0530 | [diff] [blame] | 58 | static const char HOSTAPD_DHCP_DIR[] = "/data/misc/dhcp"; |
| 59 | #endif |
Irfan Sheriff | 78dcb76 | 2011-07-22 15:20:21 -0700 | [diff] [blame] | 60 | static const char HOSTAPD_CONF_FILE[] = "/data/misc/wifi/hostapd.conf"; |
Sasha Levitskiy | 25753d5 | 2013-01-10 12:48:39 -0800 | [diff] [blame] | 61 | static const char HOSTAPD_BIN_FILE[] = "/system/bin/hostapd"; |
Nalla Kartheek | 5ed655a | 2017-03-07 14:38:42 +0530 | [diff] [blame] | 62 | static const char HOSTAPD_SOCKETS_DIR[] = "/data/misc/wifi/hostapd"; |
Dedy Lansky | 56adaf4 | 2015-09-06 14:27:22 +0300 | [diff] [blame] | 63 | static const char WIFI_HOSTAPD_GLOBAL_CTRL_IFACE[] = "/data/misc/wifi/hostapd/global"; |
Irfan Sheriff | 78dcb76 | 2011-07-22 15:20:21 -0700 | [diff] [blame] | 64 | |
Sasha Levitskiy | 25753d5 | 2013-01-10 12:48:39 -0800 | [diff] [blame] | 65 | SoftapController::SoftapController() |
| 66 | : mPid(0) {} |
Dmitry Shmidt | 5af38c3 | 2010-02-10 11:10:39 -0800 | [diff] [blame] | 67 | |
| 68 | SoftapController::~SoftapController() { |
Dmitry Shmidt | 84c65a6 | 2010-03-03 13:14:30 -0800 | [diff] [blame] | 69 | } |
| 70 | |
Nalla Kartheek | cad2dc8 | 2016-08-31 12:26:14 +0530 | [diff] [blame] | 71 | #ifdef LIBWPA_CLIENT_EXISTS |
| 72 | void *SoftapController::threadStart(void *obj){ |
| 73 | SoftapController *me = reinterpret_cast<SoftapController *>(obj); |
| 74 | struct wpa_ctrl *ctrl; |
| 75 | int count = 0; |
| 76 | |
| 77 | ALOGD("SoftapController::threadStart..."); |
| 78 | |
| 79 | DIR *dir = NULL; |
| 80 | |
| 81 | dir = opendir(HOSTAPD_SOCKETS_DIR); |
| 82 | if (NULL == dir && errno == ENOENT) { |
| 83 | mkdir(HOSTAPD_SOCKETS_DIR, S_IRWXU|S_IRWXG|S_IRWXO); |
| 84 | chown(HOSTAPD_SOCKETS_DIR, AID_WIFI, AID_WIFI); |
| 85 | chmod(HOSTAPD_SOCKETS_DIR, S_IRWXU|S_IRWXG); |
| 86 | } else { |
| 87 | if (dir != NULL) { /* Directory already exists */ |
| 88 | ALOGD("%s already exists", HOSTAPD_SOCKETS_DIR); |
| 89 | closedir(dir); |
| 90 | } |
| 91 | if (errno == EACCES) { |
| 92 | ALOGE("Cant open %s , check permissions ", HOSTAPD_SOCKETS_DIR); |
| 93 | } |
| 94 | } |
| 95 | chmod(HOSTAPD_DHCP_DIR, S_IRWXU|S_IRWXG|S_IRWXO); |
| 96 | |
Nalla Kartheek | 5ed655a | 2017-03-07 14:38:42 +0530 | [diff] [blame] | 97 | ctrl = wpa_ctrl_open2(hostapd_unix_file.c_str(), HOSTAPD_SOCKETS_DIR); |
Nalla Kartheek | cad2dc8 | 2016-08-31 12:26:14 +0530 | [diff] [blame] | 98 | while (ctrl == NULL) { |
| 99 | /* |
| 100 | * Try to connect to hostapd via wpa_ctrl interface. |
| 101 | * During conneciton process, it is possible that hostapd |
| 102 | * has station connected to it. |
| 103 | * Set sleep time to a appropriate value to lower the |
| 104 | * ratio that miss the STA-CONNECTED msg from hostapd |
| 105 | */ |
| 106 | usleep(20000); |
Nalla Kartheek | 5ed655a | 2017-03-07 14:38:42 +0530 | [diff] [blame] | 107 | ctrl = wpa_ctrl_open2(hostapd_unix_file.c_str(), HOSTAPD_SOCKETS_DIR); |
Nalla Kartheek | cad2dc8 | 2016-08-31 12:26:14 +0530 | [diff] [blame] | 108 | if (ctrl != NULL || count >= 150) { |
| 109 | break; |
| 110 | } |
| 111 | count ++; |
| 112 | } |
| 113 | if (count == 150 && ctrl == NULL) { |
| 114 | ALOGE("Connection to hostapd Error."); |
| 115 | return NULL; |
| 116 | } |
| 117 | |
| 118 | if (wpa_ctrl_attach(ctrl) != 0) { |
| 119 | wpa_ctrl_close(ctrl); |
| 120 | ALOGE("Attach to hostapd Error."); |
| 121 | return NULL; |
| 122 | } |
| 123 | |
| 124 | while (me->mHostapdFlag) { |
| 125 | int res = 0; |
| 126 | char buf[256]; |
| 127 | char dest_str[300]; |
| 128 | while (wpa_ctrl_pending(ctrl)) { |
| 129 | size_t len = sizeof(buf) - 1; |
| 130 | res = wpa_ctrl_recv(ctrl, buf, &len); |
| 131 | if (res == 0) { |
| 132 | buf[len] = '\0'; |
| 133 | ALOGD("Get event from hostapd (%s)", buf); |
| 134 | memset(dest_str, 0x0, sizeof(dest_str)); |
| 135 | snprintf(dest_str, sizeof(dest_str), "IfaceMessage active %s", buf); |
| 136 | me->mSocketClient->sendMsg(ResponseCode::InterfaceMessage, dest_str, false); |
| 137 | } else { |
| 138 | break; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | if (res < 0) { |
| 143 | break; |
| 144 | } |
| 145 | sleep(2); |
| 146 | } |
| 147 | |
| 148 | wpa_ctrl_detach(ctrl); |
| 149 | wpa_ctrl_close(ctrl); |
| 150 | |
Nalla Kartheek | 5ed655a | 2017-03-07 14:38:42 +0530 | [diff] [blame] | 151 | ALOGD("SoftapController::threadExit"); |
Nalla Kartheek | cad2dc8 | 2016-08-31 12:26:14 +0530 | [diff] [blame] | 152 | return NULL; |
| 153 | } |
| 154 | #endif |
| 155 | |
Nalla Kartheek | b9210a7 | 2016-11-17 09:56:14 +0530 | [diff] [blame] | 156 | int SoftapController::startSoftap(bool global_ctrl_iface = false, SocketClient *socketClient = NULL, |
| 157 | const char *ifname = NULL) { |
Dmitry Shmidt | 5af38c3 | 2010-02-10 11:10:39 -0800 | [diff] [blame] | 158 | pid_t pid = 1; |
Ashwini Patil | 508f10f | 2015-08-07 18:05:03 +0530 | [diff] [blame] | 159 | DIR *dir = NULL; |
Dedy Lansky | 56adaf4 | 2015-09-06 14:27:22 +0300 | [diff] [blame] | 160 | int ret; |
Dmitry Shmidt | 5af38c3 | 2010-02-10 11:10:39 -0800 | [diff] [blame] | 161 | |
Nalla Kartheek | a4c1b66 | 2016-09-14 15:48:34 +0530 | [diff] [blame] | 162 | mSocketClient = socketClient; |
Dmitry Shmidt | 5af38c3 | 2010-02-10 11:10:39 -0800 | [diff] [blame] | 163 | if (mPid) { |
Sasha Levitskiy | 25753d5 | 2013-01-10 12:48:39 -0800 | [diff] [blame] | 164 | ALOGE("SoftAP is already running"); |
| 165 | return ResponseCode::SoftapStatusResult; |
Dmitry Shmidt | 84c65a6 | 2010-03-03 13:14:30 -0800 | [diff] [blame] | 166 | } |
Irfan Sheriff | 7e9eb7b | 2012-06-15 16:11:31 -0700 | [diff] [blame] | 167 | |
Dmitry Shmidt | 6fa06b7 | 2014-09-05 16:39:29 -0700 | [diff] [blame] | 168 | if (ensure_entropy_file_exists() < 0) { |
| 169 | ALOGE("Wi-Fi entropy file was not created"); |
| 170 | } |
| 171 | |
Dmitry Shmidt | 389f8d1 | 2011-07-21 15:16:04 -0700 | [diff] [blame] | 172 | if ((pid = fork()) < 0) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 173 | ALOGE("fork failed (%s)", strerror(errno)); |
Sasha Levitskiy | 25753d5 | 2013-01-10 12:48:39 -0800 | [diff] [blame] | 174 | return ResponseCode::ServiceStartFailed; |
Dmitry Shmidt | 5af38c3 | 2010-02-10 11:10:39 -0800 | [diff] [blame] | 175 | } |
Irfan Sheriff | 7e9eb7b | 2012-06-15 16:11:31 -0700 | [diff] [blame] | 176 | |
Dmitry Shmidt | 5af38c3 | 2010-02-10 11:10:39 -0800 | [diff] [blame] | 177 | if (!pid) { |
Dmitry Shmidt | 01e182f | 2011-07-25 10:51:56 -0700 | [diff] [blame] | 178 | ensure_entropy_file_exists(); |
Dedy Lansky | 56adaf4 | 2015-09-06 14:27:22 +0300 | [diff] [blame] | 179 | if (global_ctrl_iface) { |
| 180 | ret = execl(HOSTAPD_BIN_FILE, HOSTAPD_BIN_FILE, |
| 181 | "-e", WIFI_ENTROPY_FILE, "-ddd", |
| 182 | "-g", WIFI_HOSTAPD_GLOBAL_CTRL_IFACE, |
| 183 | HOSTAPD_CONF_FILE, (char *)NULL); |
| 184 | } else { |
| 185 | ret = execl(HOSTAPD_BIN_FILE, HOSTAPD_BIN_FILE, |
| 186 | "-e", WIFI_ENTROPY_FILE, HOSTAPD_CONF_FILE, |
| 187 | (char *)NULL); |
| 188 | } |
| 189 | if (ret) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 190 | ALOGE("execl failed (%s)", strerror(errno)); |
Dmitry Shmidt | 389f8d1 | 2011-07-21 15:16:04 -0700 | [diff] [blame] | 191 | } |
Sasha Levitskiy | 25753d5 | 2013-01-10 12:48:39 -0800 | [diff] [blame] | 192 | ALOGE("SoftAP failed to start"); |
| 193 | return ResponseCode::ServiceStartFailed; |
Dmitry Shmidt | 5af38c3 | 2010-02-10 11:10:39 -0800 | [diff] [blame] | 194 | } else { |
Irfan Sheriff | 7e9eb7b | 2012-06-15 16:11:31 -0700 | [diff] [blame] | 195 | mPid = pid; |
Sasha Levitskiy | 25753d5 | 2013-01-10 12:48:39 -0800 | [diff] [blame] | 196 | ALOGD("SoftAP started successfully"); |
Irfan Sheriff | 7e9eb7b | 2012-06-15 16:11:31 -0700 | [diff] [blame] | 197 | usleep(AP_BSS_START_DELAY); |
Ashwini Patil | 508f10f | 2015-08-07 18:05:03 +0530 | [diff] [blame] | 198 | dir = opendir(HOSTAPD_SOCKETS_DIR); |
| 199 | if (NULL == dir && errno == ENOENT) { |
| 200 | mkdir(HOSTAPD_SOCKETS_DIR, S_IRWXU|S_IRWXG|S_IRWXO); |
| 201 | chown(HOSTAPD_SOCKETS_DIR, AID_WIFI, AID_WIFI); |
| 202 | chmod(HOSTAPD_SOCKETS_DIR, S_IRWXU|S_IRWXG); |
| 203 | } else { |
| 204 | if (dir != NULL) { /* Directory already exists */ |
| 205 | ALOGD("%s already exists", HOSTAPD_SOCKETS_DIR); |
| 206 | closedir(dir); |
| 207 | } |
| 208 | if (errno == EACCES) { |
| 209 | ALOGE("Cant open %s , check permissions ", HOSTAPD_SOCKETS_DIR); |
| 210 | } |
| 211 | } |
Nalla Kartheek | b9210a7 | 2016-11-17 09:56:14 +0530 | [diff] [blame] | 212 | if (ifname != NULL) { |
| 213 | std:: string wbuf(StringPrintf("/data/misc/wifi/hostapd/%s",ifname)); |
| 214 | hostapd_unix_file = wbuf; |
| 215 | } |
Nalla Kartheek | 71feca6 | 2016-12-08 15:35:51 +0530 | [diff] [blame] | 216 | #ifdef LIBWPA_CLIENT_EXISTS |
| 217 | mHostapdFlag = true; |
Nalla Kartheek | a4c1b66 | 2016-09-14 15:48:34 +0530 | [diff] [blame] | 218 | if (mSocketClient != NULL) { |
| 219 | if ((mThreadErr = pthread_create(&mThread, NULL, SoftapController::threadStart, this)) != 0) { |
| 220 | ALOGE("pthread_create failed for hostapd listen socket (%s)", strerror(errno)); |
| 221 | } |
Nalla Kartheek | cad2dc8 | 2016-08-31 12:26:14 +0530 | [diff] [blame] | 222 | } |
| 223 | #endif |
Dmitry Shmidt | 5af38c3 | 2010-02-10 11:10:39 -0800 | [diff] [blame] | 224 | } |
Sasha Levitskiy | 25753d5 | 2013-01-10 12:48:39 -0800 | [diff] [blame] | 225 | return ResponseCode::SoftapStatusResult; |
Dmitry Shmidt | 5af38c3 | 2010-02-10 11:10:39 -0800 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | int SoftapController::stopSoftap() { |
Dmitry Shmidt | 84c65a6 | 2010-03-03 13:14:30 -0800 | [diff] [blame] | 229 | |
Dmitry Shmidt | 5af38c3 | 2010-02-10 11:10:39 -0800 | [diff] [blame] | 230 | if (mPid == 0) { |
Sasha Levitskiy | 25753d5 | 2013-01-10 12:48:39 -0800 | [diff] [blame] | 231 | ALOGE("SoftAP is not running"); |
| 232 | return ResponseCode::SoftapStatusResult; |
Dmitry Shmidt | 5af38c3 | 2010-02-10 11:10:39 -0800 | [diff] [blame] | 233 | } |
Dmitry Shmidt | 389f8d1 | 2011-07-21 15:16:04 -0700 | [diff] [blame] | 234 | |
Nalla Kartheek | cad2dc8 | 2016-08-31 12:26:14 +0530 | [diff] [blame] | 235 | #ifdef LIBWPA_CLIENT_EXISTS |
| 236 | mHostapdFlag = false; |
| 237 | if (mThreadErr == 0) { |
| 238 | pthread_join(mThread, NULL); |
| 239 | } |
| 240 | #endif |
| 241 | |
Sasha Levitskiy | 25753d5 | 2013-01-10 12:48:39 -0800 | [diff] [blame] | 242 | ALOGD("Stopping the SoftAP service..."); |
Dmitry Shmidt | 389f8d1 | 2011-07-21 15:16:04 -0700 | [diff] [blame] | 243 | kill(mPid, SIGTERM); |
| 244 | waitpid(mPid, NULL, 0); |
Irfan Sheriff | 7e9eb7b | 2012-06-15 16:11:31 -0700 | [diff] [blame] | 245 | |
Dmitry Shmidt | 5af38c3 | 2010-02-10 11:10:39 -0800 | [diff] [blame] | 246 | mPid = 0; |
Sasha Levitskiy | 25753d5 | 2013-01-10 12:48:39 -0800 | [diff] [blame] | 247 | ALOGD("SoftAP stopped successfully"); |
Dmitry Shmidt | 3df450a | 2010-03-18 13:06:47 -0700 | [diff] [blame] | 248 | usleep(AP_BSS_STOP_DELAY); |
Sasha Levitskiy | 25753d5 | 2013-01-10 12:48:39 -0800 | [diff] [blame] | 249 | return ResponseCode::SoftapStatusResult; |
Dmitry Shmidt | 5af38c3 | 2010-02-10 11:10:39 -0800 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | bool SoftapController::isSoftapStarted() { |
Sasha Levitskiy | 25753d5 | 2013-01-10 12:48:39 -0800 | [diff] [blame] | 253 | return (mPid != 0); |
Dmitry Shmidt | 5af38c3 | 2010-02-10 11:10:39 -0800 | [diff] [blame] | 254 | } |
| 255 | |
Dmitry Shmidt | 84c65a6 | 2010-03-03 13:14:30 -0800 | [diff] [blame] | 256 | /* |
| 257 | * Arguments: |
Sasha Levitskiy | 25753d5 | 2013-01-10 12:48:39 -0800 | [diff] [blame] | 258 | * argv[2] - wlan interface |
| 259 | * argv[3] - SSID |
Dmitry Shmidt | eb59b57 | 2013-04-16 13:16:05 -0700 | [diff] [blame] | 260 | * argv[4] - Broadcast/Hidden |
Dmitry Shmidt | 85e6c5f | 2013-06-10 14:35:43 -0700 | [diff] [blame] | 261 | * argv[5] - Channel |
| 262 | * argv[6] - Security |
| 263 | * argv[7] - Key |
Dmitry Shmidt | 84c65a6 | 2010-03-03 13:14:30 -0800 | [diff] [blame] | 264 | */ |
Dmitry Shmidt | 5af38c3 | 2010-02-10 11:10:39 -0800 | [diff] [blame] | 265 | int SoftapController::setSoftap(int argc, char *argv[]) { |
Dmitry Shmidt | eb59b57 | 2013-04-16 13:16:05 -0700 | [diff] [blame] | 266 | int hidden = 0; |
Dmitry Shmidt | 85e6c5f | 2013-06-10 14:35:43 -0700 | [diff] [blame] | 267 | int channel = AP_CHANNEL_DEFAULT; |
Irfan Sheriff | 78dcb76 | 2011-07-22 15:20:21 -0700 | [diff] [blame] | 268 | |
Dmitry Shmidt | eb59b57 | 2013-04-16 13:16:05 -0700 | [diff] [blame] | 269 | if (argc < 5) { |
| 270 | ALOGE("Softap set is missing arguments. Please use:"); |
Dmitry Shmidt | 85e6c5f | 2013-06-10 14:35:43 -0700 | [diff] [blame] | 271 | ALOGE("softap <wlan iface> <SSID> <hidden/broadcast> <channel> <wpa2?-psk|open> <passphrase>"); |
Dmitry Shmidt | eb59b57 | 2013-04-16 13:16:05 -0700 | [diff] [blame] | 272 | return ResponseCode::CommandSyntaxError; |
| 273 | } |
| 274 | |
| 275 | if (!strcasecmp(argv[4], "hidden")) |
| 276 | hidden = 1; |
| 277 | |
Dmitry Shmidt | 85e6c5f | 2013-06-10 14:35:43 -0700 | [diff] [blame] | 278 | if (argc >= 5) { |
| 279 | channel = atoi(argv[5]); |
| 280 | if (channel <= 0) |
| 281 | channel = AP_CHANNEL_DEFAULT; |
| 282 | } |
Irfan Sheriff | 78dcb76 | 2011-07-22 15:20:21 -0700 | [diff] [blame] | 283 | |
Dan Albert | 5407e14 | 2015-03-16 10:05:59 -0700 | [diff] [blame] | 284 | std::string wbuf(StringPrintf("interface=%s\n" |
Elliott Hughes | bd37832 | 2015-02-04 13:25:14 -0800 | [diff] [blame] | 285 | "driver=nl80211\n" |
| 286 | "ctrl_interface=/data/misc/wifi/hostapd\n" |
| 287 | "ssid=%s\n" |
| 288 | "channel=%d\n" |
| 289 | "ieee80211n=1\n" |
Elliott Hughes | 4674fae | 2015-02-11 20:38:23 -0800 | [diff] [blame] | 290 | "hw_mode=%c\n" |
Elliott Hughes | bd37832 | 2015-02-04 13:25:14 -0800 | [diff] [blame] | 291 | "ignore_broadcast_ssid=%d\n" |
| 292 | "wowlan_triggers=any\n", |
Elliott Hughes | 4674fae | 2015-02-11 20:38:23 -0800 | [diff] [blame] | 293 | argv[2], argv[3], channel, (channel <= 14) ? 'g' : 'a', hidden)); |
Dmitry Shmidt | 85e6c5f | 2013-06-10 14:35:43 -0700 | [diff] [blame] | 294 | |
Elliott Hughes | bd37832 | 2015-02-04 13:25:14 -0800 | [diff] [blame] | 295 | std::string fbuf; |
Dmitry Shmidt | 85e6c5f | 2013-06-10 14:35:43 -0700 | [diff] [blame] | 296 | if (argc > 7) { |
Elliott Hughes | bd37832 | 2015-02-04 13:25:14 -0800 | [diff] [blame] | 297 | char psk_str[2*SHA256_DIGEST_LENGTH+1]; |
Dmitry Shmidt | 85e6c5f | 2013-06-10 14:35:43 -0700 | [diff] [blame] | 298 | if (!strcmp(argv[6], "wpa-psk")) { |
Adam Langley | 986d097 | 2015-11-04 14:35:25 -0800 | [diff] [blame] | 299 | if (!generatePsk(argv[3], argv[7], psk_str)) { |
| 300 | return ResponseCode::OperationFailed; |
| 301 | } |
Dan Albert | 5407e14 | 2015-03-16 10:05:59 -0700 | [diff] [blame] | 302 | fbuf = StringPrintf("%swpa=3\nwpa_pairwise=TKIP CCMP\nwpa_psk=%s\n", wbuf.c_str(), psk_str); |
Dmitry Shmidt | 85e6c5f | 2013-06-10 14:35:43 -0700 | [diff] [blame] | 303 | } else if (!strcmp(argv[6], "wpa2-psk")) { |
Adam Langley | 986d097 | 2015-11-04 14:35:25 -0800 | [diff] [blame] | 304 | if (!generatePsk(argv[3], argv[7], psk_str)) { |
| 305 | return ResponseCode::OperationFailed; |
| 306 | } |
Dan Albert | 5407e14 | 2015-03-16 10:05:59 -0700 | [diff] [blame] | 307 | fbuf = StringPrintf("%swpa=2\nrsn_pairwise=CCMP\nwpa_psk=%s\n", wbuf.c_str(), psk_str); |
Dmitry Shmidt | 85e6c5f | 2013-06-10 14:35:43 -0700 | [diff] [blame] | 308 | } else if (!strcmp(argv[6], "open")) { |
Elliott Hughes | bd37832 | 2015-02-04 13:25:14 -0800 | [diff] [blame] | 309 | fbuf = wbuf; |
Dmitry Shmidt | eb59b57 | 2013-04-16 13:16:05 -0700 | [diff] [blame] | 310 | } |
Dmitry Shmidt | 85e6c5f | 2013-06-10 14:35:43 -0700 | [diff] [blame] | 311 | } else if (argc > 6) { |
| 312 | if (!strcmp(argv[6], "open")) { |
Elliott Hughes | bd37832 | 2015-02-04 13:25:14 -0800 | [diff] [blame] | 313 | fbuf = wbuf; |
Irfan Sheriff | 78dcb76 | 2011-07-22 15:20:21 -0700 | [diff] [blame] | 314 | } |
| 315 | } else { |
Elliott Hughes | bd37832 | 2015-02-04 13:25:14 -0800 | [diff] [blame] | 316 | fbuf = wbuf; |
Irfan Sheriff | 78dcb76 | 2011-07-22 15:20:21 -0700 | [diff] [blame] | 317 | } |
| 318 | |
Dan Albert | 5407e14 | 2015-03-16 10:05:59 -0700 | [diff] [blame] | 319 | if (!WriteStringToFile(fbuf, HOSTAPD_CONF_FILE, 0660, AID_SYSTEM, AID_WIFI)) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 320 | ALOGE("Cannot write to \"%s\": %s", HOSTAPD_CONF_FILE, strerror(errno)); |
Sasha Levitskiy | 25753d5 | 2013-01-10 12:48:39 -0800 | [diff] [blame] | 321 | return ResponseCode::OperationFailed; |
Irfan Sheriff | 78dcb76 | 2011-07-22 15:20:21 -0700 | [diff] [blame] | 322 | } |
Elliott Hughes | bd37832 | 2015-02-04 13:25:14 -0800 | [diff] [blame] | 323 | return ResponseCode::SoftapStatusResult; |
Dmitry Shmidt | 5af38c3 | 2010-02-10 11:10:39 -0800 | [diff] [blame] | 324 | } |
Dmitry Shmidt | 31fd6c5 | 2010-03-12 10:01:58 -0800 | [diff] [blame] | 325 | |
Sasha Levitskiy | 25753d5 | 2013-01-10 12:48:39 -0800 | [diff] [blame] | 326 | /* |
| 327 | * Arguments: |
| 328 | * argv[2] - interface name |
| 329 | * argv[3] - AP or P2P or STA |
| 330 | */ |
| 331 | int SoftapController::fwReloadSoftap(int argc, char *argv[]) |
| 332 | { |
Sasha Levitskiy | 25753d5 | 2013-01-10 12:48:39 -0800 | [diff] [blame] | 333 | char *fwpath = NULL; |
| 334 | |
| 335 | if (argc < 4) { |
| 336 | ALOGE("SoftAP fwreload is missing arguments. Please use: softap <wlan iface> <AP|P2P|STA>"); |
| 337 | return ResponseCode::CommandSyntaxError; |
| 338 | } |
| 339 | |
| 340 | if (strcmp(argv[3], "AP") == 0) { |
| 341 | fwpath = (char *)wifi_get_fw_path(WIFI_GET_FW_PATH_AP); |
| 342 | } else if (strcmp(argv[3], "P2P") == 0) { |
| 343 | fwpath = (char *)wifi_get_fw_path(WIFI_GET_FW_PATH_P2P); |
| 344 | } else if (strcmp(argv[3], "STA") == 0) { |
| 345 | fwpath = (char *)wifi_get_fw_path(WIFI_GET_FW_PATH_STA); |
Dmitry Shmidt | 680d765 | 2016-03-21 14:02:54 -0700 | [diff] [blame] | 346 | } else { |
Sasha Levitskiy | 25753d5 | 2013-01-10 12:48:39 -0800 | [diff] [blame] | 347 | return ResponseCode::CommandParameterError; |
Dmitry Shmidt | 680d765 | 2016-03-21 14:02:54 -0700 | [diff] [blame] | 348 | } |
| 349 | if (!fwpath) { |
| 350 | ALOGE("Softap fwReload - NULL path for %s", argv[3]); |
| 351 | return ResponseCode::SoftapStatusResult; |
| 352 | } |
Sasha Levitskiy | 25753d5 | 2013-01-10 12:48:39 -0800 | [diff] [blame] | 353 | if (wifi_change_fw_path((const char *)fwpath)) { |
| 354 | ALOGE("Softap fwReload failed"); |
| 355 | return ResponseCode::OperationFailed; |
| 356 | } |
| 357 | else { |
| 358 | ALOGD("Softap fwReload - Ok"); |
| 359 | } |
| 360 | return ResponseCode::SoftapStatusResult; |
| 361 | } |
| 362 | |
Adam Langley | 986d097 | 2015-11-04 14:35:25 -0800 | [diff] [blame] | 363 | bool SoftapController::generatePsk(char *ssid, char *passphrase, char *psk_str) { |
Irfan Sheriff | 78dcb76 | 2011-07-22 15:20:21 -0700 | [diff] [blame] | 364 | unsigned char psk[SHA256_DIGEST_LENGTH]; |
Adam Langley | 986d097 | 2015-11-04 14:35:25 -0800 | [diff] [blame] | 365 | |
Irfan Sheriff | 78dcb76 | 2011-07-22 15:20:21 -0700 | [diff] [blame] | 366 | // Use the PKCS#5 PBKDF2 with 4096 iterations |
Adam Langley | 986d097 | 2015-11-04 14:35:25 -0800 | [diff] [blame] | 367 | if (PKCS5_PBKDF2_HMAC_SHA1(passphrase, strlen(passphrase), |
| 368 | reinterpret_cast<const unsigned char *>(ssid), |
| 369 | strlen(ssid), 4096, SHA256_DIGEST_LENGTH, |
| 370 | psk) != 1) { |
| 371 | ALOGE("Cannot generate PSK using PKCS#5 PBKDF2"); |
| 372 | return false; |
| 373 | } |
| 374 | |
| 375 | for (int j=0; j < SHA256_DIGEST_LENGTH; j++) { |
Sasha Levitskiy | 25753d5 | 2013-01-10 12:48:39 -0800 | [diff] [blame] | 376 | sprintf(&psk_str[j*2], "%02x", psk[j]); |
Irfan Sheriff | 78dcb76 | 2011-07-22 15:20:21 -0700 | [diff] [blame] | 377 | } |
Adam Langley | 986d097 | 2015-11-04 14:35:25 -0800 | [diff] [blame] | 378 | |
| 379 | return true; |
Dmitry Shmidt | 666fe25 | 2011-03-08 11:01:58 -0800 | [diff] [blame] | 380 | } |