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 | |
Sharon Yang | a793925 | 2019-09-19 17:22:23 | [diff] [blame] | 7 | #include <lib/sys/cpp/component_context.h> |
Kevin Marshall | 3e89fd7 | 2018-06-05 21:29:10 | [diff] [blame] | 8 | #include <algorithm> |
| 9 | #include <string> |
| 10 | #include <utility> |
| 11 | #include <vector> |
| 12 | |
Sebastien Marchand | 6d0558fd | 2019-01-25 16:49:37 | [diff] [blame] | 13 | #include "base/bind.h" |
Sharon Yang | a793925 | 2019-09-19 17:22:23 | [diff] [blame] | 14 | #include "base/fuchsia/default_context.h" |
Jeremy Manson | 18f6ea3 | 2018-11-13 21:12:27 | [diff] [blame] | 15 | #include "base/fuchsia/fuchsia_logging.h" |
Kevin Marshall | 3e89fd7 | 2018-06-05 21:29:10 | [diff] [blame] | 16 | #include "base/optional.h" |
| 17 | #include "base/run_loop.h" |
| 18 | #include "net/base/network_interfaces.h" |
| 19 | #include "net/base/network_interfaces_fuchsia.h" |
| 20 | |
| 21 | namespace net { |
Kevin Marshall | 3e89fd7 | 2018-06-05 21:29:10 | [diff] [blame] | 22 | |
Aidan Wolter | d89b754 | 2019-01-24 11:01:21 | [diff] [blame] | 23 | NetworkChangeNotifierFuchsia::NetworkChangeNotifierFuchsia( |
| 24 | uint32_t required_features) |
Kevin Marshall | 3e89fd7 | 2018-06-05 21:29:10 | [diff] [blame] | 25 | : NetworkChangeNotifierFuchsia( |
Sharon Yang | a793925 | 2019-09-19 17:22:23 | [diff] [blame] | 26 | base::fuchsia::ComponentContextForCurrentProcess() |
| 27 | ->svc() |
| 28 | ->Connect<fuchsia::netstack::Netstack>(), |
Aidan Wolter | d89b754 | 2019-01-24 11:01:21 | [diff] [blame] | 29 | required_features) {} |
Kevin Marshall | 3e89fd7 | 2018-06-05 21:29:10 | [diff] [blame] | 30 | |
| 31 | NetworkChangeNotifierFuchsia::NetworkChangeNotifierFuchsia( |
Aidan Wolter | d89b754 | 2019-01-24 11:01:21 | [diff] [blame] | 32 | fuchsia::netstack::NetstackPtr netstack, |
Eric Orth | c398f1e | 2019-07-09 21:54:55 | [diff] [blame] | 33 | uint32_t required_features, |
| 34 | SystemDnsConfigChangeNotifier* system_dns_config_notifier) |
| 35 | : NetworkChangeNotifier(NetworkChangeCalculatorParams(), |
| 36 | system_dns_config_notifier), |
| 37 | required_features_(required_features) { |
Wez | ec6e7f1 | 2019-05-17 23:02:09 | [diff] [blame] | 38 | DCHECK(netstack); |
Kevin Marshall | 3e89fd7 | 2018-06-05 21:29:10 | [diff] [blame] | 39 | |
Wez | ec6e7f1 | 2019-05-17 23:02:09 | [diff] [blame] | 40 | // Temporarily re-wrap our Netstack channel so we can query the interfaces |
| 41 | // and routing table synchronously to populate the initial state. |
| 42 | fuchsia::netstack::NetstackSyncPtr sync_netstack; |
| 43 | sync_netstack.Bind(netstack.Unbind()); |
Wez | adb2dd70c | 2019-06-01 00:16:44 | [diff] [blame] | 44 | DCHECK(sync_netstack); |
Wez | ec6e7f1 | 2019-05-17 23:02:09 | [diff] [blame] | 45 | |
| 46 | // Manually fetch the interfaces and routes. |
| 47 | std::vector<fuchsia::netstack::NetInterface> interfaces; |
| 48 | zx_status_t status = sync_netstack->GetInterfaces(&interfaces); |
| 49 | ZX_CHECK(status == ZX_OK, status) << "synchronous GetInterfaces()"; |
| 50 | std::vector<fuchsia::netstack::RouteTableEntry> routes; |
| 51 | status = sync_netstack->GetRouteTable(&routes); |
| 52 | ZX_CHECK(status == ZX_OK, status) << "synchronous GetInterfaces()"; |
| 53 | OnRouteTableReceived(std::move(interfaces), std::move(routes), false); |
| 54 | |
| 55 | // Re-wrap Netstack back into an asynchronous pointer. |
| 56 | netstack_.Bind(sync_netstack.Unbind()); |
Wez | adb2dd70c | 2019-06-01 00:16:44 | [diff] [blame] | 57 | DCHECK(netstack_); |
Jeremy Manson | 18f6ea3 | 2018-11-13 21:12:27 | [diff] [blame] | 58 | netstack_.set_error_handler([](zx_status_t status) { |
Aidan Wolter | 002daa9 | 2019-03-02 06:21:39 | [diff] [blame] | 59 | // TODO(https://crbug.com/901092): Unit tests that use NetworkService are |
| 60 | // crashing because NetworkService does not clean up properly, and the |
| 61 | // netstack connection is cancelled, causing this fatal error. |
| 62 | // When the NetworkService cleanup is fixed, we should make this log FATAL. |
| 63 | ZX_LOG(ERROR, status) << "Lost connection to netstack."; |
Jeremy Manson | 18f6ea3 | 2018-11-13 21:12:27 | [diff] [blame] | 64 | }); |
Wez | 84ac6fe | 2019-02-28 03:45:54 | [diff] [blame] | 65 | netstack_.events().OnInterfacesChanged = fit::bind_member( |
| 66 | this, &NetworkChangeNotifierFuchsia::ProcessInterfaceList); |
Kevin Marshall | 3e89fd7 | 2018-06-05 21:29:10 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | NetworkChangeNotifierFuchsia::~NetworkChangeNotifierFuchsia() { |
| 70 | DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
Paul Jensen | 61a51a1 | 2019-05-23 23:12:49 | [diff] [blame] | 71 | ClearGlobalPointer(); |
Kevin Marshall | 3e89fd7 | 2018-06-05 21:29:10 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | NetworkChangeNotifier::ConnectionType |
| 75 | NetworkChangeNotifierFuchsia::GetCurrentConnectionType() const { |
| 76 | ConnectionType type = static_cast<ConnectionType>( |
| 77 | base::subtle::Acquire_Load(&cached_connection_type_)); |
| 78 | return type; |
| 79 | } |
| 80 | |
Kevin Marshall | 3e89fd7 | 2018-06-05 21:29:10 | [diff] [blame] | 81 | void NetworkChangeNotifierFuchsia::ProcessInterfaceList( |
Wez | 3a12771b | 2019-01-13 06:49:50 | [diff] [blame] | 82 | std::vector<fuchsia::netstack::NetInterface> interfaces) { |
Wez | 84ac6fe | 2019-02-28 03:45:54 | [diff] [blame] | 83 | netstack_->GetRouteTable( |
| 84 | [this, interfaces = std::move(interfaces)]( |
| 85 | std::vector<fuchsia::netstack::RouteTableEntry> route_table) mutable { |
Wez | ec6e7f1 | 2019-05-17 23:02:09 | [diff] [blame] | 86 | OnRouteTableReceived(std::move(interfaces), std::move(route_table), |
| 87 | true); |
Wez | 84ac6fe | 2019-02-28 03:45:54 | [diff] [blame] | 88 | }); |
Kevin Marshall | 3e89fd7 | 2018-06-05 21:29:10 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | void NetworkChangeNotifierFuchsia::OnRouteTableReceived( |
Wez | 3a12771b | 2019-01-13 06:49:50 | [diff] [blame] | 92 | std::vector<fuchsia::netstack::NetInterface> interfaces, |
Wez | ec6e7f1 | 2019-05-17 23:02:09 | [diff] [blame] | 93 | std::vector<fuchsia::netstack::RouteTableEntry> route_table, |
| 94 | bool notify_observers) { |
Aidan Wolter | d89b754 | 2019-01-24 11:01:21 | [diff] [blame] | 95 | // Create a set of NICs that have default routes (ie 0.0.0.0). |
| 96 | base::flat_set<uint32_t> default_route_ids; |
| 97 | for (const auto& route : route_table) { |
| 98 | if (MaskPrefixLength( |
| 99 | internal::FuchsiaIpAddressToIPAddress(route.netmask)) == 0) { |
| 100 | default_route_ids.insert(route.nicid); |
Kevin Marshall | 3e89fd7 | 2018-06-05 21:29:10 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | |
Moe Ahmadi | 5bc17745 | 2019-01-23 18:48:59 | [diff] [blame] | 104 | ConnectionType connection_type = CONNECTION_NONE; |
Aidan Wolter | d89b754 | 2019-01-24 11:01:21 | [diff] [blame] | 105 | base::flat_set<IPAddress> addresses; |
| 106 | for (auto& interface : interfaces) { |
| 107 | // Filter out loopback and invalid connection types. |
| 108 | if ((internal::ConvertConnectionType(interface) == |
| 109 | NetworkChangeNotifier::CONNECTION_NONE) || |
| 110 | (interface.features & |
| 111 | fuchsia::hardware::ethernet::INFO_FEATURE_LOOPBACK)) { |
| 112 | continue; |
| 113 | } |
| 114 | |
| 115 | // Filter out interfaces that do not meet the |required_features_|. |
| 116 | if ((interface.features & required_features_) != required_features_) { |
| 117 | continue; |
| 118 | } |
| 119 | |
| 120 | // Filter out interfaces with non-default routes. |
| 121 | if (!default_route_ids.contains(interface.id)) { |
| 122 | continue; |
| 123 | } |
| 124 | |
Kevin Marshall | 3e89fd7 | 2018-06-05 21:29:10 | [diff] [blame] | 125 | std::vector<NetworkInterface> flattened_interfaces = |
Aidan Wolter | d89b754 | 2019-01-24 11:01:21 | [diff] [blame] | 126 | internal::NetInterfaceToNetworkInterfaces(interface); |
| 127 | if (flattened_interfaces.empty()) { |
| 128 | continue; |
| 129 | } |
| 130 | |
| 131 | // Add the addresses from this interface to the list of all addresses. |
Kevin Marshall | 3e89fd7 | 2018-06-05 21:29:10 | [diff] [blame] | 132 | std::transform( |
| 133 | flattened_interfaces.begin(), flattened_interfaces.end(), |
| 134 | std::inserter(addresses, addresses.begin()), |
| 135 | [](const NetworkInterface& interface) { return interface.address; }); |
Aidan Wolter | d89b754 | 2019-01-24 11:01:21 | [diff] [blame] | 136 | |
| 137 | // Set the default connection to the first interface connection found. |
| 138 | if (connection_type == CONNECTION_NONE) { |
Kevin Marshall | 3e89fd7 | 2018-06-05 21:29:10 | [diff] [blame] | 139 | connection_type = flattened_interfaces.front().type; |
| 140 | } |
Kevin Marshall | 3e89fd7 | 2018-06-05 21:29:10 | [diff] [blame] | 141 | } |
| 142 | |
Kevin Marshall | 3e89fd7 | 2018-06-05 21:29:10 | [diff] [blame] | 143 | if (addresses != cached_addresses_) { |
| 144 | std::swap(cached_addresses_, addresses); |
Wez | ec6e7f1 | 2019-05-17 23:02:09 | [diff] [blame] | 145 | if (notify_observers) |
| 146 | NotifyObserversOfIPAddressChange(); |
Kevin Marshall | 3e89fd7 | 2018-06-05 21:29:10 | [diff] [blame] | 147 | } |
| 148 | |
Wez | ec6e7f1 | 2019-05-17 23:02:09 | [diff] [blame] | 149 | if (connection_type != cached_connection_type_) { |
| 150 | base::subtle::Release_Store(&cached_connection_type_, connection_type); |
| 151 | if (notify_observers) |
| 152 | NotifyObserversOfConnectionTypeChange(); |
Kevin Marshall | 3e89fd7 | 2018-06-05 21:29:10 | [diff] [blame] | 153 | } |
Kevin Marshall | 3e89fd7 | 2018-06-05 21:29:10 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | } // namespace net |