blob: a9e6cae9caff30be80297ff66ba60043f13295b3 [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
Jason Jeremy Iman3081d0e2020-03-04 15:52:06 +090026#include "patchpanel/adb_proxy.h"
Garrick Evans3388a032020-03-24 11:25:55 +090027#include "patchpanel/datapath.h"
28#include "patchpanel/mac_address_generator.h"
29#include "patchpanel/manager.h"
30#include "patchpanel/minijailed_process_runner.h"
31#include "patchpanel/net_util.h"
32#include "patchpanel/scoped_ns.h"
Garrick Evans5d55f5e2019-07-17 15:28:10 +090033
Garrick Evans3388a032020-03-24 11:25:55 +090034namespace patchpanel {
Garrick Evans5d55f5e2019-07-17 15:28:10 +090035namespace {
Hugo Benichi4d4bb8f2020-07-07 12:16:07 +090036constexpr uint32_t kInvalidId = 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
Hugo Benichif0f10c72020-07-09 10:42:45 +090060void OneTimeContainerSetup(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
Hugo Benichif0f10c72020-07-09 10:42:45 +0900170// Returns the ARC management device used for VPN forwarding, ADB-over-TCP.
171std::unique_ptr<Device> MakeArcDevice(AddressManager* addr_mgr,
172 GuestMessage::GuestType guest) {
173 auto ipv4_subnet = addr_mgr->AllocateIPv4Subnet(AddressManager::Guest::ARC);
Garrick Evanse94b6de2020-02-20 09:19:13 +0900174 if (!ipv4_subnet) {
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900175 LOG(ERROR) << "Subnet already in use or unavailable";
Garrick Evanse94b6de2020-02-20 09:19:13 +0900176 return nullptr;
177 }
Hugo Benichif0f10c72020-07-09 10:42:45 +0900178
Garrick Evanse94b6de2020-02-20 09:19:13 +0900179 auto host_ipv4_addr = ipv4_subnet->AllocateAtOffset(0);
180 if (!host_ipv4_addr) {
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900181 LOG(ERROR) << "Bridge address already in use or unavailable";
Garrick Evanse94b6de2020-02-20 09:19:13 +0900182 return nullptr;
183 }
Hugo Benichif0f10c72020-07-09 10:42:45 +0900184
Garrick Evanse94b6de2020-02-20 09:19:13 +0900185 auto guest_ipv4_addr = ipv4_subnet->AllocateAtOffset(1);
186 if (!guest_ipv4_addr) {
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900187 LOG(ERROR) << "ARC address already in use or unavailable";
Garrick Evanse94b6de2020-02-20 09:19:13 +0900188 return nullptr;
189 }
190
Hugo Benichif0f10c72020-07-09 10:42:45 +0900191 int subnet_index = (guest == GuestMessage::ARC_VM) ? 1 : kAnySubnetIndex;
Hugo Benichiad1bdd92020-06-12 13:48:37 +0900192
Hugo Benichif0f10c72020-07-09 10:42:45 +0900193 auto config = std::make_unique<Device::Config>(
Hugo Benichiad1bdd92020-06-12 13:48:37 +0900194 addr_mgr->GenerateMacAddress(subnet_index), std::move(ipv4_subnet),
195 std::move(host_ipv4_addr), std::move(guest_ipv4_addr));
Garrick Evanse94b6de2020-02-20 09:19:13 +0900196
Hugo Benichif0f10c72020-07-09 10:42:45 +0900197 Device::Options opts{
198 .fwd_multicast = false,
199 .ipv6_enabled = false,
Jason Jeremy Iman3081d0e2020-03-04 15:52:06 +0900200 .adb_allowed = false,
Hugo Benichif0f10c72020-07-09 10:42:45 +0900201 };
202
203 return std::make_unique<Device>(kArcIfname, kArcBridge, kArcIfname,
204 std::move(config), opts);
205}
Garrick Evans5d55f5e2019-07-17 15:28:10 +0900206} // namespace
207
Garrick Evans69b85872020-02-04 11:40:26 +0900208ArcService::ArcService(ShillClient* shill_client,
Garrick Evans2e5c9ab2020-03-05 14:33:58 +0900209 Datapath* datapath,
210 AddressManager* addr_mgr,
Garrick Evansf5862122020-03-16 09:13:45 +0900211 TrafficForwarder* forwarder,
Hugo Benichiad1bdd92020-06-12 13:48:37 +0900212 GuestMessage::GuestType guest)
Garrick Evans2e5c9ab2020-03-05 14:33:58 +0900213 : shill_client_(shill_client),
Garrick Evans2e5c9ab2020-03-05 14:33:58 +0900214 datapath_(datapath),
215 addr_mgr_(addr_mgr),
Garrick Evansf5862122020-03-16 09:13:45 +0900216 forwarder_(forwarder),
Hugo Benichi4d4bb8f2020-07-07 12:16:07 +0900217 guest_(guest),
218 id_(kInvalidId) {
Hugo Benichif0f10c72020-07-09 10:42:45 +0900219 arc_device_ = MakeArcDevice(addr_mgr, guest_);
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900220 AllocateAddressConfigs();
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900221 shill_client_->RegisterDevicesChangedHandler(
222 base::Bind(&ArcService::OnDevicesChanged, weak_factory_.GetWeakPtr()));
Jie Jiang84c76a12020-04-17 16:45:20 +0900223 shill_client_->ScanDevices();
Garrick Evansf29f5a32019-12-06 11:34:25 +0900224}
225
226ArcService::~ArcService() {
Hugo Benichi4d4bb8f2020-07-07 12:16:07 +0900227 if (IsStarted()) {
228 Stop(id_);
Garrick Evans664a82f2019-12-17 12:18:05 +0900229 }
Garrick Evans54861622019-07-19 09:05:09 +0900230}
Garrick Evans5d55f5e2019-07-17 15:28:10 +0900231
Hugo Benichi4d4bb8f2020-07-07 12:16:07 +0900232bool ArcService::IsStarted() const {
233 return id_ != kInvalidId;
234}
235
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900236void ArcService::AllocateAddressConfigs() {
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900237 // The first usable subnet is the "other" ARC device subnet.
Garrick Evansc7071122020-04-17 12:31:57 +0900238 // As a temporary workaround, for ARCVM, allocate fixed MAC addresses.
239 uint8_t mac_addr_index = 2;
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900240 // Allocate 2 subnets each for Ethernet and WiFi and 1 for LTE WAN interfaces.
241 for (const auto itype :
242 {InterfaceType::ETHERNET, InterfaceType::ETHERNET, InterfaceType::WIFI,
243 InterfaceType::WIFI, InterfaceType::CELL}) {
Hugo Benichiad1bdd92020-06-12 13:48:37 +0900244 auto ipv4_subnet =
245 addr_mgr_->AllocateIPv4Subnet(AddressManager::Guest::ARC_NET);
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900246 if (!ipv4_subnet) {
247 LOG(ERROR) << "Subnet already in use or unavailable";
248 continue;
249 }
250 // For here out, use the same slices.
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900251 auto host_ipv4_addr = ipv4_subnet->AllocateAtOffset(0);
252 if (!host_ipv4_addr) {
253 LOG(ERROR) << "Bridge address already in use or unavailable";
254 continue;
255 }
256 auto guest_ipv4_addr = ipv4_subnet->AllocateAtOffset(1);
257 if (!guest_ipv4_addr) {
258 LOG(ERROR) << "ARC address already in use or unavailable";
259 continue;
260 }
261
Hugo Benichiad1bdd92020-06-12 13:48:37 +0900262 MacAddress mac_addr = (guest_ == GuestMessage::ARC_VM)
Garrick Evansc7071122020-04-17 12:31:57 +0900263 ? addr_mgr_->GenerateMacAddress(mac_addr_index++)
264 : addr_mgr_->GenerateMacAddress();
Hugo Benichi8e448422020-07-07 10:49:00 +0900265 available_configs_[itype].emplace_back(std::make_unique<Device::Config>(
Garrick Evansc7071122020-04-17 12:31:57 +0900266 mac_addr, std::move(ipv4_subnet), std::move(host_ipv4_addr),
267 std::move(guest_ipv4_addr)));
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900268 }
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900269
Hugo Benichi8e448422020-07-07 10:49:00 +0900270 for (const auto& kv : available_configs_)
Hugo Benichiad1bdd92020-06-12 13:48:37 +0900271 for (const auto& c : kv.second)
Hugo Benichi8e448422020-07-07 10:49:00 +0900272 all_configs_.emplace_back(c.get());
Hugo Benichif0f10c72020-07-09 10:42:45 +0900273 // Append arc0 config so that the necessary tap device gets created.
274 all_configs_.insert(all_configs_.begin(), &arc_device_->config());
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900275}
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) {
Hugo Benichi4d4bb8f2020-07-07 12:16:07 +0900309 if (IsStarted()) {
310 LOG(WARNING) << "Already running - did something crash?"
311 << " Stopping and restarting...";
312 Stop(id_);
Garrick Evansa51d0a12019-11-28 13:51:23 +0900313 }
314
Hugo Benichif0f10c72020-07-09 10:42:45 +0900315 std::string arc_device_ifname;
Hugo Benichiad1bdd92020-06-12 13:48:37 +0900316 if (guest_ == GuestMessage::ARC_VM) {
Hugo Benichi8e448422020-07-07 10:49:00 +0900317 // Allocate TAP devices for all configs.
318 for (auto* config : all_configs_) {
319 auto mac = config->mac_addr();
320 auto tap = datapath_->AddTAP("" /* auto-generate name */, &mac,
321 nullptr /* no ipv4 subnet */,
322 vm_tools::kCrosVmUser);
323 if (tap.empty()) {
324 LOG(ERROR) << "Failed to create TAP device";
325 continue;
326 }
327
328 config->set_tap_ifname(tap);
329 }
Hugo Benichif0f10c72020-07-09 10:42:45 +0900330 arc_device_ifname = arc_device_->config().tap_ifname();
Garrick Evans2961c7c2020-04-03 11:34:40 +0900331 } else {
Hugo Benichif0f10c72020-07-09 10:42:45 +0900332 OneTimeContainerSetup(*datapath_);
Hugo Benichi33860d72020-07-09 16:34:01 +0900333 if (!datapath_->NetnsAttachName(kArcNetnsName, id)) {
334 LOG(ERROR) << "Failed to attach name " << kArcNetnsName << " to pid "
335 << id;
336 return false;
337 }
Hugo Benichiad1bdd92020-06-12 13:48:37 +0900338 arc_device_ifname = ArcVethHostName(arc_device_->guest_ifname());
Hugo Benichi4d4bb8f2020-07-07 12:16:07 +0900339 if (!datapath_->ConnectVethPair(id, kArcNetnsName, arc_device_ifname,
340 arc_device_->guest_ifname(),
341 arc_device_->config().mac_addr(),
342 arc_device_->config().guest_ipv4_addr(), 30,
343 arc_device_->options().fwd_multicast)) {
Hugo Benichiad1bdd92020-06-12 13:48:37 +0900344 LOG(ERROR) << "Cannot create virtual link for device "
345 << arc_device_->phys_ifname();
346 return false;
347 }
348 }
Hugo Benichif0f10c72020-07-09 10:42:45 +0900349 id_ = id;
350
351 // Create the bridge for the management device arc0.
352 // Per crbug/1008686 this device cannot be deleted and then re-added.
353 // So instead of removing the bridge when the service stops, bring down the
354 // device instead and re-up it on restart.
355 if (!datapath_->AddBridge(kArcBridge, arc_device_->config().host_ipv4_addr(),
356 30) &&
357 !datapath_->MaskInterfaceFlags(kArcBridge, IFF_UP)) {
358 LOG(ERROR) << "Failed to bring up arc bridge " << kArcBridge;
359 return false;
360 }
Hugo Benichiad1bdd92020-06-12 13:48:37 +0900361
362 if (!datapath_->AddToBridge(kArcBridge, arc_device_ifname)) {
363 LOG(ERROR) << "Failed to bridge arc device " << arc_device_ifname << " to "
364 << kArcBridge;
365 return false;
366 }
Hugo Benichi33860d72020-07-09 16:34:01 +0900367 LOG(INFO) << "Started ARC management device " << *arc_device_.get();
Hugo Benichiad1bdd92020-06-12 13:48:37 +0900368
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900369 // Start already known Shill <-> ARC mapped devices.
Hugo Benichif0f10c72020-07-09 10:42:45 +0900370 for (const auto& ifname : shill_devices_)
371 AddDevice(ifname);
Garrick Evanscb791e72019-11-11 15:44:34 +0900372
Garrick Evansf29f5a32019-12-06 11:34:25 +0900373 return true;
Garrick Evanscb791e72019-11-11 15:44:34 +0900374}
375
Garrick Evans015b0d62020-02-07 09:06:38 +0900376void ArcService::Stop(uint32_t id) {
Hugo Benichi4d4bb8f2020-07-07 12:16:07 +0900377 if (!IsStarted()) {
378 LOG(ERROR) << "ArcService was not running";
379 return;
380 }
381
382 // After the ARC container has stopped, the pid is not known anymore.
383 if (guest_ == GuestMessage::ARC_VM && id_ != id) {
384 LOG(ERROR) << "Mismatched ARCVM CIDs " << id_ << " != " << id;
385 return;
386 }
387
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900388 // Stop Shill <-> ARC mapped devices.
Hugo Benichif0f10c72020-07-09 10:42:45 +0900389 for (const auto& ifname : shill_devices_)
390 RemoveDevice(ifname);
Garrick Evansf29f5a32019-12-06 11:34:25 +0900391
Hugo Benichiad1bdd92020-06-12 13:48:37 +0900392 // Per crbug/1008686 this device cannot be deleted and then re-added.
393 // So instead of removing the bridge, bring it down and mark it. This will
394 // allow us to detect if the device is re-added in case of a crash restart
395 // and do the right thing.
396 if (!datapath_->MaskInterfaceFlags(kArcBridge, IFF_DEBUG, IFF_UP))
397 LOG(ERROR) << "Failed to bring down arc bridge "
398 << "- it may not restart correctly";
399
Hugo Benichi33860d72020-07-09 16:34:01 +0900400 if (guest_ == GuestMessage::ARC) {
Hugo Benichiad1bdd92020-06-12 13:48:37 +0900401 datapath_->RemoveInterface(ArcVethHostName(arc_device_->phys_ifname()));
Hugo Benichi33860d72020-07-09 16:34:01 +0900402 if (!datapath_->NetnsDeleteName(kArcNetnsName))
403 LOG(WARNING) << "Failed to delete netns name " << kArcNetnsName;
Hugo Benichif0f10c72020-07-09 10:42:45 +0900404 }
405
406 // Destroy allocated TAP devices if any, including the ARC management device.
407 for (auto* config : all_configs_) {
408 if (config->tap_ifname().empty())
409 continue;
410 datapath_->RemoveInterface(config->tap_ifname());
411 config->set_tap_ifname("");
Hugo Benichi33860d72020-07-09 16:34:01 +0900412 }
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 Benichi4d4bb8f2020-07-07 12:16:07 +0900415 id_ = kInvalidId;
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) {
Hugo Benichif0f10c72020-07-09 10:42:45 +0900420 for (const std::string& ifname : removed) {
421 shill_devices_.erase(ifname);
422 RemoveDevice(ifname);
423 }
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900424
Hugo Benichif0f10c72020-07-09 10:42:45 +0900425 for (const std::string& ifname : added) {
426 shill_devices_.insert(ifname);
427 AddDevice(ifname);
428 }
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900429}
430
431void ArcService::AddDevice(const std::string& ifname) {
Hugo Benichif0f10c72020-07-09 10:42:45 +0900432 if (!IsStarted())
433 return;
434
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900435 if (ifname.empty())
436 return;
437
438 if (devices_.find(ifname) != devices_.end()) {
439 LOG(DFATAL) << "Attemping to add already tracked device: " << ifname;
440 return;
441 }
442
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900443 auto itype = InterfaceTypeFor(ifname);
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900444 Device::Options opts{
445 .fwd_multicast = IsMulticastInterface(ifname),
446 // TODO(crbug/726815) Also enable |ipv6_enabled| for cellular networks
Hugo Benichif0f10c72020-07-09 10:42:45 +0900447 // once IPv6 is enabled on cellular networks in Shill.
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900448 .ipv6_enabled =
449 (itype == InterfaceType::ETHERNET || itype == InterfaceType::WIFI),
Jason Jeremy Iman3081d0e2020-03-04 15:52:06 +0900450 .adb_allowed =
451 (itype == InterfaceType::ETHERNET || itype == InterfaceType::WIFI),
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900452 };
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900453
454 auto config = AcquireConfig(ifname);
455 if (!config) {
Hugo Benichif0f10c72020-07-09 10:42:45 +0900456 LOG(ERROR) << "Cannot acquire a Config for " << ifname;
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900457 return;
458 }
459
Garrick Evans8a067562020-05-11 12:47:30 +0900460 auto device = std::make_unique<Device>(ifname, ArcBridgeName(ifname), ifname,
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900461 std::move(config), opts);
Hugo Benichiad1bdd92020-06-12 13:48:37 +0900462 LOG(INFO) << "Starting device " << *device;
Garrick Evans54861622019-07-19 09:05:09 +0900463
464 // Create the bridge.
Hugo Benichif0f10c72020-07-09 10:42:45 +0900465 if (!datapath_->AddBridge(device->host_ifname(),
466 device->config().host_ipv4_addr(), 30)) {
467 LOG(ERROR) << "Failed to setup bridge " << device->host_ifname();
Garrick Evanse94b6de2020-02-20 09:19:13 +0900468 return;
Garrick Evans54861622019-07-19 09:05:09 +0900469 }
470
Garrick Evanse94b6de2020-02-20 09:19:13 +0900471 // Set up iptables.
472 if (!datapath_->AddInboundIPv4DNAT(
Hugo Benichif0f10c72020-07-09 10:42:45 +0900473 device->phys_ifname(),
474 IPv4AddressToString(device->config().guest_ipv4_addr())))
Garrick Evanse94b6de2020-02-20 09:19:13 +0900475 LOG(ERROR) << "Failed to configure ingress traffic rules for "
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900476 << device->phys_ifname();
Garrick Evans54861622019-07-19 09:05:09 +0900477
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900478 if (!datapath_->AddOutboundIPv4(device->host_ifname()))
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900479 LOG(ERROR) << "Failed to configure egress traffic rules for "
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900480 << device->phys_ifname();
Garrick Evans2c263102019-07-26 16:07:18 +0900481
Hugo Benichicf904022020-07-07 09:21:58 +0900482 std::string virtual_device_ifname;
483 if (guest_ == GuestMessage::ARC_VM) {
484 virtual_device_ifname = device->config().tap_ifname();
485 if (virtual_device_ifname.empty()) {
486 LOG(ERROR) << "No TAP device for " << *device;
487 return;
488 }
489 } else {
490 virtual_device_ifname = ArcVethHostName(device->guest_ifname());
491 if (!datapath_->ConnectVethPair(
Hugo Benichi4d4bb8f2020-07-07 12:16:07 +0900492 id_, kArcNetnsName, virtual_device_ifname, device->guest_ifname(),
Hugo Benichicf904022020-07-07 09:21:58 +0900493 device->config().mac_addr(), device->config().guest_ipv4_addr(), 30,
494 device->options().fwd_multicast)) {
Hugo Benichif0f10c72020-07-09 10:42:45 +0900495 LOG(ERROR) << "Cannot create veth link for device " << *device;
Hugo Benichicf904022020-07-07 09:21:58 +0900496 return;
497 }
498 }
499
500 if (!datapath_->AddToBridge(device->host_ifname(), virtual_device_ifname)) {
501 if (guest_ == GuestMessage::ARC) {
502 datapath_->RemoveInterface(virtual_device_ifname);
503 }
504 LOG(ERROR) << "Failed to bridge interface " << virtual_device_ifname;
Hugo Benichi4833fda2020-07-06 14:56:56 +0900505 return;
506 }
507
Jason Jeremy Iman3081d0e2020-03-04 15:52:06 +0900508 if (device->options().adb_allowed &&
509 !datapath_->AddAdbPortAccessRule(ifname)) {
510 LOG(ERROR) << "Failed to add ADB port access rule";
511 }
512
Hugo Benichi4833fda2020-07-06 14:56:56 +0900513 forwarder_->StartForwarding(device->phys_ifname(), device->host_ifname(),
514 device->options().ipv6_enabled,
515 device->options().fwd_multicast);
Hugo Benichif0f10c72020-07-09 10:42:45 +0900516 devices_.emplace(ifname, std::move(device));
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900517}
518
519void ArcService::RemoveDevice(const std::string& ifname) {
Hugo Benichif0f10c72020-07-09 10:42:45 +0900520 if (!IsStarted())
521 return;
522
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900523 const auto it = devices_.find(ifname);
524 if (it == devices_.end()) {
525 LOG(WARNING) << "Unknown device: " << ifname;
Garrick Evanscb791e72019-11-11 15:44:34 +0900526 return;
527 }
528
Hugo Benichif0f10c72020-07-09 10:42:45 +0900529 const auto* device = it->second.get();
Hugo Benichiad1bdd92020-06-12 13:48:37 +0900530 LOG(INFO) << "Removing device " << *device;
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900531
Hugo Benichi4833fda2020-07-06 14:56:56 +0900532 forwarder_->StopForwarding(device->phys_ifname(), device->host_ifname(),
533 device->options().ipv6_enabled,
534 device->options().fwd_multicast);
Hugo Benichif0f10c72020-07-09 10:42:45 +0900535
536 // ARCVM TAP devices are removed in VmImpl::Stop() when the service stops
Hugo Benichi4833fda2020-07-06 14:56:56 +0900537 if (guest_ == GuestMessage::ARC)
538 datapath_->RemoveInterface(ArcVethHostName(device->phys_ifname()));
Garrick Evans54861622019-07-19 09:05:09 +0900539
540 const auto& config = device->config();
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900541 datapath_->RemoveOutboundIPv4(device->host_ifname());
Garrick Evanse94b6de2020-02-20 09:19:13 +0900542 datapath_->RemoveInboundIPv4DNAT(
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900543 device->phys_ifname(), IPv4AddressToString(config.guest_ipv4_addr()));
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900544 datapath_->RemoveBridge(device->host_ifname());
Jason Jeremy Iman3081d0e2020-03-04 15:52:06 +0900545 if (device->options().adb_allowed)
546 datapath_->DeleteAdbPortAccessRule(ifname);
Hugo Benichif0f10c72020-07-09 10:42:45 +0900547
548 ReleaseConfig(ifname, it->second->release_config());
549 devices_.erase(it);
Garrick Evanscb791e72019-11-11 15:44:34 +0900550}
551
Garrick Evans38b25a42020-04-06 15:17:42 +0900552std::vector<const Device::Config*> ArcService::GetDeviceConfigs() const {
Hugo Benichi8e448422020-07-07 10:49:00 +0900553 std::vector<const Device::Config*> configs;
554 for (auto* c : all_configs_)
555 configs.emplace_back(c);
Garrick Evanse94b6de2020-02-20 09:19:13 +0900556
Hugo Benichi8e448422020-07-07 10:49:00 +0900557 return configs;
Garrick Evanse94b6de2020-02-20 09:19:13 +0900558}
Garrick Evans3388a032020-03-24 11:25:55 +0900559} // namespace patchpanel