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 | |
| 16 | using std::string; |
| 17 | |
| 18 | namespace shill { |
| 19 | |
| 20 | // static |
| 21 | const uint32 Connection::kDefaultMetric = 1; |
| 22 | // static |
Paul Stewart | 7cfca04 | 2011-12-08 14:18:17 -0800 | [diff] [blame] | 23 | const uint32 Connection::kNonDefaultMetricBase = 10; |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 24 | |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 25 | Connection::Connection(int interface_index, |
| 26 | const std::string& interface_name, |
Paul Stewart | e00600e | 2012-03-16 07:08:00 -0700 | [diff] [blame] | 27 | Technology::Identifier technology, |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 28 | const DeviceInfo *device_info) |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 29 | : is_default_(false), |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 30 | routing_request_count_(0), |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 31 | interface_index_(interface_index), |
| 32 | interface_name_(interface_name), |
Paul Stewart | e00600e | 2012-03-16 07:08:00 -0700 | [diff] [blame] | 33 | technology_(technology), |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 34 | device_info_(device_info), |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 35 | resolver_(Resolver::GetInstance()), |
| 36 | routing_table_(RoutingTable::GetInstance()), |
| 37 | rtnl_handler_(RTNLHandler::GetInstance()) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 38 | SLOG(Connection, 2) << __func__ << "(" << interface_index << ", " |
| 39 | << interface_name << ", " |
| 40 | << Technology::NameFromIdentifier(technology) << ")"; |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | Connection::~Connection() { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 44 | SLOG(Connection, 2) << __func__ << " " << interface_name_; |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 45 | |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 46 | DCHECK(!routing_request_count_); |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 47 | routing_table_->FlushRoutes(interface_index_); |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 48 | routing_table_->FlushRoutesWithTag(interface_index_); |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 49 | device_info_->FlushAddresses(interface_index_); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | void Connection::UpdateFromIPConfig(const IPConfigRefPtr &config) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 53 | SLOG(Connection, 2) << __func__ << " " << interface_name_; |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 54 | |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 55 | const IPConfig::Properties &properties = config->properties(); |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 56 | if (!properties.trusted_ip.empty() && !PinHostRoute(properties)) { |
| 57 | LOG(ERROR) << "Unable to pin host route to " << properties.trusted_ip; |
| 58 | return; |
| 59 | } |
| 60 | |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 61 | IPAddress local(properties.address_family); |
| 62 | if (!local.SetAddressFromString(properties.address)) { |
| 63 | LOG(ERROR) << "Local address " << properties.address << " is invalid"; |
| 64 | return; |
| 65 | } |
Paul Stewart | 48100b0 | 2012-03-19 07:53:52 -0700 | [diff] [blame] | 66 | local.set_prefix(properties.subnet_prefix); |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 67 | |
| 68 | IPAddress broadcast(properties.address_family); |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 69 | if (properties.broadcast_address.empty()) { |
Paul Stewart | fe1c0e1 | 2012-04-30 19:57:04 -0700 | [diff] [blame^] | 70 | if (properties.peer_address.empty()) { |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 71 | LOG(WARNING) << "Broadcast address is not set. Using default."; |
Paul Stewart | fe1c0e1 | 2012-04-30 19:57:04 -0700 | [diff] [blame^] | 72 | broadcast = local.GetDefaultBroadcast(); |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 73 | } |
| 74 | } else if (!broadcast.SetAddressFromString(properties.broadcast_address)) { |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 75 | LOG(ERROR) << "Broadcast address " << properties.broadcast_address |
| 76 | << " is invalid"; |
| 77 | return; |
| 78 | } |
| 79 | |
Paul Stewart | 48100b0 | 2012-03-19 07:53:52 -0700 | [diff] [blame] | 80 | IPAddress peer(properties.address_family); |
| 81 | if (!properties.peer_address.empty() && |
| 82 | !peer.SetAddressFromString(properties.peer_address)) { |
| 83 | LOG(ERROR) << "Peer address " << properties.peer_address |
| 84 | << " is invalid"; |
| 85 | return; |
| 86 | } |
| 87 | |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 88 | IPAddress gateway_address(properties.address_family); |
| 89 | if (!properties.gateway.empty() && |
| 90 | !gateway_address.SetAddressFromString(properties.gateway)) { |
| 91 | LOG(ERROR) << "Gateway address " << properties.peer_address |
| 92 | << " is invalid"; |
| 93 | return; |
| 94 | } |
| 95 | |
Paul Stewart | 53a3038 | 2012-04-26 09:06:59 -0700 | [diff] [blame] | 96 | if (!FixGatewayReachability(&local, gateway_address, peer)) { |
| 97 | LOG(WARNING) << "Expect limited network connectivity."; |
| 98 | } |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 99 | |
Paul Stewart | 48100b0 | 2012-03-19 07:53:52 -0700 | [diff] [blame] | 100 | rtnl_handler_->AddInterfaceAddress(interface_index_, local, broadcast, peer); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 101 | |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 102 | if (gateway_address.IsValid()) { |
| 103 | routing_table_->SetDefaultRoute(interface_index_, gateway_address, |
| 104 | GetMetric(is_default_)); |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 105 | } |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 106 | |
Paul Stewart | 3f68bb1 | 2012-03-15 13:33:10 -0700 | [diff] [blame] | 107 | // Install any explicitly configured routes at the default metric. |
| 108 | routing_table_->ConfigureRoutes(interface_index_, config, kDefaultMetric); |
| 109 | |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 110 | // Save a copy of the last non-null DNS config |
| 111 | if (!config->properties().dns_servers.empty()) { |
| 112 | dns_servers_ = config->properties().dns_servers; |
| 113 | dns_domain_search_ = config->properties().domain_search; |
| 114 | } |
| 115 | |
Paul Stewart | 10241e3 | 2012-04-23 18:15:06 -0700 | [diff] [blame] | 116 | ipconfig_rpc_identifier_ = config->GetRpcIdentifier(); |
| 117 | |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 118 | if (is_default_) { |
| 119 | resolver_->SetDNSFromIPConfig(config); |
| 120 | } |
| 121 | } |
| 122 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 123 | void Connection::SetIsDefault(bool is_default) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 124 | SLOG(Connection, 2) << __func__ << " " << interface_name_ |
| 125 | << " (index " << interface_index_ << ") " |
| 126 | << is_default_ << " -> " << is_default; |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 127 | if (is_default == is_default_) { |
| 128 | return; |
| 129 | } |
| 130 | |
Paul Stewart | 7cfca04 | 2011-12-08 14:18:17 -0800 | [diff] [blame] | 131 | routing_table_->SetDefaultMetric(interface_index_, GetMetric(is_default)); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 132 | |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 133 | is_default_ = is_default; |
| 134 | |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 135 | if (is_default) { |
| 136 | resolver_->SetDNSFromLists(dns_servers_, dns_domain_search_); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 137 | DeviceRefPtr device = device_info_->GetDevice(interface_index_); |
| 138 | if (device) { |
| 139 | device->RequestPortalDetection(); |
| 140 | } |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 141 | } |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 144 | void Connection::RequestRouting() { |
| 145 | if (routing_request_count_++ == 0) { |
| 146 | DeviceRefPtr device = device_info_->GetDevice(interface_index_); |
| 147 | DCHECK(device.get()); |
| 148 | if (!device.get()) { |
| 149 | LOG(ERROR) << "Device is NULL!"; |
| 150 | return; |
| 151 | } |
| 152 | device->DisableReversePathFilter(); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | void Connection::ReleaseRouting() { |
| 157 | DCHECK(routing_request_count_ > 0); |
| 158 | if (--routing_request_count_ == 0) { |
| 159 | DeviceRefPtr device = device_info_->GetDevice(interface_index_); |
| 160 | DCHECK(device.get()); |
| 161 | if (!device.get()) { |
| 162 | LOG(ERROR) << "Device is NULL!"; |
| 163 | return; |
| 164 | } |
| 165 | device->EnableReversePathFilter(); |
| 166 | |
| 167 | // Clear any cached routes that might have accumulated while reverse-path |
| 168 | // filtering was disabled. |
| 169 | routing_table_->FlushCache(); |
| 170 | } |
| 171 | } |
| 172 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 173 | bool Connection::RequestHostRoute(const IPAddress &address) { |
| 174 | // Set the prefix to be the entire address size. |
| 175 | IPAddress address_prefix(address); |
| 176 | address_prefix.set_prefix(address_prefix.GetLength() * 8); |
| 177 | |
Paul Stewart | 536820d | 2012-03-19 16:05:59 -0700 | [diff] [blame] | 178 | // Do not set interface_index_ since this may not be the |
| 179 | // default route through which this destination can be found. |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 180 | // However, we should tag the created route with our interface |
| 181 | // index so we can clean this route up when this connection closes. |
| 182 | if (!routing_table_->RequestRouteToHost(address_prefix, -1, |
| 183 | interface_index_)) { |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 184 | LOG(ERROR) << "Could not request route to " << address.ToString(); |
| 185 | return false; |
| 186 | } |
| 187 | |
| 188 | return true; |
| 189 | } |
| 190 | |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 191 | // static |
Paul Stewart | 53a3038 | 2012-04-26 09:06:59 -0700 | [diff] [blame] | 192 | bool Connection::FixGatewayReachability(IPAddress *local, |
| 193 | const IPAddress &gateway, |
| 194 | const IPAddress &peer) { |
| 195 | if (!gateway.IsValid()) { |
| 196 | LOG(WARNING) << "No gateway address was provided for this connection."; |
| 197 | return false; |
| 198 | } |
| 199 | |
| 200 | if (peer.IsValid()) { |
| 201 | if (gateway.Equals(peer)) { |
| 202 | return true; |
| 203 | } |
| 204 | LOG(WARNING) << "Gateway address " |
| 205 | << gateway.ToString() |
| 206 | << " does not match peer address " |
| 207 | << peer.ToString(); |
| 208 | return false; |
| 209 | } |
| 210 | |
| 211 | if (local->CanReachAddress(gateway)) { |
| 212 | return true; |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | LOG(WARNING) << "Gateway " |
| 216 | << gateway.ToString() |
| 217 | << " is unreachable from local address/prefix " |
| 218 | << local->ToString() << "/" << local->prefix(); |
| 219 | |
| 220 | size_t original_prefix = local->prefix(); |
| 221 | size_t prefix = original_prefix - 1; |
| 222 | for (; prefix >= local->GetMinPrefixLength(); --prefix) { |
| 223 | local->set_prefix(prefix); |
| 224 | if (local->CanReachAddress(gateway)) { |
| 225 | break; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | if (prefix < local->GetMinPrefixLength()) { |
| 230 | // Restore the original prefix since we cannot find a better one. |
| 231 | local->set_prefix(original_prefix); |
Paul Stewart | 53a3038 | 2012-04-26 09:06:59 -0700 | [diff] [blame] | 232 | return false; |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 233 | } |
Paul Stewart | 53a3038 | 2012-04-26 09:06:59 -0700 | [diff] [blame] | 234 | |
| 235 | LOG(WARNING) << "Mitigating this by setting local prefix to " << prefix; |
| 236 | return true; |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 237 | } |
| 238 | |
Paul Stewart | 7cfca04 | 2011-12-08 14:18:17 -0800 | [diff] [blame] | 239 | uint32 Connection::GetMetric(bool is_default) { |
| 240 | // If this is not the default route, assign a metric based on the interface |
| 241 | // index. This way all non-default routes (even to the same gateway IP) end |
| 242 | // up with unique metrics so they do not collide. |
| 243 | return is_default ? kDefaultMetric : kNonDefaultMetricBase + interface_index_; |
| 244 | } |
| 245 | |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 246 | bool Connection::PinHostRoute(const IPConfig::Properties &properties) { |
| 247 | SLOG(Connection, 2) << __func__; |
| 248 | if (properties.gateway.empty() || properties.trusted_ip.empty()) { |
| 249 | return false; |
| 250 | } |
| 251 | |
| 252 | IPAddress trusted_ip(properties.address_family); |
| 253 | if (!trusted_ip.SetAddressFromString(properties.trusted_ip)) { |
| 254 | LOG(ERROR) << "Failed to parse trusted_ip " |
| 255 | << properties.trusted_ip << "; ignored."; |
| 256 | return false; |
| 257 | } |
| 258 | |
| 259 | return RequestHostRoute(trusted_ip); |
| 260 | } |
| 261 | |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 262 | } // namespace shill |