blob: 668d8f4fa3268cd13ac8becfc9b1d0a30d45400e [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}
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;
leslb0f4d542020-09-16 23:06:03 +080036using android::hardware::wifi::hostapd::V1_3::IHostapd;
lesldab91502020-08-17 18:29:47 +080037using android::hardware::wifi::hostapd::V1_3::Generation;
38using android::hardware::wifi::hostapd::V1_3::Bandwidth;
Roshan Pius30b452e2017-12-27 13:36:21 -080039
40std::string WriteHostapdConfig(
41 const std::string& interface_name, const std::string& config)
42{
43 const std::string file_path =
44 StringPrintf(kConfFileNameFmt, interface_name.c_str());
45 if (WriteStringToFile(
46 config, file_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
47 getuid(), getgid())) {
48 return file_path;
49 }
50 // Diagnose failure
51 int error = errno;
52 wpa_printf(
53 MSG_ERROR, "Cannot write hostapd config to %s, error: %s",
54 file_path.c_str(), strerror(error));
55 struct stat st;
56 int result = stat(file_path.c_str(), &st);
57 if (result == 0) {
58 wpa_printf(
59 MSG_ERROR, "hostapd config file uid: %d, gid: %d, mode: %d",
60 st.st_uid, st.st_gid, st.st_mode);
61 } else {
62 wpa_printf(
63 MSG_ERROR,
64 "Error calling stat() on hostapd config file: %s",
65 strerror(errno));
66 }
67 return "";
68}
69
Ahmed ElArabawy7b4b40a2020-01-02 08:55:38 -080070/*
71 * Get the op_class for a channel/band
72 * The logic here is based on Table E-4 in the 802.11 Specification
73 */
74int getOpClassForChannel(int channel, int band, bool support11n, bool support11ac) {
75 // 2GHz Band
76 if ((band & IHostapd::BandMask::BAND_2_GHZ) != 0) {
77 if (channel == 14) {
78 return 82;
79 }
80 if (channel >= 1 && channel <= 13) {
81 if (!support11n) {
82 //20MHz channel
83 return 81;
84 }
85 if (channel <= 9) {
86 // HT40 with secondary channel above primary
87 return 83;
88 }
89 // HT40 with secondary channel below primary
90 return 84;
91 }
92 // Error
93 return 0;
94 }
95
96 // 5GHz Band
97 if ((band & IHostapd::BandMask::BAND_5_GHZ) != 0) {
98 if (support11ac) {
99 switch (channel) {
100 case 42:
101 case 58:
102 case 106:
103 case 122:
104 case 138:
105 case 155:
106 // 80MHz channel
107 return 128;
108 case 50:
109 case 114:
110 // 160MHz channel
111 return 129;
112 }
113 }
114
115 if (!support11n) {
116 if (channel >= 36 && channel <= 48) {
117 return 115;
118 }
119 if (channel >= 52 && channel <= 64) {
120 return 118;
121 }
122 if (channel >= 100 && channel <= 144) {
123 return 121;
124 }
125 if (channel >= 149 && channel <= 161) {
126 return 124;
127 }
128 if (channel >= 165 && channel <= 169) {
129 return 125;
130 }
131 } else {
132 switch (channel) {
133 case 36:
134 case 44:
135 // HT40 with secondary channel above primary
136 return 116;
137 case 40:
138 case 48:
139 // HT40 with secondary channel below primary
140 return 117;
141 case 52:
142 case 60:
143 // HT40 with secondary channel above primary
144 return 119;
145 case 56:
146 case 64:
147 // HT40 with secondary channel below primary
148 return 120;
149 case 100:
150 case 108:
151 case 116:
152 case 124:
153 case 132:
154 case 140:
155 // HT40 with secondary channel above primary
156 return 122;
157 case 104:
158 case 112:
159 case 120:
160 case 128:
161 case 136:
162 case 144:
163 // HT40 with secondary channel below primary
164 return 123;
165 case 149:
166 case 157:
167 // HT40 with secondary channel above primary
168 return 126;
169 case 153:
170 case 161:
171 // HT40 with secondary channel below primary
172 return 127;
173 }
174 }
175 // Error
176 return 0;
177 }
178
179 // 6GHz Band
180 if ((band & IHostapd::BandMask::BAND_6_GHZ) != 0) {
181 // Channels 1, 5. 9, 13, ...
182 if ((channel & 0x03) == 0x01) {
183 // 20MHz channel
184 return 131;
185 }
186 // Channels 3, 11, 19, 27, ...
187 if ((channel & 0x07) == 0x03) {
188 // 40MHz channel
189 return 132;
190 }
191 // Channels 7, 23, 39, 55, ...
192 if ((channel & 0x0F) == 0x07) {
193 // 80MHz channel
194 return 133;
195 }
196 // Channels 15, 47, 69, ...
197 if ((channel & 0x1F) == 0x0F) {
198 // 160MHz channel
199 return 134;
200 }
Kai Shia0cfc142020-09-16 20:05:13 -0700201 if (channel == 2) {
202 // 20MHz channel
203 return 136;
204 }
Ahmed ElArabawy7b4b40a2020-01-02 08:55:38 -0800205 // Error
206 return 0;
207 }
208
209 return 0;
210}
211
lesldd48efd2019-12-26 15:13:51 +0800212bool validatePassphrase(int passphrase_len, int min_len, int max_len)
213{
214 if (min_len != -1 && passphrase_len < min_len) return false;
215 if (max_len != -1 && passphrase_len > max_len) return false;
216 return true;
217}
218
Roshan Pius30b452e2017-12-27 13:36:21 -0800219std::string CreateHostapdConfig(
220 const IHostapd::IfaceParams& iface_params,
221 const IHostapd::NetworkParams& nw_params)
222{
leslb0f4d542020-09-16 23:06:03 +0800223 if (nw_params.V1_2.V1_0.ssid.size() >
Roshan Pius30b452e2017-12-27 13:36:21 -0800224 static_cast<uint32_t>(
225 IHostapd::ParamSizeLimits::SSID_MAX_LEN_IN_BYTES)) {
226 wpa_printf(
leslb0f4d542020-09-16 23:06:03 +0800227 MSG_ERROR, "Invalid SSID size: %zu", nw_params.V1_2.V1_0.ssid.size());
Roshan Pius30b452e2017-12-27 13:36:21 -0800228 return "";
229 }
230
231 // SSID string
232 std::stringstream ss;
233 ss << std::hex;
234 ss << std::setfill('0');
leslb0f4d542020-09-16 23:06:03 +0800235 for (uint8_t b : nw_params.V1_2.V1_0.ssid) {
Roshan Pius30b452e2017-12-27 13:36:21 -0800236 ss << std::setw(2) << static_cast<unsigned int>(b);
237 }
238 const std::string ssid_as_string = ss.str();
239
240 // Encryption config string
Kai Shi35d8ea42020-10-07 15:24:01 -0700241 uint32_t band = 0;
242 band |= iface_params.channelParams.bandMask;
243 bool is_6Ghz_band_only = band == static_cast<uint32_t>(IHostapd::BandMask::BAND_6_GHZ);
Roshan Pius30b452e2017-12-27 13:36:21 -0800244 std::string encryption_config_as_string;
leslb0f4d542020-09-16 23:06:03 +0800245 switch (nw_params.V1_2.encryptionType) {
Roshan Pius30b452e2017-12-27 13:36:21 -0800246 case IHostapd::EncryptionType::NONE:
247 // no security params
248 break;
249 case IHostapd::EncryptionType::WPA:
lesldd48efd2019-12-26 15:13:51 +0800250 if (!validatePassphrase(
leslb0f4d542020-09-16 23:06:03 +0800251 nw_params.V1_2.passphrase.size(),
lesldd48efd2019-12-26 15:13:51 +0800252 static_cast<uint32_t>(IHostapd::ParamSizeLimits::
253 WPA2_PSK_PASSPHRASE_MIN_LEN_IN_BYTES),
254 static_cast<uint32_t>(IHostapd::ParamSizeLimits::
255 WPA2_PSK_PASSPHRASE_MAX_LEN_IN_BYTES))) {
256 return "";
257 }
Roshan Pius30b452e2017-12-27 13:36:21 -0800258 encryption_config_as_string = StringPrintf(
259 "wpa=3\n"
260 "wpa_pairwise=TKIP CCMP\n"
261 "wpa_passphrase=%s",
leslb0f4d542020-09-16 23:06:03 +0800262 nw_params.V1_2.passphrase.c_str());
Roshan Pius30b452e2017-12-27 13:36:21 -0800263 break;
264 case IHostapd::EncryptionType::WPA2:
lesldd48efd2019-12-26 15:13:51 +0800265 if (!validatePassphrase(
leslb0f4d542020-09-16 23:06:03 +0800266 nw_params.V1_2.passphrase.size(),
lesldd48efd2019-12-26 15:13:51 +0800267 static_cast<uint32_t>(IHostapd::ParamSizeLimits::
268 WPA2_PSK_PASSPHRASE_MIN_LEN_IN_BYTES),
269 static_cast<uint32_t>(IHostapd::ParamSizeLimits::
270 WPA2_PSK_PASSPHRASE_MAX_LEN_IN_BYTES))) {
271 return "";
272 }
Roshan Pius30b452e2017-12-27 13:36:21 -0800273 encryption_config_as_string = StringPrintf(
274 "wpa=2\n"
275 "rsn_pairwise=CCMP\n"
276 "wpa_passphrase=%s",
leslb0f4d542020-09-16 23:06:03 +0800277 nw_params.V1_2.passphrase.c_str());
lesldd48efd2019-12-26 15:13:51 +0800278 break;
279 case IHostapd::EncryptionType::WPA3_SAE_TRANSITION:
280 if (!validatePassphrase(
leslb0f4d542020-09-16 23:06:03 +0800281 nw_params.V1_2.passphrase.size(),
lesldd48efd2019-12-26 15:13:51 +0800282 static_cast<uint32_t>(IHostapd::ParamSizeLimits::
283 WPA2_PSK_PASSPHRASE_MIN_LEN_IN_BYTES),
284 static_cast<uint32_t>(IHostapd::ParamSizeLimits::
285 WPA2_PSK_PASSPHRASE_MAX_LEN_IN_BYTES))) {
286 return "";
287 }
288 encryption_config_as_string = StringPrintf(
289 "wpa=2\n"
290 "rsn_pairwise=CCMP\n"
291 "wpa_key_mgmt=WPA-PSK SAE\n"
292 "ieee80211w=1\n"
293 "sae_require_mfp=1\n"
294 "wpa_passphrase=%s\n"
295 "sae_password=%s",
leslb0f4d542020-09-16 23:06:03 +0800296 nw_params.V1_2.passphrase.c_str(),
297 nw_params.V1_2.passphrase.c_str());
lesldd48efd2019-12-26 15:13:51 +0800298 break;
299 case IHostapd::EncryptionType::WPA3_SAE:
leslb0f4d542020-09-16 23:06:03 +0800300 if (!validatePassphrase(nw_params.V1_2.passphrase.size(), 1, -1)) {
lesldd48efd2019-12-26 15:13:51 +0800301 return "";
302 }
303 encryption_config_as_string = StringPrintf(
304 "wpa=2\n"
305 "rsn_pairwise=CCMP\n"
306 "wpa_key_mgmt=SAE\n"
307 "ieee80211w=2\n"
308 "sae_require_mfp=2\n"
Kai Shi35d8ea42020-10-07 15:24:01 -0700309 "sae_pwe=%d\n"
lesldd48efd2019-12-26 15:13:51 +0800310 "sae_password=%s",
Kai Shi35d8ea42020-10-07 15:24:01 -0700311 is_6Ghz_band_only ? 1 : 2,
leslb0f4d542020-09-16 23:06:03 +0800312 nw_params.V1_2.passphrase.c_str());
Roshan Pius30b452e2017-12-27 13:36:21 -0800313 break;
314 default:
315 wpa_printf(MSG_ERROR, "Unknown encryption type");
316 return "";
317 }
318
319 std::string channel_config_as_string;
Ahmed ElArabawya390f292019-12-21 13:07:02 -0800320 bool isFirst = true;
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800321 if (iface_params.V1_1.V1_0.channelParams.enableAcs) {
Ahmed ElArabawya390f292019-12-21 13:07:02 -0800322 std::string freqList_as_string;
Daisuke Niwac1bd20f2019-01-09 13:51:02 +0900323 for (const auto &range :
Ahmed ElArabawya390f292019-12-21 13:07:02 -0800324 iface_params.channelParams.acsChannelFreqRangesMhz) {
325 if (!isFirst) {
326 freqList_as_string += ",";
327 }
328 isFirst = false;
329
Daisuke Niwac1bd20f2019-01-09 13:51:02 +0900330 if (range.start != range.end) {
Ahmed ElArabawya390f292019-12-21 13:07:02 -0800331 freqList_as_string +=
332 StringPrintf("%d-%d", range.start, range.end);
Daisuke Niwac1bd20f2019-01-09 13:51:02 +0900333 } else {
Ahmed ElArabawya390f292019-12-21 13:07:02 -0800334 freqList_as_string += StringPrintf("%d", range.start);
Daisuke Niwac1bd20f2019-01-09 13:51:02 +0900335 }
336 }
Roshan Pius30b452e2017-12-27 13:36:21 -0800337 channel_config_as_string = StringPrintf(
338 "channel=0\n"
Daisuke Niwac1bd20f2019-01-09 13:51:02 +0900339 "acs_exclude_dfs=%d\n"
Ahmed ElArabawya390f292019-12-21 13:07:02 -0800340 "freqlist=%s",
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800341 iface_params.V1_1.V1_0.channelParams.acsShouldExcludeDfs,
Ahmed ElArabawya390f292019-12-21 13:07:02 -0800342 freqList_as_string.c_str());
Roshan Pius30b452e2017-12-27 13:36:21 -0800343 } else {
Ahmed ElArabawy7b4b40a2020-01-02 08:55:38 -0800344 int op_class = getOpClassForChannel(
345 iface_params.V1_1.V1_0.channelParams.channel,
346 band,
347 iface_params.V1_1.V1_0.hwModeParams.enable80211N,
348 iface_params.V1_1.V1_0.hwModeParams.enable80211AC);
Roshan Pius30b452e2017-12-27 13:36:21 -0800349 channel_config_as_string = StringPrintf(
Ahmed ElArabawy7b4b40a2020-01-02 08:55:38 -0800350 "channel=%d\n"
351 "op_class=%d",
352 iface_params.V1_1.V1_0.channelParams.channel, op_class);
Roshan Pius30b452e2017-12-27 13:36:21 -0800353 }
354
Roshan Pius30b452e2017-12-27 13:36:21 -0800355 std::string hw_mode_as_string;
Roshan Pius49446472018-03-07 10:23:46 -0800356 std::string ht_cap_vht_oper_chwidth_as_string;
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800357
358 if ((band & IHostapd::BandMask::BAND_2_GHZ) != 0) {
Ahmed ElArabawya390f292019-12-21 13:07:02 -0800359 if (((band & IHostapd::BandMask::BAND_5_GHZ) != 0)
360 || ((band & IHostapd::BandMask::BAND_6_GHZ) != 0)) {
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800361 hw_mode_as_string = "hw_mode=any";
362 if (iface_params.V1_1.V1_0.channelParams.enableAcs) {
363 ht_cap_vht_oper_chwidth_as_string =
364 "ht_capab=[HT40+]\n"
365 "vht_oper_chwidth=1";
366 }
367 } else {
368 hw_mode_as_string = "hw_mode=g";
Roshan Pius49446472018-03-07 10:23:46 -0800369 }
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800370 } else {
Ahmed ElArabawya390f292019-12-21 13:07:02 -0800371 if (((band & IHostapd::BandMask::BAND_5_GHZ) != 0)
372 || ((band & IHostapd::BandMask::BAND_6_GHZ) != 0)) {
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800373 hw_mode_as_string = "hw_mode=a";
374 if (iface_params.V1_1.V1_0.channelParams.enableAcs) {
375 ht_cap_vht_oper_chwidth_as_string =
376 "ht_capab=[HT40+]\n"
377 "vht_oper_chwidth=1";
378 }
379 } else {
380 wpa_printf(MSG_ERROR, "Invalid band");
381 return "";
Roshan Pius49446472018-03-07 10:23:46 -0800382 }
Roshan Pius30b452e2017-12-27 13:36:21 -0800383 }
384
Ahmed ElArabawy5362fb12020-01-02 15:01:31 -0800385 std::string he_params_as_string;
Roshan Pius36510302020-05-09 21:01:01 -0700386#ifdef CONFIG_IEEE80211AX
Ahmed ElArabawy5362fb12020-01-02 15:01:31 -0800387 if (iface_params.hwModeParams.enable80211AX) {
388 he_params_as_string = StringPrintf(
389 "ieee80211ax=1\n"
390 "he_su_beamformer=%d\n"
391 "he_su_beamformee=%d\n"
392 "he_mu_beamformer=%d\n"
Ahmed ElArabawy5362fb12020-01-02 15:01:31 -0800393 "he_twt_required=%d\n",
394 iface_params.hwModeParams.enableHeSingleUserBeamformer ? 1 : 0,
395 iface_params.hwModeParams.enableHeSingleUserBeamformee ? 1 : 0,
396 iface_params.hwModeParams.enableHeMultiUserBeamformer ? 1 : 0,
Ahmed ElArabawy5362fb12020-01-02 15:01:31 -0800397 iface_params.hwModeParams.enableHeTargetWakeTime ? 1 : 0);
398 } else {
399 he_params_as_string = "ieee80211ax=0";
400 }
Roshan Pius36510302020-05-09 21:01:01 -0700401#endif /* CONFIG_IEEE80211AX */
Ahmed ElArabawy5362fb12020-01-02 15:01:31 -0800402
leslb0f4d542020-09-16 23:06:03 +0800403#ifdef CONFIG_INTERWORKING
404 std::string access_network_params_as_string;
405 if (nw_params.isMetered) {
406 access_network_params_as_string = StringPrintf(
407 "interworking=1\n"
408 "access_network_type=2\n"); // CHARGEABLE_PUBLIC_NETWORK
409 } else {
410 access_network_params_as_string = StringPrintf(
411 "interworking=0\n");
412 }
413#endif /* CONFIG_INTERWORKING */
414
Roshan Pius30b452e2017-12-27 13:36:21 -0800415 return StringPrintf(
416 "interface=%s\n"
417 "driver=nl80211\n"
Daichi Ueuracaa74a82018-02-14 22:25:39 +0900418 "ctrl_interface=/data/vendor/wifi/hostapd/ctrl\n"
Roshan Pius30b452e2017-12-27 13:36:21 -0800419 // ssid2 signals to hostapd that the value is not a literal value
420 // for use as a SSID. In this case, we're giving it a hex
421 // std::string and hostapd needs to expect that.
422 "ssid2=%s\n"
423 "%s\n"
424 "ieee80211n=%d\n"
425 "ieee80211ac=%d\n"
Ahmed ElArabawy5362fb12020-01-02 15:01:31 -0800426 "%s\n"
Roshan Piuse453f712018-02-09 15:49:57 -0800427 "%s\n"
Roshan Pius49446472018-03-07 10:23:46 -0800428 "%s\n"
Roshan Pius30b452e2017-12-27 13:36:21 -0800429 "ignore_broadcast_ssid=%d\n"
430 "wowlan_triggers=any\n"
leslb0f4d542020-09-16 23:06:03 +0800431#ifdef CONFIG_INTERWORKING
432 "%s\n"
433#endif /* CONFIG_INTERWORKING */
Roshan Pius30b452e2017-12-27 13:36:21 -0800434 "%s\n",
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800435 iface_params.V1_1.V1_0.ifaceName.c_str(), ssid_as_string.c_str(),
Roshan Pius30b452e2017-12-27 13:36:21 -0800436 channel_config_as_string.c_str(),
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800437 iface_params.V1_1.V1_0.hwModeParams.enable80211N ? 1 : 0,
438 iface_params.V1_1.V1_0.hwModeParams.enable80211AC ? 1 : 0,
Ahmed ElArabawy5362fb12020-01-02 15:01:31 -0800439 he_params_as_string.c_str(),
Roshan Pius49446472018-03-07 10:23:46 -0800440 hw_mode_as_string.c_str(), ht_cap_vht_oper_chwidth_as_string.c_str(),
leslb0f4d542020-09-16 23:06:03 +0800441 nw_params.V1_2.V1_0.isHidden ? 1 : 0,
442#ifdef CONFIG_INTERWORKING
443 access_network_params_as_string.c_str(),
444#endif /* CONFIG_INTERWORKING */
445 encryption_config_as_string.c_str());
Roshan Pius30b452e2017-12-27 13:36:21 -0800446}
Roshan Piuse2d21012018-08-17 11:22:06 -0700447
lesldab91502020-08-17 18:29:47 +0800448Generation getGeneration(hostapd_hw_modes *current_mode)
449{
450 wpa_printf(MSG_DEBUG, "getGeneration hwmode=%d, ht_enabled=%d, vht_enabled=%d",
451 current_mode->mode, current_mode->ht_capab != 0,
452 current_mode->vht_capab != 0);
453 switch (current_mode->mode) {
454 case HOSTAPD_MODE_IEEE80211B:
455 return Generation::WIFI_STANDARD_LEGACY;
456 case HOSTAPD_MODE_IEEE80211G:
457 return current_mode->ht_capab == 0 ?
458 Generation::WIFI_STANDARD_LEGACY : Generation::WIFI_STANDARD_11N;
459 case HOSTAPD_MODE_IEEE80211A:
460 return current_mode->vht_capab == 0 ?
461 Generation::WIFI_STANDARD_11N : Generation::WIFI_STANDARD_11AC;
462 // TODO: b/162484222 miss HOSTAPD_MODE_IEEE80211AX definition.
463 default:
464 return Generation::WIFI_STANDARD_UNKNOWN;
465 }
466}
467
468Bandwidth getBandwidth(struct hostapd_config *iconf)
469{
470 wpa_printf(MSG_DEBUG, "getBandwidth %d, isHT=%d, isHT40=%d",
471 iconf->vht_oper_chwidth, iconf->ieee80211n,
472 iconf->secondary_channel);
473 switch (iconf->vht_oper_chwidth) {
474 case CHANWIDTH_80MHZ:
475 return Bandwidth::WIFI_BANDWIDTH_80;
476 case CHANWIDTH_80P80MHZ:
477 return Bandwidth::WIFI_BANDWIDTH_80P80;
478 break;
479 case CHANWIDTH_160MHZ:
480 return Bandwidth::WIFI_BANDWIDTH_160;
481 break;
482 case CHANWIDTH_USE_HT:
483 if (iconf->ieee80211n) {
484 return iconf->secondary_channel != 0 ?
485 Bandwidth::WIFI_BANDWIDTH_40 : Bandwidth::WIFI_BANDWIDTH_20;
486 }
487 return Bandwidth::WIFI_BANDWIDTH_20_NOHT;
488 default:
489 return Bandwidth::WIFI_BANDWIDTH_INVALID;
490 }
491}
492
Roshan Piuse2d21012018-08-17 11:22:06 -0700493// hostapd core functions accept "C" style function pointers, so use global
494// functions to pass to the hostapd core function and store the corresponding
495// std::function methods to be invoked.
496//
497// NOTE: Using the pattern from the vendor HAL (wifi_legacy_hal.cpp).
498//
499// Callback to be invoked once setup is complete
500std::function<void(struct hostapd_data*)> on_setup_complete_internal_callback;
501void onAsyncSetupCompleteCb(void* ctx)
502{
503 struct hostapd_data* iface_hapd = (struct hostapd_data*)ctx;
504 if (on_setup_complete_internal_callback) {
505 on_setup_complete_internal_callback(iface_hapd);
506 // Invalidate this callback since we don't want this firing
507 // again.
508 on_setup_complete_internal_callback = nullptr;
509 }
510}
lesld0621052020-08-10 13:54:28 +0800511
512// Callback to be invoked on hotspot client connection/disconnection
513std::function<void(struct hostapd_data*, const u8 *mac_addr, int authorized,
514 const u8 *p2p_dev_addr)> on_sta_authorized_internal_callback;
515void onAsyncStaAuthorizedCb(void* ctx, const u8 *mac_addr, int authorized,
516 const u8 *p2p_dev_addr)
517{
518 struct hostapd_data* iface_hapd = (struct hostapd_data*)ctx;
519 if (on_sta_authorized_internal_callback) {
520 on_sta_authorized_internal_callback(iface_hapd, mac_addr,
521 authorized, p2p_dev_addr);
522 }
523}
524
lesldab91502020-08-17 18:29:47 +0800525std::function<void(struct hostapd_data*, int level,
526 enum wpa_msg_type type, const char *txt,
527 size_t len)> on_wpa_msg_internal_callback;
528
529void onAsyncWpaEventCb(void *ctx, int level,
530 enum wpa_msg_type type, const char *txt,
531 size_t len)
532{
533 struct hostapd_data* iface_hapd = (struct hostapd_data*)ctx;
534 if (on_wpa_msg_internal_callback) {
535 on_wpa_msg_internal_callback(iface_hapd, level,
536 type, txt, len);
537 }
538}
539
540
Roshan Pius30b452e2017-12-27 13:36:21 -0800541} // namespace
542
Roshan Piuscc817562017-12-22 14:45:05 -0800543namespace android {
544namespace hardware {
545namespace wifi {
546namespace hostapd {
leslf8b1b402020-08-04 17:09:39 +0800547namespace V1_3 {
Roshan Piuscc817562017-12-22 14:45:05 -0800548namespace implementation {
549using hidl_return_util::call;
Roshan Piuse2d21012018-08-17 11:22:06 -0700550using namespace android::hardware::wifi::hostapd::V1_0;
Roshan Piuscc817562017-12-22 14:45:05 -0800551
Roshan Pius80e5a0f2020-10-19 13:48:22 -0700552Hostapd::Hostapd(struct hapd_interfaces* interfaces)
553 : interfaces_(interfaces), death_notifier_(sp<DeathNotifier>::make())
Roshan Piuscc817562017-12-22 14:45:05 -0800554{}
555
556Return<void> Hostapd::addAccessPoint(
Daisuke Niwac1bd20f2019-01-09 13:51:02 +0900557 const V1_0::IHostapd::IfaceParams& iface_params,
lesldd48efd2019-12-26 15:13:51 +0800558 const V1_0::IHostapd::NetworkParams& nw_params, addAccessPoint_cb _hidl_cb)
Daisuke Niwac1bd20f2019-01-09 13:51:02 +0900559{
560 return call(
561 this, &Hostapd::addAccessPointInternal, _hidl_cb, iface_params,
562 nw_params);
563}
564
565Return<void> Hostapd::addAccessPoint_1_1(
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800566 const V1_1::IHostapd::IfaceParams& iface_params,
lesldd48efd2019-12-26 15:13:51 +0800567 const V1_0::IHostapd::NetworkParams& nw_params, addAccessPoint_cb _hidl_cb)
Roshan Piuscc817562017-12-22 14:45:05 -0800568{
569 return call(
Daisuke Niwac1bd20f2019-01-09 13:51:02 +0900570 this, &Hostapd::addAccessPointInternal_1_1, _hidl_cb, iface_params,
Roshan Piuscc817562017-12-22 14:45:05 -0800571 nw_params);
572}
573
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800574Return<void> Hostapd::addAccessPoint_1_2(
leslb0f4d542020-09-16 23:06:03 +0800575 const V1_2::IHostapd::IfaceParams& iface_params,
576 const V1_2::IHostapd::NetworkParams& nw_params,
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800577 addAccessPoint_1_2_cb _hidl_cb)
578{
579 return call(
580 this, &Hostapd::addAccessPointInternal_1_2, _hidl_cb, iface_params,
581 nw_params);
582}
583
leslb0f4d542020-09-16 23:06:03 +0800584Return<void> Hostapd::addAccessPoint_1_3(
585 const V1_3::IHostapd::IfaceParams& iface_params,
586 const V1_3::IHostapd::NetworkParams& nw_params,
587 addAccessPoint_1_3_cb _hidl_cb)
588{
589 return call(
590 this, &Hostapd::addAccessPointInternal_1_3, _hidl_cb, iface_params,
591 nw_params);
592}
593
Roshan Piuscc817562017-12-22 14:45:05 -0800594Return<void> Hostapd::removeAccessPoint(
595 const hidl_string& iface_name, removeAccessPoint_cb _hidl_cb)
596{
597 return call(
598 this, &Hostapd::removeAccessPointInternal, _hidl_cb, iface_name);
599}
600
Roshan Piuse2d21012018-08-17 11:22:06 -0700601Return<void> Hostapd::terminate()
602{
Roshan Pius3455af42018-02-01 09:19:49 -0800603 wpa_printf(MSG_INFO, "Terminating...");
604 eloop_terminate();
605 return Void();
606}
607
Roshan Piuse2d21012018-08-17 11:22:06 -0700608Return<void> Hostapd::registerCallback(
lesl1a842e62019-12-02 19:00:37 +0800609 const sp<V1_1::IHostapdCallback>& callback, registerCallback_cb _hidl_cb)
Roshan Piuse2d21012018-08-17 11:22:06 -0700610{
611 return call(
612 this, &Hostapd::registerCallbackInternal, _hidl_cb, callback);
613}
614
leslf8b1b402020-08-04 17:09:39 +0800615Return<void> Hostapd::registerCallback_1_3(
616 const sp<V1_3::IHostapdCallback>& callback, registerCallback_1_3_cb _hidl_cb)
617{
618 return call(
619 this, &Hostapd::registerCallbackInternal_1_3, _hidl_cb, callback);
620}
621
lesl1a842e62019-12-02 19:00:37 +0800622Return<void> Hostapd::forceClientDisconnect(
623 const hidl_string& iface_name, const hidl_array<uint8_t, 6>& client_address,
624 V1_2::Ieee80211ReasonCode reason_code, forceClientDisconnect_cb _hidl_cb)
625{
626 return call(
627 this, &Hostapd::forceClientDisconnectInternal, _hidl_cb, iface_name,
628 client_address, reason_code);
629}
630
Roger Wangcede5062019-12-30 12:56:28 +0800631Return<void> Hostapd::setDebugParams(
leslf8b1b402020-08-04 17:09:39 +0800632 V1_2::DebugLevel level, setDebugParams_cb _hidl_cb)
Roger Wangcede5062019-12-30 12:56:28 +0800633{
634 return call(
635 this, &Hostapd::setDebugParamsInternal, _hidl_cb, level);
636}
lesl1a842e62019-12-02 19:00:37 +0800637
638V1_0::HostapdStatus Hostapd::addAccessPointInternal(
Daisuke Niwac1bd20f2019-01-09 13:51:02 +0900639 const V1_0::IHostapd::IfaceParams& iface_params,
lesldd48efd2019-12-26 15:13:51 +0800640 const V1_0::IHostapd::NetworkParams& nw_params)
Daisuke Niwac1bd20f2019-01-09 13:51:02 +0900641{
lesl1a842e62019-12-02 19:00:37 +0800642 return {V1_0::HostapdStatusCode::FAILURE_UNKNOWN, ""};
Daisuke Niwac1bd20f2019-01-09 13:51:02 +0900643}
644
lesl1a842e62019-12-02 19:00:37 +0800645V1_0::HostapdStatus Hostapd::addAccessPointInternal_1_1(
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800646 const V1_1::IHostapd::IfaceParams& iface_params,
lesldd48efd2019-12-26 15:13:51 +0800647 const V1_1::IHostapd::NetworkParams& nw_params)
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800648{
649 return {V1_0::HostapdStatusCode::FAILURE_UNKNOWN, ""};
650}
651
leslf8b1b402020-08-04 17:09:39 +0800652V1_2::HostapdStatus Hostapd::addAccessPointInternal_1_2(
leslb0f4d542020-09-16 23:06:03 +0800653 const V1_2::IHostapd::IfaceParams& iface_params,
654 const V1_2::IHostapd::NetworkParams& nw_params) {
655 return {V1_2::HostapdStatusCode::FAILURE_UNKNOWN, ""};
656}
657
658V1_2::HostapdStatus Hostapd::addAccessPointInternal_1_3(
659 const V1_2::IHostapd::IfaceParams& iface_params,
660 const V1_3::IHostapd::NetworkParams& nw_params)
Roshan Piuscc817562017-12-22 14:45:05 -0800661{
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800662 if (hostapd_get_iface(interfaces_, iface_params.V1_1.V1_0.ifaceName.c_str())) {
Roshan Pius087d8692017-12-27 14:11:44 -0800663 wpa_printf(
664 MSG_ERROR, "Interface %s already present",
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800665 iface_params.V1_1.V1_0.ifaceName.c_str());
leslf8b1b402020-08-04 17:09:39 +0800666 return {V1_2::HostapdStatusCode::FAILURE_IFACE_EXISTS, ""};
Roshan Pius087d8692017-12-27 14:11:44 -0800667 }
668 const auto conf_params = CreateHostapdConfig(iface_params, nw_params);
669 if (conf_params.empty()) {
670 wpa_printf(MSG_ERROR, "Failed to create config params");
leslf8b1b402020-08-04 17:09:39 +0800671 return {V1_2::HostapdStatusCode::FAILURE_ARGS_INVALID, ""};
Roshan Pius087d8692017-12-27 14:11:44 -0800672 }
673 const auto conf_file_path =
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800674 WriteHostapdConfig(iface_params.V1_1.V1_0.ifaceName, conf_params);
Roshan Pius087d8692017-12-27 14:11:44 -0800675 if (conf_file_path.empty()) {
676 wpa_printf(MSG_ERROR, "Failed to write config file");
leslf8b1b402020-08-04 17:09:39 +0800677 return {V1_2::HostapdStatusCode::FAILURE_UNKNOWN, ""};
Roshan Pius087d8692017-12-27 14:11:44 -0800678 }
679 std::string add_iface_param_str = StringPrintf(
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800680 "%s config=%s", iface_params.V1_1.V1_0.ifaceName.c_str(),
Roshan Pius087d8692017-12-27 14:11:44 -0800681 conf_file_path.c_str());
682 std::vector<char> add_iface_param_vec(
683 add_iface_param_str.begin(), add_iface_param_str.end() + 1);
684 if (hostapd_add_iface(interfaces_, add_iface_param_vec.data()) < 0) {
685 wpa_printf(
686 MSG_ERROR, "Adding interface %s failed",
687 add_iface_param_str.c_str());
leslf8b1b402020-08-04 17:09:39 +0800688 return {V1_2::HostapdStatusCode::FAILURE_UNKNOWN, ""};
Roshan Pius087d8692017-12-27 14:11:44 -0800689 }
690 struct hostapd_data* iface_hapd =
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800691 hostapd_get_iface(interfaces_, iface_params.V1_1.V1_0.ifaceName.c_str());
Roshan Pius087d8692017-12-27 14:11:44 -0800692 WPA_ASSERT(iface_hapd != nullptr && iface_hapd->iface != nullptr);
Roshan Piuse2d21012018-08-17 11:22:06 -0700693 // Register the setup complete callbacks
694 on_setup_complete_internal_callback =
695 [this](struct hostapd_data* iface_hapd) {
696 wpa_printf(
697 MSG_DEBUG, "AP interface setup completed - state %s",
698 hostapd_state_text(iface_hapd->iface->state));
699 if (iface_hapd->iface->state == HAPD_IFACE_DISABLED) {
700 // Invoke the failure callback on all registered
701 // clients.
702 for (const auto& callback : callbacks_) {
703 callback->onFailure(
704 iface_hapd->conf->iface);
705 }
706 }
707 };
lesld0621052020-08-10 13:54:28 +0800708
709 // Rgegister for new client connect/disconnect indication.
710 on_sta_authorized_internal_callback =
711 [this](struct hostapd_data* iface_hapd, const u8 *mac_addr,
712 int authorized, const u8 *p2p_dev_addr) {
713 wpa_printf(MSG_DEBUG, "notify client " MACSTR " %s",
714 MAC2STR(mac_addr),
715 (authorized) ? "Connected" : "Disconnected");
716
717 for (const auto &callback : callbacks_) {
718 // TODO: The iface need to separate to iface and ap instance
719 // identify for AP+AP case.
720 callback->onConnectedClientsChanged(iface_hapd->conf->iface,
721 iface_hapd->conf->iface, mac_addr, authorized);
722 }
723 };
724
lesldab91502020-08-17 18:29:47 +0800725 // Register for wpa_event which used to get channel switch event
726 on_wpa_msg_internal_callback =
727 [this](struct hostapd_data* iface_hapd, int level,
728 enum wpa_msg_type type, const char *txt,
729 size_t len) {
730 wpa_printf(MSG_DEBUG, "Receive wpa msg : %s", txt);
731 if (os_strncmp(txt, WPA_EVENT_CHANNEL_SWITCH,
732 strlen(WPA_EVENT_CHANNEL_SWITCH)) == 0) {
733 for (const auto &callback : callbacks_) {
734 callback->onApInstanceInfoChanged(
735 iface_hapd->conf->iface, iface_hapd->conf->iface,
736 iface_hapd->iface->freq, getBandwidth(iface_hapd->iconf),
lesl29973f82020-09-10 22:40:06 +0800737 getGeneration(iface_hapd->iface->current_mode),
738 iface_hapd->own_addr);
lesldab91502020-08-17 18:29:47 +0800739 }
740 }
741 };
lesld0621052020-08-10 13:54:28 +0800742
743 // Setup callback
Roshan Piuse2d21012018-08-17 11:22:06 -0700744 iface_hapd->setup_complete_cb = onAsyncSetupCompleteCb;
745 iface_hapd->setup_complete_cb_ctx = iface_hapd;
lesld0621052020-08-10 13:54:28 +0800746 iface_hapd->sta_authorized_cb = onAsyncStaAuthorizedCb;
747 iface_hapd->sta_authorized_cb_ctx = iface_hapd;
lesldab91502020-08-17 18:29:47 +0800748 wpa_msg_register_cb(onAsyncWpaEventCb);
lesld0621052020-08-10 13:54:28 +0800749
Roshan Pius087d8692017-12-27 14:11:44 -0800750 if (hostapd_enable_iface(iface_hapd->iface) < 0) {
751 wpa_printf(
752 MSG_ERROR, "Enabling interface %s failed",
Ahmed ElArabawy34982672019-12-06 10:10:17 -0800753 iface_params.V1_1.V1_0.ifaceName.c_str());
leslf8b1b402020-08-04 17:09:39 +0800754 return {V1_2::HostapdStatusCode::FAILURE_UNKNOWN, ""};
Roshan Pius087d8692017-12-27 14:11:44 -0800755 }
leslf8b1b402020-08-04 17:09:39 +0800756 return {V1_2::HostapdStatusCode::SUCCESS, ""};
Roshan Piuscc817562017-12-22 14:45:05 -0800757}
758
lesl1a842e62019-12-02 19:00:37 +0800759V1_0::HostapdStatus Hostapd::removeAccessPointInternal(const std::string& iface_name)
Roshan Piuscc817562017-12-22 14:45:05 -0800760{
Roshan Pius087d8692017-12-27 14:11:44 -0800761 std::vector<char> remove_iface_param_vec(
762 iface_name.begin(), iface_name.end() + 1);
763 if (hostapd_remove_iface(interfaces_, remove_iface_param_vec.data()) <
764 0) {
765 wpa_printf(
766 MSG_ERROR, "Removing interface %s failed",
767 iface_name.c_str());
lesl1a842e62019-12-02 19:00:37 +0800768 return {V1_0::HostapdStatusCode::FAILURE_UNKNOWN, ""};
Roshan Pius087d8692017-12-27 14:11:44 -0800769 }
lesl1a842e62019-12-02 19:00:37 +0800770 return {V1_0::HostapdStatusCode::SUCCESS, ""};
Roshan Piuscc817562017-12-22 14:45:05 -0800771}
Roshan Piuse2d21012018-08-17 11:22:06 -0700772
lesl1a842e62019-12-02 19:00:37 +0800773V1_0::HostapdStatus Hostapd::registerCallbackInternal(
774 const sp<V1_1::IHostapdCallback>& callback)
Roshan Piuse2d21012018-08-17 11:22:06 -0700775{
leslf8b1b402020-08-04 17:09:39 +0800776 return {V1_0::HostapdStatusCode::FAILURE_UNKNOWN, ""};
777}
778
779V1_2::HostapdStatus Hostapd::registerCallbackInternal_1_3(
780 const sp<V1_3::IHostapdCallback>& callback)
781{
Roshan Pius80e5a0f2020-10-19 13:48:22 -0700782 if (!callback->linkToDeath(death_notifier_, 0)) {
783 wpa_printf(
784 MSG_ERROR,
785 "Error registering for death notification for "
786 "hostapd callback object");
787 return {V1_2::HostapdStatusCode::FAILURE_UNKNOWN, ""};
788 }
Roshan Piuse2d21012018-08-17 11:22:06 -0700789 callbacks_.push_back(callback);
leslf8b1b402020-08-04 17:09:39 +0800790 return {V1_2::HostapdStatusCode::SUCCESS, ""};
lesl1a842e62019-12-02 19:00:37 +0800791}
792
793V1_2::HostapdStatus Hostapd::forceClientDisconnectInternal(const std::string& iface_name,
794 const std::array<uint8_t, 6>& client_address, V1_2::Ieee80211ReasonCode reason_code)
795{
796 struct hostapd_data *hapd = hostapd_get_iface(interfaces_, iface_name.c_str());
797 struct sta_info *sta;
798 if (!hapd) {
799 wpa_printf(MSG_ERROR, "Interface %s doesn't exist", iface_name.c_str());
800 return {V1_2::HostapdStatusCode::FAILURE_IFACE_UNKNOWN, ""};
801 }
802 for (sta = hapd->sta_list; sta; sta = sta->next) {
803 int res;
804 res = memcmp(sta->addr, client_address.data(), ETH_ALEN);
805 if (res == 0) {
806 wpa_printf(MSG_INFO, "Force client:" MACSTR " disconnect with reason: %d",
807 MAC2STR(client_address.data()), (uint16_t) reason_code);
808 ap_sta_disconnect(hapd, sta, sta->addr, (uint16_t) reason_code);
809 return {V1_2::HostapdStatusCode::SUCCESS, ""};
810 }
811 }
812 return {V1_2::HostapdStatusCode::FAILURE_CLIENT_UNKNOWN, ""};
Roshan Piuse2d21012018-08-17 11:22:06 -0700813}
814
leslf8b1b402020-08-04 17:09:39 +0800815V1_2::HostapdStatus Hostapd::setDebugParamsInternal(V1_2::DebugLevel level)
Roger Wangcede5062019-12-30 12:56:28 +0800816{
817 wpa_debug_level = static_cast<uint32_t>(level);
818 return {V1_2::HostapdStatusCode::SUCCESS, ""};
819}
820
Roshan Piuscc817562017-12-22 14:45:05 -0800821} // namespace implementation
leslf8b1b402020-08-04 17:09:39 +0800822} // namespace V1_3
Roshan Piuscc817562017-12-22 14:45:05 -0800823} // namespace hostapd
824} // namespace wifi
825} // namespace hardware
826} // namespace android