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