Garrick Evans | 4987953 | 2018-12-03 13:15:36 +0900 | [diff] [blame] | 1 | // Copyright 2018 The Chromium OS 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 "arc/network/device.h" |
| 6 | |
Garrick Evans | f4a9329 | 2019-03-13 14:19:43 +0900 | [diff] [blame] | 7 | #include <arpa/inet.h> |
| 8 | #include <sys/socket.h> |
Garrick Evans | 4987953 | 2018-12-03 13:15:36 +0900 | [diff] [blame] | 9 | #include <sys/types.h> |
| 10 | |
| 11 | #include <map> |
Garrick Evans | f4a9329 | 2019-03-13 14:19:43 +0900 | [diff] [blame] | 12 | #include <utility> |
Garrick Evans | 4987953 | 2018-12-03 13:15:36 +0900 | [diff] [blame] | 13 | |
| 14 | #include <base/bind.h> |
Garrick Evans | f4a9329 | 2019-03-13 14:19:43 +0900 | [diff] [blame] | 15 | #include <base/lazy_instance.h> |
Garrick Evans | 4987953 | 2018-12-03 13:15:36 +0900 | [diff] [blame] | 16 | #include <base/logging.h> |
Long Cheng | 494fc98 | 2019-09-24 19:09:03 +0000 | [diff] [blame^] | 17 | #include <base/strings/stringprintf.h> |
Garrick Evans | 4987953 | 2018-12-03 13:15:36 +0900 | [diff] [blame] | 18 | |
Long Cheng | e4c8676 | 2019-09-24 18:52:40 +0000 | [diff] [blame] | 19 | #include "arc/network/arc_ip_config.h" |
Hugo Benichi | 2ac4d07 | 2019-05-28 14:51:23 +0900 | [diff] [blame] | 20 | #include "arc/network/net_util.h" |
Garrick Evans | 4987953 | 2018-12-03 13:15:36 +0900 | [diff] [blame] | 21 | |
| 22 | namespace arc_networkd { |
| 23 | |
Garrick Evans | d2bb850 | 2019-02-20 15:59:35 +0900 | [diff] [blame] | 24 | // These are used to identify which ARC++ data path should be used when setting |
| 25 | // up the Android device. |
| 26 | const char kAndroidDevice[] = "arc0"; |
| 27 | const char kAndroidLegacyDevice[] = "android"; |
Garrick Evans | 4987953 | 2018-12-03 13:15:36 +0900 | [diff] [blame] | 28 | |
| 29 | namespace { |
Long Cheng | 494fc98 | 2019-09-24 19:09:03 +0000 | [diff] [blame^] | 30 | |
Hugo Benichi | 2ac4d07 | 2019-05-28 14:51:23 +0900 | [diff] [blame] | 31 | constexpr uint32_t kMdnsMcastAddress = Ipv4Addr(224, 0, 0, 251); |
Garrick Evans | f4a9329 | 2019-03-13 14:19:43 +0900 | [diff] [blame] | 32 | constexpr uint16_t kMdnsPort = 5353; |
Hugo Benichi | 2ac4d07 | 2019-05-28 14:51:23 +0900 | [diff] [blame] | 33 | constexpr uint32_t kSsdpMcastAddress = Ipv4Addr(239, 255, 255, 250); |
Garrick Evans | f4a9329 | 2019-03-13 14:19:43 +0900 | [diff] [blame] | 34 | constexpr uint16_t kSsdpPort = 1900; |
Long Cheng | 494fc98 | 2019-09-24 19:09:03 +0000 | [diff] [blame^] | 35 | |
Garrick Evans | f4a9329 | 2019-03-13 14:19:43 +0900 | [diff] [blame] | 36 | constexpr int kMaxRandomAddressTries = 3; |
Long Cheng | 494fc98 | 2019-09-24 19:09:03 +0000 | [diff] [blame^] | 37 | |
| 38 | std::string MacAddressToString(const MacAddress& addr) { |
| 39 | return base::StringPrintf("%02x:%02x:%02x:%02x:%02x:%02x", addr[0], addr[1], |
| 40 | addr[2], addr[3], addr[4], addr[5]); |
| 41 | } |
| 42 | |
Garrick Evans | 4987953 | 2018-12-03 13:15:36 +0900 | [diff] [blame] | 43 | } // namespace |
| 44 | |
Garrick Evans | f4a9329 | 2019-03-13 14:19:43 +0900 | [diff] [blame] | 45 | Device::Config::Config(const std::string& host_ifname, |
| 46 | const std::string& guest_ifname, |
| 47 | const MacAddress& guest_mac_addr, |
| 48 | std::unique_ptr<Subnet> ipv4_subnet, |
| 49 | std::unique_ptr<SubnetAddress> host_ipv4_addr, |
| 50 | std::unique_ptr<SubnetAddress> guest_ipv4_addr) |
| 51 | : host_ifname_(host_ifname), |
| 52 | guest_ifname_(guest_ifname), |
| 53 | guest_mac_addr_(guest_mac_addr), |
| 54 | ipv4_subnet_(std::move(ipv4_subnet)), |
| 55 | host_ipv4_addr_(std::move(host_ipv4_addr)), |
| 56 | guest_ipv4_addr_(std::move(guest_ipv4_addr)) {} |
| 57 | |
Garrick Evans | 428e476 | 2018-12-11 15:18:42 +0900 | [diff] [blame] | 58 | Device::Device(const std::string& ifname, |
Garrick Evans | f4a9329 | 2019-03-13 14:19:43 +0900 | [diff] [blame] | 59 | std::unique_ptr<Device::Config> config, |
Long Cheng | 994dfd3 | 2019-09-24 18:50:27 +0000 | [diff] [blame] | 60 | const Device::Options& options, |
| 61 | const MessageSink& msg_sink) |
Garrick Evans | f4a9329 | 2019-03-13 14:19:43 +0900 | [diff] [blame] | 62 | : ifname_(ifname), |
| 63 | config_(std::move(config)), |
| 64 | options_(options), |
Long Cheng | 494fc98 | 2019-09-24 19:09:03 +0000 | [diff] [blame^] | 65 | msg_sink_(msg_sink) { |
Garrick Evans | f4a9329 | 2019-03-13 14:19:43 +0900 | [diff] [blame] | 66 | DCHECK(config_); |
Long Cheng | e4c8676 | 2019-09-24 18:52:40 +0000 | [diff] [blame] | 67 | if (msg_sink_.is_null()) |
| 68 | return; |
| 69 | |
| 70 | DeviceMessage msg; |
| 71 | msg.set_dev_ifname(ifname_); |
| 72 | auto* dev_config = msg.mutable_dev_config(); |
| 73 | FillProto(dev_config); |
| 74 | msg_sink_.Run(msg); |
| 75 | } |
| 76 | |
| 77 | Device::~Device() { |
| 78 | if (msg_sink_.is_null()) |
| 79 | return; |
| 80 | |
| 81 | DeviceMessage msg; |
| 82 | msg.set_dev_ifname(ifname_); |
| 83 | msg.set_teardown(true); |
| 84 | msg_sink_.Run(msg); |
Garrick Evans | 4987953 | 2018-12-03 13:15:36 +0900 | [diff] [blame] | 85 | } |
| 86 | |
Long Cheng | 994dfd3 | 2019-09-24 18:50:27 +0000 | [diff] [blame] | 87 | void Device::FillProto(DeviceConfig* msg) const { |
| 88 | msg->set_br_ifname(config_->host_ifname()); |
| 89 | msg->set_br_ipv4(IPv4AddressToString(config_->host_ipv4_addr())); |
| 90 | msg->set_arc_ifname(config_->guest_ifname()); |
| 91 | msg->set_arc_ipv4(IPv4AddressToString(config_->guest_ipv4_addr())); |
| 92 | msg->set_mac_addr(MacAddressToString(config_->guest_mac_addr())); |
| 93 | |
| 94 | msg->set_fwd_multicast(options_.fwd_multicast); |
| 95 | msg->set_find_ipv6_routes(options_.find_ipv6_routes); |
| 96 | } |
| 97 | |
Garrick Evans | 894abc2 | 2019-06-07 10:49:02 +0900 | [diff] [blame] | 98 | Device::Config& Device::config() const { |
| 99 | CHECK(config_); |
| 100 | return *config_.get(); |
| 101 | } |
Garrick Evans | d2bb850 | 2019-02-20 15:59:35 +0900 | [diff] [blame] | 102 | |
Garrick Evans | ac982ea | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 103 | void Device::Enable(const std::string& ifname) { |
Long Cheng | 494fc98 | 2019-09-24 19:09:03 +0000 | [diff] [blame^] | 104 | // If operating in legacy single network mode, enable inbound traffic to ARC |
| 105 | // from the interface. |
| 106 | // TODO(b/77293260) Also enable inbound traffic rules specific to the input |
| 107 | // physical interface in multinetworking mode. |
| 108 | if (ifname_ == kAndroidLegacyDevice) { |
| 109 | LOG(INFO) << "Binding interface " << ifname << " to device " << ifname_; |
| 110 | legacy_lan_ifname_ = ifname; |
| 111 | |
| 112 | if (!msg_sink_.is_null()) { |
| 113 | DeviceMessage msg; |
| 114 | msg.set_dev_ifname(ifname_); |
| 115 | msg.set_enable_inbound_ifname(legacy_lan_ifname_); |
| 116 | msg_sink_.Run(msg); |
| 117 | } |
| 118 | } |
Garrick Evans | ac982ea | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 119 | |
Garrick Evans | f4a9329 | 2019-03-13 14:19:43 +0900 | [diff] [blame] | 120 | if (options_.fwd_multicast) { |
Garrick Evans | 894abc2 | 2019-06-07 10:49:02 +0900 | [diff] [blame] | 121 | if (!mdns_forwarder_) { |
| 122 | LOG(INFO) << "Enabling mDNS forwarding for device " << ifname_; |
| 123 | auto mdns_fwd = std::make_unique<MulticastForwarder>(); |
| 124 | if (mdns_fwd->Start(config_->host_ifname(), ifname, |
| 125 | config_->guest_ipv4_addr(), kMdnsMcastAddress, |
| 126 | kMdnsPort, |
| 127 | /* allow_stateless */ true)) { |
| 128 | mdns_forwarder_ = std::move(mdns_fwd); |
| 129 | } else { |
| 130 | LOG(WARNING) << "mDNS forwarder could not be started on " << ifname_; |
| 131 | } |
Hugo Benichi | dcce114 | 2019-06-17 10:52:15 +0900 | [diff] [blame] | 132 | } |
Garrick Evans | f4a9329 | 2019-03-13 14:19:43 +0900 | [diff] [blame] | 133 | |
Garrick Evans | 894abc2 | 2019-06-07 10:49:02 +0900 | [diff] [blame] | 134 | if (!ssdp_forwarder_) { |
| 135 | LOG(INFO) << "Enabling SSDP forwarding for device " << ifname_; |
| 136 | auto ssdp_fwd = std::make_unique<MulticastForwarder>(); |
| 137 | if (ssdp_fwd->Start(config_->host_ifname(), ifname, htonl(INADDR_ANY), |
| 138 | kSsdpMcastAddress, kSsdpPort, |
| 139 | /* allow_stateless */ false)) { |
| 140 | ssdp_forwarder_ = std::move(ssdp_fwd); |
| 141 | } else { |
| 142 | LOG(WARNING) << "SSDP forwarder could not be started on " << ifname_; |
| 143 | } |
Hugo Benichi | dcce114 | 2019-06-17 10:52:15 +0900 | [diff] [blame] | 144 | } |
Garrick Evans | 428e476 | 2018-12-11 15:18:42 +0900 | [diff] [blame] | 145 | } |
Garrick Evans | 4987953 | 2018-12-03 13:15:36 +0900 | [diff] [blame] | 146 | |
Garrick Evans | 894abc2 | 2019-06-07 10:49:02 +0900 | [diff] [blame] | 147 | if (options_.find_ipv6_routes && !router_finder_) { |
Long Cheng | 494fc98 | 2019-09-24 19:09:03 +0000 | [diff] [blame^] | 148 | LOG(INFO) << "Enabling IPV6 route finding for device " << ifname_; |
Garrick Evans | 428e476 | 2018-12-11 15:18:42 +0900 | [diff] [blame] | 149 | router_finder_.reset(new RouterFinder()); |
| 150 | router_finder_->Start( |
Hugo Benichi | ee787ff | 2019-05-20 16:42:42 +0900 | [diff] [blame] | 151 | ifname, base::Bind(&Device::OnRouteFound, weak_factory_.GetWeakPtr())); |
Garrick Evans | 428e476 | 2018-12-11 15:18:42 +0900 | [diff] [blame] | 152 | } |
Garrick Evans | 4987953 | 2018-12-03 13:15:36 +0900 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | void Device::Disable() { |
Long Cheng | e4c8676 | 2019-09-24 18:52:40 +0000 | [diff] [blame] | 156 | LOG(INFO) << "Disabling device " << ifname_; |
| 157 | |
| 158 | neighbor_finder_.reset(); |
| 159 | router_finder_.reset(); |
| 160 | ssdp_forwarder_.reset(); |
| 161 | mdns_forwarder_.reset(); |
| 162 | |
| 163 | if (msg_sink_.is_null()) |
| 164 | return; |
| 165 | |
| 166 | // Clear IPv6 info, if necessary. |
| 167 | if (options_.find_ipv6_routes) { |
| 168 | DeviceMessage msg; |
| 169 | msg.set_dev_ifname(ifname_); |
| 170 | msg.set_clear_arc_ip(true); |
| 171 | msg_sink_.Run(msg); |
Garrick Evans | 4987953 | 2018-12-03 13:15:36 +0900 | [diff] [blame] | 172 | } |
Garrick Evans | 4987953 | 2018-12-03 13:15:36 +0900 | [diff] [blame] | 173 | |
Long Cheng | 494fc98 | 2019-09-24 19:09:03 +0000 | [diff] [blame^] | 174 | // Disable inbound traffic. |
| 175 | // TODO(b/77293260) Also disable inbound traffic rules in multinetworking |
| 176 | // mode. |
| 177 | if (ifname_ == kAndroidLegacyDevice && !legacy_lan_ifname_.empty()) { |
| 178 | LOG(INFO) << "Unbinding interface " << legacy_lan_ifname_ << " from device " |
| 179 | << ifname_; |
| 180 | legacy_lan_ifname_.clear(); |
Garrick Evans | ac982ea | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 181 | |
Long Cheng | 494fc98 | 2019-09-24 19:09:03 +0000 | [diff] [blame^] | 182 | DeviceMessage msg; |
| 183 | msg.set_dev_ifname(ifname_); |
| 184 | msg.set_disable_inbound(true); |
| 185 | msg_sink_.Run(msg); |
| 186 | } |
| 187 | } |
Garrick Evans | ac982ea | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 188 | |
Garrick Evans | 4987953 | 2018-12-03 13:15:36 +0900 | [diff] [blame] | 189 | void Device::OnRouteFound(const struct in6_addr& prefix, |
| 190 | int prefix_len, |
| 191 | const struct in6_addr& router) { |
Long Cheng | e4c8676 | 2019-09-24 18:52:40 +0000 | [diff] [blame] | 192 | const std::string& ifname = |
| 193 | legacy_lan_ifname_.empty() ? ifname_ : legacy_lan_ifname_; |
| 194 | |
| 195 | if (prefix_len == 64) { |
| 196 | LOG(INFO) << "Found IPv6 network on iface " << ifname << " route=" << prefix |
| 197 | << "/" << prefix_len << ", gateway=" << router; |
| 198 | |
| 199 | memcpy(&random_address_, &prefix, sizeof(random_address_)); |
| 200 | random_address_prefix_len_ = prefix_len; |
| 201 | random_address_tries_ = 0; |
| 202 | |
| 203 | ArcIpConfig::GenerateRandom(&random_address_, random_address_prefix_len_); |
| 204 | |
| 205 | neighbor_finder_.reset(new NeighborFinder()); |
| 206 | neighbor_finder_->Check( |
| 207 | ifname, random_address_, |
| 208 | base::Bind(&Device::OnNeighborCheckResult, weak_factory_.GetWeakPtr())); |
| 209 | } else { |
| 210 | LOG(INFO) << "No IPv6 connectivity available on " << ifname; |
Garrick Evans | 4987953 | 2018-12-03 13:15:36 +0900 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | |
| 214 | void Device::OnNeighborCheckResult(bool found) { |
Long Cheng | e4c8676 | 2019-09-24 18:52:40 +0000 | [diff] [blame] | 215 | const std::string& ifname = |
| 216 | legacy_lan_ifname_.empty() ? ifname_ : legacy_lan_ifname_; |
| 217 | |
Garrick Evans | 4987953 | 2018-12-03 13:15:36 +0900 | [diff] [blame] | 218 | if (found) { |
Long Cheng | e4c8676 | 2019-09-24 18:52:40 +0000 | [diff] [blame] | 219 | if (++random_address_tries_ >= kMaxRandomAddressTries) { |
| 220 | LOG(WARNING) << "Too many IP collisions, giving up."; |
Garrick Evans | 4987953 | 2018-12-03 13:15:36 +0900 | [diff] [blame] | 221 | return; |
| 222 | } |
| 223 | |
Long Cheng | e4c8676 | 2019-09-24 18:52:40 +0000 | [diff] [blame] | 224 | struct in6_addr previous_address = random_address_; |
| 225 | ArcIpConfig::GenerateRandom(&random_address_, random_address_prefix_len_); |
Garrick Evans | 4987953 | 2018-12-03 13:15:36 +0900 | [diff] [blame] | 226 | |
| 227 | LOG(INFO) << "Detected IP collision for " << previous_address |
Long Cheng | e4c8676 | 2019-09-24 18:52:40 +0000 | [diff] [blame] | 228 | << ", retrying with new address " << random_address_; |
Garrick Evans | 4987953 | 2018-12-03 13:15:36 +0900 | [diff] [blame] | 229 | |
| 230 | neighbor_finder_->Check( |
Long Cheng | e4c8676 | 2019-09-24 18:52:40 +0000 | [diff] [blame] | 231 | ifname, random_address_, |
Garrick Evans | 4987953 | 2018-12-03 13:15:36 +0900 | [diff] [blame] | 232 | base::Bind(&Device::OnNeighborCheckResult, weak_factory_.GetWeakPtr())); |
Long Cheng | e4c8676 | 2019-09-24 18:52:40 +0000 | [diff] [blame] | 233 | } else { |
| 234 | struct in6_addr router; |
Garrick Evans | 6467aba | 2019-07-25 11:22:50 +0900 | [diff] [blame] | 235 | |
Long Cheng | e4c8676 | 2019-09-24 18:52:40 +0000 | [diff] [blame] | 236 | if (!ArcIpConfig::GetV6Address(config_->host_ifname(), &router)) { |
| 237 | LOG(ERROR) << "Error reading link local address for " |
| 238 | << config_->host_ifname(); |
| 239 | return; |
| 240 | } |
Garrick Evans | 6467aba | 2019-07-25 11:22:50 +0900 | [diff] [blame] | 241 | |
Long Cheng | e4c8676 | 2019-09-24 18:52:40 +0000 | [diff] [blame] | 242 | LOG(INFO) << "Setting IPv6 address " << random_address_ |
| 243 | << "/128, gateway=" << router << " on " << ifname; |
| 244 | |
| 245 | // Set up new ARC IPv6 address, NDP, and forwarding rules. |
| 246 | if (!msg_sink_.is_null()) { |
| 247 | DeviceMessage msg; |
| 248 | msg.set_dev_ifname(ifname_); |
| 249 | SetArcIp* setup_msg = msg.mutable_set_arc_ip(); |
| 250 | setup_msg->set_prefix(&random_address_, sizeof(struct in6_addr)); |
| 251 | setup_msg->set_prefix_len(128); |
| 252 | setup_msg->set_router(&router, sizeof(struct in6_addr)); |
| 253 | setup_msg->set_lan_ifname(ifname); |
| 254 | msg_sink_.Run(msg); |
| 255 | } |
| 256 | } |
Garrick Evans | 4987953 | 2018-12-03 13:15:36 +0900 | [diff] [blame] | 257 | } |
| 258 | |
Hugo Benichi | ee787ff | 2019-05-20 16:42:42 +0900 | [diff] [blame] | 259 | std::ostream& operator<<(std::ostream& stream, const Device& device) { |
Long Cheng | e4c8676 | 2019-09-24 18:52:40 +0000 | [diff] [blame] | 260 | stream << "{ ifname: " << device.ifname_; |
| 261 | if (!device.legacy_lan_ifname_.empty()) |
| 262 | stream << ", legacy_lan_ifname: " << device.legacy_lan_ifname_; |
| 263 | stream << ", bridge_ifname: " << device.config_->host_ifname() |
Hugo Benichi | bd8ec4d | 2019-05-28 12:52:49 +0900 | [diff] [blame] | 264 | << ", bridge_ipv4_addr: " |
| 265 | << device.config_->host_ipv4_addr_->ToCidrString() |
| 266 | << ", guest_ifname: " << device.config_->guest_ifname() |
| 267 | << ", guest_ipv4_addr: " |
| 268 | << device.config_->guest_ipv4_addr_->ToCidrString() |
| 269 | << ", guest_mac_addr: " |
Hugo Benichi | ee787ff | 2019-05-20 16:42:42 +0900 | [diff] [blame] | 270 | << MacAddressToString(device.config_->guest_mac_addr()) |
Hugo Benichi | bd8ec4d | 2019-05-28 12:52:49 +0900 | [diff] [blame] | 271 | << ", fwd_multicast: " << device.options_.fwd_multicast |
| 272 | << ", find_ipv6_routes: " << device.options_.find_ipv6_routes << '}'; |
Hugo Benichi | ee787ff | 2019-05-20 16:42:42 +0900 | [diff] [blame] | 273 | return stream; |
| 274 | } |
| 275 | |
Garrick Evans | 4987953 | 2018-12-03 13:15:36 +0900 | [diff] [blame] | 276 | } // namespace arc_networkd |