blob: d53e8a6465e1842c6a74ff2a3a7d034af4e82894 [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 auto* const forwarder = static_cast<TrafficForwarder*>(this);
247
248 GuestMessage::GuestType arc_guest =
249 USE_ARCVM ? GuestMessage::ARC_VM : GuestMessage::ARC;
Garrick Evans209a80a2020-11-30 14:42:40 +0900250 arc_svc_ = std::make_unique<ArcService>(
251 shill_client_.get(), datapath_.get(), &addr_mgr_, forwarder, arc_guest,
252 base::BindRepeating(&Manager::OnDeviceChanged,
253 weak_factory_.GetWeakPtr()));
254 cros_svc_ = std::make_unique<CrostiniService>(
Hugo Benichi69c989d2021-03-01 00:23:39 +0900255 &addr_mgr_, datapath_.get(),
Garrick Evans209a80a2020-11-30 14:42:40 +0900256 base::BindRepeating(&Manager::OnDeviceChanged,
257 weak_factory_.GetWeakPtr()));
Jie Jiang84966852020-09-18 18:49:05 +0900258 network_monitor_svc_ = std::make_unique<NetworkMonitorService>(
259 shill_client_.get(),
Jie Jiang25c1b972020-11-12 15:42:53 +0900260 base::BindRepeating(&Manager::OnNeighborReachabilityEvent,
Jie Jiang84966852020-09-18 18:49:05 +0900261 weak_factory_.GetWeakPtr()));
Hugo Benichifda77232020-08-21 18:28:15 +0900262 network_monitor_svc_->Start();
Hugo Benichi058a26a2020-11-26 10:10:39 +0900263 counters_svc_->Init(shill_client_->get_devices());
Hugo Benichifda77232020-08-21 18:28:15 +0900264 nd_proxy_->Listen();
265}
266
267void Manager::OnShutdown(int* exit_code) {
268 LOG(INFO) << "Shutting down and cleaning up";
Jie Jiang84966852020-09-18 18:49:05 +0900269 network_monitor_svc_.reset();
Hugo Benichifda77232020-08-21 18:28:15 +0900270 cros_svc_.reset();
271 arc_svc_.reset();
272 close(connected_namespaces_epollfd_);
273 // Tear down any remaining connected namespace.
274 std::vector<int> connected_namespaces_fdkeys;
275 for (const auto& kv : connected_namespaces_)
276 connected_namespaces_fdkeys.push_back(kv.first);
277 for (const int fdkey : connected_namespaces_fdkeys)
278 DisconnectNamespace(fdkey);
Hugo Benichibf811c62020-09-07 17:30:45 +0900279 if (!USE_JETSTREAM_ROUTING)
280 datapath_->Stop();
Jie Jiang00592cf2020-12-17 11:23:49 +0900281
282 brillo::DBusDaemon::OnShutdown(exit_code);
Hugo Benichifda77232020-08-21 18:28:15 +0900283}
284
285void Manager::OnSubprocessExited(pid_t pid, const siginfo_t&) {
286 LOG(ERROR) << "Subprocess " << pid << " exited unexpectedly -"
287 << " attempting to restart";
288
289 HelperProcess* proc;
290 if (pid == adb_proxy_->pid()) {
291 proc = adb_proxy_.get();
292 } else if (pid == mcast_proxy_->pid()) {
293 proc = mcast_proxy_.get();
294 } else if (pid == nd_proxy_->pid()) {
295 proc = nd_proxy_.get();
296 } else {
297 LOG(DFATAL) << "Unknown child process";
298 return;
299 }
300
301 process_reaper_.ForgetChild(pid);
302
303 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
304 FROM_HERE,
305 base::Bind(&Manager::RestartSubprocess, weak_factory_.GetWeakPtr(), proc),
306 base::TimeDelta::FromMilliseconds((2 << proc->restarts()) *
307 kSubprocessRestartDelayMs));
308}
309
310void Manager::RestartSubprocess(HelperProcess* subproc) {
311 if (subproc->Restart()) {
312 DCHECK(process_reaper_.WatchForChild(
313 FROM_HERE, subproc->pid(),
314 base::Bind(&Manager::OnSubprocessExited, weak_factory_.GetWeakPtr(),
315 subproc->pid())))
316 << "Failed to watch child process " << subproc->pid();
317 }
318}
319
Hugo Benichi2a940542020-10-26 18:50:49 +0900320void Manager::OnDefaultDeviceChanged(const ShillClient::Device& new_device,
321 const ShillClient::Device& prev_device) {
322 // Only take into account interface switches and ignore layer 3 property
323 // changes.
324 if (prev_device.ifname == new_device.ifname)
325 return;
326
Hugo Benichi058a26a2020-11-26 10:10:39 +0900327 if (prev_device.type == ShillClient::Device::Type::kVPN) {
Hugo Benichi2a940542020-10-26 18:50:49 +0900328 datapath_->StopVpnRouting(prev_device.ifname);
Hugo Benichi058a26a2020-11-26 10:10:39 +0900329 counters_svc_->OnVpnDeviceRemoved(prev_device.ifname);
330 }
Hugo Benichi2a940542020-10-26 18:50:49 +0900331
Hugo Benichi058a26a2020-11-26 10:10:39 +0900332 if (new_device.type == ShillClient::Device::Type::kVPN) {
Hugo Benichi2a940542020-10-26 18:50:49 +0900333 datapath_->StartVpnRouting(new_device.ifname);
Hugo Benichi058a26a2020-11-26 10:10:39 +0900334 counters_svc_->OnVpnDeviceAdded(new_device.ifname);
335 }
Hugo Benichi69c989d2021-03-01 00:23:39 +0900336
337 // When the default logical network changes, Crostini's tap devices must leave
338 // their current forwarding group for multicast and IPv6 ndproxy and join the
339 // forwarding group of the new logical default network.
340 for (const auto* tap_device : cros_svc_->GetDevices()) {
341 bool was_multicast =
342 multicast_virtual_ifnames_.find(tap_device->host_ifname()) !=
343 multicast_virtual_ifnames_.end();
344 StopForwarding(prev_device.ifname, tap_device->host_ifname(),
345 IsIPv6NDProxyEnabled(prev_device.type), was_multicast);
346 StartForwarding(new_device.ifname, tap_device->host_ifname(),
347 IsIPv6NDProxyEnabled(new_device.type),
348 IsMulticastInterface(new_device.ifname));
349 }
Hugo Benichi2a940542020-10-26 18:50:49 +0900350}
351
Hugo Benichi76be34a2020-08-26 22:35:54 +0900352void Manager::OnDevicesChanged(const std::set<std::string>& added,
353 const std::set<std::string>& removed) {
Hugo Benichi058a26a2020-11-26 10:10:39 +0900354 for (const std::string& ifname : removed) {
Hugo Benichi76be34a2020-08-26 22:35:54 +0900355 datapath_->StopConnectionPinning(ifname);
Hugo Benichi1e0656f2021-02-15 15:43:38 +0900356 datapath_->RemoveRedirectDnsRule(ifname);
Hugo Benichi058a26a2020-11-26 10:10:39 +0900357 counters_svc_->OnPhysicalDeviceRemoved(ifname);
358 }
Hugo Benichi76be34a2020-08-26 22:35:54 +0900359
Hugo Benichi058a26a2020-11-26 10:10:39 +0900360 for (const std::string& ifname : added) {
Hugo Benichi76be34a2020-08-26 22:35:54 +0900361 datapath_->StartConnectionPinning(ifname);
Hugo Benichi1e0656f2021-02-15 15:43:38 +0900362 ShillClient::Device device;
363 if (!shill_client_->GetDeviceProperties(ifname, &device))
364 continue;
365
366 if (!device.ipconfig.ipv4_dns_addresses.empty())
367 datapath_->AddRedirectDnsRule(ifname,
368 device.ipconfig.ipv4_dns_addresses.front());
369
Hugo Benichi058a26a2020-11-26 10:10:39 +0900370 counters_svc_->OnPhysicalDeviceAdded(ifname);
371 }
Hugo Benichi76be34a2020-08-26 22:35:54 +0900372}
373
Hugo Benichi1e0656f2021-02-15 15:43:38 +0900374void Manager::OnIPConfigsChanged(const std::string& device,
375 const ShillClient::IPConfig& ipconfig) {
376 if (ipconfig.ipv4_dns_addresses.empty()) {
377 datapath_->RemoveRedirectDnsRule(device);
378 } else {
379 datapath_->AddRedirectDnsRule(device, ipconfig.ipv4_dns_addresses.front());
380 }
381}
382
Garrick Evans209a80a2020-11-30 14:42:40 +0900383void Manager::OnDeviceChanged(const Device& device,
384 Device::ChangeEvent event,
385 GuestMessage::GuestType guest_type) {
386 dbus::Signal signal(kPatchPanelInterface, kNetworkDeviceChangedSignal);
387 NetworkDeviceChangedSignal proto;
388 proto.set_event(event == Device::ChangeEvent::ADDED
389 ? NetworkDeviceChangedSignal::DEVICE_ADDED
390 : NetworkDeviceChangedSignal::DEVICE_REMOVED);
391 auto* dev = proto.mutable_device();
392 dev->set_ifname(device.host_ifname());
Garrick Evans9ccd2a62021-02-18 14:48:28 +0900393 dev->set_phys_ifname(device.phys_ifname());
Garrick Evans209a80a2020-11-30 14:42:40 +0900394 dev->set_ipv4_addr(device.config().guest_ipv4_addr());
395 if (const auto* subnet = device.config().ipv4_subnet()) {
396 auto* sub = dev->mutable_ipv4_subnet();
397 sub->set_base_addr(subnet->BaseAddress());
398 sub->set_prefix_len(subnet->PrefixLength());
399 }
400 switch (guest_type) {
401 case GuestMessage::ARC:
402 dev->set_guest_type(NetworkDevice::ARC);
403 break;
404 case GuestMessage::ARC_VM:
405 dev->set_guest_type(NetworkDevice::ARCVM);
406 break;
407 case GuestMessage::TERMINA_VM:
408 dev->set_guest_type(NetworkDevice::TERMINA_VM);
409 break;
410 case GuestMessage::PLUGIN_VM:
411 dev->set_guest_type(NetworkDevice::PLUGIN_VM);
412 break;
413 default:
414 LOG(ERROR) << "Unknown device type";
415 }
416
Hugo Benichi69c989d2021-03-01 00:23:39 +0900417 if (guest_type == GuestMessage::TERMINA_VM ||
418 guest_type == GuestMessage::PLUGIN_VM) {
419 const ShillClient::Device& default_device = shill_client_->default_device();
420 if (event == Device::ChangeEvent::ADDED) {
421 StartForwarding(default_device.ifname, device.host_ifname(),
422 IsIPv6NDProxyEnabled(default_device.type),
423 IsMulticastInterface(default_device.ifname));
424 } else if (event == Device::ChangeEvent::REMOVED) {
425 bool was_multicast =
426 multicast_virtual_ifnames_.find(device.host_ifname()) !=
427 multicast_virtual_ifnames_.end();
428 StopForwarding(default_device.ifname, device.host_ifname(),
429 IsIPv6NDProxyEnabled(default_device.type), was_multicast);
430 }
431 }
432
Garrick Evans209a80a2020-11-30 14:42:40 +0900433 dbus::MessageWriter(&signal).AppendProtoAsArrayOfBytes(proto);
434 dbus_svc_path_->SendSignal(&signal);
435}
436
Garrick Evanse94a14e2019-11-11 10:32:13 +0900437bool Manager::StartArc(pid_t pid) {
Garrick Evans508a4bc2019-11-14 08:45:52 +0900438 if (!arc_svc_->Start(pid))
439 return false;
Garrick Evanse94a14e2019-11-11 10:32:13 +0900440
441 GuestMessage msg;
442 msg.set_event(GuestMessage::START);
443 msg.set_type(GuestMessage::ARC);
444 msg.set_arc_pid(pid);
445 SendGuestMessage(msg);
446
447 return true;
448}
449
Hugo Benichi4d4bb8f2020-07-07 12:16:07 +0900450void Manager::StopArc() {
Garrick Evanse94a14e2019-11-11 10:32:13 +0900451 GuestMessage msg;
452 msg.set_event(GuestMessage::STOP);
453 msg.set_type(GuestMessage::ARC);
454 SendGuestMessage(msg);
455
Hugo Benichi4d4bb8f2020-07-07 12:16:07 +0900456 // After the ARC container has stopped, the pid is not known anymore.
457 // The pid argument is ignored by ArcService.
458 arc_svc_->Stop(0);
Garrick Evanse94a14e2019-11-11 10:32:13 +0900459}
460
Garrick Evans015b0d62020-02-07 09:06:38 +0900461bool Manager::StartArcVm(uint32_t cid) {
Garrick Evans508a4bc2019-11-14 08:45:52 +0900462 if (!arc_svc_->Start(cid))
463 return false;
Garrick Evanse94a14e2019-11-11 10:32:13 +0900464
465 GuestMessage msg;
466 msg.set_event(GuestMessage::START);
467 msg.set_type(GuestMessage::ARC_VM);
468 msg.set_arcvm_vsock_cid(cid);
469 SendGuestMessage(msg);
470
471 return true;
472}
473
Garrick Evans015b0d62020-02-07 09:06:38 +0900474void Manager::StopArcVm(uint32_t cid) {
Garrick Evanse94a14e2019-11-11 10:32:13 +0900475 GuestMessage msg;
476 msg.set_event(GuestMessage::STOP);
477 msg.set_type(GuestMessage::ARC_VM);
478 SendGuestMessage(msg);
479
Garrick Evans21173b12019-11-20 15:23:16 +0900480 arc_svc_->Stop(cid);
Garrick Evanse94a14e2019-11-11 10:32:13 +0900481}
482
Garrick Evans51d5b552020-01-30 10:42:06 +0900483bool Manager::StartCrosVm(uint64_t vm_id,
484 GuestMessage::GuestType vm_type,
Garrick Evans53a2a982020-02-05 10:53:35 +0900485 uint32_t subnet_index) {
Garrick Evans51d5b552020-01-30 10:42:06 +0900486 DCHECK(vm_type == GuestMessage::TERMINA_VM ||
487 vm_type == GuestMessage::PLUGIN_VM);
488
489 if (!cros_svc_->Start(vm_id, vm_type == GuestMessage::TERMINA_VM,
490 subnet_index))
Garrick Evans47c19272019-11-21 10:58:21 +0900491 return false;
492
493 GuestMessage msg;
494 msg.set_event(GuestMessage::START);
Garrick Evans51d5b552020-01-30 10:42:06 +0900495 msg.set_type(vm_type);
Garrick Evans47c19272019-11-21 10:58:21 +0900496 SendGuestMessage(msg);
497
498 return true;
499}
500
Garrick Evans51d5b552020-01-30 10:42:06 +0900501void Manager::StopCrosVm(uint64_t vm_id, GuestMessage::GuestType vm_type) {
Garrick Evans47c19272019-11-21 10:58:21 +0900502 GuestMessage msg;
503 msg.set_event(GuestMessage::STOP);
Garrick Evans51d5b552020-01-30 10:42:06 +0900504 msg.set_type(vm_type);
Garrick Evans47c19272019-11-21 10:58:21 +0900505 SendGuestMessage(msg);
506
Garrick Evans51d5b552020-01-30 10:42:06 +0900507 cros_svc_->Stop(vm_id, vm_type == GuestMessage::TERMINA_VM);
Garrick Evans47c19272019-11-21 10:58:21 +0900508}
509
Garrick Evans02e6e872020-11-30 11:53:13 +0900510std::unique_ptr<dbus::Response> Manager::OnGetDevices(
511 dbus::MethodCall* method_call) {
512 std::unique_ptr<dbus::Response> dbus_response(
513 dbus::Response::FromMethodCall(method_call));
514
515 dbus::MessageReader reader(method_call);
516 dbus::MessageWriter writer(dbus_response.get());
517
518 patchpanel::GetDevicesRequest request;
519 patchpanel::GetDevicesResponse response;
520
521 if (!reader.PopArrayOfBytesAsProto(&request)) {
522 LOG(ERROR) << "Unable to parse request";
523 writer.AppendProtoAsArrayOfBytes(response);
524 return dbus_response;
525 }
526
527 static const auto arc_guest_type =
528 USE_ARCVM ? NetworkDevice::ARCVM : NetworkDevice::ARC;
529
530 arc_svc_->ScanDevices(base::BindRepeating(
531 [](patchpanel::GetDevicesResponse* resp, const Device& device) {
532 auto* dev = resp->add_devices();
533 dev->set_ifname(device.host_ifname());
Garrick Evans9ccd2a62021-02-18 14:48:28 +0900534 dev->set_phys_ifname(device.phys_ifname());
Garrick Evans02e6e872020-11-30 11:53:13 +0900535 dev->set_ipv4_addr(device.config().guest_ipv4_addr());
536 dev->set_guest_type(arc_guest_type);
537 if (const auto* subnet = device.config().ipv4_subnet()) {
538 auto* sub = dev->mutable_ipv4_subnet();
539 sub->set_base_addr(subnet->BaseAddress());
540 sub->set_prefix_len(subnet->PrefixLength());
541 }
542 },
543 &response));
544
545 cros_svc_->ScanDevices(base::BindRepeating(
546 [](patchpanel::GetDevicesResponse* resp, uint64_t vm_id, bool is_termina,
547 const Device& device) {
548 auto* dev = resp->add_devices();
549 dev->set_ifname(device.host_ifname());
Garrick Evans9ccd2a62021-02-18 14:48:28 +0900550 dev->set_phys_ifname(device.phys_ifname());
Garrick Evans02e6e872020-11-30 11:53:13 +0900551 dev->set_ipv4_addr(device.config().guest_ipv4_addr());
552 dev->set_guest_type(is_termina ? NetworkDevice::TERMINA_VM
553 : NetworkDevice::PLUGIN_VM);
554 if (const auto* subnet = device.config().ipv4_subnet()) {
555 auto* sub = dev->mutable_ipv4_subnet();
556 sub->set_base_addr(subnet->BaseAddress());
557 sub->set_prefix_len(subnet->PrefixLength());
558 }
559 },
560 &response));
561
562 writer.AppendProtoAsArrayOfBytes(response);
563 return dbus_response;
564}
565
Garrick Evans08843932019-09-17 14:41:08 +0900566std::unique_ptr<dbus::Response> Manager::OnArcStartup(
567 dbus::MethodCall* method_call) {
568 LOG(INFO) << "ARC++ starting up";
569
570 std::unique_ptr<dbus::Response> dbus_response(
571 dbus::Response::FromMethodCall(method_call));
572
573 dbus::MessageReader reader(method_call);
574 dbus::MessageWriter writer(dbus_response.get());
575
576 patchpanel::ArcStartupRequest request;
577 patchpanel::ArcStartupResponse response;
578
579 if (!reader.PopArrayOfBytesAsProto(&request)) {
580 LOG(ERROR) << "Unable to parse request";
581 writer.AppendProtoAsArrayOfBytes(response);
582 return dbus_response;
583 }
584
Garrick Evanse01bf072019-11-15 09:08:19 +0900585 if (!StartArc(request.pid()))
586 LOG(ERROR) << "Failed to start ARC++ network service";
Garrick Evanse94a14e2019-11-11 10:32:13 +0900587
Garrick Evans08843932019-09-17 14:41:08 +0900588 writer.AppendProtoAsArrayOfBytes(response);
589 return dbus_response;
590}
591
592std::unique_ptr<dbus::Response> Manager::OnArcShutdown(
593 dbus::MethodCall* method_call) {
594 LOG(INFO) << "ARC++ shutting down";
595
596 std::unique_ptr<dbus::Response> dbus_response(
597 dbus::Response::FromMethodCall(method_call));
598
599 dbus::MessageReader reader(method_call);
600 dbus::MessageWriter writer(dbus_response.get());
601
602 patchpanel::ArcShutdownRequest request;
603 patchpanel::ArcShutdownResponse response;
604
605 if (!reader.PopArrayOfBytesAsProto(&request)) {
606 LOG(ERROR) << "Unable to parse request";
607 writer.AppendProtoAsArrayOfBytes(response);
608 return dbus_response;
609 }
610
Hugo Benichi4d4bb8f2020-07-07 12:16:07 +0900611 StopArc();
Garrick Evanse94a14e2019-11-11 10:32:13 +0900612
Garrick Evans08843932019-09-17 14:41:08 +0900613 writer.AppendProtoAsArrayOfBytes(response);
614 return dbus_response;
615}
616
617std::unique_ptr<dbus::Response> Manager::OnArcVmStartup(
618 dbus::MethodCall* method_call) {
619 LOG(INFO) << "ARCVM starting up";
620
621 std::unique_ptr<dbus::Response> dbus_response(
622 dbus::Response::FromMethodCall(method_call));
623
624 dbus::MessageReader reader(method_call);
625 dbus::MessageWriter writer(dbus_response.get());
626
627 patchpanel::ArcVmStartupRequest request;
628 patchpanel::ArcVmStartupResponse response;
629
630 if (!reader.PopArrayOfBytesAsProto(&request)) {
631 LOG(ERROR) << "Unable to parse request";
632 writer.AppendProtoAsArrayOfBytes(response);
633 return dbus_response;
634 }
635
Garrick Evans47c19272019-11-21 10:58:21 +0900636 if (!StartArcVm(request.cid())) {
Garrick Evanse01bf072019-11-15 09:08:19 +0900637 LOG(ERROR) << "Failed to start ARCVM network service";
Garrick Evans47c19272019-11-21 10:58:21 +0900638 writer.AppendProtoAsArrayOfBytes(response);
639 return dbus_response;
Garrick Evanse01bf072019-11-15 09:08:19 +0900640 }
Garrick Evanse94a14e2019-11-11 10:32:13 +0900641
Garrick Evans47c19272019-11-21 10:58:21 +0900642 // Populate the response with the known devices.
Garrick Evans38b25a42020-04-06 15:17:42 +0900643 for (const auto* config : arc_svc_->GetDeviceConfigs()) {
644 if (config->tap_ifname().empty())
645 continue;
Garrick Evans47c19272019-11-21 10:58:21 +0900646
Garrick Evans38b25a42020-04-06 15:17:42 +0900647 auto* dev = response.add_devices();
648 dev->set_ifname(config->tap_ifname());
649 dev->set_ipv4_addr(config->guest_ipv4_addr());
Garrick Evansa0c9c972020-11-30 09:54:59 +0900650 dev->set_guest_type(NetworkDevice::ARCVM);
Garrick Evans38b25a42020-04-06 15:17:42 +0900651 }
Garrick Evanse94b6de2020-02-20 09:19:13 +0900652
Garrick Evans08843932019-09-17 14:41:08 +0900653 writer.AppendProtoAsArrayOfBytes(response);
654 return dbus_response;
655}
656
657std::unique_ptr<dbus::Response> Manager::OnArcVmShutdown(
658 dbus::MethodCall* method_call) {
659 LOG(INFO) << "ARCVM shutting down";
660
661 std::unique_ptr<dbus::Response> dbus_response(
662 dbus::Response::FromMethodCall(method_call));
663
664 dbus::MessageReader reader(method_call);
665 dbus::MessageWriter writer(dbus_response.get());
666
667 patchpanel::ArcVmShutdownRequest request;
668 patchpanel::ArcVmShutdownResponse response;
669
670 if (!reader.PopArrayOfBytesAsProto(&request)) {
671 LOG(ERROR) << "Unable to parse request";
672 writer.AppendProtoAsArrayOfBytes(response);
673 return dbus_response;
674 }
675
Garrick Evans21173b12019-11-20 15:23:16 +0900676 StopArcVm(request.cid());
Garrick Evanse94a14e2019-11-11 10:32:13 +0900677
Garrick Evans08843932019-09-17 14:41:08 +0900678 writer.AppendProtoAsArrayOfBytes(response);
679 return dbus_response;
680}
681
Garrick Evans47c19272019-11-21 10:58:21 +0900682std::unique_ptr<dbus::Response> Manager::OnTerminaVmStartup(
683 dbus::MethodCall* method_call) {
684 LOG(INFO) << "Termina VM starting up";
685
686 std::unique_ptr<dbus::Response> dbus_response(
687 dbus::Response::FromMethodCall(method_call));
688
689 dbus::MessageReader reader(method_call);
690 dbus::MessageWriter writer(dbus_response.get());
691
692 patchpanel::TerminaVmStartupRequest request;
693 patchpanel::TerminaVmStartupResponse response;
694
695 if (!reader.PopArrayOfBytesAsProto(&request)) {
696 LOG(ERROR) << "Unable to parse request";
697 writer.AppendProtoAsArrayOfBytes(response);
698 return dbus_response;
699 }
700
701 const int32_t cid = request.cid();
Garrick Evans53a2a982020-02-05 10:53:35 +0900702 if (!StartCrosVm(cid, GuestMessage::TERMINA_VM)) {
Garrick Evans47c19272019-11-21 10:58:21 +0900703 LOG(ERROR) << "Failed to start Termina VM network service";
704 writer.AppendProtoAsArrayOfBytes(response);
705 return dbus_response;
706 }
707
Garrick Evans51d5b552020-01-30 10:42:06 +0900708 const auto* const tap = cros_svc_->TAP(cid, true /*is_termina*/);
Garrick Evansb1c93712020-01-22 09:28:25 +0900709 if (!tap) {
710 LOG(DFATAL) << "TAP device missing";
711 writer.AppendProtoAsArrayOfBytes(response);
712 return dbus_response;
713 }
Garrick Evans47c19272019-11-21 10:58:21 +0900714
Garrick Evansb1c93712020-01-22 09:28:25 +0900715 auto* dev = response.mutable_device();
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900716 dev->set_ifname(tap->host_ifname());
Garrick Evansa0c9c972020-11-30 09:54:59 +0900717 dev->set_guest_type(NetworkDevice::TERMINA_VM);
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900718 const auto* subnet = tap->config().ipv4_subnet();
Garrick Evansb1c93712020-01-22 09:28:25 +0900719 if (!subnet) {
720 LOG(DFATAL) << "Missing required subnet for {cid: " << cid << "}";
721 writer.AppendProtoAsArrayOfBytes(response);
722 return dbus_response;
723 }
724 auto* resp_subnet = dev->mutable_ipv4_subnet();
725 resp_subnet->set_base_addr(subnet->BaseAddress());
726 resp_subnet->set_prefix_len(subnet->PrefixLength());
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900727 subnet = tap->config().lxd_ipv4_subnet();
Garrick Evansb1c93712020-01-22 09:28:25 +0900728 if (!subnet) {
729 LOG(DFATAL) << "Missing required lxd subnet for {cid: " << cid << "}";
730 writer.AppendProtoAsArrayOfBytes(response);
731 return dbus_response;
732 }
733 resp_subnet = response.mutable_container_subnet();
734 resp_subnet->set_base_addr(subnet->BaseAddress());
735 resp_subnet->set_prefix_len(subnet->PrefixLength());
Garrick Evans47c19272019-11-21 10:58:21 +0900736
737 writer.AppendProtoAsArrayOfBytes(response);
738 return dbus_response;
739}
740
741std::unique_ptr<dbus::Response> Manager::OnTerminaVmShutdown(
742 dbus::MethodCall* method_call) {
743 LOG(INFO) << "Termina VM shutting down";
744
745 std::unique_ptr<dbus::Response> dbus_response(
746 dbus::Response::FromMethodCall(method_call));
747
748 dbus::MessageReader reader(method_call);
749 dbus::MessageWriter writer(dbus_response.get());
750
751 patchpanel::TerminaVmShutdownRequest request;
752 patchpanel::TerminaVmShutdownResponse response;
753
754 if (!reader.PopArrayOfBytesAsProto(&request)) {
755 LOG(ERROR) << "Unable to parse request";
756 writer.AppendProtoAsArrayOfBytes(response);
757 return dbus_response;
758 }
759
Garrick Evans51d5b552020-01-30 10:42:06 +0900760 StopCrosVm(request.cid(), GuestMessage::TERMINA_VM);
761
762 writer.AppendProtoAsArrayOfBytes(response);
763 return dbus_response;
764}
765
766std::unique_ptr<dbus::Response> Manager::OnPluginVmStartup(
767 dbus::MethodCall* method_call) {
768 LOG(INFO) << "Plugin VM starting up";
769
770 std::unique_ptr<dbus::Response> dbus_response(
771 dbus::Response::FromMethodCall(method_call));
772
773 dbus::MessageReader reader(method_call);
774 dbus::MessageWriter writer(dbus_response.get());
775
776 patchpanel::PluginVmStartupRequest request;
777 patchpanel::PluginVmStartupResponse response;
778
779 if (!reader.PopArrayOfBytesAsProto(&request)) {
780 LOG(ERROR) << "Unable to parse request";
781 writer.AppendProtoAsArrayOfBytes(response);
782 return dbus_response;
783 }
784
Garrick Evans08fb34b2020-02-20 10:50:17 +0900785 const uint64_t vm_id = request.id();
Garrick Evans53a2a982020-02-05 10:53:35 +0900786 if (!StartCrosVm(vm_id, GuestMessage::PLUGIN_VM, request.subnet_index())) {
Garrick Evans51d5b552020-01-30 10:42:06 +0900787 LOG(ERROR) << "Failed to start Plugin VM network service";
788 writer.AppendProtoAsArrayOfBytes(response);
789 return dbus_response;
790 }
791
792 const auto* const tap = cros_svc_->TAP(vm_id, false /*is_termina*/);
793 if (!tap) {
794 LOG(DFATAL) << "TAP device missing";
795 writer.AppendProtoAsArrayOfBytes(response);
796 return dbus_response;
797 }
798
Garrick Evans51d5b552020-01-30 10:42:06 +0900799 auto* dev = response.mutable_device();
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900800 dev->set_ifname(tap->host_ifname());
Garrick Evansa0c9c972020-11-30 09:54:59 +0900801 dev->set_guest_type(NetworkDevice::PLUGIN_VM);
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900802 const auto* subnet = tap->config().ipv4_subnet();
Garrick Evans51d5b552020-01-30 10:42:06 +0900803 if (!subnet) {
804 LOG(DFATAL) << "Missing required subnet for {cid: " << vm_id << "}";
805 writer.AppendProtoAsArrayOfBytes(response);
806 return dbus_response;
807 }
808 auto* resp_subnet = dev->mutable_ipv4_subnet();
809 resp_subnet->set_base_addr(subnet->BaseAddress());
810 resp_subnet->set_prefix_len(subnet->PrefixLength());
811
812 writer.AppendProtoAsArrayOfBytes(response);
813 return dbus_response;
814}
815
816std::unique_ptr<dbus::Response> Manager::OnPluginVmShutdown(
817 dbus::MethodCall* method_call) {
818 LOG(INFO) << "Plugin VM shutting down";
819
820 std::unique_ptr<dbus::Response> dbus_response(
821 dbus::Response::FromMethodCall(method_call));
822
823 dbus::MessageReader reader(method_call);
824 dbus::MessageWriter writer(dbus_response.get());
825
826 patchpanel::PluginVmShutdownRequest request;
827 patchpanel::PluginVmShutdownResponse response;
828
829 if (!reader.PopArrayOfBytesAsProto(&request)) {
830 LOG(ERROR) << "Unable to parse request";
831 writer.AppendProtoAsArrayOfBytes(response);
832 return dbus_response;
833 }
834
835 StopCrosVm(request.id(), GuestMessage::PLUGIN_VM);
Garrick Evans47c19272019-11-21 10:58:21 +0900836
837 writer.AppendProtoAsArrayOfBytes(response);
838 return dbus_response;
839}
840
Hugo Benichi7d9d8db2020-03-30 15:56:56 +0900841std::unique_ptr<dbus::Response> Manager::OnSetVpnIntent(
842 dbus::MethodCall* method_call) {
843 std::unique_ptr<dbus::Response> dbus_response(
844 dbus::Response::FromMethodCall(method_call));
845
846 dbus::MessageReader reader(method_call);
847 dbus::MessageWriter writer(dbus_response.get());
848
849 patchpanel::SetVpnIntentRequest request;
850 patchpanel::SetVpnIntentResponse response;
851
852 bool success = reader.PopArrayOfBytesAsProto(&request);
853 if (!success) {
854 LOG(ERROR) << "Unable to parse SetVpnIntentRequest";
855 // Do not return yet to make sure we close the received fd.
856 }
857
858 base::ScopedFD client_socket;
859 reader.PopFileDescriptor(&client_socket);
860
861 if (success)
862 success = routing_svc_->SetVpnFwmark(client_socket.get(), request.policy());
863
864 response.set_success(success);
Hugo Benichib56b77c2020-01-15 16:00:56 +0900865
866 writer.AppendProtoAsArrayOfBytes(response);
867 return dbus_response;
868}
869
870std::unique_ptr<dbus::Response> Manager::OnConnectNamespace(
871 dbus::MethodCall* method_call) {
872 std::unique_ptr<dbus::Response> dbus_response(
873 dbus::Response::FromMethodCall(method_call));
874
875 dbus::MessageReader reader(method_call);
876 dbus::MessageWriter writer(dbus_response.get());
877
878 patchpanel::ConnectNamespaceRequest request;
Hugo Benichib56b77c2020-01-15 16:00:56 +0900879
880 if (!reader.PopArrayOfBytesAsProto(&request)) {
Hugo Benichicc6850f2020-01-17 13:26:06 +0900881 LOG(ERROR) << "Unable to parse ConnectNamespaceRequest";
882 // Do not return yet to make sure we close the received fd and
883 // validate other arguments.
Hugo Benichi7c342672020-09-08 09:18:14 +0900884 writer.AppendProtoAsArrayOfBytes(patchpanel::ConnectNamespaceResponse());
885 return dbus_response;
Hugo Benichib56b77c2020-01-15 16:00:56 +0900886 }
887
Hugo Benichicc6850f2020-01-17 13:26:06 +0900888 base::ScopedFD client_fd;
889 reader.PopFileDescriptor(&client_fd);
890 if (!client_fd.is_valid()) {
891 LOG(ERROR) << "ConnectNamespaceRequest: invalid file descriptor";
Hugo Benichi7c342672020-09-08 09:18:14 +0900892 writer.AppendProtoAsArrayOfBytes(patchpanel::ConnectNamespaceResponse());
893 return dbus_response;
Hugo Benichicc6850f2020-01-17 13:26:06 +0900894 }
895
896 pid_t pid = request.pid();
Garrick Evans263bd952020-12-03 20:27:43 +0900897 if (pid == 1 || pid == getpid()) {
898 LOG(ERROR) << "ConnectNamespaceRequest: privileged namespace pid " << pid;
899 writer.AppendProtoAsArrayOfBytes(patchpanel::ConnectNamespaceResponse());
900 return dbus_response;
901 }
Hugo Benichicc6850f2020-01-17 13:26:06 +0900902 {
Hugo Benichi0781d402021-02-22 13:43:11 +0900903 ScopedNS ns(pid, ScopedNS::Type::Network);
Hugo Benichicc6850f2020-01-17 13:26:06 +0900904 if (!ns.IsValid()) {
905 LOG(ERROR) << "ConnectNamespaceRequest: invalid namespace pid " << pid;
Hugo Benichi7c342672020-09-08 09:18:14 +0900906 writer.AppendProtoAsArrayOfBytes(patchpanel::ConnectNamespaceResponse());
907 return dbus_response;
Hugo Benichicc6850f2020-01-17 13:26:06 +0900908 }
909 }
910
911 const std::string& outbound_ifname = request.outbound_physical_device();
912 if (!outbound_ifname.empty() && !shill_client_->has_device(outbound_ifname)) {
913 LOG(ERROR) << "ConnectNamespaceRequest: invalid outbound ifname "
914 << outbound_ifname;
Hugo Benichi7c342672020-09-08 09:18:14 +0900915 writer.AppendProtoAsArrayOfBytes(patchpanel::ConnectNamespaceResponse());
916 return dbus_response;
Hugo Benichicc6850f2020-01-17 13:26:06 +0900917 }
918
Hugo Benichi7c342672020-09-08 09:18:14 +0900919 auto response = ConnectNamespace(std::move(client_fd), request);
920 writer.AppendProtoAsArrayOfBytes(*response);
Hugo Benichi7d9d8db2020-03-30 15:56:56 +0900921 return dbus_response;
922}
923
Jie Jiang493cde42020-07-17 21:43:39 +0900924std::unique_ptr<dbus::Response> Manager::OnGetTrafficCounters(
925 dbus::MethodCall* method_call) {
926 std::unique_ptr<dbus::Response> dbus_response(
927 dbus::Response::FromMethodCall(method_call));
928
929 dbus::MessageReader reader(method_call);
930 dbus::MessageWriter writer(dbus_response.get());
931
932 patchpanel::TrafficCountersRequest request;
933 patchpanel::TrafficCountersResponse response;
934
935 if (!reader.PopArrayOfBytesAsProto(&request)) {
936 LOG(ERROR) << "Unable to parse TrafficCountersRequest";
937 writer.AppendProtoAsArrayOfBytes(response);
938 return dbus_response;
939 }
940
941 const std::set<std::string> devices{request.devices().begin(),
942 request.devices().end()};
943 const auto counters = counters_svc_->GetCounters(devices);
944 for (const auto& kv : counters) {
945 auto* traffic_counter = response.add_counters();
946 const auto& source_and_device = kv.first;
947 const auto& counter = kv.second;
948 traffic_counter->set_source(source_and_device.first);
949 traffic_counter->set_device(source_and_device.second);
950 traffic_counter->set_rx_bytes(counter.rx_bytes);
951 traffic_counter->set_rx_packets(counter.rx_packets);
952 traffic_counter->set_tx_bytes(counter.tx_bytes);
953 traffic_counter->set_tx_packets(counter.tx_packets);
954 }
955
956 writer.AppendProtoAsArrayOfBytes(response);
957 return dbus_response;
958}
959
Jason Jeremy Imanb2a421e2020-07-04 20:48:32 +0900960std::unique_ptr<dbus::Response> Manager::OnModifyPortRule(
961 dbus::MethodCall* method_call) {
962 std::unique_ptr<dbus::Response> dbus_response(
963 dbus::Response::FromMethodCall(method_call));
964
965 dbus::MessageReader reader(method_call);
966 dbus::MessageWriter writer(dbus_response.get());
967
968 patchpanel::ModifyPortRuleRequest request;
969 patchpanel::ModifyPortRuleResponse response;
970
971 if (!reader.PopArrayOfBytesAsProto(&request)) {
972 LOG(ERROR) << "Unable to parse ModifyPortRequest";
973 writer.AppendProtoAsArrayOfBytes(response);
974 return dbus_response;
975 }
976
Jason Jeremy Imanc28df852020-07-11 17:38:26 +0900977 response.set_success(ModifyPortRule(request));
Jason Jeremy Imanb2a421e2020-07-04 20:48:32 +0900978 writer.AppendProtoAsArrayOfBytes(response);
979 return dbus_response;
980}
981
Jie Jiang25c1b972020-11-12 15:42:53 +0900982void Manager::OnNeighborReachabilityEvent(
Jie Jiang84966852020-09-18 18:49:05 +0900983 int ifindex,
984 const shill::IPAddress& ip_addr,
985 NeighborLinkMonitor::NeighborRole role,
Jie Jiang25c1b972020-11-12 15:42:53 +0900986 NeighborReachabilityEventSignal::EventType event_type) {
Jie Jiang84966852020-09-18 18:49:05 +0900987 if (!ip_addr.IsValid()) {
988 LOG(DFATAL) << "ip_addr is not valid";
989 return;
990 }
991
Jie Jiang25c1b972020-11-12 15:42:53 +0900992 using SignalProto = NeighborReachabilityEventSignal;
Jie Jiang84966852020-09-18 18:49:05 +0900993 SignalProto proto;
994 proto.set_ifindex(ifindex);
995 proto.set_ip_addr(ip_addr.ToString());
Jie Jiang25c1b972020-11-12 15:42:53 +0900996 proto.set_type(event_type);
Jie Jiang84966852020-09-18 18:49:05 +0900997 switch (role) {
998 case NeighborLinkMonitor::NeighborRole::kGateway:
999 proto.set_role(SignalProto::GATEWAY);
1000 break;
1001 case NeighborLinkMonitor::NeighborRole::kDNSServer:
1002 proto.set_role(SignalProto::DNS_SERVER);
1003 break;
1004 case NeighborLinkMonitor::NeighborRole::kGatewayAndDNSServer:
1005 proto.set_role(SignalProto::GATEWAY_AND_DNS_SERVER);
1006 break;
1007 default:
1008 NOTREACHED();
1009 }
1010
Jie Jiang25c1b972020-11-12 15:42:53 +09001011 dbus::Signal signal(kPatchPanelInterface, kNeighborReachabilityEventSignal);
Jie Jiang84966852020-09-18 18:49:05 +09001012 dbus::MessageWriter writer(&signal);
1013 if (!writer.AppendProtoAsArrayOfBytes(proto)) {
Jie Jiang25c1b972020-11-12 15:42:53 +09001014 LOG(ERROR) << "Failed to encode proto NeighborReachabilityEventSignal";
Jie Jiang84966852020-09-18 18:49:05 +09001015 return;
1016 }
1017
1018 dbus_svc_path_->SendSignal(&signal);
1019}
1020
Hugo Benichi7c342672020-09-08 09:18:14 +09001021std::unique_ptr<patchpanel::ConnectNamespaceResponse> Manager::ConnectNamespace(
Hugo Benichiadf1ec52020-01-17 16:23:58 +09001022 base::ScopedFD client_fd,
Hugo Benichi7c342672020-09-08 09:18:14 +09001023 const patchpanel::ConnectNamespaceRequest& request) {
1024 auto response = std::make_unique<patchpanel::ConnectNamespaceResponse>();
1025
Hugo Benichiadf1ec52020-01-17 16:23:58 +09001026 std::unique_ptr<Subnet> subnet =
1027 addr_mgr_.AllocateIPv4Subnet(AddressManager::Guest::MINIJAIL_NETNS);
1028 if (!subnet) {
Hugo Benichi7c342672020-09-08 09:18:14 +09001029 LOG(ERROR) << "ConnectNamespace: exhausted IPv4 subnet space";
1030 return response;
Hugo Benichie8758b52020-04-03 14:49:01 +09001031 }
Hugo Benichiadf1ec52020-01-17 16:23:58 +09001032
Hugo Benichi7352ad92020-04-07 16:11:59 +09001033 // Dup the client fd into our own: this guarantees that the fd number will
1034 // be stable and tied to the actual kernel resources used by the client.
1035 base::ScopedFD local_client_fd(dup(client_fd.get()));
1036 if (!local_client_fd.is_valid()) {
Hugo Benichi7c342672020-09-08 09:18:14 +09001037 PLOG(ERROR) << "ConnectNamespace: failed to dup() client fd";
1038 return response;
Hugo Benichi7352ad92020-04-07 16:11:59 +09001039 }
1040
Hugo Benichi7c342672020-09-08 09:18:14 +09001041 // Add the duped fd to the epoll watcher.
Hugo Benichi7352ad92020-04-07 16:11:59 +09001042 // TODO(hugobenichi) Find a way to reuse base::FileDescriptorWatcher for
1043 // listening to EPOLLHUP.
1044 struct epoll_event epevent;
1045 epevent.events = EPOLLIN; // EPOLLERR | EPOLLHUP are always waited for.
1046 epevent.data.fd = local_client_fd.get();
1047 if (epoll_ctl(connected_namespaces_epollfd_, EPOLL_CTL_ADD,
1048 local_client_fd.get(), &epevent) != 0) {
Hugo Benichi7c342672020-09-08 09:18:14 +09001049 PLOG(ERROR) << "ConnectNamespace: epoll_ctl(EPOLL_CTL_ADD) failed";
1050 return response;
Hugo Benichi7352ad92020-04-07 16:11:59 +09001051 }
1052
Hugo Benichi7c342672020-09-08 09:18:14 +09001053 const std::string ifname_id = std::to_string(connected_namespaces_next_id_);
Hugo Benichifcf81022020-12-04 11:01:37 +09001054 ConnectedNamespace nsinfo = {};
1055 nsinfo.pid = request.pid();
1056 nsinfo.netns_name = "connected_netns_" + ifname_id;
Hugo Benichi93306e52020-12-04 16:08:00 +09001057 nsinfo.source = ProtoToTrafficSource(request.traffic_source());
1058 if (nsinfo.source == TrafficSource::UNKNOWN)
1059 nsinfo.source = TrafficSource::SYSTEM;
Hugo Benichifcf81022020-12-04 11:01:37 +09001060 nsinfo.outbound_ifname = request.outbound_physical_device();
Hugo Benichi93306e52020-12-04 16:08:00 +09001061 nsinfo.route_on_vpn = request.route_on_vpn();
Hugo Benichifcf81022020-12-04 11:01:37 +09001062 nsinfo.host_ifname = "arc_ns" + ifname_id;
1063 nsinfo.peer_ifname = "veth" + ifname_id;
1064 nsinfo.peer_subnet = std::move(subnet);
1065 nsinfo.peer_mac_addr = addr_mgr_.GenerateMacAddress();
1066
1067 if (!datapath_->StartRoutingNamespace(nsinfo)) {
Hugo Benichi7c342672020-09-08 09:18:14 +09001068 LOG(ERROR) << "ConnectNamespace: failed to setup datapath";
1069 if (epoll_ctl(connected_namespaces_epollfd_, EPOLL_CTL_DEL,
1070 local_client_fd.get(), nullptr) != 0)
1071 PLOG(ERROR) << "ConnectNamespace: epoll_ctl(EPOLL_CTL_DEL) failed";
1072 return response;
1073 }
1074
Hugo Benichifcf81022020-12-04 11:01:37 +09001075 // Prepare the response before storing ConnectedNamespace.
1076 response->set_peer_ifname(nsinfo.peer_ifname);
1077 response->set_peer_ipv4_address(nsinfo.peer_subnet->AddressAtOffset(1));
1078 response->set_host_ifname(nsinfo.host_ifname);
1079 response->set_host_ipv4_address(nsinfo.peer_subnet->AddressAtOffset(0));
Hugo Benichi7c342672020-09-08 09:18:14 +09001080 auto* response_subnet = response->mutable_ipv4_subnet();
Hugo Benichifcf81022020-12-04 11:01:37 +09001081 response_subnet->set_base_addr(nsinfo.peer_subnet->BaseAddress());
1082 response_subnet->set_prefix_len(nsinfo.peer_subnet->PrefixLength());
Hugo Benichiadf1ec52020-01-17 16:23:58 +09001083
Hugo Benichifcf81022020-12-04 11:01:37 +09001084 LOG(INFO) << "Connected network namespace " << nsinfo;
1085
1086 // Store ConnectedNamespace
Hugo Benichiadf1ec52020-01-17 16:23:58 +09001087 connected_namespaces_next_id_++;
Hugo Benichi7352ad92020-04-07 16:11:59 +09001088 int fdkey = local_client_fd.release();
Hugo Benichifcf81022020-12-04 11:01:37 +09001089 connected_namespaces_.insert(std::make_pair(fdkey, std::move(nsinfo)));
Hugo Benichi7352ad92020-04-07 16:11:59 +09001090
1091 if (connected_namespaces_.size() == 1) {
1092 LOG(INFO) << "Starting ConnectNamespace client fds monitoring";
1093 CheckConnectedNamespaces();
1094 }
Hugo Benichi7c342672020-09-08 09:18:14 +09001095
1096 return response;
Hugo Benichiadf1ec52020-01-17 16:23:58 +09001097}
1098
1099void Manager::DisconnectNamespace(int client_fd) {
1100 auto it = connected_namespaces_.find(client_fd);
1101 if (it == connected_namespaces_.end()) {
Hugo Benichifcf81022020-12-04 11:01:37 +09001102 LOG(ERROR) << "No ConnectedNamespace found for client_fd " << client_fd;
Hugo Benichiadf1ec52020-01-17 16:23:58 +09001103 return;
1104 }
1105
Hugo Benichi7352ad92020-04-07 16:11:59 +09001106 // Remove the client fd dupe from the epoll watcher and close it.
1107 if (epoll_ctl(connected_namespaces_epollfd_, EPOLL_CTL_DEL, client_fd,
Hugo Benichie8758b52020-04-03 14:49:01 +09001108 nullptr) != 0)
Hugo Benichi7352ad92020-04-07 16:11:59 +09001109 PLOG(ERROR) << "DisconnectNamespace: epoll_ctl(EPOLL_CTL_DEL) failed";
Hugo Benichie8758b52020-04-03 14:49:01 +09001110 if (close(client_fd) < 0)
Hugo Benichi7352ad92020-04-07 16:11:59 +09001111 PLOG(ERROR) << "DisconnectNamespace: close(client_fd) failed";
Hugo Benichi7352ad92020-04-07 16:11:59 +09001112
Hugo Benichifcf81022020-12-04 11:01:37 +09001113 datapath_->StopRoutingNamespace(it->second);
Hugo Benichiadf1ec52020-01-17 16:23:58 +09001114 LOG(INFO) << "Disconnected network namespace " << it->second;
Hugo Benichiadf1ec52020-01-17 16:23:58 +09001115 // This release the allocated IPv4 subnet.
1116 connected_namespaces_.erase(it);
1117}
1118
Hugo Benichi7352ad92020-04-07 16:11:59 +09001119// TODO(hugobenichi) Generalize this check to all resources created by
1120// patchpanel on behalf of a remote client.
1121void Manager::CheckConnectedNamespaces() {
1122 int max_event = 10;
1123 struct epoll_event epevents[max_event];
1124 int nready = epoll_wait(connected_namespaces_epollfd_, epevents, max_event,
1125 0 /* do not block */);
1126 if (nready < 0)
1127 PLOG(ERROR) << "CheckConnectedNamespaces: epoll_wait(0) failed";
1128
1129 for (int i = 0; i < nready; i++)
1130 if (epevents[i].events & (EPOLLHUP | EPOLLERR))
1131 DisconnectNamespace(epevents[i].data.fd);
1132
1133 if (connected_namespaces_.empty()) {
1134 LOG(INFO) << "Stopping ConnectNamespace client fds monitoring";
1135 return;
1136 }
1137
Qijiang Fan2d7aeb42020-05-19 02:06:39 +09001138 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
Hugo Benichi7352ad92020-04-07 16:11:59 +09001139 FROM_HERE,
1140 base::Bind(&Manager::CheckConnectedNamespaces,
Hugo Benichie8758b52020-04-03 14:49:01 +09001141 weak_factory_.GetWeakPtr()),
Hugo Benichi7352ad92020-04-07 16:11:59 +09001142 kConnectNamespaceCheckInterval);
1143}
1144
Jason Jeremy Imanc28df852020-07-11 17:38:26 +09001145bool Manager::ModifyPortRule(const patchpanel::ModifyPortRuleRequest& request) {
1146 switch (request.proto()) {
1147 case patchpanel::ModifyPortRuleRequest::TCP:
1148 case patchpanel::ModifyPortRuleRequest::UDP:
1149 break;
1150 default:
1151 LOG(ERROR) << "Unknown protocol " << request.proto();
1152 return false;
1153 }
1154
1155 switch (request.op()) {
1156 case patchpanel::ModifyPortRuleRequest::CREATE:
1157 switch (request.type()) {
1158 case patchpanel::ModifyPortRuleRequest::ACCESS:
1159 return firewall_.AddAcceptRules(request.proto(),
1160 request.input_dst_port(),
1161 request.input_ifname());
1162 case patchpanel::ModifyPortRuleRequest::LOCKDOWN:
1163 return firewall_.AddLoopbackLockdownRules(request.proto(),
1164 request.input_dst_port());
1165 case patchpanel::ModifyPortRuleRequest::FORWARDING:
1166 return firewall_.AddIpv4ForwardRule(
1167 request.proto(), request.input_dst_ip(), request.input_dst_port(),
1168 request.input_ifname(), request.dst_ip(), request.dst_port());
1169 default:
1170 LOG(ERROR) << "Unknown port rule type " << request.type();
1171 return false;
1172 }
1173 case patchpanel::ModifyPortRuleRequest::DELETE:
1174 switch (request.type()) {
1175 case patchpanel::ModifyPortRuleRequest::ACCESS:
1176 return firewall_.DeleteAcceptRules(request.proto(),
1177 request.input_dst_port(),
1178 request.input_ifname());
1179 case patchpanel::ModifyPortRuleRequest::LOCKDOWN:
1180 return firewall_.DeleteLoopbackLockdownRules(
1181 request.proto(), request.input_dst_port());
1182 case patchpanel::ModifyPortRuleRequest::FORWARDING:
1183 return firewall_.DeleteIpv4ForwardRule(
1184 request.proto(), request.input_dst_ip(), request.input_dst_port(),
1185 request.input_ifname(), request.dst_ip(), request.dst_port());
1186 default:
1187 LOG(ERROR) << "Unknown port rule type " << request.type();
1188 return false;
1189 }
1190 default:
1191 LOG(ERROR) << "Unknown operation " << request.op();
1192 return false;
1193 }
1194}
1195
Garrick Evanse94a14e2019-11-11 10:32:13 +09001196void Manager::SendGuestMessage(const GuestMessage& msg) {
Garrick Evans96e03042019-05-28 14:30:52 +09001197 IpHelperMessage ipm;
1198 *ipm.mutable_guest_message() = msg;
Garrick Evans96e03042019-05-28 14:30:52 +09001199 adb_proxy_->SendMessage(ipm);
Garrick Evanse94a14e2019-11-11 10:32:13 +09001200 mcast_proxy_->SendMessage(ipm);
1201 nd_proxy_->SendMessage(ipm);
Garrick Evans96e03042019-05-28 14:30:52 +09001202}
1203
Garrick Evans4ac09852020-01-16 14:09:22 +09001204void Manager::StartForwarding(const std::string& ifname_physical,
1205 const std::string& ifname_virtual,
Garrick Evans4ac09852020-01-16 14:09:22 +09001206 bool ipv6,
1207 bool multicast) {
Taoyu Li7dca19a2020-03-16 16:27:07 +09001208 if (ifname_physical.empty() || ifname_virtual.empty())
Garrick Evans4ac09852020-01-16 14:09:22 +09001209 return;
1210
1211 IpHelperMessage ipm;
1212 DeviceMessage* msg = ipm.mutable_device_message();
1213 msg->set_dev_ifname(ifname_physical);
Garrick Evans4ac09852020-01-16 14:09:22 +09001214 msg->set_br_ifname(ifname_virtual);
1215
1216 if (ipv6) {
1217 LOG(INFO) << "Starting IPv6 forwarding from " << ifname_physical << " to "
1218 << ifname_virtual;
1219
1220 if (!datapath_->AddIPv6Forwarding(ifname_physical, ifname_virtual)) {
1221 LOG(ERROR) << "Failed to setup iptables forwarding rule for IPv6 from "
1222 << ifname_physical << " to " << ifname_virtual;
1223 }
1224 if (!datapath_->MaskInterfaceFlags(ifname_physical, IFF_ALLMULTI)) {
1225 LOG(WARNING) << "Failed to setup all multicast mode for interface "
1226 << ifname_physical;
1227 }
1228 if (!datapath_->MaskInterfaceFlags(ifname_virtual, IFF_ALLMULTI)) {
1229 LOG(WARNING) << "Failed to setup all multicast mode for interface "
1230 << ifname_virtual;
1231 }
1232 nd_proxy_->SendMessage(ipm);
1233 }
1234
1235 if (multicast) {
Hugo Benichi69c989d2021-03-01 00:23:39 +09001236 multicast_virtual_ifnames_.insert(ifname_virtual);
Garrick Evans4ac09852020-01-16 14:09:22 +09001237 LOG(INFO) << "Starting multicast forwarding from " << ifname_physical
1238 << " to " << ifname_virtual;
1239 mcast_proxy_->SendMessage(ipm);
1240 }
1241}
1242
1243void Manager::StopForwarding(const std::string& ifname_physical,
1244 const std::string& ifname_virtual,
1245 bool ipv6,
1246 bool multicast) {
1247 if (ifname_physical.empty())
1248 return;
1249
1250 IpHelperMessage ipm;
1251 DeviceMessage* msg = ipm.mutable_device_message();
1252 msg->set_dev_ifname(ifname_physical);
1253 msg->set_teardown(true);
Taoyu Li7dca19a2020-03-16 16:27:07 +09001254 if (!ifname_virtual.empty()) {
1255 msg->set_br_ifname(ifname_virtual);
1256 }
Garrick Evans4ac09852020-01-16 14:09:22 +09001257
1258 if (ipv6) {
Taoyu Li7dca19a2020-03-16 16:27:07 +09001259 if (ifname_virtual.empty()) {
1260 LOG(INFO) << "Stopping IPv6 forwarding on " << ifname_physical;
1261 } else {
1262 LOG(INFO) << "Stopping IPv6 forwarding from " << ifname_physical << " to "
1263 << ifname_virtual;
1264 datapath_->RemoveIPv6Forwarding(ifname_physical, ifname_virtual);
1265 }
Garrick Evans4ac09852020-01-16 14:09:22 +09001266 nd_proxy_->SendMessage(ipm);
1267 }
1268
1269 if (multicast) {
Hugo Benichi69c989d2021-03-01 00:23:39 +09001270 multicast_virtual_ifnames_.erase(ifname_virtual);
Taoyu Li7dca19a2020-03-16 16:27:07 +09001271 if (ifname_virtual.empty()) {
1272 LOG(INFO) << "Stopping multicast forwarding on " << ifname_physical;
1273 } else {
1274 LOG(INFO) << "Stopping multicast forwarding from " << ifname_physical
1275 << " to " << ifname_virtual;
1276 }
Garrick Evans4ac09852020-01-16 14:09:22 +09001277 mcast_proxy_->SendMessage(ipm);
1278 }
1279}
1280
Taoyu Lia0727dc2020-09-24 19:54:59 +09001281void Manager::OnNDProxyMessage(const NDProxyMessage& msg) {
1282 LOG_IF(DFATAL, msg.ifname().empty())
Garrick Evans4ac09852020-01-16 14:09:22 +09001283 << "Received DeviceMessage w/ empty dev_ifname";
Taoyu Lia0727dc2020-09-24 19:54:59 +09001284 switch (msg.type()) {
1285 case NDProxyMessage::ADD_ROUTE:
1286 if (!datapath_->AddIPv6HostRoute(msg.ifname(), msg.ip6addr(), 128)) {
1287 LOG(WARNING) << "Failed to setup the IPv6 route for interface "
1288 << msg.ifname() << ", addr " << msg.ip6addr();
1289 }
1290 break;
1291 case NDProxyMessage::ADD_ADDR:
1292 if (!datapath_->AddIPv6Address(msg.ifname(), msg.ip6addr())) {
1293 LOG(WARNING) << "Failed to setup the IPv6 address for interface "
1294 << msg.ifname() << ", addr " << msg.ip6addr();
1295 }
1296 break;
1297 case NDProxyMessage::DEL_ADDR:
1298 datapath_->RemoveIPv6Address(msg.ifname(), msg.ip6addr());
1299 break;
1300 default:
1301 LOG(ERROR) << "Unknown NDProxy event " << msg.type();
1302 NOTREACHED();
Garrick Evans4ac09852020-01-16 14:09:22 +09001303 }
1304}
1305
Garrick Evans3388a032020-03-24 11:25:55 +09001306} // namespace patchpanel