blob: b40087ac28db323d57e14d38700861b77144dc70 [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{
22#include "utils/eloop.h"
23}
24
Roshan Pius30b452e2017-12-27 13:36:21 -080025// The HIDL implementation for hostapd creates a hostapd.conf dynamically for
26// each interface. This file can then be used to hook onto the normal config
27// file parsing logic in hostapd code. Helps us to avoid duplication of code
28// in the HIDL interface.
29// TOOD(b/71872409): Add unit tests for this.
30namespace {
31constexpr char kConfFileNameFmt[] = "/data/vendor/wifi/hostapd/hostapd_%s.conf";
32
33using android::base::RemoveFileIfExists;
34using android::base::StringPrintf;
35using android::base::WriteStringToFile;
lesl1a842e62019-12-02 19:00:37 +080036using android::hardware::wifi::hostapd::V1_2::IHostapd;
Roshan Pius30b452e2017-12-27 13:36:21 -080037
38std::string WriteHostapdConfig(
39 const std::string& interface_name, const std::string& config)
40{
41 const std::string file_path =
42 StringPrintf(kConfFileNameFmt, interface_name.c_str());
43 if (WriteStringToFile(
44 config, file_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
45 getuid(), getgid())) {
46 return file_path;
47 }
48 // Diagnose failure
49 int error = errno;
50 wpa_printf(
51 MSG_ERROR, "Cannot write hostapd config to %s, error: %s",
52 file_path.c_str(), strerror(error));
53 struct stat st;
54 int result = stat(file_path.c_str(), &st);
55 if (result == 0) {
56 wpa_printf(
57 MSG_ERROR, "hostapd config file uid: %d, gid: %d, mode: %d",
58 st.st_uid, st.st_gid, st.st_mode);
59 } else {
60 wpa_printf(
61 MSG_ERROR,
62 "Error calling stat() on hostapd config file: %s",
63 strerror(errno));
64 }
65 return "";
66}
67
Ahmed ElArabawy7b4b40a2020-01-02 08:55:38 -080068/*
69 * Get the op_class for a channel/band
70 * The logic here is based on Table E-4 in the 802.11 Specification
71 */
72int getOpClassForChannel(int channel, int band, bool support11n, bool support11ac) {
73 // 2GHz Band
74 if ((band & IHostapd::BandMask::BAND_2_GHZ) != 0) {
75 if (channel == 14) {
76 return 82;
77 }
78 if (channel >= 1 && channel <= 13) {
79 if (!support11n) {
80 //20MHz channel
81 return 81;
82 }
83 if (channel <= 9) {
84 // HT40 with secondary channel above primary
85 return 83;
86 }
87 // HT40 with secondary channel below primary
88 return 84;
89 }
90 // Error
91 return 0;
92 }
93
94 // 5GHz Band
95 if ((band & IHostapd::BandMask::BAND_5_GHZ) != 0) {
96 if (support11ac) {
97 switch (channel) {
98 case 42:
99 case 58:
100 case 106:
101 case 122:
102 case 138:
103 case 155:
104 // 80MHz channel
105 return 128;
106 case 50:
107 case 114:
108 // 160MHz channel
109 return 129;
110 }
111 }
112
113 if (!support11n) {
114 if (channel >= 36 && channel <= 48) {
115 return 115;
116 }
117 if (channel >= 52 && channel <= 64) {
118 return 118;
119 }
120 if (channel >= 100 && channel <= 144) {
121 return 121;
122 }
123 if (channel >= 149 && channel <= 161) {
124 return 124;
125 }
126 if (channel >= 165 && channel <= 169) {
127 return 125;
128 }
129 } else {
130 switch (channel) {
131 case 36:
132 case 44:
133 // HT40 with secondary channel above primary
134 return 116;
135 case 40:
136 case 48:
137 // HT40 with secondary channel below primary
138 return 117;
139 case 52:
140 case 60:
141 // HT40 with secondary channel above primary
142 return 119;
143 case 56:
144 case 64:
145 // HT40 with secondary channel below primary
146 return 120;
147 case 100:
148 case 108:
149 case 116:
150 case 124:
151 case 132:
152 case 140:
153 // HT40 with secondary channel above primary
154 return 122;
155 case 104:
156 case 112:
157 case 120:
158 case 128:
159 case 136:
160 case 144:
161 // HT40 with secondary channel below primary
162 return 123;
163 case 149:
164 case 157:
165 // HT40 with secondary channel above primary
166 return 126;
167 case 153:
168 case 161:
169 // HT40 with secondary channel below primary
170 return 127;
171 }
172 }
173 // Error
174 return 0;
175 }
176
177 // 6GHz Band
178 if ((band & IHostapd::BandMask::BAND_6_GHZ) != 0) {
179 // Channels 1, 5. 9, 13, ...
180 if ((channel & 0x03) == 0x01) {
181 // 20MHz channel
182 return 131;
183 }
184 // Channels 3, 11, 19, 27, ...
185 if ((channel & 0x07) == 0x03) {
186 // 40MHz channel
187 return 132;
188 }
189 // Channels 7, 23, 39, 55, ...
190 if ((channel & 0x0F) == 0x07) {
191 // 80MHz channel
192 return 133;
193 }
194 // Channels 15, 47, 69, ...
195 if ((channel & 0x1F) == 0x0F) {
196 // 160MHz channel
197 return 134;
198 }
199 // Error
200 return 0;
201 }
202
203 return 0;
204}
205
Roshan Pius30b452e2017-12-27 13:36:21 -0800206std::string CreateHostapdConfig(
207 const IHostapd::IfaceParams& iface_params,
208 const IHostapd::NetworkParams& nw_params)
209{
210 if (nw_params.ssid.size() >
211 static_cast<uint32_t>(
212 IHostapd::ParamSizeLimits::SSID_MAX_LEN_IN_BYTES)) {
213 wpa_printf(
214 MSG_ERROR, "Invalid SSID size: %zu", nw_params.ssid.size());
215 return "";
216 }
Roshan Pius0b8a64f2018-01-23 11:42:34 -0800217 if ((nw_params.encryptionType != IHostapd::EncryptionType::NONE) &&
218 (nw_params.pskPassphrase.size() <
219 static_cast<uint32_t>(
220 IHostapd::ParamSizeLimits::
221 WPA2_PSK_PASSPHRASE_MIN_LEN_IN_BYTES) ||
222 nw_params.pskPassphrase.size() >
223 static_cast<uint32_t>(
224 IHostapd::ParamSizeLimits::
225 WPA2_PSK_PASSPHRASE_MAX_LEN_IN_BYTES))) {
Roshan Pius30b452e2017-12-27 13:36:21 -0800226 wpa_printf(
227 MSG_ERROR, "Invalid psk passphrase size: %zu",
228 nw_params.pskPassphrase.size());
229 return "";
230 }
231
232 // SSID string
233 std::stringstream ss;
234 ss << std::hex;
235 ss << std::setfill('0');
236 for (uint8_t b : nw_params.ssid) {
237 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;
243 switch (nw_params.encryptionType) {
244 case IHostapd::EncryptionType::NONE:
245 // no security params
246 break;
247 case IHostapd::EncryptionType::WPA:
248 encryption_config_as_string = StringPrintf(
249 "wpa=3\n"
250 "wpa_pairwise=TKIP CCMP\n"
251 "wpa_passphrase=%s",
252 nw_params.pskPassphrase.c_str());
253 break;
254 case IHostapd::EncryptionType::WPA2:
255 encryption_config_as_string = StringPrintf(
256 "wpa=2\n"
257 "rsn_pairwise=CCMP\n"
258 "wpa_passphrase=%s",
259 nw_params.pskPassphrase.c_str());
260 break;
261 default:
262 wpa_printf(MSG_ERROR, "Unknown encryption type");
263 return "";
264 }
265
Ahmed ElArabawy7b4b40a2020-01-02 08:55:38 -0800266 unsigned int band = 0;
267 band |= iface_params.channelParams.bandMask;
268
Roshan Pius30b452e2017-12-27 13:36:21 -0800269 std::string channel_config_as_string;
Ahmed ElArabawya390f292019-12-21 13:07:02 -0800270 bool isFirst = true;
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800271 if (iface_params.V1_1.V1_0.channelParams.enableAcs) {
Ahmed ElArabawya390f292019-12-21 13:07:02 -0800272 std::string freqList_as_string;
Daisuke Niwac1bd20f2019-01-09 13:51:02 +0900273 for (const auto &range :
Ahmed ElArabawya390f292019-12-21 13:07:02 -0800274 iface_params.channelParams.acsChannelFreqRangesMhz) {
275 if (!isFirst) {
276 freqList_as_string += ",";
277 }
278 isFirst = false;
279
Daisuke Niwac1bd20f2019-01-09 13:51:02 +0900280 if (range.start != range.end) {
Ahmed ElArabawya390f292019-12-21 13:07:02 -0800281 freqList_as_string +=
282 StringPrintf("%d-%d", range.start, range.end);
Daisuke Niwac1bd20f2019-01-09 13:51:02 +0900283 } else {
Ahmed ElArabawya390f292019-12-21 13:07:02 -0800284 freqList_as_string += StringPrintf("%d", range.start);
Daisuke Niwac1bd20f2019-01-09 13:51:02 +0900285 }
286 }
Roshan Pius30b452e2017-12-27 13:36:21 -0800287 channel_config_as_string = StringPrintf(
288 "channel=0\n"
Daisuke Niwac1bd20f2019-01-09 13:51:02 +0900289 "acs_exclude_dfs=%d\n"
Ahmed ElArabawya390f292019-12-21 13:07:02 -0800290 "freqlist=%s",
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800291 iface_params.V1_1.V1_0.channelParams.acsShouldExcludeDfs,
Ahmed ElArabawya390f292019-12-21 13:07:02 -0800292 freqList_as_string.c_str());
Roshan Pius30b452e2017-12-27 13:36:21 -0800293 } else {
Ahmed ElArabawy7b4b40a2020-01-02 08:55:38 -0800294 int op_class = getOpClassForChannel(
295 iface_params.V1_1.V1_0.channelParams.channel,
296 band,
297 iface_params.V1_1.V1_0.hwModeParams.enable80211N,
298 iface_params.V1_1.V1_0.hwModeParams.enable80211AC);
Roshan Pius30b452e2017-12-27 13:36:21 -0800299 channel_config_as_string = StringPrintf(
Ahmed ElArabawy7b4b40a2020-01-02 08:55:38 -0800300 "channel=%d\n"
301 "op_class=%d",
302 iface_params.V1_1.V1_0.channelParams.channel, op_class);
Roshan Pius30b452e2017-12-27 13:36:21 -0800303 }
304
Roshan Pius30b452e2017-12-27 13:36:21 -0800305 std::string hw_mode_as_string;
Roshan Pius49446472018-03-07 10:23:46 -0800306 std::string ht_cap_vht_oper_chwidth_as_string;
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800307
308 if ((band & IHostapd::BandMask::BAND_2_GHZ) != 0) {
Ahmed ElArabawya390f292019-12-21 13:07:02 -0800309 if (((band & IHostapd::BandMask::BAND_5_GHZ) != 0)
310 || ((band & IHostapd::BandMask::BAND_6_GHZ) != 0)) {
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800311 hw_mode_as_string = "hw_mode=any";
312 if (iface_params.V1_1.V1_0.channelParams.enableAcs) {
313 ht_cap_vht_oper_chwidth_as_string =
314 "ht_capab=[HT40+]\n"
315 "vht_oper_chwidth=1";
316 }
317 } else {
318 hw_mode_as_string = "hw_mode=g";
Roshan Pius49446472018-03-07 10:23:46 -0800319 }
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800320 } else {
Ahmed ElArabawya390f292019-12-21 13:07:02 -0800321 if (((band & IHostapd::BandMask::BAND_5_GHZ) != 0)
322 || ((band & IHostapd::BandMask::BAND_6_GHZ) != 0)) {
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800323 hw_mode_as_string = "hw_mode=a";
324 if (iface_params.V1_1.V1_0.channelParams.enableAcs) {
325 ht_cap_vht_oper_chwidth_as_string =
326 "ht_capab=[HT40+]\n"
327 "vht_oper_chwidth=1";
328 }
329 } else {
330 wpa_printf(MSG_ERROR, "Invalid band");
331 return "";
Roshan Pius49446472018-03-07 10:23:46 -0800332 }
Roshan Pius30b452e2017-12-27 13:36:21 -0800333 }
334
Ahmed ElArabawy5362fb12020-01-02 15:01:31 -0800335 std::string he_params_as_string;
336 if (iface_params.hwModeParams.enable80211AX) {
337 he_params_as_string = StringPrintf(
338 "ieee80211ax=1\n"
339 "he_su_beamformer=%d\n"
340 "he_su_beamformee=%d\n"
341 "he_mu_beamformer=%d\n"
342 "he_bss_color=%d\n"
343 "he_twt_required=%d\n",
344 iface_params.hwModeParams.enableHeSingleUserBeamformer ? 1 : 0,
345 iface_params.hwModeParams.enableHeSingleUserBeamformee ? 1 : 0,
346 iface_params.hwModeParams.enableHeMultiUserBeamformer ? 1 : 0,
347 iface_params.hwModeParams.heBssColor,
348 iface_params.hwModeParams.enableHeTargetWakeTime ? 1 : 0);
349 } else {
350 he_params_as_string = "ieee80211ax=0";
351 }
352
Roshan Pius30b452e2017-12-27 13:36:21 -0800353 return StringPrintf(
354 "interface=%s\n"
355 "driver=nl80211\n"
Daichi Ueuracaa74a82018-02-14 22:25:39 +0900356 "ctrl_interface=/data/vendor/wifi/hostapd/ctrl\n"
Roshan Pius30b452e2017-12-27 13:36:21 -0800357 // ssid2 signals to hostapd that the value is not a literal value
358 // for use as a SSID. In this case, we're giving it a hex
359 // std::string and hostapd needs to expect that.
360 "ssid2=%s\n"
361 "%s\n"
362 "ieee80211n=%d\n"
363 "ieee80211ac=%d\n"
Ahmed ElArabawy5362fb12020-01-02 15:01:31 -0800364 "%s\n"
Roshan Piuse453f712018-02-09 15:49:57 -0800365 "%s\n"
Roshan Pius49446472018-03-07 10:23:46 -0800366 "%s\n"
Roshan Pius30b452e2017-12-27 13:36:21 -0800367 "ignore_broadcast_ssid=%d\n"
368 "wowlan_triggers=any\n"
369 "%s\n",
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800370 iface_params.V1_1.V1_0.ifaceName.c_str(), ssid_as_string.c_str(),
Roshan Pius30b452e2017-12-27 13:36:21 -0800371 channel_config_as_string.c_str(),
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800372 iface_params.V1_1.V1_0.hwModeParams.enable80211N ? 1 : 0,
373 iface_params.V1_1.V1_0.hwModeParams.enable80211AC ? 1 : 0,
Ahmed ElArabawy5362fb12020-01-02 15:01:31 -0800374 he_params_as_string.c_str(),
Roshan Pius49446472018-03-07 10:23:46 -0800375 hw_mode_as_string.c_str(), ht_cap_vht_oper_chwidth_as_string.c_str(),
376 nw_params.isHidden ? 1 : 0, encryption_config_as_string.c_str());
Roshan Pius30b452e2017-12-27 13:36:21 -0800377}
Roshan Piuse2d21012018-08-17 11:22:06 -0700378
379// hostapd core functions accept "C" style function pointers, so use global
380// functions to pass to the hostapd core function and store the corresponding
381// std::function methods to be invoked.
382//
383// NOTE: Using the pattern from the vendor HAL (wifi_legacy_hal.cpp).
384//
385// Callback to be invoked once setup is complete
386std::function<void(struct hostapd_data*)> on_setup_complete_internal_callback;
387void onAsyncSetupCompleteCb(void* ctx)
388{
389 struct hostapd_data* iface_hapd = (struct hostapd_data*)ctx;
390 if (on_setup_complete_internal_callback) {
391 on_setup_complete_internal_callback(iface_hapd);
392 // Invalidate this callback since we don't want this firing
393 // again.
394 on_setup_complete_internal_callback = nullptr;
395 }
396}
Roshan Pius30b452e2017-12-27 13:36:21 -0800397} // namespace
398
Roshan Piuscc817562017-12-22 14:45:05 -0800399namespace android {
400namespace hardware {
401namespace wifi {
402namespace hostapd {
lesl1a842e62019-12-02 19:00:37 +0800403namespace V1_2 {
Roshan Piuscc817562017-12-22 14:45:05 -0800404namespace implementation {
405using hidl_return_util::call;
Roshan Piuse2d21012018-08-17 11:22:06 -0700406using namespace android::hardware::wifi::hostapd::V1_0;
Roshan Piuscc817562017-12-22 14:45:05 -0800407
408Hostapd::Hostapd(struct hapd_interfaces* interfaces) : interfaces_(interfaces)
409{}
410
411Return<void> Hostapd::addAccessPoint(
Daisuke Niwac1bd20f2019-01-09 13:51:02 +0900412 const V1_0::IHostapd::IfaceParams& iface_params,
413 const NetworkParams& nw_params, addAccessPoint_cb _hidl_cb)
414{
415 return call(
416 this, &Hostapd::addAccessPointInternal, _hidl_cb, iface_params,
417 nw_params);
418}
419
420Return<void> Hostapd::addAccessPoint_1_1(
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800421 const V1_1::IHostapd::IfaceParams& iface_params,
422 const NetworkParams& nw_params, addAccessPoint_cb _hidl_cb)
Roshan Piuscc817562017-12-22 14:45:05 -0800423{
424 return call(
Daisuke Niwac1bd20f2019-01-09 13:51:02 +0900425 this, &Hostapd::addAccessPointInternal_1_1, _hidl_cb, iface_params,
Roshan Piuscc817562017-12-22 14:45:05 -0800426 nw_params);
427}
428
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800429Return<void> Hostapd::addAccessPoint_1_2(
430 const IfaceParams& iface_params, const NetworkParams& nw_params,
431 addAccessPoint_1_2_cb _hidl_cb)
432{
433 return call(
434 this, &Hostapd::addAccessPointInternal_1_2, _hidl_cb, iface_params,
435 nw_params);
436}
437
Roshan Piuscc817562017-12-22 14:45:05 -0800438Return<void> Hostapd::removeAccessPoint(
439 const hidl_string& iface_name, removeAccessPoint_cb _hidl_cb)
440{
441 return call(
442 this, &Hostapd::removeAccessPointInternal, _hidl_cb, iface_name);
443}
444
Roshan Piuse2d21012018-08-17 11:22:06 -0700445Return<void> Hostapd::terminate()
446{
Roshan Pius3455af42018-02-01 09:19:49 -0800447 wpa_printf(MSG_INFO, "Terminating...");
448 eloop_terminate();
449 return Void();
450}
451
Roshan Piuse2d21012018-08-17 11:22:06 -0700452Return<void> Hostapd::registerCallback(
lesl1a842e62019-12-02 19:00:37 +0800453 const sp<V1_1::IHostapdCallback>& callback, registerCallback_cb _hidl_cb)
Roshan Piuse2d21012018-08-17 11:22:06 -0700454{
455 return call(
456 this, &Hostapd::registerCallbackInternal, _hidl_cb, callback);
457}
458
lesl1a842e62019-12-02 19:00:37 +0800459Return<void> Hostapd::forceClientDisconnect(
460 const hidl_string& iface_name, const hidl_array<uint8_t, 6>& client_address,
461 V1_2::Ieee80211ReasonCode reason_code, forceClientDisconnect_cb _hidl_cb)
462{
463 return call(
464 this, &Hostapd::forceClientDisconnectInternal, _hidl_cb, iface_name,
465 client_address, reason_code);
466}
467
Roger Wangcede5062019-12-30 12:56:28 +0800468Return<void> Hostapd::setDebugParams(
469 DebugLevel level, setDebugParams_cb _hidl_cb)
470{
471 return call(
472 this, &Hostapd::setDebugParamsInternal, _hidl_cb, level);
473}
lesl1a842e62019-12-02 19:00:37 +0800474
475V1_0::HostapdStatus Hostapd::addAccessPointInternal(
Daisuke Niwac1bd20f2019-01-09 13:51:02 +0900476 const V1_0::IHostapd::IfaceParams& iface_params,
477 const NetworkParams& nw_params)
478{
lesl1a842e62019-12-02 19:00:37 +0800479 return {V1_0::HostapdStatusCode::FAILURE_UNKNOWN, ""};
Daisuke Niwac1bd20f2019-01-09 13:51:02 +0900480}
481
lesl1a842e62019-12-02 19:00:37 +0800482V1_0::HostapdStatus Hostapd::addAccessPointInternal_1_1(
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800483 const V1_1::IHostapd::IfaceParams& iface_params,
484 const NetworkParams& nw_params)
485{
486 return {V1_0::HostapdStatusCode::FAILURE_UNKNOWN, ""};
487}
488
489HostapdStatus Hostapd::addAccessPointInternal_1_2(
Roshan Piuscc817562017-12-22 14:45:05 -0800490 const IfaceParams& iface_params, const NetworkParams& nw_params)
491{
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800492 if (hostapd_get_iface(interfaces_, iface_params.V1_1.V1_0.ifaceName.c_str())) {
Roshan Pius087d8692017-12-27 14:11:44 -0800493 wpa_printf(
494 MSG_ERROR, "Interface %s already present",
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800495 iface_params.V1_1.V1_0.ifaceName.c_str());
496 return {HostapdStatusCode::FAILURE_IFACE_EXISTS, ""};
Roshan Pius087d8692017-12-27 14:11:44 -0800497 }
498 const auto conf_params = CreateHostapdConfig(iface_params, nw_params);
499 if (conf_params.empty()) {
500 wpa_printf(MSG_ERROR, "Failed to create config params");
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800501 return {HostapdStatusCode::FAILURE_ARGS_INVALID, ""};
Roshan Pius087d8692017-12-27 14:11:44 -0800502 }
503 const auto conf_file_path =
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800504 WriteHostapdConfig(iface_params.V1_1.V1_0.ifaceName, conf_params);
Roshan Pius087d8692017-12-27 14:11:44 -0800505 if (conf_file_path.empty()) {
506 wpa_printf(MSG_ERROR, "Failed to write config file");
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800507 return {HostapdStatusCode::FAILURE_UNKNOWN, ""};
Roshan Pius087d8692017-12-27 14:11:44 -0800508 }
509 std::string add_iface_param_str = StringPrintf(
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800510 "%s config=%s", iface_params.V1_1.V1_0.ifaceName.c_str(),
Roshan Pius087d8692017-12-27 14:11:44 -0800511 conf_file_path.c_str());
512 std::vector<char> add_iface_param_vec(
513 add_iface_param_str.begin(), add_iface_param_str.end() + 1);
514 if (hostapd_add_iface(interfaces_, add_iface_param_vec.data()) < 0) {
515 wpa_printf(
516 MSG_ERROR, "Adding interface %s failed",
517 add_iface_param_str.c_str());
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800518 return {HostapdStatusCode::FAILURE_UNKNOWN, ""};
Roshan Pius087d8692017-12-27 14:11:44 -0800519 }
520 struct hostapd_data* iface_hapd =
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800521 hostapd_get_iface(interfaces_, iface_params.V1_1.V1_0.ifaceName.c_str());
Roshan Pius087d8692017-12-27 14:11:44 -0800522 WPA_ASSERT(iface_hapd != nullptr && iface_hapd->iface != nullptr);
Roshan Piuse2d21012018-08-17 11:22:06 -0700523 // Register the setup complete callbacks
524 on_setup_complete_internal_callback =
525 [this](struct hostapd_data* iface_hapd) {
526 wpa_printf(
527 MSG_DEBUG, "AP interface setup completed - state %s",
528 hostapd_state_text(iface_hapd->iface->state));
529 if (iface_hapd->iface->state == HAPD_IFACE_DISABLED) {
530 // Invoke the failure callback on all registered
531 // clients.
532 for (const auto& callback : callbacks_) {
533 callback->onFailure(
534 iface_hapd->conf->iface);
535 }
536 }
537 };
538 iface_hapd->setup_complete_cb = onAsyncSetupCompleteCb;
539 iface_hapd->setup_complete_cb_ctx = iface_hapd;
Roshan Pius087d8692017-12-27 14:11:44 -0800540 if (hostapd_enable_iface(iface_hapd->iface) < 0) {
541 wpa_printf(
542 MSG_ERROR, "Enabling interface %s failed",
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800543 iface_params.V1_1.V1_0.ifaceName.c_str());
544 return {HostapdStatusCode::FAILURE_UNKNOWN, ""};
Roshan Pius087d8692017-12-27 14:11:44 -0800545 }
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800546 return {HostapdStatusCode::SUCCESS, ""};
Roshan Piuscc817562017-12-22 14:45:05 -0800547}
548
lesl1a842e62019-12-02 19:00:37 +0800549V1_0::HostapdStatus Hostapd::removeAccessPointInternal(const std::string& iface_name)
Roshan Piuscc817562017-12-22 14:45:05 -0800550{
Roshan Pius087d8692017-12-27 14:11:44 -0800551 std::vector<char> remove_iface_param_vec(
552 iface_name.begin(), iface_name.end() + 1);
553 if (hostapd_remove_iface(interfaces_, remove_iface_param_vec.data()) <
554 0) {
555 wpa_printf(
556 MSG_ERROR, "Removing interface %s failed",
557 iface_name.c_str());
lesl1a842e62019-12-02 19:00:37 +0800558 return {V1_0::HostapdStatusCode::FAILURE_UNKNOWN, ""};
Roshan Pius087d8692017-12-27 14:11:44 -0800559 }
lesl1a842e62019-12-02 19:00:37 +0800560 return {V1_0::HostapdStatusCode::SUCCESS, ""};
Roshan Piuscc817562017-12-22 14:45:05 -0800561}
Roshan Piuse2d21012018-08-17 11:22:06 -0700562
lesl1a842e62019-12-02 19:00:37 +0800563V1_0::HostapdStatus Hostapd::registerCallbackInternal(
564 const sp<V1_1::IHostapdCallback>& callback)
Roshan Piuse2d21012018-08-17 11:22:06 -0700565{
566 callbacks_.push_back(callback);
lesl1a842e62019-12-02 19:00:37 +0800567 return {V1_0::HostapdStatusCode::SUCCESS, ""};
568}
569
570V1_2::HostapdStatus Hostapd::forceClientDisconnectInternal(const std::string& iface_name,
571 const std::array<uint8_t, 6>& client_address, V1_2::Ieee80211ReasonCode reason_code)
572{
573 struct hostapd_data *hapd = hostapd_get_iface(interfaces_, iface_name.c_str());
574 struct sta_info *sta;
575 if (!hapd) {
576 wpa_printf(MSG_ERROR, "Interface %s doesn't exist", iface_name.c_str());
577 return {V1_2::HostapdStatusCode::FAILURE_IFACE_UNKNOWN, ""};
578 }
579 for (sta = hapd->sta_list; sta; sta = sta->next) {
580 int res;
581 res = memcmp(sta->addr, client_address.data(), ETH_ALEN);
582 if (res == 0) {
583 wpa_printf(MSG_INFO, "Force client:" MACSTR " disconnect with reason: %d",
584 MAC2STR(client_address.data()), (uint16_t) reason_code);
585 ap_sta_disconnect(hapd, sta, sta->addr, (uint16_t) reason_code);
586 return {V1_2::HostapdStatusCode::SUCCESS, ""};
587 }
588 }
589 return {V1_2::HostapdStatusCode::FAILURE_CLIENT_UNKNOWN, ""};
Roshan Piuse2d21012018-08-17 11:22:06 -0700590}
591
Roger Wangcede5062019-12-30 12:56:28 +0800592V1_2::HostapdStatus Hostapd::setDebugParamsInternal(DebugLevel level)
593{
594 wpa_debug_level = static_cast<uint32_t>(level);
595 return {V1_2::HostapdStatusCode::SUCCESS, ""};
596}
597
Roshan Piuscc817562017-12-22 14:45:05 -0800598} // namespace implementation
lesl1a842e62019-12-02 19:00:37 +0800599} // namespace V1_2
Roshan Piuscc817562017-12-22 14:45:05 -0800600} // namespace hostapd
601} // namespace wifi
602} // namespace hardware
603} // namespace android