blob: 9e23247b92a74e5d500cbc1be8338abeda867cfa [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;
50using aidl::android::hardware::wifi::hostapd::Bandwidth;
51using 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,
301 const std::string br_name)
302{
303 if (nw_params.ssid.size() >
304 static_cast<uint32_t>(
305 ParamSizeLimits::SSID_MAX_LEN_IN_BYTES)) {
306 wpa_printf(
307 MSG_ERROR, "Invalid SSID size: %zu", nw_params.ssid.size());
308 return "";
309 }
310
311 // SSID string
312 std::stringstream ss;
313 ss << std::hex;
314 ss << std::setfill('0');
315 for (uint8_t b : nw_params.ssid) {
316 ss << std::setw(2) << static_cast<unsigned int>(b);
317 }
318 const std::string ssid_as_string = ss.str();
319
320 // Encryption config string
321 uint32_t band = 0;
322 band |= static_cast<uint32_t>(channelParams.bandMask);
323 bool is_6Ghz_band_only = band == static_cast<uint32_t>(band6Ghz);
324 bool is_60Ghz_band_only = band == static_cast<uint32_t>(band60Ghz);
325 std::string encryption_config_as_string;
326 switch (nw_params.encryptionType) {
327 case EncryptionType::NONE:
328 // no security params
329 break;
330 case EncryptionType::WPA:
331 if (!validatePassphrase(
332 nw_params.passphrase.size(),
333 static_cast<uint32_t>(ParamSizeLimits::
334 WPA2_PSK_PASSPHRASE_MIN_LEN_IN_BYTES),
335 static_cast<uint32_t>(ParamSizeLimits::
336 WPA2_PSK_PASSPHRASE_MAX_LEN_IN_BYTES))) {
337 return "";
338 }
339 encryption_config_as_string = StringPrintf(
340 "wpa=3\n"
341 "wpa_pairwise=%s\n"
342 "wpa_passphrase=%s",
343 is_60Ghz_band_only ? "GCMP" : "TKIP CCMP",
344 nw_params.passphrase.c_str());
345 break;
346 case EncryptionType::WPA2:
347 if (!validatePassphrase(
348 nw_params.passphrase.size(),
349 static_cast<uint32_t>(ParamSizeLimits::
350 WPA2_PSK_PASSPHRASE_MIN_LEN_IN_BYTES),
351 static_cast<uint32_t>(ParamSizeLimits::
352 WPA2_PSK_PASSPHRASE_MAX_LEN_IN_BYTES))) {
353 return "";
354 }
355 encryption_config_as_string = StringPrintf(
356 "wpa=2\n"
357 "rsn_pairwise=%s\n"
Sunil Ravib3580db2022-01-28 12:25:46 -0800358#ifdef ENABLE_HOSTAPD_CONFIG_80211W_MFP_OPTIONAL
359 "ieee80211w=1\n"
360#endif
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000361 "wpa_passphrase=%s",
362 is_60Ghz_band_only ? "GCMP" : "CCMP",
363 nw_params.passphrase.c_str());
364 break;
365 case EncryptionType::WPA3_SAE_TRANSITION:
366 if (!validatePassphrase(
367 nw_params.passphrase.size(),
368 static_cast<uint32_t>(ParamSizeLimits::
369 WPA2_PSK_PASSPHRASE_MIN_LEN_IN_BYTES),
370 static_cast<uint32_t>(ParamSizeLimits::
371 WPA2_PSK_PASSPHRASE_MAX_LEN_IN_BYTES))) {
372 return "";
373 }
374 encryption_config_as_string = StringPrintf(
375 "wpa=2\n"
376 "rsn_pairwise=%s\n"
377 "wpa_key_mgmt=WPA-PSK SAE\n"
378 "ieee80211w=1\n"
379 "sae_require_mfp=1\n"
380 "wpa_passphrase=%s\n"
381 "sae_password=%s",
382 is_60Ghz_band_only ? "GCMP" : "CCMP",
383 nw_params.passphrase.c_str(),
384 nw_params.passphrase.c_str());
385 break;
386 case EncryptionType::WPA3_SAE:
387 if (!validatePassphrase(nw_params.passphrase.size(), 1, -1)) {
388 return "";
389 }
390 encryption_config_as_string = StringPrintf(
391 "wpa=2\n"
392 "rsn_pairwise=%s\n"
393 "wpa_key_mgmt=SAE\n"
394 "ieee80211w=2\n"
395 "sae_require_mfp=2\n"
396 "sae_pwe=%d\n"
397 "sae_password=%s",
398 is_60Ghz_band_only ? "GCMP" : "CCMP",
399 is_6Ghz_band_only ? 1 : 2,
400 nw_params.passphrase.c_str());
401 break;
402 default:
403 wpa_printf(MSG_ERROR, "Unknown encryption type");
404 return "";
405 }
406
407 std::string channel_config_as_string;
408 bool isFirst = true;
409 if (channelParams.enableAcs) {
410 std::string freqList_as_string;
411 for (const auto &range :
412 channelParams.acsChannelFreqRangesMhz) {
413 if (!isFirst) {
414 freqList_as_string += ",";
415 }
416 isFirst = false;
417
418 if (range.startMhz != range.endMhz) {
419 freqList_as_string +=
420 StringPrintf("%d-%d", range.startMhz, range.endMhz);
421 } else {
422 freqList_as_string += StringPrintf("%d", range.startMhz);
423 }
424 }
425 channel_config_as_string = StringPrintf(
426 "channel=0\n"
427 "acs_exclude_dfs=%d\n"
428 "freqlist=%s",
429 channelParams.acsShouldExcludeDfs,
430 freqList_as_string.c_str());
431 } else {
432 int op_class = getOpClassForChannel(
433 channelParams.channel,
434 band,
435 iface_params.hwModeParams.enable80211N,
436 iface_params.hwModeParams.enable80211AC);
437 channel_config_as_string = StringPrintf(
438 "channel=%d\n"
439 "op_class=%d",
440 channelParams.channel, op_class);
441 }
442
443 std::string hw_mode_as_string;
444 std::string ht_cap_vht_oper_chwidth_as_string;
445 std::string enable_edmg_as_string;
446 std::string edmg_channel_as_string;
447 bool is_60Ghz_used = false;
448
449 if (((band & band60Ghz) != 0)) {
450 hw_mode_as_string = "hw_mode=ad";
451 if (iface_params.hwModeParams.enableEdmg) {
452 enable_edmg_as_string = "enable_edmg=1";
453 edmg_channel_as_string = StringPrintf(
454 "edmg_channel=%d",
455 channelParams.channel);
456 }
457 is_60Ghz_used = true;
458 } else if ((band & band2Ghz) != 0) {
459 if (((band & band5Ghz) != 0)
460 || ((band & band6Ghz) != 0)) {
461 hw_mode_as_string = "hw_mode=any";
462 if (iface_params.hwModeParams.enable80211AC) {
463 ht_cap_vht_oper_chwidth_as_string =
464 "ht_capab=[HT40+]\n"
465 "vht_oper_chwidth=1";
466 }
467 } else {
468 hw_mode_as_string = "hw_mode=g";
469 }
470 } else if (((band & band5Ghz) != 0)
471 || ((band & band6Ghz) != 0)) {
472 hw_mode_as_string = "hw_mode=a";
473 if (iface_params.hwModeParams.enable80211AC) {
474 ht_cap_vht_oper_chwidth_as_string =
475 "ht_capab=[HT40+]\n"
476 "vht_oper_chwidth=1";
477 }
478 } else {
479 wpa_printf(MSG_ERROR, "Invalid band");
480 return "";
481 }
482
483 std::string he_params_as_string;
484#ifdef CONFIG_IEEE80211AX
485 if (iface_params.hwModeParams.enable80211AX && !is_60Ghz_used) {
486 he_params_as_string = StringPrintf(
487 "ieee80211ax=1\n"
488 "he_oper_chwidth=1\n"
489 "he_su_beamformer=%d\n"
490 "he_su_beamformee=%d\n"
491 "he_mu_beamformer=%d\n"
492 "he_twt_required=%d\n",
493 iface_params.hwModeParams.enableHeSingleUserBeamformer ? 1 : 0,
494 iface_params.hwModeParams.enableHeSingleUserBeamformee ? 1 : 0,
495 iface_params.hwModeParams.enableHeMultiUserBeamformer ? 1 : 0,
496 iface_params.hwModeParams.enableHeTargetWakeTime ? 1 : 0);
497 } else {
498 he_params_as_string = "ieee80211ax=0";
499 }
500#endif /* CONFIG_IEEE80211AX */
501
502#ifdef CONFIG_INTERWORKING
503 std::string access_network_params_as_string;
504 if (nw_params.isMetered) {
505 access_network_params_as_string = StringPrintf(
506 "interworking=1\n"
507 "access_network_type=2\n"); // CHARGEABLE_PUBLIC_NETWORK
508 } else {
509 access_network_params_as_string = StringPrintf(
510 "interworking=0\n");
511 }
512#endif /* CONFIG_INTERWORKING */
513
514 std::string bridge_as_string;
515 if (!br_name.empty()) {
516 bridge_as_string = StringPrintf("bridge=%s", br_name.c_str());
517 }
518
Serik Beketayev8af7a722021-12-23 12:25:36 -0800519 // vendor_elements string
520 std::string vendor_elements_as_string;
521 if (nw_params.vendorElements.size() > 0) {
522 std::stringstream ss;
523 ss << std::hex;
524 ss << std::setfill('0');
525 for (uint8_t b : nw_params.vendorElements) {
526 ss << std::setw(2) << static_cast<unsigned int>(b);
527 }
528 vendor_elements_as_string = StringPrintf("vendor_elements=%s", ss.str().c_str());
529 }
530
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000531 return StringPrintf(
532 "interface=%s\n"
533 "driver=nl80211\n"
534 "ctrl_interface=/data/vendor/wifi/hostapd/ctrl\n"
535 // ssid2 signals to hostapd that the value is not a literal value
536 // for use as a SSID. In this case, we're giving it a hex
537 // std::string and hostapd needs to expect that.
538 "ssid2=%s\n"
539 "%s\n"
540 "ieee80211n=%d\n"
541 "ieee80211ac=%d\n"
542 "%s\n"
543 "%s\n"
544 "%s\n"
545 "ignore_broadcast_ssid=%d\n"
546 "wowlan_triggers=any\n"
547#ifdef CONFIG_INTERWORKING
548 "%s\n"
549#endif /* CONFIG_INTERWORKING */
550 "%s\n"
551 "%s\n"
552 "%s\n"
Serik Beketayev8af7a722021-12-23 12:25:36 -0800553 "%s\n"
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000554 "%s\n",
555 iface_params.name.c_str(), ssid_as_string.c_str(),
556 channel_config_as_string.c_str(),
557 iface_params.hwModeParams.enable80211N ? 1 : 0,
558 iface_params.hwModeParams.enable80211AC ? 1 : 0,
559 he_params_as_string.c_str(),
560 hw_mode_as_string.c_str(), ht_cap_vht_oper_chwidth_as_string.c_str(),
561 nw_params.isHidden ? 1 : 0,
562#ifdef CONFIG_INTERWORKING
563 access_network_params_as_string.c_str(),
564#endif /* CONFIG_INTERWORKING */
565 encryption_config_as_string.c_str(),
566 bridge_as_string.c_str(),
567 enable_edmg_as_string.c_str(),
Serik Beketayev8af7a722021-12-23 12:25:36 -0800568 edmg_channel_as_string.c_str(),
569 vendor_elements_as_string.c_str());
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000570}
571
572Generation getGeneration(hostapd_hw_modes *current_mode)
573{
574 wpa_printf(MSG_DEBUG, "getGeneration hwmode=%d, ht_enabled=%d,"
575 " vht_enabled=%d, he_supported=%d",
576 current_mode->mode, current_mode->ht_capab != 0,
577 current_mode->vht_capab != 0, current_mode->he_capab->he_supported);
578 switch (current_mode->mode) {
579 case HOSTAPD_MODE_IEEE80211B:
580 return Generation::WIFI_STANDARD_LEGACY;
581 case HOSTAPD_MODE_IEEE80211G:
582 return current_mode->ht_capab == 0 ?
583 Generation::WIFI_STANDARD_LEGACY : Generation::WIFI_STANDARD_11N;
584 case HOSTAPD_MODE_IEEE80211A:
585 if (current_mode->he_capab->he_supported) {
586 return Generation::WIFI_STANDARD_11AX;
587 }
588 return current_mode->vht_capab == 0 ?
589 Generation::WIFI_STANDARD_11N : Generation::WIFI_STANDARD_11AC;
590 case HOSTAPD_MODE_IEEE80211AD:
591 return Generation::WIFI_STANDARD_11AD;
592 default:
593 return Generation::WIFI_STANDARD_UNKNOWN;
594 }
595}
596
597Bandwidth getBandwidth(struct hostapd_config *iconf)
598{
599 wpa_printf(MSG_DEBUG, "getBandwidth %d, isHT=%d, isHT40=%d",
600 iconf->vht_oper_chwidth, iconf->ieee80211n,
601 iconf->secondary_channel);
602 switch (iconf->vht_oper_chwidth) {
603 case CHANWIDTH_80MHZ:
604 return Bandwidth::BANDWIDTH_80;
605 case CHANWIDTH_80P80MHZ:
606 return Bandwidth::BANDWIDTH_80P80;
607 break;
608 case CHANWIDTH_160MHZ:
609 return Bandwidth::BANDWIDTH_160;
610 break;
611 case CHANWIDTH_USE_HT:
612 if (iconf->ieee80211n) {
613 return iconf->secondary_channel != 0 ?
614 Bandwidth::BANDWIDTH_40 : Bandwidth::BANDWIDTH_20;
615 }
616 return Bandwidth::BANDWIDTH_20_NOHT;
617 case CHANWIDTH_2160MHZ:
618 return Bandwidth::BANDWIDTH_2160;
619 case CHANWIDTH_4320MHZ:
620 return Bandwidth::BANDWIDTH_4320;
621 case CHANWIDTH_6480MHZ:
622 return Bandwidth::BANDWIDTH_6480;
623 case CHANWIDTH_8640MHZ:
624 return Bandwidth::BANDWIDTH_8640;
625 default:
626 return Bandwidth::BANDWIDTH_INVALID;
627 }
628}
629
630bool forceStaDisconnection(struct hostapd_data* hapd,
631 const std::vector<uint8_t>& client_address,
632 const uint16_t reason_code) {
633 struct sta_info *sta;
634 for (sta = hapd->sta_list; sta; sta = sta->next) {
635 int res;
636 res = memcmp(sta->addr, client_address.data(), ETH_ALEN);
637 if (res == 0) {
638 wpa_printf(MSG_INFO, "Force client:" MACSTR " disconnect with reason: %d",
639 MAC2STR(client_address.data()), reason_code);
640 ap_sta_disconnect(hapd, sta, sta->addr, reason_code);
641 return true;
642 }
643 }
644 return false;
645}
646
647// hostapd core functions accept "C" style function pointers, so use global
648// functions to pass to the hostapd core function and store the corresponding
649// std::function methods to be invoked.
650//
651// NOTE: Using the pattern from the vendor HAL (wifi_legacy_hal.cpp).
652//
653// Callback to be invoked once setup is complete
654std::function<void(struct hostapd_data*)> on_setup_complete_internal_callback;
655void onAsyncSetupCompleteCb(void* ctx)
656{
657 struct hostapd_data* iface_hapd = (struct hostapd_data*)ctx;
658 if (on_setup_complete_internal_callback) {
659 on_setup_complete_internal_callback(iface_hapd);
660 // Invalidate this callback since we don't want this firing
661 // again in single AP mode.
662 if (strlen(iface_hapd->conf->bridge) > 0) {
663 on_setup_complete_internal_callback = nullptr;
664 }
665 }
666}
667
668// Callback to be invoked on hotspot client connection/disconnection
669std::function<void(struct hostapd_data*, const u8 *mac_addr, int authorized,
670 const u8 *p2p_dev_addr)> on_sta_authorized_internal_callback;
671void onAsyncStaAuthorizedCb(void* ctx, const u8 *mac_addr, int authorized,
672 const u8 *p2p_dev_addr)
673{
674 struct hostapd_data* iface_hapd = (struct hostapd_data*)ctx;
675 if (on_sta_authorized_internal_callback) {
676 on_sta_authorized_internal_callback(iface_hapd, mac_addr,
677 authorized, p2p_dev_addr);
678 }
679}
680
681std::function<void(struct hostapd_data*, int level,
682 enum wpa_msg_type type, const char *txt,
683 size_t len)> on_wpa_msg_internal_callback;
684
685void onAsyncWpaEventCb(void *ctx, int level,
686 enum wpa_msg_type type, const char *txt,
687 size_t len)
688{
689 struct hostapd_data* iface_hapd = (struct hostapd_data*)ctx;
690 if (on_wpa_msg_internal_callback) {
691 on_wpa_msg_internal_callback(iface_hapd, level,
692 type, txt, len);
693 }
694}
695
696inline ndk::ScopedAStatus createStatus(HostapdStatusCode status_code) {
697 return ndk::ScopedAStatus::fromServiceSpecificError(
698 static_cast<int32_t>(status_code));
699}
700
701inline ndk::ScopedAStatus createStatusWithMsg(
702 HostapdStatusCode status_code, std::string msg)
703{
704 return ndk::ScopedAStatus::fromServiceSpecificErrorWithMessage(
705 static_cast<int32_t>(status_code), msg.c_str());
706}
707
708// Method called by death_notifier_ on client death.
709void onDeath(void* cookie) {
710 wpa_printf(MSG_ERROR, "Client died. Terminating...");
711 eloop_terminate();
712}
713
714} // namespace
715
716namespace aidl {
717namespace android {
718namespace hardware {
719namespace wifi {
720namespace hostapd {
721
722Hostapd::Hostapd(struct hapd_interfaces* interfaces)
723 : interfaces_(interfaces)
724{
725 death_notifier_ = AIBinder_DeathRecipient_new(onDeath);
726}
727
728::ndk::ScopedAStatus Hostapd::addAccessPoint(
729 const IfaceParams& iface_params, const NetworkParams& nw_params)
730{
731 return addAccessPointInternal(iface_params, nw_params);
732}
733
734::ndk::ScopedAStatus Hostapd::removeAccessPoint(const std::string& iface_name)
735{
736 return removeAccessPointInternal(iface_name);
737}
738
739::ndk::ScopedAStatus Hostapd::terminate()
740{
741 wpa_printf(MSG_INFO, "Terminating...");
742 // Clear the callback to avoid IPCThreadState shutdown during the
743 // callback event.
744 callbacks_.clear();
745 eloop_terminate();
746 return ndk::ScopedAStatus::ok();
747}
748
749::ndk::ScopedAStatus Hostapd::registerCallback(
750 const std::shared_ptr<IHostapdCallback>& callback)
751{
752 return registerCallbackInternal(callback);
753}
754
755::ndk::ScopedAStatus Hostapd::forceClientDisconnect(
756 const std::string& iface_name, const std::vector<uint8_t>& client_address,
757 Ieee80211ReasonCode reason_code)
758{
759 return forceClientDisconnectInternal(iface_name, client_address, reason_code);
760}
761
762::ndk::ScopedAStatus Hostapd::setDebugParams(DebugLevel level)
763{
764 return setDebugParamsInternal(level);
765}
766
767::ndk::ScopedAStatus Hostapd::addAccessPointInternal(
768 const IfaceParams& iface_params,
769 const NetworkParams& nw_params)
770{
771 int channelParamsSize = iface_params.channelParams.size();
772 if (channelParamsSize == 1) {
773 // Single AP
774 wpa_printf(MSG_INFO, "AddSingleAccessPoint, iface=%s",
775 iface_params.name.c_str());
776 return addSingleAccessPoint(iface_params, iface_params.channelParams[0],
777 nw_params, "");
778 } else if (channelParamsSize == 2) {
779 // Concurrent APs
780 wpa_printf(MSG_INFO, "AddDualAccessPoint, iface=%s",
781 iface_params.name.c_str());
782 return addConcurrentAccessPoints(iface_params, nw_params);
783 }
784 return createStatus(HostapdStatusCode::FAILURE_ARGS_INVALID);
785}
786
787::ndk::ScopedAStatus Hostapd::addConcurrentAccessPoints(
788 const IfaceParams& iface_params, const NetworkParams& nw_params)
789{
790 int channelParamsListSize = iface_params.channelParams.size();
791 // Get available interfaces in bridge
792 std::vector<std::string> managed_interfaces;
793 std::string br_name = StringPrintf(
794 "%s", iface_params.name.c_str());
795 if (!GetInterfacesInBridge(br_name, &managed_interfaces)) {
796 return createStatusWithMsg(HostapdStatusCode::FAILURE_UNKNOWN,
797 "Get interfaces in bridge failed.");
798 }
799 if (managed_interfaces.size() < channelParamsListSize) {
800 return createStatusWithMsg(HostapdStatusCode::FAILURE_UNKNOWN,
801 "Available interfaces less than requested bands");
802 }
803 // start BSS on specified bands
804 for (std::size_t i = 0; i < channelParamsListSize; i ++) {
805 IfaceParams iface_params_new = iface_params;
806 iface_params_new.name = managed_interfaces[i];
807 ndk::ScopedAStatus status = addSingleAccessPoint(
808 iface_params_new, iface_params.channelParams[i], nw_params, br_name);
809 if (!status.isOk()) {
810 wpa_printf(MSG_ERROR, "Failed to addAccessPoint %s",
811 managed_interfaces[i].c_str());
812 return status;
813 }
814 }
815 // Save bridge interface info
816 br_interfaces_[br_name] = managed_interfaces;
817 return ndk::ScopedAStatus::ok();
818}
819
820::ndk::ScopedAStatus Hostapd::addSingleAccessPoint(
821 const IfaceParams& iface_params,
822 const ChannelParams& channelParams,
823 const NetworkParams& nw_params,
824 const std::string br_name)
825{
826 if (hostapd_get_iface(interfaces_, iface_params.name.c_str())) {
827 wpa_printf(
828 MSG_ERROR, "Interface %s already present",
829 iface_params.name.c_str());
830 return createStatus(HostapdStatusCode::FAILURE_IFACE_EXISTS);
831 }
832 const auto conf_params = CreateHostapdConfig(iface_params, channelParams, nw_params, br_name);
833 if (conf_params.empty()) {
834 wpa_printf(MSG_ERROR, "Failed to create config params");
835 return createStatus(HostapdStatusCode::FAILURE_ARGS_INVALID);
836 }
837 const auto conf_file_path =
838 WriteHostapdConfig(iface_params.name, conf_params);
839 if (conf_file_path.empty()) {
840 wpa_printf(MSG_ERROR, "Failed to write config file");
841 return createStatus(HostapdStatusCode::FAILURE_UNKNOWN);
842 }
843 std::string add_iface_param_str = StringPrintf(
844 "%s config=%s", iface_params.name.c_str(),
845 conf_file_path.c_str());
846 std::vector<char> add_iface_param_vec(
847 add_iface_param_str.begin(), add_iface_param_str.end() + 1);
848 if (hostapd_add_iface(interfaces_, add_iface_param_vec.data()) < 0) {
849 wpa_printf(
850 MSG_ERROR, "Adding interface %s failed",
851 add_iface_param_str.c_str());
852 return createStatus(HostapdStatusCode::FAILURE_UNKNOWN);
853 }
854 struct hostapd_data* iface_hapd =
855 hostapd_get_iface(interfaces_, iface_params.name.c_str());
856 WPA_ASSERT(iface_hapd != nullptr && iface_hapd->iface != nullptr);
857 // Register the setup complete callbacks
858 on_setup_complete_internal_callback =
859 [this](struct hostapd_data* iface_hapd) {
860 wpa_printf(
861 MSG_INFO, "AP interface setup completed - state %s",
862 hostapd_state_text(iface_hapd->iface->state));
863 if (iface_hapd->iface->state == HAPD_IFACE_DISABLED) {
864 // Invoke the failure callback on all registered
865 // clients.
866 for (const auto& callback : callbacks_) {
867 callback->onFailure(strlen(iface_hapd->conf->bridge) > 0 ?
Les Leee08c2862021-10-29 16:36:41 +0800868 iface_hapd->conf->bridge : iface_hapd->conf->iface,
869 iface_hapd->conf->iface);
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000870 }
871 }
872 };
873
874 // Register for new client connect/disconnect indication.
875 on_sta_authorized_internal_callback =
876 [this](struct hostapd_data* iface_hapd, const u8 *mac_addr,
877 int authorized, const u8 *p2p_dev_addr) {
878 wpa_printf(MSG_DEBUG, "notify client " MACSTR " %s",
879 MAC2STR(mac_addr),
880 (authorized) ? "Connected" : "Disconnected");
881 ClientInfo info;
882 info.ifaceName = strlen(iface_hapd->conf->bridge) > 0 ?
883 iface_hapd->conf->bridge : iface_hapd->conf->iface;
884 info.apIfaceInstance = iface_hapd->conf->iface;
885 info.clientAddress.assign(mac_addr, mac_addr + ETH_ALEN);
886 info.isConnected = authorized;
887 for (const auto &callback : callbacks_) {
888 callback->onConnectedClientsChanged(info);
889 }
890 };
891
892 // Register for wpa_event which used to get channel switch event
893 on_wpa_msg_internal_callback =
894 [this](struct hostapd_data* iface_hapd, int level,
895 enum wpa_msg_type type, const char *txt,
896 size_t len) {
897 wpa_printf(MSG_DEBUG, "Receive wpa msg : %s", txt);
898 if (os_strncmp(txt, AP_EVENT_ENABLED,
899 strlen(AP_EVENT_ENABLED)) == 0 ||
900 os_strncmp(txt, WPA_EVENT_CHANNEL_SWITCH,
901 strlen(WPA_EVENT_CHANNEL_SWITCH)) == 0) {
902 ApInfo info;
903 info.ifaceName = strlen(iface_hapd->conf->bridge) > 0 ?
904 iface_hapd->conf->bridge : iface_hapd->conf->iface,
905 info.apIfaceInstance = iface_hapd->conf->iface;
906 info.freqMhz = iface_hapd->iface->freq;
907 info.bandwidth = getBandwidth(iface_hapd->iconf);
908 info.generation = getGeneration(iface_hapd->iface->current_mode);
909 info.apIfaceInstanceMacAddress.assign(iface_hapd->own_addr,
910 iface_hapd->own_addr + ETH_ALEN);
911 for (const auto &callback : callbacks_) {
912 callback->onApInstanceInfoChanged(info);
913 }
Yu Ouyang378d3c42021-08-20 17:31:08 +0800914 } else if (os_strncmp(txt, AP_EVENT_DISABLED, strlen(AP_EVENT_DISABLED)) == 0) {
915 // Invoke the failure callback on all registered clients.
916 for (const auto& callback : callbacks_) {
917 callback->onFailure(strlen(iface_hapd->conf->bridge) > 0 ?
Les Leee08c2862021-10-29 16:36:41 +0800918 iface_hapd->conf->bridge : iface_hapd->conf->iface,
919 iface_hapd->conf->iface);
Yu Ouyang378d3c42021-08-20 17:31:08 +0800920 }
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000921 }
Yu Ouyang378d3c42021-08-20 17:31:08 +0800922 };
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000923
924 // Setup callback
925 iface_hapd->setup_complete_cb = onAsyncSetupCompleteCb;
926 iface_hapd->setup_complete_cb_ctx = iface_hapd;
927 iface_hapd->sta_authorized_cb = onAsyncStaAuthorizedCb;
928 iface_hapd->sta_authorized_cb_ctx = iface_hapd;
929 wpa_msg_register_cb(onAsyncWpaEventCb);
930
931 if (hostapd_enable_iface(iface_hapd->iface) < 0) {
932 wpa_printf(
933 MSG_ERROR, "Enabling interface %s failed",
934 iface_params.name.c_str());
935 return createStatus(HostapdStatusCode::FAILURE_UNKNOWN);
936 }
937 return ndk::ScopedAStatus::ok();
938}
939
940::ndk::ScopedAStatus Hostapd::removeAccessPointInternal(const std::string& iface_name)
941{
942 // interfaces to be removed
943 std::vector<std::string> interfaces;
944 bool is_error = false;
945
946 const auto it = br_interfaces_.find(iface_name);
947 if (it != br_interfaces_.end()) {
948 // In case bridge, remove managed interfaces
949 interfaces = it->second;
950 br_interfaces_.erase(iface_name);
951 } else {
952 // else remove current interface
953 interfaces.push_back(iface_name);
954 }
955
956 for (auto& iface : interfaces) {
957 std::vector<char> remove_iface_param_vec(
958 iface.begin(), iface.end() + 1);
959 if (hostapd_remove_iface(interfaces_, remove_iface_param_vec.data()) < 0) {
960 wpa_printf(MSG_INFO, "Remove interface %s failed", iface.c_str());
961 is_error = true;
962 }
963 }
964 if (is_error) {
965 return createStatus(HostapdStatusCode::FAILURE_UNKNOWN);
966 }
967 return ndk::ScopedAStatus::ok();
968}
969
970::ndk::ScopedAStatus Hostapd::registerCallbackInternal(
971 const std::shared_ptr<IHostapdCallback>& callback)
972{
973 binder_status_t status = AIBinder_linkToDeath(callback->asBinder().get(),
974 death_notifier_, this /* cookie */);
975 if (status != STATUS_OK) {
976 wpa_printf(
977 MSG_ERROR,
978 "Error registering for death notification for "
979 "hostapd callback object");
980 return createStatus(HostapdStatusCode::FAILURE_UNKNOWN);
981 }
982 callbacks_.push_back(callback);
983 return ndk::ScopedAStatus::ok();
984}
985
986::ndk::ScopedAStatus Hostapd::forceClientDisconnectInternal(const std::string& iface_name,
987 const std::vector<uint8_t>& client_address, Ieee80211ReasonCode reason_code)
988{
989 struct hostapd_data *hapd = hostapd_get_iface(interfaces_, iface_name.c_str());
990 bool result;
991 if (!hapd) {
992 for (auto const& iface : br_interfaces_) {
993 if (iface.first == iface_name) {
994 for (auto const& instance : iface.second) {
995 hapd = hostapd_get_iface(interfaces_, instance.c_str());
996 if (hapd) {
997 result = forceStaDisconnection(hapd, client_address,
998 (uint16_t) reason_code);
999 if (result) break;
1000 }
1001 }
1002 }
1003 }
1004 } else {
1005 result = forceStaDisconnection(hapd, client_address, (uint16_t) reason_code);
1006 }
1007 if (!hapd) {
1008 wpa_printf(MSG_ERROR, "Interface %s doesn't exist", iface_name.c_str());
1009 return createStatus(HostapdStatusCode::FAILURE_IFACE_UNKNOWN);
1010 }
1011 if (result) {
1012 return ndk::ScopedAStatus::ok();
1013 }
1014 return createStatus(HostapdStatusCode::FAILURE_CLIENT_UNKNOWN);
1015}
1016
1017::ndk::ScopedAStatus Hostapd::setDebugParamsInternal(DebugLevel level)
1018{
1019 wpa_debug_level = static_cast<uint32_t>(level);
1020 return ndk::ScopedAStatus::ok();
1021}
1022
1023} // namespace hostapd
1024} // namespace wifi
1025} // namespace hardware
1026} // namespace android
Les Leee08c2862021-10-29 16:36:41 +08001027} // namespace aidl