blob: aa16e8c7e542c654fd1574ee6079d04814bbce72 [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 }
Kai Shia0cfc142020-09-16 20:05:13 -0700199 if (channel == 2) {
200 // 20MHz channel
201 return 136;
202 }
Ahmed ElArabawy7b4b40a2020-01-02 08:55:38 -0800203 // Error
204 return 0;
205 }
206
207 return 0;
208}
209
lesldd48efd2019-12-26 15:13:51 +0800210bool validatePassphrase(int passphrase_len, int min_len, int max_len)
211{
212 if (min_len != -1 && passphrase_len < min_len) return false;
213 if (max_len != -1 && passphrase_len > max_len) return false;
214 return true;
215}
216
Roshan Pius30b452e2017-12-27 13:36:21 -0800217std::string CreateHostapdConfig(
218 const IHostapd::IfaceParams& iface_params,
219 const IHostapd::NetworkParams& nw_params)
220{
lesldd48efd2019-12-26 15:13:51 +0800221 if (nw_params.V1_0.ssid.size() >
Roshan Pius30b452e2017-12-27 13:36:21 -0800222 static_cast<uint32_t>(
223 IHostapd::ParamSizeLimits::SSID_MAX_LEN_IN_BYTES)) {
224 wpa_printf(
lesldd48efd2019-12-26 15:13:51 +0800225 MSG_ERROR, "Invalid SSID size: %zu", nw_params.V1_0.ssid.size());
Roshan Pius30b452e2017-12-27 13:36:21 -0800226 return "";
227 }
228
229 // SSID string
230 std::stringstream ss;
231 ss << std::hex;
232 ss << std::setfill('0');
lesldd48efd2019-12-26 15:13:51 +0800233 for (uint8_t b : nw_params.V1_0.ssid) {
Roshan Pius30b452e2017-12-27 13:36:21 -0800234 ss << std::setw(2) << static_cast<unsigned int>(b);
235 }
236 const std::string ssid_as_string = ss.str();
237
238 // Encryption config string
239 std::string encryption_config_as_string;
240 switch (nw_params.encryptionType) {
241 case IHostapd::EncryptionType::NONE:
242 // no security params
243 break;
244 case IHostapd::EncryptionType::WPA:
lesldd48efd2019-12-26 15:13:51 +0800245 if (!validatePassphrase(
246 nw_params.passphrase.size(),
247 static_cast<uint32_t>(IHostapd::ParamSizeLimits::
248 WPA2_PSK_PASSPHRASE_MIN_LEN_IN_BYTES),
249 static_cast<uint32_t>(IHostapd::ParamSizeLimits::
250 WPA2_PSK_PASSPHRASE_MAX_LEN_IN_BYTES))) {
251 return "";
252 }
Roshan Pius30b452e2017-12-27 13:36:21 -0800253 encryption_config_as_string = StringPrintf(
254 "wpa=3\n"
255 "wpa_pairwise=TKIP CCMP\n"
256 "wpa_passphrase=%s",
lesldd48efd2019-12-26 15:13:51 +0800257 nw_params.passphrase.c_str());
Roshan Pius30b452e2017-12-27 13:36:21 -0800258 break;
259 case IHostapd::EncryptionType::WPA2:
lesldd48efd2019-12-26 15:13:51 +0800260 if (!validatePassphrase(
261 nw_params.passphrase.size(),
262 static_cast<uint32_t>(IHostapd::ParamSizeLimits::
263 WPA2_PSK_PASSPHRASE_MIN_LEN_IN_BYTES),
264 static_cast<uint32_t>(IHostapd::ParamSizeLimits::
265 WPA2_PSK_PASSPHRASE_MAX_LEN_IN_BYTES))) {
266 return "";
267 }
Roshan Pius30b452e2017-12-27 13:36:21 -0800268 encryption_config_as_string = StringPrintf(
269 "wpa=2\n"
270 "rsn_pairwise=CCMP\n"
271 "wpa_passphrase=%s",
lesldd48efd2019-12-26 15:13:51 +0800272 nw_params.passphrase.c_str());
273 break;
274 case IHostapd::EncryptionType::WPA3_SAE_TRANSITION:
275 if (!validatePassphrase(
276 nw_params.passphrase.size(),
277 static_cast<uint32_t>(IHostapd::ParamSizeLimits::
278 WPA2_PSK_PASSPHRASE_MIN_LEN_IN_BYTES),
279 static_cast<uint32_t>(IHostapd::ParamSizeLimits::
280 WPA2_PSK_PASSPHRASE_MAX_LEN_IN_BYTES))) {
281 return "";
282 }
283 encryption_config_as_string = StringPrintf(
284 "wpa=2\n"
285 "rsn_pairwise=CCMP\n"
286 "wpa_key_mgmt=WPA-PSK SAE\n"
287 "ieee80211w=1\n"
288 "sae_require_mfp=1\n"
289 "wpa_passphrase=%s\n"
290 "sae_password=%s",
291 nw_params.passphrase.c_str(),
292 nw_params.passphrase.c_str());
293 break;
294 case IHostapd::EncryptionType::WPA3_SAE:
295 if (!validatePassphrase(nw_params.passphrase.size(), 1, -1)) {
296 return "";
297 }
298 encryption_config_as_string = StringPrintf(
299 "wpa=2\n"
300 "rsn_pairwise=CCMP\n"
301 "wpa_key_mgmt=SAE\n"
302 "ieee80211w=2\n"
303 "sae_require_mfp=2\n"
304 "sae_password=%s",
305 nw_params.passphrase.c_str());
Roshan Pius30b452e2017-12-27 13:36:21 -0800306 break;
307 default:
308 wpa_printf(MSG_ERROR, "Unknown encryption type");
309 return "";
310 }
311
Ahmed ElArabawy7b4b40a2020-01-02 08:55:38 -0800312 unsigned int band = 0;
313 band |= iface_params.channelParams.bandMask;
314
Roshan Pius30b452e2017-12-27 13:36:21 -0800315 std::string channel_config_as_string;
Ahmed ElArabawya390f292019-12-21 13:07:02 -0800316 bool isFirst = true;
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800317 if (iface_params.V1_1.V1_0.channelParams.enableAcs) {
Ahmed ElArabawya390f292019-12-21 13:07:02 -0800318 std::string freqList_as_string;
Daisuke Niwac1bd20f2019-01-09 13:51:02 +0900319 for (const auto &range :
Ahmed ElArabawya390f292019-12-21 13:07:02 -0800320 iface_params.channelParams.acsChannelFreqRangesMhz) {
321 if (!isFirst) {
322 freqList_as_string += ",";
323 }
324 isFirst = false;
325
Daisuke Niwac1bd20f2019-01-09 13:51:02 +0900326 if (range.start != range.end) {
Ahmed ElArabawya390f292019-12-21 13:07:02 -0800327 freqList_as_string +=
328 StringPrintf("%d-%d", range.start, range.end);
Daisuke Niwac1bd20f2019-01-09 13:51:02 +0900329 } else {
Ahmed ElArabawya390f292019-12-21 13:07:02 -0800330 freqList_as_string += StringPrintf("%d", range.start);
Daisuke Niwac1bd20f2019-01-09 13:51:02 +0900331 }
332 }
Roshan Pius30b452e2017-12-27 13:36:21 -0800333 channel_config_as_string = StringPrintf(
334 "channel=0\n"
Daisuke Niwac1bd20f2019-01-09 13:51:02 +0900335 "acs_exclude_dfs=%d\n"
Ahmed ElArabawya390f292019-12-21 13:07:02 -0800336 "freqlist=%s",
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800337 iface_params.V1_1.V1_0.channelParams.acsShouldExcludeDfs,
Ahmed ElArabawya390f292019-12-21 13:07:02 -0800338 freqList_as_string.c_str());
Roshan Pius30b452e2017-12-27 13:36:21 -0800339 } else {
Ahmed ElArabawy7b4b40a2020-01-02 08:55:38 -0800340 int op_class = getOpClassForChannel(
341 iface_params.V1_1.V1_0.channelParams.channel,
342 band,
343 iface_params.V1_1.V1_0.hwModeParams.enable80211N,
344 iface_params.V1_1.V1_0.hwModeParams.enable80211AC);
Roshan Pius30b452e2017-12-27 13:36:21 -0800345 channel_config_as_string = StringPrintf(
Ahmed ElArabawy7b4b40a2020-01-02 08:55:38 -0800346 "channel=%d\n"
347 "op_class=%d",
348 iface_params.V1_1.V1_0.channelParams.channel, op_class);
Roshan Pius30b452e2017-12-27 13:36:21 -0800349 }
350
Roshan Pius30b452e2017-12-27 13:36:21 -0800351 std::string hw_mode_as_string;
Roshan Pius49446472018-03-07 10:23:46 -0800352 std::string ht_cap_vht_oper_chwidth_as_string;
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800353
354 if ((band & IHostapd::BandMask::BAND_2_GHZ) != 0) {
Ahmed ElArabawya390f292019-12-21 13:07:02 -0800355 if (((band & IHostapd::BandMask::BAND_5_GHZ) != 0)
356 || ((band & IHostapd::BandMask::BAND_6_GHZ) != 0)) {
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800357 hw_mode_as_string = "hw_mode=any";
358 if (iface_params.V1_1.V1_0.channelParams.enableAcs) {
359 ht_cap_vht_oper_chwidth_as_string =
360 "ht_capab=[HT40+]\n"
361 "vht_oper_chwidth=1";
362 }
363 } else {
364 hw_mode_as_string = "hw_mode=g";
Roshan Pius49446472018-03-07 10:23:46 -0800365 }
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800366 } else {
Ahmed ElArabawya390f292019-12-21 13:07:02 -0800367 if (((band & IHostapd::BandMask::BAND_5_GHZ) != 0)
368 || ((band & IHostapd::BandMask::BAND_6_GHZ) != 0)) {
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800369 hw_mode_as_string = "hw_mode=a";
370 if (iface_params.V1_1.V1_0.channelParams.enableAcs) {
371 ht_cap_vht_oper_chwidth_as_string =
372 "ht_capab=[HT40+]\n"
373 "vht_oper_chwidth=1";
374 }
375 } else {
376 wpa_printf(MSG_ERROR, "Invalid band");
377 return "";
Roshan Pius49446472018-03-07 10:23:46 -0800378 }
Roshan Pius30b452e2017-12-27 13:36:21 -0800379 }
380
Ahmed ElArabawy5362fb12020-01-02 15:01:31 -0800381 std::string he_params_as_string;
Roshan Pius36510302020-05-09 21:01:01 -0700382#ifdef CONFIG_IEEE80211AX
Ahmed ElArabawy5362fb12020-01-02 15:01:31 -0800383 if (iface_params.hwModeParams.enable80211AX) {
384 he_params_as_string = StringPrintf(
385 "ieee80211ax=1\n"
386 "he_su_beamformer=%d\n"
387 "he_su_beamformee=%d\n"
388 "he_mu_beamformer=%d\n"
Ahmed ElArabawy5362fb12020-01-02 15:01:31 -0800389 "he_twt_required=%d\n",
390 iface_params.hwModeParams.enableHeSingleUserBeamformer ? 1 : 0,
391 iface_params.hwModeParams.enableHeSingleUserBeamformee ? 1 : 0,
392 iface_params.hwModeParams.enableHeMultiUserBeamformer ? 1 : 0,
Ahmed ElArabawy5362fb12020-01-02 15:01:31 -0800393 iface_params.hwModeParams.enableHeTargetWakeTime ? 1 : 0);
394 } else {
395 he_params_as_string = "ieee80211ax=0";
396 }
Roshan Pius36510302020-05-09 21:01:01 -0700397#endif /* CONFIG_IEEE80211AX */
Ahmed ElArabawy5362fb12020-01-02 15:01:31 -0800398
Roshan Pius30b452e2017-12-27 13:36:21 -0800399 return StringPrintf(
400 "interface=%s\n"
401 "driver=nl80211\n"
Daichi Ueuracaa74a82018-02-14 22:25:39 +0900402 "ctrl_interface=/data/vendor/wifi/hostapd/ctrl\n"
Roshan Pius30b452e2017-12-27 13:36:21 -0800403 // ssid2 signals to hostapd that the value is not a literal value
404 // for use as a SSID. In this case, we're giving it a hex
405 // std::string and hostapd needs to expect that.
406 "ssid2=%s\n"
407 "%s\n"
408 "ieee80211n=%d\n"
409 "ieee80211ac=%d\n"
Ahmed ElArabawy5362fb12020-01-02 15:01:31 -0800410 "%s\n"
Roshan Piuse453f712018-02-09 15:49:57 -0800411 "%s\n"
Roshan Pius49446472018-03-07 10:23:46 -0800412 "%s\n"
Roshan Pius30b452e2017-12-27 13:36:21 -0800413 "ignore_broadcast_ssid=%d\n"
414 "wowlan_triggers=any\n"
415 "%s\n",
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800416 iface_params.V1_1.V1_0.ifaceName.c_str(), ssid_as_string.c_str(),
Roshan Pius30b452e2017-12-27 13:36:21 -0800417 channel_config_as_string.c_str(),
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800418 iface_params.V1_1.V1_0.hwModeParams.enable80211N ? 1 : 0,
419 iface_params.V1_1.V1_0.hwModeParams.enable80211AC ? 1 : 0,
Ahmed ElArabawy5362fb12020-01-02 15:01:31 -0800420 he_params_as_string.c_str(),
Roshan Pius49446472018-03-07 10:23:46 -0800421 hw_mode_as_string.c_str(), ht_cap_vht_oper_chwidth_as_string.c_str(),
lesldd48efd2019-12-26 15:13:51 +0800422 nw_params.V1_0.isHidden ? 1 : 0, encryption_config_as_string.c_str());
Roshan Pius30b452e2017-12-27 13:36:21 -0800423}
Roshan Piuse2d21012018-08-17 11:22:06 -0700424
425// hostapd core functions accept "C" style function pointers, so use global
426// functions to pass to the hostapd core function and store the corresponding
427// std::function methods to be invoked.
428//
429// NOTE: Using the pattern from the vendor HAL (wifi_legacy_hal.cpp).
430//
431// Callback to be invoked once setup is complete
432std::function<void(struct hostapd_data*)> on_setup_complete_internal_callback;
433void onAsyncSetupCompleteCb(void* ctx)
434{
435 struct hostapd_data* iface_hapd = (struct hostapd_data*)ctx;
436 if (on_setup_complete_internal_callback) {
437 on_setup_complete_internal_callback(iface_hapd);
438 // Invalidate this callback since we don't want this firing
439 // again.
440 on_setup_complete_internal_callback = nullptr;
441 }
442}
lesld0621052020-08-10 13:54:28 +0800443
444// Callback to be invoked on hotspot client connection/disconnection
445std::function<void(struct hostapd_data*, const u8 *mac_addr, int authorized,
446 const u8 *p2p_dev_addr)> on_sta_authorized_internal_callback;
447void onAsyncStaAuthorizedCb(void* ctx, const u8 *mac_addr, int authorized,
448 const u8 *p2p_dev_addr)
449{
450 struct hostapd_data* iface_hapd = (struct hostapd_data*)ctx;
451 if (on_sta_authorized_internal_callback) {
452 on_sta_authorized_internal_callback(iface_hapd, mac_addr,
453 authorized, p2p_dev_addr);
454 }
455}
456
Roshan Pius30b452e2017-12-27 13:36:21 -0800457} // namespace
458
Roshan Piuscc817562017-12-22 14:45:05 -0800459namespace android {
460namespace hardware {
461namespace wifi {
462namespace hostapd {
leslf8b1b402020-08-04 17:09:39 +0800463namespace V1_3 {
Roshan Piuscc817562017-12-22 14:45:05 -0800464namespace implementation {
465using hidl_return_util::call;
Roshan Piuse2d21012018-08-17 11:22:06 -0700466using namespace android::hardware::wifi::hostapd::V1_0;
Roshan Piuscc817562017-12-22 14:45:05 -0800467
468Hostapd::Hostapd(struct hapd_interfaces* interfaces) : interfaces_(interfaces)
469{}
470
471Return<void> Hostapd::addAccessPoint(
Daisuke Niwac1bd20f2019-01-09 13:51:02 +0900472 const V1_0::IHostapd::IfaceParams& iface_params,
lesldd48efd2019-12-26 15:13:51 +0800473 const V1_0::IHostapd::NetworkParams& nw_params, addAccessPoint_cb _hidl_cb)
Daisuke Niwac1bd20f2019-01-09 13:51:02 +0900474{
475 return call(
476 this, &Hostapd::addAccessPointInternal, _hidl_cb, iface_params,
477 nw_params);
478}
479
480Return<void> Hostapd::addAccessPoint_1_1(
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800481 const V1_1::IHostapd::IfaceParams& iface_params,
lesldd48efd2019-12-26 15:13:51 +0800482 const V1_0::IHostapd::NetworkParams& nw_params, addAccessPoint_cb _hidl_cb)
Roshan Piuscc817562017-12-22 14:45:05 -0800483{
484 return call(
Daisuke Niwac1bd20f2019-01-09 13:51:02 +0900485 this, &Hostapd::addAccessPointInternal_1_1, _hidl_cb, iface_params,
Roshan Piuscc817562017-12-22 14:45:05 -0800486 nw_params);
487}
488
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800489Return<void> Hostapd::addAccessPoint_1_2(
490 const IfaceParams& iface_params, const NetworkParams& nw_params,
491 addAccessPoint_1_2_cb _hidl_cb)
492{
493 return call(
494 this, &Hostapd::addAccessPointInternal_1_2, _hidl_cb, iface_params,
495 nw_params);
496}
497
Roshan Piuscc817562017-12-22 14:45:05 -0800498Return<void> Hostapd::removeAccessPoint(
499 const hidl_string& iface_name, removeAccessPoint_cb _hidl_cb)
500{
501 return call(
502 this, &Hostapd::removeAccessPointInternal, _hidl_cb, iface_name);
503}
504
Roshan Piuse2d21012018-08-17 11:22:06 -0700505Return<void> Hostapd::terminate()
506{
Roshan Pius3455af42018-02-01 09:19:49 -0800507 wpa_printf(MSG_INFO, "Terminating...");
508 eloop_terminate();
509 return Void();
510}
511
Roshan Piuse2d21012018-08-17 11:22:06 -0700512Return<void> Hostapd::registerCallback(
lesl1a842e62019-12-02 19:00:37 +0800513 const sp<V1_1::IHostapdCallback>& callback, registerCallback_cb _hidl_cb)
Roshan Piuse2d21012018-08-17 11:22:06 -0700514{
515 return call(
516 this, &Hostapd::registerCallbackInternal, _hidl_cb, callback);
517}
518
leslf8b1b402020-08-04 17:09:39 +0800519Return<void> Hostapd::registerCallback_1_3(
520 const sp<V1_3::IHostapdCallback>& callback, registerCallback_1_3_cb _hidl_cb)
521{
522 return call(
523 this, &Hostapd::registerCallbackInternal_1_3, _hidl_cb, callback);
524}
525
lesl1a842e62019-12-02 19:00:37 +0800526Return<void> Hostapd::forceClientDisconnect(
527 const hidl_string& iface_name, const hidl_array<uint8_t, 6>& client_address,
528 V1_2::Ieee80211ReasonCode reason_code, forceClientDisconnect_cb _hidl_cb)
529{
530 return call(
531 this, &Hostapd::forceClientDisconnectInternal, _hidl_cb, iface_name,
532 client_address, reason_code);
533}
534
Roger Wangcede5062019-12-30 12:56:28 +0800535Return<void> Hostapd::setDebugParams(
leslf8b1b402020-08-04 17:09:39 +0800536 V1_2::DebugLevel level, setDebugParams_cb _hidl_cb)
Roger Wangcede5062019-12-30 12:56:28 +0800537{
538 return call(
539 this, &Hostapd::setDebugParamsInternal, _hidl_cb, level);
540}
lesl1a842e62019-12-02 19:00:37 +0800541
542V1_0::HostapdStatus Hostapd::addAccessPointInternal(
Daisuke Niwac1bd20f2019-01-09 13:51:02 +0900543 const V1_0::IHostapd::IfaceParams& iface_params,
lesldd48efd2019-12-26 15:13:51 +0800544 const V1_0::IHostapd::NetworkParams& nw_params)
Daisuke Niwac1bd20f2019-01-09 13:51:02 +0900545{
lesl1a842e62019-12-02 19:00:37 +0800546 return {V1_0::HostapdStatusCode::FAILURE_UNKNOWN, ""};
Daisuke Niwac1bd20f2019-01-09 13:51:02 +0900547}
548
lesl1a842e62019-12-02 19:00:37 +0800549V1_0::HostapdStatus Hostapd::addAccessPointInternal_1_1(
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800550 const V1_1::IHostapd::IfaceParams& iface_params,
lesldd48efd2019-12-26 15:13:51 +0800551 const V1_1::IHostapd::NetworkParams& nw_params)
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800552{
553 return {V1_0::HostapdStatusCode::FAILURE_UNKNOWN, ""};
554}
555
leslf8b1b402020-08-04 17:09:39 +0800556V1_2::HostapdStatus Hostapd::addAccessPointInternal_1_2(
Roshan Piuscc817562017-12-22 14:45:05 -0800557 const IfaceParams& iface_params, const NetworkParams& nw_params)
558{
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800559 if (hostapd_get_iface(interfaces_, iface_params.V1_1.V1_0.ifaceName.c_str())) {
Roshan Pius087d8692017-12-27 14:11:44 -0800560 wpa_printf(
561 MSG_ERROR, "Interface %s already present",
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800562 iface_params.V1_1.V1_0.ifaceName.c_str());
leslf8b1b402020-08-04 17:09:39 +0800563 return {V1_2::HostapdStatusCode::FAILURE_IFACE_EXISTS, ""};
Roshan Pius087d8692017-12-27 14:11:44 -0800564 }
565 const auto conf_params = CreateHostapdConfig(iface_params, nw_params);
566 if (conf_params.empty()) {
567 wpa_printf(MSG_ERROR, "Failed to create config params");
leslf8b1b402020-08-04 17:09:39 +0800568 return {V1_2::HostapdStatusCode::FAILURE_ARGS_INVALID, ""};
Roshan Pius087d8692017-12-27 14:11:44 -0800569 }
570 const auto conf_file_path =
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800571 WriteHostapdConfig(iface_params.V1_1.V1_0.ifaceName, conf_params);
Roshan Pius087d8692017-12-27 14:11:44 -0800572 if (conf_file_path.empty()) {
573 wpa_printf(MSG_ERROR, "Failed to write config file");
leslf8b1b402020-08-04 17:09:39 +0800574 return {V1_2::HostapdStatusCode::FAILURE_UNKNOWN, ""};
Roshan Pius087d8692017-12-27 14:11:44 -0800575 }
576 std::string add_iface_param_str = StringPrintf(
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800577 "%s config=%s", iface_params.V1_1.V1_0.ifaceName.c_str(),
Roshan Pius087d8692017-12-27 14:11:44 -0800578 conf_file_path.c_str());
579 std::vector<char> add_iface_param_vec(
580 add_iface_param_str.begin(), add_iface_param_str.end() + 1);
581 if (hostapd_add_iface(interfaces_, add_iface_param_vec.data()) < 0) {
582 wpa_printf(
583 MSG_ERROR, "Adding interface %s failed",
584 add_iface_param_str.c_str());
leslf8b1b402020-08-04 17:09:39 +0800585 return {V1_2::HostapdStatusCode::FAILURE_UNKNOWN, ""};
Roshan Pius087d8692017-12-27 14:11:44 -0800586 }
587 struct hostapd_data* iface_hapd =
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800588 hostapd_get_iface(interfaces_, iface_params.V1_1.V1_0.ifaceName.c_str());
Roshan Pius087d8692017-12-27 14:11:44 -0800589 WPA_ASSERT(iface_hapd != nullptr && iface_hapd->iface != nullptr);
Roshan Piuse2d21012018-08-17 11:22:06 -0700590 // Register the setup complete callbacks
591 on_setup_complete_internal_callback =
592 [this](struct hostapd_data* iface_hapd) {
593 wpa_printf(
594 MSG_DEBUG, "AP interface setup completed - state %s",
595 hostapd_state_text(iface_hapd->iface->state));
596 if (iface_hapd->iface->state == HAPD_IFACE_DISABLED) {
597 // Invoke the failure callback on all registered
598 // clients.
599 for (const auto& callback : callbacks_) {
600 callback->onFailure(
601 iface_hapd->conf->iface);
602 }
603 }
604 };
lesld0621052020-08-10 13:54:28 +0800605
606 // Rgegister for new client connect/disconnect indication.
607 on_sta_authorized_internal_callback =
608 [this](struct hostapd_data* iface_hapd, const u8 *mac_addr,
609 int authorized, const u8 *p2p_dev_addr) {
610 wpa_printf(MSG_DEBUG, "notify client " MACSTR " %s",
611 MAC2STR(mac_addr),
612 (authorized) ? "Connected" : "Disconnected");
613
614 for (const auto &callback : callbacks_) {
615 // TODO: The iface need to separate to iface and ap instance
616 // identify for AP+AP case.
617 callback->onConnectedClientsChanged(iface_hapd->conf->iface,
618 iface_hapd->conf->iface, mac_addr, authorized);
619 }
620 };
621
622
623 // Setup callback
Roshan Piuse2d21012018-08-17 11:22:06 -0700624 iface_hapd->setup_complete_cb = onAsyncSetupCompleteCb;
625 iface_hapd->setup_complete_cb_ctx = iface_hapd;
lesld0621052020-08-10 13:54:28 +0800626 iface_hapd->sta_authorized_cb = onAsyncStaAuthorizedCb;
627 iface_hapd->sta_authorized_cb_ctx = iface_hapd;
628
Roshan Pius087d8692017-12-27 14:11:44 -0800629 if (hostapd_enable_iface(iface_hapd->iface) < 0) {
630 wpa_printf(
631 MSG_ERROR, "Enabling interface %s failed",
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800632 iface_params.V1_1.V1_0.ifaceName.c_str());
leslf8b1b402020-08-04 17:09:39 +0800633 return {V1_2::HostapdStatusCode::FAILURE_UNKNOWN, ""};
Roshan Pius087d8692017-12-27 14:11:44 -0800634 }
leslf8b1b402020-08-04 17:09:39 +0800635 return {V1_2::HostapdStatusCode::SUCCESS, ""};
Roshan Piuscc817562017-12-22 14:45:05 -0800636}
637
lesl1a842e62019-12-02 19:00:37 +0800638V1_0::HostapdStatus Hostapd::removeAccessPointInternal(const std::string& iface_name)
Roshan Piuscc817562017-12-22 14:45:05 -0800639{
Roshan Pius087d8692017-12-27 14:11:44 -0800640 std::vector<char> remove_iface_param_vec(
641 iface_name.begin(), iface_name.end() + 1);
642 if (hostapd_remove_iface(interfaces_, remove_iface_param_vec.data()) <
643 0) {
644 wpa_printf(
645 MSG_ERROR, "Removing interface %s failed",
646 iface_name.c_str());
lesl1a842e62019-12-02 19:00:37 +0800647 return {V1_0::HostapdStatusCode::FAILURE_UNKNOWN, ""};
Roshan Pius087d8692017-12-27 14:11:44 -0800648 }
lesl1a842e62019-12-02 19:00:37 +0800649 return {V1_0::HostapdStatusCode::SUCCESS, ""};
Roshan Piuscc817562017-12-22 14:45:05 -0800650}
Roshan Piuse2d21012018-08-17 11:22:06 -0700651
lesl1a842e62019-12-02 19:00:37 +0800652V1_0::HostapdStatus Hostapd::registerCallbackInternal(
653 const sp<V1_1::IHostapdCallback>& callback)
Roshan Piuse2d21012018-08-17 11:22:06 -0700654{
leslf8b1b402020-08-04 17:09:39 +0800655 return {V1_0::HostapdStatusCode::FAILURE_UNKNOWN, ""};
656}
657
658V1_2::HostapdStatus Hostapd::registerCallbackInternal_1_3(
659 const sp<V1_3::IHostapdCallback>& callback)
660{
Roshan Piuse2d21012018-08-17 11:22:06 -0700661 callbacks_.push_back(callback);
leslf8b1b402020-08-04 17:09:39 +0800662 return {V1_2::HostapdStatusCode::SUCCESS, ""};
lesl1a842e62019-12-02 19:00:37 +0800663}
664
665V1_2::HostapdStatus Hostapd::forceClientDisconnectInternal(const std::string& iface_name,
666 const std::array<uint8_t, 6>& client_address, V1_2::Ieee80211ReasonCode reason_code)
667{
668 struct hostapd_data *hapd = hostapd_get_iface(interfaces_, iface_name.c_str());
669 struct sta_info *sta;
670 if (!hapd) {
671 wpa_printf(MSG_ERROR, "Interface %s doesn't exist", iface_name.c_str());
672 return {V1_2::HostapdStatusCode::FAILURE_IFACE_UNKNOWN, ""};
673 }
674 for (sta = hapd->sta_list; sta; sta = sta->next) {
675 int res;
676 res = memcmp(sta->addr, client_address.data(), ETH_ALEN);
677 if (res == 0) {
678 wpa_printf(MSG_INFO, "Force client:" MACSTR " disconnect with reason: %d",
679 MAC2STR(client_address.data()), (uint16_t) reason_code);
680 ap_sta_disconnect(hapd, sta, sta->addr, (uint16_t) reason_code);
681 return {V1_2::HostapdStatusCode::SUCCESS, ""};
682 }
683 }
684 return {V1_2::HostapdStatusCode::FAILURE_CLIENT_UNKNOWN, ""};
Roshan Piuse2d21012018-08-17 11:22:06 -0700685}
686
leslf8b1b402020-08-04 17:09:39 +0800687V1_2::HostapdStatus Hostapd::setDebugParamsInternal(V1_2::DebugLevel level)
Roger Wangcede5062019-12-30 12:56:28 +0800688{
689 wpa_debug_level = static_cast<uint32_t>(level);
690 return {V1_2::HostapdStatusCode::SUCCESS, ""};
691}
692
Roshan Piuscc817562017-12-22 14:45:05 -0800693} // namespace implementation
leslf8b1b402020-08-04 17:09:39 +0800694} // namespace V1_3
Roshan Piuscc817562017-12-22 14:45:05 -0800695} // namespace hostapd
696} // namespace wifi
697} // namespace hardware
698} // namespace android