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