blob: 06bc90dfd60293de6bd561a82ecabc07aeaac224 [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"
Chenming Huang39c53902025-02-12 11:41:29 +0530884 "ctrl_interface=/data/vendor/wifi/hostapd/ctrl\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(),
Les Lee399c6302024-09-12 03:03:38 +0000910 ssid_as_string.c_str(),
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000911 channel_config_as_string.c_str(),
912 iface_params.hwModeParams.enable80211N ? 1 : 0,
913 iface_params.hwModeParams.enable80211AC ? 1 : 0,
914 he_params_as_string.c_str(),
Sunil Ravi65251732023-01-24 05:03:35 +0000915 eht_params_as_string.c_str(),
Xin Dengfec682f2024-02-06 22:59:39 -0800916 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 +0000917 nw_params.isHidden ? 1 : 0,
918#ifdef CONFIG_INTERWORKING
919 access_network_params_as_string.c_str(),
920#endif /* CONFIG_INTERWORKING */
921 encryption_config_as_string.c_str(),
922 bridge_as_string.c_str(),
Purushottam Kushwaha0316c882021-12-20 15:07:44 +0530923 owe_transition_ifname_as_string.c_str(),
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000924 enable_edmg_as_string.c_str(),
Serik Beketayev8af7a722021-12-23 12:25:36 -0800925 edmg_channel_as_string.c_str(),
Les Lee9dfae3b2024-10-25 18:31:47 +0000926 vendor_elements_as_string.c_str(),
927 ap_isolation_as_string.c_str());
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000928}
929
930Generation getGeneration(hostapd_hw_modes *current_mode)
931{
932 wpa_printf(MSG_DEBUG, "getGeneration hwmode=%d, ht_enabled=%d,"
933 " vht_enabled=%d, he_supported=%d",
934 current_mode->mode, current_mode->ht_capab != 0,
935 current_mode->vht_capab != 0, current_mode->he_capab->he_supported);
936 switch (current_mode->mode) {
937 case HOSTAPD_MODE_IEEE80211B:
938 return Generation::WIFI_STANDARD_LEGACY;
939 case HOSTAPD_MODE_IEEE80211G:
Les Leec330c242024-12-18 22:27:43 +0000940 if (current_mode->he_capab->he_supported) {
941 return Generation::WIFI_STANDARD_11AX;
942 }
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000943 return current_mode->ht_capab == 0 ?
944 Generation::WIFI_STANDARD_LEGACY : Generation::WIFI_STANDARD_11N;
945 case HOSTAPD_MODE_IEEE80211A:
946 if (current_mode->he_capab->he_supported) {
947 return Generation::WIFI_STANDARD_11AX;
948 }
949 return current_mode->vht_capab == 0 ?
950 Generation::WIFI_STANDARD_11N : Generation::WIFI_STANDARD_11AC;
951 case HOSTAPD_MODE_IEEE80211AD:
952 return Generation::WIFI_STANDARD_11AD;
953 default:
954 return Generation::WIFI_STANDARD_UNKNOWN;
955 }
956}
957
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800958ChannelBandwidth getChannelBandwidth(struct hostapd_config *iconf)
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000959{
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800960 wpa_printf(MSG_DEBUG, "getChannelBandwidth %d, isHT=%d, isHT40=%d",
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000961 iconf->vht_oper_chwidth, iconf->ieee80211n,
962 iconf->secondary_channel);
963 switch (iconf->vht_oper_chwidth) {
Sunil8cd6f4d2022-06-28 18:40:46 +0000964 case CONF_OPER_CHWIDTH_80MHZ:
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800965 return ChannelBandwidth::BANDWIDTH_80;
Sunil8cd6f4d2022-06-28 18:40:46 +0000966 case CONF_OPER_CHWIDTH_80P80MHZ:
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800967 return ChannelBandwidth::BANDWIDTH_80P80;
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000968 break;
Sunil8cd6f4d2022-06-28 18:40:46 +0000969 case CONF_OPER_CHWIDTH_160MHZ:
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800970 return ChannelBandwidth::BANDWIDTH_160;
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000971 break;
Sunil8cd6f4d2022-06-28 18:40:46 +0000972 case CONF_OPER_CHWIDTH_USE_HT:
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000973 if (iconf->ieee80211n) {
974 return iconf->secondary_channel != 0 ?
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800975 ChannelBandwidth::BANDWIDTH_40 : ChannelBandwidth::BANDWIDTH_20;
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000976 }
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800977 return ChannelBandwidth::BANDWIDTH_20_NOHT;
Sunil8cd6f4d2022-06-28 18:40:46 +0000978 case CONF_OPER_CHWIDTH_2160MHZ:
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800979 return ChannelBandwidth::BANDWIDTH_2160;
Sunil8cd6f4d2022-06-28 18:40:46 +0000980 case CONF_OPER_CHWIDTH_4320MHZ:
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800981 return ChannelBandwidth::BANDWIDTH_4320;
Sunil8cd6f4d2022-06-28 18:40:46 +0000982 case CONF_OPER_CHWIDTH_6480MHZ:
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800983 return ChannelBandwidth::BANDWIDTH_6480;
Sunil8cd6f4d2022-06-28 18:40:46 +0000984 case CONF_OPER_CHWIDTH_8640MHZ:
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800985 return ChannelBandwidth::BANDWIDTH_8640;
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000986 default:
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800987 return ChannelBandwidth::BANDWIDTH_INVALID;
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000988 }
989}
990
Chris Weir808564b2024-08-07 10:12:16 -0700991std::optional<struct sta_info*> getStaInfoByMacAddr(const struct hostapd_data* iface_hapd,
992 const u8 *mac_addr) {
993 if (iface_hapd == nullptr || mac_addr == nullptr){
994 wpa_printf(MSG_ERROR, "nullptr passsed to getStaInfoByMacAddr!");
995 return std::nullopt;
996 }
997
998 for (struct sta_info* sta_ptr = iface_hapd->sta_list; sta_ptr; sta_ptr = sta_ptr->next) {
999 int res;
1000 res = memcmp(sta_ptr->addr, mac_addr, ETH_ALEN);
1001 if (res == 0) {
1002 return sta_ptr;
1003 }
1004 }
1005 return std::nullopt;
1006}
1007
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001008bool forceStaDisconnection(struct hostapd_data* hapd,
1009 const std::vector<uint8_t>& client_address,
1010 const uint16_t reason_code) {
Sunil Ravi1a360892022-11-29 20:16:01 +00001011 if (client_address.size() != ETH_ALEN) {
1012 return false;
1013 }
Chris Weir808564b2024-08-07 10:12:16 -07001014
1015 auto sta_ptr_optional = getStaInfoByMacAddr(hapd, client_address.data());
1016 if (sta_ptr_optional.has_value()) {
1017 wpa_printf(MSG_INFO, "Force client:" MACSTR " disconnect with reason: %d",
1018 MAC2STR(client_address.data()), reason_code);
1019 ap_sta_disconnect(hapd, sta_ptr_optional.value(), sta_ptr_optional.value()->addr,
1020 reason_code);
1021 return true;
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001022 }
Chris Weir808564b2024-08-07 10:12:16 -07001023
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001024 return false;
1025}
1026
1027// hostapd core functions accept "C" style function pointers, so use global
1028// functions to pass to the hostapd core function and store the corresponding
1029// std::function methods to be invoked.
1030//
1031// NOTE: Using the pattern from the vendor HAL (wifi_legacy_hal.cpp).
1032//
1033// Callback to be invoked once setup is complete
1034std::function<void(struct hostapd_data*)> on_setup_complete_internal_callback;
1035void onAsyncSetupCompleteCb(void* ctx)
1036{
1037 struct hostapd_data* iface_hapd = (struct hostapd_data*)ctx;
1038 if (on_setup_complete_internal_callback) {
1039 on_setup_complete_internal_callback(iface_hapd);
1040 // Invalidate this callback since we don't want this firing
1041 // again in single AP mode.
1042 if (strlen(iface_hapd->conf->bridge) > 0) {
1043 on_setup_complete_internal_callback = nullptr;
1044 }
1045 }
1046}
1047
1048// Callback to be invoked on hotspot client connection/disconnection
1049std::function<void(struct hostapd_data*, const u8 *mac_addr, int authorized,
1050 const u8 *p2p_dev_addr)> on_sta_authorized_internal_callback;
1051void onAsyncStaAuthorizedCb(void* ctx, const u8 *mac_addr, int authorized,
Sunil Ravid8128a22023-11-06 23:53:58 +00001052 const u8 *p2p_dev_addr, const u8 *ip)
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001053{
1054 struct hostapd_data* iface_hapd = (struct hostapd_data*)ctx;
1055 if (on_sta_authorized_internal_callback) {
1056 on_sta_authorized_internal_callback(iface_hapd, mac_addr,
1057 authorized, p2p_dev_addr);
1058 }
1059}
1060
1061std::function<void(struct hostapd_data*, int level,
1062 enum wpa_msg_type type, const char *txt,
1063 size_t len)> on_wpa_msg_internal_callback;
1064
1065void onAsyncWpaEventCb(void *ctx, int level,
1066 enum wpa_msg_type type, const char *txt,
1067 size_t len)
1068{
1069 struct hostapd_data* iface_hapd = (struct hostapd_data*)ctx;
1070 if (on_wpa_msg_internal_callback) {
1071 on_wpa_msg_internal_callback(iface_hapd, level,
1072 type, txt, len);
1073 }
1074}
1075
1076inline ndk::ScopedAStatus createStatus(HostapdStatusCode status_code) {
1077 return ndk::ScopedAStatus::fromServiceSpecificError(
1078 static_cast<int32_t>(status_code));
1079}
1080
1081inline ndk::ScopedAStatus createStatusWithMsg(
1082 HostapdStatusCode status_code, std::string msg)
1083{
1084 return ndk::ScopedAStatus::fromServiceSpecificErrorWithMessage(
1085 static_cast<int32_t>(status_code), msg.c_str());
1086}
1087
1088// Method called by death_notifier_ on client death.
1089void onDeath(void* cookie) {
1090 wpa_printf(MSG_ERROR, "Client died. Terminating...");
1091 eloop_terminate();
1092}
1093
1094} // namespace
1095
1096namespace aidl {
1097namespace android {
1098namespace hardware {
1099namespace wifi {
1100namespace hostapd {
1101
1102Hostapd::Hostapd(struct hapd_interfaces* interfaces)
1103 : interfaces_(interfaces)
1104{
1105 death_notifier_ = AIBinder_DeathRecipient_new(onDeath);
1106}
1107
1108::ndk::ScopedAStatus Hostapd::addAccessPoint(
1109 const IfaceParams& iface_params, const NetworkParams& nw_params)
1110{
1111 return addAccessPointInternal(iface_params, nw_params);
1112}
1113
1114::ndk::ScopedAStatus Hostapd::removeAccessPoint(const std::string& iface_name)
1115{
1116 return removeAccessPointInternal(iface_name);
1117}
1118
1119::ndk::ScopedAStatus Hostapd::terminate()
1120{
1121 wpa_printf(MSG_INFO, "Terminating...");
1122 // Clear the callback to avoid IPCThreadState shutdown during the
1123 // callback event.
1124 callbacks_.clear();
1125 eloop_terminate();
1126 return ndk::ScopedAStatus::ok();
1127}
1128
1129::ndk::ScopedAStatus Hostapd::registerCallback(
1130 const std::shared_ptr<IHostapdCallback>& callback)
1131{
1132 return registerCallbackInternal(callback);
1133}
1134
1135::ndk::ScopedAStatus Hostapd::forceClientDisconnect(
1136 const std::string& iface_name, const std::vector<uint8_t>& client_address,
1137 Ieee80211ReasonCode reason_code)
1138{
1139 return forceClientDisconnectInternal(iface_name, client_address, reason_code);
1140}
1141
1142::ndk::ScopedAStatus Hostapd::setDebugParams(DebugLevel level)
1143{
1144 return setDebugParamsInternal(level);
1145}
1146
Les Lee8d291ea2024-11-19 22:18:50 +00001147::ndk::ScopedAStatus Hostapd::removeLinkFromMultipleLinkBridgedApIface(
1148 const std::string& iface_name, const std::string& linkIdentity)
1149{
1150 return removeLinkFromMultipleLinkBridgedApIfaceInternal(iface_name, linkIdentity);
1151}
1152
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001153::ndk::ScopedAStatus Hostapd::addAccessPointInternal(
1154 const IfaceParams& iface_params,
1155 const NetworkParams& nw_params)
1156{
1157 int channelParamsSize = iface_params.channelParams.size();
1158 if (channelParamsSize == 1) {
1159 // Single AP
1160 wpa_printf(MSG_INFO, "AddSingleAccessPoint, iface=%s",
1161 iface_params.name.c_str());
1162 return addSingleAccessPoint(iface_params, iface_params.channelParams[0],
Purushottam Kushwaha0316c882021-12-20 15:07:44 +05301163 nw_params, "", "");
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001164 } else if (channelParamsSize == 2) {
1165 // Concurrent APs
1166 wpa_printf(MSG_INFO, "AddDualAccessPoint, iface=%s",
1167 iface_params.name.c_str());
1168 return addConcurrentAccessPoints(iface_params, nw_params);
1169 }
1170 return createStatus(HostapdStatusCode::FAILURE_ARGS_INVALID);
1171}
1172
Purushottam Kushwaha0316c882021-12-20 15:07:44 +05301173std::vector<uint8_t> generateRandomOweSsid()
1174{
1175 u8 random[8] = {0};
1176 os_get_random(random, 8);
1177
1178 std::string ssid = StringPrintf("Owe-%s", random);
1179 wpa_printf(MSG_INFO, "Generated OWE SSID: %s", ssid.c_str());
1180 std::vector<uint8_t> vssid(ssid.begin(), ssid.end());
1181
1182 return vssid;
1183}
1184
Les Lee399c6302024-09-12 03:03:38 +00001185
1186// Both of bridged dual APs and MLO AP will be treated as concurrenct APs.
1187// -----------------------------------------
1188// | br_name | instance#1 | instance#2 |
1189// ___________________________________________________________
1190// bridged dual APs | ap_br_wlanX | wlan X | wlanY |
1191// ___________________________________________________________
1192// MLO AP | wlanX | 0 | 1 |
1193// ___________________________________________________________
1194// Both will be added in br_interfaces_[$br_name] and use instance's name
1195// to be iface_params_new.name to create single Access point.
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001196::ndk::ScopedAStatus Hostapd::addConcurrentAccessPoints(
1197 const IfaceParams& iface_params, const NetworkParams& nw_params)
1198{
1199 int channelParamsListSize = iface_params.channelParams.size();
1200 // Get available interfaces in bridge
Les Lee399c6302024-09-12 03:03:38 +00001201 std::vector<std::string> managed_instances;
1202 std::string br_name = StringPrintf("%s", iface_params.name.c_str());
1203 if (iface_params.usesMlo) {
1204 // MLO AP is using link id as instance.
1205 for (std::size_t i = 0; i < iface_params.instanceIdentities->size(); i++) {
1206 managed_instances.push_back(iface_params.instanceIdentities->at(i)->c_str());
1207 }
1208 } else {
1209 if (!GetInterfacesInBridge(br_name, &managed_instances)) {
1210 return createStatusWithMsg(HostapdStatusCode::FAILURE_UNKNOWN,
1211 "Get interfaces in bridge failed.");
1212 }
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001213 }
Les Lee399c6302024-09-12 03:03:38 +00001214 // Either bridged AP or MLO AP should have two instances.
1215 if (managed_instances.size() < channelParamsListSize) {
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001216 return createStatusWithMsg(HostapdStatusCode::FAILURE_UNKNOWN,
Les Lee399c6302024-09-12 03:03:38 +00001217 "Available interfaces less than requested bands");
1218 }
1219
1220 if (iface_params.usesMlo
1221 && nw_params.encryptionType == EncryptionType::WPA3_OWE_TRANSITION) {
1222 return createStatusWithMsg(HostapdStatusCode::FAILURE_UNKNOWN,
1223 "Invalid encryptionType (OWE transition) for MLO SAP.");
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001224 }
1225 // start BSS on specified bands
1226 for (std::size_t i = 0; i < channelParamsListSize; i ++) {
1227 IfaceParams iface_params_new = iface_params;
Purushottam Kushwaha0316c882021-12-20 15:07:44 +05301228 NetworkParams nw_params_new = nw_params;
Purushottam Kushwaha0316c882021-12-20 15:07:44 +05301229 std::string owe_transition_ifname = "";
Les Lee399c6302024-09-12 03:03:38 +00001230 iface_params_new.name = managed_instances[i];
Ahmed ElArabawy1aaf1802022-02-04 15:58:55 -08001231 if (nw_params.encryptionType == EncryptionType::WPA3_OWE_TRANSITION) {
Purushottam Kushwaha0316c882021-12-20 15:07:44 +05301232 if (i == 0 && i+1 < channelParamsListSize) {
Les Lee399c6302024-09-12 03:03:38 +00001233 owe_transition_ifname = managed_instances[i+1];
Purushottam Kushwaha0316c882021-12-20 15:07:44 +05301234 nw_params_new.encryptionType = EncryptionType::NONE;
1235 } else {
Les Lee399c6302024-09-12 03:03:38 +00001236 owe_transition_ifname = managed_instances[0];
Purushottam Kushwaha0316c882021-12-20 15:07:44 +05301237 nw_params_new.isHidden = true;
1238 nw_params_new.ssid = generateRandomOweSsid();
1239 }
1240 }
1241
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001242 ndk::ScopedAStatus status = addSingleAccessPoint(
Purushottam Kushwaha0316c882021-12-20 15:07:44 +05301243 iface_params_new, iface_params.channelParams[i], nw_params_new,
1244 br_name, owe_transition_ifname);
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001245 if (!status.isOk()) {
1246 wpa_printf(MSG_ERROR, "Failed to addAccessPoint %s",
Les Lee399c6302024-09-12 03:03:38 +00001247 managed_instances[i].c_str());
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001248 return status;
1249 }
1250 }
Les Lee399c6302024-09-12 03:03:38 +00001251
1252 if (iface_params.usesMlo) {
1253 std::size_t i = 0;
1254 std::size_t j = 0;
1255 for (i = 0; i < interfaces_->count; i++) {
1256 struct hostapd_iface *iface = interfaces_->iface[i];
1257
1258 for (j = 0; j < iface->num_bss; j++) {
1259 struct hostapd_data *iface_hapd = iface->bss[j];
1260 if (hostapd_enable_iface(iface_hapd->iface) < 0) {
1261 wpa_printf(
1262 MSG_ERROR, "Enabling interface %s failed on %zu",
1263 iface_params.name.c_str(), i);
1264 return createStatus(HostapdStatusCode::FAILURE_UNKNOWN);
1265 }
1266 }
1267 }
1268 }
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001269 // Save bridge interface info
Les Lee399c6302024-09-12 03:03:38 +00001270 br_interfaces_[br_name] = managed_instances;
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001271 return ndk::ScopedAStatus::ok();
1272}
1273
Les Lee399c6302024-09-12 03:03:38 +00001274struct hostapd_data * hostapd_get_iface_by_link_id(struct hapd_interfaces *interfaces,
1275 const size_t link_id)
1276{
1277#ifdef CONFIG_IEEE80211BE
1278 size_t i, j;
1279
1280 for (i = 0; i < interfaces->count; i++) {
1281 struct hostapd_iface *iface = interfaces->iface[i];
1282
1283 for (j = 0; j < iface->num_bss; j++) {
1284 struct hostapd_data *hapd = iface->bss[j];
1285
1286 if (link_id == hapd->mld_link_id)
1287 return hapd;
1288 }
1289 }
Les Lee16bb41b2024-12-03 01:50:22 +00001290#endif /* CONFIG_IEEE80211BE */
Les Lee399c6302024-09-12 03:03:38 +00001291 return NULL;
1292}
1293
1294// Both of bridged dual APs and MLO AP will be treated as concurrenct APs.
1295// -----------------------------------------
1296// | br_name | iface_params.name
1297// _______________________________________________________________
1298// bridged dual APs | bridged interface name | interface name
1299// _______________________________________________________________
1300// MLO AP | AP interface name | mld link id as instance name
1301// _______________________________________________________________
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001302::ndk::ScopedAStatus Hostapd::addSingleAccessPoint(
1303 const IfaceParams& iface_params,
1304 const ChannelParams& channelParams,
1305 const NetworkParams& nw_params,
Purushottam Kushwaha0316c882021-12-20 15:07:44 +05301306 const std::string br_name,
1307 const std::string owe_transition_ifname)
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001308{
Les Lee399c6302024-09-12 03:03:38 +00001309 if (iface_params.usesMlo) { // the mlo case, iface name is instance name which is mld_link_id
Xin Deng62e2e842024-12-20 01:40:20 -08001310 if (hostapd_get_iface_by_link_id(interfaces_, std::stoi(iface_params.name.c_str()))) {
Les Lee399c6302024-09-12 03:03:38 +00001311 wpa_printf(
1312 MSG_ERROR, "Instance link id %s already present",
1313 iface_params.name.c_str());
1314 return createStatus(HostapdStatusCode::FAILURE_IFACE_EXISTS);
1315 }
Les Leeb6386ef2024-12-20 23:26:12 +00001316#ifdef CONFIG_IEEE80211BE
1317 // The MLO AP uses the same interface name for all links. Thus, make sure the
1318 // interface name wasn't used for non-mld AP only when adding a new interface.
1319 // Also it is valid to have a hostapd_data with the same interface name when adding
1320 // the second link instance.
1321 struct hostapd_data* hapd = hostapd_get_iface(interfaces_, br_name.c_str());
1322 if (hapd && !hapd->conf->mld_ap) {
1323 wpa_printf(
1324 MSG_ERROR, "Instance interface %s already present",
1325 br_name.c_str());
1326 return createStatus(HostapdStatusCode::FAILURE_IFACE_EXISTS);
1327 }
1328#endif
1329 } else {
1330 if (hostapd_get_iface(interfaces_, iface_params.name.c_str())) {
1331 wpa_printf(
1332 MSG_ERROR, "Instance interface %s already present",
1333 iface_params.name.c_str());
1334 return createStatus(HostapdStatusCode::FAILURE_IFACE_EXISTS);
1335 }
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001336 }
Purushottam Kushwaha0316c882021-12-20 15:07:44 +05301337 const auto conf_params = CreateHostapdConfig(iface_params, channelParams, nw_params,
1338 br_name, owe_transition_ifname);
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001339 if (conf_params.empty()) {
1340 wpa_printf(MSG_ERROR, "Failed to create config params");
1341 return createStatus(HostapdStatusCode::FAILURE_ARGS_INVALID);
1342 }
1343 const auto conf_file_path =
Les Lee399c6302024-09-12 03:03:38 +00001344 WriteHostapdConfig(iface_params.name, conf_params, br_name, iface_params.usesMlo);
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001345 if (conf_file_path.empty()) {
1346 wpa_printf(MSG_ERROR, "Failed to write config file");
1347 return createStatus(HostapdStatusCode::FAILURE_UNKNOWN);
1348 }
1349 std::string add_iface_param_str = StringPrintf(
Les Lee399c6302024-09-12 03:03:38 +00001350 "%s config=%s", iface_params.usesMlo ? br_name.c_str(): iface_params.name.c_str(),
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001351 conf_file_path.c_str());
1352 std::vector<char> add_iface_param_vec(
1353 add_iface_param_str.begin(), add_iface_param_str.end() + 1);
1354 if (hostapd_add_iface(interfaces_, add_iface_param_vec.data()) < 0) {
1355 wpa_printf(
Les Lee399c6302024-09-12 03:03:38 +00001356 MSG_ERROR, "Adding hostapd iface %s failed",
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001357 add_iface_param_str.c_str());
1358 return createStatus(HostapdStatusCode::FAILURE_UNKNOWN);
1359 }
Les Lee399c6302024-09-12 03:03:38 +00001360
1361 // find the iface and set up callback.
1362 struct hostapd_data* iface_hapd = iface_params.usesMlo ?
Xin Deng62e2e842024-12-20 01:40:20 -08001363 hostapd_get_iface_by_link_id(interfaces_, std::stoi(iface_params.name.c_str())) :
Les Lee399c6302024-09-12 03:03:38 +00001364 hostapd_get_iface(interfaces_, iface_params.name.c_str());
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001365 WPA_ASSERT(iface_hapd != nullptr && iface_hapd->iface != nullptr);
Les Lee399c6302024-09-12 03:03:38 +00001366 if (iface_params.usesMlo) {
1367 memcmp(iface_hapd->conf->iface, br_name.c_str(), br_name.size());
1368 }
1369
1370 // Callback discrepancy between bridged dual APs and MLO AP
1371 // Note: Only bridged dual APs will have "iface_hapd->conf->bridge" and
1372 // Only MLO AP will have "iface_hapd->mld_link_id"
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001373 // Register the setup complete callbacks
Les Lee399c6302024-09-12 03:03:38 +00001374 // -----------------------------------------
1375 // | bridged dual APs | bridged single link MLO | MLO SAP
1376 // _________________________________________________________________________________________
1377 // hapd->conf->bridge | bridged interface name | bridged interface nam | N/A
1378 // _________________________________________________________________________________________
1379 // hapd->conf->iface | AP interface name | AP interface name | AP interface name
1380 // _________________________________________________________________________________________
1381 // hapd->mld_link_id | 0 (default value) | link id (0) | link id (0 or 1)
1382 // _________________________________________________________________________________________
1383 // hapd->mld_ap | 0 | 1 | 1
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001384 on_setup_complete_internal_callback =
1385 [this](struct hostapd_data* iface_hapd) {
1386 wpa_printf(
1387 MSG_INFO, "AP interface setup completed - state %s",
1388 hostapd_state_text(iface_hapd->iface->state));
1389 if (iface_hapd->iface->state == HAPD_IFACE_DISABLED) {
1390 // Invoke the failure callback on all registered
1391 // clients.
Les Lee399c6302024-09-12 03:03:38 +00001392 std::string instanceName = iface_hapd->conf->iface;
1393#ifdef CONFIG_IEEE80211BE
1394 if (iface_hapd->conf->mld_ap
1395 && strlen(iface_hapd->conf->bridge) == 0) {
1396 instanceName = std::to_string(iface_hapd->mld_link_id);
1397 }
Les Lee16bb41b2024-12-03 01:50:22 +00001398#endif /* CONFIG_IEEE80211BE */
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001399 for (const auto& callback : callbacks_) {
Chenming Huangbaa5e582024-04-02 08:21:10 +05301400 auto status = callback->onFailure(
1401 strlen(iface_hapd->conf->bridge) > 0 ?
Les Leee08c2862021-10-29 16:36:41 +08001402 iface_hapd->conf->bridge : iface_hapd->conf->iface,
Les Lee399c6302024-09-12 03:03:38 +00001403 instanceName);
Chenming Huangbaa5e582024-04-02 08:21:10 +05301404 if (!status.isOk()) {
1405 wpa_printf(MSG_ERROR, "Failed to invoke onFailure");
1406 }
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001407 }
1408 }
1409 };
1410
1411 // Register for new client connect/disconnect indication.
1412 on_sta_authorized_internal_callback =
1413 [this](struct hostapd_data* iface_hapd, const u8 *mac_addr,
1414 int authorized, const u8 *p2p_dev_addr) {
1415 wpa_printf(MSG_DEBUG, "notify client " MACSTR " %s",
1416 MAC2STR(mac_addr),
1417 (authorized) ? "Connected" : "Disconnected");
1418 ClientInfo info;
1419 info.ifaceName = strlen(iface_hapd->conf->bridge) > 0 ?
1420 iface_hapd->conf->bridge : iface_hapd->conf->iface;
Les Lee399c6302024-09-12 03:03:38 +00001421 std::string instanceName = iface_hapd->conf->iface;
1422#ifdef CONFIG_IEEE80211BE
1423 if (iface_hapd->conf->mld_ap
1424 && strlen(iface_hapd->conf->bridge) == 0) {
1425 instanceName = std::to_string(iface_hapd->mld_link_id);
1426 }
Les Lee16bb41b2024-12-03 01:50:22 +00001427#endif /* CONFIG_IEEE80211BE */
Les Lee399c6302024-09-12 03:03:38 +00001428 info.apIfaceInstance = instanceName;
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001429 info.clientAddress.assign(mac_addr, mac_addr + ETH_ALEN);
1430 info.isConnected = authorized;
Chris Weir808564b2024-08-07 10:12:16 -07001431 if(isAidlServiceVersionAtLeast(3) && !authorized) {
1432 u16 disconnect_reason_code = WLAN_REASON_UNSPECIFIED;
1433 auto sta_ptr_optional = getStaInfoByMacAddr(iface_hapd, mac_addr);
1434 if (sta_ptr_optional.has_value()){
1435 disconnect_reason_code = sta_ptr_optional.value()->deauth_reason;
1436 }
1437 info.disconnectReasonCode =
1438 static_cast<common::DeauthenticationReasonCode>(disconnect_reason_code);
1439 }
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001440 for (const auto &callback : callbacks_) {
Chenming Huangbaa5e582024-04-02 08:21:10 +05301441 auto status = callback->onConnectedClientsChanged(info);
1442 if (!status.isOk()) {
1443 wpa_printf(MSG_ERROR, "Failed to invoke onConnectedClientsChanged");
1444 }
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001445 }
1446 };
1447
1448 // Register for wpa_event which used to get channel switch event
1449 on_wpa_msg_internal_callback =
1450 [this](struct hostapd_data* iface_hapd, int level,
1451 enum wpa_msg_type type, const char *txt,
1452 size_t len) {
1453 wpa_printf(MSG_DEBUG, "Receive wpa msg : %s", txt);
1454 if (os_strncmp(txt, AP_EVENT_ENABLED,
1455 strlen(AP_EVENT_ENABLED)) == 0 ||
1456 os_strncmp(txt, WPA_EVENT_CHANNEL_SWITCH,
1457 strlen(WPA_EVENT_CHANNEL_SWITCH)) == 0) {
Les Lee399c6302024-09-12 03:03:38 +00001458 std::string instanceName = iface_hapd->conf->iface;
1459#ifdef CONFIG_IEEE80211BE
1460 if (iface_hapd->conf->mld_ap && strlen(iface_hapd->conf->bridge) == 0) {
1461 instanceName = std::to_string(iface_hapd->mld_link_id);
1462 }
Les Lee16bb41b2024-12-03 01:50:22 +00001463#endif /* CONFIG_IEEE80211BE */
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001464 ApInfo info;
1465 info.ifaceName = strlen(iface_hapd->conf->bridge) > 0 ?
1466 iface_hapd->conf->bridge : iface_hapd->conf->iface,
Les Lee399c6302024-09-12 03:03:38 +00001467 info.apIfaceInstance = instanceName;
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001468 info.freqMhz = iface_hapd->iface->freq;
Ahmed ElArabawyb4115792022-02-08 09:33:01 -08001469 info.channelBandwidth = getChannelBandwidth(iface_hapd->iconf);
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001470 info.generation = getGeneration(iface_hapd->iface->current_mode);
1471 info.apIfaceInstanceMacAddress.assign(iface_hapd->own_addr,
1472 iface_hapd->own_addr + ETH_ALEN);
Les Lee16bb41b2024-12-03 01:50:22 +00001473#ifdef CONFIG_IEEE80211BE
1474 if (iface_hapd->conf->mld_ap) {
1475 info.mldMacAddress = macAddrToArray(iface_hapd->mld->mld_addr);
1476 }
1477#endif /* CONFIG_IEEE80211BE */
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001478 for (const auto &callback : callbacks_) {
Chenming Huangbaa5e582024-04-02 08:21:10 +05301479 auto status = callback->onApInstanceInfoChanged(info);
1480 if (!status.isOk()) {
1481 wpa_printf(MSG_ERROR,
1482 "Failed to invoke onApInstanceInfoChanged");
1483 }
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001484 }
Les Leea0c90cb2022-04-19 17:39:23 +08001485 } else if (os_strncmp(txt, AP_EVENT_DISABLED, strlen(AP_EVENT_DISABLED)) == 0
1486 || os_strncmp(txt, INTERFACE_DISABLED, strlen(INTERFACE_DISABLED)) == 0)
1487 {
Les Lee399c6302024-09-12 03:03:38 +00001488 std::string instanceName = iface_hapd->conf->iface;
1489#ifdef CONFIG_IEEE80211BE
1490 if (iface_hapd->conf->mld_ap && strlen(iface_hapd->conf->bridge) == 0) {
1491 instanceName = std::to_string(iface_hapd->mld_link_id);
1492 }
Les Lee16bb41b2024-12-03 01:50:22 +00001493#endif /* CONFIG_IEEE80211BE */
Yu Ouyang378d3c42021-08-20 17:31:08 +08001494 // Invoke the failure callback on all registered clients.
1495 for (const auto& callback : callbacks_) {
Les Lee399c6302024-09-12 03:03:38 +00001496 auto status =
1497 callback->onFailure(strlen(iface_hapd->conf->bridge) > 0 ?
Les Leee08c2862021-10-29 16:36:41 +08001498 iface_hapd->conf->bridge : iface_hapd->conf->iface,
Les Lee399c6302024-09-12 03:03:38 +00001499 instanceName);
Chenming Huangbaa5e582024-04-02 08:21:10 +05301500 if (!status.isOk()) {
1501 wpa_printf(MSG_ERROR, "Failed to invoke onFailure");
1502 }
Yu Ouyang378d3c42021-08-20 17:31:08 +08001503 }
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001504 }
Yu Ouyang378d3c42021-08-20 17:31:08 +08001505 };
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001506
1507 // Setup callback
1508 iface_hapd->setup_complete_cb = onAsyncSetupCompleteCb;
1509 iface_hapd->setup_complete_cb_ctx = iface_hapd;
1510 iface_hapd->sta_authorized_cb = onAsyncStaAuthorizedCb;
1511 iface_hapd->sta_authorized_cb_ctx = iface_hapd;
Hu Wang7c5a4322021-06-24 17:24:59 +08001512 wpa_msg_register_aidl_cb(onAsyncWpaEventCb);
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001513
Les Lee399c6302024-09-12 03:03:38 +00001514 // Multi-link MLO should enable iface after both links have been set.
1515 if (!iface_params.usesMlo && hostapd_enable_iface(iface_hapd->iface) < 0) {
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001516 wpa_printf(
1517 MSG_ERROR, "Enabling interface %s failed",
1518 iface_params.name.c_str());
1519 return createStatus(HostapdStatusCode::FAILURE_UNKNOWN);
1520 }
1521 return ndk::ScopedAStatus::ok();
1522}
1523
1524::ndk::ScopedAStatus Hostapd::removeAccessPointInternal(const std::string& iface_name)
1525{
1526 // interfaces to be removed
1527 std::vector<std::string> interfaces;
1528 bool is_error = false;
1529
1530 const auto it = br_interfaces_.find(iface_name);
1531 if (it != br_interfaces_.end()) {
1532 // In case bridge, remove managed interfaces
1533 interfaces = it->second;
1534 br_interfaces_.erase(iface_name);
1535 } else {
1536 // else remove current interface
1537 interfaces.push_back(iface_name);
1538 }
1539
1540 for (auto& iface : interfaces) {
1541 std::vector<char> remove_iface_param_vec(
1542 iface.begin(), iface.end() + 1);
1543 if (hostapd_remove_iface(interfaces_, remove_iface_param_vec.data()) < 0) {
1544 wpa_printf(MSG_INFO, "Remove interface %s failed", iface.c_str());
1545 is_error = true;
1546 }
1547 }
1548 if (is_error) {
1549 return createStatus(HostapdStatusCode::FAILURE_UNKNOWN);
1550 }
1551 return ndk::ScopedAStatus::ok();
1552}
1553
1554::ndk::ScopedAStatus Hostapd::registerCallbackInternal(
1555 const std::shared_ptr<IHostapdCallback>& callback)
1556{
1557 binder_status_t status = AIBinder_linkToDeath(callback->asBinder().get(),
1558 death_notifier_, this /* cookie */);
1559 if (status != STATUS_OK) {
1560 wpa_printf(
1561 MSG_ERROR,
1562 "Error registering for death notification for "
1563 "hostapd callback object");
1564 return createStatus(HostapdStatusCode::FAILURE_UNKNOWN);
1565 }
1566 callbacks_.push_back(callback);
Manaswini Paluri76ae6982024-02-29 14:49:20 +05301567 if (aidl_service_version == 0) {
1568 aidl_service_version = Hostapd::version;
1569 wpa_printf(MSG_INFO, "AIDL service version: %d", aidl_service_version);
1570 }
1571 if (aidl_client_version == 0) {
1572 callback->getInterfaceVersion(&aidl_client_version);
1573 wpa_printf(MSG_INFO, "AIDL client version: %d", aidl_client_version);
1574 }
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001575 return ndk::ScopedAStatus::ok();
1576}
1577
1578::ndk::ScopedAStatus Hostapd::forceClientDisconnectInternal(const std::string& iface_name,
1579 const std::vector<uint8_t>& client_address, Ieee80211ReasonCode reason_code)
1580{
1581 struct hostapd_data *hapd = hostapd_get_iface(interfaces_, iface_name.c_str());
1582 bool result;
1583 if (!hapd) {
1584 for (auto const& iface : br_interfaces_) {
1585 if (iface.first == iface_name) {
1586 for (auto const& instance : iface.second) {
1587 hapd = hostapd_get_iface(interfaces_, instance.c_str());
1588 if (hapd) {
1589 result = forceStaDisconnection(hapd, client_address,
1590 (uint16_t) reason_code);
1591 if (result) break;
1592 }
1593 }
1594 }
1595 }
1596 } else {
1597 result = forceStaDisconnection(hapd, client_address, (uint16_t) reason_code);
1598 }
1599 if (!hapd) {
1600 wpa_printf(MSG_ERROR, "Interface %s doesn't exist", iface_name.c_str());
1601 return createStatus(HostapdStatusCode::FAILURE_IFACE_UNKNOWN);
1602 }
1603 if (result) {
1604 return ndk::ScopedAStatus::ok();
1605 }
1606 return createStatus(HostapdStatusCode::FAILURE_CLIENT_UNKNOWN);
1607}
1608
1609::ndk::ScopedAStatus Hostapd::setDebugParamsInternal(DebugLevel level)
1610{
1611 wpa_debug_level = static_cast<uint32_t>(level);
1612 return ndk::ScopedAStatus::ok();
1613}
1614
Les Lee8d291ea2024-11-19 22:18:50 +00001615::ndk::ScopedAStatus Hostapd::removeLinkFromMultipleLinkBridgedApIfaceInternal(
1616const std::string& iface_name, const std::string& linkIdentity)
1617{
Les Lee16bb41b2024-12-03 01:50:22 +00001618#ifdef CONFIG_IEEE80211BE
Les Lee8d291ea2024-11-19 22:18:50 +00001619 if (!hostapd_get_iface(interfaces_, iface_name.c_str())) {
1620 wpa_printf(MSG_ERROR, "Interface %s doesn't exist", iface_name.c_str());
1621 return createStatus(HostapdStatusCode::FAILURE_IFACE_UNKNOWN);
1622 }
1623 struct hostapd_data* iface_hapd =
Xin Deng62e2e842024-12-20 01:40:20 -08001624 hostapd_get_iface_by_link_id(interfaces_, std::stoi(linkIdentity.c_str()));
Les Lee8d291ea2024-11-19 22:18:50 +00001625 if (iface_hapd) {
Les Lee16bb41b2024-12-03 01:50:22 +00001626// Currently, hostapd_link_remove is still under CONFIG_TESTING_OPTIONS.
1627// TODO: b/340821197 - Make sure to take out the hostapd_link_remove() and other related code
1628// out of CONFIG_TESTING_OPTIONS.
1629#ifdef CONFIG_TESTING_OPTIONS
Les Lee8d291ea2024-11-19 22:18:50 +00001630 if (0 == hostapd_link_remove(iface_hapd, 1)) {
1631 return ndk::ScopedAStatus::ok();
1632 }
Les Lee16bb41b2024-12-03 01:50:22 +00001633#endif /* CONFIG_TESTING_OPTIONS */
Les Lee8d291ea2024-11-19 22:18:50 +00001634 }
1635 return createStatus(HostapdStatusCode::FAILURE_ARGS_INVALID);
Les Lee16bb41b2024-12-03 01:50:22 +00001636#endif /* CONFIG_IEEE80211BE */
1637 return createStatus(HostapdStatusCode::FAILURE_UNKNOWN);
Les Lee8d291ea2024-11-19 22:18:50 +00001638}
1639
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001640} // namespace hostapd
1641} // namespace wifi
1642} // namespace hardware
1643} // namespace android
Les Leee08c2862021-10-29 16:36:41 +08001644} // namespace aidl