blob: b5118b379254e1aa7de062518d8f3651780f688f [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
Les Lee9dfae3b2024-10-25 18:31:47 +0000784 std::string ap_isolation_as_string = StringPrintf("ap_isolate=%s",
785 isAidlServiceVersionAtLeast(3) && nw_params.isClientIsolationEnabled ?
786 "1" : "0");
787
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000788 return StringPrintf(
789 "interface=%s\n"
790 "driver=nl80211\n"
Les Lee399c6302024-09-12 03:03:38 +0000791 "ctrl_interface=/data/vendor/wifi/hostapd/ctrl_%s\n"
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000792 // ssid2 signals to hostapd that the value is not a literal value
793 // for use as a SSID. In this case, we're giving it a hex
794 // std::string and hostapd needs to expect that.
795 "ssid2=%s\n"
796 "%s\n"
797 "ieee80211n=%d\n"
798 "ieee80211ac=%d\n"
799 "%s\n"
800 "%s\n"
801 "%s\n"
Sunil Ravi65251732023-01-24 05:03:35 +0000802 "%s\n"
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000803 "ignore_broadcast_ssid=%d\n"
804 "wowlan_triggers=any\n"
805#ifdef CONFIG_INTERWORKING
806 "%s\n"
807#endif /* CONFIG_INTERWORKING */
808 "%s\n"
809 "%s\n"
810 "%s\n"
Serik Beketayev8af7a722021-12-23 12:25:36 -0800811 "%s\n"
Purushottam Kushwaha0316c882021-12-20 15:07:44 +0530812 "%s\n"
Les Lee9dfae3b2024-10-25 18:31:47 +0000813 "%s\n"
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000814 "%s\n",
Les Lee399c6302024-09-12 03:03:38 +0000815 iface_params.usesMlo ? br_name.c_str() : iface_params.name.c_str(),
816 iface_params.name.c_str(),
817 ssid_as_string.c_str(),
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000818 channel_config_as_string.c_str(),
819 iface_params.hwModeParams.enable80211N ? 1 : 0,
820 iface_params.hwModeParams.enable80211AC ? 1 : 0,
821 he_params_as_string.c_str(),
Sunil Ravi65251732023-01-24 05:03:35 +0000822 eht_params_as_string.c_str(),
Xin Dengfec682f2024-02-06 22:59:39 -0800823 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 +0000824 nw_params.isHidden ? 1 : 0,
825#ifdef CONFIG_INTERWORKING
826 access_network_params_as_string.c_str(),
827#endif /* CONFIG_INTERWORKING */
828 encryption_config_as_string.c_str(),
829 bridge_as_string.c_str(),
Purushottam Kushwaha0316c882021-12-20 15:07:44 +0530830 owe_transition_ifname_as_string.c_str(),
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000831 enable_edmg_as_string.c_str(),
Serik Beketayev8af7a722021-12-23 12:25:36 -0800832 edmg_channel_as_string.c_str(),
Les Lee9dfae3b2024-10-25 18:31:47 +0000833 vendor_elements_as_string.c_str(),
834 ap_isolation_as_string.c_str());
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000835}
836
837Generation getGeneration(hostapd_hw_modes *current_mode)
838{
839 wpa_printf(MSG_DEBUG, "getGeneration hwmode=%d, ht_enabled=%d,"
840 " vht_enabled=%d, he_supported=%d",
841 current_mode->mode, current_mode->ht_capab != 0,
842 current_mode->vht_capab != 0, current_mode->he_capab->he_supported);
843 switch (current_mode->mode) {
844 case HOSTAPD_MODE_IEEE80211B:
845 return Generation::WIFI_STANDARD_LEGACY;
846 case HOSTAPD_MODE_IEEE80211G:
847 return current_mode->ht_capab == 0 ?
848 Generation::WIFI_STANDARD_LEGACY : Generation::WIFI_STANDARD_11N;
849 case HOSTAPD_MODE_IEEE80211A:
850 if (current_mode->he_capab->he_supported) {
851 return Generation::WIFI_STANDARD_11AX;
852 }
853 return current_mode->vht_capab == 0 ?
854 Generation::WIFI_STANDARD_11N : Generation::WIFI_STANDARD_11AC;
855 case HOSTAPD_MODE_IEEE80211AD:
856 return Generation::WIFI_STANDARD_11AD;
857 default:
858 return Generation::WIFI_STANDARD_UNKNOWN;
859 }
860}
861
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800862ChannelBandwidth getChannelBandwidth(struct hostapd_config *iconf)
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000863{
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800864 wpa_printf(MSG_DEBUG, "getChannelBandwidth %d, isHT=%d, isHT40=%d",
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000865 iconf->vht_oper_chwidth, iconf->ieee80211n,
866 iconf->secondary_channel);
867 switch (iconf->vht_oper_chwidth) {
Sunil8cd6f4d2022-06-28 18:40:46 +0000868 case CONF_OPER_CHWIDTH_80MHZ:
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800869 return ChannelBandwidth::BANDWIDTH_80;
Sunil8cd6f4d2022-06-28 18:40:46 +0000870 case CONF_OPER_CHWIDTH_80P80MHZ:
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800871 return ChannelBandwidth::BANDWIDTH_80P80;
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000872 break;
Sunil8cd6f4d2022-06-28 18:40:46 +0000873 case CONF_OPER_CHWIDTH_160MHZ:
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800874 return ChannelBandwidth::BANDWIDTH_160;
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000875 break;
Sunil8cd6f4d2022-06-28 18:40:46 +0000876 case CONF_OPER_CHWIDTH_USE_HT:
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000877 if (iconf->ieee80211n) {
878 return iconf->secondary_channel != 0 ?
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800879 ChannelBandwidth::BANDWIDTH_40 : ChannelBandwidth::BANDWIDTH_20;
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000880 }
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800881 return ChannelBandwidth::BANDWIDTH_20_NOHT;
Sunil8cd6f4d2022-06-28 18:40:46 +0000882 case CONF_OPER_CHWIDTH_2160MHZ:
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800883 return ChannelBandwidth::BANDWIDTH_2160;
Sunil8cd6f4d2022-06-28 18:40:46 +0000884 case CONF_OPER_CHWIDTH_4320MHZ:
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800885 return ChannelBandwidth::BANDWIDTH_4320;
Sunil8cd6f4d2022-06-28 18:40:46 +0000886 case CONF_OPER_CHWIDTH_6480MHZ:
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800887 return ChannelBandwidth::BANDWIDTH_6480;
Sunil8cd6f4d2022-06-28 18:40:46 +0000888 case CONF_OPER_CHWIDTH_8640MHZ:
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800889 return ChannelBandwidth::BANDWIDTH_8640;
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000890 default:
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800891 return ChannelBandwidth::BANDWIDTH_INVALID;
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000892 }
893}
894
Chris Weir808564b2024-08-07 10:12:16 -0700895std::optional<struct sta_info*> getStaInfoByMacAddr(const struct hostapd_data* iface_hapd,
896 const u8 *mac_addr) {
897 if (iface_hapd == nullptr || mac_addr == nullptr){
898 wpa_printf(MSG_ERROR, "nullptr passsed to getStaInfoByMacAddr!");
899 return std::nullopt;
900 }
901
902 for (struct sta_info* sta_ptr = iface_hapd->sta_list; sta_ptr; sta_ptr = sta_ptr->next) {
903 int res;
904 res = memcmp(sta_ptr->addr, mac_addr, ETH_ALEN);
905 if (res == 0) {
906 return sta_ptr;
907 }
908 }
909 return std::nullopt;
910}
911
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000912bool forceStaDisconnection(struct hostapd_data* hapd,
913 const std::vector<uint8_t>& client_address,
914 const uint16_t reason_code) {
Sunil Ravi1a360892022-11-29 20:16:01 +0000915 if (client_address.size() != ETH_ALEN) {
916 return false;
917 }
Chris Weir808564b2024-08-07 10:12:16 -0700918
919 auto sta_ptr_optional = getStaInfoByMacAddr(hapd, client_address.data());
920 if (sta_ptr_optional.has_value()) {
921 wpa_printf(MSG_INFO, "Force client:" MACSTR " disconnect with reason: %d",
922 MAC2STR(client_address.data()), reason_code);
923 ap_sta_disconnect(hapd, sta_ptr_optional.value(), sta_ptr_optional.value()->addr,
924 reason_code);
925 return true;
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000926 }
Chris Weir808564b2024-08-07 10:12:16 -0700927
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000928 return false;
929}
930
931// hostapd core functions accept "C" style function pointers, so use global
932// functions to pass to the hostapd core function and store the corresponding
933// std::function methods to be invoked.
934//
935// NOTE: Using the pattern from the vendor HAL (wifi_legacy_hal.cpp).
936//
937// Callback to be invoked once setup is complete
938std::function<void(struct hostapd_data*)> on_setup_complete_internal_callback;
939void onAsyncSetupCompleteCb(void* ctx)
940{
941 struct hostapd_data* iface_hapd = (struct hostapd_data*)ctx;
942 if (on_setup_complete_internal_callback) {
943 on_setup_complete_internal_callback(iface_hapd);
944 // Invalidate this callback since we don't want this firing
945 // again in single AP mode.
946 if (strlen(iface_hapd->conf->bridge) > 0) {
947 on_setup_complete_internal_callback = nullptr;
948 }
949 }
950}
951
952// Callback to be invoked on hotspot client connection/disconnection
953std::function<void(struct hostapd_data*, const u8 *mac_addr, int authorized,
954 const u8 *p2p_dev_addr)> on_sta_authorized_internal_callback;
955void onAsyncStaAuthorizedCb(void* ctx, const u8 *mac_addr, int authorized,
Sunil Ravid8128a22023-11-06 23:53:58 +0000956 const u8 *p2p_dev_addr, const u8 *ip)
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000957{
958 struct hostapd_data* iface_hapd = (struct hostapd_data*)ctx;
959 if (on_sta_authorized_internal_callback) {
960 on_sta_authorized_internal_callback(iface_hapd, mac_addr,
961 authorized, p2p_dev_addr);
962 }
963}
964
965std::function<void(struct hostapd_data*, int level,
966 enum wpa_msg_type type, const char *txt,
967 size_t len)> on_wpa_msg_internal_callback;
968
969void onAsyncWpaEventCb(void *ctx, int level,
970 enum wpa_msg_type type, const char *txt,
971 size_t len)
972{
973 struct hostapd_data* iface_hapd = (struct hostapd_data*)ctx;
974 if (on_wpa_msg_internal_callback) {
975 on_wpa_msg_internal_callback(iface_hapd, level,
976 type, txt, len);
977 }
978}
979
980inline ndk::ScopedAStatus createStatus(HostapdStatusCode status_code) {
981 return ndk::ScopedAStatus::fromServiceSpecificError(
982 static_cast<int32_t>(status_code));
983}
984
985inline ndk::ScopedAStatus createStatusWithMsg(
986 HostapdStatusCode status_code, std::string msg)
987{
988 return ndk::ScopedAStatus::fromServiceSpecificErrorWithMessage(
989 static_cast<int32_t>(status_code), msg.c_str());
990}
991
992// Method called by death_notifier_ on client death.
993void onDeath(void* cookie) {
994 wpa_printf(MSG_ERROR, "Client died. Terminating...");
995 eloop_terminate();
996}
997
998} // namespace
999
1000namespace aidl {
1001namespace android {
1002namespace hardware {
1003namespace wifi {
1004namespace hostapd {
1005
1006Hostapd::Hostapd(struct hapd_interfaces* interfaces)
1007 : interfaces_(interfaces)
1008{
1009 death_notifier_ = AIBinder_DeathRecipient_new(onDeath);
1010}
1011
1012::ndk::ScopedAStatus Hostapd::addAccessPoint(
1013 const IfaceParams& iface_params, const NetworkParams& nw_params)
1014{
1015 return addAccessPointInternal(iface_params, nw_params);
1016}
1017
1018::ndk::ScopedAStatus Hostapd::removeAccessPoint(const std::string& iface_name)
1019{
1020 return removeAccessPointInternal(iface_name);
1021}
1022
1023::ndk::ScopedAStatus Hostapd::terminate()
1024{
1025 wpa_printf(MSG_INFO, "Terminating...");
1026 // Clear the callback to avoid IPCThreadState shutdown during the
1027 // callback event.
1028 callbacks_.clear();
1029 eloop_terminate();
1030 return ndk::ScopedAStatus::ok();
1031}
1032
1033::ndk::ScopedAStatus Hostapd::registerCallback(
1034 const std::shared_ptr<IHostapdCallback>& callback)
1035{
1036 return registerCallbackInternal(callback);
1037}
1038
1039::ndk::ScopedAStatus Hostapd::forceClientDisconnect(
1040 const std::string& iface_name, const std::vector<uint8_t>& client_address,
1041 Ieee80211ReasonCode reason_code)
1042{
1043 return forceClientDisconnectInternal(iface_name, client_address, reason_code);
1044}
1045
1046::ndk::ScopedAStatus Hostapd::setDebugParams(DebugLevel level)
1047{
1048 return setDebugParamsInternal(level);
1049}
1050
1051::ndk::ScopedAStatus Hostapd::addAccessPointInternal(
1052 const IfaceParams& iface_params,
1053 const NetworkParams& nw_params)
1054{
1055 int channelParamsSize = iface_params.channelParams.size();
1056 if (channelParamsSize == 1) {
1057 // Single AP
1058 wpa_printf(MSG_INFO, "AddSingleAccessPoint, iface=%s",
1059 iface_params.name.c_str());
1060 return addSingleAccessPoint(iface_params, iface_params.channelParams[0],
Purushottam Kushwaha0316c882021-12-20 15:07:44 +05301061 nw_params, "", "");
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001062 } else if (channelParamsSize == 2) {
1063 // Concurrent APs
1064 wpa_printf(MSG_INFO, "AddDualAccessPoint, iface=%s",
1065 iface_params.name.c_str());
1066 return addConcurrentAccessPoints(iface_params, nw_params);
1067 }
1068 return createStatus(HostapdStatusCode::FAILURE_ARGS_INVALID);
1069}
1070
Purushottam Kushwaha0316c882021-12-20 15:07:44 +05301071std::vector<uint8_t> generateRandomOweSsid()
1072{
1073 u8 random[8] = {0};
1074 os_get_random(random, 8);
1075
1076 std::string ssid = StringPrintf("Owe-%s", random);
1077 wpa_printf(MSG_INFO, "Generated OWE SSID: %s", ssid.c_str());
1078 std::vector<uint8_t> vssid(ssid.begin(), ssid.end());
1079
1080 return vssid;
1081}
1082
Les Lee399c6302024-09-12 03:03:38 +00001083
1084// Both of bridged dual APs and MLO AP will be treated as concurrenct APs.
1085// -----------------------------------------
1086// | br_name | instance#1 | instance#2 |
1087// ___________________________________________________________
1088// bridged dual APs | ap_br_wlanX | wlan X | wlanY |
1089// ___________________________________________________________
1090// MLO AP | wlanX | 0 | 1 |
1091// ___________________________________________________________
1092// Both will be added in br_interfaces_[$br_name] and use instance's name
1093// to be iface_params_new.name to create single Access point.
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001094::ndk::ScopedAStatus Hostapd::addConcurrentAccessPoints(
1095 const IfaceParams& iface_params, const NetworkParams& nw_params)
1096{
1097 int channelParamsListSize = iface_params.channelParams.size();
1098 // Get available interfaces in bridge
Les Lee399c6302024-09-12 03:03:38 +00001099 std::vector<std::string> managed_instances;
1100 std::string br_name = StringPrintf("%s", iface_params.name.c_str());
1101 if (iface_params.usesMlo) {
1102 // MLO AP is using link id as instance.
1103 for (std::size_t i = 0; i < iface_params.instanceIdentities->size(); i++) {
1104 managed_instances.push_back(iface_params.instanceIdentities->at(i)->c_str());
1105 }
1106 } else {
1107 if (!GetInterfacesInBridge(br_name, &managed_instances)) {
1108 return createStatusWithMsg(HostapdStatusCode::FAILURE_UNKNOWN,
1109 "Get interfaces in bridge failed.");
1110 }
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001111 }
Les Lee399c6302024-09-12 03:03:38 +00001112 // Either bridged AP or MLO AP should have two instances.
1113 if (managed_instances.size() < channelParamsListSize) {
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001114 return createStatusWithMsg(HostapdStatusCode::FAILURE_UNKNOWN,
Les Lee399c6302024-09-12 03:03:38 +00001115 "Available interfaces less than requested bands");
1116 }
1117
1118 if (iface_params.usesMlo
1119 && nw_params.encryptionType == EncryptionType::WPA3_OWE_TRANSITION) {
1120 return createStatusWithMsg(HostapdStatusCode::FAILURE_UNKNOWN,
1121 "Invalid encryptionType (OWE transition) for MLO SAP.");
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001122 }
1123 // start BSS on specified bands
1124 for (std::size_t i = 0; i < channelParamsListSize; i ++) {
1125 IfaceParams iface_params_new = iface_params;
Purushottam Kushwaha0316c882021-12-20 15:07:44 +05301126 NetworkParams nw_params_new = nw_params;
Purushottam Kushwaha0316c882021-12-20 15:07:44 +05301127 std::string owe_transition_ifname = "";
Les Lee399c6302024-09-12 03:03:38 +00001128 iface_params_new.name = managed_instances[i];
Ahmed ElArabawy1aaf1802022-02-04 15:58:55 -08001129 if (nw_params.encryptionType == EncryptionType::WPA3_OWE_TRANSITION) {
Purushottam Kushwaha0316c882021-12-20 15:07:44 +05301130 if (i == 0 && i+1 < channelParamsListSize) {
Les Lee399c6302024-09-12 03:03:38 +00001131 owe_transition_ifname = managed_instances[i+1];
Purushottam Kushwaha0316c882021-12-20 15:07:44 +05301132 nw_params_new.encryptionType = EncryptionType::NONE;
1133 } else {
Les Lee399c6302024-09-12 03:03:38 +00001134 owe_transition_ifname = managed_instances[0];
Purushottam Kushwaha0316c882021-12-20 15:07:44 +05301135 nw_params_new.isHidden = true;
1136 nw_params_new.ssid = generateRandomOweSsid();
1137 }
1138 }
1139
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001140 ndk::ScopedAStatus status = addSingleAccessPoint(
Purushottam Kushwaha0316c882021-12-20 15:07:44 +05301141 iface_params_new, iface_params.channelParams[i], nw_params_new,
1142 br_name, owe_transition_ifname);
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001143 if (!status.isOk()) {
1144 wpa_printf(MSG_ERROR, "Failed to addAccessPoint %s",
Les Lee399c6302024-09-12 03:03:38 +00001145 managed_instances[i].c_str());
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001146 return status;
1147 }
1148 }
Les Lee399c6302024-09-12 03:03:38 +00001149
1150 if (iface_params.usesMlo) {
1151 std::size_t i = 0;
1152 std::size_t j = 0;
1153 for (i = 0; i < interfaces_->count; i++) {
1154 struct hostapd_iface *iface = interfaces_->iface[i];
1155
1156 for (j = 0; j < iface->num_bss; j++) {
1157 struct hostapd_data *iface_hapd = iface->bss[j];
1158 if (hostapd_enable_iface(iface_hapd->iface) < 0) {
1159 wpa_printf(
1160 MSG_ERROR, "Enabling interface %s failed on %zu",
1161 iface_params.name.c_str(), i);
1162 return createStatus(HostapdStatusCode::FAILURE_UNKNOWN);
1163 }
1164 }
1165 }
1166 }
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001167 // Save bridge interface info
Les Lee399c6302024-09-12 03:03:38 +00001168 br_interfaces_[br_name] = managed_instances;
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001169 return ndk::ScopedAStatus::ok();
1170}
1171
Les Lee399c6302024-09-12 03:03:38 +00001172struct hostapd_data * hostapd_get_iface_by_link_id(struct hapd_interfaces *interfaces,
1173 const size_t link_id)
1174{
1175#ifdef CONFIG_IEEE80211BE
1176 size_t i, j;
1177
1178 for (i = 0; i < interfaces->count; i++) {
1179 struct hostapd_iface *iface = interfaces->iface[i];
1180
1181 for (j = 0; j < iface->num_bss; j++) {
1182 struct hostapd_data *hapd = iface->bss[j];
1183
1184 if (link_id == hapd->mld_link_id)
1185 return hapd;
1186 }
1187 }
1188#endif
1189 return NULL;
1190}
1191
1192// Both of bridged dual APs and MLO AP will be treated as concurrenct APs.
1193// -----------------------------------------
1194// | br_name | iface_params.name
1195// _______________________________________________________________
1196// bridged dual APs | bridged interface name | interface name
1197// _______________________________________________________________
1198// MLO AP | AP interface name | mld link id as instance name
1199// _______________________________________________________________
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001200::ndk::ScopedAStatus Hostapd::addSingleAccessPoint(
1201 const IfaceParams& iface_params,
1202 const ChannelParams& channelParams,
1203 const NetworkParams& nw_params,
Purushottam Kushwaha0316c882021-12-20 15:07:44 +05301204 const std::string br_name,
1205 const std::string owe_transition_ifname)
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001206{
Les Lee399c6302024-09-12 03:03:38 +00001207 if (iface_params.usesMlo) { // the mlo case, iface name is instance name which is mld_link_id
1208 if (hostapd_get_iface_by_link_id(interfaces_, (size_t) iface_params.name.c_str())) {
1209 wpa_printf(
1210 MSG_ERROR, "Instance link id %s already present",
1211 iface_params.name.c_str());
1212 return createStatus(HostapdStatusCode::FAILURE_IFACE_EXISTS);
1213 }
1214 }
1215 if (hostapd_get_iface(interfaces_,
1216 iface_params.usesMlo ? br_name.c_str() : iface_params.name.c_str())) {
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001217 wpa_printf(
Les Lee399c6302024-09-12 03:03:38 +00001218 MSG_ERROR, "Instance interface %s already present",
1219 iface_params.usesMlo ? br_name.c_str() : iface_params.name.c_str());
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001220 return createStatus(HostapdStatusCode::FAILURE_IFACE_EXISTS);
1221 }
Purushottam Kushwaha0316c882021-12-20 15:07:44 +05301222 const auto conf_params = CreateHostapdConfig(iface_params, channelParams, nw_params,
1223 br_name, owe_transition_ifname);
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001224 if (conf_params.empty()) {
1225 wpa_printf(MSG_ERROR, "Failed to create config params");
1226 return createStatus(HostapdStatusCode::FAILURE_ARGS_INVALID);
1227 }
1228 const auto conf_file_path =
Les Lee399c6302024-09-12 03:03:38 +00001229 WriteHostapdConfig(iface_params.name, conf_params, br_name, iface_params.usesMlo);
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001230 if (conf_file_path.empty()) {
1231 wpa_printf(MSG_ERROR, "Failed to write config file");
1232 return createStatus(HostapdStatusCode::FAILURE_UNKNOWN);
1233 }
1234 std::string add_iface_param_str = StringPrintf(
Les Lee399c6302024-09-12 03:03:38 +00001235 "%s config=%s", iface_params.usesMlo ? br_name.c_str(): iface_params.name.c_str(),
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001236 conf_file_path.c_str());
1237 std::vector<char> add_iface_param_vec(
1238 add_iface_param_str.begin(), add_iface_param_str.end() + 1);
1239 if (hostapd_add_iface(interfaces_, add_iface_param_vec.data()) < 0) {
1240 wpa_printf(
Les Lee399c6302024-09-12 03:03:38 +00001241 MSG_ERROR, "Adding hostapd iface %s failed",
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001242 add_iface_param_str.c_str());
1243 return createStatus(HostapdStatusCode::FAILURE_UNKNOWN);
1244 }
Les Lee399c6302024-09-12 03:03:38 +00001245
1246 // find the iface and set up callback.
1247 struct hostapd_data* iface_hapd = iface_params.usesMlo ?
1248 hostapd_get_iface_by_link_id(interfaces_, (size_t) iface_params.name.c_str()) :
1249 hostapd_get_iface(interfaces_, iface_params.name.c_str());
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001250 WPA_ASSERT(iface_hapd != nullptr && iface_hapd->iface != nullptr);
Les Lee399c6302024-09-12 03:03:38 +00001251 if (iface_params.usesMlo) {
1252 memcmp(iface_hapd->conf->iface, br_name.c_str(), br_name.size());
1253 }
1254
1255 // Callback discrepancy between bridged dual APs and MLO AP
1256 // Note: Only bridged dual APs will have "iface_hapd->conf->bridge" and
1257 // Only MLO AP will have "iface_hapd->mld_link_id"
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001258 // Register the setup complete callbacks
Les Lee399c6302024-09-12 03:03:38 +00001259 // -----------------------------------------
1260 // | bridged dual APs | bridged single link MLO | MLO SAP
1261 // _________________________________________________________________________________________
1262 // hapd->conf->bridge | bridged interface name | bridged interface nam | N/A
1263 // _________________________________________________________________________________________
1264 // hapd->conf->iface | AP interface name | AP interface name | AP interface name
1265 // _________________________________________________________________________________________
1266 // hapd->mld_link_id | 0 (default value) | link id (0) | link id (0 or 1)
1267 // _________________________________________________________________________________________
1268 // hapd->mld_ap | 0 | 1 | 1
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001269 on_setup_complete_internal_callback =
1270 [this](struct hostapd_data* iface_hapd) {
1271 wpa_printf(
1272 MSG_INFO, "AP interface setup completed - state %s",
1273 hostapd_state_text(iface_hapd->iface->state));
1274 if (iface_hapd->iface->state == HAPD_IFACE_DISABLED) {
1275 // Invoke the failure callback on all registered
1276 // clients.
Les Lee399c6302024-09-12 03:03:38 +00001277 std::string instanceName = iface_hapd->conf->iface;
1278#ifdef CONFIG_IEEE80211BE
1279 if (iface_hapd->conf->mld_ap
1280 && strlen(iface_hapd->conf->bridge) == 0) {
1281 instanceName = std::to_string(iface_hapd->mld_link_id);
1282 }
1283#endif
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001284 for (const auto& callback : callbacks_) {
Chenming Huangbaa5e582024-04-02 08:21:10 +05301285 auto status = callback->onFailure(
1286 strlen(iface_hapd->conf->bridge) > 0 ?
Les Leee08c2862021-10-29 16:36:41 +08001287 iface_hapd->conf->bridge : iface_hapd->conf->iface,
Les Lee399c6302024-09-12 03:03:38 +00001288 instanceName);
Chenming Huangbaa5e582024-04-02 08:21:10 +05301289 if (!status.isOk()) {
1290 wpa_printf(MSG_ERROR, "Failed to invoke onFailure");
1291 }
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001292 }
1293 }
1294 };
1295
1296 // Register for new client connect/disconnect indication.
1297 on_sta_authorized_internal_callback =
1298 [this](struct hostapd_data* iface_hapd, const u8 *mac_addr,
1299 int authorized, const u8 *p2p_dev_addr) {
1300 wpa_printf(MSG_DEBUG, "notify client " MACSTR " %s",
1301 MAC2STR(mac_addr),
1302 (authorized) ? "Connected" : "Disconnected");
1303 ClientInfo info;
1304 info.ifaceName = strlen(iface_hapd->conf->bridge) > 0 ?
1305 iface_hapd->conf->bridge : iface_hapd->conf->iface;
Les Lee399c6302024-09-12 03:03:38 +00001306 std::string instanceName = iface_hapd->conf->iface;
1307#ifdef CONFIG_IEEE80211BE
1308 if (iface_hapd->conf->mld_ap
1309 && strlen(iface_hapd->conf->bridge) == 0) {
1310 instanceName = std::to_string(iface_hapd->mld_link_id);
1311 }
1312#endif
1313 info.apIfaceInstance = instanceName;
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001314 info.clientAddress.assign(mac_addr, mac_addr + ETH_ALEN);
1315 info.isConnected = authorized;
Chris Weir808564b2024-08-07 10:12:16 -07001316 if(isAidlServiceVersionAtLeast(3) && !authorized) {
1317 u16 disconnect_reason_code = WLAN_REASON_UNSPECIFIED;
1318 auto sta_ptr_optional = getStaInfoByMacAddr(iface_hapd, mac_addr);
1319 if (sta_ptr_optional.has_value()){
1320 disconnect_reason_code = sta_ptr_optional.value()->deauth_reason;
1321 }
1322 info.disconnectReasonCode =
1323 static_cast<common::DeauthenticationReasonCode>(disconnect_reason_code);
1324 }
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001325 for (const auto &callback : callbacks_) {
Chenming Huangbaa5e582024-04-02 08:21:10 +05301326 auto status = callback->onConnectedClientsChanged(info);
1327 if (!status.isOk()) {
1328 wpa_printf(MSG_ERROR, "Failed to invoke onConnectedClientsChanged");
1329 }
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001330 }
1331 };
1332
1333 // Register for wpa_event which used to get channel switch event
1334 on_wpa_msg_internal_callback =
1335 [this](struct hostapd_data* iface_hapd, int level,
1336 enum wpa_msg_type type, const char *txt,
1337 size_t len) {
1338 wpa_printf(MSG_DEBUG, "Receive wpa msg : %s", txt);
1339 if (os_strncmp(txt, AP_EVENT_ENABLED,
1340 strlen(AP_EVENT_ENABLED)) == 0 ||
1341 os_strncmp(txt, WPA_EVENT_CHANNEL_SWITCH,
1342 strlen(WPA_EVENT_CHANNEL_SWITCH)) == 0) {
Les Lee399c6302024-09-12 03:03:38 +00001343 std::string instanceName = iface_hapd->conf->iface;
1344#ifdef CONFIG_IEEE80211BE
1345 if (iface_hapd->conf->mld_ap && strlen(iface_hapd->conf->bridge) == 0) {
1346 instanceName = std::to_string(iface_hapd->mld_link_id);
1347 }
1348#endif
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001349 ApInfo info;
1350 info.ifaceName = strlen(iface_hapd->conf->bridge) > 0 ?
1351 iface_hapd->conf->bridge : iface_hapd->conf->iface,
Les Lee399c6302024-09-12 03:03:38 +00001352 info.apIfaceInstance = instanceName;
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001353 info.freqMhz = iface_hapd->iface->freq;
Ahmed ElArabawyb4115792022-02-08 09:33:01 -08001354 info.channelBandwidth = getChannelBandwidth(iface_hapd->iconf);
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001355 info.generation = getGeneration(iface_hapd->iface->current_mode);
1356 info.apIfaceInstanceMacAddress.assign(iface_hapd->own_addr,
1357 iface_hapd->own_addr + ETH_ALEN);
1358 for (const auto &callback : callbacks_) {
Chenming Huangbaa5e582024-04-02 08:21:10 +05301359 auto status = callback->onApInstanceInfoChanged(info);
1360 if (!status.isOk()) {
1361 wpa_printf(MSG_ERROR,
1362 "Failed to invoke onApInstanceInfoChanged");
1363 }
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001364 }
Les Leea0c90cb2022-04-19 17:39:23 +08001365 } else if (os_strncmp(txt, AP_EVENT_DISABLED, strlen(AP_EVENT_DISABLED)) == 0
1366 || os_strncmp(txt, INTERFACE_DISABLED, strlen(INTERFACE_DISABLED)) == 0)
1367 {
Les Lee399c6302024-09-12 03:03:38 +00001368 std::string instanceName = iface_hapd->conf->iface;
1369#ifdef CONFIG_IEEE80211BE
1370 if (iface_hapd->conf->mld_ap && strlen(iface_hapd->conf->bridge) == 0) {
1371 instanceName = std::to_string(iface_hapd->mld_link_id);
1372 }
1373#endif
Yu Ouyang378d3c42021-08-20 17:31:08 +08001374 // Invoke the failure callback on all registered clients.
1375 for (const auto& callback : callbacks_) {
Les Lee399c6302024-09-12 03:03:38 +00001376 auto status =
1377 callback->onFailure(strlen(iface_hapd->conf->bridge) > 0 ?
Les Leee08c2862021-10-29 16:36:41 +08001378 iface_hapd->conf->bridge : iface_hapd->conf->iface,
Les Lee399c6302024-09-12 03:03:38 +00001379 instanceName);
Chenming Huangbaa5e582024-04-02 08:21:10 +05301380 if (!status.isOk()) {
1381 wpa_printf(MSG_ERROR, "Failed to invoke onFailure");
1382 }
Yu Ouyang378d3c42021-08-20 17:31:08 +08001383 }
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001384 }
Yu Ouyang378d3c42021-08-20 17:31:08 +08001385 };
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001386
1387 // Setup callback
1388 iface_hapd->setup_complete_cb = onAsyncSetupCompleteCb;
1389 iface_hapd->setup_complete_cb_ctx = iface_hapd;
1390 iface_hapd->sta_authorized_cb = onAsyncStaAuthorizedCb;
1391 iface_hapd->sta_authorized_cb_ctx = iface_hapd;
Hu Wang7c5a4322021-06-24 17:24:59 +08001392 wpa_msg_register_aidl_cb(onAsyncWpaEventCb);
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001393
Les Lee399c6302024-09-12 03:03:38 +00001394 // Multi-link MLO should enable iface after both links have been set.
1395 if (!iface_params.usesMlo && hostapd_enable_iface(iface_hapd->iface) < 0) {
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001396 wpa_printf(
1397 MSG_ERROR, "Enabling interface %s failed",
1398 iface_params.name.c_str());
1399 return createStatus(HostapdStatusCode::FAILURE_UNKNOWN);
1400 }
1401 return ndk::ScopedAStatus::ok();
1402}
1403
1404::ndk::ScopedAStatus Hostapd::removeAccessPointInternal(const std::string& iface_name)
1405{
1406 // interfaces to be removed
1407 std::vector<std::string> interfaces;
1408 bool is_error = false;
1409
1410 const auto it = br_interfaces_.find(iface_name);
1411 if (it != br_interfaces_.end()) {
1412 // In case bridge, remove managed interfaces
1413 interfaces = it->second;
1414 br_interfaces_.erase(iface_name);
1415 } else {
1416 // else remove current interface
1417 interfaces.push_back(iface_name);
1418 }
1419
1420 for (auto& iface : interfaces) {
1421 std::vector<char> remove_iface_param_vec(
1422 iface.begin(), iface.end() + 1);
1423 if (hostapd_remove_iface(interfaces_, remove_iface_param_vec.data()) < 0) {
1424 wpa_printf(MSG_INFO, "Remove interface %s failed", iface.c_str());
1425 is_error = true;
1426 }
1427 }
1428 if (is_error) {
1429 return createStatus(HostapdStatusCode::FAILURE_UNKNOWN);
1430 }
1431 return ndk::ScopedAStatus::ok();
1432}
1433
1434::ndk::ScopedAStatus Hostapd::registerCallbackInternal(
1435 const std::shared_ptr<IHostapdCallback>& callback)
1436{
1437 binder_status_t status = AIBinder_linkToDeath(callback->asBinder().get(),
1438 death_notifier_, this /* cookie */);
1439 if (status != STATUS_OK) {
1440 wpa_printf(
1441 MSG_ERROR,
1442 "Error registering for death notification for "
1443 "hostapd callback object");
1444 return createStatus(HostapdStatusCode::FAILURE_UNKNOWN);
1445 }
1446 callbacks_.push_back(callback);
Manaswini Paluri76ae6982024-02-29 14:49:20 +05301447 if (aidl_service_version == 0) {
1448 aidl_service_version = Hostapd::version;
1449 wpa_printf(MSG_INFO, "AIDL service version: %d", aidl_service_version);
1450 }
1451 if (aidl_client_version == 0) {
1452 callback->getInterfaceVersion(&aidl_client_version);
1453 wpa_printf(MSG_INFO, "AIDL client version: %d", aidl_client_version);
1454 }
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001455 return ndk::ScopedAStatus::ok();
1456}
1457
1458::ndk::ScopedAStatus Hostapd::forceClientDisconnectInternal(const std::string& iface_name,
1459 const std::vector<uint8_t>& client_address, Ieee80211ReasonCode reason_code)
1460{
1461 struct hostapd_data *hapd = hostapd_get_iface(interfaces_, iface_name.c_str());
1462 bool result;
1463 if (!hapd) {
1464 for (auto const& iface : br_interfaces_) {
1465 if (iface.first == iface_name) {
1466 for (auto const& instance : iface.second) {
1467 hapd = hostapd_get_iface(interfaces_, instance.c_str());
1468 if (hapd) {
1469 result = forceStaDisconnection(hapd, client_address,
1470 (uint16_t) reason_code);
1471 if (result) break;
1472 }
1473 }
1474 }
1475 }
1476 } else {
1477 result = forceStaDisconnection(hapd, client_address, (uint16_t) reason_code);
1478 }
1479 if (!hapd) {
1480 wpa_printf(MSG_ERROR, "Interface %s doesn't exist", iface_name.c_str());
1481 return createStatus(HostapdStatusCode::FAILURE_IFACE_UNKNOWN);
1482 }
1483 if (result) {
1484 return ndk::ScopedAStatus::ok();
1485 }
1486 return createStatus(HostapdStatusCode::FAILURE_CLIENT_UNKNOWN);
1487}
1488
1489::ndk::ScopedAStatus Hostapd::setDebugParamsInternal(DebugLevel level)
1490{
1491 wpa_debug_level = static_cast<uint32_t>(level);
1492 return ndk::ScopedAStatus::ok();
1493}
1494
1495} // namespace hostapd
1496} // namespace wifi
1497} // namespace hardware
1498} // namespace android
Les Leee08c2862021-10-29 16:36:41 +08001499} // namespace aidl