blob: 4722813259fed3ccc310cbb20e91c164d7d4e0dc [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() {
Hugo Benichi8e448422020-07-07 10:49:00 +0900224 available_configs_.clear();
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900225 // 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();
Hugo Benichi8e448422020-07-07 10:49:00 +0900253 available_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
Hugo Benichi8e448422020-07-07 10:49:00 +0900259void 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();
Hugo Benichi8e448422020-07-07 10:49:00 +0900268 all_configs_.clear();
269 for (const auto& kv : available_configs_)
Hugo Benichiad1bdd92020-06-12 13:48:37 +0900270 for (const auto& c : kv.second)
Hugo Benichi8e448422020-07-07 10:49:00 +0900271 all_configs_.emplace_back(c.get());
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900272 for (const auto& d : existing_devices) {
273 AddDevice(d);
274 }
275}
276
277std::unique_ptr<Device::Config> ArcService::AcquireConfig(
278 const std::string& ifname) {
279 auto itype = InterfaceTypeFor(ifname);
280 if (itype == InterfaceType::UNKNOWN) {
281 LOG(ERROR) << "Unsupported interface: " << ifname;
282 return nullptr;
283 }
284
Hugo Benichi8e448422020-07-07 10:49:00 +0900285 auto& configs = available_configs_[itype];
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900286 if (configs.empty()) {
287 LOG(ERROR) << "No more addresses available. Cannot make device for "
288 << ifname;
289 return nullptr;
290 }
291 std::unique_ptr<Device::Config> config;
292 config = std::move(configs.front());
293 configs.pop_front();
294 return config;
295}
296
297void ArcService::ReleaseConfig(const std::string& ifname,
298 std::unique_ptr<Device::Config> config) {
299 auto itype = InterfaceTypeFor(ifname);
300 if (itype == InterfaceType::UNKNOWN) {
301 LOG(ERROR) << "Unsupported interface: " << ifname;
302 return;
303 }
304
Hugo Benichi8e448422020-07-07 10:49:00 +0900305 available_configs_[itype].push_front(std::move(config));
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900306}
307
Garrick Evans015b0d62020-02-07 09:06:38 +0900308bool ArcService::Start(uint32_t id) {
Garrick Evansf29f5a32019-12-06 11:34:25 +0900309 if (impl_) {
Garrick Evans015b0d62020-02-07 09:06:38 +0900310 uint32_t prev_id;
Garrick Evansf29f5a32019-12-06 11:34:25 +0900311 if (impl_->IsStarted(&prev_id)) {
312 LOG(WARNING) << "Already running - did something crash?"
313 << " Stopping and restarting...";
314 Stop(prev_id);
315 }
Garrick Evansa51d0a12019-11-28 13:51:23 +0900316 }
317
Hugo Benichi8e448422020-07-07 10:49:00 +0900318 ReallocateAddressConfigs();
Hugo Benichiad1bdd92020-06-12 13:48:37 +0900319 std::unique_ptr<Device::Config> arc_device_config =
320 MakeArcConfig(addr_mgr_, AddressManager::Guest::ARC, guest_);
321 if (guest_ == GuestMessage::ARC_VM) {
322 // Append arc0 config separately from ArcService::ReallocateAddressConfigs()
323 // so that VmImpl::Start() can create the necessary tap device.
Hugo Benichi8e448422020-07-07 10:49:00 +0900324 all_configs_.insert(all_configs_.begin(), arc_device_config.get());
325 // Allocate TAP devices for all configs.
326 for (auto* config : all_configs_) {
327 auto mac = config->mac_addr();
328 auto tap = datapath_->AddTAP("" /* auto-generate name */, &mac,
329 nullptr /* no ipv4 subnet */,
330 vm_tools::kCrosVmUser);
331 if (tap.empty()) {
332 LOG(ERROR) << "Failed to create TAP device";
333 continue;
334 }
335
336 config->set_tap_ifname(tap);
337 }
Hugo Benichicf904022020-07-07 09:21:58 +0900338 impl_ = std::make_unique<VmImpl>();
Garrick Evans2961c7c2020-04-03 11:34:40 +0900339 } else {
Hugo Benichicf904022020-07-07 09:21:58 +0900340 impl_ = std::make_unique<ContainerImpl>();
341 OneTimeSetup(*datapath_);
Hugo Benichi33860d72020-07-09 16:34:01 +0900342 if (!datapath_->NetnsAttachName(kArcNetnsName, id)) {
343 LOG(ERROR) << "Failed to attach name " << kArcNetnsName << " to pid "
344 << id;
345 return false;
346 }
Garrick Evans2961c7c2020-04-03 11:34:40 +0900347 }
Garrick Evansf29f5a32019-12-06 11:34:25 +0900348 if (!impl_->Start(id)) {
349 impl_.reset();
Garrick Evans508a4bc2019-11-14 08:45:52 +0900350 return false;
Garrick Evansf29f5a32019-12-06 11:34:25 +0900351 }
Garrick Evanscb791e72019-11-11 15:44:34 +0900352
Hugo Benichiad1bdd92020-06-12 13:48:37 +0900353 // Create the bridge for the management device arc0.
354 // Per crbug/1008686 this device cannot be deleted and then re-added.
355 // So instead of removing the bridge when the service stops, bring down the
356 // device instead and re-up it on restart.
357 if (!datapath_->AddBridge(kArcBridge, arc_device_config->host_ipv4_addr(),
358 30) &&
359 !datapath_->MaskInterfaceFlags(kArcBridge, IFF_UP)) {
360 LOG(ERROR) << "Failed to bring up arc bridge " << kArcBridge;
361 return false;
362 }
363
364 Device::Options opts{
365 .fwd_multicast = false,
366 .ipv6_enabled = false,
367 };
368 arc_device_ = std::make_unique<Device>(kArcIfname, kArcBridge, kArcIfname,
369 std::move(arc_device_config), opts);
370
371 std::string arc_device_ifname;
372 if (guest_ == GuestMessage::ARC_VM) {
373 arc_device_ifname = arc_device_->config().tap_ifname();
Hugo Benichiad1bdd92020-06-12 13:48:37 +0900374 } else {
375 arc_device_ifname = ArcVethHostName(arc_device_->guest_ifname());
Hugo Benichi33860d72020-07-09 16:34:01 +0900376 if (!datapath_->ConnectVethPair(
377 impl_->id(), kArcNetnsName, arc_device_ifname,
378 arc_device_->guest_ifname(), arc_device_->config().mac_addr(),
379 arc_device_->config().guest_ipv4_addr(), 30,
380 arc_device_->options().fwd_multicast)) {
Hugo Benichiad1bdd92020-06-12 13:48:37 +0900381 LOG(ERROR) << "Cannot create virtual link for device "
382 << arc_device_->phys_ifname();
383 return false;
384 }
385 }
386
387 if (!datapath_->AddToBridge(kArcBridge, arc_device_ifname)) {
388 LOG(ERROR) << "Failed to bridge arc device " << arc_device_ifname << " to "
389 << kArcBridge;
390 return false;
391 }
Hugo Benichi33860d72020-07-09 16:34:01 +0900392 LOG(INFO) << "Started ARC management device " << *arc_device_.get();
Hugo Benichiad1bdd92020-06-12 13:48:37 +0900393
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900394 // Start already known Shill <-> ARC mapped devices.
395 for (const auto& d : devices_)
396 StartDevice(d.second.get());
Garrick Evanscb791e72019-11-11 15:44:34 +0900397
Garrick Evansf29f5a32019-12-06 11:34:25 +0900398 return true;
Garrick Evanscb791e72019-11-11 15:44:34 +0900399}
400
Garrick Evans015b0d62020-02-07 09:06:38 +0900401void ArcService::Stop(uint32_t id) {
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900402 // Stop Shill <-> ARC mapped devices.
403 for (const auto& d : devices_)
404 StopDevice(d.second.get());
Garrick Evansf29f5a32019-12-06 11:34:25 +0900405
Hugo Benichiad1bdd92020-06-12 13:48:37 +0900406 // Per crbug/1008686 this device cannot be deleted and then re-added.
407 // So instead of removing the bridge, bring it down and mark it. This will
408 // allow us to detect if the device is re-added in case of a crash restart
409 // and do the right thing.
410 if (!datapath_->MaskInterfaceFlags(kArcBridge, IFF_DEBUG, IFF_UP))
411 LOG(ERROR) << "Failed to bring down arc bridge "
412 << "- it may not restart correctly";
413
Hugo Benichi33860d72020-07-09 16:34:01 +0900414 if (guest_ == GuestMessage::ARC) {
Hugo Benichiad1bdd92020-06-12 13:48:37 +0900415 datapath_->RemoveInterface(ArcVethHostName(arc_device_->phys_ifname()));
Hugo Benichi33860d72020-07-09 16:34:01 +0900416 if (!datapath_->NetnsDeleteName(kArcNetnsName))
417 LOG(WARNING) << "Failed to delete netns name " << kArcNetnsName;
Hugo Benichi8e448422020-07-07 10:49:00 +0900418 } else {
419 // Destroy pre allocated TAP devices, including the ARC management device.
420 for (const auto* config : all_configs_)
421 if (!config->tap_ifname().empty())
422 datapath_->RemoveInterface(config->tap_ifname());
Hugo Benichi33860d72020-07-09 16:34:01 +0900423 }
Hugo Benichiad1bdd92020-06-12 13:48:37 +0900424
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900425 if (impl_) {
426 impl_->Stop(id);
427 impl_.reset();
428 }
Hugo Benichiad1bdd92020-06-12 13:48:37 +0900429
Hugo Benichi33860d72020-07-09 16:34:01 +0900430 LOG(INFO) << "Stopped ARC management device " << *arc_device_.get();
Hugo Benichiad1bdd92020-06-12 13:48:37 +0900431 arc_device_.reset();
Garrick Evans5d55f5e2019-07-17 15:28:10 +0900432}
433
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900434void ArcService::OnDevicesChanged(const std::set<std::string>& added,
435 const std::set<std::string>& removed) {
436 for (const std::string& name : removed)
437 RemoveDevice(name);
438
439 for (const std::string& name : added)
440 AddDevice(name);
441}
442
443void ArcService::AddDevice(const std::string& ifname) {
444 if (ifname.empty())
445 return;
446
447 if (devices_.find(ifname) != devices_.end()) {
448 LOG(DFATAL) << "Attemping to add already tracked device: " << ifname;
449 return;
450 }
451
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900452 auto itype = InterfaceTypeFor(ifname);
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900453 Device::Options opts{
454 .fwd_multicast = IsMulticastInterface(ifname),
455 // TODO(crbug/726815) Also enable |ipv6_enabled| for cellular networks
456 // once IPv6 is enabled on cellular networks in shill.
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900457 .ipv6_enabled =
458 (itype == InterfaceType::ETHERNET || itype == InterfaceType::WIFI),
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900459 };
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900460
461 auto config = AcquireConfig(ifname);
462 if (!config) {
463 LOG(ERROR) << "Cannot add device for " << ifname;
464 return;
465 }
466
Garrick Evans8a067562020-05-11 12:47:30 +0900467 auto device = std::make_unique<Device>(ifname, ArcBridgeName(ifname), ifname,
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900468 std::move(config), opts);
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900469
470 StartDevice(device.get());
471 devices_.emplace(ifname, std::move(device));
472}
473
474void ArcService::StartDevice(Device* device) {
475 if (!impl_ || !impl_->IsStarted())
476 return;
477
Garrick Evans54861622019-07-19 09:05:09 +0900478 const auto& config = device->config();
479
Hugo Benichiad1bdd92020-06-12 13:48:37 +0900480 LOG(INFO) << "Starting device " << *device;
Garrick Evans54861622019-07-19 09:05:09 +0900481
482 // Create the bridge.
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900483 if (!datapath_->AddBridge(device->host_ifname(), config.host_ipv4_addr(),
Garrick Evans7a1a9ee2020-01-28 11:03:57 +0900484 30)) {
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900485 LOG(ERROR) << "Failed to setup arc bridge: " << device->host_ifname();
Garrick Evanse94b6de2020-02-20 09:19:13 +0900486 return;
Garrick Evans54861622019-07-19 09:05:09 +0900487 }
488
Garrick Evanse94b6de2020-02-20 09:19:13 +0900489 // Set up iptables.
490 if (!datapath_->AddInboundIPv4DNAT(
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900491 device->phys_ifname(), IPv4AddressToString(config.guest_ipv4_addr())))
Garrick Evanse94b6de2020-02-20 09:19:13 +0900492 LOG(ERROR) << "Failed to configure ingress traffic rules for "
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900493 << device->phys_ifname();
Garrick Evans54861622019-07-19 09:05:09 +0900494
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900495 if (!datapath_->AddOutboundIPv4(device->host_ifname()))
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900496 LOG(ERROR) << "Failed to configure egress traffic rules for "
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900497 << device->phys_ifname();
Garrick Evans2c263102019-07-26 16:07:18 +0900498
Hugo Benichicf904022020-07-07 09:21:58 +0900499 std::string virtual_device_ifname;
500 if (guest_ == GuestMessage::ARC_VM) {
501 virtual_device_ifname = device->config().tap_ifname();
502 if (virtual_device_ifname.empty()) {
503 LOG(ERROR) << "No TAP device for " << *device;
504 return;
505 }
506 } else {
507 virtual_device_ifname = ArcVethHostName(device->guest_ifname());
508 if (!datapath_->ConnectVethPair(
509 impl_->id(), kArcNetnsName, virtual_device_ifname, device->guest_ifname(),
510 device->config().mac_addr(), device->config().guest_ipv4_addr(), 30,
511 device->options().fwd_multicast)) {
512 LOG(ERROR) << "Cannot create virtual link for device " << *device;
513 return;
514 }
515 }
516
517 if (!datapath_->AddToBridge(device->host_ifname(), virtual_device_ifname)) {
518 if (guest_ == GuestMessage::ARC) {
519 datapath_->RemoveInterface(virtual_device_ifname);
520 }
521 LOG(ERROR) << "Failed to bridge interface " << virtual_device_ifname;
Hugo Benichi4833fda2020-07-06 14:56:56 +0900522 return;
523 }
524
525 forwarder_->StartForwarding(device->phys_ifname(), device->host_ifname(),
526 device->options().ipv6_enabled,
527 device->options().fwd_multicast);
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900528}
529
530void ArcService::RemoveDevice(const std::string& ifname) {
531 const auto it = devices_.find(ifname);
532 if (it == devices_.end()) {
533 LOG(WARNING) << "Unknown device: " << ifname;
Garrick Evanscb791e72019-11-11 15:44:34 +0900534 return;
535 }
536
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900537 // If the container is down, this call does nothing.
538 StopDevice(it->second.get());
539
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900540 ReleaseConfig(ifname, it->second->release_config());
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900541 devices_.erase(it);
Garrick Evans54861622019-07-19 09:05:09 +0900542}
543
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900544void ArcService::StopDevice(Device* device) {
545 if (!impl_ || !impl_->IsStarted())
546 return;
547
Hugo Benichiad1bdd92020-06-12 13:48:37 +0900548 LOG(INFO) << "Removing device " << *device;
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900549
Hugo Benichi4833fda2020-07-06 14:56:56 +0900550 forwarder_->StopForwarding(device->phys_ifname(), device->host_ifname(),
551 device->options().ipv6_enabled,
552 device->options().fwd_multicast);
553 // TAP devices are removed in VmImpl::Stop().
554 if (guest_ == GuestMessage::ARC)
555 datapath_->RemoveInterface(ArcVethHostName(device->phys_ifname()));
Garrick Evans54861622019-07-19 09:05:09 +0900556
557 const auto& config = device->config();
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900558 datapath_->RemoveOutboundIPv4(device->host_ifname());
Garrick Evanse94b6de2020-02-20 09:19:13 +0900559 datapath_->RemoveInboundIPv4DNAT(
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900560 device->phys_ifname(), IPv4AddressToString(config.guest_ipv4_addr()));
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900561 datapath_->RemoveBridge(device->host_ifname());
Garrick Evanscb791e72019-11-11 15:44:34 +0900562}
563
Garrick Evans38b25a42020-04-06 15:17:42 +0900564std::vector<const Device::Config*> ArcService::GetDeviceConfigs() const {
Hugo Benichi8e448422020-07-07 10:49:00 +0900565 std::vector<const Device::Config*> configs;
566 for (auto* c : all_configs_)
567 configs.emplace_back(c);
Garrick Evanse94b6de2020-02-20 09:19:13 +0900568
Hugo Benichi8e448422020-07-07 10:49:00 +0900569 return configs;
Garrick Evanse94b6de2020-02-20 09:19:13 +0900570}
571
Garrick Evansd90a3822019-11-12 17:53:08 +0900572// ARC++ specific functions.
573
Hugo Benichicf904022020-07-07 09:21:58 +0900574ArcService::ContainerImpl::ContainerImpl() : pid_(kInvalidPID) {}
Garrick Evansd90a3822019-11-12 17:53:08 +0900575
Garrick Evans015b0d62020-02-07 09:06:38 +0900576uint32_t ArcService::ContainerImpl::id() const {
Garrick Evans664a82f2019-12-17 12:18:05 +0900577 return pid_;
578}
579
Garrick Evans015b0d62020-02-07 09:06:38 +0900580bool ArcService::ContainerImpl::Start(uint32_t pid) {
Garrick Evansa51d0a12019-11-28 13:51:23 +0900581 // This could happen if something crashes and the stop signal is not sent.
582 // It can probably be addressed by stopping and restarting the service.
583 if (pid_ != kInvalidPID)
584 return false;
585
Garrick Evans4dec0c42019-11-29 12:51:57 +0900586 if (pid == kInvalidPID) {
Garrick Evansd90a3822019-11-12 17:53:08 +0900587 LOG(ERROR) << "Cannot start service - invalid container PID";
588 return false;
589 }
Garrick Evans4dec0c42019-11-29 12:51:57 +0900590 pid_ = pid;
Garrick Evansd90a3822019-11-12 17:53:08 +0900591
Garrick Evansd90a3822019-11-12 17:53:08 +0900592 LOG(INFO) << "ARC++ network service started {pid: " << pid_ << "}";
593 return true;
594}
595
Garrick Evans015b0d62020-02-07 09:06:38 +0900596void ArcService::ContainerImpl::Stop(uint32_t /*pid*/) {
Garrick Evans4dec0c42019-11-29 12:51:57 +0900597 if (!IsStarted())
Taoyu Li1c96d272019-12-13 14:17:43 +0900598 return;
Garrick Evans4dec0c42019-11-29 12:51:57 +0900599
Garrick Evansd90a3822019-11-12 17:53:08 +0900600 LOG(INFO) << "ARC++ network service stopped {pid: " << pid_ << "}";
601 pid_ = kInvalidPID;
602}
603
Garrick Evans015b0d62020-02-07 09:06:38 +0900604bool ArcService::ContainerImpl::IsStarted(uint32_t* pid) const {
Garrick Evansa51d0a12019-11-28 13:51:23 +0900605 if (pid)
606 *pid = pid_;
607
Garrick Evansd90a3822019-11-12 17:53:08 +0900608 return pid_ != kInvalidPID;
609}
610
Garrick Evansb4eb3892019-11-13 12:07:07 +0900611// VM specific functions
612
Hugo Benichicf904022020-07-07 09:21:58 +0900613ArcService::VmImpl::VmImpl() : cid_(kInvalidCID) {}
Garrick Evansb4eb3892019-11-13 12:07:07 +0900614
Garrick Evans015b0d62020-02-07 09:06:38 +0900615uint32_t ArcService::VmImpl::id() const {
Garrick Evans664a82f2019-12-17 12:18:05 +0900616 return cid_;
617}
618
Garrick Evans015b0d62020-02-07 09:06:38 +0900619bool ArcService::VmImpl::Start(uint32_t cid) {
Garrick Evansa51d0a12019-11-28 13:51:23 +0900620 // This can happen if concierge crashes and doesn't send the vm down RPC.
621 // It can probably be addressed by stopping and restarting the service.
622 if (cid_ != kInvalidCID)
623 return false;
624
Garrick Evans015b0d62020-02-07 09:06:38 +0900625 if (cid == kInvalidCID) {
Garrick Evansb4eb3892019-11-13 12:07:07 +0900626 LOG(ERROR) << "Invalid VM cid " << cid;
627 return false;
628 }
Garrick Evansb4eb3892019-11-13 12:07:07 +0900629 cid_ = cid;
Garrick Evansb4eb3892019-11-13 12:07:07 +0900630
Garrick Evanse94b6de2020-02-20 09:19:13 +0900631 LOG(INFO) << "ARCVM network service started {cid: " << cid_ << "}";
Garrick Evansb4eb3892019-11-13 12:07:07 +0900632 return true;
633}
634
Garrick Evans015b0d62020-02-07 09:06:38 +0900635void ArcService::VmImpl::Stop(uint32_t cid) {
Garrick Evans21173b12019-11-20 15:23:16 +0900636 if (cid_ != cid) {
637 LOG(ERROR) << "Mismatched ARCVM CIDs " << cid_ << " != " << cid;
638 return;
639 }
640
Garrick Evansb4eb3892019-11-13 12:07:07 +0900641 LOG(INFO) << "ARCVM network service stopped {cid: " << cid_ << "}";
642 cid_ = kInvalidCID;
643}
644
Garrick Evans015b0d62020-02-07 09:06:38 +0900645bool ArcService::VmImpl::IsStarted(uint32_t* cid) const {
Garrick Evansa51d0a12019-11-28 13:51:23 +0900646 if (cid)
647 *cid = cid_;
648
Garrick Evans015b0d62020-02-07 09:06:38 +0900649 return cid_ != kInvalidCID;
Garrick Evansb4eb3892019-11-13 12:07:07 +0900650}
Garrick Evans3388a032020-03-24 11:25:55 +0900651} // namespace patchpanel