1 /*
2 * aidl interface for wpa_hostapd daemon
3 * Copyright (c) 2004-2018, Jouni Malinen <[email protected]>
4 * Copyright (c) 2004-2018, Roshan Pius <[email protected]>
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
32 extern "C"
33 {
34 #include "common/wpa_ctrl.h"
35 #include "drivers/linux_ioctl.h"
36 }
37
38
39 #ifdef ANDROID_HOSTAPD_UNITTEST
40 #include "tests/unittest_overrides.h"
41 #endif
42
43 // The AIDL implementation for hostapd creates a hostapd.conf dynamically for
44 // each interface. This file can then be used to hook onto the normal config
45 // file parsing logic in hostapd code. Helps us to avoid duplication of code
46 // in the AIDL interface.
47 // TOOD(b/71872409): Add unit tests for this.
48 namespace {
49 constexpr char kConfFileNameFmt[] = "/data/vendor/wifi/hostapd/hostapd_%s.conf";
50
51 /**
52 * To add an overlay file, add
53 *
54 * PRODUCT_COPY_FILES += \
55 * <your/path/here>/hostapd_unmetered_overlay.conf:/vendor/etc/wifi/hostapd_unmetered_overlay.conf
56 *
57 * to the build file for your device, with the <your/path/here> being the path to your overlay in
58 * your repo. See the resolveVendorConfPath function in this file for more specifics on where this
59 * overlay file will wind up on your device.
60 *
61 * This overlay may configure any of the parameters listed in kOverlayableKeys. The kOverlayableKeys
62 * list is subject to change over time, as certain parameters may be added as APIs instead in the
63 * future.
64 *
65 * Example of what an overlay file might look like:
66 * $> cat hostapd_unmetered_overlay.conf
67 * dtim_period=2
68 * ap_max_inactivity=300
69 *
70 * Anything added to this overlay will be prepended to the hostapd.conf for unmetered (typically
71 * local only hotspots) interfaces.
72 */
73 constexpr char kUnmeteredIfaceOverlayPath[] = "/etc/wifi/hostapd_unmetered_overlay.conf";
74
75 /**
76 * Allow-list of hostapd.conf parameters (keys) that can be set via overlay.
77 *
78 * If introducing new APIs, be sure to remove keys from this list that would otherwise be
79 * controlled by the new API. This way we can avoid conflicting settings.
80 * Please file an FR to add new keys to this list.
81 */
82 static const std::set<std::string> kOverlayableKeys = {
83 "ap_max_inactivity",
84 "assocresp_elements"
85 "beacon_int",
86 "disassoc_low_ack",
87 "dtim_period",
88 "fragm_threshold",
89 "max_listen_interval",
90 "max_num_sta",
91 "rts_threshold",
92 "skip_inactivity_poll",
93 "uapsd_advertisement_enabled",
94 "wmm_enabled",
95 "wmm_ac_vo_aifs",
96 "wmm_ac_vo_cwmin",
97 "wmm_ac_vo_cwmax",
98 "wmm_ac_vo_txop_limit",
99 "wmm_ac_vo_acm",
100 "wmm_ac_vi_aifs",
101 "wmm_ac_vi_cwmin",
102 "wmm_ac_vi_cwmax",
103 "wmm_ac_vi_txop_limit",
104 "wmm_ac_vi_acm",
105 "wmm_ac_bk_cwmin"
106 "wmm_ac_bk_cwmax"
107 "wmm_ac_bk_aifs",
108 "wmm_ac_bk_txop_limit",
109 "wmm_ac_bk_acm",
110 "wmm_ac_be_aifs",
111 "wmm_ac_be_cwmin",
112 "wmm_ac_be_cwmax",
113 "wmm_ac_be_txop_limit",
114 "wmm_ac_be_acm",
115 };
116
117 using android::base::RemoveFileIfExists;
118 using android::base::StringPrintf;
119 #ifndef ANDROID_HOSTAPD_UNITTEST
120 using android::base::ReadFileToString;
121 using android::base::WriteStringToFile;
122 #endif
123 using aidl::android::hardware::wifi::hostapd::BandMask;
124 using aidl::android::hardware::wifi::hostapd::ChannelBandwidth;
125 using aidl::android::hardware::wifi::hostapd::ChannelParams;
126 using aidl::android::hardware::wifi::hostapd::EncryptionType;
127 using aidl::android::hardware::wifi::hostapd::Generation;
128 using aidl::android::hardware::wifi::hostapd::HostapdStatusCode;
129 using aidl::android::hardware::wifi::hostapd::IfaceParams;
130 using aidl::android::hardware::wifi::hostapd::NetworkParams;
131 using aidl::android::hardware::wifi::hostapd::ParamSizeLimits;
132
133 int band2Ghz = (int)BandMask::BAND_2_GHZ;
134 int band5Ghz = (int)BandMask::BAND_5_GHZ;
135 int band6Ghz = (int)BandMask::BAND_6_GHZ;
136 int band60Ghz = (int)BandMask::BAND_60_GHZ;
137
138 int32_t aidl_client_version = 0;
139 int32_t aidl_service_version = 0;
140
141 /**
142 * Check that the AIDL service is running at least the expected version.
143 * Use to avoid the case where the AIDL interface version
144 * is greater than the version implemented by the service.
145 */
isAidlServiceVersionAtLeast(int32_t expected_version)146 inline int32_t isAidlServiceVersionAtLeast(int32_t expected_version)
147 {
148 return expected_version <= aidl_service_version;
149 }
150
isAidlClientVersionAtLeast(int32_t expected_version)151 inline int32_t isAidlClientVersionAtLeast(int32_t expected_version)
152 {
153 return expected_version <= aidl_client_version;
154 }
155
areAidlServiceAndClientAtLeastVersion(int32_t expected_version)156 inline int32_t areAidlServiceAndClientAtLeastVersion(int32_t expected_version)
157 {
158 return isAidlServiceVersionAtLeast(expected_version)
159 && isAidlClientVersionAtLeast(expected_version);
160 }
161
162 #define MAX_PORTS 1024
GetInterfacesInBridge(std::string br_name,std::vector<std::string> * interfaces)163 bool GetInterfacesInBridge(std::string br_name,
164 std::vector<std::string>* interfaces) {
165 android::base::unique_fd sock(socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0));
166 if (sock.get() < 0) {
167 wpa_printf(MSG_ERROR, "Failed to create sock (%s) in %s",
168 strerror(errno), __FUNCTION__);
169 return false;
170 }
171
172 struct ifreq request;
173 int i, ifindices[MAX_PORTS];
174 char if_name[IFNAMSIZ];
175 unsigned long args[3];
176
177 memset(ifindices, 0, MAX_PORTS * sizeof(int));
178
179 args[0] = BRCTL_GET_PORT_LIST;
180 args[1] = (unsigned long) ifindices;
181 args[2] = MAX_PORTS;
182
183 strlcpy(request.ifr_name, br_name.c_str(), IFNAMSIZ);
184 request.ifr_data = (char *)args;
185
186 if (ioctl(sock.get(), SIOCDEVPRIVATE, &request) < 0) {
187 wpa_printf(MSG_ERROR, "Failed to ioctl SIOCDEVPRIVATE in %s",
188 __FUNCTION__);
189 return false;
190 }
191
192 for (i = 0; i < MAX_PORTS; i ++) {
193 memset(if_name, 0, IFNAMSIZ);
194 if (ifindices[i] == 0 || !if_indextoname(ifindices[i], if_name)) {
195 continue;
196 }
197 interfaces->push_back(if_name);
198 }
199 return true;
200 }
201
resolveVendorConfPath(const std::string & conf_path)202 std::string resolveVendorConfPath(const std::string& conf_path)
203 {
204 #if defined(__ANDROID_APEX__)
205 // returns "/apex/<apexname>" + conf_path
206 std::string path = android::base::GetExecutablePath();
207 return path.substr(0, path.find_first_of('/', strlen("/apex/"))) + conf_path;
208 #else
209 return std::string("/vendor") + conf_path;
210 #endif
211 }
212
logHostapdConfigError(int error,const std::string & file_path)213 void logHostapdConfigError(int error, const std::string& file_path) {
214 wpa_printf(MSG_ERROR, "Cannot read/write hostapd config %s, error: %s", file_path.c_str(),
215 strerror(error));
216 struct stat st;
217 int result = stat(file_path.c_str(), &st);
218 if (result == 0) {
219 wpa_printf(MSG_ERROR, "hostapd config file uid: %d, gid: %d, mode: %d",st.st_uid,
220 st.st_gid, st.st_mode);
221 } else {
222 wpa_printf(MSG_ERROR, "Error calling stat() on hostapd config file: %s",
223 strerror(errno));
224 }
225 }
226
WriteHostapdConfig(const std::string & instance_name,const std::string & config,const std::string br_name,const bool usesMlo)227 std::string WriteHostapdConfig(
228 const std::string& instance_name, const std::string& config,
229 const std::string br_name, const bool usesMlo)
230 {
231 std::string conf_name_as_string = instance_name;
232 if (usesMlo) {
233 conf_name_as_string = StringPrintf(
234 "%s-%s", br_name.c_str(), instance_name.c_str());
235 }
236 const std::string file_path =
237 StringPrintf(kConfFileNameFmt, conf_name_as_string.c_str());
238 if (WriteStringToFile(
239 config, file_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
240 getuid(), getgid())) {
241 return file_path;
242 }
243 // Diagnose failure
244 int error = errno;
245 logHostapdConfigError(errno, file_path);
246 return "";
247 }
248
249 /*
250 * Get the op_class for a channel/band
251 * The logic here is based on Table E-4 in the 802.11 Specification
252 */
getOpClassForChannel(int channel,int band,bool support11n,bool support11ac)253 int getOpClassForChannel(int channel, int band, bool support11n, bool support11ac) {
254 // 2GHz Band
255 if ((band & band2Ghz) != 0) {
256 if (channel == 14) {
257 return 82;
258 }
259 if (channel >= 1 && channel <= 13) {
260 if (!support11n) {
261 //20MHz channel
262 return 81;
263 }
264 if (channel <= 9) {
265 // HT40 with secondary channel above primary
266 return 83;
267 }
268 // HT40 with secondary channel below primary
269 return 84;
270 }
271 // Error
272 return 0;
273 }
274
275 // 5GHz Band
276 if ((band & band5Ghz) != 0) {
277 if (support11ac) {
278 switch (channel) {
279 case 42:
280 case 58:
281 case 106:
282 case 122:
283 case 138:
284 case 155:
285 // 80MHz channel
286 return 128;
287 case 50:
288 case 114:
289 // 160MHz channel
290 return 129;
291 }
292 }
293
294 if (!support11n) {
295 if (channel >= 36 && channel <= 48) {
296 return 115;
297 }
298 if (channel >= 52 && channel <= 64) {
299 return 118;
300 }
301 if (channel >= 100 && channel <= 144) {
302 return 121;
303 }
304 if (channel >= 149 && channel <= 161) {
305 return 124;
306 }
307 if (channel >= 165 && channel <= 169) {
308 return 125;
309 }
310 } else {
311 switch (channel) {
312 case 36:
313 case 44:
314 // HT40 with secondary channel above primary
315 return 116;
316 case 40:
317 case 48:
318 // HT40 with secondary channel below primary
319 return 117;
320 case 52:
321 case 60:
322 // HT40 with secondary channel above primary
323 return 119;
324 case 56:
325 case 64:
326 // HT40 with secondary channel below primary
327 return 120;
328 case 100:
329 case 108:
330 case 116:
331 case 124:
332 case 132:
333 case 140:
334 // HT40 with secondary channel above primary
335 return 122;
336 case 104:
337 case 112:
338 case 120:
339 case 128:
340 case 136:
341 case 144:
342 // HT40 with secondary channel below primary
343 return 123;
344 case 149:
345 case 157:
346 // HT40 with secondary channel above primary
347 return 126;
348 case 153:
349 case 161:
350 // HT40 with secondary channel below primary
351 return 127;
352 }
353 }
354 // Error
355 return 0;
356 }
357
358 // 6GHz Band
359 if ((band & band6Ghz) != 0) {
360 // Channels 1, 5. 9, 13, ...
361 if ((channel & 0x03) == 0x01) {
362 // 20MHz channel
363 return 131;
364 }
365 // Channels 3, 11, 19, 27, ...
366 if ((channel & 0x07) == 0x03) {
367 // 40MHz channel
368 return 132;
369 }
370 // Channels 7, 23, 39, 55, ...
371 if ((channel & 0x0F) == 0x07) {
372 // 80MHz channel
373 return 133;
374 }
375 // Channels 15, 47, 69, ...
376 if ((channel & 0x1F) == 0x0F) {
377 // 160MHz channel
378 return 134;
379 }
380 if (channel == 2) {
381 // 20MHz channel
382 return 136;
383 }
384 // Error
385 return 0;
386 }
387
388 if ((band & band60Ghz) != 0) {
389 if (1 <= channel && channel <= 8) {
390 return 180;
391 } else if (9 <= channel && channel <= 15) {
392 return 181;
393 } else if (17 <= channel && channel <= 22) {
394 return 182;
395 } else if (25 <= channel && channel <= 29) {
396 return 183;
397 }
398 // Error
399 return 0;
400 }
401
402 return 0;
403 }
404
validatePassphrase(int passphrase_len,int min_len,int max_len)405 bool validatePassphrase(int passphrase_len, int min_len, int max_len)
406 {
407 if (min_len != -1 && passphrase_len < min_len) return false;
408 if (max_len != -1 && passphrase_len > max_len) return false;
409 return true;
410 }
411
getInterfaceMacAddress(const std::string & if_name)412 std::string getInterfaceMacAddress(const std::string& if_name)
413 {
414 u8 addr[ETH_ALEN] = {};
415 struct ifreq ifr;
416 std::string mac_addr;
417
418 android::base::unique_fd sock(socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0));
419 if (sock.get() < 0) {
420 wpa_printf(MSG_ERROR, "Failed to create sock (%s) in %s",
421 strerror(errno), __FUNCTION__);
422 return "";
423 }
424
425 memset(&ifr, 0, sizeof(ifr));
426 strlcpy(ifr.ifr_name, if_name.c_str(), IFNAMSIZ);
427 if (ioctl(sock.get(), SIOCGIFHWADDR, &ifr) < 0) {
428 wpa_printf(MSG_ERROR, "Could not get interface %s hwaddr: %s",
429 if_name.c_str(), strerror(errno));
430 return "";
431 }
432
433 memcpy(addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
434 mac_addr = StringPrintf("" MACSTR, MAC2STR(addr));
435
436 return mac_addr;
437 }
438
trimWhitespace(const std::string & str)439 std::string trimWhitespace(const std::string& str) {
440 size_t pos = 0;
441 size_t len = str.size();
442 for (pos; pos < str.size() && std::isspace(str[pos]); ++pos){}
443 for (len; len - 1 > 0 && std::isspace(str[len-1]); --len){}
444 return str.substr(pos, len);
445 }
446
CreateHostapdConfig(const IfaceParams & iface_params,const ChannelParams & channelParams,const NetworkParams & nw_params,const std::string br_name,const std::string owe_transition_ifname)447 std::string CreateHostapdConfig(
448 const IfaceParams& iface_params,
449 const ChannelParams& channelParams,
450 const NetworkParams& nw_params,
451 const std::string br_name,
452 const std::string owe_transition_ifname)
453 {
454 if (nw_params.ssid.size() >
455 static_cast<uint32_t>(
456 ParamSizeLimits::SSID_MAX_LEN_IN_BYTES)) {
457 wpa_printf(
458 MSG_ERROR, "Invalid SSID size: %zu", nw_params.ssid.size());
459 return "";
460 }
461
462 // SSID string
463 std::stringstream ss;
464 ss << std::hex;
465 ss << std::setfill('0');
466 for (uint8_t b : nw_params.ssid) {
467 ss << std::setw(2) << static_cast<unsigned int>(b);
468 }
469 const std::string ssid_as_string = ss.str();
470
471 // Encryption config string
472 uint32_t band = 0;
473 band |= static_cast<uint32_t>(channelParams.bandMask);
474 bool is_2Ghz_band_only = band == static_cast<uint32_t>(band2Ghz);
475 bool is_6Ghz_band_only = band == static_cast<uint32_t>(band6Ghz);
476 bool is_60Ghz_band_only = band == static_cast<uint32_t>(band60Ghz);
477 std::string encryption_config_as_string;
478 switch (nw_params.encryptionType) {
479 case EncryptionType::NONE:
480 // no security params
481 break;
482 case EncryptionType::WPA:
483 if (!validatePassphrase(
484 nw_params.passphrase.size(),
485 static_cast<uint32_t>(ParamSizeLimits::
486 WPA2_PSK_PASSPHRASE_MIN_LEN_IN_BYTES),
487 static_cast<uint32_t>(ParamSizeLimits::
488 WPA2_PSK_PASSPHRASE_MAX_LEN_IN_BYTES))) {
489 return "";
490 }
491 encryption_config_as_string = StringPrintf(
492 "wpa=3\n"
493 "wpa_pairwise=%s\n"
494 "wpa_passphrase=%s",
495 is_60Ghz_band_only ? "GCMP" : "TKIP CCMP",
496 nw_params.passphrase.c_str());
497 break;
498 case EncryptionType::WPA2:
499 if (!validatePassphrase(
500 nw_params.passphrase.size(),
501 static_cast<uint32_t>(ParamSizeLimits::
502 WPA2_PSK_PASSPHRASE_MIN_LEN_IN_BYTES),
503 static_cast<uint32_t>(ParamSizeLimits::
504 WPA2_PSK_PASSPHRASE_MAX_LEN_IN_BYTES))) {
505 return "";
506 }
507 encryption_config_as_string = StringPrintf(
508 "wpa=2\n"
509 "rsn_pairwise=%s\n"
510 #ifdef ENABLE_HOSTAPD_CONFIG_80211W_MFP_OPTIONAL
511 "ieee80211w=1\n"
512 #endif
513 "wpa_passphrase=%s",
514 is_60Ghz_band_only ? "GCMP" : "CCMP",
515 nw_params.passphrase.c_str());
516 break;
517 case EncryptionType::WPA3_SAE_TRANSITION:
518 if (!validatePassphrase(
519 nw_params.passphrase.size(),
520 static_cast<uint32_t>(ParamSizeLimits::
521 WPA2_PSK_PASSPHRASE_MIN_LEN_IN_BYTES),
522 static_cast<uint32_t>(ParamSizeLimits::
523 WPA2_PSK_PASSPHRASE_MAX_LEN_IN_BYTES))) {
524 return "";
525 }
526 // WPA3 transition mode or SAE+WPA_PSK key management(AKM) is not allowed in 6GHz.
527 // Auto-convert any such configurations to SAE.
528 if ((band & band6Ghz) != 0) {
529 wpa_printf(MSG_INFO, "WPA3_SAE_TRANSITION configured in 6GHz band."
530 "Enable only SAE in key_mgmt");
531 encryption_config_as_string = StringPrintf(
532 "wpa=2\n"
533 "rsn_pairwise=CCMP\n"
534 "wpa_key_mgmt=%s\n"
535 "ieee80211w=2\n"
536 "sae_require_mfp=2\n"
537 "sae_pwe=%d\n"
538 "sae_password=%s",
539 #ifdef CONFIG_IEEE80211BE
540 iface_params.hwModeParams.enable80211BE ?
541 "SAE SAE-EXT-KEY" : "SAE",
542 #else
543 "SAE",
544 #endif
545 is_6Ghz_band_only ? 1 : 2,
546 nw_params.passphrase.c_str());
547 } else {
548 encryption_config_as_string = StringPrintf(
549 "wpa=2\n"
550 "rsn_pairwise=%s\n"
551 "wpa_key_mgmt=%s\n"
552 "ieee80211w=1\n"
553 "sae_require_mfp=1\n"
554 "wpa_passphrase=%s\n"
555 "sae_password=%s",
556 is_60Ghz_band_only ? "GCMP" : "CCMP",
557 #ifdef CONFIG_IEEE80211BE
558 iface_params.hwModeParams.enable80211BE ?
559 "WPA-PSK SAE SAE-EXT-KEY" : "WPA-PSK SAE",
560 #else
561 "WPA-PSK SAE",
562 #endif
563 nw_params.passphrase.c_str(),
564 nw_params.passphrase.c_str());
565 }
566 break;
567 case EncryptionType::WPA3_SAE:
568 if (!validatePassphrase(nw_params.passphrase.size(), 1, -1)) {
569 return "";
570 }
571 encryption_config_as_string = StringPrintf(
572 "wpa=2\n"
573 "rsn_pairwise=%s\n"
574 "wpa_key_mgmt=%s\n"
575 "ieee80211w=2\n"
576 "sae_require_mfp=2\n"
577 "sae_pwe=%d\n"
578 "sae_password=%s",
579 is_60Ghz_band_only ? "GCMP" : "CCMP",
580 #ifdef CONFIG_IEEE80211BE
581 iface_params.hwModeParams.enable80211BE ? "SAE SAE-EXT-KEY" : "SAE",
582 #else
583 "SAE",
584 #endif
585 is_6Ghz_band_only ? 1 : 2,
586 nw_params.passphrase.c_str());
587 break;
588 case EncryptionType::WPA3_OWE_TRANSITION:
589 encryption_config_as_string = StringPrintf(
590 "wpa=2\n"
591 "rsn_pairwise=%s\n"
592 "wpa_key_mgmt=OWE\n"
593 "ieee80211w=2",
594 is_60Ghz_band_only ? "GCMP" : "CCMP");
595 break;
596 case EncryptionType::WPA3_OWE:
597 encryption_config_as_string = StringPrintf(
598 "wpa=2\n"
599 "rsn_pairwise=%s\n"
600 "wpa_key_mgmt=OWE\n"
601 "ieee80211w=2",
602 is_60Ghz_band_only ? "GCMP" : "CCMP");
603 break;
604 default:
605 wpa_printf(MSG_ERROR, "Unknown encryption type");
606 return "";
607 }
608
609 std::string channel_config_as_string;
610 bool isFirst = true;
611 if (channelParams.enableAcs) {
612 std::string freqList_as_string;
613 for (const auto &range :
614 channelParams.acsChannelFreqRangesMhz) {
615 if (!isFirst) {
616 freqList_as_string += ",";
617 }
618 isFirst = false;
619
620 if (range.startMhz != range.endMhz) {
621 freqList_as_string +=
622 StringPrintf("%d-%d", range.startMhz, range.endMhz);
623 } else {
624 freqList_as_string += StringPrintf("%d", range.startMhz);
625 }
626 }
627 channel_config_as_string = StringPrintf(
628 "channel=0\n"
629 "acs_exclude_dfs=%d\n"
630 "freqlist=%s",
631 channelParams.acsShouldExcludeDfs,
632 freqList_as_string.c_str());
633 } else {
634 int op_class = getOpClassForChannel(
635 channelParams.channel,
636 band,
637 iface_params.hwModeParams.enable80211N,
638 iface_params.hwModeParams.enable80211AC);
639 channel_config_as_string = StringPrintf(
640 "channel=%d\n"
641 "op_class=%d",
642 channelParams.channel, op_class);
643 }
644
645 std::string hw_mode_as_string;
646 std::string enable_edmg_as_string;
647 std::string edmg_channel_as_string;
648 bool is_60Ghz_used = false;
649
650 if (((band & band60Ghz) != 0)) {
651 hw_mode_as_string = "hw_mode=ad";
652 if (iface_params.hwModeParams.enableEdmg) {
653 enable_edmg_as_string = "enable_edmg=1";
654 edmg_channel_as_string = StringPrintf(
655 "edmg_channel=%d",
656 channelParams.channel);
657 }
658 is_60Ghz_used = true;
659 } else if ((band & band2Ghz) != 0) {
660 if (((band & band5Ghz) != 0)
661 || ((band & band6Ghz) != 0)) {
662 hw_mode_as_string = "hw_mode=any";
663 } else {
664 hw_mode_as_string = "hw_mode=g";
665 }
666 } else if (((band & band5Ghz) != 0)
667 || ((band & band6Ghz) != 0)) {
668 hw_mode_as_string = "hw_mode=a";
669 } else {
670 wpa_printf(MSG_ERROR, "Invalid band");
671 return "";
672 }
673
674 std::string he_params_as_string;
675 #ifdef CONFIG_IEEE80211AX
676 if (iface_params.hwModeParams.enable80211AX && !is_60Ghz_used) {
677 he_params_as_string = StringPrintf(
678 "ieee80211ax=1\n"
679 "he_su_beamformer=%d\n"
680 "he_su_beamformee=%d\n"
681 "he_mu_beamformer=%d\n"
682 "he_twt_required=%d\n",
683 iface_params.hwModeParams.enableHeSingleUserBeamformer ? 1 : 0,
684 iface_params.hwModeParams.enableHeSingleUserBeamformee ? 1 : 0,
685 iface_params.hwModeParams.enableHeMultiUserBeamformer ? 1 : 0,
686 iface_params.hwModeParams.enableHeTargetWakeTime ? 1 : 0);
687 } else {
688 he_params_as_string = "ieee80211ax=0";
689 }
690 #endif /* CONFIG_IEEE80211AX */
691 std::string eht_params_as_string;
692 #ifdef CONFIG_IEEE80211BE
693 if (iface_params.hwModeParams.enable80211BE && !is_60Ghz_used) {
694 eht_params_as_string = "ieee80211be=1\n";
695 if (areAidlServiceAndClientAtLeastVersion(2)) {
696 std::string interface_mac_addr = getInterfaceMacAddress(
697 iface_params.usesMlo ? br_name : iface_params.name);
698 if (interface_mac_addr.empty()) {
699 wpa_printf(MSG_ERROR,
700 "Unable to set interface mac address as bssid for 11BE SAP");
701 return "";
702 }
703 if (iface_params.usesMlo) {
704 eht_params_as_string += StringPrintf(
705 "mld_addr=%s\n"
706 "mld_ap=1",
707 interface_mac_addr.c_str());
708 } else {
709 eht_params_as_string += StringPrintf(
710 "bssid=%s\n"
711 "mld_ap=1",
712 interface_mac_addr.c_str());
713 }
714 }
715 /* TODO set eht_su_beamformer, eht_su_beamformee, eht_mu_beamformer */
716 } else {
717 eht_params_as_string = "ieee80211be=0";
718 }
719 #endif /* CONFIG_IEEE80211BE */
720
721 std::string ht_cap_vht_oper_he_oper_eht_oper_chwidth_as_string;
722 switch (iface_params.hwModeParams.maximumChannelBandwidth) {
723 case ChannelBandwidth::BANDWIDTH_20:
724 ht_cap_vht_oper_he_oper_eht_oper_chwidth_as_string = StringPrintf(
725 #ifdef CONFIG_IEEE80211BE
726 "eht_oper_chwidth=0\n"
727 #endif /* CONFIG_IEEE80211BE */
728 #ifdef CONFIG_IEEE80211AX
729 "he_oper_chwidth=0\n"
730 #endif
731 "vht_oper_chwidth=0\n"
732 "%s", (band & band6Ghz) ? "op_class=131" : "");
733 break;
734 case ChannelBandwidth::BANDWIDTH_40:
735 ht_cap_vht_oper_he_oper_eht_oper_chwidth_as_string = StringPrintf(
736 "ht_capab=[HT40+]\n"
737 #ifdef CONFIG_IEEE80211BE
738 "eht_oper_chwidth=0\n"
739 #endif /* CONFIG_IEEE80211BE */
740 #ifdef CONFIG_IEEE80211AX
741 "he_oper_chwidth=0\n"
742 #endif
743 "vht_oper_chwidth=0\n"
744 "%s", (band & band6Ghz) ? "op_class=132" : "");
745 break;
746 case ChannelBandwidth::BANDWIDTH_80:
747 ht_cap_vht_oper_he_oper_eht_oper_chwidth_as_string = StringPrintf(
748 "ht_capab=[HT40+]\n"
749 #ifdef CONFIG_IEEE80211BE
750 "eht_oper_chwidth=%d\n"
751 #endif /* CONFIG_IEEE80211BE */
752 #ifdef CONFIG_IEEE80211AX
753 "he_oper_chwidth=%d\n"
754 #endif
755 "vht_oper_chwidth=%d\n"
756 "%s",
757 #ifdef CONFIG_IEEE80211BE
758 (iface_params.hwModeParams.enable80211BE && !is_60Ghz_used) ? 1 : 0,
759 #endif
760 #ifdef CONFIG_IEEE80211AX
761 (iface_params.hwModeParams.enable80211AX && !is_60Ghz_used) ? 1 : 0,
762 #endif
763 iface_params.hwModeParams.enable80211AC ? 1 : 0,
764 (band & band6Ghz) ? "op_class=133" : "");
765 break;
766 case ChannelBandwidth::BANDWIDTH_160:
767 ht_cap_vht_oper_he_oper_eht_oper_chwidth_as_string = StringPrintf(
768 "ht_capab=[HT40+]\n"
769 #ifdef CONFIG_IEEE80211BE
770 "eht_oper_chwidth=%d\n"
771 #endif /* CONFIG_IEEE80211BE */
772 #ifdef CONFIG_IEEE80211AX
773 "he_oper_chwidth=%d\n"
774 #endif
775 "vht_oper_chwidth=%d\n"
776 "%s",
777 #ifdef CONFIG_IEEE80211BE
778 (iface_params.hwModeParams.enable80211BE && !is_60Ghz_used) ? 2 : 0,
779 #endif
780 #ifdef CONFIG_IEEE80211AX
781 (iface_params.hwModeParams.enable80211AX && !is_60Ghz_used) ? 2 : 0,
782 #endif
783 iface_params.hwModeParams.enable80211AC ? 2 : 0,
784 (band & band6Ghz) ? "op_class=134" : "");
785 break;
786 default:
787 if (!is_2Ghz_band_only && !is_60Ghz_used) {
788 if (iface_params.hwModeParams.enable80211AC) {
789 ht_cap_vht_oper_he_oper_eht_oper_chwidth_as_string =
790 "ht_capab=[HT40+]\n"
791 "vht_oper_chwidth=1\n";
792 }
793 if (band & band6Ghz) {
794 #ifdef CONFIG_IEEE80211BE
795 if (iface_params.hwModeParams.enable80211BE)
796 ht_cap_vht_oper_he_oper_eht_oper_chwidth_as_string += "op_class=137\n";
797 else
798 ht_cap_vht_oper_he_oper_eht_oper_chwidth_as_string += "op_class=134\n";
799 #else /* CONFIG_IEEE80211BE */
800 ht_cap_vht_oper_he_oper_eht_oper_chwidth_as_string += "op_class=134\n";
801 #endif /* CONFIG_IEEE80211BE */
802 }
803 #ifdef CONFIG_IEEE80211AX
804 if (iface_params.hwModeParams.enable80211AX) {
805 ht_cap_vht_oper_he_oper_eht_oper_chwidth_as_string += "he_oper_chwidth=1\n";
806 }
807 #endif
808 #ifdef CONFIG_IEEE80211BE
809 if (iface_params.hwModeParams.enable80211BE) {
810 ht_cap_vht_oper_he_oper_eht_oper_chwidth_as_string += "eht_oper_chwidth=1";
811 }
812 #endif
813 }
814 break;
815 }
816
817 #ifdef CONFIG_INTERWORKING
818 std::string access_network_params_as_string;
819 if (nw_params.isMetered) {
820 access_network_params_as_string = StringPrintf(
821 "interworking=1\n"
822 "access_network_type=2\n"); // CHARGEABLE_PUBLIC_NETWORK
823 } else {
824 access_network_params_as_string = StringPrintf(
825 "interworking=0\n");
826 }
827 #endif /* CONFIG_INTERWORKING */
828
829 std::string bridge_as_string;
830 if (!br_name.empty() && !iface_params.usesMlo) {
831 bridge_as_string = StringPrintf("bridge=%s", br_name.c_str());
832 }
833
834 // vendor_elements string
835 std::string vendor_elements_as_string;
836 if (nw_params.vendorElements.size() > 0) {
837 std::stringstream ss;
838 ss << std::hex;
839 ss << std::setfill('0');
840 for (uint8_t b : nw_params.vendorElements) {
841 ss << std::setw(2) << static_cast<unsigned int>(b);
842 }
843 vendor_elements_as_string = StringPrintf("vendor_elements=%s", ss.str().c_str());
844 }
845
846 std::string owe_transition_ifname_as_string;
847 if (!owe_transition_ifname.empty()) {
848 owe_transition_ifname_as_string = StringPrintf(
849 "owe_transition_ifname=%s", owe_transition_ifname.c_str());
850 }
851
852 std::string ap_isolation_as_string = StringPrintf("ap_isolate=%s",
853 isAidlServiceVersionAtLeast(3) && nw_params.isClientIsolationEnabled ?
854 "1" : "0");
855
856 // Overlay for LOHS (unmetered SoftAP)
857 std::string overlay_path = resolveVendorConfPath(kUnmeteredIfaceOverlayPath);
858 std::string overlay_string;
859 if (!nw_params.isMetered
860 && 0 == access(overlay_path.c_str(), R_OK)
861 && !ReadFileToString(overlay_path, &overlay_string)) {
862 logHostapdConfigError(errno, overlay_path);
863 return "";
864 }
865 std::string sanitized_overlay = "";
866 std::istringstream overlay_stream(overlay_string);
867 for (std::string line; std::getline(overlay_stream, line);) {
868 std::string overlay_key = trimWhitespace(line.substr(0, line.find("=")));
869 if (kOverlayableKeys.contains(overlay_key)) {
870 sanitized_overlay.append(line + "\n");
871 }
872 }
873
874 return StringPrintf(
875 "%s\n"
876 "interface=%s\n"
877 "driver=nl80211\n"
878 "ctrl_interface=/data/vendor/wifi/hostapd/ctrl_%s\n"
879 // ssid2 signals to hostapd that the value is not a literal value
880 // for use as a SSID. In this case, we're giving it a hex
881 // std::string and hostapd needs to expect that.
882 "ssid2=%s\n"
883 "%s\n"
884 "ieee80211n=%d\n"
885 "ieee80211ac=%d\n"
886 "%s\n"
887 "%s\n"
888 "%s\n"
889 "%s\n"
890 "ignore_broadcast_ssid=%d\n"
891 "wowlan_triggers=any\n"
892 #ifdef CONFIG_INTERWORKING
893 "%s\n"
894 #endif /* CONFIG_INTERWORKING */
895 "%s\n"
896 "%s\n"
897 "%s\n"
898 "%s\n"
899 "%s\n"
900 "%s\n"
901 "%s\n",
902 sanitized_overlay.c_str(),
903 iface_params.usesMlo ? br_name.c_str() : iface_params.name.c_str(),
904 iface_params.name.c_str(),
905 ssid_as_string.c_str(),
906 channel_config_as_string.c_str(),
907 iface_params.hwModeParams.enable80211N ? 1 : 0,
908 iface_params.hwModeParams.enable80211AC ? 1 : 0,
909 he_params_as_string.c_str(),
910 eht_params_as_string.c_str(),
911 hw_mode_as_string.c_str(), ht_cap_vht_oper_he_oper_eht_oper_chwidth_as_string.c_str(),
912 nw_params.isHidden ? 1 : 0,
913 #ifdef CONFIG_INTERWORKING
914 access_network_params_as_string.c_str(),
915 #endif /* CONFIG_INTERWORKING */
916 encryption_config_as_string.c_str(),
917 bridge_as_string.c_str(),
918 owe_transition_ifname_as_string.c_str(),
919 enable_edmg_as_string.c_str(),
920 edmg_channel_as_string.c_str(),
921 vendor_elements_as_string.c_str(),
922 ap_isolation_as_string.c_str());
923 }
924
getGeneration(hostapd_hw_modes * current_mode)925 Generation getGeneration(hostapd_hw_modes *current_mode)
926 {
927 wpa_printf(MSG_DEBUG, "getGeneration hwmode=%d, ht_enabled=%d,"
928 " vht_enabled=%d, he_supported=%d",
929 current_mode->mode, current_mode->ht_capab != 0,
930 current_mode->vht_capab != 0, current_mode->he_capab->he_supported);
931 switch (current_mode->mode) {
932 case HOSTAPD_MODE_IEEE80211B:
933 return Generation::WIFI_STANDARD_LEGACY;
934 case HOSTAPD_MODE_IEEE80211G:
935 return current_mode->ht_capab == 0 ?
936 Generation::WIFI_STANDARD_LEGACY : Generation::WIFI_STANDARD_11N;
937 case HOSTAPD_MODE_IEEE80211A:
938 if (current_mode->he_capab->he_supported) {
939 return Generation::WIFI_STANDARD_11AX;
940 }
941 return current_mode->vht_capab == 0 ?
942 Generation::WIFI_STANDARD_11N : Generation::WIFI_STANDARD_11AC;
943 case HOSTAPD_MODE_IEEE80211AD:
944 return Generation::WIFI_STANDARD_11AD;
945 default:
946 return Generation::WIFI_STANDARD_UNKNOWN;
947 }
948 }
949
getChannelBandwidth(struct hostapd_config * iconf)950 ChannelBandwidth getChannelBandwidth(struct hostapd_config *iconf)
951 {
952 wpa_printf(MSG_DEBUG, "getChannelBandwidth %d, isHT=%d, isHT40=%d",
953 iconf->vht_oper_chwidth, iconf->ieee80211n,
954 iconf->secondary_channel);
955 switch (iconf->vht_oper_chwidth) {
956 case CONF_OPER_CHWIDTH_80MHZ:
957 return ChannelBandwidth::BANDWIDTH_80;
958 case CONF_OPER_CHWIDTH_80P80MHZ:
959 return ChannelBandwidth::BANDWIDTH_80P80;
960 break;
961 case CONF_OPER_CHWIDTH_160MHZ:
962 return ChannelBandwidth::BANDWIDTH_160;
963 break;
964 case CONF_OPER_CHWIDTH_USE_HT:
965 if (iconf->ieee80211n) {
966 return iconf->secondary_channel != 0 ?
967 ChannelBandwidth::BANDWIDTH_40 : ChannelBandwidth::BANDWIDTH_20;
968 }
969 return ChannelBandwidth::BANDWIDTH_20_NOHT;
970 case CONF_OPER_CHWIDTH_2160MHZ:
971 return ChannelBandwidth::BANDWIDTH_2160;
972 case CONF_OPER_CHWIDTH_4320MHZ:
973 return ChannelBandwidth::BANDWIDTH_4320;
974 case CONF_OPER_CHWIDTH_6480MHZ:
975 return ChannelBandwidth::BANDWIDTH_6480;
976 case CONF_OPER_CHWIDTH_8640MHZ:
977 return ChannelBandwidth::BANDWIDTH_8640;
978 default:
979 return ChannelBandwidth::BANDWIDTH_INVALID;
980 }
981 }
982
getStaInfoByMacAddr(const struct hostapd_data * iface_hapd,const u8 * mac_addr)983 std::optional<struct sta_info*> getStaInfoByMacAddr(const struct hostapd_data* iface_hapd,
984 const u8 *mac_addr) {
985 if (iface_hapd == nullptr || mac_addr == nullptr){
986 wpa_printf(MSG_ERROR, "nullptr passsed to getStaInfoByMacAddr!");
987 return std::nullopt;
988 }
989
990 for (struct sta_info* sta_ptr = iface_hapd->sta_list; sta_ptr; sta_ptr = sta_ptr->next) {
991 int res;
992 res = memcmp(sta_ptr->addr, mac_addr, ETH_ALEN);
993 if (res == 0) {
994 return sta_ptr;
995 }
996 }
997 return std::nullopt;
998 }
999
forceStaDisconnection(struct hostapd_data * hapd,const std::vector<uint8_t> & client_address,const uint16_t reason_code)1000 bool forceStaDisconnection(struct hostapd_data* hapd,
1001 const std::vector<uint8_t>& client_address,
1002 const uint16_t reason_code) {
1003 if (client_address.size() != ETH_ALEN) {
1004 return false;
1005 }
1006
1007 auto sta_ptr_optional = getStaInfoByMacAddr(hapd, client_address.data());
1008 if (sta_ptr_optional.has_value()) {
1009 wpa_printf(MSG_INFO, "Force client:" MACSTR " disconnect with reason: %d",
1010 MAC2STR(client_address.data()), reason_code);
1011 ap_sta_disconnect(hapd, sta_ptr_optional.value(), sta_ptr_optional.value()->addr,
1012 reason_code);
1013 return true;
1014 }
1015
1016 return false;
1017 }
1018
1019 // hostapd core functions accept "C" style function pointers, so use global
1020 // functions to pass to the hostapd core function and store the corresponding
1021 // std::function methods to be invoked.
1022 //
1023 // NOTE: Using the pattern from the vendor HAL (wifi_legacy_hal.cpp).
1024 //
1025 // Callback to be invoked once setup is complete
1026 std::function<void(struct hostapd_data*)> on_setup_complete_internal_callback;
onAsyncSetupCompleteCb(void * ctx)1027 void onAsyncSetupCompleteCb(void* ctx)
1028 {
1029 struct hostapd_data* iface_hapd = (struct hostapd_data*)ctx;
1030 if (on_setup_complete_internal_callback) {
1031 on_setup_complete_internal_callback(iface_hapd);
1032 // Invalidate this callback since we don't want this firing
1033 // again in single AP mode.
1034 if (strlen(iface_hapd->conf->bridge) > 0) {
1035 on_setup_complete_internal_callback = nullptr;
1036 }
1037 }
1038 }
1039
1040 // Callback to be invoked on hotspot client connection/disconnection
1041 std::function<void(struct hostapd_data*, const u8 *mac_addr, int authorized,
1042 const u8 *p2p_dev_addr)> on_sta_authorized_internal_callback;
onAsyncStaAuthorizedCb(void * ctx,const u8 * mac_addr,int authorized,const u8 * p2p_dev_addr,const u8 * ip)1043 void onAsyncStaAuthorizedCb(void* ctx, const u8 *mac_addr, int authorized,
1044 const u8 *p2p_dev_addr, const u8 *ip)
1045 {
1046 struct hostapd_data* iface_hapd = (struct hostapd_data*)ctx;
1047 if (on_sta_authorized_internal_callback) {
1048 on_sta_authorized_internal_callback(iface_hapd, mac_addr,
1049 authorized, p2p_dev_addr);
1050 }
1051 }
1052
1053 std::function<void(struct hostapd_data*, int level,
1054 enum wpa_msg_type type, const char *txt,
1055 size_t len)> on_wpa_msg_internal_callback;
1056
onAsyncWpaEventCb(void * ctx,int level,enum wpa_msg_type type,const char * txt,size_t len)1057 void onAsyncWpaEventCb(void *ctx, int level,
1058 enum wpa_msg_type type, const char *txt,
1059 size_t len)
1060 {
1061 struct hostapd_data* iface_hapd = (struct hostapd_data*)ctx;
1062 if (on_wpa_msg_internal_callback) {
1063 on_wpa_msg_internal_callback(iface_hapd, level,
1064 type, txt, len);
1065 }
1066 }
1067
createStatus(HostapdStatusCode status_code)1068 inline ndk::ScopedAStatus createStatus(HostapdStatusCode status_code) {
1069 return ndk::ScopedAStatus::fromServiceSpecificError(
1070 static_cast<int32_t>(status_code));
1071 }
1072
createStatusWithMsg(HostapdStatusCode status_code,std::string msg)1073 inline ndk::ScopedAStatus createStatusWithMsg(
1074 HostapdStatusCode status_code, std::string msg)
1075 {
1076 return ndk::ScopedAStatus::fromServiceSpecificErrorWithMessage(
1077 static_cast<int32_t>(status_code), msg.c_str());
1078 }
1079
1080 // Method called by death_notifier_ on client death.
onDeath(void * cookie)1081 void onDeath(void* cookie) {
1082 wpa_printf(MSG_ERROR, "Client died. Terminating...");
1083 eloop_terminate();
1084 }
1085
1086 } // namespace
1087
1088 namespace aidl {
1089 namespace android {
1090 namespace hardware {
1091 namespace wifi {
1092 namespace hostapd {
1093
Hostapd(struct hapd_interfaces * interfaces)1094 Hostapd::Hostapd(struct hapd_interfaces* interfaces)
1095 : interfaces_(interfaces)
1096 {
1097 death_notifier_ = AIBinder_DeathRecipient_new(onDeath);
1098 }
1099
addAccessPoint(const IfaceParams & iface_params,const NetworkParams & nw_params)1100 ::ndk::ScopedAStatus Hostapd::addAccessPoint(
1101 const IfaceParams& iface_params, const NetworkParams& nw_params)
1102 {
1103 return addAccessPointInternal(iface_params, nw_params);
1104 }
1105
removeAccessPoint(const std::string & iface_name)1106 ::ndk::ScopedAStatus Hostapd::removeAccessPoint(const std::string& iface_name)
1107 {
1108 return removeAccessPointInternal(iface_name);
1109 }
1110
terminate()1111 ::ndk::ScopedAStatus Hostapd::terminate()
1112 {
1113 wpa_printf(MSG_INFO, "Terminating...");
1114 // Clear the callback to avoid IPCThreadState shutdown during the
1115 // callback event.
1116 callbacks_.clear();
1117 eloop_terminate();
1118 return ndk::ScopedAStatus::ok();
1119 }
1120
registerCallback(const std::shared_ptr<IHostapdCallback> & callback)1121 ::ndk::ScopedAStatus Hostapd::registerCallback(
1122 const std::shared_ptr<IHostapdCallback>& callback)
1123 {
1124 return registerCallbackInternal(callback);
1125 }
1126
forceClientDisconnect(const std::string & iface_name,const std::vector<uint8_t> & client_address,Ieee80211ReasonCode reason_code)1127 ::ndk::ScopedAStatus Hostapd::forceClientDisconnect(
1128 const std::string& iface_name, const std::vector<uint8_t>& client_address,
1129 Ieee80211ReasonCode reason_code)
1130 {
1131 return forceClientDisconnectInternal(iface_name, client_address, reason_code);
1132 }
1133
setDebugParams(DebugLevel level)1134 ::ndk::ScopedAStatus Hostapd::setDebugParams(DebugLevel level)
1135 {
1136 return setDebugParamsInternal(level);
1137 }
1138
removeLinkFromMultipleLinkBridgedApIface(const std::string & iface_name,const std::string & linkIdentity)1139 ::ndk::ScopedAStatus Hostapd::removeLinkFromMultipleLinkBridgedApIface(
1140 const std::string& iface_name, const std::string& linkIdentity)
1141 {
1142 return removeLinkFromMultipleLinkBridgedApIfaceInternal(iface_name, linkIdentity);
1143 }
1144
addAccessPointInternal(const IfaceParams & iface_params,const NetworkParams & nw_params)1145 ::ndk::ScopedAStatus Hostapd::addAccessPointInternal(
1146 const IfaceParams& iface_params,
1147 const NetworkParams& nw_params)
1148 {
1149 int channelParamsSize = iface_params.channelParams.size();
1150 if (channelParamsSize == 1) {
1151 // Single AP
1152 wpa_printf(MSG_INFO, "AddSingleAccessPoint, iface=%s",
1153 iface_params.name.c_str());
1154 return addSingleAccessPoint(iface_params, iface_params.channelParams[0],
1155 nw_params, "", "");
1156 } else if (channelParamsSize == 2) {
1157 // Concurrent APs
1158 wpa_printf(MSG_INFO, "AddDualAccessPoint, iface=%s",
1159 iface_params.name.c_str());
1160 return addConcurrentAccessPoints(iface_params, nw_params);
1161 }
1162 return createStatus(HostapdStatusCode::FAILURE_ARGS_INVALID);
1163 }
1164
generateRandomOweSsid()1165 std::vector<uint8_t> generateRandomOweSsid()
1166 {
1167 u8 random[8] = {0};
1168 os_get_random(random, 8);
1169
1170 std::string ssid = StringPrintf("Owe-%s", random);
1171 wpa_printf(MSG_INFO, "Generated OWE SSID: %s", ssid.c_str());
1172 std::vector<uint8_t> vssid(ssid.begin(), ssid.end());
1173
1174 return vssid;
1175 }
1176
1177
1178 // Both of bridged dual APs and MLO AP will be treated as concurrenct APs.
1179 // -----------------------------------------
1180 // | br_name | instance#1 | instance#2 |
1181 // ___________________________________________________________
1182 // bridged dual APs | ap_br_wlanX | wlan X | wlanY |
1183 // ___________________________________________________________
1184 // MLO AP | wlanX | 0 | 1 |
1185 // ___________________________________________________________
1186 // Both will be added in br_interfaces_[$br_name] and use instance's name
1187 // to be iface_params_new.name to create single Access point.
addConcurrentAccessPoints(const IfaceParams & iface_params,const NetworkParams & nw_params)1188 ::ndk::ScopedAStatus Hostapd::addConcurrentAccessPoints(
1189 const IfaceParams& iface_params, const NetworkParams& nw_params)
1190 {
1191 int channelParamsListSize = iface_params.channelParams.size();
1192 // Get available interfaces in bridge
1193 std::vector<std::string> managed_instances;
1194 std::string br_name = StringPrintf("%s", iface_params.name.c_str());
1195 if (iface_params.usesMlo) {
1196 // MLO AP is using link id as instance.
1197 for (std::size_t i = 0; i < iface_params.instanceIdentities->size(); i++) {
1198 managed_instances.push_back(iface_params.instanceIdentities->at(i)->c_str());
1199 }
1200 } else {
1201 if (!GetInterfacesInBridge(br_name, &managed_instances)) {
1202 return createStatusWithMsg(HostapdStatusCode::FAILURE_UNKNOWN,
1203 "Get interfaces in bridge failed.");
1204 }
1205 }
1206 // Either bridged AP or MLO AP should have two instances.
1207 if (managed_instances.size() < channelParamsListSize) {
1208 return createStatusWithMsg(HostapdStatusCode::FAILURE_UNKNOWN,
1209 "Available interfaces less than requested bands");
1210 }
1211
1212 if (iface_params.usesMlo
1213 && nw_params.encryptionType == EncryptionType::WPA3_OWE_TRANSITION) {
1214 return createStatusWithMsg(HostapdStatusCode::FAILURE_UNKNOWN,
1215 "Invalid encryptionType (OWE transition) for MLO SAP.");
1216 }
1217 // start BSS on specified bands
1218 for (std::size_t i = 0; i < channelParamsListSize; i ++) {
1219 IfaceParams iface_params_new = iface_params;
1220 NetworkParams nw_params_new = nw_params;
1221 std::string owe_transition_ifname = "";
1222 iface_params_new.name = managed_instances[i];
1223 if (nw_params.encryptionType == EncryptionType::WPA3_OWE_TRANSITION) {
1224 if (i == 0 && i+1 < channelParamsListSize) {
1225 owe_transition_ifname = managed_instances[i+1];
1226 nw_params_new.encryptionType = EncryptionType::NONE;
1227 } else {
1228 owe_transition_ifname = managed_instances[0];
1229 nw_params_new.isHidden = true;
1230 nw_params_new.ssid = generateRandomOweSsid();
1231 }
1232 }
1233
1234 ndk::ScopedAStatus status = addSingleAccessPoint(
1235 iface_params_new, iface_params.channelParams[i], nw_params_new,
1236 br_name, owe_transition_ifname);
1237 if (!status.isOk()) {
1238 wpa_printf(MSG_ERROR, "Failed to addAccessPoint %s",
1239 managed_instances[i].c_str());
1240 return status;
1241 }
1242 }
1243
1244 if (iface_params.usesMlo) {
1245 std::size_t i = 0;
1246 std::size_t j = 0;
1247 for (i = 0; i < interfaces_->count; i++) {
1248 struct hostapd_iface *iface = interfaces_->iface[i];
1249
1250 for (j = 0; j < iface->num_bss; j++) {
1251 struct hostapd_data *iface_hapd = iface->bss[j];
1252 if (hostapd_enable_iface(iface_hapd->iface) < 0) {
1253 wpa_printf(
1254 MSG_ERROR, "Enabling interface %s failed on %zu",
1255 iface_params.name.c_str(), i);
1256 return createStatus(HostapdStatusCode::FAILURE_UNKNOWN);
1257 }
1258 }
1259 }
1260 }
1261 // Save bridge interface info
1262 br_interfaces_[br_name] = managed_instances;
1263 return ndk::ScopedAStatus::ok();
1264 }
1265
hostapd_get_iface_by_link_id(struct hapd_interfaces * interfaces,const size_t link_id)1266 struct hostapd_data * hostapd_get_iface_by_link_id(struct hapd_interfaces *interfaces,
1267 const size_t link_id)
1268 {
1269 #ifdef CONFIG_IEEE80211BE
1270 size_t i, j;
1271
1272 for (i = 0; i < interfaces->count; i++) {
1273 struct hostapd_iface *iface = interfaces->iface[i];
1274
1275 for (j = 0; j < iface->num_bss; j++) {
1276 struct hostapd_data *hapd = iface->bss[j];
1277
1278 if (link_id == hapd->mld_link_id)
1279 return hapd;
1280 }
1281 }
1282 #endif
1283 return NULL;
1284 }
1285
1286 // Both of bridged dual APs and MLO AP will be treated as concurrenct APs.
1287 // -----------------------------------------
1288 // | br_name | iface_params.name
1289 // _______________________________________________________________
1290 // bridged dual APs | bridged interface name | interface name
1291 // _______________________________________________________________
1292 // MLO AP | AP interface name | mld link id as instance name
1293 // _______________________________________________________________
addSingleAccessPoint(const IfaceParams & iface_params,const ChannelParams & channelParams,const NetworkParams & nw_params,const std::string br_name,const std::string owe_transition_ifname)1294 ::ndk::ScopedAStatus Hostapd::addSingleAccessPoint(
1295 const IfaceParams& iface_params,
1296 const ChannelParams& channelParams,
1297 const NetworkParams& nw_params,
1298 const std::string br_name,
1299 const std::string owe_transition_ifname)
1300 {
1301 if (iface_params.usesMlo) { // the mlo case, iface name is instance name which is mld_link_id
1302 if (hostapd_get_iface_by_link_id(interfaces_, (size_t) iface_params.name.c_str())) {
1303 wpa_printf(
1304 MSG_ERROR, "Instance link id %s already present",
1305 iface_params.name.c_str());
1306 return createStatus(HostapdStatusCode::FAILURE_IFACE_EXISTS);
1307 }
1308 }
1309 if (hostapd_get_iface(interfaces_,
1310 iface_params.usesMlo ? br_name.c_str() : iface_params.name.c_str())) {
1311 wpa_printf(
1312 MSG_ERROR, "Instance interface %s already present",
1313 iface_params.usesMlo ? br_name.c_str() : iface_params.name.c_str());
1314 return createStatus(HostapdStatusCode::FAILURE_IFACE_EXISTS);
1315 }
1316 const auto conf_params = CreateHostapdConfig(iface_params, channelParams, nw_params,
1317 br_name, owe_transition_ifname);
1318 if (conf_params.empty()) {
1319 wpa_printf(MSG_ERROR, "Failed to create config params");
1320 return createStatus(HostapdStatusCode::FAILURE_ARGS_INVALID);
1321 }
1322 const auto conf_file_path =
1323 WriteHostapdConfig(iface_params.name, conf_params, br_name, iface_params.usesMlo);
1324 if (conf_file_path.empty()) {
1325 wpa_printf(MSG_ERROR, "Failed to write config file");
1326 return createStatus(HostapdStatusCode::FAILURE_UNKNOWN);
1327 }
1328 std::string add_iface_param_str = StringPrintf(
1329 "%s config=%s", iface_params.usesMlo ? br_name.c_str(): iface_params.name.c_str(),
1330 conf_file_path.c_str());
1331 std::vector<char> add_iface_param_vec(
1332 add_iface_param_str.begin(), add_iface_param_str.end() + 1);
1333 if (hostapd_add_iface(interfaces_, add_iface_param_vec.data()) < 0) {
1334 wpa_printf(
1335 MSG_ERROR, "Adding hostapd iface %s failed",
1336 add_iface_param_str.c_str());
1337 return createStatus(HostapdStatusCode::FAILURE_UNKNOWN);
1338 }
1339
1340 // find the iface and set up callback.
1341 struct hostapd_data* iface_hapd = iface_params.usesMlo ?
1342 hostapd_get_iface_by_link_id(interfaces_, (size_t) iface_params.name.c_str()) :
1343 hostapd_get_iface(interfaces_, iface_params.name.c_str());
1344 WPA_ASSERT(iface_hapd != nullptr && iface_hapd->iface != nullptr);
1345 if (iface_params.usesMlo) {
1346 memcmp(iface_hapd->conf->iface, br_name.c_str(), br_name.size());
1347 }
1348
1349 // Callback discrepancy between bridged dual APs and MLO AP
1350 // Note: Only bridged dual APs will have "iface_hapd->conf->bridge" and
1351 // Only MLO AP will have "iface_hapd->mld_link_id"
1352 // Register the setup complete callbacks
1353 // -----------------------------------------
1354 // | bridged dual APs | bridged single link MLO | MLO SAP
1355 // _________________________________________________________________________________________
1356 // hapd->conf->bridge | bridged interface name | bridged interface nam | N/A
1357 // _________________________________________________________________________________________
1358 // hapd->conf->iface | AP interface name | AP interface name | AP interface name
1359 // _________________________________________________________________________________________
1360 // hapd->mld_link_id | 0 (default value) | link id (0) | link id (0 or 1)
1361 // _________________________________________________________________________________________
1362 // hapd->mld_ap | 0 | 1 | 1
1363 on_setup_complete_internal_callback =
1364 [this](struct hostapd_data* iface_hapd) {
1365 wpa_printf(
1366 MSG_INFO, "AP interface setup completed - state %s",
1367 hostapd_state_text(iface_hapd->iface->state));
1368 if (iface_hapd->iface->state == HAPD_IFACE_DISABLED) {
1369 // Invoke the failure callback on all registered
1370 // clients.
1371 std::string instanceName = iface_hapd->conf->iface;
1372 #ifdef CONFIG_IEEE80211BE
1373 if (iface_hapd->conf->mld_ap
1374 && strlen(iface_hapd->conf->bridge) == 0) {
1375 instanceName = std::to_string(iface_hapd->mld_link_id);
1376 }
1377 #endif
1378 for (const auto& callback : callbacks_) {
1379 auto status = callback->onFailure(
1380 strlen(iface_hapd->conf->bridge) > 0 ?
1381 iface_hapd->conf->bridge : iface_hapd->conf->iface,
1382 instanceName);
1383 if (!status.isOk()) {
1384 wpa_printf(MSG_ERROR, "Failed to invoke onFailure");
1385 }
1386 }
1387 }
1388 };
1389
1390 // Register for new client connect/disconnect indication.
1391 on_sta_authorized_internal_callback =
1392 [this](struct hostapd_data* iface_hapd, const u8 *mac_addr,
1393 int authorized, const u8 *p2p_dev_addr) {
1394 wpa_printf(MSG_DEBUG, "notify client " MACSTR " %s",
1395 MAC2STR(mac_addr),
1396 (authorized) ? "Connected" : "Disconnected");
1397 ClientInfo info;
1398 info.ifaceName = strlen(iface_hapd->conf->bridge) > 0 ?
1399 iface_hapd->conf->bridge : iface_hapd->conf->iface;
1400 std::string instanceName = iface_hapd->conf->iface;
1401 #ifdef CONFIG_IEEE80211BE
1402 if (iface_hapd->conf->mld_ap
1403 && strlen(iface_hapd->conf->bridge) == 0) {
1404 instanceName = std::to_string(iface_hapd->mld_link_id);
1405 }
1406 #endif
1407 info.apIfaceInstance = instanceName;
1408 info.clientAddress.assign(mac_addr, mac_addr + ETH_ALEN);
1409 info.isConnected = authorized;
1410 if(isAidlServiceVersionAtLeast(3) && !authorized) {
1411 u16 disconnect_reason_code = WLAN_REASON_UNSPECIFIED;
1412 auto sta_ptr_optional = getStaInfoByMacAddr(iface_hapd, mac_addr);
1413 if (sta_ptr_optional.has_value()){
1414 disconnect_reason_code = sta_ptr_optional.value()->deauth_reason;
1415 }
1416 info.disconnectReasonCode =
1417 static_cast<common::DeauthenticationReasonCode>(disconnect_reason_code);
1418 }
1419 for (const auto &callback : callbacks_) {
1420 auto status = callback->onConnectedClientsChanged(info);
1421 if (!status.isOk()) {
1422 wpa_printf(MSG_ERROR, "Failed to invoke onConnectedClientsChanged");
1423 }
1424 }
1425 };
1426
1427 // Register for wpa_event which used to get channel switch event
1428 on_wpa_msg_internal_callback =
1429 [this](struct hostapd_data* iface_hapd, int level,
1430 enum wpa_msg_type type, const char *txt,
1431 size_t len) {
1432 wpa_printf(MSG_DEBUG, "Receive wpa msg : %s", txt);
1433 if (os_strncmp(txt, AP_EVENT_ENABLED,
1434 strlen(AP_EVENT_ENABLED)) == 0 ||
1435 os_strncmp(txt, WPA_EVENT_CHANNEL_SWITCH,
1436 strlen(WPA_EVENT_CHANNEL_SWITCH)) == 0) {
1437 std::string instanceName = iface_hapd->conf->iface;
1438 #ifdef CONFIG_IEEE80211BE
1439 if (iface_hapd->conf->mld_ap && strlen(iface_hapd->conf->bridge) == 0) {
1440 instanceName = std::to_string(iface_hapd->mld_link_id);
1441 }
1442 #endif
1443 ApInfo info;
1444 info.ifaceName = strlen(iface_hapd->conf->bridge) > 0 ?
1445 iface_hapd->conf->bridge : iface_hapd->conf->iface,
1446 info.apIfaceInstance = instanceName;
1447 info.freqMhz = iface_hapd->iface->freq;
1448 info.channelBandwidth = getChannelBandwidth(iface_hapd->iconf);
1449 info.generation = getGeneration(iface_hapd->iface->current_mode);
1450 info.apIfaceInstanceMacAddress.assign(iface_hapd->own_addr,
1451 iface_hapd->own_addr + ETH_ALEN);
1452 for (const auto &callback : callbacks_) {
1453 auto status = callback->onApInstanceInfoChanged(info);
1454 if (!status.isOk()) {
1455 wpa_printf(MSG_ERROR,
1456 "Failed to invoke onApInstanceInfoChanged");
1457 }
1458 }
1459 } else if (os_strncmp(txt, AP_EVENT_DISABLED, strlen(AP_EVENT_DISABLED)) == 0
1460 || os_strncmp(txt, INTERFACE_DISABLED, strlen(INTERFACE_DISABLED)) == 0)
1461 {
1462 std::string instanceName = iface_hapd->conf->iface;
1463 #ifdef CONFIG_IEEE80211BE
1464 if (iface_hapd->conf->mld_ap && strlen(iface_hapd->conf->bridge) == 0) {
1465 instanceName = std::to_string(iface_hapd->mld_link_id);
1466 }
1467 #endif
1468 // Invoke the failure callback on all registered clients.
1469 for (const auto& callback : callbacks_) {
1470 auto status =
1471 callback->onFailure(strlen(iface_hapd->conf->bridge) > 0 ?
1472 iface_hapd->conf->bridge : iface_hapd->conf->iface,
1473 instanceName);
1474 if (!status.isOk()) {
1475 wpa_printf(MSG_ERROR, "Failed to invoke onFailure");
1476 }
1477 }
1478 }
1479 };
1480
1481 // Setup callback
1482 iface_hapd->setup_complete_cb = onAsyncSetupCompleteCb;
1483 iface_hapd->setup_complete_cb_ctx = iface_hapd;
1484 iface_hapd->sta_authorized_cb = onAsyncStaAuthorizedCb;
1485 iface_hapd->sta_authorized_cb_ctx = iface_hapd;
1486 wpa_msg_register_aidl_cb(onAsyncWpaEventCb);
1487
1488 // Multi-link MLO should enable iface after both links have been set.
1489 if (!iface_params.usesMlo && hostapd_enable_iface(iface_hapd->iface) < 0) {
1490 wpa_printf(
1491 MSG_ERROR, "Enabling interface %s failed",
1492 iface_params.name.c_str());
1493 return createStatus(HostapdStatusCode::FAILURE_UNKNOWN);
1494 }
1495 return ndk::ScopedAStatus::ok();
1496 }
1497
removeAccessPointInternal(const std::string & iface_name)1498 ::ndk::ScopedAStatus Hostapd::removeAccessPointInternal(const std::string& iface_name)
1499 {
1500 // interfaces to be removed
1501 std::vector<std::string> interfaces;
1502 bool is_error = false;
1503
1504 const auto it = br_interfaces_.find(iface_name);
1505 if (it != br_interfaces_.end()) {
1506 // In case bridge, remove managed interfaces
1507 interfaces = it->second;
1508 br_interfaces_.erase(iface_name);
1509 } else {
1510 // else remove current interface
1511 interfaces.push_back(iface_name);
1512 }
1513
1514 for (auto& iface : interfaces) {
1515 std::vector<char> remove_iface_param_vec(
1516 iface.begin(), iface.end() + 1);
1517 if (hostapd_remove_iface(interfaces_, remove_iface_param_vec.data()) < 0) {
1518 wpa_printf(MSG_INFO, "Remove interface %s failed", iface.c_str());
1519 is_error = true;
1520 }
1521 }
1522 if (is_error) {
1523 return createStatus(HostapdStatusCode::FAILURE_UNKNOWN);
1524 }
1525 return ndk::ScopedAStatus::ok();
1526 }
1527
registerCallbackInternal(const std::shared_ptr<IHostapdCallback> & callback)1528 ::ndk::ScopedAStatus Hostapd::registerCallbackInternal(
1529 const std::shared_ptr<IHostapdCallback>& callback)
1530 {
1531 binder_status_t status = AIBinder_linkToDeath(callback->asBinder().get(),
1532 death_notifier_, this /* cookie */);
1533 if (status != STATUS_OK) {
1534 wpa_printf(
1535 MSG_ERROR,
1536 "Error registering for death notification for "
1537 "hostapd callback object");
1538 return createStatus(HostapdStatusCode::FAILURE_UNKNOWN);
1539 }
1540 callbacks_.push_back(callback);
1541 if (aidl_service_version == 0) {
1542 aidl_service_version = Hostapd::version;
1543 wpa_printf(MSG_INFO, "AIDL service version: %d", aidl_service_version);
1544 }
1545 if (aidl_client_version == 0) {
1546 callback->getInterfaceVersion(&aidl_client_version);
1547 wpa_printf(MSG_INFO, "AIDL client version: %d", aidl_client_version);
1548 }
1549 return ndk::ScopedAStatus::ok();
1550 }
1551
forceClientDisconnectInternal(const std::string & iface_name,const std::vector<uint8_t> & client_address,Ieee80211ReasonCode reason_code)1552 ::ndk::ScopedAStatus Hostapd::forceClientDisconnectInternal(const std::string& iface_name,
1553 const std::vector<uint8_t>& client_address, Ieee80211ReasonCode reason_code)
1554 {
1555 struct hostapd_data *hapd = hostapd_get_iface(interfaces_, iface_name.c_str());
1556 bool result;
1557 if (!hapd) {
1558 for (auto const& iface : br_interfaces_) {
1559 if (iface.first == iface_name) {
1560 for (auto const& instance : iface.second) {
1561 hapd = hostapd_get_iface(interfaces_, instance.c_str());
1562 if (hapd) {
1563 result = forceStaDisconnection(hapd, client_address,
1564 (uint16_t) reason_code);
1565 if (result) break;
1566 }
1567 }
1568 }
1569 }
1570 } else {
1571 result = forceStaDisconnection(hapd, client_address, (uint16_t) reason_code);
1572 }
1573 if (!hapd) {
1574 wpa_printf(MSG_ERROR, "Interface %s doesn't exist", iface_name.c_str());
1575 return createStatus(HostapdStatusCode::FAILURE_IFACE_UNKNOWN);
1576 }
1577 if (result) {
1578 return ndk::ScopedAStatus::ok();
1579 }
1580 return createStatus(HostapdStatusCode::FAILURE_CLIENT_UNKNOWN);
1581 }
1582
setDebugParamsInternal(DebugLevel level)1583 ::ndk::ScopedAStatus Hostapd::setDebugParamsInternal(DebugLevel level)
1584 {
1585 wpa_debug_level = static_cast<uint32_t>(level);
1586 return ndk::ScopedAStatus::ok();
1587 }
1588
removeLinkFromMultipleLinkBridgedApIfaceInternal(const std::string & iface_name,const std::string & linkIdentity)1589 ::ndk::ScopedAStatus Hostapd::removeLinkFromMultipleLinkBridgedApIfaceInternal(
1590 const std::string& iface_name, const std::string& linkIdentity)
1591 {
1592 if (!hostapd_get_iface(interfaces_, iface_name.c_str())) {
1593 wpa_printf(MSG_ERROR, "Interface %s doesn't exist", iface_name.c_str());
1594 return createStatus(HostapdStatusCode::FAILURE_IFACE_UNKNOWN);
1595 }
1596 struct hostapd_data* iface_hapd =
1597 hostapd_get_iface_by_link_id(interfaces_, (size_t) linkIdentity.c_str());
1598 if (iface_hapd) {
1599 if (0 == hostapd_link_remove(iface_hapd, 1)) {
1600 return ndk::ScopedAStatus::ok();
1601 }
1602 }
1603 return createStatus(HostapdStatusCode::FAILURE_ARGS_INVALID);
1604 }
1605
1606 } // namespace hostapd
1607 } // namespace wifi
1608 } // namespace hardware
1609 } // namespace android
1610 } // namespace aidl
1611