1 /*
2 * Copyright (C) 2022 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
17 #include <android-base/logging.h>
18 #include <android-base/macros.h>
19 #include <net/if.h>
20 #include <private/android_filesystem_config.h>
21
22 #include <cstddef>
23 #include <iostream>
24 #include <limits>
25 #include <random>
26
27 #include "wifi_iface_util.h"
28
29 namespace {
30 // Constants to set the local bit & clear the multicast bit.
31 constexpr uint8_t kMacAddressMulticastMask = 0x01;
32 constexpr uint8_t kMacAddressLocallyAssignedMask = 0x02;
33
34 } // namespace
35
36 namespace aidl {
37 namespace android {
38 namespace hardware {
39 namespace wifi {
40 namespace iface_util {
41
WifiIfaceUtil(const std::weak_ptr<::android::wifi_system::InterfaceTool> iface_tool,const std::weak_ptr<legacy_hal::WifiLegacyHal> legacy_hal)42 WifiIfaceUtil::WifiIfaceUtil(const std::weak_ptr<::android::wifi_system::InterfaceTool> iface_tool,
43 const std::weak_ptr<legacy_hal::WifiLegacyHal> legacy_hal)
44 : iface_tool_(iface_tool),
45 legacy_hal_(legacy_hal),
46 random_mac_address_(nullptr),
47 event_handlers_map_() {}
48
getFactoryMacAddress(const std::string & iface_name)49 std::array<uint8_t, 6> WifiIfaceUtil::getFactoryMacAddress(const std::string& iface_name) {
50 return iface_tool_.lock()->GetFactoryMacAddress(iface_name.c_str());
51 }
52
setMacAddress(const std::string & iface_name,const std::array<uint8_t,6> & mac)53 bool WifiIfaceUtil::setMacAddress(const std::string& iface_name,
54 const std::array<uint8_t, 6>& mac) {
55 #ifndef WIFI_AVOID_IFACE_RESET_MAC_CHANGE
56 legacy_hal::wifi_error legacy_status;
57 uint64_t legacy_feature_set;
58 std::tie(legacy_status, legacy_feature_set) =
59 legacy_hal_.lock()->getSupportedFeatureSet(iface_name);
60
61 if (!(legacy_feature_set & WIFI_FEATURE_DYNAMIC_SET_MAC) &&
62 !iface_tool_.lock()->SetUpState(iface_name.c_str(), false)) {
63 LOG(ERROR) << "SetUpState(false) failed.";
64 return false;
65 }
66 #endif
67 bool success = iface_tool_.lock()->SetMacAddress(iface_name.c_str(), mac);
68 #ifndef WIFI_AVOID_IFACE_RESET_MAC_CHANGE
69 if (!(legacy_feature_set & WIFI_FEATURE_DYNAMIC_SET_MAC) &&
70 !iface_tool_.lock()->SetUpState(iface_name.c_str(), true)) {
71 LOG(ERROR) << "SetUpState(true) failed. Wait for driver ready.";
72 // Wait for driver ready and try to set iface UP again
73 if (legacy_hal_.lock()->waitForDriverReady() != legacy_hal::WIFI_SUCCESS) {
74 LOG(ERROR) << "SetUpState(true) wait for driver ready failed.";
75 return false;
76 }
77 if (!iface_tool_.lock()->SetUpState(iface_name.c_str(), true)) {
78 LOG(ERROR) << "SetUpState(true) failed after retry.";
79 return false;
80 }
81 }
82 #endif
83 IfaceEventHandlers event_handlers = {};
84 const auto it = event_handlers_map_.find(iface_name);
85 if (it != event_handlers_map_.end()) {
86 event_handlers = it->second;
87 }
88 if (event_handlers.on_state_toggle_off_on != nullptr) {
89 event_handlers.on_state_toggle_off_on(iface_name);
90 }
91 if (!success) {
92 LOG(ERROR) << "SetMacAddress failed on " << iface_name;
93 } else {
94 LOG(DEBUG) << "SetMacAddress succeeded on " << iface_name;
95 }
96 return success;
97 }
98
getOrCreateRandomMacAddress()99 std::array<uint8_t, 6> WifiIfaceUtil::getOrCreateRandomMacAddress() {
100 if (random_mac_address_) {
101 return *random_mac_address_.get();
102 }
103 random_mac_address_ = std::make_unique<std::array<uint8_t, 6>>(createRandomMacAddress());
104 return *random_mac_address_.get();
105 }
106
registerIfaceEventHandlers(const std::string & iface_name,IfaceEventHandlers handlers)107 void WifiIfaceUtil::registerIfaceEventHandlers(const std::string& iface_name,
108 IfaceEventHandlers handlers) {
109 event_handlers_map_[iface_name] = handlers;
110 }
111
unregisterIfaceEventHandlers(const std::string & iface_name)112 void WifiIfaceUtil::unregisterIfaceEventHandlers(const std::string& iface_name) {
113 event_handlers_map_.erase(iface_name);
114 }
115
createRandomMacAddress()116 std::array<uint8_t, 6> WifiIfaceUtil::createRandomMacAddress() {
117 std::array<uint8_t, 6> address = {};
118 std::random_device rd;
119 std::default_random_engine engine(rd());
120 std::uniform_int_distribution<uint8_t> dist(std::numeric_limits<uint8_t>::min(),
121 std::numeric_limits<uint8_t>::max());
122 for (size_t i = 0; i < address.size(); i++) {
123 address[i] = dist(engine);
124 }
125 // Set the local bit and clear the multicast bit.
126 address[0] |= kMacAddressLocallyAssignedMask;
127 address[0] &= ~kMacAddressMulticastMask;
128 return address;
129 }
130
setUpState(const std::string & iface_name,bool request_up)131 bool WifiIfaceUtil::setUpState(const std::string& iface_name, bool request_up) {
132 if (!iface_tool_.lock()->SetUpState(iface_name.c_str(), request_up)) {
133 LOG(ERROR) << "SetUpState to " << request_up << " failed";
134 return false;
135 }
136 return true;
137 }
138
ifNameToIndex(const std::string & iface_name)139 unsigned WifiIfaceUtil::ifNameToIndex(const std::string& iface_name) {
140 return if_nametoindex(iface_name.c_str());
141 }
142
createBridge(const std::string & br_name)143 bool WifiIfaceUtil::createBridge(const std::string& br_name) {
144 if (!iface_tool_.lock()->createBridge(br_name)) {
145 return false;
146 }
147
148 if (!iface_tool_.lock()->SetUpState(br_name.c_str(), true)) {
149 LOG(ERROR) << "bridge SetUpState(true) failed.";
150 }
151 return true;
152 }
153
deleteBridge(const std::string & br_name)154 bool WifiIfaceUtil::deleteBridge(const std::string& br_name) {
155 if (!iface_tool_.lock()->SetUpState(br_name.c_str(), false)) {
156 LOG(INFO) << "SetUpState(false) failed for bridge=" << br_name.c_str();
157 }
158
159 return iface_tool_.lock()->deleteBridge(br_name);
160 }
161
addIfaceToBridge(const std::string & br_name,const std::string & if_name)162 bool WifiIfaceUtil::addIfaceToBridge(const std::string& br_name, const std::string& if_name) {
163 return iface_tool_.lock()->addIfaceToBridge(br_name, if_name);
164 }
165
removeIfaceFromBridge(const std::string & br_name,const std::string & if_name)166 bool WifiIfaceUtil::removeIfaceFromBridge(const std::string& br_name, const std::string& if_name) {
167 return iface_tool_.lock()->removeIfaceFromBridge(br_name, if_name);
168 }
169
170 } // namespace iface_util
171 } // namespace wifi
172 } // namespace hardware
173 } // namespace android
174 } // namespace aidl
175