1 // 2 // Copyright (C) 2012 The Android Open Source Project 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 // 16 17 #ifndef SHILL_CONNECTION_H_ 18 #define SHILL_CONNECTION_H_ 19 20 #include <deque> 21 #include <string> 22 #include <vector> 23 24 #include <base/memory/ref_counted.h> 25 #include <base/memory/weak_ptr.h> 26 #include <gtest/gtest_prod.h> // for FRIEND_TEST 27 28 #include "shill/ipconfig.h" 29 #include "shill/net/ip_address.h" 30 #include "shill/refptr_types.h" 31 #include "shill/technology.h" 32 33 namespace shill { 34 35 class ControlInterface; 36 class DeviceInfo; 37 class FirewallProxyInterface; 38 class RTNLHandler; 39 #if !defined(__ANDROID__) 40 class Resolver; 41 #else 42 class DNSServerProxy; 43 class DNSServerProxyFactory; 44 #endif // __ANDROID__ 45 class RoutingTable; 46 struct RoutingTableEntry; 47 48 // The Conneciton maintains the implemented state of an IPConfig, e.g, 49 // the IP address, routing table and DNS table entries. 50 class Connection : public base::RefCounted<Connection> { 51 public: 52 // Clients can instantiate and use Binder to bind to a Connection and get 53 // notified when the bound Connection disconnects. Note that the client's 54 // disconnect callback will be executed at most once, and only if the bound 55 // Connection is destroyed or signals disconnect. The Binder unbinds itself 56 // from the underlying Connection when the Binder instance is destructed. 57 class Binder { 58 public: 59 Binder(const std::string& name, const base::Closure& disconnect_callback); 60 ~Binder(); 61 62 // Binds to |to_connection|. Unbinds the previous bound connection, if 63 // any. Pass nullptr to just unbind this Binder. 64 void Attach(const ConnectionRefPtr& to_connection); 65 66 const std::string& name() const { return name_; } 67 bool IsBound() const { return connection_ != nullptr; } 68 ConnectionRefPtr connection() const { return connection_.get(); } 69 70 private: 71 friend class Connection; 72 FRIEND_TEST(ConnectionTest, Binder); 73 74 // Invoked by |connection_|. 75 void OnDisconnect(); 76 77 const std::string name_; 78 base::WeakPtr<Connection> connection_; 79 const base::Closure client_disconnect_callback_; 80 81 DISALLOW_COPY_AND_ASSIGN(Binder); 82 }; 83 84 Connection(int interface_index, 85 const std::string& interface_name, 86 Technology::Identifier technology_, 87 const DeviceInfo* device_info, 88 ControlInterface* control_interface); 89 90 // Add the contents of an IPConfig reference to the list of managed state. 91 // This will replace all previous state for this address family. 92 virtual void UpdateFromIPConfig(const IPConfigRefPtr& config); 93 94 // Return the connection used by the lower binder. 95 virtual ConnectionRefPtr GetLowerConnection() const { 96 return lower_binder_.connection(); 97 } 98 99 // Sets the current connection as "default", i.e., routes and DNS entries 100 // should be used by all system components that don't select explicitly. 101 virtual bool is_default() const { return is_default_; } 102 virtual void SetIsDefault(bool is_default); 103 104 // Update and apply the new DNS servers setting to this connection. 105 virtual void UpdateDNSServers(const std::vector<std::string>& dns_servers); 106 107 virtual const std::string& interface_name() const { return interface_name_; } 108 virtual int interface_index() const { return interface_index_; } 109 virtual const std::vector<std::string>& dns_servers() const { 110 return dns_servers_; 111 } 112 virtual uint8_t table_id() const { return table_id_; } 113 114 virtual const std::string& ipconfig_rpc_identifier() const { 115 return ipconfig_rpc_identifier_; 116 } 117 118 virtual bool SetupIptableEntries(); 119 virtual bool TearDownIptableEntries(); 120 121 // Request to accept traffic routed to this connection even if it is not 122 // the default. This request is ref-counted so the caller must call 123 // ReleaseRouting() when they no longer need this facility. 124 virtual void RequestRouting(); 125 virtual void ReleaseRouting(); 126 127 // Request a host route through this connection. 128 virtual bool RequestHostRoute(const IPAddress& destination); 129 130 // Request a host route through this connection for a list of IPs in CIDR 131 // notation (|excluded_ips_cidr_|). 132 virtual bool PinPendingRoutes(int interface_index, RoutingTableEntry entry); 133 134 // Return the subnet name for this connection. 135 virtual std::string GetSubnetName() const; 136 137 virtual const IPAddress& local() const { return local_; } 138 virtual const IPAddress& gateway() const { return gateway_; } 139 virtual Technology::Identifier technology() const { return technology_; } 140 virtual const std::string& tethering() const { return tethering_; } 141 void set_tethering(const std::string& tethering) { tethering_ = tethering; } 142 143 // Return the lowest connection on which this connection depends. In case of 144 // error, a nullptr is returned. 145 virtual ConnectionRefPtr GetCarrierConnection(); 146 147 // Return true if this is an IPv6 connection. 148 virtual bool IsIPv6(); 149 150 protected: 151 friend class base::RefCounted<Connection>; 152 153 virtual ~Connection(); 154 virtual bool CreateGatewayRoute(); 155 156 private: 157 friend class ConnectionTest; 158 FRIEND_TEST(ConnectionTest, AddConfig); 159 FRIEND_TEST(ConnectionTest, AddConfigUserTrafficOnly); 160 FRIEND_TEST(ConnectionTest, Binder); 161 FRIEND_TEST(ConnectionTest, Binders); 162 FRIEND_TEST(ConnectionTest, BlackholeIPv6); 163 FRIEND_TEST(ConnectionTest, Destructor); 164 FRIEND_TEST(ConnectionTest, FixGatewayReachability); 165 FRIEND_TEST(ConnectionTest, GetCarrierConnection); 166 FRIEND_TEST(ConnectionTest, InitState); 167 FRIEND_TEST(ConnectionTest, OnRouteQueryResponse); 168 FRIEND_TEST(ConnectionTest, RequestHostRoute); 169 FRIEND_TEST(ConnectionTest, SetMTU); 170 FRIEND_TEST(ConnectionTest, UpdateDNSServers); 171 FRIEND_TEST(VPNServiceTest, OnConnectionDisconnected); 172 173 static const uint32_t kDefaultMetric; 174 static const uint32_t kNonDefaultMetricBase; 175 static const uint32_t kMarkForUserTraffic; 176 static const uint8_t kSecondaryTableId; 177 178 // Work around misconfigured servers which provide a gateway address that 179 // is unreachable with the provided netmask. 180 bool FixGatewayReachability(const IPAddress& local, 181 IPAddress* peer, 182 IPAddress* gateway, 183 const IPAddress& trusted_ip); 184 uint32_t GetMetric(bool is_default); 185 bool PinHostRoute(const IPAddress& trusted_ip, const IPAddress& gateway); 186 void SetMTU(int32_t mtu); 187 188 void OnRouteQueryResponse(int interface_index, 189 const RoutingTableEntry& entry); 190 191 void AttachBinder(Binder* binder); 192 void DetachBinder(Binder* binder); 193 void NotifyBindersOnDisconnect(); 194 195 void OnLowerDisconnect(); 196 197 // Send our DNS configuration to the resolver. 198 void PushDNSConfig(); 199 200 base::WeakPtrFactory<Connection> weak_ptr_factory_; 201 202 bool is_default_; 203 bool has_broadcast_domain_; 204 int routing_request_count_; 205 int interface_index_; 206 const std::string interface_name_; 207 Technology::Identifier technology_; 208 std::vector<std::string> dns_servers_; 209 std::vector<std::string> dns_domain_search_; 210 std::vector<std::string> excluded_ips_cidr_; 211 std::string dns_domain_name_; 212 std::string ipconfig_rpc_identifier_; 213 bool user_traffic_only_; 214 uint8_t table_id_; 215 IPAddress local_; 216 IPAddress gateway_; 217 218 // Track the tethering status of the Service associated with this connection. 219 // This property is set by a service as it takes ownership of a connection, 220 // and is read by services that are bound through this connection. 221 std::string tethering_; 222 223 // A binder to a lower Connection that this Connection depends on, if any. 224 Binder lower_binder_; 225 226 // Binders to clients -- usually to upper connections or related services and 227 // devices. 228 std::deque<Binder*> binders_; 229 230 // Store cached copies of singletons for speed/ease of testing 231 const DeviceInfo* device_info_; 232 #if !defined(__ANDROID__) 233 Resolver* resolver_; 234 #else 235 DNSServerProxyFactory* dns_server_proxy_factory_; 236 std::unique_ptr<DNSServerProxy> dns_server_proxy_; 237 #endif // __ANDROID__; 238 RoutingTable* routing_table_; 239 RTNLHandler* rtnl_handler_; 240 241 ControlInterface* control_interface_; 242 std::unique_ptr<FirewallProxyInterface> firewall_proxy_; 243 244 DISALLOW_COPY_AND_ASSIGN(Connection); 245 }; 246 247 } // namespace shill 248 249 #endif // SHILL_CONNECTION_H_ 250