Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Ben Chan | c45688b | 2014-07-02 23:50:45 -0700 | [diff] [blame] | 5 | #ifndef SHILL_CONNECTION_H_ |
| 6 | #define SHILL_CONNECTION_H_ |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 7 | |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 8 | #include <deque> |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 9 | #include <string> |
| 10 | #include <vector> |
| 11 | |
| 12 | #include <base/memory/ref_counted.h> |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 13 | #include <base/memory/weak_ptr.h> |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 14 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
| 15 | |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 16 | #include "shill/ipconfig.h" |
Peter Qiu | 8d6b597 | 2014-10-28 15:33:34 -0700 | [diff] [blame] | 17 | #include "shill/net/ip_address.h" |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 18 | #include "shill/refptr_types.h" |
Paul Stewart | e00600e | 2012-03-16 07:08:00 -0700 | [diff] [blame] | 19 | #include "shill/technology.h" |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 20 | |
| 21 | namespace shill { |
| 22 | |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 23 | class DeviceInfo; |
Prabhu Kaliamoorthi | 77e7683 | 2015-02-13 15:20:23 +0100 | [diff] [blame] | 24 | class PermissionBrokerProxyInterface; |
| 25 | class ProxyFactory; |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 26 | class RTNLHandler; |
mukesh agrawal | 23ac6b7 | 2013-01-31 18:52:37 -0800 | [diff] [blame] | 27 | class Resolver; |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 28 | class RoutingTable; |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 29 | struct RoutingTableEntry; |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 30 | |
| 31 | // The Conneciton maintains the implemented state of an IPConfig, e.g, |
| 32 | // the IP address, routing table and DNS table entries. |
| 33 | class Connection : public base::RefCounted<Connection> { |
| 34 | public: |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 35 | // Clients can instantiate and use Binder to bind to a Connection and get |
| 36 | // notified when the bound Connection disconnects. Note that the client's |
| 37 | // disconnect callback will be executed at most once, and only if the bound |
| 38 | // Connection is destroyed or signals disconnect. The Binder unbinds itself |
| 39 | // from the underlying Connection when the Binder instance is destructed. |
| 40 | class Binder { |
| 41 | public: |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 42 | Binder(const std::string& name, const base::Closure& disconnect_callback); |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 43 | ~Binder(); |
| 44 | |
Darin Petkov | ef1f9fe | 2012-05-11 16:51:52 +0200 | [diff] [blame] | 45 | // Binds to |to_connection|. Unbinds the previous bound connection, if |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 46 | // any. Pass nullptr to just unbind this Binder. |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 47 | void Attach(const ConnectionRefPtr& to_connection); |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 48 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 49 | const std::string& name() const { return name_; } |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 50 | bool IsBound() const { return connection_ != nullptr; } |
Darin Petkov | ef1f9fe | 2012-05-11 16:51:52 +0200 | [diff] [blame] | 51 | ConnectionRefPtr connection() const { return connection_.get(); } |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 52 | |
| 53 | private: |
| 54 | friend class Connection; |
| 55 | FRIEND_TEST(ConnectionTest, Binder); |
| 56 | |
| 57 | // Invoked by |connection_|. |
| 58 | void OnDisconnect(); |
| 59 | |
| 60 | const std::string name_; |
Darin Petkov | ef1f9fe | 2012-05-11 16:51:52 +0200 | [diff] [blame] | 61 | base::WeakPtr<Connection> connection_; |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 62 | const base::Closure client_disconnect_callback_; |
| 63 | |
| 64 | DISALLOW_COPY_AND_ASSIGN(Binder); |
| 65 | }; |
| 66 | |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 67 | Connection(int interface_index, |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 68 | const std::string& interface_name, |
Paul Stewart | e00600e | 2012-03-16 07:08:00 -0700 | [diff] [blame] | 69 | Technology::Identifier technology_, |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 70 | const DeviceInfo* device_info); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 71 | |
| 72 | // Add the contents of an IPConfig reference to the list of managed state. |
| 73 | // This will replace all previous state for this address family. |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 74 | virtual void UpdateFromIPConfig(const IPConfigRefPtr& config); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 75 | |
Paul Stewart | df3c0a8 | 2012-11-09 15:54:33 -0800 | [diff] [blame] | 76 | // Return the connection used by the lower binder. |
| 77 | virtual ConnectionRefPtr GetLowerConnection() const { |
| 78 | return lower_binder_.connection(); |
| 79 | } |
| 80 | |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 81 | // Sets the current connection as "default", i.e., routes and DNS entries |
| 82 | // should be used by all system components that don't select explicitly. |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 83 | virtual bool is_default() const { return is_default_; } |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 84 | virtual void SetIsDefault(bool is_default); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 85 | |
Peter Qiu | a89154b | 2014-05-23 15:45:42 -0700 | [diff] [blame] | 86 | // Update and apply the new DNS servers setting to this connection. |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 87 | virtual void UpdateDNSServers(const std::vector<std::string>& dns_servers); |
Peter Qiu | a89154b | 2014-05-23 15:45:42 -0700 | [diff] [blame] | 88 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 89 | virtual const std::string& interface_name() const { return interface_name_; } |
Paul Stewart | 4a6748d | 2012-07-17 14:31:36 -0700 | [diff] [blame] | 90 | virtual int interface_index() const { return interface_index_; } |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 91 | virtual const std::vector<std::string>& dns_servers() const { |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 92 | return dns_servers_; |
| 93 | } |
| 94 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 95 | virtual const std::string& ipconfig_rpc_identifier() const { |
Paul Stewart | 10241e3 | 2012-04-23 18:15:06 -0700 | [diff] [blame] | 96 | return ipconfig_rpc_identifier_; |
| 97 | } |
| 98 | |
Prabhu Kaliamoorthi | 77e7683 | 2015-02-13 15:20:23 +0100 | [diff] [blame] | 99 | virtual bool SetupIptableEntries(); |
| 100 | virtual bool TearDownIptableEntries(); |
| 101 | |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 102 | // Request to accept traffic routed to this connection even if it is not |
| 103 | // the default. This request is ref-counted so the caller must call |
| 104 | // ReleaseRouting() when they no longer need this facility. |
| 105 | virtual void RequestRouting(); |
| 106 | virtual void ReleaseRouting(); |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 107 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 108 | // Request a host route through this connection. |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 109 | virtual bool RequestHostRoute(const IPAddress& destination); |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 110 | |
Prabhu Kaliamoorthi | 762bfb8 | 2015-02-06 13:17:08 +0100 | [diff] [blame] | 111 | // Request a host route through this connection for a list of IPs in CIDR |
| 112 | // notation (|excluded_ips_cidr_|). |
| 113 | virtual bool PinPendingRoutes(int interface_index, RoutingTableEntry entry); |
| 114 | |
Paul Stewart | 2cb3fa7 | 2014-11-13 01:43:12 -0800 | [diff] [blame] | 115 | // Return the subnet name for this connection. |
| 116 | virtual std::string GetSubnetName() const; |
| 117 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 118 | virtual const IPAddress& local() const { return local_; } |
| 119 | virtual const IPAddress& gateway() const { return gateway_; } |
Alex Deymo | fddc09a | 2013-07-03 18:41:31 -0700 | [diff] [blame] | 120 | virtual Technology::Identifier technology() const { return technology_; } |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 121 | virtual const std::string& tethering() const { return tethering_; } |
| 122 | void set_tethering(const std::string& tethering) { tethering_ = tethering; } |
Alex Deymo | fddc09a | 2013-07-03 18:41:31 -0700 | [diff] [blame] | 123 | |
| 124 | // Return the lowest connection on which this connection depends. In case of |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 125 | // error, a nullptr is returned. |
Alex Deymo | fddc09a | 2013-07-03 18:41:31 -0700 | [diff] [blame] | 126 | virtual ConnectionRefPtr GetCarrierConnection(); |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 127 | |
Peter Qiu | f3a8f90 | 2014-08-20 10:05:42 -0700 | [diff] [blame] | 128 | // Return true if this is an IPv6 connection. |
| 129 | virtual bool IsIPv6(); |
| 130 | |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 131 | protected: |
| 132 | friend class base::RefCounted<Connection>; |
| 133 | |
| 134 | virtual ~Connection(); |
Paul Stewart | 4a6748d | 2012-07-17 14:31:36 -0700 | [diff] [blame] | 135 | virtual bool CreateGatewayRoute(); |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 136 | |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 137 | private: |
| 138 | friend class ConnectionTest; |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 139 | FRIEND_TEST(ConnectionTest, AddConfig); |
Prabhu Kaliamoorthi | 762bfb8 | 2015-02-06 13:17:08 +0100 | [diff] [blame] | 140 | FRIEND_TEST(ConnectionTest, AddConfigUserTrafficOnly); |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 141 | FRIEND_TEST(ConnectionTest, Binder); |
| 142 | FRIEND_TEST(ConnectionTest, Binders); |
Paul Stewart | 024a6c8 | 2015-01-23 14:59:40 -0800 | [diff] [blame] | 143 | FRIEND_TEST(ConnectionTest, BlackholeIPv6); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 144 | FRIEND_TEST(ConnectionTest, Destructor); |
Paul Stewart | 53a3038 | 2012-04-26 09:06:59 -0700 | [diff] [blame] | 145 | FRIEND_TEST(ConnectionTest, FixGatewayReachability); |
Alex Deymo | fddc09a | 2013-07-03 18:41:31 -0700 | [diff] [blame] | 146 | FRIEND_TEST(ConnectionTest, GetCarrierConnection); |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 147 | FRIEND_TEST(ConnectionTest, InitState); |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 148 | FRIEND_TEST(ConnectionTest, OnRouteQueryResponse); |
| 149 | FRIEND_TEST(ConnectionTest, RequestHostRoute); |
Paul Stewart | 024a6c8 | 2015-01-23 14:59:40 -0800 | [diff] [blame] | 150 | FRIEND_TEST(ConnectionTest, SetMTU); |
Peter Qiu | a89154b | 2014-05-23 15:45:42 -0700 | [diff] [blame] | 151 | FRIEND_TEST(ConnectionTest, UpdateDNSServers); |
Darin Petkov | 5eb0542 | 2012-05-11 15:45:25 +0200 | [diff] [blame] | 152 | FRIEND_TEST(VPNServiceTest, OnConnectionDisconnected); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 153 | |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 154 | static const uint32_t kDefaultMetric; |
| 155 | static const uint32_t kNonDefaultMetricBase; |
Prabhu Kaliamoorthi | 762bfb8 | 2015-02-06 13:17:08 +0100 | [diff] [blame] | 156 | static const uint32_t kMarkForUserTraffic; |
| 157 | static const uint8_t kSecondaryTableId; |
Paul Stewart | 7cfca04 | 2011-12-08 14:18:17 -0800 | [diff] [blame] | 158 | |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 159 | // Work around misconfigured servers which provide a gateway address that |
| 160 | // is unreachable with the provided netmask. |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 161 | static bool FixGatewayReachability(IPAddress* local, |
| 162 | IPAddress* peer, |
| 163 | IPAddress* gateway, |
| 164 | const IPAddress& trusted_ip); |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 165 | uint32_t GetMetric(bool is_default); |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 166 | bool PinHostRoute(const IPAddress& trusted_ip, const IPAddress& gateway); |
Paul Stewart | 024a6c8 | 2015-01-23 14:59:40 -0800 | [diff] [blame] | 167 | void SetMTU(int32_t mtu); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 168 | |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 169 | void OnRouteQueryResponse(int interface_index, |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 170 | const RoutingTableEntry& entry); |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 171 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 172 | void AttachBinder(Binder* binder); |
| 173 | void DetachBinder(Binder* binder); |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 174 | void NotifyBindersOnDisconnect(); |
| 175 | |
| 176 | void OnLowerDisconnect(); |
| 177 | |
Paul Stewart | 6f65c0b | 2012-09-11 14:57:32 -0700 | [diff] [blame] | 178 | // Send our DNS configuration to the resolver. |
| 179 | void PushDNSConfig(); |
| 180 | |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 181 | base::WeakPtrFactory<Connection> weak_ptr_factory_; |
| 182 | |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 183 | bool is_default_; |
Paul Stewart | 4a6748d | 2012-07-17 14:31:36 -0700 | [diff] [blame] | 184 | bool has_broadcast_domain_; |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 185 | int routing_request_count_; |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 186 | int interface_index_; |
| 187 | const std::string interface_name_; |
Paul Stewart | e00600e | 2012-03-16 07:08:00 -0700 | [diff] [blame] | 188 | Technology::Identifier technology_; |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 189 | std::vector<std::string> dns_servers_; |
| 190 | std::vector<std::string> dns_domain_search_; |
Prabhu Kaliamoorthi | 762bfb8 | 2015-02-06 13:17:08 +0100 | [diff] [blame] | 191 | std::vector<std::string> excluded_ips_cidr_; |
Paul Stewart | d62d603 | 2012-09-11 11:35:49 -0700 | [diff] [blame] | 192 | std::string dns_domain_name_; |
Paul Stewart | 10241e3 | 2012-04-23 18:15:06 -0700 | [diff] [blame] | 193 | std::string ipconfig_rpc_identifier_; |
Prabhu Kaliamoorthi | 762bfb8 | 2015-02-06 13:17:08 +0100 | [diff] [blame] | 194 | bool user_traffic_only_; |
| 195 | uint8_t table_id_; |
Paul Stewart | 4a6748d | 2012-07-17 14:31:36 -0700 | [diff] [blame] | 196 | IPAddress local_; |
| 197 | IPAddress gateway_; |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 198 | |
Paul Stewart | 7f5d9c0 | 2013-12-03 18:26:00 -0800 | [diff] [blame] | 199 | // Track the tethering status of the Service associated with this connection. |
| 200 | // This property is set by a service as it takes ownership of a connection, |
| 201 | // and is read by services that are bound through this connection. |
| 202 | std::string tethering_; |
| 203 | |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 204 | // A binder to a lower Connection that this Connection depends on, if any. |
| 205 | Binder lower_binder_; |
| 206 | |
| 207 | // Binders to clients -- usually to upper connections or related services and |
| 208 | // devices. |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 209 | std::deque<Binder*> binders_; |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 210 | |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 211 | // Store cached copies of singletons for speed/ease of testing |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 212 | const DeviceInfo* device_info_; |
| 213 | Resolver* resolver_; |
| 214 | RoutingTable* routing_table_; |
| 215 | RTNLHandler* rtnl_handler_; |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 216 | |
Prabhu Kaliamoorthi | 77e7683 | 2015-02-13 15:20:23 +0100 | [diff] [blame] | 217 | ProxyFactory* proxy_factory_; |
| 218 | std::unique_ptr<PermissionBrokerProxyInterface> permission_broker_; |
| 219 | |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 220 | DISALLOW_COPY_AND_ASSIGN(Connection); |
| 221 | }; |
| 222 | |
| 223 | } // namespace shill |
| 224 | |
Ben Chan | c45688b | 2014-07-02 23:50:45 -0700 | [diff] [blame] | 225 | #endif // SHILL_CONNECTION_H_ |