blob: c1db1fcf2772b262de6b729da0e67e0c820d256b [file] [log] [blame]
Dmitry Shmidt5af38c32010-02-10 11:10:39 -08001/*
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 Baillyff2c0d82010-11-17 11:45:07 -080020#include <string.h>
Dmitry Shmidt5af38c32010-02-10 11:10:39 -080021
22#include <sys/socket.h>
23#include <sys/stat.h>
Olivier Baillyff2c0d82010-11-17 11:45:07 -080024#include <sys/ioctl.h>
Dmitry Shmidt5af38c32010-02-10 11:10:39 -080025#include <sys/types.h>
26#include <sys/wait.h>
27
28#include <netinet/in.h>
29#include <arpa/inet.h>
30
Dmitry Shmidt84c65a62010-03-03 13:14:30 -080031#include <linux/wireless.h>
32
Kenny Roota2d7e3e2010-03-15 14:26:36 -070033#include <openssl/evp.h>
34#include <openssl/sha.h>
35
Dmitry Shmidt5af38c32010-02-10 11:10:39 -080036#define LOG_TAG "SoftapController"
Dan Albertb67219a2015-03-13 22:35:27 -070037#include <base/file.h>
38#include <base/stringprintf.h>
Dmitry Shmidt5af38c32010-02-10 11:10:39 -080039#include <cutils/log.h>
Dmitry Shmidt389f8d12011-07-21 15:16:04 -070040#include <netutils/ifc.h>
Irfan Sheriff78dcb762011-07-22 15:20:21 -070041#include <private/android_filesystem_config.h>
Dmitry Shmidtfe15b632011-07-19 13:55:25 -070042#include "wifi.h"
Sasha Levitskiy25753d52013-01-10 12:48:39 -080043#include "ResponseCode.h"
Dmitry Shmidt5af38c32010-02-10 11:10:39 -080044
45#include "SoftapController.h"
46
Dan Albertb67219a2015-03-13 22:35:27 -070047using android::base::StringPrintf;
48using android::base::WriteStringToFile;
49
Irfan Sheriff78dcb762011-07-22 15:20:21 -070050static const char HOSTAPD_CONF_FILE[] = "/data/misc/wifi/hostapd.conf";
Sasha Levitskiy25753d52013-01-10 12:48:39 -080051static const char HOSTAPD_BIN_FILE[] = "/system/bin/hostapd";
Irfan Sheriff78dcb762011-07-22 15:20:21 -070052
Sasha Levitskiy25753d52013-01-10 12:48:39 -080053SoftapController::SoftapController()
54 : mPid(0) {}
Dmitry Shmidt5af38c32010-02-10 11:10:39 -080055
56SoftapController::~SoftapController() {
Dmitry Shmidt84c65a62010-03-03 13:14:30 -080057}
58
Dmitry Shmidt5af38c32010-02-10 11:10:39 -080059int SoftapController::startSoftap() {
60 pid_t pid = 1;
61
Dmitry Shmidt5af38c32010-02-10 11:10:39 -080062 if (mPid) {
Sasha Levitskiy25753d52013-01-10 12:48:39 -080063 ALOGE("SoftAP is already running");
64 return ResponseCode::SoftapStatusResult;
Dmitry Shmidt84c65a62010-03-03 13:14:30 -080065 }
Irfan Sheriff7e9eb7b2012-06-15 16:11:31 -070066
Dmitry Shmidt6fa06b72014-09-05 16:39:29 -070067 if (ensure_entropy_file_exists() < 0) {
68 ALOGE("Wi-Fi entropy file was not created");
69 }
70
Dmitry Shmidt389f8d12011-07-21 15:16:04 -070071 if ((pid = fork()) < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +000072 ALOGE("fork failed (%s)", strerror(errno));
Sasha Levitskiy25753d52013-01-10 12:48:39 -080073 return ResponseCode::ServiceStartFailed;
Dmitry Shmidt5af38c32010-02-10 11:10:39 -080074 }
Irfan Sheriff7e9eb7b2012-06-15 16:11:31 -070075
Dmitry Shmidt5af38c32010-02-10 11:10:39 -080076 if (!pid) {
Dmitry Shmidt01e182f2011-07-25 10:51:56 -070077 ensure_entropy_file_exists();
Sasha Levitskiy25753d52013-01-10 12:48:39 -080078 if (execl(HOSTAPD_BIN_FILE, HOSTAPD_BIN_FILE,
Dmitry Shmidt01e182f2011-07-25 10:51:56 -070079 "-e", WIFI_ENTROPY_FILE,
Irfan Sheriff78dcb762011-07-22 15:20:21 -070080 HOSTAPD_CONF_FILE, (char *) NULL)) {
Steve Block5ea0c052012-01-06 19:18:11 +000081 ALOGE("execl failed (%s)", strerror(errno));
Dmitry Shmidt389f8d12011-07-21 15:16:04 -070082 }
Sasha Levitskiy25753d52013-01-10 12:48:39 -080083 ALOGE("SoftAP failed to start");
84 return ResponseCode::ServiceStartFailed;
Dmitry Shmidt5af38c32010-02-10 11:10:39 -080085 } else {
Irfan Sheriff7e9eb7b2012-06-15 16:11:31 -070086 mPid = pid;
Sasha Levitskiy25753d52013-01-10 12:48:39 -080087 ALOGD("SoftAP started successfully");
Irfan Sheriff7e9eb7b2012-06-15 16:11:31 -070088 usleep(AP_BSS_START_DELAY);
Dmitry Shmidt5af38c32010-02-10 11:10:39 -080089 }
Sasha Levitskiy25753d52013-01-10 12:48:39 -080090 return ResponseCode::SoftapStatusResult;
Dmitry Shmidt5af38c32010-02-10 11:10:39 -080091}
92
93int SoftapController::stopSoftap() {
Dmitry Shmidt84c65a62010-03-03 13:14:30 -080094
Dmitry Shmidt5af38c32010-02-10 11:10:39 -080095 if (mPid == 0) {
Sasha Levitskiy25753d52013-01-10 12:48:39 -080096 ALOGE("SoftAP is not running");
97 return ResponseCode::SoftapStatusResult;
Dmitry Shmidt5af38c32010-02-10 11:10:39 -080098 }
Dmitry Shmidt389f8d12011-07-21 15:16:04 -070099
Sasha Levitskiy25753d52013-01-10 12:48:39 -0800100 ALOGD("Stopping the SoftAP service...");
Dmitry Shmidt389f8d12011-07-21 15:16:04 -0700101 kill(mPid, SIGTERM);
102 waitpid(mPid, NULL, 0);
Irfan Sheriff7e9eb7b2012-06-15 16:11:31 -0700103
Dmitry Shmidt5af38c32010-02-10 11:10:39 -0800104 mPid = 0;
Sasha Levitskiy25753d52013-01-10 12:48:39 -0800105 ALOGD("SoftAP stopped successfully");
Dmitry Shmidt3df450a2010-03-18 13:06:47 -0700106 usleep(AP_BSS_STOP_DELAY);
Sasha Levitskiy25753d52013-01-10 12:48:39 -0800107 return ResponseCode::SoftapStatusResult;
Dmitry Shmidt5af38c32010-02-10 11:10:39 -0800108}
109
110bool SoftapController::isSoftapStarted() {
Sasha Levitskiy25753d52013-01-10 12:48:39 -0800111 return (mPid != 0);
Dmitry Shmidt5af38c32010-02-10 11:10:39 -0800112}
113
Dmitry Shmidt84c65a62010-03-03 13:14:30 -0800114/*
115 * Arguments:
Sasha Levitskiy25753d52013-01-10 12:48:39 -0800116 * argv[2] - wlan interface
117 * argv[3] - SSID
Dmitry Shmidteb59b572013-04-16 13:16:05 -0700118 * argv[4] - Broadcast/Hidden
Dmitry Shmidt85e6c5f2013-06-10 14:35:43 -0700119 * argv[5] - Channel
120 * argv[6] - Security
121 * argv[7] - Key
Dmitry Shmidt84c65a62010-03-03 13:14:30 -0800122 */
Dmitry Shmidt5af38c32010-02-10 11:10:39 -0800123int SoftapController::setSoftap(int argc, char *argv[]) {
Dmitry Shmidteb59b572013-04-16 13:16:05 -0700124 int hidden = 0;
Dmitry Shmidt85e6c5f2013-06-10 14:35:43 -0700125 int channel = AP_CHANNEL_DEFAULT;
Irfan Sheriff78dcb762011-07-22 15:20:21 -0700126
Dmitry Shmidteb59b572013-04-16 13:16:05 -0700127 if (argc < 5) {
128 ALOGE("Softap set is missing arguments. Please use:");
Dmitry Shmidt85e6c5f2013-06-10 14:35:43 -0700129 ALOGE("softap <wlan iface> <SSID> <hidden/broadcast> <channel> <wpa2?-psk|open> <passphrase>");
Dmitry Shmidteb59b572013-04-16 13:16:05 -0700130 return ResponseCode::CommandSyntaxError;
131 }
132
133 if (!strcasecmp(argv[4], "hidden"))
134 hidden = 1;
135
Dmitry Shmidt85e6c5f2013-06-10 14:35:43 -0700136 if (argc >= 5) {
137 channel = atoi(argv[5]);
138 if (channel <= 0)
139 channel = AP_CHANNEL_DEFAULT;
140 }
Irfan Sheriff78dcb762011-07-22 15:20:21 -0700141
Dan Albertb67219a2015-03-13 22:35:27 -0700142 std::string wbuf(StringPrintf("interface=%s\n"
Elliott Hughesbd378322015-02-04 13:25:14 -0800143 "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 Shmidt85e6c5f2013-06-10 14:35:43 -0700152
Elliott Hughesbd378322015-02-04 13:25:14 -0800153 std::string fbuf;
Dmitry Shmidt85e6c5f2013-06-10 14:35:43 -0700154 if (argc > 7) {
Elliott Hughesbd378322015-02-04 13:25:14 -0800155 char psk_str[2*SHA256_DIGEST_LENGTH+1];
Dmitry Shmidt85e6c5f2013-06-10 14:35:43 -0700156 if (!strcmp(argv[6], "wpa-psk")) {
157 generatePsk(argv[3], argv[7], psk_str);
Dan Albertb67219a2015-03-13 22:35:27 -0700158 fbuf = StringPrintf("%swpa=3\nwpa_pairwise=TKIP CCMP\nwpa_psk=%s\n", wbuf.c_str(), psk_str);
Dmitry Shmidt85e6c5f2013-06-10 14:35:43 -0700159 } else if (!strcmp(argv[6], "wpa2-psk")) {
160 generatePsk(argv[3], argv[7], psk_str);
Dan Albertb67219a2015-03-13 22:35:27 -0700161 fbuf = StringPrintf("%swpa=2\nrsn_pairwise=CCMP\nwpa_psk=%s\n", wbuf.c_str(), psk_str);
Dmitry Shmidt85e6c5f2013-06-10 14:35:43 -0700162 } else if (!strcmp(argv[6], "open")) {
Elliott Hughesbd378322015-02-04 13:25:14 -0800163 fbuf = wbuf;
Dmitry Shmidteb59b572013-04-16 13:16:05 -0700164 }
Dmitry Shmidt85e6c5f2013-06-10 14:35:43 -0700165 } else if (argc > 6) {
166 if (!strcmp(argv[6], "open")) {
Elliott Hughesbd378322015-02-04 13:25:14 -0800167 fbuf = wbuf;
Irfan Sheriff78dcb762011-07-22 15:20:21 -0700168 }
169 } else {
Elliott Hughesbd378322015-02-04 13:25:14 -0800170 fbuf = wbuf;
Irfan Sheriff78dcb762011-07-22 15:20:21 -0700171 }
172
Dan Albertb67219a2015-03-13 22:35:27 -0700173 if (!WriteStringToFile(fbuf, HOSTAPD_CONF_FILE, 0660, AID_SYSTEM, AID_WIFI)) {
Steve Block5ea0c052012-01-06 19:18:11 +0000174 ALOGE("Cannot write to \"%s\": %s", HOSTAPD_CONF_FILE, strerror(errno));
Sasha Levitskiy25753d52013-01-10 12:48:39 -0800175 return ResponseCode::OperationFailed;
Irfan Sheriff78dcb762011-07-22 15:20:21 -0700176 }
Elliott Hughesbd378322015-02-04 13:25:14 -0800177 return ResponseCode::SoftapStatusResult;
Dmitry Shmidt5af38c32010-02-10 11:10:39 -0800178}
Dmitry Shmidt31fd6c52010-03-12 10:01:58 -0800179
Sasha Levitskiy25753d52013-01-10 12:48:39 -0800180/*
181 * Arguments:
182 * argv[2] - interface name
183 * argv[3] - AP or P2P or STA
184 */
185int SoftapController::fwReloadSoftap(int argc, char *argv[])
186{
Sasha Levitskiy25753d52013-01-10 12:48:39 -0800187 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 Sheriff78dcb762011-07-22 15:20:21 -0700213void 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 Levitskiy25753d52013-01-10 12:48:39 -0800221 sprintf(&psk_str[j*2], "%02x", psk[j]);
Irfan Sheriff78dcb762011-07-22 15:20:21 -0700222 }
Dmitry Shmidt666fe252011-03-08 11:01:58 -0800223}