blob: 8d85543ca2d6e2923a9ab285c34c9b7f9c638f3d [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;
lesl1a842e62019-12-02 19:00:37 +080037using android::hardware::wifi::hostapd::V1_2::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 }
202 // Error
203 return 0;
204 }
205
206 return 0;
207}
208
lesldd48efd2019-12-26 15:13:51 +0800209bool validatePassphrase(int passphrase_len, int min_len, int max_len)
210{
211 if (min_len != -1 && passphrase_len < min_len) return false;
212 if (max_len != -1 && passphrase_len > max_len) return false;
213 return true;
214}
215
Roshan Pius30b452e2017-12-27 13:36:21 -0800216std::string CreateHostapdConfig(
217 const IHostapd::IfaceParams& iface_params,
218 const IHostapd::NetworkParams& nw_params)
219{
lesldd48efd2019-12-26 15:13:51 +0800220 if (nw_params.V1_0.ssid.size() >
Roshan Pius30b452e2017-12-27 13:36:21 -0800221 static_cast<uint32_t>(
222 IHostapd::ParamSizeLimits::SSID_MAX_LEN_IN_BYTES)) {
223 wpa_printf(
lesldd48efd2019-12-26 15:13:51 +0800224 MSG_ERROR, "Invalid SSID size: %zu", nw_params.V1_0.ssid.size());
Roshan Pius30b452e2017-12-27 13:36:21 -0800225 return "";
226 }
227
228 // SSID string
229 std::stringstream ss;
230 ss << std::hex;
231 ss << std::setfill('0');
lesldd48efd2019-12-26 15:13:51 +0800232 for (uint8_t b : nw_params.V1_0.ssid) {
Roshan Pius30b452e2017-12-27 13:36:21 -0800233 ss << std::setw(2) << static_cast<unsigned int>(b);
234 }
235 const std::string ssid_as_string = ss.str();
236
237 // Encryption config string
238 std::string encryption_config_as_string;
239 switch (nw_params.encryptionType) {
240 case IHostapd::EncryptionType::NONE:
241 // no security params
242 break;
243 case IHostapd::EncryptionType::WPA:
lesldd48efd2019-12-26 15:13:51 +0800244 if (!validatePassphrase(
245 nw_params.passphrase.size(),
246 static_cast<uint32_t>(IHostapd::ParamSizeLimits::
247 WPA2_PSK_PASSPHRASE_MIN_LEN_IN_BYTES),
248 static_cast<uint32_t>(IHostapd::ParamSizeLimits::
249 WPA2_PSK_PASSPHRASE_MAX_LEN_IN_BYTES))) {
250 return "";
251 }
Roshan Pius30b452e2017-12-27 13:36:21 -0800252 encryption_config_as_string = StringPrintf(
253 "wpa=3\n"
254 "wpa_pairwise=TKIP CCMP\n"
255 "wpa_passphrase=%s",
lesldd48efd2019-12-26 15:13:51 +0800256 nw_params.passphrase.c_str());
Roshan Pius30b452e2017-12-27 13:36:21 -0800257 break;
258 case IHostapd::EncryptionType::WPA2:
lesldd48efd2019-12-26 15:13:51 +0800259 if (!validatePassphrase(
260 nw_params.passphrase.size(),
261 static_cast<uint32_t>(IHostapd::ParamSizeLimits::
262 WPA2_PSK_PASSPHRASE_MIN_LEN_IN_BYTES),
263 static_cast<uint32_t>(IHostapd::ParamSizeLimits::
264 WPA2_PSK_PASSPHRASE_MAX_LEN_IN_BYTES))) {
265 return "";
266 }
Roshan Pius30b452e2017-12-27 13:36:21 -0800267 encryption_config_as_string = StringPrintf(
268 "wpa=2\n"
269 "rsn_pairwise=CCMP\n"
270 "wpa_passphrase=%s",
lesldd48efd2019-12-26 15:13:51 +0800271 nw_params.passphrase.c_str());
272 break;
273 case IHostapd::EncryptionType::WPA3_SAE_TRANSITION:
274 if (!validatePassphrase(
275 nw_params.passphrase.size(),
276 static_cast<uint32_t>(IHostapd::ParamSizeLimits::
277 WPA2_PSK_PASSPHRASE_MIN_LEN_IN_BYTES),
278 static_cast<uint32_t>(IHostapd::ParamSizeLimits::
279 WPA2_PSK_PASSPHRASE_MAX_LEN_IN_BYTES))) {
280 return "";
281 }
282 encryption_config_as_string = StringPrintf(
283 "wpa=2\n"
284 "rsn_pairwise=CCMP\n"
285 "wpa_key_mgmt=WPA-PSK SAE\n"
286 "ieee80211w=1\n"
287 "sae_require_mfp=1\n"
288 "wpa_passphrase=%s\n"
289 "sae_password=%s",
290 nw_params.passphrase.c_str(),
291 nw_params.passphrase.c_str());
292 break;
293 case IHostapd::EncryptionType::WPA3_SAE:
294 if (!validatePassphrase(nw_params.passphrase.size(), 1, -1)) {
295 return "";
296 }
297 encryption_config_as_string = StringPrintf(
298 "wpa=2\n"
299 "rsn_pairwise=CCMP\n"
300 "wpa_key_mgmt=SAE\n"
301 "ieee80211w=2\n"
302 "sae_require_mfp=2\n"
303 "sae_password=%s",
304 nw_params.passphrase.c_str());
Roshan Pius30b452e2017-12-27 13:36:21 -0800305 break;
306 default:
307 wpa_printf(MSG_ERROR, "Unknown encryption type");
308 return "";
309 }
310
Ahmed ElArabawy7b4b40a2020-01-02 08:55:38 -0800311 unsigned int band = 0;
312 band |= iface_params.channelParams.bandMask;
313
Roshan Pius30b452e2017-12-27 13:36:21 -0800314 std::string channel_config_as_string;
Ahmed ElArabawya390f292019-12-21 13:07:02 -0800315 bool isFirst = true;
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800316 if (iface_params.V1_1.V1_0.channelParams.enableAcs) {
Ahmed ElArabawya390f292019-12-21 13:07:02 -0800317 std::string freqList_as_string;
Daisuke Niwac1bd20f2019-01-09 13:51:02 +0900318 for (const auto &range :
Ahmed ElArabawya390f292019-12-21 13:07:02 -0800319 iface_params.channelParams.acsChannelFreqRangesMhz) {
320 if (!isFirst) {
321 freqList_as_string += ",";
322 }
323 isFirst = false;
324
Daisuke Niwac1bd20f2019-01-09 13:51:02 +0900325 if (range.start != range.end) {
Ahmed ElArabawya390f292019-12-21 13:07:02 -0800326 freqList_as_string +=
327 StringPrintf("%d-%d", range.start, range.end);
Daisuke Niwac1bd20f2019-01-09 13:51:02 +0900328 } else {
Ahmed ElArabawya390f292019-12-21 13:07:02 -0800329 freqList_as_string += StringPrintf("%d", range.start);
Daisuke Niwac1bd20f2019-01-09 13:51:02 +0900330 }
331 }
Roshan Pius30b452e2017-12-27 13:36:21 -0800332 channel_config_as_string = StringPrintf(
333 "channel=0\n"
Daisuke Niwac1bd20f2019-01-09 13:51:02 +0900334 "acs_exclude_dfs=%d\n"
Ahmed ElArabawya390f292019-12-21 13:07:02 -0800335 "freqlist=%s",
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800336 iface_params.V1_1.V1_0.channelParams.acsShouldExcludeDfs,
Ahmed ElArabawya390f292019-12-21 13:07:02 -0800337 freqList_as_string.c_str());
Roshan Pius30b452e2017-12-27 13:36:21 -0800338 } else {
Ahmed ElArabawy7b4b40a2020-01-02 08:55:38 -0800339 int op_class = getOpClassForChannel(
340 iface_params.V1_1.V1_0.channelParams.channel,
341 band,
342 iface_params.V1_1.V1_0.hwModeParams.enable80211N,
343 iface_params.V1_1.V1_0.hwModeParams.enable80211AC);
Roshan Pius30b452e2017-12-27 13:36:21 -0800344 channel_config_as_string = StringPrintf(
Ahmed ElArabawy7b4b40a2020-01-02 08:55:38 -0800345 "channel=%d\n"
346 "op_class=%d",
347 iface_params.V1_1.V1_0.channelParams.channel, op_class);
Roshan Pius30b452e2017-12-27 13:36:21 -0800348 }
349
Roshan Pius30b452e2017-12-27 13:36:21 -0800350 std::string hw_mode_as_string;
Roshan Pius49446472018-03-07 10:23:46 -0800351 std::string ht_cap_vht_oper_chwidth_as_string;
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800352
353 if ((band & IHostapd::BandMask::BAND_2_GHZ) != 0) {
Ahmed ElArabawya390f292019-12-21 13:07:02 -0800354 if (((band & IHostapd::BandMask::BAND_5_GHZ) != 0)
355 || ((band & IHostapd::BandMask::BAND_6_GHZ) != 0)) {
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800356 hw_mode_as_string = "hw_mode=any";
357 if (iface_params.V1_1.V1_0.channelParams.enableAcs) {
358 ht_cap_vht_oper_chwidth_as_string =
359 "ht_capab=[HT40+]\n"
360 "vht_oper_chwidth=1";
361 }
362 } else {
363 hw_mode_as_string = "hw_mode=g";
Roshan Pius49446472018-03-07 10:23:46 -0800364 }
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800365 } else {
Ahmed ElArabawya390f292019-12-21 13:07:02 -0800366 if (((band & IHostapd::BandMask::BAND_5_GHZ) != 0)
367 || ((band & IHostapd::BandMask::BAND_6_GHZ) != 0)) {
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800368 hw_mode_as_string = "hw_mode=a";
369 if (iface_params.V1_1.V1_0.channelParams.enableAcs) {
370 ht_cap_vht_oper_chwidth_as_string =
371 "ht_capab=[HT40+]\n"
372 "vht_oper_chwidth=1";
373 }
374 } else {
375 wpa_printf(MSG_ERROR, "Invalid band");
376 return "";
Roshan Pius49446472018-03-07 10:23:46 -0800377 }
Roshan Pius30b452e2017-12-27 13:36:21 -0800378 }
379
Ahmed ElArabawy5362fb12020-01-02 15:01:31 -0800380 std::string he_params_as_string;
Roshan Pius36510302020-05-09 21:01:01 -0700381#ifdef CONFIG_IEEE80211AX
Ahmed ElArabawy5362fb12020-01-02 15:01:31 -0800382 if (iface_params.hwModeParams.enable80211AX) {
383 he_params_as_string = StringPrintf(
384 "ieee80211ax=1\n"
385 "he_su_beamformer=%d\n"
386 "he_su_beamformee=%d\n"
387 "he_mu_beamformer=%d\n"
Ahmed ElArabawy5362fb12020-01-02 15:01:31 -0800388 "he_twt_required=%d\n",
389 iface_params.hwModeParams.enableHeSingleUserBeamformer ? 1 : 0,
390 iface_params.hwModeParams.enableHeSingleUserBeamformee ? 1 : 0,
391 iface_params.hwModeParams.enableHeMultiUserBeamformer ? 1 : 0,
Ahmed ElArabawy5362fb12020-01-02 15:01:31 -0800392 iface_params.hwModeParams.enableHeTargetWakeTime ? 1 : 0);
393 } else {
394 he_params_as_string = "ieee80211ax=0";
395 }
Roshan Pius36510302020-05-09 21:01:01 -0700396#endif /* CONFIG_IEEE80211AX */
Ahmed ElArabawy5362fb12020-01-02 15:01:31 -0800397
Roshan Pius30b452e2017-12-27 13:36:21 -0800398 return StringPrintf(
399 "interface=%s\n"
400 "driver=nl80211\n"
Daichi Ueuracaa74a82018-02-14 22:25:39 +0900401 "ctrl_interface=/data/vendor/wifi/hostapd/ctrl\n"
Roshan Pius30b452e2017-12-27 13:36:21 -0800402 // ssid2 signals to hostapd that the value is not a literal value
403 // for use as a SSID. In this case, we're giving it a hex
404 // std::string and hostapd needs to expect that.
405 "ssid2=%s\n"
406 "%s\n"
407 "ieee80211n=%d\n"
408 "ieee80211ac=%d\n"
Ahmed ElArabawy5362fb12020-01-02 15:01:31 -0800409 "%s\n"
Roshan Piuse453f712018-02-09 15:49:57 -0800410 "%s\n"
Roshan Pius49446472018-03-07 10:23:46 -0800411 "%s\n"
Roshan Pius30b452e2017-12-27 13:36:21 -0800412 "ignore_broadcast_ssid=%d\n"
413 "wowlan_triggers=any\n"
414 "%s\n",
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800415 iface_params.V1_1.V1_0.ifaceName.c_str(), ssid_as_string.c_str(),
Roshan Pius30b452e2017-12-27 13:36:21 -0800416 channel_config_as_string.c_str(),
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800417 iface_params.V1_1.V1_0.hwModeParams.enable80211N ? 1 : 0,
418 iface_params.V1_1.V1_0.hwModeParams.enable80211AC ? 1 : 0,
Ahmed ElArabawy5362fb12020-01-02 15:01:31 -0800419 he_params_as_string.c_str(),
Roshan Pius49446472018-03-07 10:23:46 -0800420 hw_mode_as_string.c_str(), ht_cap_vht_oper_chwidth_as_string.c_str(),
lesldd48efd2019-12-26 15:13:51 +0800421 nw_params.V1_0.isHidden ? 1 : 0, encryption_config_as_string.c_str());
Roshan Pius30b452e2017-12-27 13:36:21 -0800422}
Roshan Piuse2d21012018-08-17 11:22:06 -0700423
lesldab91502020-08-17 18:29:47 +0800424Generation getGeneration(hostapd_hw_modes *current_mode)
425{
426 wpa_printf(MSG_DEBUG, "getGeneration hwmode=%d, ht_enabled=%d, vht_enabled=%d",
427 current_mode->mode, current_mode->ht_capab != 0,
428 current_mode->vht_capab != 0);
429 switch (current_mode->mode) {
430 case HOSTAPD_MODE_IEEE80211B:
431 return Generation::WIFI_STANDARD_LEGACY;
432 case HOSTAPD_MODE_IEEE80211G:
433 return current_mode->ht_capab == 0 ?
434 Generation::WIFI_STANDARD_LEGACY : Generation::WIFI_STANDARD_11N;
435 case HOSTAPD_MODE_IEEE80211A:
436 return current_mode->vht_capab == 0 ?
437 Generation::WIFI_STANDARD_11N : Generation::WIFI_STANDARD_11AC;
438 // TODO: b/162484222 miss HOSTAPD_MODE_IEEE80211AX definition.
439 default:
440 return Generation::WIFI_STANDARD_UNKNOWN;
441 }
442}
443
444Bandwidth getBandwidth(struct hostapd_config *iconf)
445{
446 wpa_printf(MSG_DEBUG, "getBandwidth %d, isHT=%d, isHT40=%d",
447 iconf->vht_oper_chwidth, iconf->ieee80211n,
448 iconf->secondary_channel);
449 switch (iconf->vht_oper_chwidth) {
450 case CHANWIDTH_80MHZ:
451 return Bandwidth::WIFI_BANDWIDTH_80;
452 case CHANWIDTH_80P80MHZ:
453 return Bandwidth::WIFI_BANDWIDTH_80P80;
454 break;
455 case CHANWIDTH_160MHZ:
456 return Bandwidth::WIFI_BANDWIDTH_160;
457 break;
458 case CHANWIDTH_USE_HT:
459 if (iconf->ieee80211n) {
460 return iconf->secondary_channel != 0 ?
461 Bandwidth::WIFI_BANDWIDTH_40 : Bandwidth::WIFI_BANDWIDTH_20;
462 }
463 return Bandwidth::WIFI_BANDWIDTH_20_NOHT;
464 default:
465 return Bandwidth::WIFI_BANDWIDTH_INVALID;
466 }
467}
468
Roshan Piuse2d21012018-08-17 11:22:06 -0700469// hostapd core functions accept "C" style function pointers, so use global
470// functions to pass to the hostapd core function and store the corresponding
471// std::function methods to be invoked.
472//
473// NOTE: Using the pattern from the vendor HAL (wifi_legacy_hal.cpp).
474//
475// Callback to be invoked once setup is complete
476std::function<void(struct hostapd_data*)> on_setup_complete_internal_callback;
477void onAsyncSetupCompleteCb(void* ctx)
478{
479 struct hostapd_data* iface_hapd = (struct hostapd_data*)ctx;
480 if (on_setup_complete_internal_callback) {
481 on_setup_complete_internal_callback(iface_hapd);
482 // Invalidate this callback since we don't want this firing
483 // again.
484 on_setup_complete_internal_callback = nullptr;
485 }
486}
lesld0621052020-08-10 13:54:28 +0800487
488// Callback to be invoked on hotspot client connection/disconnection
489std::function<void(struct hostapd_data*, const u8 *mac_addr, int authorized,
490 const u8 *p2p_dev_addr)> on_sta_authorized_internal_callback;
491void onAsyncStaAuthorizedCb(void* ctx, const u8 *mac_addr, int authorized,
492 const u8 *p2p_dev_addr)
493{
494 struct hostapd_data* iface_hapd = (struct hostapd_data*)ctx;
495 if (on_sta_authorized_internal_callback) {
496 on_sta_authorized_internal_callback(iface_hapd, mac_addr,
497 authorized, p2p_dev_addr);
498 }
499}
500
lesldab91502020-08-17 18:29:47 +0800501std::function<void(struct hostapd_data*, int level,
502 enum wpa_msg_type type, const char *txt,
503 size_t len)> on_wpa_msg_internal_callback;
504
505void onAsyncWpaEventCb(void *ctx, int level,
506 enum wpa_msg_type type, const char *txt,
507 size_t len)
508{
509 struct hostapd_data* iface_hapd = (struct hostapd_data*)ctx;
510 if (on_wpa_msg_internal_callback) {
511 on_wpa_msg_internal_callback(iface_hapd, level,
512 type, txt, len);
513 }
514}
515
516
Roshan Pius30b452e2017-12-27 13:36:21 -0800517} // namespace
518
Roshan Piuscc817562017-12-22 14:45:05 -0800519namespace android {
520namespace hardware {
521namespace wifi {
522namespace hostapd {
leslf8b1b402020-08-04 17:09:39 +0800523namespace V1_3 {
Roshan Piuscc817562017-12-22 14:45:05 -0800524namespace implementation {
525using hidl_return_util::call;
Roshan Piuse2d21012018-08-17 11:22:06 -0700526using namespace android::hardware::wifi::hostapd::V1_0;
Roshan Piuscc817562017-12-22 14:45:05 -0800527
528Hostapd::Hostapd(struct hapd_interfaces* interfaces) : interfaces_(interfaces)
529{}
530
531Return<void> Hostapd::addAccessPoint(
Daisuke Niwac1bd20f2019-01-09 13:51:02 +0900532 const V1_0::IHostapd::IfaceParams& iface_params,
lesldd48efd2019-12-26 15:13:51 +0800533 const V1_0::IHostapd::NetworkParams& nw_params, addAccessPoint_cb _hidl_cb)
Daisuke Niwac1bd20f2019-01-09 13:51:02 +0900534{
535 return call(
536 this, &Hostapd::addAccessPointInternal, _hidl_cb, iface_params,
537 nw_params);
538}
539
540Return<void> Hostapd::addAccessPoint_1_1(
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800541 const V1_1::IHostapd::IfaceParams& iface_params,
lesldd48efd2019-12-26 15:13:51 +0800542 const V1_0::IHostapd::NetworkParams& nw_params, addAccessPoint_cb _hidl_cb)
Roshan Piuscc817562017-12-22 14:45:05 -0800543{
544 return call(
Daisuke Niwac1bd20f2019-01-09 13:51:02 +0900545 this, &Hostapd::addAccessPointInternal_1_1, _hidl_cb, iface_params,
Roshan Piuscc817562017-12-22 14:45:05 -0800546 nw_params);
547}
548
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800549Return<void> Hostapd::addAccessPoint_1_2(
550 const IfaceParams& iface_params, const NetworkParams& nw_params,
551 addAccessPoint_1_2_cb _hidl_cb)
552{
553 return call(
554 this, &Hostapd::addAccessPointInternal_1_2, _hidl_cb, iface_params,
555 nw_params);
556}
557
Roshan Piuscc817562017-12-22 14:45:05 -0800558Return<void> Hostapd::removeAccessPoint(
559 const hidl_string& iface_name, removeAccessPoint_cb _hidl_cb)
560{
561 return call(
562 this, &Hostapd::removeAccessPointInternal, _hidl_cb, iface_name);
563}
564
Roshan Piuse2d21012018-08-17 11:22:06 -0700565Return<void> Hostapd::terminate()
566{
Roshan Pius3455af42018-02-01 09:19:49 -0800567 wpa_printf(MSG_INFO, "Terminating...");
568 eloop_terminate();
569 return Void();
570}
571
Roshan Piuse2d21012018-08-17 11:22:06 -0700572Return<void> Hostapd::registerCallback(
lesl1a842e62019-12-02 19:00:37 +0800573 const sp<V1_1::IHostapdCallback>& callback, registerCallback_cb _hidl_cb)
Roshan Piuse2d21012018-08-17 11:22:06 -0700574{
575 return call(
576 this, &Hostapd::registerCallbackInternal, _hidl_cb, callback);
577}
578
leslf8b1b402020-08-04 17:09:39 +0800579Return<void> Hostapd::registerCallback_1_3(
580 const sp<V1_3::IHostapdCallback>& callback, registerCallback_1_3_cb _hidl_cb)
581{
582 return call(
583 this, &Hostapd::registerCallbackInternal_1_3, _hidl_cb, callback);
584}
585
lesl1a842e62019-12-02 19:00:37 +0800586Return<void> Hostapd::forceClientDisconnect(
587 const hidl_string& iface_name, const hidl_array<uint8_t, 6>& client_address,
588 V1_2::Ieee80211ReasonCode reason_code, forceClientDisconnect_cb _hidl_cb)
589{
590 return call(
591 this, &Hostapd::forceClientDisconnectInternal, _hidl_cb, iface_name,
592 client_address, reason_code);
593}
594
Roger Wangcede5062019-12-30 12:56:28 +0800595Return<void> Hostapd::setDebugParams(
leslf8b1b402020-08-04 17:09:39 +0800596 V1_2::DebugLevel level, setDebugParams_cb _hidl_cb)
Roger Wangcede5062019-12-30 12:56:28 +0800597{
598 return call(
599 this, &Hostapd::setDebugParamsInternal, _hidl_cb, level);
600}
lesl1a842e62019-12-02 19:00:37 +0800601
602V1_0::HostapdStatus Hostapd::addAccessPointInternal(
Daisuke Niwac1bd20f2019-01-09 13:51:02 +0900603 const V1_0::IHostapd::IfaceParams& iface_params,
lesldd48efd2019-12-26 15:13:51 +0800604 const V1_0::IHostapd::NetworkParams& nw_params)
Daisuke Niwac1bd20f2019-01-09 13:51:02 +0900605{
lesl1a842e62019-12-02 19:00:37 +0800606 return {V1_0::HostapdStatusCode::FAILURE_UNKNOWN, ""};
Daisuke Niwac1bd20f2019-01-09 13:51:02 +0900607}
608
lesl1a842e62019-12-02 19:00:37 +0800609V1_0::HostapdStatus Hostapd::addAccessPointInternal_1_1(
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800610 const V1_1::IHostapd::IfaceParams& iface_params,
lesldd48efd2019-12-26 15:13:51 +0800611 const V1_1::IHostapd::NetworkParams& nw_params)
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800612{
613 return {V1_0::HostapdStatusCode::FAILURE_UNKNOWN, ""};
614}
615
leslf8b1b402020-08-04 17:09:39 +0800616V1_2::HostapdStatus Hostapd::addAccessPointInternal_1_2(
Roshan Piuscc817562017-12-22 14:45:05 -0800617 const IfaceParams& iface_params, const NetworkParams& nw_params)
618{
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800619 if (hostapd_get_iface(interfaces_, iface_params.V1_1.V1_0.ifaceName.c_str())) {
Roshan Pius087d8692017-12-27 14:11:44 -0800620 wpa_printf(
621 MSG_ERROR, "Interface %s already present",
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800622 iface_params.V1_1.V1_0.ifaceName.c_str());
leslf8b1b402020-08-04 17:09:39 +0800623 return {V1_2::HostapdStatusCode::FAILURE_IFACE_EXISTS, ""};
Roshan Pius087d8692017-12-27 14:11:44 -0800624 }
625 const auto conf_params = CreateHostapdConfig(iface_params, nw_params);
626 if (conf_params.empty()) {
627 wpa_printf(MSG_ERROR, "Failed to create config params");
leslf8b1b402020-08-04 17:09:39 +0800628 return {V1_2::HostapdStatusCode::FAILURE_ARGS_INVALID, ""};
Roshan Pius087d8692017-12-27 14:11:44 -0800629 }
630 const auto conf_file_path =
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800631 WriteHostapdConfig(iface_params.V1_1.V1_0.ifaceName, conf_params);
Roshan Pius087d8692017-12-27 14:11:44 -0800632 if (conf_file_path.empty()) {
633 wpa_printf(MSG_ERROR, "Failed to write config file");
leslf8b1b402020-08-04 17:09:39 +0800634 return {V1_2::HostapdStatusCode::FAILURE_UNKNOWN, ""};
Roshan Pius087d8692017-12-27 14:11:44 -0800635 }
636 std::string add_iface_param_str = StringPrintf(
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800637 "%s config=%s", iface_params.V1_1.V1_0.ifaceName.c_str(),
Roshan Pius087d8692017-12-27 14:11:44 -0800638 conf_file_path.c_str());
639 std::vector<char> add_iface_param_vec(
640 add_iface_param_str.begin(), add_iface_param_str.end() + 1);
641 if (hostapd_add_iface(interfaces_, add_iface_param_vec.data()) < 0) {
642 wpa_printf(
643 MSG_ERROR, "Adding interface %s failed",
644 add_iface_param_str.c_str());
leslf8b1b402020-08-04 17:09:39 +0800645 return {V1_2::HostapdStatusCode::FAILURE_UNKNOWN, ""};
Roshan Pius087d8692017-12-27 14:11:44 -0800646 }
647 struct hostapd_data* iface_hapd =
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800648 hostapd_get_iface(interfaces_, iface_params.V1_1.V1_0.ifaceName.c_str());
Roshan Pius087d8692017-12-27 14:11:44 -0800649 WPA_ASSERT(iface_hapd != nullptr && iface_hapd->iface != nullptr);
Roshan Piuse2d21012018-08-17 11:22:06 -0700650 // Register the setup complete callbacks
651 on_setup_complete_internal_callback =
652 [this](struct hostapd_data* iface_hapd) {
653 wpa_printf(
654 MSG_DEBUG, "AP interface setup completed - state %s",
655 hostapd_state_text(iface_hapd->iface->state));
656 if (iface_hapd->iface->state == HAPD_IFACE_DISABLED) {
657 // Invoke the failure callback on all registered
658 // clients.
659 for (const auto& callback : callbacks_) {
660 callback->onFailure(
661 iface_hapd->conf->iface);
662 }
663 }
664 };
lesld0621052020-08-10 13:54:28 +0800665
666 // Rgegister for new client connect/disconnect indication.
667 on_sta_authorized_internal_callback =
668 [this](struct hostapd_data* iface_hapd, const u8 *mac_addr,
669 int authorized, const u8 *p2p_dev_addr) {
670 wpa_printf(MSG_DEBUG, "notify client " MACSTR " %s",
671 MAC2STR(mac_addr),
672 (authorized) ? "Connected" : "Disconnected");
673
674 for (const auto &callback : callbacks_) {
675 // TODO: The iface need to separate to iface and ap instance
676 // identify for AP+AP case.
677 callback->onConnectedClientsChanged(iface_hapd->conf->iface,
678 iface_hapd->conf->iface, mac_addr, authorized);
679 }
680 };
681
lesldab91502020-08-17 18:29:47 +0800682 // Register for wpa_event which used to get channel switch event
683 on_wpa_msg_internal_callback =
684 [this](struct hostapd_data* iface_hapd, int level,
685 enum wpa_msg_type type, const char *txt,
686 size_t len) {
687 wpa_printf(MSG_DEBUG, "Receive wpa msg : %s", txt);
688 if (os_strncmp(txt, WPA_EVENT_CHANNEL_SWITCH,
689 strlen(WPA_EVENT_CHANNEL_SWITCH)) == 0) {
690 for (const auto &callback : callbacks_) {
691 callback->onApInstanceInfoChanged(
692 iface_hapd->conf->iface, iface_hapd->conf->iface,
693 iface_hapd->iface->freq, getBandwidth(iface_hapd->iconf),
lesl29973f82020-09-10 22:40:06 +0800694 getGeneration(iface_hapd->iface->current_mode),
695 iface_hapd->own_addr);
lesldab91502020-08-17 18:29:47 +0800696 }
697 }
698 };
lesld0621052020-08-10 13:54:28 +0800699
700 // Setup callback
Roshan Piuse2d21012018-08-17 11:22:06 -0700701 iface_hapd->setup_complete_cb = onAsyncSetupCompleteCb;
702 iface_hapd->setup_complete_cb_ctx = iface_hapd;
lesld0621052020-08-10 13:54:28 +0800703 iface_hapd->sta_authorized_cb = onAsyncStaAuthorizedCb;
704 iface_hapd->sta_authorized_cb_ctx = iface_hapd;
lesldab91502020-08-17 18:29:47 +0800705 wpa_msg_register_cb(onAsyncWpaEventCb);
lesld0621052020-08-10 13:54:28 +0800706
Roshan Pius087d8692017-12-27 14:11:44 -0800707 if (hostapd_enable_iface(iface_hapd->iface) < 0) {
708 wpa_printf(
709 MSG_ERROR, "Enabling interface %s failed",
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800710 iface_params.V1_1.V1_0.ifaceName.c_str());
leslf8b1b402020-08-04 17:09:39 +0800711 return {V1_2::HostapdStatusCode::FAILURE_UNKNOWN, ""};
Roshan Pius087d8692017-12-27 14:11:44 -0800712 }
leslf8b1b402020-08-04 17:09:39 +0800713 return {V1_2::HostapdStatusCode::SUCCESS, ""};
Roshan Piuscc817562017-12-22 14:45:05 -0800714}
715
lesl1a842e62019-12-02 19:00:37 +0800716V1_0::HostapdStatus Hostapd::removeAccessPointInternal(const std::string& iface_name)
Roshan Piuscc817562017-12-22 14:45:05 -0800717{
Roshan Pius087d8692017-12-27 14:11:44 -0800718 std::vector<char> remove_iface_param_vec(
719 iface_name.begin(), iface_name.end() + 1);
720 if (hostapd_remove_iface(interfaces_, remove_iface_param_vec.data()) <
721 0) {
722 wpa_printf(
723 MSG_ERROR, "Removing interface %s failed",
724 iface_name.c_str());
lesl1a842e62019-12-02 19:00:37 +0800725 return {V1_0::HostapdStatusCode::FAILURE_UNKNOWN, ""};
Roshan Pius087d8692017-12-27 14:11:44 -0800726 }
lesl1a842e62019-12-02 19:00:37 +0800727 return {V1_0::HostapdStatusCode::SUCCESS, ""};
Roshan Piuscc817562017-12-22 14:45:05 -0800728}
Roshan Piuse2d21012018-08-17 11:22:06 -0700729
lesl1a842e62019-12-02 19:00:37 +0800730V1_0::HostapdStatus Hostapd::registerCallbackInternal(
731 const sp<V1_1::IHostapdCallback>& callback)
Roshan Piuse2d21012018-08-17 11:22:06 -0700732{
leslf8b1b402020-08-04 17:09:39 +0800733 return {V1_0::HostapdStatusCode::FAILURE_UNKNOWN, ""};
734}
735
736V1_2::HostapdStatus Hostapd::registerCallbackInternal_1_3(
737 const sp<V1_3::IHostapdCallback>& callback)
738{
Roshan Piuse2d21012018-08-17 11:22:06 -0700739 callbacks_.push_back(callback);
leslf8b1b402020-08-04 17:09:39 +0800740 return {V1_2::HostapdStatusCode::SUCCESS, ""};
lesl1a842e62019-12-02 19:00:37 +0800741}
742
743V1_2::HostapdStatus Hostapd::forceClientDisconnectInternal(const std::string& iface_name,
744 const std::array<uint8_t, 6>& client_address, V1_2::Ieee80211ReasonCode reason_code)
745{
746 struct hostapd_data *hapd = hostapd_get_iface(interfaces_, iface_name.c_str());
747 struct sta_info *sta;
748 if (!hapd) {
749 wpa_printf(MSG_ERROR, "Interface %s doesn't exist", iface_name.c_str());
750 return {V1_2::HostapdStatusCode::FAILURE_IFACE_UNKNOWN, ""};
751 }
752 for (sta = hapd->sta_list; sta; sta = sta->next) {
753 int res;
754 res = memcmp(sta->addr, client_address.data(), ETH_ALEN);
755 if (res == 0) {
756 wpa_printf(MSG_INFO, "Force client:" MACSTR " disconnect with reason: %d",
757 MAC2STR(client_address.data()), (uint16_t) reason_code);
758 ap_sta_disconnect(hapd, sta, sta->addr, (uint16_t) reason_code);
759 return {V1_2::HostapdStatusCode::SUCCESS, ""};
760 }
761 }
762 return {V1_2::HostapdStatusCode::FAILURE_CLIENT_UNKNOWN, ""};
Roshan Piuse2d21012018-08-17 11:22:06 -0700763}
764
leslf8b1b402020-08-04 17:09:39 +0800765V1_2::HostapdStatus Hostapd::setDebugParamsInternal(V1_2::DebugLevel level)
Roger Wangcede5062019-12-30 12:56:28 +0800766{
767 wpa_debug_level = static_cast<uint32_t>(level);
768 return {V1_2::HostapdStatusCode::SUCCESS, ""};
769}
770
Roshan Piuscc817562017-12-22 14:45:05 -0800771} // namespace implementation
leslf8b1b402020-08-04 17:09:39 +0800772} // namespace V1_3
Roshan Piuscc817562017-12-22 14:45:05 -0800773} // namespace hostapd
774} // namespace wifi
775} // namespace hardware
776} // namespace android