Gabriel Biren | 72cf9a5 | 2021-06-25 23:29:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * aidl 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 | */ |
| 9 | #include <iomanip> |
| 10 | #include <sstream> |
| 11 | #include <string> |
| 12 | #include <vector> |
| 13 | #include <net/if.h> |
| 14 | #include <sys/socket.h> |
| 15 | #include <linux/if_bridge.h> |
| 16 | |
| 17 | #include <android-base/file.h> |
| 18 | #include <android-base/stringprintf.h> |
| 19 | #include <android-base/unique_fd.h> |
| 20 | |
| 21 | #include "hostapd.h" |
| 22 | #include <aidl/android/hardware/wifi/hostapd/ApInfo.h> |
| 23 | #include <aidl/android/hardware/wifi/hostapd/BandMask.h> |
| 24 | #include <aidl/android/hardware/wifi/hostapd/ChannelParams.h> |
| 25 | #include <aidl/android/hardware/wifi/hostapd/ClientInfo.h> |
| 26 | #include <aidl/android/hardware/wifi/hostapd/EncryptionType.h> |
| 27 | #include <aidl/android/hardware/wifi/hostapd/HostapdStatusCode.h> |
| 28 | #include <aidl/android/hardware/wifi/hostapd/IfaceParams.h> |
| 29 | #include <aidl/android/hardware/wifi/hostapd/NetworkParams.h> |
| 30 | #include <aidl/android/hardware/wifi/hostapd/ParamSizeLimits.h> |
| 31 | |
| 32 | extern "C" |
| 33 | { |
| 34 | #include "common/wpa_ctrl.h" |
| 35 | #include "drivers/linux_ioctl.h" |
| 36 | } |
| 37 | |
| 38 | // The AIDL implementation for hostapd creates a hostapd.conf dynamically for |
| 39 | // each interface. This file can then be used to hook onto the normal config |
| 40 | // file parsing logic in hostapd code. Helps us to avoid duplication of code |
| 41 | // in the AIDL interface. |
| 42 | // TOOD(b/71872409): Add unit tests for this. |
| 43 | namespace { |
| 44 | constexpr char kConfFileNameFmt[] = "/data/vendor/wifi/hostapd/hostapd_%s.conf"; |
| 45 | |
| 46 | using android::base::RemoveFileIfExists; |
| 47 | using android::base::StringPrintf; |
| 48 | using android::base::WriteStringToFile; |
| 49 | using aidl::android::hardware::wifi::hostapd::BandMask; |
Ahmed ElArabawy | b411579 | 2022-02-08 09:33:01 -0800 | [diff] [blame] | 50 | using aidl::android::hardware::wifi::hostapd::ChannelBandwidth; |
Gabriel Biren | 72cf9a5 | 2021-06-25 23:29:26 +0000 | [diff] [blame] | 51 | using aidl::android::hardware::wifi::hostapd::ChannelParams; |
| 52 | using aidl::android::hardware::wifi::hostapd::EncryptionType; |
| 53 | using aidl::android::hardware::wifi::hostapd::Generation; |
| 54 | using aidl::android::hardware::wifi::hostapd::HostapdStatusCode; |
| 55 | using aidl::android::hardware::wifi::hostapd::IfaceParams; |
| 56 | using aidl::android::hardware::wifi::hostapd::NetworkParams; |
| 57 | using aidl::android::hardware::wifi::hostapd::ParamSizeLimits; |
| 58 | |
| 59 | int band2Ghz = (int)BandMask::BAND_2_GHZ; |
| 60 | int band5Ghz = (int)BandMask::BAND_5_GHZ; |
| 61 | int band6Ghz = (int)BandMask::BAND_6_GHZ; |
| 62 | int band60Ghz = (int)BandMask::BAND_60_GHZ; |
| 63 | |
| 64 | #define MAX_PORTS 1024 |
| 65 | bool GetInterfacesInBridge(std::string br_name, |
| 66 | std::vector<std::string>* interfaces) { |
| 67 | android::base::unique_fd sock(socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0)); |
| 68 | if (sock.get() < 0) { |
| 69 | wpa_printf(MSG_ERROR, "Failed to create sock (%s) in %s", |
| 70 | strerror(errno), __FUNCTION__); |
| 71 | return false; |
| 72 | } |
| 73 | |
| 74 | struct ifreq request; |
| 75 | int i, ifindices[MAX_PORTS]; |
| 76 | char if_name[IFNAMSIZ]; |
| 77 | unsigned long args[3]; |
| 78 | |
| 79 | memset(ifindices, 0, MAX_PORTS * sizeof(int)); |
| 80 | |
| 81 | args[0] = BRCTL_GET_PORT_LIST; |
| 82 | args[1] = (unsigned long) ifindices; |
| 83 | args[2] = MAX_PORTS; |
| 84 | |
| 85 | strlcpy(request.ifr_name, br_name.c_str(), IFNAMSIZ); |
| 86 | request.ifr_data = (char *)args; |
| 87 | |
| 88 | if (ioctl(sock.get(), SIOCDEVPRIVATE, &request) < 0) { |
| 89 | wpa_printf(MSG_ERROR, "Failed to ioctl SIOCDEVPRIVATE in %s", |
| 90 | __FUNCTION__); |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | for (i = 0; i < MAX_PORTS; i ++) { |
| 95 | memset(if_name, 0, IFNAMSIZ); |
| 96 | if (ifindices[i] == 0 || !if_indextoname(ifindices[i], if_name)) { |
| 97 | continue; |
| 98 | } |
| 99 | interfaces->push_back(if_name); |
| 100 | } |
| 101 | return true; |
| 102 | } |
| 103 | |
| 104 | std::string WriteHostapdConfig( |
| 105 | const std::string& interface_name, const std::string& config) |
| 106 | { |
| 107 | const std::string file_path = |
| 108 | StringPrintf(kConfFileNameFmt, interface_name.c_str()); |
| 109 | if (WriteStringToFile( |
| 110 | config, file_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP, |
| 111 | getuid(), getgid())) { |
| 112 | return file_path; |
| 113 | } |
| 114 | // Diagnose failure |
| 115 | int error = errno; |
| 116 | wpa_printf( |
| 117 | MSG_ERROR, "Cannot write hostapd config to %s, error: %s", |
| 118 | file_path.c_str(), strerror(error)); |
| 119 | struct stat st; |
| 120 | int result = stat(file_path.c_str(), &st); |
| 121 | if (result == 0) { |
| 122 | wpa_printf( |
| 123 | MSG_ERROR, "hostapd config file uid: %d, gid: %d, mode: %d", |
| 124 | st.st_uid, st.st_gid, st.st_mode); |
| 125 | } else { |
| 126 | wpa_printf( |
| 127 | MSG_ERROR, |
| 128 | "Error calling stat() on hostapd config file: %s", |
| 129 | strerror(errno)); |
| 130 | } |
| 131 | return ""; |
| 132 | } |
| 133 | |
| 134 | /* |
| 135 | * Get the op_class for a channel/band |
| 136 | * The logic here is based on Table E-4 in the 802.11 Specification |
| 137 | */ |
| 138 | int getOpClassForChannel(int channel, int band, bool support11n, bool support11ac) { |
| 139 | // 2GHz Band |
| 140 | if ((band & band2Ghz) != 0) { |
| 141 | if (channel == 14) { |
| 142 | return 82; |
| 143 | } |
| 144 | if (channel >= 1 && channel <= 13) { |
| 145 | if (!support11n) { |
| 146 | //20MHz channel |
| 147 | return 81; |
| 148 | } |
| 149 | if (channel <= 9) { |
| 150 | // HT40 with secondary channel above primary |
| 151 | return 83; |
| 152 | } |
| 153 | // HT40 with secondary channel below primary |
| 154 | return 84; |
| 155 | } |
| 156 | // Error |
| 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | // 5GHz Band |
| 161 | if ((band & band5Ghz) != 0) { |
| 162 | if (support11ac) { |
| 163 | switch (channel) { |
| 164 | case 42: |
| 165 | case 58: |
| 166 | case 106: |
| 167 | case 122: |
| 168 | case 138: |
| 169 | case 155: |
| 170 | // 80MHz channel |
| 171 | return 128; |
| 172 | case 50: |
| 173 | case 114: |
| 174 | // 160MHz channel |
| 175 | return 129; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | if (!support11n) { |
| 180 | if (channel >= 36 && channel <= 48) { |
| 181 | return 115; |
| 182 | } |
| 183 | if (channel >= 52 && channel <= 64) { |
| 184 | return 118; |
| 185 | } |
| 186 | if (channel >= 100 && channel <= 144) { |
| 187 | return 121; |
| 188 | } |
| 189 | if (channel >= 149 && channel <= 161) { |
| 190 | return 124; |
| 191 | } |
| 192 | if (channel >= 165 && channel <= 169) { |
| 193 | return 125; |
| 194 | } |
| 195 | } else { |
| 196 | switch (channel) { |
| 197 | case 36: |
| 198 | case 44: |
| 199 | // HT40 with secondary channel above primary |
| 200 | return 116; |
| 201 | case 40: |
| 202 | case 48: |
| 203 | // HT40 with secondary channel below primary |
| 204 | return 117; |
| 205 | case 52: |
| 206 | case 60: |
| 207 | // HT40 with secondary channel above primary |
| 208 | return 119; |
| 209 | case 56: |
| 210 | case 64: |
| 211 | // HT40 with secondary channel below primary |
| 212 | return 120; |
| 213 | case 100: |
| 214 | case 108: |
| 215 | case 116: |
| 216 | case 124: |
| 217 | case 132: |
| 218 | case 140: |
| 219 | // HT40 with secondary channel above primary |
| 220 | return 122; |
| 221 | case 104: |
| 222 | case 112: |
| 223 | case 120: |
| 224 | case 128: |
| 225 | case 136: |
| 226 | case 144: |
| 227 | // HT40 with secondary channel below primary |
| 228 | return 123; |
| 229 | case 149: |
| 230 | case 157: |
| 231 | // HT40 with secondary channel above primary |
| 232 | return 126; |
| 233 | case 153: |
| 234 | case 161: |
| 235 | // HT40 with secondary channel below primary |
| 236 | return 127; |
| 237 | } |
| 238 | } |
| 239 | // Error |
| 240 | return 0; |
| 241 | } |
| 242 | |
| 243 | // 6GHz Band |
| 244 | if ((band & band6Ghz) != 0) { |
| 245 | // Channels 1, 5. 9, 13, ... |
| 246 | if ((channel & 0x03) == 0x01) { |
| 247 | // 20MHz channel |
| 248 | return 131; |
| 249 | } |
| 250 | // Channels 3, 11, 19, 27, ... |
| 251 | if ((channel & 0x07) == 0x03) { |
| 252 | // 40MHz channel |
| 253 | return 132; |
| 254 | } |
| 255 | // Channels 7, 23, 39, 55, ... |
| 256 | if ((channel & 0x0F) == 0x07) { |
| 257 | // 80MHz channel |
| 258 | return 133; |
| 259 | } |
| 260 | // Channels 15, 47, 69, ... |
| 261 | if ((channel & 0x1F) == 0x0F) { |
| 262 | // 160MHz channel |
| 263 | return 134; |
| 264 | } |
| 265 | if (channel == 2) { |
| 266 | // 20MHz channel |
| 267 | return 136; |
| 268 | } |
| 269 | // Error |
| 270 | return 0; |
| 271 | } |
| 272 | |
| 273 | if ((band & band60Ghz) != 0) { |
| 274 | if (1 <= channel && channel <= 8) { |
| 275 | return 180; |
| 276 | } else if (9 <= channel && channel <= 15) { |
| 277 | return 181; |
| 278 | } else if (17 <= channel && channel <= 22) { |
| 279 | return 182; |
| 280 | } else if (25 <= channel && channel <= 29) { |
| 281 | return 183; |
| 282 | } |
| 283 | // Error |
| 284 | return 0; |
| 285 | } |
| 286 | |
| 287 | return 0; |
| 288 | } |
| 289 | |
| 290 | bool validatePassphrase(int passphrase_len, int min_len, int max_len) |
| 291 | { |
| 292 | if (min_len != -1 && passphrase_len < min_len) return false; |
| 293 | if (max_len != -1 && passphrase_len > max_len) return false; |
| 294 | return true; |
| 295 | } |
| 296 | |
| 297 | std::string CreateHostapdConfig( |
| 298 | const IfaceParams& iface_params, |
| 299 | const ChannelParams& channelParams, |
| 300 | const NetworkParams& nw_params, |
Purushottam Kushwaha | 0316c88 | 2021-12-20 15:07:44 +0530 | [diff] [blame] | 301 | const std::string br_name, |
| 302 | const std::string owe_transition_ifname) |
Gabriel Biren | 72cf9a5 | 2021-06-25 23:29:26 +0000 | [diff] [blame] | 303 | { |
| 304 | if (nw_params.ssid.size() > |
| 305 | static_cast<uint32_t>( |
| 306 | ParamSizeLimits::SSID_MAX_LEN_IN_BYTES)) { |
| 307 | wpa_printf( |
| 308 | MSG_ERROR, "Invalid SSID size: %zu", nw_params.ssid.size()); |
| 309 | return ""; |
| 310 | } |
| 311 | |
| 312 | // SSID string |
| 313 | std::stringstream ss; |
| 314 | ss << std::hex; |
| 315 | ss << std::setfill('0'); |
| 316 | for (uint8_t b : nw_params.ssid) { |
| 317 | ss << std::setw(2) << static_cast<unsigned int>(b); |
| 318 | } |
| 319 | const std::string ssid_as_string = ss.str(); |
| 320 | |
| 321 | // Encryption config string |
| 322 | uint32_t band = 0; |
| 323 | band |= static_cast<uint32_t>(channelParams.bandMask); |
Sunil Ravi | 4fc918f | 2022-04-17 09:26:48 -0700 | [diff] [blame] | 324 | bool is_2Ghz_band_only = band == static_cast<uint32_t>(band2Ghz); |
Gabriel Biren | 72cf9a5 | 2021-06-25 23:29:26 +0000 | [diff] [blame] | 325 | bool is_6Ghz_band_only = band == static_cast<uint32_t>(band6Ghz); |
| 326 | bool is_60Ghz_band_only = band == static_cast<uint32_t>(band60Ghz); |
| 327 | std::string encryption_config_as_string; |
| 328 | switch (nw_params.encryptionType) { |
| 329 | case EncryptionType::NONE: |
| 330 | // no security params |
| 331 | break; |
| 332 | case EncryptionType::WPA: |
| 333 | if (!validatePassphrase( |
| 334 | nw_params.passphrase.size(), |
| 335 | static_cast<uint32_t>(ParamSizeLimits:: |
| 336 | WPA2_PSK_PASSPHRASE_MIN_LEN_IN_BYTES), |
| 337 | static_cast<uint32_t>(ParamSizeLimits:: |
| 338 | WPA2_PSK_PASSPHRASE_MAX_LEN_IN_BYTES))) { |
| 339 | return ""; |
| 340 | } |
| 341 | encryption_config_as_string = StringPrintf( |
| 342 | "wpa=3\n" |
| 343 | "wpa_pairwise=%s\n" |
| 344 | "wpa_passphrase=%s", |
| 345 | is_60Ghz_band_only ? "GCMP" : "TKIP CCMP", |
| 346 | nw_params.passphrase.c_str()); |
| 347 | break; |
| 348 | case EncryptionType::WPA2: |
| 349 | if (!validatePassphrase( |
| 350 | nw_params.passphrase.size(), |
| 351 | static_cast<uint32_t>(ParamSizeLimits:: |
| 352 | WPA2_PSK_PASSPHRASE_MIN_LEN_IN_BYTES), |
| 353 | static_cast<uint32_t>(ParamSizeLimits:: |
| 354 | WPA2_PSK_PASSPHRASE_MAX_LEN_IN_BYTES))) { |
| 355 | return ""; |
| 356 | } |
| 357 | encryption_config_as_string = StringPrintf( |
| 358 | "wpa=2\n" |
| 359 | "rsn_pairwise=%s\n" |
Sunil Ravi | b3580db | 2022-01-28 12:25:46 -0800 | [diff] [blame] | 360 | #ifdef ENABLE_HOSTAPD_CONFIG_80211W_MFP_OPTIONAL |
| 361 | "ieee80211w=1\n" |
| 362 | #endif |
Gabriel Biren | 72cf9a5 | 2021-06-25 23:29:26 +0000 | [diff] [blame] | 363 | "wpa_passphrase=%s", |
| 364 | is_60Ghz_band_only ? "GCMP" : "CCMP", |
| 365 | nw_params.passphrase.c_str()); |
| 366 | break; |
| 367 | case EncryptionType::WPA3_SAE_TRANSITION: |
| 368 | if (!validatePassphrase( |
| 369 | nw_params.passphrase.size(), |
| 370 | static_cast<uint32_t>(ParamSizeLimits:: |
| 371 | WPA2_PSK_PASSPHRASE_MIN_LEN_IN_BYTES), |
| 372 | static_cast<uint32_t>(ParamSizeLimits:: |
| 373 | WPA2_PSK_PASSPHRASE_MAX_LEN_IN_BYTES))) { |
| 374 | return ""; |
| 375 | } |
| 376 | encryption_config_as_string = StringPrintf( |
| 377 | "wpa=2\n" |
| 378 | "rsn_pairwise=%s\n" |
| 379 | "wpa_key_mgmt=WPA-PSK SAE\n" |
| 380 | "ieee80211w=1\n" |
| 381 | "sae_require_mfp=1\n" |
| 382 | "wpa_passphrase=%s\n" |
| 383 | "sae_password=%s", |
| 384 | is_60Ghz_band_only ? "GCMP" : "CCMP", |
| 385 | nw_params.passphrase.c_str(), |
| 386 | nw_params.passphrase.c_str()); |
| 387 | break; |
| 388 | case EncryptionType::WPA3_SAE: |
| 389 | if (!validatePassphrase(nw_params.passphrase.size(), 1, -1)) { |
| 390 | return ""; |
| 391 | } |
| 392 | encryption_config_as_string = StringPrintf( |
| 393 | "wpa=2\n" |
| 394 | "rsn_pairwise=%s\n" |
Sunil Ravi | 6525173 | 2023-01-24 05:03:35 +0000 | [diff] [blame^] | 395 | "wpa_key_mgmt=%s\n" |
Gabriel Biren | 72cf9a5 | 2021-06-25 23:29:26 +0000 | [diff] [blame] | 396 | "ieee80211w=2\n" |
| 397 | "sae_require_mfp=2\n" |
| 398 | "sae_pwe=%d\n" |
| 399 | "sae_password=%s", |
| 400 | is_60Ghz_band_only ? "GCMP" : "CCMP", |
Sunil Ravi | 6525173 | 2023-01-24 05:03:35 +0000 | [diff] [blame^] | 401 | #ifdef CONFIG_IEEE80211BE |
| 402 | iface_params.hwModeParams.enable80211BE ? "SAE SAE-EXT-KEY" : "SAE", |
| 403 | #else |
| 404 | "SAE", |
| 405 | #endif |
Gabriel Biren | 72cf9a5 | 2021-06-25 23:29:26 +0000 | [diff] [blame] | 406 | is_6Ghz_band_only ? 1 : 2, |
| 407 | nw_params.passphrase.c_str()); |
| 408 | break; |
Ahmed ElArabawy | 1aaf180 | 2022-02-04 15:58:55 -0800 | [diff] [blame] | 409 | case EncryptionType::WPA3_OWE_TRANSITION: |
| 410 | encryption_config_as_string = StringPrintf( |
| 411 | "wpa=2\n" |
| 412 | "rsn_pairwise=%s\n" |
| 413 | "wpa_key_mgmt=OWE\n" |
| 414 | "ieee80211w=2", |
| 415 | is_60Ghz_band_only ? "GCMP" : "CCMP"); |
| 416 | break; |
| 417 | case EncryptionType::WPA3_OWE: |
Purushottam Kushwaha | 0316c88 | 2021-12-20 15:07:44 +0530 | [diff] [blame] | 418 | encryption_config_as_string = StringPrintf( |
| 419 | "wpa=2\n" |
| 420 | "rsn_pairwise=%s\n" |
| 421 | "wpa_key_mgmt=OWE\n" |
| 422 | "ieee80211w=2", |
| 423 | is_60Ghz_band_only ? "GCMP" : "CCMP"); |
| 424 | break; |
Gabriel Biren | 72cf9a5 | 2021-06-25 23:29:26 +0000 | [diff] [blame] | 425 | default: |
| 426 | wpa_printf(MSG_ERROR, "Unknown encryption type"); |
| 427 | return ""; |
| 428 | } |
| 429 | |
| 430 | std::string channel_config_as_string; |
| 431 | bool isFirst = true; |
| 432 | if (channelParams.enableAcs) { |
| 433 | std::string freqList_as_string; |
| 434 | for (const auto &range : |
| 435 | channelParams.acsChannelFreqRangesMhz) { |
| 436 | if (!isFirst) { |
| 437 | freqList_as_string += ","; |
| 438 | } |
| 439 | isFirst = false; |
| 440 | |
| 441 | if (range.startMhz != range.endMhz) { |
| 442 | freqList_as_string += |
| 443 | StringPrintf("%d-%d", range.startMhz, range.endMhz); |
| 444 | } else { |
| 445 | freqList_as_string += StringPrintf("%d", range.startMhz); |
| 446 | } |
| 447 | } |
| 448 | channel_config_as_string = StringPrintf( |
| 449 | "channel=0\n" |
| 450 | "acs_exclude_dfs=%d\n" |
| 451 | "freqlist=%s", |
| 452 | channelParams.acsShouldExcludeDfs, |
| 453 | freqList_as_string.c_str()); |
| 454 | } else { |
| 455 | int op_class = getOpClassForChannel( |
| 456 | channelParams.channel, |
| 457 | band, |
| 458 | iface_params.hwModeParams.enable80211N, |
| 459 | iface_params.hwModeParams.enable80211AC); |
| 460 | channel_config_as_string = StringPrintf( |
| 461 | "channel=%d\n" |
| 462 | "op_class=%d", |
| 463 | channelParams.channel, op_class); |
| 464 | } |
| 465 | |
| 466 | std::string hw_mode_as_string; |
Gabriel Biren | 72cf9a5 | 2021-06-25 23:29:26 +0000 | [diff] [blame] | 467 | std::string enable_edmg_as_string; |
| 468 | std::string edmg_channel_as_string; |
| 469 | bool is_60Ghz_used = false; |
| 470 | |
| 471 | if (((band & band60Ghz) != 0)) { |
| 472 | hw_mode_as_string = "hw_mode=ad"; |
| 473 | if (iface_params.hwModeParams.enableEdmg) { |
| 474 | enable_edmg_as_string = "enable_edmg=1"; |
| 475 | edmg_channel_as_string = StringPrintf( |
| 476 | "edmg_channel=%d", |
| 477 | channelParams.channel); |
| 478 | } |
| 479 | is_60Ghz_used = true; |
| 480 | } else if ((band & band2Ghz) != 0) { |
| 481 | if (((band & band5Ghz) != 0) |
| 482 | || ((band & band6Ghz) != 0)) { |
| 483 | hw_mode_as_string = "hw_mode=any"; |
Gabriel Biren | 72cf9a5 | 2021-06-25 23:29:26 +0000 | [diff] [blame] | 484 | } else { |
| 485 | hw_mode_as_string = "hw_mode=g"; |
| 486 | } |
| 487 | } else if (((band & band5Ghz) != 0) |
| 488 | || ((band & band6Ghz) != 0)) { |
| 489 | hw_mode_as_string = "hw_mode=a"; |
Gabriel Biren | 72cf9a5 | 2021-06-25 23:29:26 +0000 | [diff] [blame] | 490 | } else { |
| 491 | wpa_printf(MSG_ERROR, "Invalid band"); |
| 492 | return ""; |
| 493 | } |
| 494 | |
| 495 | std::string he_params_as_string; |
| 496 | #ifdef CONFIG_IEEE80211AX |
| 497 | if (iface_params.hwModeParams.enable80211AX && !is_60Ghz_used) { |
| 498 | he_params_as_string = StringPrintf( |
| 499 | "ieee80211ax=1\n" |
Gabriel Biren | 72cf9a5 | 2021-06-25 23:29:26 +0000 | [diff] [blame] | 500 | "he_su_beamformer=%d\n" |
| 501 | "he_su_beamformee=%d\n" |
| 502 | "he_mu_beamformer=%d\n" |
| 503 | "he_twt_required=%d\n", |
| 504 | iface_params.hwModeParams.enableHeSingleUserBeamformer ? 1 : 0, |
| 505 | iface_params.hwModeParams.enableHeSingleUserBeamformee ? 1 : 0, |
| 506 | iface_params.hwModeParams.enableHeMultiUserBeamformer ? 1 : 0, |
| 507 | iface_params.hwModeParams.enableHeTargetWakeTime ? 1 : 0); |
| 508 | } else { |
| 509 | he_params_as_string = "ieee80211ax=0"; |
| 510 | } |
| 511 | #endif /* CONFIG_IEEE80211AX */ |
Sunil Ravi | 6525173 | 2023-01-24 05:03:35 +0000 | [diff] [blame^] | 512 | std::string eht_params_as_string; |
| 513 | #ifdef CONFIG_IEEE80211BE |
| 514 | if (iface_params.hwModeParams.enable80211BE && !is_60Ghz_used) { |
| 515 | eht_params_as_string = "ieee80211be=1"; |
| 516 | /* TODO set eht_su_beamformer, eht_su_beamformee, eht_mu_beamformer */ |
| 517 | } else { |
| 518 | eht_params_as_string = "ieee80211be=0"; |
| 519 | } |
| 520 | #endif /* CONFIG_IEEE80211BE */ |
Gabriel Biren | 72cf9a5 | 2021-06-25 23:29:26 +0000 | [diff] [blame] | 521 | |
Purushottam Kushwaha | 90c710b | 2022-02-04 19:00:34 +0530 | [diff] [blame] | 522 | std::string ht_cap_vht_oper_he_oper_chwidth_as_string; |
| 523 | switch (iface_params.hwModeParams.maximumChannelBandwidth) { |
| 524 | case ChannelBandwidth::BANDWIDTH_20: |
| 525 | ht_cap_vht_oper_he_oper_chwidth_as_string = StringPrintf( |
| 526 | #ifdef CONFIG_IEEE80211AX |
| 527 | "he_oper_chwidth=0\n" |
| 528 | #endif |
| 529 | "vht_oper_chwidth=0"); |
| 530 | break; |
| 531 | case ChannelBandwidth::BANDWIDTH_40: |
| 532 | ht_cap_vht_oper_he_oper_chwidth_as_string = StringPrintf( |
| 533 | "ht_capab=[HT40+]\n" |
| 534 | #ifdef CONFIG_IEEE80211AX |
| 535 | "he_oper_chwidth=0\n" |
| 536 | #endif |
| 537 | "vht_oper_chwidth=0"); |
| 538 | break; |
| 539 | case ChannelBandwidth::BANDWIDTH_80: |
| 540 | ht_cap_vht_oper_he_oper_chwidth_as_string = StringPrintf( |
| 541 | "ht_capab=[HT40+]\n" |
| 542 | #ifdef CONFIG_IEEE80211AX |
| 543 | "he_oper_chwidth=%d\n" |
| 544 | #endif |
| 545 | "vht_oper_chwidth=%d", |
| 546 | #ifdef CONFIG_IEEE80211AX |
| 547 | (iface_params.hwModeParams.enable80211AX && !is_60Ghz_used) ? 1 : 0, |
| 548 | #endif |
| 549 | iface_params.hwModeParams.enable80211AC ? 1 : 0); |
| 550 | break; |
| 551 | case ChannelBandwidth::BANDWIDTH_160: |
| 552 | ht_cap_vht_oper_he_oper_chwidth_as_string = StringPrintf( |
| 553 | "ht_capab=[HT40+]\n" |
| 554 | #ifdef CONFIG_IEEE80211AX |
| 555 | "he_oper_chwidth=%d\n" |
| 556 | #endif |
| 557 | "vht_oper_chwidth=%d", |
| 558 | #ifdef CONFIG_IEEE80211AX |
| 559 | (iface_params.hwModeParams.enable80211AX && !is_60Ghz_used) ? 2 : 0, |
| 560 | #endif |
| 561 | iface_params.hwModeParams.enable80211AC ? 2 : 0); |
| 562 | break; |
| 563 | default: |
Sunil Ravi | ffa5cce | 2022-08-22 23:37:16 +0000 | [diff] [blame] | 564 | if (!is_2Ghz_band_only && !is_60Ghz_used) { |
| 565 | if (iface_params.hwModeParams.enable80211AC) { |
| 566 | ht_cap_vht_oper_he_oper_chwidth_as_string = |
Sunil Ravi | 4fc918f | 2022-04-17 09:26:48 -0700 | [diff] [blame] | 567 | "ht_capab=[HT40+]\n" |
| 568 | "vht_oper_chwidth=1\n"; |
Sunil Ravi | ffa5cce | 2022-08-22 23:37:16 +0000 | [diff] [blame] | 569 | } |
Purushottam Kushwaha | 90c710b | 2022-02-04 19:00:34 +0530 | [diff] [blame] | 570 | #ifdef CONFIG_IEEE80211AX |
Sunil Ravi | ffa5cce | 2022-08-22 23:37:16 +0000 | [diff] [blame] | 571 | if (iface_params.hwModeParams.enable80211AX) { |
| 572 | ht_cap_vht_oper_he_oper_chwidth_as_string += "he_oper_chwidth=1"; |
| 573 | } |
Purushottam Kushwaha | 90c710b | 2022-02-04 19:00:34 +0530 | [diff] [blame] | 574 | #endif |
Sunil Ravi | ffa5cce | 2022-08-22 23:37:16 +0000 | [diff] [blame] | 575 | } |
Purushottam Kushwaha | 90c710b | 2022-02-04 19:00:34 +0530 | [diff] [blame] | 576 | break; |
| 577 | } |
| 578 | |
Gabriel Biren | 72cf9a5 | 2021-06-25 23:29:26 +0000 | [diff] [blame] | 579 | #ifdef CONFIG_INTERWORKING |
| 580 | std::string access_network_params_as_string; |
| 581 | if (nw_params.isMetered) { |
| 582 | access_network_params_as_string = StringPrintf( |
| 583 | "interworking=1\n" |
| 584 | "access_network_type=2\n"); // CHARGEABLE_PUBLIC_NETWORK |
| 585 | } else { |
| 586 | access_network_params_as_string = StringPrintf( |
| 587 | "interworking=0\n"); |
| 588 | } |
| 589 | #endif /* CONFIG_INTERWORKING */ |
| 590 | |
| 591 | std::string bridge_as_string; |
| 592 | if (!br_name.empty()) { |
| 593 | bridge_as_string = StringPrintf("bridge=%s", br_name.c_str()); |
| 594 | } |
| 595 | |
Serik Beketayev | 8af7a72 | 2021-12-23 12:25:36 -0800 | [diff] [blame] | 596 | // vendor_elements string |
| 597 | std::string vendor_elements_as_string; |
| 598 | if (nw_params.vendorElements.size() > 0) { |
| 599 | std::stringstream ss; |
| 600 | ss << std::hex; |
| 601 | ss << std::setfill('0'); |
| 602 | for (uint8_t b : nw_params.vendorElements) { |
| 603 | ss << std::setw(2) << static_cast<unsigned int>(b); |
| 604 | } |
| 605 | vendor_elements_as_string = StringPrintf("vendor_elements=%s", ss.str().c_str()); |
| 606 | } |
| 607 | |
Purushottam Kushwaha | 0316c88 | 2021-12-20 15:07:44 +0530 | [diff] [blame] | 608 | std::string owe_transition_ifname_as_string; |
| 609 | if (!owe_transition_ifname.empty()) { |
| 610 | owe_transition_ifname_as_string = StringPrintf( |
| 611 | "owe_transition_ifname=%s", owe_transition_ifname.c_str()); |
| 612 | } |
| 613 | |
Gabriel Biren | 72cf9a5 | 2021-06-25 23:29:26 +0000 | [diff] [blame] | 614 | return StringPrintf( |
| 615 | "interface=%s\n" |
| 616 | "driver=nl80211\n" |
| 617 | "ctrl_interface=/data/vendor/wifi/hostapd/ctrl\n" |
| 618 | // ssid2 signals to hostapd that the value is not a literal value |
| 619 | // for use as a SSID. In this case, we're giving it a hex |
| 620 | // std::string and hostapd needs to expect that. |
| 621 | "ssid2=%s\n" |
| 622 | "%s\n" |
| 623 | "ieee80211n=%d\n" |
| 624 | "ieee80211ac=%d\n" |
| 625 | "%s\n" |
| 626 | "%s\n" |
| 627 | "%s\n" |
Sunil Ravi | 6525173 | 2023-01-24 05:03:35 +0000 | [diff] [blame^] | 628 | "%s\n" |
Gabriel Biren | 72cf9a5 | 2021-06-25 23:29:26 +0000 | [diff] [blame] | 629 | "ignore_broadcast_ssid=%d\n" |
| 630 | "wowlan_triggers=any\n" |
| 631 | #ifdef CONFIG_INTERWORKING |
| 632 | "%s\n" |
| 633 | #endif /* CONFIG_INTERWORKING */ |
| 634 | "%s\n" |
| 635 | "%s\n" |
| 636 | "%s\n" |
Serik Beketayev | 8af7a72 | 2021-12-23 12:25:36 -0800 | [diff] [blame] | 637 | "%s\n" |
Purushottam Kushwaha | 0316c88 | 2021-12-20 15:07:44 +0530 | [diff] [blame] | 638 | "%s\n" |
Gabriel Biren | 72cf9a5 | 2021-06-25 23:29:26 +0000 | [diff] [blame] | 639 | "%s\n", |
| 640 | iface_params.name.c_str(), ssid_as_string.c_str(), |
| 641 | channel_config_as_string.c_str(), |
| 642 | iface_params.hwModeParams.enable80211N ? 1 : 0, |
| 643 | iface_params.hwModeParams.enable80211AC ? 1 : 0, |
| 644 | he_params_as_string.c_str(), |
Sunil Ravi | 6525173 | 2023-01-24 05:03:35 +0000 | [diff] [blame^] | 645 | eht_params_as_string.c_str(), |
Purushottam Kushwaha | 90c710b | 2022-02-04 19:00:34 +0530 | [diff] [blame] | 646 | hw_mode_as_string.c_str(), ht_cap_vht_oper_he_oper_chwidth_as_string.c_str(), |
Gabriel Biren | 72cf9a5 | 2021-06-25 23:29:26 +0000 | [diff] [blame] | 647 | nw_params.isHidden ? 1 : 0, |
| 648 | #ifdef CONFIG_INTERWORKING |
| 649 | access_network_params_as_string.c_str(), |
| 650 | #endif /* CONFIG_INTERWORKING */ |
| 651 | encryption_config_as_string.c_str(), |
| 652 | bridge_as_string.c_str(), |
Purushottam Kushwaha | 0316c88 | 2021-12-20 15:07:44 +0530 | [diff] [blame] | 653 | owe_transition_ifname_as_string.c_str(), |
Gabriel Biren | 72cf9a5 | 2021-06-25 23:29:26 +0000 | [diff] [blame] | 654 | enable_edmg_as_string.c_str(), |
Serik Beketayev | 8af7a72 | 2021-12-23 12:25:36 -0800 | [diff] [blame] | 655 | edmg_channel_as_string.c_str(), |
| 656 | vendor_elements_as_string.c_str()); |
Gabriel Biren | 72cf9a5 | 2021-06-25 23:29:26 +0000 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | Generation getGeneration(hostapd_hw_modes *current_mode) |
| 660 | { |
| 661 | wpa_printf(MSG_DEBUG, "getGeneration hwmode=%d, ht_enabled=%d," |
| 662 | " vht_enabled=%d, he_supported=%d", |
| 663 | current_mode->mode, current_mode->ht_capab != 0, |
| 664 | current_mode->vht_capab != 0, current_mode->he_capab->he_supported); |
| 665 | switch (current_mode->mode) { |
| 666 | case HOSTAPD_MODE_IEEE80211B: |
| 667 | return Generation::WIFI_STANDARD_LEGACY; |
| 668 | case HOSTAPD_MODE_IEEE80211G: |
| 669 | return current_mode->ht_capab == 0 ? |
| 670 | Generation::WIFI_STANDARD_LEGACY : Generation::WIFI_STANDARD_11N; |
| 671 | case HOSTAPD_MODE_IEEE80211A: |
| 672 | if (current_mode->he_capab->he_supported) { |
| 673 | return Generation::WIFI_STANDARD_11AX; |
| 674 | } |
| 675 | return current_mode->vht_capab == 0 ? |
| 676 | Generation::WIFI_STANDARD_11N : Generation::WIFI_STANDARD_11AC; |
| 677 | case HOSTAPD_MODE_IEEE80211AD: |
| 678 | return Generation::WIFI_STANDARD_11AD; |
| 679 | default: |
| 680 | return Generation::WIFI_STANDARD_UNKNOWN; |
| 681 | } |
| 682 | } |
| 683 | |
Ahmed ElArabawy | b411579 | 2022-02-08 09:33:01 -0800 | [diff] [blame] | 684 | ChannelBandwidth getChannelBandwidth(struct hostapd_config *iconf) |
Gabriel Biren | 72cf9a5 | 2021-06-25 23:29:26 +0000 | [diff] [blame] | 685 | { |
Ahmed ElArabawy | b411579 | 2022-02-08 09:33:01 -0800 | [diff] [blame] | 686 | wpa_printf(MSG_DEBUG, "getChannelBandwidth %d, isHT=%d, isHT40=%d", |
Gabriel Biren | 72cf9a5 | 2021-06-25 23:29:26 +0000 | [diff] [blame] | 687 | iconf->vht_oper_chwidth, iconf->ieee80211n, |
| 688 | iconf->secondary_channel); |
| 689 | switch (iconf->vht_oper_chwidth) { |
Sunil | 8cd6f4d | 2022-06-28 18:40:46 +0000 | [diff] [blame] | 690 | case CONF_OPER_CHWIDTH_80MHZ: |
Ahmed ElArabawy | b411579 | 2022-02-08 09:33:01 -0800 | [diff] [blame] | 691 | return ChannelBandwidth::BANDWIDTH_80; |
Sunil | 8cd6f4d | 2022-06-28 18:40:46 +0000 | [diff] [blame] | 692 | case CONF_OPER_CHWIDTH_80P80MHZ: |
Ahmed ElArabawy | b411579 | 2022-02-08 09:33:01 -0800 | [diff] [blame] | 693 | return ChannelBandwidth::BANDWIDTH_80P80; |
Gabriel Biren | 72cf9a5 | 2021-06-25 23:29:26 +0000 | [diff] [blame] | 694 | break; |
Sunil | 8cd6f4d | 2022-06-28 18:40:46 +0000 | [diff] [blame] | 695 | case CONF_OPER_CHWIDTH_160MHZ: |
Ahmed ElArabawy | b411579 | 2022-02-08 09:33:01 -0800 | [diff] [blame] | 696 | return ChannelBandwidth::BANDWIDTH_160; |
Gabriel Biren | 72cf9a5 | 2021-06-25 23:29:26 +0000 | [diff] [blame] | 697 | break; |
Sunil | 8cd6f4d | 2022-06-28 18:40:46 +0000 | [diff] [blame] | 698 | case CONF_OPER_CHWIDTH_USE_HT: |
Gabriel Biren | 72cf9a5 | 2021-06-25 23:29:26 +0000 | [diff] [blame] | 699 | if (iconf->ieee80211n) { |
| 700 | return iconf->secondary_channel != 0 ? |
Ahmed ElArabawy | b411579 | 2022-02-08 09:33:01 -0800 | [diff] [blame] | 701 | ChannelBandwidth::BANDWIDTH_40 : ChannelBandwidth::BANDWIDTH_20; |
Gabriel Biren | 72cf9a5 | 2021-06-25 23:29:26 +0000 | [diff] [blame] | 702 | } |
Ahmed ElArabawy | b411579 | 2022-02-08 09:33:01 -0800 | [diff] [blame] | 703 | return ChannelBandwidth::BANDWIDTH_20_NOHT; |
Sunil | 8cd6f4d | 2022-06-28 18:40:46 +0000 | [diff] [blame] | 704 | case CONF_OPER_CHWIDTH_2160MHZ: |
Ahmed ElArabawy | b411579 | 2022-02-08 09:33:01 -0800 | [diff] [blame] | 705 | return ChannelBandwidth::BANDWIDTH_2160; |
Sunil | 8cd6f4d | 2022-06-28 18:40:46 +0000 | [diff] [blame] | 706 | case CONF_OPER_CHWIDTH_4320MHZ: |
Ahmed ElArabawy | b411579 | 2022-02-08 09:33:01 -0800 | [diff] [blame] | 707 | return ChannelBandwidth::BANDWIDTH_4320; |
Sunil | 8cd6f4d | 2022-06-28 18:40:46 +0000 | [diff] [blame] | 708 | case CONF_OPER_CHWIDTH_6480MHZ: |
Ahmed ElArabawy | b411579 | 2022-02-08 09:33:01 -0800 | [diff] [blame] | 709 | return ChannelBandwidth::BANDWIDTH_6480; |
Sunil | 8cd6f4d | 2022-06-28 18:40:46 +0000 | [diff] [blame] | 710 | case CONF_OPER_CHWIDTH_8640MHZ: |
Ahmed ElArabawy | b411579 | 2022-02-08 09:33:01 -0800 | [diff] [blame] | 711 | return ChannelBandwidth::BANDWIDTH_8640; |
Gabriel Biren | 72cf9a5 | 2021-06-25 23:29:26 +0000 | [diff] [blame] | 712 | default: |
Ahmed ElArabawy | b411579 | 2022-02-08 09:33:01 -0800 | [diff] [blame] | 713 | return ChannelBandwidth::BANDWIDTH_INVALID; |
Gabriel Biren | 72cf9a5 | 2021-06-25 23:29:26 +0000 | [diff] [blame] | 714 | } |
| 715 | } |
| 716 | |
| 717 | bool forceStaDisconnection(struct hostapd_data* hapd, |
| 718 | const std::vector<uint8_t>& client_address, |
| 719 | const uint16_t reason_code) { |
| 720 | struct sta_info *sta; |
Sunil Ravi | 1a36089 | 2022-11-29 20:16:01 +0000 | [diff] [blame] | 721 | if (client_address.size() != ETH_ALEN) { |
| 722 | return false; |
| 723 | } |
Gabriel Biren | 72cf9a5 | 2021-06-25 23:29:26 +0000 | [diff] [blame] | 724 | for (sta = hapd->sta_list; sta; sta = sta->next) { |
| 725 | int res; |
| 726 | res = memcmp(sta->addr, client_address.data(), ETH_ALEN); |
| 727 | if (res == 0) { |
| 728 | wpa_printf(MSG_INFO, "Force client:" MACSTR " disconnect with reason: %d", |
| 729 | MAC2STR(client_address.data()), reason_code); |
| 730 | ap_sta_disconnect(hapd, sta, sta->addr, reason_code); |
| 731 | return true; |
| 732 | } |
| 733 | } |
| 734 | return false; |
| 735 | } |
| 736 | |
| 737 | // hostapd core functions accept "C" style function pointers, so use global |
| 738 | // functions to pass to the hostapd core function and store the corresponding |
| 739 | // std::function methods to be invoked. |
| 740 | // |
| 741 | // NOTE: Using the pattern from the vendor HAL (wifi_legacy_hal.cpp). |
| 742 | // |
| 743 | // Callback to be invoked once setup is complete |
| 744 | std::function<void(struct hostapd_data*)> on_setup_complete_internal_callback; |
| 745 | void onAsyncSetupCompleteCb(void* ctx) |
| 746 | { |
| 747 | struct hostapd_data* iface_hapd = (struct hostapd_data*)ctx; |
| 748 | if (on_setup_complete_internal_callback) { |
| 749 | on_setup_complete_internal_callback(iface_hapd); |
| 750 | // Invalidate this callback since we don't want this firing |
| 751 | // again in single AP mode. |
| 752 | if (strlen(iface_hapd->conf->bridge) > 0) { |
| 753 | on_setup_complete_internal_callback = nullptr; |
| 754 | } |
| 755 | } |
| 756 | } |
| 757 | |
| 758 | // Callback to be invoked on hotspot client connection/disconnection |
| 759 | std::function<void(struct hostapd_data*, const u8 *mac_addr, int authorized, |
| 760 | const u8 *p2p_dev_addr)> on_sta_authorized_internal_callback; |
| 761 | void onAsyncStaAuthorizedCb(void* ctx, const u8 *mac_addr, int authorized, |
| 762 | const u8 *p2p_dev_addr) |
| 763 | { |
| 764 | struct hostapd_data* iface_hapd = (struct hostapd_data*)ctx; |
| 765 | if (on_sta_authorized_internal_callback) { |
| 766 | on_sta_authorized_internal_callback(iface_hapd, mac_addr, |
| 767 | authorized, p2p_dev_addr); |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | std::function<void(struct hostapd_data*, int level, |
| 772 | enum wpa_msg_type type, const char *txt, |
| 773 | size_t len)> on_wpa_msg_internal_callback; |
| 774 | |
| 775 | void onAsyncWpaEventCb(void *ctx, int level, |
| 776 | enum wpa_msg_type type, const char *txt, |
| 777 | size_t len) |
| 778 | { |
| 779 | struct hostapd_data* iface_hapd = (struct hostapd_data*)ctx; |
| 780 | if (on_wpa_msg_internal_callback) { |
| 781 | on_wpa_msg_internal_callback(iface_hapd, level, |
| 782 | type, txt, len); |
| 783 | } |
| 784 | } |
| 785 | |
| 786 | inline ndk::ScopedAStatus createStatus(HostapdStatusCode status_code) { |
| 787 | return ndk::ScopedAStatus::fromServiceSpecificError( |
| 788 | static_cast<int32_t>(status_code)); |
| 789 | } |
| 790 | |
| 791 | inline ndk::ScopedAStatus createStatusWithMsg( |
| 792 | HostapdStatusCode status_code, std::string msg) |
| 793 | { |
| 794 | return ndk::ScopedAStatus::fromServiceSpecificErrorWithMessage( |
| 795 | static_cast<int32_t>(status_code), msg.c_str()); |
| 796 | } |
| 797 | |
| 798 | // Method called by death_notifier_ on client death. |
| 799 | void onDeath(void* cookie) { |
| 800 | wpa_printf(MSG_ERROR, "Client died. Terminating..."); |
| 801 | eloop_terminate(); |
| 802 | } |
| 803 | |
| 804 | } // namespace |
| 805 | |
| 806 | namespace aidl { |
| 807 | namespace android { |
| 808 | namespace hardware { |
| 809 | namespace wifi { |
| 810 | namespace hostapd { |
| 811 | |
| 812 | Hostapd::Hostapd(struct hapd_interfaces* interfaces) |
| 813 | : interfaces_(interfaces) |
| 814 | { |
| 815 | death_notifier_ = AIBinder_DeathRecipient_new(onDeath); |
| 816 | } |
| 817 | |
| 818 | ::ndk::ScopedAStatus Hostapd::addAccessPoint( |
| 819 | const IfaceParams& iface_params, const NetworkParams& nw_params) |
| 820 | { |
| 821 | return addAccessPointInternal(iface_params, nw_params); |
| 822 | } |
| 823 | |
| 824 | ::ndk::ScopedAStatus Hostapd::removeAccessPoint(const std::string& iface_name) |
| 825 | { |
| 826 | return removeAccessPointInternal(iface_name); |
| 827 | } |
| 828 | |
| 829 | ::ndk::ScopedAStatus Hostapd::terminate() |
| 830 | { |
| 831 | wpa_printf(MSG_INFO, "Terminating..."); |
| 832 | // Clear the callback to avoid IPCThreadState shutdown during the |
| 833 | // callback event. |
| 834 | callbacks_.clear(); |
| 835 | eloop_terminate(); |
| 836 | return ndk::ScopedAStatus::ok(); |
| 837 | } |
| 838 | |
| 839 | ::ndk::ScopedAStatus Hostapd::registerCallback( |
| 840 | const std::shared_ptr<IHostapdCallback>& callback) |
| 841 | { |
| 842 | return registerCallbackInternal(callback); |
| 843 | } |
| 844 | |
| 845 | ::ndk::ScopedAStatus Hostapd::forceClientDisconnect( |
| 846 | const std::string& iface_name, const std::vector<uint8_t>& client_address, |
| 847 | Ieee80211ReasonCode reason_code) |
| 848 | { |
| 849 | return forceClientDisconnectInternal(iface_name, client_address, reason_code); |
| 850 | } |
| 851 | |
| 852 | ::ndk::ScopedAStatus Hostapd::setDebugParams(DebugLevel level) |
| 853 | { |
| 854 | return setDebugParamsInternal(level); |
| 855 | } |
| 856 | |
| 857 | ::ndk::ScopedAStatus Hostapd::addAccessPointInternal( |
| 858 | const IfaceParams& iface_params, |
| 859 | const NetworkParams& nw_params) |
| 860 | { |
| 861 | int channelParamsSize = iface_params.channelParams.size(); |
| 862 | if (channelParamsSize == 1) { |
| 863 | // Single AP |
| 864 | wpa_printf(MSG_INFO, "AddSingleAccessPoint, iface=%s", |
| 865 | iface_params.name.c_str()); |
| 866 | return addSingleAccessPoint(iface_params, iface_params.channelParams[0], |
Purushottam Kushwaha | 0316c88 | 2021-12-20 15:07:44 +0530 | [diff] [blame] | 867 | nw_params, "", ""); |
Gabriel Biren | 72cf9a5 | 2021-06-25 23:29:26 +0000 | [diff] [blame] | 868 | } else if (channelParamsSize == 2) { |
| 869 | // Concurrent APs |
| 870 | wpa_printf(MSG_INFO, "AddDualAccessPoint, iface=%s", |
| 871 | iface_params.name.c_str()); |
| 872 | return addConcurrentAccessPoints(iface_params, nw_params); |
| 873 | } |
| 874 | return createStatus(HostapdStatusCode::FAILURE_ARGS_INVALID); |
| 875 | } |
| 876 | |
Purushottam Kushwaha | 0316c88 | 2021-12-20 15:07:44 +0530 | [diff] [blame] | 877 | std::vector<uint8_t> generateRandomOweSsid() |
| 878 | { |
| 879 | u8 random[8] = {0}; |
| 880 | os_get_random(random, 8); |
| 881 | |
| 882 | std::string ssid = StringPrintf("Owe-%s", random); |
| 883 | wpa_printf(MSG_INFO, "Generated OWE SSID: %s", ssid.c_str()); |
| 884 | std::vector<uint8_t> vssid(ssid.begin(), ssid.end()); |
| 885 | |
| 886 | return vssid; |
| 887 | } |
| 888 | |
Gabriel Biren | 72cf9a5 | 2021-06-25 23:29:26 +0000 | [diff] [blame] | 889 | ::ndk::ScopedAStatus Hostapd::addConcurrentAccessPoints( |
| 890 | const IfaceParams& iface_params, const NetworkParams& nw_params) |
| 891 | { |
| 892 | int channelParamsListSize = iface_params.channelParams.size(); |
| 893 | // Get available interfaces in bridge |
| 894 | std::vector<std::string> managed_interfaces; |
| 895 | std::string br_name = StringPrintf( |
| 896 | "%s", iface_params.name.c_str()); |
| 897 | if (!GetInterfacesInBridge(br_name, &managed_interfaces)) { |
| 898 | return createStatusWithMsg(HostapdStatusCode::FAILURE_UNKNOWN, |
| 899 | "Get interfaces in bridge failed."); |
| 900 | } |
| 901 | if (managed_interfaces.size() < channelParamsListSize) { |
| 902 | return createStatusWithMsg(HostapdStatusCode::FAILURE_UNKNOWN, |
| 903 | "Available interfaces less than requested bands"); |
| 904 | } |
| 905 | // start BSS on specified bands |
| 906 | for (std::size_t i = 0; i < channelParamsListSize; i ++) { |
| 907 | IfaceParams iface_params_new = iface_params; |
Purushottam Kushwaha | 0316c88 | 2021-12-20 15:07:44 +0530 | [diff] [blame] | 908 | NetworkParams nw_params_new = nw_params; |
Gabriel Biren | 72cf9a5 | 2021-06-25 23:29:26 +0000 | [diff] [blame] | 909 | iface_params_new.name = managed_interfaces[i]; |
Purushottam Kushwaha | 0316c88 | 2021-12-20 15:07:44 +0530 | [diff] [blame] | 910 | |
| 911 | std::string owe_transition_ifname = ""; |
Ahmed ElArabawy | 1aaf180 | 2022-02-04 15:58:55 -0800 | [diff] [blame] | 912 | if (nw_params.encryptionType == EncryptionType::WPA3_OWE_TRANSITION) { |
Purushottam Kushwaha | 0316c88 | 2021-12-20 15:07:44 +0530 | [diff] [blame] | 913 | if (i == 0 && i+1 < channelParamsListSize) { |
| 914 | owe_transition_ifname = managed_interfaces[i+1]; |
| 915 | nw_params_new.encryptionType = EncryptionType::NONE; |
| 916 | } else { |
| 917 | owe_transition_ifname = managed_interfaces[0]; |
| 918 | nw_params_new.isHidden = true; |
| 919 | nw_params_new.ssid = generateRandomOweSsid(); |
| 920 | } |
| 921 | } |
| 922 | |
Gabriel Biren | 72cf9a5 | 2021-06-25 23:29:26 +0000 | [diff] [blame] | 923 | ndk::ScopedAStatus status = addSingleAccessPoint( |
Purushottam Kushwaha | 0316c88 | 2021-12-20 15:07:44 +0530 | [diff] [blame] | 924 | iface_params_new, iface_params.channelParams[i], nw_params_new, |
| 925 | br_name, owe_transition_ifname); |
Gabriel Biren | 72cf9a5 | 2021-06-25 23:29:26 +0000 | [diff] [blame] | 926 | if (!status.isOk()) { |
| 927 | wpa_printf(MSG_ERROR, "Failed to addAccessPoint %s", |
| 928 | managed_interfaces[i].c_str()); |
| 929 | return status; |
| 930 | } |
| 931 | } |
| 932 | // Save bridge interface info |
| 933 | br_interfaces_[br_name] = managed_interfaces; |
| 934 | return ndk::ScopedAStatus::ok(); |
| 935 | } |
| 936 | |
| 937 | ::ndk::ScopedAStatus Hostapd::addSingleAccessPoint( |
| 938 | const IfaceParams& iface_params, |
| 939 | const ChannelParams& channelParams, |
| 940 | const NetworkParams& nw_params, |
Purushottam Kushwaha | 0316c88 | 2021-12-20 15:07:44 +0530 | [diff] [blame] | 941 | const std::string br_name, |
| 942 | const std::string owe_transition_ifname) |
Gabriel Biren | 72cf9a5 | 2021-06-25 23:29:26 +0000 | [diff] [blame] | 943 | { |
| 944 | if (hostapd_get_iface(interfaces_, iface_params.name.c_str())) { |
| 945 | wpa_printf( |
| 946 | MSG_ERROR, "Interface %s already present", |
| 947 | iface_params.name.c_str()); |
| 948 | return createStatus(HostapdStatusCode::FAILURE_IFACE_EXISTS); |
| 949 | } |
Purushottam Kushwaha | 0316c88 | 2021-12-20 15:07:44 +0530 | [diff] [blame] | 950 | const auto conf_params = CreateHostapdConfig(iface_params, channelParams, nw_params, |
| 951 | br_name, owe_transition_ifname); |
Gabriel Biren | 72cf9a5 | 2021-06-25 23:29:26 +0000 | [diff] [blame] | 952 | if (conf_params.empty()) { |
| 953 | wpa_printf(MSG_ERROR, "Failed to create config params"); |
| 954 | return createStatus(HostapdStatusCode::FAILURE_ARGS_INVALID); |
| 955 | } |
| 956 | const auto conf_file_path = |
| 957 | WriteHostapdConfig(iface_params.name, conf_params); |
| 958 | if (conf_file_path.empty()) { |
| 959 | wpa_printf(MSG_ERROR, "Failed to write config file"); |
| 960 | return createStatus(HostapdStatusCode::FAILURE_UNKNOWN); |
| 961 | } |
| 962 | std::string add_iface_param_str = StringPrintf( |
| 963 | "%s config=%s", iface_params.name.c_str(), |
| 964 | conf_file_path.c_str()); |
| 965 | std::vector<char> add_iface_param_vec( |
| 966 | add_iface_param_str.begin(), add_iface_param_str.end() + 1); |
| 967 | if (hostapd_add_iface(interfaces_, add_iface_param_vec.data()) < 0) { |
| 968 | wpa_printf( |
| 969 | MSG_ERROR, "Adding interface %s failed", |
| 970 | add_iface_param_str.c_str()); |
| 971 | return createStatus(HostapdStatusCode::FAILURE_UNKNOWN); |
| 972 | } |
| 973 | struct hostapd_data* iface_hapd = |
| 974 | hostapd_get_iface(interfaces_, iface_params.name.c_str()); |
| 975 | WPA_ASSERT(iface_hapd != nullptr && iface_hapd->iface != nullptr); |
| 976 | // Register the setup complete callbacks |
| 977 | on_setup_complete_internal_callback = |
| 978 | [this](struct hostapd_data* iface_hapd) { |
| 979 | wpa_printf( |
| 980 | MSG_INFO, "AP interface setup completed - state %s", |
| 981 | hostapd_state_text(iface_hapd->iface->state)); |
| 982 | if (iface_hapd->iface->state == HAPD_IFACE_DISABLED) { |
| 983 | // Invoke the failure callback on all registered |
| 984 | // clients. |
| 985 | for (const auto& callback : callbacks_) { |
| 986 | callback->onFailure(strlen(iface_hapd->conf->bridge) > 0 ? |
Les Lee | e08c286 | 2021-10-29 16:36:41 +0800 | [diff] [blame] | 987 | iface_hapd->conf->bridge : iface_hapd->conf->iface, |
| 988 | iface_hapd->conf->iface); |
Gabriel Biren | 72cf9a5 | 2021-06-25 23:29:26 +0000 | [diff] [blame] | 989 | } |
| 990 | } |
| 991 | }; |
| 992 | |
| 993 | // Register for new client connect/disconnect indication. |
| 994 | on_sta_authorized_internal_callback = |
| 995 | [this](struct hostapd_data* iface_hapd, const u8 *mac_addr, |
| 996 | int authorized, const u8 *p2p_dev_addr) { |
| 997 | wpa_printf(MSG_DEBUG, "notify client " MACSTR " %s", |
| 998 | MAC2STR(mac_addr), |
| 999 | (authorized) ? "Connected" : "Disconnected"); |
| 1000 | ClientInfo info; |
| 1001 | info.ifaceName = strlen(iface_hapd->conf->bridge) > 0 ? |
| 1002 | iface_hapd->conf->bridge : iface_hapd->conf->iface; |
| 1003 | info.apIfaceInstance = iface_hapd->conf->iface; |
| 1004 | info.clientAddress.assign(mac_addr, mac_addr + ETH_ALEN); |
| 1005 | info.isConnected = authorized; |
| 1006 | for (const auto &callback : callbacks_) { |
| 1007 | callback->onConnectedClientsChanged(info); |
| 1008 | } |
| 1009 | }; |
| 1010 | |
| 1011 | // Register for wpa_event which used to get channel switch event |
| 1012 | on_wpa_msg_internal_callback = |
| 1013 | [this](struct hostapd_data* iface_hapd, int level, |
| 1014 | enum wpa_msg_type type, const char *txt, |
| 1015 | size_t len) { |
| 1016 | wpa_printf(MSG_DEBUG, "Receive wpa msg : %s", txt); |
| 1017 | if (os_strncmp(txt, AP_EVENT_ENABLED, |
| 1018 | strlen(AP_EVENT_ENABLED)) == 0 || |
| 1019 | os_strncmp(txt, WPA_EVENT_CHANNEL_SWITCH, |
| 1020 | strlen(WPA_EVENT_CHANNEL_SWITCH)) == 0) { |
| 1021 | ApInfo info; |
| 1022 | info.ifaceName = strlen(iface_hapd->conf->bridge) > 0 ? |
| 1023 | iface_hapd->conf->bridge : iface_hapd->conf->iface, |
| 1024 | info.apIfaceInstance = iface_hapd->conf->iface; |
| 1025 | info.freqMhz = iface_hapd->iface->freq; |
Ahmed ElArabawy | b411579 | 2022-02-08 09:33:01 -0800 | [diff] [blame] | 1026 | info.channelBandwidth = getChannelBandwidth(iface_hapd->iconf); |
Gabriel Biren | 72cf9a5 | 2021-06-25 23:29:26 +0000 | [diff] [blame] | 1027 | info.generation = getGeneration(iface_hapd->iface->current_mode); |
| 1028 | info.apIfaceInstanceMacAddress.assign(iface_hapd->own_addr, |
| 1029 | iface_hapd->own_addr + ETH_ALEN); |
| 1030 | for (const auto &callback : callbacks_) { |
| 1031 | callback->onApInstanceInfoChanged(info); |
| 1032 | } |
Les Lee | a0c90cb | 2022-04-19 17:39:23 +0800 | [diff] [blame] | 1033 | } else if (os_strncmp(txt, AP_EVENT_DISABLED, strlen(AP_EVENT_DISABLED)) == 0 |
| 1034 | || os_strncmp(txt, INTERFACE_DISABLED, strlen(INTERFACE_DISABLED)) == 0) |
| 1035 | { |
Yu Ouyang | 378d3c4 | 2021-08-20 17:31:08 +0800 | [diff] [blame] | 1036 | // Invoke the failure callback on all registered clients. |
| 1037 | for (const auto& callback : callbacks_) { |
| 1038 | callback->onFailure(strlen(iface_hapd->conf->bridge) > 0 ? |
Les Lee | e08c286 | 2021-10-29 16:36:41 +0800 | [diff] [blame] | 1039 | iface_hapd->conf->bridge : iface_hapd->conf->iface, |
| 1040 | iface_hapd->conf->iface); |
Yu Ouyang | 378d3c4 | 2021-08-20 17:31:08 +0800 | [diff] [blame] | 1041 | } |
Gabriel Biren | 72cf9a5 | 2021-06-25 23:29:26 +0000 | [diff] [blame] | 1042 | } |
Yu Ouyang | 378d3c4 | 2021-08-20 17:31:08 +0800 | [diff] [blame] | 1043 | }; |
Gabriel Biren | 72cf9a5 | 2021-06-25 23:29:26 +0000 | [diff] [blame] | 1044 | |
| 1045 | // Setup callback |
| 1046 | iface_hapd->setup_complete_cb = onAsyncSetupCompleteCb; |
| 1047 | iface_hapd->setup_complete_cb_ctx = iface_hapd; |
| 1048 | iface_hapd->sta_authorized_cb = onAsyncStaAuthorizedCb; |
| 1049 | iface_hapd->sta_authorized_cb_ctx = iface_hapd; |
Hu Wang | 7c5a432 | 2021-06-24 17:24:59 +0800 | [diff] [blame] | 1050 | wpa_msg_register_aidl_cb(onAsyncWpaEventCb); |
Gabriel Biren | 72cf9a5 | 2021-06-25 23:29:26 +0000 | [diff] [blame] | 1051 | |
| 1052 | if (hostapd_enable_iface(iface_hapd->iface) < 0) { |
| 1053 | wpa_printf( |
| 1054 | MSG_ERROR, "Enabling interface %s failed", |
| 1055 | iface_params.name.c_str()); |
| 1056 | return createStatus(HostapdStatusCode::FAILURE_UNKNOWN); |
| 1057 | } |
| 1058 | return ndk::ScopedAStatus::ok(); |
| 1059 | } |
| 1060 | |
| 1061 | ::ndk::ScopedAStatus Hostapd::removeAccessPointInternal(const std::string& iface_name) |
| 1062 | { |
| 1063 | // interfaces to be removed |
| 1064 | std::vector<std::string> interfaces; |
| 1065 | bool is_error = false; |
| 1066 | |
| 1067 | const auto it = br_interfaces_.find(iface_name); |
| 1068 | if (it != br_interfaces_.end()) { |
| 1069 | // In case bridge, remove managed interfaces |
| 1070 | interfaces = it->second; |
| 1071 | br_interfaces_.erase(iface_name); |
| 1072 | } else { |
| 1073 | // else remove current interface |
| 1074 | interfaces.push_back(iface_name); |
| 1075 | } |
| 1076 | |
| 1077 | for (auto& iface : interfaces) { |
| 1078 | std::vector<char> remove_iface_param_vec( |
| 1079 | iface.begin(), iface.end() + 1); |
| 1080 | if (hostapd_remove_iface(interfaces_, remove_iface_param_vec.data()) < 0) { |
| 1081 | wpa_printf(MSG_INFO, "Remove interface %s failed", iface.c_str()); |
| 1082 | is_error = true; |
| 1083 | } |
| 1084 | } |
| 1085 | if (is_error) { |
| 1086 | return createStatus(HostapdStatusCode::FAILURE_UNKNOWN); |
| 1087 | } |
| 1088 | return ndk::ScopedAStatus::ok(); |
| 1089 | } |
| 1090 | |
| 1091 | ::ndk::ScopedAStatus Hostapd::registerCallbackInternal( |
| 1092 | const std::shared_ptr<IHostapdCallback>& callback) |
| 1093 | { |
| 1094 | binder_status_t status = AIBinder_linkToDeath(callback->asBinder().get(), |
| 1095 | death_notifier_, this /* cookie */); |
| 1096 | if (status != STATUS_OK) { |
| 1097 | wpa_printf( |
| 1098 | MSG_ERROR, |
| 1099 | "Error registering for death notification for " |
| 1100 | "hostapd callback object"); |
| 1101 | return createStatus(HostapdStatusCode::FAILURE_UNKNOWN); |
| 1102 | } |
| 1103 | callbacks_.push_back(callback); |
| 1104 | return ndk::ScopedAStatus::ok(); |
| 1105 | } |
| 1106 | |
| 1107 | ::ndk::ScopedAStatus Hostapd::forceClientDisconnectInternal(const std::string& iface_name, |
| 1108 | const std::vector<uint8_t>& client_address, Ieee80211ReasonCode reason_code) |
| 1109 | { |
| 1110 | struct hostapd_data *hapd = hostapd_get_iface(interfaces_, iface_name.c_str()); |
| 1111 | bool result; |
| 1112 | if (!hapd) { |
| 1113 | for (auto const& iface : br_interfaces_) { |
| 1114 | if (iface.first == iface_name) { |
| 1115 | for (auto const& instance : iface.second) { |
| 1116 | hapd = hostapd_get_iface(interfaces_, instance.c_str()); |
| 1117 | if (hapd) { |
| 1118 | result = forceStaDisconnection(hapd, client_address, |
| 1119 | (uint16_t) reason_code); |
| 1120 | if (result) break; |
| 1121 | } |
| 1122 | } |
| 1123 | } |
| 1124 | } |
| 1125 | } else { |
| 1126 | result = forceStaDisconnection(hapd, client_address, (uint16_t) reason_code); |
| 1127 | } |
| 1128 | if (!hapd) { |
| 1129 | wpa_printf(MSG_ERROR, "Interface %s doesn't exist", iface_name.c_str()); |
| 1130 | return createStatus(HostapdStatusCode::FAILURE_IFACE_UNKNOWN); |
| 1131 | } |
| 1132 | if (result) { |
| 1133 | return ndk::ScopedAStatus::ok(); |
| 1134 | } |
| 1135 | return createStatus(HostapdStatusCode::FAILURE_CLIENT_UNKNOWN); |
| 1136 | } |
| 1137 | |
| 1138 | ::ndk::ScopedAStatus Hostapd::setDebugParamsInternal(DebugLevel level) |
| 1139 | { |
| 1140 | wpa_debug_level = static_cast<uint32_t>(level); |
| 1141 | return ndk::ScopedAStatus::ok(); |
| 1142 | } |
| 1143 | |
| 1144 | } // namespace hostapd |
| 1145 | } // namespace wifi |
| 1146 | } // namespace hardware |
| 1147 | } // namespace android |
Les Lee | e08c286 | 2021-10-29 16:36:41 +0800 | [diff] [blame] | 1148 | } // namespace aidl |