blob: afb4147f20916c49708427196aaf17874b179181 [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
38// The AIDL implementation for hostapd creates a hostapd.conf dynamically for
39// each interface. This file can then be used to hook onto the normal config
40// file parsing logic in hostapd code. Helps us to avoid duplication of code
41// in the AIDL interface.
42// TOOD(b/71872409): Add unit tests for this.
43namespace {
44constexpr char kConfFileNameFmt[] = "/data/vendor/wifi/hostapd/hostapd_%s.conf";
45
46using android::base::RemoveFileIfExists;
47using android::base::StringPrintf;
48using android::base::WriteStringToFile;
49using aidl::android::hardware::wifi::hostapd::BandMask;
Ahmed ElArabawyb4115792022-02-08 09:33:01 -080050using aidl::android::hardware::wifi::hostapd::ChannelBandwidth;
Gabriel Biren72cf9a52021-06-25 23:29:26 +000051using aidl::android::hardware::wifi::hostapd::ChannelParams;
52using aidl::android::hardware::wifi::hostapd::EncryptionType;
53using aidl::android::hardware::wifi::hostapd::Generation;
54using aidl::android::hardware::wifi::hostapd::HostapdStatusCode;
55using aidl::android::hardware::wifi::hostapd::IfaceParams;
56using aidl::android::hardware::wifi::hostapd::NetworkParams;
57using aidl::android::hardware::wifi::hostapd::ParamSizeLimits;
58
59int band2Ghz = (int)BandMask::BAND_2_GHZ;
60int band5Ghz = (int)BandMask::BAND_5_GHZ;
61int band6Ghz = (int)BandMask::BAND_6_GHZ;
62int band60Ghz = (int)BandMask::BAND_60_GHZ;
63
Manaswini Paluri76ae6982024-02-29 14:49:20 +053064int32_t aidl_client_version = 0;
65int32_t aidl_service_version = 0;
66
67/**
68 * Check that the AIDL service is running at least the expected version.
69 * Use to avoid the case where the AIDL interface version
70 * is greater than the version implemented by the service.
71 */
72inline int32_t isAidlServiceVersionAtLeast(int32_t expected_version)
73{
74 return expected_version <= aidl_service_version;
75}
76
77inline int32_t isAidlClientVersionAtLeast(int32_t expected_version)
78{
79 return expected_version <= aidl_client_version;
80}
81
Gabriel Biren72cf9a52021-06-25 23:29:26 +000082#define MAX_PORTS 1024
83bool GetInterfacesInBridge(std::string br_name,
84 std::vector<std::string>* interfaces) {
85 android::base::unique_fd sock(socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0));
86 if (sock.get() < 0) {
87 wpa_printf(MSG_ERROR, "Failed to create sock (%s) in %s",
88 strerror(errno), __FUNCTION__);
89 return false;
90 }
91
92 struct ifreq request;
93 int i, ifindices[MAX_PORTS];
94 char if_name[IFNAMSIZ];
95 unsigned long args[3];
96
97 memset(ifindices, 0, MAX_PORTS * sizeof(int));
98
99 args[0] = BRCTL_GET_PORT_LIST;
100 args[1] = (unsigned long) ifindices;
101 args[2] = MAX_PORTS;
102
103 strlcpy(request.ifr_name, br_name.c_str(), IFNAMSIZ);
104 request.ifr_data = (char *)args;
105
106 if (ioctl(sock.get(), SIOCDEVPRIVATE, &request) < 0) {
107 wpa_printf(MSG_ERROR, "Failed to ioctl SIOCDEVPRIVATE in %s",
108 __FUNCTION__);
109 return false;
110 }
111
112 for (i = 0; i < MAX_PORTS; i ++) {
113 memset(if_name, 0, IFNAMSIZ);
114 if (ifindices[i] == 0 || !if_indextoname(ifindices[i], if_name)) {
115 continue;
116 }
117 interfaces->push_back(if_name);
118 }
119 return true;
120}
121
122std::string WriteHostapdConfig(
123 const std::string& interface_name, const std::string& config)
124{
125 const std::string file_path =
126 StringPrintf(kConfFileNameFmt, interface_name.c_str());
127 if (WriteStringToFile(
128 config, file_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
129 getuid(), getgid())) {
130 return file_path;
131 }
132 // Diagnose failure
133 int error = errno;
134 wpa_printf(
135 MSG_ERROR, "Cannot write hostapd config to %s, error: %s",
136 file_path.c_str(), strerror(error));
137 struct stat st;
138 int result = stat(file_path.c_str(), &st);
139 if (result == 0) {
140 wpa_printf(
141 MSG_ERROR, "hostapd config file uid: %d, gid: %d, mode: %d",
142 st.st_uid, st.st_gid, st.st_mode);
143 } else {
144 wpa_printf(
145 MSG_ERROR,
146 "Error calling stat() on hostapd config file: %s",
147 strerror(errno));
148 }
149 return "";
150}
151
152/*
153 * Get the op_class for a channel/band
154 * The logic here is based on Table E-4 in the 802.11 Specification
155 */
156int getOpClassForChannel(int channel, int band, bool support11n, bool support11ac) {
157 // 2GHz Band
158 if ((band & band2Ghz) != 0) {
159 if (channel == 14) {
160 return 82;
161 }
162 if (channel >= 1 && channel <= 13) {
163 if (!support11n) {
164 //20MHz channel
165 return 81;
166 }
167 if (channel <= 9) {
168 // HT40 with secondary channel above primary
169 return 83;
170 }
171 // HT40 with secondary channel below primary
172 return 84;
173 }
174 // Error
175 return 0;
176 }
177
178 // 5GHz Band
179 if ((band & band5Ghz) != 0) {
180 if (support11ac) {
181 switch (channel) {
182 case 42:
183 case 58:
184 case 106:
185 case 122:
186 case 138:
187 case 155:
188 // 80MHz channel
189 return 128;
190 case 50:
191 case 114:
192 // 160MHz channel
193 return 129;
194 }
195 }
196
197 if (!support11n) {
198 if (channel >= 36 && channel <= 48) {
199 return 115;
200 }
201 if (channel >= 52 && channel <= 64) {
202 return 118;
203 }
204 if (channel >= 100 && channel <= 144) {
205 return 121;
206 }
207 if (channel >= 149 && channel <= 161) {
208 return 124;
209 }
210 if (channel >= 165 && channel <= 169) {
211 return 125;
212 }
213 } else {
214 switch (channel) {
215 case 36:
216 case 44:
217 // HT40 with secondary channel above primary
218 return 116;
219 case 40:
220 case 48:
221 // HT40 with secondary channel below primary
222 return 117;
223 case 52:
224 case 60:
225 // HT40 with secondary channel above primary
226 return 119;
227 case 56:
228 case 64:
229 // HT40 with secondary channel below primary
230 return 120;
231 case 100:
232 case 108:
233 case 116:
234 case 124:
235 case 132:
236 case 140:
237 // HT40 with secondary channel above primary
238 return 122;
239 case 104:
240 case 112:
241 case 120:
242 case 128:
243 case 136:
244 case 144:
245 // HT40 with secondary channel below primary
246 return 123;
247 case 149:
248 case 157:
249 // HT40 with secondary channel above primary
250 return 126;
251 case 153:
252 case 161:
253 // HT40 with secondary channel below primary
254 return 127;
255 }
256 }
257 // Error
258 return 0;
259 }
260
261 // 6GHz Band
262 if ((band & band6Ghz) != 0) {
263 // Channels 1, 5. 9, 13, ...
264 if ((channel & 0x03) == 0x01) {
265 // 20MHz channel
266 return 131;
267 }
268 // Channels 3, 11, 19, 27, ...
269 if ((channel & 0x07) == 0x03) {
270 // 40MHz channel
271 return 132;
272 }
273 // Channels 7, 23, 39, 55, ...
274 if ((channel & 0x0F) == 0x07) {
275 // 80MHz channel
276 return 133;
277 }
278 // Channels 15, 47, 69, ...
279 if ((channel & 0x1F) == 0x0F) {
280 // 160MHz channel
281 return 134;
282 }
283 if (channel == 2) {
284 // 20MHz channel
285 return 136;
286 }
287 // Error
288 return 0;
289 }
290
291 if ((band & band60Ghz) != 0) {
292 if (1 <= channel && channel <= 8) {
293 return 180;
294 } else if (9 <= channel && channel <= 15) {
295 return 181;
296 } else if (17 <= channel && channel <= 22) {
297 return 182;
298 } else if (25 <= channel && channel <= 29) {
299 return 183;
300 }
301 // Error
302 return 0;
303 }
304
305 return 0;
306}
307
308bool validatePassphrase(int passphrase_len, int min_len, int max_len)
309{
310 if (min_len != -1 && passphrase_len < min_len) return false;
311 if (max_len != -1 && passphrase_len > max_len) return false;
312 return true;
313}
314
Manaswini Paluri76ae6982024-02-29 14:49:20 +0530315std::string getInterfaceMacAddress(const std::string& if_name)
316{
317 u8 addr[ETH_ALEN] = {};
318 struct ifreq ifr;
319 std::string mac_addr;
320
321 android::base::unique_fd sock(socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0));
322 if (sock.get() < 0) {
323 wpa_printf(MSG_ERROR, "Failed to create sock (%s) in %s",
324 strerror(errno), __FUNCTION__);
325 return "";
326 }
327
328 memset(&ifr, 0, sizeof(ifr));
329 strlcpy(ifr.ifr_name, if_name.c_str(), IFNAMSIZ);
330 if (ioctl(sock.get(), SIOCGIFHWADDR, &ifr) < 0) {
331 wpa_printf(MSG_ERROR, "Could not get interface %s hwaddr: %s",
332 if_name.c_str(), strerror(errno));
333 return "";
334 }
335
336 memcpy(addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
337 mac_addr = StringPrintf("" MACSTR, MAC2STR(addr));
338
339 return mac_addr;
340}
341
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000342std::string CreateHostapdConfig(
343 const IfaceParams& iface_params,
344 const ChannelParams& channelParams,
345 const NetworkParams& nw_params,
Purushottam Kushwaha0316c882021-12-20 15:07:44 +0530346 const std::string br_name,
347 const std::string owe_transition_ifname)
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000348{
349 if (nw_params.ssid.size() >
350 static_cast<uint32_t>(
351 ParamSizeLimits::SSID_MAX_LEN_IN_BYTES)) {
352 wpa_printf(
353 MSG_ERROR, "Invalid SSID size: %zu", nw_params.ssid.size());
354 return "";
355 }
356
357 // SSID string
358 std::stringstream ss;
359 ss << std::hex;
360 ss << std::setfill('0');
361 for (uint8_t b : nw_params.ssid) {
362 ss << std::setw(2) << static_cast<unsigned int>(b);
363 }
364 const std::string ssid_as_string = ss.str();
365
366 // Encryption config string
367 uint32_t band = 0;
368 band |= static_cast<uint32_t>(channelParams.bandMask);
Sunil Ravi4fc918f2022-04-17 09:26:48 -0700369 bool is_2Ghz_band_only = band == static_cast<uint32_t>(band2Ghz);
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000370 bool is_6Ghz_band_only = band == static_cast<uint32_t>(band6Ghz);
371 bool is_60Ghz_band_only = band == static_cast<uint32_t>(band60Ghz);
372 std::string encryption_config_as_string;
373 switch (nw_params.encryptionType) {
374 case EncryptionType::NONE:
375 // no security params
376 break;
377 case EncryptionType::WPA:
378 if (!validatePassphrase(
379 nw_params.passphrase.size(),
380 static_cast<uint32_t>(ParamSizeLimits::
381 WPA2_PSK_PASSPHRASE_MIN_LEN_IN_BYTES),
382 static_cast<uint32_t>(ParamSizeLimits::
383 WPA2_PSK_PASSPHRASE_MAX_LEN_IN_BYTES))) {
384 return "";
385 }
386 encryption_config_as_string = StringPrintf(
387 "wpa=3\n"
388 "wpa_pairwise=%s\n"
389 "wpa_passphrase=%s",
390 is_60Ghz_band_only ? "GCMP" : "TKIP CCMP",
391 nw_params.passphrase.c_str());
392 break;
393 case EncryptionType::WPA2:
394 if (!validatePassphrase(
395 nw_params.passphrase.size(),
396 static_cast<uint32_t>(ParamSizeLimits::
397 WPA2_PSK_PASSPHRASE_MIN_LEN_IN_BYTES),
398 static_cast<uint32_t>(ParamSizeLimits::
399 WPA2_PSK_PASSPHRASE_MAX_LEN_IN_BYTES))) {
400 return "";
401 }
402 encryption_config_as_string = StringPrintf(
403 "wpa=2\n"
404 "rsn_pairwise=%s\n"
Sunil Ravib3580db2022-01-28 12:25:46 -0800405#ifdef ENABLE_HOSTAPD_CONFIG_80211W_MFP_OPTIONAL
406 "ieee80211w=1\n"
407#endif
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000408 "wpa_passphrase=%s",
409 is_60Ghz_band_only ? "GCMP" : "CCMP",
410 nw_params.passphrase.c_str());
411 break;
412 case EncryptionType::WPA3_SAE_TRANSITION:
413 if (!validatePassphrase(
414 nw_params.passphrase.size(),
415 static_cast<uint32_t>(ParamSizeLimits::
416 WPA2_PSK_PASSPHRASE_MIN_LEN_IN_BYTES),
417 static_cast<uint32_t>(ParamSizeLimits::
418 WPA2_PSK_PASSPHRASE_MAX_LEN_IN_BYTES))) {
419 return "";
420 }
Sunil Ravid917c832023-07-07 17:30:33 +0000421 // WPA3 transition mode or SAE+WPA_PSK key management(AKM) is not allowed in 6GHz.
422 // Auto-convert any such configurations to SAE.
423 if ((band & band6Ghz) != 0) {
424 wpa_printf(MSG_INFO, "WPA3_SAE_TRANSITION configured in 6GHz band."
425 "Enable only SAE in key_mgmt");
426 encryption_config_as_string = StringPrintf(
427 "wpa=2\n"
428 "rsn_pairwise=CCMP\n"
429 "wpa_key_mgmt=%s\n"
430 "ieee80211w=2\n"
431 "sae_require_mfp=2\n"
432 "sae_pwe=%d\n"
433 "sae_password=%s",
Sunil Ravic1edd3e2023-02-06 18:52:51 +0000434#ifdef CONFIG_IEEE80211BE
Sunil Ravid917c832023-07-07 17:30:33 +0000435 iface_params.hwModeParams.enable80211BE ?
436 "SAE SAE-EXT-KEY" : "SAE",
Sunil Ravic1edd3e2023-02-06 18:52:51 +0000437#else
Sunil Ravid917c832023-07-07 17:30:33 +0000438 "SAE",
Sunil Ravic1edd3e2023-02-06 18:52:51 +0000439#endif
Sunil Ravid917c832023-07-07 17:30:33 +0000440 is_6Ghz_band_only ? 1 : 2,
441 nw_params.passphrase.c_str());
442 } else {
443 encryption_config_as_string = StringPrintf(
444 "wpa=2\n"
445 "rsn_pairwise=%s\n"
446 "wpa_key_mgmt=%s\n"
447 "ieee80211w=1\n"
448 "sae_require_mfp=1\n"
449 "wpa_passphrase=%s\n"
450 "sae_password=%s",
451 is_60Ghz_band_only ? "GCMP" : "CCMP",
452#ifdef CONFIG_IEEE80211BE
453 iface_params.hwModeParams.enable80211BE ?
454 "WPA-PSK SAE SAE-EXT-KEY" : "WPA-PSK SAE",
455#else
456 "WPA-PSK SAE",
457#endif
458 nw_params.passphrase.c_str(),
459 nw_params.passphrase.c_str());
460 }
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000461 break;
462 case EncryptionType::WPA3_SAE:
463 if (!validatePassphrase(nw_params.passphrase.size(), 1, -1)) {
464 return "";
465 }
466 encryption_config_as_string = StringPrintf(
467 "wpa=2\n"
468 "rsn_pairwise=%s\n"
Sunil Ravi65251732023-01-24 05:03:35 +0000469 "wpa_key_mgmt=%s\n"
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000470 "ieee80211w=2\n"
471 "sae_require_mfp=2\n"
472 "sae_pwe=%d\n"
473 "sae_password=%s",
474 is_60Ghz_band_only ? "GCMP" : "CCMP",
Sunil Ravi65251732023-01-24 05:03:35 +0000475#ifdef CONFIG_IEEE80211BE
476 iface_params.hwModeParams.enable80211BE ? "SAE SAE-EXT-KEY" : "SAE",
477#else
478 "SAE",
479#endif
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000480 is_6Ghz_band_only ? 1 : 2,
481 nw_params.passphrase.c_str());
482 break;
Ahmed ElArabawy1aaf1802022-02-04 15:58:55 -0800483 case EncryptionType::WPA3_OWE_TRANSITION:
484 encryption_config_as_string = StringPrintf(
485 "wpa=2\n"
486 "rsn_pairwise=%s\n"
487 "wpa_key_mgmt=OWE\n"
488 "ieee80211w=2",
489 is_60Ghz_band_only ? "GCMP" : "CCMP");
490 break;
491 case EncryptionType::WPA3_OWE:
Purushottam Kushwaha0316c882021-12-20 15:07:44 +0530492 encryption_config_as_string = StringPrintf(
493 "wpa=2\n"
494 "rsn_pairwise=%s\n"
495 "wpa_key_mgmt=OWE\n"
496 "ieee80211w=2",
497 is_60Ghz_band_only ? "GCMP" : "CCMP");
498 break;
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000499 default:
500 wpa_printf(MSG_ERROR, "Unknown encryption type");
501 return "";
502 }
503
504 std::string channel_config_as_string;
505 bool isFirst = true;
506 if (channelParams.enableAcs) {
507 std::string freqList_as_string;
508 for (const auto &range :
509 channelParams.acsChannelFreqRangesMhz) {
510 if (!isFirst) {
511 freqList_as_string += ",";
512 }
513 isFirst = false;
514
515 if (range.startMhz != range.endMhz) {
516 freqList_as_string +=
517 StringPrintf("%d-%d", range.startMhz, range.endMhz);
518 } else {
519 freqList_as_string += StringPrintf("%d", range.startMhz);
520 }
521 }
522 channel_config_as_string = StringPrintf(
523 "channel=0\n"
524 "acs_exclude_dfs=%d\n"
525 "freqlist=%s",
526 channelParams.acsShouldExcludeDfs,
527 freqList_as_string.c_str());
528 } else {
529 int op_class = getOpClassForChannel(
530 channelParams.channel,
531 band,
532 iface_params.hwModeParams.enable80211N,
533 iface_params.hwModeParams.enable80211AC);
534 channel_config_as_string = StringPrintf(
535 "channel=%d\n"
536 "op_class=%d",
537 channelParams.channel, op_class);
538 }
539
540 std::string hw_mode_as_string;
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000541 std::string enable_edmg_as_string;
542 std::string edmg_channel_as_string;
543 bool is_60Ghz_used = false;
544
545 if (((band & band60Ghz) != 0)) {
546 hw_mode_as_string = "hw_mode=ad";
547 if (iface_params.hwModeParams.enableEdmg) {
548 enable_edmg_as_string = "enable_edmg=1";
549 edmg_channel_as_string = StringPrintf(
550 "edmg_channel=%d",
551 channelParams.channel);
552 }
553 is_60Ghz_used = true;
554 } else if ((band & band2Ghz) != 0) {
555 if (((band & band5Ghz) != 0)
556 || ((band & band6Ghz) != 0)) {
557 hw_mode_as_string = "hw_mode=any";
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000558 } else {
559 hw_mode_as_string = "hw_mode=g";
560 }
561 } else if (((band & band5Ghz) != 0)
562 || ((band & band6Ghz) != 0)) {
563 hw_mode_as_string = "hw_mode=a";
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000564 } else {
565 wpa_printf(MSG_ERROR, "Invalid band");
566 return "";
567 }
568
569 std::string he_params_as_string;
570#ifdef CONFIG_IEEE80211AX
571 if (iface_params.hwModeParams.enable80211AX && !is_60Ghz_used) {
572 he_params_as_string = StringPrintf(
573 "ieee80211ax=1\n"
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000574 "he_su_beamformer=%d\n"
575 "he_su_beamformee=%d\n"
576 "he_mu_beamformer=%d\n"
577 "he_twt_required=%d\n",
578 iface_params.hwModeParams.enableHeSingleUserBeamformer ? 1 : 0,
579 iface_params.hwModeParams.enableHeSingleUserBeamformee ? 1 : 0,
580 iface_params.hwModeParams.enableHeMultiUserBeamformer ? 1 : 0,
581 iface_params.hwModeParams.enableHeTargetWakeTime ? 1 : 0);
582 } else {
583 he_params_as_string = "ieee80211ax=0";
584 }
585#endif /* CONFIG_IEEE80211AX */
Sunil Ravi65251732023-01-24 05:03:35 +0000586 std::string eht_params_as_string;
587#ifdef CONFIG_IEEE80211BE
588 if (iface_params.hwModeParams.enable80211BE && !is_60Ghz_used) {
Manaswini Paluri76ae6982024-02-29 14:49:20 +0530589 eht_params_as_string = "ieee80211be=1\n";
590 if (isAidlServiceVersionAtLeast(2) && isAidlClientVersionAtLeast(2)) {
591 std::string interface_mac_addr = getInterfaceMacAddress(iface_params.name);
592 if (interface_mac_addr.empty()) {
593 wpa_printf(MSG_ERROR,
594 "Unable to set interface mac address as bssid for 11BE SAP");
595 return "";
596 }
597 eht_params_as_string += StringPrintf(
598 "bssid=%s\n"
599 "mld_ap=1",
600 interface_mac_addr.c_str());
601 }
Sunil Ravi65251732023-01-24 05:03:35 +0000602 /* TODO set eht_su_beamformer, eht_su_beamformee, eht_mu_beamformer */
603 } else {
604 eht_params_as_string = "ieee80211be=0";
605 }
606#endif /* CONFIG_IEEE80211BE */
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000607
Xin Dengfec682f2024-02-06 22:59:39 -0800608 std::string ht_cap_vht_oper_he_oper_eht_oper_chwidth_as_string;
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530609 switch (iface_params.hwModeParams.maximumChannelBandwidth) {
610 case ChannelBandwidth::BANDWIDTH_20:
Xin Dengfec682f2024-02-06 22:59:39 -0800611 ht_cap_vht_oper_he_oper_eht_oper_chwidth_as_string = StringPrintf(
612#ifdef CONFIG_IEEE80211BE
613 "eht_oper_chwidth=0\n"
614#endif /* CONFIG_IEEE80211BE */
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530615#ifdef CONFIG_IEEE80211AX
616 "he_oper_chwidth=0\n"
617#endif
Kiran Kumar Lokereb6445122024-02-06 20:06:27 -0800618 "vht_oper_chwidth=0\n"
619 "%s", (band & band6Ghz) ? "op_class=131" : "");
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530620 break;
621 case ChannelBandwidth::BANDWIDTH_40:
Xin Dengfec682f2024-02-06 22:59:39 -0800622 ht_cap_vht_oper_he_oper_eht_oper_chwidth_as_string = StringPrintf(
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530623 "ht_capab=[HT40+]\n"
Xin Dengfec682f2024-02-06 22:59:39 -0800624#ifdef CONFIG_IEEE80211BE
625 "eht_oper_chwidth=0\n"
626#endif /* CONFIG_IEEE80211BE */
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530627#ifdef CONFIG_IEEE80211AX
628 "he_oper_chwidth=0\n"
629#endif
Kiran Kumar Lokereb6445122024-02-06 20:06:27 -0800630 "vht_oper_chwidth=0\n"
631 "%s", (band & band6Ghz) ? "op_class=132" : "");
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530632 break;
633 case ChannelBandwidth::BANDWIDTH_80:
Xin Dengfec682f2024-02-06 22:59:39 -0800634 ht_cap_vht_oper_he_oper_eht_oper_chwidth_as_string = StringPrintf(
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530635 "ht_capab=[HT40+]\n"
Xin Dengfec682f2024-02-06 22:59:39 -0800636#ifdef CONFIG_IEEE80211BE
637 "eht_oper_chwidth=%d\n"
638#endif /* CONFIG_IEEE80211BE */
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530639#ifdef CONFIG_IEEE80211AX
640 "he_oper_chwidth=%d\n"
641#endif
Kiran Kumar Lokereb6445122024-02-06 20:06:27 -0800642 "vht_oper_chwidth=%d\n"
643 "%s",
Xin Dengfec682f2024-02-06 22:59:39 -0800644#ifdef CONFIG_IEEE80211BE
645 (iface_params.hwModeParams.enable80211BE && !is_60Ghz_used) ? 1 : 0,
646#endif
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530647#ifdef CONFIG_IEEE80211AX
648 (iface_params.hwModeParams.enable80211AX && !is_60Ghz_used) ? 1 : 0,
649#endif
Kiran Kumar Lokereb6445122024-02-06 20:06:27 -0800650 iface_params.hwModeParams.enable80211AC ? 1 : 0,
651 (band & band6Ghz) ? "op_class=133" : "");
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530652 break;
653 case ChannelBandwidth::BANDWIDTH_160:
Xin Dengfec682f2024-02-06 22:59:39 -0800654 ht_cap_vht_oper_he_oper_eht_oper_chwidth_as_string = StringPrintf(
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530655 "ht_capab=[HT40+]\n"
Xin Dengfec682f2024-02-06 22:59:39 -0800656#ifdef CONFIG_IEEE80211BE
657 "eht_oper_chwidth=%d\n"
658#endif /* CONFIG_IEEE80211BE */
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530659#ifdef CONFIG_IEEE80211AX
660 "he_oper_chwidth=%d\n"
661#endif
Kiran Kumar Lokereb6445122024-02-06 20:06:27 -0800662 "vht_oper_chwidth=%d\n"
663 "%s",
Xin Dengfec682f2024-02-06 22:59:39 -0800664#ifdef CONFIG_IEEE80211BE
665 (iface_params.hwModeParams.enable80211BE && !is_60Ghz_used) ? 2 : 0,
666#endif
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530667#ifdef CONFIG_IEEE80211AX
668 (iface_params.hwModeParams.enable80211AX && !is_60Ghz_used) ? 2 : 0,
669#endif
Kiran Kumar Lokereb6445122024-02-06 20:06:27 -0800670 iface_params.hwModeParams.enable80211AC ? 2 : 0,
671 (band & band6Ghz) ? "op_class=134" : "");
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530672 break;
673 default:
Sunil Raviffa5cce2022-08-22 23:37:16 +0000674 if (!is_2Ghz_band_only && !is_60Ghz_used) {
675 if (iface_params.hwModeParams.enable80211AC) {
Xin Dengfec682f2024-02-06 22:59:39 -0800676 ht_cap_vht_oper_he_oper_eht_oper_chwidth_as_string =
Sunil Ravi4fc918f2022-04-17 09:26:48 -0700677 "ht_capab=[HT40+]\n"
678 "vht_oper_chwidth=1\n";
Sunil Raviffa5cce2022-08-22 23:37:16 +0000679 }
Kiran Kumar Lokereb6445122024-02-06 20:06:27 -0800680 if (band & band6Ghz) {
Xin Deng3ee3fe12024-02-20 23:24:37 -0800681#ifdef CONFIG_IEEE80211BE
682 if (iface_params.hwModeParams.enable80211BE)
683 ht_cap_vht_oper_he_oper_eht_oper_chwidth_as_string += "op_class=137\n";
684 else
685 ht_cap_vht_oper_he_oper_eht_oper_chwidth_as_string += "op_class=134\n";
686#else /* CONFIG_IEEE80211BE */
Xin Dengfec682f2024-02-06 22:59:39 -0800687 ht_cap_vht_oper_he_oper_eht_oper_chwidth_as_string += "op_class=134\n";
Xin Deng3ee3fe12024-02-20 23:24:37 -0800688#endif /* CONFIG_IEEE80211BE */
Kiran Kumar Lokereb6445122024-02-06 20:06:27 -0800689 }
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530690#ifdef CONFIG_IEEE80211AX
Sunil Raviffa5cce2022-08-22 23:37:16 +0000691 if (iface_params.hwModeParams.enable80211AX) {
Xin Dengfec682f2024-02-06 22:59:39 -0800692 ht_cap_vht_oper_he_oper_eht_oper_chwidth_as_string += "he_oper_chwidth=1\n";
693 }
694#endif
695#ifdef CONFIG_IEEE80211BE
696 if (iface_params.hwModeParams.enable80211BE) {
697 ht_cap_vht_oper_he_oper_eht_oper_chwidth_as_string += "eht_oper_chwidth=1";
Sunil Raviffa5cce2022-08-22 23:37:16 +0000698 }
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530699#endif
Sunil Raviffa5cce2022-08-22 23:37:16 +0000700 }
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530701 break;
702 }
703
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000704#ifdef CONFIG_INTERWORKING
705 std::string access_network_params_as_string;
706 if (nw_params.isMetered) {
707 access_network_params_as_string = StringPrintf(
708 "interworking=1\n"
709 "access_network_type=2\n"); // CHARGEABLE_PUBLIC_NETWORK
710 } else {
711 access_network_params_as_string = StringPrintf(
712 "interworking=0\n");
713 }
714#endif /* CONFIG_INTERWORKING */
715
716 std::string bridge_as_string;
717 if (!br_name.empty()) {
718 bridge_as_string = StringPrintf("bridge=%s", br_name.c_str());
719 }
720
Serik Beketayev8af7a722021-12-23 12:25:36 -0800721 // vendor_elements string
722 std::string vendor_elements_as_string;
723 if (nw_params.vendorElements.size() > 0) {
724 std::stringstream ss;
725 ss << std::hex;
726 ss << std::setfill('0');
727 for (uint8_t b : nw_params.vendorElements) {
728 ss << std::setw(2) << static_cast<unsigned int>(b);
729 }
730 vendor_elements_as_string = StringPrintf("vendor_elements=%s", ss.str().c_str());
731 }
732
Purushottam Kushwaha0316c882021-12-20 15:07:44 +0530733 std::string owe_transition_ifname_as_string;
734 if (!owe_transition_ifname.empty()) {
735 owe_transition_ifname_as_string = StringPrintf(
736 "owe_transition_ifname=%s", owe_transition_ifname.c_str());
737 }
738
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000739 return StringPrintf(
740 "interface=%s\n"
741 "driver=nl80211\n"
742 "ctrl_interface=/data/vendor/wifi/hostapd/ctrl\n"
743 // ssid2 signals to hostapd that the value is not a literal value
744 // for use as a SSID. In this case, we're giving it a hex
745 // std::string and hostapd needs to expect that.
746 "ssid2=%s\n"
747 "%s\n"
748 "ieee80211n=%d\n"
749 "ieee80211ac=%d\n"
750 "%s\n"
751 "%s\n"
752 "%s\n"
Sunil Ravi65251732023-01-24 05:03:35 +0000753 "%s\n"
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000754 "ignore_broadcast_ssid=%d\n"
755 "wowlan_triggers=any\n"
756#ifdef CONFIG_INTERWORKING
757 "%s\n"
758#endif /* CONFIG_INTERWORKING */
759 "%s\n"
760 "%s\n"
761 "%s\n"
Serik Beketayev8af7a722021-12-23 12:25:36 -0800762 "%s\n"
Purushottam Kushwaha0316c882021-12-20 15:07:44 +0530763 "%s\n"
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000764 "%s\n",
765 iface_params.name.c_str(), ssid_as_string.c_str(),
766 channel_config_as_string.c_str(),
767 iface_params.hwModeParams.enable80211N ? 1 : 0,
768 iface_params.hwModeParams.enable80211AC ? 1 : 0,
769 he_params_as_string.c_str(),
Sunil Ravi65251732023-01-24 05:03:35 +0000770 eht_params_as_string.c_str(),
Xin Dengfec682f2024-02-06 22:59:39 -0800771 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 +0000772 nw_params.isHidden ? 1 : 0,
773#ifdef CONFIG_INTERWORKING
774 access_network_params_as_string.c_str(),
775#endif /* CONFIG_INTERWORKING */
776 encryption_config_as_string.c_str(),
777 bridge_as_string.c_str(),
Purushottam Kushwaha0316c882021-12-20 15:07:44 +0530778 owe_transition_ifname_as_string.c_str(),
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000779 enable_edmg_as_string.c_str(),
Serik Beketayev8af7a722021-12-23 12:25:36 -0800780 edmg_channel_as_string.c_str(),
781 vendor_elements_as_string.c_str());
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000782}
783
784Generation getGeneration(hostapd_hw_modes *current_mode)
785{
786 wpa_printf(MSG_DEBUG, "getGeneration hwmode=%d, ht_enabled=%d,"
787 " vht_enabled=%d, he_supported=%d",
788 current_mode->mode, current_mode->ht_capab != 0,
789 current_mode->vht_capab != 0, current_mode->he_capab->he_supported);
790 switch (current_mode->mode) {
791 case HOSTAPD_MODE_IEEE80211B:
792 return Generation::WIFI_STANDARD_LEGACY;
793 case HOSTAPD_MODE_IEEE80211G:
794 return current_mode->ht_capab == 0 ?
795 Generation::WIFI_STANDARD_LEGACY : Generation::WIFI_STANDARD_11N;
796 case HOSTAPD_MODE_IEEE80211A:
797 if (current_mode->he_capab->he_supported) {
798 return Generation::WIFI_STANDARD_11AX;
799 }
800 return current_mode->vht_capab == 0 ?
801 Generation::WIFI_STANDARD_11N : Generation::WIFI_STANDARD_11AC;
802 case HOSTAPD_MODE_IEEE80211AD:
803 return Generation::WIFI_STANDARD_11AD;
804 default:
805 return Generation::WIFI_STANDARD_UNKNOWN;
806 }
807}
808
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800809ChannelBandwidth getChannelBandwidth(struct hostapd_config *iconf)
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000810{
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800811 wpa_printf(MSG_DEBUG, "getChannelBandwidth %d, isHT=%d, isHT40=%d",
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000812 iconf->vht_oper_chwidth, iconf->ieee80211n,
813 iconf->secondary_channel);
814 switch (iconf->vht_oper_chwidth) {
Sunil8cd6f4d2022-06-28 18:40:46 +0000815 case CONF_OPER_CHWIDTH_80MHZ:
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800816 return ChannelBandwidth::BANDWIDTH_80;
Sunil8cd6f4d2022-06-28 18:40:46 +0000817 case CONF_OPER_CHWIDTH_80P80MHZ:
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800818 return ChannelBandwidth::BANDWIDTH_80P80;
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000819 break;
Sunil8cd6f4d2022-06-28 18:40:46 +0000820 case CONF_OPER_CHWIDTH_160MHZ:
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800821 return ChannelBandwidth::BANDWIDTH_160;
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000822 break;
Sunil8cd6f4d2022-06-28 18:40:46 +0000823 case CONF_OPER_CHWIDTH_USE_HT:
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000824 if (iconf->ieee80211n) {
825 return iconf->secondary_channel != 0 ?
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800826 ChannelBandwidth::BANDWIDTH_40 : ChannelBandwidth::BANDWIDTH_20;
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000827 }
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800828 return ChannelBandwidth::BANDWIDTH_20_NOHT;
Sunil8cd6f4d2022-06-28 18:40:46 +0000829 case CONF_OPER_CHWIDTH_2160MHZ:
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800830 return ChannelBandwidth::BANDWIDTH_2160;
Sunil8cd6f4d2022-06-28 18:40:46 +0000831 case CONF_OPER_CHWIDTH_4320MHZ:
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800832 return ChannelBandwidth::BANDWIDTH_4320;
Sunil8cd6f4d2022-06-28 18:40:46 +0000833 case CONF_OPER_CHWIDTH_6480MHZ:
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800834 return ChannelBandwidth::BANDWIDTH_6480;
Sunil8cd6f4d2022-06-28 18:40:46 +0000835 case CONF_OPER_CHWIDTH_8640MHZ:
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800836 return ChannelBandwidth::BANDWIDTH_8640;
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000837 default:
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800838 return ChannelBandwidth::BANDWIDTH_INVALID;
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000839 }
840}
841
842bool forceStaDisconnection(struct hostapd_data* hapd,
843 const std::vector<uint8_t>& client_address,
844 const uint16_t reason_code) {
845 struct sta_info *sta;
Sunil Ravi1a360892022-11-29 20:16:01 +0000846 if (client_address.size() != ETH_ALEN) {
847 return false;
848 }
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000849 for (sta = hapd->sta_list; sta; sta = sta->next) {
850 int res;
851 res = memcmp(sta->addr, client_address.data(), ETH_ALEN);
852 if (res == 0) {
853 wpa_printf(MSG_INFO, "Force client:" MACSTR " disconnect with reason: %d",
854 MAC2STR(client_address.data()), reason_code);
855 ap_sta_disconnect(hapd, sta, sta->addr, reason_code);
856 return true;
857 }
858 }
859 return false;
860}
861
862// hostapd core functions accept "C" style function pointers, so use global
863// functions to pass to the hostapd core function and store the corresponding
864// std::function methods to be invoked.
865//
866// NOTE: Using the pattern from the vendor HAL (wifi_legacy_hal.cpp).
867//
868// Callback to be invoked once setup is complete
869std::function<void(struct hostapd_data*)> on_setup_complete_internal_callback;
870void onAsyncSetupCompleteCb(void* ctx)
871{
872 struct hostapd_data* iface_hapd = (struct hostapd_data*)ctx;
873 if (on_setup_complete_internal_callback) {
874 on_setup_complete_internal_callback(iface_hapd);
875 // Invalidate this callback since we don't want this firing
876 // again in single AP mode.
877 if (strlen(iface_hapd->conf->bridge) > 0) {
878 on_setup_complete_internal_callback = nullptr;
879 }
880 }
881}
882
883// Callback to be invoked on hotspot client connection/disconnection
884std::function<void(struct hostapd_data*, const u8 *mac_addr, int authorized,
885 const u8 *p2p_dev_addr)> on_sta_authorized_internal_callback;
886void onAsyncStaAuthorizedCb(void* ctx, const u8 *mac_addr, int authorized,
Sunil Ravid8128a22023-11-06 23:53:58 +0000887 const u8 *p2p_dev_addr, const u8 *ip)
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000888{
889 struct hostapd_data* iface_hapd = (struct hostapd_data*)ctx;
890 if (on_sta_authorized_internal_callback) {
891 on_sta_authorized_internal_callback(iface_hapd, mac_addr,
892 authorized, p2p_dev_addr);
893 }
894}
895
896std::function<void(struct hostapd_data*, int level,
897 enum wpa_msg_type type, const char *txt,
898 size_t len)> on_wpa_msg_internal_callback;
899
900void onAsyncWpaEventCb(void *ctx, int level,
901 enum wpa_msg_type type, const char *txt,
902 size_t len)
903{
904 struct hostapd_data* iface_hapd = (struct hostapd_data*)ctx;
905 if (on_wpa_msg_internal_callback) {
906 on_wpa_msg_internal_callback(iface_hapd, level,
907 type, txt, len);
908 }
909}
910
911inline ndk::ScopedAStatus createStatus(HostapdStatusCode status_code) {
912 return ndk::ScopedAStatus::fromServiceSpecificError(
913 static_cast<int32_t>(status_code));
914}
915
916inline ndk::ScopedAStatus createStatusWithMsg(
917 HostapdStatusCode status_code, std::string msg)
918{
919 return ndk::ScopedAStatus::fromServiceSpecificErrorWithMessage(
920 static_cast<int32_t>(status_code), msg.c_str());
921}
922
923// Method called by death_notifier_ on client death.
924void onDeath(void* cookie) {
925 wpa_printf(MSG_ERROR, "Client died. Terminating...");
926 eloop_terminate();
927}
928
929} // namespace
930
931namespace aidl {
932namespace android {
933namespace hardware {
934namespace wifi {
935namespace hostapd {
936
937Hostapd::Hostapd(struct hapd_interfaces* interfaces)
938 : interfaces_(interfaces)
939{
940 death_notifier_ = AIBinder_DeathRecipient_new(onDeath);
941}
942
943::ndk::ScopedAStatus Hostapd::addAccessPoint(
944 const IfaceParams& iface_params, const NetworkParams& nw_params)
945{
946 return addAccessPointInternal(iface_params, nw_params);
947}
948
949::ndk::ScopedAStatus Hostapd::removeAccessPoint(const std::string& iface_name)
950{
951 return removeAccessPointInternal(iface_name);
952}
953
954::ndk::ScopedAStatus Hostapd::terminate()
955{
956 wpa_printf(MSG_INFO, "Terminating...");
957 // Clear the callback to avoid IPCThreadState shutdown during the
958 // callback event.
959 callbacks_.clear();
960 eloop_terminate();
961 return ndk::ScopedAStatus::ok();
962}
963
964::ndk::ScopedAStatus Hostapd::registerCallback(
965 const std::shared_ptr<IHostapdCallback>& callback)
966{
967 return registerCallbackInternal(callback);
968}
969
970::ndk::ScopedAStatus Hostapd::forceClientDisconnect(
971 const std::string& iface_name, const std::vector<uint8_t>& client_address,
972 Ieee80211ReasonCode reason_code)
973{
974 return forceClientDisconnectInternal(iface_name, client_address, reason_code);
975}
976
977::ndk::ScopedAStatus Hostapd::setDebugParams(DebugLevel level)
978{
979 return setDebugParamsInternal(level);
980}
981
982::ndk::ScopedAStatus Hostapd::addAccessPointInternal(
983 const IfaceParams& iface_params,
984 const NetworkParams& nw_params)
985{
986 int channelParamsSize = iface_params.channelParams.size();
987 if (channelParamsSize == 1) {
988 // Single AP
989 wpa_printf(MSG_INFO, "AddSingleAccessPoint, iface=%s",
990 iface_params.name.c_str());
991 return addSingleAccessPoint(iface_params, iface_params.channelParams[0],
Purushottam Kushwaha0316c882021-12-20 15:07:44 +0530992 nw_params, "", "");
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000993 } else if (channelParamsSize == 2) {
994 // Concurrent APs
995 wpa_printf(MSG_INFO, "AddDualAccessPoint, iface=%s",
996 iface_params.name.c_str());
997 return addConcurrentAccessPoints(iface_params, nw_params);
998 }
999 return createStatus(HostapdStatusCode::FAILURE_ARGS_INVALID);
1000}
1001
Purushottam Kushwaha0316c882021-12-20 15:07:44 +05301002std::vector<uint8_t> generateRandomOweSsid()
1003{
1004 u8 random[8] = {0};
1005 os_get_random(random, 8);
1006
1007 std::string ssid = StringPrintf("Owe-%s", random);
1008 wpa_printf(MSG_INFO, "Generated OWE SSID: %s", ssid.c_str());
1009 std::vector<uint8_t> vssid(ssid.begin(), ssid.end());
1010
1011 return vssid;
1012}
1013
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001014::ndk::ScopedAStatus Hostapd::addConcurrentAccessPoints(
1015 const IfaceParams& iface_params, const NetworkParams& nw_params)
1016{
1017 int channelParamsListSize = iface_params.channelParams.size();
1018 // Get available interfaces in bridge
1019 std::vector<std::string> managed_interfaces;
1020 std::string br_name = StringPrintf(
1021 "%s", iface_params.name.c_str());
1022 if (!GetInterfacesInBridge(br_name, &managed_interfaces)) {
1023 return createStatusWithMsg(HostapdStatusCode::FAILURE_UNKNOWN,
1024 "Get interfaces in bridge failed.");
1025 }
1026 if (managed_interfaces.size() < channelParamsListSize) {
1027 return createStatusWithMsg(HostapdStatusCode::FAILURE_UNKNOWN,
1028 "Available interfaces less than requested bands");
1029 }
1030 // start BSS on specified bands
1031 for (std::size_t i = 0; i < channelParamsListSize; i ++) {
1032 IfaceParams iface_params_new = iface_params;
Purushottam Kushwaha0316c882021-12-20 15:07:44 +05301033 NetworkParams nw_params_new = nw_params;
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001034 iface_params_new.name = managed_interfaces[i];
Purushottam Kushwaha0316c882021-12-20 15:07:44 +05301035
1036 std::string owe_transition_ifname = "";
Ahmed ElArabawy1aaf1802022-02-04 15:58:55 -08001037 if (nw_params.encryptionType == EncryptionType::WPA3_OWE_TRANSITION) {
Purushottam Kushwaha0316c882021-12-20 15:07:44 +05301038 if (i == 0 && i+1 < channelParamsListSize) {
1039 owe_transition_ifname = managed_interfaces[i+1];
1040 nw_params_new.encryptionType = EncryptionType::NONE;
1041 } else {
1042 owe_transition_ifname = managed_interfaces[0];
1043 nw_params_new.isHidden = true;
1044 nw_params_new.ssid = generateRandomOweSsid();
1045 }
1046 }
1047
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001048 ndk::ScopedAStatus status = addSingleAccessPoint(
Purushottam Kushwaha0316c882021-12-20 15:07:44 +05301049 iface_params_new, iface_params.channelParams[i], nw_params_new,
1050 br_name, owe_transition_ifname);
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001051 if (!status.isOk()) {
1052 wpa_printf(MSG_ERROR, "Failed to addAccessPoint %s",
1053 managed_interfaces[i].c_str());
1054 return status;
1055 }
1056 }
1057 // Save bridge interface info
1058 br_interfaces_[br_name] = managed_interfaces;
1059 return ndk::ScopedAStatus::ok();
1060}
1061
1062::ndk::ScopedAStatus Hostapd::addSingleAccessPoint(
1063 const IfaceParams& iface_params,
1064 const ChannelParams& channelParams,
1065 const NetworkParams& nw_params,
Purushottam Kushwaha0316c882021-12-20 15:07:44 +05301066 const std::string br_name,
1067 const std::string owe_transition_ifname)
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001068{
1069 if (hostapd_get_iface(interfaces_, iface_params.name.c_str())) {
1070 wpa_printf(
1071 MSG_ERROR, "Interface %s already present",
1072 iface_params.name.c_str());
1073 return createStatus(HostapdStatusCode::FAILURE_IFACE_EXISTS);
1074 }
Purushottam Kushwaha0316c882021-12-20 15:07:44 +05301075 const auto conf_params = CreateHostapdConfig(iface_params, channelParams, nw_params,
1076 br_name, owe_transition_ifname);
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001077 if (conf_params.empty()) {
1078 wpa_printf(MSG_ERROR, "Failed to create config params");
1079 return createStatus(HostapdStatusCode::FAILURE_ARGS_INVALID);
1080 }
1081 const auto conf_file_path =
1082 WriteHostapdConfig(iface_params.name, conf_params);
1083 if (conf_file_path.empty()) {
1084 wpa_printf(MSG_ERROR, "Failed to write config file");
1085 return createStatus(HostapdStatusCode::FAILURE_UNKNOWN);
1086 }
1087 std::string add_iface_param_str = StringPrintf(
1088 "%s config=%s", iface_params.name.c_str(),
1089 conf_file_path.c_str());
1090 std::vector<char> add_iface_param_vec(
1091 add_iface_param_str.begin(), add_iface_param_str.end() + 1);
1092 if (hostapd_add_iface(interfaces_, add_iface_param_vec.data()) < 0) {
1093 wpa_printf(
1094 MSG_ERROR, "Adding interface %s failed",
1095 add_iface_param_str.c_str());
1096 return createStatus(HostapdStatusCode::FAILURE_UNKNOWN);
1097 }
1098 struct hostapd_data* iface_hapd =
1099 hostapd_get_iface(interfaces_, iface_params.name.c_str());
1100 WPA_ASSERT(iface_hapd != nullptr && iface_hapd->iface != nullptr);
1101 // Register the setup complete callbacks
1102 on_setup_complete_internal_callback =
1103 [this](struct hostapd_data* iface_hapd) {
1104 wpa_printf(
1105 MSG_INFO, "AP interface setup completed - state %s",
1106 hostapd_state_text(iface_hapd->iface->state));
1107 if (iface_hapd->iface->state == HAPD_IFACE_DISABLED) {
1108 // Invoke the failure callback on all registered
1109 // clients.
1110 for (const auto& callback : callbacks_) {
Chenming Huangbaa5e582024-04-02 08:21:10 +05301111 auto status = callback->onFailure(
1112 strlen(iface_hapd->conf->bridge) > 0 ?
Les Leee08c2862021-10-29 16:36:41 +08001113 iface_hapd->conf->bridge : iface_hapd->conf->iface,
1114 iface_hapd->conf->iface);
Chenming Huangbaa5e582024-04-02 08:21:10 +05301115 if (!status.isOk()) {
1116 wpa_printf(MSG_ERROR, "Failed to invoke onFailure");
1117 }
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001118 }
1119 }
1120 };
1121
1122 // Register for new client connect/disconnect indication.
1123 on_sta_authorized_internal_callback =
1124 [this](struct hostapd_data* iface_hapd, const u8 *mac_addr,
1125 int authorized, const u8 *p2p_dev_addr) {
1126 wpa_printf(MSG_DEBUG, "notify client " MACSTR " %s",
1127 MAC2STR(mac_addr),
1128 (authorized) ? "Connected" : "Disconnected");
1129 ClientInfo info;
1130 info.ifaceName = strlen(iface_hapd->conf->bridge) > 0 ?
1131 iface_hapd->conf->bridge : iface_hapd->conf->iface;
1132 info.apIfaceInstance = iface_hapd->conf->iface;
1133 info.clientAddress.assign(mac_addr, mac_addr + ETH_ALEN);
1134 info.isConnected = authorized;
1135 for (const auto &callback : callbacks_) {
Chenming Huangbaa5e582024-04-02 08:21:10 +05301136 auto status = callback->onConnectedClientsChanged(info);
1137 if (!status.isOk()) {
1138 wpa_printf(MSG_ERROR, "Failed to invoke onConnectedClientsChanged");
1139 }
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001140 }
1141 };
1142
1143 // Register for wpa_event which used to get channel switch event
1144 on_wpa_msg_internal_callback =
1145 [this](struct hostapd_data* iface_hapd, int level,
1146 enum wpa_msg_type type, const char *txt,
1147 size_t len) {
1148 wpa_printf(MSG_DEBUG, "Receive wpa msg : %s", txt);
1149 if (os_strncmp(txt, AP_EVENT_ENABLED,
1150 strlen(AP_EVENT_ENABLED)) == 0 ||
1151 os_strncmp(txt, WPA_EVENT_CHANNEL_SWITCH,
1152 strlen(WPA_EVENT_CHANNEL_SWITCH)) == 0) {
1153 ApInfo info;
1154 info.ifaceName = strlen(iface_hapd->conf->bridge) > 0 ?
1155 iface_hapd->conf->bridge : iface_hapd->conf->iface,
1156 info.apIfaceInstance = iface_hapd->conf->iface;
1157 info.freqMhz = iface_hapd->iface->freq;
Ahmed ElArabawyb4115792022-02-08 09:33:01 -08001158 info.channelBandwidth = getChannelBandwidth(iface_hapd->iconf);
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001159 info.generation = getGeneration(iface_hapd->iface->current_mode);
1160 info.apIfaceInstanceMacAddress.assign(iface_hapd->own_addr,
1161 iface_hapd->own_addr + ETH_ALEN);
1162 for (const auto &callback : callbacks_) {
Chenming Huangbaa5e582024-04-02 08:21:10 +05301163 auto status = callback->onApInstanceInfoChanged(info);
1164 if (!status.isOk()) {
1165 wpa_printf(MSG_ERROR,
1166 "Failed to invoke onApInstanceInfoChanged");
1167 }
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001168 }
Les Leea0c90cb2022-04-19 17:39:23 +08001169 } else if (os_strncmp(txt, AP_EVENT_DISABLED, strlen(AP_EVENT_DISABLED)) == 0
1170 || os_strncmp(txt, INTERFACE_DISABLED, strlen(INTERFACE_DISABLED)) == 0)
1171 {
Yu Ouyang378d3c42021-08-20 17:31:08 +08001172 // Invoke the failure callback on all registered clients.
1173 for (const auto& callback : callbacks_) {
Chenming Huangbaa5e582024-04-02 08:21:10 +05301174 auto status = callback->onFailure(strlen(iface_hapd->conf->bridge) > 0 ?
Les Leee08c2862021-10-29 16:36:41 +08001175 iface_hapd->conf->bridge : iface_hapd->conf->iface,
1176 iface_hapd->conf->iface);
Chenming Huangbaa5e582024-04-02 08:21:10 +05301177 if (!status.isOk()) {
1178 wpa_printf(MSG_ERROR, "Failed to invoke onFailure");
1179 }
Yu Ouyang378d3c42021-08-20 17:31:08 +08001180 }
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001181 }
Yu Ouyang378d3c42021-08-20 17:31:08 +08001182 };
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001183
1184 // Setup callback
1185 iface_hapd->setup_complete_cb = onAsyncSetupCompleteCb;
1186 iface_hapd->setup_complete_cb_ctx = iface_hapd;
1187 iface_hapd->sta_authorized_cb = onAsyncStaAuthorizedCb;
1188 iface_hapd->sta_authorized_cb_ctx = iface_hapd;
Hu Wang7c5a4322021-06-24 17:24:59 +08001189 wpa_msg_register_aidl_cb(onAsyncWpaEventCb);
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001190
1191 if (hostapd_enable_iface(iface_hapd->iface) < 0) {
1192 wpa_printf(
1193 MSG_ERROR, "Enabling interface %s failed",
1194 iface_params.name.c_str());
1195 return createStatus(HostapdStatusCode::FAILURE_UNKNOWN);
1196 }
1197 return ndk::ScopedAStatus::ok();
1198}
1199
1200::ndk::ScopedAStatus Hostapd::removeAccessPointInternal(const std::string& iface_name)
1201{
1202 // interfaces to be removed
1203 std::vector<std::string> interfaces;
1204 bool is_error = false;
1205
1206 const auto it = br_interfaces_.find(iface_name);
1207 if (it != br_interfaces_.end()) {
1208 // In case bridge, remove managed interfaces
1209 interfaces = it->second;
1210 br_interfaces_.erase(iface_name);
1211 } else {
1212 // else remove current interface
1213 interfaces.push_back(iface_name);
1214 }
1215
1216 for (auto& iface : interfaces) {
1217 std::vector<char> remove_iface_param_vec(
1218 iface.begin(), iface.end() + 1);
1219 if (hostapd_remove_iface(interfaces_, remove_iface_param_vec.data()) < 0) {
1220 wpa_printf(MSG_INFO, "Remove interface %s failed", iface.c_str());
1221 is_error = true;
1222 }
1223 }
1224 if (is_error) {
1225 return createStatus(HostapdStatusCode::FAILURE_UNKNOWN);
1226 }
1227 return ndk::ScopedAStatus::ok();
1228}
1229
1230::ndk::ScopedAStatus Hostapd::registerCallbackInternal(
1231 const std::shared_ptr<IHostapdCallback>& callback)
1232{
1233 binder_status_t status = AIBinder_linkToDeath(callback->asBinder().get(),
1234 death_notifier_, this /* cookie */);
1235 if (status != STATUS_OK) {
1236 wpa_printf(
1237 MSG_ERROR,
1238 "Error registering for death notification for "
1239 "hostapd callback object");
1240 return createStatus(HostapdStatusCode::FAILURE_UNKNOWN);
1241 }
1242 callbacks_.push_back(callback);
Manaswini Paluri76ae6982024-02-29 14:49:20 +05301243 if (aidl_service_version == 0) {
1244 aidl_service_version = Hostapd::version;
1245 wpa_printf(MSG_INFO, "AIDL service version: %d", aidl_service_version);
1246 }
1247 if (aidl_client_version == 0) {
1248 callback->getInterfaceVersion(&aidl_client_version);
1249 wpa_printf(MSG_INFO, "AIDL client version: %d", aidl_client_version);
1250 }
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001251 return ndk::ScopedAStatus::ok();
1252}
1253
1254::ndk::ScopedAStatus Hostapd::forceClientDisconnectInternal(const std::string& iface_name,
1255 const std::vector<uint8_t>& client_address, Ieee80211ReasonCode reason_code)
1256{
1257 struct hostapd_data *hapd = hostapd_get_iface(interfaces_, iface_name.c_str());
1258 bool result;
1259 if (!hapd) {
1260 for (auto const& iface : br_interfaces_) {
1261 if (iface.first == iface_name) {
1262 for (auto const& instance : iface.second) {
1263 hapd = hostapd_get_iface(interfaces_, instance.c_str());
1264 if (hapd) {
1265 result = forceStaDisconnection(hapd, client_address,
1266 (uint16_t) reason_code);
1267 if (result) break;
1268 }
1269 }
1270 }
1271 }
1272 } else {
1273 result = forceStaDisconnection(hapd, client_address, (uint16_t) reason_code);
1274 }
1275 if (!hapd) {
1276 wpa_printf(MSG_ERROR, "Interface %s doesn't exist", iface_name.c_str());
1277 return createStatus(HostapdStatusCode::FAILURE_IFACE_UNKNOWN);
1278 }
1279 if (result) {
1280 return ndk::ScopedAStatus::ok();
1281 }
1282 return createStatus(HostapdStatusCode::FAILURE_CLIENT_UNKNOWN);
1283}
1284
1285::ndk::ScopedAStatus Hostapd::setDebugParamsInternal(DebugLevel level)
1286{
1287 wpa_debug_level = static_cast<uint32_t>(level);
1288 return ndk::ScopedAStatus::ok();
1289}
1290
1291} // namespace hostapd
1292} // namespace wifi
1293} // namespace hardware
1294} // namespace android
Les Leee08c2862021-10-29 16:36:41 +08001295} // namespace aidl