blob: 270bd516c35ff3e636402dbbd240f975d0e54159 [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>
Dmitry Shmidtfe15b632011-07-19 13:55:25 -070040#include "wifi.h"
Sasha Levitskiy25753d52013-01-10 12:48:39 -080041#include "ResponseCode.h"
Dmitry Shmidt5af38c32010-02-10 11:10:39 -080042
43#include "SoftapController.h"
44
Irfan Sheriff78dcb762011-07-22 15:20:21 -070045static const char HOSTAPD_CONF_FILE[] = "/data/misc/wifi/hostapd.conf";
Sasha Levitskiy25753d52013-01-10 12:48:39 -080046static const char HOSTAPD_BIN_FILE[] = "/system/bin/hostapd";
Irfan Sheriff78dcb762011-07-22 15:20:21 -070047
Sasha Levitskiy25753d52013-01-10 12:48:39 -080048SoftapController::SoftapController()
49 : mPid(0) {}
Dmitry Shmidt5af38c32010-02-10 11:10:39 -080050
51SoftapController::~SoftapController() {
Dmitry Shmidt84c65a62010-03-03 13:14:30 -080052}
53
Dmitry Shmidt5af38c32010-02-10 11:10:39 -080054int SoftapController::startSoftap() {
55 pid_t pid = 1;
56
Dmitry Shmidt5af38c32010-02-10 11:10:39 -080057 if (mPid) {
Sasha Levitskiy25753d52013-01-10 12:48:39 -080058 ALOGE("SoftAP is already running");
59 return ResponseCode::SoftapStatusResult;
Dmitry Shmidt84c65a62010-03-03 13:14:30 -080060 }
Irfan Sheriff7e9eb7b2012-06-15 16:11:31 -070061
Dmitry Shmidt6fa06b72014-09-05 16:39:29 -070062 if (ensure_entropy_file_exists() < 0) {
63 ALOGE("Wi-Fi entropy file was not created");
64 }
65
Dmitry Shmidt389f8d12011-07-21 15:16:04 -070066 if ((pid = fork()) < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +000067 ALOGE("fork failed (%s)", strerror(errno));
Sasha Levitskiy25753d52013-01-10 12:48:39 -080068 return ResponseCode::ServiceStartFailed;
Dmitry Shmidt5af38c32010-02-10 11:10:39 -080069 }
Irfan Sheriff7e9eb7b2012-06-15 16:11:31 -070070
Dmitry Shmidt5af38c32010-02-10 11:10:39 -080071 if (!pid) {
Dmitry Shmidt01e182f2011-07-25 10:51:56 -070072 ensure_entropy_file_exists();
Sasha Levitskiy25753d52013-01-10 12:48:39 -080073 if (execl(HOSTAPD_BIN_FILE, HOSTAPD_BIN_FILE,
Dmitry Shmidt01e182f2011-07-25 10:51:56 -070074 "-e", WIFI_ENTROPY_FILE,
Irfan Sheriff78dcb762011-07-22 15:20:21 -070075 HOSTAPD_CONF_FILE, (char *) NULL)) {
Steve Block5ea0c052012-01-06 19:18:11 +000076 ALOGE("execl failed (%s)", strerror(errno));
Dmitry Shmidt389f8d12011-07-21 15:16:04 -070077 }
Sasha Levitskiy25753d52013-01-10 12:48:39 -080078 ALOGE("SoftAP failed to start");
79 return ResponseCode::ServiceStartFailed;
Dmitry Shmidt5af38c32010-02-10 11:10:39 -080080 } else {
Irfan Sheriff7e9eb7b2012-06-15 16:11:31 -070081 mPid = pid;
Sasha Levitskiy25753d52013-01-10 12:48:39 -080082 ALOGD("SoftAP started successfully");
Irfan Sheriff7e9eb7b2012-06-15 16:11:31 -070083 usleep(AP_BSS_START_DELAY);
Dmitry Shmidt5af38c32010-02-10 11:10:39 -080084 }
Sasha Levitskiy25753d52013-01-10 12:48:39 -080085 return ResponseCode::SoftapStatusResult;
Dmitry Shmidt5af38c32010-02-10 11:10:39 -080086}
87
88int SoftapController::stopSoftap() {
Dmitry Shmidt84c65a62010-03-03 13:14:30 -080089
Dmitry Shmidt5af38c32010-02-10 11:10:39 -080090 if (mPid == 0) {
Sasha Levitskiy25753d52013-01-10 12:48:39 -080091 ALOGE("SoftAP is not running");
92 return ResponseCode::SoftapStatusResult;
Dmitry Shmidt5af38c32010-02-10 11:10:39 -080093 }
Dmitry Shmidt389f8d12011-07-21 15:16:04 -070094
Sasha Levitskiy25753d52013-01-10 12:48:39 -080095 ALOGD("Stopping the SoftAP service...");
Dmitry Shmidt389f8d12011-07-21 15:16:04 -070096 kill(mPid, SIGTERM);
97 waitpid(mPid, NULL, 0);
Irfan Sheriff7e9eb7b2012-06-15 16:11:31 -070098
Dmitry Shmidt5af38c32010-02-10 11:10:39 -080099 mPid = 0;
Sasha Levitskiy25753d52013-01-10 12:48:39 -0800100 ALOGD("SoftAP stopped successfully");
Dmitry Shmidt3df450a2010-03-18 13:06:47 -0700101 usleep(AP_BSS_STOP_DELAY);
Sasha Levitskiy25753d52013-01-10 12:48:39 -0800102 return ResponseCode::SoftapStatusResult;
Dmitry Shmidt5af38c32010-02-10 11:10:39 -0800103}
104
105bool SoftapController::isSoftapStarted() {
Sasha Levitskiy25753d52013-01-10 12:48:39 -0800106 return (mPid != 0);
Dmitry Shmidt5af38c32010-02-10 11:10:39 -0800107}
108
Dmitry Shmidt84c65a62010-03-03 13:14:30 -0800109/*
110 * Arguments:
Sasha Levitskiy25753d52013-01-10 12:48:39 -0800111 * argv[2] - wlan interface
112 * argv[3] - SSID
Dmitry Shmidteb59b572013-04-16 13:16:05 -0700113 * argv[4] - Broadcast/Hidden
Dmitry Shmidt85e6c5f2013-06-10 14:35:43 -0700114 * argv[5] - Channel
115 * argv[6] - Security
116 * argv[7] - Key
Dmitry Shmidt84c65a62010-03-03 13:14:30 -0800117 */
Dmitry Shmidt5af38c32010-02-10 11:10:39 -0800118int SoftapController::setSoftap(int argc, char *argv[]) {
Kenny Roota2d7e3e2010-03-15 14:26:36 -0700119 char psk_str[2*SHA256_DIGEST_LENGTH+1];
Sasha Levitskiy25753d52013-01-10 12:48:39 -0800120 int ret = ResponseCode::SoftapStatusResult;
Sasha Levitskiy25753d52013-01-10 12:48:39 -0800121 int fd;
Dmitry Shmidteb59b572013-04-16 13:16:05 -0700122 int hidden = 0;
Dmitry Shmidt85e6c5f2013-06-10 14:35:43 -0700123 int channel = AP_CHANNEL_DEFAULT;
Irfan Sheriff78dcb762011-07-22 15:20:21 -0700124 char *wbuf = NULL;
125 char *fbuf = NULL;
126
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
Dmitry Shmidt85e6c5f2013-06-10 14:35:43 -0700142 asprintf(&wbuf, "interface=%s\ndriver=nl80211\nctrl_interface="
143 "/data/misc/wifi/hostapd\nssid=%s\nchannel=%d\nieee80211n=1\n"
Dmitry Shmidt9e957532014-09-03 15:19:36 -0700144 "hw_mode=g\nignore_broadcast_ssid=%d\nwowlan_triggers=any\n",
Dmitry Shmidt85e6c5f2013-06-10 14:35:43 -0700145 argv[2], argv[3], channel, hidden);
146
147 if (argc > 7) {
148 if (!strcmp(argv[6], "wpa-psk")) {
149 generatePsk(argv[3], argv[7], psk_str);
Dmitry Shmidt45dff302013-11-21 16:50:22 -0800150 asprintf(&fbuf, "%swpa=3\nwpa_pairwise=TKIP CCMP\nwpa_psk=%s\n", wbuf, psk_str);
Dmitry Shmidt85e6c5f2013-06-10 14:35:43 -0700151 } else if (!strcmp(argv[6], "wpa2-psk")) {
152 generatePsk(argv[3], argv[7], psk_str);
Irfan Sheriff54b75b42011-10-25 20:37:18 -0700153 asprintf(&fbuf, "%swpa=2\nrsn_pairwise=CCMP\nwpa_psk=%s\n", wbuf, psk_str);
Dmitry Shmidt85e6c5f2013-06-10 14:35:43 -0700154 } else if (!strcmp(argv[6], "open")) {
Dmitry Shmidt0b73d662013-04-17 08:58:17 -0700155 asprintf(&fbuf, "%s", wbuf);
Dmitry Shmidteb59b572013-04-16 13:16:05 -0700156 }
Dmitry Shmidt85e6c5f2013-06-10 14:35:43 -0700157 } else if (argc > 6) {
158 if (!strcmp(argv[6], "open")) {
Irfan Sheriff78dcb762011-07-22 15:20:21 -0700159 asprintf(&fbuf, "%s", wbuf);
160 }
161 } else {
162 asprintf(&fbuf, "%s", wbuf);
163 }
164
Robert Greenwalt43682d92012-11-29 15:44:25 -0800165 fd = open(HOSTAPD_CONF_FILE, O_CREAT | O_TRUNC | O_WRONLY | O_NOFOLLOW, 0660);
Irfan Sheriff78dcb762011-07-22 15:20:21 -0700166 if (fd < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000167 ALOGE("Cannot update \"%s\": %s", HOSTAPD_CONF_FILE, strerror(errno));
Irfan Sheriff78dcb762011-07-22 15:20:21 -0700168 free(wbuf);
169 free(fbuf);
Sasha Levitskiy25753d52013-01-10 12:48:39 -0800170 return ResponseCode::OperationFailed;
Irfan Sheriff78dcb762011-07-22 15:20:21 -0700171 }
172 if (write(fd, fbuf, strlen(fbuf)) < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000173 ALOGE("Cannot write to \"%s\": %s", HOSTAPD_CONF_FILE, strerror(errno));
Sasha Levitskiy25753d52013-01-10 12:48:39 -0800174 ret = ResponseCode::OperationFailed;
Irfan Sheriff78dcb762011-07-22 15:20:21 -0700175 }
Irfan Sheriff78dcb762011-07-22 15:20:21 -0700176 free(wbuf);
177 free(fbuf);
178
179 /* Note: apparently open can fail to set permissions correctly at times */
Robert Greenwalte0644322012-11-29 10:51:33 -0800180 if (fchmod(fd, 0660) < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000181 ALOGE("Error changing permissions of %s to 0660: %s",
Irfan Sheriff78dcb762011-07-22 15:20:21 -0700182 HOSTAPD_CONF_FILE, strerror(errno));
Robert Greenwalte0644322012-11-29 10:51:33 -0800183 close(fd);
Irfan Sheriff78dcb762011-07-22 15:20:21 -0700184 unlink(HOSTAPD_CONF_FILE);
Sasha Levitskiy25753d52013-01-10 12:48:39 -0800185 return ResponseCode::OperationFailed;
Irfan Sheriff78dcb762011-07-22 15:20:21 -0700186 }
187
Robert Greenwalte0644322012-11-29 10:51:33 -0800188 if (fchown(fd, AID_SYSTEM, AID_WIFI) < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000189 ALOGE("Error changing group ownership of %s to %d: %s",
Irfan Sheriff78dcb762011-07-22 15:20:21 -0700190 HOSTAPD_CONF_FILE, AID_WIFI, strerror(errno));
Robert Greenwalte0644322012-11-29 10:51:33 -0800191 close(fd);
Irfan Sheriff78dcb762011-07-22 15:20:21 -0700192 unlink(HOSTAPD_CONF_FILE);
Sasha Levitskiy25753d52013-01-10 12:48:39 -0800193 return ResponseCode::OperationFailed;
Irfan Sheriff78dcb762011-07-22 15:20:21 -0700194 }
195
Robert Greenwalte0644322012-11-29 10:51:33 -0800196 close(fd);
Dmitry Shmidt84c65a62010-03-03 13:14:30 -0800197 return ret;
Dmitry Shmidt5af38c32010-02-10 11:10:39 -0800198}
Dmitry Shmidt31fd6c52010-03-12 10:01:58 -0800199
Sasha Levitskiy25753d52013-01-10 12:48:39 -0800200/*
201 * Arguments:
202 * argv[2] - interface name
203 * argv[3] - AP or P2P or STA
204 */
205int SoftapController::fwReloadSoftap(int argc, char *argv[])
206{
Sasha Levitskiy25753d52013-01-10 12:48:39 -0800207 char *fwpath = NULL;
208
209 if (argc < 4) {
210 ALOGE("SoftAP fwreload is missing arguments. Please use: softap <wlan iface> <AP|P2P|STA>");
211 return ResponseCode::CommandSyntaxError;
212 }
213
214 if (strcmp(argv[3], "AP") == 0) {
215 fwpath = (char *)wifi_get_fw_path(WIFI_GET_FW_PATH_AP);
216 } else if (strcmp(argv[3], "P2P") == 0) {
217 fwpath = (char *)wifi_get_fw_path(WIFI_GET_FW_PATH_P2P);
218 } else if (strcmp(argv[3], "STA") == 0) {
219 fwpath = (char *)wifi_get_fw_path(WIFI_GET_FW_PATH_STA);
220 }
221 if (!fwpath)
222 return ResponseCode::CommandParameterError;
223 if (wifi_change_fw_path((const char *)fwpath)) {
224 ALOGE("Softap fwReload failed");
225 return ResponseCode::OperationFailed;
226 }
227 else {
228 ALOGD("Softap fwReload - Ok");
229 }
230 return ResponseCode::SoftapStatusResult;
231}
232
Irfan Sheriff78dcb762011-07-22 15:20:21 -0700233void SoftapController::generatePsk(char *ssid, char *passphrase, char *psk_str) {
234 unsigned char psk[SHA256_DIGEST_LENGTH];
235 int j;
236 // Use the PKCS#5 PBKDF2 with 4096 iterations
237 PKCS5_PBKDF2_HMAC_SHA1(passphrase, strlen(passphrase),
238 reinterpret_cast<const unsigned char *>(ssid), strlen(ssid),
239 4096, SHA256_DIGEST_LENGTH, psk);
240 for (j=0; j < SHA256_DIGEST_LENGTH; j++) {
Sasha Levitskiy25753d52013-01-10 12:48:39 -0800241 sprintf(&psk_str[j*2], "%02x", psk[j]);
Irfan Sheriff78dcb762011-07-22 15:20:21 -0700242 }
Dmitry Shmidt666fe252011-03-08 11:01:58 -0800243}