blob: ea279578495d39a2e8c2a6caa3c5a962a9887bb2 [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
Garrick Evans3388a032020-03-24 11:25:55 +09005#ifndef PATCHPANEL_CLIENT_H_
6#define PATCHPANEL_CLIENT_H_
Garrick Evans08843932019-09-17 14:41:08 +09007
8#include <memory>
Jason Jeremy Iman6f1f3e72020-07-06 13:04:03 +09009#include <string>
Garrick Evans08843932019-09-17 14:41:08 +090010#include <utility>
11#include <vector>
12
Hugo Benichicc6850f2020-01-17 13:26:06 +090013#include "base/files/scoped_file.h"
Garrick Evans08843932019-09-17 14:41:08 +090014#include <brillo/brillo_export.h>
15#include <dbus/bus.h>
16#include <dbus/object_proxy.h>
Hugo Benichi8135e562019-12-12 15:56:36 +090017#include <patchpanel/proto_bindings/patchpanel_service.pb.h>
Garrick Evans08843932019-09-17 14:41:08 +090018
19namespace patchpanel {
20
Hugo Benichi7d9d8db2020-03-30 15:56:56 +090021// Simple wrapper around patchpanel DBus API. All public functions are
22// blocking DBus calls to patchpaneld.
Garrick Evans08843932019-09-17 14:41:08 +090023class BRILLO_EXPORT Client {
24 public:
25 static std::unique_ptr<Client> New();
26
Garrick Evans93a83fc2020-03-31 15:16:55 +090027 Client(const scoped_refptr<dbus::Bus>& bus, dbus::ObjectProxy* proxy)
Garrick Evans08843932019-09-17 14:41:08 +090028 : bus_(std::move(bus)), proxy_(proxy) {}
Garrick Evans93a83fc2020-03-31 15:16:55 +090029 ~Client();
Garrick Evans08843932019-09-17 14:41:08 +090030
31 bool NotifyArcStartup(pid_t pid);
Garrick Evansca2b41b2019-12-02 09:06:11 +090032 bool NotifyArcShutdown();
Garrick Evans27b74032019-11-19 13:33:47 +090033
Garrick Evans3388a032020-03-24 11:25:55 +090034 std::vector<NetworkDevice> NotifyArcVmStartup(uint32_t cid);
Garrick Evans0a189372020-02-07 08:55:27 +090035 bool NotifyArcVmShutdown(uint32_t cid);
Garrick Evans08843932019-09-17 14:41:08 +090036
Garrick Evans0a189372020-02-07 08:55:27 +090037 bool NotifyTerminaVmStartup(uint32_t cid,
Garrick Evans3388a032020-03-24 11:25:55 +090038 NetworkDevice* device,
39 IPv4Subnet* container_subnet);
Garrick Evans0a189372020-02-07 08:55:27 +090040 bool NotifyTerminaVmShutdown(uint32_t cid);
Garrick Evans27b74032019-11-19 13:33:47 +090041
Garrick Evans376f0672020-01-07 15:31:50 +090042 bool NotifyPluginVmStartup(uint64_t vm_id,
43 int subnet_index,
Garrick Evans3388a032020-03-24 11:25:55 +090044 NetworkDevice* device);
Garrick Evans376f0672020-01-07 15:31:50 +090045 bool NotifyPluginVmShutdown(uint64_t vm_id);
46
Hugo Benichi7d9d8db2020-03-30 15:56:56 +090047 // Reset the VPN routing intent mark on a socket to the default policy for
48 // the current uid. This is in general incorrect to call this method for
49 // a socket that is already connected.
50 bool DefaultVpnRouting(int socket);
51
52 // Mark a socket to be always routed through a VPN if there is one.
53 // Must be called before the socket is connected.
54 bool RouteOnVpn(int socket);
55
56 // Mark a socket to be always routed through the physical network.
57 // Must be called before the socket is connected.
58 bool BypassVpn(int socket);
59
Hugo Benichicc6850f2020-01-17 13:26:06 +090060 // Sends a ConnectNamespaceRequest for the given namespace pid. Returns a
61 // pair with a valid ScopedFD and the ConnectNamespaceResponse proto message
62 // received if the request succeeded. Closing the ScopedFD will teardown the
63 // veth and routing setup and free the allocated IPv4 subnet.
64 std::pair<base::ScopedFD, patchpanel::ConnectNamespaceResponse>
65 ConnectNamespace(pid_t pid,
66 const std::string& outbound_ifname,
67 bool forward_user_traffic);
68
Jie Jiange02d1202020-07-27 16:57:04 +090069 // Gets the traffic counters kept by patchpanel. |devices| is the set of
70 // interfaces (shill devices) for which counters should be returned, any
71 // unknown interfaces will be ignored. If |devices| is empty, counters for all
72 // known interfaces will be returned.
73 std::vector<TrafficCounter> GetTrafficCounters(
74 const std::set<std::string>& devices);
75
Jason Jeremy Iman6f1f3e72020-07-06 13:04:03 +090076 // Sends a ModifyPortRuleRequest to modify iptables ingress rules.
77 // This should only be called by permission_broker's 'devbroker'.
78 bool ModifyPortRule(patchpanel::ModifyPortRuleRequest::Operation op,
79 patchpanel::ModifyPortRuleRequest::RuleType type,
80 patchpanel::ModifyPortRuleRequest::Protocol proto,
81 const std::string& input_ifname,
82 const std::string& input_dst_ip,
83 uint32_t input_dst_port,
84 const std::string& dst_ip,
85 uint32_t dst_port);
86
Garrick Evans08843932019-09-17 14:41:08 +090087 private:
88 scoped_refptr<dbus::Bus> bus_;
89 dbus::ObjectProxy* proxy_ = nullptr; // owned by bus_
90
Hugo Benichi7d9d8db2020-03-30 15:56:56 +090091 bool SendSetVpnIntentRequest(int socket,
92 SetVpnIntentRequest::VpnRoutingPolicy policy);
93
Garrick Evans08843932019-09-17 14:41:08 +090094 DISALLOW_COPY_AND_ASSIGN(Client);
95};
96
97} // namespace patchpanel
98
Garrick Evans3388a032020-03-24 11:25:55 +090099#endif // PATCHPANEL_CLIENT_H_