blob: 6acf775fec58224e7fe3b97bd02b173e5fc44b1f [file] [log] [blame]
Garrick Evans08843932019-09-17 14:41:08 +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
Jason Jeremy Imanadffbcb2020-08-31 13:21:36 +09005#ifndef PATCHPANEL_DBUS_CLIENT_H_
6#define PATCHPANEL_DBUS_CLIENT_H_
Garrick Evans08843932019-09-17 14:41:08 +09007
8#include <memory>
Jason Jeremy Imanadffbcb2020-08-31 13:21:36 +09009#include <set>
Jason Jeremy Iman6f1f3e72020-07-06 13:04:03 +090010#include <string>
Garrick Evans08843932019-09-17 14:41:08 +090011#include <utility>
12#include <vector>
13
Hugo Benichicc6850f2020-01-17 13:26:06 +090014#include "base/files/scoped_file.h"
Garrick Evans08843932019-09-17 14:41:08 +090015#include <brillo/brillo_export.h>
16#include <dbus/bus.h>
17#include <dbus/object_proxy.h>
Hugo Benichi8135e562019-12-12 15:56:36 +090018#include <patchpanel/proto_bindings/patchpanel_service.pb.h>
Garrick Evans08843932019-09-17 14:41:08 +090019
20namespace patchpanel {
21
Hugo Benichi51ff71b2020-08-19 00:55:15 +090022// Simple wrapper around patchpanel DBus API. All public functions are blocking
Jie Jiang0a70acf2020-10-02 11:57:32 +090023// DBus calls to patchpaneld (asynchronous calls are mentioned explicitly). The
24// method names and protobuf schema used by patchpanel DBus API are defined in
25// platform2/system_api/dbus/patchpanel. Access control for clients is defined
26// in platform2/patchpanel/dbus.
Garrick Evans08843932019-09-17 14:41:08 +090027class BRILLO_EXPORT Client {
28 public:
Jie Jiang0a70acf2020-10-02 11:57:32 +090029 using GetTrafficCountersCallback =
30 base::OnceCallback<void(const std::vector<TrafficCounter>&)>;
Jie Jiang25c1b972020-11-12 15:42:53 +090031 using NeighborReachabilityEventHandler =
32 base::RepeatingCallback<void(const NeighborReachabilityEventSignal&)>;
Garrick Evansf04f0442020-12-01 12:36:44 +090033 using NetworkDeviceChangedSignalHandler =
34 base::RepeatingCallback<void(const NetworkDeviceChangedSignal&)>;
Jie Jiang0a70acf2020-10-02 11:57:32 +090035
Woody Chowdee3c8b2020-12-04 20:03:54 +090036 // This variation creates a dbus object internally
Garrick Evans08843932019-09-17 14:41:08 +090037 static std::unique_ptr<Client> New();
Woody Chowdee3c8b2020-12-04 20:03:54 +090038 static std::unique_ptr<Client> New(const scoped_refptr<dbus::Bus>& bus);
Garrick Evans08843932019-09-17 14:41:08 +090039
Jie Jiang81c84db2020-09-29 17:40:16 +090040 // Only used in tests.
41 static std::unique_ptr<Client> New(const scoped_refptr<dbus::Bus>& bus,
42 dbus::ObjectProxy* proxy);
Garrick Evans08843932019-09-17 14:41:08 +090043
Jie Jiang81c84db2020-09-29 17:40:16 +090044 virtual ~Client() = default;
Garrick Evans27b74032019-11-19 13:33:47 +090045
Garrick Evansd0a646e2020-11-25 21:08:32 +090046 virtual void RegisterOnAvailableCallback(
47 base::RepeatingCallback<void(bool)> callback) = 0;
48
Jie Jiang81c84db2020-09-29 17:40:16 +090049 virtual bool NotifyArcStartup(pid_t pid) = 0;
50 virtual bool NotifyArcShutdown() = 0;
Garrick Evans08843932019-09-17 14:41:08 +090051
Jie Jiang81c84db2020-09-29 17:40:16 +090052 virtual std::vector<NetworkDevice> NotifyArcVmStartup(uint32_t cid) = 0;
53 virtual bool NotifyArcVmShutdown(uint32_t cid) = 0;
Garrick Evans27b74032019-11-19 13:33:47 +090054
Jie Jiang81c84db2020-09-29 17:40:16 +090055 virtual bool NotifyTerminaVmStartup(uint32_t cid,
56 NetworkDevice* device,
57 IPv4Subnet* container_subnet) = 0;
58 virtual bool NotifyTerminaVmShutdown(uint32_t cid) = 0;
59
60 virtual bool NotifyPluginVmStartup(uint64_t vm_id,
61 int subnet_index,
62 NetworkDevice* device) = 0;
63 virtual bool NotifyPluginVmShutdown(uint64_t vm_id) = 0;
Garrick Evans376f0672020-01-07 15:31:50 +090064
Hugo Benichi7d9d8db2020-03-30 15:56:56 +090065 // Reset the VPN routing intent mark on a socket to the default policy for
66 // the current uid. This is in general incorrect to call this method for
67 // a socket that is already connected.
Jie Jiang81c84db2020-09-29 17:40:16 +090068 virtual bool DefaultVpnRouting(int socket) = 0;
Hugo Benichi7d9d8db2020-03-30 15:56:56 +090069
70 // Mark a socket to be always routed through a VPN if there is one.
71 // Must be called before the socket is connected.
Jie Jiang81c84db2020-09-29 17:40:16 +090072 virtual bool RouteOnVpn(int socket) = 0;
Hugo Benichi7d9d8db2020-03-30 15:56:56 +090073
74 // Mark a socket to be always routed through the physical network.
75 // Must be called before the socket is connected.
Jie Jiang81c84db2020-09-29 17:40:16 +090076 virtual bool BypassVpn(int socket) = 0;
Hugo Benichi7d9d8db2020-03-30 15:56:56 +090077
Hugo Benichicc6850f2020-01-17 13:26:06 +090078 // Sends a ConnectNamespaceRequest for the given namespace pid. Returns a
79 // pair with a valid ScopedFD and the ConnectNamespaceResponse proto message
80 // received if the request succeeded. Closing the ScopedFD will teardown the
81 // veth and routing setup and free the allocated IPv4 subnet.
Jie Jiang81c84db2020-09-29 17:40:16 +090082 virtual std::pair<base::ScopedFD, patchpanel::ConnectNamespaceResponse>
Hugo Benichicc6850f2020-01-17 13:26:06 +090083 ConnectNamespace(pid_t pid,
84 const std::string& outbound_ifname,
Garrick Evans58697022020-12-03 12:41:13 +090085 bool forward_user_traffic,
86 bool route_on_vpn,
87 TrafficCounter::Source traffic_source) = 0;
Hugo Benichicc6850f2020-01-17 13:26:06 +090088
Jie Jiang0a70acf2020-10-02 11:57:32 +090089 // Gets the traffic counters kept by patchpanel asynchronously, |callback|
90 // will be called with the counters once they are ready, or with an empty
91 // vector when an error happen. |devices| is the set of interfaces (shill
92 // devices) for which counters should be returned, any unknown interfaces will
93 // be ignored. If |devices| is empty, counters for all known interfaces will
94 // be returned.
Jie Jiang81c84db2020-09-29 17:40:16 +090095 virtual void GetTrafficCounters(const std::set<std::string>& devices,
96 GetTrafficCountersCallback callback) = 0;
Jie Jiange02d1202020-07-27 16:57:04 +090097
Jason Jeremy Iman6f1f3e72020-07-06 13:04:03 +090098 // Sends a ModifyPortRuleRequest to modify iptables ingress rules.
99 // This should only be called by permission_broker's 'devbroker'.
Jie Jiang81c84db2020-09-29 17:40:16 +0900100 virtual bool ModifyPortRule(patchpanel::ModifyPortRuleRequest::Operation op,
101 patchpanel::ModifyPortRuleRequest::RuleType type,
102 patchpanel::ModifyPortRuleRequest::Protocol proto,
103 const std::string& input_ifname,
104 const std::string& input_dst_ip,
105 uint32_t input_dst_port,
106 const std::string& dst_ip,
107 uint32_t dst_port) = 0;
Jason Jeremy Iman6f1f3e72020-07-06 13:04:03 +0900108
Garrick Evans9e637982020-11-30 11:59:27 +0900109 // Obtains a list of NetworkDevices currently managed by patchpanel.
110 virtual std::vector<NetworkDevice> GetDevices() = 0;
111
Garrick Evansf04f0442020-12-01 12:36:44 +0900112 // Registers a handler that will be called upon receiving a signal indicating
113 // that a network device managed by patchpanel was added or removed.
114 virtual void RegisterNetworkDeviceChangedSignalHandler(
115 NetworkDeviceChangedSignalHandler handler) = 0;
116
Jie Jiang25c1b972020-11-12 15:42:53 +0900117 // Registers a handler that will be called on receiving a neighbor
118 // reachability event. Currently these events are generated only for WiFi
Jie Jiange2e4c0b2020-09-16 18:48:43 +0900119 // devices. The handler is registered for as long as this patchpanel::Client
120 // instance is alive.
Jie Jiang25c1b972020-11-12 15:42:53 +0900121 virtual void RegisterNeighborReachabilityEventHandler(
122 NeighborReachabilityEventHandler handler) = 0;
Jie Jiange2e4c0b2020-09-16 18:48:43 +0900123
Jie Jiang81c84db2020-09-29 17:40:16 +0900124 protected:
125 Client() = default;
Garrick Evans08843932019-09-17 14:41:08 +0900126};
127
128} // namespace patchpanel
129
Jason Jeremy Imanadffbcb2020-08-31 13:21:36 +0900130#endif // PATCHPANEL_DBUS_CLIENT_H_