SoftapController.cpp 9.04 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
20
#include <string.h>
21 22 23

#include <sys/socket.h>
#include <sys/stat.h>
24
#include <sys/ioctl.h>
25 26 27 28 29 30
#include <sys/types.h>
#include <sys/wait.h>

#include <netinet/in.h>
#include <arpa/inet.h>

31 32
#include <linux/wireless.h>

33 34 35
#include <openssl/evp.h>
#include <openssl/sha.h>

36 37 38 39 40 41 42
#define LOG_TAG "SoftapController"
#include <cutils/log.h>

#include "SoftapController.h"

SoftapController::SoftapController() {
    mPid = 0;
43 44 45 46
    mSock = socket(AF_INET, SOCK_DGRAM, 0);
    if (mSock < 0)
        LOGE("Failed to open socket");
    memset(mIface, 0, sizeof(mIface));
47 48 49
}

SoftapController::~SoftapController() {
50 51 52 53
    if (mSock >= 0)
        close(mSock);
}

Dmitry Shmidt's avatar
Dmitry Shmidt committed
54
int SoftapController::setCommand(char *iface, const char *fname, unsigned buflen) {
55
    char tBuf[SOFTAP_MAX_BUFFER_SIZE];
56 57
    struct iwreq wrq;
    struct iw_priv_args *priv_ptr;
58 59
    int i, j, ret;
    int cmd = 0, sub_cmd = 0;
60 61

    strncpy(wrq.ifr_name, iface, sizeof(wrq.ifr_name));
62 63
    wrq.u.data.pointer = tBuf;
    wrq.u.data.length = sizeof(tBuf) / sizeof(struct iw_priv_args);
64 65 66 67 68
    wrq.u.data.flags = 0;
    if ((ret = ioctl(mSock, SIOCGIWPRIV, &wrq)) < 0) {
        LOGE("SIOCGIPRIV failed: %d", ret);
        return ret;
    }
69

70
    priv_ptr = (struct iw_priv_args *)wrq.u.data.pointer;
71 72 73 74 75
    for(i=0; i < wrq.u.data.length;i++) {
        if (strcmp(priv_ptr[i].name, fname) == 0) {
            cmd = priv_ptr[i].cmd;
            break;
        }
76
    }
77

78 79
    if (i == wrq.u.data.length) {
        LOGE("iface:%s, fname: %s - function not supported", iface, fname);
80 81 82
        return -1;
    }

83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
    if (cmd < SIOCDEVPRIVATE) {
        for(j=0; j < i; j++) {
            if ((priv_ptr[j].set_args == priv_ptr[i].set_args) &&
                (priv_ptr[j].get_args == priv_ptr[i].get_args) &&
                (priv_ptr[j].name[0] == '\0'))
                break;
        }
        if (j == i) {
            LOGE("iface:%s, fname: %s - invalid private ioctl", iface, fname);
            return -1;
        }
        sub_cmd = cmd;
        cmd = priv_ptr[j].cmd;
    }

98
    strncpy(wrq.ifr_name, iface, sizeof(wrq.ifr_name));
Dmitry Shmidt's avatar
Dmitry Shmidt committed
99
    if ((buflen == 0) && (*mBuf != 0))
100 101
        wrq.u.data.length = strlen(mBuf) + 1;
    else
Dmitry Shmidt's avatar
Dmitry Shmidt committed
102
        wrq.u.data.length = buflen;
103
    wrq.u.data.pointer = mBuf;
104 105
    wrq.u.data.flags = sub_cmd;
    ret = ioctl(mSock, cmd, &wrq);
106 107 108 109 110 111
    return ret;
}

int SoftapController::startDriver(char *iface) {
    int ret;

112
    if (mSock < 0) {
113
        LOGE("Softap driver start - failed to open socket");
114 115 116
        return -1;
    }
    if (!iface || (iface[0] == '\0')) {
117
        LOGD("Softap driver start - wrong interface");
118 119
        iface = mIface;
    }
120 121 122

    *mBuf = 0;
    ret = setCommand(iface, "START");
123 124
    usleep(AP_DRIVER_START_DELAY);
    LOGD("Softap driver start: %d", ret);
125 126 127 128
    return ret;
}

