blob: 3d69142ae2424f6929cf1af37542d7631c02bff2 [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"
Dmitry Shmidt5af38c32010-02-10 11:10:39 -080041
42#include "SoftapController.h"
43
Irfan Sheriff78dcb762011-07-22 15:20:21 -070044static const char HOSTAPD_CONF_FILE[] = "/data/misc/wifi/hostapd.conf";
45
Dmitry Shmidt5af38c32010-02-10 11:10:39 -080046SoftapController::SoftapController() {
47 mPid = 0;
Dmitry Shmidt84c65a62010-03-03 13:14:30 -080048 mSock = socket(AF_INET, SOCK_DGRAM, 0);
49 if (mSock < 0)
Steve Block5ea0c052012-01-06 19:18:11 +000050 ALOGE("Failed to open socket");
Dmitry Shmidt84c65a62010-03-03 13:14:30 -080051 memset(mIface, 0, sizeof(mIface));
Dmitry Shmidt5af38c32010-02-10 11:10:39 -080052}
53
54SoftapController::~SoftapController() {
Dmitry Shmidt84c65a62010-03-03 13:14:30 -080055 if (mSock >= 0)
56 close(mSock);
57}
58
Dmitry Shmidt666fe252011-03-08 11:01:58 -080059int SoftapController::setCommand(char *iface, const char *fname, unsigned buflen) {
Dmitry Shmidt389f8d12011-07-21 15:16:04 -070060#ifdef HAVE_HOSTAPD
61 return 0;
62#else
Dmitry Shmidt6665fb22010-09-22 17:31:07 -070063 char tBuf[SOFTAP_MAX_BUFFER_SIZE];
Dmitry Shmidt84c65a62010-03-03 13:14:30 -080064 struct iwreq wrq;
65 struct iw_priv_args *priv_ptr;
jmzhufbd11c42010-10-11 16:47:05 -070066 int i, j, ret;
67 int cmd = 0, sub_cmd = 0;
Dmitry Shmidt84c65a62010-03-03 13:14:30 -080068
69 strncpy(wrq.ifr_name, iface, sizeof(wrq.ifr_name));
Dmitry Shmidt6665fb22010-09-22 17:31:07 -070070 wrq.u.data.pointer = tBuf;
71 wrq.u.data.length = sizeof(tBuf) / sizeof(struct iw_priv_args);
Dmitry Shmidt84c65a62010-03-03 13:14:30 -080072 wrq.u.data.flags = 0;
73 if ((ret = ioctl(mSock, SIOCGIWPRIV, &wrq)) < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +000074 ALOGE("SIOCGIPRIV failed: %d", ret);
Dmitry Shmidt84c65a62010-03-03 13:14:30 -080075 return ret;
76 }
jmzhufbd11c42010-10-11 16:47:05 -070077
Dmitry Shmidt84c65a62010-03-03 13:14:30 -080078 priv_ptr = (struct iw_priv_args *)wrq.u.data.pointer;
jmzhufbd11c42010-10-11 16:47:05 -070079 for(i=0; i < wrq.u.data.length;i++) {
80 if (strcmp(priv_ptr[i].name, fname) == 0) {
81 cmd = priv_ptr[i].cmd;
82 break;
83 }
Dmitry Shmidt84c65a62010-03-03 13:14:30 -080084 }
Dmitry Shmidt5af38c32010-02-10 11:10:39 -080085
jmzhufbd11c42010-10-11 16:47:05 -070086 if (i == wrq.u.data.length) {
Steve Block5ea0c052012-01-06 19:18:11 +000087 ALOGE("iface:%s, fname: %s - function not supported", iface, fname);
Dmitry Shmidt6665fb22010-09-22 17:31:07 -070088 return -1;
89 }
90
jmzhufbd11c42010-10-11 16:47:05 -070091 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'))
96 break;
97 }
98 if (j == i) {
Steve Block5ea0c052012-01-06 19:18:11 +000099 ALOGE("iface:%s, fname: %s - invalid private ioctl", iface, fname);
jmzhufbd11c42010-10-11 16:47:05 -0700100 return -1;
101 }
102 sub_cmd = cmd;
103 cmd = priv_ptr[j].cmd;
104 }
105
Dmitry Shmidt6665fb22010-09-22 17:31:07 -0700106 strncpy(wrq.ifr_name, iface, sizeof(wrq.ifr_name));
Dmitry Shmidt666fe252011-03-08 11:01:58 -0800107 if ((buflen == 0) && (*mBuf != 0))
Dmitry Shmidt6665fb22010-09-22 17:31:07 -0700108 wrq.u.data.length = strlen(mBuf) + 1;
109 else
Dmitry Shmidt666fe252011-03-08 11:01:58 -0800110 wrq.u.data.length = buflen;
Dmitry Shmidt6665fb22010-09-22 17:31:07 -0700111 wrq.u.data.pointer = mBuf;
jmzhufbd11c42010-10-11 16:47:05 -0700112 wrq.u.data.flags = sub_cmd;
113 ret = ioctl(mSock, cmd, &wrq);
Dmitry Shmidt6665fb22010-09-22 17:31:07 -0700114 return ret;
Dmitry Shmidt389f8d12011-07-21 15:16:04 -0700115#endif
Dmitry Shmidt6665fb22010-09-22 17:31:07 -0700116}
117
118int SoftapController::startDriver(char *iface) {
119 int ret;
120
Dmitry Shmidtc3539e22010-03-12 14:16:46 -0800121 if (mSock < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000122 ALOGE("Softap driver start - failed to open socket");
Dmitry Shmidtc3539e22010-03-12 14:16:46 -0800123 return -1;
124 }
125 if (!iface || (iface[0] == '\0')) {
Steve Block7b984e32011-12-20 16:22:42 +0000126 ALOGD("Softap driver start - wrong interface");
Dmitry Shmidtc3539e22010-03-12 14:16:46 -0800127 iface = mIface;
128 }
Dmitry Shmidt6665fb22010-09-22 17:31:07 -0700129
130 *mBuf = 0;
131 ret = setCommand(iface, "START");
Dmitry Shmidt389f8d12011-07-21 15:16:04 -0700132 if (ret < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000133 ALOGE("Softap driver start: %d", ret);
Dmitry Shmidt389f8d12011-07-21 15:16:04 -0700134 return ret;
135 }
136#ifdef HAVE_HOSTAPD
137 ifc_init();
138 ret = ifc_up(iface);
139 ifc_close();
140#endif
Dmitry Shmidta1659132010-04-23 12:43:32 -0700141 usleep(AP_DRIVER_START_DELAY);
Steve Block7b984e32011-12-20 16:22:42 +0000142 ALOGD("Softap driver start: %d", ret);
Dmitry Shmidtc3539e22010-03-12 14:16:46 -0800143 return ret;
144}
145
146int SoftapController::stopDriver(char *iface) {
Dmitry Shmidt6665fb22010-09-22 17:31:07 -0700147 int ret;
Dmitry Shmidtc3539e22010-03-12 14:16:46 -0800148
149 if (mSock < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000150 ALOGE("Softap driver stop - failed to open socket");
Dmitry Shmidtc3539e22010-03-12 14:16:46 -0800151 return -1;
152 }
153 if (!iface || (iface[0] == '\0')) {
Steve Block7b984e32011-12-20 16:22:42 +0000154 ALOGD("Softap driver stop - wrong interface");
Dmitry Shmidtc3539e22010-03-12 14:16:46 -0800155 iface = mIface;
156 }
Dmitry Shmidt6665fb22010-09-22 17:31:07 -0700157 *mBuf = 0;
Dmitry Shmidt389f8d12011-07-21 15:16:04 -0700158#ifdef HAVE_HOSTAPD
159 ifc_init();
160 ret = ifc_down(iface);
161 ifc_close();
162 if (ret < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000163 ALOGE("Softap %s down: %d", iface, ret);
Dmitry Shmidt389f8d12011-07-21 15:16:04 -0700164 }
165#endif
Dmitry Shmidt6665fb22010-09-22 17:31:07 -0700166 ret = setCommand(iface, "STOP");
Steve Block7b984e32011-12-20 16:22:42 +0000167 ALOGD("Softap driver stop: %d", ret);
Dmitry Shmidtc3539e22010-03-12 14:16:46 -0800168 return ret;
169}
170
Dmitry Shmidt5af38c32010-02-10 11:10:39 -0800171int SoftapController::startSoftap() {
172 pid_t pid = 1;
Dmitry Shmidt6665fb22010-09-22 17:31:07 -0700173 int ret = 0;
Dmitry Shmidt5af38c32010-02-10 11:10:39 -0800174
Dmitry Shmidt5af38c32010-02-10 11:10:39 -0800175 if (mPid) {
Steve Block5ea0c052012-01-06 19:18:11 +0000176 ALOGE("Softap already started");
Dmitry Shmidta1659132010-04-23 12:43:32 -0700177 return 0;
Dmitry Shmidt5af38c32010-02-10 11:10:39 -0800178 }
Dmitry Shmidt84c65a62010-03-03 13:14:30 -0800179 if (mSock < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000180 ALOGE("Softap startap - failed to open socket");
Dmitry Shmidt84c65a62010-03-03 13:14:30 -0800181 return -1;
182 }
Dmitry Shmidt389f8d12011-07-21 15:16:04 -0700183#ifdef HAVE_HOSTAPD
184 if ((pid = fork()) < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000185 ALOGE("fork failed (%s)", strerror(errno));
Dmitry Shmidt5af38c32010-02-10 11:10:39 -0800186 return -1;
187 }
188#endif
189 if (!pid) {
Dmitry Shmidt389f8d12011-07-21 15:16:04 -0700190#ifdef HAVE_HOSTAPD
Dmitry Shmidt01e182f2011-07-25 10:51:56 -0700191 ensure_entropy_file_exists();
Irfan Sheriff78dcb762011-07-22 15:20:21 -0700192 if (execl("/system/bin/hostapd", "/system/bin/hostapd",
Dmitry Shmidt01e182f2011-07-25 10:51:56 -0700193 "-e", WIFI_ENTROPY_FILE,
Irfan Sheriff78dcb762011-07-22 15:20:21 -0700194 HOSTAPD_CONF_FILE, (char *) NULL)) {
Steve Block5ea0c052012-01-06 19:18:11 +0000195 ALOGE("execl failed (%s)", strerror(errno));
Dmitry Shmidt389f8d12011-07-21 15:16:04 -0700196 }
197#endif
Steve Block5ea0c052012-01-06 19:18:11 +0000198 ALOGE("Should never get here!");
Dmitry Shmidt389f8d12011-07-21 15:16:04 -0700199 return -1;
Dmitry Shmidt5af38c32010-02-10 11:10:39 -0800200 } else {
Dmitry Shmidt6665fb22010-09-22 17:31:07 -0700201 *mBuf = 0;
202 ret = setCommand(mIface, "AP_BSS_START");
Dmitry Shmidt84c65a62010-03-03 13:14:30 -0800203 if (ret) {
Steve Block5ea0c052012-01-06 19:18:11 +0000204 ALOGE("Softap startap - failed: %d", ret);
Dmitry Shmidt84c65a62010-03-03 13:14:30 -0800205 }
206 else {
207 mPid = pid;
Steve Block7b984e32011-12-20 16:22:42 +0000208 ALOGD("Softap startap - Ok");
Dmitry Shmidt3df450a2010-03-18 13:06:47 -0700209 usleep(AP_BSS_START_DELAY);
Dmitry Shmidt84c65a62010-03-03 13:14:30 -0800210 }
Dmitry Shmidt5af38c32010-02-10 11:10:39 -0800211 }
Dmitry Shmidt84c65a62010-03-03 13:14:30 -0800212 return ret;
Dmitry Shmidt5af38c32010-02-10 11:10:39 -0800213
214}
215
216int SoftapController::stopSoftap() {
Dmitry Shmidt6665fb22010-09-22 17:31:07 -0700217 int ret;
Dmitry Shmidt84c65a62010-03-03 13:14:30 -0800218
Dmitry Shmidt5af38c32010-02-10 11:10:39 -0800219 if (mPid == 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000220 ALOGE("Softap already stopped");
Dmitry Shmidt5af38c32010-02-10 11:10:39 -0800221 return 0;
222 }
Dmitry Shmidt389f8d12011-07-21 15:16:04 -0700223
224#ifdef HAVE_HOSTAPD
Steve Block7b984e32011-12-20 16:22:42 +0000225 ALOGD("Stopping Softap service");
Dmitry Shmidt389f8d12011-07-21 15:16:04 -0700226 kill(mPid, SIGTERM);
227 waitpid(mPid, NULL, 0);
228#endif
Dmitry Shmidt84c65a62010-03-03 13:14:30 -0800229 if (mSock < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000230 ALOGE("Softap stopap - failed to open socket");
Dmitry Shmidt84c65a62010-03-03 13:14:30 -0800231 return -1;
232 }
Dmitry Shmidt6665fb22010-09-22 17:31:07 -0700233 *mBuf = 0;
234 ret = setCommand(mIface, "AP_BSS_STOP");
Dmitry Shmidt5af38c32010-02-10 11:10:39 -0800235 mPid = 0;
Steve Block7b984e32011-12-20 16:22:42 +0000236 ALOGD("Softap service stopped: %d", ret);
Dmitry Shmidt3df450a2010-03-18 13:06:47 -0700237 usleep(AP_BSS_STOP_DELAY);
Dmitry Shmidt84c65a62010-03-03 13:14:30 -0800238 return ret;
Dmitry Shmidt5af38c32010-02-10 11:10:39 -0800239}
240
241bool SoftapController::isSoftapStarted() {
242 return (mPid != 0 ? true : false);
243}
244
Dmitry Shmidt7977d672010-03-09 10:57:59 -0800245int SoftapController::addParam(int pos, const char *cmd, const char *arg)
246{
247 if (pos < 0)
248 return pos;
249 if ((unsigned)(pos + strlen(cmd) + strlen(arg) + 1) >= sizeof(mBuf)) {
Steve Block5ea0c052012-01-06 19:18:11 +0000250 ALOGE("Command line is too big");
Dmitry Shmidt7977d672010-03-09 10:57:59 -0800251 return -1;
252 }
253 pos += sprintf(&mBuf[pos], "%s=%s,", cmd, arg);
254 return pos;
255}
256
Dmitry Shmidt84c65a62010-03-03 13:14:30 -0800257/*
258 * Arguments:
259 * argv[2] - wlan interface
260 * argv[3] - softap interface
Dmitry Shmidt7977d672010-03-09 10:57:59 -0800261 * argv[4] - SSID
262 * argv[5] - Security
263 * argv[6] - Key
264 * argv[7] - Channel
265 * argv[8] - Preamble
266 * argv[9] - Max SCB
Dmitry Shmidt84c65a62010-03-03 13:14:30 -0800267 */
Dmitry Shmidt5af38c32010-02-10 11:10:39 -0800268int SoftapController::setSoftap(int argc, char *argv[]) {
Kenny Roota2d7e3e2010-03-15 14:26:36 -0700269 char psk_str[2*SHA256_DIGEST_LENGTH+1];
Irfan Sheriff78dcb762011-07-22 15:20:21 -0700270 int ret = 0, i = 0, fd;
Dmitry Shmidt6665fb22010-09-22 17:31:07 -0700271 char *ssid, *iface;
Dmitry Shmidt84c65a62010-03-03 13:14:30 -0800272
Dmitry Shmidt84c65a62010-03-03 13:14:30 -0800273 if (mSock < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000274 ALOGE("Softap set - failed to open socket");
Dmitry Shmidt84c65a62010-03-03 13:14:30 -0800275 return -1;
276 }
277 if (argc < 4) {
Steve Block5ea0c052012-01-06 19:18:11 +0000278 ALOGE("Softap set - missing arguments");
Dmitry Shmidt84c65a62010-03-03 13:14:30 -0800279 return -1;
280 }
281
Dmitry Shmidt84c65a62010-03-03 13:14:30 -0800282 strncpy(mIface, argv[3], sizeof(mIface));
Dmitry Shmidt6665fb22010-09-22 17:31:07 -0700283 iface = argv[2];
Dmitry Shmidt7977d672010-03-09 10:57:59 -0800284
Irfan Sheriff78dcb762011-07-22 15:20:21 -0700285#ifdef HAVE_HOSTAPD
286 char *wbuf = NULL;
287 char *fbuf = NULL;
288
289 if (argc > 4) {
290 ssid = argv[4];
291 } else {
292 ssid = (char *)"AndroidAP";
293 }
294
295 asprintf(&wbuf, "interface=%s\ndriver=nl80211\nctrl_interface="
Dmitry Shmidt093e3c72012-03-16 12:33:34 -0700296 "/data/misc/wifi/hostapd\nssid=%s\nchannel=6\nieee80211n=1\n",
Dmitry Shmidtfa81a802012-02-28 12:41:45 -0800297 iface, ssid);
Irfan Sheriff78dcb762011-07-22 15:20:21 -0700298
299 if (argc > 5) {
300 if (!strcmp(argv[5], "wpa-psk")) {
301 generatePsk(ssid, argv[6], psk_str);
Irfan Sheriff54b75b42011-10-25 20:37:18 -0700302 asprintf(&fbuf, "%swpa=1\nwpa_pairwise=TKIP CCMP\nwpa_psk=%s\n", wbuf, psk_str);
Irfan Sheriff78dcb762011-07-22 15:20:21 -0700303 } else if (!strcmp(argv[5], "wpa2-psk")) {
304 generatePsk(ssid, argv[6], psk_str);
Irfan Sheriff54b75b42011-10-25 20:37:18 -0700305 asprintf(&fbuf, "%swpa=2\nrsn_pairwise=CCMP\nwpa_psk=%s\n", wbuf, psk_str);
Irfan Sheriff78dcb762011-07-22 15:20:21 -0700306 } else if (!strcmp(argv[5], "open")) {
307 asprintf(&fbuf, "%s", wbuf);
308 }
309 } else {
310 asprintf(&fbuf, "%s", wbuf);
311 }
312
Robert Greenwalt43682d92012-11-29 15:44:25 -0800313 fd = open(HOSTAPD_CONF_FILE, O_CREAT | O_TRUNC | O_WRONLY | O_NOFOLLOW, 0660);
Irfan Sheriff78dcb762011-07-22 15:20:21 -0700314 if (fd < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000315 ALOGE("Cannot update \"%s\": %s", HOSTAPD_CONF_FILE, strerror(errno));
Irfan Sheriff78dcb762011-07-22 15:20:21 -0700316 free(wbuf);
317 free(fbuf);
318 return -1;
319 }
320 if (write(fd, fbuf, strlen(fbuf)) < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000321 ALOGE("Cannot write to \"%s\": %s", HOSTAPD_CONF_FILE, strerror(errno));
Irfan Sheriff78dcb762011-07-22 15:20:21 -0700322 ret = -1;
323 }
Irfan Sheriff78dcb762011-07-22 15:20:21 -0700324 free(wbuf);
325 free(fbuf);
326
327 /* Note: apparently open can fail to set permissions correctly at times */
Robert Greenwalt43682d92012-11-29 15:44:25 -0800328 if (fchmod(fd, 0660) < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000329 ALOGE("Error changing permissions of %s to 0660: %s",
Irfan Sheriff78dcb762011-07-22 15:20:21 -0700330 HOSTAPD_CONF_FILE, strerror(errno));
Robert Greenwalt43682d92012-11-29 15:44:25 -0800331 close(fd);
Irfan Sheriff78dcb762011-07-22 15:20:21 -0700332 unlink(HOSTAPD_CONF_FILE);
333 return -1;
334 }
335
Robert Greenwalt43682d92012-11-29 15:44:25 -0800336 if (fchown(fd, AID_SYSTEM, AID_WIFI) < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000337 ALOGE("Error changing group ownership of %s to %d: %s",
Irfan Sheriff78dcb762011-07-22 15:20:21 -0700338 HOSTAPD_CONF_FILE, AID_WIFI, strerror(errno));
Robert Greenwalt43682d92012-11-29 15:44:25 -0800339 close(fd);
Irfan Sheriff78dcb762011-07-22 15:20:21 -0700340 unlink(HOSTAPD_CONF_FILE);
341 return -1;
342 }
343
Robert Greenwalt43682d92012-11-29 15:44:25 -0800344 close(fd);
Irfan Sheriff78dcb762011-07-22 15:20:21 -0700345#else
Dmitry Shmidt7977d672010-03-09 10:57:59 -0800346 /* Create command line */
347 i = addParam(i, "ASCII_CMD", "AP_CFG");
348 if (argc > 4) {
Dmitry Shmidt321f95a2010-03-09 15:46:34 -0800349 ssid = argv[4];
Dmitry Shmidt7977d672010-03-09 10:57:59 -0800350 } else {
Dmitry Shmidt321f95a2010-03-09 15:46:34 -0800351 ssid = (char *)"AndroidAP";
Dmitry Shmidt84c65a62010-03-03 13:14:30 -0800352 }
Dmitry Shmidt321f95a2010-03-09 15:46:34 -0800353 i = addParam(i, "SSID", ssid);
Dmitry Shmidt7977d672010-03-09 10:57:59 -0800354 if (argc > 5) {
355 i = addParam(i, "SEC", argv[5]);
356 } else {
357 i = addParam(i, "SEC", "open");
358 }
359 if (argc > 6) {
Irfan Sheriff78dcb762011-07-22 15:20:21 -0700360 generatePsk(ssid, argv[6], psk_str);
Dmitry Shmidt321f95a2010-03-09 15:46:34 -0800361 i = addParam(i, "KEY", psk_str);
Dmitry Shmidt7977d672010-03-09 10:57:59 -0800362 } else {
363 i = addParam(i, "KEY", "12345678");
364 }
365 if (argc > 7) {
366 i = addParam(i, "CHANNEL", argv[7]);
367 } else {
368 i = addParam(i, "CHANNEL", "6");
369 }
370 if (argc > 8) {
371 i = addParam(i, "PREAMBLE", argv[8]);
372 } else {
373 i = addParam(i, "PREAMBLE", "0");
374 }
375 if (argc > 9) {
376 i = addParam(i, "MAX_SCB", argv[9]);
377 } else {
378 i = addParam(i, "MAX_SCB", "8");
379 }
380 if ((i < 0) || ((unsigned)(i + 4) >= sizeof(mBuf))) {
Steve Block5ea0c052012-01-06 19:18:11 +0000381 ALOGE("Softap set - command is too big");
Dmitry Shmidt7977d672010-03-09 10:57:59 -0800382 return i;
383 }
384 sprintf(&mBuf[i], "END");
Dmitry Shmidt84c65a62010-03-03 13:14:30 -0800385
Dmitry Shmidt84c65a62010-03-03 13:14:30 -0800386 /* system("iwpriv eth0 WL_AP_CFG ASCII_CMD=AP_CFG,SSID=\"AndroidAP\",SEC=\"open\",KEY=12345,CHANNEL=1,PREAMBLE=0,MAX_SCB=8,END"); */
Dmitry Shmidt6665fb22010-09-22 17:31:07 -0700387 ret = setCommand(iface, "AP_SET_CFG");
Dmitry Shmidt84c65a62010-03-03 13:14:30 -0800388 if (ret) {
Steve Block5ea0c052012-01-06 19:18:11 +0000389 ALOGE("Softap set - failed: %d", ret);
Dmitry Shmidt84c65a62010-03-03 13:14:30 -0800390 }
391 else {
Steve Block7b984e32011-12-20 16:22:42 +0000392 ALOGD("Softap set - Ok");
Dmitry Shmidt3df450a2010-03-18 13:06:47 -0700393 usleep(AP_SET_CFG_DELAY);
Dmitry Shmidt84c65a62010-03-03 13:14:30 -0800394 }
Irfan Sheriff78dcb762011-07-22 15:20:21 -0700395#endif
Dmitry Shmidt84c65a62010-03-03 13:14:30 -0800396 return ret;
Dmitry Shmidt5af38c32010-02-10 11:10:39 -0800397}
Dmitry Shmidt31fd6c52010-03-12 10:01:58 -0800398
Irfan Sheriff78dcb762011-07-22 15:20:21 -0700399void SoftapController::generatePsk(char *ssid, char *passphrase, char *psk_str) {
400 unsigned char psk[SHA256_DIGEST_LENGTH];
401 int j;
402 // Use the PKCS#5 PBKDF2 with 4096 iterations
403 PKCS5_PBKDF2_HMAC_SHA1(passphrase, strlen(passphrase),
404 reinterpret_cast<const unsigned char *>(ssid), strlen(ssid),
405 4096, SHA256_DIGEST_LENGTH, psk);
406 for (j=0; j < SHA256_DIGEST_LENGTH; j++) {
407 sprintf(&psk_str[j<<1], "%02x", psk[j]);
408 }
409 psk_str[j<<1] = '\0';
410}
411
412
Dmitry Shmidt31fd6c52010-03-12 10:01:58 -0800413/*
414 * Arguments:
415 * argv[2] - interface name
416 * argv[3] - AP or STA
417 */
418int SoftapController::fwReloadSoftap(int argc, char *argv[])
419{
Dmitry Shmidt6665fb22010-09-22 17:31:07 -0700420 int ret, i = 0;
Dmitry Shmidt31fd6c52010-03-12 10:01:58 -0800421 char *iface;
Dmitry Shmidtfe15b632011-07-19 13:55:25 -0700422 char *fwpath;
Dmitry Shmidt31fd6c52010-03-12 10:01:58 -0800423
424 if (mSock < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000425 ALOGE("Softap fwrealod - failed to open socket");
Dmitry Shmidt31fd6c52010-03-12 10:01:58 -0800426 return -1;
427 }
428 if (argc < 4) {
Steve Block5ea0c052012-01-06 19:18:11 +0000429 ALOGE("Softap fwreload - missing arguments");
Dmitry Shmidt31fd6c52010-03-12 10:01:58 -0800430 return -1;
431 }
432
433 iface = argv[2];
Dmitry Shmidt31fd6c52010-03-12 10:01:58 -0800434
435 if (strcmp(argv[3], "AP") == 0) {
Dmitry Shmidtfe15b632011-07-19 13:55:25 -0700436 fwpath = (char *)wifi_get_fw_path(WIFI_GET_FW_PATH_AP);
Dmitry Shmidt54cf7fd2011-07-24 16:46:13 -0700437 } else if (strcmp(argv[3], "P2P") == 0) {
438 fwpath = (char *)wifi_get_fw_path(WIFI_GET_FW_PATH_P2P);
Dmitry Shmidt31fd6c52010-03-12 10:01:58 -0800439 } else {
Dmitry Shmidtfe15b632011-07-19 13:55:25 -0700440 fwpath = (char *)wifi_get_fw_path(WIFI_GET_FW_PATH_STA);
Dmitry Shmidt31fd6c52010-03-12 10:01:58 -0800441 }
Dmitry Shmidtfe15b632011-07-19 13:55:25 -0700442 if (!fwpath)
443 return -1;
Dmitry Shmidt389f8d12011-07-21 15:16:04 -0700444#ifdef HAVE_HOSTAPD
445 ret = wifi_change_fw_path((const char *)fwpath);
446#else
Dmitry Shmidtfe15b632011-07-19 13:55:25 -0700447 sprintf(mBuf, "FW_PATH=%s", fwpath);
Dmitry Shmidt6665fb22010-09-22 17:31:07 -0700448 ret = setCommand(iface, "WL_FW_RELOAD");
Dmitry Shmidt389f8d12011-07-21 15:16:04 -0700449#endif
Dmitry Shmidt31fd6c52010-03-12 10:01:58 -0800450 if (ret) {
Steve Block5ea0c052012-01-06 19:18:11 +0000451 ALOGE("Softap fwReload - failed: %d", ret);
Dmitry Shmidt31fd6c52010-03-12 10:01:58 -0800452 }
453 else {
Steve Block7b984e32011-12-20 16:22:42 +0000454 ALOGD("Softap fwReload - Ok");
Dmitry Shmidt31fd6c52010-03-12 10:01:58 -0800455 }
456 return ret;
457}
Dmitry Shmidt666fe252011-03-08 11:01:58 -0800458
459int SoftapController::clientsSoftap(char **retbuf)
460{
461 int ret;
462
463 if (mSock < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000464 ALOGE("Softap clients - failed to open socket");
Dmitry Shmidt666fe252011-03-08 11:01:58 -0800465 return -1;
466 }
467 *mBuf = 0;
468 ret = setCommand(mIface, "AP_GET_STA_LIST", SOFTAP_MAX_BUFFER_SIZE);
469 if (ret) {
Steve Block5ea0c052012-01-06 19:18:11 +0000470 ALOGE("Softap clients - failed: %d", ret);
Dmitry Shmidt666fe252011-03-08 11:01:58 -0800471 } else {
472 asprintf(retbuf, "Softap clients:%s", mBuf);
Steve Block7b984e32011-12-20 16:22:42 +0000473 ALOGD("Softap clients:%s", mBuf);
Dmitry Shmidt666fe252011-03-08 11:01:58 -0800474 }
475 return ret;
476}