blob: d58b1ed3287a8b9d7136ed1665cbf851d29bcaa4 [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 Weir5031a042024-10-21 13:31:20 -070038// don't use hostapd's wpa_printf for unit testing. It won't compile otherwise
39#ifdef ANDROID_HOSTAPD_UNITTEST
40#include <android-base/logging.h>
41constexpr size_t logbuf_size = 8192;
42static ::android::base::LogSeverity wpa_to_android_level(int level)
43{
44 if (level == MSG_ERROR)
45 return ::android::base::ERROR;
46 if (level == MSG_WARNING)
47 return ::android::base::WARNING;
48 if (level == MSG_INFO)
49 return ::android::base::INFO;
50 return ::android::base::DEBUG;
51}
52void wpa_printf(int level, const char *fmt, ...) {
53 va_list ap;
54 va_start(ap, fmt);
55 char buffer[logbuf_size];
56 int res = snprintf(buffer, logbuf_size, fmt, ap);
57 if (res > 0 && res < logbuf_size) {
58 LOG(wpa_to_android_level(level)) << buffer;
59 }
60}
61#endif
62
Gabriel Biren72cf9a52021-06-25 23:29:26 +000063// The AIDL implementation for hostapd creates a hostapd.conf dynamically for
64// each interface. This file can then be used to hook onto the normal config
65// file parsing logic in hostapd code. Helps us to avoid duplication of code
66// in the AIDL interface.
67// TOOD(b/71872409): Add unit tests for this.
68namespace {
69constexpr char kConfFileNameFmt[] = "/data/vendor/wifi/hostapd/hostapd_%s.conf";
70
71using android::base::RemoveFileIfExists;
72using android::base::StringPrintf;
73using android::base::WriteStringToFile;
74using aidl::android::hardware::wifi::hostapd::BandMask;
Ahmed ElArabawyb4115792022-02-08 09:33:01 -080075using aidl::android::hardware::wifi::hostapd::ChannelBandwidth;
Gabriel Biren72cf9a52021-06-25 23:29:26 +000076using aidl::android::hardware::wifi::hostapd::ChannelParams;
77using aidl::android::hardware::wifi::hostapd::EncryptionType;
78using aidl::android::hardware::wifi::hostapd::Generation;
79using aidl::android::hardware::wifi::hostapd::HostapdStatusCode;
80using aidl::android::hardware::wifi::hostapd::IfaceParams;
81using aidl::android::hardware::wifi::hostapd::NetworkParams;
82using aidl::android::hardware::wifi::hostapd::ParamSizeLimits;
83
84int band2Ghz = (int)BandMask::BAND_2_GHZ;
85int band5Ghz = (int)BandMask::BAND_5_GHZ;
86int band6Ghz = (int)BandMask::BAND_6_GHZ;
87int band60Ghz = (int)BandMask::BAND_60_GHZ;
88
Manaswini Paluri76ae6982024-02-29 14:49:20 +053089int32_t aidl_client_version = 0;
90int32_t aidl_service_version = 0;
91
92/**
93 * Check that the AIDL service is running at least the expected version.
94 * Use to avoid the case where the AIDL interface version
95 * is greater than the version implemented by the service.
96 */
97inline int32_t isAidlServiceVersionAtLeast(int32_t expected_version)
98{
99 return expected_version <= aidl_service_version;
100}
101
102inline int32_t isAidlClientVersionAtLeast(int32_t expected_version)
103{
104 return expected_version <= aidl_client_version;
105}
106
Sunil Ravide0e6bf2024-10-14 04:58:16 +0000107inline int32_t areAidlServiceAndClientAtLeastVersion(int32_t expected_version)
108{
109 return isAidlServiceVersionAtLeast(expected_version)
110 && isAidlClientVersionAtLeast(expected_version);
111}
112
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000113#define MAX_PORTS 1024
114bool GetInterfacesInBridge(std::string br_name,
115 std::vector<std::string>* interfaces) {
116 android::base::unique_fd sock(socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0));
117 if (sock.get() < 0) {
118 wpa_printf(MSG_ERROR, "Failed to create sock (%s) in %s",
119 strerror(errno), __FUNCTION__);
120 return false;
121 }
122
123 struct ifreq request;
124 int i, ifindices[MAX_PORTS];
125 char if_name[IFNAMSIZ];
126 unsigned long args[3];
127
128 memset(ifindices, 0, MAX_PORTS * sizeof(int));
129
130 args[0] = BRCTL_GET_PORT_LIST;
131 args[1] = (unsigned long) ifindices;
132 args[2] = MAX_PORTS;
133
134 strlcpy(request.ifr_name, br_name.c_str(), IFNAMSIZ);
135 request.ifr_data = (char *)args;
136
137 if (ioctl(sock.get(), SIOCDEVPRIVATE, &request) < 0) {
138 wpa_printf(MSG_ERROR, "Failed to ioctl SIOCDEVPRIVATE in %s",
139 __FUNCTION__);
140 return false;
141 }
142
143 for (i = 0; i < MAX_PORTS; i ++) {
144 memset(if_name, 0, IFNAMSIZ);
145 if (ifindices[i] == 0 || !if_indextoname(ifindices[i], if_name)) {
146 continue;
147 }
148 interfaces->push_back(if_name);
149 }
150 return true;
151}
152
153std::string WriteHostapdConfig(
Les Lee399c6302024-09-12 03:03:38 +0000154 const std::string& instance_name, const std::string& config,
155 const std::string br_name, const bool usesMlo)
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000156{
Les Lee399c6302024-09-12 03:03:38 +0000157 std::string conf_name_as_string = instance_name;
158 if (usesMlo) {
159 conf_name_as_string = StringPrintf(
160 "%s-%s", br_name.c_str(), instance_name.c_str());
161 }
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000162 const std::string file_path =
Les Lee399c6302024-09-12 03:03:38 +0000163 StringPrintf(kConfFileNameFmt, conf_name_as_string.c_str());
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000164 if (WriteStringToFile(
165 config, file_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
166 getuid(), getgid())) {
167 return file_path;
168 }
169 // Diagnose failure
170 int error = errno;
171 wpa_printf(
172 MSG_ERROR, "Cannot write hostapd config to %s, error: %s",
173 file_path.c_str(), strerror(error));
174 struct stat st;
175 int result = stat(file_path.c_str(), &st);
176 if (result == 0) {
177 wpa_printf(
178 MSG_ERROR, "hostapd config file uid: %d, gid: %d, mode: %d",
179 st.st_uid, st.st_gid, st.st_mode);
180 } else {
181 wpa_printf(
182 MSG_ERROR,
183 "Error calling stat() on hostapd config file: %s",
184 strerror(errno));
185 }
186 return "";
187}
188
189/*
190 * Get the op_class for a channel/band
191 * The logic here is based on Table E-4 in the 802.11 Specification
192 */
193int getOpClassForChannel(int channel, int band, bool support11n, bool support11ac) {
194 // 2GHz Band
195 if ((band & band2Ghz) != 0) {
196 if (channel == 14) {
197 return 82;
198 }
199 if (channel >= 1 && channel <= 13) {
200 if (!support11n) {
201 //20MHz channel
202 return 81;
203 }
204 if (channel <= 9) {
205 // HT40 with secondary channel above primary
206 return 83;
207 }
208 // HT40 with secondary channel below primary
209 return 84;
210 }
211 // Error
212 return 0;
213 }
214
215 // 5GHz Band
216 if ((band & band5Ghz) != 0) {
217 if (support11ac) {
218 switch (channel) {
219 case 42:
220 case 58:
221 case 106:
222 case 122:
223 case 138:
224 case 155:
225 // 80MHz channel
226 return 128;
227 case 50:
228 case 114:
229 // 160MHz channel
230 return 129;
231 }
232 }
233
234 if (!support11n) {
235 if (channel >= 36 && channel <= 48) {
236 return 115;
237 }
238 if (channel >= 52 && channel <= 64) {
239 return 118;
240 }
241 if (channel >= 100 && channel <= 144) {
242 return 121;
243 }
244 if (channel >= 149 && channel <= 161) {
245 return 124;
246 }
247 if (channel >= 165 && channel <= 169) {
248 return 125;
249 }
250 } else {
251 switch (channel) {
252 case 36:
253 case 44:
254 // HT40 with secondary channel above primary
255 return 116;
256 case 40:
257 case 48:
258 // HT40 with secondary channel below primary
259 return 117;
260 case 52:
261 case 60:
262 // HT40 with secondary channel above primary
263 return 119;
264 case 56:
265 case 64:
266 // HT40 with secondary channel below primary
267 return 120;
268 case 100:
269 case 108:
270 case 116:
271 case 124:
272 case 132:
273 case 140:
274 // HT40 with secondary channel above primary
275 return 122;
276 case 104:
277 case 112:
278 case 120:
279 case 128:
280 case 136:
281 case 144:
282 // HT40 with secondary channel below primary
283 return 123;
284 case 149:
285 case 157:
286 // HT40 with secondary channel above primary
287 return 126;
288 case 153:
289 case 161:
290 // HT40 with secondary channel below primary
291 return 127;
292 }
293 }
294 // Error
295 return 0;
296 }
297
298 // 6GHz Band
299 if ((band & band6Ghz) != 0) {
300 // Channels 1, 5. 9, 13, ...
301 if ((channel & 0x03) == 0x01) {
302 // 20MHz channel
303 return 131;
304 }
305 // Channels 3, 11, 19, 27, ...
306 if ((channel & 0x07) == 0x03) {
307 // 40MHz channel
308 return 132;
309 }
310 // Channels 7, 23, 39, 55, ...
311 if ((channel & 0x0F) == 0x07) {
312 // 80MHz channel
313 return 133;
314 }
315 // Channels 15, 47, 69, ...
316 if ((channel & 0x1F) == 0x0F) {
317 // 160MHz channel
318 return 134;
319 }
320 if (channel == 2) {
321 // 20MHz channel
322 return 136;
323 }
324 // Error
325 return 0;
326 }
327
328 if ((band & band60Ghz) != 0) {
329 if (1 <= channel && channel <= 8) {
330 return 180;
331 } else if (9 <= channel && channel <= 15) {
332 return 181;
333 } else if (17 <= channel && channel <= 22) {
334 return 182;
335 } else if (25 <= channel && channel <= 29) {
336 return 183;
337 }
338 // Error
339 return 0;
340 }
341
342 return 0;
343}
344
345bool validatePassphrase(int passphrase_len, int min_len, int max_len)
346{
347 if (min_len != -1 && passphrase_len < min_len) return false;
348 if (max_len != -1 && passphrase_len > max_len) return false;
349 return true;
350}
351
Manaswini Paluri76ae6982024-02-29 14:49:20 +0530352std::string getInterfaceMacAddress(const std::string& if_name)
353{
354 u8 addr[ETH_ALEN] = {};
355 struct ifreq ifr;
356 std::string mac_addr;
357
358 android::base::unique_fd sock(socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0));
359 if (sock.get() < 0) {
360 wpa_printf(MSG_ERROR, "Failed to create sock (%s) in %s",
361 strerror(errno), __FUNCTION__);
362 return "";
363 }
364
365 memset(&ifr, 0, sizeof(ifr));
366 strlcpy(ifr.ifr_name, if_name.c_str(), IFNAMSIZ);
367 if (ioctl(sock.get(), SIOCGIFHWADDR, &ifr) < 0) {
368 wpa_printf(MSG_ERROR, "Could not get interface %s hwaddr: %s",
369 if_name.c_str(), strerror(errno));
370 return "";
371 }
372
373 memcpy(addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
374 mac_addr = StringPrintf("" MACSTR, MAC2STR(addr));
375
376 return mac_addr;
377}
378
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000379std::string CreateHostapdConfig(
380 const IfaceParams& iface_params,
381 const ChannelParams& channelParams,
382 const NetworkParams& nw_params,
Purushottam Kushwaha0316c882021-12-20 15:07:44 +0530383 const std::string br_name,
384 const std::string owe_transition_ifname)
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000385{
386 if (nw_params.ssid.size() >
387 static_cast<uint32_t>(
388 ParamSizeLimits::SSID_MAX_LEN_IN_BYTES)) {
389 wpa_printf(
390 MSG_ERROR, "Invalid SSID size: %zu", nw_params.ssid.size());
391 return "";
392 }
393
394 // SSID string
395 std::stringstream ss;
396 ss << std::hex;
397 ss << std::setfill('0');
398 for (uint8_t b : nw_params.ssid) {
399 ss << std::setw(2) << static_cast<unsigned int>(b);
400 }
401 const std::string ssid_as_string = ss.str();
402
403 // Encryption config string
404 uint32_t band = 0;
405 band |= static_cast<uint32_t>(channelParams.bandMask);
Sunil Ravi4fc918f2022-04-17 09:26:48 -0700406 bool is_2Ghz_band_only = band == static_cast<uint32_t>(band2Ghz);
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000407 bool is_6Ghz_band_only = band == static_cast<uint32_t>(band6Ghz);
408 bool is_60Ghz_band_only = band == static_cast<uint32_t>(band60Ghz);
409 std::string encryption_config_as_string;
410 switch (nw_params.encryptionType) {
411 case EncryptionType::NONE:
412 // no security params
413 break;
414 case EncryptionType::WPA:
415 if (!validatePassphrase(
416 nw_params.passphrase.size(),
417 static_cast<uint32_t>(ParamSizeLimits::
418 WPA2_PSK_PASSPHRASE_MIN_LEN_IN_BYTES),
419 static_cast<uint32_t>(ParamSizeLimits::
420 WPA2_PSK_PASSPHRASE_MAX_LEN_IN_BYTES))) {
421 return "";
422 }
423 encryption_config_as_string = StringPrintf(
424 "wpa=3\n"
425 "wpa_pairwise=%s\n"
426 "wpa_passphrase=%s",
427 is_60Ghz_band_only ? "GCMP" : "TKIP CCMP",
428 nw_params.passphrase.c_str());
429 break;
430 case EncryptionType::WPA2:
431 if (!validatePassphrase(
432 nw_params.passphrase.size(),
433 static_cast<uint32_t>(ParamSizeLimits::
434 WPA2_PSK_PASSPHRASE_MIN_LEN_IN_BYTES),
435 static_cast<uint32_t>(ParamSizeLimits::
436 WPA2_PSK_PASSPHRASE_MAX_LEN_IN_BYTES))) {
437 return "";
438 }
439 encryption_config_as_string = StringPrintf(
440 "wpa=2\n"
441 "rsn_pairwise=%s\n"
Sunil Ravib3580db2022-01-28 12:25:46 -0800442#ifdef ENABLE_HOSTAPD_CONFIG_80211W_MFP_OPTIONAL
443 "ieee80211w=1\n"
444#endif
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000445 "wpa_passphrase=%s",
446 is_60Ghz_band_only ? "GCMP" : "CCMP",
447 nw_params.passphrase.c_str());
448 break;
449 case EncryptionType::WPA3_SAE_TRANSITION:
450 if (!validatePassphrase(
451 nw_params.passphrase.size(),
452 static_cast<uint32_t>(ParamSizeLimits::
453 WPA2_PSK_PASSPHRASE_MIN_LEN_IN_BYTES),
454 static_cast<uint32_t>(ParamSizeLimits::
455 WPA2_PSK_PASSPHRASE_MAX_LEN_IN_BYTES))) {
456 return "";
457 }
Sunil Ravid917c832023-07-07 17:30:33 +0000458 // WPA3 transition mode or SAE+WPA_PSK key management(AKM) is not allowed in 6GHz.
459 // Auto-convert any such configurations to SAE.
460 if ((band & band6Ghz) != 0) {
461 wpa_printf(MSG_INFO, "WPA3_SAE_TRANSITION configured in 6GHz band."
462 "Enable only SAE in key_mgmt");
463 encryption_config_as_string = StringPrintf(
464 "wpa=2\n"
465 "rsn_pairwise=CCMP\n"
466 "wpa_key_mgmt=%s\n"
467 "ieee80211w=2\n"
468 "sae_require_mfp=2\n"
469 "sae_pwe=%d\n"
470 "sae_password=%s",
Sunil Ravic1edd3e2023-02-06 18:52:51 +0000471#ifdef CONFIG_IEEE80211BE
Sunil Ravid917c832023-07-07 17:30:33 +0000472 iface_params.hwModeParams.enable80211BE ?
473 "SAE SAE-EXT-KEY" : "SAE",
Sunil Ravic1edd3e2023-02-06 18:52:51 +0000474#else
Sunil Ravid917c832023-07-07 17:30:33 +0000475 "SAE",
Sunil Ravic1edd3e2023-02-06 18:52:51 +0000476#endif
Sunil Ravid917c832023-07-07 17:30:33 +0000477 is_6Ghz_band_only ? 1 : 2,
478 nw_params.passphrase.c_str());
479 } else {
480 encryption_config_as_string = StringPrintf(
481 "wpa=2\n"
482 "rsn_pairwise=%s\n"
483 "wpa_key_mgmt=%s\n"
484 "ieee80211w=1\n"
485 "sae_require_mfp=1\n"
486 "wpa_passphrase=%s\n"
487 "sae_password=%s",
488 is_60Ghz_band_only ? "GCMP" : "CCMP",
489#ifdef CONFIG_IEEE80211BE
490 iface_params.hwModeParams.enable80211BE ?
491 "WPA-PSK SAE SAE-EXT-KEY" : "WPA-PSK SAE",
492#else
493 "WPA-PSK SAE",
494#endif
495 nw_params.passphrase.c_str(),
496 nw_params.passphrase.c_str());
497 }
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000498 break;
499 case EncryptionType::WPA3_SAE:
500 if (!validatePassphrase(nw_params.passphrase.size(), 1, -1)) {
501 return "";
502 }
503 encryption_config_as_string = StringPrintf(
504 "wpa=2\n"
505 "rsn_pairwise=%s\n"
Sunil Ravi65251732023-01-24 05:03:35 +0000506 "wpa_key_mgmt=%s\n"
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000507 "ieee80211w=2\n"
508 "sae_require_mfp=2\n"
509 "sae_pwe=%d\n"
510 "sae_password=%s",
511 is_60Ghz_band_only ? "GCMP" : "CCMP",
Sunil Ravi65251732023-01-24 05:03:35 +0000512#ifdef CONFIG_IEEE80211BE
513 iface_params.hwModeParams.enable80211BE ? "SAE SAE-EXT-KEY" : "SAE",
514#else
515 "SAE",
516#endif
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000517 is_6Ghz_band_only ? 1 : 2,
518 nw_params.passphrase.c_str());
519 break;
Ahmed ElArabawy1aaf1802022-02-04 15:58:55 -0800520 case EncryptionType::WPA3_OWE_TRANSITION:
521 encryption_config_as_string = StringPrintf(
522 "wpa=2\n"
523 "rsn_pairwise=%s\n"
524 "wpa_key_mgmt=OWE\n"
525 "ieee80211w=2",
526 is_60Ghz_band_only ? "GCMP" : "CCMP");
527 break;
528 case EncryptionType::WPA3_OWE:
Purushottam Kushwaha0316c882021-12-20 15:07:44 +0530529 encryption_config_as_string = StringPrintf(
530 "wpa=2\n"
531 "rsn_pairwise=%s\n"
532 "wpa_key_mgmt=OWE\n"
533 "ieee80211w=2",
534 is_60Ghz_band_only ? "GCMP" : "CCMP");
535 break;
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000536 default:
537 wpa_printf(MSG_ERROR, "Unknown encryption type");
538 return "";
539 }
540
541 std::string channel_config_as_string;
542 bool isFirst = true;
543 if (channelParams.enableAcs) {
544 std::string freqList_as_string;
545 for (const auto &range :
546 channelParams.acsChannelFreqRangesMhz) {
547 if (!isFirst) {
548 freqList_as_string += ",";
549 }
550 isFirst = false;
551
552 if (range.startMhz != range.endMhz) {
553 freqList_as_string +=
554 StringPrintf("%d-%d", range.startMhz, range.endMhz);
555 } else {
556 freqList_as_string += StringPrintf("%d", range.startMhz);
557 }
558 }
559 channel_config_as_string = StringPrintf(
560 "channel=0\n"
561 "acs_exclude_dfs=%d\n"
562 "freqlist=%s",
563 channelParams.acsShouldExcludeDfs,
564 freqList_as_string.c_str());
565 } else {
566 int op_class = getOpClassForChannel(
567 channelParams.channel,
568 band,
569 iface_params.hwModeParams.enable80211N,
570 iface_params.hwModeParams.enable80211AC);
571 channel_config_as_string = StringPrintf(
572 "channel=%d\n"
573 "op_class=%d",
574 channelParams.channel, op_class);
575 }
576
577 std::string hw_mode_as_string;
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000578 std::string enable_edmg_as_string;
579 std::string edmg_channel_as_string;
580 bool is_60Ghz_used = false;
581
582 if (((band & band60Ghz) != 0)) {
583 hw_mode_as_string = "hw_mode=ad";
584 if (iface_params.hwModeParams.enableEdmg) {
585 enable_edmg_as_string = "enable_edmg=1";
586 edmg_channel_as_string = StringPrintf(
587 "edmg_channel=%d",
588 channelParams.channel);
589 }
590 is_60Ghz_used = true;
591 } else if ((band & band2Ghz) != 0) {
592 if (((band & band5Ghz) != 0)
593 || ((band & band6Ghz) != 0)) {
594 hw_mode_as_string = "hw_mode=any";
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000595 } else {
596 hw_mode_as_string = "hw_mode=g";
597 }
598 } else if (((band & band5Ghz) != 0)
599 || ((band & band6Ghz) != 0)) {
600 hw_mode_as_string = "hw_mode=a";
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000601 } else {
602 wpa_printf(MSG_ERROR, "Invalid band");
603 return "";
604 }
605
606 std::string he_params_as_string;
607#ifdef CONFIG_IEEE80211AX
608 if (iface_params.hwModeParams.enable80211AX && !is_60Ghz_used) {
609 he_params_as_string = StringPrintf(
610 "ieee80211ax=1\n"
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000611 "he_su_beamformer=%d\n"
612 "he_su_beamformee=%d\n"
613 "he_mu_beamformer=%d\n"
614 "he_twt_required=%d\n",
615 iface_params.hwModeParams.enableHeSingleUserBeamformer ? 1 : 0,
616 iface_params.hwModeParams.enableHeSingleUserBeamformee ? 1 : 0,
617 iface_params.hwModeParams.enableHeMultiUserBeamformer ? 1 : 0,
618 iface_params.hwModeParams.enableHeTargetWakeTime ? 1 : 0);
619 } else {
620 he_params_as_string = "ieee80211ax=0";
621 }
622#endif /* CONFIG_IEEE80211AX */
Sunil Ravi65251732023-01-24 05:03:35 +0000623 std::string eht_params_as_string;
624#ifdef CONFIG_IEEE80211BE
625 if (iface_params.hwModeParams.enable80211BE && !is_60Ghz_used) {
Manaswini Paluri76ae6982024-02-29 14:49:20 +0530626 eht_params_as_string = "ieee80211be=1\n";
Sunil Ravide0e6bf2024-10-14 04:58:16 +0000627 if (areAidlServiceAndClientAtLeastVersion(2)) {
Les Lee399c6302024-09-12 03:03:38 +0000628 std::string interface_mac_addr = getInterfaceMacAddress(
629 iface_params.usesMlo ? br_name : iface_params.name);
Manaswini Paluri76ae6982024-02-29 14:49:20 +0530630 if (interface_mac_addr.empty()) {
631 wpa_printf(MSG_ERROR,
632 "Unable to set interface mac address as bssid for 11BE SAP");
633 return "";
634 }
Les Lee399c6302024-09-12 03:03:38 +0000635 if (iface_params.usesMlo) {
636 eht_params_as_string += StringPrintf(
637 "mld_addr=%s\n"
638 "mld_ap=1",
639 interface_mac_addr.c_str());
640 } else {
641 eht_params_as_string += StringPrintf(
642 "bssid=%s\n"
643 "mld_ap=1",
644 interface_mac_addr.c_str());
645 }
Manaswini Paluri76ae6982024-02-29 14:49:20 +0530646 }
Sunil Ravi65251732023-01-24 05:03:35 +0000647 /* TODO set eht_su_beamformer, eht_su_beamformee, eht_mu_beamformer */
648 } else {
649 eht_params_as_string = "ieee80211be=0";
650 }
651#endif /* CONFIG_IEEE80211BE */
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000652
Xin Dengfec682f2024-02-06 22:59:39 -0800653 std::string ht_cap_vht_oper_he_oper_eht_oper_chwidth_as_string;
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530654 switch (iface_params.hwModeParams.maximumChannelBandwidth) {
655 case ChannelBandwidth::BANDWIDTH_20:
Xin Dengfec682f2024-02-06 22:59:39 -0800656 ht_cap_vht_oper_he_oper_eht_oper_chwidth_as_string = StringPrintf(
657#ifdef CONFIG_IEEE80211BE
658 "eht_oper_chwidth=0\n"
659#endif /* CONFIG_IEEE80211BE */
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530660#ifdef CONFIG_IEEE80211AX
661 "he_oper_chwidth=0\n"
662#endif
Kiran Kumar Lokereb6445122024-02-06 20:06:27 -0800663 "vht_oper_chwidth=0\n"
664 "%s", (band & band6Ghz) ? "op_class=131" : "");
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530665 break;
666 case ChannelBandwidth::BANDWIDTH_40:
Xin Dengfec682f2024-02-06 22:59:39 -0800667 ht_cap_vht_oper_he_oper_eht_oper_chwidth_as_string = StringPrintf(
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530668 "ht_capab=[HT40+]\n"
Xin Dengfec682f2024-02-06 22:59:39 -0800669#ifdef CONFIG_IEEE80211BE
670 "eht_oper_chwidth=0\n"
671#endif /* CONFIG_IEEE80211BE */
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530672#ifdef CONFIG_IEEE80211AX
673 "he_oper_chwidth=0\n"
674#endif
Kiran Kumar Lokereb6445122024-02-06 20:06:27 -0800675 "vht_oper_chwidth=0\n"
676 "%s", (band & band6Ghz) ? "op_class=132" : "");
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530677 break;
678 case ChannelBandwidth::BANDWIDTH_80:
Xin Dengfec682f2024-02-06 22:59:39 -0800679 ht_cap_vht_oper_he_oper_eht_oper_chwidth_as_string = StringPrintf(
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530680 "ht_capab=[HT40+]\n"
Xin Dengfec682f2024-02-06 22:59:39 -0800681#ifdef CONFIG_IEEE80211BE
682 "eht_oper_chwidth=%d\n"
683#endif /* CONFIG_IEEE80211BE */
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530684#ifdef CONFIG_IEEE80211AX
685 "he_oper_chwidth=%d\n"
686#endif
Kiran Kumar Lokereb6445122024-02-06 20:06:27 -0800687 "vht_oper_chwidth=%d\n"
688 "%s",
Xin Dengfec682f2024-02-06 22:59:39 -0800689#ifdef CONFIG_IEEE80211BE
690 (iface_params.hwModeParams.enable80211BE && !is_60Ghz_used) ? 1 : 0,
691#endif
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530692#ifdef CONFIG_IEEE80211AX
693 (iface_params.hwModeParams.enable80211AX && !is_60Ghz_used) ? 1 : 0,
694#endif
Kiran Kumar Lokereb6445122024-02-06 20:06:27 -0800695 iface_params.hwModeParams.enable80211AC ? 1 : 0,
696 (band & band6Ghz) ? "op_class=133" : "");
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530697 break;
698 case ChannelBandwidth::BANDWIDTH_160:
Xin Dengfec682f2024-02-06 22:59:39 -0800699 ht_cap_vht_oper_he_oper_eht_oper_chwidth_as_string = StringPrintf(
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530700 "ht_capab=[HT40+]\n"
Xin Dengfec682f2024-02-06 22:59:39 -0800701#ifdef CONFIG_IEEE80211BE
702 "eht_oper_chwidth=%d\n"
703#endif /* CONFIG_IEEE80211BE */
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530704#ifdef CONFIG_IEEE80211AX
705 "he_oper_chwidth=%d\n"
706#endif
Kiran Kumar Lokereb6445122024-02-06 20:06:27 -0800707 "vht_oper_chwidth=%d\n"
708 "%s",
Xin Dengfec682f2024-02-06 22:59:39 -0800709#ifdef CONFIG_IEEE80211BE
710 (iface_params.hwModeParams.enable80211BE && !is_60Ghz_used) ? 2 : 0,
711#endif
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530712#ifdef CONFIG_IEEE80211AX
713 (iface_params.hwModeParams.enable80211AX && !is_60Ghz_used) ? 2 : 0,
714#endif
Kiran Kumar Lokereb6445122024-02-06 20:06:27 -0800715 iface_params.hwModeParams.enable80211AC ? 2 : 0,
716 (band & band6Ghz) ? "op_class=134" : "");
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530717 break;
718 default:
Sunil Raviffa5cce2022-08-22 23:37:16 +0000719 if (!is_2Ghz_band_only && !is_60Ghz_used) {
720 if (iface_params.hwModeParams.enable80211AC) {
Xin Dengfec682f2024-02-06 22:59:39 -0800721 ht_cap_vht_oper_he_oper_eht_oper_chwidth_as_string =
Sunil Ravi4fc918f2022-04-17 09:26:48 -0700722 "ht_capab=[HT40+]\n"
723 "vht_oper_chwidth=1\n";
Sunil Raviffa5cce2022-08-22 23:37:16 +0000724 }
Kiran Kumar Lokereb6445122024-02-06 20:06:27 -0800725 if (band & band6Ghz) {
Xin Deng3ee3fe12024-02-20 23:24:37 -0800726#ifdef CONFIG_IEEE80211BE
727 if (iface_params.hwModeParams.enable80211BE)
728 ht_cap_vht_oper_he_oper_eht_oper_chwidth_as_string += "op_class=137\n";
729 else
730 ht_cap_vht_oper_he_oper_eht_oper_chwidth_as_string += "op_class=134\n";
731#else /* CONFIG_IEEE80211BE */
Xin Dengfec682f2024-02-06 22:59:39 -0800732 ht_cap_vht_oper_he_oper_eht_oper_chwidth_as_string += "op_class=134\n";
Xin Deng3ee3fe12024-02-20 23:24:37 -0800733#endif /* CONFIG_IEEE80211BE */
Kiran Kumar Lokereb6445122024-02-06 20:06:27 -0800734 }
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530735#ifdef CONFIG_IEEE80211AX
Sunil Raviffa5cce2022-08-22 23:37:16 +0000736 if (iface_params.hwModeParams.enable80211AX) {
Xin Dengfec682f2024-02-06 22:59:39 -0800737 ht_cap_vht_oper_he_oper_eht_oper_chwidth_as_string += "he_oper_chwidth=1\n";
738 }
739#endif
740#ifdef CONFIG_IEEE80211BE
741 if (iface_params.hwModeParams.enable80211BE) {
742 ht_cap_vht_oper_he_oper_eht_oper_chwidth_as_string += "eht_oper_chwidth=1";
Sunil Raviffa5cce2022-08-22 23:37:16 +0000743 }
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530744#endif
Sunil Raviffa5cce2022-08-22 23:37:16 +0000745 }
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530746 break;
747 }
748
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000749#ifdef CONFIG_INTERWORKING
750 std::string access_network_params_as_string;
751 if (nw_params.isMetered) {
752 access_network_params_as_string = StringPrintf(
753 "interworking=1\n"
754 "access_network_type=2\n"); // CHARGEABLE_PUBLIC_NETWORK
755 } else {
756 access_network_params_as_string = StringPrintf(
757 "interworking=0\n");
758 }
759#endif /* CONFIG_INTERWORKING */
760
761 std::string bridge_as_string;
Les Lee399c6302024-09-12 03:03:38 +0000762 if (!br_name.empty() && !iface_params.usesMlo) {
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000763 bridge_as_string = StringPrintf("bridge=%s", br_name.c_str());
764 }
765
Serik Beketayev8af7a722021-12-23 12:25:36 -0800766 // vendor_elements string
767 std::string vendor_elements_as_string;
768 if (nw_params.vendorElements.size() > 0) {
769 std::stringstream ss;
770 ss << std::hex;
771 ss << std::setfill('0');
772 for (uint8_t b : nw_params.vendorElements) {
773 ss << std::setw(2) << static_cast<unsigned int>(b);
774 }
775 vendor_elements_as_string = StringPrintf("vendor_elements=%s", ss.str().c_str());
776 }
777
Purushottam Kushwaha0316c882021-12-20 15:07:44 +0530778 std::string owe_transition_ifname_as_string;
779 if (!owe_transition_ifname.empty()) {
780 owe_transition_ifname_as_string = StringPrintf(
781 "owe_transition_ifname=%s", owe_transition_ifname.c_str());
782 }
783
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000784 return StringPrintf(
785 "interface=%s\n"
786 "driver=nl80211\n"
Les Lee399c6302024-09-12 03:03:38 +0000787 "ctrl_interface=/data/vendor/wifi/hostapd/ctrl_%s\n"
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000788 // ssid2 signals to hostapd that the value is not a literal value
789 // for use as a SSID. In this case, we're giving it a hex
790 // std::string and hostapd needs to expect that.
791 "ssid2=%s\n"
792 "%s\n"
793 "ieee80211n=%d\n"
794 "ieee80211ac=%d\n"
795 "%s\n"
796 "%s\n"
797 "%s\n"
Sunil Ravi65251732023-01-24 05:03:35 +0000798 "%s\n"
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000799 "ignore_broadcast_ssid=%d\n"
800 "wowlan_triggers=any\n"
801#ifdef CONFIG_INTERWORKING
802 "%s\n"
803#endif /* CONFIG_INTERWORKING */
804 "%s\n"
805 "%s\n"
806 "%s\n"
Serik Beketayev8af7a722021-12-23 12:25:36 -0800807 "%s\n"
Purushottam Kushwaha0316c882021-12-20 15:07:44 +0530808 "%s\n"
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000809 "%s\n",
Les Lee399c6302024-09-12 03:03:38 +0000810 iface_params.usesMlo ? br_name.c_str() : iface_params.name.c_str(),
811 iface_params.name.c_str(),
812 ssid_as_string.c_str(),
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000813 channel_config_as_string.c_str(),
814 iface_params.hwModeParams.enable80211N ? 1 : 0,
815 iface_params.hwModeParams.enable80211AC ? 1 : 0,
816 he_params_as_string.c_str(),
Sunil Ravi65251732023-01-24 05:03:35 +0000817 eht_params_as_string.c_str(),
Xin Dengfec682f2024-02-06 22:59:39 -0800818 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 +0000819 nw_params.isHidden ? 1 : 0,
820#ifdef CONFIG_INTERWORKING
821 access_network_params_as_string.c_str(),
822#endif /* CONFIG_INTERWORKING */
823 encryption_config_as_string.c_str(),
824 bridge_as_string.c_str(),
Purushottam Kushwaha0316c882021-12-20 15:07:44 +0530825 owe_transition_ifname_as_string.c_str(),
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000826 enable_edmg_as_string.c_str(),
Serik Beketayev8af7a722021-12-23 12:25:36 -0800827 edmg_channel_as_string.c_str(),
828 vendor_elements_as_string.c_str());
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000829}
830
831Generation getGeneration(hostapd_hw_modes *current_mode)
832{
833 wpa_printf(MSG_DEBUG, "getGeneration hwmode=%d, ht_enabled=%d,"
834 " vht_enabled=%d, he_supported=%d",
835 current_mode->mode, current_mode->ht_capab != 0,
836 current_mode->vht_capab != 0, current_mode->he_capab->he_supported);
837 switch (current_mode->mode) {
838 case HOSTAPD_MODE_IEEE80211B:
839 return Generation::WIFI_STANDARD_LEGACY;
840 case HOSTAPD_MODE_IEEE80211G:
841 return current_mode->ht_capab == 0 ?
842 Generation::WIFI_STANDARD_LEGACY : Generation::WIFI_STANDARD_11N;
843 case HOSTAPD_MODE_IEEE80211A:
844 if (current_mode->he_capab->he_supported) {
845 return Generation::WIFI_STANDARD_11AX;
846 }
847 return current_mode->vht_capab == 0 ?
848 Generation::WIFI_STANDARD_11N : Generation::WIFI_STANDARD_11AC;
849 case HOSTAPD_MODE_IEEE80211AD:
850 return Generation::WIFI_STANDARD_11AD;
851 default:
852 return Generation::WIFI_STANDARD_UNKNOWN;
853 }
854}
855
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800856ChannelBandwidth getChannelBandwidth(struct hostapd_config *iconf)
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000857{
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800858 wpa_printf(MSG_DEBUG, "getChannelBandwidth %d, isHT=%d, isHT40=%d",
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000859 iconf->vht_oper_chwidth, iconf->ieee80211n,
860 iconf->secondary_channel);
861 switch (iconf->vht_oper_chwidth) {
Sunil8cd6f4d2022-06-28 18:40:46 +0000862 case CONF_OPER_CHWIDTH_80MHZ:
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800863 return ChannelBandwidth::BANDWIDTH_80;
Sunil8cd6f4d2022-06-28 18:40:46 +0000864 case CONF_OPER_CHWIDTH_80P80MHZ:
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800865 return ChannelBandwidth::BANDWIDTH_80P80;
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000866 break;
Sunil8cd6f4d2022-06-28 18:40:46 +0000867 case CONF_OPER_CHWIDTH_160MHZ:
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800868 return ChannelBandwidth::BANDWIDTH_160;
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000869 break;
Sunil8cd6f4d2022-06-28 18:40:46 +0000870 case CONF_OPER_CHWIDTH_USE_HT:
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000871 if (iconf->ieee80211n) {
872 return iconf->secondary_channel != 0 ?
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800873 ChannelBandwidth::BANDWIDTH_40 : ChannelBandwidth::BANDWIDTH_20;
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000874 }
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800875 return ChannelBandwidth::BANDWIDTH_20_NOHT;
Sunil8cd6f4d2022-06-28 18:40:46 +0000876 case CONF_OPER_CHWIDTH_2160MHZ:
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800877 return ChannelBandwidth::BANDWIDTH_2160;
Sunil8cd6f4d2022-06-28 18:40:46 +0000878 case CONF_OPER_CHWIDTH_4320MHZ:
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800879 return ChannelBandwidth::BANDWIDTH_4320;
Sunil8cd6f4d2022-06-28 18:40:46 +0000880 case CONF_OPER_CHWIDTH_6480MHZ:
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800881 return ChannelBandwidth::BANDWIDTH_6480;
Sunil8cd6f4d2022-06-28 18:40:46 +0000882 case CONF_OPER_CHWIDTH_8640MHZ:
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800883 return ChannelBandwidth::BANDWIDTH_8640;
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000884 default:
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800885 return ChannelBandwidth::BANDWIDTH_INVALID;
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000886 }
887}
888
Chris Weir808564b2024-08-07 10:12:16 -0700889std::optional<struct sta_info*> getStaInfoByMacAddr(const struct hostapd_data* iface_hapd,
890 const u8 *mac_addr) {
891 if (iface_hapd == nullptr || mac_addr == nullptr){
892 wpa_printf(MSG_ERROR, "nullptr passsed to getStaInfoByMacAddr!");
893 return std::nullopt;
894 }
895
896 for (struct sta_info* sta_ptr = iface_hapd->sta_list; sta_ptr; sta_ptr = sta_ptr->next) {
897 int res;
898 res = memcmp(sta_ptr->addr, mac_addr, ETH_ALEN);
899 if (res == 0) {
900 return sta_ptr;
901 }
902 }
903 return std::nullopt;
904}
905
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000906bool forceStaDisconnection(struct hostapd_data* hapd,
907 const std::vector<uint8_t>& client_address,
908 const uint16_t reason_code) {
Sunil Ravi1a360892022-11-29 20:16:01 +0000909 if (client_address.size() != ETH_ALEN) {
910 return false;
911 }
Chris Weir808564b2024-08-07 10:12:16 -0700912
913 auto sta_ptr_optional = getStaInfoByMacAddr(hapd, client_address.data());
914 if (sta_ptr_optional.has_value()) {
915 wpa_printf(MSG_INFO, "Force client:" MACSTR " disconnect with reason: %d",
916 MAC2STR(client_address.data()), reason_code);
917 ap_sta_disconnect(hapd, sta_ptr_optional.value(), sta_ptr_optional.value()->addr,
918 reason_code);
919 return true;
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000920 }
Chris Weir808564b2024-08-07 10:12:16 -0700921
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000922 return false;
923}
924
925// hostapd core functions accept "C" style function pointers, so use global
926// functions to pass to the hostapd core function and store the corresponding
927// std::function methods to be invoked.
928//
929// NOTE: Using the pattern from the vendor HAL (wifi_legacy_hal.cpp).
930//
931// Callback to be invoked once setup is complete
932std::function<void(struct hostapd_data*)> on_setup_complete_internal_callback;
933void onAsyncSetupCompleteCb(void* ctx)
934{
935 struct hostapd_data* iface_hapd = (struct hostapd_data*)ctx;
936 if (on_setup_complete_internal_callback) {
937 on_setup_complete_internal_callback(iface_hapd);
938 // Invalidate this callback since we don't want this firing
939 // again in single AP mode.
940 if (strlen(iface_hapd->conf->bridge) > 0) {
941 on_setup_complete_internal_callback = nullptr;
942 }
943 }
944}
945
946// Callback to be invoked on hotspot client connection/disconnection
947std::function<void(struct hostapd_data*, const u8 *mac_addr, int authorized,
948 const u8 *p2p_dev_addr)> on_sta_authorized_internal_callback;
949void onAsyncStaAuthorizedCb(void* ctx, const u8 *mac_addr, int authorized,
Sunil Ravid8128a22023-11-06 23:53:58 +0000950 const u8 *p2p_dev_addr, const u8 *ip)
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000951{
952 struct hostapd_data* iface_hapd = (struct hostapd_data*)ctx;
953 if (on_sta_authorized_internal_callback) {
954 on_sta_authorized_internal_callback(iface_hapd, mac_addr,
955 authorized, p2p_dev_addr);
956 }
957}
958
959std::function<void(struct hostapd_data*, int level,
960 enum wpa_msg_type type, const char *txt,
961 size_t len)> on_wpa_msg_internal_callback;
962
963void onAsyncWpaEventCb(void *ctx, int level,
964 enum wpa_msg_type type, const char *txt,
965 size_t len)
966{
967 struct hostapd_data* iface_hapd = (struct hostapd_data*)ctx;
968 if (on_wpa_msg_internal_callback) {
969 on_wpa_msg_internal_callback(iface_hapd, level,
970 type, txt, len);
971 }
972}
973
974inline ndk::ScopedAStatus createStatus(HostapdStatusCode status_code) {
975 return ndk::ScopedAStatus::fromServiceSpecificError(
976 static_cast<int32_t>(status_code));
977}
978
979inline ndk::ScopedAStatus createStatusWithMsg(
980 HostapdStatusCode status_code, std::string msg)
981{
982 return ndk::ScopedAStatus::fromServiceSpecificErrorWithMessage(
983 static_cast<int32_t>(status_code), msg.c_str());
984}
985
986// Method called by death_notifier_ on client death.
987void onDeath(void* cookie) {
988 wpa_printf(MSG_ERROR, "Client died. Terminating...");
989 eloop_terminate();
990}
991
992} // namespace
993
994namespace aidl {
995namespace android {
996namespace hardware {
997namespace wifi {
998namespace hostapd {
999
1000Hostapd::Hostapd(struct hapd_interfaces* interfaces)
1001 : interfaces_(interfaces)
1002{
1003 death_notifier_ = AIBinder_DeathRecipient_new(onDeath);
1004}
1005
1006::ndk::ScopedAStatus Hostapd::addAccessPoint(
1007 const IfaceParams& iface_params, const NetworkParams& nw_params)
1008{
1009 return addAccessPointInternal(iface_params, nw_params);
1010}
1011
1012::ndk::ScopedAStatus Hostapd::removeAccessPoint(const std::string& iface_name)
1013{
1014 return removeAccessPointInternal(iface_name);
1015}
1016
1017::ndk::ScopedAStatus Hostapd::terminate()
1018{
1019 wpa_printf(MSG_INFO, "Terminating...");
1020 // Clear the callback to avoid IPCThreadState shutdown during the
1021 // callback event.
1022 callbacks_.clear();
1023 eloop_terminate();
1024 return ndk::ScopedAStatus::ok();
1025}
1026
1027::ndk::ScopedAStatus Hostapd::registerCallback(
1028 const std::shared_ptr<IHostapdCallback>& callback)
1029{
1030 return registerCallbackInternal(callback);
1031}
1032
1033::ndk::ScopedAStatus Hostapd::forceClientDisconnect(
1034 const std::string& iface_name, const std::vector<uint8_t>& client_address,
1035 Ieee80211ReasonCode reason_code)
1036{
1037 return forceClientDisconnectInternal(iface_name, client_address, reason_code);
1038}
1039
1040::ndk::ScopedAStatus Hostapd::setDebugParams(DebugLevel level)
1041{
1042 return setDebugParamsInternal(level);
1043}
1044
1045::ndk::ScopedAStatus Hostapd::addAccessPointInternal(
1046 const IfaceParams& iface_params,
1047 const NetworkParams& nw_params)
1048{
1049 int channelParamsSize = iface_params.channelParams.size();
1050 if (channelParamsSize == 1) {
1051 // Single AP
1052 wpa_printf(MSG_INFO, "AddSingleAccessPoint, iface=%s",
1053 iface_params.name.c_str());
1054 return addSingleAccessPoint(iface_params, iface_params.channelParams[0],
Purushottam Kushwaha0316c882021-12-20 15:07:44 +05301055 nw_params, "", "");
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001056 } else if (channelParamsSize == 2) {
1057 // Concurrent APs
1058 wpa_printf(MSG_INFO, "AddDualAccessPoint, iface=%s",
1059 iface_params.name.c_str());
1060 return addConcurrentAccessPoints(iface_params, nw_params);
1061 }
1062 return createStatus(HostapdStatusCode::FAILURE_ARGS_INVALID);
1063}
1064
Purushottam Kushwaha0316c882021-12-20 15:07:44 +05301065std::vector<uint8_t> generateRandomOweSsid()
1066{
1067 u8 random[8] = {0};
1068 os_get_random(random, 8);
1069
1070 std::string ssid = StringPrintf("Owe-%s", random);
1071 wpa_printf(MSG_INFO, "Generated OWE SSID: %s", ssid.c_str());
1072 std::vector<uint8_t> vssid(ssid.begin(), ssid.end());
1073
1074 return vssid;
1075}
1076
Les Lee399c6302024-09-12 03:03:38 +00001077
1078// Both of bridged dual APs and MLO AP will be treated as concurrenct APs.
1079// -----------------------------------------
1080// | br_name | instance#1 | instance#2 |
1081// ___________________________________________________________
1082// bridged dual APs | ap_br_wlanX | wlan X | wlanY |
1083// ___________________________________________________________
1084// MLO AP | wlanX | 0 | 1 |
1085// ___________________________________________________________
1086// Both will be added in br_interfaces_[$br_name] and use instance's name
1087// to be iface_params_new.name to create single Access point.
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001088::ndk::ScopedAStatus Hostapd::addConcurrentAccessPoints(
1089 const IfaceParams& iface_params, const NetworkParams& nw_params)
1090{
1091 int channelParamsListSize = iface_params.channelParams.size();
1092 // Get available interfaces in bridge
Les Lee399c6302024-09-12 03:03:38 +00001093 std::vector<std::string> managed_instances;
1094 std::string br_name = StringPrintf("%s", iface_params.name.c_str());
1095 if (iface_params.usesMlo) {
1096 // MLO AP is using link id as instance.
1097 for (std::size_t i = 0; i < iface_params.instanceIdentities->size(); i++) {
1098 managed_instances.push_back(iface_params.instanceIdentities->at(i)->c_str());
1099 }
1100 } else {
1101 if (!GetInterfacesInBridge(br_name, &managed_instances)) {
1102 return createStatusWithMsg(HostapdStatusCode::FAILURE_UNKNOWN,
1103 "Get interfaces in bridge failed.");
1104 }
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001105 }
Les Lee399c6302024-09-12 03:03:38 +00001106 // Either bridged AP or MLO AP should have two instances.
1107 if (managed_instances.size() < channelParamsListSize) {
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001108 return createStatusWithMsg(HostapdStatusCode::FAILURE_UNKNOWN,
Les Lee399c6302024-09-12 03:03:38 +00001109 "Available interfaces less than requested bands");
1110 }
1111
1112 if (iface_params.usesMlo
1113 && nw_params.encryptionType == EncryptionType::WPA3_OWE_TRANSITION) {
1114 return createStatusWithMsg(HostapdStatusCode::FAILURE_UNKNOWN,
1115 "Invalid encryptionType (OWE transition) for MLO SAP.");
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001116 }
1117 // start BSS on specified bands
1118 for (std::size_t i = 0; i < channelParamsListSize; i ++) {
1119 IfaceParams iface_params_new = iface_params;
Purushottam Kushwaha0316c882021-12-20 15:07:44 +05301120 NetworkParams nw_params_new = nw_params;
Purushottam Kushwaha0316c882021-12-20 15:07:44 +05301121 std::string owe_transition_ifname = "";
Les Lee399c6302024-09-12 03:03:38 +00001122 iface_params_new.name = managed_instances[i];
Ahmed ElArabawy1aaf1802022-02-04 15:58:55 -08001123 if (nw_params.encryptionType == EncryptionType::WPA3_OWE_TRANSITION) {
Purushottam Kushwaha0316c882021-12-20 15:07:44 +05301124 if (i == 0 && i+1 < channelParamsListSize) {
Les Lee399c6302024-09-12 03:03:38 +00001125 owe_transition_ifname = managed_instances[i+1];
Purushottam Kushwaha0316c882021-12-20 15:07:44 +05301126 nw_params_new.encryptionType = EncryptionType::NONE;
1127 } else {
Les Lee399c6302024-09-12 03:03:38 +00001128 owe_transition_ifname = managed_instances[0];
Purushottam Kushwaha0316c882021-12-20 15:07:44 +05301129 nw_params_new.isHidden = true;
1130 nw_params_new.ssid = generateRandomOweSsid();
1131 }
1132 }
1133
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001134 ndk::ScopedAStatus status = addSingleAccessPoint(
Purushottam Kushwaha0316c882021-12-20 15:07:44 +05301135 iface_params_new, iface_params.channelParams[i], nw_params_new,
1136 br_name, owe_transition_ifname);
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001137 if (!status.isOk()) {
1138 wpa_printf(MSG_ERROR, "Failed to addAccessPoint %s",
Les Lee399c6302024-09-12 03:03:38 +00001139 managed_instances[i].c_str());
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001140 return status;
1141 }
1142 }
Les Lee399c6302024-09-12 03:03:38 +00001143
1144 if (iface_params.usesMlo) {
1145 std::size_t i = 0;
1146 std::size_t j = 0;
1147 for (i = 0; i < interfaces_->count; i++) {
1148 struct hostapd_iface *iface = interfaces_->iface[i];
1149
1150 for (j = 0; j < iface->num_bss; j++) {
1151 struct hostapd_data *iface_hapd = iface->bss[j];
1152 if (hostapd_enable_iface(iface_hapd->iface) < 0) {
1153 wpa_printf(
1154 MSG_ERROR, "Enabling interface %s failed on %zu",
1155 iface_params.name.c_str(), i);
1156 return createStatus(HostapdStatusCode::FAILURE_UNKNOWN);
1157 }
1158 }
1159 }
1160 }
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001161 // Save bridge interface info
Les Lee399c6302024-09-12 03:03:38 +00001162 br_interfaces_[br_name] = managed_instances;
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001163 return ndk::ScopedAStatus::ok();
1164}
1165
Les Lee399c6302024-09-12 03:03:38 +00001166struct hostapd_data * hostapd_get_iface_by_link_id(struct hapd_interfaces *interfaces,
1167 const size_t link_id)
1168{
1169#ifdef CONFIG_IEEE80211BE
1170 size_t i, j;
1171
1172 for (i = 0; i < interfaces->count; i++) {
1173 struct hostapd_iface *iface = interfaces->iface[i];
1174
1175 for (j = 0; j < iface->num_bss; j++) {
1176 struct hostapd_data *hapd = iface->bss[j];
1177
1178 if (link_id == hapd->mld_link_id)
1179 return hapd;
1180 }
1181 }
1182#endif
1183 return NULL;
1184}
1185
1186// Both of bridged dual APs and MLO AP will be treated as concurrenct APs.
1187// -----------------------------------------
1188// | br_name | iface_params.name
1189// _______________________________________________________________
1190// bridged dual APs | bridged interface name | interface name
1191// _______________________________________________________________
1192// MLO AP | AP interface name | mld link id as instance name
1193// _______________________________________________________________
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001194::ndk::ScopedAStatus Hostapd::addSingleAccessPoint(
1195 const IfaceParams& iface_params,
1196 const ChannelParams& channelParams,
1197 const NetworkParams& nw_params,
Purushottam Kushwaha0316c882021-12-20 15:07:44 +05301198 const std::string br_name,
1199 const std::string owe_transition_ifname)
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001200{
Les Lee399c6302024-09-12 03:03:38 +00001201 if (iface_params.usesMlo) { // the mlo case, iface name is instance name which is mld_link_id
1202 if (hostapd_get_iface_by_link_id(interfaces_, (size_t) iface_params.name.c_str())) {
1203 wpa_printf(
1204 MSG_ERROR, "Instance link id %s already present",
1205 iface_params.name.c_str());
1206 return createStatus(HostapdStatusCode::FAILURE_IFACE_EXISTS);
1207 }
1208 }
1209 if (hostapd_get_iface(interfaces_,
1210 iface_params.usesMlo ? br_name.c_str() : iface_params.name.c_str())) {
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001211 wpa_printf(
Les Lee399c6302024-09-12 03:03:38 +00001212 MSG_ERROR, "Instance interface %s already present",
1213 iface_params.usesMlo ? br_name.c_str() : iface_params.name.c_str());
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001214 return createStatus(HostapdStatusCode::FAILURE_IFACE_EXISTS);
1215 }
Purushottam Kushwaha0316c882021-12-20 15:07:44 +05301216 const auto conf_params = CreateHostapdConfig(iface_params, channelParams, nw_params,
1217 br_name, owe_transition_ifname);
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001218 if (conf_params.empty()) {
1219 wpa_printf(MSG_ERROR, "Failed to create config params");
1220 return createStatus(HostapdStatusCode::FAILURE_ARGS_INVALID);
1221 }
1222 const auto conf_file_path =
Les Lee399c6302024-09-12 03:03:38 +00001223 WriteHostapdConfig(iface_params.name, conf_params, br_name, iface_params.usesMlo);
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001224 if (conf_file_path.empty()) {
1225 wpa_printf(MSG_ERROR, "Failed to write config file");
1226 return createStatus(HostapdStatusCode::FAILURE_UNKNOWN);
1227 }
1228 std::string add_iface_param_str = StringPrintf(
Les Lee399c6302024-09-12 03:03:38 +00001229 "%s config=%s", iface_params.usesMlo ? br_name.c_str(): iface_params.name.c_str(),
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001230 conf_file_path.c_str());
1231 std::vector<char> add_iface_param_vec(
1232 add_iface_param_str.begin(), add_iface_param_str.end() + 1);
1233 if (hostapd_add_iface(interfaces_, add_iface_param_vec.data()) < 0) {
1234 wpa_printf(
Les Lee399c6302024-09-12 03:03:38 +00001235 MSG_ERROR, "Adding hostapd iface %s failed",
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001236 add_iface_param_str.c_str());
1237 return createStatus(HostapdStatusCode::FAILURE_UNKNOWN);
1238 }
Les Lee399c6302024-09-12 03:03:38 +00001239
1240 // find the iface and set up callback.
1241 struct hostapd_data* iface_hapd = iface_params.usesMlo ?
1242 hostapd_get_iface_by_link_id(interfaces_, (size_t) iface_params.name.c_str()) :
1243 hostapd_get_iface(interfaces_, iface_params.name.c_str());
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001244 WPA_ASSERT(iface_hapd != nullptr && iface_hapd->iface != nullptr);
Les Lee399c6302024-09-12 03:03:38 +00001245 if (iface_params.usesMlo) {
1246 memcmp(iface_hapd->conf->iface, br_name.c_str(), br_name.size());
1247 }
1248
1249 // Callback discrepancy between bridged dual APs and MLO AP
1250 // Note: Only bridged dual APs will have "iface_hapd->conf->bridge" and
1251 // Only MLO AP will have "iface_hapd->mld_link_id"
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001252 // Register the setup complete callbacks
Les Lee399c6302024-09-12 03:03:38 +00001253 // -----------------------------------------
1254 // | bridged dual APs | bridged single link MLO | MLO SAP
1255 // _________________________________________________________________________________________
1256 // hapd->conf->bridge | bridged interface name | bridged interface nam | N/A
1257 // _________________________________________________________________________________________
1258 // hapd->conf->iface | AP interface name | AP interface name | AP interface name
1259 // _________________________________________________________________________________________
1260 // hapd->mld_link_id | 0 (default value) | link id (0) | link id (0 or 1)
1261 // _________________________________________________________________________________________
1262 // hapd->mld_ap | 0 | 1 | 1
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001263 on_setup_complete_internal_callback =
1264 [this](struct hostapd_data* iface_hapd) {
1265 wpa_printf(
1266 MSG_INFO, "AP interface setup completed - state %s",
1267 hostapd_state_text(iface_hapd->iface->state));
1268 if (iface_hapd->iface->state == HAPD_IFACE_DISABLED) {
1269 // Invoke the failure callback on all registered
1270 // clients.
Les Lee399c6302024-09-12 03:03:38 +00001271 std::string instanceName = iface_hapd->conf->iface;
1272#ifdef CONFIG_IEEE80211BE
1273 if (iface_hapd->conf->mld_ap
1274 && strlen(iface_hapd->conf->bridge) == 0) {
1275 instanceName = std::to_string(iface_hapd->mld_link_id);
1276 }
1277#endif
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001278 for (const auto& callback : callbacks_) {
Chenming Huangbaa5e582024-04-02 08:21:10 +05301279 auto status = callback->onFailure(
1280 strlen(iface_hapd->conf->bridge) > 0 ?
Les Leee08c2862021-10-29 16:36:41 +08001281 iface_hapd->conf->bridge : iface_hapd->conf->iface,
Les Lee399c6302024-09-12 03:03:38 +00001282 instanceName);
Chenming Huangbaa5e582024-04-02 08:21:10 +05301283 if (!status.isOk()) {
1284 wpa_printf(MSG_ERROR, "Failed to invoke onFailure");
1285 }
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001286 }
1287 }
1288 };
1289
1290 // Register for new client connect/disconnect indication.
1291 on_sta_authorized_internal_callback =
1292 [this](struct hostapd_data* iface_hapd, const u8 *mac_addr,
1293 int authorized, const u8 *p2p_dev_addr) {
1294 wpa_printf(MSG_DEBUG, "notify client " MACSTR " %s",
1295 MAC2STR(mac_addr),
1296 (authorized) ? "Connected" : "Disconnected");
1297 ClientInfo info;
1298 info.ifaceName = strlen(iface_hapd->conf->bridge) > 0 ?
1299 iface_hapd->conf->bridge : iface_hapd->conf->iface;
Les Lee399c6302024-09-12 03:03:38 +00001300 std::string instanceName = iface_hapd->conf->iface;
1301#ifdef CONFIG_IEEE80211BE
1302 if (iface_hapd->conf->mld_ap
1303 && strlen(iface_hapd->conf->bridge) == 0) {
1304 instanceName = std::to_string(iface_hapd->mld_link_id);
1305 }
1306#endif
1307 info.apIfaceInstance = instanceName;
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001308 info.clientAddress.assign(mac_addr, mac_addr + ETH_ALEN);
1309 info.isConnected = authorized;
Chris Weir808564b2024-08-07 10:12:16 -07001310 if(isAidlServiceVersionAtLeast(3) && !authorized) {
1311 u16 disconnect_reason_code = WLAN_REASON_UNSPECIFIED;
1312 auto sta_ptr_optional = getStaInfoByMacAddr(iface_hapd, mac_addr);
1313 if (sta_ptr_optional.has_value()){
1314 disconnect_reason_code = sta_ptr_optional.value()->deauth_reason;
1315 }
1316 info.disconnectReasonCode =
1317 static_cast<common::DeauthenticationReasonCode>(disconnect_reason_code);
1318 }
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001319 for (const auto &callback : callbacks_) {
Chenming Huangbaa5e582024-04-02 08:21:10 +05301320 auto status = callback->onConnectedClientsChanged(info);
1321 if (!status.isOk()) {
1322 wpa_printf(MSG_ERROR, "Failed to invoke onConnectedClientsChanged");
1323 }
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001324 }
1325 };
1326
1327 // Register for wpa_event which used to get channel switch event
1328 on_wpa_msg_internal_callback =
1329 [this](struct hostapd_data* iface_hapd, int level,
1330 enum wpa_msg_type type, const char *txt,
1331 size_t len) {
1332 wpa_printf(MSG_DEBUG, "Receive wpa msg : %s", txt);
1333 if (os_strncmp(txt, AP_EVENT_ENABLED,
1334 strlen(AP_EVENT_ENABLED)) == 0 ||
1335 os_strncmp(txt, WPA_EVENT_CHANNEL_SWITCH,
1336 strlen(WPA_EVENT_CHANNEL_SWITCH)) == 0) {
Les Lee399c6302024-09-12 03:03:38 +00001337 std::string instanceName = iface_hapd->conf->iface;
1338#ifdef CONFIG_IEEE80211BE
1339 if (iface_hapd->conf->mld_ap && strlen(iface_hapd->conf->bridge) == 0) {
1340 instanceName = std::to_string(iface_hapd->mld_link_id);
1341 }
1342#endif
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001343 ApInfo info;
1344 info.ifaceName = strlen(iface_hapd->conf->bridge) > 0 ?
1345 iface_hapd->conf->bridge : iface_hapd->conf->iface,
Les Lee399c6302024-09-12 03:03:38 +00001346 info.apIfaceInstance = instanceName;
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001347 info.freqMhz = iface_hapd->iface->freq;
Ahmed ElArabawyb4115792022-02-08 09:33:01 -08001348 info.channelBandwidth = getChannelBandwidth(iface_hapd->iconf);
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001349 info.generation = getGeneration(iface_hapd->iface->current_mode);
1350 info.apIfaceInstanceMacAddress.assign(iface_hapd->own_addr,
1351 iface_hapd->own_addr + ETH_ALEN);
1352 for (const auto &callback : callbacks_) {
Chenming Huangbaa5e582024-04-02 08:21:10 +05301353 auto status = callback->onApInstanceInfoChanged(info);
1354 if (!status.isOk()) {
1355 wpa_printf(MSG_ERROR,
1356 "Failed to invoke onApInstanceInfoChanged");
1357 }
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001358 }
Les Leea0c90cb2022-04-19 17:39:23 +08001359 } else if (os_strncmp(txt, AP_EVENT_DISABLED, strlen(AP_EVENT_DISABLED)) == 0
1360 || os_strncmp(txt, INTERFACE_DISABLED, strlen(INTERFACE_DISABLED)) == 0)
1361 {
Les Lee399c6302024-09-12 03:03:38 +00001362 std::string instanceName = iface_hapd->conf->iface;
1363#ifdef CONFIG_IEEE80211BE
1364 if (iface_hapd->conf->mld_ap && strlen(iface_hapd->conf->bridge) == 0) {
1365 instanceName = std::to_string(iface_hapd->mld_link_id);
1366 }
1367#endif
Yu Ouyang378d3c42021-08-20 17:31:08 +08001368 // Invoke the failure callback on all registered clients.
1369 for (const auto& callback : callbacks_) {
Les Lee399c6302024-09-12 03:03:38 +00001370 auto status =
1371 callback->onFailure(strlen(iface_hapd->conf->bridge) > 0 ?
Les Leee08c2862021-10-29 16:36:41 +08001372 iface_hapd->conf->bridge : iface_hapd->conf->iface,
Les Lee399c6302024-09-12 03:03:38 +00001373 instanceName);
Chenming Huangbaa5e582024-04-02 08:21:10 +05301374 if (!status.isOk()) {
1375 wpa_printf(MSG_ERROR, "Failed to invoke onFailure");
1376 }
Yu Ouyang378d3c42021-08-20 17:31:08 +08001377 }
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001378 }
Yu Ouyang378d3c42021-08-20 17:31:08 +08001379 };
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001380
1381 // Setup callback
1382 iface_hapd->setup_complete_cb = onAsyncSetupCompleteCb;
1383 iface_hapd->setup_complete_cb_ctx = iface_hapd;
1384 iface_hapd->sta_authorized_cb = onAsyncStaAuthorizedCb;
1385 iface_hapd->sta_authorized_cb_ctx = iface_hapd;
Hu Wang7c5a4322021-06-24 17:24:59 +08001386 wpa_msg_register_aidl_cb(onAsyncWpaEventCb);
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001387
Les Lee399c6302024-09-12 03:03:38 +00001388 // Multi-link MLO should enable iface after both links have been set.
1389 if (!iface_params.usesMlo && hostapd_enable_iface(iface_hapd->iface) < 0) {
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001390 wpa_printf(
1391 MSG_ERROR, "Enabling interface %s failed",
1392 iface_params.name.c_str());
1393 return createStatus(HostapdStatusCode::FAILURE_UNKNOWN);
1394 }
1395 return ndk::ScopedAStatus::ok();
1396}
1397
1398::ndk::ScopedAStatus Hostapd::removeAccessPointInternal(const std::string& iface_name)
1399{
1400 // interfaces to be removed
1401 std::vector<std::string> interfaces;
1402 bool is_error = false;
1403
1404 const auto it = br_interfaces_.find(iface_name);
1405 if (it != br_interfaces_.end()) {
1406 // In case bridge, remove managed interfaces
1407 interfaces = it->second;
1408 br_interfaces_.erase(iface_name);
1409 } else {
1410 // else remove current interface
1411 interfaces.push_back(iface_name);
1412 }
1413
1414 for (auto& iface : interfaces) {
1415 std::vector<char> remove_iface_param_vec(
1416 iface.begin(), iface.end() + 1);
1417 if (hostapd_remove_iface(interfaces_, remove_iface_param_vec.data()) < 0) {
1418 wpa_printf(MSG_INFO, "Remove interface %s failed", iface.c_str());
1419 is_error = true;
1420 }
1421 }
1422 if (is_error) {
1423 return createStatus(HostapdStatusCode::FAILURE_UNKNOWN);
1424 }
1425 return ndk::ScopedAStatus::ok();
1426}
1427
1428::ndk::ScopedAStatus Hostapd::registerCallbackInternal(
1429 const std::shared_ptr<IHostapdCallback>& callback)
1430{
1431 binder_status_t status = AIBinder_linkToDeath(callback->asBinder().get(),
1432 death_notifier_, this /* cookie */);
1433 if (status != STATUS_OK) {
1434 wpa_printf(
1435 MSG_ERROR,
1436 "Error registering for death notification for "
1437 "hostapd callback object");
1438 return createStatus(HostapdStatusCode::FAILURE_UNKNOWN);
1439 }
1440 callbacks_.push_back(callback);
Manaswini Paluri76ae6982024-02-29 14:49:20 +05301441 if (aidl_service_version == 0) {
1442 aidl_service_version = Hostapd::version;
1443 wpa_printf(MSG_INFO, "AIDL service version: %d", aidl_service_version);
1444 }
1445 if (aidl_client_version == 0) {
1446 callback->getInterfaceVersion(&aidl_client_version);
1447 wpa_printf(MSG_INFO, "AIDL client version: %d", aidl_client_version);
1448 }
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001449 return ndk::ScopedAStatus::ok();
1450}
1451
1452::ndk::ScopedAStatus Hostapd::forceClientDisconnectInternal(const std::string& iface_name,
1453 const std::vector<uint8_t>& client_address, Ieee80211ReasonCode reason_code)
1454{
1455 struct hostapd_data *hapd = hostapd_get_iface(interfaces_, iface_name.c_str());
1456 bool result;
1457 if (!hapd) {
1458 for (auto const& iface : br_interfaces_) {
1459 if (iface.first == iface_name) {
1460 for (auto const& instance : iface.second) {
1461 hapd = hostapd_get_iface(interfaces_, instance.c_str());
1462 if (hapd) {
1463 result = forceStaDisconnection(hapd, client_address,
1464 (uint16_t) reason_code);
1465 if (result) break;
1466 }
1467 }
1468 }
1469 }
1470 } else {
1471 result = forceStaDisconnection(hapd, client_address, (uint16_t) reason_code);
1472 }
1473 if (!hapd) {
1474 wpa_printf(MSG_ERROR, "Interface %s doesn't exist", iface_name.c_str());
1475 return createStatus(HostapdStatusCode::FAILURE_IFACE_UNKNOWN);
1476 }
1477 if (result) {
1478 return ndk::ScopedAStatus::ok();
1479 }
1480 return createStatus(HostapdStatusCode::FAILURE_CLIENT_UNKNOWN);
1481}
1482
1483::ndk::ScopedAStatus Hostapd::setDebugParamsInternal(DebugLevel level)
1484{
1485 wpa_debug_level = static_cast<uint32_t>(level);
1486 return ndk::ScopedAStatus::ok();
1487}
1488
1489} // namespace hostapd
1490} // namespace wifi
1491} // namespace hardware
1492} // namespace android
Les Leee08c2862021-10-29 16:36:41 +08001493} // namespace aidl