1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
|
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/base/fuchsia/network_interface_cache.h"
#include <fuchsia/net/interfaces/cpp/fidl.h>
#include <optional>
#include <utility>
#include "base/containers/flat_map.h"
#include "base/logging.h"
#include "base/sequence_checker.h"
#include "base/synchronization/lock.h"
#include "net/base/network_change_notifier.h"
#include "net/base/network_interfaces.h"
#include "net/base/network_interfaces_fuchsia.h"
namespace net::internal {
namespace {
// Returns a ConnectionType derived from the supplied InterfaceProperties:
// - CONNECTION_NONE if the interface is not publicly routable.
// - Otherwise, returns a type derived from the interface's device_class.
NetworkChangeNotifier::ConnectionType GetEffectiveConnectionType(
const InterfaceProperties& properties,
bool require_wlan) {
if (!properties.IsPubliclyRoutable()) {
return NetworkChangeNotifier::CONNECTION_NONE;
}
NetworkChangeNotifier::ConnectionType connection_type =
ConvertConnectionType(properties.device_class());
if (require_wlan &&
connection_type != NetworkChangeNotifier::CONNECTION_WIFI) {
return NetworkChangeNotifier::CONNECTION_NONE;
}
return connection_type;
}
bool CanReachExternalNetwork(const InterfaceProperties& interface,
bool require_wlan) {
return GetEffectiveConnectionType(interface, require_wlan) !=
NetworkChangeNotifier::CONNECTION_NONE;
}
} // namespace
NetworkInterfaceCache::NetworkInterfaceCache(bool require_wlan)
: require_wlan_(require_wlan) {}
NetworkInterfaceCache::~NetworkInterfaceCache() = default;
std::optional<NetworkInterfaceCache::ChangeBits>
NetworkInterfaceCache::AddInterfaces(
std::vector<fuchsia::net::interfaces::Properties> interfaces) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
base::AutoLock auto_lock(lock_);
ChangeBits combined_changes = kNoChange;
for (auto& interface : interfaces) {
auto change_bits = AddInterfaceWhileLocked(std::move(interface));
if (!change_bits.has_value()) {
return std::nullopt;
}
combined_changes |= change_bits.value();
}
return combined_changes;
}
std::optional<NetworkInterfaceCache::ChangeBits>
NetworkInterfaceCache::AddInterface(
fuchsia::net::interfaces::Properties properties) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
base::AutoLock auto_lock(lock_);
return AddInterfaceWhileLocked(std::move(properties));
}
std::optional<NetworkInterfaceCache::ChangeBits>
NetworkInterfaceCache::AddInterfaceWhileLocked(
fuchsia::net::interfaces::Properties properties)
EXCLUSIVE_LOCKS_REQUIRED(lock_) VALID_CONTEXT_REQUIRED(sequence_checker_) {
if (error_state_) {
return std::nullopt;
}
auto interface = InterfaceProperties::VerifyAndCreate(std::move(properties));
if (!interface) {
LOG(ERROR) << "Incomplete interface properties.";
SetErrorWhileLocked();
return std::nullopt;
}
if (interfaces_.find(interface->id()) != interfaces_.end()) {
LOG(ERROR) << "Unexpected duplicate interface ID " << interface->id();
SetErrorWhileLocked();
return std::nullopt;
}
ChangeBits change_bits = kNoChange;
if (CanReachExternalNetwork(*interface, require_wlan_)) {
change_bits |= kIpAddressChanged;
}
interfaces_.emplace(interface->id(), std::move(*interface));
if (UpdateConnectionTypeWhileLocked()) {
change_bits |= kConnectionTypeChanged;
}
return change_bits;
}
std::optional<NetworkInterfaceCache::ChangeBits>
NetworkInterfaceCache::ChangeInterface(
fuchsia::net::interfaces::Properties properties) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
base::AutoLock auto_lock(lock_);
if (error_state_) {
return std::nullopt;
}
auto cache_entry = interfaces_.find(properties.id());
if (cache_entry == interfaces_.end()) {
LOG(ERROR) << "Unknown interface ID " << properties.id();
SetErrorWhileLocked();
return std::nullopt;
}
const bool old_can_reach =
CanReachExternalNetwork(cache_entry->second, require_wlan_);
const bool has_addresses = properties.has_addresses();
if (!cache_entry->second.Update(std::move(properties))) {
LOG(ERROR) << "Update failed";
SetErrorWhileLocked();
return std::nullopt;
}
const bool new_can_reach =
CanReachExternalNetwork(cache_entry->second, require_wlan_);
ChangeBits change_bits = kNoChange;
if (has_addresses || old_can_reach != new_can_reach) {
change_bits |= kIpAddressChanged;
}
if (UpdateConnectionTypeWhileLocked()) {
change_bits |= kConnectionTypeChanged;
}
return change_bits;
}
std::optional<NetworkInterfaceCache::ChangeBits>
NetworkInterfaceCache::RemoveInterface(
InterfaceProperties::InterfaceId interface_id) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
base::AutoLock auto_lock(lock_);
if (error_state_) {
return std::nullopt;
}
auto cache_entry = interfaces_.find(interface_id);
if (cache_entry == interfaces_.end()) {
LOG(ERROR) << "Unknown interface ID " << interface_id;
SetErrorWhileLocked();
return std::nullopt;
}
ChangeBits change_bits = kNoChange;
if (CanReachExternalNetwork(cache_entry->second, require_wlan_)) {
change_bits |= kIpAddressChanged;
}
interfaces_.erase(cache_entry);
if (UpdateConnectionTypeWhileLocked()) {
change_bits |= kConnectionTypeChanged;
}
return change_bits;
}
bool NetworkInterfaceCache::GetOnlineInterfaces(
NetworkInterfaceList* networks) const {
DCHECK(networks);
base::AutoLock auto_lock(lock_);
if (error_state_) {
return false;
}
for (const auto& [_, interface] : interfaces_) {
if (!interface.online()) {
continue;
}
if (interface.device_class().is_loopback()) {
continue;
}
interface.AppendNetworkInterfaces(networks);
}
return true;
}
NetworkChangeNotifier::ConnectionType NetworkInterfaceCache::GetConnectionType()
const {
base::AutoLock auto_lock(lock_);
if (error_state_) {
return NetworkChangeNotifier::CONNECTION_UNKNOWN;
}
return connection_type_;
}
void NetworkInterfaceCache::SetError() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
base::AutoLock auto_lock(lock_);
SetErrorWhileLocked();
}
bool NetworkInterfaceCache::UpdateConnectionTypeWhileLocked()
EXCLUSIVE_LOCKS_REQUIRED(lock_) VALID_CONTEXT_REQUIRED(sequence_checker_) {
NetworkChangeNotifier::ConnectionType connection_type =
NetworkChangeNotifier::ConnectionType::CONNECTION_NONE;
for (const auto& [_, interface] : interfaces_) {
connection_type = GetEffectiveConnectionType(interface, require_wlan_);
if (connection_type != NetworkChangeNotifier::CONNECTION_NONE) {
break;
}
}
if (connection_type != connection_type_) {
connection_type_ = connection_type;
return true;
}
return false;
}
void NetworkInterfaceCache::SetErrorWhileLocked()
EXCLUSIVE_LOCKS_REQUIRED(lock_) VALID_CONTEXT_REQUIRED(sequence_checker_) {
error_state_ = true;
interfaces_.clear();
interfaces_.shrink_to_fit();
}
} // namespace net::internal
|