blob: fa0c75615c606ba89948e6443a6ca970f5c64944 [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
64#define MAX_PORTS 1024
65bool GetInterfacesInBridge(std::string br_name,
66 std::vector<std::string>* interfaces) {
67 android::base::unique_fd sock(socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0));
68 if (sock.get() < 0) {
69 wpa_printf(MSG_ERROR, "Failed to create sock (%s) in %s",
70 strerror(errno), __FUNCTION__);
71 return false;
72 }
73
74 struct ifreq request;
75 int i, ifindices[MAX_PORTS];
76 char if_name[IFNAMSIZ];
77 unsigned long args[3];
78
79 memset(ifindices, 0, MAX_PORTS * sizeof(int));
80
81 args[0] = BRCTL_GET_PORT_LIST;
82 args[1] = (unsigned long) ifindices;
83 args[2] = MAX_PORTS;
84
85 strlcpy(request.ifr_name, br_name.c_str(), IFNAMSIZ);
86 request.ifr_data = (char *)args;
87
88 if (ioctl(sock.get(), SIOCDEVPRIVATE, &request) < 0) {
89 wpa_printf(MSG_ERROR, "Failed to ioctl SIOCDEVPRIVATE in %s",
90 __FUNCTION__);
91 return false;
92 }
93
94 for (i = 0; i < MAX_PORTS; i ++) {
95 memset(if_name, 0, IFNAMSIZ);
96 if (ifindices[i] == 0 || !if_indextoname(ifindices[i], if_name)) {
97 continue;
98 }
99 interfaces->push_back(if_name);
100 }
101 return true;
102}
103
104std::string WriteHostapdConfig(
105 const std::string& interface_name, const std::string& config)
106{
107 const std::string file_path =
108 StringPrintf(kConfFileNameFmt, interface_name.c_str());
109 if (WriteStringToFile(
110 config, file_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
111 getuid(), getgid())) {
112 return file_path;
113 }
114 // Diagnose failure
115 int error = errno;
116 wpa_printf(
117 MSG_ERROR, "Cannot write hostapd config to %s, error: %s",
118 file_path.c_str(), strerror(error));
119 struct stat st;
120 int result = stat(file_path.c_str(), &st);
121 if (result == 0) {
122 wpa_printf(
123 MSG_ERROR, "hostapd config file uid: %d, gid: %d, mode: %d",
124 st.st_uid, st.st_gid, st.st_mode);
125 } else {
126 wpa_printf(
127 MSG_ERROR,
128 "Error calling stat() on hostapd config file: %s",
129 strerror(errno));
130 }
131 return "";
132}
133
134/*
135 * Get the op_class for a channel/band
136 * The logic here is based on Table E-4 in the 802.11 Specification
137 */
138int getOpClassForChannel(int channel, int band, bool support11n, bool support11ac) {
139 // 2GHz Band
140 if ((band & band2Ghz) != 0) {
141 if (channel == 14) {
142 return 82;
143 }
144 if (channel >= 1 && channel <= 13) {
145 if (!support11n) {
146 //20MHz channel
147 return 81;
148 }
149 if (channel <= 9) {
150 // HT40 with secondary channel above primary
151 return 83;
152 }
153 // HT40 with secondary channel below primary
154 return 84;
155 }
156 // Error
157 return 0;
158 }
159
160 // 5GHz Band
161 if ((band & band5Ghz) != 0) {
162 if (support11ac) {
163 switch (channel) {
164 case 42:
165 case 58:
166 case 106:
167 case 122:
168 case 138:
169 case 155:
170 // 80MHz channel
171 return 128;
172 case 50:
173 case 114:
174 // 160MHz channel
175 return 129;
176 }
177 }
178
179 if (!support11n) {
180 if (channel >= 36 && channel <= 48) {
181 return 115;
182 }
183 if (channel >= 52 && channel <= 64) {
184 return 118;
185 }
186 if (channel >= 100 && channel <= 144) {
187 return 121;
188 }
189 if (channel >= 149 && channel <= 161) {
190 return 124;
191 }
192 if (channel >= 165 && channel <= 169) {
193 return 125;
194 }
195 } else {
196 switch (channel) {
197 case 36:
198 case 44:
199 // HT40 with secondary channel above primary
200 return 116;
201 case 40:
202 case 48:
203 // HT40 with secondary channel below primary
204 return 117;
205 case 52:
206 case 60:
207 // HT40 with secondary channel above primary
208 return 119;
209 case 56:
210 case 64:
211 // HT40 with secondary channel below primary
212 return 120;
213 case 100:
214 case 108:
215 case 116:
216 case 124:
217 case 132:
218 case 140:
219 // HT40 with secondary channel above primary
220 return 122;
221 case 104:
222 case 112:
223 case 120:
224 case 128:
225 case 136:
226 case 144:
227 // HT40 with secondary channel below primary
228 return 123;
229 case 149:
230 case 157:
231 // HT40 with secondary channel above primary
232 return 126;
233 case 153:
234 case 161:
235 // HT40 with secondary channel below primary
236 return 127;
237 }
238 }
239 // Error
240 return 0;
241 }
242
243 // 6GHz Band
244 if ((band & band6Ghz) != 0) {
245 // Channels 1, 5. 9, 13, ...
246 if ((channel & 0x03) == 0x01) {
247 // 20MHz channel
248 return 131;
249 }
250 // Channels 3, 11, 19, 27, ...
251 if ((channel & 0x07) == 0x03) {
252 // 40MHz channel
253 return 132;
254 }
255 // Channels 7, 23, 39, 55, ...
256 if ((channel & 0x0F) == 0x07) {
257 // 80MHz channel
258 return 133;
259 }
260 // Channels 15, 47, 69, ...
261 if ((channel & 0x1F) == 0x0F) {
262 // 160MHz channel
263 return 134;
264 }
265 if (channel == 2) {
266 // 20MHz channel
267 return 136;
268 }
269 // Error
270 return 0;
271 }
272
273 if ((band & band60Ghz) != 0) {
274 if (1 <= channel && channel <= 8) {
275 return 180;
276 } else if (9 <= channel && channel <= 15) {
277 return 181;
278 } else if (17 <= channel && channel <= 22) {
279 return 182;
280 } else if (25 <= channel && channel <= 29) {
281 return 183;
282 }
283 // Error
284 return 0;
285 }
286
287 return 0;
288}
289
290bool validatePassphrase(int passphrase_len, int min_len, int max_len)
291{
292 if (min_len != -1 && passphrase_len < min_len) return false;
293 if (max_len != -1 && passphrase_len > max_len) return false;
294 return true;
295}
296
297std::string CreateHostapdConfig(
298 const IfaceParams& iface_params,
299 const ChannelParams& channelParams,
300 const NetworkParams& nw_params,
Purushottam Kushwaha0316c882021-12-20 15:07:44 +0530301 const std::string br_name,
302 const std::string owe_transition_ifname)
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000303{
304 if (nw_params.ssid.size() >
305 static_cast<uint32_t>(
306 ParamSizeLimits::SSID_MAX_LEN_IN_BYTES)) {
307 wpa_printf(
308 MSG_ERROR, "Invalid SSID size: %zu", nw_params.ssid.size());
309 return "";
310 }
311
312 // SSID string
313 std::stringstream ss;
314 ss << std::hex;
315 ss << std::setfill('0');
316 for (uint8_t b : nw_params.ssid) {
317 ss << std::setw(2) << static_cast<unsigned int>(b);
318 }
319 const std::string ssid_as_string = ss.str();
320
321 // Encryption config string
322 uint32_t band = 0;
323 band |= static_cast<uint32_t>(channelParams.bandMask);
Sunil Ravi4fc918f2022-04-17 09:26:48 -0700324 bool is_2Ghz_band_only = band == static_cast<uint32_t>(band2Ghz);
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000325 bool is_6Ghz_band_only = band == static_cast<uint32_t>(band6Ghz);
326 bool is_60Ghz_band_only = band == static_cast<uint32_t>(band60Ghz);
327 std::string encryption_config_as_string;
328 switch (nw_params.encryptionType) {
329 case EncryptionType::NONE:
330 // no security params
331 break;
332 case EncryptionType::WPA:
333 if (!validatePassphrase(
334 nw_params.passphrase.size(),
335 static_cast<uint32_t>(ParamSizeLimits::
336 WPA2_PSK_PASSPHRASE_MIN_LEN_IN_BYTES),
337 static_cast<uint32_t>(ParamSizeLimits::
338 WPA2_PSK_PASSPHRASE_MAX_LEN_IN_BYTES))) {
339 return "";
340 }
341 encryption_config_as_string = StringPrintf(
342 "wpa=3\n"
343 "wpa_pairwise=%s\n"
344 "wpa_passphrase=%s",
345 is_60Ghz_band_only ? "GCMP" : "TKIP CCMP",
346 nw_params.passphrase.c_str());
347 break;
348 case EncryptionType::WPA2:
349 if (!validatePassphrase(
350 nw_params.passphrase.size(),
351 static_cast<uint32_t>(ParamSizeLimits::
352 WPA2_PSK_PASSPHRASE_MIN_LEN_IN_BYTES),
353 static_cast<uint32_t>(ParamSizeLimits::
354 WPA2_PSK_PASSPHRASE_MAX_LEN_IN_BYTES))) {
355 return "";
356 }
357 encryption_config_as_string = StringPrintf(
358 "wpa=2\n"
359 "rsn_pairwise=%s\n"
Sunil Ravib3580db2022-01-28 12:25:46 -0800360#ifdef ENABLE_HOSTAPD_CONFIG_80211W_MFP_OPTIONAL
361 "ieee80211w=1\n"
362#endif
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000363 "wpa_passphrase=%s",
364 is_60Ghz_band_only ? "GCMP" : "CCMP",
365 nw_params.passphrase.c_str());
366 break;
367 case EncryptionType::WPA3_SAE_TRANSITION:
368 if (!validatePassphrase(
369 nw_params.passphrase.size(),
370 static_cast<uint32_t>(ParamSizeLimits::
371 WPA2_PSK_PASSPHRASE_MIN_LEN_IN_BYTES),
372 static_cast<uint32_t>(ParamSizeLimits::
373 WPA2_PSK_PASSPHRASE_MAX_LEN_IN_BYTES))) {
374 return "";
375 }
Sunil Ravid917c832023-07-07 17:30:33 +0000376 // WPA3 transition mode or SAE+WPA_PSK key management(AKM) is not allowed in 6GHz.
377 // Auto-convert any such configurations to SAE.
378 if ((band & band6Ghz) != 0) {
379 wpa_printf(MSG_INFO, "WPA3_SAE_TRANSITION configured in 6GHz band."
380 "Enable only SAE in key_mgmt");
381 encryption_config_as_string = StringPrintf(
382 "wpa=2\n"
383 "rsn_pairwise=CCMP\n"
384 "wpa_key_mgmt=%s\n"
385 "ieee80211w=2\n"
386 "sae_require_mfp=2\n"
387 "sae_pwe=%d\n"
388 "sae_password=%s",
Sunil Ravic1edd3e2023-02-06 18:52:51 +0000389#ifdef CONFIG_IEEE80211BE
Sunil Ravid917c832023-07-07 17:30:33 +0000390 iface_params.hwModeParams.enable80211BE ?
391 "SAE SAE-EXT-KEY" : "SAE",
Sunil Ravic1edd3e2023-02-06 18:52:51 +0000392#else
Sunil Ravid917c832023-07-07 17:30:33 +0000393 "SAE",
Sunil Ravic1edd3e2023-02-06 18:52:51 +0000394#endif
Sunil Ravid917c832023-07-07 17:30:33 +0000395 is_6Ghz_band_only ? 1 : 2,
396 nw_params.passphrase.c_str());
397 } else {
398 encryption_config_as_string = StringPrintf(
399 "wpa=2\n"
400 "rsn_pairwise=%s\n"
401 "wpa_key_mgmt=%s\n"
402 "ieee80211w=1\n"
403 "sae_require_mfp=1\n"
404 "wpa_passphrase=%s\n"
405 "sae_password=%s",
406 is_60Ghz_band_only ? "GCMP" : "CCMP",
407#ifdef CONFIG_IEEE80211BE
408 iface_params.hwModeParams.enable80211BE ?
409 "WPA-PSK SAE SAE-EXT-KEY" : "WPA-PSK SAE",
410#else
411 "WPA-PSK SAE",
412#endif
413 nw_params.passphrase.c_str(),
414 nw_params.passphrase.c_str());
415 }
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000416 break;
417 case EncryptionType::WPA3_SAE:
418 if (!validatePassphrase(nw_params.passphrase.size(), 1, -1)) {
419 return "";
420 }
421 encryption_config_as_string = StringPrintf(
422 "wpa=2\n"
423 "rsn_pairwise=%s\n"
Sunil Ravi65251732023-01-24 05:03:35 +0000424 "wpa_key_mgmt=%s\n"
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000425 "ieee80211w=2\n"
426 "sae_require_mfp=2\n"
427 "sae_pwe=%d\n"
428 "sae_password=%s",
429 is_60Ghz_band_only ? "GCMP" : "CCMP",
Sunil Ravi65251732023-01-24 05:03:35 +0000430#ifdef CONFIG_IEEE80211BE
431 iface_params.hwModeParams.enable80211BE ? "SAE SAE-EXT-KEY" : "SAE",
432#else
433 "SAE",
434#endif
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000435 is_6Ghz_band_only ? 1 : 2,
436 nw_params.passphrase.c_str());
437 break;
Ahmed ElArabawy1aaf1802022-02-04 15:58:55 -0800438 case EncryptionType::WPA3_OWE_TRANSITION:
439 encryption_config_as_string = StringPrintf(
440 "wpa=2\n"
441 "rsn_pairwise=%s\n"
442 "wpa_key_mgmt=OWE\n"
443 "ieee80211w=2",
444 is_60Ghz_band_only ? "GCMP" : "CCMP");
445 break;
446 case EncryptionType::WPA3_OWE:
Purushottam Kushwaha0316c882021-12-20 15:07:44 +0530447 encryption_config_as_string = StringPrintf(
448 "wpa=2\n"
449 "rsn_pairwise=%s\n"
450 "wpa_key_mgmt=OWE\n"
451 "ieee80211w=2",
452 is_60Ghz_band_only ? "GCMP" : "CCMP");
453 break;
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000454 default:
455 wpa_printf(MSG_ERROR, "Unknown encryption type");
456 return "";
457 }
458
459 std::string channel_config_as_string;
460 bool isFirst = true;
461 if (channelParams.enableAcs) {
462 std::string freqList_as_string;
463 for (const auto &range :
464 channelParams.acsChannelFreqRangesMhz) {
465 if (!isFirst) {
466 freqList_as_string += ",";
467 }
468 isFirst = false;
469
470 if (range.startMhz != range.endMhz) {
471 freqList_as_string +=
472 StringPrintf("%d-%d", range.startMhz, range.endMhz);
473 } else {
474 freqList_as_string += StringPrintf("%d", range.startMhz);
475 }
476 }
477 channel_config_as_string = StringPrintf(
478 "channel=0\n"
479 "acs_exclude_dfs=%d\n"
480 "freqlist=%s",
481 channelParams.acsShouldExcludeDfs,
482 freqList_as_string.c_str());
483 } else {
484 int op_class = getOpClassForChannel(
485 channelParams.channel,
486 band,
487 iface_params.hwModeParams.enable80211N,
488 iface_params.hwModeParams.enable80211AC);
489 channel_config_as_string = StringPrintf(
490 "channel=%d\n"
491 "op_class=%d",
492 channelParams.channel, op_class);
493 }
494
495 std::string hw_mode_as_string;
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000496 std::string enable_edmg_as_string;
497 std::string edmg_channel_as_string;
498 bool is_60Ghz_used = false;
499
500 if (((band & band60Ghz) != 0)) {
501 hw_mode_as_string = "hw_mode=ad";
502 if (iface_params.hwModeParams.enableEdmg) {
503 enable_edmg_as_string = "enable_edmg=1";
504 edmg_channel_as_string = StringPrintf(
505 "edmg_channel=%d",
506 channelParams.channel);
507 }
508 is_60Ghz_used = true;
509 } else if ((band & band2Ghz) != 0) {
510 if (((band & band5Ghz) != 0)
511 || ((band & band6Ghz) != 0)) {
512 hw_mode_as_string = "hw_mode=any";
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000513 } else {
514 hw_mode_as_string = "hw_mode=g";
515 }
516 } else if (((band & band5Ghz) != 0)
517 || ((band & band6Ghz) != 0)) {
518 hw_mode_as_string = "hw_mode=a";
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000519 } else {
520 wpa_printf(MSG_ERROR, "Invalid band");
521 return "";
522 }
523
524 std::string he_params_as_string;
525#ifdef CONFIG_IEEE80211AX
526 if (iface_params.hwModeParams.enable80211AX && !is_60Ghz_used) {
527 he_params_as_string = StringPrintf(
528 "ieee80211ax=1\n"
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000529 "he_su_beamformer=%d\n"
530 "he_su_beamformee=%d\n"
531 "he_mu_beamformer=%d\n"
532 "he_twt_required=%d\n",
533 iface_params.hwModeParams.enableHeSingleUserBeamformer ? 1 : 0,
534 iface_params.hwModeParams.enableHeSingleUserBeamformee ? 1 : 0,
535 iface_params.hwModeParams.enableHeMultiUserBeamformer ? 1 : 0,
536 iface_params.hwModeParams.enableHeTargetWakeTime ? 1 : 0);
537 } else {
538 he_params_as_string = "ieee80211ax=0";
539 }
540#endif /* CONFIG_IEEE80211AX */
Sunil Ravi65251732023-01-24 05:03:35 +0000541 std::string eht_params_as_string;
542#ifdef CONFIG_IEEE80211BE
543 if (iface_params.hwModeParams.enable80211BE && !is_60Ghz_used) {
544 eht_params_as_string = "ieee80211be=1";
545 /* TODO set eht_su_beamformer, eht_su_beamformee, eht_mu_beamformer */
546 } else {
547 eht_params_as_string = "ieee80211be=0";
548 }
549#endif /* CONFIG_IEEE80211BE */
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000550
Xin Dengfec682f2024-02-06 22:59:39 -0800551 std::string ht_cap_vht_oper_he_oper_eht_oper_chwidth_as_string;
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530552 switch (iface_params.hwModeParams.maximumChannelBandwidth) {
553 case ChannelBandwidth::BANDWIDTH_20:
Xin Dengfec682f2024-02-06 22:59:39 -0800554 ht_cap_vht_oper_he_oper_eht_oper_chwidth_as_string = StringPrintf(
555#ifdef CONFIG_IEEE80211BE
556 "eht_oper_chwidth=0\n"
557#endif /* CONFIG_IEEE80211BE */
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530558#ifdef CONFIG_IEEE80211AX
559 "he_oper_chwidth=0\n"
560#endif
Kiran Kumar Lokereb6445122024-02-06 20:06:27 -0800561 "vht_oper_chwidth=0\n"
562 "%s", (band & band6Ghz) ? "op_class=131" : "");
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530563 break;
564 case ChannelBandwidth::BANDWIDTH_40:
Xin Dengfec682f2024-02-06 22:59:39 -0800565 ht_cap_vht_oper_he_oper_eht_oper_chwidth_as_string = StringPrintf(
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530566 "ht_capab=[HT40+]\n"
Xin Dengfec682f2024-02-06 22:59:39 -0800567#ifdef CONFIG_IEEE80211BE
568 "eht_oper_chwidth=0\n"
569#endif /* CONFIG_IEEE80211BE */
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530570#ifdef CONFIG_IEEE80211AX
571 "he_oper_chwidth=0\n"
572#endif
Kiran Kumar Lokereb6445122024-02-06 20:06:27 -0800573 "vht_oper_chwidth=0\n"
574 "%s", (band & band6Ghz) ? "op_class=132" : "");
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530575 break;
576 case ChannelBandwidth::BANDWIDTH_80:
Xin Dengfec682f2024-02-06 22:59:39 -0800577 ht_cap_vht_oper_he_oper_eht_oper_chwidth_as_string = StringPrintf(
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530578 "ht_capab=[HT40+]\n"
Xin Dengfec682f2024-02-06 22:59:39 -0800579#ifdef CONFIG_IEEE80211BE
580 "eht_oper_chwidth=%d\n"
581#endif /* CONFIG_IEEE80211BE */
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530582#ifdef CONFIG_IEEE80211AX
583 "he_oper_chwidth=%d\n"
584#endif
Kiran Kumar Lokereb6445122024-02-06 20:06:27 -0800585 "vht_oper_chwidth=%d\n"
586 "%s",
Xin Dengfec682f2024-02-06 22:59:39 -0800587#ifdef CONFIG_IEEE80211BE
588 (iface_params.hwModeParams.enable80211BE && !is_60Ghz_used) ? 1 : 0,
589#endif
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530590#ifdef CONFIG_IEEE80211AX
591 (iface_params.hwModeParams.enable80211AX && !is_60Ghz_used) ? 1 : 0,
592#endif
Kiran Kumar Lokereb6445122024-02-06 20:06:27 -0800593 iface_params.hwModeParams.enable80211AC ? 1 : 0,
594 (band & band6Ghz) ? "op_class=133" : "");
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530595 break;
596 case ChannelBandwidth::BANDWIDTH_160:
Xin Dengfec682f2024-02-06 22:59:39 -0800597 ht_cap_vht_oper_he_oper_eht_oper_chwidth_as_string = StringPrintf(
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530598 "ht_capab=[HT40+]\n"
Xin Dengfec682f2024-02-06 22:59:39 -0800599#ifdef CONFIG_IEEE80211BE
600 "eht_oper_chwidth=%d\n"
601#endif /* CONFIG_IEEE80211BE */
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530602#ifdef CONFIG_IEEE80211AX
603 "he_oper_chwidth=%d\n"
604#endif
Kiran Kumar Lokereb6445122024-02-06 20:06:27 -0800605 "vht_oper_chwidth=%d\n"
606 "%s",
Xin Dengfec682f2024-02-06 22:59:39 -0800607#ifdef CONFIG_IEEE80211BE
608 (iface_params.hwModeParams.enable80211BE && !is_60Ghz_used) ? 2 : 0,
609#endif
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530610#ifdef CONFIG_IEEE80211AX
611 (iface_params.hwModeParams.enable80211AX && !is_60Ghz_used) ? 2 : 0,
612#endif
Kiran Kumar Lokereb6445122024-02-06 20:06:27 -0800613 iface_params.hwModeParams.enable80211AC ? 2 : 0,
614 (band & band6Ghz) ? "op_class=134" : "");
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530615 break;
616 default:
Sunil Raviffa5cce2022-08-22 23:37:16 +0000617 if (!is_2Ghz_band_only && !is_60Ghz_used) {
618 if (iface_params.hwModeParams.enable80211AC) {
Xin Dengfec682f2024-02-06 22:59:39 -0800619 ht_cap_vht_oper_he_oper_eht_oper_chwidth_as_string =
Sunil Ravi4fc918f2022-04-17 09:26:48 -0700620 "ht_capab=[HT40+]\n"
621 "vht_oper_chwidth=1\n";
Sunil Raviffa5cce2022-08-22 23:37:16 +0000622 }
Kiran Kumar Lokereb6445122024-02-06 20:06:27 -0800623 if (band & band6Ghz) {
Xin Deng3ee3fe12024-02-20 23:24:37 -0800624#ifdef CONFIG_IEEE80211BE
625 if (iface_params.hwModeParams.enable80211BE)
626 ht_cap_vht_oper_he_oper_eht_oper_chwidth_as_string += "op_class=137\n";
627 else
628 ht_cap_vht_oper_he_oper_eht_oper_chwidth_as_string += "op_class=134\n";
629#else /* CONFIG_IEEE80211BE */
Xin Dengfec682f2024-02-06 22:59:39 -0800630 ht_cap_vht_oper_he_oper_eht_oper_chwidth_as_string += "op_class=134\n";
Xin Deng3ee3fe12024-02-20 23:24:37 -0800631#endif /* CONFIG_IEEE80211BE */
Kiran Kumar Lokereb6445122024-02-06 20:06:27 -0800632 }
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530633#ifdef CONFIG_IEEE80211AX
Sunil Raviffa5cce2022-08-22 23:37:16 +0000634 if (iface_params.hwModeParams.enable80211AX) {
Xin Dengfec682f2024-02-06 22:59:39 -0800635 ht_cap_vht_oper_he_oper_eht_oper_chwidth_as_string += "he_oper_chwidth=1\n";
636 }
637#endif
638#ifdef CONFIG_IEEE80211BE
639 if (iface_params.hwModeParams.enable80211BE) {
640 ht_cap_vht_oper_he_oper_eht_oper_chwidth_as_string += "eht_oper_chwidth=1";
Sunil Raviffa5cce2022-08-22 23:37:16 +0000641 }
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530642#endif
Sunil Raviffa5cce2022-08-22 23:37:16 +0000643 }
Purushottam Kushwaha90c710b2022-02-04 19:00:34 +0530644 break;
645 }
646
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000647#ifdef CONFIG_INTERWORKING
648 std::string access_network_params_as_string;
649 if (nw_params.isMetered) {
650 access_network_params_as_string = StringPrintf(
651 "interworking=1\n"
652 "access_network_type=2\n"); // CHARGEABLE_PUBLIC_NETWORK
653 } else {
654 access_network_params_as_string = StringPrintf(
655 "interworking=0\n");
656 }
657#endif /* CONFIG_INTERWORKING */
658
659 std::string bridge_as_string;
660 if (!br_name.empty()) {
661 bridge_as_string = StringPrintf("bridge=%s", br_name.c_str());
662 }
663
Serik Beketayev8af7a722021-12-23 12:25:36 -0800664 // vendor_elements string
665 std::string vendor_elements_as_string;
666 if (nw_params.vendorElements.size() > 0) {
667 std::stringstream ss;
668 ss << std::hex;
669 ss << std::setfill('0');
670 for (uint8_t b : nw_params.vendorElements) {
671 ss << std::setw(2) << static_cast<unsigned int>(b);
672 }
673 vendor_elements_as_string = StringPrintf("vendor_elements=%s", ss.str().c_str());
674 }
675
Purushottam Kushwaha0316c882021-12-20 15:07:44 +0530676 std::string owe_transition_ifname_as_string;
677 if (!owe_transition_ifname.empty()) {
678 owe_transition_ifname_as_string = StringPrintf(
679 "owe_transition_ifname=%s", owe_transition_ifname.c_str());
680 }
681
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000682 return StringPrintf(
683 "interface=%s\n"
684 "driver=nl80211\n"
685 "ctrl_interface=/data/vendor/wifi/hostapd/ctrl\n"
686 // ssid2 signals to hostapd that the value is not a literal value
687 // for use as a SSID. In this case, we're giving it a hex
688 // std::string and hostapd needs to expect that.
689 "ssid2=%s\n"
690 "%s\n"
691 "ieee80211n=%d\n"
692 "ieee80211ac=%d\n"
693 "%s\n"
694 "%s\n"
695 "%s\n"
Sunil Ravi65251732023-01-24 05:03:35 +0000696 "%s\n"
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000697 "ignore_broadcast_ssid=%d\n"
698 "wowlan_triggers=any\n"
699#ifdef CONFIG_INTERWORKING
700 "%s\n"
701#endif /* CONFIG_INTERWORKING */
702 "%s\n"
703 "%s\n"
704 "%s\n"
Serik Beketayev8af7a722021-12-23 12:25:36 -0800705 "%s\n"
Purushottam Kushwaha0316c882021-12-20 15:07:44 +0530706 "%s\n"
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000707 "%s\n",
708 iface_params.name.c_str(), ssid_as_string.c_str(),
709 channel_config_as_string.c_str(),
710 iface_params.hwModeParams.enable80211N ? 1 : 0,
711 iface_params.hwModeParams.enable80211AC ? 1 : 0,
712 he_params_as_string.c_str(),
Sunil Ravi65251732023-01-24 05:03:35 +0000713 eht_params_as_string.c_str(),
Xin Dengfec682f2024-02-06 22:59:39 -0800714 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 +0000715 nw_params.isHidden ? 1 : 0,
716#ifdef CONFIG_INTERWORKING
717 access_network_params_as_string.c_str(),
718#endif /* CONFIG_INTERWORKING */
719 encryption_config_as_string.c_str(),
720 bridge_as_string.c_str(),
Purushottam Kushwaha0316c882021-12-20 15:07:44 +0530721 owe_transition_ifname_as_string.c_str(),
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000722 enable_edmg_as_string.c_str(),
Serik Beketayev8af7a722021-12-23 12:25:36 -0800723 edmg_channel_as_string.c_str(),
724 vendor_elements_as_string.c_str());
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000725}
726
727Generation getGeneration(hostapd_hw_modes *current_mode)
728{
729 wpa_printf(MSG_DEBUG, "getGeneration hwmode=%d, ht_enabled=%d,"
730 " vht_enabled=%d, he_supported=%d",
731 current_mode->mode, current_mode->ht_capab != 0,
732 current_mode->vht_capab != 0, current_mode->he_capab->he_supported);
733 switch (current_mode->mode) {
734 case HOSTAPD_MODE_IEEE80211B:
735 return Generation::WIFI_STANDARD_LEGACY;
736 case HOSTAPD_MODE_IEEE80211G:
737 return current_mode->ht_capab == 0 ?
738 Generation::WIFI_STANDARD_LEGACY : Generation::WIFI_STANDARD_11N;
739 case HOSTAPD_MODE_IEEE80211A:
740 if (current_mode->he_capab->he_supported) {
741 return Generation::WIFI_STANDARD_11AX;
742 }
743 return current_mode->vht_capab == 0 ?
744 Generation::WIFI_STANDARD_11N : Generation::WIFI_STANDARD_11AC;
745 case HOSTAPD_MODE_IEEE80211AD:
746 return Generation::WIFI_STANDARD_11AD;
747 default:
748 return Generation::WIFI_STANDARD_UNKNOWN;
749 }
750}
751
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800752ChannelBandwidth getChannelBandwidth(struct hostapd_config *iconf)
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000753{
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800754 wpa_printf(MSG_DEBUG, "getChannelBandwidth %d, isHT=%d, isHT40=%d",
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000755 iconf->vht_oper_chwidth, iconf->ieee80211n,
756 iconf->secondary_channel);
757 switch (iconf->vht_oper_chwidth) {
Sunil8cd6f4d2022-06-28 18:40:46 +0000758 case CONF_OPER_CHWIDTH_80MHZ:
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800759 return ChannelBandwidth::BANDWIDTH_80;
Sunil8cd6f4d2022-06-28 18:40:46 +0000760 case CONF_OPER_CHWIDTH_80P80MHZ:
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800761 return ChannelBandwidth::BANDWIDTH_80P80;
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000762 break;
Sunil8cd6f4d2022-06-28 18:40:46 +0000763 case CONF_OPER_CHWIDTH_160MHZ:
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800764 return ChannelBandwidth::BANDWIDTH_160;
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000765 break;
Sunil8cd6f4d2022-06-28 18:40:46 +0000766 case CONF_OPER_CHWIDTH_USE_HT:
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000767 if (iconf->ieee80211n) {
768 return iconf->secondary_channel != 0 ?
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800769 ChannelBandwidth::BANDWIDTH_40 : ChannelBandwidth::BANDWIDTH_20;
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000770 }
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800771 return ChannelBandwidth::BANDWIDTH_20_NOHT;
Sunil8cd6f4d2022-06-28 18:40:46 +0000772 case CONF_OPER_CHWIDTH_2160MHZ:
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800773 return ChannelBandwidth::BANDWIDTH_2160;
Sunil8cd6f4d2022-06-28 18:40:46 +0000774 case CONF_OPER_CHWIDTH_4320MHZ:
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800775 return ChannelBandwidth::BANDWIDTH_4320;
Sunil8cd6f4d2022-06-28 18:40:46 +0000776 case CONF_OPER_CHWIDTH_6480MHZ:
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800777 return ChannelBandwidth::BANDWIDTH_6480;
Sunil8cd6f4d2022-06-28 18:40:46 +0000778 case CONF_OPER_CHWIDTH_8640MHZ:
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800779 return ChannelBandwidth::BANDWIDTH_8640;
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000780 default:
Ahmed ElArabawyb4115792022-02-08 09:33:01 -0800781 return ChannelBandwidth::BANDWIDTH_INVALID;
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000782 }
783}
784
785bool forceStaDisconnection(struct hostapd_data* hapd,
786 const std::vector<uint8_t>& client_address,
787 const uint16_t reason_code) {
788 struct sta_info *sta;
Sunil Ravi1a360892022-11-29 20:16:01 +0000789 if (client_address.size() != ETH_ALEN) {
790 return false;
791 }
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000792 for (sta = hapd->sta_list; sta; sta = sta->next) {
793 int res;
794 res = memcmp(sta->addr, client_address.data(), ETH_ALEN);
795 if (res == 0) {
796 wpa_printf(MSG_INFO, "Force client:" MACSTR " disconnect with reason: %d",
797 MAC2STR(client_address.data()), reason_code);
798 ap_sta_disconnect(hapd, sta, sta->addr, reason_code);
799 return true;
800 }
801 }
802 return false;
803}
804
805// hostapd core functions accept "C" style function pointers, so use global
806// functions to pass to the hostapd core function and store the corresponding
807// std::function methods to be invoked.
808//
809// NOTE: Using the pattern from the vendor HAL (wifi_legacy_hal.cpp).
810//
811// Callback to be invoked once setup is complete
812std::function<void(struct hostapd_data*)> on_setup_complete_internal_callback;
813void onAsyncSetupCompleteCb(void* ctx)
814{
815 struct hostapd_data* iface_hapd = (struct hostapd_data*)ctx;
816 if (on_setup_complete_internal_callback) {
817 on_setup_complete_internal_callback(iface_hapd);
818 // Invalidate this callback since we don't want this firing
819 // again in single AP mode.
820 if (strlen(iface_hapd->conf->bridge) > 0) {
821 on_setup_complete_internal_callback = nullptr;
822 }
823 }
824}
825
826// Callback to be invoked on hotspot client connection/disconnection
827std::function<void(struct hostapd_data*, const u8 *mac_addr, int authorized,
828 const u8 *p2p_dev_addr)> on_sta_authorized_internal_callback;
829void onAsyncStaAuthorizedCb(void* ctx, const u8 *mac_addr, int authorized,
Sunil Ravid8128a22023-11-06 23:53:58 +0000830 const u8 *p2p_dev_addr, const u8 *ip)
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000831{
832 struct hostapd_data* iface_hapd = (struct hostapd_data*)ctx;
833 if (on_sta_authorized_internal_callback) {
834 on_sta_authorized_internal_callback(iface_hapd, mac_addr,
835 authorized, p2p_dev_addr);
836 }
837}
838
839std::function<void(struct hostapd_data*, int level,
840 enum wpa_msg_type type, const char *txt,
841 size_t len)> on_wpa_msg_internal_callback;
842
843void onAsyncWpaEventCb(void *ctx, int level,
844 enum wpa_msg_type type, const char *txt,
845 size_t len)
846{
847 struct hostapd_data* iface_hapd = (struct hostapd_data*)ctx;
848 if (on_wpa_msg_internal_callback) {
849 on_wpa_msg_internal_callback(iface_hapd, level,
850 type, txt, len);
851 }
852}
853
854inline ndk::ScopedAStatus createStatus(HostapdStatusCode status_code) {
855 return ndk::ScopedAStatus::fromServiceSpecificError(
856 static_cast<int32_t>(status_code));
857}
858
859inline ndk::ScopedAStatus createStatusWithMsg(
860 HostapdStatusCode status_code, std::string msg)
861{
862 return ndk::ScopedAStatus::fromServiceSpecificErrorWithMessage(
863 static_cast<int32_t>(status_code), msg.c_str());
864}
865
866// Method called by death_notifier_ on client death.
867void onDeath(void* cookie) {
868 wpa_printf(MSG_ERROR, "Client died. Terminating...");
869 eloop_terminate();
870}
871
872} // namespace
873
874namespace aidl {
875namespace android {
876namespace hardware {
877namespace wifi {
878namespace hostapd {
879
880Hostapd::Hostapd(struct hapd_interfaces* interfaces)
881 : interfaces_(interfaces)
882{
883 death_notifier_ = AIBinder_DeathRecipient_new(onDeath);
884}
885
886::ndk::ScopedAStatus Hostapd::addAccessPoint(
887 const IfaceParams& iface_params, const NetworkParams& nw_params)
888{
889 return addAccessPointInternal(iface_params, nw_params);
890}
891
892::ndk::ScopedAStatus Hostapd::removeAccessPoint(const std::string& iface_name)
893{
894 return removeAccessPointInternal(iface_name);
895}
896
897::ndk::ScopedAStatus Hostapd::terminate()
898{
899 wpa_printf(MSG_INFO, "Terminating...");
900 // Clear the callback to avoid IPCThreadState shutdown during the
901 // callback event.
902 callbacks_.clear();
903 eloop_terminate();
904 return ndk::ScopedAStatus::ok();
905}
906
907::ndk::ScopedAStatus Hostapd::registerCallback(
908 const std::shared_ptr<IHostapdCallback>& callback)
909{
910 return registerCallbackInternal(callback);
911}
912
913::ndk::ScopedAStatus Hostapd::forceClientDisconnect(
914 const std::string& iface_name, const std::vector<uint8_t>& client_address,
915 Ieee80211ReasonCode reason_code)
916{
917 return forceClientDisconnectInternal(iface_name, client_address, reason_code);
918}
919
920::ndk::ScopedAStatus Hostapd::setDebugParams(DebugLevel level)
921{
922 return setDebugParamsInternal(level);
923}
924
925::ndk::ScopedAStatus Hostapd::addAccessPointInternal(
926 const IfaceParams& iface_params,
927 const NetworkParams& nw_params)
928{
929 int channelParamsSize = iface_params.channelParams.size();
930 if (channelParamsSize == 1) {
931 // Single AP
932 wpa_printf(MSG_INFO, "AddSingleAccessPoint, iface=%s",
933 iface_params.name.c_str());
934 return addSingleAccessPoint(iface_params, iface_params.channelParams[0],
Purushottam Kushwaha0316c882021-12-20 15:07:44 +0530935 nw_params, "", "");
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000936 } else if (channelParamsSize == 2) {
937 // Concurrent APs
938 wpa_printf(MSG_INFO, "AddDualAccessPoint, iface=%s",
939 iface_params.name.c_str());
940 return addConcurrentAccessPoints(iface_params, nw_params);
941 }
942 return createStatus(HostapdStatusCode::FAILURE_ARGS_INVALID);
943}
944
Purushottam Kushwaha0316c882021-12-20 15:07:44 +0530945std::vector<uint8_t> generateRandomOweSsid()
946{
947 u8 random[8] = {0};
948 os_get_random(random, 8);
949
950 std::string ssid = StringPrintf("Owe-%s", random);
951 wpa_printf(MSG_INFO, "Generated OWE SSID: %s", ssid.c_str());
952 std::vector<uint8_t> vssid(ssid.begin(), ssid.end());
953
954 return vssid;
955}
956
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000957::ndk::ScopedAStatus Hostapd::addConcurrentAccessPoints(
958 const IfaceParams& iface_params, const NetworkParams& nw_params)
959{
960 int channelParamsListSize = iface_params.channelParams.size();
961 // Get available interfaces in bridge
962 std::vector<std::string> managed_interfaces;
963 std::string br_name = StringPrintf(
964 "%s", iface_params.name.c_str());
965 if (!GetInterfacesInBridge(br_name, &managed_interfaces)) {
966 return createStatusWithMsg(HostapdStatusCode::FAILURE_UNKNOWN,
967 "Get interfaces in bridge failed.");
968 }
969 if (managed_interfaces.size() < channelParamsListSize) {
970 return createStatusWithMsg(HostapdStatusCode::FAILURE_UNKNOWN,
971 "Available interfaces less than requested bands");
972 }
973 // start BSS on specified bands
974 for (std::size_t i = 0; i < channelParamsListSize; i ++) {
975 IfaceParams iface_params_new = iface_params;
Purushottam Kushwaha0316c882021-12-20 15:07:44 +0530976 NetworkParams nw_params_new = nw_params;
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000977 iface_params_new.name = managed_interfaces[i];
Purushottam Kushwaha0316c882021-12-20 15:07:44 +0530978
979 std::string owe_transition_ifname = "";
Ahmed ElArabawy1aaf1802022-02-04 15:58:55 -0800980 if (nw_params.encryptionType == EncryptionType::WPA3_OWE_TRANSITION) {
Purushottam Kushwaha0316c882021-12-20 15:07:44 +0530981 if (i == 0 && i+1 < channelParamsListSize) {
982 owe_transition_ifname = managed_interfaces[i+1];
983 nw_params_new.encryptionType = EncryptionType::NONE;
984 } else {
985 owe_transition_ifname = managed_interfaces[0];
986 nw_params_new.isHidden = true;
987 nw_params_new.ssid = generateRandomOweSsid();
988 }
989 }
990
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000991 ndk::ScopedAStatus status = addSingleAccessPoint(
Purushottam Kushwaha0316c882021-12-20 15:07:44 +0530992 iface_params_new, iface_params.channelParams[i], nw_params_new,
993 br_name, owe_transition_ifname);
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000994 if (!status.isOk()) {
995 wpa_printf(MSG_ERROR, "Failed to addAccessPoint %s",
996 managed_interfaces[i].c_str());
997 return status;
998 }
999 }
1000 // Save bridge interface info
1001 br_interfaces_[br_name] = managed_interfaces;
1002 return ndk::ScopedAStatus::ok();
1003}
1004
1005::ndk::ScopedAStatus Hostapd::addSingleAccessPoint(
1006 const IfaceParams& iface_params,
1007 const ChannelParams& channelParams,
1008 const NetworkParams& nw_params,
Purushottam Kushwaha0316c882021-12-20 15:07:44 +05301009 const std::string br_name,
1010 const std::string owe_transition_ifname)
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001011{
1012 if (hostapd_get_iface(interfaces_, iface_params.name.c_str())) {
1013 wpa_printf(
1014 MSG_ERROR, "Interface %s already present",
1015 iface_params.name.c_str());
1016 return createStatus(HostapdStatusCode::FAILURE_IFACE_EXISTS);
1017 }
Purushottam Kushwaha0316c882021-12-20 15:07:44 +05301018 const auto conf_params = CreateHostapdConfig(iface_params, channelParams, nw_params,
1019 br_name, owe_transition_ifname);
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001020 if (conf_params.empty()) {
1021 wpa_printf(MSG_ERROR, "Failed to create config params");
1022 return createStatus(HostapdStatusCode::FAILURE_ARGS_INVALID);
1023 }
1024 const auto conf_file_path =
1025 WriteHostapdConfig(iface_params.name, conf_params);
1026 if (conf_file_path.empty()) {
1027 wpa_printf(MSG_ERROR, "Failed to write config file");
1028 return createStatus(HostapdStatusCode::FAILURE_UNKNOWN);
1029 }
1030 std::string add_iface_param_str = StringPrintf(
1031 "%s config=%s", iface_params.name.c_str(),
1032 conf_file_path.c_str());
1033 std::vector<char> add_iface_param_vec(
1034 add_iface_param_str.begin(), add_iface_param_str.end() + 1);
1035 if (hostapd_add_iface(interfaces_, add_iface_param_vec.data()) < 0) {
1036 wpa_printf(
1037 MSG_ERROR, "Adding interface %s failed",
1038 add_iface_param_str.c_str());
1039 return createStatus(HostapdStatusCode::FAILURE_UNKNOWN);
1040 }
1041 struct hostapd_data* iface_hapd =
1042 hostapd_get_iface(interfaces_, iface_params.name.c_str());
1043 WPA_ASSERT(iface_hapd != nullptr && iface_hapd->iface != nullptr);
1044 // Register the setup complete callbacks
1045 on_setup_complete_internal_callback =
1046 [this](struct hostapd_data* iface_hapd) {
1047 wpa_printf(
1048 MSG_INFO, "AP interface setup completed - state %s",
1049 hostapd_state_text(iface_hapd->iface->state));
1050 if (iface_hapd->iface->state == HAPD_IFACE_DISABLED) {
1051 // Invoke the failure callback on all registered
1052 // clients.
1053 for (const auto& callback : callbacks_) {
1054 callback->onFailure(strlen(iface_hapd->conf->bridge) > 0 ?
Les Leee08c2862021-10-29 16:36:41 +08001055 iface_hapd->conf->bridge : iface_hapd->conf->iface,
1056 iface_hapd->conf->iface);
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001057 }
1058 }
1059 };
1060
1061 // Register for new client connect/disconnect indication.
1062 on_sta_authorized_internal_callback =
1063 [this](struct hostapd_data* iface_hapd, const u8 *mac_addr,
1064 int authorized, const u8 *p2p_dev_addr) {
1065 wpa_printf(MSG_DEBUG, "notify client " MACSTR " %s",
1066 MAC2STR(mac_addr),
1067 (authorized) ? "Connected" : "Disconnected");
1068 ClientInfo info;
1069 info.ifaceName = strlen(iface_hapd->conf->bridge) > 0 ?
1070 iface_hapd->conf->bridge : iface_hapd->conf->iface;
1071 info.apIfaceInstance = iface_hapd->conf->iface;
1072 info.clientAddress.assign(mac_addr, mac_addr + ETH_ALEN);
1073 info.isConnected = authorized;
1074 for (const auto &callback : callbacks_) {
1075 callback->onConnectedClientsChanged(info);
1076 }
1077 };
1078
1079 // Register for wpa_event which used to get channel switch event
1080 on_wpa_msg_internal_callback =
1081 [this](struct hostapd_data* iface_hapd, int level,
1082 enum wpa_msg_type type, const char *txt,
1083 size_t len) {
1084 wpa_printf(MSG_DEBUG, "Receive wpa msg : %s", txt);
1085 if (os_strncmp(txt, AP_EVENT_ENABLED,
1086 strlen(AP_EVENT_ENABLED)) == 0 ||
1087 os_strncmp(txt, WPA_EVENT_CHANNEL_SWITCH,
1088 strlen(WPA_EVENT_CHANNEL_SWITCH)) == 0) {
1089 ApInfo info;
1090 info.ifaceName = strlen(iface_hapd->conf->bridge) > 0 ?
1091 iface_hapd->conf->bridge : iface_hapd->conf->iface,
1092 info.apIfaceInstance = iface_hapd->conf->iface;
1093 info.freqMhz = iface_hapd->iface->freq;
Ahmed ElArabawyb4115792022-02-08 09:33:01 -08001094 info.channelBandwidth = getChannelBandwidth(iface_hapd->iconf);
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001095 info.generation = getGeneration(iface_hapd->iface->current_mode);
1096 info.apIfaceInstanceMacAddress.assign(iface_hapd->own_addr,
1097 iface_hapd->own_addr + ETH_ALEN);
1098 for (const auto &callback : callbacks_) {
1099 callback->onApInstanceInfoChanged(info);
1100 }
Les Leea0c90cb2022-04-19 17:39:23 +08001101 } else if (os_strncmp(txt, AP_EVENT_DISABLED, strlen(AP_EVENT_DISABLED)) == 0
1102 || os_strncmp(txt, INTERFACE_DISABLED, strlen(INTERFACE_DISABLED)) == 0)
1103 {
Yu Ouyang378d3c42021-08-20 17:31:08 +08001104 // Invoke the failure callback on all registered clients.
1105 for (const auto& callback : callbacks_) {
1106 callback->onFailure(strlen(iface_hapd->conf->bridge) > 0 ?
Les Leee08c2862021-10-29 16:36:41 +08001107 iface_hapd->conf->bridge : iface_hapd->conf->iface,
1108 iface_hapd->conf->iface);
Yu Ouyang378d3c42021-08-20 17:31:08 +08001109 }
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001110 }
Yu Ouyang378d3c42021-08-20 17:31:08 +08001111 };
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001112
1113 // Setup callback
1114 iface_hapd->setup_complete_cb = onAsyncSetupCompleteCb;
1115 iface_hapd->setup_complete_cb_ctx = iface_hapd;
1116 iface_hapd->sta_authorized_cb = onAsyncStaAuthorizedCb;
1117 iface_hapd->sta_authorized_cb_ctx = iface_hapd;
Hu Wang7c5a4322021-06-24 17:24:59 +08001118 wpa_msg_register_aidl_cb(onAsyncWpaEventCb);
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001119
1120 if (hostapd_enable_iface(iface_hapd->iface) < 0) {
1121 wpa_printf(
1122 MSG_ERROR, "Enabling interface %s failed",
1123 iface_params.name.c_str());
1124 return createStatus(HostapdStatusCode::FAILURE_UNKNOWN);
1125 }
1126 return ndk::ScopedAStatus::ok();
1127}
1128
1129::ndk::ScopedAStatus Hostapd::removeAccessPointInternal(const std::string& iface_name)
1130{
1131 // interfaces to be removed
1132 std::vector<std::string> interfaces;
1133 bool is_error = false;
1134
1135 const auto it = br_interfaces_.find(iface_name);
1136 if (it != br_interfaces_.end()) {
1137 // In case bridge, remove managed interfaces
1138 interfaces = it->second;
1139 br_interfaces_.erase(iface_name);
1140 } else {
1141 // else remove current interface
1142 interfaces.push_back(iface_name);
1143 }
1144
1145 for (auto& iface : interfaces) {
1146 std::vector<char> remove_iface_param_vec(
1147 iface.begin(), iface.end() + 1);
1148 if (hostapd_remove_iface(interfaces_, remove_iface_param_vec.data()) < 0) {
1149 wpa_printf(MSG_INFO, "Remove interface %s failed", iface.c_str());
1150 is_error = true;
1151 }
1152 }
1153 if (is_error) {
1154 return createStatus(HostapdStatusCode::FAILURE_UNKNOWN);
1155 }
1156 return ndk::ScopedAStatus::ok();
1157}
1158
1159::ndk::ScopedAStatus Hostapd::registerCallbackInternal(
1160 const std::shared_ptr<IHostapdCallback>& callback)
1161{
1162 binder_status_t status = AIBinder_linkToDeath(callback->asBinder().get(),
1163 death_notifier_, this /* cookie */);
1164 if (status != STATUS_OK) {
1165 wpa_printf(
1166 MSG_ERROR,
1167 "Error registering for death notification for "
1168 "hostapd callback object");
1169 return createStatus(HostapdStatusCode::FAILURE_UNKNOWN);
1170 }
1171 callbacks_.push_back(callback);
1172 return ndk::ScopedAStatus::ok();
1173}
1174
1175::ndk::ScopedAStatus Hostapd::forceClientDisconnectInternal(const std::string& iface_name,
1176 const std::vector<uint8_t>& client_address, Ieee80211ReasonCode reason_code)
1177{
1178 struct hostapd_data *hapd = hostapd_get_iface(interfaces_, iface_name.c_str());
1179 bool result;
1180 if (!hapd) {
1181 for (auto const& iface : br_interfaces_) {
1182 if (iface.first == iface_name) {
1183 for (auto const& instance : iface.second) {
1184 hapd = hostapd_get_iface(interfaces_, instance.c_str());
1185 if (hapd) {
1186 result = forceStaDisconnection(hapd, client_address,
1187 (uint16_t) reason_code);
1188 if (result) break;
1189 }
1190 }
1191 }
1192 }
1193 } else {
1194 result = forceStaDisconnection(hapd, client_address, (uint16_t) reason_code);
1195 }
1196 if (!hapd) {
1197 wpa_printf(MSG_ERROR, "Interface %s doesn't exist", iface_name.c_str());
1198 return createStatus(HostapdStatusCode::FAILURE_IFACE_UNKNOWN);
1199 }
1200 if (result) {
1201 return ndk::ScopedAStatus::ok();
1202 }
1203 return createStatus(HostapdStatusCode::FAILURE_CLIENT_UNKNOWN);
1204}
1205
1206::ndk::ScopedAStatus Hostapd::setDebugParamsInternal(DebugLevel level)
1207{
1208 wpa_debug_level = static_cast<uint32_t>(level);
1209 return ndk::ScopedAStatus::ok();
1210}
1211
1212} // namespace hostapd
1213} // namespace wifi
1214} // namespace hardware
1215} // namespace android
Les Leee08c2862021-10-29 16:36:41 +08001216} // namespace aidl