blob: 6a7398a96e37c8f18e9acc338583704e750864e5 [file] [log] [blame]
Kevin Marshall3e89fd72018-06-05 21:29:101// 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 Yanga7939252019-09-19 17:22:237#include <lib/sys/cpp/component_context.h>
Kevin Marshall3e89fd72018-06-05 21:29:108#include <algorithm>
9#include <string>
10#include <utility>
11#include <vector>
12
Sebastien Marchand6d0558fd2019-01-25 16:49:3713#include "base/bind.h"
Sharon Yanga7939252019-09-19 17:22:2314#include "base/fuchsia/default_context.h"
Jeremy Manson18f6ea32018-11-13 21:12:2715#include "base/fuchsia/fuchsia_logging.h"
Kevin Marshall3e89fd72018-06-05 21:29:1016#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
21namespace net {
Kevin Marshall3e89fd72018-06-05 21:29:1022
Aidan Wolterd89b7542019-01-24 11:01:2123NetworkChangeNotifierFuchsia::NetworkChangeNotifierFuchsia(
24 uint32_t required_features)
Kevin Marshall3e89fd72018-06-05 21:29:1025 : NetworkChangeNotifierFuchsia(
Sharon Yanga7939252019-09-19 17:22:2326 base::fuchsia::ComponentContextForCurrentProcess()
27 ->svc()
28 ->Connect<fuchsia::netstack::Netstack>(),
Aidan Wolterd89b7542019-01-24 11:01:2129 required_features) {}
Kevin Marshall3e89fd72018-06-05 21:29:1030
31NetworkChangeNotifierFuchsia::NetworkChangeNotifierFuchsia(
Aidan Wolterd89b7542019-01-24 11:01:2132 fuchsia::netstack::NetstackPtr netstack,
Eric Orthc398f1e2019-07-09 21:54:5533 uint32_t required_features,
34 SystemDnsConfigChangeNotifier* system_dns_config_notifier)
35 : NetworkChangeNotifier(NetworkChangeCalculatorParams(),
36 system_dns_config_notifier),
37 required_features_(required_features) {
Wezec6e7f12019-05-17 23:02:0938 DCHECK(netstack);
Kevin Marshall3e89fd72018-06-05 21:29:1039
Wezec6e7f12019-05-17 23:02:0940 // 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());
Wezadb2dd70c2019-06-01 00:16:4444 DCHECK(sync_netstack);
Wezec6e7f12019-05-17 23:02:0945
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());
Wezadb2dd70c2019-06-01 00:16:4457 DCHECK(netstack_);
Jeremy Manson18f6ea32018-11-13 21:12:2758 netstack_.set_error_handler([](zx_status_t status) {
Aidan Wolter002daa92019-03-02 06:21:3959 // 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 Manson18f6ea32018-11-13 21:12:2764 });
Wez84ac6fe2019-02-28 03:45:5465 netstack_.events().OnInterfacesChanged = fit::bind_member(
66 this, &NetworkChangeNotifierFuchsia::ProcessInterfaceList);
Kevin Marshall3e89fd72018-06-05 21:29:1067}
68
69NetworkChangeNotifierFuchsia::~NetworkChangeNotifierFuchsia() {
70 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
Paul Jensen61a51a12019-05-23 23:12:4971 ClearGlobalPointer();
Kevin Marshall3e89fd72018-06-05 21:29:1072}
73
74NetworkChangeNotifier::ConnectionType
75NetworkChangeNotifierFuchsia::GetCurrentConnectionType() const {
76 ConnectionType type = static_cast<ConnectionType>(
77 base::subtle::Acquire_Load(&cached_connection_type_));
78 return type;
79}
80
Kevin Marshall3e89fd72018-06-05 21:29:1081void NetworkChangeNotifierFuchsia::ProcessInterfaceList(
Wez3a12771b2019-01-13 06:49:5082 std::vector<fuchsia::netstack::NetInterface> interfaces) {
Wez84ac6fe2019-02-28 03:45:5483 netstack_->GetRouteTable(
84 [this, interfaces = std::move(interfaces)](
85 std::vector<fuchsia::netstack::RouteTableEntry> route_table) mutable {
Wezec6e7f12019-05-17 23:02:0986 OnRouteTableReceived(std::move(interfaces), std::move(route_table),
87 true);
Wez84ac6fe2019-02-28 03:45:5488 });
Kevin Marshall3e89fd72018-06-05 21:29:1089}
90
91void NetworkChangeNotifierFuchsia::OnRouteTableReceived(
Wez3a12771b2019-01-13 06:49:5092 std::vector<fuchsia::netstack::NetInterface> interfaces,
Wezec6e7f12019-05-17 23:02:0993 std::vector<fuchsia::netstack::RouteTableEntry> route_table,
94 bool notify_observers) {
Aidan Wolterd89b7542019-01-24 11:01:2195 // 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 Marshall3e89fd72018-06-05 21:29:10101 }
102 }
103
Moe Ahmadi5bc177452019-01-23 18:48:59104 ConnectionType connection_type = CONNECTION_NONE;
Aidan Wolterd89b7542019-01-24 11:01:21105 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 Marshall3e89fd72018-06-05 21:29:10125 std::vector<NetworkInterface> flattened_interfaces =
Aidan Wolterd89b7542019-01-24 11:01:21126 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 Marshall3e89fd72018-06-05 21:29:10132 std::transform(
133 flattened_interfaces.begin(), flattened_interfaces.end(),
134 std::inserter(addresses, addresses.begin()),
135 [](const NetworkInterface& interface) { return interface.address; });
Aidan Wolterd89b7542019-01-24 11:01:21136
137 // Set the default connection to the first interface connection found.
138 if (connection_type == CONNECTION_NONE) {
Kevin Marshall3e89fd72018-06-05 21:29:10139 connection_type = flattened_interfaces.front().type;
140 }
Kevin Marshall3e89fd72018-06-05 21:29:10141 }
142
Kevin Marshall3e89fd72018-06-05 21:29:10143 if (addresses != cached_addresses_) {
144 std::swap(cached_addresses_, addresses);
Wezec6e7f12019-05-17 23:02:09145 if (notify_observers)
146 NotifyObserversOfIPAddressChange();
Kevin Marshall3e89fd72018-06-05 21:29:10147 }
148
Wezec6e7f12019-05-17 23:02:09149 if (connection_type != cached_connection_type_) {
150 base::subtle::Release_Store(&cached_connection_type_, connection_type);
151 if (notify_observers)
152 NotifyObserversOfConnectionTypeChange();
Kevin Marshall3e89fd72018-06-05 21:29:10153 }
Kevin Marshall3e89fd72018-06-05 21:29:10154}
155
156} // namespace net