mukesh agrawal | ddc378f | 2012-02-17 18:26:20 -0800 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 2 | // 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 Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 7 | #include <arpa/inet.h> |
| 8 | #include <linux/rtnetlink.h> |
| 9 | |
| 10 | #include "shill/device_info.h" |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 11 | #include "shill/logging.h" |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 12 | #include "shill/resolver.h" |
| 13 | #include "shill/routing_table.h" |
| 14 | #include "shill/rtnl_handler.h" |
| 15 | |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 16 | using base::Bind; |
| 17 | using base::Closure; |
| 18 | using base::Unretained; |
| 19 | using std::deque; |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 20 | using std::string; |
| 21 | |
| 22 | namespace shill { |
| 23 | |
| 24 | // static |
| 25 | const uint32 Connection::kDefaultMetric = 1; |
| 26 | // static |
Paul Stewart | 7cfca04 | 2011-12-08 14:18:17 -0800 | [diff] [blame] | 27 | const uint32 Connection::kNonDefaultMetricBase = 10; |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 28 | |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 29 | Connection::Binder::Binder(const string &name, |
| 30 | const Closure &disconnect_callback) |
| 31 | : name_(name), |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 32 | client_disconnect_callback_(disconnect_callback) {} |
| 33 | |
| 34 | Connection::Binder::~Binder() { |
| 35 | Attach(NULL); |
| 36 | } |
| 37 | |
Darin Petkov | ef1f9fe | 2012-05-11 16:51:52 +0200 | [diff] [blame] | 38 | void Connection::Binder::Attach(const ConnectionRefPtr &to_connection) { |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 39 | if (connection_) { |
| 40 | connection_->DetachBinder(this); |
| 41 | LOG(INFO) << name_ << ": unbound from connection: " |
| 42 | << connection_->interface_name(); |
Darin Petkov | ef1f9fe | 2012-05-11 16:51:52 +0200 | [diff] [blame] | 43 | connection_.reset(); |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 44 | } |
Darin Petkov | ef1f9fe | 2012-05-11 16:51:52 +0200 | [diff] [blame] | 45 | if (to_connection) { |
| 46 | connection_ = to_connection->weak_ptr_factory_.GetWeakPtr(); |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 47 | connection_->AttachBinder(this); |
| 48 | LOG(INFO) << name_ << ": bound to connection: " |
| 49 | << connection_->interface_name(); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | void Connection::Binder::OnDisconnect() { |
| 54 | LOG(INFO) << name_ << ": bound connection disconnected: " |
| 55 | << connection_->interface_name(); |
Darin Petkov | ef1f9fe | 2012-05-11 16:51:52 +0200 | [diff] [blame] | 56 | connection_.reset(); |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 57 | if (!client_disconnect_callback_.is_null()) { |
| 58 | SLOG(Connection, 2) << "Running client disconnect callback."; |
| 59 | client_disconnect_callback_.Run(); |
| 60 | } |
| 61 | } |
| 62 | |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 63 | Connection::Connection(int interface_index, |
| 64 | const std::string& interface_name, |
Paul Stewart | e00600e | 2012-03-16 07:08:00 -0700 | [diff] [blame] | 65 | Technology::Identifier technology, |
Paul Stewart | bf66761 | 2012-06-29 14:49:54 -0700 | [diff] [blame] | 66 | const DeviceInfo *device_info, |
| 67 | bool is_short_dns_timeout_enabled) |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 68 | : weak_ptr_factory_(this), |
| 69 | is_default_(false), |
Paul Stewart | 4a6748d | 2012-07-17 14:31:36 -0700 | [diff] [blame] | 70 | has_broadcast_domain_(false), |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 71 | routing_request_count_(0), |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 72 | interface_index_(interface_index), |
| 73 | interface_name_(interface_name), |
Paul Stewart | e00600e | 2012-03-16 07:08:00 -0700 | [diff] [blame] | 74 | technology_(technology), |
Paul Stewart | 4a6748d | 2012-07-17 14:31:36 -0700 | [diff] [blame] | 75 | local_(IPAddress::kFamilyUnknown), |
| 76 | gateway_(IPAddress::kFamilyUnknown), |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 77 | 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 Stewart | bf66761 | 2012-06-29 14:49:54 -0700 | [diff] [blame] | 82 | dns_timeout_parameters_(Resolver::kDefaultTimeout), |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 83 | device_info_(device_info), |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 84 | resolver_(Resolver::GetInstance()), |
| 85 | routing_table_(RoutingTable::GetInstance()), |
| 86 | rtnl_handler_(RTNLHandler::GetInstance()) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 87 | SLOG(Connection, 2) << __func__ << "(" << interface_index << ", " |
| 88 | << interface_name << ", " |
| 89 | << Technology::NameFromIdentifier(technology) << ")"; |
Paul Stewart | bf66761 | 2012-06-29 14:49:54 -0700 | [diff] [blame] | 90 | if (is_short_dns_timeout_enabled) { |
| 91 | dns_timeout_parameters_ = Resolver::kShortTimeout; |
| 92 | } |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | Connection::~Connection() { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 96 | SLOG(Connection, 2) << __func__ << " " << interface_name_; |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 97 | |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 98 | NotifyBindersOnDisconnect(); |
| 99 | |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 100 | DCHECK(!routing_request_count_); |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 101 | routing_table_->FlushRoutes(interface_index_); |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 102 | routing_table_->FlushRoutesWithTag(interface_index_); |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 103 | device_info_->FlushAddresses(interface_index_); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | void Connection::UpdateFromIPConfig(const IPConfigRefPtr &config) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 107 | SLOG(Connection, 2) << __func__ << " " << interface_name_; |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 108 | |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 109 | const IPConfig::Properties &properties = config->properties(); |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 110 | if (!properties.trusted_ip.empty() && !PinHostRoute(properties)) { |
| 111 | LOG(ERROR) << "Unable to pin host route to " << properties.trusted_ip; |
| 112 | return; |
| 113 | } |
| 114 | |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 115 | IPAddress local(properties.address_family); |
| 116 | if (!local.SetAddressFromString(properties.address)) { |
| 117 | LOG(ERROR) << "Local address " << properties.address << " is invalid"; |
| 118 | return; |
| 119 | } |
Paul Stewart | 48100b0 | 2012-03-19 07:53:52 -0700 | [diff] [blame] | 120 | local.set_prefix(properties.subnet_prefix); |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 121 | |
| 122 | IPAddress broadcast(properties.address_family); |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 123 | if (properties.broadcast_address.empty()) { |
Paul Stewart | fe1c0e1 | 2012-04-30 19:57:04 -0700 | [diff] [blame] | 124 | if (properties.peer_address.empty()) { |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 125 | LOG(WARNING) << "Broadcast address is not set. Using default."; |
Paul Stewart | fe1c0e1 | 2012-04-30 19:57:04 -0700 | [diff] [blame] | 126 | broadcast = local.GetDefaultBroadcast(); |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 127 | } |
| 128 | } else if (!broadcast.SetAddressFromString(properties.broadcast_address)) { |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 129 | LOG(ERROR) << "Broadcast address " << properties.broadcast_address |
| 130 | << " is invalid"; |
| 131 | return; |
| 132 | } |
| 133 | |
Paul Stewart | 48100b0 | 2012-03-19 07:53:52 -0700 | [diff] [blame] | 134 | 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 Stewart | e78ec54 | 2012-06-08 18:28:50 -0700 | [diff] [blame] | 142 | IPAddress gateway(properties.address_family); |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 143 | if (!properties.gateway.empty() && |
Paul Stewart | e78ec54 | 2012-06-08 18:28:50 -0700 | [diff] [blame] | 144 | !gateway.SetAddressFromString(properties.gateway)) { |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 145 | LOG(ERROR) << "Gateway address " << properties.peer_address |
| 146 | << " is invalid"; |
| 147 | return; |
| 148 | } |
| 149 | |
Paul Stewart | e78ec54 | 2012-06-08 18:28:50 -0700 | [diff] [blame] | 150 | if (!FixGatewayReachability(&local, &peer, gateway)) { |
Paul Stewart | 53a3038 | 2012-04-26 09:06:59 -0700 | [diff] [blame] | 151 | LOG(WARNING) << "Expect limited network connectivity."; |
| 152 | } |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 153 | |
Paul Stewart | 05a42c2 | 2012-08-02 16:47:21 -0700 | [diff] [blame] | 154 | 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 Stewart | e78ec54 | 2012-06-08 18:28:50 -0700 | [diff] [blame] | 162 | LOG(INFO) << __func__ << ": Installing with parameters:" |
| 163 | << " local=" << local.ToString() |
| 164 | << " broadcast=" << broadcast.ToString() |
| 165 | << " peer=" << peer.ToString() |
| 166 | << " gateway=" << gateway.ToString(); |
Paul Stewart | 48100b0 | 2012-03-19 07:53:52 -0700 | [diff] [blame] | 167 | rtnl_handler_->AddInterfaceAddress(interface_index_, local, broadcast, peer); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 168 | |
Paul Stewart | e78ec54 | 2012-06-08 18:28:50 -0700 | [diff] [blame] | 169 | if (gateway.IsValid()) { |
| 170 | routing_table_->SetDefaultRoute(interface_index_, gateway, |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 171 | GetMetric(is_default_)); |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 172 | } |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 173 | |
Paul Stewart | 3f68bb1 | 2012-03-15 13:33:10 -0700 | [diff] [blame] | 174 | // Install any explicitly configured routes at the default metric. |
| 175 | routing_table_->ConfigureRoutes(interface_index_, config, kDefaultMetric); |
| 176 | |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 177 | // 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 Stewart | 10241e3 | 2012-04-23 18:15:06 -0700 | [diff] [blame] | 183 | ipconfig_rpc_identifier_ = config->GetRpcIdentifier(); |
| 184 | |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 185 | if (is_default_) { |
Paul Stewart | bf66761 | 2012-06-29 14:49:54 -0700 | [diff] [blame] | 186 | resolver_->SetDNSFromIPConfig(config, dns_timeout_parameters_); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 187 | } |
Paul Stewart | 4a6748d | 2012-07-17 14:31:36 -0700 | [diff] [blame] | 188 | |
| 189 | local_ = local; |
| 190 | gateway_ = gateway; |
| 191 | has_broadcast_domain_ = !peer.IsValid(); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 194 | void Connection::SetIsDefault(bool is_default) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 195 | SLOG(Connection, 2) << __func__ << " " << interface_name_ |
| 196 | << " (index " << interface_index_ << ") " |
| 197 | << is_default_ << " -> " << is_default; |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 198 | if (is_default == is_default_) { |
| 199 | return; |
| 200 | } |
| 201 | |
Paul Stewart | 7cfca04 | 2011-12-08 14:18:17 -0800 | [diff] [blame] | 202 | routing_table_->SetDefaultMetric(interface_index_, GetMetric(is_default)); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 203 | |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 204 | is_default_ = is_default; |
| 205 | |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 206 | if (is_default) { |
Paul Stewart | bf66761 | 2012-06-29 14:49:54 -0700 | [diff] [blame] | 207 | resolver_->SetDNSFromLists(dns_servers_, dns_domain_search_, |
| 208 | dns_timeout_parameters_); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 209 | DeviceRefPtr device = device_info_->GetDevice(interface_index_); |
| 210 | if (device) { |
| 211 | device->RequestPortalDetection(); |
| 212 | } |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 213 | } |
Paul Stewart | e78ec54 | 2012-06-08 18:28:50 -0700 | [diff] [blame] | 214 | routing_table_->FlushCache(); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 215 | } |
| 216 | |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 217 | void 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 | |
| 229 | void 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 Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 246 | bool 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 Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 251 | // 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 Petkov | 5eb0542 | 2012-05-11 15:45:25 +0200 | [diff] [blame] | 260 | Bind(&Connection::OnRouteQueryResponse, |
| 261 | weak_ptr_factory_.GetWeakPtr()))) { |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 262 | LOG(ERROR) << "Could not request route to " << address.ToString(); |
| 263 | return false; |
| 264 | } |
| 265 | |
| 266 | return true; |
| 267 | } |
| 268 | |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 269 | // static |
Paul Stewart | 53a3038 | 2012-04-26 09:06:59 -0700 | [diff] [blame] | 270 | bool Connection::FixGatewayReachability(IPAddress *local, |
Paul Stewart | 4925829 | 2012-05-26 06:37:14 -0700 | [diff] [blame] | 271 | IPAddress *peer, |
| 272 | const IPAddress &gateway) { |
Paul Stewart | 53a3038 | 2012-04-26 09:06:59 -0700 | [diff] [blame] | 273 | if (!gateway.IsValid()) { |
| 274 | LOG(WARNING) << "No gateway address was provided for this connection."; |
| 275 | return false; |
| 276 | } |
| 277 | |
Paul Stewart | 4925829 | 2012-05-26 06:37:14 -0700 | [diff] [blame] | 278 | if (peer->IsValid()) { |
| 279 | if (gateway.Equals(*peer)) { |
Paul Stewart | 53a3038 | 2012-04-26 09:06:59 -0700 | [diff] [blame] | 280 | return true; |
| 281 | } |
| 282 | LOG(WARNING) << "Gateway address " |
| 283 | << gateway.ToString() |
| 284 | << " does not match peer address " |
Paul Stewart | 4925829 | 2012-05-26 06:37:14 -0700 | [diff] [blame] | 285 | << peer->ToString(); |
Paul Stewart | 53a3038 | 2012-04-26 09:06:59 -0700 | [diff] [blame] | 286 | return false; |
| 287 | } |
| 288 | |
| 289 | if (local->CanReachAddress(gateway)) { |
| 290 | return true; |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | LOG(WARNING) << "Gateway " |
| 294 | << gateway.ToString() |
| 295 | << " is unreachable from local address/prefix " |
| 296 | << local->ToString() << "/" << local->prefix(); |
| 297 | |
Paul Stewart | 2aa5d7d | 2012-06-21 22:16:54 -0700 | [diff] [blame] | 298 | bool found_new_prefix = false; |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 299 | size_t original_prefix = local->prefix(); |
Paul Stewart | 2aa5d7d | 2012-06-21 22:16:54 -0700 | [diff] [blame] | 300 | // 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 Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 311 | } |
| 312 | } |
| 313 | |
Paul Stewart | 2aa5d7d | 2012-06-21 22:16:54 -0700 | [diff] [blame] | 314 | if (!found_new_prefix) { |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 315 | // Restore the original prefix since we cannot find a better one. |
| 316 | local->set_prefix(original_prefix); |
Paul Stewart | 4925829 | 2012-05-26 06:37:14 -0700 | [diff] [blame] | 317 | DCHECK(!peer->IsValid()); |
| 318 | LOG(WARNING) << "Assuming point-to-point configuration."; |
| 319 | *peer = gateway; |
| 320 | return true; |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 321 | } |
Paul Stewart | 53a3038 | 2012-04-26 09:06:59 -0700 | [diff] [blame] | 322 | |
Paul Stewart | 2aa5d7d | 2012-06-21 22:16:54 -0700 | [diff] [blame] | 323 | LOG(WARNING) << "Mitigating this by setting local prefix to " |
| 324 | << local->prefix(); |
Paul Stewart | 53a3038 | 2012-04-26 09:06:59 -0700 | [diff] [blame] | 325 | return true; |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 326 | } |
| 327 | |
Paul Stewart | 7cfca04 | 2011-12-08 14:18:17 -0800 | [diff] [blame] | 328 | uint32 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 Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 335 | bool Connection::PinHostRoute(const IPConfig::Properties &properties) { |
| 336 | SLOG(Connection, 2) << __func__; |
| 337 | if (properties.gateway.empty() || properties.trusted_ip.empty()) { |
Darin Petkov | e8587e3 | 2012-07-02 13:56:07 +0200 | [diff] [blame] | 338 | 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 Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 342 | 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 Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 355 | void Connection::OnRouteQueryResponse(int interface_index, |
| 356 | const RoutingTableEntry &entry) { |
| 357 | SLOG(Connection, 2) << __func__ << "(" << interface_index << ", " |
Darin Petkov | 5eb0542 | 2012-05-11 15:45:25 +0200 | [diff] [blame] | 358 | << entry.tag << ")" << " @ " << interface_name_; |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 359 | 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 Stewart | 4a6748d | 2012-07-17 14:31:36 -0700 | [diff] [blame] | 371 | connection->CreateGatewayRoute(); |
| 372 | } |
| 373 | |
| 374 | bool 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 Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | void Connection::OnLowerDisconnect() { |
Darin Petkov | 5eb0542 | 2012-05-11 15:45:25 +0200 | [diff] [blame] | 389 | SLOG(Connection, 2) << __func__ << " @ " << interface_name_; |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 390 | // 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 | |
| 398 | void Connection::NotifyBindersOnDisconnect() { |
| 399 | // Note that this method may be invoked by the destructor. |
Darin Petkov | 5eb0542 | 2012-05-11 15:45:25 +0200 | [diff] [blame] | 400 | SLOG(Connection, 2) << __func__ << " @ " << interface_name_; |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 401 | |
| 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 | |
| 414 | void Connection::AttachBinder(Binder *binder) { |
Darin Petkov | 5eb0542 | 2012-05-11 15:45:25 +0200 | [diff] [blame] | 415 | SLOG(Connection, 2) << __func__ << "(" << binder->name() << ")" << " @ " |
| 416 | << interface_name_; |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 417 | binders_.push_back(binder); |
| 418 | } |
| 419 | |
| 420 | void Connection::DetachBinder(Binder *binder) { |
Darin Petkov | 5eb0542 | 2012-05-11 15:45:25 +0200 | [diff] [blame] | 421 | SLOG(Connection, 2) << __func__ << "(" << binder->name() << ")" << " @ " |
| 422 | << interface_name_; |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 423 | 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 Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 432 | } // namespace shill |