blob: 7d864988dbb5e2adaa0d54dc0eae7a312746e92d [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 Evans54861622019-07-19 09:05:09 +090010
Garrick Evans5d55f5e2019-07-17 15:28:10 +090011#include <utility>
Jason Jeremy Imanf4156cb2019-11-14 15:36:22 +090012#include <vector>
Garrick Evans5d55f5e2019-07-17 15:28:10 +090013
Garrick Evans54861622019-07-19 09:05:09 +090014#include <base/bind.h>
Garrick Evans5d55f5e2019-07-17 15:28:10 +090015#include <base/files/file_path.h>
16#include <base/files/file_util.h>
17#include <base/logging.h>
18#include <base/strings/string_number_conversions.h>
19#include <base/strings/string_util.h>
Garrick Evans54861622019-07-19 09:05:09 +090020#include <base/strings/stringprintf.h>
Garrick Evans1f5a3612019-11-08 12:59:03 +090021#include <brillo/key_value_store.h>
Garrick Evansb4eb3892019-11-13 12:07:07 +090022#include <chromeos/constants/vm_tools.h>
Garrick Evans54861622019-07-19 09:05:09 +090023
Garrick Evans3388a032020-03-24 11:25:55 +090024#include "patchpanel/datapath.h"
25#include "patchpanel/mac_address_generator.h"
26#include "patchpanel/manager.h"
27#include "patchpanel/minijailed_process_runner.h"
28#include "patchpanel/net_util.h"
29#include "patchpanel/scoped_ns.h"
Garrick Evans5d55f5e2019-07-17 15:28:10 +090030
Garrick Evans3388a032020-03-24 11:25:55 +090031namespace patchpanel {
Garrick Evansf29f5a32019-12-06 11:34:25 +090032namespace test {
33GuestMessage::GuestType guest = GuestMessage::UNKNOWN_GUEST;
34} // namespace test
35
Garrick Evans5d55f5e2019-07-17 15:28:10 +090036namespace {
Garrick Evans015b0d62020-02-07 09:06:38 +090037constexpr pid_t kInvalidPID = 0;
Garrick Evans015b0d62020-02-07 09:06:38 +090038constexpr uint32_t kInvalidCID = 0;
Garrick Evanse94b6de2020-02-20 09:19:13 +090039constexpr char kArcIfname[] = "arc0";
40constexpr char kArcBridge[] = "arcbr0";
41constexpr char kArcVmIfname[] = "arc1";
42constexpr char kArcVmBridge[] = "arc_br1";
Garrick Evans6e4eb3b2020-03-09 07:18:31 +090043constexpr std::array<const char*, 2> kEthernetInterfacePrefixes{{"eth", "usb"}};
44constexpr std::array<const char*, 2> kWifiInterfacePrefixes{{"wlan", "mlan"}};
Garrick Evans86c7d9c2020-03-17 09:25:48 +090045constexpr std::array<const char*, 2> kCellInterfacePrefixes{{"wwan", "rmnet"}};
Garrick Evans54861622019-07-19 09:05:09 +090046
Garrick Evans6d227b92019-12-03 16:11:29 +090047void OneTimeSetup(const Datapath& datapath) {
Garrick Evansa34b5862019-11-20 09:34:01 +090048 static bool done = false;
49 if (done)
50 return;
51
Garrick Evans6d227b92019-12-03 16:11:29 +090052 auto& runner = datapath.runner();
53
54 // Load networking modules needed by Android that are not compiled in the
55 // kernel. Android does not allow auto-loading of kernel modules.
Garrick Evansa34b5862019-11-20 09:34:01 +090056 // These must succeed.
Garrick Evans8e8e3472020-01-23 14:03:50 +090057 if (runner.modprobe_all({
Garrick Evansa34b5862019-11-20 09:34:01 +090058 // The netfilter modules needed by netd for iptables commands.
59 "ip6table_filter",
60 "ip6t_ipv6header",
61 "ip6t_REJECT",
62 // The xfrm modules needed for Android's ipsec APIs.
63 "xfrm4_mode_transport",
64 "xfrm4_mode_tunnel",
65 "xfrm6_mode_transport",
66 "xfrm6_mode_tunnel",
67 // The ipsec modules for AH and ESP encryption for ipv6.
68 "ah6",
69 "esp6",
70 }) != 0) {
71 LOG(ERROR) << "One or more required kernel modules failed to load."
72 << " Some Android functionality may be broken.";
73 }
74 // Optional modules.
Garrick Evans8e8e3472020-01-23 14:03:50 +090075 if (runner.modprobe_all({
Garrick Evansa34b5862019-11-20 09:34:01 +090076 // This module is not available in kernels < 3.18
77 "nf_reject_ipv6",
78 // These modules are needed for supporting Chrome traffic on Android
79 // VPN which uses Android's NAT feature. Android NAT sets up
80 // iptables
81 // rules that use these conntrack modules for FTP/TFTP.
82 "nf_nat_ftp",
83 "nf_nat_tftp",
Hugo Benichia0cde9e2019-12-16 11:57:20 +090084 // The tun module is needed by the Android 464xlat clatd process.
85 "tun",
Garrick Evansa34b5862019-11-20 09:34:01 +090086 }) != 0) {
87 LOG(WARNING) << "One or more optional kernel modules failed to load.";
88 }
89
Garrick Evans6d227b92019-12-03 16:11:29 +090090 // This is only needed for CTS (b/27932574).
Garrick Evans8e8e3472020-01-23 14:03:50 +090091 if (runner.chown("655360", "655360", "/sys/class/xt_idletimer") != 0) {
Garrick Evans6d227b92019-12-03 16:11:29 +090092 LOG(ERROR) << "Failed to change ownership of xt_idletimer.";
93 }
94
Garrick Evansa34b5862019-11-20 09:34:01 +090095 done = true;
96}
97
Garrick Evans508a4bc2019-11-14 08:45:52 +090098bool IsArcVm() {
Garrick Evansc7071122020-04-17 12:31:57 +090099 if (test::guest == GuestMessage::ARC_VM) {
100 LOG(WARNING) << "Overridden for testing";
101 return true;
102 }
103
Garrick Evans508a4bc2019-11-14 08:45:52 +0900104 const base::FilePath path("/run/chrome/is_arcvm");
105 std::string contents;
106 if (!base::ReadFileToString(path, &contents)) {
107 PLOG(ERROR) << "Could not read " << path.value();
108 }
109 return contents == "1";
110}
111
Garrick Evansf29f5a32019-12-06 11:34:25 +0900112GuestMessage::GuestType ArcGuest() {
113 if (test::guest != GuestMessage::UNKNOWN_GUEST)
114 return test::guest;
Garrick Evans508a4bc2019-11-14 08:45:52 +0900115
Garrick Evansb05a7ff2020-02-18 12:59:55 +0900116 return IsArcVm() ? GuestMessage::ARC_VM : GuestMessage::ARC;
Garrick Evans508a4bc2019-11-14 08:45:52 +0900117}
118
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900119ArcService::InterfaceType InterfaceTypeFor(const std::string& ifname) {
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900120 for (const auto& prefix : kEthernetInterfacePrefixes) {
121 if (base::StartsWith(ifname, prefix,
122 base::CompareCase::INSENSITIVE_ASCII)) {
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900123 return ArcService::InterfaceType::ETHERNET;
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900124 }
125 }
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900126 for (const auto& prefix : kWifiInterfacePrefixes) {
127 if (base::StartsWith(ifname, prefix,
128 base::CompareCase::INSENSITIVE_ASCII)) {
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900129 return ArcService::InterfaceType::WIFI;
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900130 }
131 }
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900132 for (const auto& prefix : kCellInterfacePrefixes) {
133 if (base::StartsWith(ifname, prefix,
134 base::CompareCase::INSENSITIVE_ASCII)) {
135 return ArcService::InterfaceType::CELL;
136 }
137 }
138 return ArcService::InterfaceType::UNKNOWN;
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900139}
140
141bool IsMulticastInterface(const std::string& ifname) {
142 if (ifname.empty()) {
143 return false;
144 }
145
146 int fd = socket(AF_INET, SOCK_DGRAM, 0);
147 if (fd < 0) {
148 // If IPv4 fails, try to open a socket using IPv6.
149 fd = socket(AF_INET6, SOCK_DGRAM, 0);
150 if (fd < 0) {
151 LOG(ERROR) << "Unable to create socket";
152 return false;
153 }
154 }
155
156 struct ifreq ifr;
157 memset(&ifr, 0, sizeof(ifr));
158 strncpy(ifr.ifr_name, ifname.c_str(), IFNAMSIZ);
159 if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0) {
160 PLOG(ERROR) << "SIOCGIFFLAGS failed for " << ifname;
161 close(fd);
162 return false;
163 }
164
165 close(fd);
166 return (ifr.ifr_flags & IFF_MULTICAST);
167}
168
Garrick Evanse94b6de2020-02-20 09:19:13 +0900169// Returns the configuration for the ARC management interface used for VPN
170// forwarding, ADB-over-TCP and single-networked ARCVM.
171std::unique_ptr<Device::Config> MakeArcConfig(AddressManager* addr_mgr,
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900172 AddressManager::Guest guest) {
173 auto ipv4_subnet = addr_mgr->AllocateIPv4Subnet(guest);
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 }
178 auto host_ipv4_addr = ipv4_subnet->AllocateAtOffset(0);
179 if (!host_ipv4_addr) {
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900180 LOG(ERROR) << "Bridge address already in use or unavailable";
Garrick Evanse94b6de2020-02-20 09:19:13 +0900181 return nullptr;
182 }
183 auto guest_ipv4_addr = ipv4_subnet->AllocateAtOffset(1);
184 if (!guest_ipv4_addr) {
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900185 LOG(ERROR) << "ARC address already in use or unavailable";
Garrick Evanse94b6de2020-02-20 09:19:13 +0900186 return nullptr;
187 }
188
189 return std::make_unique<Device::Config>(
Garrick Evansc7071122020-04-17 12:31:57 +0900190 addr_mgr->GenerateMacAddress(IsArcVm() ? 1 : kAnySubnetIndex),
191 std::move(ipv4_subnet), std::move(host_ipv4_addr),
192 std::move(guest_ipv4_addr));
Garrick Evanse94b6de2020-02-20 09:19:13 +0900193}
194
Garrick Evans5d55f5e2019-07-17 15:28:10 +0900195} // namespace
196
Garrick Evans69b85872020-02-04 11:40:26 +0900197ArcService::ArcService(ShillClient* shill_client,
Garrick Evans2e5c9ab2020-03-05 14:33:58 +0900198 Datapath* datapath,
199 AddressManager* addr_mgr,
Garrick Evansf5862122020-03-16 09:13:45 +0900200 TrafficForwarder* forwarder,
201 bool enable_arcvm_multinet)
Garrick Evans2e5c9ab2020-03-05 14:33:58 +0900202 : shill_client_(shill_client),
Garrick Evans2e5c9ab2020-03-05 14:33:58 +0900203 datapath_(datapath),
204 addr_mgr_(addr_mgr),
Garrick Evansf5862122020-03-16 09:13:45 +0900205 forwarder_(forwarder),
206 enable_arcvm_multinet_(enable_arcvm_multinet) {
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900207 AllocateAddressConfigs();
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900208 shill_client_->RegisterDevicesChangedHandler(
209 base::Bind(&ArcService::OnDevicesChanged, weak_factory_.GetWeakPtr()));
Jie Jiang84c76a12020-04-17 16:45:20 +0900210 shill_client_->ScanDevices();
Garrick Evansbbdf4b42020-03-05 12:59:06 +0900211 shill_client_->RegisterDefaultInterfaceChangedHandler(base::Bind(
212 &ArcService::OnDefaultInterfaceChanged, weak_factory_.GetWeakPtr()));
Garrick Evansf29f5a32019-12-06 11:34:25 +0900213}
214
215ArcService::~ArcService() {
Garrick Evans664a82f2019-12-17 12:18:05 +0900216 if (impl_) {
Garrick Evans664a82f2019-12-17 12:18:05 +0900217 Stop(impl_->id());
Garrick Evans664a82f2019-12-17 12:18:05 +0900218 }
Garrick Evans54861622019-07-19 09:05:09 +0900219}
Garrick Evans5d55f5e2019-07-17 15:28:10 +0900220
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900221void ArcService::AllocateAddressConfigs() {
222 configs_.clear();
223 // The first usable subnet is the "other" ARC device subnet.
224 // TODO(garrick): This can be removed and ARC_NET will be widened once ARCVM
225 // switches over to use .0/30.
Garrick Evansc7071122020-04-17 12:31:57 +0900226 const bool is_arcvm = IsArcVm();
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900227 AddressManager::Guest alloc =
Garrick Evansc7071122020-04-17 12:31:57 +0900228 is_arcvm ? AddressManager::Guest::ARC : AddressManager::Guest::VM_ARC;
229 // As a temporary workaround, for ARCVM, allocate fixed MAC addresses.
230 uint8_t mac_addr_index = 2;
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900231 // Allocate 2 subnets each for Ethernet and WiFi and 1 for LTE WAN interfaces.
232 for (const auto itype :
233 {InterfaceType::ETHERNET, InterfaceType::ETHERNET, InterfaceType::WIFI,
234 InterfaceType::WIFI, InterfaceType::CELL}) {
235 auto ipv4_subnet = addr_mgr_->AllocateIPv4Subnet(alloc);
236 if (!ipv4_subnet) {
237 LOG(ERROR) << "Subnet already in use or unavailable";
238 continue;
239 }
240 // For here out, use the same slices.
241 alloc = AddressManager::Guest::ARC_NET;
242 auto host_ipv4_addr = ipv4_subnet->AllocateAtOffset(0);
243 if (!host_ipv4_addr) {
244 LOG(ERROR) << "Bridge address already in use or unavailable";
245 continue;
246 }
247 auto guest_ipv4_addr = ipv4_subnet->AllocateAtOffset(1);
248 if (!guest_ipv4_addr) {
249 LOG(ERROR) << "ARC address already in use or unavailable";
250 continue;
251 }
252
Garrick Evansc7071122020-04-17 12:31:57 +0900253 MacAddress mac_addr = is_arcvm
254 ? addr_mgr_->GenerateMacAddress(mac_addr_index++)
255 : addr_mgr_->GenerateMacAddress();
256
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900257 configs_[itype].emplace_back(std::make_unique<Device::Config>(
Garrick Evansc7071122020-04-17 12:31:57 +0900258 mac_addr, std::move(ipv4_subnet), std::move(host_ipv4_addr),
259 std::move(guest_ipv4_addr)));
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900260 }
261}
262
Garrick Evans2961c7c2020-04-03 11:34:40 +0900263std::vector<Device::Config*> ArcService::ReallocateAddressConfigs() {
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900264 std::vector<std::string> existing_devices;
265 for (const auto& d : devices_) {
266 existing_devices.emplace_back(d.first);
267 }
268 for (const auto& d : existing_devices) {
269 RemoveDevice(d);
270 }
271 AllocateAddressConfigs();
Garrick Evans2961c7c2020-04-03 11:34:40 +0900272 std::vector<Device::Config*> configs;
273 if (enable_arcvm_multinet_) {
274 for (const auto& kv : configs_)
275 for (const auto& c : kv.second)
276 configs.push_back(c.get());
277 }
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900278 for (const auto& d : existing_devices) {
279 AddDevice(d);
280 }
Garrick Evans2961c7c2020-04-03 11:34:40 +0900281 return configs;
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900282}
283
284std::unique_ptr<Device::Config> ArcService::AcquireConfig(
285 const std::string& ifname) {
286 auto itype = InterfaceTypeFor(ifname);
287 if (itype == InterfaceType::UNKNOWN) {
288 LOG(ERROR) << "Unsupported interface: " << ifname;
289 return nullptr;
290 }
291
292 auto& configs = configs_[itype];
293 if (configs.empty()) {
294 LOG(ERROR) << "No more addresses available. Cannot make device for "
295 << ifname;
296 return nullptr;
297 }
298 std::unique_ptr<Device::Config> config;
299 config = std::move(configs.front());
300 configs.pop_front();
301 return config;
302}
303
304void ArcService::ReleaseConfig(const std::string& ifname,
305 std::unique_ptr<Device::Config> config) {
306 auto itype = InterfaceTypeFor(ifname);
307 if (itype == InterfaceType::UNKNOWN) {
308 LOG(ERROR) << "Unsupported interface: " << ifname;
309 return;
310 }
311
312 configs_[itype].push_front(std::move(config));
313}
314
Garrick Evans015b0d62020-02-07 09:06:38 +0900315bool ArcService::Start(uint32_t id) {
Garrick Evansf29f5a32019-12-06 11:34:25 +0900316 if (impl_) {
Garrick Evans015b0d62020-02-07 09:06:38 +0900317 uint32_t prev_id;
Garrick Evansf29f5a32019-12-06 11:34:25 +0900318 if (impl_->IsStarted(&prev_id)) {
319 LOG(WARNING) << "Already running - did something crash?"
320 << " Stopping and restarting...";
321 Stop(prev_id);
322 }
Garrick Evansa51d0a12019-11-28 13:51:23 +0900323 }
324
Garrick Evans2961c7c2020-04-03 11:34:40 +0900325 auto configs = ReallocateAddressConfigs();
Garrick Evansf29f5a32019-12-06 11:34:25 +0900326 const auto guest = ArcGuest();
Garrick Evans2961c7c2020-04-03 11:34:40 +0900327 if (guest == GuestMessage::ARC_VM) {
Garrick Evans2e5c9ab2020-03-05 14:33:58 +0900328 impl_ = std::make_unique<VmImpl>(shill_client_, datapath_, addr_mgr_,
Garrick Evans2961c7c2020-04-03 11:34:40 +0900329 forwarder_, configs);
330 } else {
Garrick Evans2e5c9ab2020-03-05 14:33:58 +0900331 impl_ = std::make_unique<ContainerImpl>(datapath_, addr_mgr_, forwarder_,
332 guest);
Garrick Evans2961c7c2020-04-03 11:34:40 +0900333 }
Garrick Evansf29f5a32019-12-06 11:34:25 +0900334 if (!impl_->Start(id)) {
335 impl_.reset();
Garrick Evans508a4bc2019-11-14 08:45:52 +0900336 return false;
Garrick Evansf29f5a32019-12-06 11:34:25 +0900337 }
Garrick Evanscb791e72019-11-11 15:44:34 +0900338
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900339 // Start already known Shill <-> ARC mapped devices.
340 for (const auto& d : devices_)
341 StartDevice(d.second.get());
Garrick Evanscb791e72019-11-11 15:44:34 +0900342
Garrick Evansf29f5a32019-12-06 11:34:25 +0900343 return true;
Garrick Evanscb791e72019-11-11 15:44:34 +0900344}
345
Garrick Evans015b0d62020-02-07 09:06:38 +0900346void ArcService::Stop(uint32_t id) {
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900347 // Stop Shill <-> ARC mapped devices.
348 for (const auto& d : devices_)
349 StopDevice(d.second.get());
Garrick Evansf29f5a32019-12-06 11:34:25 +0900350
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900351 if (impl_) {
352 impl_->Stop(id);
353 impl_.reset();
354 }
Garrick Evans5d55f5e2019-07-17 15:28:10 +0900355}
356
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900357void ArcService::OnDevicesChanged(const std::set<std::string>& added,
358 const std::set<std::string>& removed) {
359 for (const std::string& name : removed)
360 RemoveDevice(name);
361
362 for (const std::string& name : added)
363 AddDevice(name);
364}
365
366void ArcService::AddDevice(const std::string& ifname) {
367 if (ifname.empty())
368 return;
369
370 if (devices_.find(ifname) != devices_.end()) {
371 LOG(DFATAL) << "Attemping to add already tracked device: " << ifname;
372 return;
373 }
374
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900375 auto itype = InterfaceTypeFor(ifname);
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900376 Device::Options opts{
377 .fwd_multicast = IsMulticastInterface(ifname),
378 // TODO(crbug/726815) Also enable |ipv6_enabled| for cellular networks
379 // once IPv6 is enabled on cellular networks in shill.
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900380 .ipv6_enabled =
381 (itype == InterfaceType::ETHERNET || itype == InterfaceType::WIFI),
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900382 };
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900383
384 auto config = AcquireConfig(ifname);
385 if (!config) {
386 LOG(ERROR) << "Cannot add device for " << ifname;
387 return;
388 }
389
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900390 std::string host_ifname = base::StringPrintf("arc_%s", ifname.c_str());
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900391 auto device = std::make_unique<Device>(ifname, host_ifname, ifname,
392 std::move(config), opts);
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900393
394 StartDevice(device.get());
395 devices_.emplace(ifname, std::move(device));
396}
397
398void ArcService::StartDevice(Device* device) {
399 if (!impl_ || !impl_->IsStarted())
400 return;
401
402 // For now, only start devices for ARC++.
403 if (impl_->guest() != GuestMessage::ARC)
404 return;
405
Garrick Evans54861622019-07-19 09:05:09 +0900406 const auto& config = device->config();
407
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900408 LOG(INFO) << "Adding device " << device->phys_ifname()
409 << " bridge: " << device->host_ifname()
410 << " guest_iface: " << device->guest_ifname();
Garrick Evans54861622019-07-19 09:05:09 +0900411
412 // Create the bridge.
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900413 if (!datapath_->AddBridge(device->host_ifname(), config.host_ipv4_addr(),
Garrick Evans7a1a9ee2020-01-28 11:03:57 +0900414 30)) {
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900415 LOG(ERROR) << "Failed to setup arc bridge: " << device->host_ifname();
Garrick Evanse94b6de2020-02-20 09:19:13 +0900416 return;
Garrick Evans54861622019-07-19 09:05:09 +0900417 }
418
Garrick Evanse94b6de2020-02-20 09:19:13 +0900419 // Set up iptables.
420 if (!datapath_->AddInboundIPv4DNAT(
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900421 device->phys_ifname(), IPv4AddressToString(config.guest_ipv4_addr())))
Garrick Evanse94b6de2020-02-20 09:19:13 +0900422 LOG(ERROR) << "Failed to configure ingress traffic rules for "
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900423 << device->phys_ifname();
Garrick Evans54861622019-07-19 09:05:09 +0900424
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900425 if (!datapath_->AddOutboundIPv4(device->host_ifname()))
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900426 LOG(ERROR) << "Failed to configure egress traffic rules for "
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900427 << device->phys_ifname();
Garrick Evans2c263102019-07-26 16:07:18 +0900428
Garrick Evansd90a3822019-11-12 17:53:08 +0900429 if (!impl_->OnStartDevice(device)) {
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900430 LOG(ERROR) << "Failed to start device " << device->phys_ifname();
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900431 }
432}
433
434void ArcService::RemoveDevice(const std::string& ifname) {
435 const auto it = devices_.find(ifname);
436 if (it == devices_.end()) {
437 LOG(WARNING) << "Unknown device: " << ifname;
Garrick Evanscb791e72019-11-11 15:44:34 +0900438 return;
439 }
440
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900441 // If the container is down, this call does nothing.
442 StopDevice(it->second.get());
443
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900444 ReleaseConfig(ifname, it->second->release_config());
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900445 devices_.erase(it);
Garrick Evans54861622019-07-19 09:05:09 +0900446}
447
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900448void ArcService::StopDevice(Device* device) {
449 if (!impl_ || !impl_->IsStarted())
450 return;
451
452 // For now, devices are only started for ARC++.
453 if (impl_->guest() != GuestMessage::ARC)
454 return;
455
456 impl_->OnStopDevice(device);
Garrick Evans54861622019-07-19 09:05:09 +0900457
458 const auto& config = device->config();
459
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900460 LOG(INFO) << "Removing device " << device->phys_ifname()
461 << " bridge: " << device->host_ifname()
462 << " guest_iface: " << device->guest_ifname();
Garrick Evans54861622019-07-19 09:05:09 +0900463
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900464 datapath_->RemoveOutboundIPv4(device->host_ifname());
Garrick Evanse94b6de2020-02-20 09:19:13 +0900465 datapath_->RemoveInboundIPv4DNAT(
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900466 device->phys_ifname(), IPv4AddressToString(config.guest_ipv4_addr()));
Garrick Evans54861622019-07-19 09:05:09 +0900467
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900468 datapath_->RemoveBridge(device->host_ifname());
Garrick Evanscb791e72019-11-11 15:44:34 +0900469}
470
Garrick Evans1b1f67c2020-02-04 16:21:25 +0900471void ArcService::OnDefaultInterfaceChanged(const std::string& new_ifname,
472 const std::string& prev_ifname) {
Garrick Evansf29f5a32019-12-06 11:34:25 +0900473 if (impl_)
Garrick Evans1b1f67c2020-02-04 16:21:25 +0900474 impl_->OnDefaultInterfaceChanged(new_ifname, prev_ifname);
Garrick Evans54861622019-07-19 09:05:09 +0900475}
Garrick Evansba575742019-07-17 15:48:08 +0900476
Garrick Evans38b25a42020-04-06 15:17:42 +0900477std::vector<const Device::Config*> ArcService::GetDeviceConfigs() const {
478 if (impl_)
479 return impl_->GetDeviceConfigs();
Garrick Evanse94b6de2020-02-20 09:19:13 +0900480
Garrick Evans38b25a42020-04-06 15:17:42 +0900481 return {};
Garrick Evanse94b6de2020-02-20 09:19:13 +0900482}
483
Garrick Evansd90a3822019-11-12 17:53:08 +0900484// ARC++ specific functions.
485
Garrick Evans2e5c9ab2020-03-05 14:33:58 +0900486ArcService::ContainerImpl::ContainerImpl(Datapath* datapath,
487 AddressManager* addr_mgr,
488 TrafficForwarder* forwarder,
Garrick Evansd90a3822019-11-12 17:53:08 +0900489 GuestMessage::GuestType guest)
Garrick Evans2e5c9ab2020-03-05 14:33:58 +0900490 : pid_(kInvalidPID),
491 datapath_(datapath),
492 addr_mgr_(addr_mgr),
493 forwarder_(forwarder),
494 guest_(guest) {
Garrick Evans6d227b92019-12-03 16:11:29 +0900495 OneTimeSetup(*datapath_);
Garrick Evansa34b5862019-11-20 09:34:01 +0900496}
Garrick Evansd90a3822019-11-12 17:53:08 +0900497
Garrick Evansb4eb3892019-11-13 12:07:07 +0900498GuestMessage::GuestType ArcService::ContainerImpl::guest() const {
499 return guest_;
500}
501
Garrick Evans015b0d62020-02-07 09:06:38 +0900502uint32_t ArcService::ContainerImpl::id() const {
Garrick Evans664a82f2019-12-17 12:18:05 +0900503 return pid_;
504}
505
Garrick Evans015b0d62020-02-07 09:06:38 +0900506bool ArcService::ContainerImpl::Start(uint32_t pid) {
Garrick Evansa51d0a12019-11-28 13:51:23 +0900507 // This could happen if something crashes and the stop signal is not sent.
508 // It can probably be addressed by stopping and restarting the service.
509 if (pid_ != kInvalidPID)
510 return false;
511
Garrick Evans4dec0c42019-11-29 12:51:57 +0900512 if (pid == kInvalidPID) {
Garrick Evansd90a3822019-11-12 17:53:08 +0900513 LOG(ERROR) << "Cannot start service - invalid container PID";
514 return false;
515 }
Garrick Evans4dec0c42019-11-29 12:51:57 +0900516 pid_ = pid;
Garrick Evansd90a3822019-11-12 17:53:08 +0900517
Garrick Evanse94b6de2020-02-20 09:19:13 +0900518 Device::Options opts{
519 .fwd_multicast = false,
520 .ipv6_enabled = false,
Garrick Evanse94b6de2020-02-20 09:19:13 +0900521 };
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900522 auto config = MakeArcConfig(addr_mgr_, AddressManager::Guest::ARC);
Garrick Evanse94b6de2020-02-20 09:19:13 +0900523
524 // Create the bridge.
525 // Per crbug/1008686 this device cannot be deleted and then re-added.
526 // So instead of removing the bridge when the service stops, bring down the
527 // device instead and re-up it on restart.
528 if (!datapath_->AddBridge(kArcBridge, config->host_ipv4_addr(), 30) &&
529 !datapath_->MaskInterfaceFlags(kArcBridge, IFF_UP)) {
530 LOG(ERROR) << "Failed to bring up arc bridge: " << kArcBridge;
531 return false;
532 }
533
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900534 arc_device_ = std::make_unique<Device>(kArcIfname, kArcBridge, kArcIfname,
535 std::move(config), opts);
Garrick Evanse94b6de2020-02-20 09:19:13 +0900536
537 OnStartDevice(arc_device_.get());
538
Garrick Evansd90a3822019-11-12 17:53:08 +0900539 LOG(INFO) << "ARC++ network service started {pid: " << pid_ << "}";
540 return true;
541}
542
Garrick Evans015b0d62020-02-07 09:06:38 +0900543void ArcService::ContainerImpl::Stop(uint32_t /*pid*/) {
Garrick Evans4dec0c42019-11-29 12:51:57 +0900544 if (!IsStarted())
Taoyu Li1c96d272019-12-13 14:17:43 +0900545 return;
Garrick Evans4dec0c42019-11-29 12:51:57 +0900546
Garrick Evanse94b6de2020-02-20 09:19:13 +0900547 // Per crbug/1008686 this device cannot be deleted and then re-added.
548 // So instead of removing the bridge, bring it down and mark it. This will
549 // allow us to detect if the device is re-added in case of a crash restart
550 // and do the right thing.
551 if (arc_device_) {
552 OnStopDevice(arc_device_.get());
553 if (!datapath_->MaskInterfaceFlags(kArcBridge, IFF_DEBUG, IFF_UP))
554 LOG(ERROR) << "Failed to bring down arc bridge "
555 << "- it may not restart correctly";
556 }
Garrick Evansd90a3822019-11-12 17:53:08 +0900557
558 LOG(INFO) << "ARC++ network service stopped {pid: " << pid_ << "}";
559 pid_ = kInvalidPID;
560}
561
Garrick Evans015b0d62020-02-07 09:06:38 +0900562bool ArcService::ContainerImpl::IsStarted(uint32_t* pid) const {
Garrick Evansa51d0a12019-11-28 13:51:23 +0900563 if (pid)
564 *pid = pid_;
565
Garrick Evansd90a3822019-11-12 17:53:08 +0900566 return pid_ != kInvalidPID;
567}
568
569bool ArcService::ContainerImpl::OnStartDevice(Device* device) {
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900570 LOG(INFO) << "Starting device " << device->phys_ifname()
571 << " bridge: " << device->host_ifname()
572 << " guest_iface: " << device->guest_ifname() << " pid: " << pid_;
Garrick Evansd90a3822019-11-12 17:53:08 +0900573
Garrick Evans2470caa2020-03-04 14:15:41 +0900574 // Set up the virtual pair inside the container namespace.
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900575 const std::string veth_ifname = ArcVethHostName(device->guest_ifname());
Hugo Benichi76675592020-04-08 14:29:57 +0900576 const auto& config = device->config();
577 if (!datapath_->ConnectVethPair(pid_, veth_ifname, device->guest_ifname(),
578 config.mac_addr(), config.guest_ipv4_addr(),
579 30, device->options().fwd_multicast)) {
580 LOG(ERROR) << "Cannot create virtual link for device "
581 << device->phys_ifname();
Garrick Evansd90a3822019-11-12 17:53:08 +0900582 return false;
583 }
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900584 if (!datapath_->AddToBridge(device->host_ifname(), veth_ifname)) {
Garrick Evans2470caa2020-03-04 14:15:41 +0900585 datapath_->RemoveInterface(veth_ifname);
586 LOG(ERROR) << "Failed to bridge interface " << veth_ifname;
587 return false;
588 }
Garrick Evansd90a3822019-11-12 17:53:08 +0900589
Garrick Evans3bd06372020-03-23 10:42:58 +0900590 if (device != arc_device_.get()) {
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900591 forwarder_->StartForwarding(device->phys_ifname(), device->host_ifname(),
592 device->options().ipv6_enabled,
593 device->options().fwd_multicast);
Garrick Evans3bd06372020-03-23 10:42:58 +0900594 } else {
595 // Signal the container that the network device is ready.
596 datapath_->runner().WriteSentinelToContainer(pid_);
597 }
Garrick Evans2e5c9ab2020-03-05 14:33:58 +0900598
Garrick Evansd90a3822019-11-12 17:53:08 +0900599 return true;
600}
601
602void ArcService::ContainerImpl::OnStopDevice(Device* device) {
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900603 LOG(INFO) << "Stopping device " << device->phys_ifname()
604 << " bridge: " << device->host_ifname()
605 << " guest_iface: " << device->guest_ifname() << " pid: " << pid_;
Garrick Evansd90a3822019-11-12 17:53:08 +0900606
Garrick Evans2e5c9ab2020-03-05 14:33:58 +0900607 if (device != arc_device_.get())
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900608 forwarder_->StopForwarding(device->phys_ifname(), device->host_ifname(),
Garrick Evans2e5c9ab2020-03-05 14:33:58 +0900609 device->options().ipv6_enabled,
610 device->options().fwd_multicast);
611
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900612 datapath_->RemoveInterface(ArcVethHostName(device->phys_ifname()));
Garrick Evansd90a3822019-11-12 17:53:08 +0900613}
614
615void ArcService::ContainerImpl::OnDefaultInterfaceChanged(
Garrick Evansb05a7ff2020-02-18 12:59:55 +0900616 const std::string& new_ifname, const std::string& prev_ifname) {}
Garrick Evansd90a3822019-11-12 17:53:08 +0900617
Garrick Evansb4eb3892019-11-13 12:07:07 +0900618// VM specific functions
619
Garrick Evansbbdf4b42020-03-05 12:59:06 +0900620ArcService::VmImpl::VmImpl(ShillClient* shill_client,
Garrick Evans2e5c9ab2020-03-05 14:33:58 +0900621 Datapath* datapath,
622 AddressManager* addr_mgr,
Garrick Evansf5862122020-03-16 09:13:45 +0900623 TrafficForwarder* forwarder,
Garrick Evans2961c7c2020-04-03 11:34:40 +0900624 const std::vector<Device::Config*>& configs)
Garrick Evansbbdf4b42020-03-05 12:59:06 +0900625 : cid_(kInvalidCID),
626 shill_client_(shill_client),
Garrick Evans2e5c9ab2020-03-05 14:33:58 +0900627 datapath_(datapath),
628 addr_mgr_(addr_mgr),
Garrick Evansf5862122020-03-16 09:13:45 +0900629 forwarder_(forwarder),
Garrick Evans2961c7c2020-04-03 11:34:40 +0900630 configs_(configs) {}
Garrick Evansb4eb3892019-11-13 12:07:07 +0900631
632GuestMessage::GuestType ArcService::VmImpl::guest() const {
633 return GuestMessage::ARC_VM;
634}
635
Garrick Evans015b0d62020-02-07 09:06:38 +0900636uint32_t ArcService::VmImpl::id() const {
Garrick Evans664a82f2019-12-17 12:18:05 +0900637 return cid_;
638}
639
Garrick Evans38b25a42020-04-06 15:17:42 +0900640std::vector<const Device::Config*> ArcService::VmImpl::GetDeviceConfigs()
641 const {
642 std::vector<const Device::Config*> configs;
643 for (const auto* c : configs_)
644 configs.emplace_back(c);
645
646 return configs;
647}
648
Garrick Evans015b0d62020-02-07 09:06:38 +0900649bool ArcService::VmImpl::Start(uint32_t cid) {
Garrick Evansa51d0a12019-11-28 13:51:23 +0900650 // This can happen if concierge crashes and doesn't send the vm down RPC.
651 // It can probably be addressed by stopping and restarting the service.
652 if (cid_ != kInvalidCID)
653 return false;
654
Garrick Evans015b0d62020-02-07 09:06:38 +0900655 if (cid == kInvalidCID) {
Garrick Evansb4eb3892019-11-13 12:07:07 +0900656 LOG(ERROR) << "Invalid VM cid " << cid;
657 return false;
658 }
Garrick Evansb4eb3892019-11-13 12:07:07 +0900659 cid_ = cid;
Garrick Evansb4eb3892019-11-13 12:07:07 +0900660
Garrick Evanse94b6de2020-02-20 09:19:13 +0900661 Device::Options opts{
662 .fwd_multicast = true,
663 .ipv6_enabled = true,
Garrick Evanse94b6de2020-02-20 09:19:13 +0900664 };
Garrick Evans2961c7c2020-04-03 11:34:40 +0900665 auto arc_config = MakeArcConfig(addr_mgr_, AddressManager::Guest::VM_ARC);
666 configs_.insert(configs_.begin(), arc_config.get());
Garrick Evanse94b6de2020-02-20 09:19:13 +0900667
Garrick Evans2961c7c2020-04-03 11:34:40 +0900668 // Allocate TAP devices for all configs.
669 for (auto* config : configs_) {
Garrick Evansc7071122020-04-17 12:31:57 +0900670 auto mac = config->mac_addr();
671 auto tap =
672 datapath_->AddTAP("" /* auto-generate name */, &mac,
673 nullptr /* no ipv4 subnet */, vm_tools::kCrosVmUser);
Garrick Evans2961c7c2020-04-03 11:34:40 +0900674 if (tap.empty()) {
675 LOG(ERROR) << "Failed to create TAP device";
676 continue;
677 }
678
679 config->set_tap_ifname(tap);
Garrick Evanse94b6de2020-02-20 09:19:13 +0900680 }
681
Garrick Evans2961c7c2020-04-03 11:34:40 +0900682 arc_device_ = std::make_unique<Device>(
683 kArcVmIfname, kArcVmBridge, kArcVmIfname, std::move(arc_config), opts);
684 // Create the bridge.
685 if (!datapath_->AddBridge(kArcVmBridge,
686 arc_device_->config().host_ipv4_addr(), 30)) {
687 LOG(ERROR) << "Failed to setup arc bridge for device " << *arc_device_;
688 return false;
689 }
Garrick Evanse94b6de2020-02-20 09:19:13 +0900690 OnStartDevice(arc_device_.get());
691
692 LOG(INFO) << "ARCVM network service started {cid: " << cid_ << "}";
Garrick Evansb4eb3892019-11-13 12:07:07 +0900693 return true;
694}
695
Garrick Evans015b0d62020-02-07 09:06:38 +0900696void ArcService::VmImpl::Stop(uint32_t cid) {
Garrick Evans21173b12019-11-20 15:23:16 +0900697 if (cid_ != cid) {
698 LOG(ERROR) << "Mismatched ARCVM CIDs " << cid_ << " != " << cid;
699 return;
700 }
701
Garrick Evans2961c7c2020-04-03 11:34:40 +0900702 for (auto* config : configs_) {
703 const auto& tap = config->tap_ifname();
704 if (!tap.empty()) {
705 datapath_->RemoveInterface(tap);
706 config->set_tap_ifname("");
707 }
708 }
709
Garrick Evanse94b6de2020-02-20 09:19:13 +0900710 OnStopDevice(arc_device_.get());
711 datapath_->RemoveBridge(kArcVmBridge);
712 arc_device_.reset();
713
Garrick Evansb4eb3892019-11-13 12:07:07 +0900714 LOG(INFO) << "ARCVM network service stopped {cid: " << cid_ << "}";
715 cid_ = kInvalidCID;
716}
717
Garrick Evans015b0d62020-02-07 09:06:38 +0900718bool ArcService::VmImpl::IsStarted(uint32_t* cid) const {
Garrick Evansa51d0a12019-11-28 13:51:23 +0900719 if (cid)
720 *cid = cid_;
721
Garrick Evans015b0d62020-02-07 09:06:38 +0900722 return cid_ != kInvalidCID;
Garrick Evansb4eb3892019-11-13 12:07:07 +0900723}
724
725bool ArcService::VmImpl::OnStartDevice(Device* device) {
Garrick Evansf5862122020-03-16 09:13:45 +0900726 // TODO(garrick): Remove once ARCVM P is gone.
Garrick Evans2961c7c2020-04-03 11:34:40 +0900727 if (device == arc_device_.get() && !IsMultinetEnabled())
Garrick Evansf5862122020-03-16 09:13:45 +0900728 return OnStartArcPDevice();
Garrick Evansb4eb3892019-11-13 12:07:07 +0900729
Garrick Evans2961c7c2020-04-03 11:34:40 +0900730 std::string tap;
731 for (auto* config : configs_) {
732 if (config == &device->config()) {
733 tap = config->tap_ifname();
734 break;
735 }
736 }
Garrick Evansb4eb3892019-11-13 12:07:07 +0900737 if (tap.empty()) {
Garrick Evans2961c7c2020-04-03 11:34:40 +0900738 LOG(ERROR) << "No TAP device for: " << *device;
Garrick Evansb4eb3892019-11-13 12:07:07 +0900739 return false;
740 }
741
Garrick Evans2961c7c2020-04-03 11:34:40 +0900742 LOG(INFO) << "Starting device " << *device << " cid: " << cid_;
743
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900744 if (!datapath_->AddToBridge(device->host_ifname(), tap)) {
Garrick Evansb4eb3892019-11-13 12:07:07 +0900745 LOG(ERROR) << "Failed to bridge TAP device " << tap;
746 datapath_->RemoveInterface(tap);
747 return false;
748 }
749
Garrick Evansf5862122020-03-16 09:13:45 +0900750 if (device != arc_device_.get())
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900751 forwarder_->StartForwarding(device->phys_ifname(), device->host_ifname(),
752 device->options().ipv6_enabled,
753 device->options().fwd_multicast);
Garrick Evansf5862122020-03-16 09:13:45 +0900754
755 return true;
756}
757
758bool ArcService::VmImpl::OnStartArcPDevice() {
Garrick Evans2961c7c2020-04-03 11:34:40 +0900759 LOG(INFO) << "Starting device " << *arc_device_ << " cid: " << cid_;
Garrick Evansf5862122020-03-16 09:13:45 +0900760
Garrick Evans2961c7c2020-04-03 11:34:40 +0900761 if (!datapath_->AddToBridge(kArcVmBridge,
762 arc_device_->config().tap_ifname())) {
763 LOG(ERROR) << "Failed to bridge TAP device " << *arc_device_;
Garrick Evansf5862122020-03-16 09:13:45 +0900764 return false;
765 }
766
Garrick Evansf5862122020-03-16 09:13:45 +0900767 // Setup the iptables.
768 if (!datapath_->AddLegacyIPv4DNAT(
769 IPv4AddressToString(arc_device_->config().guest_ipv4_addr())))
770 LOG(ERROR) << "Failed to configure ARC traffic rules";
771
772 if (!datapath_->AddOutboundIPv4(kArcVmBridge))
773 LOG(ERROR) << "Failed to configure egress traffic rules";
774
Garrick Evansbbdf4b42020-03-05 12:59:06 +0900775 OnDefaultInterfaceChanged(shill_client_->default_interface(),
776 "" /*previous*/);
Garrick Evansbcce09e2020-03-10 15:08:04 +0900777
Garrick Evansb4eb3892019-11-13 12:07:07 +0900778 return true;
779}
780
781void ArcService::VmImpl::OnStopDevice(Device* device) {
Garrick Evansf5862122020-03-16 09:13:45 +0900782 // TODO(garrick): Remove once ARCVM P is gone.
Garrick Evans2961c7c2020-04-03 11:34:40 +0900783 if (device == arc_device_.get() && !IsMultinetEnabled())
Garrick Evansf5862122020-03-16 09:13:45 +0900784 return OnStopArcPDevice();
Garrick Evansb4eb3892019-11-13 12:07:07 +0900785
Garrick Evans2961c7c2020-04-03 11:34:40 +0900786 LOG(INFO) << "Stopping device " << *device << " cid: " << cid_;
Garrick Evansb4eb3892019-11-13 12:07:07 +0900787
Garrick Evansf5862122020-03-16 09:13:45 +0900788 if (device != arc_device_.get())
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900789 forwarder_->StopForwarding(device->phys_ifname(), device->host_ifname(),
Garrick Evansf5862122020-03-16 09:13:45 +0900790 device->options().ipv6_enabled,
791 device->options().fwd_multicast);
Garrick Evansbcce09e2020-03-10 15:08:04 +0900792
Garrick Evans2961c7c2020-04-03 11:34:40 +0900793 for (auto* config : configs_) {
794 if (config == &device->config()) {
795 config->set_tap_ifname("");
796 break;
797 }
798 }
Garrick Evansb4eb3892019-11-13 12:07:07 +0900799}
800
Garrick Evansf5862122020-03-16 09:13:45 +0900801void ArcService::VmImpl::OnStopArcPDevice() {
Garrick Evans2961c7c2020-04-03 11:34:40 +0900802 LOG(INFO) << "Stopping device " << *arc_device_.get() << " cid: " << cid_;
Garrick Evansf5862122020-03-16 09:13:45 +0900803
804 datapath_->RemoveOutboundIPv4(kArcVmBridge);
805 datapath_->RemoveLegacyIPv4DNAT();
806
807 OnDefaultInterfaceChanged("" /*new_ifname*/,
808 shill_client_->default_interface());
809
Garrick Evans2961c7c2020-04-03 11:34:40 +0900810 arc_device_->config().set_tap_ifname("");
Garrick Evansf5862122020-03-16 09:13:45 +0900811}
812
Garrick Evans1b1f67c2020-02-04 16:21:25 +0900813void ArcService::VmImpl::OnDefaultInterfaceChanged(
814 const std::string& new_ifname, const std::string& prev_ifname) {
Garrick Evans2961c7c2020-04-03 11:34:40 +0900815 if (!IsStarted() || IsMultinetEnabled())
Garrick Evansb4eb3892019-11-13 12:07:07 +0900816 return;
817
Garrick Evans2e5c9ab2020-03-05 14:33:58 +0900818 forwarder_->StopForwarding(prev_ifname, kArcVmBridge, true /*ipv6*/,
819 true /*multicast*/);
820
Garrick Evansb4eb3892019-11-13 12:07:07 +0900821 datapath_->RemoveLegacyIPv4InboundDNAT();
Garrick Evansb4eb3892019-11-13 12:07:07 +0900822
823 // If a new default interface was given, then re-enable with that.
Garrick Evans1b1f67c2020-02-04 16:21:25 +0900824 if (!new_ifname.empty()) {
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900825 datapath_->AddLegacyIPv4InboundDNAT(new_ifname);
Jason Jeremy Iman0e9f8262020-03-06 14:50:49 +0900826 forwarder_->StartForwarding(new_ifname, kArcVmBridge, true /*ipv6*/,
827 true /*multicast*/);
Garrick Evansb4eb3892019-11-13 12:07:07 +0900828 }
829}
830
Garrick Evans2961c7c2020-04-03 11:34:40 +0900831bool ArcService::VmImpl::IsMultinetEnabled() const {
832 return configs_.size() > 1;
833}
834
Garrick Evans3388a032020-03-24 11:25:55 +0900835} // namespace patchpanel