blob: 0d01da3ab2b1ecf0690c3388d8f219fefdb6f29e [file] [log] [blame]
Roshan Piuscc817562017-12-22 14:45:05 -08001/*
2 * hidl 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 */
Roshan Pius30b452e2017-12-27 13:36:21 -08009#include <iomanip>
10#include <sstream>
11#include <string>
12#include <vector>
lesl0cb0e272020-10-30 16:58:08 +080013#include <net/if.h>
14#include <sys/socket.h>
15#include <linux/if_bridge.h>
16
Roshan Pius30b452e2017-12-27 13:36:21 -080017
18#include <android-base/file.h>
19#include <android-base/stringprintf.h>
lesl0cb0e272020-10-30 16:58:08 +080020#include <android-base/unique_fd.h>
Roshan Piuscc817562017-12-22 14:45:05 -080021
22#include "hostapd.h"
23#include "hidl_return_util.h"
24
Roshan Pius3455af42018-02-01 09:19:49 -080025extern "C"
26{
lesldab91502020-08-17 18:29:47 +080027#include "common/wpa_ctrl.h"
lesl0cb0e272020-10-30 16:58:08 +080028#include "drivers/linux_ioctl.h"
Roshan Pius3455af42018-02-01 09:19:49 -080029}
30
Roshan Pius30b452e2017-12-27 13:36:21 -080031// The HIDL implementation for hostapd creates a hostapd.conf dynamically for
32// each interface. This file can then be used to hook onto the normal config
33// file parsing logic in hostapd code. Helps us to avoid duplication of code
34// in the HIDL interface.
35// TOOD(b/71872409): Add unit tests for this.
36namespace {
37constexpr char kConfFileNameFmt[] = "/data/vendor/wifi/hostapd/hostapd_%s.conf";
38
39using android::base::RemoveFileIfExists;
40using android::base::StringPrintf;
41using android::base::WriteStringToFile;
leslb0f4d542020-09-16 23:06:03 +080042using android::hardware::wifi::hostapd::V1_3::IHostapd;
lesldab91502020-08-17 18:29:47 +080043using android::hardware::wifi::hostapd::V1_3::Generation;
44using android::hardware::wifi::hostapd::V1_3::Bandwidth;
Roshan Pius30b452e2017-12-27 13:36:21 -080045
lesl0cb0e272020-10-30 16:58:08 +080046#define MAX_PORTS 1024
47bool GetInterfacesInBridge(std::string br_name,
48 std::vector<std::string>* interfaces) {
49 android::base::unique_fd sock(socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0));
50 if (sock.get() < 0) {
51 wpa_printf(MSG_ERROR, "Failed to create sock (%s) in %s",
52 strerror(errno), __FUNCTION__);
53 return false;
54 }
55
56 struct ifreq request;
57 int i, ifindices[MAX_PORTS];
58 char if_name[IFNAMSIZ];
59 unsigned long args[3];
60
61 memset(ifindices, 0, MAX_PORTS * sizeof(int));
62
63 args[0] = BRCTL_GET_PORT_LIST;
64 args[1] = (unsigned long) ifindices;
65 args[2] = MAX_PORTS;
66
67 strlcpy(request.ifr_name, br_name.c_str(), IFNAMSIZ);
68 request.ifr_data = (char *)args;
69
70 if (ioctl(sock.get(), SIOCDEVPRIVATE, &request) < 0) {
71 wpa_printf(MSG_ERROR, "Failed to ioctl SIOCDEVPRIVATE in %s",
72 __FUNCTION__);
73 return false;
74 }
75
76 for (i = 0; i < MAX_PORTS; i ++) {
77 memset(if_name, 0, IFNAMSIZ);
78 if (ifindices[i] == 0 || !if_indextoname(ifindices[i], if_name)) {
79 continue;
80 }
81 interfaces->push_back(if_name);
82 }
83 return true;
84}
85
Roshan Pius30b452e2017-12-27 13:36:21 -080086std::string WriteHostapdConfig(
87 const std::string& interface_name, const std::string& config)
88{
89 const std::string file_path =
90 StringPrintf(kConfFileNameFmt, interface_name.c_str());
91 if (WriteStringToFile(
92 config, file_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
93 getuid(), getgid())) {
94 return file_path;
95 }
96 // Diagnose failure
97 int error = errno;
98 wpa_printf(
99 MSG_ERROR, "Cannot write hostapd config to %s, error: %s",
100 file_path.c_str(), strerror(error));
101 struct stat st;
102 int result = stat(file_path.c_str(), &st);
103 if (result == 0) {
104 wpa_printf(
105 MSG_ERROR, "hostapd config file uid: %d, gid: %d, mode: %d",
106 st.st_uid, st.st_gid, st.st_mode);
107 } else {
108 wpa_printf(
109 MSG_ERROR,
110 "Error calling stat() on hostapd config file: %s",
111 strerror(errno));
112 }
113 return "";
114}
115
Ahmed ElArabawy7b4b40a2020-01-02 08:55:38 -0800116/*
117 * Get the op_class for a channel/band
118 * The logic here is based on Table E-4 in the 802.11 Specification
119 */
120int getOpClassForChannel(int channel, int band, bool support11n, bool support11ac) {
121 // 2GHz Band
122 if ((band & IHostapd::BandMask::BAND_2_GHZ) != 0) {
123 if (channel == 14) {
124 return 82;
125 }
126 if (channel >= 1 && channel <= 13) {
127 if (!support11n) {
128 //20MHz channel
129 return 81;
130 }
131 if (channel <= 9) {
132 // HT40 with secondary channel above primary
133 return 83;
134 }
135 // HT40 with secondary channel below primary
136 return 84;
137 }
138 // Error
139 return 0;
140 }
141
142 // 5GHz Band
143 if ((band & IHostapd::BandMask::BAND_5_GHZ) != 0) {
144 if (support11ac) {
145 switch (channel) {
146 case 42:
147 case 58:
148 case 106:
149 case 122:
150 case 138:
151 case 155:
152 // 80MHz channel
153 return 128;
154 case 50:
155 case 114:
156 // 160MHz channel
157 return 129;
158 }
159 }
160
161 if (!support11n) {
162 if (channel >= 36 && channel <= 48) {
163 return 115;
164 }
165 if (channel >= 52 && channel <= 64) {
166 return 118;
167 }
168 if (channel >= 100 && channel <= 144) {
169 return 121;
170 }
171 if (channel >= 149 && channel <= 161) {
172 return 124;
173 }
174 if (channel >= 165 && channel <= 169) {
175 return 125;
176 }
177 } else {
178 switch (channel) {
179 case 36:
180 case 44:
181 // HT40 with secondary channel above primary
182 return 116;
183 case 40:
184 case 48:
185 // HT40 with secondary channel below primary
186 return 117;
187 case 52:
188 case 60:
189 // HT40 with secondary channel above primary
190 return 119;
191 case 56:
192 case 64:
193 // HT40 with secondary channel below primary
194 return 120;
195 case 100:
196 case 108:
197 case 116:
198 case 124:
199 case 132:
200 case 140:
201 // HT40 with secondary channel above primary
202 return 122;
203 case 104:
204 case 112:
205 case 120:
206 case 128:
207 case 136:
208 case 144:
209 // HT40 with secondary channel below primary
210 return 123;
211 case 149:
212 case 157:
213 // HT40 with secondary channel above primary
214 return 126;
215 case 153:
216 case 161:
217 // HT40 with secondary channel below primary
218 return 127;
219 }
220 }
221 // Error
222 return 0;
223 }
224
225 // 6GHz Band
226 if ((band & IHostapd::BandMask::BAND_6_GHZ) != 0) {
227 // Channels 1, 5. 9, 13, ...
228 if ((channel & 0x03) == 0x01) {
229 // 20MHz channel
230 return 131;
231 }
232 // Channels 3, 11, 19, 27, ...
233 if ((channel & 0x07) == 0x03) {
234 // 40MHz channel
235 return 132;
236 }
237 // Channels 7, 23, 39, 55, ...
238 if ((channel & 0x0F) == 0x07) {
239 // 80MHz channel
240 return 133;
241 }
242 // Channels 15, 47, 69, ...
243 if ((channel & 0x1F) == 0x0F) {
244 // 160MHz channel
245 return 134;
246 }
Kai Shia0cfc142020-09-16 20:05:13 -0700247 if (channel == 2) {
248 // 20MHz channel
249 return 136;
250 }
Ahmed ElArabawy7b4b40a2020-01-02 08:55:38 -0800251 // Error
252 return 0;
253 }
254
Jimmy Chene8a885b2020-11-05 16:23:37 +0800255 if ((band & IHostapd::BandMask::BAND_60_GHZ) != 0) {
256 if (1 <= channel && channel <= 8) {
257 return 180;
258 } else if (9 <= channel && channel <= 15) {
259 return 181;
260 } else if (17 <= channel && channel <= 22) {
261 return 182;
262 } else if (25 <= channel && channel <= 29) {
263 return 183;
264 }
265 // Error
266 return 0;
267 }
268
Ahmed ElArabawy7b4b40a2020-01-02 08:55:38 -0800269 return 0;
270}
271
lesldd48efd2019-12-26 15:13:51 +0800272bool validatePassphrase(int passphrase_len, int min_len, int max_len)
273{
274 if (min_len != -1 && passphrase_len < min_len) return false;
275 if (max_len != -1 && passphrase_len > max_len) return false;
276 return true;
277}
278
Roshan Pius30b452e2017-12-27 13:36:21 -0800279std::string CreateHostapdConfig(
lesl31294252020-11-06 17:06:03 +0800280 const android::hardware::wifi::hostapd::V1_3::IHostapd::IfaceParams& iface_params,
281 const android::hardware::wifi::hostapd::V1_3::IHostapd::ChannelParams& channelParams,
lesl0cb0e272020-10-30 16:58:08 +0800282 const IHostapd::NetworkParams& nw_params,
283 const std::string br_name)
Roshan Pius30b452e2017-12-27 13:36:21 -0800284{
leslb0f4d542020-09-16 23:06:03 +0800285 if (nw_params.V1_2.V1_0.ssid.size() >
Roshan Pius30b452e2017-12-27 13:36:21 -0800286 static_cast<uint32_t>(
287 IHostapd::ParamSizeLimits::SSID_MAX_LEN_IN_BYTES)) {
288 wpa_printf(
leslb0f4d542020-09-16 23:06:03 +0800289 MSG_ERROR, "Invalid SSID size: %zu", nw_params.V1_2.V1_0.ssid.size());
Roshan Pius30b452e2017-12-27 13:36:21 -0800290 return "";
291 }
292
293 // SSID string
294 std::stringstream ss;
295 ss << std::hex;
296 ss << std::setfill('0');
leslb0f4d542020-09-16 23:06:03 +0800297 for (uint8_t b : nw_params.V1_2.V1_0.ssid) {
Roshan Pius30b452e2017-12-27 13:36:21 -0800298 ss << std::setw(2) << static_cast<unsigned int>(b);
299 }
300 const std::string ssid_as_string = ss.str();
301
302 // Encryption config string
Kai Shi35d8ea42020-10-07 15:24:01 -0700303 uint32_t band = 0;
Jimmy Chene8a885b2020-11-05 16:23:37 +0800304 band |= channelParams.bandMask;
Kai Shi35d8ea42020-10-07 15:24:01 -0700305 bool is_6Ghz_band_only = band == static_cast<uint32_t>(IHostapd::BandMask::BAND_6_GHZ);
Jimmy Chene8a885b2020-11-05 16:23:37 +0800306 bool is_60Ghz_band_only = band == static_cast<uint32_t>(IHostapd::BandMask::BAND_60_GHZ);
Roshan Pius30b452e2017-12-27 13:36:21 -0800307 std::string encryption_config_as_string;
leslb0f4d542020-09-16 23:06:03 +0800308 switch (nw_params.V1_2.encryptionType) {
Roshan Pius30b452e2017-12-27 13:36:21 -0800309 case IHostapd::EncryptionType::NONE:
310 // no security params
311 break;
312 case IHostapd::EncryptionType::WPA:
lesldd48efd2019-12-26 15:13:51 +0800313 if (!validatePassphrase(
leslb0f4d542020-09-16 23:06:03 +0800314 nw_params.V1_2.passphrase.size(),
lesldd48efd2019-12-26 15:13:51 +0800315 static_cast<uint32_t>(IHostapd::ParamSizeLimits::
316 WPA2_PSK_PASSPHRASE_MIN_LEN_IN_BYTES),
317 static_cast<uint32_t>(IHostapd::ParamSizeLimits::
318 WPA2_PSK_PASSPHRASE_MAX_LEN_IN_BYTES))) {
319 return "";
320 }
Roshan Pius30b452e2017-12-27 13:36:21 -0800321 encryption_config_as_string = StringPrintf(
322 "wpa=3\n"
Jimmy Chene8a885b2020-11-05 16:23:37 +0800323 "wpa_pairwise=%s\n"
Roshan Pius30b452e2017-12-27 13:36:21 -0800324 "wpa_passphrase=%s",
Jimmy Chene8a885b2020-11-05 16:23:37 +0800325 is_60Ghz_band_only ? "GCMP" : "TKIP CCMP",
leslb0f4d542020-09-16 23:06:03 +0800326 nw_params.V1_2.passphrase.c_str());
Roshan Pius30b452e2017-12-27 13:36:21 -0800327 break;
328 case IHostapd::EncryptionType::WPA2:
lesldd48efd2019-12-26 15:13:51 +0800329 if (!validatePassphrase(
leslb0f4d542020-09-16 23:06:03 +0800330 nw_params.V1_2.passphrase.size(),
lesldd48efd2019-12-26 15:13:51 +0800331 static_cast<uint32_t>(IHostapd::ParamSizeLimits::
332 WPA2_PSK_PASSPHRASE_MIN_LEN_IN_BYTES),
333 static_cast<uint32_t>(IHostapd::ParamSizeLimits::
334 WPA2_PSK_PASSPHRASE_MAX_LEN_IN_BYTES))) {
335 return "";
336 }
Roshan Pius30b452e2017-12-27 13:36:21 -0800337 encryption_config_as_string = StringPrintf(
338 "wpa=2\n"
Jimmy Chene8a885b2020-11-05 16:23:37 +0800339 "rsn_pairwise=%s\n"
Roshan Pius30b452e2017-12-27 13:36:21 -0800340 "wpa_passphrase=%s",
Jimmy Chene8a885b2020-11-05 16:23:37 +0800341 is_60Ghz_band_only ? "GCMP" : "CCMP",
leslb0f4d542020-09-16 23:06:03 +0800342 nw_params.V1_2.passphrase.c_str());
lesldd48efd2019-12-26 15:13:51 +0800343 break;
344 case IHostapd::EncryptionType::WPA3_SAE_TRANSITION:
345 if (!validatePassphrase(
leslb0f4d542020-09-16 23:06:03 +0800346 nw_params.V1_2.passphrase.size(),
lesldd48efd2019-12-26 15:13:51 +0800347 static_cast<uint32_t>(IHostapd::ParamSizeLimits::
348 WPA2_PSK_PASSPHRASE_MIN_LEN_IN_BYTES),
349 static_cast<uint32_t>(IHostapd::ParamSizeLimits::
350 WPA2_PSK_PASSPHRASE_MAX_LEN_IN_BYTES))) {
351 return "";
352 }
353 encryption_config_as_string = StringPrintf(
354 "wpa=2\n"
Jimmy Chene8a885b2020-11-05 16:23:37 +0800355 "rsn_pairwise=%s\n"
lesldd48efd2019-12-26 15:13:51 +0800356 "wpa_key_mgmt=WPA-PSK SAE\n"
357 "ieee80211w=1\n"
358 "sae_require_mfp=1\n"
359 "wpa_passphrase=%s\n"
360 "sae_password=%s",
Jimmy Chene8a885b2020-11-05 16:23:37 +0800361 is_60Ghz_band_only ? "GCMP" : "CCMP",
leslb0f4d542020-09-16 23:06:03 +0800362 nw_params.V1_2.passphrase.c_str(),
363 nw_params.V1_2.passphrase.c_str());
lesldd48efd2019-12-26 15:13:51 +0800364 break;
365 case IHostapd::EncryptionType::WPA3_SAE:
leslb0f4d542020-09-16 23:06:03 +0800366 if (!validatePassphrase(nw_params.V1_2.passphrase.size(), 1, -1)) {
lesldd48efd2019-12-26 15:13:51 +0800367 return "";
368 }
369 encryption_config_as_string = StringPrintf(
370 "wpa=2\n"
Jimmy Chene8a885b2020-11-05 16:23:37 +0800371 "rsn_pairwise=%s\n"
lesldd48efd2019-12-26 15:13:51 +0800372 "wpa_key_mgmt=SAE\n"
373 "ieee80211w=2\n"
374 "sae_require_mfp=2\n"
Kai Shi35d8ea42020-10-07 15:24:01 -0700375 "sae_pwe=%d\n"
lesldd48efd2019-12-26 15:13:51 +0800376 "sae_password=%s",
Jimmy Chene8a885b2020-11-05 16:23:37 +0800377 is_60Ghz_band_only ? "GCMP" : "CCMP",
Kai Shi35d8ea42020-10-07 15:24:01 -0700378 is_6Ghz_band_only ? 1 : 2,
leslb0f4d542020-09-16 23:06:03 +0800379 nw_params.V1_2.passphrase.c_str());
Roshan Pius30b452e2017-12-27 13:36:21 -0800380 break;
381 default:
382 wpa_printf(MSG_ERROR, "Unknown encryption type");
383 return "";
384 }
385
386 std::string channel_config_as_string;
Ahmed ElArabawya390f292019-12-21 13:07:02 -0800387 bool isFirst = true;
lesl31294252020-11-06 17:06:03 +0800388 if (channelParams.enableAcs) {
Ahmed ElArabawya390f292019-12-21 13:07:02 -0800389 std::string freqList_as_string;
Daisuke Niwac1bd20f2019-01-09 13:51:02 +0900390 for (const auto &range :
lesl31294252020-11-06 17:06:03 +0800391 channelParams.V1_2.acsChannelFreqRangesMhz) {
Ahmed ElArabawya390f292019-12-21 13:07:02 -0800392 if (!isFirst) {
393 freqList_as_string += ",";
394 }
395 isFirst = false;
396
Daisuke Niwac1bd20f2019-01-09 13:51:02 +0900397 if (range.start != range.end) {
Ahmed ElArabawya390f292019-12-21 13:07:02 -0800398 freqList_as_string +=
399 StringPrintf("%d-%d", range.start, range.end);
Daisuke Niwac1bd20f2019-01-09 13:51:02 +0900400 } else {
Ahmed ElArabawya390f292019-12-21 13:07:02 -0800401 freqList_as_string += StringPrintf("%d", range.start);
Daisuke Niwac1bd20f2019-01-09 13:51:02 +0900402 }
403 }
Roshan Pius30b452e2017-12-27 13:36:21 -0800404 channel_config_as_string = StringPrintf(
405 "channel=0\n"
Daisuke Niwac1bd20f2019-01-09 13:51:02 +0900406 "acs_exclude_dfs=%d\n"
Ahmed ElArabawya390f292019-12-21 13:07:02 -0800407 "freqlist=%s",
lesl31294252020-11-06 17:06:03 +0800408 iface_params.V1_2.V1_1.V1_0.channelParams.acsShouldExcludeDfs,
Ahmed ElArabawya390f292019-12-21 13:07:02 -0800409 freqList_as_string.c_str());
Roshan Pius30b452e2017-12-27 13:36:21 -0800410 } else {
Ahmed ElArabawy7b4b40a2020-01-02 08:55:38 -0800411 int op_class = getOpClassForChannel(
lesl31294252020-11-06 17:06:03 +0800412 channelParams.channel,
Ahmed ElArabawy7b4b40a2020-01-02 08:55:38 -0800413 band,
lesl31294252020-11-06 17:06:03 +0800414 iface_params.V1_2.V1_1.V1_0.hwModeParams.enable80211N,
415 iface_params.V1_2.V1_1.V1_0.hwModeParams.enable80211AC);
Roshan Pius30b452e2017-12-27 13:36:21 -0800416 channel_config_as_string = StringPrintf(
Ahmed ElArabawy7b4b40a2020-01-02 08:55:38 -0800417 "channel=%d\n"
418 "op_class=%d",
lesl31294252020-11-06 17:06:03 +0800419 channelParams.channel, op_class);
Roshan Pius30b452e2017-12-27 13:36:21 -0800420 }
421
Roshan Pius30b452e2017-12-27 13:36:21 -0800422 std::string hw_mode_as_string;
Roshan Pius49446472018-03-07 10:23:46 -0800423 std::string ht_cap_vht_oper_chwidth_as_string;
Jimmy Chene8a885b2020-11-05 16:23:37 +0800424 std::string enable_edmg_as_string;
425 std::string edmg_channel_as_string;
426 bool is_60Ghz_used = false;
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800427
Jimmy Chene8a885b2020-11-05 16:23:37 +0800428 if (((band & IHostapd::BandMask::BAND_60_GHZ) != 0)) {
429 hw_mode_as_string = "hw_mode=ad";
430 if (iface_params.hwModeParams.enableEdmg) {
431 enable_edmg_as_string = "enable_edmg=1";
432 edmg_channel_as_string = StringPrintf(
433 "edmg_channel=%d",
434 channelParams.channel);
435 }
436 is_60Ghz_used = true;
437 } else if ((band & IHostapd::BandMask::BAND_2_GHZ) != 0) {
Ahmed ElArabawya390f292019-12-21 13:07:02 -0800438 if (((band & IHostapd::BandMask::BAND_5_GHZ) != 0)
439 || ((band & IHostapd::BandMask::BAND_6_GHZ) != 0)) {
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800440 hw_mode_as_string = "hw_mode=any";
lesl31294252020-11-06 17:06:03 +0800441 if (iface_params.V1_2.V1_1.V1_0.hwModeParams.enable80211AC) {
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800442 ht_cap_vht_oper_chwidth_as_string =
443 "ht_capab=[HT40+]\n"
444 "vht_oper_chwidth=1";
445 }
446 } else {
447 hw_mode_as_string = "hw_mode=g";
Roshan Pius49446472018-03-07 10:23:46 -0800448 }
Jimmy Chene8a885b2020-11-05 16:23:37 +0800449 } else if (((band & IHostapd::BandMask::BAND_5_GHZ) != 0)
Ahmed ElArabawya390f292019-12-21 13:07:02 -0800450 || ((band & IHostapd::BandMask::BAND_6_GHZ) != 0)) {
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800451 hw_mode_as_string = "hw_mode=a";
Jimmy Chene8a885b2020-11-05 16:23:37 +0800452 if (iface_params.V1_2.V1_1.V1_0.hwModeParams.enable80211AC) {
453 ht_cap_vht_oper_chwidth_as_string =
454 "ht_capab=[HT40+]\n"
455 "vht_oper_chwidth=1";
Roshan Pius49446472018-03-07 10:23:46 -0800456 }
Jimmy Chene8a885b2020-11-05 16:23:37 +0800457 } else {
458 wpa_printf(MSG_ERROR, "Invalid band");
459 return "";
Roshan Pius30b452e2017-12-27 13:36:21 -0800460 }
461
Ahmed ElArabawy5362fb12020-01-02 15:01:31 -0800462 std::string he_params_as_string;
Roshan Pius36510302020-05-09 21:01:01 -0700463#ifdef CONFIG_IEEE80211AX
Jimmy Chene8a885b2020-11-05 16:23:37 +0800464 if (iface_params.V1_2.hwModeParams.enable80211AX && !is_60Ghz_used) {
Ahmed ElArabawy5362fb12020-01-02 15:01:31 -0800465 he_params_as_string = StringPrintf(
466 "ieee80211ax=1\n"
467 "he_su_beamformer=%d\n"
468 "he_su_beamformee=%d\n"
469 "he_mu_beamformer=%d\n"
Ahmed ElArabawy5362fb12020-01-02 15:01:31 -0800470 "he_twt_required=%d\n",
lesl31294252020-11-06 17:06:03 +0800471 iface_params.V1_2.hwModeParams.enableHeSingleUserBeamformer ? 1 : 0,
472 iface_params.V1_2.hwModeParams.enableHeSingleUserBeamformee ? 1 : 0,
473 iface_params.V1_2.hwModeParams.enableHeMultiUserBeamformer ? 1 : 0,
474 iface_params.V1_2.hwModeParams.enableHeTargetWakeTime ? 1 : 0);
Ahmed ElArabawy5362fb12020-01-02 15:01:31 -0800475 } else {
476 he_params_as_string = "ieee80211ax=0";
477 }
Roshan Pius36510302020-05-09 21:01:01 -0700478#endif /* CONFIG_IEEE80211AX */
Ahmed ElArabawy5362fb12020-01-02 15:01:31 -0800479
leslb0f4d542020-09-16 23:06:03 +0800480#ifdef CONFIG_INTERWORKING
481 std::string access_network_params_as_string;
482 if (nw_params.isMetered) {
483 access_network_params_as_string = StringPrintf(
484 "interworking=1\n"
485 "access_network_type=2\n"); // CHARGEABLE_PUBLIC_NETWORK
486 } else {
487 access_network_params_as_string = StringPrintf(
488 "interworking=0\n");
489 }
490#endif /* CONFIG_INTERWORKING */
491
lesl0cb0e272020-10-30 16:58:08 +0800492 std::string bridge_as_string;
493 if (!br_name.empty()) {
494 bridge_as_string = StringPrintf("bridge=%s", br_name.c_str());
495 }
496
Roshan Pius30b452e2017-12-27 13:36:21 -0800497 return StringPrintf(
498 "interface=%s\n"
499 "driver=nl80211\n"
Daichi Ueuracaa74a82018-02-14 22:25:39 +0900500 "ctrl_interface=/data/vendor/wifi/hostapd/ctrl\n"
Roshan Pius30b452e2017-12-27 13:36:21 -0800501 // ssid2 signals to hostapd that the value is not a literal value
502 // for use as a SSID. In this case, we're giving it a hex
503 // std::string and hostapd needs to expect that.
504 "ssid2=%s\n"
505 "%s\n"
506 "ieee80211n=%d\n"
507 "ieee80211ac=%d\n"
Ahmed ElArabawy5362fb12020-01-02 15:01:31 -0800508 "%s\n"
Roshan Piuse453f712018-02-09 15:49:57 -0800509 "%s\n"
Roshan Pius49446472018-03-07 10:23:46 -0800510 "%s\n"
Roshan Pius30b452e2017-12-27 13:36:21 -0800511 "ignore_broadcast_ssid=%d\n"
512 "wowlan_triggers=any\n"
leslb0f4d542020-09-16 23:06:03 +0800513#ifdef CONFIG_INTERWORKING
514 "%s\n"
515#endif /* CONFIG_INTERWORKING */
lesl0cb0e272020-10-30 16:58:08 +0800516 "%s\n"
Jimmy Chene8a885b2020-11-05 16:23:37 +0800517 "%s\n"
518 "%s\n"
Roshan Pius30b452e2017-12-27 13:36:21 -0800519 "%s\n",
lesl31294252020-11-06 17:06:03 +0800520 iface_params.V1_2.V1_1.V1_0.ifaceName.c_str(), ssid_as_string.c_str(),
Roshan Pius30b452e2017-12-27 13:36:21 -0800521 channel_config_as_string.c_str(),
lesl31294252020-11-06 17:06:03 +0800522 iface_params.V1_2.V1_1.V1_0.hwModeParams.enable80211N ? 1 : 0,
523 iface_params.V1_2.V1_1.V1_0.hwModeParams.enable80211AC ? 1 : 0,
Ahmed ElArabawy5362fb12020-01-02 15:01:31 -0800524 he_params_as_string.c_str(),
Roshan Pius49446472018-03-07 10:23:46 -0800525 hw_mode_as_string.c_str(), ht_cap_vht_oper_chwidth_as_string.c_str(),
leslb0f4d542020-09-16 23:06:03 +0800526 nw_params.V1_2.V1_0.isHidden ? 1 : 0,
527#ifdef CONFIG_INTERWORKING
lesl0cb0e272020-10-30 16:58:08 +0800528 access_network_params_as_string.c_str(),
leslb0f4d542020-09-16 23:06:03 +0800529#endif /* CONFIG_INTERWORKING */
lesl0cb0e272020-10-30 16:58:08 +0800530 encryption_config_as_string.c_str(),
Jimmy Chene8a885b2020-11-05 16:23:37 +0800531 bridge_as_string.c_str(),
532 enable_edmg_as_string.c_str(),
533 edmg_channel_as_string.c_str());
Roshan Pius30b452e2017-12-27 13:36:21 -0800534}
Roshan Piuse2d21012018-08-17 11:22:06 -0700535
lesldab91502020-08-17 18:29:47 +0800536Generation getGeneration(hostapd_hw_modes *current_mode)
537{
lesl012c1ce2021-03-22 21:50:49 +0800538 wpa_printf(MSG_DEBUG, "getGeneration hwmode=%d, ht_enabled=%d,"
539 " vht_enabled=%d, he_supported=%d",
lesldab91502020-08-17 18:29:47 +0800540 current_mode->mode, current_mode->ht_capab != 0,
lesl012c1ce2021-03-22 21:50:49 +0800541 current_mode->vht_capab != 0, current_mode->he_capab->he_supported);
lesldab91502020-08-17 18:29:47 +0800542 switch (current_mode->mode) {
543 case HOSTAPD_MODE_IEEE80211B:
544 return Generation::WIFI_STANDARD_LEGACY;
545 case HOSTAPD_MODE_IEEE80211G:
546 return current_mode->ht_capab == 0 ?
547 Generation::WIFI_STANDARD_LEGACY : Generation::WIFI_STANDARD_11N;
548 case HOSTAPD_MODE_IEEE80211A:
lesl012c1ce2021-03-22 21:50:49 +0800549 if (current_mode->he_capab->he_supported) {
550 return Generation::WIFI_STANDARD_11AX;
551 }
lesldab91502020-08-17 18:29:47 +0800552 return current_mode->vht_capab == 0 ?
553 Generation::WIFI_STANDARD_11N : Generation::WIFI_STANDARD_11AC;
Jimmy Chene8a885b2020-11-05 16:23:37 +0800554 case HOSTAPD_MODE_IEEE80211AD:
555 return Generation::WIFI_STANDARD_11AD;
lesldab91502020-08-17 18:29:47 +0800556 default:
557 return Generation::WIFI_STANDARD_UNKNOWN;
558 }
559}
560
561Bandwidth getBandwidth(struct hostapd_config *iconf)
562{
563 wpa_printf(MSG_DEBUG, "getBandwidth %d, isHT=%d, isHT40=%d",
564 iconf->vht_oper_chwidth, iconf->ieee80211n,
565 iconf->secondary_channel);
566 switch (iconf->vht_oper_chwidth) {
567 case CHANWIDTH_80MHZ:
568 return Bandwidth::WIFI_BANDWIDTH_80;
569 case CHANWIDTH_80P80MHZ:
570 return Bandwidth::WIFI_BANDWIDTH_80P80;
571 break;
572 case CHANWIDTH_160MHZ:
573 return Bandwidth::WIFI_BANDWIDTH_160;
574 break;
575 case CHANWIDTH_USE_HT:
576 if (iconf->ieee80211n) {
577 return iconf->secondary_channel != 0 ?
578 Bandwidth::WIFI_BANDWIDTH_40 : Bandwidth::WIFI_BANDWIDTH_20;
579 }
580 return Bandwidth::WIFI_BANDWIDTH_20_NOHT;
Jimmy Chene8a885b2020-11-05 16:23:37 +0800581 case CHANWIDTH_2160MHZ:
582 return Bandwidth::WIFI_BANDWIDTH_2160;
583 case CHANWIDTH_4320MHZ:
584 return Bandwidth::WIFI_BANDWIDTH_4320;
585 case CHANWIDTH_6480MHZ:
586 return Bandwidth::WIFI_BANDWIDTH_6480;
587 case CHANWIDTH_8640MHZ:
588 return Bandwidth::WIFI_BANDWIDTH_8640;
lesldab91502020-08-17 18:29:47 +0800589 default:
590 return Bandwidth::WIFI_BANDWIDTH_INVALID;
591 }
592}
593
lesl8c282e52020-12-03 18:28:49 +0800594bool forceStaDisconnection(struct hostapd_data* hapd,
595 const std::array<uint8_t, 6>& client_address,
596 const uint16_t reason_code) {
597 struct sta_info *sta;
598 for (sta = hapd->sta_list; sta; sta = sta->next) {
599 int res;
600 res = memcmp(sta->addr, client_address.data(), ETH_ALEN);
601 if (res == 0) {
602 wpa_printf(MSG_INFO, "Force client:" MACSTR " disconnect with reason: %d",
603 MAC2STR(client_address.data()), reason_code);
604 ap_sta_disconnect(hapd, sta, sta->addr, reason_code);
605 return true;
606 }
607 }
608 return false;
609}
610
Roshan Piuse2d21012018-08-17 11:22:06 -0700611// hostapd core functions accept "C" style function pointers, so use global
612// functions to pass to the hostapd core function and store the corresponding
613// std::function methods to be invoked.
614//
615// NOTE: Using the pattern from the vendor HAL (wifi_legacy_hal.cpp).
616//
617// Callback to be invoked once setup is complete
618std::function<void(struct hostapd_data*)> on_setup_complete_internal_callback;
619void onAsyncSetupCompleteCb(void* ctx)
620{
621 struct hostapd_data* iface_hapd = (struct hostapd_data*)ctx;
622 if (on_setup_complete_internal_callback) {
623 on_setup_complete_internal_callback(iface_hapd);
624 // Invalidate this callback since we don't want this firing
lesl0cb0e272020-10-30 16:58:08 +0800625 // again in single AP mode.
626 if (strlen(iface_hapd->conf->bridge) > 0) {
627 on_setup_complete_internal_callback = nullptr;
628 }
Roshan Piuse2d21012018-08-17 11:22:06 -0700629 }
630}
lesld0621052020-08-10 13:54:28 +0800631
632// Callback to be invoked on hotspot client connection/disconnection
633std::function<void(struct hostapd_data*, const u8 *mac_addr, int authorized,
634 const u8 *p2p_dev_addr)> on_sta_authorized_internal_callback;
635void onAsyncStaAuthorizedCb(void* ctx, const u8 *mac_addr, int authorized,
636 const u8 *p2p_dev_addr)
637{
638 struct hostapd_data* iface_hapd = (struct hostapd_data*)ctx;
639 if (on_sta_authorized_internal_callback) {
640 on_sta_authorized_internal_callback(iface_hapd, mac_addr,
641 authorized, p2p_dev_addr);
642 }
643}
644
lesldab91502020-08-17 18:29:47 +0800645std::function<void(struct hostapd_data*, int level,
646 enum wpa_msg_type type, const char *txt,
647 size_t len)> on_wpa_msg_internal_callback;
648
649void onAsyncWpaEventCb(void *ctx, int level,
650 enum wpa_msg_type type, const char *txt,
651 size_t len)
652{
653 struct hostapd_data* iface_hapd = (struct hostapd_data*)ctx;
654 if (on_wpa_msg_internal_callback) {
655 on_wpa_msg_internal_callback(iface_hapd, level,
656 type, txt, len);
657 }
658}
659
660
Roshan Pius30b452e2017-12-27 13:36:21 -0800661} // namespace
662
Roshan Piuscc817562017-12-22 14:45:05 -0800663namespace android {
664namespace hardware {
665namespace wifi {
666namespace hostapd {
leslf8b1b402020-08-04 17:09:39 +0800667namespace V1_3 {
Roshan Piuscc817562017-12-22 14:45:05 -0800668namespace implementation {
669using hidl_return_util::call;
Roshan Piuse2d21012018-08-17 11:22:06 -0700670using namespace android::hardware::wifi::hostapd::V1_0;
Roshan Piuscc817562017-12-22 14:45:05 -0800671
Roshan Pius80e5a0f2020-10-19 13:48:22 -0700672Hostapd::Hostapd(struct hapd_interfaces* interfaces)
673 : interfaces_(interfaces), death_notifier_(sp<DeathNotifier>::make())
Roshan Piuscc817562017-12-22 14:45:05 -0800674{}
675
676Return<void> Hostapd::addAccessPoint(
Daisuke Niwac1bd20f2019-01-09 13:51:02 +0900677 const V1_0::IHostapd::IfaceParams& iface_params,
lesldd48efd2019-12-26 15:13:51 +0800678 const V1_0::IHostapd::NetworkParams& nw_params, addAccessPoint_cb _hidl_cb)
Daisuke Niwac1bd20f2019-01-09 13:51:02 +0900679{
680 return call(
681 this, &Hostapd::addAccessPointInternal, _hidl_cb, iface_params,
682 nw_params);
683}
684
685Return<void> Hostapd::addAccessPoint_1_1(
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800686 const V1_1::IHostapd::IfaceParams& iface_params,
lesldd48efd2019-12-26 15:13:51 +0800687 const V1_0::IHostapd::NetworkParams& nw_params, addAccessPoint_cb _hidl_cb)
Roshan Piuscc817562017-12-22 14:45:05 -0800688{
689 return call(
Daisuke Niwac1bd20f2019-01-09 13:51:02 +0900690 this, &Hostapd::addAccessPointInternal_1_1, _hidl_cb, iface_params,
Roshan Piuscc817562017-12-22 14:45:05 -0800691 nw_params);
692}
693
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800694Return<void> Hostapd::addAccessPoint_1_2(
leslb0f4d542020-09-16 23:06:03 +0800695 const V1_2::IHostapd::IfaceParams& iface_params,
696 const V1_2::IHostapd::NetworkParams& nw_params,
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800697 addAccessPoint_1_2_cb _hidl_cb)
698{
699 return call(
700 this, &Hostapd::addAccessPointInternal_1_2, _hidl_cb, iface_params,
701 nw_params);
702}
703
leslb0f4d542020-09-16 23:06:03 +0800704Return<void> Hostapd::addAccessPoint_1_3(
705 const V1_3::IHostapd::IfaceParams& iface_params,
706 const V1_3::IHostapd::NetworkParams& nw_params,
707 addAccessPoint_1_3_cb _hidl_cb)
708{
709 return call(
710 this, &Hostapd::addAccessPointInternal_1_3, _hidl_cb, iface_params,
711 nw_params);
712}
713
Roshan Piuscc817562017-12-22 14:45:05 -0800714Return<void> Hostapd::removeAccessPoint(
715 const hidl_string& iface_name, removeAccessPoint_cb _hidl_cb)
716{
717 return call(
718 this, &Hostapd::removeAccessPointInternal, _hidl_cb, iface_name);
719}
720
Roshan Piuse2d21012018-08-17 11:22:06 -0700721Return<void> Hostapd::terminate()
722{
Roshan Pius3455af42018-02-01 09:19:49 -0800723 wpa_printf(MSG_INFO, "Terminating...");
lesl3b8cc0c2020-11-20 23:44:32 +0800724 // Clear the callback to avoid IPCThreadState shutdown during the
725 // callback event.
726 callbacks_.clear();
Roshan Pius3455af42018-02-01 09:19:49 -0800727 eloop_terminate();
728 return Void();
729}
730
Roshan Piuse2d21012018-08-17 11:22:06 -0700731Return<void> Hostapd::registerCallback(
lesl1a842e62019-12-02 19:00:37 +0800732 const sp<V1_1::IHostapdCallback>& callback, registerCallback_cb _hidl_cb)
Roshan Piuse2d21012018-08-17 11:22:06 -0700733{
734 return call(
735 this, &Hostapd::registerCallbackInternal, _hidl_cb, callback);
736}
737
leslf8b1b402020-08-04 17:09:39 +0800738Return<void> Hostapd::registerCallback_1_3(
739 const sp<V1_3::IHostapdCallback>& callback, registerCallback_1_3_cb _hidl_cb)
740{
741 return call(
742 this, &Hostapd::registerCallbackInternal_1_3, _hidl_cb, callback);
743}
744
lesl1a842e62019-12-02 19:00:37 +0800745Return<void> Hostapd::forceClientDisconnect(
746 const hidl_string& iface_name, const hidl_array<uint8_t, 6>& client_address,
747 V1_2::Ieee80211ReasonCode reason_code, forceClientDisconnect_cb _hidl_cb)
748{
749 return call(
750 this, &Hostapd::forceClientDisconnectInternal, _hidl_cb, iface_name,
751 client_address, reason_code);
752}
753
Roger Wangcede5062019-12-30 12:56:28 +0800754Return<void> Hostapd::setDebugParams(
leslf8b1b402020-08-04 17:09:39 +0800755 V1_2::DebugLevel level, setDebugParams_cb _hidl_cb)
Roger Wangcede5062019-12-30 12:56:28 +0800756{
757 return call(
758 this, &Hostapd::setDebugParamsInternal, _hidl_cb, level);
759}
lesl1a842e62019-12-02 19:00:37 +0800760
761V1_0::HostapdStatus Hostapd::addAccessPointInternal(
Daisuke Niwac1bd20f2019-01-09 13:51:02 +0900762 const V1_0::IHostapd::IfaceParams& iface_params,
lesldd48efd2019-12-26 15:13:51 +0800763 const V1_0::IHostapd::NetworkParams& nw_params)
Daisuke Niwac1bd20f2019-01-09 13:51:02 +0900764{
lesl1a842e62019-12-02 19:00:37 +0800765 return {V1_0::HostapdStatusCode::FAILURE_UNKNOWN, ""};
Daisuke Niwac1bd20f2019-01-09 13:51:02 +0900766}
767
lesl1a842e62019-12-02 19:00:37 +0800768V1_0::HostapdStatus Hostapd::addAccessPointInternal_1_1(
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800769 const V1_1::IHostapd::IfaceParams& iface_params,
lesldd48efd2019-12-26 15:13:51 +0800770 const V1_1::IHostapd::NetworkParams& nw_params)
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800771{
772 return {V1_0::HostapdStatusCode::FAILURE_UNKNOWN, ""};
773}
774
leslf8b1b402020-08-04 17:09:39 +0800775V1_2::HostapdStatus Hostapd::addAccessPointInternal_1_2(
leslb0f4d542020-09-16 23:06:03 +0800776 const V1_2::IHostapd::IfaceParams& iface_params,
777 const V1_2::IHostapd::NetworkParams& nw_params) {
778 return {V1_2::HostapdStatusCode::FAILURE_UNKNOWN, ""};
779}
780
781V1_2::HostapdStatus Hostapd::addAccessPointInternal_1_3(
lesl0cb0e272020-10-30 16:58:08 +0800782 const V1_3::IHostapd::IfaceParams& iface_params,
leslb0f4d542020-09-16 23:06:03 +0800783 const V1_3::IHostapd::NetworkParams& nw_params)
Roshan Piuscc817562017-12-22 14:45:05 -0800784{
lesl0cb0e272020-10-30 16:58:08 +0800785 int channelParamsListSize = iface_params.channelParamsList.size();
786 if (channelParamsListSize == 1) {
787 // Single AP
788 wpa_printf(MSG_INFO, "AddSingleAccessPoint, iface=%s",
789 iface_params.V1_2.V1_1.V1_0.ifaceName.c_str());
lesl31294252020-11-06 17:06:03 +0800790 return addSingleAccessPoint(iface_params, iface_params.channelParamsList[0],
lesl0cb0e272020-10-30 16:58:08 +0800791 nw_params, "");
792 } else if (channelParamsListSize == 2) {
793 // Concurrent APs
794 wpa_printf(MSG_INFO, "AddDualAccessPoint, iface=%s",
795 iface_params.V1_2.V1_1.V1_0.ifaceName.c_str());
796 return addConcurrentAccessPoints(iface_params, nw_params);
797 }
798 return {V1_2::HostapdStatusCode::FAILURE_ARGS_INVALID, ""};
799}
800
801V1_2::HostapdStatus Hostapd::addConcurrentAccessPoints(
802 const V1_3::IHostapd::IfaceParams& iface_params, const V1_3::IHostapd::NetworkParams& nw_params)
803{
804 int channelParamsListSize = iface_params.channelParamsList.size();
805 // Get available interfaces in bridge
806 std::vector<std::string> managed_interfaces;
807 std::string br_name = StringPrintf(
808 "%s", iface_params.V1_2.V1_1.V1_0.ifaceName.c_str());
809 if (!GetInterfacesInBridge(br_name, &managed_interfaces)) {
810 return {V1_2::HostapdStatusCode::FAILURE_UNKNOWN,
811 "Get interfaces in bridge failed."};
812 }
813 if (managed_interfaces.size() < channelParamsListSize) {
814 return {V1_2::HostapdStatusCode::FAILURE_UNKNOWN,
815 "Available interfaces less than requested bands"};
816 }
817 // start BSS on specified bands
818 for (std::size_t i = 0; i < channelParamsListSize; i ++) {
lesl31294252020-11-06 17:06:03 +0800819 V1_3::IHostapd::IfaceParams iface_params_new = iface_params;
820 iface_params_new.V1_2.V1_1.V1_0.ifaceName = managed_interfaces[i];
lesl0cb0e272020-10-30 16:58:08 +0800821 V1_2::HostapdStatus status = addSingleAccessPoint(
lesl31294252020-11-06 17:06:03 +0800822 iface_params_new, iface_params.channelParamsList[i], nw_params, br_name);
lesl0cb0e272020-10-30 16:58:08 +0800823 if (status.code != V1_2::HostapdStatusCode::SUCCESS) {
824 wpa_printf(MSG_ERROR, "Failed to addAccessPoint %s",
825 managed_interfaces[i].c_str());
826 return status;
827 }
828 }
829 // Save bridge interface info
830 br_interfaces_[br_name] = managed_interfaces;
831 return {V1_2::HostapdStatusCode::SUCCESS, ""};
832}
833
834V1_2::HostapdStatus Hostapd::addSingleAccessPoint(
lesl31294252020-11-06 17:06:03 +0800835 const V1_3::IHostapd::IfaceParams& iface_params,
836 const V1_3::IHostapd::ChannelParams& channelParams,
lesl0cb0e272020-10-30 16:58:08 +0800837 const V1_3::IHostapd::NetworkParams& nw_params,
838 const std::string br_name)
839{
lesl31294252020-11-06 17:06:03 +0800840 if (hostapd_get_iface(interfaces_, iface_params.V1_2.V1_1.V1_0.ifaceName.c_str())) {
Roshan Pius087d8692017-12-27 14:11:44 -0800841 wpa_printf(
842 MSG_ERROR, "Interface %s already present",
lesl31294252020-11-06 17:06:03 +0800843 iface_params.V1_2.V1_1.V1_0.ifaceName.c_str());
leslf8b1b402020-08-04 17:09:39 +0800844 return {V1_2::HostapdStatusCode::FAILURE_IFACE_EXISTS, ""};
Roshan Pius087d8692017-12-27 14:11:44 -0800845 }
lesl31294252020-11-06 17:06:03 +0800846 const auto conf_params = CreateHostapdConfig(iface_params, channelParams, nw_params, br_name);
Roshan Pius087d8692017-12-27 14:11:44 -0800847 if (conf_params.empty()) {
848 wpa_printf(MSG_ERROR, "Failed to create config params");
leslf8b1b402020-08-04 17:09:39 +0800849 return {V1_2::HostapdStatusCode::FAILURE_ARGS_INVALID, ""};
Roshan Pius087d8692017-12-27 14:11:44 -0800850 }
851 const auto conf_file_path =
lesl31294252020-11-06 17:06:03 +0800852 WriteHostapdConfig(iface_params.V1_2.V1_1.V1_0.ifaceName, conf_params);
Roshan Pius087d8692017-12-27 14:11:44 -0800853 if (conf_file_path.empty()) {
854 wpa_printf(MSG_ERROR, "Failed to write config file");
leslf8b1b402020-08-04 17:09:39 +0800855 return {V1_2::HostapdStatusCode::FAILURE_UNKNOWN, ""};
Roshan Pius087d8692017-12-27 14:11:44 -0800856 }
857 std::string add_iface_param_str = StringPrintf(
lesl31294252020-11-06 17:06:03 +0800858 "%s config=%s", iface_params.V1_2.V1_1.V1_0.ifaceName.c_str(),
Roshan Pius087d8692017-12-27 14:11:44 -0800859 conf_file_path.c_str());
860 std::vector<char> add_iface_param_vec(
861 add_iface_param_str.begin(), add_iface_param_str.end() + 1);
862 if (hostapd_add_iface(interfaces_, add_iface_param_vec.data()) < 0) {
863 wpa_printf(
864 MSG_ERROR, "Adding interface %s failed",
865 add_iface_param_str.c_str());
leslf8b1b402020-08-04 17:09:39 +0800866 return {V1_2::HostapdStatusCode::FAILURE_UNKNOWN, ""};
Roshan Pius087d8692017-12-27 14:11:44 -0800867 }
868 struct hostapd_data* iface_hapd =
lesl31294252020-11-06 17:06:03 +0800869 hostapd_get_iface(interfaces_, iface_params.V1_2.V1_1.V1_0.ifaceName.c_str());
Roshan Pius087d8692017-12-27 14:11:44 -0800870 WPA_ASSERT(iface_hapd != nullptr && iface_hapd->iface != nullptr);
Roshan Piuse2d21012018-08-17 11:22:06 -0700871 // Register the setup complete callbacks
872 on_setup_complete_internal_callback =
873 [this](struct hostapd_data* iface_hapd) {
874 wpa_printf(
lesl0cb0e272020-10-30 16:58:08 +0800875 MSG_INFO, "AP interface setup completed - state %s",
Roshan Piuse2d21012018-08-17 11:22:06 -0700876 hostapd_state_text(iface_hapd->iface->state));
877 if (iface_hapd->iface->state == HAPD_IFACE_DISABLED) {
878 // Invoke the failure callback on all registered
879 // clients.
880 for (const auto& callback : callbacks_) {
881 callback->onFailure(
882 iface_hapd->conf->iface);
883 }
884 }
885 };
lesld0621052020-08-10 13:54:28 +0800886
887 // Rgegister for new client connect/disconnect indication.
888 on_sta_authorized_internal_callback =
889 [this](struct hostapd_data* iface_hapd, const u8 *mac_addr,
890 int authorized, const u8 *p2p_dev_addr) {
891 wpa_printf(MSG_DEBUG, "notify client " MACSTR " %s",
892 MAC2STR(mac_addr),
893 (authorized) ? "Connected" : "Disconnected");
lesld0621052020-08-10 13:54:28 +0800894 for (const auto &callback : callbacks_) {
lesl0cb0e272020-10-30 16:58:08 +0800895 callback->onConnectedClientsChanged(strlen(iface_hapd->conf->bridge) > 0 ?
896 iface_hapd->conf->bridge : iface_hapd->conf->iface,
lesld0621052020-08-10 13:54:28 +0800897 iface_hapd->conf->iface, mac_addr, authorized);
898 }
899 };
900
lesldab91502020-08-17 18:29:47 +0800901 // Register for wpa_event which used to get channel switch event
902 on_wpa_msg_internal_callback =
903 [this](struct hostapd_data* iface_hapd, int level,
904 enum wpa_msg_type type, const char *txt,
905 size_t len) {
906 wpa_printf(MSG_DEBUG, "Receive wpa msg : %s", txt);
lesleb250ff2020-11-25 12:53:12 +0800907 if (os_strncmp(txt, AP_EVENT_ENABLED,
908 strlen(AP_EVENT_ENABLED)) == 0 ||
909 os_strncmp(txt, WPA_EVENT_CHANNEL_SWITCH,
lesldab91502020-08-17 18:29:47 +0800910 strlen(WPA_EVENT_CHANNEL_SWITCH)) == 0) {
911 for (const auto &callback : callbacks_) {
912 callback->onApInstanceInfoChanged(
lesl0cb0e272020-10-30 16:58:08 +0800913 strlen(iface_hapd->conf->bridge) > 0 ?
914 iface_hapd->conf->bridge : iface_hapd->conf->iface,
915 iface_hapd->conf->iface, iface_hapd->iface->freq,
916 getBandwidth(iface_hapd->iconf),
lesl29973f82020-09-10 22:40:06 +0800917 getGeneration(iface_hapd->iface->current_mode),
918 iface_hapd->own_addr);
lesldab91502020-08-17 18:29:47 +0800919 }
920 }
921 };
lesld0621052020-08-10 13:54:28 +0800922
923 // Setup callback
Roshan Piuse2d21012018-08-17 11:22:06 -0700924 iface_hapd->setup_complete_cb = onAsyncSetupCompleteCb;
925 iface_hapd->setup_complete_cb_ctx = iface_hapd;
lesld0621052020-08-10 13:54:28 +0800926 iface_hapd->sta_authorized_cb = onAsyncStaAuthorizedCb;
927 iface_hapd->sta_authorized_cb_ctx = iface_hapd;
lesldab91502020-08-17 18:29:47 +0800928 wpa_msg_register_cb(onAsyncWpaEventCb);
lesld0621052020-08-10 13:54:28 +0800929
Roshan Pius087d8692017-12-27 14:11:44 -0800930 if (hostapd_enable_iface(iface_hapd->iface) < 0) {
931 wpa_printf(
932 MSG_ERROR, "Enabling interface %s failed",
lesl31294252020-11-06 17:06:03 +0800933 iface_params.V1_2.V1_1.V1_0.ifaceName.c_str());
leslf8b1b402020-08-04 17:09:39 +0800934 return {V1_2::HostapdStatusCode::FAILURE_UNKNOWN, ""};
Roshan Pius087d8692017-12-27 14:11:44 -0800935 }
leslf8b1b402020-08-04 17:09:39 +0800936 return {V1_2::HostapdStatusCode::SUCCESS, ""};
Roshan Piuscc817562017-12-22 14:45:05 -0800937}
938
lesl1a842e62019-12-02 19:00:37 +0800939V1_0::HostapdStatus Hostapd::removeAccessPointInternal(const std::string& iface_name)
Roshan Piuscc817562017-12-22 14:45:05 -0800940{
Roshan Pius087d8692017-12-27 14:11:44 -0800941 std::vector<char> remove_iface_param_vec(
942 iface_name.begin(), iface_name.end() + 1);
943 if (hostapd_remove_iface(interfaces_, remove_iface_param_vec.data()) <
944 0) {
945 wpa_printf(
946 MSG_ERROR, "Removing interface %s failed",
947 iface_name.c_str());
lesl1a842e62019-12-02 19:00:37 +0800948 return {V1_0::HostapdStatusCode::FAILURE_UNKNOWN, ""};
Roshan Pius087d8692017-12-27 14:11:44 -0800949 }
lesl1a842e62019-12-02 19:00:37 +0800950 return {V1_0::HostapdStatusCode::SUCCESS, ""};
Roshan Piuscc817562017-12-22 14:45:05 -0800951}
Roshan Piuse2d21012018-08-17 11:22:06 -0700952
lesl1a842e62019-12-02 19:00:37 +0800953V1_0::HostapdStatus Hostapd::registerCallbackInternal(
954 const sp<V1_1::IHostapdCallback>& callback)
Roshan Piuse2d21012018-08-17 11:22:06 -0700955{
leslf8b1b402020-08-04 17:09:39 +0800956 return {V1_0::HostapdStatusCode::FAILURE_UNKNOWN, ""};
957}
958
959V1_2::HostapdStatus Hostapd::registerCallbackInternal_1_3(
960 const sp<V1_3::IHostapdCallback>& callback)
961{
Roshan Pius80e5a0f2020-10-19 13:48:22 -0700962 if (!callback->linkToDeath(death_notifier_, 0)) {
963 wpa_printf(
964 MSG_ERROR,
965 "Error registering for death notification for "
966 "hostapd callback object");
967 return {V1_2::HostapdStatusCode::FAILURE_UNKNOWN, ""};
968 }
Roshan Piuse2d21012018-08-17 11:22:06 -0700969 callbacks_.push_back(callback);
leslf8b1b402020-08-04 17:09:39 +0800970 return {V1_2::HostapdStatusCode::SUCCESS, ""};
lesl1a842e62019-12-02 19:00:37 +0800971}
972
973V1_2::HostapdStatus Hostapd::forceClientDisconnectInternal(const std::string& iface_name,
974 const std::array<uint8_t, 6>& client_address, V1_2::Ieee80211ReasonCode reason_code)
975{
976 struct hostapd_data *hapd = hostapd_get_iface(interfaces_, iface_name.c_str());
lesl8c282e52020-12-03 18:28:49 +0800977 bool result;
978 if (!hapd) {
979 for (auto const& iface : br_interfaces_) {
980 if (iface.first == iface_name) {
981 for (auto const& instance : iface.second) {
982 hapd = hostapd_get_iface(interfaces_, instance.c_str());
983 if (hapd) {
984 result = forceStaDisconnection(hapd, client_address,
985 (uint16_t) reason_code);
986 if (result) break;
987 }
988 }
989 }
990 }
991 } else {
992 result = forceStaDisconnection(hapd, client_address, (uint16_t) reason_code);
993 }
lesl1a842e62019-12-02 19:00:37 +0800994 if (!hapd) {
995 wpa_printf(MSG_ERROR, "Interface %s doesn't exist", iface_name.c_str());
996 return {V1_2::HostapdStatusCode::FAILURE_IFACE_UNKNOWN, ""};
997 }
lesl8c282e52020-12-03 18:28:49 +0800998 if (result) {
999 return {V1_2::HostapdStatusCode::SUCCESS, ""};
lesl1a842e62019-12-02 19:00:37 +08001000 }
1001 return {V1_2::HostapdStatusCode::FAILURE_CLIENT_UNKNOWN, ""};
Roshan Piuse2d21012018-08-17 11:22:06 -07001002}
1003
leslf8b1b402020-08-04 17:09:39 +08001004V1_2::HostapdStatus Hostapd::setDebugParamsInternal(V1_2::DebugLevel level)
Roger Wangcede5062019-12-30 12:56:28 +08001005{
1006 wpa_debug_level = static_cast<uint32_t>(level);
1007 return {V1_2::HostapdStatusCode::SUCCESS, ""};
1008}
1009
Roshan Piuscc817562017-12-22 14:45:05 -08001010} // namespace implementation
leslf8b1b402020-08-04 17:09:39 +08001011} // namespace V1_3
Roshan Piuscc817562017-12-22 14:45:05 -08001012} // namespace hostapd
1013} // namespace wifi
1014} // namespace hardware
1015} // namespace android