Kevin Marshall | 3e89fd7 | 2018-06-05 21:29:10 | [diff] [blame] | 1 | // Copyright 2018 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "net/base/network_change_notifier_fuchsia.h" |
| 6 | |
Tamir Duberstein | bf0a6a1d | 2020-08-03 18:44:11 | [diff] [blame^] | 7 | #include <lib/async-loop/cpp/loop.h> |
Sharon Yang | a793925 | 2019-09-19 17:22:23 | [diff] [blame] | 8 | #include <lib/sys/cpp/component_context.h> |
Tamir Duberstein | bf0a6a1d | 2020-08-03 18:44:11 | [diff] [blame^] | 9 | |
Kevin Marshall | 3e89fd7 | 2018-06-05 21:29:10 | [diff] [blame] | 10 | #include <algorithm> |
| 11 | #include <string> |
| 12 | #include <utility> |
| 13 | #include <vector> |
| 14 | |
Sebastien Marchand | 6d0558fd | 2019-01-25 16:49:37 | [diff] [blame] | 15 | #include "base/bind.h" |
Jeremy Manson | 18f6ea3 | 2018-11-13 21:12:27 | [diff] [blame] | 16 | #include "base/fuchsia/fuchsia_logging.h" |
Sharon Yang | b2ff20e | 2020-06-19 12:54:01 | [diff] [blame] | 17 | #include "base/fuchsia/process_context.h" |
Kevin Marshall | 3e89fd7 | 2018-06-05 21:29:10 | [diff] [blame] | 18 | #include "base/optional.h" |
| 19 | #include "base/run_loop.h" |
| 20 | #include "net/base/network_interfaces.h" |
| 21 | #include "net/base/network_interfaces_fuchsia.h" |
| 22 | |
| 23 | namespace net { |
Kevin Marshall | 3e89fd7 | 2018-06-05 21:29:10 | [diff] [blame] | 24 | |
Aidan Wolter | d89b754 | 2019-01-24 11:01:21 | [diff] [blame] | 25 | NetworkChangeNotifierFuchsia::NetworkChangeNotifierFuchsia( |
| 26 | uint32_t required_features) |
Sharon Yang | b2ff20e | 2020-06-19 12:54:01 | [diff] [blame] | 27 | : NetworkChangeNotifierFuchsia(base::ComponentContextForProcess() |
| 28 | ->svc() |
| 29 | ->Connect<fuchsia::netstack::Netstack>(), |
| 30 | required_features) {} |
Kevin Marshall | 3e89fd7 | 2018-06-05 21:29:10 | [diff] [blame] | 31 | |
| 32 | NetworkChangeNotifierFuchsia::NetworkChangeNotifierFuchsia( |
Tamir Duberstein | bf0a6a1d | 2020-08-03 18:44:11 | [diff] [blame^] | 33 | fidl::InterfaceHandle<fuchsia::netstack::Netstack> netstack, |
Eric Orth | c398f1e | 2019-07-09 21:54:55 | [diff] [blame] | 34 | uint32_t required_features, |
| 35 | SystemDnsConfigChangeNotifier* system_dns_config_notifier) |
| 36 | : NetworkChangeNotifier(NetworkChangeCalculatorParams(), |
| 37 | system_dns_config_notifier), |
| 38 | required_features_(required_features) { |
Wez | ec6e7f1 | 2019-05-17 23:02:09 | [diff] [blame] | 39 | DCHECK(netstack); |
Kevin Marshall | 3e89fd7 | 2018-06-05 21:29:10 | [diff] [blame] | 40 | |
Jeremy Manson | 18f6ea3 | 2018-11-13 21:12:27 | [diff] [blame] | 41 | netstack_.set_error_handler([](zx_status_t status) { |
Tamir Duberstein | bf0a6a1d | 2020-08-03 18:44:11 | [diff] [blame^] | 42 | ZX_LOG(FATAL, status) << "Lost connection to netstack."; |
Jeremy Manson | 18f6ea3 | 2018-11-13 21:12:27 | [diff] [blame] | 43 | }); |
Tamir Duberstein | bf0a6a1d | 2020-08-03 18:44:11 | [diff] [blame^] | 44 | |
Wez | 84ac6fe | 2019-02-28 03:45:54 | [diff] [blame] | 45 | netstack_.events().OnInterfacesChanged = fit::bind_member( |
| 46 | this, &NetworkChangeNotifierFuchsia::ProcessInterfaceList); |
Tamir Duberstein | bf0a6a1d | 2020-08-03 18:44:11 | [diff] [blame^] | 47 | |
| 48 | // Temporarily bind to a local dispatcher so we can synchronously wait for the |
| 49 | // synthetic event to populate the initial state. |
| 50 | async::Loop loop(&kAsyncLoopConfigNeverAttachToThread); |
| 51 | zx_status_t status = netstack_.Bind(std::move(netstack), loop.dispatcher()); |
| 52 | ZX_CHECK(status == ZX_OK, status) << "Bind()"; |
| 53 | on_initial_interfaces_received_ = |
| 54 | base::BindOnce(&async::Loop::Quit, base::Unretained(&loop)); |
| 55 | status = loop.Run(); |
| 56 | ZX_CHECK(status == ZX_ERR_CANCELED, status) << "loop.Run()"; |
| 57 | |
| 58 | // Bind to the dispatcher for the thread's MessagePump. |
| 59 | // |
| 60 | // Note this must be done before |loop| is destroyed, since that would close |
| 61 | // the interface handle underlying |netstack_|. |
| 62 | status = netstack_.Bind(netstack_.Unbind()); |
| 63 | ZX_CHECK(status == ZX_OK, status) << "Bind()"; |
Kevin Marshall | 3e89fd7 | 2018-06-05 21:29:10 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | NetworkChangeNotifierFuchsia::~NetworkChangeNotifierFuchsia() { |
| 67 | DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
Paul Jensen | 61a51a1 | 2019-05-23 23:12:49 | [diff] [blame] | 68 | ClearGlobalPointer(); |
Kevin Marshall | 3e89fd7 | 2018-06-05 21:29:10 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | NetworkChangeNotifier::ConnectionType |
| 72 | NetworkChangeNotifierFuchsia::GetCurrentConnectionType() const { |
| 73 | ConnectionType type = static_cast<ConnectionType>( |
| 74 | base::subtle::Acquire_Load(&cached_connection_type_)); |
| 75 | return type; |
| 76 | } |
| 77 | |
Kevin Marshall | 3e89fd7 | 2018-06-05 21:29:10 | [diff] [blame] | 78 | void NetworkChangeNotifierFuchsia::ProcessInterfaceList( |
Wez | 3a12771b | 2019-01-13 06:49:50 | [diff] [blame] | 79 | std::vector<fuchsia::netstack::NetInterface> interfaces) { |
Wez | 84ac6fe | 2019-02-28 03:45:54 | [diff] [blame] | 80 | netstack_->GetRouteTable( |
| 81 | [this, interfaces = std::move(interfaces)]( |
| 82 | std::vector<fuchsia::netstack::RouteTableEntry> route_table) mutable { |
Haines Sy | 2eb17bcb | 2019-10-26 17:48:18 | [diff] [blame] | 83 | OnRouteTableReceived(std::move(interfaces), std::move(route_table)); |
Wez | 84ac6fe | 2019-02-28 03:45:54 | [diff] [blame] | 84 | }); |
Kevin Marshall | 3e89fd7 | 2018-06-05 21:29:10 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | void NetworkChangeNotifierFuchsia::OnRouteTableReceived( |
Wez | 3a12771b | 2019-01-13 06:49:50 | [diff] [blame] | 88 | std::vector<fuchsia::netstack::NetInterface> interfaces, |
Haines Sy | 2eb17bcb | 2019-10-26 17:48:18 | [diff] [blame] | 89 | std::vector<fuchsia::netstack::RouteTableEntry> route_table) { |
Aidan Wolter | d89b754 | 2019-01-24 11:01:21 | [diff] [blame] | 90 | // Create a set of NICs that have default routes (ie 0.0.0.0). |
| 91 | base::flat_set<uint32_t> default_route_ids; |
| 92 | for (const auto& route : route_table) { |
| 93 | if (MaskPrefixLength( |
| 94 | internal::FuchsiaIpAddressToIPAddress(route.netmask)) == 0) { |
| 95 | default_route_ids.insert(route.nicid); |
Kevin Marshall | 3e89fd7 | 2018-06-05 21:29:10 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | |
Moe Ahmadi | 5bc17745 | 2019-01-23 18:48:59 | [diff] [blame] | 99 | ConnectionType connection_type = CONNECTION_NONE; |
Aidan Wolter | d89b754 | 2019-01-24 11:01:21 | [diff] [blame] | 100 | base::flat_set<IPAddress> addresses; |
| 101 | for (auto& interface : interfaces) { |
| 102 | // Filter out loopback and invalid connection types. |
| 103 | if ((internal::ConvertConnectionType(interface) == |
| 104 | NetworkChangeNotifier::CONNECTION_NONE) || |
| 105 | (interface.features & |
| 106 | fuchsia::hardware::ethernet::INFO_FEATURE_LOOPBACK)) { |
| 107 | continue; |
| 108 | } |
| 109 | |
| 110 | // Filter out interfaces that do not meet the |required_features_|. |
| 111 | if ((interface.features & required_features_) != required_features_) { |
| 112 | continue; |
| 113 | } |
| 114 | |
| 115 | // Filter out interfaces with non-default routes. |
| 116 | if (!default_route_ids.contains(interface.id)) { |
| 117 | continue; |
| 118 | } |
| 119 | |
Kevin Marshall | 3e89fd7 | 2018-06-05 21:29:10 | [diff] [blame] | 120 | std::vector<NetworkInterface> flattened_interfaces = |
Aidan Wolter | d89b754 | 2019-01-24 11:01:21 | [diff] [blame] | 121 | internal::NetInterfaceToNetworkInterfaces(interface); |
| 122 | if (flattened_interfaces.empty()) { |
| 123 | continue; |
| 124 | } |
| 125 | |
| 126 | // Add the addresses from this interface to the list of all addresses. |
Kevin Marshall | 3e89fd7 | 2018-06-05 21:29:10 | [diff] [blame] | 127 | std::transform( |
| 128 | flattened_interfaces.begin(), flattened_interfaces.end(), |
| 129 | std::inserter(addresses, addresses.begin()), |
| 130 | [](const NetworkInterface& interface) { return interface.address; }); |
Aidan Wolter | d89b754 | 2019-01-24 11:01:21 | [diff] [blame] | 131 | |
| 132 | // Set the default connection to the first interface connection found. |
| 133 | if (connection_type == CONNECTION_NONE) { |
Kevin Marshall | 3e89fd7 | 2018-06-05 21:29:10 | [diff] [blame] | 134 | connection_type = flattened_interfaces.front().type; |
| 135 | } |
Kevin Marshall | 3e89fd7 | 2018-06-05 21:29:10 | [diff] [blame] | 136 | } |
| 137 | |
Kevin Marshall | 3e89fd7 | 2018-06-05 21:29:10 | [diff] [blame] | 138 | if (addresses != cached_addresses_) { |
| 139 | std::swap(cached_addresses_, addresses); |
Haines Sy | 2eb17bcb | 2019-10-26 17:48:18 | [diff] [blame] | 140 | NotifyObserversOfIPAddressChange(); |
Kevin Marshall | 3e89fd7 | 2018-06-05 21:29:10 | [diff] [blame] | 141 | } |
| 142 | |
Wez | ec6e7f1 | 2019-05-17 23:02:09 | [diff] [blame] | 143 | if (connection_type != cached_connection_type_) { |
| 144 | base::subtle::Release_Store(&cached_connection_type_, connection_type); |
Haines Sy | 2eb17bcb | 2019-10-26 17:48:18 | [diff] [blame] | 145 | NotifyObserversOfConnectionTypeChange(); |
Kevin Marshall | 3e89fd7 | 2018-06-05 21:29:10 | [diff] [blame] | 146 | } |
Tamir Duberstein | bf0a6a1d | 2020-08-03 18:44:11 | [diff] [blame^] | 147 | |
| 148 | if (on_initial_interfaces_received_) { |
| 149 | std::move(on_initial_interfaces_received_).Run(); |
| 150 | } |
Kevin Marshall | 3e89fd7 | 2018-06-05 21:29:10 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | } // namespace net |