blob: 3f765783906df204fb2e6a2fd53058d77fb119b1 [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"
Paul Stewartdd60e452011-08-08 11:38:36 -070011#include "shill/resolver.h"
12#include "shill/routing_table.h"
13#include "shill/rtnl_handler.h"
Ben Chanfad4a0b2012-04-18 15:49:59 -070014#include "shill/scope_logger.h"
Paul Stewartdd60e452011-08-08 11:38:36 -070015
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 Stewart9a908082011-08-31 12:18:48 -070066 const DeviceInfo *device_info)
Darin Petkov13e6d552012-05-09 14:22:23 +020067 : weak_ptr_factory_(this),
68 is_default_(false),
Paul Stewartc8f4bef2011-12-13 09:45:51 -080069 routing_request_count_(0),
Paul Stewartdd60e452011-08-08 11:38:36 -070070 interface_index_(interface_index),
71 interface_name_(interface_name),
Paul Stewarte00600e2012-03-16 07:08:00 -070072 technology_(technology),
Darin Petkov13e6d552012-05-09 14:22:23 +020073 lower_binder_(
74 interface_name_,
75 // Connection owns a single instance of |lower_binder_| so it's safe
76 // to use an Unretained callback.
77 Bind(&Connection::OnLowerDisconnect, Unretained(this))),
Paul Stewart9a908082011-08-31 12:18:48 -070078 device_info_(device_info),
Paul Stewartdd60e452011-08-08 11:38:36 -070079 resolver_(Resolver::GetInstance()),
80 routing_table_(RoutingTable::GetInstance()),
81 rtnl_handler_(RTNLHandler::GetInstance()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -070082 SLOG(Connection, 2) << __func__ << "(" << interface_index << ", "
83 << interface_name << ", "
84 << Technology::NameFromIdentifier(technology) << ")";
Paul Stewartdd60e452011-08-08 11:38:36 -070085}
86
87Connection::~Connection() {
Ben Chanfad4a0b2012-04-18 15:49:59 -070088 SLOG(Connection, 2) << __func__ << " " << interface_name_;
Paul Stewart9a908082011-08-31 12:18:48 -070089
Darin Petkov13e6d552012-05-09 14:22:23 +020090 NotifyBindersOnDisconnect();
91
Paul Stewartc8f4bef2011-12-13 09:45:51 -080092 DCHECK(!routing_request_count_);
Thieu Lefb46caf2012-03-08 11:57:15 -080093 routing_table_->FlushRoutes(interface_index_);
Paul Stewarte93b0382012-04-24 13:11:28 -070094 routing_table_->FlushRoutesWithTag(interface_index_);
Paul Stewart9a908082011-08-31 12:18:48 -070095 device_info_->FlushAddresses(interface_index_);
Paul Stewartdd60e452011-08-08 11:38:36 -070096}
97
98void Connection::UpdateFromIPConfig(const IPConfigRefPtr &config) {
Ben Chanfad4a0b2012-04-18 15:49:59 -070099 SLOG(Connection, 2) << __func__ << " " << interface_name_;
Paul Stewarte6132022011-08-16 09:11:02 -0700100
Paul Stewart9a908082011-08-31 12:18:48 -0700101 const IPConfig::Properties &properties = config->properties();
Paul Stewarte93b0382012-04-24 13:11:28 -0700102 if (!properties.trusted_ip.empty() && !PinHostRoute(properties)) {
103 LOG(ERROR) << "Unable to pin host route to " << properties.trusted_ip;
104 return;
105 }
106
Paul Stewart9a908082011-08-31 12:18:48 -0700107 IPAddress local(properties.address_family);
108 if (!local.SetAddressFromString(properties.address)) {
109 LOG(ERROR) << "Local address " << properties.address << " is invalid";
110 return;
111 }
Paul Stewart48100b02012-03-19 07:53:52 -0700112 local.set_prefix(properties.subnet_prefix);
Paul Stewart9a908082011-08-31 12:18:48 -0700113
114 IPAddress broadcast(properties.address_family);
Paul Stewart1062d9d2012-04-27 10:42:27 -0700115 if (properties.broadcast_address.empty()) {
Paul Stewartfe1c0e12012-04-30 19:57:04 -0700116 if (properties.peer_address.empty()) {
Paul Stewart1062d9d2012-04-27 10:42:27 -0700117 LOG(WARNING) << "Broadcast address is not set. Using default.";
Paul Stewartfe1c0e12012-04-30 19:57:04 -0700118 broadcast = local.GetDefaultBroadcast();
Paul Stewart1062d9d2012-04-27 10:42:27 -0700119 }
120 } else if (!broadcast.SetAddressFromString(properties.broadcast_address)) {
Paul Stewart9a908082011-08-31 12:18:48 -0700121 LOG(ERROR) << "Broadcast address " << properties.broadcast_address
122 << " is invalid";
123 return;
124 }
125
Paul Stewart48100b02012-03-19 07:53:52 -0700126 IPAddress peer(properties.address_family);
127 if (!properties.peer_address.empty() &&
128 !peer.SetAddressFromString(properties.peer_address)) {
129 LOG(ERROR) << "Peer address " << properties.peer_address
130 << " is invalid";
131 return;
132 }
133
Paul Stewarte78ec542012-06-08 18:28:50 -0700134 IPAddress gateway(properties.address_family);
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700135 if (!properties.gateway.empty() &&
Paul Stewarte78ec542012-06-08 18:28:50 -0700136 !gateway.SetAddressFromString(properties.gateway)) {
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700137 LOG(ERROR) << "Gateway address " << properties.peer_address
138 << " is invalid";
139 return;
140 }
141
Paul Stewarte78ec542012-06-08 18:28:50 -0700142 if (!FixGatewayReachability(&local, &peer, gateway)) {
Paul Stewart53a30382012-04-26 09:06:59 -0700143 LOG(WARNING) << "Expect limited network connectivity.";
144 }
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700145
Paul Stewarte78ec542012-06-08 18:28:50 -0700146 LOG(INFO) << __func__ << ": Installing with parameters:"
147 << " local=" << local.ToString()
148 << " broadcast=" << broadcast.ToString()
149 << " peer=" << peer.ToString()
150 << " gateway=" << gateway.ToString();
Paul Stewart48100b02012-03-19 07:53:52 -0700151 rtnl_handler_->AddInterfaceAddress(interface_index_, local, broadcast, peer);
Paul Stewartdd60e452011-08-08 11:38:36 -0700152
Paul Stewarte78ec542012-06-08 18:28:50 -0700153 if (gateway.IsValid()) {
154 routing_table_->SetDefaultRoute(interface_index_, gateway,
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700155 GetMetric(is_default_));
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700156 }
Paul Stewartdd60e452011-08-08 11:38:36 -0700157
Paul Stewart3f68bb12012-03-15 13:33:10 -0700158 // Install any explicitly configured routes at the default metric.
159 routing_table_->ConfigureRoutes(interface_index_, config, kDefaultMetric);
160
Paul Stewartdd60e452011-08-08 11:38:36 -0700161 // Save a copy of the last non-null DNS config
162 if (!config->properties().dns_servers.empty()) {
163 dns_servers_ = config->properties().dns_servers;
164 dns_domain_search_ = config->properties().domain_search;
165 }
166
Paul Stewart10241e32012-04-23 18:15:06 -0700167 ipconfig_rpc_identifier_ = config->GetRpcIdentifier();
168
Paul Stewartdd60e452011-08-08 11:38:36 -0700169 if (is_default_) {
170 resolver_->SetDNSFromIPConfig(config);
171 }
172}
173
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800174void Connection::SetIsDefault(bool is_default) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700175 SLOG(Connection, 2) << __func__ << " " << interface_name_
176 << " (index " << interface_index_ << ") "
177 << is_default_ << " -> " << is_default;
Paul Stewartdd60e452011-08-08 11:38:36 -0700178 if (is_default == is_default_) {
179 return;
180 }
181
Paul Stewart7cfca042011-12-08 14:18:17 -0800182 routing_table_->SetDefaultMetric(interface_index_, GetMetric(is_default));
Paul Stewartdd60e452011-08-08 11:38:36 -0700183
Paul Stewartc681fa02012-03-02 19:40:04 -0800184 is_default_ = is_default;
185
Paul Stewartdd60e452011-08-08 11:38:36 -0700186 if (is_default) {
187 resolver_->SetDNSFromLists(dns_servers_, dns_domain_search_);
Paul Stewartc681fa02012-03-02 19:40:04 -0800188 DeviceRefPtr device = device_info_->GetDevice(interface_index_);
189 if (device) {
190 device->RequestPortalDetection();
191 }
Paul Stewartdd60e452011-08-08 11:38:36 -0700192 }
Paul Stewarte78ec542012-06-08 18:28:50 -0700193 routing_table_->FlushCache();
Paul Stewartdd60e452011-08-08 11:38:36 -0700194}
195
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800196void Connection::RequestRouting() {
197 if (routing_request_count_++ == 0) {
198 DeviceRefPtr device = device_info_->GetDevice(interface_index_);
199 DCHECK(device.get());
200 if (!device.get()) {
201 LOG(ERROR) << "Device is NULL!";
202 return;
203 }
204 device->DisableReversePathFilter();
205 }
206}
207
208void Connection::ReleaseRouting() {
209 DCHECK(routing_request_count_ > 0);
210 if (--routing_request_count_ == 0) {
211 DeviceRefPtr device = device_info_->GetDevice(interface_index_);
212 DCHECK(device.get());
213 if (!device.get()) {
214 LOG(ERROR) << "Device is NULL!";
215 return;
216 }
217 device->EnableReversePathFilter();
218
219 // Clear any cached routes that might have accumulated while reverse-path
220 // filtering was disabled.
221 routing_table_->FlushCache();
222 }
223}
224
Paul Stewartf748a362012-03-07 12:01:20 -0800225bool Connection::RequestHostRoute(const IPAddress &address) {
226 // Set the prefix to be the entire address size.
227 IPAddress address_prefix(address);
228 address_prefix.set_prefix(address_prefix.GetLength() * 8);
229
Darin Petkov13e6d552012-05-09 14:22:23 +0200230 // Do not set interface_index_ since this may not be the default route through
231 // which this destination can be found. However, we should tag the created
232 // route with our interface index so we can clean this route up when this
233 // connection closes. Also, add route query callback to determine the lower
234 // connection and bind to it.
235 if (!routing_table_->RequestRouteToHost(
236 address_prefix,
237 -1,
238 interface_index_,
Darin Petkov5eb05422012-05-11 15:45:25 +0200239 Bind(&Connection::OnRouteQueryResponse,
240 weak_ptr_factory_.GetWeakPtr()))) {
Paul Stewartf748a362012-03-07 12:01:20 -0800241 LOG(ERROR) << "Could not request route to " << address.ToString();
242 return false;
243 }
244
245 return true;
246}
247
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700248// static
Paul Stewart53a30382012-04-26 09:06:59 -0700249bool Connection::FixGatewayReachability(IPAddress *local,
Paul Stewart49258292012-05-26 06:37:14 -0700250 IPAddress *peer,
251 const IPAddress &gateway) {
Paul Stewart53a30382012-04-26 09:06:59 -0700252 if (!gateway.IsValid()) {
253 LOG(WARNING) << "No gateway address was provided for this connection.";
254 return false;
255 }
256
Paul Stewart49258292012-05-26 06:37:14 -0700257 if (peer->IsValid()) {
258 if (gateway.Equals(*peer)) {
Paul Stewart53a30382012-04-26 09:06:59 -0700259 return true;
260 }
261 LOG(WARNING) << "Gateway address "
262 << gateway.ToString()
263 << " does not match peer address "
Paul Stewart49258292012-05-26 06:37:14 -0700264 << peer->ToString();
Paul Stewart53a30382012-04-26 09:06:59 -0700265 return false;
266 }
267
268 if (local->CanReachAddress(gateway)) {
269 return true;
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700270 }
271
272 LOG(WARNING) << "Gateway "
273 << gateway.ToString()
274 << " is unreachable from local address/prefix "
275 << local->ToString() << "/" << local->prefix();
276
Paul Stewart2aa5d7d2012-06-21 22:16:54 -0700277 bool found_new_prefix = false;
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700278 size_t original_prefix = local->prefix();
Paul Stewart2aa5d7d2012-06-21 22:16:54 -0700279 // Only try to expand the netmask if the configured prefix is
280 // less than "all ones". This special-cases the "all-ones"
281 // prefix as a forced conversion to point-to-point networking.
282 if (local->prefix() < IPAddress::GetMaxPrefixLength(local->family())) {
283 size_t prefix = original_prefix - 1;
284 for (; prefix >= local->GetMinPrefixLength(); --prefix) {
285 local->set_prefix(prefix);
286 if (local->CanReachAddress(gateway)) {
287 found_new_prefix = true;
288 break;
289 }
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700290 }
291 }
292
Paul Stewart2aa5d7d2012-06-21 22:16:54 -0700293 if (!found_new_prefix) {
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700294 // Restore the original prefix since we cannot find a better one.
295 local->set_prefix(original_prefix);
Paul Stewart49258292012-05-26 06:37:14 -0700296 DCHECK(!peer->IsValid());
297 LOG(WARNING) << "Assuming point-to-point configuration.";
298 *peer = gateway;
299 return true;
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700300 }
Paul Stewart53a30382012-04-26 09:06:59 -0700301
Paul Stewart2aa5d7d2012-06-21 22:16:54 -0700302 LOG(WARNING) << "Mitigating this by setting local prefix to "
303 << local->prefix();
Paul Stewart53a30382012-04-26 09:06:59 -0700304 return true;
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700305}
306
Paul Stewart7cfca042011-12-08 14:18:17 -0800307uint32 Connection::GetMetric(bool is_default) {
308 // If this is not the default route, assign a metric based on the interface
309 // index. This way all non-default routes (even to the same gateway IP) end
310 // up with unique metrics so they do not collide.
311 return is_default ? kDefaultMetric : kNonDefaultMetricBase + interface_index_;
312}
313
Paul Stewarte93b0382012-04-24 13:11:28 -0700314bool Connection::PinHostRoute(const IPConfig::Properties &properties) {
315 SLOG(Connection, 2) << __func__;
316 if (properties.gateway.empty() || properties.trusted_ip.empty()) {
317 return false;
318 }
319
320 IPAddress trusted_ip(properties.address_family);
321 if (!trusted_ip.SetAddressFromString(properties.trusted_ip)) {
322 LOG(ERROR) << "Failed to parse trusted_ip "
323 << properties.trusted_ip << "; ignored.";
324 return false;
325 }
326
327 return RequestHostRoute(trusted_ip);
328}
329
Darin Petkov13e6d552012-05-09 14:22:23 +0200330void Connection::OnRouteQueryResponse(int interface_index,
331 const RoutingTableEntry &entry) {
332 SLOG(Connection, 2) << __func__ << "(" << interface_index << ", "
Darin Petkov5eb05422012-05-11 15:45:25 +0200333 << entry.tag << ")" << " @ " << interface_name_;
Darin Petkov13e6d552012-05-09 14:22:23 +0200334 lower_binder_.Attach(NULL);
335 DeviceRefPtr device = device_info_->GetDevice(interface_index);
336 if (!device) {
337 LOG(ERROR) << "Unable to lookup device for index " << interface_index;
338 return;
339 }
340 ConnectionRefPtr connection = device->connection();
341 if (!connection) {
342 LOG(ERROR) << "Device " << interface_index << " has no connection.";
343 return;
344 }
345 lower_binder_.Attach(connection);
346}
347
348void Connection::OnLowerDisconnect() {
Darin Petkov5eb05422012-05-11 15:45:25 +0200349 SLOG(Connection, 2) << __func__ << " @ " << interface_name_;
Darin Petkov13e6d552012-05-09 14:22:23 +0200350 // Ensures that |this| instance doesn't get destroyed in the middle of
351 // notifying the binders. This method needs to be separate from
352 // NotifyBindersOnDisconnect because the latter may be invoked by Connection's
353 // destructor when |this| instance's reference count is already 0.
354 ConnectionRefPtr connection(this);
355 connection->NotifyBindersOnDisconnect();
356}
357
358void Connection::NotifyBindersOnDisconnect() {
359 // Note that this method may be invoked by the destructor.
Darin Petkov5eb05422012-05-11 15:45:25 +0200360 SLOG(Connection, 2) << __func__ << " @ " << interface_name_;
Darin Petkov13e6d552012-05-09 14:22:23 +0200361
362 // Unbinds the lower connection before notifying the binders. This ensures
363 // correct behavior in case of circular binding.
364 lower_binder_.Attach(NULL);
365 while (!binders_.empty()) {
366 // Pop the binder first and then notify it to ensure that each binder is
367 // notified only once.
368 Binder *binder = binders_.front();
369 binders_.pop_front();
370 binder->OnDisconnect();
371 }
372}
373
374void Connection::AttachBinder(Binder *binder) {
Darin Petkov5eb05422012-05-11 15:45:25 +0200375 SLOG(Connection, 2) << __func__ << "(" << binder->name() << ")" << " @ "
376 << interface_name_;
Darin Petkov13e6d552012-05-09 14:22:23 +0200377 binders_.push_back(binder);
378}
379
380void Connection::DetachBinder(Binder *binder) {
Darin Petkov5eb05422012-05-11 15:45:25 +0200381 SLOG(Connection, 2) << __func__ << "(" << binder->name() << ")" << " @ "
382 << interface_name_;
Darin Petkov13e6d552012-05-09 14:22:23 +0200383 for (deque<Binder *>::iterator it = binders_.begin();
384 it != binders_.end(); ++it) {
385 if (binder == *it) {
386 binders_.erase(it);
387 return;
388 }
389 }
390}
391
Paul Stewartdd60e452011-08-08 11:38:36 -0700392} // namespace shill