blob: 55ec2b463332c570b1d4d99e656baafefb4548cd [file] [log] [blame]
mukesh agrawalddc378f2012-02-17 18:26:20 -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
5#include "shill/connection.h"
6
Paul Stewart9a908082011-08-31 12:18:48 -07007#include <arpa/inet.h>
8#include <linux/rtnetlink.h>
9
10#include "shill/device_info.h"
Christopher Wileyb691efd2012-08-09 13:51:51 -070011#include "shill/logging.h"
Paul Stewartdd60e452011-08-08 11:38:36 -070012#include "shill/resolver.h"
13#include "shill/routing_table.h"
14#include "shill/rtnl_handler.h"
15
Darin Petkov13e6d552012-05-09 14:22:23 +020016using base::Bind;
17using base::Closure;
18using base::Unretained;
19using std::deque;
Paul Stewartdd60e452011-08-08 11:38:36 -070020using std::string;
21
22namespace shill {
23
24// static
25const uint32 Connection::kDefaultMetric = 1;
26// static
Paul Stewart7cfca042011-12-08 14:18:17 -080027const uint32 Connection::kNonDefaultMetricBase = 10;
Paul Stewartdd60e452011-08-08 11:38:36 -070028
Darin Petkov13e6d552012-05-09 14:22:23 +020029Connection::Binder::Binder(const string &name,
30 const Closure &disconnect_callback)
31 : name_(name),
Darin Petkov13e6d552012-05-09 14:22:23 +020032 client_disconnect_callback_(disconnect_callback) {}
33
34Connection::Binder::~Binder() {
35 Attach(NULL);
36}
37
Darin Petkovef1f9fe2012-05-11 16:51:52 +020038void Connection::Binder::Attach(const ConnectionRefPtr &to_connection) {
Darin Petkov13e6d552012-05-09 14:22:23 +020039 if (connection_) {
40 connection_->DetachBinder(this);
41 LOG(INFO) << name_ << ": unbound from connection: "
42 << connection_->interface_name();
Darin Petkovef1f9fe2012-05-11 16:51:52 +020043 connection_.reset();
Darin Petkov13e6d552012-05-09 14:22:23 +020044 }
Darin Petkovef1f9fe2012-05-11 16:51:52 +020045 if (to_connection) {
46 connection_ = to_connection->weak_ptr_factory_.GetWeakPtr();
Darin Petkov13e6d552012-05-09 14:22:23 +020047 connection_->AttachBinder(this);
48 LOG(INFO) << name_ << ": bound to connection: "
49 << connection_->interface_name();
50 }
51}
52
53void Connection::Binder::OnDisconnect() {
54 LOG(INFO) << name_ << ": bound connection disconnected: "
55 << connection_->interface_name();
Darin Petkovef1f9fe2012-05-11 16:51:52 +020056 connection_.reset();
Darin Petkov13e6d552012-05-09 14:22:23 +020057 if (!client_disconnect_callback_.is_null()) {
58 SLOG(Connection, 2) << "Running client disconnect callback.";
59 client_disconnect_callback_.Run();
60 }
61}
62
Paul Stewart9a908082011-08-31 12:18:48 -070063Connection::Connection(int interface_index,
64 const std::string& interface_name,
Paul Stewarte00600e2012-03-16 07:08:00 -070065 Technology::Identifier technology,
Paul Stewartbf667612012-06-29 14:49:54 -070066 const DeviceInfo *device_info,
67 bool is_short_dns_timeout_enabled)
Darin Petkov13e6d552012-05-09 14:22:23 +020068 : weak_ptr_factory_(this),
69 is_default_(false),
Paul Stewart4a6748d2012-07-17 14:31:36 -070070 has_broadcast_domain_(false),
Paul Stewartc8f4bef2011-12-13 09:45:51 -080071 routing_request_count_(0),
Paul Stewartdd60e452011-08-08 11:38:36 -070072 interface_index_(interface_index),
73 interface_name_(interface_name),
Paul Stewarte00600e2012-03-16 07:08:00 -070074 technology_(technology),
Paul Stewart4a6748d2012-07-17 14:31:36 -070075 local_(IPAddress::kFamilyUnknown),
76 gateway_(IPAddress::kFamilyUnknown),
Darin Petkov13e6d552012-05-09 14:22:23 +020077 lower_binder_(
78 interface_name_,
79 // Connection owns a single instance of |lower_binder_| so it's safe
80 // to use an Unretained callback.
81 Bind(&Connection::OnLowerDisconnect, Unretained(this))),
Paul Stewartbf667612012-06-29 14:49:54 -070082 dns_timeout_parameters_(Resolver::kDefaultTimeout),
Paul Stewart9a908082011-08-31 12:18:48 -070083 device_info_(device_info),
Paul Stewartdd60e452011-08-08 11:38:36 -070084 resolver_(Resolver::GetInstance()),
85 routing_table_(RoutingTable::GetInstance()),
86 rtnl_handler_(RTNLHandler::GetInstance()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -070087 SLOG(Connection, 2) << __func__ << "(" << interface_index << ", "
88 << interface_name << ", "
89 << Technology::NameFromIdentifier(technology) << ")";
Paul Stewartbf667612012-06-29 14:49:54 -070090 if (is_short_dns_timeout_enabled) {
91 dns_timeout_parameters_ = Resolver::kShortTimeout;
92 }
Paul Stewartdd60e452011-08-08 11:38:36 -070093}
94
95Connection::~Connection() {
Ben Chanfad4a0b2012-04-18 15:49:59 -070096 SLOG(Connection, 2) << __func__ << " " << interface_name_;
Paul Stewart9a908082011-08-31 12:18:48 -070097
Darin Petkov13e6d552012-05-09 14:22:23 +020098 NotifyBindersOnDisconnect();
99
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800100 DCHECK(!routing_request_count_);
Thieu Lefb46caf2012-03-08 11:57:15 -0800101 routing_table_->FlushRoutes(interface_index_);
Paul Stewarte93b0382012-04-24 13:11:28 -0700102 routing_table_->FlushRoutesWithTag(interface_index_);
Paul Stewart9a908082011-08-31 12:18:48 -0700103 device_info_->FlushAddresses(interface_index_);
Paul Stewartdd60e452011-08-08 11:38:36 -0700104}
105
106void Connection::UpdateFromIPConfig(const IPConfigRefPtr &config) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700107 SLOG(Connection, 2) << __func__ << " " << interface_name_;
Paul Stewarte6132022011-08-16 09:11:02 -0700108
Paul Stewart9a908082011-08-31 12:18:48 -0700109 const IPConfig::Properties &properties = config->properties();
Paul Stewarte93b0382012-04-24 13:11:28 -0700110 if (!properties.trusted_ip.empty() && !PinHostRoute(properties)) {
111 LOG(ERROR) << "Unable to pin host route to " << properties.trusted_ip;
112 return;
113 }
114
Paul Stewart9a908082011-08-31 12:18:48 -0700115 IPAddress local(properties.address_family);
116 if (!local.SetAddressFromString(properties.address)) {
117 LOG(ERROR) << "Local address " << properties.address << " is invalid";
118 return;
119 }
Paul Stewart48100b02012-03-19 07:53:52 -0700120 local.set_prefix(properties.subnet_prefix);
Paul Stewart9a908082011-08-31 12:18:48 -0700121
122 IPAddress broadcast(properties.address_family);
Paul Stewart1062d9d2012-04-27 10:42:27 -0700123 if (properties.broadcast_address.empty()) {
Paul Stewartfe1c0e12012-04-30 19:57:04 -0700124 if (properties.peer_address.empty()) {
Paul Stewart1062d9d2012-04-27 10:42:27 -0700125 LOG(WARNING) << "Broadcast address is not set. Using default.";
Paul Stewartfe1c0e12012-04-30 19:57:04 -0700126 broadcast = local.GetDefaultBroadcast();
Paul Stewart1062d9d2012-04-27 10:42:27 -0700127 }
128 } else if (!broadcast.SetAddressFromString(properties.broadcast_address)) {
Paul Stewart9a908082011-08-31 12:18:48 -0700129 LOG(ERROR) << "Broadcast address " << properties.broadcast_address
130 << " is invalid";
131 return;
132 }
133
Paul Stewart48100b02012-03-19 07:53:52 -0700134 IPAddress peer(properties.address_family);
135 if (!properties.peer_address.empty() &&
136 !peer.SetAddressFromString(properties.peer_address)) {
137 LOG(ERROR) << "Peer address " << properties.peer_address
138 << " is invalid";
139 return;
140 }
141
Paul Stewarte78ec542012-06-08 18:28:50 -0700142 IPAddress gateway(properties.address_family);
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700143 if (!properties.gateway.empty() &&
Paul Stewarte78ec542012-06-08 18:28:50 -0700144 !gateway.SetAddressFromString(properties.gateway)) {
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700145 LOG(ERROR) << "Gateway address " << properties.peer_address
146 << " is invalid";
147 return;
148 }
149
Paul Stewarte78ec542012-06-08 18:28:50 -0700150 if (!FixGatewayReachability(&local, &peer, gateway)) {
Paul Stewart53a30382012-04-26 09:06:59 -0700151 LOG(WARNING) << "Expect limited network connectivity.";
152 }
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700153
Paul Stewart05a42c22012-08-02 16:47:21 -0700154 if (device_info_->HasOtherAddress(interface_index_, local)) {
155 // The address has changed for this interface. We need to flush
156 // everything and start over.
157 LOG(INFO) << __func__ << ": Flushing old addresses and routes.";
158 routing_table_->FlushRoutes(interface_index_);
159 device_info_->FlushAddresses(interface_index_);
160 }
161
Paul Stewarte78ec542012-06-08 18:28:50 -0700162 LOG(INFO) << __func__ << ": Installing with parameters:"
163 << " local=" << local.ToString()
164 << " broadcast=" << broadcast.ToString()
165 << " peer=" << peer.ToString()
166 << " gateway=" << gateway.ToString();
Paul Stewart48100b02012-03-19 07:53:52 -0700167 rtnl_handler_->AddInterfaceAddress(interface_index_, local, broadcast, peer);
Paul Stewartdd60e452011-08-08 11:38:36 -0700168
Paul Stewarte78ec542012-06-08 18:28:50 -0700169 if (gateway.IsValid()) {
170 routing_table_->SetDefaultRoute(interface_index_, gateway,
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700171 GetMetric(is_default_));
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700172 }
Paul Stewartdd60e452011-08-08 11:38:36 -0700173
Paul Stewart3f68bb12012-03-15 13:33:10 -0700174 // Install any explicitly configured routes at the default metric.
175 routing_table_->ConfigureRoutes(interface_index_, config, kDefaultMetric);
176
Paul Stewartdd60e452011-08-08 11:38:36 -0700177 // Save a copy of the last non-null DNS config
178 if (!config->properties().dns_servers.empty()) {
179 dns_servers_ = config->properties().dns_servers;
180 dns_domain_search_ = config->properties().domain_search;
181 }
182
Paul Stewart10241e32012-04-23 18:15:06 -0700183 ipconfig_rpc_identifier_ = config->GetRpcIdentifier();
184
Paul Stewartdd60e452011-08-08 11:38:36 -0700185 if (is_default_) {
Paul Stewartbf667612012-06-29 14:49:54 -0700186 resolver_->SetDNSFromIPConfig(config, dns_timeout_parameters_);
Paul Stewartdd60e452011-08-08 11:38:36 -0700187 }
Paul Stewart4a6748d2012-07-17 14:31:36 -0700188
189 local_ = local;
190 gateway_ = gateway;
191 has_broadcast_domain_ = !peer.IsValid();
Paul Stewartdd60e452011-08-08 11:38:36 -0700192}
193
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800194void Connection::SetIsDefault(bool is_default) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700195 SLOG(Connection, 2) << __func__ << " " << interface_name_
196 << " (index " << interface_index_ << ") "
197 << is_default_ << " -> " << is_default;
Paul Stewartdd60e452011-08-08 11:38:36 -0700198 if (is_default == is_default_) {
199 return;
200 }
201
Paul Stewart7cfca042011-12-08 14:18:17 -0800202 routing_table_->SetDefaultMetric(interface_index_, GetMetric(is_default));
Paul Stewartdd60e452011-08-08 11:38:36 -0700203
Paul Stewartc681fa02012-03-02 19:40:04 -0800204 is_default_ = is_default;
205
Paul Stewartdd60e452011-08-08 11:38:36 -0700206 if (is_default) {
Paul Stewartbf667612012-06-29 14:49:54 -0700207 resolver_->SetDNSFromLists(dns_servers_, dns_domain_search_,
208 dns_timeout_parameters_);
Paul Stewartc681fa02012-03-02 19:40:04 -0800209 DeviceRefPtr device = device_info_->GetDevice(interface_index_);
210 if (device) {
211 device->RequestPortalDetection();
212 }
Paul Stewartdd60e452011-08-08 11:38:36 -0700213 }
Paul Stewarte78ec542012-06-08 18:28:50 -0700214 routing_table_->FlushCache();
Paul Stewartdd60e452011-08-08 11:38:36 -0700215}
216
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800217void Connection::RequestRouting() {
218 if (routing_request_count_++ == 0) {
219 DeviceRefPtr device = device_info_->GetDevice(interface_index_);
220 DCHECK(device.get());
221 if (!device.get()) {
222 LOG(ERROR) << "Device is NULL!";
223 return;
224 }
225 device->DisableReversePathFilter();
226 }
227}
228
229void Connection::ReleaseRouting() {
230 DCHECK(routing_request_count_ > 0);
231 if (--routing_request_count_ == 0) {
232 DeviceRefPtr device = device_info_->GetDevice(interface_index_);
233 DCHECK(device.get());
234 if (!device.get()) {
235 LOG(ERROR) << "Device is NULL!";
236 return;
237 }
238 device->EnableReversePathFilter();
239
240 // Clear any cached routes that might have accumulated while reverse-path
241 // filtering was disabled.
242 routing_table_->FlushCache();
243 }
244}
245
Paul Stewartf748a362012-03-07 12:01:20 -0800246bool Connection::RequestHostRoute(const IPAddress &address) {
247 // Set the prefix to be the entire address size.
248 IPAddress address_prefix(address);
249 address_prefix.set_prefix(address_prefix.GetLength() * 8);
250
Darin Petkov13e6d552012-05-09 14:22:23 +0200251 // Do not set interface_index_ since this may not be the default route through
252 // which this destination can be found. However, we should tag the created
253 // route with our interface index so we can clean this route up when this
254 // connection closes. Also, add route query callback to determine the lower
255 // connection and bind to it.
256 if (!routing_table_->RequestRouteToHost(
257 address_prefix,
258 -1,
259 interface_index_,
Darin Petkov5eb05422012-05-11 15:45:25 +0200260 Bind(&Connection::OnRouteQueryResponse,
261 weak_ptr_factory_.GetWeakPtr()))) {
Paul Stewartf748a362012-03-07 12:01:20 -0800262 LOG(ERROR) << "Could not request route to " << address.ToString();
263 return false;
264 }
265
266 return true;
267}
268
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700269// static
Paul Stewart53a30382012-04-26 09:06:59 -0700270bool Connection::FixGatewayReachability(IPAddress *local,
Paul Stewart49258292012-05-26 06:37:14 -0700271 IPAddress *peer,
272 const IPAddress &gateway) {
Paul Stewart53a30382012-04-26 09:06:59 -0700273 if (!gateway.IsValid()) {
274 LOG(WARNING) << "No gateway address was provided for this connection.";
275 return false;
276 }
277
Paul Stewart49258292012-05-26 06:37:14 -0700278 if (peer->IsValid()) {
279 if (gateway.Equals(*peer)) {
Paul Stewart53a30382012-04-26 09:06:59 -0700280 return true;
281 }
282 LOG(WARNING) << "Gateway address "
283 << gateway.ToString()
284 << " does not match peer address "
Paul Stewart49258292012-05-26 06:37:14 -0700285 << peer->ToString();
Paul Stewart53a30382012-04-26 09:06:59 -0700286 return false;
287 }
288
289 if (local->CanReachAddress(gateway)) {
290 return true;
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700291 }
292
293 LOG(WARNING) << "Gateway "
294 << gateway.ToString()
295 << " is unreachable from local address/prefix "
296 << local->ToString() << "/" << local->prefix();
297
Paul Stewart2aa5d7d2012-06-21 22:16:54 -0700298 bool found_new_prefix = false;
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700299 size_t original_prefix = local->prefix();
Paul Stewart2aa5d7d2012-06-21 22:16:54 -0700300 // Only try to expand the netmask if the configured prefix is
301 // less than "all ones". This special-cases the "all-ones"
302 // prefix as a forced conversion to point-to-point networking.
303 if (local->prefix() < IPAddress::GetMaxPrefixLength(local->family())) {
304 size_t prefix = original_prefix - 1;
305 for (; prefix >= local->GetMinPrefixLength(); --prefix) {
306 local->set_prefix(prefix);
307 if (local->CanReachAddress(gateway)) {
308 found_new_prefix = true;
309 break;
310 }
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700311 }
312 }
313
Paul Stewart2aa5d7d2012-06-21 22:16:54 -0700314 if (!found_new_prefix) {
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700315 // Restore the original prefix since we cannot find a better one.
316 local->set_prefix(original_prefix);
Paul Stewart49258292012-05-26 06:37:14 -0700317 DCHECK(!peer->IsValid());
318 LOG(WARNING) << "Assuming point-to-point configuration.";
319 *peer = gateway;
320 return true;
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700321 }
Paul Stewart53a30382012-04-26 09:06:59 -0700322
Paul Stewart2aa5d7d2012-06-21 22:16:54 -0700323 LOG(WARNING) << "Mitigating this by setting local prefix to "
324 << local->prefix();
Paul Stewart53a30382012-04-26 09:06:59 -0700325 return true;
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700326}
327
Paul Stewart7cfca042011-12-08 14:18:17 -0800328uint32 Connection::GetMetric(bool is_default) {
329 // If this is not the default route, assign a metric based on the interface
330 // index. This way all non-default routes (even to the same gateway IP) end
331 // up with unique metrics so they do not collide.
332 return is_default ? kDefaultMetric : kNonDefaultMetricBase + interface_index_;
333}
334
Paul Stewarte93b0382012-04-24 13:11:28 -0700335bool Connection::PinHostRoute(const IPConfig::Properties &properties) {
336 SLOG(Connection, 2) << __func__;
337 if (properties.gateway.empty() || properties.trusted_ip.empty()) {
Darin Petkove8587e32012-07-02 13:56:07 +0200338 LOG_IF(ERROR, properties.gateway.empty())
339 << "No gateway -- unable to pin host route.";
340 LOG_IF(ERROR, properties.trusted_ip.empty())
341 << "No trusted IP -- unable to pin host route.";
Paul Stewarte93b0382012-04-24 13:11:28 -0700342 return false;
343 }
344
345 IPAddress trusted_ip(properties.address_family);
346 if (!trusted_ip.SetAddressFromString(properties.trusted_ip)) {
347 LOG(ERROR) << "Failed to parse trusted_ip "
348 << properties.trusted_ip << "; ignored.";
349 return false;
350 }
351
352 return RequestHostRoute(trusted_ip);
353}
354
Darin Petkov13e6d552012-05-09 14:22:23 +0200355void Connection::OnRouteQueryResponse(int interface_index,
356 const RoutingTableEntry &entry) {
357 SLOG(Connection, 2) << __func__ << "(" << interface_index << ", "
Darin Petkov5eb05422012-05-11 15:45:25 +0200358 << entry.tag << ")" << " @ " << interface_name_;
Darin Petkov13e6d552012-05-09 14:22:23 +0200359 lower_binder_.Attach(NULL);
360 DeviceRefPtr device = device_info_->GetDevice(interface_index);
361 if (!device) {
362 LOG(ERROR) << "Unable to lookup device for index " << interface_index;
363 return;
364 }
365 ConnectionRefPtr connection = device->connection();
366 if (!connection) {
367 LOG(ERROR) << "Device " << interface_index << " has no connection.";
368 return;
369 }
370 lower_binder_.Attach(connection);
Paul Stewart4a6748d2012-07-17 14:31:36 -0700371 connection->CreateGatewayRoute();
372}
373
374bool Connection::CreateGatewayRoute() {
375 // Ensure that the gateway for the lower connection remains reachable,
376 // since we may create routes that conflict with it.
377 if (!has_broadcast_domain_) {
378 return false;
379 }
380 // It is not worth keeping track of this route, since it is benign,
381 // and only pins persistent state that was already true of the connection.
382 // If DHCP parameters change later (without the connection having been
383 // destroyed and recreated), the binding processes will likely terminate
384 // and restart, causing a new link route to be created.
385 return routing_table_->CreateLinkRoute(interface_index_, local_, gateway_);
Darin Petkov13e6d552012-05-09 14:22:23 +0200386}
387
388void Connection::OnLowerDisconnect() {
Darin Petkov5eb05422012-05-11 15:45:25 +0200389 SLOG(Connection, 2) << __func__ << " @ " << interface_name_;
Darin Petkov13e6d552012-05-09 14:22:23 +0200390 // Ensures that |this| instance doesn't get destroyed in the middle of
391 // notifying the binders. This method needs to be separate from
392 // NotifyBindersOnDisconnect because the latter may be invoked by Connection's
393 // destructor when |this| instance's reference count is already 0.
394 ConnectionRefPtr connection(this);
395 connection->NotifyBindersOnDisconnect();
396}
397
398void Connection::NotifyBindersOnDisconnect() {
399 // Note that this method may be invoked by the destructor.
Darin Petkov5eb05422012-05-11 15:45:25 +0200400 SLOG(Connection, 2) << __func__ << " @ " << interface_name_;
Darin Petkov13e6d552012-05-09 14:22:23 +0200401
402 // Unbinds the lower connection before notifying the binders. This ensures
403 // correct behavior in case of circular binding.
404 lower_binder_.Attach(NULL);
405 while (!binders_.empty()) {
406 // Pop the binder first and then notify it to ensure that each binder is
407 // notified only once.
408 Binder *binder = binders_.front();
409 binders_.pop_front();
410 binder->OnDisconnect();
411 }
412}
413
414void Connection::AttachBinder(Binder *binder) {
Darin Petkov5eb05422012-05-11 15:45:25 +0200415 SLOG(Connection, 2) << __func__ << "(" << binder->name() << ")" << " @ "
416 << interface_name_;
Darin Petkov13e6d552012-05-09 14:22:23 +0200417 binders_.push_back(binder);
418}
419
420void Connection::DetachBinder(Binder *binder) {
Darin Petkov5eb05422012-05-11 15:45:25 +0200421 SLOG(Connection, 2) << __func__ << "(" << binder->name() << ")" << " @ "
422 << interface_name_;
Darin Petkov13e6d552012-05-09 14:22:23 +0200423 for (deque<Binder *>::iterator it = binders_.begin();
424 it != binders_.end(); ++it) {
425 if (binder == *it) {
426 binders_.erase(it);
427 return;
428 }
429 }
430}
431
Paul Stewartdd60e452011-08-08 11:38:36 -0700432} // namespace shill