2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <sys/socket.h>
24 #include <sys/ioctl.h>
25 #include <sys/types.h>
28 #include <netinet/in.h>
29 #include <arpa/inet.h>
31 #include <linux/wireless.h>
33 #include <openssl/evp.h>
34 #include <openssl/sha.h>
36 #define LOG_TAG "SoftapController"
37 #include <cutils/log.h>
38 #include <netutils/ifc.h>
39 #include <private/android_filesystem_config.h>
42 #include "SoftapController.h"
44 static const char HOSTAPD_CONF_FILE[] = "/data/misc/wifi/hostapd.conf";
46 SoftapController::SoftapController() {
48 mSock = socket(AF_INET, SOCK_DGRAM, 0);
50 ALOGE("Failed to open socket");
53 SoftapController::~SoftapController() {
58 int SoftapController::startSoftap() {
63 ALOGE("Softap already started");
67 ALOGE("Softap startap - failed to open socket");
71 if ((pid = fork()) < 0) {
72 ALOGE("fork failed (%s)", strerror(errno));
77 ensure_entropy_file_exists();
78 if (execl("/system/bin/hostapd", "/system/bin/hostapd",
79 "-e", WIFI_ENTROPY_FILE,
80 HOSTAPD_CONF_FILE, (char *) NULL)) {
81 ALOGE("execl failed (%s)", strerror(errno));
83 ALOGE("Should never get here!");
87 ALOGD("Softap startap - Ok");
88 usleep(AP_BSS_START_DELAY);
94 int SoftapController::stopSoftap() {
97 ALOGE("Softap already stopped");
101 ALOGD("Stopping Softap service");
103 waitpid(mPid, NULL, 0);
106 ALOGE("Softap stopap - failed to open socket");
110 ALOGD("Softap service stopped");
111 usleep(AP_BSS_STOP_DELAY);
115 bool SoftapController::isSoftapStarted() {
116 return (mPid != 0 ? true : false);
121 * argv[2] - wlan interface
129 int SoftapController::setSoftap(int argc, char *argv[]) {
130 char psk_str[2*SHA256_DIGEST_LENGTH+1];
131 int ret = 0, i = 0, fd;
135 ALOGE("Softap set - failed to open socket");
139 ALOGE("Softap set - missing arguments");
151 ssid = (char *)"AndroidAP";
154 asprintf(&wbuf, "interface=%s\ndriver=nl80211\nctrl_interface="
155 "/data/misc/wifi/hostapd\nssid=%s\nchannel=6\nieee80211n=1\n",
159 if (!strcmp(argv[4], "wpa-psk")) {
160 generatePsk(ssid, argv[5], psk_str);
161 asprintf(&fbuf, "%swpa=1\nwpa_pairwise=TKIP CCMP\nwpa_psk=%s\n", wbuf, psk_str);
162 } else if (!strcmp(argv[4], "wpa2-psk")) {
163 generatePsk(ssid, argv[5], psk_str);
164 asprintf(&fbuf, "%swpa=2\nrsn_pairwise=CCMP\nwpa_psk=%s\n", wbuf, psk_str);
165 } else if (!strcmp(argv[4], "open")) {
166 asprintf(&fbuf, "%s", wbuf);
169 asprintf(&fbuf, "%s", wbuf);
172 fd = open(HOSTAPD_CONF_FILE, O_CREAT | O_TRUNC | O_WRONLY | O_NOFOLLOW, 0660);
174 ALOGE("Cannot update \"%s\": %s", HOSTAPD_CONF_FILE, strerror(errno));
179 if (write(fd, fbuf, strlen(fbuf)) < 0) {
180 ALOGE("Cannot write to \"%s\": %s", HOSTAPD_CONF_FILE, strerror(errno));
186 /* Note: apparently open can fail to set permissions correctly at times */
187 if (fchmod(fd, 0660) < 0) {
188 ALOGE("Error changing permissions of %s to 0660: %s",
189 HOSTAPD_CONF_FILE, strerror(errno));
191 unlink(HOSTAPD_CONF_FILE);
195 if (fchown(fd, AID_SYSTEM, AID_WIFI) < 0) {
196 ALOGE("Error changing group ownership of %s to %d: %s",
197 HOSTAPD_CONF_FILE, AID_WIFI, strerror(errno));
199 unlink(HOSTAPD_CONF_FILE);
207 void SoftapController::generatePsk(char *ssid, char *passphrase, char *psk_str) {
208 unsigned char psk[SHA256_DIGEST_LENGTH];
210 // Use the PKCS#5 PBKDF2 with 4096 iterations
211 PKCS5_PBKDF2_HMAC_SHA1(passphrase, strlen(passphrase),
212 reinterpret_cast<const unsigned char *>(ssid), strlen(ssid),
213 4096, SHA256_DIGEST_LENGTH, psk);
214 for (j=0; j < SHA256_DIGEST_LENGTH; j++) {
215 sprintf(&psk_str[j<<1], "%02x", psk[j]);
217 psk_str[j<<1] = '\0';
223 * argv[2] - interface name
224 * argv[3] - AP or STA
226 int SoftapController::fwReloadSoftap(int argc, char *argv[])
233 ALOGE("Softap fwrealod - failed to open socket");
237 ALOGE("Softap fwreload - missing arguments");
243 if (strcmp(argv[3], "AP") == 0) {
244 fwpath = (char *)wifi_get_fw_path(WIFI_GET_FW_PATH_AP);
245 } else if (strcmp(argv[3], "P2P") == 0) {
246 fwpath = (char *)wifi_get_fw_path(WIFI_GET_FW_PATH_P2P);
248 fwpath = (char *)wifi_get_fw_path(WIFI_GET_FW_PATH_STA);
252 ret = wifi_change_fw_path((const char *)fwpath);
254 ALOGE("Softap fwReload - failed: %d", ret);
257 ALOGD("Softap fwReload - Ok");
262 int SoftapController::clientsSoftap(char **retbuf)
264 /* TODO: implement over hostapd */