blob: b3e8be3a44adf32a1579c393ef30aedadd197c09 [file] [log] [blame]
Garrick Evans5d55f5e2019-07-17 15:28:10 +09001// 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 Evans3388a032020-03-24 11:25:55 +09005#include "patchpanel/arc_service.h"
Garrick Evans5d55f5e2019-07-17 15:28:10 +09006
Garrick Evans54861622019-07-19 09:05:09 +09007#include <linux/rtnetlink.h>
8#include <net/if.h>
Garrick Evans6e4eb3b2020-03-09 07:18:31 +09009#include <sys/ioctl.h>
Garrick Evans71e4a862020-05-18 12:22:23 +090010#include <sys/utsname.h>
Garrick Evans54861622019-07-19 09:05:09 +090011
Garrick Evans5d55f5e2019-07-17 15:28:10 +090012#include <utility>
Jason Jeremy Imanf4156cb2019-11-14 15:36:22 +090013#include <vector>
Garrick Evans5d55f5e2019-07-17 15:28:10 +090014
Garrick Evans54861622019-07-19 09:05:09 +090015#include <base/bind.h>
Garrick Evans5d55f5e2019-07-17 15:28:10 +090016#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 Evans54861622019-07-19 09:05:09 +090021#include <base/strings/stringprintf.h>
Qijiang Fan2d7aeb42020-05-19 02:06:39 +090022#include <base/system/sys_info.h>
Garrick Evans1f5a3612019-11-08 12:59:03 +090023#include <brillo/key_value_store.h>
Garrick Evansb4eb3892019-11-13 12:07:07 +090024#include <chromeos/constants/vm_tools.h>
Garrick Evans54861622019-07-19 09:05:09 +090025
Garrick Evans3388a032020-03-24 11:25:55 +090026#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 Evans5d55f5e2019-07-17 15:28:10 +090032
Garrick Evans3388a032020-03-24 11:25:55 +090033namespace patchpanel {
Garrick Evans5d55f5e2019-07-17 15:28:10 +090034namespace {
Garrick Evans015b0d62020-02-07 09:06:38 +090035constexpr pid_t kInvalidPID = 0;
Garrick Evans015b0d62020-02-07 09:06:38 +090036constexpr uint32_t kInvalidCID = 0;
Hugo Benichi33860d72020-07-09 16:34:01 +090037constexpr char kArcNetnsName[] = "arc_netns";
Garrick Evanse94b6de2020-02-20 09:19:13 +090038constexpr char kArcIfname[] = "arc0";
39constexpr char kArcBridge[] = "arcbr0";
Garrick Evans6e4eb3b2020-03-09 07:18:31 +090040constexpr std::array<const char*, 2> kEthernetInterfacePrefixes{{"eth", "usb"}};
41constexpr std::array<const char*, 2> kWifiInterfacePrefixes{{"wlan", "mlan"}};
Garrick Evans86c7d9c2020-03-17 09:25:48 +090042constexpr std::array<const char*, 2> kCellInterfacePrefixes{{"wwan", "rmnet"}};
Garrick Evans54861622019-07-19 09:05:09 +090043
Garrick Evans71e4a862020-05-18 12:22:23 +090044bool KernelVersion(int* major, int* minor) {
45 struct utsname u;
46 if (uname(&u) != 0) {
47 PLOG(ERROR) << "uname failed";
48 *major = *minor = 0;
49 return false;
50 }
51 int unused;
52 if (sscanf(u.release, "%d.%d.%d", major, minor, &unused) != 3) {
53 LOG(ERROR) << "unexpected release string: " << u.release;
54 *major = *minor = 0;
55 return false;
56 }
57 return true;
58}
59
Garrick Evans6d227b92019-12-03 16:11:29 +090060void OneTimeSetup(const Datapath& datapath) {
Garrick Evansa34b5862019-11-20 09:34:01 +090061 static bool done = false;
62 if (done)
63 return;
64
Garrick Evans6d227b92019-12-03 16:11:29 +090065 auto& runner = datapath.runner();
66
67 // Load networking modules needed by Android that are not compiled in the
68 // kernel. Android does not allow auto-loading of kernel modules.
Garrick Evansc53b9702020-05-13 13:20:09 +090069 // Expected for all kernels.
Garrick Evans8e8e3472020-01-23 14:03:50 +090070 if (runner.modprobe_all({
Garrick Evansa34b5862019-11-20 09:34:01 +090071 // The netfilter modules needed by netd for iptables commands.
72 "ip6table_filter",
73 "ip6t_ipv6header",
74 "ip6t_REJECT",
Garrick Evansa34b5862019-11-20 09:34:01 +090075 // The ipsec modules for AH and ESP encryption for ipv6.
76 "ah6",
77 "esp6",
78 }) != 0) {
79 LOG(ERROR) << "One or more required kernel modules failed to load."
80 << " Some Android functionality may be broken.";
81 }
Garrick Evansc53b9702020-05-13 13:20:09 +090082 // The xfrm modules needed for Android's ipsec APIs on kernels < 5.4.
Garrick Evans71e4a862020-05-18 12:22:23 +090083 int major, minor;
84 if (KernelVersion(&major, &minor) &&
85 (major < 5 || (major == 5 && minor < 4)) &&
86 runner.modprobe_all({
87 "xfrm4_mode_transport",
88 "xfrm4_mode_tunnel",
89 "xfrm6_mode_transport",
90 "xfrm6_mode_tunnel",
91 }) != 0) {
Garrick Evansc53b9702020-05-13 13:20:09 +090092 LOG(ERROR) << "One or more required kernel modules failed to load."
93 << " Some Android functionality may be broken.";
94 }
95
Garrick Evansa34b5862019-11-20 09:34:01 +090096 // Optional modules.
Garrick Evans8e8e3472020-01-23 14:03:50 +090097 if (runner.modprobe_all({
Garrick Evansa34b5862019-11-20 09:34:01 +090098 // This module is not available in kernels < 3.18
99 "nf_reject_ipv6",
100 // These modules are needed for supporting Chrome traffic on Android
101 // VPN which uses Android's NAT feature. Android NAT sets up
102 // iptables
103 // rules that use these conntrack modules for FTP/TFTP.
104 "nf_nat_ftp",
105 "nf_nat_tftp",
Hugo Benichia0cde9e2019-12-16 11:57:20 +0900106 // The tun module is needed by the Android 464xlat clatd process.
107 "tun",
Garrick Evansa34b5862019-11-20 09:34:01 +0900108 }) != 0) {
109 LOG(WARNING) << "One or more optional kernel modules failed to load.";
110 }
111
Garrick Evans6d227b92019-12-03 16:11:29 +0900112 // This is only needed for CTS (b/27932574).
Garrick Evans8e8e3472020-01-23 14:03:50 +0900113 if (runner.chown("655360", "655360", "/sys/class/xt_idletimer") != 0) {
Garrick Evans6d227b92019-12-03 16:11:29 +0900114 LOG(ERROR) << "Failed to change ownership of xt_idletimer.";
115 }
116
Garrick Evansa34b5862019-11-20 09:34:01 +0900117 done = true;
118}
119
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900120ArcService::InterfaceType InterfaceTypeFor(const std::string& ifname) {
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900121 for (const auto& prefix : kEthernetInterfacePrefixes) {
122 if (base::StartsWith(ifname, prefix,
123 base::CompareCase::INSENSITIVE_ASCII)) {
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900124 return ArcService::InterfaceType::ETHERNET;
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900125 }
126 }
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900127 for (const auto& prefix : kWifiInterfacePrefixes) {
128 if (base::StartsWith(ifname, prefix,
129 base::CompareCase::INSENSITIVE_ASCII)) {
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900130 return ArcService::InterfaceType::WIFI;
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900131 }
132 }
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900133 for (const auto& prefix : kCellInterfacePrefixes) {
134 if (base::StartsWith(ifname, prefix,
135 base::CompareCase::INSENSITIVE_ASCII)) {
136 return ArcService::InterfaceType::CELL;
137 }
138 }
139 return ArcService::InterfaceType::UNKNOWN;
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900140}
141
142bool IsMulticastInterface(const std::string& ifname) {
143 if (ifname.empty()) {
144 return false;
145 }
146
147 int fd = socket(AF_INET, SOCK_DGRAM, 0);
148 if (fd < 0) {
149 // If IPv4 fails, try to open a socket using IPv6.
150 fd = socket(AF_INET6, SOCK_DGRAM, 0);
151 if (fd < 0) {
152 LOG(ERROR) << "Unable to create socket";
153 return false;
154 }
155 }
156
157 struct ifreq ifr;
158 memset(&ifr, 0, sizeof(ifr));
159 strncpy(ifr.ifr_name, ifname.c_str(), IFNAMSIZ);
160 if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0) {
161 PLOG(ERROR) << "SIOCGIFFLAGS failed for " << ifname;
162 close(fd);
163 return false;
164 }
165
166 close(fd);
167 return (ifr.ifr_flags & IFF_MULTICAST);
168}
169
Garrick Evanse94b6de2020-02-20 09:19:13 +0900170// Returns the configuration for the ARC management interface used for VPN
171// forwarding, ADB-over-TCP and single-networked ARCVM.
Hugo Benichiad1bdd92020-06-12 13:48:37 +0900172std::unique_ptr<Device::Config> MakeArcConfig(
173 AddressManager* addr_mgr,
174 AddressManager::Guest guest,
175 GuestMessage::GuestType guest_type) {
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900176 auto ipv4_subnet = addr_mgr->AllocateIPv4Subnet(guest);
Garrick Evanse94b6de2020-02-20 09:19:13 +0900177 if (!ipv4_subnet) {
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900178 LOG(ERROR) << "Subnet already in use or unavailable";
Garrick Evanse94b6de2020-02-20 09:19:13 +0900179 return nullptr;
180 }
181 auto host_ipv4_addr = ipv4_subnet->AllocateAtOffset(0);
182 if (!host_ipv4_addr) {
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900183 LOG(ERROR) << "Bridge address already in use or unavailable";
Garrick Evanse94b6de2020-02-20 09:19:13 +0900184 return nullptr;
185 }
186 auto guest_ipv4_addr = ipv4_subnet->AllocateAtOffset(1);
187 if (!guest_ipv4_addr) {
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900188 LOG(ERROR) << "ARC address already in use or unavailable";
Garrick Evanse94b6de2020-02-20 09:19:13 +0900189 return nullptr;
190 }
191
Hugo Benichiad1bdd92020-06-12 13:48:37 +0900192 int subnet_index = (guest_type == GuestMessage::ARC_VM) ? 1 : kAnySubnetIndex;
193
Garrick Evanse94b6de2020-02-20 09:19:13 +0900194 return std::make_unique<Device::Config>(
Hugo Benichiad1bdd92020-06-12 13:48:37 +0900195 addr_mgr->GenerateMacAddress(subnet_index), std::move(ipv4_subnet),
196 std::move(host_ipv4_addr), std::move(guest_ipv4_addr));
Garrick Evanse94b6de2020-02-20 09:19:13 +0900197}
198
Garrick Evans5d55f5e2019-07-17 15:28:10 +0900199} // namespace
200
Garrick Evans69b85872020-02-04 11:40:26 +0900201ArcService::ArcService(ShillClient* shill_client,
Garrick Evans2e5c9ab2020-03-05 14:33:58 +0900202 Datapath* datapath,
203 AddressManager* addr_mgr,
Garrick Evansf5862122020-03-16 09:13:45 +0900204 TrafficForwarder* forwarder,
Hugo Benichiad1bdd92020-06-12 13:48:37 +0900205 GuestMessage::GuestType guest)
Garrick Evans2e5c9ab2020-03-05 14:33:58 +0900206 : shill_client_(shill_client),
Garrick Evans2e5c9ab2020-03-05 14:33:58 +0900207 datapath_(datapath),
208 addr_mgr_(addr_mgr),
Garrick Evansf5862122020-03-16 09:13:45 +0900209 forwarder_(forwarder),
Hugo Benichiad1bdd92020-06-12 13:48:37 +0900210 guest_(guest) {
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900211 AllocateAddressConfigs();
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900212 shill_client_->RegisterDevicesChangedHandler(
213 base::Bind(&ArcService::OnDevicesChanged, weak_factory_.GetWeakPtr()));
Jie Jiang84c76a12020-04-17 16:45:20 +0900214 shill_client_->ScanDevices();
Garrick Evansf29f5a32019-12-06 11:34:25 +0900215}
216
217ArcService::~ArcService() {
Garrick Evans664a82f2019-12-17 12:18:05 +0900218 if (impl_) {
Garrick Evans664a82f2019-12-17 12:18:05 +0900219 Stop(impl_->id());
Garrick Evans664a82f2019-12-17 12:18:05 +0900220 }
Garrick Evans54861622019-07-19 09:05:09 +0900221}
Garrick Evans5d55f5e2019-07-17 15:28:10 +0900222
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900223void ArcService::AllocateAddressConfigs() {
224 configs_.clear();
225 // The first usable subnet is the "other" ARC device subnet.
Garrick Evansc7071122020-04-17 12:31:57 +0900226 // As a temporary workaround, for ARCVM, allocate fixed MAC addresses.
227 uint8_t mac_addr_index = 2;
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900228 // Allocate 2 subnets each for Ethernet and WiFi and 1 for LTE WAN interfaces.
229 for (const auto itype :
230 {InterfaceType::ETHERNET, InterfaceType::ETHERNET, InterfaceType::WIFI,
231 InterfaceType::WIFI, InterfaceType::CELL}) {
Hugo Benichiad1bdd92020-06-12 13:48:37 +0900232 auto ipv4_subnet =
233 addr_mgr_->AllocateIPv4Subnet(AddressManager::Guest::ARC_NET);
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900234 if (!ipv4_subnet) {
235 LOG(ERROR) << "Subnet already in use or unavailable";
236 continue;
237 }
238 // For here out, use the same slices.
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900239 auto host_ipv4_addr = ipv4_subnet->AllocateAtOffset(0);
240 if (!host_ipv4_addr) {
241 LOG(ERROR) << "Bridge address already in use or unavailable";
242 continue;
243 }
244 auto guest_ipv4_addr = ipv4_subnet->AllocateAtOffset(1);
245 if (!guest_ipv4_addr) {
246 LOG(ERROR) << "ARC address already in use or unavailable";
247 continue;
248 }
249
Hugo Benichiad1bdd92020-06-12 13:48:37 +0900250 MacAddress mac_addr = (guest_ == GuestMessage::ARC_VM)
Garrick Evansc7071122020-04-17 12:31:57 +0900251 ? addr_mgr_->GenerateMacAddress(mac_addr_index++)
252 : addr_mgr_->GenerateMacAddress();
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900253 configs_[itype].emplace_back(std::make_unique<Device::Config>(
Garrick Evansc7071122020-04-17 12:31:57 +0900254 mac_addr, std::move(ipv4_subnet), std::move(host_ipv4_addr),
255 std::move(guest_ipv4_addr)));
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900256 }
257}
258
Garrick Evans2961c7c2020-04-03 11:34:40 +0900259std::vector<Device::Config*> ArcService::ReallocateAddressConfigs() {
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900260 std::vector<std::string> existing_devices;
261 for (const auto& d : devices_) {
262 existing_devices.emplace_back(d.first);
263 }
264 for (const auto& d : existing_devices) {
265 RemoveDevice(d);
266 }
267 AllocateAddressConfigs();
Garrick Evans2961c7c2020-04-03 11:34:40 +0900268 std::vector<Device::Config*> configs;
Hugo Benichiad1bdd92020-06-12 13:48:37 +0900269 for (const auto& kv : configs_) {
270 for (const auto& c : kv.second)
271 configs.push_back(c.get());
Garrick Evans2961c7c2020-04-03 11:34:40 +0900272 }
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900273 for (const auto& d : existing_devices) {
274 AddDevice(d);
275 }
Garrick Evans2961c7c2020-04-03 11:34:40 +0900276 return configs;
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900277}
278
279std::unique_ptr<Device::Config> ArcService::AcquireConfig(
280 const std::string& ifname) {
281 auto itype = InterfaceTypeFor(ifname);
282 if (itype == InterfaceType::UNKNOWN) {
283 LOG(ERROR) << "Unsupported interface: " << ifname;
284 return nullptr;
285 }
286
287 auto& configs = configs_[itype];
288 if (configs.empty()) {
289 LOG(ERROR) << "No more addresses available. Cannot make device for "
290 << ifname;
291 return nullptr;
292 }
293 std::unique_ptr<Device::Config> config;
294 config = std::move(configs.front());
295 configs.pop_front();
296 return config;
297}
298
299void ArcService::ReleaseConfig(const std::string& ifname,
300 std::unique_ptr<Device::Config> config) {
301 auto itype = InterfaceTypeFor(ifname);
302 if (itype == InterfaceType::UNKNOWN) {
303 LOG(ERROR) << "Unsupported interface: " << ifname;
304 return;
305 }
306
307 configs_[itype].push_front(std::move(config));
308}
309
Garrick Evans015b0d62020-02-07 09:06:38 +0900310bool ArcService::Start(uint32_t id) {
Garrick Evansf29f5a32019-12-06 11:34:25 +0900311 if (impl_) {
Garrick Evans015b0d62020-02-07 09:06:38 +0900312 uint32_t prev_id;
Garrick Evansf29f5a32019-12-06 11:34:25 +0900313 if (impl_->IsStarted(&prev_id)) {
314 LOG(WARNING) << "Already running - did something crash?"
315 << " Stopping and restarting...";
316 Stop(prev_id);
317 }
Garrick Evansa51d0a12019-11-28 13:51:23 +0900318 }
319
Garrick Evans2961c7c2020-04-03 11:34:40 +0900320 auto configs = ReallocateAddressConfigs();
Hugo Benichiad1bdd92020-06-12 13:48:37 +0900321 std::unique_ptr<Device::Config> arc_device_config =
322 MakeArcConfig(addr_mgr_, AddressManager::Guest::ARC, guest_);
323 if (guest_ == GuestMessage::ARC_VM) {
324 // Append arc0 config separately from ArcService::ReallocateAddressConfigs()
325 // so that VmImpl::Start() can create the necessary tap device.
326 configs.insert(configs.begin(), arc_device_config.get());
Hugo Benichi4833fda2020-07-06 14:56:56 +0900327 impl_ = std::make_unique<VmImpl>(datapath_, configs);
Garrick Evans2961c7c2020-04-03 11:34:40 +0900328 } else {
Hugo Benichi4833fda2020-07-06 14:56:56 +0900329 impl_ = std::make_unique<ContainerImpl>(datapath_);
Hugo Benichi33860d72020-07-09 16:34:01 +0900330 if (!datapath_->NetnsAttachName(kArcNetnsName, id)) {
331 LOG(ERROR) << "Failed to attach name " << kArcNetnsName << " to pid "
332 << id;
333 return false;
334 }
Garrick Evans2961c7c2020-04-03 11:34:40 +0900335 }
Garrick Evansf29f5a32019-12-06 11:34:25 +0900336 if (!impl_->Start(id)) {
337 impl_.reset();
Garrick Evans508a4bc2019-11-14 08:45:52 +0900338 return false;
Garrick Evansf29f5a32019-12-06 11:34:25 +0900339 }
Garrick Evanscb791e72019-11-11 15:44:34 +0900340
Hugo Benichiad1bdd92020-06-12 13:48:37 +0900341 // Create the bridge for the management device arc0.
342 // Per crbug/1008686 this device cannot be deleted and then re-added.
343 // So instead of removing the bridge when the service stops, bring down the
344 // device instead and re-up it on restart.
345 if (!datapath_->AddBridge(kArcBridge, arc_device_config->host_ipv4_addr(),
346 30) &&
347 !datapath_->MaskInterfaceFlags(kArcBridge, IFF_UP)) {
348 LOG(ERROR) << "Failed to bring up arc bridge " << kArcBridge;
349 return false;
350 }
351
352 Device::Options opts{
353 .fwd_multicast = false,
354 .ipv6_enabled = false,
355 };
356 arc_device_ = std::make_unique<Device>(kArcIfname, kArcBridge, kArcIfname,
357 std::move(arc_device_config), opts);
358
359 std::string arc_device_ifname;
360 if (guest_ == GuestMessage::ARC_VM) {
361 arc_device_ifname = arc_device_->config().tap_ifname();
362 // The tap device associated with arc_device is created by VmImpl::Start().
363 } else {
364 arc_device_ifname = ArcVethHostName(arc_device_->guest_ifname());
Hugo Benichi33860d72020-07-09 16:34:01 +0900365 if (!datapath_->ConnectVethPair(
366 impl_->id(), kArcNetnsName, arc_device_ifname,
367 arc_device_->guest_ifname(), arc_device_->config().mac_addr(),
368 arc_device_->config().guest_ipv4_addr(), 30,
369 arc_device_->options().fwd_multicast)) {
Hugo Benichiad1bdd92020-06-12 13:48:37 +0900370 LOG(ERROR) << "Cannot create virtual link for device "
371 << arc_device_->phys_ifname();
372 return false;
373 }
374 }
375
376 if (!datapath_->AddToBridge(kArcBridge, arc_device_ifname)) {
377 LOG(ERROR) << "Failed to bridge arc device " << arc_device_ifname << " to "
378 << kArcBridge;
379 return false;
380 }
Hugo Benichi33860d72020-07-09 16:34:01 +0900381 LOG(INFO) << "Started ARC management device " << *arc_device_.get();
Hugo Benichiad1bdd92020-06-12 13:48:37 +0900382
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900383 // Start already known Shill <-> ARC mapped devices.
384 for (const auto& d : devices_)
385 StartDevice(d.second.get());
Garrick Evanscb791e72019-11-11 15:44:34 +0900386
Garrick Evansf29f5a32019-12-06 11:34:25 +0900387 return true;
Garrick Evanscb791e72019-11-11 15:44:34 +0900388}
389
Garrick Evans015b0d62020-02-07 09:06:38 +0900390void ArcService::Stop(uint32_t id) {
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900391 // Stop Shill <-> ARC mapped devices.
392 for (const auto& d : devices_)
393 StopDevice(d.second.get());
Garrick Evansf29f5a32019-12-06 11:34:25 +0900394
Hugo Benichiad1bdd92020-06-12 13:48:37 +0900395 // Per crbug/1008686 this device cannot be deleted and then re-added.
396 // So instead of removing the bridge, bring it down and mark it. This will
397 // allow us to detect if the device is re-added in case of a crash restart
398 // and do the right thing.
399 if (!datapath_->MaskInterfaceFlags(kArcBridge, IFF_DEBUG, IFF_UP))
400 LOG(ERROR) << "Failed to bring down arc bridge "
401 << "- it may not restart correctly";
402
Hugo Benichi33860d72020-07-09 16:34:01 +0900403 if (guest_ == GuestMessage::ARC) {
Hugo Benichiad1bdd92020-06-12 13:48:37 +0900404 datapath_->RemoveInterface(ArcVethHostName(arc_device_->phys_ifname()));
Hugo Benichi33860d72020-07-09 16:34:01 +0900405 if (!datapath_->NetnsDeleteName(kArcNetnsName))
406 LOG(WARNING) << "Failed to delete netns name " << kArcNetnsName;
407 }
Hugo Benichiad1bdd92020-06-12 13:48:37 +0900408
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900409 if (impl_) {
410 impl_->Stop(id);
411 impl_.reset();
412 }
Hugo Benichiad1bdd92020-06-12 13:48:37 +0900413
Hugo Benichi33860d72020-07-09 16:34:01 +0900414 LOG(INFO) << "Stopped ARC management device " << *arc_device_.get();
Hugo Benichiad1bdd92020-06-12 13:48:37 +0900415 arc_device_.reset();
Garrick Evans5d55f5e2019-07-17 15:28:10 +0900416}
417
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900418void ArcService::OnDevicesChanged(const std::set<std::string>& added,
419 const std::set<std::string>& removed) {
420 for (const std::string& name : removed)
421 RemoveDevice(name);
422
423 for (const std::string& name : added)
424 AddDevice(name);
425}
426
427void ArcService::AddDevice(const std::string& ifname) {
428 if (ifname.empty())
429 return;
430
431 if (devices_.find(ifname) != devices_.end()) {
432 LOG(DFATAL) << "Attemping to add already tracked device: " << ifname;
433 return;
434 }
435
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900436 auto itype = InterfaceTypeFor(ifname);
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900437 Device::Options opts{
438 .fwd_multicast = IsMulticastInterface(ifname),
439 // TODO(crbug/726815) Also enable |ipv6_enabled| for cellular networks
440 // once IPv6 is enabled on cellular networks in shill.
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900441 .ipv6_enabled =
442 (itype == InterfaceType::ETHERNET || itype == InterfaceType::WIFI),
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900443 };
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900444
445 auto config = AcquireConfig(ifname);
446 if (!config) {
447 LOG(ERROR) << "Cannot add device for " << ifname;
448 return;
449 }
450
Garrick Evans8a067562020-05-11 12:47:30 +0900451 auto device = std::make_unique<Device>(ifname, ArcBridgeName(ifname), ifname,
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900452 std::move(config), opts);
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900453
454 StartDevice(device.get());
455 devices_.emplace(ifname, std::move(device));
456}
457
458void ArcService::StartDevice(Device* device) {
459 if (!impl_ || !impl_->IsStarted())
460 return;
461
Garrick Evans54861622019-07-19 09:05:09 +0900462 const auto& config = device->config();
463
Hugo Benichiad1bdd92020-06-12 13:48:37 +0900464 LOG(INFO) << "Starting device " << *device;
Garrick Evans54861622019-07-19 09:05:09 +0900465
466 // Create the bridge.
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900467 if (!datapath_->AddBridge(device->host_ifname(), config.host_ipv4_addr(),
Garrick Evans7a1a9ee2020-01-28 11:03:57 +0900468 30)) {
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900469 LOG(ERROR) << "Failed to setup arc bridge: " << device->host_ifname();
Garrick Evanse94b6de2020-02-20 09:19:13 +0900470 return;
Garrick Evans54861622019-07-19 09:05:09 +0900471 }
472
Garrick Evanse94b6de2020-02-20 09:19:13 +0900473 // Set up iptables.
474 if (!datapath_->AddInboundIPv4DNAT(
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900475 device->phys_ifname(), IPv4AddressToString(config.guest_ipv4_addr())))
Garrick Evanse94b6de2020-02-20 09:19:13 +0900476 LOG(ERROR) << "Failed to configure ingress traffic rules for "
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900477 << device->phys_ifname();
Garrick Evans54861622019-07-19 09:05:09 +0900478
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900479 if (!datapath_->AddOutboundIPv4(device->host_ifname()))
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900480 LOG(ERROR) << "Failed to configure egress traffic rules for "
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900481 << device->phys_ifname();
Garrick Evans2c263102019-07-26 16:07:18 +0900482
Hugo Benichi4833fda2020-07-06 14:56:56 +0900483 if (!impl_->OnStartDevice(device)) {
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900484 LOG(ERROR) << "Failed to start device " << device->phys_ifname();
Hugo Benichi4833fda2020-07-06 14:56:56 +0900485 return;
486 }
487
488 forwarder_->StartForwarding(device->phys_ifname(), device->host_ifname(),
489 device->options().ipv6_enabled,
490 device->options().fwd_multicast);
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900491}
492
493void ArcService::RemoveDevice(const std::string& ifname) {
494 const auto it = devices_.find(ifname);
495 if (it == devices_.end()) {
496 LOG(WARNING) << "Unknown device: " << ifname;
Garrick Evanscb791e72019-11-11 15:44:34 +0900497 return;
498 }
499
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900500 // If the container is down, this call does nothing.
501 StopDevice(it->second.get());
502
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900503 ReleaseConfig(ifname, it->second->release_config());
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900504 devices_.erase(it);
Garrick Evans54861622019-07-19 09:05:09 +0900505}
506
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900507void ArcService::StopDevice(Device* device) {
508 if (!impl_ || !impl_->IsStarted())
509 return;
510
Hugo Benichiad1bdd92020-06-12 13:48:37 +0900511 LOG(INFO) << "Removing device " << *device;
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900512
Hugo Benichi4833fda2020-07-06 14:56:56 +0900513 forwarder_->StopForwarding(device->phys_ifname(), device->host_ifname(),
514 device->options().ipv6_enabled,
515 device->options().fwd_multicast);
516 // TAP devices are removed in VmImpl::Stop().
517 if (guest_ == GuestMessage::ARC)
518 datapath_->RemoveInterface(ArcVethHostName(device->phys_ifname()));
Garrick Evans54861622019-07-19 09:05:09 +0900519
520 const auto& config = device->config();
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900521 datapath_->RemoveOutboundIPv4(device->host_ifname());
Garrick Evanse94b6de2020-02-20 09:19:13 +0900522 datapath_->RemoveInboundIPv4DNAT(
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900523 device->phys_ifname(), IPv4AddressToString(config.guest_ipv4_addr()));
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900524 datapath_->RemoveBridge(device->host_ifname());
Garrick Evanscb791e72019-11-11 15:44:34 +0900525}
526
Garrick Evans38b25a42020-04-06 15:17:42 +0900527std::vector<const Device::Config*> ArcService::GetDeviceConfigs() const {
528 if (impl_)
529 return impl_->GetDeviceConfigs();
Garrick Evanse94b6de2020-02-20 09:19:13 +0900530
Garrick Evans38b25a42020-04-06 15:17:42 +0900531 return {};
Garrick Evanse94b6de2020-02-20 09:19:13 +0900532}
533
Garrick Evansd90a3822019-11-12 17:53:08 +0900534// ARC++ specific functions.
535
Hugo Benichi4833fda2020-07-06 14:56:56 +0900536ArcService::ContainerImpl::ContainerImpl(Datapath* datapath)
537 : pid_(kInvalidPID), datapath_(datapath) {
Garrick Evans6d227b92019-12-03 16:11:29 +0900538 OneTimeSetup(*datapath_);
Garrick Evansa34b5862019-11-20 09:34:01 +0900539}
Garrick Evansd90a3822019-11-12 17:53:08 +0900540
Garrick Evans015b0d62020-02-07 09:06:38 +0900541uint32_t ArcService::ContainerImpl::id() const {
Garrick Evans664a82f2019-12-17 12:18:05 +0900542 return pid_;
543}
544
Garrick Evans015b0d62020-02-07 09:06:38 +0900545bool ArcService::ContainerImpl::Start(uint32_t pid) {
Garrick Evansa51d0a12019-11-28 13:51:23 +0900546 // This could happen if something crashes and the stop signal is not sent.
547 // It can probably be addressed by stopping and restarting the service.
548 if (pid_ != kInvalidPID)
549 return false;
550
Garrick Evans4dec0c42019-11-29 12:51:57 +0900551 if (pid == kInvalidPID) {
Garrick Evansd90a3822019-11-12 17:53:08 +0900552 LOG(ERROR) << "Cannot start service - invalid container PID";
553 return false;
554 }
Garrick Evans4dec0c42019-11-29 12:51:57 +0900555 pid_ = pid;
Garrick Evansd90a3822019-11-12 17:53:08 +0900556
Garrick Evansd90a3822019-11-12 17:53:08 +0900557 LOG(INFO) << "ARC++ network service started {pid: " << pid_ << "}";
558 return true;
559}
560
Garrick Evans015b0d62020-02-07 09:06:38 +0900561void ArcService::ContainerImpl::Stop(uint32_t /*pid*/) {
Garrick Evans4dec0c42019-11-29 12:51:57 +0900562 if (!IsStarted())
Taoyu Li1c96d272019-12-13 14:17:43 +0900563 return;
Garrick Evans4dec0c42019-11-29 12:51:57 +0900564
Garrick Evansd90a3822019-11-12 17:53:08 +0900565 LOG(INFO) << "ARC++ network service stopped {pid: " << pid_ << "}";
566 pid_ = kInvalidPID;
567}
568
Garrick Evans015b0d62020-02-07 09:06:38 +0900569bool ArcService::ContainerImpl::IsStarted(uint32_t* pid) const {
Garrick Evansa51d0a12019-11-28 13:51:23 +0900570 if (pid)
571 *pid = pid_;
572
Garrick Evansd90a3822019-11-12 17:53:08 +0900573 return pid_ != kInvalidPID;
574}
575
576bool ArcService::ContainerImpl::OnStartDevice(Device* device) {
Garrick Evans2470caa2020-03-04 14:15:41 +0900577 // Set up the virtual pair inside the container namespace.
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900578 const std::string veth_ifname = ArcVethHostName(device->guest_ifname());
Hugo Benichi76675592020-04-08 14:29:57 +0900579 const auto& config = device->config();
Hugo Benichi33860d72020-07-09 16:34:01 +0900580 if (!datapath_->ConnectVethPair(pid_, kArcNetnsName, veth_ifname,
581 device->guest_ifname(), config.mac_addr(),
582 config.guest_ipv4_addr(), 30,
583 device->options().fwd_multicast)) {
Hugo Benichi76675592020-04-08 14:29:57 +0900584 LOG(ERROR) << "Cannot create virtual link for device "
585 << device->phys_ifname();
Garrick Evansd90a3822019-11-12 17:53:08 +0900586 return false;
587 }
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900588 if (!datapath_->AddToBridge(device->host_ifname(), veth_ifname)) {
Garrick Evans2470caa2020-03-04 14:15:41 +0900589 datapath_->RemoveInterface(veth_ifname);
590 LOG(ERROR) << "Failed to bridge interface " << veth_ifname;
591 return false;
592 }
Garrick Evansd90a3822019-11-12 17:53:08 +0900593 return true;
594}
595
Garrick Evansb4eb3892019-11-13 12:07:07 +0900596// VM specific functions
597
Hugo Benichiad1bdd92020-06-12 13:48:37 +0900598ArcService::VmImpl::VmImpl(Datapath* datapath,
Garrick Evans2961c7c2020-04-03 11:34:40 +0900599 const std::vector<Device::Config*>& configs)
Hugo Benichi4833fda2020-07-06 14:56:56 +0900600 : cid_(kInvalidCID), datapath_(datapath), configs_(configs) {}
Garrick Evansb4eb3892019-11-13 12:07:07 +0900601
Garrick Evans015b0d62020-02-07 09:06:38 +0900602uint32_t ArcService::VmImpl::id() const {
Garrick Evans664a82f2019-12-17 12:18:05 +0900603 return cid_;
604}
605
Garrick Evans38b25a42020-04-06 15:17:42 +0900606std::vector<const Device::Config*> ArcService::VmImpl::GetDeviceConfigs()
607 const {
608 std::vector<const Device::Config*> configs;
609 for (const auto* c : configs_)
610 configs.emplace_back(c);
611
612 return configs;
613}
614
Garrick Evans015b0d62020-02-07 09:06:38 +0900615bool ArcService::VmImpl::Start(uint32_t cid) {
Garrick Evansa51d0a12019-11-28 13:51:23 +0900616 // This can happen if concierge crashes and doesn't send the vm down RPC.
617 // It can probably be addressed by stopping and restarting the service.
618 if (cid_ != kInvalidCID)
619 return false;
620
Garrick Evans015b0d62020-02-07 09:06:38 +0900621 if (cid == kInvalidCID) {
Garrick Evansb4eb3892019-11-13 12:07:07 +0900622 LOG(ERROR) << "Invalid VM cid " << cid;
623 return false;
624 }
Garrick Evansb4eb3892019-11-13 12:07:07 +0900625 cid_ = cid;
Garrick Evansb4eb3892019-11-13 12:07:07 +0900626
Garrick Evans2961c7c2020-04-03 11:34:40 +0900627 // Allocate TAP devices for all configs.
628 for (auto* config : configs_) {
Garrick Evansc7071122020-04-17 12:31:57 +0900629 auto mac = config->mac_addr();
630 auto tap =
631 datapath_->AddTAP("" /* auto-generate name */, &mac,
632 nullptr /* no ipv4 subnet */, vm_tools::kCrosVmUser);
Garrick Evans2961c7c2020-04-03 11:34:40 +0900633 if (tap.empty()) {
634 LOG(ERROR) << "Failed to create TAP device";
635 continue;
636 }
637
638 config->set_tap_ifname(tap);
Garrick Evanse94b6de2020-02-20 09:19:13 +0900639 }
640
Garrick Evanse94b6de2020-02-20 09:19:13 +0900641 LOG(INFO) << "ARCVM network service started {cid: " << cid_ << "}";
Garrick Evansb4eb3892019-11-13 12:07:07 +0900642 return true;
643}
644
Garrick Evans015b0d62020-02-07 09:06:38 +0900645void ArcService::VmImpl::Stop(uint32_t cid) {
Garrick Evans21173b12019-11-20 15:23:16 +0900646 if (cid_ != cid) {
647 LOG(ERROR) << "Mismatched ARCVM CIDs " << cid_ << " != " << cid;
648 return;
649 }
650
Hugo Benichiad1bdd92020-06-12 13:48:37 +0900651 for (auto* config : configs_)
652 if (!config->tap_ifname().empty())
653 datapath_->RemoveInterface(config->tap_ifname());
Garrick Evanse94b6de2020-02-20 09:19:13 +0900654
Garrick Evansb4eb3892019-11-13 12:07:07 +0900655 LOG(INFO) << "ARCVM network service stopped {cid: " << cid_ << "}";
656 cid_ = kInvalidCID;
657}
658
Garrick Evans015b0d62020-02-07 09:06:38 +0900659bool ArcService::VmImpl::IsStarted(uint32_t* cid) const {
Garrick Evansa51d0a12019-11-28 13:51:23 +0900660 if (cid)
661 *cid = cid_;
662
Garrick Evans015b0d62020-02-07 09:06:38 +0900663 return cid_ != kInvalidCID;
Garrick Evansb4eb3892019-11-13 12:07:07 +0900664}
665
666bool ArcService::VmImpl::OnStartDevice(Device* device) {
Hugo Benichiad1bdd92020-06-12 13:48:37 +0900667 const std::string& tap = device->config().tap_ifname();
Garrick Evansb4eb3892019-11-13 12:07:07 +0900668 if (tap.empty()) {
Garrick Evans2961c7c2020-04-03 11:34:40 +0900669 LOG(ERROR) << "No TAP device for: " << *device;
Garrick Evansb4eb3892019-11-13 12:07:07 +0900670 return false;
671 }
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900672 if (!datapath_->AddToBridge(device->host_ifname(), tap)) {
Garrick Evansb4eb3892019-11-13 12:07:07 +0900673 LOG(ERROR) << "Failed to bridge TAP device " << tap;
Garrick Evansb4eb3892019-11-13 12:07:07 +0900674 return false;
675 }
Garrick Evansb4eb3892019-11-13 12:07:07 +0900676 return true;
677}
Garrick Evans3388a032020-03-24 11:25:55 +0900678} // namespace patchpanel