blob: 0b1899cfcdbad0a2594174f7718a5199936fdddb [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 Shmidt389f8d12011-07-21 15:16:04 -070062 if ((pid = fork()) < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +000063 ALOGE("fork failed (%s)", strerror(errno));
Sasha Levitskiy25753d52013-01-10 12:48:39 -080064 return ResponseCode::ServiceStartFailed;
Dmitry Shmidt5af38c32010-02-10 11:10:39 -080065 }
Irfan Sheriff7e9eb7b2012-06-15 16:11:31 -070066
Dmitry Shmidt5af38c32010-02-10 11:10:39 -080067 if (!pid) {
Dmitry Shmidt01e182f2011-07-25 10:51:56 -070068 ensure_entropy_file_exists();
Sasha Levitskiy25753d52013-01-10 12:48:39 -080069 if (execl(HOSTAPD_BIN_FILE, HOSTAPD_BIN_FILE,
Dmitry Shmidt01e182f2011-07-25 10:51:56 -070070 "-e", WIFI_ENTROPY_FILE,
Irfan Sheriff78dcb762011-07-22 15:20:21 -070071 HOSTAPD_CONF_FILE, (char *) NULL)) {
Steve Block5ea0c052012-01-06 19:18:11 +000072 ALOGE("execl failed (%s)", strerror(errno));
Dmitry Shmidt389f8d12011-07-21 15:16:04 -070073 }
Sasha Levitskiy25753d52013-01-10 12:48:39 -080074 ALOGE("SoftAP failed to start");
75 return ResponseCode::ServiceStartFailed;
Dmitry Shmidt5af38c32010-02-10 11:10:39 -080076 } else {
Irfan Sheriff7e9eb7b2012-06-15 16:11:31 -070077 mPid = pid;
Sasha Levitskiy25753d52013-01-10 12:48:39 -080078 ALOGD("SoftAP started successfully");
Irfan Sheriff7e9eb7b2012-06-15 16:11:31 -070079 usleep(AP_BSS_START_DELAY);
Dmitry Shmidt5af38c32010-02-10 11:10:39 -080080 }
Sasha Levitskiy25753d52013-01-10 12:48:39 -080081 return ResponseCode::SoftapStatusResult;
Dmitry Shmidt5af38c32010-02-10 11:10:39 -080082}
83
84int SoftapController::stopSoftap() {
Dmitry Shmidt84c65a62010-03-03 13:14:30 -080085
Dmitry Shmidt5af38c32010-02-10 11:10:39 -080086 if (mPid == 0) {
Sasha Levitskiy25753d52013-01-10 12:48:39 -080087 ALOGE("SoftAP is not running");
88 return ResponseCode::SoftapStatusResult;
Dmitry Shmidt5af38c32010-02-10 11:10:39 -080089 }
Dmitry Shmidt389f8d12011-07-21 15:16:04 -070090
Sasha Levitskiy25753d52013-01-10 12:48:39 -080091 ALOGD("Stopping the SoftAP service...");
Dmitry Shmidt389f8d12011-07-21 15:16:04 -070092 kill(mPid, SIGTERM);
93 waitpid(mPid, NULL, 0);
Irfan Sheriff7e9eb7b2012-06-15 16:11:31 -070094
Dmitry Shmidt5af38c32010-02-10 11:10:39 -080095 mPid = 0;
Sasha Levitskiy25753d52013-01-10 12:48:39 -080096 ALOGD("SoftAP stopped successfully");
Dmitry Shmidt3df450a2010-03-18 13:06:47 -070097 usleep(AP_BSS_STOP_DELAY);
Sasha Levitskiy25753d52013-01-10 12:48:39 -080098 return ResponseCode::SoftapStatusResult;
Dmitry Shmidt5af38c32010-02-10 11:10:39 -080099}
100
101bool SoftapController::isSoftapStarted() {
Sasha Levitskiy25753d52013-01-10 12:48:39 -0800102 return (mPid != 0);
Dmitry Shmidt5af38c32010-02-10 11:10:39 -0800103}
104
Dmitry Shmidt84c65a62010-03-03 13:14:30 -0800105/*
106 * Arguments:
Sasha Levitskiy25753d52013-01-10 12:48:39 -0800107 * argv[2] - wlan interface
108 * argv[3] - SSID
109 * argv[4] - Security
110 * argv[5] - Key
Dmitry Shmidt84c65a62010-03-03 13:14:30 -0800111 */
Dmitry Shmidt5af38c32010-02-10 11:10:39 -0800112int SoftapController::setSoftap(int argc, char *argv[]) {
Kenny Roota2d7e3e2010-03-15 14:26:36 -0700113 char psk_str[2*SHA256_DIGEST_LENGTH+1];
Sasha Levitskiy25753d52013-01-10 12:48:39 -0800114 int ret = ResponseCode::SoftapStatusResult;
115 int i = 0;
116 int fd;
Dmitry Shmidt84c65a62010-03-03 13:14:30 -0800117
Dmitry Shmidt84c65a62010-03-03 13:14:30 -0800118 if (argc < 4) {
Sasha Levitskiy25753d52013-01-10 12:48:39 -0800119 ALOGE("Softap set is missing arguments. Please use: softap <wlan iface> <SSID> <wpa2?-psk|open> <passphrase>");
120 return ResponseCode::CommandSyntaxError;
Dmitry Shmidt84c65a62010-03-03 13:14:30 -0800121 }
122
Irfan Sheriff78dcb762011-07-22 15:20:21 -0700123 char *wbuf = NULL;
124 char *fbuf = NULL;
125
Irfan Sheriff78dcb762011-07-22 15:20:21 -0700126 asprintf(&wbuf, "interface=%s\ndriver=nl80211\nctrl_interface="
Jeff Johnsond5b19ca2013-04-11 15:11:27 -0700127 "/data/misc/wifi/hostapd\nssid=%s\nchannel=6\nieee80211n=1\n"
128 "hw_mode=g\n",
Sasha Levitskiy25753d52013-01-10 12:48:39 -0800129 argv[2], argv[3]);
Irfan Sheriff78dcb762011-07-22 15:20:21 -0700130
Irfan Sheriff7e9eb7b2012-06-15 16:11:31 -0700131 if (argc > 4) {
132 if (!strcmp(argv[4], "wpa-psk")) {
Sasha Levitskiy25753d52013-01-10 12:48:39 -0800133 generatePsk(argv[3], argv[5], psk_str);
Irfan Sheriff54b75b42011-10-25 20:37:18 -0700134 asprintf(&fbuf, "%swpa=1\nwpa_pairwise=TKIP CCMP\nwpa_psk=%s\n", wbuf, psk_str);
Irfan Sheriff7e9eb7b2012-06-15 16:11:31 -0700135 } else if (!strcmp(argv[4], "wpa2-psk")) {
Sasha Levitskiy25753d52013-01-10 12:48:39 -0800136 generatePsk(argv[3], argv[5], psk_str);
Irfan Sheriff54b75b42011-10-25 20:37:18 -0700137 asprintf(&fbuf, "%swpa=2\nrsn_pairwise=CCMP\nwpa_psk=%s\n", wbuf, psk_str);
Irfan Sheriff7e9eb7b2012-06-15 16:11:31 -0700138 } else if (!strcmp(argv[4], "open")) {
Irfan Sheriff78dcb762011-07-22 15:20:21 -0700139 asprintf(&fbuf, "%s", wbuf);
140 }
141 } else {
142 asprintf(&fbuf, "%s", wbuf);
143 }
144
Robert Greenwalt43682d92012-11-29 15:44:25 -0800145 fd = open(HOSTAPD_CONF_FILE, O_CREAT | O_TRUNC | O_WRONLY | O_NOFOLLOW, 0660);
Irfan Sheriff78dcb762011-07-22 15:20:21 -0700146 if (fd < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000147 ALOGE("Cannot update \"%s\": %s", HOSTAPD_CONF_FILE, strerror(errno));
Irfan Sheriff78dcb762011-07-22 15:20:21 -0700148 free(wbuf);
149 free(fbuf);
Sasha Levitskiy25753d52013-01-10 12:48:39 -0800150 return ResponseCode::OperationFailed;
Irfan Sheriff78dcb762011-07-22 15:20:21 -0700151 }
152 if (write(fd, fbuf, strlen(fbuf)) < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000153 ALOGE("Cannot write to \"%s\": %s", HOSTAPD_CONF_FILE, strerror(errno));
Sasha Levitskiy25753d52013-01-10 12:48:39 -0800154 ret = ResponseCode::OperationFailed;
Irfan Sheriff78dcb762011-07-22 15:20:21 -0700155 }
Irfan Sheriff78dcb762011-07-22 15:20:21 -0700156 free(wbuf);
157 free(fbuf);
158
159 /* Note: apparently open can fail to set permissions correctly at times */
Robert Greenwalte0644322012-11-29 10:51:33 -0800160 if (fchmod(fd, 0660) < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000161 ALOGE("Error changing permissions of %s to 0660: %s",
Irfan Sheriff78dcb762011-07-22 15:20:21 -0700162 HOSTAPD_CONF_FILE, strerror(errno));
Robert Greenwalte0644322012-11-29 10:51:33 -0800163 close(fd);
Irfan Sheriff78dcb762011-07-22 15:20:21 -0700164 unlink(HOSTAPD_CONF_FILE);
Sasha Levitskiy25753d52013-01-10 12:48:39 -0800165 return ResponseCode::OperationFailed;
Irfan Sheriff78dcb762011-07-22 15:20:21 -0700166 }
167
Robert Greenwalte0644322012-11-29 10:51:33 -0800168 if (fchown(fd, AID_SYSTEM, AID_WIFI) < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000169 ALOGE("Error changing group ownership of %s to %d: %s",
Irfan Sheriff78dcb762011-07-22 15:20:21 -0700170 HOSTAPD_CONF_FILE, AID_WIFI, strerror(errno));
Robert Greenwalte0644322012-11-29 10:51:33 -0800171 close(fd);
Irfan Sheriff78dcb762011-07-22 15:20:21 -0700172 unlink(HOSTAPD_CONF_FILE);
Sasha Levitskiy25753d52013-01-10 12:48:39 -0800173 return ResponseCode::OperationFailed;
Irfan Sheriff78dcb762011-07-22 15:20:21 -0700174 }
175
Robert Greenwalte0644322012-11-29 10:51:33 -0800176 close(fd);
Dmitry Shmidt84c65a62010-03-03 13:14:30 -0800177 return ret;
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{
187 int i = 0;
188 char *fwpath = NULL;
189
190 if (argc < 4) {
191 ALOGE("SoftAP fwreload is missing arguments. Please use: softap <wlan iface> <AP|P2P|STA>");
192 return ResponseCode::CommandSyntaxError;
193 }
194
195 if (strcmp(argv[3], "AP") == 0) {
196 fwpath = (char *)wifi_get_fw_path(WIFI_GET_FW_PATH_AP);
197 } else if (strcmp(argv[3], "P2P") == 0) {
198 fwpath = (char *)wifi_get_fw_path(WIFI_GET_FW_PATH_P2P);
199 } else if (strcmp(argv[3], "STA") == 0) {
200 fwpath = (char *)wifi_get_fw_path(WIFI_GET_FW_PATH_STA);
201 }
202 if (!fwpath)
203 return ResponseCode::CommandParameterError;
204 if (wifi_change_fw_path((const char *)fwpath)) {
205 ALOGE("Softap fwReload failed");
206 return ResponseCode::OperationFailed;
207 }
208 else {
209 ALOGD("Softap fwReload - Ok");
210 }
211 return ResponseCode::SoftapStatusResult;
212}
213
Irfan Sheriff78dcb762011-07-22 15:20:21 -0700214void SoftapController::generatePsk(char *ssid, char *passphrase, char *psk_str) {
215 unsigned char psk[SHA256_DIGEST_LENGTH];
216 int j;
217 // Use the PKCS#5 PBKDF2 with 4096 iterations
218 PKCS5_PBKDF2_HMAC_SHA1(passphrase, strlen(passphrase),
219 reinterpret_cast<const unsigned char *>(ssid), strlen(ssid),
220 4096, SHA256_DIGEST_LENGTH, psk);
221 for (j=0; j < SHA256_DIGEST_LENGTH; j++) {
Sasha Levitskiy25753d52013-01-10 12:48:39 -0800222 sprintf(&psk_str[j*2], "%02x", psk[j]);
Irfan Sheriff78dcb762011-07-22 15:20:21 -0700223 }
Dmitry Shmidt666fe252011-03-08 11:01:58 -0800224}