Garrick Evans | 47c1927 | 2019-11-21 10:58:21 +0900 | [diff] [blame] | 1 | // Copyright 2019 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_CROSTINI_SERVICE_H_ |
| 6 | #define PATCHPANEL_CROSTINI_SERVICE_H_ |
Garrick Evans | 47c1927 | 2019-11-21 10:58:21 +0900 | [diff] [blame] | 7 | |
Garrick Evans | b1c9371 | 2020-01-22 09:28:25 +0900 | [diff] [blame] | 8 | #include <map> |
| 9 | #include <memory> |
Garrick Evans | 47c1927 | 2019-11-21 10:58:21 +0900 | [diff] [blame] | 10 | #include <string> |
| 11 | |
| 12 | #include <base/memory/weak_ptr.h> |
Jason Jeremy Iman | fa8b6d2 | 2020-02-20 03:44:21 +0000 | [diff] [blame] | 13 | #include <permission_broker/dbus-proxies.h> |
Garrick Evans | 47c1927 | 2019-11-21 10:58:21 +0900 | [diff] [blame] | 14 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame^] | 15 | #include "patchpanel/address_manager.h" |
| 16 | #include "patchpanel/datapath.h" |
| 17 | #include "patchpanel/device.h" |
| 18 | #include "patchpanel/shill_client.h" |
| 19 | #include "patchpanel/traffic_forwarder.h" |
Garrick Evans | 47c1927 | 2019-11-21 10:58:21 +0900 | [diff] [blame] | 20 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame^] | 21 | namespace patchpanel { |
Garrick Evans | 47c1927 | 2019-11-21 10:58:21 +0900 | [diff] [blame] | 22 | |
Garrick Evans | b1c9371 | 2020-01-22 09:28:25 +0900 | [diff] [blame] | 23 | // Crostini networking service handling address allocation and TAP device |
Garrick Evans | 51d5b55 | 2020-01-30 10:42:06 +0900 | [diff] [blame] | 24 | // management for Crostini VMs. |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 25 | class CrostiniService { |
Garrick Evans | 47c1927 | 2019-11-21 10:58:21 +0900 | [diff] [blame] | 26 | public: |
Garrick Evans | 69b8587 | 2020-02-04 11:40:26 +0900 | [diff] [blame] | 27 | // All pointers are required and must not be null, and are owned by the |
| 28 | // caller. |
| 29 | CrostiniService(ShillClient* shill_client, |
Garrick Evans | 1b1f67c | 2020-02-04 16:21:25 +0900 | [diff] [blame] | 30 | AddressManager* addr_mgr, |
| 31 | Datapath* datapath, |
| 32 | TrafficForwarder* forwarder); |
Garrick Evans | c1ac5c4 | 2020-03-31 15:31:22 +0900 | [diff] [blame] | 33 | ~CrostiniService(); |
Garrick Evans | 47c1927 | 2019-11-21 10:58:21 +0900 | [diff] [blame] | 34 | |
Garrick Evans | 51d5b55 | 2020-01-30 10:42:06 +0900 | [diff] [blame] | 35 | bool Start(uint64_t vm_id, bool is_termina, int subnet_index); |
| 36 | void Stop(uint64_t vm_id, bool is_termina); |
Garrick Evans | 47c1927 | 2019-11-21 10:58:21 +0900 | [diff] [blame] | 37 | |
Garrick Evans | 51d5b55 | 2020-01-30 10:42:06 +0900 | [diff] [blame] | 38 | const Device* const TAP(uint64_t vm_id, bool is_termina) const; |
Garrick Evans | 47c1927 | 2019-11-21 10:58:21 +0900 | [diff] [blame] | 39 | |
| 40 | private: |
Garrick Evans | 51d5b55 | 2020-01-30 10:42:06 +0900 | [diff] [blame] | 41 | std::unique_ptr<Device> AddTAP(bool is_termina, int subnet_index); |
Garrick Evans | 1b1f67c | 2020-02-04 16:21:25 +0900 | [diff] [blame] | 42 | void OnDefaultInterfaceChanged(const std::string& new_ifname, |
| 43 | const std::string& prev_ifname); |
| 44 | void StartForwarding(const std::string& phys_ifname, |
Jason Jeremy Iman | 0e9f826 | 2020-03-06 14:50:49 +0900 | [diff] [blame] | 45 | const std::string& virt_ifname); |
Garrick Evans | 1b1f67c | 2020-02-04 16:21:25 +0900 | [diff] [blame] | 46 | void StopForwarding(const std::string& phys_ifname, |
| 47 | const std::string& virt_ifname); |
Garrick Evans | b1c9371 | 2020-01-22 09:28:25 +0900 | [diff] [blame] | 48 | |
Jason Jeremy Iman | fa8b6d2 | 2020-02-20 03:44:21 +0000 | [diff] [blame] | 49 | bool SetupFirewallClient(); |
| 50 | |
| 51 | // Checks ADB sideloading status and set it to |adb_sideloading_enabled_|. |
| 52 | // This function will call itself again if ADB sideloading status is not |
| 53 | // known yet. Otherwise, it will process all currently running Crostini VMs. |
| 54 | void CheckAdbSideloadingStatus(); |
| 55 | |
| 56 | // Start and stop ADB traffic forwarding from Crostini's TAP device |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame^] | 57 | // patchpanel's adb-proxy. |ifname| is the Crostini's TAP interface that |
Jason Jeremy Iman | fa8b6d2 | 2020-02-20 03:44:21 +0000 | [diff] [blame] | 58 | // will be forwarded. These methods call permission broker DBUS APIs to port |
| 59 | // forward and accept traffic. |
| 60 | void StartAdbPortForwarding(const std::string& ifname); |
| 61 | void StopAdbPortForwarding(const std::string& ifname); |
| 62 | |
Garrick Evans | 69b8587 | 2020-02-04 11:40:26 +0900 | [diff] [blame] | 63 | ShillClient* shill_client_; |
Garrick Evans | 1b1f67c | 2020-02-04 16:21:25 +0900 | [diff] [blame] | 64 | AddressManager* addr_mgr_; |
Garrick Evans | 47c1927 | 2019-11-21 10:58:21 +0900 | [diff] [blame] | 65 | Datapath* datapath_; |
Garrick Evans | 1b1f67c | 2020-02-04 16:21:25 +0900 | [diff] [blame] | 66 | TrafficForwarder* forwarder_; |
| 67 | |
Garrick Evans | 51d5b55 | 2020-01-30 10:42:06 +0900 | [diff] [blame] | 68 | // Mapping of VM IDs to TAP devices |
| 69 | std::map<std::string, std::unique_ptr<Device>> taps_; |
Garrick Evans | 47c1927 | 2019-11-21 10:58:21 +0900 | [diff] [blame] | 70 | |
Jason Jeremy Iman | fa8b6d2 | 2020-02-20 03:44:21 +0000 | [diff] [blame] | 71 | bool adb_sideloading_enabled_; |
| 72 | scoped_refptr<dbus::Bus> bus_; |
| 73 | std::unique_ptr<org::chromium::PermissionBrokerProxy> |
| 74 | permission_broker_proxy_; |
| 75 | |
| 76 | // Mapping from Crostini's TAP interface to lifeline write file descriptor. |
| 77 | // The file descriptor is the write end of the pipe used for communicating |
| 78 | // with remote firewall server (permission_broker), where the remote firewall |
| 79 | // server will use the read end of the pipe to detect when this process exits |
| 80 | // or close the write end of the pipe. |
| 81 | std::map<const std::string, base::ScopedFD> lifeline_fds_; |
| 82 | |
Garrick Evans | 47c1927 | 2019-11-21 10:58:21 +0900 | [diff] [blame] | 83 | base::WeakPtrFactory<CrostiniService> weak_factory_{this}; |
| 84 | DISALLOW_COPY_AND_ASSIGN(CrostiniService); |
| 85 | }; |
| 86 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame^] | 87 | } // namespace patchpanel |
Garrick Evans | 47c1927 | 2019-11-21 10:58:21 +0900 | [diff] [blame] | 88 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame^] | 89 | #endif // PATCHPANEL_CROSTINI_SERVICE_H_ |