blob: 841526ce5f1f37b43f83037ececde32d3c6de7b0 [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>
Taoyu Lia0727dc2020-09-24 19:54:59 +090019#include <base/files/scoped_file.h>
Kevin Cernekee95d4ae92016-06-19 10:26:29 -070020#include <base/logging.h>
Taoyu Lic85c44b2019-12-04 17:32:57 +090021#include <base/strings/string_number_conversions.h>
Garrick Evans96e03042019-05-28 14:30:52 +090022#include <base/strings/string_split.h>
Garrick Evans6f258d02019-06-28 16:32:07 +090023#include <base/strings/string_util.h>
Taoyu Li179dcc62019-10-17 11:21:08 +090024#include <base/strings/stringprintf.h>
hschamf9546312020-04-14 15:12:40 +090025#include <base/threading/thread_task_runner_handle.h>
Taoyu Lic85c44b2019-12-04 17:32:57 +090026#include <brillo/key_value_store.h>
Kevin Cernekee27bcaa62016-12-03 11:16:26 -080027#include <brillo/minijail/minijail.h>
28
Garrick Evans3388a032020-03-24 11:25:55 +090029#include "patchpanel/ipc.pb.h"
30#include "patchpanel/mac_address_generator.h"
31#include "patchpanel/net_util.h"
32#include "patchpanel/routing_service.h"
33#include "patchpanel/scoped_ns.h"
Garrick Evans428e4762018-12-11 15:18:42 +090034
Garrick Evans3388a032020-03-24 11:25:55 +090035namespace patchpanel {
Garrick Evans08843932019-09-17 14:41:08 +090036namespace {
Garrick Evans4c042572019-12-17 13:42:25 +090037constexpr int kSubprocessRestartDelayMs = 900;
Garrick Evans08843932019-09-17 14:41:08 +090038
Hugo Benichi321f23b2020-09-25 15:42:05 +090039// Constants used for dropping locally originated traffic bound to an incorrect
40// source IPv4 address.
41constexpr char kGuestIPv4Subnet[] = "100.115.92.0/23";
42constexpr std::array<const char*, 6> kPhysicalIfnamePrefixes{
43 {"eth+", "wlan+", "mlan+", "usb+", "wwan+", "rmnet+"}};
44
Hugo Benichi7352ad92020-04-07 16:11:59 +090045// Time interval between epoll checks on file descriptors committed by callers
46// of ConnectNamespace DBus API.
47constexpr const base::TimeDelta kConnectNamespaceCheckInterval =
Hugo Benichifa9462e2020-06-26 09:50:48 +090048 base::TimeDelta::FromSeconds(5);
Hugo Benichi7352ad92020-04-07 16:11:59 +090049
Garrick Evans08843932019-09-17 14:41:08 +090050// Passes |method_call| to |handler| and passes the response to
51// |response_sender|. If |handler| returns nullptr, an empty response is
52// created and sent.
53void HandleSynchronousDBusMethodCall(
54 base::Callback<std::unique_ptr<dbus::Response>(dbus::MethodCall*)> handler,
55 dbus::MethodCall* method_call,
56 dbus::ExportedObject::ResponseSender response_sender) {
57 std::unique_ptr<dbus::Response> response = handler.Run(method_call);
58 if (!response)
59 response = dbus::Response::FromMethodCall(method_call);
Qijiang Fan0c657d42020-08-24 19:07:50 +090060 std::move(response_sender).Run(std::move(response));
Garrick Evans08843932019-09-17 14:41:08 +090061}
62
63} // namespace
Kevin Cernekee95d4ae92016-06-19 10:26:29 -070064
Taoyu Lice7caa62019-10-01 15:43:33 +090065Manager::Manager(std::unique_ptr<HelperProcess> adb_proxy,
Jason Jeremy Imand89b5f52019-10-24 10:39:17 +090066 std::unique_ptr<HelperProcess> mcast_proxy,
Garrick Evans1f5a3612019-11-08 12:59:03 +090067 std::unique_ptr<HelperProcess> nd_proxy)
Garrick Evans3915af32019-07-25 15:44:34 +090068 : adb_proxy_(std::move(adb_proxy)),
Jason Jeremy Imand89b5f52019-10-24 10:39:17 +090069 mcast_proxy_(std::move(mcast_proxy)),
Garrick Evans4ee5ce22020-03-18 07:05:17 +090070 nd_proxy_(std::move(nd_proxy)) {
Taoyu Li179dcc62019-10-17 11:21:08 +090071 runner_ = std::make_unique<MinijailedProcessRunner>();
Jason Jeremy Imana7273a32020-08-04 11:25:31 +090072 datapath_ = std::make_unique<Datapath>(runner_.get(), &firewall_);
Hugo Benichi7352ad92020-04-07 16:11:59 +090073 connected_namespaces_epollfd_ = epoll_create(1 /* size */);
Taoyu Li179dcc62019-10-17 11:21:08 +090074}
Long Chengd4415582019-09-24 19:16:09 +000075
Garrick Evans207e7482019-12-16 11:54:36 +090076Manager::~Manager() {
77 OnShutdown(nullptr);
78}
79
Jason Jeremy Imanf4156cb2019-11-14 15:36:22 +090080std::map<const std::string, bool> Manager::cached_feature_enabled_ = {};
81
82bool Manager::ShouldEnableFeature(
83 int min_android_sdk_version,
84 int min_chrome_milestone,
85 const std::vector<std::string>& supported_boards,
86 const std::string& feature_name) {
87 static const char kLsbReleasePath[] = "/etc/lsb-release";
88
89 const auto& cached_result = cached_feature_enabled_.find(feature_name);
90 if (cached_result != cached_feature_enabled_.end())
91 return cached_result->second;
92
93 auto check = [min_android_sdk_version, min_chrome_milestone,
94 &supported_boards, &feature_name]() {
95 brillo::KeyValueStore store;
96 if (!store.Load(base::FilePath(kLsbReleasePath))) {
97 LOG(ERROR) << "Could not read lsb-release";
98 return false;
99 }
100
101 std::string value;
102 if (!store.GetString("CHROMEOS_ARC_ANDROID_SDK_VERSION", &value)) {
103 LOG(ERROR) << feature_name
104 << " disabled - cannot determine Android SDK version";
105 return false;
106 }
107 int ver = 0;
108 if (!base::StringToInt(value.c_str(), &ver)) {
109 LOG(ERROR) << feature_name << " disabled - invalid Android SDK version";
110 return false;
111 }
112 if (ver < min_android_sdk_version) {
113 LOG(INFO) << feature_name << " disabled for Android SDK " << value;
114 return false;
115 }
116
117 if (!store.GetString("CHROMEOS_RELEASE_CHROME_MILESTONE", &value)) {
118 LOG(ERROR) << feature_name
119 << " disabled - cannot determine ChromeOS milestone";
120 return false;
121 }
122 if (!base::StringToInt(value.c_str(), &ver)) {
123 LOG(ERROR) << feature_name << " disabled - invalid ChromeOS milestone";
124 return false;
125 }
126 if (ver < min_chrome_milestone) {
127 LOG(INFO) << feature_name << " disabled for ChromeOS milestone " << value;
128 return false;
129 }
130
131 if (!store.GetString("CHROMEOS_RELEASE_BOARD", &value)) {
132 LOG(ERROR) << feature_name << " disabled - cannot determine board";
133 return false;
134 }
135 if (!supported_boards.empty() &&
136 std::find(supported_boards.begin(), supported_boards.end(), value) ==
137 supported_boards.end()) {
138 LOG(INFO) << feature_name << " disabled for board " << value;
139 return false;
140 }
141 return true;
142 };
143
144 bool result = check();
145 cached_feature_enabled_.emplace(feature_name, result);
146 return result;
147}
148
Kevin Cernekee95d4ae92016-06-19 10:26:29 -0700149int Manager::OnInit() {
Garrick Evans54861622019-07-19 09:05:09 +0900150 prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
Kevin Cernekee27bcaa62016-12-03 11:16:26 -0800151
152 // Handle subprocess lifecycle.
153 process_reaper_.Register(this);
Hugo Benichi935eca92018-07-03 13:47:24 +0900154
155 CHECK(process_reaper_.WatchForChild(
Garrick Evans96e03042019-05-28 14:30:52 +0900156 FROM_HERE, adb_proxy_->pid(),
157 base::Bind(&Manager::OnSubprocessExited, weak_factory_.GetWeakPtr(),
158 adb_proxy_->pid())))
159 << "Failed to watch adb-proxy child process";
Taoyu Liaf944c92019-10-01 12:22:31 +0900160 CHECK(process_reaper_.WatchForChild(
Jason Jeremy Imand89b5f52019-10-24 10:39:17 +0900161 FROM_HERE, mcast_proxy_->pid(),
162 base::Bind(&Manager::OnSubprocessExited, weak_factory_.GetWeakPtr(),
163 nd_proxy_->pid())))
164 << "Failed to watch multicast-proxy child process";
165 CHECK(process_reaper_.WatchForChild(
Taoyu Liaf944c92019-10-01 12:22:31 +0900166 FROM_HERE, nd_proxy_->pid(),
167 base::Bind(&Manager::OnSubprocessExited, weak_factory_.GetWeakPtr(),
168 nd_proxy_->pid())))
Jason Jeremy Imand89b5f52019-10-24 10:39:17 +0900169 << "Failed to watch nd-proxy child process";
Garrick Evans96e03042019-05-28 14:30:52 +0900170
Garrick Evans49879532018-12-03 13:15:36 +0900171 // Run after Daemon::OnInit().
hschamf9546312020-04-14 15:12:40 +0900172 base::ThreadTaskRunnerHandle::Get()->PostTask(
Kevin Cernekee95d4ae92016-06-19 10:26:29 -0700173 FROM_HERE,
174 base::Bind(&Manager::InitialSetup, weak_factory_.GetWeakPtr()));
175
176 return DBusDaemon::OnInit();
177}
178
179void Manager::InitialSetup() {
Garrick Evans08843932019-09-17 14:41:08 +0900180 LOG(INFO) << "Setting up DBus service interface";
181 dbus_svc_path_ = bus_->GetExportedObject(
182 dbus::ObjectPath(patchpanel::kPatchPanelServicePath));
183 if (!dbus_svc_path_) {
184 LOG(FATAL) << "Failed to export " << patchpanel::kPatchPanelServicePath
185 << " object";
186 }
187
Hugo Benichi76be34a2020-08-26 22:35:54 +0900188 shill_client_ = std::make_unique<ShillClient>(bus_);
189
Garrick Evans08843932019-09-17 14:41:08 +0900190 using ServiceMethod =
191 std::unique_ptr<dbus::Response> (Manager::*)(dbus::MethodCall*);
192 const std::map<const char*, ServiceMethod> kServiceMethods = {
193 {patchpanel::kArcStartupMethod, &Manager::OnArcStartup},
194 {patchpanel::kArcShutdownMethod, &Manager::OnArcShutdown},
195 {patchpanel::kArcVmStartupMethod, &Manager::OnArcVmStartup},
196 {patchpanel::kArcVmShutdownMethod, &Manager::OnArcVmShutdown},
Garrick Evans47c19272019-11-21 10:58:21 +0900197 {patchpanel::kTerminaVmStartupMethod, &Manager::OnTerminaVmStartup},
198 {patchpanel::kTerminaVmShutdownMethod, &Manager::OnTerminaVmShutdown},
Garrick Evans51d5b552020-01-30 10:42:06 +0900199 {patchpanel::kPluginVmStartupMethod, &Manager::OnPluginVmStartup},
200 {patchpanel::kPluginVmShutdownMethod, &Manager::OnPluginVmShutdown},
Hugo Benichi7d9d8db2020-03-30 15:56:56 +0900201 {patchpanel::kSetVpnIntentMethod, &Manager::OnSetVpnIntent},
Hugo Benichib56b77c2020-01-15 16:00:56 +0900202 {patchpanel::kConnectNamespaceMethod, &Manager::OnConnectNamespace},
Jie Jiang493cde42020-07-17 21:43:39 +0900203 {patchpanel::kGetTrafficCountersMethod, &Manager::OnGetTrafficCounters},
Jason Jeremy Imanb2a421e2020-07-04 20:48:32 +0900204 {patchpanel::kModifyPortRuleMethod, &Manager::OnModifyPortRule},
Garrick Evans08843932019-09-17 14:41:08 +0900205 };
206
207 for (const auto& kv : kServiceMethods) {
208 if (!dbus_svc_path_->ExportMethodAndBlock(
209 patchpanel::kPatchPanelInterface, kv.first,
210 base::Bind(&HandleSynchronousDBusMethodCall,
211 base::Bind(kv.second, base::Unretained(this))))) {
212 LOG(FATAL) << "Failed to export method " << kv.first;
213 }
214 }
215
216 if (!bus_->RequestOwnershipAndBlock(patchpanel::kPatchPanelServiceName,
217 dbus::Bus::REQUIRE_PRIMARY)) {
218 LOG(FATAL) << "Failed to take ownership of "
219 << patchpanel::kPatchPanelServiceName;
220 }
221 LOG(INFO) << "DBus service interface ready";
222
Hugo Benichifda77232020-08-21 18:28:15 +0900223 routing_svc_ = std::make_unique<RoutingService>();
224
225 StartDatapath();
226
Taoyu Lia0727dc2020-09-24 19:54:59 +0900227 nd_proxy_->RegisterNDProxyMessageHandler(
228 base::Bind(&Manager::OnNDProxyMessage, weak_factory_.GetWeakPtr()));
Hugo Benichifda77232020-08-21 18:28:15 +0900229
Hugo Benichifda77232020-08-21 18:28:15 +0900230 auto* const forwarder = static_cast<TrafficForwarder*>(this);
231
232 GuestMessage::GuestType arc_guest =
233 USE_ARCVM ? GuestMessage::ARC_VM : GuestMessage::ARC;
234 arc_svc_ = std::make_unique<ArcService>(shill_client_.get(), datapath_.get(),
235 &addr_mgr_, forwarder, arc_guest);
236 cros_svc_ = std::make_unique<CrostiniService>(shill_client_.get(), &addr_mgr_,
237 datapath_.get(), forwarder);
Jie Jiang84966852020-09-18 18:49:05 +0900238 network_monitor_svc_ = std::make_unique<NetworkMonitorService>(
239 shill_client_.get(),
240 base::BindRepeating(&Manager::OnNeighborConnectedStateChanged,
241 weak_factory_.GetWeakPtr()));
Hugo Benichifda77232020-08-21 18:28:15 +0900242 network_monitor_svc_->Start();
243
244 counters_svc_ =
245 std::make_unique<CountersService>(shill_client_.get(), runner_.get());
246
247 nd_proxy_->Listen();
248}
249
250void Manager::OnShutdown(int* exit_code) {
251 LOG(INFO) << "Shutting down and cleaning up";
Jie Jiang84966852020-09-18 18:49:05 +0900252 network_monitor_svc_.reset();
Hugo Benichifda77232020-08-21 18:28:15 +0900253 cros_svc_.reset();
254 arc_svc_.reset();
255 close(connected_namespaces_epollfd_);
256 // Tear down any remaining connected namespace.
257 std::vector<int> connected_namespaces_fdkeys;
258 for (const auto& kv : connected_namespaces_)
259 connected_namespaces_fdkeys.push_back(kv.first);
260 for (const int fdkey : connected_namespaces_fdkeys)
261 DisconnectNamespace(fdkey);
262 StopDatapath();
263}
264
265void Manager::OnSubprocessExited(pid_t pid, const siginfo_t&) {
266 LOG(ERROR) << "Subprocess " << pid << " exited unexpectedly -"
267 << " attempting to restart";
268
269 HelperProcess* proc;
270 if (pid == adb_proxy_->pid()) {
271 proc = adb_proxy_.get();
272 } else if (pid == mcast_proxy_->pid()) {
273 proc = mcast_proxy_.get();
274 } else if (pid == nd_proxy_->pid()) {
275 proc = nd_proxy_.get();
276 } else {
277 LOG(DFATAL) << "Unknown child process";
278 return;
279 }
280
281 process_reaper_.ForgetChild(pid);
282
283 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
284 FROM_HERE,
285 base::Bind(&Manager::RestartSubprocess, weak_factory_.GetWeakPtr(), proc),
286 base::TimeDelta::FromMilliseconds((2 << proc->restarts()) *
287 kSubprocessRestartDelayMs));
288}
289
290void Manager::RestartSubprocess(HelperProcess* subproc) {
291 if (subproc->Restart()) {
292 DCHECK(process_reaper_.WatchForChild(
293 FROM_HERE, subproc->pid(),
294 base::Bind(&Manager::OnSubprocessExited, weak_factory_.GetWeakPtr(),
295 subproc->pid())))
296 << "Failed to watch child process " << subproc->pid();
297 }
298}
299
300void Manager::StartDatapath() {
301 // b/162966185: Allow Jetstream to disable:
302 // - the IP forwarding setup used for hosting VMs and containers,
303 // - the iptables rules for fwmark based routing.
304 if (USE_JETSTREAM_ROUTING) {
305 LOG(INFO) << "Skipping Datapath routing setup";
306 return;
307 }
308
Taoyu Li6d479442019-12-09 13:02:29 +0900309 auto& runner = datapath_->runner();
Hugo Benichifda77232020-08-21 18:28:15 +0900310
Garrick Evans7cf8c542020-05-25 09:50:17 +0900311 // Enable IPv4 packet forwarding
312 if (runner.sysctl_w("net.ipv4.ip_forward", "1") != 0) {
313 LOG(ERROR) << "Failed to update net.ipv4.ip_forward."
314 << " Guest connectivity will not work correctly.";
315 }
Garrick Evans28d194e2019-12-17 10:22:28 +0900316 // Limit local port range: Android owns 47104-61000.
317 // TODO(garrick): The original history behind this tweak is gone. Some
318 // investigation is needed to see if it is still applicable.
Garrick Evans8e8e3472020-01-23 14:03:50 +0900319 if (runner.sysctl_w("net.ipv4.ip_local_port_range", "32768 47103") != 0) {
Garrick Evans28d194e2019-12-17 10:22:28 +0900320 LOG(ERROR) << "Failed to limit local port range. Some Android features or"
321 << " apps may not work correctly.";
322 }
Hugo Benichi76be34a2020-08-26 22:35:54 +0900323 // Enable IPv6 packet forwarding
Garrick Evans8e8e3472020-01-23 14:03:50 +0900324 if (runner.sysctl_w("net.ipv6.conf.all.forwarding", "1") != 0) {
Taoyu Li6d479442019-12-09 13:02:29 +0900325 LOG(ERROR) << "Failed to update net.ipv6.conf.all.forwarding."
326 << " IPv6 functionality may be broken.";
327 }
Taoyu Li6d479442019-12-09 13:02:29 +0900328
Garrick Evansd291af62020-05-25 10:39:06 +0900329 if (!datapath_->AddSNATMarkRules()) {
330 LOG(ERROR) << "Failed to install SNAT mark rules."
331 << " Guest connectivity may be broken.";
332 }
333 if (!datapath_->AddForwardEstablishedRule()) {
334 LOG(ERROR) << "Failed to install forwarding rule for established"
335 << " connections.";
336 }
337
Hugo Benichi321f23b2020-09-25 15:42:05 +0900338 // chromium:898210: Drop any locally originated traffic that would exit a
339 // physical interface with a source IPv4 address from the subnet of IPs used
340 // for VMs, containers, and connected namespaces This is needed to prevent
341 // packets leaking with an incorrect src IP when a local process binds to the
342 // wrong interface.
343 for (const auto& oif : kPhysicalIfnamePrefixes) {
344 if (!datapath_->AddSourceIPv4DropRule(oif, kGuestIPv4Subnet))
345 LOG(WARNING) << "Failed to set up IPv4 drop rule for src ip "
346 << kGuestIPv4Subnet << " exiting " << oif;
Garrick Evansff6e37f2020-05-25 10:54:47 +0900347 }
348
Garrick Evansc50426b2020-05-25 11:00:55 +0900349 if (!datapath_->AddOutboundIPv4SNATMark("vmtap+")) {
350 LOG(ERROR) << "Failed to set up NAT for TAP devices."
351 << " Guest connectivity may be broken.";
352 }
Hugo Benichi76be34a2020-08-26 22:35:54 +0900353
354 shill_client_->RegisterDevicesChangedHandler(base::BindRepeating(
355 &Manager::OnDevicesChanged, weak_factory_.GetWeakPtr()));
Long Chengd4415582019-09-24 19:16:09 +0000356}
Garrick Evans49879532018-12-03 13:15:36 +0900357
Hugo Benichifda77232020-08-21 18:28:15 +0900358void Manager::StopDatapath() {
359 if (USE_JETSTREAM_ROUTING)
360 return;
Garrick Evans28d194e2019-12-17 10:22:28 +0900361
Garrick Evansc50426b2020-05-25 11:00:55 +0900362 datapath_->RemoveOutboundIPv4SNATMark("vmtap+");
Garrick Evansd291af62020-05-25 10:39:06 +0900363 datapath_->RemoveForwardEstablishedRule();
364 datapath_->RemoveSNATMarkRules();
Hugo Benichi321f23b2020-09-25 15:42:05 +0900365 for (const auto& oif : kPhysicalIfnamePrefixes)
366 datapath_->RemoveSourceIPv4DropRule(oif, kGuestIPv4Subnet);
Garrick Evansd291af62020-05-25 10:39:06 +0900367
Garrick Evans7cf8c542020-05-25 09:50:17 +0900368 auto& runner = datapath_->runner();
Garrick Evans28d194e2019-12-17 10:22:28 +0900369 // Restore original local port range.
370 // TODO(garrick): The original history behind this tweak is gone. Some
371 // investigation is needed to see if it is still applicable.
Garrick Evans7cf8c542020-05-25 09:50:17 +0900372 if (runner.sysctl_w("net.ipv4.ip_local_port_range", "32768 61000") != 0) {
Garrick Evans28d194e2019-12-17 10:22:28 +0900373 LOG(ERROR) << "Failed to restore local port range";
374 }
Garrick Evans7cf8c542020-05-25 09:50:17 +0900375 // Disable packet forwarding
376 if (runner.sysctl_w("net.ipv6.conf.all.forwarding", "0") != 0) {
377 LOG(ERROR) << "Failed to restore net.ipv6.conf.all.forwarding.";
378 }
379 if (runner.sysctl_w("net.ipv4.ip_forward", "0") != 0) {
380 LOG(ERROR) << "Failed to restore net.ipv4.ip_forward.";
381 }
Kevin Cernekee27bcaa62016-12-03 11:16:26 -0800382}
383
Hugo Benichi76be34a2020-08-26 22:35:54 +0900384void Manager::OnDevicesChanged(const std::set<std::string>& added,
385 const std::set<std::string>& removed) {
386 for (const std::string& ifname : removed)
387 datapath_->StopConnectionPinning(ifname);
388
389 for (const std::string& ifname : added)
390 datapath_->StartConnectionPinning(ifname);
391}
392
Garrick Evanse94a14e2019-11-11 10:32:13 +0900393bool Manager::StartArc(pid_t pid) {
Garrick Evans508a4bc2019-11-14 08:45:52 +0900394 if (!arc_svc_->Start(pid))
395 return false;
Garrick Evanse94a14e2019-11-11 10:32:13 +0900396
397 GuestMessage msg;
398 msg.set_event(GuestMessage::START);
399 msg.set_type(GuestMessage::ARC);
400 msg.set_arc_pid(pid);
401 SendGuestMessage(msg);
402
403 return true;
404}
405
Hugo Benichi4d4bb8f2020-07-07 12:16:07 +0900406void Manager::StopArc() {
Garrick Evanse94a14e2019-11-11 10:32:13 +0900407 GuestMessage msg;
408 msg.set_event(GuestMessage::STOP);
409 msg.set_type(GuestMessage::ARC);
410 SendGuestMessage(msg);
411
Hugo Benichi4d4bb8f2020-07-07 12:16:07 +0900412 // After the ARC container has stopped, the pid is not known anymore.
413 // The pid argument is ignored by ArcService.
414 arc_svc_->Stop(0);
Garrick Evanse94a14e2019-11-11 10:32:13 +0900415}
416
Garrick Evans015b0d62020-02-07 09:06:38 +0900417bool Manager::StartArcVm(uint32_t cid) {
Garrick Evans508a4bc2019-11-14 08:45:52 +0900418 if (!arc_svc_->Start(cid))
419 return false;
Garrick Evanse94a14e2019-11-11 10:32:13 +0900420
421 GuestMessage msg;
422 msg.set_event(GuestMessage::START);
423 msg.set_type(GuestMessage::ARC_VM);
424 msg.set_arcvm_vsock_cid(cid);
425 SendGuestMessage(msg);
426
427 return true;
428}
429
Garrick Evans015b0d62020-02-07 09:06:38 +0900430void Manager::StopArcVm(uint32_t cid) {
Garrick Evanse94a14e2019-11-11 10:32:13 +0900431 GuestMessage msg;
432 msg.set_event(GuestMessage::STOP);
433 msg.set_type(GuestMessage::ARC_VM);
434 SendGuestMessage(msg);
435
Garrick Evans21173b12019-11-20 15:23:16 +0900436 arc_svc_->Stop(cid);
Garrick Evanse94a14e2019-11-11 10:32:13 +0900437}
438
Garrick Evans51d5b552020-01-30 10:42:06 +0900439bool Manager::StartCrosVm(uint64_t vm_id,
440 GuestMessage::GuestType vm_type,
Garrick Evans53a2a982020-02-05 10:53:35 +0900441 uint32_t subnet_index) {
Garrick Evans51d5b552020-01-30 10:42:06 +0900442 DCHECK(vm_type == GuestMessage::TERMINA_VM ||
443 vm_type == GuestMessage::PLUGIN_VM);
444
445 if (!cros_svc_->Start(vm_id, vm_type == GuestMessage::TERMINA_VM,
446 subnet_index))
Garrick Evans47c19272019-11-21 10:58:21 +0900447 return false;
448
449 GuestMessage msg;
450 msg.set_event(GuestMessage::START);
Garrick Evans51d5b552020-01-30 10:42:06 +0900451 msg.set_type(vm_type);
Garrick Evans47c19272019-11-21 10:58:21 +0900452 SendGuestMessage(msg);
453
454 return true;
455}
456
Garrick Evans51d5b552020-01-30 10:42:06 +0900457void Manager::StopCrosVm(uint64_t vm_id, GuestMessage::GuestType vm_type) {
Garrick Evans47c19272019-11-21 10:58:21 +0900458 GuestMessage msg;
459 msg.set_event(GuestMessage::STOP);
Garrick Evans51d5b552020-01-30 10:42:06 +0900460 msg.set_type(vm_type);
Garrick Evans47c19272019-11-21 10:58:21 +0900461 SendGuestMessage(msg);
462
Garrick Evans51d5b552020-01-30 10:42:06 +0900463 cros_svc_->Stop(vm_id, vm_type == GuestMessage::TERMINA_VM);
Garrick Evans47c19272019-11-21 10:58:21 +0900464}
465
Garrick Evans08843932019-09-17 14:41:08 +0900466std::unique_ptr<dbus::Response> Manager::OnArcStartup(
467 dbus::MethodCall* method_call) {
468 LOG(INFO) << "ARC++ starting up";
469
470 std::unique_ptr<dbus::Response> dbus_response(
471 dbus::Response::FromMethodCall(method_call));
472
473 dbus::MessageReader reader(method_call);
474 dbus::MessageWriter writer(dbus_response.get());
475
476 patchpanel::ArcStartupRequest request;
477 patchpanel::ArcStartupResponse response;
478
479 if (!reader.PopArrayOfBytesAsProto(&request)) {
480 LOG(ERROR) << "Unable to parse request";
481 writer.AppendProtoAsArrayOfBytes(response);
482 return dbus_response;
483 }
484
Garrick Evanse01bf072019-11-15 09:08:19 +0900485 if (!StartArc(request.pid()))
486 LOG(ERROR) << "Failed to start ARC++ network service";
Garrick Evanse94a14e2019-11-11 10:32:13 +0900487
Garrick Evans08843932019-09-17 14:41:08 +0900488 writer.AppendProtoAsArrayOfBytes(response);
489 return dbus_response;
490}
491
492std::unique_ptr<dbus::Response> Manager::OnArcShutdown(
493 dbus::MethodCall* method_call) {
494 LOG(INFO) << "ARC++ shutting down";
495
496 std::unique_ptr<dbus::Response> dbus_response(
497 dbus::Response::FromMethodCall(method_call));
498
499 dbus::MessageReader reader(method_call);
500 dbus::MessageWriter writer(dbus_response.get());
501
502 patchpanel::ArcShutdownRequest request;
503 patchpanel::ArcShutdownResponse response;
504
505 if (!reader.PopArrayOfBytesAsProto(&request)) {
506 LOG(ERROR) << "Unable to parse request";
507 writer.AppendProtoAsArrayOfBytes(response);
508 return dbus_response;
509 }
510
Hugo Benichi4d4bb8f2020-07-07 12:16:07 +0900511 StopArc();
Garrick Evanse94a14e2019-11-11 10:32:13 +0900512
Garrick Evans08843932019-09-17 14:41:08 +0900513 writer.AppendProtoAsArrayOfBytes(response);
514 return dbus_response;
515}
516
517std::unique_ptr<dbus::Response> Manager::OnArcVmStartup(
518 dbus::MethodCall* method_call) {
519 LOG(INFO) << "ARCVM starting up";
520
521 std::unique_ptr<dbus::Response> dbus_response(
522 dbus::Response::FromMethodCall(method_call));
523
524 dbus::MessageReader reader(method_call);
525 dbus::MessageWriter writer(dbus_response.get());
526
527 patchpanel::ArcVmStartupRequest request;
528 patchpanel::ArcVmStartupResponse response;
529
530 if (!reader.PopArrayOfBytesAsProto(&request)) {
531 LOG(ERROR) << "Unable to parse request";
532 writer.AppendProtoAsArrayOfBytes(response);
533 return dbus_response;
534 }
535
Garrick Evans47c19272019-11-21 10:58:21 +0900536 if (!StartArcVm(request.cid())) {
Garrick Evanse01bf072019-11-15 09:08:19 +0900537 LOG(ERROR) << "Failed to start ARCVM network service";
Garrick Evans47c19272019-11-21 10:58:21 +0900538 writer.AppendProtoAsArrayOfBytes(response);
539 return dbus_response;
Garrick Evanse01bf072019-11-15 09:08:19 +0900540 }
Garrick Evanse94a14e2019-11-11 10:32:13 +0900541
Garrick Evans47c19272019-11-21 10:58:21 +0900542 // Populate the response with the known devices.
Garrick Evans38b25a42020-04-06 15:17:42 +0900543 for (const auto* config : arc_svc_->GetDeviceConfigs()) {
544 if (config->tap_ifname().empty())
545 continue;
Garrick Evans47c19272019-11-21 10:58:21 +0900546
Garrick Evans38b25a42020-04-06 15:17:42 +0900547 auto* dev = response.add_devices();
548 dev->set_ifname(config->tap_ifname());
549 dev->set_ipv4_addr(config->guest_ipv4_addr());
550 }
Garrick Evanse94b6de2020-02-20 09:19:13 +0900551
Garrick Evans08843932019-09-17 14:41:08 +0900552 writer.AppendProtoAsArrayOfBytes(response);
553 return dbus_response;
554}
555
556std::unique_ptr<dbus::Response> Manager::OnArcVmShutdown(
557 dbus::MethodCall* method_call) {
558 LOG(INFO) << "ARCVM shutting down";
559
560 std::unique_ptr<dbus::Response> dbus_response(
561 dbus::Response::FromMethodCall(method_call));
562
563 dbus::MessageReader reader(method_call);
564 dbus::MessageWriter writer(dbus_response.get());
565
566 patchpanel::ArcVmShutdownRequest request;
567 patchpanel::ArcVmShutdownResponse response;
568
569 if (!reader.PopArrayOfBytesAsProto(&request)) {
570 LOG(ERROR) << "Unable to parse request";
571 writer.AppendProtoAsArrayOfBytes(response);
572 return dbus_response;
573 }
574
Garrick Evans21173b12019-11-20 15:23:16 +0900575 StopArcVm(request.cid());
Garrick Evanse94a14e2019-11-11 10:32:13 +0900576
Garrick Evans08843932019-09-17 14:41:08 +0900577 writer.AppendProtoAsArrayOfBytes(response);
578 return dbus_response;
579}
580
Garrick Evans47c19272019-11-21 10:58:21 +0900581std::unique_ptr<dbus::Response> Manager::OnTerminaVmStartup(
582 dbus::MethodCall* method_call) {
583 LOG(INFO) << "Termina VM starting up";
584
585 std::unique_ptr<dbus::Response> dbus_response(
586 dbus::Response::FromMethodCall(method_call));
587
588 dbus::MessageReader reader(method_call);
589 dbus::MessageWriter writer(dbus_response.get());
590
591 patchpanel::TerminaVmStartupRequest request;
592 patchpanel::TerminaVmStartupResponse response;
593
594 if (!reader.PopArrayOfBytesAsProto(&request)) {
595 LOG(ERROR) << "Unable to parse request";
596 writer.AppendProtoAsArrayOfBytes(response);
597 return dbus_response;
598 }
599
600 const int32_t cid = request.cid();
Garrick Evans53a2a982020-02-05 10:53:35 +0900601 if (!StartCrosVm(cid, GuestMessage::TERMINA_VM)) {
Garrick Evans47c19272019-11-21 10:58:21 +0900602 LOG(ERROR) << "Failed to start Termina VM network service";
603 writer.AppendProtoAsArrayOfBytes(response);
604 return dbus_response;
605 }
606
Garrick Evans51d5b552020-01-30 10:42:06 +0900607 const auto* const tap = cros_svc_->TAP(cid, true /*is_termina*/);
Garrick Evansb1c93712020-01-22 09:28:25 +0900608 if (!tap) {
609 LOG(DFATAL) << "TAP device missing";
610 writer.AppendProtoAsArrayOfBytes(response);
611 return dbus_response;
612 }
Garrick Evans47c19272019-11-21 10:58:21 +0900613
Garrick Evansb1c93712020-01-22 09:28:25 +0900614 auto* dev = response.mutable_device();
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900615 dev->set_ifname(tap->host_ifname());
616 const auto* subnet = tap->config().ipv4_subnet();
Garrick Evansb1c93712020-01-22 09:28:25 +0900617 if (!subnet) {
618 LOG(DFATAL) << "Missing required subnet for {cid: " << cid << "}";
619 writer.AppendProtoAsArrayOfBytes(response);
620 return dbus_response;
621 }
622 auto* resp_subnet = dev->mutable_ipv4_subnet();
623 resp_subnet->set_base_addr(subnet->BaseAddress());
624 resp_subnet->set_prefix_len(subnet->PrefixLength());
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900625 subnet = tap->config().lxd_ipv4_subnet();
Garrick Evansb1c93712020-01-22 09:28:25 +0900626 if (!subnet) {
627 LOG(DFATAL) << "Missing required lxd subnet for {cid: " << cid << "}";
628 writer.AppendProtoAsArrayOfBytes(response);
629 return dbus_response;
630 }
631 resp_subnet = response.mutable_container_subnet();
632 resp_subnet->set_base_addr(subnet->BaseAddress());
633 resp_subnet->set_prefix_len(subnet->PrefixLength());
Garrick Evans47c19272019-11-21 10:58:21 +0900634
635 writer.AppendProtoAsArrayOfBytes(response);
636 return dbus_response;
637}
638
639std::unique_ptr<dbus::Response> Manager::OnTerminaVmShutdown(
640 dbus::MethodCall* method_call) {
641 LOG(INFO) << "Termina VM shutting down";
642
643 std::unique_ptr<dbus::Response> dbus_response(
644 dbus::Response::FromMethodCall(method_call));
645
646 dbus::MessageReader reader(method_call);
647 dbus::MessageWriter writer(dbus_response.get());
648
649 patchpanel::TerminaVmShutdownRequest request;
650 patchpanel::TerminaVmShutdownResponse response;
651
652 if (!reader.PopArrayOfBytesAsProto(&request)) {
653 LOG(ERROR) << "Unable to parse request";
654 writer.AppendProtoAsArrayOfBytes(response);
655 return dbus_response;
656 }
657
Garrick Evans51d5b552020-01-30 10:42:06 +0900658 StopCrosVm(request.cid(), GuestMessage::TERMINA_VM);
659
660 writer.AppendProtoAsArrayOfBytes(response);
661 return dbus_response;
662}
663
664std::unique_ptr<dbus::Response> Manager::OnPluginVmStartup(
665 dbus::MethodCall* method_call) {
666 LOG(INFO) << "Plugin VM starting up";
667
668 std::unique_ptr<dbus::Response> dbus_response(
669 dbus::Response::FromMethodCall(method_call));
670
671 dbus::MessageReader reader(method_call);
672 dbus::MessageWriter writer(dbus_response.get());
673
674 patchpanel::PluginVmStartupRequest request;
675 patchpanel::PluginVmStartupResponse response;
676
677 if (!reader.PopArrayOfBytesAsProto(&request)) {
678 LOG(ERROR) << "Unable to parse request";
679 writer.AppendProtoAsArrayOfBytes(response);
680 return dbus_response;
681 }
682
Garrick Evans08fb34b2020-02-20 10:50:17 +0900683 const uint64_t vm_id = request.id();
Garrick Evans53a2a982020-02-05 10:53:35 +0900684 if (!StartCrosVm(vm_id, GuestMessage::PLUGIN_VM, request.subnet_index())) {
Garrick Evans51d5b552020-01-30 10:42:06 +0900685 LOG(ERROR) << "Failed to start Plugin VM network service";
686 writer.AppendProtoAsArrayOfBytes(response);
687 return dbus_response;
688 }
689
690 const auto* const tap = cros_svc_->TAP(vm_id, false /*is_termina*/);
691 if (!tap) {
692 LOG(DFATAL) << "TAP device missing";
693 writer.AppendProtoAsArrayOfBytes(response);
694 return dbus_response;
695 }
696
Garrick Evans51d5b552020-01-30 10:42:06 +0900697 auto* dev = response.mutable_device();
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900698 dev->set_ifname(tap->host_ifname());
699 const auto* subnet = tap->config().ipv4_subnet();
Garrick Evans51d5b552020-01-30 10:42:06 +0900700 if (!subnet) {
701 LOG(DFATAL) << "Missing required subnet for {cid: " << vm_id << "}";
702 writer.AppendProtoAsArrayOfBytes(response);
703 return dbus_response;
704 }
705 auto* resp_subnet = dev->mutable_ipv4_subnet();
706 resp_subnet->set_base_addr(subnet->BaseAddress());
707 resp_subnet->set_prefix_len(subnet->PrefixLength());
708
709 writer.AppendProtoAsArrayOfBytes(response);
710 return dbus_response;
711}
712
713std::unique_ptr<dbus::Response> Manager::OnPluginVmShutdown(
714 dbus::MethodCall* method_call) {
715 LOG(INFO) << "Plugin VM shutting down";
716
717 std::unique_ptr<dbus::Response> dbus_response(
718 dbus::Response::FromMethodCall(method_call));
719
720 dbus::MessageReader reader(method_call);
721 dbus::MessageWriter writer(dbus_response.get());
722
723 patchpanel::PluginVmShutdownRequest request;
724 patchpanel::PluginVmShutdownResponse response;
725
726 if (!reader.PopArrayOfBytesAsProto(&request)) {
727 LOG(ERROR) << "Unable to parse request";
728 writer.AppendProtoAsArrayOfBytes(response);
729 return dbus_response;
730 }
731
732 StopCrosVm(request.id(), GuestMessage::PLUGIN_VM);
Garrick Evans47c19272019-11-21 10:58:21 +0900733
734 writer.AppendProtoAsArrayOfBytes(response);
735 return dbus_response;
736}
737
Hugo Benichi7d9d8db2020-03-30 15:56:56 +0900738std::unique_ptr<dbus::Response> Manager::OnSetVpnIntent(
739 dbus::MethodCall* method_call) {
740 std::unique_ptr<dbus::Response> dbus_response(
741 dbus::Response::FromMethodCall(method_call));
742
743 dbus::MessageReader reader(method_call);
744 dbus::MessageWriter writer(dbus_response.get());
745
746 patchpanel::SetVpnIntentRequest request;
747 patchpanel::SetVpnIntentResponse response;
748
749 bool success = reader.PopArrayOfBytesAsProto(&request);
750 if (!success) {
751 LOG(ERROR) << "Unable to parse SetVpnIntentRequest";
752 // Do not return yet to make sure we close the received fd.
753 }
754
755 base::ScopedFD client_socket;
756 reader.PopFileDescriptor(&client_socket);
757
758 if (success)
759 success = routing_svc_->SetVpnFwmark(client_socket.get(), request.policy());
760
761 response.set_success(success);
Hugo Benichib56b77c2020-01-15 16:00:56 +0900762
763 writer.AppendProtoAsArrayOfBytes(response);
764 return dbus_response;
765}
766
767std::unique_ptr<dbus::Response> Manager::OnConnectNamespace(
768 dbus::MethodCall* method_call) {
769 std::unique_ptr<dbus::Response> dbus_response(
770 dbus::Response::FromMethodCall(method_call));
771
772 dbus::MessageReader reader(method_call);
773 dbus::MessageWriter writer(dbus_response.get());
774
775 patchpanel::ConnectNamespaceRequest request;
776 patchpanel::ConnectNamespaceResponse response;
777
Hugo Benichicc6850f2020-01-17 13:26:06 +0900778 bool success = true;
Hugo Benichib56b77c2020-01-15 16:00:56 +0900779 if (!reader.PopArrayOfBytesAsProto(&request)) {
Hugo Benichicc6850f2020-01-17 13:26:06 +0900780 LOG(ERROR) << "Unable to parse ConnectNamespaceRequest";
781 // Do not return yet to make sure we close the received fd and
782 // validate other arguments.
783 success = false;
Hugo Benichib56b77c2020-01-15 16:00:56 +0900784 }
785
Hugo Benichicc6850f2020-01-17 13:26:06 +0900786 base::ScopedFD client_fd;
787 reader.PopFileDescriptor(&client_fd);
788 if (!client_fd.is_valid()) {
789 LOG(ERROR) << "ConnectNamespaceRequest: invalid file descriptor";
790 success = false;
791 }
792
793 pid_t pid = request.pid();
794 {
795 ScopedNS ns(pid);
796 if (!ns.IsValid()) {
797 LOG(ERROR) << "ConnectNamespaceRequest: invalid namespace pid " << pid;
798 success = false;
799 }
800 }
801
802 const std::string& outbound_ifname = request.outbound_physical_device();
803 if (!outbound_ifname.empty() && !shill_client_->has_device(outbound_ifname)) {
804 LOG(ERROR) << "ConnectNamespaceRequest: invalid outbound ifname "
805 << outbound_ifname;
806 success = false;
807 }
808
Hugo Benichiadf1ec52020-01-17 16:23:58 +0900809 if (success)
810 ConnectNamespace(std::move(client_fd), request, response);
Hugo Benichib56b77c2020-01-15 16:00:56 +0900811
Hugo Benichi7d9d8db2020-03-30 15:56:56 +0900812 writer.AppendProtoAsArrayOfBytes(response);
813 return dbus_response;
814}
815
Jie Jiang493cde42020-07-17 21:43:39 +0900816std::unique_ptr<dbus::Response> Manager::OnGetTrafficCounters(
817 dbus::MethodCall* method_call) {
818 std::unique_ptr<dbus::Response> dbus_response(
819 dbus::Response::FromMethodCall(method_call));
820
821 dbus::MessageReader reader(method_call);
822 dbus::MessageWriter writer(dbus_response.get());
823
824 patchpanel::TrafficCountersRequest request;
825 patchpanel::TrafficCountersResponse response;
826
827 if (!reader.PopArrayOfBytesAsProto(&request)) {
828 LOG(ERROR) << "Unable to parse TrafficCountersRequest";
829 writer.AppendProtoAsArrayOfBytes(response);
830 return dbus_response;
831 }
832
833 const std::set<std::string> devices{request.devices().begin(),
834 request.devices().end()};
835 const auto counters = counters_svc_->GetCounters(devices);
836 for (const auto& kv : counters) {
837 auto* traffic_counter = response.add_counters();
838 const auto& source_and_device = kv.first;
839 const auto& counter = kv.second;
840 traffic_counter->set_source(source_and_device.first);
841 traffic_counter->set_device(source_and_device.second);
842 traffic_counter->set_rx_bytes(counter.rx_bytes);
843 traffic_counter->set_rx_packets(counter.rx_packets);
844 traffic_counter->set_tx_bytes(counter.tx_bytes);
845 traffic_counter->set_tx_packets(counter.tx_packets);
846 }
847
848 writer.AppendProtoAsArrayOfBytes(response);
849 return dbus_response;
850}
851
Jason Jeremy Imanb2a421e2020-07-04 20:48:32 +0900852std::unique_ptr<dbus::Response> Manager::OnModifyPortRule(
853 dbus::MethodCall* method_call) {
854 std::unique_ptr<dbus::Response> dbus_response(
855 dbus::Response::FromMethodCall(method_call));
856
857 dbus::MessageReader reader(method_call);
858 dbus::MessageWriter writer(dbus_response.get());
859
860 patchpanel::ModifyPortRuleRequest request;
861 patchpanel::ModifyPortRuleResponse response;
862
863 if (!reader.PopArrayOfBytesAsProto(&request)) {
864 LOG(ERROR) << "Unable to parse ModifyPortRequest";
865 writer.AppendProtoAsArrayOfBytes(response);
866 return dbus_response;
867 }
868
Jason Jeremy Imanc28df852020-07-11 17:38:26 +0900869 response.set_success(ModifyPortRule(request));
Jason Jeremy Imanb2a421e2020-07-04 20:48:32 +0900870 writer.AppendProtoAsArrayOfBytes(response);
871 return dbus_response;
872}
873
Jie Jiang84966852020-09-18 18:49:05 +0900874void Manager::OnNeighborConnectedStateChanged(
875 int ifindex,
876 const shill::IPAddress& ip_addr,
877 NeighborLinkMonitor::NeighborRole role,
878 bool connected) {
879 if (!ip_addr.IsValid()) {
880 LOG(DFATAL) << "ip_addr is not valid";
881 return;
882 }
883
884 using SignalProto = NeighborConnectedStateChangedSignal;
885 SignalProto proto;
886 proto.set_ifindex(ifindex);
887 proto.set_ip_addr(ip_addr.ToString());
888 switch (role) {
889 case NeighborLinkMonitor::NeighborRole::kGateway:
890 proto.set_role(SignalProto::GATEWAY);
891 break;
892 case NeighborLinkMonitor::NeighborRole::kDNSServer:
893 proto.set_role(SignalProto::DNS_SERVER);
894 break;
895 case NeighborLinkMonitor::NeighborRole::kGatewayAndDNSServer:
896 proto.set_role(SignalProto::GATEWAY_AND_DNS_SERVER);
897 break;
898 default:
899 NOTREACHED();
900 }
901
902 dbus::Signal signal(kPatchPanelInterface,
903 kNeighborConnectedStateChangedSignal);
904 dbus::MessageWriter writer(&signal);
905 if (!writer.AppendProtoAsArrayOfBytes(proto)) {
906 LOG(ERROR) << "Failed to encode proto NeighborConnectedStateChangedSignal";
907 return;
908 }
909
910 dbus_svc_path_->SendSignal(&signal);
911}
912
Hugo Benichiadf1ec52020-01-17 16:23:58 +0900913void Manager::ConnectNamespace(
914 base::ScopedFD client_fd,
915 const patchpanel::ConnectNamespaceRequest& request,
916 patchpanel::ConnectNamespaceResponse& response) {
917 std::unique_ptr<Subnet> subnet =
918 addr_mgr_.AllocateIPv4Subnet(AddressManager::Guest::MINIJAIL_NETNS);
919 if (!subnet) {
920 LOG(ERROR) << "ConnectNamespaceRequest: exhausted IPv4 subnet space";
921 return;
922 }
923
924 const std::string ifname_id = std::to_string(connected_namespaces_next_id_);
Hugo Benichi33860d72020-07-09 16:34:01 +0900925 const std::string netns_name = "connected_netns_" + ifname_id;
Hugo Benichiadf1ec52020-01-17 16:23:58 +0900926 const std::string host_ifname = "arc_ns" + ifname_id;
927 const std::string client_ifname = "veth" + ifname_id;
Hugo Benichie8758b52020-04-03 14:49:01 +0900928 const uint32_t host_ipv4_addr = subnet->AddressAtOffset(0);
929 const uint32_t client_ipv4_addr = subnet->AddressAtOffset(1);
Hugo Benichiadf1ec52020-01-17 16:23:58 +0900930
Hugo Benichie8758b52020-04-03 14:49:01 +0900931 // Veth interface configuration and client routing configuration:
Hugo Benichi33860d72020-07-09 16:34:01 +0900932 // - attach a name to the client namespace.
933 // - create veth pair across the current namespace and the client namespace.
Hugo Benichiadf1ec52020-01-17 16:23:58 +0900934 // - configure IPv4 address on remote veth inside client namespace.
Hugo Benichie8758b52020-04-03 14:49:01 +0900935 // - configure IPv4 address on local veth inside host namespace.
936 // - add a default IPv4 /0 route sending traffic to that remote veth.
Hugo Benichie8758b52020-04-03 14:49:01 +0900937 pid_t pid = request.pid();
Hugo Benichi33860d72020-07-09 16:34:01 +0900938 if (!datapath_->NetnsAttachName(netns_name, pid)) {
939 LOG(ERROR) << "ConnectNamespaceRequest: failed to attach name "
940 << netns_name << " to namespace pid " << pid;
941 return;
942 }
943 if (!datapath_->ConnectVethPair(pid, netns_name, host_ifname, client_ifname,
Hugo Benichi2fd0c6e2020-04-17 16:12:05 +0900944 addr_mgr_.GenerateMacAddress(),
945 client_ipv4_addr, subnet->PrefixLength(),
946 false /* enable_multicast */)) {
Hugo Benichie8758b52020-04-03 14:49:01 +0900947 LOG(ERROR) << "ConnectNamespaceRequest: failed to create veth pair for "
948 "namespace pid "
949 << pid;
Hugo Benichi33860d72020-07-09 16:34:01 +0900950 datapath_->NetnsDeleteName(netns_name);
Hugo Benichie8758b52020-04-03 14:49:01 +0900951 return;
952 }
953 if (!datapath_->ConfigureInterface(
954 host_ifname, addr_mgr_.GenerateMacAddress(), host_ipv4_addr,
955 subnet->PrefixLength(), true /* link up */,
956 false /* enable_multicast */)) {
957 LOG(ERROR) << "ConnectNamespaceRequest: cannot configure host interface "
958 << host_ifname;
959 datapath_->RemoveInterface(host_ifname);
Hugo Benichi33860d72020-07-09 16:34:01 +0900960 datapath_->NetnsDeleteName(netns_name);
Hugo Benichie8758b52020-04-03 14:49:01 +0900961 return;
962 }
Hugo Benichi33860d72020-07-09 16:34:01 +0900963 bool peer_route_setup_success;
Hugo Benichie8758b52020-04-03 14:49:01 +0900964 {
965 ScopedNS ns(pid);
Hugo Benichi33860d72020-07-09 16:34:01 +0900966 peer_route_setup_success =
967 ns.IsValid() &&
968 datapath_->AddIPv4Route(host_ipv4_addr, INADDR_ANY, INADDR_ANY);
969 }
970 if (!peer_route_setup_success) {
971 LOG(ERROR) << "ConnectNamespaceRequest: failed to add default /0 route to "
972 << host_ifname << " inside namespace pid " << pid;
973 datapath_->RemoveInterface(host_ifname);
974 datapath_->NetnsDeleteName(netns_name);
975 return;
Hugo Benichie8758b52020-04-03 14:49:01 +0900976 }
977
978 // Host namespace routing configuration
979 // - ingress: add route to client subnet via |host_ifname|.
980 // - egress: - allow forwarding for traffic outgoing |host_ifname|.
981 // - add SNAT mark 0x1/0x1 for traffic outgoing |host_ifname|.
982 // Note that by default unsolicited ingress traffic is not forwarded to the
983 // client namespace unless the client specifically set port forwarding
984 // through permission_broker DBus APIs.
985 // TODO(hugobenichi) If allow_user_traffic is false, then prevent forwarding
986 // both ways between client namespace and other guest containers and VMs.
987 // TODO(hugobenichi) If outbound_physical_device is defined, then set strong
988 // routing to that interface routing table.
989 if (!datapath_->AddIPv4Route(host_ipv4_addr, subnet->BaseAddress(),
990 subnet->Netmask())) {
991 LOG(ERROR)
992 << "ConnectNamespaceRequest: failed to set route to client namespace";
993 datapath_->RemoveInterface(host_ifname);
Hugo Benichi33860d72020-07-09 16:34:01 +0900994 datapath_->NetnsDeleteName(netns_name);
Hugo Benichie8758b52020-04-03 14:49:01 +0900995 return;
996 }
997 if (!datapath_->AddOutboundIPv4(host_ifname)) {
998 LOG(ERROR) << "ConnectNamespaceRequest: failed to allow FORWARD for "
999 "traffic outgoing from "
1000 << host_ifname;
1001 datapath_->RemoveInterface(host_ifname);
1002 datapath_->DeleteIPv4Route(host_ipv4_addr, subnet->BaseAddress(),
1003 subnet->Netmask());
Hugo Benichi33860d72020-07-09 16:34:01 +09001004 datapath_->NetnsDeleteName(netns_name);
Hugo Benichie8758b52020-04-03 14:49:01 +09001005 return;
1006 }
1007 if (!datapath_->AddOutboundIPv4SNATMark(host_ifname)) {
1008 LOG(ERROR) << "ConnectNamespaceRequest: failed to set SNAT for traffic "
1009 "outgoing from "
1010 << host_ifname;
1011 datapath_->RemoveInterface(host_ifname);
1012 datapath_->DeleteIPv4Route(host_ipv4_addr, subnet->BaseAddress(),
1013 subnet->Netmask());
1014 datapath_->RemoveOutboundIPv4(host_ifname);
Hugo Benichi33860d72020-07-09 16:34:01 +09001015 datapath_->NetnsDeleteName(netns_name);
Hugo Benichie8758b52020-04-03 14:49:01 +09001016 return;
1017 }
Hugo Benichiadf1ec52020-01-17 16:23:58 +09001018
Hugo Benichi7352ad92020-04-07 16:11:59 +09001019 // Dup the client fd into our own: this guarantees that the fd number will
1020 // be stable and tied to the actual kernel resources used by the client.
1021 base::ScopedFD local_client_fd(dup(client_fd.get()));
1022 if (!local_client_fd.is_valid()) {
1023 PLOG(ERROR) << "ConnectNamespaceRequest: failed to dup() client fd";
Hugo Benichie8758b52020-04-03 14:49:01 +09001024 datapath_->RemoveInterface(host_ifname);
1025 datapath_->DeleteIPv4Route(host_ipv4_addr, subnet->BaseAddress(),
1026 subnet->Netmask());
1027 datapath_->RemoveOutboundIPv4(host_ifname);
1028 datapath_->RemoveOutboundIPv4SNATMark(host_ifname);
Hugo Benichi33860d72020-07-09 16:34:01 +09001029 datapath_->NetnsDeleteName(netns_name);
Hugo Benichi7352ad92020-04-07 16:11:59 +09001030 return;
1031 }
1032
1033 // Add the dupe fd to the epoll watcher.
1034 // TODO(hugobenichi) Find a way to reuse base::FileDescriptorWatcher for
1035 // listening to EPOLLHUP.
1036 struct epoll_event epevent;
1037 epevent.events = EPOLLIN; // EPOLLERR | EPOLLHUP are always waited for.
1038 epevent.data.fd = local_client_fd.get();
1039 if (epoll_ctl(connected_namespaces_epollfd_, EPOLL_CTL_ADD,
1040 local_client_fd.get(), &epevent) != 0) {
1041 PLOG(ERROR) << "ConnectNamespaceResponse: epoll_ctl(EPOLL_CTL_ADD) failed";
Hugo Benichie8758b52020-04-03 14:49:01 +09001042 datapath_->RemoveInterface(host_ifname);
1043 datapath_->DeleteIPv4Route(host_ipv4_addr, subnet->BaseAddress(),
1044 subnet->Netmask());
1045 datapath_->RemoveOutboundIPv4(host_ifname);
1046 datapath_->RemoveOutboundIPv4SNATMark(host_ifname);
Hugo Benichi33860d72020-07-09 16:34:01 +09001047 datapath_->NetnsDeleteName(netns_name);
Hugo Benichi7352ad92020-04-07 16:11:59 +09001048 return;
1049 }
1050
Hugo Benichiadf1ec52020-01-17 16:23:58 +09001051 // Prepare the response before storing ConnectNamespaceInfo.
Hugo Benichi2fd0c6e2020-04-17 16:12:05 +09001052 response.set_peer_ifname(client_ifname);
1053 response.set_peer_ipv4_address(host_ipv4_addr);
1054 response.set_host_ifname(host_ifname);
1055 response.set_host_ipv4_address(client_ipv4_addr);
Hugo Benichiadf1ec52020-01-17 16:23:58 +09001056 auto* response_subnet = response.mutable_ipv4_subnet();
1057 response_subnet->set_base_addr(subnet->BaseAddress());
1058 response_subnet->set_prefix_len(subnet->PrefixLength());
1059
1060 // Store ConnectNamespaceInfo
1061 connected_namespaces_next_id_++;
Hugo Benichi7352ad92020-04-07 16:11:59 +09001062 int fdkey = local_client_fd.release();
Hugo Benichiadf1ec52020-01-17 16:23:58 +09001063 connected_namespaces_[fdkey] = {};
1064 ConnectNamespaceInfo& ns_info = connected_namespaces_[fdkey];
1065 ns_info.pid = request.pid();
Hugo Benichi33860d72020-07-09 16:34:01 +09001066 ns_info.netns_name = std::move(netns_name);
Hugo Benichiadf1ec52020-01-17 16:23:58 +09001067 ns_info.outbound_ifname = request.outbound_physical_device();
1068 ns_info.host_ifname = std::move(host_ifname);
1069 ns_info.client_ifname = std::move(client_ifname);
1070 ns_info.client_subnet = std::move(subnet);
1071
Hugo Benichiadf1ec52020-01-17 16:23:58 +09001072 LOG(INFO) << "Connected network namespace " << ns_info;
Hugo Benichi7352ad92020-04-07 16:11:59 +09001073
1074 if (connected_namespaces_.size() == 1) {
1075 LOG(INFO) << "Starting ConnectNamespace client fds monitoring";
1076 CheckConnectedNamespaces();
1077 }
Hugo Benichiadf1ec52020-01-17 16:23:58 +09001078}
1079
1080void Manager::DisconnectNamespace(int client_fd) {
1081 auto it = connected_namespaces_.find(client_fd);
1082 if (it == connected_namespaces_.end()) {
1083 LOG(ERROR) << "No ConnectNamespaceInfo found for client_fd " << client_fd;
1084 return;
1085 }
1086
Hugo Benichi7352ad92020-04-07 16:11:59 +09001087 // Remove the client fd dupe from the epoll watcher and close it.
1088 if (epoll_ctl(connected_namespaces_epollfd_, EPOLL_CTL_DEL, client_fd,
Hugo Benichie8758b52020-04-03 14:49:01 +09001089 nullptr) != 0)
Hugo Benichi7352ad92020-04-07 16:11:59 +09001090 PLOG(ERROR) << "DisconnectNamespace: epoll_ctl(EPOLL_CTL_DEL) failed";
Hugo Benichie8758b52020-04-03 14:49:01 +09001091 if (close(client_fd) < 0)
Hugo Benichi7352ad92020-04-07 16:11:59 +09001092 PLOG(ERROR) << "DisconnectNamespace: close(client_fd) failed";
Hugo Benichi7352ad92020-04-07 16:11:59 +09001093
Hugo Benichie8758b52020-04-03 14:49:01 +09001094 // Destroy the interface configuration and routing configuration:
Hugo Benichiadf1ec52020-01-17 16:23:58 +09001095 // - destroy veth pair.
Hugo Benichie8758b52020-04-03 14:49:01 +09001096 // - remove forwarding rules on host namespace.
1097 // - remove SNAT marking rule on host namespace.
Hugo Benichi33860d72020-07-09 16:34:01 +09001098 // Delete the network namespace attached to the client namespace.
Hugo Benichie8758b52020-04-03 14:49:01 +09001099 // Note that the default route set inside the client namespace by patchpanel
1100 // is not destroyed: it is assumed the client will also teardown its
1101 // namespace if it triggered DisconnectNamespace.
1102 datapath_->RemoveInterface(it->second.host_ifname);
1103 datapath_->RemoveOutboundIPv4(it->second.host_ifname);
1104 datapath_->RemoveOutboundIPv4SNATMark(it->second.host_ifname);
1105 datapath_->DeleteIPv4Route(it->second.client_subnet->AddressAtOffset(0),
1106 it->second.client_subnet->BaseAddress(),
1107 it->second.client_subnet->Netmask());
Hugo Benichi33860d72020-07-09 16:34:01 +09001108 datapath_->NetnsDeleteName(it->second.netns_name);
Hugo Benichiadf1ec52020-01-17 16:23:58 +09001109
1110 LOG(INFO) << "Disconnected network namespace " << it->second;
1111
1112 // This release the allocated IPv4 subnet.
1113 connected_namespaces_.erase(it);
1114}
1115
Hugo Benichi7352ad92020-04-07 16:11:59 +09001116// TODO(hugobenichi) Generalize this check to all resources created by
1117// patchpanel on behalf of a remote client.
1118void Manager::CheckConnectedNamespaces() {
1119 int max_event = 10;
1120 struct epoll_event epevents[max_event];
1121 int nready = epoll_wait(connected_namespaces_epollfd_, epevents, max_event,
1122 0 /* do not block */);
1123 if (nready < 0)
1124 PLOG(ERROR) << "CheckConnectedNamespaces: epoll_wait(0) failed";
1125
1126 for (int i = 0; i < nready; i++)
1127 if (epevents[i].events & (EPOLLHUP | EPOLLERR))
1128 DisconnectNamespace(epevents[i].data.fd);
1129
1130 if (connected_namespaces_.empty()) {
1131 LOG(INFO) << "Stopping ConnectNamespace client fds monitoring";
1132 return;
1133 }
1134
Qijiang Fan2d7aeb42020-05-19 02:06:39 +09001135 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
Hugo Benichi7352ad92020-04-07 16:11:59 +09001136 FROM_HERE,
1137 base::Bind(&Manager::CheckConnectedNamespaces,
Hugo Benichie8758b52020-04-03 14:49:01 +09001138 weak_factory_.GetWeakPtr()),
Hugo Benichi7352ad92020-04-07 16:11:59 +09001139 kConnectNamespaceCheckInterval);
1140}
1141
Jason Jeremy Imanc28df852020-07-11 17:38:26 +09001142bool Manager::ModifyPortRule(const patchpanel::ModifyPortRuleRequest& request) {
1143 switch (request.proto()) {
1144 case patchpanel::ModifyPortRuleRequest::TCP:
1145 case patchpanel::ModifyPortRuleRequest::UDP:
1146 break;
1147 default:
1148 LOG(ERROR) << "Unknown protocol " << request.proto();
1149 return false;
1150 }
1151
1152 switch (request.op()) {
1153 case patchpanel::ModifyPortRuleRequest::CREATE:
1154 switch (request.type()) {
1155 case patchpanel::ModifyPortRuleRequest::ACCESS:
1156 return firewall_.AddAcceptRules(request.proto(),
1157 request.input_dst_port(),
1158 request.input_ifname());
1159 case patchpanel::ModifyPortRuleRequest::LOCKDOWN:
1160 return firewall_.AddLoopbackLockdownRules(request.proto(),
1161 request.input_dst_port());
1162 case patchpanel::ModifyPortRuleRequest::FORWARDING:
1163 return firewall_.AddIpv4ForwardRule(
1164 request.proto(), request.input_dst_ip(), request.input_dst_port(),
1165 request.input_ifname(), request.dst_ip(), request.dst_port());
1166 default:
1167 LOG(ERROR) << "Unknown port rule type " << request.type();
1168 return false;
1169 }
1170 case patchpanel::ModifyPortRuleRequest::DELETE:
1171 switch (request.type()) {
1172 case patchpanel::ModifyPortRuleRequest::ACCESS:
1173 return firewall_.DeleteAcceptRules(request.proto(),
1174 request.input_dst_port(),
1175 request.input_ifname());
1176 case patchpanel::ModifyPortRuleRequest::LOCKDOWN:
1177 return firewall_.DeleteLoopbackLockdownRules(
1178 request.proto(), request.input_dst_port());
1179 case patchpanel::ModifyPortRuleRequest::FORWARDING:
1180 return firewall_.DeleteIpv4ForwardRule(
1181 request.proto(), request.input_dst_ip(), request.input_dst_port(),
1182 request.input_ifname(), request.dst_ip(), request.dst_port());
1183 default:
1184 LOG(ERROR) << "Unknown port rule type " << request.type();
1185 return false;
1186 }
1187 default:
1188 LOG(ERROR) << "Unknown operation " << request.op();
1189 return false;
1190 }
1191}
1192
Garrick Evanse94a14e2019-11-11 10:32:13 +09001193void Manager::SendGuestMessage(const GuestMessage& msg) {
Garrick Evans96e03042019-05-28 14:30:52 +09001194 IpHelperMessage ipm;
1195 *ipm.mutable_guest_message() = msg;
Garrick Evans96e03042019-05-28 14:30:52 +09001196 adb_proxy_->SendMessage(ipm);
Garrick Evanse94a14e2019-11-11 10:32:13 +09001197 mcast_proxy_->SendMessage(ipm);
1198 nd_proxy_->SendMessage(ipm);
Garrick Evans96e03042019-05-28 14:30:52 +09001199}
1200
Garrick Evans4ac09852020-01-16 14:09:22 +09001201void Manager::StartForwarding(const std::string& ifname_physical,
1202 const std::string& ifname_virtual,
Garrick Evans4ac09852020-01-16 14:09:22 +09001203 bool ipv6,
1204 bool multicast) {
Taoyu Li7dca19a2020-03-16 16:27:07 +09001205 if (ifname_physical.empty() || ifname_virtual.empty())
Garrick Evans4ac09852020-01-16 14:09:22 +09001206 return;
1207
1208 IpHelperMessage ipm;
1209 DeviceMessage* msg = ipm.mutable_device_message();
1210 msg->set_dev_ifname(ifname_physical);
Garrick Evans4ac09852020-01-16 14:09:22 +09001211 msg->set_br_ifname(ifname_virtual);
1212
1213 if (ipv6) {
1214 LOG(INFO) << "Starting IPv6 forwarding from " << ifname_physical << " to "
1215 << ifname_virtual;
1216
1217 if (!datapath_->AddIPv6Forwarding(ifname_physical, ifname_virtual)) {
1218 LOG(ERROR) << "Failed to setup iptables forwarding rule for IPv6 from "
1219 << ifname_physical << " to " << ifname_virtual;
1220 }
1221 if (!datapath_->MaskInterfaceFlags(ifname_physical, IFF_ALLMULTI)) {
1222 LOG(WARNING) << "Failed to setup all multicast mode for interface "
1223 << ifname_physical;
1224 }
1225 if (!datapath_->MaskInterfaceFlags(ifname_virtual, IFF_ALLMULTI)) {
1226 LOG(WARNING) << "Failed to setup all multicast mode for interface "
1227 << ifname_virtual;
1228 }
1229 nd_proxy_->SendMessage(ipm);
1230 }
1231
1232 if (multicast) {
1233 LOG(INFO) << "Starting multicast forwarding from " << ifname_physical
1234 << " to " << ifname_virtual;
1235 mcast_proxy_->SendMessage(ipm);
1236 }
1237}
1238
1239void Manager::StopForwarding(const std::string& ifname_physical,
1240 const std::string& ifname_virtual,
1241 bool ipv6,
1242 bool multicast) {
1243 if (ifname_physical.empty())
1244 return;
1245
1246 IpHelperMessage ipm;
1247 DeviceMessage* msg = ipm.mutable_device_message();
1248 msg->set_dev_ifname(ifname_physical);
1249 msg->set_teardown(true);
Taoyu Li7dca19a2020-03-16 16:27:07 +09001250 if (!ifname_virtual.empty()) {
1251 msg->set_br_ifname(ifname_virtual);
1252 }
Garrick Evans4ac09852020-01-16 14:09:22 +09001253
1254 if (ipv6) {
Taoyu Li7dca19a2020-03-16 16:27:07 +09001255 if (ifname_virtual.empty()) {
1256 LOG(INFO) << "Stopping IPv6 forwarding on " << ifname_physical;
1257 } else {
1258 LOG(INFO) << "Stopping IPv6 forwarding from " << ifname_physical << " to "
1259 << ifname_virtual;
1260 datapath_->RemoveIPv6Forwarding(ifname_physical, ifname_virtual);
1261 }
Garrick Evans4ac09852020-01-16 14:09:22 +09001262 nd_proxy_->SendMessage(ipm);
1263 }
1264
1265 if (multicast) {
Taoyu Li7dca19a2020-03-16 16:27:07 +09001266 if (ifname_virtual.empty()) {
1267 LOG(INFO) << "Stopping multicast forwarding on " << ifname_physical;
1268 } else {
1269 LOG(INFO) << "Stopping multicast forwarding from " << ifname_physical
1270 << " to " << ifname_virtual;
1271 }
Garrick Evans4ac09852020-01-16 14:09:22 +09001272 mcast_proxy_->SendMessage(ipm);
1273 }
1274}
1275
Taoyu Lia0727dc2020-09-24 19:54:59 +09001276void Manager::OnNDProxyMessage(const NDProxyMessage& msg) {
1277 LOG_IF(DFATAL, msg.ifname().empty())
Garrick Evans4ac09852020-01-16 14:09:22 +09001278 << "Received DeviceMessage w/ empty dev_ifname";
Taoyu Lia0727dc2020-09-24 19:54:59 +09001279 switch (msg.type()) {
1280 case NDProxyMessage::ADD_ROUTE:
1281 if (!datapath_->AddIPv6HostRoute(msg.ifname(), msg.ip6addr(), 128)) {
1282 LOG(WARNING) << "Failed to setup the IPv6 route for interface "
1283 << msg.ifname() << ", addr " << msg.ip6addr();
1284 }
1285 break;
1286 case NDProxyMessage::ADD_ADDR:
1287 if (!datapath_->AddIPv6Address(msg.ifname(), msg.ip6addr())) {
1288 LOG(WARNING) << "Failed to setup the IPv6 address for interface "
1289 << msg.ifname() << ", addr " << msg.ip6addr();
1290 }
1291 break;
1292 case NDProxyMessage::DEL_ADDR:
1293 datapath_->RemoveIPv6Address(msg.ifname(), msg.ip6addr());
1294 break;
1295 default:
1296 LOG(ERROR) << "Unknown NDProxy event " << msg.type();
1297 NOTREACHED();
Garrick Evans4ac09852020-01-16 14:09:22 +09001298 }
1299}
1300
Hugo Benichiadf1ec52020-01-17 16:23:58 +09001301std::ostream& operator<<(std::ostream& stream,
1302 const Manager::ConnectNamespaceInfo& ns_info) {
1303 stream << "{ pid: " << ns_info.pid;
1304 if (!ns_info.outbound_ifname.empty()) {
1305 stream << ", outbound_ifname: " << ns_info.outbound_ifname;
1306 }
1307 stream << ", host_ifname: " << ns_info.host_ifname
1308 << ", client_ifname: " << ns_info.client_ifname
1309 << ", subnet: " << ns_info.client_subnet->ToCidrString() << '}';
1310 return stream;
1311}
1312
Garrick Evans3388a032020-03-24 11:25:55 +09001313} // namespace patchpanel