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