int SoftapController::stopDriver(char *iface) {
129
    int ret;
130 131

    if (mSock < 0) {
132
        LOGE("Softap driver stop - failed to open socket");
133 134 135
        return -1;
    }
    if (!iface || (iface[0] == '\0')) {
136
        LOGD("Softap driver stop - wrong interface");
137 138
        iface = mIface;
    }
139 140
    *mBuf = 0;
    ret = setCommand(iface, "STOP");
141
    LOGD("Softap driver stop: %d", ret);
142 143 144
    return ret;
}

145 146
int SoftapController::startSoftap() {
    pid_t pid = 1;
147
    int ret = 0;
148 149 150

    if (mPid) {
        LOGE("Softap already started");
151
        return 0;
152
    }
153
    if (mSock < 0) {
154
        LOGE("Softap startap - failed to open socket");
155 156
        return -1;
    }
157 158 159 160 161 162
#if 0
   if ((pid = fork()) < 0) {
        LOGE("fork failed (%s)", strerror(errno));
        return -1;
    }
#endif
163
    /* system("iwpriv wl0.1 AP_BSS_START"); */
164
    if (!pid) {
165 166
        /* start hostapd */
        return ret;
167
    } else {
168 169
        *mBuf = 0;
        ret = setCommand(mIface, "AP_BSS_START");
170
        if (ret) {
171
            LOGE("Softap startap - failed: %d", ret);
172 173 174
        }
        else {
           mPid = pid;
175
           LOGD("Softap startap - Ok");
176
           usleep(AP_BSS_START_DELAY);
177
        }
178
    }
179
    return ret;
180 181 182 183

}

int SoftapController::stopSoftap() {
184
    int ret;
185

186 187 188 189
    if (mPid == 0) {
        LOGE("Softap already stopped");
        return 0;
    }
190
    if (mSock < 0) {
191
        LOGE("Softap stopap - failed to open socket");
192 193
        return -1;
    }
194 195
    *mBuf = 0;
    ret = setCommand(mIface, "AP_BSS_STOP");
196 197 198 199 200 201
#if 0
    LOGD("Stopping Softap service");
    kill(mPid, SIGTERM);
    waitpid(mPid, NULL, 0);
#endif
    mPid = 0;
202
    LOGD("Softap service stopped: %d", ret);
203
    usleep(AP_BSS_STOP_DELAY);
204
    return ret;
205 206 207 208 209 210
}

bool SoftapController::isSoftapStarted() {
    return (mPid != 0 ? true : false);
}

211 212 213 214 215 216 217 218 219 220 221 222
int SoftapController::addParam(int pos, const char *cmd, const char *arg)
{
    if (pos < 0)
        return pos;
    if ((unsigned)(pos + strlen(cmd) + strlen(arg) + 1) >= sizeof(mBuf)) {
        LOGE("Command line is too big");
        return -1;
    }
    pos += sprintf(&mBuf[pos], "%s=%s,", cmd, arg);
    return pos;
}

223 224 225 226
/*
 * Arguments:
 *      argv[2] - wlan interface
 *      argv[3] - softap interface
227 228 229 230 231 232
 *      argv[4] - SSID
 *	argv[5] - Security
 *	argv[6] - Key
 *	argv[7] - Channel
 *	argv[8] - Preamble
 *	argv[9] - Max SCB
233
 */
