blob: 9bf8ca0366608f23b783b73c4ab5c5c3421a0523 [file] [log] [blame]
Paul Stewartc681fa02012-03-02 19:40:04 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Paul Stewartdd60e452011-08-08 11:38:36 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Ben Chanc45688b2014-07-02 23:50:45 -07005#ifndef SHILL_CONNECTION_H_
6#define SHILL_CONNECTION_H_
Paul Stewartdd60e452011-08-08 11:38:36 -07007
Darin Petkov13e6d552012-05-09 14:22:23 +02008#include <deque>
Paul Stewartdd60e452011-08-08 11:38:36 -07009#include <string>
10#include <vector>
11
12#include <base/memory/ref_counted.h>
Darin Petkov13e6d552012-05-09 14:22:23 +020013#include <base/memory/weak_ptr.h>
Paul Stewartdd60e452011-08-08 11:38:36 -070014#include <gtest/gtest_prod.h> // for FRIEND_TEST
15
Paul Stewarte93b0382012-04-24 13:11:28 -070016#include "shill/ipconfig.h"
Peter Qiu8d6b5972014-10-28 15:33:34 -070017#include "shill/net/ip_address.h"
Paul Stewartdd60e452011-08-08 11:38:36 -070018#include "shill/refptr_types.h"
Paul Stewarte00600e2012-03-16 07:08:00 -070019#include "shill/technology.h"
Paul Stewartdd60e452011-08-08 11:38:36 -070020
21namespace shill {
22
Paul Stewart9a908082011-08-31 12:18:48 -070023class DeviceInfo;
Prabhu Kaliamoorthi77e76832015-02-13 15:20:23 +010024class PermissionBrokerProxyInterface;
25class ProxyFactory;
Darin Petkov13e6d552012-05-09 14:22:23 +020026class RTNLHandler;
mukesh agrawal23ac6b72013-01-31 18:52:37 -080027class Resolver;
Paul Stewartdd60e452011-08-08 11:38:36 -070028class RoutingTable;
Darin Petkov13e6d552012-05-09 14:22:23 +020029struct RoutingTableEntry;
Paul Stewartdd60e452011-08-08 11:38:36 -070030
31// The Conneciton maintains the implemented state of an IPConfig, e.g,
32// the IP address, routing table and DNS table entries.
33class Connection : public base::RefCounted<Connection> {
34 public:
Darin Petkov13e6d552012-05-09 14:22:23 +020035 // 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 Stewarta794cd62015-06-16 13:13:10 -070042 Binder(const std::string& name, const base::Closure& disconnect_callback);
Darin Petkov13e6d552012-05-09 14:22:23 +020043 ~Binder();
44
Darin Petkovef1f9fe2012-05-11 16:51:52 +020045 // Binds to |to_connection|. Unbinds the previous bound connection, if
Ben Chancc225ef2014-09-30 13:26:51 -070046 // any. Pass nullptr to just unbind this Binder.
Paul Stewarta794cd62015-06-16 13:13:10 -070047 void Attach(const ConnectionRefPtr& to_connection);
Darin Petkov13e6d552012-05-09 14:22:23 +020048
Paul Stewarta794cd62015-06-16 13:13:10 -070049 const std::string& name() const { return name_; }
Ben Chancc225ef2014-09-30 13:26:51 -070050 bool IsBound() const { return connection_ != nullptr; }
Darin Petkovef1f9fe2012-05-11 16:51:52 +020051 ConnectionRefPtr connection() const { return connection_.get(); }
Darin Petkov13e6d552012-05-09 14:22:23 +020052
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 Petkovef1f9fe2012-05-11 16:51:52 +020061 base::WeakPtr<Connection> connection_;
Darin Petkov13e6d552012-05-09 14:22:23 +020062 const base::Closure client_disconnect_callback_;
63
64 DISALLOW_COPY_AND_ASSIGN(Binder);
65 };
66
Paul Stewart9a908082011-08-31 12:18:48 -070067 Connection(int interface_index,
Paul Stewarta794cd62015-06-16 13:13:10 -070068 const std::string& interface_name,
Paul Stewarte00600e2012-03-16 07:08:00 -070069 Technology::Identifier technology_,
Paul Stewarta794cd62015-06-16 13:13:10 -070070 const DeviceInfo* device_info);
Paul Stewartdd60e452011-08-08 11:38:36 -070071
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 Stewarta794cd62015-06-16 13:13:10 -070074 virtual void UpdateFromIPConfig(const IPConfigRefPtr& config);
Paul Stewartdd60e452011-08-08 11:38:36 -070075
Paul Stewartdf3c0a82012-11-09 15:54:33 -080076 // Return the connection used by the lower binder.
77 virtual ConnectionRefPtr GetLowerConnection() const {
78 return lower_binder_.connection();
79 }
80
Paul Stewartdd60e452011-08-08 11:38:36 -070081 // 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 Stewartc681fa02012-03-02 19:40:04 -080083 virtual bool is_default() const { return is_default_; }
Paul Stewartc1dec4d2011-12-08 15:25:28 -080084 virtual void SetIsDefault(bool is_default);
Paul Stewartdd60e452011-08-08 11:38:36 -070085
Peter Qiua89154b2014-05-23 15:45:42 -070086 // Update and apply the new DNS servers setting to this connection.
Paul Stewarta794cd62015-06-16 13:13:10 -070087 virtual void UpdateDNSServers(const std::vector<std::string>& dns_servers);
Peter Qiua89154b2014-05-23 15:45:42 -070088
Paul Stewarta794cd62015-06-16 13:13:10 -070089 virtual const std::string& interface_name() const { return interface_name_; }
Paul Stewart4a6748d2012-07-17 14:31:36 -070090 virtual int interface_index() const { return interface_index_; }
Paul Stewarta794cd62015-06-16 13:13:10 -070091 virtual const std::vector<std::string>& dns_servers() const {
Paul Stewartc8f4bef2011-12-13 09:45:51 -080092 return dns_servers_;
93 }
94
Paul Stewarta794cd62015-06-16 13:13:10 -070095 virtual const std::string& ipconfig_rpc_identifier() const {
Paul Stewart10241e32012-04-23 18:15:06 -070096 return ipconfig_rpc_identifier_;
97 }
98
Prabhu Kaliamoorthi77e76832015-02-13 15:20:23 +010099 virtual bool SetupIptableEntries();
100 virtual bool TearDownIptableEntries();
101
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800102 // 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 Stewartbe5f5b32011-12-07 17:11:11 -0800107
Paul Stewartf748a362012-03-07 12:01:20 -0800108 // Request a host route through this connection.
Paul Stewarta794cd62015-06-16 13:13:10 -0700109 virtual bool RequestHostRoute(const IPAddress& destination);
Paul Stewartf748a362012-03-07 12:01:20 -0800110
Prabhu Kaliamoorthi762bfb82015-02-06 13:17:08 +0100111 // 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 Stewart2cb3fa72014-11-13 01:43:12 -0800115 // Return the subnet name for this connection.
116 virtual std::string GetSubnetName() const;
117
Paul Stewarta794cd62015-06-16 13:13:10 -0700118 virtual const IPAddress& local() const { return local_; }
119 virtual const IPAddress& gateway() const { return gateway_; }
Alex Deymofddc09a2013-07-03 18:41:31 -0700120 virtual Technology::Identifier technology() const { return technology_; }
Paul Stewarta794cd62015-06-16 13:13:10 -0700121 virtual const std::string& tethering() const { return tethering_; }
122 void set_tethering(const std::string& tethering) { tethering_ = tethering; }
Alex Deymofddc09a2013-07-03 18:41:31 -0700123
124 // Return the lowest connection on which this connection depends. In case of
Ben Chancc225ef2014-09-30 13:26:51 -0700125 // error, a nullptr is returned.
Alex Deymofddc09a2013-07-03 18:41:31 -0700126 virtual ConnectionRefPtr GetCarrierConnection();
Paul Stewart6c72c972012-07-27 11:29:20 -0700127
Peter Qiuf3a8f902014-08-20 10:05:42 -0700128 // Return true if this is an IPv6 connection.
129 virtual bool IsIPv6();
130
Darin Petkov13e6d552012-05-09 14:22:23 +0200131 protected:
132 friend class base::RefCounted<Connection>;
133
134 virtual ~Connection();
Paul Stewart4a6748d2012-07-17 14:31:36 -0700135 virtual bool CreateGatewayRoute();
Darin Petkov13e6d552012-05-09 14:22:23 +0200136
Paul Stewartdd60e452011-08-08 11:38:36 -0700137 private:
138 friend class ConnectionTest;
Paul Stewartdd60e452011-08-08 11:38:36 -0700139 FRIEND_TEST(ConnectionTest, AddConfig);
Prabhu Kaliamoorthi762bfb82015-02-06 13:17:08 +0100140 FRIEND_TEST(ConnectionTest, AddConfigUserTrafficOnly);
Darin Petkov13e6d552012-05-09 14:22:23 +0200141 FRIEND_TEST(ConnectionTest, Binder);
142 FRIEND_TEST(ConnectionTest, Binders);
Paul Stewart024a6c82015-01-23 14:59:40 -0800143 FRIEND_TEST(ConnectionTest, BlackholeIPv6);
Paul Stewartdd60e452011-08-08 11:38:36 -0700144 FRIEND_TEST(ConnectionTest, Destructor);
Paul Stewart53a30382012-04-26 09:06:59 -0700145 FRIEND_TEST(ConnectionTest, FixGatewayReachability);
Alex Deymofddc09a2013-07-03 18:41:31 -0700146 FRIEND_TEST(ConnectionTest, GetCarrierConnection);
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700147 FRIEND_TEST(ConnectionTest, InitState);
Darin Petkov13e6d552012-05-09 14:22:23 +0200148 FRIEND_TEST(ConnectionTest, OnRouteQueryResponse);
149 FRIEND_TEST(ConnectionTest, RequestHostRoute);
Paul Stewart024a6c82015-01-23 14:59:40 -0800150 FRIEND_TEST(ConnectionTest, SetMTU);
Peter Qiua89154b2014-05-23 15:45:42 -0700151 FRIEND_TEST(ConnectionTest, UpdateDNSServers);
Darin Petkov5eb05422012-05-11 15:45:25 +0200152 FRIEND_TEST(VPNServiceTest, OnConnectionDisconnected);
Paul Stewartdd60e452011-08-08 11:38:36 -0700153
Ben Chan7fab8972014-08-10 17:14:46 -0700154 static const uint32_t kDefaultMetric;
155 static const uint32_t kNonDefaultMetricBase;
Prabhu Kaliamoorthi762bfb82015-02-06 13:17:08 +0100156 static const uint32_t kMarkForUserTraffic;
157 static const uint8_t kSecondaryTableId;
Paul Stewart7cfca042011-12-08 14:18:17 -0800158
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700159 // Work around misconfigured servers which provide a gateway address that
160 // is unreachable with the provided netmask.
Paul Stewarta794cd62015-06-16 13:13:10 -0700161 static bool FixGatewayReachability(IPAddress* local,
162 IPAddress* peer,
163 IPAddress* gateway,
164 const IPAddress& trusted_ip);
Ben Chan7fab8972014-08-10 17:14:46 -0700165 uint32_t GetMetric(bool is_default);
Paul Stewarta794cd62015-06-16 13:13:10 -0700166 bool PinHostRoute(const IPAddress& trusted_ip, const IPAddress& gateway);
Paul Stewart024a6c82015-01-23 14:59:40 -0800167 void SetMTU(int32_t mtu);
Paul Stewartdd60e452011-08-08 11:38:36 -0700168
Darin Petkov13e6d552012-05-09 14:22:23 +0200169 void OnRouteQueryResponse(int interface_index,
Paul Stewarta794cd62015-06-16 13:13:10 -0700170 const RoutingTableEntry& entry);
Darin Petkov13e6d552012-05-09 14:22:23 +0200171
Paul Stewarta794cd62015-06-16 13:13:10 -0700172 void AttachBinder(Binder* binder);
173 void DetachBinder(Binder* binder);
Darin Petkov13e6d552012-05-09 14:22:23 +0200174 void NotifyBindersOnDisconnect();
175
176 void OnLowerDisconnect();
177
Paul Stewart6f65c0b2012-09-11 14:57:32 -0700178 // Send our DNS configuration to the resolver.
179 void PushDNSConfig();
180
Darin Petkov13e6d552012-05-09 14:22:23 +0200181 base::WeakPtrFactory<Connection> weak_ptr_factory_;
182
Paul Stewartdd60e452011-08-08 11:38:36 -0700183 bool is_default_;
Paul Stewart4a6748d2012-07-17 14:31:36 -0700184 bool has_broadcast_domain_;
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800185 int routing_request_count_;
Paul Stewartdd60e452011-08-08 11:38:36 -0700186 int interface_index_;
187 const std::string interface_name_;
Paul Stewarte00600e2012-03-16 07:08:00 -0700188 Technology::Identifier technology_;
Paul Stewartdd60e452011-08-08 11:38:36 -0700189 std::vector<std::string> dns_servers_;
190 std::vector<std::string> dns_domain_search_;
Prabhu Kaliamoorthi762bfb82015-02-06 13:17:08 +0100191 std::vector<std::string> excluded_ips_cidr_;
Paul Stewartd62d6032012-09-11 11:35:49 -0700192 std::string dns_domain_name_;
Paul Stewart10241e32012-04-23 18:15:06 -0700193 std::string ipconfig_rpc_identifier_;
Prabhu Kaliamoorthi762bfb82015-02-06 13:17:08 +0100194 bool user_traffic_only_;
195 uint8_t table_id_;
Paul Stewart4a6748d2012-07-17 14:31:36 -0700196 IPAddress local_;
197 IPAddress gateway_;
Paul Stewartdd60e452011-08-08 11:38:36 -0700198
Paul Stewart7f5d9c02013-12-03 18:26:00 -0800199 // 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 Petkov13e6d552012-05-09 14:22:23 +0200204 // 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 Stewarta794cd62015-06-16 13:13:10 -0700209 std::deque<Binder*> binders_;
Darin Petkov13e6d552012-05-09 14:22:23 +0200210
Paul Stewartdd60e452011-08-08 11:38:36 -0700211 // Store cached copies of singletons for speed/ease of testing
Paul Stewarta794cd62015-06-16 13:13:10 -0700212 const DeviceInfo* device_info_;
213 Resolver* resolver_;
214 RoutingTable* routing_table_;
215 RTNLHandler* rtnl_handler_;
Paul Stewartdd60e452011-08-08 11:38:36 -0700216
Prabhu Kaliamoorthi77e76832015-02-13 15:20:23 +0100217 ProxyFactory* proxy_factory_;
218 std::unique_ptr<PermissionBrokerProxyInterface> permission_broker_;
219
Paul Stewartdd60e452011-08-08 11:38:36 -0700220 DISALLOW_COPY_AND_ASSIGN(Connection);
221};
222
223} // namespace shill
224
Ben Chanc45688b2014-07-02 23:50:45 -0700225#endif // SHILL_CONNECTION_H_