1/*
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#include <string.h>
21
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/ioctl.h>
25#include <sys/types.h>
26#include <sys/wait.h>
27
28#include <netinet/in.h>
29#include <arpa/inet.h>
30
31#include <linux/wireless.h>
32
33#include <openssl/evp.h>
34#include <openssl/sha.h>
35
36#define LOG_TAG "SoftapController"
37#include <cutils/log.h>
38#include <netutils/ifc.h>
39#include <private/android_filesystem_config.h>
40#include "wifi.h"
41#include "ResponseCode.h"
42
43#include "SoftapController.h"
44
45static const char HOSTAPD_CONF_FILE[]    = "/data/misc/wifi/hostapd.conf";
46static const char HOSTAPD_BIN_FILE[]    = "/system/bin/hostapd";
47
48SoftapController::SoftapController()
49    : mPid(0) {}
50
51SoftapController::~SoftapController() {
52}
53
54int SoftapController::startSoftap() {
55    pid_t pid = 1;
56
57    if (mPid) {
58        ALOGE("SoftAP is already running");
59        return ResponseCode::SoftapStatusResult;
60    }
61
62    if ((pid = fork()) < 0) {
63        ALOGE("fork failed (%s)", strerror(errno));
64        return ResponseCode::ServiceStartFailed;
65    }
66
67    if (!pid) {
68        ensure_entropy_file_exists();
69        if (execl(HOSTAPD_BIN_FILE, HOSTAPD_BIN_FILE,
70                  "-e", WIFI_ENTROPY_FILE,
71                  HOSTAPD_CONF_FILE, (char *) NULL)) {
72            ALOGE("execl failed (%s)", strerror(errno));
73        }
74        ALOGE("SoftAP failed to start");
75        return ResponseCode::ServiceStartFailed;
76    } else {
77        mPid = pid;
78        ALOGD("SoftAP started successfully");
79        usleep(AP_BSS_START_DELAY);
80    }
81    return ResponseCode::SoftapStatusResult;
82}
83
84int SoftapController::stopSoftap() {
85
86    if (mPid == 0) {
87        ALOGE("SoftAP is not running");
88        return ResponseCode::SoftapStatusResult;
89    }
90
91    ALOGD("Stopping the SoftAP service...");
92    kill(mPid, SIGTERM);
93    waitpid(mPid, NULL, 0);
94
95    mPid = 0;
96    ALOGD("SoftAP stopped successfully");
97    usleep(AP_BSS_STOP_DELAY);
98    return ResponseCode::SoftapStatusResult;
99}
100
101bool SoftapController::isSoftapStarted() {
102    return (mPid != 0);
103}
104
105/*
106 * Arguments:
107 *  argv[2] - wlan interface
108 *  argv[3] - SSID
109 *  argv[4] - Security
110 *  argv[5] - Key
111 */
112int SoftapController::setSoftap(int argc, char *argv[]) {
113    char psk_str[2*SHA256_DIGEST_LENGTH+1];
114    int ret = ResponseCode::SoftapStatusResult;
115    int i = 0;
116    int fd;
117
118    if (argc < 4) {
119        ALOGE("Softap set is missing arguments. Please use: softap <wlan iface> <SSID> <wpa2?-psk|open> <passphrase>");
120        return ResponseCode::CommandSyntaxError;
121    }
122
123    char *wbuf = NULL;
124    char *fbuf = NULL;
125
126    asprintf(&wbuf, "interface=%s\ndriver=nl80211\nctrl_interface="
127            "/data/misc/wifi/hostapd\nssid=%s\nchannel=6\nieee80211n=1\n"
128            "hw_mode=g\n",
129            argv[2], argv[3]);
130
131    if (argc > 4) {
132        if (!strcmp(argv[4], "wpa-psk")) {
133            generatePsk(argv[3], argv[5], psk_str);
134            asprintf(&fbuf, "%swpa=1\nwpa_pairwise=TKIP CCMP\nwpa_psk=%s\n", wbuf, psk_str);
135        } else if (!strcmp(argv[4], "wpa2-psk")) {
136            generatePsk(argv[3], argv[5], psk_str);
137            asprintf(&fbuf, "%swpa=2\nrsn_pairwise=CCMP\nwpa_psk=%s\n", wbuf, psk_str);
138        } else if (!strcmp(argv[4], "open")) {
139            asprintf(&fbuf, "%s", wbuf);
140        }
141    } else {
142        asprintf(&fbuf, "%s", wbuf);
143    }
144
145    fd = open(HOSTAPD_CONF_FILE, O_CREAT | O_TRUNC | O_WRONLY | O_NOFOLLOW, 0660);
146    if (fd < 0) {
147        ALOGE("Cannot update \"%s\": %s", HOSTAPD_CONF_FILE, strerror(errno));
148        free(wbuf);
149        free(fbuf);
150        return ResponseCode::OperationFailed;
151    }
152    if (write(fd, fbuf, strlen(fbuf)) < 0) {
153        ALOGE("Cannot write to \"%s\": %s", HOSTAPD_CONF_FILE, strerror(errno));
154        ret = ResponseCode::OperationFailed;
155    }
156    free(wbuf);
157    free(fbuf);
158
159    /* Note: apparently open can fail to set permissions correctly at times */
160    if (fchmod(fd, 0660) < 0) {
161        ALOGE("Error changing permissions of %s to 0660: %s",
162                HOSTAPD_CONF_FILE, strerror(errno));
163        close(fd);
164        unlink(HOSTAPD_CONF_FILE);
165        return ResponseCode::OperationFailed;
166    }
167
168    if (fchown(fd, AID_SYSTEM, AID_WIFI) < 0) {
169        ALOGE("Error changing group ownership of %s to %d: %s",
170                HOSTAPD_CONF_FILE, AID_WIFI, strerror(errno));
171        close(fd);
172        unlink(HOSTAPD_CONF_FILE);
173        return ResponseCode::OperationFailed;
174    }
175
176    close(fd);
177    return ret;
178}
179
180/*
181 * Arguments:
182 *	argv[2] - interface name
183 *	argv[3] - AP or P2P or STA
184 */
185int SoftapController::fwReloadSoftap(int argc, char *argv[])
186{
187    int i = 0;
188    char *fwpath = NULL;
189
190    if (argc < 4) {
191        ALOGE("SoftAP fwreload is missing arguments. Please use: softap <wlan iface> <AP|P2P|STA>");
192        return ResponseCode::CommandSyntaxError;
193    }
194
195    if (strcmp(argv[3], "AP") == 0) {
196        fwpath = (char *)wifi_get_fw_path(WIFI_GET_FW_PATH_AP);
197    } else if (strcmp(argv[3], "P2P") == 0) {
198        fwpath = (char *)wifi_get_fw_path(WIFI_GET_FW_PATH_P2P);
199    } else if (strcmp(argv[3], "STA") == 0) {
200        fwpath = (char *)wifi_get_fw_path(WIFI_GET_FW_PATH_STA);
201    }
202    if (!fwpath)
203        return ResponseCode::CommandParameterError;
204    if (wifi_change_fw_path((const char *)fwpath)) {
205        ALOGE("Softap fwReload failed");
206        return ResponseCode::OperationFailed;
207    }
208    else {
209        ALOGD("Softap fwReload - Ok");
210    }
211    return ResponseCode::SoftapStatusResult;
212}
213
214void SoftapController::generatePsk(char *ssid, char *passphrase, char *psk_str) {
215    unsigned char psk[SHA256_DIGEST_LENGTH];
216    int j;
217    // Use the PKCS#5 PBKDF2 with 4096 iterations
218    PKCS5_PBKDF2_HMAC_SHA1(passphrase, strlen(passphrase),
219            reinterpret_cast<const unsigned char *>(ssid), strlen(ssid),
220            4096, SHA256_DIGEST_LENGTH, psk);
221    for (j=0; j < SHA256_DIGEST_LENGTH; j++) {
222        sprintf(&psk_str[j*2], "%02x", psk[j]);
223    }
224}
225