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");
51 memset(mIface, 0, sizeof(mIface));
54 SoftapController::~SoftapController() {
59 int SoftapController::setCommand(char *iface, const char *fname, unsigned buflen) {
63 char tBuf[SOFTAP_MAX_BUFFER_SIZE];
65 struct iw_priv_args *priv_ptr;
67 int cmd = 0, sub_cmd = 0;
69 strncpy(wrq.ifr_name, iface, sizeof(wrq.ifr_name));
70 wrq.u.data.pointer = tBuf;
71 wrq.u.data.length = sizeof(tBuf) / sizeof(struct iw_priv_args);
73 if ((ret = ioctl(mSock, SIOCGIWPRIV, &wrq)) < 0) {
74 ALOGE("SIOCGIPRIV failed: %d", ret);
78 priv_ptr = (struct iw_priv_args *)wrq.u.data.pointer;
79 for(i=0; i < wrq.u.data.length;i++) {
80 if (strcmp(priv_ptr[i].name, fname) == 0) {
81 cmd = priv_ptr[i].cmd;
86 if (i == wrq.u.data.length) {
87 ALOGE("iface:%s, fname: %s - function not supported", iface, fname);
91 if (cmd < SIOCDEVPRIVATE) {
92 for(j=0; j < i; j++) {
93 if ((priv_ptr[j].set_args == priv_ptr[i].set_args) &&
94 (priv_ptr[j].get_args == priv_ptr[i].get_args) &&
95 (priv_ptr[j].name[0] == '\0'))
99 ALOGE("iface:%s, fname: %s - invalid private ioctl", iface, fname);
103 cmd = priv_ptr[j].cmd;
106 strncpy(wrq.ifr_name, iface, sizeof(wrq.ifr_name));
107 if ((buflen == 0) && (*mBuf != 0))
108 wrq.u.data.length = strlen(mBuf) + 1;
110 wrq.u.data.length = buflen;
111 wrq.u.data.pointer = mBuf;
112 wrq.u.data.flags = sub_cmd;
113 ret = ioctl(mSock, cmd, &wrq);
118 int SoftapController::startDriver(char *iface) {
122 ALOGE("Softap driver start - failed to open socket");
125 if (!iface || (iface[0] == '\0')) {
126 ALOGD("Softap driver start - wrong interface");
131 ret = setCommand(iface, "START");
133 ALOGE("Softap driver start: %d", ret);
141 usleep(AP_DRIVER_START_DELAY);
142 ALOGD("Softap driver start: %d", ret);
146 int SoftapController::stopDriver(char *iface) {
150 ALOGE("Softap driver stop - failed to open socket");
153 if (!iface || (iface[0] == '\0')) {
154 ALOGD("Softap driver stop - wrong interface");
160 ret = ifc_down(iface);
163 ALOGE("Softap %s down: %d", iface, ret);
166 ret = setCommand(iface, "STOP");
167 ALOGD("Softap driver stop: %d", ret);
171 int SoftapController::startSoftap() {
176 ALOGE("Softap already started");
180 ALOGE("Softap startap - failed to open socket");
184 if ((pid = fork()) < 0) {
185 ALOGE("fork failed (%s)", strerror(errno));
191 ensure_entropy_file_exists();
192 if (execl("/system/bin/hostapd", "/system/bin/hostapd",
193 "-e", WIFI_ENTROPY_FILE,
194 HOSTAPD_CONF_FILE, (char *) NULL)) {
195 ALOGE("execl failed (%s)", strerror(errno));
198 ALOGE("Should never get here!");
202 ret = setCommand(mIface, "AP_BSS_START");
204 ALOGE("Softap startap - failed: %d", ret);
208 ALOGD("Softap startap - Ok");
209 usleep(AP_BSS_START_DELAY);
216 int SoftapController::stopSoftap() {
220 ALOGE("Softap already stopped");
225 ALOGD("Stopping Softap service");
227 waitpid(mPid, NULL, 0);
230 ALOGE("Softap stopap - failed to open socket");
234 ret = setCommand(mIface, "AP_BSS_STOP");
236 ALOGD("Softap service stopped: %d", ret);
237 usleep(AP_BSS_STOP_DELAY);
241 bool SoftapController::isSoftapStarted() {
242 return (mPid != 0 ? true : false);
245 int SoftapController::addParam(int pos, const char *cmd, const char *arg)
249 if ((unsigned)(pos + strlen(cmd) + strlen(arg) + 1) >= sizeof(mBuf)) {
250 ALOGE("Command line is too big");
253 pos += sprintf(&mBuf[pos], "%s=%s,", cmd, arg);
259 * argv[2] - wlan interface
260 * argv[3] - softap interface
268 int SoftapController::setSoftap(int argc, char *argv[]) {
269 char psk_str[2*SHA256_DIGEST_LENGTH+1];
270 int ret = 0, i = 0, fd;
274 ALOGE("Softap set - failed to open socket");
278 ALOGE("Softap set - missing arguments");
282 strncpy(mIface, argv[3], sizeof(mIface));
292 ssid = (char *)"AndroidAP";
295 asprintf(&wbuf, "interface=%s\ndriver=nl80211\nctrl_interface="
296 "/data/misc/wifi/hostapd\nssid=%s\nchannel=6\nieee80211n=1\n",
300 if (!strcmp(argv[5], "wpa-psk")) {
301 generatePsk(ssid, argv[6], psk_str);
302 asprintf(&fbuf, "%swpa=1\nwpa_pairwise=TKIP CCMP\nwpa_psk=%s\n", wbuf, psk_str);
303 } else if (!strcmp(argv[5], "wpa2-psk")) {
304 generatePsk(ssid, argv[6], psk_str);
305 asprintf(&fbuf, "%swpa=2\nrsn_pairwise=CCMP\nwpa_psk=%s\n", wbuf, psk_str);
306 } else if (!strcmp(argv[5], "open")) {
307 asprintf(&fbuf, "%s", wbuf);
310 asprintf(&fbuf, "%s", wbuf);
313 fd = open(HOSTAPD_CONF_FILE, O_CREAT | O_TRUNC | O_WRONLY, 0660);
315 ALOGE("Cannot update \"%s\": %s", HOSTAPD_CONF_FILE, strerror(errno));
320 if (write(fd, fbuf, strlen(fbuf)) < 0) {
321 ALOGE("Cannot write to \"%s\": %s", HOSTAPD_CONF_FILE, strerror(errno));
328 /* Note: apparently open can fail to set permissions correctly at times */
329 if (chmod(HOSTAPD_CONF_FILE, 0660) < 0) {
330 ALOGE("Error changing permissions of %s to 0660: %s",
331 HOSTAPD_CONF_FILE, strerror(errno));
332 unlink(HOSTAPD_CONF_FILE);
336 if (chown(HOSTAPD_CONF_FILE, AID_SYSTEM, AID_WIFI) < 0) {
337 ALOGE("Error changing group ownership of %s to %d: %s",
338 HOSTAPD_CONF_FILE, AID_WIFI, strerror(errno));
339 unlink(HOSTAPD_CONF_FILE);
344 /* Create command line */
345 i = addParam(i, "ASCII_CMD", "AP_CFG");
349 ssid = (char *)"AndroidAP";
351 i = addParam(i, "SSID", ssid);
353 i = addParam(i, "SEC", argv[5]);
355 i = addParam(i, "SEC", "open");
358 generatePsk(ssid, argv[6], psk_str);
359 i = addParam(i, "KEY", psk_str);
361 i = addParam(i, "KEY", "12345678");
364 i = addParam(i, "CHANNEL", argv[7]);
366 i = addParam(i, "CHANNEL", "6");
369 i = addParam(i, "PREAMBLE", argv[8]);
371 i = addParam(i, "PREAMBLE", "0");
374 i = addParam(i, "MAX_SCB", argv[9]);
376 i = addParam(i, "MAX_SCB", "8");
378 if ((i < 0) || ((unsigned)(i + 4) >= sizeof(mBuf))) {
379 ALOGE("Softap set - command is too big");
382 sprintf(&mBuf[i], "END");
384 /* system("iwpriv eth0 WL_AP_CFG ASCII_CMD=AP_CFG,SSID=\"AndroidAP\",SEC=\"open\",KEY=12345,CHANNEL=1,PREAMBLE=0,MAX_SCB=8,END"); */
385 ret = setCommand(iface, "AP_SET_CFG");
387 ALOGE("Softap set - failed: %d", ret);
390 ALOGD("Softap set - Ok");
391 usleep(AP_SET_CFG_DELAY);
397 void SoftapController::generatePsk(char *ssid, char *passphrase, char *psk_str) {
398 unsigned char psk[SHA256_DIGEST_LENGTH];
400 // Use the PKCS#5 PBKDF2 with 4096 iterations
401 PKCS5_PBKDF2_HMAC_SHA1(passphrase, strlen(passphrase),
402 reinterpret_cast<const unsigned char *>(ssid), strlen(ssid),
403 4096, SHA256_DIGEST_LENGTH, psk);
404 for (j=0; j < SHA256_DIGEST_LENGTH; j++) {
405 sprintf(&psk_str[j<<1], "%02x", psk[j]);
407 psk_str[j<<1] = '\0';
413 * argv[2] - interface name
414 * argv[3] - AP or STA
416 int SoftapController::fwReloadSoftap(int argc, char *argv[])
423 ALOGE("Softap fwrealod - failed to open socket");
427 ALOGE("Softap fwreload - missing arguments");
433 if (strcmp(argv[3], "AP") == 0) {
434 fwpath = (char *)wifi_get_fw_path(WIFI_GET_FW_PATH_AP);
435 } else if (strcmp(argv[3], "P2P") == 0) {
436 fwpath = (char *)wifi_get_fw_path(WIFI_GET_FW_PATH_P2P);
438 fwpath = (char *)wifi_get_fw_path(WIFI_GET_FW_PATH_STA);
443 ret = wifi_change_fw_path((const char *)fwpath);
445 sprintf(mBuf, "FW_PATH=%s", fwpath);
446 ret = setCommand(iface, "WL_FW_RELOAD");
449 ALOGE("Softap fwReload - failed: %d", ret);
452 ALOGD("Softap fwReload - Ok");
457 int SoftapController::clientsSoftap(char **retbuf)
462 ALOGE("Softap clients - failed to open socket");
466 ret = setCommand(mIface, "AP_GET_STA_LIST", SOFTAP_MAX_BUFFER_SIZE);
468 ALOGE("Softap clients - failed: %d", ret);
470 asprintf(retbuf, "Softap clients:%s", mBuf);
471 ALOGD("Softap clients:%s", mBuf);