blob: 1de4cebb95f1fc4c7847366011ad6732e03d17ec [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
5#include "arc/network/arc_service.h"
6
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
24#include "arc/network/datapath.h"
Garrick Evans54861622019-07-19 09:05:09 +090025#include "arc/network/mac_address_generator.h"
Jason Jeremy Imanf4156cb2019-11-14 15:36:22 +090026#include "arc/network/manager.h"
Garrick Evans3915af32019-07-25 15:44:34 +090027#include "arc/network/minijailed_process_runner.h"
Garrick Evans54861622019-07-19 09:05:09 +090028#include "arc/network/net_util.h"
29#include "arc/network/scoped_ns.h"
Garrick Evans5d55f5e2019-07-17 15:28:10 +090030
31namespace arc_networkd {
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 Evansb4eb3892019-11-13 12:07:07 +090038constexpr pid_t kTestPID = -2;
Garrick Evans015b0d62020-02-07 09:06:38 +090039constexpr uint32_t kInvalidCID = 0;
Garrick Evanse94b6de2020-02-20 09:19:13 +090040constexpr char kArcIfname[] = "arc0";
41constexpr char kArcBridge[] = "arcbr0";
42constexpr char kArcVmIfname[] = "arc1";
43constexpr char kArcVmBridge[] = "arc_br1";
Garrick Evans6e4eb3b2020-03-09 07:18:31 +090044constexpr std::array<const char*, 2> kEthernetInterfacePrefixes{{"eth", "usb"}};
45constexpr std::array<const char*, 2> kWifiInterfacePrefixes{{"wlan", "mlan"}};
Garrick Evans86c7d9c2020-03-17 09:25:48 +090046constexpr std::array<const char*, 2> kCellInterfacePrefixes{{"wwan", "rmnet"}};
Garrick Evans54861622019-07-19 09:05:09 +090047
Garrick Evans6d227b92019-12-03 16:11:29 +090048void OneTimeSetup(const Datapath& datapath) {
Garrick Evansa34b5862019-11-20 09:34:01 +090049 static bool done = false;
50 if (done)
51 return;
52
Garrick Evans6d227b92019-12-03 16:11:29 +090053 auto& runner = datapath.runner();
54
55 // Load networking modules needed by Android that are not compiled in the
56 // kernel. Android does not allow auto-loading of kernel modules.
Garrick Evansa34b5862019-11-20 09:34:01 +090057 // These must succeed.
Garrick Evans8e8e3472020-01-23 14:03:50 +090058 if (runner.modprobe_all({
Garrick Evansa34b5862019-11-20 09:34:01 +090059 // The netfilter modules needed by netd for iptables commands.
60 "ip6table_filter",
61 "ip6t_ipv6header",
62 "ip6t_REJECT",
63 // The xfrm modules needed for Android's ipsec APIs.
64 "xfrm4_mode_transport",
65 "xfrm4_mode_tunnel",
66 "xfrm6_mode_transport",
67 "xfrm6_mode_tunnel",
68 // The ipsec modules for AH and ESP encryption for ipv6.
69 "ah6",
70 "esp6",
71 }) != 0) {
72 LOG(ERROR) << "One or more required kernel modules failed to load."
73 << " Some Android functionality may be broken.";
74 }
75 // Optional modules.
Garrick Evans8e8e3472020-01-23 14:03:50 +090076 if (runner.modprobe_all({
Garrick Evansa34b5862019-11-20 09:34:01 +090077 // This module is not available in kernels < 3.18
78 "nf_reject_ipv6",
79 // These modules are needed for supporting Chrome traffic on Android
80 // VPN which uses Android's NAT feature. Android NAT sets up
81 // iptables
82 // rules that use these conntrack modules for FTP/TFTP.
83 "nf_nat_ftp",
84 "nf_nat_tftp",
Hugo Benichia0cde9e2019-12-16 11:57:20 +090085 // The tun module is needed by the Android 464xlat clatd process.
86 "tun",
Garrick Evansa34b5862019-11-20 09:34:01 +090087 }) != 0) {
88 LOG(WARNING) << "One or more optional kernel modules failed to load.";
89 }
90
Garrick Evans6d227b92019-12-03 16:11:29 +090091 // This is only needed for CTS (b/27932574).
Garrick Evans8e8e3472020-01-23 14:03:50 +090092 if (runner.chown("655360", "655360", "/sys/class/xt_idletimer") != 0) {
Garrick Evans6d227b92019-12-03 16:11:29 +090093 LOG(ERROR) << "Failed to change ownership of xt_idletimer.";
94 }
95
Garrick Evansa34b5862019-11-20 09:34:01 +090096 done = true;
97}
98
Garrick Evans508a4bc2019-11-14 08:45:52 +090099bool IsArcVm() {
100 const base::FilePath path("/run/chrome/is_arcvm");
101 std::string contents;
102 if (!base::ReadFileToString(path, &contents)) {
103 PLOG(ERROR) << "Could not read " << path.value();
104 }
105 return contents == "1";
106}
107
Garrick Evansf29f5a32019-12-06 11:34:25 +0900108GuestMessage::GuestType ArcGuest() {
109 if (test::guest != GuestMessage::UNKNOWN_GUEST)
110 return test::guest;
Garrick Evans508a4bc2019-11-14 08:45:52 +0900111
Garrick Evansb05a7ff2020-02-18 12:59:55 +0900112 return IsArcVm() ? GuestMessage::ARC_VM : GuestMessage::ARC;
Garrick Evans508a4bc2019-11-14 08:45:52 +0900113}
114
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900115ArcService::InterfaceType InterfaceTypeFor(const std::string& ifname) {
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900116 for (const auto& prefix : kEthernetInterfacePrefixes) {
117 if (base::StartsWith(ifname, prefix,
118 base::CompareCase::INSENSITIVE_ASCII)) {
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900119 return ArcService::InterfaceType::ETHERNET;
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900120 }
121 }
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900122 for (const auto& prefix : kWifiInterfacePrefixes) {
123 if (base::StartsWith(ifname, prefix,
124 base::CompareCase::INSENSITIVE_ASCII)) {
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900125 return ArcService::InterfaceType::WIFI;
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900126 }
127 }
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900128 for (const auto& prefix : kCellInterfacePrefixes) {
129 if (base::StartsWith(ifname, prefix,
130 base::CompareCase::INSENSITIVE_ASCII)) {
131 return ArcService::InterfaceType::CELL;
132 }
133 }
134 return ArcService::InterfaceType::UNKNOWN;
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900135}
136
137bool IsMulticastInterface(const std::string& ifname) {
138 if (ifname.empty()) {
139 return false;
140 }
141
142 int fd = socket(AF_INET, SOCK_DGRAM, 0);
143 if (fd < 0) {
144 // If IPv4 fails, try to open a socket using IPv6.
145 fd = socket(AF_INET6, SOCK_DGRAM, 0);
146 if (fd < 0) {
147 LOG(ERROR) << "Unable to create socket";
148 return false;
149 }
150 }
151
152 struct ifreq ifr;
153 memset(&ifr, 0, sizeof(ifr));
154 strncpy(ifr.ifr_name, ifname.c_str(), IFNAMSIZ);
155 if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0) {
156 PLOG(ERROR) << "SIOCGIFFLAGS failed for " << ifname;
157 close(fd);
158 return false;
159 }
160
161 close(fd);
162 return (ifr.ifr_flags & IFF_MULTICAST);
163}
164
Garrick Evanse94b6de2020-02-20 09:19:13 +0900165// Returns the configuration for the ARC management interface used for VPN
166// forwarding, ADB-over-TCP and single-networked ARCVM.
167std::unique_ptr<Device::Config> MakeArcConfig(AddressManager* addr_mgr,
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900168 AddressManager::Guest guest) {
169 auto ipv4_subnet = addr_mgr->AllocateIPv4Subnet(guest);
Garrick Evanse94b6de2020-02-20 09:19:13 +0900170 if (!ipv4_subnet) {
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900171 LOG(ERROR) << "Subnet already in use or unavailable";
Garrick Evanse94b6de2020-02-20 09:19:13 +0900172 return nullptr;
173 }
174 auto host_ipv4_addr = ipv4_subnet->AllocateAtOffset(0);
175 if (!host_ipv4_addr) {
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900176 LOG(ERROR) << "Bridge address already in use or unavailable";
Garrick Evanse94b6de2020-02-20 09:19:13 +0900177 return nullptr;
178 }
179 auto guest_ipv4_addr = ipv4_subnet->AllocateAtOffset(1);
180 if (!guest_ipv4_addr) {
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900181 LOG(ERROR) << "ARC address already in use or unavailable";
Garrick Evanse94b6de2020-02-20 09:19:13 +0900182 return nullptr;
183 }
184
185 return std::make_unique<Device::Config>(
Garrick Evanse94b6de2020-02-20 09:19:13 +0900186 addr_mgr->GenerateMacAddress(), std::move(ipv4_subnet),
187 std::move(host_ipv4_addr), std::move(guest_ipv4_addr));
188}
189
Garrick Evans5d55f5e2019-07-17 15:28:10 +0900190} // namespace
191
Garrick Evans69b85872020-02-04 11:40:26 +0900192ArcService::ArcService(ShillClient* shill_client,
Garrick Evans2e5c9ab2020-03-05 14:33:58 +0900193 Datapath* datapath,
194 AddressManager* addr_mgr,
Garrick Evansf5862122020-03-16 09:13:45 +0900195 TrafficForwarder* forwarder,
196 bool enable_arcvm_multinet)
Garrick Evans2e5c9ab2020-03-05 14:33:58 +0900197 : shill_client_(shill_client),
Garrick Evans2e5c9ab2020-03-05 14:33:58 +0900198 datapath_(datapath),
199 addr_mgr_(addr_mgr),
Garrick Evansf5862122020-03-16 09:13:45 +0900200 forwarder_(forwarder),
201 enable_arcvm_multinet_(enable_arcvm_multinet) {
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900202 AllocateAddressConfigs();
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900203 shill_client_->RegisterDevicesChangedHandler(
204 base::Bind(&ArcService::OnDevicesChanged, weak_factory_.GetWeakPtr()));
205 shill_client_->ScanDevices(
206 base::Bind(&ArcService::OnDevicesChanged, weak_factory_.GetWeakPtr()));
Garrick Evansbbdf4b42020-03-05 12:59:06 +0900207 shill_client_->RegisterDefaultInterfaceChangedHandler(base::Bind(
208 &ArcService::OnDefaultInterfaceChanged, weak_factory_.GetWeakPtr()));
Garrick Evansf29f5a32019-12-06 11:34:25 +0900209}
210
211ArcService::~ArcService() {
Garrick Evans664a82f2019-12-17 12:18:05 +0900212 if (impl_) {
Garrick Evans664a82f2019-12-17 12:18:05 +0900213 Stop(impl_->id());
Garrick Evans664a82f2019-12-17 12:18:05 +0900214 }
Garrick Evans54861622019-07-19 09:05:09 +0900215}
Garrick Evans5d55f5e2019-07-17 15:28:10 +0900216
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900217void ArcService::AllocateAddressConfigs() {
218 configs_.clear();
219 // The first usable subnet is the "other" ARC device subnet.
220 // TODO(garrick): This can be removed and ARC_NET will be widened once ARCVM
221 // switches over to use .0/30.
222 AddressManager::Guest alloc =
223 IsArcVm() ? AddressManager::Guest::ARC : AddressManager::Guest::VM_ARC;
224 // Allocate 2 subnets each for Ethernet and WiFi and 1 for LTE WAN interfaces.
225 for (const auto itype :
226 {InterfaceType::ETHERNET, InterfaceType::ETHERNET, InterfaceType::WIFI,
227 InterfaceType::WIFI, InterfaceType::CELL}) {
228 auto ipv4_subnet = addr_mgr_->AllocateIPv4Subnet(alloc);
229 if (!ipv4_subnet) {
230 LOG(ERROR) << "Subnet already in use or unavailable";
231 continue;
232 }
233 // For here out, use the same slices.
234 alloc = AddressManager::Guest::ARC_NET;
235 auto host_ipv4_addr = ipv4_subnet->AllocateAtOffset(0);
236 if (!host_ipv4_addr) {
237 LOG(ERROR) << "Bridge address already in use or unavailable";
238 continue;
239 }
240 auto guest_ipv4_addr = ipv4_subnet->AllocateAtOffset(1);
241 if (!guest_ipv4_addr) {
242 LOG(ERROR) << "ARC address already in use or unavailable";
243 continue;
244 }
245
246 configs_[itype].emplace_back(std::make_unique<Device::Config>(
247 addr_mgr_->GenerateMacAddress(), std::move(ipv4_subnet),
248 std::move(host_ipv4_addr), std::move(guest_ipv4_addr)));
249 }
250}
251
252void ArcService::ReallocateAddressConfigs() {
253 std::vector<std::string> existing_devices;
254 for (const auto& d : devices_) {
255 existing_devices.emplace_back(d.first);
256 }
257 for (const auto& d : existing_devices) {
258 RemoveDevice(d);
259 }
260 AllocateAddressConfigs();
261 for (const auto& d : existing_devices) {
262 AddDevice(d);
263 }
264}
265
266std::unique_ptr<Device::Config> ArcService::AcquireConfig(
267 const std::string& ifname) {
268 auto itype = InterfaceTypeFor(ifname);
269 if (itype == InterfaceType::UNKNOWN) {
270 LOG(ERROR) << "Unsupported interface: " << ifname;
271 return nullptr;
272 }
273
274 auto& configs = configs_[itype];
275 if (configs.empty()) {
276 LOG(ERROR) << "No more addresses available. Cannot make device for "
277 << ifname;
278 return nullptr;
279 }
280 std::unique_ptr<Device::Config> config;
281 config = std::move(configs.front());
282 configs.pop_front();
283 return config;
284}
285
286void ArcService::ReleaseConfig(const std::string& ifname,
287 std::unique_ptr<Device::Config> config) {
288 auto itype = InterfaceTypeFor(ifname);
289 if (itype == InterfaceType::UNKNOWN) {
290 LOG(ERROR) << "Unsupported interface: " << ifname;
291 return;
292 }
293
294 configs_[itype].push_front(std::move(config));
295}
296
Garrick Evans015b0d62020-02-07 09:06:38 +0900297bool ArcService::Start(uint32_t id) {
Garrick Evansf29f5a32019-12-06 11:34:25 +0900298 if (impl_) {
Garrick Evans015b0d62020-02-07 09:06:38 +0900299 uint32_t prev_id;
Garrick Evansf29f5a32019-12-06 11:34:25 +0900300 if (impl_->IsStarted(&prev_id)) {
301 LOG(WARNING) << "Already running - did something crash?"
302 << " Stopping and restarting...";
303 Stop(prev_id);
304 }
Garrick Evansa51d0a12019-11-28 13:51:23 +0900305 }
306
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900307 ReallocateAddressConfigs();
308
Garrick Evansf29f5a32019-12-06 11:34:25 +0900309 const auto guest = ArcGuest();
310 if (guest == GuestMessage::ARC_VM)
Garrick Evans2e5c9ab2020-03-05 14:33:58 +0900311 impl_ = std::make_unique<VmImpl>(shill_client_, datapath_, addr_mgr_,
Garrick Evansf5862122020-03-16 09:13:45 +0900312 forwarder_, enable_arcvm_multinet_);
Garrick Evansf29f5a32019-12-06 11:34:25 +0900313 else
Garrick Evans2e5c9ab2020-03-05 14:33:58 +0900314 impl_ = std::make_unique<ContainerImpl>(datapath_, addr_mgr_, forwarder_,
315 guest);
Garrick Evansf29f5a32019-12-06 11:34:25 +0900316
317 if (!impl_->Start(id)) {
318 impl_.reset();
Garrick Evans508a4bc2019-11-14 08:45:52 +0900319 return false;
Garrick Evansf29f5a32019-12-06 11:34:25 +0900320 }
Garrick Evanscb791e72019-11-11 15:44:34 +0900321
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900322 // Start already known Shill <-> ARC mapped devices.
323 for (const auto& d : devices_)
324 StartDevice(d.second.get());
Garrick Evanscb791e72019-11-11 15:44:34 +0900325
Garrick Evansf29f5a32019-12-06 11:34:25 +0900326 return true;
Garrick Evanscb791e72019-11-11 15:44:34 +0900327}
328
Garrick Evans015b0d62020-02-07 09:06:38 +0900329void ArcService::Stop(uint32_t id) {
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900330 // Stop Shill <-> ARC mapped devices.
331 for (const auto& d : devices_)
332 StopDevice(d.second.get());
Garrick Evansf29f5a32019-12-06 11:34:25 +0900333
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900334 if (impl_) {
335 impl_->Stop(id);
336 impl_.reset();
337 }
Garrick Evans5d55f5e2019-07-17 15:28:10 +0900338}
339
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900340void ArcService::OnDevicesChanged(const std::set<std::string>& added,
341 const std::set<std::string>& removed) {
342 for (const std::string& name : removed)
343 RemoveDevice(name);
344
345 for (const std::string& name : added)
346 AddDevice(name);
347}
348
349void ArcService::AddDevice(const std::string& ifname) {
350 if (ifname.empty())
351 return;
352
353 if (devices_.find(ifname) != devices_.end()) {
354 LOG(DFATAL) << "Attemping to add already tracked device: " << ifname;
355 return;
356 }
357
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900358 auto itype = InterfaceTypeFor(ifname);
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900359 Device::Options opts{
360 .fwd_multicast = IsMulticastInterface(ifname),
361 // TODO(crbug/726815) Also enable |ipv6_enabled| for cellular networks
362 // once IPv6 is enabled on cellular networks in shill.
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900363 .ipv6_enabled =
364 (itype == InterfaceType::ETHERNET || itype == InterfaceType::WIFI),
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900365 .use_default_interface = false,
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900366 };
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900367
368 auto config = AcquireConfig(ifname);
369 if (!config) {
370 LOG(ERROR) << "Cannot add device for " << ifname;
371 return;
372 }
373
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900374 std::string host_ifname = base::StringPrintf("arc_%s", ifname.c_str());
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900375 auto device = std::make_unique<Device>(ifname, host_ifname, ifname,
376 std::move(config), opts);
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900377
378 StartDevice(device.get());
379 devices_.emplace(ifname, std::move(device));
380}
381
382void ArcService::StartDevice(Device* device) {
383 if (!impl_ || !impl_->IsStarted())
384 return;
385
386 // For now, only start devices for ARC++.
387 if (impl_->guest() != GuestMessage::ARC)
388 return;
389
Garrick Evans54861622019-07-19 09:05:09 +0900390 const auto& config = device->config();
391
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900392 LOG(INFO) << "Adding device " << device->phys_ifname()
393 << " bridge: " << device->host_ifname()
394 << " guest_iface: " << device->guest_ifname();
Garrick Evans54861622019-07-19 09:05:09 +0900395
396 // Create the bridge.
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900397 if (!datapath_->AddBridge(device->host_ifname(), config.host_ipv4_addr(),
Garrick Evans7a1a9ee2020-01-28 11:03:57 +0900398 30)) {
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900399 LOG(ERROR) << "Failed to setup arc bridge: " << device->host_ifname();
Garrick Evanse94b6de2020-02-20 09:19:13 +0900400 return;
Garrick Evans54861622019-07-19 09:05:09 +0900401 }
402
Garrick Evanse94b6de2020-02-20 09:19:13 +0900403 // Set up iptables.
404 if (!datapath_->AddInboundIPv4DNAT(
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900405 device->phys_ifname(), IPv4AddressToString(config.guest_ipv4_addr())))
Garrick Evanse94b6de2020-02-20 09:19:13 +0900406 LOG(ERROR) << "Failed to configure ingress traffic rules for "
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900407 << device->phys_ifname();
Garrick Evans54861622019-07-19 09:05:09 +0900408
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900409 if (!datapath_->AddOutboundIPv4(device->host_ifname()))
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900410 LOG(ERROR) << "Failed to configure egress traffic rules for "
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900411 << device->phys_ifname();
Garrick Evans2c263102019-07-26 16:07:18 +0900412
Garrick Evansd90a3822019-11-12 17:53:08 +0900413 if (!impl_->OnStartDevice(device)) {
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900414 LOG(ERROR) << "Failed to start device " << device->phys_ifname();
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900415 }
416}
417
418void ArcService::RemoveDevice(const std::string& ifname) {
419 const auto it = devices_.find(ifname);
420 if (it == devices_.end()) {
421 LOG(WARNING) << "Unknown device: " << ifname;
Garrick Evanscb791e72019-11-11 15:44:34 +0900422 return;
423 }
424
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900425 // If the container is down, this call does nothing.
426 StopDevice(it->second.get());
427
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900428 ReleaseConfig(ifname, it->second->release_config());
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900429 devices_.erase(it);
Garrick Evans54861622019-07-19 09:05:09 +0900430}
431
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900432void ArcService::StopDevice(Device* device) {
433 if (!impl_ || !impl_->IsStarted())
434 return;
435
436 // For now, devices are only started for ARC++.
437 if (impl_->guest() != GuestMessage::ARC)
438 return;
439
440 impl_->OnStopDevice(device);
Garrick Evans54861622019-07-19 09:05:09 +0900441
442 const auto& config = device->config();
443
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900444 LOG(INFO) << "Removing device " << device->phys_ifname()
445 << " bridge: " << device->host_ifname()
446 << " guest_iface: " << device->guest_ifname();
Garrick Evans54861622019-07-19 09:05:09 +0900447
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900448 datapath_->RemoveOutboundIPv4(device->host_ifname());
Garrick Evanse94b6de2020-02-20 09:19:13 +0900449 datapath_->RemoveInboundIPv4DNAT(
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900450 device->phys_ifname(), IPv4AddressToString(config.guest_ipv4_addr()));
Garrick Evans54861622019-07-19 09:05:09 +0900451
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900452 datapath_->RemoveBridge(device->host_ifname());
Garrick Evanscb791e72019-11-11 15:44:34 +0900453}
454
Garrick Evans1b1f67c2020-02-04 16:21:25 +0900455void ArcService::OnDefaultInterfaceChanged(const std::string& new_ifname,
456 const std::string& prev_ifname) {
Garrick Evansf29f5a32019-12-06 11:34:25 +0900457 if (impl_)
Garrick Evans1b1f67c2020-02-04 16:21:25 +0900458 impl_->OnDefaultInterfaceChanged(new_ifname, prev_ifname);
Garrick Evans54861622019-07-19 09:05:09 +0900459}
Garrick Evansba575742019-07-17 15:48:08 +0900460
Garrick Evanse94b6de2020-02-20 09:19:13 +0900461Device* ArcService::ArcDevice() const {
462 if (!impl_)
463 return nullptr;
464
465 return impl_->ArcDevice();
466}
467
Garrick Evansd90a3822019-11-12 17:53:08 +0900468// ARC++ specific functions.
469
Garrick Evans2e5c9ab2020-03-05 14:33:58 +0900470ArcService::ContainerImpl::ContainerImpl(Datapath* datapath,
471 AddressManager* addr_mgr,
472 TrafficForwarder* forwarder,
Garrick Evansd90a3822019-11-12 17:53:08 +0900473 GuestMessage::GuestType guest)
Garrick Evans2e5c9ab2020-03-05 14:33:58 +0900474 : pid_(kInvalidPID),
475 datapath_(datapath),
476 addr_mgr_(addr_mgr),
477 forwarder_(forwarder),
478 guest_(guest) {
Garrick Evans6d227b92019-12-03 16:11:29 +0900479 OneTimeSetup(*datapath_);
Garrick Evansa34b5862019-11-20 09:34:01 +0900480}
Garrick Evansd90a3822019-11-12 17:53:08 +0900481
Garrick Evansb4eb3892019-11-13 12:07:07 +0900482GuestMessage::GuestType ArcService::ContainerImpl::guest() const {
483 return guest_;
484}
485
Garrick Evans015b0d62020-02-07 09:06:38 +0900486uint32_t ArcService::ContainerImpl::id() const {
Garrick Evans664a82f2019-12-17 12:18:05 +0900487 return pid_;
488}
489
Garrick Evans015b0d62020-02-07 09:06:38 +0900490bool ArcService::ContainerImpl::Start(uint32_t pid) {
Garrick Evansa51d0a12019-11-28 13:51:23 +0900491 // This could happen if something crashes and the stop signal is not sent.
492 // It can probably be addressed by stopping and restarting the service.
493 if (pid_ != kInvalidPID)
494 return false;
495
Garrick Evans4dec0c42019-11-29 12:51:57 +0900496 if (pid == kInvalidPID) {
Garrick Evansd90a3822019-11-12 17:53:08 +0900497 LOG(ERROR) << "Cannot start service - invalid container PID";
498 return false;
499 }
Garrick Evans4dec0c42019-11-29 12:51:57 +0900500 pid_ = pid;
Garrick Evansd90a3822019-11-12 17:53:08 +0900501
Garrick Evanse94b6de2020-02-20 09:19:13 +0900502 Device::Options opts{
503 .fwd_multicast = false,
504 .ipv6_enabled = false,
505 .use_default_interface = false,
Garrick Evanse94b6de2020-02-20 09:19:13 +0900506 };
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900507 auto config = MakeArcConfig(addr_mgr_, AddressManager::Guest::ARC);
Garrick Evanse94b6de2020-02-20 09:19:13 +0900508
509 // Create the bridge.
510 // Per crbug/1008686 this device cannot be deleted and then re-added.
511 // So instead of removing the bridge when the service stops, bring down the
512 // device instead and re-up it on restart.
513 if (!datapath_->AddBridge(kArcBridge, config->host_ipv4_addr(), 30) &&
514 !datapath_->MaskInterfaceFlags(kArcBridge, IFF_UP)) {
515 LOG(ERROR) << "Failed to bring up arc bridge: " << kArcBridge;
516 return false;
517 }
518
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900519 arc_device_ = std::make_unique<Device>(kArcIfname, kArcBridge, kArcIfname,
520 std::move(config), opts);
Garrick Evanse94b6de2020-02-20 09:19:13 +0900521
522 OnStartDevice(arc_device_.get());
523
Garrick Evansd90a3822019-11-12 17:53:08 +0900524 LOG(INFO) << "ARC++ network service started {pid: " << pid_ << "}";
525 return true;
526}
527
Garrick Evans015b0d62020-02-07 09:06:38 +0900528void ArcService::ContainerImpl::Stop(uint32_t /*pid*/) {
Garrick Evans4dec0c42019-11-29 12:51:57 +0900529 if (!IsStarted())
Taoyu Li1c96d272019-12-13 14:17:43 +0900530 return;
Garrick Evans4dec0c42019-11-29 12:51:57 +0900531
Garrick Evanse94b6de2020-02-20 09:19:13 +0900532 // Per crbug/1008686 this device cannot be deleted and then re-added.
533 // So instead of removing the bridge, bring it down and mark it. This will
534 // allow us to detect if the device is re-added in case of a crash restart
535 // and do the right thing.
536 if (arc_device_) {
537 OnStopDevice(arc_device_.get());
538 if (!datapath_->MaskInterfaceFlags(kArcBridge, IFF_DEBUG, IFF_UP))
539 LOG(ERROR) << "Failed to bring down arc bridge "
540 << "- it may not restart correctly";
541 }
Garrick Evansd90a3822019-11-12 17:53:08 +0900542
543 LOG(INFO) << "ARC++ network service stopped {pid: " << pid_ << "}";
544 pid_ = kInvalidPID;
545}
546
Garrick Evans015b0d62020-02-07 09:06:38 +0900547bool ArcService::ContainerImpl::IsStarted(uint32_t* pid) const {
Garrick Evansa51d0a12019-11-28 13:51:23 +0900548 if (pid)
549 *pid = pid_;
550
Garrick Evansd90a3822019-11-12 17:53:08 +0900551 return pid_ != kInvalidPID;
552}
553
554bool ArcService::ContainerImpl::OnStartDevice(Device* device) {
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900555 LOG(INFO) << "Starting device " << device->phys_ifname()
556 << " bridge: " << device->host_ifname()
557 << " guest_iface: " << device->guest_ifname() << " pid: " << pid_;
Garrick Evansd90a3822019-11-12 17:53:08 +0900558
Garrick Evans2470caa2020-03-04 14:15:41 +0900559 // Set up the virtual pair inside the container namespace.
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900560 const std::string veth_ifname = ArcVethHostName(device->guest_ifname());
Garrick Evans2470caa2020-03-04 14:15:41 +0900561 {
562 ScopedNS ns(pid_);
563 if (!ns.IsValid() && pid_ != kTestPID) {
564 LOG(ERROR)
565 << "Cannot create virtual link -- invalid container namespace?";
566 return false;
567 }
568
569 if (!datapath_->AddVirtualInterfacePair(veth_ifname,
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900570 device->guest_ifname())) {
Garrick Evans2470caa2020-03-04 14:15:41 +0900571 LOG(ERROR) << "Failed to create virtual interface pair for "
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900572 << device->phys_ifname();
Garrick Evans2470caa2020-03-04 14:15:41 +0900573 return false;
574 }
575
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900576 const auto& config = device->config();
577
Garrick Evans2470caa2020-03-04 14:15:41 +0900578 if (!datapath_->ConfigureInterface(
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900579 device->guest_ifname(), config.mac_addr(), config.guest_ipv4_addr(),
580 30, true /* link up */, device->options().fwd_multicast)) {
581 LOG(ERROR) << "Failed to configure interface " << device->guest_ifname();
582 datapath_->RemoveInterface(device->guest_ifname());
Garrick Evans2470caa2020-03-04 14:15:41 +0900583 return false;
584 }
Garrick Evansd90a3822019-11-12 17:53:08 +0900585 }
586
Garrick Evans2470caa2020-03-04 14:15:41 +0900587 // Now pull the host end out into the root namespace and add it to the bridge.
588 if (datapath_->runner().RestoreDefaultNamespace(veth_ifname, pid_) != 0) {
589 LOG(ERROR) << "Failed to prepare interface " << veth_ifname;
590 {
591 ScopedNS ns(pid_);
592 if (ns.IsValid()) {
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900593 datapath_->RemoveInterface(device->guest_ifname());
Garrick Evans2470caa2020-03-04 14:15:41 +0900594 } else {
595 LOG(ERROR) << "Failed to re-enter container namespace."
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900596 << " Subsequent attempts to restart "
597 << device->phys_ifname() << " may not succeed.";
Garrick Evans2470caa2020-03-04 14:15:41 +0900598 }
599 }
600 return false;
601 }
602 if (!datapath_->ToggleInterface(veth_ifname, true /*up*/)) {
603 LOG(ERROR) << "Failed to bring up interface " << veth_ifname;
Garrick Evansd90a3822019-11-12 17:53:08 +0900604 datapath_->RemoveInterface(veth_ifname);
Garrick Evansd90a3822019-11-12 17:53:08 +0900605 return false;
606 }
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900607 if (!datapath_->AddToBridge(device->host_ifname(), veth_ifname)) {
Garrick Evans2470caa2020-03-04 14:15:41 +0900608 datapath_->RemoveInterface(veth_ifname);
609 LOG(ERROR) << "Failed to bridge interface " << veth_ifname;
610 return false;
611 }
Garrick Evansd90a3822019-11-12 17:53:08 +0900612
Garrick Evans3bd06372020-03-23 10:42:58 +0900613 if (device != arc_device_.get()) {
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900614 forwarder_->StartForwarding(device->phys_ifname(), device->host_ifname(),
615 device->options().ipv6_enabled,
616 device->options().fwd_multicast);
Garrick Evans3bd06372020-03-23 10:42:58 +0900617 } else {
618 // Signal the container that the network device is ready.
619 datapath_->runner().WriteSentinelToContainer(pid_);
620 }
Garrick Evans2e5c9ab2020-03-05 14:33:58 +0900621
Garrick Evansd90a3822019-11-12 17:53:08 +0900622 return true;
623}
624
625void ArcService::ContainerImpl::OnStopDevice(Device* device) {
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900626 LOG(INFO) << "Stopping device " << device->phys_ifname()
627 << " bridge: " << device->host_ifname()
628 << " guest_iface: " << device->guest_ifname() << " pid: " << pid_;
Garrick Evansd90a3822019-11-12 17:53:08 +0900629
Garrick Evans2e5c9ab2020-03-05 14:33:58 +0900630 if (device != arc_device_.get())
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900631 forwarder_->StopForwarding(device->phys_ifname(), device->host_ifname(),
Garrick Evans2e5c9ab2020-03-05 14:33:58 +0900632 device->options().ipv6_enabled,
633 device->options().fwd_multicast);
634
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900635 datapath_->RemoveInterface(ArcVethHostName(device->phys_ifname()));
Garrick Evansd90a3822019-11-12 17:53:08 +0900636}
637
638void ArcService::ContainerImpl::OnDefaultInterfaceChanged(
Garrick Evansb05a7ff2020-02-18 12:59:55 +0900639 const std::string& new_ifname, const std::string& prev_ifname) {}
Garrick Evansd90a3822019-11-12 17:53:08 +0900640
Garrick Evansb4eb3892019-11-13 12:07:07 +0900641// VM specific functions
642
Garrick Evansbbdf4b42020-03-05 12:59:06 +0900643ArcService::VmImpl::VmImpl(ShillClient* shill_client,
Garrick Evans2e5c9ab2020-03-05 14:33:58 +0900644 Datapath* datapath,
645 AddressManager* addr_mgr,
Garrick Evansf5862122020-03-16 09:13:45 +0900646 TrafficForwarder* forwarder,
647 bool enable_multinet)
Garrick Evansbbdf4b42020-03-05 12:59:06 +0900648 : cid_(kInvalidCID),
649 shill_client_(shill_client),
Garrick Evans2e5c9ab2020-03-05 14:33:58 +0900650 datapath_(datapath),
651 addr_mgr_(addr_mgr),
Garrick Evansf5862122020-03-16 09:13:45 +0900652 forwarder_(forwarder),
653 enable_multinet_(enable_multinet) {}
Garrick Evansb4eb3892019-11-13 12:07:07 +0900654
655GuestMessage::GuestType ArcService::VmImpl::guest() const {
656 return GuestMessage::ARC_VM;
657}
658
Garrick Evans015b0d62020-02-07 09:06:38 +0900659uint32_t ArcService::VmImpl::id() const {
Garrick Evans664a82f2019-12-17 12:18:05 +0900660 return cid_;
661}
662
Garrick Evans015b0d62020-02-07 09:06:38 +0900663bool ArcService::VmImpl::Start(uint32_t cid) {
Garrick Evansa51d0a12019-11-28 13:51:23 +0900664 // This can happen if concierge crashes and doesn't send the vm down RPC.
665 // It can probably be addressed by stopping and restarting the service.
666 if (cid_ != kInvalidCID)
667 return false;
668
Garrick Evans015b0d62020-02-07 09:06:38 +0900669 if (cid == kInvalidCID) {
Garrick Evansb4eb3892019-11-13 12:07:07 +0900670 LOG(ERROR) << "Invalid VM cid " << cid;
671 return false;
672 }
Garrick Evansb4eb3892019-11-13 12:07:07 +0900673 cid_ = cid;
Garrick Evansb4eb3892019-11-13 12:07:07 +0900674
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900675 // Allocate pool of 6 TAP devices and IPv4 allocs
676
Garrick Evanse94b6de2020-02-20 09:19:13 +0900677 Device::Options opts{
678 .fwd_multicast = true,
679 .ipv6_enabled = true,
680 .use_default_interface = true,
Garrick Evanse94b6de2020-02-20 09:19:13 +0900681 };
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900682 auto config = MakeArcConfig(addr_mgr_, AddressManager::Guest::VM_ARC);
Garrick Evanse94b6de2020-02-20 09:19:13 +0900683
684 // Create the bridge.
685 if (!datapath_->AddBridge(kArcVmBridge, config->host_ipv4_addr(), 30)) {
686 LOG(ERROR) << "Failed to setup arc bridge: " << kArcVmBridge;
687 return false;
688 }
689
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900690 arc_device_ = std::make_unique<Device>(kArcVmIfname, kArcVmBridge,
691 kArcVmIfname, std::move(config), opts);
Garrick Evanse94b6de2020-02-20 09:19:13 +0900692
693 OnStartDevice(arc_device_.get());
694
695 LOG(INFO) << "ARCVM network service started {cid: " << cid_ << "}";
Garrick Evansb4eb3892019-11-13 12:07:07 +0900696 return true;
697}
698
Garrick Evans015b0d62020-02-07 09:06:38 +0900699void ArcService::VmImpl::Stop(uint32_t cid) {
Garrick Evans21173b12019-11-20 15:23:16 +0900700 if (cid_ != cid) {
701 LOG(ERROR) << "Mismatched ARCVM CIDs " << cid_ << " != " << cid;
702 return;
703 }
704
Garrick Evanse94b6de2020-02-20 09:19:13 +0900705 OnStopDevice(arc_device_.get());
706 datapath_->RemoveBridge(kArcVmBridge);
707 arc_device_.reset();
708
Garrick Evansb4eb3892019-11-13 12:07:07 +0900709 LOG(INFO) << "ARCVM network service stopped {cid: " << cid_ << "}";
710 cid_ = kInvalidCID;
711}
712
Garrick Evans015b0d62020-02-07 09:06:38 +0900713bool ArcService::VmImpl::IsStarted(uint32_t* cid) const {
Garrick Evansa51d0a12019-11-28 13:51:23 +0900714 if (cid)
715 *cid = cid_;
716
Garrick Evans015b0d62020-02-07 09:06:38 +0900717 return cid_ != kInvalidCID;
Garrick Evansb4eb3892019-11-13 12:07:07 +0900718}
719
720bool ArcService::VmImpl::OnStartDevice(Device* device) {
Garrick Evansf5862122020-03-16 09:13:45 +0900721 // TODO(garrick): Remove once ARCVM P is gone.
722 if (device == arc_device_.get() && !enable_multinet_)
723 return OnStartArcPDevice();
Garrick Evansb4eb3892019-11-13 12:07:07 +0900724
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900725 LOG(INFO) << "Starting device " << device->phys_ifname()
726 << " bridge: " << device->host_ifname()
727 << " guest_iface: " << device->guest_ifname() << " cid: " << cid_;
Garrick Evansb4eb3892019-11-13 12:07:07 +0900728
Garrick Evansb4eb3892019-11-13 12:07:07 +0900729 // Since the interface will be added to the bridge, no address configuration
730 // should be provided here.
731 std::string tap =
732 datapath_->AddTAP("" /* auto-generate name */, nullptr /* no mac addr */,
733 nullptr /* no ipv4 subnet */, vm_tools::kCrosVmUser);
734 if (tap.empty()) {
735 LOG(ERROR) << "Failed to create TAP device for VM";
736 return false;
737 }
738
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900739 if (!datapath_->AddToBridge(device->host_ifname(), tap)) {
Garrick Evansb4eb3892019-11-13 12:07:07 +0900740 LOG(ERROR) << "Failed to bridge TAP device " << tap;
741 datapath_->RemoveInterface(tap);
742 return false;
743 }
744
Garrick Evansf5862122020-03-16 09:13:45 +0900745 device->set_tap_ifname(tap);
746
747 if (device != arc_device_.get())
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900748 forwarder_->StartForwarding(device->phys_ifname(), device->host_ifname(),
749 device->options().ipv6_enabled,
750 device->options().fwd_multicast);
Garrick Evansf5862122020-03-16 09:13:45 +0900751
752 return true;
753}
754
755bool ArcService::VmImpl::OnStartArcPDevice() {
756 LOG(INFO) << "Starting device " << kArcVmIfname << " bridge: " << kArcVmBridge
757 << " guest_iface: " << kArcVmIfname << " cid: " << cid_;
758
759 // Since the interface will be added to the bridge, no address configuration
760 // should be provided here.
761 std::string tap =
762 datapath_->AddTAP("" /* auto-generate name */, nullptr /* no mac addr */,
763 nullptr /* no ipv4 subnet */, vm_tools::kCrosVmUser);
764 if (tap.empty()) {
765 LOG(ERROR) << "Failed to create TAP device for VM";
766 return false;
767 }
768
769 if (!datapath_->AddToBridge(kArcVmBridge, tap)) {
770 LOG(ERROR) << "Failed to bridge TAP device " << tap;
771 datapath_->RemoveInterface(tap);
772 return false;
773 }
774
775 arc_device_->set_tap_ifname(tap);
776
777 // Setup the iptables.
778 if (!datapath_->AddLegacyIPv4DNAT(
779 IPv4AddressToString(arc_device_->config().guest_ipv4_addr())))
780 LOG(ERROR) << "Failed to configure ARC traffic rules";
781
782 if (!datapath_->AddOutboundIPv4(kArcVmBridge))
783 LOG(ERROR) << "Failed to configure egress traffic rules";
784
Garrick Evansbbdf4b42020-03-05 12:59:06 +0900785 OnDefaultInterfaceChanged(shill_client_->default_interface(),
786 "" /*previous*/);
Garrick Evansbcce09e2020-03-10 15:08:04 +0900787
Garrick Evansb4eb3892019-11-13 12:07:07 +0900788 return true;
789}
790
791void ArcService::VmImpl::OnStopDevice(Device* device) {
Garrick Evansf5862122020-03-16 09:13:45 +0900792 // TODO(garrick): Remove once ARCVM P is gone.
793 if (device == arc_device_.get() && !enable_multinet_)
794 return OnStopArcPDevice();
Garrick Evansb4eb3892019-11-13 12:07:07 +0900795
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900796 LOG(INFO) << "Stopping " << device->phys_ifname()
797 << " bridge: " << device->host_ifname()
798 << " guest_iface: " << device->guest_ifname() << " cid: " << cid_;
Garrick Evansb4eb3892019-11-13 12:07:07 +0900799
Garrick Evansf5862122020-03-16 09:13:45 +0900800 if (device != arc_device_.get())
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900801 forwarder_->StopForwarding(device->phys_ifname(), device->host_ifname(),
Garrick Evansf5862122020-03-16 09:13:45 +0900802 device->options().ipv6_enabled,
803 device->options().fwd_multicast);
Garrick Evansbcce09e2020-03-10 15:08:04 +0900804
805 datapath_->RemoveInterface(device->tap_ifname());
806 device->set_tap_ifname("");
Garrick Evansb4eb3892019-11-13 12:07:07 +0900807}
808
Garrick Evansf5862122020-03-16 09:13:45 +0900809void ArcService::VmImpl::OnStopArcPDevice() {
810 LOG(INFO) << "Stopping " << kArcVmIfname << " bridge: " << kArcVmBridge
811 << " guest_iface: " << kArcVmIfname << " cid: " << cid_;
812
813 datapath_->RemoveOutboundIPv4(kArcVmBridge);
814 datapath_->RemoveLegacyIPv4DNAT();
815
816 OnDefaultInterfaceChanged("" /*new_ifname*/,
817 shill_client_->default_interface());
818
819 datapath_->RemoveInterface(arc_device_->tap_ifname());
820 arc_device_->set_tap_ifname("");
821}
822
Garrick Evans1b1f67c2020-02-04 16:21:25 +0900823void ArcService::VmImpl::OnDefaultInterfaceChanged(
824 const std::string& new_ifname, const std::string& prev_ifname) {
Garrick Evansf5862122020-03-16 09:13:45 +0900825 if (!IsStarted() || enable_multinet_)
Garrick Evansb4eb3892019-11-13 12:07:07 +0900826 return;
827
Garrick Evans2e5c9ab2020-03-05 14:33:58 +0900828 forwarder_->StopForwarding(prev_ifname, kArcVmBridge, true /*ipv6*/,
829 true /*multicast*/);
830
Garrick Evansb4eb3892019-11-13 12:07:07 +0900831 datapath_->RemoveLegacyIPv4InboundDNAT();
Garrick Evansb4eb3892019-11-13 12:07:07 +0900832
833 // If a new default interface was given, then re-enable with that.
Garrick Evans1b1f67c2020-02-04 16:21:25 +0900834 if (!new_ifname.empty()) {
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900835 datapath_->AddLegacyIPv4InboundDNAT(new_ifname);
Jason Jeremy Iman0e9f8262020-03-06 14:50:49 +0900836 forwarder_->StartForwarding(new_ifname, kArcVmBridge, true /*ipv6*/,
837 true /*multicast*/);
Garrick Evansb4eb3892019-11-13 12:07:07 +0900838 }
839}
840
Garrick Evans5d55f5e2019-07-17 15:28:10 +0900841} // namespace arc_networkd