blob: 12d0d9e750e24c5ae679fa138b580cedc33a57c6 [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
Les Lee8d291ea2024-11-19 22:18:50 +00001139::ndk::ScopedAStatus Hostapd::removeLinkFromMultipleLinkBridgedApIface(
1140 const std::string& iface_name, const std::string& linkIdentity)
1141{
1142 return removeLinkFromMultipleLinkBridgedApIfaceInternal(iface_name, linkIdentity);
1143}
1144
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001145::ndk::ScopedAStatus Hostapd::addAccessPointInternal(
1146 const IfaceParams& iface_params,
1147 const NetworkParams& nw_params)
1148{
1149 int channelParamsSize = iface_params.channelParams.size();
1150 if (channelParamsSize == 1) {
1151 // Single AP
1152 wpa_printf(MSG_INFO, "AddSingleAccessPoint, iface=%s",
1153 iface_params.name.c_str());
1154 return addSingleAccessPoint(iface_params, iface_params.channelParams[0],
Purushottam Kushwaha0316c882021-12-20 15:07:44 +05301155 nw_params, "", "");
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001156 } else if (channelParamsSize == 2) {
1157 // Concurrent APs
1158 wpa_printf(MSG_INFO, "AddDualAccessPoint, iface=%s",
1159 iface_params.name.c_str());
1160 return addConcurrentAccessPoints(iface_params, nw_params);
1161 }
1162 return createStatus(HostapdStatusCode::FAILURE_ARGS_INVALID);
1163}
1164
Purushottam Kushwaha0316c882021-12-20 15:07:44 +05301165std::vector<uint8_t> generateRandomOweSsid()
1166{
1167 u8 random[8] = {0};
1168 os_get_random(random, 8);
1169
1170 std::string ssid = StringPrintf("Owe-%s", random);
1171 wpa_printf(MSG_INFO, "Generated OWE SSID: %s", ssid.c_str());
1172 std::vector<uint8_t> vssid(ssid.begin(), ssid.end());
1173
1174 return vssid;
1175}
1176
Les Lee399c6302024-09-12 03:03:38 +00001177
1178// Both of bridged dual APs and MLO AP will be treated as concurrenct APs.
1179// -----------------------------------------
1180// | br_name | instance#1 | instance#2 |
1181// ___________________________________________________________
1182// bridged dual APs | ap_br_wlanX | wlan X | wlanY |
1183// ___________________________________________________________
1184// MLO AP | wlanX | 0 | 1 |
1185// ___________________________________________________________
1186// Both will be added in br_interfaces_[$br_name] and use instance's name
1187// to be iface_params_new.name to create single Access point.
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001188::ndk::ScopedAStatus Hostapd::addConcurrentAccessPoints(
1189 const IfaceParams& iface_params, const NetworkParams& nw_params)
1190{
1191 int channelParamsListSize = iface_params.channelParams.size();
1192 // Get available interfaces in bridge
Les Lee399c6302024-09-12 03:03:38 +00001193 std::vector<std::string> managed_instances;
1194 std::string br_name = StringPrintf("%s", iface_params.name.c_str());
1195 if (iface_params.usesMlo) {
1196 // MLO AP is using link id as instance.
1197 for (std::size_t i = 0; i < iface_params.instanceIdentities->size(); i++) {
1198 managed_instances.push_back(iface_params.instanceIdentities->at(i)->c_str());
1199 }
1200 } else {
1201 if (!GetInterfacesInBridge(br_name, &managed_instances)) {
1202 return createStatusWithMsg(HostapdStatusCode::FAILURE_UNKNOWN,
1203 "Get interfaces in bridge failed.");
1204 }
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001205 }
Les Lee399c6302024-09-12 03:03:38 +00001206 // Either bridged AP or MLO AP should have two instances.
1207 if (managed_instances.size() < channelParamsListSize) {
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001208 return createStatusWithMsg(HostapdStatusCode::FAILURE_UNKNOWN,
Les Lee399c6302024-09-12 03:03:38 +00001209 "Available interfaces less than requested bands");
1210 }
1211
1212 if (iface_params.usesMlo
1213 && nw_params.encryptionType == EncryptionType::WPA3_OWE_TRANSITION) {
1214 return createStatusWithMsg(HostapdStatusCode::FAILURE_UNKNOWN,
1215 "Invalid encryptionType (OWE transition) for MLO SAP.");
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001216 }
1217 // start BSS on specified bands
1218 for (std::size_t i = 0; i < channelParamsListSize; i ++) {
1219 IfaceParams iface_params_new = iface_params;
Purushottam Kushwaha0316c882021-12-20 15:07:44 +05301220 NetworkParams nw_params_new = nw_params;
Purushottam Kushwaha0316c882021-12-20 15:07:44 +05301221 std::string owe_transition_ifname = "";
Les Lee399c6302024-09-12 03:03:38 +00001222 iface_params_new.name = managed_instances[i];
Ahmed ElArabawy1aaf1802022-02-04 15:58:55 -08001223 if (nw_params.encryptionType == EncryptionType::WPA3_OWE_TRANSITION) {
Purushottam Kushwaha0316c882021-12-20 15:07:44 +05301224 if (i == 0 && i+1 < channelParamsListSize) {
Les Lee399c6302024-09-12 03:03:38 +00001225 owe_transition_ifname = managed_instances[i+1];
Purushottam Kushwaha0316c882021-12-20 15:07:44 +05301226 nw_params_new.encryptionType = EncryptionType::NONE;
1227 } else {
Les Lee399c6302024-09-12 03:03:38 +00001228 owe_transition_ifname = managed_instances[0];
Purushottam Kushwaha0316c882021-12-20 15:07:44 +05301229 nw_params_new.isHidden = true;
1230 nw_params_new.ssid = generateRandomOweSsid();
1231 }
1232 }
1233
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001234 ndk::ScopedAStatus status = addSingleAccessPoint(
Purushottam Kushwaha0316c882021-12-20 15:07:44 +05301235 iface_params_new, iface_params.channelParams[i], nw_params_new,
1236 br_name, owe_transition_ifname);
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001237 if (!status.isOk()) {
1238 wpa_printf(MSG_ERROR, "Failed to addAccessPoint %s",
Les Lee399c6302024-09-12 03:03:38 +00001239 managed_instances[i].c_str());
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001240 return status;
1241 }
1242 }
Les Lee399c6302024-09-12 03:03:38 +00001243
1244 if (iface_params.usesMlo) {
1245 std::size_t i = 0;
1246 std::size_t j = 0;
1247 for (i = 0; i < interfaces_->count; i++) {
1248 struct hostapd_iface *iface = interfaces_->iface[i];
1249
1250 for (j = 0; j < iface->num_bss; j++) {
1251 struct hostapd_data *iface_hapd = iface->bss[j];
1252 if (hostapd_enable_iface(iface_hapd->iface) < 0) {
1253 wpa_printf(
1254 MSG_ERROR, "Enabling interface %s failed on %zu",
1255 iface_params.name.c_str(), i);
1256 return createStatus(HostapdStatusCode::FAILURE_UNKNOWN);
1257 }
1258 }
1259 }
1260 }
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001261 // Save bridge interface info
Les Lee399c6302024-09-12 03:03:38 +00001262 br_interfaces_[br_name] = managed_instances;
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001263 return ndk::ScopedAStatus::ok();
1264}
1265
Les Lee399c6302024-09-12 03:03:38 +00001266struct hostapd_data * hostapd_get_iface_by_link_id(struct hapd_interfaces *interfaces,
1267 const size_t link_id)
1268{
1269#ifdef CONFIG_IEEE80211BE
1270 size_t i, j;
1271
1272 for (i = 0; i < interfaces->count; i++) {
1273 struct hostapd_iface *iface = interfaces->iface[i];
1274
1275 for (j = 0; j < iface->num_bss; j++) {
1276 struct hostapd_data *hapd = iface->bss[j];
1277
1278 if (link_id == hapd->mld_link_id)
1279 return hapd;
1280 }
1281 }
1282#endif
1283 return NULL;
1284}
1285
1286// Both of bridged dual APs and MLO AP will be treated as concurrenct APs.
1287// -----------------------------------------
1288// | br_name | iface_params.name
1289// _______________________________________________________________
1290// bridged dual APs | bridged interface name | interface name
1291// _______________________________________________________________
1292// MLO AP | AP interface name | mld link id as instance name
1293// _______________________________________________________________
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001294::ndk::ScopedAStatus Hostapd::addSingleAccessPoint(
1295 const IfaceParams& iface_params,
1296 const ChannelParams& channelParams,
1297 const NetworkParams& nw_params,
Purushottam Kushwaha0316c882021-12-20 15:07:44 +05301298 const std::string br_name,
1299 const std::string owe_transition_ifname)
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001300{
Les Lee399c6302024-09-12 03:03:38 +00001301 if (iface_params.usesMlo) { // the mlo case, iface name is instance name which is mld_link_id
1302 if (hostapd_get_iface_by_link_id(interfaces_, (size_t) iface_params.name.c_str())) {
1303 wpa_printf(
1304 MSG_ERROR, "Instance link id %s already present",
1305 iface_params.name.c_str());
1306 return createStatus(HostapdStatusCode::FAILURE_IFACE_EXISTS);
1307 }
1308 }
1309 if (hostapd_get_iface(interfaces_,
1310 iface_params.usesMlo ? br_name.c_str() : iface_params.name.c_str())) {
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001311 wpa_printf(
Les Lee399c6302024-09-12 03:03:38 +00001312 MSG_ERROR, "Instance interface %s already present",
1313 iface_params.usesMlo ? br_name.c_str() : iface_params.name.c_str());
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001314 return createStatus(HostapdStatusCode::FAILURE_IFACE_EXISTS);
1315 }
Purushottam Kushwaha0316c882021-12-20 15:07:44 +05301316 const auto conf_params = CreateHostapdConfig(iface_params, channelParams, nw_params,
1317 br_name, owe_transition_ifname);
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001318 if (conf_params.empty()) {
1319 wpa_printf(MSG_ERROR, "Failed to create config params");
1320 return createStatus(HostapdStatusCode::FAILURE_ARGS_INVALID);
1321 }
1322 const auto conf_file_path =
Les Lee399c6302024-09-12 03:03:38 +00001323 WriteHostapdConfig(iface_params.name, conf_params, br_name, iface_params.usesMlo);
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001324 if (conf_file_path.empty()) {
1325 wpa_printf(MSG_ERROR, "Failed to write config file");
1326 return createStatus(HostapdStatusCode::FAILURE_UNKNOWN);
1327 }
1328 std::string add_iface_param_str = StringPrintf(
Les Lee399c6302024-09-12 03:03:38 +00001329 "%s config=%s", iface_params.usesMlo ? br_name.c_str(): iface_params.name.c_str(),
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001330 conf_file_path.c_str());
1331 std::vector<char> add_iface_param_vec(
1332 add_iface_param_str.begin(), add_iface_param_str.end() + 1);
1333 if (hostapd_add_iface(interfaces_, add_iface_param_vec.data()) < 0) {
1334 wpa_printf(
Les Lee399c6302024-09-12 03:03:38 +00001335 MSG_ERROR, "Adding hostapd iface %s failed",
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001336 add_iface_param_str.c_str());
1337 return createStatus(HostapdStatusCode::FAILURE_UNKNOWN);
1338 }
Les Lee399c6302024-09-12 03:03:38 +00001339
1340 // find the iface and set up callback.
1341 struct hostapd_data* iface_hapd = iface_params.usesMlo ?
1342 hostapd_get_iface_by_link_id(interfaces_, (size_t) iface_params.name.c_str()) :
1343 hostapd_get_iface(interfaces_, iface_params.name.c_str());
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001344 WPA_ASSERT(iface_hapd != nullptr && iface_hapd->iface != nullptr);
Les Lee399c6302024-09-12 03:03:38 +00001345 if (iface_params.usesMlo) {
1346 memcmp(iface_hapd->conf->iface, br_name.c_str(), br_name.size());
1347 }
1348
1349 // Callback discrepancy between bridged dual APs and MLO AP
1350 // Note: Only bridged dual APs will have "iface_hapd->conf->bridge" and
1351 // Only MLO AP will have "iface_hapd->mld_link_id"
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001352 // Register the setup complete callbacks
Les Lee399c6302024-09-12 03:03:38 +00001353 // -----------------------------------------
1354 // | bridged dual APs | bridged single link MLO | MLO SAP
1355 // _________________________________________________________________________________________
1356 // hapd->conf->bridge | bridged interface name | bridged interface nam | N/A
1357 // _________________________________________________________________________________________
1358 // hapd->conf->iface | AP interface name | AP interface name | AP interface name
1359 // _________________________________________________________________________________________
1360 // hapd->mld_link_id | 0 (default value) | link id (0) | link id (0 or 1)
1361 // _________________________________________________________________________________________
1362 // hapd->mld_ap | 0 | 1 | 1
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001363 on_setup_complete_internal_callback =
1364 [this](struct hostapd_data* iface_hapd) {
1365 wpa_printf(
1366 MSG_INFO, "AP interface setup completed - state %s",
1367 hostapd_state_text(iface_hapd->iface->state));
1368 if (iface_hapd->iface->state == HAPD_IFACE_DISABLED) {
1369 // Invoke the failure callback on all registered
1370 // clients.
Les Lee399c6302024-09-12 03:03:38 +00001371 std::string instanceName = iface_hapd->conf->iface;
1372#ifdef CONFIG_IEEE80211BE
1373 if (iface_hapd->conf->mld_ap
1374 && strlen(iface_hapd->conf->bridge) == 0) {
1375 instanceName = std::to_string(iface_hapd->mld_link_id);
1376 }
1377#endif
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001378 for (const auto& callback : callbacks_) {
Chenming Huangbaa5e582024-04-02 08:21:10 +05301379 auto status = callback->onFailure(
1380 strlen(iface_hapd->conf->bridge) > 0 ?
Les Leee08c2862021-10-29 16:36:41 +08001381 iface_hapd->conf->bridge : iface_hapd->conf->iface,
Les Lee399c6302024-09-12 03:03:38 +00001382 instanceName);
Chenming Huangbaa5e582024-04-02 08:21:10 +05301383 if (!status.isOk()) {
1384 wpa_printf(MSG_ERROR, "Failed to invoke onFailure");
1385 }
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001386 }
1387 }
1388 };
1389
1390 // Register for new client connect/disconnect indication.
1391 on_sta_authorized_internal_callback =
1392 [this](struct hostapd_data* iface_hapd, const u8 *mac_addr,
1393 int authorized, const u8 *p2p_dev_addr) {
1394 wpa_printf(MSG_DEBUG, "notify client " MACSTR " %s",
1395 MAC2STR(mac_addr),
1396 (authorized) ? "Connected" : "Disconnected");
1397 ClientInfo info;
1398 info.ifaceName = strlen(iface_hapd->conf->bridge) > 0 ?
1399 iface_hapd->conf->bridge : iface_hapd->conf->iface;
Les Lee399c6302024-09-12 03:03:38 +00001400 std::string instanceName = iface_hapd->conf->iface;
1401#ifdef CONFIG_IEEE80211BE
1402 if (iface_hapd->conf->mld_ap
1403 && strlen(iface_hapd->conf->bridge) == 0) {
1404 instanceName = std::to_string(iface_hapd->mld_link_id);
1405 }
1406#endif
1407 info.apIfaceInstance = instanceName;
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001408 info.clientAddress.assign(mac_addr, mac_addr + ETH_ALEN);
1409 info.isConnected = authorized;
Chris Weir808564b2024-08-07 10:12:16 -07001410 if(isAidlServiceVersionAtLeast(3) && !authorized) {
1411 u16 disconnect_reason_code = WLAN_REASON_UNSPECIFIED;
1412 auto sta_ptr_optional = getStaInfoByMacAddr(iface_hapd, mac_addr);
1413 if (sta_ptr_optional.has_value()){
1414 disconnect_reason_code = sta_ptr_optional.value()->deauth_reason;
1415 }
1416 info.disconnectReasonCode =
1417 static_cast<common::DeauthenticationReasonCode>(disconnect_reason_code);
1418 }
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001419 for (const auto &callback : callbacks_) {
Chenming Huangbaa5e582024-04-02 08:21:10 +05301420 auto status = callback->onConnectedClientsChanged(info);
1421 if (!status.isOk()) {
1422 wpa_printf(MSG_ERROR, "Failed to invoke onConnectedClientsChanged");
1423 }
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001424 }
1425 };
1426
1427 // Register for wpa_event which used to get channel switch event
1428 on_wpa_msg_internal_callback =
1429 [this](struct hostapd_data* iface_hapd, int level,
1430 enum wpa_msg_type type, const char *txt,
1431 size_t len) {
1432 wpa_printf(MSG_DEBUG, "Receive wpa msg : %s", txt);
1433 if (os_strncmp(txt, AP_EVENT_ENABLED,
1434 strlen(AP_EVENT_ENABLED)) == 0 ||
1435 os_strncmp(txt, WPA_EVENT_CHANNEL_SWITCH,
1436 strlen(WPA_EVENT_CHANNEL_SWITCH)) == 0) {
Les Lee399c6302024-09-12 03:03:38 +00001437 std::string instanceName = iface_hapd->conf->iface;
1438#ifdef CONFIG_IEEE80211BE
1439 if (iface_hapd->conf->mld_ap && strlen(iface_hapd->conf->bridge) == 0) {
1440 instanceName = std::to_string(iface_hapd->mld_link_id);
1441 }
1442#endif
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001443 ApInfo info;
1444 info.ifaceName = strlen(iface_hapd->conf->bridge) > 0 ?
1445 iface_hapd->conf->bridge : iface_hapd->conf->iface,
Les Lee399c6302024-09-12 03:03:38 +00001446 info.apIfaceInstance = instanceName;
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001447 info.freqMhz = iface_hapd->iface->freq;
Ahmed ElArabawyb4115792022-02-08 09:33:01 -08001448 info.channelBandwidth = getChannelBandwidth(iface_hapd->iconf);
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001449 info.generation = getGeneration(iface_hapd->iface->current_mode);
1450 info.apIfaceInstanceMacAddress.assign(iface_hapd->own_addr,
1451 iface_hapd->own_addr + ETH_ALEN);
1452 for (const auto &callback : callbacks_) {
Chenming Huangbaa5e582024-04-02 08:21:10 +05301453 auto status = callback->onApInstanceInfoChanged(info);
1454 if (!status.isOk()) {
1455 wpa_printf(MSG_ERROR,
1456 "Failed to invoke onApInstanceInfoChanged");
1457 }
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001458 }
Les Leea0c90cb2022-04-19 17:39:23 +08001459 } else if (os_strncmp(txt, AP_EVENT_DISABLED, strlen(AP_EVENT_DISABLED)) == 0
1460 || os_strncmp(txt, INTERFACE_DISABLED, strlen(INTERFACE_DISABLED)) == 0)
1461 {
Les Lee399c6302024-09-12 03:03:38 +00001462 std::string instanceName = iface_hapd->conf->iface;
1463#ifdef CONFIG_IEEE80211BE
1464 if (iface_hapd->conf->mld_ap && strlen(iface_hapd->conf->bridge) == 0) {
1465 instanceName = std::to_string(iface_hapd->mld_link_id);
1466 }
1467#endif
Yu Ouyang378d3c42021-08-20 17:31:08 +08001468 // Invoke the failure callback on all registered clients.
1469 for (const auto& callback : callbacks_) {
Les Lee399c6302024-09-12 03:03:38 +00001470 auto status =
1471 callback->onFailure(strlen(iface_hapd->conf->bridge) > 0 ?
Les Leee08c2862021-10-29 16:36:41 +08001472 iface_hapd->conf->bridge : iface_hapd->conf->iface,
Les Lee399c6302024-09-12 03:03:38 +00001473 instanceName);
Chenming Huangbaa5e582024-04-02 08:21:10 +05301474 if (!status.isOk()) {
1475 wpa_printf(MSG_ERROR, "Failed to invoke onFailure");
1476 }
Yu Ouyang378d3c42021-08-20 17:31:08 +08001477 }
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001478 }
Yu Ouyang378d3c42021-08-20 17:31:08 +08001479 };
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001480
1481 // Setup callback
1482 iface_hapd->setup_complete_cb = onAsyncSetupCompleteCb;
1483 iface_hapd->setup_complete_cb_ctx = iface_hapd;
1484 iface_hapd->sta_authorized_cb = onAsyncStaAuthorizedCb;
1485 iface_hapd->sta_authorized_cb_ctx = iface_hapd;
Hu Wang7c5a4322021-06-24 17:24:59 +08001486 wpa_msg_register_aidl_cb(onAsyncWpaEventCb);
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001487
Les Lee399c6302024-09-12 03:03:38 +00001488 // Multi-link MLO should enable iface after both links have been set.
1489 if (!iface_params.usesMlo && hostapd_enable_iface(iface_hapd->iface) < 0) {
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001490 wpa_printf(
1491 MSG_ERROR, "Enabling interface %s failed",
1492 iface_params.name.c_str());
1493 return createStatus(HostapdStatusCode::FAILURE_UNKNOWN);
1494 }
1495 return ndk::ScopedAStatus::ok();
1496}
1497
1498::ndk::ScopedAStatus Hostapd::removeAccessPointInternal(const std::string& iface_name)
1499{
1500 // interfaces to be removed
1501 std::vector<std::string> interfaces;
1502 bool is_error = false;
1503
1504 const auto it = br_interfaces_.find(iface_name);
1505 if (it != br_interfaces_.end()) {
1506 // In case bridge, remove managed interfaces
1507 interfaces = it->second;
1508 br_interfaces_.erase(iface_name);
1509 } else {
1510 // else remove current interface
1511 interfaces.push_back(iface_name);
1512 }
1513
1514 for (auto& iface : interfaces) {
1515 std::vector<char> remove_iface_param_vec(
1516 iface.begin(), iface.end() + 1);
1517 if (hostapd_remove_iface(interfaces_, remove_iface_param_vec.data()) < 0) {
1518 wpa_printf(MSG_INFO, "Remove interface %s failed", iface.c_str());
1519 is_error = true;
1520 }
1521 }
1522 if (is_error) {
1523 return createStatus(HostapdStatusCode::FAILURE_UNKNOWN);
1524 }
1525 return ndk::ScopedAStatus::ok();
1526}
1527
1528::ndk::ScopedAStatus Hostapd::registerCallbackInternal(
1529 const std::shared_ptr<IHostapdCallback>& callback)
1530{
1531 binder_status_t status = AIBinder_linkToDeath(callback->asBinder().get(),
1532 death_notifier_, this /* cookie */);
1533 if (status != STATUS_OK) {
1534 wpa_printf(
1535 MSG_ERROR,
1536 "Error registering for death notification for "
1537 "hostapd callback object");
1538 return createStatus(HostapdStatusCode::FAILURE_UNKNOWN);
1539 }
1540 callbacks_.push_back(callback);
Manaswini Paluri76ae6982024-02-29 14:49:20 +05301541 if (aidl_service_version == 0) {
1542 aidl_service_version = Hostapd::version;
1543 wpa_printf(MSG_INFO, "AIDL service version: %d", aidl_service_version);
1544 }
1545 if (aidl_client_version == 0) {
1546 callback->getInterfaceVersion(&aidl_client_version);
1547 wpa_printf(MSG_INFO, "AIDL client version: %d", aidl_client_version);
1548 }
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001549 return ndk::ScopedAStatus::ok();
1550}
1551
1552::ndk::ScopedAStatus Hostapd::forceClientDisconnectInternal(const std::string& iface_name,
1553 const std::vector<uint8_t>& client_address, Ieee80211ReasonCode reason_code)
1554{
1555 struct hostapd_data *hapd = hostapd_get_iface(interfaces_, iface_name.c_str());
1556 bool result;
1557 if (!hapd) {
1558 for (auto const& iface : br_interfaces_) {
1559 if (iface.first == iface_name) {
1560 for (auto const& instance : iface.second) {
1561 hapd = hostapd_get_iface(interfaces_, instance.c_str());
1562 if (hapd) {
1563 result = forceStaDisconnection(hapd, client_address,
1564 (uint16_t) reason_code);
1565 if (result) break;
1566 }
1567 }
1568 }
1569 }
1570 } else {
1571 result = forceStaDisconnection(hapd, client_address, (uint16_t) reason_code);
1572 }
1573 if (!hapd) {
1574 wpa_printf(MSG_ERROR, "Interface %s doesn't exist", iface_name.c_str());
1575 return createStatus(HostapdStatusCode::FAILURE_IFACE_UNKNOWN);
1576 }
1577 if (result) {
1578 return ndk::ScopedAStatus::ok();
1579 }
1580 return createStatus(HostapdStatusCode::FAILURE_CLIENT_UNKNOWN);
1581}
1582
1583::ndk::ScopedAStatus Hostapd::setDebugParamsInternal(DebugLevel level)
1584{
1585 wpa_debug_level = static_cast<uint32_t>(level);
1586 return ndk::ScopedAStatus::ok();
1587}
1588
Les Lee8d291ea2024-11-19 22:18:50 +00001589::ndk::ScopedAStatus Hostapd::removeLinkFromMultipleLinkBridgedApIfaceInternal(
1590const std::string& iface_name, const std::string& linkIdentity)
1591{
1592 if (!hostapd_get_iface(interfaces_, iface_name.c_str())) {
1593 wpa_printf(MSG_ERROR, "Interface %s doesn't exist", iface_name.c_str());
1594 return createStatus(HostapdStatusCode::FAILURE_IFACE_UNKNOWN);
1595 }
1596 struct hostapd_data* iface_hapd =
1597 hostapd_get_iface_by_link_id(interfaces_, (size_t) linkIdentity.c_str());
1598 if (iface_hapd) {
1599 if (0 == hostapd_link_remove(iface_hapd, 1)) {
1600 return ndk::ScopedAStatus::ok();
1601 }
1602 }
1603 return createStatus(HostapdStatusCode::FAILURE_ARGS_INVALID);
1604}
1605
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001606} // namespace hostapd
1607} // namespace wifi
1608} // namespace hardware
1609} // namespace android
Les Leee08c2862021-10-29 16:36:41 +08001610} // namespace aidl