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