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; |
Paul Stewart | d62d603 | 2012-09-11 11:35:49 -0700 | [diff] [blame] | 21 | using std::vector; |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 22 | |
| 23 | namespace shill { |
| 24 | |
| 25 | // static |
| 26 | const uint32 Connection::kDefaultMetric = 1; |
| 27 | // static |
Paul Stewart | 7cfca04 | 2011-12-08 14:18:17 -0800 | [diff] [blame] | 28 | const uint32 Connection::kNonDefaultMetricBase = 10; |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 29 | |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 30 | Connection::Binder::Binder(const string &name, |
| 31 | const Closure &disconnect_callback) |
| 32 | : name_(name), |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 33 | client_disconnect_callback_(disconnect_callback) {} |
| 34 | |
| 35 | Connection::Binder::~Binder() { |
| 36 | Attach(NULL); |
| 37 | } |
| 38 | |
Darin Petkov | ef1f9fe | 2012-05-11 16:51:52 +0200 | [diff] [blame] | 39 | void Connection::Binder::Attach(const ConnectionRefPtr &to_connection) { |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 40 | if (connection_) { |
| 41 | connection_->DetachBinder(this); |
| 42 | LOG(INFO) << name_ << ": unbound from connection: " |
| 43 | << connection_->interface_name(); |
Darin Petkov | ef1f9fe | 2012-05-11 16:51:52 +0200 | [diff] [blame] | 44 | connection_.reset(); |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 45 | } |
Darin Petkov | ef1f9fe | 2012-05-11 16:51:52 +0200 | [diff] [blame] | 46 | if (to_connection) { |
| 47 | connection_ = to_connection->weak_ptr_factory_.GetWeakPtr(); |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 48 | connection_->AttachBinder(this); |
| 49 | LOG(INFO) << name_ << ": bound to connection: " |
| 50 | << connection_->interface_name(); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | void Connection::Binder::OnDisconnect() { |
| 55 | LOG(INFO) << name_ << ": bound connection disconnected: " |
| 56 | << connection_->interface_name(); |
Darin Petkov | ef1f9fe | 2012-05-11 16:51:52 +0200 | [diff] [blame] | 57 | connection_.reset(); |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 58 | if (!client_disconnect_callback_.is_null()) { |
| 59 | SLOG(Connection, 2) << "Running client disconnect callback."; |
| 60 | client_disconnect_callback_.Run(); |
| 61 | } |
| 62 | } |
| 63 | |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 64 | Connection::Connection(int interface_index, |
| 65 | const std::string& interface_name, |
Paul Stewart | e00600e | 2012-03-16 07:08:00 -0700 | [diff] [blame] | 66 | Technology::Identifier technology, |
Paul Stewart | bf66761 | 2012-06-29 14:49:54 -0700 | [diff] [blame] | 67 | const DeviceInfo *device_info, |
| 68 | bool is_short_dns_timeout_enabled) |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 69 | : weak_ptr_factory_(this), |
| 70 | is_default_(false), |
Paul Stewart | 4a6748d | 2012-07-17 14:31:36 -0700 | [diff] [blame] | 71 | has_broadcast_domain_(false), |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 72 | routing_request_count_(0), |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 73 | interface_index_(interface_index), |
| 74 | interface_name_(interface_name), |
Paul Stewart | e00600e | 2012-03-16 07:08:00 -0700 | [diff] [blame] | 75 | technology_(technology), |
Paul Stewart | 4a6748d | 2012-07-17 14:31:36 -0700 | [diff] [blame] | 76 | local_(IPAddress::kFamilyUnknown), |
| 77 | gateway_(IPAddress::kFamilyUnknown), |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 78 | 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 Stewart | bf66761 | 2012-06-29 14:49:54 -0700 | [diff] [blame] | 83 | dns_timeout_parameters_(Resolver::kDefaultTimeout), |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 84 | device_info_(device_info), |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 85 | resolver_(Resolver::GetInstance()), |
| 86 | routing_table_(RoutingTable::GetInstance()), |
| 87 | rtnl_handler_(RTNLHandler::GetInstance()) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 88 | SLOG(Connection, 2) << __func__ << "(" << interface_index << ", " |
| 89 | << interface_name << ", " |
| 90 | << Technology::NameFromIdentifier(technology) << ")"; |
Paul Stewart | bf66761 | 2012-06-29 14:49:54 -0700 | [diff] [blame] | 91 | if (is_short_dns_timeout_enabled) { |
| 92 | dns_timeout_parameters_ = Resolver::kShortTimeout; |
| 93 | } |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | Connection::~Connection() { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 97 | SLOG(Connection, 2) << __func__ << " " << interface_name_; |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 98 | |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 99 | NotifyBindersOnDisconnect(); |
| 100 | |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 101 | DCHECK(!routing_request_count_); |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 102 | routing_table_->FlushRoutes(interface_index_); |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 103 | routing_table_->FlushRoutesWithTag(interface_index_); |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 104 | device_info_->FlushAddresses(interface_index_); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | void Connection::UpdateFromIPConfig(const IPConfigRefPtr &config) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 108 | SLOG(Connection, 2) << __func__ << " " << interface_name_; |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 109 | |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 110 | const IPConfig::Properties &properties = config->properties(); |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 111 | if (!properties.trusted_ip.empty() && !PinHostRoute(properties)) { |
| 112 | LOG(ERROR) << "Unable to pin host route to " << properties.trusted_ip; |
| 113 | return; |
| 114 | } |
| 115 | |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 116 | IPAddress local(properties.address_family); |
| 117 | if (!local.SetAddressFromString(properties.address)) { |
| 118 | LOG(ERROR) << "Local address " << properties.address << " is invalid"; |
| 119 | return; |
| 120 | } |
Paul Stewart | 48100b0 | 2012-03-19 07:53:52 -0700 | [diff] [blame] | 121 | local.set_prefix(properties.subnet_prefix); |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 122 | |
| 123 | IPAddress broadcast(properties.address_family); |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 124 | if (properties.broadcast_address.empty()) { |
Paul Stewart | fe1c0e1 | 2012-04-30 19:57:04 -0700 | [diff] [blame] | 125 | if (properties.peer_address.empty()) { |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 126 | LOG(WARNING) << "Broadcast address is not set. Using default."; |
Paul Stewart | fe1c0e1 | 2012-04-30 19:57:04 -0700 | [diff] [blame] | 127 | broadcast = local.GetDefaultBroadcast(); |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 128 | } |
| 129 | } else if (!broadcast.SetAddressFromString(properties.broadcast_address)) { |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 130 | LOG(ERROR) << "Broadcast address " << properties.broadcast_address |
| 131 | << " is invalid"; |
| 132 | return; |
| 133 | } |
| 134 | |
Paul Stewart | 48100b0 | 2012-03-19 07:53:52 -0700 | [diff] [blame] | 135 | IPAddress peer(properties.address_family); |
| 136 | if (!properties.peer_address.empty() && |
| 137 | !peer.SetAddressFromString(properties.peer_address)) { |
| 138 | LOG(ERROR) << "Peer address " << properties.peer_address |
| 139 | << " is invalid"; |
| 140 | return; |
| 141 | } |
| 142 | |
Paul Stewart | e78ec54 | 2012-06-08 18:28:50 -0700 | [diff] [blame] | 143 | IPAddress gateway(properties.address_family); |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 144 | if (!properties.gateway.empty() && |
Paul Stewart | e78ec54 | 2012-06-08 18:28:50 -0700 | [diff] [blame] | 145 | !gateway.SetAddressFromString(properties.gateway)) { |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 146 | LOG(ERROR) << "Gateway address " << properties.peer_address |
| 147 | << " is invalid"; |
| 148 | return; |
| 149 | } |
| 150 | |
Paul Stewart | e78ec54 | 2012-06-08 18:28:50 -0700 | [diff] [blame] | 151 | if (!FixGatewayReachability(&local, &peer, gateway)) { |
Paul Stewart | 53a3038 | 2012-04-26 09:06:59 -0700 | [diff] [blame] | 152 | LOG(WARNING) << "Expect limited network connectivity."; |
| 153 | } |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 154 | |
Paul Stewart | 05a42c2 | 2012-08-02 16:47:21 -0700 | [diff] [blame] | 155 | if (device_info_->HasOtherAddress(interface_index_, local)) { |
| 156 | // The address has changed for this interface. We need to flush |
| 157 | // everything and start over. |
| 158 | LOG(INFO) << __func__ << ": Flushing old addresses and routes."; |
| 159 | routing_table_->FlushRoutes(interface_index_); |
| 160 | device_info_->FlushAddresses(interface_index_); |
| 161 | } |
| 162 | |
Paul Stewart | e78ec54 | 2012-06-08 18:28:50 -0700 | [diff] [blame] | 163 | LOG(INFO) << __func__ << ": Installing with parameters:" |
| 164 | << " local=" << local.ToString() |
| 165 | << " broadcast=" << broadcast.ToString() |
| 166 | << " peer=" << peer.ToString() |
| 167 | << " gateway=" << gateway.ToString(); |
Paul Stewart | 48100b0 | 2012-03-19 07:53:52 -0700 | [diff] [blame] | 168 | rtnl_handler_->AddInterfaceAddress(interface_index_, local, broadcast, peer); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 169 | |
Paul Stewart | e78ec54 | 2012-06-08 18:28:50 -0700 | [diff] [blame] | 170 | if (gateway.IsValid()) { |
| 171 | routing_table_->SetDefaultRoute(interface_index_, gateway, |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 172 | GetMetric(is_default_)); |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 173 | } |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 174 | |
Paul Stewart | 3f68bb1 | 2012-03-15 13:33:10 -0700 | [diff] [blame] | 175 | // Install any explicitly configured routes at the default metric. |
| 176 | routing_table_->ConfigureRoutes(interface_index_, config, kDefaultMetric); |
| 177 | |
Paul Stewart | d62d603 | 2012-09-11 11:35:49 -0700 | [diff] [blame] | 178 | // Save a copy of the last non-null DNS config. |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 179 | if (!config->properties().dns_servers.empty()) { |
| 180 | dns_servers_ = config->properties().dns_servers; |
Paul Stewart | d62d603 | 2012-09-11 11:35:49 -0700 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | if (!config->properties().domain_search.empty()) { |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 184 | dns_domain_search_ = config->properties().domain_search; |
Paul Stewart | d62d603 | 2012-09-11 11:35:49 -0700 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | if (!config->properties().domain_name.empty()) { |
| 188 | dns_domain_name_ = config->properties().domain_name; |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Paul Stewart | 10241e3 | 2012-04-23 18:15:06 -0700 | [diff] [blame] | 191 | ipconfig_rpc_identifier_ = config->GetRpcIdentifier(); |
| 192 | |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 193 | if (is_default_) { |
Paul Stewart | 6f65c0b | 2012-09-11 14:57:32 -0700 | [diff] [blame] | 194 | PushDNSConfig(); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 195 | } |
Paul Stewart | 4a6748d | 2012-07-17 14:31:36 -0700 | [diff] [blame] | 196 | |
| 197 | local_ = local; |
| 198 | gateway_ = gateway; |
| 199 | has_broadcast_domain_ = !peer.IsValid(); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 202 | void Connection::SetIsDefault(bool is_default) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 203 | SLOG(Connection, 2) << __func__ << " " << interface_name_ |
| 204 | << " (index " << interface_index_ << ") " |
| 205 | << is_default_ << " -> " << is_default; |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 206 | if (is_default == is_default_) { |
| 207 | return; |
| 208 | } |
| 209 | |
Paul Stewart | 7cfca04 | 2011-12-08 14:18:17 -0800 | [diff] [blame] | 210 | routing_table_->SetDefaultMetric(interface_index_, GetMetric(is_default)); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 211 | |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 212 | is_default_ = is_default; |
| 213 | |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 214 | if (is_default) { |
Paul Stewart | 6f65c0b | 2012-09-11 14:57:32 -0700 | [diff] [blame] | 215 | PushDNSConfig(); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 216 | DeviceRefPtr device = device_info_->GetDevice(interface_index_); |
| 217 | if (device) { |
| 218 | device->RequestPortalDetection(); |
| 219 | } |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 220 | } |
Paul Stewart | e78ec54 | 2012-06-08 18:28:50 -0700 | [diff] [blame] | 221 | routing_table_->FlushCache(); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 222 | } |
| 223 | |
Paul Stewart | 6f65c0b | 2012-09-11 14:57:32 -0700 | [diff] [blame] | 224 | void Connection::PushDNSConfig() { |
| 225 | vector<string> domain_search = dns_domain_search_; |
| 226 | if (domain_search.empty() && !dns_domain_name_.empty()) { |
| 227 | SLOG(Connection, 2) << "Setting domain search to domain name " |
| 228 | << dns_domain_name_; |
| 229 | domain_search.push_back(dns_domain_name_ + "."); |
| 230 | } |
| 231 | resolver_->SetDNSFromLists(dns_servers_, domain_search, |
| 232 | dns_timeout_parameters_); |
| 233 | } |
| 234 | |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 235 | void Connection::RequestRouting() { |
| 236 | if (routing_request_count_++ == 0) { |
| 237 | DeviceRefPtr device = device_info_->GetDevice(interface_index_); |
| 238 | DCHECK(device.get()); |
| 239 | if (!device.get()) { |
| 240 | LOG(ERROR) << "Device is NULL!"; |
| 241 | return; |
| 242 | } |
| 243 | device->DisableReversePathFilter(); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | void Connection::ReleaseRouting() { |
| 248 | DCHECK(routing_request_count_ > 0); |
| 249 | if (--routing_request_count_ == 0) { |
| 250 | DeviceRefPtr device = device_info_->GetDevice(interface_index_); |
| 251 | DCHECK(device.get()); |
| 252 | if (!device.get()) { |
| 253 | LOG(ERROR) << "Device is NULL!"; |
| 254 | return; |
| 255 | } |
| 256 | device->EnableReversePathFilter(); |
| 257 | |
| 258 | // Clear any cached routes that might have accumulated while reverse-path |
| 259 | // filtering was disabled. |
| 260 | routing_table_->FlushCache(); |
| 261 | } |
| 262 | } |
| 263 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 264 | bool Connection::RequestHostRoute(const IPAddress &address) { |
| 265 | // Set the prefix to be the entire address size. |
| 266 | IPAddress address_prefix(address); |
| 267 | address_prefix.set_prefix(address_prefix.GetLength() * 8); |
| 268 | |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 269 | // Do not set interface_index_ since this may not be the default route through |
| 270 | // which this destination can be found. However, we should tag the created |
| 271 | // route with our interface index so we can clean this route up when this |
| 272 | // connection closes. Also, add route query callback to determine the lower |
| 273 | // connection and bind to it. |
| 274 | if (!routing_table_->RequestRouteToHost( |
| 275 | address_prefix, |
| 276 | -1, |
| 277 | interface_index_, |
Darin Petkov | 5eb0542 | 2012-05-11 15:45:25 +0200 | [diff] [blame] | 278 | Bind(&Connection::OnRouteQueryResponse, |
| 279 | weak_ptr_factory_.GetWeakPtr()))) { |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 280 | LOG(ERROR) << "Could not request route to " << address.ToString(); |
| 281 | return false; |
| 282 | } |
| 283 | |
| 284 | return true; |
| 285 | } |
| 286 | |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 287 | // static |
Paul Stewart | 53a3038 | 2012-04-26 09:06:59 -0700 | [diff] [blame] | 288 | bool Connection::FixGatewayReachability(IPAddress *local, |
Paul Stewart | 4925829 | 2012-05-26 06:37:14 -0700 | [diff] [blame] | 289 | IPAddress *peer, |
| 290 | const IPAddress &gateway) { |
Paul Stewart | 53a3038 | 2012-04-26 09:06:59 -0700 | [diff] [blame] | 291 | if (!gateway.IsValid()) { |
| 292 | LOG(WARNING) << "No gateway address was provided for this connection."; |
| 293 | return false; |
| 294 | } |
| 295 | |
Paul Stewart | 4925829 | 2012-05-26 06:37:14 -0700 | [diff] [blame] | 296 | if (peer->IsValid()) { |
| 297 | if (gateway.Equals(*peer)) { |
Paul Stewart | 53a3038 | 2012-04-26 09:06:59 -0700 | [diff] [blame] | 298 | return true; |
| 299 | } |
| 300 | LOG(WARNING) << "Gateway address " |
| 301 | << gateway.ToString() |
| 302 | << " does not match peer address " |
Paul Stewart | 4925829 | 2012-05-26 06:37:14 -0700 | [diff] [blame] | 303 | << peer->ToString(); |
Paul Stewart | 53a3038 | 2012-04-26 09:06:59 -0700 | [diff] [blame] | 304 | return false; |
| 305 | } |
| 306 | |
| 307 | if (local->CanReachAddress(gateway)) { |
| 308 | return true; |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | LOG(WARNING) << "Gateway " |
| 312 | << gateway.ToString() |
| 313 | << " is unreachable from local address/prefix " |
| 314 | << local->ToString() << "/" << local->prefix(); |
| 315 | |
Paul Stewart | 2aa5d7d | 2012-06-21 22:16:54 -0700 | [diff] [blame] | 316 | bool found_new_prefix = false; |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 317 | size_t original_prefix = local->prefix(); |
Paul Stewart | 2aa5d7d | 2012-06-21 22:16:54 -0700 | [diff] [blame] | 318 | // Only try to expand the netmask if the configured prefix is |
| 319 | // less than "all ones". This special-cases the "all-ones" |
| 320 | // prefix as a forced conversion to point-to-point networking. |
| 321 | if (local->prefix() < IPAddress::GetMaxPrefixLength(local->family())) { |
| 322 | size_t prefix = original_prefix - 1; |
| 323 | for (; prefix >= local->GetMinPrefixLength(); --prefix) { |
| 324 | local->set_prefix(prefix); |
| 325 | if (local->CanReachAddress(gateway)) { |
| 326 | found_new_prefix = true; |
| 327 | break; |
| 328 | } |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 329 | } |
| 330 | } |
| 331 | |
Paul Stewart | 2aa5d7d | 2012-06-21 22:16:54 -0700 | [diff] [blame] | 332 | if (!found_new_prefix) { |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 333 | // Restore the original prefix since we cannot find a better one. |
| 334 | local->set_prefix(original_prefix); |
Paul Stewart | 4925829 | 2012-05-26 06:37:14 -0700 | [diff] [blame] | 335 | DCHECK(!peer->IsValid()); |
| 336 | LOG(WARNING) << "Assuming point-to-point configuration."; |
| 337 | *peer = gateway; |
| 338 | return true; |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 339 | } |
Paul Stewart | 53a3038 | 2012-04-26 09:06:59 -0700 | [diff] [blame] | 340 | |
Paul Stewart | 2aa5d7d | 2012-06-21 22:16:54 -0700 | [diff] [blame] | 341 | LOG(WARNING) << "Mitigating this by setting local prefix to " |
| 342 | << local->prefix(); |
Paul Stewart | 53a3038 | 2012-04-26 09:06:59 -0700 | [diff] [blame] | 343 | return true; |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 344 | } |
| 345 | |
Paul Stewart | 7cfca04 | 2011-12-08 14:18:17 -0800 | [diff] [blame] | 346 | uint32 Connection::GetMetric(bool is_default) { |
| 347 | // If this is not the default route, assign a metric based on the interface |
| 348 | // index. This way all non-default routes (even to the same gateway IP) end |
| 349 | // up with unique metrics so they do not collide. |
| 350 | return is_default ? kDefaultMetric : kNonDefaultMetricBase + interface_index_; |
| 351 | } |
| 352 | |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 353 | bool Connection::PinHostRoute(const IPConfig::Properties &properties) { |
| 354 | SLOG(Connection, 2) << __func__; |
| 355 | if (properties.gateway.empty() || properties.trusted_ip.empty()) { |
Darin Petkov | e8587e3 | 2012-07-02 13:56:07 +0200 | [diff] [blame] | 356 | LOG_IF(ERROR, properties.gateway.empty()) |
| 357 | << "No gateway -- unable to pin host route."; |
| 358 | LOG_IF(ERROR, properties.trusted_ip.empty()) |
| 359 | << "No trusted IP -- unable to pin host route."; |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 360 | return false; |
| 361 | } |
| 362 | |
| 363 | IPAddress trusted_ip(properties.address_family); |
| 364 | if (!trusted_ip.SetAddressFromString(properties.trusted_ip)) { |
| 365 | LOG(ERROR) << "Failed to parse trusted_ip " |
| 366 | << properties.trusted_ip << "; ignored."; |
| 367 | return false; |
| 368 | } |
| 369 | |
| 370 | return RequestHostRoute(trusted_ip); |
| 371 | } |
| 372 | |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 373 | void Connection::OnRouteQueryResponse(int interface_index, |
| 374 | const RoutingTableEntry &entry) { |
| 375 | SLOG(Connection, 2) << __func__ << "(" << interface_index << ", " |
Darin Petkov | 5eb0542 | 2012-05-11 15:45:25 +0200 | [diff] [blame] | 376 | << entry.tag << ")" << " @ " << interface_name_; |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 377 | lower_binder_.Attach(NULL); |
| 378 | DeviceRefPtr device = device_info_->GetDevice(interface_index); |
| 379 | if (!device) { |
| 380 | LOG(ERROR) << "Unable to lookup device for index " << interface_index; |
| 381 | return; |
| 382 | } |
| 383 | ConnectionRefPtr connection = device->connection(); |
| 384 | if (!connection) { |
| 385 | LOG(ERROR) << "Device " << interface_index << " has no connection."; |
| 386 | return; |
| 387 | } |
| 388 | lower_binder_.Attach(connection); |
Paul Stewart | 4a6748d | 2012-07-17 14:31:36 -0700 | [diff] [blame] | 389 | connection->CreateGatewayRoute(); |
| 390 | } |
| 391 | |
| 392 | bool Connection::CreateGatewayRoute() { |
| 393 | // Ensure that the gateway for the lower connection remains reachable, |
| 394 | // since we may create routes that conflict with it. |
| 395 | if (!has_broadcast_domain_) { |
| 396 | return false; |
| 397 | } |
| 398 | // It is not worth keeping track of this route, since it is benign, |
| 399 | // and only pins persistent state that was already true of the connection. |
| 400 | // If DHCP parameters change later (without the connection having been |
| 401 | // destroyed and recreated), the binding processes will likely terminate |
| 402 | // and restart, causing a new link route to be created. |
| 403 | return routing_table_->CreateLinkRoute(interface_index_, local_, gateway_); |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | void Connection::OnLowerDisconnect() { |
Darin Petkov | 5eb0542 | 2012-05-11 15:45:25 +0200 | [diff] [blame] | 407 | SLOG(Connection, 2) << __func__ << " @ " << interface_name_; |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 408 | // Ensures that |this| instance doesn't get destroyed in the middle of |
| 409 | // notifying the binders. This method needs to be separate from |
| 410 | // NotifyBindersOnDisconnect because the latter may be invoked by Connection's |
| 411 | // destructor when |this| instance's reference count is already 0. |
| 412 | ConnectionRefPtr connection(this); |
| 413 | connection->NotifyBindersOnDisconnect(); |
| 414 | } |
| 415 | |
| 416 | void Connection::NotifyBindersOnDisconnect() { |
| 417 | // Note that this method may be invoked by the destructor. |
Darin Petkov | 5eb0542 | 2012-05-11 15:45:25 +0200 | [diff] [blame] | 418 | SLOG(Connection, 2) << __func__ << " @ " << interface_name_; |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 419 | |
| 420 | // Unbinds the lower connection before notifying the binders. This ensures |
| 421 | // correct behavior in case of circular binding. |
| 422 | lower_binder_.Attach(NULL); |
| 423 | while (!binders_.empty()) { |
| 424 | // Pop the binder first and then notify it to ensure that each binder is |
| 425 | // notified only once. |
| 426 | Binder *binder = binders_.front(); |
| 427 | binders_.pop_front(); |
| 428 | binder->OnDisconnect(); |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | void Connection::AttachBinder(Binder *binder) { |
Darin Petkov | 5eb0542 | 2012-05-11 15:45:25 +0200 | [diff] [blame] | 433 | SLOG(Connection, 2) << __func__ << "(" << binder->name() << ")" << " @ " |
| 434 | << interface_name_; |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 435 | binders_.push_back(binder); |
| 436 | } |
| 437 | |
| 438 | void Connection::DetachBinder(Binder *binder) { |
Darin Petkov | 5eb0542 | 2012-05-11 15:45:25 +0200 | [diff] [blame] | 439 | SLOG(Connection, 2) << __func__ << "(" << binder->name() << ")" << " @ " |
| 440 | << interface_name_; |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 441 | for (deque<Binder *>::iterator it = binders_.begin(); |
| 442 | it != binders_.end(); ++it) { |
| 443 | if (binder == *it) { |
| 444 | binders_.erase(it); |
| 445 | return; |
| 446 | } |
| 447 | } |
| 448 | } |
| 449 | |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 450 | } // namespace shill |