blob: b68ddc776951f56dcf2dd56097021e476177069d [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>
13
14#include <android-base/file.h>
15#include <android-base/stringprintf.h>
Roshan Piuscc817562017-12-22 14:45:05 -080016
17#include "hostapd.h"
18#include "hidl_return_util.h"
19
Roshan Pius3455af42018-02-01 09:19:49 -080020extern "C"
21{
lesldab91502020-08-17 18:29:47 +080022#include "common/wpa_ctrl.h"
Roshan Pius3455af42018-02-01 09:19:49 -080023#include "utils/eloop.h"
24}
25
Roshan Pius30b452e2017-12-27 13:36:21 -080026// The HIDL implementation for hostapd creates a hostapd.conf dynamically for
27// each interface. This file can then be used to hook onto the normal config
28// file parsing logic in hostapd code. Helps us to avoid duplication of code
29// in the HIDL interface.
30// TOOD(b/71872409): Add unit tests for this.
31namespace {
32constexpr char kConfFileNameFmt[] = "/data/vendor/wifi/hostapd/hostapd_%s.conf";
33
34using android::base::RemoveFileIfExists;
35using android::base::StringPrintf;
36using android::base::WriteStringToFile;
leslb0f4d542020-09-16 23:06:03 +080037using android::hardware::wifi::hostapd::V1_3::IHostapd;
lesldab91502020-08-17 18:29:47 +080038using android::hardware::wifi::hostapd::V1_3::Generation;
39using android::hardware::wifi::hostapd::V1_3::Bandwidth;
Roshan Pius30b452e2017-12-27 13:36:21 -080040
41std::string WriteHostapdConfig(
42 const std::string& interface_name, const std::string& config)
43{
44 const std::string file_path =
45 StringPrintf(kConfFileNameFmt, interface_name.c_str());
46 if (WriteStringToFile(
47 config, file_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
48 getuid(), getgid())) {
49 return file_path;
50 }
51 // Diagnose failure
52 int error = errno;
53 wpa_printf(
54 MSG_ERROR, "Cannot write hostapd config to %s, error: %s",
55 file_path.c_str(), strerror(error));
56 struct stat st;
57 int result = stat(file_path.c_str(), &st);
58 if (result == 0) {
59 wpa_printf(
60 MSG_ERROR, "hostapd config file uid: %d, gid: %d, mode: %d",
61 st.st_uid, st.st_gid, st.st_mode);
62 } else {
63 wpa_printf(
64 MSG_ERROR,
65 "Error calling stat() on hostapd config file: %s",
66 strerror(errno));
67 }
68 return "";
69}
70
Ahmed ElArabawy7b4b40a2020-01-02 08:55:38 -080071/*
72 * Get the op_class for a channel/band
73 * The logic here is based on Table E-4 in the 802.11 Specification
74 */
75int getOpClassForChannel(int channel, int band, bool support11n, bool support11ac) {
76 // 2GHz Band
77 if ((band & IHostapd::BandMask::BAND_2_GHZ) != 0) {
78 if (channel == 14) {
79 return 82;
80 }
81 if (channel >= 1 && channel <= 13) {
82 if (!support11n) {
83 //20MHz channel
84 return 81;
85 }
86 if (channel <= 9) {
87 // HT40 with secondary channel above primary
88 return 83;
89 }
90 // HT40 with secondary channel below primary
91 return 84;
92 }
93 // Error
94 return 0;
95 }
96
97 // 5GHz Band
98 if ((band & IHostapd::BandMask::BAND_5_GHZ) != 0) {
99 if (support11ac) {
100 switch (channel) {
101 case 42:
102 case 58:
103 case 106:
104 case 122:
105 case 138:
106 case 155:
107 // 80MHz channel
108 return 128;
109 case 50:
110 case 114:
111 // 160MHz channel
112 return 129;
113 }
114 }
115
116 if (!support11n) {
117 if (channel >= 36 && channel <= 48) {
118 return 115;
119 }
120 if (channel >= 52 && channel <= 64) {
121 return 118;
122 }
123 if (channel >= 100 && channel <= 144) {
124 return 121;
125 }
126 if (channel >= 149 && channel <= 161) {
127 return 124;
128 }
129 if (channel >= 165 && channel <= 169) {
130 return 125;
131 }
132 } else {
133 switch (channel) {
134 case 36:
135 case 44:
136 // HT40 with secondary channel above primary
137 return 116;
138 case 40:
139 case 48:
140 // HT40 with secondary channel below primary
141 return 117;
142 case 52:
143 case 60:
144 // HT40 with secondary channel above primary
145 return 119;
146 case 56:
147 case 64:
148 // HT40 with secondary channel below primary
149 return 120;
150 case 100:
151 case 108:
152 case 116:
153 case 124:
154 case 132:
155 case 140:
156 // HT40 with secondary channel above primary
157 return 122;
158 case 104:
159 case 112:
160 case 120:
161 case 128:
162 case 136:
163 case 144:
164 // HT40 with secondary channel below primary
165 return 123;
166 case 149:
167 case 157:
168 // HT40 with secondary channel above primary
169 return 126;
170 case 153:
171 case 161:
172 // HT40 with secondary channel below primary
173 return 127;
174 }
175 }
176 // Error
177 return 0;
178 }
179
180 // 6GHz Band
181 if ((band & IHostapd::BandMask::BAND_6_GHZ) != 0) {
182 // Channels 1, 5. 9, 13, ...
183 if ((channel & 0x03) == 0x01) {
184 // 20MHz channel
185 return 131;
186 }
187 // Channels 3, 11, 19, 27, ...
188 if ((channel & 0x07) == 0x03) {
189 // 40MHz channel
190 return 132;
191 }
192 // Channels 7, 23, 39, 55, ...
193 if ((channel & 0x0F) == 0x07) {
194 // 80MHz channel
195 return 133;
196 }
197 // Channels 15, 47, 69, ...
198 if ((channel & 0x1F) == 0x0F) {
199 // 160MHz channel
200 return 134;
201 }
Kai Shia0cfc142020-09-16 20:05:13 -0700202 if (channel == 2) {
203 // 20MHz channel
204 return 136;
205 }
Ahmed ElArabawy7b4b40a2020-01-02 08:55:38 -0800206 // Error
207 return 0;
208 }
209
210 return 0;
211}
212
lesldd48efd2019-12-26 15:13:51 +0800213bool validatePassphrase(int passphrase_len, int min_len, int max_len)
214{
215 if (min_len != -1 && passphrase_len < min_len) return false;
216 if (max_len != -1 && passphrase_len > max_len) return false;
217 return true;
218}
219
Roshan Pius30b452e2017-12-27 13:36:21 -0800220std::string CreateHostapdConfig(
221 const IHostapd::IfaceParams& iface_params,
222 const IHostapd::NetworkParams& nw_params)
223{
leslb0f4d542020-09-16 23:06:03 +0800224 if (nw_params.V1_2.V1_0.ssid.size() >
Roshan Pius30b452e2017-12-27 13:36:21 -0800225 static_cast<uint32_t>(
226 IHostapd::ParamSizeLimits::SSID_MAX_LEN_IN_BYTES)) {
227 wpa_printf(
leslb0f4d542020-09-16 23:06:03 +0800228 MSG_ERROR, "Invalid SSID size: %zu", nw_params.V1_2.V1_0.ssid.size());
Roshan Pius30b452e2017-12-27 13:36:21 -0800229 return "";
230 }
231
232 // SSID string
233 std::stringstream ss;
234 ss << std::hex;
235 ss << std::setfill('0');
leslb0f4d542020-09-16 23:06:03 +0800236 for (uint8_t b : nw_params.V1_2.V1_0.ssid) {
Roshan Pius30b452e2017-12-27 13:36:21 -0800237 ss << std::setw(2) << static_cast<unsigned int>(b);
238 }
239 const std::string ssid_as_string = ss.str();
240
241 // Encryption config string
242 std::string encryption_config_as_string;
leslb0f4d542020-09-16 23:06:03 +0800243 switch (nw_params.V1_2.encryptionType) {
Roshan Pius30b452e2017-12-27 13:36:21 -0800244 case IHostapd::EncryptionType::NONE:
245 // no security params
246 break;
247 case IHostapd::EncryptionType::WPA:
lesldd48efd2019-12-26 15:13:51 +0800248 if (!validatePassphrase(
leslb0f4d542020-09-16 23:06:03 +0800249 nw_params.V1_2.passphrase.size(),
lesldd48efd2019-12-26 15:13:51 +0800250 static_cast<uint32_t>(IHostapd::ParamSizeLimits::
251 WPA2_PSK_PASSPHRASE_MIN_LEN_IN_BYTES),
252 static_cast<uint32_t>(IHostapd::ParamSizeLimits::
253 WPA2_PSK_PASSPHRASE_MAX_LEN_IN_BYTES))) {
254 return "";
255 }
Roshan Pius30b452e2017-12-27 13:36:21 -0800256 encryption_config_as_string = StringPrintf(
257 "wpa=3\n"
258 "wpa_pairwise=TKIP CCMP\n"
259 "wpa_passphrase=%s",
leslb0f4d542020-09-16 23:06:03 +0800260 nw_params.V1_2.passphrase.c_str());
Roshan Pius30b452e2017-12-27 13:36:21 -0800261 break;
262 case IHostapd::EncryptionType::WPA2:
lesldd48efd2019-12-26 15:13:51 +0800263 if (!validatePassphrase(
leslb0f4d542020-09-16 23:06:03 +0800264 nw_params.V1_2.passphrase.size(),
lesldd48efd2019-12-26 15:13:51 +0800265 static_cast<uint32_t>(IHostapd::ParamSizeLimits::
266 WPA2_PSK_PASSPHRASE_MIN_LEN_IN_BYTES),
267 static_cast<uint32_t>(IHostapd::ParamSizeLimits::
268 WPA2_PSK_PASSPHRASE_MAX_LEN_IN_BYTES))) {
269 return "";
270 }
Roshan Pius30b452e2017-12-27 13:36:21 -0800271 encryption_config_as_string = StringPrintf(
272 "wpa=2\n"
273 "rsn_pairwise=CCMP\n"
274 "wpa_passphrase=%s",
leslb0f4d542020-09-16 23:06:03 +0800275 nw_params.V1_2.passphrase.c_str());
lesldd48efd2019-12-26 15:13:51 +0800276 break;
277 case IHostapd::EncryptionType::WPA3_SAE_TRANSITION:
278 if (!validatePassphrase(
leslb0f4d542020-09-16 23:06:03 +0800279 nw_params.V1_2.passphrase.size(),
lesldd48efd2019-12-26 15:13:51 +0800280 static_cast<uint32_t>(IHostapd::ParamSizeLimits::
281 WPA2_PSK_PASSPHRASE_MIN_LEN_IN_BYTES),
282 static_cast<uint32_t>(IHostapd::ParamSizeLimits::
283 WPA2_PSK_PASSPHRASE_MAX_LEN_IN_BYTES))) {
284 return "";
285 }
286 encryption_config_as_string = StringPrintf(
287 "wpa=2\n"
288 "rsn_pairwise=CCMP\n"
289 "wpa_key_mgmt=WPA-PSK SAE\n"
290 "ieee80211w=1\n"
291 "sae_require_mfp=1\n"
292 "wpa_passphrase=%s\n"
293 "sae_password=%s",
leslb0f4d542020-09-16 23:06:03 +0800294 nw_params.V1_2.passphrase.c_str(),
295 nw_params.V1_2.passphrase.c_str());
lesldd48efd2019-12-26 15:13:51 +0800296 break;
297 case IHostapd::EncryptionType::WPA3_SAE:
leslb0f4d542020-09-16 23:06:03 +0800298 if (!validatePassphrase(nw_params.V1_2.passphrase.size(), 1, -1)) {
lesldd48efd2019-12-26 15:13:51 +0800299 return "";
300 }
301 encryption_config_as_string = StringPrintf(
302 "wpa=2\n"
303 "rsn_pairwise=CCMP\n"
304 "wpa_key_mgmt=SAE\n"
305 "ieee80211w=2\n"
306 "sae_require_mfp=2\n"
307 "sae_password=%s",
leslb0f4d542020-09-16 23:06:03 +0800308 nw_params.V1_2.passphrase.c_str());
Roshan Pius30b452e2017-12-27 13:36:21 -0800309 break;
310 default:
311 wpa_printf(MSG_ERROR, "Unknown encryption type");
312 return "";
313 }
314
Ahmed ElArabawy7b4b40a2020-01-02 08:55:38 -0800315 unsigned int band = 0;
316 band |= iface_params.channelParams.bandMask;
317
Roshan Pius30b452e2017-12-27 13:36:21 -0800318 std::string channel_config_as_string;
Ahmed ElArabawya390f292019-12-21 13:07:02 -0800319 bool isFirst = true;
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800320 if (iface_params.V1_1.V1_0.channelParams.enableAcs) {
Ahmed ElArabawya390f292019-12-21 13:07:02 -0800321 std::string freqList_as_string;
Daisuke Niwac1bd20f2019-01-09 13:51:02 +0900322 for (const auto &range :
Ahmed ElArabawya390f292019-12-21 13:07:02 -0800323 iface_params.channelParams.acsChannelFreqRangesMhz) {
324 if (!isFirst) {
325 freqList_as_string += ",";
326 }
327 isFirst = false;
328
Daisuke Niwac1bd20f2019-01-09 13:51:02 +0900329 if (range.start != range.end) {
Ahmed ElArabawya390f292019-12-21 13:07:02 -0800330 freqList_as_string +=
331 StringPrintf("%d-%d", range.start, range.end);
Daisuke Niwac1bd20f2019-01-09 13:51:02 +0900332 } else {
Ahmed ElArabawya390f292019-12-21 13:07:02 -0800333 freqList_as_string += StringPrintf("%d", range.start);
Daisuke Niwac1bd20f2019-01-09 13:51:02 +0900334 }
335 }
Roshan Pius30b452e2017-12-27 13:36:21 -0800336 channel_config_as_string = StringPrintf(
337 "channel=0\n"
Daisuke Niwac1bd20f2019-01-09 13:51:02 +0900338 "acs_exclude_dfs=%d\n"
Ahmed ElArabawya390f292019-12-21 13:07:02 -0800339 "freqlist=%s",
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800340 iface_params.V1_1.V1_0.channelParams.acsShouldExcludeDfs,
Ahmed ElArabawya390f292019-12-21 13:07:02 -0800341 freqList_as_string.c_str());
Roshan Pius30b452e2017-12-27 13:36:21 -0800342 } else {
Ahmed ElArabawy7b4b40a2020-01-02 08:55:38 -0800343 int op_class = getOpClassForChannel(
344 iface_params.V1_1.V1_0.channelParams.channel,
345 band,
346 iface_params.V1_1.V1_0.hwModeParams.enable80211N,
347 iface_params.V1_1.V1_0.hwModeParams.enable80211AC);
Roshan Pius30b452e2017-12-27 13:36:21 -0800348 channel_config_as_string = StringPrintf(
Ahmed ElArabawy7b4b40a2020-01-02 08:55:38 -0800349 "channel=%d\n"
350 "op_class=%d",
351 iface_params.V1_1.V1_0.channelParams.channel, op_class);
Roshan Pius30b452e2017-12-27 13:36:21 -0800352 }
353
Roshan Pius30b452e2017-12-27 13:36:21 -0800354 std::string hw_mode_as_string;
Roshan Pius49446472018-03-07 10:23:46 -0800355 std::string ht_cap_vht_oper_chwidth_as_string;
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800356
357 if ((band & IHostapd::BandMask::BAND_2_GHZ) != 0) {
Ahmed ElArabawya390f292019-12-21 13:07:02 -0800358 if (((band & IHostapd::BandMask::BAND_5_GHZ) != 0)
359 || ((band & IHostapd::BandMask::BAND_6_GHZ) != 0)) {
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800360 hw_mode_as_string = "hw_mode=any";
361 if (iface_params.V1_1.V1_0.channelParams.enableAcs) {
362 ht_cap_vht_oper_chwidth_as_string =
363 "ht_capab=[HT40+]\n"
364 "vht_oper_chwidth=1";
365 }
366 } else {
367 hw_mode_as_string = "hw_mode=g";
Roshan Pius49446472018-03-07 10:23:46 -0800368 }
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800369 } else {
Ahmed ElArabawya390f292019-12-21 13:07:02 -0800370 if (((band & IHostapd::BandMask::BAND_5_GHZ) != 0)
371 || ((band & IHostapd::BandMask::BAND_6_GHZ) != 0)) {
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800372 hw_mode_as_string = "hw_mode=a";
373 if (iface_params.V1_1.V1_0.channelParams.enableAcs) {
374 ht_cap_vht_oper_chwidth_as_string =
375 "ht_capab=[HT40+]\n"
376 "vht_oper_chwidth=1";
377 }
378 } else {
379 wpa_printf(MSG_ERROR, "Invalid band");
380 return "";
Roshan Pius49446472018-03-07 10:23:46 -0800381 }
Roshan Pius30b452e2017-12-27 13:36:21 -0800382 }
383
Ahmed ElArabawy5362fb12020-01-02 15:01:31 -0800384 std::string he_params_as_string;
Roshan Pius36510302020-05-09 21:01:01 -0700385#ifdef CONFIG_IEEE80211AX
Ahmed ElArabawy5362fb12020-01-02 15:01:31 -0800386 if (iface_params.hwModeParams.enable80211AX) {
387 he_params_as_string = StringPrintf(
388 "ieee80211ax=1\n"
389 "he_su_beamformer=%d\n"
390 "he_su_beamformee=%d\n"
391 "he_mu_beamformer=%d\n"
Ahmed ElArabawy5362fb12020-01-02 15:01:31 -0800392 "he_twt_required=%d\n",
393 iface_params.hwModeParams.enableHeSingleUserBeamformer ? 1 : 0,
394 iface_params.hwModeParams.enableHeSingleUserBeamformee ? 1 : 0,
395 iface_params.hwModeParams.enableHeMultiUserBeamformer ? 1 : 0,
Ahmed ElArabawy5362fb12020-01-02 15:01:31 -0800396 iface_params.hwModeParams.enableHeTargetWakeTime ? 1 : 0);
397 } else {
398 he_params_as_string = "ieee80211ax=0";
399 }
Roshan Pius36510302020-05-09 21:01:01 -0700400#endif /* CONFIG_IEEE80211AX */
Ahmed ElArabawy5362fb12020-01-02 15:01:31 -0800401
leslb0f4d542020-09-16 23:06:03 +0800402#ifdef CONFIG_INTERWORKING
403 std::string access_network_params_as_string;
404 if (nw_params.isMetered) {
405 access_network_params_as_string = StringPrintf(
406 "interworking=1\n"
407 "access_network_type=2\n"); // CHARGEABLE_PUBLIC_NETWORK
408 } else {
409 access_network_params_as_string = StringPrintf(
410 "interworking=0\n");
411 }
412#endif /* CONFIG_INTERWORKING */
413
Roshan Pius30b452e2017-12-27 13:36:21 -0800414 return StringPrintf(
415 "interface=%s\n"
416 "driver=nl80211\n"
Daichi Ueuracaa74a82018-02-14 22:25:39 +0900417 "ctrl_interface=/data/vendor/wifi/hostapd/ctrl\n"
Roshan Pius30b452e2017-12-27 13:36:21 -0800418 // ssid2 signals to hostapd that the value is not a literal value
419 // for use as a SSID. In this case, we're giving it a hex
420 // std::string and hostapd needs to expect that.
421 "ssid2=%s\n"
422 "%s\n"
423 "ieee80211n=%d\n"
424 "ieee80211ac=%d\n"
Ahmed ElArabawy5362fb12020-01-02 15:01:31 -0800425 "%s\n"
Roshan Piuse453f712018-02-09 15:49:57 -0800426 "%s\n"
Roshan Pius49446472018-03-07 10:23:46 -0800427 "%s\n"
Roshan Pius30b452e2017-12-27 13:36:21 -0800428 "ignore_broadcast_ssid=%d\n"
429 "wowlan_triggers=any\n"
leslb0f4d542020-09-16 23:06:03 +0800430#ifdef CONFIG_INTERWORKING
431 "%s\n"
432#endif /* CONFIG_INTERWORKING */
Roshan Pius30b452e2017-12-27 13:36:21 -0800433 "%s\n",
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800434 iface_params.V1_1.V1_0.ifaceName.c_str(), ssid_as_string.c_str(),
Roshan Pius30b452e2017-12-27 13:36:21 -0800435 channel_config_as_string.c_str(),
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800436 iface_params.V1_1.V1_0.hwModeParams.enable80211N ? 1 : 0,
437 iface_params.V1_1.V1_0.hwModeParams.enable80211AC ? 1 : 0,
Ahmed ElArabawy5362fb12020-01-02 15:01:31 -0800438 he_params_as_string.c_str(),
Roshan Pius49446472018-03-07 10:23:46 -0800439 hw_mode_as_string.c_str(), ht_cap_vht_oper_chwidth_as_string.c_str(),
leslb0f4d542020-09-16 23:06:03 +0800440 nw_params.V1_2.V1_0.isHidden ? 1 : 0,
441#ifdef CONFIG_INTERWORKING
442 access_network_params_as_string.c_str(),
443#endif /* CONFIG_INTERWORKING */
444 encryption_config_as_string.c_str());
Roshan Pius30b452e2017-12-27 13:36:21 -0800445}
Roshan Piuse2d21012018-08-17 11:22:06 -0700446
lesldab91502020-08-17 18:29:47 +0800447Generation getGeneration(hostapd_hw_modes *current_mode)
448{
449 wpa_printf(MSG_DEBUG, "getGeneration hwmode=%d, ht_enabled=%d, vht_enabled=%d",
450 current_mode->mode, current_mode->ht_capab != 0,
451 current_mode->vht_capab != 0);
452 switch (current_mode->mode) {
453 case HOSTAPD_MODE_IEEE80211B:
454 return Generation::WIFI_STANDARD_LEGACY;
455 case HOSTAPD_MODE_IEEE80211G:
456 return current_mode->ht_capab == 0 ?
457 Generation::WIFI_STANDARD_LEGACY : Generation::WIFI_STANDARD_11N;
458 case HOSTAPD_MODE_IEEE80211A:
459 return current_mode->vht_capab == 0 ?
460 Generation::WIFI_STANDARD_11N : Generation::WIFI_STANDARD_11AC;
461 // TODO: b/162484222 miss HOSTAPD_MODE_IEEE80211AX definition.
462 default:
463 return Generation::WIFI_STANDARD_UNKNOWN;
464 }
465}
466
467Bandwidth getBandwidth(struct hostapd_config *iconf)
468{
469 wpa_printf(MSG_DEBUG, "getBandwidth %d, isHT=%d, isHT40=%d",
470 iconf->vht_oper_chwidth, iconf->ieee80211n,
471 iconf->secondary_channel);
472 switch (iconf->vht_oper_chwidth) {
473 case CHANWIDTH_80MHZ:
474 return Bandwidth::WIFI_BANDWIDTH_80;
475 case CHANWIDTH_80P80MHZ:
476 return Bandwidth::WIFI_BANDWIDTH_80P80;
477 break;
478 case CHANWIDTH_160MHZ:
479 return Bandwidth::WIFI_BANDWIDTH_160;
480 break;
481 case CHANWIDTH_USE_HT:
482 if (iconf->ieee80211n) {
483 return iconf->secondary_channel != 0 ?
484 Bandwidth::WIFI_BANDWIDTH_40 : Bandwidth::WIFI_BANDWIDTH_20;
485 }
486 return Bandwidth::WIFI_BANDWIDTH_20_NOHT;
487 default:
488 return Bandwidth::WIFI_BANDWIDTH_INVALID;
489 }
490}
491
Roshan Piuse2d21012018-08-17 11:22:06 -0700492// hostapd core functions accept "C" style function pointers, so use global
493// functions to pass to the hostapd core function and store the corresponding
494// std::function methods to be invoked.
495//
496// NOTE: Using the pattern from the vendor HAL (wifi_legacy_hal.cpp).
497//
498// Callback to be invoked once setup is complete
499std::function<void(struct hostapd_data*)> on_setup_complete_internal_callback;
500void onAsyncSetupCompleteCb(void* ctx)
501{
502 struct hostapd_data* iface_hapd = (struct hostapd_data*)ctx;
503 if (on_setup_complete_internal_callback) {
504 on_setup_complete_internal_callback(iface_hapd);
505 // Invalidate this callback since we don't want this firing
506 // again.
507 on_setup_complete_internal_callback = nullptr;
508 }
509}
lesld0621052020-08-10 13:54:28 +0800510
511// Callback to be invoked on hotspot client connection/disconnection
512std::function<void(struct hostapd_data*, const u8 *mac_addr, int authorized,
513 const u8 *p2p_dev_addr)> on_sta_authorized_internal_callback;
514void onAsyncStaAuthorizedCb(void* ctx, const u8 *mac_addr, int authorized,
515 const u8 *p2p_dev_addr)
516{
517 struct hostapd_data* iface_hapd = (struct hostapd_data*)ctx;
518 if (on_sta_authorized_internal_callback) {
519 on_sta_authorized_internal_callback(iface_hapd, mac_addr,
520 authorized, p2p_dev_addr);
521 }
522}
523
lesldab91502020-08-17 18:29:47 +0800524std::function<void(struct hostapd_data*, int level,
525 enum wpa_msg_type type, const char *txt,
526 size_t len)> on_wpa_msg_internal_callback;
527
528void onAsyncWpaEventCb(void *ctx, int level,
529 enum wpa_msg_type type, const char *txt,
530 size_t len)
531{
532 struct hostapd_data* iface_hapd = (struct hostapd_data*)ctx;
533 if (on_wpa_msg_internal_callback) {
534 on_wpa_msg_internal_callback(iface_hapd, level,
535 type, txt, len);
536 }
537}
538
539
Roshan Pius30b452e2017-12-27 13:36:21 -0800540} // namespace
541
Roshan Piuscc817562017-12-22 14:45:05 -0800542namespace android {
543namespace hardware {
544namespace wifi {
545namespace hostapd {
leslf8b1b402020-08-04 17:09:39 +0800546namespace V1_3 {
Roshan Piuscc817562017-12-22 14:45:05 -0800547namespace implementation {
548using hidl_return_util::call;
Roshan Piuse2d21012018-08-17 11:22:06 -0700549using namespace android::hardware::wifi::hostapd::V1_0;
Roshan Piuscc817562017-12-22 14:45:05 -0800550
551Hostapd::Hostapd(struct hapd_interfaces* interfaces) : interfaces_(interfaces)
552{}
553
554Return<void> Hostapd::addAccessPoint(
Daisuke Niwac1bd20f2019-01-09 13:51:02 +0900555 const V1_0::IHostapd::IfaceParams& iface_params,
lesldd48efd2019-12-26 15:13:51 +0800556 const V1_0::IHostapd::NetworkParams& nw_params, addAccessPoint_cb _hidl_cb)
Daisuke Niwac1bd20f2019-01-09 13:51:02 +0900557{
558 return call(
559 this, &Hostapd::addAccessPointInternal, _hidl_cb, iface_params,
560 nw_params);
561}
562
563Return<void> Hostapd::addAccessPoint_1_1(
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800564 const V1_1::IHostapd::IfaceParams& iface_params,
lesldd48efd2019-12-26 15:13:51 +0800565 const V1_0::IHostapd::NetworkParams& nw_params, addAccessPoint_cb _hidl_cb)
Roshan Piuscc817562017-12-22 14:45:05 -0800566{
567 return call(
Daisuke Niwac1bd20f2019-01-09 13:51:02 +0900568 this, &Hostapd::addAccessPointInternal_1_1, _hidl_cb, iface_params,
Roshan Piuscc817562017-12-22 14:45:05 -0800569 nw_params);
570}
571
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800572Return<void> Hostapd::addAccessPoint_1_2(
leslb0f4d542020-09-16 23:06:03 +0800573 const V1_2::IHostapd::IfaceParams& iface_params,
574 const V1_2::IHostapd::NetworkParams& nw_params,
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800575 addAccessPoint_1_2_cb _hidl_cb)
576{
577 return call(
578 this, &Hostapd::addAccessPointInternal_1_2, _hidl_cb, iface_params,
579 nw_params);
580}
581
leslb0f4d542020-09-16 23:06:03 +0800582Return<void> Hostapd::addAccessPoint_1_3(
583 const V1_3::IHostapd::IfaceParams& iface_params,
584 const V1_3::IHostapd::NetworkParams& nw_params,
585 addAccessPoint_1_3_cb _hidl_cb)
586{
587 return call(
588 this, &Hostapd::addAccessPointInternal_1_3, _hidl_cb, iface_params,
589 nw_params);
590}
591
Roshan Piuscc817562017-12-22 14:45:05 -0800592Return<void> Hostapd::removeAccessPoint(
593 const hidl_string& iface_name, removeAccessPoint_cb _hidl_cb)
594{
595 return call(
596 this, &Hostapd::removeAccessPointInternal, _hidl_cb, iface_name);
597}
598
Roshan Piuse2d21012018-08-17 11:22:06 -0700599Return<void> Hostapd::terminate()
600{
Roshan Pius3455af42018-02-01 09:19:49 -0800601 wpa_printf(MSG_INFO, "Terminating...");
602 eloop_terminate();
603 return Void();
604}
605
Roshan Piuse2d21012018-08-17 11:22:06 -0700606Return<void> Hostapd::registerCallback(
lesl1a842e62019-12-02 19:00:37 +0800607 const sp<V1_1::IHostapdCallback>& callback, registerCallback_cb _hidl_cb)
Roshan Piuse2d21012018-08-17 11:22:06 -0700608{
609 return call(
610 this, &Hostapd::registerCallbackInternal, _hidl_cb, callback);
611}
612
leslf8b1b402020-08-04 17:09:39 +0800613Return<void> Hostapd::registerCallback_1_3(
614 const sp<V1_3::IHostapdCallback>& callback, registerCallback_1_3_cb _hidl_cb)
615{
616 return call(
617 this, &Hostapd::registerCallbackInternal_1_3, _hidl_cb, callback);
618}
619
lesl1a842e62019-12-02 19:00:37 +0800620Return<void> Hostapd::forceClientDisconnect(
621 const hidl_string& iface_name, const hidl_array<uint8_t, 6>& client_address,
622 V1_2::Ieee80211ReasonCode reason_code, forceClientDisconnect_cb _hidl_cb)
623{
624 return call(
625 this, &Hostapd::forceClientDisconnectInternal, _hidl_cb, iface_name,
626 client_address, reason_code);
627}
628
Roger Wangcede5062019-12-30 12:56:28 +0800629Return<void> Hostapd::setDebugParams(
leslf8b1b402020-08-04 17:09:39 +0800630 V1_2::DebugLevel level, setDebugParams_cb _hidl_cb)
Roger Wangcede5062019-12-30 12:56:28 +0800631{
632 return call(
633 this, &Hostapd::setDebugParamsInternal, _hidl_cb, level);
634}
lesl1a842e62019-12-02 19:00:37 +0800635
636V1_0::HostapdStatus Hostapd::addAccessPointInternal(
Daisuke Niwac1bd20f2019-01-09 13:51:02 +0900637 const V1_0::IHostapd::IfaceParams& iface_params,
lesldd48efd2019-12-26 15:13:51 +0800638 const V1_0::IHostapd::NetworkParams& nw_params)
Daisuke Niwac1bd20f2019-01-09 13:51:02 +0900639{
lesl1a842e62019-12-02 19:00:37 +0800640 return {V1_0::HostapdStatusCode::FAILURE_UNKNOWN, ""};
Daisuke Niwac1bd20f2019-01-09 13:51:02 +0900641}
642
lesl1a842e62019-12-02 19:00:37 +0800643V1_0::HostapdStatus Hostapd::addAccessPointInternal_1_1(
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800644 const V1_1::IHostapd::IfaceParams& iface_params,
lesldd48efd2019-12-26 15:13:51 +0800645 const V1_1::IHostapd::NetworkParams& nw_params)
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800646{
647 return {V1_0::HostapdStatusCode::FAILURE_UNKNOWN, ""};
648}
649
leslf8b1b402020-08-04 17:09:39 +0800650V1_2::HostapdStatus Hostapd::addAccessPointInternal_1_2(
leslb0f4d542020-09-16 23:06:03 +0800651 const V1_2::IHostapd::IfaceParams& iface_params,
652 const V1_2::IHostapd::NetworkParams& nw_params) {
653 return {V1_2::HostapdStatusCode::FAILURE_UNKNOWN, ""};
654}
655
656V1_2::HostapdStatus Hostapd::addAccessPointInternal_1_3(
657 const V1_2::IHostapd::IfaceParams& iface_params,
658 const V1_3::IHostapd::NetworkParams& nw_params)
Roshan Piuscc817562017-12-22 14:45:05 -0800659{
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800660 if (hostapd_get_iface(interfaces_, iface_params.V1_1.V1_0.ifaceName.c_str())) {
Roshan Pius087d8692017-12-27 14:11:44 -0800661 wpa_printf(
662 MSG_ERROR, "Interface %s already present",
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800663 iface_params.V1_1.V1_0.ifaceName.c_str());
leslf8b1b402020-08-04 17:09:39 +0800664 return {V1_2::HostapdStatusCode::FAILURE_IFACE_EXISTS, ""};
Roshan Pius087d8692017-12-27 14:11:44 -0800665 }
666 const auto conf_params = CreateHostapdConfig(iface_params, nw_params);
667 if (conf_params.empty()) {
668 wpa_printf(MSG_ERROR, "Failed to create config params");
leslf8b1b402020-08-04 17:09:39 +0800669 return {V1_2::HostapdStatusCode::FAILURE_ARGS_INVALID, ""};
Roshan Pius087d8692017-12-27 14:11:44 -0800670 }
671 const auto conf_file_path =
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800672 WriteHostapdConfig(iface_params.V1_1.V1_0.ifaceName, conf_params);
Roshan Pius087d8692017-12-27 14:11:44 -0800673 if (conf_file_path.empty()) {
674 wpa_printf(MSG_ERROR, "Failed to write config file");
leslf8b1b402020-08-04 17:09:39 +0800675 return {V1_2::HostapdStatusCode::FAILURE_UNKNOWN, ""};
Roshan Pius087d8692017-12-27 14:11:44 -0800676 }
677 std::string add_iface_param_str = StringPrintf(
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800678 "%s config=%s", iface_params.V1_1.V1_0.ifaceName.c_str(),
Roshan Pius087d8692017-12-27 14:11:44 -0800679 conf_file_path.c_str());
680 std::vector<char> add_iface_param_vec(
681 add_iface_param_str.begin(), add_iface_param_str.end() + 1);
682 if (hostapd_add_iface(interfaces_, add_iface_param_vec.data()) < 0) {
683 wpa_printf(
684 MSG_ERROR, "Adding interface %s failed",
685 add_iface_param_str.c_str());
leslf8b1b402020-08-04 17:09:39 +0800686 return {V1_2::HostapdStatusCode::FAILURE_UNKNOWN, ""};
Roshan Pius087d8692017-12-27 14:11:44 -0800687 }
688 struct hostapd_data* iface_hapd =
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800689 hostapd_get_iface(interfaces_, iface_params.V1_1.V1_0.ifaceName.c_str());
Roshan Pius087d8692017-12-27 14:11:44 -0800690 WPA_ASSERT(iface_hapd != nullptr && iface_hapd->iface != nullptr);
Roshan Piuse2d21012018-08-17 11:22:06 -0700691 // Register the setup complete callbacks
692 on_setup_complete_internal_callback =
693 [this](struct hostapd_data* iface_hapd) {
694 wpa_printf(
695 MSG_DEBUG, "AP interface setup completed - state %s",
696 hostapd_state_text(iface_hapd->iface->state));
697 if (iface_hapd->iface->state == HAPD_IFACE_DISABLED) {
698 // Invoke the failure callback on all registered
699 // clients.
700 for (const auto& callback : callbacks_) {
701 callback->onFailure(
702 iface_hapd->conf->iface);
703 }
704 }
705 };
lesld0621052020-08-10 13:54:28 +0800706
707 // Rgegister for new client connect/disconnect indication.
708 on_sta_authorized_internal_callback =
709 [this](struct hostapd_data* iface_hapd, const u8 *mac_addr,
710 int authorized, const u8 *p2p_dev_addr) {
711 wpa_printf(MSG_DEBUG, "notify client " MACSTR " %s",
712 MAC2STR(mac_addr),
713 (authorized) ? "Connected" : "Disconnected");
714
715 for (const auto &callback : callbacks_) {
716 // TODO: The iface need to separate to iface and ap instance
717 // identify for AP+AP case.
718 callback->onConnectedClientsChanged(iface_hapd->conf->iface,
719 iface_hapd->conf->iface, mac_addr, authorized);
720 }
721 };
722
lesldab91502020-08-17 18:29:47 +0800723 // Register for wpa_event which used to get channel switch event
724 on_wpa_msg_internal_callback =
725 [this](struct hostapd_data* iface_hapd, int level,
726 enum wpa_msg_type type, const char *txt,
727 size_t len) {
728 wpa_printf(MSG_DEBUG, "Receive wpa msg : %s", txt);
729 if (os_strncmp(txt, WPA_EVENT_CHANNEL_SWITCH,
730 strlen(WPA_EVENT_CHANNEL_SWITCH)) == 0) {
731 for (const auto &callback : callbacks_) {
732 callback->onApInstanceInfoChanged(
733 iface_hapd->conf->iface, iface_hapd->conf->iface,
734 iface_hapd->iface->freq, getBandwidth(iface_hapd->iconf),
lesl29973f82020-09-10 22:40:06 +0800735 getGeneration(iface_hapd->iface->current_mode),
736 iface_hapd->own_addr);
lesldab91502020-08-17 18:29:47 +0800737 }
738 }
739 };
lesld0621052020-08-10 13:54:28 +0800740
741 // Setup callback
Roshan Piuse2d21012018-08-17 11:22:06 -0700742 iface_hapd->setup_complete_cb = onAsyncSetupCompleteCb;
743 iface_hapd->setup_complete_cb_ctx = iface_hapd;
lesld0621052020-08-10 13:54:28 +0800744 iface_hapd->sta_authorized_cb = onAsyncStaAuthorizedCb;
745 iface_hapd->sta_authorized_cb_ctx = iface_hapd;
lesldab91502020-08-17 18:29:47 +0800746 wpa_msg_register_cb(onAsyncWpaEventCb);
lesld0621052020-08-10 13:54:28 +0800747
Roshan Pius087d8692017-12-27 14:11:44 -0800748 if (hostapd_enable_iface(iface_hapd->iface) < 0) {
749 wpa_printf(
750 MSG_ERROR, "Enabling interface %s failed",
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800751 iface_params.V1_1.V1_0.ifaceName.c_str());
leslf8b1b402020-08-04 17:09:39 +0800752 return {V1_2::HostapdStatusCode::FAILURE_UNKNOWN, ""};
Roshan Pius087d8692017-12-27 14:11:44 -0800753 }
leslf8b1b402020-08-04 17:09:39 +0800754 return {V1_2::HostapdStatusCode::SUCCESS, ""};
Roshan Piuscc817562017-12-22 14:45:05 -0800755}
756
lesl1a842e62019-12-02 19:00:37 +0800757V1_0::HostapdStatus Hostapd::removeAccessPointInternal(const std::string& iface_name)
Roshan Piuscc817562017-12-22 14:45:05 -0800758{
Roshan Pius087d8692017-12-27 14:11:44 -0800759 std::vector<char> remove_iface_param_vec(
760 iface_name.begin(), iface_name.end() + 1);
761 if (hostapd_remove_iface(interfaces_, remove_iface_param_vec.data()) <
762 0) {
763 wpa_printf(
764 MSG_ERROR, "Removing interface %s failed",
765 iface_name.c_str());
lesl1a842e62019-12-02 19:00:37 +0800766 return {V1_0::HostapdStatusCode::FAILURE_UNKNOWN, ""};
Roshan Pius087d8692017-12-27 14:11:44 -0800767 }
lesl1a842e62019-12-02 19:00:37 +0800768 return {V1_0::HostapdStatusCode::SUCCESS, ""};
Roshan Piuscc817562017-12-22 14:45:05 -0800769}
Roshan Piuse2d21012018-08-17 11:22:06 -0700770
lesl1a842e62019-12-02 19:00:37 +0800771V1_0::HostapdStatus Hostapd::registerCallbackInternal(
772 const sp<V1_1::IHostapdCallback>& callback)
Roshan Piuse2d21012018-08-17 11:22:06 -0700773{
leslf8b1b402020-08-04 17:09:39 +0800774 return {V1_0::HostapdStatusCode::FAILURE_UNKNOWN, ""};
775}
776
777V1_2::HostapdStatus Hostapd::registerCallbackInternal_1_3(
778 const sp<V1_3::IHostapdCallback>& callback)
779{
Roshan Piuse2d21012018-08-17 11:22:06 -0700780 callbacks_.push_back(callback);
leslf8b1b402020-08-04 17:09:39 +0800781 return {V1_2::HostapdStatusCode::SUCCESS, ""};
lesl1a842e62019-12-02 19:00:37 +0800782}
783
784V1_2::HostapdStatus Hostapd::forceClientDisconnectInternal(const std::string& iface_name,
785 const std::array<uint8_t, 6>& client_address, V1_2::Ieee80211ReasonCode reason_code)
786{
787 struct hostapd_data *hapd = hostapd_get_iface(interfaces_, iface_name.c_str());
788 struct sta_info *sta;
789 if (!hapd) {
790 wpa_printf(MSG_ERROR, "Interface %s doesn't exist", iface_name.c_str());
791 return {V1_2::HostapdStatusCode::FAILURE_IFACE_UNKNOWN, ""};
792 }
793 for (sta = hapd->sta_list; sta; sta = sta->next) {
794 int res;
795 res = memcmp(sta->addr, client_address.data(), ETH_ALEN);
796 if (res == 0) {
797 wpa_printf(MSG_INFO, "Force client:" MACSTR " disconnect with reason: %d",
798 MAC2STR(client_address.data()), (uint16_t) reason_code);
799 ap_sta_disconnect(hapd, sta, sta->addr, (uint16_t) reason_code);
800 return {V1_2::HostapdStatusCode::SUCCESS, ""};
801 }
802 }
803 return {V1_2::HostapdStatusCode::FAILURE_CLIENT_UNKNOWN, ""};
Roshan Piuse2d21012018-08-17 11:22:06 -0700804}
805
leslf8b1b402020-08-04 17:09:39 +0800806V1_2::HostapdStatus Hostapd::setDebugParamsInternal(V1_2::DebugLevel level)
Roger Wangcede5062019-12-30 12:56:28 +0800807{
808 wpa_debug_level = static_cast<uint32_t>(level);
809 return {V1_2::HostapdStatusCode::SUCCESS, ""};
810}
811
Roshan Piuscc817562017-12-22 14:45:05 -0800812} // namespace implementation
leslf8b1b402020-08-04 17:09:39 +0800813} // namespace V1_3
Roshan Piuscc817562017-12-22 14:45:05 -0800814} // namespace hostapd
815} // namespace wifi
816} // namespace hardware
817} // namespace android