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