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