blob: ab5f26a4f7a583141fcf2b9de71093e813dd32d0 [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 Jiange2e4c0b2020-09-16 18:48:43 +090031 using NeighborConnectedStateChangedHandler =
32 base::RepeatingCallback<void(const NeighborConnectedStateChangedSignal&)>;
Jie Jiang0a70acf2020-10-02 11:57:32 +090033
Garrick Evans08843932019-09-17 14:41:08 +090034 static std::unique_ptr<Client> New();
35
Garrick Evans93a83fc2020-03-31 15:16:55 +090036 Client(const scoped_refptr<dbus::Bus>& bus, dbus::ObjectProxy* proxy)
Garrick Evans08843932019-09-17 14:41:08 +090037 : bus_(std::move(bus)), proxy_(proxy) {}
Garrick Evans93a83fc2020-03-31 15:16:55 +090038 ~Client();
Garrick Evans08843932019-09-17 14:41:08 +090039
40 bool NotifyArcStartup(pid_t pid);
Garrick Evansca2b41b2019-12-02 09:06:11 +090041 bool NotifyArcShutdown();
Garrick Evans27b74032019-11-19 13:33:47 +090042
Garrick Evans3388a032020-03-24 11:25:55 +090043 std::vector<NetworkDevice> NotifyArcVmStartup(uint32_t cid);
Garrick Evans0a189372020-02-07 08:55:27 +090044 bool NotifyArcVmShutdown(uint32_t cid);
Garrick Evans08843932019-09-17 14:41:08 +090045
Garrick Evans0a189372020-02-07 08:55:27 +090046 bool NotifyTerminaVmStartup(uint32_t cid,
Garrick Evans3388a032020-03-24 11:25:55 +090047 NetworkDevice* device,
48 IPv4Subnet* container_subnet);
Garrick Evans0a189372020-02-07 08:55:27 +090049 bool NotifyTerminaVmShutdown(uint32_t cid);
Garrick Evans27b74032019-11-19 13:33:47 +090050
Garrick Evans376f0672020-01-07 15:31:50 +090051 bool NotifyPluginVmStartup(uint64_t vm_id,
52 int subnet_index,
Garrick Evans3388a032020-03-24 11:25:55 +090053 NetworkDevice* device);
Garrick Evans376f0672020-01-07 15:31:50 +090054 bool NotifyPluginVmShutdown(uint64_t vm_id);
55
Hugo Benichi7d9d8db2020-03-30 15:56:56 +090056 // Reset the VPN routing intent mark on a socket to the default policy for
57 // the current uid. This is in general incorrect to call this method for
58 // a socket that is already connected.
59 bool DefaultVpnRouting(int socket);
60
61 // Mark a socket to be always routed through a VPN if there is one.
62 // Must be called before the socket is connected.
63 bool RouteOnVpn(int socket);
64
65 // Mark a socket to be always routed through the physical network.
66 // Must be called before the socket is connected.
67 bool BypassVpn(int socket);
68
Hugo Benichicc6850f2020-01-17 13:26:06 +090069 // Sends a ConnectNamespaceRequest for the given namespace pid. Returns a
70 // pair with a valid ScopedFD and the ConnectNamespaceResponse proto message
71 // received if the request succeeded. Closing the ScopedFD will teardown the
72 // veth and routing setup and free the allocated IPv4 subnet.
73 std::pair<base::ScopedFD, patchpanel::ConnectNamespaceResponse>
74 ConnectNamespace(pid_t pid,
75 const std::string& outbound_ifname,
76 bool forward_user_traffic);
77
Jie Jiang0a70acf2020-10-02 11:57:32 +090078 // Gets the traffic counters kept by patchpanel asynchronously, |callback|
79 // will be called with the counters once they are ready, or with an empty
80 // vector when an error happen. |devices| is the set of interfaces (shill
81 // devices) for which counters should be returned, any unknown interfaces will
82 // be ignored. If |devices| is empty, counters for all known interfaces will
83 // be returned.
84 void GetTrafficCounters(const std::set<std::string>& devices,
85 GetTrafficCountersCallback callback);
Jie Jiange02d1202020-07-27 16:57:04 +090086
Jason Jeremy Iman6f1f3e72020-07-06 13:04:03 +090087 // Sends a ModifyPortRuleRequest to modify iptables ingress rules.
88 // This should only be called by permission_broker's 'devbroker'.
89 bool ModifyPortRule(patchpanel::ModifyPortRuleRequest::Operation op,
90 patchpanel::ModifyPortRuleRequest::RuleType type,
91 patchpanel::ModifyPortRuleRequest::Protocol proto,
92 const std::string& input_ifname,
93 const std::string& input_dst_ip,
94 uint32_t input_dst_port,
95 const std::string& dst_ip,
96 uint32_t dst_port);
97
Jie Jiange2e4c0b2020-09-16 18:48:43 +090098 // Registers a handler that will be called on receiving a signal of neighbor
99 // connected state changed. Currently these events are generated only for WiFi
100 // devices. The handler is registered for as long as this patchpanel::Client
101 // instance is alive.
102 void RegisterNeighborConnectedStateChangedHandler(
103 NeighborConnectedStateChangedHandler handler);
104
Garrick Evans08843932019-09-17 14:41:08 +0900105 private:
106 scoped_refptr<dbus::Bus> bus_;
107 dbus::ObjectProxy* proxy_ = nullptr; // owned by bus_
108
Hugo Benichi7d9d8db2020-03-30 15:56:56 +0900109 bool SendSetVpnIntentRequest(int socket,
110 SetVpnIntentRequest::VpnRoutingPolicy policy);
111
Garrick Evans08843932019-09-17 14:41:08 +0900112 DISALLOW_COPY_AND_ASSIGN(Client);
113};
114
115} // namespace patchpanel
116
Jason Jeremy Imanadffbcb2020-08-31 13:21:36 +0900117#endif // PATCHPANEL_DBUS_CLIENT_H_