blob: 352c353a0243cefb3e3e490b9565dcd03ff2ebfa [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
Dmitry Shmidt5af38c32010-02-10 11:10:39 -080031#define LOG_TAG "SoftapController"
32#include <cutils/log.h>
33
34#include "SoftapController.h"
Dmitry Shmidt321f95a2010-03-09 15:46:34 -080035#include "sha1.h"
Dmitry Shmidt5af38c32010-02-10 11:10:39 -080036
37SoftapController::SoftapController() {
38 mPid = 0;
Dmitry Shmidt84c65a62010-03-03 13:14:30 -080039 mSock = socket(AF_INET, SOCK_DGRAM, 0);
40 if (mSock < 0)
41 LOGE("Failed to open socket");
42 memset(mIface, 0, sizeof(mIface));
Dmitry Shmidt5af38c32010-02-10 11:10:39 -080043}
44
45SoftapController::~SoftapController() {
Dmitry Shmidt84c65a62010-03-03 13:14:30 -080046 if (mSock >= 0)
47 close(mSock);
48}
49
50int SoftapController::getPrivFuncNum(char *iface, const char *fname) {
51 struct iwreq wrq;
52 struct iw_priv_args *priv_ptr;
53 int i, ret;
54
55 strncpy(wrq.ifr_name, iface, sizeof(wrq.ifr_name));
56 wrq.u.data.pointer = mBuf;
57 wrq.u.data.length = sizeof(mBuf) / sizeof(struct iw_priv_args);
58 wrq.u.data.flags = 0;
59 if ((ret = ioctl(mSock, SIOCGIWPRIV, &wrq)) < 0) {
60 LOGE("SIOCGIPRIV failed: %d", ret);
61 return ret;
62 }
63 priv_ptr = (struct iw_priv_args *)wrq.u.data.pointer;
Dmitry Shmidt84c65a62010-03-03 13:14:30 -080064 for(i=0;(i < wrq.u.data.length);i++) {
65 if (strcmp(priv_ptr[i].name, fname) == 0)
66 return priv_ptr[i].cmd;
67 }
68 return -1;
Dmitry Shmidt5af38c32010-02-10 11:10:39 -080069}
70
71int SoftapController::startSoftap() {
Dmitry Shmidt84c65a62010-03-03 13:14:30 -080072 struct iwreq wrq;
Dmitry Shmidt5af38c32010-02-10 11:10:39 -080073 pid_t pid = 1;
Dmitry Shmidt84c65a62010-03-03 13:14:30 -080074 int fnum, ret = 0;
Dmitry Shmidt5af38c32010-02-10 11:10:39 -080075
76 LOGD("Softap start");
77 if (mPid) {
78 LOGE("Softap already started");
79 errno = EBUSY;
80 return -1;
81 }
Dmitry Shmidt84c65a62010-03-03 13:14:30 -080082 if (mSock < 0) {
83 LOGE("Failed to open socket");
84 return -1;
85 }
Dmitry Shmidt5af38c32010-02-10 11:10:39 -080086#if 0
87 if ((pid = fork()) < 0) {
88 LOGE("fork failed (%s)", strerror(errno));
89 return -1;
90 }
91#endif
Dmitry Shmidt84c65a62010-03-03 13:14:30 -080092 /* system("iwpriv wl0.1 AP_BSS_START"); */
Dmitry Shmidt5af38c32010-02-10 11:10:39 -080093 if (!pid) {
Dmitry Shmidt84c65a62010-03-03 13:14:30 -080094 /* start hostapd */
95 return ret;
Dmitry Shmidt5af38c32010-02-10 11:10:39 -080096 } else {
Dmitry Shmidt84c65a62010-03-03 13:14:30 -080097 fnum = getPrivFuncNum(mIface, "AP_BSS_START");
98 if (fnum < 0) {
99 LOGE("Softap start - function not supported");
100 return -1;
101 }
102 strncpy(wrq.ifr_name, mIface, sizeof(wrq.ifr_name));
103 wrq.u.data.length = 0;
104 wrq.u.data.pointer = mBuf;
105 wrq.u.data.flags = 0;
106 ret = ioctl(mSock, fnum, &wrq);
107 if (ret) {
108 LOGE("Softap start - failed: %d", ret);
109 }
110 else {
111 mPid = pid;
112 LOGD("Softap start - Ok");
113 }
Dmitry Shmidt5af38c32010-02-10 11:10:39 -0800114 }
Dmitry Shmidt84c65a62010-03-03 13:14:30 -0800115 return ret;
Dmitry Shmidt5af38c32010-02-10 11:10:39 -0800116
117}
118
119int SoftapController::stopSoftap() {
Dmitry Shmidt84c65a62010-03-03 13:14:30 -0800120 struct iwreq wrq;
121 int fnum, ret;
122
Dmitry Shmidt5af38c32010-02-10 11:10:39 -0800123 if (mPid == 0) {
124 LOGE("Softap already stopped");
125 return 0;
126 }
Dmitry Shmidt84c65a62010-03-03 13:14:30 -0800127 if (mSock < 0) {
Dmitry Shmidt7977d672010-03-09 10:57:59 -0800128 LOGE("Softap stop - failed to open socket");
Dmitry Shmidt84c65a62010-03-03 13:14:30 -0800129 return -1;
130 }
131 fnum = getPrivFuncNum(mIface, "WL_AP_STOP");
132 if (fnum < 0) {
133 LOGE("Softap stop - function not supported");
134 return -1;
135 }
136 strncpy(wrq.ifr_name, mIface, sizeof(wrq.ifr_name));
137 wrq.u.data.length = 0;
138 wrq.u.data.pointer = mBuf;
139 wrq.u.data.flags = 0;
140 ret = ioctl(mSock, fnum, &wrq);
Dmitry Shmidt5af38c32010-02-10 11:10:39 -0800141#if 0
142 LOGD("Stopping Softap service");
143 kill(mPid, SIGTERM);
144 waitpid(mPid, NULL, 0);
145#endif
146 mPid = 0;
Dmitry Shmidt84c65a62010-03-03 13:14:30 -0800147 LOGD("Softap service stopped: %d", ret);
148 return ret;
Dmitry Shmidt5af38c32010-02-10 11:10:39 -0800149}
150
151bool SoftapController::isSoftapStarted() {
152 return (mPid != 0 ? true : false);
153}
154
Dmitry Shmidt7977d672010-03-09 10:57:59 -0800155int SoftapController::addParam(int pos, const char *cmd, const char *arg)
156{
157 if (pos < 0)
158 return pos;
159 if ((unsigned)(pos + strlen(cmd) + strlen(arg) + 1) >= sizeof(mBuf)) {
160 LOGE("Command line is too big");
161 return -1;
162 }
163 pos += sprintf(&mBuf[pos], "%s=%s,", cmd, arg);
164 return pos;
165}
166
Dmitry Shmidt84c65a62010-03-03 13:14:30 -0800167/*
168 * Arguments:
169 * argv[2] - wlan interface
170 * argv[3] - softap interface
Dmitry Shmidt7977d672010-03-09 10:57:59 -0800171 * argv[4] - SSID
172 * argv[5] - Security
173 * argv[6] - Key
174 * argv[7] - Channel
175 * argv[8] - Preamble
176 * argv[9] - Max SCB
Dmitry Shmidt84c65a62010-03-03 13:14:30 -0800177 */
Dmitry Shmidt5af38c32010-02-10 11:10:39 -0800178int SoftapController::setSoftap(int argc, char *argv[]) {
Dmitry Shmidt321f95a2010-03-09 15:46:34 -0800179 unsigned char psk[MAX_SHA1_LEN];
180 char psk_str[2*MAX_SHA1_LEN+1];
Dmitry Shmidt84c65a62010-03-03 13:14:30 -0800181 struct iwreq wrq;
Dmitry Shmidt7977d672010-03-09 10:57:59 -0800182 int fnum, ret, i = 0;
Dmitry Shmidt321f95a2010-03-09 15:46:34 -0800183 char *ssid;
Dmitry Shmidt84c65a62010-03-03 13:14:30 -0800184
Dmitry Shmidt84c65a62010-03-03 13:14:30 -0800185 if (mSock < 0) {
Dmitry Shmidt7977d672010-03-09 10:57:59 -0800186 LOGE("Softap set - failed to open socket");
Dmitry Shmidt84c65a62010-03-03 13:14:30 -0800187 return -1;
188 }
189 if (argc < 4) {
Dmitry Shmidt7977d672010-03-09 10:57:59 -0800190 LOGE("Softap set - missing arguments");
Dmitry Shmidt84c65a62010-03-03 13:14:30 -0800191 return -1;
192 }
193
194 fnum = getPrivFuncNum(argv[2], "WL_AP_CFG");
195 if (fnum < 0) {
196 LOGE("Softap set - function not supported");
197 return -1;
198 }
199
200 strncpy(mIface, argv[3], sizeof(mIface));
201 strncpy(wrq.ifr_name, argv[2], sizeof(wrq.ifr_name));
Dmitry Shmidt7977d672010-03-09 10:57:59 -0800202
203 /* Create command line */
204 i = addParam(i, "ASCII_CMD", "AP_CFG");
205 if (argc > 4) {
Dmitry Shmidt321f95a2010-03-09 15:46:34 -0800206 ssid = argv[4];
Dmitry Shmidt7977d672010-03-09 10:57:59 -0800207 } else {
Dmitry Shmidt321f95a2010-03-09 15:46:34 -0800208 ssid = (char *)"AndroidAP";
Dmitry Shmidt84c65a62010-03-03 13:14:30 -0800209 }
Dmitry Shmidt321f95a2010-03-09 15:46:34 -0800210 i = addParam(i, "SSID", ssid);
Dmitry Shmidt7977d672010-03-09 10:57:59 -0800211 if (argc > 5) {
212 i = addParam(i, "SEC", argv[5]);
213 } else {
214 i = addParam(i, "SEC", "open");
215 }
216 if (argc > 6) {
Dmitry Shmidt321f95a2010-03-09 15:46:34 -0800217 int j;
218 pbkdf2_sha1(argv[6], ssid, strlen(ssid), 4096, psk, MAX_SHA1_LEN);
219 for(j=0;(j < MAX_SHA1_LEN);j++) {
220 sprintf(&psk_str[j<<1], "%02x", psk[j]);
221 }
222 psk_str[j<<1] = '\0';
223 i = addParam(i, "KEY", psk_str);
Dmitry Shmidt7977d672010-03-09 10:57:59 -0800224 } else {
225 i = addParam(i, "KEY", "12345678");
226 }
227 if (argc > 7) {
228 i = addParam(i, "CHANNEL", argv[7]);
229 } else {
230 i = addParam(i, "CHANNEL", "6");
231 }
232 if (argc > 8) {
233 i = addParam(i, "PREAMBLE", argv[8]);
234 } else {
235 i = addParam(i, "PREAMBLE", "0");
236 }
237 if (argc > 9) {
238 i = addParam(i, "MAX_SCB", argv[9]);
239 } else {
240 i = addParam(i, "MAX_SCB", "8");
241 }
242 if ((i < 0) || ((unsigned)(i + 4) >= sizeof(mBuf))) {
243 LOGE("Softap set - command is too big");
244 return i;
245 }
246 sprintf(&mBuf[i], "END");
Dmitry Shmidt84c65a62010-03-03 13:14:30 -0800247
248 wrq.u.data.length = strlen(mBuf) + 1;
249 wrq.u.data.pointer = mBuf;
250 wrq.u.data.flags = 0;
251 /* system("iwpriv eth0 WL_AP_CFG ASCII_CMD=AP_CFG,SSID=\"AndroidAP\",SEC=\"open\",KEY=12345,CHANNEL=1,PREAMBLE=0,MAX_SCB=8,END"); */
252 ret = ioctl(mSock, fnum, &wrq);
253 if (ret) {
254 LOGE("Softap set - failed: %d", ret);
255 }
256 else {
257 LOGD("Softap set - Ok");
258 }
259 return ret;
Dmitry Shmidt5af38c32010-02-10 11:10:39 -0800260}