234
int SoftapController::setSoftap(int argc, char *argv[]) {
235 236
    unsigned char psk[SHA256_DIGEST_LENGTH];
    char psk_str[2*SHA256_DIGEST_LENGTH+1];
237 238
    int ret, i = 0;
    char *ssid, *iface;
239 240

    if (mSock < 0) {
241
        LOGE("Softap set - failed to open socket");
242 243 244
        return -1;
    }
    if (argc < 4) {
245
        LOGE("Softap set - missing arguments");
246 247 248 249
        return -1;
    }

    strncpy(mIface, argv[3], sizeof(mIface));
250
    iface = argv[2];
251 252 253 254

    /* Create command line */
    i = addParam(i, "ASCII_CMD", "AP_CFG");
    if (argc > 4) {
255
        ssid = argv[4];
256
    } else {
257
        ssid = (char *)"AndroidAP";
258
    }
259
    i = addParam(i, "SSID", ssid);
260 261 262 263 264 265
    if (argc > 5) {
        i = addParam(i, "SEC", argv[5]);
    } else {
        i = addParam(i, "SEC", "open");
    }
    if (argc > 6) {
266
        int j;
267 268 269 270 271
        // Use the PKCS#5 PBKDF2 with 4096 iterations
        PKCS5_PBKDF2_HMAC_SHA1(argv[6], strlen(argv[6]),
                reinterpret_cast<const unsigned char *>(ssid), strlen(ssid),
                4096, SHA256_DIGEST_LENGTH, psk);
        for (j=0; j < SHA256_DIGEST_LENGTH; j++) {
272 273 274 275
            sprintf(&psk_str[j<<1], "%02x", psk[j]);
        }
        psk_str[j<<1] = '\0';
        i = addParam(i, "KEY", psk_str);
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
    } else {
        i = addParam(i, "KEY", "12345678");
    }
    if (argc > 7) {
        i = addParam(i, "CHANNEL", argv[7]);
    } else {
        i = addParam(i, "CHANNEL", "6");
    }
    if (argc > 8) {
        i = addParam(i, "PREAMBLE", argv[8]);
    } else {
        i = addParam(i, "PREAMBLE", "0");
    }
    if (argc > 9) {
        i = addParam(i, "MAX_SCB", argv[9]);
    } else {
        i = addParam(i, "MAX_SCB", "8");
    }
    if ((i < 0) || ((unsigned)(i + 4) >= sizeof(mBuf))) {
        LOGE("Softap set - command is too big");
        return i;
297
    }
298
    sprintf(&mBuf[i], "END");
299 300

    /* system("iwpriv eth0 WL_AP_CFG ASCII_CMD=AP_CFG,SSID=\"AndroidAP\",SEC=\"open\",KEY=12345,CHANNEL=1,PREAMBLE=0,MAX_SCB=8,END"); */
301
    ret = setCommand(iface, "AP_SET_CFG");
302 303 304 305 306
    if (ret) {
        LOGE("Softap set - failed: %d", ret);
    }
    else {
        LOGD("Softap set - Ok");
307
        usleep(AP_SET_CFG_DELAY);
308 309
    }
    return ret;
310
}
311 312 313 314 315 316 317 318

/*
 * Arguments:
 *	argv[2] - interface name
 *	argv[3] - AP or STA
 */
int SoftapController::fwReloadSoftap(int argc, char *argv[])
{
319
    int ret, i = 0;
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
    char *iface;

    if (mSock < 0) {
        LOGE("Softap fwrealod - failed to open socket");
        return -1;
    }
    if (argc < 4) {
        LOGE("Softap fwreload - missing arguments");
        return -1;
    }

    iface = argv[2];

    if (strcmp(argv[3], "AP") == 0) {
#ifdef WIFI_DRIVER_FW_AP_PATH
        sprintf(mBuf, "FW_PATH=%s", WIFI_DRIVER_FW_AP_PATH);
#endif
    } else {
#ifdef WIFI_DRIVER_FW_STA_PATH
        sprintf(mBuf, "FW_PATH=%s", WIFI_DRIVER_FW_STA_PATH);
#endif
    }
342
    ret = setCommand(iface, "WL_FW_RELOAD");
343 344 345 346 347 348 349 350
    if (ret) {
        LOGE("Softap fwReload - failed: %d", ret);
    }
    else {
        LOGD("Softap fwReload - Ok");
    }
    return ret;
}
Dmitry Shmidt's avatar
Dmitry Shmidt committed
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369

int SoftapController::clientsSoftap(char **retbuf)
{
    int ret;

    if (mSock < 0) {
        LOGE("Softap clients - failed to open socket");
        return -1;
    }
    *mBuf = 0;
    ret = setCommand(mIface, "AP_GET_STA_LIST", SOFTAP_MAX_BUFFER_SIZE);
    if (ret) {
        LOGE("Softap clients - failed: %d", ret);
    } else {
        asprintf(retbuf, "Softap clients:%s", mBuf);
        LOGD("Softap clients:%s", mBuf);
    }
    return ret;
}