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 | |
Hidehiko Abe | 3a7e513 | 2018-02-15 13:07:50 +0900 | [diff] [blame] | 5 | #ifndef ARC_NETWORK_MANAGER_H_ |
| 6 | #define ARC_NETWORK_MANAGER_H_ |
Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 7 | |
Garrick Evans | 4987953 | 2018-12-03 13:15:36 +0900 | [diff] [blame] | 8 | #include <map> |
Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 9 | #include <memory> |
Garrick Evans | 4987953 | 2018-12-03 13:15:36 +0900 | [diff] [blame] | 10 | #include <set> |
Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 11 | #include <string> |
Jason Jeremy Iman | f4156cb | 2019-11-14 15:36:22 +0900 | [diff] [blame] | 12 | #include <vector> |
Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 13 | |
| 14 | #include <base/memory/weak_ptr.h> |
| 15 | #include <brillo/daemons/dbus_daemon.h> |
Kevin Cernekee | 27bcaa6 | 2016-12-03 11:16:26 -0800 | [diff] [blame] | 16 | #include <brillo/process_reaper.h> |
Garrick Evans | 0884393 | 2019-09-17 14:41:08 +0900 | [diff] [blame] | 17 | #include <chromeos/dbus/service_constants.h> |
Hugo Benichi | 8135e56 | 2019-12-12 15:56:36 +0900 | [diff] [blame] | 18 | #include <patchpanel/proto_bindings/patchpanel_service.pb.h> |
Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 19 | |
Garrick Evans | f4a9329 | 2019-03-13 14:19:43 +0900 | [diff] [blame] | 20 | #include "arc/network/address_manager.h" |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 21 | #include "arc/network/arc_service.h" |
Garrick Evans | 47c1927 | 2019-11-21 10:58:21 +0900 | [diff] [blame] | 22 | #include "arc/network/crostini_service.h" |
Hidehiko Abe | 3a7e513 | 2018-02-15 13:07:50 +0900 | [diff] [blame] | 23 | #include "arc/network/helper_process.h" |
Hugo Benichi | 7d9d8db | 2020-03-30 15:56:56 +0900 | [diff] [blame] | 24 | #include "arc/network/routing_service.h" |
Hidehiko Abe | 3a7e513 | 2018-02-15 13:07:50 +0900 | [diff] [blame] | 25 | #include "arc/network/shill_client.h" |
Long Cheng | d441558 | 2019-09-24 19:16:09 +0000 | [diff] [blame] | 26 | #include "arc/network/socket.h" |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 27 | #include "arc/network/traffic_forwarder.h" |
Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 28 | |
| 29 | namespace arc_networkd { |
| 30 | |
| 31 | // Main class that runs the mainloop and responds to LAN interface changes. |
Garrick Evans | 4ac0985 | 2020-01-16 14:09:22 +0900 | [diff] [blame] | 32 | class Manager final : public brillo::DBusDaemon, private TrafficForwarder { |
Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 33 | public: |
Taoyu Li | ce7caa6 | 2019-10-01 15:43:33 +0900 | [diff] [blame] | 34 | Manager(std::unique_ptr<HelperProcess> adb_proxy, |
Jason Jeremy Iman | d89b5f5 | 2019-10-24 10:39:17 +0900 | [diff] [blame] | 35 | std::unique_ptr<HelperProcess> mcast_proxy, |
Garrick Evans | 1f5a361 | 2019-11-08 12:59:03 +0900 | [diff] [blame] | 36 | std::unique_ptr<HelperProcess> nd_proxy); |
Garrick Evans | 207e748 | 2019-12-16 11:54:36 +0900 | [diff] [blame] | 37 | ~Manager(); |
Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 38 | |
Garrick Evans | 4ac0985 | 2020-01-16 14:09:22 +0900 | [diff] [blame] | 39 | // TrafficForwarder methods. |
| 40 | |
| 41 | void StartForwarding(const std::string& ifname_physical, |
| 42 | const std::string& ifname_virtual, |
Garrick Evans | 4ac0985 | 2020-01-16 14:09:22 +0900 | [diff] [blame] | 43 | bool ipv6, |
| 44 | bool multicast) override; |
| 45 | |
| 46 | void StopForwarding(const std::string& ifname_physical, |
| 47 | const std::string& ifname_virtual, |
| 48 | bool ipv6, |
| 49 | bool multicast) override; |
| 50 | |
Jason Jeremy Iman | f4156cb | 2019-11-14 15:36:22 +0900 | [diff] [blame] | 51 | // This function is used to enable specific features only on selected |
| 52 | // combination of Android version, Chrome version, and boards. |
| 53 | // Empty |supportedBoards| means that the feature should be enabled on all |
| 54 | // board. |
| 55 | static bool ShouldEnableFeature( |
| 56 | int min_android_sdk_version, |
| 57 | int min_chrome_milestone, |
| 58 | const std::vector<std::string>& supported_boards, |
| 59 | const std::string& feature_name); |
| 60 | |
Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 61 | protected: |
| 62 | int OnInit() override; |
| 63 | |
| 64 | private: |
Garrick Evans | 4987953 | 2018-12-03 13:15:36 +0900 | [diff] [blame] | 65 | void InitialSetup(); |
Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 66 | |
Garrick Evans | e94a14e | 2019-11-11 10:32:13 +0900 | [diff] [blame] | 67 | bool StartArc(pid_t pid); |
Garrick Evans | 21173b1 | 2019-11-20 15:23:16 +0900 | [diff] [blame] | 68 | void StopArc(pid_t pid); |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 69 | bool StartArcVm(uint32_t cid); |
| 70 | void StopArcVm(uint32_t cid); |
Garrick Evans | 51d5b55 | 2020-01-30 10:42:06 +0900 | [diff] [blame] | 71 | bool StartCrosVm(uint64_t vm_id, |
| 72 | GuestMessage::GuestType vm_type, |
Garrick Evans | 53a2a98 | 2020-02-05 10:53:35 +0900 | [diff] [blame] | 73 | uint32_t subnet_index = kAnySubnetIndex); |
Garrick Evans | 51d5b55 | 2020-01-30 10:42:06 +0900 | [diff] [blame] | 74 | void StopCrosVm(uint64_t vm_id, GuestMessage::GuestType vm_type); |
Garrick Evans | e94a14e | 2019-11-11 10:32:13 +0900 | [diff] [blame] | 75 | |
Kevin Cernekee | 27bcaa6 | 2016-12-03 11:16:26 -0800 | [diff] [blame] | 76 | // Callback from ProcessReaper to notify Manager that one of the |
| 77 | // subprocesses died. |
| 78 | void OnSubprocessExited(pid_t pid, const siginfo_t& info); |
Garrick Evans | 4c04257 | 2019-12-17 13:42:25 +0900 | [diff] [blame] | 79 | void RestartSubprocess(HelperProcess* subproc); |
Kevin Cernekee | 27bcaa6 | 2016-12-03 11:16:26 -0800 | [diff] [blame] | 80 | |
Garrick Evans | 664a82f | 2019-12-17 12:18:05 +0900 | [diff] [blame] | 81 | // Callback from Daemon to notify that SIGTERM or SIGINT was received and |
Kevin Cernekee | 27bcaa6 | 2016-12-03 11:16:26 -0800 | [diff] [blame] | 82 | // the daemon should clean up in preparation to exit. |
| 83 | void OnShutdown(int* exit_code) override; |
| 84 | |
Garrick Evans | 4ac0985 | 2020-01-16 14:09:22 +0900 | [diff] [blame] | 85 | // Callback from NDProxy telling us to add a new IPv6 route. |
| 86 | void OnDeviceMessageFromNDProxy(const DeviceMessage& msg); |
| 87 | |
Garrick Evans | 0884393 | 2019-09-17 14:41:08 +0900 | [diff] [blame] | 88 | // Handles DBus notification indicating ARC++ is booting up. |
| 89 | std::unique_ptr<dbus::Response> OnArcStartup(dbus::MethodCall* method_call); |
| 90 | |
| 91 | // Handles DBus notification indicating ARC++ is spinning down. |
| 92 | std::unique_ptr<dbus::Response> OnArcShutdown(dbus::MethodCall* method_call); |
| 93 | |
| 94 | // Handles DBus notification indicating ARCVM is booting up. |
| 95 | std::unique_ptr<dbus::Response> OnArcVmStartup(dbus::MethodCall* method_call); |
| 96 | |
| 97 | // Handles DBus notification indicating ARCVM is spinning down. |
| 98 | std::unique_ptr<dbus::Response> OnArcVmShutdown( |
| 99 | dbus::MethodCall* method_call); |
| 100 | |
Garrick Evans | 47c1927 | 2019-11-21 10:58:21 +0900 | [diff] [blame] | 101 | // Handles DBus notification indicating a Termina VM is booting up. |
| 102 | std::unique_ptr<dbus::Response> OnTerminaVmStartup( |
| 103 | dbus::MethodCall* method_call); |
| 104 | |
| 105 | // Handles DBus notification indicating a Termina VM is spinning down. |
| 106 | std::unique_ptr<dbus::Response> OnTerminaVmShutdown( |
| 107 | dbus::MethodCall* method_call); |
| 108 | |
Garrick Evans | 51d5b55 | 2020-01-30 10:42:06 +0900 | [diff] [blame] | 109 | // Handles DBus notification indicating a Plugin VM is booting up. |
| 110 | std::unique_ptr<dbus::Response> OnPluginVmStartup( |
| 111 | dbus::MethodCall* method_call); |
| 112 | |
| 113 | // Handles DBus notification indicating a Plugin VM is spinning down. |
| 114 | std::unique_ptr<dbus::Response> OnPluginVmShutdown( |
| 115 | dbus::MethodCall* method_call); |
| 116 | |
Hugo Benichi | 7d9d8db | 2020-03-30 15:56:56 +0900 | [diff] [blame] | 117 | // Handles DBus requests for setting a VPN intent fwmark on a socket. |
| 118 | std::unique_ptr<dbus::Response> OnSetVpnIntent(dbus::MethodCall* method_call); |
| 119 | |
Hugo Benichi | b56b77c | 2020-01-15 16:00:56 +0900 | [diff] [blame^] | 120 | // Handles DBus requests for connect and routing an existing network |
| 121 | // namespace created via minijail or through the rtnl |
| 122 | std::unique_ptr<dbus::Response> OnConnectNamespace( |
| 123 | dbus::MethodCall* method_call); |
| 124 | |
Garrick Evans | e94a14e | 2019-11-11 10:32:13 +0900 | [diff] [blame] | 125 | // Dispatch |msg| to child processes. |
| 126 | void SendGuestMessage(const GuestMessage& msg); |
Garrick Evans | 96e0304 | 2019-05-28 14:30:52 +0900 | [diff] [blame] | 127 | |
Hugo Benichi | 935eca9 | 2018-07-03 13:47:24 +0900 | [diff] [blame] | 128 | friend std::ostream& operator<<(std::ostream& stream, const Manager& manager); |
| 129 | |
Garrick Evans | 69b8587 | 2020-02-04 11:40:26 +0900 | [diff] [blame] | 130 | std::unique_ptr<ShillClient> shill_client_; |
Hugo Benichi | 7d9d8db | 2020-03-30 15:56:56 +0900 | [diff] [blame] | 131 | std::unique_ptr<RoutingService> routing_svc_; |
Garrick Evans | 69b8587 | 2020-02-04 11:40:26 +0900 | [diff] [blame] | 132 | |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 133 | // Guest services. |
| 134 | std::unique_ptr<ArcService> arc_svc_; |
Garrick Evans | 47c1927 | 2019-11-21 10:58:21 +0900 | [diff] [blame] | 135 | std::unique_ptr<CrostiniService> cros_svc_; |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 136 | |
Garrick Evans | 0884393 | 2019-09-17 14:41:08 +0900 | [diff] [blame] | 137 | // DBus service. |
| 138 | dbus::ExportedObject* dbus_svc_path_; // Owned by |bus_|. |
| 139 | |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 140 | // Other services. |
Garrick Evans | 3915af3 | 2019-07-25 15:44:34 +0900 | [diff] [blame] | 141 | brillo::ProcessReaper process_reaper_; |
Garrick Evans | 96e0304 | 2019-05-28 14:30:52 +0900 | [diff] [blame] | 142 | std::unique_ptr<HelperProcess> adb_proxy_; |
Jason Jeremy Iman | d89b5f5 | 2019-10-24 10:39:17 +0900 | [diff] [blame] | 143 | std::unique_ptr<HelperProcess> mcast_proxy_; |
Taoyu Li | ce7caa6 | 2019-10-01 15:43:33 +0900 | [diff] [blame] | 144 | std::unique_ptr<HelperProcess> nd_proxy_; |
Garrick Evans | 96e0304 | 2019-05-28 14:30:52 +0900 | [diff] [blame] | 145 | |
Garrick Evans | f4a9329 | 2019-03-13 14:19:43 +0900 | [diff] [blame] | 146 | AddressManager addr_mgr_; |
Garrick Evans | f4a9329 | 2019-03-13 14:19:43 +0900 | [diff] [blame] | 147 | |
Jason Jeremy Iman | f4156cb | 2019-11-14 15:36:22 +0900 | [diff] [blame] | 148 | // |cached_feature_enabled| stores the cached result of if a feature should be |
| 149 | // enabled. |
| 150 | static std::map<const std::string, bool> cached_feature_enabled_; |
| 151 | |
Taoyu Li | 179dcc6 | 2019-10-17 11:21:08 +0900 | [diff] [blame] | 152 | std::unique_ptr<MinijailedProcessRunner> runner_; |
| 153 | std::unique_ptr<Datapath> datapath_; |
| 154 | |
Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 155 | base::WeakPtrFactory<Manager> weak_factory_{this}; |
Garrick Evans | f4a9329 | 2019-03-13 14:19:43 +0900 | [diff] [blame] | 156 | DISALLOW_COPY_AND_ASSIGN(Manager); |
Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 157 | }; |
| 158 | |
| 159 | } // namespace arc_networkd |
| 160 | |
Hidehiko Abe | 3a7e513 | 2018-02-15 13:07:50 +0900 | [diff] [blame] | 161 | #endif // ARC_NETWORK_MANAGER_H_ |