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" |
Dan Albert | b67219a | 2015-03-13 22:35:27 -0700 | [diff] [blame^] | 37 | #include <base/file.h> |
| 38 | #include <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" |
| 46 | |
Dan Albert | b67219a | 2015-03-13 22:35:27 -0700 | [diff] [blame^] | 47 | using android::base::StringPrintf; |
| 48 | using android::base::WriteStringToFile; |
| 49 | |
Irfan Sheriff | 78dcb76 | 2011-07-22 15:20:21 -0700 | [diff] [blame] | 50 | static const char HOSTAPD_CONF_FILE[] = "/data/misc/wifi/hostapd.conf"; |
Sasha Levitskiy | 25753d5 | 2013-01-10 12:48:39 -0800 | [diff] [blame] | 51 | static const char HOSTAPD_BIN_FILE[] = "/system/bin/hostapd"; |
Irfan Sheriff | 78dcb76 | 2011-07-22 15:20:21 -0700 | [diff] [blame] | 52 | |
Sasha Levitskiy | 25753d5 | 2013-01-10 12:48:39 -0800 | [diff] [blame] | 53 | SoftapController::SoftapController() |
| 54 | : mPid(0) {} |
Dmitry Shmidt | 5af38c3 | 2010-02-10 11:10:39 -0800 | [diff] [blame] | 55 | |
| 56 | SoftapController::~SoftapController() { |
Dmitry Shmidt | 84c65a6 | 2010-03-03 13:14:30 -0800 | [diff] [blame] | 57 | } |
| 58 | |
Dmitry Shmidt | 5af38c3 | 2010-02-10 11:10:39 -0800 | [diff] [blame] | 59 | int SoftapController::startSoftap() { |
| 60 | pid_t pid = 1; |
| 61 | |
Dmitry Shmidt | 5af38c3 | 2010-02-10 11:10:39 -0800 | [diff] [blame] | 62 | if (mPid) { |
Sasha Levitskiy | 25753d5 | 2013-01-10 12:48:39 -0800 | [diff] [blame] | 63 | ALOGE("SoftAP is already running"); |
| 64 | return ResponseCode::SoftapStatusResult; |
Dmitry Shmidt | 84c65a6 | 2010-03-03 13:14:30 -0800 | [diff] [blame] | 65 | } |
Irfan Sheriff | 7e9eb7b | 2012-06-15 16:11:31 -0700 | [diff] [blame] | 66 | |
Dmitry Shmidt | 6fa06b7 | 2014-09-05 16:39:29 -0700 | [diff] [blame] | 67 | if (ensure_entropy_file_exists() < 0) { |
| 68 | ALOGE("Wi-Fi entropy file was not created"); |
| 69 | } |
| 70 | |
Dmitry Shmidt | 389f8d1 | 2011-07-21 15:16:04 -0700 | [diff] [blame] | 71 | if ((pid = fork()) < 0) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 72 | ALOGE("fork failed (%s)", strerror(errno)); |
Sasha Levitskiy | 25753d5 | 2013-01-10 12:48:39 -0800 | [diff] [blame] | 73 | return ResponseCode::ServiceStartFailed; |
Dmitry Shmidt | 5af38c3 | 2010-02-10 11:10:39 -0800 | [diff] [blame] | 74 | } |
Irfan Sheriff | 7e9eb7b | 2012-06-15 16:11:31 -0700 | [diff] [blame] | 75 | |
Dmitry Shmidt | 5af38c3 | 2010-02-10 11:10:39 -0800 | [diff] [blame] | 76 | if (!pid) { |
Dmitry Shmidt | 01e182f | 2011-07-25 10:51:56 -0700 | [diff] [blame] | 77 | ensure_entropy_file_exists(); |
Sasha Levitskiy | 25753d5 | 2013-01-10 12:48:39 -0800 | [diff] [blame] | 78 | if (execl(HOSTAPD_BIN_FILE, HOSTAPD_BIN_FILE, |
Dmitry Shmidt | 01e182f | 2011-07-25 10:51:56 -0700 | [diff] [blame] | 79 | "-e", WIFI_ENTROPY_FILE, |
Irfan Sheriff | 78dcb76 | 2011-07-22 15:20:21 -0700 | [diff] [blame] | 80 | HOSTAPD_CONF_FILE, (char *) NULL)) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 81 | ALOGE("execl failed (%s)", strerror(errno)); |
Dmitry Shmidt | 389f8d1 | 2011-07-21 15:16:04 -0700 | [diff] [blame] | 82 | } |
Sasha Levitskiy | 25753d5 | 2013-01-10 12:48:39 -0800 | [diff] [blame] | 83 | ALOGE("SoftAP failed to start"); |
| 84 | return ResponseCode::ServiceStartFailed; |
Dmitry Shmidt | 5af38c3 | 2010-02-10 11:10:39 -0800 | [diff] [blame] | 85 | } else { |
Irfan Sheriff | 7e9eb7b | 2012-06-15 16:11:31 -0700 | [diff] [blame] | 86 | mPid = pid; |
Sasha Levitskiy | 25753d5 | 2013-01-10 12:48:39 -0800 | [diff] [blame] | 87 | ALOGD("SoftAP started successfully"); |
Irfan Sheriff | 7e9eb7b | 2012-06-15 16:11:31 -0700 | [diff] [blame] | 88 | usleep(AP_BSS_START_DELAY); |
Dmitry Shmidt | 5af38c3 | 2010-02-10 11:10:39 -0800 | [diff] [blame] | 89 | } |
Sasha Levitskiy | 25753d5 | 2013-01-10 12:48:39 -0800 | [diff] [blame] | 90 | return ResponseCode::SoftapStatusResult; |
Dmitry Shmidt | 5af38c3 | 2010-02-10 11:10:39 -0800 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | int SoftapController::stopSoftap() { |
Dmitry Shmidt | 84c65a6 | 2010-03-03 13:14:30 -0800 | [diff] [blame] | 94 | |
Dmitry Shmidt | 5af38c3 | 2010-02-10 11:10:39 -0800 | [diff] [blame] | 95 | if (mPid == 0) { |
Sasha Levitskiy | 25753d5 | 2013-01-10 12:48:39 -0800 | [diff] [blame] | 96 | ALOGE("SoftAP is not running"); |
| 97 | return ResponseCode::SoftapStatusResult; |
Dmitry Shmidt | 5af38c3 | 2010-02-10 11:10:39 -0800 | [diff] [blame] | 98 | } |
Dmitry Shmidt | 389f8d1 | 2011-07-21 15:16:04 -0700 | [diff] [blame] | 99 | |
Sasha Levitskiy | 25753d5 | 2013-01-10 12:48:39 -0800 | [diff] [blame] | 100 | ALOGD("Stopping the SoftAP service..."); |
Dmitry Shmidt | 389f8d1 | 2011-07-21 15:16:04 -0700 | [diff] [blame] | 101 | kill(mPid, SIGTERM); |
| 102 | waitpid(mPid, NULL, 0); |
Irfan Sheriff | 7e9eb7b | 2012-06-15 16:11:31 -0700 | [diff] [blame] | 103 | |
Dmitry Shmidt | 5af38c3 | 2010-02-10 11:10:39 -0800 | [diff] [blame] | 104 | mPid = 0; |
Sasha Levitskiy | 25753d5 | 2013-01-10 12:48:39 -0800 | [diff] [blame] | 105 | ALOGD("SoftAP stopped successfully"); |
Dmitry Shmidt | 3df450a | 2010-03-18 13:06:47 -0700 | [diff] [blame] | 106 | usleep(AP_BSS_STOP_DELAY); |
Sasha Levitskiy | 25753d5 | 2013-01-10 12:48:39 -0800 | [diff] [blame] | 107 | return ResponseCode::SoftapStatusResult; |
Dmitry Shmidt | 5af38c3 | 2010-02-10 11:10:39 -0800 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | bool SoftapController::isSoftapStarted() { |
Sasha Levitskiy | 25753d5 | 2013-01-10 12:48:39 -0800 | [diff] [blame] | 111 | return (mPid != 0); |
Dmitry Shmidt | 5af38c3 | 2010-02-10 11:10:39 -0800 | [diff] [blame] | 112 | } |
| 113 | |
Dmitry Shmidt | 84c65a6 | 2010-03-03 13:14:30 -0800 | [diff] [blame] | 114 | /* |
| 115 | * Arguments: |
Sasha Levitskiy | 25753d5 | 2013-01-10 12:48:39 -0800 | [diff] [blame] | 116 | * argv[2] - wlan interface |
| 117 | * argv[3] - SSID |
Dmitry Shmidt | eb59b57 | 2013-04-16 13:16:05 -0700 | [diff] [blame] | 118 | * argv[4] - Broadcast/Hidden |
Dmitry Shmidt | 85e6c5f | 2013-06-10 14:35:43 -0700 | [diff] [blame] | 119 | * argv[5] - Channel |
| 120 | * argv[6] - Security |
| 121 | * argv[7] - Key |
Dmitry Shmidt | 84c65a6 | 2010-03-03 13:14:30 -0800 | [diff] [blame] | 122 | */ |
Dmitry Shmidt | 5af38c3 | 2010-02-10 11:10:39 -0800 | [diff] [blame] | 123 | int SoftapController::setSoftap(int argc, char *argv[]) { |
Dmitry Shmidt | eb59b57 | 2013-04-16 13:16:05 -0700 | [diff] [blame] | 124 | int hidden = 0; |
Dmitry Shmidt | 85e6c5f | 2013-06-10 14:35:43 -0700 | [diff] [blame] | 125 | int channel = AP_CHANNEL_DEFAULT; |
Irfan Sheriff | 78dcb76 | 2011-07-22 15:20:21 -0700 | [diff] [blame] | 126 | |
Dmitry Shmidt | eb59b57 | 2013-04-16 13:16:05 -0700 | [diff] [blame] | 127 | if (argc < 5) { |
| 128 | ALOGE("Softap set is missing arguments. Please use:"); |
Dmitry Shmidt | 85e6c5f | 2013-06-10 14:35:43 -0700 | [diff] [blame] | 129 | ALOGE("softap <wlan iface> <SSID> <hidden/broadcast> <channel> <wpa2?-psk|open> <passphrase>"); |
Dmitry Shmidt | eb59b57 | 2013-04-16 13:16:05 -0700 | [diff] [blame] | 130 | return ResponseCode::CommandSyntaxError; |
| 131 | } |
| 132 | |
| 133 | if (!strcasecmp(argv[4], "hidden")) |
| 134 | hidden = 1; |
| 135 | |
Dmitry Shmidt | 85e6c5f | 2013-06-10 14:35:43 -0700 | [diff] [blame] | 136 | if (argc >= 5) { |
| 137 | channel = atoi(argv[5]); |
| 138 | if (channel <= 0) |
| 139 | channel = AP_CHANNEL_DEFAULT; |
| 140 | } |
Irfan Sheriff | 78dcb76 | 2011-07-22 15:20:21 -0700 | [diff] [blame] | 141 | |
Dan Albert | b67219a | 2015-03-13 22:35:27 -0700 | [diff] [blame^] | 142 | std::string wbuf(StringPrintf("interface=%s\n" |
Elliott Hughes | bd37832 | 2015-02-04 13:25:14 -0800 | [diff] [blame] | 143 | "driver=nl80211\n" |
| 144 | "ctrl_interface=/data/misc/wifi/hostapd\n" |
| 145 | "ssid=%s\n" |
| 146 | "channel=%d\n" |
| 147 | "ieee80211n=1\n" |
| 148 | "hw_mode=g\n" |
| 149 | "ignore_broadcast_ssid=%d\n" |
| 150 | "wowlan_triggers=any\n", |
| 151 | argv[2], argv[3], channel, hidden)); |
Dmitry Shmidt | 85e6c5f | 2013-06-10 14:35:43 -0700 | [diff] [blame] | 152 | |
Elliott Hughes | bd37832 | 2015-02-04 13:25:14 -0800 | [diff] [blame] | 153 | std::string fbuf; |
Dmitry Shmidt | 85e6c5f | 2013-06-10 14:35:43 -0700 | [diff] [blame] | 154 | if (argc > 7) { |
Elliott Hughes | bd37832 | 2015-02-04 13:25:14 -0800 | [diff] [blame] | 155 | char psk_str[2*SHA256_DIGEST_LENGTH+1]; |
Dmitry Shmidt | 85e6c5f | 2013-06-10 14:35:43 -0700 | [diff] [blame] | 156 | if (!strcmp(argv[6], "wpa-psk")) { |
| 157 | generatePsk(argv[3], argv[7], psk_str); |
Dan Albert | b67219a | 2015-03-13 22:35:27 -0700 | [diff] [blame^] | 158 | 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] | 159 | } else if (!strcmp(argv[6], "wpa2-psk")) { |
| 160 | generatePsk(argv[3], argv[7], psk_str); |
Dan Albert | b67219a | 2015-03-13 22:35:27 -0700 | [diff] [blame^] | 161 | 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] | 162 | } else if (!strcmp(argv[6], "open")) { |
Elliott Hughes | bd37832 | 2015-02-04 13:25:14 -0800 | [diff] [blame] | 163 | fbuf = wbuf; |
Dmitry Shmidt | eb59b57 | 2013-04-16 13:16:05 -0700 | [diff] [blame] | 164 | } |
Dmitry Shmidt | 85e6c5f | 2013-06-10 14:35:43 -0700 | [diff] [blame] | 165 | } else if (argc > 6) { |
| 166 | if (!strcmp(argv[6], "open")) { |
Elliott Hughes | bd37832 | 2015-02-04 13:25:14 -0800 | [diff] [blame] | 167 | fbuf = wbuf; |
Irfan Sheriff | 78dcb76 | 2011-07-22 15:20:21 -0700 | [diff] [blame] | 168 | } |
| 169 | } else { |
Elliott Hughes | bd37832 | 2015-02-04 13:25:14 -0800 | [diff] [blame] | 170 | fbuf = wbuf; |
Irfan Sheriff | 78dcb76 | 2011-07-22 15:20:21 -0700 | [diff] [blame] | 171 | } |
| 172 | |
Dan Albert | b67219a | 2015-03-13 22:35:27 -0700 | [diff] [blame^] | 173 | if (!WriteStringToFile(fbuf, HOSTAPD_CONF_FILE, 0660, AID_SYSTEM, AID_WIFI)) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 174 | ALOGE("Cannot write to \"%s\": %s", HOSTAPD_CONF_FILE, strerror(errno)); |
Sasha Levitskiy | 25753d5 | 2013-01-10 12:48:39 -0800 | [diff] [blame] | 175 | return ResponseCode::OperationFailed; |
Irfan Sheriff | 78dcb76 | 2011-07-22 15:20:21 -0700 | [diff] [blame] | 176 | } |
Elliott Hughes | bd37832 | 2015-02-04 13:25:14 -0800 | [diff] [blame] | 177 | return ResponseCode::SoftapStatusResult; |
Dmitry Shmidt | 5af38c3 | 2010-02-10 11:10:39 -0800 | [diff] [blame] | 178 | } |
Dmitry Shmidt | 31fd6c5 | 2010-03-12 10:01:58 -0800 | [diff] [blame] | 179 | |
Sasha Levitskiy | 25753d5 | 2013-01-10 12:48:39 -0800 | [diff] [blame] | 180 | /* |
| 181 | * Arguments: |
| 182 | * argv[2] - interface name |
| 183 | * argv[3] - AP or P2P or STA |
| 184 | */ |
| 185 | int SoftapController::fwReloadSoftap(int argc, char *argv[]) |
| 186 | { |
Sasha Levitskiy | 25753d5 | 2013-01-10 12:48:39 -0800 | [diff] [blame] | 187 | char *fwpath = NULL; |
| 188 | |
| 189 | if (argc < 4) { |
| 190 | ALOGE("SoftAP fwreload is missing arguments. Please use: softap <wlan iface> <AP|P2P|STA>"); |
| 191 | return ResponseCode::CommandSyntaxError; |
| 192 | } |
| 193 | |
| 194 | if (strcmp(argv[3], "AP") == 0) { |
| 195 | fwpath = (char *)wifi_get_fw_path(WIFI_GET_FW_PATH_AP); |
| 196 | } else if (strcmp(argv[3], "P2P") == 0) { |
| 197 | fwpath = (char *)wifi_get_fw_path(WIFI_GET_FW_PATH_P2P); |
| 198 | } else if (strcmp(argv[3], "STA") == 0) { |
| 199 | fwpath = (char *)wifi_get_fw_path(WIFI_GET_FW_PATH_STA); |
| 200 | } |
| 201 | if (!fwpath) |
| 202 | return ResponseCode::CommandParameterError; |
| 203 | if (wifi_change_fw_path((const char *)fwpath)) { |
| 204 | ALOGE("Softap fwReload failed"); |
| 205 | return ResponseCode::OperationFailed; |
| 206 | } |
| 207 | else { |
| 208 | ALOGD("Softap fwReload - Ok"); |
| 209 | } |
| 210 | return ResponseCode::SoftapStatusResult; |
| 211 | } |
| 212 | |
Irfan Sheriff | 78dcb76 | 2011-07-22 15:20:21 -0700 | [diff] [blame] | 213 | void SoftapController::generatePsk(char *ssid, char *passphrase, char *psk_str) { |
| 214 | unsigned char psk[SHA256_DIGEST_LENGTH]; |
| 215 | int j; |
| 216 | // Use the PKCS#5 PBKDF2 with 4096 iterations |
| 217 | PKCS5_PBKDF2_HMAC_SHA1(passphrase, strlen(passphrase), |
| 218 | reinterpret_cast<const unsigned char *>(ssid), strlen(ssid), |
| 219 | 4096, SHA256_DIGEST_LENGTH, psk); |
| 220 | for (j=0; j < SHA256_DIGEST_LENGTH; j++) { |
Sasha Levitskiy | 25753d5 | 2013-01-10 12:48:39 -0800 | [diff] [blame] | 221 | sprintf(&psk_str[j*2], "%02x", psk[j]); |
Irfan Sheriff | 78dcb76 | 2011-07-22 15:20:21 -0700 | [diff] [blame] | 222 | } |
Dmitry Shmidt | 666fe25 | 2011-03-08 11:01:58 -0800 | [diff] [blame] | 223 | } |