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" |
Jie Jiang | 493cde4 | 2020-07-17 21:43:39 +0900 | [diff] [blame] | 23 | #include "patchpanel/counters_service.h" |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 24 | #include "patchpanel/crostini_service.h" |
Jason Jeremy Iman | c28df85 | 2020-07-11 17:38:26 +0900 | [diff] [blame] | 25 | #include "patchpanel/firewall.h" |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 26 | #include "patchpanel/helper_process.h" |
Jie Jiang | 01c1a2e | 2020-04-08 20:58:30 +0900 | [diff] [blame] | 27 | #include "patchpanel/network_monitor_service.h" |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 28 | #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 Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 33 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 34 | namespace patchpanel { |
Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 35 | |
| 36 | // Main class that runs the mainloop and responds to LAN interface changes. |
Garrick Evans | 4ac0985 | 2020-01-16 14:09:22 +0900 | [diff] [blame] | 37 | class Manager final : public brillo::DBusDaemon, private TrafficForwarder { |
Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 38 | public: |
Hugo Benichi | adf1ec5 | 2020-01-17 16:23:58 +0900 | [diff] [blame] | 39 | // 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 Benichi | 33860d7 | 2020-07-09 16:34:01 +0900 | [diff] [blame] | 43 | // The name attached to the client network namespace. |
| 44 | std::string netns_name; |
Hugo Benichi | adf1ec5 | 2020-01-17 16:23:58 +0900 | [diff] [blame] | 45 | // 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 Li | ce7caa6 | 2019-10-01 15:43:33 +0900 | [diff] [blame] | 57 | Manager(std::unique_ptr<HelperProcess> adb_proxy, |
Jason Jeremy Iman | d89b5f5 | 2019-10-24 10:39:17 +0900 | [diff] [blame] | 58 | std::unique_ptr<HelperProcess> mcast_proxy, |
Garrick Evans | 1f5a361 | 2019-11-08 12:59:03 +0900 | [diff] [blame] | 59 | std::unique_ptr<HelperProcess> nd_proxy); |
Qijiang Fan | 6bc59e1 | 2020-11-11 02:51:06 +0900 | [diff] [blame^] | 60 | Manager(const Manager&) = delete; |
| 61 | Manager& operator=(const Manager&) = delete; |
| 62 | |
Garrick Evans | 207e748 | 2019-12-16 11:54:36 +0900 | [diff] [blame] | 63 | ~Manager(); |
Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 64 | |
Garrick Evans | 4ac0985 | 2020-01-16 14:09:22 +0900 | [diff] [blame] | 65 | // TrafficForwarder methods. |
| 66 | |
| 67 | void StartForwarding(const std::string& ifname_physical, |
| 68 | const std::string& ifname_virtual, |
Garrick Evans | 4ac0985 | 2020-01-16 14:09:22 +0900 | [diff] [blame] | 69 | 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 Iman | f4156cb | 2019-11-14 15:36:22 +0900 | [diff] [blame] | 77 | // 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 Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 87 | protected: |
| 88 | int OnInit() override; |
| 89 | |
| 90 | private: |
Hugo Benichi | 76be34a | 2020-08-26 22:35:54 +0900 | [diff] [blame] | 91 | void OnDevicesChanged(const std::set<std::string>& added, |
| 92 | const std::set<std::string>& removed); |
Garrick Evans | 4987953 | 2018-12-03 13:15:36 +0900 | [diff] [blame] | 93 | void InitialSetup(); |
Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 94 | |
Garrick Evans | e94a14e | 2019-11-11 10:32:13 +0900 | [diff] [blame] | 95 | bool StartArc(pid_t pid); |
Hugo Benichi | 4d4bb8f | 2020-07-07 12:16:07 +0900 | [diff] [blame] | 96 | void StopArc(); |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 97 | bool StartArcVm(uint32_t cid); |
| 98 | void StopArcVm(uint32_t cid); |
Garrick Evans | 51d5b55 | 2020-01-30 10:42:06 +0900 | [diff] [blame] | 99 | bool StartCrosVm(uint64_t vm_id, |
| 100 | GuestMessage::GuestType vm_type, |
Garrick Evans | 53a2a98 | 2020-02-05 10:53:35 +0900 | [diff] [blame] | 101 | uint32_t subnet_index = kAnySubnetIndex); |
Garrick Evans | 51d5b55 | 2020-01-30 10:42:06 +0900 | [diff] [blame] | 102 | void StopCrosVm(uint64_t vm_id, GuestMessage::GuestType vm_type); |
Garrick Evans | e94a14e | 2019-11-11 10:32:13 +0900 | [diff] [blame] | 103 | |
Kevin Cernekee | 27bcaa6 | 2016-12-03 11:16:26 -0800 | [diff] [blame] | 104 | // Callback from ProcessReaper to notify Manager that one of the |
| 105 | // subprocesses died. |
| 106 | void OnSubprocessExited(pid_t pid, const siginfo_t& info); |
Garrick Evans | 4c04257 | 2019-12-17 13:42:25 +0900 | [diff] [blame] | 107 | void RestartSubprocess(HelperProcess* subproc); |
Kevin Cernekee | 27bcaa6 | 2016-12-03 11:16:26 -0800 | [diff] [blame] | 108 | |
Garrick Evans | 664a82f | 2019-12-17 12:18:05 +0900 | [diff] [blame] | 109 | // Callback from Daemon to notify that SIGTERM or SIGINT was received and |
Kevin Cernekee | 27bcaa6 | 2016-12-03 11:16:26 -0800 | [diff] [blame] | 110 | // the daemon should clean up in preparation to exit. |
| 111 | void OnShutdown(int* exit_code) override; |
| 112 | |
Taoyu Li | a0727dc | 2020-09-24 19:54:59 +0900 | [diff] [blame] | 113 | // Callback from NDProxy telling us to add a new IPv6 route to guest or IPv6 |
| 114 | // address to guest-facing interface. |
| 115 | void OnNDProxyMessage(const NDProxyMessage& msg); |
Garrick Evans | 4ac0985 | 2020-01-16 14:09:22 +0900 | [diff] [blame] | 116 | |
Garrick Evans | 0884393 | 2019-09-17 14:41:08 +0900 | [diff] [blame] | 117 | // Handles DBus notification indicating ARC++ is booting up. |
| 118 | std::unique_ptr<dbus::Response> OnArcStartup(dbus::MethodCall* method_call); |
| 119 | |
| 120 | // Handles DBus notification indicating ARC++ is spinning down. |
| 121 | std::unique_ptr<dbus::Response> OnArcShutdown(dbus::MethodCall* method_call); |
| 122 | |
| 123 | // Handles DBus notification indicating ARCVM is booting up. |
| 124 | std::unique_ptr<dbus::Response> OnArcVmStartup(dbus::MethodCall* method_call); |
| 125 | |
| 126 | // Handles DBus notification indicating ARCVM is spinning down. |
| 127 | std::unique_ptr<dbus::Response> OnArcVmShutdown( |
| 128 | dbus::MethodCall* method_call); |
| 129 | |
Garrick Evans | 47c1927 | 2019-11-21 10:58:21 +0900 | [diff] [blame] | 130 | // Handles DBus notification indicating a Termina VM is booting up. |
| 131 | std::unique_ptr<dbus::Response> OnTerminaVmStartup( |
| 132 | dbus::MethodCall* method_call); |
| 133 | |
| 134 | // Handles DBus notification indicating a Termina VM is spinning down. |
| 135 | std::unique_ptr<dbus::Response> OnTerminaVmShutdown( |
| 136 | dbus::MethodCall* method_call); |
| 137 | |
Garrick Evans | 51d5b55 | 2020-01-30 10:42:06 +0900 | [diff] [blame] | 138 | // Handles DBus notification indicating a Plugin VM is booting up. |
| 139 | std::unique_ptr<dbus::Response> OnPluginVmStartup( |
| 140 | dbus::MethodCall* method_call); |
| 141 | |
| 142 | // Handles DBus notification indicating a Plugin VM is spinning down. |
| 143 | std::unique_ptr<dbus::Response> OnPluginVmShutdown( |
| 144 | dbus::MethodCall* method_call); |
| 145 | |
Hugo Benichi | 7d9d8db | 2020-03-30 15:56:56 +0900 | [diff] [blame] | 146 | // Handles DBus requests for setting a VPN intent fwmark on a socket. |
| 147 | std::unique_ptr<dbus::Response> OnSetVpnIntent(dbus::MethodCall* method_call); |
| 148 | |
Hugo Benichi | b56b77c | 2020-01-15 16:00:56 +0900 | [diff] [blame] | 149 | // Handles DBus requests for connect and routing an existing network |
| 150 | // namespace created via minijail or through the rtnl |
| 151 | std::unique_ptr<dbus::Response> OnConnectNamespace( |
| 152 | dbus::MethodCall* method_call); |
| 153 | |
Jie Jiang | 493cde4 | 2020-07-17 21:43:39 +0900 | [diff] [blame] | 154 | // Handles DBus requests for querying traffic counters. |
| 155 | std::unique_ptr<dbus::Response> OnGetTrafficCounters( |
| 156 | dbus::MethodCall* method_call); |
| 157 | |
Jason Jeremy Iman | b2a421e | 2020-07-04 20:48:32 +0900 | [diff] [blame] | 158 | // Handles DBus requests for creating iptables rules by permission_broker. |
| 159 | std::unique_ptr<dbus::Response> OnModifyPortRule( |
| 160 | dbus::MethodCall* method_call); |
| 161 | |
Jie Jiang | 8496685 | 2020-09-18 18:49:05 +0900 | [diff] [blame] | 162 | // Sends out DBus signal for notifying neighbor connected state changed. |
| 163 | void OnNeighborConnectedStateChanged(int ifindex, |
| 164 | const shill::IPAddress& ip_addr, |
| 165 | NeighborLinkMonitor::NeighborRole role, |
| 166 | bool connected); |
| 167 | |
Hugo Benichi | 7c34267 | 2020-09-08 09:18:14 +0900 | [diff] [blame] | 168 | std::unique_ptr<patchpanel::ConnectNamespaceResponse> ConnectNamespace( |
| 169 | base::ScopedFD client_fd, |
| 170 | const patchpanel::ConnectNamespaceRequest& request); |
Hugo Benichi | adf1ec5 | 2020-01-17 16:23:58 +0900 | [diff] [blame] | 171 | void DisconnectNamespace(int client_fd); |
Hugo Benichi | 7352ad9 | 2020-04-07 16:11:59 +0900 | [diff] [blame] | 172 | // Detects if any file descriptor committed in ConnectNamespace DBus API has |
| 173 | // been invalidated by the caller. Calls DisconnectNamespace for any invalid |
| 174 | // fd found. |
| 175 | void CheckConnectedNamespaces(); |
Hugo Benichi | adf1ec5 | 2020-01-17 16:23:58 +0900 | [diff] [blame] | 176 | |
Jason Jeremy Iman | c28df85 | 2020-07-11 17:38:26 +0900 | [diff] [blame] | 177 | bool ModifyPortRule(const patchpanel::ModifyPortRuleRequest& request); |
| 178 | |
Garrick Evans | e94a14e | 2019-11-11 10:32:13 +0900 | [diff] [blame] | 179 | // Dispatch |msg| to child processes. |
| 180 | void SendGuestMessage(const GuestMessage& msg); |
Garrick Evans | 96e0304 | 2019-05-28 14:30:52 +0900 | [diff] [blame] | 181 | |
Hugo Benichi | 935eca9 | 2018-07-03 13:47:24 +0900 | [diff] [blame] | 182 | friend std::ostream& operator<<(std::ostream& stream, const Manager& manager); |
| 183 | |
Garrick Evans | 69b8587 | 2020-02-04 11:40:26 +0900 | [diff] [blame] | 184 | std::unique_ptr<ShillClient> shill_client_; |
Hugo Benichi | 7d9d8db | 2020-03-30 15:56:56 +0900 | [diff] [blame] | 185 | std::unique_ptr<RoutingService> routing_svc_; |
Garrick Evans | 69b8587 | 2020-02-04 11:40:26 +0900 | [diff] [blame] | 186 | |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 187 | // Guest services. |
| 188 | std::unique_ptr<ArcService> arc_svc_; |
Garrick Evans | 47c1927 | 2019-11-21 10:58:21 +0900 | [diff] [blame] | 189 | std::unique_ptr<CrostiniService> cros_svc_; |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 190 | |
Garrick Evans | 0884393 | 2019-09-17 14:41:08 +0900 | [diff] [blame] | 191 | // DBus service. |
| 192 | dbus::ExportedObject* dbus_svc_path_; // Owned by |bus_|. |
| 193 | |
Jason Jeremy Iman | c28df85 | 2020-07-11 17:38:26 +0900 | [diff] [blame] | 194 | // Firewall service. |
| 195 | Firewall firewall_; |
| 196 | |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 197 | // Other services. |
Garrick Evans | 3915af3 | 2019-07-25 15:44:34 +0900 | [diff] [blame] | 198 | brillo::ProcessReaper process_reaper_; |
Garrick Evans | 96e0304 | 2019-05-28 14:30:52 +0900 | [diff] [blame] | 199 | std::unique_ptr<HelperProcess> adb_proxy_; |
Jason Jeremy Iman | d89b5f5 | 2019-10-24 10:39:17 +0900 | [diff] [blame] | 200 | std::unique_ptr<HelperProcess> mcast_proxy_; |
Taoyu Li | ce7caa6 | 2019-10-01 15:43:33 +0900 | [diff] [blame] | 201 | std::unique_ptr<HelperProcess> nd_proxy_; |
Jie Jiang | 493cde4 | 2020-07-17 21:43:39 +0900 | [diff] [blame] | 202 | std::unique_ptr<CountersService> counters_svc_; |
Jie Jiang | 01c1a2e | 2020-04-08 20:58:30 +0900 | [diff] [blame] | 203 | std::unique_ptr<NetworkMonitorService> network_monitor_svc_; |
Garrick Evans | 96e0304 | 2019-05-28 14:30:52 +0900 | [diff] [blame] | 204 | |
Garrick Evans | f4a9329 | 2019-03-13 14:19:43 +0900 | [diff] [blame] | 205 | AddressManager addr_mgr_; |
Garrick Evans | f4a9329 | 2019-03-13 14:19:43 +0900 | [diff] [blame] | 206 | |
Jason Jeremy Iman | f4156cb | 2019-11-14 15:36:22 +0900 | [diff] [blame] | 207 | // |cached_feature_enabled| stores the cached result of if a feature should be |
| 208 | // enabled. |
| 209 | static std::map<const std::string, bool> cached_feature_enabled_; |
| 210 | |
Taoyu Li | 179dcc6 | 2019-10-17 11:21:08 +0900 | [diff] [blame] | 211 | std::unique_ptr<MinijailedProcessRunner> runner_; |
| 212 | std::unique_ptr<Datapath> datapath_; |
| 213 | |
Hugo Benichi | adf1ec5 | 2020-01-17 16:23:58 +0900 | [diff] [blame] | 214 | // All namespaces currently connected through patchpanel ConnectNamespace |
| 215 | // API, keyed by file descriptors committed by clients when calling |
| 216 | // ConnectNamespace. |
| 217 | std::map<int, ConnectNamespaceInfo> connected_namespaces_; |
| 218 | int connected_namespaces_next_id_{0}; |
Hugo Benichi | 7352ad9 | 2020-04-07 16:11:59 +0900 | [diff] [blame] | 219 | // epoll file descriptor for watching client fds committed with the |
| 220 | // ConnectNamespace DBus API. |
| 221 | int connected_namespaces_epollfd_; |
Hugo Benichi | adf1ec5 | 2020-01-17 16:23:58 +0900 | [diff] [blame] | 222 | |
Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 223 | base::WeakPtrFactory<Manager> weak_factory_{this}; |
| 224 | }; |
| 225 | |
Hugo Benichi | adf1ec5 | 2020-01-17 16:23:58 +0900 | [diff] [blame] | 226 | std::ostream& operator<<(std::ostream& stream, |
| 227 | const Manager::ConnectNamespaceInfo& ns_info); |
| 228 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 229 | } // namespace patchpanel |
Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 230 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 231 | #endif // PATCHPANEL_MANAGER_H_ |