blob: 412919c9cb92a31008379855b60535cf141494bd [file] [log] [blame]
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001/*
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
32extern "C"
33{
34#include "common/wpa_ctrl.h"
35#include "drivers/linux_ioctl.h"
36}
37
Chris Weird41879a2024-10-30 15:53:24 -070038
Chris Weir5031a042024-10-21 13:31:20 -070039#ifdef ANDROID_HOSTAPD_UNITTEST
Chris Weird41879a2024-10-30 15:53:24 -070040#include "tests/unittest_overrides.h"
Chris Weir5031a042024-10-21 13:31:20 -070041#endif
42
Gabriel Biren72cf9a52021-06-25 23:29:26 +000043// The AIDL implementation for hostapd creates a hostapd.conf dynamically for
44// each interface. This file can then be used to hook onto the normal config
45// file parsing logic in hostapd code. Helps us to avoid duplication of code
46// in the AIDL interface.
47// TOOD(b/71872409): Add unit tests for this.
48namespace {
49constexpr char kConfFileNameFmt[] = "/data/vendor/wifi/hostapd/hostapd_%s.conf";
50
Chris Weird41879a2024-10-30 15:53:24 -070051/**
52 * To add an overlay file, add
53 *
54 * PRODUCT_COPY_FILES += \
55 * <your/path/here>/hostapd_unmetered_overlay.conf:/vendor/etc/wifi/hostapd_unmetered_overlay.conf
56 *
57 * to the build file for your device, with the <your/path/here> being the path to your overlay in
58 * your repo. See the resolveVendorConfPath function in this file for more specifics on where this
59 * overlay file will wind up on your device.
60 *
61 * This overlay may configure any of the parameters listed in kOverlayableKeys. The kOverlayableKeys
62 * list is subject to change over time, as certain parameters may be added as APIs instead in the
63 * future.
64 *
65 * Example of what an overlay file might look like:
66 * $> cat hostapd_unmetered_overlay.conf
67 * dtim_period=2
68 * ap_max_inactivity=300
69 *
70 * Anything added to this overlay will be prepended to the hostapd.conf for unmetered (typically
71 * local only hotspots) interfaces.
72 */
73constexpr char kUnmeteredIfaceOverlayPath[] = "/etc/wifi/hostapd_unmetered_overlay.conf";
74
75/**
76 * Allow-list of hostapd.conf parameters (keys) that can be set via overlay.
77 *
78 * If introducing new APIs, be sure to remove keys from this list that would otherwise be
79 * controlled by the new API. This way we can avoid conflicting settings.
80 * Please file an FR to add new keys to this list.
81 */
82static const std::set<std::string> kOverlayableKeys = {
83 "ap_max_inactivity",
84 "assocresp_elements"
85 "beacon_int",
86 "disassoc_low_ack",
87 "dtim_period",
88 "fragm_threshold",
89 "max_listen_interval",
90 "max_num_sta",
91 "rts_threshold",
92 "skip_inactivity_poll",
93 "uapsd_advertisement_enabled",
94 "wmm_enabled",
95 "wmm_ac_vo_aifs",
96 "wmm_ac_vo_cwmin",
97 "wmm_ac_vo_cwmax",
98 "wmm_ac_vo_txop_limit",
99 "wmm_ac_vo_acm",
100 "wmm_ac_vi_aifs",
101 "wmm_ac_vi_cwmin",
102 "wmm_ac_vi_cwmax",
103 "wmm_ac_vi_txop_limit",
104 "wmm_ac_vi_acm",
105 "wmm_ac_bk_cwmin"
106 "wmm_ac_bk_cwmax"
107 "wmm_ac_bk_aifs",
108 "wmm_ac_bk_txop_limit",
109 "wmm_ac_bk_acm",
110 "wmm_ac_be_aifs",
111 "wmm_ac_be_cwmin",
112 "wmm_ac_be_cwmax",
113 "wmm_ac_be_txop_limit",
114 "wmm_ac_be_acm",
115};
116
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000117using android::base::RemoveFileIfExists;
118using android::base::StringPrintf;
Chris Weird41879a2024-10-30 15:53:24 -0700119#ifndef ANDROID_HOSTAPD_UNITTEST
120using android::base::ReadFileToString;
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000121using android::base::WriteStringToFile;
Chris Weird41879a2024-10-30 15:53:24 -0700122#endif
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000123using aidl::android::hardware::wifi::hostapd::BandMask;
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800124using aidl::android::hardware::wifi::hostapd::ChannelBandwidth;
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000125using aidl::android::hardware::wifi::hostapd::ChannelParams;
126using aidl::android::hardware::wifi::hostapd::EncryptionType;
127using aidl::android::hardware::wifi::hostapd::Generation;
128using aidl::android::hardware::wifi::hostapd::HostapdStatusCode;
129using aidl::android::hardware::wifi::hostapd::IfaceParams;
130using aidl::android::hardware::wifi::hostapd::NetworkParams;
131using aidl::android::hardware::wifi::hostapd::ParamSizeLimits;
132
133int band2Ghz = (int)BandMask::BAND_2_GHZ;
134int band5Ghz = (int)BandMask::BAND_5_GHZ;
135int band6Ghz = (int)BandMask::BAND_6_GHZ;
136int band60Ghz = (int)BandMask::BAND_60_GHZ;
137
Manaswini Paluri76ae6982024-02-29 14:49:20 +0530138int32_t aidl_client_version = 0;
139int32_t aidl_service_version = 0;
140
Les Lee16bb41b2024-12-03 01:50:22 +0000141inline std::array<uint8_t, ETH_ALEN> macAddrToArray(const uint8_t* mac_addr) {
142 std::array<uint8_t, ETH_ALEN> arr;
143 std::copy(mac_addr, mac_addr + ETH_ALEN, std::begin(arr));
144 return arr;
145}
146
Manaswini Paluri76ae6982024-02-29 14:49:20 +0530147/**
148 * Check that the AIDL service is running at least the expected version.
149 * Use to avoid the case where the AIDL interface version
150 * is greater than the version implemented by the service.
151 */
152inline int32_t isAidlServiceVersionAtLeast(int32_t expected_version)
153{
154 return expected_version <= aidl_service_version;
155}
156
157inline int32_t isAidlClientVersionAtLeast(int32_t expected_version)
158{
159 return expected_version <= aidl_client_version;
160}
161
Sunil Ravide0e6bf2024-10-14 04:58:16 +0000162inline int32_t areAidlServiceAndClientAtLeastVersion(int32_t expected_version)
163{
164 return isAidlServiceVersionAtLeast(expected_version)
165 && isAidlClientVersionAtLeast(expected_version);
166}
167
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000168#define MAX_PORTS 1024
169bool GetInterfacesInBridge(std::string br_name,
170 std::vector<std::string>* interfaces) {
171 android::base::unique_fd sock(socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0));
172 if (sock.get() < 0) {
173 wpa_printf(MSG_ERROR, "Failed to create sock (%s) in %s",
174 strerror(errno), __FUNCTION__);
175 return false;
176 }
177
178 struct ifreq request;
179 int i, ifindices[MAX_PORTS];
180 char if_name[IFNAMSIZ];
181 unsigned long args[3];
182
183 memset(ifindices, 0, MAX_PORTS * sizeof(int));
184
185 args[0] = BRCTL_GET_PORT_LIST;
186 args[1] = (unsigned long) ifindices;
187 args[2] = MAX_PORTS;
188
189 strlcpy(request.ifr_name, br_name.c_str(), IFNAMSIZ);
190 request.ifr_data = (char *)args;
191
192 if (ioctl(sock.get(), SIOCDEVPRIVATE, &request) < 0) {
193 wpa_printf(MSG_ERROR, "Failed to ioctl SIOCDEVPRIVATE in %s",
194 __FUNCTION__);
195 return false;
196 }
197
198 for (i = 0; i < MAX_PORTS; i ++) {
199 memset(if_name, 0, IFNAMSIZ);
200 if (ifindices[i] == 0 || !if_indextoname(ifindices[i], if_name)) {
201 continue;
202 }
203 interfaces->push_back(if_name);
204 }
205 return true;
206}
207
Chris Weird41879a2024-10-30 15:53:24 -0700208std::string resolveVendorConfPath(const std::string& conf_path)
209{
210#if defined(__ANDROID_APEX__)
211 // returns "/apex/<apexname>" + conf_path
212 std::string path = android::base::GetExecutablePath();
213 return path.substr(0, path.find_first_of('/', strlen("/apex/"))) + conf_path;
214#else
215 return std::string("/vendor") + conf_path;
216#endif
217}
218
219void logHostapdConfigError(int error, const std::string& file_path) {
220 wpa_printf(MSG_ERROR, "Cannot read/write hostapd config %s, error: %s", file_path.c_str(),
221 strerror(error));
222 struct stat st;
223 int result = stat(file_path.c_str(), &st);
224 if (result == 0) {
225 wpa_printf(MSG_ERROR, "hostapd config file uid: %d, gid: %d, mode: %d",st.st_uid,
226 st.st_gid, st.st_mode);
227 } else {
228 wpa_printf(MSG_ERROR, "Error calling stat() on hostapd config file: %s",
229 strerror(errno));
230 }
231}
232
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000233std::string WriteHostapdConfig(
Les Lee399c6302024-09-12 03:03:38 +0000234 const std::string& instance_name, const std::string& config,
235 const std::string br_name, const bool usesMlo)
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000236{
Les Lee399c6302024-09-12 03:03:38 +0000237 std::string conf_name_as_string = instance_name;
238 if (usesMlo) {
239 conf_name_as_string = StringPrintf(
240 "%s-%s", br_name.c_str(), instance_name.c_str());
241 }
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000242 const std::string file_path =
Les Lee399c6302024-09-12 03:03:38 +0000243 StringPrintf(kConfFileNameFmt, conf_name_as_string.c_str());
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000244 if (WriteStringToFile(
245 config, file_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
246 getuid(), getgid())) {
247 return file_path;
248 }
249 // Diagnose failure
250 int error = errno;
Chris Weird41879a2024-10-30 15:53:24 -0700251 logHostapdConfigError(errno, file_path);
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000252 return "";
253}
254
255/*
256 * Get the op_class for a channel/band
257 * The logic here is based on Table E-4 in the 802.11 Specification
258 */
259int getOpClassForChannel(int channel, int band, bool support11n, bool support11ac) {
260 // 2GHz Band
261 if ((band & band2Ghz) != 0) {
262 if (channel == 14) {
263 return 82;
264 }
265 if (channel >= 1 && channel <= 13) {
266 if (!support11n) {
267 //20MHz channel
268 return 81;
269 }
270 if (channel <= 9) {
271 // HT40 with secondary channel above primary
272 return 83;
273 }
274 // HT40 with secondary channel below primary
275 return 84;
276 }
277 // Error
278 return 0;
279 }
280
281 // 5GHz Band
282 if ((band & band5Ghz) != 0) {
283 if (support11ac) {
284 switch (channel) {
285 case 42:
286 case 58:
287 case 106:
288 case 122:
289 case 138:
290 case 155:
291 // 80MHz channel
292 return 128;
293 case 50:
294 case 114:
295 // 160MHz channel
296 return 129;
297 }
298 }
299
300 if (!support11n) {
301 if (channel >= 36 && channel <= 48) {
302 return 115;
303 }
304 if (channel >= 52 && channel <= 64) {
305 return 118;
306 }
307 if (channel >= 100 && channel <= 144) {
308 return 121;
309 }
310 if (channel >= 149 && channel <= 161) {
311 return 124;
312 }
313 if (channel >= 165 && channel <= 169) {
314 return 125;
315 }
316 } else {
317 switch (channel) {
318 case 36:
319 case 44:
320 // HT40 with secondary channel above primary
321 return 116;
322 case 40:
323 case 48:
324 // HT40 with secondary channel below primary
325 return 117;
326 case 52:
327 case 60:
328 // HT40 with secondary channel above primary
329 return 119;
330 case 56:
331 case 64:
332 // HT40 with secondary channel below primary
333 return 120;
334 case 100:
335 case 108:
336 case 116:
337 case 124:
338 case 132:
339 case 140:
340 // HT40 with secondary channel above primary
341 return 122;
342 case 104:
343 case 112:
344 case 120:
345 case 128:
346 case 136:
347 case 144:
348 // HT40 with secondary channel below primary
349 return 123;
350 case 149:
351 case 157:
352 // HT40 with secondary channel above primary
353 return 126;
354 case 153:
355 case 161:
356 // HT40 with secondary channel below primary
357 return 127;
358 }
359 }
360 // Error
361 return 0;
362 }
363
364 // 6GHz Band
365 if ((band & band6Ghz) != 0) {
366 // Channels 1, 5. 9, 13, ...
367 if ((channel & 0x03) == 0x01) {
368 // 20MHz channel
369 return 131;
370 }
371 // Channels 3, 11, 19, 27, ...
372 if ((channel & 0x07) == 0x03) {
373 // 40MHz channel
374 return 132;
375 }
376 // Channels 7, 23, 39, 55, ...
377 if ((channel & 0x0F) == 0x07) {
378 // 80MHz channel
379 return 133;
380 }
381 // Channels 15, 47, 69, ...
382 if ((channel & 0x1F) == 0x0F) {
383 // 160MHz channel
384 return 134;
385 }
386 if (channel == 2) {
387 // 20MHz channel
388 return 136;
389 }
390 // Error
391 return 0;
392 }
393
394 if ((band & band60Ghz) != 0) {
395 if (1 <= channel && channel <= 8) {
396 return 180;
397 } else if (9 <= channel && channel <= 15) {
398 return 181;
399 } else if (17 <= channel && channel <= 22) {
400 return 182;
401 } else if (25 <= channel && channel <= 29) {
402 return 183;
403 }
404 // Error
405 return 0;
406 }
407
408 return 0;
409}
410
411bool validatePassphrase(int passphrase_len, int min_len, int max_len)
412{
413 if (min_len != -1 && passphrase_len < min_len) return false;
414 if (max_len != -1 && passphrase_len > max_len) return false;
415 return true;
416}
417
Manaswini Paluri76ae6982024-02-29 14:49:20 +0530418std::string getInterfaceMacAddress(const std::string& if_name)
419{
420 u8 addr[ETH_ALEN] = {};
421 struct ifreq ifr;
422 std::string mac_addr;
423
424 android::base::unique_fd sock(socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0));
425 if (sock.get() < 0) {
426 wpa_printf(MSG_ERROR, "Failed to create sock (%s) in %s",
427 strerror(errno), __FUNCTION__);
428 return "";
429 }
430
431 memset(&ifr, 0, sizeof(ifr));
432 strlcpy(ifr.ifr_name, if_name.c_str(), IFNAMSIZ);
433 if (ioctl(sock.get(), SIOCGIFHWADDR, &ifr) < 0) {
434 wpa_printf(MSG_ERROR, "Could not get interface %s hwaddr: %s",
435 if_name.c_str(), strerror(errno));
436 return "";
437 }
438
439 memcpy(addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
440 mac_addr = StringPrintf("" MACSTR, MAC2STR(addr));
441
442 return mac_addr;
443}
444
Chris Weird41879a2024-10-30 15:53:24 -0700445std::string trimWhitespace(const std::string& str) {
446 size_t pos = 0;
447 size_t len = str.size();
448 for (pos; pos < str.size() && std::isspace(str[pos]); ++pos){}
449 for (len; len - 1 > 0 && std::isspace(str[len-1]); --len){}
450 return str.substr(pos, len);
451}
452
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000453std::string CreateHostapdConfig(
454 const IfaceParams& iface_params,
455 const ChannelParams& channelParams,
456 const NetworkParams& nw_params,
Purushottam Kushwaha0316c882021-12-20 15:07:44 +0530457 const std::string br_name,
458 const std::string owe_transition_ifname)
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000459{
460 if (nw_params.ssid.size() >
461 static_cast<uint32_t>(
462 ParamSizeLimits::SSID_MAX_LEN_IN_BYTES)) {
463 wpa_printf(
464 MSG_ERROR, "Invalid SSID size: %zu", nw_params.ssid.size());
465 return "";
466 }
467
468 // SSID string
469 std::stringstream ss;
470 ss << std::hex;
471 ss << std::setfill('0');
472 for (uint8_t b : nw_params.ssid) {
473 ss << std::setw(2) << static_cast<unsigned int>(b);
474 }
475 const std::string ssid_as_string = ss.str();
476
477 // Encryption config string
478 uint32_t band = 0;
479 band |= static_cast<uint32_t>(channelParams.bandMask);
Sunil Ravi4fc918f2022-04-17 09:26:48 -0700480 bool is_2Ghz_band_only = band == static_cast<uint32_t>(band2Ghz);
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000481 bool is_6Ghz_band_only = band == static_cast<uint32_t>(band6Ghz);
482 bool is_60Ghz_band_only = band == static_cast<uint32_t>(band60Ghz);
483 std::string encryption_config_as_string;
484 switch (nw_params.encryptionType) {
485 case EncryptionType::NONE:
486 // no security params
487 break;
488 case EncryptionType::WPA:
489 if (!validatePassphrase(
490 nw_params.passphrase.size(),
491 static_cast<uint32_t>(ParamSizeLimits::
492 WPA2_PSK_PASSPHRASE_MIN_LEN_IN_BYTES),
493 static_cast<uint32_t>(ParamSizeLimits::
494 WPA2_PSK_PASSPHRASE_MAX_LEN_IN_BYTES))) {
495 return "";
496 }
497 encryption_config_as_string = StringPrintf(
498 "wpa=3\n"
499 "wpa_pairwise=%s\n"
500 "wpa_passphrase=%s",
501 is_60Ghz_band_only ? "GCMP" : "TKIP CCMP",
502 nw_params.passphrase.c_str());
503 break;
504 case EncryptionType::WPA2:
505 if (!validatePassphrase(
506 nw_params.passphrase.size(),
507 static_cast<uint32_t>(ParamSizeLimits::
508 WPA2_PSK_PASSPHRASE_MIN_LEN_IN_BYTES),
509 static_cast<uint32_t>(ParamSizeLimits::
510 WPA2_PSK_PASSPHRASE_MAX_LEN_IN_BYTES))) {
511 return "";
512 }
513 encryption_config_as_string = StringPrintf(
514 "wpa=2\n"
515 "rsn_pairwise=%s\n"
Sunil Ravib3580db2022-01-28 12:25:46 -0800516#ifdef ENABLE_HOSTAPD_CONFIG_80211W_MFP_OPTIONAL
517 "ieee80211w=1\n"
518#endif
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000519 "wpa_passphrase=%s",
520 is_60Ghz_band_only ? "GCMP" : "CCMP",
521 nw_params.passphrase.c_str());
522 break;
523 case EncryptionType::WPA3_SAE_TRANSITION:
524 if (!validatePassphrase(
525 nw_params.passphrase.size(),
526 static_cast<uint32_t>(ParamSizeLimits::
527 WPA2_PSK_PASSPHRASE_MIN_LEN_IN_BYTES),
528 static_cast<uint32_t>(ParamSizeLimits::
529 WPA2_PSK_PASSPHRASE_MAX_LEN_IN_BYTES))) {
530 return "";
531 }
Sunil Ravid917c832023-07-07 17:30:33 +0000532 // WPA3 transition mode or SAE+WPA_PSK key management(AKM) is not allowed in 6GHz.
533 // Auto-convert any such configurations to SAE.
534 if ((band & band6Ghz) != 0) {
535 wpa_printf(MSG_INFO, "WPA3_SAE_TRANSITION configured in 6GHz band."
536 "Enable only SAE in key_mgmt");
537 encryption_config_as_string = StringPrintf(
538 "wpa=2\n"
539 "rsn_pairwise=CCMP\n"
540 "wpa_key_mgmt=%s\n"
541 "ieee80211w=2\n"
542 "sae_require_mfp=2\n"
543 "sae_pwe=%d\n"
544 "sae_password=%s",
Sunil Ravic1edd3e2023-02-06 18:52:51 +0000545#ifdef CONFIG_IEEE80211BE
Sunil Ravid917c832023-07-07 17:30:33 +0000546 iface_params.hwModeParams.enable80211BE ?
547 "SAE SAE-EXT-KEY" : "SAE",
Sunil Ravic1edd3e2023-02-06 18:52:51 +0000548#else
Sunil Ravid917c832023-07-07 17:30:33 +0000549 "SAE",
Sunil Ravic1edd3e2023-02-06 18:52:51 +0000550#endif
Sunil Ravid917c832023-07-07 17:30:33 +0000551 is_6Ghz_band_only ? 1 : 2,
552 nw_params.passphrase.c_str());
553 } else {
554 encryption_config_as_string = StringPrintf(
555 "wpa=2\n"
556 "rsn_pairwise=%s\n"
557 "wpa_key_mgmt=%s\n"
558 "ieee80211w=1\n"
559 "sae_require_mfp=1\n"
560 "wpa_passphrase=%s\n"
561 "sae_password=%s",
562 is_60Ghz_band_only ? "GCMP" : "CCMP",
563#ifdef CONFIG_IEEE80211BE
564 iface_params.hwModeParams.enable80211BE ?
565 "WPA-PSK SAE SAE-EXT-KEY" : "WPA-PSK SAE",
566#else
567 "WPA-PSK SAE",
568#endif
569 nw_params.passphrase.c_str(),
570 nw_params.passphrase.c_str());
571 }
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000572 break;
573 case EncryptionType::WPA3_SAE:
574 if (!validatePassphrase(nw_params.passphrase.size(), 1, -1)) {
575 return "";
576 }
577 encryption_config_as_string = StringPrintf(
578 "wpa=2\n"
579 "rsn_pairwise=%s\n"
Sunil Ravi65251732023-01-24 05:03:35 +0000580 "wpa_key_mgmt=%s\n"
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000581 "ieee80211w=2\n"
582 "sae_require_mfp=2\n"
583 "sae_pwe=%d\n"
584 "sae_password=%s",
585 is_60Ghz_band_only ? "GCMP" : "CCMP",
Sunil Ravi65251732023-01-24 05:03:35 +0000586#ifdef CONFIG_IEEE80211BE
587 iface_params.hwModeParams.enable80211BE ? "SAE SAE-EXT-KEY" : "SAE",
588#else
589 "SAE",
590#endif
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000591 is_6Ghz_band_only ? 1 : 2,
592 nw_params.passphrase.c_str());
593 break;
Ahmed ElArabawy1aaf1802022-02-04 15:58:55 -0800594 case EncryptionType::WPA3_OWE_TRANSITION:
595 encryption_config_as_string = StringPrintf(
596 "wpa=2\n"
597 "rsn_pairwise=%s\n"
598 "wpa_key_mgmt=OWE\n"
599 "ieee80211w=2",
600 is_60Ghz_band_only ? "GCMP" : "CCMP");
601 break;
602 case EncryptionType::WPA3_OWE:
Purushottam Kushwaha0316c882021-12-20 15:07:44 +0530603 encryption_config_as_string = StringPrintf(
604 "wpa=2\n"
605 "rsn_pairwise=%s\n"
606 "wpa_key_mgmt=OWE\n"
607 "ieee80211w=2",
608 is_60Ghz_band_only ? "GCMP" : "CCMP");
609 break;
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000610 default:
611 wpa_printf(MSG_ERROR, "Unknown encryption type");
612 return "";
613 }
614
615 std::string channel_config_as_string;
616 bool isFirst = true;
617 if (channelParams.enableAcs) {
618 std::string freqList_as_string;
619 for (const auto &range :
620 channelParams.acsChannelFreqRangesMhz) {
621 if (!isFirst) {
622 freqList_as_string += ",";
623 }
624 isFirst = false;
625
626 if (range.startMhz != range.endMhz) {
627 freqList_as_string +=
628 StringPrintf("%d-%d", range.startMhz, range.endMhz);
629 } else {
630 freqList_as_string += StringPrintf("%d", range.startMhz);
631 }
632 }
633 channel_config_as_string = StringPrintf(
634 "channel=0\n"
635 "acs_exclude_dfs=%d\n"
636 "freqlist=%s",
637 channelParams.acsShouldExcludeDfs,
638 freqList_as_string.c_str());
639 } else {
640 int op_class = getOpClassForChannel(
641 channelParams.channel,
642 band,
643 iface_params.hwModeParams.enable80211N,
644 iface_params.hwModeParams.enable80211AC);
645 channel_config_as_string = StringPrintf(
646 "channel=%d\n"
647 "op_class=%d",
648 channelParams.channel, op_class);
649 }
650
651 std::string hw_mode_as_string;
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000652 std::string enable_edmg_as_string;
653 std::string edmg_channel_as_string;
654 bool is_60Ghz_used = false;
655
656 if (((band & band60Ghz) != 0)) {
657 hw_mode_as_string = "hw_mode=ad";
658 if (iface_params.hwModeParams.enableEdmg) {
659 enable_edmg_as_string = "enable_edmg=1";
660 edmg_channel_as_string = StringPrintf(
661 "edmg_channel=%d",
662 channelParams.channel);
663 }
664 is_60Ghz_used = true;
665 } else if ((band & band2Ghz) != 0) {
666 if (((band & band5Ghz) != 0)
667 || ((band & band6Ghz) != 0)) {
668 hw_mode_as_string = "hw_mode=any";
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000669 } else {
670 hw_mode_as_string = "hw_mode=g";
671 }
672 } else if (((band & band5Ghz) != 0)
673 || ((band & band6Ghz) != 0)) {
674 hw_mode_as_string = "hw_mode=a";
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000675 } else {
676 wpa_printf(MSG_ERROR, "Invalid band");
677 return "";
678 }
679
680 std::string he_params_as_string;
681#ifdef CONFIG_IEEE80211AX
682 if (iface_params.hwModeParams.enable80211AX && !is_60Ghz_used) {
683 he_params_as_string = StringPrintf(
684 "ieee80211ax=1\n"
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000685 "he_su_beamformer=%d\n"
686 "he_su_beamformee=%d\n"
687 "he_mu_beamformer=%d\n"
688 "he_twt_required=%d\n",
689 iface_params.hwModeParams.enableHeSingleUserBeamformer ? 1 : 0,
690 iface_params.hwModeParams.enableHeSingleUserBeamformee ? 1 : 0,
691 iface_params.hwModeParams.enableHeMultiUserBeamformer ? 1 : 0,
692 iface_params.hwModeParams.enableHeTargetWakeTime ? 1 : 0);
693 } else {
694 he_params_as_string = "ieee80211ax=0";
695 }
696#endif /* CONFIG_IEEE80211AX */
Sunil Ravi65251732023-01-24 05:03:35 +0000697 std::string eht_params_as_string;
698#ifdef CONFIG_IEEE80211BE
699 if (iface_params.hwModeParams.enable80211BE && !is_60Ghz_used) {
Manaswini Paluri76ae6982024-02-29 14:49:20 +0530700 eht_params_as_string = "ieee80211be=1\n";
Sunil Ravide0e6bf2024-10-14 04:58:16 +0000701 if (areAidlServiceAndClientAtLeastVersion(2)) {
Les Lee399c6302024-09-12 03:03:38 +0000702 std::string interface_mac_addr = getInterfaceMacAddress(
703 iface_params.usesMlo ? br_name : iface_params.name);
Manaswini Paluri76ae6982024-02-29 14:49:20 +0530704 if (interface_mac_addr.empty()) {
705 wpa_printf(MSG_ERROR,
706 "Unable to set interface mac address as bssid for 11BE SAP");
707 return "";
708 }
Les Lee399c6302024-09-12 03:03:38 +0000709 if (iface_params.usesMlo) {
710 eht_params_as_string += StringPrintf(
711 "mld_addr=%s\n"
712 "mld_ap=1",
713 interface_mac_addr.c_str());
714 } else {
715 eht_params_as_string += StringPrintf(
716 "bssid=%s\n"
717 "mld_ap=1",
718 interface_mac_addr.c_str());
719 }
Manaswini Paluri76ae6982024-02-29 14:49:20 +0530720 }
Sunil Ravi65251732023-01-24 05:03:35 +0000721 /* TODO set eht_su_beamformer, eht_su_beamformee, eht_mu_beamformer */
722 } else {
723 eht_params_as_string = "ieee80211be=0";
724 }
725#endif /* CONFIG_IEEE80211BE */
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000726
Xin Dengfec682f2024-02-06 22:59:39 -0800727 std::string ht_cap_vht_oper_he_oper_eht_oper_chwidth_as_string;
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530728 switch (iface_params.hwModeParams.maximumChannelBandwidth) {
729 case ChannelBandwidth::BANDWIDTH_20:
Xin Dengfec682f2024-02-06 22:59:39 -0800730 ht_cap_vht_oper_he_oper_eht_oper_chwidth_as_string = StringPrintf(
731#ifdef CONFIG_IEEE80211BE
732 "eht_oper_chwidth=0\n"
733#endif /* CONFIG_IEEE80211BE */
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530734#ifdef CONFIG_IEEE80211AX
735 "he_oper_chwidth=0\n"
736#endif
Kiran Kumar Lokereb6445122024-02-06 20:06:27 -0800737 "vht_oper_chwidth=0\n"
738 "%s", (band & band6Ghz) ? "op_class=131" : "");
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530739 break;
740 case ChannelBandwidth::BANDWIDTH_40:
Xin Dengfec682f2024-02-06 22:59:39 -0800741 ht_cap_vht_oper_he_oper_eht_oper_chwidth_as_string = StringPrintf(
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530742 "ht_capab=[HT40+]\n"
Xin Dengfec682f2024-02-06 22:59:39 -0800743#ifdef CONFIG_IEEE80211BE
744 "eht_oper_chwidth=0\n"
745#endif /* CONFIG_IEEE80211BE */
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530746#ifdef CONFIG_IEEE80211AX
747 "he_oper_chwidth=0\n"
748#endif
Kiran Kumar Lokereb6445122024-02-06 20:06:27 -0800749 "vht_oper_chwidth=0\n"
750 "%s", (band & band6Ghz) ? "op_class=132" : "");
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530751 break;
752 case ChannelBandwidth::BANDWIDTH_80:
Xin Dengfec682f2024-02-06 22:59:39 -0800753 ht_cap_vht_oper_he_oper_eht_oper_chwidth_as_string = StringPrintf(
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530754 "ht_capab=[HT40+]\n"
Xin Dengfec682f2024-02-06 22:59:39 -0800755#ifdef CONFIG_IEEE80211BE
756 "eht_oper_chwidth=%d\n"
757#endif /* CONFIG_IEEE80211BE */
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530758#ifdef CONFIG_IEEE80211AX
759 "he_oper_chwidth=%d\n"
760#endif
Kiran Kumar Lokereb6445122024-02-06 20:06:27 -0800761 "vht_oper_chwidth=%d\n"
762 "%s",
Xin Dengfec682f2024-02-06 22:59:39 -0800763#ifdef CONFIG_IEEE80211BE
764 (iface_params.hwModeParams.enable80211BE && !is_60Ghz_used) ? 1 : 0,
765#endif
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530766#ifdef CONFIG_IEEE80211AX
767 (iface_params.hwModeParams.enable80211AX && !is_60Ghz_used) ? 1 : 0,
768#endif
Kiran Kumar Lokereb6445122024-02-06 20:06:27 -0800769 iface_params.hwModeParams.enable80211AC ? 1 : 0,
770 (band & band6Ghz) ? "op_class=133" : "");
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530771 break;
772 case ChannelBandwidth::BANDWIDTH_160:
Xin Dengfec682f2024-02-06 22:59:39 -0800773 ht_cap_vht_oper_he_oper_eht_oper_chwidth_as_string = StringPrintf(
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530774 "ht_capab=[HT40+]\n"
Xin Dengfec682f2024-02-06 22:59:39 -0800775#ifdef CONFIG_IEEE80211BE
776 "eht_oper_chwidth=%d\n"
777#endif /* CONFIG_IEEE80211BE */
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530778#ifdef CONFIG_IEEE80211AX
779 "he_oper_chwidth=%d\n"
780#endif
Kiran Kumar Lokereb6445122024-02-06 20:06:27 -0800781 "vht_oper_chwidth=%d\n"
782 "%s",
Xin Dengfec682f2024-02-06 22:59:39 -0800783#ifdef CONFIG_IEEE80211BE
784 (iface_params.hwModeParams.enable80211BE && !is_60Ghz_used) ? 2 : 0,
785#endif
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530786#ifdef CONFIG_IEEE80211AX
787 (iface_params.hwModeParams.enable80211AX && !is_60Ghz_used) ? 2 : 0,
788#endif
Kiran Kumar Lokereb6445122024-02-06 20:06:27 -0800789 iface_params.hwModeParams.enable80211AC ? 2 : 0,
790 (band & band6Ghz) ? "op_class=134" : "");
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530791 break;
792 default:
Sunil Raviffa5cce2022-08-22 23:37:16 +0000793 if (!is_2Ghz_band_only && !is_60Ghz_used) {
794 if (iface_params.hwModeParams.enable80211AC) {
Xin Dengfec682f2024-02-06 22:59:39 -0800795 ht_cap_vht_oper_he_oper_eht_oper_chwidth_as_string =
Sunil Ravi4fc918f2022-04-17 09:26:48 -0700796 "ht_capab=[HT40+]\n"
797 "vht_oper_chwidth=1\n";
Sunil Raviffa5cce2022-08-22 23:37:16 +0000798 }
Kiran Kumar Lokereb6445122024-02-06 20:06:27 -0800799 if (band & band6Ghz) {
Xin Deng3ee3fe12024-02-20 23:24:37 -0800800#ifdef CONFIG_IEEE80211BE
801 if (iface_params.hwModeParams.enable80211BE)
802 ht_cap_vht_oper_he_oper_eht_oper_chwidth_as_string += "op_class=137\n";
803 else
804 ht_cap_vht_oper_he_oper_eht_oper_chwidth_as_string += "op_class=134\n";
805#else /* CONFIG_IEEE80211BE */
Xin Dengfec682f2024-02-06 22:59:39 -0800806 ht_cap_vht_oper_he_oper_eht_oper_chwidth_as_string += "op_class=134\n";
Xin Deng3ee3fe12024-02-20 23:24:37 -0800807#endif /* CONFIG_IEEE80211BE */
Kiran Kumar Lokereb6445122024-02-06 20:06:27 -0800808 }
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530809#ifdef CONFIG_IEEE80211AX
Sunil Raviffa5cce2022-08-22 23:37:16 +0000810 if (iface_params.hwModeParams.enable80211AX) {
Xin Dengfec682f2024-02-06 22:59:39 -0800811 ht_cap_vht_oper_he_oper_eht_oper_chwidth_as_string += "he_oper_chwidth=1\n";
812 }
813#endif
814#ifdef CONFIG_IEEE80211BE
815 if (iface_params.hwModeParams.enable80211BE) {
816 ht_cap_vht_oper_he_oper_eht_oper_chwidth_as_string += "eht_oper_chwidth=1";
Sunil Raviffa5cce2022-08-22 23:37:16 +0000817 }
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530818#endif
Sunil Raviffa5cce2022-08-22 23:37:16 +0000819 }
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530820 break;
821 }
822
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000823#ifdef CONFIG_INTERWORKING
824 std::string access_network_params_as_string;
825 if (nw_params.isMetered) {
826 access_network_params_as_string = StringPrintf(
827 "interworking=1\n"
828 "access_network_type=2\n"); // CHARGEABLE_PUBLIC_NETWORK
829 } else {
830 access_network_params_as_string = StringPrintf(
831 "interworking=0\n");
832 }
833#endif /* CONFIG_INTERWORKING */
834
835 std::string bridge_as_string;
Les Lee399c6302024-09-12 03:03:38 +0000836 if (!br_name.empty() && !iface_params.usesMlo) {
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000837 bridge_as_string = StringPrintf("bridge=%s", br_name.c_str());
838 }
839
Serik Beketayev8af7a722021-12-23 12:25:36 -0800840 // vendor_elements string
841 std::string vendor_elements_as_string;
842 if (nw_params.vendorElements.size() > 0) {
843 std::stringstream ss;
844 ss << std::hex;
845 ss << std::setfill('0');
846 for (uint8_t b : nw_params.vendorElements) {
847 ss << std::setw(2) << static_cast<unsigned int>(b);
848 }
849 vendor_elements_as_string = StringPrintf("vendor_elements=%s", ss.str().c_str());
850 }
851
Purushottam Kushwaha0316c882021-12-20 15:07:44 +0530852 std::string owe_transition_ifname_as_string;
853 if (!owe_transition_ifname.empty()) {
854 owe_transition_ifname_as_string = StringPrintf(
855 "owe_transition_ifname=%s", owe_transition_ifname.c_str());
856 }
857
Les Lee9dfae3b2024-10-25 18:31:47 +0000858 std::string ap_isolation_as_string = StringPrintf("ap_isolate=%s",
859 isAidlServiceVersionAtLeast(3) && nw_params.isClientIsolationEnabled ?
860 "1" : "0");
861
Chris Weird41879a2024-10-30 15:53:24 -0700862 // Overlay for LOHS (unmetered SoftAP)
863 std::string overlay_path = resolveVendorConfPath(kUnmeteredIfaceOverlayPath);
864 std::string overlay_string;
865 if (!nw_params.isMetered
866 && 0 == access(overlay_path.c_str(), R_OK)
867 && !ReadFileToString(overlay_path, &overlay_string)) {
868 logHostapdConfigError(errno, overlay_path);
869 return "";
870 }
871 std::string sanitized_overlay = "";
872 std::istringstream overlay_stream(overlay_string);
873 for (std::string line; std::getline(overlay_stream, line);) {
874 std::string overlay_key = trimWhitespace(line.substr(0, line.find("=")));
875 if (kOverlayableKeys.contains(overlay_key)) {
876 sanitized_overlay.append(line + "\n");
877 }
878 }
879
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000880 return StringPrintf(
Chris Weird41879a2024-10-30 15:53:24 -0700881 "%s\n"
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000882 "interface=%s\n"
883 "driver=nl80211\n"
Les Lee399c6302024-09-12 03:03:38 +0000884 "ctrl_interface=/data/vendor/wifi/hostapd/ctrl_%s\n"
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000885 // ssid2 signals to hostapd that the value is not a literal value
886 // for use as a SSID. In this case, we're giving it a hex
887 // std::string and hostapd needs to expect that.
888 "ssid2=%s\n"
889 "%s\n"
890 "ieee80211n=%d\n"
891 "ieee80211ac=%d\n"
892 "%s\n"
893 "%s\n"
894 "%s\n"
Sunil Ravi65251732023-01-24 05:03:35 +0000895 "%s\n"
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000896 "ignore_broadcast_ssid=%d\n"
897 "wowlan_triggers=any\n"
898#ifdef CONFIG_INTERWORKING
899 "%s\n"
900#endif /* CONFIG_INTERWORKING */
901 "%s\n"
902 "%s\n"
903 "%s\n"
Serik Beketayev8af7a722021-12-23 12:25:36 -0800904 "%s\n"
Purushottam Kushwaha0316c882021-12-20 15:07:44 +0530905 "%s\n"
Les Lee9dfae3b2024-10-25 18:31:47 +0000906 "%s\n"
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000907 "%s\n",
Chris Weird41879a2024-10-30 15:53:24 -0700908 sanitized_overlay.c_str(),
Les Lee399c6302024-09-12 03:03:38 +0000909 iface_params.usesMlo ? br_name.c_str() : iface_params.name.c_str(),
910 iface_params.name.c_str(),
911 ssid_as_string.c_str(),
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000912 channel_config_as_string.c_str(),
913 iface_params.hwModeParams.enable80211N ? 1 : 0,
914 iface_params.hwModeParams.enable80211AC ? 1 : 0,
915 he_params_as_string.c_str(),
Sunil Ravi65251732023-01-24 05:03:35 +0000916 eht_params_as_string.c_str(),
Xin Dengfec682f2024-02-06 22:59:39 -0800917 hw_mode_as_string.c_str(), ht_cap_vht_oper_he_oper_eht_oper_chwidth_as_string.c_str(),
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000918 nw_params.isHidden ? 1 : 0,
919#ifdef CONFIG_INTERWORKING
920 access_network_params_as_string.c_str(),
921#endif /* CONFIG_INTERWORKING */
922 encryption_config_as_string.c_str(),
923 bridge_as_string.c_str(),
Purushottam Kushwaha0316c882021-12-20 15:07:44 +0530924 owe_transition_ifname_as_string.c_str(),
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000925 enable_edmg_as_string.c_str(),
Serik Beketayev8af7a722021-12-23 12:25:36 -0800926 edmg_channel_as_string.c_str(),
Les Lee9dfae3b2024-10-25 18:31:47 +0000927 vendor_elements_as_string.c_str(),
928 ap_isolation_as_string.c_str());
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000929}
930
931Generation getGeneration(hostapd_hw_modes *current_mode)
932{
933 wpa_printf(MSG_DEBUG, "getGeneration hwmode=%d, ht_enabled=%d,"
934 " vht_enabled=%d, he_supported=%d",
935 current_mode->mode, current_mode->ht_capab != 0,
936 current_mode->vht_capab != 0, current_mode->he_capab->he_supported);
937 switch (current_mode->mode) {
938 case HOSTAPD_MODE_IEEE80211B:
939 return Generation::WIFI_STANDARD_LEGACY;
940 case HOSTAPD_MODE_IEEE80211G:
Les Leec330c242024-12-18 22:27:43 +0000941 if (current_mode->he_capab->he_supported) {
942 return Generation::WIFI_STANDARD_11AX;
943 }
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000944 return current_mode->ht_capab == 0 ?
945 Generation::WIFI_STANDARD_LEGACY : Generation::WIFI_STANDARD_11N;
946 case HOSTAPD_MODE_IEEE80211A:
947 if (current_mode->he_capab->he_supported) {
948 return Generation::WIFI_STANDARD_11AX;
949 }
950 return current_mode->vht_capab == 0 ?
951 Generation::WIFI_STANDARD_11N : Generation::WIFI_STANDARD_11AC;
952 case HOSTAPD_MODE_IEEE80211AD:
953 return Generation::WIFI_STANDARD_11AD;
954 default:
955 return Generation::WIFI_STANDARD_UNKNOWN;
956 }
957}
958
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800959ChannelBandwidth getChannelBandwidth(struct hostapd_config *iconf)
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000960{
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800961 wpa_printf(MSG_DEBUG, "getChannelBandwidth %d, isHT=%d, isHT40=%d",
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000962 iconf->vht_oper_chwidth, iconf->ieee80211n,
963 iconf->secondary_channel);
964 switch (iconf->vht_oper_chwidth) {
Sunil8cd6f4d2022-06-28 18:40:46 +0000965 case CONF_OPER_CHWIDTH_80MHZ:
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800966 return ChannelBandwidth::BANDWIDTH_80;
Sunil8cd6f4d2022-06-28 18:40:46 +0000967 case CONF_OPER_CHWIDTH_80P80MHZ:
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800968 return ChannelBandwidth::BANDWIDTH_80P80;
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000969 break;
Sunil8cd6f4d2022-06-28 18:40:46 +0000970 case CONF_OPER_CHWIDTH_160MHZ:
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800971 return ChannelBandwidth::BANDWIDTH_160;
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000972 break;
Sunil8cd6f4d2022-06-28 18:40:46 +0000973 case CONF_OPER_CHWIDTH_USE_HT:
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000974 if (iconf->ieee80211n) {
975 return iconf->secondary_channel != 0 ?
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800976 ChannelBandwidth::BANDWIDTH_40 : ChannelBandwidth::BANDWIDTH_20;
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000977 }
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800978 return ChannelBandwidth::BANDWIDTH_20_NOHT;
Sunil8cd6f4d2022-06-28 18:40:46 +0000979 case CONF_OPER_CHWIDTH_2160MHZ:
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800980 return ChannelBandwidth::BANDWIDTH_2160;
Sunil8cd6f4d2022-06-28 18:40:46 +0000981 case CONF_OPER_CHWIDTH_4320MHZ:
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800982 return ChannelBandwidth::BANDWIDTH_4320;
Sunil8cd6f4d2022-06-28 18:40:46 +0000983 case CONF_OPER_CHWIDTH_6480MHZ:
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800984 return ChannelBandwidth::BANDWIDTH_6480;
Sunil8cd6f4d2022-06-28 18:40:46 +0000985 case CONF_OPER_CHWIDTH_8640MHZ:
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800986 return ChannelBandwidth::BANDWIDTH_8640;
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000987 default:
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800988 return ChannelBandwidth::BANDWIDTH_INVALID;
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000989 }
990}
991
Chris Weir808564b2024-08-07 10:12:16 -0700992std::optional<struct sta_info*> getStaInfoByMacAddr(const struct hostapd_data* iface_hapd,
993 const u8 *mac_addr) {
994 if (iface_hapd == nullptr || mac_addr == nullptr){
995 wpa_printf(MSG_ERROR, "nullptr passsed to getStaInfoByMacAddr!");
996 return std::nullopt;
997 }
998
999 for (struct sta_info* sta_ptr = iface_hapd->sta_list; sta_ptr; sta_ptr = sta_ptr->next) {
1000 int res;
1001 res = memcmp(sta_ptr->addr, mac_addr, ETH_ALEN);
1002 if (res == 0) {
1003 return sta_ptr;
1004 }
1005 }
1006 return std::nullopt;
1007}
1008
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001009bool forceStaDisconnection(struct hostapd_data* hapd,
1010 const std::vector<uint8_t>& client_address,
1011 const uint16_t reason_code) {
Sunil Ravi1a360892022-11-29 20:16:01 +00001012 if (client_address.size() != ETH_ALEN) {
1013 return false;
1014 }
Chris Weir808564b2024-08-07 10:12:16 -07001015
1016 auto sta_ptr_optional = getStaInfoByMacAddr(hapd, client_address.data());
1017 if (sta_ptr_optional.has_value()) {
1018 wpa_printf(MSG_INFO, "Force client:" MACSTR " disconnect with reason: %d",
1019 MAC2STR(client_address.data()), reason_code);
1020 ap_sta_disconnect(hapd, sta_ptr_optional.value(), sta_ptr_optional.value()->addr,
1021 reason_code);
1022 return true;
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001023 }
Chris Weir808564b2024-08-07 10:12:16 -07001024
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001025 return false;
1026}
1027
1028// hostapd core functions accept "C" style function pointers, so use global
1029// functions to pass to the hostapd core function and store the corresponding
1030// std::function methods to be invoked.
1031//
1032// NOTE: Using the pattern from the vendor HAL (wifi_legacy_hal.cpp).
1033//
1034// Callback to be invoked once setup is complete
1035std::function<void(struct hostapd_data*)> on_setup_complete_internal_callback;
1036void onAsyncSetupCompleteCb(void* ctx)
1037{
1038 struct hostapd_data* iface_hapd = (struct hostapd_data*)ctx;
1039 if (on_setup_complete_internal_callback) {
1040 on_setup_complete_internal_callback(iface_hapd);
1041 // Invalidate this callback since we don't want this firing
1042 // again in single AP mode.
1043 if (strlen(iface_hapd->conf->bridge) > 0) {
1044 on_setup_complete_internal_callback = nullptr;
1045 }
1046 }
1047}
1048
1049// Callback to be invoked on hotspot client connection/disconnection
1050std::function<void(struct hostapd_data*, const u8 *mac_addr, int authorized,
1051 const u8 *p2p_dev_addr)> on_sta_authorized_internal_callback;
1052void onAsyncStaAuthorizedCb(void* ctx, const u8 *mac_addr, int authorized,
Sunil Ravid8128a22023-11-06 23:53:58 +00001053 const u8 *p2p_dev_addr, const u8 *ip)
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001054{
1055 struct hostapd_data* iface_hapd = (struct hostapd_data*)ctx;
1056 if (on_sta_authorized_internal_callback) {
1057 on_sta_authorized_internal_callback(iface_hapd, mac_addr,
1058 authorized, p2p_dev_addr);
1059 }
1060}
1061
1062std::function<void(struct hostapd_data*, int level,
1063 enum wpa_msg_type type, const char *txt,
1064 size_t len)> on_wpa_msg_internal_callback;
1065
1066void onAsyncWpaEventCb(void *ctx, int level,
1067 enum wpa_msg_type type, const char *txt,
1068 size_t len)
1069{
1070 struct hostapd_data* iface_hapd = (struct hostapd_data*)ctx;
1071 if (on_wpa_msg_internal_callback) {
1072 on_wpa_msg_internal_callback(iface_hapd, level,
1073 type, txt, len);
1074 }
1075}
1076
1077inline ndk::ScopedAStatus createStatus(HostapdStatusCode status_code) {
1078 return ndk::ScopedAStatus::fromServiceSpecificError(
1079 static_cast<int32_t>(status_code));
1080}
1081
1082inline ndk::ScopedAStatus createStatusWithMsg(
1083 HostapdStatusCode status_code, std::string msg)
1084{
1085 return ndk::ScopedAStatus::fromServiceSpecificErrorWithMessage(
1086 static_cast<int32_t>(status_code), msg.c_str());
1087}
1088
1089// Method called by death_notifier_ on client death.
1090void onDeath(void* cookie) {
1091 wpa_printf(MSG_ERROR, "Client died. Terminating...");
1092 eloop_terminate();
1093}
1094
1095} // namespace
1096
1097namespace aidl {
1098namespace android {
1099namespace hardware {
1100namespace wifi {
1101namespace hostapd {
1102
1103Hostapd::Hostapd(struct hapd_interfaces* interfaces)
1104 : interfaces_(interfaces)
1105{
1106 death_notifier_ = AIBinder_DeathRecipient_new(onDeath);
1107}
1108
1109::ndk::ScopedAStatus Hostapd::addAccessPoint(
1110 const IfaceParams& iface_params, const NetworkParams& nw_params)
1111{
1112 return addAccessPointInternal(iface_params, nw_params);
1113}
1114
1115::ndk::ScopedAStatus Hostapd::removeAccessPoint(const std::string& iface_name)
1116{
1117 return removeAccessPointInternal(iface_name);
1118}
1119
1120::ndk::ScopedAStatus Hostapd::terminate()
1121{
1122 wpa_printf(MSG_INFO, "Terminating...");
1123 // Clear the callback to avoid IPCThreadState shutdown during the
1124 // callback event.
1125 callbacks_.clear();
1126 eloop_terminate();
1127 return ndk::ScopedAStatus::ok();
1128}
1129
1130::ndk::ScopedAStatus Hostapd::registerCallback(
1131 const std::shared_ptr<IHostapdCallback>& callback)
1132{
1133 return registerCallbackInternal(callback);
1134}
1135
1136::ndk::ScopedAStatus Hostapd::forceClientDisconnect(
1137 const std::string& iface_name, const std::vector<uint8_t>& client_address,
1138 Ieee80211ReasonCode reason_code)
1139{
1140 return forceClientDisconnectInternal(iface_name, client_address, reason_code);
1141}
1142
1143::ndk::ScopedAStatus Hostapd::setDebugParams(DebugLevel level)
1144{
1145 return setDebugParamsInternal(level);
1146}
1147
Les Lee8d291ea2024-11-19 22:18:50 +00001148::ndk::ScopedAStatus Hostapd::removeLinkFromMultipleLinkBridgedApIface(
1149 const std::string& iface_name, const std::string& linkIdentity)
1150{
1151 return removeLinkFromMultipleLinkBridgedApIfaceInternal(iface_name, linkIdentity);
1152}
1153
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001154::ndk::ScopedAStatus Hostapd::addAccessPointInternal(
1155 const IfaceParams& iface_params,
1156 const NetworkParams& nw_params)
1157{
1158 int channelParamsSize = iface_params.channelParams.size();
1159 if (channelParamsSize == 1) {
1160 // Single AP
1161 wpa_printf(MSG_INFO, "AddSingleAccessPoint, iface=%s",
1162 iface_params.name.c_str());
1163 return addSingleAccessPoint(iface_params, iface_params.channelParams[0],
Purushottam Kushwaha0316c882021-12-20 15:07:44 +05301164 nw_params, "", "");
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001165 } else if (channelParamsSize == 2) {
1166 // Concurrent APs
1167 wpa_printf(MSG_INFO, "AddDualAccessPoint, iface=%s",
1168 iface_params.name.c_str());
1169 return addConcurrentAccessPoints(iface_params, nw_params);
1170 }
1171 return createStatus(HostapdStatusCode::FAILURE_ARGS_INVALID);
1172}
1173
Purushottam Kushwaha0316c882021-12-20 15:07:44 +05301174std::vector<uint8_t> generateRandomOweSsid()
1175{
1176 u8 random[8] = {0};
1177 os_get_random(random, 8);
1178
1179 std::string ssid = StringPrintf("Owe-%s", random);
1180 wpa_printf(MSG_INFO, "Generated OWE SSID: %s", ssid.c_str());
1181 std::vector<uint8_t> vssid(ssid.begin(), ssid.end());
1182
1183 return vssid;
1184}
1185
Les Lee399c6302024-09-12 03:03:38 +00001186
1187// Both of bridged dual APs and MLO AP will be treated as concurrenct APs.
1188// -----------------------------------------
1189// | br_name | instance#1 | instance#2 |
1190// ___________________________________________________________
1191// bridged dual APs | ap_br_wlanX | wlan X | wlanY |
1192// ___________________________________________________________
1193// MLO AP | wlanX | 0 | 1 |
1194// ___________________________________________________________
1195// Both will be added in br_interfaces_[$br_name] and use instance's name
1196// to be iface_params_new.name to create single Access point.
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001197::ndk::ScopedAStatus Hostapd::addConcurrentAccessPoints(
1198 const IfaceParams& iface_params, const NetworkParams& nw_params)
1199{
1200 int channelParamsListSize = iface_params.channelParams.size();
1201 // Get available interfaces in bridge
Les Lee399c6302024-09-12 03:03:38 +00001202 std::vector<std::string> managed_instances;
1203 std::string br_name = StringPrintf("%s", iface_params.name.c_str());
1204 if (iface_params.usesMlo) {
1205 // MLO AP is using link id as instance.
1206 for (std::size_t i = 0; i < iface_params.instanceIdentities->size(); i++) {
1207 managed_instances.push_back(iface_params.instanceIdentities->at(i)->c_str());
1208 }
1209 } else {
1210 if (!GetInterfacesInBridge(br_name, &managed_instances)) {
1211 return createStatusWithMsg(HostapdStatusCode::FAILURE_UNKNOWN,
1212 "Get interfaces in bridge failed.");
1213 }
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001214 }
Les Lee399c6302024-09-12 03:03:38 +00001215 // Either bridged AP or MLO AP should have two instances.
1216 if (managed_instances.size() < channelParamsListSize) {
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001217 return createStatusWithMsg(HostapdStatusCode::FAILURE_UNKNOWN,
Les Lee399c6302024-09-12 03:03:38 +00001218 "Available interfaces less than requested bands");
1219 }
1220
1221 if (iface_params.usesMlo
1222 && nw_params.encryptionType == EncryptionType::WPA3_OWE_TRANSITION) {
1223 return createStatusWithMsg(HostapdStatusCode::FAILURE_UNKNOWN,
1224 "Invalid encryptionType (OWE transition) for MLO SAP.");
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001225 }
1226 // start BSS on specified bands
1227 for (std::size_t i = 0; i < channelParamsListSize; i ++) {
1228 IfaceParams iface_params_new = iface_params;
Purushottam Kushwaha0316c882021-12-20 15:07:44 +05301229 NetworkParams nw_params_new = nw_params;
Purushottam Kushwaha0316c882021-12-20 15:07:44 +05301230 std::string owe_transition_ifname = "";
Les Lee399c6302024-09-12 03:03:38 +00001231 iface_params_new.name = managed_instances[i];
Ahmed ElArabawy1aaf1802022-02-04 15:58:55 -08001232 if (nw_params.encryptionType == EncryptionType::WPA3_OWE_TRANSITION) {
Purushottam Kushwaha0316c882021-12-20 15:07:44 +05301233 if (i == 0 && i+1 < channelParamsListSize) {
Les Lee399c6302024-09-12 03:03:38 +00001234 owe_transition_ifname = managed_instances[i+1];
Purushottam Kushwaha0316c882021-12-20 15:07:44 +05301235 nw_params_new.encryptionType = EncryptionType::NONE;
1236 } else {
Les Lee399c6302024-09-12 03:03:38 +00001237 owe_transition_ifname = managed_instances[0];
Purushottam Kushwaha0316c882021-12-20 15:07:44 +05301238 nw_params_new.isHidden = true;
1239 nw_params_new.ssid = generateRandomOweSsid();
1240 }
1241 }
1242
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001243 ndk::ScopedAStatus status = addSingleAccessPoint(
Purushottam Kushwaha0316c882021-12-20 15:07:44 +05301244 iface_params_new, iface_params.channelParams[i], nw_params_new,
1245 br_name, owe_transition_ifname);
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001246 if (!status.isOk()) {
1247 wpa_printf(MSG_ERROR, "Failed to addAccessPoint %s",
Les Lee399c6302024-09-12 03:03:38 +00001248 managed_instances[i].c_str());
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001249 return status;
1250 }
1251 }
Les Lee399c6302024-09-12 03:03:38 +00001252
1253 if (iface_params.usesMlo) {
1254 std::size_t i = 0;
1255 std::size_t j = 0;
1256 for (i = 0; i < interfaces_->count; i++) {
1257 struct hostapd_iface *iface = interfaces_->iface[i];
1258
1259 for (j = 0; j < iface->num_bss; j++) {
1260 struct hostapd_data *iface_hapd = iface->bss[j];
1261 if (hostapd_enable_iface(iface_hapd->iface) < 0) {
1262 wpa_printf(
1263 MSG_ERROR, "Enabling interface %s failed on %zu",
1264 iface_params.name.c_str(), i);
1265 return createStatus(HostapdStatusCode::FAILURE_UNKNOWN);
1266 }
1267 }
1268 }
1269 }
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001270 // Save bridge interface info
Les Lee399c6302024-09-12 03:03:38 +00001271 br_interfaces_[br_name] = managed_instances;
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001272 return ndk::ScopedAStatus::ok();
1273}
1274
Les Lee399c6302024-09-12 03:03:38 +00001275struct hostapd_data * hostapd_get_iface_by_link_id(struct hapd_interfaces *interfaces,
1276 const size_t link_id)
1277{
1278#ifdef CONFIG_IEEE80211BE
1279 size_t i, j;
1280
1281 for (i = 0; i < interfaces->count; i++) {
1282 struct hostapd_iface *iface = interfaces->iface[i];
1283
1284 for (j = 0; j < iface->num_bss; j++) {
1285 struct hostapd_data *hapd = iface->bss[j];
1286
1287 if (link_id == hapd->mld_link_id)
1288 return hapd;
1289 }
1290 }
Les Lee16bb41b2024-12-03 01:50:22 +00001291#endif /* CONFIG_IEEE80211BE */
Les Lee399c6302024-09-12 03:03:38 +00001292 return NULL;
1293}
1294
1295// Both of bridged dual APs and MLO AP will be treated as concurrenct APs.
1296// -----------------------------------------
1297// | br_name | iface_params.name
1298// _______________________________________________________________
1299// bridged dual APs | bridged interface name | interface name
1300// _______________________________________________________________
1301// MLO AP | AP interface name | mld link id as instance name
1302// _______________________________________________________________
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001303::ndk::ScopedAStatus Hostapd::addSingleAccessPoint(
1304 const IfaceParams& iface_params,
1305 const ChannelParams& channelParams,
1306 const NetworkParams& nw_params,
Purushottam Kushwaha0316c882021-12-20 15:07:44 +05301307 const std::string br_name,
1308 const std::string owe_transition_ifname)
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001309{
Les Lee399c6302024-09-12 03:03:38 +00001310 if (iface_params.usesMlo) { // the mlo case, iface name is instance name which is mld_link_id
Xin Deng62e2e842024-12-20 01:40:20 -08001311 if (hostapd_get_iface_by_link_id(interfaces_, std::stoi(iface_params.name.c_str()))) {
Les Lee399c6302024-09-12 03:03:38 +00001312 wpa_printf(
1313 MSG_ERROR, "Instance link id %s already present",
1314 iface_params.name.c_str());
1315 return createStatus(HostapdStatusCode::FAILURE_IFACE_EXISTS);
1316 }
Les Leeb6386ef2024-12-20 23:26:12 +00001317#ifdef CONFIG_IEEE80211BE
1318 // The MLO AP uses the same interface name for all links. Thus, make sure the
1319 // interface name wasn't used for non-mld AP only when adding a new interface.
1320 // Also it is valid to have a hostapd_data with the same interface name when adding
1321 // the second link instance.
1322 struct hostapd_data* hapd = hostapd_get_iface(interfaces_, br_name.c_str());
1323 if (hapd && !hapd->conf->mld_ap) {
1324 wpa_printf(
1325 MSG_ERROR, "Instance interface %s already present",
1326 br_name.c_str());
1327 return createStatus(HostapdStatusCode::FAILURE_IFACE_EXISTS);
1328 }
1329#endif
1330 } else {
1331 if (hostapd_get_iface(interfaces_, iface_params.name.c_str())) {
1332 wpa_printf(
1333 MSG_ERROR, "Instance interface %s already present",
1334 iface_params.name.c_str());
1335 return createStatus(HostapdStatusCode::FAILURE_IFACE_EXISTS);
1336 }
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001337 }
Purushottam Kushwaha0316c882021-12-20 15:07:44 +05301338 const auto conf_params = CreateHostapdConfig(iface_params, channelParams, nw_params,
1339 br_name, owe_transition_ifname);
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001340 if (conf_params.empty()) {
1341 wpa_printf(MSG_ERROR, "Failed to create config params");
1342 return createStatus(HostapdStatusCode::FAILURE_ARGS_INVALID);
1343 }
1344 const auto conf_file_path =
Les Lee399c6302024-09-12 03:03:38 +00001345 WriteHostapdConfig(iface_params.name, conf_params, br_name, iface_params.usesMlo);
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001346 if (conf_file_path.empty()) {
1347 wpa_printf(MSG_ERROR, "Failed to write config file");
1348 return createStatus(HostapdStatusCode::FAILURE_UNKNOWN);
1349 }
1350 std::string add_iface_param_str = StringPrintf(
Les Lee399c6302024-09-12 03:03:38 +00001351 "%s config=%s", iface_params.usesMlo ? br_name.c_str(): iface_params.name.c_str(),
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001352 conf_file_path.c_str());
1353 std::vector<char> add_iface_param_vec(
1354 add_iface_param_str.begin(), add_iface_param_str.end() + 1);
1355 if (hostapd_add_iface(interfaces_, add_iface_param_vec.data()) < 0) {
1356 wpa_printf(
Les Lee399c6302024-09-12 03:03:38 +00001357 MSG_ERROR, "Adding hostapd iface %s failed",
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001358 add_iface_param_str.c_str());
1359 return createStatus(HostapdStatusCode::FAILURE_UNKNOWN);
1360 }
Les Lee399c6302024-09-12 03:03:38 +00001361
1362 // find the iface and set up callback.
1363 struct hostapd_data* iface_hapd = iface_params.usesMlo ?
Xin Deng62e2e842024-12-20 01:40:20 -08001364 hostapd_get_iface_by_link_id(interfaces_, std::stoi(iface_params.name.c_str())) :
Les Lee399c6302024-09-12 03:03:38 +00001365 hostapd_get_iface(interfaces_, iface_params.name.c_str());
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001366 WPA_ASSERT(iface_hapd != nullptr && iface_hapd->iface != nullptr);
Les Lee399c6302024-09-12 03:03:38 +00001367 if (iface_params.usesMlo) {
1368 memcmp(iface_hapd->conf->iface, br_name.c_str(), br_name.size());
1369 }
1370
1371 // Callback discrepancy between bridged dual APs and MLO AP
1372 // Note: Only bridged dual APs will have "iface_hapd->conf->bridge" and
1373 // Only MLO AP will have "iface_hapd->mld_link_id"
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001374 // Register the setup complete callbacks
Les Lee399c6302024-09-12 03:03:38 +00001375 // -----------------------------------------
1376 // | bridged dual APs | bridged single link MLO | MLO SAP
1377 // _________________________________________________________________________________________
1378 // hapd->conf->bridge | bridged interface name | bridged interface nam | N/A
1379 // _________________________________________________________________________________________
1380 // hapd->conf->iface | AP interface name | AP interface name | AP interface name
1381 // _________________________________________________________________________________________
1382 // hapd->mld_link_id | 0 (default value) | link id (0) | link id (0 or 1)
1383 // _________________________________________________________________________________________
1384 // hapd->mld_ap | 0 | 1 | 1
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001385 on_setup_complete_internal_callback =
1386 [this](struct hostapd_data* iface_hapd) {
1387 wpa_printf(
1388 MSG_INFO, "AP interface setup completed - state %s",
1389 hostapd_state_text(iface_hapd->iface->state));
1390 if (iface_hapd->iface->state == HAPD_IFACE_DISABLED) {
1391 // Invoke the failure callback on all registered
1392 // clients.
Les Lee399c6302024-09-12 03:03:38 +00001393 std::string instanceName = iface_hapd->conf->iface;
1394#ifdef CONFIG_IEEE80211BE
1395 if (iface_hapd->conf->mld_ap
1396 && strlen(iface_hapd->conf->bridge) == 0) {
1397 instanceName = std::to_string(iface_hapd->mld_link_id);
1398 }
Les Lee16bb41b2024-12-03 01:50:22 +00001399#endif /* CONFIG_IEEE80211BE */
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001400 for (const auto& callback : callbacks_) {
Chenming Huangbaa5e582024-04-02 08:21:10 +05301401 auto status = callback->onFailure(
1402 strlen(iface_hapd->conf->bridge) > 0 ?
Les Leee08c2862021-10-29 16:36:41 +08001403 iface_hapd->conf->bridge : iface_hapd->conf->iface,
Les Lee399c6302024-09-12 03:03:38 +00001404 instanceName);
Chenming Huangbaa5e582024-04-02 08:21:10 +05301405 if (!status.isOk()) {
1406 wpa_printf(MSG_ERROR, "Failed to invoke onFailure");
1407 }
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001408 }
1409 }
1410 };
1411
1412 // Register for new client connect/disconnect indication.
1413 on_sta_authorized_internal_callback =
1414 [this](struct hostapd_data* iface_hapd, const u8 *mac_addr,
1415 int authorized, const u8 *p2p_dev_addr) {
1416 wpa_printf(MSG_DEBUG, "notify client " MACSTR " %s",
1417 MAC2STR(mac_addr),
1418 (authorized) ? "Connected" : "Disconnected");
1419 ClientInfo info;
1420 info.ifaceName = strlen(iface_hapd->conf->bridge) > 0 ?
1421 iface_hapd->conf->bridge : iface_hapd->conf->iface;
Les Lee399c6302024-09-12 03:03:38 +00001422 std::string instanceName = iface_hapd->conf->iface;
1423#ifdef CONFIG_IEEE80211BE
1424 if (iface_hapd->conf->mld_ap
1425 && strlen(iface_hapd->conf->bridge) == 0) {
1426 instanceName = std::to_string(iface_hapd->mld_link_id);
1427 }
Les Lee16bb41b2024-12-03 01:50:22 +00001428#endif /* CONFIG_IEEE80211BE */
Les Lee399c6302024-09-12 03:03:38 +00001429 info.apIfaceInstance = instanceName;
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001430 info.clientAddress.assign(mac_addr, mac_addr + ETH_ALEN);
1431 info.isConnected = authorized;
Chris Weir808564b2024-08-07 10:12:16 -07001432 if(isAidlServiceVersionAtLeast(3) && !authorized) {
1433 u16 disconnect_reason_code = WLAN_REASON_UNSPECIFIED;
1434 auto sta_ptr_optional = getStaInfoByMacAddr(iface_hapd, mac_addr);
1435 if (sta_ptr_optional.has_value()){
1436 disconnect_reason_code = sta_ptr_optional.value()->deauth_reason;
1437 }
1438 info.disconnectReasonCode =
1439 static_cast<common::DeauthenticationReasonCode>(disconnect_reason_code);
1440 }
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001441 for (const auto &callback : callbacks_) {
Chenming Huangbaa5e582024-04-02 08:21:10 +05301442 auto status = callback->onConnectedClientsChanged(info);
1443 if (!status.isOk()) {
1444 wpa_printf(MSG_ERROR, "Failed to invoke onConnectedClientsChanged");
1445 }
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001446 }
1447 };
1448
1449 // Register for wpa_event which used to get channel switch event
1450 on_wpa_msg_internal_callback =
1451 [this](struct hostapd_data* iface_hapd, int level,
1452 enum wpa_msg_type type, const char *txt,
1453 size_t len) {
1454 wpa_printf(MSG_DEBUG, "Receive wpa msg : %s", txt);
1455 if (os_strncmp(txt, AP_EVENT_ENABLED,
1456 strlen(AP_EVENT_ENABLED)) == 0 ||
1457 os_strncmp(txt, WPA_EVENT_CHANNEL_SWITCH,
1458 strlen(WPA_EVENT_CHANNEL_SWITCH)) == 0) {
Les Lee399c6302024-09-12 03:03:38 +00001459 std::string instanceName = iface_hapd->conf->iface;
1460#ifdef CONFIG_IEEE80211BE
1461 if (iface_hapd->conf->mld_ap && strlen(iface_hapd->conf->bridge) == 0) {
1462 instanceName = std::to_string(iface_hapd->mld_link_id);
1463 }
Les Lee16bb41b2024-12-03 01:50:22 +00001464#endif /* CONFIG_IEEE80211BE */
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001465 ApInfo info;
1466 info.ifaceName = strlen(iface_hapd->conf->bridge) > 0 ?
1467 iface_hapd->conf->bridge : iface_hapd->conf->iface,
Les Lee399c6302024-09-12 03:03:38 +00001468 info.apIfaceInstance = instanceName;
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001469 info.freqMhz = iface_hapd->iface->freq;
Ahmed ElArabawyb4115792022-02-08 09:33:01 -08001470 info.channelBandwidth = getChannelBandwidth(iface_hapd->iconf);
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001471 info.generation = getGeneration(iface_hapd->iface->current_mode);
1472 info.apIfaceInstanceMacAddress.assign(iface_hapd->own_addr,
1473 iface_hapd->own_addr + ETH_ALEN);
Les Lee16bb41b2024-12-03 01:50:22 +00001474#ifdef CONFIG_IEEE80211BE
1475 if (iface_hapd->conf->mld_ap) {
1476 info.mldMacAddress = macAddrToArray(iface_hapd->mld->mld_addr);
1477 }
1478#endif /* CONFIG_IEEE80211BE */
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001479 for (const auto &callback : callbacks_) {
Chenming Huangbaa5e582024-04-02 08:21:10 +05301480 auto status = callback->onApInstanceInfoChanged(info);
1481 if (!status.isOk()) {
1482 wpa_printf(MSG_ERROR,
1483 "Failed to invoke onApInstanceInfoChanged");
1484 }
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001485 }
Les Leea0c90cb2022-04-19 17:39:23 +08001486 } else if (os_strncmp(txt, AP_EVENT_DISABLED, strlen(AP_EVENT_DISABLED)) == 0
1487 || os_strncmp(txt, INTERFACE_DISABLED, strlen(INTERFACE_DISABLED)) == 0)
1488 {
Les Lee399c6302024-09-12 03:03:38 +00001489 std::string instanceName = iface_hapd->conf->iface;
1490#ifdef CONFIG_IEEE80211BE
1491 if (iface_hapd->conf->mld_ap && strlen(iface_hapd->conf->bridge) == 0) {
1492 instanceName = std::to_string(iface_hapd->mld_link_id);
1493 }
Les Lee16bb41b2024-12-03 01:50:22 +00001494#endif /* CONFIG_IEEE80211BE */
Yu Ouyang378d3c42021-08-20 17:31:08 +08001495 // Invoke the failure callback on all registered clients.
1496 for (const auto& callback : callbacks_) {
Les Lee399c6302024-09-12 03:03:38 +00001497 auto status =
1498 callback->onFailure(strlen(iface_hapd->conf->bridge) > 0 ?
Les Leee08c2862021-10-29 16:36:41 +08001499 iface_hapd->conf->bridge : iface_hapd->conf->iface,
Les Lee399c6302024-09-12 03:03:38 +00001500 instanceName);
Chenming Huangbaa5e582024-04-02 08:21:10 +05301501 if (!status.isOk()) {
1502 wpa_printf(MSG_ERROR, "Failed to invoke onFailure");
1503 }
Yu Ouyang378d3c42021-08-20 17:31:08 +08001504 }
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001505 }
Yu Ouyang378d3c42021-08-20 17:31:08 +08001506 };
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001507
1508 // Setup callback
1509 iface_hapd->setup_complete_cb = onAsyncSetupCompleteCb;
1510 iface_hapd->setup_complete_cb_ctx = iface_hapd;
1511 iface_hapd->sta_authorized_cb = onAsyncStaAuthorizedCb;
1512 iface_hapd->sta_authorized_cb_ctx = iface_hapd;
Hu Wang7c5a4322021-06-24 17:24:59 +08001513 wpa_msg_register_aidl_cb(onAsyncWpaEventCb);
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001514
Les Lee399c6302024-09-12 03:03:38 +00001515 // Multi-link MLO should enable iface after both links have been set.
1516 if (!iface_params.usesMlo && hostapd_enable_iface(iface_hapd->iface) < 0) {
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001517 wpa_printf(
1518 MSG_ERROR, "Enabling interface %s failed",
1519 iface_params.name.c_str());
1520 return createStatus(HostapdStatusCode::FAILURE_UNKNOWN);
1521 }
1522 return ndk::ScopedAStatus::ok();
1523}
1524
1525::ndk::ScopedAStatus Hostapd::removeAccessPointInternal(const std::string& iface_name)
1526{
1527 // interfaces to be removed
1528 std::vector<std::string> interfaces;
1529 bool is_error = false;
1530
1531 const auto it = br_interfaces_.find(iface_name);
1532 if (it != br_interfaces_.end()) {
1533 // In case bridge, remove managed interfaces
1534 interfaces = it->second;
1535 br_interfaces_.erase(iface_name);
1536 } else {
1537 // else remove current interface
1538 interfaces.push_back(iface_name);
1539 }
1540
1541 for (auto& iface : interfaces) {
1542 std::vector<char> remove_iface_param_vec(
1543 iface.begin(), iface.end() + 1);
1544 if (hostapd_remove_iface(interfaces_, remove_iface_param_vec.data()) < 0) {
1545 wpa_printf(MSG_INFO, "Remove interface %s failed", iface.c_str());
1546 is_error = true;
1547 }
1548 }
1549 if (is_error) {
1550 return createStatus(HostapdStatusCode::FAILURE_UNKNOWN);
1551 }
1552 return ndk::ScopedAStatus::ok();
1553}
1554
1555::ndk::ScopedAStatus Hostapd::registerCallbackInternal(
1556 const std::shared_ptr<IHostapdCallback>& callback)
1557{
1558 binder_status_t status = AIBinder_linkToDeath(callback->asBinder().get(),
1559 death_notifier_, this /* cookie */);
1560 if (status != STATUS_OK) {
1561 wpa_printf(
1562 MSG_ERROR,
1563 "Error registering for death notification for "
1564 "hostapd callback object");
1565 return createStatus(HostapdStatusCode::FAILURE_UNKNOWN);
1566 }
1567 callbacks_.push_back(callback);
Manaswini Paluri76ae6982024-02-29 14:49:20 +05301568 if (aidl_service_version == 0) {
1569 aidl_service_version = Hostapd::version;
1570 wpa_printf(MSG_INFO, "AIDL service version: %d", aidl_service_version);
1571 }
1572 if (aidl_client_version == 0) {
1573 callback->getInterfaceVersion(&aidl_client_version);
1574 wpa_printf(MSG_INFO, "AIDL client version: %d", aidl_client_version);
1575 }
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001576 return ndk::ScopedAStatus::ok();
1577}
1578
1579::ndk::ScopedAStatus Hostapd::forceClientDisconnectInternal(const std::string& iface_name,
1580 const std::vector<uint8_t>& client_address, Ieee80211ReasonCode reason_code)
1581{
1582 struct hostapd_data *hapd = hostapd_get_iface(interfaces_, iface_name.c_str());
1583 bool result;
1584 if (!hapd) {
1585 for (auto const& iface : br_interfaces_) {
1586 if (iface.first == iface_name) {
1587 for (auto const& instance : iface.second) {
1588 hapd = hostapd_get_iface(interfaces_, instance.c_str());
1589 if (hapd) {
1590 result = forceStaDisconnection(hapd, client_address,
1591 (uint16_t) reason_code);
1592 if (result) break;
1593 }
1594 }
1595 }
1596 }
1597 } else {
1598 result = forceStaDisconnection(hapd, client_address, (uint16_t) reason_code);
1599 }
1600 if (!hapd) {
1601 wpa_printf(MSG_ERROR, "Interface %s doesn't exist", iface_name.c_str());
1602 return createStatus(HostapdStatusCode::FAILURE_IFACE_UNKNOWN);
1603 }
1604 if (result) {
1605 return ndk::ScopedAStatus::ok();
1606 }
1607 return createStatus(HostapdStatusCode::FAILURE_CLIENT_UNKNOWN);
1608}
1609
1610::ndk::ScopedAStatus Hostapd::setDebugParamsInternal(DebugLevel level)
1611{
1612 wpa_debug_level = static_cast<uint32_t>(level);
1613 return ndk::ScopedAStatus::ok();
1614}
1615
Les Lee8d291ea2024-11-19 22:18:50 +00001616::ndk::ScopedAStatus Hostapd::removeLinkFromMultipleLinkBridgedApIfaceInternal(
1617const std::string& iface_name, const std::string& linkIdentity)
1618{
Les Lee16bb41b2024-12-03 01:50:22 +00001619#ifdef CONFIG_IEEE80211BE
Les Lee8d291ea2024-11-19 22:18:50 +00001620 if (!hostapd_get_iface(interfaces_, iface_name.c_str())) {
1621 wpa_printf(MSG_ERROR, "Interface %s doesn't exist", iface_name.c_str());
1622 return createStatus(HostapdStatusCode::FAILURE_IFACE_UNKNOWN);
1623 }
1624 struct hostapd_data* iface_hapd =
Xin Deng62e2e842024-12-20 01:40:20 -08001625 hostapd_get_iface_by_link_id(interfaces_, std::stoi(linkIdentity.c_str()));
Les Lee8d291ea2024-11-19 22:18:50 +00001626 if (iface_hapd) {
Les Lee16bb41b2024-12-03 01:50:22 +00001627// Currently, hostapd_link_remove is still under CONFIG_TESTING_OPTIONS.
1628// TODO: b/340821197 - Make sure to take out the hostapd_link_remove() and other related code
1629// out of CONFIG_TESTING_OPTIONS.
1630#ifdef CONFIG_TESTING_OPTIONS
Les Lee8d291ea2024-11-19 22:18:50 +00001631 if (0 == hostapd_link_remove(iface_hapd, 1)) {
1632 return ndk::ScopedAStatus::ok();
1633 }
Les Lee16bb41b2024-12-03 01:50:22 +00001634#endif /* CONFIG_TESTING_OPTIONS */
Les Lee8d291ea2024-11-19 22:18:50 +00001635 }
1636 return createStatus(HostapdStatusCode::FAILURE_ARGS_INVALID);
Les Lee16bb41b2024-12-03 01:50:22 +00001637#endif /* CONFIG_IEEE80211BE */
1638 return createStatus(HostapdStatusCode::FAILURE_UNKNOWN);
Les Lee8d291ea2024-11-19 22:18:50 +00001639}
1640
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001641} // namespace hostapd
1642} // namespace wifi
1643} // namespace hardware
1644} // namespace android
Les Leee08c2862021-10-29 16:36:41 +08001645} // namespace aidl