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 | |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame^] | 59 | void OneTimeContainerSetup(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 | |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame^] | 169 | // Returns the ARC management device used for VPN forwarding, ADB-over-TCP. |
| 170 | std::unique_ptr<Device> MakeArcDevice(AddressManager* addr_mgr, |
| 171 | GuestMessage::GuestType guest) { |
| 172 | auto ipv4_subnet = addr_mgr->AllocateIPv4Subnet(AddressManager::Guest::ARC); |
Garrick Evans | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 173 | if (!ipv4_subnet) { |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 174 | LOG(ERROR) << "Subnet already in use or unavailable"; |
Garrick Evans | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 175 | return nullptr; |
| 176 | } |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame^] | 177 | |
Garrick Evans | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 178 | auto host_ipv4_addr = ipv4_subnet->AllocateAtOffset(0); |
| 179 | if (!host_ipv4_addr) { |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 180 | LOG(ERROR) << "Bridge address already in use or unavailable"; |
Garrick Evans | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 181 | return nullptr; |
| 182 | } |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame^] | 183 | |
Garrick Evans | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 184 | auto guest_ipv4_addr = ipv4_subnet->AllocateAtOffset(1); |
| 185 | if (!guest_ipv4_addr) { |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 186 | LOG(ERROR) << "ARC address already in use or unavailable"; |
Garrick Evans | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 187 | return nullptr; |
| 188 | } |
| 189 | |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame^] | 190 | int subnet_index = (guest == GuestMessage::ARC_VM) ? 1 : kAnySubnetIndex; |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 191 | |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame^] | 192 | auto config = std::make_unique<Device::Config>( |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 193 | addr_mgr->GenerateMacAddress(subnet_index), std::move(ipv4_subnet), |
| 194 | std::move(host_ipv4_addr), std::move(guest_ipv4_addr)); |
Garrick Evans | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 195 | |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame^] | 196 | Device::Options opts{ |
| 197 | .fwd_multicast = false, |
| 198 | .ipv6_enabled = false, |
| 199 | }; |
| 200 | |
| 201 | return std::make_unique<Device>(kArcIfname, kArcBridge, kArcIfname, |
| 202 | std::move(config), opts); |
| 203 | } |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 204 | } // namespace |
| 205 | |
Garrick Evans | 69b8587 | 2020-02-04 11:40:26 +0900 | [diff] [blame] | 206 | ArcService::ArcService(ShillClient* shill_client, |
Garrick Evans | 2e5c9ab | 2020-03-05 14:33:58 +0900 | [diff] [blame] | 207 | Datapath* datapath, |
| 208 | AddressManager* addr_mgr, |
Garrick Evans | f586212 | 2020-03-16 09:13:45 +0900 | [diff] [blame] | 209 | TrafficForwarder* forwarder, |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 210 | GuestMessage::GuestType guest) |
Garrick Evans | 2e5c9ab | 2020-03-05 14:33:58 +0900 | [diff] [blame] | 211 | : shill_client_(shill_client), |
Garrick Evans | 2e5c9ab | 2020-03-05 14:33:58 +0900 | [diff] [blame] | 212 | datapath_(datapath), |
| 213 | addr_mgr_(addr_mgr), |
Garrick Evans | f586212 | 2020-03-16 09:13:45 +0900 | [diff] [blame] | 214 | forwarder_(forwarder), |
Hugo Benichi | 4d4bb8f | 2020-07-07 12:16:07 +0900 | [diff] [blame] | 215 | guest_(guest), |
| 216 | id_(kInvalidId) { |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame^] | 217 | arc_device_ = MakeArcDevice(addr_mgr, guest_); |
Garrick Evans | 86c7d9c | 2020-03-17 09:25:48 +0900 | [diff] [blame] | 218 | AllocateAddressConfigs(); |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 219 | shill_client_->RegisterDevicesChangedHandler( |
| 220 | base::Bind(&ArcService::OnDevicesChanged, weak_factory_.GetWeakPtr())); |
Jie Jiang | 84c76a1 | 2020-04-17 16:45:20 +0900 | [diff] [blame] | 221 | shill_client_->ScanDevices(); |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | ArcService::~ArcService() { |
Hugo Benichi | 4d4bb8f | 2020-07-07 12:16:07 +0900 | [diff] [blame] | 225 | if (IsStarted()) { |
| 226 | Stop(id_); |
Garrick Evans | 664a82f | 2019-12-17 12:18:05 +0900 | [diff] [blame] | 227 | } |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 228 | } |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 229 | |
Hugo Benichi | 4d4bb8f | 2020-07-07 12:16:07 +0900 | [diff] [blame] | 230 | bool ArcService::IsStarted() const { |
| 231 | return id_ != kInvalidId; |
| 232 | } |
| 233 | |
Garrick Evans | 86c7d9c | 2020-03-17 09:25:48 +0900 | [diff] [blame] | 234 | void ArcService::AllocateAddressConfigs() { |
Garrick Evans | 86c7d9c | 2020-03-17 09:25:48 +0900 | [diff] [blame] | 235 | // The first usable subnet is the "other" ARC device subnet. |
Garrick Evans | c707112 | 2020-04-17 12:31:57 +0900 | [diff] [blame] | 236 | // As a temporary workaround, for ARCVM, allocate fixed MAC addresses. |
| 237 | uint8_t mac_addr_index = 2; |
Garrick Evans | 86c7d9c | 2020-03-17 09:25:48 +0900 | [diff] [blame] | 238 | // Allocate 2 subnets each for Ethernet and WiFi and 1 for LTE WAN interfaces. |
| 239 | for (const auto itype : |
| 240 | {InterfaceType::ETHERNET, InterfaceType::ETHERNET, InterfaceType::WIFI, |
| 241 | InterfaceType::WIFI, InterfaceType::CELL}) { |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 242 | auto ipv4_subnet = |
| 243 | addr_mgr_->AllocateIPv4Subnet(AddressManager::Guest::ARC_NET); |
Garrick Evans | 86c7d9c | 2020-03-17 09:25:48 +0900 | [diff] [blame] | 244 | if (!ipv4_subnet) { |
| 245 | LOG(ERROR) << "Subnet already in use or unavailable"; |
| 246 | continue; |
| 247 | } |
| 248 | // For here out, use the same slices. |
Garrick Evans | 86c7d9c | 2020-03-17 09:25:48 +0900 | [diff] [blame] | 249 | auto host_ipv4_addr = ipv4_subnet->AllocateAtOffset(0); |
| 250 | if (!host_ipv4_addr) { |
| 251 | LOG(ERROR) << "Bridge address already in use or unavailable"; |
| 252 | continue; |
| 253 | } |
| 254 | auto guest_ipv4_addr = ipv4_subnet->AllocateAtOffset(1); |
| 255 | if (!guest_ipv4_addr) { |
| 256 | LOG(ERROR) << "ARC address already in use or unavailable"; |
| 257 | continue; |
| 258 | } |
| 259 | |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 260 | MacAddress mac_addr = (guest_ == GuestMessage::ARC_VM) |
Garrick Evans | c707112 | 2020-04-17 12:31:57 +0900 | [diff] [blame] | 261 | ? addr_mgr_->GenerateMacAddress(mac_addr_index++) |
| 262 | : addr_mgr_->GenerateMacAddress(); |
Hugo Benichi | 8e44842 | 2020-07-07 10:49:00 +0900 | [diff] [blame] | 263 | available_configs_[itype].emplace_back(std::make_unique<Device::Config>( |
Garrick Evans | c707112 | 2020-04-17 12:31:57 +0900 | [diff] [blame] | 264 | mac_addr, std::move(ipv4_subnet), std::move(host_ipv4_addr), |
| 265 | std::move(guest_ipv4_addr))); |
Garrick Evans | 86c7d9c | 2020-03-17 09:25:48 +0900 | [diff] [blame] | 266 | } |
Garrick Evans | 86c7d9c | 2020-03-17 09:25:48 +0900 | [diff] [blame] | 267 | |
Hugo Benichi | 8e44842 | 2020-07-07 10:49:00 +0900 | [diff] [blame] | 268 | for (const auto& kv : available_configs_) |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 269 | for (const auto& c : kv.second) |
Hugo Benichi | 8e44842 | 2020-07-07 10:49:00 +0900 | [diff] [blame] | 270 | all_configs_.emplace_back(c.get()); |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame^] | 271 | // Append arc0 config so that the necessary tap device gets created. |
| 272 | all_configs_.insert(all_configs_.begin(), &arc_device_->config()); |
Garrick Evans | 86c7d9c | 2020-03-17 09:25:48 +0900 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | std::unique_ptr<Device::Config> ArcService::AcquireConfig( |
| 276 | const std::string& ifname) { |
| 277 | auto itype = InterfaceTypeFor(ifname); |
| 278 | if (itype == InterfaceType::UNKNOWN) { |
| 279 | LOG(ERROR) << "Unsupported interface: " << ifname; |
| 280 | return nullptr; |
| 281 | } |
| 282 | |
Hugo Benichi | 8e44842 | 2020-07-07 10:49:00 +0900 | [diff] [blame] | 283 | auto& configs = available_configs_[itype]; |
Garrick Evans | 86c7d9c | 2020-03-17 09:25:48 +0900 | [diff] [blame] | 284 | if (configs.empty()) { |
| 285 | LOG(ERROR) << "No more addresses available. Cannot make device for " |
| 286 | << ifname; |
| 287 | return nullptr; |
| 288 | } |
| 289 | std::unique_ptr<Device::Config> config; |
| 290 | config = std::move(configs.front()); |
| 291 | configs.pop_front(); |
| 292 | return config; |
| 293 | } |
| 294 | |
| 295 | void ArcService::ReleaseConfig(const std::string& ifname, |
| 296 | std::unique_ptr<Device::Config> config) { |
| 297 | auto itype = InterfaceTypeFor(ifname); |
| 298 | if (itype == InterfaceType::UNKNOWN) { |
| 299 | LOG(ERROR) << "Unsupported interface: " << ifname; |
| 300 | return; |
| 301 | } |
| 302 | |
Hugo Benichi | 8e44842 | 2020-07-07 10:49:00 +0900 | [diff] [blame] | 303 | available_configs_[itype].push_front(std::move(config)); |
Garrick Evans | 86c7d9c | 2020-03-17 09:25:48 +0900 | [diff] [blame] | 304 | } |
| 305 | |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 306 | bool ArcService::Start(uint32_t id) { |
Hugo Benichi | 4d4bb8f | 2020-07-07 12:16:07 +0900 | [diff] [blame] | 307 | if (IsStarted()) { |
| 308 | LOG(WARNING) << "Already running - did something crash?" |
| 309 | << " Stopping and restarting..."; |
| 310 | Stop(id_); |
Garrick Evans | a51d0a1 | 2019-11-28 13:51:23 +0900 | [diff] [blame] | 311 | } |
| 312 | |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame^] | 313 | std::string arc_device_ifname; |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 314 | if (guest_ == GuestMessage::ARC_VM) { |
Hugo Benichi | 8e44842 | 2020-07-07 10:49:00 +0900 | [diff] [blame] | 315 | // Allocate TAP devices for all configs. |
| 316 | for (auto* config : all_configs_) { |
| 317 | auto mac = config->mac_addr(); |
| 318 | auto tap = datapath_->AddTAP("" /* auto-generate name */, &mac, |
| 319 | nullptr /* no ipv4 subnet */, |
| 320 | vm_tools::kCrosVmUser); |
| 321 | if (tap.empty()) { |
| 322 | LOG(ERROR) << "Failed to create TAP device"; |
| 323 | continue; |
| 324 | } |
| 325 | |
| 326 | config->set_tap_ifname(tap); |
| 327 | } |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame^] | 328 | arc_device_ifname = arc_device_->config().tap_ifname(); |
Garrick Evans | 2961c7c | 2020-04-03 11:34:40 +0900 | [diff] [blame] | 329 | } else { |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame^] | 330 | OneTimeContainerSetup(*datapath_); |
Hugo Benichi | 33860d7 | 2020-07-09 16:34:01 +0900 | [diff] [blame] | 331 | if (!datapath_->NetnsAttachName(kArcNetnsName, id)) { |
| 332 | LOG(ERROR) << "Failed to attach name " << kArcNetnsName << " to pid " |
| 333 | << id; |
| 334 | return false; |
| 335 | } |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 336 | arc_device_ifname = ArcVethHostName(arc_device_->guest_ifname()); |
Hugo Benichi | 4d4bb8f | 2020-07-07 12:16:07 +0900 | [diff] [blame] | 337 | if (!datapath_->ConnectVethPair(id, kArcNetnsName, arc_device_ifname, |
| 338 | arc_device_->guest_ifname(), |
| 339 | arc_device_->config().mac_addr(), |
| 340 | arc_device_->config().guest_ipv4_addr(), 30, |
| 341 | arc_device_->options().fwd_multicast)) { |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 342 | LOG(ERROR) << "Cannot create virtual link for device " |
| 343 | << arc_device_->phys_ifname(); |
| 344 | return false; |
| 345 | } |
| 346 | } |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame^] | 347 | id_ = id; |
| 348 | |
| 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 | } |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 359 | |
| 360 | if (!datapath_->AddToBridge(kArcBridge, arc_device_ifname)) { |
| 361 | LOG(ERROR) << "Failed to bridge arc device " << arc_device_ifname << " to " |
| 362 | << kArcBridge; |
| 363 | return false; |
| 364 | } |
Hugo Benichi | 33860d7 | 2020-07-09 16:34:01 +0900 | [diff] [blame] | 365 | LOG(INFO) << "Started ARC management device " << *arc_device_.get(); |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 366 | |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 367 | // Start already known Shill <-> ARC mapped devices. |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame^] | 368 | for (const auto& ifname : shill_devices_) |
| 369 | AddDevice(ifname); |
Garrick Evans | cb791e7 | 2019-11-11 15:44:34 +0900 | [diff] [blame] | 370 | |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 371 | return true; |
Garrick Evans | cb791e7 | 2019-11-11 15:44:34 +0900 | [diff] [blame] | 372 | } |
| 373 | |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 374 | void ArcService::Stop(uint32_t id) { |
Hugo Benichi | 4d4bb8f | 2020-07-07 12:16:07 +0900 | [diff] [blame] | 375 | if (!IsStarted()) { |
| 376 | LOG(ERROR) << "ArcService was not running"; |
| 377 | return; |
| 378 | } |
| 379 | |
| 380 | // After the ARC container has stopped, the pid is not known anymore. |
| 381 | if (guest_ == GuestMessage::ARC_VM && id_ != id) { |
| 382 | LOG(ERROR) << "Mismatched ARCVM CIDs " << id_ << " != " << id; |
| 383 | return; |
| 384 | } |
| 385 | |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 386 | // Stop Shill <-> ARC mapped devices. |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame^] | 387 | for (const auto& ifname : shill_devices_) |
| 388 | RemoveDevice(ifname); |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 389 | |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 390 | // Per crbug/1008686 this device cannot be deleted and then re-added. |
| 391 | // So instead of removing the bridge, bring it down and mark it. This will |
| 392 | // allow us to detect if the device is re-added in case of a crash restart |
| 393 | // and do the right thing. |
| 394 | if (!datapath_->MaskInterfaceFlags(kArcBridge, IFF_DEBUG, IFF_UP)) |
| 395 | LOG(ERROR) << "Failed to bring down arc bridge " |
| 396 | << "- it may not restart correctly"; |
| 397 | |
Hugo Benichi | 33860d7 | 2020-07-09 16:34:01 +0900 | [diff] [blame] | 398 | if (guest_ == GuestMessage::ARC) { |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 399 | datapath_->RemoveInterface(ArcVethHostName(arc_device_->phys_ifname())); |
Hugo Benichi | 33860d7 | 2020-07-09 16:34:01 +0900 | [diff] [blame] | 400 | if (!datapath_->NetnsDeleteName(kArcNetnsName)) |
| 401 | LOG(WARNING) << "Failed to delete netns name " << kArcNetnsName; |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame^] | 402 | } |
| 403 | |
| 404 | // Destroy allocated TAP devices if any, including the ARC management device. |
| 405 | for (auto* config : all_configs_) { |
| 406 | if (config->tap_ifname().empty()) |
| 407 | continue; |
| 408 | datapath_->RemoveInterface(config->tap_ifname()); |
| 409 | config->set_tap_ifname(""); |
Hugo Benichi | 33860d7 | 2020-07-09 16:34:01 +0900 | [diff] [blame] | 410 | } |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 411 | |
Hugo Benichi | 33860d7 | 2020-07-09 16:34:01 +0900 | [diff] [blame] | 412 | LOG(INFO) << "Stopped ARC management device " << *arc_device_.get(); |
Hugo Benichi | 4d4bb8f | 2020-07-07 12:16:07 +0900 | [diff] [blame] | 413 | id_ = kInvalidId; |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 414 | } |
| 415 | |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 416 | void ArcService::OnDevicesChanged(const std::set<std::string>& added, |
| 417 | const std::set<std::string>& removed) { |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame^] | 418 | for (const std::string& ifname : removed) { |
| 419 | shill_devices_.erase(ifname); |
| 420 | RemoveDevice(ifname); |
| 421 | } |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 422 | |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame^] | 423 | for (const std::string& ifname : added) { |
| 424 | shill_devices_.insert(ifname); |
| 425 | AddDevice(ifname); |
| 426 | } |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | void ArcService::AddDevice(const std::string& ifname) { |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame^] | 430 | if (!IsStarted()) |
| 431 | return; |
| 432 | |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 433 | if (ifname.empty()) |
| 434 | return; |
| 435 | |
| 436 | if (devices_.find(ifname) != devices_.end()) { |
| 437 | LOG(DFATAL) << "Attemping to add already tracked device: " << ifname; |
| 438 | return; |
| 439 | } |
| 440 | |
Garrick Evans | 86c7d9c | 2020-03-17 09:25:48 +0900 | [diff] [blame] | 441 | auto itype = InterfaceTypeFor(ifname); |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 442 | Device::Options opts{ |
| 443 | .fwd_multicast = IsMulticastInterface(ifname), |
| 444 | // TODO(crbug/726815) Also enable |ipv6_enabled| for cellular networks |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame^] | 445 | // once IPv6 is enabled on cellular networks in Shill. |
Garrick Evans | 86c7d9c | 2020-03-17 09:25:48 +0900 | [diff] [blame] | 446 | .ipv6_enabled = |
| 447 | (itype == InterfaceType::ETHERNET || itype == InterfaceType::WIFI), |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 448 | }; |
Garrick Evans | 86c7d9c | 2020-03-17 09:25:48 +0900 | [diff] [blame] | 449 | |
| 450 | auto config = AcquireConfig(ifname); |
| 451 | if (!config) { |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame^] | 452 | LOG(ERROR) << "Cannot acquire a Config for " << ifname; |
Garrick Evans | 86c7d9c | 2020-03-17 09:25:48 +0900 | [diff] [blame] | 453 | return; |
| 454 | } |
| 455 | |
Garrick Evans | 8a06756 | 2020-05-11 12:47:30 +0900 | [diff] [blame] | 456 | auto device = std::make_unique<Device>(ifname, ArcBridgeName(ifname), ifname, |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 457 | std::move(config), opts); |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 458 | LOG(INFO) << "Starting device " << *device; |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 459 | |
| 460 | // Create the bridge. |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame^] | 461 | if (!datapath_->AddBridge(device->host_ifname(), |
| 462 | device->config().host_ipv4_addr(), 30)) { |
| 463 | LOG(ERROR) << "Failed to setup bridge " << device->host_ifname(); |
Garrick Evans | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 464 | return; |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 465 | } |
| 466 | |
Garrick Evans | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 467 | // Set up iptables. |
| 468 | if (!datapath_->AddInboundIPv4DNAT( |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame^] | 469 | device->phys_ifname(), |
| 470 | IPv4AddressToString(device->config().guest_ipv4_addr()))) |
Garrick Evans | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 471 | LOG(ERROR) << "Failed to configure ingress traffic rules for " |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 472 | << device->phys_ifname(); |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 473 | |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 474 | if (!datapath_->AddOutboundIPv4(device->host_ifname())) |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 475 | LOG(ERROR) << "Failed to configure egress traffic rules for " |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 476 | << device->phys_ifname(); |
Garrick Evans | 2c26310 | 2019-07-26 16:07:18 +0900 | [diff] [blame] | 477 | |
Hugo Benichi | cf90402 | 2020-07-07 09:21:58 +0900 | [diff] [blame] | 478 | std::string virtual_device_ifname; |
| 479 | if (guest_ == GuestMessage::ARC_VM) { |
| 480 | virtual_device_ifname = device->config().tap_ifname(); |
| 481 | if (virtual_device_ifname.empty()) { |
| 482 | LOG(ERROR) << "No TAP device for " << *device; |
| 483 | return; |
| 484 | } |
| 485 | } else { |
| 486 | virtual_device_ifname = ArcVethHostName(device->guest_ifname()); |
| 487 | if (!datapath_->ConnectVethPair( |
Hugo Benichi | 4d4bb8f | 2020-07-07 12:16:07 +0900 | [diff] [blame] | 488 | id_, kArcNetnsName, virtual_device_ifname, device->guest_ifname(), |
Hugo Benichi | cf90402 | 2020-07-07 09:21:58 +0900 | [diff] [blame] | 489 | device->config().mac_addr(), device->config().guest_ipv4_addr(), 30, |
| 490 | device->options().fwd_multicast)) { |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame^] | 491 | LOG(ERROR) << "Cannot create veth link for device " << *device; |
Hugo Benichi | cf90402 | 2020-07-07 09:21:58 +0900 | [diff] [blame] | 492 | return; |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | if (!datapath_->AddToBridge(device->host_ifname(), virtual_device_ifname)) { |
| 497 | if (guest_ == GuestMessage::ARC) { |
| 498 | datapath_->RemoveInterface(virtual_device_ifname); |
| 499 | } |
| 500 | LOG(ERROR) << "Failed to bridge interface " << virtual_device_ifname; |
Hugo Benichi | 4833fda | 2020-07-06 14:56:56 +0900 | [diff] [blame] | 501 | return; |
| 502 | } |
| 503 | |
| 504 | forwarder_->StartForwarding(device->phys_ifname(), device->host_ifname(), |
| 505 | device->options().ipv6_enabled, |
| 506 | device->options().fwd_multicast); |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame^] | 507 | devices_.emplace(ifname, std::move(device)); |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | void ArcService::RemoveDevice(const std::string& ifname) { |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame^] | 511 | if (!IsStarted()) |
| 512 | return; |
| 513 | |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 514 | const auto it = devices_.find(ifname); |
| 515 | if (it == devices_.end()) { |
| 516 | LOG(WARNING) << "Unknown device: " << ifname; |
Garrick Evans | cb791e7 | 2019-11-11 15:44:34 +0900 | [diff] [blame] | 517 | return; |
| 518 | } |
| 519 | |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame^] | 520 | const auto* device = it->second.get(); |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 521 | LOG(INFO) << "Removing device " << *device; |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 522 | |
Hugo Benichi | 4833fda | 2020-07-06 14:56:56 +0900 | [diff] [blame] | 523 | forwarder_->StopForwarding(device->phys_ifname(), device->host_ifname(), |
| 524 | device->options().ipv6_enabled, |
| 525 | device->options().fwd_multicast); |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame^] | 526 | |
| 527 | // ARCVM TAP devices are removed in VmImpl::Stop() when the service stops |
Hugo Benichi | 4833fda | 2020-07-06 14:56:56 +0900 | [diff] [blame] | 528 | if (guest_ == GuestMessage::ARC) |
| 529 | datapath_->RemoveInterface(ArcVethHostName(device->phys_ifname())); |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 530 | |
| 531 | const auto& config = device->config(); |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 532 | datapath_->RemoveOutboundIPv4(device->host_ifname()); |
Garrick Evans | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 533 | datapath_->RemoveInboundIPv4DNAT( |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 534 | device->phys_ifname(), IPv4AddressToString(config.guest_ipv4_addr())); |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 535 | datapath_->RemoveBridge(device->host_ifname()); |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame^] | 536 | |
| 537 | ReleaseConfig(ifname, it->second->release_config()); |
| 538 | devices_.erase(it); |
Garrick Evans | cb791e7 | 2019-11-11 15:44:34 +0900 | [diff] [blame] | 539 | } |
| 540 | |
Garrick Evans | 38b25a4 | 2020-04-06 15:17:42 +0900 | [diff] [blame] | 541 | std::vector<const Device::Config*> ArcService::GetDeviceConfigs() const { |
Hugo Benichi | 8e44842 | 2020-07-07 10:49:00 +0900 | [diff] [blame] | 542 | std::vector<const Device::Config*> configs; |
| 543 | for (auto* c : all_configs_) |
| 544 | configs.emplace_back(c); |
Garrick Evans | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 545 | |
Hugo Benichi | 8e44842 | 2020-07-07 10:49:00 +0900 | [diff] [blame] | 546 | return configs; |
Garrick Evans | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 547 | } |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 548 | } // namespace patchpanel |