blob: e8403c226e0b4590d4140e2ba7374a4b217bda2d [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>
9#include <utility>
10#include <vector>
11
Hugo Benichicc6850f2020-01-17 13:26:06 +090012#include "base/files/scoped_file.h"
Garrick Evans08843932019-09-17 14:41:08 +090013#include <brillo/brillo_export.h>
14#include <dbus/bus.h>
15#include <dbus/object_proxy.h>
Hugo Benichi8135e562019-12-12 15:56:36 +090016#include <patchpanel/proto_bindings/patchpanel_service.pb.h>
Garrick Evans08843932019-09-17 14:41:08 +090017
18namespace patchpanel {
19
Hugo Benichi7d9d8db2020-03-30 15:56:56 +090020// Simple wrapper around patchpanel DBus API. All public functions are
21// blocking DBus calls to patchpaneld.
Garrick Evans08843932019-09-17 14:41:08 +090022class BRILLO_EXPORT Client {
23 public:
24 static std::unique_ptr<Client> New();
25
Garrick Evans93a83fc2020-03-31 15:16:55 +090026 Client(const scoped_refptr<dbus::Bus>& bus, dbus::ObjectProxy* proxy)
Garrick Evans08843932019-09-17 14:41:08 +090027 : bus_(std::move(bus)), proxy_(proxy) {}
Garrick Evans93a83fc2020-03-31 15:16:55 +090028 ~Client();
Garrick Evans08843932019-09-17 14:41:08 +090029
30 bool NotifyArcStartup(pid_t pid);
Garrick Evansca2b41b2019-12-02 09:06:11 +090031 bool NotifyArcShutdown();
Garrick Evans27b74032019-11-19 13:33:47 +090032
Garrick Evans3388a032020-03-24 11:25:55 +090033 std::vector<NetworkDevice> NotifyArcVmStartup(uint32_t cid);
Garrick Evans0a189372020-02-07 08:55:27 +090034 bool NotifyArcVmShutdown(uint32_t cid);
Garrick Evans08843932019-09-17 14:41:08 +090035
Garrick Evans0a189372020-02-07 08:55:27 +090036 bool NotifyTerminaVmStartup(uint32_t cid,
Garrick Evans3388a032020-03-24 11:25:55 +090037 NetworkDevice* device,
38 IPv4Subnet* container_subnet);
Garrick Evans0a189372020-02-07 08:55:27 +090039 bool NotifyTerminaVmShutdown(uint32_t cid);
Garrick Evans27b74032019-11-19 13:33:47 +090040
Garrick Evans376f0672020-01-07 15:31:50 +090041 bool NotifyPluginVmStartup(uint64_t vm_id,
42 int subnet_index,
Garrick Evans3388a032020-03-24 11:25:55 +090043 NetworkDevice* device);
Garrick Evans376f0672020-01-07 15:31:50 +090044 bool NotifyPluginVmShutdown(uint64_t vm_id);
45
Hugo Benichi7d9d8db2020-03-30 15:56:56 +090046 // Reset the VPN routing intent mark on a socket to the default policy for
47 // the current uid. This is in general incorrect to call this method for
48 // a socket that is already connected.
49 bool DefaultVpnRouting(int socket);
50
51 // Mark a socket to be always routed through a VPN if there is one.
52 // Must be called before the socket is connected.
53 bool RouteOnVpn(int socket);
54
55 // Mark a socket to be always routed through the physical network.
56 // Must be called before the socket is connected.
57 bool BypassVpn(int socket);
58
Hugo Benichicc6850f2020-01-17 13:26:06 +090059 // Sends a ConnectNamespaceRequest for the given namespace pid. Returns a
60 // pair with a valid ScopedFD and the ConnectNamespaceResponse proto message
61 // received if the request succeeded. Closing the ScopedFD will teardown the
62 // veth and routing setup and free the allocated IPv4 subnet.
63 std::pair<base::ScopedFD, patchpanel::ConnectNamespaceResponse>
64 ConnectNamespace(pid_t pid,
65 const std::string& outbound_ifname,
66 bool forward_user_traffic);
67
Garrick Evans08843932019-09-17 14:41:08 +090068 private:
69 scoped_refptr<dbus::Bus> bus_;
70 dbus::ObjectProxy* proxy_ = nullptr; // owned by bus_
71
Hugo Benichi7d9d8db2020-03-30 15:56:56 +090072 bool SendSetVpnIntentRequest(int socket,
73 SetVpnIntentRequest::VpnRoutingPolicy policy);
74
Garrick Evans08843932019-09-17 14:41:08 +090075 DISALLOW_COPY_AND_ASSIGN(Client);
76};
77
78} // namespace patchpanel
79
Garrick Evans3388a032020-03-24 11:25:55 +090080#endif // PATCHPANEL_CLIENT_H_