Roshan Pius | cc81756 | 2017-12-22 14:45:05 -0800 | [diff] [blame] | 1 | /* |
| 2 | * hidl interface for wpa_hostapd daemon |
| 3 | * Copyright (c) 2004-2018, Jouni Malinen <j@w1.fi> |
| 4 | * Copyright (c) 2004-2018, Roshan Pius <rpius@google.com> |
| 5 | * |
| 6 | * This software may be distributed under the terms of the BSD license. |
| 7 | * See README for more details. |
| 8 | */ |
Roshan Pius | 30b452e | 2017-12-27 13:36:21 -0800 | [diff] [blame] | 9 | #include <iomanip> |
| 10 | #include <sstream> |
| 11 | #include <string> |
| 12 | #include <vector> |
| 13 | |
| 14 | #include <android-base/file.h> |
| 15 | #include <android-base/stringprintf.h> |
Roshan Pius | cc81756 | 2017-12-22 14:45:05 -0800 | [diff] [blame] | 16 | |
| 17 | #include "hostapd.h" |
| 18 | #include "hidl_return_util.h" |
| 19 | |
Roshan Pius | 3455af4 | 2018-02-01 09:19:49 -0800 | [diff] [blame] | 20 | extern "C" |
| 21 | { |
| 22 | #include "utils/eloop.h" |
| 23 | } |
| 24 | |
Roshan Pius | 30b452e | 2017-12-27 13:36:21 -0800 | [diff] [blame] | 25 | // The HIDL implementation for hostapd creates a hostapd.conf dynamically for |
| 26 | // each interface. This file can then be used to hook onto the normal config |
| 27 | // file parsing logic in hostapd code. Helps us to avoid duplication of code |
| 28 | // in the HIDL interface. |
| 29 | // TOOD(b/71872409): Add unit tests for this. |
| 30 | namespace { |
| 31 | constexpr char kConfFileNameFmt[] = "/data/vendor/wifi/hostapd/hostapd_%s.conf"; |
| 32 | |
| 33 | using android::base::RemoveFileIfExists; |
| 34 | using android::base::StringPrintf; |
| 35 | using android::base::WriteStringToFile; |
lesl | 1a842e6 | 2019-12-02 19:00:37 +0800 | [diff] [blame] | 36 | using android::hardware::wifi::hostapd::V1_2::IHostapd; |
Roshan Pius | 30b452e | 2017-12-27 13:36:21 -0800 | [diff] [blame] | 37 | |
| 38 | std::string WriteHostapdConfig( |
| 39 | const std::string& interface_name, const std::string& config) |
| 40 | { |
| 41 | const std::string file_path = |
| 42 | StringPrintf(kConfFileNameFmt, interface_name.c_str()); |
| 43 | if (WriteStringToFile( |
| 44 | config, file_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP, |
| 45 | getuid(), getgid())) { |
| 46 | return file_path; |
| 47 | } |
| 48 | // Diagnose failure |
| 49 | int error = errno; |
| 50 | wpa_printf( |
| 51 | MSG_ERROR, "Cannot write hostapd config to %s, error: %s", |
| 52 | file_path.c_str(), strerror(error)); |
| 53 | struct stat st; |
| 54 | int result = stat(file_path.c_str(), &st); |
| 55 | if (result == 0) { |
| 56 | wpa_printf( |
| 57 | MSG_ERROR, "hostapd config file uid: %d, gid: %d, mode: %d", |
| 58 | st.st_uid, st.st_gid, st.st_mode); |
| 59 | } else { |
| 60 | wpa_printf( |
| 61 | MSG_ERROR, |
| 62 | "Error calling stat() on hostapd config file: %s", |
| 63 | strerror(errno)); |
| 64 | } |
| 65 | return ""; |
| 66 | } |
| 67 | |
Ahmed ElArabawy | 7b4b40a | 2020-01-02 08:55:38 -0800 | [diff] [blame] | 68 | /* |
| 69 | * Get the op_class for a channel/band |
| 70 | * The logic here is based on Table E-4 in the 802.11 Specification |
| 71 | */ |
| 72 | int getOpClassForChannel(int channel, int band, bool support11n, bool support11ac) { |
| 73 | // 2GHz Band |
| 74 | if ((band & IHostapd::BandMask::BAND_2_GHZ) != 0) { |
| 75 | if (channel == 14) { |
| 76 | return 82; |
| 77 | } |
| 78 | if (channel >= 1 && channel <= 13) { |
| 79 | if (!support11n) { |
| 80 | //20MHz channel |
| 81 | return 81; |
| 82 | } |
| 83 | if (channel <= 9) { |
| 84 | // HT40 with secondary channel above primary |
| 85 | return 83; |
| 86 | } |
| 87 | // HT40 with secondary channel below primary |
| 88 | return 84; |
| 89 | } |
| 90 | // Error |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | // 5GHz Band |
| 95 | if ((band & IHostapd::BandMask::BAND_5_GHZ) != 0) { |
| 96 | if (support11ac) { |
| 97 | switch (channel) { |
| 98 | case 42: |
| 99 | case 58: |
| 100 | case 106: |
| 101 | case 122: |
| 102 | case 138: |
| 103 | case 155: |
| 104 | // 80MHz channel |
| 105 | return 128; |
| 106 | case 50: |
| 107 | case 114: |
| 108 | // 160MHz channel |
| 109 | return 129; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | if (!support11n) { |
| 114 | if (channel >= 36 && channel <= 48) { |
| 115 | return 115; |
| 116 | } |
| 117 | if (channel >= 52 && channel <= 64) { |
| 118 | return 118; |
| 119 | } |
| 120 | if (channel >= 100 && channel <= 144) { |
| 121 | return 121; |
| 122 | } |
| 123 | if (channel >= 149 && channel <= 161) { |
| 124 | return 124; |
| 125 | } |
| 126 | if (channel >= 165 && channel <= 169) { |
| 127 | return 125; |
| 128 | } |
| 129 | } else { |
| 130 | switch (channel) { |
| 131 | case 36: |
| 132 | case 44: |
| 133 | // HT40 with secondary channel above primary |
| 134 | return 116; |
| 135 | case 40: |
| 136 | case 48: |
| 137 | // HT40 with secondary channel below primary |
| 138 | return 117; |
| 139 | case 52: |
| 140 | case 60: |
| 141 | // HT40 with secondary channel above primary |
| 142 | return 119; |
| 143 | case 56: |
| 144 | case 64: |
| 145 | // HT40 with secondary channel below primary |
| 146 | return 120; |
| 147 | case 100: |
| 148 | case 108: |
| 149 | case 116: |
| 150 | case 124: |
| 151 | case 132: |
| 152 | case 140: |
| 153 | // HT40 with secondary channel above primary |
| 154 | return 122; |
| 155 | case 104: |
| 156 | case 112: |
| 157 | case 120: |
| 158 | case 128: |
| 159 | case 136: |
| 160 | case 144: |
| 161 | // HT40 with secondary channel below primary |
| 162 | return 123; |
| 163 | case 149: |
| 164 | case 157: |
| 165 | // HT40 with secondary channel above primary |
| 166 | return 126; |
| 167 | case 153: |
| 168 | case 161: |
| 169 | // HT40 with secondary channel below primary |
| 170 | return 127; |
| 171 | } |
| 172 | } |
| 173 | // Error |
| 174 | return 0; |
| 175 | } |
| 176 | |
| 177 | // 6GHz Band |
| 178 | if ((band & IHostapd::BandMask::BAND_6_GHZ) != 0) { |
| 179 | // Channels 1, 5. 9, 13, ... |
| 180 | if ((channel & 0x03) == 0x01) { |
| 181 | // 20MHz channel |
| 182 | return 131; |
| 183 | } |
| 184 | // Channels 3, 11, 19, 27, ... |
| 185 | if ((channel & 0x07) == 0x03) { |
| 186 | // 40MHz channel |
| 187 | return 132; |
| 188 | } |
| 189 | // Channels 7, 23, 39, 55, ... |
| 190 | if ((channel & 0x0F) == 0x07) { |
| 191 | // 80MHz channel |
| 192 | return 133; |
| 193 | } |
| 194 | // Channels 15, 47, 69, ... |
| 195 | if ((channel & 0x1F) == 0x0F) { |
| 196 | // 160MHz channel |
| 197 | return 134; |
| 198 | } |
| 199 | // Error |
| 200 | return 0; |
| 201 | } |
| 202 | |
| 203 | return 0; |
| 204 | } |
| 205 | |
lesl | dd48efd | 2019-12-26 15:13:51 +0800 | [diff] [blame] | 206 | bool validatePassphrase(int passphrase_len, int min_len, int max_len) |
| 207 | { |
| 208 | if (min_len != -1 && passphrase_len < min_len) return false; |
| 209 | if (max_len != -1 && passphrase_len > max_len) return false; |
| 210 | return true; |
| 211 | } |
| 212 | |
Roshan Pius | 30b452e | 2017-12-27 13:36:21 -0800 | [diff] [blame] | 213 | std::string CreateHostapdConfig( |
| 214 | const IHostapd::IfaceParams& iface_params, |
| 215 | const IHostapd::NetworkParams& nw_params) |
| 216 | { |
lesl | dd48efd | 2019-12-26 15:13:51 +0800 | [diff] [blame] | 217 | if (nw_params.V1_0.ssid.size() > |
Roshan Pius | 30b452e | 2017-12-27 13:36:21 -0800 | [diff] [blame] | 218 | static_cast<uint32_t>( |
| 219 | IHostapd::ParamSizeLimits::SSID_MAX_LEN_IN_BYTES)) { |
| 220 | wpa_printf( |
lesl | dd48efd | 2019-12-26 15:13:51 +0800 | [diff] [blame] | 221 | MSG_ERROR, "Invalid SSID size: %zu", nw_params.V1_0.ssid.size()); |
Roshan Pius | 30b452e | 2017-12-27 13:36:21 -0800 | [diff] [blame] | 222 | return ""; |
| 223 | } |
| 224 | |
| 225 | // SSID string |
| 226 | std::stringstream ss; |
| 227 | ss << std::hex; |
| 228 | ss << std::setfill('0'); |
lesl | dd48efd | 2019-12-26 15:13:51 +0800 | [diff] [blame] | 229 | for (uint8_t b : nw_params.V1_0.ssid) { |
Roshan Pius | 30b452e | 2017-12-27 13:36:21 -0800 | [diff] [blame] | 230 | ss << std::setw(2) << static_cast<unsigned int>(b); |
| 231 | } |
| 232 | const std::string ssid_as_string = ss.str(); |
| 233 | |
| 234 | // Encryption config string |
| 235 | std::string encryption_config_as_string; |
| 236 | switch (nw_params.encryptionType) { |
| 237 | case IHostapd::EncryptionType::NONE: |
| 238 | // no security params |
| 239 | break; |
| 240 | case IHostapd::EncryptionType::WPA: |
lesl | dd48efd | 2019-12-26 15:13:51 +0800 | [diff] [blame] | 241 | if (!validatePassphrase( |
| 242 | nw_params.passphrase.size(), |
| 243 | static_cast<uint32_t>(IHostapd::ParamSizeLimits:: |
| 244 | WPA2_PSK_PASSPHRASE_MIN_LEN_IN_BYTES), |
| 245 | static_cast<uint32_t>(IHostapd::ParamSizeLimits:: |
| 246 | WPA2_PSK_PASSPHRASE_MAX_LEN_IN_BYTES))) { |
| 247 | return ""; |
| 248 | } |
Roshan Pius | 30b452e | 2017-12-27 13:36:21 -0800 | [diff] [blame] | 249 | encryption_config_as_string = StringPrintf( |
| 250 | "wpa=3\n" |
| 251 | "wpa_pairwise=TKIP CCMP\n" |
| 252 | "wpa_passphrase=%s", |
lesl | dd48efd | 2019-12-26 15:13:51 +0800 | [diff] [blame] | 253 | nw_params.passphrase.c_str()); |
Roshan Pius | 30b452e | 2017-12-27 13:36:21 -0800 | [diff] [blame] | 254 | break; |
| 255 | case IHostapd::EncryptionType::WPA2: |
lesl | dd48efd | 2019-12-26 15:13:51 +0800 | [diff] [blame] | 256 | if (!validatePassphrase( |
| 257 | nw_params.passphrase.size(), |
| 258 | static_cast<uint32_t>(IHostapd::ParamSizeLimits:: |
| 259 | WPA2_PSK_PASSPHRASE_MIN_LEN_IN_BYTES), |
| 260 | static_cast<uint32_t>(IHostapd::ParamSizeLimits:: |
| 261 | WPA2_PSK_PASSPHRASE_MAX_LEN_IN_BYTES))) { |
| 262 | return ""; |
| 263 | } |
Roshan Pius | 30b452e | 2017-12-27 13:36:21 -0800 | [diff] [blame] | 264 | encryption_config_as_string = StringPrintf( |
| 265 | "wpa=2\n" |
| 266 | "rsn_pairwise=CCMP\n" |
| 267 | "wpa_passphrase=%s", |
lesl | dd48efd | 2019-12-26 15:13:51 +0800 | [diff] [blame] | 268 | nw_params.passphrase.c_str()); |
| 269 | break; |
| 270 | case IHostapd::EncryptionType::WPA3_SAE_TRANSITION: |
| 271 | if (!validatePassphrase( |
| 272 | nw_params.passphrase.size(), |
| 273 | static_cast<uint32_t>(IHostapd::ParamSizeLimits:: |
| 274 | WPA2_PSK_PASSPHRASE_MIN_LEN_IN_BYTES), |
| 275 | static_cast<uint32_t>(IHostapd::ParamSizeLimits:: |
| 276 | WPA2_PSK_PASSPHRASE_MAX_LEN_IN_BYTES))) { |
| 277 | return ""; |
| 278 | } |
| 279 | encryption_config_as_string = StringPrintf( |
| 280 | "wpa=2\n" |
| 281 | "rsn_pairwise=CCMP\n" |
| 282 | "wpa_key_mgmt=WPA-PSK SAE\n" |
| 283 | "ieee80211w=1\n" |
| 284 | "sae_require_mfp=1\n" |
| 285 | "wpa_passphrase=%s\n" |
| 286 | "sae_password=%s", |
| 287 | nw_params.passphrase.c_str(), |
| 288 | nw_params.passphrase.c_str()); |
| 289 | break; |
| 290 | case IHostapd::EncryptionType::WPA3_SAE: |
| 291 | if (!validatePassphrase(nw_params.passphrase.size(), 1, -1)) { |
| 292 | return ""; |
| 293 | } |
| 294 | encryption_config_as_string = StringPrintf( |
| 295 | "wpa=2\n" |
| 296 | "rsn_pairwise=CCMP\n" |
| 297 | "wpa_key_mgmt=SAE\n" |
| 298 | "ieee80211w=2\n" |
| 299 | "sae_require_mfp=2\n" |
| 300 | "sae_password=%s", |
| 301 | nw_params.passphrase.c_str()); |
Roshan Pius | 30b452e | 2017-12-27 13:36:21 -0800 | [diff] [blame] | 302 | break; |
| 303 | default: |
| 304 | wpa_printf(MSG_ERROR, "Unknown encryption type"); |
| 305 | return ""; |
| 306 | } |
| 307 | |
Ahmed ElArabawy | 7b4b40a | 2020-01-02 08:55:38 -0800 | [diff] [blame] | 308 | unsigned int band = 0; |
| 309 | band |= iface_params.channelParams.bandMask; |
| 310 | |
Roshan Pius | 30b452e | 2017-12-27 13:36:21 -0800 | [diff] [blame] | 311 | std::string channel_config_as_string; |
Ahmed ElArabawy | a390f29 | 2019-12-21 13:07:02 -0800 | [diff] [blame] | 312 | bool isFirst = true; |
Ahmed ElArabawy | 3498267 | 2019-12-06 10:10:17 -0800 | [diff] [blame] | 313 | if (iface_params.V1_1.V1_0.channelParams.enableAcs) { |
Ahmed ElArabawy | a390f29 | 2019-12-21 13:07:02 -0800 | [diff] [blame] | 314 | std::string freqList_as_string; |
Daisuke Niwa | c1bd20f | 2019-01-09 13:51:02 +0900 | [diff] [blame] | 315 | for (const auto &range : |
Ahmed ElArabawy | a390f29 | 2019-12-21 13:07:02 -0800 | [diff] [blame] | 316 | iface_params.channelParams.acsChannelFreqRangesMhz) { |
| 317 | if (!isFirst) { |
| 318 | freqList_as_string += ","; |
| 319 | } |
| 320 | isFirst = false; |
| 321 | |
Daisuke Niwa | c1bd20f | 2019-01-09 13:51:02 +0900 | [diff] [blame] | 322 | if (range.start != range.end) { |
Ahmed ElArabawy | a390f29 | 2019-12-21 13:07:02 -0800 | [diff] [blame] | 323 | freqList_as_string += |
| 324 | StringPrintf("%d-%d", range.start, range.end); |
Daisuke Niwa | c1bd20f | 2019-01-09 13:51:02 +0900 | [diff] [blame] | 325 | } else { |
Ahmed ElArabawy | a390f29 | 2019-12-21 13:07:02 -0800 | [diff] [blame] | 326 | freqList_as_string += StringPrintf("%d", range.start); |
Daisuke Niwa | c1bd20f | 2019-01-09 13:51:02 +0900 | [diff] [blame] | 327 | } |
| 328 | } |
Roshan Pius | 30b452e | 2017-12-27 13:36:21 -0800 | [diff] [blame] | 329 | channel_config_as_string = StringPrintf( |
| 330 | "channel=0\n" |
Daisuke Niwa | c1bd20f | 2019-01-09 13:51:02 +0900 | [diff] [blame] | 331 | "acs_exclude_dfs=%d\n" |
Ahmed ElArabawy | a390f29 | 2019-12-21 13:07:02 -0800 | [diff] [blame] | 332 | "freqlist=%s", |
Ahmed ElArabawy | 3498267 | 2019-12-06 10:10:17 -0800 | [diff] [blame] | 333 | iface_params.V1_1.V1_0.channelParams.acsShouldExcludeDfs, |
Ahmed ElArabawy | a390f29 | 2019-12-21 13:07:02 -0800 | [diff] [blame] | 334 | freqList_as_string.c_str()); |
Roshan Pius | 30b452e | 2017-12-27 13:36:21 -0800 | [diff] [blame] | 335 | } else { |
Ahmed ElArabawy | 7b4b40a | 2020-01-02 08:55:38 -0800 | [diff] [blame] | 336 | int op_class = getOpClassForChannel( |
| 337 | iface_params.V1_1.V1_0.channelParams.channel, |
| 338 | band, |
| 339 | iface_params.V1_1.V1_0.hwModeParams.enable80211N, |
| 340 | iface_params.V1_1.V1_0.hwModeParams.enable80211AC); |
Roshan Pius | 30b452e | 2017-12-27 13:36:21 -0800 | [diff] [blame] | 341 | channel_config_as_string = StringPrintf( |
Ahmed ElArabawy | 7b4b40a | 2020-01-02 08:55:38 -0800 | [diff] [blame] | 342 | "channel=%d\n" |
| 343 | "op_class=%d", |
| 344 | iface_params.V1_1.V1_0.channelParams.channel, op_class); |
Roshan Pius | 30b452e | 2017-12-27 13:36:21 -0800 | [diff] [blame] | 345 | } |
| 346 | |
Roshan Pius | 30b452e | 2017-12-27 13:36:21 -0800 | [diff] [blame] | 347 | std::string hw_mode_as_string; |
Roshan Pius | 4944647 | 2018-03-07 10:23:46 -0800 | [diff] [blame] | 348 | std::string ht_cap_vht_oper_chwidth_as_string; |
Ahmed ElArabawy | 3498267 | 2019-12-06 10:10:17 -0800 | [diff] [blame] | 349 | |
| 350 | if ((band & IHostapd::BandMask::BAND_2_GHZ) != 0) { |
Ahmed ElArabawy | a390f29 | 2019-12-21 13:07:02 -0800 | [diff] [blame] | 351 | if (((band & IHostapd::BandMask::BAND_5_GHZ) != 0) |
| 352 | || ((band & IHostapd::BandMask::BAND_6_GHZ) != 0)) { |
Ahmed ElArabawy | 3498267 | 2019-12-06 10:10:17 -0800 | [diff] [blame] | 353 | hw_mode_as_string = "hw_mode=any"; |
| 354 | if (iface_params.V1_1.V1_0.channelParams.enableAcs) { |
| 355 | ht_cap_vht_oper_chwidth_as_string = |
| 356 | "ht_capab=[HT40+]\n" |
| 357 | "vht_oper_chwidth=1"; |
| 358 | } |
| 359 | } else { |
| 360 | hw_mode_as_string = "hw_mode=g"; |
Roshan Pius | 4944647 | 2018-03-07 10:23:46 -0800 | [diff] [blame] | 361 | } |
Ahmed ElArabawy | 3498267 | 2019-12-06 10:10:17 -0800 | [diff] [blame] | 362 | } else { |
Ahmed ElArabawy | a390f29 | 2019-12-21 13:07:02 -0800 | [diff] [blame] | 363 | if (((band & IHostapd::BandMask::BAND_5_GHZ) != 0) |
| 364 | || ((band & IHostapd::BandMask::BAND_6_GHZ) != 0)) { |
Ahmed ElArabawy | 3498267 | 2019-12-06 10:10:17 -0800 | [diff] [blame] | 365 | hw_mode_as_string = "hw_mode=a"; |
| 366 | if (iface_params.V1_1.V1_0.channelParams.enableAcs) { |
| 367 | ht_cap_vht_oper_chwidth_as_string = |
| 368 | "ht_capab=[HT40+]\n" |
| 369 | "vht_oper_chwidth=1"; |
| 370 | } |
| 371 | } else { |
| 372 | wpa_printf(MSG_ERROR, "Invalid band"); |
| 373 | return ""; |
Roshan Pius | 4944647 | 2018-03-07 10:23:46 -0800 | [diff] [blame] | 374 | } |
Roshan Pius | 30b452e | 2017-12-27 13:36:21 -0800 | [diff] [blame] | 375 | } |
| 376 | |
Ahmed ElArabawy | 5362fb1 | 2020-01-02 15:01:31 -0800 | [diff] [blame] | 377 | std::string he_params_as_string; |
Roshan Pius | 3651030 | 2020-05-09 21:01:01 -0700 | [diff] [blame] | 378 | #ifdef CONFIG_IEEE80211AX |
Ahmed ElArabawy | 5362fb1 | 2020-01-02 15:01:31 -0800 | [diff] [blame] | 379 | if (iface_params.hwModeParams.enable80211AX) { |
| 380 | he_params_as_string = StringPrintf( |
| 381 | "ieee80211ax=1\n" |
| 382 | "he_su_beamformer=%d\n" |
| 383 | "he_su_beamformee=%d\n" |
| 384 | "he_mu_beamformer=%d\n" |
Ahmed ElArabawy | 5362fb1 | 2020-01-02 15:01:31 -0800 | [diff] [blame] | 385 | "he_twt_required=%d\n", |
| 386 | iface_params.hwModeParams.enableHeSingleUserBeamformer ? 1 : 0, |
| 387 | iface_params.hwModeParams.enableHeSingleUserBeamformee ? 1 : 0, |
| 388 | iface_params.hwModeParams.enableHeMultiUserBeamformer ? 1 : 0, |
Ahmed ElArabawy | 5362fb1 | 2020-01-02 15:01:31 -0800 | [diff] [blame] | 389 | iface_params.hwModeParams.enableHeTargetWakeTime ? 1 : 0); |
| 390 | } else { |
| 391 | he_params_as_string = "ieee80211ax=0"; |
| 392 | } |
Roshan Pius | 3651030 | 2020-05-09 21:01:01 -0700 | [diff] [blame] | 393 | #endif /* CONFIG_IEEE80211AX */ |
Ahmed ElArabawy | 5362fb1 | 2020-01-02 15:01:31 -0800 | [diff] [blame] | 394 | |
Roshan Pius | 30b452e | 2017-12-27 13:36:21 -0800 | [diff] [blame] | 395 | return StringPrintf( |
| 396 | "interface=%s\n" |
| 397 | "driver=nl80211\n" |
Daichi Ueura | caa74a8 | 2018-02-14 22:25:39 +0900 | [diff] [blame] | 398 | "ctrl_interface=/data/vendor/wifi/hostapd/ctrl\n" |
Roshan Pius | 30b452e | 2017-12-27 13:36:21 -0800 | [diff] [blame] | 399 | // ssid2 signals to hostapd that the value is not a literal value |
| 400 | // for use as a SSID. In this case, we're giving it a hex |
| 401 | // std::string and hostapd needs to expect that. |
| 402 | "ssid2=%s\n" |
| 403 | "%s\n" |
| 404 | "ieee80211n=%d\n" |
| 405 | "ieee80211ac=%d\n" |
Ahmed ElArabawy | 5362fb1 | 2020-01-02 15:01:31 -0800 | [diff] [blame] | 406 | "%s\n" |
Roshan Pius | e453f71 | 2018-02-09 15:49:57 -0800 | [diff] [blame] | 407 | "%s\n" |
Roshan Pius | 4944647 | 2018-03-07 10:23:46 -0800 | [diff] [blame] | 408 | "%s\n" |
Roshan Pius | 30b452e | 2017-12-27 13:36:21 -0800 | [diff] [blame] | 409 | "ignore_broadcast_ssid=%d\n" |
| 410 | "wowlan_triggers=any\n" |
| 411 | "%s\n", |
Ahmed ElArabawy | 3498267 | 2019-12-06 10:10:17 -0800 | [diff] [blame] | 412 | iface_params.V1_1.V1_0.ifaceName.c_str(), ssid_as_string.c_str(), |
Roshan Pius | 30b452e | 2017-12-27 13:36:21 -0800 | [diff] [blame] | 413 | channel_config_as_string.c_str(), |
Ahmed ElArabawy | 3498267 | 2019-12-06 10:10:17 -0800 | [diff] [blame] | 414 | iface_params.V1_1.V1_0.hwModeParams.enable80211N ? 1 : 0, |
| 415 | iface_params.V1_1.V1_0.hwModeParams.enable80211AC ? 1 : 0, |
Ahmed ElArabawy | 5362fb1 | 2020-01-02 15:01:31 -0800 | [diff] [blame] | 416 | he_params_as_string.c_str(), |
Roshan Pius | 4944647 | 2018-03-07 10:23:46 -0800 | [diff] [blame] | 417 | hw_mode_as_string.c_str(), ht_cap_vht_oper_chwidth_as_string.c_str(), |
lesl | dd48efd | 2019-12-26 15:13:51 +0800 | [diff] [blame] | 418 | nw_params.V1_0.isHidden ? 1 : 0, encryption_config_as_string.c_str()); |
Roshan Pius | 30b452e | 2017-12-27 13:36:21 -0800 | [diff] [blame] | 419 | } |
Roshan Pius | e2d2101 | 2018-08-17 11:22:06 -0700 | [diff] [blame] | 420 | |
| 421 | // hostapd core functions accept "C" style function pointers, so use global |
| 422 | // functions to pass to the hostapd core function and store the corresponding |
| 423 | // std::function methods to be invoked. |
| 424 | // |
| 425 | // NOTE: Using the pattern from the vendor HAL (wifi_legacy_hal.cpp). |
| 426 | // |
| 427 | // Callback to be invoked once setup is complete |
| 428 | std::function<void(struct hostapd_data*)> on_setup_complete_internal_callback; |
| 429 | void onAsyncSetupCompleteCb(void* ctx) |
| 430 | { |
| 431 | struct hostapd_data* iface_hapd = (struct hostapd_data*)ctx; |
| 432 | if (on_setup_complete_internal_callback) { |
| 433 | on_setup_complete_internal_callback(iface_hapd); |
| 434 | // Invalidate this callback since we don't want this firing |
| 435 | // again. |
| 436 | on_setup_complete_internal_callback = nullptr; |
| 437 | } |
| 438 | } |
Roshan Pius | 30b452e | 2017-12-27 13:36:21 -0800 | [diff] [blame] | 439 | } // namespace |
| 440 | |
Roshan Pius | cc81756 | 2017-12-22 14:45:05 -0800 | [diff] [blame] | 441 | namespace android { |
| 442 | namespace hardware { |
| 443 | namespace wifi { |
| 444 | namespace hostapd { |
lesl | f8b1b40 | 2020-08-04 17:09:39 +0800 | [diff] [blame^] | 445 | namespace V1_3 { |
Roshan Pius | cc81756 | 2017-12-22 14:45:05 -0800 | [diff] [blame] | 446 | namespace implementation { |
| 447 | using hidl_return_util::call; |
Roshan Pius | e2d2101 | 2018-08-17 11:22:06 -0700 | [diff] [blame] | 448 | using namespace android::hardware::wifi::hostapd::V1_0; |
Roshan Pius | cc81756 | 2017-12-22 14:45:05 -0800 | [diff] [blame] | 449 | |
| 450 | Hostapd::Hostapd(struct hapd_interfaces* interfaces) : interfaces_(interfaces) |
| 451 | {} |
| 452 | |
| 453 | Return<void> Hostapd::addAccessPoint( |
Daisuke Niwa | c1bd20f | 2019-01-09 13:51:02 +0900 | [diff] [blame] | 454 | const V1_0::IHostapd::IfaceParams& iface_params, |
lesl | dd48efd | 2019-12-26 15:13:51 +0800 | [diff] [blame] | 455 | const V1_0::IHostapd::NetworkParams& nw_params, addAccessPoint_cb _hidl_cb) |
Daisuke Niwa | c1bd20f | 2019-01-09 13:51:02 +0900 | [diff] [blame] | 456 | { |
| 457 | return call( |
| 458 | this, &Hostapd::addAccessPointInternal, _hidl_cb, iface_params, |
| 459 | nw_params); |
| 460 | } |
| 461 | |
| 462 | Return<void> Hostapd::addAccessPoint_1_1( |
Ahmed ElArabawy | 3498267 | 2019-12-06 10:10:17 -0800 | [diff] [blame] | 463 | const V1_1::IHostapd::IfaceParams& iface_params, |
lesl | dd48efd | 2019-12-26 15:13:51 +0800 | [diff] [blame] | 464 | const V1_0::IHostapd::NetworkParams& nw_params, addAccessPoint_cb _hidl_cb) |
Roshan Pius | cc81756 | 2017-12-22 14:45:05 -0800 | [diff] [blame] | 465 | { |
| 466 | return call( |
Daisuke Niwa | c1bd20f | 2019-01-09 13:51:02 +0900 | [diff] [blame] | 467 | this, &Hostapd::addAccessPointInternal_1_1, _hidl_cb, iface_params, |
Roshan Pius | cc81756 | 2017-12-22 14:45:05 -0800 | [diff] [blame] | 468 | nw_params); |
| 469 | } |
| 470 | |
Ahmed ElArabawy | 3498267 | 2019-12-06 10:10:17 -0800 | [diff] [blame] | 471 | Return<void> Hostapd::addAccessPoint_1_2( |
| 472 | const IfaceParams& iface_params, const NetworkParams& nw_params, |
| 473 | addAccessPoint_1_2_cb _hidl_cb) |
| 474 | { |
| 475 | return call( |
| 476 | this, &Hostapd::addAccessPointInternal_1_2, _hidl_cb, iface_params, |
| 477 | nw_params); |
| 478 | } |
| 479 | |
Roshan Pius | cc81756 | 2017-12-22 14:45:05 -0800 | [diff] [blame] | 480 | Return<void> Hostapd::removeAccessPoint( |
| 481 | const hidl_string& iface_name, removeAccessPoint_cb _hidl_cb) |
| 482 | { |
| 483 | return call( |
| 484 | this, &Hostapd::removeAccessPointInternal, _hidl_cb, iface_name); |
| 485 | } |
| 486 | |
Roshan Pius | e2d2101 | 2018-08-17 11:22:06 -0700 | [diff] [blame] | 487 | Return<void> Hostapd::terminate() |
| 488 | { |
Roshan Pius | 3455af4 | 2018-02-01 09:19:49 -0800 | [diff] [blame] | 489 | wpa_printf(MSG_INFO, "Terminating..."); |
| 490 | eloop_terminate(); |
| 491 | return Void(); |
| 492 | } |
| 493 | |
Roshan Pius | e2d2101 | 2018-08-17 11:22:06 -0700 | [diff] [blame] | 494 | Return<void> Hostapd::registerCallback( |
lesl | 1a842e6 | 2019-12-02 19:00:37 +0800 | [diff] [blame] | 495 | const sp<V1_1::IHostapdCallback>& callback, registerCallback_cb _hidl_cb) |
Roshan Pius | e2d2101 | 2018-08-17 11:22:06 -0700 | [diff] [blame] | 496 | { |
| 497 | return call( |
| 498 | this, &Hostapd::registerCallbackInternal, _hidl_cb, callback); |
| 499 | } |
| 500 | |
lesl | f8b1b40 | 2020-08-04 17:09:39 +0800 | [diff] [blame^] | 501 | Return<void> Hostapd::registerCallback_1_3( |
| 502 | const sp<V1_3::IHostapdCallback>& callback, registerCallback_1_3_cb _hidl_cb) |
| 503 | { |
| 504 | return call( |
| 505 | this, &Hostapd::registerCallbackInternal_1_3, _hidl_cb, callback); |
| 506 | } |
| 507 | |
lesl | 1a842e6 | 2019-12-02 19:00:37 +0800 | [diff] [blame] | 508 | Return<void> Hostapd::forceClientDisconnect( |
| 509 | const hidl_string& iface_name, const hidl_array<uint8_t, 6>& client_address, |
| 510 | V1_2::Ieee80211ReasonCode reason_code, forceClientDisconnect_cb _hidl_cb) |
| 511 | { |
| 512 | return call( |
| 513 | this, &Hostapd::forceClientDisconnectInternal, _hidl_cb, iface_name, |
| 514 | client_address, reason_code); |
| 515 | } |
| 516 | |
Roger Wang | cede506 | 2019-12-30 12:56:28 +0800 | [diff] [blame] | 517 | Return<void> Hostapd::setDebugParams( |
lesl | f8b1b40 | 2020-08-04 17:09:39 +0800 | [diff] [blame^] | 518 | V1_2::DebugLevel level, setDebugParams_cb _hidl_cb) |
Roger Wang | cede506 | 2019-12-30 12:56:28 +0800 | [diff] [blame] | 519 | { |
| 520 | return call( |
| 521 | this, &Hostapd::setDebugParamsInternal, _hidl_cb, level); |
| 522 | } |
lesl | 1a842e6 | 2019-12-02 19:00:37 +0800 | [diff] [blame] | 523 | |
| 524 | V1_0::HostapdStatus Hostapd::addAccessPointInternal( |
Daisuke Niwa | c1bd20f | 2019-01-09 13:51:02 +0900 | [diff] [blame] | 525 | const V1_0::IHostapd::IfaceParams& iface_params, |
lesl | dd48efd | 2019-12-26 15:13:51 +0800 | [diff] [blame] | 526 | const V1_0::IHostapd::NetworkParams& nw_params) |
Daisuke Niwa | c1bd20f | 2019-01-09 13:51:02 +0900 | [diff] [blame] | 527 | { |
lesl | 1a842e6 | 2019-12-02 19:00:37 +0800 | [diff] [blame] | 528 | return {V1_0::HostapdStatusCode::FAILURE_UNKNOWN, ""}; |
Daisuke Niwa | c1bd20f | 2019-01-09 13:51:02 +0900 | [diff] [blame] | 529 | } |
| 530 | |
lesl | 1a842e6 | 2019-12-02 19:00:37 +0800 | [diff] [blame] | 531 | V1_0::HostapdStatus Hostapd::addAccessPointInternal_1_1( |
Ahmed ElArabawy | 3498267 | 2019-12-06 10:10:17 -0800 | [diff] [blame] | 532 | const V1_1::IHostapd::IfaceParams& iface_params, |
lesl | dd48efd | 2019-12-26 15:13:51 +0800 | [diff] [blame] | 533 | const V1_1::IHostapd::NetworkParams& nw_params) |
Ahmed ElArabawy | 3498267 | 2019-12-06 10:10:17 -0800 | [diff] [blame] | 534 | { |
| 535 | return {V1_0::HostapdStatusCode::FAILURE_UNKNOWN, ""}; |
| 536 | } |
| 537 | |
lesl | f8b1b40 | 2020-08-04 17:09:39 +0800 | [diff] [blame^] | 538 | V1_2::HostapdStatus Hostapd::addAccessPointInternal_1_2( |
Roshan Pius | cc81756 | 2017-12-22 14:45:05 -0800 | [diff] [blame] | 539 | const IfaceParams& iface_params, const NetworkParams& nw_params) |
| 540 | { |
Ahmed ElArabawy | 3498267 | 2019-12-06 10:10:17 -0800 | [diff] [blame] | 541 | if (hostapd_get_iface(interfaces_, iface_params.V1_1.V1_0.ifaceName.c_str())) { |
Roshan Pius | 087d869 | 2017-12-27 14:11:44 -0800 | [diff] [blame] | 542 | wpa_printf( |
| 543 | MSG_ERROR, "Interface %s already present", |
Ahmed ElArabawy | 3498267 | 2019-12-06 10:10:17 -0800 | [diff] [blame] | 544 | iface_params.V1_1.V1_0.ifaceName.c_str()); |
lesl | f8b1b40 | 2020-08-04 17:09:39 +0800 | [diff] [blame^] | 545 | return {V1_2::HostapdStatusCode::FAILURE_IFACE_EXISTS, ""}; |
Roshan Pius | 087d869 | 2017-12-27 14:11:44 -0800 | [diff] [blame] | 546 | } |
| 547 | const auto conf_params = CreateHostapdConfig(iface_params, nw_params); |
| 548 | if (conf_params.empty()) { |
| 549 | wpa_printf(MSG_ERROR, "Failed to create config params"); |
lesl | f8b1b40 | 2020-08-04 17:09:39 +0800 | [diff] [blame^] | 550 | return {V1_2::HostapdStatusCode::FAILURE_ARGS_INVALID, ""}; |
Roshan Pius | 087d869 | 2017-12-27 14:11:44 -0800 | [diff] [blame] | 551 | } |
| 552 | const auto conf_file_path = |
Ahmed ElArabawy | 3498267 | 2019-12-06 10:10:17 -0800 | [diff] [blame] | 553 | WriteHostapdConfig(iface_params.V1_1.V1_0.ifaceName, conf_params); |
Roshan Pius | 087d869 | 2017-12-27 14:11:44 -0800 | [diff] [blame] | 554 | if (conf_file_path.empty()) { |
| 555 | wpa_printf(MSG_ERROR, "Failed to write config file"); |
lesl | f8b1b40 | 2020-08-04 17:09:39 +0800 | [diff] [blame^] | 556 | return {V1_2::HostapdStatusCode::FAILURE_UNKNOWN, ""}; |
Roshan Pius | 087d869 | 2017-12-27 14:11:44 -0800 | [diff] [blame] | 557 | } |
| 558 | std::string add_iface_param_str = StringPrintf( |
Ahmed ElArabawy | 3498267 | 2019-12-06 10:10:17 -0800 | [diff] [blame] | 559 | "%s config=%s", iface_params.V1_1.V1_0.ifaceName.c_str(), |
Roshan Pius | 087d869 | 2017-12-27 14:11:44 -0800 | [diff] [blame] | 560 | conf_file_path.c_str()); |
| 561 | std::vector<char> add_iface_param_vec( |
| 562 | add_iface_param_str.begin(), add_iface_param_str.end() + 1); |
| 563 | if (hostapd_add_iface(interfaces_, add_iface_param_vec.data()) < 0) { |
| 564 | wpa_printf( |
| 565 | MSG_ERROR, "Adding interface %s failed", |
| 566 | add_iface_param_str.c_str()); |
lesl | f8b1b40 | 2020-08-04 17:09:39 +0800 | [diff] [blame^] | 567 | return {V1_2::HostapdStatusCode::FAILURE_UNKNOWN, ""}; |
Roshan Pius | 087d869 | 2017-12-27 14:11:44 -0800 | [diff] [blame] | 568 | } |
| 569 | struct hostapd_data* iface_hapd = |
Ahmed ElArabawy | 3498267 | 2019-12-06 10:10:17 -0800 | [diff] [blame] | 570 | hostapd_get_iface(interfaces_, iface_params.V1_1.V1_0.ifaceName.c_str()); |
Roshan Pius | 087d869 | 2017-12-27 14:11:44 -0800 | [diff] [blame] | 571 | WPA_ASSERT(iface_hapd != nullptr && iface_hapd->iface != nullptr); |
Roshan Pius | e2d2101 | 2018-08-17 11:22:06 -0700 | [diff] [blame] | 572 | // Register the setup complete callbacks |
| 573 | on_setup_complete_internal_callback = |
| 574 | [this](struct hostapd_data* iface_hapd) { |
| 575 | wpa_printf( |
| 576 | MSG_DEBUG, "AP interface setup completed - state %s", |
| 577 | hostapd_state_text(iface_hapd->iface->state)); |
| 578 | if (iface_hapd->iface->state == HAPD_IFACE_DISABLED) { |
| 579 | // Invoke the failure callback on all registered |
| 580 | // clients. |
| 581 | for (const auto& callback : callbacks_) { |
| 582 | callback->onFailure( |
| 583 | iface_hapd->conf->iface); |
| 584 | } |
| 585 | } |
| 586 | }; |
| 587 | iface_hapd->setup_complete_cb = onAsyncSetupCompleteCb; |
| 588 | iface_hapd->setup_complete_cb_ctx = iface_hapd; |
Roshan Pius | 087d869 | 2017-12-27 14:11:44 -0800 | [diff] [blame] | 589 | if (hostapd_enable_iface(iface_hapd->iface) < 0) { |
| 590 | wpa_printf( |
| 591 | MSG_ERROR, "Enabling interface %s failed", |
Ahmed ElArabawy | 3498267 | 2019-12-06 10:10:17 -0800 | [diff] [blame] | 592 | iface_params.V1_1.V1_0.ifaceName.c_str()); |
lesl | f8b1b40 | 2020-08-04 17:09:39 +0800 | [diff] [blame^] | 593 | return {V1_2::HostapdStatusCode::FAILURE_UNKNOWN, ""}; |
Roshan Pius | 087d869 | 2017-12-27 14:11:44 -0800 | [diff] [blame] | 594 | } |
lesl | f8b1b40 | 2020-08-04 17:09:39 +0800 | [diff] [blame^] | 595 | return {V1_2::HostapdStatusCode::SUCCESS, ""}; |
Roshan Pius | cc81756 | 2017-12-22 14:45:05 -0800 | [diff] [blame] | 596 | } |
| 597 | |
lesl | 1a842e6 | 2019-12-02 19:00:37 +0800 | [diff] [blame] | 598 | V1_0::HostapdStatus Hostapd::removeAccessPointInternal(const std::string& iface_name) |
Roshan Pius | cc81756 | 2017-12-22 14:45:05 -0800 | [diff] [blame] | 599 | { |
Roshan Pius | 087d869 | 2017-12-27 14:11:44 -0800 | [diff] [blame] | 600 | std::vector<char> remove_iface_param_vec( |
| 601 | iface_name.begin(), iface_name.end() + 1); |
| 602 | if (hostapd_remove_iface(interfaces_, remove_iface_param_vec.data()) < |
| 603 | 0) { |
| 604 | wpa_printf( |
| 605 | MSG_ERROR, "Removing interface %s failed", |
| 606 | iface_name.c_str()); |
lesl | 1a842e6 | 2019-12-02 19:00:37 +0800 | [diff] [blame] | 607 | return {V1_0::HostapdStatusCode::FAILURE_UNKNOWN, ""}; |
Roshan Pius | 087d869 | 2017-12-27 14:11:44 -0800 | [diff] [blame] | 608 | } |
lesl | 1a842e6 | 2019-12-02 19:00:37 +0800 | [diff] [blame] | 609 | return {V1_0::HostapdStatusCode::SUCCESS, ""}; |
Roshan Pius | cc81756 | 2017-12-22 14:45:05 -0800 | [diff] [blame] | 610 | } |
Roshan Pius | e2d2101 | 2018-08-17 11:22:06 -0700 | [diff] [blame] | 611 | |
lesl | 1a842e6 | 2019-12-02 19:00:37 +0800 | [diff] [blame] | 612 | V1_0::HostapdStatus Hostapd::registerCallbackInternal( |
| 613 | const sp<V1_1::IHostapdCallback>& callback) |
Roshan Pius | e2d2101 | 2018-08-17 11:22:06 -0700 | [diff] [blame] | 614 | { |
lesl | f8b1b40 | 2020-08-04 17:09:39 +0800 | [diff] [blame^] | 615 | return {V1_0::HostapdStatusCode::FAILURE_UNKNOWN, ""}; |
| 616 | } |
| 617 | |
| 618 | V1_2::HostapdStatus Hostapd::registerCallbackInternal_1_3( |
| 619 | const sp<V1_3::IHostapdCallback>& callback) |
| 620 | { |
Roshan Pius | e2d2101 | 2018-08-17 11:22:06 -0700 | [diff] [blame] | 621 | callbacks_.push_back(callback); |
lesl | f8b1b40 | 2020-08-04 17:09:39 +0800 | [diff] [blame^] | 622 | return {V1_2::HostapdStatusCode::SUCCESS, ""}; |
lesl | 1a842e6 | 2019-12-02 19:00:37 +0800 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | V1_2::HostapdStatus Hostapd::forceClientDisconnectInternal(const std::string& iface_name, |
| 626 | const std::array<uint8_t, 6>& client_address, V1_2::Ieee80211ReasonCode reason_code) |
| 627 | { |
| 628 | struct hostapd_data *hapd = hostapd_get_iface(interfaces_, iface_name.c_str()); |
| 629 | struct sta_info *sta; |
| 630 | if (!hapd) { |
| 631 | wpa_printf(MSG_ERROR, "Interface %s doesn't exist", iface_name.c_str()); |
| 632 | return {V1_2::HostapdStatusCode::FAILURE_IFACE_UNKNOWN, ""}; |
| 633 | } |
| 634 | for (sta = hapd->sta_list; sta; sta = sta->next) { |
| 635 | int res; |
| 636 | res = memcmp(sta->addr, client_address.data(), ETH_ALEN); |
| 637 | if (res == 0) { |
| 638 | wpa_printf(MSG_INFO, "Force client:" MACSTR " disconnect with reason: %d", |
| 639 | MAC2STR(client_address.data()), (uint16_t) reason_code); |
| 640 | ap_sta_disconnect(hapd, sta, sta->addr, (uint16_t) reason_code); |
| 641 | return {V1_2::HostapdStatusCode::SUCCESS, ""}; |
| 642 | } |
| 643 | } |
| 644 | return {V1_2::HostapdStatusCode::FAILURE_CLIENT_UNKNOWN, ""}; |
Roshan Pius | e2d2101 | 2018-08-17 11:22:06 -0700 | [diff] [blame] | 645 | } |
| 646 | |
lesl | f8b1b40 | 2020-08-04 17:09:39 +0800 | [diff] [blame^] | 647 | V1_2::HostapdStatus Hostapd::setDebugParamsInternal(V1_2::DebugLevel level) |
Roger Wang | cede506 | 2019-12-30 12:56:28 +0800 | [diff] [blame] | 648 | { |
| 649 | wpa_debug_level = static_cast<uint32_t>(level); |
| 650 | return {V1_2::HostapdStatusCode::SUCCESS, ""}; |
| 651 | } |
| 652 | |
Roshan Pius | cc81756 | 2017-12-22 14:45:05 -0800 | [diff] [blame] | 653 | } // namespace implementation |
lesl | f8b1b40 | 2020-08-04 17:09:39 +0800 | [diff] [blame^] | 654 | } // namespace V1_3 |
Roshan Pius | cc81756 | 2017-12-22 14:45:05 -0800 | [diff] [blame] | 655 | } // namespace hostapd |
| 656 | } // namespace wifi |
| 657 | } // namespace hardware |
| 658 | } // namespace android |