Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 1 | // Copyright 2019 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/arc_service.h" |
| 6 | |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 7 | #include <linux/rtnetlink.h> |
| 8 | #include <net/if.h> |
| 9 | |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 10 | #include <utility> |
Jason Jeremy Iman | f4156cb | 2019-11-14 15:36:22 +0900 | [diff] [blame] | 11 | #include <vector> |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 12 | |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 13 | #include <base/bind.h> |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 14 | #include <base/files/file_path.h> |
| 15 | #include <base/files/file_util.h> |
| 16 | #include <base/logging.h> |
| 17 | #include <base/strings/string_number_conversions.h> |
| 18 | #include <base/strings/string_util.h> |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 19 | #include <base/strings/stringprintf.h> |
Garrick Evans | 1f5a361 | 2019-11-08 12:59:03 +0900 | [diff] [blame] | 20 | #include <brillo/key_value_store.h> |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 21 | #include <chromeos/constants/vm_tools.h> |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 22 | #include <shill/net/rtnl_message.h> |
| 23 | |
| 24 | #include "arc/network/datapath.h" |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 25 | #include "arc/network/mac_address_generator.h" |
Jason Jeremy Iman | f4156cb | 2019-11-14 15:36:22 +0900 | [diff] [blame] | 26 | #include "arc/network/manager.h" |
Garrick Evans | 3915af3 | 2019-07-25 15:44:34 +0900 | [diff] [blame] | 27 | #include "arc/network/minijailed_process_runner.h" |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 28 | #include "arc/network/net_util.h" |
| 29 | #include "arc/network/scoped_ns.h" |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 30 | |
| 31 | namespace arc_networkd { |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 32 | |
| 33 | namespace test { |
| 34 | GuestMessage::GuestType guest = GuestMessage::UNKNOWN_GUEST; |
| 35 | } // namespace test |
| 36 | |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 37 | namespace { |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 38 | constexpr pid_t kInvalidPID = 0; |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 39 | constexpr pid_t kTestPID = -2; |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 40 | constexpr uint32_t kInvalidCID = 0; |
Garrick Evans | 260ff30 | 2019-07-25 11:22:50 +0900 | [diff] [blame] | 41 | constexpr int kInvalidTableID = -1; |
| 42 | constexpr int kMaxTableRetries = 10; // Based on 1 second delay. |
| 43 | constexpr base::TimeDelta kTableRetryDelay = base::TimeDelta::FromSeconds(1); |
| 44 | // Android adds a constant to the interface index to derive the table id. |
| 45 | // This is defined in system/netd/server/RouteController.h |
| 46 | constexpr int kRouteControllerRouteTableOffsetFromIndex = 1000; |
Jason Jeremy Iman | f4156cb | 2019-11-14 15:36:22 +0900 | [diff] [blame] | 47 | constexpr int kMultinetMinAndroidSdkVersion = 28; |
| 48 | constexpr char kMultinetFeatureName[] = "ARC multi-networking"; |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 49 | |
| 50 | // This wrapper is required since the base class is a singleton that hides its |
| 51 | // constructor. It is necessary here because the message loop thread has to be |
| 52 | // reassociated to the container's network namespace; and since the container |
| 53 | // can be repeatedly created and destroyed, the handler must be as well. |
| 54 | class RTNetlinkHandler : public shill::RTNLHandler { |
| 55 | public: |
| 56 | RTNetlinkHandler() = default; |
| 57 | ~RTNetlinkHandler() = default; |
| 58 | |
| 59 | private: |
| 60 | DISALLOW_COPY_AND_ASSIGN(RTNetlinkHandler); |
| 61 | }; |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 62 | |
Garrick Evans | 260ff30 | 2019-07-25 11:22:50 +0900 | [diff] [blame] | 63 | int GetAndroidRoutingTableId(const std::string& ifname, pid_t pid) { |
| 64 | base::FilePath ifindex_path(base::StringPrintf( |
| 65 | "/proc/%d/root/sys/class/net/%s/ifindex", pid, ifname.c_str())); |
| 66 | std::string contents; |
| 67 | if (!base::ReadFileToString(ifindex_path, &contents)) { |
| 68 | PLOG(WARNING) << "Could not read " << ifindex_path.value(); |
| 69 | return kInvalidTableID; |
| 70 | } |
| 71 | |
| 72 | base::TrimWhitespaceASCII(contents, base::TRIM_TRAILING, &contents); |
| 73 | int table_id = kInvalidTableID; |
| 74 | if (!base::StringToInt(contents, &table_id)) { |
| 75 | LOG(ERROR) << "Could not parse ifindex from " << ifindex_path.value() |
| 76 | << ": " << contents; |
| 77 | return kInvalidTableID; |
| 78 | } |
| 79 | table_id += kRouteControllerRouteTableOffsetFromIndex; |
| 80 | |
| 81 | LOG(INFO) << "Found table id " << table_id << " for container interface " |
| 82 | << ifname; |
| 83 | return table_id; |
| 84 | } |
| 85 | |
Garrick Evans | 6d227b9 | 2019-12-03 16:11:29 +0900 | [diff] [blame] | 86 | void OneTimeSetup(const Datapath& datapath) { |
Garrick Evans | a34b586 | 2019-11-20 09:34:01 +0900 | [diff] [blame] | 87 | static bool done = false; |
| 88 | if (done) |
| 89 | return; |
| 90 | |
Garrick Evans | 6d227b9 | 2019-12-03 16:11:29 +0900 | [diff] [blame] | 91 | auto& runner = datapath.runner(); |
| 92 | |
| 93 | // Load networking modules needed by Android that are not compiled in the |
| 94 | // kernel. Android does not allow auto-loading of kernel modules. |
Garrick Evans | a34b586 | 2019-11-20 09:34:01 +0900 | [diff] [blame] | 95 | // These must succeed. |
Garrick Evans | 8e8e347 | 2020-01-23 14:03:50 +0900 | [diff] [blame] | 96 | if (runner.modprobe_all({ |
Garrick Evans | a34b586 | 2019-11-20 09:34:01 +0900 | [diff] [blame] | 97 | // The netfilter modules needed by netd for iptables commands. |
| 98 | "ip6table_filter", |
| 99 | "ip6t_ipv6header", |
| 100 | "ip6t_REJECT", |
| 101 | // The xfrm modules needed for Android's ipsec APIs. |
| 102 | "xfrm4_mode_transport", |
| 103 | "xfrm4_mode_tunnel", |
| 104 | "xfrm6_mode_transport", |
| 105 | "xfrm6_mode_tunnel", |
| 106 | // The ipsec modules for AH and ESP encryption for ipv6. |
| 107 | "ah6", |
| 108 | "esp6", |
| 109 | }) != 0) { |
| 110 | LOG(ERROR) << "One or more required kernel modules failed to load." |
| 111 | << " Some Android functionality may be broken."; |
| 112 | } |
| 113 | // Optional modules. |
Garrick Evans | 8e8e347 | 2020-01-23 14:03:50 +0900 | [diff] [blame] | 114 | if (runner.modprobe_all({ |
Garrick Evans | a34b586 | 2019-11-20 09:34:01 +0900 | [diff] [blame] | 115 | // This module is not available in kernels < 3.18 |
| 116 | "nf_reject_ipv6", |
| 117 | // These modules are needed for supporting Chrome traffic on Android |
| 118 | // VPN which uses Android's NAT feature. Android NAT sets up |
| 119 | // iptables |
| 120 | // rules that use these conntrack modules for FTP/TFTP. |
| 121 | "nf_nat_ftp", |
| 122 | "nf_nat_tftp", |
Hugo Benichi | a0cde9e | 2019-12-16 11:57:20 +0900 | [diff] [blame] | 123 | // The tun module is needed by the Android 464xlat clatd process. |
| 124 | "tun", |
Garrick Evans | a34b586 | 2019-11-20 09:34:01 +0900 | [diff] [blame] | 125 | }) != 0) { |
| 126 | LOG(WARNING) << "One or more optional kernel modules failed to load."; |
| 127 | } |
| 128 | |
Garrick Evans | 6d227b9 | 2019-12-03 16:11:29 +0900 | [diff] [blame] | 129 | // This is only needed for CTS (b/27932574). |
Garrick Evans | 8e8e347 | 2020-01-23 14:03:50 +0900 | [diff] [blame] | 130 | if (runner.chown("655360", "655360", "/sys/class/xt_idletimer") != 0) { |
Garrick Evans | 6d227b9 | 2019-12-03 16:11:29 +0900 | [diff] [blame] | 131 | LOG(ERROR) << "Failed to change ownership of xt_idletimer."; |
| 132 | } |
| 133 | |
Garrick Evans | a34b586 | 2019-11-20 09:34:01 +0900 | [diff] [blame] | 134 | done = true; |
| 135 | } |
| 136 | |
Garrick Evans | 508a4bc | 2019-11-14 08:45:52 +0900 | [diff] [blame] | 137 | bool IsArcVm() { |
| 138 | const base::FilePath path("/run/chrome/is_arcvm"); |
| 139 | std::string contents; |
| 140 | if (!base::ReadFileToString(path, &contents)) { |
| 141 | PLOG(ERROR) << "Could not read " << path.value(); |
| 142 | } |
| 143 | return contents == "1"; |
| 144 | } |
| 145 | |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 146 | GuestMessage::GuestType ArcGuest() { |
| 147 | if (test::guest != GuestMessage::UNKNOWN_GUEST) |
| 148 | return test::guest; |
Garrick Evans | 508a4bc | 2019-11-14 08:45:52 +0900 | [diff] [blame] | 149 | |
| 150 | return IsArcVm() ? GuestMessage::ARC_VM |
Jason Jeremy Iman | f4156cb | 2019-11-14 15:36:22 +0900 | [diff] [blame] | 151 | : Manager::ShouldEnableFeature(kMultinetMinAndroidSdkVersion, |
| 152 | 0, std::vector<std::string>(), |
| 153 | kMultinetFeatureName) |
| 154 | ? GuestMessage::ARC |
| 155 | : GuestMessage::ARC_LEGACY; |
Garrick Evans | 508a4bc | 2019-11-14 08:45:52 +0900 | [diff] [blame] | 156 | } |
| 157 | |
Garrick Evans | bc91a35 | 2020-01-16 16:43:26 +0900 | [diff] [blame] | 158 | constexpr const char kArcDevicePrefix[] = "arc"; |
| 159 | bool IsArcDevice(const std::string& ifname) { |
| 160 | return base::StartsWith(ifname, kArcDevicePrefix, |
| 161 | base::CompareCase::INSENSITIVE_ASCII); |
| 162 | } |
| 163 | |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 164 | } // namespace |
| 165 | |
Garrick Evans | 69b8587 | 2020-02-04 11:40:26 +0900 | [diff] [blame^] | 166 | ArcService::ArcService(ShillClient* shill_client, |
| 167 | DeviceManagerBase* dev_mgr, |
| 168 | Datapath* datapath) |
| 169 | : shill_client_(shill_client), dev_mgr_(dev_mgr), datapath_(datapath) { |
| 170 | DCHECK(shill_client_); |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 171 | DCHECK(dev_mgr_); |
Taoyu Li | 179dcc6 | 2019-10-17 11:21:08 +0900 | [diff] [blame] | 172 | DCHECK(datapath_); |
Garrick Evans | 3915af3 | 2019-07-25 15:44:34 +0900 | [diff] [blame] | 173 | |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 174 | dev_mgr_->RegisterDeviceAddedHandler( |
| 175 | GuestMessage::ARC, |
| 176 | base::Bind(&ArcService::OnDeviceAdded, base::Unretained(this))); |
| 177 | dev_mgr_->RegisterDeviceRemovedHandler( |
| 178 | GuestMessage::ARC, |
| 179 | base::Bind(&ArcService::OnDeviceRemoved, base::Unretained(this))); |
| 180 | dev_mgr_->RegisterDefaultInterfaceChangedHandler( |
| 181 | GuestMessage::ARC, base::Bind(&ArcService::OnDefaultInterfaceChanged, |
| 182 | base::Unretained(this))); |
| 183 | } |
| 184 | |
| 185 | ArcService::~ArcService() { |
Garrick Evans | 664a82f | 2019-12-17 12:18:05 +0900 | [diff] [blame] | 186 | if (impl_) { |
| 187 | // Stop the service. |
| 188 | Stop(impl_->id()); |
| 189 | // Delete all the bridges and veth pairs. |
| 190 | dev_mgr_->ProcessDevices( |
| 191 | base::Bind(&ArcService::OnDeviceRemoved, weak_factory_.GetWeakPtr())); |
| 192 | } |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 193 | dev_mgr_->UnregisterAllGuestHandlers(GuestMessage::ARC); |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 194 | } |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 195 | |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 196 | bool ArcService::Start(uint32_t id) { |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 197 | if (impl_) { |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 198 | uint32_t prev_id; |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 199 | if (impl_->IsStarted(&prev_id)) { |
| 200 | LOG(WARNING) << "Already running - did something crash?" |
| 201 | << " Stopping and restarting..."; |
| 202 | Stop(prev_id); |
| 203 | } |
Garrick Evans | a51d0a1 | 2019-11-28 13:51:23 +0900 | [diff] [blame] | 204 | } |
| 205 | |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 206 | const auto guest = ArcGuest(); |
| 207 | if (guest == GuestMessage::ARC_VM) |
| 208 | impl_ = std::make_unique<VmImpl>(dev_mgr_, datapath_); |
| 209 | else |
| 210 | impl_ = std::make_unique<ContainerImpl>(dev_mgr_, datapath_, guest); |
| 211 | |
| 212 | if (!impl_->Start(id)) { |
| 213 | impl_.reset(); |
Garrick Evans | 508a4bc | 2019-11-14 08:45:52 +0900 | [diff] [blame] | 214 | return false; |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 215 | } |
Garrick Evans | cb791e7 | 2019-11-11 15:44:34 +0900 | [diff] [blame] | 216 | |
| 217 | // Start known host devices, any new ones will be setup in the process. |
| 218 | dev_mgr_->ProcessDevices( |
| 219 | base::Bind(&ArcService::StartDevice, weak_factory_.GetWeakPtr())); |
| 220 | |
| 221 | // If this is the first time the service is starting this will create the |
| 222 | // Android bridge device; otherwise it does nothing (this is a workaround for |
| 223 | // the bug in Shill that casues a Bus crash when it sees the ARC bridge a |
| 224 | // second time). Do this after processing the existing devices so it doesn't |
| 225 | // get started twice. |
Garrick Evans | cfd4282 | 2019-11-15 12:38:23 +0900 | [diff] [blame] | 226 | std::string arc; |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 227 | switch (guest) { |
Garrick Evans | cfd4282 | 2019-11-15 12:38:23 +0900 | [diff] [blame] | 228 | case GuestMessage::ARC: |
| 229 | arc = kAndroidDevice; |
| 230 | break; |
| 231 | case GuestMessage::ARC_LEGACY: |
| 232 | arc = kAndroidLegacyDevice; |
| 233 | break; |
| 234 | case GuestMessage::ARC_VM: |
| 235 | arc = kAndroidVmDevice; |
| 236 | break; |
| 237 | default: |
| 238 | LOG(DFATAL) << "Unexpected guest: " << guest; |
Garrick Evans | 21173b1 | 2019-11-20 15:23:16 +0900 | [diff] [blame] | 239 | return false; |
Garrick Evans | cfd4282 | 2019-11-15 12:38:23 +0900 | [diff] [blame] | 240 | } |
| 241 | dev_mgr_->Add(arc); |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 242 | dev_mgr_->OnGuestStart(guest); |
| 243 | return true; |
Garrick Evans | cb791e7 | 2019-11-11 15:44:34 +0900 | [diff] [blame] | 244 | } |
| 245 | |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 246 | void ArcService::Stop(uint32_t id) { |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 247 | if (!impl_) |
| 248 | return; |
| 249 | |
| 250 | dev_mgr_->OnGuestStop(impl_->guest()); |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 251 | |
| 252 | // Stop known host devices. Note that this does not teardown any existing |
| 253 | // devices. |
| 254 | dev_mgr_->ProcessDevices( |
| 255 | base::Bind(&ArcService::StopDevice, weak_factory_.GetWeakPtr())); |
Garrick Evans | 21173b1 | 2019-11-20 15:23:16 +0900 | [diff] [blame] | 256 | |
| 257 | impl_->Stop(id); |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 258 | impl_.reset(); |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 259 | } |
| 260 | |
Garrick Evans | 8ff0845 | 2019-11-25 09:24:26 +0900 | [diff] [blame] | 261 | bool ArcService::AllowDevice(Device* device) const { |
Garrick Evans | a1134d7 | 2019-12-02 14:25:37 +0900 | [diff] [blame] | 262 | if (!device->IsArc()) |
Garrick Evans | 0001d01 | 2019-11-22 10:06:10 +0900 | [diff] [blame] | 263 | return false; |
| 264 | |
Garrick Evans | 8ff0845 | 2019-11-25 09:24:26 +0900 | [diff] [blame] | 265 | // ARC P+ is multi-network enabled and should process all devices. |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 266 | if ((impl_ && impl_->guest() == GuestMessage::ARC) || |
| 267 | (!impl_ && ArcGuest() == GuestMessage::ARC)) |
Garrick Evans | 8ff0845 | 2019-11-25 09:24:26 +0900 | [diff] [blame] | 268 | return true; |
| 269 | |
| 270 | // ARC N and ARCVM (for now) are both single-network - meaning they only use |
| 271 | // the "default" device which uses the default interface from shill. |
| 272 | return device->UsesDefaultInterface(); |
| 273 | } |
| 274 | |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 275 | void ArcService::OnDeviceAdded(Device* device) { |
Garrick Evans | 8ff0845 | 2019-11-25 09:24:26 +0900 | [diff] [blame] | 276 | if (!AllowDevice(device)) |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 277 | return; |
Garrick Evans | ba57574 | 2019-07-17 15:48:08 +0900 | [diff] [blame] | 278 | |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 279 | const auto& config = device->config(); |
| 280 | |
| 281 | LOG(INFO) << "Adding device " << device->ifname() |
| 282 | << " bridge: " << config.host_ifname() |
Garrick Evans | 310ab55 | 2019-10-08 11:07:53 +0900 | [diff] [blame] | 283 | << " guest_iface: " << config.guest_ifname(); |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 284 | |
| 285 | // Create the bridge. |
Garrick Evans | 7a1a9ee | 2020-01-28 11:03:57 +0900 | [diff] [blame] | 286 | if (!datapath_->AddBridge(config.host_ifname(), config.host_ipv4_addr(), |
| 287 | 30)) { |
Garrick Evans | 664a82f | 2019-12-17 12:18:05 +0900 | [diff] [blame] | 288 | // Per crbug/1008686 this device cannot be deleted and then re-added. |
| 289 | // It could be that arc-networkd was restarted after a crash and this device |
| 290 | // is being re-added. |
| 291 | if (!device->IsAndroid()) { |
| 292 | LOG(ERROR) << "Failed to setup arc bridge: " << config.host_ifname(); |
| 293 | return; |
| 294 | } |
| 295 | if (!datapath_->MaskInterfaceFlags(config.host_ifname(), IFF_UP)) { |
| 296 | LOG(ERROR) << "Failed to bring up arc bridge: " << config.host_ifname(); |
| 297 | return; |
| 298 | } |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | // Setup the iptables. |
Garrick Evans | 8ff0845 | 2019-11-25 09:24:26 +0900 | [diff] [blame] | 302 | if (device->UsesDefaultInterface()) { |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 303 | if (!datapath_->AddLegacyIPv4DNAT( |
| 304 | IPv4AddressToString(config.guest_ipv4_addr()))) |
| 305 | LOG(ERROR) << "Failed to configure ARC traffic rules"; |
| 306 | |
| 307 | if (!datapath_->AddOutboundIPv4(config.host_ifname())) |
| 308 | LOG(ERROR) << "Failed to configure egress traffic rules"; |
| 309 | } else if (!device->IsAndroid()) { |
| 310 | if (!datapath_->AddInboundIPv4DNAT( |
| 311 | device->ifname(), IPv4AddressToString(config.guest_ipv4_addr()))) |
| 312 | LOG(ERROR) << "Failed to configure ingress traffic rules for " |
| 313 | << device->ifname(); |
| 314 | |
| 315 | if (!datapath_->AddOutboundIPv4(config.host_ifname())) |
| 316 | LOG(ERROR) << "Failed to configure egress traffic rules"; |
| 317 | } |
| 318 | |
Garrick Evans | a1134d7 | 2019-12-02 14:25:37 +0900 | [diff] [blame] | 319 | device->set_context(std::make_unique<Context>()); |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 320 | |
| 321 | StartDevice(device); |
| 322 | } |
| 323 | |
| 324 | void ArcService::StartDevice(Device* device) { |
Garrick Evans | 8ff0845 | 2019-11-25 09:24:26 +0900 | [diff] [blame] | 325 | if (!AllowDevice(device)) |
Garrick Evans | cfd4282 | 2019-11-15 12:38:23 +0900 | [diff] [blame] | 326 | return; |
| 327 | |
Garrick Evans | 310ab55 | 2019-10-08 11:07:53 +0900 | [diff] [blame] | 328 | // This can happen if OnDeviceAdded is invoked when the container is down. |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 329 | if (!impl_ || !impl_->IsStarted()) |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 330 | return; |
| 331 | |
Garrick Evans | 2c26310 | 2019-07-26 16:07:18 +0900 | [diff] [blame] | 332 | // If there is no context, then this is a new device and it needs to run |
| 333 | // through the full setup process. |
Garrick Evans | a1134d7 | 2019-12-02 14:25:37 +0900 | [diff] [blame] | 334 | Context* ctx = dynamic_cast<Context*>(device->context()); |
Garrick Evans | 2c26310 | 2019-07-26 16:07:18 +0900 | [diff] [blame] | 335 | if (!ctx) |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 336 | return OnDeviceAdded(device); |
| 337 | |
Garrick Evans | 2c26310 | 2019-07-26 16:07:18 +0900 | [diff] [blame] | 338 | if (ctx->IsStarted()) { |
| 339 | LOG(ERROR) << "Attempt to restart device " << device->ifname(); |
| 340 | return; |
| 341 | } |
| 342 | |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 343 | if (!impl_->OnStartDevice(device)) { |
Garrick Evans | cb791e7 | 2019-11-11 15:44:34 +0900 | [diff] [blame] | 344 | LOG(ERROR) << "Failed to start device " << device->ifname(); |
| 345 | return; |
| 346 | } |
| 347 | |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 348 | ctx->Start(); |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | void ArcService::OnDeviceRemoved(Device* device) { |
Garrick Evans | 8ff0845 | 2019-11-25 09:24:26 +0900 | [diff] [blame] | 352 | if (!AllowDevice(device)) |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 353 | return; |
| 354 | |
Garrick Evans | 310ab55 | 2019-10-08 11:07:53 +0900 | [diff] [blame] | 355 | // If the container is down, this call does nothing. |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 356 | StopDevice(device); |
| 357 | |
| 358 | const auto& config = device->config(); |
| 359 | |
| 360 | LOG(INFO) << "Removing device " << device->ifname() |
| 361 | << " bridge: " << config.host_ifname() |
| 362 | << " guest_iface: " << config.guest_ifname(); |
| 363 | |
Taoyu Li | f7e8b57 | 2019-12-13 15:22:09 +0900 | [diff] [blame] | 364 | device->StopIPv6RoutingLegacy(); |
Garrick Evans | 8ff0845 | 2019-11-25 09:24:26 +0900 | [diff] [blame] | 365 | if (device->UsesDefaultInterface()) { |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 366 | datapath_->RemoveOutboundIPv4(config.host_ifname()); |
| 367 | datapath_->RemoveLegacyIPv4DNAT(); |
| 368 | } else if (!device->IsAndroid()) { |
| 369 | datapath_->RemoveOutboundIPv4(config.host_ifname()); |
| 370 | datapath_->RemoveInboundIPv4DNAT( |
| 371 | device->ifname(), IPv4AddressToString(config.guest_ipv4_addr())); |
| 372 | } |
| 373 | |
Garrick Evans | 664a82f | 2019-12-17 12:18:05 +0900 | [diff] [blame] | 374 | // Per crbug/1008686 this device cannot be deleted and then re-added. |
| 375 | // So instead of removing the bridge, bring it down and mark it. This will |
| 376 | // allow us to detect if the device is re-added in case of a crash restart and |
| 377 | // do the right thing. |
| 378 | if (device->IsAndroid()) { |
| 379 | // This can be safely deleted now. |
| 380 | datapath_->RemoveInterface(ArcVethHostName("arc0")); |
| 381 | if (!datapath_->MaskInterfaceFlags(config.host_ifname(), IFF_DEBUG, IFF_UP)) |
| 382 | LOG(ERROR) << "Failed to bring down arc bridge " |
| 383 | << "- it may not restart correctly"; |
| 384 | } else { |
| 385 | datapath_->RemoveBridge(config.host_ifname()); |
| 386 | } |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 387 | |
Garrick Evans | a1134d7 | 2019-12-02 14:25:37 +0900 | [diff] [blame] | 388 | device->set_context(nullptr); |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | void ArcService::StopDevice(Device* device) { |
Garrick Evans | 8ff0845 | 2019-11-25 09:24:26 +0900 | [diff] [blame] | 392 | if (!AllowDevice(device)) |
Garrick Evans | cfd4282 | 2019-11-15 12:38:23 +0900 | [diff] [blame] | 393 | return; |
| 394 | |
Garrick Evans | 310ab55 | 2019-10-08 11:07:53 +0900 | [diff] [blame] | 395 | // This can happen if the device if OnDeviceRemoved is invoked when the |
| 396 | // container is down. |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 397 | if (!impl_ || !impl_->IsStarted()) |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 398 | return; |
| 399 | |
Garrick Evans | a1134d7 | 2019-12-02 14:25:37 +0900 | [diff] [blame] | 400 | Context* ctx = dynamic_cast<Context*>(device->context()); |
Garrick Evans | 2c26310 | 2019-07-26 16:07:18 +0900 | [diff] [blame] | 401 | if (!ctx) { |
| 402 | LOG(ERROR) << "Attempt to stop removed device " << device->ifname(); |
| 403 | return; |
| 404 | } |
| 405 | |
| 406 | if (!ctx->IsStarted()) { |
| 407 | LOG(ERROR) << "Attempt to re-stop device " << device->ifname(); |
| 408 | return; |
| 409 | } |
| 410 | |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 411 | impl_->OnStopDevice(device); |
Garrick Evans | cb791e7 | 2019-11-11 15:44:34 +0900 | [diff] [blame] | 412 | |
| 413 | ctx->Stop(); |
| 414 | } |
| 415 | |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 416 | void ArcService::OnDefaultInterfaceChanged(const std::string& ifname) { |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 417 | if (impl_) |
| 418 | impl_->OnDefaultInterfaceChanged(ifname); |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 419 | } |
Garrick Evans | ba57574 | 2019-07-17 15:48:08 +0900 | [diff] [blame] | 420 | |
Garrick Evans | 2c26310 | 2019-07-26 16:07:18 +0900 | [diff] [blame] | 421 | // Context |
| 422 | |
| 423 | ArcService::Context::Context() : Device::Context() { |
| 424 | Stop(); |
| 425 | } |
| 426 | |
| 427 | void ArcService::Context::Start() { |
| 428 | Stop(); |
| 429 | started_ = true; |
| 430 | } |
| 431 | |
| 432 | void ArcService::Context::Stop() { |
| 433 | started_ = false; |
| 434 | link_up_ = false; |
| 435 | routing_table_id_ = kInvalidTableID; |
| 436 | routing_table_attempts_ = 0; |
| 437 | } |
| 438 | |
| 439 | bool ArcService::Context::IsStarted() const { |
| 440 | return started_; |
| 441 | } |
| 442 | |
| 443 | bool ArcService::Context::IsLinkUp() const { |
| 444 | return link_up_; |
| 445 | } |
| 446 | |
| 447 | bool ArcService::Context::SetLinkUp(bool link_up) { |
| 448 | if (link_up == link_up_) |
| 449 | return false; |
| 450 | |
| 451 | link_up_ = link_up; |
| 452 | return true; |
| 453 | } |
| 454 | |
| 455 | bool ArcService::Context::HasIPv6() const { |
| 456 | return routing_table_id_ != kInvalidTableID; |
| 457 | } |
| 458 | |
| 459 | bool ArcService::Context::SetHasIPv6(int routing_table_id) { |
| 460 | if (routing_table_id <= kRouteControllerRouteTableOffsetFromIndex) |
| 461 | return false; |
| 462 | |
| 463 | routing_table_id_ = routing_table_id; |
| 464 | return true; |
| 465 | } |
| 466 | |
Garrick Evans | dc3fc45 | 2019-09-06 14:15:04 +0900 | [diff] [blame] | 467 | void ArcService::Context::ClearIPv6() { |
| 468 | routing_table_id_ = kInvalidTableID; |
| 469 | routing_table_attempts_ = 0; |
| 470 | } |
| 471 | |
Garrick Evans | 2c26310 | 2019-07-26 16:07:18 +0900 | [diff] [blame] | 472 | int ArcService::Context::RoutingTableID() const { |
| 473 | return routing_table_id_; |
| 474 | } |
| 475 | |
| 476 | int ArcService::Context::RoutingTableAttempts() { |
| 477 | return routing_table_attempts_++; |
| 478 | } |
| 479 | |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 480 | const std::string& ArcService::Context::TAP() const { |
| 481 | return tap_; |
| 482 | } |
| 483 | |
| 484 | void ArcService::Context::SetTAP(const std::string& tap) { |
| 485 | tap_ = tap; |
| 486 | } |
| 487 | |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 488 | // ARC++ specific functions. |
| 489 | |
| 490 | ArcService::ContainerImpl::ContainerImpl(DeviceManagerBase* dev_mgr, |
| 491 | Datapath* datapath, |
| 492 | GuestMessage::GuestType guest) |
Garrick Evans | a34b586 | 2019-11-20 09:34:01 +0900 | [diff] [blame] | 493 | : pid_(kInvalidPID), dev_mgr_(dev_mgr), datapath_(datapath), guest_(guest) { |
Garrick Evans | 6d227b9 | 2019-12-03 16:11:29 +0900 | [diff] [blame] | 494 | OneTimeSetup(*datapath_); |
Garrick Evans | a34b586 | 2019-11-20 09:34:01 +0900 | [diff] [blame] | 495 | } |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 496 | |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 497 | GuestMessage::GuestType ArcService::ContainerImpl::guest() const { |
| 498 | return guest_; |
| 499 | } |
| 500 | |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 501 | uint32_t ArcService::ContainerImpl::id() const { |
Garrick Evans | 664a82f | 2019-12-17 12:18:05 +0900 | [diff] [blame] | 502 | return pid_; |
| 503 | } |
| 504 | |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 505 | bool ArcService::ContainerImpl::Start(uint32_t pid) { |
Garrick Evans | a51d0a1 | 2019-11-28 13:51:23 +0900 | [diff] [blame] | 506 | // This could happen if something crashes and the stop signal is not sent. |
| 507 | // It can probably be addressed by stopping and restarting the service. |
| 508 | if (pid_ != kInvalidPID) |
| 509 | return false; |
| 510 | |
Garrick Evans | 8ff0845 | 2019-11-25 09:24:26 +0900 | [diff] [blame] | 511 | // TODO(garrick): Remove this test hack. |
| 512 | if (pid == kTestPID) { |
| 513 | LOG(WARNING) << "Running with test PID"; |
| 514 | pid_ = pid; |
| 515 | return true; |
| 516 | } |
Garrick Evans | 4dec0c4 | 2019-11-29 12:51:57 +0900 | [diff] [blame] | 517 | if (pid == kInvalidPID) { |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 518 | LOG(ERROR) << "Cannot start service - invalid container PID"; |
| 519 | return false; |
| 520 | } |
Garrick Evans | 4dec0c4 | 2019-11-29 12:51:57 +0900 | [diff] [blame] | 521 | pid_ = pid; |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 522 | |
Garrick Evans | bc91a35 | 2020-01-16 16:43:26 +0900 | [diff] [blame] | 523 | // Start listening for RTNetlink messages to be notified when a virtual ARC |
| 524 | // interface is brought up or down. |
| 525 | // TODO(garrick): Delete when NDProxy is enabled on all boards. |
| 526 | { |
| 527 | host_link_listener_ = std::make_unique<shill::RTNLListener>( |
| 528 | shill::RTNLHandler::kRequestLink, |
| 529 | Bind(&ArcService::ContainerImpl::HostLinkMsgHandler, |
| 530 | weak_factory_.GetWeakPtr())); |
| 531 | shill::RTNLHandler::GetInstance()->Start(RTMGRP_LINK); |
| 532 | } |
| 533 | |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 534 | // Start listening for RTNetlink messages in the container's net namespace |
| 535 | // to be notified whenever it brings up an interface. |
| 536 | { |
| 537 | ScopedNS ns(pid_); |
| 538 | if (ns.IsValid()) { |
| 539 | rtnl_handler_ = std::make_unique<RTNetlinkHandler>(); |
| 540 | rtnl_handler_->Start(RTMGRP_LINK); |
| 541 | link_listener_ = std::make_unique<shill::RTNLListener>( |
| 542 | shill::RTNLHandler::kRequestLink, |
| 543 | Bind(&ArcService::ContainerImpl::LinkMsgHandler, |
| 544 | weak_factory_.GetWeakPtr()), |
| 545 | rtnl_handler_.get()); |
| 546 | } else { |
| 547 | // This is bad - it means we won't ever be able to tell when the container |
| 548 | // brings up an interface. |
| 549 | LOG(ERROR) |
| 550 | << "Cannot start netlink listener - invalid container namespace?"; |
| 551 | return false; |
| 552 | } |
| 553 | } |
| 554 | |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 555 | LOG(INFO) << "ARC++ network service started {pid: " << pid_ << "}"; |
| 556 | return true; |
| 557 | } |
| 558 | |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 559 | void ArcService::ContainerImpl::Stop(uint32_t /*pid*/) { |
Garrick Evans | 4dec0c4 | 2019-11-29 12:51:57 +0900 | [diff] [blame] | 560 | if (!IsStarted()) |
Taoyu Li | 1c96d27 | 2019-12-13 14:17:43 +0900 | [diff] [blame] | 561 | return; |
Garrick Evans | 4dec0c4 | 2019-11-29 12:51:57 +0900 | [diff] [blame] | 562 | |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 563 | rtnl_handler_->RemoveListener(link_listener_.get()); |
| 564 | link_listener_.reset(); |
| 565 | rtnl_handler_.reset(); |
| 566 | |
| 567 | LOG(INFO) << "ARC++ network service stopped {pid: " << pid_ << "}"; |
| 568 | pid_ = kInvalidPID; |
| 569 | } |
| 570 | |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 571 | bool ArcService::ContainerImpl::IsStarted(uint32_t* pid) const { |
Garrick Evans | a51d0a1 | 2019-11-28 13:51:23 +0900 | [diff] [blame] | 572 | if (pid) |
| 573 | *pid = pid_; |
| 574 | |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 575 | return pid_ != kInvalidPID; |
| 576 | } |
| 577 | |
| 578 | bool ArcService::ContainerImpl::OnStartDevice(Device* device) { |
| 579 | const auto& config = device->config(); |
| 580 | |
| 581 | LOG(INFO) << "Starting device " << device->ifname() |
| 582 | << " bridge: " << config.host_ifname() |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 583 | << " guest_iface: " << config.guest_ifname() << " pid: " << pid_; |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 584 | |
| 585 | std::string veth_ifname = datapath_->AddVirtualBridgedInterface( |
| 586 | device->ifname(), MacAddressToString(config.guest_mac_addr()), |
| 587 | config.host_ifname()); |
| 588 | if (veth_ifname.empty()) { |
| 589 | LOG(ERROR) << "Failed to create virtual interface for container"; |
| 590 | return false; |
| 591 | } |
| 592 | |
| 593 | if (!datapath_->AddInterfaceToContainer( |
Garrick Evans | 7a1a9ee | 2020-01-28 11:03:57 +0900 | [diff] [blame] | 594 | pid_, veth_ifname, config.guest_ifname(), config.guest_ipv4_addr(), |
| 595 | 30, device->options().fwd_multicast)) { |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 596 | LOG(ERROR) << "Failed to create container interface."; |
| 597 | datapath_->RemoveInterface(veth_ifname); |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 598 | return false; |
| 599 | } |
| 600 | |
Garrick Evans | 708ec34 | 2020-01-21 10:00:29 +0900 | [diff] [blame] | 601 | // Setup callback for legacy IPv6 discovery, if applicable. |
| 602 | if (device->options().ipv6_enabled && |
| 603 | device->options().find_ipv6_routes_legacy) { |
| 604 | device->RegisterIPv6Handlers( |
| 605 | base::Bind(&ArcService::ContainerImpl::SetupIPv6, |
| 606 | weak_factory_.GetWeakPtr()), |
| 607 | base::Bind(&ArcService::ContainerImpl::TeardownIPv6, |
| 608 | weak_factory_.GetWeakPtr())); |
| 609 | } |
| 610 | |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 611 | // Signal the container that the network device is ready. |
Garrick Evans | 8ff0845 | 2019-11-25 09:24:26 +0900 | [diff] [blame] | 612 | if (device->IsAndroid()) { |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 613 | datapath_->runner().WriteSentinelToContainer(base::IntToString(pid_)); |
| 614 | } |
Garrick Evans | 708ec34 | 2020-01-21 10:00:29 +0900 | [diff] [blame] | 615 | |
Taoyu Li | 1c96d27 | 2019-12-13 14:17:43 +0900 | [diff] [blame] | 616 | dev_mgr_->StartForwarding(*device); |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 617 | return true; |
| 618 | } |
| 619 | |
| 620 | void ArcService::ContainerImpl::OnStopDevice(Device* device) { |
| 621 | const auto& config = device->config(); |
| 622 | |
| 623 | LOG(INFO) << "Stopping device " << device->ifname() |
| 624 | << " bridge: " << config.host_ifname() |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 625 | << " guest_iface: " << config.guest_ifname() << " pid: " << pid_; |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 626 | |
Taoyu Li | f7e8b57 | 2019-12-13 15:22:09 +0900 | [diff] [blame] | 627 | device->StopIPv6RoutingLegacy(); |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 628 | if (!device->IsAndroid()) { |
| 629 | datapath_->RemoveInterface(ArcVethHostName(device->ifname())); |
| 630 | } |
Garrick Evans | 708ec34 | 2020-01-21 10:00:29 +0900 | [diff] [blame] | 631 | |
| 632 | device->UnregisterIPv6Handlers(); |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | void ArcService::ContainerImpl::OnDefaultInterfaceChanged( |
| 636 | const std::string& ifname) { |
| 637 | if (!IsStarted()) |
| 638 | return; |
| 639 | |
| 640 | // For ARC N, we must always be able to find the arc0 device and, at a |
| 641 | // minimum, disable it. |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 642 | if (guest() == GuestMessage::ARC_LEGACY) { |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 643 | datapath_->RemoveLegacyIPv4InboundDNAT(); |
| 644 | auto* device = dev_mgr_->FindByGuestInterface("arc0"); |
| 645 | if (!device) { |
| 646 | LOG(DFATAL) << "Expected legacy Android device missing"; |
| 647 | return; |
| 648 | } |
Taoyu Li | f7e8b57 | 2019-12-13 15:22:09 +0900 | [diff] [blame] | 649 | device->StopIPv6RoutingLegacy(); |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 650 | |
| 651 | // If a new default interface was given, then re-enable with that. |
| 652 | if (!ifname.empty()) { |
| 653 | datapath_->AddLegacyIPv4InboundDNAT(ifname); |
Taoyu Li | f7e8b57 | 2019-12-13 15:22:09 +0900 | [diff] [blame] | 654 | device->StartIPv6RoutingLegacy(ifname); |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 655 | } |
| 656 | return; |
| 657 | } |
| 658 | |
| 659 | // For ARC P and later, we're only concerned with resetting the device when it |
| 660 | // becomes the default (again) in order to ensure any previous configuration. |
| 661 | // is cleared. |
| 662 | if (ifname.empty()) |
| 663 | return; |
| 664 | |
| 665 | auto* device = dev_mgr_->FindByGuestInterface(ifname); |
| 666 | if (!device) { |
| 667 | LOG(ERROR) << "Expected default device missing: " << ifname; |
| 668 | return; |
| 669 | } |
| 670 | device->StopIPv6RoutingLegacy(); |
| 671 | device->StartIPv6RoutingLegacy(ifname); |
| 672 | } |
| 673 | |
| 674 | void ArcService::ContainerImpl::LinkMsgHandler(const shill::RTNLMessage& msg) { |
| 675 | if (!msg.HasAttribute(IFLA_IFNAME)) { |
| 676 | LOG(ERROR) << "Link event message does not have IFLA_IFNAME"; |
| 677 | return; |
| 678 | } |
| 679 | bool link_up = msg.link_status().flags & IFF_UP; |
| 680 | shill::ByteString b(msg.GetAttribute(IFLA_IFNAME)); |
| 681 | std::string ifname(reinterpret_cast<const char*>( |
| 682 | b.GetSubstring(0, IFNAMSIZ).GetConstData())); |
| 683 | |
| 684 | auto* device = dev_mgr_->FindByGuestInterface(ifname); |
| 685 | if (!device) |
| 686 | return; |
| 687 | |
Garrick Evans | a1134d7 | 2019-12-02 14:25:37 +0900 | [diff] [blame] | 688 | Context* ctx = dynamic_cast<Context*>(device->context()); |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 689 | if (!ctx) { |
| 690 | LOG(DFATAL) << "Context missing"; |
| 691 | return; |
| 692 | } |
| 693 | |
| 694 | // If the link status is unchanged, there is nothing to do. |
| 695 | if (!ctx->SetLinkUp(link_up)) |
| 696 | return; |
| 697 | |
| 698 | if (!link_up) { |
| 699 | LOG(INFO) << ifname << " is now down"; |
| 700 | return; |
| 701 | } |
| 702 | LOG(INFO) << ifname << " is now up"; |
| 703 | |
Garrick Evans | 8ff0845 | 2019-11-25 09:24:26 +0900 | [diff] [blame] | 704 | if (device->UsesDefaultInterface()) { |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 705 | OnDefaultInterfaceChanged(dev_mgr_->DefaultInterface()); |
| 706 | return; |
| 707 | } |
| 708 | |
Garrick Evans | 8ff0845 | 2019-11-25 09:24:26 +0900 | [diff] [blame] | 709 | if (device->IsAndroid()) |
| 710 | return; |
| 711 | |
Taoyu Li | f7e8b57 | 2019-12-13 15:22:09 +0900 | [diff] [blame] | 712 | device->StartIPv6RoutingLegacy(ifname); |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 713 | } |
| 714 | |
Garrick Evans | bc91a35 | 2020-01-16 16:43:26 +0900 | [diff] [blame] | 715 | // TODO(garrick): Remove once NDProxy is enabled on all boards. |
| 716 | void ArcService::ContainerImpl::HostLinkMsgHandler( |
| 717 | const shill::RTNLMessage& msg) { |
| 718 | if (!msg.HasAttribute(IFLA_IFNAME)) { |
| 719 | LOG(ERROR) << "Link event message does not have IFLA_IFNAME"; |
| 720 | return; |
| 721 | } |
| 722 | |
| 723 | // Only consider virtual interfaces that were created for guests; for now this |
| 724 | // only includes those prefixed with 'arc'. |
| 725 | shill::ByteString b(msg.GetAttribute(IFLA_IFNAME)); |
| 726 | std::string ifname(reinterpret_cast<const char*>( |
| 727 | b.GetSubstring(0, IFNAMSIZ).GetConstData())); |
| 728 | if (!IsArcDevice(ifname)) |
| 729 | return; |
| 730 | |
| 731 | bool link_up = msg.link_status().flags & IFF_UP; |
| 732 | Device* device = dev_mgr_->FindByHostInterface(ifname); |
| 733 | |
| 734 | if (!device || !device->HostLinkUp(link_up)) |
| 735 | return; |
| 736 | |
| 737 | if (!link_up) { |
| 738 | LOG(INFO) << ifname << " is now down"; |
| 739 | device->StopIPv6RoutingLegacy(); |
| 740 | return; |
| 741 | } |
| 742 | |
| 743 | // The link is now up. |
| 744 | LOG(INFO) << ifname << " is now up"; |
| 745 | |
| 746 | if (device->UsesDefaultInterface()) |
| 747 | device->StartIPv6RoutingLegacy(dev_mgr_->DefaultInterface()); |
| 748 | else if (!device->IsAndroid()) |
| 749 | device->StartIPv6RoutingLegacy(device->config().guest_ifname()); |
| 750 | } |
| 751 | |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 752 | void ArcService::ContainerImpl::SetupIPv6(Device* device) { |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 753 | auto& ipv6_config = device->ipv6_config(); |
| 754 | if (ipv6_config.ifname.empty()) |
| 755 | return; |
| 756 | |
Garrick Evans | a1134d7 | 2019-12-02 14:25:37 +0900 | [diff] [blame] | 757 | Context* ctx = dynamic_cast<Context*>(device->context()); |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 758 | if (!ctx) { |
| 759 | LOG(DFATAL) << "Context missing"; |
| 760 | return; |
| 761 | } |
| 762 | if (ctx->HasIPv6()) |
| 763 | return; |
| 764 | |
| 765 | LOG(INFO) << "Setting up IPv6 for " << ipv6_config.ifname; |
| 766 | |
| 767 | int table_id = |
| 768 | GetAndroidRoutingTableId(device->config().guest_ifname(), pid_); |
| 769 | if (table_id == kInvalidTableID) { |
| 770 | if (ctx->RoutingTableAttempts() < kMaxTableRetries) { |
| 771 | LOG(INFO) << "Could not look up routing table ID for container interface " |
| 772 | << device->config().guest_ifname() << " - trying again..."; |
| 773 | base::MessageLoop::current()->task_runner()->PostDelayedTask( |
| 774 | FROM_HERE, |
| 775 | base::Bind(&ArcService::ContainerImpl::SetupIPv6, |
| 776 | weak_factory_.GetWeakPtr(), device), |
| 777 | kTableRetryDelay); |
| 778 | } else { |
| 779 | LOG(DFATAL) |
| 780 | << "Could not look up routing table ID for container interface " |
| 781 | << device->config().guest_ifname(); |
| 782 | } |
| 783 | return; |
| 784 | } |
| 785 | |
| 786 | LOG(INFO) << "Setting IPv6 address " << ipv6_config.addr |
| 787 | << "/128, gateway=" << ipv6_config.router << " on " |
| 788 | << ipv6_config.ifname; |
| 789 | |
| 790 | char buf[INET6_ADDRSTRLEN] = {0}; |
| 791 | if (!inet_ntop(AF_INET6, &ipv6_config.addr, buf, sizeof(buf))) { |
| 792 | LOG(DFATAL) << "Invalid address: " << ipv6_config.addr; |
| 793 | return; |
| 794 | } |
| 795 | std::string addr = buf; |
| 796 | |
| 797 | if (!inet_ntop(AF_INET6, &ipv6_config.router, buf, sizeof(buf))) { |
| 798 | LOG(DFATAL) << "Invalid router address: " << ipv6_config.router; |
| 799 | return; |
| 800 | } |
| 801 | std::string router = buf; |
| 802 | |
| 803 | const auto& config = device->config(); |
| 804 | { |
| 805 | ScopedNS ns(pid_); |
| 806 | if (!ns.IsValid()) { |
| 807 | LOG(ERROR) << "Invalid container namespace (" << pid_ |
| 808 | << ") - cannot configure IPv6."; |
| 809 | return; |
| 810 | } |
Taoyu Li | 90c1391 | 2019-11-26 17:56:54 +0900 | [diff] [blame] | 811 | // Tag the interface so that ARC can detect this manual configuration and |
| 812 | // skip disabling and re-enabling IPv6 (b/144545910). |
Garrick Evans | 664a82f | 2019-12-17 12:18:05 +0900 | [diff] [blame] | 813 | if (!datapath_->MaskInterfaceFlags(config.guest_ifname(), IFF_DEBUG)) { |
Taoyu Li | 90c1391 | 2019-11-26 17:56:54 +0900 | [diff] [blame] | 814 | LOG(ERROR) << "Failed to mark IPv6 manual config flag on interface"; |
| 815 | } |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 816 | if (!datapath_->AddIPv6GatewayRoutes(config.guest_ifname(), addr, router, |
| 817 | ipv6_config.prefix_len, table_id)) { |
| 818 | LOG(ERROR) << "Failed to setup IPv6 routes in the container"; |
| 819 | return; |
| 820 | } |
| 821 | } |
| 822 | |
Taoyu Li | 1ac4ab8 | 2020-01-07 16:37:45 +0900 | [diff] [blame] | 823 | if (!datapath_->AddIPv6HostRoute(config.host_ifname(), addr, 128)) { |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 824 | LOG(ERROR) << "Failed to setup the IPv6 route for interface " |
| 825 | << config.host_ifname(); |
| 826 | return; |
| 827 | } |
| 828 | |
| 829 | if (!datapath_->AddIPv6Neighbor(ipv6_config.ifname, addr)) { |
| 830 | LOG(ERROR) << "Failed to setup the IPv6 neighbor proxy"; |
Taoyu Li | 1ac4ab8 | 2020-01-07 16:37:45 +0900 | [diff] [blame] | 831 | datapath_->RemoveIPv6HostRoute(config.host_ifname(), addr, 128); |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 832 | return; |
| 833 | } |
| 834 | |
Taoyu Li | 2980ee9 | 2019-11-18 17:49:32 +0900 | [diff] [blame] | 835 | if (!datapath_->AddIPv6Forwarding(ipv6_config.ifname, |
| 836 | device->config().host_ifname())) { |
| 837 | LOG(ERROR) << "Failed to setup iptables for IPv6"; |
| 838 | datapath_->RemoveIPv6Neighbor(ipv6_config.ifname, addr); |
Taoyu Li | 1ac4ab8 | 2020-01-07 16:37:45 +0900 | [diff] [blame] | 839 | datapath_->RemoveIPv6HostRoute(config.host_ifname(), addr, 128); |
Taoyu Li | 2980ee9 | 2019-11-18 17:49:32 +0900 | [diff] [blame] | 840 | return; |
| 841 | } |
| 842 | |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 843 | ctx->SetHasIPv6(table_id); |
| 844 | } |
| 845 | |
| 846 | void ArcService::ContainerImpl::TeardownIPv6(Device* device) { |
Garrick Evans | a1134d7 | 2019-12-02 14:25:37 +0900 | [diff] [blame] | 847 | Context* ctx = dynamic_cast<Context*>(device->context()); |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 848 | if (!ctx || !ctx->HasIPv6()) |
| 849 | return; |
| 850 | |
| 851 | auto& ipv6_config = device->ipv6_config(); |
| 852 | LOG(INFO) << "Clearing IPv6 for " << ipv6_config.ifname; |
| 853 | int table_id = ctx->RoutingTableID(); |
| 854 | ctx->ClearIPv6(); |
| 855 | |
| 856 | char buf[INET6_ADDRSTRLEN] = {0}; |
| 857 | if (!inet_ntop(AF_INET6, &ipv6_config.addr, buf, sizeof(buf))) { |
| 858 | LOG(DFATAL) << "Invalid address: " << ipv6_config.addr; |
| 859 | return; |
| 860 | } |
| 861 | std::string addr = buf; |
| 862 | |
| 863 | if (!inet_ntop(AF_INET6, &ipv6_config.router, buf, sizeof(buf))) { |
| 864 | LOG(DFATAL) << "Invalid router address: " << ipv6_config.router; |
| 865 | return; |
| 866 | } |
| 867 | std::string router = buf; |
| 868 | |
| 869 | const auto& config = device->config(); |
Taoyu Li | 2980ee9 | 2019-11-18 17:49:32 +0900 | [diff] [blame] | 870 | datapath_->RemoveIPv6Forwarding(ipv6_config.ifname, config.host_ifname()); |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 871 | datapath_->RemoveIPv6Neighbor(ipv6_config.ifname, addr); |
Taoyu Li | 1ac4ab8 | 2020-01-07 16:37:45 +0900 | [diff] [blame] | 872 | datapath_->RemoveIPv6HostRoute(config.host_ifname(), addr, 128); |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 873 | |
| 874 | ScopedNS ns(pid_); |
| 875 | if (ns.IsValid()) { |
| 876 | datapath_->RemoveIPv6GatewayRoutes(config.guest_ifname(), addr, router, |
| 877 | ipv6_config.prefix_len, table_id); |
| 878 | } |
| 879 | } |
| 880 | |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 881 | // VM specific functions |
| 882 | |
| 883 | ArcService::VmImpl::VmImpl(DeviceManagerBase* dev_mgr, Datapath* datapath) |
| 884 | : cid_(kInvalidCID), dev_mgr_(dev_mgr), datapath_(datapath) {} |
| 885 | |
| 886 | GuestMessage::GuestType ArcService::VmImpl::guest() const { |
| 887 | return GuestMessage::ARC_VM; |
| 888 | } |
| 889 | |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 890 | uint32_t ArcService::VmImpl::id() const { |
Garrick Evans | 664a82f | 2019-12-17 12:18:05 +0900 | [diff] [blame] | 891 | return cid_; |
| 892 | } |
| 893 | |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 894 | bool ArcService::VmImpl::Start(uint32_t cid) { |
Garrick Evans | a51d0a1 | 2019-11-28 13:51:23 +0900 | [diff] [blame] | 895 | // This can happen if concierge crashes and doesn't send the vm down RPC. |
| 896 | // It can probably be addressed by stopping and restarting the service. |
| 897 | if (cid_ != kInvalidCID) |
| 898 | return false; |
| 899 | |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 900 | if (cid == kInvalidCID) { |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 901 | LOG(ERROR) << "Invalid VM cid " << cid; |
| 902 | return false; |
| 903 | } |
| 904 | |
| 905 | cid_ = cid; |
| 906 | LOG(INFO) << "ARCVM network service started {cid: " << cid_ << "}"; |
| 907 | |
| 908 | return true; |
| 909 | } |
| 910 | |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 911 | void ArcService::VmImpl::Stop(uint32_t cid) { |
Garrick Evans | 21173b1 | 2019-11-20 15:23:16 +0900 | [diff] [blame] | 912 | if (cid_ != cid) { |
| 913 | LOG(ERROR) << "Mismatched ARCVM CIDs " << cid_ << " != " << cid; |
| 914 | return; |
| 915 | } |
| 916 | |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 917 | LOG(INFO) << "ARCVM network service stopped {cid: " << cid_ << "}"; |
| 918 | cid_ = kInvalidCID; |
| 919 | } |
| 920 | |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 921 | bool ArcService::VmImpl::IsStarted(uint32_t* cid) const { |
Garrick Evans | a51d0a1 | 2019-11-28 13:51:23 +0900 | [diff] [blame] | 922 | if (cid) |
| 923 | *cid = cid_; |
| 924 | |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 925 | return cid_ != kInvalidCID; |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 926 | } |
| 927 | |
| 928 | bool ArcService::VmImpl::OnStartDevice(Device* device) { |
| 929 | // TODO(garrick): Remove this once ARCVM supports ad hoc interface |
Garrick Evans | 8ff0845 | 2019-11-25 09:24:26 +0900 | [diff] [blame] | 930 | // configurations. |
| 931 | if (!device->UsesDefaultInterface()) |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 932 | return false; |
| 933 | |
| 934 | const auto& config = device->config(); |
| 935 | |
| 936 | LOG(INFO) << "Starting device " << device->ifname() |
| 937 | << " bridge: " << config.host_ifname() |
| 938 | << " guest_iface: " << config.guest_ifname() << " cid: " << cid_; |
| 939 | |
Garrick Evans | a1134d7 | 2019-12-02 14:25:37 +0900 | [diff] [blame] | 940 | Context* ctx = dynamic_cast<Context*>(device->context()); |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 941 | if (!ctx) { |
| 942 | LOG(ERROR) << "Context missing"; |
| 943 | return false; |
| 944 | } |
| 945 | |
| 946 | // Since the interface will be added to the bridge, no address configuration |
| 947 | // should be provided here. |
| 948 | std::string tap = |
| 949 | datapath_->AddTAP("" /* auto-generate name */, nullptr /* no mac addr */, |
| 950 | nullptr /* no ipv4 subnet */, vm_tools::kCrosVmUser); |
| 951 | if (tap.empty()) { |
| 952 | LOG(ERROR) << "Failed to create TAP device for VM"; |
| 953 | return false; |
| 954 | } |
| 955 | |
| 956 | if (!datapath_->AddToBridge(config.host_ifname(), tap)) { |
| 957 | LOG(ERROR) << "Failed to bridge TAP device " << tap; |
| 958 | datapath_->RemoveInterface(tap); |
| 959 | return false; |
| 960 | } |
| 961 | |
| 962 | ctx->SetTAP(tap); |
| 963 | // TODO(garrick): Remove this once ARCVM supports ad hoc interface |
| 964 | // configurations; but for now ARCVM needs to be treated like ARC++ N. |
| 965 | OnDefaultInterfaceChanged(dev_mgr_->DefaultInterface()); |
Taoyu Li | 1c96d27 | 2019-12-13 14:17:43 +0900 | [diff] [blame] | 966 | dev_mgr_->StartForwarding(*device); |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 967 | return true; |
| 968 | } |
| 969 | |
| 970 | void ArcService::VmImpl::OnStopDevice(Device* device) { |
| 971 | // TODO(garrick): Remove this once ARCVM supports ad hoc interface |
Garrick Evans | 8ff0845 | 2019-11-25 09:24:26 +0900 | [diff] [blame] | 972 | // configurations. |
| 973 | if (!device->UsesDefaultInterface()) |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 974 | return; |
| 975 | |
| 976 | const auto& config = device->config(); |
| 977 | |
| 978 | LOG(INFO) << "Stopping " << device->ifname() |
| 979 | << " bridge: " << config.host_ifname() |
| 980 | << " guest_iface: " << config.guest_ifname() << " cid: " << cid_; |
| 981 | |
Garrick Evans | a1134d7 | 2019-12-02 14:25:37 +0900 | [diff] [blame] | 982 | Context* ctx = dynamic_cast<Context*>(device->context()); |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 983 | if (!ctx) { |
| 984 | LOG(ERROR) << "Context missing"; |
| 985 | return; |
| 986 | } |
| 987 | |
Taoyu Li | f7e8b57 | 2019-12-13 15:22:09 +0900 | [diff] [blame] | 988 | device->StopIPv6RoutingLegacy(); |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 989 | datapath_->RemoveInterface(ctx->TAP()); |
| 990 | } |
| 991 | |
| 992 | void ArcService::VmImpl::OnDefaultInterfaceChanged(const std::string& ifname) { |
| 993 | if (!IsStarted()) |
| 994 | return; |
| 995 | |
| 996 | // TODO(garrick): Remove this once ARCVM supports ad hoc interface |
| 997 | // configurations; but for now ARCVM needs to be treated like ARC++ N. |
| 998 | datapath_->RemoveLegacyIPv4InboundDNAT(); |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 999 | auto* device = dev_mgr_->FindByGuestInterface("arc1"); |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 1000 | if (!device) { |
| 1001 | LOG(DFATAL) << "Expected Android device missing"; |
| 1002 | return; |
| 1003 | } |
Taoyu Li | f7e8b57 | 2019-12-13 15:22:09 +0900 | [diff] [blame] | 1004 | device->StopIPv6RoutingLegacy(); |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 1005 | |
| 1006 | // If a new default interface was given, then re-enable with that. |
| 1007 | if (!ifname.empty()) { |
| 1008 | datapath_->AddLegacyIPv4InboundDNAT(ifname); |
Taoyu Li | f7e8b57 | 2019-12-13 15:22:09 +0900 | [diff] [blame] | 1009 | device->StartIPv6RoutingLegacy(ifname); |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 1010 | } |
| 1011 | } |
| 1012 | |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 1013 | } // namespace arc_networkd |