Dmitry Shmidt | 5af38c3 | 2010-02-10 11:10:39 -0800 | [diff] [blame] | 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> |
Olivier Bailly | ff2c0d8 | 2010-11-17 11:45:07 -0800 | [diff] [blame] | 20 | #include <string.h> |
Dmitry Shmidt | 5af38c3 | 2010-02-10 11:10:39 -0800 | [diff] [blame] | 21 | |
| 22 | #include <sys/socket.h> |
| 23 | #include <sys/stat.h> |
Olivier Bailly | ff2c0d8 | 2010-11-17 11:45:07 -0800 | [diff] [blame] | 24 | #include <sys/ioctl.h> |
Dmitry Shmidt | 5af38c3 | 2010-02-10 11:10:39 -0800 | [diff] [blame] | 25 | #include <sys/types.h> |
| 26 | #include <sys/wait.h> |
| 27 | |
| 28 | #include <netinet/in.h> |
| 29 | #include <arpa/inet.h> |
| 30 | |
Dmitry Shmidt | 84c65a6 | 2010-03-03 13:14:30 -0800 | [diff] [blame] | 31 | #include <linux/wireless.h> |
| 32 | |
Kenny Root | a2d7e3e | 2010-03-15 14:26:36 -0700 | [diff] [blame] | 33 | #include <openssl/evp.h> |
| 34 | #include <openssl/sha.h> |
| 35 | |
Dmitry Shmidt | 5af38c3 | 2010-02-10 11:10:39 -0800 | [diff] [blame] | 36 | #define LOG_TAG "SoftapController" |
| 37 | #include <cutils/log.h> |
Dmitry Shmidt | 389f8d1 | 2011-07-21 15:16:04 -0700 | [diff] [blame] | 38 | #include <netutils/ifc.h> |
Dmitry Shmidt | fe15b63 | 2011-07-19 13:55:25 -0700 | [diff] [blame] | 39 | #include "wifi.h" |
Dmitry Shmidt | 5af38c3 | 2010-02-10 11:10:39 -0800 | [diff] [blame] | 40 | |
| 41 | #include "SoftapController.h" |
| 42 | |
| 43 | SoftapController::SoftapController() { |
| 44 | mPid = 0; |
Dmitry Shmidt | 84c65a6 | 2010-03-03 13:14:30 -0800 | [diff] [blame] | 45 | mSock = socket(AF_INET, SOCK_DGRAM, 0); |
| 46 | if (mSock < 0) |
| 47 | LOGE("Failed to open socket"); |
| 48 | memset(mIface, 0, sizeof(mIface)); |
Dmitry Shmidt | 5af38c3 | 2010-02-10 11:10:39 -0800 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | SoftapController::~SoftapController() { |
Dmitry Shmidt | 84c65a6 | 2010-03-03 13:14:30 -0800 | [diff] [blame] | 52 | if (mSock >= 0) |
| 53 | close(mSock); |
| 54 | } |
| 55 | |
Dmitry Shmidt | 666fe25 | 2011-03-08 11:01:58 -0800 | [diff] [blame] | 56 | int SoftapController::setCommand(char *iface, const char *fname, unsigned buflen) { |
Dmitry Shmidt | 389f8d1 | 2011-07-21 15:16:04 -0700 | [diff] [blame] | 57 | #ifdef HAVE_HOSTAPD |
| 58 | return 0; |
| 59 | #else |
Dmitry Shmidt | 6665fb2 | 2010-09-22 17:31:07 -0700 | [diff] [blame] | 60 | char tBuf[SOFTAP_MAX_BUFFER_SIZE]; |
Dmitry Shmidt | 84c65a6 | 2010-03-03 13:14:30 -0800 | [diff] [blame] | 61 | struct iwreq wrq; |
| 62 | struct iw_priv_args *priv_ptr; |
jmzhu | fbd11c4 | 2010-10-11 16:47:05 -0700 | [diff] [blame] | 63 | int i, j, ret; |
| 64 | int cmd = 0, sub_cmd = 0; |
Dmitry Shmidt | 84c65a6 | 2010-03-03 13:14:30 -0800 | [diff] [blame] | 65 | |
| 66 | strncpy(wrq.ifr_name, iface, sizeof(wrq.ifr_name)); |
Dmitry Shmidt | 6665fb2 | 2010-09-22 17:31:07 -0700 | [diff] [blame] | 67 | wrq.u.data.pointer = tBuf; |
| 68 | wrq.u.data.length = sizeof(tBuf) / sizeof(struct iw_priv_args); |
Dmitry Shmidt | 84c65a6 | 2010-03-03 13:14:30 -0800 | [diff] [blame] | 69 | wrq.u.data.flags = 0; |
| 70 | if ((ret = ioctl(mSock, SIOCGIWPRIV, &wrq)) < 0) { |
| 71 | LOGE("SIOCGIPRIV failed: %d", ret); |
| 72 | return ret; |
| 73 | } |
jmzhu | fbd11c4 | 2010-10-11 16:47:05 -0700 | [diff] [blame] | 74 | |
Dmitry Shmidt | 84c65a6 | 2010-03-03 13:14:30 -0800 | [diff] [blame] | 75 | priv_ptr = (struct iw_priv_args *)wrq.u.data.pointer; |
jmzhu | fbd11c4 | 2010-10-11 16:47:05 -0700 | [diff] [blame] | 76 | for(i=0; i < wrq.u.data.length;i++) { |
| 77 | if (strcmp(priv_ptr[i].name, fname) == 0) { |
| 78 | cmd = priv_ptr[i].cmd; |
| 79 | break; |
| 80 | } |
Dmitry Shmidt | 84c65a6 | 2010-03-03 13:14:30 -0800 | [diff] [blame] | 81 | } |
Dmitry Shmidt | 5af38c3 | 2010-02-10 11:10:39 -0800 | [diff] [blame] | 82 | |
jmzhu | fbd11c4 | 2010-10-11 16:47:05 -0700 | [diff] [blame] | 83 | if (i == wrq.u.data.length) { |
| 84 | LOGE("iface:%s, fname: %s - function not supported", iface, fname); |
Dmitry Shmidt | 6665fb2 | 2010-09-22 17:31:07 -0700 | [diff] [blame] | 85 | return -1; |
| 86 | } |
| 87 | |
jmzhu | fbd11c4 | 2010-10-11 16:47:05 -0700 | [diff] [blame] | 88 | if (cmd < SIOCDEVPRIVATE) { |
| 89 | for(j=0; j < i; j++) { |
| 90 | if ((priv_ptr[j].set_args == priv_ptr[i].set_args) && |
| 91 | (priv_ptr[j].get_args == priv_ptr[i].get_args) && |
| 92 | (priv_ptr[j].name[0] == '\0')) |
| 93 | break; |
| 94 | } |
| 95 | if (j == i) { |
| 96 | LOGE("iface:%s, fname: %s - invalid private ioctl", iface, fname); |
| 97 | return -1; |
| 98 | } |
| 99 | sub_cmd = cmd; |
| 100 | cmd = priv_ptr[j].cmd; |
| 101 | } |
| 102 | |
Dmitry Shmidt | 6665fb2 | 2010-09-22 17:31:07 -0700 | [diff] [blame] | 103 | strncpy(wrq.ifr_name, iface, sizeof(wrq.ifr_name)); |
Dmitry Shmidt | 666fe25 | 2011-03-08 11:01:58 -0800 | [diff] [blame] | 104 | if ((buflen == 0) && (*mBuf != 0)) |
Dmitry Shmidt | 6665fb2 | 2010-09-22 17:31:07 -0700 | [diff] [blame] | 105 | wrq.u.data.length = strlen(mBuf) + 1; |
| 106 | else |
Dmitry Shmidt | 666fe25 | 2011-03-08 11:01:58 -0800 | [diff] [blame] | 107 | wrq.u.data.length = buflen; |
Dmitry Shmidt | 6665fb2 | 2010-09-22 17:31:07 -0700 | [diff] [blame] | 108 | wrq.u.data.pointer = mBuf; |
jmzhu | fbd11c4 | 2010-10-11 16:47:05 -0700 | [diff] [blame] | 109 | wrq.u.data.flags = sub_cmd; |
| 110 | ret = ioctl(mSock, cmd, &wrq); |
Dmitry Shmidt | 6665fb2 | 2010-09-22 17:31:07 -0700 | [diff] [blame] | 111 | return ret; |
Dmitry Shmidt | 389f8d1 | 2011-07-21 15:16:04 -0700 | [diff] [blame] | 112 | #endif |
Dmitry Shmidt | 6665fb2 | 2010-09-22 17:31:07 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | int SoftapController::startDriver(char *iface) { |
| 116 | int ret; |
| 117 | |
Dmitry Shmidt | c3539e2 | 2010-03-12 14:16:46 -0800 | [diff] [blame] | 118 | if (mSock < 0) { |
Dmitry Shmidt | a165913 | 2010-04-23 12:43:32 -0700 | [diff] [blame] | 119 | LOGE("Softap driver start - failed to open socket"); |
Dmitry Shmidt | c3539e2 | 2010-03-12 14:16:46 -0800 | [diff] [blame] | 120 | return -1; |
| 121 | } |
| 122 | if (!iface || (iface[0] == '\0')) { |
Dmitry Shmidt | a165913 | 2010-04-23 12:43:32 -0700 | [diff] [blame] | 123 | LOGD("Softap driver start - wrong interface"); |
Dmitry Shmidt | c3539e2 | 2010-03-12 14:16:46 -0800 | [diff] [blame] | 124 | iface = mIface; |
| 125 | } |
Dmitry Shmidt | 6665fb2 | 2010-09-22 17:31:07 -0700 | [diff] [blame] | 126 | |
| 127 | *mBuf = 0; |
| 128 | ret = setCommand(iface, "START"); |
Dmitry Shmidt | 389f8d1 | 2011-07-21 15:16:04 -0700 | [diff] [blame] | 129 | if (ret < 0) { |
| 130 | LOGE("Softap driver start: %d", ret); |
| 131 | return ret; |
| 132 | } |
| 133 | #ifdef HAVE_HOSTAPD |
| 134 | ifc_init(); |
| 135 | ret = ifc_up(iface); |
| 136 | ifc_close(); |
| 137 | #endif |
Dmitry Shmidt | a165913 | 2010-04-23 12:43:32 -0700 | [diff] [blame] | 138 | usleep(AP_DRIVER_START_DELAY); |
| 139 | LOGD("Softap driver start: %d", ret); |
Dmitry Shmidt | c3539e2 | 2010-03-12 14:16:46 -0800 | [diff] [blame] | 140 | return ret; |
| 141 | } |
| 142 | |
| 143 | int SoftapController::stopDriver(char *iface) { |
Dmitry Shmidt | 6665fb2 | 2010-09-22 17:31:07 -0700 | [diff] [blame] | 144 | int ret; |
Dmitry Shmidt | c3539e2 | 2010-03-12 14:16:46 -0800 | [diff] [blame] | 145 | |
| 146 | if (mSock < 0) { |
Dmitry Shmidt | a165913 | 2010-04-23 12:43:32 -0700 | [diff] [blame] | 147 | LOGE("Softap driver stop - failed to open socket"); |
Dmitry Shmidt | c3539e2 | 2010-03-12 14:16:46 -0800 | [diff] [blame] | 148 | return -1; |
| 149 | } |
| 150 | if (!iface || (iface[0] == '\0')) { |
Dmitry Shmidt | a165913 | 2010-04-23 12:43:32 -0700 | [diff] [blame] | 151 | LOGD("Softap driver stop - wrong interface"); |
Dmitry Shmidt | c3539e2 | 2010-03-12 14:16:46 -0800 | [diff] [blame] | 152 | iface = mIface; |
| 153 | } |
Dmitry Shmidt | 6665fb2 | 2010-09-22 17:31:07 -0700 | [diff] [blame] | 154 | *mBuf = 0; |
Dmitry Shmidt | 389f8d1 | 2011-07-21 15:16:04 -0700 | [diff] [blame] | 155 | #ifdef HAVE_HOSTAPD |
| 156 | ifc_init(); |
| 157 | ret = ifc_down(iface); |
| 158 | ifc_close(); |
| 159 | if (ret < 0) { |
| 160 | LOGE("Softap %s down: %d", iface, ret); |
| 161 | return ret; |
| 162 | } |
| 163 | #endif |
Dmitry Shmidt | 6665fb2 | 2010-09-22 17:31:07 -0700 | [diff] [blame] | 164 | ret = setCommand(iface, "STOP"); |
Dmitry Shmidt | a165913 | 2010-04-23 12:43:32 -0700 | [diff] [blame] | 165 | LOGD("Softap driver stop: %d", ret); |
Dmitry Shmidt | c3539e2 | 2010-03-12 14:16:46 -0800 | [diff] [blame] | 166 | return ret; |
| 167 | } |
| 168 | |
Dmitry Shmidt | 5af38c3 | 2010-02-10 11:10:39 -0800 | [diff] [blame] | 169 | int SoftapController::startSoftap() { |
| 170 | pid_t pid = 1; |
Dmitry Shmidt | 6665fb2 | 2010-09-22 17:31:07 -0700 | [diff] [blame] | 171 | int ret = 0; |
Dmitry Shmidt | 5af38c3 | 2010-02-10 11:10:39 -0800 | [diff] [blame] | 172 | |
Dmitry Shmidt | 5af38c3 | 2010-02-10 11:10:39 -0800 | [diff] [blame] | 173 | if (mPid) { |
| 174 | LOGE("Softap already started"); |
Dmitry Shmidt | a165913 | 2010-04-23 12:43:32 -0700 | [diff] [blame] | 175 | return 0; |
Dmitry Shmidt | 5af38c3 | 2010-02-10 11:10:39 -0800 | [diff] [blame] | 176 | } |
Dmitry Shmidt | 84c65a6 | 2010-03-03 13:14:30 -0800 | [diff] [blame] | 177 | if (mSock < 0) { |
Dmitry Shmidt | c3539e2 | 2010-03-12 14:16:46 -0800 | [diff] [blame] | 178 | LOGE("Softap startap - failed to open socket"); |
Dmitry Shmidt | 84c65a6 | 2010-03-03 13:14:30 -0800 | [diff] [blame] | 179 | return -1; |
| 180 | } |
Dmitry Shmidt | 389f8d1 | 2011-07-21 15:16:04 -0700 | [diff] [blame] | 181 | #ifdef HAVE_HOSTAPD |
| 182 | if ((pid = fork()) < 0) { |
Dmitry Shmidt | 5af38c3 | 2010-02-10 11:10:39 -0800 | [diff] [blame] | 183 | LOGE("fork failed (%s)", strerror(errno)); |
| 184 | return -1; |
| 185 | } |
| 186 | #endif |
| 187 | if (!pid) { |
Dmitry Shmidt | 389f8d1 | 2011-07-21 15:16:04 -0700 | [diff] [blame] | 188 | #ifdef HAVE_HOSTAPD |
| 189 | if (execl("/system/bin/hostapd", "/system/bin/hostapd", "-B", |
| 190 | "/data/misc/wifi/hostapd.conf", (char *) NULL)) { |
| 191 | LOGE("execl failed (%s)", strerror(errno)); |
| 192 | } |
| 193 | #endif |
| 194 | LOGE("Should never get here!"); |
| 195 | return -1; |
Dmitry Shmidt | 5af38c3 | 2010-02-10 11:10:39 -0800 | [diff] [blame] | 196 | } else { |
Dmitry Shmidt | 6665fb2 | 2010-09-22 17:31:07 -0700 | [diff] [blame] | 197 | *mBuf = 0; |
| 198 | ret = setCommand(mIface, "AP_BSS_START"); |
Dmitry Shmidt | 84c65a6 | 2010-03-03 13:14:30 -0800 | [diff] [blame] | 199 | if (ret) { |
Dmitry Shmidt | c3539e2 | 2010-03-12 14:16:46 -0800 | [diff] [blame] | 200 | LOGE("Softap startap - failed: %d", ret); |
Dmitry Shmidt | 84c65a6 | 2010-03-03 13:14:30 -0800 | [diff] [blame] | 201 | } |
| 202 | else { |
| 203 | mPid = pid; |
Dmitry Shmidt | c3539e2 | 2010-03-12 14:16:46 -0800 | [diff] [blame] | 204 | LOGD("Softap startap - Ok"); |
Dmitry Shmidt | 3df450a | 2010-03-18 13:06:47 -0700 | [diff] [blame] | 205 | usleep(AP_BSS_START_DELAY); |
Dmitry Shmidt | 84c65a6 | 2010-03-03 13:14:30 -0800 | [diff] [blame] | 206 | } |
Dmitry Shmidt | 5af38c3 | 2010-02-10 11:10:39 -0800 | [diff] [blame] | 207 | } |
Dmitry Shmidt | 84c65a6 | 2010-03-03 13:14:30 -0800 | [diff] [blame] | 208 | return ret; |
Dmitry Shmidt | 5af38c3 | 2010-02-10 11:10:39 -0800 | [diff] [blame] | 209 | |
| 210 | } |
| 211 | |
| 212 | int SoftapController::stopSoftap() { |
Dmitry Shmidt | 6665fb2 | 2010-09-22 17:31:07 -0700 | [diff] [blame] | 213 | int ret; |
Dmitry Shmidt | 84c65a6 | 2010-03-03 13:14:30 -0800 | [diff] [blame] | 214 | |
Dmitry Shmidt | 5af38c3 | 2010-02-10 11:10:39 -0800 | [diff] [blame] | 215 | if (mPid == 0) { |
| 216 | LOGE("Softap already stopped"); |
| 217 | return 0; |
| 218 | } |
Dmitry Shmidt | 389f8d1 | 2011-07-21 15:16:04 -0700 | [diff] [blame] | 219 | |
| 220 | #ifdef HAVE_HOSTAPD |
| 221 | LOGD("Stopping Softap service"); |
| 222 | kill(mPid, SIGTERM); |
| 223 | waitpid(mPid, NULL, 0); |
| 224 | #endif |
Dmitry Shmidt | 84c65a6 | 2010-03-03 13:14:30 -0800 | [diff] [blame] | 225 | if (mSock < 0) { |
Dmitry Shmidt | c3539e2 | 2010-03-12 14:16:46 -0800 | [diff] [blame] | 226 | LOGE("Softap stopap - failed to open socket"); |
Dmitry Shmidt | 84c65a6 | 2010-03-03 13:14:30 -0800 | [diff] [blame] | 227 | return -1; |
| 228 | } |
Dmitry Shmidt | 6665fb2 | 2010-09-22 17:31:07 -0700 | [diff] [blame] | 229 | *mBuf = 0; |
| 230 | ret = setCommand(mIface, "AP_BSS_STOP"); |
Dmitry Shmidt | 5af38c3 | 2010-02-10 11:10:39 -0800 | [diff] [blame] | 231 | mPid = 0; |
Dmitry Shmidt | 84c65a6 | 2010-03-03 13:14:30 -0800 | [diff] [blame] | 232 | LOGD("Softap service stopped: %d", ret); |
Dmitry Shmidt | 3df450a | 2010-03-18 13:06:47 -0700 | [diff] [blame] | 233 | usleep(AP_BSS_STOP_DELAY); |
Dmitry Shmidt | 84c65a6 | 2010-03-03 13:14:30 -0800 | [diff] [blame] | 234 | return ret; |
Dmitry Shmidt | 5af38c3 | 2010-02-10 11:10:39 -0800 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | bool SoftapController::isSoftapStarted() { |
| 238 | return (mPid != 0 ? true : false); |
| 239 | } |
| 240 | |
Dmitry Shmidt | 7977d67 | 2010-03-09 10:57:59 -0800 | [diff] [blame] | 241 | int SoftapController::addParam(int pos, const char *cmd, const char *arg) |
| 242 | { |
| 243 | if (pos < 0) |
| 244 | return pos; |
| 245 | if ((unsigned)(pos + strlen(cmd) + strlen(arg) + 1) >= sizeof(mBuf)) { |
| 246 | LOGE("Command line is too big"); |
| 247 | return -1; |
| 248 | } |
| 249 | pos += sprintf(&mBuf[pos], "%s=%s,", cmd, arg); |
| 250 | return pos; |
| 251 | } |
| 252 | |
Dmitry Shmidt | 84c65a6 | 2010-03-03 13:14:30 -0800 | [diff] [blame] | 253 | /* |
| 254 | * Arguments: |
| 255 | * argv[2] - wlan interface |
| 256 | * argv[3] - softap interface |
Dmitry Shmidt | 7977d67 | 2010-03-09 10:57:59 -0800 | [diff] [blame] | 257 | * argv[4] - SSID |
| 258 | * argv[5] - Security |
| 259 | * argv[6] - Key |
| 260 | * argv[7] - Channel |
| 261 | * argv[8] - Preamble |
| 262 | * argv[9] - Max SCB |
Dmitry Shmidt | 84c65a6 | 2010-03-03 13:14:30 -0800 | [diff] [blame] | 263 | */ |
Dmitry Shmidt | 5af38c3 | 2010-02-10 11:10:39 -0800 | [diff] [blame] | 264 | int SoftapController::setSoftap(int argc, char *argv[]) { |
Kenny Root | a2d7e3e | 2010-03-15 14:26:36 -0700 | [diff] [blame] | 265 | unsigned char psk[SHA256_DIGEST_LENGTH]; |
| 266 | char psk_str[2*SHA256_DIGEST_LENGTH+1]; |
Dmitry Shmidt | 6665fb2 | 2010-09-22 17:31:07 -0700 | [diff] [blame] | 267 | int ret, i = 0; |
| 268 | char *ssid, *iface; |
Dmitry Shmidt | 84c65a6 | 2010-03-03 13:14:30 -0800 | [diff] [blame] | 269 | |
Dmitry Shmidt | 84c65a6 | 2010-03-03 13:14:30 -0800 | [diff] [blame] | 270 | if (mSock < 0) { |
Dmitry Shmidt | 7977d67 | 2010-03-09 10:57:59 -0800 | [diff] [blame] | 271 | LOGE("Softap set - failed to open socket"); |
Dmitry Shmidt | 84c65a6 | 2010-03-03 13:14:30 -0800 | [diff] [blame] | 272 | return -1; |
| 273 | } |
| 274 | if (argc < 4) { |
Dmitry Shmidt | 7977d67 | 2010-03-09 10:57:59 -0800 | [diff] [blame] | 275 | LOGE("Softap set - missing arguments"); |
Dmitry Shmidt | 84c65a6 | 2010-03-03 13:14:30 -0800 | [diff] [blame] | 276 | return -1; |
| 277 | } |
| 278 | |
Dmitry Shmidt | 84c65a6 | 2010-03-03 13:14:30 -0800 | [diff] [blame] | 279 | strncpy(mIface, argv[3], sizeof(mIface)); |
Dmitry Shmidt | 6665fb2 | 2010-09-22 17:31:07 -0700 | [diff] [blame] | 280 | iface = argv[2]; |
Dmitry Shmidt | 7977d67 | 2010-03-09 10:57:59 -0800 | [diff] [blame] | 281 | |
| 282 | /* Create command line */ |
| 283 | i = addParam(i, "ASCII_CMD", "AP_CFG"); |
| 284 | if (argc > 4) { |
Dmitry Shmidt | 321f95a | 2010-03-09 15:46:34 -0800 | [diff] [blame] | 285 | ssid = argv[4]; |
Dmitry Shmidt | 7977d67 | 2010-03-09 10:57:59 -0800 | [diff] [blame] | 286 | } else { |
Dmitry Shmidt | 321f95a | 2010-03-09 15:46:34 -0800 | [diff] [blame] | 287 | ssid = (char *)"AndroidAP"; |
Dmitry Shmidt | 84c65a6 | 2010-03-03 13:14:30 -0800 | [diff] [blame] | 288 | } |
Dmitry Shmidt | 321f95a | 2010-03-09 15:46:34 -0800 | [diff] [blame] | 289 | i = addParam(i, "SSID", ssid); |
Dmitry Shmidt | 7977d67 | 2010-03-09 10:57:59 -0800 | [diff] [blame] | 290 | if (argc > 5) { |
| 291 | i = addParam(i, "SEC", argv[5]); |
| 292 | } else { |
| 293 | i = addParam(i, "SEC", "open"); |
| 294 | } |
| 295 | if (argc > 6) { |
Dmitry Shmidt | 321f95a | 2010-03-09 15:46:34 -0800 | [diff] [blame] | 296 | int j; |
Kenny Root | a2d7e3e | 2010-03-15 14:26:36 -0700 | [diff] [blame] | 297 | // Use the PKCS#5 PBKDF2 with 4096 iterations |
| 298 | PKCS5_PBKDF2_HMAC_SHA1(argv[6], strlen(argv[6]), |
| 299 | reinterpret_cast<const unsigned char *>(ssid), strlen(ssid), |
| 300 | 4096, SHA256_DIGEST_LENGTH, psk); |
| 301 | for (j=0; j < SHA256_DIGEST_LENGTH; j++) { |
Dmitry Shmidt | 321f95a | 2010-03-09 15:46:34 -0800 | [diff] [blame] | 302 | sprintf(&psk_str[j<<1], "%02x", psk[j]); |
| 303 | } |
| 304 | psk_str[j<<1] = '\0'; |
| 305 | i = addParam(i, "KEY", psk_str); |
Dmitry Shmidt | 7977d67 | 2010-03-09 10:57:59 -0800 | [diff] [blame] | 306 | } else { |
| 307 | i = addParam(i, "KEY", "12345678"); |
| 308 | } |
| 309 | if (argc > 7) { |
| 310 | i = addParam(i, "CHANNEL", argv[7]); |
| 311 | } else { |
| 312 | i = addParam(i, "CHANNEL", "6"); |
| 313 | } |
| 314 | if (argc > 8) { |
| 315 | i = addParam(i, "PREAMBLE", argv[8]); |
| 316 | } else { |
| 317 | i = addParam(i, "PREAMBLE", "0"); |
| 318 | } |
| 319 | if (argc > 9) { |
| 320 | i = addParam(i, "MAX_SCB", argv[9]); |
| 321 | } else { |
| 322 | i = addParam(i, "MAX_SCB", "8"); |
| 323 | } |
| 324 | if ((i < 0) || ((unsigned)(i + 4) >= sizeof(mBuf))) { |
| 325 | LOGE("Softap set - command is too big"); |
| 326 | return i; |
| 327 | } |
| 328 | sprintf(&mBuf[i], "END"); |
Dmitry Shmidt | 84c65a6 | 2010-03-03 13:14:30 -0800 | [diff] [blame] | 329 | |
Dmitry Shmidt | 84c65a6 | 2010-03-03 13:14:30 -0800 | [diff] [blame] | 330 | /* 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 Shmidt | 6665fb2 | 2010-09-22 17:31:07 -0700 | [diff] [blame] | 331 | ret = setCommand(iface, "AP_SET_CFG"); |
Dmitry Shmidt | 84c65a6 | 2010-03-03 13:14:30 -0800 | [diff] [blame] | 332 | if (ret) { |
| 333 | LOGE("Softap set - failed: %d", ret); |
| 334 | } |
| 335 | else { |
| 336 | LOGD("Softap set - Ok"); |
Dmitry Shmidt | 3df450a | 2010-03-18 13:06:47 -0700 | [diff] [blame] | 337 | usleep(AP_SET_CFG_DELAY); |
Dmitry Shmidt | 84c65a6 | 2010-03-03 13:14:30 -0800 | [diff] [blame] | 338 | } |
| 339 | return ret; |
Dmitry Shmidt | 5af38c3 | 2010-02-10 11:10:39 -0800 | [diff] [blame] | 340 | } |
Dmitry Shmidt | 31fd6c5 | 2010-03-12 10:01:58 -0800 | [diff] [blame] | 341 | |
| 342 | /* |
| 343 | * Arguments: |
| 344 | * argv[2] - interface name |
| 345 | * argv[3] - AP or STA |
| 346 | */ |
| 347 | int SoftapController::fwReloadSoftap(int argc, char *argv[]) |
| 348 | { |
Dmitry Shmidt | 6665fb2 | 2010-09-22 17:31:07 -0700 | [diff] [blame] | 349 | int ret, i = 0; |
Dmitry Shmidt | 31fd6c5 | 2010-03-12 10:01:58 -0800 | [diff] [blame] | 350 | char *iface; |
Dmitry Shmidt | fe15b63 | 2011-07-19 13:55:25 -0700 | [diff] [blame] | 351 | char *fwpath; |
Dmitry Shmidt | 31fd6c5 | 2010-03-12 10:01:58 -0800 | [diff] [blame] | 352 | |
| 353 | if (mSock < 0) { |
| 354 | LOGE("Softap fwrealod - failed to open socket"); |
| 355 | return -1; |
| 356 | } |
| 357 | if (argc < 4) { |
| 358 | LOGE("Softap fwreload - missing arguments"); |
| 359 | return -1; |
| 360 | } |
| 361 | |
| 362 | iface = argv[2]; |
Dmitry Shmidt | 31fd6c5 | 2010-03-12 10:01:58 -0800 | [diff] [blame] | 363 | |
| 364 | if (strcmp(argv[3], "AP") == 0) { |
Dmitry Shmidt | fe15b63 | 2011-07-19 13:55:25 -0700 | [diff] [blame] | 365 | fwpath = (char *)wifi_get_fw_path(WIFI_GET_FW_PATH_AP); |
Dmitry Shmidt | 54cf7fd | 2011-07-24 16:46:13 -0700 | [diff] [blame^] | 366 | } else if (strcmp(argv[3], "P2P") == 0) { |
| 367 | fwpath = (char *)wifi_get_fw_path(WIFI_GET_FW_PATH_P2P); |
Dmitry Shmidt | 31fd6c5 | 2010-03-12 10:01:58 -0800 | [diff] [blame] | 368 | } else { |
Dmitry Shmidt | fe15b63 | 2011-07-19 13:55:25 -0700 | [diff] [blame] | 369 | fwpath = (char *)wifi_get_fw_path(WIFI_GET_FW_PATH_STA); |
Dmitry Shmidt | 31fd6c5 | 2010-03-12 10:01:58 -0800 | [diff] [blame] | 370 | } |
Dmitry Shmidt | fe15b63 | 2011-07-19 13:55:25 -0700 | [diff] [blame] | 371 | if (!fwpath) |
| 372 | return -1; |
Dmitry Shmidt | 389f8d1 | 2011-07-21 15:16:04 -0700 | [diff] [blame] | 373 | #ifdef HAVE_HOSTAPD |
| 374 | ret = wifi_change_fw_path((const char *)fwpath); |
| 375 | #else |
Dmitry Shmidt | fe15b63 | 2011-07-19 13:55:25 -0700 | [diff] [blame] | 376 | sprintf(mBuf, "FW_PATH=%s", fwpath); |
Dmitry Shmidt | 6665fb2 | 2010-09-22 17:31:07 -0700 | [diff] [blame] | 377 | ret = setCommand(iface, "WL_FW_RELOAD"); |
Dmitry Shmidt | 389f8d1 | 2011-07-21 15:16:04 -0700 | [diff] [blame] | 378 | #endif |
Dmitry Shmidt | 31fd6c5 | 2010-03-12 10:01:58 -0800 | [diff] [blame] | 379 | if (ret) { |
| 380 | LOGE("Softap fwReload - failed: %d", ret); |
| 381 | } |
| 382 | else { |
| 383 | LOGD("Softap fwReload - Ok"); |
| 384 | } |
| 385 | return ret; |
| 386 | } |
Dmitry Shmidt | 666fe25 | 2011-03-08 11:01:58 -0800 | [diff] [blame] | 387 | |
| 388 | int SoftapController::clientsSoftap(char **retbuf) |
| 389 | { |
| 390 | int ret; |
| 391 | |
| 392 | if (mSock < 0) { |
| 393 | LOGE("Softap clients - failed to open socket"); |
| 394 | return -1; |
| 395 | } |
| 396 | *mBuf = 0; |
| 397 | ret = setCommand(mIface, "AP_GET_STA_LIST", SOFTAP_MAX_BUFFER_SIZE); |
| 398 | if (ret) { |
| 399 | LOGE("Softap clients - failed: %d", ret); |
| 400 | } else { |
| 401 | asprintf(retbuf, "Softap clients:%s", mBuf); |
| 402 | LOGD("Softap clients:%s", mBuf); |
| 403 | } |
| 404 | return ret; |
| 405 | } |