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( |
| 44 | const std::weak_ptr<wifi_system::InterfaceTool> iface_tool) |
| 45 | : iface_tool_(iface_tool), |
| 46 | random_mac_address_(nullptr), |
| 47 | event_handlers_map_() {} |
Roshan Pius | 99dab38 | 2019-02-14 07:57:10 -0800 | [diff] [blame] | 48 | |
| 49 | std::array<uint8_t, 6> WifiIfaceUtil::getFactoryMacAddress( |
| 50 | const std::string& iface_name) { |
Roshan Pius | c885df0 | 2019-05-21 14:49:05 -0700 | [diff] [blame] | 51 | return iface_tool_.lock()->GetFactoryMacAddress(iface_name.c_str()); |
Roshan Pius | 99dab38 | 2019-02-14 07:57:10 -0800 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | bool WifiIfaceUtil::setMacAddress(const std::string& iface_name, |
| 55 | const std::array<uint8_t, 6>& mac) { |
Ahmed ElArabawy | 3ee519b | 2020-04-20 16:28:49 +0800 | [diff] [blame] | 56 | #ifndef WIFI_AVOID_IFACE_RESET_MAC_CHANGE |
Roshan Pius | c885df0 | 2019-05-21 14:49:05 -0700 | [diff] [blame] | 57 | if (!iface_tool_.lock()->SetUpState(iface_name.c_str(), false)) { |
Roshan Pius | 99dab38 | 2019-02-14 07:57:10 -0800 | [diff] [blame] | 58 | LOG(ERROR) << "SetUpState(false) failed."; |
| 59 | return false; |
| 60 | } |
Ahmed ElArabawy | 3ee519b | 2020-04-20 16:28:49 +0800 | [diff] [blame] | 61 | #endif |
Roshan Pius | c885df0 | 2019-05-21 14:49:05 -0700 | [diff] [blame] | 62 | if (!iface_tool_.lock()->SetMacAddress(iface_name.c_str(), mac)) { |
Roshan Pius | 99dab38 | 2019-02-14 07:57:10 -0800 | [diff] [blame] | 63 | LOG(ERROR) << "SetMacAddress failed."; |
| 64 | return false; |
| 65 | } |
Ahmed ElArabawy | 3ee519b | 2020-04-20 16:28:49 +0800 | [diff] [blame] | 66 | #ifndef WIFI_AVOID_IFACE_RESET_MAC_CHANGE |
Roshan Pius | c885df0 | 2019-05-21 14:49:05 -0700 | [diff] [blame] | 67 | if (!iface_tool_.lock()->SetUpState(iface_name.c_str(), true)) { |
Roshan Pius | 99dab38 | 2019-02-14 07:57:10 -0800 | [diff] [blame] | 68 | LOG(ERROR) << "SetUpState(true) failed."; |
| 69 | return false; |
| 70 | } |
Ahmed ElArabawy | 3ee519b | 2020-04-20 16:28:49 +0800 | [diff] [blame] | 71 | #endif |
Roshan Pius | 356d5a6 | 2019-05-17 15:07:34 -0700 | [diff] [blame] | 72 | IfaceEventHandlers event_handlers = {}; |
| 73 | const auto it = event_handlers_map_.find(iface_name); |
| 74 | if (it != event_handlers_map_.end()) { |
| 75 | event_handlers = it->second; |
| 76 | } |
| 77 | if (event_handlers.on_state_toggle_off_on != nullptr) { |
| 78 | event_handlers.on_state_toggle_off_on(iface_name); |
| 79 | } |
Roshan Pius | 99dab38 | 2019-02-14 07:57:10 -0800 | [diff] [blame] | 80 | LOG(DEBUG) << "Successfully SetMacAddress."; |
| 81 | return true; |
| 82 | } |
| 83 | |
Roshan Pius | 3b6705f | 2019-02-14 09:43:43 -0800 | [diff] [blame] | 84 | std::array<uint8_t, 6> WifiIfaceUtil::getOrCreateRandomMacAddress() { |
| 85 | if (random_mac_address_) { |
| 86 | return *random_mac_address_.get(); |
| 87 | } |
| 88 | random_mac_address_ = |
| 89 | std::make_unique<std::array<uint8_t, 6>>(createRandomMacAddress()); |
| 90 | return *random_mac_address_.get(); |
| 91 | } |
| 92 | |
Roshan Pius | 356d5a6 | 2019-05-17 15:07:34 -0700 | [diff] [blame] | 93 | void WifiIfaceUtil::registerIfaceEventHandlers(const std::string& iface_name, |
| 94 | IfaceEventHandlers handlers) { |
| 95 | event_handlers_map_[iface_name] = handlers; |
| 96 | } |
| 97 | |
| 98 | void WifiIfaceUtil::unregisterIfaceEventHandlers( |
| 99 | const std::string& iface_name) { |
| 100 | event_handlers_map_.erase(iface_name); |
| 101 | } |
| 102 | |
Roshan Pius | 3b6705f | 2019-02-14 09:43:43 -0800 | [diff] [blame] | 103 | std::array<uint8_t, 6> WifiIfaceUtil::createRandomMacAddress() { |
Roshan Pius | c444679 | 2019-02-14 13:20:39 -0800 | [diff] [blame] | 104 | std::array<uint8_t, 6> address = {}; |
| 105 | std::random_device rd; |
| 106 | std::default_random_engine engine(rd()); |
| 107 | std::uniform_int_distribution<uint8_t> dist( |
| 108 | std::numeric_limits<uint8_t>::min(), |
| 109 | std::numeric_limits<uint8_t>::max()); |
| 110 | for (size_t i = 0; i < address.size(); i++) { |
| 111 | address[i] = dist(engine); |
| 112 | } |
| 113 | // Set the local bit and clear the multicast bit. |
| 114 | address[0] |= kMacAddressLocallyAssignedMask; |
| 115 | address[0] &= ~kMacAddressMulticastMask; |
| 116 | return address; |
Roshan Pius | 3b6705f | 2019-02-14 09:43:43 -0800 | [diff] [blame] | 117 | } |
Roshan Pius | 5ba0a90 | 2020-04-14 11:55:42 -0700 | [diff] [blame] | 118 | |
| 119 | bool WifiIfaceUtil::setUpState(const std::string& iface_name, bool request_up) { |
| 120 | if (!iface_tool_.lock()->SetUpState(iface_name.c_str(), request_up)) { |
| 121 | LOG(ERROR) << "SetUpState to " << request_up << " failed"; |
| 122 | return false; |
| 123 | } |
| 124 | return true; |
| 125 | } |
Nate Jiang | 63f34ed | 2020-05-20 23:22:51 -0700 | [diff] [blame] | 126 | |
| 127 | unsigned WifiIfaceUtil::ifNameToIndex(const std::string& iface_name) { |
| 128 | return if_nametoindex(iface_name.c_str()); |
| 129 | } |
Roshan Pius | 99dab38 | 2019-02-14 07:57:10 -0800 | [diff] [blame] | 130 | } // namespace iface_util |
| 131 | } // namespace implementation |
Jimmy Chen | d460df3 | 2019-11-29 17:31:22 +0200 | [diff] [blame] | 132 | } // namespace V1_5 |
Roshan Pius | 99dab38 | 2019-02-14 07:57:10 -0800 | [diff] [blame] | 133 | } // namespace wifi |
| 134 | } // namespace hardware |
| 135 | } // namespace android |