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