blob: 794c25540f91c9e6385e5633bcdc90c66d63470d [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 Stewart5b7ba8c2012-04-18 09:08:00 -0700134 IPAddress gateway_address(properties.address_family);
135 if (!properties.gateway.empty() &&
136 !gateway_address.SetAddressFromString(properties.gateway)) {
137 LOG(ERROR) << "Gateway address " << properties.peer_address
138 << " is invalid";
139 return;
140 }
141
Paul Stewart53a30382012-04-26 09:06:59 -0700142 if (!FixGatewayReachability(&local, gateway_address, peer)) {
143 LOG(WARNING) << "Expect limited network connectivity.";
144 }
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700145
Paul Stewart48100b02012-03-19 07:53:52 -0700146 rtnl_handler_->AddInterfaceAddress(interface_index_, local, broadcast, peer);
Paul Stewartdd60e452011-08-08 11:38:36 -0700147
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700148 if (gateway_address.IsValid()) {
149 routing_table_->SetDefaultRoute(interface_index_, gateway_address,
150 GetMetric(is_default_));
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700151 }
Paul Stewartdd60e452011-08-08 11:38:36 -0700152
Paul Stewart3f68bb12012-03-15 13:33:10 -0700153 // Install any explicitly configured routes at the default metric.
154 routing_table_->ConfigureRoutes(interface_index_, config, kDefaultMetric);
155
Paul Stewartdd60e452011-08-08 11:38:36 -0700156 // Save a copy of the last non-null DNS config
157 if (!config->properties().dns_servers.empty()) {
158 dns_servers_ = config->properties().dns_servers;
159 dns_domain_search_ = config->properties().domain_search;
160 }
161
Paul Stewart10241e32012-04-23 18:15:06 -0700162 ipconfig_rpc_identifier_ = config->GetRpcIdentifier();
163
Paul Stewartdd60e452011-08-08 11:38:36 -0700164 if (is_default_) {
165 resolver_->SetDNSFromIPConfig(config);
166 }
167}
168
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800169void Connection::SetIsDefault(bool is_default) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700170 SLOG(Connection, 2) << __func__ << " " << interface_name_
171 << " (index " << interface_index_ << ") "
172 << is_default_ << " -> " << is_default;
Paul Stewartdd60e452011-08-08 11:38:36 -0700173 if (is_default == is_default_) {
174 return;
175 }
176
Paul Stewart7cfca042011-12-08 14:18:17 -0800177 routing_table_->SetDefaultMetric(interface_index_, GetMetric(is_default));
Paul Stewartdd60e452011-08-08 11:38:36 -0700178
Paul Stewartc681fa02012-03-02 19:40:04 -0800179 is_default_ = is_default;
180
Paul Stewartdd60e452011-08-08 11:38:36 -0700181 if (is_default) {
182 resolver_->SetDNSFromLists(dns_servers_, dns_domain_search_);
Paul Stewartc681fa02012-03-02 19:40:04 -0800183 DeviceRefPtr device = device_info_->GetDevice(interface_index_);
184 if (device) {
185 device->RequestPortalDetection();
186 }
Paul Stewartdd60e452011-08-08 11:38:36 -0700187 }
Paul Stewartdd60e452011-08-08 11:38:36 -0700188}
189
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800190void Connection::RequestRouting() {
191 if (routing_request_count_++ == 0) {
192 DeviceRefPtr device = device_info_->GetDevice(interface_index_);
193 DCHECK(device.get());
194 if (!device.get()) {
195 LOG(ERROR) << "Device is NULL!";
196 return;
197 }
198 device->DisableReversePathFilter();
199 }
200}
201
202void Connection::ReleaseRouting() {
203 DCHECK(routing_request_count_ > 0);
204 if (--routing_request_count_ == 0) {
205 DeviceRefPtr device = device_info_->GetDevice(interface_index_);
206 DCHECK(device.get());
207 if (!device.get()) {
208 LOG(ERROR) << "Device is NULL!";
209 return;
210 }
211 device->EnableReversePathFilter();
212
213 // Clear any cached routes that might have accumulated while reverse-path
214 // filtering was disabled.
215 routing_table_->FlushCache();
216 }
217}
218
Paul Stewartf748a362012-03-07 12:01:20 -0800219bool Connection::RequestHostRoute(const IPAddress &address) {
220 // Set the prefix to be the entire address size.
221 IPAddress address_prefix(address);
222 address_prefix.set_prefix(address_prefix.GetLength() * 8);
223
Darin Petkov13e6d552012-05-09 14:22:23 +0200224 // Do not set interface_index_ since this may not be the default route through
225 // which this destination can be found. However, we should tag the created
226 // route with our interface index so we can clean this route up when this
227 // connection closes. Also, add route query callback to determine the lower
228 // connection and bind to it.
229 if (!routing_table_->RequestRouteToHost(
230 address_prefix,
231 -1,
232 interface_index_,
233 RoutingTable::Query::Callback(
234 Bind(&Connection::OnRouteQueryResponse,
235 weak_ptr_factory_.GetWeakPtr())))) {
Paul Stewartf748a362012-03-07 12:01:20 -0800236 LOG(ERROR) << "Could not request route to " << address.ToString();
237 return false;
238 }
239
240 return true;
241}
242
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700243// static
Paul Stewart53a30382012-04-26 09:06:59 -0700244bool Connection::FixGatewayReachability(IPAddress *local,
245 const IPAddress &gateway,
246 const IPAddress &peer) {
247 if (!gateway.IsValid()) {
248 LOG(WARNING) << "No gateway address was provided for this connection.";
249 return false;
250 }
251
252 if (peer.IsValid()) {
253 if (gateway.Equals(peer)) {
254 return true;
255 }
256 LOG(WARNING) << "Gateway address "
257 << gateway.ToString()
258 << " does not match peer address "
259 << peer.ToString();
260 return false;
261 }
262
263 if (local->CanReachAddress(gateway)) {
264 return true;
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700265 }
266
267 LOG(WARNING) << "Gateway "
268 << gateway.ToString()
269 << " is unreachable from local address/prefix "
270 << local->ToString() << "/" << local->prefix();
271
272 size_t original_prefix = local->prefix();
273 size_t prefix = original_prefix - 1;
274 for (; prefix >= local->GetMinPrefixLength(); --prefix) {
275 local->set_prefix(prefix);
276 if (local->CanReachAddress(gateway)) {
277 break;
278 }
279 }
280
281 if (prefix < local->GetMinPrefixLength()) {
282 // Restore the original prefix since we cannot find a better one.
283 local->set_prefix(original_prefix);
Paul Stewart53a30382012-04-26 09:06:59 -0700284 return false;
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700285 }
Paul Stewart53a30382012-04-26 09:06:59 -0700286
287 LOG(WARNING) << "Mitigating this by setting local prefix to " << prefix;
288 return true;
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700289}
290
Paul Stewart7cfca042011-12-08 14:18:17 -0800291uint32 Connection::GetMetric(bool is_default) {
292 // If this is not the default route, assign a metric based on the interface
293 // index. This way all non-default routes (even to the same gateway IP) end
294 // up with unique metrics so they do not collide.
295 return is_default ? kDefaultMetric : kNonDefaultMetricBase + interface_index_;
296}
297
Paul Stewarte93b0382012-04-24 13:11:28 -0700298bool Connection::PinHostRoute(const IPConfig::Properties &properties) {
299 SLOG(Connection, 2) << __func__;
300 if (properties.gateway.empty() || properties.trusted_ip.empty()) {
301 return false;
302 }
303
304 IPAddress trusted_ip(properties.address_family);
305 if (!trusted_ip.SetAddressFromString(properties.trusted_ip)) {
306 LOG(ERROR) << "Failed to parse trusted_ip "
307 << properties.trusted_ip << "; ignored.";
308 return false;
309 }
310
311 return RequestHostRoute(trusted_ip);
312}
313
Darin Petkov13e6d552012-05-09 14:22:23 +0200314void Connection::OnRouteQueryResponse(int interface_index,
315 const RoutingTableEntry &entry) {
316 SLOG(Connection, 2) << __func__ << "(" << interface_index << ", "
317 << entry.tag << ")";
318 lower_binder_.Attach(NULL);
319 DeviceRefPtr device = device_info_->GetDevice(interface_index);
320 if (!device) {
321 LOG(ERROR) << "Unable to lookup device for index " << interface_index;
322 return;
323 }
324 ConnectionRefPtr connection = device->connection();
325 if (!connection) {
326 LOG(ERROR) << "Device " << interface_index << " has no connection.";
327 return;
328 }
329 lower_binder_.Attach(connection);
330}
331
332void Connection::OnLowerDisconnect() {
333 SLOG(Connection, 2) << __func__ << "(" << interface_name_ << ")";
334 // Ensures that |this| instance doesn't get destroyed in the middle of
335 // notifying the binders. This method needs to be separate from
336 // NotifyBindersOnDisconnect because the latter may be invoked by Connection's
337 // destructor when |this| instance's reference count is already 0.
338 ConnectionRefPtr connection(this);
339 connection->NotifyBindersOnDisconnect();
340}
341
342void Connection::NotifyBindersOnDisconnect() {
343 // Note that this method may be invoked by the destructor.
344 SLOG(Connection, 2) << __func__ << "(" << interface_name_ << ")";
345
346 // Unbinds the lower connection before notifying the binders. This ensures
347 // correct behavior in case of circular binding.
348 lower_binder_.Attach(NULL);
349 while (!binders_.empty()) {
350 // Pop the binder first and then notify it to ensure that each binder is
351 // notified only once.
352 Binder *binder = binders_.front();
353 binders_.pop_front();
354 binder->OnDisconnect();
355 }
356}
357
358void Connection::AttachBinder(Binder *binder) {
359 SLOG(Connection, 2) << __func__ << "(" << interface_name_ << ")";
360 binders_.push_back(binder);
361}
362
363void Connection::DetachBinder(Binder *binder) {
364 SLOG(Connection, 2) << __func__ << "(" << interface_name_ << ")";
365 for (deque<Binder *>::iterator it = binders_.begin();
366 it != binders_.end(); ++it) {
367 if (binder == *it) {
368 binders_.erase(it);
369 return;
370 }
371 }
372}
373
Paul Stewartdd60e452011-08-08 11:38:36 -0700374} // namespace shill