blob: fb8dab7440e04cbbca0ed3becb666b490e691c4b [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 Benichi058a26a2020-11-26 10:10:39 +0900261 counters_svc_->Init(shill_client_->get_devices());
Hugo Benichifda77232020-08-21 18:28:15 +0900262 nd_proxy_->Listen();
263}
264
265void Manager::OnShutdown(int* exit_code) {
266 LOG(INFO) << "Shutting down and cleaning up";
Jie Jiang84966852020-09-18 18:49:05 +0900267 network_monitor_svc_.reset();
Hugo Benichifda77232020-08-21 18:28:15 +0900268 cros_svc_.reset();
269 arc_svc_.reset();
270 close(connected_namespaces_epollfd_);
271 // Tear down any remaining connected namespace.
272 std::vector<int> connected_namespaces_fdkeys;
273 for (const auto& kv : connected_namespaces_)
274 connected_namespaces_fdkeys.push_back(kv.first);
275 for (const int fdkey : connected_namespaces_fdkeys)
276 DisconnectNamespace(fdkey);
Hugo Benichibf811c62020-09-07 17:30:45 +0900277 if (!USE_JETSTREAM_ROUTING)
278 datapath_->Stop();
Jie Jiang00592cf2020-12-17 11:23:49 +0900279
280 brillo::DBusDaemon::OnShutdown(exit_code);
Hugo Benichifda77232020-08-21 18:28:15 +0900281}
282
283void Manager::OnSubprocessExited(pid_t pid, const siginfo_t&) {
284 LOG(ERROR) << "Subprocess " << pid << " exited unexpectedly -"
285 << " attempting to restart";
286
287 HelperProcess* proc;
288 if (pid == adb_proxy_->pid()) {
289 proc = adb_proxy_.get();
290 } else if (pid == mcast_proxy_->pid()) {
291 proc = mcast_proxy_.get();
292 } else if (pid == nd_proxy_->pid()) {
293 proc = nd_proxy_.get();
294 } else {
295 LOG(DFATAL) << "Unknown child process";
296 return;
297 }
298
299 process_reaper_.ForgetChild(pid);
300
301 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
302 FROM_HERE,
303 base::Bind(&Manager::RestartSubprocess, weak_factory_.GetWeakPtr(), proc),
304 base::TimeDelta::FromMilliseconds((2 << proc->restarts()) *
305 kSubprocessRestartDelayMs));
306}
307
308void Manager::RestartSubprocess(HelperProcess* subproc) {
309 if (subproc->Restart()) {
310 DCHECK(process_reaper_.WatchForChild(
311 FROM_HERE, subproc->pid(),
312 base::Bind(&Manager::OnSubprocessExited, weak_factory_.GetWeakPtr(),
313 subproc->pid())))
314 << "Failed to watch child process " << subproc->pid();
315 }
316}
317
Hugo Benichi2a940542020-10-26 18:50:49 +0900318void Manager::OnDefaultDeviceChanged(const ShillClient::Device& new_device,
319 const ShillClient::Device& prev_device) {
320 // Only take into account interface switches and ignore layer 3 property
321 // changes.
322 if (prev_device.ifname == new_device.ifname)
323 return;
324
Hugo Benichi058a26a2020-11-26 10:10:39 +0900325 if (prev_device.type == ShillClient::Device::Type::kVPN) {
Hugo Benichi2a940542020-10-26 18:50:49 +0900326 datapath_->StopVpnRouting(prev_device.ifname);
Hugo Benichi058a26a2020-11-26 10:10:39 +0900327 counters_svc_->OnVpnDeviceRemoved(prev_device.ifname);
328 }
Hugo Benichi2a940542020-10-26 18:50:49 +0900329
Hugo Benichi058a26a2020-11-26 10:10:39 +0900330 if (new_device.type == ShillClient::Device::Type::kVPN) {
Hugo Benichi2a940542020-10-26 18:50:49 +0900331 datapath_->StartVpnRouting(new_device.ifname);
Hugo Benichi058a26a2020-11-26 10:10:39 +0900332 counters_svc_->OnVpnDeviceAdded(new_device.ifname);
333 }
Hugo Benichi69c989d2021-03-01 00:23:39 +0900334
335 // When the default logical network changes, Crostini's tap devices must leave
336 // their current forwarding group for multicast and IPv6 ndproxy and join the
337 // forwarding group of the new logical default network.
338 for (const auto* tap_device : cros_svc_->GetDevices()) {
339 bool was_multicast =
340 multicast_virtual_ifnames_.find(tap_device->host_ifname()) !=
341 multicast_virtual_ifnames_.end();
342 StopForwarding(prev_device.ifname, tap_device->host_ifname(),
343 IsIPv6NDProxyEnabled(prev_device.type), was_multicast);
344 StartForwarding(new_device.ifname, tap_device->host_ifname(),
345 IsIPv6NDProxyEnabled(new_device.type),
346 IsMulticastInterface(new_device.ifname));
347 }
Hugo Benichi2a940542020-10-26 18:50:49 +0900348}
349
Hugo Benichi76be34a2020-08-26 22:35:54 +0900350void Manager::OnDevicesChanged(const std::set<std::string>& added,
351 const std::set<std::string>& removed) {
Hugo Benichi058a26a2020-11-26 10:10:39 +0900352 for (const std::string& ifname : removed) {
Hugo Benichi76be34a2020-08-26 22:35:54 +0900353 datapath_->StopConnectionPinning(ifname);
Hugo Benichi1e0656f2021-02-15 15:43:38 +0900354 datapath_->RemoveRedirectDnsRule(ifname);
Hugo Benichi058a26a2020-11-26 10:10:39 +0900355 counters_svc_->OnPhysicalDeviceRemoved(ifname);
356 }
Hugo Benichi76be34a2020-08-26 22:35:54 +0900357
Hugo Benichi058a26a2020-11-26 10:10:39 +0900358 for (const std::string& ifname : added) {
Hugo Benichi76be34a2020-08-26 22:35:54 +0900359 datapath_->StartConnectionPinning(ifname);
Hugo Benichi1e0656f2021-02-15 15:43:38 +0900360 ShillClient::Device device;
361 if (!shill_client_->GetDeviceProperties(ifname, &device))
362 continue;
363
364 if (!device.ipconfig.ipv4_dns_addresses.empty())
365 datapath_->AddRedirectDnsRule(ifname,
366 device.ipconfig.ipv4_dns_addresses.front());
367
Hugo Benichi058a26a2020-11-26 10:10:39 +0900368 counters_svc_->OnPhysicalDeviceAdded(ifname);
369 }
Hugo Benichi76be34a2020-08-26 22:35:54 +0900370}
371
Hugo Benichi1e0656f2021-02-15 15:43:38 +0900372void Manager::OnIPConfigsChanged(const std::string& device,
373 const ShillClient::IPConfig& ipconfig) {
374 if (ipconfig.ipv4_dns_addresses.empty()) {
375 datapath_->RemoveRedirectDnsRule(device);
376 } else {
377 datapath_->AddRedirectDnsRule(device, ipconfig.ipv4_dns_addresses.front());
378 }
379}
380
Garrick Evans209a80a2020-11-30 14:42:40 +0900381void Manager::OnDeviceChanged(const Device& device,
382 Device::ChangeEvent event,
383 GuestMessage::GuestType guest_type) {
384 dbus::Signal signal(kPatchPanelInterface, kNetworkDeviceChangedSignal);
385 NetworkDeviceChangedSignal proto;
386 proto.set_event(event == Device::ChangeEvent::ADDED
387 ? NetworkDeviceChangedSignal::DEVICE_ADDED
388 : NetworkDeviceChangedSignal::DEVICE_REMOVED);
389 auto* dev = proto.mutable_device();
390 dev->set_ifname(device.host_ifname());
Garrick Evans9ccd2a62021-02-18 14:48:28 +0900391 dev->set_phys_ifname(device.phys_ifname());
Garrick Evans209a80a2020-11-30 14:42:40 +0900392 dev->set_ipv4_addr(device.config().guest_ipv4_addr());
393 if (const auto* subnet = device.config().ipv4_subnet()) {
394 auto* sub = dev->mutable_ipv4_subnet();
395 sub->set_base_addr(subnet->BaseAddress());
396 sub->set_prefix_len(subnet->PrefixLength());
397 }
398 switch (guest_type) {
399 case GuestMessage::ARC:
400 dev->set_guest_type(NetworkDevice::ARC);
401 break;
402 case GuestMessage::ARC_VM:
403 dev->set_guest_type(NetworkDevice::ARCVM);
404 break;
405 case GuestMessage::TERMINA_VM:
406 dev->set_guest_type(NetworkDevice::TERMINA_VM);
407 break;
408 case GuestMessage::PLUGIN_VM:
409 dev->set_guest_type(NetworkDevice::PLUGIN_VM);
410 break;
411 default:
412 LOG(ERROR) << "Unknown device type";
413 }
414
Hugo Benichi69c989d2021-03-01 00:23:39 +0900415 if (guest_type == GuestMessage::TERMINA_VM ||
416 guest_type == GuestMessage::PLUGIN_VM) {
417 const ShillClient::Device& default_device = shill_client_->default_device();
418 if (event == Device::ChangeEvent::ADDED) {
419 StartForwarding(default_device.ifname, device.host_ifname(),
420 IsIPv6NDProxyEnabled(default_device.type),
421 IsMulticastInterface(default_device.ifname));
422 } else if (event == Device::ChangeEvent::REMOVED) {
423 bool was_multicast =
424 multicast_virtual_ifnames_.find(device.host_ifname()) !=
425 multicast_virtual_ifnames_.end();
426 StopForwarding(default_device.ifname, device.host_ifname(),
427 IsIPv6NDProxyEnabled(default_device.type), was_multicast);
428 }
429 }
430
Hugo Benichib9d123e2021-02-27 23:37:16 +0900431 if (guest_type == GuestMessage::ARC || guest_type == GuestMessage::ARC_VM) {
432 if (event == Device::ChangeEvent::ADDED) {
433 StartForwarding(device.phys_ifname(), device.host_ifname(),
434 device.options().ipv6_enabled,
435 device.options().fwd_multicast);
436 } else if (event == Device::ChangeEvent::REMOVED) {
437 StopForwarding(device.phys_ifname(), device.host_ifname(),
438 device.options().ipv6_enabled,
439 device.options().fwd_multicast);
440 }
441 }
442
Garrick Evans209a80a2020-11-30 14:42:40 +0900443 dbus::MessageWriter(&signal).AppendProtoAsArrayOfBytes(proto);
444 dbus_svc_path_->SendSignal(&signal);
445}
446
Garrick Evanse94a14e2019-11-11 10:32:13 +0900447bool Manager::StartArc(pid_t pid) {
Garrick Evans508a4bc2019-11-14 08:45:52 +0900448 if (!arc_svc_->Start(pid))
449 return false;
Garrick Evanse94a14e2019-11-11 10:32:13 +0900450
451 GuestMessage msg;
452 msg.set_event(GuestMessage::START);
453 msg.set_type(GuestMessage::ARC);
454 msg.set_arc_pid(pid);
455 SendGuestMessage(msg);
456
457 return true;
458}
459
Hugo Benichi4d4bb8f2020-07-07 12:16:07 +0900460void Manager::StopArc() {
Garrick Evanse94a14e2019-11-11 10:32:13 +0900461 GuestMessage msg;
462 msg.set_event(GuestMessage::STOP);
463 msg.set_type(GuestMessage::ARC);
464 SendGuestMessage(msg);
465
Hugo Benichi4d4bb8f2020-07-07 12:16:07 +0900466 // After the ARC container has stopped, the pid is not known anymore.
467 // The pid argument is ignored by ArcService.
468 arc_svc_->Stop(0);
Garrick Evanse94a14e2019-11-11 10:32:13 +0900469}
470
Garrick Evans015b0d62020-02-07 09:06:38 +0900471bool Manager::StartArcVm(uint32_t cid) {
Garrick Evans508a4bc2019-11-14 08:45:52 +0900472 if (!arc_svc_->Start(cid))
473 return false;
Garrick Evanse94a14e2019-11-11 10:32:13 +0900474
475 GuestMessage msg;
476 msg.set_event(GuestMessage::START);
477 msg.set_type(GuestMessage::ARC_VM);
478 msg.set_arcvm_vsock_cid(cid);
479 SendGuestMessage(msg);
480
481 return true;
482}
483
Garrick Evans015b0d62020-02-07 09:06:38 +0900484void Manager::StopArcVm(uint32_t cid) {
Garrick Evanse94a14e2019-11-11 10:32:13 +0900485 GuestMessage msg;
486 msg.set_event(GuestMessage::STOP);
487 msg.set_type(GuestMessage::ARC_VM);
488 SendGuestMessage(msg);
489
Garrick Evans21173b12019-11-20 15:23:16 +0900490 arc_svc_->Stop(cid);
Garrick Evanse94a14e2019-11-11 10:32:13 +0900491}
492
Garrick Evans51d5b552020-01-30 10:42:06 +0900493bool Manager::StartCrosVm(uint64_t vm_id,
494 GuestMessage::GuestType vm_type,
Garrick Evans53a2a982020-02-05 10:53:35 +0900495 uint32_t subnet_index) {
Garrick Evans51d5b552020-01-30 10:42:06 +0900496 DCHECK(vm_type == GuestMessage::TERMINA_VM ||
497 vm_type == GuestMessage::PLUGIN_VM);
498
499 if (!cros_svc_->Start(vm_id, vm_type == GuestMessage::TERMINA_VM,
500 subnet_index))
Garrick Evans47c19272019-11-21 10:58:21 +0900501 return false;
502
503 GuestMessage msg;
504 msg.set_event(GuestMessage::START);
Garrick Evans51d5b552020-01-30 10:42:06 +0900505 msg.set_type(vm_type);
Garrick Evans47c19272019-11-21 10:58:21 +0900506 SendGuestMessage(msg);
507
508 return true;
509}
510
Garrick Evans51d5b552020-01-30 10:42:06 +0900511void Manager::StopCrosVm(uint64_t vm_id, GuestMessage::GuestType vm_type) {
Garrick Evans47c19272019-11-21 10:58:21 +0900512 GuestMessage msg;
513 msg.set_event(GuestMessage::STOP);
Garrick Evans51d5b552020-01-30 10:42:06 +0900514 msg.set_type(vm_type);
Garrick Evans47c19272019-11-21 10:58:21 +0900515 SendGuestMessage(msg);
516
Garrick Evans51d5b552020-01-30 10:42:06 +0900517 cros_svc_->Stop(vm_id, vm_type == GuestMessage::TERMINA_VM);
Garrick Evans47c19272019-11-21 10:58:21 +0900518}
519
Garrick Evans02e6e872020-11-30 11:53:13 +0900520std::unique_ptr<dbus::Response> Manager::OnGetDevices(
521 dbus::MethodCall* method_call) {
522 std::unique_ptr<dbus::Response> dbus_response(
523 dbus::Response::FromMethodCall(method_call));
524
525 dbus::MessageReader reader(method_call);
526 dbus::MessageWriter writer(dbus_response.get());
527
528 patchpanel::GetDevicesRequest request;
529 patchpanel::GetDevicesResponse response;
530
531 if (!reader.PopArrayOfBytesAsProto(&request)) {
532 LOG(ERROR) << "Unable to parse request";
533 writer.AppendProtoAsArrayOfBytes(response);
534 return dbus_response;
535 }
536
537 static const auto arc_guest_type =
538 USE_ARCVM ? NetworkDevice::ARCVM : NetworkDevice::ARC;
539
540 arc_svc_->ScanDevices(base::BindRepeating(
541 [](patchpanel::GetDevicesResponse* resp, const Device& device) {
542 auto* dev = resp->add_devices();
543 dev->set_ifname(device.host_ifname());
Garrick Evans9ccd2a62021-02-18 14:48:28 +0900544 dev->set_phys_ifname(device.phys_ifname());
Garrick Evans02e6e872020-11-30 11:53:13 +0900545 dev->set_ipv4_addr(device.config().guest_ipv4_addr());
546 dev->set_guest_type(arc_guest_type);
547 if (const auto* subnet = device.config().ipv4_subnet()) {
548 auto* sub = dev->mutable_ipv4_subnet();
549 sub->set_base_addr(subnet->BaseAddress());
550 sub->set_prefix_len(subnet->PrefixLength());
551 }
552 },
553 &response));
554
555 cros_svc_->ScanDevices(base::BindRepeating(
556 [](patchpanel::GetDevicesResponse* resp, uint64_t vm_id, bool is_termina,
557 const Device& device) {
558 auto* dev = resp->add_devices();
559 dev->set_ifname(device.host_ifname());
Garrick Evans9ccd2a62021-02-18 14:48:28 +0900560 dev->set_phys_ifname(device.phys_ifname());
Garrick Evans02e6e872020-11-30 11:53:13 +0900561 dev->set_ipv4_addr(device.config().guest_ipv4_addr());
562 dev->set_guest_type(is_termina ? NetworkDevice::TERMINA_VM
563 : NetworkDevice::PLUGIN_VM);
564 if (const auto* subnet = device.config().ipv4_subnet()) {
565 auto* sub = dev->mutable_ipv4_subnet();
566 sub->set_base_addr(subnet->BaseAddress());
567 sub->set_prefix_len(subnet->PrefixLength());
568 }
569 },
570 &response));
571
572 writer.AppendProtoAsArrayOfBytes(response);
573 return dbus_response;
574}
575
Garrick Evans08843932019-09-17 14:41:08 +0900576std::unique_ptr<dbus::Response> Manager::OnArcStartup(
577 dbus::MethodCall* method_call) {
578 LOG(INFO) << "ARC++ starting up";
579
580 std::unique_ptr<dbus::Response> dbus_response(
581 dbus::Response::FromMethodCall(method_call));
582
583 dbus::MessageReader reader(method_call);
584 dbus::MessageWriter writer(dbus_response.get());
585
586 patchpanel::ArcStartupRequest request;
587 patchpanel::ArcStartupResponse response;
588
589 if (!reader.PopArrayOfBytesAsProto(&request)) {
590 LOG(ERROR) << "Unable to parse request";
591 writer.AppendProtoAsArrayOfBytes(response);
592 return dbus_response;
593 }
594
Garrick Evanse01bf072019-11-15 09:08:19 +0900595 if (!StartArc(request.pid()))
596 LOG(ERROR) << "Failed to start ARC++ network service";
Garrick Evanse94a14e2019-11-11 10:32:13 +0900597
Garrick Evans08843932019-09-17 14:41:08 +0900598 writer.AppendProtoAsArrayOfBytes(response);
599 return dbus_response;
600}
601
602std::unique_ptr<dbus::Response> Manager::OnArcShutdown(
603 dbus::MethodCall* method_call) {
604 LOG(INFO) << "ARC++ shutting down";
605
606 std::unique_ptr<dbus::Response> dbus_response(
607 dbus::Response::FromMethodCall(method_call));
608
609 dbus::MessageReader reader(method_call);
610 dbus::MessageWriter writer(dbus_response.get());
611
612 patchpanel::ArcShutdownRequest request;
613 patchpanel::ArcShutdownResponse response;
614
615 if (!reader.PopArrayOfBytesAsProto(&request)) {
616 LOG(ERROR) << "Unable to parse request";
617 writer.AppendProtoAsArrayOfBytes(response);
618 return dbus_response;
619 }
620
Hugo Benichi4d4bb8f2020-07-07 12:16:07 +0900621 StopArc();
Garrick Evanse94a14e2019-11-11 10:32:13 +0900622
Garrick Evans08843932019-09-17 14:41:08 +0900623 writer.AppendProtoAsArrayOfBytes(response);
624 return dbus_response;
625}
626
627std::unique_ptr<dbus::Response> Manager::OnArcVmStartup(
628 dbus::MethodCall* method_call) {
629 LOG(INFO) << "ARCVM starting up";
630
631 std::unique_ptr<dbus::Response> dbus_response(
632 dbus::Response::FromMethodCall(method_call));
633
634 dbus::MessageReader reader(method_call);
635 dbus::MessageWriter writer(dbus_response.get());
636
637 patchpanel::ArcVmStartupRequest request;
638 patchpanel::ArcVmStartupResponse response;
639
640 if (!reader.PopArrayOfBytesAsProto(&request)) {
641 LOG(ERROR) << "Unable to parse request";
642 writer.AppendProtoAsArrayOfBytes(response);
643 return dbus_response;
644 }
645
Garrick Evans47c19272019-11-21 10:58:21 +0900646 if (!StartArcVm(request.cid())) {
Garrick Evanse01bf072019-11-15 09:08:19 +0900647 LOG(ERROR) << "Failed to start ARCVM network service";
Garrick Evans47c19272019-11-21 10:58:21 +0900648 writer.AppendProtoAsArrayOfBytes(response);
649 return dbus_response;
Garrick Evanse01bf072019-11-15 09:08:19 +0900650 }
Garrick Evanse94a14e2019-11-11 10:32:13 +0900651
Garrick Evans47c19272019-11-21 10:58:21 +0900652 // Populate the response with the known devices.
Garrick Evans38b25a42020-04-06 15:17:42 +0900653 for (const auto* config : arc_svc_->GetDeviceConfigs()) {
654 if (config->tap_ifname().empty())
655 continue;
Garrick Evans47c19272019-11-21 10:58:21 +0900656
Garrick Evans38b25a42020-04-06 15:17:42 +0900657 auto* dev = response.add_devices();
658 dev->set_ifname(config->tap_ifname());
659 dev->set_ipv4_addr(config->guest_ipv4_addr());
Garrick Evansa0c9c972020-11-30 09:54:59 +0900660 dev->set_guest_type(NetworkDevice::ARCVM);
Garrick Evans38b25a42020-04-06 15:17:42 +0900661 }
Garrick Evanse94b6de2020-02-20 09:19:13 +0900662
Garrick Evans08843932019-09-17 14:41:08 +0900663 writer.AppendProtoAsArrayOfBytes(response);
664 return dbus_response;
665}
666
667std::unique_ptr<dbus::Response> Manager::OnArcVmShutdown(
668 dbus::MethodCall* method_call) {
669 LOG(INFO) << "ARCVM shutting down";
670
671 std::unique_ptr<dbus::Response> dbus_response(
672 dbus::Response::FromMethodCall(method_call));
673
674 dbus::MessageReader reader(method_call);
675 dbus::MessageWriter writer(dbus_response.get());
676
677 patchpanel::ArcVmShutdownRequest request;
678 patchpanel::ArcVmShutdownResponse response;
679
680 if (!reader.PopArrayOfBytesAsProto(&request)) {
681 LOG(ERROR) << "Unable to parse request";
682 writer.AppendProtoAsArrayOfBytes(response);
683 return dbus_response;
684 }
685
Garrick Evans21173b12019-11-20 15:23:16 +0900686 StopArcVm(request.cid());
Garrick Evanse94a14e2019-11-11 10:32:13 +0900687
Garrick Evans08843932019-09-17 14:41:08 +0900688 writer.AppendProtoAsArrayOfBytes(response);
689 return dbus_response;
690}
691
Garrick Evans47c19272019-11-21 10:58:21 +0900692std::unique_ptr<dbus::Response> Manager::OnTerminaVmStartup(
693 dbus::MethodCall* method_call) {
694 LOG(INFO) << "Termina VM starting up";
695
696 std::unique_ptr<dbus::Response> dbus_response(
697 dbus::Response::FromMethodCall(method_call));
698
699 dbus::MessageReader reader(method_call);
700 dbus::MessageWriter writer(dbus_response.get());
701
702 patchpanel::TerminaVmStartupRequest request;
703 patchpanel::TerminaVmStartupResponse response;
704
705 if (!reader.PopArrayOfBytesAsProto(&request)) {
706 LOG(ERROR) << "Unable to parse request";
707 writer.AppendProtoAsArrayOfBytes(response);
708 return dbus_response;
709 }
710
711 const int32_t cid = request.cid();
Garrick Evans53a2a982020-02-05 10:53:35 +0900712 if (!StartCrosVm(cid, GuestMessage::TERMINA_VM)) {
Garrick Evans47c19272019-11-21 10:58:21 +0900713 LOG(ERROR) << "Failed to start Termina VM network service";
714 writer.AppendProtoAsArrayOfBytes(response);
715 return dbus_response;
716 }
717
Garrick Evans51d5b552020-01-30 10:42:06 +0900718 const auto* const tap = cros_svc_->TAP(cid, true /*is_termina*/);
Garrick Evansb1c93712020-01-22 09:28:25 +0900719 if (!tap) {
720 LOG(DFATAL) << "TAP device missing";
721 writer.AppendProtoAsArrayOfBytes(response);
722 return dbus_response;
723 }
Garrick Evans47c19272019-11-21 10:58:21 +0900724
Garrick Evansb1c93712020-01-22 09:28:25 +0900725 auto* dev = response.mutable_device();
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900726 dev->set_ifname(tap->host_ifname());
Garrick Evansa0c9c972020-11-30 09:54:59 +0900727 dev->set_guest_type(NetworkDevice::TERMINA_VM);
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900728 const auto* subnet = tap->config().ipv4_subnet();
Garrick Evansb1c93712020-01-22 09:28:25 +0900729 if (!subnet) {
730 LOG(DFATAL) << "Missing required subnet for {cid: " << cid << "}";
731 writer.AppendProtoAsArrayOfBytes(response);
732 return dbus_response;
733 }
734 auto* resp_subnet = dev->mutable_ipv4_subnet();
735 resp_subnet->set_base_addr(subnet->BaseAddress());
736 resp_subnet->set_prefix_len(subnet->PrefixLength());
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900737 subnet = tap->config().lxd_ipv4_subnet();
Garrick Evansb1c93712020-01-22 09:28:25 +0900738 if (!subnet) {
739 LOG(DFATAL) << "Missing required lxd subnet for {cid: " << cid << "}";
740 writer.AppendProtoAsArrayOfBytes(response);
741 return dbus_response;
742 }
743 resp_subnet = response.mutable_container_subnet();
744 resp_subnet->set_base_addr(subnet->BaseAddress());
745 resp_subnet->set_prefix_len(subnet->PrefixLength());
Garrick Evans47c19272019-11-21 10:58:21 +0900746
747 writer.AppendProtoAsArrayOfBytes(response);
748 return dbus_response;
749}
750
751std::unique_ptr<dbus::Response> Manager::OnTerminaVmShutdown(
752 dbus::MethodCall* method_call) {
753 LOG(INFO) << "Termina VM shutting down";
754
755 std::unique_ptr<dbus::Response> dbus_response(
756 dbus::Response::FromMethodCall(method_call));
757
758 dbus::MessageReader reader(method_call);
759 dbus::MessageWriter writer(dbus_response.get());
760
761 patchpanel::TerminaVmShutdownRequest request;
762 patchpanel::TerminaVmShutdownResponse response;
763
764 if (!reader.PopArrayOfBytesAsProto(&request)) {
765 LOG(ERROR) << "Unable to parse request";
766 writer.AppendProtoAsArrayOfBytes(response);
767 return dbus_response;
768 }
769
Garrick Evans51d5b552020-01-30 10:42:06 +0900770 StopCrosVm(request.cid(), GuestMessage::TERMINA_VM);
771
772 writer.AppendProtoAsArrayOfBytes(response);
773 return dbus_response;
774}
775
776std::unique_ptr<dbus::Response> Manager::OnPluginVmStartup(
777 dbus::MethodCall* method_call) {
778 LOG(INFO) << "Plugin VM starting up";
779
780 std::unique_ptr<dbus::Response> dbus_response(
781 dbus::Response::FromMethodCall(method_call));
782
783 dbus::MessageReader reader(method_call);
784 dbus::MessageWriter writer(dbus_response.get());
785
786 patchpanel::PluginVmStartupRequest request;
787 patchpanel::PluginVmStartupResponse response;
788
789 if (!reader.PopArrayOfBytesAsProto(&request)) {
790 LOG(ERROR) << "Unable to parse request";
791 writer.AppendProtoAsArrayOfBytes(response);
792 return dbus_response;
793 }
794
Garrick Evans08fb34b2020-02-20 10:50:17 +0900795 const uint64_t vm_id = request.id();
Garrick Evans53a2a982020-02-05 10:53:35 +0900796 if (!StartCrosVm(vm_id, GuestMessage::PLUGIN_VM, request.subnet_index())) {
Garrick Evans51d5b552020-01-30 10:42:06 +0900797 LOG(ERROR) << "Failed to start Plugin VM network service";
798 writer.AppendProtoAsArrayOfBytes(response);
799 return dbus_response;
800 }
801
802 const auto* const tap = cros_svc_->TAP(vm_id, false /*is_termina*/);
803 if (!tap) {
804 LOG(DFATAL) << "TAP device missing";
805 writer.AppendProtoAsArrayOfBytes(response);
806 return dbus_response;
807 }
808
Garrick Evans51d5b552020-01-30 10:42:06 +0900809 auto* dev = response.mutable_device();
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900810 dev->set_ifname(tap->host_ifname());
Garrick Evansa0c9c972020-11-30 09:54:59 +0900811 dev->set_guest_type(NetworkDevice::PLUGIN_VM);
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900812 const auto* subnet = tap->config().ipv4_subnet();
Garrick Evans51d5b552020-01-30 10:42:06 +0900813 if (!subnet) {
814 LOG(DFATAL) << "Missing required subnet for {cid: " << vm_id << "}";
815 writer.AppendProtoAsArrayOfBytes(response);
816 return dbus_response;
817 }
818 auto* resp_subnet = dev->mutable_ipv4_subnet();
819 resp_subnet->set_base_addr(subnet->BaseAddress());
820 resp_subnet->set_prefix_len(subnet->PrefixLength());
821
822 writer.AppendProtoAsArrayOfBytes(response);
823 return dbus_response;
824}
825
826std::unique_ptr<dbus::Response> Manager::OnPluginVmShutdown(
827 dbus::MethodCall* method_call) {
828 LOG(INFO) << "Plugin VM shutting down";
829
830 std::unique_ptr<dbus::Response> dbus_response(
831 dbus::Response::FromMethodCall(method_call));
832
833 dbus::MessageReader reader(method_call);
834 dbus::MessageWriter writer(dbus_response.get());
835
836 patchpanel::PluginVmShutdownRequest request;
837 patchpanel::PluginVmShutdownResponse response;
838
839 if (!reader.PopArrayOfBytesAsProto(&request)) {
840 LOG(ERROR) << "Unable to parse request";
841 writer.AppendProtoAsArrayOfBytes(response);
842 return dbus_response;
843 }
844
845 StopCrosVm(request.id(), GuestMessage::PLUGIN_VM);
Garrick Evans47c19272019-11-21 10:58:21 +0900846
847 writer.AppendProtoAsArrayOfBytes(response);
848 return dbus_response;
849}
850
Hugo Benichi7d9d8db2020-03-30 15:56:56 +0900851std::unique_ptr<dbus::Response> Manager::OnSetVpnIntent(
852 dbus::MethodCall* method_call) {
853 std::unique_ptr<dbus::Response> dbus_response(
854 dbus::Response::FromMethodCall(method_call));
855
856 dbus::MessageReader reader(method_call);
857 dbus::MessageWriter writer(dbus_response.get());
858
859 patchpanel::SetVpnIntentRequest request;
860 patchpanel::SetVpnIntentResponse response;
861
862 bool success = reader.PopArrayOfBytesAsProto(&request);
863 if (!success) {
864 LOG(ERROR) << "Unable to parse SetVpnIntentRequest";
865 // Do not return yet to make sure we close the received fd.
866 }
867
868 base::ScopedFD client_socket;
869 reader.PopFileDescriptor(&client_socket);
870
871 if (success)
872 success = routing_svc_->SetVpnFwmark(client_socket.get(), request.policy());
873
874 response.set_success(success);
Hugo Benichib56b77c2020-01-15 16:00:56 +0900875
876 writer.AppendProtoAsArrayOfBytes(response);
877 return dbus_response;
878}
879
880std::unique_ptr<dbus::Response> Manager::OnConnectNamespace(
881 dbus::MethodCall* method_call) {
882 std::unique_ptr<dbus::Response> dbus_response(
883 dbus::Response::FromMethodCall(method_call));
884
885 dbus::MessageReader reader(method_call);
886 dbus::MessageWriter writer(dbus_response.get());
887
888 patchpanel::ConnectNamespaceRequest request;
Hugo Benichib56b77c2020-01-15 16:00:56 +0900889
890 if (!reader.PopArrayOfBytesAsProto(&request)) {
Hugo Benichicc6850f2020-01-17 13:26:06 +0900891 LOG(ERROR) << "Unable to parse ConnectNamespaceRequest";
892 // Do not return yet to make sure we close the received fd and
893 // validate other arguments.
Hugo Benichi7c342672020-09-08 09:18:14 +0900894 writer.AppendProtoAsArrayOfBytes(patchpanel::ConnectNamespaceResponse());
895 return dbus_response;
Hugo Benichib56b77c2020-01-15 16:00:56 +0900896 }
897
Hugo Benichicc6850f2020-01-17 13:26:06 +0900898 base::ScopedFD client_fd;
899 reader.PopFileDescriptor(&client_fd);
900 if (!client_fd.is_valid()) {
901 LOG(ERROR) << "ConnectNamespaceRequest: invalid file descriptor";
Hugo Benichi7c342672020-09-08 09:18:14 +0900902 writer.AppendProtoAsArrayOfBytes(patchpanel::ConnectNamespaceResponse());
903 return dbus_response;
Hugo Benichicc6850f2020-01-17 13:26:06 +0900904 }
905
906 pid_t pid = request.pid();
Garrick Evans263bd952020-12-03 20:27:43 +0900907 if (pid == 1 || pid == getpid()) {
908 LOG(ERROR) << "ConnectNamespaceRequest: privileged namespace pid " << pid;
909 writer.AppendProtoAsArrayOfBytes(patchpanel::ConnectNamespaceResponse());
910 return dbus_response;
911 }
Hugo Benichicc6850f2020-01-17 13:26:06 +0900912 {
Hugo Benichi0781d402021-02-22 13:43:11 +0900913 ScopedNS ns(pid, ScopedNS::Type::Network);
Hugo Benichicc6850f2020-01-17 13:26:06 +0900914 if (!ns.IsValid()) {
915 LOG(ERROR) << "ConnectNamespaceRequest: invalid namespace pid " << pid;
Hugo Benichi7c342672020-09-08 09:18:14 +0900916 writer.AppendProtoAsArrayOfBytes(patchpanel::ConnectNamespaceResponse());
917 return dbus_response;
Hugo Benichicc6850f2020-01-17 13:26:06 +0900918 }
919 }
920
921 const std::string& outbound_ifname = request.outbound_physical_device();
922 if (!outbound_ifname.empty() && !shill_client_->has_device(outbound_ifname)) {
923 LOG(ERROR) << "ConnectNamespaceRequest: invalid outbound ifname "
924 << outbound_ifname;
Hugo Benichi7c342672020-09-08 09:18:14 +0900925 writer.AppendProtoAsArrayOfBytes(patchpanel::ConnectNamespaceResponse());
926 return dbus_response;
Hugo Benichicc6850f2020-01-17 13:26:06 +0900927 }
928
Hugo Benichi7c342672020-09-08 09:18:14 +0900929 auto response = ConnectNamespace(std::move(client_fd), request);
930 writer.AppendProtoAsArrayOfBytes(*response);
Hugo Benichi7d9d8db2020-03-30 15:56:56 +0900931 return dbus_response;
932}
933
Jie Jiang493cde42020-07-17 21:43:39 +0900934std::unique_ptr<dbus::Response> Manager::OnGetTrafficCounters(
935 dbus::MethodCall* method_call) {
936 std::unique_ptr<dbus::Response> dbus_response(
937 dbus::Response::FromMethodCall(method_call));
938
939 dbus::MessageReader reader(method_call);
940 dbus::MessageWriter writer(dbus_response.get());
941
942 patchpanel::TrafficCountersRequest request;
943 patchpanel::TrafficCountersResponse response;
944
945 if (!reader.PopArrayOfBytesAsProto(&request)) {
946 LOG(ERROR) << "Unable to parse TrafficCountersRequest";
947 writer.AppendProtoAsArrayOfBytes(response);
948 return dbus_response;
949 }
950
951 const std::set<std::string> devices{request.devices().begin(),
952 request.devices().end()};
953 const auto counters = counters_svc_->GetCounters(devices);
954 for (const auto& kv : counters) {
955 auto* traffic_counter = response.add_counters();
956 const auto& source_and_device = kv.first;
957 const auto& counter = kv.second;
958 traffic_counter->set_source(source_and_device.first);
959 traffic_counter->set_device(source_and_device.second);
960 traffic_counter->set_rx_bytes(counter.rx_bytes);
961 traffic_counter->set_rx_packets(counter.rx_packets);
962 traffic_counter->set_tx_bytes(counter.tx_bytes);
963 traffic_counter->set_tx_packets(counter.tx_packets);
964 }
965
966 writer.AppendProtoAsArrayOfBytes(response);
967 return dbus_response;
968}
969
Jason Jeremy Imanb2a421e2020-07-04 20:48:32 +0900970std::unique_ptr<dbus::Response> Manager::OnModifyPortRule(
971 dbus::MethodCall* method_call) {
972 std::unique_ptr<dbus::Response> dbus_response(
973 dbus::Response::FromMethodCall(method_call));
974
975 dbus::MessageReader reader(method_call);
976 dbus::MessageWriter writer(dbus_response.get());
977
978 patchpanel::ModifyPortRuleRequest request;
979 patchpanel::ModifyPortRuleResponse response;
980
981 if (!reader.PopArrayOfBytesAsProto(&request)) {
982 LOG(ERROR) << "Unable to parse ModifyPortRequest";
983 writer.AppendProtoAsArrayOfBytes(response);
984 return dbus_response;
985 }
986
Jason Jeremy Imanc28df852020-07-11 17:38:26 +0900987 response.set_success(ModifyPortRule(request));
Jason Jeremy Imanb2a421e2020-07-04 20:48:32 +0900988 writer.AppendProtoAsArrayOfBytes(response);
989 return dbus_response;
990}
991
Jie Jiang25c1b972020-11-12 15:42:53 +0900992void Manager::OnNeighborReachabilityEvent(
Jie Jiang84966852020-09-18 18:49:05 +0900993 int ifindex,
994 const shill::IPAddress& ip_addr,
995 NeighborLinkMonitor::NeighborRole role,
Jie Jiang25c1b972020-11-12 15:42:53 +0900996 NeighborReachabilityEventSignal::EventType event_type) {
Jie Jiang84966852020-09-18 18:49:05 +0900997 if (!ip_addr.IsValid()) {
998 LOG(DFATAL) << "ip_addr is not valid";
999 return;
1000 }
1001
Jie Jiang25c1b972020-11-12 15:42:53 +09001002 using SignalProto = NeighborReachabilityEventSignal;
Jie Jiang84966852020-09-18 18:49:05 +09001003 SignalProto proto;
1004 proto.set_ifindex(ifindex);
1005 proto.set_ip_addr(ip_addr.ToString());
Jie Jiang25c1b972020-11-12 15:42:53 +09001006 proto.set_type(event_type);
Jie Jiang84966852020-09-18 18:49:05 +09001007 switch (role) {
1008 case NeighborLinkMonitor::NeighborRole::kGateway:
1009 proto.set_role(SignalProto::GATEWAY);
1010 break;
1011 case NeighborLinkMonitor::NeighborRole::kDNSServer:
1012 proto.set_role(SignalProto::DNS_SERVER);
1013 break;
1014 case NeighborLinkMonitor::NeighborRole::kGatewayAndDNSServer:
1015 proto.set_role(SignalProto::GATEWAY_AND_DNS_SERVER);
1016 break;
1017 default:
1018 NOTREACHED();
1019 }
1020
Jie Jiang25c1b972020-11-12 15:42:53 +09001021 dbus::Signal signal(kPatchPanelInterface, kNeighborReachabilityEventSignal);
Jie Jiang84966852020-09-18 18:49:05 +09001022 dbus::MessageWriter writer(&signal);
1023 if (!writer.AppendProtoAsArrayOfBytes(proto)) {
Jie Jiang25c1b972020-11-12 15:42:53 +09001024 LOG(ERROR) << "Failed to encode proto NeighborReachabilityEventSignal";
Jie Jiang84966852020-09-18 18:49:05 +09001025 return;
1026 }
1027
1028 dbus_svc_path_->SendSignal(&signal);
1029}
1030
Hugo Benichi7c342672020-09-08 09:18:14 +09001031std::unique_ptr<patchpanel::ConnectNamespaceResponse> Manager::ConnectNamespace(
Hugo Benichiadf1ec52020-01-17 16:23:58 +09001032 base::ScopedFD client_fd,
Hugo Benichi7c342672020-09-08 09:18:14 +09001033 const patchpanel::ConnectNamespaceRequest& request) {
1034 auto response = std::make_unique<patchpanel::ConnectNamespaceResponse>();
1035
Hugo Benichiadf1ec52020-01-17 16:23:58 +09001036 std::unique_ptr<Subnet> subnet =
1037 addr_mgr_.AllocateIPv4Subnet(AddressManager::Guest::MINIJAIL_NETNS);
1038 if (!subnet) {
Hugo Benichi7c342672020-09-08 09:18:14 +09001039 LOG(ERROR) << "ConnectNamespace: exhausted IPv4 subnet space";
1040 return response;
Hugo Benichie8758b52020-04-03 14:49:01 +09001041 }
Hugo Benichiadf1ec52020-01-17 16:23:58 +09001042
Hugo Benichi7352ad92020-04-07 16:11:59 +09001043 // Dup the client fd into our own: this guarantees that the fd number will
1044 // be stable and tied to the actual kernel resources used by the client.
1045 base::ScopedFD local_client_fd(dup(client_fd.get()));
1046 if (!local_client_fd.is_valid()) {
Hugo Benichi7c342672020-09-08 09:18:14 +09001047 PLOG(ERROR) << "ConnectNamespace: failed to dup() client fd";
1048 return response;
Hugo Benichi7352ad92020-04-07 16:11:59 +09001049 }
1050
Hugo Benichi7c342672020-09-08 09:18:14 +09001051 // Add the duped fd to the epoll watcher.
Hugo Benichi7352ad92020-04-07 16:11:59 +09001052 // TODO(hugobenichi) Find a way to reuse base::FileDescriptorWatcher for
1053 // listening to EPOLLHUP.
1054 struct epoll_event epevent;
1055 epevent.events = EPOLLIN; // EPOLLERR | EPOLLHUP are always waited for.
1056 epevent.data.fd = local_client_fd.get();
1057 if (epoll_ctl(connected_namespaces_epollfd_, EPOLL_CTL_ADD,
1058 local_client_fd.get(), &epevent) != 0) {
Hugo Benichi7c342672020-09-08 09:18:14 +09001059 PLOG(ERROR) << "ConnectNamespace: epoll_ctl(EPOLL_CTL_ADD) failed";
1060 return response;
Hugo Benichi7352ad92020-04-07 16:11:59 +09001061 }
1062
Hugo Benichi7c342672020-09-08 09:18:14 +09001063 const std::string ifname_id = std::to_string(connected_namespaces_next_id_);
Hugo Benichifcf81022020-12-04 11:01:37 +09001064 ConnectedNamespace nsinfo = {};
1065 nsinfo.pid = request.pid();
1066 nsinfo.netns_name = "connected_netns_" + ifname_id;
Hugo Benichi93306e52020-12-04 16:08:00 +09001067 nsinfo.source = ProtoToTrafficSource(request.traffic_source());
1068 if (nsinfo.source == TrafficSource::UNKNOWN)
1069 nsinfo.source = TrafficSource::SYSTEM;
Hugo Benichifcf81022020-12-04 11:01:37 +09001070 nsinfo.outbound_ifname = request.outbound_physical_device();
Hugo Benichi93306e52020-12-04 16:08:00 +09001071 nsinfo.route_on_vpn = request.route_on_vpn();
Hugo Benichifcf81022020-12-04 11:01:37 +09001072 nsinfo.host_ifname = "arc_ns" + ifname_id;
1073 nsinfo.peer_ifname = "veth" + ifname_id;
1074 nsinfo.peer_subnet = std::move(subnet);
1075 nsinfo.peer_mac_addr = addr_mgr_.GenerateMacAddress();
1076
1077 if (!datapath_->StartRoutingNamespace(nsinfo)) {
Hugo Benichi7c342672020-09-08 09:18:14 +09001078 LOG(ERROR) << "ConnectNamespace: failed to setup datapath";
1079 if (epoll_ctl(connected_namespaces_epollfd_, EPOLL_CTL_DEL,
1080 local_client_fd.get(), nullptr) != 0)
1081 PLOG(ERROR) << "ConnectNamespace: epoll_ctl(EPOLL_CTL_DEL) failed";
1082 return response;
1083 }
1084
Hugo Benichifcf81022020-12-04 11:01:37 +09001085 // Prepare the response before storing ConnectedNamespace.
1086 response->set_peer_ifname(nsinfo.peer_ifname);
1087 response->set_peer_ipv4_address(nsinfo.peer_subnet->AddressAtOffset(1));
1088 response->set_host_ifname(nsinfo.host_ifname);
1089 response->set_host_ipv4_address(nsinfo.peer_subnet->AddressAtOffset(0));
Hugo Benichi7c342672020-09-08 09:18:14 +09001090 auto* response_subnet = response->mutable_ipv4_subnet();
Hugo Benichifcf81022020-12-04 11:01:37 +09001091 response_subnet->set_base_addr(nsinfo.peer_subnet->BaseAddress());
1092 response_subnet->set_prefix_len(nsinfo.peer_subnet->PrefixLength());
Hugo Benichiadf1ec52020-01-17 16:23:58 +09001093
Hugo Benichifcf81022020-12-04 11:01:37 +09001094 LOG(INFO) << "Connected network namespace " << nsinfo;
1095
1096 // Store ConnectedNamespace
Hugo Benichiadf1ec52020-01-17 16:23:58 +09001097 connected_namespaces_next_id_++;
Hugo Benichi7352ad92020-04-07 16:11:59 +09001098 int fdkey = local_client_fd.release();
Hugo Benichifcf81022020-12-04 11:01:37 +09001099 connected_namespaces_.insert(std::make_pair(fdkey, std::move(nsinfo)));
Hugo Benichi7352ad92020-04-07 16:11:59 +09001100
1101 if (connected_namespaces_.size() == 1) {
1102 LOG(INFO) << "Starting ConnectNamespace client fds monitoring";
1103 CheckConnectedNamespaces();
1104 }
Hugo Benichi7c342672020-09-08 09:18:14 +09001105
1106 return response;
Hugo Benichiadf1ec52020-01-17 16:23:58 +09001107}
1108
1109void Manager::DisconnectNamespace(int client_fd) {
1110 auto it = connected_namespaces_.find(client_fd);
1111 if (it == connected_namespaces_.end()) {
Hugo Benichifcf81022020-12-04 11:01:37 +09001112 LOG(ERROR) << "No ConnectedNamespace found for client_fd " << client_fd;
Hugo Benichiadf1ec52020-01-17 16:23:58 +09001113 return;
1114 }
1115
Hugo Benichi7352ad92020-04-07 16:11:59 +09001116 // Remove the client fd dupe from the epoll watcher and close it.
1117 if (epoll_ctl(connected_namespaces_epollfd_, EPOLL_CTL_DEL, client_fd,
Hugo Benichie8758b52020-04-03 14:49:01 +09001118 nullptr) != 0)
Hugo Benichi7352ad92020-04-07 16:11:59 +09001119 PLOG(ERROR) << "DisconnectNamespace: epoll_ctl(EPOLL_CTL_DEL) failed";
Hugo Benichie8758b52020-04-03 14:49:01 +09001120 if (close(client_fd) < 0)
Hugo Benichi7352ad92020-04-07 16:11:59 +09001121 PLOG(ERROR) << "DisconnectNamespace: close(client_fd) failed";
Hugo Benichi7352ad92020-04-07 16:11:59 +09001122
Hugo Benichifcf81022020-12-04 11:01:37 +09001123 datapath_->StopRoutingNamespace(it->second);
Hugo Benichiadf1ec52020-01-17 16:23:58 +09001124 LOG(INFO) << "Disconnected network namespace " << it->second;
Hugo Benichiadf1ec52020-01-17 16:23:58 +09001125 // This release the allocated IPv4 subnet.
1126 connected_namespaces_.erase(it);
1127}
1128
Hugo Benichi7352ad92020-04-07 16:11:59 +09001129// TODO(hugobenichi) Generalize this check to all resources created by
1130// patchpanel on behalf of a remote client.
1131void Manager::CheckConnectedNamespaces() {
1132 int max_event = 10;
1133 struct epoll_event epevents[max_event];
1134 int nready = epoll_wait(connected_namespaces_epollfd_, epevents, max_event,
1135 0 /* do not block */);
1136 if (nready < 0)
1137 PLOG(ERROR) << "CheckConnectedNamespaces: epoll_wait(0) failed";
1138
1139 for (int i = 0; i < nready; i++)
1140 if (epevents[i].events & (EPOLLHUP | EPOLLERR))
1141 DisconnectNamespace(epevents[i].data.fd);
1142
1143 if (connected_namespaces_.empty()) {
1144 LOG(INFO) << "Stopping ConnectNamespace client fds monitoring";
1145 return;
1146 }
1147
Qijiang Fan2d7aeb42020-05-19 02:06:39 +09001148 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
Hugo Benichi7352ad92020-04-07 16:11:59 +09001149 FROM_HERE,
1150 base::Bind(&Manager::CheckConnectedNamespaces,
Hugo Benichie8758b52020-04-03 14:49:01 +09001151 weak_factory_.GetWeakPtr()),
Hugo Benichi7352ad92020-04-07 16:11:59 +09001152 kConnectNamespaceCheckInterval);
1153}
1154
Jason Jeremy Imanc28df852020-07-11 17:38:26 +09001155bool Manager::ModifyPortRule(const patchpanel::ModifyPortRuleRequest& request) {
1156 switch (request.proto()) {
1157 case patchpanel::ModifyPortRuleRequest::TCP:
1158 case patchpanel::ModifyPortRuleRequest::UDP:
1159 break;
1160 default:
1161 LOG(ERROR) << "Unknown protocol " << request.proto();
1162 return false;
1163 }
1164
1165 switch (request.op()) {
1166 case patchpanel::ModifyPortRuleRequest::CREATE:
1167 switch (request.type()) {
1168 case patchpanel::ModifyPortRuleRequest::ACCESS:
1169 return firewall_.AddAcceptRules(request.proto(),
1170 request.input_dst_port(),
1171 request.input_ifname());
1172 case patchpanel::ModifyPortRuleRequest::LOCKDOWN:
1173 return firewall_.AddLoopbackLockdownRules(request.proto(),
1174 request.input_dst_port());
1175 case patchpanel::ModifyPortRuleRequest::FORWARDING:
1176 return firewall_.AddIpv4ForwardRule(
1177 request.proto(), request.input_dst_ip(), request.input_dst_port(),
1178 request.input_ifname(), request.dst_ip(), request.dst_port());
1179 default:
1180 LOG(ERROR) << "Unknown port rule type " << request.type();
1181 return false;
1182 }
1183 case patchpanel::ModifyPortRuleRequest::DELETE:
1184 switch (request.type()) {
1185 case patchpanel::ModifyPortRuleRequest::ACCESS:
1186 return firewall_.DeleteAcceptRules(request.proto(),
1187 request.input_dst_port(),
1188 request.input_ifname());
1189 case patchpanel::ModifyPortRuleRequest::LOCKDOWN:
1190 return firewall_.DeleteLoopbackLockdownRules(
1191 request.proto(), request.input_dst_port());
1192 case patchpanel::ModifyPortRuleRequest::FORWARDING:
1193 return firewall_.DeleteIpv4ForwardRule(
1194 request.proto(), request.input_dst_ip(), request.input_dst_port(),
1195 request.input_ifname(), request.dst_ip(), request.dst_port());
1196 default:
1197 LOG(ERROR) << "Unknown port rule type " << request.type();
1198 return false;
1199 }
1200 default:
1201 LOG(ERROR) << "Unknown operation " << request.op();
1202 return false;
1203 }
1204}
1205
Garrick Evanse94a14e2019-11-11 10:32:13 +09001206void Manager::SendGuestMessage(const GuestMessage& msg) {
Garrick Evans96e03042019-05-28 14:30:52 +09001207 IpHelperMessage ipm;
1208 *ipm.mutable_guest_message() = msg;
Garrick Evans96e03042019-05-28 14:30:52 +09001209 adb_proxy_->SendMessage(ipm);
Garrick Evanse94a14e2019-11-11 10:32:13 +09001210 mcast_proxy_->SendMessage(ipm);
1211 nd_proxy_->SendMessage(ipm);
Garrick Evans96e03042019-05-28 14:30:52 +09001212}
1213
Garrick Evans4ac09852020-01-16 14:09:22 +09001214void Manager::StartForwarding(const std::string& ifname_physical,
1215 const std::string& ifname_virtual,
Garrick Evans4ac09852020-01-16 14:09:22 +09001216 bool ipv6,
1217 bool multicast) {
Taoyu Li7dca19a2020-03-16 16:27:07 +09001218 if (ifname_physical.empty() || ifname_virtual.empty())
Garrick Evans4ac09852020-01-16 14:09:22 +09001219 return;
1220
1221 IpHelperMessage ipm;
1222 DeviceMessage* msg = ipm.mutable_device_message();
1223 msg->set_dev_ifname(ifname_physical);
Garrick Evans4ac09852020-01-16 14:09:22 +09001224 msg->set_br_ifname(ifname_virtual);
1225
1226 if (ipv6) {
1227 LOG(INFO) << "Starting IPv6 forwarding from " << ifname_physical << " to "
1228 << ifname_virtual;
1229
1230 if (!datapath_->AddIPv6Forwarding(ifname_physical, ifname_virtual)) {
1231 LOG(ERROR) << "Failed to setup iptables forwarding rule for IPv6 from "
1232 << ifname_physical << " to " << ifname_virtual;
1233 }
1234 if (!datapath_->MaskInterfaceFlags(ifname_physical, IFF_ALLMULTI)) {
1235 LOG(WARNING) << "Failed to setup all multicast mode for interface "
1236 << ifname_physical;
1237 }
1238 if (!datapath_->MaskInterfaceFlags(ifname_virtual, IFF_ALLMULTI)) {
1239 LOG(WARNING) << "Failed to setup all multicast mode for interface "
1240 << ifname_virtual;
1241 }
1242 nd_proxy_->SendMessage(ipm);
1243 }
1244
1245 if (multicast) {
Hugo Benichi69c989d2021-03-01 00:23:39 +09001246 multicast_virtual_ifnames_.insert(ifname_virtual);
Garrick Evans4ac09852020-01-16 14:09:22 +09001247 LOG(INFO) << "Starting multicast forwarding from " << ifname_physical
1248 << " to " << ifname_virtual;
1249 mcast_proxy_->SendMessage(ipm);
1250 }
1251}
1252
1253void Manager::StopForwarding(const std::string& ifname_physical,
1254 const std::string& ifname_virtual,
1255 bool ipv6,
1256 bool multicast) {
1257 if (ifname_physical.empty())
1258 return;
1259
1260 IpHelperMessage ipm;
1261 DeviceMessage* msg = ipm.mutable_device_message();
1262 msg->set_dev_ifname(ifname_physical);
1263 msg->set_teardown(true);
Taoyu Li7dca19a2020-03-16 16:27:07 +09001264 if (!ifname_virtual.empty()) {
1265 msg->set_br_ifname(ifname_virtual);
1266 }
Garrick Evans4ac09852020-01-16 14:09:22 +09001267
1268 if (ipv6) {
Taoyu Li7dca19a2020-03-16 16:27:07 +09001269 if (ifname_virtual.empty()) {
1270 LOG(INFO) << "Stopping IPv6 forwarding on " << ifname_physical;
1271 } else {
1272 LOG(INFO) << "Stopping IPv6 forwarding from " << ifname_physical << " to "
1273 << ifname_virtual;
1274 datapath_->RemoveIPv6Forwarding(ifname_physical, ifname_virtual);
1275 }
Garrick Evans4ac09852020-01-16 14:09:22 +09001276 nd_proxy_->SendMessage(ipm);
1277 }
1278
1279 if (multicast) {
Hugo Benichi69c989d2021-03-01 00:23:39 +09001280 multicast_virtual_ifnames_.erase(ifname_virtual);
Taoyu Li7dca19a2020-03-16 16:27:07 +09001281 if (ifname_virtual.empty()) {
1282 LOG(INFO) << "Stopping multicast forwarding on " << ifname_physical;
1283 } else {
1284 LOG(INFO) << "Stopping multicast forwarding from " << ifname_physical
1285 << " to " << ifname_virtual;
1286 }
Garrick Evans4ac09852020-01-16 14:09:22 +09001287 mcast_proxy_->SendMessage(ipm);
1288 }
1289}
1290
Taoyu Lia0727dc2020-09-24 19:54:59 +09001291void Manager::OnNDProxyMessage(const NDProxyMessage& msg) {
1292 LOG_IF(DFATAL, msg.ifname().empty())
Garrick Evans4ac09852020-01-16 14:09:22 +09001293 << "Received DeviceMessage w/ empty dev_ifname";
Taoyu Lia0727dc2020-09-24 19:54:59 +09001294 switch (msg.type()) {
1295 case NDProxyMessage::ADD_ROUTE:
1296 if (!datapath_->AddIPv6HostRoute(msg.ifname(), msg.ip6addr(), 128)) {
1297 LOG(WARNING) << "Failed to setup the IPv6 route for interface "
1298 << msg.ifname() << ", addr " << msg.ip6addr();
1299 }
1300 break;
1301 case NDProxyMessage::ADD_ADDR:
1302 if (!datapath_->AddIPv6Address(msg.ifname(), msg.ip6addr())) {
1303 LOG(WARNING) << "Failed to setup the IPv6 address for interface "
1304 << msg.ifname() << ", addr " << msg.ip6addr();
1305 }
1306 break;
1307 case NDProxyMessage::DEL_ADDR:
1308 datapath_->RemoveIPv6Address(msg.ifname(), msg.ip6addr());
1309 break;
1310 default:
1311 LOG(ERROR) << "Unknown NDProxy event " << msg.type();
1312 NOTREACHED();
Garrick Evans4ac09852020-01-16 14:09:22 +09001313 }
1314}
1315
Garrick Evans3388a032020-03-24 11:25:55 +09001316} // namespace patchpanel