blob: 208491c793e4645bd1f290aeb3560a5e5b509f1b [file] [log] [blame]
Gabriel Biren57ededa2021-09-03 16:08:50 +00001/*
2 * WPA Supplicant - Supplicant Aidl interface
3 * Copyright (c) 2021, Google Inc. All rights reserved.
4 *
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
7 */
8
9#include "aidl_manager.h"
10#include "aidl_return_util.h"
11#include "misc_utils.h"
12#include "supplicant.h"
13#include "p2p_iface.h"
14
15#include <android-base/file.h>
16#include <fcntl.h>
17#include <sys/stat.h>
18
19namespace {
20
21// Pre-populated interface params for interfaces controlled by wpa_supplicant.
22// Note: This may differ for other OEM's. So, modify this accordingly.
23constexpr char kIfaceDriverName[] = "nl80211";
24constexpr char kStaIfaceConfPath[] =
25 "/data/vendor/wifi/wpa/wpa_supplicant.conf";
26static const char* kStaIfaceConfOverlayPaths[] = {
27 "/apex/com.android.wifi.hal/etc/wifi/wpa_supplicant_overlay.conf",
28 "/vendor/etc/wifi/wpa_supplicant_overlay.conf",
29};
30constexpr char kP2pIfaceConfPath[] =
31 "/data/vendor/wifi/wpa/p2p_supplicant.conf";
32static const char* kP2pIfaceConfOverlayPaths[] = {
33 "/apex/com.android.wifi.hal/etc/wifi/p2p_supplicant_overlay.conf",
34 "/vendor/etc/wifi/p2p_supplicant_overlay.conf",
35};
36// Migrate conf files for existing devices.
37static const char* kTemplateConfPaths[] = {
38 "/apex/com.android.wifi.hal/etc/wifi/wpa_supplicant.conf",
39 "/vendor/etc/wifi/wpa_supplicant.conf",
40 "/system/etc/wifi/wpa_supplicant.conf",
41};
42constexpr char kOldStaIfaceConfPath[] = "/data/misc/wifi/wpa_supplicant.conf";
43constexpr char kOldP2pIfaceConfPath[] = "/data/misc/wifi/p2p_supplicant.conf";
44constexpr mode_t kConfigFileMode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
45
46const char* resolvePath(const char* paths[], size_t size)
47{
48 for (int i = 0; i < size; ++i) {
49 if (access(paths[i], R_OK) == 0) {
50 return paths[i];
51 }
52 }
53 return nullptr;
54}
55
56int copyFile(
57 const std::string& src_file_path, const std::string& dest_file_path)
58{
59 std::string file_contents;
60 if (!android::base::ReadFileToString(src_file_path, &file_contents)) {
61 wpa_printf(
62 MSG_ERROR, "Failed to read from %s. Errno: %s",
63 src_file_path.c_str(), strerror(errno));
64 return -1;
65 }
66 if (!android::base::WriteStringToFile(
67 file_contents, dest_file_path, kConfigFileMode, getuid(),
68 getgid())) {
69 wpa_printf(
70 MSG_ERROR, "Failed to write to %s. Errno: %s",
71 dest_file_path.c_str(), strerror(errno));
72 return -1;
73 }
74 return 0;
75}
76
77/**
78 * Copy |src_file_path| to |dest_file_path| if it exists.
79 *
80 * Returns 1 if |src_file_path| does not exist or not accessible,
81 * Returns -1 if the copy fails.
82 * Returns 0 if the copy succeeds.
83 */
84int copyFileIfItExists(
85 const std::string& src_file_path, const std::string& dest_file_path)
86{
87 int ret = access(src_file_path.c_str(), R_OK);
88 // Sepolicy denial (2018+ device) will return EACCESS instead of ENOENT.
89 if ((ret != 0) && ((errno == ENOENT) || (errno == EACCES))) {
90 return 1;
91 }
92 ret = copyFile(src_file_path, dest_file_path);
93 if (ret != 0) {
94 wpa_printf(
95 MSG_ERROR, "Failed copying %s to %s.",
96 src_file_path.c_str(), dest_file_path.c_str());
97 return -1;
98 }
99 return 0;
100}
101
102/**
103 * Ensure that the specified config file pointed by |config_file_path| exists.
104 * a) If the |config_file_path| exists with the correct permissions, return.
105 * b) If the |config_file_path| does not exist, but |old_config_file_path|
106 * exists, copy over the contents of the |old_config_file_path| to
107 * |config_file_path|.
108 * c) If the |config_file_path| & |old_config_file_path|
109 * does not exists, copy over the contents of |template_config_file_path|.
110 */
111int ensureConfigFileExists(
112 const std::string& config_file_path,
113 const std::string& old_config_file_path)
114{
115 int ret = access(config_file_path.c_str(), R_OK | W_OK);
116 if (ret == 0) {
117 return 0;
118 }
119 if (errno == EACCES) {
120 ret = chmod(config_file_path.c_str(), kConfigFileMode);
121 if (ret == 0) {
122 return 0;
123 } else {
124 wpa_printf(
125 MSG_ERROR, "Cannot set RW to %s. Errno: %s",
126 config_file_path.c_str(), strerror(errno));
127 return -1;
128 }
129 } else if (errno != ENOENT) {
130 wpa_printf(
131 MSG_ERROR, "Cannot acces %s. Errno: %s",
132 config_file_path.c_str(), strerror(errno));
133 return -1;
134 }
135 ret = copyFileIfItExists(old_config_file_path, config_file_path);
136 if (ret == 0) {
137 wpa_printf(
138 MSG_INFO, "Migrated conf file from %s to %s",
139 old_config_file_path.c_str(), config_file_path.c_str());
140 unlink(old_config_file_path.c_str());
141 return 0;
142 } else if (ret == -1) {
143 unlink(config_file_path.c_str());
144 return -1;
145 }
146 const char* path =
147 resolvePath(kTemplateConfPaths, sizeof(kTemplateConfPaths));
148 if (path != nullptr) {
149 ret = copyFileIfItExists(path, config_file_path);
150 if (ret == 0) {
151 wpa_printf(
152 MSG_INFO, "Copied template conf file from %s to %s",
153 path, config_file_path.c_str());
154 return 0;
155 } else if (ret == -1) {
156 unlink(config_file_path.c_str());
157 return -1;
158 }
159 }
160 // Did not create the conf file.
161 return -1;
162}
163} // namespace
164
165namespace aidl {
166namespace android {
167namespace hardware {
168namespace wifi {
169namespace supplicant {
170using aidl_return_util::validateAndCall;
171using misc_utils::createStatus;
172using misc_utils::createStatusWithMsg;
173
174Supplicant::Supplicant(struct wpa_global* global) : wpa_global_(global) {}
175bool Supplicant::isValid()
176{
177 // This top level object cannot be invalidated.
178 return true;
179}
180
181::ndk::ScopedAStatus Supplicant::addP2pInterface(
182 const std::string& in_name,
183 std::shared_ptr<ISupplicantP2pIface>* _aidl_return)
184{
185 return validateAndCall(
186 this, SupplicantStatusCode::FAILURE_IFACE_INVALID,
187 &Supplicant::addP2pInterfaceInternal, _aidl_return, in_name);
188}
189
190::ndk::ScopedAStatus Supplicant::addStaInterface(
191 const std::string& in_name,
192 std::shared_ptr<ISupplicantStaIface>* _aidl_return)
193{
194 return validateAndCall(
195 this, SupplicantStatusCode::FAILURE_IFACE_INVALID,
196 &Supplicant::addStaInterfaceInternal, _aidl_return, in_name);
197}
198
199::ndk::ScopedAStatus Supplicant::removeInterface(
200 const IfaceInfo& in_ifaceInfo)
201{
202 return validateAndCall(
203 this, SupplicantStatusCode::FAILURE_IFACE_INVALID,
204 &Supplicant::removeInterfaceInternal, in_ifaceInfo);
205}
206
207::ndk::ScopedAStatus Supplicant::getP2pInterface(
208 const std::string& in_name,
209 std::shared_ptr<ISupplicantP2pIface>* _aidl_return)
210{
211 return validateAndCall(
212 this, SupplicantStatusCode::FAILURE_IFACE_INVALID,
213 &Supplicant::getP2pInterfaceInternal, _aidl_return, in_name);
214}
215
216::ndk::ScopedAStatus Supplicant::getStaInterface(
217 const std::string& in_name,
218 std::shared_ptr<ISupplicantStaIface>* _aidl_return)
219{
220 return validateAndCall(
221 this, SupplicantStatusCode::FAILURE_IFACE_INVALID,
222 &Supplicant::getStaInterfaceInternal, _aidl_return, in_name);
223}
224
225::ndk::ScopedAStatus Supplicant::listInterfaces(
226 std::vector<IfaceInfo>* _aidl_return)
227{
228 return validateAndCall(
229 this, SupplicantStatusCode::FAILURE_IFACE_INVALID,
230 &Supplicant::listInterfacesInternal, _aidl_return);
231}
232
233::ndk::ScopedAStatus Supplicant::registerCallback(
234 const std::shared_ptr<ISupplicantCallback>& in_callback)
235{
236 return validateAndCall(
237 this, SupplicantStatusCode::FAILURE_IFACE_INVALID,
238 &Supplicant::registerCallbackInternal, in_callback);
239}
240
241::ndk::ScopedAStatus Supplicant::setDebugParams(
242 DebugLevel in_level, bool in_showTimestamp,
243 bool in_showKeys)
244{
245 return validateAndCall(
246 this, SupplicantStatusCode::FAILURE_IFACE_INVALID,
247 &Supplicant::setDebugParamsInternal, in_level,
248 in_showTimestamp, in_showKeys);
249}
250
251::ndk::ScopedAStatus Supplicant::setConcurrencyPriority(
252 IfaceType in_type)
253{
254 return validateAndCall(
255 this, SupplicantStatusCode::FAILURE_IFACE_INVALID,
256 &Supplicant::setConcurrencyPriorityInternal, in_type);
257}
258
259::ndk::ScopedAStatus Supplicant::getDebugLevel(DebugLevel* _aidl_return)
260{
261 *_aidl_return = static_cast<DebugLevel>(wpa_debug_level);
262 return ndk::ScopedAStatus::ok();
263}
264
265::ndk::ScopedAStatus Supplicant::isDebugShowTimestampEnabled(bool* _aidl_return)
266{
267 *_aidl_return = ((wpa_debug_timestamp != 0) ? true : false);
268 return ndk::ScopedAStatus::ok();
269}
270
271::ndk::ScopedAStatus Supplicant::isDebugShowKeysEnabled(bool* _aidl_return)
272{
273 *_aidl_return = ((wpa_debug_show_keys != 0) ? true : false);
274 return ndk::ScopedAStatus::ok();
275}
276
277::ndk::ScopedAStatus Supplicant::terminate()
278{
279 wpa_printf(MSG_INFO, "Terminating...");
280 wpa_supplicant_terminate_proc(wpa_global_);
281 return ndk::ScopedAStatus::ok();
282}
283
284ndk::ScopedAStatus Supplicant::addP2pDevInterface(struct wpa_interface iface_params)
285{
286 char primary_ifname[IFNAMSIZ];
287 u32 primary_ifname_len =
288 strlen(iface_params.ifname) - strlen(P2P_MGMT_DEVICE_PREFIX);
289
290 if(primary_ifname_len > IFNAMSIZ) {
291 wpa_printf(MSG_DEBUG, "%s, Invalid primary iface name ", __FUNCTION__);
292 return createStatus(SupplicantStatusCode::FAILURE_ARGS_INVALID);
293 }
294
295 strncpy(primary_ifname, iface_params.ifname +
296 strlen(P2P_MGMT_DEVICE_PREFIX), primary_ifname_len);
297 wpa_printf(MSG_DEBUG, "%s, Initialize p2p-dev-wlan0 iface with"
298 "primary_iface = %s", __FUNCTION__, primary_ifname);
299 struct wpa_supplicant* wpa_s =
300 wpa_supplicant_get_iface(wpa_global_, primary_ifname);
301 if (!wpa_s) {
302 wpa_printf(MSG_DEBUG, "%s,NULL wpa_s for wlan0", __FUNCTION__);
303 return createStatus(SupplicantStatusCode::FAILURE_IFACE_UNKNOWN);
304 }
305 if (wpas_p2p_add_p2pdev_interface(
306 wpa_s, wpa_s->global->params.conf_p2p_dev) < 0) {
307 wpa_printf(MSG_INFO,
308 "Failed to enable P2P Device");
309 return createStatus(SupplicantStatusCode::FAILURE_UNKNOWN);
310 }
311 return ndk::ScopedAStatus::ok();
312}
313
314std::pair<std::shared_ptr<ISupplicantP2pIface>, ndk::ScopedAStatus>
315Supplicant::addP2pInterfaceInternal(const std::string& name)
316{
317 std::shared_ptr<ISupplicantP2pIface> iface;
318
319 // Check if required |ifname| argument is empty.
320 if (name.empty()) {
321 return {nullptr, createStatus(SupplicantStatusCode::FAILURE_ARGS_INVALID)};
322 }
323 // Try to get the wpa_supplicant record for this iface, return
324 // the iface object with the appropriate status code if it exists.
325 ndk::ScopedAStatus status;
326 std::tie(iface, status) = getP2pInterfaceInternal(name);
327 if (status.isOk()) {
328 wpa_printf(MSG_INFO, "Iface already exists, return existing");
329 return {iface, ndk::ScopedAStatus::ok()};
330 }
331
332 struct wpa_interface iface_params = {};
333 iface_params.driver = kIfaceDriverName;
334 if (ensureConfigFileExists(
335 kP2pIfaceConfPath, kOldP2pIfaceConfPath) != 0) {
336 wpa_printf(
337 MSG_ERROR, "Conf file does not exists: %s",
338 kP2pIfaceConfPath);
339 return {nullptr, createStatusWithMsg(
340 SupplicantStatusCode::FAILURE_UNKNOWN, "Conf file does not exist")};
341 }
342 iface_params.confname = kP2pIfaceConfPath;
343 const char* path = resolvePath(
344 kP2pIfaceConfOverlayPaths,
345 sizeof(kP2pIfaceConfOverlayPaths));
346 if (path != nullptr) {
347 iface_params.confanother = path;
348 }
349
350 iface_params.ifname = name.c_str();
351 if (strncmp(iface_params.ifname, P2P_MGMT_DEVICE_PREFIX,
352 strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
353 status = addP2pDevInterface(iface_params);
354 if (!status.isOk()) {
355 return {iface, createStatus(static_cast<SupplicantStatusCode>(
356 status.getServiceSpecificError()))};
357 }
358 } else {
359 struct wpa_supplicant* wpa_s =
360 wpa_supplicant_add_iface(wpa_global_, &iface_params, NULL);
361 if (!wpa_s) {
362 return {nullptr, createStatus(SupplicantStatusCode::FAILURE_UNKNOWN)};
363 }
364 // Request the current scan results from the driver and update
365 // the local BSS list wpa_s->bss. This is to avoid a full scan
366 // while processing the connect request on newly created interface.
367 wpa_supplicant_update_scan_results(wpa_s);
368 }
369 // The supplicant core creates a corresponding aidl object via
370 // AidlManager when |wpa_supplicant_add_iface| is called.
371 return getP2pInterfaceInternal(name);
372}
373
374std::pair<std::shared_ptr<ISupplicantStaIface>, ndk::ScopedAStatus>
375Supplicant::addStaInterfaceInternal(const std::string& name)
376{
377 std::shared_ptr<ISupplicantStaIface> iface;
378
379 // Check if required |ifname| argument is empty.
380 if (name.empty()) {
381 return {nullptr, createStatus(SupplicantStatusCode::FAILURE_ARGS_INVALID)};
382 }
383 // Try to get the wpa_supplicant record for this iface, return
384 // the iface object with the appropriate status code if it exists.
385 ndk::ScopedAStatus status;
386 std::tie(iface, status) = getStaInterfaceInternal(name);
387 if (status.isOk()) {
388 wpa_printf(MSG_INFO, "Iface already exists, return existing");
389 return {iface, ndk::ScopedAStatus::ok()};
390 }
391
392 struct wpa_interface iface_params = {};
393 iface_params.driver = kIfaceDriverName;
394 if (ensureConfigFileExists(
395 kStaIfaceConfPath, kOldStaIfaceConfPath) != 0) {
396 wpa_printf(
397 MSG_ERROR, "Conf file does not exists: %s",
398 kStaIfaceConfPath);
399 return {nullptr, createStatusWithMsg(
400 SupplicantStatusCode::FAILURE_UNKNOWN, "Conf file does not exist")};
401 }
402 iface_params.confname = kStaIfaceConfPath;
403 const char* path = resolvePath(
404 kStaIfaceConfOverlayPaths,
405 sizeof(kStaIfaceConfOverlayPaths));
406 if (path != nullptr) {
407 iface_params.confanother = path;
408 }
409
410 iface_params.ifname = name.c_str();
411 if (strncmp(iface_params.ifname, P2P_MGMT_DEVICE_PREFIX,
412 strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
413 status = addP2pDevInterface(iface_params);
414 if (!status.isOk()) {
415 return {iface, createStatus(static_cast<SupplicantStatusCode>(
416 status.getServiceSpecificError()))};
417 }
418 } else {
419 struct wpa_supplicant* wpa_s =
420 wpa_supplicant_add_iface(wpa_global_, &iface_params, NULL);
421 if (!wpa_s) {
422 return {nullptr, createStatus(SupplicantStatusCode::FAILURE_UNKNOWN)};
423 }
424 // Request the current scan results from the driver and update
425 // the local BSS list wpa_s->bss. This is to avoid a full scan
426 // while processing the connect request on newly created interface.
427 wpa_supplicant_update_scan_results(wpa_s);
428 }
429 // The supplicant core creates a corresponding aidl object via
430 // AidlManager when |wpa_supplicant_add_iface| is called.
431 return getStaInterfaceInternal(name);
432}
433
434ndk::ScopedAStatus Supplicant::removeInterfaceInternal(
435 const IfaceInfo& iface_info)
436{
437 struct wpa_supplicant* wpa_s =
438 wpa_supplicant_get_iface(wpa_global_, iface_info.name.c_str());
439 if (!wpa_s) {
440 return createStatus(SupplicantStatusCode::FAILURE_IFACE_UNKNOWN);
441 }
442 if (wpa_supplicant_remove_iface(wpa_global_, wpa_s, 0)) {
443 return createStatus(SupplicantStatusCode::FAILURE_UNKNOWN);
444 }
445 return ndk::ScopedAStatus::ok();
446}
447
448std::pair<std::shared_ptr<ISupplicantP2pIface>, ndk::ScopedAStatus>
449Supplicant::getP2pInterfaceInternal(const std::string& name)
450{
451 struct wpa_supplicant* wpa_s =
452 wpa_supplicant_get_iface(wpa_global_, name.c_str());
453 if (!wpa_s) {
454 return {nullptr, createStatus(SupplicantStatusCode::FAILURE_IFACE_UNKNOWN)};
455 }
456 AidlManager* aidl_manager = AidlManager::getInstance();
457 std::shared_ptr<ISupplicantP2pIface> iface;
458 if (!aidl_manager ||
459 aidl_manager->getP2pIfaceAidlObjectByIfname(
460 wpa_s->ifname, &iface)) {
461 return {iface, createStatus(SupplicantStatusCode::FAILURE_UNKNOWN)};
462 }
463 // Set this flag true here, since there is no AIDL initialize
464 // method for the p2p config, and the supplicant interface is
465 // not ready when the p2p iface is created.
466 wpa_s->conf->persistent_reconnect = true;
467 return {iface, ndk::ScopedAStatus::ok()};
468}
469
470std::pair<std::shared_ptr<ISupplicantStaIface>, ndk::ScopedAStatus>
471Supplicant::getStaInterfaceInternal(const std::string& name)
472{
473 struct wpa_supplicant* wpa_s =
474 wpa_supplicant_get_iface(wpa_global_, name.c_str());
475 if (!wpa_s) {
476 return {nullptr, createStatus(SupplicantStatusCode::FAILURE_IFACE_UNKNOWN)};
477 }
478 AidlManager* aidl_manager = AidlManager::getInstance();
479 std::shared_ptr<ISupplicantStaIface> iface;
480 if (!aidl_manager ||
481 aidl_manager->getStaIfaceAidlObjectByIfname(
482 wpa_s->ifname, &iface)) {
483 return {iface, createStatus(SupplicantStatusCode::FAILURE_UNKNOWN)};
484 }
485 return {iface, ndk::ScopedAStatus::ok()};
486}
487
488std::pair<std::vector<IfaceInfo>, ndk::ScopedAStatus>
489Supplicant::listInterfacesInternal()
490{
491 std::vector<IfaceInfo> ifaces;
492 for (struct wpa_supplicant* wpa_s = wpa_global_->ifaces; wpa_s;
493 wpa_s = wpa_s->next) {
494 if (wpa_s->global->p2p_init_wpa_s == wpa_s) {
495 ifaces.emplace_back(IfaceInfo{
496 IfaceType::P2P, wpa_s->ifname});
497 } else {
498 ifaces.emplace_back(IfaceInfo{
499 IfaceType::STA, wpa_s->ifname});
500 }
501 }
502 return {std::move(ifaces), ndk::ScopedAStatus::ok()};
503}
504
505ndk::ScopedAStatus Supplicant::registerCallbackInternal(
506 const std::shared_ptr<ISupplicantCallback>& callback)
507{
508 AidlManager* aidl_manager = AidlManager::getInstance();
509 if (!aidl_manager ||
510 aidl_manager->addSupplicantCallbackAidlObject(callback)) {
511 return createStatus(SupplicantStatusCode::FAILURE_UNKNOWN);
512 }
513 return ndk::ScopedAStatus::ok();
514}
515
516ndk::ScopedAStatus Supplicant::setDebugParamsInternal(
517 DebugLevel level, bool show_timestamp, bool show_keys)
518{
519 if (wpa_supplicant_set_debug_params(
520 wpa_global_, static_cast<uint32_t>(level), show_timestamp,
521 show_keys)) {
522 return createStatus(SupplicantStatusCode::FAILURE_UNKNOWN);
523 }
524 return ndk::ScopedAStatus::ok();
525}
526
527ndk::ScopedAStatus Supplicant::setConcurrencyPriorityInternal(IfaceType type)
528{
529 if (type == IfaceType::STA) {
530 wpa_global_->conc_pref =
531 wpa_global::wpa_conc_pref::WPA_CONC_PREF_STA;
532 } else if (type == IfaceType::P2P) {
533 wpa_global_->conc_pref =
534 wpa_global::wpa_conc_pref::WPA_CONC_PREF_P2P;
535 } else {
536 return createStatus(SupplicantStatusCode::FAILURE_ARGS_INVALID);
537 }
538 return ndk::ScopedAStatus::ok();
539}
540} // namespace supplicant
541} // namespace wifi
542} // namespace hardware
543} // namespace android
544} // namespace aidl