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