blob: b5817526b6a930fd422d2d65a649c4932e318f57 [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;
Alex Deymofddc09a2013-07-03 18:41:31 -070020using std::set;
Paul Stewartdd60e452011-08-08 11:38:36 -070021using std::string;
Paul Stewartd62d6032012-09-11 11:35:49 -070022using std::vector;
Paul Stewartdd60e452011-08-08 11:38:36 -070023
24namespace shill {
25
26// static
27const uint32 Connection::kDefaultMetric = 1;
28// static
Paul Stewart7cfca042011-12-08 14:18:17 -080029const uint32 Connection::kNonDefaultMetricBase = 10;
Paul Stewartdd60e452011-08-08 11:38:36 -070030
Darin Petkov13e6d552012-05-09 14:22:23 +020031Connection::Binder::Binder(const string &name,
32 const Closure &disconnect_callback)
33 : name_(name),
Darin Petkov13e6d552012-05-09 14:22:23 +020034 client_disconnect_callback_(disconnect_callback) {}
35
36Connection::Binder::~Binder() {
37 Attach(NULL);
38}
39
Darin Petkovef1f9fe2012-05-11 16:51:52 +020040void Connection::Binder::Attach(const ConnectionRefPtr &to_connection) {
Darin Petkov13e6d552012-05-09 14:22:23 +020041 if (connection_) {
42 connection_->DetachBinder(this);
43 LOG(INFO) << name_ << ": unbound from connection: "
44 << connection_->interface_name();
Darin Petkovef1f9fe2012-05-11 16:51:52 +020045 connection_.reset();
Darin Petkov13e6d552012-05-09 14:22:23 +020046 }
Darin Petkovef1f9fe2012-05-11 16:51:52 +020047 if (to_connection) {
48 connection_ = to_connection->weak_ptr_factory_.GetWeakPtr();
Darin Petkov13e6d552012-05-09 14:22:23 +020049 connection_->AttachBinder(this);
50 LOG(INFO) << name_ << ": bound to connection: "
51 << connection_->interface_name();
52 }
53}
54
55void Connection::Binder::OnDisconnect() {
56 LOG(INFO) << name_ << ": bound connection disconnected: "
57 << connection_->interface_name();
Darin Petkovef1f9fe2012-05-11 16:51:52 +020058 connection_.reset();
Darin Petkov13e6d552012-05-09 14:22:23 +020059 if (!client_disconnect_callback_.is_null()) {
60 SLOG(Connection, 2) << "Running client disconnect callback.";
61 client_disconnect_callback_.Run();
62 }
63}
64
Paul Stewart9a908082011-08-31 12:18:48 -070065Connection::Connection(int interface_index,
66 const std::string& interface_name,
Paul Stewarte00600e2012-03-16 07:08:00 -070067 Technology::Identifier technology,
mukesh agrawal23ac6b72013-01-31 18:52:37 -080068 const DeviceInfo *device_info)
Darin Petkov13e6d552012-05-09 14:22:23 +020069 : weak_ptr_factory_(this),
70 is_default_(false),
Paul Stewart4a6748d2012-07-17 14:31:36 -070071 has_broadcast_domain_(false),
Paul Stewartc8f4bef2011-12-13 09:45:51 -080072 routing_request_count_(0),
Paul Stewartdd60e452011-08-08 11:38:36 -070073 interface_index_(interface_index),
74 interface_name_(interface_name),
Paul Stewarte00600e2012-03-16 07:08:00 -070075 technology_(technology),
Paul Stewart4a6748d2012-07-17 14:31:36 -070076 local_(IPAddress::kFamilyUnknown),
77 gateway_(IPAddress::kFamilyUnknown),
Darin Petkov13e6d552012-05-09 14:22:23 +020078 lower_binder_(
79 interface_name_,
80 // Connection owns a single instance of |lower_binder_| so it's safe
81 // to use an Unretained callback.
82 Bind(&Connection::OnLowerDisconnect, Unretained(this))),
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 Stewartdd60e452011-08-08 11:38:36 -070090}
91
92Connection::~Connection() {
Ben Chanfad4a0b2012-04-18 15:49:59 -070093 SLOG(Connection, 2) << __func__ << " " << interface_name_;
Paul Stewart9a908082011-08-31 12:18:48 -070094
Darin Petkov13e6d552012-05-09 14:22:23 +020095 NotifyBindersOnDisconnect();
96
Paul Stewartc8f4bef2011-12-13 09:45:51 -080097 DCHECK(!routing_request_count_);
Thieu Lefb46caf2012-03-08 11:57:15 -080098 routing_table_->FlushRoutes(interface_index_);
Paul Stewarte93b0382012-04-24 13:11:28 -070099 routing_table_->FlushRoutesWithTag(interface_index_);
Paul Stewart9a908082011-08-31 12:18:48 -0700100 device_info_->FlushAddresses(interface_index_);
Paul Stewartdd60e452011-08-08 11:38:36 -0700101}
102
103void Connection::UpdateFromIPConfig(const IPConfigRefPtr &config) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700104 SLOG(Connection, 2) << __func__ << " " << interface_name_;
Paul Stewarte6132022011-08-16 09:11:02 -0700105
Paul Stewart9a908082011-08-31 12:18:48 -0700106 const IPConfig::Properties &properties = config->properties();
Paul Stewart73fcc3f2013-02-25 12:16:53 -0800107 IPAddress gateway(properties.address_family);
108 if (!properties.gateway.empty() &&
109 !gateway.SetAddressFromString(properties.gateway)) {
110 LOG(ERROR) << "Gateway address " << properties.gateway << " is invalid";
Paul Stewarte93b0382012-04-24 13:11:28 -0700111 return;
112 }
113
Paul Stewart73fcc3f2013-02-25 12:16:53 -0800114 IPAddress trusted_ip(properties.address_family);
115 if (!properties.trusted_ip.empty()) {
116 if (!trusted_ip.SetAddressFromString(properties.trusted_ip)) {
117 LOG(ERROR) << "Trusted IP address "
118 << properties.trusted_ip << " is invalid";
119 return;
120 }
121 if (!PinHostRoute(trusted_ip, gateway)) {
122 LOG(ERROR) << "Unable to pin host route to " << properties.trusted_ip;
123 return;
124 }
125 }
126
Paul Stewart9a908082011-08-31 12:18:48 -0700127 IPAddress local(properties.address_family);
128 if (!local.SetAddressFromString(properties.address)) {
129 LOG(ERROR) << "Local address " << properties.address << " is invalid";
130 return;
131 }
Paul Stewart48100b02012-03-19 07:53:52 -0700132 local.set_prefix(properties.subnet_prefix);
Paul Stewart9a908082011-08-31 12:18:48 -0700133
134 IPAddress broadcast(properties.address_family);
Paul Stewart1062d9d2012-04-27 10:42:27 -0700135 if (properties.broadcast_address.empty()) {
Paul Stewartfe1c0e12012-04-30 19:57:04 -0700136 if (properties.peer_address.empty()) {
Paul Stewart1062d9d2012-04-27 10:42:27 -0700137 LOG(WARNING) << "Broadcast address is not set. Using default.";
Paul Stewartfe1c0e12012-04-30 19:57:04 -0700138 broadcast = local.GetDefaultBroadcast();
Paul Stewart1062d9d2012-04-27 10:42:27 -0700139 }
140 } else if (!broadcast.SetAddressFromString(properties.broadcast_address)) {
Paul Stewart9a908082011-08-31 12:18:48 -0700141 LOG(ERROR) << "Broadcast address " << properties.broadcast_address
142 << " is invalid";
143 return;
144 }
145
Paul Stewart48100b02012-03-19 07:53:52 -0700146 IPAddress peer(properties.address_family);
147 if (!properties.peer_address.empty() &&
148 !peer.SetAddressFromString(properties.peer_address)) {
149 LOG(ERROR) << "Peer address " << properties.peer_address
150 << " is invalid";
151 return;
152 }
153
Paul Stewart73fcc3f2013-02-25 12:16:53 -0800154 if (!FixGatewayReachability(&local, &peer, &gateway, trusted_ip)) {
Paul Stewart53a30382012-04-26 09:06:59 -0700155 LOG(WARNING) << "Expect limited network connectivity.";
156 }
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700157
Paul Stewart05a42c22012-08-02 16:47:21 -0700158 if (device_info_->HasOtherAddress(interface_index_, local)) {
159 // The address has changed for this interface. We need to flush
160 // everything and start over.
161 LOG(INFO) << __func__ << ": Flushing old addresses and routes.";
162 routing_table_->FlushRoutes(interface_index_);
163 device_info_->FlushAddresses(interface_index_);
164 }
165
Paul Stewarte78ec542012-06-08 18:28:50 -0700166 LOG(INFO) << __func__ << ": Installing with parameters:"
167 << " local=" << local.ToString()
168 << " broadcast=" << broadcast.ToString()
169 << " peer=" << peer.ToString()
170 << " gateway=" << gateway.ToString();
Paul Stewart48100b02012-03-19 07:53:52 -0700171 rtnl_handler_->AddInterfaceAddress(interface_index_, local, broadcast, peer);
Paul Stewartdd60e452011-08-08 11:38:36 -0700172
Paul Stewarte78ec542012-06-08 18:28:50 -0700173 if (gateway.IsValid()) {
174 routing_table_->SetDefaultRoute(interface_index_, gateway,
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700175 GetMetric(is_default_));
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700176 }
Paul Stewartdd60e452011-08-08 11:38:36 -0700177
Paul Stewart3f68bb12012-03-15 13:33:10 -0700178 // Install any explicitly configured routes at the default metric.
179 routing_table_->ConfigureRoutes(interface_index_, config, kDefaultMetric);
180
Ben Chana0163122012-09-25 15:10:52 -0700181 if (properties.blackhole_ipv6) {
182 routing_table_->CreateBlackholeRoute(interface_index_,
183 IPAddress::kFamilyIPv6,
184 kDefaultMetric);
185 }
186
Paul Stewartd62d6032012-09-11 11:35:49 -0700187 // Save a copy of the last non-null DNS config.
Paul Stewartdd60e452011-08-08 11:38:36 -0700188 if (!config->properties().dns_servers.empty()) {
189 dns_servers_ = config->properties().dns_servers;
Paul Stewartd62d6032012-09-11 11:35:49 -0700190 }
191
192 if (!config->properties().domain_search.empty()) {
Paul Stewartdd60e452011-08-08 11:38:36 -0700193 dns_domain_search_ = config->properties().domain_search;
Paul Stewartd62d6032012-09-11 11:35:49 -0700194 }
195
196 if (!config->properties().domain_name.empty()) {
197 dns_domain_name_ = config->properties().domain_name;
Paul Stewartdd60e452011-08-08 11:38:36 -0700198 }
199
Paul Stewart10241e32012-04-23 18:15:06 -0700200 ipconfig_rpc_identifier_ = config->GetRpcIdentifier();
201
Paul Stewartdd60e452011-08-08 11:38:36 -0700202 if (is_default_) {
Paul Stewart6f65c0b2012-09-11 14:57:32 -0700203 PushDNSConfig();
Paul Stewartdd60e452011-08-08 11:38:36 -0700204 }
Paul Stewart4a6748d2012-07-17 14:31:36 -0700205
206 local_ = local;
207 gateway_ = gateway;
208 has_broadcast_domain_ = !peer.IsValid();
Paul Stewartdd60e452011-08-08 11:38:36 -0700209}
210
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800211void Connection::SetIsDefault(bool is_default) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700212 SLOG(Connection, 2) << __func__ << " " << interface_name_
213 << " (index " << interface_index_ << ") "
214 << is_default_ << " -> " << is_default;
Paul Stewartdd60e452011-08-08 11:38:36 -0700215 if (is_default == is_default_) {
216 return;
217 }
218
Paul Stewart7cfca042011-12-08 14:18:17 -0800219 routing_table_->SetDefaultMetric(interface_index_, GetMetric(is_default));
Paul Stewartdd60e452011-08-08 11:38:36 -0700220
Paul Stewartc681fa02012-03-02 19:40:04 -0800221 is_default_ = is_default;
222
Paul Stewartdd60e452011-08-08 11:38:36 -0700223 if (is_default) {
Paul Stewart6f65c0b2012-09-11 14:57:32 -0700224 PushDNSConfig();
Paul Stewartc681fa02012-03-02 19:40:04 -0800225 DeviceRefPtr device = device_info_->GetDevice(interface_index_);
226 if (device) {
227 device->RequestPortalDetection();
228 }
Paul Stewartdd60e452011-08-08 11:38:36 -0700229 }
Paul Stewarte78ec542012-06-08 18:28:50 -0700230 routing_table_->FlushCache();
Paul Stewartdd60e452011-08-08 11:38:36 -0700231}
232
Paul Stewart6f65c0b2012-09-11 14:57:32 -0700233void Connection::PushDNSConfig() {
234 vector<string> domain_search = dns_domain_search_;
235 if (domain_search.empty() && !dns_domain_name_.empty()) {
236 SLOG(Connection, 2) << "Setting domain search to domain name "
237 << dns_domain_name_;
238 domain_search.push_back(dns_domain_name_ + ".");
239 }
mukesh agrawal23ac6b72013-01-31 18:52:37 -0800240 resolver_->SetDNSFromLists(dns_servers_, domain_search);
Paul Stewart6f65c0b2012-09-11 14:57:32 -0700241}
242
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800243void Connection::RequestRouting() {
244 if (routing_request_count_++ == 0) {
245 DeviceRefPtr device = device_info_->GetDevice(interface_index_);
246 DCHECK(device.get());
247 if (!device.get()) {
248 LOG(ERROR) << "Device is NULL!";
249 return;
250 }
251 device->DisableReversePathFilter();
252 }
253}
254
255void Connection::ReleaseRouting() {
256 DCHECK(routing_request_count_ > 0);
257 if (--routing_request_count_ == 0) {
258 DeviceRefPtr device = device_info_->GetDevice(interface_index_);
259 DCHECK(device.get());
260 if (!device.get()) {
261 LOG(ERROR) << "Device is NULL!";
262 return;
263 }
264 device->EnableReversePathFilter();
265
266 // Clear any cached routes that might have accumulated while reverse-path
267 // filtering was disabled.
268 routing_table_->FlushCache();
269 }
270}
271
Paul Stewartf748a362012-03-07 12:01:20 -0800272bool Connection::RequestHostRoute(const IPAddress &address) {
273 // Set the prefix to be the entire address size.
274 IPAddress address_prefix(address);
275 address_prefix.set_prefix(address_prefix.GetLength() * 8);
276
Darin Petkov13e6d552012-05-09 14:22:23 +0200277 // Do not set interface_index_ since this may not be the default route through
278 // which this destination can be found. However, we should tag the created
279 // route with our interface index so we can clean this route up when this
280 // connection closes. Also, add route query callback to determine the lower
281 // connection and bind to it.
282 if (!routing_table_->RequestRouteToHost(
283 address_prefix,
284 -1,
285 interface_index_,
Darin Petkov5eb05422012-05-11 15:45:25 +0200286 Bind(&Connection::OnRouteQueryResponse,
287 weak_ptr_factory_.GetWeakPtr()))) {
Paul Stewartf748a362012-03-07 12:01:20 -0800288 LOG(ERROR) << "Could not request route to " << address.ToString();
289 return false;
290 }
291
292 return true;
293}
294
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700295// static
Paul Stewart53a30382012-04-26 09:06:59 -0700296bool Connection::FixGatewayReachability(IPAddress *local,
Paul Stewart49258292012-05-26 06:37:14 -0700297 IPAddress *peer,
Paul Stewart73fcc3f2013-02-25 12:16:53 -0800298 IPAddress *gateway,
299 const IPAddress &trusted_ip) {
300 if (!gateway->IsValid()) {
Paul Stewart53a30382012-04-26 09:06:59 -0700301 LOG(WARNING) << "No gateway address was provided for this connection.";
302 return false;
303 }
304
Paul Stewart49258292012-05-26 06:37:14 -0700305 if (peer->IsValid()) {
Paul Stewart73fcc3f2013-02-25 12:16:53 -0800306 if (!gateway->Equals(*peer)) {
307 LOG(WARNING) << "Gateway address "
308 << gateway->ToString()
309 << " does not match peer address "
310 << peer->ToString();
311 return false;
Paul Stewart53a30382012-04-26 09:06:59 -0700312 }
Paul Stewart73fcc3f2013-02-25 12:16:53 -0800313 if (gateway->Equals(trusted_ip)) {
314 // In order to send outgoing traffic in a point-to-point network,
315 // the gateway IP address isn't of significance. As opposed to
316 // broadcast networks, we never ARP for the gateway IP address,
317 // but just send the IP packet addressed to the recipient. As
318 // such, since using the external trusted IP address as the
319 // gateway or peer wreaks havoc on the routing rules, we choose
320 // not to supply a gateway address. Here's an example:
321 //
322 // Client <-> Internet <-> VPN Gateway <-> Internal Network
323 // 192.168.1.2 10.0.1.25 172.16.5.0/24
324 //
325 // In this example, a client connects to a VPN gateway on its
326 // public IP address 10.0.1.25. It gets issued an IP address
327 // from the VPN internal pool. For some VPN gateways, this
328 // results in a pushed-down PPP configuration which specifies:
329 //
330 // Client local address: 172.16.5.13
331 // Client peer address: 10.0.1.25
332 // Client default gateway: 10.0.1.25
333 //
334 // If we take this literally, we need to resolve the fact that
335 // 10.0.1.25 is now listed as the default gateway and interface
336 // peer address for the point-to-point interface. However, in
337 // order to route tunneled packets to the VPN gateway we must
338 // use the external route through the physical interface and
339 // not the tunnel, or else we end up in an infinite loop
340 // re-entering the tunnel trying to route towards the VPN server.
341 //
342 // We can do this by pinning a route, but we would need to wait
343 // for the pinning process to complete before assigning this
344 // address. Currently this process is asynchronous and will
345 // complete only after returning to the event loop. Additionally,
346 // since there's no metric associated with assigning an address
347 // to an interface, it's always possible that having the peer
348 // address of the interface might still trump a host route.
349 //
350 // To solve this problem, we reset the peer and gateway
351 // addresses. Neither is required in order to perform the
352 // underlying routing task. A gateway route can be specified
353 // without an IP endpoint on point-to-point links, and simply
354 // specify the outbound interface index. Similarly, a peer
355 // IP address is not necessary either, and will be assigned
356 // the same IP address as the local IP. This approach
357 // simplifies routing and doesn't change the desired
358 // functional behavior.
359 //
360 LOG(INFO) << "Removing gateway and peer addresses to preserve "
361 << "routability to trusted IP address.";
362 peer->SetAddressToDefault();
363 gateway->SetAddressToDefault();
364 }
365 return true;
Paul Stewart53a30382012-04-26 09:06:59 -0700366 }
367
Paul Stewart73fcc3f2013-02-25 12:16:53 -0800368 if (local->CanReachAddress(*gateway)) {
Paul Stewart53a30382012-04-26 09:06:59 -0700369 return true;
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700370 }
371
372 LOG(WARNING) << "Gateway "
Paul Stewart73fcc3f2013-02-25 12:16:53 -0800373 << gateway->ToString()
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700374 << " is unreachable from local address/prefix "
375 << local->ToString() << "/" << local->prefix();
376
Paul Stewart2aa5d7d2012-06-21 22:16:54 -0700377 bool found_new_prefix = false;
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700378 size_t original_prefix = local->prefix();
Paul Stewart2aa5d7d2012-06-21 22:16:54 -0700379 // Only try to expand the netmask if the configured prefix is
380 // less than "all ones". This special-cases the "all-ones"
381 // prefix as a forced conversion to point-to-point networking.
382 if (local->prefix() < IPAddress::GetMaxPrefixLength(local->family())) {
383 size_t prefix = original_prefix - 1;
384 for (; prefix >= local->GetMinPrefixLength(); --prefix) {
385 local->set_prefix(prefix);
Paul Stewart73fcc3f2013-02-25 12:16:53 -0800386 if (local->CanReachAddress(*gateway)) {
Paul Stewart2aa5d7d2012-06-21 22:16:54 -0700387 found_new_prefix = true;
388 break;
389 }
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700390 }
391 }
392
Paul Stewart2aa5d7d2012-06-21 22:16:54 -0700393 if (!found_new_prefix) {
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700394 // Restore the original prefix since we cannot find a better one.
395 local->set_prefix(original_prefix);
Paul Stewart49258292012-05-26 06:37:14 -0700396 DCHECK(!peer->IsValid());
397 LOG(WARNING) << "Assuming point-to-point configuration.";
Paul Stewart73fcc3f2013-02-25 12:16:53 -0800398 *peer = *gateway;
Paul Stewart49258292012-05-26 06:37:14 -0700399 return true;
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700400 }
Paul Stewart53a30382012-04-26 09:06:59 -0700401
Paul Stewart2aa5d7d2012-06-21 22:16:54 -0700402 LOG(WARNING) << "Mitigating this by setting local prefix to "
403 << local->prefix();
Paul Stewart53a30382012-04-26 09:06:59 -0700404 return true;
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700405}
406
Paul Stewart7cfca042011-12-08 14:18:17 -0800407uint32 Connection::GetMetric(bool is_default) {
408 // If this is not the default route, assign a metric based on the interface
409 // index. This way all non-default routes (even to the same gateway IP) end
410 // up with unique metrics so they do not collide.
411 return is_default ? kDefaultMetric : kNonDefaultMetricBase + interface_index_;
412}
413
Paul Stewart73fcc3f2013-02-25 12:16:53 -0800414bool Connection::PinHostRoute(const IPAddress &trusted_ip,
415 const IPAddress &gateway) {
Paul Stewarte93b0382012-04-24 13:11:28 -0700416 SLOG(Connection, 2) << __func__;
Paul Stewart73fcc3f2013-02-25 12:16:53 -0800417 if (!trusted_ip.IsValid() || !gateway.IsValid()) {
418 LOG_IF(ERROR, !gateway.IsValid())
Darin Petkove8587e32012-07-02 13:56:07 +0200419 << "No gateway -- unable to pin host route.";
Paul Stewart73fcc3f2013-02-25 12:16:53 -0800420 LOG_IF(ERROR, !trusted_ip.IsValid())
Darin Petkove8587e32012-07-02 13:56:07 +0200421 << "No trusted IP -- unable to pin host route.";
Paul Stewarte93b0382012-04-24 13:11:28 -0700422 return false;
423 }
424
Paul Stewarte93b0382012-04-24 13:11:28 -0700425 return RequestHostRoute(trusted_ip);
426}
427
Darin Petkov13e6d552012-05-09 14:22:23 +0200428void Connection::OnRouteQueryResponse(int interface_index,
429 const RoutingTableEntry &entry) {
430 SLOG(Connection, 2) << __func__ << "(" << interface_index << ", "
Darin Petkov5eb05422012-05-11 15:45:25 +0200431 << entry.tag << ")" << " @ " << interface_name_;
Darin Petkov13e6d552012-05-09 14:22:23 +0200432 lower_binder_.Attach(NULL);
433 DeviceRefPtr device = device_info_->GetDevice(interface_index);
434 if (!device) {
435 LOG(ERROR) << "Unable to lookup device for index " << interface_index;
436 return;
437 }
438 ConnectionRefPtr connection = device->connection();
439 if (!connection) {
440 LOG(ERROR) << "Device " << interface_index << " has no connection.";
441 return;
442 }
443 lower_binder_.Attach(connection);
Paul Stewart4a6748d2012-07-17 14:31:36 -0700444 connection->CreateGatewayRoute();
Paul Stewart8596f9f2013-03-14 07:58:26 -0700445 device->OnConnectionUpdated();
Paul Stewart4a6748d2012-07-17 14:31:36 -0700446}
447
448bool Connection::CreateGatewayRoute() {
449 // Ensure that the gateway for the lower connection remains reachable,
450 // since we may create routes that conflict with it.
451 if (!has_broadcast_domain_) {
452 return false;
453 }
Paul Stewart856b8842013-07-10 11:59:13 -0700454
455 // If there is no gateway, don't try to create a route to it.
456 if (!gateway_.IsValid()) {
457 return false;
458 }
459
Paul Stewart4a6748d2012-07-17 14:31:36 -0700460 // It is not worth keeping track of this route, since it is benign,
461 // and only pins persistent state that was already true of the connection.
462 // If DHCP parameters change later (without the connection having been
463 // destroyed and recreated), the binding processes will likely terminate
464 // and restart, causing a new link route to be created.
465 return routing_table_->CreateLinkRoute(interface_index_, local_, gateway_);
Darin Petkov13e6d552012-05-09 14:22:23 +0200466}
467
468void Connection::OnLowerDisconnect() {
Darin Petkov5eb05422012-05-11 15:45:25 +0200469 SLOG(Connection, 2) << __func__ << " @ " << interface_name_;
Darin Petkov13e6d552012-05-09 14:22:23 +0200470 // Ensures that |this| instance doesn't get destroyed in the middle of
471 // notifying the binders. This method needs to be separate from
472 // NotifyBindersOnDisconnect because the latter may be invoked by Connection's
473 // destructor when |this| instance's reference count is already 0.
474 ConnectionRefPtr connection(this);
475 connection->NotifyBindersOnDisconnect();
476}
477
478void Connection::NotifyBindersOnDisconnect() {
479 // Note that this method may be invoked by the destructor.
Darin Petkov5eb05422012-05-11 15:45:25 +0200480 SLOG(Connection, 2) << __func__ << " @ " << interface_name_;
Darin Petkov13e6d552012-05-09 14:22:23 +0200481
482 // Unbinds the lower connection before notifying the binders. This ensures
483 // correct behavior in case of circular binding.
484 lower_binder_.Attach(NULL);
485 while (!binders_.empty()) {
486 // Pop the binder first and then notify it to ensure that each binder is
487 // notified only once.
488 Binder *binder = binders_.front();
489 binders_.pop_front();
490 binder->OnDisconnect();
491 }
492}
493
494void Connection::AttachBinder(Binder *binder) {
Darin Petkov5eb05422012-05-11 15:45:25 +0200495 SLOG(Connection, 2) << __func__ << "(" << binder->name() << ")" << " @ "
496 << interface_name_;
Darin Petkov13e6d552012-05-09 14:22:23 +0200497 binders_.push_back(binder);
498}
499
500void Connection::DetachBinder(Binder *binder) {
Darin Petkov5eb05422012-05-11 15:45:25 +0200501 SLOG(Connection, 2) << __func__ << "(" << binder->name() << ")" << " @ "
502 << interface_name_;
Darin Petkov13e6d552012-05-09 14:22:23 +0200503 for (deque<Binder *>::iterator it = binders_.begin();
504 it != binders_.end(); ++it) {
505 if (binder == *it) {
506 binders_.erase(it);
507 return;
508 }
509 }
510}
511
Alex Deymofddc09a2013-07-03 18:41:31 -0700512ConnectionRefPtr Connection::GetCarrierConnection() {
513 SLOG(Connection, 2) << __func__ << " @ " << interface_name_;
514 set<Connection *> visited;
515 ConnectionRefPtr carrier = this;
516 while (carrier->GetLowerConnection()) {
517 if (ContainsKey(visited, carrier.get())) {
518 LOG(ERROR) << "Circular connection chain starting at: "
519 << carrier->interface_name();
520 // If a loop is detected return a NULL value to signal that the carrier
521 // connection is unknown.
522 return NULL;
523 }
524 visited.insert(carrier.get());
525 carrier = carrier->GetLowerConnection();
526 }
527 SLOG(Connection, 2) << "Carrier connection: " << carrier->interface_name()
528 << " @ " << interface_name_;
529 return carrier;
530}
531
Paul Stewartdd60e452011-08-08 11:38:36 -0700532} // namespace shill