blob: d55e4f825169e39ea2b993b52fef777c18ec0a80 [file] [log] [blame]
Roshan Pius99dab382019-02-14 07:57:10 -08001/*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Nate Jiang63f34ed2020-05-20 23:22:51 -070017#include <net/if.h>
Roshan Piusc4446792019-02-14 13:20:39 -080018#include <cstddef>
19#include <iostream>
20#include <limits>
21#include <random>
22
Roshan Pius99dab382019-02-14 07:57:10 -080023#include <android-base/logging.h>
24#include <android-base/macros.h>
25#include <private/android_filesystem_config.h>
26
Roshan Piusc4446792019-02-14 13:20:39 -080027#undef NAN
Roshan Pius99dab382019-02-14 07:57:10 -080028#include "wifi_iface_util.h"
29
Roshan Piusc4446792019-02-14 13:20:39 -080030namespace {
31// Constants to set the local bit & clear the multicast bit.
32constexpr uint8_t kMacAddressMulticastMask = 0x01;
33constexpr uint8_t kMacAddressLocallyAssignedMask = 0x02;
34} // namespace
35
Roshan Pius99dab382019-02-14 07:57:10 -080036namespace android {
37namespace hardware {
38namespace wifi {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -080039namespace V1_6 {
Roshan Pius99dab382019-02-14 07:57:10 -080040namespace implementation {
41namespace iface_util {
42
Ahmed ElArabawy687ce132022-01-11 16:42:48 -080043WifiIfaceUtil::WifiIfaceUtil(const std::weak_ptr<wifi_system::InterfaceTool> iface_tool,
44 const std::weak_ptr<legacy_hal::WifiLegacyHal> legacy_hal)
Roshan Piusc885df02019-05-21 14:49:05 -070045 : iface_tool_(iface_tool),
Roshan Pius8c1a67b2021-03-02 10:00:23 -080046 legacy_hal_(legacy_hal),
Roshan Piusc885df02019-05-21 14:49:05 -070047 random_mac_address_(nullptr),
48 event_handlers_map_() {}
Roshan Pius99dab382019-02-14 07:57:10 -080049
Ahmed ElArabawy687ce132022-01-11 16:42:48 -080050std::array<uint8_t, 6> WifiIfaceUtil::getFactoryMacAddress(const std::string& iface_name) {
Roshan Piusc885df02019-05-21 14:49:05 -070051 return iface_tool_.lock()->GetFactoryMacAddress(iface_name.c_str());
Roshan Pius99dab382019-02-14 07:57:10 -080052}
53
54bool WifiIfaceUtil::setMacAddress(const std::string& iface_name,
55 const std::array<uint8_t, 6>& mac) {
Ahmed ElArabawy3ee519b2020-04-20 16:28:49 +080056#ifndef WIFI_AVOID_IFACE_RESET_MAC_CHANGE
Veerendranath Jakkamfce46f42021-10-08 03:05:31 +053057 legacy_hal::wifi_error legacy_status;
58 uint64_t legacy_feature_set;
59 std::tie(legacy_status, legacy_feature_set) =
Ahmed ElArabawy687ce132022-01-11 16:42:48 -080060 legacy_hal_.lock()->getSupportedFeatureSet(iface_name);
Veerendranath Jakkamfce46f42021-10-08 03:05:31 +053061
62 if (!(legacy_feature_set & WIFI_FEATURE_DYNAMIC_SET_MAC) &&
63 !iface_tool_.lock()->SetUpState(iface_name.c_str(), false)) {
Roshan Pius99dab382019-02-14 07:57:10 -080064 LOG(ERROR) << "SetUpState(false) failed.";
65 return false;
66 }
Ahmed ElArabawy3ee519b2020-04-20 16:28:49 +080067#endif
Roshan Pius8c1a67b2021-03-02 10:00:23 -080068 bool success = iface_tool_.lock()->SetMacAddress(iface_name.c_str(), mac);
Ahmed ElArabawy3ee519b2020-04-20 16:28:49 +080069#ifndef WIFI_AVOID_IFACE_RESET_MAC_CHANGE
Veerendranath Jakkamfce46f42021-10-08 03:05:31 +053070 if (!(legacy_feature_set & WIFI_FEATURE_DYNAMIC_SET_MAC) &&
71 !iface_tool_.lock()->SetUpState(iface_name.c_str(), true)) {
Roshan Pius8c1a67b2021-03-02 10:00:23 -080072 LOG(ERROR) << "SetUpState(true) failed. Wait for driver ready.";
73 // Wait for driver ready and try to set iface UP again
Ahmed ElArabawy687ce132022-01-11 16:42:48 -080074 if (legacy_hal_.lock()->waitForDriverReady() != legacy_hal::WIFI_SUCCESS) {
Roshan Pius8c1a67b2021-03-02 10:00:23 -080075 LOG(ERROR) << "SetUpState(true) wait for driver ready failed.";
76 return false;
77 }
78 if (!iface_tool_.lock()->SetUpState(iface_name.c_str(), true)) {
79 LOG(ERROR) << "SetUpState(true) failed after retry.";
80 return false;
81 }
Roshan Pius99dab382019-02-14 07:57:10 -080082 }
Ahmed ElArabawy3ee519b2020-04-20 16:28:49 +080083#endif
Roshan Pius356d5a62019-05-17 15:07:34 -070084 IfaceEventHandlers event_handlers = {};
85 const auto it = event_handlers_map_.find(iface_name);
86 if (it != event_handlers_map_.end()) {
87 event_handlers = it->second;
88 }
89 if (event_handlers.on_state_toggle_off_on != nullptr) {
90 event_handlers.on_state_toggle_off_on(iface_name);
91 }
Roshan Pius8c1a67b2021-03-02 10:00:23 -080092 if (!success) {
Les Leeda9f5fe2021-06-29 22:48:11 +080093 LOG(ERROR) << "SetMacAddress failed on " << iface_name;
Roshan Pius8c1a67b2021-03-02 10:00:23 -080094 } else {
Les Leeda9f5fe2021-06-29 22:48:11 +080095 LOG(DEBUG) << "SetMacAddress succeeded on " << iface_name;
Roshan Pius8c1a67b2021-03-02 10:00:23 -080096 }
97 return success;
Roshan Pius99dab382019-02-14 07:57:10 -080098}
99
Roshan Pius3b6705f2019-02-14 09:43:43 -0800100std::array<uint8_t, 6> WifiIfaceUtil::getOrCreateRandomMacAddress() {
101 if (random_mac_address_) {
102 return *random_mac_address_.get();
103 }
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800104 random_mac_address_ = std::make_unique<std::array<uint8_t, 6>>(createRandomMacAddress());
Roshan Pius3b6705f2019-02-14 09:43:43 -0800105 return *random_mac_address_.get();
106}
107
Roshan Pius356d5a62019-05-17 15:07:34 -0700108void WifiIfaceUtil::registerIfaceEventHandlers(const std::string& iface_name,
109 IfaceEventHandlers handlers) {
110 event_handlers_map_[iface_name] = handlers;
111}
112
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800113void WifiIfaceUtil::unregisterIfaceEventHandlers(const std::string& iface_name) {
Roshan Pius356d5a62019-05-17 15:07:34 -0700114 event_handlers_map_.erase(iface_name);
115}
116
Roshan Pius3b6705f2019-02-14 09:43:43 -0800117std::array<uint8_t, 6> WifiIfaceUtil::createRandomMacAddress() {
Roshan Piusc4446792019-02-14 13:20:39 -0800118 std::array<uint8_t, 6> address = {};
119 std::random_device rd;
120 std::default_random_engine engine(rd());
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800121 std::uniform_int_distribution<uint8_t> dist(std::numeric_limits<uint8_t>::min(),
122 std::numeric_limits<uint8_t>::max());
Roshan Piusc4446792019-02-14 13:20:39 -0800123 for (size_t i = 0; i < address.size(); i++) {
124 address[i] = dist(engine);
125 }
126 // Set the local bit and clear the multicast bit.
127 address[0] |= kMacAddressLocallyAssignedMask;
128 address[0] &= ~kMacAddressMulticastMask;
129 return address;
Roshan Pius3b6705f2019-02-14 09:43:43 -0800130}
Roshan Pius5ba0a902020-04-14 11:55:42 -0700131
132bool WifiIfaceUtil::setUpState(const std::string& iface_name, bool request_up) {
133 if (!iface_tool_.lock()->SetUpState(iface_name.c_str(), request_up)) {
134 LOG(ERROR) << "SetUpState to " << request_up << " failed";
135 return false;
136 }
137 return true;
138}
Nate Jiang63f34ed2020-05-20 23:22:51 -0700139
140unsigned WifiIfaceUtil::ifNameToIndex(const std::string& iface_name) {
141 return if_nametoindex(iface_name.c_str());
142}
leslc92aa852020-11-16 15:20:33 +0800143
144bool WifiIfaceUtil::createBridge(const std::string& br_name) {
145 if (!iface_tool_.lock()->createBridge(br_name)) {
146 return false;
147 }
148
149 if (!iface_tool_.lock()->SetUpState(br_name.c_str(), true)) {
150 LOG(ERROR) << "bridge SetUpState(true) failed.";
151 }
152 return true;
153}
154
155bool WifiIfaceUtil::deleteBridge(const std::string& br_name) {
156 if (!iface_tool_.lock()->SetUpState(br_name.c_str(), false)) {
157 LOG(INFO) << "SetUpState(false) failed for bridge=" << br_name.c_str();
158 }
159
160 return iface_tool_.lock()->deleteBridge(br_name);
161}
162
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800163bool WifiIfaceUtil::addIfaceToBridge(const std::string& br_name, const std::string& if_name) {
leslc92aa852020-11-16 15:20:33 +0800164 return iface_tool_.lock()->addIfaceToBridge(br_name, if_name);
165}
166
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800167bool WifiIfaceUtil::removeIfaceFromBridge(const std::string& br_name, const std::string& if_name) {
leslc92aa852020-11-16 15:20:33 +0800168 return iface_tool_.lock()->removeIfaceFromBridge(br_name, if_name);
169}
170
Roshan Pius99dab382019-02-14 07:57:10 -0800171} // namespace iface_util
172} // namespace implementation
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800173} // namespace V1_6
Roshan Pius99dab382019-02-14 07:57:10 -0800174} // namespace wifi
175} // namespace hardware
176} // namespace android