blob: 3ab6d72e81e35b57227908b9e5f9f31db005c30d [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>
20
21#include <sys/socket.h>
22#include <sys/stat.h>
23#include <sys/types.h>
24#include <sys/wait.h>
25
26#include <netinet/in.h>
27#include <arpa/inet.h>
28
Dmitry Shmidt84c65a62010-03-03 13:14:30 -080029#include <linux/wireless.h>
30
Kenny Roota2d7e3e2010-03-15 14:26:36 -070031#include <openssl/evp.h>
32#include <openssl/sha.h>
33
Dmitry Shmidt5af38c32010-02-10 11:10:39 -080034#define LOG_TAG "SoftapController"
35#include <cutils/log.h>
36
37#include "SoftapController.h"
38
39SoftapController::SoftapController() {
40 mPid = 0;
Dmitry Shmidt84c65a62010-03-03 13:14:30 -080041 mSock = socket(AF_INET, SOCK_DGRAM, 0);
42 if (mSock < 0)
43 LOGE("Failed to open socket");
44 memset(mIface, 0, sizeof(mIface));
Dmitry Shmidt5af38c32010-02-10 11:10:39 -080045}
46
47SoftapController::~SoftapController() {
Dmitry Shmidt84c65a62010-03-03 13:14:30 -080048 if (mSock >= 0)
49 close(mSock);
50}
51
jmzhufbd11c42010-10-11 16:47:05 -070052int SoftapController::setCommand(char *iface, const char *fname) {
Dmitry Shmidt6665fb22010-09-22 17:31:07 -070053 char tBuf[SOFTAP_MAX_BUFFER_SIZE];
Dmitry Shmidt84c65a62010-03-03 13:14:30 -080054 struct iwreq wrq;
55 struct iw_priv_args *priv_ptr;
jmzhufbd11c42010-10-11 16:47:05 -070056 int i, j, ret;
57 int cmd = 0, sub_cmd = 0;
Dmitry Shmidt84c65a62010-03-03 13:14:30 -080058
59 strncpy(wrq.ifr_name, iface, sizeof(wrq.ifr_name));
Dmitry Shmidt6665fb22010-09-22 17:31:07 -070060 wrq.u.data.pointer = tBuf;
61 wrq.u.data.length = sizeof(tBuf) / sizeof(struct iw_priv_args);
Dmitry Shmidt84c65a62010-03-03 13:14:30 -080062 wrq.u.data.flags = 0;
63 if ((ret = ioctl(mSock, SIOCGIWPRIV, &wrq)) < 0) {
64 LOGE("SIOCGIPRIV failed: %d", ret);
65 return ret;
66 }
jmzhufbd11c42010-10-11 16:47:05 -070067
Dmitry Shmidt84c65a62010-03-03 13:14:30 -080068 priv_ptr = (struct iw_priv_args *)wrq.u.data.pointer;
jmzhufbd11c42010-10-11 16:47:05 -070069 for(i=0; i < wrq.u.data.length;i++) {
70 if (strcmp(priv_ptr[i].name, fname) == 0) {
71 cmd = priv_ptr[i].cmd;
72 break;
73 }
Dmitry Shmidt84c65a62010-03-03 13:14:30 -080074 }
Dmitry Shmidt5af38c32010-02-10 11:10:39 -080075
jmzhufbd11c42010-10-11 16:47:05 -070076 if (i == wrq.u.data.length) {
77 LOGE("iface:%s, fname: %s - function not supported", iface, fname);
Dmitry Shmidt6665fb22010-09-22 17:31:07 -070078 return -1;
79 }
80
jmzhufbd11c42010-10-11 16:47:05 -070081 if (cmd < SIOCDEVPRIVATE) {
82 for(j=0; j < i; j++) {
83 if ((priv_ptr[j].set_args == priv_ptr[i].set_args) &&
84 (priv_ptr[j].get_args == priv_ptr[i].get_args) &&
85 (priv_ptr[j].name[0] == '\0'))
86 break;
87 }
88 if (j == i) {
89 LOGE("iface:%s, fname: %s - invalid private ioctl", iface, fname);
90 return -1;
91 }
92 sub_cmd = cmd;
93 cmd = priv_ptr[j].cmd;
94 }
95
Dmitry Shmidt6665fb22010-09-22 17:31:07 -070096 strncpy(wrq.ifr_name, iface, sizeof(wrq.ifr_name));
97 if (*mBuf != 0)
98 wrq.u.data.length = strlen(mBuf) + 1;
99 else
100 wrq.u.data.length = 0;
101 wrq.u.data.pointer = mBuf;
jmzhufbd11c42010-10-11 16:47:05 -0700102 wrq.u.data.flags = sub_cmd;
103 ret = ioctl(mSock, cmd, &wrq);
Dmitry Shmidt6665fb22010-09-22 17:31:07 -0700104 return ret;
105}
106
107int SoftapController::startDriver(char *iface) {
108 int ret;
109
Dmitry Shmidtc3539e22010-03-12 14:16:46 -0800110 if (mSock < 0) {
Dmitry Shmidta1659132010-04-23 12:43:32 -0700111 LOGE("Softap driver start - failed to open socket");
Dmitry Shmidtc3539e22010-03-12 14:16:46 -0800112 return -1;
113 }
114 if (!iface || (iface[0] == '\0')) {
Dmitry Shmidta1659132010-04-23 12:43:32 -0700115 LOGD("Softap driver start - wrong interface");
Dmitry Shmidtc3539e22010-03-12 14:16:46 -0800116 iface = mIface;
117 }
Dmitry Shmidt6665fb22010-09-22 17:31:07 -0700118
119 *mBuf = 0;
120 ret = setCommand(iface, "START");
Dmitry Shmidta1659132010-04-23 12:43:32 -0700121 usleep(AP_DRIVER_START_DELAY);
122 LOGD("Softap driver start: %d", ret);
Dmitry Shmidtc3539e22010-03-12 14:16:46 -0800123 return ret;
124}
125
126int SoftapController::stopDriver(char *iface) {
Dmitry Shmidt6665fb22010-09-22 17:31:07 -0700127 int ret;
Dmitry Shmidtc3539e22010-03-12 14:16:46 -0800128
129 if (mSock < 0) {
Dmitry Shmidta1659132010-04-23 12:43:32 -0700130 LOGE("Softap driver stop - failed to open socket");
Dmitry Shmidtc3539e22010-03-12 14:16:46 -0800131 return -1;
132 }
133 if (!iface || (iface[0] == '\0')) {
Dmitry Shmidta1659132010-04-23 12:43:32 -0700134 LOGD("Softap driver stop - wrong interface");
Dmitry Shmidtc3539e22010-03-12 14:16:46 -0800135 iface = mIface;
136 }
Dmitry Shmidt6665fb22010-09-22 17:31:07 -0700137 *mBuf = 0;
138 ret = setCommand(iface, "STOP");
Dmitry Shmidta1659132010-04-23 12:43:32 -0700139 LOGD("Softap driver stop: %d", ret);
Dmitry Shmidtc3539e22010-03-12 14:16:46 -0800140 return ret;
141}
142
Dmitry Shmidt5af38c32010-02-10 11:10:39 -0800143int SoftapController::startSoftap() {
144 pid_t pid = 1;
Dmitry Shmidt6665fb22010-09-22 17:31:07 -0700145 int ret = 0;
Dmitry Shmidt5af38c32010-02-10 11:10:39 -0800146
Dmitry Shmidt5af38c32010-02-10 11:10:39 -0800147 if (mPid) {
148 LOGE("Softap already started");
Dmitry Shmidta1659132010-04-23 12:43:32 -0700149 return 0;
Dmitry Shmidt5af38c32010-02-10 11:10:39 -0800150 }
Dmitry Shmidt84c65a62010-03-03 13:14:30 -0800151 if (mSock < 0) {
Dmitry Shmidtc3539e22010-03-12 14:16:46 -0800152 LOGE("Softap startap - failed to open socket");
Dmitry Shmidt84c65a62010-03-03 13:14:30 -0800153 return -1;
154 }
Dmitry Shmidt5af38c32010-02-10 11:10:39 -0800155#if 0
156 if ((pid = fork()) < 0) {
157 LOGE("fork failed (%s)", strerror(errno));
158 return -1;
159 }
160#endif
Dmitry Shmidt84c65a62010-03-03 13:14:30 -0800161 /* system("iwpriv wl0.1 AP_BSS_START"); */
Dmitry Shmidt5af38c32010-02-10 11:10:39 -0800162 if (!pid) {
Dmitry Shmidt84c65a62010-03-03 13:14:30 -0800163 /* start hostapd */
164 return ret;
Dmitry Shmidt5af38c32010-02-10 11:10:39 -0800165 } else {
Dmitry Shmidt6665fb22010-09-22 17:31:07 -0700166 *mBuf = 0;
167 ret = setCommand(mIface, "AP_BSS_START");
Dmitry Shmidt84c65a62010-03-03 13:14:30 -0800168 if (ret) {
Dmitry Shmidtc3539e22010-03-12 14:16:46 -0800169 LOGE("Softap startap - failed: %d", ret);
Dmitry Shmidt84c65a62010-03-03 13:14:30 -0800170 }
171 else {
172 mPid = pid;
Dmitry Shmidtc3539e22010-03-12 14:16:46 -0800173 LOGD("Softap startap - Ok");
Dmitry Shmidt3df450a2010-03-18 13:06:47 -0700174 usleep(AP_BSS_START_DELAY);
Dmitry Shmidt84c65a62010-03-03 13:14:30 -0800175 }
Dmitry Shmidt5af38c32010-02-10 11:10:39 -0800176 }
Dmitry Shmidt84c65a62010-03-03 13:14:30 -0800177 return ret;
Dmitry Shmidt5af38c32010-02-10 11:10:39 -0800178
179}
180
181int SoftapController::stopSoftap() {
Dmitry Shmidt6665fb22010-09-22 17:31:07 -0700182 int ret;
Dmitry Shmidt84c65a62010-03-03 13:14:30 -0800183
Dmitry Shmidt5af38c32010-02-10 11:10:39 -0800184 if (mPid == 0) {
185 LOGE("Softap already stopped");
186 return 0;
187 }
Dmitry Shmidt84c65a62010-03-03 13:14:30 -0800188 if (mSock < 0) {
Dmitry Shmidtc3539e22010-03-12 14:16:46 -0800189 LOGE("Softap stopap - failed to open socket");
Dmitry Shmidt84c65a62010-03-03 13:14:30 -0800190 return -1;
191 }
Dmitry Shmidt6665fb22010-09-22 17:31:07 -0700192 *mBuf = 0;
193 ret = setCommand(mIface, "AP_BSS_STOP");
Dmitry Shmidt5af38c32010-02-10 11:10:39 -0800194#if 0
195 LOGD("Stopping Softap service");
196 kill(mPid, SIGTERM);
197 waitpid(mPid, NULL, 0);
198#endif
199 mPid = 0;
Dmitry Shmidt84c65a62010-03-03 13:14:30 -0800200 LOGD("Softap service stopped: %d", ret);
Dmitry Shmidt3df450a2010-03-18 13:06:47 -0700201 usleep(AP_BSS_STOP_DELAY);
Dmitry Shmidt84c65a62010-03-03 13:14:30 -0800202 return ret;
Dmitry Shmidt5af38c32010-02-10 11:10:39 -0800203}
204
205bool SoftapController::isSoftapStarted() {
206 return (mPid != 0 ? true : false);
207}
208
Dmitry Shmidt7977d672010-03-09 10:57:59 -0800209int SoftapController::addParam(int pos, const char *cmd, const char *arg)
210{
211 if (pos < 0)
212 return pos;
213 if ((unsigned)(pos + strlen(cmd) + strlen(arg) + 1) >= sizeof(mBuf)) {
214 LOGE("Command line is too big");
215 return -1;
216 }
217 pos += sprintf(&mBuf[pos], "%s=%s,", cmd, arg);
218 return pos;
219}
220
Dmitry Shmidt84c65a62010-03-03 13:14:30 -0800221/*
222 * Arguments:
223 * argv[2] - wlan interface
224 * argv[3] - softap interface
Dmitry Shmidt7977d672010-03-09 10:57:59 -0800225 * argv[4] - SSID
226 * argv[5] - Security
227 * argv[6] - Key
228 * argv[7] - Channel
229 * argv[8] - Preamble
230 * argv[9] - Max SCB
Dmitry Shmidt84c65a62010-03-03 13:14:30 -0800231 */
Dmitry Shmidt5af38c32010-02-10 11:10:39 -0800232int SoftapController::setSoftap(int argc, char *argv[]) {
Kenny Roota2d7e3e2010-03-15 14:26:36 -0700233 unsigned char psk[SHA256_DIGEST_LENGTH];
234 char psk_str[2*SHA256_DIGEST_LENGTH+1];
Dmitry Shmidt6665fb22010-09-22 17:31:07 -0700235 int ret, i = 0;
236 char *ssid, *iface;
Dmitry Shmidt84c65a62010-03-03 13:14:30 -0800237
Dmitry Shmidt84c65a62010-03-03 13:14:30 -0800238 if (mSock < 0) {
Dmitry Shmidt7977d672010-03-09 10:57:59 -0800239 LOGE("Softap set - failed to open socket");
Dmitry Shmidt84c65a62010-03-03 13:14:30 -0800240 return -1;
241 }
242 if (argc < 4) {
Dmitry Shmidt7977d672010-03-09 10:57:59 -0800243 LOGE("Softap set - missing arguments");
Dmitry Shmidt84c65a62010-03-03 13:14:30 -0800244 return -1;
245 }
246
Dmitry Shmidt84c65a62010-03-03 13:14:30 -0800247 strncpy(mIface, argv[3], sizeof(mIface));
Dmitry Shmidt6665fb22010-09-22 17:31:07 -0700248 iface = argv[2];
Dmitry Shmidt7977d672010-03-09 10:57:59 -0800249
250 /* Create command line */
251 i = addParam(i, "ASCII_CMD", "AP_CFG");
252 if (argc > 4) {
Dmitry Shmidt321f95a2010-03-09 15:46:34 -0800253 ssid = argv[4];
Dmitry Shmidt7977d672010-03-09 10:57:59 -0800254 } else {
Dmitry Shmidt321f95a2010-03-09 15:46:34 -0800255 ssid = (char *)"AndroidAP";
Dmitry Shmidt84c65a62010-03-03 13:14:30 -0800256 }
Dmitry Shmidt321f95a2010-03-09 15:46:34 -0800257 i = addParam(i, "SSID", ssid);
Dmitry Shmidt7977d672010-03-09 10:57:59 -0800258 if (argc > 5) {
259 i = addParam(i, "SEC", argv[5]);
260 } else {
261 i = addParam(i, "SEC", "open");
262 }
263 if (argc > 6) {
Dmitry Shmidt321f95a2010-03-09 15:46:34 -0800264 int j;
Kenny Roota2d7e3e2010-03-15 14:26:36 -0700265 // Use the PKCS#5 PBKDF2 with 4096 iterations
266 PKCS5_PBKDF2_HMAC_SHA1(argv[6], strlen(argv[6]),
267 reinterpret_cast<const unsigned char *>(ssid), strlen(ssid),
268 4096, SHA256_DIGEST_LENGTH, psk);
269 for (j=0; j < SHA256_DIGEST_LENGTH; j++) {
Dmitry Shmidt321f95a2010-03-09 15:46:34 -0800270 sprintf(&psk_str[j<<1], "%02x", psk[j]);
271 }
272 psk_str[j<<1] = '\0';
273 i = addParam(i, "KEY", psk_str);
Dmitry Shmidt7977d672010-03-09 10:57:59 -0800274 } else {
275 i = addParam(i, "KEY", "12345678");
276 }
277 if (argc > 7) {
278 i = addParam(i, "CHANNEL", argv[7]);
279 } else {
280 i = addParam(i, "CHANNEL", "6");
281 }
282 if (argc > 8) {
283 i = addParam(i, "PREAMBLE", argv[8]);
284 } else {
285 i = addParam(i, "PREAMBLE", "0");
286 }
287 if (argc > 9) {
288 i = addParam(i, "MAX_SCB", argv[9]);
289 } else {
290 i = addParam(i, "MAX_SCB", "8");
291 }
292 if ((i < 0) || ((unsigned)(i + 4) >= sizeof(mBuf))) {
293 LOGE("Softap set - command is too big");
294 return i;
295 }
296 sprintf(&mBuf[i], "END");
Dmitry Shmidt84c65a62010-03-03 13:14:30 -0800297
Dmitry Shmidt84c65a62010-03-03 13:14:30 -0800298 /* 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 -0700299 ret = setCommand(iface, "AP_SET_CFG");
Dmitry Shmidt84c65a62010-03-03 13:14:30 -0800300 if (ret) {
301 LOGE("Softap set - failed: %d", ret);
302 }
303 else {
304 LOGD("Softap set - Ok");
Dmitry Shmidt3df450a2010-03-18 13:06:47 -0700305 usleep(AP_SET_CFG_DELAY);
Dmitry Shmidt84c65a62010-03-03 13:14:30 -0800306 }
307 return ret;
Dmitry Shmidt5af38c32010-02-10 11:10:39 -0800308}
Dmitry Shmidt31fd6c52010-03-12 10:01:58 -0800309
310/*
311 * Arguments:
312 * argv[2] - interface name
313 * argv[3] - AP or STA
314 */
315int SoftapController::fwReloadSoftap(int argc, char *argv[])
316{
Dmitry Shmidt6665fb22010-09-22 17:31:07 -0700317 int ret, i = 0;
Dmitry Shmidt31fd6c52010-03-12 10:01:58 -0800318 char *iface;
319
320 if (mSock < 0) {
321 LOGE("Softap fwrealod - failed to open socket");
322 return -1;
323 }
324 if (argc < 4) {
325 LOGE("Softap fwreload - missing arguments");
326 return -1;
327 }
328
329 iface = argv[2];
Dmitry Shmidt31fd6c52010-03-12 10:01:58 -0800330
331 if (strcmp(argv[3], "AP") == 0) {
332#ifdef WIFI_DRIVER_FW_AP_PATH
333 sprintf(mBuf, "FW_PATH=%s", WIFI_DRIVER_FW_AP_PATH);
334#endif
335 } else {
336#ifdef WIFI_DRIVER_FW_STA_PATH
337 sprintf(mBuf, "FW_PATH=%s", WIFI_DRIVER_FW_STA_PATH);
338#endif
339 }
Dmitry Shmidt6665fb22010-09-22 17:31:07 -0700340 ret = setCommand(iface, "WL_FW_RELOAD");
Dmitry Shmidt31fd6c52010-03-12 10:01:58 -0800341 if (ret) {
342 LOGE("Softap fwReload - failed: %d", ret);
343 }
344 else {
345 LOGD("Softap fwReload - Ok");
346 }
347 return ret;
348}