blob: be254b96c2c29e7a7e8e3379aeeb8ff4f9aa6d8b [file] [log] [blame]
Kevin Cernekee95d4ae92016-06-19 10:26:29 -07001// Copyright 2016 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/manager.h"
Kevin Cernekee4e62cc12016-12-03 11:50:53 -08006
Kevin Cernekee95d4ae92016-06-19 10:26:29 -07007#include <arpa/inet.h>
Garrick Evans4ac09852020-01-16 14:09:22 +09008#include <net/if.h>
Hugo Benichi935eca92018-07-03 13:47:24 +09009#include <netinet/in.h>
Kevin Cernekee95d4ae92016-06-19 10:26:29 -070010#include <stdint.h>
Hugo Benichi7352ad92020-04-07 16:11:59 +090011#include <sys/epoll.h>
Garrick Evans54861622019-07-19 09:05:09 +090012#include <sys/prctl.h>
Garrick Evans96e03042019-05-28 14:30:52 +090013#include <sys/socket.h>
14#include <sys/un.h>
Kevin Cernekee95d4ae92016-06-19 10:26:29 -070015
Kevin Cernekee27bcaa62016-12-03 11:16:26 -080016#include <utility>
17
Hugo Benichicc6850f2020-01-17 13:26:06 +090018#include <base/bind.h>
Qijiang Fan713061e2021-03-08 15:45:12 +090019#include <base/check.h>
Taoyu Lia0727dc2020-09-24 19:54:59 +090020#include <base/files/scoped_file.h>
Kevin Cernekee95d4ae92016-06-19 10:26:29 -070021#include <base/logging.h>
Qijiang Fan886c4692021-02-19 11:54:10 +090022#include <base/notreached.h>
Taoyu Lic85c44b2019-12-04 17:32:57 +090023#include <base/strings/string_number_conversions.h>
Garrick Evans96e03042019-05-28 14:30:52 +090024#include <base/strings/string_split.h>
Garrick Evans6f258d02019-06-28 16:32:07 +090025#include <base/strings/string_util.h>
Taoyu Li179dcc62019-10-17 11:21:08 +090026#include <base/strings/stringprintf.h>
hschamf9546312020-04-14 15:12:40 +090027#include <base/threading/thread_task_runner_handle.h>
Taoyu Lic85c44b2019-12-04 17:32:57 +090028#include <brillo/key_value_store.h>
Kevin Cernekee27bcaa62016-12-03 11:16:26 -080029#include <brillo/minijail/minijail.h>
30
Garrick Evans3388a032020-03-24 11:25:55 +090031#include "patchpanel/ipc.pb.h"
32#include "patchpanel/mac_address_generator.h"
33#include "patchpanel/net_util.h"
34#include "patchpanel/routing_service.h"
35#include "patchpanel/scoped_ns.h"
Garrick Evans428e4762018-12-11 15:18:42 +090036
Garrick Evans3388a032020-03-24 11:25:55 +090037namespace patchpanel {
Garrick Evans08843932019-09-17 14:41:08 +090038namespace {
Garrick Evans4c042572019-12-17 13:42:25 +090039constexpr int kSubprocessRestartDelayMs = 900;
Garrick Evans08843932019-09-17 14:41:08 +090040
Hugo Benichi7352ad92020-04-07 16:11:59 +090041// Time interval between epoll checks on file descriptors committed by callers
42// of ConnectNamespace DBus API.
43constexpr const base::TimeDelta kConnectNamespaceCheckInterval =
Hugo Benichifa9462e2020-06-26 09:50:48 +090044 base::TimeDelta::FromSeconds(5);
Hugo Benichi7352ad92020-04-07 16:11:59 +090045
Garrick Evans08843932019-09-17 14:41:08 +090046// Passes |method_call| to |handler| and passes the response to
47// |response_sender|. If |handler| returns nullptr, an empty response is
48// created and sent.
49void HandleSynchronousDBusMethodCall(
50 base::Callback<std::unique_ptr<dbus::Response>(dbus::MethodCall*)> handler,
51 dbus::MethodCall* method_call,
52 dbus::ExportedObject::ResponseSender response_sender) {
53 std::unique_ptr<dbus::Response> response = handler.Run(method_call);
54 if (!response)
55 response = dbus::Response::FromMethodCall(method_call);
Qijiang Fan0c657d42020-08-24 19:07:50 +090056 std::move(response_sender).Run(std::move(response));
Garrick Evans08843932019-09-17 14:41:08 +090057}
58
Hugo Benichi69c989d2021-03-01 00:23:39 +090059bool IsIPv6NDProxyEnabled(ShillClient::Device::Type type) {
60 static const std::set<ShillClient::Device::Type> ndproxy_allowed_types{
61 ShillClient::Device::Type::kCellular,
62 ShillClient::Device::Type::kEthernet,
63 ShillClient::Device::Type::kEthernetEap,
64 ShillClient::Device::Type::kWifi,
65 };
66 return ndproxy_allowed_types.find(type) != ndproxy_allowed_types.end();
67}
68
Garrick Evans08843932019-09-17 14:41:08 +090069} // namespace
Kevin Cernekee95d4ae92016-06-19 10:26:29 -070070
Taoyu Lice7caa62019-10-01 15:43:33 +090071Manager::Manager(std::unique_ptr<HelperProcess> adb_proxy,
Jason Jeremy Imand89b5f52019-10-24 10:39:17 +090072 std::unique_ptr<HelperProcess> mcast_proxy,
Garrick Evans1f5a3612019-11-08 12:59:03 +090073 std::unique_ptr<HelperProcess> nd_proxy)
Garrick Evans3915af32019-07-25 15:44:34 +090074 : adb_proxy_(std::move(adb_proxy)),
Jason Jeremy Imand89b5f52019-10-24 10:39:17 +090075 mcast_proxy_(std::move(mcast_proxy)),
Garrick Evans4ee5ce22020-03-18 07:05:17 +090076 nd_proxy_(std::move(nd_proxy)) {
Taoyu Li179dcc62019-10-17 11:21:08 +090077 runner_ = std::make_unique<MinijailedProcessRunner>();
Jason Jeremy Imana7273a32020-08-04 11:25:31 +090078 datapath_ = std::make_unique<Datapath>(runner_.get(), &firewall_);
Hugo Benichi7352ad92020-04-07 16:11:59 +090079 connected_namespaces_epollfd_ = epoll_create(1 /* size */);
Taoyu Li179dcc62019-10-17 11:21:08 +090080}
Long Chengd4415582019-09-24 19:16:09 +000081
Jason Jeremy Imanf4156cb2019-11-14 15:36:22 +090082std::map<const std::string, bool> Manager::cached_feature_enabled_ = {};
83
84bool Manager::ShouldEnableFeature(
85 int min_android_sdk_version,
86 int min_chrome_milestone,
87 const std::vector<std::string>& supported_boards,
88 const std::string& feature_name) {
89 static const char kLsbReleasePath[] = "/etc/lsb-release";
90
91 const auto& cached_result = cached_feature_enabled_.find(feature_name);
92 if (cached_result != cached_feature_enabled_.end())
93 return cached_result->second;
94
95 auto check = [min_android_sdk_version, min_chrome_milestone,
96 &supported_boards, &feature_name]() {
97 brillo::KeyValueStore store;
98 if (!store.Load(base::FilePath(kLsbReleasePath))) {
99 LOG(ERROR) << "Could not read lsb-release";
100 return false;
101 }
102
103 std::string value;
104 if (!store.GetString("CHROMEOS_ARC_ANDROID_SDK_VERSION", &value)) {
105 LOG(ERROR) << feature_name
106 << " disabled - cannot determine Android SDK version";
107 return false;
108 }
109 int ver = 0;
110 if (!base::StringToInt(value.c_str(), &ver)) {
111 LOG(ERROR) << feature_name << " disabled - invalid Android SDK version";
112 return false;
113 }
114 if (ver < min_android_sdk_version) {
115 LOG(INFO) << feature_name << " disabled for Android SDK " << value;
116 return false;
117 }
118
119 if (!store.GetString("CHROMEOS_RELEASE_CHROME_MILESTONE", &value)) {
120 LOG(ERROR) << feature_name
121 << " disabled - cannot determine ChromeOS milestone";
122 return false;
123 }
124 if (!base::StringToInt(value.c_str(), &ver)) {
125 LOG(ERROR) << feature_name << " disabled - invalid ChromeOS milestone";
126 return false;
127 }
128 if (ver < min_chrome_milestone) {
129 LOG(INFO) << feature_name << " disabled for ChromeOS milestone " << value;
130 return false;
131 }
132
133 if (!store.GetString("CHROMEOS_RELEASE_BOARD", &value)) {
134 LOG(ERROR) << feature_name << " disabled - cannot determine board";
135 return false;
136 }
137 if (!supported_boards.empty() &&
138 std::find(supported_boards.begin(), supported_boards.end(), value) ==
139 supported_boards.end()) {
140 LOG(INFO) << feature_name << " disabled for board " << value;
141 return false;
142 }
143 return true;
144 };
145
146 bool result = check();
147 cached_feature_enabled_.emplace(feature_name, result);
148 return result;
149}
150
Kevin Cernekee95d4ae92016-06-19 10:26:29 -0700151int Manager::OnInit() {
Garrick Evans54861622019-07-19 09:05:09 +0900152 prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
Kevin Cernekee27bcaa62016-12-03 11:16:26 -0800153
154 // Handle subprocess lifecycle.
155 process_reaper_.Register(this);
Hugo Benichi935eca92018-07-03 13:47:24 +0900156
157 CHECK(process_reaper_.WatchForChild(
Garrick Evans96e03042019-05-28 14:30:52 +0900158 FROM_HERE, adb_proxy_->pid(),
159 base::Bind(&Manager::OnSubprocessExited, weak_factory_.GetWeakPtr(),
160 adb_proxy_->pid())))
161 << "Failed to watch adb-proxy child process";
Taoyu Liaf944c92019-10-01 12:22:31 +0900162 CHECK(process_reaper_.WatchForChild(
Jason Jeremy Imand89b5f52019-10-24 10:39:17 +0900163 FROM_HERE, mcast_proxy_->pid(),
164 base::Bind(&Manager::OnSubprocessExited, weak_factory_.GetWeakPtr(),
165 nd_proxy_->pid())))
166 << "Failed to watch multicast-proxy child process";
167 CHECK(process_reaper_.WatchForChild(
Taoyu Liaf944c92019-10-01 12:22:31 +0900168 FROM_HERE, nd_proxy_->pid(),
169 base::Bind(&Manager::OnSubprocessExited, weak_factory_.GetWeakPtr(),
170 nd_proxy_->pid())))
Jason Jeremy Imand89b5f52019-10-24 10:39:17 +0900171 << "Failed to watch nd-proxy child process";
Garrick Evans96e03042019-05-28 14:30:52 +0900172
Garrick Evans49879532018-12-03 13:15:36 +0900173 // Run after Daemon::OnInit().
hschamf9546312020-04-14 15:12:40 +0900174 base::ThreadTaskRunnerHandle::Get()->PostTask(
Kevin Cernekee95d4ae92016-06-19 10:26:29 -0700175 FROM_HERE,
176 base::Bind(&Manager::InitialSetup, weak_factory_.GetWeakPtr()));
177
178 return DBusDaemon::OnInit();
179}
180
181void Manager::InitialSetup() {
Garrick Evans08843932019-09-17 14:41:08 +0900182 LOG(INFO) << "Setting up DBus service interface";
183 dbus_svc_path_ = bus_->GetExportedObject(
184 dbus::ObjectPath(patchpanel::kPatchPanelServicePath));
185 if (!dbus_svc_path_) {
186 LOG(FATAL) << "Failed to export " << patchpanel::kPatchPanelServicePath
187 << " object";
188 }
189
Hugo Benichi76be34a2020-08-26 22:35:54 +0900190 shill_client_ = std::make_unique<ShillClient>(bus_);
191
Garrick Evans08843932019-09-17 14:41:08 +0900192 using ServiceMethod =
193 std::unique_ptr<dbus::Response> (Manager::*)(dbus::MethodCall*);
194 const std::map<const char*, ServiceMethod> kServiceMethods = {
195 {patchpanel::kArcStartupMethod, &Manager::OnArcStartup},
196 {patchpanel::kArcShutdownMethod, &Manager::OnArcShutdown},
197 {patchpanel::kArcVmStartupMethod, &Manager::OnArcVmStartup},
198 {patchpanel::kArcVmShutdownMethod, &Manager::OnArcVmShutdown},
Garrick Evans47c19272019-11-21 10:58:21 +0900199 {patchpanel::kTerminaVmStartupMethod, &Manager::OnTerminaVmStartup},
200 {patchpanel::kTerminaVmShutdownMethod, &Manager::OnTerminaVmShutdown},
Garrick Evans51d5b552020-01-30 10:42:06 +0900201 {patchpanel::kPluginVmStartupMethod, &Manager::OnPluginVmStartup},
202 {patchpanel::kPluginVmShutdownMethod, &Manager::OnPluginVmShutdown},
Hugo Benichi7d9d8db2020-03-30 15:56:56 +0900203 {patchpanel::kSetVpnIntentMethod, &Manager::OnSetVpnIntent},
Hugo Benichib56b77c2020-01-15 16:00:56 +0900204 {patchpanel::kConnectNamespaceMethod, &Manager::OnConnectNamespace},
Jie Jiang493cde42020-07-17 21:43:39 +0900205 {patchpanel::kGetTrafficCountersMethod, &Manager::OnGetTrafficCounters},
Jason Jeremy Imanb2a421e2020-07-04 20:48:32 +0900206 {patchpanel::kModifyPortRuleMethod, &Manager::OnModifyPortRule},
Garrick Evans02e6e872020-11-30 11:53:13 +0900207 {patchpanel::kGetDevicesMethod, &Manager::OnGetDevices},
Garrick Evans08843932019-09-17 14:41:08 +0900208 };
209
210 for (const auto& kv : kServiceMethods) {
211 if (!dbus_svc_path_->ExportMethodAndBlock(
212 patchpanel::kPatchPanelInterface, kv.first,
213 base::Bind(&HandleSynchronousDBusMethodCall,
214 base::Bind(kv.second, base::Unretained(this))))) {
215 LOG(FATAL) << "Failed to export method " << kv.first;
216 }
217 }
218
219 if (!bus_->RequestOwnershipAndBlock(patchpanel::kPatchPanelServiceName,
220 dbus::Bus::REQUIRE_PRIMARY)) {
221 LOG(FATAL) << "Failed to take ownership of "
222 << patchpanel::kPatchPanelServiceName;
223 }
224 LOG(INFO) << "DBus service interface ready";
225
Hugo Benichifda77232020-08-21 18:28:15 +0900226 routing_svc_ = std::make_unique<RoutingService>();
Hugo Benichi058a26a2020-11-26 10:10:39 +0900227 counters_svc_ =
228 std::make_unique<CountersService>(datapath_.get(), runner_.get());
Hugo Benichifda77232020-08-21 18:28:15 +0900229
Hugo Benichibf811c62020-09-07 17:30:45 +0900230 // b/162966185: Allow Jetstream to disable:
231 // - the IP forwarding setup used for hosting VMs and containers,
232 // - the iptables rules for fwmark based routing.
233 if (!USE_JETSTREAM_ROUTING) {
234 datapath_->Start();
Hugo Benichi2a940542020-10-26 18:50:49 +0900235 shill_client_->RegisterDefaultDeviceChangedHandler(base::BindRepeating(
236 &Manager::OnDefaultDeviceChanged, weak_factory_.GetWeakPtr()));
Hugo Benichibf811c62020-09-07 17:30:45 +0900237 shill_client_->RegisterDevicesChangedHandler(base::BindRepeating(
238 &Manager::OnDevicesChanged, weak_factory_.GetWeakPtr()));
Hugo Benichi1e0656f2021-02-15 15:43:38 +0900239 shill_client_->RegisterIPConfigsChangedHandler(base::BindRepeating(
240 &Manager::OnIPConfigsChanged, weak_factory_.GetWeakPtr()));
Hugo Benichibf811c62020-09-07 17:30:45 +0900241 }
Hugo Benichifda77232020-08-21 18:28:15 +0900242
Taoyu Lia0727dc2020-09-24 19:54:59 +0900243 nd_proxy_->RegisterNDProxyMessageHandler(
244 base::Bind(&Manager::OnNDProxyMessage, weak_factory_.GetWeakPtr()));
Hugo Benichifda77232020-08-21 18:28:15 +0900245
Hugo Benichifda77232020-08-21 18:28:15 +0900246 GuestMessage::GuestType arc_guest =
247 USE_ARCVM ? GuestMessage::ARC_VM : GuestMessage::ARC;
Garrick Evans209a80a2020-11-30 14:42:40 +0900248 arc_svc_ = std::make_unique<ArcService>(
Hugo Benichib9d123e2021-02-27 23:37:16 +0900249 shill_client_.get(), datapath_.get(), &addr_mgr_, arc_guest,
Garrick Evans209a80a2020-11-30 14:42:40 +0900250 base::BindRepeating(&Manager::OnDeviceChanged,
251 weak_factory_.GetWeakPtr()));
252 cros_svc_ = std::make_unique<CrostiniService>(
Hugo Benichi69c989d2021-03-01 00:23:39 +0900253 &addr_mgr_, datapath_.get(),
Garrick Evans209a80a2020-11-30 14:42:40 +0900254 base::BindRepeating(&Manager::OnDeviceChanged,
255 weak_factory_.GetWeakPtr()));
Jie Jiang84966852020-09-18 18:49:05 +0900256 network_monitor_svc_ = std::make_unique<NetworkMonitorService>(
257 shill_client_.get(),
Jie Jiang25c1b972020-11-12 15:42:53 +0900258 base::BindRepeating(&Manager::OnNeighborReachabilityEvent,
Jie Jiang84966852020-09-18 18:49:05 +0900259 weak_factory_.GetWeakPtr()));
Hugo Benichifda77232020-08-21 18:28:15 +0900260 network_monitor_svc_->Start();
Hugo Benichifda77232020-08-21 18:28:15 +0900261 nd_proxy_->Listen();
262}
263
264void Manager::OnShutdown(int* exit_code) {
265 LOG(INFO) << "Shutting down and cleaning up";
Jie Jiang84966852020-09-18 18:49:05 +0900266 network_monitor_svc_.reset();
Hugo Benichifda77232020-08-21 18:28:15 +0900267 cros_svc_.reset();
268 arc_svc_.reset();
269 close(connected_namespaces_epollfd_);
270 // Tear down any remaining connected namespace.
271 std::vector<int> connected_namespaces_fdkeys;
272 for (const auto& kv : connected_namespaces_)
273 connected_namespaces_fdkeys.push_back(kv.first);
274 for (const int fdkey : connected_namespaces_fdkeys)
275 DisconnectNamespace(fdkey);
Hugo Benichibf811c62020-09-07 17:30:45 +0900276 if (!USE_JETSTREAM_ROUTING)
277 datapath_->Stop();
Jie Jiang00592cf2020-12-17 11:23:49 +0900278
279 brillo::DBusDaemon::OnShutdown(exit_code);
Hugo Benichifda77232020-08-21 18:28:15 +0900280}
281
282void Manager::OnSubprocessExited(pid_t pid, const siginfo_t&) {
283 LOG(ERROR) << "Subprocess " << pid << " exited unexpectedly -"
284 << " attempting to restart";
285
286 HelperProcess* proc;
287 if (pid == adb_proxy_->pid()) {
288 proc = adb_proxy_.get();
289 } else if (pid == mcast_proxy_->pid()) {
290 proc = mcast_proxy_.get();
291 } else if (pid == nd_proxy_->pid()) {
292 proc = nd_proxy_.get();
293 } else {
294 LOG(DFATAL) << "Unknown child process";
295 return;
296 }
297
298 process_reaper_.ForgetChild(pid);
299
300 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
301 FROM_HERE,
302 base::Bind(&Manager::RestartSubprocess, weak_factory_.GetWeakPtr(), proc),
303 base::TimeDelta::FromMilliseconds((2 << proc->restarts()) *
304 kSubprocessRestartDelayMs));
305}
306
307void Manager::RestartSubprocess(HelperProcess* subproc) {
308 if (subproc->Restart()) {
309 DCHECK(process_reaper_.WatchForChild(
310 FROM_HERE, subproc->pid(),
311 base::Bind(&Manager::OnSubprocessExited, weak_factory_.GetWeakPtr(),
312 subproc->pid())))
313 << "Failed to watch child process " << subproc->pid();
314 }
315}
316
Hugo Benichi2a940542020-10-26 18:50:49 +0900317void Manager::OnDefaultDeviceChanged(const ShillClient::Device& new_device,
318 const ShillClient::Device& prev_device) {
319 // Only take into account interface switches and ignore layer 3 property
320 // changes.
321 if (prev_device.ifname == new_device.ifname)
322 return;
323
Hugo Benichi058a26a2020-11-26 10:10:39 +0900324 if (prev_device.type == ShillClient::Device::Type::kVPN) {
Hugo Benichi2a940542020-10-26 18:50:49 +0900325 datapath_->StopVpnRouting(prev_device.ifname);
Hugo Benichi058a26a2020-11-26 10:10:39 +0900326 counters_svc_->OnVpnDeviceRemoved(prev_device.ifname);
327 }
Hugo Benichi2a940542020-10-26 18:50:49 +0900328
Hugo Benichi058a26a2020-11-26 10:10:39 +0900329 if (new_device.type == ShillClient::Device::Type::kVPN) {
Hugo Benichi2a940542020-10-26 18:50:49 +0900330 datapath_->StartVpnRouting(new_device.ifname);
Hugo Benichi058a26a2020-11-26 10:10:39 +0900331 counters_svc_->OnVpnDeviceAdded(new_device.ifname);
332 }
Hugo Benichi69c989d2021-03-01 00:23:39 +0900333
334 // When the default logical network changes, Crostini's tap devices must leave
335 // their current forwarding group for multicast and IPv6 ndproxy and join the
336 // forwarding group of the new logical default network.
337 for (const auto* tap_device : cros_svc_->GetDevices()) {
Hugo Benichi6f118a32021-03-01 12:28:14 +0900338 StopForwarding(prev_device.ifname, tap_device->host_ifname());
339 StartForwarding(new_device.ifname, tap_device->host_ifname());
Hugo Benichi69c989d2021-03-01 00:23:39 +0900340 }
Hugo Benichi2a940542020-10-26 18:50:49 +0900341}
342
Hugo Benichi76be34a2020-08-26 22:35:54 +0900343void Manager::OnDevicesChanged(const std::set<std::string>& added,
344 const std::set<std::string>& removed) {
Hugo Benichi058a26a2020-11-26 10:10:39 +0900345 for (const std::string& ifname : removed) {
Hugo Benichi76be34a2020-08-26 22:35:54 +0900346 datapath_->StopConnectionPinning(ifname);
Hugo Benichi1e0656f2021-02-15 15:43:38 +0900347 datapath_->RemoveRedirectDnsRule(ifname);
Hugo Benichi058a26a2020-11-26 10:10:39 +0900348 counters_svc_->OnPhysicalDeviceRemoved(ifname);
349 }
Hugo Benichi76be34a2020-08-26 22:35:54 +0900350
Hugo Benichi058a26a2020-11-26 10:10:39 +0900351 for (const std::string& ifname : added) {
Hugo Benichi76be34a2020-08-26 22:35:54 +0900352 datapath_->StartConnectionPinning(ifname);
Hugo Benichi1e0656f2021-02-15 15:43:38 +0900353 ShillClient::Device device;
354 if (!shill_client_->GetDeviceProperties(ifname, &device))
355 continue;
356
357 if (!device.ipconfig.ipv4_dns_addresses.empty())
358 datapath_->AddRedirectDnsRule(ifname,
359 device.ipconfig.ipv4_dns_addresses.front());
360
Hugo Benichi058a26a2020-11-26 10:10:39 +0900361 counters_svc_->OnPhysicalDeviceAdded(ifname);
362 }
Hugo Benichi76be34a2020-08-26 22:35:54 +0900363}
364
Hugo Benichi1e0656f2021-02-15 15:43:38 +0900365void Manager::OnIPConfigsChanged(const std::string& device,
366 const ShillClient::IPConfig& ipconfig) {
367 if (ipconfig.ipv4_dns_addresses.empty()) {
368 datapath_->RemoveRedirectDnsRule(device);
369 } else {
370 datapath_->AddRedirectDnsRule(device, ipconfig.ipv4_dns_addresses.front());
371 }
372}
373
Garrick Evans209a80a2020-11-30 14:42:40 +0900374void Manager::OnDeviceChanged(const Device& device,
375 Device::ChangeEvent event,
376 GuestMessage::GuestType guest_type) {
377 dbus::Signal signal(kPatchPanelInterface, kNetworkDeviceChangedSignal);
378 NetworkDeviceChangedSignal proto;
379 proto.set_event(event == Device::ChangeEvent::ADDED
380 ? NetworkDeviceChangedSignal::DEVICE_ADDED
381 : NetworkDeviceChangedSignal::DEVICE_REMOVED);
382 auto* dev = proto.mutable_device();
383 dev->set_ifname(device.host_ifname());
Garrick Evans9ccd2a62021-02-18 14:48:28 +0900384 dev->set_phys_ifname(device.phys_ifname());
Garrick Evans209a80a2020-11-30 14:42:40 +0900385 dev->set_ipv4_addr(device.config().guest_ipv4_addr());
386 if (const auto* subnet = device.config().ipv4_subnet()) {
387 auto* sub = dev->mutable_ipv4_subnet();
388 sub->set_base_addr(subnet->BaseAddress());
389 sub->set_prefix_len(subnet->PrefixLength());
390 }
391 switch (guest_type) {
392 case GuestMessage::ARC:
393 dev->set_guest_type(NetworkDevice::ARC);
394 break;
395 case GuestMessage::ARC_VM:
396 dev->set_guest_type(NetworkDevice::ARCVM);
397 break;
398 case GuestMessage::TERMINA_VM:
399 dev->set_guest_type(NetworkDevice::TERMINA_VM);
400 break;
401 case GuestMessage::PLUGIN_VM:
402 dev->set_guest_type(NetworkDevice::PLUGIN_VM);
403 break;
404 default:
Hugo Benichi6f118a32021-03-01 12:28:14 +0900405 dev->set_guest_type(NetworkDevice::UNKNOWN);
Garrick Evans209a80a2020-11-30 14:42:40 +0900406 LOG(ERROR) << "Unknown device type";
Hugo Benichi6f118a32021-03-01 12:28:14 +0900407 return;
Garrick Evans209a80a2020-11-30 14:42:40 +0900408 }
409
Hugo Benichi6f118a32021-03-01 12:28:14 +0900410 if (dev->guest_type() != NetworkDevice::UNKNOWN) {
411 const std::string& upstream_device =
412 (guest_type == GuestMessage::ARC || guest_type == GuestMessage::ARC_VM)
413 ? device.phys_ifname()
414 : shill_client_->default_interface();
Hugo Benichi69c989d2021-03-01 00:23:39 +0900415
Hugo Benichib9d123e2021-02-27 23:37:16 +0900416 if (event == Device::ChangeEvent::ADDED) {
Hugo Benichi6f118a32021-03-01 12:28:14 +0900417 StartForwarding(upstream_device, device.host_ifname());
Hugo Benichib9d123e2021-02-27 23:37:16 +0900418 } else if (event == Device::ChangeEvent::REMOVED) {
Hugo Benichi6f118a32021-03-01 12:28:14 +0900419 StopForwarding(upstream_device, device.host_ifname());
Hugo Benichib9d123e2021-02-27 23:37:16 +0900420 }
421 }
422
Garrick Evans209a80a2020-11-30 14:42:40 +0900423 dbus::MessageWriter(&signal).AppendProtoAsArrayOfBytes(proto);
424 dbus_svc_path_->SendSignal(&signal);
425}
426
Garrick Evanse94a14e2019-11-11 10:32:13 +0900427bool Manager::StartArc(pid_t pid) {
Garrick Evans508a4bc2019-11-14 08:45:52 +0900428 if (!arc_svc_->Start(pid))
429 return false;
Garrick Evanse94a14e2019-11-11 10:32:13 +0900430
431 GuestMessage msg;
432 msg.set_event(GuestMessage::START);
433 msg.set_type(GuestMessage::ARC);
434 msg.set_arc_pid(pid);
435 SendGuestMessage(msg);
436
437 return true;
438}
439
Hugo Benichi4d4bb8f2020-07-07 12:16:07 +0900440void Manager::StopArc() {
Garrick Evanse94a14e2019-11-11 10:32:13 +0900441 GuestMessage msg;
442 msg.set_event(GuestMessage::STOP);
443 msg.set_type(GuestMessage::ARC);
444 SendGuestMessage(msg);
445
Hugo Benichi4d4bb8f2020-07-07 12:16:07 +0900446 // After the ARC container has stopped, the pid is not known anymore.
447 // The pid argument is ignored by ArcService.
448 arc_svc_->Stop(0);
Garrick Evanse94a14e2019-11-11 10:32:13 +0900449}
450
Garrick Evans015b0d62020-02-07 09:06:38 +0900451bool Manager::StartArcVm(uint32_t cid) {
Garrick Evans508a4bc2019-11-14 08:45:52 +0900452 if (!arc_svc_->Start(cid))
453 return false;
Garrick Evanse94a14e2019-11-11 10:32:13 +0900454
455 GuestMessage msg;
456 msg.set_event(GuestMessage::START);
457 msg.set_type(GuestMessage::ARC_VM);
458 msg.set_arcvm_vsock_cid(cid);
459 SendGuestMessage(msg);
460
461 return true;
462}
463
Garrick Evans015b0d62020-02-07 09:06:38 +0900464void Manager::StopArcVm(uint32_t cid) {
Garrick Evanse94a14e2019-11-11 10:32:13 +0900465 GuestMessage msg;
466 msg.set_event(GuestMessage::STOP);
467 msg.set_type(GuestMessage::ARC_VM);
468 SendGuestMessage(msg);
469
Garrick Evans21173b12019-11-20 15:23:16 +0900470 arc_svc_->Stop(cid);
Garrick Evanse94a14e2019-11-11 10:32:13 +0900471}
472
Garrick Evans51d5b552020-01-30 10:42:06 +0900473bool Manager::StartCrosVm(uint64_t vm_id,
474 GuestMessage::GuestType vm_type,
Garrick Evans53a2a982020-02-05 10:53:35 +0900475 uint32_t subnet_index) {
Garrick Evans51d5b552020-01-30 10:42:06 +0900476 DCHECK(vm_type == GuestMessage::TERMINA_VM ||
477 vm_type == GuestMessage::PLUGIN_VM);
478
479 if (!cros_svc_->Start(vm_id, vm_type == GuestMessage::TERMINA_VM,
480 subnet_index))
Garrick Evans47c19272019-11-21 10:58:21 +0900481 return false;
482
483 GuestMessage msg;
484 msg.set_event(GuestMessage::START);
Garrick Evans51d5b552020-01-30 10:42:06 +0900485 msg.set_type(vm_type);
Garrick Evans47c19272019-11-21 10:58:21 +0900486 SendGuestMessage(msg);
487
488 return true;
489}
490
Garrick Evans51d5b552020-01-30 10:42:06 +0900491void Manager::StopCrosVm(uint64_t vm_id, GuestMessage::GuestType vm_type) {
Garrick Evans47c19272019-11-21 10:58:21 +0900492 GuestMessage msg;
493 msg.set_event(GuestMessage::STOP);
Garrick Evans51d5b552020-01-30 10:42:06 +0900494 msg.set_type(vm_type);
Garrick Evans47c19272019-11-21 10:58:21 +0900495 SendGuestMessage(msg);
496
Garrick Evans51d5b552020-01-30 10:42:06 +0900497 cros_svc_->Stop(vm_id, vm_type == GuestMessage::TERMINA_VM);
Garrick Evans47c19272019-11-21 10:58:21 +0900498}
499
Garrick Evans02e6e872020-11-30 11:53:13 +0900500std::unique_ptr<dbus::Response> Manager::OnGetDevices(
501 dbus::MethodCall* method_call) {
502 std::unique_ptr<dbus::Response> dbus_response(
503 dbus::Response::FromMethodCall(method_call));
504
505 dbus::MessageReader reader(method_call);
506 dbus::MessageWriter writer(dbus_response.get());
507
508 patchpanel::GetDevicesRequest request;
509 patchpanel::GetDevicesResponse response;
510
511 if (!reader.PopArrayOfBytesAsProto(&request)) {
512 LOG(ERROR) << "Unable to parse request";
513 writer.AppendProtoAsArrayOfBytes(response);
514 return dbus_response;
515 }
516
517 static const auto arc_guest_type =
518 USE_ARCVM ? NetworkDevice::ARCVM : NetworkDevice::ARC;
519
520 arc_svc_->ScanDevices(base::BindRepeating(
521 [](patchpanel::GetDevicesResponse* resp, const Device& device) {
522 auto* dev = resp->add_devices();
523 dev->set_ifname(device.host_ifname());
Garrick Evans9ccd2a62021-02-18 14:48:28 +0900524 dev->set_phys_ifname(device.phys_ifname());
Garrick Evans02e6e872020-11-30 11:53:13 +0900525 dev->set_ipv4_addr(device.config().guest_ipv4_addr());
526 dev->set_guest_type(arc_guest_type);
527 if (const auto* subnet = device.config().ipv4_subnet()) {
528 auto* sub = dev->mutable_ipv4_subnet();
529 sub->set_base_addr(subnet->BaseAddress());
530 sub->set_prefix_len(subnet->PrefixLength());
531 }
532 },
533 &response));
534
535 cros_svc_->ScanDevices(base::BindRepeating(
536 [](patchpanel::GetDevicesResponse* resp, uint64_t vm_id, bool is_termina,
537 const Device& device) {
538 auto* dev = resp->add_devices();
539 dev->set_ifname(device.host_ifname());
Garrick Evans9ccd2a62021-02-18 14:48:28 +0900540 dev->set_phys_ifname(device.phys_ifname());
Garrick Evans02e6e872020-11-30 11:53:13 +0900541 dev->set_ipv4_addr(device.config().guest_ipv4_addr());
542 dev->set_guest_type(is_termina ? NetworkDevice::TERMINA_VM
543 : NetworkDevice::PLUGIN_VM);
544 if (const auto* subnet = device.config().ipv4_subnet()) {
545 auto* sub = dev->mutable_ipv4_subnet();
546 sub->set_base_addr(subnet->BaseAddress());
547 sub->set_prefix_len(subnet->PrefixLength());
548 }
549 },
550 &response));
551
552 writer.AppendProtoAsArrayOfBytes(response);
553 return dbus_response;
554}
555
Garrick Evans08843932019-09-17 14:41:08 +0900556std::unique_ptr<dbus::Response> Manager::OnArcStartup(
557 dbus::MethodCall* method_call) {
558 LOG(INFO) << "ARC++ starting up";
559
560 std::unique_ptr<dbus::Response> dbus_response(
561 dbus::Response::FromMethodCall(method_call));
562
563 dbus::MessageReader reader(method_call);
564 dbus::MessageWriter writer(dbus_response.get());
565
566 patchpanel::ArcStartupRequest request;
567 patchpanel::ArcStartupResponse response;
568
569 if (!reader.PopArrayOfBytesAsProto(&request)) {
570 LOG(ERROR) << "Unable to parse request";
571 writer.AppendProtoAsArrayOfBytes(response);
572 return dbus_response;
573 }
574
Garrick Evanse01bf072019-11-15 09:08:19 +0900575 if (!StartArc(request.pid()))
576 LOG(ERROR) << "Failed to start ARC++ network service";
Garrick Evanse94a14e2019-11-11 10:32:13 +0900577
Garrick Evans08843932019-09-17 14:41:08 +0900578 writer.AppendProtoAsArrayOfBytes(response);
579 return dbus_response;
580}
581
582std::unique_ptr<dbus::Response> Manager::OnArcShutdown(
583 dbus::MethodCall* method_call) {
584 LOG(INFO) << "ARC++ shutting down";
585
586 std::unique_ptr<dbus::Response> dbus_response(
587 dbus::Response::FromMethodCall(method_call));
588
589 dbus::MessageReader reader(method_call);
590 dbus::MessageWriter writer(dbus_response.get());
591
592 patchpanel::ArcShutdownRequest request;
593 patchpanel::ArcShutdownResponse response;
594
595 if (!reader.PopArrayOfBytesAsProto(&request)) {
596 LOG(ERROR) << "Unable to parse request";
597 writer.AppendProtoAsArrayOfBytes(response);
598 return dbus_response;
599 }
600
Hugo Benichi4d4bb8f2020-07-07 12:16:07 +0900601 StopArc();
Garrick Evanse94a14e2019-11-11 10:32:13 +0900602
Garrick Evans08843932019-09-17 14:41:08 +0900603 writer.AppendProtoAsArrayOfBytes(response);
604 return dbus_response;
605}
606
607std::unique_ptr<dbus::Response> Manager::OnArcVmStartup(
608 dbus::MethodCall* method_call) {
609 LOG(INFO) << "ARCVM starting up";
610
611 std::unique_ptr<dbus::Response> dbus_response(
612 dbus::Response::FromMethodCall(method_call));
613
614 dbus::MessageReader reader(method_call);
615 dbus::MessageWriter writer(dbus_response.get());
616
617 patchpanel::ArcVmStartupRequest request;
618 patchpanel::ArcVmStartupResponse response;
619
620 if (!reader.PopArrayOfBytesAsProto(&request)) {
621 LOG(ERROR) << "Unable to parse request";
622 writer.AppendProtoAsArrayOfBytes(response);
623 return dbus_response;
624 }
625
Garrick Evans47c19272019-11-21 10:58:21 +0900626 if (!StartArcVm(request.cid())) {
Garrick Evanse01bf072019-11-15 09:08:19 +0900627 LOG(ERROR) << "Failed to start ARCVM network service";
Garrick Evans47c19272019-11-21 10:58:21 +0900628 writer.AppendProtoAsArrayOfBytes(response);
629 return dbus_response;
Garrick Evanse01bf072019-11-15 09:08:19 +0900630 }
Garrick Evanse94a14e2019-11-11 10:32:13 +0900631
Garrick Evans47c19272019-11-21 10:58:21 +0900632 // Populate the response with the known devices.
Garrick Evans38b25a42020-04-06 15:17:42 +0900633 for (const auto* config : arc_svc_->GetDeviceConfigs()) {
634 if (config->tap_ifname().empty())
635 continue;
Garrick Evans47c19272019-11-21 10:58:21 +0900636
Garrick Evans38b25a42020-04-06 15:17:42 +0900637 auto* dev = response.add_devices();
638 dev->set_ifname(config->tap_ifname());
639 dev->set_ipv4_addr(config->guest_ipv4_addr());
Garrick Evansa0c9c972020-11-30 09:54:59 +0900640 dev->set_guest_type(NetworkDevice::ARCVM);
Garrick Evans38b25a42020-04-06 15:17:42 +0900641 }
Garrick Evanse94b6de2020-02-20 09:19:13 +0900642
Garrick Evans08843932019-09-17 14:41:08 +0900643 writer.AppendProtoAsArrayOfBytes(response);
644 return dbus_response;
645}
646
647std::unique_ptr<dbus::Response> Manager::OnArcVmShutdown(
648 dbus::MethodCall* method_call) {
649 LOG(INFO) << "ARCVM shutting down";
650
651 std::unique_ptr<dbus::Response> dbus_response(
652 dbus::Response::FromMethodCall(method_call));
653
654 dbus::MessageReader reader(method_call);
655 dbus::MessageWriter writer(dbus_response.get());
656
657 patchpanel::ArcVmShutdownRequest request;
658 patchpanel::ArcVmShutdownResponse response;
659
660 if (!reader.PopArrayOfBytesAsProto(&request)) {
661 LOG(ERROR) << "Unable to parse request";
662 writer.AppendProtoAsArrayOfBytes(response);
663 return dbus_response;
664 }
665
Garrick Evans21173b12019-11-20 15:23:16 +0900666 StopArcVm(request.cid());
Garrick Evanse94a14e2019-11-11 10:32:13 +0900667
Garrick Evans08843932019-09-17 14:41:08 +0900668 writer.AppendProtoAsArrayOfBytes(response);
669 return dbus_response;
670}
671
Garrick Evans47c19272019-11-21 10:58:21 +0900672std::unique_ptr<dbus::Response> Manager::OnTerminaVmStartup(
673 dbus::MethodCall* method_call) {
674 LOG(INFO) << "Termina VM starting up";
675
676 std::unique_ptr<dbus::Response> dbus_response(
677 dbus::Response::FromMethodCall(method_call));
678
679 dbus::MessageReader reader(method_call);
680 dbus::MessageWriter writer(dbus_response.get());
681
682 patchpanel::TerminaVmStartupRequest request;
683 patchpanel::TerminaVmStartupResponse response;
684
685 if (!reader.PopArrayOfBytesAsProto(&request)) {
686 LOG(ERROR) << "Unable to parse request";
687 writer.AppendProtoAsArrayOfBytes(response);
688 return dbus_response;
689 }
690
691 const int32_t cid = request.cid();
Garrick Evans53a2a982020-02-05 10:53:35 +0900692 if (!StartCrosVm(cid, GuestMessage::TERMINA_VM)) {
Garrick Evans47c19272019-11-21 10:58:21 +0900693 LOG(ERROR) << "Failed to start Termina VM network service";
694 writer.AppendProtoAsArrayOfBytes(response);
695 return dbus_response;
696 }
697
Garrick Evans51d5b552020-01-30 10:42:06 +0900698 const auto* const tap = cros_svc_->TAP(cid, true /*is_termina*/);
Garrick Evansb1c93712020-01-22 09:28:25 +0900699 if (!tap) {
700 LOG(DFATAL) << "TAP device missing";
701 writer.AppendProtoAsArrayOfBytes(response);
702 return dbus_response;
703 }
Garrick Evans47c19272019-11-21 10:58:21 +0900704
Garrick Evansb1c93712020-01-22 09:28:25 +0900705 auto* dev = response.mutable_device();
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900706 dev->set_ifname(tap->host_ifname());
Garrick Evansa0c9c972020-11-30 09:54:59 +0900707 dev->set_guest_type(NetworkDevice::TERMINA_VM);
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900708 const auto* subnet = tap->config().ipv4_subnet();
Garrick Evansb1c93712020-01-22 09:28:25 +0900709 if (!subnet) {
710 LOG(DFATAL) << "Missing required subnet for {cid: " << cid << "}";
711 writer.AppendProtoAsArrayOfBytes(response);
712 return dbus_response;
713 }
714 auto* resp_subnet = dev->mutable_ipv4_subnet();
715 resp_subnet->set_base_addr(subnet->BaseAddress());
716 resp_subnet->set_prefix_len(subnet->PrefixLength());
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900717 subnet = tap->config().lxd_ipv4_subnet();
Garrick Evansb1c93712020-01-22 09:28:25 +0900718 if (!subnet) {
719 LOG(DFATAL) << "Missing required lxd subnet for {cid: " << cid << "}";
720 writer.AppendProtoAsArrayOfBytes(response);
721 return dbus_response;
722 }
723 resp_subnet = response.mutable_container_subnet();
724 resp_subnet->set_base_addr(subnet->BaseAddress());
725 resp_subnet->set_prefix_len(subnet->PrefixLength());
Garrick Evans47c19272019-11-21 10:58:21 +0900726
727 writer.AppendProtoAsArrayOfBytes(response);
728 return dbus_response;
729}
730
731std::unique_ptr<dbus::Response> Manager::OnTerminaVmShutdown(
732 dbus::MethodCall* method_call) {
733 LOG(INFO) << "Termina VM shutting down";
734
735 std::unique_ptr<dbus::Response> dbus_response(
736 dbus::Response::FromMethodCall(method_call));
737
738 dbus::MessageReader reader(method_call);
739 dbus::MessageWriter writer(dbus_response.get());
740
741 patchpanel::TerminaVmShutdownRequest request;
742 patchpanel::TerminaVmShutdownResponse response;
743
744 if (!reader.PopArrayOfBytesAsProto(&request)) {
745 LOG(ERROR) << "Unable to parse request";
746 writer.AppendProtoAsArrayOfBytes(response);
747 return dbus_response;
748 }
749
Garrick Evans51d5b552020-01-30 10:42:06 +0900750 StopCrosVm(request.cid(), GuestMessage::TERMINA_VM);
751
752 writer.AppendProtoAsArrayOfBytes(response);
753 return dbus_response;
754}
755
756std::unique_ptr<dbus::Response> Manager::OnPluginVmStartup(
757 dbus::MethodCall* method_call) {
758 LOG(INFO) << "Plugin VM starting up";
759
760 std::unique_ptr<dbus::Response> dbus_response(
761 dbus::Response::FromMethodCall(method_call));
762
763 dbus::MessageReader reader(method_call);
764 dbus::MessageWriter writer(dbus_response.get());
765
766 patchpanel::PluginVmStartupRequest request;
767 patchpanel::PluginVmStartupResponse response;
768
769 if (!reader.PopArrayOfBytesAsProto(&request)) {
770 LOG(ERROR) << "Unable to parse request";
771 writer.AppendProtoAsArrayOfBytes(response);
772 return dbus_response;
773 }
774
Garrick Evans08fb34b2020-02-20 10:50:17 +0900775 const uint64_t vm_id = request.id();
Garrick Evans53a2a982020-02-05 10:53:35 +0900776 if (!StartCrosVm(vm_id, GuestMessage::PLUGIN_VM, request.subnet_index())) {
Garrick Evans51d5b552020-01-30 10:42:06 +0900777 LOG(ERROR) << "Failed to start Plugin VM network service";
778 writer.AppendProtoAsArrayOfBytes(response);
779 return dbus_response;
780 }
781
782 const auto* const tap = cros_svc_->TAP(vm_id, false /*is_termina*/);
783 if (!tap) {
784 LOG(DFATAL) << "TAP device missing";
785 writer.AppendProtoAsArrayOfBytes(response);
786 return dbus_response;
787 }
788
Garrick Evans51d5b552020-01-30 10:42:06 +0900789 auto* dev = response.mutable_device();
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900790 dev->set_ifname(tap->host_ifname());
Garrick Evansa0c9c972020-11-30 09:54:59 +0900791 dev->set_guest_type(NetworkDevice::PLUGIN_VM);
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900792 const auto* subnet = tap->config().ipv4_subnet();
Garrick Evans51d5b552020-01-30 10:42:06 +0900793 if (!subnet) {
794 LOG(DFATAL) << "Missing required subnet for {cid: " << vm_id << "}";
795 writer.AppendProtoAsArrayOfBytes(response);
796 return dbus_response;
797 }
798 auto* resp_subnet = dev->mutable_ipv4_subnet();
799 resp_subnet->set_base_addr(subnet->BaseAddress());
800 resp_subnet->set_prefix_len(subnet->PrefixLength());
801
802 writer.AppendProtoAsArrayOfBytes(response);
803 return dbus_response;
804}
805
806std::unique_ptr<dbus::Response> Manager::OnPluginVmShutdown(
807 dbus::MethodCall* method_call) {
808 LOG(INFO) << "Plugin VM shutting down";
809
810 std::unique_ptr<dbus::Response> dbus_response(
811 dbus::Response::FromMethodCall(method_call));
812
813 dbus::MessageReader reader(method_call);
814 dbus::MessageWriter writer(dbus_response.get());
815
816 patchpanel::PluginVmShutdownRequest request;
817 patchpanel::PluginVmShutdownResponse response;
818
819 if (!reader.PopArrayOfBytesAsProto(&request)) {
820 LOG(ERROR) << "Unable to parse request";
821 writer.AppendProtoAsArrayOfBytes(response);
822 return dbus_response;
823 }
824
825 StopCrosVm(request.id(), GuestMessage::PLUGIN_VM);
Garrick Evans47c19272019-11-21 10:58:21 +0900826
827 writer.AppendProtoAsArrayOfBytes(response);
828 return dbus_response;
829}
830
Hugo Benichi7d9d8db2020-03-30 15:56:56 +0900831std::unique_ptr<dbus::Response> Manager::OnSetVpnIntent(
832 dbus::MethodCall* method_call) {
833 std::unique_ptr<dbus::Response> dbus_response(
834 dbus::Response::FromMethodCall(method_call));
835
836 dbus::MessageReader reader(method_call);
837 dbus::MessageWriter writer(dbus_response.get());
838
839 patchpanel::SetVpnIntentRequest request;
840 patchpanel::SetVpnIntentResponse response;
841
842 bool success = reader.PopArrayOfBytesAsProto(&request);
843 if (!success) {
844 LOG(ERROR) << "Unable to parse SetVpnIntentRequest";
845 // Do not return yet to make sure we close the received fd.
846 }
847
848 base::ScopedFD client_socket;
849 reader.PopFileDescriptor(&client_socket);
850
851 if (success)
852 success = routing_svc_->SetVpnFwmark(client_socket.get(), request.policy());
853
854 response.set_success(success);
Hugo Benichib56b77c2020-01-15 16:00:56 +0900855
856 writer.AppendProtoAsArrayOfBytes(response);
857 return dbus_response;
858}
859
860std::unique_ptr<dbus::Response> Manager::OnConnectNamespace(
861 dbus::MethodCall* method_call) {
862 std::unique_ptr<dbus::Response> dbus_response(
863 dbus::Response::FromMethodCall(method_call));
864
865 dbus::MessageReader reader(method_call);
866 dbus::MessageWriter writer(dbus_response.get());
867
868 patchpanel::ConnectNamespaceRequest request;
Hugo Benichib56b77c2020-01-15 16:00:56 +0900869
870 if (!reader.PopArrayOfBytesAsProto(&request)) {
Hugo Benichicc6850f2020-01-17 13:26:06 +0900871 LOG(ERROR) << "Unable to parse ConnectNamespaceRequest";
872 // Do not return yet to make sure we close the received fd and
873 // validate other arguments.
Hugo Benichi7c342672020-09-08 09:18:14 +0900874 writer.AppendProtoAsArrayOfBytes(patchpanel::ConnectNamespaceResponse());
875 return dbus_response;
Hugo Benichib56b77c2020-01-15 16:00:56 +0900876 }
877
Hugo Benichicc6850f2020-01-17 13:26:06 +0900878 base::ScopedFD client_fd;
879 reader.PopFileDescriptor(&client_fd);
880 if (!client_fd.is_valid()) {
881 LOG(ERROR) << "ConnectNamespaceRequest: invalid file descriptor";
Hugo Benichi7c342672020-09-08 09:18:14 +0900882 writer.AppendProtoAsArrayOfBytes(patchpanel::ConnectNamespaceResponse());
883 return dbus_response;
Hugo Benichicc6850f2020-01-17 13:26:06 +0900884 }
885
886 pid_t pid = request.pid();
Garrick Evans263bd952020-12-03 20:27:43 +0900887 if (pid == 1 || pid == getpid()) {
888 LOG(ERROR) << "ConnectNamespaceRequest: privileged namespace pid " << pid;
889 writer.AppendProtoAsArrayOfBytes(patchpanel::ConnectNamespaceResponse());
890 return dbus_response;
891 }
Hugo Benichicc6850f2020-01-17 13:26:06 +0900892 {
Hugo Benichi0781d402021-02-22 13:43:11 +0900893 ScopedNS ns(pid, ScopedNS::Type::Network);
Hugo Benichicc6850f2020-01-17 13:26:06 +0900894 if (!ns.IsValid()) {
895 LOG(ERROR) << "ConnectNamespaceRequest: invalid namespace pid " << pid;
Hugo Benichi7c342672020-09-08 09:18:14 +0900896 writer.AppendProtoAsArrayOfBytes(patchpanel::ConnectNamespaceResponse());
897 return dbus_response;
Hugo Benichicc6850f2020-01-17 13:26:06 +0900898 }
899 }
900
901 const std::string& outbound_ifname = request.outbound_physical_device();
902 if (!outbound_ifname.empty() && !shill_client_->has_device(outbound_ifname)) {
903 LOG(ERROR) << "ConnectNamespaceRequest: invalid outbound ifname "
904 << outbound_ifname;
Hugo Benichi7c342672020-09-08 09:18:14 +0900905 writer.AppendProtoAsArrayOfBytes(patchpanel::ConnectNamespaceResponse());
906 return dbus_response;
Hugo Benichicc6850f2020-01-17 13:26:06 +0900907 }
908
Hugo Benichi7c342672020-09-08 09:18:14 +0900909 auto response = ConnectNamespace(std::move(client_fd), request);
910 writer.AppendProtoAsArrayOfBytes(*response);
Hugo Benichi7d9d8db2020-03-30 15:56:56 +0900911 return dbus_response;
912}
913
Jie Jiang493cde42020-07-17 21:43:39 +0900914std::unique_ptr<dbus::Response> Manager::OnGetTrafficCounters(
915 dbus::MethodCall* method_call) {
916 std::unique_ptr<dbus::Response> dbus_response(
917 dbus::Response::FromMethodCall(method_call));
918
919 dbus::MessageReader reader(method_call);
920 dbus::MessageWriter writer(dbus_response.get());
921
922 patchpanel::TrafficCountersRequest request;
923 patchpanel::TrafficCountersResponse response;
924
925 if (!reader.PopArrayOfBytesAsProto(&request)) {
926 LOG(ERROR) << "Unable to parse TrafficCountersRequest";
927 writer.AppendProtoAsArrayOfBytes(response);
928 return dbus_response;
929 }
930
931 const std::set<std::string> devices{request.devices().begin(),
932 request.devices().end()};
933 const auto counters = counters_svc_->GetCounters(devices);
934 for (const auto& kv : counters) {
935 auto* traffic_counter = response.add_counters();
936 const auto& source_and_device = kv.first;
937 const auto& counter = kv.second;
938 traffic_counter->set_source(source_and_device.first);
939 traffic_counter->set_device(source_and_device.second);
940 traffic_counter->set_rx_bytes(counter.rx_bytes);
941 traffic_counter->set_rx_packets(counter.rx_packets);
942 traffic_counter->set_tx_bytes(counter.tx_bytes);
943 traffic_counter->set_tx_packets(counter.tx_packets);
944 }
945
946 writer.AppendProtoAsArrayOfBytes(response);
947 return dbus_response;
948}
949
Jason Jeremy Imanb2a421e2020-07-04 20:48:32 +0900950std::unique_ptr<dbus::Response> Manager::OnModifyPortRule(
951 dbus::MethodCall* method_call) {
952 std::unique_ptr<dbus::Response> dbus_response(
953 dbus::Response::FromMethodCall(method_call));
954
955 dbus::MessageReader reader(method_call);
956 dbus::MessageWriter writer(dbus_response.get());
957
958 patchpanel::ModifyPortRuleRequest request;
959 patchpanel::ModifyPortRuleResponse response;
960
961 if (!reader.PopArrayOfBytesAsProto(&request)) {
962 LOG(ERROR) << "Unable to parse ModifyPortRequest";
963 writer.AppendProtoAsArrayOfBytes(response);
964 return dbus_response;
965 }
966
Jason Jeremy Imanc28df852020-07-11 17:38:26 +0900967 response.set_success(ModifyPortRule(request));
Jason Jeremy Imanb2a421e2020-07-04 20:48:32 +0900968 writer.AppendProtoAsArrayOfBytes(response);
969 return dbus_response;
970}
971
Jie Jiang25c1b972020-11-12 15:42:53 +0900972void Manager::OnNeighborReachabilityEvent(
Jie Jiang84966852020-09-18 18:49:05 +0900973 int ifindex,
974 const shill::IPAddress& ip_addr,
975 NeighborLinkMonitor::NeighborRole role,
Jie Jiang25c1b972020-11-12 15:42:53 +0900976 NeighborReachabilityEventSignal::EventType event_type) {
Jie Jiang84966852020-09-18 18:49:05 +0900977 if (!ip_addr.IsValid()) {
978 LOG(DFATAL) << "ip_addr is not valid";
979 return;
980 }
981
Jie Jiang25c1b972020-11-12 15:42:53 +0900982 using SignalProto = NeighborReachabilityEventSignal;
Jie Jiang84966852020-09-18 18:49:05 +0900983 SignalProto proto;
984 proto.set_ifindex(ifindex);
985 proto.set_ip_addr(ip_addr.ToString());
Jie Jiang25c1b972020-11-12 15:42:53 +0900986 proto.set_type(event_type);
Jie Jiang84966852020-09-18 18:49:05 +0900987 switch (role) {
988 case NeighborLinkMonitor::NeighborRole::kGateway:
989 proto.set_role(SignalProto::GATEWAY);
990 break;
991 case NeighborLinkMonitor::NeighborRole::kDNSServer:
992 proto.set_role(SignalProto::DNS_SERVER);
993 break;
994 case NeighborLinkMonitor::NeighborRole::kGatewayAndDNSServer:
995 proto.set_role(SignalProto::GATEWAY_AND_DNS_SERVER);
996 break;
997 default:
998 NOTREACHED();
999 }
1000
Jie Jiang25c1b972020-11-12 15:42:53 +09001001 dbus::Signal signal(kPatchPanelInterface, kNeighborReachabilityEventSignal);
Jie Jiang84966852020-09-18 18:49:05 +09001002 dbus::MessageWriter writer(&signal);
1003 if (!writer.AppendProtoAsArrayOfBytes(proto)) {
Jie Jiang25c1b972020-11-12 15:42:53 +09001004 LOG(ERROR) << "Failed to encode proto NeighborReachabilityEventSignal";
Jie Jiang84966852020-09-18 18:49:05 +09001005 return;
1006 }
1007
1008 dbus_svc_path_->SendSignal(&signal);
1009}
1010
Hugo Benichi7c342672020-09-08 09:18:14 +09001011std::unique_ptr<patchpanel::ConnectNamespaceResponse> Manager::ConnectNamespace(
Hugo Benichiadf1ec52020-01-17 16:23:58 +09001012 base::ScopedFD client_fd,
Hugo Benichi7c342672020-09-08 09:18:14 +09001013 const patchpanel::ConnectNamespaceRequest& request) {
1014 auto response = std::make_unique<patchpanel::ConnectNamespaceResponse>();
1015
Hugo Benichiadf1ec52020-01-17 16:23:58 +09001016 std::unique_ptr<Subnet> subnet =
1017 addr_mgr_.AllocateIPv4Subnet(AddressManager::Guest::MINIJAIL_NETNS);
1018 if (!subnet) {
Hugo Benichi7c342672020-09-08 09:18:14 +09001019 LOG(ERROR) << "ConnectNamespace: exhausted IPv4 subnet space";
1020 return response;
Hugo Benichie8758b52020-04-03 14:49:01 +09001021 }
Hugo Benichiadf1ec52020-01-17 16:23:58 +09001022
Hugo Benichi7352ad92020-04-07 16:11:59 +09001023 // Dup the client fd into our own: this guarantees that the fd number will
1024 // be stable and tied to the actual kernel resources used by the client.
1025 base::ScopedFD local_client_fd(dup(client_fd.get()));
1026 if (!local_client_fd.is_valid()) {
Hugo Benichi7c342672020-09-08 09:18:14 +09001027 PLOG(ERROR) << "ConnectNamespace: failed to dup() client fd";
1028 return response;
Hugo Benichi7352ad92020-04-07 16:11:59 +09001029 }
1030
Hugo Benichi7c342672020-09-08 09:18:14 +09001031 // Add the duped fd to the epoll watcher.
Hugo Benichi7352ad92020-04-07 16:11:59 +09001032 // TODO(hugobenichi) Find a way to reuse base::FileDescriptorWatcher for
1033 // listening to EPOLLHUP.
1034 struct epoll_event epevent;
1035 epevent.events = EPOLLIN; // EPOLLERR | EPOLLHUP are always waited for.
1036 epevent.data.fd = local_client_fd.get();
1037 if (epoll_ctl(connected_namespaces_epollfd_, EPOLL_CTL_ADD,
1038 local_client_fd.get(), &epevent) != 0) {
Hugo Benichi7c342672020-09-08 09:18:14 +09001039 PLOG(ERROR) << "ConnectNamespace: epoll_ctl(EPOLL_CTL_ADD) failed";
1040 return response;
Hugo Benichi7352ad92020-04-07 16:11:59 +09001041 }
1042
Hugo Benichi7c342672020-09-08 09:18:14 +09001043 const std::string ifname_id = std::to_string(connected_namespaces_next_id_);
Hugo Benichifcf81022020-12-04 11:01:37 +09001044 ConnectedNamespace nsinfo = {};
1045 nsinfo.pid = request.pid();
1046 nsinfo.netns_name = "connected_netns_" + ifname_id;
Hugo Benichi93306e52020-12-04 16:08:00 +09001047 nsinfo.source = ProtoToTrafficSource(request.traffic_source());
1048 if (nsinfo.source == TrafficSource::UNKNOWN)
1049 nsinfo.source = TrafficSource::SYSTEM;
Hugo Benichifcf81022020-12-04 11:01:37 +09001050 nsinfo.outbound_ifname = request.outbound_physical_device();
Hugo Benichi93306e52020-12-04 16:08:00 +09001051 nsinfo.route_on_vpn = request.route_on_vpn();
Hugo Benichifcf81022020-12-04 11:01:37 +09001052 nsinfo.host_ifname = "arc_ns" + ifname_id;
1053 nsinfo.peer_ifname = "veth" + ifname_id;
1054 nsinfo.peer_subnet = std::move(subnet);
1055 nsinfo.peer_mac_addr = addr_mgr_.GenerateMacAddress();
1056
1057 if (!datapath_->StartRoutingNamespace(nsinfo)) {
Hugo Benichi7c342672020-09-08 09:18:14 +09001058 LOG(ERROR) << "ConnectNamespace: failed to setup datapath";
1059 if (epoll_ctl(connected_namespaces_epollfd_, EPOLL_CTL_DEL,
1060 local_client_fd.get(), nullptr) != 0)
1061 PLOG(ERROR) << "ConnectNamespace: epoll_ctl(EPOLL_CTL_DEL) failed";
1062 return response;
1063 }
1064
Hugo Benichifcf81022020-12-04 11:01:37 +09001065 // Prepare the response before storing ConnectedNamespace.
1066 response->set_peer_ifname(nsinfo.peer_ifname);
1067 response->set_peer_ipv4_address(nsinfo.peer_subnet->AddressAtOffset(1));
1068 response->set_host_ifname(nsinfo.host_ifname);
1069 response->set_host_ipv4_address(nsinfo.peer_subnet->AddressAtOffset(0));
Hugo Benichi7c342672020-09-08 09:18:14 +09001070 auto* response_subnet = response->mutable_ipv4_subnet();
Hugo Benichifcf81022020-12-04 11:01:37 +09001071 response_subnet->set_base_addr(nsinfo.peer_subnet->BaseAddress());
1072 response_subnet->set_prefix_len(nsinfo.peer_subnet->PrefixLength());
Hugo Benichiadf1ec52020-01-17 16:23:58 +09001073
Hugo Benichifcf81022020-12-04 11:01:37 +09001074 LOG(INFO) << "Connected network namespace " << nsinfo;
1075
1076 // Store ConnectedNamespace
Hugo Benichiadf1ec52020-01-17 16:23:58 +09001077 connected_namespaces_next_id_++;
Hugo Benichi7352ad92020-04-07 16:11:59 +09001078 int fdkey = local_client_fd.release();
Hugo Benichifcf81022020-12-04 11:01:37 +09001079 connected_namespaces_.insert(std::make_pair(fdkey, std::move(nsinfo)));
Hugo Benichi7352ad92020-04-07 16:11:59 +09001080
1081 if (connected_namespaces_.size() == 1) {
1082 LOG(INFO) << "Starting ConnectNamespace client fds monitoring";
1083 CheckConnectedNamespaces();
1084 }
Hugo Benichi7c342672020-09-08 09:18:14 +09001085
1086 return response;
Hugo Benichiadf1ec52020-01-17 16:23:58 +09001087}
1088
1089void Manager::DisconnectNamespace(int client_fd) {
1090 auto it = connected_namespaces_.find(client_fd);
1091 if (it == connected_namespaces_.end()) {
Hugo Benichifcf81022020-12-04 11:01:37 +09001092 LOG(ERROR) << "No ConnectedNamespace found for client_fd " << client_fd;
Hugo Benichiadf1ec52020-01-17 16:23:58 +09001093 return;
1094 }
1095
Hugo Benichi7352ad92020-04-07 16:11:59 +09001096 // Remove the client fd dupe from the epoll watcher and close it.
1097 if (epoll_ctl(connected_namespaces_epollfd_, EPOLL_CTL_DEL, client_fd,
Hugo Benichie8758b52020-04-03 14:49:01 +09001098 nullptr) != 0)
Hugo Benichi7352ad92020-04-07 16:11:59 +09001099 PLOG(ERROR) << "DisconnectNamespace: epoll_ctl(EPOLL_CTL_DEL) failed";
Hugo Benichie8758b52020-04-03 14:49:01 +09001100 if (close(client_fd) < 0)
Hugo Benichi7352ad92020-04-07 16:11:59 +09001101 PLOG(ERROR) << "DisconnectNamespace: close(client_fd) failed";
Hugo Benichi7352ad92020-04-07 16:11:59 +09001102
Hugo Benichifcf81022020-12-04 11:01:37 +09001103 datapath_->StopRoutingNamespace(it->second);
Hugo Benichiadf1ec52020-01-17 16:23:58 +09001104 LOG(INFO) << "Disconnected network namespace " << it->second;
Hugo Benichiadf1ec52020-01-17 16:23:58 +09001105 // This release the allocated IPv4 subnet.
1106 connected_namespaces_.erase(it);
1107}
1108
Hugo Benichi7352ad92020-04-07 16:11:59 +09001109// TODO(hugobenichi) Generalize this check to all resources created by
1110// patchpanel on behalf of a remote client.
1111void Manager::CheckConnectedNamespaces() {
1112 int max_event = 10;
1113 struct epoll_event epevents[max_event];
1114 int nready = epoll_wait(connected_namespaces_epollfd_, epevents, max_event,
1115 0 /* do not block */);
1116 if (nready < 0)
1117 PLOG(ERROR) << "CheckConnectedNamespaces: epoll_wait(0) failed";
1118
1119 for (int i = 0; i < nready; i++)
1120 if (epevents[i].events & (EPOLLHUP | EPOLLERR))
1121 DisconnectNamespace(epevents[i].data.fd);
1122
1123 if (connected_namespaces_.empty()) {
1124 LOG(INFO) << "Stopping ConnectNamespace client fds monitoring";
1125 return;
1126 }
1127
Qijiang Fan2d7aeb42020-05-19 02:06:39 +09001128 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
Hugo Benichi7352ad92020-04-07 16:11:59 +09001129 FROM_HERE,
1130 base::Bind(&Manager::CheckConnectedNamespaces,
Hugo Benichie8758b52020-04-03 14:49:01 +09001131 weak_factory_.GetWeakPtr()),
Hugo Benichi7352ad92020-04-07 16:11:59 +09001132 kConnectNamespaceCheckInterval);
1133}
1134
Jason Jeremy Imanc28df852020-07-11 17:38:26 +09001135bool Manager::ModifyPortRule(const patchpanel::ModifyPortRuleRequest& request) {
1136 switch (request.proto()) {
1137 case patchpanel::ModifyPortRuleRequest::TCP:
1138 case patchpanel::ModifyPortRuleRequest::UDP:
1139 break;
1140 default:
1141 LOG(ERROR) << "Unknown protocol " << request.proto();
1142 return false;
1143 }
1144
1145 switch (request.op()) {
1146 case patchpanel::ModifyPortRuleRequest::CREATE:
1147 switch (request.type()) {
1148 case patchpanel::ModifyPortRuleRequest::ACCESS:
1149 return firewall_.AddAcceptRules(request.proto(),
1150 request.input_dst_port(),
1151 request.input_ifname());
1152 case patchpanel::ModifyPortRuleRequest::LOCKDOWN:
1153 return firewall_.AddLoopbackLockdownRules(request.proto(),
1154 request.input_dst_port());
1155 case patchpanel::ModifyPortRuleRequest::FORWARDING:
1156 return firewall_.AddIpv4ForwardRule(
1157 request.proto(), request.input_dst_ip(), request.input_dst_port(),
1158 request.input_ifname(), request.dst_ip(), request.dst_port());
1159 default:
1160 LOG(ERROR) << "Unknown port rule type " << request.type();
1161 return false;
1162 }
1163 case patchpanel::ModifyPortRuleRequest::DELETE:
1164 switch (request.type()) {
1165 case patchpanel::ModifyPortRuleRequest::ACCESS:
1166 return firewall_.DeleteAcceptRules(request.proto(),
1167 request.input_dst_port(),
1168 request.input_ifname());
1169 case patchpanel::ModifyPortRuleRequest::LOCKDOWN:
1170 return firewall_.DeleteLoopbackLockdownRules(
1171 request.proto(), request.input_dst_port());
1172 case patchpanel::ModifyPortRuleRequest::FORWARDING:
1173 return firewall_.DeleteIpv4ForwardRule(
1174 request.proto(), request.input_dst_ip(), request.input_dst_port(),
1175 request.input_ifname(), request.dst_ip(), request.dst_port());
1176 default:
1177 LOG(ERROR) << "Unknown port rule type " << request.type();
1178 return false;
1179 }
1180 default:
1181 LOG(ERROR) << "Unknown operation " << request.op();
1182 return false;
1183 }
1184}
1185
Garrick Evanse94a14e2019-11-11 10:32:13 +09001186void Manager::SendGuestMessage(const GuestMessage& msg) {
Garrick Evans96e03042019-05-28 14:30:52 +09001187 IpHelperMessage ipm;
1188 *ipm.mutable_guest_message() = msg;
Garrick Evans96e03042019-05-28 14:30:52 +09001189 adb_proxy_->SendMessage(ipm);
Garrick Evanse94a14e2019-11-11 10:32:13 +09001190 mcast_proxy_->SendMessage(ipm);
1191 nd_proxy_->SendMessage(ipm);
Garrick Evans96e03042019-05-28 14:30:52 +09001192}
1193
Garrick Evans4ac09852020-01-16 14:09:22 +09001194void Manager::StartForwarding(const std::string& ifname_physical,
Hugo Benichi6f118a32021-03-01 12:28:14 +09001195 const std::string& ifname_virtual) {
Taoyu Li7dca19a2020-03-16 16:27:07 +09001196 if (ifname_physical.empty() || ifname_virtual.empty())
Garrick Evans4ac09852020-01-16 14:09:22 +09001197 return;
1198
1199 IpHelperMessage ipm;
1200 DeviceMessage* msg = ipm.mutable_device_message();
1201 msg->set_dev_ifname(ifname_physical);
Garrick Evans4ac09852020-01-16 14:09:22 +09001202 msg->set_br_ifname(ifname_virtual);
1203
Hugo Benichi6f118a32021-03-01 12:28:14 +09001204 ShillClient::Device upstream_device;
1205 shill_client_->GetDeviceProperties(ifname_physical, &upstream_device);
1206 if (IsIPv6NDProxyEnabled(upstream_device.type)) {
1207 ndproxy_virtual_ifnames_.insert(ifname_virtual);
Garrick Evans4ac09852020-01-16 14:09:22 +09001208 LOG(INFO) << "Starting IPv6 forwarding from " << ifname_physical << " to "
1209 << ifname_virtual;
1210
1211 if (!datapath_->AddIPv6Forwarding(ifname_physical, ifname_virtual)) {
1212 LOG(ERROR) << "Failed to setup iptables forwarding rule for IPv6 from "
1213 << ifname_physical << " to " << ifname_virtual;
1214 }
1215 if (!datapath_->MaskInterfaceFlags(ifname_physical, IFF_ALLMULTI)) {
1216 LOG(WARNING) << "Failed to setup all multicast mode for interface "
1217 << ifname_physical;
1218 }
1219 if (!datapath_->MaskInterfaceFlags(ifname_virtual, IFF_ALLMULTI)) {
1220 LOG(WARNING) << "Failed to setup all multicast mode for interface "
1221 << ifname_virtual;
1222 }
1223 nd_proxy_->SendMessage(ipm);
1224 }
1225
Hugo Benichi6f118a32021-03-01 12:28:14 +09001226 if (IsMulticastInterface(ifname_physical)) {
Hugo Benichi69c989d2021-03-01 00:23:39 +09001227 multicast_virtual_ifnames_.insert(ifname_virtual);
Garrick Evans4ac09852020-01-16 14:09:22 +09001228 LOG(INFO) << "Starting multicast forwarding from " << ifname_physical
1229 << " to " << ifname_virtual;
1230 mcast_proxy_->SendMessage(ipm);
1231 }
1232}
1233
1234void Manager::StopForwarding(const std::string& ifname_physical,
Hugo Benichi6f118a32021-03-01 12:28:14 +09001235 const std::string& ifname_virtual) {
Garrick Evans4ac09852020-01-16 14:09:22 +09001236 if (ifname_physical.empty())
1237 return;
1238
1239 IpHelperMessage ipm;
1240 DeviceMessage* msg = ipm.mutable_device_message();
1241 msg->set_dev_ifname(ifname_physical);
1242 msg->set_teardown(true);
Taoyu Li7dca19a2020-03-16 16:27:07 +09001243 if (!ifname_virtual.empty()) {
1244 msg->set_br_ifname(ifname_virtual);
1245 }
Garrick Evans4ac09852020-01-16 14:09:22 +09001246
Hugo Benichi6f118a32021-03-01 12:28:14 +09001247 if (ndproxy_virtual_ifnames_.find(ifname_virtual) !=
1248 ndproxy_virtual_ifnames_.end()) {
1249 ndproxy_virtual_ifnames_.erase(ifname_virtual);
Taoyu Li7dca19a2020-03-16 16:27:07 +09001250 if (ifname_virtual.empty()) {
1251 LOG(INFO) << "Stopping IPv6 forwarding on " << ifname_physical;
1252 } else {
1253 LOG(INFO) << "Stopping IPv6 forwarding from " << ifname_physical << " to "
1254 << ifname_virtual;
1255 datapath_->RemoveIPv6Forwarding(ifname_physical, ifname_virtual);
1256 }
Garrick Evans4ac09852020-01-16 14:09:22 +09001257 nd_proxy_->SendMessage(ipm);
1258 }
1259
Hugo Benichi6f118a32021-03-01 12:28:14 +09001260 if (multicast_virtual_ifnames_.find(ifname_virtual) !=
1261 multicast_virtual_ifnames_.end()) {
Hugo Benichi69c989d2021-03-01 00:23:39 +09001262 multicast_virtual_ifnames_.erase(ifname_virtual);
Taoyu Li7dca19a2020-03-16 16:27:07 +09001263 if (ifname_virtual.empty()) {
1264 LOG(INFO) << "Stopping multicast forwarding on " << ifname_physical;
1265 } else {
1266 LOG(INFO) << "Stopping multicast forwarding from " << ifname_physical
1267 << " to " << ifname_virtual;
1268 }
Garrick Evans4ac09852020-01-16 14:09:22 +09001269 mcast_proxy_->SendMessage(ipm);
1270 }
1271}
1272
Taoyu Lia0727dc2020-09-24 19:54:59 +09001273void Manager::OnNDProxyMessage(const NDProxyMessage& msg) {
1274 LOG_IF(DFATAL, msg.ifname().empty())
Garrick Evans4ac09852020-01-16 14:09:22 +09001275 << "Received DeviceMessage w/ empty dev_ifname";
Taoyu Lia0727dc2020-09-24 19:54:59 +09001276 switch (msg.type()) {
1277 case NDProxyMessage::ADD_ROUTE:
1278 if (!datapath_->AddIPv6HostRoute(msg.ifname(), msg.ip6addr(), 128)) {
1279 LOG(WARNING) << "Failed to setup the IPv6 route for interface "
1280 << msg.ifname() << ", addr " << msg.ip6addr();
1281 }
1282 break;
1283 case NDProxyMessage::ADD_ADDR:
1284 if (!datapath_->AddIPv6Address(msg.ifname(), msg.ip6addr())) {
1285 LOG(WARNING) << "Failed to setup the IPv6 address for interface "
1286 << msg.ifname() << ", addr " << msg.ip6addr();
1287 }
1288 break;
1289 case NDProxyMessage::DEL_ADDR:
1290 datapath_->RemoveIPv6Address(msg.ifname(), msg.ip6addr());
1291 break;
1292 default:
1293 LOG(ERROR) << "Unknown NDProxy event " << msg.type();
1294 NOTREACHED();
Garrick Evans4ac09852020-01-16 14:09:22 +09001295 }
1296}
1297
Garrick Evans3388a032020-03-24 11:25:55 +09001298} // namespace patchpanel