blob: 58963cb3108d6d04f70b0926137812c22d3debad [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
141/**
142 * Check that the AIDL service is running at least the expected version.
143 * Use to avoid the case where the AIDL interface version
144 * is greater than the version implemented by the service.
145 */
146inline int32_t isAidlServiceVersionAtLeast(int32_t expected_version)
147{
148 return expected_version <= aidl_service_version;
149}
150
151inline int32_t isAidlClientVersionAtLeast(int32_t expected_version)
152{
153 return expected_version <= aidl_client_version;
154}
155
Sunil Ravide0e6bf2024-10-14 04:58:16 +0000156inline int32_t areAidlServiceAndClientAtLeastVersion(int32_t expected_version)
157{
158 return isAidlServiceVersionAtLeast(expected_version)
159 && isAidlClientVersionAtLeast(expected_version);
160}
161
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000162#define MAX_PORTS 1024
163bool GetInterfacesInBridge(std::string br_name,
164 std::vector<std::string>* interfaces) {
165 android::base::unique_fd sock(socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0));
166 if (sock.get() < 0) {
167 wpa_printf(MSG_ERROR, "Failed to create sock (%s) in %s",
168 strerror(errno), __FUNCTION__);
169 return false;
170 }
171
172 struct ifreq request;
173 int i, ifindices[MAX_PORTS];
174 char if_name[IFNAMSIZ];
175 unsigned long args[3];
176
177 memset(ifindices, 0, MAX_PORTS * sizeof(int));
178
179 args[0] = BRCTL_GET_PORT_LIST;
180 args[1] = (unsigned long) ifindices;
181 args[2] = MAX_PORTS;
182
183 strlcpy(request.ifr_name, br_name.c_str(), IFNAMSIZ);
184 request.ifr_data = (char *)args;
185
186 if (ioctl(sock.get(), SIOCDEVPRIVATE, &request) < 0) {
187 wpa_printf(MSG_ERROR, "Failed to ioctl SIOCDEVPRIVATE in %s",
188 __FUNCTION__);
189 return false;
190 }
191
192 for (i = 0; i < MAX_PORTS; i ++) {
193 memset(if_name, 0, IFNAMSIZ);
194 if (ifindices[i] == 0 || !if_indextoname(ifindices[i], if_name)) {
195 continue;
196 }
197 interfaces->push_back(if_name);
198 }
199 return true;
200}
201
Chris Weird41879a2024-10-30 15:53:24 -0700202std::string resolveVendorConfPath(const std::string& conf_path)
203{
204#if defined(__ANDROID_APEX__)
205 // returns "/apex/<apexname>" + conf_path
206 std::string path = android::base::GetExecutablePath();
207 return path.substr(0, path.find_first_of('/', strlen("/apex/"))) + conf_path;
208#else
209 return std::string("/vendor") + conf_path;
210#endif
211}
212
213void logHostapdConfigError(int error, const std::string& file_path) {
214 wpa_printf(MSG_ERROR, "Cannot read/write hostapd config %s, error: %s", file_path.c_str(),
215 strerror(error));
216 struct stat st;
217 int result = stat(file_path.c_str(), &st);
218 if (result == 0) {
219 wpa_printf(MSG_ERROR, "hostapd config file uid: %d, gid: %d, mode: %d",st.st_uid,
220 st.st_gid, st.st_mode);
221 } else {
222 wpa_printf(MSG_ERROR, "Error calling stat() on hostapd config file: %s",
223 strerror(errno));
224 }
225}
226
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000227std::string WriteHostapdConfig(
Les Lee399c6302024-09-12 03:03:38 +0000228 const std::string& instance_name, const std::string& config,
229 const std::string br_name, const bool usesMlo)
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000230{
Les Lee399c6302024-09-12 03:03:38 +0000231 std::string conf_name_as_string = instance_name;
232 if (usesMlo) {
233 conf_name_as_string = StringPrintf(
234 "%s-%s", br_name.c_str(), instance_name.c_str());
235 }
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000236 const std::string file_path =
Les Lee399c6302024-09-12 03:03:38 +0000237 StringPrintf(kConfFileNameFmt, conf_name_as_string.c_str());
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000238 if (WriteStringToFile(
239 config, file_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
240 getuid(), getgid())) {
241 return file_path;
242 }
243 // Diagnose failure
244 int error = errno;
Chris Weird41879a2024-10-30 15:53:24 -0700245 logHostapdConfigError(errno, file_path);
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000246 return "";
247}
248
249/*
250 * Get the op_class for a channel/band
251 * The logic here is based on Table E-4 in the 802.11 Specification
252 */
253int getOpClassForChannel(int channel, int band, bool support11n, bool support11ac) {
254 // 2GHz Band
255 if ((band & band2Ghz) != 0) {
256 if (channel == 14) {
257 return 82;
258 }
259 if (channel >= 1 && channel <= 13) {
260 if (!support11n) {
261 //20MHz channel
262 return 81;
263 }
264 if (channel <= 9) {
265 // HT40 with secondary channel above primary
266 return 83;
267 }
268 // HT40 with secondary channel below primary
269 return 84;
270 }
271 // Error
272 return 0;
273 }
274
275 // 5GHz Band
276 if ((band & band5Ghz) != 0) {
277 if (support11ac) {
278 switch (channel) {
279 case 42:
280 case 58:
281 case 106:
282 case 122:
283 case 138:
284 case 155:
285 // 80MHz channel
286 return 128;
287 case 50:
288 case 114:
289 // 160MHz channel
290 return 129;
291 }
292 }
293
294 if (!support11n) {
295 if (channel >= 36 && channel <= 48) {
296 return 115;
297 }
298 if (channel >= 52 && channel <= 64) {
299 return 118;
300 }
301 if (channel >= 100 && channel <= 144) {
302 return 121;
303 }
304 if (channel >= 149 && channel <= 161) {
305 return 124;
306 }
307 if (channel >= 165 && channel <= 169) {
308 return 125;
309 }
310 } else {
311 switch (channel) {
312 case 36:
313 case 44:
314 // HT40 with secondary channel above primary
315 return 116;
316 case 40:
317 case 48:
318 // HT40 with secondary channel below primary
319 return 117;
320 case 52:
321 case 60:
322 // HT40 with secondary channel above primary
323 return 119;
324 case 56:
325 case 64:
326 // HT40 with secondary channel below primary
327 return 120;
328 case 100:
329 case 108:
330 case 116:
331 case 124:
332 case 132:
333 case 140:
334 // HT40 with secondary channel above primary
335 return 122;
336 case 104:
337 case 112:
338 case 120:
339 case 128:
340 case 136:
341 case 144:
342 // HT40 with secondary channel below primary
343 return 123;
344 case 149:
345 case 157:
346 // HT40 with secondary channel above primary
347 return 126;
348 case 153:
349 case 161:
350 // HT40 with secondary channel below primary
351 return 127;
352 }
353 }
354 // Error
355 return 0;
356 }
357
358 // 6GHz Band
359 if ((band & band6Ghz) != 0) {
360 // Channels 1, 5. 9, 13, ...
361 if ((channel & 0x03) == 0x01) {
362 // 20MHz channel
363 return 131;
364 }
365 // Channels 3, 11, 19, 27, ...
366 if ((channel & 0x07) == 0x03) {
367 // 40MHz channel
368 return 132;
369 }
370 // Channels 7, 23, 39, 55, ...
371 if ((channel & 0x0F) == 0x07) {
372 // 80MHz channel
373 return 133;
374 }
375 // Channels 15, 47, 69, ...
376 if ((channel & 0x1F) == 0x0F) {
377 // 160MHz channel
378 return 134;
379 }
380 if (channel == 2) {
381 // 20MHz channel
382 return 136;
383 }
384 // Error
385 return 0;
386 }
387
388 if ((band & band60Ghz) != 0) {
389 if (1 <= channel && channel <= 8) {
390 return 180;
391 } else if (9 <= channel && channel <= 15) {
392 return 181;
393 } else if (17 <= channel && channel <= 22) {
394 return 182;
395 } else if (25 <= channel && channel <= 29) {
396 return 183;
397 }
398 // Error
399 return 0;
400 }
401
402 return 0;
403}
404
405bool validatePassphrase(int passphrase_len, int min_len, int max_len)
406{
407 if (min_len != -1 && passphrase_len < min_len) return false;
408 if (max_len != -1 && passphrase_len > max_len) return false;
409 return true;
410}
411
Manaswini Paluri76ae6982024-02-29 14:49:20 +0530412std::string getInterfaceMacAddress(const std::string& if_name)
413{
414 u8 addr[ETH_ALEN] = {};
415 struct ifreq ifr;
416 std::string mac_addr;
417
418 android::base::unique_fd sock(socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0));
419 if (sock.get() < 0) {
420 wpa_printf(MSG_ERROR, "Failed to create sock (%s) in %s",
421 strerror(errno), __FUNCTION__);
422 return "";
423 }
424
425 memset(&ifr, 0, sizeof(ifr));
426 strlcpy(ifr.ifr_name, if_name.c_str(), IFNAMSIZ);
427 if (ioctl(sock.get(), SIOCGIFHWADDR, &ifr) < 0) {
428 wpa_printf(MSG_ERROR, "Could not get interface %s hwaddr: %s",
429 if_name.c_str(), strerror(errno));
430 return "";
431 }
432
433 memcpy(addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
434 mac_addr = StringPrintf("" MACSTR, MAC2STR(addr));
435
436 return mac_addr;
437}
438
Chris Weird41879a2024-10-30 15:53:24 -0700439std::string trimWhitespace(const std::string& str) {
440 size_t pos = 0;
441 size_t len = str.size();
442 for (pos; pos < str.size() && std::isspace(str[pos]); ++pos){}
443 for (len; len - 1 > 0 && std::isspace(str[len-1]); --len){}
444 return str.substr(pos, len);
445}
446
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000447std::string CreateHostapdConfig(
448 const IfaceParams& iface_params,
449 const ChannelParams& channelParams,
450 const NetworkParams& nw_params,
Purushottam Kushwaha0316c882021-12-20 15:07:44 +0530451 const std::string br_name,
452 const std::string owe_transition_ifname)
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000453{
454 if (nw_params.ssid.size() >
455 static_cast<uint32_t>(
456 ParamSizeLimits::SSID_MAX_LEN_IN_BYTES)) {
457 wpa_printf(
458 MSG_ERROR, "Invalid SSID size: %zu", nw_params.ssid.size());
459 return "";
460 }
461
462 // SSID string
463 std::stringstream ss;
464 ss << std::hex;
465 ss << std::setfill('0');
466 for (uint8_t b : nw_params.ssid) {
467 ss << std::setw(2) << static_cast<unsigned int>(b);
468 }
469 const std::string ssid_as_string = ss.str();
470
471 // Encryption config string
472 uint32_t band = 0;
473 band |= static_cast<uint32_t>(channelParams.bandMask);
Sunil Ravi4fc918f2022-04-17 09:26:48 -0700474 bool is_2Ghz_band_only = band == static_cast<uint32_t>(band2Ghz);
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000475 bool is_6Ghz_band_only = band == static_cast<uint32_t>(band6Ghz);
476 bool is_60Ghz_band_only = band == static_cast<uint32_t>(band60Ghz);
477 std::string encryption_config_as_string;
478 switch (nw_params.encryptionType) {
479 case EncryptionType::NONE:
480 // no security params
481 break;
482 case EncryptionType::WPA:
483 if (!validatePassphrase(
484 nw_params.passphrase.size(),
485 static_cast<uint32_t>(ParamSizeLimits::
486 WPA2_PSK_PASSPHRASE_MIN_LEN_IN_BYTES),
487 static_cast<uint32_t>(ParamSizeLimits::
488 WPA2_PSK_PASSPHRASE_MAX_LEN_IN_BYTES))) {
489 return "";
490 }
491 encryption_config_as_string = StringPrintf(
492 "wpa=3\n"
493 "wpa_pairwise=%s\n"
494 "wpa_passphrase=%s",
495 is_60Ghz_band_only ? "GCMP" : "TKIP CCMP",
496 nw_params.passphrase.c_str());
497 break;
498 case EncryptionType::WPA2:
499 if (!validatePassphrase(
500 nw_params.passphrase.size(),
501 static_cast<uint32_t>(ParamSizeLimits::
502 WPA2_PSK_PASSPHRASE_MIN_LEN_IN_BYTES),
503 static_cast<uint32_t>(ParamSizeLimits::
504 WPA2_PSK_PASSPHRASE_MAX_LEN_IN_BYTES))) {
505 return "";
506 }
507 encryption_config_as_string = StringPrintf(
508 "wpa=2\n"
509 "rsn_pairwise=%s\n"
Sunil Ravib3580db2022-01-28 12:25:46 -0800510#ifdef ENABLE_HOSTAPD_CONFIG_80211W_MFP_OPTIONAL
511 "ieee80211w=1\n"
512#endif
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000513 "wpa_passphrase=%s",
514 is_60Ghz_band_only ? "GCMP" : "CCMP",
515 nw_params.passphrase.c_str());
516 break;
517 case EncryptionType::WPA3_SAE_TRANSITION:
518 if (!validatePassphrase(
519 nw_params.passphrase.size(),
520 static_cast<uint32_t>(ParamSizeLimits::
521 WPA2_PSK_PASSPHRASE_MIN_LEN_IN_BYTES),
522 static_cast<uint32_t>(ParamSizeLimits::
523 WPA2_PSK_PASSPHRASE_MAX_LEN_IN_BYTES))) {
524 return "";
525 }
Sunil Ravid917c832023-07-07 17:30:33 +0000526 // WPA3 transition mode or SAE+WPA_PSK key management(AKM) is not allowed in 6GHz.
527 // Auto-convert any such configurations to SAE.
528 if ((band & band6Ghz) != 0) {
529 wpa_printf(MSG_INFO, "WPA3_SAE_TRANSITION configured in 6GHz band."
530 "Enable only SAE in key_mgmt");
531 encryption_config_as_string = StringPrintf(
532 "wpa=2\n"
533 "rsn_pairwise=CCMP\n"
534 "wpa_key_mgmt=%s\n"
535 "ieee80211w=2\n"
536 "sae_require_mfp=2\n"
537 "sae_pwe=%d\n"
538 "sae_password=%s",
Sunil Ravic1edd3e2023-02-06 18:52:51 +0000539#ifdef CONFIG_IEEE80211BE
Sunil Ravid917c832023-07-07 17:30:33 +0000540 iface_params.hwModeParams.enable80211BE ?
541 "SAE SAE-EXT-KEY" : "SAE",
Sunil Ravic1edd3e2023-02-06 18:52:51 +0000542#else
Sunil Ravid917c832023-07-07 17:30:33 +0000543 "SAE",
Sunil Ravic1edd3e2023-02-06 18:52:51 +0000544#endif
Sunil Ravid917c832023-07-07 17:30:33 +0000545 is_6Ghz_band_only ? 1 : 2,
546 nw_params.passphrase.c_str());
547 } else {
548 encryption_config_as_string = StringPrintf(
549 "wpa=2\n"
550 "rsn_pairwise=%s\n"
551 "wpa_key_mgmt=%s\n"
552 "ieee80211w=1\n"
553 "sae_require_mfp=1\n"
554 "wpa_passphrase=%s\n"
555 "sae_password=%s",
556 is_60Ghz_band_only ? "GCMP" : "CCMP",
557#ifdef CONFIG_IEEE80211BE
558 iface_params.hwModeParams.enable80211BE ?
559 "WPA-PSK SAE SAE-EXT-KEY" : "WPA-PSK SAE",
560#else
561 "WPA-PSK SAE",
562#endif
563 nw_params.passphrase.c_str(),
564 nw_params.passphrase.c_str());
565 }
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000566 break;
567 case EncryptionType::WPA3_SAE:
568 if (!validatePassphrase(nw_params.passphrase.size(), 1, -1)) {
569 return "";
570 }
571 encryption_config_as_string = StringPrintf(
572 "wpa=2\n"
573 "rsn_pairwise=%s\n"
Sunil Ravi65251732023-01-24 05:03:35 +0000574 "wpa_key_mgmt=%s\n"
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000575 "ieee80211w=2\n"
576 "sae_require_mfp=2\n"
577 "sae_pwe=%d\n"
578 "sae_password=%s",
579 is_60Ghz_band_only ? "GCMP" : "CCMP",
Sunil Ravi65251732023-01-24 05:03:35 +0000580#ifdef CONFIG_IEEE80211BE
581 iface_params.hwModeParams.enable80211BE ? "SAE SAE-EXT-KEY" : "SAE",
582#else
583 "SAE",
584#endif
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000585 is_6Ghz_band_only ? 1 : 2,
586 nw_params.passphrase.c_str());
587 break;
Ahmed ElArabawy1aaf1802022-02-04 15:58:55 -0800588 case EncryptionType::WPA3_OWE_TRANSITION:
589 encryption_config_as_string = StringPrintf(
590 "wpa=2\n"
591 "rsn_pairwise=%s\n"
592 "wpa_key_mgmt=OWE\n"
593 "ieee80211w=2",
594 is_60Ghz_band_only ? "GCMP" : "CCMP");
595 break;
596 case EncryptionType::WPA3_OWE:
Purushottam Kushwaha0316c882021-12-20 15:07:44 +0530597 encryption_config_as_string = StringPrintf(
598 "wpa=2\n"
599 "rsn_pairwise=%s\n"
600 "wpa_key_mgmt=OWE\n"
601 "ieee80211w=2",
602 is_60Ghz_band_only ? "GCMP" : "CCMP");
603 break;
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000604 default:
605 wpa_printf(MSG_ERROR, "Unknown encryption type");
606 return "";
607 }
608
609 std::string channel_config_as_string;
610 bool isFirst = true;
611 if (channelParams.enableAcs) {
612 std::string freqList_as_string;
613 for (const auto &range :
614 channelParams.acsChannelFreqRangesMhz) {
615 if (!isFirst) {
616 freqList_as_string += ",";
617 }
618 isFirst = false;
619
620 if (range.startMhz != range.endMhz) {
621 freqList_as_string +=
622 StringPrintf("%d-%d", range.startMhz, range.endMhz);
623 } else {
624 freqList_as_string += StringPrintf("%d", range.startMhz);
625 }
626 }
627 channel_config_as_string = StringPrintf(
628 "channel=0\n"
629 "acs_exclude_dfs=%d\n"
630 "freqlist=%s",
631 channelParams.acsShouldExcludeDfs,
632 freqList_as_string.c_str());
633 } else {
634 int op_class = getOpClassForChannel(
635 channelParams.channel,
636 band,
637 iface_params.hwModeParams.enable80211N,
638 iface_params.hwModeParams.enable80211AC);
639 channel_config_as_string = StringPrintf(
640 "channel=%d\n"
641 "op_class=%d",
642 channelParams.channel, op_class);
643 }
644
645 std::string hw_mode_as_string;
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000646 std::string enable_edmg_as_string;
647 std::string edmg_channel_as_string;
648 bool is_60Ghz_used = false;
649
650 if (((band & band60Ghz) != 0)) {
651 hw_mode_as_string = "hw_mode=ad";
652 if (iface_params.hwModeParams.enableEdmg) {
653 enable_edmg_as_string = "enable_edmg=1";
654 edmg_channel_as_string = StringPrintf(
655 "edmg_channel=%d",
656 channelParams.channel);
657 }
658 is_60Ghz_used = true;
659 } else if ((band & band2Ghz) != 0) {
660 if (((band & band5Ghz) != 0)
661 || ((band & band6Ghz) != 0)) {
662 hw_mode_as_string = "hw_mode=any";
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000663 } else {
664 hw_mode_as_string = "hw_mode=g";
665 }
666 } else if (((band & band5Ghz) != 0)
667 || ((band & band6Ghz) != 0)) {
668 hw_mode_as_string = "hw_mode=a";
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000669 } else {
670 wpa_printf(MSG_ERROR, "Invalid band");
671 return "";
672 }
673
674 std::string he_params_as_string;
675#ifdef CONFIG_IEEE80211AX
676 if (iface_params.hwModeParams.enable80211AX && !is_60Ghz_used) {
677 he_params_as_string = StringPrintf(
678 "ieee80211ax=1\n"
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000679 "he_su_beamformer=%d\n"
680 "he_su_beamformee=%d\n"
681 "he_mu_beamformer=%d\n"
682 "he_twt_required=%d\n",
683 iface_params.hwModeParams.enableHeSingleUserBeamformer ? 1 : 0,
684 iface_params.hwModeParams.enableHeSingleUserBeamformee ? 1 : 0,
685 iface_params.hwModeParams.enableHeMultiUserBeamformer ? 1 : 0,
686 iface_params.hwModeParams.enableHeTargetWakeTime ? 1 : 0);
687 } else {
688 he_params_as_string = "ieee80211ax=0";
689 }
690#endif /* CONFIG_IEEE80211AX */
Sunil Ravi65251732023-01-24 05:03:35 +0000691 std::string eht_params_as_string;
692#ifdef CONFIG_IEEE80211BE
693 if (iface_params.hwModeParams.enable80211BE && !is_60Ghz_used) {
Manaswini Paluri76ae6982024-02-29 14:49:20 +0530694 eht_params_as_string = "ieee80211be=1\n";
Sunil Ravide0e6bf2024-10-14 04:58:16 +0000695 if (areAidlServiceAndClientAtLeastVersion(2)) {
Les Lee399c6302024-09-12 03:03:38 +0000696 std::string interface_mac_addr = getInterfaceMacAddress(
697 iface_params.usesMlo ? br_name : iface_params.name);
Manaswini Paluri76ae6982024-02-29 14:49:20 +0530698 if (interface_mac_addr.empty()) {
699 wpa_printf(MSG_ERROR,
700 "Unable to set interface mac address as bssid for 11BE SAP");
701 return "";
702 }
Les Lee399c6302024-09-12 03:03:38 +0000703 if (iface_params.usesMlo) {
704 eht_params_as_string += StringPrintf(
705 "mld_addr=%s\n"
706 "mld_ap=1",
707 interface_mac_addr.c_str());
708 } else {
709 eht_params_as_string += StringPrintf(
710 "bssid=%s\n"
711 "mld_ap=1",
712 interface_mac_addr.c_str());
713 }
Manaswini Paluri76ae6982024-02-29 14:49:20 +0530714 }
Sunil Ravi65251732023-01-24 05:03:35 +0000715 /* TODO set eht_su_beamformer, eht_su_beamformee, eht_mu_beamformer */
716 } else {
717 eht_params_as_string = "ieee80211be=0";
718 }
719#endif /* CONFIG_IEEE80211BE */
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000720
Xin Dengfec682f2024-02-06 22:59:39 -0800721 std::string ht_cap_vht_oper_he_oper_eht_oper_chwidth_as_string;
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530722 switch (iface_params.hwModeParams.maximumChannelBandwidth) {
723 case ChannelBandwidth::BANDWIDTH_20:
Xin Dengfec682f2024-02-06 22:59:39 -0800724 ht_cap_vht_oper_he_oper_eht_oper_chwidth_as_string = StringPrintf(
725#ifdef CONFIG_IEEE80211BE
726 "eht_oper_chwidth=0\n"
727#endif /* CONFIG_IEEE80211BE */
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530728#ifdef CONFIG_IEEE80211AX
729 "he_oper_chwidth=0\n"
730#endif
Kiran Kumar Lokereb6445122024-02-06 20:06:27 -0800731 "vht_oper_chwidth=0\n"
732 "%s", (band & band6Ghz) ? "op_class=131" : "");
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530733 break;
734 case ChannelBandwidth::BANDWIDTH_40:
Xin Dengfec682f2024-02-06 22:59:39 -0800735 ht_cap_vht_oper_he_oper_eht_oper_chwidth_as_string = StringPrintf(
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530736 "ht_capab=[HT40+]\n"
Xin Dengfec682f2024-02-06 22:59:39 -0800737#ifdef CONFIG_IEEE80211BE
738 "eht_oper_chwidth=0\n"
739#endif /* CONFIG_IEEE80211BE */
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530740#ifdef CONFIG_IEEE80211AX
741 "he_oper_chwidth=0\n"
742#endif
Kiran Kumar Lokereb6445122024-02-06 20:06:27 -0800743 "vht_oper_chwidth=0\n"
744 "%s", (band & band6Ghz) ? "op_class=132" : "");
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530745 break;
746 case ChannelBandwidth::BANDWIDTH_80:
Xin Dengfec682f2024-02-06 22:59:39 -0800747 ht_cap_vht_oper_he_oper_eht_oper_chwidth_as_string = StringPrintf(
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530748 "ht_capab=[HT40+]\n"
Xin Dengfec682f2024-02-06 22:59:39 -0800749#ifdef CONFIG_IEEE80211BE
750 "eht_oper_chwidth=%d\n"
751#endif /* CONFIG_IEEE80211BE */
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530752#ifdef CONFIG_IEEE80211AX
753 "he_oper_chwidth=%d\n"
754#endif
Kiran Kumar Lokereb6445122024-02-06 20:06:27 -0800755 "vht_oper_chwidth=%d\n"
756 "%s",
Xin Dengfec682f2024-02-06 22:59:39 -0800757#ifdef CONFIG_IEEE80211BE
758 (iface_params.hwModeParams.enable80211BE && !is_60Ghz_used) ? 1 : 0,
759#endif
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530760#ifdef CONFIG_IEEE80211AX
761 (iface_params.hwModeParams.enable80211AX && !is_60Ghz_used) ? 1 : 0,
762#endif
Kiran Kumar Lokereb6445122024-02-06 20:06:27 -0800763 iface_params.hwModeParams.enable80211AC ? 1 : 0,
764 (band & band6Ghz) ? "op_class=133" : "");
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530765 break;
766 case ChannelBandwidth::BANDWIDTH_160:
Xin Dengfec682f2024-02-06 22:59:39 -0800767 ht_cap_vht_oper_he_oper_eht_oper_chwidth_as_string = StringPrintf(
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530768 "ht_capab=[HT40+]\n"
Xin Dengfec682f2024-02-06 22:59:39 -0800769#ifdef CONFIG_IEEE80211BE
770 "eht_oper_chwidth=%d\n"
771#endif /* CONFIG_IEEE80211BE */
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530772#ifdef CONFIG_IEEE80211AX
773 "he_oper_chwidth=%d\n"
774#endif
Kiran Kumar Lokereb6445122024-02-06 20:06:27 -0800775 "vht_oper_chwidth=%d\n"
776 "%s",
Xin Dengfec682f2024-02-06 22:59:39 -0800777#ifdef CONFIG_IEEE80211BE
778 (iface_params.hwModeParams.enable80211BE && !is_60Ghz_used) ? 2 : 0,
779#endif
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530780#ifdef CONFIG_IEEE80211AX
781 (iface_params.hwModeParams.enable80211AX && !is_60Ghz_used) ? 2 : 0,
782#endif
Kiran Kumar Lokereb6445122024-02-06 20:06:27 -0800783 iface_params.hwModeParams.enable80211AC ? 2 : 0,
784 (band & band6Ghz) ? "op_class=134" : "");
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530785 break;
786 default:
Sunil Raviffa5cce2022-08-22 23:37:16 +0000787 if (!is_2Ghz_band_only && !is_60Ghz_used) {
788 if (iface_params.hwModeParams.enable80211AC) {
Xin Dengfec682f2024-02-06 22:59:39 -0800789 ht_cap_vht_oper_he_oper_eht_oper_chwidth_as_string =
Sunil Ravi4fc918f2022-04-17 09:26:48 -0700790 "ht_capab=[HT40+]\n"
791 "vht_oper_chwidth=1\n";
Sunil Raviffa5cce2022-08-22 23:37:16 +0000792 }
Kiran Kumar Lokereb6445122024-02-06 20:06:27 -0800793 if (band & band6Ghz) {
Xin Deng3ee3fe12024-02-20 23:24:37 -0800794#ifdef CONFIG_IEEE80211BE
795 if (iface_params.hwModeParams.enable80211BE)
796 ht_cap_vht_oper_he_oper_eht_oper_chwidth_as_string += "op_class=137\n";
797 else
798 ht_cap_vht_oper_he_oper_eht_oper_chwidth_as_string += "op_class=134\n";
799#else /* CONFIG_IEEE80211BE */
Xin Dengfec682f2024-02-06 22:59:39 -0800800 ht_cap_vht_oper_he_oper_eht_oper_chwidth_as_string += "op_class=134\n";
Xin Deng3ee3fe12024-02-20 23:24:37 -0800801#endif /* CONFIG_IEEE80211BE */
Kiran Kumar Lokereb6445122024-02-06 20:06:27 -0800802 }
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530803#ifdef CONFIG_IEEE80211AX
Sunil Raviffa5cce2022-08-22 23:37:16 +0000804 if (iface_params.hwModeParams.enable80211AX) {
Xin Dengfec682f2024-02-06 22:59:39 -0800805 ht_cap_vht_oper_he_oper_eht_oper_chwidth_as_string += "he_oper_chwidth=1\n";
806 }
807#endif
808#ifdef CONFIG_IEEE80211BE
809 if (iface_params.hwModeParams.enable80211BE) {
810 ht_cap_vht_oper_he_oper_eht_oper_chwidth_as_string += "eht_oper_chwidth=1";
Sunil Raviffa5cce2022-08-22 23:37:16 +0000811 }
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530812#endif
Sunil Raviffa5cce2022-08-22 23:37:16 +0000813 }
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530814 break;
815 }
816
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000817#ifdef CONFIG_INTERWORKING
818 std::string access_network_params_as_string;
819 if (nw_params.isMetered) {
820 access_network_params_as_string = StringPrintf(
821 "interworking=1\n"
822 "access_network_type=2\n"); // CHARGEABLE_PUBLIC_NETWORK
823 } else {
824 access_network_params_as_string = StringPrintf(
825 "interworking=0\n");
826 }
827#endif /* CONFIG_INTERWORKING */
828
829 std::string bridge_as_string;
Les Lee399c6302024-09-12 03:03:38 +0000830 if (!br_name.empty() && !iface_params.usesMlo) {
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000831 bridge_as_string = StringPrintf("bridge=%s", br_name.c_str());
832 }
833
Serik Beketayev8af7a722021-12-23 12:25:36 -0800834 // vendor_elements string
835 std::string vendor_elements_as_string;
836 if (nw_params.vendorElements.size() > 0) {
837 std::stringstream ss;
838 ss << std::hex;
839 ss << std::setfill('0');
840 for (uint8_t b : nw_params.vendorElements) {
841 ss << std::setw(2) << static_cast<unsigned int>(b);
842 }
843 vendor_elements_as_string = StringPrintf("vendor_elements=%s", ss.str().c_str());
844 }
845
Purushottam Kushwaha0316c882021-12-20 15:07:44 +0530846 std::string owe_transition_ifname_as_string;
847 if (!owe_transition_ifname.empty()) {
848 owe_transition_ifname_as_string = StringPrintf(
849 "owe_transition_ifname=%s", owe_transition_ifname.c_str());
850 }
851
Les Lee9dfae3b2024-10-25 18:31:47 +0000852 std::string ap_isolation_as_string = StringPrintf("ap_isolate=%s",
853 isAidlServiceVersionAtLeast(3) && nw_params.isClientIsolationEnabled ?
854 "1" : "0");
855
Chris Weird41879a2024-10-30 15:53:24 -0700856 // Overlay for LOHS (unmetered SoftAP)
857 std::string overlay_path = resolveVendorConfPath(kUnmeteredIfaceOverlayPath);
858 std::string overlay_string;
859 if (!nw_params.isMetered
860 && 0 == access(overlay_path.c_str(), R_OK)
861 && !ReadFileToString(overlay_path, &overlay_string)) {
862 logHostapdConfigError(errno, overlay_path);
863 return "";
864 }
865 std::string sanitized_overlay = "";
866 std::istringstream overlay_stream(overlay_string);
867 for (std::string line; std::getline(overlay_stream, line);) {
868 std::string overlay_key = trimWhitespace(line.substr(0, line.find("=")));
869 if (kOverlayableKeys.contains(overlay_key)) {
870 sanitized_overlay.append(line + "\n");
871 }
872 }
873
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000874 return StringPrintf(
Chris Weird41879a2024-10-30 15:53:24 -0700875 "%s\n"
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000876 "interface=%s\n"
877 "driver=nl80211\n"
Les Lee399c6302024-09-12 03:03:38 +0000878 "ctrl_interface=/data/vendor/wifi/hostapd/ctrl_%s\n"
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000879 // ssid2 signals to hostapd that the value is not a literal value
880 // for use as a SSID. In this case, we're giving it a hex
881 // std::string and hostapd needs to expect that.
882 "ssid2=%s\n"
883 "%s\n"
884 "ieee80211n=%d\n"
885 "ieee80211ac=%d\n"
886 "%s\n"
887 "%s\n"
888 "%s\n"
Sunil Ravi65251732023-01-24 05:03:35 +0000889 "%s\n"
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000890 "ignore_broadcast_ssid=%d\n"
891 "wowlan_triggers=any\n"
892#ifdef CONFIG_INTERWORKING
893 "%s\n"
894#endif /* CONFIG_INTERWORKING */
895 "%s\n"
896 "%s\n"
897 "%s\n"
Serik Beketayev8af7a722021-12-23 12:25:36 -0800898 "%s\n"
Purushottam Kushwaha0316c882021-12-20 15:07:44 +0530899 "%s\n"
Les Lee9dfae3b2024-10-25 18:31:47 +0000900 "%s\n"
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000901 "%s\n",
Chris Weird41879a2024-10-30 15:53:24 -0700902 sanitized_overlay.c_str(),
Les Lee399c6302024-09-12 03:03:38 +0000903 iface_params.usesMlo ? br_name.c_str() : iface_params.name.c_str(),
904 iface_params.name.c_str(),
905 ssid_as_string.c_str(),
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000906 channel_config_as_string.c_str(),
907 iface_params.hwModeParams.enable80211N ? 1 : 0,
908 iface_params.hwModeParams.enable80211AC ? 1 : 0,
909 he_params_as_string.c_str(),
Sunil Ravi65251732023-01-24 05:03:35 +0000910 eht_params_as_string.c_str(),
Xin Dengfec682f2024-02-06 22:59:39 -0800911 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 +0000912 nw_params.isHidden ? 1 : 0,
913#ifdef CONFIG_INTERWORKING
914 access_network_params_as_string.c_str(),
915#endif /* CONFIG_INTERWORKING */
916 encryption_config_as_string.c_str(),
917 bridge_as_string.c_str(),
Purushottam Kushwaha0316c882021-12-20 15:07:44 +0530918 owe_transition_ifname_as_string.c_str(),
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000919 enable_edmg_as_string.c_str(),
Serik Beketayev8af7a722021-12-23 12:25:36 -0800920 edmg_channel_as_string.c_str(),
Les Lee9dfae3b2024-10-25 18:31:47 +0000921 vendor_elements_as_string.c_str(),
922 ap_isolation_as_string.c_str());
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000923}
924
925Generation getGeneration(hostapd_hw_modes *current_mode)
926{
927 wpa_printf(MSG_DEBUG, "getGeneration hwmode=%d, ht_enabled=%d,"
928 " vht_enabled=%d, he_supported=%d",
929 current_mode->mode, current_mode->ht_capab != 0,
930 current_mode->vht_capab != 0, current_mode->he_capab->he_supported);
931 switch (current_mode->mode) {
932 case HOSTAPD_MODE_IEEE80211B:
933 return Generation::WIFI_STANDARD_LEGACY;
934 case HOSTAPD_MODE_IEEE80211G:
935 return current_mode->ht_capab == 0 ?
936 Generation::WIFI_STANDARD_LEGACY : Generation::WIFI_STANDARD_11N;
937 case HOSTAPD_MODE_IEEE80211A:
938 if (current_mode->he_capab->he_supported) {
939 return Generation::WIFI_STANDARD_11AX;
940 }
941 return current_mode->vht_capab == 0 ?
942 Generation::WIFI_STANDARD_11N : Generation::WIFI_STANDARD_11AC;
943 case HOSTAPD_MODE_IEEE80211AD:
944 return Generation::WIFI_STANDARD_11AD;
945 default:
946 return Generation::WIFI_STANDARD_UNKNOWN;
947 }
948}
949
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800950ChannelBandwidth getChannelBandwidth(struct hostapd_config *iconf)
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000951{
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800952 wpa_printf(MSG_DEBUG, "getChannelBandwidth %d, isHT=%d, isHT40=%d",
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000953 iconf->vht_oper_chwidth, iconf->ieee80211n,
954 iconf->secondary_channel);
955 switch (iconf->vht_oper_chwidth) {
Sunil8cd6f4d2022-06-28 18:40:46 +0000956 case CONF_OPER_CHWIDTH_80MHZ:
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800957 return ChannelBandwidth::BANDWIDTH_80;
Sunil8cd6f4d2022-06-28 18:40:46 +0000958 case CONF_OPER_CHWIDTH_80P80MHZ:
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800959 return ChannelBandwidth::BANDWIDTH_80P80;
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000960 break;
Sunil8cd6f4d2022-06-28 18:40:46 +0000961 case CONF_OPER_CHWIDTH_160MHZ:
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800962 return ChannelBandwidth::BANDWIDTH_160;
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000963 break;
Sunil8cd6f4d2022-06-28 18:40:46 +0000964 case CONF_OPER_CHWIDTH_USE_HT:
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000965 if (iconf->ieee80211n) {
966 return iconf->secondary_channel != 0 ?
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800967 ChannelBandwidth::BANDWIDTH_40 : ChannelBandwidth::BANDWIDTH_20;
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000968 }
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800969 return ChannelBandwidth::BANDWIDTH_20_NOHT;
Sunil8cd6f4d2022-06-28 18:40:46 +0000970 case CONF_OPER_CHWIDTH_2160MHZ:
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800971 return ChannelBandwidth::BANDWIDTH_2160;
Sunil8cd6f4d2022-06-28 18:40:46 +0000972 case CONF_OPER_CHWIDTH_4320MHZ:
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800973 return ChannelBandwidth::BANDWIDTH_4320;
Sunil8cd6f4d2022-06-28 18:40:46 +0000974 case CONF_OPER_CHWIDTH_6480MHZ:
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800975 return ChannelBandwidth::BANDWIDTH_6480;
Sunil8cd6f4d2022-06-28 18:40:46 +0000976 case CONF_OPER_CHWIDTH_8640MHZ:
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800977 return ChannelBandwidth::BANDWIDTH_8640;
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000978 default:
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800979 return ChannelBandwidth::BANDWIDTH_INVALID;
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000980 }
981}
982
Chris Weir808564b2024-08-07 10:12:16 -0700983std::optional<struct sta_info*> getStaInfoByMacAddr(const struct hostapd_data* iface_hapd,
984 const u8 *mac_addr) {
985 if (iface_hapd == nullptr || mac_addr == nullptr){
986 wpa_printf(MSG_ERROR, "nullptr passsed to getStaInfoByMacAddr!");
987 return std::nullopt;
988 }
989
990 for (struct sta_info* sta_ptr = iface_hapd->sta_list; sta_ptr; sta_ptr = sta_ptr->next) {
991 int res;
992 res = memcmp(sta_ptr->addr, mac_addr, ETH_ALEN);
993 if (res == 0) {
994 return sta_ptr;
995 }
996 }
997 return std::nullopt;
998}
999
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001000bool forceStaDisconnection(struct hostapd_data* hapd,
1001 const std::vector<uint8_t>& client_address,
1002 const uint16_t reason_code) {
Sunil Ravi1a360892022-11-29 20:16:01 +00001003 if (client_address.size() != ETH_ALEN) {
1004 return false;
1005 }
Chris Weir808564b2024-08-07 10:12:16 -07001006
1007 auto sta_ptr_optional = getStaInfoByMacAddr(hapd, client_address.data());
1008 if (sta_ptr_optional.has_value()) {
1009 wpa_printf(MSG_INFO, "Force client:" MACSTR " disconnect with reason: %d",
1010 MAC2STR(client_address.data()), reason_code);
1011 ap_sta_disconnect(hapd, sta_ptr_optional.value(), sta_ptr_optional.value()->addr,
1012 reason_code);
1013 return true;
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001014 }
Chris Weir808564b2024-08-07 10:12:16 -07001015
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001016 return false;
1017}
1018
1019// hostapd core functions accept "C" style function pointers, so use global
1020// functions to pass to the hostapd core function and store the corresponding
1021// std::function methods to be invoked.
1022//
1023// NOTE: Using the pattern from the vendor HAL (wifi_legacy_hal.cpp).
1024//
1025// Callback to be invoked once setup is complete
1026std::function<void(struct hostapd_data*)> on_setup_complete_internal_callback;
1027void onAsyncSetupCompleteCb(void* ctx)
1028{
1029 struct hostapd_data* iface_hapd = (struct hostapd_data*)ctx;
1030 if (on_setup_complete_internal_callback) {
1031 on_setup_complete_internal_callback(iface_hapd);
1032 // Invalidate this callback since we don't want this firing
1033 // again in single AP mode.
1034 if (strlen(iface_hapd->conf->bridge) > 0) {
1035 on_setup_complete_internal_callback = nullptr;
1036 }
1037 }
1038}
1039
1040// Callback to be invoked on hotspot client connection/disconnection
1041std::function<void(struct hostapd_data*, const u8 *mac_addr, int authorized,
1042 const u8 *p2p_dev_addr)> on_sta_authorized_internal_callback;
1043void onAsyncStaAuthorizedCb(void* ctx, const u8 *mac_addr, int authorized,
Sunil Ravid8128a22023-11-06 23:53:58 +00001044 const u8 *p2p_dev_addr, const u8 *ip)
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001045{
1046 struct hostapd_data* iface_hapd = (struct hostapd_data*)ctx;
1047 if (on_sta_authorized_internal_callback) {
1048 on_sta_authorized_internal_callback(iface_hapd, mac_addr,
1049 authorized, p2p_dev_addr);
1050 }
1051}
1052
1053std::function<void(struct hostapd_data*, int level,
1054 enum wpa_msg_type type, const char *txt,
1055 size_t len)> on_wpa_msg_internal_callback;
1056
1057void onAsyncWpaEventCb(void *ctx, int level,
1058 enum wpa_msg_type type, const char *txt,
1059 size_t len)
1060{
1061 struct hostapd_data* iface_hapd = (struct hostapd_data*)ctx;
1062 if (on_wpa_msg_internal_callback) {
1063 on_wpa_msg_internal_callback(iface_hapd, level,
1064 type, txt, len);
1065 }
1066}
1067
1068inline ndk::ScopedAStatus createStatus(HostapdStatusCode status_code) {
1069 return ndk::ScopedAStatus::fromServiceSpecificError(
1070 static_cast<int32_t>(status_code));
1071}
1072
1073inline ndk::ScopedAStatus createStatusWithMsg(
1074 HostapdStatusCode status_code, std::string msg)
1075{
1076 return ndk::ScopedAStatus::fromServiceSpecificErrorWithMessage(
1077 static_cast<int32_t>(status_code), msg.c_str());
1078}
1079
1080// Method called by death_notifier_ on client death.
1081void onDeath(void* cookie) {
1082 wpa_printf(MSG_ERROR, "Client died. Terminating...");
1083 eloop_terminate();
1084}
1085
1086} // namespace
1087
1088namespace aidl {
1089namespace android {
1090namespace hardware {
1091namespace wifi {
1092namespace hostapd {
1093
1094Hostapd::Hostapd(struct hapd_interfaces* interfaces)
1095 : interfaces_(interfaces)
1096{
1097 death_notifier_ = AIBinder_DeathRecipient_new(onDeath);
1098}
1099
1100::ndk::ScopedAStatus Hostapd::addAccessPoint(
1101 const IfaceParams& iface_params, const NetworkParams& nw_params)
1102{
1103 return addAccessPointInternal(iface_params, nw_params);
1104}
1105
1106::ndk::ScopedAStatus Hostapd::removeAccessPoint(const std::string& iface_name)
1107{
1108 return removeAccessPointInternal(iface_name);
1109}
1110
1111::ndk::ScopedAStatus Hostapd::terminate()
1112{
1113 wpa_printf(MSG_INFO, "Terminating...");
1114 // Clear the callback to avoid IPCThreadState shutdown during the
1115 // callback event.
1116 callbacks_.clear();
1117 eloop_terminate();
1118 return ndk::ScopedAStatus::ok();
1119}
1120
1121::ndk::ScopedAStatus Hostapd::registerCallback(
1122 const std::shared_ptr<IHostapdCallback>& callback)
1123{
1124 return registerCallbackInternal(callback);
1125}
1126
1127::ndk::ScopedAStatus Hostapd::forceClientDisconnect(
1128 const std::string& iface_name, const std::vector<uint8_t>& client_address,
1129 Ieee80211ReasonCode reason_code)
1130{
1131 return forceClientDisconnectInternal(iface_name, client_address, reason_code);
1132}
1133
1134::ndk::ScopedAStatus Hostapd::setDebugParams(DebugLevel level)
1135{
1136 return setDebugParamsInternal(level);
1137}
1138
1139::ndk::ScopedAStatus Hostapd::addAccessPointInternal(
1140 const IfaceParams& iface_params,
1141 const NetworkParams& nw_params)
1142{
1143 int channelParamsSize = iface_params.channelParams.size();
1144 if (channelParamsSize == 1) {
1145 // Single AP
1146 wpa_printf(MSG_INFO, "AddSingleAccessPoint, iface=%s",
1147 iface_params.name.c_str());
1148 return addSingleAccessPoint(iface_params, iface_params.channelParams[0],
Purushottam Kushwaha0316c882021-12-20 15:07:44 +05301149 nw_params, "", "");
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001150 } else if (channelParamsSize == 2) {
1151 // Concurrent APs
1152 wpa_printf(MSG_INFO, "AddDualAccessPoint, iface=%s",
1153 iface_params.name.c_str());
1154 return addConcurrentAccessPoints(iface_params, nw_params);
1155 }
1156 return createStatus(HostapdStatusCode::FAILURE_ARGS_INVALID);
1157}
1158
Purushottam Kushwaha0316c882021-12-20 15:07:44 +05301159std::vector<uint8_t> generateRandomOweSsid()
1160{
1161 u8 random[8] = {0};
1162 os_get_random(random, 8);
1163
1164 std::string ssid = StringPrintf("Owe-%s", random);
1165 wpa_printf(MSG_INFO, "Generated OWE SSID: %s", ssid.c_str());
1166 std::vector<uint8_t> vssid(ssid.begin(), ssid.end());
1167
1168 return vssid;
1169}
1170
Les Lee399c6302024-09-12 03:03:38 +00001171
1172// Both of bridged dual APs and MLO AP will be treated as concurrenct APs.
1173// -----------------------------------------
1174// | br_name | instance#1 | instance#2 |
1175// ___________________________________________________________
1176// bridged dual APs | ap_br_wlanX | wlan X | wlanY |
1177// ___________________________________________________________
1178// MLO AP | wlanX | 0 | 1 |
1179// ___________________________________________________________
1180// Both will be added in br_interfaces_[$br_name] and use instance's name
1181// to be iface_params_new.name to create single Access point.
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001182::ndk::ScopedAStatus Hostapd::addConcurrentAccessPoints(
1183 const IfaceParams& iface_params, const NetworkParams& nw_params)
1184{
1185 int channelParamsListSize = iface_params.channelParams.size();
1186 // Get available interfaces in bridge
Les Lee399c6302024-09-12 03:03:38 +00001187 std::vector<std::string> managed_instances;
1188 std::string br_name = StringPrintf("%s", iface_params.name.c_str());
1189 if (iface_params.usesMlo) {
1190 // MLO AP is using link id as instance.
1191 for (std::size_t i = 0; i < iface_params.instanceIdentities->size(); i++) {
1192 managed_instances.push_back(iface_params.instanceIdentities->at(i)->c_str());
1193 }
1194 } else {
1195 if (!GetInterfacesInBridge(br_name, &managed_instances)) {
1196 return createStatusWithMsg(HostapdStatusCode::FAILURE_UNKNOWN,
1197 "Get interfaces in bridge failed.");
1198 }
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001199 }
Les Lee399c6302024-09-12 03:03:38 +00001200 // Either bridged AP or MLO AP should have two instances.
1201 if (managed_instances.size() < channelParamsListSize) {
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001202 return createStatusWithMsg(HostapdStatusCode::FAILURE_UNKNOWN,
Les Lee399c6302024-09-12 03:03:38 +00001203 "Available interfaces less than requested bands");
1204 }
1205
1206 if (iface_params.usesMlo
1207 && nw_params.encryptionType == EncryptionType::WPA3_OWE_TRANSITION) {
1208 return createStatusWithMsg(HostapdStatusCode::FAILURE_UNKNOWN,
1209 "Invalid encryptionType (OWE transition) for MLO SAP.");
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001210 }
1211 // start BSS on specified bands
1212 for (std::size_t i = 0; i < channelParamsListSize; i ++) {
1213 IfaceParams iface_params_new = iface_params;
Purushottam Kushwaha0316c882021-12-20 15:07:44 +05301214 NetworkParams nw_params_new = nw_params;
Purushottam Kushwaha0316c882021-12-20 15:07:44 +05301215 std::string owe_transition_ifname = "";
Les Lee399c6302024-09-12 03:03:38 +00001216 iface_params_new.name = managed_instances[i];
Ahmed ElArabawy1aaf1802022-02-04 15:58:55 -08001217 if (nw_params.encryptionType == EncryptionType::WPA3_OWE_TRANSITION) {
Purushottam Kushwaha0316c882021-12-20 15:07:44 +05301218 if (i == 0 && i+1 < channelParamsListSize) {
Les Lee399c6302024-09-12 03:03:38 +00001219 owe_transition_ifname = managed_instances[i+1];
Purushottam Kushwaha0316c882021-12-20 15:07:44 +05301220 nw_params_new.encryptionType = EncryptionType::NONE;
1221 } else {
Les Lee399c6302024-09-12 03:03:38 +00001222 owe_transition_ifname = managed_instances[0];
Purushottam Kushwaha0316c882021-12-20 15:07:44 +05301223 nw_params_new.isHidden = true;
1224 nw_params_new.ssid = generateRandomOweSsid();
1225 }
1226 }
1227
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001228 ndk::ScopedAStatus status = addSingleAccessPoint(
Purushottam Kushwaha0316c882021-12-20 15:07:44 +05301229 iface_params_new, iface_params.channelParams[i], nw_params_new,
1230 br_name, owe_transition_ifname);
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001231 if (!status.isOk()) {
1232 wpa_printf(MSG_ERROR, "Failed to addAccessPoint %s",
Les Lee399c6302024-09-12 03:03:38 +00001233 managed_instances[i].c_str());
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001234 return status;
1235 }
1236 }
Les Lee399c6302024-09-12 03:03:38 +00001237
1238 if (iface_params.usesMlo) {
1239 std::size_t i = 0;
1240 std::size_t j = 0;
1241 for (i = 0; i < interfaces_->count; i++) {
1242 struct hostapd_iface *iface = interfaces_->iface[i];
1243
1244 for (j = 0; j < iface->num_bss; j++) {
1245 struct hostapd_data *iface_hapd = iface->bss[j];
1246 if (hostapd_enable_iface(iface_hapd->iface) < 0) {
1247 wpa_printf(
1248 MSG_ERROR, "Enabling interface %s failed on %zu",
1249 iface_params.name.c_str(), i);
1250 return createStatus(HostapdStatusCode::FAILURE_UNKNOWN);
1251 }
1252 }
1253 }
1254 }
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001255 // Save bridge interface info
Les Lee399c6302024-09-12 03:03:38 +00001256 br_interfaces_[br_name] = managed_instances;
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001257 return ndk::ScopedAStatus::ok();
1258}
1259
Les Lee399c6302024-09-12 03:03:38 +00001260struct hostapd_data * hostapd_get_iface_by_link_id(struct hapd_interfaces *interfaces,
1261 const size_t link_id)
1262{
1263#ifdef CONFIG_IEEE80211BE
1264 size_t i, j;
1265
1266 for (i = 0; i < interfaces->count; i++) {
1267 struct hostapd_iface *iface = interfaces->iface[i];
1268
1269 for (j = 0; j < iface->num_bss; j++) {
1270 struct hostapd_data *hapd = iface->bss[j];
1271
1272 if (link_id == hapd->mld_link_id)
1273 return hapd;
1274 }
1275 }
1276#endif
1277 return NULL;
1278}
1279
1280// Both of bridged dual APs and MLO AP will be treated as concurrenct APs.
1281// -----------------------------------------
1282// | br_name | iface_params.name
1283// _______________________________________________________________
1284// bridged dual APs | bridged interface name | interface name
1285// _______________________________________________________________
1286// MLO AP | AP interface name | mld link id as instance name
1287// _______________________________________________________________
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001288::ndk::ScopedAStatus Hostapd::addSingleAccessPoint(
1289 const IfaceParams& iface_params,
1290 const ChannelParams& channelParams,
1291 const NetworkParams& nw_params,
Purushottam Kushwaha0316c882021-12-20 15:07:44 +05301292 const std::string br_name,
1293 const std::string owe_transition_ifname)
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001294{
Les Lee399c6302024-09-12 03:03:38 +00001295 if (iface_params.usesMlo) { // the mlo case, iface name is instance name which is mld_link_id
1296 if (hostapd_get_iface_by_link_id(interfaces_, (size_t) iface_params.name.c_str())) {
1297 wpa_printf(
1298 MSG_ERROR, "Instance link id %s already present",
1299 iface_params.name.c_str());
1300 return createStatus(HostapdStatusCode::FAILURE_IFACE_EXISTS);
1301 }
1302 }
1303 if (hostapd_get_iface(interfaces_,
1304 iface_params.usesMlo ? br_name.c_str() : iface_params.name.c_str())) {
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001305 wpa_printf(
Les Lee399c6302024-09-12 03:03:38 +00001306 MSG_ERROR, "Instance interface %s already present",
1307 iface_params.usesMlo ? br_name.c_str() : iface_params.name.c_str());
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001308 return createStatus(HostapdStatusCode::FAILURE_IFACE_EXISTS);
1309 }
Purushottam Kushwaha0316c882021-12-20 15:07:44 +05301310 const auto conf_params = CreateHostapdConfig(iface_params, channelParams, nw_params,
1311 br_name, owe_transition_ifname);
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001312 if (conf_params.empty()) {
1313 wpa_printf(MSG_ERROR, "Failed to create config params");
1314 return createStatus(HostapdStatusCode::FAILURE_ARGS_INVALID);
1315 }
1316 const auto conf_file_path =
Les Lee399c6302024-09-12 03:03:38 +00001317 WriteHostapdConfig(iface_params.name, conf_params, br_name, iface_params.usesMlo);
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001318 if (conf_file_path.empty()) {
1319 wpa_printf(MSG_ERROR, "Failed to write config file");
1320 return createStatus(HostapdStatusCode::FAILURE_UNKNOWN);
1321 }
1322 std::string add_iface_param_str = StringPrintf(
Les Lee399c6302024-09-12 03:03:38 +00001323 "%s config=%s", iface_params.usesMlo ? br_name.c_str(): iface_params.name.c_str(),
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001324 conf_file_path.c_str());
1325 std::vector<char> add_iface_param_vec(
1326 add_iface_param_str.begin(), add_iface_param_str.end() + 1);
1327 if (hostapd_add_iface(interfaces_, add_iface_param_vec.data()) < 0) {
1328 wpa_printf(
Les Lee399c6302024-09-12 03:03:38 +00001329 MSG_ERROR, "Adding hostapd iface %s failed",
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001330 add_iface_param_str.c_str());
1331 return createStatus(HostapdStatusCode::FAILURE_UNKNOWN);
1332 }
Les Lee399c6302024-09-12 03:03:38 +00001333
1334 // find the iface and set up callback.
1335 struct hostapd_data* iface_hapd = iface_params.usesMlo ?
1336 hostapd_get_iface_by_link_id(interfaces_, (size_t) iface_params.name.c_str()) :
1337 hostapd_get_iface(interfaces_, iface_params.name.c_str());
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001338 WPA_ASSERT(iface_hapd != nullptr && iface_hapd->iface != nullptr);
Les Lee399c6302024-09-12 03:03:38 +00001339 if (iface_params.usesMlo) {
1340 memcmp(iface_hapd->conf->iface, br_name.c_str(), br_name.size());
1341 }
1342
1343 // Callback discrepancy between bridged dual APs and MLO AP
1344 // Note: Only bridged dual APs will have "iface_hapd->conf->bridge" and
1345 // Only MLO AP will have "iface_hapd->mld_link_id"
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001346 // Register the setup complete callbacks
Les Lee399c6302024-09-12 03:03:38 +00001347 // -----------------------------------------
1348 // | bridged dual APs | bridged single link MLO | MLO SAP
1349 // _________________________________________________________________________________________
1350 // hapd->conf->bridge | bridged interface name | bridged interface nam | N/A
1351 // _________________________________________________________________________________________
1352 // hapd->conf->iface | AP interface name | AP interface name | AP interface name
1353 // _________________________________________________________________________________________
1354 // hapd->mld_link_id | 0 (default value) | link id (0) | link id (0 or 1)
1355 // _________________________________________________________________________________________
1356 // hapd->mld_ap | 0 | 1 | 1
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001357 on_setup_complete_internal_callback =
1358 [this](struct hostapd_data* iface_hapd) {
1359 wpa_printf(
1360 MSG_INFO, "AP interface setup completed - state %s",
1361 hostapd_state_text(iface_hapd->iface->state));
1362 if (iface_hapd->iface->state == HAPD_IFACE_DISABLED) {
1363 // Invoke the failure callback on all registered
1364 // clients.
Les Lee399c6302024-09-12 03:03:38 +00001365 std::string instanceName = iface_hapd->conf->iface;
1366#ifdef CONFIG_IEEE80211BE
1367 if (iface_hapd->conf->mld_ap
1368 && strlen(iface_hapd->conf->bridge) == 0) {
1369 instanceName = std::to_string(iface_hapd->mld_link_id);
1370 }
1371#endif
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001372 for (const auto& callback : callbacks_) {
Chenming Huangbaa5e582024-04-02 08:21:10 +05301373 auto status = callback->onFailure(
1374 strlen(iface_hapd->conf->bridge) > 0 ?
Les Leee08c2862021-10-29 16:36:41 +08001375 iface_hapd->conf->bridge : iface_hapd->conf->iface,
Les Lee399c6302024-09-12 03:03:38 +00001376 instanceName);
Chenming Huangbaa5e582024-04-02 08:21:10 +05301377 if (!status.isOk()) {
1378 wpa_printf(MSG_ERROR, "Failed to invoke onFailure");
1379 }
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001380 }
1381 }
1382 };
1383
1384 // Register for new client connect/disconnect indication.
1385 on_sta_authorized_internal_callback =
1386 [this](struct hostapd_data* iface_hapd, const u8 *mac_addr,
1387 int authorized, const u8 *p2p_dev_addr) {
1388 wpa_printf(MSG_DEBUG, "notify client " MACSTR " %s",
1389 MAC2STR(mac_addr),
1390 (authorized) ? "Connected" : "Disconnected");
1391 ClientInfo info;
1392 info.ifaceName = strlen(iface_hapd->conf->bridge) > 0 ?
1393 iface_hapd->conf->bridge : iface_hapd->conf->iface;
Les Lee399c6302024-09-12 03:03:38 +00001394 std::string instanceName = iface_hapd->conf->iface;
1395#ifdef CONFIG_IEEE80211BE
1396 if (iface_hapd->conf->mld_ap
1397 && strlen(iface_hapd->conf->bridge) == 0) {
1398 instanceName = std::to_string(iface_hapd->mld_link_id);
1399 }
1400#endif
1401 info.apIfaceInstance = instanceName;
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001402 info.clientAddress.assign(mac_addr, mac_addr + ETH_ALEN);
1403 info.isConnected = authorized;
Chris Weir808564b2024-08-07 10:12:16 -07001404 if(isAidlServiceVersionAtLeast(3) && !authorized) {
1405 u16 disconnect_reason_code = WLAN_REASON_UNSPECIFIED;
1406 auto sta_ptr_optional = getStaInfoByMacAddr(iface_hapd, mac_addr);
1407 if (sta_ptr_optional.has_value()){
1408 disconnect_reason_code = sta_ptr_optional.value()->deauth_reason;
1409 }
1410 info.disconnectReasonCode =
1411 static_cast<common::DeauthenticationReasonCode>(disconnect_reason_code);
1412 }
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001413 for (const auto &callback : callbacks_) {
Chenming Huangbaa5e582024-04-02 08:21:10 +05301414 auto status = callback->onConnectedClientsChanged(info);
1415 if (!status.isOk()) {
1416 wpa_printf(MSG_ERROR, "Failed to invoke onConnectedClientsChanged");
1417 }
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001418 }
1419 };
1420
1421 // Register for wpa_event which used to get channel switch event
1422 on_wpa_msg_internal_callback =
1423 [this](struct hostapd_data* iface_hapd, int level,
1424 enum wpa_msg_type type, const char *txt,
1425 size_t len) {
1426 wpa_printf(MSG_DEBUG, "Receive wpa msg : %s", txt);
1427 if (os_strncmp(txt, AP_EVENT_ENABLED,
1428 strlen(AP_EVENT_ENABLED)) == 0 ||
1429 os_strncmp(txt, WPA_EVENT_CHANNEL_SWITCH,
1430 strlen(WPA_EVENT_CHANNEL_SWITCH)) == 0) {
Les Lee399c6302024-09-12 03:03:38 +00001431 std::string instanceName = iface_hapd->conf->iface;
1432#ifdef CONFIG_IEEE80211BE
1433 if (iface_hapd->conf->mld_ap && strlen(iface_hapd->conf->bridge) == 0) {
1434 instanceName = std::to_string(iface_hapd->mld_link_id);
1435 }
1436#endif
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001437 ApInfo info;
1438 info.ifaceName = strlen(iface_hapd->conf->bridge) > 0 ?
1439 iface_hapd->conf->bridge : iface_hapd->conf->iface,
Les Lee399c6302024-09-12 03:03:38 +00001440 info.apIfaceInstance = instanceName;
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001441 info.freqMhz = iface_hapd->iface->freq;
Ahmed ElArabawyb4115792022-02-08 09:33:01 -08001442 info.channelBandwidth = getChannelBandwidth(iface_hapd->iconf);
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001443 info.generation = getGeneration(iface_hapd->iface->current_mode);
1444 info.apIfaceInstanceMacAddress.assign(iface_hapd->own_addr,
1445 iface_hapd->own_addr + ETH_ALEN);
1446 for (const auto &callback : callbacks_) {
Chenming Huangbaa5e582024-04-02 08:21:10 +05301447 auto status = callback->onApInstanceInfoChanged(info);
1448 if (!status.isOk()) {
1449 wpa_printf(MSG_ERROR,
1450 "Failed to invoke onApInstanceInfoChanged");
1451 }
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001452 }
Les Leea0c90cb2022-04-19 17:39:23 +08001453 } else if (os_strncmp(txt, AP_EVENT_DISABLED, strlen(AP_EVENT_DISABLED)) == 0
1454 || os_strncmp(txt, INTERFACE_DISABLED, strlen(INTERFACE_DISABLED)) == 0)
1455 {
Les Lee399c6302024-09-12 03:03:38 +00001456 std::string instanceName = iface_hapd->conf->iface;
1457#ifdef CONFIG_IEEE80211BE
1458 if (iface_hapd->conf->mld_ap && strlen(iface_hapd->conf->bridge) == 0) {
1459 instanceName = std::to_string(iface_hapd->mld_link_id);
1460 }
1461#endif
Yu Ouyang378d3c42021-08-20 17:31:08 +08001462 // Invoke the failure callback on all registered clients.
1463 for (const auto& callback : callbacks_) {
Les Lee399c6302024-09-12 03:03:38 +00001464 auto status =
1465 callback->onFailure(strlen(iface_hapd->conf->bridge) > 0 ?
Les Leee08c2862021-10-29 16:36:41 +08001466 iface_hapd->conf->bridge : iface_hapd->conf->iface,
Les Lee399c6302024-09-12 03:03:38 +00001467 instanceName);
Chenming Huangbaa5e582024-04-02 08:21:10 +05301468 if (!status.isOk()) {
1469 wpa_printf(MSG_ERROR, "Failed to invoke onFailure");
1470 }
Yu Ouyang378d3c42021-08-20 17:31:08 +08001471 }
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001472 }
Yu Ouyang378d3c42021-08-20 17:31:08 +08001473 };
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001474
1475 // Setup callback
1476 iface_hapd->setup_complete_cb = onAsyncSetupCompleteCb;
1477 iface_hapd->setup_complete_cb_ctx = iface_hapd;
1478 iface_hapd->sta_authorized_cb = onAsyncStaAuthorizedCb;
1479 iface_hapd->sta_authorized_cb_ctx = iface_hapd;
Hu Wang7c5a4322021-06-24 17:24:59 +08001480 wpa_msg_register_aidl_cb(onAsyncWpaEventCb);
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001481
Les Lee399c6302024-09-12 03:03:38 +00001482 // Multi-link MLO should enable iface after both links have been set.
1483 if (!iface_params.usesMlo && hostapd_enable_iface(iface_hapd->iface) < 0) {
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001484 wpa_printf(
1485 MSG_ERROR, "Enabling interface %s failed",
1486 iface_params.name.c_str());
1487 return createStatus(HostapdStatusCode::FAILURE_UNKNOWN);
1488 }
1489 return ndk::ScopedAStatus::ok();
1490}
1491
1492::ndk::ScopedAStatus Hostapd::removeAccessPointInternal(const std::string& iface_name)
1493{
1494 // interfaces to be removed
1495 std::vector<std::string> interfaces;
1496 bool is_error = false;
1497
1498 const auto it = br_interfaces_.find(iface_name);
1499 if (it != br_interfaces_.end()) {
1500 // In case bridge, remove managed interfaces
1501 interfaces = it->second;
1502 br_interfaces_.erase(iface_name);
1503 } else {
1504 // else remove current interface
1505 interfaces.push_back(iface_name);
1506 }
1507
1508 for (auto& iface : interfaces) {
1509 std::vector<char> remove_iface_param_vec(
1510 iface.begin(), iface.end() + 1);
1511 if (hostapd_remove_iface(interfaces_, remove_iface_param_vec.data()) < 0) {
1512 wpa_printf(MSG_INFO, "Remove interface %s failed", iface.c_str());
1513 is_error = true;
1514 }
1515 }
1516 if (is_error) {
1517 return createStatus(HostapdStatusCode::FAILURE_UNKNOWN);
1518 }
1519 return ndk::ScopedAStatus::ok();
1520}
1521
1522::ndk::ScopedAStatus Hostapd::registerCallbackInternal(
1523 const std::shared_ptr<IHostapdCallback>& callback)
1524{
1525 binder_status_t status = AIBinder_linkToDeath(callback->asBinder().get(),
1526 death_notifier_, this /* cookie */);
1527 if (status != STATUS_OK) {
1528 wpa_printf(
1529 MSG_ERROR,
1530 "Error registering for death notification for "
1531 "hostapd callback object");
1532 return createStatus(HostapdStatusCode::FAILURE_UNKNOWN);
1533 }
1534 callbacks_.push_back(callback);
Manaswini Paluri76ae6982024-02-29 14:49:20 +05301535 if (aidl_service_version == 0) {
1536 aidl_service_version = Hostapd::version;
1537 wpa_printf(MSG_INFO, "AIDL service version: %d", aidl_service_version);
1538 }
1539 if (aidl_client_version == 0) {
1540 callback->getInterfaceVersion(&aidl_client_version);
1541 wpa_printf(MSG_INFO, "AIDL client version: %d", aidl_client_version);
1542 }
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001543 return ndk::ScopedAStatus::ok();
1544}
1545
1546::ndk::ScopedAStatus Hostapd::forceClientDisconnectInternal(const std::string& iface_name,
1547 const std::vector<uint8_t>& client_address, Ieee80211ReasonCode reason_code)
1548{
1549 struct hostapd_data *hapd = hostapd_get_iface(interfaces_, iface_name.c_str());
1550 bool result;
1551 if (!hapd) {
1552 for (auto const& iface : br_interfaces_) {
1553 if (iface.first == iface_name) {
1554 for (auto const& instance : iface.second) {
1555 hapd = hostapd_get_iface(interfaces_, instance.c_str());
1556 if (hapd) {
1557 result = forceStaDisconnection(hapd, client_address,
1558 (uint16_t) reason_code);
1559 if (result) break;
1560 }
1561 }
1562 }
1563 }
1564 } else {
1565 result = forceStaDisconnection(hapd, client_address, (uint16_t) reason_code);
1566 }
1567 if (!hapd) {
1568 wpa_printf(MSG_ERROR, "Interface %s doesn't exist", iface_name.c_str());
1569 return createStatus(HostapdStatusCode::FAILURE_IFACE_UNKNOWN);
1570 }
1571 if (result) {
1572 return ndk::ScopedAStatus::ok();
1573 }
1574 return createStatus(HostapdStatusCode::FAILURE_CLIENT_UNKNOWN);
1575}
1576
1577::ndk::ScopedAStatus Hostapd::setDebugParamsInternal(DebugLevel level)
1578{
1579 wpa_debug_level = static_cast<uint32_t>(level);
1580 return ndk::ScopedAStatus::ok();
1581}
1582
1583} // namespace hostapd
1584} // namespace wifi
1585} // namespace hardware
1586} // namespace android
Les Leee08c2862021-10-29 16:36:41 +08001587} // namespace aidl