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 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 5 | #include "patchpanel/arc_service.h" |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 6 | |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 7 | #include <linux/rtnetlink.h> |
| 8 | #include <net/if.h> |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 9 | #include <sys/ioctl.h> |
Garrick Evans | 71e4a86 | 2020-05-18 12:22:23 +0900 | [diff] [blame] | 10 | #include <sys/utsname.h> |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 11 | |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 12 | #include <utility> |
Jason Jeremy Iman | f4156cb | 2019-11-14 15:36:22 +0900 | [diff] [blame] | 13 | #include <vector> |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 14 | |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 15 | #include <base/bind.h> |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 16 | #include <base/files/file_path.h> |
| 17 | #include <base/files/file_util.h> |
| 18 | #include <base/logging.h> |
| 19 | #include <base/strings/string_number_conversions.h> |
| 20 | #include <base/strings/string_util.h> |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 21 | #include <base/strings/stringprintf.h> |
Qijiang Fan | 2d7aeb4 | 2020-05-19 02:06:39 +0900 | [diff] [blame] | 22 | #include <base/system/sys_info.h> |
Garrick Evans | 1f5a361 | 2019-11-08 12:59:03 +0900 | [diff] [blame] | 23 | #include <brillo/key_value_store.h> |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 24 | #include <chromeos/constants/vm_tools.h> |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 25 | |
Jason Jeremy Iman | 3081d0e | 2020-03-04 15:52:06 +0900 | [diff] [blame] | 26 | #include "patchpanel/adb_proxy.h" |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 27 | #include "patchpanel/datapath.h" |
| 28 | #include "patchpanel/mac_address_generator.h" |
| 29 | #include "patchpanel/manager.h" |
| 30 | #include "patchpanel/minijailed_process_runner.h" |
| 31 | #include "patchpanel/net_util.h" |
| 32 | #include "patchpanel/scoped_ns.h" |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 33 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 34 | namespace patchpanel { |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 35 | namespace { |
Hugo Benichi | 0781d40 | 2021-02-22 13:43:11 +0900 | [diff] [blame^] | 36 | // UID of Android root, relative to the host pid namespace. |
Hugo Benichi | 6c0233e | 2020-10-06 22:37:57 +0900 | [diff] [blame] | 37 | const int32_t kAndroidRootUid = 655360; |
Hugo Benichi | 4d4bb8f | 2020-07-07 12:16:07 +0900 | [diff] [blame] | 38 | constexpr uint32_t kInvalidId = 0; |
Hugo Benichi | 33860d7 | 2020-07-09 16:34:01 +0900 | [diff] [blame] | 39 | constexpr char kArcNetnsName[] = "arc_netns"; |
Garrick Evans | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 40 | constexpr char kArcIfname[] = "arc0"; |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 41 | constexpr std::array<const char*, 2> kEthernetInterfacePrefixes{{"eth", "usb"}}; |
| 42 | constexpr std::array<const char*, 2> kWifiInterfacePrefixes{{"wlan", "mlan"}}; |
Garrick Evans | 86c7d9c | 2020-03-17 09:25:48 +0900 | [diff] [blame] | 43 | constexpr std::array<const char*, 2> kCellInterfacePrefixes{{"wwan", "rmnet"}}; |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 44 | |
Garrick Evans | 71e4a86 | 2020-05-18 12:22:23 +0900 | [diff] [blame] | 45 | bool KernelVersion(int* major, int* minor) { |
| 46 | struct utsname u; |
| 47 | if (uname(&u) != 0) { |
| 48 | PLOG(ERROR) << "uname failed"; |
| 49 | *major = *minor = 0; |
| 50 | return false; |
| 51 | } |
| 52 | int unused; |
| 53 | if (sscanf(u.release, "%d.%d.%d", major, minor, &unused) != 3) { |
| 54 | LOG(ERROR) << "unexpected release string: " << u.release; |
| 55 | *major = *minor = 0; |
| 56 | return false; |
| 57 | } |
| 58 | return true; |
| 59 | } |
| 60 | |
Hugo Benichi | 0781d40 | 2021-02-22 13:43:11 +0900 | [diff] [blame^] | 61 | // Makes Android root the owner of /sys/class/ + |path|. |pid| is the ARC |
| 62 | // container pid. |
| 63 | void SetSysfsOwnerToAndroidRoot(uint32_t pid, const std::string& path) { |
| 64 | ScopedNS ns(pid, ScopedNS::Type::Mount); |
| 65 | if (!ns.IsValid()) { |
| 66 | LOG(ERROR) << "Cannot enter mnt namespace for pid " << pid; |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | const std::string sysfs_path = "/sys/class/" + path; |
| 71 | if (chown(sysfs_path.c_str(), kAndroidRootUid, kAndroidRootUid) == -1) |
| 72 | PLOG(ERROR) << "Failed to change ownership of " + sysfs_path; |
| 73 | } |
| 74 | |
| 75 | void OneTimeContainerSetup(const Datapath& datapath, uint32_t pid) { |
Garrick Evans | a34b586 | 2019-11-20 09:34:01 +0900 | [diff] [blame] | 76 | static bool done = false; |
| 77 | if (done) |
| 78 | return; |
| 79 | |
Garrick Evans | 6d227b9 | 2019-12-03 16:11:29 +0900 | [diff] [blame] | 80 | auto& runner = datapath.runner(); |
| 81 | |
| 82 | // Load networking modules needed by Android that are not compiled in the |
| 83 | // kernel. Android does not allow auto-loading of kernel modules. |
Garrick Evans | c53b970 | 2020-05-13 13:20:09 +0900 | [diff] [blame] | 84 | // Expected for all kernels. |
Garrick Evans | 8e8e347 | 2020-01-23 14:03:50 +0900 | [diff] [blame] | 85 | if (runner.modprobe_all({ |
Garrick Evans | a34b586 | 2019-11-20 09:34:01 +0900 | [diff] [blame] | 86 | // The netfilter modules needed by netd for iptables commands. |
| 87 | "ip6table_filter", |
| 88 | "ip6t_ipv6header", |
| 89 | "ip6t_REJECT", |
Garrick Evans | a34b586 | 2019-11-20 09:34:01 +0900 | [diff] [blame] | 90 | // The ipsec modules for AH and ESP encryption for ipv6. |
| 91 | "ah6", |
| 92 | "esp6", |
| 93 | }) != 0) { |
| 94 | LOG(ERROR) << "One or more required kernel modules failed to load." |
| 95 | << " Some Android functionality may be broken."; |
| 96 | } |
Garrick Evans | c53b970 | 2020-05-13 13:20:09 +0900 | [diff] [blame] | 97 | // The xfrm modules needed for Android's ipsec APIs on kernels < 5.4. |
Garrick Evans | 71e4a86 | 2020-05-18 12:22:23 +0900 | [diff] [blame] | 98 | int major, minor; |
| 99 | if (KernelVersion(&major, &minor) && |
| 100 | (major < 5 || (major == 5 && minor < 4)) && |
| 101 | runner.modprobe_all({ |
| 102 | "xfrm4_mode_transport", |
| 103 | "xfrm4_mode_tunnel", |
| 104 | "xfrm6_mode_transport", |
| 105 | "xfrm6_mode_tunnel", |
| 106 | }) != 0) { |
Garrick Evans | c53b970 | 2020-05-13 13:20:09 +0900 | [diff] [blame] | 107 | LOG(ERROR) << "One or more required kernel modules failed to load." |
| 108 | << " Some Android functionality may be broken."; |
| 109 | } |
| 110 | |
Garrick Evans | a34b586 | 2019-11-20 09:34:01 +0900 | [diff] [blame] | 111 | // Optional modules. |
Garrick Evans | 8e8e347 | 2020-01-23 14:03:50 +0900 | [diff] [blame] | 112 | if (runner.modprobe_all({ |
Garrick Evans | a34b586 | 2019-11-20 09:34:01 +0900 | [diff] [blame] | 113 | // This module is not available in kernels < 3.18 |
| 114 | "nf_reject_ipv6", |
| 115 | // These modules are needed for supporting Chrome traffic on Android |
| 116 | // VPN which uses Android's NAT feature. Android NAT sets up |
| 117 | // iptables |
| 118 | // rules that use these conntrack modules for FTP/TFTP. |
| 119 | "nf_nat_ftp", |
| 120 | "nf_nat_tftp", |
Hugo Benichi | a0cde9e | 2019-12-16 11:57:20 +0900 | [diff] [blame] | 121 | // The tun module is needed by the Android 464xlat clatd process. |
| 122 | "tun", |
Garrick Evans | a34b586 | 2019-11-20 09:34:01 +0900 | [diff] [blame] | 123 | }) != 0) { |
| 124 | LOG(WARNING) << "One or more optional kernel modules failed to load."; |
| 125 | } |
| 126 | |
Garrick Evans | 6d227b9 | 2019-12-03 16:11:29 +0900 | [diff] [blame] | 127 | // This is only needed for CTS (b/27932574). |
Hugo Benichi | 0781d40 | 2021-02-22 13:43:11 +0900 | [diff] [blame^] | 128 | SetSysfsOwnerToAndroidRoot(pid, "xt_idletimer"); |
Garrick Evans | 6d227b9 | 2019-12-03 16:11:29 +0900 | [diff] [blame] | 129 | |
Garrick Evans | a34b586 | 2019-11-20 09:34:01 +0900 | [diff] [blame] | 130 | done = true; |
| 131 | } |
| 132 | |
Garrick Evans | 86c7d9c | 2020-03-17 09:25:48 +0900 | [diff] [blame] | 133 | ArcService::InterfaceType InterfaceTypeFor(const std::string& ifname) { |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 134 | for (const auto& prefix : kEthernetInterfacePrefixes) { |
| 135 | if (base::StartsWith(ifname, prefix, |
| 136 | base::CompareCase::INSENSITIVE_ASCII)) { |
Garrick Evans | 86c7d9c | 2020-03-17 09:25:48 +0900 | [diff] [blame] | 137 | return ArcService::InterfaceType::ETHERNET; |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 138 | } |
| 139 | } |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 140 | for (const auto& prefix : kWifiInterfacePrefixes) { |
| 141 | if (base::StartsWith(ifname, prefix, |
| 142 | base::CompareCase::INSENSITIVE_ASCII)) { |
Garrick Evans | 86c7d9c | 2020-03-17 09:25:48 +0900 | [diff] [blame] | 143 | return ArcService::InterfaceType::WIFI; |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 144 | } |
| 145 | } |
Garrick Evans | 86c7d9c | 2020-03-17 09:25:48 +0900 | [diff] [blame] | 146 | for (const auto& prefix : kCellInterfacePrefixes) { |
| 147 | if (base::StartsWith(ifname, prefix, |
| 148 | base::CompareCase::INSENSITIVE_ASCII)) { |
| 149 | return ArcService::InterfaceType::CELL; |
| 150 | } |
| 151 | } |
| 152 | return ArcService::InterfaceType::UNKNOWN; |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | bool IsMulticastInterface(const std::string& ifname) { |
| 156 | if (ifname.empty()) { |
| 157 | return false; |
| 158 | } |
| 159 | |
| 160 | int fd = socket(AF_INET, SOCK_DGRAM, 0); |
| 161 | if (fd < 0) { |
| 162 | // If IPv4 fails, try to open a socket using IPv6. |
| 163 | fd = socket(AF_INET6, SOCK_DGRAM, 0); |
| 164 | if (fd < 0) { |
| 165 | LOG(ERROR) << "Unable to create socket"; |
| 166 | return false; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | struct ifreq ifr; |
| 171 | memset(&ifr, 0, sizeof(ifr)); |
| 172 | strncpy(ifr.ifr_name, ifname.c_str(), IFNAMSIZ); |
| 173 | if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0) { |
| 174 | PLOG(ERROR) << "SIOCGIFFLAGS failed for " << ifname; |
| 175 | close(fd); |
| 176 | return false; |
| 177 | } |
| 178 | |
| 179 | close(fd); |
| 180 | return (ifr.ifr_flags & IFF_MULTICAST); |
| 181 | } |
| 182 | |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame] | 183 | // Returns the ARC management device used for VPN forwarding, ADB-over-TCP. |
| 184 | std::unique_ptr<Device> MakeArcDevice(AddressManager* addr_mgr, |
| 185 | GuestMessage::GuestType guest) { |
| 186 | auto ipv4_subnet = addr_mgr->AllocateIPv4Subnet(AddressManager::Guest::ARC); |
Garrick Evans | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 187 | if (!ipv4_subnet) { |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 188 | LOG(ERROR) << "Subnet already in use or unavailable"; |
Garrick Evans | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 189 | return nullptr; |
| 190 | } |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame] | 191 | |
Garrick Evans | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 192 | auto host_ipv4_addr = ipv4_subnet->AllocateAtOffset(0); |
| 193 | if (!host_ipv4_addr) { |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 194 | LOG(ERROR) << "Bridge address already in use or unavailable"; |
Garrick Evans | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 195 | return nullptr; |
| 196 | } |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame] | 197 | |
Garrick Evans | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 198 | auto guest_ipv4_addr = ipv4_subnet->AllocateAtOffset(1); |
| 199 | if (!guest_ipv4_addr) { |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 200 | LOG(ERROR) << "ARC address already in use or unavailable"; |
Garrick Evans | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 201 | return nullptr; |
| 202 | } |
| 203 | |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame] | 204 | int subnet_index = (guest == GuestMessage::ARC_VM) ? 1 : kAnySubnetIndex; |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 205 | |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame] | 206 | auto config = std::make_unique<Device::Config>( |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 207 | addr_mgr->GenerateMacAddress(subnet_index), std::move(ipv4_subnet), |
| 208 | std::move(host_ipv4_addr), std::move(guest_ipv4_addr)); |
Garrick Evans | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 209 | |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame] | 210 | Device::Options opts{ |
| 211 | .fwd_multicast = false, |
| 212 | .ipv6_enabled = false, |
Jason Jeremy Iman | 3081d0e | 2020-03-04 15:52:06 +0900 | [diff] [blame] | 213 | .adb_allowed = false, |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame] | 214 | }; |
| 215 | |
| 216 | return std::make_unique<Device>(kArcIfname, kArcBridge, kArcIfname, |
| 217 | std::move(config), opts); |
| 218 | } |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 219 | } // namespace |
| 220 | |
Garrick Evans | 69b8587 | 2020-02-04 11:40:26 +0900 | [diff] [blame] | 221 | ArcService::ArcService(ShillClient* shill_client, |
Garrick Evans | 2e5c9ab | 2020-03-05 14:33:58 +0900 | [diff] [blame] | 222 | Datapath* datapath, |
| 223 | AddressManager* addr_mgr, |
Garrick Evans | f586212 | 2020-03-16 09:13:45 +0900 | [diff] [blame] | 224 | TrafficForwarder* forwarder, |
Garrick Evans | 209a80a | 2020-11-30 14:42:40 +0900 | [diff] [blame] | 225 | GuestMessage::GuestType guest, |
| 226 | Device::ChangeEventHandler device_changed_handler) |
Garrick Evans | 2e5c9ab | 2020-03-05 14:33:58 +0900 | [diff] [blame] | 227 | : shill_client_(shill_client), |
Garrick Evans | 2e5c9ab | 2020-03-05 14:33:58 +0900 | [diff] [blame] | 228 | datapath_(datapath), |
| 229 | addr_mgr_(addr_mgr), |
Garrick Evans | f586212 | 2020-03-16 09:13:45 +0900 | [diff] [blame] | 230 | forwarder_(forwarder), |
Hugo Benichi | 4d4bb8f | 2020-07-07 12:16:07 +0900 | [diff] [blame] | 231 | guest_(guest), |
Garrick Evans | 209a80a | 2020-11-30 14:42:40 +0900 | [diff] [blame] | 232 | device_changed_handler_(device_changed_handler), |
Hugo Benichi | 4d4bb8f | 2020-07-07 12:16:07 +0900 | [diff] [blame] | 233 | id_(kInvalidId) { |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame] | 234 | arc_device_ = MakeArcDevice(addr_mgr, guest_); |
Garrick Evans | 86c7d9c | 2020-03-17 09:25:48 +0900 | [diff] [blame] | 235 | AllocateAddressConfigs(); |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 236 | shill_client_->RegisterDevicesChangedHandler( |
| 237 | base::Bind(&ArcService::OnDevicesChanged, weak_factory_.GetWeakPtr())); |
Jie Jiang | 84c76a1 | 2020-04-17 16:45:20 +0900 | [diff] [blame] | 238 | shill_client_->ScanDevices(); |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | ArcService::~ArcService() { |
Hugo Benichi | 4d4bb8f | 2020-07-07 12:16:07 +0900 | [diff] [blame] | 242 | if (IsStarted()) { |
| 243 | Stop(id_); |
Garrick Evans | 664a82f | 2019-12-17 12:18:05 +0900 | [diff] [blame] | 244 | } |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 245 | } |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 246 | |
Hugo Benichi | 4d4bb8f | 2020-07-07 12:16:07 +0900 | [diff] [blame] | 247 | bool ArcService::IsStarted() const { |
| 248 | return id_ != kInvalidId; |
| 249 | } |
| 250 | |
Garrick Evans | 86c7d9c | 2020-03-17 09:25:48 +0900 | [diff] [blame] | 251 | void ArcService::AllocateAddressConfigs() { |
Garrick Evans | 86c7d9c | 2020-03-17 09:25:48 +0900 | [diff] [blame] | 252 | // The first usable subnet is the "other" ARC device subnet. |
Garrick Evans | c707112 | 2020-04-17 12:31:57 +0900 | [diff] [blame] | 253 | // As a temporary workaround, for ARCVM, allocate fixed MAC addresses. |
| 254 | uint8_t mac_addr_index = 2; |
Garrick Evans | 86c7d9c | 2020-03-17 09:25:48 +0900 | [diff] [blame] | 255 | // Allocate 2 subnets each for Ethernet and WiFi and 1 for LTE WAN interfaces. |
| 256 | for (const auto itype : |
| 257 | {InterfaceType::ETHERNET, InterfaceType::ETHERNET, InterfaceType::WIFI, |
| 258 | InterfaceType::WIFI, InterfaceType::CELL}) { |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 259 | auto ipv4_subnet = |
| 260 | addr_mgr_->AllocateIPv4Subnet(AddressManager::Guest::ARC_NET); |
Garrick Evans | 86c7d9c | 2020-03-17 09:25:48 +0900 | [diff] [blame] | 261 | if (!ipv4_subnet) { |
| 262 | LOG(ERROR) << "Subnet already in use or unavailable"; |
| 263 | continue; |
| 264 | } |
| 265 | // For here out, use the same slices. |
Garrick Evans | 86c7d9c | 2020-03-17 09:25:48 +0900 | [diff] [blame] | 266 | auto host_ipv4_addr = ipv4_subnet->AllocateAtOffset(0); |
| 267 | if (!host_ipv4_addr) { |
| 268 | LOG(ERROR) << "Bridge address already in use or unavailable"; |
| 269 | continue; |
| 270 | } |
| 271 | auto guest_ipv4_addr = ipv4_subnet->AllocateAtOffset(1); |
| 272 | if (!guest_ipv4_addr) { |
| 273 | LOG(ERROR) << "ARC address already in use or unavailable"; |
| 274 | continue; |
| 275 | } |
| 276 | |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 277 | MacAddress mac_addr = (guest_ == GuestMessage::ARC_VM) |
Garrick Evans | c707112 | 2020-04-17 12:31:57 +0900 | [diff] [blame] | 278 | ? addr_mgr_->GenerateMacAddress(mac_addr_index++) |
| 279 | : addr_mgr_->GenerateMacAddress(); |
Hugo Benichi | 8e44842 | 2020-07-07 10:49:00 +0900 | [diff] [blame] | 280 | available_configs_[itype].emplace_back(std::make_unique<Device::Config>( |
Garrick Evans | c707112 | 2020-04-17 12:31:57 +0900 | [diff] [blame] | 281 | mac_addr, std::move(ipv4_subnet), std::move(host_ipv4_addr), |
| 282 | std::move(guest_ipv4_addr))); |
Garrick Evans | 86c7d9c | 2020-03-17 09:25:48 +0900 | [diff] [blame] | 283 | } |
Garrick Evans | 86c7d9c | 2020-03-17 09:25:48 +0900 | [diff] [blame] | 284 | |
Hugo Benichi | 8e44842 | 2020-07-07 10:49:00 +0900 | [diff] [blame] | 285 | for (const auto& kv : available_configs_) |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 286 | for (const auto& c : kv.second) |
Hugo Benichi | 8e44842 | 2020-07-07 10:49:00 +0900 | [diff] [blame] | 287 | all_configs_.emplace_back(c.get()); |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame] | 288 | // Append arc0 config so that the necessary tap device gets created. |
| 289 | all_configs_.insert(all_configs_.begin(), &arc_device_->config()); |
Garrick Evans | 86c7d9c | 2020-03-17 09:25:48 +0900 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | std::unique_ptr<Device::Config> ArcService::AcquireConfig( |
| 293 | const std::string& ifname) { |
| 294 | auto itype = InterfaceTypeFor(ifname); |
| 295 | if (itype == InterfaceType::UNKNOWN) { |
| 296 | LOG(ERROR) << "Unsupported interface: " << ifname; |
| 297 | return nullptr; |
| 298 | } |
| 299 | |
Hugo Benichi | 8e44842 | 2020-07-07 10:49:00 +0900 | [diff] [blame] | 300 | auto& configs = available_configs_[itype]; |
Garrick Evans | 86c7d9c | 2020-03-17 09:25:48 +0900 | [diff] [blame] | 301 | if (configs.empty()) { |
| 302 | LOG(ERROR) << "No more addresses available. Cannot make device for " |
| 303 | << ifname; |
| 304 | return nullptr; |
| 305 | } |
| 306 | std::unique_ptr<Device::Config> config; |
| 307 | config = std::move(configs.front()); |
| 308 | configs.pop_front(); |
| 309 | return config; |
| 310 | } |
| 311 | |
| 312 | void ArcService::ReleaseConfig(const std::string& ifname, |
| 313 | std::unique_ptr<Device::Config> config) { |
| 314 | auto itype = InterfaceTypeFor(ifname); |
| 315 | if (itype == InterfaceType::UNKNOWN) { |
| 316 | LOG(ERROR) << "Unsupported interface: " << ifname; |
| 317 | return; |
| 318 | } |
| 319 | |
Hugo Benichi | 8e44842 | 2020-07-07 10:49:00 +0900 | [diff] [blame] | 320 | available_configs_[itype].push_front(std::move(config)); |
Garrick Evans | 86c7d9c | 2020-03-17 09:25:48 +0900 | [diff] [blame] | 321 | } |
| 322 | |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 323 | bool ArcService::Start(uint32_t id) { |
Hugo Benichi | 4d4bb8f | 2020-07-07 12:16:07 +0900 | [diff] [blame] | 324 | if (IsStarted()) { |
| 325 | LOG(WARNING) << "Already running - did something crash?" |
| 326 | << " Stopping and restarting..."; |
| 327 | Stop(id_); |
Garrick Evans | a51d0a1 | 2019-11-28 13:51:23 +0900 | [diff] [blame] | 328 | } |
| 329 | |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame] | 330 | std::string arc_device_ifname; |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 331 | if (guest_ == GuestMessage::ARC_VM) { |
Hugo Benichi | 8e44842 | 2020-07-07 10:49:00 +0900 | [diff] [blame] | 332 | // Allocate TAP devices for all configs. |
| 333 | for (auto* config : all_configs_) { |
| 334 | auto mac = config->mac_addr(); |
| 335 | auto tap = datapath_->AddTAP("" /* auto-generate name */, &mac, |
| 336 | nullptr /* no ipv4 subnet */, |
| 337 | vm_tools::kCrosVmUser); |
| 338 | if (tap.empty()) { |
| 339 | LOG(ERROR) << "Failed to create TAP device"; |
| 340 | continue; |
| 341 | } |
| 342 | |
| 343 | config->set_tap_ifname(tap); |
| 344 | } |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame] | 345 | arc_device_ifname = arc_device_->config().tap_ifname(); |
Garrick Evans | 2961c7c | 2020-04-03 11:34:40 +0900 | [diff] [blame] | 346 | } else { |
Hugo Benichi | 0781d40 | 2021-02-22 13:43:11 +0900 | [diff] [blame^] | 347 | OneTimeContainerSetup(*datapath_, id); |
Hugo Benichi | 33860d7 | 2020-07-09 16:34:01 +0900 | [diff] [blame] | 348 | if (!datapath_->NetnsAttachName(kArcNetnsName, id)) { |
| 349 | LOG(ERROR) << "Failed to attach name " << kArcNetnsName << " to pid " |
| 350 | << id; |
| 351 | return false; |
| 352 | } |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 353 | arc_device_ifname = ArcVethHostName(arc_device_->guest_ifname()); |
Hugo Benichi | 4d4bb8f | 2020-07-07 12:16:07 +0900 | [diff] [blame] | 354 | if (!datapath_->ConnectVethPair(id, kArcNetnsName, arc_device_ifname, |
| 355 | arc_device_->guest_ifname(), |
| 356 | arc_device_->config().mac_addr(), |
| 357 | arc_device_->config().guest_ipv4_addr(), 30, |
| 358 | arc_device_->options().fwd_multicast)) { |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 359 | LOG(ERROR) << "Cannot create virtual link for device " |
| 360 | << arc_device_->phys_ifname(); |
| 361 | return false; |
| 362 | } |
Hugo Benichi | 40fbf0b | 2021-01-20 19:44:15 +0900 | [diff] [blame] | 363 | // Allow netd to write to /sys/class/net/arc0/mtu (b/175571457). |
Hugo Benichi | 0781d40 | 2021-02-22 13:43:11 +0900 | [diff] [blame^] | 364 | SetSysfsOwnerToAndroidRoot(id, |
| 365 | "net/" + arc_device_->guest_ifname() + "/mtu"); |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 366 | } |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame] | 367 | id_ = id; |
| 368 | |
| 369 | // Create the bridge for the management device arc0. |
| 370 | // Per crbug/1008686 this device cannot be deleted and then re-added. |
| 371 | // So instead of removing the bridge when the service stops, bring down the |
| 372 | // device instead and re-up it on restart. |
| 373 | if (!datapath_->AddBridge(kArcBridge, arc_device_->config().host_ipv4_addr(), |
| 374 | 30) && |
| 375 | !datapath_->MaskInterfaceFlags(kArcBridge, IFF_UP)) { |
| 376 | LOG(ERROR) << "Failed to bring up arc bridge " << kArcBridge; |
| 377 | return false; |
| 378 | } |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 379 | |
| 380 | if (!datapath_->AddToBridge(kArcBridge, arc_device_ifname)) { |
| 381 | LOG(ERROR) << "Failed to bridge arc device " << arc_device_ifname << " to " |
| 382 | << kArcBridge; |
| 383 | return false; |
| 384 | } |
Hugo Benichi | 33860d7 | 2020-07-09 16:34:01 +0900 | [diff] [blame] | 385 | LOG(INFO) << "Started ARC management device " << *arc_device_.get(); |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 386 | |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 387 | // Start already known Shill <-> ARC mapped devices. |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame] | 388 | for (const auto& ifname : shill_devices_) |
| 389 | AddDevice(ifname); |
Garrick Evans | cb791e7 | 2019-11-11 15:44:34 +0900 | [diff] [blame] | 390 | |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 391 | return true; |
Garrick Evans | cb791e7 | 2019-11-11 15:44:34 +0900 | [diff] [blame] | 392 | } |
| 393 | |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 394 | void ArcService::Stop(uint32_t id) { |
Hugo Benichi | 4d4bb8f | 2020-07-07 12:16:07 +0900 | [diff] [blame] | 395 | if (!IsStarted()) { |
| 396 | LOG(ERROR) << "ArcService was not running"; |
| 397 | return; |
| 398 | } |
| 399 | |
| 400 | // After the ARC container has stopped, the pid is not known anymore. |
| 401 | if (guest_ == GuestMessage::ARC_VM && id_ != id) { |
| 402 | LOG(ERROR) << "Mismatched ARCVM CIDs " << id_ << " != " << id; |
| 403 | return; |
| 404 | } |
| 405 | |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 406 | // Stop Shill <-> ARC mapped devices. |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame] | 407 | for (const auto& ifname : shill_devices_) |
| 408 | RemoveDevice(ifname); |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 409 | |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 410 | // Per crbug/1008686 this device cannot be deleted and then re-added. |
| 411 | // So instead of removing the bridge, bring it down and mark it. This will |
| 412 | // allow us to detect if the device is re-added in case of a crash restart |
| 413 | // and do the right thing. |
| 414 | if (!datapath_->MaskInterfaceFlags(kArcBridge, IFF_DEBUG, IFF_UP)) |
| 415 | LOG(ERROR) << "Failed to bring down arc bridge " |
| 416 | << "- it may not restart correctly"; |
| 417 | |
Hugo Benichi | 33860d7 | 2020-07-09 16:34:01 +0900 | [diff] [blame] | 418 | if (guest_ == GuestMessage::ARC) { |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 419 | datapath_->RemoveInterface(ArcVethHostName(arc_device_->phys_ifname())); |
Hugo Benichi | 33860d7 | 2020-07-09 16:34:01 +0900 | [diff] [blame] | 420 | if (!datapath_->NetnsDeleteName(kArcNetnsName)) |
| 421 | LOG(WARNING) << "Failed to delete netns name " << kArcNetnsName; |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | // Destroy allocated TAP devices if any, including the ARC management device. |
| 425 | for (auto* config : all_configs_) { |
| 426 | if (config->tap_ifname().empty()) |
| 427 | continue; |
| 428 | datapath_->RemoveInterface(config->tap_ifname()); |
| 429 | config->set_tap_ifname(""); |
Hugo Benichi | 33860d7 | 2020-07-09 16:34:01 +0900 | [diff] [blame] | 430 | } |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 431 | |
Hugo Benichi | 33860d7 | 2020-07-09 16:34:01 +0900 | [diff] [blame] | 432 | LOG(INFO) << "Stopped ARC management device " << *arc_device_.get(); |
Hugo Benichi | 4d4bb8f | 2020-07-07 12:16:07 +0900 | [diff] [blame] | 433 | id_ = kInvalidId; |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 434 | } |
| 435 | |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 436 | void ArcService::OnDevicesChanged(const std::set<std::string>& added, |
| 437 | const std::set<std::string>& removed) { |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame] | 438 | for (const std::string& ifname : removed) { |
| 439 | shill_devices_.erase(ifname); |
| 440 | RemoveDevice(ifname); |
| 441 | } |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 442 | |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame] | 443 | for (const std::string& ifname : added) { |
| 444 | shill_devices_.insert(ifname); |
| 445 | AddDevice(ifname); |
| 446 | } |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | void ArcService::AddDevice(const std::string& ifname) { |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame] | 450 | if (!IsStarted()) |
| 451 | return; |
| 452 | |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 453 | if (ifname.empty()) |
| 454 | return; |
| 455 | |
| 456 | if (devices_.find(ifname) != devices_.end()) { |
| 457 | LOG(DFATAL) << "Attemping to add already tracked device: " << ifname; |
| 458 | return; |
| 459 | } |
| 460 | |
Garrick Evans | 86c7d9c | 2020-03-17 09:25:48 +0900 | [diff] [blame] | 461 | auto itype = InterfaceTypeFor(ifname); |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 462 | Device::Options opts{ |
| 463 | .fwd_multicast = IsMulticastInterface(ifname), |
| 464 | // TODO(crbug/726815) Also enable |ipv6_enabled| for cellular networks |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame] | 465 | // once IPv6 is enabled on cellular networks in Shill. |
Garrick Evans | 86c7d9c | 2020-03-17 09:25:48 +0900 | [diff] [blame] | 466 | .ipv6_enabled = |
| 467 | (itype == InterfaceType::ETHERNET || itype == InterfaceType::WIFI), |
Jason Jeremy Iman | 3081d0e | 2020-03-04 15:52:06 +0900 | [diff] [blame] | 468 | .adb_allowed = |
| 469 | (itype == InterfaceType::ETHERNET || itype == InterfaceType::WIFI), |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 470 | }; |
Garrick Evans | 86c7d9c | 2020-03-17 09:25:48 +0900 | [diff] [blame] | 471 | |
| 472 | auto config = AcquireConfig(ifname); |
| 473 | if (!config) { |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame] | 474 | LOG(ERROR) << "Cannot acquire a Config for " << ifname; |
Garrick Evans | 86c7d9c | 2020-03-17 09:25:48 +0900 | [diff] [blame] | 475 | return; |
| 476 | } |
| 477 | |
Garrick Evans | 8a06756 | 2020-05-11 12:47:30 +0900 | [diff] [blame] | 478 | auto device = std::make_unique<Device>(ifname, ArcBridgeName(ifname), ifname, |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 479 | std::move(config), opts); |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 480 | LOG(INFO) << "Starting device " << *device; |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 481 | |
| 482 | // Create the bridge. |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame] | 483 | if (!datapath_->AddBridge(device->host_ifname(), |
| 484 | device->config().host_ipv4_addr(), 30)) { |
| 485 | LOG(ERROR) << "Failed to setup bridge " << device->host_ifname(); |
Garrick Evans | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 486 | return; |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 487 | } |
| 488 | |
Hugo Benichi | 8d622b5 | 2020-08-13 15:24:12 +0900 | [diff] [blame] | 489 | datapath_->StartRoutingDevice(device->phys_ifname(), device->host_ifname(), |
| 490 | device->config().guest_ipv4_addr(), |
Hugo Benichi | 93306e5 | 2020-12-04 16:08:00 +0900 | [diff] [blame] | 491 | TrafficSource::ARC, false /*route_on_vpn*/); |
Garrick Evans | 2c26310 | 2019-07-26 16:07:18 +0900 | [diff] [blame] | 492 | |
Hugo Benichi | cf90402 | 2020-07-07 09:21:58 +0900 | [diff] [blame] | 493 | std::string virtual_device_ifname; |
| 494 | if (guest_ == GuestMessage::ARC_VM) { |
| 495 | virtual_device_ifname = device->config().tap_ifname(); |
| 496 | if (virtual_device_ifname.empty()) { |
| 497 | LOG(ERROR) << "No TAP device for " << *device; |
| 498 | return; |
| 499 | } |
| 500 | } else { |
| 501 | virtual_device_ifname = ArcVethHostName(device->guest_ifname()); |
| 502 | if (!datapath_->ConnectVethPair( |
Hugo Benichi | 4d4bb8f | 2020-07-07 12:16:07 +0900 | [diff] [blame] | 503 | id_, kArcNetnsName, virtual_device_ifname, device->guest_ifname(), |
Hugo Benichi | cf90402 | 2020-07-07 09:21:58 +0900 | [diff] [blame] | 504 | device->config().mac_addr(), device->config().guest_ipv4_addr(), 30, |
| 505 | device->options().fwd_multicast)) { |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame] | 506 | LOG(ERROR) << "Cannot create veth link for device " << *device; |
Hugo Benichi | cf90402 | 2020-07-07 09:21:58 +0900 | [diff] [blame] | 507 | return; |
| 508 | } |
Hugo Benichi | 6c0233e | 2020-10-06 22:37:57 +0900 | [diff] [blame] | 509 | // Allow netd to write to /sys/class/net/<guest_ifname>/mtu (b/169936104). |
Hugo Benichi | 0781d40 | 2021-02-22 13:43:11 +0900 | [diff] [blame^] | 510 | SetSysfsOwnerToAndroidRoot(id_, "net/" + device->guest_ifname() + "/mtu"); |
Hugo Benichi | cf90402 | 2020-07-07 09:21:58 +0900 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | if (!datapath_->AddToBridge(device->host_ifname(), virtual_device_ifname)) { |
| 514 | if (guest_ == GuestMessage::ARC) { |
| 515 | datapath_->RemoveInterface(virtual_device_ifname); |
| 516 | } |
| 517 | LOG(ERROR) << "Failed to bridge interface " << virtual_device_ifname; |
Hugo Benichi | 4833fda | 2020-07-06 14:56:56 +0900 | [diff] [blame] | 518 | return; |
| 519 | } |
| 520 | |
Jason Jeremy Iman | 3081d0e | 2020-03-04 15:52:06 +0900 | [diff] [blame] | 521 | if (device->options().adb_allowed && |
| 522 | !datapath_->AddAdbPortAccessRule(ifname)) { |
| 523 | LOG(ERROR) << "Failed to add ADB port access rule"; |
| 524 | } |
| 525 | |
Hugo Benichi | 4833fda | 2020-07-06 14:56:56 +0900 | [diff] [blame] | 526 | forwarder_->StartForwarding(device->phys_ifname(), device->host_ifname(), |
| 527 | device->options().ipv6_enabled, |
| 528 | device->options().fwd_multicast); |
Garrick Evans | 209a80a | 2020-11-30 14:42:40 +0900 | [diff] [blame] | 529 | device_changed_handler_.Run(*device, Device::ChangeEvent::ADDED, guest_); |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame] | 530 | devices_.emplace(ifname, std::move(device)); |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | void ArcService::RemoveDevice(const std::string& ifname) { |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame] | 534 | if (!IsStarted()) |
| 535 | return; |
| 536 | |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 537 | const auto it = devices_.find(ifname); |
| 538 | if (it == devices_.end()) { |
| 539 | LOG(WARNING) << "Unknown device: " << ifname; |
Garrick Evans | cb791e7 | 2019-11-11 15:44:34 +0900 | [diff] [blame] | 540 | return; |
| 541 | } |
| 542 | |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame] | 543 | const auto* device = it->second.get(); |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 544 | LOG(INFO) << "Removing device " << *device; |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 545 | |
Garrick Evans | 209a80a | 2020-11-30 14:42:40 +0900 | [diff] [blame] | 546 | device_changed_handler_.Run(*device, Device::ChangeEvent::REMOVED, guest_); |
| 547 | |
Hugo Benichi | 4833fda | 2020-07-06 14:56:56 +0900 | [diff] [blame] | 548 | forwarder_->StopForwarding(device->phys_ifname(), device->host_ifname(), |
| 549 | device->options().ipv6_enabled, |
| 550 | device->options().fwd_multicast); |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame] | 551 | |
| 552 | // ARCVM TAP devices are removed in VmImpl::Stop() when the service stops |
Hugo Benichi | 4833fda | 2020-07-06 14:56:56 +0900 | [diff] [blame] | 553 | if (guest_ == GuestMessage::ARC) |
| 554 | datapath_->RemoveInterface(ArcVethHostName(device->phys_ifname())); |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 555 | |
Hugo Benichi | 8d622b5 | 2020-08-13 15:24:12 +0900 | [diff] [blame] | 556 | datapath_->StopRoutingDevice(device->phys_ifname(), device->host_ifname(), |
| 557 | device->config().guest_ipv4_addr(), |
Hugo Benichi | 93306e5 | 2020-12-04 16:08:00 +0900 | [diff] [blame] | 558 | TrafficSource::ARC, false /*route_on_vpn*/); |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 559 | datapath_->RemoveBridge(device->host_ifname()); |
Jason Jeremy Iman | 3081d0e | 2020-03-04 15:52:06 +0900 | [diff] [blame] | 560 | if (device->options().adb_allowed) |
| 561 | datapath_->DeleteAdbPortAccessRule(ifname); |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame] | 562 | |
| 563 | ReleaseConfig(ifname, it->second->release_config()); |
| 564 | devices_.erase(it); |
Garrick Evans | cb791e7 | 2019-11-11 15:44:34 +0900 | [diff] [blame] | 565 | } |
| 566 | |
Garrick Evans | 38b25a4 | 2020-04-06 15:17:42 +0900 | [diff] [blame] | 567 | std::vector<const Device::Config*> ArcService::GetDeviceConfigs() const { |
Hugo Benichi | 8e44842 | 2020-07-07 10:49:00 +0900 | [diff] [blame] | 568 | std::vector<const Device::Config*> configs; |
| 569 | for (auto* c : all_configs_) |
| 570 | configs.emplace_back(c); |
Garrick Evans | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 571 | |
Hugo Benichi | 8e44842 | 2020-07-07 10:49:00 +0900 | [diff] [blame] | 572 | return configs; |
Garrick Evans | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 573 | } |
Garrick Evans | 02e6e87 | 2020-11-30 11:53:13 +0900 | [diff] [blame] | 574 | |
| 575 | void ArcService::ScanDevices( |
| 576 | base::RepeatingCallback<void(const Device&)> callback) const { |
| 577 | for (const auto& [_, d] : devices_) |
| 578 | callback.Run(*d.get()); |
| 579 | } |
| 580 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 581 | } // namespace patchpanel |