Garrick Evans | 0884393 | 2019-09-17 14:41:08 +0900 | [diff] [blame] | 1 | // 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 Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 5 | #ifndef PATCHPANEL_CLIENT_H_ |
| 6 | #define PATCHPANEL_CLIENT_H_ |
Garrick Evans | 0884393 | 2019-09-17 14:41:08 +0900 | [diff] [blame] | 7 | |
| 8 | #include <memory> |
| 9 | #include <utility> |
| 10 | #include <vector> |
| 11 | |
Hugo Benichi | cc6850f | 2020-01-17 13:26:06 +0900 | [diff] [blame] | 12 | #include "base/files/scoped_file.h" |
Garrick Evans | 0884393 | 2019-09-17 14:41:08 +0900 | [diff] [blame] | 13 | #include <brillo/brillo_export.h> |
| 14 | #include <dbus/bus.h> |
| 15 | #include <dbus/object_proxy.h> |
Hugo Benichi | 8135e56 | 2019-12-12 15:56:36 +0900 | [diff] [blame] | 16 | #include <patchpanel/proto_bindings/patchpanel_service.pb.h> |
Garrick Evans | 0884393 | 2019-09-17 14:41:08 +0900 | [diff] [blame] | 17 | |
| 18 | namespace patchpanel { |
| 19 | |
Hugo Benichi | 7d9d8db | 2020-03-30 15:56:56 +0900 | [diff] [blame] | 20 | // Simple wrapper around patchpanel DBus API. All public functions are |
| 21 | // blocking DBus calls to patchpaneld. |
Garrick Evans | 0884393 | 2019-09-17 14:41:08 +0900 | [diff] [blame] | 22 | class BRILLO_EXPORT Client { |
| 23 | public: |
| 24 | static std::unique_ptr<Client> New(); |
| 25 | |
Garrick Evans | 93a83fc | 2020-03-31 15:16:55 +0900 | [diff] [blame] | 26 | Client(const scoped_refptr<dbus::Bus>& bus, dbus::ObjectProxy* proxy) |
Garrick Evans | 0884393 | 2019-09-17 14:41:08 +0900 | [diff] [blame] | 27 | : bus_(std::move(bus)), proxy_(proxy) {} |
Garrick Evans | 93a83fc | 2020-03-31 15:16:55 +0900 | [diff] [blame] | 28 | ~Client(); |
Garrick Evans | 0884393 | 2019-09-17 14:41:08 +0900 | [diff] [blame] | 29 | |
| 30 | bool NotifyArcStartup(pid_t pid); |
Garrick Evans | ca2b41b | 2019-12-02 09:06:11 +0900 | [diff] [blame] | 31 | bool NotifyArcShutdown(); |
Garrick Evans | 27b7403 | 2019-11-19 13:33:47 +0900 | [diff] [blame] | 32 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 33 | std::vector<NetworkDevice> NotifyArcVmStartup(uint32_t cid); |
Garrick Evans | 0a18937 | 2020-02-07 08:55:27 +0900 | [diff] [blame] | 34 | bool NotifyArcVmShutdown(uint32_t cid); |
Garrick Evans | 0884393 | 2019-09-17 14:41:08 +0900 | [diff] [blame] | 35 | |
Garrick Evans | 0a18937 | 2020-02-07 08:55:27 +0900 | [diff] [blame] | 36 | bool NotifyTerminaVmStartup(uint32_t cid, |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 37 | NetworkDevice* device, |
| 38 | IPv4Subnet* container_subnet); |
Garrick Evans | 0a18937 | 2020-02-07 08:55:27 +0900 | [diff] [blame] | 39 | bool NotifyTerminaVmShutdown(uint32_t cid); |
Garrick Evans | 27b7403 | 2019-11-19 13:33:47 +0900 | [diff] [blame] | 40 | |
Garrick Evans | 376f067 | 2020-01-07 15:31:50 +0900 | [diff] [blame] | 41 | bool NotifyPluginVmStartup(uint64_t vm_id, |
| 42 | int subnet_index, |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 43 | NetworkDevice* device); |
Garrick Evans | 376f067 | 2020-01-07 15:31:50 +0900 | [diff] [blame] | 44 | bool NotifyPluginVmShutdown(uint64_t vm_id); |
| 45 | |
Hugo Benichi | 7d9d8db | 2020-03-30 15:56:56 +0900 | [diff] [blame] | 46 | // 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 Benichi | cc6850f | 2020-01-17 13:26:06 +0900 | [diff] [blame] | 59 | // 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 Evans | 0884393 | 2019-09-17 14:41:08 +0900 | [diff] [blame] | 68 | private: |
| 69 | scoped_refptr<dbus::Bus> bus_; |
| 70 | dbus::ObjectProxy* proxy_ = nullptr; // owned by bus_ |
| 71 | |
Hugo Benichi | 7d9d8db | 2020-03-30 15:56:56 +0900 | [diff] [blame] | 72 | bool SendSetVpnIntentRequest(int socket, |
| 73 | SetVpnIntentRequest::VpnRoutingPolicy policy); |
| 74 | |
Garrick Evans | 0884393 | 2019-09-17 14:41:08 +0900 | [diff] [blame] | 75 | DISALLOW_COPY_AND_ASSIGN(Client); |
| 76 | }; |
| 77 | |
| 78 | } // namespace patchpanel |
| 79 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 80 | #endif // PATCHPANEL_CLIENT_H_ |