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 | |
Jason Jeremy Iman | adffbcb | 2020-08-31 13:21:36 +0900 | [diff] [blame] | 5 | #ifndef PATCHPANEL_DBUS_CLIENT_H_ |
| 6 | #define PATCHPANEL_DBUS_CLIENT_H_ |
Garrick Evans | 0884393 | 2019-09-17 14:41:08 +0900 | [diff] [blame] | 7 | |
| 8 | #include <memory> |
Jason Jeremy Iman | adffbcb | 2020-08-31 13:21:36 +0900 | [diff] [blame] | 9 | #include <set> |
Jason Jeremy Iman | 6f1f3e7 | 2020-07-06 13:04:03 +0900 | [diff] [blame] | 10 | #include <string> |
Garrick Evans | 0884393 | 2019-09-17 14:41:08 +0900 | [diff] [blame] | 11 | #include <utility> |
| 12 | #include <vector> |
| 13 | |
Hugo Benichi | cc6850f | 2020-01-17 13:26:06 +0900 | [diff] [blame] | 14 | #include "base/files/scoped_file.h" |
Garrick Evans | 0884393 | 2019-09-17 14:41:08 +0900 | [diff] [blame] | 15 | #include <brillo/brillo_export.h> |
| 16 | #include <dbus/bus.h> |
| 17 | #include <dbus/object_proxy.h> |
Hugo Benichi | 8135e56 | 2019-12-12 15:56:36 +0900 | [diff] [blame] | 18 | #include <patchpanel/proto_bindings/patchpanel_service.pb.h> |
Garrick Evans | 0884393 | 2019-09-17 14:41:08 +0900 | [diff] [blame] | 19 | |
| 20 | namespace patchpanel { |
| 21 | |
Hugo Benichi | 51ff71b | 2020-08-19 00:55:15 +0900 | [diff] [blame] | 22 | // Simple wrapper around patchpanel DBus API. All public functions are blocking |
Jie Jiang | 0a70acf | 2020-10-02 11:57:32 +0900 | [diff] [blame] | 23 | // 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 Evans | 0884393 | 2019-09-17 14:41:08 +0900 | [diff] [blame] | 27 | class BRILLO_EXPORT Client { |
| 28 | public: |
Jie Jiang | 0a70acf | 2020-10-02 11:57:32 +0900 | [diff] [blame] | 29 | using GetTrafficCountersCallback = |
| 30 | base::OnceCallback<void(const std::vector<TrafficCounter>&)>; |
| 31 | |
Garrick Evans | 0884393 | 2019-09-17 14:41:08 +0900 | [diff] [blame] | 32 | static std::unique_ptr<Client> New(); |
| 33 | |
Garrick Evans | 93a83fc | 2020-03-31 15:16:55 +0900 | [diff] [blame] | 34 | Client(const scoped_refptr<dbus::Bus>& bus, dbus::ObjectProxy* proxy) |
Garrick Evans | 0884393 | 2019-09-17 14:41:08 +0900 | [diff] [blame] | 35 | : bus_(std::move(bus)), proxy_(proxy) {} |
Garrick Evans | 93a83fc | 2020-03-31 15:16:55 +0900 | [diff] [blame] | 36 | ~Client(); |
Garrick Evans | 0884393 | 2019-09-17 14:41:08 +0900 | [diff] [blame] | 37 | |
| 38 | bool NotifyArcStartup(pid_t pid); |
Garrick Evans | ca2b41b | 2019-12-02 09:06:11 +0900 | [diff] [blame] | 39 | bool NotifyArcShutdown(); |
Garrick Evans | 27b7403 | 2019-11-19 13:33:47 +0900 | [diff] [blame] | 40 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 41 | std::vector<NetworkDevice> NotifyArcVmStartup(uint32_t cid); |
Garrick Evans | 0a18937 | 2020-02-07 08:55:27 +0900 | [diff] [blame] | 42 | bool NotifyArcVmShutdown(uint32_t cid); |
Garrick Evans | 0884393 | 2019-09-17 14:41:08 +0900 | [diff] [blame] | 43 | |
Garrick Evans | 0a18937 | 2020-02-07 08:55:27 +0900 | [diff] [blame] | 44 | bool NotifyTerminaVmStartup(uint32_t cid, |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 45 | NetworkDevice* device, |
| 46 | IPv4Subnet* container_subnet); |
Garrick Evans | 0a18937 | 2020-02-07 08:55:27 +0900 | [diff] [blame] | 47 | bool NotifyTerminaVmShutdown(uint32_t cid); |
Garrick Evans | 27b7403 | 2019-11-19 13:33:47 +0900 | [diff] [blame] | 48 | |
Garrick Evans | 376f067 | 2020-01-07 15:31:50 +0900 | [diff] [blame] | 49 | bool NotifyPluginVmStartup(uint64_t vm_id, |
| 50 | int subnet_index, |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 51 | NetworkDevice* device); |
Garrick Evans | 376f067 | 2020-01-07 15:31:50 +0900 | [diff] [blame] | 52 | bool NotifyPluginVmShutdown(uint64_t vm_id); |
| 53 | |
Hugo Benichi | 7d9d8db | 2020-03-30 15:56:56 +0900 | [diff] [blame] | 54 | // Reset the VPN routing intent mark on a socket to the default policy for |
| 55 | // the current uid. This is in general incorrect to call this method for |
| 56 | // a socket that is already connected. |
| 57 | bool DefaultVpnRouting(int socket); |
| 58 | |
| 59 | // Mark a socket to be always routed through a VPN if there is one. |
| 60 | // Must be called before the socket is connected. |
| 61 | bool RouteOnVpn(int socket); |
| 62 | |
| 63 | // Mark a socket to be always routed through the physical network. |
| 64 | // Must be called before the socket is connected. |
| 65 | bool BypassVpn(int socket); |
| 66 | |
Hugo Benichi | cc6850f | 2020-01-17 13:26:06 +0900 | [diff] [blame] | 67 | // Sends a ConnectNamespaceRequest for the given namespace pid. Returns a |
| 68 | // pair with a valid ScopedFD and the ConnectNamespaceResponse proto message |
| 69 | // received if the request succeeded. Closing the ScopedFD will teardown the |
| 70 | // veth and routing setup and free the allocated IPv4 subnet. |
| 71 | std::pair<base::ScopedFD, patchpanel::ConnectNamespaceResponse> |
| 72 | ConnectNamespace(pid_t pid, |
| 73 | const std::string& outbound_ifname, |
| 74 | bool forward_user_traffic); |
| 75 | |
Jie Jiang | 0a70acf | 2020-10-02 11:57:32 +0900 | [diff] [blame] | 76 | // Gets the traffic counters kept by patchpanel asynchronously, |callback| |
| 77 | // will be called with the counters once they are ready, or with an empty |
| 78 | // vector when an error happen. |devices| is the set of interfaces (shill |
| 79 | // devices) for which counters should be returned, any unknown interfaces will |
| 80 | // be ignored. If |devices| is empty, counters for all known interfaces will |
| 81 | // be returned. |
| 82 | void GetTrafficCounters(const std::set<std::string>& devices, |
| 83 | GetTrafficCountersCallback callback); |
Jie Jiang | e02d120 | 2020-07-27 16:57:04 +0900 | [diff] [blame] | 84 | |
Jason Jeremy Iman | 6f1f3e7 | 2020-07-06 13:04:03 +0900 | [diff] [blame] | 85 | // Sends a ModifyPortRuleRequest to modify iptables ingress rules. |
| 86 | // This should only be called by permission_broker's 'devbroker'. |
| 87 | bool ModifyPortRule(patchpanel::ModifyPortRuleRequest::Operation op, |
| 88 | patchpanel::ModifyPortRuleRequest::RuleType type, |
| 89 | patchpanel::ModifyPortRuleRequest::Protocol proto, |
| 90 | const std::string& input_ifname, |
| 91 | const std::string& input_dst_ip, |
| 92 | uint32_t input_dst_port, |
| 93 | const std::string& dst_ip, |
| 94 | uint32_t dst_port); |
| 95 | |
Garrick Evans | 0884393 | 2019-09-17 14:41:08 +0900 | [diff] [blame] | 96 | private: |
| 97 | scoped_refptr<dbus::Bus> bus_; |
| 98 | dbus::ObjectProxy* proxy_ = nullptr; // owned by bus_ |
| 99 | |
Hugo Benichi | 7d9d8db | 2020-03-30 15:56:56 +0900 | [diff] [blame] | 100 | bool SendSetVpnIntentRequest(int socket, |
| 101 | SetVpnIntentRequest::VpnRoutingPolicy policy); |
| 102 | |
Garrick Evans | 0884393 | 2019-09-17 14:41:08 +0900 | [diff] [blame] | 103 | DISALLOW_COPY_AND_ASSIGN(Client); |
| 104 | }; |
| 105 | |
| 106 | } // namespace patchpanel |
| 107 | |
Jason Jeremy Iman | adffbcb | 2020-08-31 13:21:36 +0900 | [diff] [blame] | 108 | #endif // PATCHPANEL_DBUS_CLIENT_H_ |