blob: 0759bde70495e4c6ef0ea9effcc9663866e30e33 [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"
37#include <cutils/log.h>
Dmitry Shmidt389f8d12011-07-21 15:16:04 -070038#include <netutils/ifc.h>
Irfan Sheriff78dcb762011-07-22 15:20:21 -070039#include <private/android_filesystem_config.h>
Elliott Hughesbd378322015-02-04 13:25:14 -080040#include <utils/file.h>
41#include <utils/stringprintf.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
Irfan Sheriff78dcb762011-07-22 15:20:21 -070047static const char HOSTAPD_CONF_FILE[] = "/data/misc/wifi/hostapd.conf";
Sasha Levitskiy25753d52013-01-10 12:48:39 -080048static const char HOSTAPD_BIN_FILE[] = "/system/bin/hostapd";
Irfan Sheriff78dcb762011-07-22 15:20:21 -070049
Sasha Levitskiy25753d52013-01-10 12:48:39 -080050SoftapController::SoftapController()
51 : mPid(0) {}
Dmitry Shmidt5af38c32010-02-10 11:10:39 -080052
53SoftapController::~SoftapController() {
Dmitry Shmidt84c65a62010-03-03 13:14:30 -080054}
55
Dmitry Shmidt5af38c32010-02-10 11:10:39 -080056int SoftapController::startSoftap() {
57 pid_t pid = 1;
58
Dmitry Shmidt5af38c32010-02-10 11:10:39 -080059 if (mPid) {
Sasha Levitskiy25753d52013-01-10 12:48:39 -080060 ALOGE("SoftAP is already running");
61 return ResponseCode::SoftapStatusResult;
Dmitry Shmidt84c65a62010-03-03 13:14:30 -080062 }
Irfan Sheriff7e9eb7b2012-06-15 16:11:31 -070063
Dmitry Shmidt6fa06b72014-09-05 16:39:29 -070064 if (ensure_entropy_file_exists() < 0) {
65 ALOGE("Wi-Fi entropy file was not created");
66 }
67
Dmitry Shmidt389f8d12011-07-21 15:16:04 -070068 if ((pid = fork()) < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +000069 ALOGE("fork failed (%s)", strerror(errno));
Sasha Levitskiy25753d52013-01-10 12:48:39 -080070 return ResponseCode::ServiceStartFailed;
Dmitry Shmidt5af38c32010-02-10 11:10:39 -080071 }
Irfan Sheriff7e9eb7b2012-06-15 16:11:31 -070072
Dmitry Shmidt5af38c32010-02-10 11:10:39 -080073 if (!pid) {
Dmitry Shmidt01e182f2011-07-25 10:51:56 -070074 ensure_entropy_file_exists();
Sasha Levitskiy25753d52013-01-10 12:48:39 -080075 if (execl(HOSTAPD_BIN_FILE, HOSTAPD_BIN_FILE,
Dmitry Shmidt01e182f2011-07-25 10:51:56 -070076 "-e", WIFI_ENTROPY_FILE,
Irfan Sheriff78dcb762011-07-22 15:20:21 -070077 HOSTAPD_CONF_FILE, (char *) NULL)) {
Steve Block5ea0c052012-01-06 19:18:11 +000078 ALOGE("execl failed (%s)", strerror(errno));
Dmitry Shmidt389f8d12011-07-21 15:16:04 -070079 }
Sasha Levitskiy25753d52013-01-10 12:48:39 -080080 ALOGE("SoftAP failed to start");
81 return ResponseCode::ServiceStartFailed;
Dmitry Shmidt5af38c32010-02-10 11:10:39 -080082 } else {
Irfan Sheriff7e9eb7b2012-06-15 16:11:31 -070083 mPid = pid;
Sasha Levitskiy25753d52013-01-10 12:48:39 -080084 ALOGD("SoftAP started successfully");
Irfan Sheriff7e9eb7b2012-06-15 16:11:31 -070085 usleep(AP_BSS_START_DELAY);
Dmitry Shmidt5af38c32010-02-10 11:10:39 -080086 }
Sasha Levitskiy25753d52013-01-10 12:48:39 -080087 return ResponseCode::SoftapStatusResult;
Dmitry Shmidt5af38c32010-02-10 11:10:39 -080088}
89
90int SoftapController::stopSoftap() {
Dmitry Shmidt84c65a62010-03-03 13:14:30 -080091
Dmitry Shmidt5af38c32010-02-10 11:10:39 -080092 if (mPid == 0) {
Sasha Levitskiy25753d52013-01-10 12:48:39 -080093 ALOGE("SoftAP is not running");
94 return ResponseCode::SoftapStatusResult;
Dmitry Shmidt5af38c32010-02-10 11:10:39 -080095 }
Dmitry Shmidt389f8d12011-07-21 15:16:04 -070096
Sasha Levitskiy25753d52013-01-10 12:48:39 -080097 ALOGD("Stopping the SoftAP service...");
Dmitry Shmidt389f8d12011-07-21 15:16:04 -070098 kill(mPid, SIGTERM);
99 waitpid(mPid, NULL, 0);
Irfan Sheriff7e9eb7b2012-06-15 16:11:31 -0700100
Dmitry Shmidt5af38c32010-02-10 11:10:39 -0800101 mPid = 0;
Sasha Levitskiy25753d52013-01-10 12:48:39 -0800102 ALOGD("SoftAP stopped successfully");
Dmitry Shmidt3df450a2010-03-18 13:06:47 -0700103 usleep(AP_BSS_STOP_DELAY);
Sasha Levitskiy25753d52013-01-10 12:48:39 -0800104 return ResponseCode::SoftapStatusResult;
Dmitry Shmidt5af38c32010-02-10 11:10:39 -0800105}
106
107bool SoftapController::isSoftapStarted() {
Sasha Levitskiy25753d52013-01-10 12:48:39 -0800108 return (mPid != 0);
Dmitry Shmidt5af38c32010-02-10 11:10:39 -0800109}
110
Dmitry Shmidt84c65a62010-03-03 13:14:30 -0800111/*
112 * Arguments:
Sasha Levitskiy25753d52013-01-10 12:48:39 -0800113 * argv[2] - wlan interface
114 * argv[3] - SSID
Dmitry Shmidteb59b572013-04-16 13:16:05 -0700115 * argv[4] - Broadcast/Hidden
Dmitry Shmidt85e6c5f2013-06-10 14:35:43 -0700116 * argv[5] - Channel
117 * argv[6] - Security
118 * argv[7] - Key
Dmitry Shmidt84c65a62010-03-03 13:14:30 -0800119 */
Dmitry Shmidt5af38c32010-02-10 11:10:39 -0800120int SoftapController::setSoftap(int argc, char *argv[]) {
Dmitry Shmidteb59b572013-04-16 13:16:05 -0700121 int hidden = 0;
Dmitry Shmidt85e6c5f2013-06-10 14:35:43 -0700122 int channel = AP_CHANNEL_DEFAULT;
Irfan Sheriff78dcb762011-07-22 15:20:21 -0700123
Dmitry Shmidteb59b572013-04-16 13:16:05 -0700124 if (argc < 5) {
125 ALOGE("Softap set is missing arguments. Please use:");
Dmitry Shmidt85e6c5f2013-06-10 14:35:43 -0700126 ALOGE("softap <wlan iface> <SSID> <hidden/broadcast> <channel> <wpa2?-psk|open> <passphrase>");
Dmitry Shmidteb59b572013-04-16 13:16:05 -0700127 return ResponseCode::CommandSyntaxError;
128 }
129
130 if (!strcasecmp(argv[4], "hidden"))
131 hidden = 1;
132
Dmitry Shmidt85e6c5f2013-06-10 14:35:43 -0700133 if (argc >= 5) {
134 channel = atoi(argv[5]);
135 if (channel <= 0)
136 channel = AP_CHANNEL_DEFAULT;
137 }
Irfan Sheriff78dcb762011-07-22 15:20:21 -0700138
Elliott Hughesbd378322015-02-04 13:25:14 -0800139 std::string wbuf(android::StringPrintf("interface=%s\n"
140 "driver=nl80211\n"
141 "ctrl_interface=/data/misc/wifi/hostapd\n"
142 "ssid=%s\n"
143 "channel=%d\n"
144 "ieee80211n=1\n"
145 "hw_mode=g\n"
146 "ignore_broadcast_ssid=%d\n"
147 "wowlan_triggers=any\n",
148 argv[2], argv[3], channel, hidden));
Dmitry Shmidt85e6c5f2013-06-10 14:35:43 -0700149
Elliott Hughesbd378322015-02-04 13:25:14 -0800150 std::string fbuf;
Dmitry Shmidt85e6c5f2013-06-10 14:35:43 -0700151 if (argc > 7) {
Elliott Hughesbd378322015-02-04 13:25:14 -0800152 char psk_str[2*SHA256_DIGEST_LENGTH+1];
Dmitry Shmidt85e6c5f2013-06-10 14:35:43 -0700153 if (!strcmp(argv[6], "wpa-psk")) {
154 generatePsk(argv[3], argv[7], psk_str);
Elliott Hughesbd378322015-02-04 13:25:14 -0800155 fbuf = android::StringPrintf("%swpa=3\nwpa_pairwise=TKIP CCMP\nwpa_psk=%s\n", wbuf.c_str(), psk_str);
Dmitry Shmidt85e6c5f2013-06-10 14:35:43 -0700156 } else if (!strcmp(argv[6], "wpa2-psk")) {
157 generatePsk(argv[3], argv[7], psk_str);
Elliott Hughesbd378322015-02-04 13:25:14 -0800158 fbuf = android::StringPrintf("%swpa=2\nrsn_pairwise=CCMP\nwpa_psk=%s\n", wbuf.c_str(), psk_str);
Dmitry Shmidt85e6c5f2013-06-10 14:35:43 -0700159 } else if (!strcmp(argv[6], "open")) {
Elliott Hughesbd378322015-02-04 13:25:14 -0800160 fbuf = wbuf;
Dmitry Shmidteb59b572013-04-16 13:16:05 -0700161 }
Dmitry Shmidt85e6c5f2013-06-10 14:35:43 -0700162 } else if (argc > 6) {
163 if (!strcmp(argv[6], "open")) {
Elliott Hughesbd378322015-02-04 13:25:14 -0800164 fbuf = wbuf;
Irfan Sheriff78dcb762011-07-22 15:20:21 -0700165 }
166 } else {
Elliott Hughesbd378322015-02-04 13:25:14 -0800167 fbuf = wbuf;
Irfan Sheriff78dcb762011-07-22 15:20:21 -0700168 }
169
Elliott Hughesbd378322015-02-04 13:25:14 -0800170 if (!android::WriteStringToFile(fbuf, HOSTAPD_CONF_FILE, 0660, AID_SYSTEM, AID_WIFI)) {
Steve Block5ea0c052012-01-06 19:18:11 +0000171 ALOGE("Cannot write to \"%s\": %s", HOSTAPD_CONF_FILE, strerror(errno));
Sasha Levitskiy25753d52013-01-10 12:48:39 -0800172 return ResponseCode::OperationFailed;
Irfan Sheriff78dcb762011-07-22 15:20:21 -0700173 }
Elliott Hughesbd378322015-02-04 13:25:14 -0800174 return ResponseCode::SoftapStatusResult;
Dmitry Shmidt5af38c32010-02-10 11:10:39 -0800175}
Dmitry Shmidt31fd6c52010-03-12 10:01:58 -0800176
Sasha Levitskiy25753d52013-01-10 12:48:39 -0800177/*
178 * Arguments:
179 * argv[2] - interface name
180 * argv[3] - AP or P2P or STA
181 */
182int SoftapController::fwReloadSoftap(int argc, char *argv[])
183{
Sasha Levitskiy25753d52013-01-10 12:48:39 -0800184 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 Sheriff78dcb762011-07-22 15:20:21 -0700210void 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 Levitskiy25753d52013-01-10 12:48:39 -0800218 sprintf(&psk_str[j*2], "%02x", psk[j]);
Irfan Sheriff78dcb762011-07-22 15:20:21 -0700219 }
Dmitry Shmidt666fe252011-03-08 11:01:58 -0800220}