blob: 5f4dfad64b8a9d6d3ad2d85f15e9256d2fbcb60e [file] [log] [blame]
Garrick Evans47c19272019-11-21 10:58:21 +09001// 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 Evans3388a032020-03-24 11:25:55 +09005#ifndef PATCHPANEL_CROSTINI_SERVICE_H_
6#define PATCHPANEL_CROSTINI_SERVICE_H_
Garrick Evans47c19272019-11-21 10:58:21 +09007
Garrick Evansb1c93712020-01-22 09:28:25 +09008#include <map>
9#include <memory>
Garrick Evans47c19272019-11-21 10:58:21 +090010#include <string>
11
12#include <base/memory/weak_ptr.h>
Jason Jeremy Imanfa8b6d22020-02-20 03:44:21 +000013#include <permission_broker/dbus-proxies.h>
Garrick Evans47c19272019-11-21 10:58:21 +090014
Garrick Evans3388a032020-03-24 11:25:55 +090015#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 Evans47c19272019-11-21 10:58:21 +090020
Garrick Evans3388a032020-03-24 11:25:55 +090021namespace patchpanel {
Garrick Evans47c19272019-11-21 10:58:21 +090022
Garrick Evansb1c93712020-01-22 09:28:25 +090023// Crostini networking service handling address allocation and TAP device
Garrick Evans51d5b552020-01-30 10:42:06 +090024// management for Crostini VMs.
Garrick Evansf29f5a32019-12-06 11:34:25 +090025class CrostiniService {
Garrick Evans47c19272019-11-21 10:58:21 +090026 public:
Garrick Evans69b85872020-02-04 11:40:26 +090027 // All pointers are required and must not be null, and are owned by the
28 // caller.
29 CrostiniService(ShillClient* shill_client,
Garrick Evans1b1f67c2020-02-04 16:21:25 +090030 AddressManager* addr_mgr,
31 Datapath* datapath,
32 TrafficForwarder* forwarder);
Garrick Evansc1ac5c42020-03-31 15:31:22 +090033 ~CrostiniService();
Garrick Evans47c19272019-11-21 10:58:21 +090034
Garrick Evans51d5b552020-01-30 10:42:06 +090035 bool Start(uint64_t vm_id, bool is_termina, int subnet_index);
36 void Stop(uint64_t vm_id, bool is_termina);
Garrick Evans47c19272019-11-21 10:58:21 +090037
Garrick Evans51d5b552020-01-30 10:42:06 +090038 const Device* const TAP(uint64_t vm_id, bool is_termina) const;
Garrick Evans47c19272019-11-21 10:58:21 +090039
40 private:
Garrick Evans51d5b552020-01-30 10:42:06 +090041 std::unique_ptr<Device> AddTAP(bool is_termina, int subnet_index);
Garrick Evans1b1f67c2020-02-04 16:21:25 +090042 void OnDefaultInterfaceChanged(const std::string& new_ifname,
43 const std::string& prev_ifname);
44 void StartForwarding(const std::string& phys_ifname,
Jason Jeremy Iman0e9f8262020-03-06 14:50:49 +090045 const std::string& virt_ifname);
Garrick Evans1b1f67c2020-02-04 16:21:25 +090046 void StopForwarding(const std::string& phys_ifname,
47 const std::string& virt_ifname);
Garrick Evansb1c93712020-01-22 09:28:25 +090048
Jason Jeremy Imanfa8b6d22020-02-20 03:44:21 +000049 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 Evans3388a032020-03-24 11:25:55 +090057 // patchpanel's adb-proxy. |ifname| is the Crostini's TAP interface that
Jason Jeremy Imanfa8b6d22020-02-20 03:44:21 +000058 // 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 Evans69b85872020-02-04 11:40:26 +090063 ShillClient* shill_client_;
Garrick Evans1b1f67c2020-02-04 16:21:25 +090064 AddressManager* addr_mgr_;
Garrick Evans47c19272019-11-21 10:58:21 +090065 Datapath* datapath_;
Garrick Evans1b1f67c2020-02-04 16:21:25 +090066 TrafficForwarder* forwarder_;
67
Garrick Evans51d5b552020-01-30 10:42:06 +090068 // Mapping of VM IDs to TAP devices
69 std::map<std::string, std::unique_ptr<Device>> taps_;
Garrick Evans47c19272019-11-21 10:58:21 +090070
Jason Jeremy Imanfa8b6d22020-02-20 03:44:21 +000071 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 Evans47c19272019-11-21 10:58:21 +090083 base::WeakPtrFactory<CrostiniService> weak_factory_{this};
84 DISALLOW_COPY_AND_ASSIGN(CrostiniService);
85};
86
Garrick Evans3388a032020-03-24 11:25:55 +090087} // namespace patchpanel
Garrick Evans47c19272019-11-21 10:58:21 +090088
Garrick Evans3388a032020-03-24 11:25:55 +090089#endif // PATCHPANEL_CROSTINI_SERVICE_H_