blob: f8e455261c949d834a89fb1f907c31bbdd8831ce [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#ifndef PATCHPANEL_MANAGER_H_
6#define PATCHPANEL_MANAGER_H_
Kevin Cernekee95d4ae92016-06-19 10:26:29 -07007
Hugo Benichiadf1ec52020-01-17 16:23:58 +09008#include <iostream>
Garrick Evans49879532018-12-03 13:15:36 +09009#include <map>
Kevin Cernekee95d4ae92016-06-19 10:26:29 -070010#include <memory>
Garrick Evans49879532018-12-03 13:15:36 +090011#include <set>
Kevin Cernekee95d4ae92016-06-19 10:26:29 -070012#include <string>
Jason Jeremy Imanf4156cb2019-11-14 15:36:22 +090013#include <vector>
Kevin Cernekee95d4ae92016-06-19 10:26:29 -070014
15#include <base/memory/weak_ptr.h>
16#include <brillo/daemons/dbus_daemon.h>
Simon Glass2b1da092020-05-21 12:24:16 -060017#include <brillo/process/process_reaper.h>
Garrick Evans08843932019-09-17 14:41:08 +090018#include <chromeos/dbus/service_constants.h>
Hugo Benichi8135e562019-12-12 15:56:36 +090019#include <patchpanel/proto_bindings/patchpanel_service.pb.h>
Kevin Cernekee95d4ae92016-06-19 10:26:29 -070020
Garrick Evans3388a032020-03-24 11:25:55 +090021#include "patchpanel/address_manager.h"
22#include "patchpanel/arc_service.h"
Jie Jiang493cde42020-07-17 21:43:39 +090023#include "patchpanel/counters_service.h"
Garrick Evans3388a032020-03-24 11:25:55 +090024#include "patchpanel/crostini_service.h"
Jason Jeremy Imanc28df852020-07-11 17:38:26 +090025#include "patchpanel/firewall.h"
Garrick Evans3388a032020-03-24 11:25:55 +090026#include "patchpanel/helper_process.h"
Jie Jiang01c1a2e2020-04-08 20:58:30 +090027#include "patchpanel/network_monitor_service.h"
Garrick Evans3388a032020-03-24 11:25:55 +090028#include "patchpanel/routing_service.h"
29#include "patchpanel/shill_client.h"
30#include "patchpanel/socket.h"
31#include "patchpanel/subnet.h"
32#include "patchpanel/traffic_forwarder.h"
Kevin Cernekee95d4ae92016-06-19 10:26:29 -070033
Garrick Evans3388a032020-03-24 11:25:55 +090034namespace patchpanel {
Kevin Cernekee95d4ae92016-06-19 10:26:29 -070035
36// Main class that runs the mainloop and responds to LAN interface changes.
Garrick Evans4ac09852020-01-16 14:09:22 +090037class Manager final : public brillo::DBusDaemon, private TrafficForwarder {
Kevin Cernekee95d4ae92016-06-19 10:26:29 -070038 public:
Hugo Benichiadf1ec52020-01-17 16:23:58 +090039 // Metadata for tracking state associated with a connected namespace.
40 struct ConnectNamespaceInfo {
41 // The pid of the client network namespace.
42 pid_t pid;
Hugo Benichi33860d72020-07-09 16:34:01 +090043 // The name attached to the client network namespace.
44 std::string netns_name;
Hugo Benichiadf1ec52020-01-17 16:23:58 +090045 // Name of the shill device for routing outbound traffic from the client
46 // namespace. Empty if outbound traffic should be forwarded to the highest
47 // priority network (physical or virtual).
48 std::string outbound_ifname;
49 // Name of the "local" veth device visible on the host namespace.
50 std::string host_ifname;
51 // Name of the "remote" veth device moved into the client namespace.
52 std::string client_ifname;
53 // IPv4 subnet assigned to the client namespace.
54 std::unique_ptr<Subnet> client_subnet;
55 };
56
Taoyu Lice7caa62019-10-01 15:43:33 +090057 Manager(std::unique_ptr<HelperProcess> adb_proxy,
Jason Jeremy Imand89b5f52019-10-24 10:39:17 +090058 std::unique_ptr<HelperProcess> mcast_proxy,
Garrick Evans1f5a3612019-11-08 12:59:03 +090059 std::unique_ptr<HelperProcess> nd_proxy);
Qijiang Fan6bc59e12020-11-11 02:51:06 +090060 Manager(const Manager&) = delete;
61 Manager& operator=(const Manager&) = delete;
62
Garrick Evans207e7482019-12-16 11:54:36 +090063 ~Manager();
Kevin Cernekee95d4ae92016-06-19 10:26:29 -070064
Garrick Evans4ac09852020-01-16 14:09:22 +090065 // TrafficForwarder methods.
66
67 void StartForwarding(const std::string& ifname_physical,
68 const std::string& ifname_virtual,
Garrick Evans4ac09852020-01-16 14:09:22 +090069 bool ipv6,
70 bool multicast) override;
71
72 void StopForwarding(const std::string& ifname_physical,
73 const std::string& ifname_virtual,
74 bool ipv6,
75 bool multicast) override;
76
Jason Jeremy Imanf4156cb2019-11-14 15:36:22 +090077 // This function is used to enable specific features only on selected
78 // combination of Android version, Chrome version, and boards.
79 // Empty |supportedBoards| means that the feature should be enabled on all
80 // board.
81 static bool ShouldEnableFeature(
82 int min_android_sdk_version,
83 int min_chrome_milestone,
84 const std::vector<std::string>& supported_boards,
85 const std::string& feature_name);
86
Kevin Cernekee95d4ae92016-06-19 10:26:29 -070087 protected:
88 int OnInit() override;
89
90 private:
Hugo Benichi2a940542020-10-26 18:50:49 +090091 void OnDefaultDeviceChanged(const ShillClient::Device& new_device,
92 const ShillClient::Device& prev_device);
Hugo Benichi76be34a2020-08-26 22:35:54 +090093 void OnDevicesChanged(const std::set<std::string>& added,
94 const std::set<std::string>& removed);
Garrick Evans209a80a2020-11-30 14:42:40 +090095
96 void OnDeviceChanged(const Device& device,
97 Device::ChangeEvent event,
98 GuestMessage::GuestType guest_type);
99
Garrick Evans49879532018-12-03 13:15:36 +0900100 void InitialSetup();
Kevin Cernekee95d4ae92016-06-19 10:26:29 -0700101
Garrick Evanse94a14e2019-11-11 10:32:13 +0900102 bool StartArc(pid_t pid);
Hugo Benichi4d4bb8f2020-07-07 12:16:07 +0900103 void StopArc();
Garrick Evans015b0d62020-02-07 09:06:38 +0900104 bool StartArcVm(uint32_t cid);
105 void StopArcVm(uint32_t cid);
Garrick Evans51d5b552020-01-30 10:42:06 +0900106 bool StartCrosVm(uint64_t vm_id,
107 GuestMessage::GuestType vm_type,
Garrick Evans53a2a982020-02-05 10:53:35 +0900108 uint32_t subnet_index = kAnySubnetIndex);
Garrick Evans51d5b552020-01-30 10:42:06 +0900109 void StopCrosVm(uint64_t vm_id, GuestMessage::GuestType vm_type);
Garrick Evanse94a14e2019-11-11 10:32:13 +0900110
Kevin Cernekee27bcaa62016-12-03 11:16:26 -0800111 // Callback from ProcessReaper to notify Manager that one of the
112 // subprocesses died.
113 void OnSubprocessExited(pid_t pid, const siginfo_t& info);
Garrick Evans4c042572019-12-17 13:42:25 +0900114 void RestartSubprocess(HelperProcess* subproc);
Kevin Cernekee27bcaa62016-12-03 11:16:26 -0800115
Garrick Evans664a82f2019-12-17 12:18:05 +0900116 // Callback from Daemon to notify that SIGTERM or SIGINT was received and
Kevin Cernekee27bcaa62016-12-03 11:16:26 -0800117 // the daemon should clean up in preparation to exit.
118 void OnShutdown(int* exit_code) override;
119
Taoyu Lia0727dc2020-09-24 19:54:59 +0900120 // Callback from NDProxy telling us to add a new IPv6 route to guest or IPv6
121 // address to guest-facing interface.
122 void OnNDProxyMessage(const NDProxyMessage& msg);
Garrick Evans4ac09852020-01-16 14:09:22 +0900123
Garrick Evans02e6e872020-11-30 11:53:13 +0900124 // Handles DBus request for managed device list.
125 std::unique_ptr<dbus::Response> OnGetDevices(dbus::MethodCall* method_call);
126
Garrick Evans08843932019-09-17 14:41:08 +0900127 // Handles DBus notification indicating ARC++ is booting up.
128 std::unique_ptr<dbus::Response> OnArcStartup(dbus::MethodCall* method_call);
129
130 // Handles DBus notification indicating ARC++ is spinning down.
131 std::unique_ptr<dbus::Response> OnArcShutdown(dbus::MethodCall* method_call);
132
133 // Handles DBus notification indicating ARCVM is booting up.
134 std::unique_ptr<dbus::Response> OnArcVmStartup(dbus::MethodCall* method_call);
135
136 // Handles DBus notification indicating ARCVM is spinning down.
137 std::unique_ptr<dbus::Response> OnArcVmShutdown(
138 dbus::MethodCall* method_call);
139
Garrick Evans47c19272019-11-21 10:58:21 +0900140 // Handles DBus notification indicating a Termina VM is booting up.
141 std::unique_ptr<dbus::Response> OnTerminaVmStartup(
142 dbus::MethodCall* method_call);
143
144 // Handles DBus notification indicating a Termina VM is spinning down.
145 std::unique_ptr<dbus::Response> OnTerminaVmShutdown(
146 dbus::MethodCall* method_call);
147
Garrick Evans51d5b552020-01-30 10:42:06 +0900148 // Handles DBus notification indicating a Plugin VM is booting up.
149 std::unique_ptr<dbus::Response> OnPluginVmStartup(
150 dbus::MethodCall* method_call);
151
152 // Handles DBus notification indicating a Plugin VM is spinning down.
153 std::unique_ptr<dbus::Response> OnPluginVmShutdown(
154 dbus::MethodCall* method_call);
155
Hugo Benichi7d9d8db2020-03-30 15:56:56 +0900156 // Handles DBus requests for setting a VPN intent fwmark on a socket.
157 std::unique_ptr<dbus::Response> OnSetVpnIntent(dbus::MethodCall* method_call);
158
Hugo Benichib56b77c2020-01-15 16:00:56 +0900159 // Handles DBus requests for connect and routing an existing network
160 // namespace created via minijail or through the rtnl
161 std::unique_ptr<dbus::Response> OnConnectNamespace(
162 dbus::MethodCall* method_call);
163
Jie Jiang493cde42020-07-17 21:43:39 +0900164 // Handles DBus requests for querying traffic counters.
165 std::unique_ptr<dbus::Response> OnGetTrafficCounters(
166 dbus::MethodCall* method_call);
167
Jason Jeremy Imanb2a421e2020-07-04 20:48:32 +0900168 // Handles DBus requests for creating iptables rules by permission_broker.
169 std::unique_ptr<dbus::Response> OnModifyPortRule(
170 dbus::MethodCall* method_call);
171
Jie Jiang25c1b972020-11-12 15:42:53 +0900172 // Sends out DBus signal for notifying neighbor reachability event.
173 void OnNeighborReachabilityEvent(
174 int ifindex,
175 const shill::IPAddress& ip_addr,
176 NeighborLinkMonitor::NeighborRole role,
177 NeighborReachabilityEventSignal::EventType event_type);
Jie Jiang84966852020-09-18 18:49:05 +0900178
Hugo Benichi7c342672020-09-08 09:18:14 +0900179 std::unique_ptr<patchpanel::ConnectNamespaceResponse> ConnectNamespace(
180 base::ScopedFD client_fd,
181 const patchpanel::ConnectNamespaceRequest& request);
Hugo Benichiadf1ec52020-01-17 16:23:58 +0900182 void DisconnectNamespace(int client_fd);
Hugo Benichi7352ad92020-04-07 16:11:59 +0900183 // Detects if any file descriptor committed in ConnectNamespace DBus API has
184 // been invalidated by the caller. Calls DisconnectNamespace for any invalid
185 // fd found.
186 void CheckConnectedNamespaces();
Hugo Benichiadf1ec52020-01-17 16:23:58 +0900187
Jason Jeremy Imanc28df852020-07-11 17:38:26 +0900188 bool ModifyPortRule(const patchpanel::ModifyPortRuleRequest& request);
189
Garrick Evanse94a14e2019-11-11 10:32:13 +0900190 // Dispatch |msg| to child processes.
191 void SendGuestMessage(const GuestMessage& msg);
Garrick Evans96e03042019-05-28 14:30:52 +0900192
Hugo Benichi935eca92018-07-03 13:47:24 +0900193 friend std::ostream& operator<<(std::ostream& stream, const Manager& manager);
194
Garrick Evans69b85872020-02-04 11:40:26 +0900195 std::unique_ptr<ShillClient> shill_client_;
Hugo Benichi7d9d8db2020-03-30 15:56:56 +0900196 std::unique_ptr<RoutingService> routing_svc_;
Garrick Evans69b85872020-02-04 11:40:26 +0900197
Garrick Evans5d55f5e2019-07-17 15:28:10 +0900198 // Guest services.
199 std::unique_ptr<ArcService> arc_svc_;
Garrick Evans47c19272019-11-21 10:58:21 +0900200 std::unique_ptr<CrostiniService> cros_svc_;
Garrick Evans5d55f5e2019-07-17 15:28:10 +0900201
Garrick Evans08843932019-09-17 14:41:08 +0900202 // DBus service.
203 dbus::ExportedObject* dbus_svc_path_; // Owned by |bus_|.
204
Jason Jeremy Imanc28df852020-07-11 17:38:26 +0900205 // Firewall service.
206 Firewall firewall_;
207
Garrick Evans5d55f5e2019-07-17 15:28:10 +0900208 // Other services.
Garrick Evans3915af32019-07-25 15:44:34 +0900209 brillo::ProcessReaper process_reaper_;
Garrick Evans96e03042019-05-28 14:30:52 +0900210 std::unique_ptr<HelperProcess> adb_proxy_;
Jason Jeremy Imand89b5f52019-10-24 10:39:17 +0900211 std::unique_ptr<HelperProcess> mcast_proxy_;
Taoyu Lice7caa62019-10-01 15:43:33 +0900212 std::unique_ptr<HelperProcess> nd_proxy_;
Jie Jiang493cde42020-07-17 21:43:39 +0900213 std::unique_ptr<CountersService> counters_svc_;
Jie Jiang01c1a2e2020-04-08 20:58:30 +0900214 std::unique_ptr<NetworkMonitorService> network_monitor_svc_;
Garrick Evans96e03042019-05-28 14:30:52 +0900215
Garrick Evansf4a93292019-03-13 14:19:43 +0900216 AddressManager addr_mgr_;
Garrick Evansf4a93292019-03-13 14:19:43 +0900217
Jason Jeremy Imanf4156cb2019-11-14 15:36:22 +0900218 // |cached_feature_enabled| stores the cached result of if a feature should be
219 // enabled.
220 static std::map<const std::string, bool> cached_feature_enabled_;
221
Taoyu Li179dcc62019-10-17 11:21:08 +0900222 std::unique_ptr<MinijailedProcessRunner> runner_;
223 std::unique_ptr<Datapath> datapath_;
224
Hugo Benichiadf1ec52020-01-17 16:23:58 +0900225 // All namespaces currently connected through patchpanel ConnectNamespace
226 // API, keyed by file descriptors committed by clients when calling
227 // ConnectNamespace.
228 std::map<int, ConnectNamespaceInfo> connected_namespaces_;
229 int connected_namespaces_next_id_{0};
Hugo Benichi7352ad92020-04-07 16:11:59 +0900230 // epoll file descriptor for watching client fds committed with the
231 // ConnectNamespace DBus API.
232 int connected_namespaces_epollfd_;
Hugo Benichiadf1ec52020-01-17 16:23:58 +0900233
Kevin Cernekee95d4ae92016-06-19 10:26:29 -0700234 base::WeakPtrFactory<Manager> weak_factory_{this};
235};
236
Hugo Benichiadf1ec52020-01-17 16:23:58 +0900237std::ostream& operator<<(std::ostream& stream,
238 const Manager::ConnectNamespaceInfo& ns_info);
239
Garrick Evans3388a032020-03-24 11:25:55 +0900240} // namespace patchpanel
Kevin Cernekee95d4ae92016-06-19 10:26:29 -0700241
Garrick Evans3388a032020-03-24 11:25:55 +0900242#endif // PATCHPANEL_MANAGER_H_