blob: 340b22c11b13d49c35f320d075edf52b06db88dd [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;
Roshan Piuse2d21012018-08-17 11:22:06 -070036using android::hardware::wifi::hostapd::V1_1::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
68std::string CreateHostapdConfig(
69 const IHostapd::IfaceParams& iface_params,
70 const IHostapd::NetworkParams& nw_params)
71{
72 if (nw_params.ssid.size() >
73 static_cast<uint32_t>(
74 IHostapd::ParamSizeLimits::SSID_MAX_LEN_IN_BYTES)) {
75 wpa_printf(
76 MSG_ERROR, "Invalid SSID size: %zu", nw_params.ssid.size());
77 return "";
78 }
Roshan Pius0b8a64f2018-01-23 11:42:34 -080079 if ((nw_params.encryptionType != IHostapd::EncryptionType::NONE) &&
80 (nw_params.pskPassphrase.size() <
81 static_cast<uint32_t>(
82 IHostapd::ParamSizeLimits::
83 WPA2_PSK_PASSPHRASE_MIN_LEN_IN_BYTES) ||
84 nw_params.pskPassphrase.size() >
85 static_cast<uint32_t>(
86 IHostapd::ParamSizeLimits::
87 WPA2_PSK_PASSPHRASE_MAX_LEN_IN_BYTES))) {
Roshan Pius30b452e2017-12-27 13:36:21 -080088 wpa_printf(
89 MSG_ERROR, "Invalid psk passphrase size: %zu",
90 nw_params.pskPassphrase.size());
91 return "";
92 }
93
94 // SSID string
95 std::stringstream ss;
96 ss << std::hex;
97 ss << std::setfill('0');
98 for (uint8_t b : nw_params.ssid) {
99 ss << std::setw(2) << static_cast<unsigned int>(b);
100 }
101 const std::string ssid_as_string = ss.str();
102
103 // Encryption config string
104 std::string encryption_config_as_string;
105 switch (nw_params.encryptionType) {
106 case IHostapd::EncryptionType::NONE:
107 // no security params
108 break;
109 case IHostapd::EncryptionType::WPA:
110 encryption_config_as_string = StringPrintf(
111 "wpa=3\n"
112 "wpa_pairwise=TKIP CCMP\n"
113 "wpa_passphrase=%s",
114 nw_params.pskPassphrase.c_str());
115 break;
116 case IHostapd::EncryptionType::WPA2:
117 encryption_config_as_string = StringPrintf(
118 "wpa=2\n"
119 "rsn_pairwise=CCMP\n"
120 "wpa_passphrase=%s",
121 nw_params.pskPassphrase.c_str());
122 break;
123 default:
124 wpa_printf(MSG_ERROR, "Unknown encryption type");
125 return "";
126 }
127
128 std::string channel_config_as_string;
129 if (iface_params.channelParams.enableAcs) {
130 channel_config_as_string = StringPrintf(
131 "channel=0\n"
132 "acs_exclude_dfs=%d",
133 iface_params.channelParams.acsShouldExcludeDfs);
134 } else {
135 channel_config_as_string = StringPrintf(
136 "channel=%d", iface_params.channelParams.channel);
137 }
138
139 // Hw Mode String
140 std::string hw_mode_as_string;
Roshan Pius49446472018-03-07 10:23:46 -0800141 std::string ht_cap_vht_oper_chwidth_as_string;
Roshan Pius30b452e2017-12-27 13:36:21 -0800142 switch (iface_params.channelParams.band) {
143 case IHostapd::Band::BAND_2_4_GHZ:
Roshan Piuse453f712018-02-09 15:49:57 -0800144 hw_mode_as_string = "hw_mode=g";
Roshan Pius30b452e2017-12-27 13:36:21 -0800145 break;
146 case IHostapd::Band::BAND_5_GHZ:
Roshan Pius49446472018-03-07 10:23:46 -0800147 hw_mode_as_string = "hw_mode=a";
148 if (iface_params.channelParams.enableAcs) {
149 ht_cap_vht_oper_chwidth_as_string =
150 "ht_capab=[HT40+]\n"
151 "vht_oper_chwidth=1";
152 }
Roshan Pius30b452e2017-12-27 13:36:21 -0800153 break;
154 case IHostapd::Band::BAND_ANY:
Roshan Pius49446472018-03-07 10:23:46 -0800155 hw_mode_as_string = "hw_mode=any";
156 if (iface_params.channelParams.enableAcs) {
157 ht_cap_vht_oper_chwidth_as_string =
158 "ht_capab=[HT40+]\n"
159 "vht_oper_chwidth=1";
160 }
Roshan Pius30b452e2017-12-27 13:36:21 -0800161 break;
162 default:
163 wpa_printf(MSG_ERROR, "Invalid band");
164 return "";
165 }
166
167 return StringPrintf(
168 "interface=%s\n"
169 "driver=nl80211\n"
Daichi Ueuracaa74a82018-02-14 22:25:39 +0900170 "ctrl_interface=/data/vendor/wifi/hostapd/ctrl\n"
Roshan Pius30b452e2017-12-27 13:36:21 -0800171 // ssid2 signals to hostapd that the value is not a literal value
172 // for use as a SSID. In this case, we're giving it a hex
173 // std::string and hostapd needs to expect that.
174 "ssid2=%s\n"
175 "%s\n"
176 "ieee80211n=%d\n"
177 "ieee80211ac=%d\n"
Roshan Piuse453f712018-02-09 15:49:57 -0800178 "%s\n"
Roshan Pius49446472018-03-07 10:23:46 -0800179 "%s\n"
Roshan Pius30b452e2017-12-27 13:36:21 -0800180 "ignore_broadcast_ssid=%d\n"
181 "wowlan_triggers=any\n"
182 "%s\n",
183 iface_params.ifaceName.c_str(), ssid_as_string.c_str(),
184 channel_config_as_string.c_str(),
185 iface_params.hwModeParams.enable80211N ? 1 : 0,
186 iface_params.hwModeParams.enable80211AC ? 1 : 0,
Roshan Pius49446472018-03-07 10:23:46 -0800187 hw_mode_as_string.c_str(), ht_cap_vht_oper_chwidth_as_string.c_str(),
188 nw_params.isHidden ? 1 : 0, encryption_config_as_string.c_str());
Roshan Pius30b452e2017-12-27 13:36:21 -0800189}
Roshan Piuse2d21012018-08-17 11:22:06 -0700190
191// hostapd core functions accept "C" style function pointers, so use global
192// functions to pass to the hostapd core function and store the corresponding
193// std::function methods to be invoked.
194//
195// NOTE: Using the pattern from the vendor HAL (wifi_legacy_hal.cpp).
196//
197// Callback to be invoked once setup is complete
198std::function<void(struct hostapd_data*)> on_setup_complete_internal_callback;
199void onAsyncSetupCompleteCb(void* ctx)
200{
201 struct hostapd_data* iface_hapd = (struct hostapd_data*)ctx;
202 if (on_setup_complete_internal_callback) {
203 on_setup_complete_internal_callback(iface_hapd);
204 // Invalidate this callback since we don't want this firing
205 // again.
206 on_setup_complete_internal_callback = nullptr;
207 }
208}
Roshan Pius30b452e2017-12-27 13:36:21 -0800209} // namespace
210
Roshan Piuscc817562017-12-22 14:45:05 -0800211namespace android {
212namespace hardware {
213namespace wifi {
214namespace hostapd {
Roshan Piuse2d21012018-08-17 11:22:06 -0700215namespace V1_1 {
Roshan Piuscc817562017-12-22 14:45:05 -0800216namespace implementation {
217using hidl_return_util::call;
Roshan Piuse2d21012018-08-17 11:22:06 -0700218using namespace android::hardware::wifi::hostapd::V1_0;
Roshan Piuscc817562017-12-22 14:45:05 -0800219
220Hostapd::Hostapd(struct hapd_interfaces* interfaces) : interfaces_(interfaces)
221{}
222
223Return<void> Hostapd::addAccessPoint(
224 const IfaceParams& iface_params, const NetworkParams& nw_params,
225 addAccessPoint_cb _hidl_cb)
226{
227 return call(
228 this, &Hostapd::addAccessPointInternal, _hidl_cb, iface_params,
229 nw_params);
230}
231
232Return<void> Hostapd::removeAccessPoint(
233 const hidl_string& iface_name, removeAccessPoint_cb _hidl_cb)
234{
235 return call(
236 this, &Hostapd::removeAccessPointInternal, _hidl_cb, iface_name);
237}
238
Roshan Piuse2d21012018-08-17 11:22:06 -0700239Return<void> Hostapd::terminate()
240{
Roshan Pius3455af42018-02-01 09:19:49 -0800241 wpa_printf(MSG_INFO, "Terminating...");
242 eloop_terminate();
243 return Void();
244}
245
Roshan Piuse2d21012018-08-17 11:22:06 -0700246Return<void> Hostapd::registerCallback(
247 const sp<IHostapdCallback>& callback, registerCallback_cb _hidl_cb)
248{
249 return call(
250 this, &Hostapd::registerCallbackInternal, _hidl_cb, callback);
251}
252
Roshan Piuscc817562017-12-22 14:45:05 -0800253HostapdStatus Hostapd::addAccessPointInternal(
254 const IfaceParams& iface_params, const NetworkParams& nw_params)
255{
Roshan Pius087d8692017-12-27 14:11:44 -0800256 if (hostapd_get_iface(interfaces_, iface_params.ifaceName.c_str())) {
257 wpa_printf(
258 MSG_ERROR, "Interface %s already present",
259 iface_params.ifaceName.c_str());
260 return {HostapdStatusCode::FAILURE_IFACE_EXISTS, ""};
261 }
262 const auto conf_params = CreateHostapdConfig(iface_params, nw_params);
263 if (conf_params.empty()) {
264 wpa_printf(MSG_ERROR, "Failed to create config params");
265 return {HostapdStatusCode::FAILURE_ARGS_INVALID, ""};
266 }
267 const auto conf_file_path =
268 WriteHostapdConfig(iface_params.ifaceName, conf_params);
269 if (conf_file_path.empty()) {
270 wpa_printf(MSG_ERROR, "Failed to write config file");
271 return {HostapdStatusCode::FAILURE_UNKNOWN, ""};
272 }
273 std::string add_iface_param_str = StringPrintf(
274 "%s config=%s", iface_params.ifaceName.c_str(),
275 conf_file_path.c_str());
276 std::vector<char> add_iface_param_vec(
277 add_iface_param_str.begin(), add_iface_param_str.end() + 1);
278 if (hostapd_add_iface(interfaces_, add_iface_param_vec.data()) < 0) {
279 wpa_printf(
280 MSG_ERROR, "Adding interface %s failed",
281 add_iface_param_str.c_str());
282 return {HostapdStatusCode::FAILURE_UNKNOWN, ""};
283 }
284 struct hostapd_data* iface_hapd =
285 hostapd_get_iface(interfaces_, iface_params.ifaceName.c_str());
286 WPA_ASSERT(iface_hapd != nullptr && iface_hapd->iface != nullptr);
Roshan Piuse2d21012018-08-17 11:22:06 -0700287 // Register the setup complete callbacks
288 on_setup_complete_internal_callback =
289 [this](struct hostapd_data* iface_hapd) {
290 wpa_printf(
291 MSG_DEBUG, "AP interface setup completed - state %s",
292 hostapd_state_text(iface_hapd->iface->state));
293 if (iface_hapd->iface->state == HAPD_IFACE_DISABLED) {
294 // Invoke the failure callback on all registered
295 // clients.
296 for (const auto& callback : callbacks_) {
297 callback->onFailure(
298 iface_hapd->conf->iface);
299 }
300 }
301 };
302 iface_hapd->setup_complete_cb = onAsyncSetupCompleteCb;
303 iface_hapd->setup_complete_cb_ctx = iface_hapd;
Roshan Pius087d8692017-12-27 14:11:44 -0800304 if (hostapd_enable_iface(iface_hapd->iface) < 0) {
305 wpa_printf(
306 MSG_ERROR, "Enabling interface %s failed",
307 iface_params.ifaceName.c_str());
308 return {HostapdStatusCode::FAILURE_UNKNOWN, ""};
309 }
Roshan Piuscc817562017-12-22 14:45:05 -0800310 return {HostapdStatusCode::SUCCESS, ""};
311}
312
313HostapdStatus Hostapd::removeAccessPointInternal(const std::string& iface_name)
314{
Roshan Pius087d8692017-12-27 14:11:44 -0800315 std::vector<char> remove_iface_param_vec(
316 iface_name.begin(), iface_name.end() + 1);
317 if (hostapd_remove_iface(interfaces_, remove_iface_param_vec.data()) <
318 0) {
319 wpa_printf(
320 MSG_ERROR, "Removing interface %s failed",
321 iface_name.c_str());
322 return {HostapdStatusCode::FAILURE_UNKNOWN, ""};
323 }
Roshan Piuscc817562017-12-22 14:45:05 -0800324 return {HostapdStatusCode::SUCCESS, ""};
325}
Roshan Piuse2d21012018-08-17 11:22:06 -0700326
327HostapdStatus Hostapd::registerCallbackInternal(
328 const sp<IHostapdCallback>& callback)
329{
330 callbacks_.push_back(callback);
331 return {HostapdStatusCode::SUCCESS, ""};
332}
333
Roshan Piuscc817562017-12-22 14:45:05 -0800334} // namespace implementation
Roshan Piuse2d21012018-08-17 11:22:06 -0700335} // namespace V1_1
Roshan Piuscc817562017-12-22 14:45:05 -0800336} // namespace hostapd
337} // namespace wifi
338} // namespace hardware
339} // namespace android