Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 1 | // 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 Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 5 | #ifndef PATCHPANEL_MANAGER_H_ |
| 6 | #define PATCHPANEL_MANAGER_H_ |
Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 7 | |
Hugo Benichi | adf1ec5 | 2020-01-17 16:23:58 +0900 | [diff] [blame] | 8 | #include <iostream> |
Garrick Evans | 4987953 | 2018-12-03 13:15:36 +0900 | [diff] [blame] | 9 | #include <map> |
Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 10 | #include <memory> |
Garrick Evans | 4987953 | 2018-12-03 13:15:36 +0900 | [diff] [blame] | 11 | #include <set> |
Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 12 | #include <string> |
Jason Jeremy Iman | f4156cb | 2019-11-14 15:36:22 +0900 | [diff] [blame] | 13 | #include <vector> |
Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 14 | |
| 15 | #include <base/memory/weak_ptr.h> |
| 16 | #include <brillo/daemons/dbus_daemon.h> |
Simon Glass | 2b1da09 | 2020-05-21 12:24:16 -0600 | [diff] [blame^] | 17 | #include <brillo/process/process_reaper.h> |
Garrick Evans | 0884393 | 2019-09-17 14:41:08 +0900 | [diff] [blame] | 18 | #include <chromeos/dbus/service_constants.h> |
Hugo Benichi | 8135e56 | 2019-12-12 15:56:36 +0900 | [diff] [blame] | 19 | #include <patchpanel/proto_bindings/patchpanel_service.pb.h> |
Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 20 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 21 | #include "patchpanel/address_manager.h" |
| 22 | #include "patchpanel/arc_service.h" |
| 23 | #include "patchpanel/crostini_service.h" |
| 24 | #include "patchpanel/helper_process.h" |
| 25 | #include "patchpanel/routing_service.h" |
| 26 | #include "patchpanel/shill_client.h" |
| 27 | #include "patchpanel/socket.h" |
| 28 | #include "patchpanel/subnet.h" |
| 29 | #include "patchpanel/traffic_forwarder.h" |
Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 30 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 31 | namespace patchpanel { |
Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 32 | |
| 33 | // Main class that runs the mainloop and responds to LAN interface changes. |
Garrick Evans | 4ac0985 | 2020-01-16 14:09:22 +0900 | [diff] [blame] | 34 | class Manager final : public brillo::DBusDaemon, private TrafficForwarder { |
Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 35 | public: |
Hugo Benichi | adf1ec5 | 2020-01-17 16:23:58 +0900 | [diff] [blame] | 36 | // Metadata for tracking state associated with a connected namespace. |
| 37 | struct ConnectNamespaceInfo { |
| 38 | // The pid of the client network namespace. |
| 39 | pid_t pid; |
| 40 | // Name of the shill device for routing outbound traffic from the client |
| 41 | // namespace. Empty if outbound traffic should be forwarded to the highest |
| 42 | // priority network (physical or virtual). |
| 43 | std::string outbound_ifname; |
| 44 | // Name of the "local" veth device visible on the host namespace. |
| 45 | std::string host_ifname; |
| 46 | // Name of the "remote" veth device moved into the client namespace. |
| 47 | std::string client_ifname; |
| 48 | // IPv4 subnet assigned to the client namespace. |
| 49 | std::unique_ptr<Subnet> client_subnet; |
| 50 | }; |
| 51 | |
Taoyu Li | ce7caa6 | 2019-10-01 15:43:33 +0900 | [diff] [blame] | 52 | Manager(std::unique_ptr<HelperProcess> adb_proxy, |
Jason Jeremy Iman | d89b5f5 | 2019-10-24 10:39:17 +0900 | [diff] [blame] | 53 | std::unique_ptr<HelperProcess> mcast_proxy, |
Garrick Evans | 1f5a361 | 2019-11-08 12:59:03 +0900 | [diff] [blame] | 54 | std::unique_ptr<HelperProcess> nd_proxy); |
Garrick Evans | 207e748 | 2019-12-16 11:54:36 +0900 | [diff] [blame] | 55 | ~Manager(); |
Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 56 | |
Garrick Evans | 4ac0985 | 2020-01-16 14:09:22 +0900 | [diff] [blame] | 57 | // TrafficForwarder methods. |
| 58 | |
| 59 | void StartForwarding(const std::string& ifname_physical, |
| 60 | const std::string& ifname_virtual, |
Garrick Evans | 4ac0985 | 2020-01-16 14:09:22 +0900 | [diff] [blame] | 61 | bool ipv6, |
| 62 | bool multicast) override; |
| 63 | |
| 64 | void StopForwarding(const std::string& ifname_physical, |
| 65 | const std::string& ifname_virtual, |
| 66 | bool ipv6, |
| 67 | bool multicast) override; |
| 68 | |
Jason Jeremy Iman | f4156cb | 2019-11-14 15:36:22 +0900 | [diff] [blame] | 69 | // This function is used to enable specific features only on selected |
| 70 | // combination of Android version, Chrome version, and boards. |
| 71 | // Empty |supportedBoards| means that the feature should be enabled on all |
| 72 | // board. |
| 73 | static bool ShouldEnableFeature( |
| 74 | int min_android_sdk_version, |
| 75 | int min_chrome_milestone, |
| 76 | const std::vector<std::string>& supported_boards, |
| 77 | const std::string& feature_name); |
| 78 | |
Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 79 | protected: |
| 80 | int OnInit() override; |
| 81 | |
| 82 | private: |
Garrick Evans | 4987953 | 2018-12-03 13:15:36 +0900 | [diff] [blame] | 83 | void InitialSetup(); |
Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 84 | |
Garrick Evans | e94a14e | 2019-11-11 10:32:13 +0900 | [diff] [blame] | 85 | bool StartArc(pid_t pid); |
Garrick Evans | 21173b1 | 2019-11-20 15:23:16 +0900 | [diff] [blame] | 86 | void StopArc(pid_t pid); |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 87 | bool StartArcVm(uint32_t cid); |
| 88 | void StopArcVm(uint32_t cid); |
Garrick Evans | 51d5b55 | 2020-01-30 10:42:06 +0900 | [diff] [blame] | 89 | bool StartCrosVm(uint64_t vm_id, |
| 90 | GuestMessage::GuestType vm_type, |
Garrick Evans | 53a2a98 | 2020-02-05 10:53:35 +0900 | [diff] [blame] | 91 | uint32_t subnet_index = kAnySubnetIndex); |
Garrick Evans | 51d5b55 | 2020-01-30 10:42:06 +0900 | [diff] [blame] | 92 | void StopCrosVm(uint64_t vm_id, GuestMessage::GuestType vm_type); |
Garrick Evans | e94a14e | 2019-11-11 10:32:13 +0900 | [diff] [blame] | 93 | |
Kevin Cernekee | 27bcaa6 | 2016-12-03 11:16:26 -0800 | [diff] [blame] | 94 | // Callback from ProcessReaper to notify Manager that one of the |
| 95 | // subprocesses died. |
| 96 | void OnSubprocessExited(pid_t pid, const siginfo_t& info); |
Garrick Evans | 4c04257 | 2019-12-17 13:42:25 +0900 | [diff] [blame] | 97 | void RestartSubprocess(HelperProcess* subproc); |
Kevin Cernekee | 27bcaa6 | 2016-12-03 11:16:26 -0800 | [diff] [blame] | 98 | |
Garrick Evans | 664a82f | 2019-12-17 12:18:05 +0900 | [diff] [blame] | 99 | // Callback from Daemon to notify that SIGTERM or SIGINT was received and |
Kevin Cernekee | 27bcaa6 | 2016-12-03 11:16:26 -0800 | [diff] [blame] | 100 | // the daemon should clean up in preparation to exit. |
| 101 | void OnShutdown(int* exit_code) override; |
| 102 | |
Garrick Evans | 4ac0985 | 2020-01-16 14:09:22 +0900 | [diff] [blame] | 103 | // Callback from NDProxy telling us to add a new IPv6 route. |
| 104 | void OnDeviceMessageFromNDProxy(const DeviceMessage& msg); |
| 105 | |
Garrick Evans | 0884393 | 2019-09-17 14:41:08 +0900 | [diff] [blame] | 106 | // Handles DBus notification indicating ARC++ is booting up. |
| 107 | std::unique_ptr<dbus::Response> OnArcStartup(dbus::MethodCall* method_call); |
| 108 | |
| 109 | // Handles DBus notification indicating ARC++ is spinning down. |
| 110 | std::unique_ptr<dbus::Response> OnArcShutdown(dbus::MethodCall* method_call); |
| 111 | |
| 112 | // Handles DBus notification indicating ARCVM is booting up. |
| 113 | std::unique_ptr<dbus::Response> OnArcVmStartup(dbus::MethodCall* method_call); |
| 114 | |
| 115 | // Handles DBus notification indicating ARCVM is spinning down. |
| 116 | std::unique_ptr<dbus::Response> OnArcVmShutdown( |
| 117 | dbus::MethodCall* method_call); |
| 118 | |
Garrick Evans | 47c1927 | 2019-11-21 10:58:21 +0900 | [diff] [blame] | 119 | // Handles DBus notification indicating a Termina VM is booting up. |
| 120 | std::unique_ptr<dbus::Response> OnTerminaVmStartup( |
| 121 | dbus::MethodCall* method_call); |
| 122 | |
| 123 | // Handles DBus notification indicating a Termina VM is spinning down. |
| 124 | std::unique_ptr<dbus::Response> OnTerminaVmShutdown( |
| 125 | dbus::MethodCall* method_call); |
| 126 | |
Garrick Evans | 51d5b55 | 2020-01-30 10:42:06 +0900 | [diff] [blame] | 127 | // Handles DBus notification indicating a Plugin VM is booting up. |
| 128 | std::unique_ptr<dbus::Response> OnPluginVmStartup( |
| 129 | dbus::MethodCall* method_call); |
| 130 | |
| 131 | // Handles DBus notification indicating a Plugin VM is spinning down. |
| 132 | std::unique_ptr<dbus::Response> OnPluginVmShutdown( |
| 133 | dbus::MethodCall* method_call); |
| 134 | |
Hugo Benichi | 7d9d8db | 2020-03-30 15:56:56 +0900 | [diff] [blame] | 135 | // Handles DBus requests for setting a VPN intent fwmark on a socket. |
| 136 | std::unique_ptr<dbus::Response> OnSetVpnIntent(dbus::MethodCall* method_call); |
| 137 | |
Hugo Benichi | b56b77c | 2020-01-15 16:00:56 +0900 | [diff] [blame] | 138 | // Handles DBus requests for connect and routing an existing network |
| 139 | // namespace created via minijail or through the rtnl |
| 140 | std::unique_ptr<dbus::Response> OnConnectNamespace( |
| 141 | dbus::MethodCall* method_call); |
| 142 | |
Hugo Benichi | adf1ec5 | 2020-01-17 16:23:58 +0900 | [diff] [blame] | 143 | void ConnectNamespace(base::ScopedFD client_fd, |
| 144 | const patchpanel::ConnectNamespaceRequest& request, |
| 145 | patchpanel::ConnectNamespaceResponse& response); |
| 146 | void DisconnectNamespace(int client_fd); |
Hugo Benichi | 7352ad9 | 2020-04-07 16:11:59 +0900 | [diff] [blame] | 147 | // Detects if any file descriptor committed in ConnectNamespace DBus API has |
| 148 | // been invalidated by the caller. Calls DisconnectNamespace for any invalid |
| 149 | // fd found. |
| 150 | void CheckConnectedNamespaces(); |
Hugo Benichi | adf1ec5 | 2020-01-17 16:23:58 +0900 | [diff] [blame] | 151 | |
Garrick Evans | e94a14e | 2019-11-11 10:32:13 +0900 | [diff] [blame] | 152 | // Dispatch |msg| to child processes. |
| 153 | void SendGuestMessage(const GuestMessage& msg); |
Garrick Evans | 96e0304 | 2019-05-28 14:30:52 +0900 | [diff] [blame] | 154 | |
Hugo Benichi | 935eca9 | 2018-07-03 13:47:24 +0900 | [diff] [blame] | 155 | friend std::ostream& operator<<(std::ostream& stream, const Manager& manager); |
| 156 | |
Garrick Evans | 69b8587 | 2020-02-04 11:40:26 +0900 | [diff] [blame] | 157 | std::unique_ptr<ShillClient> shill_client_; |
Hugo Benichi | 7d9d8db | 2020-03-30 15:56:56 +0900 | [diff] [blame] | 158 | std::unique_ptr<RoutingService> routing_svc_; |
Garrick Evans | 69b8587 | 2020-02-04 11:40:26 +0900 | [diff] [blame] | 159 | |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 160 | // Guest services. |
| 161 | std::unique_ptr<ArcService> arc_svc_; |
Garrick Evans | 47c1927 | 2019-11-21 10:58:21 +0900 | [diff] [blame] | 162 | std::unique_ptr<CrostiniService> cros_svc_; |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 163 | |
Garrick Evans | 0884393 | 2019-09-17 14:41:08 +0900 | [diff] [blame] | 164 | // DBus service. |
| 165 | dbus::ExportedObject* dbus_svc_path_; // Owned by |bus_|. |
| 166 | |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 167 | // Other services. |
Garrick Evans | 3915af3 | 2019-07-25 15:44:34 +0900 | [diff] [blame] | 168 | brillo::ProcessReaper process_reaper_; |
Garrick Evans | 96e0304 | 2019-05-28 14:30:52 +0900 | [diff] [blame] | 169 | std::unique_ptr<HelperProcess> adb_proxy_; |
Jason Jeremy Iman | d89b5f5 | 2019-10-24 10:39:17 +0900 | [diff] [blame] | 170 | std::unique_ptr<HelperProcess> mcast_proxy_; |
Taoyu Li | ce7caa6 | 2019-10-01 15:43:33 +0900 | [diff] [blame] | 171 | std::unique_ptr<HelperProcess> nd_proxy_; |
Garrick Evans | 96e0304 | 2019-05-28 14:30:52 +0900 | [diff] [blame] | 172 | |
Garrick Evans | f4a9329 | 2019-03-13 14:19:43 +0900 | [diff] [blame] | 173 | AddressManager addr_mgr_; |
Garrick Evans | f4a9329 | 2019-03-13 14:19:43 +0900 | [diff] [blame] | 174 | |
Jason Jeremy Iman | f4156cb | 2019-11-14 15:36:22 +0900 | [diff] [blame] | 175 | // |cached_feature_enabled| stores the cached result of if a feature should be |
| 176 | // enabled. |
| 177 | static std::map<const std::string, bool> cached_feature_enabled_; |
| 178 | |
Taoyu Li | 179dcc6 | 2019-10-17 11:21:08 +0900 | [diff] [blame] | 179 | std::unique_ptr<MinijailedProcessRunner> runner_; |
| 180 | std::unique_ptr<Datapath> datapath_; |
| 181 | |
Hugo Benichi | adf1ec5 | 2020-01-17 16:23:58 +0900 | [diff] [blame] | 182 | // All namespaces currently connected through patchpanel ConnectNamespace |
| 183 | // API, keyed by file descriptors committed by clients when calling |
| 184 | // ConnectNamespace. |
| 185 | std::map<int, ConnectNamespaceInfo> connected_namespaces_; |
| 186 | int connected_namespaces_next_id_{0}; |
Hugo Benichi | 7352ad9 | 2020-04-07 16:11:59 +0900 | [diff] [blame] | 187 | // epoll file descriptor for watching client fds committed with the |
| 188 | // ConnectNamespace DBus API. |
| 189 | int connected_namespaces_epollfd_; |
Hugo Benichi | adf1ec5 | 2020-01-17 16:23:58 +0900 | [diff] [blame] | 190 | |
Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 191 | base::WeakPtrFactory<Manager> weak_factory_{this}; |
Garrick Evans | f4a9329 | 2019-03-13 14:19:43 +0900 | [diff] [blame] | 192 | DISALLOW_COPY_AND_ASSIGN(Manager); |
Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 193 | }; |
| 194 | |
Hugo Benichi | adf1ec5 | 2020-01-17 16:23:58 +0900 | [diff] [blame] | 195 | std::ostream& operator<<(std::ostream& stream, |
| 196 | const Manager::ConnectNamespaceInfo& ns_info); |
| 197 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 198 | } // namespace patchpanel |
Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 199 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 200 | #endif // PATCHPANEL_MANAGER_H_ |