blob: 558aa06f8c6a3531c3f3a2255729a3d4936533cb [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 Shmidt389f8d12011-07-21 15:16:04 -070064 if ((pid = fork()) < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +000065 ALOGE("fork failed (%s)", strerror(errno));
Sasha Levitskiy25753d52013-01-10 12:48:39 -080066 return ResponseCode::ServiceStartFailed;
Dmitry Shmidt5af38c32010-02-10 11:10:39 -080067 }
Irfan Sheriff7e9eb7b2012-06-15 16:11:31 -070068
Dmitry Shmidt5af38c32010-02-10 11:10:39 -080069 if (!pid) {
Dmitry Shmidt01e182f2011-07-25 10:51:56 -070070 ensure_entropy_file_exists();
Sasha Levitskiy25753d52013-01-10 12:48:39 -080071 if (execl(HOSTAPD_BIN_FILE, HOSTAPD_BIN_FILE,
Dmitry Shmidt01e182f2011-07-25 10:51:56 -070072 "-e", WIFI_ENTROPY_FILE,
Irfan Sheriff78dcb762011-07-22 15:20:21 -070073 HOSTAPD_CONF_FILE, (char *) NULL)) {
Steve Block5ea0c052012-01-06 19:18:11 +000074 ALOGE("execl failed (%s)", strerror(errno));
Dmitry Shmidt389f8d12011-07-21 15:16:04 -070075 }
Sasha Levitskiy25753d52013-01-10 12:48:39 -080076 ALOGE("SoftAP failed to start");
77 return ResponseCode::ServiceStartFailed;
Dmitry Shmidt5af38c32010-02-10 11:10:39 -080078 } else {
Irfan Sheriff7e9eb7b2012-06-15 16:11:31 -070079 mPid = pid;
Sasha Levitskiy25753d52013-01-10 12:48:39 -080080 ALOGD("SoftAP started successfully");
Irfan Sheriff7e9eb7b2012-06-15 16:11:31 -070081 usleep(AP_BSS_START_DELAY);
Dmitry Shmidt5af38c32010-02-10 11:10:39 -080082 }
Sasha Levitskiy25753d52013-01-10 12:48:39 -080083 return ResponseCode::SoftapStatusResult;
Dmitry Shmidt5af38c32010-02-10 11:10:39 -080084}
85
86int SoftapController::stopSoftap() {
Dmitry Shmidt84c65a62010-03-03 13:14:30 -080087
Dmitry Shmidt5af38c32010-02-10 11:10:39 -080088 if (mPid == 0) {
Sasha Levitskiy25753d52013-01-10 12:48:39 -080089 ALOGE("SoftAP is not running");
90 return ResponseCode::SoftapStatusResult;
Dmitry Shmidt5af38c32010-02-10 11:10:39 -080091 }
Dmitry Shmidt389f8d12011-07-21 15:16:04 -070092
Sasha Levitskiy25753d52013-01-10 12:48:39 -080093 ALOGD("Stopping the SoftAP service...");
Dmitry Shmidt389f8d12011-07-21 15:16:04 -070094 kill(mPid, SIGTERM);
95 waitpid(mPid, NULL, 0);
Irfan Sheriff7e9eb7b2012-06-15 16:11:31 -070096
Dmitry Shmidt5af38c32010-02-10 11:10:39 -080097 mPid = 0;
Sasha Levitskiy25753d52013-01-10 12:48:39 -080098 ALOGD("SoftAP stopped successfully");
Dmitry Shmidt3df450a2010-03-18 13:06:47 -070099 usleep(AP_BSS_STOP_DELAY);
Sasha Levitskiy25753d52013-01-10 12:48:39 -0800100 return ResponseCode::SoftapStatusResult;
Dmitry Shmidt5af38c32010-02-10 11:10:39 -0800101}
102
103bool SoftapController::isSoftapStarted() {
Sasha Levitskiy25753d52013-01-10 12:48:39 -0800104 return (mPid != 0);
Dmitry Shmidt5af38c32010-02-10 11:10:39 -0800105}
106
Dmitry Shmidt84c65a62010-03-03 13:14:30 -0800107/*
108 * Arguments:
Sasha Levitskiy25753d52013-01-10 12:48:39 -0800109 * argv[2] - wlan interface
110 * argv[3] - SSID
Dmitry Shmidteb59b572013-04-16 13:16:05 -0700111 * argv[4] - Broadcast/Hidden
Dmitry Shmidt85e6c5f2013-06-10 14:35:43 -0700112 * argv[5] - Channel
113 * argv[6] - Security
114 * argv[7] - Key
Dmitry Shmidt84c65a62010-03-03 13:14:30 -0800115 */
Dmitry Shmidt5af38c32010-02-10 11:10:39 -0800116int SoftapController::setSoftap(int argc, char *argv[]) {
Dmitry Shmidteb59b572013-04-16 13:16:05 -0700117 int hidden = 0;
Dmitry Shmidt85e6c5f2013-06-10 14:35:43 -0700118 int channel = AP_CHANNEL_DEFAULT;
Irfan Sheriff78dcb762011-07-22 15:20:21 -0700119
Dmitry Shmidteb59b572013-04-16 13:16:05 -0700120 if (argc < 5) {
121 ALOGE("Softap set is missing arguments. Please use:");
Dmitry Shmidt85e6c5f2013-06-10 14:35:43 -0700122 ALOGE("softap <wlan iface> <SSID> <hidden/broadcast> <channel> <wpa2?-psk|open> <passphrase>");
Dmitry Shmidteb59b572013-04-16 13:16:05 -0700123 return ResponseCode::CommandSyntaxError;
124 }
125
126 if (!strcasecmp(argv[4], "hidden"))
127 hidden = 1;
128
Dmitry Shmidt85e6c5f2013-06-10 14:35:43 -0700129 if (argc >= 5) {
130 channel = atoi(argv[5]);
131 if (channel <= 0)
132 channel = AP_CHANNEL_DEFAULT;
133 }
Irfan Sheriff78dcb762011-07-22 15:20:21 -0700134
Elliott Hughesbd378322015-02-04 13:25:14 -0800135 std::string wbuf(android::StringPrintf("interface=%s\n"
136 "driver=nl80211\n"
137 "ctrl_interface=/data/misc/wifi/hostapd\n"
138 "ssid=%s\n"
139 "channel=%d\n"
140 "ieee80211n=1\n"
141 "hw_mode=g\n"
142 "ignore_broadcast_ssid=%d\n"
143 "wowlan_triggers=any\n",
144 argv[2], argv[3], channel, hidden));
Dmitry Shmidt85e6c5f2013-06-10 14:35:43 -0700145
Elliott Hughesbd378322015-02-04 13:25:14 -0800146 std::string fbuf;
Dmitry Shmidt85e6c5f2013-06-10 14:35:43 -0700147 if (argc > 7) {
Elliott Hughesbd378322015-02-04 13:25:14 -0800148 char psk_str[2*SHA256_DIGEST_LENGTH+1];
Dmitry Shmidt85e6c5f2013-06-10 14:35:43 -0700149 if (!strcmp(argv[6], "wpa-psk")) {
150 generatePsk(argv[3], argv[7], psk_str);
Elliott Hughesbd378322015-02-04 13:25:14 -0800151 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 -0700152 } else if (!strcmp(argv[6], "wpa2-psk")) {
153 generatePsk(argv[3], argv[7], psk_str);
Elliott Hughesbd378322015-02-04 13:25:14 -0800154 fbuf = android::StringPrintf("%swpa=2\nrsn_pairwise=CCMP\nwpa_psk=%s\n", wbuf.c_str(), psk_str);
Dmitry Shmidt85e6c5f2013-06-10 14:35:43 -0700155 } else if (!strcmp(argv[6], "open")) {
Elliott Hughesbd378322015-02-04 13:25:14 -0800156 fbuf = wbuf;
Dmitry Shmidteb59b572013-04-16 13:16:05 -0700157 }
Dmitry Shmidt85e6c5f2013-06-10 14:35:43 -0700158 } else if (argc > 6) {
159 if (!strcmp(argv[6], "open")) {
Elliott Hughesbd378322015-02-04 13:25:14 -0800160 fbuf = wbuf;
Irfan Sheriff78dcb762011-07-22 15:20:21 -0700161 }
162 } else {
Elliott Hughesbd378322015-02-04 13:25:14 -0800163 fbuf = wbuf;
Irfan Sheriff78dcb762011-07-22 15:20:21 -0700164 }
165
Elliott Hughesbd378322015-02-04 13:25:14 -0800166 if (!android::WriteStringToFile(fbuf, HOSTAPD_CONF_FILE, 0660, AID_SYSTEM, AID_WIFI)) {
Steve Block5ea0c052012-01-06 19:18:11 +0000167 ALOGE("Cannot write to \"%s\": %s", HOSTAPD_CONF_FILE, strerror(errno));
Sasha Levitskiy25753d52013-01-10 12:48:39 -0800168 return ResponseCode::OperationFailed;
Irfan Sheriff78dcb762011-07-22 15:20:21 -0700169 }
Elliott Hughesbd378322015-02-04 13:25:14 -0800170 return ResponseCode::SoftapStatusResult;
Dmitry Shmidt5af38c32010-02-10 11:10:39 -0800171}
Dmitry Shmidt31fd6c52010-03-12 10:01:58 -0800172
Sasha Levitskiy25753d52013-01-10 12:48:39 -0800173/*
174 * Arguments:
175 * argv[2] - interface name
176 * argv[3] - AP or P2P or STA
177 */
178int SoftapController::fwReloadSoftap(int argc, char *argv[])
179{
Sasha Levitskiy25753d52013-01-10 12:48:39 -0800180 char *fwpath = NULL;
181
182 if (argc < 4) {
183 ALOGE("SoftAP fwreload is missing arguments. Please use: softap <wlan iface> <AP|P2P|STA>");
184 return ResponseCode::CommandSyntaxError;
185 }
186
187 if (strcmp(argv[3], "AP") == 0) {
188 fwpath = (char *)wifi_get_fw_path(WIFI_GET_FW_PATH_AP);
189 } else if (strcmp(argv[3], "P2P") == 0) {
190 fwpath = (char *)wifi_get_fw_path(WIFI_GET_FW_PATH_P2P);
191 } else if (strcmp(argv[3], "STA") == 0) {
192 fwpath = (char *)wifi_get_fw_path(WIFI_GET_FW_PATH_STA);
193 }
194 if (!fwpath)
195 return ResponseCode::CommandParameterError;
196 if (wifi_change_fw_path((const char *)fwpath)) {
197 ALOGE("Softap fwReload failed");
198 return ResponseCode::OperationFailed;
199 }
200 else {
201 ALOGD("Softap fwReload - Ok");
202 }
203 return ResponseCode::SoftapStatusResult;
204}
205
Irfan Sheriff78dcb762011-07-22 15:20:21 -0700206void SoftapController::generatePsk(char *ssid, char *passphrase, char *psk_str) {
207 unsigned char psk[SHA256_DIGEST_LENGTH];
208 int j;
209 // Use the PKCS#5 PBKDF2 with 4096 iterations
210 PKCS5_PBKDF2_HMAC_SHA1(passphrase, strlen(passphrase),
211 reinterpret_cast<const unsigned char *>(ssid), strlen(ssid),
212 4096, SHA256_DIGEST_LENGTH, psk);
213 for (j=0; j < SHA256_DIGEST_LENGTH; j++) {
Sasha Levitskiy25753d52013-01-10 12:48:39 -0800214 sprintf(&psk_str[j*2], "%02x", psk[j]);
Irfan Sheriff78dcb762011-07-22 15:20:21 -0700215 }
Dmitry Shmidt666fe252011-03-08 11:01:58 -0800216}