blob: ecd59b440a771086dd15c67b5baf23f635b004fc [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_,
Darin Petkov5eb05422012-05-11 15:45:25 +0200233 Bind(&Connection::OnRouteQueryResponse,
234 weak_ptr_factory_.GetWeakPtr()))) {
Paul Stewartf748a362012-03-07 12:01:20 -0800235 LOG(ERROR) << "Could not request route to " << address.ToString();
236 return false;
237 }
238
239 return true;
240}
241
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700242// static
Paul Stewart53a30382012-04-26 09:06:59 -0700243bool Connection::FixGatewayReachability(IPAddress *local,
244 const IPAddress &gateway,
245 const IPAddress &peer) {
246 if (!gateway.IsValid()) {
247 LOG(WARNING) << "No gateway address was provided for this connection.";
248 return false;
249 }
250
251 if (peer.IsValid()) {
252 if (gateway.Equals(peer)) {
253 return true;
254 }
255 LOG(WARNING) << "Gateway address "
256 << gateway.ToString()
257 << " does not match peer address "
258 << peer.ToString();
259 return false;
260 }
261
262 if (local->CanReachAddress(gateway)) {
263 return true;
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700264 }
265
266 LOG(WARNING) << "Gateway "
267 << gateway.ToString()
268 << " is unreachable from local address/prefix "
269 << local->ToString() << "/" << local->prefix();
270
271 size_t original_prefix = local->prefix();
272 size_t prefix = original_prefix - 1;
273 for (; prefix >= local->GetMinPrefixLength(); --prefix) {
274 local->set_prefix(prefix);
275 if (local->CanReachAddress(gateway)) {
276 break;
277 }
278 }
279
280 if (prefix < local->GetMinPrefixLength()) {
281 // Restore the original prefix since we cannot find a better one.
282 local->set_prefix(original_prefix);
Paul Stewart53a30382012-04-26 09:06:59 -0700283 return false;
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700284 }
Paul Stewart53a30382012-04-26 09:06:59 -0700285
286 LOG(WARNING) << "Mitigating this by setting local prefix to " << prefix;
287 return true;
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700288}
289
Paul Stewart7cfca042011-12-08 14:18:17 -0800290uint32 Connection::GetMetric(bool is_default) {
291 // If this is not the default route, assign a metric based on the interface
292 // index. This way all non-default routes (even to the same gateway IP) end
293 // up with unique metrics so they do not collide.
294 return is_default ? kDefaultMetric : kNonDefaultMetricBase + interface_index_;
295}
296
Paul Stewarte93b0382012-04-24 13:11:28 -0700297bool Connection::PinHostRoute(const IPConfig::Properties &properties) {
298 SLOG(Connection, 2) << __func__;
299 if (properties.gateway.empty() || properties.trusted_ip.empty()) {
300 return false;
301 }
302
303 IPAddress trusted_ip(properties.address_family);
304 if (!trusted_ip.SetAddressFromString(properties.trusted_ip)) {
305 LOG(ERROR) << "Failed to parse trusted_ip "
306 << properties.trusted_ip << "; ignored.";
307 return false;
308 }
309
310 return RequestHostRoute(trusted_ip);
311}
312
Darin Petkov13e6d552012-05-09 14:22:23 +0200313void Connection::OnRouteQueryResponse(int interface_index,
314 const RoutingTableEntry &entry) {
315 SLOG(Connection, 2) << __func__ << "(" << interface_index << ", "
Darin Petkov5eb05422012-05-11 15:45:25 +0200316 << entry.tag << ")" << " @ " << interface_name_;
Darin Petkov13e6d552012-05-09 14:22:23 +0200317 lower_binder_.Attach(NULL);
318 DeviceRefPtr device = device_info_->GetDevice(interface_index);
319 if (!device) {
320 LOG(ERROR) << "Unable to lookup device for index " << interface_index;
321 return;
322 }
323 ConnectionRefPtr connection = device->connection();
324 if (!connection) {
325 LOG(ERROR) << "Device " << interface_index << " has no connection.";
326 return;
327 }
328 lower_binder_.Attach(connection);
329}
330
331void Connection::OnLowerDisconnect() {
Darin Petkov5eb05422012-05-11 15:45:25 +0200332 SLOG(Connection, 2) << __func__ << " @ " << interface_name_;
Darin Petkov13e6d552012-05-09 14:22:23 +0200333 // Ensures that |this| instance doesn't get destroyed in the middle of
334 // notifying the binders. This method needs to be separate from
335 // NotifyBindersOnDisconnect because the latter may be invoked by Connection's
336 // destructor when |this| instance's reference count is already 0.
337 ConnectionRefPtr connection(this);
338 connection->NotifyBindersOnDisconnect();
339}
340
341void Connection::NotifyBindersOnDisconnect() {
342 // Note that this method may be invoked by the destructor.
Darin Petkov5eb05422012-05-11 15:45:25 +0200343 SLOG(Connection, 2) << __func__ << " @ " << interface_name_;
Darin Petkov13e6d552012-05-09 14:22:23 +0200344
345 // Unbinds the lower connection before notifying the binders. This ensures
346 // correct behavior in case of circular binding.
347 lower_binder_.Attach(NULL);
348 while (!binders_.empty()) {
349 // Pop the binder first and then notify it to ensure that each binder is
350 // notified only once.
351 Binder *binder = binders_.front();
352 binders_.pop_front();
353 binder->OnDisconnect();
354 }
355}
356
357void Connection::AttachBinder(Binder *binder) {
Darin Petkov5eb05422012-05-11 15:45:25 +0200358 SLOG(Connection, 2) << __func__ << "(" << binder->name() << ")" << " @ "
359 << interface_name_;
Darin Petkov13e6d552012-05-09 14:22:23 +0200360 binders_.push_back(binder);
361}
362
363void Connection::DetachBinder(Binder *binder) {
Darin Petkov5eb05422012-05-11 15:45:25 +0200364 SLOG(Connection, 2) << __func__ << "(" << binder->name() << ")" << " @ "
365 << interface_name_;
Darin Petkov13e6d552012-05-09 14:22:23 +0200366 for (deque<Binder *>::iterator it = binders_.begin();
367 it != binders_.end(); ++it) {
368 if (binder == *it) {
369 binders_.erase(it);
370 return;
371 }
372 }
373}
374
Paul Stewartdd60e452011-08-08 11:38:36 -0700375} // namespace shill