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