blob: b833fef8aae3f73a2dbcc6a4381b2a7f02352f00 [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
Hidehiko Abe3a7e5132018-02-15 13:07:50 +09005#ifndef ARC_NETWORK_MANAGER_H_
6#define ARC_NETWORK_MANAGER_H_
Kevin Cernekee95d4ae92016-06-19 10:26:29 -07007
Garrick Evans49879532018-12-03 13:15:36 +09008#include <map>
Kevin Cernekee95d4ae92016-06-19 10:26:29 -07009#include <memory>
Garrick Evans49879532018-12-03 13:15:36 +090010#include <set>
Kevin Cernekee95d4ae92016-06-19 10:26:29 -070011#include <string>
Jason Jeremy Imanf4156cb2019-11-14 15:36:22 +090012#include <vector>
Kevin Cernekee95d4ae92016-06-19 10:26:29 -070013
14#include <base/memory/weak_ptr.h>
15#include <brillo/daemons/dbus_daemon.h>
Kevin Cernekee27bcaa62016-12-03 11:16:26 -080016#include <brillo/process_reaper.h>
Garrick Evans08843932019-09-17 14:41:08 +090017#include <chromeos/dbus/service_constants.h>
Hugo Benichi8135e562019-12-12 15:56:36 +090018#include <patchpanel/proto_bindings/patchpanel_service.pb.h>
Kevin Cernekee95d4ae92016-06-19 10:26:29 -070019
Garrick Evansf4a93292019-03-13 14:19:43 +090020#include "arc/network/address_manager.h"
Garrick Evans5d55f5e2019-07-17 15:28:10 +090021#include "arc/network/arc_service.h"
Garrick Evans47c19272019-11-21 10:58:21 +090022#include "arc/network/crostini_service.h"
Hidehiko Abe3a7e5132018-02-15 13:07:50 +090023#include "arc/network/helper_process.h"
Hugo Benichi7d9d8db2020-03-30 15:56:56 +090024#include "arc/network/routing_service.h"
Hidehiko Abe3a7e5132018-02-15 13:07:50 +090025#include "arc/network/shill_client.h"
Long Chengd4415582019-09-24 19:16:09 +000026#include "arc/network/socket.h"
Garrick Evans6e4eb3b2020-03-09 07:18:31 +090027#include "arc/network/traffic_forwarder.h"
Kevin Cernekee95d4ae92016-06-19 10:26:29 -070028
29namespace arc_networkd {
30
31// Main class that runs the mainloop and responds to LAN interface changes.
Garrick Evans4ac09852020-01-16 14:09:22 +090032class Manager final : public brillo::DBusDaemon, private TrafficForwarder {
Kevin Cernekee95d4ae92016-06-19 10:26:29 -070033 public:
Taoyu Lice7caa62019-10-01 15:43:33 +090034 Manager(std::unique_ptr<HelperProcess> adb_proxy,
Jason Jeremy Imand89b5f52019-10-24 10:39:17 +090035 std::unique_ptr<HelperProcess> mcast_proxy,
Garrick Evans1f5a3612019-11-08 12:59:03 +090036 std::unique_ptr<HelperProcess> nd_proxy);
Garrick Evans207e7482019-12-16 11:54:36 +090037 ~Manager();
Kevin Cernekee95d4ae92016-06-19 10:26:29 -070038
Garrick Evans4ac09852020-01-16 14:09:22 +090039 // TrafficForwarder methods.
40
41 void StartForwarding(const std::string& ifname_physical,
42 const std::string& ifname_virtual,
Garrick Evans4ac09852020-01-16 14:09:22 +090043 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 Imanf4156cb2019-11-14 15:36:22 +090051 // 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 Cernekee95d4ae92016-06-19 10:26:29 -070061 protected:
62 int OnInit() override;
63
64 private:
Garrick Evans49879532018-12-03 13:15:36 +090065 void InitialSetup();
Kevin Cernekee95d4ae92016-06-19 10:26:29 -070066
Garrick Evanse94a14e2019-11-11 10:32:13 +090067 bool StartArc(pid_t pid);
Garrick Evans21173b12019-11-20 15:23:16 +090068 void StopArc(pid_t pid);
Garrick Evans015b0d62020-02-07 09:06:38 +090069 bool StartArcVm(uint32_t cid);
70 void StopArcVm(uint32_t cid);
Garrick Evans51d5b552020-01-30 10:42:06 +090071 bool StartCrosVm(uint64_t vm_id,
72 GuestMessage::GuestType vm_type,
Garrick Evans53a2a982020-02-05 10:53:35 +090073 uint32_t subnet_index = kAnySubnetIndex);
Garrick Evans51d5b552020-01-30 10:42:06 +090074 void StopCrosVm(uint64_t vm_id, GuestMessage::GuestType vm_type);
Garrick Evanse94a14e2019-11-11 10:32:13 +090075
Kevin Cernekee27bcaa62016-12-03 11:16:26 -080076 // Callback from ProcessReaper to notify Manager that one of the
77 // subprocesses died.
78 void OnSubprocessExited(pid_t pid, const siginfo_t& info);
Garrick Evans4c042572019-12-17 13:42:25 +090079 void RestartSubprocess(HelperProcess* subproc);
Kevin Cernekee27bcaa62016-12-03 11:16:26 -080080
Garrick Evans664a82f2019-12-17 12:18:05 +090081 // Callback from Daemon to notify that SIGTERM or SIGINT was received and
Kevin Cernekee27bcaa62016-12-03 11:16:26 -080082 // the daemon should clean up in preparation to exit.
83 void OnShutdown(int* exit_code) override;
84
Garrick Evans4ac09852020-01-16 14:09:22 +090085 // Callback from NDProxy telling us to add a new IPv6 route.
86 void OnDeviceMessageFromNDProxy(const DeviceMessage& msg);
87
Garrick Evans08843932019-09-17 14:41:08 +090088 // 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 Evans47c19272019-11-21 10:58:21 +0900101 // 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 Evans51d5b552020-01-30 10:42:06 +0900109 // 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 Benichi7d9d8db2020-03-30 15:56:56 +0900117 // Handles DBus requests for setting a VPN intent fwmark on a socket.
118 std::unique_ptr<dbus::Response> OnSetVpnIntent(dbus::MethodCall* method_call);
119
Garrick Evanse94a14e2019-11-11 10:32:13 +0900120 // Dispatch |msg| to child processes.
121 void SendGuestMessage(const GuestMessage& msg);
Garrick Evans96e03042019-05-28 14:30:52 +0900122
Hugo Benichi935eca92018-07-03 13:47:24 +0900123 friend std::ostream& operator<<(std::ostream& stream, const Manager& manager);
124
Garrick Evans69b85872020-02-04 11:40:26 +0900125 std::unique_ptr<ShillClient> shill_client_;
Hugo Benichi7d9d8db2020-03-30 15:56:56 +0900126 std::unique_ptr<RoutingService> routing_svc_;
Garrick Evans69b85872020-02-04 11:40:26 +0900127
Garrick Evans5d55f5e2019-07-17 15:28:10 +0900128 // Guest services.
129 std::unique_ptr<ArcService> arc_svc_;
Garrick Evans47c19272019-11-21 10:58:21 +0900130 std::unique_ptr<CrostiniService> cros_svc_;
Garrick Evans5d55f5e2019-07-17 15:28:10 +0900131
Garrick Evans08843932019-09-17 14:41:08 +0900132 // DBus service.
133 dbus::ExportedObject* dbus_svc_path_; // Owned by |bus_|.
134
Garrick Evans5d55f5e2019-07-17 15:28:10 +0900135 // Other services.
Garrick Evans3915af32019-07-25 15:44:34 +0900136 brillo::ProcessReaper process_reaper_;
Garrick Evans96e03042019-05-28 14:30:52 +0900137 std::unique_ptr<HelperProcess> adb_proxy_;
Jason Jeremy Imand89b5f52019-10-24 10:39:17 +0900138 std::unique_ptr<HelperProcess> mcast_proxy_;
Taoyu Lice7caa62019-10-01 15:43:33 +0900139 std::unique_ptr<HelperProcess> nd_proxy_;
Garrick Evans96e03042019-05-28 14:30:52 +0900140
Garrick Evansf4a93292019-03-13 14:19:43 +0900141 AddressManager addr_mgr_;
Garrick Evansf4a93292019-03-13 14:19:43 +0900142
Jason Jeremy Imanf4156cb2019-11-14 15:36:22 +0900143 // |cached_feature_enabled| stores the cached result of if a feature should be
144 // enabled.
145 static std::map<const std::string, bool> cached_feature_enabled_;
146
Taoyu Li179dcc62019-10-17 11:21:08 +0900147 std::unique_ptr<MinijailedProcessRunner> runner_;
148 std::unique_ptr<Datapath> datapath_;
149
Kevin Cernekee95d4ae92016-06-19 10:26:29 -0700150 base::WeakPtrFactory<Manager> weak_factory_{this};
Garrick Evansf4a93292019-03-13 14:19:43 +0900151 DISALLOW_COPY_AND_ASSIGN(Manager);
Kevin Cernekee95d4ae92016-06-19 10:26:29 -0700152};
153
154} // namespace arc_networkd
155
Hidehiko Abe3a7e5132018-02-15 13:07:50 +0900156#endif // ARC_NETWORK_MANAGER_H_