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